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
  16. rs_interrupt_single
  17. do_serial_bh
  18. do_softint
  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. rs_ioctl
  43. rs_set_termios
  44. rs_close
  45. rs_hangup
  46. block_til_ready
  47. rs_open
  48. show_serial_version
  49. get_auto_irq
  50. do_auto_irq
  51. autoconfig
  52. rs_init
  53. register_serial
  54. unregister_serial

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

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