Import 2.3.25pre1
[davej-history.git] / drivers / char / serial.c
blob0511c78f40ec58a6f4ca630c1fb2ad113c6dd9f6
1 /*
2 * linux/drivers/char/serial.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997,
6 * 1998, 1999 Theodore Ts'o
8 * Extensively rewritten by Theodore Ts'o, 8/16/92 -- 9/14/92. Now
9 * much more extensible to support other serial cards based on the
10 * 16450/16550A UART's. Added support for the AST FourPort and the
11 * Accent Async board.
13 * set_serial_info fixed to set the flags, custom divisor, and uart
14 * type fields. Fix suggested by Michael K. Johnson 12/12/92.
16 * 11/95: TIOCMIWAIT, TIOCGICOUNT by Angelo Haritsis <ah@doc.ic.ac.uk>
18 * 03/96: Modularised by Angelo Haritsis <ah@doc.ic.ac.uk>
20 * rs_set_termios fixed to look also for changes of the input
21 * flags INPCK, BRKINT, PARMRK, IGNPAR and IGNBRK.
22 * Bernd Anhäupl 05/17/96.
24 * 1/97: Extended dumb serial ports are a config option now.
25 * Saves 4k. Michael A. Griffith <grif@acm.org>
27 * 8/97: Fix bug in rs_set_termios with RTS
28 * Stanislav V. Voronyi <stas@uanet.kharkov.ua>
30 * 3/98: Change the IRQ detection, use of probe_irq_o*(),
31 * supress TIOCSERGWILD and TIOCSERSWILD
32 * Etienne Lorrain <etienne.lorrain@ibm.net>
34 * 4/98: Added changes to support the ARM architecture proposed by
35 * Russell King
37 * 5/99: Updated to include support for the XR16C850 and ST16C654
38 * uarts. Stuart MacDonald <stuartm@connecttech.com>
40 * 8/99: Generalized PCI support added. Theodore Ts'o
42 * This module exports the following rs232 io functions:
44 * int rs_init(void);
48 * Serial driver configuration section. Here are the various options:
50 * CONFIG_HUB6
51 * Enables support for the venerable Bell Technologies
52 * HUB6 card.
54 * CONFIG_SERIAL_MANY_PORTS
55 * Enables support for ports beyond the standard, stupid
56 * COM 1/2/3/4.
58 * CONFIG_SERIAL_MULTIPORT
59 * Enables support for special multiport board support.
61 * CONFIG_SERIAL_SHARE_IRQ
62 * Enables support for multiple serial ports on one IRQ
64 * CONFIG_SERIAL_DETECT_IRQ
65 * Enable the autodetection of IRQ on standart ports
67 * SERIAL_PARANOIA_CHECK
68 * Check the magic number for the async_structure where
69 * ever possible.
72 #include <linux/config.h>
73 #include <linux/version.h>
75 #undef SERIAL_PARANOIA_CHECK
76 #define CONFIG_SERIAL_NOPAUSE_IO
77 #define SERIAL_DO_RESTART
78 #define CONFIG_SERIAL_PCI_MEMMAPPED
80 #if 0
81 /* These defines are normally controlled by the autoconf.h */
82 #define CONFIG_SERIAL_MANY_PORTS
83 #define CONFIG_SERIAL_SHARE_IRQ
84 #define CONFIG_SERIAL_DETECT_IRQ
85 #define CONFIG_SERIAL_MULTIPORT
86 #define CONFIG_HUB6
87 #endif
89 #if (defined(CONFIG_PCI) && (LINUX_VERSION_CODE >= 131072))
90 #define ENABLE_SERIAL_PCI
91 #define CONFIG_SERIAL_SHARE_IRQ
92 #endif
94 /* Set of debugging defines */
96 #undef SERIAL_DEBUG_INTR
97 #undef SERIAL_DEBUG_OPEN
98 #undef SERIAL_DEBUG_FLOW
99 #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
100 #undef SERIAL_DEBUG_PCI
102 /* Sanity checks */
104 #ifdef CONFIG_SERIAL_MULTIPORT
105 #ifndef CONFIG_SERIAL_SHARE_IRQ
106 #define CONFIG_SERIAL_SHARE_IRQ
107 #endif
108 #endif
110 #ifdef CONFIG_HUB6
111 #ifndef CONFIG_SERIAL_MANY_PORTS
112 #define CONFIG_SERIAL_MANY_PORTS
113 #endif
114 #ifndef CONFIG_SERIAL_SHARE_IRQ
115 #define CONFIG_SERIAL_SHARE_IRQ
116 #endif
117 #endif
119 #define RS_STROBE_TIME (10*HZ)
120 #define RS_ISR_PASS_LIMIT 256
122 #define IRQ_T(state) \
123 ((state->flags & ASYNC_SHARE_IRQ) ? SA_SHIRQ : SA_INTERRUPT)
125 #define SERIAL_INLINE
127 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
128 #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
129 kdevname(tty->device), (info->flags), serial_refcount,info->count,tty->count,s)
130 #else
131 #define DBG_CNT(s)
132 #endif
135 * End of serial driver configuration section.
138 #if (LINUX_VERSION_CODE > 66304)
139 #define NEW_MODULES
140 #ifdef LOCAL_HEADERS /* We're building standalone */
141 #define MODULE
142 #endif
143 #endif
145 #ifdef NEW_MODULES
146 #ifdef MODVERSIONS
147 #include <linux/modversions.h>
148 #endif
149 #else /* !NEW_MODULES */
150 #ifdef MODVERSIONS
151 #define MODULE
152 #endif
153 #endif /* NEW_MODULES */
154 #include <linux/module.h>
155 #include <linux/types.h>
156 #ifdef LOCAL_HEADERS
157 #include "serial_local.h"
158 #else
159 #include <linux/serial.h>
160 #include <linux/serialP.h>
161 #include <linux/serial_reg.h>
162 #include <asm/serial.h>
163 static char *serial_version = "4.30";
164 #endif
166 #include <linux/errno.h>
167 #include <linux/signal.h>
168 #include <linux/sched.h>
169 #include <linux/timer.h>
170 #include <linux/interrupt.h>
171 #include <linux/tty.h>
172 #include <linux/tty_flip.h>
173 #include <linux/major.h>
174 #include <linux/string.h>
175 #include <linux/fcntl.h>
176 #include <linux/ptrace.h>
177 #include <linux/ioport.h>
178 #include <linux/mm.h>
179 #include <linux/malloc.h>
180 #if (LINUX_VERSION_CODE >= 131343) /* 2.1.15 -- XX get correct version */
181 #include <linux/init.h>
182 #else
183 #define __initfunc(x) x
184 #endif
185 #include <linux/delay.h>
186 #ifdef CONFIG_SERIAL_CONSOLE
187 #include <linux/console.h>
188 #endif
189 #ifdef ENABLE_SERIAL_PCI
190 #include <linux/pci.h>
191 #endif
193 #include <asm/system.h>
194 #include <asm/io.h>
195 #include <asm/irq.h>
196 #include <asm/bitops.h>
198 #ifdef CONFIG_MAC_SERIAL
199 #define SERIAL_DEV_OFFSET 2
200 #else
201 #define SERIAL_DEV_OFFSET 0
202 #endif
204 #ifdef SERIAL_INLINE
205 #define _INLINE_ inline
206 #endif
208 static char *serial_name = "Serial driver";
210 static DECLARE_TASK_QUEUE(tq_serial);
212 static struct tty_driver serial_driver, callout_driver;
213 static int serial_refcount;
215 /* serial subtype definitions */
216 #ifndef SERIAL_TYPE_NORMAL
217 #define SERIAL_TYPE_NORMAL 1
218 #define SERIAL_TYPE_CALLOUT 2
219 #endif
221 /* number of characters left in xmit buffer before we ask for more */
222 #define WAKEUP_CHARS 256
225 * IRQ_timeout - How long the timeout should be for each IRQ
226 * should be after the IRQ has been active.
229 static struct async_struct *IRQ_ports[NR_IRQS];
230 #ifdef CONFIG_SERIAL_MULTIPORT
231 static struct rs_multiport_struct rs_multiport[NR_IRQS];
232 #endif
233 static int IRQ_timeout[NR_IRQS];
234 #ifdef CONFIG_SERIAL_CONSOLE
235 static struct console sercons;
236 #endif
238 static unsigned detect_uart_irq (struct serial_state * state);
239 static void autoconfig(struct serial_state * info);
240 static void change_speed(struct async_struct *info, struct termios *old);
241 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
244 * Here we define the default xmit fifo size used for each type of
245 * UART
247 static struct serial_uart_config uart_config[] = {
248 { "unknown", 1, 0 },
249 { "8250", 1, 0 },
250 { "16450", 1, 0 },
251 { "16550", 1, 0 },
252 { "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
253 { "cirrus", 1, 0 }, /* usurped by cyclades.c */
254 { "ST16650", 1, UART_CLEAR_FIFO |UART_STARTECH },
255 { "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO |
256 UART_STARTECH },
257 { "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO},
258 { "Startech", 1, 0}, /* usurped by cyclades.c */
259 { "16C950", 128, UART_CLEAR_FIFO | UART_USE_FIFO},
260 { "ST16654", 64, UART_CLEAR_FIFO | UART_USE_FIFO |
261 UART_STARTECH },
262 { "XR16850", 128, UART_CLEAR_FIFO | UART_USE_FIFO |
263 UART_STARTECH },
264 { 0, 0}
267 static struct serial_state rs_table[RS_TABLE_SIZE] = {
268 SERIAL_PORT_DFNS /* Defined in serial.h */
271 #define NR_PORTS (sizeof(rs_table)/sizeof(struct serial_state))
273 #ifdef ENABLE_SERIAL_PCI
274 #define NR_PCI_BOARDS 8
275 static struct pci_board_inst serial_pci_board[NR_PCI_BOARDS];
276 static int serial_pci_board_idx = 0;
277 #define PCI_BASE_ADDRESS(dev, r) ((dev)->resource[r].start)
278 #endif /* ENABLE_SERIAL_PCI */
280 static struct tty_struct *serial_table[NR_PORTS];
281 static struct termios *serial_termios[NR_PORTS];
282 static struct termios *serial_termios_locked[NR_PORTS];
284 #ifndef MIN
285 #define MIN(a,b) ((a) < (b) ? (a) : (b))
286 #endif
289 * tmp_buf is used as a temporary buffer by serial_write. We need to
290 * lock it in case the copy_from_user blocks while swapping in a page,
291 * and some other program tries to do a serial write at the same time.
292 * Since the lock will only come under contention when the system is
293 * swapping and available memory is low, it makes sense to share one
294 * buffer across all the serial ports, since it significantly saves
295 * memory if large numbers of serial ports are open.
297 static unsigned char *tmp_buf;
298 #ifdef DECLARE_MUTEX
299 static DECLARE_MUTEX(tmp_buf_sem);
300 #else
301 static struct semaphore tmp_buf_sem = MUTEX;
302 #endif
305 * Provide backwards compatibility for kernels prior to 2.1.XX.
307 #if (LINUX_VERSION_CODE < 0x20000)
308 typedef dev_t kdev_t;
309 #endif
311 #if (LINUX_VERSION_CODE < 0x02017E)
312 static signed long schedule_timeout(signed long timeout)
314 unsigned long expire;
316 expire = timeout + jiffies;
318 current->timeout = jiffies + timeout;
319 schedule();
321 timeout = expire - jiffies;
322 return timeout < 0 ? 0 : timeout;
324 #endif
326 #ifndef time_after
327 #define time_after(a,b) ((long)(b) - (long)(a) < 0)
328 #endif
330 #if (LINUX_VERSION_CODE < 0x020100)
331 static inline int irq_cannonicalize(int irq)
333 return ((irq == 2) ? 9 : irq);
335 #endif
337 #if (LINUX_VERSION_CODE < 131336)
338 static int copy_from_user(void *to, const void *from_user, unsigned long len)
340 int error;
342 error = verify_area(VERIFY_READ, from_user, len);
343 if (error)
344 return len;
345 memcpy_fromfs(to, from_user, len);
346 return 0;
349 static int copy_to_user(void *to_user, const void *from, unsigned long len)
351 int error;
353 error = verify_area(VERIFY_WRITE, to_user, len);
354 if (error)
355 return len;
356 memcpy_tofs(to_user, from, len);
357 return 0;
360 static inline int signal_pending(struct task_struct *p)
362 return (p->signal & (~p->blocked != 0));
365 #else
366 #include <asm/uaccess.h>
367 #endif
369 #ifdef CAP_SYS_ADMIN
370 #define serial_isroot() (capable(CAP_SYS_ADMIN))
371 #else
372 #define serial_isroot() (suser())
373 #endif
375 #if (LINUX_VERSION_CODE < 131394) /* 2.1.66 */
376 #define test_and_clear_bit(x,y) clear_bit(x,y)
378 static inline void remove_bh(int nr)
380 bh_base[nr] = NULL;
381 bh_mask &= ~(1 << nr);
383 #endif
386 static inline int serial_paranoia_check(struct async_struct *info,
387 kdev_t device, const char *routine)
389 #ifdef SERIAL_PARANOIA_CHECK
390 static const char *badmagic =
391 "Warning: bad magic number for serial struct (%s) in %s\n";
392 static const char *badinfo =
393 "Warning: null async_struct for (%s) in %s\n";
395 if (!info) {
396 printk(badinfo, kdevname(device), routine);
397 return 1;
399 if (info->magic != SERIAL_MAGIC) {
400 printk(badmagic, kdevname(device), routine);
401 return 1;
403 #endif
404 return 0;
407 static inline unsigned int serial_in(struct async_struct *info, int offset)
409 #ifdef CONFIG_HUB6
410 if (info->hub6) {
411 outb(info->hub6 - 1 + offset, info->port);
412 return inb(info->port+1);
413 } else
414 #endif
415 #ifdef CONFIG_SERIAL_PCI_MEMMAPPED
416 if (info->iomem_base)
417 return readb(info->iomem_base + (offset<<info->iomem_reg_shift));
418 else
419 #endif
420 return inb(info->port + offset);
423 static inline unsigned int serial_inp(struct async_struct *info, int offset)
425 #ifdef CONFIG_HUB6
426 if (info->hub6) {
427 outb(info->hub6 - 1 + offset, info->port);
428 return inb_p(info->port+1);
429 } else
430 #endif
431 #ifdef CONFIG_SERIAL_PCI_MEMMAPPED
432 if (info->iomem_base)
433 return readb(info->iomem_base + (offset<<info->iomem_reg_shift));
434 else
435 #endif
436 #ifdef CONFIG_SERIAL_NOPAUSE_IO
437 return inb(info->port + offset);
438 #else
439 return inb_p(info->port + offset);
440 #endif
443 static inline void serial_out(struct async_struct *info, int offset, int value)
445 #ifdef CONFIG_HUB6
446 if (info->hub6) {
447 outb(info->hub6 - 1 + offset, info->port);
448 outb(value, info->port+1);
449 } else
450 #endif
451 #ifdef CONFIG_SERIAL_PCI_MEMMAPPED
452 if (info->iomem_base)
453 writeb(value, info->iomem_base + (offset<<info->iomem_reg_shift));
454 else
455 #endif
456 outb(value, info->port+offset);
459 static inline void serial_outp(struct async_struct *info, int offset,
460 int value)
462 #ifdef CONFIG_HUB6
463 if (info->hub6) {
464 outb(info->hub6 - 1 + offset, info->port);
465 outb_p(value, info->port+1);
466 } else
467 #endif
468 #ifdef CONFIG_SERIAL_PCI_MEMMAPPED
469 if (info->iomem_base)
470 writeb(value, info->iomem_base + (offset<<info->iomem_reg_shift));
471 else
472 #endif
473 #ifdef CONFIG_SERIAL_NOPAUSE_IO
474 outb(value, info->port+offset);
475 #else
476 outb_p(value, info->port+offset);
477 #endif
481 * For the 16C950
483 void serial_icr_write(struct async_struct *info, int offset, int value)
485 serial_out(info, UART_SCR, offset);
486 serial_out(info, UART_ICR, value);
489 unsigned int serial_icr_read(struct async_struct *info, int offset)
491 int value;
493 serial_icr_write(info, UART_ACR, info->ACR | UART_ACR_ICRRD);
494 serial_out(info, UART_SCR, offset);
495 value = serial_in(info, UART_ICR);
496 serial_icr_write(info, UART_ACR, info->ACR);
497 return value;
501 * ------------------------------------------------------------
502 * rs_stop() and rs_start()
504 * This routines are called before setting or resetting tty->stopped.
505 * They enable or disable transmitter interrupts, as necessary.
506 * ------------------------------------------------------------
508 static void rs_stop(struct tty_struct *tty)
510 struct async_struct *info = (struct async_struct *)tty->driver_data;
511 unsigned long flags;
513 if (serial_paranoia_check(info, tty->device, "rs_stop"))
514 return;
516 save_flags(flags); cli();
517 if (info->IER & UART_IER_THRI) {
518 info->IER &= ~UART_IER_THRI;
519 serial_out(info, UART_IER, info->IER);
521 if (info->state->type == PORT_16C950) {
522 info->ACR |= UART_ACR_TXDIS;
523 serial_icr_write(info, UART_ACR, info->ACR);
525 restore_flags(flags);
528 static void rs_start(struct tty_struct *tty)
530 struct async_struct *info = (struct async_struct *)tty->driver_data;
531 unsigned long flags;
533 if (serial_paranoia_check(info, tty->device, "rs_start"))
534 return;
536 save_flags(flags); cli();
537 if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
538 info->IER |= UART_IER_THRI;
539 serial_out(info, UART_IER, info->IER);
541 if (info->state->type == PORT_16C950) {
542 info->ACR &= ~UART_ACR_TXDIS;
543 serial_icr_write(info, UART_ACR, info->ACR);
545 restore_flags(flags);
549 * ----------------------------------------------------------------------
551 * Here starts the interrupt handling routines. All of the following
552 * subroutines are declared as inline and are folded into
553 * rs_interrupt(). They were separated out for readability's sake.
555 * Note: rs_interrupt() is a "fast" interrupt, which means that it
556 * runs with interrupts turned off. People who may want to modify
557 * rs_interrupt() should try to keep the interrupt handler as fast as
558 * possible. After you are done making modifications, it is not a bad
559 * idea to do:
561 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
563 * and look at the resulting assemble code in serial.s.
565 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
566 * -----------------------------------------------------------------------
570 * This routine is used by the interrupt handler to schedule
571 * processing in the software interrupt portion of the driver.
573 static _INLINE_ void rs_sched_event(struct async_struct *info,
574 int event)
576 info->event |= 1 << event;
577 queue_task(&info->tqueue, &tq_serial);
578 mark_bh(SERIAL_BH);
581 static _INLINE_ void receive_chars(struct async_struct *info,
582 int *status)
584 struct tty_struct *tty = info->tty;
585 unsigned char ch;
586 int ignored = 0;
587 struct async_icount *icount;
589 icount = &info->state->icount;
590 do {
591 ch = serial_inp(info, UART_RX);
592 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
593 break;
594 *tty->flip.char_buf_ptr = ch;
595 icount->rx++;
597 #ifdef SERIAL_DEBUG_INTR
598 printk("DR%02x:%02x...", ch, *status);
599 #endif
600 *tty->flip.flag_buf_ptr = 0;
601 if (*status & (UART_LSR_BI | UART_LSR_PE |
602 UART_LSR_FE | UART_LSR_OE)) {
604 * For statistics only
606 if (*status & UART_LSR_BI) {
607 *status &= ~(UART_LSR_FE | UART_LSR_PE);
608 icount->brk++;
609 } else if (*status & UART_LSR_PE)
610 icount->parity++;
611 else if (*status & UART_LSR_FE)
612 icount->frame++;
613 if (*status & UART_LSR_OE)
614 icount->overrun++;
617 * Now check to see if character should be
618 * ignored, and mask off conditions which
619 * should be ignored.
621 if (*status & info->ignore_status_mask) {
622 if (++ignored > 100)
623 break;
624 goto ignore_char;
626 *status &= info->read_status_mask;
628 if (*status & (UART_LSR_BI)) {
629 #ifdef SERIAL_DEBUG_INTR
630 printk("handling break....");
631 #endif
632 *tty->flip.flag_buf_ptr = TTY_BREAK;
633 if (info->flags & ASYNC_SAK)
634 do_SAK(tty);
635 } else if (*status & UART_LSR_PE)
636 *tty->flip.flag_buf_ptr = TTY_PARITY;
637 else if (*status & UART_LSR_FE)
638 *tty->flip.flag_buf_ptr = TTY_FRAME;
639 if (*status & UART_LSR_OE) {
641 * Overrun is special, since it's
642 * reported immediately, and doesn't
643 * affect the current character
645 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
646 tty->flip.count++;
647 tty->flip.flag_buf_ptr++;
648 tty->flip.char_buf_ptr++;
649 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
653 tty->flip.flag_buf_ptr++;
654 tty->flip.char_buf_ptr++;
655 tty->flip.count++;
656 ignore_char:
657 *status = serial_inp(info, UART_LSR);
658 } while (*status & UART_LSR_DR);
659 #if (LINUX_VERSION_CODE > 131394) /* 2.1.66 */
660 tty_flip_buffer_push(tty);
661 #else
662 queue_task_irq_off(&tty->flip.tqueue, &tq_timer);
663 #endif
666 static _INLINE_ void transmit_chars(struct async_struct *info, int *intr_done)
668 int count;
670 if (info->x_char) {
671 serial_outp(info, UART_TX, info->x_char);
672 info->state->icount.tx++;
673 info->x_char = 0;
674 if (intr_done)
675 *intr_done = 0;
676 return;
678 if ((info->xmit_cnt <= 0) || info->tty->stopped ||
679 info->tty->hw_stopped) {
680 info->IER &= ~UART_IER_THRI;
681 serial_out(info, UART_IER, info->IER);
682 return;
685 count = info->xmit_fifo_size;
686 do {
687 serial_out(info, UART_TX, info->xmit_buf[info->xmit_tail++]);
688 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
689 info->state->icount.tx++;
690 if (--info->xmit_cnt <= 0)
691 break;
692 } while (--count > 0);
694 if (info->xmit_cnt < WAKEUP_CHARS)
695 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
697 #ifdef SERIAL_DEBUG_INTR
698 printk("THRE...");
699 #endif
700 if (intr_done)
701 *intr_done = 0;
703 if (info->xmit_cnt <= 0) {
704 info->IER &= ~UART_IER_THRI;
705 serial_out(info, UART_IER, info->IER);
709 static _INLINE_ void check_modem_status(struct async_struct *info)
711 int status;
712 struct async_icount *icount;
714 status = serial_in(info, UART_MSR);
716 if (status & UART_MSR_ANY_DELTA) {
717 icount = &info->state->icount;
718 /* update input line counters */
719 if (status & UART_MSR_TERI)
720 icount->rng++;
721 if (status & UART_MSR_DDSR)
722 icount->dsr++;
723 if (status & UART_MSR_DDCD) {
724 icount->dcd++;
725 #ifdef CONFIG_HARD_PPS
726 if ((info->flags & ASYNC_HARDPPS_CD) &&
727 (status & UART_MSR_DCD))
728 hardpps();
729 #endif
731 if (status & UART_MSR_DCTS)
732 icount->cts++;
733 wake_up_interruptible(&info->delta_msr_wait);
736 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
737 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
738 printk("ttys%d CD now %s...", info->line,
739 (status & UART_MSR_DCD) ? "on" : "off");
740 #endif
741 if (status & UART_MSR_DCD)
742 wake_up_interruptible(&info->open_wait);
743 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
744 (info->flags & ASYNC_CALLOUT_NOHUP))) {
745 #ifdef SERIAL_DEBUG_OPEN
746 printk("doing serial hangup...");
747 #endif
748 if (info->tty)
749 tty_hangup(info->tty);
752 if (info->flags & ASYNC_CTS_FLOW) {
753 if (info->tty->hw_stopped) {
754 if (status & UART_MSR_CTS) {
755 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
756 printk("CTS tx start...");
757 #endif
758 info->tty->hw_stopped = 0;
759 info->IER |= UART_IER_THRI;
760 serial_out(info, UART_IER, info->IER);
761 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
762 return;
764 } else {
765 if (!(status & UART_MSR_CTS)) {
766 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
767 printk("CTS tx stop...");
768 #endif
769 info->tty->hw_stopped = 1;
770 info->IER &= ~UART_IER_THRI;
771 serial_out(info, UART_IER, info->IER);
777 #ifdef CONFIG_SERIAL_SHARE_IRQ
779 * This is the serial driver's generic interrupt routine
781 static void rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
783 int status;
784 struct async_struct * info;
785 int pass_counter = 0;
786 struct async_struct *end_mark = 0;
787 #ifdef CONFIG_SERIAL_MULTIPORT
788 int first_multi = 0;
789 struct rs_multiport_struct *multi;
790 #endif
792 #ifdef SERIAL_DEBUG_INTR
793 printk("rs_interrupt(%d)...", irq);
794 #endif
796 info = IRQ_ports[irq];
797 if (!info)
798 return;
800 #ifdef CONFIG_SERIAL_MULTIPORT
801 multi = &rs_multiport[irq];
802 if (multi->port_monitor)
803 first_multi = inb(multi->port_monitor);
804 #endif
806 do {
807 if (!info->tty ||
808 (serial_in(info, UART_IIR) & UART_IIR_NO_INT)) {
809 if (!end_mark)
810 end_mark = info;
811 goto next;
813 end_mark = 0;
815 info->last_active = jiffies;
817 status = serial_inp(info, UART_LSR);
818 #ifdef SERIAL_DEBUG_INTR
819 printk("status = %x...", status);
820 #endif
821 if (status & UART_LSR_DR)
822 receive_chars(info, &status);
823 check_modem_status(info);
824 if (status & UART_LSR_THRE)
825 transmit_chars(info, 0);
827 next:
828 info = info->next_port;
829 if (!info) {
830 info = IRQ_ports[irq];
831 if (pass_counter++ > RS_ISR_PASS_LIMIT) {
832 #if 0
833 printk("rs loop break\n");
834 #endif
835 break; /* Prevent infinite loops */
837 continue;
839 } while (end_mark != info);
840 #ifdef CONFIG_SERIAL_MULTIPORT
841 if (multi->port_monitor)
842 printk("rs port monitor (normal) irq %d: 0x%x, 0x%x\n",
843 info->state->irq, first_multi,
844 inb(multi->port_monitor));
845 #endif
846 #ifdef SERIAL_DEBUG_INTR
847 printk("end.\n");
848 #endif
850 #endif /* #ifdef CONFIG_SERIAL_SHARE_IRQ */
854 * This is the serial driver's interrupt routine for a single port
856 static void rs_interrupt_single(int irq, void *dev_id, struct pt_regs * regs)
858 int status;
859 int pass_counter = 0;
860 struct async_struct * info;
861 #ifdef CONFIG_SERIAL_MULTIPORT
862 int first_multi = 0;
863 struct rs_multiport_struct *multi;
864 #endif
866 #ifdef SERIAL_DEBUG_INTR
867 printk("rs_interrupt_single(%d)...", irq);
868 #endif
870 info = IRQ_ports[irq];
871 if (!info || !info->tty)
872 return;
874 #ifdef CONFIG_SERIAL_MULTIPORT
875 multi = &rs_multiport[irq];
876 if (multi->port_monitor)
877 first_multi = inb(multi->port_monitor);
878 #endif
880 do {
881 status = serial_inp(info, UART_LSR);
882 #ifdef SERIAL_DEBUG_INTR
883 printk("status = %x...", status);
884 #endif
885 if (status & UART_LSR_DR)
886 receive_chars(info, &status);
887 check_modem_status(info);
888 if (status & UART_LSR_THRE)
889 transmit_chars(info, 0);
890 if (pass_counter++ > RS_ISR_PASS_LIMIT) {
891 #if 0
892 printk("rs_single loop break.\n");
893 #endif
894 break;
896 } while (!(serial_in(info, UART_IIR) & UART_IIR_NO_INT));
897 info->last_active = jiffies;
898 #ifdef CONFIG_SERIAL_MULTIPORT
899 if (multi->port_monitor)
900 printk("rs port monitor (single) irq %d: 0x%x, 0x%x\n",
901 info->state->irq, first_multi,
902 inb(multi->port_monitor));
903 #endif
904 #ifdef SERIAL_DEBUG_INTR
905 printk("end.\n");
906 #endif
909 #ifdef CONFIG_SERIAL_MULTIPORT
911 * This is the serial driver's for multiport boards
913 static void rs_interrupt_multi(int irq, void *dev_id, struct pt_regs * regs)
915 int status;
916 struct async_struct * info;
917 int pass_counter = 0;
918 int first_multi= 0;
919 struct rs_multiport_struct *multi;
921 #ifdef SERIAL_DEBUG_INTR
922 printk("rs_interrupt_multi(%d)...", irq);
923 #endif
925 info = IRQ_ports[irq];
926 if (!info)
927 return;
928 multi = &rs_multiport[irq];
929 if (!multi->port1) {
930 /* Should never happen */
931 printk("rs_interrupt_multi: NULL port1!\n");
932 return;
934 if (multi->port_monitor)
935 first_multi = inb(multi->port_monitor);
937 while (1) {
938 if (!info->tty ||
939 (serial_in(info, UART_IIR) & UART_IIR_NO_INT))
940 goto next;
942 info->last_active = jiffies;
944 status = serial_inp(info, UART_LSR);
945 #ifdef SERIAL_DEBUG_INTR
946 printk("status = %x...", status);
947 #endif
948 if (status & UART_LSR_DR)
949 receive_chars(info, &status);
950 check_modem_status(info);
951 if (status & UART_LSR_THRE)
952 transmit_chars(info, 0);
954 next:
955 info = info->next_port;
956 if (info)
957 continue;
959 info = IRQ_ports[irq];
960 if (pass_counter++ > RS_ISR_PASS_LIMIT) {
961 #if 1
962 printk("rs_multi loop break\n");
963 #endif
964 break; /* Prevent infinite loops */
966 if (multi->port_monitor)
967 printk("rs port monitor irq %d: 0x%x, 0x%x\n",
968 info->state->irq, first_multi,
969 inb(multi->port_monitor));
970 if ((inb(multi->port1) & multi->mask1) != multi->match1)
971 continue;
972 if (!multi->port2)
973 break;
974 if ((inb(multi->port2) & multi->mask2) != multi->match2)
975 continue;
976 if (!multi->port3)
977 break;
978 if ((inb(multi->port3) & multi->mask3) != multi->match3)
979 continue;
980 if (!multi->port4)
981 break;
982 if ((inb(multi->port4) & multi->mask4) != multi->match4)
983 continue;
984 break;
986 #ifdef SERIAL_DEBUG_INTR
987 printk("end.\n");
988 #endif
990 #endif
993 * -------------------------------------------------------------------
994 * Here ends the serial interrupt routines.
995 * -------------------------------------------------------------------
999 * This routine is used to handle the "bottom half" processing for the
1000 * serial driver, known also the "software interrupt" processing.
1001 * This processing is done at the kernel interrupt level, after the
1002 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
1003 * is where time-consuming activities which can not be done in the
1004 * interrupt driver proper are done; the interrupt driver schedules
1005 * them using rs_sched_event(), and they get done here.
1007 static void do_serial_bh(void)
1009 run_task_queue(&tq_serial);
1012 static void do_softint(void *private_)
1014 struct async_struct *info = (struct async_struct *) private_;
1015 struct tty_struct *tty;
1017 tty = info->tty;
1018 if (!tty)
1019 return;
1021 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
1022 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1023 tty->ldisc.write_wakeup)
1024 (tty->ldisc.write_wakeup)(tty);
1025 wake_up_interruptible(&tty->write_wait);
1030 * This subroutine is called when the RS_TIMER goes off. It is used
1031 * by the serial driver to handle ports that do not have an interrupt
1032 * (irq=0). This doesn't work very well for 16450's, but gives barely
1033 * passable results for a 16550A. (Although at the expense of much
1034 * CPU overhead).
1036 static void rs_timer(void)
1038 static unsigned long last_strobe = 0;
1039 struct async_struct *info;
1040 unsigned int i;
1041 unsigned long flags;
1043 if ((jiffies - last_strobe) >= RS_STROBE_TIME) {
1044 for (i=1; i < NR_IRQS; i++) {
1045 info = IRQ_ports[i];
1046 if (!info)
1047 continue;
1048 save_flags(flags); cli();
1049 #ifdef CONFIG_SERIAL_SHARE_IRQ
1050 if (info->next_port) {
1051 do {
1052 serial_out(info, UART_IER, 0);
1053 info->IER |= UART_IER_THRI;
1054 serial_out(info, UART_IER, info->IER);
1055 info = info->next_port;
1056 } while (info);
1057 #ifdef CONFIG_SERIAL_MULTIPORT
1058 if (rs_multiport[i].port1)
1059 rs_interrupt_multi(i, NULL, NULL);
1060 else
1061 #endif
1062 rs_interrupt(i, NULL, NULL);
1063 } else
1064 #endif /* CONFIG_SERIAL_SHARE_IRQ */
1065 rs_interrupt_single(i, NULL, NULL);
1066 restore_flags(flags);
1069 last_strobe = jiffies;
1070 timer_table[RS_TIMER].expires = jiffies + RS_STROBE_TIME;
1071 timer_active |= 1 << RS_TIMER;
1073 if (IRQ_ports[0]) {
1074 save_flags(flags); cli();
1075 #ifdef CONFIG_SERIAL_SHARE_IRQ
1076 rs_interrupt(0, NULL, NULL);
1077 #else
1078 rs_interrupt_single(0, NULL, NULL);
1079 #endif
1080 restore_flags(flags);
1082 timer_table[RS_TIMER].expires = jiffies + IRQ_timeout[0] - 2;
1087 * ---------------------------------------------------------------
1088 * Low level utility subroutines for the serial driver: routines to
1089 * figure out the appropriate timeout for an interrupt chain, routines
1090 * to initialize and startup a serial port, and routines to shutdown a
1091 * serial port. Useful stuff like that.
1092 * ---------------------------------------------------------------
1096 * This routine figures out the correct timeout for a particular IRQ.
1097 * It uses the smallest timeout of all of the serial ports in a
1098 * particular interrupt chain. Now only used for IRQ 0....
1100 static void figure_IRQ_timeout(int irq)
1102 struct async_struct *info;
1103 int timeout = 60*HZ; /* 60 seconds === a long time :-) */
1105 info = IRQ_ports[irq];
1106 if (!info) {
1107 IRQ_timeout[irq] = 60*HZ;
1108 return;
1110 while (info) {
1111 if (info->timeout < timeout)
1112 timeout = info->timeout;
1113 info = info->next_port;
1115 if (!irq)
1116 timeout = timeout / 2;
1117 IRQ_timeout[irq] = timeout ? timeout : 1;
1120 static int startup(struct async_struct * info)
1122 unsigned long flags;
1123 int retval=0;
1124 void (*handler)(int, void *, struct pt_regs *);
1125 struct serial_state *state= info->state;
1126 unsigned long page;
1127 #ifdef CONFIG_SERIAL_MANY_PORTS
1128 unsigned short ICP;
1129 #endif
1131 page = get_zeroed_page(GFP_KERNEL);
1132 if (!page)
1133 return -ENOMEM;
1135 save_flags(flags); cli();
1137 if (info->flags & ASYNC_INITIALIZED) {
1138 free_page(page);
1139 goto errout;
1142 if (!CONFIGURED_SERIAL_PORT(state) || !state->type) {
1143 if (info->tty)
1144 set_bit(TTY_IO_ERROR, &info->tty->flags);
1145 free_page(page);
1146 goto errout;
1148 if (info->xmit_buf)
1149 free_page(page);
1150 else
1151 info->xmit_buf = (unsigned char *) page;
1153 #ifdef SERIAL_DEBUG_OPEN
1154 printk("starting up ttys%d (irq %d)...", info->line, state->irq);
1155 #endif
1157 if (uart_config[info->state->type].flags & UART_STARTECH) {
1158 /* Wake up UART */
1159 serial_outp(info, UART_LCR, 0xBF);
1160 serial_outp(info, UART_EFR, UART_EFR_ECB);
1162 * Turn off LCR == 0xBF so we actually set the IER
1163 * register on the XR16C850
1165 serial_outp(info, UART_LCR, 0);
1166 serial_outp(info, UART_IER, 0);
1168 * Now reset LCR so we can turn off the ECB bit
1170 serial_outp(info, UART_LCR, 0xBF);
1171 serial_outp(info, UART_EFR, 0);
1173 * For a XR16C850, we need to set the trigger levels
1175 if (info->state->type == PORT_16850) {
1176 serial_outp(info, UART_FCTR, UART_FCTR_TRGD |
1177 UART_FCTR_RX);
1178 serial_outp(info, UART_TRG, UART_TRG_96);
1179 serial_outp(info, UART_FCTR, UART_FCTR_TRGD |
1180 UART_FCTR_TX);
1181 serial_outp(info, UART_TRG, UART_TRG_96);
1183 serial_outp(info, UART_LCR, 0);
1186 if (info->state->type == PORT_16750) {
1187 /* Wake up UART */
1188 serial_outp(info, UART_IER, 0);
1191 if (info->state->type == PORT_16C950) {
1192 /* Wake up and initialize UART */
1193 info->ACR = 0;
1194 serial_outp(info, UART_LCR, 0xBF);
1195 serial_outp(info, UART_EFR, UART_EFR_ECB);
1196 serial_outp(info, UART_IER, 0);
1197 serial_outp(info, UART_LCR, 0);
1198 serial_icr_write(info, UART_CSR, 0); /* Reset the UART */
1199 serial_outp(info, UART_LCR, 0xBF);
1200 serial_outp(info, UART_EFR, UART_EFR_ECB);
1201 serial_outp(info, UART_LCR, 0);
1205 * Clear the FIFO buffers and disable them
1206 * (they will be reenabled in change_speed())
1208 if (uart_config[state->type].flags & UART_CLEAR_FIFO) {
1209 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
1210 serial_outp(info, UART_FCR, (UART_FCR_ENABLE_FIFO |
1211 UART_FCR_CLEAR_RCVR |
1212 UART_FCR_CLEAR_XMIT));
1213 serial_outp(info, UART_FCR, 0);
1217 * Clear the interrupt registers.
1219 (void) serial_inp(info, UART_LSR);
1220 (void) serial_inp(info, UART_RX);
1221 (void) serial_inp(info, UART_IIR);
1222 (void) serial_inp(info, UART_MSR);
1225 * At this point there's no way the LSR could still be 0xFF;
1226 * if it is, then bail out, because there's likely no UART
1227 * here.
1229 if (serial_inp(info, UART_LSR) == 0xff) {
1230 printk("LSR safety check engaged!\n");
1231 if (serial_isroot()) {
1232 if (info->tty)
1233 set_bit(TTY_IO_ERROR, &info->tty->flags);
1234 } else
1235 retval = -ENODEV;
1236 goto errout;
1240 * Allocate the IRQ if necessary
1242 if (state->irq && (!IRQ_ports[state->irq] ||
1243 !IRQ_ports[state->irq]->next_port)) {
1244 if (IRQ_ports[state->irq]) {
1245 #ifdef CONFIG_SERIAL_SHARE_IRQ
1246 free_irq(state->irq, NULL);
1247 #ifdef CONFIG_SERIAL_MULTIPORT
1248 if (rs_multiport[state->irq].port1)
1249 handler = rs_interrupt_multi;
1250 else
1251 #endif
1252 handler = rs_interrupt;
1253 #else
1254 retval = -EBUSY;
1255 goto errout;
1256 #endif /* CONFIG_SERIAL_SHARE_IRQ */
1257 } else
1258 handler = rs_interrupt_single;
1260 retval = request_irq(state->irq, handler, IRQ_T(state),
1261 "serial", NULL);
1262 if (retval) {
1263 if (serial_isroot()) {
1264 if (info->tty)
1265 set_bit(TTY_IO_ERROR,
1266 &info->tty->flags);
1267 retval = 0;
1269 goto errout;
1274 * Insert serial port into IRQ chain.
1276 info->prev_port = 0;
1277 info->next_port = IRQ_ports[state->irq];
1278 if (info->next_port)
1279 info->next_port->prev_port = info;
1280 IRQ_ports[state->irq] = info;
1281 figure_IRQ_timeout(state->irq);
1284 * Now, initialize the UART
1286 serial_outp(info, UART_LCR, UART_LCR_WLEN8); /* reset DLAB */
1288 info->MCR = 0;
1289 if (info->tty->termios->c_cflag & CBAUD)
1290 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
1291 #ifdef CONFIG_SERIAL_MANY_PORTS
1292 if (info->flags & ASYNC_FOURPORT) {
1293 if (state->irq == 0)
1294 info->MCR |= UART_MCR_OUT1;
1295 } else
1296 #endif
1298 if (state->irq != 0)
1299 info->MCR |= UART_MCR_OUT2;
1301 info->MCR |= ALPHA_KLUDGE_MCR; /* Don't ask */
1302 serial_outp(info, UART_MCR, info->MCR);
1305 * Finally, enable interrupts
1307 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
1308 serial_outp(info, UART_IER, info->IER); /* enable interrupts */
1310 #ifdef CONFIG_SERIAL_MANY_PORTS
1311 if (info->flags & ASYNC_FOURPORT) {
1312 /* Enable interrupts on the AST Fourport board */
1313 ICP = (info->port & 0xFE0) | 0x01F;
1314 outb_p(0x80, ICP);
1315 (void) inb_p(ICP);
1317 #endif
1320 * And clear the interrupt registers again for luck.
1322 (void)serial_inp(info, UART_LSR);
1323 (void)serial_inp(info, UART_RX);
1324 (void)serial_inp(info, UART_IIR);
1325 (void)serial_inp(info, UART_MSR);
1327 if (info->tty)
1328 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1329 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1332 * Set up serial timers...
1334 timer_table[RS_TIMER].expires = jiffies + 2*HZ/100;
1335 timer_active |= 1 << RS_TIMER;
1338 * Set up the tty->alt_speed kludge
1340 #if (LINUX_VERSION_CODE >= 131394) /* Linux 2.1.66 */
1341 if (info->tty) {
1342 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1343 info->tty->alt_speed = 57600;
1344 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1345 info->tty->alt_speed = 115200;
1346 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1347 info->tty->alt_speed = 230400;
1348 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1349 info->tty->alt_speed = 460800;
1351 #endif
1354 * and set the speed of the serial port
1356 change_speed(info, 0);
1358 info->flags |= ASYNC_INITIALIZED;
1359 restore_flags(flags);
1360 return 0;
1362 errout:
1363 restore_flags(flags);
1364 return retval;
1368 * This routine will shutdown a serial port; interrupts are disabled, and
1369 * DTR is dropped if the hangup on close termio flag is on.
1371 static void shutdown(struct async_struct * info)
1373 unsigned long flags;
1374 struct serial_state *state;
1375 int retval;
1377 if (!(info->flags & ASYNC_INITIALIZED))
1378 return;
1380 state = info->state;
1382 #ifdef SERIAL_DEBUG_OPEN
1383 printk("Shutting down serial port %d (irq %d)....", info->line,
1384 state->irq);
1385 #endif
1387 save_flags(flags); cli(); /* Disable interrupts */
1390 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1391 * here so the queue might never be waken up
1393 wake_up_interruptible(&info->delta_msr_wait);
1396 * First unlink the serial port from the IRQ chain...
1398 if (info->next_port)
1399 info->next_port->prev_port = info->prev_port;
1400 if (info->prev_port)
1401 info->prev_port->next_port = info->next_port;
1402 else
1403 IRQ_ports[state->irq] = info->next_port;
1404 figure_IRQ_timeout(state->irq);
1407 * Free the IRQ, if necessary
1409 if (state->irq && (!IRQ_ports[state->irq] ||
1410 !IRQ_ports[state->irq]->next_port)) {
1411 if (IRQ_ports[state->irq]) {
1412 free_irq(state->irq, NULL);
1413 retval = request_irq(state->irq, rs_interrupt_single,
1414 IRQ_T(state), "serial", NULL);
1416 if (retval)
1417 printk("serial shutdown: request_irq: error %d"
1418 " Couldn't reacquire IRQ.\n", retval);
1419 } else
1420 free_irq(state->irq, NULL);
1423 if (info->xmit_buf) {
1424 free_page((unsigned long) info->xmit_buf);
1425 info->xmit_buf = 0;
1428 info->IER = 0;
1429 serial_outp(info, UART_IER, 0x00); /* disable all intrs */
1430 #ifdef CONFIG_SERIAL_MANY_PORTS
1431 if (info->flags & ASYNC_FOURPORT) {
1432 /* reset interrupts on the AST Fourport board */
1433 (void) inb((info->port & 0xFE0) | 0x01F);
1434 info->MCR |= UART_MCR_OUT1;
1435 } else
1436 #endif
1437 info->MCR &= ~UART_MCR_OUT2;
1438 info->MCR |= ALPHA_KLUDGE_MCR; /* Don't ask */
1440 /* disable break condition */
1441 serial_out(info, UART_LCR, serial_inp(info, UART_LCR) & ~UART_LCR_SBC);
1443 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
1444 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1445 serial_outp(info, UART_MCR, info->MCR);
1447 /* disable FIFO's */
1448 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
1449 UART_FCR_CLEAR_XMIT));
1450 (void)serial_in(info, UART_RX); /* read data port to reset things */
1452 if (info->tty)
1453 set_bit(TTY_IO_ERROR, &info->tty->flags);
1455 if (uart_config[info->state->type].flags & UART_STARTECH) {
1456 /* Arrange to enter sleep mode */
1457 serial_outp(info, UART_LCR, 0xBF);
1458 serial_outp(info, UART_EFR, UART_EFR_ECB);
1459 serial_outp(info, UART_IER, UART_IERX_SLEEP);
1460 serial_outp(info, UART_LCR, 0);
1462 if (info->state->type == PORT_16750) {
1463 /* Arrange to enter sleep mode */
1464 serial_outp(info, UART_IER, UART_IERX_SLEEP);
1466 info->flags &= ~ASYNC_INITIALIZED;
1467 restore_flags(flags);
1470 #if (LINUX_VERSION_CODE < 131394) /* Linux 2.1.66 */
1471 static int baud_table[] = {
1472 0, 50, 75, 110, 134, 150, 200, 300,
1473 600, 1200, 1800, 2400, 4800, 9600, 19200,
1474 38400, 57600, 115200, 230400, 460800, 0 };
1476 static int tty_get_baud_rate(struct tty_struct *tty)
1478 struct async_struct * info = (struct async_struct *)tty->driver_data;
1479 unsigned int cflag, i;
1481 cflag = tty->termios->c_cflag;
1483 i = cflag & CBAUD;
1484 if (i & CBAUDEX) {
1485 i &= ~CBAUDEX;
1486 if (i < 1 || i > 2)
1487 tty->termios->c_cflag &= ~CBAUDEX;
1488 else
1489 i += 15;
1491 if (i == 15) {
1492 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1493 i += 1;
1494 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1495 i += 2;
1497 return baud_table[i];
1499 #endif
1502 * This routine is called to set the UART divisor registers to match
1503 * the specified baud rate for a serial port.
1505 static void change_speed(struct async_struct *info,
1506 struct termios *old_termios)
1508 int quot = 0, baud_base, baud;
1509 unsigned cflag, cval, fcr = 0;
1510 int bits;
1511 unsigned long flags;
1513 if (!info->tty || !info->tty->termios)
1514 return;
1515 cflag = info->tty->termios->c_cflag;
1516 if (!CONFIGURED_SERIAL_PORT(info))
1517 return;
1519 /* byte size and parity */
1520 switch (cflag & CSIZE) {
1521 case CS5: cval = 0x00; bits = 7; break;
1522 case CS6: cval = 0x01; bits = 8; break;
1523 case CS7: cval = 0x02; bits = 9; break;
1524 case CS8: cval = 0x03; bits = 10; break;
1525 /* Never happens, but GCC is too dumb to figure it out */
1526 default: cval = 0x00; bits = 7; break;
1528 if (cflag & CSTOPB) {
1529 cval |= 0x04;
1530 bits++;
1532 if (cflag & PARENB) {
1533 cval |= UART_LCR_PARITY;
1534 bits++;
1536 if (!(cflag & PARODD))
1537 cval |= UART_LCR_EPAR;
1538 #ifdef CMSPAR
1539 if (cflag & CMSPAR)
1540 cval |= UART_LCR_SPAR;
1541 #endif
1543 /* Determine divisor based on baud rate */
1544 baud = tty_get_baud_rate(info->tty);
1545 if (!baud)
1546 baud = 9600; /* B0 transition handled in rs_set_termios */
1547 baud_base = info->state->baud_base;
1548 if (info->state->type == PORT_16C950) {
1549 if (baud <= baud_base)
1550 serial_icr_write(info, UART_TCR, 0);
1551 else if (baud <= 2*baud_base) {
1552 serial_icr_write(info, UART_TCR, 0x8);
1553 baud_base = baud_base * 2;
1554 } else if (baud <= 4*baud_base) {
1555 serial_icr_write(info, UART_TCR, 0x4);
1556 baud_base = baud_base * 4;
1557 } else
1558 serial_icr_write(info, UART_TCR, 0);
1560 if (baud == 38400 &&
1561 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1562 quot = info->state->custom_divisor;
1563 else {
1564 if (baud == 134)
1565 /* Special case since 134 is really 134.5 */
1566 quot = (2*baud_base / 269);
1567 else if (baud)
1568 quot = baud_base / baud;
1570 /* If the quotient is zero refuse the change */
1571 if (!quot && old_termios) {
1572 info->tty->termios->c_cflag &= ~CBAUD;
1573 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
1574 baud = tty_get_baud_rate(info->tty);
1575 if (!baud)
1576 baud = 9600;
1577 if (baud == 38400 &&
1578 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1579 quot = info->state->custom_divisor;
1580 else {
1581 if (baud == 134)
1582 /* Special case since 134 is really 134.5 */
1583 quot = (2*baud_base / 269);
1584 else if (baud)
1585 quot = baud_base / baud;
1588 /* As a last resort, if the quotient is zero, default to 9600 bps */
1589 if (!quot)
1590 quot = baud_base / 9600;
1591 info->quot = quot;
1592 info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
1593 info->timeout += HZ/50; /* Add .02 seconds of slop */
1595 /* Set up FIFO's */
1596 if (uart_config[info->state->type].flags & UART_USE_FIFO) {
1597 if ((info->state->baud_base / quot) < 2400)
1598 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
1599 else
1600 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
1602 if (info->state->type == PORT_16750)
1603 fcr |= UART_FCR7_64BYTE;
1605 /* CTS flow control flag and modem status interrupts */
1606 info->IER &= ~UART_IER_MSI;
1607 if (info->flags & ASYNC_HARDPPS_CD)
1608 info->IER |= UART_IER_MSI;
1609 if (cflag & CRTSCTS) {
1610 info->flags |= ASYNC_CTS_FLOW;
1611 info->IER |= UART_IER_MSI;
1612 } else
1613 info->flags &= ~ASYNC_CTS_FLOW;
1614 if (cflag & CLOCAL)
1615 info->flags &= ~ASYNC_CHECK_CD;
1616 else {
1617 info->flags |= ASYNC_CHECK_CD;
1618 info->IER |= UART_IER_MSI;
1620 serial_out(info, UART_IER, info->IER);
1623 * Set up parity check flag
1625 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1627 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1628 if (I_INPCK(info->tty))
1629 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1630 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1631 info->read_status_mask |= UART_LSR_BI;
1634 * Characters to ignore
1636 info->ignore_status_mask = 0;
1637 if (I_IGNPAR(info->tty))
1638 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1639 if (I_IGNBRK(info->tty)) {
1640 info->ignore_status_mask |= UART_LSR_BI;
1642 * If we're ignore parity and break indicators, ignore
1643 * overruns too. (For real raw support).
1645 if (I_IGNPAR(info->tty))
1646 info->ignore_status_mask |= UART_LSR_OE;
1649 * !!! ignore all characters if CREAD is not set
1651 if ((cflag & CREAD) == 0)
1652 info->ignore_status_mask |= UART_LSR_DR;
1653 save_flags(flags); cli();
1654 if (uart_config[info->state->type].flags & UART_STARTECH) {
1655 serial_outp(info, UART_LCR, 0xBF);
1656 serial_outp(info, UART_EFR,
1657 (cflag & CRTSCTS) ? UART_EFR_CTS : 0);
1659 serial_outp(info, UART_LCR, cval | UART_LCR_DLAB); /* set DLAB */
1660 serial_outp(info, UART_DLL, quot & 0xff); /* LS of divisor */
1661 serial_outp(info, UART_DLM, quot >> 8); /* MS of divisor */
1662 if (info->state->type == PORT_16750)
1663 serial_outp(info, UART_FCR, fcr); /* set fcr */
1664 serial_outp(info, UART_LCR, cval); /* reset DLAB */
1665 info->LCR = cval; /* Save LCR */
1666 if (info->state->type != PORT_16750)
1667 serial_outp(info, UART_FCR, fcr); /* set fcr */
1668 restore_flags(flags);
1671 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
1673 struct async_struct *info = (struct async_struct *)tty->driver_data;
1674 unsigned long flags;
1676 if (serial_paranoia_check(info, tty->device, "rs_put_char"))
1677 return;
1679 if (!tty || !info->xmit_buf)
1680 return;
1682 save_flags(flags); cli();
1683 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
1684 restore_flags(flags);
1685 return;
1688 info->xmit_buf[info->xmit_head++] = ch;
1689 info->xmit_head &= SERIAL_XMIT_SIZE-1;
1690 info->xmit_cnt++;
1691 restore_flags(flags);
1694 static void rs_flush_chars(struct tty_struct *tty)
1696 struct async_struct *info = (struct async_struct *)tty->driver_data;
1697 unsigned long flags;
1699 if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
1700 return;
1702 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1703 !info->xmit_buf)
1704 return;
1706 save_flags(flags); cli();
1707 info->IER |= UART_IER_THRI;
1708 serial_out(info, UART_IER, info->IER);
1709 restore_flags(flags);
1712 static int rs_write(struct tty_struct * tty, int from_user,
1713 const unsigned char *buf, int count)
1715 int c, ret = 0;
1716 struct async_struct *info = (struct async_struct *)tty->driver_data;
1717 unsigned long flags;
1719 if (serial_paranoia_check(info, tty->device, "rs_write"))
1720 return 0;
1722 if (!tty || !info->xmit_buf || !tmp_buf)
1723 return 0;
1725 save_flags(flags);
1726 if (from_user) {
1727 down(&tmp_buf_sem);
1728 while (1) {
1729 c = MIN(count,
1730 MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1731 SERIAL_XMIT_SIZE - info->xmit_head));
1732 if (c <= 0)
1733 break;
1735 c -= copy_from_user(tmp_buf, buf, c);
1736 if (!c) {
1737 if (!ret)
1738 ret = -EFAULT;
1739 break;
1741 cli();
1742 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1743 SERIAL_XMIT_SIZE - info->xmit_head));
1744 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1745 info->xmit_head = ((info->xmit_head + c) &
1746 (SERIAL_XMIT_SIZE-1));
1747 info->xmit_cnt += c;
1748 restore_flags(flags);
1749 buf += c;
1750 count -= c;
1751 ret += c;
1753 up(&tmp_buf_sem);
1754 } else {
1755 while (1) {
1756 cli();
1757 c = MIN(count,
1758 MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1759 SERIAL_XMIT_SIZE - info->xmit_head));
1760 if (c <= 0) {
1761 restore_flags(flags);
1762 break;
1764 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1765 info->xmit_head = ((info->xmit_head + c) &
1766 (SERIAL_XMIT_SIZE-1));
1767 info->xmit_cnt += c;
1768 restore_flags(flags);
1769 buf += c;
1770 count -= c;
1771 ret += c;
1774 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1775 !(info->IER & UART_IER_THRI)) {
1776 info->IER |= UART_IER_THRI;
1777 serial_out(info, UART_IER, info->IER);
1779 return ret;
1782 static int rs_write_room(struct tty_struct *tty)
1784 struct async_struct *info = (struct async_struct *)tty->driver_data;
1785 int ret;
1787 if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1788 return 0;
1789 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1790 if (ret < 0)
1791 ret = 0;
1792 return ret;
1795 static int rs_chars_in_buffer(struct tty_struct *tty)
1797 struct async_struct *info = (struct async_struct *)tty->driver_data;
1799 if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1800 return 0;
1801 return info->xmit_cnt;
1804 static void rs_flush_buffer(struct tty_struct *tty)
1806 struct async_struct *info = (struct async_struct *)tty->driver_data;
1807 unsigned long flags;
1809 if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1810 return;
1811 save_flags(flags); cli();
1812 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1813 restore_flags(flags);
1814 wake_up_interruptible(&tty->write_wait);
1815 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1816 tty->ldisc.write_wakeup)
1817 (tty->ldisc.write_wakeup)(tty);
1821 * This function is used to send a high-priority XON/XOFF character to
1822 * the device
1824 static void rs_send_xchar(struct tty_struct *tty, char ch)
1826 struct async_struct *info = (struct async_struct *)tty->driver_data;
1828 if (serial_paranoia_check(info, tty->device, "rs_send_char"))
1829 return;
1831 info->x_char = ch;
1832 if (ch) {
1833 /* Make sure transmit interrupts are on */
1834 info->IER |= UART_IER_THRI;
1835 serial_out(info, UART_IER, info->IER);
1840 * ------------------------------------------------------------
1841 * rs_throttle()
1843 * This routine is called by the upper-layer tty layer to signal that
1844 * incoming characters should be throttled.
1845 * ------------------------------------------------------------
1847 static void rs_throttle(struct tty_struct * tty)
1849 struct async_struct *info = (struct async_struct *)tty->driver_data;
1850 unsigned long flags;
1851 #ifdef SERIAL_DEBUG_THROTTLE
1852 char buf[64];
1854 printk("throttle %s: %d....\n", tty_name(tty, buf),
1855 tty->ldisc.chars_in_buffer(tty));
1856 #endif
1858 if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1859 return;
1861 if (I_IXOFF(tty))
1862 rs_send_xchar(tty, STOP_CHAR(tty));
1864 if (tty->termios->c_cflag & CRTSCTS)
1865 info->MCR &= ~UART_MCR_RTS;
1867 save_flags(flags); cli();
1868 serial_out(info, UART_MCR, info->MCR);
1869 restore_flags(flags);
1872 static void rs_unthrottle(struct tty_struct * tty)
1874 struct async_struct *info = (struct async_struct *)tty->driver_data;
1875 unsigned long flags;
1876 #ifdef SERIAL_DEBUG_THROTTLE
1877 char buf[64];
1879 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1880 tty->ldisc.chars_in_buffer(tty));
1881 #endif
1883 if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1884 return;
1886 if (I_IXOFF(tty)) {
1887 if (info->x_char)
1888 info->x_char = 0;
1889 else
1890 rs_send_xchar(tty, START_CHAR(tty));
1892 if (tty->termios->c_cflag & CRTSCTS)
1893 info->MCR |= UART_MCR_RTS;
1894 save_flags(flags); cli();
1895 serial_out(info, UART_MCR, info->MCR);
1896 restore_flags(flags);
1900 * ------------------------------------------------------------
1901 * rs_ioctl() and friends
1902 * ------------------------------------------------------------
1905 static int get_serial_info(struct async_struct * info,
1906 struct serial_struct * retinfo)
1908 struct serial_struct tmp;
1909 struct serial_state *state = info->state;
1911 if (!retinfo)
1912 return -EFAULT;
1913 memset(&tmp, 0, sizeof(tmp));
1914 tmp.type = state->type;
1915 tmp.line = state->line;
1916 tmp.port = state->port;
1917 tmp.irq = state->irq;
1918 tmp.flags = state->flags;
1919 tmp.xmit_fifo_size = state->xmit_fifo_size;
1920 tmp.baud_base = state->baud_base;
1921 tmp.close_delay = state->close_delay;
1922 tmp.closing_wait = state->closing_wait;
1923 tmp.custom_divisor = state->custom_divisor;
1924 tmp.hub6 = state->hub6;
1925 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1926 return -EFAULT;
1927 return 0;
1930 static int set_serial_info(struct async_struct * info,
1931 struct serial_struct * new_info)
1933 struct serial_struct new_serial;
1934 struct serial_state old_state, *state;
1935 unsigned int i,change_irq,change_port;
1936 int retval = 0;
1938 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1939 return -EFAULT;
1940 state = info->state;
1941 old_state = *state;
1943 change_irq = new_serial.irq != state->irq;
1944 change_port = (new_serial.port != state->port) ||
1945 (new_serial.hub6 != state->hub6);
1947 if (!serial_isroot()) {
1948 if (change_irq || change_port ||
1949 (new_serial.baud_base != state->baud_base) ||
1950 (new_serial.type != state->type) ||
1951 (new_serial.close_delay != state->close_delay) ||
1952 (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1953 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1954 (state->flags & ~ASYNC_USR_MASK)))
1955 return -EPERM;
1956 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
1957 (new_serial.flags & ASYNC_USR_MASK));
1958 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1959 (new_serial.flags & ASYNC_USR_MASK));
1960 state->custom_divisor = new_serial.custom_divisor;
1961 goto check_and_exit;
1964 new_serial.irq = irq_cannonicalize(new_serial.irq);
1966 if ((new_serial.irq >= NR_IRQS) || (new_serial.port > 0xffff) ||
1967 (new_serial.baud_base < 9600)|| (new_serial.type < PORT_UNKNOWN) ||
1968 (new_serial.type > PORT_MAX) || (new_serial.type == PORT_CIRRUS) ||
1969 (new_serial.type == PORT_STARTECH)) {
1970 return -EINVAL;
1973 if ((new_serial.type != state->type) ||
1974 (new_serial.xmit_fifo_size <= 0))
1975 new_serial.xmit_fifo_size =
1976 uart_config[state->type].dfl_xmit_fifo_size;
1978 /* Make sure address is not already in use */
1979 if (new_serial.type) {
1980 for (i = 0 ; i < NR_PORTS; i++)
1981 if ((state != &rs_table[i]) &&
1982 (rs_table[i].port == new_serial.port) &&
1983 rs_table[i].type)
1984 return -EADDRINUSE;
1987 if ((change_port || change_irq) && (state->count > 1))
1988 return -EBUSY;
1991 * OK, past this point, all the error checking has been done.
1992 * At this point, we start making changes.....
1995 state->baud_base = new_serial.baud_base;
1996 state->flags = ((state->flags & ~ASYNC_FLAGS) |
1997 (new_serial.flags & ASYNC_FLAGS));
1998 info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
1999 (info->flags & ASYNC_INTERNAL_FLAGS));
2000 state->custom_divisor = new_serial.custom_divisor;
2001 state->type = new_serial.type;
2002 state->close_delay = new_serial.close_delay * HZ/100;
2003 state->closing_wait = new_serial.closing_wait * HZ/100;
2004 #if (LINUX_VERSION_CODE > 0x200100)
2005 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2006 #endif
2007 info->xmit_fifo_size = state->xmit_fifo_size =
2008 new_serial.xmit_fifo_size;
2010 if (state->type != PORT_UNKNOWN && state->port)
2011 release_region(state->port,8);
2012 if (change_port || change_irq) {
2014 * We need to shutdown the serial port at the old
2015 * port/irq combination.
2017 shutdown(info);
2018 state->irq = new_serial.irq;
2019 info->port = state->port = new_serial.port;
2020 info->hub6 = state->hub6 = new_serial.hub6;
2022 if ((state->type != PORT_UNKNOWN) && state->port)
2023 request_region(state->port,8,"serial(set)");
2026 check_and_exit:
2027 if (!state->port || !state->type)
2028 return 0;
2029 if (info->flags & ASYNC_INITIALIZED) {
2030 if (((old_state.flags & ASYNC_SPD_MASK) !=
2031 (state->flags & ASYNC_SPD_MASK)) ||
2032 (old_state.custom_divisor != state->custom_divisor)) {
2033 #if (LINUX_VERSION_CODE >= 131394) /* Linux 2.1.66 */
2034 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
2035 info->tty->alt_speed = 57600;
2036 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
2037 info->tty->alt_speed = 115200;
2038 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
2039 info->tty->alt_speed = 230400;
2040 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
2041 info->tty->alt_speed = 460800;
2042 #endif
2043 change_speed(info, 0);
2045 } else
2046 retval = startup(info);
2047 return retval;
2052 * get_lsr_info - get line status register info
2054 * Purpose: Let user call ioctl() to get info when the UART physically
2055 * is emptied. On bus types like RS485, the transmitter must
2056 * release the bus after transmitting. This must be done when
2057 * the transmit shift register is empty, not be done when the
2058 * transmit holding register is empty. This functionality
2059 * allows an RS485 driver to be written in user space.
2061 static int get_lsr_info(struct async_struct * info, unsigned int *value)
2063 unsigned char status;
2064 unsigned int result;
2065 unsigned long flags;
2067 save_flags(flags); cli();
2068 status = serial_in(info, UART_LSR);
2069 restore_flags(flags);
2070 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
2071 if (copy_to_user(value, &result, sizeof(int)))
2072 return -EFAULT;
2073 return 0;
2077 static int get_modem_info(struct async_struct * info, unsigned int *value)
2079 unsigned char control, status;
2080 unsigned int result;
2081 unsigned long flags;
2083 control = info->MCR;
2084 save_flags(flags); cli();
2085 status = serial_in(info, UART_MSR);
2086 restore_flags(flags);
2087 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
2088 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
2089 #ifdef TIOCM_OUT1
2090 | ((control & UART_MCR_OUT1) ? TIOCM_OUT1 : 0)
2091 | ((control & UART_MCR_OUT2) ? TIOCM_OUT2 : 0)
2092 #endif
2093 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
2094 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
2095 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
2096 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
2098 if (copy_to_user(value, &result, sizeof(int)))
2099 return -EFAULT;
2100 return 0;
2103 static int set_modem_info(struct async_struct * info, unsigned int cmd,
2104 unsigned int *value)
2106 unsigned int arg;
2107 unsigned long flags;
2109 if (copy_from_user(&arg, value, sizeof(int)))
2110 return -EFAULT;
2112 switch (cmd) {
2113 case TIOCMBIS:
2114 if (arg & TIOCM_RTS)
2115 info->MCR |= UART_MCR_RTS;
2116 if (arg & TIOCM_DTR)
2117 info->MCR |= UART_MCR_DTR;
2118 #ifdef TIOCM_OUT1
2119 if (arg & TIOCM_OUT1)
2120 info->MCR |= UART_MCR_OUT1;
2121 if (arg & TIOCM_OUT2)
2122 info->MCR |= UART_MCR_OUT2;
2123 #endif
2124 break;
2125 case TIOCMBIC:
2126 if (arg & TIOCM_RTS)
2127 info->MCR &= ~UART_MCR_RTS;
2128 if (arg & TIOCM_DTR)
2129 info->MCR &= ~UART_MCR_DTR;
2130 #ifdef TIOCM_OUT1
2131 if (arg & TIOCM_OUT1)
2132 info->MCR &= ~UART_MCR_OUT1;
2133 if (arg & TIOCM_OUT2)
2134 info->MCR &= ~UART_MCR_OUT2;
2135 #endif
2136 break;
2137 case TIOCMSET:
2138 info->MCR = ((info->MCR & ~(UART_MCR_RTS |
2139 #ifdef TIOCM_OUT1
2140 UART_MCR_OUT1 |
2141 UART_MCR_OUT2 |
2142 #endif
2143 UART_MCR_DTR))
2144 | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
2145 #ifdef TIOCM_OUT1
2146 | ((arg & TIOCM_OUT1) ? UART_MCR_OUT1 : 0)
2147 | ((arg & TIOCM_OUT2) ? UART_MCR_OUT2 : 0)
2148 #endif
2149 | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
2150 break;
2151 default:
2152 return -EINVAL;
2154 save_flags(flags); cli();
2155 info->MCR |= ALPHA_KLUDGE_MCR; /* Don't ask */
2156 serial_out(info, UART_MCR, info->MCR);
2157 restore_flags(flags);
2158 return 0;
2161 static int do_autoconfig(struct async_struct * info)
2163 int retval;
2165 if (!serial_isroot())
2166 return -EPERM;
2168 if (info->state->count > 1)
2169 return -EBUSY;
2171 shutdown(info);
2173 autoconfig(info->state);
2174 if ((info->state->flags & ASYNC_AUTO_IRQ) &&
2175 (info->state->port != 0) &&
2176 (info->state->type != PORT_UNKNOWN))
2177 info->state->irq = detect_uart_irq(info->state);
2179 retval = startup(info);
2180 if (retval)
2181 return retval;
2182 return 0;
2186 * rs_break() --- routine which turns the break handling on or off
2188 #if (LINUX_VERSION_CODE < 131394) /* Linux 2.1.66 */
2189 static void send_break( struct async_struct * info, int duration)
2191 if (!CONFIGURED_SERIAL_PORT(info))
2192 return;
2193 current->state = TASK_INTERRUPTIBLE;
2194 current->timeout = jiffies + duration;
2195 cli();
2196 info->LCR |= UART_LCR_SBC;
2197 serial_out(info, UART_LCR, info->LCR);
2198 schedule();
2199 info->LCR &= ~UART_LCR_SBC;
2200 serial_out(info, UART_LCR, info->LCR);
2201 sti();
2203 #else
2204 static void rs_break(struct tty_struct *tty, int break_state)
2206 struct async_struct * info = (struct async_struct *)tty->driver_data;
2207 unsigned long flags;
2209 if (serial_paranoia_check(info, tty->device, "rs_break"))
2210 return;
2212 if (!CONFIGURED_SERIAL_PORT(info))
2213 return;
2214 save_flags(flags); cli();
2215 if (break_state == -1)
2216 info->LCR |= UART_LCR_SBC;
2217 else
2218 info->LCR &= ~UART_LCR_SBC;
2219 serial_out(info, UART_LCR, info->LCR);
2220 restore_flags(flags);
2222 #endif
2224 #ifdef CONFIG_SERIAL_MULTIPORT
2225 static int get_multiport_struct(struct async_struct * info,
2226 struct serial_multiport_struct *retinfo)
2228 struct serial_multiport_struct ret;
2229 struct rs_multiport_struct *multi;
2231 multi = &rs_multiport[info->state->irq];
2233 ret.port_monitor = multi->port_monitor;
2235 ret.port1 = multi->port1;
2236 ret.mask1 = multi->mask1;
2237 ret.match1 = multi->match1;
2239 ret.port2 = multi->port2;
2240 ret.mask2 = multi->mask2;
2241 ret.match2 = multi->match2;
2243 ret.port3 = multi->port3;
2244 ret.mask3 = multi->mask3;
2245 ret.match3 = multi->match3;
2247 ret.port4 = multi->port4;
2248 ret.mask4 = multi->mask4;
2249 ret.match4 = multi->match4;
2251 ret.irq = info->state->irq;
2253 if (copy_to_user(retinfo,&ret,sizeof(*retinfo)))
2254 return -EFAULT;
2255 return 0;
2258 static int set_multiport_struct(struct async_struct * info,
2259 struct serial_multiport_struct *in_multi)
2261 struct serial_multiport_struct new_multi;
2262 struct rs_multiport_struct *multi;
2263 struct serial_state *state;
2264 int was_multi, now_multi;
2265 int retval;
2266 void (*handler)(int, void *, struct pt_regs *);
2268 if (!serial_isroot())
2269 return -EPERM;
2270 state = info->state;
2272 if (copy_from_user(&new_multi, in_multi,
2273 sizeof(struct serial_multiport_struct)))
2274 return -EFAULT;
2276 if (new_multi.irq != state->irq || state->irq == 0 ||
2277 !IRQ_ports[state->irq])
2278 return -EINVAL;
2280 multi = &rs_multiport[state->irq];
2281 was_multi = (multi->port1 != 0);
2283 multi->port_monitor = new_multi.port_monitor;
2285 if (multi->port1)
2286 release_region(multi->port1,1);
2287 multi->port1 = new_multi.port1;
2288 multi->mask1 = new_multi.mask1;
2289 multi->match1 = new_multi.match1;
2290 if (multi->port1)
2291 request_region(multi->port1,1,"serial(multiport1)");
2293 if (multi->port2)
2294 release_region(multi->port2,1);
2295 multi->port2 = new_multi.port2;
2296 multi->mask2 = new_multi.mask2;
2297 multi->match2 = new_multi.match2;
2298 if (multi->port2)
2299 request_region(multi->port2,1,"serial(multiport2)");
2301 if (multi->port3)
2302 release_region(multi->port3,1);
2303 multi->port3 = new_multi.port3;
2304 multi->mask3 = new_multi.mask3;
2305 multi->match3 = new_multi.match3;
2306 if (multi->port3)
2307 request_region(multi->port3,1,"serial(multiport3)");
2309 if (multi->port4)
2310 release_region(multi->port4,1);
2311 multi->port4 = new_multi.port4;
2312 multi->mask4 = new_multi.mask4;
2313 multi->match4 = new_multi.match4;
2314 if (multi->port4)
2315 request_region(multi->port4,1,"serial(multiport4)");
2317 now_multi = (multi->port1 != 0);
2319 if (IRQ_ports[state->irq]->next_port &&
2320 (was_multi != now_multi)) {
2321 free_irq(state->irq, NULL);
2322 if (now_multi)
2323 handler = rs_interrupt_multi;
2324 else
2325 handler = rs_interrupt;
2327 retval = request_irq(state->irq, handler, IRQ_T(state),
2328 "serial", NULL);
2329 if (retval) {
2330 printk("Couldn't reallocate serial interrupt "
2331 "driver!!\n");
2334 return 0;
2336 #endif
2338 static int rs_ioctl(struct tty_struct *tty, struct file * file,
2339 unsigned int cmd, unsigned long arg)
2341 struct async_struct * info = (struct async_struct *)tty->driver_data;
2342 struct async_icount cprev, cnow; /* kernel counter temps */
2343 struct serial_icounter_struct icount;
2344 unsigned long flags;
2345 #if (LINUX_VERSION_CODE < 131394) /* Linux 2.1.66 */
2346 int retval, tmp;
2347 #endif
2349 if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
2350 return -ENODEV;
2352 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2353 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
2354 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
2355 if (tty->flags & (1 << TTY_IO_ERROR))
2356 return -EIO;
2359 switch (cmd) {
2360 #if (LINUX_VERSION_CODE < 131394) /* Linux 2.1.66 */
2361 case TCSBRK: /* SVID version: non-zero arg --> no break */
2362 retval = tty_check_change(tty);
2363 if (retval)
2364 return retval;
2365 tty_wait_until_sent(tty, 0);
2366 if (signal_pending(current))
2367 return -EINTR;
2368 if (!arg) {
2369 send_break(info, HZ/4); /* 1/4 second */
2370 if (signal_pending(current))
2371 return -EINTR;
2373 return 0;
2374 case TCSBRKP: /* support for POSIX tcsendbreak() */
2375 retval = tty_check_change(tty);
2376 if (retval)
2377 return retval;
2378 tty_wait_until_sent(tty, 0);
2379 if (signal_pending(current))
2380 return -EINTR;
2381 send_break(info, arg ? arg*(HZ/10) : HZ/4);
2382 if (signal_pending(current))
2383 return -EINTR;
2384 return 0;
2385 case TIOCGSOFTCAR:
2386 tmp = C_CLOCAL(tty) ? 1 : 0;
2387 if (copy_to_user((void *)arg, &tmp, sizeof(int)))
2388 return -EFAULT;
2389 return 0;
2390 case TIOCSSOFTCAR:
2391 if (copy_from_user(&tmp, (void *)arg, sizeof(int)))
2392 return -EFAULT;
2394 tty->termios->c_cflag =
2395 ((tty->termios->c_cflag & ~CLOCAL) |
2396 (tmp ? CLOCAL : 0));
2397 return 0;
2398 #endif
2399 case TIOCMGET:
2400 return get_modem_info(info, (unsigned int *) arg);
2401 case TIOCMBIS:
2402 case TIOCMBIC:
2403 case TIOCMSET:
2404 return set_modem_info(info, cmd, (unsigned int *) arg);
2405 case TIOCGSERIAL:
2406 return get_serial_info(info,
2407 (struct serial_struct *) arg);
2408 case TIOCSSERIAL:
2409 return set_serial_info(info,
2410 (struct serial_struct *) arg);
2411 case TIOCSERCONFIG:
2412 return do_autoconfig(info);
2414 case TIOCSERGETLSR: /* Get line status register */
2415 return get_lsr_info(info, (unsigned int *) arg);
2417 case TIOCSERGSTRUCT:
2418 if (copy_to_user((struct async_struct *) arg,
2419 info, sizeof(struct async_struct)))
2420 return -EFAULT;
2421 return 0;
2423 #ifdef CONFIG_SERIAL_MULTIPORT
2424 case TIOCSERGETMULTI:
2425 return get_multiport_struct(info,
2426 (struct serial_multiport_struct *) arg);
2427 case TIOCSERSETMULTI:
2428 return set_multiport_struct(info,
2429 (struct serial_multiport_struct *) arg);
2430 #endif
2433 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
2434 * - mask passed in arg for lines of interest
2435 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
2436 * Caller should use TIOCGICOUNT to see which one it was
2438 case TIOCMIWAIT:
2439 save_flags(flags); cli();
2440 /* note the counters on entry */
2441 cprev = info->state->icount;
2442 restore_flags(flags);
2443 while (1) {
2444 interruptible_sleep_on(&info->delta_msr_wait);
2445 /* see if a signal did it */
2446 if (signal_pending(current))
2447 return -ERESTARTSYS;
2448 save_flags(flags); cli();
2449 cnow = info->state->icount; /* atomic copy */
2450 restore_flags(flags);
2451 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2452 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2453 return -EIO; /* no change => error */
2454 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2455 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2456 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2457 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
2458 return 0;
2460 cprev = cnow;
2462 /* NOTREACHED */
2465 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
2466 * Return: write counters to the user passed counter struct
2467 * NB: both 1->0 and 0->1 transitions are counted except for
2468 * RI where only 0->1 is counted.
2470 case TIOCGICOUNT:
2471 save_flags(flags); cli();
2472 cnow = info->state->icount;
2473 restore_flags(flags);
2474 icount.cts = cnow.cts;
2475 icount.dsr = cnow.dsr;
2476 icount.rng = cnow.rng;
2477 icount.dcd = cnow.dcd;
2478 icount.rx = cnow.rx;
2479 icount.tx = cnow.tx;
2480 icount.frame = cnow.frame;
2481 icount.overrun = cnow.overrun;
2482 icount.parity = cnow.parity;
2483 icount.brk = cnow.brk;
2484 icount.buf_overrun = cnow.buf_overrun;
2486 if (copy_to_user((void *)arg, &icount, sizeof(icount)))
2487 return -EFAULT;
2488 return 0;
2489 case TIOCSERGWILD:
2490 case TIOCSERSWILD:
2491 /* "setserial -W" is called in Debian boot */
2492 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
2493 return 0;
2495 default:
2496 return -ENOIOCTLCMD;
2498 return 0;
2501 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
2503 struct async_struct *info = (struct async_struct *)tty->driver_data;
2504 unsigned long flags;
2505 unsigned int cflag = tty->termios->c_cflag;
2507 if ( (cflag == old_termios->c_cflag)
2508 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
2509 == RELEVANT_IFLAG(old_termios->c_iflag)))
2510 return;
2512 change_speed(info, old_termios);
2514 /* Handle transition to B0 status */
2515 if ((old_termios->c_cflag & CBAUD) &&
2516 !(cflag & CBAUD)) {
2517 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
2518 save_flags(flags); cli();
2519 serial_out(info, UART_MCR, info->MCR);
2520 restore_flags(flags);
2523 /* Handle transition away from B0 status */
2524 if (!(old_termios->c_cflag & CBAUD) &&
2525 (cflag & CBAUD)) {
2526 info->MCR |= UART_MCR_DTR;
2527 if (!(tty->termios->c_cflag & CRTSCTS) ||
2528 !test_bit(TTY_THROTTLED, &tty->flags)) {
2529 info->MCR |= UART_MCR_RTS;
2531 save_flags(flags); cli();
2532 serial_out(info, UART_MCR, info->MCR);
2533 restore_flags(flags);
2536 /* Handle turning off CRTSCTS */
2537 if ((old_termios->c_cflag & CRTSCTS) &&
2538 !(tty->termios->c_cflag & CRTSCTS)) {
2539 tty->hw_stopped = 0;
2540 rs_start(tty);
2543 #if 0
2545 * No need to wake up processes in open wait, since they
2546 * sample the CLOCAL flag once, and don't recheck it.
2547 * XXX It's not clear whether the current behavior is correct
2548 * or not. Hence, this may change.....
2550 if (!(old_termios->c_cflag & CLOCAL) &&
2551 (tty->termios->c_cflag & CLOCAL))
2552 wake_up_interruptible(&info->open_wait);
2553 #endif
2557 * ------------------------------------------------------------
2558 * rs_close()
2560 * This routine is called when the serial port gets closed. First, we
2561 * wait for the last remaining data to be sent. Then, we unlink its
2562 * async structure from the interrupt chain if necessary, and we free
2563 * that IRQ if nothing is left in the chain.
2564 * ------------------------------------------------------------
2566 static void rs_close(struct tty_struct *tty, struct file * filp)
2568 struct async_struct * info = (struct async_struct *)tty->driver_data;
2569 struct serial_state *state;
2570 unsigned long flags;
2572 if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
2573 return;
2575 state = info->state;
2577 save_flags(flags); cli();
2579 if (tty_hung_up_p(filp)) {
2580 DBG_CNT("before DEC-hung");
2581 MOD_DEC_USE_COUNT;
2582 restore_flags(flags);
2583 return;
2586 #ifdef SERIAL_DEBUG_OPEN
2587 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
2588 #endif
2589 if ((tty->count == 1) && (state->count != 1)) {
2591 * Uh, oh. tty->count is 1, which means that the tty
2592 * structure will be freed. state->count should always
2593 * be one in these conditions. If it's greater than
2594 * one, we've got real problems, since it means the
2595 * serial port won't be shutdown.
2597 printk("rs_close: bad serial port count; tty->count is 1, "
2598 "state->count is %d\n", state->count);
2599 state->count = 1;
2601 if (--state->count < 0) {
2602 printk("rs_close: bad serial port count for ttys%d: %d\n",
2603 info->line, state->count);
2604 state->count = 0;
2606 if (state->count) {
2607 DBG_CNT("before DEC-2");
2608 MOD_DEC_USE_COUNT;
2609 restore_flags(flags);
2610 return;
2612 info->flags |= ASYNC_CLOSING;
2614 * Save the termios structure, since this port may have
2615 * separate termios for callout and dialin.
2617 if (info->flags & ASYNC_NORMAL_ACTIVE)
2618 info->state->normal_termios = *tty->termios;
2619 if (info->flags & ASYNC_CALLOUT_ACTIVE)
2620 info->state->callout_termios = *tty->termios;
2622 * Now we wait for the transmit buffer to clear; and we notify
2623 * the line discipline to only process XON/XOFF characters.
2625 tty->closing = 1;
2626 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
2627 tty_wait_until_sent(tty, info->closing_wait);
2629 * At this point we stop accepting input. To do this, we
2630 * disable the receive line status interrupts, and tell the
2631 * interrupt driver to stop checking the data ready bit in the
2632 * line status register.
2634 info->IER &= ~UART_IER_RLSI;
2635 info->read_status_mask &= ~UART_LSR_DR;
2636 if (info->flags & ASYNC_INITIALIZED) {
2637 serial_out(info, UART_IER, info->IER);
2639 * Before we drop DTR, make sure the UART transmitter
2640 * has completely drained; this is especially
2641 * important if there is a transmit FIFO!
2643 rs_wait_until_sent(tty, info->timeout);
2645 shutdown(info);
2646 if (tty->driver.flush_buffer)
2647 tty->driver.flush_buffer(tty);
2648 if (tty->ldisc.flush_buffer)
2649 tty->ldisc.flush_buffer(tty);
2650 tty->closing = 0;
2651 info->event = 0;
2652 info->tty = 0;
2653 if (info->blocked_open) {
2654 if (info->close_delay) {
2655 current->state = TASK_INTERRUPTIBLE;
2656 schedule_timeout(info->close_delay);
2658 wake_up_interruptible(&info->open_wait);
2660 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
2661 ASYNC_CLOSING);
2662 wake_up_interruptible(&info->close_wait);
2663 MOD_DEC_USE_COUNT;
2664 restore_flags(flags);
2668 * rs_wait_until_sent() --- wait until the transmitter is empty
2670 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
2672 struct async_struct * info = (struct async_struct *)tty->driver_data;
2673 unsigned long orig_jiffies, char_time;
2674 int lsr;
2676 if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
2677 return;
2679 if (info->state->type == PORT_UNKNOWN)
2680 return;
2682 if (info->xmit_fifo_size == 0)
2683 return; /* Just in case.... */
2685 orig_jiffies = jiffies;
2687 * Set the check interval to be 1/5 of the estimated time to
2688 * send a single character, and make it at least 1. The check
2689 * interval should also be less than the timeout.
2691 * Note: we have to use pretty tight timings here to satisfy
2692 * the NIST-PCTS.
2694 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
2695 char_time = char_time / 5;
2696 if (char_time == 0)
2697 char_time = 1;
2698 if (timeout)
2699 char_time = MIN(char_time, timeout);
2701 * If the transmitter hasn't cleared in twice the approximate
2702 * amount of time to send the entire FIFO, it probably won't
2703 * ever clear. This assumes the UART isn't doing flow
2704 * control, which is currently the case. Hence, if it ever
2705 * takes longer than info->timeout, this is probably due to a
2706 * UART bug of some kind. So, we clamp the timeout parameter at
2707 * 2*info->timeout.
2709 if (!timeout || timeout > 2*info->timeout)
2710 timeout = 2*info->timeout;
2711 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2712 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
2713 printk("jiff=%lu...", jiffies);
2714 #endif
2715 while (!((lsr = serial_inp(info, UART_LSR)) & UART_LSR_TEMT)) {
2716 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2717 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2718 #endif
2719 current->state = TASK_INTERRUPTIBLE;
2720 schedule_timeout(char_time);
2721 if (signal_pending(current))
2722 break;
2723 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2724 break;
2726 current->state = TASK_RUNNING;
2727 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2728 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2729 #endif
2733 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
2735 static void rs_hangup(struct tty_struct *tty)
2737 struct async_struct * info = (struct async_struct *)tty->driver_data;
2738 struct serial_state *state = info->state;
2740 if (serial_paranoia_check(info, tty->device, "rs_hangup"))
2741 return;
2743 state = info->state;
2745 rs_flush_buffer(tty);
2746 shutdown(info);
2747 info->event = 0;
2748 state->count = 0;
2749 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
2750 info->tty = 0;
2751 wake_up_interruptible(&info->open_wait);
2755 * ------------------------------------------------------------
2756 * rs_open() and friends
2757 * ------------------------------------------------------------
2759 static int block_til_ready(struct tty_struct *tty, struct file * filp,
2760 struct async_struct *info)
2762 #ifdef DECLARE_WAITQUEUE
2763 DECLARE_WAITQUEUE(wait, current);
2764 #else
2765 struct wait_queue wait = { current, NULL };
2766 #endif
2767 struct serial_state *state = info->state;
2768 int retval;
2769 int do_clocal = 0, extra_count = 0;
2770 unsigned long flags;
2773 * If the device is in the middle of being closed, then block
2774 * until it's done, and then try again.
2776 if (tty_hung_up_p(filp) ||
2777 (info->flags & ASYNC_CLOSING)) {
2778 if (info->flags & ASYNC_CLOSING)
2779 interruptible_sleep_on(&info->close_wait);
2780 #ifdef SERIAL_DO_RESTART
2781 return ((info->flags & ASYNC_HUP_NOTIFY) ?
2782 -EAGAIN : -ERESTARTSYS);
2783 #else
2784 return -EAGAIN;
2785 #endif
2789 * If this is a callout device, then just make sure the normal
2790 * device isn't being used.
2792 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
2793 if (info->flags & ASYNC_NORMAL_ACTIVE)
2794 return -EBUSY;
2795 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2796 (info->flags & ASYNC_SESSION_LOCKOUT) &&
2797 (info->session != current->session))
2798 return -EBUSY;
2799 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2800 (info->flags & ASYNC_PGRP_LOCKOUT) &&
2801 (info->pgrp != current->pgrp))
2802 return -EBUSY;
2803 info->flags |= ASYNC_CALLOUT_ACTIVE;
2804 return 0;
2808 * If non-blocking mode is set, or the port is not enabled,
2809 * then make the check up front and then exit.
2811 if ((filp->f_flags & O_NONBLOCK) ||
2812 (tty->flags & (1 << TTY_IO_ERROR))) {
2813 if (info->flags & ASYNC_CALLOUT_ACTIVE)
2814 return -EBUSY;
2815 info->flags |= ASYNC_NORMAL_ACTIVE;
2816 return 0;
2819 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
2820 if (state->normal_termios.c_cflag & CLOCAL)
2821 do_clocal = 1;
2822 } else {
2823 if (tty->termios->c_cflag & CLOCAL)
2824 do_clocal = 1;
2828 * Block waiting for the carrier detect and the line to become
2829 * free (i.e., not in use by the callout). While we are in
2830 * this loop, state->count is dropped by one, so that
2831 * rs_close() knows when to free things. We restore it upon
2832 * exit, either normal or abnormal.
2834 retval = 0;
2835 add_wait_queue(&info->open_wait, &wait);
2836 #ifdef SERIAL_DEBUG_OPEN
2837 printk("block_til_ready before block: ttys%d, count = %d\n",
2838 state->line, state->count);
2839 #endif
2840 save_flags(flags); cli();
2841 if (!tty_hung_up_p(filp)) {
2842 extra_count = 1;
2843 state->count--;
2845 restore_flags(flags);
2846 info->blocked_open++;
2847 while (1) {
2848 save_flags(flags); cli();
2849 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2850 (tty->termios->c_cflag & CBAUD))
2851 serial_out(info, UART_MCR,
2852 serial_inp(info, UART_MCR) |
2853 (UART_MCR_DTR | UART_MCR_RTS));
2854 restore_flags(flags);
2855 set_current_state(TASK_INTERRUPTIBLE);
2856 if (tty_hung_up_p(filp) ||
2857 !(info->flags & ASYNC_INITIALIZED)) {
2858 #ifdef SERIAL_DO_RESTART
2859 if (info->flags & ASYNC_HUP_NOTIFY)
2860 retval = -EAGAIN;
2861 else
2862 retval = -ERESTARTSYS;
2863 #else
2864 retval = -EAGAIN;
2865 #endif
2866 break;
2868 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2869 !(info->flags & ASYNC_CLOSING) &&
2870 (do_clocal || (serial_in(info, UART_MSR) &
2871 UART_MSR_DCD)))
2872 break;
2873 if (signal_pending(current)) {
2874 retval = -ERESTARTSYS;
2875 break;
2877 #ifdef SERIAL_DEBUG_OPEN
2878 printk("block_til_ready blocking: ttys%d, count = %d\n",
2879 info->line, state->count);
2880 #endif
2881 schedule();
2883 current->state = TASK_RUNNING;
2884 remove_wait_queue(&info->open_wait, &wait);
2885 if (extra_count)
2886 state->count++;
2887 info->blocked_open--;
2888 #ifdef SERIAL_DEBUG_OPEN
2889 printk("block_til_ready after blocking: ttys%d, count = %d\n",
2890 info->line, state->count);
2891 #endif
2892 if (retval)
2893 return retval;
2894 info->flags |= ASYNC_NORMAL_ACTIVE;
2895 return 0;
2898 static int get_async_struct(int line, struct async_struct **ret_info)
2900 struct async_struct *info;
2901 struct serial_state *sstate;
2903 sstate = rs_table + line;
2904 sstate->count++;
2905 if (sstate->info) {
2906 *ret_info = sstate->info;
2907 return 0;
2909 info = kmalloc(sizeof(struct async_struct), GFP_KERNEL);
2910 if (!info) {
2911 sstate->count--;
2912 return -ENOMEM;
2914 memset(info, 0, sizeof(struct async_struct));
2915 #ifdef DECLARE_WAITQUEUE
2916 init_waitqueue_head(&info->open_wait);
2917 init_waitqueue_head(&info->close_wait);
2918 init_waitqueue_head(&info->delta_msr_wait);
2919 #endif
2920 info->magic = SERIAL_MAGIC;
2921 info->port = sstate->port;
2922 info->flags = sstate->flags;
2923 #ifdef CONFIG_SERIAL_PCI_MEMMAPPED
2924 info->iomem_base = sstate->iomem_base;
2925 info->iomem_reg_shift = sstate->iomem_reg_shift;
2926 #endif
2927 info->xmit_fifo_size = sstate->xmit_fifo_size;
2928 info->line = line;
2929 info->tqueue.routine = do_softint;
2930 info->tqueue.data = info;
2931 info->state = sstate;
2932 if (sstate->info) {
2933 kfree_s(info, sizeof(struct async_struct));
2934 *ret_info = sstate->info;
2935 return 0;
2937 *ret_info = sstate->info = info;
2938 return 0;
2942 * This routine is called whenever a serial port is opened. It
2943 * enables interrupts for a serial port, linking in its async structure into
2944 * the IRQ chain. It also performs the serial-specific
2945 * initialization for the tty structure.
2947 static int rs_open(struct tty_struct *tty, struct file * filp)
2949 struct async_struct *info;
2950 int retval, line;
2951 unsigned long page;
2953 MOD_INC_USE_COUNT;
2954 line = MINOR(tty->device) - tty->driver.minor_start;
2955 if ((line < 0) || (line >= NR_PORTS)) {
2956 MOD_DEC_USE_COUNT;
2957 return -ENODEV;
2959 retval = get_async_struct(line, &info);
2960 if (retval) {
2961 MOD_DEC_USE_COUNT;
2962 return retval;
2964 tty->driver_data = info;
2965 info->tty = tty;
2966 if (serial_paranoia_check(info, tty->device, "rs_open"))
2967 return -ENODEV;
2969 #ifdef SERIAL_DEBUG_OPEN
2970 printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
2971 info->state->count);
2972 #endif
2973 #if (LINUX_VERSION_CODE > 0x20100)
2974 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2975 #endif
2977 if (!tmp_buf) {
2978 page = get_zeroed_page(GFP_KERNEL);
2979 if (!page) {
2980 return -ENOMEM;
2982 if (tmp_buf)
2983 free_page(page);
2984 else
2985 tmp_buf = (unsigned char *) page;
2989 * If the port is the middle of closing, bail out now
2991 if (tty_hung_up_p(filp) ||
2992 (info->flags & ASYNC_CLOSING)) {
2993 if (info->flags & ASYNC_CLOSING)
2994 interruptible_sleep_on(&info->close_wait);
2995 #ifdef SERIAL_DO_RESTART
2996 return ((info->flags & ASYNC_HUP_NOTIFY) ?
2997 -EAGAIN : -ERESTARTSYS);
2998 #else
2999 return -EAGAIN;
3000 #endif
3004 * Start up serial port
3006 retval = startup(info);
3007 if (retval) {
3008 return retval;
3011 retval = block_til_ready(tty, filp, info);
3012 if (retval) {
3013 #ifdef SERIAL_DEBUG_OPEN
3014 printk("rs_open returning after block_til_ready with %d\n",
3015 retval);
3016 #endif
3017 return retval;
3020 if ((info->state->count == 1) &&
3021 (info->flags & ASYNC_SPLIT_TERMIOS)) {
3022 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
3023 *tty->termios = info->state->normal_termios;
3024 else
3025 *tty->termios = info->state->callout_termios;
3026 change_speed(info, 0);
3028 #ifdef CONFIG_SERIAL_CONSOLE
3029 if (sercons.cflag && sercons.index == line) {
3030 tty->termios->c_cflag = sercons.cflag;
3031 sercons.cflag = 0;
3032 change_speed(info, 0);
3034 #endif
3035 info->session = current->session;
3036 info->pgrp = current->pgrp;
3038 #ifdef SERIAL_DEBUG_OPEN
3039 printk("rs_open ttys%d successful...", info->line);
3040 #endif
3041 return 0;
3045 * /proc fs routines....
3048 static inline int line_info(char *buf, struct serial_state *state)
3050 struct async_struct *info = state->info, scr_info;
3051 char stat_buf[30], control, status;
3052 int ret;
3053 unsigned long flags;
3055 ret = sprintf(buf, "%d: uart:%s port:%X irq:%d",
3056 state->line, uart_config[state->type].name,
3057 state->port, state->irq);
3059 if (!state->port || (state->type == PORT_UNKNOWN)) {
3060 ret += sprintf(buf+ret, "\n");
3061 return ret;
3065 * Figure out the current RS-232 lines
3067 if (!info) {
3068 info = &scr_info; /* This is just for serial_{in,out} */
3070 info->magic = SERIAL_MAGIC;
3071 info->port = state->port;
3072 info->flags = state->flags;
3073 info->quot = 0;
3074 info->tty = 0;
3076 save_flags(flags); cli();
3077 status = serial_in(info, UART_MSR);
3078 control = info ? info->MCR : serial_in(info, UART_MCR);
3079 restore_flags(flags);
3081 stat_buf[0] = 0;
3082 stat_buf[1] = 0;
3083 if (control & UART_MCR_RTS)
3084 strcat(stat_buf, "|RTS");
3085 if (status & UART_MSR_CTS)
3086 strcat(stat_buf, "|CTS");
3087 if (control & UART_MCR_DTR)
3088 strcat(stat_buf, "|DTR");
3089 if (status & UART_MSR_DSR)
3090 strcat(stat_buf, "|DSR");
3091 if (status & UART_MSR_DCD)
3092 strcat(stat_buf, "|CD");
3093 if (status & UART_MSR_RI)
3094 strcat(stat_buf, "|RI");
3096 if (info->quot) {
3097 ret += sprintf(buf+ret, " baud:%d",
3098 state->baud_base / info->quot);
3101 ret += sprintf(buf+ret, " tx:%d rx:%d",
3102 state->icount.tx, state->icount.rx);
3104 if (state->icount.frame)
3105 ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
3107 if (state->icount.parity)
3108 ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
3110 if (state->icount.brk)
3111 ret += sprintf(buf+ret, " brk:%d", state->icount.brk);
3113 if (state->icount.overrun)
3114 ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
3117 * Last thing is the RS-232 status lines
3119 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
3120 return ret;
3123 int rs_read_proc(char *page, char **start, off_t off, int count,
3124 int *eof, void *data)
3126 int i, len = 0, l;
3127 off_t begin = 0;
3129 len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
3130 for (i = 0; i < NR_PORTS && len < 4000; i++) {
3131 l = line_info(page + len, &rs_table[i]);
3132 len += l;
3133 if (len+begin > off+count)
3134 goto done;
3135 if (len+begin < off) {
3136 begin += len;
3137 len = 0;
3140 *eof = 1;
3141 done:
3142 if (off >= len+begin)
3143 return 0;
3144 *start = page + (begin-off);
3145 return ((count < begin+len-off) ? count : begin+len-off);
3149 * ---------------------------------------------------------------------
3150 * rs_init() and friends
3152 * rs_init() is called at boot-time to initialize the serial driver.
3153 * ---------------------------------------------------------------------
3157 * This routine prints out the appropriate serial driver version
3158 * number, and identifies which options were configured into this
3159 * driver.
3161 static _INLINE_ void show_serial_version(void)
3163 printk(KERN_INFO "%s version %s with", serial_name, serial_version);
3164 #ifdef CONFIG_HUB6
3165 printk(" HUB-6");
3166 #define SERIAL_OPT
3167 #endif
3168 #ifdef CONFIG_SERIAL_MANY_PORTS
3169 printk(" MANY_PORTS");
3170 #define SERIAL_OPT
3171 #endif
3172 #ifdef CONFIG_SERIAL_MULTIPORT
3173 printk(" MULTIPORT");
3174 #define SERIAL_OPT
3175 #endif
3176 #ifdef CONFIG_SERIAL_SHARE_IRQ
3177 printk(" SHARE_IRQ");
3178 #define SERIAL_OPT
3179 #endif
3180 #ifdef CONFIG_SERIAL_DETECT_IRQ
3181 printk(" DETECT_IRQ");
3182 #define SERIAL_OPT
3183 #endif
3184 #ifdef ENABLE_SERIAL_PCI
3185 printk(" SERIAL_PCI");
3186 #ifdef CONFIG_SERIAL_PCI_MEMMAPPED
3187 printk(" PCI_IOMEM");
3188 #endif
3189 #define SERIAL_OPT
3190 #endif
3191 #ifdef SERIAL_OPT
3192 printk(" enabled\n");
3193 #else
3194 printk(" no serial options enabled\n");
3195 #endif
3196 #undef SERIAL_OPT
3200 * This routine detect the IRQ of a serial port by clearing OUT2 when
3201 * no UART interrupt are requested (IER = 0) (*GPL*). This seems to work at
3202 * each time, as long as no other device permanently request the IRQ.
3203 * If no IRQ is detected, or multiple IRQ appear, this function returns 0.
3204 * The variable "state" and the field "state->port" should not be null.
3206 static unsigned detect_uart_irq (struct serial_state * state)
3208 int irq;
3209 unsigned long irqs;
3210 unsigned char save_mcr, save_ier;
3211 struct async_struct scr_info; /* serial_{in,out} because HUB6 */
3213 #ifdef CONFIG_SERIAL_MANY_PORTS
3214 unsigned char save_ICP=0; /* no warning */
3215 unsigned short ICP=0;
3217 if (state->flags & ASYNC_FOURPORT) {
3218 ICP = (state->port & 0xFE0) | 0x01F;
3219 save_ICP = inb_p(ICP);
3220 outb_p(0x80, ICP);
3221 (void) inb_p(ICP);
3223 #endif
3224 scr_info.magic = SERIAL_MAGIC;
3225 scr_info.port = state->port;
3226 scr_info.flags = state->flags;
3227 #ifdef CONFIG_HUB6
3228 scr_info.hub6 = state->hub6;
3229 #endif
3231 /* forget possible initially masked and pending IRQ */
3232 probe_irq_off(probe_irq_on());
3233 save_mcr = serial_inp(&scr_info, UART_MCR);
3234 save_ier = serial_inp(&scr_info, UART_IER);
3235 serial_outp(&scr_info, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
3237 irqs = probe_irq_on();
3238 serial_outp(&scr_info, UART_MCR, 0);
3239 udelay (10);
3240 if (state->flags & ASYNC_FOURPORT) {
3241 serial_outp(&scr_info, UART_MCR,
3242 UART_MCR_DTR | UART_MCR_RTS);
3243 } else {
3244 serial_outp(&scr_info, UART_MCR,
3245 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
3247 serial_outp(&scr_info, UART_IER, 0x0f); /* enable all intrs */
3248 (void)serial_inp(&scr_info, UART_LSR);
3249 (void)serial_inp(&scr_info, UART_RX);
3250 (void)serial_inp(&scr_info, UART_IIR);
3251 (void)serial_inp(&scr_info, UART_MSR);
3252 serial_outp(&scr_info, UART_TX, 0xFF);
3253 udelay (20);
3254 irq = probe_irq_off(irqs);
3256 serial_outp(&scr_info, UART_MCR, save_mcr);
3257 serial_outp(&scr_info, UART_IER, save_ier);
3258 #ifdef CONFIG_SERIAL_MANY_PORTS
3259 if (state->flags & ASYNC_FOURPORT)
3260 outb_p(save_ICP, ICP);
3261 #endif
3262 return (irq > 0)? irq : 0;
3266 * This is a quickie test to see how big the FIFO is.
3267 * It doesn't work at all the time, more's the pity.
3269 static int size_fifo(struct async_struct *info)
3271 unsigned char old_fcr, old_mcr, old_dll, old_dlm;
3272 int count;
3274 old_fcr = serial_inp(info, UART_FCR);
3275 old_mcr = serial_inp(info, UART_MCR);
3276 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO |
3277 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
3278 serial_outp(info, UART_MCR, UART_MCR_LOOP);
3279 serial_outp(info, UART_LCR, UART_LCR_DLAB);
3280 old_dll = serial_inp(info, UART_DLL);
3281 old_dlm = serial_inp(info, UART_DLM);
3282 serial_outp(info, UART_DLL, 0x01);
3283 serial_outp(info, UART_DLM, 0x00);
3284 serial_outp(info, UART_LCR, 0x03);
3285 for (count = 0; count < 256; count++)
3286 serial_outp(info, UART_TX, count);
3287 mdelay(20);
3288 for (count = 0; (serial_inp(info, UART_LSR) & UART_LSR_DR) &&
3289 (count < 256); count++)
3290 serial_inp(info, UART_RX);
3291 serial_outp(info, UART_FCR, old_fcr);
3292 serial_outp(info, UART_MCR, old_mcr);
3293 serial_outp(info, UART_LCR, UART_LCR_DLAB);
3294 serial_outp(info, UART_DLL, old_dll);
3295 serial_outp(info, UART_DLM, old_dlm);
3297 return count;
3301 * This is a helper routine to autodetect StarTech/Exar UART's. When
3302 * this function is called we know it is at least a StarTech 16650 V2,
3303 * but it might be one of several StarTech UARTs, or one of its
3304 * clones. (We treat the broken original StarTech 16650 V1 as a
3305 * 16550A, and why not? Startech doesn't seem to even acknowledge its
3306 * existence.)
3308 * What evil have men's minds wrought...
3310 static void autoconfig_startech_uarts(struct async_struct *info,
3311 struct serial_state *state,
3312 unsigned long flags)
3314 unsigned char scratch, status1, status2, old_fctr, old_emsr;
3317 * Here we check for the XR16C85x family. We do this by
3318 * checking for to see if we can replace the scratch register
3319 * with the receive FIFO count register.
3321 * XXX I don't have one of these chips, but it should also be
3322 * possible to check for them by setting DLL and DLM to 0, and
3323 * then reading back DLL and DLM. If the DLM reads back as
3324 * 0x10, then the UART is a XR16C850 and the DLL contains the
3325 * chip revision.
3327 old_fctr = serial_inp(info, UART_FCTR);
3328 serial_outp(info, UART_FCTR, old_fctr | UART_FCTR_SCR_SWAP);
3329 old_emsr = serial_inp(info, UART_EMSR);
3330 serial_outp(info, UART_EMSR, 0x00);
3331 serial_outp(info, UART_LCR, 0x00);
3332 scratch = serial_in(info, UART_SCR);
3333 serial_outp(info, UART_SCR, 0xa5);
3334 status1 = serial_in(info, UART_SCR);
3335 serial_outp(info, UART_SCR, 0x5a);
3336 status2 = serial_in(info, UART_SCR);
3337 serial_outp(info, UART_SCR, scratch);
3338 if ((status1 != 0xa5) || (status2 != 0x5a)) {
3339 serial_outp(info, UART_LCR, 0xBF);
3340 serial_outp(info, UART_FCTR, old_fctr | UART_FCTR_SCR_SWAP);
3341 serial_outp(info, UART_EMSR, old_emsr);
3342 serial_outp(info, UART_FCTR, old_fctr);
3343 state->type = PORT_16850;
3344 return;
3346 serial_outp(info, UART_IER, old_fctr);
3349 * We distinguish between the '654 and the '650 by counting
3350 * how many bytes are in the FIFO. I'm using this for now,
3351 * since that's the technique that was sent to me in the
3352 * serial driver update, but I'm not convinced this works.
3353 * I've had problems doing this in the past. -TYT
3355 if (size_fifo(info) == 64)
3356 state->type = PORT_16654;
3357 else
3358 state->type = PORT_16650V2;
3362 * This routine is called by rs_init() to initialize a specific serial
3363 * port. It determines what type of UART chip this serial port is
3364 * using: 8250, 16450, 16550, 16550A. The important question is
3365 * whether or not this UART is a 16550A or not, since this will
3366 * determine whether or not we can use its FIFO features or not.
3368 static void autoconfig(struct serial_state * state)
3370 unsigned char status1, status2, scratch, scratch2, scratch3;
3371 struct async_struct *info, scr_info;
3372 unsigned long flags;
3374 state->type = PORT_UNKNOWN;
3376 if (!CONFIGURED_SERIAL_PORT(state))
3377 return;
3379 info = &scr_info; /* This is just for serial_{in,out} */
3381 info->magic = SERIAL_MAGIC;
3382 info->state = state;
3383 info->port = state->port;
3384 info->flags = state->flags;
3385 #ifdef CONFIG_HUB6
3386 info->hub6 = state->hub6;
3387 #endif
3388 #ifdef CONFIG_SERIAL_PCI_MEMMAPPED
3389 info->iomem_base = state->iomem_base;
3390 info->iomem_reg_shift = state->iomem_reg_shift;
3391 #endif
3393 save_flags(flags); cli();
3395 if (!state->iomem_base) {
3397 * Do a simple existence test first; if we fail this,
3398 * there's no point trying anything else.
3400 * 0x80 is used as a nonsense port to prevent against
3401 * false positives due to ISA bus float. The
3402 * assumption is that 0x80 is a non-existent port;
3403 * which should be safe since include/asm/io.h also
3404 * makes this assumption.
3406 scratch = serial_inp(info, UART_IER);
3407 serial_outp(info, UART_IER, 0);
3408 outb(0xff, 0x080);
3409 scratch2 = serial_inp(info, UART_IER);
3410 serial_outp(info, UART_IER, scratch);
3411 if (scratch2) {
3412 restore_flags(flags);
3413 return; /* We failed; there's nothing here */
3418 * Check to see if a UART is really there. Certain broken
3419 * internal modems based on the Rockwell chipset fail this
3420 * test, because they apparently don't implement the loopback
3421 * test mode. So this test is skipped on the COM 1 through
3422 * COM 4 ports. This *should* be safe, since no board
3423 * manufacturer would be stupid enough to design a board
3424 * that conflicts with COM 1-4 --- we hope!
3426 if (!(state->flags & ASYNC_SKIP_TEST)) {
3427 scratch = serial_inp(info, UART_MCR);
3428 serial_outp(info, UART_MCR, UART_MCR_LOOP | scratch);
3429 serial_outp(info, UART_MCR, UART_MCR_LOOP | 0x0A);
3430 status1 = serial_inp(info, UART_MSR) & 0xF0;
3431 serial_outp(info, UART_MCR, scratch);
3432 if (status1 != 0x90) {
3433 restore_flags(flags);
3434 return;
3438 scratch2 = serial_in(info, UART_LCR);
3439 serial_outp(info, UART_LCR, 0xBF); /* set up for StarTech test */
3440 serial_outp(info, UART_EFR, 0); /* EFR is the same as FCR */
3441 serial_outp(info, UART_LCR, 0);
3442 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
3443 scratch = serial_in(info, UART_IIR) >> 6;
3444 switch (scratch) {
3445 case 0:
3446 state->type = PORT_16450;
3447 break;
3448 case 1:
3449 state->type = PORT_UNKNOWN;
3450 break;
3451 case 2:
3452 state->type = PORT_16550;
3453 break;
3454 case 3:
3455 state->type = PORT_16550A;
3456 break;
3458 if (state->type == PORT_16550A) {
3459 /* Check for Oxford Semiconductor 16C950 */
3460 scratch = serial_icr_read(info, UART_ID1);
3461 scratch2 = serial_icr_read(info, UART_ID2);
3462 scratch3 = serial_icr_read(info, UART_ID3);
3464 if (scratch == 0x16 && scratch2 == 0xC9 &&
3465 (scratch3 == 0x50 || scratch3 == 0x52 ||
3466 scratch3 == 0x54)) {
3467 state->type = PORT_16C950;
3468 state->revision = serial_icr_read(info, UART_REV);
3471 if (state->type == PORT_16550A) {
3472 /* Check for Startech UART's */
3473 serial_outp(info, UART_LCR, UART_LCR_DLAB);
3474 if (serial_in(info, UART_EFR) == 0) {
3475 state->type = PORT_16650;
3476 } else {
3477 serial_outp(info, UART_LCR, 0xBF);
3478 if (serial_in(info, UART_EFR) == 0)
3479 autoconfig_startech_uarts(info, state, flags);
3482 if (state->type == PORT_16550A) {
3483 /* Check for TI 16750 */
3484 serial_outp(info, UART_LCR, scratch2 | UART_LCR_DLAB);
3485 serial_outp(info, UART_FCR,
3486 UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
3487 scratch = serial_in(info, UART_IIR) >> 5;
3488 if (scratch == 7) {
3490 * If this is a 16750, and not a cheap UART
3491 * clone, then it should only go into 64 byte
3492 * mode if the UART_FCR7_64BYTE bit was set
3493 * while UART_LCR_DLAB was latched.
3495 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
3496 serial_outp(info, UART_LCR, 0);
3497 serial_outp(info, UART_FCR,
3498 UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
3499 scratch = serial_in(info, UART_IIR) >> 5;
3500 if (scratch == 6)
3501 state->type = PORT_16750;
3503 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
3505 serial_outp(info, UART_LCR, scratch2);
3506 if (state->type == PORT_16450) {
3507 scratch = serial_in(info, UART_SCR);
3508 serial_outp(info, UART_SCR, 0xa5);
3509 status1 = serial_in(info, UART_SCR);
3510 serial_outp(info, UART_SCR, 0x5a);
3511 status2 = serial_in(info, UART_SCR);
3512 serial_outp(info, UART_SCR, scratch);
3514 if ((status1 != 0xa5) || (status2 != 0x5a))
3515 state->type = PORT_8250;
3517 state->xmit_fifo_size = uart_config[state->type].dfl_xmit_fifo_size;
3519 if (state->type == PORT_UNKNOWN) {
3520 restore_flags(flags);
3521 return;
3524 if (info->port)
3525 request_region(info->port,8,"serial(auto)");
3528 * Reset the UART.
3530 serial_outp(info, UART_MCR, 0x00 | ALPHA_KLUDGE_MCR); /* Don't ask */
3531 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
3532 UART_FCR_CLEAR_XMIT));
3533 (void)serial_in(info, UART_RX);
3534 serial_outp(info, UART_IER, 0);
3536 restore_flags(flags);
3539 int register_serial(struct serial_struct *req);
3540 void unregister_serial(int line);
3542 #if (LINUX_VERSION_CODE > 0x20100)
3543 EXPORT_SYMBOL(register_serial);
3544 EXPORT_SYMBOL(unregister_serial);
3545 #else
3546 static struct symbol_table serial_syms = {
3547 #include <linux/symtab_begin.h>
3548 X(register_serial),
3549 X(unregister_serial),
3550 #include <linux/symtab_end.h>
3552 #endif
3554 #ifdef ENABLE_SERIAL_PCI
3556 * Some PCI serial cards using the PLX 9050 PCI interface chip require
3557 * that the card interrupt be explicitly enabled or disabled. This
3558 * seems to be mainly needed on card using the PLX which also use I/O
3559 * mapped memory.
3561 * Note that __init is a no-op if MODULE is defined; we depend on this.
3563 static void __init pci_plx9050_fn(struct pci_dev *dev,
3564 struct pci_board *board,
3565 int enable)
3567 u8 data, *p;
3569 pci_read_config_byte(dev, PCI_COMMAND, &data);
3571 if (enable)
3572 pci_write_config_byte(dev, PCI_COMMAND,
3573 data | PCI_COMMAND_MEMORY);
3575 /* enable/disable interrupts */
3576 p = ioremap(PCI_BASE_ADDRESS(dev, 0), 0x80);
3577 writel(enable ? 0x41 : 0x00, p + 0x4c);
3578 iounmap(p);
3580 if (!enable)
3581 pci_write_config_byte(dev, PCI_COMMAND,
3582 data & ~PCI_COMMAND_MEMORY);
3586 * This is the configuration table for all of the PCI serial boards
3587 * which we support.
3589 static struct pci_board pci_boards[] = {
3591 * Vendor ID, Device ID,
3592 * Subvendor ID, Subdevice ID,
3593 * Number of Ports, Base (Maximum) Baud Rate,
3594 * Offset of register holding Uart register offset
3595 * Mask to apply to above register's value
3597 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
3598 PCI_SUBVENDOR_ID_CONNECT_TECH,
3599 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_232,
3600 SPCI_FL_BASE1, 8, 1382400 },
3601 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
3602 PCI_SUBVENDOR_ID_CONNECT_TECH,
3603 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_232,
3604 SPCI_FL_BASE1, 4, 1382400 },
3605 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
3606 PCI_SUBVENDOR_ID_CONNECT_TECH,
3607 PCI_SUBDEVICE_ID_CONNECT_TECH_BH2_232,
3608 SPCI_FL_BASE1, 2, 1382400 },
3609 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960V2,
3610 PCI_SUBVENDOR_ID_CONNECT_TECH,
3611 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_232,
3612 SPCI_FL_BASE1, 8, 1382400 },
3613 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960V2,
3614 PCI_SUBVENDOR_ID_CONNECT_TECH,
3615 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_232,
3616 SPCI_FL_BASE1, 4, 1382400 },
3617 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960V2,
3618 PCI_SUBVENDOR_ID_CONNECT_TECH,
3619 PCI_SUBDEVICE_ID_CONNECT_TECH_BH2_232,
3620 SPCI_FL_BASE1, 2, 1382400 },
3621 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
3622 PCI_SUBVENDOR_ID_CONNECT_TECH,
3623 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_485,
3624 SPCI_FL_BASE1, 8, 921600 },
3625 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
3626 PCI_SUBVENDOR_ID_CONNECT_TECH,
3627 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_485_4_4,
3628 SPCI_FL_BASE1, 8, 921600 },
3629 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
3630 PCI_SUBVENDOR_ID_CONNECT_TECH,
3631 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_485,
3632 SPCI_FL_BASE1, 4, 921600 },
3633 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
3634 PCI_SUBVENDOR_ID_CONNECT_TECH,
3635 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_485_2_2,
3636 SPCI_FL_BASE1, 4, 921600 },
3637 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
3638 PCI_SUBVENDOR_ID_CONNECT_TECH,
3639 PCI_SUBDEVICE_ID_CONNECT_TECH_BH2_485,
3640 SPCI_FL_BASE1, 2, 921600 },
3641 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960V2,
3642 PCI_SUBVENDOR_ID_CONNECT_TECH,
3643 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_485,
3644 SPCI_FL_BASE1, 8, 921600 },
3645 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960V2,
3646 PCI_SUBVENDOR_ID_CONNECT_TECH,
3647 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_485_4_4,
3648 SPCI_FL_BASE1, 8, 921600 },
3649 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960V2,
3650 PCI_SUBVENDOR_ID_CONNECT_TECH,
3651 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_485,
3652 SPCI_FL_BASE1, 4, 921600 },
3653 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960V2,
3654 PCI_SUBVENDOR_ID_CONNECT_TECH,
3655 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_485_2_2,
3656 SPCI_FL_BASE1, 4, 921600 },
3657 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960V2,
3658 PCI_SUBVENDOR_ID_CONNECT_TECH,
3659 PCI_SUBDEVICE_ID_CONNECT_TECH_BH2_485,
3660 SPCI_FL_BASE1, 2, 921600 },
3661 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_U530,
3662 PCI_ANY_ID, PCI_ANY_ID,
3663 SPCI_FL_BASE2 | SPCI_FL_BASE_TABLE, 1, 115200 },
3664 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_UCOMM2,
3665 PCI_ANY_ID, PCI_ANY_ID,
3666 SPCI_FL_BASE2 | SPCI_FL_BASE_TABLE, 2, 115200 },
3667 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_UCOMM422,
3668 PCI_ANY_ID, PCI_ANY_ID,
3669 SPCI_FL_BASE2 | SPCI_FL_BASE_TABLE, 4, 115200 },
3670 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_UCOMM232,
3671 PCI_ANY_ID, PCI_ANY_ID,
3672 SPCI_FL_BASE2 | SPCI_FL_BASE_TABLE, 2, 115200 },
3673 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_COMM4,
3674 PCI_ANY_ID, PCI_ANY_ID,
3675 SPCI_FL_BASE2 | SPCI_FL_BASE_TABLE, 4, 115200 },
3676 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_COMM8,
3677 PCI_ANY_ID, PCI_ANY_ID,
3678 SPCI_FL_BASE2 | SPCI_FL_BASE_TABLE, 8, 115200 },
3679 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_GTEK_SERIAL2,
3680 PCI_ANY_ID, PCI_ANY_ID,
3681 SPCI_FL_BASE2 | SPCI_FL_BASE_TABLE, 2, 115200 },
3682 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_SPCOM200,
3683 PCI_ANY_ID, PCI_ANY_ID,
3684 SPCI_FL_BASE2 | SPCI_FL_BASE_TABLE, 2, 921600 },
3685 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
3686 PCI_SUBVENDOR_ID_KEYSPAN,
3687 PCI_SUBDEVICE_ID_KEYSPAN_SX2,
3688 SPCI_FL_BASE2 | SPCI_FL_IOMEM, 2, 921600,
3689 0x400, 7, pci_plx9050_fn },
3690 { PCI_VENDOR_ID_PANACOM, PCI_DEVICE_ID_PANACOM_QUADMODEM,
3691 PCI_ANY_ID, PCI_ANY_ID,
3692 SPCI_FL_BASE2 | SPCI_FL_IOMEM, 4, 921600,
3693 0x400, 7, pci_plx9050_fn },
3694 { PCI_VENDOR_ID_PANACOM, PCI_DEVICE_ID_PANACOM_DUALMODEM,
3695 PCI_ANY_ID, PCI_ANY_ID,
3696 SPCI_FL_BASE2 | SPCI_FL_IOMEM, 2, 921600,
3697 0x400, 7, pci_plx9050_fn },
3698 { 0, }
3702 * Query PCI space for known serial boards
3703 * If found, add them to the PCI device space in rs_table[]
3705 * Accept a maximum of eight boards
3708 static void probe_serial_pci(void)
3710 u16 subvendor, subdevice;
3711 int k, line;
3712 struct pci_dev *dev = NULL;
3713 struct pci_board *board;
3714 struct serial_struct fake_state;
3715 int uart_offset, base_baud, base_idx;
3716 unsigned long port;
3718 #ifdef SERIAL_DEBUG_PCI
3719 printk(KERN_DEBUG "Entered probe_serial_pci()\n");
3720 #endif
3722 if (!pcibios_present()) {
3723 #ifdef SERIAL_DEBUG_PCI
3724 printk(KERN_DEBUG "Leaving probe_serial_pci() (no pcibios)\n");
3725 #endif
3726 return;
3729 for(dev=pci_devices; dev; dev=dev->next) {
3730 pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID,
3731 &subvendor);
3732 pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &subdevice);
3734 for (board = pci_boards; board->vendor; board++) {
3735 if (board->vendor != (unsigned short) PCI_ANY_ID &&
3736 dev->vendor != board->vendor)
3737 continue;
3738 if (board->device != (unsigned short) PCI_ANY_ID &&
3739 dev->device != board->device)
3740 continue;
3741 if (board->subvendor != (unsigned short) PCI_ANY_ID &&
3742 subvendor != board->subvendor)
3743 continue;
3744 if (board->subdevice != (unsigned short) PCI_ANY_ID &&
3745 subdevice != board->subdevice)
3746 continue;
3747 break;
3750 if (board->vendor == 0) {
3751 #ifdef SERIAL_DEBUG_PCI
3752 printk(KERN_DEBUG
3753 "Found unknown serial board: %x:%x, %x:%x, %x\n",
3754 dev->vendor, dev->device, subvendor, subdevice,
3755 dev->class);
3756 printk(KERN_DEBUG
3757 " Addresses: %lx, %lx, %lx, %lx\n",
3758 PCI_BASE_ADDRESS(dev, 0), PCI_BASE_ADDRESS(dev, 1),
3759 PCI_BASE_ADDRESS(dev, 2), PCI_BASE_ADDRESS(dev, 3));
3760 #endif
3761 continue;
3765 * Run the initialization function, if any
3767 if (board->init_fn)
3768 (board->init_fn)(dev, board, 1);
3771 * Register the serial board in the array so we can
3772 * shutdown the board later, if necessary.
3774 serial_pci_board[serial_pci_board_idx].board = board;
3775 serial_pci_board[serial_pci_board_idx].dev = dev;
3776 serial_pci_board_idx++;
3778 base_idx = board->flags & SPCI_FL_BASE_MASK;
3779 port = PCI_BASE_ADDRESS(dev, base_idx);
3780 if (board->flags & SPCI_FL_IOMEM)
3781 port &= PCI_BASE_ADDRESS_MEM_MASK;
3782 else
3783 port &= PCI_BASE_ADDRESS_IO_MASK;
3786 * Set some defaults for the loop below, which
3787 * actually registers each serial port belonging to
3788 * the card.
3790 uart_offset = board->uart_offset;
3791 if (!uart_offset)
3792 uart_offset = 8;
3793 base_baud = board->base_baud;
3794 if (!base_baud)
3795 base_baud = BASE_BAUD;
3796 #ifndef CONFIG_SERIAL_PCI_MEMMAPPED
3797 if (board->flags & SPCI_FL_IOMEM) {
3798 #ifdef SERIAL_DEBUG_PCI
3799 printk(KERN_DEBUG
3800 "Can't support memory mapped PCI serial device\n");
3801 #endif
3802 continue;
3804 #endif
3805 memset(&fake_state, 0, sizeof(fake_state));
3807 #ifdef SERIAL_DEBUG_PCI
3808 printk(KERN_DEBUG
3809 "Found Serial PCI device: %x:%x, %x:%x, %x\n",
3810 dev->vendor, dev->device, subvendor, subdevice,
3811 dev->class);
3812 printk(KERN_DEBUG
3813 " IRQ: %d, base: %lx (%s), num_ports: %d\n",
3814 dev->irq, port, board->flags & SPCI_FL_IOMEM ?
3815 "iomem" : "port", board->num_ports);
3816 #endif
3818 for (k=0; k < board->num_ports; k++) {
3819 if (board->flags & SPCI_FL_BASE_TABLE) {
3820 port = PCI_BASE_ADDRESS(dev, base_idx++);
3821 if (board->flags & SPCI_FL_IOMEM)
3822 port &= PCI_BASE_ADDRESS_MEM_MASK;
3823 else
3824 port &= PCI_BASE_ADDRESS_IO_MASK;
3826 fake_state.irq = dev->irq;
3827 fake_state.port = port;
3828 #ifdef CONFIG_SERIAL_PCI_MEMMAPPED
3829 if (board->flags & SPCI_FL_IOMEM) {
3830 fake_state.iomem_base =
3831 ioremap(port, board->uart_offset);
3832 fake_state.iomem_reg_shift = board->reg_shift;
3833 fake_state.port = 0;
3835 #endif
3836 port += uart_offset;
3837 fake_state.flags = ASYNC_SKIP_TEST | ASYNC_SHARE_IRQ;
3838 line = register_serial(&fake_state);
3839 if (line < 0)
3840 break;
3841 rs_table[line].baud_base = base_baud;
3845 #ifdef SERIAL_DEBUG_PCI
3846 printk(KERN_DEBUG "Leaving probe_serial_pci() (probe finished)\n");
3847 #endif
3848 return;
3851 #endif /* ENABLE_SERIAL_PCI */
3854 * The serial driver boot-time initialization code!
3856 int __init rs_init(void)
3858 int i;
3859 struct serial_state * state;
3860 extern void atomwide_serial_init (void);
3861 extern void dualsp_serial_init (void);
3863 #ifdef CONFIG_ATOMWIDE_SERIAL
3864 atomwide_serial_init ();
3865 #endif
3866 #ifdef CONFIG_DUALSP_SERIAL
3867 dualsp_serial_init ();
3868 #endif
3870 init_bh(SERIAL_BH, do_serial_bh);
3871 timer_table[RS_TIMER].fn = rs_timer;
3872 timer_table[RS_TIMER].expires = 0;
3874 for (i = 0; i < NR_IRQS; i++) {
3875 IRQ_ports[i] = 0;
3876 IRQ_timeout[i] = 0;
3877 #ifdef CONFIG_SERIAL_MULTIPORT
3878 memset(&rs_multiport[i], 0,
3879 sizeof(struct rs_multiport_struct));
3880 #endif
3882 #ifdef CONFIG_SERIAL_CONSOLE
3884 * The interrupt of the serial console port
3885 * can't be shared.
3887 if (sercons.flags & CON_CONSDEV) {
3888 for(i = 0; i < NR_PORTS; i++)
3889 if (i != sercons.index &&
3890 rs_table[i].irq == rs_table[sercons.index].irq)
3891 rs_table[i].irq = 0;
3893 #endif
3894 show_serial_version();
3896 /* Initialize the tty_driver structure */
3898 memset(&serial_driver, 0, sizeof(struct tty_driver));
3899 serial_driver.magic = TTY_DRIVER_MAGIC;
3900 #if (LINUX_VERSION_CODE > 0x20100)
3901 serial_driver.driver_name = "serial";
3902 #endif
3903 serial_driver.name = "ttyS";
3904 serial_driver.major = TTY_MAJOR;
3905 serial_driver.minor_start = 64 + SERIAL_DEV_OFFSET;
3906 serial_driver.num = NR_PORTS;
3907 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
3908 serial_driver.subtype = SERIAL_TYPE_NORMAL;
3909 serial_driver.init_termios = tty_std_termios;
3910 serial_driver.init_termios.c_cflag =
3911 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
3912 serial_driver.flags = TTY_DRIVER_REAL_RAW;
3913 serial_driver.refcount = &serial_refcount;
3914 serial_driver.table = serial_table;
3915 serial_driver.termios = serial_termios;
3916 serial_driver.termios_locked = serial_termios_locked;
3918 serial_driver.open = rs_open;
3919 serial_driver.close = rs_close;
3920 serial_driver.write = rs_write;
3921 serial_driver.put_char = rs_put_char;
3922 serial_driver.flush_chars = rs_flush_chars;
3923 serial_driver.write_room = rs_write_room;
3924 serial_driver.chars_in_buffer = rs_chars_in_buffer;
3925 serial_driver.flush_buffer = rs_flush_buffer;
3926 serial_driver.ioctl = rs_ioctl;
3927 serial_driver.throttle = rs_throttle;
3928 serial_driver.unthrottle = rs_unthrottle;
3929 serial_driver.set_termios = rs_set_termios;
3930 serial_driver.stop = rs_stop;
3931 serial_driver.start = rs_start;
3932 serial_driver.hangup = rs_hangup;
3933 #if (LINUX_VERSION_CODE >= 131394) /* Linux 2.1.66 */
3934 serial_driver.break_ctl = rs_break;
3935 #endif
3936 #if (LINUX_VERSION_CODE >= 131343)
3937 serial_driver.send_xchar = rs_send_xchar;
3938 serial_driver.wait_until_sent = rs_wait_until_sent;
3939 serial_driver.read_proc = rs_read_proc;
3940 #endif
3943 * The callout device is just like normal device except for
3944 * major number and the subtype code.
3946 callout_driver = serial_driver;
3947 callout_driver.name = "cua";
3948 callout_driver.major = TTYAUX_MAJOR;
3949 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
3950 #if (LINUX_VERSION_CODE >= 131343)
3951 callout_driver.read_proc = 0;
3952 callout_driver.proc_entry = 0;
3953 #endif
3955 if (tty_register_driver(&serial_driver))
3956 panic("Couldn't register serial driver\n");
3957 if (tty_register_driver(&callout_driver))
3958 panic("Couldn't register callout driver\n");
3960 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
3961 state->magic = SSTATE_MAGIC;
3962 state->line = i;
3963 state->type = PORT_UNKNOWN;
3964 state->custom_divisor = 0;
3965 state->close_delay = 5*HZ/10;
3966 state->closing_wait = 30*HZ;
3967 state->callout_termios = callout_driver.init_termios;
3968 state->normal_termios = serial_driver.init_termios;
3969 state->icount.cts = state->icount.dsr =
3970 state->icount.rng = state->icount.dcd = 0;
3971 state->icount.rx = state->icount.tx = 0;
3972 state->icount.frame = state->icount.parity = 0;
3973 state->icount.overrun = state->icount.brk = 0;
3974 state->irq = irq_cannonicalize(state->irq);
3975 if (state->port && check_region(state->port,8))
3976 continue;
3977 if (state->flags & ASYNC_BOOT_AUTOCONF)
3978 autoconfig(state);
3980 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
3981 if (state->type == PORT_UNKNOWN)
3982 continue;
3983 if ( (state->flags & ASYNC_BOOT_AUTOCONF)
3984 && (state->flags & ASYNC_AUTO_IRQ)
3985 && (state->port != 0))
3986 state->irq = detect_uart_irq(state);
3987 printk(KERN_INFO "ttyS%02d%s at 0x%04x (irq = %d) is a %s\n",
3988 state->line + SERIAL_DEV_OFFSET,
3989 (state->flags & ASYNC_FOURPORT) ? " FourPort" : "",
3990 state->port, state->irq,
3991 uart_config[state->type].name);
3993 #ifdef ENABLE_SERIAL_PCI
3994 probe_serial_pci();
3995 #endif
3996 return 0;
4000 * register_serial and unregister_serial allows for serial ports to be
4001 * configured at run-time, to support PCMCIA modems.
4003 int register_serial(struct serial_struct *req)
4005 int i;
4006 unsigned long flags;
4007 struct serial_state *state;
4009 save_flags(flags);
4010 cli();
4011 for (i = 0; i < NR_PORTS; i++) {
4012 if ((rs_table[i].port == req->port) &&
4013 (rs_table[i].iomem_base == req->iomem_base))
4014 break;
4016 if (i == NR_PORTS) {
4017 for (i = 0; i < NR_PORTS; i++)
4018 if ((rs_table[i].type == PORT_UNKNOWN) &&
4019 (rs_table[i].count == 0))
4020 break;
4022 if (i == NR_PORTS) {
4023 restore_flags(flags);
4024 return -1;
4026 state = &rs_table[i];
4027 if (rs_table[i].count) {
4028 restore_flags(flags);
4029 printk("Couldn't configure serial #%d (port=%d,irq=%d): "
4030 "device already open\n", i, req->port, req->irq);
4031 return -1;
4033 state->irq = req->irq;
4034 state->port = req->port;
4035 state->flags = req->flags;
4036 #ifdef CONFIG_SERIAL_PCI_MEMMAPPED
4037 state->iomem_base = req->iomem_base;
4038 state->iomem_reg_shift = req->iomem_reg_shift;
4039 #endif
4041 autoconfig(state);
4042 if (state->type == PORT_UNKNOWN) {
4043 restore_flags(flags);
4044 printk("register_serial(): autoconfig failed\n");
4045 return -1;
4047 restore_flags(flags);
4049 if ((state->flags & ASYNC_AUTO_IRQ) && CONFIGURED_SERIAL_PORT(state))
4050 state->irq = detect_uart_irq(state);
4052 printk(KERN_INFO "ttyS%02d at %s 0x%04lx (irq = %d) is a %s\n",
4053 state->line + SERIAL_DEV_OFFSET,
4054 state->iomem_base ? "iomem" : "port",
4055 state->iomem_base ? (unsigned long)state->iomem_base :
4056 (unsigned long)state->port,
4057 state->irq, uart_config[state->type].name);
4058 return state->line + SERIAL_DEV_OFFSET;
4061 void unregister_serial(int line)
4063 unsigned long flags;
4064 struct serial_state *state = &rs_table[line];
4066 save_flags(flags);
4067 cli();
4068 if (state->info && state->info->tty)
4069 tty_hangup(state->info->tty);
4070 state->type = PORT_UNKNOWN;
4071 printk(KERN_INFO "tty%02d unloaded\n", state->line);
4072 restore_flags(flags);
4075 #ifdef MODULE
4076 int init_module(void)
4078 return rs_init();
4081 void cleanup_module(void)
4083 unsigned long flags;
4084 int e1, e2;
4085 int i;
4086 struct async_struct *info;
4088 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
4089 save_flags(flags);
4090 cli();
4091 timer_active &= ~(1 << RS_TIMER);
4092 timer_table[RS_TIMER].fn = NULL;
4093 timer_table[RS_TIMER].expires = 0;
4094 remove_bh(SERIAL_BH);
4095 if ((e1 = tty_unregister_driver(&serial_driver)))
4096 printk("SERIAL: failed to unregister serial driver (%d)\n",
4097 e1);
4098 if ((e2 = tty_unregister_driver(&callout_driver)))
4099 printk("SERIAL: failed to unregister callout driver (%d)\n",
4100 e2);
4101 restore_flags(flags);
4103 for (i = 0; i < NR_PORTS; i++) {
4104 if ((rs_table[i].type != PORT_UNKNOWN) && rs_table[i].port)
4105 release_region(rs_table[i].port, 8);
4106 info = rs_table[i].info;
4107 if (info) {
4108 rs_table[i].info = NULL;
4109 kfree_s(info, sizeof(struct async_struct));
4111 #if defined(ENABLE_SERIAL_PCI) && defined (CONFIG_SERIAL_PCI_MEMMAPPED)
4112 if (rs_table[i].iomem_base)
4113 iounmap(rs_table[i].iomem_base);
4114 #endif
4116 #ifdef ENABLE_SERIAL_PCI
4117 for (i=0; i < serial_pci_board_idx; i++) {
4118 struct pci_board_inst *brd = &serial_pci_board[i];
4120 if (brd->board->init_fn)
4121 (brd->board->init_fn)(brd->dev, brd->board, 0);
4123 #endif
4124 if (tmp_buf) {
4125 free_page((unsigned long) tmp_buf);
4126 tmp_buf = NULL;
4129 #endif /* MODULE */
4133 * ------------------------------------------------------------
4134 * Serial console driver
4135 * ------------------------------------------------------------
4137 #ifdef CONFIG_SERIAL_CONSOLE
4139 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
4142 * Wait for transmitter & holding register to empty
4144 static inline void wait_for_xmitr(struct serial_state *ser)
4146 int lsr;
4147 unsigned int tmout = 1000000;
4149 do {
4150 lsr = inb(ser->port + UART_LSR);
4151 if (--tmout == 0) break;
4152 } while ((lsr & BOTH_EMPTY) != BOTH_EMPTY);
4156 * Print a string to the serial port trying not to disturb
4157 * any possible real use of the port...
4159 static void serial_console_write(struct console *co, const char *s,
4160 unsigned count)
4162 struct serial_state *ser;
4163 int ier;
4164 unsigned i;
4166 ser = rs_table + co->index;
4168 * First save the IER then disable the interrupts
4170 ier = inb(ser->port + UART_IER);
4171 outb(0x00, ser->port + UART_IER);
4174 * Now, do each character
4176 for (i = 0; i < count; i++, s++) {
4177 wait_for_xmitr(ser);
4180 * Send the character out.
4181 * If a LF, also do CR...
4183 outb(*s, ser->port + UART_TX);
4184 if (*s == 10) {
4185 wait_for_xmitr(ser);
4186 outb(13, ser->port + UART_TX);
4191 * Finally, Wait for transmitter & holding register to empty
4192 * and restore the IER
4194 wait_for_xmitr(ser);
4195 outb(ier, ser->port + UART_IER);
4199 * Receive character from the serial port
4201 static int serial_console_wait_key(struct console *co)
4203 struct serial_state *ser;
4204 int ier;
4205 int lsr;
4206 int c;
4208 ser = rs_table + co->index;
4211 * First save the IER then disable the interrupts so
4212 * that the real driver for the port does not get the
4213 * character.
4215 ier = inb(ser->port + UART_IER);
4216 outb(0x00, ser->port + UART_IER);
4218 do {
4219 lsr = inb(ser->port + UART_LSR);
4220 } while (!(lsr & UART_LSR_DR));
4221 c = inb(ser->port + UART_RX);
4224 * Restore the interrupts
4226 outb(ier, ser->port + UART_IER);
4228 return c;
4231 static kdev_t serial_console_device(struct console *c)
4233 return MKDEV(TTY_MAJOR, 64 + c->index);
4237 * Setup initial baud/bits/parity. We do two things here:
4238 * - construct a cflag setting for the first rs_open()
4239 * - initialize the serial port
4240 * Return non-zero if we didn't find a serial port.
4242 static int __init serial_console_setup(struct console *co, char *options)
4244 struct serial_state *ser;
4245 unsigned cval;
4246 int baud = 9600;
4247 int bits = 8;
4248 int parity = 'n';
4249 int cflag = CREAD | HUPCL | CLOCAL;
4250 int quot = 0;
4251 char *s;
4253 if (options) {
4254 baud = simple_strtoul(options, NULL, 10);
4255 s = options;
4256 while(*s >= '0' && *s <= '9')
4257 s++;
4258 if (*s) parity = *s++;
4259 if (*s) bits = *s - '0';
4263 * Now construct a cflag setting.
4265 switch(baud) {
4266 case 1200:
4267 cflag |= B1200;
4268 break;
4269 case 2400:
4270 cflag |= B2400;
4271 break;
4272 case 4800:
4273 cflag |= B4800;
4274 break;
4275 case 19200:
4276 cflag |= B19200;
4277 break;
4278 case 38400:
4279 cflag |= B38400;
4280 break;
4281 case 57600:
4282 cflag |= B57600;
4283 break;
4284 case 115200:
4285 cflag |= B115200;
4286 break;
4287 case 9600:
4288 default:
4289 cflag |= B9600;
4290 break;
4292 switch(bits) {
4293 case 7:
4294 cflag |= CS7;
4295 break;
4296 default:
4297 case 8:
4298 cflag |= CS8;
4299 break;
4301 switch(parity) {
4302 case 'o': case 'O':
4303 cflag |= PARODD;
4304 break;
4305 case 'e': case 'E':
4306 cflag |= PARENB;
4307 break;
4309 co->cflag = cflag;
4312 * Divisor, bytesize and parity
4314 ser = rs_table + co->index;
4315 quot = ser->baud_base / baud;
4316 cval = cflag & (CSIZE | CSTOPB);
4317 #if defined(__powerpc__) || defined(__alpha__)
4318 cval >>= 8;
4319 #else /* !__powerpc__ && !__alpha__ */
4320 cval >>= 4;
4321 #endif /* !__powerpc__ && !__alpha__ */
4322 if (cflag & PARENB)
4323 cval |= UART_LCR_PARITY;
4324 if (!(cflag & PARODD))
4325 cval |= UART_LCR_EPAR;
4328 * Disable UART interrupts, set DTR and RTS high
4329 * and set speed.
4331 outb(cval | UART_LCR_DLAB, ser->port + UART_LCR); /* set DLAB */
4332 outb(quot & 0xff, ser->port + UART_DLL); /* LS of divisor */
4333 outb(quot >> 8, ser->port + UART_DLM); /* MS of divisor */
4334 outb(cval, ser->port + UART_LCR); /* reset DLAB */
4335 outb(0, ser->port + UART_IER);
4336 outb(UART_MCR_DTR | UART_MCR_RTS, ser->port + UART_MCR);
4339 * If we read 0xff from the LSR, there is no UART here.
4341 if (inb(ser->port + UART_LSR) == 0xff)
4342 return -1;
4343 return 0;
4346 static struct console sercons = {
4347 "ttyS",
4348 serial_console_write,
4349 NULL,
4350 serial_console_device,
4351 serial_console_wait_key,
4352 NULL,
4353 serial_console_setup,
4354 CON_PRINTBUFFER,
4357 NULL
4361 * Register console.
4363 void __init serial_console_init(void)
4365 register_console(&sercons);
4367 #endif
4370 Local variables:
4371 compile-command: "gcc -D__KERNEL__ -I../../include -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -fno-strict-aliasing -D__SMP__ -pipe -fno-strength-reduce -DCPU=686 -march=i686 -DMODULE -DMODVERSIONS -include ../../include/linux/modversions.h -DEXPORT_SYMTAB -c serial.c"
4372 End: