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
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
35 * This module exports the following rs232 io functions:
41 * Serial driver configuration section. Here are the various options:
44 * Enables support for the venerable Bell Technologies
47 * CONFIG_SERIAL_MANY_PORTS
48 * Enables support for ports beyond the standard, stupid
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
65 #undef SERIAL_PARANOIA_CHECK
66 #define CONFIG_SERIAL_NOPAUSE_IO
67 #define SERIAL_DO_RESTART
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
81 #ifdef CONFIG_SERIAL_MULTIPORT
82 #ifndef CONFIG_SERIAL_SHARE_IRQ
83 #define CONFIG_SERIAL_SHARE_IRQ
88 #ifndef CONFIG_SERIAL_MANY_PORTS
89 #define CONFIG_SERIAL_MANY_PORTS
91 #ifndef CONFIG_SERIAL_SHARE_IRQ
92 #define CONFIG_SERIAL_SHARE_IRQ
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)
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>
145 #include <asm/system.h>
148 #include <asm/uaccess.h>
149 #include <asm/bitops.h>
150 #include <asm/serial.h>
153 #define _INLINE_ inline
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
];
176 static int IRQ_timeout
[NR_IRQS
];
177 #ifdef CONFIG_SERIAL_CONSOLE
178 static struct console sercons
;
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
190 static struct serial_uart_config uart_config
[] = {
195 { "16550A", 16, UART_CLEAR_FIFO
| UART_USE_FIFO
},
197 { "ST16650", 1, UART_CLEAR_FIFO
|UART_STARTECH
},
198 { "ST16650V2", 32, UART_CLEAR_FIFO
| UART_USE_FIFO
|
200 { "TI16750", 64, UART_CLEAR_FIFO
| UART_USE_FIFO
},
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
];
215 #define MIN(a,b) ((a) < (b) ? (a) : (b))
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";
240 printk(badinfo
, kdevname(device
), routine
);
243 if (info
->magic
!= SERIAL_MAGIC
) {
244 printk(badmagic
, kdevname(device
), routine
);
251 static inline unsigned int serial_in(struct async_struct
*info
, int offset
)
255 outb(info
->hub6
- 1 + offset
, info
->port
);
256 return inb(info
->port
+1);
259 return inb(info
->port
+ offset
);
262 static inline unsigned int serial_inp(struct async_struct
*info
, int offset
)
266 outb(info
->hub6
- 1 + offset
, info
->port
);
267 return inb_p(info
->port
+1);
270 #ifdef CONFIG_SERIAL_NOPAUSE_IO
271 return inb(info
->port
+ offset
);
273 return inb_p(info
->port
+ offset
);
277 static inline void serial_out(struct async_struct
*info
, int offset
, int value
)
281 outb(info
->hub6
- 1 + offset
, info
->port
);
282 outb(value
, info
->port
+1);
285 outb(value
, info
->port
+offset
);
288 static inline void serial_outp(struct async_struct
*info
, int offset
,
293 outb(info
->hub6
- 1 + offset
, info
->port
);
294 outb_p(value
, info
->port
+1);
297 #ifdef CONFIG_SERIAL_NOPAUSE_IO
298 outb(value
, info
->port
+offset
);
300 outb_p(value
, info
->port
+offset
);
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
;
317 if (serial_paranoia_check(info
, tty
->device
, "rs_stop"))
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
;
333 if (serial_paranoia_check(info
, tty
->device
, "rs_start"))
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
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
,
372 info
->event
|= 1 << event
;
373 queue_task(&info
->tqueue
, &tq_serial
);
377 static _INLINE_
void receive_chars(struct async_struct
*info
,
380 struct tty_struct
*tty
= info
->tty
;
383 struct async_icount
*icount
;
385 icount
= &info
->state
->icount
;
387 ch
= serial_inp(info
, UART_RX
);
388 if (tty
->flip
.count
>= TTY_FLIPBUF_SIZE
)
390 *tty
->flip
.char_buf_ptr
= ch
;
393 #ifdef SERIAL_DEBUG_INTR
394 printk("DR%02x:%02x...", ch
, *status
);
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
);
405 } else if (*status
& UART_LSR_PE
)
407 else if (*status
& UART_LSR_FE
)
409 if (*status
& UART_LSR_OE
)
413 * Now check to see if character should be
414 * ignored, and mask off conditions which
417 if (*status
& info
->ignore_status_mask
) {
422 *status
&= info
->read_status_mask
;
424 if (*status
& (UART_LSR_BI
)) {
425 #ifdef SERIAL_DEBUG_INTR
426 printk("handling break....");
428 *tty
->flip
.flag_buf_ptr
= TTY_BREAK
;
429 if (info
->flags
& ASYNC_SAK
)
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
) {
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
++;
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
)
463 serial_outp(info
, UART_TX
, info
->x_char
);
464 info
->state
->icount
.tx
++;
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
);
477 count
= info
->xmit_fifo_size
;
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)
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
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
)
504 struct async_icount
*icount
;
506 status
= serial_in(info
, UART_MSR
);
508 if (status
& UART_MSR_ANY_DELTA
) {
509 icount
= &info
->state
->icount
;
510 /* update input line counters */
511 if (status
& UART_MSR_TERI
)
513 if (status
& UART_MSR_DDSR
)
515 if (status
& UART_MSR_DDCD
) {
517 #ifdef CONFIG_HARD_PPS
518 if ((info
->flags
& ASYNC_HARDPPS_CD
) &&
519 (status
& UART_MSR_DCD
))
523 if (status
& UART_MSR_DCTS
)
525 wake_up_interruptible(&info
->delta_msr_wait
);
528 if ((info
->flags
& ASYNC_CHECK_CD
) && (status
& UART_MSR_DDCD
)) {
529 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
530 printk("ttys%d CD now %s...", info
->line
,
531 (status
& UART_MSR_DCD
) ? "on" : "off");
533 if (status
& UART_MSR_DCD
)
534 wake_up_interruptible(&info
->open_wait
);
535 else if (!((info
->flags
& ASYNC_CALLOUT_ACTIVE
) &&
536 (info
->flags
& ASYNC_CALLOUT_NOHUP
))) {
537 #ifdef SERIAL_DEBUG_OPEN
538 printk("doing serial hangup...");
541 tty_hangup(info
->tty
);
544 if (info
->flags
& ASYNC_CTS_FLOW
) {
545 if (info
->tty
->hw_stopped
) {
546 if (status
& UART_MSR_CTS
) {
547 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
548 printk("CTS tx start...");
550 info
->tty
->hw_stopped
= 0;
551 info
->IER
|= UART_IER_THRI
;
552 serial_out(info
, UART_IER
, info
->IER
);
553 rs_sched_event(info
, RS_EVENT_WRITE_WAKEUP
);
557 if (!(status
& UART_MSR_CTS
)) {
558 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
559 printk("CTS tx stop...");
561 info
->tty
->hw_stopped
= 1;
562 info
->IER
&= ~UART_IER_THRI
;
563 serial_out(info
, UART_IER
, info
->IER
);
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
)
576 struct async_struct
* info
;
577 int pass_counter
= 0;
578 struct async_struct
*end_mark
= 0;
579 #ifdef CONFIG_SERIAL_MULTIPORT
581 struct rs_multiport_struct
*multi
;
584 #ifdef SERIAL_DEBUG_INTR
585 printk("rs_interrupt(%d)...", irq
);
588 info
= IRQ_ports
[irq
];
592 #ifdef CONFIG_SERIAL_MULTIPORT
593 multi
= &rs_multiport
[irq
];
594 if (multi
->port_monitor
)
595 first_multi
= inb(multi
->port_monitor
);
600 (serial_in(info
, UART_IIR
) & UART_IIR_NO_INT
)) {
607 info
->last_active
= jiffies
;
609 status
= serial_inp(info
, UART_LSR
);
610 #ifdef SERIAL_DEBUG_INTR
611 printk("status = %x...", status
);
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);
620 info
= info
->next_port
;
622 info
= IRQ_ports
[irq
];
623 if (pass_counter
++ > RS_ISR_PASS_LIMIT
) {
625 printk("rs loop break\n");
627 break; /* Prevent infinite loops */
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
));
638 #ifdef SERIAL_DEBUG_INTR
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
)
651 int pass_counter
= 0;
652 struct async_struct
* info
;
653 #ifdef CONFIG_SERIAL_MULTIPORT
655 struct rs_multiport_struct
*multi
;
658 #ifdef SERIAL_DEBUG_INTR
659 printk("rs_interrupt_single(%d)...", irq
);
662 info
= IRQ_ports
[irq
];
663 if (!info
|| !info
->tty
)
666 #ifdef CONFIG_SERIAL_MULTIPORT
667 multi
= &rs_multiport
[irq
];
668 if (multi
->port_monitor
)
669 first_multi
= inb(multi
->port_monitor
);
673 status
= serial_inp(info
, UART_LSR
);
674 #ifdef SERIAL_DEBUG_INTR
675 printk("status = %x...", status
);
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
) {
684 printk("rs_single loop break.\n");
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
));
696 #ifdef SERIAL_DEBUG_INTR
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
)
708 struct async_struct
* info
;
709 int pass_counter
= 0;
711 struct rs_multiport_struct
*multi
;
713 #ifdef SERIAL_DEBUG_INTR
714 printk("rs_interrupt_multi(%d)...", irq
);
717 info
= IRQ_ports
[irq
];
720 multi
= &rs_multiport
[irq
];
722 /* Should never happen */
723 printk("rs_interrupt_multi: NULL port1!\n");
726 if (multi
->port_monitor
)
727 first_multi
= inb(multi
->port_monitor
);
731 (serial_in(info
, UART_IIR
) & UART_IIR_NO_INT
))
734 info
->last_active
= jiffies
;
736 status
= serial_inp(info
, UART_LSR
);
737 #ifdef SERIAL_DEBUG_INTR
738 printk("status = %x...", status
);
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);
747 info
= info
->next_port
;
751 info
= IRQ_ports
[irq
];
752 if (pass_counter
++ > RS_ISR_PASS_LIMIT
) {
754 printk("rs_multi loop break\n");
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
)
766 if ((inb(multi
->port2
) & multi
->mask2
) != multi
->match2
)
770 if ((inb(multi
->port3
) & multi
->mask3
) != multi
->match3
)
774 if ((inb(multi
->port4
) & multi
->mask4
) == multi
->match4
)
778 #ifdef SERIAL_DEBUG_INTR
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
;
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
828 static void rs_timer(void)
830 static unsigned long last_strobe
= 0;
831 struct async_struct
*info
;
835 if ((jiffies
- last_strobe
) >= RS_STROBE_TIME
) {
836 for (i
=1; i
< NR_IRQS
; i
++) {
840 save_flags(flags
); cli();
841 #ifdef CONFIG_SERIAL_SHARE_IRQ
842 if (info
->next_port
) {
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
;
849 #ifdef CONFIG_SERIAL_MULTIPORT
850 if (rs_multiport
[i
].port1
)
851 rs_interrupt_multi(i
, NULL
, NULL
);
854 rs_interrupt(i
, NULL
, NULL
);
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
;
866 save_flags(flags
); cli();
867 #ifdef CONFIG_SERIAL_SHARE_IRQ
868 rs_interrupt(0, NULL
, NULL
);
870 rs_interrupt_single(0, NULL
, NULL
);
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
];
899 IRQ_timeout
[irq
] = 60*HZ
;
903 if (info
->timeout
< timeout
)
904 timeout
= info
->timeout
;
905 info
= info
->next_port
;
908 timeout
= timeout
/ 2;
909 IRQ_timeout
[irq
] = timeout
? timeout
: 1;
912 static int startup(struct async_struct
* info
)
916 void (*handler
)(int, void *, struct pt_regs
*);
917 struct serial_state
*state
= info
->state
;
919 #ifdef CONFIG_SERIAL_MANY_PORTS
923 page
= get_free_page(GFP_KERNEL
);
927 save_flags(flags
); cli();
929 if (info
->flags
& ASYNC_INITIALIZED
) {
934 if (!state
->port
|| !state
->type
) {
936 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
943 info
->xmit_buf
= (unsigned char *) page
;
945 #ifdef SERIAL_DEBUG_OPEN
946 printk("starting up ttys%d (irq %d)...", info
->line
, state
->irq
);
949 if (uart_config
[info
->state
->type
].flags
& UART_STARTECH
) {
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
) {
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
976 if (serial_inp(info
, UART_LSR
) == 0xff) {
977 if (capable(CAP_SYS_ADMIN
)) {
979 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
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
;
998 handler
= rs_interrupt
;
1002 #endif /* CONFIG_SERIAL_SHARE_IRQ */
1004 handler
= rs_interrupt_single
;
1006 retval
= request_irq(state
->irq
, handler
, IRQ_T(info
),
1009 if (capable(CAP_SYS_ADMIN
)) {
1011 set_bit(TTY_IO_ERROR
,
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 */
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
;
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
;
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;
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
);
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
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
1115 info
->flags
|= ASYNC_INITIALIZED
;
1116 restore_flags(flags
);
1120 restore_flags(flags
);
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
;
1134 if (!(info
->flags
& ASYNC_INITIALIZED
))
1137 state
= info
->state
;
1139 #ifdef SERIAL_DEBUG_OPEN
1140 printk("Shutting down serial port %d (irq %d)....", info
->line
,
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
;
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
);
1174 printk("serial shutdown: request_irq: error %d"
1175 " Couldn't reacquire IRQ.\n", retval
);
1177 free_irq(state
->irq
, NULL
);
1180 if (info
->xmit_buf
) {
1181 free_page((unsigned long) info
->xmit_buf
);
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
;
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
;
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 */
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;
1242 unsigned long flags
;
1244 if (!info
->tty
|| !info
->tty
->termios
)
1246 cflag
= info
->tty
->termios
->c_cflag
;
1247 if (!(port
= info
->port
))
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
) {
1263 if (cflag
& PARENB
) {
1264 cval
|= UART_LCR_PARITY
;
1267 if (!(cflag
& PARODD
))
1268 cval
|= UART_LCR_EPAR
;
1271 cval
|= UART_LCR_SPAR
;
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
;
1282 /* Special case since 134 is really 134.5 */
1283 quot
= (2*baud_base
/ 269);
1285 quot
= baud_base
/ baud
;
1287 /* If the quotient is ever zero, default to 9600 bps */
1289 quot
= baud_base
/ 9600;
1291 info
->timeout
= ((info
->xmit_fifo_size
*HZ
*bits
*quot
) / baud_base
);
1292 info
->timeout
+= HZ
/50; /* Add .02 seconds of slop */
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
;
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
;
1312 info
->flags
&= ~ASYNC_CTS_FLOW
;
1314 info
->flags
&= ~ASYNC_CHECK_CD
;
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"))
1377 if (!tty
|| !info
->xmit_buf
)
1380 save_flags(flags
); cli();
1381 if (info
->xmit_cnt
>= SERIAL_XMIT_SIZE
- 1) {
1382 restore_flags(flags
);
1386 info
->xmit_buf
[info
->xmit_head
++] = ch
;
1387 info
->xmit_head
&= SERIAL_XMIT_SIZE
-1;
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"))
1400 if (info
->xmit_cnt
<= 0 || tty
->stopped
|| tty
->hw_stopped
||
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
)
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"))
1420 if (!tty
|| !info
->xmit_buf
|| !tmp_buf
)
1428 MIN(SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1,
1429 SERIAL_XMIT_SIZE
- info
->xmit_head
));
1433 c
-= copy_from_user(tmp_buf
, buf
, c
);
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
);
1456 MIN(SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1,
1457 SERIAL_XMIT_SIZE
- info
->xmit_head
));
1459 restore_flags(flags
);
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
);
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
);
1480 static int rs_write_room(struct tty_struct
*tty
)
1482 struct async_struct
*info
= (struct async_struct
*)tty
->driver_data
;
1485 if (serial_paranoia_check(info
, tty
->device
, "rs_write_room"))
1487 ret
= SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1;
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"))
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"))
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
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"))
1531 /* Make sure transmit interrupts are on */
1532 info
->IER
|= UART_IER_THRI
;
1533 serial_out(info
, UART_IER
, info
->IER
);
1538 * ------------------------------------------------------------
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
1552 printk("throttle %s: %d....\n", tty_name(tty
, buf
),
1553 tty
->ldisc
.chars_in_buffer(tty
));
1556 if (serial_paranoia_check(info
, tty
->device
, "rs_throttle"))
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
1577 printk("unthrottle %s: %d....\n", tty_name(tty
, buf
),
1578 tty
->ldisc
.chars_in_buffer(tty
));
1581 if (serial_paranoia_check(info
, tty
->device
, "rs_unthrottle"))
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
;
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
)))
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
;
1636 if (copy_from_user(&new_serial
,new_info
,sizeof(new_serial
)))
1638 state
= info
->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
)))
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
)) {
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
) &&
1682 if ((change_port
|| change_irq
) && (state
->count
> 1))
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.
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)");
1719 if (!state
->port
|| !state
->type
)
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;
1736 retval
= startup(info
);
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)
1778 | ((control
& UART_MCR_OUT1
) ? TIOCM_OUT1
: 0)
1779 | ((control
& UART_MCR_OUT2
) ? TIOCM_OUT2
: 0)
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
)
1793 unsigned long flags
;
1795 error
= get_user(arg
, value
);
1800 if (arg
& TIOCM_RTS
)
1801 info
->MCR
|= UART_MCR_RTS
;
1802 if (arg
& TIOCM_DTR
)
1803 info
->MCR
|= UART_MCR_DTR
;
1805 if (arg
& TIOCM_OUT1
)
1806 info
->MCR
|= UART_MCR_OUT1
;
1807 if (arg
& TIOCM_OUT2
)
1808 info
->MCR
|= UART_MCR_OUT2
;
1812 if (arg
& TIOCM_RTS
)
1813 info
->MCR
&= ~UART_MCR_RTS
;
1814 if (arg
& TIOCM_DTR
)
1815 info
->MCR
&= ~UART_MCR_DTR
;
1817 if (arg
& TIOCM_OUT1
)
1818 info
->MCR
&= ~UART_MCR_OUT1
;
1819 if (arg
& TIOCM_OUT2
)
1820 info
->MCR
&= ~UART_MCR_OUT2
;
1824 info
->MCR
= ((info
->MCR
& ~(UART_MCR_RTS
|
1830 | ((arg
& TIOCM_RTS
) ? UART_MCR_RTS
: 0)
1832 | ((arg
& TIOCM_OUT1
) ? UART_MCR_OUT1
: 0)
1833 | ((arg
& TIOCM_OUT2
) ? UART_MCR_OUT2
: 0)
1835 | ((arg
& TIOCM_DTR
) ? UART_MCR_DTR
: 0));
1840 save_flags(flags
); cli();
1841 serial_out(info
, UART_MCR
, info
->MCR
);
1842 restore_flags(flags
);
1846 static int do_autoconfig(struct async_struct
* info
)
1850 if (!capable(CAP_SYS_ADMIN
))
1853 if (info
->state
->count
> 1)
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
);
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"))
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
);
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
)))
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
;
1935 void (*handler
)(int, void *, struct pt_regs
*);
1937 if (!capable(CAP_SYS_ADMIN
))
1939 state
= info
->state
;
1941 if (copy_from_user(&new_multi
, in_multi
,
1942 sizeof(struct serial_multiport_struct
)))
1945 if (new_multi
.irq
!= state
->irq
|| state
->irq
== 0 ||
1946 !IRQ_ports
[state
->irq
])
1949 multi
= &rs_multiport
[state
->irq
];
1950 was_multi
= (multi
->port1
!= 0);
1952 multi
->port_monitor
= new_multi
.port_monitor
;
1955 release_region(multi
->port1
,1);
1956 multi
->port1
= new_multi
.port1
;
1957 multi
->mask1
= new_multi
.mask1
;
1958 multi
->match1
= new_multi
.match1
;
1960 request_region(multi
->port1
,1,"serial(multiport1)");
1963 release_region(multi
->port2
,1);
1964 multi
->port2
= new_multi
.port2
;
1965 multi
->mask2
= new_multi
.mask2
;
1966 multi
->match2
= new_multi
.match2
;
1968 request_region(multi
->port2
,1,"serial(multiport2)");
1971 release_region(multi
->port3
,1);
1972 multi
->port3
= new_multi
.port3
;
1973 multi
->mask3
= new_multi
.mask3
;
1974 multi
->match3
= new_multi
.match3
;
1976 request_region(multi
->port3
,1,"serial(multiport3)");
1979 release_region(multi
->port4
,1);
1980 multi
->port4
= new_multi
.port4
;
1981 multi
->mask4
= new_multi
.mask4
;
1982 multi
->match4
= new_multi
.match4
;
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
);
1992 handler
= rs_interrupt_multi
;
1994 handler
= rs_interrupt
;
1996 retval
= request_irq(state
->irq
, handler
, IRQ_T(info
),
1999 printk("Couldn't reallocate serial interrupt "
2008 static int rs_ioctl(struct tty_struct
*tty
, struct file
* file
,
2009 unsigned int cmd
, unsigned long arg
)
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"))
2020 if ((cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCSSERIAL
) &&
2021 (cmd
!= TIOCSERCONFIG
) && (cmd
!= TIOCSERGSTRUCT
) &&
2022 (cmd
!= TIOCMIWAIT
) && (cmd
!= TIOCGICOUNT
)) {
2023 if (tty
->flags
& (1 << TTY_IO_ERROR
))
2029 return get_modem_info(info
, (unsigned int *) arg
);
2033 return set_modem_info(info
, cmd
, (unsigned int *) arg
);
2035 return get_serial_info(info
,
2036 (struct serial_struct
*) arg
);
2038 return set_serial_info(info
,
2039 (struct serial_struct
*) arg
);
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
)))
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
);
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
2068 save_flags(flags
); cli();
2069 /* note the counters on entry */
2070 cprev
= info
->state
->icount
;
2071 restore_flags(flags
);
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
)) ) {
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.
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
;
2130 /* "setserial -W" is called in Debian boot */
2131 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
2135 return -ENOIOCTLCMD
;
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
)))
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;
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
);
2195 * ------------------------------------------------------------
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"))
2213 state
= info
->state
;
2215 save_flags(flags
); cli();
2217 if (tty_hung_up_p(filp
)) {
2218 DBG_CNT("before DEC-hung");
2220 restore_flags(flags
);
2224 #ifdef SERIAL_DEBUG_OPEN
2225 printk("rs_close ttys%d, count = %d\n", info
->line
, state
->count
);
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
);
2239 if (--state
->count
< 0) {
2240 printk("rs_close: bad serial port count for ttys%d: %d\n",
2241 info
->line
, state
->count
);
2245 DBG_CNT("before DEC-2");
2247 restore_flags(flags
);
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.
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
);
2284 if (tty
->driver
.flush_buffer
)
2285 tty
->driver
.flush_buffer(tty
);
2286 if (tty
->ldisc
.flush_buffer
)
2287 tty
->ldisc
.flush_buffer(tty
);
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
|
2300 wake_up_interruptible(&info
->close_wait
);
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
;
2314 if (serial_paranoia_check(info
, tty
->device
, "rs_wait_until_sent"))
2317 if (info
->state
->type
== PORT_UNKNOWN
)
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
2332 char_time
= (info
->timeout
- HZ
/50) / info
->xmit_fifo_size
;
2333 char_time
= char_time
/ 5;
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
);
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
);
2346 current
->state
= TASK_INTERRUPTIBLE
;
2347 current
->counter
= 0; /* make us low-priority */
2348 schedule_timeout(char_time
);
2349 if (signal_pending(current
))
2351 if (timeout
&& ((orig_jiffies
+ timeout
) < jiffies
))
2354 current
->state
= TASK_RUNNING
;
2355 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2356 printk("lsr = %d (jiff=%lu)...done\n", lsr
, jiffies
);
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"))
2371 state
= info
->state
;
2373 rs_flush_buffer(tty
);
2377 info
->flags
&= ~(ASYNC_NORMAL_ACTIVE
|ASYNC_CALLOUT_ACTIVE
);
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
;
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
);
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
)
2419 if ((info
->flags
& ASYNC_CALLOUT_ACTIVE
) &&
2420 (info
->flags
& ASYNC_SESSION_LOCKOUT
) &&
2421 (info
->session
!= current
->session
))
2423 if ((info
->flags
& ASYNC_CALLOUT_ACTIVE
) &&
2424 (info
->flags
& ASYNC_PGRP_LOCKOUT
) &&
2425 (info
->pgrp
!= current
->pgrp
))
2427 info
->flags
|= ASYNC_CALLOUT_ACTIVE
;
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
)
2439 info
->flags
|= ASYNC_NORMAL_ACTIVE
;
2443 if (info
->flags
& ASYNC_CALLOUT_ACTIVE
) {
2444 if (state
->normal_termios
.c_cflag
& CLOCAL
)
2447 if (tty
->termios
->c_cflag
& CLOCAL
)
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.
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
);
2464 save_flags(flags
); cli();
2465 if (!tty_hung_up_p(filp
)) {
2469 restore_flags(flags
);
2470 info
->blocked_open
++;
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
)
2486 retval
= -ERESTARTSYS
;
2492 if (!(info
->flags
& ASYNC_CALLOUT_ACTIVE
) &&
2493 !(info
->flags
& ASYNC_CLOSING
) &&
2494 (do_clocal
|| (serial_in(info
, UART_MSR
) &
2497 if (signal_pending(current
)) {
2498 retval
= -ERESTARTSYS
;
2501 #ifdef SERIAL_DEBUG_OPEN
2502 printk("block_til_ready blocking: ttys%d, count = %d\n",
2503 info
->line
, state
->count
);
2507 current
->state
= TASK_RUNNING
;
2508 remove_wait_queue(&info
->open_wait
, &wait
);
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
);
2518 info
->flags
|= ASYNC_NORMAL_ACTIVE
;
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
;
2530 *ret_info
= sstate
->info
;
2533 info
= kmalloc(sizeof(struct async_struct
), GFP_KERNEL
);
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
;
2544 info
->tqueue
.routine
= do_softint
;
2545 info
->tqueue
.data
= info
;
2546 info
->state
= sstate
;
2548 kfree_s(info
, sizeof(struct async_struct
));
2549 *ret_info
= sstate
->info
;
2552 *ret_info
= sstate
->info
= info
;
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
;
2569 line
= MINOR(tty
->device
) - tty
->driver
.minor_start
;
2570 if ((line
< 0) || (line
>= NR_PORTS
)) {
2574 retval
= get_async_struct(line
, &info
);
2579 tty
->driver_data
= info
;
2581 if (serial_paranoia_check(info
, tty
->device
, "rs_open")) {
2586 #ifdef SERIAL_DEBUG_OPEN
2587 printk("rs_open %s%d, count = %d\n", tty
->driver
.name
, info
->line
,
2588 info
->state
->count
);
2590 info
->tty
->low_latency
= (info
->flags
& ASYNC_LOW_LATENCY
) ? 1 : 0;
2593 page
= get_free_page(GFP_KERNEL
);
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
);
2612 #ifdef SERIAL_DO_RESTART
2613 return ((info
->flags
& ASYNC_HUP_NOTIFY
) ?
2614 -EAGAIN
: -ERESTARTSYS
);
2621 * Start up serial port
2623 retval
= startup(info
);
2629 retval
= block_til_ready(tty
, filp
, info
);
2632 #ifdef SERIAL_DEBUG_OPEN
2633 printk("rs_open returning after block_til_ready with %d\n",
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
;
2644 *tty
->termios
= info
->state
->callout_termios
;
2647 #ifdef CONFIG_SERIAL_CONSOLE
2648 if (sercons
.cflag
&& sercons
.index
== line
) {
2649 tty
->termios
->c_cflag
= sercons
.cflag
;
2654 info
->session
= current
->session
;
2655 info
->pgrp
= current
->pgrp
;
2657 #ifdef SERIAL_DEBUG_OPEN
2658 printk("rs_open ttys%d successful...", info
->line
);
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
;
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");
2684 * Figure out the current RS-232 lines
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
;
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
);
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");
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);
2742 int rs_read_proc(char *page
, char **start
, off_t off
, int count
,
2743 int *eof
, void *data
)
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
]);
2752 if (len
+begin
> off
+count
)
2754 if (len
+begin
< off
) {
2761 if (off
>= len
+begin
)
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
2780 static _INLINE_
void show_serial_version(void)
2782 printk(KERN_INFO
"%s version %s with", serial_name
, serial_version
);
2787 #ifdef CONFIG_SERIAL_MANY_PORTS
2788 printk(" MANY_PORTS");
2791 #ifdef CONFIG_SERIAL_MULTIPORT
2792 printk(" MULTIPORT");
2795 #ifdef CONFIG_SERIAL_SHARE_IRQ
2796 printk(" SHARE_IRQ");
2799 #ifdef CONFIG_SERIAL_DETECT_IRQ
2800 printk(" DETECT_IRQ");
2804 printk(" enabled\n");
2806 printk(" no serial options enabled\n");
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
)
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
);
2836 scr_info
.magic
= SERIAL_MAGIC
;
2837 scr_info
.port
= state
->port
;
2838 scr_info
.flags
= state
->flags
;
2840 scr_info
.hub6
= state
->hub6
;
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);
2852 if (state
->flags
& ASYNC_FOURPORT
) {
2853 serial_outp(&scr_info
, UART_MCR
,
2854 UART_MCR_DTR
| UART_MCR_RTS
);
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);
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
);
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
;
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
;
2901 info
->hub6
= state
->hub6
;
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);
2918 scratch2
= serial_inp(info
, UART_IER
);
2919 serial_outp(info
, UART_IER
, scratch
);
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
);
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;
2954 state
->type
= PORT_16450
;
2957 state
->type
= PORT_UNKNOWN
;
2960 state
->type
= PORT_16550
;
2963 state
->type
= PORT_16550A
;
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
;
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;
2984 serial_outp(info
, UART_LCR
, 0);
2985 scratch
= serial_in(info
, UART_IIR
) >> 5;
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
);
3010 request_region(info
->port
,8,"serial(auto)");
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);
3022 serial_outp(info
, UART_MCR
, 0x00);
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))
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 ();
3051 #ifdef CONFIG_DUALSP_SERIAL
3052 dualsp_serial_init ();
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
++) {
3062 #ifdef CONFIG_SERIAL_MULTIPORT
3063 memset(&rs_multiport
[i
], 0,
3064 sizeof(struct rs_multiport_struct
));
3067 #ifdef CONFIG_SERIAL_CONSOLE
3069 * The interrupt of the serial console port
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;
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
;
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))
3154 if (state
->flags
& ASYNC_BOOT_AUTOCONF
)
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
)
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",
3170 (state
->flags
& ASYNC_FOURPORT
) ? " FourPort" : "",
3171 state
->port
, state
->irq
,
3172 uart_config
[state
->type
].name
);
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
)
3184 unsigned long flags
;
3185 struct serial_state
*state
;
3189 for (i
= 0; i
< NR_PORTS
; i
++) {
3190 if (rs_table
[i
].port
== req
->port
)
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))
3199 if (i
== NR_PORTS
) {
3200 restore_flags(flags
);
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
);
3210 state
->irq
= req
->irq
;
3211 state
->port
= req
->port
;
3212 state
->flags
= req
->flags
;
3215 if (state
->type
== PORT_UNKNOWN
) {
3216 restore_flags(flags
);
3217 printk("register_serial(): autoconfig failed\n");
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
);
3231 void unregister_serial(int line
)
3233 unsigned long flags
;
3234 struct serial_state
*state
= &rs_table
[line
];
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
);
3246 int init_module(void)
3251 void cleanup_module(void)
3253 unsigned long flags
;
3257 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
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",
3267 if ((e2
= tty_unregister_driver(&callout_driver
)))
3268 printk("SERIAL: failed to unregister callout driver (%d)\n",
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);
3277 free_page((unsigned long) tmp_buf
);
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
)
3299 unsigned int tmout
= 1000000;
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
,
3314 struct serial_state
*ser
;
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
);
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
;
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
3367 ier
= inb(ser
->port
+ UART_IER
);
3368 outb(0x00, ser
->port
+ UART_IER
);
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
);
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
;
3401 int cflag
= CREAD
| HUPCL
| CLOCAL
;
3406 baud
= simple_strtoul(options
, NULL
, 10);
3408 while(*s
>= '0' && *s
<= '9')
3410 if (*s
) parity
= *s
++;
3411 if (*s
) bits
= *s
- '0';
3415 * Now construct a cflag setting.
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__)
3471 #else /* !__powerpc__ && !__alpha__ */
3473 #endif /* !__powerpc__ && !__alpha__ */
3475 cval
|= UART_LCR_PARITY
;
3476 if (!(cflag
& PARODD
))
3477 cval
|= UART_LCR_EPAR
;
3480 * Disable UART interrupts, set DTR and RTS high
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)
3498 static struct console sercons
= {
3500 serial_console_write
,
3502 serial_console_device
,
3503 serial_console_wait_key
,
3505 serial_console_setup
,
3515 __initfunc (long serial_console_init(long kmem_start
, long kmem_end
))
3517 register_console(&sercons
);