2 * esp.c - driver for Hayes ESP serial cards
4 * --- Notices from serial.c, upon which this driver is based ---
6 * Copyright (C) 1991, 1992 Linus Torvalds
8 * Extensively rewritten by Theodore Ts'o, 8/16/92 -- 9/14/92. Now
9 * much more extensible to support other serial cards based on the
10 * 16450/16550A UART's. Added support for the AST FourPort and the
13 * set_serial_info fixed to set the flags, custom divisor, and uart
14 * type fields. Fix suggested by Michael K. Johnson 12/12/92.
16 * 11/95: TIOCMIWAIT, TIOCGICOUNT by Angelo Haritsis <ah@doc.ic.ac.uk>
18 * 03/96: Modularised by Angelo Haritsis <ah@doc.ic.ac.uk>
20 * rs_set_termios fixed to look also for changes of the input
21 * flags INPCK, BRKINT, PARMRK, IGNPAR and IGNBRK.
22 * Bernd Anhäupl 05/17/96.
24 * --- End of notices from serial.c ---
26 * Support for the ESP serial card by Andrew J. Robinson
27 * <arobinso@nyx.net> (Card detection routine taken from a patch
28 * by Dennis J. Boylan). Patches to allow use with 2.1.x contributed
31 * Most recent changes: (Andrew J. Robinson)
32 * Support for PIO mode. This allows the driver to work properly with
35 * This module exports the following rs232 io functions:
37 * int espserial_init(void);
40 #include <linux/module.h>
41 #include <linux/errno.h>
42 #include <linux/signal.h>
43 #include <linux/sched.h>
44 #include <linux/interrupt.h>
45 #include <linux/tty.h>
46 #include <linux/tty_flip.h>
47 #include <linux/serial.h>
48 #include <linux/serialP.h>
49 #include <linux/serial_reg.h>
50 #include <linux/major.h>
51 #include <linux/string.h>
52 #include <linux/fcntl.h>
53 #include <linux/ptrace.h>
54 #include <linux/ioport.h>
56 #include <linux/init.h>
58 #include <asm/system.h>
60 #include <asm/segment.h>
61 #include <asm/bitops.h>
64 #include <linux/malloc.h>
65 #include <asm/uaccess.h>
67 #include <linux/hayesesp.h>
69 #define NR_PORTS 64 /* maximum number of ports */
70 #define NR_PRIMARY 8 /* maximum number of primary ports */
72 /* The following variables can be set by giving module options */
73 static int irq
[NR_PRIMARY
]; /* IRQ for each base port */
74 static unsigned int divisor
[NR_PRIMARY
]; /* custom divisor for each port */
75 static unsigned int dma
= ESP_DMA_CHANNEL
; /* DMA channel */
76 static unsigned int rx_trigger
= ESP_RX_TRIGGER
;
77 static unsigned int tx_trigger
= ESP_TX_TRIGGER
;
78 static unsigned int flow_off
= ESP_FLOW_OFF
;
79 static unsigned int flow_on
= ESP_FLOW_ON
;
80 static unsigned int rx_timeout
= ESP_RX_TMOUT
;
81 static unsigned int pio_threshold
= ESP_PIO_THRESHOLD
;
83 MODULE_PARM(irq
, "1-8i");
84 MODULE_PARM(divisor
, "1-8i");
85 MODULE_PARM(dma
, "i");
86 MODULE_PARM(rx_trigger
, "i");
87 MODULE_PARM(tx_trigger
, "i");
88 MODULE_PARM(flow_off
, "i");
89 MODULE_PARM(flow_on
, "i");
90 MODULE_PARM(rx_timeout
, "i");
91 MODULE_PARM(pio_threshold
, "i");
95 static char *dma_buffer
;
97 static struct esp_pio_buffer
*free_pio_buf
;
99 #define DMA_BUFFER_SZ 1024
101 #define WAKEUP_CHARS 1024
103 static char *serial_name
= "ESP serial driver";
104 static char *serial_version
= "2.2";
106 static DECLARE_TASK_QUEUE(tq_esp
);
108 static struct tty_driver esp_driver
, esp_callout_driver
;
109 static int serial_refcount
;
111 /* serial subtype definitions */
112 #define SERIAL_TYPE_NORMAL 1
113 #define SERIAL_TYPE_CALLOUT 2
116 * Serial driver configuration section. Here are the various options:
118 * SERIAL_PARANOIA_CHECK
119 * Check the magic number for the esp_structure where
123 #undef SERIAL_PARANOIA_CHECK
124 #define SERIAL_DO_RESTART
126 #undef SERIAL_DEBUG_INTR
127 #undef SERIAL_DEBUG_OPEN
128 #undef SERIAL_DEBUG_FLOW
130 #define _INLINE_ inline
132 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
133 #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
134 kdevname(tty->device), (info->flags), serial_refcount,info->count,tty->count,s)
139 static struct esp_struct
*ports
;
141 static void change_speed(struct esp_struct
*info
);
142 static void rs_wait_until_sent(struct tty_struct
*, int);
145 * The ESP card has a clock rate of 14.7456 MHz (that is, 2**ESPC_SCALE
146 * times the normal 1.8432 Mhz clock of most serial boards).
148 #define BASE_BAUD ((1843200 / 16) * (1 << ESPC_SCALE))
150 /* Standard COM flags (except for COM4, because of the 8514 problem) */
151 #define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
153 static struct tty_struct
*serial_table
[NR_PORTS
];
154 static struct termios
*serial_termios
[NR_PORTS
];
155 static struct termios
*serial_termios_locked
[NR_PORTS
];
158 #define MIN(a,b) ((a) < (b) ? (a) : (b))
162 * tmp_buf is used as a temporary buffer by serial_write. We need to
163 * lock it in case the memcpy_fromfs blocks while swapping in a page,
164 * and some other program tries to do a serial write at the same time.
165 * Since the lock will only come under contention when the system is
166 * swapping and available memory is low, it makes sense to share one
167 * buffer across all the serial ports, since it significantly saves
168 * memory if large numbers of serial ports are open.
170 static unsigned char *tmp_buf
;
171 static DECLARE_MUTEX(tmp_buf_sem
);
173 static inline int serial_paranoia_check(struct esp_struct
*info
,
174 kdev_t device
, const char *routine
)
176 #ifdef SERIAL_PARANOIA_CHECK
177 static const char *badmagic
=
178 "Warning: bad magic number for serial struct (%s) in %s\n";
179 static const char *badinfo
=
180 "Warning: null esp_struct for (%s) in %s\n";
183 printk(badinfo
, kdevname(device
), routine
);
186 if (info
->magic
!= ESP_MAGIC
) {
187 printk(badmagic
, kdevname(device
), routine
);
194 static inline unsigned int serial_in(struct esp_struct
*info
, int offset
)
196 return inb(info
->port
+ offset
);
199 static inline void serial_out(struct esp_struct
*info
, int offset
,
202 outb(value
, info
->port
+offset
);
206 * ------------------------------------------------------------
207 * rs_stop() and rs_start()
209 * This routines are called before setting or resetting tty->stopped.
210 * They enable or disable transmitter interrupts, as necessary.
211 * ------------------------------------------------------------
213 static void rs_stop(struct tty_struct
*tty
)
215 struct esp_struct
*info
= (struct esp_struct
*)tty
->driver_data
;
218 if (serial_paranoia_check(info
, tty
->device
, "rs_stop"))
221 save_flags(flags
); cli();
222 if (info
->IER
& UART_IER_THRI
) {
223 info
->IER
&= ~UART_IER_THRI
;
224 serial_out(info
, UART_ESI_CMD1
, ESI_SET_SRV_MASK
);
225 serial_out(info
, UART_ESI_CMD2
, info
->IER
);
228 restore_flags(flags
);
231 static void rs_start(struct tty_struct
*tty
)
233 struct esp_struct
*info
= (struct esp_struct
*)tty
->driver_data
;
236 if (serial_paranoia_check(info
, tty
->device
, "rs_start"))
239 save_flags(flags
); cli();
240 if (info
->xmit_cnt
&& info
->xmit_buf
&& !(info
->IER
& UART_IER_THRI
)) {
241 info
->IER
|= UART_IER_THRI
;
242 serial_out(info
, UART_ESI_CMD1
, ESI_SET_SRV_MASK
);
243 serial_out(info
, UART_ESI_CMD2
, info
->IER
);
245 restore_flags(flags
);
249 * ----------------------------------------------------------------------
251 * Here starts the interrupt handling routines. All of the following
252 * subroutines are declared as inline and are folded into
253 * rs_interrupt(). They were separated out for readability's sake.
255 * Note: rs_interrupt() is a "fast" interrupt, which means that it
256 * runs with interrupts turned off. People who may want to modify
257 * rs_interrupt() should try to keep the interrupt handler as fast as
258 * possible. After you are done making modifications, it is not a bad
261 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
263 * and look at the resulting assemble code in serial.s.
265 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
266 * -----------------------------------------------------------------------
270 * This routine is used by the interrupt handler to schedule
271 * processing in the software interrupt portion of the driver.
273 static _INLINE_
void rs_sched_event(struct esp_struct
*info
,
276 info
->event
|= 1 << event
;
277 queue_task(&info
->tqueue
, &tq_esp
);
280 static _INLINE_
struct esp_pio_buffer
*get_pio_buffer(void)
282 struct esp_pio_buffer
*buf
;
286 free_pio_buf
= buf
->next
;
288 buf
= (struct esp_pio_buffer
*)
289 kmalloc(sizeof(struct esp_pio_buffer
), GFP_ATOMIC
);
295 static _INLINE_
void release_pio_buffer(struct esp_pio_buffer
*buf
)
297 buf
->next
= free_pio_buf
;
301 static _INLINE_
void receive_chars_pio(struct esp_struct
*info
, int num_bytes
)
303 struct tty_struct
*tty
= info
->tty
;
305 struct esp_pio_buffer
*pio_buf
;
306 struct esp_pio_buffer
*err_buf
;
307 unsigned char status_mask
;
309 pio_buf
= get_pio_buffer();
314 err_buf
= get_pio_buffer();
317 release_pio_buffer(pio_buf
);
323 status_mask
= (info
->read_status_mask
>> 2) & 0x07;
325 for (i
= 0; i
< num_bytes
- 1; i
+= 2) {
326 *((unsigned short *)(pio_buf
->data
+ i
)) =
327 inw(info
->port
+ UART_ESI_RX
);
328 err_buf
->data
[i
] = serial_in(info
, UART_ESI_RWS
);
329 err_buf
->data
[i
+ 1] = (err_buf
->data
[i
] >> 3) & status_mask
;
330 err_buf
->data
[i
] &= status_mask
;
333 if (num_bytes
& 0x0001) {
334 pio_buf
->data
[num_bytes
- 1] = serial_in(info
, UART_ESI_RX
);
335 err_buf
->data
[num_bytes
- 1] =
336 (serial_in(info
, UART_ESI_RWS
) >> 3) & status_mask
;
341 /* make sure everything is still ok since interrupts were enabled */
345 release_pio_buffer(pio_buf
);
346 release_pio_buffer(err_buf
);
347 info
->stat_flags
&= ~ESP_STAT_RX_TIMEOUT
;
351 status_mask
= (info
->ignore_status_mask
>> 2) & 0x07;
353 for (i
= 0; i
< num_bytes
; i
++) {
354 if (!(err_buf
->data
[i
] & status_mask
)) {
355 *(tty
->flip
.char_buf_ptr
++) = pio_buf
->data
[i
];
357 if (err_buf
->data
[i
] & 0x04) {
358 *(tty
->flip
.flag_buf_ptr
++) = TTY_BREAK
;
360 if (info
->flags
& ASYNC_SAK
)
363 else if (err_buf
->data
[i
] & 0x02)
364 *(tty
->flip
.flag_buf_ptr
++) = TTY_FRAME
;
365 else if (err_buf
->data
[i
] & 0x01)
366 *(tty
->flip
.flag_buf_ptr
++) = TTY_PARITY
;
368 *(tty
->flip
.flag_buf_ptr
++) = 0;
374 queue_task(&tty
->flip
.tqueue
, &tq_timer
);
376 info
->stat_flags
&= ~ESP_STAT_RX_TIMEOUT
;
377 release_pio_buffer(pio_buf
);
378 release_pio_buffer(err_buf
);
381 static _INLINE_
void receive_chars_dma(struct esp_struct
*info
, int num_bytes
)
384 info
->stat_flags
&= ~ESP_STAT_RX_TIMEOUT
;
385 dma_bytes
= num_bytes
;
386 info
->stat_flags
|= ESP_STAT_DMA_RX
;
388 flags
=claim_dma_lock();
391 set_dma_mode(dma
, DMA_MODE_READ
);
392 set_dma_addr(dma
, virt_to_bus(dma_buffer
));
393 set_dma_count(dma
, dma_bytes
);
395 release_dma_lock(flags
);
397 serial_out(info
, UART_ESI_CMD1
, ESI_START_DMA_RX
);
400 static _INLINE_
void receive_chars_dma_done(struct esp_struct
*info
,
403 struct tty_struct
*tty
= info
->tty
;
408 flags
=claim_dma_lock();
412 info
->stat_flags
&= ~ESP_STAT_DMA_RX
;
413 num_bytes
= dma_bytes
- get_dma_residue(dma
);
414 release_dma_lock(flags
);
416 info
->icount
.rx
+= num_bytes
;
418 memcpy(tty
->flip
.char_buf_ptr
, dma_buffer
, num_bytes
);
419 tty
->flip
.char_buf_ptr
+= num_bytes
;
420 tty
->flip
.count
+= num_bytes
;
421 memset(tty
->flip
.flag_buf_ptr
, 0, num_bytes
);
422 tty
->flip
.flag_buf_ptr
+= num_bytes
;
425 tty
->flip
.flag_buf_ptr
--;
427 status
&= (0x1c & info
->read_status_mask
);
429 if (status
& info
->ignore_status_mask
) {
431 tty
->flip
.char_buf_ptr
--;
432 tty
->flip
.flag_buf_ptr
--;
433 } else if (status
& 0x10) {
434 *tty
->flip
.flag_buf_ptr
= TTY_BREAK
;
435 (info
->icount
.brk
)++;
436 if (info
->flags
& ASYNC_SAK
)
438 } else if (status
& 0x08) {
439 *tty
->flip
.flag_buf_ptr
= TTY_FRAME
;
440 (info
->icount
.frame
)++;
442 else if (status
& 0x04) {
443 *tty
->flip
.flag_buf_ptr
= TTY_PARITY
;
444 (info
->icount
.parity
)++;
447 tty
->flip
.flag_buf_ptr
++;
449 queue_task(&tty
->flip
.tqueue
, &tq_timer
);
452 if (dma_bytes
!= num_bytes
) {
453 num_bytes
= dma_bytes
- num_bytes
;
455 receive_chars_dma(info
, num_bytes
);
460 static _INLINE_
void transmit_chars_pio(struct esp_struct
*info
,
464 struct esp_pio_buffer
*pio_buf
;
466 pio_buf
= get_pio_buffer();
471 while (space_avail
&& info
->xmit_cnt
) {
472 if (info
->xmit_tail
+ space_avail
<= ESP_XMIT_SIZE
) {
473 memcpy(pio_buf
->data
,
474 &(info
->xmit_buf
[info
->xmit_tail
]),
477 i
= ESP_XMIT_SIZE
- info
->xmit_tail
;
478 memcpy(pio_buf
->data
,
479 &(info
->xmit_buf
[info
->xmit_tail
]), i
);
480 memcpy(&(pio_buf
->data
[i
]), info
->xmit_buf
,
484 info
->xmit_cnt
-= space_avail
;
485 info
->xmit_tail
= (info
->xmit_tail
+ space_avail
) &
490 for (i
= 0; i
< space_avail
- 1; i
+= 2) {
491 outw(*((unsigned short *)(pio_buf
->data
+ i
)),
492 info
->port
+ UART_ESI_TX
);
495 if (space_avail
& 0x0001)
496 serial_out(info
, UART_ESI_TX
,
497 pio_buf
->data
[space_avail
- 1]);
501 if (info
->xmit_cnt
) {
502 serial_out(info
, UART_ESI_CMD1
, ESI_NO_COMMAND
);
503 serial_out(info
, UART_ESI_CMD1
, ESI_GET_TX_AVAIL
);
504 space_avail
= serial_in(info
, UART_ESI_STAT1
) << 8;
505 space_avail
|= serial_in(info
, UART_ESI_STAT2
);
507 if (space_avail
> info
->xmit_cnt
)
508 space_avail
= info
->xmit_cnt
;
512 if (info
->xmit_cnt
< WAKEUP_CHARS
) {
513 rs_sched_event(info
, ESP_EVENT_WRITE_WAKEUP
);
515 #ifdef SERIAL_DEBUG_INTR
519 if (info
->xmit_cnt
<= 0) {
520 info
->IER
&= ~UART_IER_THRI
;
521 serial_out(info
, UART_ESI_CMD1
,
523 serial_out(info
, UART_ESI_CMD2
, info
->IER
);
527 release_pio_buffer(pio_buf
);
530 static _INLINE_
void transmit_chars_dma(struct esp_struct
*info
, int num_bytes
)
534 dma_bytes
= num_bytes
;
536 if (info
->xmit_tail
+ dma_bytes
<= ESP_XMIT_SIZE
) {
537 memcpy(dma_buffer
, &(info
->xmit_buf
[info
->xmit_tail
]),
540 int i
= ESP_XMIT_SIZE
- info
->xmit_tail
;
541 memcpy(dma_buffer
, &(info
->xmit_buf
[info
->xmit_tail
]),
543 memcpy(&(dma_buffer
[i
]), info
->xmit_buf
, dma_bytes
- i
);
546 info
->xmit_cnt
-= dma_bytes
;
547 info
->xmit_tail
= (info
->xmit_tail
+ dma_bytes
) & (ESP_XMIT_SIZE
- 1);
549 if (info
->xmit_cnt
< WAKEUP_CHARS
) {
550 rs_sched_event(info
, ESP_EVENT_WRITE_WAKEUP
);
552 #ifdef SERIAL_DEBUG_INTR
556 if (info
->xmit_cnt
<= 0) {
557 info
->IER
&= ~UART_IER_THRI
;
558 serial_out(info
, UART_ESI_CMD1
, ESI_SET_SRV_MASK
);
559 serial_out(info
, UART_ESI_CMD2
, info
->IER
);
563 info
->stat_flags
|= ESP_STAT_DMA_TX
;
565 flags
=claim_dma_lock();
568 set_dma_mode(dma
, DMA_MODE_WRITE
);
569 set_dma_addr(dma
, virt_to_bus(dma_buffer
));
570 set_dma_count(dma
, dma_bytes
);
572 release_dma_lock(flags
);
574 serial_out(info
, UART_ESI_CMD1
, ESI_START_DMA_TX
);
577 static _INLINE_
void transmit_chars_dma_done(struct esp_struct
*info
)
583 flags
=claim_dma_lock();
587 num_bytes
= dma_bytes
- get_dma_residue(dma
);
588 info
->icount
.tx
+= dma_bytes
;
589 release_dma_lock(flags
);
591 if (dma_bytes
!= num_bytes
) {
592 dma_bytes
-= num_bytes
;
593 memmove(dma_buffer
, dma_buffer
+ num_bytes
, dma_bytes
);
595 flags
=claim_dma_lock();
598 set_dma_mode(dma
, DMA_MODE_WRITE
);
599 set_dma_addr(dma
, virt_to_bus(dma_buffer
));
600 set_dma_count(dma
, dma_bytes
);
602 release_dma_lock(flags
);
604 serial_out(info
, UART_ESI_CMD1
, ESI_START_DMA_TX
);
607 info
->stat_flags
&= ~ESP_STAT_DMA_TX
;
611 static _INLINE_
void check_modem_status(struct esp_struct
*info
)
615 serial_out(info
, UART_ESI_CMD1
, ESI_GET_UART_STAT
);
616 status
= serial_in(info
, UART_ESI_STAT2
);
618 if (status
& UART_MSR_ANY_DELTA
) {
619 /* update input line counters */
620 if (status
& UART_MSR_TERI
)
622 if (status
& UART_MSR_DDSR
)
624 if (status
& UART_MSR_DDCD
)
626 if (status
& UART_MSR_DCTS
)
628 wake_up_interruptible(&info
->delta_msr_wait
);
631 if ((info
->flags
& ASYNC_CHECK_CD
) && (status
& UART_MSR_DDCD
)) {
632 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
633 printk("ttys%d CD now %s...", info
->line
,
634 (status
& UART_MSR_DCD
) ? "on" : "off");
636 if (status
& UART_MSR_DCD
)
637 wake_up_interruptible(&info
->open_wait
);
638 else if (!((info
->flags
& ASYNC_CALLOUT_ACTIVE
) &&
639 (info
->flags
& ASYNC_CALLOUT_NOHUP
))) {
640 #ifdef SERIAL_DEBUG_OPEN
641 printk("scheduling hangup...");
643 queue_task(&info
->tqueue_hangup
, &tq_scheduler
);
649 * This is the serial driver's interrupt routine
651 static void rs_interrupt_single(int irq
, void *dev_id
, struct pt_regs
* regs
)
653 struct esp_struct
* info
;
655 unsigned int scratch
;
657 #ifdef SERIAL_DEBUG_INTR
658 printk("rs_interrupt_single(%d)...", irq
);
660 info
= (struct esp_struct
*)dev_id
;
662 scratch
= serial_in(info
, UART_ESI_SID
);
671 if (scratch
& 0x04) { /* error */
672 serial_out(info
, UART_ESI_CMD1
, ESI_GET_ERR_STAT
);
673 err_status
= serial_in(info
, UART_ESI_STAT1
);
674 serial_in(info
, UART_ESI_STAT2
);
676 if (err_status
& 0x01)
677 info
->stat_flags
|= ESP_STAT_RX_TIMEOUT
;
679 if (err_status
& 0x20) /* UART status */
680 check_modem_status(info
);
682 if (err_status
& 0x80) /* Start break */
683 wake_up_interruptible(&info
->break_wait
);
686 if ((scratch
& 0x88) || /* DMA completed or timed out */
687 (err_status
& 0x1c) /* receive error */) {
688 if (info
->stat_flags
& ESP_STAT_DMA_RX
)
689 receive_chars_dma_done(info
, err_status
);
690 else if (info
->stat_flags
& ESP_STAT_DMA_TX
)
691 transmit_chars_dma_done(info
);
694 if (!(info
->stat_flags
& (ESP_STAT_DMA_RX
| ESP_STAT_DMA_TX
)) &&
695 ((scratch
& 0x01) || (info
->stat_flags
& ESP_STAT_RX_TIMEOUT
)) &&
696 (info
->IER
& UART_IER_RDI
)) {
699 serial_out(info
, UART_ESI_CMD1
, ESI_NO_COMMAND
);
700 serial_out(info
, UART_ESI_CMD1
, ESI_GET_RX_AVAIL
);
701 num_bytes
= serial_in(info
, UART_ESI_STAT1
) << 8;
702 num_bytes
|= serial_in(info
, UART_ESI_STAT2
);
704 if (num_bytes
> (TTY_FLIPBUF_SIZE
- info
->tty
->flip
.count
))
705 num_bytes
= TTY_FLIPBUF_SIZE
- info
->tty
->flip
.count
;
709 (info
->stat_flags
& ESP_STAT_USE_PIO
) ||
710 (num_bytes
<= info
->config
.pio_threshold
))
711 receive_chars_pio(info
, num_bytes
);
713 receive_chars_dma(info
, num_bytes
);
717 if (!(info
->stat_flags
& (ESP_STAT_DMA_RX
| ESP_STAT_DMA_TX
)) &&
718 (scratch
& 0x02) && (info
->IER
& UART_IER_THRI
)) {
719 if ((info
->xmit_cnt
<= 0) || info
->tty
->stopped
) {
720 info
->IER
&= ~UART_IER_THRI
;
721 serial_out(info
, UART_ESI_CMD1
, ESI_SET_SRV_MASK
);
722 serial_out(info
, UART_ESI_CMD2
, info
->IER
);
726 serial_out(info
, UART_ESI_CMD1
, ESI_NO_COMMAND
);
727 serial_out(info
, UART_ESI_CMD1
, ESI_GET_TX_AVAIL
);
728 num_bytes
= serial_in(info
, UART_ESI_STAT1
) << 8;
729 num_bytes
|= serial_in(info
, UART_ESI_STAT2
);
731 if (num_bytes
> info
->xmit_cnt
)
732 num_bytes
= info
->xmit_cnt
;
736 (info
->stat_flags
& ESP_STAT_USE_PIO
) ||
737 (num_bytes
<= info
->config
.pio_threshold
))
738 transmit_chars_pio(info
, num_bytes
);
740 transmit_chars_dma(info
, num_bytes
);
745 info
->last_active
= jiffies
;
747 #ifdef SERIAL_DEBUG_INTR
754 * -------------------------------------------------------------------
755 * Here ends the serial interrupt routines.
756 * -------------------------------------------------------------------
760 * This routine is used to handle the "bottom half" processing for the
761 * serial driver, known also the "software interrupt" processing.
762 * This processing is done at the kernel interrupt level, after the
763 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
764 * is where time-consuming activities which can not be done in the
765 * interrupt driver proper are done; the interrupt driver schedules
766 * them using rs_sched_event(), and they get done here.
768 static void do_serial_bh(void)
770 run_task_queue(&tq_esp
);
773 static void do_softint(void *private_
)
775 struct esp_struct
*info
= (struct esp_struct
*) private_
;
776 struct tty_struct
*tty
;
782 if (test_and_clear_bit(ESP_EVENT_WRITE_WAKEUP
, &info
->event
)) {
783 if ((tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) &&
784 tty
->ldisc
.write_wakeup
)
785 (tty
->ldisc
.write_wakeup
)(tty
);
786 wake_up_interruptible(&tty
->write_wait
);
791 * This routine is called from the scheduler tqueue when the interrupt
792 * routine has signalled that a hangup has occurred. The path of
793 * hangup processing is:
795 * serial interrupt routine -> (scheduler tqueue) ->
796 * do_serial_hangup() -> tty->hangup() -> esp_hangup()
799 static void do_serial_hangup(void *private_
)
801 struct esp_struct
*info
= (struct esp_struct
*) private_
;
802 struct tty_struct
*tty
;
812 * ---------------------------------------------------------------
813 * Low level utility subroutines for the serial driver: routines to
814 * figure out the appropriate timeout for an interrupt chain, routines
815 * to initialize and startup a serial port, and routines to shutdown a
816 * serial port. Useful stuff like that.
817 * ---------------------------------------------------------------
820 static _INLINE_
void esp_basic_init(struct esp_struct
* info
)
822 /* put ESPC in enhanced mode */
823 serial_out(info
, UART_ESI_CMD1
, ESI_SET_MODE
);
825 if (info
->stat_flags
& ESP_STAT_NEVER_DMA
)
826 serial_out(info
, UART_ESI_CMD2
, 0x01);
828 serial_out(info
, UART_ESI_CMD2
, 0x31);
830 /* disable interrupts for now */
831 serial_out(info
, UART_ESI_CMD1
, ESI_SET_SRV_MASK
);
832 serial_out(info
, UART_ESI_CMD2
, 0x00);
834 /* set interrupt and DMA channel */
835 serial_out(info
, UART_ESI_CMD1
, ESI_SET_IRQ
);
837 if (info
->stat_flags
& ESP_STAT_NEVER_DMA
)
838 serial_out(info
, UART_ESI_CMD2
, 0x01);
840 serial_out(info
, UART_ESI_CMD2
, (dma
<< 4) | 0x01);
842 serial_out(info
, UART_ESI_CMD1
, ESI_SET_ENH_IRQ
);
844 if (info
->line
% 8) /* secondary port */
845 serial_out(info
, UART_ESI_CMD2
, 0x0d); /* shared */
846 else if (info
->irq
== 9)
847 serial_out(info
, UART_ESI_CMD2
, 0x02);
849 serial_out(info
, UART_ESI_CMD2
, info
->irq
);
851 /* set error status mask (check this) */
852 serial_out(info
, UART_ESI_CMD1
, ESI_SET_ERR_MASK
);
854 if (info
->stat_flags
& ESP_STAT_NEVER_DMA
)
855 serial_out(info
, UART_ESI_CMD2
, 0xa1);
857 serial_out(info
, UART_ESI_CMD2
, 0xbd);
859 serial_out(info
, UART_ESI_CMD2
, 0x00);
861 /* set DMA timeout */
862 serial_out(info
, UART_ESI_CMD1
, ESI_SET_DMA_TMOUT
);
863 serial_out(info
, UART_ESI_CMD2
, 0xff);
865 /* set FIFO trigger levels */
866 serial_out(info
, UART_ESI_CMD1
, ESI_SET_TRIGGER
);
867 serial_out(info
, UART_ESI_CMD2
, info
->config
.rx_trigger
>> 8);
868 serial_out(info
, UART_ESI_CMD2
, info
->config
.rx_trigger
);
869 serial_out(info
, UART_ESI_CMD2
, info
->config
.tx_trigger
>> 8);
870 serial_out(info
, UART_ESI_CMD2
, info
->config
.tx_trigger
);
872 /* Set clock scaling and wait states */
873 serial_out(info
, UART_ESI_CMD1
, ESI_SET_PRESCALAR
);
874 serial_out(info
, UART_ESI_CMD2
, 0x04 | ESPC_SCALE
);
876 /* set reinterrupt pacing */
877 serial_out(info
, UART_ESI_CMD1
, ESI_SET_REINTR
);
878 serial_out(info
, UART_ESI_CMD2
, 0xff);
881 static int startup(struct esp_struct
* info
)
885 unsigned int num_chars
;
887 save_flags(flags
); cli();
889 if (info
->flags
& ASYNC_INITIALIZED
) {
890 restore_flags(flags
);
894 if (!info
->xmit_buf
) {
895 info
->xmit_buf
= (unsigned char *)get_free_page(GFP_KERNEL
);
896 if (!info
->xmit_buf
) {
897 restore_flags(flags
);
902 #ifdef SERIAL_DEBUG_OPEN
903 printk("starting up ttys%d (irq %d)...", info
->line
, info
->irq
);
906 /* Flush the RX buffer. Using the ESI flush command may cause */
907 /* wild interrupts, so read all the data instead. */
909 serial_out(info
, UART_ESI_CMD1
, ESI_NO_COMMAND
);
910 serial_out(info
, UART_ESI_CMD1
, ESI_GET_RX_AVAIL
);
911 num_chars
= serial_in(info
, UART_ESI_STAT1
) << 8;
912 num_chars
|= serial_in(info
, UART_ESI_STAT2
);
914 while (num_chars
> 1) {
915 inw(info
->port
+ UART_ESI_RX
);
920 serial_in(info
, UART_ESI_RX
);
922 /* set receive character timeout */
923 serial_out(info
, UART_ESI_CMD1
, ESI_SET_RX_TIMEOUT
);
924 serial_out(info
, UART_ESI_CMD2
, info
->config
.rx_timeout
);
926 /* clear all flags except the "never DMA" flag */
927 info
->stat_flags
&= ESP_STAT_NEVER_DMA
;
929 if (info
->stat_flags
& ESP_STAT_NEVER_DMA
)
930 info
->stat_flags
|= ESP_STAT_USE_PIO
;
936 retval
= request_irq(info
->irq
, rs_interrupt_single
, SA_SHIRQ
,
940 if (capable(CAP_SYS_ADMIN
)) {
942 set_bit(TTY_IO_ERROR
,
947 restore_flags(flags
);
951 if (!(info
->stat_flags
& ESP_STAT_USE_PIO
) && !dma_buffer
) {
952 dma_buffer
= (char *)__get_dma_pages(
953 GFP_KERNEL
, get_order(DMA_BUFFER_SZ
));
955 /* use PIO mode if DMA buf/chan cannot be allocated */
957 info
->stat_flags
|= ESP_STAT_USE_PIO
;
958 else if (request_dma(dma
, "esp serial")) {
959 free_pages((unsigned int)dma_buffer
,
960 get_order(DMA_BUFFER_SZ
));
962 info
->stat_flags
|= ESP_STAT_USE_PIO
;
967 info
->MCR
= UART_MCR_DTR
| UART_MCR_RTS
| UART_MCR_OUT2
;
968 serial_out(info
, UART_ESI_CMD1
, ESI_WRITE_UART
);
969 serial_out(info
, UART_ESI_CMD2
, UART_MCR
);
970 serial_out(info
, UART_ESI_CMD2
, info
->MCR
);
973 * Finally, enable interrupts
975 /* info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI; */
976 info
->IER
= UART_IER_RLSI
| UART_IER_RDI
| UART_IER_DMA_TMOUT
|
978 serial_out(info
, UART_ESI_CMD1
, ESI_SET_SRV_MASK
);
979 serial_out(info
, UART_ESI_CMD2
, info
->IER
);
982 clear_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
983 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
986 * Set up the tty->alt_speed kludge
989 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_HI
)
990 info
->tty
->alt_speed
= 57600;
991 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_VHI
)
992 info
->tty
->alt_speed
= 115200;
993 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_SHI
)
994 info
->tty
->alt_speed
= 230400;
995 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_WARP
)
996 info
->tty
->alt_speed
= 460800;
1000 * set the speed of the serial port
1004 info
->flags
|= ASYNC_INITIALIZED
;
1005 restore_flags(flags
);
1010 * This routine will shutdown a serial port; interrupts are disabled, and
1011 * DTR is dropped if the hangup on close termio flag is on.
1013 static void shutdown(struct esp_struct
* info
)
1015 unsigned long flags
, f
;
1017 if (!(info
->flags
& ASYNC_INITIALIZED
))
1020 #ifdef SERIAL_DEBUG_OPEN
1021 printk("Shutting down serial port %d (irq %d)....", info
->line
,
1025 save_flags(flags
); cli(); /* Disable interrupts */
1028 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1029 * here so the queue might never be waken up
1031 wake_up_interruptible(&info
->delta_msr_wait
);
1032 wake_up_interruptible(&info
->break_wait
);
1034 /* stop a DMA transfer on the port being closed */
1036 if (info
->stat_flags
& (ESP_STAT_DMA_RX
| ESP_STAT_DMA_TX
)) {
1040 release_dma_lock(f
);
1048 free_irq(info
->irq
, info
);
1051 struct esp_struct
*current_port
= ports
;
1053 while (current_port
) {
1054 if ((current_port
!= info
) &&
1055 (current_port
->flags
& ASYNC_INITIALIZED
))
1058 current_port
= current_port
->next_port
;
1061 if (!current_port
) {
1063 free_pages((unsigned int)dma_buffer
,
1064 get_order(DMA_BUFFER_SZ
));
1069 if (info
->xmit_buf
) {
1070 free_page((unsigned long) info
->xmit_buf
);
1075 serial_out(info
, UART_ESI_CMD1
, ESI_SET_SRV_MASK
);
1076 serial_out(info
, UART_ESI_CMD2
, 0x00);
1078 if (!info
->tty
|| (info
->tty
->termios
->c_cflag
& HUPCL
))
1079 info
->MCR
&= ~(UART_MCR_DTR
|UART_MCR_RTS
);
1081 info
->MCR
&= ~UART_MCR_OUT2
;
1082 serial_out(info
, UART_ESI_CMD1
, ESI_WRITE_UART
);
1083 serial_out(info
, UART_ESI_CMD2
, UART_MCR
);
1084 serial_out(info
, UART_ESI_CMD2
, info
->MCR
);
1087 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
1089 info
->flags
&= ~ASYNC_INITIALIZED
;
1090 restore_flags(flags
);
1094 * This routine is called to set the UART divisor registers to match
1095 * the specified baud rate for a serial port.
1097 static void change_speed(struct esp_struct
*info
)
1099 unsigned short port
;
1101 unsigned cflag
,cval
;
1103 unsigned char flow1
= 0, flow2
= 0;
1104 unsigned long flags
;
1106 if (!info
->tty
|| !info
->tty
->termios
)
1108 cflag
= info
->tty
->termios
->c_cflag
;
1111 /* byte size and parity */
1112 switch (cflag
& CSIZE
) {
1113 case CS5
: cval
= 0x00; bits
= 7; break;
1114 case CS6
: cval
= 0x01; bits
= 8; break;
1115 case CS7
: cval
= 0x02; bits
= 9; break;
1116 case CS8
: cval
= 0x03; bits
= 10; break;
1117 default: cval
= 0x00; bits
= 7; break;
1119 if (cflag
& CSTOPB
) {
1123 if (cflag
& PARENB
) {
1124 cval
|= UART_LCR_PARITY
;
1127 if (!(cflag
& PARODD
))
1128 cval
|= UART_LCR_EPAR
;
1131 cval
|= UART_LCR_SPAR
;
1134 baud
= tty_get_baud_rate(info
->tty
);
1135 if (baud
== 38400 &&
1136 ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_CUST
))
1137 quot
= info
->custom_divisor
;
1140 /* Special case since 134 is really 134.5 */
1141 quot
= (2*BASE_BAUD
/ 269);
1143 quot
= BASE_BAUD
/ baud
;
1145 /* If the quotient is ever zero, default to 9600 bps */
1147 quot
= BASE_BAUD
/ 9600;
1149 info
->timeout
= ((1024 * HZ
* bits
* quot
) / BASE_BAUD
) + (HZ
/ 50);
1151 /* CTS flow control flag and modem status interrupts */
1152 /* info->IER &= ~UART_IER_MSI; */
1153 if (cflag
& CRTSCTS
) {
1154 info
->flags
|= ASYNC_CTS_FLOW
;
1155 /* info->IER |= UART_IER_MSI; */
1159 info
->flags
&= ~ASYNC_CTS_FLOW
;
1161 info
->flags
&= ~ASYNC_CHECK_CD
;
1163 info
->flags
|= ASYNC_CHECK_CD
;
1164 /* info->IER |= UART_IER_MSI; */
1168 * Set up parity check flag
1170 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1172 info
->read_status_mask
= UART_LSR_OE
| UART_LSR_THRE
| UART_LSR_DR
;
1173 if (I_INPCK(info
->tty
))
1174 info
->read_status_mask
|= UART_LSR_FE
| UART_LSR_PE
;
1175 if (I_BRKINT(info
->tty
) || I_PARMRK(info
->tty
))
1176 info
->read_status_mask
|= UART_LSR_BI
;
1178 info
->ignore_status_mask
= 0;
1180 /* This should be safe, but for some broken bits of hardware... */
1181 if (I_IGNPAR(info
->tty
)) {
1182 info
->ignore_status_mask
|= UART_LSR_PE
| UART_LSR_FE
;
1183 info
->read_status_mask
|= UART_LSR_PE
| UART_LSR_FE
;
1186 if (I_IGNBRK(info
->tty
)) {
1187 info
->ignore_status_mask
|= UART_LSR_BI
;
1188 info
->read_status_mask
|= UART_LSR_BI
;
1190 * If we're ignore parity and break indicators, ignore
1191 * overruns too. (For real raw support).
1193 if (I_IGNPAR(info
->tty
)) {
1194 info
->ignore_status_mask
|= UART_LSR_OE
| \
1195 UART_LSR_PE
| UART_LSR_FE
;
1196 info
->read_status_mask
|= UART_LSR_OE
| \
1197 UART_LSR_PE
| UART_LSR_FE
;
1201 if (I_IXOFF(info
->tty
))
1204 save_flags(flags
); cli();
1206 serial_out(info
, UART_ESI_CMD1
, ESI_SET_BAUD
);
1207 serial_out(info
, UART_ESI_CMD2
, quot
>> 8);
1208 serial_out(info
, UART_ESI_CMD2
, quot
& 0xff);
1210 /* set data bits, parity, etc. */
1211 serial_out(info
, UART_ESI_CMD1
, ESI_WRITE_UART
);
1212 serial_out(info
, UART_ESI_CMD2
, UART_LCR
);
1213 serial_out(info
, UART_ESI_CMD2
, cval
);
1215 /* Enable flow control */
1216 serial_out(info
, UART_ESI_CMD1
, ESI_SET_FLOW_CNTL
);
1217 serial_out(info
, UART_ESI_CMD2
, flow1
);
1218 serial_out(info
, UART_ESI_CMD2
, flow2
);
1220 /* set flow control characters (XON/XOFF only) */
1221 if (I_IXOFF(info
->tty
)) {
1222 serial_out(info
, UART_ESI_CMD1
, ESI_SET_FLOW_CHARS
);
1223 serial_out(info
, UART_ESI_CMD2
, START_CHAR(info
->tty
));
1224 serial_out(info
, UART_ESI_CMD2
, STOP_CHAR(info
->tty
));
1225 serial_out(info
, UART_ESI_CMD2
, 0x10);
1226 serial_out(info
, UART_ESI_CMD2
, 0x21);
1227 switch (cflag
& CSIZE
) {
1229 serial_out(info
, UART_ESI_CMD2
, 0x1f);
1232 serial_out(info
, UART_ESI_CMD2
, 0x3f);
1236 serial_out(info
, UART_ESI_CMD2
, 0x7f);
1239 serial_out(info
, UART_ESI_CMD2
, 0xff);
1244 /* Set high/low water */
1245 serial_out(info
, UART_ESI_CMD1
, ESI_SET_FLOW_LVL
);
1246 serial_out(info
, UART_ESI_CMD2
, info
->config
.flow_off
>> 8);
1247 serial_out(info
, UART_ESI_CMD2
, info
->config
.flow_off
);
1248 serial_out(info
, UART_ESI_CMD2
, info
->config
.flow_on
>> 8);
1249 serial_out(info
, UART_ESI_CMD2
, info
->config
.flow_on
);
1251 restore_flags(flags
);
1254 static void rs_put_char(struct tty_struct
*tty
, unsigned char ch
)
1256 struct esp_struct
*info
= (struct esp_struct
*)tty
->driver_data
;
1257 unsigned long flags
;
1259 if (serial_paranoia_check(info
, tty
->device
, "rs_put_char"))
1262 if (!tty
|| !info
->xmit_buf
)
1265 save_flags(flags
); cli();
1266 if (info
->xmit_cnt
>= ESP_XMIT_SIZE
- 1) {
1267 restore_flags(flags
);
1271 info
->xmit_buf
[info
->xmit_head
++] = ch
;
1272 info
->xmit_head
&= ESP_XMIT_SIZE
-1;
1274 restore_flags(flags
);
1277 static void rs_flush_chars(struct tty_struct
*tty
)
1279 struct esp_struct
*info
= (struct esp_struct
*)tty
->driver_data
;
1280 unsigned long flags
;
1282 if (serial_paranoia_check(info
, tty
->device
, "rs_flush_chars"))
1285 if (info
->xmit_cnt
<= 0 || tty
->stopped
|| !info
->xmit_buf
)
1288 save_flags(flags
); cli();
1289 if (!(info
->IER
& UART_IER_THRI
)) {
1290 info
->IER
|= UART_IER_THRI
;
1291 serial_out(info
, UART_ESI_CMD1
, ESI_SET_SRV_MASK
);
1292 serial_out(info
, UART_ESI_CMD2
, info
->IER
);
1294 restore_flags(flags
);
1297 static int rs_write(struct tty_struct
* tty
, int from_user
,
1298 const unsigned char *buf
, int count
)
1301 struct esp_struct
*info
= (struct esp_struct
*)tty
->driver_data
;
1302 unsigned long flags
;
1304 if (serial_paranoia_check(info
, tty
->device
, "rs_write"))
1307 if (!tty
|| !info
->xmit_buf
|| !tmp_buf
)
1314 /* Thanks to R. Wolff for suggesting how to do this with */
1315 /* interrupts enabled */
1318 t
= ESP_XMIT_SIZE
- info
->xmit_cnt
- 1;
1323 t
= ESP_XMIT_SIZE
- info
->xmit_head
;
1332 c
-= copy_from_user(tmp_buf
, buf
, c
);
1340 memcpy(info
->xmit_buf
+ info
->xmit_head
, tmp_buf
, c
);
1342 memcpy(info
->xmit_buf
+ info
->xmit_head
, buf
, c
);
1344 info
->xmit_head
= (info
->xmit_head
+ c
) & (ESP_XMIT_SIZE
-1);
1345 info
->xmit_cnt
+= c
;
1354 save_flags(flags
); cli();
1356 if (info
->xmit_cnt
&& !tty
->stopped
&& !(info
->IER
& UART_IER_THRI
)) {
1357 info
->IER
|= UART_IER_THRI
;
1358 serial_out(info
, UART_ESI_CMD1
, ESI_SET_SRV_MASK
);
1359 serial_out(info
, UART_ESI_CMD2
, info
->IER
);
1362 restore_flags(flags
);
1366 static int rs_write_room(struct tty_struct
*tty
)
1368 struct esp_struct
*info
= (struct esp_struct
*)tty
->driver_data
;
1371 if (serial_paranoia_check(info
, tty
->device
, "rs_write_room"))
1373 ret
= ESP_XMIT_SIZE
- info
->xmit_cnt
- 1;
1379 static int rs_chars_in_buffer(struct tty_struct
*tty
)
1381 struct esp_struct
*info
= (struct esp_struct
*)tty
->driver_data
;
1383 if (serial_paranoia_check(info
, tty
->device
, "rs_chars_in_buffer"))
1385 return info
->xmit_cnt
;
1388 static void rs_flush_buffer(struct tty_struct
*tty
)
1390 struct esp_struct
*info
= (struct esp_struct
*)tty
->driver_data
;
1392 if (serial_paranoia_check(info
, tty
->device
, "rs_flush_buffer"))
1395 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
1397 wake_up_interruptible(&tty
->write_wait
);
1398 if ((tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) &&
1399 tty
->ldisc
.write_wakeup
)
1400 (tty
->ldisc
.write_wakeup
)(tty
);
1404 * ------------------------------------------------------------
1407 * This routine is called by the upper-layer tty layer to signal that
1408 * incoming characters should be throttled.
1409 * ------------------------------------------------------------
1411 static void rs_throttle(struct tty_struct
* tty
)
1413 struct esp_struct
*info
= (struct esp_struct
*)tty
->driver_data
;
1414 #ifdef SERIAL_DEBUG_THROTTLE
1417 printk("throttle %s: %d....\n", tty_name(tty
, buf
),
1418 tty
->ldisc
.chars_in_buffer(tty
));
1421 if (serial_paranoia_check(info
, tty
->device
, "rs_throttle"))
1425 info
->IER
&= ~UART_IER_RDI
;
1426 serial_out(info
, UART_ESI_CMD1
, ESI_SET_SRV_MASK
);
1427 serial_out(info
, UART_ESI_CMD2
, info
->IER
);
1428 serial_out(info
, UART_ESI_CMD1
, ESI_SET_RX_TIMEOUT
);
1429 serial_out(info
, UART_ESI_CMD2
, 0x00);
1433 static void rs_unthrottle(struct tty_struct
* tty
)
1435 struct esp_struct
*info
= (struct esp_struct
*)tty
->driver_data
;
1436 #ifdef SERIAL_DEBUG_THROTTLE
1439 printk("unthrottle %s: %d....\n", tty_name(tty
, buf
),
1440 tty
->ldisc
.chars_in_buffer(tty
));
1443 if (serial_paranoia_check(info
, tty
->device
, "rs_unthrottle"))
1447 info
->IER
|= UART_IER_RDI
;
1448 serial_out(info
, UART_ESI_CMD1
, ESI_SET_SRV_MASK
);
1449 serial_out(info
, UART_ESI_CMD2
, info
->IER
);
1450 serial_out(info
, UART_ESI_CMD1
, ESI_SET_RX_TIMEOUT
);
1451 serial_out(info
, UART_ESI_CMD2
, info
->config
.rx_timeout
);
1456 * ------------------------------------------------------------
1457 * rs_ioctl() and friends
1458 * ------------------------------------------------------------
1461 static int get_serial_info(struct esp_struct
* info
,
1462 struct serial_struct
* retinfo
)
1464 struct serial_struct tmp
;
1468 memset(&tmp
, 0, sizeof(tmp
));
1469 tmp
.type
= PORT_16550A
;
1470 tmp
.line
= info
->line
;
1471 tmp
.port
= info
->port
;
1472 tmp
.irq
= info
->irq
;
1473 tmp
.flags
= info
->flags
;
1474 tmp
.xmit_fifo_size
= 1024;
1475 tmp
.baud_base
= BASE_BAUD
;
1476 tmp
.close_delay
= info
->close_delay
;
1477 tmp
.closing_wait
= info
->closing_wait
;
1478 tmp
.custom_divisor
= info
->custom_divisor
;
1480 if (copy_to_user(retinfo
,&tmp
,sizeof(*retinfo
)))
1485 static int get_esp_config(struct esp_struct
* info
,
1486 struct hayes_esp_config
* retinfo
)
1488 struct hayes_esp_config tmp
;
1493 memset(&tmp
, 0, sizeof(tmp
));
1494 tmp
.rx_timeout
= info
->config
.rx_timeout
;
1495 tmp
.rx_trigger
= info
->config
.rx_trigger
;
1496 tmp
.tx_trigger
= info
->config
.tx_trigger
;
1497 tmp
.flow_off
= info
->config
.flow_off
;
1498 tmp
.flow_on
= info
->config
.flow_on
;
1499 tmp
.pio_threshold
= info
->config
.pio_threshold
;
1500 tmp
.dma_channel
= (info
->stat_flags
& ESP_STAT_NEVER_DMA
? 0 : dma
);
1502 if (copy_to_user(retinfo
,&tmp
,sizeof(*retinfo
)))
1508 static int set_serial_info(struct esp_struct
* info
,
1509 struct serial_struct
* new_info
)
1511 struct serial_struct new_serial
;
1512 struct esp_struct old_info
;
1513 unsigned int change_irq
;
1515 struct esp_struct
*current_async
;
1517 if (copy_from_user(&new_serial
,new_info
,sizeof(new_serial
)))
1521 if ((new_serial
.type
!= PORT_16550A
) ||
1522 (new_serial
.hub6
) ||
1523 (info
->port
!= new_serial
.port
) ||
1524 (new_serial
.baud_base
!= BASE_BAUD
) ||
1525 (new_serial
.irq
> 15) ||
1526 (new_serial
.irq
< 2) ||
1527 (new_serial
.irq
== 6) ||
1528 (new_serial
.irq
== 8) ||
1529 (new_serial
.irq
== 13))
1532 change_irq
= new_serial
.irq
!= info
->irq
;
1534 if (change_irq
&& (info
->line
% 8))
1537 if (!capable(CAP_SYS_ADMIN
)) {
1539 (new_serial
.close_delay
!= info
->close_delay
) ||
1540 ((new_serial
.flags
& ~ASYNC_USR_MASK
) !=
1541 (info
->flags
& ~ASYNC_USR_MASK
)))
1543 info
->flags
= ((info
->flags
& ~ASYNC_USR_MASK
) |
1544 (new_serial
.flags
& ASYNC_USR_MASK
));
1545 info
->custom_divisor
= new_serial
.custom_divisor
;
1547 if (new_serial
.irq
== 2)
1551 current_async
= ports
;
1553 while (current_async
) {
1554 if ((current_async
->line
>= info
->line
) &&
1555 (current_async
->line
< (info
->line
+ 8))) {
1556 if (current_async
== info
) {
1557 if (current_async
->count
> 1)
1559 } else if (current_async
->count
)
1563 current_async
= current_async
->next_port
;
1568 * OK, past this point, all the error checking has been done.
1569 * At this point, we start making changes.....
1572 info
->flags
= ((info
->flags
& ~ASYNC_FLAGS
) |
1573 (new_serial
.flags
& ASYNC_FLAGS
));
1574 info
->custom_divisor
= new_serial
.custom_divisor
;
1575 info
->close_delay
= new_serial
.close_delay
* HZ
/100;
1576 info
->closing_wait
= new_serial
.closing_wait
* HZ
/100;
1580 * We need to shutdown the serial port at the old
1581 * port/irq combination.
1585 current_async
= ports
;
1587 while (current_async
) {
1588 if ((current_async
->line
>= info
->line
) &&
1589 (current_async
->line
< (info
->line
+ 8)))
1590 current_async
->irq
= new_serial
.irq
;
1592 current_async
= current_async
->next_port
;
1595 serial_out(info
, UART_ESI_CMD1
, ESI_SET_ENH_IRQ
);
1597 serial_out(info
, UART_ESI_CMD2
, 0x02);
1599 serial_out(info
, UART_ESI_CMD2
, info
->irq
);
1603 if (info
->flags
& ASYNC_INITIALIZED
) {
1604 if (((old_info
.flags
& ASYNC_SPD_MASK
) !=
1605 (info
->flags
& ASYNC_SPD_MASK
)) ||
1606 (old_info
.custom_divisor
!= info
->custom_divisor
)) {
1607 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_HI
)
1608 info
->tty
->alt_speed
= 57600;
1609 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_VHI
)
1610 info
->tty
->alt_speed
= 115200;
1611 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_SHI
)
1612 info
->tty
->alt_speed
= 230400;
1613 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_WARP
)
1614 info
->tty
->alt_speed
= 460800;
1618 retval
= startup(info
);
1623 static int set_esp_config(struct esp_struct
* info
,
1624 struct hayes_esp_config
* new_info
)
1626 struct hayes_esp_config new_config
;
1627 unsigned int change_dma
;
1629 struct esp_struct
*current_async
;
1631 /* Perhaps a non-sysadmin user should be able to do some of these */
1632 /* operations. I haven't decided yet. */
1634 if (!capable(CAP_SYS_ADMIN
))
1637 if (copy_from_user(&new_config
, new_info
, sizeof(new_config
)))
1640 if ((new_config
.flow_on
>= new_config
.flow_off
) ||
1641 (new_config
.rx_trigger
< 1) ||
1642 (new_config
.tx_trigger
< 1) ||
1643 (new_config
.flow_off
< 1) ||
1644 (new_config
.flow_on
< 1) ||
1645 (new_config
.rx_trigger
> 1023) ||
1646 (new_config
.tx_trigger
> 1023) ||
1647 (new_config
.flow_off
> 1023) ||
1648 (new_config
.flow_on
> 1023) ||
1649 (new_config
.pio_threshold
< 0) ||
1650 (new_config
.pio_threshold
> 1024))
1653 if ((new_config
.dma_channel
!= 1) && (new_config
.dma_channel
!= 3))
1654 new_config
.dma_channel
= 0;
1656 if (info
->stat_flags
& ESP_STAT_NEVER_DMA
)
1657 change_dma
= new_config
.dma_channel
;
1659 change_dma
= (new_config
.dma_channel
!= dma
);
1662 if (new_config
.dma_channel
) {
1663 /* PIO mode to DMA mode transition OR */
1664 /* change current DMA channel */
1666 current_async
= ports
;
1668 while (current_async
) {
1669 if (current_async
== info
) {
1670 if (current_async
->count
> 1)
1672 } else if (current_async
->count
)
1676 current_async
->next_port
;
1680 dma
= new_config
.dma_channel
;
1681 info
->stat_flags
&= ~ESP_STAT_NEVER_DMA
;
1683 /* all ports must use the same DMA channel */
1685 current_async
= ports
;
1687 while (current_async
) {
1688 esp_basic_init(current_async
);
1689 current_async
= current_async
->next_port
;
1692 /* DMA mode to PIO mode only */
1694 if (info
->count
> 1)
1698 info
->stat_flags
|= ESP_STAT_NEVER_DMA
;
1699 esp_basic_init(info
);
1703 info
->config
.pio_threshold
= new_config
.pio_threshold
;
1705 if ((new_config
.flow_off
!= info
->config
.flow_off
) ||
1706 (new_config
.flow_on
!= info
->config
.flow_on
)) {
1707 unsigned long flags
;
1709 info
->config
.flow_off
= new_config
.flow_off
;
1710 info
->config
.flow_on
= new_config
.flow_on
;
1711 save_flags(flags
); cli();
1712 serial_out(info
, UART_ESI_CMD1
, ESI_SET_FLOW_LVL
);
1713 serial_out(info
, UART_ESI_CMD2
, new_config
.flow_off
>> 8);
1714 serial_out(info
, UART_ESI_CMD2
, new_config
.flow_off
);
1715 serial_out(info
, UART_ESI_CMD2
, new_config
.flow_on
>> 8);
1716 serial_out(info
, UART_ESI_CMD2
, new_config
.flow_on
);
1717 restore_flags(flags
);
1720 if ((new_config
.rx_trigger
!= info
->config
.rx_trigger
) ||
1721 (new_config
.tx_trigger
!= info
->config
.tx_trigger
)) {
1722 unsigned long flags
;
1724 info
->config
.rx_trigger
= new_config
.rx_trigger
;
1725 info
->config
.tx_trigger
= new_config
.tx_trigger
;
1726 save_flags(flags
); cli();
1727 serial_out(info
, UART_ESI_CMD1
, ESI_SET_TRIGGER
);
1728 serial_out(info
, UART_ESI_CMD2
,
1729 new_config
.rx_trigger
>> 8);
1730 serial_out(info
, UART_ESI_CMD2
, new_config
.rx_trigger
);
1731 serial_out(info
, UART_ESI_CMD2
,
1732 new_config
.tx_trigger
>> 8);
1733 serial_out(info
, UART_ESI_CMD2
, new_config
.tx_trigger
);
1734 restore_flags(flags
);
1737 if (new_config
.rx_timeout
!= info
->config
.rx_timeout
) {
1738 unsigned long flags
;
1740 info
->config
.rx_timeout
= new_config
.rx_timeout
;
1741 save_flags(flags
); cli();
1743 if (info
->IER
& UART_IER_RDI
) {
1744 serial_out(info
, UART_ESI_CMD1
,
1745 ESI_SET_RX_TIMEOUT
);
1746 serial_out(info
, UART_ESI_CMD2
,
1747 new_config
.rx_timeout
);
1750 restore_flags(flags
);
1753 if (!(info
->flags
& ASYNC_INITIALIZED
))
1754 retval
= startup(info
);
1760 * get_lsr_info - get line status register info
1762 * Purpose: Let user call ioctl() to get info when the UART physically
1763 * is emptied. On bus types like RS485, the transmitter must
1764 * release the bus after transmitting. This must be done when
1765 * the transmit shift register is empty, not be done when the
1766 * transmit holding register is empty. This functionality
1767 * allows an RS485 driver to be written in user space.
1769 static int get_lsr_info(struct esp_struct
* info
, unsigned int *value
)
1771 unsigned char status
;
1772 unsigned int result
;
1775 serial_out(info
, UART_ESI_CMD1
, ESI_GET_UART_STAT
);
1776 status
= serial_in(info
, UART_ESI_STAT1
);
1778 result
= ((status
& UART_LSR_TEMT
) ? TIOCSER_TEMT
: 0);
1779 return put_user(result
,value
);
1783 static int get_modem_info(struct esp_struct
* info
, unsigned int *value
)
1785 unsigned char control
, status
;
1786 unsigned int result
;
1788 control
= info
->MCR
;
1790 serial_out(info
, UART_ESI_CMD1
, ESI_GET_UART_STAT
);
1791 status
= serial_in(info
, UART_ESI_STAT2
);
1793 result
= ((control
& UART_MCR_RTS
) ? TIOCM_RTS
: 0)
1794 | ((control
& UART_MCR_DTR
) ? TIOCM_DTR
: 0)
1795 | ((status
& UART_MSR_DCD
) ? TIOCM_CAR
: 0)
1796 | ((status
& UART_MSR_RI
) ? TIOCM_RNG
: 0)
1797 | ((status
& UART_MSR_DSR
) ? TIOCM_DSR
: 0)
1798 | ((status
& UART_MSR_CTS
) ? TIOCM_CTS
: 0);
1799 return put_user(result
,value
);
1802 static int set_modem_info(struct esp_struct
* info
, unsigned int cmd
,
1803 unsigned int *value
)
1808 error
= get_user(arg
, value
);
1814 if (arg
& TIOCM_RTS
)
1815 info
->MCR
|= UART_MCR_RTS
;
1816 if (arg
& TIOCM_DTR
)
1817 info
->MCR
|= UART_MCR_DTR
;
1820 if (arg
& TIOCM_RTS
)
1821 info
->MCR
&= ~UART_MCR_RTS
;
1822 if (arg
& TIOCM_DTR
)
1823 info
->MCR
&= ~UART_MCR_DTR
;
1826 info
->MCR
= ((info
->MCR
& ~(UART_MCR_RTS
| UART_MCR_DTR
))
1827 | ((arg
& TIOCM_RTS
) ? UART_MCR_RTS
: 0)
1828 | ((arg
& TIOCM_DTR
) ? UART_MCR_DTR
: 0));
1834 serial_out(info
, UART_ESI_CMD1
, ESI_WRITE_UART
);
1835 serial_out(info
, UART_ESI_CMD2
, UART_MCR
);
1836 serial_out(info
, UART_ESI_CMD2
, info
->MCR
);
1842 * rs_break() --- routine which turns the break handling on or off
1844 static void esp_break(struct tty_struct
*tty
, int break_state
)
1846 struct esp_struct
* info
= (struct esp_struct
*)tty
->driver_data
;
1847 unsigned long flags
;
1849 if (serial_paranoia_check(info
, tty
->device
, "esp_break"))
1852 save_flags(flags
); cli();
1853 if (break_state
== -1) {
1854 serial_out(info
, UART_ESI_CMD1
, ESI_ISSUE_BREAK
);
1855 serial_out(info
, UART_ESI_CMD2
, 0x01);
1857 interruptible_sleep_on(&info
->break_wait
);
1859 serial_out(info
, UART_ESI_CMD1
, ESI_ISSUE_BREAK
);
1860 serial_out(info
, UART_ESI_CMD2
, 0x00);
1862 restore_flags(flags
);
1865 static int rs_ioctl(struct tty_struct
*tty
, struct file
* file
,
1866 unsigned int cmd
, unsigned long arg
)
1869 struct esp_struct
* info
= (struct esp_struct
*)tty
->driver_data
;
1870 struct async_icount cprev
, cnow
; /* kernel counter temps */
1871 struct serial_icounter_struct
*p_cuser
; /* user space */
1873 if (serial_paranoia_check(info
, tty
->device
, "rs_ioctl"))
1876 if ((cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCSSERIAL
) &&
1877 (cmd
!= TIOCSERCONFIG
) && (cmd
!= TIOCSERGWILD
) &&
1878 (cmd
!= TIOCSERSWILD
) && (cmd
!= TIOCSERGSTRUCT
) &&
1879 (cmd
!= TIOCMIWAIT
) && (cmd
!= TIOCGICOUNT
) &&
1880 (cmd
!= TIOCGHAYESESP
) && (cmd
!= TIOCSHAYESESP
)) {
1881 if (tty
->flags
& (1 << TTY_IO_ERROR
))
1887 return get_modem_info(info
, (unsigned int *) arg
);
1891 return set_modem_info(info
, cmd
, (unsigned int *) arg
);
1893 return get_serial_info(info
,
1894 (struct serial_struct
*) arg
);
1896 return set_serial_info(info
,
1897 (struct serial_struct
*) arg
);
1899 /* do not reconfigure after initial configuration */
1903 return put_user(0L, (unsigned long *) arg
);
1905 case TIOCSERGETLSR
: /* Get line status register */
1906 return get_lsr_info(info
, (unsigned int *) arg
);
1914 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1915 * - mask passed in arg for lines of interest
1916 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1917 * Caller should use TIOCGICOUNT to see which one it was
1921 cprev
= info
->icount
; /* note the counters on entry */
1924 interruptible_sleep_on(&info
->delta_msr_wait
);
1925 /* see if a signal did it */
1926 if (signal_pending(current
))
1927 return -ERESTARTSYS
;
1929 cnow
= info
->icount
; /* atomic copy */
1931 if (cnow
.rng
== cprev
.rng
&& cnow
.dsr
== cprev
.dsr
&&
1932 cnow
.dcd
== cprev
.dcd
&& cnow
.cts
== cprev
.cts
)
1933 return -EIO
; /* no change => error */
1934 if ( ((arg
& TIOCM_RNG
) && (cnow
.rng
!= cprev
.rng
)) ||
1935 ((arg
& TIOCM_DSR
) && (cnow
.dsr
!= cprev
.dsr
)) ||
1936 ((arg
& TIOCM_CD
) && (cnow
.dcd
!= cprev
.dcd
)) ||
1937 ((arg
& TIOCM_CTS
) && (cnow
.cts
!= cprev
.cts
)) ) {
1945 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1946 * Return: write counters to the user passed counter struct
1947 * NB: both 1->0 and 0->1 transitions are counted except for
1948 * RI where only 0->1 is counted.
1952 cnow
= info
->icount
;
1954 p_cuser
= (struct serial_icounter_struct
*) arg
;
1955 if ((error
= put_user(cnow
.cts
, &p_cuser
->cts
)))
1957 if ((error
= put_user(cnow
.dsr
, &p_cuser
->dsr
)))
1959 if ((error
= put_user(cnow
.rng
, &p_cuser
->rng
)))
1961 if ((error
= put_user(cnow
.dcd
, &p_cuser
->dcd
)))
1966 return (get_esp_config(info
,
1967 (struct hayes_esp_config
*)arg
));
1969 return (set_esp_config(info
,
1970 (struct hayes_esp_config
*)arg
));
1973 return -ENOIOCTLCMD
;
1978 static void rs_set_termios(struct tty_struct
*tty
, struct termios
*old_termios
)
1980 struct esp_struct
*info
= (struct esp_struct
*)tty
->driver_data
;
1982 if ( (tty
->termios
->c_cflag
== old_termios
->c_cflag
)
1983 && ( RELEVANT_IFLAG(tty
->termios
->c_iflag
)
1984 == RELEVANT_IFLAG(old_termios
->c_iflag
)))
1989 /* Handle transition to B0 status */
1990 if ((old_termios
->c_cflag
& CBAUD
) &&
1991 !(tty
->termios
->c_cflag
& CBAUD
)) {
1992 info
->MCR
&= ~(UART_MCR_DTR
|UART_MCR_RTS
);
1994 serial_out(info
, UART_ESI_CMD1
, ESI_WRITE_UART
);
1995 serial_out(info
, UART_ESI_CMD2
, UART_MCR
);
1996 serial_out(info
, UART_ESI_CMD2
, info
->MCR
);
2000 /* Handle transition away from B0 status */
2001 if (!(old_termios
->c_cflag
& CBAUD
) &&
2002 (tty
->termios
->c_cflag
& CBAUD
)) {
2003 info
->MCR
|= (UART_MCR_DTR
| UART_MCR_RTS
);
2005 serial_out(info
, UART_ESI_CMD1
, ESI_WRITE_UART
);
2006 serial_out(info
, UART_ESI_CMD2
, UART_MCR
);
2007 serial_out(info
, UART_ESI_CMD2
, info
->MCR
);
2011 /* Handle turning of CRTSCTS */
2012 if ((old_termios
->c_cflag
& CRTSCTS
) &&
2013 !(tty
->termios
->c_cflag
& CRTSCTS
)) {
2019 * No need to wake up processes in open wait, since they
2020 * sample the CLOCAL flag once, and don't recheck it.
2021 * XXX It's not clear whether the current behavior is correct
2022 * or not. Hence, this may change.....
2024 if (!(old_termios
->c_cflag
& CLOCAL
) &&
2025 (tty
->termios
->c_cflag
& CLOCAL
))
2026 wake_up_interruptible(&info
->open_wait
);
2031 * ------------------------------------------------------------
2034 * This routine is called when the serial port gets closed. First, we
2035 * wait for the last remaining data to be sent. Then, we unlink its
2036 * async structure from the interrupt chain if necessary, and we free
2037 * that IRQ if nothing is left in the chain.
2038 * ------------------------------------------------------------
2040 static void rs_close(struct tty_struct
*tty
, struct file
* filp
)
2042 struct esp_struct
* info
= (struct esp_struct
*)tty
->driver_data
;
2043 unsigned long flags
;
2045 if (!info
|| serial_paranoia_check(info
, tty
->device
, "rs_close"))
2048 save_flags(flags
); cli();
2050 if (tty_hung_up_p(filp
)) {
2051 DBG_CNT("before DEC-hung");
2053 restore_flags(flags
);
2057 #ifdef SERIAL_DEBUG_OPEN
2058 printk("rs_close ttys%d, count = %d\n", info
->line
, info
->count
);
2060 if ((tty
->count
== 1) && (info
->count
!= 1)) {
2062 * Uh, oh. tty->count is 1, which means that the tty
2063 * structure will be freed. Info->count should always
2064 * be one in these conditions. If it's greater than
2065 * one, we've got real problems, since it means the
2066 * serial port won't be shutdown.
2068 printk("rs_close: bad serial port count; tty->count is 1, "
2069 "info->count is %d\n", info
->count
);
2072 if (--info
->count
< 0) {
2073 printk("rs_close: bad serial port count for ttys%d: %d\n",
2074 info
->line
, info
->count
);
2078 DBG_CNT("before DEC-2");
2080 restore_flags(flags
);
2083 info
->flags
|= ASYNC_CLOSING
;
2085 * Save the termios structure, since this port may have
2086 * separate termios for callout and dialin.
2088 if (info
->flags
& ASYNC_NORMAL_ACTIVE
)
2089 info
->normal_termios
= *tty
->termios
;
2090 if (info
->flags
& ASYNC_CALLOUT_ACTIVE
)
2091 info
->callout_termios
= *tty
->termios
;
2093 * Now we wait for the transmit buffer to clear; and we notify
2094 * the line discipline to only process XON/XOFF characters.
2097 if (info
->closing_wait
!= ASYNC_CLOSING_WAIT_NONE
)
2098 tty_wait_until_sent(tty
, info
->closing_wait
);
2100 * At this point we stop accepting input. To do this, we
2101 * disable the receive line status interrupts, and tell the
2102 * interrupt driver to stop checking the data ready bit in the
2103 * line status register.
2105 /* info->IER &= ~UART_IER_RLSI; */
2106 info
->IER
&= ~UART_IER_RDI
;
2107 info
->read_status_mask
&= ~UART_LSR_DR
;
2108 if (info
->flags
& ASYNC_INITIALIZED
) {
2109 serial_out(info
, UART_ESI_CMD1
, ESI_SET_SRV_MASK
);
2110 serial_out(info
, UART_ESI_CMD2
, info
->IER
);
2112 /* disable receive timeout */
2113 serial_out(info
, UART_ESI_CMD1
, ESI_SET_RX_TIMEOUT
);
2114 serial_out(info
, UART_ESI_CMD2
, 0x00);
2117 * Before we drop DTR, make sure the UART transmitter
2118 * has completely drained; this is especially
2119 * important if there is a transmit FIFO!
2121 rs_wait_until_sent(tty
, info
->timeout
);
2124 if (tty
->driver
.flush_buffer
)
2125 tty
->driver
.flush_buffer(tty
);
2126 if (tty
->ldisc
.flush_buffer
)
2127 tty
->ldisc
.flush_buffer(tty
);
2132 if (info
->blocked_open
) {
2133 if (info
->close_delay
) {
2134 current
->state
= TASK_INTERRUPTIBLE
;
2135 schedule_timeout(info
->close_delay
);
2137 wake_up_interruptible(&info
->open_wait
);
2139 info
->flags
&= ~(ASYNC_NORMAL_ACTIVE
|ASYNC_CALLOUT_ACTIVE
|
2141 wake_up_interruptible(&info
->close_wait
);
2143 restore_flags(flags
);
2146 static void rs_wait_until_sent(struct tty_struct
*tty
, int timeout
)
2148 struct esp_struct
*info
= (struct esp_struct
*)tty
->driver_data
;
2149 unsigned long orig_jiffies
, char_time
;
2150 unsigned long flags
;
2152 if (serial_paranoia_check(info
, tty
->device
, "rs_wait_until_sent"))
2155 orig_jiffies
= jiffies
;
2156 char_time
= ((info
->timeout
- HZ
/ 50) / 1024) / 5;
2161 save_flags(flags
); cli();
2162 serial_out(info
, UART_ESI_CMD1
, ESI_NO_COMMAND
);
2163 serial_out(info
, UART_ESI_CMD1
, ESI_GET_TX_AVAIL
);
2165 while ((serial_in(info
, UART_ESI_STAT1
) != 0x03) ||
2166 (serial_in(info
, UART_ESI_STAT2
) != 0xff)) {
2167 current
->state
= TASK_INTERRUPTIBLE
;
2168 schedule_timeout(char_time
);
2170 if (signal_pending(current
))
2173 if (timeout
&& ((orig_jiffies
+ timeout
) < jiffies
))
2176 serial_out(info
, UART_ESI_CMD1
, ESI_NO_COMMAND
);
2177 serial_out(info
, UART_ESI_CMD1
, ESI_GET_TX_AVAIL
);
2180 restore_flags(flags
);
2181 current
->state
= TASK_RUNNING
;
2185 * esp_hangup() --- called by tty_hangup() when a hangup is signaled.
2187 static void esp_hangup(struct tty_struct
*tty
)
2189 struct esp_struct
* info
= (struct esp_struct
*)tty
->driver_data
;
2191 if (serial_paranoia_check(info
, tty
->device
, "esp_hangup"))
2194 rs_flush_buffer(tty
);
2198 info
->flags
&= ~(ASYNC_NORMAL_ACTIVE
|ASYNC_CALLOUT_ACTIVE
);
2200 wake_up_interruptible(&info
->open_wait
);
2204 * ------------------------------------------------------------
2205 * esp_open() and friends
2206 * ------------------------------------------------------------
2208 static int block_til_ready(struct tty_struct
*tty
, struct file
* filp
,
2209 struct esp_struct
*info
)
2211 DECLARE_WAITQUEUE(wait
, current
);
2216 * If the device is in the middle of being closed, then block
2217 * until it's done, and then try again.
2219 if (tty_hung_up_p(filp
) ||
2220 (info
->flags
& ASYNC_CLOSING
)) {
2221 if (info
->flags
& ASYNC_CLOSING
)
2222 interruptible_sleep_on(&info
->close_wait
);
2223 #ifdef SERIAL_DO_RESTART
2224 if (info
->flags
& ASYNC_HUP_NOTIFY
)
2227 return -ERESTARTSYS
;
2234 * If this is a callout device, then just make sure the normal
2235 * device isn't being used.
2237 if (tty
->driver
.subtype
== SERIAL_TYPE_CALLOUT
) {
2238 if (info
->flags
& ASYNC_NORMAL_ACTIVE
)
2240 if ((info
->flags
& ASYNC_CALLOUT_ACTIVE
) &&
2241 (info
->flags
& ASYNC_SESSION_LOCKOUT
) &&
2242 (info
->session
!= current
->session
))
2244 if ((info
->flags
& ASYNC_CALLOUT_ACTIVE
) &&
2245 (info
->flags
& ASYNC_PGRP_LOCKOUT
) &&
2246 (info
->pgrp
!= current
->pgrp
))
2248 info
->flags
|= ASYNC_CALLOUT_ACTIVE
;
2253 * If non-blocking mode is set, or the port is not enabled,
2254 * then make the check up front and then exit.
2256 if ((filp
->f_flags
& O_NONBLOCK
) ||
2257 (tty
->flags
& (1 << TTY_IO_ERROR
))) {
2258 if (info
->flags
& ASYNC_CALLOUT_ACTIVE
)
2260 info
->flags
|= ASYNC_NORMAL_ACTIVE
;
2264 if (info
->flags
& ASYNC_CALLOUT_ACTIVE
) {
2265 if (info
->normal_termios
.c_cflag
& CLOCAL
)
2268 if (tty
->termios
->c_cflag
& CLOCAL
)
2273 * Block waiting for the carrier detect and the line to become
2274 * free (i.e., not in use by the callout). While we are in
2275 * this loop, info->count is dropped by one, so that
2276 * rs_close() knows when to free things. We restore it upon
2277 * exit, either normal or abnormal.
2280 add_wait_queue(&info
->open_wait
, &wait
);
2281 #ifdef SERIAL_DEBUG_OPEN
2282 printk("block_til_ready before block: ttys%d, count = %d\n",
2283 info
->line
, info
->count
);
2286 if (!tty_hung_up_p(filp
))
2289 info
->blocked_open
++;
2292 if (!(info
->flags
& ASYNC_CALLOUT_ACTIVE
) &&
2293 (tty
->termios
->c_cflag
& CBAUD
)) {
2294 unsigned int scratch
;
2296 serial_out(info
, UART_ESI_CMD1
, ESI_READ_UART
);
2297 serial_out(info
, UART_ESI_CMD2
, UART_MCR
);
2298 scratch
= serial_in(info
, UART_ESI_STAT1
);
2299 serial_out(info
, UART_ESI_CMD1
, ESI_WRITE_UART
);
2300 serial_out(info
, UART_ESI_CMD2
, UART_MCR
);
2301 serial_out(info
, UART_ESI_CMD2
,
2302 scratch
| UART_MCR_DTR
| UART_MCR_RTS
);
2305 set_current_state(TASK_INTERRUPTIBLE
);
2306 if (tty_hung_up_p(filp
) ||
2307 !(info
->flags
& ASYNC_INITIALIZED
)) {
2308 #ifdef SERIAL_DO_RESTART
2309 if (info
->flags
& ASYNC_HUP_NOTIFY
)
2312 retval
= -ERESTARTSYS
;
2319 serial_out(info
, UART_ESI_CMD1
, ESI_GET_UART_STAT
);
2320 if (serial_in(info
, UART_ESI_STAT2
) & UART_MSR_DCD
)
2323 if (!(info
->flags
& ASYNC_CALLOUT_ACTIVE
) &&
2324 !(info
->flags
& ASYNC_CLOSING
) &&
2327 if (signal_pending(current
)) {
2328 retval
= -ERESTARTSYS
;
2331 #ifdef SERIAL_DEBUG_OPEN
2332 printk("block_til_ready blocking: ttys%d, count = %d\n",
2333 info
->line
, info
->count
);
2337 current
->state
= TASK_RUNNING
;
2338 remove_wait_queue(&info
->open_wait
, &wait
);
2339 if (!tty_hung_up_p(filp
))
2341 info
->blocked_open
--;
2342 #ifdef SERIAL_DEBUG_OPEN
2343 printk("block_til_ready after blocking: ttys%d, count = %d\n",
2344 info
->line
, info
->count
);
2348 info
->flags
|= ASYNC_NORMAL_ACTIVE
;
2353 * This routine is called whenever a serial port is opened. It
2354 * enables interrupts for a serial port, linking in its async structure into
2355 * the IRQ chain. It also performs the serial-specific
2356 * initialization for the tty structure.
2358 static int esp_open(struct tty_struct
*tty
, struct file
* filp
)
2360 struct esp_struct
*info
;
2364 line
= MINOR(tty
->device
) - tty
->driver
.minor_start
;
2365 if ((line
< 0) || (line
>= NR_PORTS
))
2368 /* find the port in the chain */
2372 while (info
&& (info
->line
!= line
))
2373 info
= info
->next_port
;
2376 serial_paranoia_check(info
, tty
->device
, "esp_open");
2380 #ifdef SERIAL_DEBUG_OPEN
2381 printk("esp_open %s%d, count = %d\n", tty
->driver
.name
, info
->line
,
2386 tty
->driver_data
= info
;
2390 page
= get_free_page(GFP_KERNEL
);
2396 tmp_buf
= (unsigned char *) page
;
2400 * Start up serial port
2402 retval
= startup(info
);
2406 retval
= block_til_ready(tty
, filp
, info
);
2408 #ifdef SERIAL_DEBUG_OPEN
2409 printk("esp_open returning after block_til_ready with %d\n",
2415 if ((info
->count
== 1) && (info
->flags
& ASYNC_SPLIT_TERMIOS
)) {
2416 if (tty
->driver
.subtype
== SERIAL_TYPE_NORMAL
)
2417 *tty
->termios
= info
->normal_termios
;
2419 *tty
->termios
= info
->callout_termios
;
2423 info
->session
= current
->session
;
2424 info
->pgrp
= current
->pgrp
;
2426 #ifdef SERIAL_DEBUG_OPEN
2427 printk("esp_open ttys%d successful...", info
->line
);
2433 * ---------------------------------------------------------------------
2434 * espserial_init() and friends
2436 * espserial_init() is called at boot-time to initialize the serial driver.
2437 * ---------------------------------------------------------------------
2441 * This routine prints out the appropriate serial driver version
2442 * number, and identifies which options were configured into this
2446 static _INLINE_
void show_serial_version(void)
2448 printk(KERN_INFO
"%s version %s (DMA %u)\n",
2449 serial_name
, serial_version
, dma
);
2453 * This routine is called by espserial_init() to initialize a specific serial
2456 static _INLINE_
int autoconfig(struct esp_struct
* info
, int *region_start
)
2458 int port_detected
= 0;
2459 unsigned long flags
;
2461 save_flags(flags
); cli();
2464 * Check for ESP card
2467 if (!check_region(info
->port
, 8) &&
2468 serial_in(info
, UART_ESI_BASE
) == 0xf3) {
2469 serial_out(info
, UART_ESI_CMD1
, 0x00);
2470 serial_out(info
, UART_ESI_CMD1
, 0x01);
2472 if ((serial_in(info
, UART_ESI_STAT2
) & 0x70) == 0x20) {
2476 serial_out(info
, UART_ESI_CMD1
, 0x02);
2478 if (serial_in(info
, UART_ESI_STAT1
) & 0x01)
2484 if (ports
&& (ports
->port
== (info
->port
- 8))) {
2485 release_region(*region_start
,
2486 info
->port
- *region_start
);
2488 *region_start
= info
->port
;
2490 request_region(*region_start
,
2491 info
->port
- *region_start
+ 8,
2494 /* put card in enhanced mode */
2495 /* this prevents access through */
2496 /* the "old" IO ports */
2497 esp_basic_init(info
);
2500 serial_out(info
, UART_ESI_CMD1
, ESI_WRITE_UART
);
2501 serial_out(info
, UART_ESI_CMD2
, UART_MCR
);
2502 serial_out(info
, UART_ESI_CMD2
, 0x00);
2506 restore_flags(flags
);
2507 return (port_detected
);
2511 * The serial driver boot-time initialization code!
2513 int __init
espserial_init(void)
2517 struct esp_struct
* info
;
2518 struct esp_struct
*last_primary
= 0;
2519 int esp
[] = {0x100,0x140,0x180,0x200,0x240,0x280,0x300,0x380};
2521 init_bh(ESP_BH
, do_serial_bh
);
2523 for (i
= 0; i
< NR_PRIMARY
; i
++) {
2525 if ((irq
[i
] < 2) || (irq
[i
] > 15) || (irq
[i
] == 6) ||
2526 (irq
[i
] == 8) || (irq
[i
] == 13))
2528 else if (irq
[i
] == 2)
2533 if ((dma
!= 1) && (dma
!= 3))
2536 if ((rx_trigger
< 1) || (rx_trigger
> 1023))
2539 if ((tx_trigger
< 1) || (tx_trigger
> 1023))
2542 if ((flow_off
< 1) || (flow_off
> 1023))
2545 if ((flow_on
< 1) || (flow_on
> 1023))
2548 if ((rx_timeout
< 0) || (rx_timeout
> 255))
2551 if (flow_on
>= flow_off
)
2552 flow_on
= flow_off
- 1;
2554 show_serial_version();
2556 /* Initialize the tty_driver structure */
2558 memset(&esp_driver
, 0, sizeof(struct tty_driver
));
2559 esp_driver
.magic
= TTY_DRIVER_MAGIC
;
2560 esp_driver
.name
= "ttyP";
2561 esp_driver
.major
= ESP_IN_MAJOR
;
2562 esp_driver
.minor_start
= 0;
2563 esp_driver
.num
= NR_PORTS
;
2564 esp_driver
.type
= TTY_DRIVER_TYPE_SERIAL
;
2565 esp_driver
.subtype
= SERIAL_TYPE_NORMAL
;
2566 esp_driver
.init_termios
= tty_std_termios
;
2567 esp_driver
.init_termios
.c_cflag
=
2568 B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
2569 esp_driver
.flags
= TTY_DRIVER_REAL_RAW
;
2570 esp_driver
.refcount
= &serial_refcount
;
2571 esp_driver
.table
= serial_table
;
2572 esp_driver
.termios
= serial_termios
;
2573 esp_driver
.termios_locked
= serial_termios_locked
;
2575 esp_driver
.open
= esp_open
;
2576 esp_driver
.close
= rs_close
;
2577 esp_driver
.write
= rs_write
;
2578 esp_driver
.put_char
= rs_put_char
;
2579 esp_driver
.flush_chars
= rs_flush_chars
;
2580 esp_driver
.write_room
= rs_write_room
;
2581 esp_driver
.chars_in_buffer
= rs_chars_in_buffer
;
2582 esp_driver
.flush_buffer
= rs_flush_buffer
;
2583 esp_driver
.ioctl
= rs_ioctl
;
2584 esp_driver
.throttle
= rs_throttle
;
2585 esp_driver
.unthrottle
= rs_unthrottle
;
2586 esp_driver
.set_termios
= rs_set_termios
;
2587 esp_driver
.stop
= rs_stop
;
2588 esp_driver
.start
= rs_start
;
2589 esp_driver
.hangup
= esp_hangup
;
2590 esp_driver
.break_ctl
= esp_break
;
2591 esp_driver
.wait_until_sent
= rs_wait_until_sent
;
2594 * The callout device is just like normal device except for
2595 * major number and the subtype code.
2597 esp_callout_driver
= esp_driver
;
2598 esp_callout_driver
.name
= "cup";
2599 esp_callout_driver
.major
= ESP_OUT_MAJOR
;
2600 esp_callout_driver
.subtype
= SERIAL_TYPE_CALLOUT
;
2602 if (tty_register_driver(&esp_driver
))
2604 printk(KERN_ERR
"Couldn't register esp serial driver");
2608 if (tty_register_driver(&esp_callout_driver
))
2610 printk(KERN_ERR
"Couldn't register esp callout driver");
2611 tty_unregister_driver(&esp_driver
);
2615 info
= (struct esp_struct
*)kmalloc(sizeof(struct esp_struct
),
2620 printk(KERN_ERR
"Couldn't allocate memory for esp serial device information\n");
2621 tty_unregister_driver(&esp_driver
);
2622 tty_unregister_driver(&esp_callout_driver
);
2626 memset((void *)info
, 0, sizeof(struct esp_struct
));
2627 /* rx_trigger, tx_trigger are needed by autoconfig */
2628 info
->config
.rx_trigger
= rx_trigger
;
2629 info
->config
.tx_trigger
= tx_trigger
;
2635 info
->port
= esp
[i
] + offset
;
2637 info
->line
= (i
* 8) + (offset
/ 8);
2639 if (!autoconfig(info
, ®ion_start
)) {
2645 info
->custom_divisor
= (divisor
[i
] >> (offset
/ 2)) & 0xf;
2646 info
->flags
= STD_COM_FLAGS
;
2647 if (info
->custom_divisor
)
2648 info
->flags
|= ASYNC_SPD_CUST
;
2649 info
->magic
= ESP_MAGIC
;
2650 info
->close_delay
= 5*HZ
/10;
2651 info
->closing_wait
= 30*HZ
;
2652 info
->tqueue
.routine
= do_softint
;
2653 info
->tqueue
.data
= info
;
2654 info
->tqueue_hangup
.routine
= do_serial_hangup
;
2655 info
->tqueue_hangup
.data
= info
;
2656 info
->callout_termios
= esp_callout_driver
.init_termios
;
2657 info
->normal_termios
= esp_driver
.init_termios
;
2658 info
->config
.rx_timeout
= rx_timeout
;
2659 info
->config
.flow_on
= flow_on
;
2660 info
->config
.flow_off
= flow_off
;
2661 info
->config
.pio_threshold
= pio_threshold
;
2662 info
->next_port
= ports
;
2663 init_waitqueue_head(&info
->open_wait
);
2664 init_waitqueue_head(&info
->close_wait
);
2665 init_waitqueue_head(&info
->delta_msr_wait
);
2666 init_waitqueue_head(&info
->break_wait
);
2668 printk(KERN_INFO
"ttyP%d at 0x%04x (irq = %d) is an ESP ",
2669 info
->line
, info
->port
, info
->irq
);
2671 if (info
->line
% 8) {
2672 printk("secondary port\n");
2673 /* 8 port cards can't do DMA */
2674 info
->stat_flags
|= ESP_STAT_NEVER_DMA
;
2677 last_primary
->stat_flags
|= ESP_STAT_NEVER_DMA
;
2679 printk("primary port\n");
2680 last_primary
= info
;
2685 info
->stat_flags
|= ESP_STAT_NEVER_DMA
;
2687 info
= (struct esp_struct
*)kmalloc(sizeof(struct esp_struct
),
2691 printk(KERN_ERR
"Couldn't allocate memory for esp serial device information\n");
2693 /* allow use of the already detected ports */
2697 memset((void *)info
, 0, sizeof(struct esp_struct
));
2698 /* rx_trigger, tx_trigger are needed by autoconfig */
2699 info
->config
.rx_trigger
= rx_trigger
;
2700 info
->config
.tx_trigger
= tx_trigger
;
2708 } while (i
< NR_PRIMARY
);
2710 /* free the last port memory allocation */
2718 int init_module(void)
2720 return espserial_init();
2723 void cleanup_module(void)
2725 unsigned long flags
;
2727 unsigned int region_start
, region_end
;
2728 struct esp_struct
*temp_async
;
2729 struct esp_pio_buffer
*pio_buf
;
2731 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2735 if ((e1
= tty_unregister_driver(&esp_driver
)))
2736 printk("SERIAL: failed to unregister serial driver (%d)\n",
2738 if ((e2
= tty_unregister_driver(&esp_callout_driver
)))
2739 printk("SERIAL: failed to unregister callout driver (%d)\n",
2741 restore_flags(flags
);
2745 region_start
= region_end
= ports
->port
;
2748 while (temp_async
) {
2749 if ((region_start
- temp_async
->port
) == 8) {
2750 region_start
= temp_async
->port
;
2751 temp_async
->port
= 0;
2753 } else if ((temp_async
->port
- region_end
)
2755 region_end
= temp_async
->port
;
2756 temp_async
->port
= 0;
2759 temp_async
= temp_async
->next_port
;
2762 release_region(region_start
,
2763 region_end
- region_start
+ 8);
2766 temp_async
= ports
->next_port
;
2772 free_pages((unsigned int)dma_buffer
,
2773 get_order(DMA_BUFFER_SZ
));
2776 free_page((unsigned long)tmp_buf
);
2778 while (free_pio_buf
) {
2779 pio_buf
= free_pio_buf
->next
;
2780 kfree(free_pio_buf
);
2781 free_pio_buf
= pio_buf
;