home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / KERNEL-S / V1.0 / LINUX-1.0 / LINUX-1 / linux / drivers / char / serial.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-13  |  53.2 KB  |  2,076 lines

  1. /*
  2.  *  linux/kernel/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/tty.h>
  25. #include <linux/serial.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/config.h>
  28. #include <linux/major.h>
  29. #include <linux/string.h>
  30. #include <linux/fcntl.h>
  31. #include <linux/ptrace.h>
  32.  
  33. #include <asm/system.h>
  34. #include <asm/io.h>
  35. #include <asm/segment.h>
  36. #include <asm/bitops.h>
  37.  
  38. /*
  39.  * Serial driver configuration section.  Here are the various options:
  40.  *
  41.  * CONFIG_AUTO_IRQ
  42.  *        Enables automatic IRQ detection.  I've put in some
  43.  *         fixes to this which should make this work much more
  44.  *         cleanly than it used to in 0.98pl2-6.  It should be
  45.  *         much less vulnerable to false IRQs now.
  46.  * 
  47.  * CONFIG_AST_FOURPORT
  48.  *        Enables support for the AST Fourport serial port.
  49.  * 
  50.  * CONFIG_ACCENT_ASYNC
  51.  *        Enables support for the Accent Async 4 port serial
  52.  *         port.
  53.  *
  54.  * CONFIG_HUB6
  55.  *        Enables support for the venerable Bell Technologies
  56.  *        HUB6 card.
  57.  */
  58.  
  59. #undef ISR_HACK
  60.  
  61. /*
  62.  * rs_event        - Bitfield of serial lines that events pending
  63.  *                 to be processed at the next clock tick.
  64.  * IRQ_timeout        - How long the timeout should be for each IRQ
  65.  *                 should be after the IRQ has been active.
  66.  * IRQ_timer        - Array of timeout values for each interrupt IRQ.
  67.  *                 This is based on jiffies; not offsets.
  68.  * 
  69.  * We assume here that int's are 32 bits, so an array of two gives us
  70.  * 64 lines, which is the maximum we can support.
  71.  */
  72. static int rs_event[2];
  73.  
  74. static struct async_struct *IRQ_ports[16];
  75. static int IRQ_active;
  76. static unsigned long IRQ_timer[16];
  77. static int IRQ_timeout[16];
  78. static volatile int rs_irq_triggered;
  79. static volatile int rs_triggered;
  80. static int rs_wild_int_mask;
  81.  
  82. static void autoconfig(struct async_struct * info);
  83. static void change_speed(unsigned int line);
  84.     
  85. /*
  86.  * This assumes you have a 1.8432 MHz clock for your UART.
  87.  *
  88.  * It'd be nice if someone built a serial card with a 24.576 MHz
  89.  * clock, since the 16550A is capable of handling a top speed of 1.5
  90.  * megabits/second; but this requires the faster clock.
  91.  */
  92. #define BASE_BAUD ( 1843200 / 16 )
  93.  
  94. #ifdef CONFIG_AUTO_IRQ
  95. #define AUTO_IRQ_FLAG ASYNC_AUTO_IRQ
  96. #else
  97. #define AUTO_IRQ_FLAG 0
  98. #endif
  99.  
  100. /* Standard COM flags (except for COM4, because of the 8514 problem) */
  101. #define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST | AUTO_IRQ_FLAG)
  102. #define STD_COM4_FLAGS (ASYNC_BOOT_AUTOCONF | AUTO_IRQ_FLAG)
  103.  
  104. #ifdef CONFIG_AST_FOURPORT
  105. #define FOURPORT_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_FOURPORT | AUTO_IRQ_FLAG)
  106. #else
  107. #define FOURPORT_FLAGS (ASYNC_FOURPORT | AUTO_IRQ_FLAG)
  108. #endif
  109.  
  110. #ifdef CONFIG_ACCENT_ASYNC
  111. #define ACCENT_FLAGS (ASYNC_BOOT_AUTOCONF | AUTO_IRQ_FLAG)
  112. #else
  113. #define ACCENT_FLAGS AUTO_IRQ_FLAG
  114. #endif
  115.  
  116. #ifdef CONFIG_BOCA
  117. #define BOCA_FLAGS (ASYNC_BOOT_AUTOCONF | AUTO_IRQ_FLAG)
  118. #else
  119. #define BOCA_FLAGS AUTO_IRQ_FLAG
  120. #endif
  121.  
  122. #ifdef CONFIG_HUB6
  123. #define HUB6_FLAGS (ASYNC_BOOT_AUTOCONF)
  124. #else
  125. #define HUB6_FLAGS 0
  126. #endif
  127.     
  128. /*
  129.  * The following define the access methods for the HUB6 card. All
  130.  * access is through two ports for all 24 possible chips. The card is
  131.  * selected through the high 2 bits, the port on that card with the
  132.  * "middle" 3 bits, and the register on that port with the bottom
  133.  * 3 bits.
  134.  *
  135.  * While the access port and interrupt is configurable, the default
  136.  * port locations are 0x302 for the port control register, and 0x303
  137.  * for the data read/write register. Normally, the interrupt is at irq3
  138.  * but can be anything from 3 to 7 inclusive. Note tht using 3 will
  139.  * require disabling com2.
  140.  */
  141.  
  142. #define C_P(card,port) (((card)<<6|(port)<<3) + 1)
  143.  
  144. struct async_struct rs_table[] = {
  145.     /* UART CLK   PORT IRQ     FLAGS        */
  146.     { BASE_BAUD, 0x3F8, 4, STD_COM_FLAGS },        /* ttyS0 */
  147.     { BASE_BAUD, 0x2F8, 3, STD_COM_FLAGS },        /* ttyS1 */
  148.     { BASE_BAUD, 0x3E8, 4, STD_COM_FLAGS },        /* ttyS2 */
  149.     { BASE_BAUD, 0x2E8, 3, STD_COM4_FLAGS },    /* ttyS3 */
  150.  
  151.     { BASE_BAUD, 0x1A0, 9, FOURPORT_FLAGS },     /* ttyS4 */
  152.     { BASE_BAUD, 0x1A8, 9, FOURPORT_FLAGS },    /* ttyS5 */
  153.     { BASE_BAUD, 0x1B0, 9, FOURPORT_FLAGS },    /* ttyS6 */
  154.     { BASE_BAUD, 0x1B8, 9, FOURPORT_FLAGS },    /* ttyS7 */
  155.  
  156.     { BASE_BAUD, 0x2A0, 5, FOURPORT_FLAGS },    /* ttyS8 */
  157.     { BASE_BAUD, 0x2A8, 5, FOURPORT_FLAGS },    /* ttyS9 */
  158.     { BASE_BAUD, 0x2B0, 5, FOURPORT_FLAGS },    /* ttyS10 */
  159.     { BASE_BAUD, 0x2B8, 5, FOURPORT_FLAGS },    /* ttyS11 */
  160.     
  161.     { BASE_BAUD, 0x330, 4, ACCENT_FLAGS },        /* ttyS12 */
  162.     { BASE_BAUD, 0x338, 4, ACCENT_FLAGS },        /* ttyS13 */
  163.     { BASE_BAUD, 0x000, 0, 0 },    /* ttyS14 (spare; user configurable) */
  164.     { BASE_BAUD, 0x000, 0, 0 },    /* ttyS15 (spare; user configurable) */
  165.  
  166.     { BASE_BAUD, 0x100, 12, BOCA_FLAGS },    /* ttyS16 */
  167.     { BASE_BAUD, 0x108, 12, BOCA_FLAGS },    /* ttyS17 */
  168.     { BASE_BAUD, 0x110, 12, BOCA_FLAGS },    /* ttyS18 */
  169.     { BASE_BAUD, 0x118, 12, BOCA_FLAGS },    /* ttyS19 */
  170.     { BASE_BAUD, 0x120, 12, BOCA_FLAGS },    /* ttyS20 */
  171.     { BASE_BAUD, 0x128, 12, BOCA_FLAGS },    /* ttyS21 */
  172.     { BASE_BAUD, 0x130, 12, BOCA_FLAGS },    /* ttyS22 */
  173.     { BASE_BAUD, 0x138, 12, BOCA_FLAGS },    /* ttyS23 */
  174.     { BASE_BAUD, 0x140, 12, BOCA_FLAGS },    /* ttyS24 */
  175.     { BASE_BAUD, 0x148, 12, BOCA_FLAGS },    /* ttyS25 */
  176.     { BASE_BAUD, 0x150, 12, BOCA_FLAGS },    /* ttyS26 */
  177.     { BASE_BAUD, 0x158, 12, BOCA_FLAGS },    /* ttyS27 */
  178.     { BASE_BAUD, 0x160, 12, BOCA_FLAGS },    /* ttyS28 */
  179.     { BASE_BAUD, 0x168, 12, BOCA_FLAGS },    /* ttyS29 */
  180.     { BASE_BAUD, 0x170, 12, BOCA_FLAGS },    /* ttyS30 */
  181.     { BASE_BAUD, 0x178, 12, BOCA_FLAGS },    /* ttyS31 */
  182.  
  183. /* You can have up to four HUB6's in the system, but I've only
  184.  * included two cards here for a total of twelve ports.
  185.  */
  186.     { BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,0) },    /* ttyS32 */
  187.     { BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,1) },    /* ttyS33 */
  188.     { BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,2) },    /* ttyS34 */
  189.     { BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,3) },    /* ttyS35 */
  190.     { BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,4) },    /* ttyS36 */
  191.     { BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,5) },    /* ttyS37 */
  192.     { BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,0) },    /* ttyS32 */
  193.     { BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,1) },    /* ttyS33 */
  194.     { BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,2) },    /* ttyS34 */
  195.     { BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,3) },    /* ttyS35 */
  196.     { BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,4) },    /* ttyS36 */
  197.     { BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,5) },    /* ttyS37 */
  198. };
  199.  
  200. #define NR_PORTS    (sizeof(rs_table)/sizeof(struct async_struct))
  201.  
  202. /*
  203.  * This is used to figure out the divsor speeds and the timeouts
  204.  */
  205. static int baud_table[] = {
  206.     0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
  207.     9600, 19200, 38400, 57600, 115200, 0 };
  208.  
  209. static void rs_throttle(struct tty_struct * tty, int status);
  210.  
  211. static inline unsigned int serial_in(struct async_struct *info, int offset)
  212. {
  213.     if (info->hub6) {
  214.     outb(info->hub6 - 1 + offset, info->port);
  215.     return inb(info->port+1);
  216.     } else
  217.     return inb(info->port + offset);
  218. }
  219.  
  220. static inline unsigned int serial_inp(struct async_struct *info, int offset)
  221. {
  222.     if (info->hub6) {
  223.     outb(info->hub6 - 1 + offset, info->port);
  224.     return inb_p(info->port+1);
  225.     } else
  226.     return inb_p(info->port + offset);
  227. }
  228.  
  229. static inline void serial_out(struct async_struct *info, int offset, int value)
  230. {
  231.     if (info->hub6) {
  232.     outb(info->hub6 - 1 + offset, info->port);
  233.     outb(value, info->port+1);
  234.     } else
  235.     outb(value, info->port+offset);
  236. }
  237.  
  238. static inline void serial_outp(struct async_struct *info, int offset,
  239.                    int value)
  240. {
  241.     if (info->hub6) {
  242.     outb(info->hub6 - 1 + offset, info->port);
  243.     outb_p(value, info->port+1);
  244.     } else
  245.     outb_p(value, info->port+offset);
  246. }
  247.  
  248. /*
  249.  * ------------------------------------------------------------
  250.  * rs_stop() and rs_start()
  251.  *
  252.  * This routines are called before setting or resetting tty->stopped.
  253.  * They enable or disable transmitter interrupts, as necessary.
  254.  * ------------------------------------------------------------
  255.  */
  256. static void rs_stop(struct tty_struct *tty)
  257. {
  258.     struct async_struct *info;
  259.     
  260.     info = rs_table + DEV_TO_SL(tty->line);
  261.  
  262.     if (info->flags & ASYNC_CLOSING) {
  263.         tty->stopped = 0;
  264.         tty->hw_stopped = 0;
  265.         return;
  266.     }
  267.  
  268.     info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
  269. #ifdef ISR_HACK
  270.     serial_out(info, UART_IER, info->IER);
  271. #endif
  272. }
  273.  
  274. static void rs_start(struct tty_struct *tty)
  275. {
  276.     struct async_struct *info;
  277.     
  278.     info = rs_table + DEV_TO_SL(tty->line);
  279.     
  280.     info->IER = (UART_IER_MSI | UART_IER_RLSI |
  281.              UART_IER_THRI | UART_IER_RDI);
  282. #ifdef ISR_HACK
  283.     serial_out(info, UART_IER, info->IER);
  284. #endif
  285. }
  286.  
  287. /*
  288.  * ----------------------------------------------------------------------
  289.  *
  290.  * Here starts the interrupt handling routines.  All of the following
  291.  * subroutines are declared as inline and are folded into
  292.  * rs_interrupt().  They were separated out for readability's sake.
  293.  *
  294.  * Note: rs_interrupt() is a "fast" interrupt, which means that it
  295.  * runs with interrupts turned off.  People who may want to modify
  296.  * rs_interrupt() should try to keep the interrupt handler as fast as
  297.  * possible.  After you are done making modifications, it is not a bad
  298.  * idea to do:
  299.  * 
  300.  * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
  301.  *
  302.  * and look at the resulting assemble code in serial.s.
  303.  *
  304.  *                 - Ted Ts'o (tytso@mit.edu), 7-Mar-93
  305.  * -----------------------------------------------------------------------
  306.  */
  307.  
  308. /*
  309.  * This is the serial driver's interrupt routine while we are probing
  310.  * for submarines.
  311.  */
  312. static void rs_probe(int irq)
  313. {
  314.     rs_irq_triggered = irq;
  315.     rs_triggered |= 1 << irq;
  316.     return;
  317. }
  318.  
  319. /*
  320.  * This routine is used by the interrupt handler to schedule
  321.  * processing in the software interrupt portion of the driver.
  322.  */
  323. static inline void rs_sched_event(struct async_struct *info,
  324.                   int event)
  325. {
  326.     info->event |= 1 << event;
  327.     set_bit(info->line, rs_event);
  328.     mark_bh(SERIAL_BH);
  329. }
  330.  
  331. static inline void receive_chars(struct async_struct *info,
  332.                  int *status)
  333. {
  334.     struct tty_queue * queue;
  335.     int head, tail, ch;
  336.  
  337. /*
  338.  * Just like the LEFT(x) macro, except it uses the loal tail
  339.  * and head variables.
  340.  */
  341. #define VLEFT ((tail-head-1)&(TTY_BUF_SIZE-1))
  342.  
  343.     queue = &info->tty->read_q;
  344.     head = queue->head;
  345.     tail = queue->tail;
  346.     do {
  347.         ch = serial_inp(info, UART_RX);
  348.         /*
  349.          * There must be at least 2 characters
  350.          * free in the queue; otherwise we punt.
  351.          */
  352.         if (VLEFT < 2)
  353.             break;
  354.         if (*status & info->read_status_mask) {
  355.             set_bit(head, &info->tty->readq_flags);
  356.             if (*status & (UART_LSR_BI)) {
  357.                 queue->buf[head++]= TTY_BREAK;
  358.                 rs_sched_event(info, RS_EVENT_BREAK);
  359.             } else if (*status & UART_LSR_PE)
  360.                 queue->buf[head++]= TTY_PARITY;
  361.             else if (*status & UART_LSR_FE)
  362.                 queue->buf[head++]= TTY_FRAME;
  363.             else if (*status & UART_LSR_OE)
  364.                 queue->buf[head++]= TTY_OVERRUN;
  365.             head &= TTY_BUF_SIZE-1;
  366.         }
  367.         queue->buf[head++] = ch;
  368.         head &= TTY_BUF_SIZE-1;
  369.     } while ((*status = serial_inp(info, UART_LSR)) & UART_LSR_DR);
  370.     queue->head = head;
  371.     if ((VLEFT < RQ_THRESHOLD_LW) && !set_bit(TTY_RQ_THROTTLED,
  372.                           &info->tty->flags)) 
  373.         rs_throttle(info->tty, TTY_THROTTLE_RQ_FULL);
  374.     rs_sched_event(info, RS_EVENT_READ_PROCESS);
  375. #ifdef SERIAL_DEBUG_INTR
  376.     printk("DR...");
  377. #endif
  378. }
  379.  
  380. static inline void transmit_chars(struct async_struct *info, int *done_work)
  381. {
  382.     struct tty_queue * queue;
  383.     int head, tail, count;
  384.     
  385.     queue = &info->tty->write_q;
  386.     head = queue->head;
  387.     tail = queue->tail;
  388.     if (head==tail && !info->x_char) {
  389.         info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
  390. #ifdef ISR_HACK
  391.         serial_out(info, UART_IER, info->IER);
  392. #endif
  393.         return;
  394.     }
  395.     count = info->xmit_fifo_size;
  396.     if (info->x_char) {
  397.         serial_outp(info, UART_TX, info->x_char);
  398.         info->x_char = 0;
  399.         count--;
  400.     }
  401.     while (count-- && (tail != head)) {
  402.         serial_outp(info, UART_TX, queue->buf[tail++]);
  403.         tail &= TTY_BUF_SIZE-1;
  404.     }
  405.     queue->tail = tail;
  406.     if (VLEFT > WAKEUP_CHARS) {
  407.         rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
  408.         if (info->tty->write_data_cnt) {
  409.             set_bit(info->tty->line, &tty_check_write);
  410.             mark_bh(TTY_BH);
  411.         }
  412.     }
  413. #ifdef SERIAL_DEBUG_INTR
  414.     printk("THRE...");
  415. #endif
  416.     (*done_work)++;
  417. }
  418.  
  419. static inline int check_modem_status(struct async_struct *info)
  420. {
  421.     int    status;
  422.     
  423.     status = serial_in(info, UART_MSR);
  424.         
  425.     if ((status & UART_MSR_DDCD) && !C_CLOCAL(info->tty)) {
  426. #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
  427.         printk("ttys%d CD now %s...", info->line,
  428.                (status & UART_MSR_DCD) ? "on" : "off");
  429. #endif        
  430.         if (status & UART_MSR_DCD)
  431.             rs_sched_event(info, RS_EVENT_OPEN_WAKEUP);
  432.         else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
  433.                (info->flags & ASYNC_CALLOUT_NOHUP))) {
  434. #ifdef SERIAL_DEBUG_OPEN
  435.             printk("scheduling hangup...");
  436. #endif
  437.             rs_sched_event(info, RS_EVENT_HANGUP);
  438.         }
  439.     }
  440.     if (C_CRTSCTS(info->tty) && !(info->flags & ASYNC_CLOSING)) {
  441.         if (info->tty->hw_stopped) {
  442.             if (status & UART_MSR_CTS) {
  443. #ifdef SERIAL_DEBUG_INTR
  444.                 printk("CTS tx start...");
  445. #endif
  446.                 info->tty->hw_stopped = 0;
  447.                 rs_start(info->tty);
  448.                 return 1;
  449.             }
  450.         } else {
  451.             if (!(status & UART_MSR_CTS)) {
  452. #ifdef SERIAL_DEBUG_INTR
  453.                 printk("CTS tx stop...");
  454. #endif
  455.                 info->tty->hw_stopped = 1;
  456.                 rs_stop(info->tty);
  457.             }
  458.         }
  459.     }
  460.     return 0;
  461. }
  462.  
  463. static inline void figure_RS_timer(void)
  464. {
  465.     int    timeout = jiffies + 60*HZ; /* 60 seconds; really big :-) */
  466.     int    i, mask;
  467.     
  468.     if (!IRQ_active)
  469.         return;
  470.     for (i=0, mask = 1; mask <= IRQ_active; i++, mask <<= 1) {
  471.         if (!(mask & IRQ_active))
  472.             continue;
  473.         if (IRQ_timer[i] < timeout)
  474.             timeout = IRQ_timer[i];
  475.     }
  476.     timer_table[RS_TIMER].expires = timeout;
  477.     timer_active |= 1 << RS_TIMER;
  478. }
  479.  
  480.  
  481. /*
  482.  * This is the serial driver's generic interrupt routine
  483.  */
  484. static void rs_interrupt(int irq)
  485. {
  486.     int status;
  487.     struct async_struct * info;
  488.     int done, done_work, pass_number, recheck_count;
  489.  
  490.     rs_irq_triggered = irq;
  491.     rs_triggered |= 1 << irq;
  492.     
  493.     info = IRQ_ports[irq];
  494.     done = 1;
  495.     done_work = 0;
  496.     pass_number = 0;
  497.     while (info) {
  498.         if (info->tty &&
  499.             info->tty->termios &&
  500.             (!pass_number ||
  501.              !(serial_inp(info, UART_IIR) & UART_IIR_NO_INT))) {
  502.             done = 0;
  503.             status = serial_inp(info, UART_LSR);
  504.             if (status & UART_LSR_DR) {
  505.                 receive_chars(info, &status);
  506.                 done_work++;
  507.             }
  508.         recheck_count = 0;
  509.         recheck_write:
  510.             if (status & UART_LSR_THRE) {
  511.                 wake_up_interruptible(&info->xmit_wait);
  512.                 if (!info->tty->stopped &&
  513.                     !info->tty->hw_stopped)
  514.                     transmit_chars(info, &done_work);
  515.             }
  516.             if (check_modem_status(info) &&
  517.                 (recheck_count++ <= 64))
  518.                 goto recheck_write;
  519. #ifdef SERIAL_DEBUG_INTR
  520.             if (recheck_count > 16)
  521.                 printk("recheck_count = %d\n", recheck_count);
  522. #endif
  523.         }
  524. #ifdef ISR_HACK
  525.         serial_outp(info, UART_IER, 0);
  526.         serial_out(info, UART_IER, info->IER);
  527. #endif
  528.         
  529.         info = info->next_port;
  530.         if (!info && !done) {
  531.             info = IRQ_ports[irq];
  532.             done = 1;
  533.             if (pass_number++ > 64)
  534.                 break;         /* Prevent infinite loops */
  535.         }
  536.     }
  537.     if ((info = IRQ_ports[irq]) != NULL) {
  538. #ifdef 0
  539.         do {
  540.             serial_outp(info, UART_IER, 0);
  541.             serial_out(info, UART_IER, info->IER);
  542.             info = info->next_port;
  543.         } while (info);
  544. #endif            
  545.         if (irq && !done_work)
  546.             IRQ_timer[irq] = jiffies + 1500;
  547.         else
  548.             IRQ_timer[irq] = jiffies + IRQ_timeout[irq];
  549.         IRQ_active |= 1 << irq;
  550.     }
  551.     figure_RS_timer();
  552. }
  553.  
  554. /*
  555.  * -------------------------------------------------------------------
  556.  * Here ends the serial interrupt routines.
  557.  * -------------------------------------------------------------------
  558.  */
  559.  
  560. /*
  561.  * This routine is called when we receive a break on a serial line.
  562.  * It is executed out of the software interrupt routine.
  563.  */
  564. static inline void handle_rs_break(struct async_struct *info)
  565. {
  566.     if (info->flags & ASYNC_SAK)
  567.         do_SAK(info->tty);
  568.         
  569.     if (!I_IGNBRK(info->tty) && I_BRKINT(info->tty)) {
  570.         flush_input(info->tty);
  571.         flush_output(info->tty);
  572.         if (info->tty->pgrp > 0)
  573.             kill_pg(info->tty->pgrp, SIGINT,1);
  574.     }
  575. }
  576.  
  577. /*
  578.  * This routine is used to handle the "bottom half" processing for the
  579.  * serial driver, known also the "software interrupt" processing.
  580.  * This processing is done at the kernel interrupt level, after the
  581.  * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
  582.  * is where time-consuming activities which can not be done in the
  583.  * interrupt driver proper are done; the interrupt driver schedules
  584.  * them using rs_sched_event(), and they get done here.
  585.  */
  586. static void do_softint(void *unused)
  587. {
  588.     int            i;
  589.     struct async_struct    *info;
  590.     
  591.     for (i = 0, info = rs_table; i < NR_PORTS; i++,info++) {
  592.         if (clear_bit(i, rs_event)) {
  593.             if (!info->tty)    
  594.                 continue;
  595.             if (clear_bit(RS_EVENT_READ_PROCESS, &info->event)) {
  596.                 TTY_READ_FLUSH(info->tty);
  597.             }
  598.             if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
  599.                 wake_up_interruptible(&info->tty->write_q.proc_list);
  600.             }
  601.             if (clear_bit(RS_EVENT_HANGUP, &info->event)) {
  602.                 tty_hangup(info->tty);
  603.                 wake_up_interruptible(&info->open_wait);
  604.                 info->flags &= ~(ASYNC_NORMAL_ACTIVE|
  605.                          ASYNC_CALLOUT_ACTIVE);
  606.             }
  607.             if (clear_bit(RS_EVENT_BREAK, &info->event))
  608.                 handle_rs_break(info);
  609.             if (clear_bit(RS_EVENT_OPEN_WAKEUP, &info->event)) {
  610.                 wake_up_interruptible(&info->open_wait);
  611.             }
  612.         }
  613.     }
  614. }
  615.  
  616. /*
  617.  * This subroutine is called when the RS_TIMER goes off.  It is used
  618.  * by the serial driver to run the rs_interrupt routine at certain
  619.  * intervals, either because a serial interrupt might have been lost,
  620.  * or because (in the case of IRQ=0) the serial port does not have an
  621.  * interrupt, and is being checked only via the timer interrupts.
  622.  */
  623. static void rs_timer(void)
  624. {
  625.     int    i, mask;
  626.     int    timeout = 0;
  627.  
  628.     for (i = 0, mask = 1; mask <= IRQ_active; i++, mask <<= 1) {
  629.         if ((mask & IRQ_active) && (IRQ_timer[i] <= jiffies)) {
  630.             IRQ_active &= ~mask;
  631.             cli();
  632. #ifdef SERIAL_DEBUG_TIMER
  633.             printk("rs_timer: rs_interrupt(%d)...", i);
  634. #endif
  635.             rs_interrupt(i);
  636.             sti();
  637.         }
  638.         if (mask & IRQ_active) {
  639.             if (!timeout || (IRQ_timer[i] < timeout))
  640.                 timeout = IRQ_timer[i];
  641.         }
  642.     }
  643.     if (timeout) {
  644.         timer_table[RS_TIMER].expires = timeout;
  645.         timer_active |= 1 << RS_TIMER;
  646.     }
  647. }
  648.  
  649. /*
  650.  * ---------------------------------------------------------------
  651.  * Low level utility subroutines for the serial driver:  routines to
  652.  * figure out the appropriate timeout for an interrupt chain, routines
  653.  * to initialize and startup a serial port, and routines to shutdown a
  654.  * serial port.  Useful stuff like that.
  655.  * ---------------------------------------------------------------
  656.  */
  657.  
  658. /*
  659.  * Grab all interrupts in preparation for doing an automatic irq
  660.  * detection.  dontgrab is a mask of irq's _not_ to grab.  Returns a
  661.  * mask of irq's which were grabbed and should therefore be freed
  662.  * using free_all_interrupts().
  663.  */
  664. static int grab_all_interrupts(int dontgrab)
  665. {
  666.     int             irq_lines = 0;
  667.     int            i, mask;
  668.     struct sigaction     sa;
  669.     
  670.     sa.sa_handler = rs_probe;
  671.     sa.sa_flags = (SA_INTERRUPT);
  672.     sa.sa_mask = 0;
  673.     sa.sa_restorer = NULL;
  674.     
  675.     for (i = 0, mask = 1; i < 16; i++, mask <<= 1) {
  676.         if (!(mask & dontgrab) && !irqaction(i, &sa)) {
  677.             irq_lines |= mask;
  678.         }
  679.     }
  680.     return irq_lines;
  681. }
  682.  
  683. /*
  684.  * Release all interrupts grabbed by grab_all_interrupts
  685.  */
  686. static void free_all_interrupts(int irq_lines)
  687. {
  688.     int    i;
  689.     
  690.     for (i = 0; i < 16; i++) {
  691.         if (irq_lines & (1 << i))
  692.             free_irq(i);
  693.     }
  694. }
  695.  
  696. /*
  697.  * This routine figures out the correct timeout for a particular IRQ.
  698.  * It uses the smallest timeout of all of the serial ports in a
  699.  * particular interrupt chain.
  700.  */
  701. static void figure_IRQ_timeout(int irq)
  702. {
  703.     struct    async_struct    *info;
  704.     int    timeout = 6000;    /* 60 seconds === a long time :-) */
  705.  
  706.     info = IRQ_ports[irq];
  707.     if (!info) {
  708.         IRQ_timeout[irq] = 6000;
  709.         return;
  710.     }
  711.     while (info) {
  712.         if (info->timeout < timeout)
  713.             timeout = info->timeout;
  714.         info = info->next_port;
  715.     }
  716.     if (!irq)
  717.         timeout = timeout / 2;
  718.     IRQ_timeout[irq] = timeout ? timeout : 1;
  719. }
  720.  
  721. static int startup(struct async_struct * info, int get_irq)
  722. {
  723.     unsigned short ICP;
  724.     unsigned long flags;
  725.     struct sigaction    sa;
  726.     int            retval;
  727.  
  728.     if (info->flags & ASYNC_INITIALIZED)
  729.         return 0;
  730.  
  731.     if (!info->port || !info->type) {
  732.         if (info->tty)
  733.             set_bit(TTY_IO_ERROR, &info->tty->flags);
  734.         return 0;
  735.     }
  736.  
  737.     save_flags(flags); cli();
  738.  
  739. #ifdef SERIAL_DEBUG_OPEN
  740.     printk("starting up ttys%d (irq %d)...", info->line, info->irq);
  741. #endif
  742.  
  743.     /*
  744.      * Allocate the IRQ if necessary
  745.      */
  746.     if (get_irq && info->irq && !IRQ_ports[info->irq]) {
  747.         sa.sa_handler = rs_interrupt;
  748.         sa.sa_flags = (SA_INTERRUPT);
  749.         sa.sa_mask = 0;
  750.         sa.sa_restorer = NULL;
  751.         retval = irqaction(info->irq,&sa);
  752.         if (retval) {
  753.             restore_flags(flags);
  754.             return retval;
  755.         }
  756.     }
  757.  
  758.     /*
  759.      * Clear the FIFO buffers and disable them
  760.      * (they will be reenabled in change_speed())
  761.      */
  762.     if (info->type == PORT_16550A) {
  763.         serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
  764.                          UART_FCR_CLEAR_XMIT));
  765.         info->xmit_fifo_size = 16;
  766.     } else
  767.         info->xmit_fifo_size = 1;
  768.  
  769.     /*
  770.      * Clear the interrupt registers.
  771.      */
  772.     (void)serial_inp(info, UART_LSR);
  773.     (void)serial_inp(info, UART_RX);
  774.     (void)serial_inp(info, UART_IIR);
  775.     (void)serial_inp(info, UART_MSR);
  776.  
  777.     /*
  778.      * Now, initialize the UART 
  779.      */
  780.     serial_outp(info, UART_LCR, UART_LCR_WLEN8);    /* reset DLAB */
  781.     if (info->flags & ASYNC_FOURPORT) 
  782.         serial_outp(info, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
  783.     else
  784.         serial_outp(info, UART_MCR,
  785.                 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
  786.     
  787.     /*
  788.      * Finally, enable interrupts
  789.      */
  790. #ifdef ISR_HACK
  791.     info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
  792.     serial_outp(info, UART_IER, info->IER);    /* enable interrupts */
  793. #else
  794.     info->IER = (UART_IER_MSI | UART_IER_RLSI |
  795.              UART_IER_THRI | UART_IER_RDI);
  796.     serial_outp(info, UART_IER, info->IER);    /* enable all intrs */
  797. #endif
  798.     if (info->flags & ASYNC_FOURPORT) {
  799.         /* Enable interrupts on the AST Fourport board */
  800.         ICP = (info->port & 0xFE0) | 0x01F;
  801.         outb_p(0x80, ICP);
  802.         (void) inb_p(ICP);
  803.     }
  804.  
  805.     /*
  806.      * And clear the interrupt registers again for luck.
  807.      */
  808.     (void)serial_inp(info, UART_LSR);
  809.     (void)serial_inp(info, UART_RX);
  810.     (void)serial_inp(info, UART_IIR);
  811.     (void)serial_inp(info, UART_MSR);
  812.  
  813.     if (info->tty)
  814.         clear_bit(TTY_IO_ERROR, &info->tty->flags);
  815.     /*
  816.      * Set up parity check flag
  817.      */
  818.     if (info->tty && info->tty->termios && I_INPCK(info->tty))
  819.         info->read_status_mask = (UART_LSR_OE | UART_LSR_BI |
  820.                       UART_LSR_FE | UART_LSR_PE);
  821.     else
  822.         info->read_status_mask = (UART_LSR_OE | UART_LSR_BI |
  823.                       UART_LSR_FE);
  824.  
  825.     /*
  826.      * Insert serial port into IRQ chain.
  827.      */
  828.     info->prev_port = 0;
  829.     info->next_port = IRQ_ports[info->irq];
  830.     if (info->next_port)
  831.         info->next_port->prev_port = info;
  832.     IRQ_ports[info->irq] = info;
  833.     figure_IRQ_timeout(info->irq);
  834.  
  835.     /*
  836.      * Set up serial timers...
  837.      */
  838.     IRQ_active |= 1 << info->irq;
  839.     figure_RS_timer();
  840.  
  841.     /*
  842.      * and set the speed of the serial port
  843.      */
  844.     change_speed(info->line);
  845.  
  846.     info->flags |= ASYNC_INITIALIZED;
  847.     restore_flags(flags);
  848.     return 0;
  849. }
  850.  
  851. /*
  852.  * This routine will shutdown a serial port; interrupts are disabled, and
  853.  * DTR is dropped if the hangup on close termio flag is on.
  854.  */
  855. static void shutdown(struct async_struct * info, int do_free_irq)
  856. {
  857.     unsigned long flags;
  858.  
  859.     if (!(info->flags & ASYNC_INITIALIZED))
  860.         return;
  861.  
  862. #ifdef SERIAL_DEBUG_OPEN
  863.     printk("Shutting down serial port %d (irq %d)....", info->line,
  864.            info->irq);
  865. #endif
  866.     
  867.     save_flags(flags); cli(); /* Disable interrupts */
  868.     
  869.     /*
  870.      * First unlink the serial port from the IRQ chain...
  871.      */
  872.     if (info->next_port)
  873.         info->next_port->prev_port = info->prev_port;
  874.     if (info->prev_port)
  875.         info->prev_port->next_port = info->next_port;
  876.     else
  877.         IRQ_ports[info->irq] = info->next_port;
  878.     figure_IRQ_timeout(info->irq);
  879.     
  880.     /*
  881.      * Free the IRQ, if necessary
  882.      */
  883.     if (do_free_irq && info->irq && !IRQ_ports[info->irq])
  884.         free_irq(info->irq);
  885.     
  886.     info->IER = 0;
  887.     serial_outp(info, UART_IER, 0x00);    /* disable all intrs */
  888.     if (info->flags & ASYNC_FOURPORT) {
  889.         /* reset interrupts on the AST Fourport board */
  890.         (void) inb((info->port & 0xFE0) | 0x01F);
  891.     }
  892.     if (info->tty && !(info->tty->termios->c_cflag & HUPCL))
  893.         serial_outp(info, UART_MCR, UART_MCR_DTR);
  894.     else
  895.         /* reset DTR,RTS,OUT_2 */        
  896.         serial_outp(info, UART_MCR, 0x00);
  897.  
  898.     /* disable FIFO's */    
  899.     serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
  900.                      UART_FCR_CLEAR_XMIT));
  901.     (void)serial_in(info, UART_RX);    /* read data port to reset things */
  902.     
  903.     if (info->tty)
  904.         set_bit(TTY_IO_ERROR, &info->tty->flags);
  905.     
  906.     info->flags &= ~ASYNC_INITIALIZED;
  907.     restore_flags(flags);
  908. }
  909.  
  910. /*
  911.  * This routine is called to set the UART divisor registers to match
  912.  * the specified baud rate for a serial port.
  913.  */
  914. static void change_speed(unsigned int line)
  915. {
  916.     struct async_struct * info;
  917.     unsigned short port;
  918.     int    quot = 0;
  919.     unsigned cflag,cval,mcr,fcr;
  920.     int    i;
  921.  
  922.     if (line >= NR_PORTS)
  923.         return;
  924.     info = rs_table + line;
  925.     if (!info->tty || !info->tty->termios)
  926.         return;
  927.     cflag = info->tty->termios->c_cflag;
  928.     if (!(port = info->port))
  929.         return;
  930.     i = cflag & CBAUD;
  931.     if (i == 15) {
  932.         if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  933.             i += 1;
  934.         if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  935.             i += 2;
  936.         if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
  937.             quot = info->custom_divisor;
  938.     }
  939.     if (quot) {
  940.         info->timeout = ((info->xmit_fifo_size*HZ*15*quot) /
  941.                  info->baud_base) + 2;
  942.     } else if (baud_table[i] == 134) {
  943.         quot = (2*info->baud_base / 269);
  944.         info->timeout = (info->xmit_fifo_size*HZ*30/269) + 2;
  945.     } else if (baud_table[i]) {
  946.         quot = info->baud_base / baud_table[i];
  947.         info->timeout = (info->xmit_fifo_size*HZ*15/baud_table[i]) + 2;
  948.     } else {
  949.         quot = 0;
  950.         info->timeout = 0;
  951.     }
  952.     cli();
  953.     mcr = serial_in(info, UART_MCR);
  954.     if (quot) {
  955.         serial_out(info, UART_MCR, mcr | UART_MCR_DTR);
  956.     } else {
  957.         serial_out(info, UART_MCR, mcr & ~UART_MCR_DTR);
  958.         sti();
  959.         return;
  960.     }
  961.     sti();
  962.     /* byte size and parity */
  963.     cval = cflag & (CSIZE | CSTOPB);
  964.     cval >>= 4;
  965.     if (cflag & PARENB)
  966.         cval |= UART_LCR_PARITY;
  967.     if (!(cflag & PARODD))
  968.         cval |= UART_LCR_EPAR;
  969.     if (info->type == PORT_16550A) {
  970.         if ((info->baud_base / quot) < 2400)
  971.             fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
  972.         else
  973.             fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
  974.     } else
  975.         fcr = 0;
  976.     
  977.     cli();
  978.     serial_outp(info, UART_LCR, cval | UART_LCR_DLAB);    /* set DLAB */
  979.     serial_outp(info, UART_DLL, quot & 0xff);    /* LS of divisor */
  980.     serial_outp(info, UART_DLM, quot >> 8);        /* MS of divisor */
  981.     serial_outp(info, UART_LCR, cval);        /* reset DLAB */
  982.     serial_outp(info, UART_FCR, fcr);     /* set fcr */
  983.     sti();
  984. }
  985.  
  986. /*
  987.  * ------------------------------------------------------------
  988.  * rs_write() and friends
  989.  * ------------------------------------------------------------
  990.  */
  991.  
  992. /*
  993.  * This routine is used by rs_write to restart transmitter interrupts,
  994.  * which are disabled after we have a transmitter interrupt which went
  995.  * unacknowledged because we had run out of data to transmit.
  996.  * 
  997.  * Note: this subroutine must be called with the interrupts *off*
  998.  */
  999. static inline void restart_port(struct async_struct *info)
  1000. {
  1001.     struct tty_queue * queue;
  1002.     int head, tail, count;
  1003.     
  1004.     if (!info)
  1005.         return;
  1006.  
  1007.     if (serial_inp(info, UART_LSR) & UART_LSR_THRE) {
  1008.         if (info->x_char) {
  1009.             serial_outp(info, UART_TX, info->x_char);
  1010.             info->x_char = 0;
  1011.         } else {
  1012.             queue = &info->tty->write_q;
  1013.             head = queue->head;
  1014.             tail = queue->tail;
  1015.             count = info->xmit_fifo_size;
  1016.             while (count--) {
  1017.                 if (tail == head)
  1018.                     break;
  1019.                 serial_outp(info, UART_TX, queue->buf[tail++]);
  1020.                 tail &= TTY_BUF_SIZE-1;
  1021.             }
  1022.             queue->tail = tail;
  1023.         }
  1024.     }
  1025. }    
  1026.  
  1027. /*
  1028.  * This routine gets called when tty_write has put something into
  1029.  * the write_queue.  
  1030.  */
  1031. void rs_write(struct tty_struct * tty)
  1032. {
  1033.     struct async_struct *info;
  1034.  
  1035.     if (!tty || tty->stopped || tty->hw_stopped)
  1036.         return;
  1037.     info = rs_table + DEV_TO_SL(tty->line);
  1038.     cli();
  1039.     if (!info || !info->tty || !(info->flags & ASYNC_INITIALIZED)) {
  1040.         sti();
  1041.         return;
  1042.     }
  1043.     restart_port(info);
  1044.     info->IER = (UART_IER_MSI | UART_IER_RLSI |
  1045.              UART_IER_THRI | UART_IER_RDI);
  1046. #ifdef ISR_HACK
  1047.     serial_out(info, UART_IER, info->IER);
  1048. #endif
  1049.     sti();
  1050. }
  1051.  
  1052. /*
  1053.  * ------------------------------------------------------------
  1054.  * rs_throttle()
  1055.  * 
  1056.  * This routine is called by the upper-layer tty layer to signal that
  1057.  * incoming characters should be throttled (and that the throttle
  1058.  * should be released).
  1059.  * ------------------------------------------------------------
  1060.  */
  1061. static void rs_throttle(struct tty_struct * tty, int status)
  1062. {
  1063.     struct async_struct *info;
  1064.     unsigned char mcr;
  1065.     unsigned long flags;
  1066.  
  1067.     save_flags(flags); cli();
  1068. #if SERIAL_DEBUG_THROTTLE
  1069.     printk("throttle tty%d: %d (%d, %d)....\n", DEV_TO_SL(tty->line),
  1070.            status, LEFT(&tty->read_q), LEFT(&tty->secondary));
  1071. #endif
  1072.     switch (status) {
  1073.     case TTY_THROTTLE_RQ_FULL:
  1074.         info = rs_table + DEV_TO_SL(tty->line);
  1075.         if (I_IXOFF(tty)) {
  1076.             info->x_char = STOP_CHAR(tty);
  1077.         } else {
  1078.             mcr = serial_inp(info, UART_MCR);
  1079.             mcr &= ~UART_MCR_RTS;
  1080.             serial_out(info, UART_MCR, mcr);
  1081.         }
  1082.         break;
  1083.     case TTY_THROTTLE_RQ_AVAIL:
  1084.         info = rs_table + DEV_TO_SL(tty->line);
  1085.         if (I_IXOFF(tty)) {
  1086.             if (info->x_char)
  1087.                 info->x_char = 0;
  1088.             else
  1089.                 info->x_char = START_CHAR(tty);
  1090.         } else {
  1091.             mcr = serial_in(info, UART_MCR);
  1092.             mcr |= UART_MCR_RTS;
  1093.             serial_out(info, UART_MCR, mcr);
  1094.         }
  1095.         break;
  1096.     }
  1097.     restore_flags(flags);
  1098. }
  1099.  
  1100. /*
  1101.  * ------------------------------------------------------------
  1102.  * rs_ioctl() and friends
  1103.  * ------------------------------------------------------------
  1104.  */
  1105.  
  1106. static int get_serial_info(struct async_struct * info,
  1107.                struct serial_struct * retinfo)
  1108. {
  1109.     struct serial_struct tmp;
  1110.   
  1111.     if (!retinfo)
  1112.         return -EFAULT;
  1113.     memset(&tmp, 0, sizeof(tmp));
  1114.     tmp.type = info->type;
  1115.     tmp.line = info->line;
  1116.     tmp.port = info->port;
  1117.     tmp.irq = info->irq;
  1118.     tmp.flags = info->flags;
  1119.     tmp.baud_base = info->baud_base;
  1120.     tmp.close_delay = info->close_delay;
  1121.     tmp.custom_divisor = info->custom_divisor;
  1122.     tmp.hub6 = info->hub6;
  1123.     memcpy_tofs(retinfo,&tmp,sizeof(*retinfo));
  1124.     return 0;
  1125. }
  1126.  
  1127. static int set_serial_info(struct async_struct * info,
  1128.                struct serial_struct * new_info)
  1129. {
  1130.     struct serial_struct new_serial;
  1131.     struct async_struct old_info;
  1132.     unsigned int        i,change_irq,change_port;
  1133.     int             retval;
  1134.     struct             sigaction sa;
  1135.  
  1136.     if (!new_info)
  1137.         return -EFAULT;
  1138.     memcpy_fromfs(&new_serial,new_info,sizeof(new_serial));
  1139.     old_info = *info;
  1140.  
  1141.     change_irq = new_serial.irq != info->irq;
  1142.     change_port = (new_serial.port != info->port) || (new_serial.hub6 != info->hub6);
  1143.  
  1144.     if (!suser()) {
  1145.         if (change_irq || change_port ||
  1146.             (new_serial.baud_base != info->baud_base) ||
  1147.             (new_serial.type != info->type) ||
  1148.             (new_serial.close_delay != info->close_delay) ||
  1149.             ((new_serial.flags & ~ASYNC_USR_MASK) !=
  1150.              (info->flags & ~ASYNC_USR_MASK)))
  1151.             return -EPERM;
  1152.         info->flags = ((info->flags & ~ASYNC_USR_MASK) |
  1153.                    (new_serial.flags & ASYNC_USR_MASK));
  1154.         info->custom_divisor = new_serial.custom_divisor;
  1155.         goto check_and_exit;
  1156.     }
  1157.  
  1158.     if (new_serial.irq == 2)
  1159.         new_serial.irq = 9;
  1160.  
  1161.     if ((new_serial.irq > 15) || (new_serial.port > 0xffff) ||
  1162.         (new_serial.type < PORT_UNKNOWN) || (new_serial.type > PORT_MAX)) {
  1163.         return -EINVAL;
  1164.     }
  1165.  
  1166.     /* Make sure address is not already in use */
  1167.     for (i = 0 ; i < NR_PORTS; i++)
  1168.         if ((info != &rs_table[i]) &&
  1169.             (rs_table[i].port == new_serial.port) && rs_table[i].type)
  1170.             return -EADDRINUSE;
  1171.  
  1172.     /*
  1173.      * If necessary, first we try to grab the new IRQ for serial
  1174.      * interrupts.  (We have to do this early, since we may get an
  1175.      * error trying to do this.)
  1176.      */
  1177.     if (new_serial.port && new_serial.type && new_serial.irq &&
  1178.         (change_irq || !(info->flags & ASYNC_INITIALIZED))) {
  1179.         if (!IRQ_ports[new_serial.irq]) {
  1180.             sa.sa_handler = rs_interrupt;
  1181.             sa.sa_flags = (SA_INTERRUPT);
  1182.             sa.sa_mask = 0;
  1183.             sa.sa_restorer = NULL;
  1184.             retval = irqaction(new_serial.irq,&sa);
  1185.             if (retval)
  1186.                 return retval;
  1187.         }
  1188.     }
  1189.  
  1190.     if ((change_port || change_irq) && (info->count > 1))
  1191.         return -EBUSY;
  1192.  
  1193.     /*
  1194.      * OK, past this point, all the error checking has been done.
  1195.      * At this point, we start making changes.....
  1196.      */
  1197.  
  1198.     info->baud_base = new_serial.baud_base;
  1199.     info->flags = ((info->flags & ~ASYNC_FLAGS) |
  1200.             (new_serial.flags & ASYNC_FLAGS));
  1201.     info->custom_divisor = new_serial.custom_divisor;
  1202.     info->type = new_serial.type;
  1203.     info->close_delay = new_serial.close_delay;
  1204.  
  1205.     if (change_port || change_irq) {
  1206.         /*
  1207.          * We need to shutdown the serial port at the old
  1208.          * port/irq combination.
  1209.          */
  1210.         shutdown(info, change_irq);
  1211.         info->irq = new_serial.irq;
  1212.         info->port = new_serial.port;
  1213.         info->hub6 = new_serial.hub6;
  1214.     }
  1215.     
  1216. check_and_exit:
  1217.     if (!info->port || !info->type)
  1218.         return 0;
  1219.     if (info->flags & ASYNC_INITIALIZED) {
  1220.         if (((old_info.flags & ASYNC_SPD_MASK) !=
  1221.              (info->flags & ASYNC_SPD_MASK)) ||
  1222.             (old_info.custom_divisor != info->custom_divisor))
  1223.             change_speed(info->line);
  1224.     } else
  1225.         (void) startup(info, 0);
  1226.     return 0;
  1227. }
  1228.  
  1229. static int get_modem_info(struct async_struct * info, unsigned int *value)
  1230. {
  1231.     unsigned char control, status;
  1232.     unsigned int result;
  1233.  
  1234.     cli();
  1235.     control = serial_in(info, UART_MCR);
  1236.     status = serial_in(info, UART_MSR);
  1237.     sti();
  1238.     result =  ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
  1239.         | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
  1240.         | ((status  & UART_MSR_DCD) ? TIOCM_CAR : 0)
  1241.         | ((status  & UART_MSR_RI) ? TIOCM_RNG : 0)
  1242.         | ((status  & UART_MSR_DSR) ? TIOCM_DSR : 0)
  1243.         | ((status  & UART_MSR_CTS) ? TIOCM_CTS : 0);
  1244.     put_fs_long(result,(unsigned long *) value);
  1245.     return 0;
  1246. }
  1247.  
  1248. static int set_modem_info(struct async_struct * info, unsigned int cmd,
  1249.               unsigned int *value)
  1250. {
  1251.     unsigned char control;
  1252.     unsigned int arg = get_fs_long((unsigned long *) value);
  1253.     
  1254.     cli();
  1255.     control = serial_in(info, UART_MCR);
  1256.     sti();
  1257.  
  1258.     switch (cmd) {
  1259.         case TIOCMBIS:
  1260.             if (arg & TIOCM_RTS)
  1261.                 control |= UART_MCR_RTS;
  1262.             if (arg & TIOCM_DTR)
  1263.                 control |= UART_MCR_DTR;
  1264.             break;
  1265.         case TIOCMBIC:
  1266.             if (arg & TIOCM_RTS)
  1267.                 control &= ~UART_MCR_RTS;
  1268.             if (arg & TIOCM_DTR)
  1269.                 control &= ~UART_MCR_DTR;
  1270.             break;
  1271.         case TIOCMSET:
  1272.             control = (control & ~(UART_MCR_RTS | UART_MCR_DTR))
  1273.                 | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
  1274.                 | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0);
  1275.             break;
  1276.         default:
  1277.             return -EINVAL;
  1278.     }
  1279.     cli();
  1280.     serial_out(info, UART_MCR, control);
  1281.     sti();
  1282.     return 0;
  1283. }
  1284.  
  1285. static int do_autoconfig(struct async_struct * info)
  1286. {
  1287.     int            retval;
  1288.     
  1289.     if (!suser())
  1290.         return -EPERM;
  1291.     
  1292.     if (info->count > 1)
  1293.         return -EBUSY;
  1294.     
  1295.     shutdown(info, 1);
  1296.  
  1297.     cli();
  1298.     autoconfig(info);
  1299.     sti();
  1300.  
  1301.     retval = startup(info, 1);
  1302.     if (retval)
  1303.         return retval;
  1304.     return 0;
  1305. }
  1306.  
  1307.  
  1308. /*
  1309.  * This routine sends a break character out the serial port.
  1310.  */
  1311. static void send_break(    struct async_struct * info, int duration)
  1312. {
  1313.     if (!info->port)
  1314.         return;
  1315.     current->state = TASK_INTERRUPTIBLE;
  1316.     current->timeout = jiffies + duration;
  1317.     cli();
  1318.     serial_out(info, UART_LCR, serial_inp(info, UART_LCR) | UART_LCR_SBC);
  1319.     schedule();
  1320.     serial_out(info, UART_LCR, serial_inp(info, UART_LCR) & ~UART_LCR_SBC);
  1321.     sti();
  1322. }
  1323.  
  1324. /*
  1325.  * This routine returns a bitfield of "wild interrupts".  Basically,
  1326.  * any unclaimed interrupts which is flapping around.
  1327.  */
  1328. static int check_wild_interrupts(int doprint)
  1329. {
  1330.     int    i, mask;
  1331.     int    wild_interrupts = 0;
  1332.     int    irq_lines;
  1333.     unsigned long timeout;
  1334.     unsigned long flags;
  1335.     
  1336.     /* Turn on interrupts (they may be off) */
  1337.     save_flags(flags); sti();
  1338.  
  1339.     irq_lines = grab_all_interrupts(0);
  1340.     
  1341.     /*
  1342.      * Delay for 0.1 seconds -- we use a busy loop since this may 
  1343.      * occur during the bootup sequence
  1344.      */
  1345.     timeout = jiffies+10;
  1346.     while (timeout >= jiffies)
  1347.         ;
  1348.     
  1349.     rs_triggered = 0;    /* Reset after letting things settle */
  1350.  
  1351.     timeout = jiffies+10;
  1352.     while (timeout >= jiffies)
  1353.         ;
  1354.     
  1355.     for (i = 0, mask = 1; i < 16; i++, mask <<= 1) {
  1356.         if ((rs_triggered & (1 << i)) &&
  1357.             (irq_lines & (1 << i))) {
  1358.             wild_interrupts |= mask;
  1359.             if (doprint)
  1360.                 printk("Wild interrupt?  (IRQ %d)\n", i);
  1361.         }
  1362.     }
  1363.     free_all_interrupts(irq_lines);
  1364.     restore_flags(flags);
  1365.     return wild_interrupts;
  1366. }
  1367.  
  1368. static int rs_ioctl(struct tty_struct *tty, struct file * file,
  1369.             unsigned int cmd, unsigned long arg)
  1370. {
  1371.     int error, line;
  1372.     struct async_struct * info;
  1373.  
  1374.     line = DEV_TO_SL(tty->line);
  1375.     if (line < 0 || line >= NR_PORTS)
  1376.         return -ENODEV;
  1377.     info = rs_table + line;
  1378.     
  1379.     switch (cmd) {
  1380.         case TCSBRK:    /* SVID version: non-zero arg --> no break */
  1381.             if (!arg)
  1382.                 send_break(info, HZ/4);    /* 1/4 second */
  1383.             return 0;
  1384.         case TCSBRKP:    /* support for POSIX tcsendbreak() */
  1385.             send_break(info, arg ? arg*(HZ/10) : HZ/4);
  1386.             return 0;
  1387.         case TIOCGSOFTCAR:
  1388.             error = verify_area(VERIFY_WRITE, (void *) arg,sizeof(long));
  1389.             if (error)
  1390.                 return error;
  1391.             put_fs_long(C_CLOCAL(tty) ? 1 : 0,
  1392.                     (unsigned long *) arg);
  1393.             return 0;
  1394.         case TIOCSSOFTCAR:
  1395.             arg = get_fs_long((unsigned long *) arg);
  1396.             tty->termios->c_cflag =
  1397.                 ((tty->termios->c_cflag & ~CLOCAL) |
  1398.                  (arg ? CLOCAL : 0));
  1399.             return 0;
  1400.         case TIOCMGET:
  1401.             error = verify_area(VERIFY_WRITE, (void *) arg,
  1402.                 sizeof(unsigned int));
  1403.             if (error)
  1404.                 return error;
  1405.             return get_modem_info(info, (unsigned int *) arg);
  1406.         case TIOCMBIS:
  1407.         case TIOCMBIC:
  1408.         case TIOCMSET:
  1409.             return set_modem_info(info, cmd, (unsigned int *) arg);
  1410.         case TIOCGSERIAL:
  1411.             error = verify_area(VERIFY_WRITE, (void *) arg,
  1412.                         sizeof(struct serial_struct));
  1413.             if (error)
  1414.                 return error;
  1415.             return get_serial_info(info,
  1416.                            (struct serial_struct *) arg);
  1417.         case TIOCSSERIAL:
  1418.             return set_serial_info(info,
  1419.                            (struct serial_struct *) arg);
  1420.         case TIOCSERCONFIG:
  1421.             return do_autoconfig(info);
  1422.  
  1423.         case TIOCSERGWILD:
  1424.             error = verify_area(VERIFY_WRITE, (void *) arg,
  1425.                         sizeof(int));
  1426.             if (error)
  1427.                 return error;
  1428.             put_fs_long(rs_wild_int_mask, (unsigned long *) arg);
  1429.             return 0;
  1430.  
  1431.         case TIOCSERSWILD:
  1432.             if (!suser())
  1433.                 return -EPERM;
  1434.             rs_wild_int_mask = get_fs_long((unsigned long *) arg);
  1435.             if (rs_wild_int_mask < 0)
  1436.                 rs_wild_int_mask = check_wild_interrupts(0);
  1437.             return 0;
  1438.  
  1439.         default:
  1440.             return -EINVAL;
  1441.         }
  1442.     return 0;
  1443. }
  1444.  
  1445. static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
  1446. {
  1447.     struct async_struct *info;
  1448.  
  1449.     if (tty->termios->c_cflag == old_termios->c_cflag)
  1450.         return;
  1451.  
  1452.     info = &rs_table[DEV_TO_SL(tty->line)];
  1453.  
  1454.     change_speed(DEV_TO_SL(tty->line));
  1455.     
  1456.     if ((old_termios->c_cflag & CRTSCTS) &&
  1457.         !(tty->termios->c_cflag & CRTSCTS)) {
  1458.         tty->hw_stopped = 0;
  1459.         rs_write(tty);
  1460.     }
  1461.  
  1462.     if (!(old_termios->c_cflag & CLOCAL) &&
  1463.         (tty->termios->c_cflag & CLOCAL))
  1464.         wake_up_interruptible(&info->open_wait);
  1465.  
  1466.     if (I_INPCK(tty))
  1467.         info->read_status_mask = (UART_LSR_OE | UART_LSR_BI |
  1468.                       UART_LSR_FE | UART_LSR_PE);
  1469.     else
  1470.         info->read_status_mask = (UART_LSR_OE | UART_LSR_BI |
  1471.                       UART_LSR_FE);
  1472. }
  1473.  
  1474. /*
  1475.  * ------------------------------------------------------------
  1476.  * rs_close()
  1477.  * 
  1478.  * This routine is called when the serial port gets closed.  First, we
  1479.  * wait for the last remaining data to be sent.  Then, we unlink its
  1480.  * async structure from the interrupt chain if necessary, and we free
  1481.  * that IRQ if nothing is left in the chain.
  1482.  * ------------------------------------------------------------
  1483.  */
  1484. static void rs_close(struct tty_struct *tty, struct file * filp)
  1485. {
  1486.     struct async_struct * info;
  1487.     int line;
  1488.  
  1489.     if (tty_hung_up_p(filp))
  1490.         return;
  1491.     
  1492.     line = DEV_TO_SL(tty->line);
  1493.     if ((line < 0) || (line >= NR_PORTS))
  1494.         return;
  1495.     info = rs_table + line;
  1496. #ifdef SERIAL_DEBUG_OPEN
  1497.     printk("rs_close ttys%d, count = %d\n", info->line, info->count);
  1498. #endif
  1499.     if ((tty->count == 1) && (info->count != 1)) {
  1500.         /*
  1501.          * Uh, oh.  tty->count is 1, which means that the tty
  1502.          * structure will be freed.  Info->count should always
  1503.          * be one in these conditions.  If it's greater than
  1504.          * one, we've got real problems, since it means the
  1505.          * serial port won't be shutdown.
  1506.          */
  1507.         printk("rs_close: bad serial port count; tty->count is 1, "
  1508.                "info->count is %d\n", info->count);
  1509.         info->count = 1;
  1510.     }
  1511.     if (--info->count < 0) {
  1512.         printk("rs_close: bad serial port count for ttys%d: %d\n",
  1513.                info->line, info->count);
  1514.         info->count = 0;
  1515.     }
  1516.     if (info->count)
  1517.         return;
  1518.     info->flags |= ASYNC_CLOSING;
  1519.     /*
  1520.      * Save the termios structure, since this port may have
  1521.      * separate termios for callout and dialin.
  1522.      */
  1523.     if (info->flags & ASYNC_NORMAL_ACTIVE)
  1524.         info->normal_termios = *tty->termios;
  1525.     if (info->flags & ASYNC_CALLOUT_ACTIVE)
  1526.         info->callout_termios = *tty->termios;
  1527.     tty->stopped = 0;        /* Force flush to succeed */
  1528.     tty->hw_stopped = 0;
  1529.     if (info->flags & ASYNC_INITIALIZED) {
  1530.         rs_start(tty);
  1531.         wait_until_sent(tty, 6000); /* 60 seconds timeout */
  1532.     } else
  1533.         flush_output(tty);
  1534.     flush_input(tty);
  1535.     cli();
  1536.     /*
  1537.      * Make sure the UART transmitter has completely drained; this
  1538.      * is especially important if there is a transmit FIFO!
  1539.      */
  1540.     if (!(serial_inp(info, UART_LSR) & UART_LSR_THRE)) {
  1541.         rs_start(tty);    /* Make sure THRI interrupt enabled */
  1542.         interruptible_sleep_on(&info->xmit_wait);
  1543.     }
  1544.     sti();
  1545.     shutdown(info, 1);
  1546.     clear_bit(line, rs_event);
  1547.     info->event = 0;
  1548.     info->tty = 0;
  1549.     if (info->blocked_open) {
  1550.         if (info->close_delay) {
  1551.             tty->count++; /* avoid race condition */
  1552.             current->state = TASK_INTERRUPTIBLE;
  1553.             current->timeout = jiffies + info->close_delay;
  1554.             schedule();
  1555.             tty->count--;
  1556.         }
  1557.         wake_up_interruptible(&info->open_wait);
  1558.     }
  1559.     info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
  1560.              ASYNC_CLOSING);
  1561.     wake_up_interruptible(&info->close_wait);
  1562. }
  1563.  
  1564. /*
  1565.  * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
  1566.  */
  1567. void rs_hangup(struct tty_struct *tty)
  1568. {
  1569.     struct async_struct * info;
  1570.     int line;
  1571.  
  1572.     line = DEV_TO_SL(tty->line);
  1573.     if ((line < 0) || (line >= NR_PORTS))
  1574.         return;
  1575.     info = rs_table + line;
  1576.     
  1577.     shutdown(info, 1);
  1578.     clear_bit(line, rs_event);
  1579.     info->event = 0;
  1580.     info->count = 0;
  1581.     info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
  1582.     info->tty = 0;
  1583.     wake_up_interruptible(&info->open_wait);
  1584. }
  1585.  
  1586. /*
  1587.  * ------------------------------------------------------------
  1588.  * rs_open() and friends
  1589.  * ------------------------------------------------------------
  1590.  */
  1591. static int block_til_ready(struct tty_struct *tty, struct file * filp,
  1592.                struct async_struct *info)
  1593. {
  1594.     struct wait_queue wait = { current, NULL };
  1595.     int        retval;
  1596.     int        do_clocal = C_CLOCAL(tty);
  1597.  
  1598.     /*
  1599.      * If the device is in the middle of being closed, then block
  1600.      * until it's done, and then try again.
  1601.      */
  1602.     if (info->flags & ASYNC_CLOSING) {
  1603.         interruptible_sleep_on(&info->close_wait);
  1604. #ifdef SERIAL_DO_RESTART
  1605.         if (info->flags & ASYNC_HUP_NOTIFY)
  1606.             return -EAGAIN;
  1607.         else
  1608.             return -ERESTARTSYS;
  1609. #else
  1610.         return -EAGAIN;
  1611. #endif
  1612.     }
  1613.  
  1614.     /*
  1615.      * If this is a callout device, then just make sure the normal
  1616.      * device isn't being used.
  1617.      */
  1618.     if (MAJOR(filp->f_rdev) == TTYAUX_MAJOR) {
  1619.         if (info->flags & ASYNC_NORMAL_ACTIVE)
  1620.             return -EBUSY;
  1621.         if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
  1622.             (info->flags & ASYNC_SESSION_LOCKOUT) &&
  1623.             (info->session != current->session))
  1624.             return -EBUSY;
  1625.         if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
  1626.             (info->flags & ASYNC_PGRP_LOCKOUT) &&
  1627.             (info->pgrp != current->pgrp))
  1628.             return -EBUSY;
  1629.         info->flags |= ASYNC_CALLOUT_ACTIVE;
  1630.         return 0;
  1631.     }
  1632.     
  1633.     /*
  1634.      * If non-blocking mode is set, then make the check up front
  1635.      * and then exit.
  1636.      */
  1637.     if (filp->f_flags & O_NONBLOCK) {
  1638.         if (info->flags & ASYNC_CALLOUT_ACTIVE)
  1639.             return -EBUSY;
  1640.         info->flags |= ASYNC_NORMAL_ACTIVE;
  1641.         return 0;
  1642.     }
  1643.  
  1644.     /*
  1645.      * Block waiting for the carrier detect and the line to become
  1646.      * free (i.e., not in use by the callout).  While we are in
  1647.      * this loop, info->count is dropped by one, so that
  1648.      * rs_close() knows when to free things.  We restore it upon
  1649.      * exit, either normal or abnormal.
  1650.      */
  1651.     retval = 0;
  1652.     add_wait_queue(&info->open_wait, &wait);
  1653. #ifdef SERIAL_DEBUG_OPEN
  1654.     printk("block_til_ready before block: ttys%d, count = %d\n",
  1655.            info->line, info->count);
  1656. #endif
  1657.     info->count--;
  1658.     info->blocked_open++;
  1659.     while (1) {
  1660.         cli();
  1661.         if (!(info->flags & ASYNC_CALLOUT_ACTIVE))
  1662.             serial_out(info, UART_MCR,
  1663.                    serial_inp(info, UART_MCR) |
  1664.                    (UART_MCR_DTR | UART_MCR_RTS));
  1665.         sti();
  1666.         current->state = TASK_INTERRUPTIBLE;
  1667.         if (tty_hung_up_p(filp) ||
  1668.             !(info->flags & ASYNC_INITIALIZED)) {
  1669. #ifdef SERIAL_DO_RESTART
  1670.             if (info->flags & ASYNC_HUP_NOTIFY)
  1671.                 retval = -EAGAIN;
  1672.             else
  1673.                 retval = -ERESTARTSYS;
  1674. #else
  1675.             retval = -EAGAIN;
  1676. #endif
  1677.             break;
  1678.         }
  1679.         if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
  1680.             !(info->flags & ASYNC_CLOSING) &&
  1681.             (do_clocal || (serial_in(info, UART_MSR) &
  1682.                    UART_MSR_DCD)))
  1683.             break;
  1684.         if (current->signal & ~current->blocked) {
  1685.             retval = -ERESTARTSYS;
  1686.             break;
  1687.         }
  1688. #ifdef SERIAL_DEBUG_OPEN
  1689.         printk("block_til_ready blocking: ttys%d, count = %d\n",
  1690.                info->line, info->count);
  1691. #endif
  1692.         schedule();
  1693.     }
  1694.     current->state = TASK_RUNNING;
  1695.     remove_wait_queue(&info->open_wait, &wait);
  1696.     if (!tty_hung_up_p(filp))
  1697.         info->count++;
  1698.     info->blocked_open--;
  1699. #ifdef SERIAL_DEBUG_OPEN
  1700.     printk("block_til_ready after blocking: ttys%d, count = %d\n",
  1701.            info->line, info->count);
  1702. #endif
  1703.     if (retval)
  1704.         return retval;
  1705.     info->flags |= ASYNC_NORMAL_ACTIVE;
  1706.     return 0;
  1707. }    
  1708.  
  1709. /*
  1710.  * This routine is called whenever a serial port is opened.  It
  1711.  * enables interrupts for a serial port, linking in its async structure into
  1712.  * the IRQ chain.   It also performs the serial-speicific
  1713.  * initalization for the tty structure.
  1714.  */
  1715. int rs_open(struct tty_struct *tty, struct file * filp)
  1716. {
  1717.     struct async_struct    *info;
  1718.     int             retval, line;
  1719.  
  1720.     line = DEV_TO_SL(tty->line);
  1721.     if ((line < 0) || (line >= NR_PORTS))
  1722.         return -ENODEV;
  1723.     info = rs_table + line;
  1724. #ifdef SERIAL_DEBUG_OPEN
  1725.     printk("rs_open ttys%d, count = %d\n", info->line, info->count);
  1726. #endif
  1727.     info->count++;
  1728.     info->tty = tty;
  1729.     
  1730.     tty->write = rs_write;
  1731.     tty->close = rs_close;
  1732.     tty->ioctl = rs_ioctl;
  1733.     tty->throttle = rs_throttle;
  1734.     tty->set_termios = rs_set_termios;
  1735.     tty->stop = rs_stop;
  1736.     tty->start = rs_start;
  1737.     tty->hangup = rs_hangup;
  1738.     if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
  1739.         if (MAJOR(filp->f_rdev) == TTY_MAJOR)
  1740.             *tty->termios = info->normal_termios;
  1741.         else 
  1742.             *tty->termios = info->callout_termios;
  1743.     }
  1744.     /*
  1745.      * Start up serial port
  1746.      */
  1747.     retval = startup(info, 1);
  1748.     if (retval)
  1749.         return retval;
  1750.  
  1751.     retval = block_til_ready(tty, filp, info);
  1752.     if (retval) {
  1753. #ifdef SERIAL_DEBUG_OPEN
  1754.         printk("rs_open returning after block_til_ready with %d\n",
  1755.                retval);
  1756. #endif
  1757.         return retval;
  1758.     }
  1759.  
  1760.     info->session = current->session;
  1761.     info->pgrp = current->pgrp;
  1762.  
  1763. #ifdef SERIAL_DEBUG_OPEN
  1764.     printk("rs_open ttys%d successful...", info->line);
  1765. #endif
  1766.     return 0;
  1767. }
  1768.  
  1769. /*
  1770.  * ---------------------------------------------------------------------
  1771.  * rs_init() and friends
  1772.  *
  1773.  * rs_init() is called at boot-time to initialize the serial driver.
  1774.  * ---------------------------------------------------------------------
  1775.  */
  1776.  
  1777. /*
  1778.  * This routine prints out the appropriate serial driver version
  1779.  * number, and identifies which options were configured into this
  1780.  * driver.
  1781.  */
  1782. static void show_serial_version(void)
  1783. {
  1784.     printk("Serial driver version 3.99a with");
  1785. #ifdef CONFIG_AST_FOURPORT
  1786.     printk(" AST_FOURPORT");
  1787. #define SERIAL_OPT
  1788. #endif
  1789. #ifdef CONFIG_ACCENT_ASYNC
  1790.     printk(" ACCENT_ASYNC");
  1791. #define SERIAL_OPT
  1792. #endif
  1793. #ifdef CONFIG_HUB6
  1794.     printk(" HUB-6");
  1795. #define SERIAL_OPT
  1796. #endif
  1797. #ifdef CONFIG_AUTO_IRQ
  1798.     printk (" AUTO_IRQ");
  1799. #define SERIAL_OPT
  1800. #endif
  1801. #ifdef SERIAL_OPT
  1802.     printk(" enabled\n");
  1803. #else
  1804.     printk(" no serial options enabled\n");
  1805. #endif
  1806. #undef SERIAL_OPT
  1807. }
  1808.  
  1809. /*
  1810.  * This routine is called by do_auto_irq(); it attempts to determine
  1811.  * which interrupt a serial port is configured to use.  It is not
  1812.  * fool-proof, but it works a large part of the time.
  1813.  */
  1814. static int get_auto_irq(struct async_struct *info)
  1815. {
  1816.     unsigned char save_MCR, save_IER, save_ICP=0;
  1817.     unsigned short ICP=0, port = info->port;
  1818.     unsigned long timeout;
  1819.     
  1820.     /*
  1821.      * Enable interrupts and see who answers
  1822.      */
  1823.     rs_irq_triggered = 0;
  1824.     cli();
  1825.     save_IER = serial_inp(info, UART_IER);
  1826.     save_MCR = serial_inp(info, UART_MCR);
  1827.     if (info->flags & ASYNC_FOURPORT)  {
  1828.         serial_outp(info, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
  1829.         serial_outp(info, UART_IER, 0x0f);    /* enable all intrs */
  1830.         ICP = (port & 0xFE0) | 0x01F;
  1831.         save_ICP = inb_p(ICP);
  1832.         outb_p(0x80, ICP);
  1833.         (void) inb_p(ICP);
  1834.     } else {
  1835.         serial_outp(info, UART_MCR,
  1836.                 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
  1837.         serial_outp(info, UART_IER, 0x0f);    /* enable all intrs */
  1838.     }
  1839.     sti();
  1840.     /*
  1841.      * Next, clear the interrupt registers.
  1842.      */
  1843.     (void)serial_inp(info, UART_LSR);
  1844.     (void)serial_inp(info, UART_RX);
  1845.     (void)serial_inp(info, UART_IIR);
  1846.     (void)serial_inp(info, UART_MSR);
  1847.     
  1848.     timeout = jiffies+2;
  1849.     while (timeout >= jiffies) {
  1850.         if (rs_irq_triggered)
  1851.             break;
  1852.     }
  1853.     /*
  1854.      * Now check to see if we got any business, and clean up.
  1855.      */
  1856.     cli();
  1857.     serial_outp(info, UART_IER, save_IER);
  1858.     serial_outp(info, UART_MCR, save_MCR);
  1859.     if (info->flags & ASYNC_FOURPORT)
  1860.         outb_p(save_ICP, ICP);
  1861.     sti();
  1862.     return(rs_irq_triggered);
  1863. }
  1864.  
  1865. /*
  1866.  * Calls get_auto_irq() multiple times, to make sure we don't get
  1867.  * faked out by random interrupts
  1868.  */
  1869. static int do_auto_irq(struct async_struct * info)
  1870. {
  1871.     unsigned         port = info->port;
  1872.     int             irq_lines = 0;
  1873.     int            irq_try_1 = 0, irq_try_2 = 0;
  1874.     int            retries;
  1875.     unsigned long flags;
  1876.  
  1877.     if (!port)
  1878.         return 0;
  1879.  
  1880.     /* Turn on interrupts (they may be off) */
  1881.     save_flags(flags); sti();
  1882.  
  1883.     irq_lines = grab_all_interrupts(rs_wild_int_mask);
  1884.     
  1885.     for (retries = 0; retries < 5; retries++) {
  1886.         if (!irq_try_1)
  1887.             irq_try_1 = get_auto_irq(info);
  1888.         if (!irq_try_2)
  1889.             irq_try_2 = get_auto_irq(info);
  1890.         if (irq_try_1 && irq_try_2) {
  1891.             if (irq_try_1 == irq_try_2)
  1892.                 break;
  1893.             irq_try_1 = irq_try_2 = 0;
  1894.         }
  1895.     }
  1896.     restore_flags(flags);
  1897.     free_all_interrupts(irq_lines);
  1898.     return (irq_try_1 == irq_try_2) ? irq_try_1 : 0;
  1899. }
  1900.  
  1901. /*
  1902.  * This routine is called by rs_init() to initialize a specific serial
  1903.  * port.  It determines what type of UART ship this serial port is
  1904.  * using: 8250, 16450, 16550, 16550A.  The important question is
  1905.  * whether or not this UART is a 16550A or not, since this will
  1906.  * determine whether or not we can use its FIFO features or not.
  1907.  */
  1908. static void autoconfig(struct async_struct * info)
  1909. {
  1910.     unsigned char status1, status2, scratch, scratch2;
  1911.     unsigned port = info->port;
  1912.     unsigned long flags;
  1913.  
  1914.     info->type = PORT_UNKNOWN;
  1915.     
  1916.     if (!port)
  1917.         return;
  1918.  
  1919.     save_flags(flags); cli();
  1920.     
  1921.     /*
  1922.      * Do a simple existence test first; if we fail this, there's
  1923.      * no point trying anything else.
  1924.      */
  1925.     scratch = serial_inp(info, UART_IER);
  1926.     serial_outp(info, UART_IER, 0);
  1927.     scratch2 = serial_inp(info, UART_IER);
  1928.     serial_outp(info, UART_IER, scratch);
  1929.     if (scratch2) {
  1930.         restore_flags(flags);
  1931.         return;        /* We failed; there's nothing here */
  1932.     }
  1933.  
  1934.     /* 
  1935.      * Check to see if a UART is really there.  Certain broken
  1936.      * internal modems based on the Rockwell chipset fail this
  1937.      * test, because they apparently don't implement the loopback
  1938.      * test mode.  So this test is skipped on the COM 1 through
  1939.      * COM 4 ports.  This *should* be safe, since no board
  1940.      * manufactucturer would be stupid enough to design a board
  1941.      * that conflicts with COM 1-4 --- we hope!
  1942.      */
  1943.     if (!(info->flags & ASYNC_SKIP_TEST)) {
  1944.         scratch = serial_inp(info, UART_MCR);
  1945.         serial_outp(info, UART_MCR, UART_MCR_LOOP | scratch);
  1946.         scratch2 = serial_inp(info, UART_MSR);
  1947.         serial_outp(info, UART_MCR, UART_MCR_LOOP | 0x0A);
  1948.         status1 = serial_inp(info, UART_MSR) & 0xF0;
  1949.         serial_outp(info, UART_MCR, scratch);
  1950.         serial_outp(info, UART_MSR, scratch2);
  1951.         if (status1 != 0x90) {
  1952.             restore_flags(flags);
  1953.             return;
  1954.         }
  1955.     } 
  1956.     
  1957.     /*
  1958.      * If the AUTO_IRQ flag is set, try to do the automatic IRQ
  1959.      * detection.
  1960.      */
  1961.     if (info->flags & ASYNC_AUTO_IRQ)
  1962.         info->irq = do_auto_irq(info);
  1963.         
  1964.     serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
  1965.     scratch = serial_in(info, UART_IIR) >> 6;
  1966.     info->xmit_fifo_size = 1;
  1967.     switch (scratch) {
  1968.         case 0:
  1969.             info->type = PORT_16450;
  1970.             break;
  1971.         case 1:
  1972.             info->type = PORT_UNKNOWN;
  1973.             break;
  1974.         case 2:
  1975.             info->type = PORT_16550;
  1976.             break;
  1977.         case 3:
  1978.             info->type = PORT_16550A;
  1979.             info->xmit_fifo_size = 16;
  1980.             break;
  1981.     }
  1982.     if (info->type == PORT_16450) {
  1983.         scratch = serial_in(info, UART_SCR);
  1984.         serial_outp(info, UART_SCR, 0xa5);
  1985.         status1 = serial_in(info, UART_SCR);
  1986.         serial_outp(info, UART_SCR, 0x5a);
  1987.         status2 = serial_in(info, UART_SCR);
  1988.         serial_outp(info, UART_SCR, scratch);
  1989.  
  1990.         if ((status1 != 0xa5) || (status2 != 0x5a))
  1991.             info->type = PORT_8250;
  1992.     }
  1993.  
  1994.     /*
  1995.      * Reset the UART.
  1996.      */
  1997.     serial_outp(info, UART_MCR, 0x00);
  1998.     serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
  1999.                      UART_FCR_CLEAR_XMIT));
  2000.     (void)serial_in(info, UART_RX);
  2001.     
  2002.     restore_flags(flags);
  2003. }
  2004.  
  2005. /*
  2006.  * The serial driver boot-time initialization code!
  2007.  */
  2008. long rs_init(long kmem_start)
  2009. {
  2010.     int i;
  2011.     struct async_struct * info;
  2012.     
  2013.     memset(&rs_event, 0, sizeof(rs_event));
  2014.     bh_base[SERIAL_BH].routine = do_softint;
  2015.     timer_table[RS_TIMER].fn = rs_timer;
  2016.     timer_table[RS_TIMER].expires = 0;
  2017.     IRQ_active = 0;
  2018. #ifdef CONFIG_AUTO_IRQ
  2019.     rs_wild_int_mask = check_wild_interrupts(1);
  2020. #endif
  2021.  
  2022.     for (i = 0; i < 16; i++) {
  2023.         IRQ_ports[i] = 0;
  2024.         IRQ_timeout[i] = 0;
  2025.     }
  2026.     
  2027.     show_serial_version();
  2028.     for (i = 0, info = rs_table; i < NR_PORTS; i++,info++) {
  2029.         info->line = i;
  2030.         info->tty = 0;
  2031.         info->type = PORT_UNKNOWN;
  2032.         info->custom_divisor = 0;
  2033.         info->close_delay = 50;
  2034.         info->x_char = 0;
  2035.         info->event = 0;
  2036.         info->count = 0;
  2037.         info->blocked_open = 0;
  2038.         memset(&info->callout_termios, 0, sizeof(struct termios));
  2039.         memset(&info->normal_termios, 0, sizeof(struct termios));
  2040.         info->open_wait = 0;
  2041.         info->xmit_wait = 0;
  2042.         info->close_wait = 0;
  2043.         info->next_port = 0;
  2044.         info->prev_port = 0;
  2045.         if (info->irq == 2)
  2046.             info->irq = 9;
  2047.         if (!(info->flags & ASYNC_BOOT_AUTOCONF))
  2048.             continue;
  2049.         autoconfig(info);
  2050.         if (info->type == PORT_UNKNOWN)
  2051.             continue;
  2052.         printk("tty%02d%s at 0x%04x (irq = %d)", info->line, 
  2053.                (info->flags & ASYNC_FOURPORT) ? " FourPort" : "",
  2054.                info->port, info->irq);
  2055.         switch (info->type) {
  2056.             case PORT_8250:
  2057.                 printk(" is a 8250\n");
  2058.                 break;
  2059.             case PORT_16450:
  2060.                 printk(" is a 16450\n");
  2061.                 break;
  2062.             case PORT_16550:
  2063.                 printk(" is a 16550\n");
  2064.                 break;
  2065.             case PORT_16550A:
  2066.                 printk(" is a 16550A\n");
  2067.                 break;
  2068.             default:
  2069.                 printk("\n");
  2070.                 break;
  2071.         }
  2072.     }
  2073.     return kmem_start;
  2074. }
  2075.  
  2076.