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 = 6000; /* 60 seconds === a long time :-) */
 854 
 855         info = IRQ_ports[irq];
 856         if (!info) {
 857                 IRQ_timeout[irq] = 6000;
 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 #ifdef __alpha__
 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         cval = cflag & (CSIZE | CSTOPB);
1170         cval >>= 4;
1171         if (cflag & PARENB)
1172                 cval |= UART_LCR_PARITY;
1173         if (!(cflag & PARODD))
1174                 cval |= UART_LCR_EPAR;
1175         if (info->type == PORT_16550A) {
1176                 if ((info->baud_base / quot) < 2400)
1177                         fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
1178                 else
1179                         fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
1180         } else if (info->type == PORT_16650) {
1181                 /*
1182                  * On the 16650, we disable the FIFOs altogether
1183                  * because of a design bug in how the implement
1184                  * things.  We could support it by completely changing
1185                  * how we handle the interrupt driver, but not today....
1186                  *
1187                  * N.B.  Because there's no way to set a FIFO trigger
1188                  * at 1 char, we'd probably disable at speed below
1189                  * 2400 baud anyway...
1190                  */
1191                 fcr = 0;
1192         } else
1193                 fcr = 0;
1194         
1195         /* CTS flow control flag and modem status interrupts */
1196         info->IER &= ~UART_IER_MSI;
1197         if (cflag & CRTSCTS) {
1198                 info->flags |= ASYNC_CTS_FLOW;
1199                 info->IER |= UART_IER_MSI;
1200         } else
1201                 info->flags &= ~ASYNC_CTS_FLOW;
1202         if (cflag & CLOCAL)
1203                 info->flags &= ~ASYNC_CHECK_CD;
1204         else {
1205                 info->flags |= ASYNC_CHECK_CD;
1206                 info->IER |= UART_IER_MSI;
1207         }
1208         serial_out(info, UART_IER, info->IER);
1209 
1210         /*
1211          * Set up parity check flag
1212          */
1213         info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1214         if (I_INPCK(info->tty))
1215                 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1216         if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1217                 info->read_status_mask |= UART_LSR_BI;
1218         
1219         info->ignore_status_mask = 0;
1220         if (I_IGNPAR(info->tty)) {
1221                 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1222                 info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
1223         }
1224         if (I_IGNBRK(info->tty)) {
1225                 info->ignore_status_mask |= UART_LSR_BI;
1226                 info->read_status_mask |= UART_LSR_BI;
1227                 /*
1228                  * If we're ignore parity and break indicators, ignore 
1229                  * overruns too.  (For real raw support).
1230                  */
1231                 if (I_IGNPAR(info->tty)) {
1232                         info->ignore_status_mask |= UART_LSR_OE;
1233                         info->read_status_mask |= UART_LSR_OE;
1234                 }
1235         }
1236         
1237         cli();
1238         serial_outp(info, UART_LCR, cval | UART_LCR_DLAB);      /* set DLAB */
1239         serial_outp(info, UART_DLL, quot & 0xff);       /* LS of divisor */
1240         serial_outp(info, UART_DLM, quot >> 8);         /* MS of divisor */
1241         serial_outp(info, UART_LCR, cval);              /* reset DLAB */
1242         serial_outp(info, UART_FCR, fcr);       /* set fcr */
1243         sti();
1244 }
1245 
1246 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
     /* [previous][next][first][last][top][bottom][index][help] */
