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

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