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

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