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�pl 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 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> -
36 * several cleanups, use module_init/module_exit, etc
38 * This module exports the following rs232 io functions:
40 * int espserial_init(void);
43 #include <linux/module.h>
44 #include <linux/errno.h>
45 #include <linux/signal.h>
46 #include <linux/sched.h>
47 #include <linux/interrupt.h>
48 #include <linux/tty.h>
49 #include <linux/tty_flip.h>
50 #include <linux/serial.h>
51 #include <linux/serialP.h>
52 #include <linux/serial_reg.h>
53 #include <linux/major.h>
54 #include <linux/string.h>
55 #include <linux/fcntl.h>
56 #include <linux/ptrace.h>
57 #include <linux/ioport.h>
59 #include <linux/init.h>
60 #include <linux/delay.h>
62 #include <asm/system.h>
64 #include <asm/bitops.h>
67 #include <linux/slab.h>
68 #include <asm/uaccess.h>
70 #include <linux/hayesesp.h>
72 #define NR_PORTS 64 /* maximum number of ports */
73 #define NR_PRIMARY 8 /* maximum number of primary ports */
74 #define REGION_SIZE 8 /* size of io region to request */
76 /* The following variables can be set by giving module options */
77 static int irq
[NR_PRIMARY
]; /* IRQ for each base port */
78 static unsigned int divisor
[NR_PRIMARY
]; /* custom divisor for each port */
79 static unsigned int dma
= ESP_DMA_CHANNEL
; /* DMA channel */
80 static unsigned int rx_trigger
= ESP_RX_TRIGGER
;
81 static unsigned int tx_trigger
= ESP_TX_TRIGGER
;
82 static unsigned int flow_off
= ESP_FLOW_OFF
;
83 static unsigned int flow_on
= ESP_FLOW_ON
;
84 static unsigned int rx_timeout
= ESP_RX_TMOUT
;
85 static unsigned int pio_threshold
= ESP_PIO_THRESHOLD
;
87 MODULE_LICENSE("GPL");
89 module_param_array(irq
, int, NULL
, 0);
90 module_param_array(divisor
, uint
, NULL
, 0);
91 module_param(dma
, uint
, 0);
92 module_param(rx_trigger
, uint
, 0);
93 module_param(tx_trigger
, uint
, 0);
94 module_param(flow_off
, uint
, 0);
95 module_param(flow_on
, uint
, 0);
96 module_param(rx_timeout
, uint
, 0);
97 module_param(pio_threshold
, uint
, 0);
101 static char *dma_buffer
;
102 static int dma_bytes
;
103 static struct esp_pio_buffer
*free_pio_buf
;
105 #define DMA_BUFFER_SZ 1024
107 #define WAKEUP_CHARS 1024
109 static char serial_name
[] __initdata
= "ESP serial driver";
110 static char serial_version
[] __initdata
= "2.2";
112 static struct tty_driver
*esp_driver
;
114 /* serial subtype definitions */
115 #define SERIAL_TYPE_NORMAL 1
118 * Serial driver configuration section. Here are the various options:
120 * SERIAL_PARANOIA_CHECK
121 * Check the magic number for the esp_structure where
125 #undef SERIAL_PARANOIA_CHECK
126 #define SERIAL_DO_RESTART
128 #undef SERIAL_DEBUG_INTR
129 #undef SERIAL_DEBUG_OPEN
130 #undef SERIAL_DEBUG_FLOW
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 tty->name, (info->flags), serial_driver.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 inline int serial_paranoia_check(struct esp_struct
*info
,
154 char *name
, const char *routine
)
156 #ifdef SERIAL_PARANOIA_CHECK
157 static const char badmagic
[] = KERN_WARNING
158 "Warning: bad magic number for serial struct (%s) in %s\n";
159 static const char badinfo
[] = KERN_WARNING
160 "Warning: null esp_struct for (%s) in %s\n";
163 printk(badinfo
, name
, routine
);
166 if (info
->magic
!= ESP_MAGIC
) {
167 printk(badmagic
, name
, routine
);
174 static inline unsigned int serial_in(struct esp_struct
*info
, int offset
)
176 return inb(info
->port
+ offset
);
179 static inline void serial_out(struct esp_struct
*info
, int offset
,
182 outb(value
, info
->port
+offset
);
186 * ------------------------------------------------------------
187 * rs_stop() and rs_start()
189 * This routines are called before setting or resetting tty->stopped.
190 * They enable or disable transmitter interrupts, as necessary.
191 * ------------------------------------------------------------
193 static void rs_stop(struct tty_struct
*tty
)
195 struct esp_struct
*info
= (struct esp_struct
*)tty
->driver_data
;
198 if (serial_paranoia_check(info
, tty
->name
, "rs_stop"))
201 spin_lock_irqsave(&info
->lock
, flags
);
202 if (info
->IER
& UART_IER_THRI
) {
203 info
->IER
&= ~UART_IER_THRI
;
204 serial_out(info
, UART_ESI_CMD1
, ESI_SET_SRV_MASK
);
205 serial_out(info
, UART_ESI_CMD2
, info
->IER
);
207 spin_unlock_irqrestore(&info
->lock
, flags
);
210 static void rs_start(struct tty_struct
*tty
)
212 struct esp_struct
*info
= (struct esp_struct
*)tty
->driver_data
;
215 if (serial_paranoia_check(info
, tty
->name
, "rs_start"))
218 spin_lock_irqsave(&info
->lock
, flags
);
219 if (info
->xmit_cnt
&& info
->xmit_buf
&& !(info
->IER
& UART_IER_THRI
)) {
220 info
->IER
|= UART_IER_THRI
;
221 serial_out(info
, UART_ESI_CMD1
, ESI_SET_SRV_MASK
);
222 serial_out(info
, UART_ESI_CMD2
, info
->IER
);
224 spin_unlock_irqrestore(&info
->lock
, flags
);
228 * ----------------------------------------------------------------------
230 * Here starts the interrupt handling routines. All of the following
231 * subroutines are declared as inline and are folded into
232 * rs_interrupt(). They were separated out for readability's sake.
234 * Note: rs_interrupt() is a "fast" interrupt, which means that it
235 * runs with interrupts turned off. People who may want to modify
236 * rs_interrupt() should try to keep the interrupt handler as fast as
237 * possible. After you are done making modifications, it is not a bad
240 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
242 * and look at the resulting assemble code in serial.s.
244 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
245 * -----------------------------------------------------------------------
249 * This routine is used by the interrupt handler to schedule
250 * processing in the software interrupt portion of the driver.
252 static inline void rs_sched_event(struct esp_struct
*info
,
255 info
->event
|= 1 << event
;
256 schedule_work(&info
->tqueue
);
259 static DEFINE_SPINLOCK(pio_lock
);
261 static inline struct esp_pio_buffer
*get_pio_buffer(void)
263 struct esp_pio_buffer
*buf
;
266 spin_lock_irqsave(&pio_lock
, flags
);
269 free_pio_buf
= buf
->next
;
271 buf
= kmalloc(sizeof(struct esp_pio_buffer
), GFP_ATOMIC
);
273 spin_unlock_irqrestore(&pio_lock
, flags
);
277 static inline void release_pio_buffer(struct esp_pio_buffer
*buf
)
280 spin_lock_irqsave(&pio_lock
, flags
);
281 buf
->next
= free_pio_buf
;
283 spin_unlock_irqrestore(&pio_lock
, flags
);
286 static inline void receive_chars_pio(struct esp_struct
*info
, int num_bytes
)
288 struct tty_struct
*tty
= info
->tty
;
290 struct esp_pio_buffer
*pio_buf
;
291 struct esp_pio_buffer
*err_buf
;
292 unsigned char status_mask
;
294 pio_buf
= get_pio_buffer();
299 err_buf
= get_pio_buffer();
302 release_pio_buffer(pio_buf
);
306 status_mask
= (info
->read_status_mask
>> 2) & 0x07;
308 for (i
= 0; i
< num_bytes
- 1; i
+= 2) {
309 *((unsigned short *)(pio_buf
->data
+ i
)) =
310 inw(info
->port
+ UART_ESI_RX
);
311 err_buf
->data
[i
] = serial_in(info
, UART_ESI_RWS
);
312 err_buf
->data
[i
+ 1] = (err_buf
->data
[i
] >> 3) & status_mask
;
313 err_buf
->data
[i
] &= status_mask
;
316 if (num_bytes
& 0x0001) {
317 pio_buf
->data
[num_bytes
- 1] = serial_in(info
, UART_ESI_RX
);
318 err_buf
->data
[num_bytes
- 1] =
319 (serial_in(info
, UART_ESI_RWS
) >> 3) & status_mask
;
322 /* make sure everything is still ok since interrupts were enabled */
326 release_pio_buffer(pio_buf
);
327 release_pio_buffer(err_buf
);
328 info
->stat_flags
&= ~ESP_STAT_RX_TIMEOUT
;
332 status_mask
= (info
->ignore_status_mask
>> 2) & 0x07;
334 for (i
= 0; i
< num_bytes
; i
++) {
335 if (!(err_buf
->data
[i
] & status_mask
)) {
338 if (err_buf
->data
[i
] & 0x04) {
340 if (info
->flags
& ASYNC_SAK
)
343 else if (err_buf
->data
[i
] & 0x02)
345 else if (err_buf
->data
[i
] & 0x01)
347 tty_insert_flip_char(tty
, pio_buf
->data
[i
], flag
);
351 tty_schedule_flip(tty
);
353 info
->stat_flags
&= ~ESP_STAT_RX_TIMEOUT
;
354 release_pio_buffer(pio_buf
);
355 release_pio_buffer(err_buf
);
358 static inline void receive_chars_dma(struct esp_struct
*info
, int num_bytes
)
361 info
->stat_flags
&= ~ESP_STAT_RX_TIMEOUT
;
362 dma_bytes
= num_bytes
;
363 info
->stat_flags
|= ESP_STAT_DMA_RX
;
365 flags
=claim_dma_lock();
368 set_dma_mode(dma
, DMA_MODE_READ
);
369 set_dma_addr(dma
, isa_virt_to_bus(dma_buffer
));
370 set_dma_count(dma
, dma_bytes
);
372 release_dma_lock(flags
);
374 serial_out(info
, UART_ESI_CMD1
, ESI_START_DMA_RX
);
377 static inline void receive_chars_dma_done(struct esp_struct
*info
,
380 struct tty_struct
*tty
= info
->tty
;
384 flags
=claim_dma_lock();
388 info
->stat_flags
&= ~ESP_STAT_DMA_RX
;
389 num_bytes
= dma_bytes
- get_dma_residue(dma
);
390 release_dma_lock(flags
);
392 info
->icount
.rx
+= num_bytes
;
395 tty_insert_flip_string(tty
, dma_buffer
, num_bytes
- 1);
397 status
&= (0x1c & info
->read_status_mask
);
399 /* Is the status significant or do we throw the last byte ? */
400 if (!(status
& info
->ignore_status_mask
)) {
404 statflag
= TTY_BREAK
;
405 (info
->icount
.brk
)++;
406 if (info
->flags
& ASYNC_SAK
)
408 } else if (status
& 0x08) {
409 statflag
= TTY_FRAME
;
410 (info
->icount
.frame
)++;
412 else if (status
& 0x04) {
413 statflag
= TTY_PARITY
;
414 (info
->icount
.parity
)++;
416 tty_insert_flip_char(tty
, dma_buffer
[num_bytes
- 1], statflag
);
418 tty_schedule_flip(tty
);
421 if (dma_bytes
!= num_bytes
) {
422 num_bytes
= dma_bytes
- num_bytes
;
424 receive_chars_dma(info
, num_bytes
);
429 /* Caller must hold info->lock */
431 static inline void transmit_chars_pio(struct esp_struct
*info
,
435 struct esp_pio_buffer
*pio_buf
;
437 pio_buf
= get_pio_buffer();
442 while (space_avail
&& info
->xmit_cnt
) {
443 if (info
->xmit_tail
+ space_avail
<= ESP_XMIT_SIZE
) {
444 memcpy(pio_buf
->data
,
445 &(info
->xmit_buf
[info
->xmit_tail
]),
448 i
= ESP_XMIT_SIZE
- info
->xmit_tail
;
449 memcpy(pio_buf
->data
,
450 &(info
->xmit_buf
[info
->xmit_tail
]), i
);
451 memcpy(&(pio_buf
->data
[i
]), info
->xmit_buf
,
455 info
->xmit_cnt
-= space_avail
;
456 info
->xmit_tail
= (info
->xmit_tail
+ space_avail
) &
459 for (i
= 0; i
< space_avail
- 1; i
+= 2) {
460 outw(*((unsigned short *)(pio_buf
->data
+ i
)),
461 info
->port
+ UART_ESI_TX
);
464 if (space_avail
& 0x0001)
465 serial_out(info
, UART_ESI_TX
,
466 pio_buf
->data
[space_avail
- 1]);
468 if (info
->xmit_cnt
) {
469 serial_out(info
, UART_ESI_CMD1
, ESI_NO_COMMAND
);
470 serial_out(info
, UART_ESI_CMD1
, ESI_GET_TX_AVAIL
);
471 space_avail
= serial_in(info
, UART_ESI_STAT1
) << 8;
472 space_avail
|= serial_in(info
, UART_ESI_STAT2
);
474 if (space_avail
> info
->xmit_cnt
)
475 space_avail
= info
->xmit_cnt
;
479 if (info
->xmit_cnt
< WAKEUP_CHARS
) {
480 rs_sched_event(info
, ESP_EVENT_WRITE_WAKEUP
);
482 #ifdef SERIAL_DEBUG_INTR
486 if (info
->xmit_cnt
<= 0) {
487 info
->IER
&= ~UART_IER_THRI
;
488 serial_out(info
, UART_ESI_CMD1
,
490 serial_out(info
, UART_ESI_CMD2
, info
->IER
);
494 release_pio_buffer(pio_buf
);
497 /* Caller must hold info->lock */
498 static inline void transmit_chars_dma(struct esp_struct
*info
, int num_bytes
)
502 dma_bytes
= num_bytes
;
504 if (info
->xmit_tail
+ dma_bytes
<= ESP_XMIT_SIZE
) {
505 memcpy(dma_buffer
, &(info
->xmit_buf
[info
->xmit_tail
]),
508 int i
= ESP_XMIT_SIZE
- info
->xmit_tail
;
509 memcpy(dma_buffer
, &(info
->xmit_buf
[info
->xmit_tail
]),
511 memcpy(&(dma_buffer
[i
]), info
->xmit_buf
, dma_bytes
- i
);
514 info
->xmit_cnt
-= dma_bytes
;
515 info
->xmit_tail
= (info
->xmit_tail
+ dma_bytes
) & (ESP_XMIT_SIZE
- 1);
517 if (info
->xmit_cnt
< WAKEUP_CHARS
) {
518 rs_sched_event(info
, ESP_EVENT_WRITE_WAKEUP
);
520 #ifdef SERIAL_DEBUG_INTR
524 if (info
->xmit_cnt
<= 0) {
525 info
->IER
&= ~UART_IER_THRI
;
526 serial_out(info
, UART_ESI_CMD1
, ESI_SET_SRV_MASK
);
527 serial_out(info
, UART_ESI_CMD2
, info
->IER
);
531 info
->stat_flags
|= ESP_STAT_DMA_TX
;
533 flags
=claim_dma_lock();
536 set_dma_mode(dma
, DMA_MODE_WRITE
);
537 set_dma_addr(dma
, isa_virt_to_bus(dma_buffer
));
538 set_dma_count(dma
, dma_bytes
);
540 release_dma_lock(flags
);
542 serial_out(info
, UART_ESI_CMD1
, ESI_START_DMA_TX
);
545 static inline void transmit_chars_dma_done(struct esp_struct
*info
)
551 flags
=claim_dma_lock();
555 num_bytes
= dma_bytes
- get_dma_residue(dma
);
556 info
->icount
.tx
+= dma_bytes
;
557 release_dma_lock(flags
);
559 if (dma_bytes
!= num_bytes
) {
560 dma_bytes
-= num_bytes
;
561 memmove(dma_buffer
, dma_buffer
+ num_bytes
, dma_bytes
);
563 flags
=claim_dma_lock();
566 set_dma_mode(dma
, DMA_MODE_WRITE
);
567 set_dma_addr(dma
, isa_virt_to_bus(dma_buffer
));
568 set_dma_count(dma
, dma_bytes
);
570 release_dma_lock(flags
);
572 serial_out(info
, UART_ESI_CMD1
, ESI_START_DMA_TX
);
575 info
->stat_flags
&= ~ESP_STAT_DMA_TX
;
579 static inline void check_modem_status(struct esp_struct
*info
)
583 serial_out(info
, UART_ESI_CMD1
, ESI_GET_UART_STAT
);
584 status
= serial_in(info
, UART_ESI_STAT2
);
586 if (status
& UART_MSR_ANY_DELTA
) {
587 /* update input line counters */
588 if (status
& UART_MSR_TERI
)
590 if (status
& UART_MSR_DDSR
)
592 if (status
& UART_MSR_DDCD
)
594 if (status
& UART_MSR_DCTS
)
596 wake_up_interruptible(&info
->delta_msr_wait
);
599 if ((info
->flags
& ASYNC_CHECK_CD
) && (status
& UART_MSR_DDCD
)) {
600 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
601 printk("ttys%d CD now %s...", info
->line
,
602 (status
& UART_MSR_DCD
) ? "on" : "off");
604 if (status
& UART_MSR_DCD
)
605 wake_up_interruptible(&info
->open_wait
);
607 #ifdef SERIAL_DEBUG_OPEN
608 printk("scheduling hangup...");
610 schedule_work(&info
->tqueue_hangup
);
616 * This is the serial driver's interrupt routine
618 static irqreturn_t
rs_interrupt_single(int irq
, void *dev_id
)
620 struct esp_struct
* info
;
622 unsigned int scratch
;
624 #ifdef SERIAL_DEBUG_INTR
625 printk("rs_interrupt_single(%d)...", irq
);
627 info
= (struct esp_struct
*)dev_id
;
629 scratch
= serial_in(info
, UART_ESI_SID
);
631 spin_lock(&info
->lock
);
634 spin_unlock(&info
->lock
);
638 if (scratch
& 0x04) { /* error */
639 serial_out(info
, UART_ESI_CMD1
, ESI_GET_ERR_STAT
);
640 err_status
= serial_in(info
, UART_ESI_STAT1
);
641 serial_in(info
, UART_ESI_STAT2
);
643 if (err_status
& 0x01)
644 info
->stat_flags
|= ESP_STAT_RX_TIMEOUT
;
646 if (err_status
& 0x20) /* UART status */
647 check_modem_status(info
);
649 if (err_status
& 0x80) /* Start break */
650 wake_up_interruptible(&info
->break_wait
);
653 if ((scratch
& 0x88) || /* DMA completed or timed out */
654 (err_status
& 0x1c) /* receive error */) {
655 if (info
->stat_flags
& ESP_STAT_DMA_RX
)
656 receive_chars_dma_done(info
, err_status
);
657 else if (info
->stat_flags
& ESP_STAT_DMA_TX
)
658 transmit_chars_dma_done(info
);
661 if (!(info
->stat_flags
& (ESP_STAT_DMA_RX
| ESP_STAT_DMA_TX
)) &&
662 ((scratch
& 0x01) || (info
->stat_flags
& ESP_STAT_RX_TIMEOUT
)) &&
663 (info
->IER
& UART_IER_RDI
)) {
666 serial_out(info
, UART_ESI_CMD1
, ESI_NO_COMMAND
);
667 serial_out(info
, UART_ESI_CMD1
, ESI_GET_RX_AVAIL
);
668 num_bytes
= serial_in(info
, UART_ESI_STAT1
) << 8;
669 num_bytes
|= serial_in(info
, UART_ESI_STAT2
);
671 num_bytes
= tty_buffer_request_room(info
->tty
, num_bytes
);
675 (info
->stat_flags
& ESP_STAT_USE_PIO
) ||
676 (num_bytes
<= info
->config
.pio_threshold
))
677 receive_chars_pio(info
, num_bytes
);
679 receive_chars_dma(info
, num_bytes
);
683 if (!(info
->stat_flags
& (ESP_STAT_DMA_RX
| ESP_STAT_DMA_TX
)) &&
684 (scratch
& 0x02) && (info
->IER
& UART_IER_THRI
)) {
685 if ((info
->xmit_cnt
<= 0) || info
->tty
->stopped
) {
686 info
->IER
&= ~UART_IER_THRI
;
687 serial_out(info
, UART_ESI_CMD1
, ESI_SET_SRV_MASK
);
688 serial_out(info
, UART_ESI_CMD2
, info
->IER
);
692 serial_out(info
, UART_ESI_CMD1
, ESI_NO_COMMAND
);
693 serial_out(info
, UART_ESI_CMD1
, ESI_GET_TX_AVAIL
);
694 num_bytes
= serial_in(info
, UART_ESI_STAT1
) << 8;
695 num_bytes
|= serial_in(info
, UART_ESI_STAT2
);
697 if (num_bytes
> info
->xmit_cnt
)
698 num_bytes
= info
->xmit_cnt
;
702 (info
->stat_flags
& ESP_STAT_USE_PIO
) ||
703 (num_bytes
<= info
->config
.pio_threshold
))
704 transmit_chars_pio(info
, num_bytes
);
706 transmit_chars_dma(info
, num_bytes
);
711 info
->last_active
= jiffies
;
713 #ifdef SERIAL_DEBUG_INTR
716 spin_unlock(&info
->lock
);
721 * -------------------------------------------------------------------
722 * Here ends the serial interrupt routines.
723 * -------------------------------------------------------------------
726 static void do_softint(struct work_struct
*work
)
728 struct esp_struct
*info
=
729 container_of(work
, struct esp_struct
, tqueue
);
730 struct tty_struct
*tty
;
736 if (test_and_clear_bit(ESP_EVENT_WRITE_WAKEUP
, &info
->event
)) {
742 * This routine is called from the scheduler tqueue when the interrupt
743 * routine has signalled that a hangup has occurred. The path of
744 * hangup processing is:
746 * serial interrupt routine -> (scheduler tqueue) ->
747 * do_serial_hangup() -> tty->hangup() -> esp_hangup()
750 static void do_serial_hangup(struct work_struct
*work
)
752 struct esp_struct
*info
=
753 container_of(work
, struct esp_struct
, tqueue_hangup
);
754 struct tty_struct
*tty
;
762 * ---------------------------------------------------------------
763 * Low level utility subroutines for the serial driver: routines to
764 * figure out the appropriate timeout for an interrupt chain, routines
765 * to initialize and startup a serial port, and routines to shutdown a
766 * serial port. Useful stuff like that.
768 * Caller should hold lock
769 * ---------------------------------------------------------------
772 static inline void esp_basic_init(struct esp_struct
* info
)
774 /* put ESPC in enhanced mode */
775 serial_out(info
, UART_ESI_CMD1
, ESI_SET_MODE
);
777 if (info
->stat_flags
& ESP_STAT_NEVER_DMA
)
778 serial_out(info
, UART_ESI_CMD2
, 0x01);
780 serial_out(info
, UART_ESI_CMD2
, 0x31);
782 /* disable interrupts for now */
783 serial_out(info
, UART_ESI_CMD1
, ESI_SET_SRV_MASK
);
784 serial_out(info
, UART_ESI_CMD2
, 0x00);
786 /* set interrupt and DMA channel */
787 serial_out(info
, UART_ESI_CMD1
, ESI_SET_IRQ
);
789 if (info
->stat_flags
& ESP_STAT_NEVER_DMA
)
790 serial_out(info
, UART_ESI_CMD2
, 0x01);
792 serial_out(info
, UART_ESI_CMD2
, (dma
<< 4) | 0x01);
794 serial_out(info
, UART_ESI_CMD1
, ESI_SET_ENH_IRQ
);
796 if (info
->line
% 8) /* secondary port */
797 serial_out(info
, UART_ESI_CMD2
, 0x0d); /* shared */
798 else if (info
->irq
== 9)
799 serial_out(info
, UART_ESI_CMD2
, 0x02);
801 serial_out(info
, UART_ESI_CMD2
, info
->irq
);
803 /* set error status mask (check this) */
804 serial_out(info
, UART_ESI_CMD1
, ESI_SET_ERR_MASK
);
806 if (info
->stat_flags
& ESP_STAT_NEVER_DMA
)
807 serial_out(info
, UART_ESI_CMD2
, 0xa1);
809 serial_out(info
, UART_ESI_CMD2
, 0xbd);
811 serial_out(info
, UART_ESI_CMD2
, 0x00);
813 /* set DMA timeout */
814 serial_out(info
, UART_ESI_CMD1
, ESI_SET_DMA_TMOUT
);
815 serial_out(info
, UART_ESI_CMD2
, 0xff);
817 /* set FIFO trigger levels */
818 serial_out(info
, UART_ESI_CMD1
, ESI_SET_TRIGGER
);
819 serial_out(info
, UART_ESI_CMD2
, info
->config
.rx_trigger
>> 8);
820 serial_out(info
, UART_ESI_CMD2
, info
->config
.rx_trigger
);
821 serial_out(info
, UART_ESI_CMD2
, info
->config
.tx_trigger
>> 8);
822 serial_out(info
, UART_ESI_CMD2
, info
->config
.tx_trigger
);
824 /* Set clock scaling and wait states */
825 serial_out(info
, UART_ESI_CMD1
, ESI_SET_PRESCALAR
);
826 serial_out(info
, UART_ESI_CMD2
, 0x04 | ESPC_SCALE
);
828 /* set reinterrupt pacing */
829 serial_out(info
, UART_ESI_CMD1
, ESI_SET_REINTR
);
830 serial_out(info
, UART_ESI_CMD2
, 0xff);
833 static int startup(struct esp_struct
* info
)
837 unsigned int num_chars
;
839 spin_lock_irqsave(&info
->lock
, flags
);
841 if (info
->flags
& ASYNC_INITIALIZED
)
844 if (!info
->xmit_buf
) {
845 info
->xmit_buf
= (unsigned char *)get_zeroed_page(GFP_ATOMIC
);
851 #ifdef SERIAL_DEBUG_OPEN
852 printk("starting up ttys%d (irq %d)...", info
->line
, info
->irq
);
855 /* Flush the RX buffer. Using the ESI flush command may cause */
856 /* wild interrupts, so read all the data instead. */
858 serial_out(info
, UART_ESI_CMD1
, ESI_NO_COMMAND
);
859 serial_out(info
, UART_ESI_CMD1
, ESI_GET_RX_AVAIL
);
860 num_chars
= serial_in(info
, UART_ESI_STAT1
) << 8;
861 num_chars
|= serial_in(info
, UART_ESI_STAT2
);
863 while (num_chars
> 1) {
864 inw(info
->port
+ UART_ESI_RX
);
869 serial_in(info
, UART_ESI_RX
);
871 /* set receive character timeout */
872 serial_out(info
, UART_ESI_CMD1
, ESI_SET_RX_TIMEOUT
);
873 serial_out(info
, UART_ESI_CMD2
, info
->config
.rx_timeout
);
875 /* clear all flags except the "never DMA" flag */
876 info
->stat_flags
&= ESP_STAT_NEVER_DMA
;
878 if (info
->stat_flags
& ESP_STAT_NEVER_DMA
)
879 info
->stat_flags
|= ESP_STAT_USE_PIO
;
881 spin_unlock_irqrestore(&info
->lock
, flags
);
887 retval
= request_irq(info
->irq
, rs_interrupt_single
, IRQF_SHARED
,
891 if (capable(CAP_SYS_ADMIN
)) {
893 set_bit(TTY_IO_ERROR
,
900 if (!(info
->stat_flags
& ESP_STAT_USE_PIO
) && !dma_buffer
) {
901 dma_buffer
= (char *)__get_dma_pages(
902 GFP_KERNEL
, get_order(DMA_BUFFER_SZ
));
904 /* use PIO mode if DMA buf/chan cannot be allocated */
906 info
->stat_flags
|= ESP_STAT_USE_PIO
;
907 else if (request_dma(dma
, "esp serial")) {
908 free_pages((unsigned long)dma_buffer
,
909 get_order(DMA_BUFFER_SZ
));
911 info
->stat_flags
|= ESP_STAT_USE_PIO
;
916 info
->MCR
= UART_MCR_DTR
| UART_MCR_RTS
| UART_MCR_OUT2
;
918 spin_lock_irqsave(&info
->lock
, flags
);
919 serial_out(info
, UART_ESI_CMD1
, ESI_WRITE_UART
);
920 serial_out(info
, UART_ESI_CMD2
, UART_MCR
);
921 serial_out(info
, UART_ESI_CMD2
, info
->MCR
);
924 * Finally, enable interrupts
926 /* info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI; */
927 info
->IER
= UART_IER_RLSI
| UART_IER_RDI
| UART_IER_DMA_TMOUT
|
929 serial_out(info
, UART_ESI_CMD1
, ESI_SET_SRV_MASK
);
930 serial_out(info
, UART_ESI_CMD2
, info
->IER
);
933 clear_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
934 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
935 spin_unlock_irqrestore(&info
->lock
, flags
);
938 * Set up the tty->alt_speed kludge
941 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_HI
)
942 info
->tty
->alt_speed
= 57600;
943 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_VHI
)
944 info
->tty
->alt_speed
= 115200;
945 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_SHI
)
946 info
->tty
->alt_speed
= 230400;
947 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_WARP
)
948 info
->tty
->alt_speed
= 460800;
952 * set the speed of the serial port
955 info
->flags
|= ASYNC_INITIALIZED
;
959 spin_unlock_irqrestore(&info
->lock
, flags
);
965 * This routine will shutdown a serial port; interrupts are disabled, and
966 * DTR is dropped if the hangup on close termio flag is on.
968 static void shutdown(struct esp_struct
* info
)
970 unsigned long flags
, f
;
972 if (!(info
->flags
& ASYNC_INITIALIZED
))
975 #ifdef SERIAL_DEBUG_OPEN
976 printk("Shutting down serial port %d (irq %d)....", info
->line
,
980 spin_lock_irqsave(&info
->lock
, flags
);
982 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
983 * here so the queue might never be waken up
985 wake_up_interruptible(&info
->delta_msr_wait
);
986 wake_up_interruptible(&info
->break_wait
);
988 /* stop a DMA transfer on the port being closed */
989 /* DMA lock is higher priority always */
990 if (info
->stat_flags
& (ESP_STAT_DMA_RX
| ESP_STAT_DMA_TX
)) {
1002 free_irq(info
->irq
, info
);
1005 struct esp_struct
*current_port
= ports
;
1007 while (current_port
) {
1008 if ((current_port
!= info
) &&
1009 (current_port
->flags
& ASYNC_INITIALIZED
))
1012 current_port
= current_port
->next_port
;
1015 if (!current_port
) {
1017 free_pages((unsigned long)dma_buffer
,
1018 get_order(DMA_BUFFER_SZ
));
1023 if (info
->xmit_buf
) {
1024 free_page((unsigned long) info
->xmit_buf
);
1025 info
->xmit_buf
= NULL
;
1029 serial_out(info
, UART_ESI_CMD1
, ESI_SET_SRV_MASK
);
1030 serial_out(info
, UART_ESI_CMD2
, 0x00);
1032 if (!info
->tty
|| (info
->tty
->termios
->c_cflag
& HUPCL
))
1033 info
->MCR
&= ~(UART_MCR_DTR
|UART_MCR_RTS
);
1035 info
->MCR
&= ~UART_MCR_OUT2
;
1036 serial_out(info
, UART_ESI_CMD1
, ESI_WRITE_UART
);
1037 serial_out(info
, UART_ESI_CMD2
, UART_MCR
);
1038 serial_out(info
, UART_ESI_CMD2
, info
->MCR
);
1041 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
1043 info
->flags
&= ~ASYNC_INITIALIZED
;
1044 spin_unlock_irqrestore(&info
->lock
, flags
);
1048 * This routine is called to set the UART divisor registers to match
1049 * the specified baud rate for a serial port.
1051 static void change_speed(struct esp_struct
*info
)
1053 unsigned short port
;
1055 unsigned cflag
,cval
;
1057 unsigned char flow1
= 0, flow2
= 0;
1058 unsigned long flags
;
1060 if (!info
->tty
|| !info
->tty
->termios
)
1062 cflag
= info
->tty
->termios
->c_cflag
;
1065 /* byte size and parity */
1066 switch (cflag
& CSIZE
) {
1067 case CS5
: cval
= 0x00; bits
= 7; break;
1068 case CS6
: cval
= 0x01; bits
= 8; break;
1069 case CS7
: cval
= 0x02; bits
= 9; break;
1070 case CS8
: cval
= 0x03; bits
= 10; break;
1071 default: cval
= 0x00; bits
= 7; break;
1073 if (cflag
& CSTOPB
) {
1077 if (cflag
& PARENB
) {
1078 cval
|= UART_LCR_PARITY
;
1081 if (!(cflag
& PARODD
))
1082 cval
|= UART_LCR_EPAR
;
1085 cval
|= UART_LCR_SPAR
;
1088 baud
= tty_get_baud_rate(info
->tty
);
1089 if (baud
== 38400 &&
1090 ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_CUST
))
1091 quot
= info
->custom_divisor
;
1094 /* Special case since 134 is really 134.5 */
1095 quot
= (2*BASE_BAUD
/ 269);
1097 quot
= BASE_BAUD
/ baud
;
1099 /* If the quotient is ever zero, default to 9600 bps */
1101 quot
= BASE_BAUD
/ 9600;
1103 info
->timeout
= ((1024 * HZ
* bits
* quot
) / BASE_BAUD
) + (HZ
/ 50);
1105 /* CTS flow control flag and modem status interrupts */
1106 /* info->IER &= ~UART_IER_MSI; */
1107 if (cflag
& CRTSCTS
) {
1108 info
->flags
|= ASYNC_CTS_FLOW
;
1109 /* info->IER |= UART_IER_MSI; */
1113 info
->flags
&= ~ASYNC_CTS_FLOW
;
1115 info
->flags
&= ~ASYNC_CHECK_CD
;
1117 info
->flags
|= ASYNC_CHECK_CD
;
1118 /* info->IER |= UART_IER_MSI; */
1122 * Set up parity check flag
1124 info
->read_status_mask
= UART_LSR_OE
| UART_LSR_THRE
| UART_LSR_DR
;
1125 if (I_INPCK(info
->tty
))
1126 info
->read_status_mask
|= UART_LSR_FE
| UART_LSR_PE
;
1127 if (I_BRKINT(info
->tty
) || I_PARMRK(info
->tty
))
1128 info
->read_status_mask
|= UART_LSR_BI
;
1130 info
->ignore_status_mask
= 0;
1132 /* This should be safe, but for some broken bits of hardware... */
1133 if (I_IGNPAR(info
->tty
)) {
1134 info
->ignore_status_mask
|= UART_LSR_PE
| UART_LSR_FE
;
1135 info
->read_status_mask
|= UART_LSR_PE
| UART_LSR_FE
;
1138 if (I_IGNBRK(info
->tty
)) {
1139 info
->ignore_status_mask
|= UART_LSR_BI
;
1140 info
->read_status_mask
|= UART_LSR_BI
;
1142 * If we're ignore parity and break indicators, ignore
1143 * overruns too. (For real raw support).
1145 if (I_IGNPAR(info
->tty
)) {
1146 info
->ignore_status_mask
|= UART_LSR_OE
| \
1147 UART_LSR_PE
| UART_LSR_FE
;
1148 info
->read_status_mask
|= UART_LSR_OE
| \
1149 UART_LSR_PE
| UART_LSR_FE
;
1153 if (I_IXOFF(info
->tty
))
1156 spin_lock_irqsave(&info
->lock
, flags
);
1158 serial_out(info
, UART_ESI_CMD1
, ESI_SET_BAUD
);
1159 serial_out(info
, UART_ESI_CMD2
, quot
>> 8);
1160 serial_out(info
, UART_ESI_CMD2
, quot
& 0xff);
1162 /* set data bits, parity, etc. */
1163 serial_out(info
, UART_ESI_CMD1
, ESI_WRITE_UART
);
1164 serial_out(info
, UART_ESI_CMD2
, UART_LCR
);
1165 serial_out(info
, UART_ESI_CMD2
, cval
);
1167 /* Enable flow control */
1168 serial_out(info
, UART_ESI_CMD1
, ESI_SET_FLOW_CNTL
);
1169 serial_out(info
, UART_ESI_CMD2
, flow1
);
1170 serial_out(info
, UART_ESI_CMD2
, flow2
);
1172 /* set flow control characters (XON/XOFF only) */
1173 if (I_IXOFF(info
->tty
)) {
1174 serial_out(info
, UART_ESI_CMD1
, ESI_SET_FLOW_CHARS
);
1175 serial_out(info
, UART_ESI_CMD2
, START_CHAR(info
->tty
));
1176 serial_out(info
, UART_ESI_CMD2
, STOP_CHAR(info
->tty
));
1177 serial_out(info
, UART_ESI_CMD2
, 0x10);
1178 serial_out(info
, UART_ESI_CMD2
, 0x21);
1179 switch (cflag
& CSIZE
) {
1181 serial_out(info
, UART_ESI_CMD2
, 0x1f);
1184 serial_out(info
, UART_ESI_CMD2
, 0x3f);
1188 serial_out(info
, UART_ESI_CMD2
, 0x7f);
1191 serial_out(info
, UART_ESI_CMD2
, 0xff);
1196 /* Set high/low water */
1197 serial_out(info
, UART_ESI_CMD1
, ESI_SET_FLOW_LVL
);
1198 serial_out(info
, UART_ESI_CMD2
, info
->config
.flow_off
>> 8);
1199 serial_out(info
, UART_ESI_CMD2
, info
->config
.flow_off
);
1200 serial_out(info
, UART_ESI_CMD2
, info
->config
.flow_on
>> 8);
1201 serial_out(info
, UART_ESI_CMD2
, info
->config
.flow_on
);
1203 spin_unlock_irqrestore(&info
->lock
, flags
);
1206 static void rs_put_char(struct tty_struct
*tty
, unsigned char ch
)
1208 struct esp_struct
*info
= (struct esp_struct
*)tty
->driver_data
;
1209 unsigned long flags
;
1211 if (serial_paranoia_check(info
, tty
->name
, "rs_put_char"))
1214 if (!info
->xmit_buf
)
1217 spin_lock_irqsave(&info
->lock
, flags
);
1218 if (info
->xmit_cnt
< ESP_XMIT_SIZE
- 1) {
1219 info
->xmit_buf
[info
->xmit_head
++] = ch
;
1220 info
->xmit_head
&= ESP_XMIT_SIZE
-1;
1223 spin_unlock_irqrestore(&info
->lock
, flags
);
1226 static void rs_flush_chars(struct tty_struct
*tty
)
1228 struct esp_struct
*info
= (struct esp_struct
*)tty
->driver_data
;
1229 unsigned long flags
;
1231 if (serial_paranoia_check(info
, tty
->name
, "rs_flush_chars"))
1234 spin_lock_irqsave(&info
->lock
, flags
);
1236 if (info
->xmit_cnt
<= 0 || tty
->stopped
|| !info
->xmit_buf
)
1239 if (!(info
->IER
& UART_IER_THRI
)) {
1240 info
->IER
|= UART_IER_THRI
;
1241 serial_out(info
, UART_ESI_CMD1
, ESI_SET_SRV_MASK
);
1242 serial_out(info
, UART_ESI_CMD2
, info
->IER
);
1245 spin_unlock_irqrestore(&info
->lock
, flags
);
1248 static int rs_write(struct tty_struct
* tty
,
1249 const unsigned char *buf
, int count
)
1252 struct esp_struct
*info
= (struct esp_struct
*)tty
->driver_data
;
1253 unsigned long flags
;
1255 if (serial_paranoia_check(info
, tty
->name
, "rs_write"))
1258 if (!info
->xmit_buf
)
1262 /* Thanks to R. Wolff for suggesting how to do this with */
1263 /* interrupts enabled */
1266 t
= ESP_XMIT_SIZE
- info
->xmit_cnt
- 1;
1271 t
= ESP_XMIT_SIZE
- info
->xmit_head
;
1279 memcpy(info
->xmit_buf
+ info
->xmit_head
, buf
, c
);
1281 info
->xmit_head
= (info
->xmit_head
+ c
) & (ESP_XMIT_SIZE
-1);
1282 info
->xmit_cnt
+= c
;
1288 spin_lock_irqsave(&info
->lock
, flags
);
1290 if (info
->xmit_cnt
&& !tty
->stopped
&& !(info
->IER
& UART_IER_THRI
)) {
1291 info
->IER
|= UART_IER_THRI
;
1292 serial_out(info
, UART_ESI_CMD1
, ESI_SET_SRV_MASK
);
1293 serial_out(info
, UART_ESI_CMD2
, info
->IER
);
1296 spin_unlock_irqrestore(&info
->lock
, flags
);
1300 static int rs_write_room(struct tty_struct
*tty
)
1302 struct esp_struct
*info
= (struct esp_struct
*)tty
->driver_data
;
1304 unsigned long flags
;
1306 if (serial_paranoia_check(info
, tty
->name
, "rs_write_room"))
1309 spin_lock_irqsave(&info
->lock
, flags
);
1311 ret
= ESP_XMIT_SIZE
- info
->xmit_cnt
- 1;
1314 spin_unlock_irqrestore(&info
->lock
, flags
);
1318 static int rs_chars_in_buffer(struct tty_struct
*tty
)
1320 struct esp_struct
*info
= (struct esp_struct
*)tty
->driver_data
;
1322 if (serial_paranoia_check(info
, tty
->name
, "rs_chars_in_buffer"))
1324 return info
->xmit_cnt
;
1327 static void rs_flush_buffer(struct tty_struct
*tty
)
1329 struct esp_struct
*info
= (struct esp_struct
*)tty
->driver_data
;
1330 unsigned long flags
;
1332 if (serial_paranoia_check(info
, tty
->name
, "rs_flush_buffer"))
1334 spin_lock_irqsave(&info
->lock
, flags
);
1335 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
1336 spin_unlock_irqrestore(&info
->lock
, flags
);
1341 * ------------------------------------------------------------
1344 * This routine is called by the upper-layer tty layer to signal that
1345 * incoming characters should be throttled.
1346 * ------------------------------------------------------------
1348 static void rs_throttle(struct tty_struct
* tty
)
1350 struct esp_struct
*info
= (struct esp_struct
*)tty
->driver_data
;
1351 unsigned long flags
;
1352 #ifdef SERIAL_DEBUG_THROTTLE
1355 printk("throttle %s: %d....\n", tty_name(tty
, buf
),
1356 tty
->ldisc
.chars_in_buffer(tty
));
1359 if (serial_paranoia_check(info
, tty
->name
, "rs_throttle"))
1362 spin_lock_irqsave(&info
->lock
, flags
);
1363 info
->IER
&= ~UART_IER_RDI
;
1364 serial_out(info
, UART_ESI_CMD1
, ESI_SET_SRV_MASK
);
1365 serial_out(info
, UART_ESI_CMD2
, info
->IER
);
1366 serial_out(info
, UART_ESI_CMD1
, ESI_SET_RX_TIMEOUT
);
1367 serial_out(info
, UART_ESI_CMD2
, 0x00);
1368 spin_unlock_irqrestore(&info
->lock
, flags
);
1371 static void rs_unthrottle(struct tty_struct
* tty
)
1373 struct esp_struct
*info
= (struct esp_struct
*)tty
->driver_data
;
1374 unsigned long flags
;
1375 #ifdef SERIAL_DEBUG_THROTTLE
1378 printk("unthrottle %s: %d....\n", tty_name(tty
, buf
),
1379 tty
->ldisc
.chars_in_buffer(tty
));
1382 if (serial_paranoia_check(info
, tty
->name
, "rs_unthrottle"))
1385 spin_lock_irqsave(&info
->lock
, flags
);
1386 info
->IER
|= UART_IER_RDI
;
1387 serial_out(info
, UART_ESI_CMD1
, ESI_SET_SRV_MASK
);
1388 serial_out(info
, UART_ESI_CMD2
, info
->IER
);
1389 serial_out(info
, UART_ESI_CMD1
, ESI_SET_RX_TIMEOUT
);
1390 serial_out(info
, UART_ESI_CMD2
, info
->config
.rx_timeout
);
1391 spin_unlock_irqrestore(&info
->lock
, flags
);
1395 * ------------------------------------------------------------
1396 * rs_ioctl() and friends
1397 * ------------------------------------------------------------
1400 static int get_serial_info(struct esp_struct
* info
,
1401 struct serial_struct __user
*retinfo
)
1403 struct serial_struct tmp
;
1405 memset(&tmp
, 0, sizeof(tmp
));
1406 tmp
.type
= PORT_16550A
;
1407 tmp
.line
= info
->line
;
1408 tmp
.port
= info
->port
;
1409 tmp
.irq
= info
->irq
;
1410 tmp
.flags
= info
->flags
;
1411 tmp
.xmit_fifo_size
= 1024;
1412 tmp
.baud_base
= BASE_BAUD
;
1413 tmp
.close_delay
= info
->close_delay
;
1414 tmp
.closing_wait
= info
->closing_wait
;
1415 tmp
.custom_divisor
= info
->custom_divisor
;
1417 if (copy_to_user(retinfo
,&tmp
,sizeof(*retinfo
)))
1422 static int get_esp_config(struct esp_struct
* info
,
1423 struct hayes_esp_config __user
*retinfo
)
1425 struct hayes_esp_config tmp
;
1430 memset(&tmp
, 0, sizeof(tmp
));
1431 tmp
.rx_timeout
= info
->config
.rx_timeout
;
1432 tmp
.rx_trigger
= info
->config
.rx_trigger
;
1433 tmp
.tx_trigger
= info
->config
.tx_trigger
;
1434 tmp
.flow_off
= info
->config
.flow_off
;
1435 tmp
.flow_on
= info
->config
.flow_on
;
1436 tmp
.pio_threshold
= info
->config
.pio_threshold
;
1437 tmp
.dma_channel
= (info
->stat_flags
& ESP_STAT_NEVER_DMA
? 0 : dma
);
1439 return copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)) ? -EFAULT
: 0;
1442 static int set_serial_info(struct esp_struct
* info
,
1443 struct serial_struct __user
*new_info
)
1445 struct serial_struct new_serial
;
1446 struct esp_struct old_info
;
1447 unsigned int change_irq
;
1449 struct esp_struct
*current_async
;
1451 if (copy_from_user(&new_serial
,new_info
,sizeof(new_serial
)))
1455 if ((new_serial
.type
!= PORT_16550A
) ||
1456 (new_serial
.hub6
) ||
1457 (info
->port
!= new_serial
.port
) ||
1458 (new_serial
.baud_base
!= BASE_BAUD
) ||
1459 (new_serial
.irq
> 15) ||
1460 (new_serial
.irq
< 2) ||
1461 (new_serial
.irq
== 6) ||
1462 (new_serial
.irq
== 8) ||
1463 (new_serial
.irq
== 13))
1466 change_irq
= new_serial
.irq
!= info
->irq
;
1468 if (change_irq
&& (info
->line
% 8))
1471 if (!capable(CAP_SYS_ADMIN
)) {
1473 (new_serial
.close_delay
!= info
->close_delay
) ||
1474 ((new_serial
.flags
& ~ASYNC_USR_MASK
) !=
1475 (info
->flags
& ~ASYNC_USR_MASK
)))
1477 info
->flags
= ((info
->flags
& ~ASYNC_USR_MASK
) |
1478 (new_serial
.flags
& ASYNC_USR_MASK
));
1479 info
->custom_divisor
= new_serial
.custom_divisor
;
1481 if (new_serial
.irq
== 2)
1485 current_async
= ports
;
1487 while (current_async
) {
1488 if ((current_async
->line
>= info
->line
) &&
1489 (current_async
->line
< (info
->line
+ 8))) {
1490 if (current_async
== info
) {
1491 if (current_async
->count
> 1)
1493 } else if (current_async
->count
)
1497 current_async
= current_async
->next_port
;
1502 * OK, past this point, all the error checking has been done.
1503 * At this point, we start making changes.....
1506 info
->flags
= ((info
->flags
& ~ASYNC_FLAGS
) |
1507 (new_serial
.flags
& ASYNC_FLAGS
));
1508 info
->custom_divisor
= new_serial
.custom_divisor
;
1509 info
->close_delay
= new_serial
.close_delay
* HZ
/100;
1510 info
->closing_wait
= new_serial
.closing_wait
* HZ
/100;
1514 * We need to shutdown the serial port at the old
1515 * port/irq combination.
1519 current_async
= ports
;
1521 while (current_async
) {
1522 if ((current_async
->line
>= info
->line
) &&
1523 (current_async
->line
< (info
->line
+ 8)))
1524 current_async
->irq
= new_serial
.irq
;
1526 current_async
= current_async
->next_port
;
1529 serial_out(info
, UART_ESI_CMD1
, ESI_SET_ENH_IRQ
);
1531 serial_out(info
, UART_ESI_CMD2
, 0x02);
1533 serial_out(info
, UART_ESI_CMD2
, info
->irq
);
1537 if (info
->flags
& ASYNC_INITIALIZED
) {
1538 if (((old_info
.flags
& ASYNC_SPD_MASK
) !=
1539 (info
->flags
& ASYNC_SPD_MASK
)) ||
1540 (old_info
.custom_divisor
!= info
->custom_divisor
)) {
1541 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_HI
)
1542 info
->tty
->alt_speed
= 57600;
1543 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_VHI
)
1544 info
->tty
->alt_speed
= 115200;
1545 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_SHI
)
1546 info
->tty
->alt_speed
= 230400;
1547 if ((info
->flags
& ASYNC_SPD_MASK
) == ASYNC_SPD_WARP
)
1548 info
->tty
->alt_speed
= 460800;
1552 retval
= startup(info
);
1557 static int set_esp_config(struct esp_struct
* info
,
1558 struct hayes_esp_config __user
* new_info
)
1560 struct hayes_esp_config new_config
;
1561 unsigned int change_dma
;
1563 struct esp_struct
*current_async
;
1564 unsigned long flags
;
1566 /* Perhaps a non-sysadmin user should be able to do some of these */
1567 /* operations. I haven't decided yet. */
1569 if (!capable(CAP_SYS_ADMIN
))
1572 if (copy_from_user(&new_config
, new_info
, sizeof(new_config
)))
1575 if ((new_config
.flow_on
>= new_config
.flow_off
) ||
1576 (new_config
.rx_trigger
< 1) ||
1577 (new_config
.tx_trigger
< 1) ||
1578 (new_config
.flow_off
< 1) ||
1579 (new_config
.flow_on
< 1) ||
1580 (new_config
.rx_trigger
> 1023) ||
1581 (new_config
.tx_trigger
> 1023) ||
1582 (new_config
.flow_off
> 1023) ||
1583 (new_config
.flow_on
> 1023) ||
1584 (new_config
.pio_threshold
< 0) ||
1585 (new_config
.pio_threshold
> 1024))
1588 if ((new_config
.dma_channel
!= 1) && (new_config
.dma_channel
!= 3))
1589 new_config
.dma_channel
= 0;
1591 if (info
->stat_flags
& ESP_STAT_NEVER_DMA
)
1592 change_dma
= new_config
.dma_channel
;
1594 change_dma
= (new_config
.dma_channel
!= dma
);
1597 if (new_config
.dma_channel
) {
1598 /* PIO mode to DMA mode transition OR */
1599 /* change current DMA channel */
1601 current_async
= ports
;
1603 while (current_async
) {
1604 if (current_async
== info
) {
1605 if (current_async
->count
> 1)
1607 } else if (current_async
->count
)
1611 current_async
->next_port
;
1615 dma
= new_config
.dma_channel
;
1616 info
->stat_flags
&= ~ESP_STAT_NEVER_DMA
;
1618 /* all ports must use the same DMA channel */
1620 spin_lock_irqsave(&info
->lock
, flags
);
1621 current_async
= ports
;
1623 while (current_async
) {
1624 esp_basic_init(current_async
);
1625 current_async
= current_async
->next_port
;
1627 spin_unlock_irqrestore(&info
->lock
, flags
);
1629 /* DMA mode to PIO mode only */
1631 if (info
->count
> 1)
1635 spin_lock_irqsave(&info
->lock
, flags
);
1636 info
->stat_flags
|= ESP_STAT_NEVER_DMA
;
1637 esp_basic_init(info
);
1638 spin_unlock_irqrestore(&info
->lock
, flags
);
1642 info
->config
.pio_threshold
= new_config
.pio_threshold
;
1644 if ((new_config
.flow_off
!= info
->config
.flow_off
) ||
1645 (new_config
.flow_on
!= info
->config
.flow_on
)) {
1646 unsigned long flags
;
1648 info
->config
.flow_off
= new_config
.flow_off
;
1649 info
->config
.flow_on
= new_config
.flow_on
;
1651 spin_lock_irqsave(&info
->lock
, flags
);
1652 serial_out(info
, UART_ESI_CMD1
, ESI_SET_FLOW_LVL
);
1653 serial_out(info
, UART_ESI_CMD2
, new_config
.flow_off
>> 8);
1654 serial_out(info
, UART_ESI_CMD2
, new_config
.flow_off
);
1655 serial_out(info
, UART_ESI_CMD2
, new_config
.flow_on
>> 8);
1656 serial_out(info
, UART_ESI_CMD2
, new_config
.flow_on
);
1657 spin_unlock_irqrestore(&info
->lock
, flags
);
1660 if ((new_config
.rx_trigger
!= info
->config
.rx_trigger
) ||
1661 (new_config
.tx_trigger
!= info
->config
.tx_trigger
)) {
1662 unsigned long flags
;
1664 info
->config
.rx_trigger
= new_config
.rx_trigger
;
1665 info
->config
.tx_trigger
= new_config
.tx_trigger
;
1666 spin_lock_irqsave(&info
->lock
, flags
);
1667 serial_out(info
, UART_ESI_CMD1
, ESI_SET_TRIGGER
);
1668 serial_out(info
, UART_ESI_CMD2
,
1669 new_config
.rx_trigger
>> 8);
1670 serial_out(info
, UART_ESI_CMD2
, new_config
.rx_trigger
);
1671 serial_out(info
, UART_ESI_CMD2
,
1672 new_config
.tx_trigger
>> 8);
1673 serial_out(info
, UART_ESI_CMD2
, new_config
.tx_trigger
);
1674 spin_unlock_irqrestore(&info
->lock
, flags
);
1677 if (new_config
.rx_timeout
!= info
->config
.rx_timeout
) {
1678 unsigned long flags
;
1680 info
->config
.rx_timeout
= new_config
.rx_timeout
;
1681 spin_lock_irqsave(&info
->lock
, flags
);
1683 if (info
->IER
& UART_IER_RDI
) {
1684 serial_out(info
, UART_ESI_CMD1
,
1685 ESI_SET_RX_TIMEOUT
);
1686 serial_out(info
, UART_ESI_CMD2
,
1687 new_config
.rx_timeout
);
1690 spin_unlock_irqrestore(&info
->lock
, flags
);
1693 if (!(info
->flags
& ASYNC_INITIALIZED
))
1694 retval
= startup(info
);
1700 * get_lsr_info - get line status register info
1702 * Purpose: Let user call ioctl() to get info when the UART physically
1703 * is emptied. On bus types like RS485, the transmitter must
1704 * release the bus after transmitting. This must be done when
1705 * the transmit shift register is empty, not be done when the
1706 * transmit holding register is empty. This functionality
1707 * allows an RS485 driver to be written in user space.
1709 static int get_lsr_info(struct esp_struct
* info
, unsigned int __user
*value
)
1711 unsigned char status
;
1712 unsigned int result
;
1713 unsigned long flags
;
1715 spin_lock_irqsave(&info
->lock
, flags
);
1716 serial_out(info
, UART_ESI_CMD1
, ESI_GET_UART_STAT
);
1717 status
= serial_in(info
, UART_ESI_STAT1
);
1718 spin_unlock_irqrestore(&info
->lock
, flags
);
1719 result
= ((status
& UART_LSR_TEMT
) ? TIOCSER_TEMT
: 0);
1720 return put_user(result
,value
);
1724 static int esp_tiocmget(struct tty_struct
*tty
, struct file
*file
)
1726 struct esp_struct
* info
= (struct esp_struct
*)tty
->driver_data
;
1727 unsigned char control
, status
;
1728 unsigned long flags
;
1730 if (serial_paranoia_check(info
, tty
->name
, __FUNCTION__
))
1732 if (tty
->flags
& (1 << TTY_IO_ERROR
))
1735 control
= info
->MCR
;
1737 spin_lock_irqsave(&info
->lock
, flags
);
1738 serial_out(info
, UART_ESI_CMD1
, ESI_GET_UART_STAT
);
1739 status
= serial_in(info
, UART_ESI_STAT2
);
1740 spin_unlock_irqrestore(&info
->lock
, flags
);
1742 return ((control
& UART_MCR_RTS
) ? TIOCM_RTS
: 0)
1743 | ((control
& UART_MCR_DTR
) ? TIOCM_DTR
: 0)
1744 | ((status
& UART_MSR_DCD
) ? TIOCM_CAR
: 0)
1745 | ((status
& UART_MSR_RI
) ? TIOCM_RNG
: 0)
1746 | ((status
& UART_MSR_DSR
) ? TIOCM_DSR
: 0)
1747 | ((status
& UART_MSR_CTS
) ? TIOCM_CTS
: 0);
1750 static int esp_tiocmset(struct tty_struct
*tty
, struct file
*file
,
1751 unsigned int set
, unsigned int clear
)
1753 struct esp_struct
* info
= (struct esp_struct
*)tty
->driver_data
;
1754 unsigned long flags
;
1756 if (serial_paranoia_check(info
, tty
->name
, __FUNCTION__
))
1758 if (tty
->flags
& (1 << TTY_IO_ERROR
))
1761 spin_lock_irqsave(&info
->lock
, flags
);
1763 if (set
& TIOCM_RTS
)
1764 info
->MCR
|= UART_MCR_RTS
;
1765 if (set
& TIOCM_DTR
)
1766 info
->MCR
|= UART_MCR_DTR
;
1768 if (clear
& TIOCM_RTS
)
1769 info
->MCR
&= ~UART_MCR_RTS
;
1770 if (clear
& TIOCM_DTR
)
1771 info
->MCR
&= ~UART_MCR_DTR
;
1773 serial_out(info
, UART_ESI_CMD1
, ESI_WRITE_UART
);
1774 serial_out(info
, UART_ESI_CMD2
, UART_MCR
);
1775 serial_out(info
, UART_ESI_CMD2
, info
->MCR
);
1777 spin_unlock_irqrestore(&info
->lock
, flags
);
1782 * rs_break() --- routine which turns the break handling on or off
1784 static void esp_break(struct tty_struct
*tty
, int break_state
)
1786 struct esp_struct
* info
= (struct esp_struct
*)tty
->driver_data
;
1787 unsigned long flags
;
1789 if (serial_paranoia_check(info
, tty
->name
, "esp_break"))
1792 if (break_state
== -1) {
1793 spin_lock_irqsave(&info
->lock
, flags
);
1794 serial_out(info
, UART_ESI_CMD1
, ESI_ISSUE_BREAK
);
1795 serial_out(info
, UART_ESI_CMD2
, 0x01);
1796 spin_unlock_irqrestore(&info
->lock
, flags
);
1798 /* FIXME - new style wait needed here */
1799 interruptible_sleep_on(&info
->break_wait
);
1801 spin_lock_irqsave(&info
->lock
, flags
);
1802 serial_out(info
, UART_ESI_CMD1
, ESI_ISSUE_BREAK
);
1803 serial_out(info
, UART_ESI_CMD2
, 0x00);
1804 spin_unlock_irqrestore(&info
->lock
, flags
);
1808 static int rs_ioctl(struct tty_struct
*tty
, struct file
* file
,
1809 unsigned int cmd
, unsigned long arg
)
1811 struct esp_struct
* info
= (struct esp_struct
*)tty
->driver_data
;
1812 struct async_icount cprev
, cnow
; /* kernel counter temps */
1813 struct serial_icounter_struct __user
*p_cuser
; /* user space */
1814 void __user
*argp
= (void __user
*)arg
;
1815 unsigned long flags
;
1817 if (serial_paranoia_check(info
, tty
->name
, "rs_ioctl"))
1820 if ((cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCSSERIAL
) &&
1821 (cmd
!= TIOCSERCONFIG
) && (cmd
!= TIOCSERGWILD
) &&
1822 (cmd
!= TIOCSERSWILD
) && (cmd
!= TIOCSERGSTRUCT
) &&
1823 (cmd
!= TIOCMIWAIT
) && (cmd
!= TIOCGICOUNT
) &&
1824 (cmd
!= TIOCGHAYESESP
) && (cmd
!= TIOCSHAYESESP
)) {
1825 if (tty
->flags
& (1 << TTY_IO_ERROR
))
1831 return get_serial_info(info
, argp
);
1833 return set_serial_info(info
, argp
);
1835 /* do not reconfigure after initial configuration */
1839 return put_user(0L, (unsigned long __user
*)argp
);
1841 case TIOCSERGETLSR
: /* Get line status register */
1842 return get_lsr_info(info
, argp
);
1845 if (!capable(CAP_SYS_ADMIN
))
1850 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1851 * - mask passed in arg for lines of interest
1852 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1853 * Caller should use TIOCGICOUNT to see which one it was
1856 spin_lock_irqsave(&info
->lock
, flags
);
1857 cprev
= info
->icount
; /* note the counters on entry */
1858 spin_unlock_irqrestore(&info
->lock
, flags
);
1860 /* FIXME: convert to new style wakeup */
1861 interruptible_sleep_on(&info
->delta_msr_wait
);
1862 /* see if a signal did it */
1863 if (signal_pending(current
))
1864 return -ERESTARTSYS
;
1865 spin_lock_irqsave(&info
->lock
, flags
);
1866 cnow
= info
->icount
; /* atomic copy */
1867 spin_unlock_irqrestore(&info
->lock
, flags
);
1868 if (cnow
.rng
== cprev
.rng
&&
1869 cnow
.dsr
== cprev
.dsr
&&
1870 cnow
.dcd
== cprev
.dcd
&&
1871 cnow
.cts
== cprev
.cts
)
1872 return -EIO
; /* no change => error */
1873 if (((arg
& TIOCM_RNG
) &&
1874 (cnow
.rng
!= cprev
.rng
)) ||
1875 ((arg
& TIOCM_DSR
) &&
1876 (cnow
.dsr
!= cprev
.dsr
)) ||
1877 ((arg
& TIOCM_CD
) &&
1878 (cnow
.dcd
!= cprev
.dcd
)) ||
1879 ((arg
& TIOCM_CTS
) &&
1880 (cnow
.cts
!= cprev
.cts
)) ) {
1888 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1889 * Return: write counters to the user passed counter struct
1890 * NB: both 1->0 and 0->1 transitions are counted except for
1891 * RI where only 0->1 is counted.
1894 spin_lock_irqsave(&info
->lock
, flags
);
1895 cnow
= info
->icount
;
1896 spin_unlock_irqrestore(&info
->lock
, flags
);
1898 if (put_user(cnow
.cts
, &p_cuser
->cts
) ||
1899 put_user(cnow
.dsr
, &p_cuser
->dsr
) ||
1900 put_user(cnow
.rng
, &p_cuser
->rng
) ||
1901 put_user(cnow
.dcd
, &p_cuser
->dcd
))
1906 return get_esp_config(info
, argp
);
1908 return set_esp_config(info
, argp
);
1911 return -ENOIOCTLCMD
;
1916 static void rs_set_termios(struct tty_struct
*tty
, struct ktermios
*old_termios
)
1918 struct esp_struct
*info
= (struct esp_struct
*)tty
->driver_data
;
1919 unsigned long flags
;
1923 spin_lock_irqsave(&info
->lock
, flags
);
1925 /* Handle transition to B0 status */
1926 if ((old_termios
->c_cflag
& CBAUD
) &&
1927 !(tty
->termios
->c_cflag
& CBAUD
)) {
1928 info
->MCR
&= ~(UART_MCR_DTR
|UART_MCR_RTS
);
1929 serial_out(info
, UART_ESI_CMD1
, ESI_WRITE_UART
);
1930 serial_out(info
, UART_ESI_CMD2
, UART_MCR
);
1931 serial_out(info
, UART_ESI_CMD2
, info
->MCR
);
1934 /* Handle transition away from B0 status */
1935 if (!(old_termios
->c_cflag
& CBAUD
) &&
1936 (tty
->termios
->c_cflag
& CBAUD
)) {
1937 info
->MCR
|= (UART_MCR_DTR
| UART_MCR_RTS
);
1938 serial_out(info
, UART_ESI_CMD1
, ESI_WRITE_UART
);
1939 serial_out(info
, UART_ESI_CMD2
, UART_MCR
);
1940 serial_out(info
, UART_ESI_CMD2
, info
->MCR
);
1943 spin_unlock_irqrestore(&info
->lock
, flags
);
1945 /* Handle turning of CRTSCTS */
1946 if ((old_termios
->c_cflag
& CRTSCTS
) &&
1947 !(tty
->termios
->c_cflag
& CRTSCTS
)) {
1953 * ------------------------------------------------------------
1956 * This routine is called when the serial port gets closed. First, we
1957 * wait for the last remaining data to be sent. Then, we unlink its
1958 * async structure from the interrupt chain if necessary, and we free
1959 * that IRQ if nothing is left in the chain.
1960 * ------------------------------------------------------------
1962 static void rs_close(struct tty_struct
*tty
, struct file
* filp
)
1964 struct esp_struct
* info
= (struct esp_struct
*)tty
->driver_data
;
1965 unsigned long flags
;
1967 if (!info
|| serial_paranoia_check(info
, tty
->name
, "rs_close"))
1970 spin_lock_irqsave(&info
->lock
, flags
);
1972 if (tty_hung_up_p(filp
)) {
1973 DBG_CNT("before DEC-hung");
1977 #ifdef SERIAL_DEBUG_OPEN
1978 printk("rs_close ttys%d, count = %d\n", info
->line
, info
->count
);
1980 if ((tty
->count
== 1) && (info
->count
!= 1)) {
1982 * Uh, oh. tty->count is 1, which means that the tty
1983 * structure will be freed. Info->count should always
1984 * be one in these conditions. If it's greater than
1985 * one, we've got real problems, since it means the
1986 * serial port won't be shutdown.
1988 printk("rs_close: bad serial port count; tty->count is 1, "
1989 "info->count is %d\n", info
->count
);
1992 if (--info
->count
< 0) {
1993 printk("rs_close: bad serial port count for ttys%d: %d\n",
1994 info
->line
, info
->count
);
1998 DBG_CNT("before DEC-2");
2001 info
->flags
|= ASYNC_CLOSING
;
2003 spin_unlock_irqrestore(&info
->lock
, flags
);
2005 * Now we wait for the transmit buffer to clear; and we notify
2006 * the line discipline to only process XON/XOFF characters.
2009 if (info
->closing_wait
!= ASYNC_CLOSING_WAIT_NONE
)
2010 tty_wait_until_sent(tty
, info
->closing_wait
);
2012 * At this point we stop accepting input. To do this, we
2013 * disable the receive line status interrupts, and tell the
2014 * interrupt driver to stop checking the data ready bit in the
2015 * line status register.
2017 /* info->IER &= ~UART_IER_RLSI; */
2018 info
->IER
&= ~UART_IER_RDI
;
2019 info
->read_status_mask
&= ~UART_LSR_DR
;
2020 if (info
->flags
& ASYNC_INITIALIZED
) {
2022 spin_lock_irqsave(&info
->lock
, flags
);
2023 serial_out(info
, UART_ESI_CMD1
, ESI_SET_SRV_MASK
);
2024 serial_out(info
, UART_ESI_CMD2
, info
->IER
);
2026 /* disable receive timeout */
2027 serial_out(info
, UART_ESI_CMD1
, ESI_SET_RX_TIMEOUT
);
2028 serial_out(info
, UART_ESI_CMD2
, 0x00);
2030 spin_unlock_irqrestore(&info
->lock
, flags
);
2033 * Before we drop DTR, make sure the UART transmitter
2034 * has completely drained; this is especially
2035 * important if there is a transmit FIFO!
2037 rs_wait_until_sent(tty
, info
->timeout
);
2040 if (tty
->driver
->flush_buffer
)
2041 tty
->driver
->flush_buffer(tty
);
2042 tty_ldisc_flush(tty
);
2047 if (info
->blocked_open
) {
2048 if (info
->close_delay
) {
2049 msleep_interruptible(jiffies_to_msecs(info
->close_delay
));
2051 wake_up_interruptible(&info
->open_wait
);
2053 info
->flags
&= ~(ASYNC_NORMAL_ACTIVE
|ASYNC_CLOSING
);
2054 wake_up_interruptible(&info
->close_wait
);
2058 spin_unlock_irqrestore(&info
->lock
, flags
);
2061 static void rs_wait_until_sent(struct tty_struct
*tty
, int timeout
)
2063 struct esp_struct
*info
= (struct esp_struct
*)tty
->driver_data
;
2064 unsigned long orig_jiffies
, char_time
;
2065 unsigned long flags
;
2067 if (serial_paranoia_check(info
, tty
->name
, "rs_wait_until_sent"))
2070 orig_jiffies
= jiffies
;
2071 char_time
= ((info
->timeout
- HZ
/ 50) / 1024) / 5;
2076 spin_lock_irqsave(&info
->lock
, flags
);
2077 serial_out(info
, UART_ESI_CMD1
, ESI_NO_COMMAND
);
2078 serial_out(info
, UART_ESI_CMD1
, ESI_GET_TX_AVAIL
);
2080 while ((serial_in(info
, UART_ESI_STAT1
) != 0x03) ||
2081 (serial_in(info
, UART_ESI_STAT2
) != 0xff)) {
2083 spin_unlock_irqrestore(&info
->lock
, flags
);
2084 msleep_interruptible(jiffies_to_msecs(char_time
));
2086 if (signal_pending(current
))
2089 if (timeout
&& time_after(jiffies
, orig_jiffies
+ timeout
))
2092 spin_lock_irqsave(&info
->lock
, flags
);
2093 serial_out(info
, UART_ESI_CMD1
, ESI_NO_COMMAND
);
2094 serial_out(info
, UART_ESI_CMD1
, ESI_GET_TX_AVAIL
);
2096 spin_unlock_irqrestore(&info
->lock
, flags
);
2097 set_current_state(TASK_RUNNING
);
2101 * esp_hangup() --- called by tty_hangup() when a hangup is signaled.
2103 static void esp_hangup(struct tty_struct
*tty
)
2105 struct esp_struct
* info
= (struct esp_struct
*)tty
->driver_data
;
2107 if (serial_paranoia_check(info
, tty
->name
, "esp_hangup"))
2110 rs_flush_buffer(tty
);
2114 info
->flags
&= ~ASYNC_NORMAL_ACTIVE
;
2116 wake_up_interruptible(&info
->open_wait
);
2120 * ------------------------------------------------------------
2121 * esp_open() and friends
2122 * ------------------------------------------------------------
2124 static int block_til_ready(struct tty_struct
*tty
, struct file
* filp
,
2125 struct esp_struct
*info
)
2127 DECLARE_WAITQUEUE(wait
, current
);
2130 unsigned long flags
;
2133 * If the device is in the middle of being closed, then block
2134 * until it's done, and then try again.
2136 if (tty_hung_up_p(filp
) ||
2137 (info
->flags
& ASYNC_CLOSING
)) {
2138 if (info
->flags
& ASYNC_CLOSING
)
2139 interruptible_sleep_on(&info
->close_wait
);
2140 #ifdef SERIAL_DO_RESTART
2141 if (info
->flags
& ASYNC_HUP_NOTIFY
)
2144 return -ERESTARTSYS
;
2151 * If non-blocking mode is set, or the port is not enabled,
2152 * then make the check up front and then exit.
2154 if ((filp
->f_flags
& O_NONBLOCK
) ||
2155 (tty
->flags
& (1 << TTY_IO_ERROR
))) {
2156 info
->flags
|= ASYNC_NORMAL_ACTIVE
;
2160 if (tty
->termios
->c_cflag
& CLOCAL
)
2164 * Block waiting for the carrier detect and the line to become
2165 * free (i.e., not in use by the callout). While we are in
2166 * this loop, info->count is dropped by one, so that
2167 * rs_close() knows when to free things. We restore it upon
2168 * exit, either normal or abnormal.
2171 add_wait_queue(&info
->open_wait
, &wait
);
2172 #ifdef SERIAL_DEBUG_OPEN
2173 printk("block_til_ready before block: ttys%d, count = %d\n",
2174 info
->line
, info
->count
);
2176 spin_lock_irqsave(&info
->lock
, flags
);
2177 if (!tty_hung_up_p(filp
))
2179 info
->blocked_open
++;
2181 if ((tty
->termios
->c_cflag
& CBAUD
)) {
2182 unsigned int scratch
;
2184 serial_out(info
, UART_ESI_CMD1
, ESI_READ_UART
);
2185 serial_out(info
, UART_ESI_CMD2
, UART_MCR
);
2186 scratch
= serial_in(info
, UART_ESI_STAT1
);
2187 serial_out(info
, UART_ESI_CMD1
, ESI_WRITE_UART
);
2188 serial_out(info
, UART_ESI_CMD2
, UART_MCR
);
2189 serial_out(info
, UART_ESI_CMD2
,
2190 scratch
| UART_MCR_DTR
| UART_MCR_RTS
);
2192 set_current_state(TASK_INTERRUPTIBLE
);
2193 if (tty_hung_up_p(filp
) ||
2194 !(info
->flags
& ASYNC_INITIALIZED
)) {
2195 #ifdef SERIAL_DO_RESTART
2196 if (info
->flags
& ASYNC_HUP_NOTIFY
)
2199 retval
= -ERESTARTSYS
;
2206 serial_out(info
, UART_ESI_CMD1
, ESI_GET_UART_STAT
);
2207 if (serial_in(info
, UART_ESI_STAT2
) & UART_MSR_DCD
)
2210 if (!(info
->flags
& ASYNC_CLOSING
) &&
2213 if (signal_pending(current
)) {
2214 retval
= -ERESTARTSYS
;
2217 #ifdef SERIAL_DEBUG_OPEN
2218 printk("block_til_ready blocking: ttys%d, count = %d\n",
2219 info
->line
, info
->count
);
2221 spin_unlock_irqrestore(&info
->lock
, flags
);
2223 spin_lock_irqsave(&info
->lock
, flags
);
2225 set_current_state(TASK_RUNNING
);
2226 remove_wait_queue(&info
->open_wait
, &wait
);
2227 if (!tty_hung_up_p(filp
))
2229 info
->blocked_open
--;
2230 spin_unlock_irqrestore(&info
->lock
, flags
);
2231 #ifdef SERIAL_DEBUG_OPEN
2232 printk("block_til_ready after blocking: ttys%d, count = %d\n",
2233 info
->line
, info
->count
);
2237 info
->flags
|= ASYNC_NORMAL_ACTIVE
;
2242 * This routine is called whenever a serial port is opened. It
2243 * enables interrupts for a serial port, linking in its async structure into
2244 * the IRQ chain. It also performs the serial-specific
2245 * initialization for the tty structure.
2247 static int esp_open(struct tty_struct
*tty
, struct file
* filp
)
2249 struct esp_struct
*info
;
2251 unsigned long flags
;
2254 if ((line
< 0) || (line
>= NR_PORTS
))
2257 /* find the port in the chain */
2261 while (info
&& (info
->line
!= line
))
2262 info
= info
->next_port
;
2265 serial_paranoia_check(info
, tty
->name
, "esp_open");
2269 #ifdef SERIAL_DEBUG_OPEN
2270 printk("esp_open %s, count = %d\n", tty
->name
, info
->count
);
2272 spin_lock_irqsave(&info
->lock
, flags
);
2274 tty
->driver_data
= info
;
2277 spin_unlock_irqrestore(&info
->lock
, flags
);
2280 * Start up serial port
2282 retval
= startup(info
);
2286 retval
= block_til_ready(tty
, filp
, info
);
2288 #ifdef SERIAL_DEBUG_OPEN
2289 printk("esp_open returning after block_til_ready with %d\n",
2295 #ifdef SERIAL_DEBUG_OPEN
2296 printk("esp_open %s successful...", tty
->name
);
2302 * ---------------------------------------------------------------------
2303 * espserial_init() and friends
2305 * espserial_init() is called at boot-time to initialize the serial driver.
2306 * ---------------------------------------------------------------------
2310 * This routine prints out the appropriate serial driver version
2311 * number, and identifies which options were configured into this
2315 static inline void show_serial_version(void)
2317 printk(KERN_INFO
"%s version %s (DMA %u)\n",
2318 serial_name
, serial_version
, dma
);
2322 * This routine is called by espserial_init() to initialize a specific serial
2325 static inline int autoconfig(struct esp_struct
* info
)
2327 int port_detected
= 0;
2328 unsigned long flags
;
2330 if (!request_region(info
->port
, REGION_SIZE
, "esp serial"))
2333 spin_lock_irqsave(&info
->lock
, flags
);
2335 * Check for ESP card
2338 if (serial_in(info
, UART_ESI_BASE
) == 0xf3) {
2339 serial_out(info
, UART_ESI_CMD1
, 0x00);
2340 serial_out(info
, UART_ESI_CMD1
, 0x01);
2342 if ((serial_in(info
, UART_ESI_STAT2
) & 0x70) == 0x20) {
2346 serial_out(info
, UART_ESI_CMD1
, 0x02);
2348 if (serial_in(info
, UART_ESI_STAT1
) & 0x01)
2355 /* put card in enhanced mode */
2356 /* this prevents access through */
2357 /* the "old" IO ports */
2358 esp_basic_init(info
);
2361 serial_out(info
, UART_ESI_CMD1
, ESI_WRITE_UART
);
2362 serial_out(info
, UART_ESI_CMD2
, UART_MCR
);
2363 serial_out(info
, UART_ESI_CMD2
, 0x00);
2367 release_region(info
->port
, REGION_SIZE
);
2369 spin_unlock_irqrestore(&info
->lock
, flags
);
2370 return (port_detected
);
2373 static const struct tty_operations esp_ops
= {
2377 .put_char
= rs_put_char
,
2378 .flush_chars
= rs_flush_chars
,
2379 .write_room
= rs_write_room
,
2380 .chars_in_buffer
= rs_chars_in_buffer
,
2381 .flush_buffer
= rs_flush_buffer
,
2383 .throttle
= rs_throttle
,
2384 .unthrottle
= rs_unthrottle
,
2385 .set_termios
= rs_set_termios
,
2388 .hangup
= esp_hangup
,
2389 .break_ctl
= esp_break
,
2390 .wait_until_sent
= rs_wait_until_sent
,
2391 .tiocmget
= esp_tiocmget
,
2392 .tiocmset
= esp_tiocmset
,
2396 * The serial driver boot-time initialization code!
2398 static int __init
espserial_init(void)
2401 struct esp_struct
* info
;
2402 struct esp_struct
*last_primary
= NULL
;
2403 int esp
[] = {0x100,0x140,0x180,0x200,0x240,0x280,0x300,0x380};
2405 esp_driver
= alloc_tty_driver(NR_PORTS
);
2409 for (i
= 0; i
< NR_PRIMARY
; i
++) {
2411 if ((irq
[i
] < 2) || (irq
[i
] > 15) || (irq
[i
] == 6) ||
2412 (irq
[i
] == 8) || (irq
[i
] == 13))
2414 else if (irq
[i
] == 2)
2419 if ((dma
!= 1) && (dma
!= 3))
2422 if ((rx_trigger
< 1) || (rx_trigger
> 1023))
2425 if ((tx_trigger
< 1) || (tx_trigger
> 1023))
2428 if ((flow_off
< 1) || (flow_off
> 1023))
2431 if ((flow_on
< 1) || (flow_on
> 1023))
2434 if ((rx_timeout
< 0) || (rx_timeout
> 255))
2437 if (flow_on
>= flow_off
)
2438 flow_on
= flow_off
- 1;
2440 show_serial_version();
2442 /* Initialize the tty_driver structure */
2444 esp_driver
->owner
= THIS_MODULE
;
2445 esp_driver
->name
= "ttyP";
2446 esp_driver
->major
= ESP_IN_MAJOR
;
2447 esp_driver
->minor_start
= 0;
2448 esp_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
2449 esp_driver
->subtype
= SERIAL_TYPE_NORMAL
;
2450 esp_driver
->init_termios
= tty_std_termios
;
2451 esp_driver
->init_termios
.c_cflag
=
2452 B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
2453 esp_driver
->flags
= TTY_DRIVER_REAL_RAW
;
2454 tty_set_operations(esp_driver
, &esp_ops
);
2455 if (tty_register_driver(esp_driver
))
2457 printk(KERN_ERR
"Couldn't register esp serial driver");
2458 put_tty_driver(esp_driver
);
2462 info
= kzalloc(sizeof(struct esp_struct
), GFP_KERNEL
);
2466 printk(KERN_ERR
"Couldn't allocate memory for esp serial device information\n");
2467 tty_unregister_driver(esp_driver
);
2468 put_tty_driver(esp_driver
);
2472 spin_lock_init(&info
->lock
);
2473 /* rx_trigger, tx_trigger are needed by autoconfig */
2474 info
->config
.rx_trigger
= rx_trigger
;
2475 info
->config
.tx_trigger
= tx_trigger
;
2481 info
->port
= esp
[i
] + offset
;
2483 info
->line
= (i
* 8) + (offset
/ 8);
2485 if (!autoconfig(info
)) {
2491 info
->custom_divisor
= (divisor
[i
] >> (offset
/ 2)) & 0xf;
2492 info
->flags
= STD_COM_FLAGS
;
2493 if (info
->custom_divisor
)
2494 info
->flags
|= ASYNC_SPD_CUST
;
2495 info
->magic
= ESP_MAGIC
;
2496 info
->close_delay
= 5*HZ
/10;
2497 info
->closing_wait
= 30*HZ
;
2498 INIT_WORK(&info
->tqueue
, do_softint
);
2499 INIT_WORK(&info
->tqueue_hangup
, do_serial_hangup
);
2500 info
->config
.rx_timeout
= rx_timeout
;
2501 info
->config
.flow_on
= flow_on
;
2502 info
->config
.flow_off
= flow_off
;
2503 info
->config
.pio_threshold
= pio_threshold
;
2504 info
->next_port
= ports
;
2505 init_waitqueue_head(&info
->open_wait
);
2506 init_waitqueue_head(&info
->close_wait
);
2507 init_waitqueue_head(&info
->delta_msr_wait
);
2508 init_waitqueue_head(&info
->break_wait
);
2510 printk(KERN_INFO
"ttyP%d at 0x%04x (irq = %d) is an ESP ",
2511 info
->line
, info
->port
, info
->irq
);
2513 if (info
->line
% 8) {
2514 printk("secondary port\n");
2515 /* 8 port cards can't do DMA */
2516 info
->stat_flags
|= ESP_STAT_NEVER_DMA
;
2519 last_primary
->stat_flags
|= ESP_STAT_NEVER_DMA
;
2521 printk("primary port\n");
2522 last_primary
= info
;
2527 info
->stat_flags
|= ESP_STAT_NEVER_DMA
;
2529 info
= kzalloc(sizeof(struct esp_struct
), GFP_KERNEL
);
2532 printk(KERN_ERR
"Couldn't allocate memory for esp serial device information\n");
2534 /* allow use of the already detected ports */
2538 /* rx_trigger, tx_trigger are needed by autoconfig */
2539 info
->config
.rx_trigger
= rx_trigger
;
2540 info
->config
.tx_trigger
= tx_trigger
;
2548 } while (i
< NR_PRIMARY
);
2550 /* free the last port memory allocation */
2556 static void __exit
espserial_exit(void)
2559 struct esp_struct
*temp_async
;
2560 struct esp_pio_buffer
*pio_buf
;
2562 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2563 if ((e1
= tty_unregister_driver(esp_driver
)))
2564 printk("SERIAL: failed to unregister serial driver (%d)\n",
2566 put_tty_driver(esp_driver
);
2570 release_region(ports
->port
, REGION_SIZE
);
2572 temp_async
= ports
->next_port
;
2578 free_pages((unsigned long)dma_buffer
,
2579 get_order(DMA_BUFFER_SZ
));
2581 while (free_pio_buf
) {
2582 pio_buf
= free_pio_buf
->next
;
2583 kfree(free_pio_buf
);
2584 free_pio_buf
= pio_buf
;
2588 module_init(espserial_init
);
2589 module_exit(espserial_exit
);