1247 {
1248         struct async_struct *info = (struct async_struct *)tty->driver_data;
1249         unsigned long flags;
1250 
1251         if (serial_paranoia_check(info, tty->device, "rs_put_char"))
1252                 return;
1253 
1254         if (!tty || !info->xmit_buf)
1255                 return;
1256 
1257         save_flags(flags); cli();
1258         if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
1259                 restore_flags(flags);
1260                 return;
1261         }
1262 
1263         info->xmit_buf[info->xmit_head++] = ch;
1264         info->xmit_head &= SERIAL_XMIT_SIZE-1;
1265         info->xmit_cnt++;
1266         restore_flags(flags);
1267 }
1268 
1269 static void rs_flush_chars(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1270 {
1271         struct async_struct *info = (struct async_struct *)tty->driver_data;
1272         unsigned long flags;
1273                                 
1274         if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
1275                 return;
1276 
1277         if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1278             !info->xmit_buf)
1279                 return;
1280 
1281         save_flags(flags); cli();
1282         info->IER |= UART_IER_THRI;
1283         serial_out(info, UART_IER, info->IER);
1284         restore_flags(flags);
1285 }
1286 
1287 static int rs_write(struct tty_struct * tty, int from_user,
     /* [previous][next][first][last][top][bottom][index][help] */
1288                     unsigned char *buf, int count)
1289 {
1290         int     c, total = 0;
1291         struct async_struct *info = (struct async_struct *)tty->driver_data;
1292         unsigned long flags;
1293                                 
1294         if (serial_paranoia_check(info, tty->device, "rs_write"))
1295                 return 0;
1296 
1297         if (!tty || !info->xmit_buf || !tmp_buf)
1298                 return 0;
1299             
1300         save_flags(flags);
1301         while (1) {
1302                 cli();          
1303                 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1304                                    SERIAL_XMIT_SIZE - info->xmit_head));
1305                 if (c <= 0)
1306                         break;
1307 
1308                 if (from_user) {
1309                         down(&tmp_buf_sem);
1310                         memcpy_fromfs(tmp_buf, buf, c);
1311                         c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1312                                        SERIAL_XMIT_SIZE - info->xmit_head));
1313                         memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1314                         up(&tmp_buf_sem);
1315                 } else
1316                         memcpy(info->xmit_buf + info->xmit_head, buf, c);
1317                 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1318                 info->xmit_cnt += c;
1319                 restore_flags(flags);
1320                 buf += c;
1321                 count -= c;
1322                 total += c;
1323         }
1324         if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1325             !(info->IER & UART_IER_THRI)) {
1326                 info->IER |= UART_IER_THRI;
1327                 serial_out(info, UART_IER, info->IER);
1328         }
1329         restore_flags(flags);
1330         return total;
1331 }
1332 
1333 static int rs_write_room(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1334 {
1335         struct async_struct *info = (struct async_struct *)tty->driver_data;
1336         int     ret;
1337                                 
1338         if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1339                 return 0;
1340         ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1341         if (ret < 0)
1342                 ret = 0;
1343         return ret;
1344 }
1345 
1346 static int rs_chars_in_buffer(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1347 {
1348         struct async_struct *info = (struct async_struct *)tty->driver_data;
1349                                 
1350         if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1351                 return 0;
1352         return info->xmit_cnt;
1353 }
1354 
1355 static void rs_flush_buffer(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1356 {
1357         struct async_struct *info = (struct async_struct *)tty->driver_data;
1358                                 
1359         if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1360                 return;
1361         cli();
1362         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1363         sti();
1364         wake_up_interruptible(&tty->write_wait);
1365         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1366             tty->ldisc.write_wakeup)
1367                 (tty->ldisc.write_wakeup)(tty);
1368 }
1369 
1370 /*
1371  * ------------------------------------------------------------
1372  * rs_throttle()
1373  * 
1374  * This routine is called by the upper-layer tty layer to signal that
1375  * incoming characters should be throttled.
1376  * ------------------------------------------------------------
1377  */
1378 static void rs_throttle(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1379 {
1380         struct async_struct *info = (struct async_struct *)tty->driver_data;
1381 #ifdef SERIAL_DEBUG_THROTTLE
1382         char    buf[64];
1383         
1384         printk("throttle %s: %d....\n", _tty_name(tty, buf),
1385                tty->ldisc.chars_in_buffer(tty));
1386 #endif
1387 
1388         if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1389                 return;
1390         
1391         if (I_IXOFF(tty))
1392                 info->x_char = STOP_CHAR(tty);
1393 
1394         info->MCR &= ~UART_MCR_RTS;
1395         info->MCR_noint &= ~UART_MCR_RTS;
1396         cli();
1397         serial_out(info, UART_MCR, info->MCR);
1398         sti();
1399 }
1400 
1401 static void rs_unthrottle(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1402 {
1403         struct async_struct *info = (struct async_struct *)tty->driver_data;
1404 #ifdef SERIAL_DEBUG_THROTTLE
1405         char    buf[64];
1406         
1407         printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1408                tty->ldisc.chars_in_buffer(tty));
1409 #endif
1410 
1411         if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1412                 return;
1413         
1414         if (I_IXOFF(tty)) {
1415                 if (info->x_char)
1416                         info->x_char = 0;
1417                 else
1418                         info->x_char = START_CHAR(tty);
1419         }
1420         info->MCR |= UART_MCR_RTS;
1421         info->MCR_noint |= UART_MCR_RTS;
1422         cli();
1423         serial_out(info, UART_MCR, info->MCR);
1424         sti();
1425 }
1426 
1427 /*
1428  * ------------------------------------------------------------
1429  * rs_ioctl() and friends
1430  * ------------------------------------------------------------
1431  */
1432 
1433 static int get_serial_info(struct async_struct * info,
     /* [previous][next][first][last][top][bottom][index][help] */
1434                            struct serial_struct * retinfo)
1435 {
1436         struct serial_struct tmp;
1437   
1438         if (!retinfo)
1439                 return -EFAULT;
1440         memset(&tmp, 0, sizeof(tmp));
1441         tmp.type = info->type;
1442         tmp.line = info->line;
1443         tmp.port = info->port;
1444         tmp.irq = info->irq;
1445         tmp.flags = info->flags;
1446         tmp.baud_base = info->baud_base;
1447         tmp.close_delay = info->close_delay;
1448         tmp.closing_wait = info->closing_wait;
1449         tmp.custom_divisor = info->custom_divisor;
1450         tmp.hub6 = info->hub6;
1451         memcpy_tofs(retinfo,&tmp,sizeof(*retinfo));
1452         return 0;
1453 }
1454 
1455 static int set_serial_info(struct async_struct * info,
     /* [previous][next][first][last][top][bottom][index][help] */
1456                            struct serial_struct * new_info)
1457 {
1458         struct serial_struct new_serial;
1459         struct async_struct old_info;
1460         unsigned int            i,change_irq,change_port;
1461         int                     retval = 0;
1462 
1463         if (!new_info)
1464                 return -EFAULT;
1465         memcpy_fromfs(&new_serial,new_info,sizeof(new_serial));
1466         old_info = *info;
1467 
1468         change_irq = new_serial.irq != info->irq;
1469         change_port = (new_serial.port != info->port) || (new_serial.hub6 != info->hub6);
1470 
1471         if (!suser()) {
1472                 if (change_irq || change_port ||
1473                     (new_serial.baud_base != info->baud_base) ||
1474                     (new_serial.type != info->type) ||
1475                     (new_serial.close_delay != info->close_delay) ||
1476                     ((new_serial.flags & ~ASYNC_USR_MASK) !=
1477                      (info->flags & ~ASYNC_USR_MASK)))
1478                         return -EPERM;
1479                 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1480                                (new_serial.flags & ASYNC_USR_MASK));
1481                 info->custom_divisor = new_serial.custom_divisor;
1482                 goto check_and_exit;
1483         }
1484 
1485         if (new_serial.irq == 2)
1486                 new_serial.irq = 9;
1487 
1488         if ((new_serial.irq > 15) || (new_serial.port > 0xffff) ||
1489             (new_serial.type < PORT_UNKNOWN) || (new_serial.type > PORT_MAX)) {
1490                 return -EINVAL;
1491         }
1492 
1493         /* Make sure address is not already in use */
1494         if (new_serial.type) {
1495                 for (i = 0 ; i < NR_PORTS; i++)
1496                         if ((info != &rs_table[i]) &&
1497                             (rs_table[i].port == new_serial.port) &&
1498                             rs_table[i].type)
1499                                 return -EADDRINUSE;
1500         }
1501 
1502         if ((change_port || change_irq) && (info->count > 1))
1503                 return -EBUSY;
1504 
1505         /*
1506          * OK, past this point, all the error checking has been done.
1507          * At this point, we start making changes.....
1508          */
1509 
1510         info->baud_base = new_serial.baud_base;
1511         info->flags = ((info->flags & ~ASYNC_FLAGS) |
1512                         (new_serial.flags & ASYNC_FLAGS));
1513         info->custom_divisor = new_serial.custom_divisor;
1514         info->type = new_serial.type;
1515         info->close_delay = new_serial.close_delay;
1516         info->closing_wait = new_serial.closing_wait;
1517 
1518         release_region(info->port,8);
1519         if (change_port || change_irq) {
1520                 /*
1521                  * We need to shutdown the serial port at the old
1522                  * port/irq combination.
1523                  */
1524                 shutdown(info);
1525                 info->irq = new_serial.irq;
1526                 info->port = new_serial.port;
1527                 info->hub6 = new_serial.hub6;
1528         }
1529         if(info->type != PORT_UNKNOWN)
1530                 request_region(info->port,8,"serial(set)");
1531 
1532         
1533 check_and_exit:
1534         if (!info->port || !info->type)
1535                 return 0;
1536         if (info->flags & ASYNC_INITIALIZED) {
1537                 if (((old_info.flags & ASYNC_SPD_MASK) !=
1538                      (info->flags & ASYNC_SPD_MASK)) ||
1539                     (old_info.custom_divisor != info->custom_divisor))
1540                         change_speed(info);
1541         } else
1542                 retval = startup(info);
1543         return retval;
1544 }
1545 
1546 
1547 /*
1548  * get_lsr_info - get line status register info
1549  *
1550  * Purpose: Let user call ioctl() to get info when the UART physically
1551  *          is emptied.  On bus types like RS485, the transmitter must
1552  *          release the bus after transmitting. This must be done when
1553  *          the transmit shift register is empty, not be done when the
1554  *          transmit holding register is empty.  This functionality
1555  *          allows RS485 driver to be written in user space. 
1556  */
1557 static int get_lsr_info(struct async_struct * info, unsigned int *value)
     /* [previous][next][first][last][top][bottom][index][help] */
1558 {
1559         unsigned char status;
1560         unsigned int result;
1561 
1562         cli();
1563         status = serial_in(info, UART_LSR);
1564         sti();
1565         result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1566         put_fs_long(result,(unsigned long *) value);
1567         return 0;
1568 }
1569 
1570 
1571 static int get_modem_info(struct async_struct * info, unsigned int *value)
     /* [previous][next][first][last][top][bottom][index][help] */
1572 {
1573         unsigned char control, status;
1574         unsigned int result;
1575 
1576         control = info->MCR;
1577         cli();
1578         status = serial_in(info, UART_MSR);
1579         sti();
1580         result =  ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1581                 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1582                 | ((status  & UART_MSR_DCD) ? TIOCM_CAR : 0)
1583                 | ((status  & UART_MSR_RI) ? TIOCM_RNG : 0)
1584                 | ((status  & UART_MSR_DSR) ? TIOCM_DSR : 0)
1585                 | ((status  & UART_MSR_CTS) ? TIOCM_CTS : 0);
1586         put_fs_long(result,(unsigned long *) value);
1587         return 0;
1588 }
1589 
1590 static int set_modem_info(struct async_struct * info, unsigned int cmd,
     /* [previous][next][first][last][top][bottom][index][help] */
1591                           unsigned int *value)
1592 {
1593         int error;
1594         unsigned int arg;
1595 
1596         error = verify_area(VERIFY_READ, value, sizeof(int));
1597         if (error)
1598                 return error;
1599         arg = get_fs_long((unsigned long *) value);
1600         switch (cmd) {
1601         case TIOCMBIS: 
1602                 if (arg & TIOCM_RTS) {
1603                         info->MCR |= UART_MCR_RTS;
1604                         info->MCR_noint |= UART_MCR_RTS;
1605                 }
1606                 if (arg & TIOCM_DTR) {
1607                         info->MCR |= UART_MCR_DTR;
1608                         info->MCR_noint |= UART_MCR_DTR;
1609                 }
1610                 break;
1611         case TIOCMBIC:
1612                 if (arg & TIOCM_RTS) {
1613                         info->MCR &= ~UART_MCR_RTS;
1614                         info->MCR_noint &= ~UART_MCR_RTS;
1615                 }
1616                 if (arg & TIOCM_DTR) {
1617                         info->MCR &= ~UART_MCR_DTR;
1618                         info->MCR_noint &= ~UART_MCR_DTR;
1619                 }
1620                 break;
1621         case TIOCMSET:
1622                 info->MCR = ((info->MCR & ~(UART_MCR_RTS | UART_MCR_DTR))
1623                              | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1624                              | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1625                 info->MCR_noint = ((info->MCR_noint
1626                                     & ~(UART_MCR_RTS | UART_MCR_DTR))
1627                                    | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1628                                    | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1629                 break;
1630         default:
1631                 return -EINVAL;
1632         }
1633         cli();
1634         serial_out(info, UART_MCR, info->MCR);
1635         sti();
1636         return 0;
1637 }
1638 
1639 static int do_autoconfig(struct async_struct * info)
     /* [previous][next][first][last][top][bottom][index][help] */
1640 {
1641         int                     retval;
1642         
1643         if (!suser())
1644                 return -EPERM;
1645         
1646         if (info->count > 1)
1647                 return -EBUSY;
1648         
1649         shutdown(info);
1650 
1651         cli();
1652         autoconfig(info);
1653         sti();
1654 
1655         retval = startup(info);
1656         if (retval)
1657                 return retval;
1658         return 0;
1659 }
1660 
1661 
1662 /*
1663  * This routine sends a break character out the serial port.
1664  */
1665 static void send_break( struct async_struct * info, int duration)
     /* [previous][next][first][last][top][bottom][index][help] */
1666 {
1667         if (!info->port)
1668                 return;
1669         current->state = TASK_INTERRUPTIBLE;
1670         current->timeout = jiffies + duration;
1671         cli();
1672         serial_out(info, UART_LCR, serial_inp(info, UART_LCR) | UART_LCR_SBC);
1673         schedule();
1674         serial_out(info, UART_LCR, serial_inp(info, UART_LCR) & ~UART_LCR_SBC);
1675         sti();
1676 }
1677 
1678 /*
1679  * This routine returns a bitfield of "wild interrupts".  Basically,
1680  * any unclaimed interrupts which is flapping around.
1681  */
1682 static int check_wild_interrupts(int doprint)
     /* [previous][next][first][last][top][bottom][index][help] */
1683 {
1684         int     i, mask;
1685         int     wild_interrupts = 0;
1686         int     irq_lines;
1687         unsigned long timeout;
1688         unsigned long flags;
1689         
1690         /* Turn on interrupts (they may be off) */
1691         save_flags(flags); sti();
1692 
1693         irq_lines = grab_all_interrupts(0);
1694         
1695         /*
1696          * Delay for 0.1 seconds -- we use a busy loop since this may 
1697          * occur during the bootup sequence
1698          */
1699         timeout = jiffies+10;
1700         while (timeout >= jiffies)
1701                 ;
1702         
1703         rs_triggered = 0;       /* Reset after letting things settle */
1704 
1705         timeout = jiffies+10;
1706         while (timeout >= jiffies)
1707                 ;
1708         
1709         for (i = 0, mask = 1; i < 16; i++, mask <<= 1) {
1710                 if ((rs_triggered & (1 << i)) &&
1711                     (irq_lines & (1 << i))) {
1712                         wild_interrupts |= mask;
1713                         if (doprint)
1714                                 printk("Wild interrupt?  (IRQ %d)\n", i);
1715                 }
1716         }
1717         free_all_interrupts(irq_lines);
1718         restore_flags(flags);
1719         return wild_interrupts;
1720 }
1721 
1722 static int get_multiport_struct(struct async_struct * info,
     /* [previous][next][first][last][top][bottom][index][help] */
1723                                 struct serial_multiport_struct *retinfo)
1724 {
1725         struct serial_multiport_struct ret;
1726         struct rs_multiport_struct *multi;
1727         
1728         multi = &rs_multiport[info->irq];
1729 
1730         ret.port_monitor = multi->port_monitor;
1731         
1732         ret.port1 = multi->port1;
1733         ret.mask1 = multi->mask1;
1734         ret.match1 = multi->match1;
1735         
1736         ret.port2 = multi->port2;
1737         ret.mask2 = multi->mask2;
1738         ret.match2 = multi->match2;
1739         
1740         ret.port3 = multi->port3;
1741         ret.mask3 = multi->mask3;
1742         ret.match3 = multi->match3;
1743         
1744         ret.port4 = multi->port4;
1745         ret.mask4 = multi->mask4;
1746         ret.match4 = multi->match4;
1747 
1748         ret.irq = info->irq;
1749 
1750         memcpy_tofs(retinfo,&ret,sizeof(*retinfo));
1751         return 0;
1752         
1753 }
1754 
1755 static int set_multiport_struct(struct async_struct * info,
     /* [previous][next][first][last][top][bottom][index][help] */
1756                                 struct serial_multiport_struct *in_multi)
1757 {
1758         struct serial_multiport_struct new_multi;
1759         struct rs_multiport_struct *multi;
1760         int     was_multi, now_multi;
1761         int     retval;
1762         void (*handler)(int, struct pt_regs *);
1763 
1764         if (!suser())
1765                 return -EPERM;
1766         if (!in_multi)
1767                 return -EFAULT;
1768         memcpy_fromfs(&new_multi, in_multi,
1769                       sizeof(struct serial_multiport_struct));
1770 
1771         if (new_multi.irq != info->irq || info->irq == 0 ||
1772             !IRQ_ports[info->irq])
1773                 return -EINVAL;
1774 
1775         multi = &rs_multiport[info->irq];
1776         was_multi = (multi->port1 != 0);
1777         
1778         multi->port_monitor = new_multi.port_monitor;
1779         
1780         multi->port1 = new_multi.port1;
1781         multi->mask1 = new_multi.mask1;
1782         multi->match1 = new_multi.match1;
1783 
1784         multi->port2 = new_multi.port2;
1785         multi->mask2 = new_multi.mask2;
1786         multi->match2 = new_multi.match2;
1787 
1788         multi->port3 = new_multi.port3;
1789         multi->mask3 = new_multi.mask3;
1790         multi->match3 = new_multi.match3;
1791 
1792         multi->port4 = new_multi.port4;
1793         multi->mask4 = new_multi.mask4;
1794         multi->match4 = new_multi.match4;
1795 
1796         now_multi = (multi->port1 != 0);
1797         
1798         if (IRQ_ports[info->irq]->next_port &&
1799             (was_multi != now_multi)) {
1800                 free_irq(info->irq);
1801                 if (now_multi)
1802                         handler = rs_interrupt_multi;
1803                 else
1804                         handler = rs_interrupt;
1805 
1806                 retval = request_irq(info->irq, handler, SA_INTERRUPT,
1807                                      "serial");
1808                 if (retval) {
1809                         printk("Couldn't reallocate serial interrupt "
1810                                "driver!!\n");
1811                 }
1812         }
1813 
1814         return 0;
1815 }
1816 
1817 static int rs_ioctl(struct tty_struct *tty, struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
1818                     unsigned int cmd, unsigned long arg)
1819 {
1820         int error;
1821         struct async_struct * info = (struct async_struct *)tty->driver_data;
1822         int retval;
1823 
1824         if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1825                 return -ENODEV;
1826 
1827         if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1828             (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
1829             (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1830                 if (tty->flags & (1 << TTY_IO_ERROR))
1831                     return -EIO;
1832         }
1833         
1834         switch (cmd) {
1835                 case TCSBRK:    /* SVID version: non-zero arg --> no break */
1836                         retval = tty_check_change(tty);
1837                         if (retval)
1838                                 return retval;
1839                         tty_wait_until_sent(tty, 0);
1840                         if (!arg)
1841                                 send_break(info, HZ/4); /* 1/4 second */
1842                         return 0;
1843                 case TCSBRKP:   /* support for POSIX tcsendbreak() */
1844                         retval = tty_check_change(tty);
1845                         if (retval)
1846                                 return retval;
1847                         tty_wait_until_sent(tty, 0);
1848                         send_break(info, arg ? arg*(HZ/10) : HZ/4);
1849                         return 0;
1850                 case TIOCGSOFTCAR:
1851                         error = verify_area(VERIFY_WRITE, (void *) arg,sizeof(long));
1852                         if (error)
1853                                 return error;
1854                         put_fs_long(C_CLOCAL(tty) ? 1 : 0,
1855                                     (unsigned long *) arg);
1856                         return 0;
1857                 case TIOCSSOFTCAR:
1858                         arg = get_fs_long((unsigned long *) arg);
1859                         tty->termios->c_cflag =
1860                                 ((tty->termios->c_cflag & ~CLOCAL) |
1861                                  (arg ? CLOCAL : 0));
1862                         return 0;
1863                 case TIOCMGET:
1864                         error = verify_area(VERIFY_WRITE, (void *) arg,
1865                                 sizeof(unsigned int));
1866                         if (error)
1867                                 return error;
1868                         return get_modem_info(info, (unsigned int *) arg);
1869                 case TIOCMBIS:
1870                 case TIOCMBIC:
1871                 case TIOCMSET:
1872                         return set_modem_info(info, cmd, (unsigned int *) arg);
1873                 case TIOCGSERIAL:
1874                         error = verify_area(VERIFY_WRITE, (void *) arg,
1875                                                 sizeof(struct serial_struct));
1876                         if (error)
1877                                 return error;
1878                         return get_serial_info(info,
1879                                                (struct serial_struct *) arg);
1880                 case TIOCSSERIAL:
1881                         return set_serial_info(info,
1882                                                (struct serial_struct *) arg);
1883                 case TIOCSERCONFIG:
1884                         return do_autoconfig(info);
1885 
1886                 case TIOCSERGWILD:
1887                         error = verify_area(VERIFY_WRITE, (void *) arg,
1888                                             sizeof(int));
1889                         if (error)
1890                                 return error;
1891                         put_fs_long(rs_wild_int_mask, (unsigned long *) arg);
1892                         return 0;
1893 
1894                 case TIOCSERGETLSR: /* Get line status register */
1895                         error = verify_area(VERIFY_WRITE, (void *) arg,
1896                                 sizeof(unsigned int));
1897                         if (error)
1898                                 return error;
1899                         else
1900                             return get_lsr_info(info, (unsigned int *) arg);
1901 
1902                 case TIOCSERSWILD:
1903                         if (!suser())
1904                                 return -EPERM;
1905                         rs_wild_int_mask = get_fs_long((unsigned long *) arg);
1906                         if (rs_wild_int_mask < 0)
1907                                 rs_wild_int_mask = check_wild_interrupts(0);
1908                         return 0;
1909 
1910                 case TIOCSERGSTRUCT:
1911                         error = verify_area(VERIFY_WRITE, (void *) arg,
1912                                                 sizeof(struct async_struct));
1913                         if (error)
1914                                 return error;
1915                         memcpy_tofs((struct async_struct *) arg,
1916                                     info, sizeof(struct async_struct));
1917                         return 0;
1918                         
1919                 case TIOCSERGETMULTI:
1920                         error = verify_area(VERIFY_WRITE, (void *) arg,
1921                                     sizeof(struct serial_multiport_struct));
1922                         if (error)
1923                                 return error;
1924                         return get_multiport_struct(info,
1925                                        (struct serial_multiport_struct *) arg);
1926                 case TIOCSERSETMULTI:
1927                         return set_multiport_struct(info,
1928                                        (struct serial_multiport_struct *) arg);
1929                 default:
1930                         return -ENOIOCTLCMD;
1931                 }
1932         return 0;
1933 }
1934 
1935 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
     /* [previous][next][first][last][top][bottom][index][help] */
1936 {
1937         struct async_struct *info = (struct async_struct *)tty->driver_data;
1938 
1939         if (tty->termios->c_cflag == old_termios->c_cflag)
1940                 return;
1941 
1942         change_speed(info);
1943 
1944         if ((old_termios->c_cflag & CRTSCTS) &&
1945             !(tty->termios->c_cflag & CRTSCTS)) {
1946                 tty->hw_stopped = 0;
1947                 rs_start(tty);
1948         }
1949 
1950 #if 0
1951         /*
1952          * No need to wake up processes in open wait, since they
1953          * sample the CLOCAL flag once, and don't recheck it.
1954          * XXX  It's not clear whether the current behavior is correct
1955          * or not.  Hence, this may change.....
1956          */
1957         if (!(old_termios->c_cflag & CLOCAL) &&
1958             (tty->termios->c_cflag & CLOCAL))
1959                 wake_up_interruptible(&info->open_wait);
1960 #endif
1961 }
1962 
1963 /*
1964  * ------------------------------------------------------------
1965  * rs_close()
1966  * 
1967  * This routine is called when the serial port gets closed.  First, we
1968  * wait for the last remaining data to be sent.  Then, we unlink its
1969  * async structure from the interrupt chain if necessary, and we free
1970  * that IRQ if nothing is left in the chain.
1971  * ------------------------------------------------------------
1972  */
1973 static void rs_close(struct tty_struct *tty, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
1974 {
1975         struct async_struct * info = (struct async_struct *)tty->driver_data;
1976         unsigned long flags;
1977         unsigned long timeout;
1978 
1979         if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1980                 return;
1981         
1982         save_flags(flags); cli();
1983         
1984         if (tty_hung_up_p(filp)) {
1985                 restore_flags(flags);
1986                 return;
1987         }
1988         
1989 #ifdef SERIAL_DEBUG_OPEN
1990         printk("rs_close ttys%d, count = %d\n", info->line, info->count);
1991 #endif
1992         if ((tty->count == 1) && (info->count != 1)) {
1993                 /*
1994                  * Uh, oh.  tty->count is 1, which means that the tty
1995                  * structure will be freed.  Info->count should always
1996                  * be one in these conditions.  If it's greater than
1997                  * one, we've got real problems, since it means the
1998                  * serial port won't be shutdown.
1999                  */
2000                 printk("rs_close: bad serial port count; tty->count is 1, "
2001                        "info->count is %d\n", info->count);
2002                 info->count = 1;
2003         }
2004         if (--info->count < 0) {
2005                 printk("rs_close: bad serial port count for ttys%d: %d\n",
2006                        info->line, info->count);
2007                 info->count = 0;
2008         }
2009         if (info->count) {
2010                 restore_flags(flags);
2011                 return;
2012         }
2013         info->flags |= ASYNC_CLOSING;
2014         /*
2015          * Save the termios structure, since this port may have
2016          * separate termios for callout and dialin.
2017          */
2018         if (info->flags & ASYNC_NORMAL_ACTIVE)
2019                 info->normal_termios = *tty->termios;
2020         if (info->flags & ASYNC_CALLOUT_ACTIVE)
2021                 info->callout_termios = *tty->termios;
2022         /*
2023          * Now we wait for the transmit buffer to clear; and we notify 
2024          * the line discipline to only process XON/XOFF characters.
2025          */
2026         tty->closing = 1;
2027         if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
2028                 tty_wait_until_sent(tty, info->closing_wait);
2029         /*
2030          * At this point we stop accepting input.  To do this, we
2031          * disable the receive line status interrupts, and tell the
2032          * interrupt driver to stop checking the data ready bit in the
2033          * line status register.
2034          */
2035         info->IER &= ~UART_IER_RLSI;
2036         info->read_status_mask &= ~UART_LSR_DR;
2037         if (info->flags & ASYNC_INITIALIZED) {
2038                 serial_out(info, UART_IER, info->IER);
2039                 /*
2040                  * Before we drop DTR, make sure the UART transmitter
2041                  * has completely drained; this is especially
2042                  * important if there is a transmit FIFO!
2043                  */
2044                 timeout = jiffies+HZ;
2045                 while (!(serial_inp(info, UART_LSR) & UART_LSR_TEMT)) {
2046                         current->state = TASK_INTERRUPTIBLE;
2047                         current->timeout = jiffies + info->timeout;
2048                         schedule();
2049                         if (jiffies > timeout)
2050                                 break;
2051                 }
2052         }
2053         shutdown(info);
2054         if (tty->driver.flush_buffer)
2055                 tty->driver.flush_buffer(tty);
2056         if (tty->ldisc.flush_buffer)
2057                 tty->ldisc.flush_buffer(tty);
2058         tty->closing = 0;
2059         info->event = 0;
2060         info->tty = 0;
2061         if (tty->ldisc.num != ldiscs[N_TTY].num) {
2062                 if (tty->ldisc.close)
2063                         (tty->ldisc.close)(tty);
2064                 tty->ldisc = ldiscs[N_TTY];
2065                 tty->termios->c_line = N_TTY;
2066                 if (tty->ldisc.open)
2067                         (tty->ldisc.open)(tty);
2068         }
2069         if (info->blocked_open) {
2070                 if (info->close_delay) {
2071                         current->state = TASK_INTERRUPTIBLE;
2072                         current->timeout = jiffies + info->close_delay;
2073                         schedule();
2074                 }
2075                 wake_up_interruptible(&info->open_wait);
2076         }
2077         info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
2078                          ASYNC_CLOSING);
2079         wake_up_interruptible(&info->close_wait);
2080         restore_flags(flags);
2081 }
2082 
2083 /*
2084  * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
2085  */
2086 void rs_hangup(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
2087 {
2088         struct async_struct * info = (struct async_struct *)tty->driver_data;
2089         
2090         if (serial_paranoia_check(info, tty->device, "rs_hangup"))
2091                 return;
2092         
2093         rs_flush_buffer(tty);
2094         shutdown(info);
2095         info->event = 0;
2096         info->count = 0;
2097         info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
2098         info->tty = 0;
2099         wake_up_interruptible(&info->open_wait);
2100 }
2101 
2102 /*
2103  * ------------------------------------------------------------
2104  * rs_open() and friends
2105  * ------------------------------------------------------------
2106  */
2107 static int block_til_ready(struct tty_struct *tty, struct file * filp,
     /* [previous][next][first][last][top][bottom][index][help] */
2108                            struct async_struct *info)
2109 {
2110         struct wait_queue wait = { current, NULL };
2111         int             retval;
2112         int             do_clocal = 0;
2113 
2114         /*
2115          * If the device is in the middle of being closed, then block
2116          * until it's done, and then try again.
2117          */
2118         if (info->flags & ASYNC_CLOSING) {
2119                 interruptible_sleep_on(&info->close_wait);
2120 #ifdef SERIAL_DO_RESTART
2121                 if (info->flags & ASYNC_HUP_NOTIFY)
2122                         return -EAGAIN;
2123                 else
2124                         return -ERESTARTSYS;
2125 #else
2126                 return -EAGAIN;
2127 #endif
2128         }
2129 
2130         /*
2131          * If this is a callout device, then just make sure the normal
2132          * device isn't being used.
2133          */
2134         if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
2135                 if (info->flags & ASYNC_NORMAL_ACTIVE)
2136                         return -EBUSY;
2137                 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2138                     (info->flags & ASYNC_SESSION_LOCKOUT) &&
2139                     (info->session != current->session))
2140                     return -EBUSY;
2141                 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2142                     (info->flags & ASYNC_PGRP_LOCKOUT) &&
2143                     (info->pgrp != current->pgrp))
2144                     return -EBUSY;
2145                 info->flags |= ASYNC_CALLOUT_ACTIVE;
2146                 return 0;
2147         }
2148         
2149         /*
2150          * If non-blocking mode is set, or the port is not enabled,
2151          * then make the check up front and then exit.
2152          */
2153         if ((filp->f_flags & O_NONBLOCK) ||
2154             (tty->flags & (1 << TTY_IO_ERROR))) {
2155                 if (info->flags & ASYNC_CALLOUT_ACTIVE)
2156                         return -EBUSY;
2157                 info->flags |= ASYNC_NORMAL_ACTIVE;
2158                 return 0;
2159         }
2160 
2161         if (info->flags & ASYNC_CALLOUT_ACTIVE) {
2162                 if (info->normal_termios.c_cflag & CLOCAL)
2163                         do_clocal = 1;
2164         } else {
2165                 if (tty->termios->c_cflag & CLOCAL)
2166                         do_clocal = 1;
2167         }
2168         
2169         /*
2170          * Block waiting for the carrier detect and the line to become
2171          * free (i.e., not in use by the callout).  While we are in
2172          * this loop, info->count is dropped by one, so that
2173          * rs_close() knows when to free things.  We restore it upon
2174          * exit, either normal or abnormal.
2175          */
2176         retval = 0;
2177         add_wait_queue(&info->open_wait, &wait);
2178 #ifdef SERIAL_DEBUG_OPEN
2179         printk("block_til_ready before block: ttys%d, count = %d\n",
2180                info->line, info->count);
2181 #endif
2182         info->count--;
2183         info->blocked_open++;
2184         while (1) {
2185                 cli();
2186                 if (!(info->flags & ASYNC_CALLOUT_ACTIVE))
2187                         serial_out(info, UART_MCR,
2188                                    serial_inp(info, UART_MCR) |
2189                                    (UART_MCR_DTR | UART_MCR_RTS));
2190                 sti();
2191                 current->state = TASK_INTERRUPTIBLE;
2192                 if (tty_hung_up_p(filp) ||
2193                     !(info->flags & ASYNC_INITIALIZED)) {
2194 #ifdef SERIAL_DO_RESTART
2195                         if (info->flags & ASYNC_HUP_NOTIFY)
2196                                 retval = -EAGAIN;
2197                         else
2198                                 retval = -ERESTARTSYS;  
2199 #else
2200                         retval = -EAGAIN;
2201 #endif
2202                         break;
2203                 }
2204                 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2205                     !(info->flags & ASYNC_CLOSING) &&
2206                     (do_clocal || (serial_in(info, UART_MSR) &
2207                                    UART_MSR_DCD)))
2208                         break;
2209                 if (current->signal & ~current->blocked) {
2210                         retval = -ERESTARTSYS;
2211                         break;
2212                 }
2213 #ifdef SERIAL_DEBUG_OPEN
2214                 printk("block_til_ready blocking: ttys%d, count = %d\n",
2215                        info->line, info->count);
2216 #endif
2217                 schedule();
2218         }
2219         current->state = TASK_RUNNING;
2220         remove_wait_queue(&info->open_wait, &wait);
2221         if (!tty_hung_up_p(filp))
2222                 info->count++;
2223         info->blocked_open--;
2224 #ifdef SERIAL_DEBUG_OPEN
2225         printk("block_til_ready after blocking: ttys%d, count = %d\n",
2226                info->line, info->count);
2227 #endif
2228         if (retval)
2229                 return retval;
2230         info->flags |= ASYNC_NORMAL_ACTIVE;
2231         return 0;
2232 }       
2233 
2234 /*
2235  * This routine is called whenever a serial port is opened.  It
2236  * enables interrupts for a serial port, linking in its async structure into
2237  * the IRQ chain.   It also performs the serial-specific
2238  * initialization for the tty structure.
2239  */
2240 int rs_open(struct tty_struct *tty, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
2241 {
2242         struct async_struct     *info;
2243         int                     retval, line;
2244 
2245         line = MINOR(tty->device) - tty->driver.minor_start;
2246         if ((line < 0) || (line >= NR_PORTS))
2247                 return -ENODEV;
2248         info = rs_table + line;
2249         if (serial_paranoia_check(info, tty->device, "rs_open"))
2250                 return -ENODEV;
2251 
2252 #ifdef SERIAL_DEBUG_OPEN
2253         printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
2254                info->count);
2255 #endif
2256         info->count++;
2257         tty->driver_data = info;
2258         info->tty = tty;
2259 
2260         if (!tmp_buf) {
2261                 tmp_buf = (unsigned char *) get_free_page(GFP_KERNEL);
2262                 if (!tmp_buf)
2263                         return -ENOMEM;
2264         }
2265         
2266         /*
2267          * Start up serial port
2268          */
2269         retval = startup(info);
2270         if (retval)
2271                 return retval;
2272 
2273         retval = block_til_ready(tty, filp, info);
2274         if (retval) {
2275 #ifdef SERIAL_DEBUG_OPEN
2276                 printk("rs_open returning after block_til_ready with %d\n",
2277                        retval);
2278 #endif
2279                 return retval;
2280         }
2281 
2282         if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
2283                 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2284                         *tty->termios = info->normal_termios;
2285                 else 
2286                         *tty->termios = info->callout_termios;
2287                 change_speed(info);
2288         }
2289 
2290         info->session = current->session;
2291         info->pgrp = current->pgrp;
2292 
2293 #ifdef SERIAL_DEBUG_OPEN
2294         printk("rs_open ttys%d successful...", info->line);
2295 #endif
2296         return 0;
2297 }
2298 
2299 /*
2300  * ---------------------------------------------------------------------
2301  * rs_init() and friends
2302  *
2303  * rs_init() is called at boot-time to initialize the serial driver.
2304  * ---------------------------------------------------------------------
2305  */
2306 
2307 /*
2308  * This routine prints out the appropriate serial driver version
2309  * number, and identifies which options were configured into this
2310  * driver.
2311  */
2312 static void show_serial_version(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2313 {
2314         printk("Serial driver version 4.11 with");
2315 #ifdef CONFIG_HUB6
2316         printk(" HUB-6");
2317 #define SERIAL_OPT
2318 #endif
2319 #ifdef SERIAL_OPT
2320         printk(" enabled\n");
2321 #else
2322         printk(" no serial options enabled\n");
2323 #endif
2324 #undef SERIAL_OPT
2325 }
2326 
2327 /*
2328  * This routine is called by do_auto_irq(); it attempts to determine
2329  * which interrupt a serial port is configured to use.  It is not
2330  * fool-proof, but it works a large part of the time.
2331  */
2332 static int get_auto_irq(struct async_struct *info)
     /* [previous][next][first][last][top][bottom][index][help] */
2333 {
2334         unsigned char save_MCR, save_IER, save_ICP=0;
2335         unsigned short ICP=0, port = info->port;
2336         unsigned long timeout;
2337         
2338         /*
2339          * Enable interrupts and see who answers
2340          */
2341         rs_irq_triggered = 0;
2342         cli();
2343         save_IER = serial_inp(info, UART_IER);
2344         save_MCR = serial_inp(info, UART_MCR);
2345         if (info->flags & ASYNC_FOURPORT)  {
2346                 serial_outp(info, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
2347                 serial_outp(info, UART_IER, 0x0f);      /* enable all intrs */
2348                 ICP = (port & 0xFE0) | 0x01F;
2349                 save_ICP = inb_p(ICP);
2350                 outb_p(0x80, ICP);
2351                 (void) inb_p(ICP);
2352         } else {
2353                 serial_outp(info, UART_MCR,
2354                             UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
2355                 serial_outp(info, UART_IER, 0x0f);      /* enable all intrs */
2356         }
2357         sti();
2358         /*
2359          * Next, clear the interrupt registers.
2360          */
2361         (void)serial_inp(info, UART_LSR);
2362         (void)serial_inp(info, UART_RX);
2363         (void)serial_inp(info, UART_IIR);
2364         (void)serial_inp(info, UART_MSR);
2365         
2366         timeout = jiffies+2;
2367         while (timeout >= jiffies) {
2368                 if (rs_irq_triggered)
2369                         break;
2370         }
2371         /*
2372          * Now check to see if we got any business, and clean up.
2373          */
2374         cli();
2375         serial_outp(info, UART_IER, save_IER);
2376         serial_outp(info, UART_MCR, save_MCR);
2377         if (info->flags & ASYNC_FOURPORT)
2378                 outb_p(save_ICP, ICP);
2379         sti();
2380         return(rs_irq_triggered);
2381 }
2382 
2383 /*
2384  * Calls get_auto_irq() multiple times, to make sure we don't get
2385  * faked out by random interrupts
2386  */
2387 static int do_auto_irq(struct async_struct * info)
     /* [previous][next][first][last][top][bottom][index][help] */
2388 {
2389         unsigned                port = info->port;
2390         int                     irq_lines = 0;
2391         int                     irq_try_1 = 0, irq_try_2 = 0;
2392         int                     retries;
2393         unsigned long flags;
2394 
2395         if (!port)
2396                 return 0;
2397 
2398         /* Turn on interrupts (they may be off) */
2399         save_flags(flags); sti();
2400 
2401         irq_lines = grab_all_interrupts(rs_wild_int_mask);
2402         
2403         for (retries = 0; retries < 5; retries++) {
2404                 if (!irq_try_1)
2405                         irq_try_1 = get_auto_irq(info);
2406                 if (!irq_try_2)
2407                         irq_try_2 = get_auto_irq(info);
2408                 if (irq_try_1 && irq_try_2) {
2409                         if (irq_try_1 == irq_try_2)
2410                                 break;
2411                         irq_try_1 = irq_try_2 = 0;
2412                 }
2413         }
2414         restore_flags(flags);
2415         free_all_interrupts(irq_lines);
2416         return (irq_try_1 == irq_try_2) ? irq_try_1 : 0;
2417 }
2418 
2419 /*
2420  * This routine is called by rs_init() to initialize a specific serial
2421  * port.  It determines what type of UART ship this serial port is
2422  * using: 8250, 16450, 16550, 16550A.  The important question is
2423  * whether or not this UART is a 16550A or not, since this will
2424  * determine whether or not we can use its FIFO features or not.
2425  */
2426 static void autoconfig(struct async_struct * info)
     /* [previous][next][first][last][top][bottom][index][help] */
2427 {
2428         unsigned char status1, status2, scratch, scratch2;
2429         unsigned port = info->port;
2430         unsigned long flags;
2431 
2432         info->type = PORT_UNKNOWN;
2433         
2434         if (!port)
2435                 return;
2436 
2437         save_flags(flags); cli();
2438         
2439         /*
2440          * Do a simple existence test first; if we fail this, there's
2441          * no point trying anything else.
2442          *
2443          * 0x80 is used as a nonsense port to prevent against false
2444          * positives due to ISA bus float.  The assumption is that
2445          * 0x80 is a non-existent port; which should be safe since
2446          * include/asm/io.h also makes this assumption.
2447          */
2448         scratch = serial_inp(info, UART_IER);
2449         serial_outp(info, UART_IER, 0);
2450         outb(0xff, 0x080);
2451         scratch2 = serial_inp(info, UART_IER);
2452         serial_outp(info, UART_IER, scratch);
2453         if (scratch2) {
2454                 restore_flags(flags);
2455                 return;         /* We failed; there's nothing here */
2456         }
2457 
2458         /* 
2459          * Check to see if a UART is really there.  Certain broken
2460          * internal modems based on the Rockwell chipset fail this
2461          * test, because they apparently don't implement the loopback
2462          * test mode.  So this test is skipped on the COM 1 through
2463          * COM 4 ports.  This *should* be safe, since no board
2464          * manufacturer would be stupid enough to design a board
2465          * that conflicts with COM 1-4 --- we hope!
2466          */
2467         if (!(info->flags & ASYNC_SKIP_TEST)) {
2468                 scratch = serial_inp(info, UART_MCR);
2469                 serial_outp(info, UART_MCR, UART_MCR_LOOP | scratch);
2470                 scratch2 = serial_inp(info, UART_MSR);
2471                 serial_outp(info, UART_MCR, UART_MCR_LOOP | 0x0A);
2472                 status1 = serial_inp(info, UART_MSR) & 0xF0;
2473                 serial_outp(info, UART_MCR, scratch);
2474                 serial_outp(info, UART_MSR, scratch2);
2475                 if (status1 != 0x90) {
2476                         restore_flags(flags);
2477                         return;
2478                 }
2479         } 
2480         
2481         /*
2482          * If the AUTO_IRQ flag is set, try to do the automatic IRQ
2483          * detection.
2484          */
2485         if (info->flags & ASYNC_AUTO_IRQ)
2486                 info->irq = do_auto_irq(info);
2487                 
2488         scratch2 = serial_in(info, UART_LCR);
2489         serial_outp(info, UART_LCR, scratch2 | UART_LCR_DLAB);
2490         serial_outp(info, UART_EFR, 0); /* EFR is the same as FCR */
2491         serial_outp(info, UART_LCR, scratch2);
2492         serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
2493         scratch = serial_in(info, UART_IIR) >> 6;
2494         info->xmit_fifo_size = 1;
2495         switch (scratch) {
2496                 case 0:
2497                         info->type = PORT_16450;
2498                         break;
2499                 case 1:
2500                         info->type = PORT_UNKNOWN;
2501                         break;
2502                 case 2:
2503                         info->type = PORT_16550;
2504                         break;
2505                 case 3:
2506                         serial_outp(info, UART_LCR, scratch2 | UART_LCR_DLAB);
2507                         if (serial_in(info, UART_EFR) == 0) {
2508                                 info->type = PORT_16650;
2509                                 info->xmit_fifo_size = 32;
2510                         } else {
2511                                 info->type = PORT_16550A;
2512                                 info->xmit_fifo_size = 16;
2513                         }
2514                         serial_outp(info, UART_LCR, scratch2);
2515                         break;
2516         }
2517         if (info->type == PORT_16450) {
2518                 scratch = serial_in(info, UART_SCR);
2519                 serial_outp(info, UART_SCR, 0xa5);
2520                 status1 = serial_in(info, UART_SCR);
2521                 serial_outp(info, UART_SCR, 0x5a);
2522                 status2 = serial_in(info, UART_SCR);
2523                 serial_outp(info, UART_SCR, scratch);
2524 
2525                 if ((status1 != 0xa5) || (status2 != 0x5a))
2526                         info->type = PORT_8250;
2527         }
2528         request_region(info->port,8,"serial(auto)");
2529 
2530         /*
2531          * Reset the UART.
2532          */
2533 #ifdef __alpha__
2534         /*
2535          * I wonder what DEC did to the OUT1 and OUT2 lines?
2536          * clearing them results in endless interrupts.
2537          */
2538         serial_outp(info, UART_MCR, 0x0c);
2539 #else
2540         serial_outp(info, UART_MCR, 0x00);
2541 #endif
2542         serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
2543                                      UART_FCR_CLEAR_XMIT));
2544         (void)serial_in(info, UART_RX);
2545         
2546         restore_flags(flags);
2547 }
2548 
2549 /*
2550  * The serial driver boot-time initialization code!
2551  */
2552 long rs_init(long kmem_start)
     /* [previous][next][first][last][top][bottom][index][help] */
2553 {
2554         int i;
2555         struct async_struct * info;
2556         
2557         bh_base[SERIAL_BH].routine = do_serial_bh;
2558         enable_bh(SERIAL_BH);
2559         timer_table[RS_TIMER].fn = rs_timer;
2560         timer_table[RS_TIMER].expires = 0;
2561 #ifdef CONFIG_AUTO_IRQ
2562         rs_wild_int_mask = check_wild_interrupts(1);
2563 #endif
2564 
2565         for (i = 0; i < 16; i++) {
2566                 IRQ_ports[i] = 0;
2567                 IRQ_timeout[i] = 0;
2568                 memset(&rs_multiport[i], 0, sizeof(struct rs_multiport_struct));
2569         }
2570         
2571         show_serial_version();
2572 
2573         /* Initialize the tty_driver structure */
2574         
2575         memset(&serial_driver, 0, sizeof(struct tty_driver));
2576         serial_driver.magic = TTY_DRIVER_MAGIC;
2577         serial_driver.name = "ttyS";
2578         serial_driver.major = TTY_MAJOR;
2579         serial_driver.minor_start = 64;
2580         serial_driver.num = NR_PORTS;
2581         serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2582         serial_driver.subtype = SERIAL_TYPE_NORMAL;
2583         serial_driver.init_termios = tty_std_termios;
2584         serial_driver.init_termios.c_cflag =
2585                 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2586         serial_driver.flags = TTY_DRIVER_REAL_RAW;
2587         serial_driver.refcount = &serial_refcount;
2588         serial_driver.table = serial_table;
2589         serial_driver.termios = serial_termios;
2590         serial_driver.termios_locked = serial_termios_locked;
2591 
2592         serial_driver.open = rs_open;
2593         serial_driver.close = rs_close;
2594         serial_driver.write = rs_write;
2595         serial_driver.put_char = rs_put_char;
2596         serial_driver.flush_chars = rs_flush_chars;
2597         serial_driver.write_room = rs_write_room;
2598         serial_driver.chars_in_buffer = rs_chars_in_buffer;
2599         serial_driver.flush_buffer = rs_flush_buffer;
2600         serial_driver.ioctl = rs_ioctl;
2601         serial_driver.throttle = rs_throttle;
2602         serial_driver.unthrottle = rs_unthrottle;
2603         serial_driver.set_termios = rs_set_termios;
2604         serial_driver.stop = rs_stop;
2605         serial_driver.start = rs_start;
2606         serial_driver.hangup = rs_hangup;
2607 
2608         /*
2609          * The callout device is just like normal device except for
2610          * major number and the subtype code.
2611          */
2612         callout_driver = serial_driver;
2613         callout_driver.name = "cua";
2614         callout_driver.major = TTYAUX_MAJOR;
2615         callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2616 
2617         if (tty_register_driver(&serial_driver))
2618                 panic("Couldn't register serial driver\n");
2619         if (tty_register_driver(&callout_driver))
2620                 panic("Couldn't register callout driver\n");
2621         
2622         for (i = 0, info = rs_table; i < NR_PORTS; i++,info++) {
2623                 info->magic = SERIAL_MAGIC;
2624                 info->line = i;
2625                 info->tty = 0;
2626                 info->type = PORT_UNKNOWN;
2627                 info->custom_divisor = 0;
2628                 info->close_delay = 50;
2629                 info->closing_wait = 3000;
2630                 info->x_char = 0;
2631                 info->event = 0;
2632                 info->count = 0;
2633                 info->blocked_open = 0;
2634                 info->tqueue.routine = do_softint;
2635                 info->tqueue.data = info;
2636                 info->tqueue_hangup.routine = do_serial_hangup;
2637                 info->tqueue_hangup.data = info;
2638                 info->callout_termios =callout_driver.init_termios;
2639                 info->normal_termios = serial_driver.init_termios;
2640                 info->open_wait = 0;
2641                 info->close_wait = 0;
2642                 info->next_port = 0;
2643                 info->prev_port = 0;
2644                 if (info->irq == 2)
2645                         info->irq = 9;
2646                 if (!(info->flags & ASYNC_BOOT_AUTOCONF))
2647                         continue;
2648                 autoconfig(info);
2649                 if (info->type == PORT_UNKNOWN)
2650                         continue;
2651                 printk("tty%02d%s at 0x%04x (irq = %d)", info->line, 
2652                        (info->flags & ASYNC_FOURPORT) ? " FourPort" : "",
2653                        info->port, info->irq);
2654                 switch (info->type) {
2655                         case PORT_8250:
2656                                 printk(" is a 8250\n");
2657                                 break;
2658                         case PORT_16450:
2659                                 printk(" is a 16450\n");
2660                                 break;
2661                         case PORT_16550:
2662                                 printk(" is a 16550\n");
2663                                 break;
2664                         case PORT_16550A:
2665                                 printk(" is a 16550A\n");
2666                                 break;
2667                         case PORT_16650:
2668                                 printk(" is a 16650\n");
2669                                 break;
2670                         default:
2671                                 printk("\n");
2672                                 break;
2673                 }
2674         }
2675         return kmem_start;
2676 }
2677 
2678 /*
2679  * register_serial and unregister_serial allows for serial ports to be
2680  * configured at run-time, to support PCMCIA modems.
2681  */
2682 int register_serial(struct serial_struct *req)
     /* [previous][next][first][last][top][bottom][index][help] */
2683 {
2684         int i;
2685         unsigned long flags;
2686         struct async_struct *info;
2687 
2688         save_flags(flags);
2689         cli();
2690         for (i = 0; i < NR_PORTS; i++) {
2691                 if (rs_table[i].port == req->port)
2692                         break;
2693         }
2694         if (i == NR_PORTS) {
2695                 for (i = 0; i < NR_PORTS; i++)
2696                         if ((rs_table[i].type == PORT_UNKNOWN) &&
2697                             (rs_table[i].count == 0))
2698                                 break;
2699         }
2700         if (i == NR_PORTS) {
2701                 restore_flags(flags);
2702                 return -1;
2703         }
2704         info = &rs_table[i];
2705         if (rs_table[i].count) {
2706                 restore_flags(flags);
2707                 printk("Couldn't configure serial #%d (port=%d,irq=%d): "
2708                        "device already open\n", i, req->port, req->irq);
2709                 return -1;
2710         }
2711         info->irq = req->irq;
2712         info->port = req->port;
2713         autoconfig(info);
2714         if (info->type == PORT_UNKNOWN) {
2715                 restore_flags(flags);
2716                 printk("register_serial(): autoconfig failed\n");
2717                 return -1;
2718         }
2719         printk("tty%02d at 0x%04x (irq = %d)", info->line, 
2720                info->port, info->irq);
2721         switch (info->type) {
2722         case PORT_8250:
2723                 printk(" is a 8250\n"); break;
2724         case PORT_16450:
2725                 printk(" is a 16450\n"); break;
2726         case PORT_16550:
2727                 printk(" is a 16550\n"); break;
2728         case PORT_16550A:
2729                 printk(" is a 16550A\n"); break;
2730         default:
2731                 printk("\n"); break;
2732         }
2733         restore_flags(flags);
2734         return info->line;
2735 }
2736 
2737 void unregister_serial(int line)
     /* [previous][next][first][last][top][bottom][index][help] */
2738 {
2739         unsigned long flags;
2740         struct async_struct *info = &rs_table[line];
2741 
2742         save_flags(flags);
2743         cli();
2744         if (info->tty)
2745                 tty_hangup(info->tty);
2746         info->type = PORT_UNKNOWN;
2747         printk("tty%02d unloaded\n", info->line);
2748         restore_flags(flags);
2749 }

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