cc4a327b6d99274648784601eb4f3c47582ffe2a
[linux-flexiantxendom0-3.2.10.git] / arch / mips / au1000 / common / serial.c
1 /*
2  *
3  * BRIEF MODULE DESCRIPTION
4  *      Au1000 serial port driver.
5  *
6  * Copyright 2001 MontaVista Software Inc.
7  * Author: MontaVista Software, Inc.
8  *              ppopov@mvista.com or source@mvista.com
9  *
10  *  Derived almost entirely from drivers/char/serial.c:
11  *
12  *  Copyright (C) 1991, 1992  Linus Torvalds
13  *  Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 
14  *              1998, 1999  Theodore Ts'o
15  *
16  *  This program is free software; you can redistribute  it and/or modify it
17  *  under  the terms of  the GNU General  Public License as published by the
18  *  Free Software Foundation;  either version 2 of the  License, or (at your
19  *  option) any later version.
20  *
21  *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
22  *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
23  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
24  *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
25  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
27  *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28  *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
29  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  *  You should have received a copy of the  GNU General Public License along
33  *  with this program; if not, write  to the Free Software Foundation, Inc.,
34  *  675 Mass Ave, Cambridge, MA 02139, USA.
35  */
36
37 static char *serial_version = "1.01";
38 static char *serial_revdate = "2001-02-08";
39
40
41 #include <linux/config.h>
42 #include <linux/version.h>
43
44 #undef SERIAL_PARANOIA_CHECK
45 #define CONFIG_SERIAL_NOPAUSE_IO
46 #define SERIAL_DO_RESTART
47
48
49 /* Set of debugging defines */
50
51 #undef SERIAL_DEBUG_INTR
52 #undef SERIAL_DEBUG_OPEN
53 #undef SERIAL_DEBUG_FLOW
54 #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
55 #undef SERIAL_DEBUG_PCI
56 #undef SERIAL_DEBUG_AUTOCONF
57
58 #ifdef MODULE
59 #undef CONFIG_AU1000_SERIAL_CONSOLE
60 #endif
61
62 #define CONFIG_SERIAL_RSA
63
64 #define RS_STROBE_TIME (10*HZ)
65 #define RS_ISR_PASS_LIMIT 256
66   
67 /*
68  * End of serial driver configuration section.
69  */
70
71 #ifdef MODVERSIONS
72 #include <linux/modversions.h>
73 #endif
74 #include <linux/module.h>
75
76 #include <linux/types.h>
77 #ifdef LOCAL_HEADERS
78 #include "serial_local.h"
79 #else
80 #include <linux/serial.h>
81 #include <linux/serialP.h>
82 #include <asm/au1000.h>
83 #include <asm/serial.h>
84 #define LOCAL_VERSTRING ""
85 #endif
86
87 #include <linux/errno.h>
88 #include <linux/signal.h>
89 #include <linux/sched.h>
90 #include <linux/timer.h>
91 #include <linux/interrupt.h>
92 #include <linux/tty.h>
93 #include <linux/tty_flip.h>
94 #include <linux/major.h>
95 #include <linux/string.h>
96 #include <linux/fcntl.h>
97 #include <linux/ptrace.h>
98 #include <linux/ioport.h>
99 #include <linux/mm.h>
100 #include <linux/slab.h>
101 #include <linux/init.h>
102 #include <asm/uaccess.h>
103 #include <linux/delay.h>
104 #ifdef CONFIG_AU1000_SERIAL_CONSOLE
105 #include <linux/console.h>
106 #endif
107 #ifdef CONFIG_MAGIC_SYSRQ
108 #include <linux/sysrq.h>
109 #endif
110
111 #include <asm/system.h>
112 #include <asm/io.h>
113 #include <asm/irq.h>
114 #include <asm/bitops.h>
115
116 #ifdef CONFIG_MAC_SERIAL
117 #define SERIAL_DEV_OFFSET       2
118 #else
119 #define SERIAL_DEV_OFFSET       0
120 #endif
121
122 #ifdef SERIAL_INLINE
123 #define _INLINE_ inline
124 #else
125 #define _INLINE_
126 #endif
127
128 static char *serial_name = "Serial driver";
129
130 static DECLARE_TASK_QUEUE(tq_serial);
131
132 static struct tty_driver serial_driver, callout_driver;
133 static int serial_refcount;
134
135 static struct timer_list serial_timer;
136
137 extern unsigned long get_au1000_uart_baud(void);
138
139 /* serial subtype definitions */
140 #ifndef SERIAL_TYPE_NORMAL
141 #define SERIAL_TYPE_NORMAL      1
142 #define SERIAL_TYPE_CALLOUT     2
143 #endif
144
145 /* number of characters left in xmit buffer before we ask for more */
146 #define WAKEUP_CHARS 256
147
148 /*
149  * IRQ_timeout          - How long the timeout should be for each IRQ
150  *                              should be after the IRQ has been active.
151  */
152
153 static struct async_struct *IRQ_ports[NR_IRQS];
154 static int IRQ_timeout[NR_IRQS];
155 #ifdef CONFIG_AU1000_SERIAL_CONSOLE
156 static struct console sercons;
157 static int lsr_break_flag;
158 #endif
159 #if defined(CONFIG_AU1000_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
160 static unsigned long break_pressed; /* break, really ... */
161 #endif
162
163 static void autoconfig(struct serial_state * state);
164 static void change_speed(struct async_struct *info, struct termios *old);
165 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
166
167 /*
168  * Here we define the default xmit fifo size used for each type of
169  * UART
170  */
171 static struct serial_uart_config uart_config[] = {
172         { "unknown", 1, 0 }, 
173         { "8250", 1, 0 }, 
174         { "16450", 1, 0 }, 
175         { "16550", 1, 0 }, 
176         { 0, 0}
177 };
178
179
180 static struct serial_state rs_table[RS_TABLE_SIZE] = {
181         SERIAL_PORT_DFNS        /* Defined in serial.h */
182 };
183
184 #define NR_PORTS        (sizeof(rs_table)/sizeof(struct serial_state))
185
186 #ifndef PREPARE_FUNC
187 #define PREPARE_FUNC(dev)  (dev->prepare)
188 #define ACTIVATE_FUNC(dev)  (dev->activate)
189 #define DEACTIVATE_FUNC(dev)  (dev->deactivate)
190 #endif
191
192 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
193
194 static struct tty_struct *serial_table[NR_PORTS];
195 static struct termios *serial_termios[NR_PORTS];
196 static struct termios *serial_termios_locked[NR_PORTS];
197
198
199 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
200 #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
201  kdevname(tty->device), (info->flags), serial_refcount,info->count,tty->count,s)
202 #else
203 #define DBG_CNT(s)
204 #endif
205
206 /*
207  * tmp_buf is used as a temporary buffer by serial_write.  We need to
208  * lock it in case the copy_from_user blocks while swapping in a page,
209  * and some other program tries to do a serial write at the same time.
210  * Since the lock will only come under contention when the system is
211  * swapping and available memory is low, it makes sense to share one
212  * buffer across all the serial ports, since it significantly saves
213  * memory if large numbers of serial ports are open.
214  */
215 static unsigned char *tmp_buf;
216 #ifdef DECLARE_MUTEX
217 static DECLARE_MUTEX(tmp_buf_sem);
218 #else
219 static struct semaphore tmp_buf_sem = MUTEX;
220 #endif
221
222
223 static inline int serial_paranoia_check(struct async_struct *info,
224                                         kdev_t device, const char *routine)
225 {
226 #ifdef SERIAL_PARANOIA_CHECK
227         static const char *badmagic =
228                 "Warning: bad magic number for serial struct (%s) in %s\n";
229         static const char *badinfo =
230                 "Warning: null async_struct for (%s) in %s\n";
231
232         if (!info) {
233                 printk(badinfo, kdevname(device), routine);
234                 return 1;
235         }
236         if (info->magic != SERIAL_MAGIC) {
237                 printk(badmagic, kdevname(device), routine);
238                 return 1;
239         }
240 #endif
241         return 0;
242 }
243
244
245 static _INLINE_ unsigned int serial_in(struct async_struct *info, int offset)
246 {
247         return (inl(info->port+offset) & 0xff);
248 }
249
250 static _INLINE_ void serial_out(struct async_struct *info, int offset, int value)
251 {
252         outl(value & 0xff, info->port+offset);
253 }
254
255
256 /*
257  * We used to support using pause I/O for certain machines.  We
258  * haven't supported this for a while, but just in case it's badly
259  * needed for certain old 386 machines, I've left these #define's
260  * in....
261  */
262 #define serial_inp(info, offset)                serial_in(info, offset)
263 #define serial_outp(info, offset, value)        serial_out(info, offset, value)
264
265
266 /*
267  * ------------------------------------------------------------
268  * rs_stop() and rs_start()
269  *
270  * This routines are called before setting or resetting tty->stopped.
271  * They enable or disable transmitter interrupts, as necessary.
272  * ------------------------------------------------------------
273  */
274 static void rs_stop(struct tty_struct *tty)
275 {
276         struct async_struct *info = (struct async_struct *)tty->driver_data;
277         unsigned long flags;
278
279         if (serial_paranoia_check(info, tty->device, "rs_stop"))
280                 return;
281         
282         save_flags(flags); cli();
283         if (info->IER & UART_IER_THRI) {
284                 info->IER &= ~UART_IER_THRI;
285                 serial_out(info, UART_IER, info->IER);
286         }
287         restore_flags(flags);
288 }
289
290 static void rs_start(struct tty_struct *tty)
291 {
292         struct async_struct *info = (struct async_struct *)tty->driver_data;
293         unsigned long flags;
294         
295         if (serial_paranoia_check(info, tty->device, "rs_start"))
296                 return;
297         
298         save_flags(flags); cli();
299         if (info->xmit.head != info->xmit.tail
300             && info->xmit.buf
301             && !(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 /*
309  * ----------------------------------------------------------------------
310  *
311  * Here starts the interrupt handling routines.  All of the following
312  * subroutines are declared as inline and are folded into
313  * rs_interrupt().  They were separated out for readability's sake.
314  *
315  * Note: rs_interrupt() is a "fast" interrupt, which means that it
316  * runs with interrupts turned off.  People who may want to modify
317  * rs_interrupt() should try to keep the interrupt handler as fast as
318  * possible.  After you are done making modifications, it is not a bad
319  * idea to do:
320  * 
321  * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
322  *
323  * and look at the resulting assemble code in serial.s.
324  *
325  *                              - Ted Ts'o (tytso@mit.edu), 7-Mar-93
326  * -----------------------------------------------------------------------
327  */
328
329 /*
330  * This routine is used by the interrupt handler to schedule
331  * processing in the software interrupt portion of the driver.
332  */
333 static _INLINE_ void rs_sched_event(struct async_struct *info,
334                                   int event)
335 {
336         info->event |= 1 << event;
337         queue_task(&info->tqueue, &tq_serial);
338         mark_bh(SERIAL_BH);
339 }
340
341 static _INLINE_ void receive_chars(struct async_struct *info,
342                                  int *status, struct pt_regs * regs)
343 {
344         struct tty_struct *tty = info->tty;
345         unsigned char ch;
346         int ignored = 0;
347         struct  async_icount *icount;
348
349         icount = &info->state->icount;
350         do {
351                 ch = serial_inp(info, UART_RX);
352                 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
353                         goto ignore_char;
354                 *tty->flip.char_buf_ptr = ch;
355                 icount->rx++;
356                 
357 #ifdef SERIAL_DEBUG_INTR
358                 printk("DR%02x:%02x...", ch, *status);
359 #endif
360                 *tty->flip.flag_buf_ptr = 0;
361                 if (*status & (UART_LSR_BI | UART_LSR_PE |
362                                UART_LSR_FE | UART_LSR_OE)) {
363                         /*
364                          * For statistics only
365                          */
366                         if (*status & UART_LSR_BI) {
367                                 *status &= ~(UART_LSR_FE | UART_LSR_PE);
368                                 icount->brk++;
369                                 /*
370                                  * We do the SysRQ and SAK checking
371                                  * here because otherwise the break
372                                  * may get masked by ignore_status_mask
373                                  * or read_status_mask.
374                                  */
375 #if defined(CONFIG_AU1000_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
376                                 if (info->line == sercons.index) {
377                                         if (!break_pressed) {
378                                                 break_pressed = jiffies;
379                                                 goto ignore_char;
380                                         }
381                                         break_pressed = 0;
382                                 }
383 #endif
384                                 if (info->flags & ASYNC_SAK)
385                                         do_SAK(tty);
386                         } else if (*status & UART_LSR_PE)
387                                 icount->parity++;
388                         else if (*status & UART_LSR_FE)
389                                 icount->frame++;
390                         if (*status & UART_LSR_OE)
391                                 icount->overrun++;
392
393                         /*
394                          * Now check to see if character should be
395                          * ignored, and mask off conditions which
396                          * should be ignored.
397                          */
398                         if (*status & info->ignore_status_mask) {
399                                 if (++ignored > 100)
400                                         break;
401                                 goto ignore_char;
402                         }
403                         *status &= info->read_status_mask;
404
405 #ifdef CONFIG_AU1000_SERIAL_CONSOLE
406                         if (info->line == sercons.index) {
407                                 /* Recover the break flag from console xmit */
408                                 *status |= lsr_break_flag;
409                                 lsr_break_flag = 0;
410                         }
411 #endif
412                         if (*status & (UART_LSR_BI)) {
413 #ifdef SERIAL_DEBUG_INTR
414                                 printk("handling break....");
415 #endif
416                                 *tty->flip.flag_buf_ptr = TTY_BREAK;
417                         } else if (*status & UART_LSR_PE)
418                                 *tty->flip.flag_buf_ptr = TTY_PARITY;
419                         else if (*status & UART_LSR_FE)
420                                 *tty->flip.flag_buf_ptr = TTY_FRAME;
421                         if (*status & UART_LSR_OE) {
422                                 /*
423                                  * Overrun is special, since it's
424                                  * reported immediately, and doesn't
425                                  * affect the current character
426                                  */
427                                 tty->flip.count++;
428                                 tty->flip.flag_buf_ptr++;
429                                 tty->flip.char_buf_ptr++;
430                                 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
431                                 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
432                                         goto ignore_char;
433                         }
434                 }
435 #if defined(CONFIG_AU1000_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
436                 if (break_pressed && info->line == sercons.index) {
437                         if (ch != 0 &&
438                             time_before(jiffies, break_pressed + HZ*5)) {
439                                 handle_sysrq(ch, regs, NULL, NULL);
440                                 break_pressed = 0;
441                                 goto ignore_char;
442                         }
443                         break_pressed = 0;
444                 }
445 #endif
446                 tty->flip.flag_buf_ptr++;
447                 tty->flip.char_buf_ptr++;
448                 tty->flip.count++;
449         ignore_char:
450                 *status = serial_inp(info, UART_LSR);
451         } while (*status & UART_LSR_DR);
452         tty_flip_buffer_push(tty);
453 }
454
455 static _INLINE_ void transmit_chars(struct async_struct *info, int *intr_done)
456 {
457         int count;
458
459         if (info->x_char) {
460                 serial_outp(info, UART_TX, info->x_char);
461                 info->state->icount.tx++;
462                 info->x_char = 0;
463                 if (intr_done)
464                         *intr_done = 0;
465                 return;
466         }
467         if (info->xmit.head == info->xmit.tail
468             || info->tty->stopped
469             || info->tty->hw_stopped) {
470                 info->IER &= ~UART_IER_THRI;
471                 serial_out(info, UART_IER, info->IER);
472                 return;
473         }
474         
475         count = info->xmit_fifo_size;
476         do {
477                 serial_out(info, UART_TX, info->xmit.buf[info->xmit.tail]);
478                 info->xmit.tail = (info->xmit.tail + 1) & (SERIAL_XMIT_SIZE-1);
479                 info->state->icount.tx++;
480                 if (info->xmit.head == info->xmit.tail)
481                         break;
482         } while (--count > 0);
483         
484         if (CIRC_CNT(info->xmit.head,
485                      info->xmit.tail,
486                      SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
487                 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
488
489 #ifdef SERIAL_DEBUG_INTR
490         printk("THRE...");
491 #endif
492         if (intr_done)
493                 *intr_done = 0;
494
495         if (info->xmit.head == info->xmit.tail) {
496                 info->IER &= ~UART_IER_THRI;
497                 serial_out(info, UART_IER, info->IER);
498         }
499 }
500
501 static _INLINE_ void check_modem_status(struct async_struct *info)
502 {
503         int     status;
504         struct  async_icount *icount;
505         
506         status = serial_in(info, UART_MSR);
507
508         if (status & UART_MSR_ANY_DELTA) {
509                 icount = &info->state->icount;
510                 /* update input line counters */
511                 if (status & UART_MSR_TERI)
512                         icount->rng++;
513                 if (status & UART_MSR_DDSR)
514                         icount->dsr++;
515                 if (status & UART_MSR_DDCD) {
516                         icount->dcd++;
517 #ifdef CONFIG_HARD_PPS
518                         if ((info->flags & ASYNC_HARDPPS_CD) &&
519                             (status & UART_MSR_DCD))
520                                 hardpps();
521 #endif
522                 }
523                 if (status & UART_MSR_DCTS)
524                         icount->cts++;
525                 wake_up_interruptible(&info->delta_msr_wait);
526         }
527
528         if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
529 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
530                 printk("ttys%d CD now %s...", info->line,
531                        (status & UART_MSR_DCD) ? "on" : "off");
532 #endif          
533                 if (status & UART_MSR_DCD)
534                         wake_up_interruptible(&info->open_wait);
535                 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
536                            (info->flags & ASYNC_CALLOUT_NOHUP))) {
537 #ifdef SERIAL_DEBUG_OPEN
538                         printk("doing serial hangup...");
539 #endif
540                         if (info->tty)
541                                 tty_hangup(info->tty);
542                 }
543         }
544         if (info->flags & ASYNC_CTS_FLOW) {
545                 if (info->tty->hw_stopped) {
546                         if (status & UART_MSR_CTS) {
547 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
548                                 printk("CTS tx start...");
549 #endif
550                                 info->tty->hw_stopped = 0;
551                                 info->IER |= UART_IER_THRI;
552                                 serial_out(info, UART_IER, info->IER);
553                                 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
554                                 return;
555                         }
556                 } else {
557                         if (!(status & UART_MSR_CTS)) {
558 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
559                                 printk("CTS tx stop...");
560 #endif
561                                 info->tty->hw_stopped = 1;
562                                 info->IER &= ~UART_IER_THRI;
563                                 serial_out(info, UART_IER, info->IER);
564                         }
565                 }
566         }
567 }
568
569
570
571 /*
572  * This is the serial driver's interrupt routine for a single port
573  */
574 static void rs_interrupt_single(int irq, void *dev_id, struct pt_regs * regs)
575 {
576         int status;
577         int pass_counter = 0;
578         struct async_struct * info;
579         
580 #ifdef SERIAL_DEBUG_INTR
581         printk("rs_interrupt_single(%d)...", irq);
582 #endif
583
584         info = IRQ_ports[irq];
585         if (!info || !info->tty)
586                 return;
587
588         do {
589                 status = serial_inp(info, UART_LSR);
590 #ifdef SERIAL_DEBUG_INTR
591                 printk("status = %x...", status);
592 #endif
593                 if (status & UART_LSR_DR)
594                         receive_chars(info, &status, regs);
595                 check_modem_status(info);
596                 if (status & UART_LSR_THRE)
597                         transmit_chars(info, 0);
598                 if (pass_counter++ > RS_ISR_PASS_LIMIT) {
599 #if 0
600                         printk("rs_single loop break.\n");
601 #endif
602                         break;
603                 }
604         } while (!(serial_in(info, UART_IIR) & UART_IIR_NO_INT));
605         info->last_active = jiffies;
606 #ifdef SERIAL_DEBUG_INTR
607         printk("end.\n");
608 #endif
609 }
610
611
612 /*
613  * -------------------------------------------------------------------
614  * Here ends the serial interrupt routines.
615  * -------------------------------------------------------------------
616  */
617
618 /*
619  * This routine is used to handle the "bottom half" processing for the
620  * serial driver, known also the "software interrupt" processing.
621  * This processing is done at the kernel interrupt level, after the
622  * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
623  * is where time-consuming activities which can not be done in the
624  * interrupt driver proper are done; the interrupt driver schedules
625  * them using rs_sched_event(), and they get done here.
626  */
627 static void do_serial_bh(void)
628 {
629         run_task_queue(&tq_serial);
630 }
631
632 static void do_softint(void *private_)
633 {
634         struct async_struct     *info = (struct async_struct *) private_;
635         struct tty_struct       *tty;
636         
637         tty = info->tty;
638         if (!tty)
639                 return;
640
641         if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
642                 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
643                     tty->ldisc.write_wakeup)
644                         (tty->ldisc.write_wakeup)(tty);
645                 wake_up_interruptible(&tty->write_wait);
646 #ifdef SERIAL_HAVE_POLL_WAIT
647                 wake_up_interruptible(&tty->poll_wait);
648 #endif
649         }
650 }
651
652 /*
653  * This subroutine is called when the RS_TIMER goes off.  It is used
654  * by the serial driver to handle ports that do not have an interrupt
655  * (irq=0).  This doesn't work very well for 16450's, but gives barely
656  * passable results for a 16550A.  (Although at the expense of much
657  * CPU overhead).
658  */
659 static void rs_timer(unsigned long dummy)
660 {
661         static unsigned long last_strobe;
662         struct async_struct *info;
663         unsigned int    i;
664         unsigned long flags;
665
666         if ((jiffies - last_strobe) >= RS_STROBE_TIME) {
667                 for (i=0; i < NR_IRQS; i++) {
668                         info = IRQ_ports[i];
669                         if (!info)
670                                 continue;
671                         save_flags(flags); cli();
672                                 rs_interrupt_single(i, NULL, NULL);
673                         restore_flags(flags);
674                 }
675         }
676         last_strobe = jiffies;
677         mod_timer(&serial_timer, jiffies + RS_STROBE_TIME);
678
679 #if 0
680         if (IRQ_ports[0]) {
681                 save_flags(flags); cli();
682                 rs_interrupt_single(0, NULL, NULL);
683                 restore_flags(flags);
684
685                 mod_timer(&serial_timer, jiffies + IRQ_timeout[0]);
686         }
687 #endif
688 }
689
690 /*
691  * ---------------------------------------------------------------
692  * Low level utility subroutines for the serial driver:  routines to
693  * figure out the appropriate timeout for an interrupt chain, routines
694  * to initialize and startup a serial port, and routines to shutdown a
695  * serial port.  Useful stuff like that.
696  * ---------------------------------------------------------------
697  */
698
699 /*
700  * This routine figures out the correct timeout for a particular IRQ.
701  * It uses the smallest timeout of all of the serial ports in a
702  * particular interrupt chain.  Now only used for IRQ 0....
703  */
704 static void figure_IRQ_timeout(int irq)
705 {
706         struct  async_struct    *info;
707         int     timeout = 60*HZ;        /* 60 seconds === a long time :-) */
708
709         info = IRQ_ports[irq];
710         if (!info) {
711                 IRQ_timeout[irq] = 60*HZ;
712                 return;
713         }
714         while (info) {
715                 if (info->timeout < timeout)
716                         timeout = info->timeout;
717                 info = info->next_port;
718         }
719         if (!irq)
720                 timeout = timeout / 2;
721         IRQ_timeout[irq] = (timeout > 3) ? timeout-2 : 1;
722 }
723
724
725 static int startup(struct async_struct * info)
726 {
727         unsigned long flags;
728         int     retval=0;
729         void (*handler)(int, void *, struct pt_regs *);
730         struct serial_state *state= info->state;
731         unsigned long page;
732
733         page = get_zeroed_page(GFP_KERNEL);
734         if (!page)
735                 return -ENOMEM;
736
737         save_flags(flags); cli();
738
739         if (info->flags & ASYNC_INITIALIZED) {
740                 free_page(page);
741                 goto errout;
742         }
743
744         if (!CONFIGURED_SERIAL_PORT(state) || !state->type) {
745                 if (info->tty)
746                         set_bit(TTY_IO_ERROR, &info->tty->flags);
747                 free_page(page);
748                 goto errout;
749         }
750         if (info->xmit.buf)
751                 free_page(page);
752         else
753                 info->xmit.buf = (unsigned char *) page;
754
755
756         if (inl(UART_MOD_CNTRL + state->port) != 0x3) {
757                 outl(3, UART_MOD_CNTRL + state->port);
758         }
759 #ifdef SERIAL_DEBUG_OPEN
760         printk("starting up ttys%d (irq %d)...", info->line, state->irq);
761 #endif
762
763
764         /*
765          * Clear the FIFO buffers and disable them
766          * (they will be reenabled in change_speed())
767          */
768         if (uart_config[state->type].flags & UART_CLEAR_FIFO) {
769                 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
770                 serial_outp(info, UART_FCR, (UART_FCR_ENABLE_FIFO |
771                                              UART_FCR_CLEAR_RCVR |
772                                              UART_FCR_CLEAR_XMIT));
773                 serial_outp(info, UART_FCR, 0);
774         }
775
776         /*
777          * Clear the interrupt registers.
778          */
779         (void) serial_inp(info, UART_LSR);
780         (void) serial_inp(info, UART_RX);
781         (void) serial_inp(info, UART_IIR);
782         (void) serial_inp(info, UART_MSR);
783
784         /*
785          * At this point there's no way the LSR could still be 0xFF;
786          * if it is, then bail out, because there's likely no UART
787          * here.
788          */
789         if (!(info->flags & ASYNC_BUGGY_UART) &&
790             (serial_inp(info, UART_LSR) == 0xff)) {
791                 printk("LSR safety check engaged!\n");
792                 if (capable(CAP_SYS_ADMIN)) {
793                         if (info->tty)
794                                 set_bit(TTY_IO_ERROR, &info->tty->flags);
795                 } else
796                         retval = -ENODEV;
797                 goto errout;
798         }
799         
800         /*
801          * Allocate the IRQ if necessary
802          */
803 #if 0
804         /* au1000, uart0 irq is 0 */
805         if (state->irq && (!IRQ_ports[state->irq] || !IRQ_ports[state->irq]->next_port)) {
806 #endif
807         if ((!IRQ_ports[state->irq] || !IRQ_ports[state->irq]->next_port)) {
808                 if (IRQ_ports[state->irq]) {
809                         retval = -EBUSY;
810                         goto errout;
811                 } else 
812                         handler = rs_interrupt_single;
813
814                 retval = request_irq(state->irq, handler, SA_SHIRQ,
815                                      "serial", &IRQ_ports[state->irq]);
816                 if (retval) {
817                         if (capable(CAP_SYS_ADMIN)) {
818                                 if (info->tty)
819                                         set_bit(TTY_IO_ERROR,
820                                                 &info->tty->flags);
821                                 retval = 0;
822                         }
823                         goto errout;
824                 }
825         }
826
827         /*
828          * Insert serial port into IRQ chain.
829          */
830         info->prev_port = 0;
831         info->next_port = IRQ_ports[state->irq];
832         if (info->next_port)
833                 info->next_port->prev_port = info;
834         IRQ_ports[state->irq] = info;
835         figure_IRQ_timeout(state->irq);
836
837         /*
838          * Now, initialize the UART 
839          */
840         serial_outp(info, UART_LCR, UART_LCR_WLEN8);
841
842         info->MCR = 0;
843         if (info->tty->termios->c_cflag & CBAUD)
844                 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
845         {
846                 if (state->irq != 0)
847                         info->MCR |= UART_MCR_OUT2;
848         }
849         info->MCR |= ALPHA_KLUDGE_MCR;          /* Don't ask */
850         serial_outp(info, UART_MCR, info->MCR);
851         
852         /*
853          * Finally, enable interrupts
854          */
855         info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
856         serial_outp(info, UART_IER, info->IER); /* enable interrupts */
857         
858
859         /*
860          * And clear the interrupt registers again for luck.
861          */
862         (void)serial_inp(info, UART_LSR);
863         (void)serial_inp(info, UART_RX);
864         (void)serial_inp(info, UART_IIR);
865         (void)serial_inp(info, UART_MSR);
866
867         if (info->tty)
868                 clear_bit(TTY_IO_ERROR, &info->tty->flags);
869         info->xmit.head = info->xmit.tail = 0;
870
871         /*
872          * Set up serial timers...
873          */
874         mod_timer(&serial_timer, jiffies + 2*HZ/100);
875
876         /*
877          * Set up the tty->alt_speed kludge
878          */
879         if (info->tty) {
880                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
881                         info->tty->alt_speed = 57600;
882                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
883                         info->tty->alt_speed = 115200;
884                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
885                         info->tty->alt_speed = 230400;
886                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
887                         info->tty->alt_speed = 460800;
888         }
889         
890         /*
891          * and set the speed of the serial port
892          */
893         change_speed(info, 0);
894
895         info->flags |= ASYNC_INITIALIZED;
896         restore_flags(flags);
897         return 0;
898         
899 errout:
900         restore_flags(flags);
901         return retval;
902 }
903
904 /*
905  * This routine will shutdown a serial port; interrupts are disabled, and
906  * DTR is dropped if the hangup on close termio flag is on.
907  */
908 static void shutdown(struct async_struct * info)
909 {
910         unsigned long   flags;
911         struct serial_state *state;
912         int             retval;
913
914         if (!(info->flags & ASYNC_INITIALIZED))
915                 return;
916
917         state = info->state;
918
919 #ifdef SERIAL_DEBUG_OPEN
920         printk("Shutting down serial port %d (irq %d)....", info->line,
921                state->irq);
922 #endif
923         
924         save_flags(flags); cli(); /* Disable interrupts */
925
926         /*
927          * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
928          * here so the queue might never be waken up
929          */
930         wake_up_interruptible(&info->delta_msr_wait);
931         
932         /*
933          * First unlink the serial port from the IRQ chain...
934          */
935         if (info->next_port)
936                 info->next_port->prev_port = info->prev_port;
937         if (info->prev_port)
938                 info->prev_port->next_port = info->next_port;
939         else
940                 IRQ_ports[state->irq] = info->next_port;
941         figure_IRQ_timeout(state->irq);
942         
943         /*
944          * Free the IRQ, if necessary
945          */
946 //      if (state->irq && (!IRQ_ports[state->irq] ||
947         if ((!IRQ_ports[state->irq] ||
948                           !IRQ_ports[state->irq]->next_port)) {
949                 if (IRQ_ports[state->irq]) {
950                         free_irq(state->irq, &IRQ_ports[state->irq]);
951                         retval = request_irq(state->irq, rs_interrupt_single,
952                                              SA_SHIRQ, "serial",
953                                              &IRQ_ports[state->irq]);
954                         
955                         if (retval)
956                                 printk("serial shutdown: request_irq: error %d"
957                                        "  Couldn't reacquire IRQ.\n", retval);
958                 } else
959                         free_irq(state->irq, &IRQ_ports[state->irq]);
960         }
961
962         if (info->xmit.buf) {
963                 unsigned long pg = (unsigned long) info->xmit.buf;
964                 info->xmit.buf = 0;
965                 free_page(pg);
966         }
967
968         info->IER = 0;
969         serial_outp(info, UART_IER, 0x00);      /* disable all intrs */
970                 info->MCR &= ~UART_MCR_OUT2;
971         info->MCR |= ALPHA_KLUDGE_MCR;          /* Don't ask */
972         
973         /* disable break condition */
974         serial_out(info, UART_LCR, serial_inp(info, UART_LCR) & ~UART_LCR_SBC);
975         
976         if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
977                 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
978         serial_outp(info, UART_MCR, info->MCR);
979
980         /* disable FIFO's */    
981         serial_outp(info, UART_FCR, (UART_FCR_ENABLE_FIFO |
982                                      UART_FCR_CLEAR_RCVR |
983                                      UART_FCR_CLEAR_XMIT));
984         serial_outp(info, UART_FCR, 0);
985
986         (void)serial_in(info, UART_RX);    /* read data port to reset things */
987         
988         if (info->tty)
989                 set_bit(TTY_IO_ERROR, &info->tty->flags);
990
991         info->flags &= ~ASYNC_INITIALIZED;
992         restore_flags(flags);
993 }
994
995
996 /*
997  * This routine is called to set the UART divisor registers to match
998  * the specified baud rate for a serial port.
999  */
1000 static void change_speed(struct async_struct *info,
1001                          struct termios *old_termios)
1002 {
1003         int     quot = 0, baud_base, baud;
1004         unsigned cflag, cval, fcr = 0;
1005         int     bits;
1006         unsigned long   flags;
1007
1008         if (!info->tty || !info->tty->termios)
1009                 return;
1010         cflag = info->tty->termios->c_cflag;
1011         if (!CONFIGURED_SERIAL_PORT(info))
1012                 return;
1013
1014         /* byte size and parity */
1015         switch (cflag & CSIZE) {
1016               case CS5: cval = 0x00; bits = 7; break;
1017               case CS6: cval = 0x01; bits = 8; break;
1018               case CS7: cval = 0x02; bits = 9; break;
1019               case CS8: cval = 0x03; bits = 10; break;
1020               /* Never happens, but GCC is too dumb to figure it out */
1021               default:  cval = 0x00; bits = 7; break;
1022               }
1023         if (cflag & CSTOPB) {
1024                 cval |= 0x04;
1025                 bits++;
1026         }
1027         if (cflag & PARENB) {
1028                 cval |= UART_LCR_PARITY;
1029                 bits++;
1030         }
1031         if (!(cflag & PARODD))
1032                 cval |= UART_LCR_EPAR;
1033 #ifdef CMSPAR
1034         if (cflag & CMSPAR)
1035                 cval |= UART_LCR_SPAR;
1036 #endif
1037
1038         /* Determine divisor based on baud rate */
1039         baud = tty_get_baud_rate(info->tty);
1040         if (!baud) {
1041                 baud = 9600;    /* B0 transition handled in rs_set_termios */
1042         }
1043         baud_base = info->state->baud_base;
1044         //if (baud == 38400 &&
1045         if (((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)) {
1046                 quot = info->state->custom_divisor;
1047         }
1048         else {
1049                 if (baud == 134)
1050                         /* Special case since 134 is really 134.5 */
1051                         quot = (2*baud_base / 269);
1052                 else if (baud)
1053                         quot = baud_base / baud;
1054         }
1055         /* If the quotient is zero refuse the change */
1056         if (!quot && old_termios) {
1057                 info->tty->termios->c_cflag &= ~CBAUD;
1058                 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
1059                 baud = tty_get_baud_rate(info->tty);
1060                 if (!baud)
1061                         baud = 9600;
1062                 if (baud == 38400 &&
1063                     ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1064                         quot = info->state->custom_divisor;
1065                 else {
1066                         if (baud == 134)
1067                                 /* Special case since 134 is really 134.5 */
1068                                 quot = (2*baud_base / 269);
1069                         else if (baud)
1070                                 quot = baud_base / baud;
1071                 }
1072         }
1073         /* As a last resort, if the quotient is zero, default to 9600 bps */
1074         if (!quot)
1075                 quot = baud_base / 9600;
1076         
1077         info->quot = quot;
1078         info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
1079         info->timeout += HZ/50;         /* Add .02 seconds of slop */
1080
1081         /* Set up FIFO's */
1082         if (uart_config[info->state->type].flags & UART_USE_FIFO) {
1083                 if ((info->state->baud_base / quot) < 2400)
1084                         fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIGGER_1;
1085                 else
1086                         fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIGGER_8;
1087         }
1088         
1089         /* CTS flow control flag and modem status interrupts */
1090         info->IER &= ~UART_IER_MSI;
1091         if (info->flags & ASYNC_HARDPPS_CD)
1092                 info->IER |= UART_IER_MSI;
1093         if (cflag & CRTSCTS) {
1094                 info->flags |= ASYNC_CTS_FLOW;
1095                 info->IER |= UART_IER_MSI;
1096         } else
1097                 info->flags &= ~ASYNC_CTS_FLOW;
1098         if (cflag & CLOCAL)
1099                 info->flags &= ~ASYNC_CHECK_CD;
1100         else {
1101                 info->flags |= ASYNC_CHECK_CD;
1102                 info->IER |= UART_IER_MSI;
1103         }
1104         serial_out(info, UART_IER, info->IER);
1105
1106         /*
1107          * Set up parity check flag
1108          */
1109 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1110
1111         info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1112         if (I_INPCK(info->tty))
1113                 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1114         if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1115                 info->read_status_mask |= UART_LSR_BI;
1116         
1117         /*
1118          * Characters to ignore
1119          */
1120         info->ignore_status_mask = 0;
1121         if (I_IGNPAR(info->tty))
1122                 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1123         if (I_IGNBRK(info->tty)) {
1124                 info->ignore_status_mask |= UART_LSR_BI;
1125                 /*
1126                  * If we're ignore parity and break indicators, ignore 
1127                  * overruns too.  (For real raw support).
1128                  */
1129                 if (I_IGNPAR(info->tty))
1130                         info->ignore_status_mask |= UART_LSR_OE;
1131         }
1132         /*
1133          * !!! ignore all characters if CREAD is not set
1134          */
1135         if ((cflag & CREAD) == 0)
1136                 info->ignore_status_mask |= UART_LSR_DR;
1137         save_flags(flags); cli();
1138
1139         serial_outp(info, UART_CLK, quot & 0xffff);
1140         serial_outp(info, UART_LCR, cval);
1141         info->LCR = cval;                               /* Save LCR */
1142         restore_flags(flags);
1143 }
1144
1145 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
1146 {
1147         struct async_struct *info = (struct async_struct *)tty->driver_data;
1148         unsigned long flags;
1149
1150         if (serial_paranoia_check(info, tty->device, "rs_put_char"))
1151                 return;
1152
1153         if (!tty || !info->xmit.buf)
1154                 return;
1155
1156         save_flags(flags); cli();
1157         if (CIRC_SPACE(info->xmit.head,
1158                        info->xmit.tail,
1159                        SERIAL_XMIT_SIZE) == 0) {
1160                 restore_flags(flags);
1161                 return;
1162         }
1163
1164         info->xmit.buf[info->xmit.head] = ch;
1165         info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
1166         restore_flags(flags);
1167 }
1168
1169 static void rs_flush_chars(struct tty_struct *tty)
1170 {
1171         struct async_struct *info = (struct async_struct *)tty->driver_data;
1172         unsigned long flags;
1173                                 
1174         if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
1175                 return;
1176
1177         if (info->xmit.head == info->xmit.tail
1178             || tty->stopped
1179             || tty->hw_stopped
1180             || !info->xmit.buf)
1181                 return;
1182
1183         save_flags(flags); cli();
1184         info->IER |= UART_IER_THRI;
1185         serial_out(info, UART_IER, info->IER);
1186         restore_flags(flags);
1187 }
1188
1189 static int rs_write(struct tty_struct * tty, int from_user,
1190                     const unsigned char *buf, int count)
1191 {
1192         int     c, ret = 0;
1193         struct async_struct *info = (struct async_struct *)tty->driver_data;
1194         unsigned long flags;
1195                                 
1196         if (serial_paranoia_check(info, tty->device, "rs_write"))
1197                 return 0;
1198
1199         if (!tty || !info->xmit.buf || !tmp_buf)
1200                 return 0;
1201
1202         save_flags(flags);
1203         if (from_user) {
1204                 down(&tmp_buf_sem);
1205                 while (1) {
1206                         int c1;
1207                         c = CIRC_SPACE_TO_END(info->xmit.head,
1208                                               info->xmit.tail,
1209                                               SERIAL_XMIT_SIZE);
1210                         if (count < c)
1211                                 c = count;
1212                         if (c <= 0)
1213                                 break;
1214
1215                         c -= copy_from_user(tmp_buf, buf, c);
1216                         if (!c) {
1217                                 if (!ret)
1218                                         ret = -EFAULT;
1219                                 break;
1220                         }
1221                         cli();
1222                         c1 = CIRC_SPACE_TO_END(info->xmit.head,
1223                                                info->xmit.tail,
1224                                                SERIAL_XMIT_SIZE);
1225                         if (c1 < c)
1226                                 c = c1;
1227                         memcpy(info->xmit.buf + info->xmit.head, tmp_buf, c);
1228                         info->xmit.head = ((info->xmit.head + c) &
1229                                            (SERIAL_XMIT_SIZE-1));
1230                         restore_flags(flags);
1231                         buf += c;
1232                         count -= c;
1233                         ret += c;
1234                 }
1235                 up(&tmp_buf_sem);
1236         } else {
1237                 cli();
1238                 while (1) {
1239                         c = CIRC_SPACE_TO_END(info->xmit.head,
1240                                               info->xmit.tail,
1241                                               SERIAL_XMIT_SIZE);
1242                         if (count < c)
1243                                 c = count;
1244                         if (c <= 0) {
1245                                 break;
1246                         }
1247                         memcpy(info->xmit.buf + info->xmit.head, buf, c);
1248                         info->xmit.head = ((info->xmit.head + c) &
1249                                            (SERIAL_XMIT_SIZE-1));
1250                         buf += c;
1251                         count -= c;
1252                         ret += c;
1253                 }
1254                 restore_flags(flags);
1255         }
1256         if (info->xmit.head != info->xmit.tail
1257             && !tty->stopped
1258             && !tty->hw_stopped
1259             && !(info->IER & UART_IER_THRI)) {
1260                 info->IER |= UART_IER_THRI;
1261                 serial_out(info, UART_IER, info->IER);
1262         }
1263         return ret;
1264 }
1265
1266 static int rs_write_room(struct tty_struct *tty)
1267 {
1268         struct async_struct *info = (struct async_struct *)tty->driver_data;
1269
1270         if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1271                 return 0;
1272         return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1273 }
1274
1275 static int rs_chars_in_buffer(struct tty_struct *tty)
1276 {
1277         struct async_struct *info = (struct async_struct *)tty->driver_data;
1278                                 
1279         if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1280                 return 0;
1281         return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1282 }
1283
1284 static void rs_flush_buffer(struct tty_struct *tty)
1285 {
1286         struct async_struct *info = (struct async_struct *)tty->driver_data;
1287         unsigned long flags;
1288         
1289         if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1290                 return;
1291         save_flags(flags); cli();
1292         info->xmit.head = info->xmit.tail = 0;
1293         restore_flags(flags);
1294         wake_up_interruptible(&tty->write_wait);
1295 #ifdef SERIAL_HAVE_POLL_WAIT
1296         wake_up_interruptible(&tty->poll_wait);
1297 #endif
1298         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1299             tty->ldisc.write_wakeup)
1300                 (tty->ldisc.write_wakeup)(tty);
1301 }
1302
1303 /*
1304  * This function is used to send a high-priority XON/XOFF character to
1305  * the device
1306  */
1307 static void rs_send_xchar(struct tty_struct *tty, char ch)
1308 {
1309         struct async_struct *info = (struct async_struct *)tty->driver_data;
1310
1311         if (serial_paranoia_check(info, tty->device, "rs_send_char"))
1312                 return;
1313
1314         info->x_char = ch;
1315         if (ch) {
1316                 /* Make sure transmit interrupts are on */
1317                 info->IER |= UART_IER_THRI;
1318                 serial_out(info, UART_IER, info->IER);
1319         }
1320 }
1321
1322 /*
1323  * ------------------------------------------------------------
1324  * rs_throttle()
1325  * 
1326  * This routine is called by the upper-layer tty layer to signal that
1327  * incoming characters should be throttled.
1328  * ------------------------------------------------------------
1329  */
1330 static void rs_throttle(struct tty_struct * tty)
1331 {
1332         struct async_struct *info = (struct async_struct *)tty->driver_data;
1333         unsigned long flags;
1334 #ifdef SERIAL_DEBUG_THROTTLE
1335         char    buf[64];
1336         
1337         printk("throttle %s: %d....\n", tty_name(tty, buf),
1338                tty->ldisc.chars_in_buffer(tty));
1339 #endif
1340
1341         if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1342                 return;
1343         
1344         if (I_IXOFF(tty))
1345                 rs_send_xchar(tty, STOP_CHAR(tty));
1346
1347         if (tty->termios->c_cflag & CRTSCTS)
1348                 info->MCR &= ~UART_MCR_RTS;
1349
1350         save_flags(flags); cli();
1351         serial_out(info, UART_MCR, info->MCR);
1352         restore_flags(flags);
1353 }
1354
1355 static void rs_unthrottle(struct tty_struct * tty)
1356 {
1357         struct async_struct *info = (struct async_struct *)tty->driver_data;
1358         unsigned long flags;
1359 #ifdef SERIAL_DEBUG_THROTTLE
1360         char    buf[64];
1361         
1362         printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1363                tty->ldisc.chars_in_buffer(tty));
1364 #endif
1365
1366         if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1367                 return;
1368         
1369         if (I_IXOFF(tty)) {
1370                 if (info->x_char)
1371                         info->x_char = 0;
1372                 else
1373                         rs_send_xchar(tty, START_CHAR(tty));
1374         }
1375         if (tty->termios->c_cflag & CRTSCTS)
1376                 info->MCR |= UART_MCR_RTS;
1377         save_flags(flags); cli();
1378         serial_out(info, UART_MCR, info->MCR);
1379         restore_flags(flags);
1380 }
1381
1382 /*
1383  * ------------------------------------------------------------
1384  * rs_ioctl() and friends
1385  * ------------------------------------------------------------
1386  */
1387
1388 static int get_serial_info(struct async_struct * info,
1389                            struct serial_struct * retinfo)
1390 {
1391         struct serial_struct tmp;
1392         struct serial_state *state = info->state;
1393    
1394         if (!retinfo)
1395                 return -EFAULT;
1396         memset(&tmp, 0, sizeof(tmp));
1397         tmp.type = state->type;
1398         tmp.line = state->line;
1399         tmp.port = state->port;
1400         if (HIGH_BITS_OFFSET)
1401                 tmp.port_high = state->port >> HIGH_BITS_OFFSET;
1402         else
1403                 tmp.port_high = 0;
1404         tmp.irq = state->irq;
1405         tmp.flags = state->flags;
1406         tmp.xmit_fifo_size = state->xmit_fifo_size;
1407         tmp.baud_base = state->baud_base;
1408         tmp.close_delay = state->close_delay;
1409         tmp.closing_wait = state->closing_wait;
1410         tmp.custom_divisor = state->custom_divisor;
1411         tmp.hub6 = state->hub6;
1412         tmp.io_type = state->io_type;
1413         if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1414                 return -EFAULT;
1415         return 0;
1416 }
1417
1418 static int set_serial_info(struct async_struct * info,
1419                            struct serial_struct * new_info)
1420 {
1421         struct serial_struct new_serial;
1422         struct serial_state old_state, *state;
1423         unsigned int            i,change_irq,change_port;
1424         int                     retval = 0;
1425         unsigned long           new_port;
1426
1427         if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1428                 return -EFAULT;
1429         state = info->state;
1430         old_state = *state;
1431
1432         new_port = new_serial.port;
1433         if (HIGH_BITS_OFFSET)
1434                 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
1435
1436         change_irq = new_serial.irq != state->irq;
1437         change_port = (new_port != ((int) state->port)) ||
1438                 (new_serial.hub6 != state->hub6);
1439   
1440         if (!capable(CAP_SYS_ADMIN)) {
1441                 if (change_irq || change_port ||
1442                     (new_serial.baud_base != state->baud_base) ||
1443                     (new_serial.type != state->type) ||
1444                     (new_serial.close_delay != state->close_delay) ||
1445                     (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1446                     ((new_serial.flags & ~ASYNC_USR_MASK) !=
1447                      (state->flags & ~ASYNC_USR_MASK)))
1448                         return -EPERM;
1449                 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
1450                                (new_serial.flags & ASYNC_USR_MASK));
1451                 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1452                                (new_serial.flags & ASYNC_USR_MASK));
1453                 state->custom_divisor = new_serial.custom_divisor;
1454                 goto check_and_exit;
1455         }
1456
1457         new_serial.irq = irq_cannonicalize(new_serial.irq);
1458
1459         if ((new_serial.irq >= NR_IRQS) || (new_serial.irq < 0) || 
1460             (new_serial.baud_base < 9600)|| (new_serial.type < PORT_UNKNOWN) ||
1461             (new_serial.type > PORT_MAX) || (new_serial.type == PORT_CIRRUS) ||
1462             (new_serial.type == PORT_STARTECH)) {
1463                 return -EINVAL;
1464         }
1465
1466         if ((new_serial.type != state->type) ||
1467             (new_serial.xmit_fifo_size <= 0))
1468                 new_serial.xmit_fifo_size =
1469                         uart_config[new_serial.type].dfl_xmit_fifo_size;
1470
1471         /* Make sure address is not already in use */
1472         if (new_serial.type) {
1473                 for (i = 0 ; i < NR_PORTS; i++)
1474                         if ((state != &rs_table[i]) &&
1475                             (rs_table[i].port == new_port) &&
1476                             rs_table[i].type)
1477                                 return -EADDRINUSE;
1478         }
1479
1480         if ((change_port || change_irq) && (state->count > 1))
1481                 return -EBUSY;
1482
1483         /*
1484          * OK, past this point, all the error checking has been done.
1485          * At this point, we start making changes.....
1486          */
1487
1488         state->baud_base = new_serial.baud_base;
1489         state->flags = ((state->flags & ~ASYNC_FLAGS) |
1490                         (new_serial.flags & ASYNC_FLAGS));
1491         info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
1492                        (info->flags & ASYNC_INTERNAL_FLAGS));
1493         state->custom_divisor = new_serial.custom_divisor;
1494         state->close_delay = new_serial.close_delay * HZ/100;
1495         state->closing_wait = new_serial.closing_wait * HZ/100;
1496         info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1497         info->xmit_fifo_size = state->xmit_fifo_size =
1498                 new_serial.xmit_fifo_size;
1499
1500         if ((state->type != PORT_UNKNOWN) && state->port) {
1501                 release_region(state->port,8);
1502         }
1503         state->type = new_serial.type;
1504         if (change_port || change_irq) {
1505                 /*
1506                  * We need to shutdown the serial port at the old
1507                  * port/irq combination.
1508                  */
1509                 shutdown(info);
1510                 state->irq = new_serial.irq;
1511                 info->port = state->port = new_port;
1512                 info->hub6 = state->hub6 = new_serial.hub6;
1513                 if (info->hub6)
1514                         info->io_type = state->io_type = SERIAL_IO_HUB6;
1515                 else if (info->io_type == SERIAL_IO_HUB6)
1516                         info->io_type = state->io_type = SERIAL_IO_PORT;
1517         }
1518         if ((state->type != PORT_UNKNOWN) && state->port) {
1519                         request_region(state->port,8,"serial(set)");
1520         }
1521
1522         
1523 check_and_exit:
1524         if (!state->port || !state->type)
1525                 return 0;
1526         if (info->flags & ASYNC_INITIALIZED) {
1527                 if (((old_state.flags & ASYNC_SPD_MASK) !=
1528                      (state->flags & ASYNC_SPD_MASK)) ||
1529                     (old_state.custom_divisor != state->custom_divisor)) {
1530                         if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1531                                 info->tty->alt_speed = 57600;
1532                         if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1533                                 info->tty->alt_speed = 115200;
1534                         if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1535                                 info->tty->alt_speed = 230400;
1536                         if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1537                                 info->tty->alt_speed = 460800;
1538                         change_speed(info, 0);
1539                 }
1540         } else {
1541                 retval = startup(info);
1542         }
1543         return retval;
1544 }
1545
1546
1547 /*
1548  * get_lsr_info - get line status register info
1549  *
1550  * Purpose: Let user call ioctl() to get info when the UART physically
1551  *          is emptied.  On bus types like RS485, the transmitter must
1552  *          release the bus after transmitting. This must be done when
1553  *          the transmit shift register is empty, not be done when the
1554  *          transmit holding register is empty.  This functionality
1555  *          allows an RS485 driver to be written in user space. 
1556  */
1557 static int get_lsr_info(struct async_struct * info, unsigned int *value)
1558 {
1559         unsigned char status;
1560         unsigned int result;
1561         unsigned long flags;
1562
1563         save_flags(flags); cli();
1564         status = serial_in(info, UART_LSR);
1565         restore_flags(flags);
1566         result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1567
1568         /*
1569          * If we're about to load something into the transmit
1570          * register, we'll pretend the transmitter isn't empty to
1571          * avoid a race condition (depending on when the transmit
1572          * interrupt happens).
1573          */
1574         if (info->x_char || 
1575             ((CIRC_CNT(info->xmit.head, info->xmit.tail,
1576                        SERIAL_XMIT_SIZE) > 0) &&
1577              !info->tty->stopped && !info->tty->hw_stopped))
1578                 result &= TIOCSER_TEMT;
1579
1580         if (copy_to_user(value, &result, sizeof(int)))
1581                 return -EFAULT;
1582         return 0;
1583 }
1584
1585
1586 static int get_modem_info(struct async_struct * info, unsigned int *value)
1587 {
1588         unsigned char control, status;
1589         unsigned int result;
1590         unsigned long flags;
1591
1592         control = info->MCR;
1593         save_flags(flags); cli();
1594         status = serial_in(info, UART_MSR);
1595         restore_flags(flags);
1596         result =  ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1597                 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1598 #ifdef TIOCM_OUT1
1599                 | ((control & UART_MCR_OUT1) ? TIOCM_OUT1 : 0)
1600                 | ((control & UART_MCR_OUT2) ? TIOCM_OUT2 : 0)
1601 #endif
1602                 | ((status  & UART_MSR_DCD) ? TIOCM_CAR : 0)
1603                 | ((status  & UART_MSR_RI) ? TIOCM_RNG : 0)
1604                 | ((status  & UART_MSR_DSR) ? TIOCM_DSR : 0)
1605                 | ((status  & UART_MSR_CTS) ? TIOCM_CTS : 0);
1606
1607         if (copy_to_user(value, &result, sizeof(int)))
1608                 return -EFAULT;
1609         return 0;
1610 }
1611
1612 static int set_modem_info(struct async_struct * info, unsigned int cmd,
1613                           unsigned int *value)
1614 {
1615         unsigned int arg;
1616         unsigned long flags;
1617
1618         if (copy_from_user(&arg, value, sizeof(int)))
1619                 return -EFAULT;
1620
1621         switch (cmd) {
1622         case TIOCMBIS: 
1623                 if (arg & TIOCM_RTS)
1624                         info->MCR |= UART_MCR_RTS;
1625                 if (arg & TIOCM_DTR)
1626                         info->MCR |= UART_MCR_DTR;
1627 #ifdef TIOCM_OUT1
1628                 if (arg & TIOCM_OUT1)
1629                         info->MCR |= UART_MCR_OUT1;
1630                 if (arg & TIOCM_OUT2)
1631                         info->MCR |= UART_MCR_OUT2;
1632 #endif
1633                 if (arg & TIOCM_LOOP)
1634                         info->MCR |= UART_MCR_LOOP;
1635                 break;
1636         case TIOCMBIC:
1637                 if (arg & TIOCM_RTS)
1638                         info->MCR &= ~UART_MCR_RTS;
1639                 if (arg & TIOCM_DTR)
1640                         info->MCR &= ~UART_MCR_DTR;
1641 #ifdef TIOCM_OUT1
1642                 if (arg & TIOCM_OUT1)
1643                         info->MCR &= ~UART_MCR_OUT1;
1644                 if (arg & TIOCM_OUT2)
1645                         info->MCR &= ~UART_MCR_OUT2;
1646 #endif
1647                 if (arg & TIOCM_LOOP)
1648                         info->MCR &= ~UART_MCR_LOOP;
1649                 break;
1650         case TIOCMSET:
1651                 info->MCR = ((info->MCR & ~(UART_MCR_RTS |
1652 #ifdef TIOCM_OUT1
1653                                             UART_MCR_OUT1 |
1654                                             UART_MCR_OUT2 |
1655 #endif
1656                                             UART_MCR_LOOP |
1657                                             UART_MCR_DTR))
1658                              | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1659 #ifdef TIOCM_OUT1
1660                              | ((arg & TIOCM_OUT1) ? UART_MCR_OUT1 : 0)
1661                              | ((arg & TIOCM_OUT2) ? UART_MCR_OUT2 : 0)
1662 #endif
1663                              | ((arg & TIOCM_LOOP) ? UART_MCR_LOOP : 0)
1664                              | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1665                 break;
1666         default:
1667                 return -EINVAL;
1668         }
1669         save_flags(flags); cli();
1670         info->MCR |= ALPHA_KLUDGE_MCR;          /* Don't ask */
1671         serial_out(info, UART_MCR, info->MCR);
1672         restore_flags(flags);
1673         return 0;
1674 }
1675
1676 static int do_autoconfig(struct async_struct * info)
1677 {
1678         int retval;
1679         
1680         if (!capable(CAP_SYS_ADMIN))
1681                 return -EPERM;
1682         
1683         if (info->state->count > 1)
1684                 return -EBUSY;
1685         
1686         shutdown(info);
1687
1688         autoconfig(info->state);
1689         retval = startup(info);
1690         if (retval)
1691                 return retval;
1692         return 0;
1693 }
1694
1695 /*
1696  * rs_break() --- routine which turns the break handling on or off
1697  */
1698 static void rs_break(struct tty_struct *tty, int break_state)
1699 {
1700         struct async_struct * info = (struct async_struct *)tty->driver_data;
1701         unsigned long flags;
1702         
1703         if (serial_paranoia_check(info, tty->device, "rs_break"))
1704                 return;
1705
1706         if (!CONFIGURED_SERIAL_PORT(info))
1707                 return;
1708         save_flags(flags); cli();
1709         if (break_state == -1)
1710                 info->LCR |= UART_LCR_SBC;
1711         else
1712                 info->LCR &= ~UART_LCR_SBC;
1713         serial_out(info, UART_LCR, info->LCR);
1714         restore_flags(flags);
1715 }
1716
1717
1718 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1719                     unsigned int cmd, unsigned long arg)
1720 {
1721         struct async_struct * info = (struct async_struct *)tty->driver_data;
1722         struct async_icount cprev, cnow;        /* kernel counter temps */
1723         struct serial_icounter_struct icount;
1724         unsigned long flags;
1725         
1726         if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1727                 return -ENODEV;
1728
1729         if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1730             (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1731             (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1732                 if (tty->flags & (1 << TTY_IO_ERROR))
1733                     return -EIO;
1734         }
1735         
1736         switch (cmd) {
1737                 case TIOCMGET:
1738                         return get_modem_info(info, (unsigned int *) arg);
1739                 case TIOCMBIS:
1740                 case TIOCMBIC:
1741                 case TIOCMSET:
1742                         return set_modem_info(info, cmd, (unsigned int *) arg);
1743                 case TIOCGSERIAL:
1744                         return get_serial_info(info,
1745                                                (struct serial_struct *) arg);
1746                 case TIOCSSERIAL:
1747                         return set_serial_info(info,
1748                                                (struct serial_struct *) arg);
1749                 case TIOCSERCONFIG:
1750                         return do_autoconfig(info);
1751
1752                 case TIOCSERGETLSR: /* Get line status register */
1753                         return get_lsr_info(info, (unsigned int *) arg);
1754
1755                 case TIOCSERGSTRUCT:
1756                         if (copy_to_user((struct async_struct *) arg,
1757                                          info, sizeof(struct async_struct)))
1758                                 return -EFAULT;
1759                         return 0;
1760                                 
1761                         
1762                 /*
1763                  * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1764                  * - mask passed in arg for lines of interest
1765                  *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1766                  * Caller should use TIOCGICOUNT to see which one it was
1767                  */
1768                 case TIOCMIWAIT:
1769                         save_flags(flags); cli();
1770                         /* note the counters on entry */
1771                         cprev = info->state->icount;
1772                         restore_flags(flags);
1773                         /* Force modem status interrupts on */
1774                         info->IER |= UART_IER_MSI;
1775                         serial_out(info, UART_IER, info->IER);
1776                         while (1) {
1777                                 interruptible_sleep_on(&info->delta_msr_wait);
1778                                 /* see if a signal did it */
1779                                 if (signal_pending(current))
1780                                         return -ERESTARTSYS;
1781                                 save_flags(flags); cli();
1782                                 cnow = info->state->icount; /* atomic copy */
1783                                 restore_flags(flags);
1784                                 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr && 
1785                                     cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1786                                         return -EIO; /* no change => error */
1787                                 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1788                                      ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1789                                      ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
1790                                      ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1791                                         return 0;
1792                                 }
1793                                 cprev = cnow;
1794                         }
1795                         /* NOTREACHED */
1796
1797                 /* 
1798                  * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1799                  * Return: write counters to the user passed counter struct
1800                  * NB: both 1->0 and 0->1 transitions are counted except for
1801                  *     RI where only 0->1 is counted.
1802                  */
1803                 case TIOCGICOUNT:
1804                         save_flags(flags); cli();
1805                         cnow = info->state->icount;
1806                         restore_flags(flags);
1807                         icount.cts = cnow.cts;
1808                         icount.dsr = cnow.dsr;
1809                         icount.rng = cnow.rng;
1810                         icount.dcd = cnow.dcd;
1811                         icount.rx = cnow.rx;
1812                         icount.tx = cnow.tx;
1813                         icount.frame = cnow.frame;
1814                         icount.overrun = cnow.overrun;
1815                         icount.parity = cnow.parity;
1816                         icount.brk = cnow.brk;
1817                         icount.buf_overrun = cnow.buf_overrun;
1818                         
1819                         if (copy_to_user((void *)arg, &icount, sizeof(icount)))
1820                                 return -EFAULT;
1821                         return 0;
1822                 case TIOCSERGWILD:
1823                 case TIOCSERSWILD:
1824                         /* "setserial -W" is called in Debian boot */
1825                         printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1826                         return 0;
1827
1828                 default:
1829                         return -ENOIOCTLCMD;
1830                 }
1831         return 0;
1832 }
1833
1834 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1835 {
1836         struct async_struct *info = (struct async_struct *)tty->driver_data;
1837         unsigned long flags;
1838         unsigned int cflag = tty->termios->c_cflag;
1839         
1840         if (   (cflag == old_termios->c_cflag)
1841             && (   RELEVANT_IFLAG(tty->termios->c_iflag) 
1842                 == RELEVANT_IFLAG(old_termios->c_iflag)))
1843           return;
1844
1845         change_speed(info, old_termios);
1846
1847         /* Handle transition to B0 status */
1848         if ((old_termios->c_cflag & CBAUD) &&
1849             !(cflag & CBAUD)) {
1850                 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1851                 save_flags(flags); cli();
1852                 serial_out(info, UART_MCR, info->MCR);
1853                 restore_flags(flags);
1854         }
1855         
1856         /* Handle transition away from B0 status */
1857         if (!(old_termios->c_cflag & CBAUD) &&
1858             (cflag & CBAUD)) {
1859                 info->MCR |= UART_MCR_DTR;
1860                 if (!(tty->termios->c_cflag & CRTSCTS) || 
1861                     !test_bit(TTY_THROTTLED, &tty->flags)) {
1862                         info->MCR |= UART_MCR_RTS;
1863                 }
1864                 save_flags(flags); cli();
1865                 serial_out(info, UART_MCR, info->MCR);
1866                 restore_flags(flags);
1867         }
1868         
1869         /* Handle turning off CRTSCTS */
1870         if ((old_termios->c_cflag & CRTSCTS) &&
1871             !(tty->termios->c_cflag & CRTSCTS)) {
1872                 tty->hw_stopped = 0;
1873                 rs_start(tty);
1874         }
1875 }
1876
1877 /*
1878  * ------------------------------------------------------------
1879  * rs_close()
1880  * 
1881  * This routine is called when the serial port gets closed.  First, we
1882  * wait for the last remaining data to be sent.  Then, we unlink its
1883  * async structure from the interrupt chain if necessary, and we free
1884  * that IRQ if nothing is left in the chain.
1885  * ------------------------------------------------------------
1886  */
1887 static void rs_close(struct tty_struct *tty, struct file * filp)
1888 {
1889         struct async_struct * info = (struct async_struct *)tty->driver_data;
1890         struct serial_state *state;
1891         unsigned long flags;
1892
1893         if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1894                 return;
1895
1896         state = info->state;
1897         
1898         save_flags(flags); cli();
1899         
1900         if (tty_hung_up_p(filp)) {
1901                 DBG_CNT("before DEC-hung");
1902                 MOD_DEC_USE_COUNT;
1903                 restore_flags(flags);
1904                 return;
1905         }
1906         
1907 #ifdef SERIAL_DEBUG_OPEN
1908         printk("rs_close ttys%d, count = %d\n", info->line, state->count);
1909 #endif
1910         if ((tty->count == 1) && (state->count != 1)) {
1911                 /*
1912                  * Uh, oh.  tty->count is 1, which means that the tty
1913                  * structure will be freed.  state->count should always
1914                  * be one in these conditions.  If it's greater than
1915                  * one, we've got real problems, since it means the
1916                  * serial port won't be shutdown.
1917                  */
1918                 printk("rs_close: bad serial port count; tty->count is 1, "
1919                        "state->count is %d\n", state->count);
1920                 state->count = 1;
1921         }
1922         if (--state->count < 0) {
1923                 printk("rs_close: bad serial port count for ttys%d: %d\n",
1924                        info->line, state->count);
1925                 state->count = 0;
1926         }
1927         if (state->count) {
1928                 DBG_CNT("before DEC-2");
1929                 MOD_DEC_USE_COUNT;
1930                 restore_flags(flags);
1931                 return;
1932         }
1933         info->flags |= ASYNC_CLOSING;
1934         restore_flags(flags);
1935         /*
1936          * Save the termios structure, since this port may have
1937          * separate termios for callout and dialin.
1938          */
1939         if (info->flags & ASYNC_NORMAL_ACTIVE)
1940                 info->state->normal_termios = *tty->termios;
1941         if (info->flags & ASYNC_CALLOUT_ACTIVE)
1942                 info->state->callout_termios = *tty->termios;
1943         /*
1944          * Now we wait for the transmit buffer to clear; and we notify 
1945          * the line discipline to only process XON/XOFF characters.
1946          */
1947         tty->closing = 1;
1948         if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1949                 tty_wait_until_sent(tty, info->closing_wait);
1950         /*
1951          * At this point we stop accepting input.  To do this, we
1952          * disable the receive line status interrupts, and tell the
1953          * interrupt driver to stop checking the data ready bit in the
1954          * line status register.
1955          */
1956         info->IER &= ~UART_IER_RLSI;
1957         info->read_status_mask &= ~UART_LSR_DR;
1958         if (info->flags & ASYNC_INITIALIZED) {
1959                 serial_out(info, UART_IER, info->IER);
1960                 /*
1961                  * Before we drop DTR, make sure the UART transmitter
1962                  * has completely drained; this is especially
1963                  * important if there is a transmit FIFO!
1964                  */
1965                 rs_wait_until_sent(tty, info->timeout);
1966         }
1967         shutdown(info);
1968         if (tty->driver.flush_buffer)
1969                 tty->driver.flush_buffer(tty);
1970         if (tty->ldisc.flush_buffer)
1971                 tty->ldisc.flush_buffer(tty);
1972         tty->closing = 0;
1973         info->event = 0;
1974         info->tty = 0;
1975         if (info->blocked_open) {
1976                 if (info->close_delay) {
1977                         set_current_state(TASK_INTERRUPTIBLE);
1978                         schedule_timeout(info->close_delay);
1979                 }
1980                 wake_up_interruptible(&info->open_wait);
1981         }
1982         info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1983                          ASYNC_CLOSING);
1984         wake_up_interruptible(&info->close_wait);
1985         MOD_DEC_USE_COUNT;
1986 }
1987
1988 /*
1989  * rs_wait_until_sent() --- wait until the transmitter is empty
1990  */
1991 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1992 {
1993         struct async_struct * info = (struct async_struct *)tty->driver_data;
1994         unsigned long orig_jiffies, char_time;
1995         int lsr;
1996         
1997         if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
1998                 return;
1999
2000         if (info->state->type == PORT_UNKNOWN)
2001                 return;
2002
2003         if (info->xmit_fifo_size == 0)
2004                 return; /* Just in case.... */
2005
2006         orig_jiffies = jiffies;
2007         /*
2008          * Set the check interval to be 1/5 of the estimated time to
2009          * send a single character, and make it at least 1.  The check
2010          * interval should also be less than the timeout.
2011          * 
2012          * Note: we have to use pretty tight timings here to satisfy
2013          * the NIST-PCTS.
2014          */
2015         char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
2016         char_time = char_time / 5;
2017         if (char_time == 0)
2018                 char_time = 1;
2019         if (timeout && timeout < char_time)
2020                 char_time = timeout;
2021         /*
2022          * If the transmitter hasn't cleared in twice the approximate
2023          * amount of time to send the entire FIFO, it probably won't
2024          * ever clear.  This assumes the UART isn't doing flow
2025          * control, which is currently the case.  Hence, if it ever
2026          * takes longer than info->timeout, this is probably due to a
2027          * UART bug of some kind.  So, we clamp the timeout parameter at
2028          * 2*info->timeout.
2029          */
2030         if (!timeout || timeout > 2*info->timeout)
2031                 timeout = 2*info->timeout;
2032 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2033         printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
2034         printk("jiff=%lu...", jiffies);
2035 #endif
2036         while (!((lsr = serial_inp(info, UART_LSR)) & UART_LSR_TEMT)) {
2037 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2038                 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2039 #endif
2040                 set_current_state(TASK_INTERRUPTIBLE);
2041                 schedule_timeout(char_time);
2042                 if (signal_pending(current))
2043                         break;
2044                 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2045                         break;
2046         }
2047         set_current_state(TASK_RUNNING);
2048 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2049         printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2050 #endif
2051 }
2052
2053 /*
2054  * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
2055  */
2056 static void rs_hangup(struct tty_struct *tty)
2057 {
2058         struct async_struct * info = (struct async_struct *)tty->driver_data;
2059         struct serial_state *state = info->state;
2060         
2061         if (serial_paranoia_check(info, tty->device, "rs_hangup"))
2062                 return;
2063
2064         state = info->state;
2065         
2066         rs_flush_buffer(tty);
2067         if (info->flags & ASYNC_CLOSING)
2068                 return;
2069         shutdown(info);
2070         info->event = 0;
2071         state->count = 0;
2072         info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
2073         info->tty = 0;
2074         wake_up_interruptible(&info->open_wait);
2075 }
2076
2077 /*
2078  * ------------------------------------------------------------
2079  * rs_open() and friends
2080  * ------------------------------------------------------------
2081  */
2082 static int block_til_ready(struct tty_struct *tty, struct file * filp,
2083                            struct async_struct *info)
2084 {
2085         DECLARE_WAITQUEUE(wait, current);
2086         struct serial_state *state = info->state;
2087         int             retval;
2088         int             do_clocal = 0, extra_count = 0;
2089         unsigned long   flags;
2090
2091         /*
2092          * If the device is in the middle of being closed, then block
2093          * until it's done, and then try again.
2094          */
2095         if (tty_hung_up_p(filp) ||
2096             (info->flags & ASYNC_CLOSING)) {
2097                 if (info->flags & ASYNC_CLOSING)
2098                         interruptible_sleep_on(&info->close_wait);
2099 #ifdef SERIAL_DO_RESTART
2100                 return ((info->flags & ASYNC_HUP_NOTIFY) ?
2101                         -EAGAIN : -ERESTARTSYS);
2102 #else
2103                 return -EAGAIN;
2104 #endif
2105         }
2106
2107         /*
2108          * If this is a callout device, then just make sure the normal
2109          * device isn't being used.
2110          */
2111         if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
2112                 if (info->flags & ASYNC_NORMAL_ACTIVE)
2113                         return -EBUSY;
2114                 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2115                     (info->flags & ASYNC_SESSION_LOCKOUT) &&
2116                     (info->session != current->session))
2117                     return -EBUSY;
2118                 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2119                     (info->flags & ASYNC_PGRP_LOCKOUT) &&
2120                     (info->pgrp != current->pgrp))
2121                     return -EBUSY;
2122                 info->flags |= ASYNC_CALLOUT_ACTIVE;
2123                 return 0;
2124         }
2125         
2126         /*
2127          * If non-blocking mode is set, or the port is not enabled,
2128          * then make the check up front and then exit.
2129          */
2130         if ((filp->f_flags & O_NONBLOCK) ||
2131             (tty->flags & (1 << TTY_IO_ERROR))) {
2132                 if (info->flags & ASYNC_CALLOUT_ACTIVE)
2133                         return -EBUSY;
2134                 info->flags |= ASYNC_NORMAL_ACTIVE;
2135                 return 0;
2136         }
2137
2138         if (info->flags & ASYNC_CALLOUT_ACTIVE) {
2139                 if (state->normal_termios.c_cflag & CLOCAL)
2140                         do_clocal = 1;
2141         } else {
2142                 if (tty->termios->c_cflag & CLOCAL)
2143                         do_clocal = 1;
2144         }
2145         
2146         /*
2147          * Block waiting for the carrier detect and the line to become
2148          * free (i.e., not in use by the callout).  While we are in
2149          * this loop, state->count is dropped by one, so that
2150          * rs_close() knows when to free things.  We restore it upon
2151          * exit, either normal or abnormal.
2152          */
2153         retval = 0;
2154         add_wait_queue(&info->open_wait, &wait);
2155 #ifdef SERIAL_DEBUG_OPEN
2156         printk("block_til_ready before block: ttys%d, count = %d\n",
2157                state->line, state->count);
2158 #endif
2159         save_flags(flags); cli();
2160         if (!tty_hung_up_p(filp)) {
2161                 extra_count = 1;
2162                 state->count--;
2163         }
2164         restore_flags(flags);
2165         info->blocked_open++;
2166         while (1) {
2167                 save_flags(flags); cli();
2168                 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2169                     (tty->termios->c_cflag & CBAUD))
2170                         serial_out(info, UART_MCR,
2171                                    serial_inp(info, UART_MCR) |
2172                                    (UART_MCR_DTR | UART_MCR_RTS));
2173                 restore_flags(flags);
2174                 set_current_state(TASK_INTERRUPTIBLE);
2175                 if (tty_hung_up_p(filp) ||
2176                     !(info->flags & ASYNC_INITIALIZED)) {
2177 #ifdef SERIAL_DO_RESTART
2178                         if (info->flags & ASYNC_HUP_NOTIFY)
2179                                 retval = -EAGAIN;
2180                         else
2181                                 retval = -ERESTARTSYS;  
2182 #else
2183                         retval = -EAGAIN;
2184 #endif
2185                         break;
2186                 }
2187                 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2188                     !(info->flags & ASYNC_CLOSING) &&
2189                     (do_clocal || (serial_in(info, UART_MSR) &
2190                                    UART_MSR_DCD)))
2191                         break;
2192                 if (signal_pending(current)) {
2193                         retval = -ERESTARTSYS;
2194                         break;
2195                 }
2196 #ifdef SERIAL_DEBUG_OPEN
2197                 printk("block_til_ready blocking: ttys%d, count = %d\n",
2198                        info->line, state->count);
2199 #endif
2200                 schedule();
2201         }
2202         set_current_state(TASK_RUNNING);
2203         remove_wait_queue(&info->open_wait, &wait);
2204         if (extra_count)
2205                 state->count++;
2206         info->blocked_open--;
2207 #ifdef SERIAL_DEBUG_OPEN
2208         printk("block_til_ready after blocking: ttys%d, count = %d\n",
2209                info->line, state->count);
2210 #endif
2211         if (retval)
2212                 return retval;
2213         info->flags |= ASYNC_NORMAL_ACTIVE;
2214         return 0;
2215 }
2216
2217 static int get_async_struct(int line, struct async_struct **ret_info)
2218 {
2219         struct async_struct *info;
2220         struct serial_state *sstate;
2221
2222         sstate = rs_table + line;
2223         sstate->count++;
2224         if (sstate->info) {
2225                 *ret_info = sstate->info;
2226                 return 0;
2227         }
2228         info = kmalloc(sizeof(struct async_struct), GFP_KERNEL);
2229         if (!info) {
2230                 sstate->count--;
2231                 return -ENOMEM;
2232         }
2233         memset(info, 0, sizeof(struct async_struct));
2234         init_waitqueue_head(&info->open_wait);
2235         init_waitqueue_head(&info->close_wait);
2236         init_waitqueue_head(&info->delta_msr_wait);
2237         info->magic = SERIAL_MAGIC;
2238         info->port = sstate->port;
2239         info->flags = sstate->flags;
2240         info->io_type = sstate->io_type;
2241         info->iomem_base = sstate->iomem_base;
2242         info->iomem_reg_shift = sstate->iomem_reg_shift;
2243         info->xmit_fifo_size = sstate->xmit_fifo_size;
2244         info->line = line;
2245         info->tqueue.routine = do_softint;
2246         info->tqueue.data = info;
2247         info->state = sstate;
2248         if (sstate->info) {
2249                 kfree(info);
2250                 *ret_info = sstate->info;
2251                 return 0;
2252         }
2253         *ret_info = sstate->info = info;
2254         return 0;
2255 }
2256
2257 /*
2258  * This routine is called whenever a serial port is opened.  It
2259  * enables interrupts for a serial port, linking in its async structure into
2260  * the IRQ chain.   It also performs the serial-specific
2261  * initialization for the tty structure.
2262  */
2263 static int rs_open(struct tty_struct *tty, struct file * filp)
2264 {
2265         struct async_struct     *info;
2266         int                     retval, line;
2267         unsigned long           page;
2268
2269         MOD_INC_USE_COUNT;
2270         line = MINOR(tty->device) - tty->driver.minor_start;
2271         if ((line < 0) || (line >= NR_PORTS)) {
2272                 MOD_DEC_USE_COUNT;
2273                 return -ENODEV;
2274         }
2275         retval = get_async_struct(line, &info);
2276         if (retval) {
2277                 MOD_DEC_USE_COUNT;
2278                 return retval;
2279         }
2280         tty->driver_data = info;
2281         info->tty = tty;
2282         if (serial_paranoia_check(info, tty->device, "rs_open")) {
2283                 MOD_DEC_USE_COUNT;              
2284                 return -ENODEV;
2285         }
2286
2287 #ifdef SERIAL_DEBUG_OPEN
2288         printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
2289                info->state->count);
2290 #endif
2291         info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2292
2293         if (!tmp_buf) {
2294                 page = get_zeroed_page(GFP_KERNEL);
2295                 if (!page) {
2296                         MOD_DEC_USE_COUNT;
2297                         return -ENOMEM;
2298                 }
2299                 if (tmp_buf)
2300                         free_page(page);
2301                 else
2302                         tmp_buf = (unsigned char *) page;
2303         }
2304
2305         /*
2306          * If the port is the middle of closing, bail out now
2307          */
2308         if (tty_hung_up_p(filp) ||
2309             (info->flags & ASYNC_CLOSING)) {
2310                 if (info->flags & ASYNC_CLOSING)
2311                         interruptible_sleep_on(&info->close_wait);
2312                 MOD_DEC_USE_COUNT;
2313 #ifdef SERIAL_DO_RESTART
2314                 return ((info->flags & ASYNC_HUP_NOTIFY) ?
2315                         -EAGAIN : -ERESTARTSYS);
2316 #else
2317                 return -EAGAIN;
2318 #endif
2319         }
2320
2321         /*
2322          * Start up serial port
2323          */
2324         retval = startup(info);
2325         if (retval) {
2326                 MOD_DEC_USE_COUNT;
2327                 return retval;
2328         }
2329
2330         retval = block_til_ready(tty, filp, info);
2331         if (retval) {
2332 #ifdef SERIAL_DEBUG_OPEN
2333                 printk("rs_open returning after block_til_ready with %d\n",
2334                        retval);
2335 #endif
2336                 MOD_DEC_USE_COUNT;
2337                 return retval;
2338         }
2339
2340         if ((info->state->count == 1) &&
2341             (info->flags & ASYNC_SPLIT_TERMIOS)) {
2342                 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2343                         *tty->termios = info->state->normal_termios;
2344                 else 
2345                         *tty->termios = info->state->callout_termios;
2346                 change_speed(info, 0);
2347         }
2348 #ifdef CONFIG_AU1000_SERIAL_CONSOLE
2349         if (sercons.cflag && sercons.index == line) {
2350                 tty->termios->c_cflag = sercons.cflag;
2351                 sercons.cflag = 0;
2352                 change_speed(info, 0);
2353         }
2354 #endif
2355         info->session = current->session;
2356         info->pgrp = current->pgrp;
2357
2358 #ifdef SERIAL_DEBUG_OPEN
2359         printk("rs_open ttys%d successful...", info->line);
2360 #endif
2361         return 0;
2362 }
2363
2364 /*
2365  * /proc fs routines....
2366  */
2367
2368 static inline int line_info(char *buf, struct serial_state *state)
2369 {
2370         struct async_struct *info = state->info, scr_info;
2371         char    stat_buf[30], control, status;
2372         int     ret;
2373         unsigned long flags;
2374
2375         ret = sprintf(buf, "%d: uart:%s port:%lX irq:%d",
2376                       state->line, uart_config[state->type].name, 
2377                       state->port, state->irq);
2378
2379         if (!state->port || (state->type == PORT_UNKNOWN)) {
2380                 ret += sprintf(buf+ret, "\n");
2381                 return ret;
2382         }
2383
2384         /*
2385          * Figure out the current RS-232 lines
2386          */
2387         if (!info) {
2388                 info = &scr_info;       /* This is just for serial_{in,out} */
2389
2390                 info->magic = SERIAL_MAGIC;
2391                 info->port = state->port;
2392                 info->flags = state->flags;
2393                 info->quot = 0;
2394                 info->tty = 0;
2395         }
2396         save_flags(flags); cli();
2397         status = serial_in(info, UART_MSR);
2398         control = info != &scr_info ? info->MCR : serial_in(info, UART_MCR);
2399         restore_flags(flags); 
2400
2401         stat_buf[0] = 0;
2402         stat_buf[1] = 0;
2403         if (control & UART_MCR_RTS)
2404                 strcat(stat_buf, "|RTS");
2405         if (status & UART_MSR_CTS)
2406                 strcat(stat_buf, "|CTS");
2407         if (control & UART_MCR_DTR)
2408                 strcat(stat_buf, "|DTR");
2409         if (status & UART_MSR_DSR)
2410                 strcat(stat_buf, "|DSR");
2411         if (status & UART_MSR_DCD)
2412                 strcat(stat_buf, "|CD");
2413         if (status & UART_MSR_RI)
2414                 strcat(stat_buf, "|RI");
2415
2416         if (info->quot) {
2417                 ret += sprintf(buf+ret, " baud:%d",
2418                                state->baud_base / info->quot);
2419         }
2420
2421         ret += sprintf(buf+ret, " tx:%d rx:%d",
2422                       state->icount.tx, state->icount.rx);
2423
2424         if (state->icount.frame)
2425                 ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
2426         
2427         if (state->icount.parity)
2428                 ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
2429         
2430         if (state->icount.brk)
2431                 ret += sprintf(buf+ret, " brk:%d", state->icount.brk);  
2432
2433         if (state->icount.overrun)
2434                 ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
2435
2436         /*
2437          * Last thing is the RS-232 status lines
2438          */
2439         ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2440         return ret;
2441 }
2442
2443 int rs_read_proc(char *page, char **start, off_t off, int count,
2444                  int *eof, void *data)
2445 {
2446         int i, len = 0, l;
2447         off_t   begin = 0;
2448
2449         len += sprintf(page, "serinfo:1.0 driver:%s%s revision:%s\n",
2450                        serial_version, LOCAL_VERSTRING, serial_revdate);
2451         for (i = 0; i < NR_PORTS && len < 4000; i++) {
2452                 l = line_info(page + len, &rs_table[i]);
2453                 len += l;
2454                 if (len+begin > off+count)
2455                         goto done;
2456                 if (len+begin < off) {
2457                         begin += len;
2458                         len = 0;
2459                 }
2460         }
2461         *eof = 1;
2462 done:
2463         if (off >= len+begin)
2464                 return 0;
2465         *start = page + (off-begin);
2466         return ((count < begin+len-off) ? count : begin+len-off);
2467 }
2468
2469 /*
2470  * ---------------------------------------------------------------------
2471  * rs_init() and friends
2472  *
2473  * rs_init() is called at boot-time to initialize the serial driver.
2474  * ---------------------------------------------------------------------
2475  */
2476
2477 /*
2478  * This routine prints out the appropriate serial driver version
2479  * number, and identifies which options were configured into this
2480  * driver.
2481  */
2482 static char serial_options[] __initdata =
2483        " no serial options enabled\n";
2484 #undef SERIAL_OPT
2485
2486 static _INLINE_ void show_serial_version(void)
2487 {
2488         printk(KERN_INFO "%s version %s%s (%s) with%s", serial_name,
2489                serial_version, LOCAL_VERSTRING, serial_revdate,
2490                serial_options);
2491 }
2492
2493
2494 /*
2495  * This routine is called by rs_init() to initialize a specific serial
2496  * port.  It determines what type of UART chip this serial port is
2497  * using: 8250, 16450, 16550, 16550A.  The important question is
2498  * whether or not this UART is a 16550A or not, since this will
2499  * determine whether or not we can use its FIFO features or not.
2500  */
2501 static void autoconfig(struct serial_state * state)
2502 {
2503         struct async_struct *info, scr_info;
2504         unsigned long flags;
2505
2506
2507 #ifdef SERIAL_DEBUG_AUTOCONF
2508         printk("Testing ttyS%d (0x%04lx, 0x%04x)...\n", state->line,
2509                state->port, (unsigned) state->iomem_base);
2510 #endif
2511         
2512         if (!CONFIGURED_SERIAL_PORT(state))
2513                 return;
2514
2515         if (inl(UART_MOD_CNTRL + state->port) != 0x3) {
2516                 outl(3, UART_MOD_CNTRL + state->port);
2517         }
2518                 
2519         state->type = PORT_16550;
2520         info = &scr_info;       /* This is just for serial_{in,out} */
2521
2522         info->magic = SERIAL_MAGIC;
2523         info->state = state;
2524         info->port = state->port;
2525         info->flags = state->flags;
2526         info->io_type = state->io_type;
2527         info->iomem_base = state->iomem_base;
2528         info->iomem_reg_shift = state->iomem_reg_shift;
2529
2530
2531         save_flags(flags); cli();
2532         state->xmit_fifo_size = uart_config[state->type].dfl_xmit_fifo_size;
2533
2534         if (info->port) {
2535                         request_region(info->port,8,"serial(auto)");
2536         }
2537
2538         /*
2539          * Reset the UART.
2540          */
2541         serial_outp(info, UART_FCR, (UART_FCR_ENABLE_FIFO |
2542                                      UART_FCR_CLEAR_RCVR |
2543                                      UART_FCR_CLEAR_XMIT));
2544         serial_outp(info, UART_FCR, 0);
2545         (void)serial_in(info, UART_RX);
2546         serial_outp(info, UART_IER, 0);
2547         
2548         restore_flags(flags);
2549 }
2550
2551 int register_serial(struct serial_struct *req);
2552 void unregister_serial(int line);
2553
2554 EXPORT_SYMBOL(register_serial);
2555 EXPORT_SYMBOL(unregister_serial);
2556
2557
2558 /*
2559  * The serial driver boot-time initialization code!
2560  */
2561 static int __init rs_init(void)
2562 {
2563         int i;
2564         struct serial_state * state;
2565
2566         init_bh(SERIAL_BH, do_serial_bh);
2567         init_timer(&serial_timer);
2568         serial_timer.function = rs_timer;
2569         mod_timer(&serial_timer, jiffies + RS_STROBE_TIME);
2570
2571         for (i = 0; i < NR_IRQS; i++) {
2572                 IRQ_ports[i] = 0;
2573                 IRQ_timeout[i] = 0;
2574         }
2575 #ifdef CONFIG_AU1000_SERIAL_CONSOLE
2576         /*
2577          *      The interrupt of the serial console port
2578          *      can't be shared.
2579          */
2580         if (sercons.flags & CON_CONSDEV) {
2581                 for(i = 0; i < NR_PORTS; i++)
2582                         if (i != sercons.index &&
2583                             rs_table[i].irq == rs_table[sercons.index].irq)
2584                                 rs_table[i].irq = 0;
2585         }
2586 #endif
2587         show_serial_version();
2588
2589         /* Initialize the tty_driver structure */
2590         
2591         memset(&serial_driver, 0, sizeof(struct tty_driver));
2592         serial_driver.magic = TTY_DRIVER_MAGIC;
2593         serial_driver.driver_name = "serial";
2594 #if (LINUX_VERSION_CODE > 0x2032D && defined(CONFIG_DEVFS_FS))
2595         serial_driver.name = "tts/%d";
2596 #else
2597         serial_driver.name = "ttyS";
2598 #endif
2599         serial_driver.major = TTY_MAJOR;
2600         serial_driver.minor_start = 64 + SERIAL_DEV_OFFSET;
2601         serial_driver.num = NR_PORTS;
2602         serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2603         serial_driver.subtype = SERIAL_TYPE_NORMAL;
2604         serial_driver.init_termios = tty_std_termios;
2605         serial_driver.init_termios.c_cflag =
2606                 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2607         serial_driver.flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
2608         serial_driver.refcount = &serial_refcount;
2609         serial_driver.table = serial_table;
2610         serial_driver.termios = serial_termios;
2611         serial_driver.termios_locked = serial_termios_locked;
2612
2613         serial_driver.open = rs_open;
2614         serial_driver.close = rs_close;
2615         serial_driver.write = rs_write;
2616         serial_driver.put_char = rs_put_char;
2617         serial_driver.flush_chars = rs_flush_chars;
2618         serial_driver.write_room = rs_write_room;
2619         serial_driver.chars_in_buffer = rs_chars_in_buffer;
2620         serial_driver.flush_buffer = rs_flush_buffer;
2621         serial_driver.ioctl = rs_ioctl;
2622         serial_driver.throttle = rs_throttle;
2623         serial_driver.unthrottle = rs_unthrottle;
2624         serial_driver.set_termios = rs_set_termios;
2625         serial_driver.stop = rs_stop;
2626         serial_driver.start = rs_start;
2627         serial_driver.hangup = rs_hangup;
2628         serial_driver.break_ctl = rs_break;
2629         serial_driver.send_xchar = rs_send_xchar;
2630         serial_driver.wait_until_sent = rs_wait_until_sent;
2631         serial_driver.read_proc = rs_read_proc;
2632         
2633         /*
2634          * The callout device is just like normal device except for
2635          * major number and the subtype code.
2636          */
2637         callout_driver = serial_driver;
2638 #if (LINUX_VERSION_CODE > 0x2032D && defined(CONFIG_DEVFS_FS))
2639         callout_driver.name = "cua/%d";
2640 #else
2641         callout_driver.name = "cua";
2642 #endif
2643         callout_driver.major = TTYAUX_MAJOR;
2644         callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2645         callout_driver.read_proc = 0;
2646         callout_driver.proc_entry = 0;
2647
2648         if (tty_register_driver(&serial_driver))
2649                 panic("Couldn't register serial driver\n");
2650         if (tty_register_driver(&callout_driver))
2651                 panic("Couldn't register callout driver\n");
2652         
2653         for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
2654                 state->baud_base = get_au1000_uart_baud();
2655                 state->magic = SSTATE_MAGIC;
2656                 state->line = i;
2657                 state->type = PORT_UNKNOWN;
2658                 state->custom_divisor = 0;
2659                 state->close_delay = 5*HZ/10;
2660                 state->closing_wait = 30*HZ;
2661                 state->callout_termios = callout_driver.init_termios;
2662                 state->normal_termios = serial_driver.init_termios;
2663                 state->icount.cts = state->icount.dsr = 
2664                         state->icount.rng = state->icount.dcd = 0;
2665                 state->icount.rx = state->icount.tx = 0;
2666                 state->icount.frame = state->icount.parity = 0;
2667                 state->icount.overrun = state->icount.brk = 0;
2668                 state->irq = irq_cannonicalize(state->irq);
2669                 if (state->hub6)
2670                         state->io_type = SERIAL_IO_HUB6;
2671                 if (state->port && check_region(state->port,8)) {
2672                         continue;
2673                 }
2674
2675                 if (state->flags & ASYNC_BOOT_AUTOCONF) {
2676                         autoconfig(state);
2677                 }
2678         }
2679         for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
2680                 if (state->type == PORT_UNKNOWN) {
2681                         continue;
2682                 }
2683                 printk(KERN_INFO "ttyS%02d%s at 0x%04lx (irq = %d) is a %s\n",
2684                        state->line + SERIAL_DEV_OFFSET,
2685                        (state->flags & ASYNC_FOURPORT) ? " FourPort" : "",
2686                        state->port, state->irq,
2687                        uart_config[state->type].name);
2688                 tty_register_devfs(&serial_driver, 0,
2689                                    serial_driver.minor_start + state->line);
2690                 tty_register_devfs(&callout_driver, 0,
2691                                    callout_driver.minor_start + state->line);
2692         }
2693         return 0;
2694 }
2695
2696 /*
2697  * register_serial and unregister_serial allows for 16x50 serial ports to be
2698  * configured at run-time, to support PCMCIA modems.
2699  */
2700  
2701 /**
2702  *      register_serial - configure a 16x50 serial port at runtime
2703  *      @req: request structure
2704  *
2705  *      Configure the serial port specified by the request. If the
2706  *      port exists and is in use an error is returned. If the port
2707  *      is not currently in the table it is added.
2708  *
2709  *      The port is then probed and if neccessary the IRQ is autodetected
2710  *      If this fails an error is returned.
2711  *
2712  *      On success the port is ready to use and the line number is returned.
2713  */
2714  
2715 int register_serial(struct serial_struct *req)
2716 {
2717         int i;
2718         unsigned long flags;
2719         struct serial_state *state;
2720         struct async_struct *info;
2721         unsigned long port;
2722
2723         port = req->port;
2724         if (HIGH_BITS_OFFSET)
2725                 port += (unsigned long) req->port_high << HIGH_BITS_OFFSET;
2726
2727         save_flags(flags); cli();
2728         for (i = 0; i < NR_PORTS; i++) {
2729                 if ((rs_table[i].port == port) &&
2730                     (rs_table[i].iomem_base == req->iomem_base))
2731                         break;
2732         }
2733         if (i == NR_PORTS) {
2734                 for (i = 0; i < NR_PORTS; i++)
2735                         if ((rs_table[i].type == PORT_UNKNOWN) &&
2736                             (rs_table[i].count == 0))
2737                                 break;
2738         }
2739         if (i == NR_PORTS) {
2740                 restore_flags(flags);
2741                 return -1;
2742         }
2743         state = &rs_table[i];
2744         if (rs_table[i].count) {
2745                 restore_flags(flags);
2746                 printk("Couldn't configure serial #%d (port=%ld,irq=%d): "
2747                        "device already open\n", i, port, req->irq);
2748                 return -1;
2749         }
2750         state->irq = req->irq;
2751         state->port = port;
2752         state->flags = req->flags;
2753         state->io_type = req->io_type;
2754         state->iomem_base = req->iomem_base;
2755         state->iomem_reg_shift = req->iomem_reg_shift;
2756         if (req->baud_base)
2757                 state->baud_base = req->baud_base;
2758         if ((info = state->info) != NULL) {
2759                 info->port = port;
2760                 info->flags = req->flags;
2761                 info->io_type = req->io_type;
2762                 info->iomem_base = req->iomem_base;
2763                 info->iomem_reg_shift = req->iomem_reg_shift;
2764         }
2765         autoconfig(state);
2766         if (state->type == PORT_UNKNOWN) {
2767                 restore_flags(flags);
2768                 printk("register_serial(): autoconfig failed\n");
2769                 return -1;
2770         }
2771         restore_flags(flags);
2772
2773        printk(KERN_INFO "ttyS%02d at %s 0x%04lx (irq = %d) is a %s\n",
2774               state->line + SERIAL_DEV_OFFSET,
2775               state->iomem_base ? "iomem" : "port",
2776               state->iomem_base ? (unsigned long)state->iomem_base :
2777               state->port, state->irq, uart_config[state->type].name);
2778         tty_register_devfs(&serial_driver, 0,
2779                            serial_driver.minor_start + state->line); 
2780         tty_register_devfs(&callout_driver, 0,
2781                            callout_driver.minor_start + state->line);
2782         return state->line + SERIAL_DEV_OFFSET;
2783 }
2784
2785 /**
2786  *      unregister_serial - deconfigure a 16x50 serial port
2787  *      @line: line to deconfigure
2788  *
2789  *      The port specified is deconfigured and its resources are freed. Any
2790  *      user of the port is disconnected as if carrier was dropped. Line is
2791  *      the port number returned by register_serial().
2792  */
2793
2794 void unregister_serial(int line)
2795 {
2796         unsigned long flags;
2797         struct serial_state *state = &rs_table[line];
2798
2799         save_flags(flags); cli();
2800         if (state->info && state->info->tty)
2801                 tty_hangup(state->info->tty);
2802         state->type = PORT_UNKNOWN;
2803         printk(KERN_INFO "tty%02d unloaded\n", state->line);
2804         /* These will be hidden, because they are devices that will no longer
2805          * be available to the system. (ie, PCMCIA modems, once ejected)
2806          */
2807         tty_unregister_devfs(&serial_driver,
2808                              serial_driver.minor_start + state->line);
2809         tty_unregister_devfs(&callout_driver,
2810                              callout_driver.minor_start + state->line);
2811         restore_flags(flags);
2812 }
2813
2814 static void __exit rs_fini(void) 
2815 {
2816         unsigned long flags;
2817         int e1, e2;
2818         int i;
2819         struct async_struct *info;
2820
2821         /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2822         del_timer_sync(&serial_timer);
2823         save_flags(flags); cli();
2824         remove_bh(SERIAL_BH);
2825         if ((e1 = tty_unregister_driver(&serial_driver)))
2826                 printk("serial: failed to unregister serial driver (%d)\n",
2827                        e1);
2828         if ((e2 = tty_unregister_driver(&callout_driver)))
2829                 printk("serial: failed to unregister callout driver (%d)\n", 
2830                        e2);
2831         restore_flags(flags);
2832
2833         for (i = 0; i < NR_PORTS; i++) {
2834                 if ((info = rs_table[i].info)) {
2835                         rs_table[i].info = NULL;
2836                         kfree(info);
2837                 }
2838                 if ((rs_table[i].type != PORT_UNKNOWN) && rs_table[i].port) {
2839                                 release_region(rs_table[i].port, 8);
2840                 }
2841         }
2842         if (tmp_buf) {
2843                 unsigned long pg = (unsigned long) tmp_buf;
2844                 tmp_buf = NULL;
2845                 free_page(pg);
2846         }
2847 }
2848
2849 module_init(rs_init);
2850 module_exit(rs_fini);
2851 MODULE_DESCRIPTION("Au1000 serial driver");
2852
2853
2854 /*
2855  * ------------------------------------------------------------
2856  * Serial console driver
2857  * ------------------------------------------------------------
2858  */
2859 #ifdef CONFIG_AU1000_SERIAL_CONSOLE
2860
2861 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
2862
2863 static struct async_struct async_sercons;
2864
2865 /*
2866  *      Wait for transmitter & holding register to empty
2867  */
2868 static inline void wait_for_xmitr(struct async_struct *info)
2869 {
2870         unsigned int status, tmout = 0xffffff;
2871
2872         do {
2873                 status = serial_in(info, UART_LSR);
2874
2875                 if (status & UART_LSR_BI)
2876                         lsr_break_flag = UART_LSR_BI;
2877                 
2878                 if (--tmout == 0)
2879                         break;
2880         } while((status & BOTH_EMPTY) != BOTH_EMPTY);
2881 }
2882
2883
2884 /*
2885  *      Print a string to the serial port trying not to disturb
2886  *      any possible real use of the port...
2887  *
2888  *      The console_lock must be held when we get here.
2889  */
2890 static void serial_console_write(struct console *co, const char *s,
2891                                 unsigned count)
2892 {
2893         static struct async_struct *info = &async_sercons;
2894         int ier;
2895         unsigned i;
2896
2897         /*
2898          *      First save the IER then disable the interrupts
2899          */
2900         ier = serial_in(info, UART_IER);
2901         serial_out(info, UART_IER, 0x00);
2902
2903         /*
2904          *      Now, do each character
2905          */
2906         for (i = 0; i < count; i++, s++) {
2907                 wait_for_xmitr(info);
2908
2909                 /*
2910                  *      Send the character out.
2911                  *      If a LF, also do CR...
2912                  */
2913                 serial_out(info, UART_TX, *s);
2914                 if (*s == 10) {
2915                         wait_for_xmitr(info);
2916                         serial_out(info, UART_TX, 13);
2917                 }
2918         }
2919
2920         /*
2921          *      Finally, Wait for transmitter & holding register to empty
2922          *      and restore the IER
2923          */
2924         wait_for_xmitr(info);
2925         serial_out(info, UART_IER, ier);
2926 }
2927
2928 /*
2929  *      Receive character from the serial port
2930  */
2931 static int serial_console_wait_key(struct console *co)
2932 {
2933         static struct async_struct *info;
2934         int ier, c;
2935
2936         info = &async_sercons;
2937
2938         /*
2939          *      First save the IER then disable the interrupts so
2940          *      that the real driver for the port does not get the
2941          *      character.
2942          */
2943         ier = serial_in(info, UART_IER);
2944         serial_out(info, UART_IER, 0x00);
2945  
2946         while ((serial_in(info, UART_LSR) & UART_LSR_DR) == 0);
2947         c = serial_in(info, UART_RX);
2948
2949         /*
2950          *      Restore the interrupts
2951          */
2952         serial_out(info, UART_IER, ier);
2953
2954         return c;
2955 }
2956
2957 static kdev_t serial_console_device(struct console *c)
2958 {
2959         return MKDEV(TTY_MAJOR, 64 + c->index);
2960 }
2961
2962 /*
2963  *      Setup initial baud/bits/parity. We do two things here:
2964  *      - construct a cflag setting for the first rs_open()
2965  *      - initialize the serial port
2966  *      Return non-zero if we didn't find a serial port.
2967  */
2968 static int __init serial_console_setup(struct console *co, char *options)
2969 {
2970         static struct async_struct *info;
2971         struct serial_state *state;
2972         unsigned cval;
2973         int     baud = 9600;
2974         int     bits = 8;
2975         int     parity = 'n';
2976         int     cflag = CREAD | HUPCL | CLOCAL;
2977         int     quot = 0;
2978         char    *s;
2979
2980         if (options) {
2981                 baud = simple_strtoul(options, NULL, 10);
2982                 s = options;
2983                 while(*s >= '0' && *s <= '9')
2984                         s++;
2985                 if (*s) parity = *s++;
2986                 if (*s) bits   = *s - '0';
2987         }
2988
2989         /*
2990          *      Now construct a cflag setting.
2991          */
2992         switch(baud) {
2993                 case 1200:
2994                         cflag |= B1200;
2995                         break;
2996                 case 2400:
2997                         cflag |= B2400;
2998                         break;
2999                 case 4800:
3000                         cflag |= B4800;
3001                         break;
3002                 case 19200:
3003                         cflag |= B19200;
3004                         break;
3005                 case 38400:
3006                         cflag |= B38400;
3007                         break;
3008                 case 57600:
3009                         cflag |= B57600;
3010                         break;
3011                 case 115200:
3012                         cflag |= B115200;
3013                         break;
3014                 case 9600:
3015                 default:
3016                         cflag |= B9600;
3017                         break;
3018         }
3019         switch(bits) {
3020                 case 7:
3021                         cflag |= CS7;
3022                         break;
3023                 default:
3024                 case 8:
3025                         cflag |= CS8;
3026                         break;
3027         }
3028         switch(parity) {
3029                 case 'o': case 'O':
3030                         cflag |= PARODD;
3031                         break;
3032                 case 'e': case 'E':
3033                         cflag |= PARENB;
3034                         break;
3035         }
3036         co->cflag = cflag;
3037
3038         /*
3039          *      Divisor, bytesize and parity
3040          */
3041         state = rs_table + co->index;
3042         info = &async_sercons;
3043         info->magic = SERIAL_MAGIC;
3044         info->state = state;
3045         info->port = state->port;
3046         info->flags = state->flags;
3047         info->io_type = state->io_type;
3048         info->iomem_base = state->iomem_base;
3049         info->iomem_reg_shift = state->iomem_reg_shift;
3050         state->baud_base = get_au1000_uart_baud();
3051         quot = state->baud_base / baud;
3052
3053         cval = cflag & (CSIZE | CSTOPB);
3054         cval >>= 4;
3055         if (cflag & PARENB)
3056                 cval |= UART_LCR_PARITY;
3057         if (!(cflag & PARODD))
3058                 cval |= UART_LCR_EPAR;
3059
3060         /*
3061          *      Disable UART interrupts, set DTR and RTS high
3062          *      and set speed.
3063          */
3064         serial_out(info, UART_CLK, quot & 0xffff);
3065         serial_out(info, UART_IER, 0);
3066         serial_out(info, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
3067
3068         /*
3069          *      If we read 0xff from the LSR, there is no UART here.
3070          */
3071         if (serial_in(info, UART_LSR) == 0xff)
3072                 return -1;
3073
3074         return 0;
3075 }
3076
3077 static struct console sercons = {
3078         name:           "ttyS",
3079         write:          serial_console_write,
3080         device:         serial_console_device,
3081         wait_key:       serial_console_wait_key,
3082         setup:          serial_console_setup,
3083         flags:          CON_PRINTBUFFER,
3084         index:          -1,
3085 };
3086
3087 /*
3088  *      Register console.
3089  */
3090 void __init au1000_serial_console_init(void)
3091 {
3092         register_console(&sercons);
3093 }
3094 #endif
3095
3096 /*
3097   Local variables:
3098   compile-command: "gcc -D__KERNEL__ -I../../include -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -fno-strict-aliasing -pipe -fno-strength-reduce -march=i586 -DMODULE -DMODVERSIONS -include ../../include/linux/modversions.h   -DEXPORT_SYMTAB -c serial.c"
3099   End:
3100 */