root/drivers/char/serial.c

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

DEFINITIONS

This source file includes following definitions.
  1. serial_paranoia_check
  2. serial_in
  3. serial_inp
  4. serial_out
  5. serial_outp
  6. rs_stop
  7. rs_start
  8. rs_probe
  9. rs_sched_event
  10. receive_chars
  11. transmit_chars
  12. check_modem_status
  13. rs_interrupt
  14. rs_interrupt_single
  15. rs_interrupt
  16. rs_interrupt_single
  17. do_serial_bh
  18. do_softint
  19. rs_timer
  20. grab_all_interrupts
  21. free_all_interrupts
  22. figure_IRQ_timeout
  23. startup
  24. shutdown
  25. change_speed
  26. rs_put_char
  27. rs_flush_chars
  28. rs_write
  29. rs_write_room
  30. rs_chars_in_buffer
  31. rs_flush_buffer
  32. rs_throttle
  33. rs_unthrottle
  34. get_serial_info
  35. set_serial_info
  36. get_lsr_info
  37. get_modem_info
  38. set_modem_info
  39. do_autoconfig
  40. send_break
  41. check_wild_interrupts
  42. rs_ioctl
  43. rs_set_termios
  44. rs_close
  45. rs_hangup
  46. block_til_ready
  47. rs_open
  48. show_serial_version
  49. get_auto_irq
  50. do_auto_irq
  51. autoconfig
  52. rs_init
  53. register_serial
  54. unregister_serial

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

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