root/drivers/char/serial.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. serial_paranoia_check
  2. serial_in
  3. serial_inp
  4. serial_out
  5. serial_outp
  6. rs_stop
  7. rs_start
  8. rs_probe
  9. rs_sched_event
  10. receive_chars
  11. transmit_chars
  12. check_modem_status
  13. rs_interrupt
  14. rs_interrupt_single
  15. rs_interrupt_multi
  16. do_serial_bh
  17. do_softint
  18. do_serial_hangup
  19. rs_timer
  20. grab_all_interrupts
  21. free_all_interrupts
  22. figure_IRQ_timeout
  23. startup
  24. shutdown
  25. change_speed
  26. rs_put_char
  27. rs_flush_chars
  28. rs_write
  29. rs_write_room
  30. rs_chars_in_buffer
  31. rs_flush_buffer
  32. rs_throttle
  33. rs_unthrottle
  34. get_serial_info
  35. set_serial_info
  36. get_lsr_info
  37. get_modem_info
  38. set_modem_info
  39. do_autoconfig
  40. send_break
  41. check_wild_interrupts
  42. get_multiport_struct
  43. set_multiport_struct
  44. rs_ioctl
  45. rs_set_termios
  46. rs_close
  47. rs_hangup
  48. block_til_ready
  49. rs_open
  50. show_serial_version
  51. get_auto_irq
  52. do_auto_irq
  53. autoconfig
  54. rs_init
  55. register_serial
  56. unregister_serial

   1 /*
   2  *  linux/drivers/char/serial.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  *
   6  *  Extensively rewritten by Theodore Ts'o, 8/16/92 -- 9/14/92.  Now
   7  *  much more extensible to support other serial cards based on the
   8  *  16450/16550A UART's.  Added support for the AST FourPort and the
   9  *  Accent Async board.  
  10  *
  11  *  set_serial_info fixed to set the flags, custom divisor, and uart
  12  *      type fields.  Fix suggested by Michael K. Johnson 12/12/92.
  13  *
  14  * This module exports the following rs232 io functions:
  15  *
  16  *      long rs_init(long);
  17  *      int  rs_open(struct tty_struct * tty, struct file * filp)
  18  */
  19 
  20 #include <linux/errno.h>
  21 #include <linux/signal.h>
  22 #include <linux/sched.h>
  23 #include <linux/timer.h>
  24 #include <linux/interrupt.h>
  25 #include <linux/tty.h>
  26 #include <linux/tty_flip.h>
  27 #include <linux/serial.h>
  28 #include <linux/serial_reg.h>
  29 #include <linux/config.h>
  30 #include <linux/major.h>
  31 #include <linux/string.h>
  32 #include <linux/fcntl.h>
  33 #include <linux/ptrace.h>
  34 #include <linux/major.h>
  35 #include <linux/ioport.h>
  36 #include <linux/mm.h>
  37 
  38 #include <asm/system.h>
  39 #include <asm/io.h>
  40 #include <asm/segment.h>
  41 #include <asm/bitops.h>
  42 
  43 DECLARE_TASK_QUEUE(tq_serial);
  44 
  45 struct tty_driver serial_driver, callout_driver;
  46 static int serial_refcount;
  47 
  48 /* serial subtype definitions */
  49 #define SERIAL_TYPE_NORMAL      1
  50 #define SERIAL_TYPE_CALLOUT     2
  51 
  52 /* number of characters left in xmit buffer before we ask for more */
  53 #define WAKEUP_CHARS 256
  54 
  55 /*
  56  * Serial driver configuration section.  Here are the various options:
  57  *
  58  * CONFIG_HUB6
  59  *              Enables support for the venerable Bell Technologies
  60  *              HUB6 card.
  61  *
  62  * SERIAL_PARANOIA_CHECK
  63  *              Check the magic number for the async_structure where
  64  *              ever possible.
  65  */
  66 
  67 #define SERIAL_PARANOIA_CHECK
  68 #define CONFIG_SERIAL_NOPAUSE_IO
  69 #define SERIAL_DO_RESTART
  70 
  71 #undef SERIAL_DEBUG_INTR
  72 #undef SERIAL_DEBUG_OPEN
  73 #undef SERIAL_DEBUG_FLOW
  74 
  75 #define RS_STROBE_TIME 10
  76 #define RS_ISR_PASS_LIMIT 256
  77 
  78 #define _INLINE_ inline
  79   
  80 /*
  81  * IRQ_timeout          - How long the timeout should be for each IRQ
  82  *                              should be after the IRQ has been active.
  83  */
  84 
  85 static struct async_struct *IRQ_ports[16];
  86 static struct rs_multiport_struct rs_multiport[16];
  87 static int IRQ_timeout[16];
  88 static volatile int rs_irq_triggered;
  89 static volatile int rs_triggered;
  90 static int rs_wild_int_mask;
  91 
  92 static void autoconfig(struct async_struct * info);
  93 static void change_speed(struct async_struct *info);
  94         
  95 /*
  96  * This assumes you have a 1.8432 MHz clock for your UART.
  97  *
  98  * It'd be nice if someone built a serial card with a 24.576 MHz
  99  * clock, since the 16550A is capable of handling a top speed of 1.5
 100  * megabits/second; but this requires the faster clock.
 101  */
 102 #define BASE_BAUD ( 1843200 / 16 )
 103 
 104 /* Standard COM flags (except for COM4, because of the 8514 problem) */
 105 #define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST )
 106 #define STD_COM4_FLAGS ASYNC_BOOT_AUTOCONF
 107 
 108 #define FOURPORT_FLAGS ASYNC_FOURPORT
 109 #define ACCENT_FLAGS 0
 110 #define BOCA_FLAGS 0
 111 #define HUB6_FLAGS 0
 112         
 113 /*
 114  * The following define the access methods for the HUB6 card. All
 115  * access is through two ports for all 24 possible chips. The card is
 116  * selected through the high 2 bits, the port on that card with the
 117  * "middle" 3 bits, and the register on that port with the bottom
 118  * 3 bits.
 119  *
 120  * While the access port and interrupt is configurable, the default
 121  * port locations are 0x302 for the port control register, and 0x303
 122  * for the data read/write register. Normally, the interrupt is at irq3
 123  * but can be anything from 3 to 7 inclusive. Note that using 3 will
 124  * require disabling com2.
 125  */
 126 
 127 #define C_P(card,port) (((card)<<6|(port)<<3) + 1)
 128 
 129 struct async_struct rs_table[] = {
 130         /* UART CLK   PORT IRQ     FLAGS        */
 131         { 0, BASE_BAUD, 0x3F8, 4, STD_COM_FLAGS },      /* ttyS0 */
 132         { 0, BASE_BAUD, 0x2F8, 3, STD_COM_FLAGS },      /* ttyS1 */
 133         { 0, BASE_BAUD, 0x3E8, 4, STD_COM_FLAGS },      /* ttyS2 */
 134         { 0, BASE_BAUD, 0x2E8, 3, STD_COM4_FLAGS },     /* ttyS3 */
 135 
 136         { 0, BASE_BAUD, 0x1A0, 9, FOURPORT_FLAGS },     /* ttyS4 */
 137         { 0, BASE_BAUD, 0x1A8, 9, FOURPORT_FLAGS },     /* ttyS5 */
 138         { 0, BASE_BAUD, 0x1B0, 9, FOURPORT_FLAGS },     /* ttyS6 */
 139         { 0, BASE_BAUD, 0x1B8, 9, FOURPORT_FLAGS },     /* ttyS7 */
 140 
 141         { 0, BASE_BAUD, 0x2A0, 5, FOURPORT_FLAGS },     /* ttyS8 */
 142         { 0, BASE_BAUD, 0x2A8, 5, FOURPORT_FLAGS },     /* ttyS9 */
 143         { 0, BASE_BAUD, 0x2B0, 5, FOURPORT_FLAGS },     /* ttyS10 */
 144         { 0, BASE_BAUD, 0x2B8, 5, FOURPORT_FLAGS },     /* ttyS11 */
 145         
 146         { 0, BASE_BAUD, 0x330, 4, ACCENT_FLAGS },       /* ttyS12 */
 147         { 0, BASE_BAUD, 0x338, 4, ACCENT_FLAGS },       /* ttyS13 */
 148         { 0, BASE_BAUD, 0x000, 0, 0 },  /* ttyS14 (spare; user configurable) */
 149         { 0, BASE_BAUD, 0x000, 0, 0 },  /* ttyS15 (spare; user configurable) */
 150 
 151         { 0, BASE_BAUD, 0x100, 12, BOCA_FLAGS },        /* ttyS16 */
 152         { 0, BASE_BAUD, 0x108, 12, BOCA_FLAGS },        /* ttyS17 */
 153         { 0, BASE_BAUD, 0x110, 12, BOCA_FLAGS },        /* ttyS18 */
 154         { 0, BASE_BAUD, 0x118, 12, BOCA_FLAGS },        /* ttyS19 */
 155         { 0, BASE_BAUD, 0x120, 12, BOCA_FLAGS },        /* ttyS20 */
 156         { 0, BASE_BAUD, 0x128, 12, BOCA_FLAGS },        /* ttyS21 */
 157         { 0, BASE_BAUD, 0x130, 12, BOCA_FLAGS },        /* ttyS22 */
 158         { 0, BASE_BAUD, 0x138, 12, BOCA_FLAGS },        /* ttyS23 */
 159         { 0, BASE_BAUD, 0x140, 12, BOCA_FLAGS },        /* ttyS24 */
 160         { 0, BASE_BAUD, 0x148, 12, BOCA_FLAGS },        /* ttyS25 */
 161         { 0, BASE_BAUD, 0x150, 12, BOCA_FLAGS },        /* ttyS26 */
 162         { 0, BASE_BAUD, 0x158, 12, BOCA_FLAGS },        /* ttyS27 */
 163         { 0, BASE_BAUD, 0x160, 12, BOCA_FLAGS },        /* ttyS28 */
 164         { 0, BASE_BAUD, 0x168, 12, BOCA_FLAGS },        /* ttyS29 */
 165         { 0, BASE_BAUD, 0x170, 12, BOCA_FLAGS },        /* ttyS30 */
 166         { 0, BASE_BAUD, 0x178, 12, BOCA_FLAGS },        /* ttyS31 */
 167 
 168 /* You can have up to four HUB6's in the system, but I've only
 169  * included two cards here for a total of twelve ports.
 170  */
 171         { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,0) },       /* ttyS32 */
 172         { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,1) },       /* ttyS33 */
 173         { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,2) },       /* ttyS34 */
 174         { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,3) },       /* ttyS35 */
 175         { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,4) },       /* ttyS36 */
 176         { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,5) },       /* ttyS37 */
 177         { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,0) },       /* ttyS32 */
 178         { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,1) },       /* ttyS33 */
 179         { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,2) },       /* ttyS34 */
 180         { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,3) },       /* ttyS35 */
 181         { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,4) },       /* ttyS36 */
 182         { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,5) },       /* ttyS37 */
 183 };
 184 
 185 #define NR_PORTS        (sizeof(rs_table)/sizeof(struct async_struct))
 186 
 187 static struct tty_struct *serial_table[NR_PORTS];
 188 static struct termios *serial_termios[NR_PORTS];
 189 static struct termios *serial_termios_locked[NR_PORTS];
 190 
 191 #ifndef MIN
 192 #define MIN(a,b)        ((a) < (b) ? (a) : (b))
 193 #endif
 194 
 195 /*
 196  * tmp_buf is used as a temporary buffer by serial_write.  We need to
 197  * lock it in case the memcpy_fromfs blocks while swapping in a page,
 198  * and some other program tries to do a serial write at the same time.
 199  * Since the lock will only come under contention when the system is
 200  * swapping and available memory is low, it makes sense to share one
 201  * buffer across all the serial ports, since it significantly saves
 202  * memory if large numbers of serial ports are open.
 203  */
 204 static unsigned char *tmp_buf = 0;
 205 static struct semaphore tmp_buf_sem = MUTEX;
 206 
 207 static inline int serial_paranoia_check(struct async_struct *info,
     /* [previous][next][first][last][top][bottom][index][help] */
 208                                         dev_t device, const char *routine)
 209 {
 210 #ifdef SERIAL_PARANOIA_CHECK
 211         static const char *badmagic =
 212                 "Warning: bad magic number for serial struct (%d, %d) in %s\n";
 213         static const char *badinfo =
 214                 "Warning: null async_struct for (%d, %d) in %s\n";
 215 
 216         if (!info) {
 217                 printk(badinfo, MAJOR(device), MINOR(device), routine);
 218                 return 1;
 219         }
 220         if (info->magic != SERIAL_MAGIC) {
 221                 printk(badmagic, MAJOR(device), MINOR(device), routine);
 222                 return 1;
 223         }
 224 #endif
 225         return 0;
 226 }
 227 
 228 /*
 229  * This is used to figure out the divisor speeds and the timeouts
 230  */
 231 static int baud_table[] = {
 232         0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
 233         9600, 19200, 38400, 57600, 115200, 0 };
 234 
 235 static inline unsigned int serial_in(struct async_struct *info, int offset)
     /* [previous][next][first][last][top][bottom][index][help] */
 236 {
 237 #ifdef CONFIG_HUB6
 238     if (info->hub6) {
 239         outb(info->hub6 - 1 + offset, info->port);
 240         return inb(info->port+1);
 241     } else
 242 #endif
 243         return inb(info->port + offset);
 244 }
 245 
 246 static inline unsigned int serial_inp(struct async_struct *info, int offset)
     /* [previous][next][first][last][top][bottom][index][help] */
 247 {
 248 #ifdef CONFIG_HUB6
 249     if (info->hub6) {
 250         outb(info->hub6 - 1 + offset, info->port);
 251         return inb_p(info->port+1);
 252     } else
 253 #endif
 254 #ifdef CONFIG_SERIAL_NOPAUSE_IO
 255         return inb(info->port + offset);
 256 #else
 257         return inb_p(info->port + offset);
 258 #endif
 259 }
 260 
 261 static inline void serial_out(struct async_struct *info, int offset, int value)
     /* [previous][next][first][last][top][bottom][index][help] */
 262 {
 263 #ifdef CONFIG_HUB6
 264     if (info->hub6) {
 265         outb(info->hub6 - 1 + offset, info->port);
 266         outb(value, info->port+1);
 267     } else
 268 #endif
 269         outb(value, info->port+offset);
 270 }
 271 
 272 static inline void serial_outp(struct async_struct *info, int offset,
     /* [previous][next][first][last][top][bottom][index][help] */
 273                                int value)
 274 {
 275 #ifdef CONFIG_HUB6
 276     if (info->hub6) {
 277         outb(info->hub6 - 1 + offset, info->port);
 278         outb_p(value, info->port+1);
 279     } else
 280 #endif
 281 #ifdef CONFIG_SERIAL_NOPAUSE_IO
 282         outb(value, info->port+offset);
 283 #else
 284         outb_p(value, info->port+offset);
 285 #endif
 286 }
 287 
 288 /*
 289  * ------------------------------------------------------------
 290  * rs_stop() and rs_start()
 291  *
 292  * This routines are called before setting or resetting tty->stopped.
 293  * They enable or disable transmitter interrupts, as necessary.
 294  * ------------------------------------------------------------
 295  */
 296 static void rs_stop(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 297 {
 298         struct async_struct *info = (struct async_struct *)tty->driver_data;
 299         unsigned long flags;
 300 
 301         if (serial_paranoia_check(info, tty->device, "rs_stop"))
 302                 return;
 303         
 304         save_flags(flags); cli();
 305         if (info->IER & UART_IER_THRI) {
 306                 info->IER &= ~UART_IER_THRI;
 307                 serial_out(info, UART_IER, info->IER);
 308         }
 309         restore_flags(flags);
 310 }
 311 
 312 static void rs_start(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 313 {
 314         struct async_struct *info = (struct async_struct *)tty->driver_data;
 315         unsigned long flags;
 316         
 317         if (serial_paranoia_check(info, tty->device, "rs_start"))
 318                 return;
 319         
 320         save_flags(flags); cli();
 321         if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
 322                 info->IER |= UART_IER_THRI;
 323                 serial_out(info, UART_IER, info->IER);
 324         }
 325         restore_flags(flags);
 326 }
 327 
 328 /*
 329  * ----------------------------------------------------------------------
 330  *
 331  * Here starts the interrupt handling routines.  All of the following
 332  * subroutines are declared as inline and are folded into
 333  * rs_interrupt().  They were separated out for readability's sake.
 334  *
 335  * Note: rs_interrupt() is a "fast" interrupt, which means that it
 336  * runs with interrupts turned off.  People who may want to modify
 337  * rs_interrupt() should try to keep the interrupt handler as fast as
 338  * possible.  After you are done making modifications, it is not a bad
 339  * idea to do:
 340  * 
 341  * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
 342  *
 343  * and look at the resulting assemble code in serial.s.
 344  *
 345  *                              - Ted Ts'o (tytso@mit.edu), 7-Mar-93
 346  * -----------------------------------------------------------------------
 347  */
 348 
 349 /*
 350  * This is the serial driver's interrupt routine while we are probing
 351  * for submarines.
 352  */
 353 static void rs_probe(int irq, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 354 {
 355         rs_irq_triggered = irq;
 356         rs_triggered |= 1 << irq;
 357         return;
 358 }
 359 
 360 /*
 361  * This routine is used by the interrupt handler to schedule
 362  * processing in the software interrupt portion of the driver.
 363  */
 364 static _INLINE_ void rs_sched_event(struct async_struct *info,
     /* [previous][next][first][last][top][bottom][index][help] */
 365                                   int event)
 366 {
 367         info->event |= 1 << event;
 368         queue_task_irq_off(&info->tqueue, &tq_serial);
 369         mark_bh(SERIAL_BH);
 370 }
 371 
 372 static _INLINE_ void receive_chars(struct async_struct *info,
     /* [previous][next][first][last][top][bottom][index][help] */
 373                                  int *status)
 374 {
 375         struct tty_struct *tty = info->tty;
 376         unsigned char ch;
 377         int ignored = 0;
 378 
 379         do {
 380                 ch = serial_inp(info, UART_RX);
 381                 if (*status & info->ignore_status_mask) {
 382                         if (++ignored > 100)
 383                                 break;
 384                         goto ignore_char;
 385                 }
 386                 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
 387                         break;
 388                 tty->flip.count++;
 389                 if (*status & (UART_LSR_BI)) {
 390                         printk("handling break....");
 391                         *tty->flip.flag_buf_ptr++ = TTY_BREAK;
 392                         if (info->flags & ASYNC_SAK)
 393                                 do_SAK(tty);
 394                 } else if (*status & UART_LSR_PE)
 395                         *tty->flip.flag_buf_ptr++ = TTY_PARITY;
 396                 else if (*status & UART_LSR_FE)
 397                         *tty->flip.flag_buf_ptr++ = TTY_FRAME;
 398                 else if (*status & UART_LSR_OE) 
 399                         *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
 400                 else
 401                         *tty->flip.flag_buf_ptr++ = 0;
 402                 *tty->flip.char_buf_ptr++ = ch;
 403         ignore_char:
 404                 *status = serial_inp(info, UART_LSR) & info->read_status_mask;
 405         } while (*status & UART_LSR_DR);
 406         queue_task_irq_off(&tty->flip.tqueue, &tq_timer);
 407 #ifdef SERIAL_DEBUG_INTR
 408         printk("DR...");
 409 #endif
 410 }
 411 
 412 static _INLINE_ void transmit_chars(struct async_struct *info, int *intr_done)
     /* [previous][next][first][last][top][bottom][index][help] */
 413 {
 414         int count;
 415         
 416         if (info->x_char) {
 417                 serial_outp(info, UART_TX, info->x_char);
 418                 info->x_char = 0;
 419                 if (intr_done)
 420                         *intr_done = 0;
 421                 return;
 422         }
 423         if ((info->xmit_cnt <= 0) || info->tty->stopped ||
 424             info->tty->hw_stopped) {
 425                 info->IER &= ~UART_IER_THRI;
 426                 serial_out(info, UART_IER, info->IER);
 427                 return;
 428         }
 429         
 430         count = info->xmit_fifo_size;
 431         do {
 432                 serial_out(info, UART_TX, info->xmit_buf[info->xmit_tail++]);
 433                 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
 434                 if (--info->xmit_cnt <= 0)
 435                         break;
 436         } while (--count > 0);
 437         
 438         if (info->xmit_cnt < WAKEUP_CHARS)
 439                 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
 440 
 441 #ifdef SERIAL_DEBUG_INTR
 442         printk("THRE...");
 443 #endif
 444         if (intr_done)
 445                 *intr_done = 0;
 446 
 447         if (info->xmit_cnt <= 0) {
 448                 info->IER &= ~UART_IER_THRI;
 449                 serial_out(info, UART_IER, info->IER);
 450         }
 451 }
 452 
 453 static _INLINE_ void check_modem_status(struct async_struct *info)
     /* [previous][next][first][last][top][bottom][index][help] */
 454 {
 455         int     status;
 456         
 457         status = serial_in(info, UART_MSR);
 458 
 459         if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
 460 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
 461                 printk("ttys%d CD now %s...", info->line,
 462                        (status & UART_MSR_DCD) ? "on" : "off");
 463 #endif          
 464                 if (status & UART_MSR_DCD)
 465                         wake_up_interruptible(&info->open_wait);
 466                 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
 467                            (info->flags & ASYNC_CALLOUT_NOHUP))) {
 468 #ifdef SERIAL_DEBUG_OPEN
 469                         printk("scheduling hangup...");
 470 #endif
 471                         queue_task_irq_off(&info->tqueue_hangup,
 472                                            &tq_scheduler);
 473                 }
 474         }
 475         if (info->flags & ASYNC_CTS_FLOW) {
 476                 if (info->tty->hw_stopped) {
 477                         if (status & UART_MSR_CTS) {
 478 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
 479                                 printk("CTS tx start...");
 480 #endif
 481                                 info->tty->hw_stopped = 0;
 482                                 info->IER |= UART_IER_THRI;
 483                                 serial_out(info, UART_IER, info->IER);
 484                                 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
 485                                 return;
 486                         }
 487                 } else {
 488                         if (!(status & UART_MSR_CTS)) {
 489 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
 490                                 printk("CTS tx stop...");
 491 #endif
 492                                 info->tty->hw_stopped = 1;
 493                                 info->IER &= ~UART_IER_THRI;
 494                                 serial_out(info, UART_IER, info->IER);
 495                         }
 496                 }
 497         }
 498 }
 499 
 500 /*
 501  * This is the serial driver's generic interrupt routine
 502  */
 503 static void rs_interrupt(int irq, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 504 {
 505         int status;
 506         struct async_struct * info;
 507         int pass_counter = 0;
 508         struct async_struct *end_mark = 0;
 509         int first_multi = 0;
 510         struct rs_multiport_struct *multi;
 511 
 512 #ifdef SERIAL_DEBUG_INTR
 513         printk("rs_interrupt(%d)...", irq);
 514 #endif
 515         
 516         info = IRQ_ports[irq];
 517         if (!info)
 518                 return;
 519         
 520         multi = &rs_multiport[irq];
 521         if (multi->port_monitor)
 522                 first_multi = inb(multi->port_monitor);
 523 
 524         do {
 525                 if (!info->tty ||
 526                     (serial_in(info, UART_IIR) & UART_IIR_NO_INT)) {
 527                         if (!end_mark)
 528                                 end_mark = info;
 529                         goto next;
 530                 }
 531                 end_mark = 0;
 532 
 533                 info->last_active = jiffies;
 534 
 535                 status = serial_inp(info, UART_LSR) & info->read_status_mask;
 536 #ifdef SERIAL_DEBUG_INTR
 537                 printk("status = %x...", status);
 538 #endif
 539                 if (status & UART_LSR_DR)
 540                         receive_chars(info, &status);
 541                 check_modem_status(info);
 542                 if (status & UART_LSR_THRE)
 543                         transmit_chars(info, 0);
 544 
 545         next:
 546                 info = info->next_port;
 547                 if (!info) {
 548                         info = IRQ_ports[irq];
 549                         if (pass_counter++ > RS_ISR_PASS_LIMIT) {
 550 #if 0
 551                                 printk("rs loop break\n");
 552 #endif
 553                                 break;  /* Prevent infinite loops */
 554                         }
 555                         continue;
 556                 }
 557         } while (end_mark != info);
 558         if (multi->port_monitor)
 559                 printk("rs port monitor (normal) irq %d: 0x%x, 0x%x\n",
 560                        info->irq, first_multi, inb(multi->port_monitor));
 561 #ifdef SERIAL_DEBUG_INTR
 562         printk("end.\n");
 563 #endif
 564 }
 565 
 566 /*
 567  * This is the serial driver's interrupt routine for a single port
 568  */
 569 static void rs_interrupt_single(int irq, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 570 {
 571         int status;
 572         int pass_counter = 0;
 573         int first_multi = 0;
 574         struct async_struct * info;
 575         struct rs_multiport_struct *multi;
 576         
 577 #ifdef SERIAL_DEBUG_INTR
 578         printk("rs_interrupt_single(%d)...", irq);
 579 #endif
 580         
 581         info = IRQ_ports[irq];
 582         if (!info || !info->tty)
 583                 return;
 584 
 585         multi = &rs_multiport[irq];
 586         if (multi->port_monitor)
 587                 first_multi = inb(multi->port_monitor);
 588 
 589         do {
 590                 status = serial_inp(info, UART_LSR) & info->read_status_mask;
 591 #ifdef SERIAL_DEBUG_INTR
 592                 printk("status = %x...", status);
 593 #endif
 594                 if (status & UART_LSR_DR)
 595                         receive_chars(info, &status);
 596                 check_modem_status(info);
 597                 if (status & UART_LSR_THRE)
 598                         transmit_chars(info, 0);
 599                 if (pass_counter++ > RS_ISR_PASS_LIMIT) {
 600 #if 0
 601                         printk("rs_single loop break.\n");
 602 #endif
 603                         break;
 604                 }
 605         } while (!(serial_in(info, UART_IIR) & UART_IIR_NO_INT));
 606         info->last_active = jiffies;
 607         if (multi->port_monitor)
 608                 printk("rs port monitor (single) irq %d: 0x%x, 0x%x\n",
 609                        info->irq, first_multi, inb(multi->port_monitor));
 610 #ifdef SERIAL_DEBUG_INTR
 611         printk("end.\n");
 612 #endif
 613 }
 614 
 615 /*
 616  * This is the serial driver's for multiport boards
 617  */
 618 static void rs_interrupt_multi(int irq, struct pt_regs * regs)
     /* [previous][next][first][last][top][bottom][index][help] */
 619 {
 620         int status;
 621         struct async_struct * info;
 622         int pass_counter = 0;
 623         int first_multi= 0;
 624         struct rs_multiport_struct *multi;
 625 
 626 #ifdef SERIAL_DEBUG_INTR
 627         printk("rs_interrupt_multi(%d)...", irq);
 628 #endif
 629         
 630         info = IRQ_ports[irq];
 631         if (!info)
 632                 return;
 633         multi = &rs_multiport[irq];
 634         if (!multi->port1) {
 635                 /* Should never happen */
 636                 printk("rs_interrupt_multi: NULL port1!\n");
 637                 return;
 638         }
 639         if (multi->port_monitor)
 640                 first_multi = inb(multi->port_monitor);
 641         
 642         while (1) {
 643                 if (!info->tty ||
 644                     (serial_in(info, UART_IIR) & UART_IIR_NO_INT))
 645                         goto next;
 646 
 647                 info->last_active = jiffies;
 648 
 649                 status = serial_inp(info, UART_LSR) & info->read_status_mask;
 650 #ifdef SERIAL_DEBUG_INTR
 651                 printk("status = %x...", status);
 652 #endif
 653                 if (status & UART_LSR_DR)
 654                         receive_chars(info, &status);
 655                 check_modem_status(info);
 656                 if (status & UART_LSR_THRE)
 657                         transmit_chars(info, 0);
 658 
 659         next:
 660                 info = info->next_port;
 661                 if (info)
 662                         continue;
 663 
 664                 info = IRQ_ports[irq];
 665                 if (pass_counter++ > RS_ISR_PASS_LIMIT) {
 666 #if 1
 667                         printk("rs_multi loop break\n");
 668 #endif
 669                         break;  /* Prevent infinite loops */
 670                 }
 671                 if (multi->port_monitor)
 672                         printk("rs port monitor irq %d: 0x%x, 0x%x\n",
 673                                info->irq, first_multi,
 674                                inb(multi->port_monitor));
 675                 if ((inb(multi->port1) & multi->mask1) != multi->match1)
 676                         continue;
 677                 if (!multi->port2)
 678                         break;
 679                 if ((inb(multi->port2) & multi->mask2) != multi->match2)
 680                         continue;
 681                 if (!multi->port3)
 682                         break;
 683                 if ((inb(multi->port3) & multi->mask3) != multi->match3)
 684                         continue;
 685                 if (!multi->port4)
 686                         break;
 687                 if ((inb(multi->port4) & multi->mask4) == multi->match4)
 688                         continue;
 689                 break;
 690         } 
 691 #ifdef SERIAL_DEBUG_INTR
 692         printk("end.\n");
 693 #endif
 694 }
 695 
 696 
 697 /*
 698  * -------------------------------------------------------------------
 699  * Here ends the serial interrupt routines.
 700  * -------------------------------------------------------------------
 701  */
 702 
 703 /*
 704  * This routine is used to handle the "bottom half" processing for the
 705  * serial driver, known also the "software interrupt" processing.
 706  * This processing is done at the kernel interrupt level, after the
 707  * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
 708  * is where time-consuming activities which can not be done in the
 709  * interrupt driver proper are done; the interrupt driver schedules
 710  * them using rs_sched_event(), and they get done here.
 711  */
 712 static void do_serial_bh(void *unused)
     /* [previous][next][first][last][top][bottom][index][help] */
 713 {
 714         run_task_queue(&tq_serial);
 715 }
 716 
 717 static void do_softint(void *private_)
     /* [previous][next][first][last][top][bottom][index][help] */
 718 {
 719         struct async_struct     *info = (struct async_struct *) private_;
 720         struct tty_struct       *tty;
 721         
 722         tty = info->tty;
 723         if (!tty)
 724                 return;
 725 
 726         if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
 727                 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
 728                     tty->ldisc.write_wakeup)
 729                         (tty->ldisc.write_wakeup)(tty);
 730                 wake_up_interruptible(&tty->write_wait);
 731         }
 732 }
 733 
 734 /*
 735  * This routine is called from the scheduler tqueue when the interrupt
 736  * routine has signalled that a hangup has occurred.  The path of
 737  * hangup processing is:
 738  *
 739  *      serial interrupt routine -> (scheduler tqueue) ->
 740  *      do_serial_hangup() -> tty->hangup() -> rs_hangup()
 741  * 
 742  */
 743 static void do_serial_hangup(void *private_)
     /* [previous][next][first][last][top][bottom][index][help] */
 744 {
 745         struct async_struct     *info = (struct async_struct *) private_;
 746         struct tty_struct       *tty;
 747         
 748         tty = info->tty;
 749         if (!tty)
 750                 return;
 751 
 752         tty_hangup(tty);
 753 }
 754 
 755 
 756 /*
 757  * This subroutine is called when the RS_TIMER goes off.  It is used
 758  * by the serial driver to handle ports that do not have an interrupt
 759  * (irq=0).  This doesn't work very well for 16450's, but gives barely
 760  * passable results for a 16550A.  (Although at the expense of much
 761  * CPU overhead).
 762  */
 763 static void rs_timer(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 764 {
 765         static unsigned long last_strobe = 0;
 766         struct async_struct *info;
 767         unsigned int    i;
 768 
 769         if ((jiffies - last_strobe) >= RS_STROBE_TIME*HZ) {
 770                 for (i=1; i < 16; i++) {
 771                         info = IRQ_ports[i];
 772                         if (!info)
 773                                 continue;
 774                         cli();
 775                         if (info->next_port) {
 776                                 do {
 777                                         serial_out(info, UART_IER, 0);
 778                                         info->IER |= UART_IER_THRI;
 779                                         serial_out(info, UART_IER, info->IER);
 780                                         info = info->next_port;
 781                                 } while (info);
 782                                 if (rs_multiport[i].port1)
 783                                         rs_interrupt_multi(i, NULL);
 784                                 else
 785                                         rs_interrupt(i, NULL);
 786                         } else
 787                                 rs_interrupt_single(i, NULL);
 788                         sti();
 789                 }
 790         }
 791         last_strobe = jiffies;
 792         timer_table[RS_TIMER].expires = jiffies + RS_STROBE_TIME * HZ;
 793         timer_active |= 1 << RS_TIMER;
 794 
 795         if (IRQ_ports[0]) {
 796                 cli();
 797                 rs_interrupt(0, NULL);
 798                 sti();
 799 
 800                 timer_table[RS_TIMER].expires = jiffies + IRQ_timeout[0] - 2;
 801         }
 802 }
 803 
 804 /*
 805  * ---------------------------------------------------------------
 806  * Low level utility subroutines for the serial driver:  routines to
 807  * figure out the appropriate timeout for an interrupt chain, routines
 808  * to initialize and startup a serial port, and routines to shutdown a
 809  * serial port.  Useful stuff like that.
 810  * ---------------------------------------------------------------
 811  */
 812 
 813 /*
 814  * Grab all interrupts in preparation for doing an automatic irq
 815  * detection.  dontgrab is a mask of irq's _not_ to grab.  Returns a
 816  * mask of irq's which were grabbed and should therefore be freed
 817  * using free_all_interrupts().
 818  */
 819 static int grab_all_interrupts(int dontgrab)
     /* [previous][next][first][last][top][bottom][index][help] */
 820 {
 821         int                     irq_lines = 0;
 822         int                     i, mask;
 823         
 824         for (i = 0, mask = 1; i < 16; i++, mask <<= 1) {
 825                 if (!(mask & dontgrab) && !request_irq(i, rs_probe, SA_INTERRUPT, "serial probe")) {
 826                         irq_lines |= mask;
 827                 }
 828         }
 829         return irq_lines;
 830 }
 831 
 832 /*
 833  * Release all interrupts grabbed by grab_all_interrupts
 834  */
 835 static void free_all_interrupts(int irq_lines)
     /* [previous][next][first][last][top][bottom][index][help] */
 836 {
 837         int     i;
 838         
 839         for (i = 0; i < 16; i++) {
 840                 if (irq_lines & (1 << i))
 841                         free_irq(i);
 842         }
 843 }
 844 
 845 /*
 846  * This routine figures out the correct timeout for a particular IRQ.
 847  * It uses the smallest timeout of all of the serial ports in a
 848  * particular interrupt chain.  Now only used for IRQ 0....
 849  */
 850 static void figure_IRQ_timeout(int irq)
     /* [previous][next][first][last][top][bottom][index][help] */
 851 {
 852         struct  async_struct    *info;
 853         int     timeout = 60*HZ;        /* 60 seconds === a long time :-) */
 854 
 855         info = IRQ_ports[irq];
 856         if (!info) {
 857                 IRQ_timeout[irq] = 60*HZ;
 858                 return;
 859         }
 860         while (info) {
 861                 if (info->timeout < timeout)
 862                         timeout = info->timeout;
 863                 info = info->next_port;
 864         }
 865         if (!irq)
 866                 timeout = timeout / 2;
 867         IRQ_timeout[irq] = timeout ? timeout : 1;
 868 }
 869 
 870 static int startup(struct async_struct * info)
     /* [previous][next][first][last][top][bottom][index][help] */
 871 {
 872         unsigned short ICP;
 873         unsigned long flags;
 874         int     retval;
 875         void (*handler)(int, struct pt_regs *);
 876 
 877         if (info->flags & ASYNC_INITIALIZED)
 878                 return 0;
 879 
 880         if (!info->port || !info->type) {
 881                 if (info->tty)
 882                         set_bit(TTY_IO_ERROR, &info->tty->flags);
 883                 return 0;
 884         }
 885 
 886         if (!info->xmit_buf) {
 887                 info->xmit_buf = (unsigned char *) get_free_page(GFP_KERNEL);
 888                 if (!info->xmit_buf)
 889                         return -ENOMEM;
 890         }
 891 
 892         save_flags(flags); cli();
 893 
 894 #ifdef SERIAL_DEBUG_OPEN
 895         printk("starting up ttys%d (irq %d)...", info->line, info->irq);
 896 #endif
 897 
 898         /*
 899          * Clear the FIFO buffers and disable them
 900          * (they will be reenabled in change_speed())
 901          */
 902         if (info->type == PORT_16650) {
 903                 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
 904                                              UART_FCR_CLEAR_XMIT));
 905                 info->xmit_fifo_size = 1; /* disabled for now */
 906         } else if (info->type == PORT_16550A) {
 907                 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
 908                                              UART_FCR_CLEAR_XMIT));
 909                 info->xmit_fifo_size = 16;
 910         } else
 911                 info->xmit_fifo_size = 1;
 912 
 913         /*
 914          * At this point there's no way the LSR could still be 0xFF;
 915          * if it is, then bail out, because there's likely no UART
 916          * here.
 917          */
 918         if (serial_inp(info, UART_LSR) == 0xff) {
 919                 restore_flags(flags);
 920                 if (suser()) {
 921                         if (info->tty)
 922                                 set_bit(TTY_IO_ERROR, &info->tty->flags);
 923                         return 0;
 924                 } else
 925                         return -ENODEV;
 926         }
 927         
 928         /*
 929          * Allocate the IRQ if necessary
 930          */
 931         if (info->irq && (!IRQ_ports[info->irq] ||
 932                           !IRQ_ports[info->irq]->next_port)) {
 933                 if (IRQ_ports[info->irq]) {
 934                         free_irq(info->irq);
 935                         if (rs_multiport[info->irq].port1)
 936                                 handler = rs_interrupt_multi;
 937                         else
 938                                 handler = rs_interrupt;
 939                 } else 
 940                         handler = rs_interrupt_single;
 941 
 942                 retval = request_irq(info->irq, handler, SA_INTERRUPT, "serial");
 943                 if (retval) {
 944                         restore_flags(flags);
 945                         if (suser()) {
 946                                 if (info->tty)
 947                                         set_bit(TTY_IO_ERROR,
 948                                                 &info->tty->flags);
 949                                 return 0;
 950                         } else
 951                                 return retval;
 952                 }
 953         }
 954 
 955         /*
 956          * Clear the interrupt registers.
 957          */
 958      /* (void) serial_inp(info, UART_LSR); */   /* (see above) */
 959         (void) serial_inp(info, UART_RX);
 960         (void) serial_inp(info, UART_IIR);
 961         (void) serial_inp(info, UART_MSR);
 962 
 963         /*
 964          * Now, initialize the UART 
 965          */
 966         serial_outp(info, UART_LCR, UART_LCR_WLEN8);    /* reset DLAB */
 967         if (info->flags & ASYNC_FOURPORT) {
 968                 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
 969                 info->MCR_noint = UART_MCR_DTR | UART_MCR_OUT1;
 970         } else {
 971                 info->MCR = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
 972                 info->MCR_noint = UART_MCR_DTR | UART_MCR_RTS;
 973         }
 974 #if defined(__alpha__) && !defined(CONFIG_PCI)
 975         info->MCR |= UART_MCR_OUT1 | UART_MCR_OUT2;
 976         info->MCR_noint |= UART_MCR_OUT1 | UART_MCR_OUT2;
 977 #endif
 978         if (info->irq == 0)
 979                 info->MCR = info->MCR_noint;
 980         serial_outp(info, UART_MCR, info->MCR);
 981         
 982         /*
 983          * Finally, enable interrupts
 984          */
 985         info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
 986         serial_outp(info, UART_IER, info->IER); /* enable interrupts */
 987         
 988         if (info->flags & ASYNC_FOURPORT) {
 989                 /* Enable interrupts on the AST Fourport board */
 990                 ICP = (info->port & 0xFE0) | 0x01F;
 991                 outb_p(0x80, ICP);
 992                 (void) inb_p(ICP);
 993         }
 994 
 995         /*
 996          * And clear the interrupt registers again for luck.
 997          */
 998         (void)serial_inp(info, UART_LSR);
 999         (void)serial_inp(info, UART_RX);
