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

   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                 return -ENODEV;
 876         }
 877         
 878         /*
 879          * Allocate the IRQ if necessary
 880          */
 881         if (info->irq && (!IRQ_ports[info->irq] ||
 882                           !IRQ_ports[info->irq]->next_port)) {
 883                 if (IRQ_ports[info->irq]) {
 884                         free_irq(info->irq);
 885                         sa.sa_handler = rs_interrupt;
 886                 } else 
 887                         sa.sa_handler = rs_interrupt_single;
 888 
 889                 sa.sa_flags = (SA_INTERRUPT);
 890                 sa.sa_mask = 0;
 891                 sa.sa_restorer = NULL;
 892                 retval = irqaction(info->irq,&sa);
 893                 if (retval) {
 894                         restore_flags(flags);
 895                         return retval;
 896                 }
 897         }
 898 
 899         /*
 900          * Clear the interrupt registers.
 901          */
 902      /* (void) serial_inp(info, UART_LSR); */   /* (see above) */
 903         (void) serial_inp(info, UART_RX);
 904         (void) serial_inp(info, UART_IIR);
 905         (void) serial_inp(info, UART_MSR);
 906 
 907         /*
 908          * Now, initialize the UART 
 909          */
 910         serial_outp(info, UART_LCR, UART_LCR_WLEN8);    /* reset DLAB */
 911         if (info->flags & ASYNC_FOURPORT) {
 912                 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
 913                 info->MCR_noint = UART_MCR_DTR | UART_MCR_OUT1;
 914         } else {
 915                 info->MCR = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
 916                 info->MCR_noint = UART_MCR_DTR | UART_MCR_RTS;
 917         }
 918         serial_outp(info, UART_MCR, info->MCR);
 919         
 920         /*
 921          * Finally, enable interrupts
 922          */
 923         info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
 924         serial_outp(info, UART_IER, info->IER); /* enable interrupts */
 925         
 926         if (info->flags & ASYNC_FOURPORT) {
 927                 /* Enable interrupts on the AST Fourport board */
 928                 ICP = (info->port & 0xFE0) | 0x01F;
 929                 outb_p(0x80, ICP);
 930                 (void) inb_p(ICP);
 931         }
 932 
 933         /*
 934          * And clear the interrupt registers again for luck.
 935          */
 936         (void)serial_inp(info, UART_LSR);
 937         (void)serial_inp(info, UART_RX);
 938         (void)serial_inp(info, UART_IIR);
 939         (void)serial_inp(info, UART_MSR);
 940 
 941         if (info->tty)
 942                 clear_bit(TTY_IO_ERROR, &info->tty->flags);
 943         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
 944 
 945         /*
 946          * Insert serial port into IRQ chain.
 947          */
 948         info->prev_port = 0;
 949         info->next_port = IRQ_ports[info->irq];
 950         if (info->next_port)
 951                 info->next_port->prev_port = info;
 952         IRQ_ports[info->irq] = info;
 953         figure_IRQ_timeout(info->irq);
 954 
 955         /*
 956          * Set up serial timers...
 957          */
 958         timer_table[RS_TIMER].expires = jiffies + 2;
 959         timer_active |= 1 << RS_TIMER;
 960 
 961         /*
 962          * and set the speed of the serial port
 963          */
 964         change_speed(info);
 965 
 966         info->flags |= ASYNC_INITIALIZED;
 967         restore_flags(flags);
 968         return 0;
 969 }
 970 
 971 /*
 972  * This routine will shutdown a serial port; interrupts are disabled, and
 973  * DTR is dropped if the hangup on close termio flag is on.
 974  */
 975 static void shutdown(struct async_struct * info)
     /* [previous][next][first][last][top][bottom][index][help] */
 976 {
 977         struct sigaction        sa;
 978         unsigned long   flags;
 979         unsigned long timeout;
 980         int             retval;
 981 
 982         if (!(info->flags & ASYNC_INITIALIZED))
 983                 return;
 984 
 985 #ifdef SERIAL_DEBUG_OPEN
 986         printk("Shutting down serial port %d (irq %d)....", info->line,
 987                info->irq);
 988 #endif
 989         
 990         save_flags(flags); cli(); /* Disable interrupts */
 991         
 992         /*
 993          * First unlink the serial port from the IRQ chain...
 994          */
 995         if (info->next_port)
 996                 info->next_port->prev_port = info->prev_port;
 997         if (info->prev_port)
 998                 info->prev_port->next_port = info->next_port;
 999         else
1000                 IRQ_ports[info->irq] = info->next_port;
1001         figure_IRQ_timeout(info->irq);
1002         
1003         /*
1004          * Free the IRQ, if necessary
1005          */
1006         if (info->irq && (!IRQ_ports[info->irq] ||
1007                           !IRQ_ports[info->irq]->next_port)) {
1008                 if (IRQ_ports[info->irq]) {
1009                         free_irq(info->irq);
1010                         sa.sa_flags = (SA_INTERRUPT);
1011                         sa.sa_mask = 0;
1012                         sa.sa_restorer = NULL;
1013                         sa.sa_handler = rs_interrupt_single;
1014                         retval = irqaction(info->irq, &sa);
1015                         
1016                         if (retval)
1017                                 printk("serial shutdown: irqaction: error %d"
1018                                        "  Couldn't reacquire IRQ.\n", retval);
1019                 } else
1020                         free_irq(info->irq);
1021         }
1022 
1023         if (info->xmit_buf) {
1024                 free_page((unsigned long) info->xmit_buf);
1025                 info->xmit_buf = 0;
1026         }
1027                         
1028         info->IER = 0;
1029         serial_outp(info, UART_IER, 0x00);      /* disable all intrs */
1030         if (info->flags & ASYNC_FOURPORT) {
1031                 /* reset interrupts on the AST Fourport board */
1032                 (void) inb((info->port & 0xFE0) | 0x01F);
1033         }
1034         
1035         /*
1036          * Bebore we drop DTR, make sure the UART transmitter has
1037          * completely drained; this is especially important if there
1038          * is a transmit FIFO!
1039          * 
1040          * We busy loop here, which is not great; unfortunately the
1041          * UART does not provide an interrupt for TEMT, and putting it
1042          * in the interrupt handler would slow down normal accesses
1043          * anyway.
1044          */
1045         sti();
1046         timeout = jiffies + info->timeout;
1047         while (!(serial_inp(info, UART_LSR) & UART_LSR_TEMT)) {
1048                 if (jiffies > timeout)
1049                         break;
1050         }
1051         cli();
1052         
1053         if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
1054                 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1055                 info->MCR_noint &= ~(UART_MCR_DTR|UART_MCR_RTS);
1056         }
1057         serial_outp(info, UART_MCR, info->MCR_noint);
1058 
1059         /* disable FIFO's */    
1060         serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
1061                                      UART_FCR_CLEAR_XMIT));
1062         (void)serial_in(info, UART_RX);    /* read data port to reset things */
1063         
1064         if (info->tty)
1065                 set_bit(TTY_IO_ERROR, &info->tty->flags);
1066         
1067         info->flags &= ~ASYNC_INITIALIZED;
1068         restore_flags(flags);
1069 }
1070 
1071 /*
1072  * This routine is called to set the UART divisor registers to match
1073  * the specified baud rate for a serial port.
1074  */
1075 static void change_speed(struct async_struct *info)
     /* [previous][next][first][last][top][bottom][index][help] */
