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

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