1000         (void)serial_inp(info, UART_IIR);
1001         (void)serial_inp(info, UART_MSR);
1002 
1003         if (info->tty)
1004                 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1005         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1006 
1007         /*
1008          * Insert serial port into IRQ chain.
1009          */
1010         info->prev_port = 0;
1011         info->next_port = IRQ_ports[info->irq];
1012         if (info->next_port)
1013                 info->next_port->prev_port = info;
1014         IRQ_ports[info->irq] = info;
1015         figure_IRQ_timeout(info->irq);
1016 
1017         /*
1018          * Set up serial timers...
1019          */
1020         timer_table[RS_TIMER].expires = jiffies + 2;
1021         timer_active |= 1 << RS_TIMER;
1022 
1023         /*
1024          * and set the speed of the serial port
1025          */
1026         change_speed(info);
1027 
1028         info->flags |= ASYNC_INITIALIZED;
1029         restore_flags(flags);
1030         return 0;
1031 }
1032 
1033 /*
1034  * This routine will shutdown a serial port; interrupts are disabled, and
1035  * DTR is dropped if the hangup on close termio flag is on.
1036  */
1037 static void shutdown(struct async_struct * info)
     /* [previous][next][first][last][top][bottom][index][help] */
1038 {
1039         unsigned long   flags;
1040         int             retval;
1041 
1042         if (!(info->flags & ASYNC_INITIALIZED))
1043                 return;
1044 
1045 #ifdef SERIAL_DEBUG_OPEN
1046         printk("Shutting down serial port %d (irq %d)....", info->line,
1047                info->irq);
1048 #endif
1049         
1050         save_flags(flags); cli(); /* Disable interrupts */
1051         
1052         /*
1053          * First unlink the serial port from the IRQ chain...
1054          */
1055         if (info->next_port)
1056                 info->next_port->prev_port = info->prev_port;
1057         if (info->prev_port)
1058                 info->prev_port->next_port = info->next_port;
1059         else
1060                 IRQ_ports[info->irq] = info->next_port;
1061         figure_IRQ_timeout(info->irq);
1062         
1063         /*
1064          * Free the IRQ, if necessary
1065          */
1066         if (info->irq && (!IRQ_ports[info->irq] ||
1067                           !IRQ_ports[info->irq]->next_port)) {
1068                 if (IRQ_ports[info->irq]) {
1069                         free_irq(info->irq);
1070                         retval = request_irq(info->irq, rs_interrupt_single, SA_INTERRUPT, "serial");
1071                         
1072                         if (retval)
1073                                 printk("serial shutdown: request_irq: error %d"
1074                                        "  Couldn't reacquire IRQ.\n", retval);
1075                 } else
1076                         free_irq(info->irq);
1077         }
1078 
1079         if (info->xmit_buf) {
1080                 free_page((unsigned long) info->xmit_buf);
1081                 info->xmit_buf = 0;
1082         }
1083 
1084         info->IER = 0;
1085         serial_outp(info, UART_IER, 0x00);      /* disable all intrs */
1086         if (info->flags & ASYNC_FOURPORT) {
1087                 /* reset interrupts on the AST Fourport board */
1088                 (void) inb((info->port & 0xFE0) | 0x01F);
1089         }
1090         
1091         if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
1092                 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1093                 info->MCR_noint &= ~(UART_MCR_DTR|UART_MCR_RTS);
1094         }
1095         serial_outp(info, UART_MCR, info->MCR_noint);
1096 
1097         /* disable FIFO's */    
1098         serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
1099                                      UART_FCR_CLEAR_XMIT));
1100         (void)serial_in(info, UART_RX);    /* read data port to reset things */
1101         
1102         if (info->tty)
1103                 set_bit(TTY_IO_ERROR, &info->tty->flags);
1104         
1105         info->flags &= ~ASYNC_INITIALIZED;
1106         restore_flags(flags);
1107 }
1108 
1109 /*
1110  * This routine is called to set the UART divisor registers to match
1111  * the specified baud rate for a serial port.
1112  */
1113 static void change_speed(struct async_struct *info)
     /* [previous][next][first][last][top][bottom][index][help] */
