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

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