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

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