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 #define _INLINE_ inline
134 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
135 #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
136 tty->name, (info->flags), serial_driver.refcount,info->count,tty->count,s)
141 static struct esp_struct *ports;
143 static void change_speed(struct esp_struct *info);
144 static void rs_wait_until_sent(struct tty_struct *, int);
147 * The ESP card has a clock rate of 14.7456 MHz (that is, 2**ESPC_SCALE
148 * times the normal 1.8432 Mhz clock of most serial boards).
150 #define BASE_BAUD ((1843200 / 16) * (1 << ESPC_SCALE))
152 /* Standard COM flags (except for COM4, because of the 8514 problem) */
153 #define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
156 * tmp_buf is used as a temporary buffer by serial_write. We need to
157 * lock it in case the memcpy_fromfs blocks while swapping in a page,
158 * and some other program tries to do a serial write at the same time.
159 * Since the lock will only come under contention when the system is
160 * swapping and available memory is low, it makes sense to share one
161 * buffer across all the serial ports, since it significantly saves
162 * memory if large numbers of serial ports are open.
164 static unsigned char *tmp_buf;
165 static DECLARE_MUTEX(tmp_buf_sem);
167 static inline int serial_paranoia_check(struct esp_struct *info,
168 char *name, const char *routine)
170 #ifdef SERIAL_PARANOIA_CHECK
171 static const char badmagic[] = KERN_WARNING
172 "Warning: bad magic number for serial struct (%s) in %s\n";
173 static const char badinfo[] = KERN_WARNING
174 "Warning: null esp_struct for (%s) in %s\n";
177 printk(badinfo, name, routine);
180 if (info->magic != ESP_MAGIC) {
181 printk(badmagic, name, routine);
188 static inline unsigned int serial_in(struct esp_struct *info, int offset)
190 return inb(info->port + offset);
193 static inline void serial_out(struct esp_struct *info, int offset,
196 outb(value, info->port+offset);
200 * ------------------------------------------------------------
201 * rs_stop() and rs_start()
203 * This routines are called before setting or resetting tty->stopped.
204 * They enable or disable transmitter interrupts, as necessary.
205 * ------------------------------------------------------------
207 static void rs_stop(struct tty_struct *tty)
209 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
212 if (serial_paranoia_check(info, tty->name, "rs_stop"))
215 save_flags(flags); cli();
216 if (info->IER & UART_IER_THRI) {
217 info->IER &= ~UART_IER_THRI;
218 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
219 serial_out(info, UART_ESI_CMD2, info->IER);
222 restore_flags(flags);
225 static void rs_start(struct tty_struct *tty)
227 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
230 if (serial_paranoia_check(info, tty->name, "rs_start"))
233 save_flags(flags); cli();
234 if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
235 info->IER |= UART_IER_THRI;
236 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
237 serial_out(info, UART_ESI_CMD2, info->IER);
239 restore_flags(flags);
243 * ----------------------------------------------------------------------
245 * Here starts the interrupt handling routines. All of the following
246 * subroutines are declared as inline and are folded into
247 * rs_interrupt(). They were separated out for readability's sake.
249 * Note: rs_interrupt() is a "fast" interrupt, which means that it
250 * runs with interrupts turned off. People who may want to modify
251 * rs_interrupt() should try to keep the interrupt handler as fast as
252 * possible. After you are done making modifications, it is not a bad
255 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
257 * and look at the resulting assemble code in serial.s.
259 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
260 * -----------------------------------------------------------------------
264 * This routine is used by the interrupt handler to schedule
265 * processing in the software interrupt portion of the driver.
267 static _INLINE_ void rs_sched_event(struct esp_struct *info,
270 info->event |= 1 << event;
271 schedule_work(&info->tqueue);
274 static _INLINE_ struct esp_pio_buffer *get_pio_buffer(void)
276 struct esp_pio_buffer *buf;
280 free_pio_buf = buf->next;
282 buf = kmalloc(sizeof(struct esp_pio_buffer), GFP_ATOMIC);
288 static _INLINE_ void release_pio_buffer(struct esp_pio_buffer *buf)
290 buf->next = free_pio_buf;
294 static _INLINE_ void receive_chars_pio(struct esp_struct *info, int num_bytes)
296 struct tty_struct *tty = info->tty;
298 struct esp_pio_buffer *pio_buf;
299 struct esp_pio_buffer *err_buf;
300 unsigned char status_mask;
302 pio_buf = get_pio_buffer();
307 err_buf = get_pio_buffer();
310 release_pio_buffer(pio_buf);
316 status_mask = (info->read_status_mask >> 2) & 0x07;
318 for (i = 0; i < num_bytes - 1; i += 2) {
319 *((unsigned short *)(pio_buf->data + i)) =
320 inw(info->port + UART_ESI_RX);
321 err_buf->data[i] = serial_in(info, UART_ESI_RWS);
322 err_buf->data[i + 1] = (err_buf->data[i] >> 3) & status_mask;
323 err_buf->data[i] &= status_mask;
326 if (num_bytes & 0x0001) {
327 pio_buf->data[num_bytes - 1] = serial_in(info, UART_ESI_RX);
328 err_buf->data[num_bytes - 1] =
329 (serial_in(info, UART_ESI_RWS) >> 3) & status_mask;
334 /* make sure everything is still ok since interrupts were enabled */
338 release_pio_buffer(pio_buf);
339 release_pio_buffer(err_buf);
340 info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
344 status_mask = (info->ignore_status_mask >> 2) & 0x07;
346 for (i = 0; i < num_bytes; i++) {
347 if (!(err_buf->data[i] & status_mask)) {
348 *(tty->flip.char_buf_ptr++) = pio_buf->data[i];
350 if (err_buf->data[i] & 0x04) {
351 *(tty->flip.flag_buf_ptr++) = TTY_BREAK;
353 if (info->flags & ASYNC_SAK)
356 else if (err_buf->data[i] & 0x02)
357 *(tty->flip.flag_buf_ptr++) = TTY_FRAME;
358 else if (err_buf->data[i] & 0x01)
359 *(tty->flip.flag_buf_ptr++) = TTY_PARITY;
361 *(tty->flip.flag_buf_ptr++) = 0;
367 schedule_delayed_work(&tty->flip.work, 1);
369 info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
370 release_pio_buffer(pio_buf);
371 release_pio_buffer(err_buf);
374 static _INLINE_ void receive_chars_dma(struct esp_struct *info, int num_bytes)
377 info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
378 dma_bytes = num_bytes;
379 info->stat_flags |= ESP_STAT_DMA_RX;
381 flags=claim_dma_lock();
384 set_dma_mode(dma, DMA_MODE_READ);
385 set_dma_addr(dma, isa_virt_to_bus(dma_buffer));
386 set_dma_count(dma, dma_bytes);
388 release_dma_lock(flags);
390 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_RX);
393 static _INLINE_ void receive_chars_dma_done(struct esp_struct *info,
396 struct tty_struct *tty = info->tty;
401 flags=claim_dma_lock();
405 info->stat_flags &= ~ESP_STAT_DMA_RX;
406 num_bytes = dma_bytes - get_dma_residue(dma);
407 release_dma_lock(flags);
409 info->icount.rx += num_bytes;
411 memcpy(tty->flip.char_buf_ptr, dma_buffer, num_bytes);
412 tty->flip.char_buf_ptr += num_bytes;
413 tty->flip.count += num_bytes;
414 memset(tty->flip.flag_buf_ptr, 0, num_bytes);
415 tty->flip.flag_buf_ptr += num_bytes;
418 tty->flip.flag_buf_ptr--;
420 status &= (0x1c & info->read_status_mask);
422 if (status & info->ignore_status_mask) {
424 tty->flip.char_buf_ptr--;
425 tty->flip.flag_buf_ptr--;
426 } else if (status & 0x10) {
427 *tty->flip.flag_buf_ptr = TTY_BREAK;
428 (info->icount.brk)++;
429 if (info->flags & ASYNC_SAK)
431 } else if (status & 0x08) {
432 *tty->flip.flag_buf_ptr = TTY_FRAME;
433 (info->icount.frame)++;
435 else if (status & 0x04) {
436 *tty->flip.flag_buf_ptr = TTY_PARITY;
437 (info->icount.parity)++;
440 tty->flip.flag_buf_ptr++;
442 schedule_delayed_work(&tty->flip.work, 1);
445 if (dma_bytes != num_bytes) {
446 num_bytes = dma_bytes - num_bytes;
448 receive_chars_dma(info, num_bytes);
453 static _INLINE_ void transmit_chars_pio(struct esp_struct *info,
457 struct esp_pio_buffer *pio_buf;
459 pio_buf = get_pio_buffer();
464 while (space_avail && info->xmit_cnt) {
465 if (info->xmit_tail + space_avail <= ESP_XMIT_SIZE) {
466 memcpy(pio_buf->data,
467 &(info->xmit_buf[info->xmit_tail]),
470 i = ESP_XMIT_SIZE - info->xmit_tail;
471 memcpy(pio_buf->data,
472 &(info->xmit_buf[info->xmit_tail]), i);
473 memcpy(&(pio_buf->data[i]), info->xmit_buf,
477 info->xmit_cnt -= space_avail;
478 info->xmit_tail = (info->xmit_tail + space_avail) &
483 for (i = 0; i < space_avail - 1; i += 2) {
484 outw(*((unsigned short *)(pio_buf->data + i)),
485 info->port + UART_ESI_TX);
488 if (space_avail & 0x0001)
489 serial_out(info, UART_ESI_TX,
490 pio_buf->data[space_avail - 1]);
494 if (info->xmit_cnt) {
495 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
496 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
497 space_avail = serial_in(info, UART_ESI_STAT1) << 8;
498 space_avail |= serial_in(info, UART_ESI_STAT2);
500 if (space_avail > info->xmit_cnt)
501 space_avail = info->xmit_cnt;
505 if (info->xmit_cnt < WAKEUP_CHARS) {
506 rs_sched_event(info, ESP_EVENT_WRITE_WAKEUP);
508 #ifdef SERIAL_DEBUG_INTR
512 if (info->xmit_cnt <= 0) {
513 info->IER &= ~UART_IER_THRI;
514 serial_out(info, UART_ESI_CMD1,
516 serial_out(info, UART_ESI_CMD2, info->IER);
520 release_pio_buffer(pio_buf);
523 static _INLINE_ void transmit_chars_dma(struct esp_struct *info, int num_bytes)
527 dma_bytes = num_bytes;
529 if (info->xmit_tail + dma_bytes <= ESP_XMIT_SIZE) {
530 memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
533 int i = ESP_XMIT_SIZE - info->xmit_tail;
534 memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
536 memcpy(&(dma_buffer[i]), info->xmit_buf, dma_bytes - i);
539 info->xmit_cnt -= dma_bytes;
540 info->xmit_tail = (info->xmit_tail + dma_bytes) & (ESP_XMIT_SIZE - 1);
542 if (info->xmit_cnt < WAKEUP_CHARS) {
543 rs_sched_event(info, ESP_EVENT_WRITE_WAKEUP);
545 #ifdef SERIAL_DEBUG_INTR
549 if (info->xmit_cnt <= 0) {
550 info->IER &= ~UART_IER_THRI;
551 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
552 serial_out(info, UART_ESI_CMD2, info->IER);
556 info->stat_flags |= ESP_STAT_DMA_TX;
558 flags=claim_dma_lock();
561 set_dma_mode(dma, DMA_MODE_WRITE);
562 set_dma_addr(dma, isa_virt_to_bus(dma_buffer));
563 set_dma_count(dma, dma_bytes);
565 release_dma_lock(flags);
567 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
570 static _INLINE_ void transmit_chars_dma_done(struct esp_struct *info)
576 flags=claim_dma_lock();
580 num_bytes = dma_bytes - get_dma_residue(dma);
581 info->icount.tx += dma_bytes;
582 release_dma_lock(flags);
584 if (dma_bytes != num_bytes) {
585 dma_bytes -= num_bytes;
586 memmove(dma_buffer, dma_buffer + num_bytes, dma_bytes);
588 flags=claim_dma_lock();
591 set_dma_mode(dma, DMA_MODE_WRITE);
592 set_dma_addr(dma, isa_virt_to_bus(dma_buffer));
593 set_dma_count(dma, dma_bytes);
595 release_dma_lock(flags);
597 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
600 info->stat_flags &= ~ESP_STAT_DMA_TX;
604 static _INLINE_ void check_modem_status(struct esp_struct *info)
608 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
609 status = serial_in(info, UART_ESI_STAT2);
611 if (status & UART_MSR_ANY_DELTA) {
612 /* update input line counters */
613 if (status & UART_MSR_TERI)
615 if (status & UART_MSR_DDSR)
617 if (status & UART_MSR_DDCD)
619 if (status & UART_MSR_DCTS)
621 wake_up_interruptible(&info->delta_msr_wait);
624 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
625 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
626 printk("ttys%d CD now %s...", info->line,
627 (status & UART_MSR_DCD) ? "on" : "off");
629 if (status & UART_MSR_DCD)
630 wake_up_interruptible(&info->open_wait);
632 #ifdef SERIAL_DEBUG_OPEN
633 printk("scheduling hangup...");
635 schedule_work(&info->tqueue_hangup);
641 * This is the serial driver's interrupt routine
643 static irqreturn_t rs_interrupt_single(int irq, void *dev_id,
644 struct pt_regs *regs)
646 struct esp_struct * info;
648 unsigned int scratch;
650 #ifdef SERIAL_DEBUG_INTR
651 printk("rs_interrupt_single(%d)...", irq);
653 info = (struct esp_struct *)dev_id;
655 scratch = serial_in(info, UART_ESI_SID);
664 if (scratch & 0x04) { /* error */
665 serial_out(info, UART_ESI_CMD1, ESI_GET_ERR_STAT);
666 err_status = serial_in(info, UART_ESI_STAT1);
667 serial_in(info, UART_ESI_STAT2);
669 if (err_status & 0x01)
670 info->stat_flags |= ESP_STAT_RX_TIMEOUT;
672 if (err_status & 0x20) /* UART status */
673 check_modem_status(info);
675 if (err_status & 0x80) /* Start break */
676 wake_up_interruptible(&info->break_wait);
679 if ((scratch & 0x88) || /* DMA completed or timed out */
680 (err_status & 0x1c) /* receive error */) {
681 if (info->stat_flags & ESP_STAT_DMA_RX)
682 receive_chars_dma_done(info, err_status);
683 else if (info->stat_flags & ESP_STAT_DMA_TX)
684 transmit_chars_dma_done(info);
687 if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
688 ((scratch & 0x01) || (info->stat_flags & ESP_STAT_RX_TIMEOUT)) &&
689 (info->IER & UART_IER_RDI)) {
692 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
693 serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
694 num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
695 num_bytes |= serial_in(info, UART_ESI_STAT2);
697 if (num_bytes > (TTY_FLIPBUF_SIZE - info->tty->flip.count))
698 num_bytes = TTY_FLIPBUF_SIZE - info->tty->flip.count;
702 (info->stat_flags & ESP_STAT_USE_PIO) ||
703 (num_bytes <= info->config.pio_threshold))
704 receive_chars_pio(info, num_bytes);
706 receive_chars_dma(info, num_bytes);
710 if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
711 (scratch & 0x02) && (info->IER & UART_IER_THRI)) {
712 if ((info->xmit_cnt <= 0) || info->tty->stopped) {
713 info->IER &= ~UART_IER_THRI;
714 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
715 serial_out(info, UART_ESI_CMD2, info->IER);
719 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
720 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
721 num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
722 num_bytes |= serial_in(info, UART_ESI_STAT2);
724 if (num_bytes > info->xmit_cnt)
725 num_bytes = info->xmit_cnt;
729 (info->stat_flags & ESP_STAT_USE_PIO) ||
730 (num_bytes <= info->config.pio_threshold))
731 transmit_chars_pio(info, num_bytes);
733 transmit_chars_dma(info, num_bytes);
738 info->last_active = jiffies;
740 #ifdef SERIAL_DEBUG_INTR
748 * -------------------------------------------------------------------
749 * Here ends the serial interrupt routines.
750 * -------------------------------------------------------------------
753 static void do_softint(void *private_)
755 struct esp_struct *info = (struct esp_struct *) private_;
756 struct tty_struct *tty;
762 if (test_and_clear_bit(ESP_EVENT_WRITE_WAKEUP, &info->event)) {
768 * This routine is called from the scheduler tqueue when the interrupt
769 * routine has signalled that a hangup has occurred. The path of
770 * hangup processing is:
772 * serial interrupt routine -> (scheduler tqueue) ->
773 * do_serial_hangup() -> tty->hangup() -> esp_hangup()
776 static void do_serial_hangup(void *private_)
778 struct esp_struct *info = (struct esp_struct *) private_;
779 struct tty_struct *tty;
787 * ---------------------------------------------------------------
788 * Low level utility subroutines for the serial driver: routines to
789 * figure out the appropriate timeout for an interrupt chain, routines
790 * to initialize and startup a serial port, and routines to shutdown a
791 * serial port. Useful stuff like that.
792 * ---------------------------------------------------------------
795 static _INLINE_ void esp_basic_init(struct esp_struct * info)
797 /* put ESPC in enhanced mode */
798 serial_out(info, UART_ESI_CMD1, ESI_SET_MODE);
800 if (info->stat_flags & ESP_STAT_NEVER_DMA)
801 serial_out(info, UART_ESI_CMD2, 0x01);
803 serial_out(info, UART_ESI_CMD2, 0x31);
805 /* disable interrupts for now */
806 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
807 serial_out(info, UART_ESI_CMD2, 0x00);
809 /* set interrupt and DMA channel */
810 serial_out(info, UART_ESI_CMD1, ESI_SET_IRQ);
812 if (info->stat_flags & ESP_STAT_NEVER_DMA)
813 serial_out(info, UART_ESI_CMD2, 0x01);
815 serial_out(info, UART_ESI_CMD2, (dma << 4) | 0x01);
817 serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
819 if (info->line % 8) /* secondary port */
820 serial_out(info, UART_ESI_CMD2, 0x0d); /* shared */
821 else if (info->irq == 9)
822 serial_out(info, UART_ESI_CMD2, 0x02);
824 serial_out(info, UART_ESI_CMD2, info->irq);
826 /* set error status mask (check this) */
827 serial_out(info, UART_ESI_CMD1, ESI_SET_ERR_MASK);
829 if (info->stat_flags & ESP_STAT_NEVER_DMA)
830 serial_out(info, UART_ESI_CMD2, 0xa1);
832 serial_out(info, UART_ESI_CMD2, 0xbd);
834 serial_out(info, UART_ESI_CMD2, 0x00);
836 /* set DMA timeout */
837 serial_out(info, UART_ESI_CMD1, ESI_SET_DMA_TMOUT);
838 serial_out(info, UART_ESI_CMD2, 0xff);
840 /* set FIFO trigger levels */
841 serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
842 serial_out(info, UART_ESI_CMD2, info->config.rx_trigger >> 8);
843 serial_out(info, UART_ESI_CMD2, info->config.rx_trigger);
844 serial_out(info, UART_ESI_CMD2, info->config.tx_trigger >> 8);
845 serial_out(info, UART_ESI_CMD2, info->config.tx_trigger);
847 /* Set clock scaling and wait states */
848 serial_out(info, UART_ESI_CMD1, ESI_SET_PRESCALAR);
849 serial_out(info, UART_ESI_CMD2, 0x04 | ESPC_SCALE);
851 /* set reinterrupt pacing */
852 serial_out(info, UART_ESI_CMD1, ESI_SET_REINTR);
853 serial_out(info, UART_ESI_CMD2, 0xff);
856 static int startup(struct esp_struct * info)
860 unsigned int num_chars;
862 save_flags(flags); cli();
864 if (info->flags & ASYNC_INITIALIZED)
867 if (!info->xmit_buf) {
868 info->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
874 #ifdef SERIAL_DEBUG_OPEN
875 printk("starting up ttys%d (irq %d)...", info->line, info->irq);
878 /* Flush the RX buffer. Using the ESI flush command may cause */
879 /* wild interrupts, so read all the data instead. */
881 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
882 serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
883 num_chars = serial_in(info, UART_ESI_STAT1) << 8;
884 num_chars |= serial_in(info, UART_ESI_STAT2);
886 while (num_chars > 1) {
887 inw(info->port + UART_ESI_RX);
892 serial_in(info, UART_ESI_RX);
894 /* set receive character timeout */
895 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
896 serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
898 /* clear all flags except the "never DMA" flag */
899 info->stat_flags &= ESP_STAT_NEVER_DMA;
901 if (info->stat_flags & ESP_STAT_NEVER_DMA)
902 info->stat_flags |= ESP_STAT_USE_PIO;
908 retval = request_irq(info->irq, rs_interrupt_single, SA_SHIRQ,
912 if (capable(CAP_SYS_ADMIN)) {
914 set_bit(TTY_IO_ERROR,
921 if (!(info->stat_flags & ESP_STAT_USE_PIO) && !dma_buffer) {
922 dma_buffer = (char *)__get_dma_pages(
923 GFP_KERNEL, get_order(DMA_BUFFER_SZ));
925 /* use PIO mode if DMA buf/chan cannot be allocated */
927 info->stat_flags |= ESP_STAT_USE_PIO;
928 else if (request_dma(dma, "esp serial")) {
929 free_pages((unsigned long)dma_buffer,
930 get_order(DMA_BUFFER_SZ));
932 info->stat_flags |= ESP_STAT_USE_PIO;
937 info->MCR = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
938 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
939 serial_out(info, UART_ESI_CMD2, UART_MCR);
940 serial_out(info, UART_ESI_CMD2, info->MCR);
943 * Finally, enable interrupts
945 /* info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI; */
946 info->IER = UART_IER_RLSI | UART_IER_RDI | UART_IER_DMA_TMOUT |
948 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
949 serial_out(info, UART_ESI_CMD2, info->IER);
952 clear_bit(TTY_IO_ERROR, &info->tty->flags);
953 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
956 * Set up the tty->alt_speed kludge
959 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
960 info->tty->alt_speed = 57600;
961 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
962 info->tty->alt_speed = 115200;
963 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
964 info->tty->alt_speed = 230400;
965 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
966 info->tty->alt_speed = 460800;
970 * set the speed of the serial port
974 info->flags |= ASYNC_INITIALIZED;
976 out: restore_flags(flags);
981 * This routine will shutdown a serial port; interrupts are disabled, and
982 * DTR is dropped if the hangup on close termio flag is on.
984 static void shutdown(struct esp_struct * info)
986 unsigned long flags, f;
988 if (!(info->flags & ASYNC_INITIALIZED))
991 #ifdef SERIAL_DEBUG_OPEN
992 printk("Shutting down serial port %d (irq %d)....", info->line,
996 save_flags(flags); cli(); /* Disable interrupts */
999 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1000 * here so the queue might never be waken up
1002 wake_up_interruptible(&info->delta_msr_wait);
1003 wake_up_interruptible(&info->break_wait);
1005 /* stop a DMA transfer on the port being closed */
1007 if (info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) {
1011 release_dma_lock(f);
1019 free_irq(info->irq, info);
1022 struct esp_struct *current_port = ports;
1024 while (current_port) {
1025 if ((current_port != info) &&
1026 (current_port->flags & ASYNC_INITIALIZED))
1029 current_port = current_port->next_port;
1032 if (!current_port) {
1034 free_pages((unsigned long)dma_buffer,
1035 get_order(DMA_BUFFER_SZ));
1040 if (info->xmit_buf) {
1041 free_page((unsigned long) info->xmit_buf);
1042 info->xmit_buf = NULL;
1046 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1047 serial_out(info, UART_ESI_CMD2, 0x00);
1049 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
1050 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1052 info->MCR &= ~UART_MCR_OUT2;
1053 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1054 serial_out(info, UART_ESI_CMD2, UART_MCR);
1055 serial_out(info, UART_ESI_CMD2, info->MCR);
1058 set_bit(TTY_IO_ERROR, &info->tty->flags);
1060 info->flags &= ~ASYNC_INITIALIZED;
1061 restore_flags(flags);
1065 * This routine is called to set the UART divisor registers to match
1066 * the specified baud rate for a serial port.
1068 static void change_speed(struct esp_struct *info)
1070 unsigned short port;
1072 unsigned cflag,cval;
1074 unsigned char flow1 = 0, flow2 = 0;
1075 unsigned long flags;
1077 if (!info->tty || !info->tty->termios)
1079 cflag = info->tty->termios->c_cflag;
1082 /* byte size and parity */
1083 switch (cflag & CSIZE) {
1084 case CS5: cval = 0x00; bits = 7; break;
1085 case CS6: cval = 0x01; bits = 8; break;
1086 case CS7: cval = 0x02; bits = 9; break;
1087 case CS8: cval = 0x03; bits = 10; break;
1088 default: cval = 0x00; bits = 7; break;
1090 if (cflag & CSTOPB) {
1094 if (cflag & PARENB) {
1095 cval |= UART_LCR_PARITY;
1098 if (!(cflag & PARODD))
1099 cval |= UART_LCR_EPAR;
1102 cval |= UART_LCR_SPAR;
1105 baud = tty_get_baud_rate(info->tty);
1106 if (baud == 38400 &&
1107 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1108 quot = info->custom_divisor;
1111 /* Special case since 134 is really 134.5 */
1112 quot = (2*BASE_BAUD / 269);
1114 quot = BASE_BAUD / baud;
1116 /* If the quotient is ever zero, default to 9600 bps */
1118 quot = BASE_BAUD / 9600;
1120 info->timeout = ((1024 * HZ * bits * quot) / BASE_BAUD) + (HZ / 50);
1122 /* CTS flow control flag and modem status interrupts */
1123 /* info->IER &= ~UART_IER_MSI; */
1124 if (cflag & CRTSCTS) {
1125 info->flags |= ASYNC_CTS_FLOW;
1126 /* info->IER |= UART_IER_MSI; */
1130 info->flags &= ~ASYNC_CTS_FLOW;
1132 info->flags &= ~ASYNC_CHECK_CD;
1134 info->flags |= ASYNC_CHECK_CD;
1135 /* info->IER |= UART_IER_MSI; */
1139 * Set up parity check flag
1141 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1143 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1144 if (I_INPCK(info->tty))
1145 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1146 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1147 info->read_status_mask |= UART_LSR_BI;
1149 info->ignore_status_mask = 0;
1151 /* This should be safe, but for some broken bits of hardware... */
1152 if (I_IGNPAR(info->tty)) {
1153 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1154 info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
1157 if (I_IGNBRK(info->tty)) {
1158 info->ignore_status_mask |= UART_LSR_BI;
1159 info->read_status_mask |= UART_LSR_BI;
1161 * If we're ignore parity and break indicators, ignore
1162 * overruns too. (For real raw support).
1164 if (I_IGNPAR(info->tty)) {
1165 info->ignore_status_mask |= UART_LSR_OE | \
1166 UART_LSR_PE | UART_LSR_FE;
1167 info->read_status_mask |= UART_LSR_OE | \
1168 UART_LSR_PE | UART_LSR_FE;
1172 if (I_IXOFF(info->tty))
1175 save_flags(flags); cli();
1177 serial_out(info, UART_ESI_CMD1, ESI_SET_BAUD);
1178 serial_out(info, UART_ESI_CMD2, quot >> 8);
1179 serial_out(info, UART_ESI_CMD2, quot & 0xff);
1181 /* set data bits, parity, etc. */
1182 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1183 serial_out(info, UART_ESI_CMD2, UART_LCR);
1184 serial_out(info, UART_ESI_CMD2, cval);
1186 /* Enable flow control */
1187 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CNTL);
1188 serial_out(info, UART_ESI_CMD2, flow1);
1189 serial_out(info, UART_ESI_CMD2, flow2);
1191 /* set flow control characters (XON/XOFF only) */
1192 if (I_IXOFF(info->tty)) {
1193 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CHARS);
1194 serial_out(info, UART_ESI_CMD2, START_CHAR(info->tty));
1195 serial_out(info, UART_ESI_CMD2, STOP_CHAR(info->tty));
1196 serial_out(info, UART_ESI_CMD2, 0x10);
1197 serial_out(info, UART_ESI_CMD2, 0x21);
1198 switch (cflag & CSIZE) {
1200 serial_out(info, UART_ESI_CMD2, 0x1f);
1203 serial_out(info, UART_ESI_CMD2, 0x3f);
1207 serial_out(info, UART_ESI_CMD2, 0x7f);
1210 serial_out(info, UART_ESI_CMD2, 0xff);
1215 /* Set high/low water */
1216 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1217 serial_out(info, UART_ESI_CMD2, info->config.flow_off >> 8);
1218 serial_out(info, UART_ESI_CMD2, info->config.flow_off);
1219 serial_out(info, UART_ESI_CMD2, info->config.flow_on >> 8);
1220 serial_out(info, UART_ESI_CMD2, info->config.flow_on);
1222 restore_flags(flags);
1225 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
1227 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1228 unsigned long flags;
1230 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
1233 if (!tty || !info->xmit_buf)
1236 save_flags(flags); cli();
1237 if (info->xmit_cnt >= ESP_XMIT_SIZE - 1) {
1238 restore_flags(flags);
1242 info->xmit_buf[info->xmit_head++] = ch;
1243 info->xmit_head &= ESP_XMIT_SIZE-1;
1245 restore_flags(flags);
1248 static void rs_flush_chars(struct tty_struct *tty)
1250 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1251 unsigned long flags;
1253 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
1256 if (info->xmit_cnt <= 0 || tty->stopped || !info->xmit_buf)
1259 save_flags(flags); cli();
1260 if (!(info->IER & UART_IER_THRI)) {
1261 info->IER |= UART_IER_THRI;
1262 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1263 serial_out(info, UART_ESI_CMD2, info->IER);
1265 restore_flags(flags);
1268 static int rs_write(struct tty_struct * tty,
1269 const unsigned char *buf, int count)
1272 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1273 unsigned long flags;
1275 if (serial_paranoia_check(info, tty->name, "rs_write"))
1278 if (!tty || !info->xmit_buf || !tmp_buf)
1282 /* Thanks to R. Wolff for suggesting how to do this with */
1283 /* interrupts enabled */
1286 t = ESP_XMIT_SIZE - info->xmit_cnt - 1;
1291 t = ESP_XMIT_SIZE - info->xmit_head;
1299 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1301 info->xmit_head = (info->xmit_head + c) & (ESP_XMIT_SIZE-1);
1302 info->xmit_cnt += c;
1308 save_flags(flags); cli();
1310 if (info->xmit_cnt && !tty->stopped && !(info->IER & UART_IER_THRI)) {
1311 info->IER |= UART_IER_THRI;
1312 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1313 serial_out(info, UART_ESI_CMD2, info->IER);
1316 restore_flags(flags);
1320 static int rs_write_room(struct tty_struct *tty)
1322 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1325 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
1327 ret = ESP_XMIT_SIZE - info->xmit_cnt - 1;
1333 static int rs_chars_in_buffer(struct tty_struct *tty)
1335 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1337 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
1339 return info->xmit_cnt;
1342 static void rs_flush_buffer(struct tty_struct *tty)
1344 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1346 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
1349 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1355 * ------------------------------------------------------------
1358 * This routine is called by the upper-layer tty layer to signal that
1359 * incoming characters should be throttled.
1360 * ------------------------------------------------------------
1362 static void rs_throttle(struct tty_struct * tty)
1364 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1365 #ifdef SERIAL_DEBUG_THROTTLE
1368 printk("throttle %s: %d....\n", tty_name(tty, buf),
1369 tty->ldisc.chars_in_buffer(tty));
1372 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
1376 info->IER &= ~UART_IER_RDI;
1377 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1378 serial_out(info, UART_ESI_CMD2, info->IER);
1379 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1380 serial_out(info, UART_ESI_CMD2, 0x00);
1384 static void rs_unthrottle(struct tty_struct * tty)
1386 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1387 #ifdef SERIAL_DEBUG_THROTTLE
1390 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1391 tty->ldisc.chars_in_buffer(tty));
1394 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1398 info->IER |= UART_IER_RDI;
1399 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1400 serial_out(info, UART_ESI_CMD2, info->IER);
1401 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1402 serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
1407 * ------------------------------------------------------------
1408 * rs_ioctl() and friends
1409 * ------------------------------------------------------------
1412 static int get_serial_info(struct esp_struct * info,
1413 struct serial_struct __user *retinfo)
1415 struct serial_struct tmp;
1417 memset(&tmp, 0, sizeof(tmp));
1418 tmp.type = PORT_16550A;
1419 tmp.line = info->line;
1420 tmp.port = info->port;
1421 tmp.irq = info->irq;
1422 tmp.flags = info->flags;
1423 tmp.xmit_fifo_size = 1024;
1424 tmp.baud_base = BASE_BAUD;
1425 tmp.close_delay = info->close_delay;
1426 tmp.closing_wait = info->closing_wait;
1427 tmp.custom_divisor = info->custom_divisor;
1429 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1434 static int get_esp_config(struct esp_struct * info,
1435 struct hayes_esp_config __user *retinfo)
1437 struct hayes_esp_config tmp;
1442 memset(&tmp, 0, sizeof(tmp));
1443 tmp.rx_timeout = info->config.rx_timeout;
1444 tmp.rx_trigger = info->config.rx_trigger;
1445 tmp.tx_trigger = info->config.tx_trigger;
1446 tmp.flow_off = info->config.flow_off;
1447 tmp.flow_on = info->config.flow_on;
1448 tmp.pio_threshold = info->config.pio_threshold;
1449 tmp.dma_channel = (info->stat_flags & ESP_STAT_NEVER_DMA ? 0 : dma);
1451 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
1454 static int set_serial_info(struct esp_struct * info,
1455 struct serial_struct __user *new_info)
1457 struct serial_struct new_serial;
1458 struct esp_struct old_info;
1459 unsigned int change_irq;
1461 struct esp_struct *current_async;
1463 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1467 if ((new_serial.type != PORT_16550A) ||
1468 (new_serial.hub6) ||
1469 (info->port != new_serial.port) ||
1470 (new_serial.baud_base != BASE_BAUD) ||
1471 (new_serial.irq > 15) ||
1472 (new_serial.irq < 2) ||
1473 (new_serial.irq == 6) ||
1474 (new_serial.irq == 8) ||
1475 (new_serial.irq == 13))
1478 change_irq = new_serial.irq != info->irq;
1480 if (change_irq && (info->line % 8))
1483 if (!capable(CAP_SYS_ADMIN)) {
1485 (new_serial.close_delay != info->close_delay) ||
1486 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1487 (info->flags & ~ASYNC_USR_MASK)))
1489 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1490 (new_serial.flags & ASYNC_USR_MASK));
1491 info->custom_divisor = new_serial.custom_divisor;
1493 if (new_serial.irq == 2)
1497 current_async = ports;
1499 while (current_async) {
1500 if ((current_async->line >= info->line) &&
1501 (current_async->line < (info->line + 8))) {
1502 if (current_async == info) {
1503 if (current_async->count > 1)
1505 } else if (current_async->count)
1509 current_async = current_async->next_port;
1514 * OK, past this point, all the error checking has been done.
1515 * At this point, we start making changes.....
1518 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1519 (new_serial.flags & ASYNC_FLAGS));
1520 info->custom_divisor = new_serial.custom_divisor;
1521 info->close_delay = new_serial.close_delay * HZ/100;
1522 info->closing_wait = new_serial.closing_wait * HZ/100;
1526 * We need to shutdown the serial port at the old
1527 * port/irq combination.
1531 current_async = ports;
1533 while (current_async) {
1534 if ((current_async->line >= info->line) &&
1535 (current_async->line < (info->line + 8)))
1536 current_async->irq = new_serial.irq;
1538 current_async = current_async->next_port;
1541 serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
1543 serial_out(info, UART_ESI_CMD2, 0x02);
1545 serial_out(info, UART_ESI_CMD2, info->irq);
1549 if (info->flags & ASYNC_INITIALIZED) {
1550 if (((old_info.flags & ASYNC_SPD_MASK) !=
1551 (info->flags & ASYNC_SPD_MASK)) ||
1552 (old_info.custom_divisor != info->custom_divisor)) {
1553 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1554 info->tty->alt_speed = 57600;
1555 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1556 info->tty->alt_speed = 115200;
1557 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1558 info->tty->alt_speed = 230400;
1559 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1560 info->tty->alt_speed = 460800;
1564 retval = startup(info);
1569 static int set_esp_config(struct esp_struct * info,
1570 struct hayes_esp_config __user * new_info)
1572 struct hayes_esp_config new_config;
1573 unsigned int change_dma;
1575 struct esp_struct *current_async;
1577 /* Perhaps a non-sysadmin user should be able to do some of these */
1578 /* operations. I haven't decided yet. */
1580 if (!capable(CAP_SYS_ADMIN))
1583 if (copy_from_user(&new_config, new_info, sizeof(new_config)))
1586 if ((new_config.flow_on >= new_config.flow_off) ||
1587 (new_config.rx_trigger < 1) ||
1588 (new_config.tx_trigger < 1) ||
1589 (new_config.flow_off < 1) ||
1590 (new_config.flow_on < 1) ||
1591 (new_config.rx_trigger > 1023) ||
1592 (new_config.tx_trigger > 1023) ||
1593 (new_config.flow_off > 1023) ||
1594 (new_config.flow_on > 1023) ||
1595 (new_config.pio_threshold < 0) ||
1596 (new_config.pio_threshold > 1024))
1599 if ((new_config.dma_channel != 1) && (new_config.dma_channel != 3))
1600 new_config.dma_channel = 0;
1602 if (info->stat_flags & ESP_STAT_NEVER_DMA)
1603 change_dma = new_config.dma_channel;
1605 change_dma = (new_config.dma_channel != dma);
1608 if (new_config.dma_channel) {
1609 /* PIO mode to DMA mode transition OR */
1610 /* change current DMA channel */
1612 current_async = ports;
1614 while (current_async) {
1615 if (current_async == info) {
1616 if (current_async->count > 1)
1618 } else if (current_async->count)
1622 current_async->next_port;
1626 dma = new_config.dma_channel;
1627 info->stat_flags &= ~ESP_STAT_NEVER_DMA;
1629 /* all ports must use the same DMA channel */
1631 current_async = ports;
1633 while (current_async) {
1634 esp_basic_init(current_async);
1635 current_async = current_async->next_port;
1638 /* DMA mode to PIO mode only */
1640 if (info->count > 1)
1644 info->stat_flags |= ESP_STAT_NEVER_DMA;
1645 esp_basic_init(info);
1649 info->config.pio_threshold = new_config.pio_threshold;
1651 if ((new_config.flow_off != info->config.flow_off) ||
1652 (new_config.flow_on != info->config.flow_on)) {
1653 unsigned long flags;
1655 info->config.flow_off = new_config.flow_off;
1656 info->config.flow_on = new_config.flow_on;
1657 save_flags(flags); cli();
1658 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1659 serial_out(info, UART_ESI_CMD2, new_config.flow_off >> 8);
1660 serial_out(info, UART_ESI_CMD2, new_config.flow_off);
1661 serial_out(info, UART_ESI_CMD2, new_config.flow_on >> 8);
1662 serial_out(info, UART_ESI_CMD2, new_config.flow_on);
1663 restore_flags(flags);
1666 if ((new_config.rx_trigger != info->config.rx_trigger) ||
1667 (new_config.tx_trigger != info->config.tx_trigger)) {
1668 unsigned long flags;
1670 info->config.rx_trigger = new_config.rx_trigger;
1671 info->config.tx_trigger = new_config.tx_trigger;
1672 save_flags(flags); cli();
1673 serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
1674 serial_out(info, UART_ESI_CMD2,
1675 new_config.rx_trigger >> 8);
1676 serial_out(info, UART_ESI_CMD2, new_config.rx_trigger);
1677 serial_out(info, UART_ESI_CMD2,
1678 new_config.tx_trigger >> 8);
1679 serial_out(info, UART_ESI_CMD2, new_config.tx_trigger);
1680 restore_flags(flags);
1683 if (new_config.rx_timeout != info->config.rx_timeout) {
1684 unsigned long flags;
1686 info->config.rx_timeout = new_config.rx_timeout;
1687 save_flags(flags); cli();
1689 if (info->IER & UART_IER_RDI) {
1690 serial_out(info, UART_ESI_CMD1,
1691 ESI_SET_RX_TIMEOUT);
1692 serial_out(info, UART_ESI_CMD2,
1693 new_config.rx_timeout);
1696 restore_flags(flags);
1699 if (!(info->flags & ASYNC_INITIALIZED))
1700 retval = startup(info);
1706 * get_lsr_info - get line status register info
1708 * Purpose: Let user call ioctl() to get info when the UART physically
1709 * is emptied. On bus types like RS485, the transmitter must
1710 * release the bus after transmitting. This must be done when
1711 * the transmit shift register is empty, not be done when the
1712 * transmit holding register is empty. This functionality
1713 * allows an RS485 driver to be written in user space.
1715 static int get_lsr_info(struct esp_struct * info, unsigned int __user *value)
1717 unsigned char status;
1718 unsigned int result;
1721 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1722 status = serial_in(info, UART_ESI_STAT1);
1724 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1725 return put_user(result,value);
1729 static int esp_tiocmget(struct tty_struct *tty, struct file *file)
1731 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1732 unsigned char control, status;
1734 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1736 if (tty->flags & (1 << TTY_IO_ERROR))
1739 control = info->MCR;
1741 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1742 status = serial_in(info, UART_ESI_STAT2);
1744 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1745 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1746 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1747 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1748 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1749 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1752 static int esp_tiocmset(struct tty_struct *tty, struct file *file,
1753 unsigned int set, unsigned int clear)
1755 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1757 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1759 if (tty->flags & (1 << TTY_IO_ERROR))
1764 if (set & TIOCM_RTS)
1765 info->MCR |= UART_MCR_RTS;
1766 if (set & TIOCM_DTR)
1767 info->MCR |= UART_MCR_DTR;
1769 if (clear & TIOCM_RTS)
1770 info->MCR &= ~UART_MCR_RTS;
1771 if (clear & TIOCM_DTR)
1772 info->MCR &= ~UART_MCR_DTR;
1774 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1775 serial_out(info, UART_ESI_CMD2, UART_MCR);
1776 serial_out(info, UART_ESI_CMD2, info->MCR);
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 save_flags(flags); cli();
1793 if (break_state == -1) {
1794 serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1795 serial_out(info, UART_ESI_CMD2, 0x01);
1797 interruptible_sleep_on(&info->break_wait);
1799 serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1800 serial_out(info, UART_ESI_CMD2, 0x00);
1802 restore_flags(flags);
1805 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1806 unsigned int cmd, unsigned long arg)
1808 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1809 struct async_icount cprev, cnow; /* kernel counter temps */
1810 struct serial_icounter_struct __user *p_cuser; /* user space */
1811 void __user *argp = (void __user *)arg;
1813 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1816 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1817 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1818 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT) &&
1819 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT) &&
1820 (cmd != TIOCGHAYESESP) && (cmd != TIOCSHAYESESP)) {
1821 if (tty->flags & (1 << TTY_IO_ERROR))
1827 return get_serial_info(info, argp);
1829 return set_serial_info(info, argp);
1831 /* do not reconfigure after initial configuration */
1835 return put_user(0L, (unsigned long __user *)argp);
1837 case TIOCSERGETLSR: /* Get line status register */
1838 return get_lsr_info(info, argp);
1841 if (!capable(CAP_SYS_ADMIN))
1846 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1847 * - mask passed in arg for lines of interest
1848 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1849 * Caller should use TIOCGICOUNT to see which one it was
1853 cprev = info->icount; /* note the counters on entry */
1856 interruptible_sleep_on(&info->delta_msr_wait);
1857 /* see if a signal did it */
1858 if (signal_pending(current))
1859 return -ERESTARTSYS;
1861 cnow = info->icount; /* atomic copy */
1863 if (cnow.rng == cprev.rng &&
1864 cnow.dsr == cprev.dsr &&
1865 cnow.dcd == cprev.dcd &&
1866 cnow.cts == cprev.cts)
1867 return -EIO; /* no change => error */
1868 if (((arg & TIOCM_RNG) &&
1869 (cnow.rng != cprev.rng)) ||
1870 ((arg & TIOCM_DSR) &&
1871 (cnow.dsr != cprev.dsr)) ||
1872 ((arg & TIOCM_CD) &&
1873 (cnow.dcd != cprev.dcd)) ||
1874 ((arg & TIOCM_CTS) &&
1875 (cnow.cts != cprev.cts)) ) {
1883 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1884 * Return: write counters to the user passed counter struct
1885 * NB: both 1->0 and 0->1 transitions are counted except for
1886 * RI where only 0->1 is counted.
1890 cnow = info->icount;
1893 if (put_user(cnow.cts, &p_cuser->cts) ||
1894 put_user(cnow.dsr, &p_cuser->dsr) ||
1895 put_user(cnow.rng, &p_cuser->rng) ||
1896 put_user(cnow.dcd, &p_cuser->dcd))
1901 return get_esp_config(info, argp);
1903 return set_esp_config(info, argp);
1906 return -ENOIOCTLCMD;
1911 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1913 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1915 if ( (tty->termios->c_cflag == old_termios->c_cflag)
1916 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
1917 == RELEVANT_IFLAG(old_termios->c_iflag)))
1922 /* Handle transition to B0 status */
1923 if ((old_termios->c_cflag & CBAUD) &&
1924 !(tty->termios->c_cflag & CBAUD)) {
1925 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1927 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1928 serial_out(info, UART_ESI_CMD2, UART_MCR);
1929 serial_out(info, UART_ESI_CMD2, info->MCR);
1933 /* Handle transition away from B0 status */
1934 if (!(old_termios->c_cflag & CBAUD) &&
1935 (tty->termios->c_cflag & CBAUD)) {
1936 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);
1944 /* Handle turning of CRTSCTS */
1945 if ((old_termios->c_cflag & CRTSCTS) &&
1946 !(tty->termios->c_cflag & CRTSCTS)) {
1952 * No need to wake up processes in open wait, since they
1953 * sample the CLOCAL flag once, and don't recheck it.
1954 * XXX It's not clear whether the current behavior is correct
1955 * or not. Hence, this may change.....
1957 if (!(old_termios->c_cflag & CLOCAL) &&
1958 (tty->termios->c_cflag & CLOCAL))
1959 wake_up_interruptible(&info->open_wait);
1964 * ------------------------------------------------------------
1967 * This routine is called when the serial port gets closed. First, we
1968 * wait for the last remaining data to be sent. Then, we unlink its
1969 * async structure from the interrupt chain if necessary, and we free
1970 * that IRQ if nothing is left in the chain.
1971 * ------------------------------------------------------------
1973 static void rs_close(struct tty_struct *tty, struct file * filp)
1975 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1976 unsigned long flags;
1978 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1981 save_flags(flags); cli();
1983 if (tty_hung_up_p(filp)) {
1984 DBG_CNT("before DEC-hung");
1988 #ifdef SERIAL_DEBUG_OPEN
1989 printk("rs_close ttys%d, count = %d\n", info->line, info->count);
1991 if ((tty->count == 1) && (info->count != 1)) {
1993 * Uh, oh. tty->count is 1, which means that the tty
1994 * structure will be freed. Info->count should always
1995 * be one in these conditions. If it's greater than
1996 * one, we've got real problems, since it means the
1997 * serial port won't be shutdown.
1999 printk("rs_close: bad serial port count; tty->count is 1, "
2000 "info->count is %d\n", info->count);
2003 if (--info->count < 0) {
2004 printk("rs_close: bad serial port count for ttys%d: %d\n",
2005 info->line, info->count);
2009 DBG_CNT("before DEC-2");
2012 info->flags |= ASYNC_CLOSING;
2014 * Now we wait for the transmit buffer to clear; and we notify
2015 * the line discipline to only process XON/XOFF characters.
2018 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
2019 tty_wait_until_sent(tty, info->closing_wait);
2021 * At this point we stop accepting input. To do this, we
2022 * disable the receive line status interrupts, and tell the
2023 * interrupt driver to stop checking the data ready bit in the
2024 * line status register.
2026 /* info->IER &= ~UART_IER_RLSI; */
2027 info->IER &= ~UART_IER_RDI;
2028 info->read_status_mask &= ~UART_LSR_DR;
2029 if (info->flags & ASYNC_INITIALIZED) {
2030 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
2031 serial_out(info, UART_ESI_CMD2, info->IER);
2033 /* disable receive timeout */
2034 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
2035 serial_out(info, UART_ESI_CMD2, 0x00);
2038 * Before we drop DTR, make sure the UART transmitter
2039 * has completely drained; this is especially
2040 * important if there is a transmit FIFO!
2042 rs_wait_until_sent(tty, info->timeout);
2045 if (tty->driver->flush_buffer)
2046 tty->driver->flush_buffer(tty);
2047 tty_ldisc_flush(tty);
2052 if (info->blocked_open) {
2053 if (info->close_delay) {
2054 msleep_interruptible(jiffies_to_msecs(info->close_delay));
2056 wake_up_interruptible(&info->open_wait);
2058 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
2059 wake_up_interruptible(&info->close_wait);
2061 restore_flags(flags);
2064 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
2066 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
2067 unsigned long orig_jiffies, char_time;
2068 unsigned long flags;
2070 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
2073 orig_jiffies = jiffies;
2074 char_time = ((info->timeout - HZ / 50) / 1024) / 5;
2079 save_flags(flags); cli();
2080 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2081 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2083 while ((serial_in(info, UART_ESI_STAT1) != 0x03) ||
2084 (serial_in(info, UART_ESI_STAT2) != 0xff)) {
2085 msleep_interruptible(jiffies_to_msecs(char_time));
2087 if (signal_pending(current))
2090 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2093 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2094 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2097 restore_flags(flags);
2098 set_current_state(TASK_RUNNING);
2102 * esp_hangup() --- called by tty_hangup() when a hangup is signaled.
2104 static void esp_hangup(struct tty_struct *tty)
2106 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
2108 if (serial_paranoia_check(info, tty->name, "esp_hangup"))
2111 rs_flush_buffer(tty);
2115 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2117 wake_up_interruptible(&info->open_wait);
2121 * ------------------------------------------------------------
2122 * esp_open() and friends
2123 * ------------------------------------------------------------
2125 static int block_til_ready(struct tty_struct *tty, struct file * filp,
2126 struct esp_struct *info)
2128 DECLARE_WAITQUEUE(wait, current);
2131 unsigned long flags;
2134 * If the device is in the middle of being closed, then block
2135 * until it's done, and then try again.
2137 if (tty_hung_up_p(filp) ||
2138 (info->flags & ASYNC_CLOSING)) {
2139 if (info->flags & ASYNC_CLOSING)
2140 interruptible_sleep_on(&info->close_wait);
2141 #ifdef SERIAL_DO_RESTART
2142 if (info->flags & ASYNC_HUP_NOTIFY)
2145 return -ERESTARTSYS;
2152 * If non-blocking mode is set, or the port is not enabled,
2153 * then make the check up front and then exit.
2155 if ((filp->f_flags & O_NONBLOCK) ||
2156 (tty->flags & (1 << TTY_IO_ERROR))) {
2157 info->flags |= ASYNC_NORMAL_ACTIVE;
2161 if (tty->termios->c_cflag & CLOCAL)
2165 * Block waiting for the carrier detect and the line to become
2166 * free (i.e., not in use by the callout). While we are in
2167 * this loop, info->count is dropped by one, so that
2168 * rs_close() knows when to free things. We restore it upon
2169 * exit, either normal or abnormal.
2172 add_wait_queue(&info->open_wait, &wait);
2173 #ifdef SERIAL_DEBUG_OPEN
2174 printk("block_til_ready before block: ttys%d, count = %d\n",
2175 info->line, info->count);
2179 if (!tty_hung_up_p(filp))
2181 restore_flags(flags);
2182 info->blocked_open++;
2186 if ((tty->termios->c_cflag & CBAUD)) {
2187 unsigned int scratch;
2189 serial_out(info, UART_ESI_CMD1, ESI_READ_UART);
2190 serial_out(info, UART_ESI_CMD2, UART_MCR);
2191 scratch = serial_in(info, UART_ESI_STAT1);
2192 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2193 serial_out(info, UART_ESI_CMD2, UART_MCR);
2194 serial_out(info, UART_ESI_CMD2,
2195 scratch | UART_MCR_DTR | UART_MCR_RTS);
2197 restore_flags(flags);
2198 set_current_state(TASK_INTERRUPTIBLE);
2199 if (tty_hung_up_p(filp) ||
2200 !(info->flags & ASYNC_INITIALIZED)) {
2201 #ifdef SERIAL_DO_RESTART
2202 if (info->flags & ASYNC_HUP_NOTIFY)
2205 retval = -ERESTARTSYS;
2212 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
2213 if (serial_in(info, UART_ESI_STAT2) & UART_MSR_DCD)
2216 if (!(info->flags & ASYNC_CLOSING) &&
2219 if (signal_pending(current)) {
2220 retval = -ERESTARTSYS;
2223 #ifdef SERIAL_DEBUG_OPEN
2224 printk("block_til_ready blocking: ttys%d, count = %d\n",
2225 info->line, info->count);
2229 set_current_state(TASK_RUNNING);
2230 remove_wait_queue(&info->open_wait, &wait);
2231 if (!tty_hung_up_p(filp))
2233 info->blocked_open--;
2234 #ifdef SERIAL_DEBUG_OPEN
2235 printk("block_til_ready after blocking: ttys%d, count = %d\n",
2236 info->line, info->count);
2240 info->flags |= ASYNC_NORMAL_ACTIVE;
2245 * This routine is called whenever a serial port is opened. It
2246 * enables interrupts for a serial port, linking in its async structure into
2247 * the IRQ chain. It also performs the serial-specific
2248 * initialization for the tty structure.
2250 static int esp_open(struct tty_struct *tty, struct file * filp)
2252 struct esp_struct *info;
2256 if ((line < 0) || (line >= NR_PORTS))
2259 /* find the port in the chain */
2263 while (info && (info->line != line))
2264 info = info->next_port;
2267 serial_paranoia_check(info, tty->name, "esp_open");
2271 #ifdef SERIAL_DEBUG_OPEN
2272 printk("esp_open %s, count = %d\n", tty->name, info->count);
2275 tty->driver_data = info;
2279 tmp_buf = (unsigned char *) get_zeroed_page(GFP_KERNEL);
2285 * Start up serial port
2287 retval = startup(info);
2291 retval = block_til_ready(tty, filp, info);
2293 #ifdef SERIAL_DEBUG_OPEN
2294 printk("esp_open returning after block_til_ready with %d\n",
2300 #ifdef SERIAL_DEBUG_OPEN
2301 printk("esp_open %s successful...", tty->name);
2307 * ---------------------------------------------------------------------
2308 * espserial_init() and friends
2310 * espserial_init() is called at boot-time to initialize the serial driver.
2311 * ---------------------------------------------------------------------
2315 * This routine prints out the appropriate serial driver version
2316 * number, and identifies which options were configured into this
2320 static _INLINE_ void show_serial_version(void)
2322 printk(KERN_INFO "%s version %s (DMA %u)\n",
2323 serial_name, serial_version, dma);
2327 * This routine is called by espserial_init() to initialize a specific serial
2330 static _INLINE_ int autoconfig(struct esp_struct * info)
2332 int port_detected = 0;
2333 unsigned long flags;
2335 if (!request_region(info->port, REGION_SIZE, "esp serial"))
2338 save_flags(flags); cli();
2341 * Check for ESP card
2344 if (serial_in(info, UART_ESI_BASE) == 0xf3) {
2345 serial_out(info, UART_ESI_CMD1, 0x00);
2346 serial_out(info, UART_ESI_CMD1, 0x01);
2348 if ((serial_in(info, UART_ESI_STAT2) & 0x70) == 0x20) {
2352 serial_out(info, UART_ESI_CMD1, 0x02);
2354 if (serial_in(info, UART_ESI_STAT1) & 0x01)
2361 /* put card in enhanced mode */
2362 /* this prevents access through */
2363 /* the "old" IO ports */
2364 esp_basic_init(info);
2367 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2368 serial_out(info, UART_ESI_CMD2, UART_MCR);
2369 serial_out(info, UART_ESI_CMD2, 0x00);
2373 release_region(info->port, REGION_SIZE);
2375 restore_flags(flags);
2376 return (port_detected);
2379 static struct tty_operations esp_ops = {
2383 .put_char = rs_put_char,
2384 .flush_chars = rs_flush_chars,
2385 .write_room = rs_write_room,
2386 .chars_in_buffer = rs_chars_in_buffer,
2387 .flush_buffer = rs_flush_buffer,
2389 .throttle = rs_throttle,
2390 .unthrottle = rs_unthrottle,
2391 .set_termios = rs_set_termios,
2394 .hangup = esp_hangup,
2395 .break_ctl = esp_break,
2396 .wait_until_sent = rs_wait_until_sent,
2397 .tiocmget = esp_tiocmget,
2398 .tiocmset = esp_tiocmset,
2402 * The serial driver boot-time initialization code!
2404 static int __init espserial_init(void)
2407 struct esp_struct * info;
2408 struct esp_struct *last_primary = NULL;
2409 int esp[] = {0x100,0x140,0x180,0x200,0x240,0x280,0x300,0x380};
2411 esp_driver = alloc_tty_driver(NR_PORTS);
2415 for (i = 0; i < NR_PRIMARY; i++) {
2417 if ((irq[i] < 2) || (irq[i] > 15) || (irq[i] == 6) ||
2418 (irq[i] == 8) || (irq[i] == 13))
2420 else if (irq[i] == 2)
2425 if ((dma != 1) && (dma != 3))
2428 if ((rx_trigger < 1) || (rx_trigger > 1023))
2431 if ((tx_trigger < 1) || (tx_trigger > 1023))
2434 if ((flow_off < 1) || (flow_off > 1023))
2437 if ((flow_on < 1) || (flow_on > 1023))
2440 if ((rx_timeout < 0) || (rx_timeout > 255))
2443 if (flow_on >= flow_off)
2444 flow_on = flow_off - 1;
2446 show_serial_version();
2448 /* Initialize the tty_driver structure */
2450 esp_driver->owner = THIS_MODULE;
2451 esp_driver->name = "ttyP";
2452 esp_driver->devfs_name = "tts/P";
2453 esp_driver->major = ESP_IN_MAJOR;
2454 esp_driver->minor_start = 0;
2455 esp_driver->type = TTY_DRIVER_TYPE_SERIAL;
2456 esp_driver->subtype = SERIAL_TYPE_NORMAL;
2457 esp_driver->init_termios = tty_std_termios;
2458 esp_driver->init_termios.c_cflag =
2459 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2460 esp_driver->flags = TTY_DRIVER_REAL_RAW;
2461 tty_set_operations(esp_driver, &esp_ops);
2462 if (tty_register_driver(esp_driver))
2464 printk(KERN_ERR "Couldn't register esp serial driver");
2465 put_tty_driver(esp_driver);
2469 info = kmalloc(sizeof(struct esp_struct), GFP_KERNEL);
2473 printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
2474 tty_unregister_driver(esp_driver);
2475 put_tty_driver(esp_driver);
2479 memset((void *)info, 0, sizeof(struct esp_struct));
2480 /* rx_trigger, tx_trigger are needed by autoconfig */
2481 info->config.rx_trigger = rx_trigger;
2482 info->config.tx_trigger = tx_trigger;
2488 info->port = esp[i] + offset;
2490 info->line = (i * 8) + (offset / 8);
2492 if (!autoconfig(info)) {
2498 info->custom_divisor = (divisor[i] >> (offset / 2)) & 0xf;
2499 info->flags = STD_COM_FLAGS;
2500 if (info->custom_divisor)
2501 info->flags |= ASYNC_SPD_CUST;
2502 info->magic = ESP_MAGIC;
2503 info->close_delay = 5*HZ/10;
2504 info->closing_wait = 30*HZ;
2505 INIT_WORK(&info->tqueue, do_softint, info);
2506 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
2507 info->config.rx_timeout = rx_timeout;
2508 info->config.flow_on = flow_on;
2509 info->config.flow_off = flow_off;
2510 info->config.pio_threshold = pio_threshold;
2511 info->next_port = ports;
2512 init_waitqueue_head(&info->open_wait);
2513 init_waitqueue_head(&info->close_wait);
2514 init_waitqueue_head(&info->delta_msr_wait);
2515 init_waitqueue_head(&info->break_wait);
2517 printk(KERN_INFO "ttyP%d at 0x%04x (irq = %d) is an ESP ",
2518 info->line, info->port, info->irq);
2520 if (info->line % 8) {
2521 printk("secondary port\n");
2522 /* 8 port cards can't do DMA */
2523 info->stat_flags |= ESP_STAT_NEVER_DMA;
2526 last_primary->stat_flags |= ESP_STAT_NEVER_DMA;
2528 printk("primary port\n");
2529 last_primary = info;
2534 info->stat_flags |= ESP_STAT_NEVER_DMA;
2536 info = kmalloc(sizeof(struct esp_struct), GFP_KERNEL);
2539 printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
2541 /* allow use of the already detected ports */
2545 memset((void *)info, 0, sizeof(struct esp_struct));
2546 /* rx_trigger, tx_trigger are needed by autoconfig */
2547 info->config.rx_trigger = rx_trigger;
2548 info->config.tx_trigger = tx_trigger;
2556 } while (i < NR_PRIMARY);
2558 /* free the last port memory allocation */
2564 static void __exit espserial_exit(void)
2566 unsigned long flags;
2568 struct esp_struct *temp_async;
2569 struct esp_pio_buffer *pio_buf;
2571 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2574 if ((e1 = tty_unregister_driver(esp_driver)))
2575 printk("SERIAL: failed to unregister serial driver (%d)\n",
2577 restore_flags(flags);
2578 put_tty_driver(esp_driver);
2582 release_region(ports->port, REGION_SIZE);
2584 temp_async = ports->next_port;
2590 free_pages((unsigned long)dma_buffer,
2591 get_order(DMA_BUFFER_SZ));
2594 free_page((unsigned long)tmp_buf);
2596 while (free_pio_buf) {
2597 pio_buf = free_pio_buf->next;
2598 kfree(free_pio_buf);
2599 free_pio_buf = pio_buf;
2603 module_init(espserial_init);
2604 module_exit(espserial_exit);