1114 {
1115         unsigned short port;
1116         int     quot = 0;
1117         unsigned cflag,cval,fcr;
1118         int     i;
1119 
1120         if (!info->tty || !info->tty->termios)
1121                 return;
1122         cflag = info->tty->termios->c_cflag;
1123         if (!(port = info->port))
1124                 return;
1125         i = cflag & CBAUD;
1126         if (i & CBAUDEX) {
1127                 i &= ~CBAUDEX;
1128                 if (i < 1 || i > 2) 
1129                         info->tty->termios->c_cflag &= ~CBAUDEX;
1130                 else
1131                         i += 15;
1132         }
1133         if (i == 15) {
1134                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1135                         i += 1;
1136                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1137                         i += 2;
1138                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
1139                         quot = info->custom_divisor;
1140         }
1141         if (quot) {
1142                 info->timeout = ((info->xmit_fifo_size*HZ*15*quot) /
1143                                  info->baud_base) + 2;
1144         } else if (baud_table[i] == 134) {
1145                 quot = (2*info->baud_base / 269);
1146                 info->timeout = (info->xmit_fifo_size*HZ*30/269) + 2;
1147         } else if (baud_table[i]) {
1148                 quot = info->baud_base / baud_table[i];
1149                 info->timeout = (info->xmit_fifo_size*HZ*15/baud_table[i]) + 2;
1150         } else {
1151                 quot = 0;
1152                 info->timeout = 0;
1153         }
1154         if (quot) {
1155                 info->MCR |= UART_MCR_DTR;
1156                 info->MCR_noint |= UART_MCR_DTR;
1157                 cli();
1158                 serial_out(info, UART_MCR, info->MCR);
1159                 sti();
1160         } else {
1161                 info->MCR &= ~UART_MCR_DTR;
1162                 info->MCR_noint &= ~UART_MCR_DTR;
1163                 cli();
1164                 serial_out(info, UART_MCR, info->MCR);
1165                 sti();
1166                 return;
1167         }
1168         /* byte size and parity */
1169         switch (cflag & CSIZE) {
1170               case CS5: cval = 0x00; break;
1171               case CS6: cval = 0x01; break;
1172               case CS7: cval = 0x02; break;
1173               case CS8: cval = 0x03; break;
1174               default:  cval = 0x00; break;     /* too keep GCC shut... */
1175         }
1176         if (cflag & CSTOPB) {
1177                 cval |= 0x04;
1178         }
1179         if (cflag & PARENB)
1180                 cval |= UART_LCR_PARITY;
1181         if (!(cflag & PARODD))
1182                 cval |= UART_LCR_EPAR;
1183         if (info->type == PORT_16550A) {
1184                 if ((info->baud_base / quot) < 2400)
1185                         fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
1186                 else
1187                         fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
1188         } else if (info->type == PORT_16650) {
1189                 /*
1190                  * On the 16650, we disable the FIFOs altogether
1191                  * because of a design bug in how the implement
1192                  * things.  We could support it by completely changing
1193                  * how we handle the interrupt driver, but not today....
1194                  *
1195                  * N.B.  Because there's no way to set a FIFO trigger
1196                  * at 1 char, we'd probably disable at speed below
1197                  * 2400 baud anyway...
1198                  */
1199                 fcr = 0;
1200         } else
1201                 fcr = 0;
1202         
1203         /* CTS flow control flag and modem status interrupts */
1204         info->IER &= ~UART_IER_MSI;
1205         if (cflag & CRTSCTS) {
1206                 info->flags |= ASYNC_CTS_FLOW;
1207                 info->IER |= UART_IER_MSI;
1208         } else
1209                 info->flags &= ~ASYNC_CTS_FLOW;
1210         if (cflag & CLOCAL)
1211                 info->flags &= ~ASYNC_CHECK_CD;
1212         else {
1213                 info->flags |= ASYNC_CHECK_CD;
1214                 info->IER |= UART_IER_MSI;
1215         }
1216         serial_out(info, UART_IER, info->IER);
1217 
1218         /*
1219          * Set up parity check flag
1220          */
1221         info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1222         if (I_INPCK(info->tty))
1223                 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1224         if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1225                 info->read_status_mask |= UART_LSR_BI;
1226         
1227         info->ignore_status_mask = 0;
1228         if (I_IGNPAR(info->tty)) {
1229                 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1230                 info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
1231         }
1232         if (I_IGNBRK(info->tty)) {
1233                 info->ignore_status_mask |= UART_LSR_BI;
1234                 info->read_status_mask |= UART_LSR_BI;
1235                 /*
1236                  * If we're ignore parity and break indicators, ignore 
1237                  * overruns too.  (For real raw support).
1238                  */
1239                 if (I_IGNPAR(info->tty)) {
1240                         info->ignore_status_mask |= UART_LSR_OE;
1241                         info->read_status_mask |= UART_LSR_OE;
1242                 }
1243         }
1244         
1245         cli();
1246         serial_outp(info, UART_LCR, cval | UART_LCR_DLAB);      /* set DLAB */
1247         serial_outp(info, UART_DLL, quot & 0xff);       /* LS of divisor */
1248         serial_outp(info, UART_DLM, quot >> 8);         /* MS of divisor */
1249         serial_outp(info, UART_LCR, cval);              /* reset DLAB */
1250         serial_outp(info, UART_FCR, fcr);       /* set fcr */
1251         sti();
1252 }
1253 
1254 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
     /* [previous][next][first][last][top][bottom][index][help] */
