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

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