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
  16. rs_interrupt_single
  17. do_serial_bh
  18. do_softint
  19. rs_timer
  20. grab_all_interrupts
  21. free_all_interrupts
  22. figure_IRQ_timeout
  23. startup
  24. shutdown
  25. change_speed
  26. rs_put_char
  27. rs_flush_chars
  28. rs_write
  29. rs_write_room
  30. rs_chars_in_buffer
  31. rs_flush_buffer
  32. rs_throttle
  33. rs_unthrottle
  34. get_serial_info
  35. set_serial_info
  36. get_modem_info
  37. set_modem_info
  38. do_autoconfig
  39. send_break
  40. check_wild_interrupts
  41. rs_ioctl
  42. rs_set_termios
  43. rs_close
  44. rs_hangup
  45. block_til_ready
  46. rs_open
  47. show_serial_version
  48. get_auto_irq
  49. do_auto_irq
  50. autoconfig
  51. rs_init
  52. register_serial
  53. unregister_serial

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

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