1255 {
1256         struct async_struct *info = (struct async_struct *)tty->driver_data;
1257         unsigned long flags;
1258 
1259         if (serial_paranoia_check(info, tty->device, "rs_put_char"))
1260                 return;
1261 
1262         if (!tty || !info->xmit_buf)
1263                 return;
1264 
1265         save_flags(flags); cli();
1266         if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
1267                 restore_flags(flags);
1268                 return;
1269         }
1270 
1271         info->xmit_buf[info->xmit_head++] = ch;
1272         info->xmit_head &= SERIAL_XMIT_SIZE-1;
1273         info->xmit_cnt++;
1274         restore_flags(flags);
1275 }
1276 
1277 static void rs_flush_chars(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1278 {
1279         struct async_struct *info = (struct async_struct *)tty->driver_data;
1280         unsigned long flags;
1281                                 
1282         if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
1283                 return;
1284 
1285         if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1286             !info->xmit_buf)
1287                 return;
1288 
1289         save_flags(flags); cli();
1290         info->IER |= UART_IER_THRI;
1291         serial_out(info, UART_IER, info->IER);
1292         restore_flags(flags);
1293 }
1294 
1295 static int rs_write(struct tty_struct * tty, int from_user,
     /* [previous][next][first][last][top][bottom][index][help] */
1296                     unsigned char *buf, int count)
1297 {
1298         int     c, total = 0;
1299         struct async_struct *info = (struct async_struct *)tty->driver_data;
1300         unsigned long flags;
1301                                 
1302         if (serial_paranoia_check(info, tty->device, "rs_write"))
1303                 return 0;
1304 
1305         if (!tty || !info->xmit_buf || !tmp_buf)
1306                 return 0;
1307             
1308         save_flags(flags);
1309         while (1) {
1310                 cli();          
1311                 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1312                                    SERIAL_XMIT_SIZE - info->xmit_head));
1313                 if (c <= 0)
1314                         break;
1315 
1316                 if (from_user) {
1317                         down(&tmp_buf_sem);
1318                         memcpy_fromfs(tmp_buf, buf, c);
1319                         c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1320                                        SERIAL_XMIT_SIZE - info->xmit_head));
1321                         memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1322                         up(&tmp_buf_sem);
1323                 } else
1324                         memcpy(info->xmit_buf + info->xmit_head, buf, c);
1325                 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1326                 info->xmit_cnt += c;
1327                 restore_flags(flags);
1328                 buf += c;
1329                 count -= c;
1330                 total += c;
1331         }
1332         if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1333             !(info->IER & UART_IER_THRI)) {
1334                 info->IER |= UART_IER_THRI;
1335                 serial_out(info, UART_IER, info->IER);
1336         }
1337         restore_flags(flags);
1338         return total;
1339 }
1340 
1341 static int rs_write_room(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1342 {
1343         struct async_struct *info = (struct async_struct *)tty->driver_data;
1344         int     ret;
1345                                 
1346         if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1347                 return 0;
1348         ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1349         if (ret < 0)
1350                 ret = 0;
1351         return ret;
1352 }
1353 
1354 static int rs_chars_in_buffer(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1355 {
1356         struct async_struct *info = (struct async_struct *)tty->driver_data;
1357                                 
1358         if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1359                 return 0;
1360         return info->xmit_cnt;
1361 }
1362 
1363 static void rs_flush_buffer(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1364 {
1365         struct async_struct *info = (struct async_struct *)tty->driver_data;
1366                                 
1367         if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1368                 return;
1369         cli();
1370         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1371         sti();
1372         wake_up_interruptible(&tty->write_wait);
1373         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1374             tty->ldisc.write_wakeup)
1375                 (tty->ldisc.write_wakeup)(tty);
1376 }
1377 
1378 /*
1379  * ------------------------------------------------------------
1380  * rs_throttle()
1381  * 
1382  * This routine is called by the upper-layer tty layer to signal that
1383  * incoming characters should be throttled.
1384  * ------------------------------------------------------------
1385  */
1386 static void rs_throttle(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1387 {
1388         struct async_struct *info = (struct async_struct *)tty->driver_data;
1389 #ifdef SERIAL_DEBUG_THROTTLE
1390         char    buf[64];
1391         
1392         printk("throttle %s: %d....\n", _tty_name(tty, buf),
1393                tty->ldisc.chars_in_buffer(tty));
1394 #endif
1395 
1396         if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1397                 return;
1398         
1399         if (I_IXOFF(tty))
1400                 info->x_char = STOP_CHAR(tty);
1401 
1402         info->MCR &= ~UART_MCR_RTS;
1403         info->MCR_noint &= ~UART_MCR_RTS;
1404         cli();
1405         serial_out(info, UART_MCR, info->MCR);
1406         sti();
1407 }
1408 
1409 static void rs_unthrottle(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1410 {
1411         struct async_struct *info = (struct async_struct *)tty->driver_data;
1412 #ifdef SERIAL_DEBUG_THROTTLE
1413         char    buf[64];
1414         
1415         printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1416                tty->ldisc.chars_in_buffer(tty));
1417 #endif
1418 
1419         if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1420                 return;
1421         
1422         if (I_IXOFF(tty)) {
1423                 if (info->x_char)
1424                         info->x_char = 0;
1425                 else
1426                         info->x_char = START_CHAR(tty);
1427         }
1428         info->MCR |= UART_MCR_RTS;
1429         info->MCR_noint |= UART_MCR_RTS;
1430         cli();
1431         serial_out(info, UART_MCR, info->MCR);
1432         sti();
1433 }
1434 
1435 /*
1436  * ------------------------------------------------------------
1437  * rs_ioctl() and friends
1438  * ------------------------------------------------------------
1439  */
1440 
1441 static int get_serial_info(struct async_struct * info,
     /* [previous][next][first][last][top][bottom][index][help] */
1442                            struct serial_struct * retinfo)
1443 {
1444         struct serial_struct tmp;
1445   
1446         if (!retinfo)
1447                 return -EFAULT;
1448         memset(&tmp, 0, sizeof(tmp));
1449         tmp.type = info->type;
1450         tmp.line = info->line;
1451         tmp.port = info->port;
1452         tmp.irq = info->irq;
1453         tmp.flags = info->flags;
1454         tmp.baud_base = info->baud_base;
1455         tmp.close_delay = info->close_delay;
1456         tmp.closing_wait = info->closing_wait;
1457         tmp.custom_divisor = info->custom_divisor;
1458         tmp.hub6 = info->hub6;
1459         memcpy_tofs(retinfo,&tmp,sizeof(*retinfo));
1460         return 0;
1461 }
1462 
1463 static int set_serial_info(struct async_struct * info,
     /* [previous][next][first][last][top][bottom][index][help] */
1464                            struct serial_struct * new_info)
1465 {
1466         struct serial_struct new_serial;
1467         struct async_struct old_info;
1468         unsigned int            i,change_irq,change_port;
1469         int                     retval = 0;
1470 
1471         if (!new_info)
1472                 return -EFAULT;
1473         memcpy_fromfs(&new_serial,new_info,sizeof(new_serial));
1474         old_info = *info;
1475 
1476         change_irq = new_serial.irq != info->irq;
1477         change_port = (new_serial.port != info->port) || (new_serial.hub6 != info->hub6);
1478 
1479         if (!suser()) {
1480                 if (change_irq || change_port ||
1481                     (new_serial.baud_base != info->baud_base) ||
1482                     (new_serial.type != info->type) ||
1483                     (new_serial.close_delay != info->close_delay) ||
1484                     ((new_serial.flags & ~ASYNC_USR_MASK) !=
1485                      (info->flags & ~ASYNC_USR_MASK)))
1486                         return -EPERM;
1487                 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1488                                (new_serial.flags & ASYNC_USR_MASK));
1489                 info->custom_divisor = new_serial.custom_divisor;
1490                 goto check_and_exit;
1491         }
1492 
1493         if (new_serial.irq == 2)
1494                 new_serial.irq = 9;
1495 
1496         if ((new_serial.irq > 15) || (new_serial.port > 0xffff) ||
1497             (new_serial.type < PORT_UNKNOWN) || (new_serial.type > PORT_MAX)) {
1498                 return -EINVAL;
1499         }
1500 
1501         /* Make sure address is not already in use */
1502         if (new_serial.type) {
1503                 for (i = 0 ; i < NR_PORTS; i++)
1504                         if ((info != &rs_table[i]) &&
1505                             (rs_table[i].port == new_serial.port) &&
1506                             rs_table[i].type)
1507                                 return -EADDRINUSE;
1508         }
1509 
1510         if ((change_port || change_irq) && (info->count > 1))
1511                 return -EBUSY;
1512 
1513         /*
1514          * OK, past this point, all the error checking has been done.
1515          * At this point, we start making changes.....
1516          */
1517 
1518         info->baud_base = new_serial.baud_base;
1519         info->flags = ((info->flags & ~ASYNC_FLAGS) |
1520                         (new_serial.flags & ASYNC_FLAGS));
1521         info->custom_divisor = new_serial.custom_divisor;
1522         info->type = new_serial.type;
1523         info->close_delay = new_serial.close_delay;
1524         info->closing_wait = new_serial.closing_wait;
1525 
1526         release_region(info->port,8);
1527         if (change_port || change_irq) {
1528                 /*
1529                  * We need to shutdown the serial port at the old
1530                  * port/irq combination.
1531                  */
1532                 shutdown(info);
1533                 info->irq = new_serial.irq;
1534                 info->port = new_serial.port;
1535                 info->hub6 = new_serial.hub6;
1536         }
1537         if(info->type != PORT_UNKNOWN)
1538                 request_region(info->port,8,"serial(set)");
1539 
1540         
1541 check_and_exit:
1542         if (!info->port || !info->type)
1543                 return 0;
1544         if (info->flags & ASYNC_INITIALIZED) {
1545                 if (((old_info.flags & ASYNC_SPD_MASK) !=
1546                      (info->flags & ASYNC_SPD_MASK)) ||
1547                     (old_info.custom_divisor != info->custom_divisor))
1548                         change_speed(info);
1549         } else
1550                 retval = startup(info);
1551         return retval;
1552 }
1553 
1554 
1555 /*
1556  * get_lsr_info - get line status register info
1557  *
1558  * Purpose: Let user call ioctl() to get info when the UART physically
1559  *          is emptied.  On bus types like RS485, the transmitter must
1560  *          release the bus after transmitting. This must be done when
1561  *          the transmit shift register is empty, not be done when the
1562  *          transmit holding register is empty.  This functionality
1563  *          allows RS485 driver to be written in user space. 
1564  */
1565 static int get_lsr_info(struct async_struct * info, unsigned int *value)
     /* [previous][next][first][last][top][bottom][index][help] */
1566 {
1567         unsigned char status;
1568         unsigned int result;
1569 
1570         cli();
1571         status = serial_in(info, UART_LSR);
1572         sti();
1573         result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1574         put_user(result,value);
1575         return 0;
1576 }
1577 
1578 
1579 static int get_modem_info(struct async_struct * info, unsigned int *value)
     /* [previous][next][first][last][top][bottom][index][help] */
1580 {
1581         unsigned char control, status;
1582         unsigned int result;
1583 
1584         control = info->MCR;
1585         cli();
1586         status = serial_in(info, UART_MSR);
1587         sti();
1588         result =  ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1589                 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1590                 | ((status  & UART_MSR_DCD) ? TIOCM_CAR : 0)
1591                 | ((status  & UART_MSR_RI) ? TIOCM_RNG : 0)
1592                 | ((status  & UART_MSR_DSR) ? TIOCM_DSR : 0)
1593                 | ((status  & UART_MSR_CTS) ? TIOCM_CTS : 0);
1594         put_user(result,value);
1595         return 0;
1596 }
1597 
1598 static int set_modem_info(struct async_struct * info, unsigned int cmd,
     /* [previous][next][first][last][top][bottom][index][help] */
1599                           unsigned int *value)
1600 {
1601         int error;
1602         unsigned int arg;
1603 
1604         error = verify_area(VERIFY_READ, value, sizeof(int));
1605         if (error)
1606                 return error;
1607         arg = get_user(value);
1608         switch (cmd) {
1609         case TIOCMBIS: 
1610                 if (arg & TIOCM_RTS) {
1611                         info->MCR |= UART_MCR_RTS;
1612                         info->MCR_noint |= UART_MCR_RTS;
1613                 }
1614                 if (arg & TIOCM_DTR) {
1615                         info->MCR |= UART_MCR_DTR;
1616                         info->MCR_noint |= UART_MCR_DTR;
1617                 }
1618                 break;
1619         case TIOCMBIC:
1620                 if (arg & TIOCM_RTS) {
1621                         info->MCR &= ~UART_MCR_RTS;
1622                         info->MCR_noint &= ~UART_MCR_RTS;
1623                 }
1624                 if (arg & TIOCM_DTR) {
1625                         info->MCR &= ~UART_MCR_DTR;
1626                         info->MCR_noint &= ~UART_MCR_DTR;
1627                 }
1628                 break;
1629         case TIOCMSET:
1630                 info->MCR = ((info->MCR & ~(UART_MCR_RTS | UART_MCR_DTR))
1631                              | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1632                              | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1633                 info->MCR_noint = ((info->MCR_noint
1634                                     & ~(UART_MCR_RTS | UART_MCR_DTR))
1635                                    | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1636                                    | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1637                 break;
1638         default:
1639                 return -EINVAL;
1640         }
1641         cli();
1642         serial_out(info, UART_MCR, info->MCR);
1643         sti();
1644         return 0;
1645 }
1646 
1647 static int do_autoconfig(struct async_struct * info)
     /* [previous][next][first][last][top][bottom][index][help] */
1648 {
1649         int                     retval;
1650         
1651         if (!suser())
1652                 return -EPERM;
1653         
1654         if (info->count > 1)
1655                 return -EBUSY;
1656         
1657         shutdown(info);
1658 
1659         cli();
1660         autoconfig(info);
1661         sti();
1662 
1663         retval = startup(info);
1664         if (retval)
1665                 return retval;
1666         return 0;
1667 }
1668 
1669 
1670 /*
1671  * This routine sends a break character out the serial port.
1672  */
1673 static void send_break( struct async_struct * info, int duration)
     /* [previous][next][first][last][top][bottom][index][help] */
1674 {
1675         if (!info->port)
1676                 return;
1677         current->state = TASK_INTERRUPTIBLE;
1678         current->timeout = jiffies + duration;
1679         cli();
1680         serial_out(info, UART_LCR, serial_inp(info, UART_LCR) | UART_LCR_SBC);
1681         schedule();
1682         serial_out(info, UART_LCR, serial_inp(info, UART_LCR) & ~UART_LCR_SBC);
1683         sti();
1684 }
1685 
1686 /*
1687  * This routine returns a bitfield of "wild interrupts".  Basically,
1688  * any unclaimed interrupts which is flapping around.
1689  */
1690 static int check_wild_interrupts(int doprint)
     /* [previous][next][first][last][top][bottom][index][help] */
1691 {
1692         int     i, mask;
1693         int     wild_interrupts = 0;
1694         int     irq_lines;
1695         unsigned long timeout;
1696         unsigned long flags;
1697         
1698         /* Turn on interrupts (they may be off) */
1699         save_flags(flags); sti();
1700 
1701         irq_lines = grab_all_interrupts(0);
1702         
1703         /*
1704          * Delay for 0.1 seconds -- we use a busy loop since this may 
1705          * occur during the bootup sequence
1706          */
1707         timeout = jiffies+10;
1708         while (timeout >= jiffies)
1709                 ;
1710         
1711         rs_triggered = 0;       /* Reset after letting things settle */
1712 
1713         timeout = jiffies+10;
1714         while (timeout >= jiffies)
1715                 ;
1716         
1717         for (i = 0, mask = 1; i < 16; i++, mask <<= 1) {
1718                 if ((rs_triggered & (1 << i)) &&
1719                     (irq_lines & (1 << i))) {
1720                         wild_interrupts |= mask;
1721                         if (doprint)
1722                                 printk("Wild interrupt?  (IRQ %d)\n", i);
1723                 }
1724         }
1725         free_all_interrupts(irq_lines);
1726         restore_flags(flags);
1727         return wild_interrupts;
1728 }
1729 
1730 static int get_multiport_struct(struct async_struct * info,
     /* [previous][next][first][last][top][bottom][index][help] */
1731                                 struct serial_multiport_struct *retinfo)
1732 {
1733         struct serial_multiport_struct ret;
1734         struct rs_multiport_struct *multi;
1735         
1736         multi = &rs_multiport[info->irq];
1737 
1738         ret.port_monitor = multi->port_monitor;
1739         
1740         ret.port1 = multi->port1;
1741         ret.mask1 = multi->mask1;
1742         ret.match1 = multi->match1;
1743         
1744         ret.port2 = multi->port2;
1745         ret.mask2 = multi->mask2;
1746         ret.match2 = multi->match2;
1747         
1748         ret.port3 = multi->port3;
1749         ret.mask3 = multi->mask3;
1750         ret.match3 = multi->match3;
1751         
1752         ret.port4 = multi->port4;
1753         ret.mask4 = multi->mask4;
1754         ret.match4 = multi->match4;
1755 
1756         ret.irq = info->irq;
1757 
1758         memcpy_tofs(retinfo,&ret,sizeof(*retinfo));
1759         return 0;
1760         
1761 }
1762 
1763 static int set_multiport_struct(struct async_struct * info,
     /* [previous][next][first][last][top][bottom][index][help] */
1764                                 struct serial_multiport_struct *in_multi)
1765 {
1766         struct serial_multiport_struct new_multi;
1767         struct rs_multiport_struct *multi;
1768         int     was_multi, now_multi;
1769         int     retval;
1770         void (*handler)(int, struct pt_regs *);
1771 
1772         if (!suser())
1773                 return -EPERM;
1774         if (!in_multi)
1775                 return -EFAULT;
1776         memcpy_fromfs(&new_multi, in_multi,
1777                       sizeof(struct serial_multiport_struct));
1778 
1779         if (new_multi.irq != info->irq || info->irq == 0 ||
1780             !IRQ_ports[info->irq])
1781                 return -EINVAL;
1782 
1783         multi = &rs_multiport[info->irq];
1784         was_multi = (multi->port1 != 0);
1785         
1786         multi->port_monitor = new_multi.port_monitor;
1787         
1788         multi->port1 = new_multi.port1;
1789         multi->mask1 = new_multi.mask1;
1790         multi->match1 = new_multi.match1;
1791 
1792         multi->port2 = new_multi.port2;
1793         multi->mask2 = new_multi.mask2;
1794         multi->match2 = new_multi.match2;
1795 
1796         multi->port3 = new_multi.port3;
1797         multi->mask3 = new_multi.mask3;
1798         multi->match3 = new_multi.match3;
1799 
1800         multi->port4 = new_multi.port4;
1801         multi->mask4 = new_multi.mask4;
1802         multi->match4 = new_multi.match4;
1803 
1804         now_multi = (multi->port1 != 0);
1805         
1806         if (IRQ_ports[info->irq]->next_port &&
1807             (was_multi != now_multi)) {
1808                 free_irq(info->irq);
1809                 if (now_multi)
1810                         handler = rs_interrupt_multi;
1811                 else
1812                         handler = rs_interrupt;
1813 
1814                 retval = request_irq(info->irq, handler, SA_INTERRUPT,
1815                                      "serial");
1816                 if (retval) {
1817                         printk("Couldn't reallocate serial interrupt "
1818                                "driver!!\n");
1819                 }
1820         }
1821 
1822         return 0;
1823 }
1824 
1825 static int rs_ioctl(struct tty_struct *tty, struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
1826                     unsigned int cmd, unsigned long arg)
1827 {
1828         int error;
1829         struct async_struct * info = (struct async_struct *)tty->driver_data;
1830         int retval;
1831 
1832         if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1833                 return -ENODEV;
1834 
1835         if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1836             (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
1837             (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1838                 if (tty->flags & (1 << TTY_IO_ERROR))
1839                     return -EIO;
1840         }
1841         
1842         switch (cmd) {
1843                 case TCSBRK:    /* SVID version: non-zero arg --> no break */
1844                         retval = tty_check_change(tty);
1845                         if (retval)
1846                                 return retval;
1847                         tty_wait_until_sent(tty, 0);
1848                         if (!arg)
1849                                 send_break(info, HZ/4); /* 1/4 second */
1850                         return 0;
1851                 case TCSBRKP:   /* support for POSIX tcsendbreak() */
1852                         retval = tty_check_change(tty);
1853                         if (retval)
1854                                 return retval;
1855                         tty_wait_until_sent(tty, 0);
1856                         send_break(info, arg ? arg*(HZ/10) : HZ/4);
1857                         return 0;
1858                 case TIOCGSOFTCAR:
1859                         error = verify_area(VERIFY_WRITE, (void *) arg,sizeof(long));
1860                         if (error)
1861                                 return error;
1862                         put_fs_long(C_CLOCAL(tty) ? 1 : 0,
1863                                     (unsigned long *) arg);
1864                         return 0;
1865                 case TIOCSSOFTCAR:
1866                         arg = get_fs_long((unsigned long *) arg);
1867                         tty->termios->c_cflag =
1868                                 ((tty->termios->c_cflag & ~CLOCAL) |
1869                                  (arg ? CLOCAL : 0));
1870                         return 0;
1871                 case TIOCMGET:
1872                         error = verify_area(VERIFY_WRITE, (void *) arg,
1873                                 sizeof(unsigned int));
1874                         if (error)
1875                                 return error;
1876                         return get_modem_info(info, (unsigned int *) arg);
1877                 case TIOCMBIS:
1878                 case TIOCMBIC:
1879                 case TIOCMSET:
1880                         return set_modem_info(info, cmd, (unsigned int *) arg);
1881                 case TIOCGSERIAL:
1882                         error = verify_area(VERIFY_WRITE, (void *) arg,
1883                                                 sizeof(struct serial_struct));
1884                         if (error)
1885                                 return error;
1886                         return get_serial_info(info,
1887                                                (struct serial_struct *) arg);
1888                 case TIOCSSERIAL:
1889                         return set_serial_info(info,
1890                                                (struct serial_struct *) arg);
1891                 case TIOCSERCONFIG:
1892                         return do_autoconfig(info);
1893 
1894                 case TIOCSERGWILD:
1895                         error = verify_area(VERIFY_WRITE, (void *) arg,
1896                                             sizeof(int));
1897                         if (error)
1898                                 return error;
1899                         put_fs_long(rs_wild_int_mask, (unsigned long *) arg);
1900                         return 0;
1901 
1902                 case TIOCSERGETLSR: /* Get line status register */
1903                         error = verify_area(VERIFY_WRITE, (void *) arg,
1904                                 sizeof(unsigned int));
1905                         if (error)
1906                                 return error;
1907                         else
1908                             return get_lsr_info(info, (unsigned int *) arg);
1909 
1910                 case TIOCSERSWILD:
1911                         if (!suser())
1912                                 return -EPERM;
1913                         rs_wild_int_mask = get_fs_long((unsigned long *) arg);
1914                         if (rs_wild_int_mask < 0)
1915                                 rs_wild_int_mask = check_wild_interrupts(0);
1916                         return 0;
1917 
1918                 case TIOCSERGSTRUCT:
1919                         error = verify_area(VERIFY_WRITE, (void *) arg,
1920                                                 sizeof(struct async_struct));
1921                         if (error)
1922                                 return error;
1923                         memcpy_tofs((struct async_struct *) arg,
1924                                     info, sizeof(struct async_struct));
1925                         return 0;
1926                         
1927                 case TIOCSERGETMULTI:
1928                         error = verify_area(VERIFY_WRITE, (void *) arg,
1929                                     sizeof(struct serial_multiport_struct));
1930                         if (error)
1931                                 return error;
1932                         return get_multiport_struct(info,
1933                                        (struct serial_multiport_struct *) arg);
1934                 case TIOCSERSETMULTI:
1935                         return set_multiport_struct(info,
1936                                        (struct serial_multiport_struct *) arg);
1937                 default:
1938                         return -ENOIOCTLCMD;
1939                 }
1940         return 0;
1941 }
1942 
1943 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
     /* [previous][next][first][last][top][bottom][index][help] */
1944 {
1945         struct async_struct *info = (struct async_struct *)tty->driver_data;
1946 
1947         if (tty->termios->c_cflag == old_termios->c_cflag)
1948                 return;
1949 
1950         change_speed(info);
1951 
1952         if ((old_termios->c_cflag & CRTSCTS) &&
1953             !(tty->termios->c_cflag & CRTSCTS)) {
1954                 tty->hw_stopped = 0;
1955                 rs_start(tty);
1956         }
1957 
1958 #if 0
1959         /*
1960          * No need to wake up processes in open wait, since they
1961          * sample the CLOCAL flag once, and don't recheck it.
1962          * XXX  It's not clear whether the current behavior is correct
1963          * or not.  Hence, this may change.....
1964          */
1965         if (!(old_termios->c_cflag & CLOCAL) &&
1966             (tty->termios->c_cflag & CLOCAL))
1967                 wake_up_interruptible(&info->open_wait);
1968 #endif
1969 }
1970 
1971 /*
1972  * ------------------------------------------------------------
1973  * rs_close()
1974  * 
1975  * This routine is called when the serial port gets closed.  First, we
1976  * wait for the last remaining data to be sent.  Then, we unlink its
1977  * async structure from the interrupt chain if necessary, and we free
1978  * that IRQ if nothing is left in the chain.
1979  * ------------------------------------------------------------
1980  */
1981 static void rs_close(struct tty_struct *tty, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
1982 {
1983         struct async_struct * info = (struct async_struct *)tty->driver_data;
1984         unsigned long flags;
1985         unsigned long timeout;
1986 
1987         if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1988                 return;
1989         
1990         save_flags(flags); cli();
1991         
1992         if (tty_hung_up_p(filp)) {
1993                 restore_flags(flags);
1994                 return;
1995         }
1996         
1997 #ifdef SERIAL_DEBUG_OPEN
1998         printk("rs_close ttys%d, count = %d\n", info->line, info->count);
1999 #endif
2000         if ((tty->count == 1) && (info->count != 1)) {
2001                 /*
2002                  * Uh, oh.  tty->count is 1, which means that the tty
2003                  * structure will be freed.  Info->count should always
2004                  * be one in these conditions.  If it's greater than
2005                  * one, we've got real problems, since it means the
2006                  * serial port won't be shutdown.
2007                  */
2008                 printk("rs_close: bad serial port count; tty->count is 1, "
2009                        "info->count is %d\n", info->count);
2010                 info->count = 1;
2011         }
2012         if (--info->count < 0) {
2013                 printk("rs_close: bad serial port count for ttys%d: %d\n",
2014                        info->line, info->count);
2015                 info->count = 0;
2016         }
2017         if (info->count) {
2018                 restore_flags(flags);
2019                 return;
2020         }
2021         info->flags |= ASYNC_CLOSING;
2022         /*
2023          * Save the termios structure, since this port may have
2024          * separate termios for callout and dialin.
2025          */
2026         if (info->flags & ASYNC_NORMAL_ACTIVE)
2027                 info->normal_termios = *tty->termios;
2028         if (info->flags & ASYNC_CALLOUT_ACTIVE)
2029                 info->callout_termios = *tty->termios;
2030         /*
2031          * Now we wait for the transmit buffer to clear; and we notify 
2032          * the line discipline to only process XON/XOFF characters.
2033          */
2034         tty->closing = 1;
2035         if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
2036                 tty_wait_until_sent(tty, info->closing_wait);
2037         /*
2038          * At this point we stop accepting input.  To do this, we
2039          * disable the receive line status interrupts, and tell the
2040          * interrupt driver to stop checking the data ready bit in the
2041          * line status register.
2042          */
2043         info->IER &= ~UART_IER_RLSI;
2044         info->read_status_mask &= ~UART_LSR_DR;
2045         if (info->flags & ASYNC_INITIALIZED) {
2046                 serial_out(info, UART_IER, info->IER);
2047                 /*
2048                  * Before we drop DTR, make sure the UART transmitter
2049                  * has completely drained; this is especially
2050                  * important if there is a transmit FIFO!
2051                  */
2052                 timeout = jiffies+HZ;
2053                 while (!(serial_inp(info, UART_LSR) & UART_LSR_TEMT)) {
2054                         current->state = TASK_INTERRUPTIBLE;
2055                         current->timeout = jiffies + info->timeout;
2056                         schedule();
2057                         if (jiffies > timeout)
2058                                 break;
2059                 }
2060         }
2061         shutdown(info);
2062         if (tty->driver.flush_buffer)
2063                 tty->driver.flush_buffer(tty);
2064         if (tty->ldisc.flush_buffer)
2065                 tty->ldisc.flush_buffer(tty);
2066         tty->closing = 0;
2067         info->event = 0;
2068         info->tty = 0;
2069         if (tty->ldisc.num != ldiscs[N_TTY].num) {
2070                 if (tty->ldisc.close)
2071                         (tty->ldisc.close)(tty);
2072                 tty->ldisc = ldiscs[N_TTY];
2073                 tty->termios->c_line = N_TTY;
2074                 if (tty->ldisc.open)
2075                         (tty->ldisc.open)(tty);
2076         }
2077         if (info->blocked_open) {
2078                 if (info->close_delay) {
2079                         current->state = TASK_INTERRUPTIBLE;
2080                         current->timeout = jiffies + info->close_delay;
2081                         schedule();
2082                 }
2083                 wake_up_interruptible(&info->open_wait);
2084         }
2085         info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
2086                          ASYNC_CLOSING);
2087         wake_up_interruptible(&info->close_wait);
2088         restore_flags(flags);
2089 }
2090 
2091 /*
2092  * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
2093  */
2094 void rs_hangup(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
2095 {
2096         struct async_struct * info = (struct async_struct *)tty->driver_data;
2097         
2098         if (serial_paranoia_check(info, tty->device, "rs_hangup"))
2099                 return;
2100         
2101         rs_flush_buffer(tty);
2102         shutdown(info);
2103         info->event = 0;
2104         info->count = 0;
2105         info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
2106         info->tty = 0;
2107         wake_up_interruptible(&info->open_wait);
2108 }
2109 
2110 /*
2111  * ------------------------------------------------------------
2112  * rs_open() and friends
2113  * ------------------------------------------------------------
2114  */
2115 static int block_til_ready(struct tty_struct *tty, struct file * filp,
     /* [previous][next][first][last][top][bottom][index][help] */
2116                            struct async_struct *info)
2117 {
2118         struct wait_queue wait = { current, NULL };
2119         int             retval;
2120         int             do_clocal = 0;
2121 
2122         /*
2123          * If the device is in the middle of being closed, then block
2124          * until it's done, and then try again.
2125          */
2126         if (info->flags & ASYNC_CLOSING) {
2127                 interruptible_sleep_on(&info->close_wait);
2128 #ifdef SERIAL_DO_RESTART
2129                 if (info->flags & ASYNC_HUP_NOTIFY)
2130                         return -EAGAIN;
2131                 else
2132                         return -ERESTARTSYS;
2133 #else
2134                 return -EAGAIN;
2135 #endif
2136         }
2137 
2138         /*
2139          * If this is a callout device, then just make sure the normal
2140          * device isn't being used.
2141          */
2142         if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
2143                 if (info->flags & ASYNC_NORMAL_ACTIVE)
2144                         return -EBUSY;
2145                 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2146                     (info->flags & ASYNC_SESSION_LOCKOUT) &&
2147                     (info->session != current->session))
2148                     return -EBUSY;
2149                 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2150                     (info->flags & ASYNC_PGRP_LOCKOUT) &&
2151                     (info->pgrp != current->pgrp))
2152                     return -EBUSY;
2153                 info->flags |= ASYNC_CALLOUT_ACTIVE;
2154                 return 0;
2155         }
2156         
2157         /*
2158          * If non-blocking mode is set, or the port is not enabled,
2159          * then make the check up front and then exit.
2160          */
2161         if ((filp->f_flags & O_NONBLOCK) ||
2162             (tty->flags & (1 << TTY_IO_ERROR))) {
2163                 if (info->flags & ASYNC_CALLOUT_ACTIVE)
2164                         return -EBUSY;
2165                 info->flags |= ASYNC_NORMAL_ACTIVE;
2166                 return 0;
2167         }
2168 
2169         if (info->flags & ASYNC_CALLOUT_ACTIVE) {
2170                 if (info->normal_termios.c_cflag & CLOCAL)
2171                         do_clocal = 1;
2172         } else {
2173                 if (tty->termios->c_cflag & CLOCAL)
2174                         do_clocal = 1;
2175         }
2176         
2177         /*
2178          * Block waiting for the carrier detect and the line to become
2179          * free (i.e., not in use by the callout).  While we are in
2180          * this loop, info->count is dropped by one, so that
2181          * rs_close() knows when to free things.  We restore it upon
2182          * exit, either normal or abnormal.
2183          */
2184         retval = 0;
2185         add_wait_queue(&info->open_wait, &wait);
2186 #ifdef SERIAL_DEBUG_OPEN
2187         printk("block_til_ready before block: ttys%d, count = %d\n",
2188                info->line, info->count);
2189 #endif
2190         info->count--;
2191         info->blocked_open++;
2192         while (1) {
2193                 cli();
2194                 if (!(info->flags & ASYNC_CALLOUT_ACTIVE))
2195                         serial_out(info, UART_MCR,
2196                                    serial_inp(info, UART_MCR) |
2197                                    (UART_MCR_DTR | UART_MCR_RTS));
2198                 sti();
2199                 current->state = TASK_INTERRUPTIBLE;
2200                 if (tty_hung_up_p(filp) ||
2201                     !(info->flags & ASYNC_INITIALIZED)) {
2202 #ifdef SERIAL_DO_RESTART
2203                         if (info->flags & ASYNC_HUP_NOTIFY)
2204                                 retval = -EAGAIN;
2205                         else
2206                                 retval = -ERESTARTSYS;  
2207 #else
2208                         retval = -EAGAIN;
2209 #endif
2210                         break;
2211                 }
2212                 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2213                     !(info->flags & ASYNC_CLOSING) &&
2214                     (do_clocal || (serial_in(info, UART_MSR) &
2215                                    UART_MSR_DCD)))
2216                         break;
2217                 if (current->signal & ~current->blocked) {
2218                         retval = -ERESTARTSYS;
2219                         break;
2220                 }
2221 #ifdef SERIAL_DEBUG_OPEN
2222                 printk("block_til_ready blocking: ttys%d, count = %d\n",
2223                        info->line, info->count);
2224 #endif
2225                 schedule();
2226         }
2227         current->state = TASK_RUNNING;
2228         remove_wait_queue(&info->open_wait, &wait);
2229         if (!tty_hung_up_p(filp))
2230                 info->count++;
2231         info->blocked_open--;
2232 #ifdef SERIAL_DEBUG_OPEN
2233         printk("block_til_ready after blocking: ttys%d, count = %d\n",
2234                info->line, info->count);
2235 #endif
2236         if (retval)
2237                 return retval;
2238         info->flags |= ASYNC_NORMAL_ACTIVE;
2239         return 0;
2240 }       
2241 
2242 /*
2243  * This routine is called whenever a serial port is opened.  It
2244  * enables interrupts for a serial port, linking in its async structure into
2245  * the IRQ chain.   It also performs the serial-specific
2246  * initialization for the tty structure.
2247  */
2248 int rs_open(struct tty_struct *tty, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
2249 {
2250         struct async_struct     *info;
2251         int                     retval, line;
2252 
2253         line = MINOR(tty->device) - tty->driver.minor_start;
2254         if ((line < 0) || (line >= NR_PORTS))
2255                 return -ENODEV;
2256         info = rs_table + line;
2257         if (serial_paranoia_check(info, tty->device, "rs_open"))
2258                 return -ENODEV;
2259 
2260 #ifdef SERIAL_DEBUG_OPEN
2261         printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
2262                info->count);
2263 #endif
2264         info->count++;
2265         tty->driver_data = info;
2266         info->tty = tty;
2267 
2268         if (!tmp_buf) {
2269                 tmp_buf = (unsigned char *) get_free_page(GFP_KERNEL);
2270                 if (!tmp_buf)
2271                         return -ENOMEM;
2272         }
2273         
2274         /*
2275          * Start up serial port
2276          */
2277         retval = startup(info);
2278         if (retval)
2279                 return retval;
2280 
2281         retval = block_til_ready(tty, filp, info);
2282         if (retval) {
2283 #ifdef SERIAL_DEBUG_OPEN
2284                 printk("rs_open returning after block_til_ready with %d\n",
2285                        retval);
2286 #endif
2287                 return retval;
2288         }
2289 
2290         if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
2291                 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2292                         *tty->termios = info->normal_termios;
2293                 else 
2294                         *tty->termios = info->callout_termios;
2295                 change_speed(info);
2296         }
2297 
2298         info->session = current->session;
2299         info->pgrp = current->pgrp;
2300 
2301 #ifdef SERIAL_DEBUG_OPEN
2302         printk("rs_open ttys%d successful...", info->line);
2303 #endif
2304         return 0;
2305 }
2306 
2307 /*
2308  * ---------------------------------------------------------------------
2309  * rs_init() and friends
2310  *
2311  * rs_init() is called at boot-time to initialize the serial driver.
2312  * ---------------------------------------------------------------------
2313  */
2314 
2315 /*
2316  * This routine prints out the appropriate serial driver version
2317  * number, and identifies which options were configured into this
2318  * driver.
2319  */
2320 static void show_serial_version(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2321 {
2322         printk("Serial driver version 4.11 with");
2323 #ifdef CONFIG_HUB6
2324         printk(" HUB-6");
2325 #define SERIAL_OPT
2326 #endif
2327 #ifdef SERIAL_OPT
2328         printk(" enabled\n");
2329 #else
2330         printk(" no serial options enabled\n");
2331 #endif
2332 #undef SERIAL_OPT
2333 }
2334 
2335 /*
2336  * This routine is called by do_auto_irq(); it attempts to determine
2337  * which interrupt a serial port is configured to use.  It is not
2338  * fool-proof, but it works a large part of the time.
2339  */
2340 static int get_auto_irq(struct async_struct *info)
     /* [previous][next][first][last][top][bottom][index][help] */
2341 {
2342         unsigned char save_MCR, save_IER, save_ICP=0;
2343         unsigned short ICP=0, port = info->port;
2344         unsigned long timeout;
2345         
2346         /*
2347          * Enable interrupts and see who answers
2348          */
2349         rs_irq_triggered = 0;
2350         cli();
2351         save_IER = serial_inp(info, UART_IER);
2352         save_MCR = serial_inp(info, UART_MCR);
2353         if (info->flags & ASYNC_FOURPORT)  {
2354                 serial_outp(info, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
2355                 serial_outp(info, UART_IER, 0x0f);      /* enable all intrs */
2356                 ICP = (port & 0xFE0) | 0x01F;
2357                 save_ICP = inb_p(ICP);
2358                 outb_p(0x80, ICP);
2359                 (void) inb_p(ICP);
2360         } else {
2361                 serial_outp(info, UART_MCR,
2362                             UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
2363                 serial_outp(info, UART_IER, 0x0f);      /* enable all intrs */
2364         }
2365         sti();
2366         /*
2367          * Next, clear the interrupt registers.
2368          */
2369         (void)serial_inp(info, UART_LSR);
2370         (void)serial_inp(info, UART_RX);
2371         (void)serial_inp(info, UART_IIR);
2372         (void)serial_inp(info, UART_MSR);
2373         
2374         timeout = jiffies+2;
2375         while (timeout >= jiffies) {
2376                 if (rs_irq_triggered)
2377                         break;
2378         }
2379         /*
2380          * Now check to see if we got any business, and clean up.
2381          */
2382         cli();
2383         serial_outp(info, UART_IER, save_IER);
2384         serial_outp(info, UART_MCR, save_MCR);
2385         if (info->flags & ASYNC_FOURPORT)
2386                 outb_p(save_ICP, ICP);
2387         sti();
2388         return(rs_irq_triggered);
2389 }
2390 
2391 /*
2392  * Calls get_auto_irq() multiple times, to make sure we don't get
2393  * faked out by random interrupts
2394  */
2395 static int do_auto_irq(struct async_struct * info)
     /* [previous][next][first][last][top][bottom][index][help] */
2396 {
2397         unsigned                port = info->port;
2398         int                     irq_lines = 0;
2399         int                     irq_try_1 = 0, irq_try_2 = 0;
2400         int                     retries;
2401         unsigned long flags;
2402 
2403         if (!port)
2404                 return 0;
2405 
2406         /* Turn on interrupts (they may be off) */
2407         save_flags(flags); sti();
2408 
2409         irq_lines = grab_all_interrupts(rs_wild_int_mask);
2410         
2411         for (retries = 0; retries < 5; retries++) {
2412                 if (!irq_try_1)
2413                         irq_try_1 = get_auto_irq(info);
2414                 if (!irq_try_2)
2415                         irq_try_2 = get_auto_irq(info);
2416                 if (irq_try_1 && irq_try_2) {
2417                         if (irq_try_1 == irq_try_2)
2418                                 break;
2419                         irq_try_1 = irq_try_2 = 0;
2420                 }
2421         }
2422         restore_flags(flags);
2423         free_all_interrupts(irq_lines);
2424         return (irq_try_1 == irq_try_2) ? irq_try_1 : 0;
2425 }
2426 
2427 /*
2428  * This routine is called by rs_init() to initialize a specific serial
2429  * port.  It determines what type of UART ship this serial port is
2430  * using: 8250, 16450, 16550, 16550A.  The important question is
2431  * whether or not this UART is a 16550A or not, since this will
2432  * determine whether or not we can use its FIFO features or not.
2433  */
2434 static void autoconfig(struct async_struct * info)
     /* [previous][next][first][last][top][bottom][index][help] */
2435 {
2436         unsigned char status1, status2, scratch, scratch2;
2437         unsigned port = info->port;
2438         unsigned long flags;
2439 
2440         info->type = PORT_UNKNOWN;
2441         
2442         if (!port)
2443                 return;
2444 
2445         save_flags(flags); cli();
2446         
2447         /*
2448          * Do a simple existence test first; if we fail this, there's
2449          * no point trying anything else.
2450          *
2451          * 0x80 is used as a nonsense port to prevent against false
2452          * positives due to ISA bus float.  The assumption is that
2453          * 0x80 is a non-existent port; which should be safe since
2454          * include/asm/io.h also makes this assumption.
2455          */
2456         scratch = serial_inp(info, UART_IER);
2457         serial_outp(info, UART_IER, 0);
2458         outb(0xff, 0x080);
2459         scratch2 = serial_inp(info, UART_IER);
2460         serial_outp(info, UART_IER, scratch);
2461         if (scratch2) {
2462                 restore_flags(flags);
2463                 return;         /* We failed; there's nothing here */
2464         }
2465 
2466         /* 
2467          * Check to see if a UART is really there.  Certain broken
2468          * internal modems based on the Rockwell chipset fail this
2469          * test, because they apparently don't implement the loopback
2470          * test mode.  So this test is skipped on the COM 1 through
2471          * COM 4 ports.  This *should* be safe, since no board
2472          * manufacturer would be stupid enough to design a board
2473          * that conflicts with COM 1-4 --- we hope!
2474          */
2475         if (!(info->flags & ASYNC_SKIP_TEST)) {
2476                 scratch = serial_inp(info, UART_MCR);
2477                 serial_outp(info, UART_MCR, UART_MCR_LOOP | scratch);
2478                 scratch2 = serial_inp(info, UART_MSR);
2479                 serial_outp(info, UART_MCR, UART_MCR_LOOP | 0x0A);
2480                 status1 = serial_inp(info, UART_MSR) & 0xF0;
2481                 serial_outp(info, UART_MCR, scratch);
2482                 serial_outp(info, UART_MSR, scratch2);
2483                 if (status1 != 0x90) {
2484                         restore_flags(flags);
2485                         return;
2486                 }
2487         } 
2488         
2489         /*
2490          * If the AUTO_IRQ flag is set, try to do the automatic IRQ
2491          * detection.
2492          */
2493         if (info->flags & ASYNC_AUTO_IRQ)
2494                 info->irq = do_auto_irq(info);
2495                 
2496         scratch2 = serial_in(info, UART_LCR);
2497         serial_outp(info, UART_LCR, scratch2 | UART_LCR_DLAB);
2498         serial_outp(info, UART_EFR, 0); /* EFR is the same as FCR */
2499         serial_outp(info, UART_LCR, scratch2);
2500         serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
2501         scratch = serial_in(info, UART_IIR) >> 6;
2502         info->xmit_fifo_size = 1;
2503         switch (scratch) {
2504                 case 0:
2505                         info->type = PORT_16450;
2506                         break;
2507                 case 1:
2508                         info->type = PORT_UNKNOWN;
2509                         break;
2510                 case 2:
2511                         info->type = PORT_16550;
2512                         break;
2513                 case 3:
2514                         serial_outp(info, UART_LCR, scratch2 | UART_LCR_DLAB);
2515                         if (serial_in(info, UART_EFR) == 0) {
2516                                 info->type = PORT_16650;
2517                                 info->xmit_fifo_size = 32;
2518                         } else {
2519                                 info->type = PORT_16550A;
2520                                 info->xmit_fifo_size = 16;
2521                         }
2522                         serial_outp(info, UART_LCR, scratch2);
2523                         break;
2524         }
2525         if (info->type == PORT_16450) {
2526                 scratch = serial_in(info, UART_SCR);
2527                 serial_outp(info, UART_SCR, 0xa5);
2528                 status1 = serial_in(info, UART_SCR);
2529                 serial_outp(info, UART_SCR, 0x5a);
2530                 status2 = serial_in(info, UART_SCR);
2531                 serial_outp(info, UART_SCR, scratch);
2532 
2533                 if ((status1 != 0xa5) || (status2 != 0x5a))
2534                         info->type = PORT_8250;
2535         }
2536         request_region(info->port,8,"serial(auto)");
2537 
2538         /*
2539          * Reset the UART.
2540          */
2541 #if defined(__alpha__) && !defined(CONFIG_PCI)
2542         /*
2543          * I wonder what DEC did to the OUT1 and OUT2 lines?
2544          * clearing them results in endless interrupts.
2545          */
2546         serial_outp(info, UART_MCR, 0x0c);
2547 #else
2548         serial_outp(info, UART_MCR, 0x00);
2549 #endif
2550         serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
2551                                      UART_FCR_CLEAR_XMIT));
2552         (void)serial_in(info, UART_RX);
2553         
2554         restore_flags(flags);
2555 }
2556 
2557 /*
2558  * The serial driver boot-time initialization code!
2559  */
2560 long rs_init(long kmem_start)
     /* [previous][next][first][last][top][bottom][index][help] */
2561 {
2562         int i;
2563         struct async_struct * info;
2564         
2565         bh_base[SERIAL_BH].routine = do_serial_bh;
2566         enable_bh(SERIAL_BH);
2567         timer_table[RS_TIMER].fn = rs_timer;
2568         timer_table[RS_TIMER].expires = 0;
2569 #ifdef CONFIG_AUTO_IRQ
2570         rs_wild_int_mask = check_wild_interrupts(1);
2571 #endif
2572 
2573         for (i = 0; i < 16; i++) {
2574                 IRQ_ports[i] = 0;
2575                 IRQ_timeout[i] = 0;
2576                 memset(&rs_multiport[i], 0, sizeof(struct rs_multiport_struct));
2577         }
2578         
2579         show_serial_version();
2580 
2581         /* Initialize the tty_driver structure */
2582         
2583         memset(&serial_driver, 0, sizeof(struct tty_driver));
2584         serial_driver.magic = TTY_DRIVER_MAGIC;
2585         serial_driver.name = "ttyS";
2586         serial_driver.major = TTY_MAJOR;
2587         serial_driver.minor_start = 64;
2588         serial_driver.num = NR_PORTS;
2589         serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2590         serial_driver.subtype = SERIAL_TYPE_NORMAL;
2591         serial_driver.init_termios = tty_std_termios;
2592         serial_driver.init_termios.c_cflag =
2593                 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2594         serial_driver.flags = TTY_DRIVER_REAL_RAW;
2595         serial_driver.refcount = &serial_refcount;
2596         serial_driver.table = serial_table;
2597         serial_driver.termios = serial_termios;
2598         serial_driver.termios_locked = serial_termios_locked;
2599 
2600         serial_driver.open = rs_open;
2601         serial_driver.close = rs_close;
2602         serial_driver.write = rs_write;
2603         serial_driver.put_char = rs_put_char;
2604         serial_driver.flush_chars = rs_flush_chars;
2605         serial_driver.write_room = rs_write_room;
2606         serial_driver.chars_in_buffer = rs_chars_in_buffer;
2607         serial_driver.flush_buffer = rs_flush_buffer;
2608         serial_driver.ioctl = rs_ioctl;
2609         serial_driver.throttle = rs_throttle;
2610         serial_driver.unthrottle = rs_unthrottle;
2611         serial_driver.set_termios = rs_set_termios;
2612         serial_driver.stop = rs_stop;
2613         serial_driver.start = rs_start;
2614         serial_driver.hangup = rs_hangup;
2615 
2616         /*
2617          * The callout device is just like normal device except for
2618          * major number and the subtype code.
2619          */
2620         callout_driver = serial_driver;
2621         callout_driver.name = "cua";
2622         callout_driver.major = TTYAUX_MAJOR;
2623         callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2624 
2625         if (tty_register_driver(&serial_driver))
2626                 panic("Couldn't register serial driver\n");
2627         if (tty_register_driver(&callout_driver))
2628                 panic("Couldn't register callout driver\n");
2629         
2630         for (i = 0, info = rs_table; i < NR_PORTS; i++,info++) {
2631                 info->magic = SERIAL_MAGIC;
2632                 info->line = i;
2633                 info->tty = 0;
2634                 info->type = PORT_UNKNOWN;
2635                 info->custom_divisor = 0;
2636                 info->close_delay = 50;
2637                 info->closing_wait = 3000;
2638                 info->x_char = 0;
2639                 info->event = 0;
2640                 info->count = 0;
2641                 info->blocked_open = 0;
2642                 info->tqueue.routine = do_softint;
2643                 info->tqueue.data = info;
2644                 info->tqueue_hangup.routine = do_serial_hangup;
2645                 info->tqueue_hangup.data = info;
2646                 info->callout_termios =callout_driver.init_termios;
2647                 info->normal_termios = serial_driver.init_termios;
2648                 info->open_wait = 0;
2649                 info->close_wait = 0;
2650                 info->next_port = 0;
2651                 info->prev_port = 0;
2652                 if (info->irq == 2)
2653                         info->irq = 9;
2654                 if (!(info->flags & ASYNC_BOOT_AUTOCONF))
2655                         continue;
2656                 autoconfig(info);
2657                 if (info->type == PORT_UNKNOWN)
2658                         continue;
2659                 printk("tty%02d%s at 0x%04x (irq = %d)", info->line, 
2660                        (info->flags & ASYNC_FOURPORT) ? " FourPort" : "",
2661                        info->port, info->irq);
2662                 switch (info->type) {
2663                         case PORT_8250:
2664                                 printk(" is a 8250\n");
2665                                 break;
2666                         case PORT_16450:
2667                                 printk(" is a 16450\n");
2668                                 break;
2669                         case PORT_16550:
2670                                 printk(" is a 16550\n");
2671                                 break;
2672                         case PORT_16550A:
2673                                 printk(" is a 16550A\n");
2674                                 break;
2675                         case PORT_16650:
2676                                 printk(" is a 16650\n");
2677                                 break;
2678                         default:
2679                                 printk("\n");
2680                                 break;
2681                 }
2682         }
2683         return kmem_start;
2684 }
2685 
2686 /*
2687  * register_serial and unregister_serial allows for serial ports to be
2688  * configured at run-time, to support PCMCIA modems.
2689  */
2690 int register_serial(struct serial_struct *req)
     /* [previous][next][first][last][top][bottom][index][help] */
2691 {
2692         int i;
2693         unsigned long flags;
2694         struct async_struct *info;
2695 
2696         save_flags(flags);
2697         cli();
2698         for (i = 0; i < NR_PORTS; i++) {
2699                 if (rs_table[i].port == req->port)
2700                         break;
2701         }
2702         if (i == NR_PORTS) {
2703                 for (i = 0; i < NR_PORTS; i++)
2704                         if ((rs_table[i].type == PORT_UNKNOWN) &&
2705                             (rs_table[i].count == 0))
2706                                 break;
2707         }
2708         if (i == NR_PORTS) {
2709                 restore_flags(flags);
2710                 return -1;
2711         }
2712         info = &rs_table[i];
2713         if (rs_table[i].count) {
2714                 restore_flags(flags);
2715                 printk("Couldn't configure serial #%d (port=%d,irq=%d): "
2716                        "device already open\n", i, req->port, req->irq);
2717                 return -1;
2718         }
2719         info->irq = req->irq;
2720         info->port = req->port;
2721         autoconfig(info);
2722         if (info->type == PORT_UNKNOWN) {
2723                 restore_flags(flags);
2724                 printk("register_serial(): autoconfig failed\n");
2725                 return -1;
2726         }
2727         printk("tty%02d at 0x%04x (irq = %d)", info->line, 
2728                info->port, info->irq);
2729         switch (info->type) {
2730         case PORT_8250:
2731                 printk(" is a 8250\n"); break;
2732         case PORT_16450:
2733                 printk(" is a 16450\n"); break;
2734         case PORT_16550:
2735                 printk(" is a 16550\n"); break;
2736         case PORT_16550A:
2737                 printk(" is a 16550A\n"); break;
2738         default:
2739                 printk("\n"); break;
2740         }
2741         restore_flags(flags);
2742         return info->line;
2743 }
2744 
2745 void unregister_serial(int line)
     /* [previous][next][first][last][top][bottom][index][help] */
2746 {
2747         unsigned long flags;
2748         struct async_struct *info = &rs_table[line];
2749 
2750         save_flags(flags);
2751         cli();
2752         if (info->tty)
2753                 tty_hangup(info->tty);
2754         info->type = PORT_UNKNOWN;
2755         printk("tty%02d unloaded\n", info->line);
2756         restore_flags(flags);
2757 }

/* [previous][next][first][last][top][bottom][index][help] */