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_lsr_info
  37. get_modem_info
  38. set_modem_info
  39. do_autoconfig
  40. send_break
  41. check_wild_interrupts
  42. rs_ioctl
  43. rs_set_termios
  44. rs_close
  45. rs_hangup
  46. block_til_ready
  47. rs_open
  48. show_serial_version
  49. get_auto_irq
  50. do_auto_irq
  51. autoconfig
  52. rs_init
  53. register_serial
  54. unregister_serial

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

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