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

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