root/drivers/char/serial.c

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

DEFINITIONS

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

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