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

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