2 * linux/kernel/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 * This module exports the following rs232 io functions:
17 * int rs_open(struct tty_struct * tty, struct file * filp)
20 #include <linux/errno.h>
21 #include <linux/signal.h>
22 #include <linux/sched.h>
23 #include <linux/timer.h>
24 #include <linux/tty.h>
25 #include <linux/serial.h>
26 #include <linux/interrupt.h>
27 #include <linux/config.h>
28 #include <linux/major.h>
29 #include <linux/string.h>
30 #include <linux/fcntl.h>
31 #include <linux/ptrace.h>
33 #include <asm/system.h>
35 #include <asm/segment.h>
36 #include <asm/bitops.h>
39 * Serial driver configuration section. Here are the various options:
42 * Enables automatic IRQ detection. I've put in some
43 * fixes to this which should make this work much more
44 * cleanly than it used to in 0.98pl2-6. It should be
45 * much less vulnerable to false IRQs now.
48 * Enables support for the AST Fourport serial port.
51 * Enables support for the Accent Async 4 port serial
55 * Enables support for the venerable Bell Technologies
62 * rs_event - Bitfield of serial lines that events pending
63 * to be processed at the next clock tick.
64 * IRQ_timeout - How long the timeout should be for each IRQ
65 * should be after the IRQ has been active.
66 * IRQ_timer - Array of timeout values for each interrupt IRQ.
67 * This is based on jiffies; not offsets.
69 * We assume here that int's are 32 bits, so an array of two gives us
70 * 64 lines, which is the maximum we can support.
72 static int rs_event
[2];
74 static struct async_struct
*IRQ_ports
[16];
75 static int IRQ_active
;
76 static unsigned long IRQ_timer
[16];
77 static int IRQ_timeout
[16];
78 static volatile int rs_irq_triggered
;
79 static volatile int rs_triggered
;
80 static int rs_wild_int_mask
;
82 static void autoconfig(struct async_struct
* info
);
83 static void change_speed(unsigned int line
);
86 * This assumes you have a 1.8432 MHz clock for your UART.
88 * It'd be nice if someone built a serial card with a 24.576 MHz
89 * clock, since the 16550A is capable of handling a top speed of 1.5
90 * megabits/second; but this requires the faster clock.
92 #define BASE_BAUD ( 1843200 / 16 )
94 #ifdef CONFIG_AUTO_IRQ
95 #define AUTO_IRQ_FLAG ASYNC_AUTO_IRQ
97 #define AUTO_IRQ_FLAG 0
100 /* Standard COM flags (except for COM4, because of the 8514 problem) */
101 #define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST | AUTO_IRQ_FLAG)
102 #define STD_COM4_FLAGS (ASYNC_BOOT_AUTOCONF | AUTO_IRQ_FLAG)
104 #ifdef CONFIG_AST_FOURPORT
105 #define FOURPORT_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_FOURPORT | AUTO_IRQ_FLAG)
107 #define FOURPORT_FLAGS (ASYNC_FOURPORT | AUTO_IRQ_FLAG)
110 #ifdef CONFIG_ACCENT_ASYNC
111 #define ACCENT_FLAGS (ASYNC_BOOT_AUTOCONF | AUTO_IRQ_FLAG)
113 #define ACCENT_FLAGS AUTO_IRQ_FLAG
117 #define BOCA_FLAGS (ASYNC_BOOT_AUTOCONF | AUTO_IRQ_FLAG)
119 #define BOCA_FLAGS AUTO_IRQ_FLAG
123 #define HUB6_FLAGS (ASYNC_BOOT_AUTOCONF)
129 * The following define the access methods for the HUB6 card. All
130 * access is through two ports for all 24 possible chips. The card is
131 * selected through the high 2 bits, the port on that card with the
132 * "middle" 3 bits, and the register on that port with the bottom
135 * While the access port and interrupt is configurable, the default
136 * port locations are 0x302 for the port control register, and 0x303
137 * for the data read/write register. Normally, the interrupt is at irq3
138 * but can be anything from 3 to 7 inclusive. Note tht using 3 will
139 * require disabling com2.
142 #define C_P(card,port) (((card)<<6|(port)<<3) + 1)
144 struct async_struct rs_table
[] = {
145 /* UART CLK PORT IRQ FLAGS */
146 { BASE_BAUD
, 0x3F8, 4, STD_COM_FLAGS
}, /* ttyS0 */
147 { BASE_BAUD
, 0x2F8, 3, STD_COM_FLAGS
}, /* ttyS1 */
148 { BASE_BAUD
, 0x3E8, 4, STD_COM_FLAGS
}, /* ttyS2 */
149 { BASE_BAUD
, 0x2E8, 3, STD_COM4_FLAGS
}, /* ttyS3 */
151 { BASE_BAUD
, 0x1A0, 9, FOURPORT_FLAGS
}, /* ttyS4 */
152 { BASE_BAUD
, 0x1A8, 9, FOURPORT_FLAGS
}, /* ttyS5 */
153 { BASE_BAUD
, 0x1B0, 9, FOURPORT_FLAGS
}, /* ttyS6 */
154 { BASE_BAUD
, 0x1B8, 9, FOURPORT_FLAGS
}, /* ttyS7 */
156 { BASE_BAUD
, 0x2A0, 5, FOURPORT_FLAGS
}, /* ttyS8 */
157 { BASE_BAUD
, 0x2A8, 5, FOURPORT_FLAGS
}, /* ttyS9 */
158 { BASE_BAUD
, 0x2B0, 5, FOURPORT_FLAGS
}, /* ttyS10 */
159 { BASE_BAUD
, 0x2B8, 5, FOURPORT_FLAGS
}, /* ttyS11 */
161 { BASE_BAUD
, 0x330, 4, ACCENT_FLAGS
}, /* ttyS12 */
162 { BASE_BAUD
, 0x338, 4, ACCENT_FLAGS
}, /* ttyS13 */
163 { BASE_BAUD
, 0x000, 0, 0 }, /* ttyS14 (spare; user configurable) */
164 { BASE_BAUD
, 0x000, 0, 0 }, /* ttyS15 (spare; user configurable) */
166 { BASE_BAUD
, 0x100, 12, BOCA_FLAGS
}, /* ttyS16 */
167 { BASE_BAUD
, 0x108, 12, BOCA_FLAGS
}, /* ttyS17 */
168 { BASE_BAUD
, 0x110, 12, BOCA_FLAGS
}, /* ttyS18 */
169 { BASE_BAUD
, 0x118, 12, BOCA_FLAGS
}, /* ttyS19 */
170 { BASE_BAUD
, 0x120, 12, BOCA_FLAGS
}, /* ttyS20 */
171 { BASE_BAUD
, 0x128, 12, BOCA_FLAGS
}, /* ttyS21 */
172 { BASE_BAUD
, 0x130, 12, BOCA_FLAGS
}, /* ttyS22 */
173 { BASE_BAUD
, 0x138, 12, BOCA_FLAGS
}, /* ttyS23 */
174 { BASE_BAUD
, 0x140, 12, BOCA_FLAGS
}, /* ttyS24 */
175 { BASE_BAUD
, 0x148, 12, BOCA_FLAGS
}, /* ttyS25 */
176 { BASE_BAUD
, 0x150, 12, BOCA_FLAGS
}, /* ttyS26 */
177 { BASE_BAUD
, 0x158, 12, BOCA_FLAGS
}, /* ttyS27 */
178 { BASE_BAUD
, 0x160, 12, BOCA_FLAGS
}, /* ttyS28 */
179 { BASE_BAUD
, 0x168, 12, BOCA_FLAGS
}, /* ttyS29 */
180 { BASE_BAUD
, 0x170, 12, BOCA_FLAGS
}, /* ttyS30 */
181 { BASE_BAUD
, 0x178, 12, BOCA_FLAGS
}, /* ttyS31 */
183 /* You can have up to four HUB6's in the system, but I've only
184 * included two cards here for a total of twelve ports.
186 { BASE_BAUD
, 0x302, 3, HUB6_FLAGS
, C_P(0,0) }, /* ttyS32 */
187 { BASE_BAUD
, 0x302, 3, HUB6_FLAGS
, C_P(0,1) }, /* ttyS33 */
188 { BASE_BAUD
, 0x302, 3, HUB6_FLAGS
, C_P(0,2) }, /* ttyS34 */
189 { BASE_BAUD
, 0x302, 3, HUB6_FLAGS
, C_P(0,3) }, /* ttyS35 */
190 { BASE_BAUD
, 0x302, 3, HUB6_FLAGS
, C_P(0,4) }, /* ttyS36 */
191 { BASE_BAUD
, 0x302, 3, HUB6_FLAGS
, C_P(0,5) }, /* ttyS37 */
192 { BASE_BAUD
, 0x302, 3, HUB6_FLAGS
, C_P(1,0) }, /* ttyS32 */
193 { BASE_BAUD
, 0x302, 3, HUB6_FLAGS
, C_P(1,1) }, /* ttyS33 */
194 { BASE_BAUD
, 0x302, 3, HUB6_FLAGS
, C_P(1,2) }, /* ttyS34 */
195 { BASE_BAUD
, 0x302, 3, HUB6_FLAGS
, C_P(1,3) }, /* ttyS35 */
196 { BASE_BAUD
, 0x302, 3, HUB6_FLAGS
, C_P(1,4) }, /* ttyS36 */
197 { BASE_BAUD
, 0x302, 3, HUB6_FLAGS
, C_P(1,5) }, /* ttyS37 */
200 #define NR_PORTS (sizeof(rs_table)/sizeof(struct async_struct))
203 * This is used to figure out the divsor speeds and the timeouts
205 static int baud_table
[] = {
206 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
207 9600, 19200, 38400, 57600, 115200, 0 };
209 static void rs_throttle(struct tty_struct
* tty
, int status
);
211 static inline unsigned int serial_in(struct async_struct
*info
, int offset
)
214 outb(info
->hub6
- 1 + offset
, info
->port
);
215 return inb(info
->port
+1);
217 return inb(info
->port
+ offset
);
220 static inline unsigned int serial_inp(struct async_struct
*info
, int offset
)
223 outb(info
->hub6
- 1 + offset
, info
->port
);
224 return inb_p(info
->port
+1);
226 return inb_p(info
->port
+ offset
);
229 static inline void serial_out(struct async_struct
*info
, int offset
, int value
)
232 outb(info
->hub6
- 1 + offset
, info
->port
);
233 outb(value
, info
->port
+1);
235 outb(value
, info
->port
+offset
);
238 static inline void serial_outp(struct async_struct
*info
, int offset
,
242 outb(info
->hub6
- 1 + offset
, info
->port
);
243 outb_p(value
, info
->port
+1);
245 outb_p(value
, info
->port
+offset
);
249 * ------------------------------------------------------------
250 * rs_stop() and rs_start()
252 * This routines are called before setting or resetting tty->stopped.
253 * They enable or disable transmitter interrupts, as necessary.
254 * ------------------------------------------------------------
256 static void rs_stop(struct tty_struct
*tty
)
258 struct async_struct
*info
;
260 info
= rs_table
+ DEV_TO_SL(tty
->line
);
262 info
->IER
= UART_IER_MSI
| UART_IER_RLSI
| UART_IER_RDI
;
264 serial_out(info
, UART_IER
, info
->IER
);
268 static void rs_start(struct tty_struct
*tty
)
270 struct async_struct
*info
;
272 info
= rs_table
+ DEV_TO_SL(tty
->line
);
274 info
->IER
= (UART_IER_MSI
| UART_IER_RLSI
|
275 UART_IER_THRI
| UART_IER_RDI
);
277 serial_out(info
, UART_IER
, info
->IER
);
282 * ----------------------------------------------------------------------
284 * Here starts the interrupt handling routines. All of the following
285 * subroutines are declared as inline and are folded into
286 * rs_interrupt(). They were separated out for readability's sake.
288 * Note: rs_interrupt() is a "fast" interrupt, which means that it
289 * runs with interrupts turned off. People who may want to modify
290 * rs_interrupt() should try to keep the interrupt handler as fast as
291 * possible. After you are done making modifications, it is not a bad
294 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
296 * and look at the resulting assemble code in serial.s.
298 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
299 * -----------------------------------------------------------------------
303 * This is the serial driver's interrupt routine while we are probing
306 static void rs_probe(int irq
)
308 rs_irq_triggered
= irq
;
309 rs_triggered
|= 1 << irq
;
314 * This routine is used by the interrupt handler to schedule
315 * processing in the software interrupt portion of the driver.
317 static inline void rs_sched_event(struct async_struct
*info
,
320 info
->event
|= 1 << event
;
321 set_bit(info
->line
, rs_event
);
325 static inline void receive_chars(struct async_struct
*info
,
328 struct tty_queue
* queue
;
332 * Just like the LEFT(x) macro, except it uses the loal tail
333 * and head variables.
335 #define VLEFT ((tail-head-1)&(TTY_BUF_SIZE-1))
337 queue
= &info
->tty
->read_q
;
341 ch
= serial_inp(info
, UART_RX
);
343 * There must be at least 2 characters
344 * free in the queue; otherwise we punt.
348 if (*status
& info
->read_status_mask
) {
349 set_bit(head
, &info
->tty
->readq_flags
);
350 if (*status
& (UART_LSR_BI
)) {
351 queue
->buf
[head
++]= TTY_BREAK
;
352 rs_sched_event(info
, RS_EVENT_BREAK
);
353 } else if (*status
& UART_LSR_PE
)
354 queue
->buf
[head
++]= TTY_PARITY
;
355 else if (*status
& UART_LSR_FE
)
356 queue
->buf
[head
++]= TTY_FRAME
;
357 else if (*status
& UART_LSR_OE
)
358 queue
->buf
[head
++]= TTY_OVERRUN
;
359 head
&= TTY_BUF_SIZE
-1;
361 queue
->buf
[head
++] = ch
;
362 head
&= TTY_BUF_SIZE
-1;
363 } while ((*status
= serial_inp(info
, UART_LSR
)) & UART_LSR_DR
);
365 if ((VLEFT
< RQ_THRESHOLD_LW
) && !set_bit(TTY_RQ_THROTTLED
,
367 rs_throttle(info
->tty
, TTY_THROTTLE_RQ_FULL
);
368 rs_sched_event(info
, RS_EVENT_READ_PROCESS
);
369 #ifdef SERIAL_DEBUG_INTR
374 static inline void transmit_chars(struct async_struct
*info
, int *done_work
)
376 struct tty_queue
* queue
;
377 int head
, tail
, count
;
379 queue
= &info
->tty
->write_q
;
382 if (head
==tail
&& !info
->x_char
) {
383 info
->IER
= UART_IER_MSI
| UART_IER_RLSI
| UART_IER_RDI
;
385 serial_out(info
, UART_IER
, info
->IER
);
389 count
= info
->xmit_fifo_size
;
391 serial_outp(info
, UART_TX
, info
->x_char
);
395 while (count
-- && (tail
!= head
)) {
396 serial_outp(info
, UART_TX
, queue
->buf
[tail
++]);
397 tail
&= TTY_BUF_SIZE
-1;
400 if (VLEFT
> WAKEUP_CHARS
) {
401 rs_sched_event(info
, RS_EVENT_WRITE_WAKEUP
);
402 if (info
->tty
->write_data_cnt
) {
403 set_bit(info
->tty
->line
, &tty_check_write
);
407 #ifdef SERIAL_DEBUG_INTR
413 static inline int check_modem_status(struct async_struct
*info
)
417 status
= serial_in(info
, UART_MSR
);
419 if ((status
& UART_MSR_DDCD
) && !C_CLOCAL(info
->tty
)) {
420 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
421 printk("ttys%d CD now %s...", info
->line
,
422 (status
& UART_MSR_DCD
) ? "on" : "off");
424 if (status
& UART_MSR_DCD
)
425 rs_sched_event(info
, RS_EVENT_OPEN_WAKEUP
);
426 else if (!((info
->flags
& ASYNC_CALLOUT_ACTIVE
) &&
427 (info
->flags
& ASYNC_CALLOUT_NOHUP
))) {
428 #ifdef SERIAL_DEBUG_OPEN
429 printk("scheduling hangup...");
431 rs_sched_event(info
, RS_EVENT_HANGUP
);
434 if (C_CRTSCTS(info
->tty
)) {
435 if (info
->tty
->hw_stopped
) {
436 if (status
& UART_MSR_CTS
) {
437 #ifdef SERIAL_DEBUG_INTR
438 printk("CTS tx start...");
440 info
->tty
->hw_stopped
= 0;
445 if (!(status
& UART_MSR_CTS
)) {
446 #ifdef SERIAL_DEBUG_INTR
447 printk("CTS tx stop...");
449 info
->tty
->hw_stopped
= 1;
457 static inline void figure_RS_timer(void)
459 int timeout
= 6000; /* 60 seconds; really big :-) */
464 for (i
=0, mask
= 1; mask
<= IRQ_active
; i
++, mask
<<= 1) {
465 if (!(mask
& IRQ_active
))
467 if (IRQ_timer
[i
] < timeout
)
468 timeout
= IRQ_timer
[i
];
470 timer_table
[RS_TIMER
].expires
= jiffies
+ timeout
;
471 timer_active
|= 1 << RS_TIMER
;
476 * This is the serial driver's generic interrupt routine
478 static void rs_interrupt(int irq
)
481 struct async_struct
* info
;
482 int done
, done_work
, pass_number
, recheck_count
;
484 rs_irq_triggered
= irq
;
485 rs_triggered
|= 1 << irq
;
487 info
= IRQ_ports
[irq
];
493 info
->tty
->termios
&&
495 !(serial_inp(info
, UART_IIR
) & UART_IIR_NO_INT
))) {
497 status
= serial_inp(info
, UART_LSR
);
498 if (status
& UART_LSR_DR
) {
499 receive_chars(info
, &status
);
504 if (status
& UART_LSR_THRE
) {
505 wake_up_interruptible(&info
->xmit_wait
);
506 if (!info
->tty
->stopped
&&
507 !info
->tty
->hw_stopped
)
508 transmit_chars(info
, &done_work
);
510 if (check_modem_status(info
) &&
511 (recheck_count
++ <= 64))
513 #ifdef SERIAL_DEBUG_INTR
514 if (recheck_count
> 16)
515 printk("recheck_count = %d\n", recheck_count
);
519 serial_outp(info
, UART_IER
, 0);
520 serial_out(info
, UART_IER
, info
->IER
);
523 info
= info
->next_port
;
524 if (!info
&& !done
) {
525 info
= IRQ_ports
[irq
];
527 if (pass_number
++ > 64)
528 break; /* Prevent infinite loops */
531 if ((info
= IRQ_ports
[irq
]) != NULL
) {
534 serial_outp(info
, UART_IER
, 0);
535 serial_out(info
, UART_IER
, info
->IER
);
536 info
= info
->next_port
;
539 if (irq
&& !done_work
)
540 IRQ_timer
[irq
] = jiffies
+ 1500;
542 IRQ_timer
[irq
] = jiffies
+ IRQ_timeout
[irq
];
543 IRQ_active
|= 1 << irq
;
549 * -------------------------------------------------------------------
550 * Here ends the serial interrupt routines.
551 * -------------------------------------------------------------------
555 * This routine is called when we receive a break on a serial line.
556 * It is executed out of the software interrupt routine.
558 static inline void handle_rs_break(struct async_struct
*info
)
560 if (info
->flags
& ASYNC_SAK
)
563 if (!I_IGNBRK(info
->tty
) && I_BRKINT(info
->tty
)) {
564 flush_input(info
->tty
);
565 flush_output(info
->tty
);
566 if (info
->tty
->pgrp
> 0)
567 kill_pg(info
->tty
->pgrp
, SIGINT
,1);
572 * This routine is used to handle the "bottom half" processing for the
573 * serial driver, known also the "software interrupt" processing.
574 * This processing is done at the kernel interrupt level, after the
575 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
576 * is where time-consuming activities which can not be done in the
577 * interrupt driver proper are done; the interrupt driver schedules
578 * them using rs_sched_event(), and they get done here.
580 static void do_softint(void *unused
)
583 struct async_struct
*info
;
585 for (i
= 0, info
= rs_table
; i
< NR_PORTS
; i
++,info
++) {
586 if (clear_bit(i
, rs_event
)) {
589 if (clear_bit(RS_EVENT_READ_PROCESS
, &info
->event
)) {
590 TTY_READ_FLUSH(info
->tty
);
592 if (clear_bit(RS_EVENT_WRITE_WAKEUP
, &info
->event
)) {
593 wake_up_interruptible(&info
->tty
->write_q
.proc_list
);
595 if (clear_bit(RS_EVENT_HANGUP
, &info
->event
)) {
596 tty_hangup(info
->tty
);
597 wake_up_interruptible(&info
->open_wait
);
598 info
->flags
&= ~(ASYNC_NORMAL_ACTIVE
|
599 ASYNC_CALLOUT_ACTIVE
);
601 if (clear_bit(RS_EVENT_BREAK
, &info
->event
))
602 handle_rs_break(info
);
603 if (clear_bit(RS_EVENT_OPEN_WAKEUP
, &info
->event
)) {
604 wake_up_interruptible(&info
->open_wait
);
611 * This subroutine is called when the RS_TIMER goes off. It is used
612 * by the serial driver to run the rs_interrupt routine at certain
613 * intervals, either because a serial interrupt might have been lost,
614 * or because (in the case of IRQ=0) the serial port does not have an
615 * interrupt, and is being checked only via the timer interrupts.
617 static void rs_timer(void)
622 for (i
= 0, mask
= 1; mask
<= IRQ_active
; i
++, mask
<<= 1) {
623 if ((mask
& IRQ_active
) && (IRQ_timer
[i
] <= jiffies
)) {
626 #ifdef SERIAL_DEBUG_TIMER
627 printk("rs_timer: rs_interrupt(%d)...", i
);
632 if (mask
& IRQ_active
) {
633 if (!timeout
|| (IRQ_timer
[i
] < timeout
))
634 timeout
= IRQ_timer
[i
];
638 timer_table
[RS_TIMER
].expires
= timeout
;
639 timer_active
|= 1 << RS_TIMER
;
644 * ---------------------------------------------------------------
645 * Low level utility subroutines for the serial driver: routines to
646 * figure out the appropriate timeout for an interrupt chain, routines
647 * to initialize and startup a serial port, and routines to shutdown a
648 * serial port. Useful stuff like that.
649 * ---------------------------------------------------------------
653 * Grab all interrupts in preparation for doing an automatic irq
654 * detection. dontgrab is a mask of irq's _not_ to grab. Returns a
655 * mask of irq's which were grabbed and should therefore be freed
656 * using free_all_interrupts().
658 static int grab_all_interrupts(int dontgrab
)
664 sa
.sa_handler
= rs_probe
;
665 sa
.sa_flags
= (SA_INTERRUPT
);
667 sa
.sa_restorer
= NULL
;
669 for (i
= 0, mask
= 1; i
< 16; i
++, mask
<<= 1) {
670 if (!(mask
& dontgrab
) && !irqaction(i
, &sa
)) {
678 * Release all interrupts grabbed by grab_all_interrupts
680 static void free_all_interrupts(int irq_lines
)
684 for (i
= 0; i
< 16; i
++) {
685 if (irq_lines
& (1 << i
))
691 * This routine figures out the correct timeout for a particular IRQ.
692 * It uses the smallest timeout of all of the serial ports in a
693 * particular interrupt chain.
695 static void figure_IRQ_timeout(int irq
)
697 struct async_struct
*info
;
698 int timeout
= 6000; /* 60 seconds === a long time :-) */
700 info
= IRQ_ports
[irq
];
702 IRQ_timeout
[irq
] = 6000;
706 if (info
->timeout
< timeout
)
707 timeout
= info
->timeout
;
708 info
= info
->next_port
;
711 timeout
= timeout
/ 2;
712 IRQ_timeout
[irq
] = timeout
? timeout
: 1;
715 static int startup(struct async_struct
* info
, int get_irq
)
722 if (info
->flags
& ASYNC_INITIALIZED
)
725 if (!info
->port
|| !info
->type
) {
727 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
731 save_flags(flags
); cli();
733 #ifdef SERIAL_DEBUG_OPEN
734 printk("starting up ttys%d (irq %d)...", info
->line
, info
->irq
);
738 * Allocate the IRQ if necessary
740 if (get_irq
&& info
->irq
&& !IRQ_ports
[info
->irq
]) {
741 sa
.sa_handler
= rs_interrupt
;
742 sa
.sa_flags
= (SA_INTERRUPT
);
744 sa
.sa_restorer
= NULL
;
745 retval
= irqaction(info
->irq
,&sa
);
747 restore_flags(flags
);
753 * Clear the FIFO buffers and disable them
754 * (they will be reenabled in change_speed())
756 if (info
->type
== PORT_16550A
) {
757 serial_outp(info
, UART_FCR
, (UART_FCR_CLEAR_RCVR
|
758 UART_FCR_CLEAR_XMIT
));
759 info
->xmit_fifo_size
= 16;
761 info
->xmit_fifo_size
= 1;
764 * Clear the interrupt registers.
766 (void)serial_inp(info
, UART_LSR
);
767 (void)serial_inp(info
, UART_RX
);
768 (void)serial_inp(info
, UART_IIR
);
769 (void)serial_inp(info
, UART_MSR
);
772 * Now, initialize the UART
774 serial_outp(info
, UART_LCR
, UART_LCR_WLEN8
); /* reset DLAB */
775 if (info
->flags
& ASYNC_FOURPORT
)
776 serial_outp(info
, UART_MCR
, UART_MCR_DTR
| UART_MCR_RTS
);
778 serial_outp(info
, UART_MCR
,
779 UART_MCR_DTR
| UART_MCR_RTS
| UART_MCR_OUT2
);
782 * Finally, enable interrupts
785 info
->IER
= UART_IER_MSI
| UART_IER_RLSI
| UART_IER_RDI
;
786 serial_outp(info
, UART_IER
, info
->IER
); /* enable interrupts */
788 info
->IER
= (UART_IER_MSI
| UART_IER_RLSI
|
789 UART_IER_THRI
| UART_IER_RDI
);
790 serial_outp(info
, UART_IER
, info
->IER
); /* enable all intrs */
792 if (info
->flags
& ASYNC_FOURPORT
) {
793 /* Enable interrupts on the AST Fourport board */
794 ICP
= (info
->port
& 0xFE0) | 0x01F;
800 * And clear the interrupt registers again for luck.
802 (void)serial_inp(info
, UART_LSR
);
803 (void)serial_inp(info
, UART_RX
);
804 (void)serial_inp(info
, UART_IIR
);
805 (void)serial_inp(info
, UART_MSR
);
808 clear_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
810 * Set up parity check flag
812 if (info
->tty
&& info
->tty
->termios
&& I_INPCK(info
->tty
))
813 info
->read_status_mask
= (UART_LSR_OE
| UART_LSR_BI
|
814 UART_LSR_FE
| UART_LSR_PE
);
816 info
->read_status_mask
= (UART_LSR_OE
| UART_LSR_BI
|
820 * Insert serial port into IRQ chain.
823 info
->next_port
= IRQ_ports
[info
->irq
];
825 info
->next_port
->prev_port
= info
;
826 IRQ_ports
[info
->irq
] = info
;
827 figure_IRQ_timeout(info
->irq
);
830 * Set up serial timers...
832 IRQ_active
|= 1 << info
->irq
;
836 * and set the speed of the serial port
838 change_speed(info
->line
);
840 info
->flags
|= ASYNC_INITIALIZED
;
841 restore_flags(flags
);
846 * This routine will shutdown a serial port; interrupts are disabled, and
847 * DTR is dropped if the hangup on close termio flag is on.
849 static void shutdown(struct async_struct
* info
, int do_free_irq
)
853 if (!(info
->flags
& ASYNC_INITIALIZED
))
856 #ifdef SERIAL_DEBUG_OPEN
857 printk("Shutting down serial port %d (irq %d)....", info
->line
,
861 save_flags(flags
); cli(); /* Disable interrupts */
864 * First unlink the serial port from the IRQ chain...
867 info
->next_port
->prev_port
= info
->prev_port
;
869 info
->prev_port
->next_port
= info
->next_port
;
871 IRQ_ports
[info
->irq
] = info
->next_port
;
872 figure_IRQ_timeout(info
->irq
);
875 * Free the IRQ, if necessary
877 if (do_free_irq
&& info
->irq
&& !IRQ_ports
[info
->irq
])
881 serial_outp(info
, UART_IER
, 0x00); /* disable all intrs */
882 if (info
->flags
& ASYNC_FOURPORT
) {
883 /* reset interrupts on the AST Fourport board */
884 (void) inb((info
->port
& 0xFE0) | 0x01F);
886 if (info
->tty
&& !(info
->tty
->termios
->c_cflag
& HUPCL
))
887 serial_outp(info
, UART_MCR
, UART_MCR_DTR
);
889 /* reset DTR,RTS,OUT_2 */
890 serial_outp(info
, UART_MCR
, 0x00);
893 serial_outp(info
, UART_FCR
, (UART_FCR_CLEAR_RCVR
|
894 UART_FCR_CLEAR_XMIT
));
895 (void)serial_in(info
, UART_RX
); /* read data port to reset things */
898 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
900 info
->flags
&= ~ASYNC_INITIALIZED
;
901 restore_flags(flags
);
905 * This routine is called to set the UART divisor registers to match
906 * the specified baud rate for a serial port.
908 static void change_speed(unsigned int line
)
910 struct async_struct
* info
;
913 unsigned cflag
,cval
,mcr
,fcr
;
916 if (line
>= NR_PORTS
)
918 info
= rs_table
+ line
;
919 if (!info
->tty
|| !info
->tty
->termios
)
921 cflag
= info
->tty
->termios
->c_cflag
;
922 if (!(port
= info
->port
))
926 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_HI
)
928 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_VHI
)
930 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_CUST
)
931 quot
= info
->custom_divisor
;
934 info
->timeout
= ((info
->xmit_fifo_size
*HZ
*15*quot
) /
935 info
->baud_base
) + 2;
936 } else if (baud_table
[i
] == 134) {
937 quot
= (2*info
->baud_base
/ 269);
938 info
->timeout
= (info
->xmit_fifo_size
*HZ
*30/269) + 2;
939 } else if (baud_table
[i
]) {
940 quot
= info
->baud_base
/ baud_table
[i
];
941 info
->timeout
= (info
->xmit_fifo_size
*HZ
*15/baud_table
[i
]) + 2;
947 mcr
= serial_in(info
, UART_MCR
);
949 serial_out(info
, UART_MCR
, mcr
| UART_MCR_DTR
);
951 serial_out(info
, UART_MCR
, mcr
& ~UART_MCR_DTR
);
956 /* byte size and parity */
957 cval
= cflag
& (CSIZE
| CSTOPB
);
960 cval
|= UART_LCR_PARITY
;
961 if (!(cflag
& PARODD
))
962 cval
|= UART_LCR_EPAR
;
963 if (info
->type
== PORT_16550A
) {
964 if ((info
->baud_base
/ quot
) < 2400)
965 fcr
= UART_FCR_ENABLE_FIFO
| UART_FCR_TRIGGER_1
;
967 fcr
= UART_FCR_ENABLE_FIFO
| UART_FCR_TRIGGER_8
;
972 serial_outp(info
, UART_LCR
, cval
| UART_LCR_DLAB
); /* set DLAB */
973 serial_outp(info
, UART_DLL
, quot
& 0xff); /* LS of divisor */
974 serial_outp(info
, UART_DLM
, quot
>> 8); /* MS of divisor */
975 serial_outp(info
, UART_LCR
, cval
); /* reset DLAB */
976 serial_outp(info
, UART_FCR
, fcr
); /* set fcr */
981 * ------------------------------------------------------------
982 * rs_write() and friends
983 * ------------------------------------------------------------
987 * This routine is used by rs_write to restart transmitter interrupts,
988 * which are disabled after we have a transmitter interrupt which went
989 * unacknowledged because we had run out of data to transmit.
991 * Note: this subroutine must be called with the interrupts *off*
993 static inline void restart_port(struct async_struct
*info
)
995 struct tty_queue
* queue
;
996 int head
, tail
, count
;
1001 if (serial_inp(info
, UART_LSR
) & UART_LSR_THRE
) {
1003 serial_outp(info
, UART_TX
, info
->x_char
);
1006 queue
= &info
->tty
->write_q
;
1009 count
= info
->xmit_fifo_size
;
1013 serial_outp(info
, UART_TX
, queue
->buf
[tail
++]);
1014 tail
&= TTY_BUF_SIZE
-1;
1022 * This routine gets called when tty_write has put something into
1025 void rs_write(struct tty_struct
* tty
)
1027 struct async_struct
*info
;
1029 if (!tty
|| tty
->stopped
|| tty
->hw_stopped
)
1031 info
= rs_table
+ DEV_TO_SL(tty
->line
);
1032 if (!info
|| !info
->tty
|| !(info
->flags
& ASYNC_INITIALIZED
))
1036 info
->IER
= (UART_IER_MSI
| UART_IER_RLSI
|
1037 UART_IER_THRI
| UART_IER_RDI
);
1039 serial_out(info
, UART_IER
, info
->IER
);
1045 * ------------------------------------------------------------
1048 * This routine is called by the upper-layer tty layer to signal that
1049 * incoming characters should be throttled (and that the throttle
1050 * should be released).
1051 * ------------------------------------------------------------
1053 static void rs_throttle(struct tty_struct
* tty
, int status
)
1055 struct async_struct
*info
;
1057 unsigned long flags
;
1059 save_flags(flags
); cli();
1060 #if SERIAL_DEBUG_THROTTLE
1061 printk("throttle tty%d: %d (%d, %d)....\n", DEV_TO_SL(tty
->line
),
1062 status
, LEFT(&tty
->read_q
), LEFT(&tty
->secondary
));
1065 case TTY_THROTTLE_RQ_FULL
:
1066 info
= rs_table
+ DEV_TO_SL(tty
->line
);
1068 info
->x_char
= STOP_CHAR(tty
);
1070 mcr
= serial_inp(info
, UART_MCR
);
1071 mcr
&= ~UART_MCR_RTS
;
1072 serial_out(info
, UART_MCR
, mcr
);
1075 case TTY_THROTTLE_RQ_AVAIL
:
1076 info
= rs_table
+ DEV_TO_SL(tty
->line
);
1081 info
->x_char
= START_CHAR(tty
);
1083 mcr
= serial_in(info
, UART_MCR
);
1084 mcr
|= UART_MCR_RTS
;
1085 serial_out(info
, UART_MCR
, mcr
);
1089 restore_flags(flags
);
1093 * ------------------------------------------------------------
1094 * rs_ioctl() and friends
1095 * ------------------------------------------------------------
1098 static int get_serial_info(struct async_struct
* info
,
1099 struct serial_struct
* retinfo
)
1101 struct serial_struct tmp
;
1105 memset(&tmp
, 0, sizeof(tmp
));
1106 tmp
.type
= info
->type
;
1107 tmp
.line
= info
->line
;
1108 tmp
.port
= info
->port
;
1109 tmp
.irq
= info
->irq
;
1110 tmp
.flags
= info
->flags
;
1111 tmp
.baud_base
= info
->baud_base
;
1112 tmp
.close_delay
= info
->close_delay
;
1113 tmp
.custom_divisor
= info
->custom_divisor
;
1114 tmp
.hub6
= info
->hub6
;
1115 memcpy_tofs(retinfo
,&tmp
,sizeof(*retinfo
));
1119 static int set_serial_info(struct async_struct
* info
,
1120 struct serial_struct
* new_info
)
1122 struct serial_struct new_serial
;
1123 struct async_struct old_info
;
1124 unsigned int i
,change_irq
,change_port
;
1126 struct sigaction sa
;
1130 memcpy_fromfs(&new_serial
,new_info
,sizeof(new_serial
));
1133 change_irq
= new_serial
.irq
!= info
->irq
;
1134 change_port
= (new_serial
.port
!= info
->port
) || (new_serial
.hub6
!= info
->hub6
);
1137 if (change_irq
|| change_port
||
1138 (new_serial
.baud_base
!= info
->baud_base
) ||
1139 (new_serial
.type
!= info
->type
) ||
1140 (new_serial
.close_delay
!= info
->close_delay
) ||
1141 ((new_serial
.flags
& ~ASYNC_USR_MASK
) !=
1142 (info
->flags
& ~ASYNC_USR_MASK
)))
1144 info
->flags
= ((info
->flags
& ~ASYNC_USR_MASK
) |
1145 (new_serial
.flags
& ASYNC_USR_MASK
));
1146 info
->custom_divisor
= new_serial
.custom_divisor
;
1147 goto check_and_exit
;
1150 if (new_serial
.irq
== 2)
1153 if ((new_serial
.irq
> 15) || (new_serial
.port
> 0xffff) ||
1154 (new_serial
.type
< PORT_UNKNOWN
) || (new_serial
.type
> PORT_MAX
)) {
1158 /* Make sure address is not already in use */
1159 for (i
= 0 ; i
< NR_PORTS
; i
++)
1160 if ((info
!= &rs_table
[i
]) &&
1161 (rs_table
[i
].port
== new_serial
.port
) && rs_table
[i
].type
)
1165 * If necessary, first we try to grab the new IRQ for serial
1166 * interrupts. (We have to do this early, since we may get an
1167 * error trying to do this.)
1169 if (new_serial
.port
&& new_serial
.type
&& new_serial
.irq
&&
1170 (change_irq
|| !(info
->flags
& ASYNC_INITIALIZED
))) {
1171 if (!IRQ_ports
[new_serial
.irq
]) {
1172 sa
.sa_handler
= rs_interrupt
;
1173 sa
.sa_flags
= (SA_INTERRUPT
);
1175 sa
.sa_restorer
= NULL
;
1176 retval
= irqaction(new_serial
.irq
,&sa
);
1182 if ((change_port
|| change_irq
) && (info
->count
> 1))
1186 * OK, past this point, all the error checking has been done.
1187 * At this point, we start making changes.....
1190 info
->baud_base
= new_serial
.baud_base
;
1191 info
->flags
= ((info
->flags
& ~ASYNC_FLAGS
) |
1192 (new_serial
.flags
& ASYNC_FLAGS
));
1193 info
->custom_divisor
= new_serial
.custom_divisor
;
1194 info
->type
= new_serial
.type
;
1195 info
->close_delay
= new_serial
.close_delay
;
1197 if (change_port
|| change_irq
) {
1199 * We need to shutdown the serial port at the old
1200 * port/irq combination.
1202 shutdown(info
, change_irq
);
1203 info
->irq
= new_serial
.irq
;
1204 info
->port
= new_serial
.port
;
1205 info
->hub6
= new_serial
.hub6
;
1209 if (!info
->port
|| !info
->type
)
1211 if (info
->flags
& ASYNC_INITIALIZED
) {
1212 if (((old_info
.flags
& ASYNC_SPD_MASK
) !=
1213 (info
->flags
& ASYNC_SPD_MASK
)) ||
1214 (old_info
.custom_divisor
!= info
->custom_divisor
))
1215 change_speed(info
->line
);
1217 (void) startup(info
, 0);
1221 static int get_modem_info(struct async_struct
* info
, unsigned int *value
)
1223 unsigned char control
, status
;
1224 unsigned int result
;
1227 control
= serial_in(info
, UART_MCR
);
1228 status
= serial_in(info
, UART_MSR
);
1230 result
= ((control
& UART_MCR_RTS
) ? TIOCM_RTS
: 0)
1231 | ((control
& UART_MCR_DTR
) ? TIOCM_DTR
: 0)
1232 | ((status
& UART_MSR_DCD
) ? TIOCM_CAR
: 0)
1233 | ((status
& UART_MSR_RI
) ? TIOCM_RNG
: 0)
1234 | ((status
& UART_MSR_DSR
) ? TIOCM_DSR
: 0)
1235 | ((status
& UART_MSR_CTS
) ? TIOCM_CTS
: 0);
1236 put_fs_long(result
,(unsigned long *) value
);
1240 static int set_modem_info(struct async_struct
* info
, unsigned int cmd
,
1241 unsigned int *value
)
1243 unsigned char control
;
1244 unsigned int arg
= get_fs_long((unsigned long *) value
);
1247 control
= serial_in(info
, UART_MCR
);
1252 if (arg
& TIOCM_RTS
)
1253 control
|= UART_MCR_RTS
;
1254 if (arg
& TIOCM_DTR
)
1255 control
|= UART_MCR_DTR
;
1258 if (arg
& TIOCM_RTS
)
1259 control
&= ~UART_MCR_RTS
;
1260 if (arg
& TIOCM_DTR
)
1261 control
&= ~UART_MCR_DTR
;
1264 control
= (control
& ~(UART_MCR_RTS
| UART_MCR_DTR
))
1265 | ((arg
& TIOCM_RTS
) ? UART_MCR_RTS
: 0)
1266 | ((arg
& TIOCM_DTR
) ? UART_MCR_DTR
: 0);
1272 serial_out(info
, UART_MCR
, control
);
1277 static int do_autoconfig(struct async_struct
* info
)
1284 if (info
->count
> 1)
1293 retval
= startup(info
, 1);
1301 * This routine sends a break character out the serial port.
1303 static void send_break( struct async_struct
* info
, int duration
)
1307 current
->state
= TASK_INTERRUPTIBLE
;
1308 current
->timeout
= jiffies
+ duration
;
1310 serial_out(info
, UART_LCR
, serial_inp(info
, UART_LCR
) | UART_LCR_SBC
);
1312 serial_out(info
, UART_LCR
, serial_inp(info
, UART_LCR
) & ~UART_LCR_SBC
);
1317 * This routine returns a bitfield of "wild interrupts". Basically,
1318 * any unclaimed interrupts which is flapping around.
1320 static int check_wild_interrupts(int doprint
)
1323 int wild_interrupts
= 0;
1325 unsigned long timeout
;
1326 unsigned long flags
;
1328 /* Turn on interrupts (they may be off) */
1329 save_flags(flags
); sti();
1331 irq_lines
= grab_all_interrupts(0);
1334 * Delay for 0.1 seconds -- we use a busy loop since this may
1335 * occur during the bootup sequence
1337 timeout
= jiffies
+10;
1338 while (timeout
>= jiffies
)
1341 rs_triggered
= 0; /* Reset after letting things settle */
1343 timeout
= jiffies
+10;
1344 while (timeout
>= jiffies
)
1347 for (i
= 0, mask
= 1; i
< 16; i
++, mask
<<= 1) {
1348 if ((rs_triggered
& (1 << i
)) &&
1349 (irq_lines
& (1 << i
))) {
1350 wild_interrupts
|= mask
;
1352 printk("Wild interrupt? (IRQ %d)\n", i
);
1355 free_all_interrupts(irq_lines
);
1356 restore_flags(flags
);
1357 return wild_interrupts
;
1360 static int rs_ioctl(struct tty_struct
*tty
, struct file
* file
,
1361 unsigned int cmd
, unsigned long arg
)
1364 struct async_struct
* info
;
1366 line
= DEV_TO_SL(tty
->line
);
1367 if (line
< 0 || line
>= NR_PORTS
)
1369 info
= rs_table
+ line
;
1372 case TCSBRK
: /* SVID version: non-zero arg --> no break */
1374 send_break(info
, HZ
/4); /* 1/4 second */
1376 case TCSBRKP
: /* support for POSIX tcsendbreak() */
1377 send_break(info
, arg
? arg
*(HZ
/10) : HZ
/4);
1380 error
= verify_area(VERIFY_WRITE
, (void *) arg
,sizeof(long));
1383 put_fs_long(C_CLOCAL(tty
) ? 1 : 0,
1384 (unsigned long *) arg
);
1387 arg
= get_fs_long((unsigned long *) arg
);
1388 tty
->termios
->c_cflag
=
1389 ((tty
->termios
->c_cflag
& ~CLOCAL
) |
1390 (arg
? CLOCAL
: 0));
1393 error
= verify_area(VERIFY_WRITE
, (void *) arg
,
1394 sizeof(unsigned int));
1397 return get_modem_info(info
, (unsigned int *) arg
);
1401 return set_modem_info(info
, cmd
, (unsigned int *) arg
);
1403 error
= verify_area(VERIFY_WRITE
, (void *) arg
,
1404 sizeof(struct serial_struct
));
1407 return get_serial_info(info
,
1408 (struct serial_struct
*) arg
);
1410 return set_serial_info(info
,
1411 (struct serial_struct
*) arg
);
1413 return do_autoconfig(info
);
1416 error
= verify_area(VERIFY_WRITE
, (void *) arg
,
1420 put_fs_long(rs_wild_int_mask
, (unsigned long *) arg
);
1426 rs_wild_int_mask
= get_fs_long((unsigned long *) arg
);
1427 if (rs_wild_int_mask
< 0)
1428 rs_wild_int_mask
= check_wild_interrupts(0);
1437 static void rs_set_termios(struct tty_struct
*tty
, struct termios
*old_termios
)
1439 struct async_struct
*info
;
1441 if (tty
->termios
->c_cflag
== old_termios
->c_cflag
)
1444 info
= &rs_table
[DEV_TO_SL(tty
->line
)];
1446 change_speed(DEV_TO_SL(tty
->line
));
1448 if ((old_termios
->c_cflag
& CRTSCTS
) &&
1449 !(tty
->termios
->c_cflag
& CRTSCTS
)) {
1450 tty
->hw_stopped
= 0;
1454 if (!(old_termios
->c_cflag
& CLOCAL
) &&
1455 (tty
->termios
->c_cflag
& CLOCAL
))
1456 wake_up_interruptible(&info
->open_wait
);
1459 info
->read_status_mask
= (UART_LSR_OE
| UART_LSR_BI
|
1460 UART_LSR_FE
| UART_LSR_PE
);
1462 info
->read_status_mask
= (UART_LSR_OE
| UART_LSR_BI
|
1467 * ------------------------------------------------------------
1470 * This routine is called when the serial port gets closed. First, we
1471 * wait for the last remaining data to be sent. Then, we unlink its
1472 * async structure from the interrupt chain if necessary, and we free
1473 * that IRQ if nothing is left in the chain.
1474 * ------------------------------------------------------------
1476 static void rs_close(struct tty_struct
*tty
, struct file
* filp
)
1478 struct async_struct
* info
;
1481 if (tty_hung_up_p(filp
))
1484 line
= DEV_TO_SL(tty
->line
);
1485 if ((line
< 0) || (line
>= NR_PORTS
))
1487 info
= rs_table
+ line
;
1488 #ifdef SERIAL_DEBUG_OPEN
1489 printk("rs_close ttys%d, count = %d\n", info
->line
, info
->count
);
1491 if ((tty
->count
== 1) && (info
->count
!= 1)) {
1493 * Uh, oh. tty->count is 1, which means that the tty
1494 * structure will be freed. Info->count should always
1495 * be one in these conditions. If it's greater than
1496 * one, we've got real problems, since it means the
1497 * serial port won't be shutdown.
1499 printk("rs_close: bad serial port count; tty->count is 1, "
1500 "info->count is %d\n", info
->count
);
1503 if (--info
->count
< 0) {
1504 printk("rs_close: bad serial port count for ttys%d: %d\n",
1505 info
->line
, info
->count
);
1510 info
->flags
|= ASYNC_CLOSING
;
1512 * Save the termios structure, since this port may have
1513 * separate termios for callout and dialin.
1515 if (info
->flags
& ASYNC_NORMAL_ACTIVE
)
1516 info
->normal_termios
= *tty
->termios
;
1517 if (info
->flags
& ASYNC_CALLOUT_ACTIVE
)
1518 info
->callout_termios
= *tty
->termios
;
1519 tty
->stopped
= 0; /* Force flush to succeed */
1520 tty
->hw_stopped
= 0;
1521 if (info
->flags
& ASYNC_INITIALIZED
) {
1524 * XXX There should be a timeout added to
1525 * wait_until_sent, eventually. TYT 1/19/94
1527 wait_until_sent(tty
);
1533 * Make sure the UART transmitter has completely drained; this
1534 * is especially important if there is a transmit FIFO!
1536 if (!(serial_inp(info
, UART_LSR
) & UART_LSR_THRE
)) {
1537 rs_start(tty
); /* Make sure THRI interrupt enabled */
1538 interruptible_sleep_on(&info
->xmit_wait
);
1542 clear_bit(line
, rs_event
);
1545 if (info
->blocked_open
) {
1546 if (info
->close_delay
) {
1547 tty
->count
++; /* avoid race condition */
1548 current
->state
= TASK_INTERRUPTIBLE
;
1549 current
->timeout
= jiffies
+ info
->close_delay
;
1553 wake_up_interruptible(&info
->open_wait
);
1555 info
->flags
&= ~(ASYNC_NORMAL_ACTIVE
|ASYNC_CALLOUT_ACTIVE
|
1557 wake_up_interruptible(&info
->close_wait
);
1561 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1563 void rs_hangup(struct tty_struct
*tty
)
1565 struct async_struct
* info
;
1568 line
= DEV_TO_SL(tty
->line
);
1569 if ((line
< 0) || (line
>= NR_PORTS
))
1571 info
= rs_table
+ line
;
1574 clear_bit(line
, rs_event
);
1577 info
->flags
&= ~(ASYNC_NORMAL_ACTIVE
|ASYNC_CALLOUT_ACTIVE
);
1579 wake_up_interruptible(&info
->open_wait
);
1583 * ------------------------------------------------------------
1584 * rs_open() and friends
1585 * ------------------------------------------------------------
1587 static int block_til_ready(struct tty_struct
*tty
, struct file
* filp
,
1588 struct async_struct
*info
)
1590 struct wait_queue wait
= { current
, NULL
};
1592 int do_clocal
= C_CLOCAL(tty
);
1595 * If the device is in the middle of being closed, then block
1596 * until it's done, and then try again.
1598 if (info
->flags
& ASYNC_CLOSING
) {
1599 interruptible_sleep_on(&info
->close_wait
);
1600 #ifdef SERIAL_DO_RESTART
1601 if (info
->flags
& ASYNC_HUP_NOTIFY
)
1604 return -ERESTARTSYS
;
1611 * If this is a callout device, then just make sure the normal
1612 * device isn't being used.
1614 if (MAJOR(filp
->f_rdev
) == TTYAUX_MAJOR
) {
1615 if (info
->flags
& ASYNC_NORMAL_ACTIVE
)
1617 if ((info
->flags
& ASYNC_CALLOUT_ACTIVE
) &&
1618 (info
->flags
& ASYNC_SESSION_LOCKOUT
) &&
1619 (info
->session
!= current
->session
))
1621 if ((info
->flags
& ASYNC_CALLOUT_ACTIVE
) &&
1622 (info
->flags
& ASYNC_PGRP_LOCKOUT
) &&
1623 (info
->pgrp
!= current
->pgrp
))
1625 info
->flags
|= ASYNC_CALLOUT_ACTIVE
;
1630 * If non-blocking mode is set, then make the check up front
1633 if (filp
->f_flags
& O_NONBLOCK
) {
1634 if (info
->flags
& ASYNC_CALLOUT_ACTIVE
)
1636 info
->flags
|= ASYNC_NORMAL_ACTIVE
;
1641 * Block waiting for the carrier detect and the line to become
1642 * free (i.e., not in use by the callout). While we are in
1643 * this loop, info->count is dropped by one, so that
1644 * rs_close() knows when to free things. We restore it upon
1645 * exit, either normal or abnormal.
1648 add_wait_queue(&info
->open_wait
, &wait
);
1649 #ifdef SERIAL_DEBUG_OPEN
1650 printk("block_til_ready before block: ttys%d, count = %d\n",
1651 info
->line
, info
->count
);
1654 info
->blocked_open
++;
1657 if (!(info
->flags
& ASYNC_CALLOUT_ACTIVE
))
1658 serial_out(info
, UART_MCR
,
1659 serial_inp(info
, UART_MCR
) |
1660 (UART_MCR_DTR
| UART_MCR_RTS
));
1662 current
->state
= TASK_INTERRUPTIBLE
;
1663 if (tty_hung_up_p(filp
) ||
1664 !(info
->flags
& ASYNC_INITIALIZED
)) {
1665 #ifdef SERIAL_DO_RESTART
1666 if (info
->flags
& ASYNC_HUP_NOTIFY
)
1669 retval
= -ERESTARTSYS
;
1675 if (!(info
->flags
& ASYNC_CALLOUT_ACTIVE
) &&
1676 !(info
->flags
& ASYNC_CLOSING
) &&
1677 (do_clocal
|| (serial_in(info
, UART_MSR
) &
1680 if (current
->signal
& ~current
->blocked
) {
1681 retval
= -ERESTARTSYS
;
1684 #ifdef SERIAL_DEBUG_OPEN
1685 printk("block_til_ready blocking: ttys%d, count = %d\n",
1686 info
->line
, info
->count
);
1690 current
->state
= TASK_RUNNING
;
1691 remove_wait_queue(&info
->open_wait
, &wait
);
1692 if (!tty_hung_up_p(filp
))
1694 info
->blocked_open
--;
1695 #ifdef SERIAL_DEBUG_OPEN
1696 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1697 info
->line
, info
->count
);
1701 info
->flags
|= ASYNC_NORMAL_ACTIVE
;
1706 * This routine is called whenever a serial port is opened. It
1707 * enables interrupts for a serial port, linking in its async structure into
1708 * the IRQ chain. It also performs the serial-speicific
1709 * initalization for the tty structure.
1711 int rs_open(struct tty_struct
*tty
, struct file
* filp
)
1713 struct async_struct
*info
;
1716 line
= DEV_TO_SL(tty
->line
);
1717 if ((line
< 0) || (line
>= NR_PORTS
))
1719 info
= rs_table
+ line
;
1720 #ifdef SERIAL_DEBUG_OPEN
1721 printk("rs_open ttys%d, count = %d\n", info
->line
, info
->count
);
1726 tty
->write
= rs_write
;
1727 tty
->close
= rs_close
;
1728 tty
->ioctl
= rs_ioctl
;
1729 tty
->throttle
= rs_throttle
;
1730 tty
->set_termios
= rs_set_termios
;
1731 tty
->stop
= rs_stop
;
1732 tty
->start
= rs_start
;
1733 tty
->hangup
= rs_hangup
;
1734 if ((info
->count
== 1) && (info
->flags
& ASYNC_SPLIT_TERMIOS
)) {
1735 if (MAJOR(filp
->f_rdev
) == TTY_MAJOR
)
1736 *tty
->termios
= info
->normal_termios
;
1738 *tty
->termios
= info
->callout_termios
;
1741 * Start up serial port
1743 retval
= startup(info
, 1);
1747 retval
= block_til_ready(tty
, filp
, info
);
1749 #ifdef SERIAL_DEBUG_OPEN
1750 printk("rs_open returning after block_til_ready with %d\n",
1756 info
->session
= current
->session
;
1757 info
->pgrp
= current
->pgrp
;
1759 #ifdef SERIAL_DEBUG_OPEN
1760 printk("rs_open ttys%d successful...", info
->line
);
1766 * ---------------------------------------------------------------------
1767 * rs_init() and friends
1769 * rs_init() is called at boot-time to initialize the serial driver.
1770 * ---------------------------------------------------------------------
1774 * This routine prints out the appropriate serial driver version
1775 * number, and identifies which options were configured into this
1778 static void show_serial_version(void)
1780 printk("Serial driver version 3.99a with");
1781 #ifdef CONFIG_AST_FOURPORT
1782 printk(" AST_FOURPORT");
1785 #ifdef CONFIG_ACCENT_ASYNC
1786 printk(" ACCENT_ASYNC");
1793 #ifdef CONFIG_AUTO_IRQ
1794 printk (" AUTO_IRQ");
1798 printk(" enabled\n");
1800 printk(" no serial options enabled\n");
1806 * This routine is called by do_auto_irq(); it attempts to determine
1807 * which interrupt a serial port is configured to use. It is not
1808 * fool-proof, but it works a large part of the time.
1810 static int get_auto_irq(struct async_struct
*info
)
1812 unsigned char save_MCR
, save_IER
, save_ICP
=0;
1813 unsigned short ICP
=0, port
= info
->port
;
1814 unsigned long timeout
;
1817 * Enable interrupts and see who answers
1819 rs_irq_triggered
= 0;
1821 save_IER
= serial_inp(info
, UART_IER
);
1822 save_MCR
= serial_inp(info
, UART_MCR
);
1823 if (info
->flags
& ASYNC_FOURPORT
) {
1824 serial_outp(info
, UART_MCR
, UART_MCR_DTR
| UART_MCR_RTS
);
1825 serial_outp(info
, UART_IER
, 0x0f); /* enable all intrs */
1826 ICP
= (port
& 0xFE0) | 0x01F;
1827 save_ICP
= inb_p(ICP
);
1831 serial_outp(info
, UART_MCR
,
1832 UART_MCR_DTR
| UART_MCR_RTS
| UART_MCR_OUT2
);
1833 serial_outp(info
, UART_IER
, 0x0f); /* enable all intrs */
1837 * Next, clear the interrupt registers.
1839 (void)serial_inp(info
, UART_LSR
);
1840 (void)serial_inp(info
, UART_RX
);
1841 (void)serial_inp(info
, UART_IIR
);
1842 (void)serial_inp(info
, UART_MSR
);
1844 timeout
= jiffies
+2;
1845 while (timeout
>= jiffies
) {
1846 if (rs_irq_triggered
)
1850 * Now check to see if we got any business, and clean up.
1853 serial_outp(info
, UART_IER
, save_IER
);
1854 serial_outp(info
, UART_MCR
, save_MCR
);
1855 if (info
->flags
& ASYNC_FOURPORT
)
1856 outb_p(save_ICP
, ICP
);
1858 return(rs_irq_triggered
);
1862 * Calls get_auto_irq() multiple times, to make sure we don't get
1863 * faked out by random interrupts
1865 static int do_auto_irq(struct async_struct
* info
)
1867 unsigned port
= info
->port
;
1869 int irq_try_1
= 0, irq_try_2
= 0;
1871 unsigned long flags
;
1876 /* Turn on interrupts (they may be off) */
1877 save_flags(flags
); sti();
1879 irq_lines
= grab_all_interrupts(rs_wild_int_mask
);
1881 for (retries
= 0; retries
< 5; retries
++) {
1883 irq_try_1
= get_auto_irq(info
);
1885 irq_try_2
= get_auto_irq(info
);
1886 if (irq_try_1
&& irq_try_2
) {
1887 if (irq_try_1
== irq_try_2
)
1889 irq_try_1
= irq_try_2
= 0;
1892 restore_flags(flags
);
1893 free_all_interrupts(irq_lines
);
1894 return (irq_try_1
== irq_try_2
) ? irq_try_1
: 0;
1898 * This routine is called by rs_init() to initialize a specific serial
1899 * port. It determines what type of UART ship this serial port is
1900 * using: 8250, 16450, 16550, 16550A. The important question is
1901 * whether or not this UART is a 16550A or not, since this will
1902 * determine whether or not we can use its FIFO features or not.
1904 static void autoconfig(struct async_struct
* info
)
1906 unsigned char status1
, status2
, scratch
, scratch2
;
1907 unsigned port
= info
->port
;
1908 unsigned long flags
;
1910 info
->type
= PORT_UNKNOWN
;
1915 save_flags(flags
); cli();
1918 * Do a simple existence test first; if we fail this, there's
1919 * no point trying anything else.
1921 scratch
= serial_inp(info
, UART_IER
);
1922 serial_outp(info
, UART_IER
, 0);
1923 scratch2
= serial_inp(info
, UART_IER
);
1924 serial_outp(info
, UART_IER
, scratch
);
1926 restore_flags(flags
);
1927 return; /* We failed; there's nothing here */
1931 * Check to see if a UART is really there. Certain broken
1932 * internal modems based on the Rockwell chipset fail this
1933 * test, because they apparently don't implement the loopback
1934 * test mode. So this test is skipped on the COM 1 through
1935 * COM 4 ports. This *should* be safe, since no board
1936 * manufactucturer would be stupid enough to design a board
1937 * that conflicts with COM 1-4 --- we hope!
1939 if (!(info
->flags
& ASYNC_SKIP_TEST
)) {
1940 scratch
= serial_inp(info
, UART_MCR
);
1941 serial_outp(info
, UART_MCR
, UART_MCR_LOOP
| scratch
);
1942 scratch2
= serial_inp(info
, UART_MSR
);
1943 serial_outp(info
, UART_MCR
, UART_MCR_LOOP
| 0x0A);
1944 status1
= serial_inp(info
, UART_MSR
) & 0xF0;
1945 serial_outp(info
, UART_MCR
, scratch
);
1946 serial_outp(info
, UART_MSR
, scratch2
);
1947 if (status1
!= 0x90) {
1948 restore_flags(flags
);
1954 * If the AUTO_IRQ flag is set, try to do the automatic IRQ
1957 if (info
->flags
& ASYNC_AUTO_IRQ
)
1958 info
->irq
= do_auto_irq(info
);
1960 serial_outp(info
, UART_FCR
, UART_FCR_ENABLE_FIFO
);
1961 scratch
= serial_in(info
, UART_IIR
) >> 6;
1962 info
->xmit_fifo_size
= 1;
1965 info
->type
= PORT_16450
;
1968 info
->type
= PORT_UNKNOWN
;
1971 info
->type
= PORT_16550
;
1974 info
->type
= PORT_16550A
;
1975 info
->xmit_fifo_size
= 16;
1978 if (info
->type
== PORT_16450
) {
1979 scratch
= serial_in(info
, UART_SCR
);
1980 serial_outp(info
, UART_SCR
, 0xa5);
1981 status1
= serial_in(info
, UART_SCR
);
1982 serial_outp(info
, UART_SCR
, 0x5a);
1983 status2
= serial_in(info
, UART_SCR
);
1984 serial_outp(info
, UART_SCR
, scratch
);
1986 if ((status1
!= 0xa5) || (status2
!= 0x5a))
1987 info
->type
= PORT_8250
;
1993 serial_outp(info
, UART_MCR
, 0x00);
1994 serial_outp(info
, UART_FCR
, (UART_FCR_CLEAR_RCVR
|
1995 UART_FCR_CLEAR_XMIT
));
1996 (void)serial_in(info
, UART_RX
);
1998 restore_flags(flags
);
2002 * The serial driver boot-time initialization code!
2004 long rs_init(long kmem_start
)
2007 struct async_struct
* info
;
2009 memset(&rs_event
, 0, sizeof(rs_event
));
2010 bh_base
[SERIAL_BH
].routine
= do_softint
;
2011 timer_table
[RS_TIMER
].fn
= rs_timer
;
2012 timer_table
[RS_TIMER
].expires
= 0;
2014 #ifdef CONFIG_AUTO_IRQ
2015 rs_wild_int_mask
= check_wild_interrupts(1);
2018 for (i
= 0; i
< 16; i
++) {
2023 show_serial_version();
2024 for (i
= 0, info
= rs_table
; i
< NR_PORTS
; i
++,info
++) {
2027 info
->type
= PORT_UNKNOWN
;
2028 info
->custom_divisor
= 0;
2029 info
->close_delay
= 50;
2033 info
->blocked_open
= 0;
2034 memset(&info
->callout_termios
, 0, sizeof(struct termios
));
2035 memset(&info
->normal_termios
, 0, sizeof(struct termios
));
2036 info
->open_wait
= 0;
2037 info
->xmit_wait
= 0;
2038 info
->close_wait
= 0;
2039 info
->next_port
= 0;
2040 info
->prev_port
= 0;
2043 if (!(info
->flags
& ASYNC_BOOT_AUTOCONF
))
2046 if (info
->type
== PORT_UNKNOWN
)
2048 printk("tty%02d%s at 0x%04x (irq = %d)", info
->line
,
2049 (info
->flags
& ASYNC_FOURPORT
) ? " FourPort" : "",
2050 info
->port
, info
->irq
);
2051 switch (info
->type
) {
2053 printk(" is a 8250\n");
2056 printk(" is a 16450\n");
2059 printk(" is a 16550\n");
2062 printk(" is a 16550A\n");