3 * BRIEF MODULE DESCRIPTION
4 * Au1000 serial port driver.
6 * Copyright 2001 MontaVista Software Inc.
7 * Author: MontaVista Software, Inc.
8 * ppopov@mvista.com or source@mvista.com
10 * Derived almost entirely from drivers/char/serial.c:
12 * Copyright (C) 1991, 1992 Linus Torvalds
13 * Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997,
14 * 1998, 1999 Theodore Ts'o
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.
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.
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.
37 static char *serial_version = "1.01";
38 static char *serial_revdate = "2001-02-08";
41 #include <linux/config.h>
42 #include <linux/version.h>
44 #undef SERIAL_PARANOIA_CHECK
45 #define CONFIG_SERIAL_NOPAUSE_IO
46 #define SERIAL_DO_RESTART
49 /* Set of debugging defines */
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
59 #undef CONFIG_AU1000_SERIAL_CONSOLE
62 #define CONFIG_SERIAL_RSA
64 #define RS_STROBE_TIME (10*HZ)
65 #define RS_ISR_PASS_LIMIT 256
68 * End of serial driver configuration section.
72 #include <linux/modversions.h>
74 #include <linux/module.h>
76 #include <linux/types.h>
78 #include "serial_local.h"
80 #include <linux/serial.h>
81 #include <linux/serialP.h>
82 #include <asm/au1000.h>
83 #include <asm/serial.h>
84 #define LOCAL_VERSTRING ""
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>
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>
107 #ifdef CONFIG_MAGIC_SYSRQ
108 #include <linux/sysrq.h>
111 #include <asm/system.h>
114 #include <asm/bitops.h>
116 #ifdef CONFIG_MAC_SERIAL
117 #define SERIAL_DEV_OFFSET 2
119 #define SERIAL_DEV_OFFSET 0
123 #define _INLINE_ inline
128 static char *serial_name = "Serial driver";
130 static DECLARE_TASK_QUEUE(tq_serial);
132 static struct tty_driver serial_driver, callout_driver;
133 static int serial_refcount;
135 static struct timer_list serial_timer;
137 extern unsigned long get_au1000_uart_baud(void);
139 /* serial subtype definitions */
140 #ifndef SERIAL_TYPE_NORMAL
141 #define SERIAL_TYPE_NORMAL 1
142 #define SERIAL_TYPE_CALLOUT 2
145 /* number of characters left in xmit buffer before we ask for more */
146 #define WAKEUP_CHARS 256
149 * IRQ_timeout - How long the timeout should be for each IRQ
150 * should be after the IRQ has been active.
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;
159 #if defined(CONFIG_AU1000_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
160 static unsigned long break_pressed; /* break, really ... */
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);
168 * Here we define the default xmit fifo size used for each type of
171 static struct serial_uart_config uart_config[] = {
180 static struct serial_state rs_table[RS_TABLE_SIZE] = {
181 SERIAL_PORT_DFNS /* Defined in serial.h */
184 #define NR_PORTS (sizeof(rs_table)/sizeof(struct serial_state))
187 #define PREPARE_FUNC(dev) (dev->prepare)
188 #define ACTIVATE_FUNC(dev) (dev->activate)
189 #define DEACTIVATE_FUNC(dev) (dev->deactivate)
192 #define HIGH_BITS_OFFSET ((sizeof(long)-sizeof(int))*8)
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];
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)
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.
215 static unsigned char *tmp_buf;
217 static DECLARE_MUTEX(tmp_buf_sem);
219 static struct semaphore tmp_buf_sem = MUTEX;
223 static inline int serial_paranoia_check(struct async_struct *info,
224 kdev_t device, const char *routine)
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";
233 printk(badinfo, kdevname(device), routine);
236 if (info->magic != SERIAL_MAGIC) {
237 printk(badmagic, kdevname(device), routine);
245 static _INLINE_ unsigned int serial_in(struct async_struct *info, int offset)
247 return (inl(info->port+offset) & 0xff);
250 static _INLINE_ void serial_out(struct async_struct *info, int offset, int value)
252 outl(value & 0xff, info->port+offset);
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
262 #define serial_inp(info, offset) serial_in(info, offset)
263 #define serial_outp(info, offset, value) serial_out(info, offset, value)
267 * ------------------------------------------------------------
268 * rs_stop() and rs_start()
270 * This routines are called before setting or resetting tty->stopped.
271 * They enable or disable transmitter interrupts, as necessary.
272 * ------------------------------------------------------------
274 static void rs_stop(struct tty_struct *tty)
276 struct async_struct *info = (struct async_struct *)tty->driver_data;
279 if (serial_paranoia_check(info, tty->device, "rs_stop"))
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);
287 restore_flags(flags);
290 static void rs_start(struct tty_struct *tty)
292 struct async_struct *info = (struct async_struct *)tty->driver_data;
295 if (serial_paranoia_check(info, tty->device, "rs_start"))
298 save_flags(flags); cli();
299 if (info->xmit.head != info->xmit.tail
301 && !(info->IER & UART_IER_THRI)) {
302 info->IER |= UART_IER_THRI;
303 serial_out(info, UART_IER, info->IER);
305 restore_flags(flags);
309 * ----------------------------------------------------------------------
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.
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
321 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
323 * and look at the resulting assemble code in serial.s.
325 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
326 * -----------------------------------------------------------------------
330 * This routine is used by the interrupt handler to schedule
331 * processing in the software interrupt portion of the driver.
333 static _INLINE_ void rs_sched_event(struct async_struct *info,
336 info->event |= 1 << event;
337 queue_task(&info->tqueue, &tq_serial);
341 static _INLINE_ void receive_chars(struct async_struct *info,
342 int *status, struct pt_regs * regs)
344 struct tty_struct *tty = info->tty;
347 struct async_icount *icount;
349 icount = &info->state->icount;
351 ch = serial_inp(info, UART_RX);
352 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
354 *tty->flip.char_buf_ptr = ch;
357 #ifdef SERIAL_DEBUG_INTR
358 printk("DR%02x:%02x...", ch, *status);
360 *tty->flip.flag_buf_ptr = 0;
361 if (*status & (UART_LSR_BI | UART_LSR_PE |
362 UART_LSR_FE | UART_LSR_OE)) {
364 * For statistics only
366 if (*status & UART_LSR_BI) {
367 *status &= ~(UART_LSR_FE | UART_LSR_PE);
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.
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;
384 if (info->flags & ASYNC_SAK)
386 } else if (*status & UART_LSR_PE)
388 else if (*status & UART_LSR_FE)
390 if (*status & UART_LSR_OE)
394 * Now check to see if character should be
395 * ignored, and mask off conditions which
398 if (*status & info->ignore_status_mask) {
403 *status &= info->read_status_mask;
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;
412 if (*status & (UART_LSR_BI)) {
413 #ifdef SERIAL_DEBUG_INTR
414 printk("handling break....");
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) {
423 * Overrun is special, since it's
424 * reported immediately, and doesn't
425 * affect the current character
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)
435 #if defined(CONFIG_AU1000_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
436 if (break_pressed && info->line == sercons.index) {
438 time_before(jiffies, break_pressed + HZ*5)) {
439 handle_sysrq(ch, regs, NULL, NULL);
446 tty->flip.flag_buf_ptr++;
447 tty->flip.char_buf_ptr++;
450 *status = serial_inp(info, UART_LSR);
451 } while (*status & UART_LSR_DR);
452 tty_flip_buffer_push(tty);
455 static _INLINE_ void transmit_chars(struct async_struct *info, int *intr_done)
460 serial_outp(info, UART_TX, info->x_char);
461 info->state->icount.tx++;
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);
475 count = info->xmit_fifo_size;
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)
482 } while (--count > 0);
484 if (CIRC_CNT(info->xmit.head,
486 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
487 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
489 #ifdef SERIAL_DEBUG_INTR
495 if (info->xmit.head == info->xmit.tail) {
496 info->IER &= ~UART_IER_THRI;
497 serial_out(info, UART_IER, info->IER);
501 static _INLINE_ void check_modem_status(struct async_struct *info)
504 struct async_icount *icount;
506 status = serial_in(info, UART_MSR);
508 if (status & UART_MSR_ANY_DELTA) {
509 icount = &info->state->icount;
510 /* update input line counters */
511 if (status & UART_MSR_TERI)
513 if (status & UART_MSR_DDSR)
515 if (status & UART_MSR_DDCD) {
517 #ifdef CONFIG_HARD_PPS
518 if ((info->flags & ASYNC_HARDPPS_CD) &&
519 (status & UART_MSR_DCD))
523 if (status & UART_MSR_DCTS)
525 wake_up_interruptible(&info->delta_msr_wait);
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");
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...");
541 tty_hangup(info->tty);
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...");
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);
557 if (!(status & UART_MSR_CTS)) {
558 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
559 printk("CTS tx stop...");
561 info->tty->hw_stopped = 1;
562 info->IER &= ~UART_IER_THRI;
563 serial_out(info, UART_IER, info->IER);
572 * This is the serial driver's interrupt routine for a single port
574 static void rs_interrupt_single(int irq, void *dev_id, struct pt_regs * regs)
577 int pass_counter = 0;
578 struct async_struct * info;
580 #ifdef SERIAL_DEBUG_INTR
581 printk("rs_interrupt_single(%d)...", irq);
584 info = IRQ_ports[irq];
585 if (!info || !info->tty)
589 status = serial_inp(info, UART_LSR);
590 #ifdef SERIAL_DEBUG_INTR
591 printk("status = %x...", status);
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) {
600 printk("rs_single loop break.\n");
604 } while (!(serial_in(info, UART_IIR) & UART_IIR_NO_INT));
605 info->last_active = jiffies;
606 #ifdef SERIAL_DEBUG_INTR
613 * -------------------------------------------------------------------
614 * Here ends the serial interrupt routines.
615 * -------------------------------------------------------------------
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.
627 static void do_serial_bh(void)
629 run_task_queue(&tq_serial);
632 static void do_softint(void *private_)
634 struct async_struct *info = (struct async_struct *) private_;
635 struct tty_struct *tty;
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);
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
659 static void rs_timer(unsigned long dummy)
661 static unsigned long last_strobe;
662 struct async_struct *info;
666 if ((jiffies - last_strobe) >= RS_STROBE_TIME) {
667 for (i=0; i < NR_IRQS; i++) {
671 save_flags(flags); cli();
672 rs_interrupt_single(i, NULL, NULL);
673 restore_flags(flags);
676 last_strobe = jiffies;
677 mod_timer(&serial_timer, jiffies + RS_STROBE_TIME);
681 save_flags(flags); cli();
682 rs_interrupt_single(0, NULL, NULL);
683 restore_flags(flags);
685 mod_timer(&serial_timer, jiffies + IRQ_timeout[0]);
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 * ---------------------------------------------------------------
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....
704 static void figure_IRQ_timeout(int irq)
706 struct async_struct *info;
707 int timeout = 60*HZ; /* 60 seconds === a long time :-) */
709 info = IRQ_ports[irq];
711 IRQ_timeout[irq] = 60*HZ;
715 if (info->timeout < timeout)
716 timeout = info->timeout;
717 info = info->next_port;
720 timeout = timeout / 2;
721 IRQ_timeout[irq] = (timeout > 3) ? timeout-2 : 1;
725 static int startup(struct async_struct * info)
729 void (*handler)(int, void *, struct pt_regs *);
730 struct serial_state *state= info->state;
733 page = get_zeroed_page(GFP_KERNEL);
737 save_flags(flags); cli();
739 if (info->flags & ASYNC_INITIALIZED) {
744 if (!CONFIGURED_SERIAL_PORT(state) || !state->type) {
746 set_bit(TTY_IO_ERROR, &info->tty->flags);
753 info->xmit.buf = (unsigned char *) page;
756 if (inl(UART_MOD_CNTRL + state->port) != 0x3) {
757 outl(3, UART_MOD_CNTRL + state->port);
759 #ifdef SERIAL_DEBUG_OPEN
760 printk("starting up ttys%d (irq %d)...", info->line, state->irq);
765 * Clear the FIFO buffers and disable them
766 * (they will be reenabled in change_speed())
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);
777 * Clear the interrupt registers.
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);
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
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)) {
794 set_bit(TTY_IO_ERROR, &info->tty->flags);
801 * Allocate the IRQ if necessary
804 /* au1000, uart0 irq is 0 */
805 if (state->irq && (!IRQ_ports[state->irq] || !IRQ_ports[state->irq]->next_port)) {
807 if ((!IRQ_ports[state->irq] || !IRQ_ports[state->irq]->next_port)) {
808 if (IRQ_ports[state->irq]) {
812 handler = rs_interrupt_single;
814 retval = request_irq(state->irq, handler, SA_SHIRQ,
815 "serial", &IRQ_ports[state->irq]);
817 if (capable(CAP_SYS_ADMIN)) {
819 set_bit(TTY_IO_ERROR,
828 * Insert serial port into IRQ chain.
831 info->next_port = IRQ_ports[state->irq];
833 info->next_port->prev_port = info;
834 IRQ_ports[state->irq] = info;
835 figure_IRQ_timeout(state->irq);
838 * Now, initialize the UART
840 serial_outp(info, UART_LCR, UART_LCR_WLEN8);
843 if (info->tty->termios->c_cflag & CBAUD)
844 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
847 info->MCR |= UART_MCR_OUT2;
849 info->MCR |= ALPHA_KLUDGE_MCR; /* Don't ask */
850 serial_outp(info, UART_MCR, info->MCR);
853 * Finally, enable interrupts
855 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
856 serial_outp(info, UART_IER, info->IER); /* enable interrupts */
860 * And clear the interrupt registers again for luck.
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);
868 clear_bit(TTY_IO_ERROR, &info->tty->flags);
869 info->xmit.head = info->xmit.tail = 0;
872 * Set up serial timers...
874 mod_timer(&serial_timer, jiffies + 2*HZ/100);
877 * Set up the tty->alt_speed kludge
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;
891 * and set the speed of the serial port
893 change_speed(info, 0);
895 info->flags |= ASYNC_INITIALIZED;
896 restore_flags(flags);
900 restore_flags(flags);
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.
908 static void shutdown(struct async_struct * info)
911 struct serial_state *state;
914 if (!(info->flags & ASYNC_INITIALIZED))
919 #ifdef SERIAL_DEBUG_OPEN
920 printk("Shutting down serial port %d (irq %d)....", info->line,
924 save_flags(flags); cli(); /* Disable interrupts */
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
930 wake_up_interruptible(&info->delta_msr_wait);
933 * First unlink the serial port from the IRQ chain...
936 info->next_port->prev_port = info->prev_port;
938 info->prev_port->next_port = info->next_port;
940 IRQ_ports[state->irq] = info->next_port;
941 figure_IRQ_timeout(state->irq);
944 * Free the IRQ, if necessary
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,
953 &IRQ_ports[state->irq]);
956 printk("serial shutdown: request_irq: error %d"
957 " Couldn't reacquire IRQ.\n", retval);
959 free_irq(state->irq, &IRQ_ports[state->irq]);
962 if (info->xmit.buf) {
963 unsigned long pg = (unsigned long) info->xmit.buf;
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 */
973 /* disable break condition */
974 serial_out(info, UART_LCR, serial_inp(info, UART_LCR) & ~UART_LCR_SBC);
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);
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);
986 (void)serial_in(info, UART_RX); /* read data port to reset things */
989 set_bit(TTY_IO_ERROR, &info->tty->flags);
991 info->flags &= ~ASYNC_INITIALIZED;
992 restore_flags(flags);
997 * This routine is called to set the UART divisor registers to match
998 * the specified baud rate for a serial port.
1000 static void change_speed(struct async_struct *info,
1001 struct termios *old_termios)
1003 int quot = 0, baud_base, baud;
1004 unsigned cflag, cval, fcr = 0;
1006 unsigned long flags;
1008 if (!info->tty || !info->tty->termios)
1010 cflag = info->tty->termios->c_cflag;
1011 if (!CONFIGURED_SERIAL_PORT(info))
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;
1023 if (cflag & CSTOPB) {
1027 if (cflag & PARENB) {
1028 cval |= UART_LCR_PARITY;
1031 if (!(cflag & PARODD))
1032 cval |= UART_LCR_EPAR;
1035 cval |= UART_LCR_SPAR;
1038 /* Determine divisor based on baud rate */
1039 baud = tty_get_baud_rate(info->tty);
1041 baud = 9600; /* B0 transition handled in rs_set_termios */
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;
1050 /* Special case since 134 is really 134.5 */
1051 quot = (2*baud_base / 269);
1053 quot = baud_base / baud;
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);
1062 if (baud == 38400 &&
1063 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1064 quot = info->state->custom_divisor;
1067 /* Special case since 134 is really 134.5 */
1068 quot = (2*baud_base / 269);
1070 quot = baud_base / baud;
1073 /* As a last resort, if the quotient is zero, default to 9600 bps */
1075 quot = baud_base / 9600;
1078 info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
1079 info->timeout += HZ/50; /* Add .02 seconds of slop */
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;
1086 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIGGER_8;
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;
1097 info->flags &= ~ASYNC_CTS_FLOW;
1099 info->flags &= ~ASYNC_CHECK_CD;
1101 info->flags |= ASYNC_CHECK_CD;
1102 info->IER |= UART_IER_MSI;
1104 serial_out(info, UART_IER, info->IER);
1107 * Set up parity check flag
1109 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
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;
1118 * Characters to ignore
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;
1126 * If we're ignore parity and break indicators, ignore
1127 * overruns too. (For real raw support).
1129 if (I_IGNPAR(info->tty))
1130 info->ignore_status_mask |= UART_LSR_OE;
1133 * !!! ignore all characters if CREAD is not set
1135 if ((cflag & CREAD) == 0)
1136 info->ignore_status_mask |= UART_LSR_DR;
1137 save_flags(flags); cli();
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);
1145 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
1147 struct async_struct *info = (struct async_struct *)tty->driver_data;
1148 unsigned long flags;
1150 if (serial_paranoia_check(info, tty->device, "rs_put_char"))
1153 if (!tty || !info->xmit.buf)
1156 save_flags(flags); cli();
1157 if (CIRC_SPACE(info->xmit.head,
1159 SERIAL_XMIT_SIZE) == 0) {
1160 restore_flags(flags);
1164 info->xmit.buf[info->xmit.head] = ch;
1165 info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
1166 restore_flags(flags);
1169 static void rs_flush_chars(struct tty_struct *tty)
1171 struct async_struct *info = (struct async_struct *)tty->driver_data;
1172 unsigned long flags;
1174 if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
1177 if (info->xmit.head == info->xmit.tail
1183 save_flags(flags); cli();
1184 info->IER |= UART_IER_THRI;
1185 serial_out(info, UART_IER, info->IER);
1186 restore_flags(flags);
1189 static int rs_write(struct tty_struct * tty, int from_user,
1190 const unsigned char *buf, int count)
1193 struct async_struct *info = (struct async_struct *)tty->driver_data;
1194 unsigned long flags;
1196 if (serial_paranoia_check(info, tty->device, "rs_write"))
1199 if (!tty || !info->xmit.buf || !tmp_buf)
1207 c = CIRC_SPACE_TO_END(info->xmit.head,
1215 c -= copy_from_user(tmp_buf, buf, c);
1222 c1 = CIRC_SPACE_TO_END(info->xmit.head,
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);
1239 c = CIRC_SPACE_TO_END(info->xmit.head,
1247 memcpy(info->xmit.buf + info->xmit.head, buf, c);
1248 info->xmit.head = ((info->xmit.head + c) &
1249 (SERIAL_XMIT_SIZE-1));
1254 restore_flags(flags);
1256 if (info->xmit.head != info->xmit.tail
1259 && !(info->IER & UART_IER_THRI)) {
1260 info->IER |= UART_IER_THRI;
1261 serial_out(info, UART_IER, info->IER);
1266 static int rs_write_room(struct tty_struct *tty)
1268 struct async_struct *info = (struct async_struct *)tty->driver_data;
1270 if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1272 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1275 static int rs_chars_in_buffer(struct tty_struct *tty)
1277 struct async_struct *info = (struct async_struct *)tty->driver_data;
1279 if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1281 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1284 static void rs_flush_buffer(struct tty_struct *tty)
1286 struct async_struct *info = (struct async_struct *)tty->driver_data;
1287 unsigned long flags;
1289 if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
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);
1298 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1299 tty->ldisc.write_wakeup)
1300 (tty->ldisc.write_wakeup)(tty);
1304 * This function is used to send a high-priority XON/XOFF character to
1307 static void rs_send_xchar(struct tty_struct *tty, char ch)
1309 struct async_struct *info = (struct async_struct *)tty->driver_data;
1311 if (serial_paranoia_check(info, tty->device, "rs_send_char"))
1316 /* Make sure transmit interrupts are on */
1317 info->IER |= UART_IER_THRI;
1318 serial_out(info, UART_IER, info->IER);
1323 * ------------------------------------------------------------
1326 * This routine is called by the upper-layer tty layer to signal that
1327 * incoming characters should be throttled.
1328 * ------------------------------------------------------------
1330 static void rs_throttle(struct tty_struct * tty)
1332 struct async_struct *info = (struct async_struct *)tty->driver_data;
1333 unsigned long flags;
1334 #ifdef SERIAL_DEBUG_THROTTLE
1337 printk("throttle %s: %d....\n", tty_name(tty, buf),
1338 tty->ldisc.chars_in_buffer(tty));
1341 if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1345 rs_send_xchar(tty, STOP_CHAR(tty));
1347 if (tty->termios->c_cflag & CRTSCTS)
1348 info->MCR &= ~UART_MCR_RTS;
1350 save_flags(flags); cli();
1351 serial_out(info, UART_MCR, info->MCR);
1352 restore_flags(flags);
1355 static void rs_unthrottle(struct tty_struct * tty)
1357 struct async_struct *info = (struct async_struct *)tty->driver_data;
1358 unsigned long flags;
1359 #ifdef SERIAL_DEBUG_THROTTLE
1362 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1363 tty->ldisc.chars_in_buffer(tty));
1366 if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1373 rs_send_xchar(tty, START_CHAR(tty));
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);
1383 * ------------------------------------------------------------
1384 * rs_ioctl() and friends
1385 * ------------------------------------------------------------
1388 static int get_serial_info(struct async_struct * info,
1389 struct serial_struct * retinfo)
1391 struct serial_struct tmp;
1392 struct serial_state *state = info->state;
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;
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)))
1418 static int set_serial_info(struct async_struct * info,
1419 struct serial_struct * new_info)
1421 struct serial_struct new_serial;
1422 struct serial_state old_state, *state;
1423 unsigned int i,change_irq,change_port;
1425 unsigned long new_port;
1427 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1429 state = info->state;
1432 new_port = new_serial.port;
1433 if (HIGH_BITS_OFFSET)
1434 new_port += (unsigned long) new_serial.port_high << HIGH_BITS_OFFSET;
1436 change_irq = new_serial.irq != state->irq;
1437 change_port = (new_port != ((int) state->port)) ||
1438 (new_serial.hub6 != state->hub6);
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)))
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;
1457 new_serial.irq = irq_cannonicalize(new_serial.irq);
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)) {
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;
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) &&
1480 if ((change_port || change_irq) && (state->count > 1))
1484 * OK, past this point, all the error checking has been done.
1485 * At this point, we start making changes.....
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;
1500 if ((state->type != PORT_UNKNOWN) && state->port) {
1501 release_region(state->port,8);
1503 state->type = new_serial.type;
1504 if (change_port || change_irq) {
1506 * We need to shutdown the serial port at the old
1507 * port/irq combination.
1510 state->irq = new_serial.irq;
1511 info->port = state->port = new_port;
1512 info->hub6 = state->hub6 = new_serial.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;
1518 if ((state->type != PORT_UNKNOWN) && state->port) {
1519 request_region(state->port,8,"serial(set)");
1524 if (!state->port || !state->type)
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);
1541 retval = startup(info);
1548 * get_lsr_info - get line status register info
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.
1557 static int get_lsr_info(struct async_struct * info, unsigned int *value)
1559 unsigned char status;
1560 unsigned int result;
1561 unsigned long flags;
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);
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).
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;
1580 if (copy_to_user(value, &result, sizeof(int)))
1586 static int get_modem_info(struct async_struct * info, unsigned int *value)
1588 unsigned char control, status;
1589 unsigned int result;
1590 unsigned long flags;
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)
1599 | ((control & UART_MCR_OUT1) ? TIOCM_OUT1 : 0)
1600 | ((control & UART_MCR_OUT2) ? TIOCM_OUT2 : 0)
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);
1607 if (copy_to_user(value, &result, sizeof(int)))
1612 static int set_modem_info(struct async_struct * info, unsigned int cmd,
1613 unsigned int *value)
1616 unsigned long flags;
1618 if (copy_from_user(&arg, value, sizeof(int)))
1623 if (arg & TIOCM_RTS)
1624 info->MCR |= UART_MCR_RTS;
1625 if (arg & TIOCM_DTR)
1626 info->MCR |= UART_MCR_DTR;
1628 if (arg & TIOCM_OUT1)
1629 info->MCR |= UART_MCR_OUT1;
1630 if (arg & TIOCM_OUT2)
1631 info->MCR |= UART_MCR_OUT2;
1633 if (arg & TIOCM_LOOP)
1634 info->MCR |= UART_MCR_LOOP;
1637 if (arg & TIOCM_RTS)
1638 info->MCR &= ~UART_MCR_RTS;
1639 if (arg & TIOCM_DTR)
1640 info->MCR &= ~UART_MCR_DTR;
1642 if (arg & TIOCM_OUT1)
1643 info->MCR &= ~UART_MCR_OUT1;
1644 if (arg & TIOCM_OUT2)
1645 info->MCR &= ~UART_MCR_OUT2;
1647 if (arg & TIOCM_LOOP)
1648 info->MCR &= ~UART_MCR_LOOP;
1651 info->MCR = ((info->MCR & ~(UART_MCR_RTS |
1658 | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1660 | ((arg & TIOCM_OUT1) ? UART_MCR_OUT1 : 0)
1661 | ((arg & TIOCM_OUT2) ? UART_MCR_OUT2 : 0)
1663 | ((arg & TIOCM_LOOP) ? UART_MCR_LOOP : 0)
1664 | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
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);
1676 static int do_autoconfig(struct async_struct * info)
1680 if (!capable(CAP_SYS_ADMIN))
1683 if (info->state->count > 1)
1688 autoconfig(info->state);
1689 retval = startup(info);
1696 * rs_break() --- routine which turns the break handling on or off
1698 static void rs_break(struct tty_struct *tty, int break_state)
1700 struct async_struct * info = (struct async_struct *)tty->driver_data;
1701 unsigned long flags;
1703 if (serial_paranoia_check(info, tty->device, "rs_break"))
1706 if (!CONFIGURED_SERIAL_PORT(info))
1708 save_flags(flags); cli();
1709 if (break_state == -1)
1710 info->LCR |= UART_LCR_SBC;
1712 info->LCR &= ~UART_LCR_SBC;
1713 serial_out(info, UART_LCR, info->LCR);
1714 restore_flags(flags);
1718 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1719 unsigned int cmd, unsigned long arg)
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;
1726 if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1729 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1730 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1731 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1732 if (tty->flags & (1 << TTY_IO_ERROR))
1738 return get_modem_info(info, (unsigned int *) arg);
1742 return set_modem_info(info, cmd, (unsigned int *) arg);
1744 return get_serial_info(info,
1745 (struct serial_struct *) arg);
1747 return set_serial_info(info,
1748 (struct serial_struct *) arg);
1750 return do_autoconfig(info);
1752 case TIOCSERGETLSR: /* Get line status register */
1753 return get_lsr_info(info, (unsigned int *) arg);
1755 case TIOCSERGSTRUCT:
1756 if (copy_to_user((struct async_struct *) arg,
1757 info, sizeof(struct async_struct)))
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
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);
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)) ) {
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.
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;
1819 if (copy_to_user((void *)arg, &icount, sizeof(icount)))
1824 /* "setserial -W" is called in Debian boot */
1825 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1829 return -ENOIOCTLCMD;
1834 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1836 struct async_struct *info = (struct async_struct *)tty->driver_data;
1837 unsigned long flags;
1838 unsigned int cflag = tty->termios->c_cflag;
1840 if ( (cflag == old_termios->c_cflag)
1841 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
1842 == RELEVANT_IFLAG(old_termios->c_iflag)))
1845 change_speed(info, old_termios);
1847 /* Handle transition to B0 status */
1848 if ((old_termios->c_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);
1856 /* Handle transition away from B0 status */
1857 if (!(old_termios->c_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;
1864 save_flags(flags); cli();
1865 serial_out(info, UART_MCR, info->MCR);
1866 restore_flags(flags);
1869 /* Handle turning off CRTSCTS */
1870 if ((old_termios->c_cflag & CRTSCTS) &&
1871 !(tty->termios->c_cflag & CRTSCTS)) {
1872 tty->hw_stopped = 0;
1878 * ------------------------------------------------------------
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 * ------------------------------------------------------------
1887 static void rs_close(struct tty_struct *tty, struct file * filp)
1889 struct async_struct * info = (struct async_struct *)tty->driver_data;
1890 struct serial_state *state;
1891 unsigned long flags;
1893 if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1896 state = info->state;
1898 save_flags(flags); cli();
1900 if (tty_hung_up_p(filp)) {
1901 DBG_CNT("before DEC-hung");
1903 restore_flags(flags);
1907 #ifdef SERIAL_DEBUG_OPEN
1908 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
1910 if ((tty->count == 1) && (state->count != 1)) {
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.
1918 printk("rs_close: bad serial port count; tty->count is 1, "
1919 "state->count is %d\n", state->count);
1922 if (--state->count < 0) {
1923 printk("rs_close: bad serial port count for ttys%d: %d\n",
1924 info->line, state->count);
1928 DBG_CNT("before DEC-2");
1930 restore_flags(flags);
1933 info->flags |= ASYNC_CLOSING;
1934 restore_flags(flags);
1936 * Save the termios structure, since this port may have
1937 * separate termios for callout and dialin.
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;
1944 * Now we wait for the transmit buffer to clear; and we notify
1945 * the line discipline to only process XON/XOFF characters.
1948 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1949 tty_wait_until_sent(tty, info->closing_wait);
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.
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);
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!
1965 rs_wait_until_sent(tty, info->timeout);
1968 if (tty->driver.flush_buffer)
1969 tty->driver.flush_buffer(tty);
1970 if (tty->ldisc.flush_buffer)
1971 tty->ldisc.flush_buffer(tty);
1975 if (info->blocked_open) {
1976 if (info->close_delay) {
1977 set_current_state(TASK_INTERRUPTIBLE);
1978 schedule_timeout(info->close_delay);
1980 wake_up_interruptible(&info->open_wait);
1982 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1984 wake_up_interruptible(&info->close_wait);
1989 * rs_wait_until_sent() --- wait until the transmitter is empty
1991 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1993 struct async_struct * info = (struct async_struct *)tty->driver_data;
1994 unsigned long orig_jiffies, char_time;
1997 if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
2000 if (info->state->type == PORT_UNKNOWN)
2003 if (info->xmit_fifo_size == 0)
2004 return; /* Just in case.... */
2006 orig_jiffies = jiffies;
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.
2012 * Note: we have to use pretty tight timings here to satisfy
2015 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
2016 char_time = char_time / 5;
2019 if (timeout && timeout < char_time)
2020 char_time = timeout;
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
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);
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);
2040 set_current_state(TASK_INTERRUPTIBLE);
2041 schedule_timeout(char_time);
2042 if (signal_pending(current))
2044 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2047 set_current_state(TASK_RUNNING);
2048 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2049 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2054 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
2056 static void rs_hangup(struct tty_struct *tty)
2058 struct async_struct * info = (struct async_struct *)tty->driver_data;
2059 struct serial_state *state = info->state;
2061 if (serial_paranoia_check(info, tty->device, "rs_hangup"))
2064 state = info->state;
2066 rs_flush_buffer(tty);
2067 if (info->flags & ASYNC_CLOSING)
2072 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
2074 wake_up_interruptible(&info->open_wait);
2078 * ------------------------------------------------------------
2079 * rs_open() and friends
2080 * ------------------------------------------------------------
2082 static int block_til_ready(struct tty_struct *tty, struct file * filp,
2083 struct async_struct *info)
2085 DECLARE_WAITQUEUE(wait, current);
2086 struct serial_state *state = info->state;
2088 int do_clocal = 0, extra_count = 0;
2089 unsigned long flags;
2092 * If the device is in the middle of being closed, then block
2093 * until it's done, and then try again.
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);
2108 * If this is a callout device, then just make sure the normal
2109 * device isn't being used.
2111 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
2112 if (info->flags & ASYNC_NORMAL_ACTIVE)
2114 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2115 (info->flags & ASYNC_SESSION_LOCKOUT) &&
2116 (info->session != current->session))
2118 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2119 (info->flags & ASYNC_PGRP_LOCKOUT) &&
2120 (info->pgrp != current->pgrp))
2122 info->flags |= ASYNC_CALLOUT_ACTIVE;
2127 * If non-blocking mode is set, or the port is not enabled,
2128 * then make the check up front and then exit.
2130 if ((filp->f_flags & O_NONBLOCK) ||
2131 (tty->flags & (1 << TTY_IO_ERROR))) {
2132 if (info->flags & ASYNC_CALLOUT_ACTIVE)
2134 info->flags |= ASYNC_NORMAL_ACTIVE;
2138 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
2139 if (state->normal_termios.c_cflag & CLOCAL)
2142 if (tty->termios->c_cflag & CLOCAL)
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.
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);
2159 save_flags(flags); cli();
2160 if (!tty_hung_up_p(filp)) {
2164 restore_flags(flags);
2165 info->blocked_open++;
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)
2181 retval = -ERESTARTSYS;
2187 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2188 !(info->flags & ASYNC_CLOSING) &&
2189 (do_clocal || (serial_in(info, UART_MSR) &
2192 if (signal_pending(current)) {
2193 retval = -ERESTARTSYS;
2196 #ifdef SERIAL_DEBUG_OPEN
2197 printk("block_til_ready blocking: ttys%d, count = %d\n",
2198 info->line, state->count);
2202 set_current_state(TASK_RUNNING);
2203 remove_wait_queue(&info->open_wait, &wait);
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);
2213 info->flags |= ASYNC_NORMAL_ACTIVE;
2217 static int get_async_struct(int line, struct async_struct **ret_info)
2219 struct async_struct *info;
2220 struct serial_state *sstate;
2222 sstate = rs_table + line;
2225 *ret_info = sstate->info;
2228 info = kmalloc(sizeof(struct async_struct), GFP_KERNEL);
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;
2245 info->tqueue.routine = do_softint;
2246 info->tqueue.data = info;
2247 info->state = sstate;
2250 *ret_info = sstate->info;
2253 *ret_info = sstate->info = info;
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.
2263 static int rs_open(struct tty_struct *tty, struct file * filp)
2265 struct async_struct *info;
2270 line = MINOR(tty->device) - tty->driver.minor_start;
2271 if ((line < 0) || (line >= NR_PORTS)) {
2275 retval = get_async_struct(line, &info);
2280 tty->driver_data = info;
2282 if (serial_paranoia_check(info, tty->device, "rs_open")) {
2287 #ifdef SERIAL_DEBUG_OPEN
2288 printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
2289 info->state->count);
2291 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2294 page = get_zeroed_page(GFP_KERNEL);
2302 tmp_buf = (unsigned char *) page;
2306 * If the port is the middle of closing, bail out now
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);
2313 #ifdef SERIAL_DO_RESTART
2314 return ((info->flags & ASYNC_HUP_NOTIFY) ?
2315 -EAGAIN : -ERESTARTSYS);
2322 * Start up serial port
2324 retval = startup(info);
2330 retval = block_til_ready(tty, filp, info);
2332 #ifdef SERIAL_DEBUG_OPEN
2333 printk("rs_open returning after block_til_ready with %d\n",
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;
2345 *tty->termios = info->state->callout_termios;
2346 change_speed(info, 0);
2348 #ifdef CONFIG_AU1000_SERIAL_CONSOLE
2349 if (sercons.cflag && sercons.index == line) {
2350 tty->termios->c_cflag = sercons.cflag;
2352 change_speed(info, 0);
2355 info->session = current->session;
2356 info->pgrp = current->pgrp;
2358 #ifdef SERIAL_DEBUG_OPEN
2359 printk("rs_open ttys%d successful...", info->line);
2365 * /proc fs routines....
2368 static inline int line_info(char *buf, struct serial_state *state)
2370 struct async_struct *info = state->info, scr_info;
2371 char stat_buf[30], control, status;
2373 unsigned long flags;
2375 ret = sprintf(buf, "%d: uart:%s port:%lX irq:%d",
2376 state->line, uart_config[state->type].name,
2377 state->port, state->irq);
2379 if (!state->port || (state->type == PORT_UNKNOWN)) {
2380 ret += sprintf(buf+ret, "\n");
2385 * Figure out the current RS-232 lines
2388 info = &scr_info; /* This is just for serial_{in,out} */
2390 info->magic = SERIAL_MAGIC;
2391 info->port = state->port;
2392 info->flags = state->flags;
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);
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");
2417 ret += sprintf(buf+ret, " baud:%d",
2418 state->baud_base / info->quot);
2421 ret += sprintf(buf+ret, " tx:%d rx:%d",
2422 state->icount.tx, state->icount.rx);
2424 if (state->icount.frame)
2425 ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
2427 if (state->icount.parity)
2428 ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
2430 if (state->icount.brk)
2431 ret += sprintf(buf+ret, " brk:%d", state->icount.brk);
2433 if (state->icount.overrun)
2434 ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
2437 * Last thing is the RS-232 status lines
2439 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2443 int rs_read_proc(char *page, char **start, off_t off, int count,
2444 int *eof, void *data)
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]);
2454 if (len+begin > off+count)
2456 if (len+begin < off) {
2463 if (off >= len+begin)
2465 *start = page + (off-begin);
2466 return ((count < begin+len-off) ? count : begin+len-off);
2470 * ---------------------------------------------------------------------
2471 * rs_init() and friends
2473 * rs_init() is called at boot-time to initialize the serial driver.
2474 * ---------------------------------------------------------------------
2478 * This routine prints out the appropriate serial driver version
2479 * number, and identifies which options were configured into this
2482 static char serial_options[] __initdata =
2483 " no serial options enabled\n";
2486 static _INLINE_ void show_serial_version(void)
2488 printk(KERN_INFO "%s version %s%s (%s) with%s", serial_name,
2489 serial_version, LOCAL_VERSTRING, serial_revdate,
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.
2501 static void autoconfig(struct serial_state * state)
2503 struct async_struct *info, scr_info;
2504 unsigned long flags;
2507 #ifdef SERIAL_DEBUG_AUTOCONF
2508 printk("Testing ttyS%d (0x%04lx, 0x%04x)...\n", state->line,
2509 state->port, (unsigned) state->iomem_base);
2512 if (!CONFIGURED_SERIAL_PORT(state))
2515 if (inl(UART_MOD_CNTRL + state->port) != 0x3) {
2516 outl(3, UART_MOD_CNTRL + state->port);
2519 state->type = PORT_16550;
2520 info = &scr_info; /* This is just for serial_{in,out} */
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;
2531 save_flags(flags); cli();
2532 state->xmit_fifo_size = uart_config[state->type].dfl_xmit_fifo_size;
2535 request_region(info->port,8,"serial(auto)");
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);
2548 restore_flags(flags);
2551 int register_serial(struct serial_struct *req);
2552 void unregister_serial(int line);
2554 EXPORT_SYMBOL(register_serial);
2555 EXPORT_SYMBOL(unregister_serial);
2559 * The serial driver boot-time initialization code!
2561 static int __init rs_init(void)
2564 struct serial_state * state;
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);
2571 for (i = 0; i < NR_IRQS; i++) {
2575 #ifdef CONFIG_AU1000_SERIAL_CONSOLE
2577 * The interrupt of the serial console port
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;
2587 show_serial_version();
2589 /* Initialize the tty_driver structure */
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";
2597 serial_driver.name = "ttyS";
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;
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;
2634 * The callout device is just like normal device except for
2635 * major number and the subtype code.
2637 callout_driver = serial_driver;
2638 #if (LINUX_VERSION_CODE > 0x2032D && defined(CONFIG_DEVFS_FS))
2639 callout_driver.name = "cua/%d";
2641 callout_driver.name = "cua";
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;
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");
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;
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);
2670 state->io_type = SERIAL_IO_HUB6;
2671 if (state->port && check_region(state->port,8)) {
2675 if (state->flags & ASYNC_BOOT_AUTOCONF) {
2679 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
2680 if (state->type == PORT_UNKNOWN) {
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);
2697 * register_serial and unregister_serial allows for 16x50 serial ports to be
2698 * configured at run-time, to support PCMCIA modems.
2702 * register_serial - configure a 16x50 serial port at runtime
2703 * @req: request structure
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.
2709 * The port is then probed and if neccessary the IRQ is autodetected
2710 * If this fails an error is returned.
2712 * On success the port is ready to use and the line number is returned.
2715 int register_serial(struct serial_struct *req)
2718 unsigned long flags;
2719 struct serial_state *state;
2720 struct async_struct *info;
2724 if (HIGH_BITS_OFFSET)
2725 port += (unsigned long) req->port_high << HIGH_BITS_OFFSET;
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))
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))
2739 if (i == NR_PORTS) {
2740 restore_flags(flags);
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);
2750 state->irq = req->irq;
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;
2757 state->baud_base = req->baud_base;
2758 if ((info = state->info) != NULL) {
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;
2766 if (state->type == PORT_UNKNOWN) {
2767 restore_flags(flags);
2768 printk("register_serial(): autoconfig failed\n");
2771 restore_flags(flags);
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;
2786 * unregister_serial - deconfigure a 16x50 serial port
2787 * @line: line to deconfigure
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().
2794 void unregister_serial(int line)
2796 unsigned long flags;
2797 struct serial_state *state = &rs_table[line];
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)
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);
2814 static void __exit rs_fini(void)
2816 unsigned long flags;
2819 struct async_struct *info;
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",
2828 if ((e2 = tty_unregister_driver(&callout_driver)))
2829 printk("serial: failed to unregister callout driver (%d)\n",
2831 restore_flags(flags);
2833 for (i = 0; i < NR_PORTS; i++) {
2834 if ((info = rs_table[i].info)) {
2835 rs_table[i].info = NULL;
2838 if ((rs_table[i].type != PORT_UNKNOWN) && rs_table[i].port) {
2839 release_region(rs_table[i].port, 8);
2843 unsigned long pg = (unsigned long) tmp_buf;
2849 module_init(rs_init);
2850 module_exit(rs_fini);
2851 MODULE_DESCRIPTION("Au1000 serial driver");
2855 * ------------------------------------------------------------
2856 * Serial console driver
2857 * ------------------------------------------------------------
2859 #ifdef CONFIG_AU1000_SERIAL_CONSOLE
2861 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
2863 static struct async_struct async_sercons;
2866 * Wait for transmitter & holding register to empty
2868 static inline void wait_for_xmitr(struct async_struct *info)
2870 unsigned int status, tmout = 0xffffff;
2873 status = serial_in(info, UART_LSR);
2875 if (status & UART_LSR_BI)
2876 lsr_break_flag = UART_LSR_BI;
2880 } while((status & BOTH_EMPTY) != BOTH_EMPTY);
2885 * Print a string to the serial port trying not to disturb
2886 * any possible real use of the port...
2888 * The console_lock must be held when we get here.
2890 static void serial_console_write(struct console *co, const char *s,
2893 static struct async_struct *info = &async_sercons;
2898 * First save the IER then disable the interrupts
2900 ier = serial_in(info, UART_IER);
2901 serial_out(info, UART_IER, 0x00);
2904 * Now, do each character
2906 for (i = 0; i < count; i++, s++) {
2907 wait_for_xmitr(info);
2910 * Send the character out.
2911 * If a LF, also do CR...
2913 serial_out(info, UART_TX, *s);
2915 wait_for_xmitr(info);
2916 serial_out(info, UART_TX, 13);
2921 * Finally, Wait for transmitter & holding register to empty
2922 * and restore the IER
2924 wait_for_xmitr(info);
2925 serial_out(info, UART_IER, ier);
2929 * Receive character from the serial port
2931 static int serial_console_wait_key(struct console *co)
2933 static struct async_struct *info;
2936 info = &async_sercons;
2939 * First save the IER then disable the interrupts so
2940 * that the real driver for the port does not get the
2943 ier = serial_in(info, UART_IER);
2944 serial_out(info, UART_IER, 0x00);
2946 while ((serial_in(info, UART_LSR) & UART_LSR_DR) == 0);
2947 c = serial_in(info, UART_RX);
2950 * Restore the interrupts
2952 serial_out(info, UART_IER, ier);
2957 static kdev_t serial_console_device(struct console *c)
2959 return MKDEV(TTY_MAJOR, 64 + c->index);
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.
2968 static int __init serial_console_setup(struct console *co, char *options)
2970 static struct async_struct *info;
2971 struct serial_state *state;
2976 int cflag = CREAD | HUPCL | CLOCAL;
2981 baud = simple_strtoul(options, NULL, 10);
2983 while(*s >= '0' && *s <= '9')
2985 if (*s) parity = *s++;
2986 if (*s) bits = *s - '0';
2990 * Now construct a cflag setting.
3039 * Divisor, bytesize and parity
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;
3053 cval = cflag & (CSIZE | CSTOPB);
3056 cval |= UART_LCR_PARITY;
3057 if (!(cflag & PARODD))
3058 cval |= UART_LCR_EPAR;
3061 * Disable UART interrupts, set DTR and RTS high
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);
3069 * If we read 0xff from the LSR, there is no UART here.
3071 if (serial_in(info, UART_LSR) == 0xff)
3077 static struct console sercons = {
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,
3090 void __init au1000_serial_console_init(void)
3092 register_console(&sercons);
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"