1076 {
1077         unsigned short port;
1078         int     quot = 0;
1079         unsigned cflag,cval,fcr;
1080         int     i;
1081 
1082         if (!info->tty || !info->tty->termios)
1083                 return;
1084         cflag = info->tty->termios->c_cflag;
1085         if (!(port = info->port))
1086                 return;
1087         i = cflag & CBAUD;
1088         if (i == 15) {
1089                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1090                         i += 1;
1091                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1092                         i += 2;
1093                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
1094                         quot = info->custom_divisor;
1095         }
1096         if (quot) {
1097                 info->timeout = ((info->xmit_fifo_size*HZ*15*quot) /
1098                                  info->baud_base) + 2;
1099         } else if (baud_table[i] == 134) {
1100                 quot = (2*info->baud_base / 269);
1101                 info->timeout = (info->xmit_fifo_size*HZ*30/269) + 2;
1102         } else if (baud_table[i]) {
1103                 quot = info->baud_base / baud_table[i];
1104                 info->timeout = (info->xmit_fifo_size*HZ*15/baud_table[i]) + 2;
1105         } else {
1106                 quot = 0;
1107                 info->timeout = 0;
1108         }
1109         if (quot) {
1110                 info->MCR |= UART_MCR_DTR;
1111                 info->MCR_noint |= UART_MCR_DTR;
1112                 cli();
1113                 serial_out(info, UART_MCR, info->MCR);
1114                 sti();
1115         } else {
1116                 info->MCR &= ~UART_MCR_DTR;
1117                 info->MCR_noint &= ~UART_MCR_DTR;
1118                 cli();
1119                 serial_out(info, UART_MCR, info->MCR);
1120                 sti();
1121                 return;
1122         }
1123         /* byte size and parity */
1124         cval = cflag & (CSIZE | CSTOPB);
1125         cval >>= 4;
1126         if (cflag & PARENB)
1127                 cval |= UART_LCR_PARITY;
1128         if (!(cflag & PARODD))
1129                 cval |= UART_LCR_EPAR;
1130         if (info->type == PORT_16550A) {
1131                 if ((info->baud_base / quot) < 2400)
1132                         fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
1133                 else
1134                         fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
1135         } else
1136                 fcr = 0;
1137         
1138         /* CTS flow control flag */
1139         if (cflag & CRTSCTS)
1140                 info->flags |= ASYNC_CTS_FLOW;
1141         else
1142                 info->flags &= ~ASYNC_CTS_FLOW;
1143         if (cflag & CLOCAL)
1144                 info->flags &= ~ASYNC_CHECK_CD;
1145         else
1146                 info->flags |= ASYNC_CHECK_CD;
1147 
1148         /*
1149          * Set up parity check flag
1150          */
1151         info->read_status_mask = UART_LSR_OE;
1152         if (I_INPCK(info->tty))
1153                 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1154         if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1155                 info->read_status_mask |= UART_LSR_BI;
1156         
1157         info->ignore_status_mask = 0;
1158         if (I_IGNPAR(info->tty))
1159                 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1160         if (I_IGNBRK(info->tty)) {
1161                 info->ignore_status_mask |= UART_LSR_BI;
1162                 /*
1163                  * If we're ignore parity and break indicators, ignore 
1164                  * overruns too.  (For real raw support).
1165                  */
1166                 if (I_IGNPAR(info->tty))
1167                         info->ignore_status_mask |= UART_LSR_OE;
1168         }
1169         
1170         cli();
1171         serial_outp(info, UART_LCR, cval | UART_LCR_DLAB);      /* set DLAB */
1172         serial_outp(info, UART_DLL, quot & 0xff);       /* LS of divisor */
1173         serial_outp(info, UART_DLM, quot >> 8);         /* MS of divisor */
1174         serial_outp(info, UART_LCR, cval);              /* reset DLAB */
1175         serial_outp(info, UART_FCR, fcr);       /* set fcr */
1176         sti();
1177 }
1178 
1179 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
     /* [previous][next][first][last][top][bottom][index][help] */
1180 {
1181         struct async_struct *info = tty->driver_data;
1182 
1183         if (serial_paranoia_check(info, tty->device, "rs_put_char"))
1184                 return;
1185 
1186         if (!tty || tty->stopped || tty->hw_stopped || !info->xmit_buf)
1187                 return;
1188 
1189         if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1190                 return;
1191 
1192         info->xmit_buf[info->xmit_head++] = ch;
1193         info->xmit_head &= SERIAL_XMIT_SIZE-1;
1194         info->xmit_cnt++;
1195 }
1196 
1197 static void rs_flush_chars(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1198 {
1199         struct async_struct *info = tty->driver_data;
1200         unsigned long flags;
1201                                 
1202         if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
1203                 return;
1204 
1205         if (info->xmit_cnt == 0 || tty->stopped || tty->hw_stopped ||
1206             !info->xmit_buf)
1207                 return;
1208 
1209         save_flags(flags); cli();
1210         info->IER |= UART_IER_THRI;
1211         serial_out(info, UART_IER, info->IER);
1212         restore_flags(flags);
1213 }
1214 
1215 static int rs_write(struct tty_struct * tty, int from_user,
     /* [previous][next][first][last][top][bottom][index][help] */
1216                     unsigned char *buf, int count)
1217 {
1218         int     c, total = 0;
1219         struct async_struct *info = tty->driver_data;
1220         unsigned long flags;
1221                                 
1222         if (serial_paranoia_check(info, tty->device, "rs_write"))
1223                 return 0;
1224 
1225         if (!tty || !info->xmit_buf)
1226                 return 0;
1227             
1228         while (1) {
1229                 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1230                                    SERIAL_XMIT_SIZE - info->xmit_head));
1231                 if (!c)
1232                         break;
1233 
1234                 if (from_user)
1235                         memcpy_fromfs(info->xmit_buf + info->xmit_head,
1236                                       buf, c);
1237                 else
1238                         memcpy(info->xmit_buf + info->xmit_head, buf, c);
1239                 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1240                 cli();
1241                 info->xmit_cnt += c;
1242                 sti();
1243                 buf += c;
1244                 count -= c;
1245                 total += c;
1246         }
1247         save_flags(flags); cli();
1248         if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1249             !(info->IER & UART_IER_THRI)) {
1250                 info->IER |= UART_IER_THRI;
1251                 serial_out(info, UART_IER, info->IER);
1252         }
1253         restore_flags(flags);
1254         return total;
1255 }
1256 
1257 static int rs_write_room(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1258 {
1259         struct async_struct *info = tty->driver_data;
1260                                 
1261         if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1262                 return 0;
1263         return SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1264 }
1265 
1266 static int rs_chars_in_buffer(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1267 {
1268         struct async_struct *info = tty->driver_data;
1269                                 
1270         if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1271                 return 0;
1272         return info->xmit_cnt;
1273 }
1274 
1275 static void rs_flush_buffer(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1276 {
1277         struct async_struct *info = tty->driver_data;
1278                                 
1279         if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1280                 return;
1281         cli();
1282         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1283         sti();
1284         wake_up_interruptible(&tty->write_wait);
1285         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1286             tty->ldisc.write_wakeup)
1287                 (tty->ldisc.write_wakeup)(tty);
1288 }
1289 
1290 /*
1291  * ------------------------------------------------------------
1292  * rs_throttle()
1293  * 
1294  * This routine is called by the upper-layer tty layer to signal that
1295  * incoming characters should be throttled.
1296  * ------------------------------------------------------------
1297  */
1298 static void rs_throttle(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1299 {
1300         struct async_struct *info = tty->driver_data;
1301 #ifdef SERIAL_DEBUG_THROTTLE
1302         char    buf[64];
1303         
1304         printk("throttle %s: %d....\n", _tty_name(tty, buf),
1305                tty->ldisc.chars_in_buffer(tty));
1306 #endif
1307 
1308         if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1309                 return;
1310         
1311         if (I_IXOFF(tty))
1312                 info->x_char = STOP_CHAR(tty);
1313 
1314         info->MCR &= ~UART_MCR_RTS;
1315         info->MCR_noint &= ~UART_MCR_RTS;
1316         cli();
1317         serial_out(info, UART_MCR, info->MCR);
1318         sti();
1319 }
1320 
1321 static void rs_unthrottle(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1322 {
1323         struct async_struct *info = tty->driver_data;
1324 #ifdef SERIAL_DEBUG_THROTTLE
1325         char    buf[64];
1326         
1327         printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1328                tty->ldisc.chars_in_buffer(tty));
1329 #endif
1330 
1331         if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1332                 return;
1333         
1334         if (I_IXOFF(tty)) {
1335                 if (info->x_char)
1336                         info->x_char = 0;
1337                 else
1338                         info->x_char = START_CHAR(tty);
1339         }
1340         info->MCR |= UART_MCR_RTS;
1341         info->MCR_noint |= UART_MCR_RTS;
1342         cli();
1343         serial_out(info, UART_MCR, info->MCR);
1344         sti();
1345 }
1346 
1347 /*
1348  * ------------------------------------------------------------
1349  * rs_ioctl() and friends
1350  * ------------------------------------------------------------
1351  */
1352 
1353 static int get_serial_info(struct async_struct * info,
     /* [previous][next][first][last][top][bottom][index][help] */
1354                            struct serial_struct * retinfo)
1355 {
1356         struct serial_struct tmp;
1357   
1358         if (!retinfo)
1359                 return -EFAULT;
1360         memset(&tmp, 0, sizeof(tmp));
1361         tmp.type = info->type;
1362         tmp.line = info->line;
1363         tmp.port = info->port;
1364         tmp.irq = info->irq;
1365         tmp.flags = info->flags;
1366         tmp.baud_base = info->baud_base;
1367         tmp.close_delay = info->close_delay;
1368         tmp.custom_divisor = info->custom_divisor;
1369         tmp.hub6 = info->hub6;
1370         memcpy_tofs(retinfo,&tmp,sizeof(*retinfo));
1371         return 0;
1372 }
1373 
1374 static int set_serial_info(struct async_struct * info,
     /* [previous][next][first][last][top][bottom][index][help] */
1375                            struct serial_struct * new_info)
1376 {
1377         struct serial_struct new_serial;
1378         struct async_struct old_info;
1379         unsigned int            i,change_irq,change_port;
1380         int                     retval = 0;
1381 
1382         if (!new_info)
1383                 return -EFAULT;
1384         memcpy_fromfs(&new_serial,new_info,sizeof(new_serial));
1385         old_info = *info;
1386 
1387         change_irq = new_serial.irq != info->irq;
1388         change_port = (new_serial.port != info->port) || (new_serial.hub6 != info->hub6);
1389 
1390         if (!suser()) {
1391                 if (change_irq || change_port ||
1392                     (new_serial.baud_base != info->baud_base) ||
1393                     (new_serial.type != info->type) ||
1394                     (new_serial.close_delay != info->close_delay) ||
1395                     ((new_serial.flags & ~ASYNC_USR_MASK) !=
1396                      (info->flags & ~ASYNC_USR_MASK)))
1397                         return -EPERM;
1398                 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1399                                (new_serial.flags & ASYNC_USR_MASK));
1400                 info->custom_divisor = new_serial.custom_divisor;
1401                 goto check_and_exit;
1402         }
1403 
1404         if (new_serial.irq == 2)
1405                 new_serial.irq = 9;
1406 
1407         if ((new_serial.irq > 15) || (new_serial.port > 0xffff) ||
1408             (new_serial.type < PORT_UNKNOWN) || (new_serial.type > PORT_MAX)) {
1409                 return -EINVAL;
1410         }
1411 
1412         /* Make sure address is not already in use */
1413         for (i = 0 ; i < NR_PORTS; i++)
1414                 if ((info != &rs_table[i]) &&
1415                     (rs_table[i].port == new_serial.port) && rs_table[i].type)
1416                         return -EADDRINUSE;
1417 
1418         if ((change_port || change_irq) && (info->count > 1))
1419                 return -EBUSY;
1420 
1421         /*
1422          * OK, past this point, all the error checking has been done.
1423          * At this point, we start making changes.....
1424          */
1425 
1426         info->baud_base = new_serial.baud_base;
1427         info->flags = ((info->flags & ~ASYNC_FLAGS) |
1428                         (new_serial.flags & ASYNC_FLAGS));
1429         info->custom_divisor = new_serial.custom_divisor;
1430         info->type = new_serial.type;
1431         info->close_delay = new_serial.close_delay;
1432 
1433         if (change_port || change_irq) {
1434                 /*
1435                  * We need to shutdown the serial port at the old
1436                  * port/irq combination.
1437                  */
1438                 shutdown(info);
1439                 info->irq = new_serial.irq;
1440                 info->port = new_serial.port;
1441                 info->hub6 = new_serial.hub6;
1442         }
1443         
1444 check_and_exit:
1445         if (!info->port || !info->type)
1446                 return 0;
1447         if (info->flags & ASYNC_INITIALIZED) {
1448                 if (((old_info.flags & ASYNC_SPD_MASK) !=
1449                      (info->flags & ASYNC_SPD_MASK)) ||
1450                     (old_info.custom_divisor != info->custom_divisor))
1451                         change_speed(info);
1452         } else
1453                 retval = startup(info);
1454         return retval;
1455 }
1456 
1457 static int get_modem_info(struct async_struct * info, unsigned int *value)
     /* [previous][next][first][last][top][bottom][index][help] */
1458 {
1459         unsigned char control, status;
1460         unsigned int result;
1461 
1462         control = info->MCR;
1463         cli();
1464         status = serial_in(info, UART_MSR);
1465         sti();
1466         result =  ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1467                 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1468                 | ((status  & UART_MSR_DCD) ? TIOCM_CAR : 0)
1469                 | ((status  & UART_MSR_RI) ? TIOCM_RNG : 0)
1470                 | ((status  & UART_MSR_DSR) ? TIOCM_DSR : 0)
1471                 | ((status  & UART_MSR_CTS) ? TIOCM_CTS : 0);
1472         put_fs_long(result,(unsigned long *) value);
1473         return 0;
1474 }
1475 
1476 static int set_modem_info(struct async_struct * info, unsigned int cmd,
     /* [previous][next][first][last][top][bottom][index][help] */
1477                           unsigned int *value)
1478 {
1479         unsigned int arg = get_fs_long((unsigned long *) value);
1480 
1481         switch (cmd) {
1482         case TIOCMBIS: 
1483                 if (arg & TIOCM_RTS) {
1484                         info->MCR |= UART_MCR_RTS;
1485                         info->MCR_noint |= UART_MCR_RTS;
1486                 }
1487                 if (arg & TIOCM_DTR) {
1488                         info->MCR |= UART_MCR_DTR;
1489                         info->MCR_noint |= UART_MCR_DTR;
1490                 }
1491                 break;
1492         case TIOCMBIC:
1493                 if (arg & TIOCM_RTS) {
1494                         info->MCR &= ~UART_MCR_RTS;
1495                         info->MCR_noint &= ~UART_MCR_RTS;
1496                 }
1497                 if (arg & TIOCM_DTR) {
1498                         info->MCR &= ~UART_MCR_DTR;
1499                         info->MCR_noint &= ~UART_MCR_DTR;
1500                 }
1501                 break;
1502         case TIOCMSET:
1503                 info->MCR = ((info->MCR & ~(UART_MCR_RTS | UART_MCR_DTR))
1504                              | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1505                              | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1506                 info->MCR_noint = ((info->MCR_noint
1507                                     & ~(UART_MCR_RTS | UART_MCR_DTR))
1508                                    | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1509                                    | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1510                 break;
1511         default:
1512                 return -EINVAL;
1513         }
1514         cli();
1515         serial_out(info, UART_MCR, info->MCR);
1516         sti();
1517         return 0;
1518 }
1519 
1520 static int do_autoconfig(struct async_struct * info)
     /* [previous][next][first][last][top][bottom][index][help] */
1521 {
1522         int                     retval;
1523         
1524         if (!suser())
1525                 return -EPERM;
1526         
1527         if (info->count > 1)
1528                 return -EBUSY;
1529         
1530         shutdown(info);
1531 
1532         cli();
1533         autoconfig(info);
1534         sti();
1535 
1536         retval = startup(info);
1537         if (retval)
1538                 return retval;
1539         return 0;
1540 }
1541 
1542 
1543 /*
1544  * This routine sends a break character out the serial port.
1545  */
1546 static void send_break( struct async_struct * info, int duration)
     /* [previous][next][first][last][top][bottom][index][help] */
1547 {
1548         if (!info->port)
1549                 return;
1550         current->state = TASK_INTERRUPTIBLE;
1551         current->timeout = jiffies + duration;
1552         cli();
1553         serial_out(info, UART_LCR, serial_inp(info, UART_LCR) | UART_LCR_SBC);
1554         schedule();
1555         serial_out(info, UART_LCR, serial_inp(info, UART_LCR) & ~UART_LCR_SBC);
1556         sti();
1557 }
1558 
1559 /*
1560  * This routine returns a bitfield of "wild interrupts".  Basically,
1561  * any unclaimed interrupts which is flapping around.
1562  */
1563 static int check_wild_interrupts(int doprint)
     /* [previous][next][first][last][top][bottom][index][help] */
1564 {
1565         int     i, mask;
1566         int     wild_interrupts = 0;
1567         int     irq_lines;
1568         unsigned long timeout;
1569         unsigned long flags;
1570         
1571         /* Turn on interrupts (they may be off) */
1572         save_flags(flags); sti();
1573 
1574         irq_lines = grab_all_interrupts(0);
1575         
1576         /*
1577          * Delay for 0.1 seconds -- we use a busy loop since this may 
1578          * occur during the bootup sequence
1579          */
1580         timeout = jiffies+10;
1581         while (timeout >= jiffies)
1582                 ;
1583         
1584         rs_triggered = 0;       /* Reset after letting things settle */
1585 
1586         timeout = jiffies+10;
1587         while (timeout >= jiffies)
1588                 ;
1589         
1590         for (i = 0, mask = 1; i < 16; i++, mask <<= 1) {
1591                 if ((rs_triggered & (1 << i)) &&
1592                     (irq_lines & (1 << i))) {
1593                         wild_interrupts |= mask;
1594                         if (doprint)
1595                                 printk("Wild interrupt?  (IRQ %d)\n", i);
1596                 }
1597         }
1598         free_all_interrupts(irq_lines);
1599         restore_flags(flags);
1600         return wild_interrupts;
1601 }
1602 
1603 static int rs_ioctl(struct tty_struct *tty, struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
1604                     unsigned int cmd, unsigned long arg)
1605 {
1606         int error;
1607         struct async_struct * info = tty->driver_data;
1608         int retval;
1609 
1610         if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1611                 return -ENODEV;
1612         
1613         switch (cmd) {
1614                 case TCSBRK:    /* SVID version: non-zero arg --> no break */
1615                         retval = tty_check_change(tty);
1616                         if (retval)
1617                                 return retval;
1618                         wait_until_sent(tty, 0);
1619                         if (!arg)
1620                                 send_break(info, HZ/4); /* 1/4 second */
1621                         return 0;
1622                 case TCSBRKP:   /* support for POSIX tcsendbreak() */
1623                         retval = tty_check_change(tty);
1624                         if (retval)
1625                                 return retval;
1626                         wait_until_sent(tty, 0);
1627                         send_break(info, arg ? arg*(HZ/10) : HZ/4);
1628                         return 0;
1629                 case TIOCGSOFTCAR:
1630                         error = verify_area(VERIFY_WRITE, (void *) arg,sizeof(long));
1631                         if (error)
1632                                 return error;
1633                         put_fs_long(C_CLOCAL(tty) ? 1 : 0,
1634                                     (unsigned long *) arg);
1635                         return 0;
1636                 case TIOCSSOFTCAR:
1637                         arg = get_fs_long((unsigned long *) arg);
1638                         tty->termios->c_cflag =
1639                                 ((tty->termios->c_cflag & ~CLOCAL) |
1640                                  (arg ? CLOCAL : 0));
1641                         return 0;
1642                 case TIOCMGET:
1643                         error = verify_area(VERIFY_WRITE, (void *) arg,
1644                                 sizeof(unsigned int));
1645                         if (error)
1646                                 return error;
1647                         return get_modem_info(info, (unsigned int *) arg);
1648                 case TIOCMBIS:
1649                 case TIOCMBIC:
1650                 case TIOCMSET:
1651                         return set_modem_info(info, cmd, (unsigned int *) arg);
1652                 case TIOCGSERIAL:
1653                         error = verify_area(VERIFY_WRITE, (void *) arg,
1654                                                 sizeof(struct serial_struct));
1655                         if (error)
1656                                 return error;
1657                         return get_serial_info(info,
1658                                                (struct serial_struct *) arg);
1659                 case TIOCSSERIAL:
1660                         return set_serial_info(info,
1661                                                (struct serial_struct *) arg);
1662                 case TIOCSERCONFIG:
1663                         return do_autoconfig(info);
1664 
1665                 case TIOCSERGWILD:
1666                         error = verify_area(VERIFY_WRITE, (void *) arg,
1667                                             sizeof(int));
1668                         if (error)
1669                                 return error;
1670                         put_fs_long(rs_wild_int_mask, (unsigned long *) arg);
1671                         return 0;
1672 
1673                 case TIOCSERSWILD:
1674                         if (!suser())
1675                                 return -EPERM;
1676                         rs_wild_int_mask = get_fs_long((unsigned long *) arg);
1677                         if (rs_wild_int_mask < 0)
1678                                 rs_wild_int_mask = check_wild_interrupts(0);
1679                         return 0;
1680 
1681                 case TIOCSERGSTRUCT:
1682                         error = verify_area(VERIFY_WRITE, (void *) arg,
1683                                                 sizeof(struct async_struct));
1684                         if (error)
1685                                 return error;
1686                         memcpy_tofs((struct async_struct *) arg,
1687                                     info, sizeof(struct async_struct));
1688                         return 0;
1689                         
1690                 default:
1691                         return -ENOIOCTLCMD;
1692                 }
1693         return 0;
1694 }
1695 
1696 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
     /* [previous][next][first][last][top][bottom][index][help] */
1697 {
1698         struct async_struct *info = tty->driver_data;
1699 
1700         if (tty->termios->c_cflag == old_termios->c_cflag)
1701                 return;
1702 
1703         change_speed(info);
1704 
1705         if ((old_termios->c_cflag & CRTSCTS) &&
1706             !(tty->termios->c_cflag & CRTSCTS)) {
1707                 tty->hw_stopped = 0;
1708                 rs_start(tty);
1709         }
1710 
1711         if (!(old_termios->c_cflag & CLOCAL) &&
1712             (tty->termios->c_cflag & CLOCAL))
1713                 wake_up_interruptible(&info->open_wait);
1714 }
1715 
1716 /*
1717  * ------------------------------------------------------------
1718  * rs_close()
1719  * 
1720  * This routine is called when the serial port gets closed.  First, we
1721  * wait for the last remaining data to be sent.  Then, we unlink its
1722  * async structure from the interrupt chain if necessary, and we free
1723  * that IRQ if nothing is left in the chain.
1724  * ------------------------------------------------------------
1725  */
1726 static void rs_close(struct tty_struct *tty, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
1727 {
1728         struct async_struct * info = tty->driver_data;
1729 
1730         if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1731                 return;
1732         
1733         if (tty_hung_up_p(filp))
1734                 return;
1735         
1736 #ifdef SERIAL_DEBUG_OPEN
1737         printk("rs_close ttys%d, count = %d\n", info->line, info->count);
1738 #endif
1739         if ((tty->count == 1) && (info->count != 1)) {
1740                 /*
1741                  * Uh, oh.  tty->count is 1, which means that the tty
1742                  * structure will be freed.  Info->count should always
1743                  * be one in these conditions.  If it's greater than
1744                  * one, we've got real problems, since it means the
1745                  * serial port won't be shutdown.
1746                  */
1747                 printk("rs_close: bad serial port count; tty->count is 1, "
1748                        "info->count is %d\n", info->count);
1749                 info->count = 1;
1750         }
1751         if (--info->count < 0) {
1752                 printk("rs_close: bad serial port count for ttys%d: %d\n",
1753                        info->line, info->count);
1754                 info->count = 0;
1755         }
1756         if (info->count)
1757                 return;
1758         info->flags |= ASYNC_CLOSING;
1759         info->flags &= ~ASYNC_CTS_FLOW;
1760         /*
1761          * Save the termios structure, since this port may have
1762          * separate termios for callout and dialin.
1763          */
1764         if (info->flags & ASYNC_NORMAL_ACTIVE)
1765                 info->normal_termios = *tty->termios;
1766         if (info->flags & ASYNC_CALLOUT_ACTIVE)
1767                 info->callout_termios = *tty->termios;
1768         tty->stopped = 0;               /* Force flush to succeed */
1769         tty->hw_stopped = 0;
1770         if (info->flags & ASYNC_INITIALIZED) {
1771                 rs_start(tty);
1772                 wait_until_sent(tty, 6000); /* 60 seconds timeout */
1773         }
1774         shutdown(info);
1775         if (tty->driver.flush_buffer)
1776                 tty->driver.flush_buffer(tty);
1777         if (tty->ldisc.flush_buffer)
1778                 tty->ldisc.flush_buffer(tty);
1779         info->event = 0;
1780         info->tty = 0;
1781         if (tty->ldisc.num != ldiscs[N_TTY].num) {
1782                 if (tty->ldisc.close)
1783                         (tty->ldisc.close)(tty);
1784                 tty->ldisc = ldiscs[N_TTY];
1785                 tty->termios->c_line = N_TTY;
1786                 if (tty->ldisc.open)
1787                         (tty->ldisc.open)(tty);
1788         }
1789         if (info->blocked_open) {
1790                 if (info->close_delay) {
1791                         tty->count++; /* avoid race condition */
1792                         current->state = TASK_INTERRUPTIBLE;
1793                         current->timeout = jiffies + info->close_delay;
1794                         schedule();
1795                         tty->count--;
1796                 }
1797                 wake_up_interruptible(&info->open_wait);
1798         }
1799         info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1800                          ASYNC_CLOSING);
1801         wake_up_interruptible(&info->close_wait);
1802 }
1803 
1804 /*
1805  * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1806  */
1807 void rs_hangup(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1808 {
1809         struct async_struct * info = tty->driver_data;
1810         
1811         if (serial_paranoia_check(info, tty->device, "rs_hangup"))
1812                 return;
1813         
1814         shutdown(info);
1815         info->event = 0;
1816         info->count = 0;
1817         info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1818         info->tty = 0;
1819         wake_up_interruptible(&info->open_wait);
1820 }
1821 
1822 /*
1823  * ------------------------------------------------------------
1824  * rs_open() and friends
1825  * ------------------------------------------------------------
1826  */
1827 static int block_til_ready(struct tty_struct *tty, struct file * filp,
     /* [previous][next][first][last][top][bottom][index][help] */
1828                            struct async_struct *info)
1829 {
1830         struct wait_queue wait = { current, NULL };
1831         int             retval;
1832         int             do_clocal = C_CLOCAL(tty);
1833 
1834         /*
1835          * If the device is in the middle of being closed, then block
1836          * until it's done, and then try again.
1837          */
1838         if (info->flags & ASYNC_CLOSING) {
1839                 interruptible_sleep_on(&info->close_wait);
1840 #ifdef SERIAL_DO_RESTART
1841                 if (info->flags & ASYNC_HUP_NOTIFY)
1842                         return -EAGAIN;
1843                 else
1844                         return -ERESTARTSYS;
1845 #else
1846                 return -EAGAIN;
1847 #endif
1848         }
1849 
1850         /*
1851          * If this is a callout device, then just make sure the normal
1852          * device isn't being used.
1853          */
1854         if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1855                 if (info->flags & ASYNC_NORMAL_ACTIVE)
1856                         return -EBUSY;
1857                 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1858                     (info->flags & ASYNC_SESSION_LOCKOUT) &&
1859                     (info->session != current->session))
1860                     return -EBUSY;
1861                 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1862                     (info->flags & ASYNC_PGRP_LOCKOUT) &&
1863                     (info->pgrp != current->pgrp))
1864                     return -EBUSY;
1865                 info->flags |= ASYNC_CALLOUT_ACTIVE;
1866                 return 0;
1867         }
1868         
1869         /*
1870          * If non-blocking mode is set, then make the check up front
1871          * and then exit.
1872          */
1873         if (filp->f_flags & O_NONBLOCK) {
1874                 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1875                         return -EBUSY;
1876                 info->flags |= ASYNC_NORMAL_ACTIVE;
1877                 return 0;
1878         }
1879 
1880         /*
1881          * Block waiting for the carrier detect and the line to become
1882          * free (i.e., not in use by the callout).  While we are in
1883          * this loop, info->count is dropped by one, so that
1884          * rs_close() knows when to free things.  We restore it upon
1885          * exit, either normal or abnormal.
1886          */
1887         retval = 0;
1888         add_wait_queue(&info->open_wait, &wait);
1889 #ifdef SERIAL_DEBUG_OPEN
1890         printk("block_til_ready before block: ttys%d, count = %d\n",
1891                info->line, info->count);
1892 #endif
1893         info->count--;
1894         info->blocked_open++;
1895         while (1) {
1896                 cli();
1897                 if (!(info->flags & ASYNC_CALLOUT_ACTIVE))
1898                         serial_out(info, UART_MCR,
1899                                    serial_inp(info, UART_MCR) |
1900                                    (UART_MCR_DTR | UART_MCR_RTS));
1901                 sti();
1902                 current->state = TASK_INTERRUPTIBLE;
1903                 if (tty_hung_up_p(filp) ||
1904                     !(info->flags & ASYNC_INITIALIZED)) {
1905 #ifdef SERIAL_DO_RESTART
1906                         if (info->flags & ASYNC_HUP_NOTIFY)
1907                                 retval = -EAGAIN;
1908                         else
1909                                 retval = -ERESTARTSYS;  
1910 #else
1911                         retval = -EAGAIN;
1912 #endif
1913                         break;
1914                 }
1915                 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1916                     !(info->flags & ASYNC_CLOSING) &&
1917                     (do_clocal || (serial_in(info, UART_MSR) &
1918                                    UART_MSR_DCD)))
1919                         break;
1920                 if (current->signal & ~current->blocked) {
1921                         retval = -ERESTARTSYS;
1922                         break;
1923                 }
1924 #ifdef SERIAL_DEBUG_OPEN
1925                 printk("block_til_ready blocking: ttys%d, count = %d\n",
1926                        info->line, info->count);
1927 #endif
1928                 schedule();
1929         }
1930         current->state = TASK_RUNNING;
1931         remove_wait_queue(&info->open_wait, &wait);
1932         if (!tty_hung_up_p(filp))
1933                 info->count++;
1934         info->blocked_open--;
1935 #ifdef SERIAL_DEBUG_OPEN
1936         printk("block_til_ready after blocking: ttys%d, count = %d\n",
1937                info->line, info->count);
1938 #endif
1939         if (retval)
1940                 return retval;
1941         info->flags |= ASYNC_NORMAL_ACTIVE;
1942         return 0;
1943 }       
1944 
1945 /*
1946  * This routine is called whenever a serial port is opened.  It
1947  * enables interrupts for a serial port, linking in its async structure into
1948  * the IRQ chain.   It also performs the serial-speicific
1949  * initalization for the tty structure.
1950  */
1951 int rs_open(struct tty_struct *tty, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
1952 {
1953         struct async_struct     *info;
1954         int                     retval, line;
1955 
1956         line = MINOR(tty->device) - tty->driver.minor_start;
1957         if ((line < 0) || (line >= NR_PORTS))
1958                 return -ENODEV;
1959         info = rs_table + line;
1960         if (serial_paranoia_check(info, tty->device, "rs_open"))
1961                 return -ENODEV;
1962         
1963 #ifdef SERIAL_DEBUG_OPEN
1964         printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
1965                info->count);
1966 #endif
1967         info->count++;
1968         tty->driver_data = info;
1969         info->tty = tty;
1970         
1971         if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
1972                 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1973                         *tty->termios = info->normal_termios;
1974                 else 
1975                         *tty->termios = info->callout_termios;
1976         }
1977         /*
1978          * Start up serial port
1979          */
1980         retval = startup(info);
1981         if (retval)
1982                 return retval;
1983 
1984         retval = block_til_ready(tty, filp, info);
1985         if (retval) {
1986 #ifdef SERIAL_DEBUG_OPEN
1987                 printk("rs_open returning after block_til_ready with %d\n",
1988                        retval);
1989 #endif
1990                 return retval;
1991         }
1992 
1993         info->session = current->session;
1994         info->pgrp = current->pgrp;
1995 
1996 #ifdef SERIAL_DEBUG_OPEN
1997         printk("rs_open ttys%d successful...", info->line);
1998 #endif
1999         return 0;
2000 }
2001 
2002 /*
2003  * ---------------------------------------------------------------------
2004  * rs_init() and friends
2005  *
2006  * rs_init() is called at boot-time to initialize the serial driver.
2007  * ---------------------------------------------------------------------
2008  */
2009 
2010 /*
2011  * This routine prints out the appropriate serial driver version
2012  * number, and identifies which options were configured into this
2013  * driver.
2014  */
2015 static void show_serial_version(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2016 {
2017         printk("Serial driver version 4.00 with");
2018 #ifdef CONFIG_HUB6
2019         printk(" HUB-6");
2020 #define SERIAL_OPT
2021 #endif
2022 #ifdef SERIAL_OPT
2023         printk(" enabled\n");
2024 #else
2025         printk(" no serial options enabled\n");
2026 #endif
2027 #undef SERIAL_OPT
2028 }
2029 
2030 /*
2031  * This routine is called by do_auto_irq(); it attempts to determine
2032  * which interrupt a serial port is configured to use.  It is not
2033  * fool-proof, but it works a large part of the time.
2034  */
2035 static int get_auto_irq(struct async_struct *info)
     /* [previous][next][first][last][top][bottom][index][help] */
2036 {
2037         unsigned char save_MCR, save_IER, save_ICP=0;
2038         unsigned short ICP=0, port = info->port;
2039         unsigned long timeout;
2040         
2041         /*
2042          * Enable interrupts and see who answers
2043          */
2044         rs_irq_triggered = 0;
2045         cli();
2046         save_IER = serial_inp(info, UART_IER);
2047         save_MCR = serial_inp(info, UART_MCR);
2048         if (info->flags & ASYNC_FOURPORT)  {
2049                 serial_outp(info, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
2050                 serial_outp(info, UART_IER, 0x0f);      /* enable all intrs */
2051                 ICP = (port & 0xFE0) | 0x01F;
2052                 save_ICP = inb_p(ICP);
2053                 outb_p(0x80, ICP);
2054                 (void) inb_p(ICP);
2055         } else {
2056                 serial_outp(info, UART_MCR,
2057                             UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
2058                 serial_outp(info, UART_IER, 0x0f);      /* enable all intrs */
2059         }
2060         sti();
2061         /*
2062          * Next, clear the interrupt registers.
2063          */
2064         (void)serial_inp(info, UART_LSR);
2065         (void)serial_inp(info, UART_RX);
2066         (void)serial_inp(info, UART_IIR);
2067         (void)serial_inp(info, UART_MSR);
2068         
2069         timeout = jiffies+2;
2070         while (timeout >= jiffies) {
2071                 if (rs_irq_triggered)
2072                         break;
2073         }
2074         /*
2075          * Now check to see if we got any business, and clean up.
2076          */
2077         cli();
2078         serial_outp(info, UART_IER, save_IER);
2079         serial_outp(info, UART_MCR, save_MCR);
2080         if (info->flags & ASYNC_FOURPORT)
2081                 outb_p(save_ICP, ICP);
2082         sti();
2083         return(rs_irq_triggered);
2084 }
2085 
2086 /*
2087  * Calls get_auto_irq() multiple times, to make sure we don't get
2088  * faked out by random interrupts
2089  */
2090 static int do_auto_irq(struct async_struct * info)
     /* [previous][next][first][last][top][bottom][index][help] */
2091 {
2092         unsigned                port = info->port;
2093         int                     irq_lines = 0;
2094         int                     irq_try_1 = 0, irq_try_2 = 0;
2095         int                     retries;
2096         unsigned long flags;
2097 
2098         if (!port)
2099                 return 0;
2100 
2101         /* Turn on interrupts (they may be off) */
2102         save_flags(flags); sti();
2103 
2104         irq_lines = grab_all_interrupts(rs_wild_int_mask);
2105         
2106         for (retries = 0; retries < 5; retries++) {
2107                 if (!irq_try_1)
2108                         irq_try_1 = get_auto_irq(info);
2109                 if (!irq_try_2)
2110                         irq_try_2 = get_auto_irq(info);
2111                 if (irq_try_1 && irq_try_2) {
2112                         if (irq_try_1 == irq_try_2)
2113                                 break;
2114                         irq_try_1 = irq_try_2 = 0;
2115                 }
2116         }
2117         restore_flags(flags);
2118         free_all_interrupts(irq_lines);
2119         return (irq_try_1 == irq_try_2) ? irq_try_1 : 0;
2120 }
2121 
2122 /*
2123  * This routine is called by rs_init() to initialize a specific serial
2124  * port.  It determines what type of UART ship this serial port is
2125  * using: 8250, 16450, 16550, 16550A.  The important question is
2126  * whether or not this UART is a 16550A or not, since this will
2127  * determine whether or not we can use its FIFO features or not.
2128  */
2129 static void autoconfig(struct async_struct * info)
     /* [previous][next][first][last][top][bottom][index][help] */
2130 {
2131         unsigned char status1, status2, scratch, scratch2;
2132         unsigned port = info->port;
2133         unsigned long flags;
2134 
2135         info->type = PORT_UNKNOWN;
2136         
2137         if (!port)
2138                 return;
2139 
2140         save_flags(flags); cli();
2141         
2142         /*
2143          * Do a simple existence test first; if we fail this, there's
2144          * no point trying anything else.
2145          */
2146         scratch = serial_inp(info, UART_IER);
2147         serial_outp(info, UART_IER, 0);
2148         scratch2 = serial_inp(info, UART_IER);
2149         serial_outp(info, UART_IER, scratch);
2150         if (scratch2) {
2151                 restore_flags(flags);
2152                 return;         /* We failed; there's nothing here */
2153         }
2154 
2155         /* 
2156          * Check to see if a UART is really there.  Certain broken
2157          * internal modems based on the Rockwell chipset fail this
2158          * test, because they apparently don't implement the loopback
2159          * test mode.  So this test is skipped on the COM 1 through
2160          * COM 4 ports.  This *should* be safe, since no board
2161          * manufactucturer would be stupid enough to design a board
2162          * that conflicts with COM 1-4 --- we hope!
2163          */
2164         if (!(info->flags & ASYNC_SKIP_TEST)) {
2165                 scratch = serial_inp(info, UART_MCR);
2166                 serial_outp(info, UART_MCR, UART_MCR_LOOP | scratch);
2167                 scratch2 = serial_inp(info, UART_MSR);
2168                 serial_outp(info, UART_MCR, UART_MCR_LOOP | 0x0A);
2169                 status1 = serial_inp(info, UART_MSR) & 0xF0;
2170                 serial_outp(info, UART_MCR, scratch);
2171                 serial_outp(info, UART_MSR, scratch2);
2172                 if (status1 != 0x90) {
2173                         restore_flags(flags);
2174                         return;
2175                 }
2176         } 
2177         
2178         /*
2179          * If the AUTO_IRQ flag is set, try to do the automatic IRQ
2180          * detection.
2181          */
2182         if (info->flags & ASYNC_AUTO_IRQ)
2183                 info->irq = do_auto_irq(info);
2184                 
2185         serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
2186         scratch = serial_in(info, UART_IIR) >> 6;
2187         info->xmit_fifo_size = 1;
2188         switch (scratch) {
2189                 case 0:
2190                         info->type = PORT_16450;
2191                         break;
2192                 case 1:
2193                         info->type = PORT_UNKNOWN;
2194                         break;
2195                 case 2:
2196                         info->type = PORT_16550;
2197                         break;
2198                 case 3:
2199                         info->type = PORT_16550A;
2200                         info->xmit_fifo_size = 16;
2201                         break;
2202         }
2203         if (info->type == PORT_16450) {
2204                 scratch = serial_in(info, UART_SCR);
2205                 serial_outp(info, UART_SCR, 0xa5);
2206                 status1 = serial_in(info, UART_SCR);
2207                 serial_outp(info, UART_SCR, 0x5a);
2208                 status2 = serial_in(info, UART_SCR);
2209                 serial_outp(info, UART_SCR, scratch);
2210 
2211                 if ((status1 != 0xa5) || (status2 != 0x5a))
2212                         info->type = PORT_8250;
2213         }
2214 
2215         /*
2216          * Reset the UART.
2217          */
2218         serial_outp(info, UART_MCR, 0x00);
2219         serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
2220                                      UART_FCR_CLEAR_XMIT));
2221         (void)serial_in(info, UART_RX);
2222         
2223         restore_flags(flags);
2224 }
2225 
2226 /*
2227  * The serial driver boot-time initialization code!
2228  */
2229 long rs_init(long kmem_start)
     /* [previous][next][first][last][top][bottom][index][help] */
2230 {
2231         int i;
2232         struct async_struct * info;
2233         
2234         bh_base[SERIAL_BH].routine = do_serial_bh;
2235         timer_table[RS_TIMER].fn = rs_timer;
2236         timer_table[RS_TIMER].expires = 0;
2237 #ifdef CONFIG_AUTO_IRQ
2238         rs_wild_int_mask = check_wild_interrupts(1);
2239 #endif
2240 
2241         for (i = 0; i < 16; i++) {
2242                 IRQ_ports[i] = 0;
2243                 IRQ_timeout[i] = 0;
2244         }
2245         
2246         show_serial_version();
2247 
2248         /* Initialize the tty_driver structure */
2249         
2250         memset(&serial_driver, 0, sizeof(struct tty_driver));
2251         serial_driver.magic = TTY_DRIVER_MAGIC;
2252         serial_driver.name = "ttyS";
2253         serial_driver.major = TTY_MAJOR;
2254         serial_driver.minor_start = 64;
2255         serial_driver.num = NR_PORTS;
2256         serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2257         serial_driver.subtype = SERIAL_TYPE_NORMAL;
2258         serial_driver.init_termios = tty_std_termios;
2259         serial_driver.init_termios.c_cflag =
2260                 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2261         serial_driver.flags = TTY_DRIVER_REAL_RAW;
2262         serial_driver.refcount = &serial_refcount;
2263         serial_driver.table = serial_table;
2264         serial_driver.termios = serial_termios;
2265         serial_driver.termios_locked = serial_termios_locked;
2266 
2267         serial_driver.open = rs_open;
2268         serial_driver.close = rs_close;
2269         serial_driver.write = rs_write;
2270         serial_driver.put_char = rs_put_char;
2271         serial_driver.flush_chars = rs_flush_chars;
2272         serial_driver.write_room = rs_write_room;
2273         serial_driver.chars_in_buffer = rs_chars_in_buffer;
2274         serial_driver.flush_buffer = rs_flush_buffer;
2275         serial_driver.ioctl = rs_ioctl;
2276         serial_driver.throttle = rs_throttle;
2277         serial_driver.unthrottle = rs_unthrottle;
2278         serial_driver.set_termios = rs_set_termios;
2279         serial_driver.stop = rs_stop;
2280         serial_driver.start = rs_start;
2281         serial_driver.hangup = rs_hangup;
2282 
2283         /*
2284          * The callout device is just like normal device except for
2285          * major number and the subtype code.
2286          */
2287         callout_driver = serial_driver;
2288         callout_driver.name = "cua";
2289         callout_driver.major = TTYAUX_MAJOR;
2290         callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2291 
2292         if (tty_register_driver(&serial_driver))
2293                 panic("Couldn't register serial driver\n");
2294         if (tty_register_driver(&callout_driver))
2295                 panic("Couldn't register callout driver\n");
2296         
2297         for (i = 0, info = rs_table; i < NR_PORTS; i++,info++) {
2298                 info->magic = SERIAL_MAGIC;
2299                 info->line = i;
2300                 info->tty = 0;
2301                 info->type = PORT_UNKNOWN;
2302                 info->custom_divisor = 0;
2303                 info->close_delay = 50;
2304                 info->x_char = 0;
2305                 info->event = 0;
2306                 info->count = 0;
2307                 info->blocked_open = 0;
2308                 info->tqueue.routine = do_softint;
2309                 info->tqueue.data = info;
2310                 info->callout_termios =callout_driver.init_termios;
2311                 info->normal_termios = serial_driver.init_termios;
2312                 info->open_wait = 0;
2313                 info->close_wait = 0;
2314                 info->next_port = 0;
2315                 info->prev_port = 0;
2316                 if (info->irq == 2)
2317                         info->irq = 9;
2318                 if (!(info->flags & ASYNC_BOOT_AUTOCONF))
2319                         continue;
2320                 autoconfig(info);
2321                 if (info->type == PORT_UNKNOWN)
2322                         continue;
2323                 printk("tty%02d%s at 0x%04x (irq = %d)", info->line, 
2324                        (info->flags & ASYNC_FOURPORT) ? " FourPort" : "",
2325                        info->port, info->irq);
2326                 switch (info->type) {
2327                         case PORT_8250:
2328                                 printk(" is a 8250\n");
2329                                 break;
2330                         case PORT_16450:
2331                                 printk(" is a 16450\n");
2332                                 break;
2333                         case PORT_16550:
2334                                 printk(" is a 16550\n");
2335                                 break;
2336                         case PORT_16550A:
2337                                 printk(" is a 16550A\n");
2338                                 break;
2339                         default:
2340                                 printk("\n");
2341                                 break;
2342                 }
2343         }
2344         return kmem_start;
2345 }
2346 

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