root/drivers/char/serial.c

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

DEFINITIONS

This source file includes following definitions.
  1. serial_in
  2. serial_inp
  3. serial_out
  4. serial_outp
  5. rs_stop
  6. rs_start
  7. rs_probe
  8. rs_sched_event
  9. receive_chars
  10. transmit_chars
  11. check_modem_status
  12. figure_RS_timer
  13. rs_interrupt
  14. handle_rs_break
  15. do_softint
  16. rs_timer
  17. grab_all_interrupts
  18. free_all_interrupts
  19. figure_IRQ_timeout
  20. startup
  21. shutdown
  22. change_speed
  23. restart_port
  24. rs_write
  25. rs_throttle
  26. get_serial_info
  27. set_serial_info
  28. get_modem_info
  29. set_modem_info
  30. do_autoconfig
  31. send_break
  32. check_wild_interrupts
  33. rs_ioctl
  34. rs_set_termios
  35. rs_close
  36. rs_hangup
  37. block_til_ready
  38. rs_open
  39. show_serial_version
  40. get_auto_irq
  41. do_auto_irq
  42. autoconfig
  43. rs_init

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

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