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

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