root/drivers/char/serial.c

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

DEFINITIONS

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

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

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