>> Btw, I've been looking at why Andrea thinks he's patches are needed,
[davej-history.git] / drivers / char / serial.c
blob141a0c6974127e3e82f875286e92df4ec98e776a
1 /*
2 * linux/drivers/char/serial.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * Extensively rewritten by Theodore Ts'o, 8/16/92 -- 9/14/92. Now
7 * much more extensible to support other serial cards based on the
8 * 16450/16550A UART's. Added support for the AST FourPort and the
9 * Accent Async board.
11 * set_serial_info fixed to set the flags, custom divisor, and uart
12 * type fields. Fix suggested by Michael K. Johnson 12/12/92.
14 * 11/95: TIOCMIWAIT, TIOCGICOUNT by Angelo Haritsis <ah@doc.ic.ac.uk>
16 * 03/96: Modularised by Angelo Haritsis <ah@doc.ic.ac.uk>
18 * rs_set_termios fixed to look also for changes of the input
19 * flags INPCK, BRKINT, PARMRK, IGNPAR and IGNBRK.
20 * Bernd Anhäupl 05/17/96.
22 * 1/97: Extended dumb serial ports are a config option now.
23 * Saves 4k. Michael A. Griffith <grif@acm.org>
25 * 8/97: Fix bug in rs_set_termios with RTS
26 * Stanislav V. Voronyi <stas@uanet.kharkov.ua>
28 * 3/98: Change the IRQ detection, use of probe_irq_o*(),
29 * supress TIOCSERGWILD and TIOCSERSWILD
30 * Etienne Lorrain <etienne.lorrain@ibm.net>
32 * 4/98: Added changes to support the ARM architecture proposed by
33 * Russell King
35 * This module exports the following rs232 io functions:
37 * int rs_init(void);
41 * Serial driver configuration section. Here are the various options:
43 * CONFIG_HUB6
44 * Enables support for the venerable Bell Technologies
45 * HUB6 card.
47 * CONFIG_SERIAL_MANY_PORTS
48 * Enables support for ports beyond the standard, stupid
49 * COM 1/2/3/4.
51 * CONFIG_SERIAL_MULTIPORT
52 * Enables support for special multiport board support.
54 * CONFIG_SERIAL_SHARE_IRQ
55 * Enables support for multiple serial ports on one IRQ
57 * CONFIG_SERIAL_DETECT_IRQ
58 * Enable the autodetection of IRQ on standart ports
60 * SERIAL_PARANOIA_CHECK
61 * Check the magic number for the async_structure where
62 * ever possible.
65 #undef SERIAL_PARANOIA_CHECK
66 #define CONFIG_SERIAL_NOPAUSE_IO
67 #define SERIAL_DO_RESTART
69 #if 0
70 /* Normally these defines are controlled by the autoconf.h */
72 #define CONFIG_SERIAL_MANY_PORTS
73 #define CONFIG_SERIAL_SHARE_IRQ
74 #define CONFIG_SERIAL_DETECT_IRQ
75 #define CONFIG_SERIAL_MULTIPORT
76 #define CONFIG_HUB6
77 #endif
79 /* Sanity checks */
81 #ifdef CONFIG_SERIAL_MULTIPORT
82 #ifndef CONFIG_SERIAL_SHARE_IRQ
83 #define CONFIG_SERIAL_SHARE_IRQ
84 #endif
85 #endif
87 #ifdef CONFIG_HUB6
88 #ifndef CONFIG_SERIAL_MANY_PORTS
89 #define CONFIG_SERIAL_MANY_PORTS
90 #endif
91 #ifndef CONFIG_SERIAL_SHARE_IRQ
92 #define CONFIG_SERIAL_SHARE_IRQ
93 #endif
94 #endif
96 /* Set of debugging defines */
98 #undef SERIAL_DEBUG_INTR
99 #undef SERIAL_DEBUG_OPEN
100 #undef SERIAL_DEBUG_FLOW
101 #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
103 #define RS_STROBE_TIME (10*HZ)
104 #define RS_ISR_PASS_LIMIT 256
106 #define IRQ_T(info) ((info->flags & ASYNC_SHARE_IRQ) ? SA_SHIRQ : SA_INTERRUPT)
108 #define SERIAL_INLINE
110 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
111 #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
112 kdevname(tty->device), (info->flags), serial_refcount,info->count,tty->count,s)
113 #else
114 #define DBG_CNT(s)
115 #endif
118 * End of serial driver configuration section.
121 #include <linux/config.h>
122 #include <linux/module.h>
123 #include <linux/errno.h>
124 #include <linux/signal.h>
125 #include <linux/sched.h>
126 #include <linux/timer.h>
127 #include <linux/interrupt.h>
128 #include <linux/tty.h>
129 #include <linux/tty_flip.h>
130 #include <linux/serial.h>
131 #include <linux/serial_reg.h>
132 #include <linux/major.h>
133 #include <linux/string.h>
134 #include <linux/fcntl.h>
135 #include <linux/ptrace.h>
136 #include <linux/ioport.h>
137 #include <linux/mm.h>
138 #include <linux/malloc.h>
139 #include <linux/init.h>
140 #include <linux/delay.h>
141 #ifdef CONFIG_SERIAL_CONSOLE
142 #include <linux/console.h>
143 #endif
145 #include <asm/system.h>
146 #include <asm/io.h>
147 #include <asm/irq.h>
148 #include <asm/uaccess.h>
149 #include <asm/bitops.h>
150 #include <asm/serial.h>
152 #ifdef SERIAL_INLINE
153 #define _INLINE_ inline
154 #endif
156 static char *serial_name = "Serial driver";
157 static char *serial_version = "4.26";
159 static DECLARE_TASK_QUEUE(tq_serial);
161 static struct tty_driver serial_driver, callout_driver;
162 static int serial_refcount;
164 /* number of characters left in xmit buffer before we ask for more */
165 #define WAKEUP_CHARS 256
168 * IRQ_timeout - How long the timeout should be for each IRQ
169 * should be after the IRQ has been active.
172 static struct async_struct *IRQ_ports[NR_IRQS];
173 #ifdef CONFIG_SERIAL_MULTIPORT
174 static struct rs_multiport_struct rs_multiport[NR_IRQS];
175 #endif
176 static int IRQ_timeout[NR_IRQS];
177 #ifdef CONFIG_SERIAL_CONSOLE
178 static struct console sercons;
179 #endif
181 static unsigned detect_uart_irq (struct serial_state * state);
182 static void autoconfig(struct serial_state * info);
183 static void change_speed(struct async_struct *info);
184 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
187 * Here we define the default xmit fifo size used for each type of
188 * UART
190 static struct serial_uart_config uart_config[] = {
191 { "unknown", 1, 0 },
192 { "8250", 1, 0 },
193 { "16450", 1, 0 },
194 { "16550", 1, 0 },
195 { "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
196 { "cirrus", 1, 0 },
197 { "ST16650", 1, UART_CLEAR_FIFO |UART_STARTECH },
198 { "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO |
199 UART_STARTECH },
200 { "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO},
201 { 0, 0}
204 static struct serial_state rs_table[] = {
205 SERIAL_PORT_DFNS /* Defined in serial.h */
208 #define NR_PORTS (sizeof(rs_table)/sizeof(struct serial_state))
210 static struct tty_struct *serial_table[NR_PORTS];
211 static struct termios *serial_termios[NR_PORTS];
212 static struct termios *serial_termios_locked[NR_PORTS];
214 #ifndef MIN
215 #define MIN(a,b) ((a) < (b) ? (a) : (b))
216 #endif
219 * tmp_buf is used as a temporary buffer by serial_write. We need to
220 * lock it in case the copy_from_user blocks while swapping in a page,
221 * and some other program tries to do a serial write at the same time.
222 * Since the lock will only come under contention when the system is
223 * swapping and available memory is low, it makes sense to share one
224 * buffer across all the serial ports, since it significantly saves
225 * memory if large numbers of serial ports are open.
227 static unsigned char *tmp_buf;
228 static struct semaphore tmp_buf_sem = MUTEX;
230 static inline int serial_paranoia_check(struct async_struct *info,
231 kdev_t device, const char *routine)
233 #ifdef SERIAL_PARANOIA_CHECK
234 static const char *badmagic =
235 "Warning: bad magic number for serial struct (%s) in %s\n";
236 static const char *badinfo =
237 "Warning: null async_struct for (%s) in %s\n";
239 if (!info) {
240 printk(badinfo, kdevname(device), routine);
241 return 1;
243 if (info->magic != SERIAL_MAGIC) {
244 printk(badmagic, kdevname(device), routine);
245 return 1;
247 #endif
248 return 0;
251 static inline unsigned int serial_in(struct async_struct *info, int offset)
253 #ifdef CONFIG_HUB6
254 if (info->hub6) {
255 outb(info->hub6 - 1 + offset, info->port);
256 return inb(info->port+1);
257 } else
258 #endif
259 return inb(info->port + offset);
262 static inline unsigned int serial_inp(struct async_struct *info, int offset)
264 #ifdef CONFIG_HUB6
265 if (info->hub6) {
266 outb(info->hub6 - 1 + offset, info->port);
267 return inb_p(info->port+1);
268 } else
269 #endif
270 #ifdef CONFIG_SERIAL_NOPAUSE_IO
271 return inb(info->port + offset);
272 #else
273 return inb_p(info->port + offset);
274 #endif
277 static inline void serial_out(struct async_struct *info, int offset, int value)
279 #ifdef CONFIG_HUB6
280 if (info->hub6) {
281 outb(info->hub6 - 1 + offset, info->port);
282 outb(value, info->port+1);
283 } else
284 #endif
285 outb(value, info->port+offset);
288 static inline void serial_outp(struct async_struct *info, int offset,
289 int value)
291 #ifdef CONFIG_HUB6
292 if (info->hub6) {
293 outb(info->hub6 - 1 + offset, info->port);
294 outb_p(value, info->port+1);
295 } else
296 #endif
297 #ifdef CONFIG_SERIAL_NOPAUSE_IO
298 outb(value, info->port+offset);
299 #else
300 outb_p(value, info->port+offset);
301 #endif
305 * ------------------------------------------------------------
306 * rs_stop() and rs_start()
308 * This routines are called before setting or resetting tty->stopped.
309 * They enable or disable transmitter interrupts, as necessary.
310 * ------------------------------------------------------------
312 static void rs_stop(struct tty_struct *tty)
314 struct async_struct *info = (struct async_struct *)tty->driver_data;
315 unsigned long flags;
317 if (serial_paranoia_check(info, tty->device, "rs_stop"))
318 return;
320 save_flags(flags); cli();
321 if (info->IER & UART_IER_THRI) {
322 info->IER &= ~UART_IER_THRI;
323 serial_out(info, UART_IER, info->IER);
325 restore_flags(flags);
328 static void rs_start(struct tty_struct *tty)
330 struct async_struct *info = (struct async_struct *)tty->driver_data;
331 unsigned long flags;
333 if (serial_paranoia_check(info, tty->device, "rs_start"))
334 return;
336 save_flags(flags); cli();
337 if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
338 info->IER |= UART_IER_THRI;
339 serial_out(info, UART_IER, info->IER);
341 restore_flags(flags);
345 * ----------------------------------------------------------------------
347 * Here starts the interrupt handling routines. All of the following
348 * subroutines are declared as inline and are folded into
349 * rs_interrupt(). They were separated out for readability's sake.
351 * Note: rs_interrupt() is a "fast" interrupt, which means that it
352 * runs with interrupts turned off. People who may want to modify
353 * rs_interrupt() should try to keep the interrupt handler as fast as
354 * possible. After you are done making modifications, it is not a bad
355 * idea to do:
357 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
359 * and look at the resulting assemble code in serial.s.
361 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
362 * -----------------------------------------------------------------------
366 * This routine is used by the interrupt handler to schedule
367 * processing in the software interrupt portion of the driver.
369 static _INLINE_ void rs_sched_event(struct async_struct *info,
370 int event)
372 info->event |= 1 << event;
373 queue_task(&info->tqueue, &tq_serial);
374 mark_bh(SERIAL_BH);
377 static _INLINE_ void receive_chars(struct async_struct *info,
378 int *status)
380 struct tty_struct *tty = info->tty;
381 unsigned char ch;
382 int ignored = 0;
383 struct async_icount *icount;
385 icount = &info->state->icount;
386 do {
387 ch = serial_inp(info, UART_RX);
388 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
389 break;
390 *tty->flip.char_buf_ptr = ch;
391 icount->rx++;
393 #ifdef SERIAL_DEBUG_INTR
394 printk("DR%02x:%02x...", ch, *status);
395 #endif
396 *tty->flip.flag_buf_ptr = 0;
397 if (*status & (UART_LSR_BI | UART_LSR_PE |
398 UART_LSR_FE | UART_LSR_OE)) {
400 * For statistics only
402 if (*status & UART_LSR_BI) {
403 *status &= ~(UART_LSR_FE | UART_LSR_PE);
404 icount->brk++;
405 } else if (*status & UART_LSR_PE)
406 icount->parity++;
407 else if (*status & UART_LSR_FE)
408 icount->frame++;
409 if (*status & UART_LSR_OE)
410 icount->overrun++;
413 * Now check to see if character should be
414 * ignored, and mask off conditions which
415 * should be ignored.
417 if (*status & info->ignore_status_mask) {
418 if (++ignored > 100)
419 break;
420 goto ignore_char;
422 *status &= info->read_status_mask;
424 if (*status & (UART_LSR_BI)) {
425 #ifdef SERIAL_DEBUG_INTR
426 printk("handling break....");
427 #endif
428 *tty->flip.flag_buf_ptr = TTY_BREAK;
429 if (info->flags & ASYNC_SAK)
430 do_SAK(tty);
431 } else if (*status & UART_LSR_PE)
432 *tty->flip.flag_buf_ptr = TTY_PARITY;
433 else if (*status & UART_LSR_FE)
434 *tty->flip.flag_buf_ptr = TTY_FRAME;
435 if (*status & UART_LSR_OE) {
437 * Overrun is special, since it's
438 * reported immediately, and doesn't
439 * affect the current character
441 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
442 tty->flip.count++;
443 tty->flip.flag_buf_ptr++;
444 tty->flip.char_buf_ptr++;
445 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
449 tty->flip.flag_buf_ptr++;
450 tty->flip.char_buf_ptr++;
451 tty->flip.count++;
452 ignore_char:
453 *status = serial_inp(info, UART_LSR);
454 } while (*status & UART_LSR_DR);
455 tty_flip_buffer_push(tty);
458 static _INLINE_ void transmit_chars(struct async_struct *info, int *intr_done)
460 int count;
462 if (info->x_char) {
463 serial_outp(info, UART_TX, info->x_char);
464 info->state->icount.tx++;
465 info->x_char = 0;
466 if (intr_done)
467 *intr_done = 0;
468 return;
470 if ((info->xmit_cnt <= 0) || info->tty->stopped ||
471 info->tty->hw_stopped) {
472 info->IER &= ~UART_IER_THRI;
473 serial_out(info, UART_IER, info->IER);
474 return;
477 count = info->xmit_fifo_size;
478 do {
479 serial_out(info, UART_TX, info->xmit_buf[info->xmit_tail++]);
480 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
481 info->state->icount.tx++;
482 if (--info->xmit_cnt <= 0)
483 break;
484 } while (--count > 0);
486 if (info->xmit_cnt < WAKEUP_CHARS)
487 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
489 #ifdef SERIAL_DEBUG_INTR
490 printk("THRE...");
491 #endif
492 if (intr_done)
493 *intr_done = 0;
495 if (info->xmit_cnt <= 0) {
496 info->IER &= ~UART_IER_THRI;
497 serial_out(info, UART_IER, info->IER);
501 static _INLINE_ void check_modem_status(struct async_struct *info)
503 int status;
504 struct async_icount *icount;
506 status = serial_in(info, UART_MSR);
508 if (status & UART_MSR_ANY_DELTA) {
509 icount = &info->state->icount;
510 /* update input line counters */
511 if (status & UART_MSR_TERI)
512 icount->rng++;
513 if (status & UART_MSR_DDSR)
514 icount->dsr++;
515 if (status & UART_MSR_DDCD) {
516 icount->dcd++;
517 #ifdef CONFIG_HARD_PPS
518 if ((info->flags & ASYNC_HARDPPS_CD) &&
519 (status & UART_MSR_DCD))
520 hardpps();
521 #endif
523 if (status & UART_MSR_DCTS)
524 icount->cts++;
525 wake_up_interruptible(&info->delta_msr_wait);
528 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
529 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
530 printk("ttys%d CD now %s...", info->line,
531 (status & UART_MSR_DCD) ? "on" : "off");
532 #endif
533 if (status & UART_MSR_DCD)
534 wake_up_interruptible(&info->open_wait);
535 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
536 (info->flags & ASYNC_CALLOUT_NOHUP))) {
537 #ifdef SERIAL_DEBUG_OPEN
538 printk("doing serial hangup...");
539 #endif
540 if (info->tty)
541 tty_hangup(info->tty);
544 if (info->flags & ASYNC_CTS_FLOW) {
545 if (info->tty->hw_stopped) {
546 if (status & UART_MSR_CTS) {
547 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
548 printk("CTS tx start...");
549 #endif
550 info->tty->hw_stopped = 0;
551 info->IER |= UART_IER_THRI;
552 serial_out(info, UART_IER, info->IER);
553 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
554 return;
556 } else {
557 if (!(status & UART_MSR_CTS)) {
558 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
559 printk("CTS tx stop...");
560 #endif
561 info->tty->hw_stopped = 1;
562 info->IER &= ~UART_IER_THRI;
563 serial_out(info, UART_IER, info->IER);
569 #ifdef CONFIG_SERIAL_SHARE_IRQ
571 * This is the serial driver's generic interrupt routine
573 static void rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
575 int status;
576 struct async_struct * info;
577 int pass_counter = 0;
578 struct async_struct *end_mark = 0;
579 #ifdef CONFIG_SERIAL_MULTIPORT
580 int first_multi = 0;
581 struct rs_multiport_struct *multi;
582 #endif
584 #ifdef SERIAL_DEBUG_INTR
585 printk("rs_interrupt(%d)...", irq);
586 #endif
588 info = IRQ_ports[irq];
589 if (!info)
590 return;
592 #ifdef CONFIG_SERIAL_MULTIPORT
593 multi = &rs_multiport[irq];
594 if (multi->port_monitor)
595 first_multi = inb(multi->port_monitor);
596 #endif
598 do {
599 if (!info->tty ||
600 (serial_in(info, UART_IIR) & UART_IIR_NO_INT)) {
601 if (!end_mark)
602 end_mark = info;
603 goto next;
605 end_mark = 0;
607 info->last_active = jiffies;
609 status = serial_inp(info, UART_LSR);
610 #ifdef SERIAL_DEBUG_INTR
611 printk("status = %x...", status);
612 #endif
613 if (status & UART_LSR_DR)
614 receive_chars(info, &status);
615 check_modem_status(info);
616 if (status & UART_LSR_THRE)
617 transmit_chars(info, 0);
619 next:
620 info = info->next_port;
621 if (!info) {
622 info = IRQ_ports[irq];
623 if (pass_counter++ > RS_ISR_PASS_LIMIT) {
624 #if 0
625 printk("rs loop break\n");
626 #endif
627 break; /* Prevent infinite loops */
629 continue;
631 } while (end_mark != info);
632 #ifdef CONFIG_SERIAL_MULTIPORT
633 if (multi->port_monitor)
634 printk("rs port monitor (normal) irq %d: 0x%x, 0x%x\n",
635 info->state->irq, first_multi,
636 inb(multi->port_monitor));
637 #endif
638 #ifdef SERIAL_DEBUG_INTR
639 printk("end.\n");
640 #endif
642 #endif /* #ifdef CONFIG_SERIAL_SHARE_IRQ */
646 * This is the serial driver's interrupt routine for a single port
648 static void rs_interrupt_single(int irq, void *dev_id, struct pt_regs * regs)
650 int status;
651 int pass_counter = 0;
652 struct async_struct * info;
653 #ifdef CONFIG_SERIAL_MULTIPORT
654 int first_multi = 0;
655 struct rs_multiport_struct *multi;
656 #endif
658 #ifdef SERIAL_DEBUG_INTR
659 printk("rs_interrupt_single(%d)...", irq);
660 #endif
662 info = IRQ_ports[irq];
663 if (!info || !info->tty)
664 return;
666 #ifdef CONFIG_SERIAL_MULTIPORT
667 multi = &rs_multiport[irq];
668 if (multi->port_monitor)
669 first_multi = inb(multi->port_monitor);
670 #endif
672 do {
673 status = serial_inp(info, UART_LSR);
674 #ifdef SERIAL_DEBUG_INTR
675 printk("status = %x...", status);
676 #endif
677 if (status & UART_LSR_DR)
678 receive_chars(info, &status);
679 check_modem_status(info);
680 if (status & UART_LSR_THRE)
681 transmit_chars(info, 0);
682 if (pass_counter++ > RS_ISR_PASS_LIMIT) {
683 #if 0
684 printk("rs_single loop break.\n");
685 #endif
686 break;
688 } while (!(serial_in(info, UART_IIR) & UART_IIR_NO_INT));
689 info->last_active = jiffies;
690 #ifdef CONFIG_SERIAL_MULTIPORT
691 if (multi->port_monitor)
692 printk("rs port monitor (single) irq %d: 0x%x, 0x%x\n",
693 info->state->irq, first_multi,
694 inb(multi->port_monitor));
695 #endif
696 #ifdef SERIAL_DEBUG_INTR
697 printk("end.\n");
698 #endif
701 #ifdef CONFIG_SERIAL_MULTIPORT
703 * This is the serial driver's for multiport boards
705 static void rs_interrupt_multi(int irq, void *dev_id, struct pt_regs * regs)
707 int status;
708 struct async_struct * info;
709 int pass_counter = 0;
710 int first_multi= 0;
711 struct rs_multiport_struct *multi;
713 #ifdef SERIAL_DEBUG_INTR
714 printk("rs_interrupt_multi(%d)...", irq);
715 #endif
717 info = IRQ_ports[irq];
718 if (!info)
719 return;
720 multi = &rs_multiport[irq];
721 if (!multi->port1) {
722 /* Should never happen */
723 printk("rs_interrupt_multi: NULL port1!\n");
724 return;
726 if (multi->port_monitor)
727 first_multi = inb(multi->port_monitor);
729 while (1) {
730 if (!info->tty ||
731 (serial_in(info, UART_IIR) & UART_IIR_NO_INT))
732 goto next;
734 info->last_active = jiffies;
736 status = serial_inp(info, UART_LSR);
737 #ifdef SERIAL_DEBUG_INTR
738 printk("status = %x...", status);
739 #endif
740 if (status & UART_LSR_DR)
741 receive_chars(info, &status);
742 check_modem_status(info);
743 if (status & UART_LSR_THRE)
744 transmit_chars(info, 0);
746 next:
747 info = info->next_port;
748 if (info)
749 continue;
751 info = IRQ_ports[irq];
752 if (pass_counter++ > RS_ISR_PASS_LIMIT) {
753 #if 1
754 printk("rs_multi loop break\n");
755 #endif
756 break; /* Prevent infinite loops */
758 if (multi->port_monitor)
759 printk("rs port monitor irq %d: 0x%x, 0x%x\n",
760 info->state->irq, first_multi,
761 inb(multi->port_monitor));
762 if ((inb(multi->port1) & multi->mask1) != multi->match1)
763 continue;
764 if (!multi->port2)
765 break;
766 if ((inb(multi->port2) & multi->mask2) != multi->match2)
767 continue;
768 if (!multi->port3)
769 break;
770 if ((inb(multi->port3) & multi->mask3) != multi->match3)
771 continue;
772 if (!multi->port4)
773 break;
774 if ((inb(multi->port4) & multi->mask4) == multi->match4)
775 continue;
776 break;
778 #ifdef SERIAL_DEBUG_INTR
779 printk("end.\n");
780 #endif
782 #endif
785 * -------------------------------------------------------------------
786 * Here ends the serial interrupt routines.
787 * -------------------------------------------------------------------
791 * This routine is used to handle the "bottom half" processing for the
792 * serial driver, known also the "software interrupt" processing.
793 * This processing is done at the kernel interrupt level, after the
794 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
795 * is where time-consuming activities which can not be done in the
796 * interrupt driver proper are done; the interrupt driver schedules
797 * them using rs_sched_event(), and they get done here.
799 static void do_serial_bh(void)
801 run_task_queue(&tq_serial);
804 static void do_softint(void *private_)
806 struct async_struct *info = (struct async_struct *) private_;
807 struct tty_struct *tty;
809 tty = info->tty;
810 if (!tty)
811 return;
813 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
814 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
815 tty->ldisc.write_wakeup)
816 (tty->ldisc.write_wakeup)(tty);
817 wake_up_interruptible(&tty->write_wait);
822 * This subroutine is called when the RS_TIMER goes off. It is used
823 * by the serial driver to handle ports that do not have an interrupt
824 * (irq=0). This doesn't work very well for 16450's, but gives barely
825 * passable results for a 16550A. (Although at the expense of much
826 * CPU overhead).
828 static void rs_timer(void)
830 static unsigned long last_strobe = 0;
831 struct async_struct *info;
832 unsigned int i;
833 unsigned long flags;
835 if ((jiffies - last_strobe) >= RS_STROBE_TIME) {
836 for (i=1; i < NR_IRQS; i++) {
837 info = IRQ_ports[i];
838 if (!info)
839 continue;
840 save_flags(flags); cli();
841 #ifdef CONFIG_SERIAL_SHARE_IRQ
842 if (info->next_port) {
843 do {
844 serial_out(info, UART_IER, 0);
845 info->IER |= UART_IER_THRI;
846 serial_out(info, UART_IER, info->IER);
847 info = info->next_port;
848 } while (info);
849 #ifdef CONFIG_SERIAL_MULTIPORT
850 if (rs_multiport[i].port1)
851 rs_interrupt_multi(i, NULL, NULL);
852 else
853 #endif
854 rs_interrupt(i, NULL, NULL);
855 } else
856 #endif /* CONFIG_SERIAL_SHARE_IRQ */
857 rs_interrupt_single(i, NULL, NULL);
858 restore_flags(flags);
861 last_strobe = jiffies;
862 timer_table[RS_TIMER].expires = jiffies + RS_STROBE_TIME;
863 timer_active |= 1 << RS_TIMER;
865 if (IRQ_ports[0]) {
866 save_flags(flags); cli();
867 #ifdef CONFIG_SERIAL_SHARE_IRQ
868 rs_interrupt(0, NULL, NULL);
869 #else
870 rs_interrupt_single(0, NULL, NULL);
871 #endif
872 restore_flags(flags);
874 timer_table[RS_TIMER].expires = jiffies + IRQ_timeout[0] - 2;
879 * ---------------------------------------------------------------
880 * Low level utility subroutines for the serial driver: routines to
881 * figure out the appropriate timeout for an interrupt chain, routines
882 * to initialize and startup a serial port, and routines to shutdown a
883 * serial port. Useful stuff like that.
884 * ---------------------------------------------------------------
888 * This routine figures out the correct timeout for a particular IRQ.
889 * It uses the smallest timeout of all of the serial ports in a
890 * particular interrupt chain. Now only used for IRQ 0....
892 static void figure_IRQ_timeout(int irq)
894 struct async_struct *info;
895 int timeout = 60*HZ; /* 60 seconds === a long time :-) */
897 info = IRQ_ports[irq];
898 if (!info) {
899 IRQ_timeout[irq] = 60*HZ;
900 return;
902 while (info) {
903 if (info->timeout < timeout)
904 timeout = info->timeout;
905 info = info->next_port;
907 if (!irq)
908 timeout = timeout / 2;
909 IRQ_timeout[irq] = timeout ? timeout : 1;
912 static int startup(struct async_struct * info)
914 unsigned long flags;
915 int retval=0;
916 void (*handler)(int, void *, struct pt_regs *);
917 struct serial_state *state= info->state;
918 unsigned long page;
919 #ifdef CONFIG_SERIAL_MANY_PORTS
920 unsigned short ICP;
921 #endif
923 page = get_free_page(GFP_KERNEL);
924 if (!page)
925 return -ENOMEM;
927 save_flags(flags); cli();
929 if (info->flags & ASYNC_INITIALIZED) {
930 free_page(page);
931 goto errout;
934 if (!state->port || !state->type) {
935 if (info->tty)
936 set_bit(TTY_IO_ERROR, &info->tty->flags);
937 free_page(page);
938 goto errout;
940 if (info->xmit_buf)
941 free_page(page);
942 else
943 info->xmit_buf = (unsigned char *) page;
945 #ifdef SERIAL_DEBUG_OPEN
946 printk("starting up ttys%d (irq %d)...", info->line, state->irq);
947 #endif
949 if (uart_config[info->state->type].flags & UART_STARTECH) {
950 /* Wake up UART */
951 serial_outp(info, UART_LCR, 0xBF);
952 serial_outp(info, UART_EFR, UART_EFR_ECB);
953 serial_outp(info, UART_IER, 0);
954 serial_outp(info, UART_EFR, 0);
955 serial_outp(info, UART_LCR, 0);
958 if (info->state->type == PORT_16750) {
959 /* Wake up UART */
960 serial_outp(info, UART_IER, 0);
964 * Clear the FIFO buffers and disable them
965 * (they will be reenabled in change_speed())
967 if (uart_config[state->type].flags & UART_CLEAR_FIFO)
968 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
969 UART_FCR_CLEAR_XMIT));
972 * At this point there's no way the LSR could still be 0xFF;
973 * if it is, then bail out, because there's likely no UART
974 * here.
976 if (serial_inp(info, UART_LSR) == 0xff) {
977 if (capable(CAP_SYS_ADMIN)) {
978 if (info->tty)
979 set_bit(TTY_IO_ERROR, &info->tty->flags);
980 } else
981 retval = -ENODEV;
982 goto errout;
986 * Allocate the IRQ if necessary
988 if (state->irq && (!IRQ_ports[state->irq] ||
989 !IRQ_ports[state->irq]->next_port)) {
990 if (IRQ_ports[state->irq]) {
991 #ifdef CONFIG_SERIAL_SHARE_IRQ
992 free_irq(state->irq, NULL);
993 #ifdef CONFIG_SERIAL_MULTIPORT
994 if (rs_multiport[state->irq].port1)
995 handler = rs_interrupt_multi;
996 else
997 #endif
998 handler = rs_interrupt;
999 #else
1000 retval = -EBUSY;
1001 goto errout;
1002 #endif /* CONFIG_SERIAL_SHARE_IRQ */
1003 } else
1004 handler = rs_interrupt_single;
1006 retval = request_irq(state->irq, handler, IRQ_T(info),
1007 "serial", NULL);
1008 if (retval) {
1009 if (capable(CAP_SYS_ADMIN)) {
1010 if (info->tty)
1011 set_bit(TTY_IO_ERROR,
1012 &info->tty->flags);
1013 retval = 0;
1015 goto errout;
1020 * Insert serial port into IRQ chain.
1022 info->prev_port = 0;
1023 info->next_port = IRQ_ports[state->irq];
1024 if (info->next_port)
1025 info->next_port->prev_port = info;
1026 IRQ_ports[state->irq] = info;
1027 figure_IRQ_timeout(state->irq);
1030 * Clear the interrupt registers.
1032 /* (void) serial_inp(info, UART_LSR); */ /* (see above) */
1033 (void) serial_inp(info, UART_RX);
1034 (void) serial_inp(info, UART_IIR);
1035 (void) serial_inp(info, UART_MSR);
1038 * Now, initialize the UART
1040 serial_outp(info, UART_LCR, UART_LCR_WLEN8); /* reset DLAB */
1042 info->MCR = 0;
1043 if (info->tty->termios->c_cflag & CBAUD)
1044 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
1045 #ifdef CONFIG_SERIAL_MANY_PORTS
1046 if (info->flags & ASYNC_FOURPORT) {
1047 if (state->irq == 0)
1048 info->MCR |= UART_MCR_OUT1;
1049 } else
1050 #endif
1052 if (state->irq != 0)
1053 info->MCR |= UART_MCR_OUT2;
1055 #if defined(__alpha__) && !defined(CONFIG_PCI)
1057 * DEC did something gratutiously wrong....
1059 info->MCR |= UART_MCR_OUT1 | UART_MCR_OUT2;
1060 #endif
1061 serial_outp(info, UART_MCR, info->MCR);
1064 * Finally, enable interrupts
1066 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
1067 serial_outp(info, UART_IER, info->IER); /* enable interrupts */
1069 #ifdef CONFIG_SERIAL_MANY_PORTS
1070 if (info->flags & ASYNC_FOURPORT) {
1071 /* Enable interrupts on the AST Fourport board */
1072 ICP = (info->port & 0xFE0) | 0x01F;
1073 outb_p(0x80, ICP);
1074 (void) inb_p(ICP);
1076 #endif
1079 * And clear the interrupt registers again for luck.
1081 (void)serial_inp(info, UART_LSR);
1082 (void)serial_inp(info, UART_RX);
1083 (void)serial_inp(info, UART_IIR);
1084 (void)serial_inp(info, UART_MSR);
1086 if (info->tty)
1087 clear_bit(TTY_IO_ERROR, &info->tty->flags);
1088 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1091 * Set up serial timers...
1093 timer_table[RS_TIMER].expires = jiffies + 2*HZ/100;
1094 timer_active |= 1 << RS_TIMER;
1097 * Set up the tty->alt_speed kludge
1099 if (info->tty) {
1100 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1101 info->tty->alt_speed = 57600;
1102 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1103 info->tty->alt_speed = 115200;
1104 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1105 info->tty->alt_speed = 230400;
1106 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1107 info->tty->alt_speed = 460800;
1111 * and set the speed of the serial port
1113 change_speed(info);
1115 info->flags |= ASYNC_INITIALIZED;
1116 restore_flags(flags);
1117 return 0;
1119 errout:
1120 restore_flags(flags);
1121 return retval;
1125 * This routine will shutdown a serial port; interrupts are disabled, and
1126 * DTR is dropped if the hangup on close termio flag is on.
1128 static void shutdown(struct async_struct * info)
1130 unsigned long flags;
1131 struct serial_state *state;
1132 int retval;
1134 if (!(info->flags & ASYNC_INITIALIZED))
1135 return;
1137 state = info->state;
1139 #ifdef SERIAL_DEBUG_OPEN
1140 printk("Shutting down serial port %d (irq %d)....", info->line,
1141 state->irq);
1142 #endif
1144 save_flags(flags); cli(); /* Disable interrupts */
1147 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1148 * here so the queue might never be waken up
1150 wake_up_interruptible(&info->delta_msr_wait);
1153 * First unlink the serial port from the IRQ chain...
1155 if (info->next_port)
1156 info->next_port->prev_port = info->prev_port;
1157 if (info->prev_port)
1158 info->prev_port->next_port = info->next_port;
1159 else
1160 IRQ_ports[state->irq] = info->next_port;
1161 figure_IRQ_timeout(state->irq);
1164 * Free the IRQ, if necessary
1166 if (state->irq && (!IRQ_ports[state->irq] ||
1167 !IRQ_ports[state->irq]->next_port)) {
1168 if (IRQ_ports[state->irq]) {
1169 free_irq(state->irq, NULL);
1170 retval = request_irq(state->irq, rs_interrupt_single,
1171 IRQ_T(info), "serial", NULL);
1173 if (retval)
1174 printk("serial shutdown: request_irq: error %d"
1175 " Couldn't reacquire IRQ.\n", retval);
1176 } else
1177 free_irq(state->irq, NULL);
1180 if (info->xmit_buf) {
1181 free_page((unsigned long) info->xmit_buf);
1182 info->xmit_buf = 0;
1185 info->IER = 0;
1186 serial_outp(info, UART_IER, 0x00); /* disable all intrs */
1187 #ifdef CONFIG_SERIAL_MANY_PORTS
1188 if (info->flags & ASYNC_FOURPORT) {
1189 /* reset interrupts on the AST Fourport board */
1190 (void) inb((info->port & 0xFE0) | 0x01F);
1191 info->MCR |= UART_MCR_OUT1;
1192 } else
1193 #endif
1194 info->MCR &= ~UART_MCR_OUT2;
1195 #if defined(__alpha__) && !defined(CONFIG_PCI)
1197 * DEC did something gratutiously wrong....
1199 info->MCR |= UART_MCR_OUT1 | UART_MCR_OUT2;
1200 #endif
1202 /* disable break condition */
1203 serial_out(info, UART_LCR, serial_inp(info, UART_LCR) & ~UART_LCR_SBC);
1205 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
1206 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1207 serial_outp(info, UART_MCR, info->MCR);
1209 /* disable FIFO's */
1210 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
1211 UART_FCR_CLEAR_XMIT));
1212 (void)serial_in(info, UART_RX); /* read data port to reset things */
1214 if (info->tty)
1215 set_bit(TTY_IO_ERROR, &info->tty->flags);
1217 if (uart_config[info->state->type].flags & UART_STARTECH) {
1218 /* Arrange to enter sleep mode */
1219 serial_outp(info, UART_LCR, 0xBF);
1220 serial_outp(info, UART_EFR, UART_EFR_ECB);
1221 serial_outp(info, UART_IER, UART_IERX_SLEEP);
1222 serial_outp(info, UART_LCR, 0);
1224 if (info->state->type == PORT_16750) {
1225 /* Arrange to enter sleep mode */
1226 serial_outp(info, UART_IER, UART_IERX_SLEEP);
1228 info->flags &= ~ASYNC_INITIALIZED;
1229 restore_flags(flags);
1233 * This routine is called to set the UART divisor registers to match
1234 * the specified baud rate for a serial port.
1236 static void change_speed(struct async_struct *info)
1238 unsigned short port;
1239 int quot = 0, baud_base, baud;
1240 unsigned cflag, cval, fcr = 0;
1241 int bits;
1242 unsigned long flags;
1244 if (!info->tty || !info->tty->termios)
1245 return;
1246 cflag = info->tty->termios->c_cflag;
1247 if (!(port = info->port))
1248 return;
1250 /* byte size and parity */
1251 switch (cflag & CSIZE) {
1252 case CS5: cval = 0x00; bits = 7; break;
1253 case CS6: cval = 0x01; bits = 8; break;
1254 case CS7: cval = 0x02; bits = 9; break;
1255 case CS8: cval = 0x03; bits = 10; break;
1256 /* Never happens, but GCC is too dumb to figure it out */
1257 default: cval = 0x00; bits = 7; break;
1259 if (cflag & CSTOPB) {
1260 cval |= 0x04;
1261 bits++;
1263 if (cflag & PARENB) {
1264 cval |= UART_LCR_PARITY;
1265 bits++;
1267 if (!(cflag & PARODD))
1268 cval |= UART_LCR_EPAR;
1269 #ifdef CMSPAR
1270 if (cflag & CMSPAR)
1271 cval |= UART_LCR_SPAR;
1272 #endif
1274 /* Determine divisor based on baud rate */
1275 baud = tty_get_baud_rate(info->tty);
1276 baud_base = info->state->baud_base;
1277 if (baud == 38400 &&
1278 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1279 quot = info->state->custom_divisor;
1280 else {
1281 if (baud == 134)
1282 /* Special case since 134 is really 134.5 */
1283 quot = (2*baud_base / 269);
1284 else if (baud)
1285 quot = baud_base / baud;
1287 /* If the quotient is ever zero, default to 9600 bps */
1288 if (!quot)
1289 quot = baud_base / 9600;
1290 info->quot = quot;
1291 info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
1292 info->timeout += HZ/50; /* Add .02 seconds of slop */
1294 /* Set up FIFO's */
1295 if (uart_config[info->state->type].flags & UART_USE_FIFO) {
1296 if ((info->state->baud_base / quot) < 2400)
1297 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
1298 else
1299 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
1301 if (info->state->type == PORT_16750)
1302 fcr |= UART_FCR7_64BYTE;
1304 /* CTS flow control flag and modem status interrupts */
1305 info->IER &= ~UART_IER_MSI;
1306 if (info->flags & ASYNC_HARDPPS_CD)
1307 info->IER |= UART_IER_MSI;
1308 if (cflag & CRTSCTS) {
1309 info->flags |= ASYNC_CTS_FLOW;
1310 info->IER |= UART_IER_MSI;
1311 } else
1312 info->flags &= ~ASYNC_CTS_FLOW;
1313 if (cflag & CLOCAL)
1314 info->flags &= ~ASYNC_CHECK_CD;
1315 else {
1316 info->flags |= ASYNC_CHECK_CD;
1317 info->IER |= UART_IER_MSI;
1319 serial_out(info, UART_IER, info->IER);
1322 * Set up parity check flag
1324 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1326 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1327 if (I_INPCK(info->tty))
1328 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1329 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1330 info->read_status_mask |= UART_LSR_BI;
1333 * Characters to ignore
1335 info->ignore_status_mask = 0;
1336 if (I_IGNPAR(info->tty))
1337 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1338 if (I_IGNBRK(info->tty)) {
1339 info->ignore_status_mask |= UART_LSR_BI;
1341 * If we're ignore parity and break indicators, ignore
1342 * overruns too. (For real raw support).
1344 if (I_IGNPAR(info->tty))
1345 info->ignore_status_mask |= UART_LSR_OE;
1348 * !!! ignore all characters if CREAD is not set
1350 if ((cflag & CREAD) == 0)
1351 info->ignore_status_mask |= UART_LSR_DR;
1352 save_flags(flags); cli();
1353 if (uart_config[info->state->type].flags & UART_STARTECH) {
1354 serial_outp(info, UART_LCR, 0xBF);
1355 serial_outp(info, UART_EFR,
1356 (cflag & CRTSCTS) ? UART_EFR_CTS : 0);
1358 serial_outp(info, UART_LCR, cval | UART_LCR_DLAB); /* set DLAB */
1359 serial_outp(info, UART_DLL, quot & 0xff); /* LS of divisor */
1360 serial_outp(info, UART_DLM, quot >> 8); /* MS of divisor */
1361 if (info->state->type == PORT_16750)
1362 serial_outp(info, UART_FCR, fcr); /* set fcr */
1363 serial_outp(info, UART_LCR, cval); /* reset DLAB */
1364 if (info->state->type != PORT_16750)
1365 serial_outp(info, UART_FCR, fcr); /* set fcr */
1366 restore_flags(flags);
1369 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
1371 struct async_struct *info = (struct async_struct *)tty->driver_data;
1372 unsigned long flags;
1374 if (serial_paranoia_check(info, tty->device, "rs_put_char"))
1375 return;
1377 if (!tty || !info->xmit_buf)
1378 return;
1380 save_flags(flags); cli();
1381 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
1382 restore_flags(flags);
1383 return;
1386 info->xmit_buf[info->xmit_head++] = ch;
1387 info->xmit_head &= SERIAL_XMIT_SIZE-1;
1388 info->xmit_cnt++;
1389 restore_flags(flags);
1392 static void rs_flush_chars(struct tty_struct *tty)
1394 struct async_struct *info = (struct async_struct *)tty->driver_data;
1395 unsigned long flags;
1397 if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
1398 return;
1400 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1401 !info->xmit_buf)
1402 return;
1404 save_flags(flags); cli();
1405 info->IER |= UART_IER_THRI;
1406 serial_out(info, UART_IER, info->IER);
1407 restore_flags(flags);
1410 static int rs_write(struct tty_struct * tty, int from_user,
1411 const unsigned char *buf, int count)
1413 int c, ret = 0;
1414 struct async_struct *info = (struct async_struct *)tty->driver_data;
1415 unsigned long flags;
1417 if (serial_paranoia_check(info, tty->device, "rs_write"))
1418 return 0;
1420 if (!tty || !info->xmit_buf || !tmp_buf)
1421 return 0;
1423 save_flags(flags);
1424 if (from_user) {
1425 down(&tmp_buf_sem);
1426 while (1) {
1427 c = MIN(count,
1428 MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1429 SERIAL_XMIT_SIZE - info->xmit_head));
1430 if (c <= 0)
1431 break;
1433 c -= copy_from_user(tmp_buf, buf, c);
1434 if (!c) {
1435 if (!ret)
1436 ret = -EFAULT;
1437 break;
1439 cli();
1440 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1441 SERIAL_XMIT_SIZE - info->xmit_head));
1442 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1443 info->xmit_head = ((info->xmit_head + c) &
1444 (SERIAL_XMIT_SIZE-1));
1445 info->xmit_cnt += c;
1446 restore_flags(flags);
1447 buf += c;
1448 count -= c;
1449 ret += c;
1451 up(&tmp_buf_sem);
1452 } else {
1453 while (1) {
1454 cli();
1455 c = MIN(count,
1456 MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1457 SERIAL_XMIT_SIZE - info->xmit_head));
1458 if (c <= 0) {
1459 restore_flags(flags);
1460 break;
1462 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1463 info->xmit_head = ((info->xmit_head + c) &
1464 (SERIAL_XMIT_SIZE-1));
1465 info->xmit_cnt += c;
1466 restore_flags(flags);
1467 buf += c;
1468 count -= c;
1469 ret += c;
1472 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1473 !(info->IER & UART_IER_THRI)) {
1474 info->IER |= UART_IER_THRI;
1475 serial_out(info, UART_IER, info->IER);
1477 return ret;
1480 static int rs_write_room(struct tty_struct *tty)
1482 struct async_struct *info = (struct async_struct *)tty->driver_data;
1483 int ret;
1485 if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1486 return 0;
1487 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1488 if (ret < 0)
1489 ret = 0;
1490 return ret;
1493 static int rs_chars_in_buffer(struct tty_struct *tty)
1495 struct async_struct *info = (struct async_struct *)tty->driver_data;
1497 if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1498 return 0;
1499 return info->xmit_cnt;
1502 static void rs_flush_buffer(struct tty_struct *tty)
1504 struct async_struct *info = (struct async_struct *)tty->driver_data;
1505 unsigned long flags;
1507 if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1508 return;
1509 save_flags(flags); cli();
1510 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1511 restore_flags(flags);
1512 wake_up_interruptible(&tty->write_wait);
1513 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1514 tty->ldisc.write_wakeup)
1515 (tty->ldisc.write_wakeup)(tty);
1519 * This function is used to send a high-priority XON/XOFF character to
1520 * the device
1522 static void rs_send_xchar(struct tty_struct *tty, char ch)
1524 struct async_struct *info = (struct async_struct *)tty->driver_data;
1526 if (serial_paranoia_check(info, tty->device, "rs_send_char"))
1527 return;
1529 info->x_char = ch;
1530 if (ch) {
1531 /* Make sure transmit interrupts are on */
1532 info->IER |= UART_IER_THRI;
1533 serial_out(info, UART_IER, info->IER);
1538 * ------------------------------------------------------------
1539 * rs_throttle()
1541 * This routine is called by the upper-layer tty layer to signal that
1542 * incoming characters should be throttled.
1543 * ------------------------------------------------------------
1545 static void rs_throttle(struct tty_struct * tty)
1547 struct async_struct *info = (struct async_struct *)tty->driver_data;
1548 unsigned long flags;
1549 #ifdef SERIAL_DEBUG_THROTTLE
1550 char buf[64];
1552 printk("throttle %s: %d....\n", tty_name(tty, buf),
1553 tty->ldisc.chars_in_buffer(tty));
1554 #endif
1556 if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1557 return;
1559 if (I_IXOFF(tty))
1560 rs_send_xchar(tty, STOP_CHAR(tty));
1562 if (tty->termios->c_cflag & CRTSCTS)
1563 info->MCR &= ~UART_MCR_RTS;
1565 save_flags(flags); cli();
1566 serial_out(info, UART_MCR, info->MCR);
1567 restore_flags(flags);
1570 static void rs_unthrottle(struct tty_struct * tty)
1572 struct async_struct *info = (struct async_struct *)tty->driver_data;
1573 unsigned long flags;
1574 #ifdef SERIAL_DEBUG_THROTTLE
1575 char buf[64];
1577 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1578 tty->ldisc.chars_in_buffer(tty));
1579 #endif
1581 if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1582 return;
1584 if (I_IXOFF(tty)) {
1585 if (info->x_char)
1586 info->x_char = 0;
1587 else
1588 rs_send_xchar(tty, START_CHAR(tty));
1590 if (tty->termios->c_cflag & CRTSCTS)
1591 info->MCR |= UART_MCR_RTS;
1592 save_flags(flags); cli();
1593 serial_out(info, UART_MCR, info->MCR);
1594 restore_flags(flags);
1598 * ------------------------------------------------------------
1599 * rs_ioctl() and friends
1600 * ------------------------------------------------------------
1603 static int get_serial_info(struct async_struct * info,
1604 struct serial_struct * retinfo)
1606 struct serial_struct tmp;
1607 struct serial_state *state = info->state;
1609 if (!retinfo)
1610 return -EFAULT;
1611 memset(&tmp, 0, sizeof(tmp));
1612 tmp.type = state->type;
1613 tmp.line = state->line;
1614 tmp.port = state->port;
1615 tmp.irq = state->irq;
1616 tmp.flags = state->flags;
1617 tmp.xmit_fifo_size = state->xmit_fifo_size;
1618 tmp.baud_base = state->baud_base;
1619 tmp.close_delay = state->close_delay;
1620 tmp.closing_wait = state->closing_wait;
1621 tmp.custom_divisor = state->custom_divisor;
1622 tmp.hub6 = state->hub6;
1623 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1624 return -EFAULT;
1625 return 0;
1628 static int set_serial_info(struct async_struct * info,
1629 struct serial_struct * new_info)
1631 struct serial_struct new_serial;
1632 struct serial_state old_state, *state;
1633 unsigned int i,change_irq,change_port;
1634 int retval = 0;
1636 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1637 return -EFAULT;
1638 state = info->state;
1639 old_state = *state;
1641 change_irq = new_serial.irq != state->irq;
1642 change_port = (new_serial.port != state->port) ||
1643 (new_serial.hub6 != state->hub6);
1645 if (!capable(CAP_SYS_ADMIN)) {
1646 if (change_irq || change_port ||
1647 (new_serial.baud_base != state->baud_base) ||
1648 (new_serial.type != state->type) ||
1649 (new_serial.close_delay != state->close_delay) ||
1650 (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1651 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1652 (state->flags & ~ASYNC_USR_MASK)))
1653 return -EPERM;
1654 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
1655 (new_serial.flags & ASYNC_USR_MASK));
1656 state->custom_divisor = new_serial.custom_divisor;
1657 goto check_and_exit;
1660 new_serial.irq = irq_cannonicalize(new_serial.irq);
1662 if ((new_serial.irq >= NR_IRQS) || (new_serial.port > 0xffff) ||
1663 (new_serial.type < PORT_UNKNOWN) ||
1664 (new_serial.type > PORT_MAX)) {
1665 return -EINVAL;
1668 if ((new_serial.type != state->type) ||
1669 (new_serial.xmit_fifo_size <= 0))
1670 new_serial.xmit_fifo_size =
1671 uart_config[state->type].dfl_xmit_fifo_size;
1673 /* Make sure address is not already in use */
1674 if (new_serial.type) {
1675 for (i = 0 ; i < NR_PORTS; i++)
1676 if ((state != &rs_table[i]) &&
1677 (rs_table[i].port == new_serial.port) &&
1678 rs_table[i].type)
1679 return -EADDRINUSE;
1682 if ((change_port || change_irq) && (state->count > 1))
1683 return -EBUSY;
1686 * OK, past this point, all the error checking has been done.
1687 * At this point, we start making changes.....
1690 state->baud_base = new_serial.baud_base;
1691 state->flags = ((state->flags & ~ASYNC_FLAGS) |
1692 (new_serial.flags & ASYNC_FLAGS));
1693 info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
1694 (info->flags & ASYNC_INTERNAL_FLAGS));
1695 state->custom_divisor = new_serial.custom_divisor;
1696 state->type = new_serial.type;
1697 state->close_delay = new_serial.close_delay * HZ/100;
1698 state->closing_wait = new_serial.closing_wait * HZ/100;
1699 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1700 info->xmit_fifo_size = state->xmit_fifo_size =
1701 new_serial.xmit_fifo_size;
1703 release_region(state->port,8);
1704 if (change_port || change_irq) {
1706 * We need to shutdown the serial port at the old
1707 * port/irq combination.
1709 shutdown(info);
1710 state->irq = new_serial.irq;
1711 info->port = state->port = new_serial.port;
1712 info->hub6 = state->hub6 = new_serial.hub6;
1714 if (state->type != PORT_UNKNOWN)
1715 request_region(state->port,8,"serial(set)");
1718 check_and_exit:
1719 if (!state->port || !state->type)
1720 return 0;
1721 if (state->flags & ASYNC_INITIALIZED) {
1722 if (((old_state.flags & ASYNC_SPD_MASK) !=
1723 (state->flags & ASYNC_SPD_MASK)) ||
1724 (old_state.custom_divisor != state->custom_divisor)) {
1725 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1726 info->tty->alt_speed = 57600;
1727 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1728 info->tty->alt_speed = 115200;
1729 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1730 info->tty->alt_speed = 230400;
1731 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1732 info->tty->alt_speed = 460800;
1733 change_speed(info);
1735 } else
1736 retval = startup(info);
1737 return retval;
1742 * get_lsr_info - get line status register info
1744 * Purpose: Let user call ioctl() to get info when the UART physically
1745 * is emptied. On bus types like RS485, the transmitter must
1746 * release the bus after transmitting. This must be done when
1747 * the transmit shift register is empty, not be done when the
1748 * transmit holding register is empty. This functionality
1749 * allows an RS485 driver to be written in user space.
1751 static int get_lsr_info(struct async_struct * info, unsigned int *value)
1753 unsigned char status;
1754 unsigned int result;
1755 unsigned long flags;
1757 save_flags(flags); cli();
1758 status = serial_in(info, UART_LSR);
1759 restore_flags(flags);
1760 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1761 return put_user(result,value);
1765 static int get_modem_info(struct async_struct * info, unsigned int *value)
1767 unsigned char control, status;
1768 unsigned int result;
1769 unsigned long flags;
1771 control = info->MCR;
1772 save_flags(flags); cli();
1773 status = serial_in(info, UART_MSR);
1774 restore_flags(flags);
1775 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1776 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1777 #ifdef TIOCM_OUT1
1778 | ((control & UART_MCR_OUT1) ? TIOCM_OUT1 : 0)
1779 | ((control & UART_MCR_OUT2) ? TIOCM_OUT2 : 0)
1780 #endif
1781 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1782 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1783 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1784 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1785 return put_user(result,value);
1788 static int set_modem_info(struct async_struct * info, unsigned int cmd,
1789 unsigned int *value)
1791 int error;
1792 unsigned int arg;
1793 unsigned long flags;
1795 error = get_user(arg, value);
1796 if (error)
1797 return error;
1798 switch (cmd) {
1799 case TIOCMBIS:
1800 if (arg & TIOCM_RTS)
1801 info->MCR |= UART_MCR_RTS;
1802 if (arg & TIOCM_DTR)
1803 info->MCR |= UART_MCR_DTR;
1804 #ifdef TIOCM_OUT1
1805 if (arg & TIOCM_OUT1)
1806 info->MCR |= UART_MCR_OUT1;
1807 if (arg & TIOCM_OUT2)
1808 info->MCR |= UART_MCR_OUT2;
1809 #endif
1810 break;
1811 case TIOCMBIC:
1812 if (arg & TIOCM_RTS)
1813 info->MCR &= ~UART_MCR_RTS;
1814 if (arg & TIOCM_DTR)
1815 info->MCR &= ~UART_MCR_DTR;
1816 #ifdef TIOCM_OUT1
1817 if (arg & TIOCM_OUT1)
1818 info->MCR &= ~UART_MCR_OUT1;
1819 if (arg & TIOCM_OUT2)
1820 info->MCR &= ~UART_MCR_OUT2;
1821 #endif
1822 break;
1823 case TIOCMSET:
1824 info->MCR = ((info->MCR & ~(UART_MCR_RTS |
1825 #ifdef TIOCM_OUT1
1826 UART_MCR_OUT1 |
1827 UART_MCR_OUT2 |
1828 #endif
1829 UART_MCR_DTR))
1830 | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1831 #ifdef TIOCM_OUT1
1832 | ((arg & TIOCM_OUT1) ? UART_MCR_OUT1 : 0)
1833 | ((arg & TIOCM_OUT2) ? UART_MCR_OUT2 : 0)
1834 #endif
1835 | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1836 break;
1837 default:
1838 return -EINVAL;
1840 save_flags(flags); cli();
1841 serial_out(info, UART_MCR, info->MCR);
1842 restore_flags(flags);
1843 return 0;
1846 static int do_autoconfig(struct async_struct * info)
1848 int retval;
1850 if (!capable(CAP_SYS_ADMIN))
1851 return -EPERM;
1853 if (info->state->count > 1)
1854 return -EBUSY;
1856 shutdown(info);
1858 autoconfig(info->state);
1859 if ((info->state->flags & ASYNC_AUTO_IRQ) &&
1860 (info->state->port != 0) &&
1861 (info->state->type != PORT_UNKNOWN))
1862 info->state->irq = detect_uart_irq(info->state);
1864 retval = startup(info);
1865 if (retval)
1866 return retval;
1867 return 0;
1871 * rs_break() --- routine which turns the break handling on or off
1873 static void rs_break(struct tty_struct *tty, int break_state)
1875 struct async_struct * info = (struct async_struct *)tty->driver_data;
1876 unsigned long flags;
1878 if (serial_paranoia_check(info, tty->device, "rs_break"))
1879 return;
1881 if (!info->port)
1882 return;
1883 save_flags(flags); cli();
1884 if (break_state == -1)
1885 serial_out(info, UART_LCR,
1886 serial_inp(info, UART_LCR) | UART_LCR_SBC);
1887 else
1888 serial_out(info, UART_LCR,
1889 serial_inp(info, UART_LCR) & ~UART_LCR_SBC);
1890 restore_flags(flags);
1893 #ifdef CONFIG_SERIAL_MULTIPORT
1894 static int get_multiport_struct(struct async_struct * info,
1895 struct serial_multiport_struct *retinfo)
1897 struct serial_multiport_struct ret;
1898 struct rs_multiport_struct *multi;
1900 multi = &rs_multiport[info->state->irq];
1902 ret.port_monitor = multi->port_monitor;
1904 ret.port1 = multi->port1;
1905 ret.mask1 = multi->mask1;
1906 ret.match1 = multi->match1;
1908 ret.port2 = multi->port2;
1909 ret.mask2 = multi->mask2;
1910 ret.match2 = multi->match2;
1912 ret.port3 = multi->port3;
1913 ret.mask3 = multi->mask3;
1914 ret.match3 = multi->match3;
1916 ret.port4 = multi->port4;
1917 ret.mask4 = multi->mask4;
1918 ret.match4 = multi->match4;
1920 ret.irq = info->state->irq;
1922 if (copy_to_user(retinfo,&ret,sizeof(*retinfo)))
1923 return -EFAULT;
1924 return 0;
1927 static int set_multiport_struct(struct async_struct * info,
1928 struct serial_multiport_struct *in_multi)
1930 struct serial_multiport_struct new_multi;
1931 struct rs_multiport_struct *multi;
1932 struct serial_state *state;
1933 int was_multi, now_multi;
1934 int retval;
1935 void (*handler)(int, void *, struct pt_regs *);
1937 if (!capable(CAP_SYS_ADMIN))
1938 return -EPERM;
1939 state = info->state;
1941 if (copy_from_user(&new_multi, in_multi,
1942 sizeof(struct serial_multiport_struct)))
1943 return -EFAULT;
1945 if (new_multi.irq != state->irq || state->irq == 0 ||
1946 !IRQ_ports[state->irq])
1947 return -EINVAL;
1949 multi = &rs_multiport[state->irq];
1950 was_multi = (multi->port1 != 0);
1952 multi->port_monitor = new_multi.port_monitor;
1954 if (multi->port1)
1955 release_region(multi->port1,1);
1956 multi->port1 = new_multi.port1;
1957 multi->mask1 = new_multi.mask1;
1958 multi->match1 = new_multi.match1;
1959 if (multi->port1)
1960 request_region(multi->port1,1,"serial(multiport1)");
1962 if (multi->port2)
1963 release_region(multi->port2,1);
1964 multi->port2 = new_multi.port2;
1965 multi->mask2 = new_multi.mask2;
1966 multi->match2 = new_multi.match2;
1967 if (multi->port2)
1968 request_region(multi->port2,1,"serial(multiport2)");
1970 if (multi->port3)
1971 release_region(multi->port3,1);
1972 multi->port3 = new_multi.port3;
1973 multi->mask3 = new_multi.mask3;
1974 multi->match3 = new_multi.match3;
1975 if (multi->port3)
1976 request_region(multi->port3,1,"serial(multiport3)");
1978 if (multi->port4)
1979 release_region(multi->port4,1);
1980 multi->port4 = new_multi.port4;
1981 multi->mask4 = new_multi.mask4;
1982 multi->match4 = new_multi.match4;
1983 if (multi->port4)
1984 request_region(multi->port4,1,"serial(multiport4)");
1986 now_multi = (multi->port1 != 0);
1988 if (IRQ_ports[state->irq]->next_port &&
1989 (was_multi != now_multi)) {
1990 free_irq(state->irq, NULL);
1991 if (now_multi)
1992 handler = rs_interrupt_multi;
1993 else
1994 handler = rs_interrupt;
1996 retval = request_irq(state->irq, handler, IRQ_T(info),
1997 "serial", NULL);
1998 if (retval) {
1999 printk("Couldn't reallocate serial interrupt "
2000 "driver!!\n");
2004 return 0;
2006 #endif
2008 static int rs_ioctl(struct tty_struct *tty, struct file * file,
2009 unsigned int cmd, unsigned long arg)
2011 int error;
2012 struct async_struct * info = (struct async_struct *)tty->driver_data;
2013 struct async_icount cprev, cnow; /* kernel counter temps */
2014 struct serial_icounter_struct *p_cuser; /* user space */
2015 unsigned long flags;
2017 if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
2018 return -ENODEV;
2020 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2021 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
2022 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
2023 if (tty->flags & (1 << TTY_IO_ERROR))
2024 return -EIO;
2027 switch (cmd) {
2028 case TIOCMGET:
2029 return get_modem_info(info, (unsigned int *) arg);
2030 case TIOCMBIS:
2031 case TIOCMBIC:
2032 case TIOCMSET:
2033 return set_modem_info(info, cmd, (unsigned int *) arg);
2034 case TIOCGSERIAL:
2035 return get_serial_info(info,
2036 (struct serial_struct *) arg);
2037 case TIOCSSERIAL:
2038 return set_serial_info(info,
2039 (struct serial_struct *) arg);
2040 case TIOCSERCONFIG:
2041 return do_autoconfig(info);
2043 case TIOCSERGETLSR: /* Get line status register */
2044 return get_lsr_info(info, (unsigned int *) arg);
2046 case TIOCSERGSTRUCT:
2047 if (copy_to_user((struct async_struct *) arg,
2048 info, sizeof(struct async_struct)))
2049 return -EFAULT;
2050 return 0;
2052 #ifdef CONFIG_SERIAL_MULTIPORT
2053 case TIOCSERGETMULTI:
2054 return get_multiport_struct(info,
2055 (struct serial_multiport_struct *) arg);
2056 case TIOCSERSETMULTI:
2057 return set_multiport_struct(info,
2058 (struct serial_multiport_struct *) arg);
2059 #endif
2062 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
2063 * - mask passed in arg for lines of interest
2064 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
2065 * Caller should use TIOCGICOUNT to see which one it was
2067 case TIOCMIWAIT:
2068 save_flags(flags); cli();
2069 /* note the counters on entry */
2070 cprev = info->state->icount;
2071 restore_flags(flags);
2072 while (1) {
2073 interruptible_sleep_on(&info->delta_msr_wait);
2074 /* see if a signal did it */
2075 if (signal_pending(current))
2076 return -ERESTARTSYS;
2077 save_flags(flags); cli();
2078 cnow = info->state->icount; /* atomic copy */
2079 restore_flags(flags);
2080 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
2081 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
2082 return -EIO; /* no change => error */
2083 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
2084 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
2085 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
2086 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
2087 return 0;
2089 cprev = cnow;
2091 /* NOTREACHED */
2094 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
2095 * Return: write counters to the user passed counter struct
2096 * NB: both 1->0 and 0->1 transitions are counted except for
2097 * RI where only 0->1 is counted.
2099 case TIOCGICOUNT:
2100 save_flags(flags); cli();
2101 cnow = info->state->icount;
2102 restore_flags(flags);
2103 p_cuser = (struct serial_icounter_struct *) arg;
2104 error = put_user(cnow.cts, &p_cuser->cts);
2105 if (error) return error;
2106 error = put_user(cnow.dsr, &p_cuser->dsr);
2107 if (error) return error;
2108 error = put_user(cnow.rng, &p_cuser->rng);
2109 if (error) return error;
2110 error = put_user(cnow.dcd, &p_cuser->dcd);
2111 if (error) return error;
2112 error = put_user(cnow.rx, &p_cuser->rx);
2113 if (error) return error;
2114 error = put_user(cnow.tx, &p_cuser->tx);
2115 if (error) return error;
2116 error = put_user(cnow.frame, &p_cuser->frame);
2117 if (error) return error;
2118 error = put_user(cnow.overrun, &p_cuser->overrun);
2119 if (error) return error;
2120 error = put_user(cnow.parity, &p_cuser->parity);
2121 if (error) return error;
2122 error = put_user(cnow.brk, &p_cuser->brk);
2123 if (error) return error;
2124 error = put_user(cnow.buf_overrun, &p_cuser->buf_overrun);
2125 if (error) return error;
2126 return 0;
2128 case TIOCSERGWILD:
2129 case TIOCSERSWILD:
2130 /* "setserial -W" is called in Debian boot */
2131 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
2132 return 0;
2134 default:
2135 return -ENOIOCTLCMD;
2137 return 0;
2140 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
2142 struct async_struct *info = (struct async_struct *)tty->driver_data;
2143 unsigned long flags;
2145 if ( (tty->termios->c_cflag == old_termios->c_cflag)
2146 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
2147 == RELEVANT_IFLAG(old_termios->c_iflag)))
2148 return;
2150 change_speed(info);
2152 /* Handle transition to B0 status */
2153 if ((old_termios->c_cflag & CBAUD) &&
2154 !(tty->termios->c_cflag & CBAUD)) {
2155 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
2156 save_flags(flags); cli();
2157 serial_out(info, UART_MCR, info->MCR);
2158 restore_flags(flags);
2161 /* Handle transition away from B0 status */
2162 if (!(old_termios->c_cflag & CBAUD) &&
2163 (tty->termios->c_cflag & CBAUD)) {
2164 info->MCR |= UART_MCR_DTR;
2165 if (!(tty->termios->c_cflag & CRTSCTS) ||
2166 !test_bit(TTY_THROTTLED, &tty->flags)) {
2167 info->MCR |= UART_MCR_RTS;
2169 save_flags(flags); cli();
2170 serial_out(info, UART_MCR, info->MCR);
2171 restore_flags(flags);
2174 /* Handle turning off CRTSCTS */
2175 if ((old_termios->c_cflag & CRTSCTS) &&
2176 !(tty->termios->c_cflag & CRTSCTS)) {
2177 tty->hw_stopped = 0;
2178 rs_start(tty);
2181 #if 0
2183 * No need to wake up processes in open wait, since they
2184 * sample the CLOCAL flag once, and don't recheck it.
2185 * XXX It's not clear whether the current behavior is correct
2186 * or not. Hence, this may change.....
2188 if (!(old_termios->c_cflag & CLOCAL) &&
2189 (tty->termios->c_cflag & CLOCAL))
2190 wake_up_interruptible(&info->open_wait);
2191 #endif
2195 * ------------------------------------------------------------
2196 * rs_close()
2198 * This routine is called when the serial port gets closed. First, we
2199 * wait for the last remaining data to be sent. Then, we unlink its
2200 * async structure from the interrupt chain if necessary, and we free
2201 * that IRQ if nothing is left in the chain.
2202 * ------------------------------------------------------------
2204 static void rs_close(struct tty_struct *tty, struct file * filp)
2206 struct async_struct * info = (struct async_struct *)tty->driver_data;
2207 struct serial_state *state;
2208 unsigned long flags;
2210 if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
2211 return;
2213 state = info->state;
2215 save_flags(flags); cli();
2217 if (tty_hung_up_p(filp)) {
2218 DBG_CNT("before DEC-hung");
2219 MOD_DEC_USE_COUNT;
2220 restore_flags(flags);
2221 return;
2224 #ifdef SERIAL_DEBUG_OPEN
2225 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
2226 #endif
2227 if ((tty->count == 1) && (state->count != 1)) {
2229 * Uh, oh. tty->count is 1, which means that the tty
2230 * structure will be freed. state->count should always
2231 * be one in these conditions. If it's greater than
2232 * one, we've got real problems, since it means the
2233 * serial port won't be shutdown.
2235 printk("rs_close: bad serial port count; tty->count is 1, "
2236 "state->count is %d\n", state->count);
2237 state->count = 1;
2239 if (--state->count < 0) {
2240 printk("rs_close: bad serial port count for ttys%d: %d\n",
2241 info->line, state->count);
2242 state->count = 0;
2244 if (state->count) {
2245 DBG_CNT("before DEC-2");
2246 MOD_DEC_USE_COUNT;
2247 restore_flags(flags);
2248 return;
2250 info->flags |= ASYNC_CLOSING;
2252 * Save the termios structure, since this port may have
2253 * separate termios for callout and dialin.
2255 if (info->flags & ASYNC_NORMAL_ACTIVE)
2256 info->state->normal_termios = *tty->termios;
2257 if (info->flags & ASYNC_CALLOUT_ACTIVE)
2258 info->state->callout_termios = *tty->termios;
2260 * Now we wait for the transmit buffer to clear; and we notify
2261 * the line discipline to only process XON/XOFF characters.
2263 tty->closing = 1;
2264 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
2265 tty_wait_until_sent(tty, info->closing_wait);
2267 * At this point we stop accepting input. To do this, we
2268 * disable the receive line status interrupts, and tell the
2269 * interrupt driver to stop checking the data ready bit in the
2270 * line status register.
2272 info->IER &= ~UART_IER_RLSI;
2273 info->read_status_mask &= ~UART_LSR_DR;
2274 if (info->flags & ASYNC_INITIALIZED) {
2275 serial_out(info, UART_IER, info->IER);
2277 * Before we drop DTR, make sure the UART transmitter
2278 * has completely drained; this is especially
2279 * important if there is a transmit FIFO!
2281 rs_wait_until_sent(tty, info->timeout);
2283 shutdown(info);
2284 if (tty->driver.flush_buffer)
2285 tty->driver.flush_buffer(tty);
2286 if (tty->ldisc.flush_buffer)
2287 tty->ldisc.flush_buffer(tty);
2288 tty->closing = 0;
2289 info->event = 0;
2290 info->tty = 0;
2291 if (info->blocked_open) {
2292 if (info->close_delay) {
2293 current->state = TASK_INTERRUPTIBLE;
2294 schedule_timeout(info->close_delay);
2296 wake_up_interruptible(&info->open_wait);
2298 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
2299 ASYNC_CLOSING);
2300 wake_up_interruptible(&info->close_wait);
2301 MOD_DEC_USE_COUNT;
2302 restore_flags(flags);
2306 * rs_wait_until_sent() --- wait until the transmitter is empty
2308 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
2310 struct async_struct * info = (struct async_struct *)tty->driver_data;
2311 unsigned long orig_jiffies, char_time;
2312 int lsr;
2314 if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
2315 return;
2317 if (info->state->type == PORT_UNKNOWN)
2318 return;
2320 if (info->xmit_fifo_size == 0)
2321 return; /* Just in case.... */
2323 orig_jiffies = jiffies;
2325 * Set the check interval to be 1/5 of the estimated time to
2326 * send a single character, and make it at least 1. The check
2327 * interval should also be less than the timeout.
2329 * Note: we have to use pretty tight timings here to satisfy
2330 * the NIST-PCTS.
2332 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
2333 char_time = char_time / 5;
2334 if (char_time == 0)
2335 char_time = 1;
2336 if (timeout)
2337 char_time = MIN(char_time, timeout);
2338 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2339 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
2340 printk("jiff=%lu...", jiffies);
2341 #endif
2342 while (!((lsr = serial_inp(info, UART_LSR)) & UART_LSR_TEMT)) {
2343 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2344 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2345 #endif
2346 current->state = TASK_INTERRUPTIBLE;
2347 current->counter = 0; /* make us low-priority */
2348 schedule_timeout(char_time);
2349 if (signal_pending(current))
2350 break;
2351 if (timeout && ((orig_jiffies + timeout) < jiffies))
2352 break;
2354 current->state = TASK_RUNNING;
2355 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2356 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2357 #endif
2361 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
2363 static void rs_hangup(struct tty_struct *tty)
2365 struct async_struct * info = (struct async_struct *)tty->driver_data;
2366 struct serial_state *state = info->state;
2368 if (serial_paranoia_check(info, tty->device, "rs_hangup"))
2369 return;
2371 state = info->state;
2373 rs_flush_buffer(tty);
2374 shutdown(info);
2375 info->event = 0;
2376 state->count = 0;
2377 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
2378 info->tty = 0;
2379 wake_up_interruptible(&info->open_wait);
2383 * ------------------------------------------------------------
2384 * rs_open() and friends
2385 * ------------------------------------------------------------
2387 static int block_til_ready(struct tty_struct *tty, struct file * filp,
2388 struct async_struct *info)
2390 struct wait_queue wait = { current, NULL };
2391 struct serial_state *state = info->state;
2392 int retval;
2393 int do_clocal = 0, extra_count = 0;
2394 unsigned long flags;
2397 * If the device is in the middle of being closed, then block
2398 * until it's done, and then try again.
2400 if (tty_hung_up_p(filp) ||
2401 (info->flags & ASYNC_CLOSING)) {
2402 if (info->flags & ASYNC_CLOSING)
2403 interruptible_sleep_on(&info->close_wait);
2404 #ifdef SERIAL_DO_RESTART
2405 return ((info->flags & ASYNC_HUP_NOTIFY) ?
2406 -EAGAIN : -ERESTARTSYS);
2407 #else
2408 return -EAGAIN;
2409 #endif
2413 * If this is a callout device, then just make sure the normal
2414 * device isn't being used.
2416 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
2417 if (info->flags & ASYNC_NORMAL_ACTIVE)
2418 return -EBUSY;
2419 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2420 (info->flags & ASYNC_SESSION_LOCKOUT) &&
2421 (info->session != current->session))
2422 return -EBUSY;
2423 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2424 (info->flags & ASYNC_PGRP_LOCKOUT) &&
2425 (info->pgrp != current->pgrp))
2426 return -EBUSY;
2427 info->flags |= ASYNC_CALLOUT_ACTIVE;
2428 return 0;
2432 * If non-blocking mode is set, or the port is not enabled,
2433 * then make the check up front and then exit.
2435 if ((filp->f_flags & O_NONBLOCK) ||
2436 (tty->flags & (1 << TTY_IO_ERROR))) {
2437 if (info->flags & ASYNC_CALLOUT_ACTIVE)
2438 return -EBUSY;
2439 info->flags |= ASYNC_NORMAL_ACTIVE;
2440 return 0;
2443 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
2444 if (state->normal_termios.c_cflag & CLOCAL)
2445 do_clocal = 1;
2446 } else {
2447 if (tty->termios->c_cflag & CLOCAL)
2448 do_clocal = 1;
2452 * Block waiting for the carrier detect and the line to become
2453 * free (i.e., not in use by the callout). While we are in
2454 * this loop, state->count is dropped by one, so that
2455 * rs_close() knows when to free things. We restore it upon
2456 * exit, either normal or abnormal.
2458 retval = 0;
2459 add_wait_queue(&info->open_wait, &wait);
2460 #ifdef SERIAL_DEBUG_OPEN
2461 printk("block_til_ready before block: ttys%d, count = %d\n",
2462 state->line, state->count);
2463 #endif
2464 save_flags(flags); cli();
2465 if (!tty_hung_up_p(filp)) {
2466 extra_count = 1;
2467 state->count--;
2469 restore_flags(flags);
2470 info->blocked_open++;
2471 while (1) {
2472 save_flags(flags); cli();
2473 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2474 (tty->termios->c_cflag & CBAUD))
2475 serial_out(info, UART_MCR,
2476 serial_inp(info, UART_MCR) |
2477 (UART_MCR_DTR | UART_MCR_RTS));
2478 restore_flags(flags);
2479 current->state = TASK_INTERRUPTIBLE;
2480 if (tty_hung_up_p(filp) ||
2481 !(info->flags & ASYNC_INITIALIZED)) {
2482 #ifdef SERIAL_DO_RESTART
2483 if (info->flags & ASYNC_HUP_NOTIFY)
2484 retval = -EAGAIN;
2485 else
2486 retval = -ERESTARTSYS;
2487 #else
2488 retval = -EAGAIN;
2489 #endif
2490 break;
2492 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2493 !(info->flags & ASYNC_CLOSING) &&
2494 (do_clocal || (serial_in(info, UART_MSR) &
2495 UART_MSR_DCD)))
2496 break;
2497 if (signal_pending(current)) {
2498 retval = -ERESTARTSYS;
2499 break;
2501 #ifdef SERIAL_DEBUG_OPEN
2502 printk("block_til_ready blocking: ttys%d, count = %d\n",
2503 info->line, state->count);
2504 #endif
2505 schedule();
2507 current->state = TASK_RUNNING;
2508 remove_wait_queue(&info->open_wait, &wait);
2509 if (extra_count)
2510 state->count++;
2511 info->blocked_open--;
2512 #ifdef SERIAL_DEBUG_OPEN
2513 printk("block_til_ready after blocking: ttys%d, count = %d\n",
2514 info->line, state->count);
2515 #endif
2516 if (retval)
2517 return retval;
2518 info->flags |= ASYNC_NORMAL_ACTIVE;
2519 return 0;
2522 static int get_async_struct(int line, struct async_struct **ret_info)
2524 struct async_struct *info;
2525 struct serial_state *sstate;
2527 sstate = rs_table + line;
2528 sstate->count++;
2529 if (sstate->info) {
2530 *ret_info = sstate->info;
2531 return 0;
2533 info = kmalloc(sizeof(struct async_struct), GFP_KERNEL);
2534 if (!info) {
2535 sstate->count--;
2536 return -ENOMEM;
2538 memset(info, 0, sizeof(struct async_struct));
2539 info->magic = SERIAL_MAGIC;
2540 info->port = sstate->port;
2541 info->flags = sstate->flags;
2542 info->xmit_fifo_size = sstate->xmit_fifo_size;
2543 info->line = line;
2544 info->tqueue.routine = do_softint;
2545 info->tqueue.data = info;
2546 info->state = sstate;
2547 if (sstate->info) {
2548 kfree_s(info, sizeof(struct async_struct));
2549 *ret_info = sstate->info;
2550 return 0;
2552 *ret_info = sstate->info = info;
2553 return 0;
2557 * This routine is called whenever a serial port is opened. It
2558 * enables interrupts for a serial port, linking in its async structure into
2559 * the IRQ chain. It also performs the serial-specific
2560 * initialization for the tty structure.
2562 static int rs_open(struct tty_struct *tty, struct file * filp)
2564 struct async_struct *info;
2565 int retval, line;
2566 unsigned long page;
2568 MOD_INC_USE_COUNT;
2569 line = MINOR(tty->device) - tty->driver.minor_start;
2570 if ((line < 0) || (line >= NR_PORTS)) {
2571 MOD_DEC_USE_COUNT;
2572 return -ENODEV;
2574 retval = get_async_struct(line, &info);
2575 if (retval) {
2576 MOD_DEC_USE_COUNT;
2577 return retval;
2579 tty->driver_data = info;
2580 info->tty = tty;
2581 if (serial_paranoia_check(info, tty->device, "rs_open")) {
2582 MOD_DEC_USE_COUNT;
2583 return -ENODEV;
2586 #ifdef SERIAL_DEBUG_OPEN
2587 printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
2588 info->state->count);
2589 #endif
2590 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2592 if (!tmp_buf) {
2593 page = get_free_page(GFP_KERNEL);
2594 if (!page) {
2595 MOD_DEC_USE_COUNT;
2596 return -ENOMEM;
2598 if (tmp_buf)
2599 free_page(page);
2600 else
2601 tmp_buf = (unsigned char *) page;
2605 * If the port is the middle of closing, bail out now
2607 if (tty_hung_up_p(filp) ||
2608 (info->flags & ASYNC_CLOSING)) {
2609 if (info->flags & ASYNC_CLOSING)
2610 interruptible_sleep_on(&info->close_wait);
2611 MOD_DEC_USE_COUNT;
2612 #ifdef SERIAL_DO_RESTART
2613 return ((info->flags & ASYNC_HUP_NOTIFY) ?
2614 -EAGAIN : -ERESTARTSYS);
2615 #else
2616 return -EAGAIN;
2617 #endif
2621 * Start up serial port
2623 retval = startup(info);
2624 if (retval) {
2625 MOD_DEC_USE_COUNT;
2626 return retval;
2629 retval = block_til_ready(tty, filp, info);
2630 if (retval) {
2631 MOD_DEC_USE_COUNT;
2632 #ifdef SERIAL_DEBUG_OPEN
2633 printk("rs_open returning after block_til_ready with %d\n",
2634 retval);
2635 #endif
2636 return retval;
2639 if ((info->state->count == 1) &&
2640 (info->flags & ASYNC_SPLIT_TERMIOS)) {
2641 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2642 *tty->termios = info->state->normal_termios;
2643 else
2644 *tty->termios = info->state->callout_termios;
2645 change_speed(info);
2647 #ifdef CONFIG_SERIAL_CONSOLE
2648 if (sercons.cflag && sercons.index == line) {
2649 tty->termios->c_cflag = sercons.cflag;
2650 sercons.cflag = 0;
2651 change_speed(info);
2653 #endif
2654 info->session = current->session;
2655 info->pgrp = current->pgrp;
2657 #ifdef SERIAL_DEBUG_OPEN
2658 printk("rs_open ttys%d successful...", info->line);
2659 #endif
2660 return 0;
2664 * /proc fs routines....
2667 static inline int line_info(char *buf, struct serial_state *state)
2669 struct async_struct *info = state->info, scr_info;
2670 char stat_buf[30], control, status;
2671 int ret;
2672 unsigned long flags;
2674 ret = sprintf(buf, "%d: uart:%s port:%X irq:%d",
2675 state->line, uart_config[state->type].name,
2676 state->port, state->irq);
2678 if (!state->port || (state->type == PORT_UNKNOWN)) {
2679 ret += sprintf(buf+ret, "\n");
2680 return ret;
2684 * Figure out the current RS-232 lines
2686 if (!info) {
2687 info = &scr_info; /* This is just for serial_{in,out} */
2689 info->magic = SERIAL_MAGIC;
2690 info->port = state->port;
2691 info->flags = state->flags;
2692 info->quot = 0;
2693 info->tty = 0;
2695 save_flags(flags); cli();
2696 status = serial_in(info, UART_MSR);
2697 control = info ? info->MCR : serial_in(info, UART_MCR);
2698 restore_flags(flags);
2700 stat_buf[0] = 0;
2701 stat_buf[1] = 0;
2702 if (control & UART_MCR_RTS)
2703 strcat(stat_buf, "|RTS");
2704 if (status & UART_MSR_CTS)
2705 strcat(stat_buf, "|CTS");
2706 if (control & UART_MCR_DTR)
2707 strcat(stat_buf, "|DTR");
2708 if (status & UART_MSR_DSR)
2709 strcat(stat_buf, "|DSR");
2710 if (status & UART_MSR_DCD)
2711 strcat(stat_buf, "|CD");
2712 if (status & UART_MSR_RI)
2713 strcat(stat_buf, "|RI");
2715 if (info->quot) {
2716 ret += sprintf(buf+ret, " baud:%d",
2717 state->baud_base / info->quot);
2720 ret += sprintf(buf+ret, " tx:%d rx:%d",
2721 state->icount.tx, state->icount.rx);
2723 if (state->icount.frame)
2724 ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
2726 if (state->icount.parity)
2727 ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
2729 if (state->icount.brk)
2730 ret += sprintf(buf+ret, " brk:%d", state->icount.brk);
2732 if (state->icount.overrun)
2733 ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
2736 * Last thing is the RS-232 status lines
2738 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2739 return ret;
2742 int rs_read_proc(char *page, char **start, off_t off, int count,
2743 int *eof, void *data)
2745 int i, len = 0, l;
2746 off_t begin = 0;
2748 len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
2749 for (i = 0; i < NR_PORTS && len < 4000; i++) {
2750 l = line_info(page + len, &rs_table[i]);
2751 len += l;
2752 if (len+begin > off+count)
2753 goto done;
2754 if (len+begin < off) {
2755 begin += len;
2756 len = 0;
2759 *eof = 1;
2760 done:
2761 if (off >= len+begin)
2762 return 0;
2763 *start = page + (begin-off);
2764 return ((count < begin+len-off) ? count : begin+len-off);
2768 * ---------------------------------------------------------------------
2769 * rs_init() and friends
2771 * rs_init() is called at boot-time to initialize the serial driver.
2772 * ---------------------------------------------------------------------
2776 * This routine prints out the appropriate serial driver version
2777 * number, and identifies which options were configured into this
2778 * driver.
2780 static _INLINE_ void show_serial_version(void)
2782 printk(KERN_INFO "%s version %s with", serial_name, serial_version);
2783 #ifdef CONFIG_HUB6
2784 printk(" HUB-6");
2785 #define SERIAL_OPT
2786 #endif
2787 #ifdef CONFIG_SERIAL_MANY_PORTS
2788 printk(" MANY_PORTS");
2789 #define SERIAL_OPT
2790 #endif
2791 #ifdef CONFIG_SERIAL_MULTIPORT
2792 printk(" MULTIPORT");
2793 #define SERIAL_OPT
2794 #endif
2795 #ifdef CONFIG_SERIAL_SHARE_IRQ
2796 printk(" SHARE_IRQ");
2797 #define SERIAL_OPT
2798 #endif
2799 #ifdef CONFIG_SERIAL_DETECT_IRQ
2800 printk(" DETECT_IRQ");
2801 #define SERIAL_OPT
2802 #endif
2803 #ifdef SERIAL_OPT
2804 printk(" enabled\n");
2805 #else
2806 printk(" no serial options enabled\n");
2807 #endif
2808 #undef SERIAL_OPT
2812 * This routine detect the IRQ of a serial port by clearing OUT2 when
2813 * no UART interrupt are requested (IER = 0) (*GPL*). This seems to work at
2814 * each time, as long as no other device permanently request the IRQ.
2815 * If no IRQ is detected, or multiple IRQ appear, this function returns 0.
2816 * The variable "state" and the field "state->port" should not be null.
2818 static unsigned detect_uart_irq (struct serial_state * state)
2820 int irq;
2821 unsigned long irqs;
2822 unsigned char save_mcr, save_ier;
2823 struct async_struct scr_info; /* serial_{in,out} because HUB6 */
2825 #ifdef CONFIG_SERIAL_MANY_PORTS
2826 unsigned char save_ICP=0; /* no warning */
2827 unsigned short ICP=0;
2829 if (state->flags & ASYNC_FOURPORT) {
2830 ICP = (state->port & 0xFE0) | 0x01F;
2831 save_ICP = inb_p(ICP);
2832 outb_p(0x80, ICP);
2833 (void) inb_p(ICP);
2835 #endif
2836 scr_info.magic = SERIAL_MAGIC;
2837 scr_info.port = state->port;
2838 scr_info.flags = state->flags;
2839 #ifdef CONFIG_HUB6
2840 scr_info.hub6 = state->hub6;
2841 #endif
2843 /* forget possible initially masked and pending IRQ */
2844 probe_irq_off(probe_irq_on());
2845 save_mcr = serial_inp(&scr_info, UART_MCR);
2846 save_ier = serial_inp(&scr_info, UART_IER);
2847 serial_outp(&scr_info, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2);
2849 irqs = probe_irq_on();
2850 serial_outp(&scr_info, UART_MCR, 0);
2851 udelay (10);
2852 if (state->flags & ASYNC_FOURPORT) {
2853 serial_outp(&scr_info, UART_MCR,
2854 UART_MCR_DTR | UART_MCR_RTS);
2855 } else {
2856 serial_outp(&scr_info, UART_MCR,
2857 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
2859 serial_outp(&scr_info, UART_IER, 0x0f); /* enable all intrs */
2860 (void)serial_inp(&scr_info, UART_LSR);
2861 (void)serial_inp(&scr_info, UART_RX);
2862 (void)serial_inp(&scr_info, UART_IIR);
2863 (void)serial_inp(&scr_info, UART_MSR);
2864 serial_outp(&scr_info, UART_TX, 0xFF);
2865 udelay (20);
2866 irq = probe_irq_off(irqs);
2868 serial_outp(&scr_info, UART_MCR, save_mcr);
2869 serial_outp(&scr_info, UART_IER, save_ier);
2870 #ifdef CONFIG_SERIAL_MANY_PORTS
2871 if (state->flags & ASYNC_FOURPORT)
2872 outb_p(save_ICP, ICP);
2873 #endif
2874 return (irq > 0)? irq : 0;
2878 * This routine is called by rs_init() to initialize a specific serial
2879 * port. It determines what type of UART chip this serial port is
2880 * using: 8250, 16450, 16550, 16550A. The important question is
2881 * whether or not this UART is a 16550A or not, since this will
2882 * determine whether or not we can use its FIFO features or not.
2884 static void autoconfig(struct serial_state * state)
2886 unsigned char status1, status2, scratch, scratch2;
2887 struct async_struct *info, scr_info;
2888 unsigned long flags;
2890 state->type = PORT_UNKNOWN;
2892 if (!state->port)
2893 return;
2895 info = &scr_info; /* This is just for serial_{in,out} */
2897 info->magic = SERIAL_MAGIC;
2898 info->port = state->port;
2899 info->flags = state->flags;
2900 #ifdef CONFIG_HUB6
2901 info->hub6 = state->hub6;
2902 #endif
2904 save_flags(flags); cli();
2907 * Do a simple existence test first; if we fail this, there's
2908 * no point trying anything else.
2910 * 0x80 is used as a nonsense port to prevent against false
2911 * positives due to ISA bus float. The assumption is that
2912 * 0x80 is a non-existent port; which should be safe since
2913 * include/asm/io.h also makes this assumption.
2915 scratch = serial_inp(info, UART_IER);
2916 serial_outp(info, UART_IER, 0);
2917 outb(0xff, 0x080);
2918 scratch2 = serial_inp(info, UART_IER);
2919 serial_outp(info, UART_IER, scratch);
2920 if (scratch2) {
2921 restore_flags(flags);
2922 return; /* We failed; there's nothing here */
2926 * Check to see if a UART is really there. Certain broken
2927 * internal modems based on the Rockwell chipset fail this
2928 * test, because they apparently don't implement the loopback
2929 * test mode. So this test is skipped on the COM 1 through
2930 * COM 4 ports. This *should* be safe, since no board
2931 * manufacturer would be stupid enough to design a board
2932 * that conflicts with COM 1-4 --- we hope!
2934 if (!(state->flags & ASYNC_SKIP_TEST)) {
2935 scratch = serial_inp(info, UART_MCR);
2936 serial_outp(info, UART_MCR, UART_MCR_LOOP | scratch);
2937 serial_outp(info, UART_MCR, UART_MCR_LOOP | 0x0A);
2938 status1 = serial_inp(info, UART_MSR) & 0xF0;
2939 serial_outp(info, UART_MCR, scratch);
2940 if (status1 != 0x90) {
2941 restore_flags(flags);
2942 return;
2946 scratch2 = serial_in(info, UART_LCR);
2947 serial_outp(info, UART_LCR, 0xBF); /* set up for StarTech test */
2948 serial_outp(info, UART_EFR, 0); /* EFR is the same as FCR */
2949 serial_outp(info, UART_LCR, 0);
2950 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
2951 scratch = serial_in(info, UART_IIR) >> 6;
2952 switch (scratch) {
2953 case 0:
2954 state->type = PORT_16450;
2955 break;
2956 case 1:
2957 state->type = PORT_UNKNOWN;
2958 break;
2959 case 2:
2960 state->type = PORT_16550;
2961 break;
2962 case 3:
2963 state->type = PORT_16550A;
2964 break;
2966 if (state->type == PORT_16550A) {
2967 /* Check for Startech UART's */
2968 serial_outp(info, UART_LCR, scratch2 | UART_LCR_DLAB);
2969 if (serial_in(info, UART_EFR) == 0) {
2970 state->type = PORT_16650;
2971 } else {
2972 serial_outp(info, UART_LCR, 0xBF);
2973 if (serial_in(info, UART_EFR) == 0)
2974 state->type = PORT_16650V2;
2977 if (state->type == PORT_16550A) {
2978 /* Check for TI 16750 */
2979 serial_outp(info, UART_LCR, scratch2 | UART_LCR_DLAB);
2980 serial_outp(info, UART_FCR,
2981 UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
2982 scratch = serial_in(info, UART_IIR) >> 5;
2983 if (scratch == 7) {
2984 serial_outp(info, UART_LCR, 0);
2985 scratch = serial_in(info, UART_IIR) >> 5;
2986 if (scratch == 6)
2987 state->type = PORT_16750;
2989 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
2991 serial_outp(info, UART_LCR, scratch2);
2992 if (state->type == PORT_16450) {
2993 scratch = serial_in(info, UART_SCR);
2994 serial_outp(info, UART_SCR, 0xa5);
2995 status1 = serial_in(info, UART_SCR);
2996 serial_outp(info, UART_SCR, 0x5a);
2997 status2 = serial_in(info, UART_SCR);
2998 serial_outp(info, UART_SCR, scratch);
3000 if ((status1 != 0xa5) || (status2 != 0x5a))
3001 state->type = PORT_8250;
3003 state->xmit_fifo_size = uart_config[state->type].dfl_xmit_fifo_size;
3005 if (state->type == PORT_UNKNOWN) {
3006 restore_flags(flags);
3007 return;
3010 request_region(info->port,8,"serial(auto)");
3013 * Reset the UART.
3015 #if defined(__alpha__) && !defined(CONFIG_PCI)
3017 * I wonder what DEC did to the OUT1 and OUT2 lines?
3018 * clearing them results in endless interrupts.
3020 serial_outp(info, UART_MCR, 0x0c);
3021 #else
3022 serial_outp(info, UART_MCR, 0x00);
3023 #endif
3024 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
3025 UART_FCR_CLEAR_XMIT));
3026 (void)serial_in(info, UART_RX);
3027 serial_outp(info, UART_IER, 0);
3029 restore_flags(flags);
3032 int register_serial(struct serial_struct *req);
3033 void unregister_serial(int line);
3035 EXPORT_SYMBOL(register_serial);
3036 EXPORT_SYMBOL(unregister_serial);
3039 * The serial driver boot-time initialization code!
3041 __initfunc(int rs_init(void))
3043 int i;
3044 struct serial_state * state;
3045 extern void atomwide_serial_init (void);
3046 extern void dualsp_serial_init (void);
3048 #ifdef CONFIG_ATOMWIDE_SERIAL
3049 atomwide_serial_init ();
3050 #endif
3051 #ifdef CONFIG_DUALSP_SERIAL
3052 dualsp_serial_init ();
3053 #endif
3055 init_bh(SERIAL_BH, do_serial_bh);
3056 timer_table[RS_TIMER].fn = rs_timer;
3057 timer_table[RS_TIMER].expires = 0;
3059 for (i = 0; i < NR_IRQS; i++) {
3060 IRQ_ports[i] = 0;
3061 IRQ_timeout[i] = 0;
3062 #ifdef CONFIG_SERIAL_MULTIPORT
3063 memset(&rs_multiport[i], 0,
3064 sizeof(struct rs_multiport_struct));
3065 #endif
3067 #ifdef CONFIG_SERIAL_CONSOLE
3069 * The interrupt of the serial console port
3070 * can't be shared.
3072 if (sercons.flags & CON_CONSDEV) {
3073 for(i = 0; i < NR_PORTS; i++)
3074 if (i != sercons.index &&
3075 rs_table[i].irq == rs_table[sercons.index].irq)
3076 rs_table[i].irq = 0;
3078 #endif
3079 show_serial_version();
3081 /* Initialize the tty_driver structure */
3083 memset(&serial_driver, 0, sizeof(struct tty_driver));
3084 serial_driver.magic = TTY_DRIVER_MAGIC;
3085 serial_driver.driver_name = "serial";
3086 serial_driver.name = "ttyS";
3087 serial_driver.major = TTY_MAJOR;
3088 serial_driver.minor_start = 64;
3089 serial_driver.num = NR_PORTS;
3090 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
3091 serial_driver.subtype = SERIAL_TYPE_NORMAL;
3092 serial_driver.init_termios = tty_std_termios;
3093 serial_driver.init_termios.c_cflag =
3094 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
3095 serial_driver.flags = TTY_DRIVER_REAL_RAW;
3096 serial_driver.refcount = &serial_refcount;
3097 serial_driver.table = serial_table;
3098 serial_driver.termios = serial_termios;
3099 serial_driver.termios_locked = serial_termios_locked;
3101 serial_driver.open = rs_open;
3102 serial_driver.close = rs_close;
3103 serial_driver.write = rs_write;
3104 serial_driver.put_char = rs_put_char;
3105 serial_driver.flush_chars = rs_flush_chars;
3106 serial_driver.write_room = rs_write_room;
3107 serial_driver.chars_in_buffer = rs_chars_in_buffer;
3108 serial_driver.flush_buffer = rs_flush_buffer;
3109 serial_driver.ioctl = rs_ioctl;
3110 serial_driver.throttle = rs_throttle;
3111 serial_driver.unthrottle = rs_unthrottle;
3112 serial_driver.send_xchar = rs_send_xchar;
3113 serial_driver.set_termios = rs_set_termios;
3114 serial_driver.stop = rs_stop;
3115 serial_driver.start = rs_start;
3116 serial_driver.hangup = rs_hangup;
3117 serial_driver.break_ctl = rs_break;
3118 serial_driver.wait_until_sent = rs_wait_until_sent;
3119 serial_driver.read_proc = rs_read_proc;
3122 * The callout device is just like normal device except for
3123 * major number and the subtype code.
3125 callout_driver = serial_driver;
3126 callout_driver.name = "cua";
3127 callout_driver.major = TTYAUX_MAJOR;
3128 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
3129 callout_driver.read_proc = 0;
3130 callout_driver.proc_entry = 0;
3132 if (tty_register_driver(&serial_driver))
3133 panic("Couldn't register serial driver\n");
3134 if (tty_register_driver(&callout_driver))
3135 panic("Couldn't register callout driver\n");
3137 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
3138 state->magic = SSTATE_MAGIC;
3139 state->line = i;
3140 state->type = PORT_UNKNOWN;
3141 state->custom_divisor = 0;
3142 state->close_delay = 5*HZ/10;
3143 state->closing_wait = 30*HZ;
3144 state->callout_termios = callout_driver.init_termios;
3145 state->normal_termios = serial_driver.init_termios;
3146 state->icount.cts = state->icount.dsr =
3147 state->icount.rng = state->icount.dcd = 0;
3148 state->icount.rx = state->icount.tx = 0;
3149 state->icount.frame = state->icount.parity = 0;
3150 state->icount.overrun = state->icount.brk = 0;
3151 state->irq = irq_cannonicalize(state->irq);
3152 if (check_region(state->port,8))
3153 continue;
3154 if (state->flags & ASYNC_BOOT_AUTOCONF)
3155 autoconfig(state);
3158 * Detect the IRQ only once every port is initialised,
3159 * because some 16450 do not reset to 0 the MCR register.
3161 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
3162 if (state->type == PORT_UNKNOWN)
3163 continue;
3164 if ( (state->flags & ASYNC_BOOT_AUTOCONF)
3165 && (state->flags & ASYNC_AUTO_IRQ)
3166 && (state->port != 0))
3167 state->irq = detect_uart_irq(state);
3168 printk(KERN_INFO "ttyS%02d%s at 0x%04x (irq = %d) is a %s\n",
3169 state->line,
3170 (state->flags & ASYNC_FOURPORT) ? " FourPort" : "",
3171 state->port, state->irq,
3172 uart_config[state->type].name);
3174 return 0;
3178 * register_serial and unregister_serial allows for serial ports to be
3179 * configured at run-time, to support PCMCIA modems.
3181 int register_serial(struct serial_struct *req)
3183 int i;
3184 unsigned long flags;
3185 struct serial_state *state;
3187 save_flags(flags);
3188 cli();
3189 for (i = 0; i < NR_PORTS; i++) {
3190 if (rs_table[i].port == req->port)
3191 break;
3193 if (i == NR_PORTS) {
3194 for (i = 0; i < NR_PORTS; i++)
3195 if ((rs_table[i].type == PORT_UNKNOWN) &&
3196 (rs_table[i].count == 0))
3197 break;
3199 if (i == NR_PORTS) {
3200 restore_flags(flags);
3201 return -1;
3203 state = &rs_table[i];
3204 if (rs_table[i].count) {
3205 restore_flags(flags);
3206 printk("Couldn't configure serial #%d (port=%d,irq=%d): "
3207 "device already open\n", i, req->port, req->irq);
3208 return -1;
3210 state->irq = req->irq;
3211 state->port = req->port;
3212 state->flags = req->flags;
3214 autoconfig(state);
3215 if (state->type == PORT_UNKNOWN) {
3216 restore_flags(flags);
3217 printk("register_serial(): autoconfig failed\n");
3218 return -1;
3220 restore_flags(flags);
3222 if ((state->flags & ASYNC_AUTO_IRQ) && (state->port != 0))
3223 state->irq = detect_uart_irq(state);
3225 printk(KERN_INFO "tty%02d at 0x%04x (irq = %d) is a %s\n",
3226 state->line, state->port, state->irq,
3227 uart_config[state->type].name);
3228 return state->line;
3231 void unregister_serial(int line)
3233 unsigned long flags;
3234 struct serial_state *state = &rs_table[line];
3236 save_flags(flags);
3237 cli();
3238 if (state->info && state->info->tty)
3239 tty_hangup(state->info->tty);
3240 state->type = PORT_UNKNOWN;
3241 printk(KERN_INFO "tty%02d unloaded\n", state->line);
3242 restore_flags(flags);
3245 #ifdef MODULE
3246 int init_module(void)
3248 return rs_init();
3251 void cleanup_module(void)
3253 unsigned long flags;
3254 int e1, e2;
3255 int i;
3257 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
3258 save_flags(flags);
3259 cli();
3260 timer_active &= ~(1 << RS_TIMER);
3261 timer_table[RS_TIMER].fn = NULL;
3262 timer_table[RS_TIMER].expires = 0;
3263 remove_bh(SERIAL_BH);
3264 if ((e1 = tty_unregister_driver(&serial_driver)))
3265 printk("SERIAL: failed to unregister serial driver (%d)\n",
3266 e1);
3267 if ((e2 = tty_unregister_driver(&callout_driver)))
3268 printk("SERIAL: failed to unregister callout driver (%d)\n",
3269 e2);
3270 restore_flags(flags);
3272 for (i = 0; i < NR_PORTS; i++) {
3273 if (rs_table[i].type != PORT_UNKNOWN)
3274 release_region(rs_table[i].port, 8);
3276 if (tmp_buf) {
3277 free_page((unsigned long) tmp_buf);
3278 tmp_buf = NULL;
3281 #endif /* MODULE */
3285 * ------------------------------------------------------------
3286 * Serial console driver
3287 * ------------------------------------------------------------
3289 #ifdef CONFIG_SERIAL_CONSOLE
3291 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
3294 * Wait for transmitter & holding register to empty
3296 static inline void wait_for_xmitr(struct serial_state *ser)
3298 int lsr;
3299 unsigned int tmout = 1000000;
3301 do {
3302 lsr = inb(ser->port + UART_LSR);
3303 if (--tmout == 0) break;
3304 } while ((lsr & BOTH_EMPTY) != BOTH_EMPTY);
3308 * Print a string to the serial port trying not to disturb
3309 * any possible real use of the port...
3311 static void serial_console_write(struct console *co, const char *s,
3312 unsigned count)
3314 struct serial_state *ser;
3315 int ier;
3316 unsigned i;
3318 ser = rs_table + co->index;
3320 * First save the IER then disable the interrupts
3322 ier = inb(ser->port + UART_IER);
3323 outb(0x00, ser->port + UART_IER);
3326 * Now, do each character
3328 for (i = 0; i < count; i++, s++) {
3329 wait_for_xmitr(ser);
3332 * Send the character out.
3333 * If a LF, also do CR...
3335 outb(*s, ser->port + UART_TX);
3336 if (*s == 10) {
3337 wait_for_xmitr(ser);
3338 outb(13, ser->port + UART_TX);
3343 * Finally, Wait for transmitter & holding register to empty
3344 * and restore the IER
3346 wait_for_xmitr(ser);
3347 outb(ier, ser->port + UART_IER);
3351 * Receive character from the serial port
3353 static int serial_console_wait_key(struct console *co)
3355 struct serial_state *ser;
3356 int ier;
3357 int lsr;
3358 int c;
3360 ser = rs_table + co->index;
3363 * First save the IER then disable the interrupts so
3364 * that the real driver for the port does not get the
3365 * character.
3367 ier = inb(ser->port + UART_IER);
3368 outb(0x00, ser->port + UART_IER);
3370 do {
3371 lsr = inb(ser->port + UART_LSR);
3372 } while (!(lsr & UART_LSR_DR));
3373 c = inb(ser->port + UART_RX);
3376 * Restore the interrupts
3378 outb(ier, ser->port + UART_IER);
3380 return c;
3383 static kdev_t serial_console_device(struct console *c)
3385 return MKDEV(TTY_MAJOR, 64 + c->index);
3389 * Setup initial baud/bits/parity. We do two things here:
3390 * - construct a cflag setting for the first rs_open()
3391 * - initialize the serial port
3392 * Return non-zero if we didn't find a serial port.
3394 __initfunc(static int serial_console_setup(struct console *co, char *options))
3396 struct serial_state *ser;
3397 unsigned cval;
3398 int baud = 9600;
3399 int bits = 8;
3400 int parity = 'n';
3401 int cflag = CREAD | HUPCL | CLOCAL;
3402 int quot = 0;
3403 char *s;
3405 if (options) {
3406 baud = simple_strtoul(options, NULL, 10);
3407 s = options;
3408 while(*s >= '0' && *s <= '9')
3409 s++;
3410 if (*s) parity = *s++;
3411 if (*s) bits = *s - '0';
3415 * Now construct a cflag setting.
3417 switch(baud) {
3418 case 1200:
3419 cflag |= B1200;
3420 break;
3421 case 2400:
3422 cflag |= B2400;
3423 break;
3424 case 4800:
3425 cflag |= B4800;
3426 break;
3427 case 19200:
3428 cflag |= B19200;
3429 break;
3430 case 38400:
3431 cflag |= B38400;
3432 break;
3433 case 57600:
3434 cflag |= B57600;
3435 break;
3436 case 115200:
3437 cflag |= B115200;
3438 break;
3439 case 9600:
3440 default:
3441 cflag |= B9600;
3442 break;
3444 switch(bits) {
3445 case 7:
3446 cflag |= CS7;
3447 break;
3448 default:
3449 case 8:
3450 cflag |= CS8;
3451 break;
3453 switch(parity) {
3454 case 'o': case 'O':
3455 cflag |= PARODD;
3456 break;
3457 case 'e': case 'E':
3458 cflag |= PARENB;
3459 break;
3461 co->cflag = cflag;
3464 * Divisor, bytesize and parity
3466 ser = rs_table + co->index;
3467 quot = ser->baud_base / baud;
3468 cval = cflag & (CSIZE | CSTOPB);
3469 #if defined(__powerpc__) || defined(__alpha__)
3470 cval >>= 8;
3471 #else /* !__powerpc__ && !__alpha__ */
3472 cval >>= 4;
3473 #endif /* !__powerpc__ && !__alpha__ */
3474 if (cflag & PARENB)
3475 cval |= UART_LCR_PARITY;
3476 if (!(cflag & PARODD))
3477 cval |= UART_LCR_EPAR;
3480 * Disable UART interrupts, set DTR and RTS high
3481 * and set speed.
3483 outb(cval | UART_LCR_DLAB, ser->port + UART_LCR); /* set DLAB */
3484 outb(quot & 0xff, ser->port + UART_DLL); /* LS of divisor */
3485 outb(quot >> 8, ser->port + UART_DLM); /* MS of divisor */
3486 outb(cval, ser->port + UART_LCR); /* reset DLAB */
3487 outb(0, ser->port + UART_IER);
3488 outb(UART_MCR_DTR | UART_MCR_RTS, ser->port + UART_MCR);
3491 * If we read 0xff from the LSR, there is no UART here.
3493 if (inb(ser->port + UART_LSR) == 0xff)
3494 return -1;
3495 return 0;
3498 static struct console sercons = {
3499 "ttyS",
3500 serial_console_write,
3501 NULL,
3502 serial_console_device,
3503 serial_console_wait_key,
3504 NULL,
3505 serial_console_setup,
3506 CON_PRINTBUFFER,
3509 NULL
3513 * Register console.
3515 __initfunc (long serial_console_init(long kmem_start, long kmem_end))
3517 register_console(&sercons);
3518 return kmem_start;
3520 #endif