* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / drivers / char / serial.c
blob44fd4fe8f1f3c2b72ba553da90cbf0ca2aaa7bef
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>
156 #ifdef LOCAL_HEADERS
157 #include "serial_local.h"
158 #else
159 #include <linux/serial.h>
160 #include <linux/serial_reg.h>
161 #include <asm/serial.h>
162 static char *serial_version = "4.30";
163 #endif
165 #include <linux/errno.h>
166 #include <linux/signal.h>
167 #include <linux/sched.h>
168 #include <linux/timer.h>
169 #include <linux/interrupt.h>
170 #include <linux/tty.h>
171 #include <linux/tty_flip.h>
172 #include <linux/major.h>
173 #include <linux/string.h>
174 #include <linux/fcntl.h>
175 #include <linux/ptrace.h>
176 #include <linux/ioport.h>
177 #include <linux/mm.h>
178 #include <linux/malloc.h>
179 #if (LINUX_VERSION_CODE >= 131343) /* 2.1.15 -- XX get correct version */
180 #include <linux/init.h>
181 #else
182 #define __initfunc(x) x
183 #endif
184 #include <linux/delay.h>
185 #ifdef CONFIG_SERIAL_CONSOLE
186 #include <linux/console.h>
187 #endif
188 #ifdef ENABLE_SERIAL_PCI
189 #include <linux/pci.h>
190 #endif
192 #include <asm/system.h>
193 #include <asm/io.h>
194 #include <asm/irq.h>
195 #include <asm/bitops.h>
197 #ifdef CONFIG_MAC_SERIAL
198 #define SERIAL_DEV_OFFSET 2
199 #else
200 #define SERIAL_DEV_OFFSET 0
201 #endif
203 #ifdef SERIAL_INLINE
204 #define _INLINE_ inline
205 #endif
207 static char *serial_name = "Serial driver";
209 static DECLARE_TASK_QUEUE(tq_serial);
211 static struct tty_driver serial_driver, callout_driver;
212 static int serial_refcount;
214 /* serial subtype definitions */
215 #ifndef SERIAL_TYPE_NORMAL
216 #define SERIAL_TYPE_NORMAL 1
217 #define SERIAL_TYPE_CALLOUT 2
218 #endif
220 /* number of characters left in xmit buffer before we ask for more */
221 #define WAKEUP_CHARS 256
224 * IRQ_timeout - How long the timeout should be for each IRQ
225 * should be after the IRQ has been active.
228 static struct async_struct *IRQ_ports[NR_IRQS];
229 #ifdef CONFIG_SERIAL_MULTIPORT
230 static struct rs_multiport_struct rs_multiport[NR_IRQS];
231 #endif
232 static int IRQ_timeout[NR_IRQS];
233 #ifdef CONFIG_SERIAL_CONSOLE
234 static struct console sercons;
235 #endif
237 static unsigned detect_uart_irq (struct serial_state * state);
238 static void autoconfig(struct serial_state * info);
239 static void change_speed(struct async_struct *info, struct termios *old);
240 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
243 * Here we define the default xmit fifo size used for each type of
244 * UART
246 static struct serial_uart_config uart_config[] = {
247 { "unknown", 1, 0 },
248 { "8250", 1, 0 },
249 { "16450", 1, 0 },
250 { "16550", 1, 0 },
251 { "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
252 { "cirrus", 1, 0 }, /* usurped by cyclades.c */
253 { "ST16650", 1, UART_CLEAR_FIFO |UART_STARTECH },
254 { "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO |
255 UART_STARTECH },
256 { "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO},
257 { "Startech", 1, 0}, /* usurped by cyclades.c */
258 { "16C950", 128, UART_CLEAR_FIFO | UART_USE_FIFO},
259 { "ST16654", 64, UART_CLEAR_FIFO | UART_USE_FIFO |
260 UART_STARTECH },
261 { "XR16850", 128, UART_CLEAR_FIFO | UART_USE_FIFO |
262 UART_STARTECH },
263 { 0, 0}
266 static struct serial_state rs_table[RS_TABLE_SIZE] = {
267 SERIAL_PORT_DFNS /* Defined in serial.h */
270 #define NR_PORTS (sizeof(rs_table)/sizeof(struct serial_state))
272 #ifdef ENABLE_SERIAL_PCI
273 #define NR_PCI_BOARDS 8
274 static struct pci_board_inst serial_pci_board[NR_PCI_BOARDS];
275 static int serial_pci_board_idx = 0;
276 #define PCI_BASE_ADDRESS(dev, r) ((dev)->resource[r].start)
277 #endif /* ENABLE_SERIAL_PCI */
279 static struct tty_struct *serial_table[NR_PORTS];
280 static struct termios *serial_termios[NR_PORTS];
281 static struct termios *serial_termios_locked[NR_PORTS];
283 #ifndef MIN
284 #define MIN(a,b) ((a) < (b) ? (a) : (b))
285 #endif
288 * tmp_buf is used as a temporary buffer by serial_write. We need to
289 * lock it in case the copy_from_user blocks while swapping in a page,
290 * and some other program tries to do a serial write at the same time.
291 * Since the lock will only come under contention when the system is
292 * swapping and available memory is low, it makes sense to share one
293 * buffer across all the serial ports, since it significantly saves
294 * memory if large numbers of serial ports are open.
296 static unsigned char *tmp_buf;
297 #ifdef DECLARE_MUTEX
298 static DECLARE_MUTEX(tmp_buf_sem);
299 #else
300 static struct semaphore tmp_buf_sem = MUTEX;
301 #endif
304 * Provide backwards compatibility for kernels prior to 2.1.XX.
306 #if (LINUX_VERSION_CODE < 0x20000)
307 typedef dev_t kdev_t;
308 #endif
310 #if (LINUX_VERSION_CODE < 0x02017E)
311 static signed long schedule_timeout(signed long timeout)
313 unsigned long expire;
315 expire = timeout + jiffies;
317 current->timeout = jiffies + timeout;
318 schedule();
320 timeout = expire - jiffies;
321 return timeout < 0 ? 0 : timeout;
323 #endif
325 #ifndef time_after
326 #define time_after(a,b) ((long)(b) - (long)(a) < 0)
327 #endif
329 #if (LINUX_VERSION_CODE < 0x020100)
330 static inline int irq_cannonicalize(int irq)
332 return ((irq == 2) ? 9 : irq);
334 #endif
336 #if (LINUX_VERSION_CODE < 131336)
337 static int copy_from_user(void *to, const void *from_user, unsigned long len)
339 int error;
341 error = verify_area(VERIFY_READ, from_user, len);
342 if (error)
343 return len;
344 memcpy_fromfs(to, from_user, len);
345 return 0;
348 static int copy_to_user(void *to_user, const void *from, unsigned long len)
350 int error;
352 error = verify_area(VERIFY_WRITE, to_user, len);
353 if (error)
354 return len;
355 memcpy_tofs(to_user, from, len);
356 return 0;
359 static inline int signal_pending(struct task_struct *p)
361 return (p->signal & (~p->blocked != 0));
364 #else
365 #include <asm/uaccess.h>
366 #endif
368 #ifdef CAP_SYS_ADMIN
369 #define serial_isroot() (capable(CAP_SYS_ADMIN))
370 #else
371 #define serial_isroot() (suser())
372 #endif
374 #if (LINUX_VERSION_CODE < 131394) /* 2.1.66 */
375 #define test_and_clear_bit(x,y) clear_bit(x,y)
377 static inline void remove_bh(int nr)
379 bh_base[nr] = NULL;
380 bh_mask &= ~(1 << nr);
382 #endif
385 static inline int serial_paranoia_check(struct async_struct *info,
386 kdev_t device, const char *routine)
388 #ifdef SERIAL_PARANOIA_CHECK
389 static const char *badmagic =
390 "Warning: bad magic number for serial struct (%s) in %s\n";
391 static const char *badinfo =
392 "Warning: null async_struct for (%s) in %s\n";
394 if (!info) {
395 printk(badinfo, kdevname(device), routine);
396 return 1;
398 if (info->magic != SERIAL_MAGIC) {
399 printk(badmagic, kdevname(device), routine);
400 return 1;
402 #endif
403 return 0;
406 static inline unsigned int serial_in(struct async_struct *info, int offset)
408 #ifdef CONFIG_HUB6
409 if (info->hub6) {
410 outb(info->hub6 - 1 + offset, info->port);
411 return inb(info->port+1);
412 } else
413 #endif
414 #ifdef CONFIG_SERIAL_PCI_MEMMAPPED
415 if (info->iomem_base)
416 return readb(info->iomem_base + (offset<<info->iomem_reg_shift));
417 else
418 #endif
419 return inb(info->port + offset);
422 static inline unsigned int serial_inp(struct async_struct *info, int offset)
424 #ifdef CONFIG_HUB6
425 if (info->hub6) {
426 outb(info->hub6 - 1 + offset, info->port);
427 return inb_p(info->port+1);
428 } else
429 #endif
430 #ifdef CONFIG_SERIAL_PCI_MEMMAPPED
431 if (info->iomem_base)
432 return readb(info->iomem_base + (offset<<info->iomem_reg_shift));
433 else
434 #endif
435 #ifdef CONFIG_SERIAL_NOPAUSE_IO
436 return inb(info->port + offset);
437 #else
438 return inb_p(info->port + offset);
439 #endif
442 static inline void serial_out(struct async_struct *info, int offset, int value)
444 #ifdef CONFIG_HUB6
445 if (info->hub6) {
446 outb(info->hub6 - 1 + offset, info->port);
447 outb(value, info->port+1);
448 } else
449 #endif
450 #ifdef CONFIG_SERIAL_PCI_MEMMAPPED
451 if (info->iomem_base)
452 writeb(value, info->iomem_base + (offset<<info->iomem_reg_shift));
453 else
454 #endif
455 outb(value, info->port+offset);
458 static inline void serial_outp(struct async_struct *info, int offset,
459 int value)
461 #ifdef CONFIG_HUB6
462 if (info->hub6) {
463 outb(info->hub6 - 1 + offset, info->port);
464 outb_p(value, info->port+1);
465 } else
466 #endif
467 #ifdef CONFIG_SERIAL_PCI_MEMMAPPED
468 if (info->iomem_base)
469 writeb(value, info->iomem_base + (offset<<info->iomem_reg_shift));
470 else
471 #endif
472 #ifdef CONFIG_SERIAL_NOPAUSE_IO
473 outb(value, info->port+offset);
474 #else
475 outb_p(value, info->port+offset);
476 #endif
480 * For the 16C950
482 void serial_icr_write(struct async_struct *info, int offset, int value)
484 serial_out(info, UART_SCR, offset);
485 serial_out(info, UART_ICR, value);
488 unsigned int serial_icr_read(struct async_struct *info, int offset)
490 int value;
492 serial_icr_write(info, UART_ACR, info->ACR | UART_ACR_ICRRD);
493 serial_out(info, UART_SCR, offset);
494 value = serial_in(info, UART_ICR);
495 serial_icr_write(info, UART_ACR, info->ACR);
496 return value;
500 * ------------------------------------------------------------
501 * rs_stop() and rs_start()
503 * This routines are called before setting or resetting tty->stopped.
504 * They enable or disable transmitter interrupts, as necessary.
505 * ------------------------------------------------------------
507 static void rs_stop(struct tty_struct *tty)
509 struct async_struct *info = (struct async_struct *)tty->driver_data;
510 unsigned long flags;
512 if (serial_paranoia_check(info, tty->device, "rs_stop"))
513 return;
515 save_flags(flags); cli();
516 if (info->IER & UART_IER_THRI) {
517 info->IER &= ~UART_IER_THRI;
518 serial_out(info, UART_IER, info->IER);
520 if (info->state->type == PORT_16C950) {
521 info->ACR |= UART_ACR_TXDIS;
522 serial_icr_write(info, UART_ACR, info->ACR);
524 restore_flags(flags);
527 static void rs_start(struct tty_struct *tty)
529 struct async_struct *info = (struct async_struct *)tty->driver_data;
530 unsigned long flags;
532 if (serial_paranoia_check(info, tty->device, "rs_start"))
533 return;
535 save_flags(flags); cli();
536 if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
537 info->IER |= UART_IER_THRI;
538 serial_out(info, UART_IER, info->IER);
540 if (info->state->type == PORT_16C950) {
541 info->ACR &= ~UART_ACR_TXDIS;
542 serial_icr_write(info, UART_ACR, info->ACR);
544 restore_flags(flags);
548 * ----------------------------------------------------------------------
550 * Here starts the interrupt handling routines. All of the following
551 * subroutines are declared as inline and are folded into
552 * rs_interrupt(). They were separated out for readability's sake.
554 * Note: rs_interrupt() is a "fast" interrupt, which means that it
555 * runs with interrupts turned off. People who may want to modify
556 * rs_interrupt() should try to keep the interrupt handler as fast as
557 * possible. After you are done making modifications, it is not a bad
558 * idea to do:
560 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
562 * and look at the resulting assemble code in serial.s.
564 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
565 * -----------------------------------------------------------------------
569 * This routine is used by the interrupt handler to schedule
570 * processing in the software interrupt portion of the driver.
572 static _INLINE_ void rs_sched_event(struct async_struct *info,
573 int event)
575 info->event |= 1 << event;
576 queue_task(&info->tqueue, &tq_serial);
577 mark_bh(SERIAL_BH);
580 static _INLINE_ void receive_chars(struct async_struct *info,
581 int *status)
583 struct tty_struct *tty = info->tty;
584 unsigned char ch;
585 int ignored = 0;
586 struct async_icount *icount;
588 icount = &info->state->icount;
589 do {
590 ch = serial_inp(info, UART_RX);
591 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
592 break;
593 *tty->flip.char_buf_ptr = ch;
594 icount->rx++;
596 #ifdef SERIAL_DEBUG_INTR
597 printk("DR%02x:%02x...", ch, *status);
598 #endif
599 *tty->flip.flag_buf_ptr = 0;
600 if (*status & (UART_LSR_BI | UART_LSR_PE |
601 UART_LSR_FE | UART_LSR_OE)) {
603 * For statistics only
605 if (*status & UART_LSR_BI) {
606 *status &= ~(UART_LSR_FE | UART_LSR_PE);
607 icount->brk++;
608 } else if (*status & UART_LSR_PE)
609 icount->parity++;
610 else if (*status & UART_LSR_FE)
611 icount->frame++;
612 if (*status & UART_LSR_OE)
613 icount->overrun++;
616 * Now check to see if character should be
617 * ignored, and mask off conditions which
618 * should be ignored.
620 if (*status & info->ignore_status_mask) {
621 if (++ignored > 100)
622 break;
623 goto ignore_char;
625 *status &= info->read_status_mask;
627 if (*status & (UART_LSR_BI)) {
628 #ifdef SERIAL_DEBUG_INTR
629 printk("handling break....");
630 #endif
631 *tty->flip.flag_buf_ptr = TTY_BREAK;
632 if (info->flags & ASYNC_SAK)
633 do_SAK(tty);
634 } else if (*status & UART_LSR_PE)
635 *tty->flip.flag_buf_ptr = TTY_PARITY;
636 else if (*status & UART_LSR_FE)
637 *tty->flip.flag_buf_ptr = TTY_FRAME;
638 if (*status & UART_LSR_OE) {
640 * Overrun is special, since it's
641 * reported immediately, and doesn't
642 * affect the current character
644 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
645 tty->flip.count++;
646 tty->flip.flag_buf_ptr++;
647 tty->flip.char_buf_ptr++;
648 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
652 tty->flip.flag_buf_ptr++;
653 tty->flip.char_buf_ptr++;
654 tty->flip.count++;
655 ignore_char:
656 *status = serial_inp(info, UART_LSR);
657 } while (*status & UART_LSR_DR);
658 #if (LINUX_VERSION_CODE > 131394) /* 2.1.66 */
659 tty_flip_buffer_push(tty);
660 #else
661 queue_task_irq_off(&tty->flip.tqueue, &tq_timer);
662 #endif
665 static _INLINE_ void transmit_chars(struct async_struct *info, int *intr_done)
667 int count;
669 if (info->x_char) {
670 serial_outp(info, UART_TX, info->x_char);
671 info->state->icount.tx++;
672 info->x_char = 0;
673 if (intr_done)
674 *intr_done = 0;
675 return;
677 if ((info->xmit_cnt <= 0) || info->tty->stopped ||
678 info->tty->hw_stopped) {
679 info->IER &= ~UART_IER_THRI;
680 serial_out(info, UART_IER, info->IER);
681 return;
684 count = info->xmit_fifo_size;
685 do {
686 serial_out(info, UART_TX, info->xmit_buf[info->xmit_tail++]);
687 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
688 info->state->icount.tx++;
689 if (--info->xmit_cnt <= 0)
690 break;
691 } while (--count > 0);
693 if (info->xmit_cnt < WAKEUP_CHARS)
694 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
696 #ifdef SERIAL_DEBUG_INTR
697 printk("THRE...");
698 #endif
699 if (intr_done)
700 *intr_done = 0;
702 if (info->xmit_cnt <= 0) {
703 info->IER &= ~UART_IER_THRI;
704 serial_out(info, UART_IER, info->IER);
708 static _INLINE_ void check_modem_status(struct async_struct *info)
710 int status;
711 struct async_icount *icount;
713 status = serial_in(info, UART_MSR);
715 if (status & UART_MSR_ANY_DELTA) {
716 icount = &info->state->icount;
717 /* update input line counters */
718 if (status & UART_MSR_TERI)
719 icount->rng++;
720 if (status & UART_MSR_DDSR)
721 icount->dsr++;
722 if (status & UART_MSR_DDCD) {
723 icount->dcd++;
724 #ifdef CONFIG_HARD_PPS
725 if ((info->flags & ASYNC_HARDPPS_CD) &&
726 (status & UART_MSR_DCD))
727 hardpps();
728 #endif
730 if (status & UART_MSR_DCTS)
731 icount->cts++;
732 wake_up_interruptible(&info->delta_msr_wait);
735 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
736 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
737 printk("ttys%d CD now %s...", info->line,
738 (status & UART_MSR_DCD) ? "on" : "off");
739 #endif
740 if (status & UART_MSR_DCD)
741 wake_up_interruptible(&info->open_wait);
742 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
743 (info->flags & ASYNC_CALLOUT_NOHUP))) {
744 #ifdef SERIAL_DEBUG_OPEN
745 printk("doing serial hangup...");
746 #endif
747 if (info->tty)
748 tty_hangup(info->tty);
751 if (info->flags & ASYNC_CTS_FLOW) {
752 if (info->tty->hw_stopped) {
753 if (status & UART_MSR_CTS) {
754 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
755 printk("CTS tx start...");
756 #endif
757 info->tty->hw_stopped = 0;
758 info->IER |= UART_IER_THRI;
759 serial_out(info, UART_IER, info->IER);
760 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
761 return;
763 } else {
764 if (!(status & UART_MSR_CTS)) {
765 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
766 printk("CTS tx stop...");
767 #endif
768 info->tty->hw_stopped = 1;
769 info->IER &= ~UART_IER_THRI;
770 serial_out(info, UART_IER, info->IER);
776 #ifdef CONFIG_SERIAL_SHARE_IRQ
778 * This is the serial driver's generic interrupt routine
780 static void rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
782 int status;
783 struct async_struct * info;
784 int pass_counter = 0;
785 struct async_struct *end_mark = 0;
786 #ifdef CONFIG_SERIAL_MULTIPORT
787 int first_multi = 0;
788 struct rs_multiport_struct *multi;
789 #endif
791 #ifdef SERIAL_DEBUG_INTR
792 printk("rs_interrupt(%d)...", irq);
793 #endif
795 info = IRQ_ports[irq];
796 if (!info)
797 return;
799 #ifdef CONFIG_SERIAL_MULTIPORT
800 multi = &rs_multiport[irq];
801 if (multi->port_monitor)
802 first_multi = inb(multi->port_monitor);
803 #endif
805 do {
806 if (!info->tty ||
807 (serial_in(info, UART_IIR) & UART_IIR_NO_INT)) {
808 if (!end_mark)
809 end_mark = info;
810 goto next;
812 end_mark = 0;
814 info->last_active = jiffies;
816 status = serial_inp(info, UART_LSR);
817 #ifdef SERIAL_DEBUG_INTR
818 printk("status = %x...", status);
819 #endif
820 if (status & UART_LSR_DR)
821 receive_chars(info, &status);
822 check_modem_status(info);
823 if (status & UART_LSR_THRE)
824 transmit_chars(info, 0);
826 next:
827 info = info->next_port;
828 if (!info) {
829 info = IRQ_ports[irq];
830 if (pass_counter++ > RS_ISR_PASS_LIMIT) {
831 #if 0
832 printk("rs loop break\n");
833 #endif
834 break; /* Prevent infinite loops */
836 continue;
838 } while (end_mark != info);
839 #ifdef CONFIG_SERIAL_MULTIPORT
840 if (multi->port_monitor)
841 printk("rs port monitor (normal) irq %d: 0x%x, 0x%x\n",
842 info->state->irq, first_multi,
843 inb(multi->port_monitor));
844 #endif
845 #ifdef SERIAL_DEBUG_INTR
846 printk("end.\n");
847 #endif
849 #endif /* #ifdef CONFIG_SERIAL_SHARE_IRQ */
853 * This is the serial driver's interrupt routine for a single port
855 static void rs_interrupt_single(int irq, void *dev_id, struct pt_regs * regs)
857 int status;
858 int pass_counter = 0;
859 struct async_struct * info;
860 #ifdef CONFIG_SERIAL_MULTIPORT
861 int first_multi = 0;
862 struct rs_multiport_struct *multi;
863 #endif
865 #ifdef SERIAL_DEBUG_INTR
866 printk("rs_interrupt_single(%d)...", irq);
867 #endif
869 info = IRQ_ports[irq];
870 if (!info || !info->tty)
871 return;
873 #ifdef CONFIG_SERIAL_MULTIPORT
874 multi = &rs_multiport[irq];
875 if (multi->port_monitor)
876 first_multi = inb(multi->port_monitor);
877 #endif
879 do {
880 status = serial_inp(info, UART_LSR);
881 #ifdef SERIAL_DEBUG_INTR
882 printk("status = %x...", status);
883 #endif
884 if (status & UART_LSR_DR)
885 receive_chars(info, &status);
886 check_modem_status(info);
887 if (status & UART_LSR_THRE)
888 transmit_chars(info, 0);
889 if (pass_counter++ > RS_ISR_PASS_LIMIT) {
890 #if 0
891 printk("rs_single loop break.\n");
892 #endif
893 break;
895 } while (!(serial_in(info, UART_IIR) & UART_IIR_NO_INT));
896 info->last_active = jiffies;
897 #ifdef CONFIG_SERIAL_MULTIPORT
898 if (multi->port_monitor)
899 printk("rs port monitor (single) irq %d: 0x%x, 0x%x\n",
900 info->state->irq, first_multi,
901 inb(multi->port_monitor));
902 #endif
903 #ifdef SERIAL_DEBUG_INTR
904 printk("end.\n");
905 #endif
908 #ifdef CONFIG_SERIAL_MULTIPORT
910 * This is the serial driver's for multiport boards
912 static void rs_interrupt_multi(int irq, void *dev_id, struct pt_regs * regs)
914 int status;
915 struct async_struct * info;
916 int pass_counter = 0;
917 int first_multi= 0;
918 struct rs_multiport_struct *multi;
920 #ifdef SERIAL_DEBUG_INTR
921 printk("rs_interrupt_multi(%d)...", irq);
922 #endif
924 info = IRQ_ports[irq];
925 if (!info)
926 return;
927 multi = &rs_multiport[irq];
928 if (!multi->port1) {
929 /* Should never happen */
930 printk("rs_interrupt_multi: NULL port1!\n");
931 return;
933 if (multi->port_monitor)
934 first_multi = inb(multi->port_monitor);
936 while (1) {
937 if (!info->tty ||
938 (serial_in(info, UART_IIR) & UART_IIR_NO_INT))
939 goto next;
941 info->last_active = jiffies;
943 status = serial_inp(info, UART_LSR);
944 #ifdef SERIAL_DEBUG_INTR
945 printk("status = %x...", status);
946 #endif
947 if (status & UART_LSR_DR)
948 receive_chars(info, &status);
949 check_modem_status(info);
950 if (status & UART_LSR_THRE)
951 transmit_chars(info, 0);
953 next:
954 info = info->next_port;
955 if (info)
956 continue;
958 info = IRQ_ports[irq];
959 if (pass_counter++ > RS_ISR_PASS_LIMIT) {
960 #if 1
961 printk("rs_multi loop break\n");
962 #endif
963 break; /* Prevent infinite loops */
965 if (multi->port_monitor)
966 printk("rs port monitor irq %d: 0x%x, 0x%x\n",
967 info->state->irq, first_multi,
968 inb(multi->port_monitor));
969 if ((inb(multi->port1) & multi->mask1) != multi->match1)
970 continue;
971 if (!multi->port2)
972 break;
973 if ((inb(multi->port2) & multi->mask2) != multi->match2)
974 continue;
975 if (!multi->port3)
976 break;
977 if ((inb(multi->port3) & multi->mask3) != multi->match3)
978 continue;
979 if (!multi->port4)
980 break;
981 if ((inb(multi->port4) & multi->mask4) != multi->match4)
982 continue;
983 break;
985 #ifdef SERIAL_DEBUG_INTR
986 printk("end.\n");
987 #endif
989 #endif
992 * -------------------------------------------------------------------
993 * Here ends the serial interrupt routines.
994 * -------------------------------------------------------------------
998 * This routine is used to handle the "bottom half" processing for the
999 * serial driver, known also the "software interrupt" processing.
1000 * This processing is done at the kernel interrupt level, after the
1001 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
1002 * is where time-consuming activities which can not be done in the
1003 * interrupt driver proper are done; the interrupt driver schedules
1004 * them using rs_sched_event(), and they get done here.
1006 static void do_serial_bh(void)
1008 run_task_queue(&tq_serial);
1011 static void do_softint(void *private_)
1013 struct async_struct *info = (struct async_struct *) private_;
1014 struct tty_struct *tty;
1016 tty = info->tty;
1017 if (!tty)
1018 return;
1020 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
1021 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1022 tty->ldisc.write_wakeup)
1023 (tty->ldisc.write_wakeup)(tty);
1024 wake_up_interruptible(&tty->write_wait);
1029 * This subroutine is called when the RS_TIMER goes off. It is used
1030 * by the serial driver to handle ports that do not have an interrupt
1031 * (irq=0). This doesn't work very well for 16450's, but gives barely
1032 * passable results for a 16550A. (Although at the expense of much
1033 * CPU overhead).
1035 static void rs_timer(void)
1037 static unsigned long last_strobe = 0;
1038 struct async_struct *info;
1039 unsigned int i;
1040 unsigned long flags;
1042 if ((jiffies - last_strobe) >= RS_STROBE_TIME) {
1043 for (i=1; i < NR_IRQS; i++) {
1044 info = IRQ_ports[i];
1045 if (!info)
1046 continue;
1047 save_flags(flags); cli();
1048 #ifdef CONFIG_SERIAL_SHARE_IRQ
1049 if (info->next_port) {
1050 do {
1051 serial_out(info, UART_IER, 0);
1052 info->IER |= UART_IER_THRI;
1053 serial_out(info, UART_IER, info->IER);
1054 info = info->next_port;
1055 } while (info);
1056 #ifdef CONFIG_SERIAL_MULTIPORT
1057 if (rs_multiport[i].port1)
1058 rs_interrupt_multi(i, NULL, NULL);
1059 else
1060 #endif
1061 rs_interrupt(i, NULL, NULL);
1062 } else
1063 #endif /* CONFIG_SERIAL_SHARE_IRQ */
1064 rs_interrupt_single(i, NULL, NULL);
1065 restore_flags(flags);
1068 last_strobe = jiffies;
1069 timer_table[RS_TIMER].expires = jiffies + RS_STROBE_TIME;
1070 timer_active |= 1 << RS_TIMER;
1072 if (IRQ_ports[0]) {
1073 save_flags(flags); cli();
1074 #ifdef CONFIG_SERIAL_SHARE_IRQ
1075 rs_interrupt(0, NULL, NULL);
1076 #else
1077 rs_interrupt_single(0, NULL, NULL);
1078 #endif
1079 restore_flags(flags);
1081 timer_table[RS_TIMER].expires = jiffies + IRQ_timeout[0] - 2;
1086 * ---------------------------------------------------------------
1087 * Low level utility subroutines for the serial driver: routines to
1088 * figure out the appropriate timeout for an interrupt chain, routines
1089 * to initialize and startup a serial port, and routines to shutdown a
1090 * serial port. Useful stuff like that.
1091 * ---------------------------------------------------------------
1095 * This routine figures out the correct timeout for a particular IRQ.
1096 * It uses the smallest timeout of all of the serial ports in a
1097 * particular interrupt chain. Now only used for IRQ 0....
1099 static void figure_IRQ_timeout(int irq)
1101 struct async_struct *info;
1102 int timeout = 60*HZ; /* 60 seconds === a long time :-) */
1104 info = IRQ_ports[irq];
1105 if (!info) {
1106 IRQ_timeout[irq] = 60*HZ;
1107 return;
1109 while (info) {
1110 if (info->timeout < timeout)
1111 timeout = info->timeout;
1112 info = info->next_port;
1114 if (!irq)
1115 timeout = timeout / 2;
1116 IRQ_timeout[irq] = timeout ? timeout : 1;
1119 static int startup(struct async_struct * info)
1121 unsigned long flags;
1122 int retval=0;
1123 void (*handler)(int, void *, struct pt_regs *);
1124 struct serial_state *state= info->state;
1125 unsigned long page;
1126 #ifdef CONFIG_SERIAL_MANY_PORTS
1127 unsigned short ICP;
1128 #endif
1130 page = get_free_page(GFP_KERNEL);
1131 if (!page)
1132 return -ENOMEM;
1134 save_flags(flags); cli();
1136 if (info->flags & ASYNC_INITIALIZED) {
1137 free_page(page);
1138 goto errout;
1141 if (!CONFIGURED_SERIAL_PORT(state) || !state->type) {
1142 if (info->tty)
1143 set_bit(TTY_IO_ERROR, &info->tty->flags);
1144 free_page(page);
1145 goto errout;
1147 if (info->xmit_buf)
1148 free_page(page);
1149 else
1150 info->xmit_buf = (unsigned char *) page;
1152 #ifdef SERIAL_DEBUG_OPEN
1153 printk("starting up ttys%d (irq %d)...", info->line, state->irq);
1154 #endif
1156 if (uart_config[info->state->type].flags & UART_STARTECH) {
1157 /* Wake up UART */
1158 serial_outp(info, UART_LCR, 0xBF);
1159 serial_outp(info, UART_EFR, UART_EFR_ECB);
1161 * Turn off LCR == 0xBF so we actually set the IER
1162 * register on the XR16C850
1164 serial_outp(info, UART_LCR, 0);
1165 serial_outp(info, UART_IER, 0);
1167 * Now reset LCR so we can turn off the ECB bit
1169 serial_outp(info, UART_LCR, 0xBF);
1170 serial_outp(info, UART_EFR, 0);
1172 * For a XR16C850, we need to set the trigger levels
1174 if (info->state->type == PORT_16850) {
1175 serial_outp(info, UART_FCTR, UART_FCTR_TRGD |
1176 UART_FCTR_RX);
1177 serial_outp(info, UART_TRG, UART_TRG_96);
1178 serial_outp(info, UART_FCTR, UART_FCTR_TRGD |
1179 UART_FCTR_TX);
1180 serial_outp(info, UART_TRG, UART_TRG_96);
1182 serial_outp(info, UART_LCR, 0);
1185 if (info->state->type == PORT_16750) {
1186 /* Wake up UART */
1187 serial_outp(info, UART_IER, 0);
1190 if (info->state->type == PORT_16C950) {
1191 /* Wake up and initialize UART */
1192 info->ACR = 0;
1193 serial_outp(info, UART_LCR, 0xBF);
1194 serial_outp(info, UART_EFR, UART_EFR_ECB);
1195 serial_outp(info, UART_IER, 0);
1196 serial_outp(info, UART_LCR, 0);
1197 serial_icr_write(info, UART_CSR, 0); /* Reset the UART */
1198 serial_outp(info, UART_LCR, 0xBF);
1199 serial_outp(info, UART_EFR, UART_EFR_ECB);
1200 serial_outp(info, UART_LCR, 0);
1204 * Clear the FIFO buffers and disable them
1205 * (they will be reenabled in change_speed())
1207 if (uart_config[state->type].flags & UART_CLEAR_FIFO) {
1208 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
1209 serial_outp(info, UART_FCR, (UART_FCR_ENABLE_FIFO |
1210 UART_FCR_CLEAR_RCVR |
1211 UART_FCR_CLEAR_XMIT));
1212 serial_outp(info, UART_FCR, 0);
1216 * Clear the interrupt registers.
1218 (void) serial_inp(info, UART_LSR);
1219 (void) serial_inp(info, UART_RX);
1220 (void) serial_inp(info, UART_IIR);
1221 (void) serial_inp(info, UART_MSR);
1224 * At this point there's no way the LSR could still be 0xFF;
1225 * if it is, then bail out, because there's likely no UART
1226 * here.
1228 if (serial_inp(info, UART_LSR) == 0xff) {
1229 printk("LSR safety check engaged!\n");
1230 if (serial_isroot()) {
1231 if (info->tty)
1232 set_bit(TTY_IO_ERROR, &info->tty->flags);
1233 } else
1234 retval = -ENODEV;
1235 goto errout;
1239 * Allocate the IRQ if necessary
1241 if (state->irq && (!IRQ_ports[state->irq] ||
1242 !IRQ_ports[state->irq]->next_port)) {
1243 if (IRQ_ports[state->irq]) {
1244 #ifdef CONFIG_SERIAL_SHARE_IRQ
1245 free_irq(state->irq, NULL);
1246 #ifdef CONFIG_SERIAL_MULTIPORT
1247 if (rs_multiport[state->irq].port1)
1248 handler = rs_interrupt_multi;
1249 else
1250 #endif
1251 handler = rs_interrupt;
1252 #else
1253 retval = -EBUSY;
1254 goto errout;
1255 #endif /* CONFIG_SERIAL_SHARE_IRQ */
1256 } else
1257 handler = rs_interrupt_single;
1259 retval = request_irq(state->irq, handler, IRQ_T(state),
1260 "serial", NULL);
1261 if (retval) {
1262 if (serial_isroot()) {
1263 if (info->tty)
1264 set_bit(TTY_IO_ERROR,
1265 &info->tty->flags);
1266 retval = 0;
1268 goto errout;
1273 * Insert serial port into IRQ chain.
1275 info->prev_port = 0;
1276 info->next_port = IRQ_ports[state->irq];
1277 if (info->next_port)
1278 info->next_port->prev_port = info;
1279 IRQ_ports[state->irq] = info;
1280 figure_IRQ_timeout(state->irq);
1283 * Now, initialize the UART
1285 serial_outp(info, UART_LCR, UART_LCR_WLEN8); /* reset DLAB */
1287 info->MCR = 0;
1288 if (info->tty->termios->c_cflag & CBAUD)
1289 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
1290 #ifdef CONFIG_SERIAL_MANY_PORTS
1291 if (info->flags & ASYNC_FOURPORT) {
1292 if (state->irq == 0)
1293 info->MCR |= UART_MCR_OUT1;
1294 } else
1295 #endif
1297 if (state->irq != 0)
1298 info->MCR |= UART_MCR_OUT2;
1300 info->MCR |= ALPHA_KLUDGE_MCR; /* Don't ask */
1301 serial_outp(info, UART_MCR, info->MCR);
1304 * Finally, enable interrupts
1306 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
1307 serial_outp(info, UART_IER, info->IER); /* enable interrupts */
1309 #ifdef CONFIG_SERIAL_MANY_PORTS
1310 if (info->flags & ASYNC_FOURPORT) {
1311 /* Enable interrupts on the AST Fourport board */
1312 ICP = (info->port & 0xFE0) | 0x01F;
1313 outb_p(0x80, ICP);
1314 (void) inb_p(ICP);
1316 #endif
1319 * And clear the interrupt registers again for luck.
1321 (void)serial_inp(info, UART_LSR);
1322 (void)serial_inp(info, UART_RX);
1323 (void)serial_inp(info, UART_IIR);
1324 (void)serial_inp(info, UART_MSR);
1326 if (info->tty)
1327 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1328 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1331 * Set up serial timers...
1333 timer_table[RS_TIMER].expires = jiffies + 2*HZ/100;
1334 timer_active |= 1 << RS_TIMER;
1337 * Set up the tty->alt_speed kludge
1339 #if (LINUX_VERSION_CODE >= 131394) /* Linux 2.1.66 */
1340 if (info->tty) {
1341 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1342 info->tty->alt_speed = 57600;
1343 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1344 info->tty->alt_speed = 115200;
1345 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1346 info->tty->alt_speed = 230400;
1347 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1348 info->tty->alt_speed = 460800;
1350 #endif
1353 * and set the speed of the serial port
1355 change_speed(info, 0);
1357 info->flags |= ASYNC_INITIALIZED;
1358 restore_flags(flags);
1359 return 0;
1361 errout:
1362 restore_flags(flags);
1363 return retval;
1367 * This routine will shutdown a serial port; interrupts are disabled, and
1368 * DTR is dropped if the hangup on close termio flag is on.
1370 static void shutdown(struct async_struct * info)
1372 unsigned long flags;
1373 struct serial_state *state;
1374 int retval;
1376 if (!(info->flags & ASYNC_INITIALIZED))
1377 return;
1379 state = info->state;
1381 #ifdef SERIAL_DEBUG_OPEN
1382 printk("Shutting down serial port %d (irq %d)....", info->line,
1383 state->irq);
1384 #endif
1386 save_flags(flags); cli(); /* Disable interrupts */
1389 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1390 * here so the queue might never be waken up
1392 wake_up_interruptible(&info->delta_msr_wait);
1395 * First unlink the serial port from the IRQ chain...
1397 if (info->next_port)
1398 info->next_port->prev_port = info->prev_port;
1399 if (info->prev_port)
1400 info->prev_port->next_port = info->next_port;
1401 else
1402 IRQ_ports[state->irq] = info->next_port;
1403 figure_IRQ_timeout(state->irq);
1406 * Free the IRQ, if necessary
1408 if (state->irq && (!IRQ_ports[state->irq] ||
1409 !IRQ_ports[state->irq]->next_port)) {
1410 if (IRQ_ports[state->irq]) {
1411 free_irq(state->irq, NULL);
1412 retval = request_irq(state->irq, rs_interrupt_single,
1413 IRQ_T(state), "serial", NULL);
1415 if (retval)
1416 printk("serial shutdown: request_irq: error %d"
1417 " Couldn't reacquire IRQ.\n", retval);
1418 } else
1419 free_irq(state->irq, NULL);
1422 if (info->xmit_buf) {
1423 free_page((unsigned long) info->xmit_buf);
1424 info->xmit_buf = 0;
1427 info->IER = 0;
1428 serial_outp(info, UART_IER, 0x00); /* disable all intrs */
1429 #ifdef CONFIG_SERIAL_MANY_PORTS
1430 if (info->flags & ASYNC_FOURPORT) {
1431 /* reset interrupts on the AST Fourport board */
1432 (void) inb((info->port & 0xFE0) | 0x01F);
1433 info->MCR |= UART_MCR_OUT1;
1434 } else
1435 #endif
1436 info->MCR &= ~UART_MCR_OUT2;
1437 info->MCR |= ALPHA_KLUDGE_MCR; /* Don't ask */
1439 /* disable break condition */
1440 serial_out(info, UART_LCR, serial_inp(info, UART_LCR) & ~UART_LCR_SBC);
1442 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
1443 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1444 serial_outp(info, UART_MCR, info->MCR);
1446 /* disable FIFO's */
1447 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
1448 UART_FCR_CLEAR_XMIT));
1449 (void)serial_in(info, UART_RX); /* read data port to reset things */
1451 if (info->tty)
1452 set_bit(TTY_IO_ERROR, &info->tty->flags);
1454 if (uart_config[info->state->type].flags & UART_STARTECH) {
1455 /* Arrange to enter sleep mode */
1456 serial_outp(info, UART_LCR, 0xBF);
1457 serial_outp(info, UART_EFR, UART_EFR_ECB);
1458 serial_outp(info, UART_IER, UART_IERX_SLEEP);
1459 serial_outp(info, UART_LCR, 0);
1461 if (info->state->type == PORT_16750) {
1462 /* Arrange to enter sleep mode */
1463 serial_outp(info, UART_IER, UART_IERX_SLEEP);
1465 info->flags &= ~ASYNC_INITIALIZED;
1466 restore_flags(flags);
1469 #if (LINUX_VERSION_CODE < 131394) /* Linux 2.1.66 */
1470 static int baud_table[] = {
1471 0, 50, 75, 110, 134, 150, 200, 300,
1472 600, 1200, 1800, 2400, 4800, 9600, 19200,
1473 38400, 57600, 115200, 230400, 460800, 0 };
1475 static int tty_get_baud_rate(struct tty_struct *tty)
1477 struct async_struct * info = (struct async_struct *)tty->driver_data;
1478 unsigned int cflag, i;
1480 cflag = tty->termios->c_cflag;
1482 i = cflag & CBAUD;
1483 if (i & CBAUDEX) {
1484 i &= ~CBAUDEX;
1485 if (i < 1 || i > 2)
1486 tty->termios->c_cflag &= ~CBAUDEX;
1487 else
1488 i += 15;
1490 if (i == 15) {
1491 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1492 i += 1;
1493 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1494 i += 2;
1496 return baud_table[i];
1498 #endif
1501 * This routine is called to set the UART divisor registers to match
1502 * the specified baud rate for a serial port.
1504 static void change_speed(struct async_struct *info,
1505 struct termios *old_termios)
1507 int quot = 0, baud_base, baud;
1508 unsigned cflag, cval, fcr = 0;
1509 int bits;
1510 unsigned long flags;
1512 if (!info->tty || !info->tty->termios)
1513 return;
1514 cflag = info->tty->termios->c_cflag;
1515 if (!CONFIGURED_SERIAL_PORT(info))
1516 return;
1518 /* byte size and parity */
1519 switch (cflag & CSIZE) {
1520 case CS5: cval = 0x00; bits = 7; break;
1521 case CS6: cval = 0x01; bits = 8; break;
1522 case CS7: cval = 0x02; bits = 9; break;
1523 case CS8: cval = 0x03; bits = 10; break;
1524 /* Never happens, but GCC is too dumb to figure it out */
1525 default: cval = 0x00; bits = 7; break;
1527 if (cflag & CSTOPB) {
1528 cval |= 0x04;
1529 bits++;
1531 if (cflag & PARENB) {
1532 cval |= UART_LCR_PARITY;
1533 bits++;
1535 if (!(cflag & PARODD))
1536 cval |= UART_LCR_EPAR;
1537 #ifdef CMSPAR
1538 if (cflag & CMSPAR)
1539 cval |= UART_LCR_SPAR;
1540 #endif
1542 /* Determine divisor based on baud rate */
1543 baud = tty_get_baud_rate(info->tty);
1544 if (!baud)
1545 baud = 9600; /* B0 transition handled in rs_set_termios */
1546 baud_base = info->state->baud_base;
1547 if (info->state->type == PORT_16C950) {
1548 if (baud <= baud_base)
1549 serial_icr_write(info, UART_TCR, 0);
1550 else if (baud <= 2*baud_base) {
1551 serial_icr_write(info, UART_TCR, 0x8);
1552 baud_base = baud_base * 2;
1553 } else if (baud <= 4*baud_base) {
1554 serial_icr_write(info, UART_TCR, 0x4);
1555 baud_base = baud_base * 4;
1556 } else
1557 serial_icr_write(info, UART_TCR, 0);
1559 if (baud == 38400 &&
1560 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1561 quot = info->state->custom_divisor;
1562 else {
1563 if (baud == 134)
1564 /* Special case since 134 is really 134.5 */
1565 quot = (2*baud_base / 269);
1566 else if (baud)
1567 quot = baud_base / baud;
1569 /* If the quotient is zero refuse the change */
1570 if (!quot && old_termios) {
1571 info->tty->termios->c_cflag &= ~CBAUD;
1572 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
1573 baud = tty_get_baud_rate(info->tty);
1574 if (!baud)
1575 baud = 9600;
1576 if (baud == 38400 &&
1577 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1578 quot = info->state->custom_divisor;
1579 else {
1580 if (baud == 134)
1581 /* Special case since 134 is really 134.5 */
1582 quot = (2*baud_base / 269);
1583 else if (baud)
1584 quot = baud_base / baud;
1587 /* As a last resort, if the quotient is zero, default to 9600 bps */
1588 if (!quot)
1589 quot = baud_base / 9600;
1590 info->quot = quot;
1591 info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
1592 info->timeout += HZ/50; /* Add .02 seconds of slop */
1594 /* Set up FIFO's */
1595 if (uart_config[info->state->type].flags & UART_USE_FIFO) {
1596 if ((info->state->baud_base / quot) < 2400)
1597 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
1598 else
1599 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
1601 if (info->state->type == PORT_16750)
1602 fcr |= UART_FCR7_64BYTE;
1604 /* CTS flow control flag and modem status interrupts */
1605 info->IER &= ~UART_IER_MSI;
1606 if (info->flags & ASYNC_HARDPPS_CD)
1607 info->IER |= UART_IER_MSI;
1608 if (cflag & CRTSCTS) {
1609 info->flags |= ASYNC_CTS_FLOW;
1610 info->IER |= UART_IER_MSI;
1611 } else
1612 info->flags &= ~ASYNC_CTS_FLOW;
1613 if (cflag & CLOCAL)
1614 info->flags &= ~ASYNC_CHECK_CD;
1615 else {
1616 info->flags |= ASYNC_CHECK_CD;
1617 info->IER |= UART_IER_MSI;
1619 serial_out(info, UART_IER, info->IER);
1622 * Set up parity check flag
1624 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1626 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1627 if (I_INPCK(info->tty))
1628 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1629 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1630 info->read_status_mask |= UART_LSR_BI;
1633 * Characters to ignore
1635 info->ignore_status_mask = 0;
1636 if (I_IGNPAR(info->tty))
1637 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1638 if (I_IGNBRK(info->tty)) {
1639 info->ignore_status_mask |= UART_LSR_BI;
1641 * If we're ignore parity and break indicators, ignore
1642 * overruns too. (For real raw support).
1644 if (I_IGNPAR(info->tty))
1645 info->ignore_status_mask |= UART_LSR_OE;
1648 * !!! ignore all characters if CREAD is not set
1650 if ((cflag & CREAD) == 0)
1651 info->ignore_status_mask |= UART_LSR_DR;
1652 save_flags(flags); cli();
1653 if (uart_config[info->state->type].flags & UART_STARTECH) {
1654 serial_outp(info, UART_LCR, 0xBF);
1655 serial_outp(info, UART_EFR,
1656 (cflag & CRTSCTS) ? UART_EFR_CTS : 0);
1658 serial_outp(info, UART_LCR, cval | UART_LCR_DLAB); /* set DLAB */
1659 serial_outp(info, UART_DLL, quot & 0xff); /* LS of divisor */
1660 serial_outp(info, UART_DLM, quot >> 8); /* MS of divisor */
1661 if (info->state->type == PORT_16750)
1662 serial_outp(info, UART_FCR, fcr); /* set fcr */
1663 serial_outp(info, UART_LCR, cval); /* reset DLAB */
1664 info->LCR = cval; /* Save LCR */
1665 if (info->state->type != PORT_16750)
1666 serial_outp(info, UART_FCR, fcr); /* set fcr */
1667 restore_flags(flags);
1670 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
1672 struct async_struct *info = (struct async_struct *)tty->driver_data;
1673 unsigned long flags;
1675 if (serial_paranoia_check(info, tty->device, "rs_put_char"))
1676 return;
1678 if (!tty || !info->xmit_buf)
1679 return;
1681 save_flags(flags); cli();
1682 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
1683 restore_flags(flags);
1684 return;
1687 info->xmit_buf[info->xmit_head++] = ch;
1688 info->xmit_head &= SERIAL_XMIT_SIZE-1;
1689 info->xmit_cnt++;
1690 restore_flags(flags);
1693 static void rs_flush_chars(struct tty_struct *tty)
1695 struct async_struct *info = (struct async_struct *)tty->driver_data;
1696 unsigned long flags;
1698 if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
1699 return;
1701 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1702 !info->xmit_buf)
1703 return;
1705 save_flags(flags); cli();
1706 info->IER |= UART_IER_THRI;
1707 serial_out(info, UART_IER, info->IER);
1708 restore_flags(flags);
1711 static int rs_write(struct tty_struct * tty, int from_user,
1712 const unsigned char *buf, int count)
1714 int c, ret = 0;
1715 struct async_struct *info = (struct async_struct *)tty->driver_data;
1716 unsigned long flags;
1718 if (serial_paranoia_check(info, tty->device, "rs_write"))
1719 return 0;
1721 if (!tty || !info->xmit_buf || !tmp_buf)
1722 return 0;
1724 save_flags(flags);
1725 if (from_user) {
1726 down(&tmp_buf_sem);
1727 while (1) {
1728 c = MIN(count,
1729 MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1730 SERIAL_XMIT_SIZE - info->xmit_head));
1731 if (c <= 0)
1732 break;
1734 c -= copy_from_user(tmp_buf, buf, c);
1735 if (!c) {
1736 if (!ret)
1737 ret = -EFAULT;
1738 break;
1740 cli();
1741 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1742 SERIAL_XMIT_SIZE - info->xmit_head));
1743 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1744 info->xmit_head = ((info->xmit_head + c) &
1745 (SERIAL_XMIT_SIZE-1));
1746 info->xmit_cnt += c;
1747 restore_flags(flags);
1748 buf += c;
1749 count -= c;
1750 ret += c;
1752 up(&tmp_buf_sem);
1753 } else {
1754 while (1) {
1755 cli();
1756 c = MIN(count,
1757 MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1758 SERIAL_XMIT_SIZE - info->xmit_head));
1759 if (c <= 0) {
1760 restore_flags(flags);
1761 break;
1763 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1764 info->xmit_head = ((info->xmit_head + c) &
1765 (SERIAL_XMIT_SIZE-1));
1766 info->xmit_cnt += c;
1767 restore_flags(flags);
1768 buf += c;
1769 count -= c;
1770 ret += c;
1773 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1774 !(info->IER & UART_IER_THRI)) {
1775 info->IER |= UART_IER_THRI;
1776 serial_out(info, UART_IER, info->IER);
1778 return ret;
1781 static int rs_write_room(struct tty_struct *tty)
1783 struct async_struct *info = (struct async_struct *)tty->driver_data;
1784 int ret;
1786 if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1787 return 0;
1788 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1789 if (ret < 0)
1790 ret = 0;
1791 return ret;
1794 static int rs_chars_in_buffer(struct tty_struct *tty)
1796 struct async_struct *info = (struct async_struct *)tty->driver_data;
1798 if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1799 return 0;
1800 return info->xmit_cnt;
1803 static void rs_flush_buffer(struct tty_struct *tty)
1805 struct async_struct *info = (struct async_struct *)tty->driver_data;
1806 unsigned long flags;
1808 if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1809 return;
1810 save_flags(flags); cli();
1811 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1812 restore_flags(flags);
1813 wake_up_interruptible(&tty->write_wait);
1814 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1815 tty->ldisc.write_wakeup)
1816 (tty->ldisc.write_wakeup)(tty);
1820 * This function is used to send a high-priority XON/XOFF character to
1821 * the device
1823 static void rs_send_xchar(struct tty_struct *tty, char ch)
1825 struct async_struct *info = (struct async_struct *)tty->driver_data;
1827 if (serial_paranoia_check(info, tty->device, "rs_send_char"))
1828 return;
1830 info->x_char = ch;
1831 if (ch) {
1832 /* Make sure transmit interrupts are on */
1833 info->IER |= UART_IER_THRI;
1834 serial_out(info, UART_IER, info->IER);
1839 * ------------------------------------------------------------
1840 * rs_throttle()
1842 * This routine is called by the upper-layer tty layer to signal that
1843 * incoming characters should be throttled.
1844 * ------------------------------------------------------------
1846 static void rs_throttle(struct tty_struct * tty)
1848 struct async_struct *info = (struct async_struct *)tty->driver_data;
1849 unsigned long flags;
1850 #ifdef SERIAL_DEBUG_THROTTLE
1851 char buf[64];
1853 printk("throttle %s: %d....\n", tty_name(tty, buf),
1854 tty->ldisc.chars_in_buffer(tty));
1855 #endif
1857 if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1858 return;
1860 if (I_IXOFF(tty))
1861 rs_send_xchar(tty, STOP_CHAR(tty));
1863 if (tty->termios->c_cflag & CRTSCTS)
1864 info->MCR &= ~UART_MCR_RTS;
1866 save_flags(flags); cli();
1867 serial_out(info, UART_MCR, info->MCR);
1868 restore_flags(flags);
1871 static void rs_unthrottle(struct tty_struct * tty)
1873 struct async_struct *info = (struct async_struct *)tty->driver_data;
1874 unsigned long flags;
1875 #ifdef SERIAL_DEBUG_THROTTLE
1876 char buf[64];
1878 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1879 tty->ldisc.chars_in_buffer(tty));
1880 #endif
1882 if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1883 return;
1885 if (I_IXOFF(tty)) {
1886 if (info->x_char)
1887 info->x_char = 0;
1888 else
1889 rs_send_xchar(tty, START_CHAR(tty));
1891 if (tty->termios->c_cflag & CRTSCTS)
1892 info->MCR |= UART_MCR_RTS;
1893 save_flags(flags); cli();
1894 serial_out(info, UART_MCR, info->MCR);
1895 restore_flags(flags);
1899 * ------------------------------------------------------------
1900 * rs_ioctl() and friends
1901 * ------------------------------------------------------------
1904 static int get_serial_info(struct async_struct * info,
1905 struct serial_struct * retinfo)
1907 struct serial_struct tmp;
1908 struct serial_state *state = info->state;
1910 if (!retinfo)
1911 return -EFAULT;
1912 memset(&tmp, 0, sizeof(tmp));
1913 tmp.type = state->type;
1914 tmp.line = state->line;
1915 tmp.port = state->port;
1916 tmp.irq = state->irq;
1917 tmp.flags = state->flags;
1918 tmp.xmit_fifo_size = state->xmit_fifo_size;
1919 tmp.baud_base = state->baud_base;
1920 tmp.close_delay = state->close_delay;
1921 tmp.closing_wait = state->closing_wait;
1922 tmp.custom_divisor = state->custom_divisor;
1923 tmp.hub6 = state->hub6;
1924 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1925 return -EFAULT;
1926 return 0;
1929 static int set_serial_info(struct async_struct * info,
1930 struct serial_struct * new_info)
1932 struct serial_struct new_serial;
1933 struct serial_state old_state, *state;
1934 unsigned int i,change_irq,change_port;
1935 int retval = 0;
1937 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1938 return -EFAULT;
1939 state = info->state;
1940 old_state = *state;
1942 change_irq = new_serial.irq != state->irq;
1943 change_port = (new_serial.port != state->port) ||
1944 (new_serial.hub6 != state->hub6);
1946 if (!serial_isroot()) {
1947 if (change_irq || change_port ||
1948 (new_serial.baud_base != state->baud_base) ||
1949 (new_serial.type != state->type) ||
1950 (new_serial.close_delay != state->close_delay) ||
1951 (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1952 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1953 (state->flags & ~ASYNC_USR_MASK)))
1954 return -EPERM;
1955 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
1956 (new_serial.flags & ASYNC_USR_MASK));
1957 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1958 (new_serial.flags & ASYNC_USR_MASK));
1959 state->custom_divisor = new_serial.custom_divisor;
1960 goto check_and_exit;
1963 new_serial.irq = irq_cannonicalize(new_serial.irq);
1965 if ((new_serial.irq >= NR_IRQS) || (new_serial.port > 0xffff) ||
1966 (new_serial.baud_base < 9600)|| (new_serial.type < PORT_UNKNOWN) ||
1967 (new_serial.type > PORT_MAX) || (new_serial.type == PORT_CIRRUS) ||
1968 (new_serial.type == PORT_STARTECH)) {
1969 return -EINVAL;
1972 if ((new_serial.type != state->type) ||
1973 (new_serial.xmit_fifo_size <= 0))
1974 new_serial.xmit_fifo_size =
1975 uart_config[state->type].dfl_xmit_fifo_size;
1977 /* Make sure address is not already in use */
1978 if (new_serial.type) {
1979 for (i = 0 ; i < NR_PORTS; i++)
1980 if ((state != &rs_table[i]) &&
1981 (rs_table[i].port == new_serial.port) &&
1982 rs_table[i].type)
1983 return -EADDRINUSE;
1986 if ((change_port || change_irq) && (state->count > 1))
1987 return -EBUSY;
1990 * OK, past this point, all the error checking has been done.
1991 * At this point, we start making changes.....
1994 state->baud_base = new_serial.baud_base;
1995 state->flags = ((state->flags & ~ASYNC_FLAGS) |
1996 (new_serial.flags & ASYNC_FLAGS));
1997 info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
1998 (info->flags & ASYNC_INTERNAL_FLAGS));
1999 state->custom_divisor = new_serial.custom_divisor;
2000 state->type = new_serial.type;
2001 state->close_delay = new_serial.close_delay * HZ/100;
2002 state->closing_wait = new_serial.closing_wait * HZ/100;
2003 #if (LINUX_VERSION_CODE > 0x200100)
2004 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2005 #endif
2006 info->xmit_fifo_size = state->xmit_fifo_size =
2007 new_serial.xmit_fifo_size;
2009 if (state->type != PORT_UNKNOWN && state->port)
2010 release_region(state->port,8);
2011 if (change_port || change_irq) {
2013 * We need to shutdown the serial port at the old
2014 * port/irq combination.
2016 shutdown(info);
2017 state->irq = new_serial.irq;
2018 info->port = state->port = new_serial.port;
2019 info->hub6 = state->hub6 = new_serial.hub6;
2021 if ((state->type != PORT_UNKNOWN) && state->port)
2022 request_region(state->port,8,"serial(set)");
2025 check_and_exit:
2026 if (!state->port || !state->type)
2027 return 0;
2028 if (info->flags & ASYNC_INITIALIZED) {
2029 if (((old_state.flags & ASYNC_SPD_MASK) !=
2030 (state->flags & ASYNC_SPD_MASK)) ||
2031 (old_state.custom_divisor != state->custom_divisor)) {
2032 #if (LINUX_VERSION_CODE >= 131394) /* Linux 2.1.66 */
2033 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
2034 info->tty->alt_speed = 57600;
2035 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
2036 info->tty->alt_speed = 115200;
2037 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
2038 info->tty->alt_speed = 230400;
2039 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
2040 info->tty->alt_speed = 460800;
2041 #endif
2042 change_speed(info, 0);
2044 } else
2045 retval = startup(info);
2046 return retval;
2051 * get_lsr_info - get line status register info
2053 * Purpose: Let user call ioctl() to get info when the UART physically
2054 * is emptied. On bus types like RS485, the transmitter must
2055 * release the bus after transmitting. This must be done when
2056 * the transmit shift register is empty, not be done when the
2057 * transmit holding register is empty. This functionality
2058 * allows an RS485 driver to be written in user space.
2060 static int get_lsr_info(struct async_struct * info, unsigned int *value)
2062 unsigned char status;
2063 unsigned int result;
2064 unsigned long flags;
2066 save_flags(flags); cli();
2067 status = serial_in(info, UART_LSR);
2068 restore_flags(flags);
2069 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
2070 if (copy_to_user(value, &result, sizeof(int)))
2071 return -EFAULT;
2072 return 0;
2076 static int get_modem_info(struct async_struct * info, unsigned int *value)
2078 unsigned char control, status;
2079 unsigned int result;
2080 unsigned long flags;
2082 control = info->MCR;
2083 save_flags(flags); cli();
2084 status = serial_in(info, UART_MSR);
2085 restore_flags(flags);
2086 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
2087 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
2088 #ifdef TIOCM_OUT1
2089 | ((control & UART_MCR_OUT1) ? TIOCM_OUT1 : 0)
2090 | ((control & UART_MCR_OUT2) ? TIOCM_OUT2 : 0)
2091 #endif
2092 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
2093 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
2094 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
2095 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
2097 if (copy_to_user(value, &result, sizeof(int)))
2098 return -EFAULT;
2099 return 0;
2102 static int set_modem_info(struct async_struct * info, unsigned int cmd,
2103 unsigned int *value)
2105 unsigned int arg;
2106 unsigned long flags;
2108 if (copy_from_user(&arg, value, sizeof(int)))
2109 return -EFAULT;
2111 switch (cmd) {
2112 case TIOCMBIS:
2113 if (arg & TIOCM_RTS)
2114 info->MCR |= UART_MCR_RTS;
2115 if (arg & TIOCM_DTR)
2116 info->MCR |= UART_MCR_DTR;
2117 #ifdef TIOCM_OUT1
2118 if (arg & TIOCM_OUT1)
2119 info->MCR |= UART_MCR_OUT1;
2120 if (arg & TIOCM_OUT2)
2121 info->MCR |= UART_MCR_OUT2;
2122 #endif
2123 break;
2124 case TIOCMBIC:
2125 if (arg & TIOCM_RTS)
2126 info->MCR &= ~UART_MCR_RTS;
2127 if (arg & TIOCM_DTR)
2128 info->MCR &= ~UART_MCR_DTR;
2129 #ifdef TIOCM_OUT1
2130 if (arg & TIOCM_OUT1)
2131 info->MCR &= ~UART_MCR_OUT1;
2132 if (arg & TIOCM_OUT2)
2133 info->MCR &= ~UART_MCR_OUT2;
2134 #endif
2135 break;
2136 case TIOCMSET:
2137 info->MCR = ((info->MCR & ~(UART_MCR_RTS |
2138 #ifdef TIOCM_OUT1
2139 UART_MCR_OUT1 |
2140 UART_MCR_OUT2 |
2141 #endif
2142 UART_MCR_DTR))
2143 | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
2144 #ifdef TIOCM_OUT1
2145 | ((arg & TIOCM_OUT1) ? UART_MCR_OUT1 : 0)
2146 | ((arg & TIOCM_OUT2) ? UART_MCR_OUT2 : 0)
2147 #endif
2148 | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
2149 break;
2150 default:
2151 return -EINVAL;
2153 save_flags(flags); cli();
2154 info->MCR |= ALPHA_KLUDGE_MCR; /* Don't ask */
2155 serial_out(info, UART_MCR, info->MCR);
2156 restore_flags(flags);
2157 return 0;
2160 static int do_autoconfig(struct async_struct * info)
2162 int retval;
2164 if (!serial_isroot())
2165 return -EPERM;
2167 if (info->state->count > 1)
2168 return -EBUSY;
2170 shutdown(info);
2172 autoconfig(info->state);
2173 if ((info->state->flags & ASYNC_AUTO_IRQ) &&
2174 (info->state->port != 0) &&
2175 (info->state->type != PORT_UNKNOWN))
2176 info->state->irq = detect_uart_irq(info->state);
2178 retval = startup(info);
2179 if (retval)
2180 return retval;
2181 return 0;
2185 * rs_break() --- routine which turns the break handling on or off
2187 #if (LINUX_VERSION_CODE < 131394) /* Linux 2.1.66 */
2188 static void send_break( struct async_struct * info, int duration)
2190 if (!CONFIGURED_SERIAL_PORT(info))
2191 return;
2192 current->state = TASK_INTERRUPTIBLE;
2193 current->timeout = jiffies + duration;
2194 cli();
2195 info->LCR |= UART_LCR_SBC;
2196 serial_out(info, UART_LCR, info->LCR);
2197 schedule();
2198 info->LCR &= ~UART_LCR_SBC;
2199 serial_out(info, UART_LCR, info->LCR);
2200 sti();
2202 #else
2203 static void rs_break(struct tty_struct *tty, int break_state)
2205 struct async_struct * info = (struct async_struct *)tty->driver_data;
2206 unsigned long flags;
2208 if (serial_paranoia_check(info, tty->device, "rs_break"))
2209 return;
2211 if (!CONFIGURED_SERIAL_PORT(info))
2212 return;
2213 save_flags(flags); cli();
2214 if (break_state == -1)
2215 info->LCR |= UART_LCR_SBC;
2216 else
2217 info->LCR &= ~UART_LCR_SBC;
2218 serial_out(info, UART_LCR, info->LCR);
2219 restore_flags(flags);
2221 #endif
2223 #ifdef CONFIG_SERIAL_MULTIPORT
2224 static int get_multiport_struct(struct async_struct * info,
2225 struct serial_multiport_struct *retinfo)
2227 struct serial_multiport_struct ret;
2228 struct rs_multiport_struct *multi;
2230 multi = &rs_multiport[info->state->irq];
2232 ret.port_monitor = multi->port_monitor;
2234 ret.port1 = multi->port1;
2235 ret.mask1 = multi->mask1;
2236 ret.match1 = multi->match1;
2238 ret.port2 = multi->port2;
2239 ret.mask2 = multi->mask2;
2240 ret.match2 = multi->match2;
2242 ret.port3 = multi->port3;
2243 ret.mask3 = multi->mask3;
2244 ret.match3 = multi->match3;
2246 ret.port4 = multi->port4;
2247 ret.mask4 = multi->mask4;
2248 ret.match4 = multi->match4;
2250 ret.irq = info->state->irq;
2252 if (copy_to_user(retinfo,&ret,sizeof(*retinfo)))
2253 return -EFAULT;
2254 return 0;
2257 static int set_multiport_struct(struct async_struct * info,
2258 struct serial_multiport_struct *in_multi)
2260 struct serial_multiport_struct new_multi;
2261 struct rs_multiport_struct *multi;
2262 struct serial_state *state;
2263 int was_multi, now_multi;
2264 int retval;
2265 void (*handler)(int, void *, struct pt_regs *);
2267 if (!serial_isroot())
2268 return -EPERM;
2269 state = info->state;
2271 if (copy_from_user(&new_multi, in_multi,
2272 sizeof(struct serial_multiport_struct)))
2273 return -EFAULT;
2275 if (new_multi.irq != state->irq || state->irq == 0 ||
2276 !IRQ_ports[state->irq])
2277 return -EINVAL;
2279 multi = &rs_multiport[state->irq];
2280 was_multi = (multi->port1 != 0);
2282 multi->port_monitor = new_multi.port_monitor;
2284 if (multi->port1)
2285 release_region(multi->port1,1);
2286 multi->port1 = new_multi.port1;
2287 multi->mask1 = new_multi.mask1;
2288 multi->match1 = new_multi.match1;
2289 if (multi->port1)
2290 request_region(multi->port1,1,"serial(multiport1)");
2292 if (multi->port2)
2293 release_region(multi->port2,1);
2294 multi->port2 = new_multi.port2;
2295 multi->mask2 = new_multi.mask2;
2296 multi->match2 = new_multi.match2;
2297 if (multi->port2)
2298 request_region(multi->port2,1,"serial(multiport2)");
2300 if (multi->port3)
2301 release_region(multi->port3,1);
2302 multi->port3 = new_multi.port3;
2303 multi->mask3 = new_multi.mask3;
2304 multi->match3 = new_multi.match3;
2305 if (multi->port3)
2306 request_region(multi->port3,1,"serial(multiport3)");
2308 if (multi->port4)
2309 release_region(multi->port4,1);
2310 multi->port4 = new_multi.port4;
2311 multi->mask4 = new_multi.mask4;
2312 multi->match4 = new_multi.match4;
2313 if (multi->port4)
2314 request_region(multi->port4,1,"serial(multiport4)");
2316 now_multi = (multi->port1 != 0);
2318 if (IRQ_ports[state->irq]->next_port &&
2319 (was_multi != now_multi)) {
2320 free_irq(state->irq, NULL);
2321 if (now_multi)
2322 handler = rs_interrupt_multi;
2323 else
2324 handler = rs_interrupt;
2326 retval = request_irq(state->irq, handler, IRQ_T(state),
2327 "serial", NULL);
2328 if (retval) {
2329 printk("Couldn't reallocate serial interrupt "
2330 "driver!!\n");
2333 return 0;
2335 #endif
2337 static int rs_ioctl(struct tty_struct *tty, struct file * file,
2338 unsigned int cmd, unsigned long arg)
2340 struct async_struct * info = (struct async_struct *)tty->driver_data;
2341 struct async_icount cprev, cnow; /* kernel counter temps */
2342 struct serial_icounter_struct icount;
2343 unsigned long flags;
2344 #if (LINUX_VERSION_CODE < 131394) /* Linux 2.1.66 */
2345 int retval, tmp;
2346 #endif
2348 if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
2349 return -ENODEV;
2351 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2352 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
2353 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
2354 if (tty->flags & (1 << TTY_IO_ERROR))
2355 return -EIO;
2358 switch (cmd) {
2359 #if (LINUX_VERSION_CODE < 131394) /* Linux 2.1.66 */
2360 case TCSBRK: /* SVID version: non-zero arg --> no break */
2361 retval = tty_check_change(tty);
2362 if (retval)
2363 return retval;
2364 tty_wait_until_sent(tty, 0);
2365 if (signal_pending(current))
2366 return -EINTR;
2367 if (!arg) {
2368 send_break(info, HZ/4); /* 1/4 second */
2369 if (signal_pending(current))
2370 return -EINTR;
2372 return 0;
2373 case TCSBRKP: /* support for POSIX tcsendbreak() */
2374 retval = tty_check_change(tty);
2375 if (retval)
2376 return retval;
2377 tty_wait_until_sent(tty, 0);
2378 if (signal_pending(current))
2379 return -EINTR;
2380 send_break(info, arg ? arg*(HZ/10) : HZ/4);
2381 if (signal_pending(current))
2382 return -EINTR;
2383 return 0;
2384 case TIOCGSOFTCAR:
2385 tmp = C_CLOCAL(tty) ? 1 : 0;
2386 if (copy_to_user((void *)arg, &tmp, sizeof(int)))
2387 return -EFAULT;
2388 return 0;
2389 case TIOCSSOFTCAR:
2390 if (copy_from_user(&tmp, (void *)arg, sizeof(int)))
2391 return -EFAULT;
2393 tty->termios->c_cflag =
2394 ((tty->termios->c_cflag & ~CLOCAL) |
2395 (tmp ? CLOCAL : 0));
2396 return 0;
2397 #endif
2398 case TIOCMGET:
2399 return get_modem_info(info, (unsigned int *) arg);
2400 case TIOCMBIS:
2401 case TIOCMBIC:
2402 case TIOCMSET:
2403 return set_modem_info(info, cmd, (unsigned int *) arg);
2404 case TIOCGSERIAL:
2405 return get_serial_info(info,
2406 (struct serial_struct *) arg);
2407 case TIOCSSERIAL:
2408 return set_serial_info(info,
2409 (struct serial_struct *) arg);
2410 case TIOCSERCONFIG:
2411 return do_autoconfig(info);
2413 case TIOCSERGETLSR: /* Get line status register */
2414 return get_lsr_info(info, (unsigned int *) arg);
2416 case TIOCSERGSTRUCT:
2417 if (copy_to_user((struct async_struct *) arg,
2418 info, sizeof(struct async_struct)))
2419 return -EFAULT;
2420 return 0;
2422 #ifdef CONFIG_SERIAL_MULTIPORT
2423 case TIOCSERGETMULTI:
2424 return get_multiport_struct(info,
2425 (struct serial_multiport_struct *) arg);
2426 case TIOCSERSETMULTI:
2427 return set_multiport_struct(info,
2428 (struct serial_multiport_struct *) arg);
2429 #endif
2432 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
2433 * - mask passed in arg for lines of interest
2434 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
2435 * Caller should use TIOCGICOUNT to see which one it was
2437 case TIOCMIWAIT:
2438 save_flags(flags); cli();
2439 /* note the counters on entry */
2440 cprev = info->state->icount;
2441 restore_flags(flags);
2442 while (1) {
2443 interruptible_sleep_on(&info->delta_msr_wait);
2444 /* see if a signal did it */
2445 if (signal_pending(current))
2446 return -ERESTARTSYS;
2447 save_flags(flags); cli();
2448 cnow = info->state->icount; /* atomic copy */
2449 restore_flags(flags);
2450 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2451 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2452 return -EIO; /* no change => error */
2453 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2454 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2455 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2456 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
2457 return 0;
2459 cprev = cnow;
2461 /* NOTREACHED */
2464 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
2465 * Return: write counters to the user passed counter struct
2466 * NB: both 1->0 and 0->1 transitions are counted except for
2467 * RI where only 0->1 is counted.
2469 case TIOCGICOUNT:
2470 save_flags(flags); cli();
2471 cnow = info->state->icount;
2472 restore_flags(flags);
2473 icount.cts = cnow.cts;
2474 icount.dsr = cnow.dsr;
2475 icount.rng = cnow.rng;
2476 icount.dcd = cnow.dcd;
2477 icount.rx = cnow.rx;
2478 icount.tx = cnow.tx;
2479 icount.frame = cnow.frame;
2480 icount.overrun = cnow.overrun;
2481 icount.parity = cnow.parity;
2482 icount.brk = cnow.brk;
2483 icount.buf_overrun = cnow.buf_overrun;
2485 if (copy_to_user((void *)arg, &icount, sizeof(icount)))
2486 return -EFAULT;
2487 return 0;
2488 case TIOCSERGWILD:
2489 case TIOCSERSWILD:
2490 /* "setserial -W" is called in Debian boot */
2491 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
2492 return 0;
2494 default:
2495 return -ENOIOCTLCMD;
2497 return 0;
2500 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
2502 struct async_struct *info = (struct async_struct *)tty->driver_data;
2503 unsigned long flags;
2504 unsigned int cflag = tty->termios->c_cflag;
2506 if ( (cflag == old_termios->c_cflag)
2507 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
2508 == RELEVANT_IFLAG(old_termios->c_iflag)))
2509 return;
2511 change_speed(info, old_termios);
2513 /* Handle transition to B0 status */
2514 if ((old_termios->c_cflag & CBAUD) &&
2515 !(cflag & CBAUD)) {
2516 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
2517 save_flags(flags); cli();
2518 serial_out(info, UART_MCR, info->MCR);
2519 restore_flags(flags);
2522 /* Handle transition away from B0 status */
2523 if (!(old_termios->c_cflag & CBAUD) &&
2524 (cflag & CBAUD)) {
2525 info->MCR |= UART_MCR_DTR;
2526 if (!(tty->termios->c_cflag & CRTSCTS) ||
2527 !test_bit(TTY_THROTTLED, &tty->flags)) {
2528 info->MCR |= UART_MCR_RTS;
2530 save_flags(flags); cli();
2531 serial_out(info, UART_MCR, info->MCR);
2532 restore_flags(flags);
2535 /* Handle turning off CRTSCTS */
2536 if ((old_termios->c_cflag & CRTSCTS) &&
2537 !(tty->termios->c_cflag & CRTSCTS)) {
2538 tty->hw_stopped = 0;
2539 rs_start(tty);
2542 #if 0
2544 * No need to wake up processes in open wait, since they
2545 * sample the CLOCAL flag once, and don't recheck it.
2546 * XXX It's not clear whether the current behavior is correct
2547 * or not. Hence, this may change.....
2549 if (!(old_termios->c_cflag & CLOCAL) &&
2550 (tty->termios->c_cflag & CLOCAL))
2551 wake_up_interruptible(&info->open_wait);
2552 #endif
2556 * ------------------------------------------------------------
2557 * rs_close()
2559 * This routine is called when the serial port gets closed. First, we
2560 * wait for the last remaining data to be sent. Then, we unlink its
2561 * async structure from the interrupt chain if necessary, and we free
2562 * that IRQ if nothing is left in the chain.
2563 * ------------------------------------------------------------
2565 static void rs_close(struct tty_struct *tty, struct file * filp)
2567 struct async_struct * info = (struct async_struct *)tty->driver_data;
2568 struct serial_state *state;
2569 unsigned long flags;
2571 if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
2572 return;
2574 state = info->state;
2576 save_flags(flags); cli();
2578 if (tty_hung_up_p(filp)) {
2579 DBG_CNT("before DEC-hung");
2580 MOD_DEC_USE_COUNT;
2581 restore_flags(flags);
2582 return;
2585 #ifdef SERIAL_DEBUG_OPEN
2586 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
2587 #endif
2588 if ((tty->count == 1) && (state->count != 1)) {
2590 * Uh, oh. tty->count is 1, which means that the tty
2591 * structure will be freed. state->count should always
2592 * be one in these conditions. If it's greater than
2593 * one, we've got real problems, since it means the
2594 * serial port won't be shutdown.
2596 printk("rs_close: bad serial port count; tty->count is 1, "
2597 "state->count is %d\n", state->count);
2598 state->count = 1;
2600 if (--state->count < 0) {
2601 printk("rs_close: bad serial port count for ttys%d: %d\n",
2602 info->line, state->count);
2603 state->count = 0;
2605 if (state->count) {
2606 DBG_CNT("before DEC-2");
2607 MOD_DEC_USE_COUNT;
2608 restore_flags(flags);
2609 return;
2611 info->flags |= ASYNC_CLOSING;
2613 * Save the termios structure, since this port may have
2614 * separate termios for callout and dialin.
2616 if (info->flags & ASYNC_NORMAL_ACTIVE)
2617 info->state->normal_termios = *tty->termios;
2618 if (info->flags & ASYNC_CALLOUT_ACTIVE)
2619 info->state->callout_termios = *tty->termios;
2621 * Now we wait for the transmit buffer to clear; and we notify
2622 * the line discipline to only process XON/XOFF characters.
2624 tty->closing = 1;
2625 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
2626 tty_wait_until_sent(tty, info->closing_wait);
2628 * At this point we stop accepting input. To do this, we
2629 * disable the receive line status interrupts, and tell the
2630 * interrupt driver to stop checking the data ready bit in the
2631 * line status register.
2633 info->IER &= ~UART_IER_RLSI;
2634 info->read_status_mask &= ~UART_LSR_DR;
2635 if (info->flags & ASYNC_INITIALIZED) {
2636 serial_out(info, UART_IER, info->IER);
2638 * Before we drop DTR, make sure the UART transmitter
2639 * has completely drained; this is especially
2640 * important if there is a transmit FIFO!
2642 rs_wait_until_sent(tty, info->timeout);
2644 shutdown(info);
2645 if (tty->driver.flush_buffer)
2646 tty->driver.flush_buffer(tty);
2647 if (tty->ldisc.flush_buffer)
2648 tty->ldisc.flush_buffer(tty);
2649 tty->closing = 0;
2650 info->event = 0;
2651 info->tty = 0;
2652 if (info->blocked_open) {
2653 if (info->close_delay) {
2654 current->state = TASK_INTERRUPTIBLE;
2655 schedule_timeout(info->close_delay);
2657 wake_up_interruptible(&info->open_wait);
2659 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
2660 ASYNC_CLOSING);
2661 wake_up_interruptible(&info->close_wait);
2662 MOD_DEC_USE_COUNT;
2663 restore_flags(flags);
2667 * rs_wait_until_sent() --- wait until the transmitter is empty
2669 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
2671 struct async_struct * info = (struct async_struct *)tty->driver_data;
2672 unsigned long orig_jiffies, char_time;
2673 int lsr;
2675 if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
2676 return;
2678 if (info->state->type == PORT_UNKNOWN)
2679 return;
2681 if (info->xmit_fifo_size == 0)
2682 return; /* Just in case.... */
2684 orig_jiffies = jiffies;
2686 * Set the check interval to be 1/5 of the estimated time to
2687 * send a single character, and make it at least 1. The check
2688 * interval should also be less than the timeout.
2690 * Note: we have to use pretty tight timings here to satisfy
2691 * the NIST-PCTS.
2693 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
2694 char_time = char_time / 5;
2695 if (char_time == 0)
2696 char_time = 1;
2697 if (timeout)
2698 char_time = MIN(char_time, timeout);
2700 * If the transmitter hasn't cleared in twice the approximate
2701 * amount of time to send the entire FIFO, it probably won't
2702 * ever clear. This assumes the UART isn't doing flow
2703 * control, which is currently the case. Hence, if it ever
2704 * takes longer than info->timeout, this is probably due to a
2705 * UART bug of some kind. So, we clamp the timeout parameter at
2706 * 2*info->timeout.
2708 if (!timeout || timeout > 2*info->timeout)
2709 timeout = 2*info->timeout;
2710 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2711 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
2712 printk("jiff=%lu...", jiffies);
2713 #endif
2714 while (!((lsr = serial_inp(info, UART_LSR)) & UART_LSR_TEMT)) {
2715 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2716 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2717 #endif
2718 current->state = TASK_INTERRUPTIBLE;
2719 schedule_timeout(char_time);
2720 if (signal_pending(current))
2721 break;
2722 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2723 break;
2725 current->state = TASK_RUNNING;
2726 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2727 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2728 #endif
2732 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
2734 static void rs_hangup(struct tty_struct *tty)
2736 struct async_struct * info = (struct async_struct *)tty->driver_data;
2737 struct serial_state *state = info->state;
2739 if (serial_paranoia_check(info, tty->device, "rs_hangup"))
2740 return;
2742 state = info->state;
2744 rs_flush_buffer(tty);
2745 shutdown(info);
2746 info->event = 0;
2747 state->count = 0;
2748 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
2749 info->tty = 0;
2750 wake_up_interruptible(&info->open_wait);
2754 * ------------------------------------------------------------
2755 * rs_open() and friends
2756 * ------------------------------------------------------------
2758 static int block_til_ready(struct tty_struct *tty, struct file * filp,
2759 struct async_struct *info)
2761 #ifdef DECLARE_WAITQUEUE
2762 DECLARE_WAITQUEUE(wait, current);
2763 #else
2764 struct wait_queue wait = { current, NULL };
2765 #endif
2766 struct serial_state *state = info->state;
2767 int retval;
2768 int do_clocal = 0, extra_count = 0;
2769 unsigned long flags;
2772 * If the device is in the middle of being closed, then block
2773 * until it's done, and then try again.
2775 if (tty_hung_up_p(filp) ||
2776 (info->flags & ASYNC_CLOSING)) {
2777 if (info->flags & ASYNC_CLOSING)
2778 interruptible_sleep_on(&info->close_wait);
2779 #ifdef SERIAL_DO_RESTART
2780 return ((info->flags & ASYNC_HUP_NOTIFY) ?
2781 -EAGAIN : -ERESTARTSYS);
2782 #else
2783 return -EAGAIN;
2784 #endif
2788 * If this is a callout device, then just make sure the normal
2789 * device isn't being used.
2791 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
2792 if (info->flags & ASYNC_NORMAL_ACTIVE)
2793 return -EBUSY;
2794 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2795 (info->flags & ASYNC_SESSION_LOCKOUT) &&
2796 (info->session != current->session))
2797 return -EBUSY;
2798 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2799 (info->flags & ASYNC_PGRP_LOCKOUT) &&
2800 (info->pgrp != current->pgrp))
2801 return -EBUSY;
2802 info->flags |= ASYNC_CALLOUT_ACTIVE;
2803 return 0;
2807 * If non-blocking mode is set, or the port is not enabled,
2808 * then make the check up front and then exit.
2810 if ((filp->f_flags & O_NONBLOCK) ||
2811 (tty->flags & (1 << TTY_IO_ERROR))) {
2812 if (info->flags & ASYNC_CALLOUT_ACTIVE)
2813 return -EBUSY;
2814 info->flags |= ASYNC_NORMAL_ACTIVE;
2815 return 0;
2818 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
2819 if (state->normal_termios.c_cflag & CLOCAL)
2820 do_clocal = 1;
2821 } else {
2822 if (tty->termios->c_cflag & CLOCAL)
2823 do_clocal = 1;
2827 * Block waiting for the carrier detect and the line to become
2828 * free (i.e., not in use by the callout). While we are in
2829 * this loop, state->count is dropped by one, so that
2830 * rs_close() knows when to free things. We restore it upon
2831 * exit, either normal or abnormal.
2833 retval = 0;
2834 add_wait_queue(&info->open_wait, &wait);
2835 #ifdef SERIAL_DEBUG_OPEN
2836 printk("block_til_ready before block: ttys%d, count = %d\n",
2837 state->line, state->count);
2838 #endif
2839 save_flags(flags); cli();
2840 if (!tty_hung_up_p(filp)) {
2841 extra_count = 1;
2842 state->count--;
2844 restore_flags(flags);
2845 info->blocked_open++;
2846 while (1) {
2847 save_flags(flags); cli();
2848 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2849 (tty->termios->c_cflag & CBAUD))
2850 serial_out(info, UART_MCR,
2851 serial_inp(info, UART_MCR) |
2852 (UART_MCR_DTR | UART_MCR_RTS));
2853 restore_flags(flags);
2854 set_current_state(TASK_INTERRUPTIBLE);
2855 if (tty_hung_up_p(filp) ||
2856 !(info->flags & ASYNC_INITIALIZED)) {
2857 #ifdef SERIAL_DO_RESTART
2858 if (info->flags & ASYNC_HUP_NOTIFY)
2859 retval = -EAGAIN;
2860 else
2861 retval = -ERESTARTSYS;
2862 #else
2863 retval = -EAGAIN;
2864 #endif
2865 break;
2867 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2868 !(info->flags & ASYNC_CLOSING) &&
2869 (do_clocal || (serial_in(info, UART_MSR) &
2870 UART_MSR_DCD)))
2871 break;
2872 if (signal_pending(current)) {
2873 retval = -ERESTARTSYS;
2874 break;
2876 #ifdef SERIAL_DEBUG_OPEN
2877 printk("block_til_ready blocking: ttys%d, count = %d\n",
2878 info->line, state->count);
2879 #endif
2880 schedule();
2882 current->state = TASK_RUNNING;
2883 remove_wait_queue(&info->open_wait, &wait);
2884 if (extra_count)
2885 state->count++;
2886 info->blocked_open--;
2887 #ifdef SERIAL_DEBUG_OPEN
2888 printk("block_til_ready after blocking: ttys%d, count = %d\n",
2889 info->line, state->count);
2890 #endif
2891 if (retval)
2892 return retval;
2893 info->flags |= ASYNC_NORMAL_ACTIVE;
2894 return 0;
2897 static int get_async_struct(int line, struct async_struct **ret_info)
2899 struct async_struct *info;
2900 struct serial_state *sstate;
2902 sstate = rs_table + line;
2903 sstate->count++;
2904 if (sstate->info) {
2905 *ret_info = sstate->info;
2906 return 0;
2908 info = kmalloc(sizeof(struct async_struct), GFP_KERNEL);
2909 if (!info) {
2910 sstate->count--;
2911 return -ENOMEM;
2913 memset(info, 0, sizeof(struct async_struct));
2914 #ifdef DECLARE_WAITQUEUE
2915 init_waitqueue_head(&info->open_wait);
2916 init_waitqueue_head(&info->close_wait);
2917 init_waitqueue_head(&info->delta_msr_wait);
2918 #endif
2919 info->magic = SERIAL_MAGIC;
2920 info->port = sstate->port;
2921 info->flags = sstate->flags;
2922 #ifdef CONFIG_SERIAL_PCI_MEMMAPPED
2923 info->iomem_base = sstate->iomem_base;
2924 info->iomem_reg_shift = sstate->iomem_reg_shift;
2925 #endif
2926 info->xmit_fifo_size = sstate->xmit_fifo_size;
2927 info->line = line;
2928 info->tqueue.routine = do_softint;
2929 info->tqueue.data = info;
2930 info->state = sstate;
2931 if (sstate->info) {
2932 kfree_s(info, sizeof(struct async_struct));
2933 *ret_info = sstate->info;
2934 return 0;
2936 *ret_info = sstate->info = info;
2937 return 0;
2941 * This routine is called whenever a serial port is opened. It
2942 * enables interrupts for a serial port, linking in its async structure into
2943 * the IRQ chain. It also performs the serial-specific
2944 * initialization for the tty structure.
2946 static int rs_open(struct tty_struct *tty, struct file * filp)
2948 struct async_struct *info;
2949 int retval, line;
2950 unsigned long page;
2952 MOD_INC_USE_COUNT;
2953 line = MINOR(tty->device) - tty->driver.minor_start;
2954 if ((line < 0) || (line >= NR_PORTS)) {
2955 MOD_DEC_USE_COUNT;
2956 return -ENODEV;
2958 retval = get_async_struct(line, &info);
2959 if (retval) {
2960 MOD_DEC_USE_COUNT;
2961 return retval;
2963 tty->driver_data = info;
2964 info->tty = tty;
2965 if (serial_paranoia_check(info, tty->device, "rs_open"))
2966 return -ENODEV;
2968 #ifdef SERIAL_DEBUG_OPEN
2969 printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
2970 info->state->count);
2971 #endif
2972 #if (LINUX_VERSION_CODE > 0x20100)
2973 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2974 #endif
2976 if (!tmp_buf) {
2977 page = get_free_page(GFP_KERNEL);
2978 if (!page) {
2979 return -ENOMEM;
2981 if (tmp_buf)
2982 free_page(page);
2983 else
2984 tmp_buf = (unsigned char *) page;
2988 * If the port is the middle of closing, bail out now
2990 if (tty_hung_up_p(filp) ||
2991 (info->flags & ASYNC_CLOSING)) {
2992 if (info->flags & ASYNC_CLOSING)
2993 interruptible_sleep_on(&info->close_wait);
2994 #ifdef SERIAL_DO_RESTART
2995 return ((info->flags & ASYNC_HUP_NOTIFY) ?
2996 -EAGAIN : -ERESTARTSYS);
2997 #else
2998 return -EAGAIN;
2999 #endif
3003 * Start up serial port
3005 retval = startup(info);
3006 if (retval) {
3007 return retval;
3010 retval = block_til_ready(tty, filp, info);
3011 if (retval) {
3012 #ifdef SERIAL_DEBUG_OPEN
3013 printk("rs_open returning after block_til_ready with %d\n",
3014 retval);
3015 #endif
3016 return retval;
3019 if ((info->state->count == 1) &&
3020 (info->flags & ASYNC_SPLIT_TERMIOS)) {
3021 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
3022 *tty->termios = info->state->normal_termios;
3023 else
3024 *tty->termios = info->state->callout_termios;
3025 change_speed(info, 0);
3027 #ifdef CONFIG_SERIAL_CONSOLE
3028 if (sercons.cflag && sercons.index == line) {
3029 tty->termios->c_cflag = sercons.cflag;
3030 sercons.cflag = 0;
3031 change_speed(info, 0);
3033 #endif
3034 info->session = current->session;
3035 info->pgrp = current->pgrp;
3037 #ifdef SERIAL_DEBUG_OPEN
3038 printk("rs_open ttys%d successful...", info->line);
3039 #endif
3040 return 0;
3044 * /proc fs routines....
3047 static inline int line_info(char *buf, struct serial_state *state)
3049 struct async_struct *info = state->info, scr_info;
3050 char stat_buf[30], control, status;
3051 int ret;
3052 unsigned long flags;
3054 ret = sprintf(buf, "%d: uart:%s port:%X irq:%d",
3055 state->line, uart_config[state->type].name,
3056 state->port, state->irq);
3058 if (!state->port || (state->type == PORT_UNKNOWN)) {
3059 ret += sprintf(buf+ret, "\n");
3060 return ret;
3064 * Figure out the current RS-232 lines
3066 if (!info) {
3067 info = &scr_info; /* This is just for serial_{in,out} */
3069 info->magic = SERIAL_MAGIC;
3070 info->port = state->port;
3071 info->flags = state->flags;
3072 info->quot = 0;
3073 info->tty = 0;
3075 save_flags(flags); cli();
3076 status = serial_in(info, UART_MSR);
3077 control = info ? info->MCR : serial_in(info, UART_MCR);
3078 restore_flags(flags);
3080 stat_buf[0] = 0;
3081 stat_buf[1] = 0;
3082 if (control & UART_MCR_RTS)
3083 strcat(stat_buf, "|RTS");
3084 if (status & UART_MSR_CTS)
3085 strcat(stat_buf, "|CTS");
3086 if (control & UART_MCR_DTR)
3087 strcat(stat_buf, "|DTR");
3088 if (status & UART_MSR_DSR)
3089 strcat(stat_buf, "|DSR");
3090 if (status & UART_MSR_DCD)
3091 strcat(stat_buf, "|CD");
3092 if (status & UART_MSR_RI)
3093 strcat(stat_buf, "|RI");
3095 if (info->quot) {
3096 ret += sprintf(buf+ret, " baud:%d",
3097 state->baud_base / info->quot);
3100 ret += sprintf(buf+ret, " tx:%d rx:%d",
3101 state->icount.tx, state->icount.rx);
3103 if (state->icount.frame)
3104 ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
3106 if (state->icount.parity)
3107 ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
3109 if (state->icount.brk)
3110 ret += sprintf(buf+ret, " brk:%d", state->icount.brk);
3112 if (state->icount.overrun)
3113 ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
3116 * Last thing is the RS-232 status lines
3118 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
3119 return ret;
3122 int rs_read_proc(char *page, char **start, off_t off, int count,
3123 int *eof, void *data)
3125 int i, len = 0, l;
3126 off_t begin = 0;
3128 len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
3129 for (i = 0; i < NR_PORTS && len < 4000; i++) {
3130 l = line_info(page + len, &rs_table[i]);
3131 len += l;
3132 if (len+begin > off+count)
3133 goto done;
3134 if (len+begin < off) {
3135 begin += len;
3136 len = 0;
3139 *eof = 1;
3140 done:
3141 if (off >= len+begin)
3142 return 0;
3143 *start = page + (begin-off);
3144 return ((count < begin+len-off) ? count : begin+len-off);
3148 * ---------------------------------------------------------------------
3149 * rs_init() and friends
3151 * rs_init() is called at boot-time to initialize the serial driver.
3152 * ---------------------------------------------------------------------
3156 * This routine prints out the appropriate serial driver version
3157 * number, and identifies which options were configured into this
3158 * driver.
3160 static _INLINE_ void show_serial_version(void)
3162 printk(KERN_INFO "%s version %s with", serial_name, serial_version);
3163 #ifdef CONFIG_HUB6
3164 printk(" HUB-6");
3165 #define SERIAL_OPT
3166 #endif
3167 #ifdef CONFIG_SERIAL_MANY_PORTS
3168 printk(" MANY_PORTS");
3169 #define SERIAL_OPT
3170 #endif
3171 #ifdef CONFIG_SERIAL_MULTIPORT
3172 printk(" MULTIPORT");
3173 #define SERIAL_OPT
3174 #endif
3175 #ifdef CONFIG_SERIAL_SHARE_IRQ
3176 printk(" SHARE_IRQ");
3177 #define SERIAL_OPT
3178 #endif
3179 #ifdef CONFIG_SERIAL_DETECT_IRQ
3180 printk(" DETECT_IRQ");
3181 #define SERIAL_OPT
3182 #endif
3183 #ifdef ENABLE_SERIAL_PCI
3184 printk(" SERIAL_PCI");
3185 #ifdef CONFIG_SERIAL_PCI_MEMMAPPED
3186 printk(" PCI_IOMEM");
3187 #endif
3188 #define SERIAL_OPT
3189 #endif
3190 #ifdef SERIAL_OPT
3191 printk(" enabled\n");
3192 #else
3193 printk(" no serial options enabled\n");
3194 #endif
3195 #undef SERIAL_OPT
3199 * This routine detect the IRQ of a serial port by clearing OUT2 when
3200 * no UART interrupt are requested (IER = 0) (*GPL*). This seems to work at
3201 * each time, as long as no other device permanently request the IRQ.
3202 * If no IRQ is detected, or multiple IRQ appear, this function returns 0.
3203 * The variable "state" and the field "state->port" should not be null.
3205 static unsigned detect_uart_irq (struct serial_state * state)
3207 int irq;
3208 unsigned long irqs;
3209 unsigned char save_mcr, save_ier;
3210 struct async_struct scr_info; /* serial_{in,out} because HUB6 */
3212 #ifdef CONFIG_SERIAL_MANY_PORTS
3213 unsigned char save_ICP=0; /* no warning */
3214 unsigned short ICP=0;
3216 if (state->flags & ASYNC_FOURPORT) {
3217 ICP = (state->port & 0xFE0) | 0x01F;
3218 save_ICP = inb_p(ICP);
3219 outb_p(0x80, ICP);
3220 (void) inb_p(ICP);
3222 #endif
3223 scr_info.magic = SERIAL_MAGIC;
3224 scr_info.port = state->port;
3225 scr_info.flags = state->flags;
3226 #ifdef CONFIG_HUB6
3227 scr_info.hub6 = state->hub6;
3228 #endif
3230 /* forget possible initially masked and pending IRQ */
3231 probe_irq_off(probe_irq_on());
3232 save_mcr = serial_inp(&scr_info, UART_MCR);
3233 save_ier = serial_inp(&scr_info, UART_IER);
3234 serial_outp(&scr_info, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
3236 irqs = probe_irq_on();
3237 serial_outp(&scr_info, UART_MCR, 0);
3238 udelay (10);
3239 if (state->flags & ASYNC_FOURPORT) {
3240 serial_outp(&scr_info, UART_MCR,
3241 UART_MCR_DTR | UART_MCR_RTS);
3242 } else {
3243 serial_outp(&scr_info, UART_MCR,
3244 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
3246 serial_outp(&scr_info, UART_IER, 0x0f); /* enable all intrs */
3247 (void)serial_inp(&scr_info, UART_LSR);
3248 (void)serial_inp(&scr_info, UART_RX);
3249 (void)serial_inp(&scr_info, UART_IIR);
3250 (void)serial_inp(&scr_info, UART_MSR);
3251 serial_outp(&scr_info, UART_TX, 0xFF);
3252 udelay (20);
3253 irq = probe_irq_off(irqs);
3255 serial_outp(&scr_info, UART_MCR, save_mcr);
3256 serial_outp(&scr_info, UART_IER, save_ier);
3257 #ifdef CONFIG_SERIAL_MANY_PORTS
3258 if (state->flags & ASYNC_FOURPORT)
3259 outb_p(save_ICP, ICP);
3260 #endif
3261 return (irq > 0)? irq : 0;
3265 * This is a quickie test to see how big the FIFO is.
3266 * It doesn't work at all the time, more's the pity.
3268 static int size_fifo(struct async_struct *info)
3270 unsigned char old_fcr, old_mcr, old_dll, old_dlm;
3271 int count;
3273 old_fcr = serial_inp(info, UART_FCR);
3274 old_mcr = serial_inp(info, UART_MCR);
3275 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO |
3276 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
3277 serial_outp(info, UART_MCR, UART_MCR_LOOP);
3278 serial_outp(info, UART_LCR, UART_LCR_DLAB);
3279 old_dll = serial_inp(info, UART_DLL);
3280 old_dlm = serial_inp(info, UART_DLM);
3281 serial_outp(info, UART_DLL, 0x01);
3282 serial_outp(info, UART_DLM, 0x00);
3283 serial_outp(info, UART_LCR, 0x03);
3284 for (count = 0; count < 256; count++)
3285 serial_outp(info, UART_TX, count);
3286 mdelay(20);
3287 for (count = 0; (serial_inp(info, UART_LSR) & UART_LSR_DR) &&
3288 (count < 256); count++)
3289 serial_inp(info, UART_RX);
3290 serial_outp(info, UART_FCR, old_fcr);
3291 serial_outp(info, UART_MCR, old_mcr);
3292 serial_outp(info, UART_LCR, UART_LCR_DLAB);
3293 serial_outp(info, UART_DLL, old_dll);
3294 serial_outp(info, UART_DLM, old_dlm);
3296 return count;
3300 * This is a helper routine to autodetect StarTech/Exar UART's. When
3301 * this function is called we know it is at least a StarTech 16650 V2,
3302 * but it might be one of several StarTech UARTs, or one of its
3303 * clones. (We treat the broken original StarTech 16650 V1 as a
3304 * 16550A, and why not? Startech doesn't seem to even acknowledge its
3305 * existence.)
3307 * What evil have men's minds wrought...
3309 static void autoconfig_startech_uarts(struct async_struct *info,
3310 struct serial_state *state,
3311 unsigned long flags)
3313 unsigned char scratch, status1, status2, old_fctr, old_emsr;
3316 * Here we check for the XR16C85x family. We do this by
3317 * checking for to see if we can replace the scratch register
3318 * with the receive FIFO count register.
3320 * XXX I don't have one of these chips, but it should also be
3321 * possible to check for them by setting DLL and DLM to 0, and
3322 * then reading back DLL and DLM. If the DLM reads back as
3323 * 0x10, then the UART is a XR16C850 and the DLL contains the
3324 * chip revision.
3326 old_fctr = serial_inp(info, UART_FCTR);
3327 serial_outp(info, UART_FCTR, old_fctr | UART_FCTR_SCR_SWAP);
3328 old_emsr = serial_inp(info, UART_EMSR);
3329 serial_outp(info, UART_EMSR, 0x00);
3330 serial_outp(info, UART_LCR, 0x00);
3331 scratch = serial_in(info, UART_SCR);
3332 serial_outp(info, UART_SCR, 0xa5);
3333 status1 = serial_in(info, UART_SCR);
3334 serial_outp(info, UART_SCR, 0x5a);
3335 status2 = serial_in(info, UART_SCR);
3336 serial_outp(info, UART_SCR, scratch);
3337 if ((status1 != 0xa5) || (status2 != 0x5a)) {
3338 serial_outp(info, UART_LCR, 0xBF);
3339 serial_outp(info, UART_FCTR, old_fctr | UART_FCTR_SCR_SWAP);
3340 serial_outp(info, UART_EMSR, old_emsr);
3341 serial_outp(info, UART_FCTR, old_fctr);
3342 state->type = PORT_16850;
3343 return;
3345 serial_outp(info, UART_IER, old_fctr);
3348 * We distinguish between the '654 and the '650 by counting
3349 * how many bytes are in the FIFO. I'm using this for now,
3350 * since that's the technique that was sent to me in the
3351 * serial driver update, but I'm not convinced this works.
3352 * I've had problems doing this in the past. -TYT
3354 if (size_fifo(info) == 64)
3355 state->type = PORT_16654;
3356 else
3357 state->type = PORT_16650V2;
3361 * This routine is called by rs_init() to initialize a specific serial
3362 * port. It determines what type of UART chip this serial port is
3363 * using: 8250, 16450, 16550, 16550A. The important question is
3364 * whether or not this UART is a 16550A or not, since this will
3365 * determine whether or not we can use its FIFO features or not.
3367 static void autoconfig(struct serial_state * state)
3369 unsigned char status1, status2, scratch, scratch2, scratch3;
3370 struct async_struct *info, scr_info;
3371 unsigned long flags;
3373 state->type = PORT_UNKNOWN;
3375 if (!CONFIGURED_SERIAL_PORT(state))
3376 return;
3378 info = &scr_info; /* This is just for serial_{in,out} */
3380 info->magic = SERIAL_MAGIC;
3381 info->state = state;
3382 info->port = state->port;
3383 info->flags = state->flags;
3384 #ifdef CONFIG_HUB6
3385 info->hub6 = state->hub6;
3386 #endif
3387 #ifdef CONFIG_SERIAL_PCI_MEMMAPPED
3388 info->iomem_base = state->iomem_base;
3389 info->iomem_reg_shift = state->iomem_reg_shift;
3390 #endif
3392 save_flags(flags); cli();
3394 if (!state->iomem_base) {
3396 * Do a simple existence test first; if we fail this,
3397 * there's no point trying anything else.
3399 * 0x80 is used as a nonsense port to prevent against
3400 * false positives due to ISA bus float. The
3401 * assumption is that 0x80 is a non-existent port;
3402 * which should be safe since include/asm/io.h also
3403 * makes this assumption.
3405 scratch = serial_inp(info, UART_IER);
3406 serial_outp(info, UART_IER, 0);
3407 outb(0xff, 0x080);
3408 scratch2 = serial_inp(info, UART_IER);
3409 serial_outp(info, UART_IER, scratch);
3410 if (scratch2) {
3411 restore_flags(flags);
3412 return; /* We failed; there's nothing here */
3417 * Check to see if a UART is really there. Certain broken
3418 * internal modems based on the Rockwell chipset fail this
3419 * test, because they apparently don't implement the loopback
3420 * test mode. So this test is skipped on the COM 1 through
3421 * COM 4 ports. This *should* be safe, since no board
3422 * manufacturer would be stupid enough to design a board
3423 * that conflicts with COM 1-4 --- we hope!
3425 if (!(state->flags & ASYNC_SKIP_TEST)) {
3426 scratch = serial_inp(info, UART_MCR);
3427 serial_outp(info, UART_MCR, UART_MCR_LOOP | scratch);
3428 serial_outp(info, UART_MCR, UART_MCR_LOOP | 0x0A);
3429 status1 = serial_inp(info, UART_MSR) & 0xF0;
3430 serial_outp(info, UART_MCR, scratch);
3431 if (status1 != 0x90) {
3432 restore_flags(flags);
3433 return;
3437 scratch2 = serial_in(info, UART_LCR);
3438 serial_outp(info, UART_LCR, 0xBF); /* set up for StarTech test */
3439 serial_outp(info, UART_EFR, 0); /* EFR is the same as FCR */
3440 serial_outp(info, UART_LCR, 0);
3441 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
3442 scratch = serial_in(info, UART_IIR) >> 6;
3443 switch (scratch) {
3444 case 0:
3445 state->type = PORT_16450;
3446 break;
3447 case 1:
3448 state->type = PORT_UNKNOWN;
3449 break;
3450 case 2:
3451 state->type = PORT_16550;
3452 break;
3453 case 3:
3454 state->type = PORT_16550A;
3455 break;
3457 if (state->type == PORT_16550A) {
3458 /* Check for Oxford Semiconductor 16C950 */
3459 scratch = serial_icr_read(info, UART_ID1);
3460 scratch2 = serial_icr_read(info, UART_ID2);
3461 scratch3 = serial_icr_read(info, UART_ID3);
3463 if (scratch == 0x16 && scratch2 == 0xC9 &&
3464 (scratch3 == 0x50 || scratch3 == 0x52 ||
3465 scratch3 == 0x54)) {
3466 state->type = PORT_16C950;
3467 state->revision = serial_icr_read(info, UART_REV);
3470 if (state->type == PORT_16550A) {
3471 /* Check for Startech UART's */
3472 serial_outp(info, UART_LCR, UART_LCR_DLAB);
3473 if (serial_in(info, UART_EFR) == 0) {
3474 state->type = PORT_16650;
3475 } else {
3476 serial_outp(info, UART_LCR, 0xBF);
3477 if (serial_in(info, UART_EFR) == 0)
3478 autoconfig_startech_uarts(info, state, flags);
3481 if (state->type == PORT_16550A) {
3482 /* Check for TI 16750 */
3483 serial_outp(info, UART_LCR, scratch2 | UART_LCR_DLAB);
3484 serial_outp(info, UART_FCR,
3485 UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
3486 scratch = serial_in(info, UART_IIR) >> 5;
3487 if (scratch == 7) {
3489 * If this is a 16750, and not a cheap UART
3490 * clone, then it should only go into 64 byte
3491 * mode if the UART_FCR7_64BYTE bit was set
3492 * while UART_LCR_DLAB was latched.
3494 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
3495 serial_outp(info, UART_LCR, 0);
3496 serial_outp(info, UART_FCR,
3497 UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
3498 scratch = serial_in(info, UART_IIR) >> 5;
3499 if (scratch == 6)
3500 state->type = PORT_16750;
3502 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
3504 serial_outp(info, UART_LCR, scratch2);
3505 if (state->type == PORT_16450) {
3506 scratch = serial_in(info, UART_SCR);
3507 serial_outp(info, UART_SCR, 0xa5);
3508 status1 = serial_in(info, UART_SCR);
3509 serial_outp(info, UART_SCR, 0x5a);
3510 status2 = serial_in(info, UART_SCR);
3511 serial_outp(info, UART_SCR, scratch);
3513 if ((status1 != 0xa5) || (status2 != 0x5a))
3514 state->type = PORT_8250;
3516 state->xmit_fifo_size = uart_config[state->type].dfl_xmit_fifo_size;
3518 if (state->type == PORT_UNKNOWN) {
3519 restore_flags(flags);
3520 return;
3523 if (info->port)
3524 request_region(info->port,8,"serial(auto)");
3527 * Reset the UART.
3529 serial_outp(info, UART_MCR, 0x00 | ALPHA_KLUDGE_MCR); /* Don't ask */
3530 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
3531 UART_FCR_CLEAR_XMIT));
3532 (void)serial_in(info, UART_RX);
3533 serial_outp(info, UART_IER, 0);
3535 restore_flags(flags);
3538 int register_serial(struct serial_struct *req);
3539 void unregister_serial(int line);
3541 #if (LINUX_VERSION_CODE > 0x20100)
3542 EXPORT_SYMBOL(register_serial);
3543 EXPORT_SYMBOL(unregister_serial);
3544 #else
3545 static struct symbol_table serial_syms = {
3546 #include <linux/symtab_begin.h>
3547 X(register_serial),
3548 X(unregister_serial),
3549 #include <linux/symtab_end.h>
3551 #endif
3553 #ifdef ENABLE_SERIAL_PCI
3555 * Some PCI serial cards using the PLX 9050 PCI interface chip require
3556 * that the card interrupt be explicitly enabled or disabled. This
3557 * seems to be mainly needed on card using the PLX which also use I/O
3558 * mapped memory.
3560 * Note that __init is a no-op if MODULE is defined; we depend on this.
3562 static void __init pci_plx9050_fn(struct pci_dev *dev,
3563 struct pci_board *board,
3564 int enable)
3566 u8 data, *p;
3568 pci_read_config_byte(dev, PCI_COMMAND, &data);
3570 if (enable)
3571 pci_write_config_byte(dev, PCI_COMMAND,
3572 data | PCI_COMMAND_MEMORY);
3574 /* enable/disable interrupts */
3575 p = ioremap(PCI_BASE_ADDRESS(dev, 0), 0x80);
3576 writel(enable ? 0x41 : 0x00, p + 0x4c);
3577 iounmap(p);
3579 if (!enable)
3580 pci_write_config_byte(dev, PCI_COMMAND,
3581 data & ~PCI_COMMAND_MEMORY);
3585 * This is the configuration table for all of the PCI serial boards
3586 * which we support.
3588 static struct pci_board pci_boards[] = {
3590 * Vendor ID, Device ID,
3591 * Subvendor ID, Subdevice ID,
3592 * Number of Ports, Base (Maximum) Baud Rate,
3593 * Offset of register holding Uart register offset
3594 * Mask to apply to above register's value
3596 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
3597 PCI_SUBVENDOR_ID_CONNECT_TECH,
3598 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_232,
3599 SPCI_FL_BASE1, 8, 1382400 },
3600 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
3601 PCI_SUBVENDOR_ID_CONNECT_TECH,
3602 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_232,
3603 SPCI_FL_BASE1, 4, 1382400 },
3604 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
3605 PCI_SUBVENDOR_ID_CONNECT_TECH,
3606 PCI_SUBDEVICE_ID_CONNECT_TECH_BH2_232,
3607 SPCI_FL_BASE1, 2, 1382400 },
3608 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960V2,
3609 PCI_SUBVENDOR_ID_CONNECT_TECH,
3610 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_232,
3611 SPCI_FL_BASE1, 8, 1382400 },
3612 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960V2,
3613 PCI_SUBVENDOR_ID_CONNECT_TECH,
3614 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_232,
3615 SPCI_FL_BASE1, 4, 1382400 },
3616 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960V2,
3617 PCI_SUBVENDOR_ID_CONNECT_TECH,
3618 PCI_SUBDEVICE_ID_CONNECT_TECH_BH2_232,
3619 SPCI_FL_BASE1, 2, 1382400 },
3620 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
3621 PCI_SUBVENDOR_ID_CONNECT_TECH,
3622 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_485,
3623 SPCI_FL_BASE1, 8, 921600 },
3624 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
3625 PCI_SUBVENDOR_ID_CONNECT_TECH,
3626 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_485_4_4,
3627 SPCI_FL_BASE1, 8, 921600 },
3628 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
3629 PCI_SUBVENDOR_ID_CONNECT_TECH,
3630 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_485,
3631 SPCI_FL_BASE1, 4, 921600 },
3632 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
3633 PCI_SUBVENDOR_ID_CONNECT_TECH,
3634 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_485_2_2,
3635 SPCI_FL_BASE1, 4, 921600 },
3636 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960,
3637 PCI_SUBVENDOR_ID_CONNECT_TECH,
3638 PCI_SUBDEVICE_ID_CONNECT_TECH_BH2_485,
3639 SPCI_FL_BASE1, 2, 921600 },
3640 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960V2,
3641 PCI_SUBVENDOR_ID_CONNECT_TECH,
3642 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_485,
3643 SPCI_FL_BASE1, 8, 921600 },
3644 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960V2,
3645 PCI_SUBVENDOR_ID_CONNECT_TECH,
3646 PCI_SUBDEVICE_ID_CONNECT_TECH_BH8_485_4_4,
3647 SPCI_FL_BASE1, 8, 921600 },
3648 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960V2,
3649 PCI_SUBVENDOR_ID_CONNECT_TECH,
3650 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_485,
3651 SPCI_FL_BASE1, 4, 921600 },
3652 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960V2,
3653 PCI_SUBVENDOR_ID_CONNECT_TECH,
3654 PCI_SUBDEVICE_ID_CONNECT_TECH_BH4_485_2_2,
3655 SPCI_FL_BASE1, 4, 921600 },
3656 { PCI_VENDOR_ID_V3, PCI_DEVICE_ID_V3_V960V2,
3657 PCI_SUBVENDOR_ID_CONNECT_TECH,
3658 PCI_SUBDEVICE_ID_CONNECT_TECH_BH2_485,
3659 SPCI_FL_BASE1, 2, 921600 },
3660 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_U530,
3661 PCI_ANY_ID, PCI_ANY_ID,
3662 SPCI_FL_BASE2 | SPCI_FL_BASE_TABLE, 1, 115200 },
3663 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_UCOMM2,
3664 PCI_ANY_ID, PCI_ANY_ID,
3665 SPCI_FL_BASE2 | SPCI_FL_BASE_TABLE, 2, 115200 },
3666 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_UCOMM422,
3667 PCI_ANY_ID, PCI_ANY_ID,
3668 SPCI_FL_BASE2 | SPCI_FL_BASE_TABLE, 4, 115200 },
3669 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_UCOMM232,
3670 PCI_ANY_ID, PCI_ANY_ID,
3671 SPCI_FL_BASE2 | SPCI_FL_BASE_TABLE, 2, 115200 },
3672 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_COMM4,
3673 PCI_ANY_ID, PCI_ANY_ID,
3674 SPCI_FL_BASE2 | SPCI_FL_BASE_TABLE, 4, 115200 },
3675 { PCI_VENDOR_ID_SEALEVEL, PCI_DEVICE_ID_SEALEVEL_COMM8,
3676 PCI_ANY_ID, PCI_ANY_ID,
3677 SPCI_FL_BASE2 | SPCI_FL_BASE_TABLE, 8, 115200 },
3678 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_GTEK_SERIAL2,
3679 PCI_ANY_ID, PCI_ANY_ID,
3680 SPCI_FL_BASE2 | SPCI_FL_BASE_TABLE, 2, 115200 },
3681 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_SPCOM200,
3682 PCI_ANY_ID, PCI_ANY_ID,
3683 SPCI_FL_BASE2 | SPCI_FL_BASE_TABLE, 2, 921600 },
3684 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050,
3685 PCI_SUBVENDOR_ID_KEYSPAN,
3686 PCI_SUBDEVICE_ID_KEYSPAN_SX2,
3687 SPCI_FL_BASE2 | SPCI_FL_IOMEM, 2, 921600,
3688 0x400, 7, pci_plx9050_fn },
3689 { PCI_VENDOR_ID_PANACOM, PCI_DEVICE_ID_PANACOM_QUADMODEM,
3690 PCI_ANY_ID, PCI_ANY_ID,
3691 SPCI_FL_BASE2 | SPCI_FL_IOMEM, 4, 921600,
3692 0x400, 7, pci_plx9050_fn },
3693 { PCI_VENDOR_ID_PANACOM, PCI_DEVICE_ID_PANACOM_DUALMODEM,
3694 PCI_ANY_ID, PCI_ANY_ID,
3695 SPCI_FL_BASE2 | SPCI_FL_IOMEM, 2, 921600,
3696 0x400, 7, pci_plx9050_fn },
3697 { 0, }
3701 * Query PCI space for known serial boards
3702 * If found, add them to the PCI device space in rs_table[]
3704 * Accept a maximum of eight boards
3707 static void probe_serial_pci(void)
3709 u16 subvendor, subdevice;
3710 int k, line;
3711 struct pci_dev *dev = NULL;
3712 struct pci_board *board;
3713 struct serial_struct fake_state;
3714 int uart_offset, base_baud, base_idx;
3715 unsigned long port;
3717 #ifdef SERIAL_DEBUG_PCI
3718 printk(KERN_DEBUG "Entered probe_serial_pci()\n");
3719 #endif
3721 if (!pcibios_present()) {
3722 #ifdef SERIAL_DEBUG_PCI
3723 printk(KERN_DEBUG "Leaving probe_serial_pci() (no pcibios)\n");
3724 #endif
3725 return;
3728 for(dev=pci_devices; dev; dev=dev->next) {
3729 pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID,
3730 &subvendor);
3731 pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &subdevice);
3733 for (board = pci_boards; board->vendor; board++) {
3734 if (board->vendor != (unsigned short) PCI_ANY_ID &&
3735 dev->vendor != board->vendor)
3736 continue;
3737 if (board->device != (unsigned short) PCI_ANY_ID &&
3738 dev->device != board->device)
3739 continue;
3740 if (board->subvendor != (unsigned short) PCI_ANY_ID &&
3741 subvendor != board->subvendor)
3742 continue;
3743 if (board->subdevice != (unsigned short) PCI_ANY_ID &&
3744 subdevice != board->subdevice)
3745 continue;
3746 break;
3749 if (board->vendor == 0) {
3750 #ifdef SERIAL_DEBUG_PCI
3751 printk(KERN_DEBUG
3752 "Found unknown serial board: %x:%x, %x:%x, %x\n",
3753 dev->vendor, dev->device, subvendor, subdevice,
3754 dev->class);
3755 printk(KERN_DEBUG
3756 " Addresses: %lx, %lx, %lx, %lx\n",
3757 PCI_BASE_ADDRESS(dev, 0), PCI_BASE_ADDRESS(dev, 1),
3758 PCI_BASE_ADDRESS(dev, 2), PCI_BASE_ADDRESS(dev, 3));
3759 #endif
3760 continue;
3764 * Run the initialization function, if any
3766 if (board->init_fn)
3767 (board->init_fn)(dev, board, 1);
3770 * Register the serial board in the array so we can
3771 * shutdown the board later, if necessary.
3773 serial_pci_board[serial_pci_board_idx].board = board;
3774 serial_pci_board[serial_pci_board_idx].dev = dev;
3775 serial_pci_board_idx++;
3777 base_idx = board->flags & SPCI_FL_BASE_MASK;
3778 port = PCI_BASE_ADDRESS(dev, base_idx);
3779 if (board->flags & SPCI_FL_IOMEM)
3780 port &= PCI_BASE_ADDRESS_MEM_MASK;
3781 else
3782 port &= PCI_BASE_ADDRESS_IO_MASK;
3785 * Set some defaults for the loop below, which
3786 * actually registers each serial port belonging to
3787 * the card.
3789 uart_offset = board->uart_offset;
3790 if (!uart_offset)
3791 uart_offset = 8;
3792 base_baud = board->base_baud;
3793 if (!base_baud)
3794 base_baud = BASE_BAUD;
3795 #ifndef CONFIG_SERIAL_PCI_MEMMAPPED
3796 if (board->flags & SPCI_FL_IOMEM) {
3797 #ifdef SERIAL_DEBUG_PCI
3798 printk(KERN_DEBUG
3799 "Can't support memory mapped PCI serial device\n");
3800 #endif
3801 continue;
3803 #endif
3804 memset(&fake_state, 0, sizeof(fake_state));
3806 #ifdef SERIAL_DEBUG_PCI
3807 printk(KERN_DEBUG
3808 "Found Serial PCI device: %x:%x, %x:%x, %x\n",
3809 dev->vendor, dev->device, subvendor, subdevice,
3810 dev->class);
3811 printk(KERN_DEBUG
3812 " IRQ: %d, base: %lx (%s), num_ports: %d\n",
3813 dev->irq, port, board->flags & SPCI_FL_IOMEM ?
3814 "iomem" : "port", board->num_ports);
3815 #endif
3817 for (k=0; k < board->num_ports; k++) {
3818 if (board->flags & SPCI_FL_BASE_TABLE) {
3819 port = PCI_BASE_ADDRESS(dev, base_idx++);
3820 if (board->flags & SPCI_FL_IOMEM)
3821 port &= PCI_BASE_ADDRESS_MEM_MASK;
3822 else
3823 port &= PCI_BASE_ADDRESS_IO_MASK;
3825 fake_state.irq = dev->irq;
3826 fake_state.port = port;
3827 #ifdef CONFIG_SERIAL_PCI_MEMMAPPED
3828 if (board->flags & SPCI_FL_IOMEM) {
3829 fake_state.iomem_base =
3830 ioremap(port, board->uart_offset);
3831 fake_state.iomem_reg_shift = board->reg_shift;
3832 fake_state.port = 0;
3834 #endif
3835 port += uart_offset;
3836 fake_state.flags = ASYNC_SKIP_TEST | ASYNC_SHARE_IRQ;
3837 line = register_serial(&fake_state);
3838 if (line < 0)
3839 break;
3840 rs_table[line].baud_base = base_baud;
3844 #ifdef SERIAL_DEBUG_PCI
3845 printk(KERN_DEBUG "Leaving probe_serial_pci() (probe finished)\n");
3846 #endif
3847 return;
3850 #endif /* ENABLE_SERIAL_PCI */
3853 * The serial driver boot-time initialization code!
3855 int __init rs_init(void)
3857 int i;
3858 struct serial_state * state;
3859 extern void atomwide_serial_init (void);
3860 extern void dualsp_serial_init (void);
3862 #ifdef CONFIG_ATOMWIDE_SERIAL
3863 atomwide_serial_init ();
3864 #endif
3865 #ifdef CONFIG_DUALSP_SERIAL
3866 dualsp_serial_init ();
3867 #endif
3869 init_bh(SERIAL_BH, do_serial_bh);
3870 timer_table[RS_TIMER].fn = rs_timer;
3871 timer_table[RS_TIMER].expires = 0;
3873 for (i = 0; i < NR_IRQS; i++) {
3874 IRQ_ports[i] = 0;
3875 IRQ_timeout[i] = 0;
3876 #ifdef CONFIG_SERIAL_MULTIPORT
3877 memset(&rs_multiport[i], 0,
3878 sizeof(struct rs_multiport_struct));
3879 #endif
3881 #ifdef CONFIG_SERIAL_CONSOLE
3883 * The interrupt of the serial console port
3884 * can't be shared.
3886 if (sercons.flags & CON_CONSDEV) {
3887 for(i = 0; i < NR_PORTS; i++)
3888 if (i != sercons.index &&
3889 rs_table[i].irq == rs_table[sercons.index].irq)
3890 rs_table[i].irq = 0;
3892 #endif
3893 show_serial_version();
3895 /* Initialize the tty_driver structure */
3897 memset(&serial_driver, 0, sizeof(struct tty_driver));
3898 serial_driver.magic = TTY_DRIVER_MAGIC;
3899 #if (LINUX_VERSION_CODE > 0x20100)
3900 serial_driver.driver_name = "serial";
3901 #endif
3902 serial_driver.name = "ttyS";
3903 serial_driver.major = TTY_MAJOR;
3904 serial_driver.minor_start = 64 + SERIAL_DEV_OFFSET;
3905 serial_driver.num = NR_PORTS;
3906 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
3907 serial_driver.subtype = SERIAL_TYPE_NORMAL;
3908 serial_driver.init_termios = tty_std_termios;
3909 serial_driver.init_termios.c_cflag =
3910 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
3911 serial_driver.flags = TTY_DRIVER_REAL_RAW;
3912 serial_driver.refcount = &serial_refcount;
3913 serial_driver.table = serial_table;
3914 serial_driver.termios = serial_termios;
3915 serial_driver.termios_locked = serial_termios_locked;
3917 serial_driver.open = rs_open;
3918 serial_driver.close = rs_close;
3919 serial_driver.write = rs_write;
3920 serial_driver.put_char = rs_put_char;
3921 serial_driver.flush_chars = rs_flush_chars;
3922 serial_driver.write_room = rs_write_room;
3923 serial_driver.chars_in_buffer = rs_chars_in_buffer;
3924 serial_driver.flush_buffer = rs_flush_buffer;
3925 serial_driver.ioctl = rs_ioctl;
3926 serial_driver.throttle = rs_throttle;
3927 serial_driver.unthrottle = rs_unthrottle;
3928 serial_driver.set_termios = rs_set_termios;
3929 serial_driver.stop = rs_stop;
3930 serial_driver.start = rs_start;
3931 serial_driver.hangup = rs_hangup;
3932 #if (LINUX_VERSION_CODE >= 131394) /* Linux 2.1.66 */
3933 serial_driver.break_ctl = rs_break;
3934 #endif
3935 #if (LINUX_VERSION_CODE >= 131343)
3936 serial_driver.send_xchar = rs_send_xchar;
3937 serial_driver.wait_until_sent = rs_wait_until_sent;
3938 serial_driver.read_proc = rs_read_proc;
3939 #endif
3942 * The callout device is just like normal device except for
3943 * major number and the subtype code.
3945 callout_driver = serial_driver;
3946 callout_driver.name = "cua";
3947 callout_driver.major = TTYAUX_MAJOR;
3948 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
3949 #if (LINUX_VERSION_CODE >= 131343)
3950 callout_driver.read_proc = 0;
3951 callout_driver.proc_entry = 0;
3952 #endif
3954 if (tty_register_driver(&serial_driver))
3955 panic("Couldn't register serial driver\n");
3956 if (tty_register_driver(&callout_driver))
3957 panic("Couldn't register callout driver\n");
3959 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
3960 state->magic = SSTATE_MAGIC;
3961 state->line = i;
3962 state->type = PORT_UNKNOWN;
3963 state->custom_divisor = 0;
3964 state->close_delay = 5*HZ/10;
3965 state->closing_wait = 30*HZ;
3966 state->callout_termios = callout_driver.init_termios;
3967 state->normal_termios = serial_driver.init_termios;
3968 state->icount.cts = state->icount.dsr =
3969 state->icount.rng = state->icount.dcd = 0;
3970 state->icount.rx = state->icount.tx = 0;
3971 state->icount.frame = state->icount.parity = 0;
3972 state->icount.overrun = state->icount.brk = 0;
3973 state->irq = irq_cannonicalize(state->irq);
3974 if (state->port && check_region(state->port,8))
3975 continue;
3976 if (state->flags & ASYNC_BOOT_AUTOCONF)
3977 autoconfig(state);
3979 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
3980 if (state->type == PORT_UNKNOWN)
3981 continue;
3982 if ( (state->flags & ASYNC_BOOT_AUTOCONF)
3983 && (state->flags & ASYNC_AUTO_IRQ)
3984 && (state->port != 0))
3985 state->irq = detect_uart_irq(state);
3986 printk(KERN_INFO "ttyS%02d%s at 0x%04x (irq = %d) is a %s\n",
3987 state->line + SERIAL_DEV_OFFSET,
3988 (state->flags & ASYNC_FOURPORT) ? " FourPort" : "",
3989 state->port, state->irq,
3990 uart_config[state->type].name);
3992 #ifdef ENABLE_SERIAL_PCI
3993 probe_serial_pci();
3994 #endif
3995 return 0;
3999 * register_serial and unregister_serial allows for serial ports to be
4000 * configured at run-time, to support PCMCIA modems.
4002 int register_serial(struct serial_struct *req)
4004 int i;
4005 unsigned long flags;
4006 struct serial_state *state;
4008 save_flags(flags);
4009 cli();
4010 for (i = 0; i < NR_PORTS; i++) {
4011 if ((rs_table[i].port == req->port) &&
4012 (rs_table[i].iomem_base == req->iomem_base))
4013 break;
4015 if (i == NR_PORTS) {
4016 for (i = 0; i < NR_PORTS; i++)
4017 if ((rs_table[i].type == PORT_UNKNOWN) &&
4018 (rs_table[i].count == 0))
4019 break;
4021 if (i == NR_PORTS) {
4022 restore_flags(flags);
4023 return -1;
4025 state = &rs_table[i];
4026 if (rs_table[i].count) {
4027 restore_flags(flags);
4028 printk("Couldn't configure serial #%d (port=%d,irq=%d): "
4029 "device already open\n", i, req->port, req->irq);
4030 return -1;
4032 state->irq = req->irq;
4033 state->port = req->port;
4034 state->flags = req->flags;
4035 #ifdef CONFIG_SERIAL_PCI_MEMMAPPED
4036 state->iomem_base = req->iomem_base;
4037 state->iomem_reg_shift = req->iomem_reg_shift;
4038 #endif
4040 autoconfig(state);
4041 if (state->type == PORT_UNKNOWN) {
4042 restore_flags(flags);
4043 printk("register_serial(): autoconfig failed\n");
4044 return -1;
4046 restore_flags(flags);
4048 if ((state->flags & ASYNC_AUTO_IRQ) && CONFIGURED_SERIAL_PORT(state))
4049 state->irq = detect_uart_irq(state);
4051 printk(KERN_INFO "ttyS%02d at %s 0x%04lx (irq = %d) is a %s\n",
4052 state->line + SERIAL_DEV_OFFSET,
4053 state->iomem_base ? "iomem" : "port",
4054 state->iomem_base ? (unsigned long)state->iomem_base :
4055 (unsigned long)state->port,
4056 state->irq, uart_config[state->type].name);
4057 return state->line + SERIAL_DEV_OFFSET;
4060 void unregister_serial(int line)
4062 unsigned long flags;
4063 struct serial_state *state = &rs_table[line];
4065 save_flags(flags);
4066 cli();
4067 if (state->info && state->info->tty)
4068 tty_hangup(state->info->tty);
4069 state->type = PORT_UNKNOWN;
4070 printk(KERN_INFO "tty%02d unloaded\n", state->line);
4071 restore_flags(flags);
4074 #ifdef MODULE
4075 int init_module(void)
4077 return rs_init();
4080 void cleanup_module(void)
4082 unsigned long flags;
4083 int e1, e2;
4084 int i;
4085 struct async_struct *info;
4087 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
4088 save_flags(flags);
4089 cli();
4090 timer_active &= ~(1 << RS_TIMER);
4091 timer_table[RS_TIMER].fn = NULL;
4092 timer_table[RS_TIMER].expires = 0;
4093 remove_bh(SERIAL_BH);
4094 if ((e1 = tty_unregister_driver(&serial_driver)))
4095 printk("SERIAL: failed to unregister serial driver (%d)\n",
4096 e1);
4097 if ((e2 = tty_unregister_driver(&callout_driver)))
4098 printk("SERIAL: failed to unregister callout driver (%d)\n",
4099 e2);
4100 restore_flags(flags);
4102 for (i = 0; i < NR_PORTS; i++) {
4103 if ((rs_table[i].type != PORT_UNKNOWN) && rs_table[i].port)
4104 release_region(rs_table[i].port, 8);
4105 info = rs_table[i].info;
4106 if (info) {
4107 rs_table[i].info = NULL;
4108 kfree_s(info, sizeof(struct async_struct));
4110 #if defined(ENABLE_SERIAL_PCI) && defined (CONFIG_SERIAL_PCI_MEMMAPPED)
4111 if (rs_table[i].iomem_base)
4112 iounmap(rs_table[i].iomem_base);
4113 #endif
4115 #ifdef ENABLE_SERIAL_PCI
4116 for (i=0; i < serial_pci_board_idx; i++) {
4117 struct pci_board_inst *brd = &serial_pci_board[i];
4119 if (brd->board->init_fn)
4120 (brd->board->init_fn)(brd->dev, brd->board, 0);
4122 #endif
4123 if (tmp_buf) {
4124 free_page((unsigned long) tmp_buf);
4125 tmp_buf = NULL;
4128 #endif /* MODULE */
4132 * ------------------------------------------------------------
4133 * Serial console driver
4134 * ------------------------------------------------------------
4136 #ifdef CONFIG_SERIAL_CONSOLE
4138 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
4141 * Wait for transmitter & holding register to empty
4143 static inline void wait_for_xmitr(struct serial_state *ser)
4145 int lsr;
4146 unsigned int tmout = 1000000;
4148 do {
4149 lsr = inb(ser->port + UART_LSR);
4150 if (--tmout == 0) break;
4151 } while ((lsr & BOTH_EMPTY) != BOTH_EMPTY);
4155 * Print a string to the serial port trying not to disturb
4156 * any possible real use of the port...
4158 static void serial_console_write(struct console *co, const char *s,
4159 unsigned count)
4161 struct serial_state *ser;
4162 int ier;
4163 unsigned i;
4165 ser = rs_table + co->index;
4167 * First save the IER then disable the interrupts
4169 ier = inb(ser->port + UART_IER);
4170 outb(0x00, ser->port + UART_IER);
4173 * Now, do each character
4175 for (i = 0; i < count; i++, s++) {
4176 wait_for_xmitr(ser);
4179 * Send the character out.
4180 * If a LF, also do CR...
4182 outb(*s, ser->port + UART_TX);
4183 if (*s == 10) {
4184 wait_for_xmitr(ser);
4185 outb(13, ser->port + UART_TX);
4190 * Finally, Wait for transmitter & holding register to empty
4191 * and restore the IER
4193 wait_for_xmitr(ser);
4194 outb(ier, ser->port + UART_IER);
4198 * Receive character from the serial port
4200 static int serial_console_wait_key(struct console *co)
4202 struct serial_state *ser;
4203 int ier;
4204 int lsr;
4205 int c;
4207 ser = rs_table + co->index;
4210 * First save the IER then disable the interrupts so
4211 * that the real driver for the port does not get the
4212 * character.
4214 ier = inb(ser->port + UART_IER);
4215 outb(0x00, ser->port + UART_IER);
4217 do {
4218 lsr = inb(ser->port + UART_LSR);
4219 } while (!(lsr & UART_LSR_DR));
4220 c = inb(ser->port + UART_RX);
4223 * Restore the interrupts
4225 outb(ier, ser->port + UART_IER);
4227 return c;
4230 static kdev_t serial_console_device(struct console *c)
4232 return MKDEV(TTY_MAJOR, 64 + c->index);
4236 * Setup initial baud/bits/parity. We do two things here:
4237 * - construct a cflag setting for the first rs_open()
4238 * - initialize the serial port
4239 * Return non-zero if we didn't find a serial port.
4241 static int __init serial_console_setup(struct console *co, char *options)
4243 struct serial_state *ser;
4244 unsigned cval;
4245 int baud = 9600;
4246 int bits = 8;
4247 int parity = 'n';
4248 int cflag = CREAD | HUPCL | CLOCAL;
4249 int quot = 0;
4250 char *s;
4252 if (options) {
4253 baud = simple_strtoul(options, NULL, 10);
4254 s = options;
4255 while(*s >= '0' && *s <= '9')
4256 s++;
4257 if (*s) parity = *s++;
4258 if (*s) bits = *s - '0';
4262 * Now construct a cflag setting.
4264 switch(baud) {
4265 case 1200:
4266 cflag |= B1200;
4267 break;
4268 case 2400:
4269 cflag |= B2400;
4270 break;
4271 case 4800:
4272 cflag |= B4800;
4273 break;
4274 case 19200:
4275 cflag |= B19200;
4276 break;
4277 case 38400:
4278 cflag |= B38400;
4279 break;
4280 case 57600:
4281 cflag |= B57600;
4282 break;
4283 case 115200:
4284 cflag |= B115200;
4285 break;
4286 case 9600:
4287 default:
4288 cflag |= B9600;
4289 break;
4291 switch(bits) {
4292 case 7:
4293 cflag |= CS7;
4294 break;
4295 default:
4296 case 8:
4297 cflag |= CS8;
4298 break;
4300 switch(parity) {
4301 case 'o': case 'O':
4302 cflag |= PARODD;
4303 break;
4304 case 'e': case 'E':
4305 cflag |= PARENB;
4306 break;
4308 co->cflag = cflag;
4311 * Divisor, bytesize and parity
4313 ser = rs_table + co->index;
4314 quot = ser->baud_base / baud;
4315 cval = cflag & (CSIZE | CSTOPB);
4316 #if defined(__powerpc__) || defined(__alpha__)
4317 cval >>= 8;
4318 #else /* !__powerpc__ && !__alpha__ */
4319 cval >>= 4;
4320 #endif /* !__powerpc__ && !__alpha__ */
4321 if (cflag & PARENB)
4322 cval |= UART_LCR_PARITY;
4323 if (!(cflag & PARODD))
4324 cval |= UART_LCR_EPAR;
4327 * Disable UART interrupts, set DTR and RTS high
4328 * and set speed.
4330 outb(cval | UART_LCR_DLAB, ser->port + UART_LCR); /* set DLAB */
4331 outb(quot & 0xff, ser->port + UART_DLL); /* LS of divisor */
4332 outb(quot >> 8, ser->port + UART_DLM); /* MS of divisor */
4333 outb(cval, ser->port + UART_LCR); /* reset DLAB */
4334 outb(0, ser->port + UART_IER);
4335 outb(UART_MCR_DTR | UART_MCR_RTS, ser->port + UART_MCR);
4338 * If we read 0xff from the LSR, there is no UART here.
4340 if (inb(ser->port + UART_LSR) == 0xff)
4341 return -1;
4342 return 0;
4345 static struct console sercons = {
4346 "ttyS",
4347 serial_console_write,
4348 NULL,
4349 serial_console_device,
4350 serial_console_wait_key,
4351 NULL,
4352 serial_console_setup,
4353 CON_PRINTBUFFER,
4356 NULL
4360 * Register console.
4362 long __init serial_console_init(long kmem_start, long kmem_end)
4364 register_console(&sercons);
4365 return kmem_start;
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: