root/drivers/char/serial.c

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

DEFINITIONS

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

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