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
  57. init_module
  58. cleanup_module

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

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