2 * esp.c - driver for Hayes ESP serial cards
4 * --- Notices from serial.c, upon which this driver is based ---
6 * Copyright (C) 1991, 1992 Linus Torvalds
8 * Extensively rewritten by Theodore Ts'o, 8/16/92 -- 9/14/92. Now
9 * much more extensible to support other serial cards based on the
10 * 16450/16550A UART's. Added support for the AST FourPort and the
13 * set_serial_info fixed to set the flags, custom divisor, and uart
14 * type fields. Fix suggested by Michael K. Johnson 12/12/92.
16 * 11/95: TIOCMIWAIT, TIOCGICOUNT by Angelo Haritsis <ah@doc.ic.ac.uk>
18 * 03/96: Modularised by Angelo Haritsis <ah@doc.ic.ac.uk>
20 * rs_set_termios fixed to look also for changes of the input
21 * flags INPCK, BRKINT, PARMRK, IGNPAR and IGNBRK.
22 * Bernd Anhäupl 05/17/96.
24 * --- End of notices from serial.c ---
26 * Support for the ESP serial card by Andrew J. Robinson
27 * <arobinso@nyx.net> (Card detection routine taken from a patch
28 * by Dennis J. Boylan). Patches to allow use with 2.1.x contributed
31 * Most recent changes: (Andrew J. Robinson)
32 * Support for PIO mode. This allows the driver to work properly with
35 * 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>
61 #include <asm/system.h>
63 #include <asm/bitops.h>
66 #include <linux/slab.h>
67 #include <asm/uaccess.h>
69 #include <linux/hayesesp.h>
71 #define NR_PORTS 64 /* maximum number of ports */
72 #define NR_PRIMARY 8 /* maximum number of primary ports */
74 /* The following variables can be set by giving module options */
75 static int irq[NR_PRIMARY]; /* IRQ for each base port */
76 static unsigned int divisor[NR_PRIMARY]; /* custom divisor for each port */
77 static unsigned int dma = ESP_DMA_CHANNEL; /* DMA channel */
78 static unsigned int rx_trigger = ESP_RX_TRIGGER;
79 static unsigned int tx_trigger = ESP_TX_TRIGGER;
80 static unsigned int flow_off = ESP_FLOW_OFF;
81 static unsigned int flow_on = ESP_FLOW_ON;
82 static unsigned int rx_timeout = ESP_RX_TMOUT;
83 static unsigned int pio_threshold = ESP_PIO_THRESHOLD;
85 MODULE_LICENSE("GPL");
87 MODULE_PARM(irq, "1-8i");
88 MODULE_PARM(divisor, "1-8i");
89 MODULE_PARM(dma, "i");
90 MODULE_PARM(rx_trigger, "i");
91 MODULE_PARM(tx_trigger, "i");
92 MODULE_PARM(flow_off, "i");
93 MODULE_PARM(flow_on, "i");
94 MODULE_PARM(rx_timeout, "i");
95 MODULE_PARM(pio_threshold, "i");
99 static char *dma_buffer;
100 static int dma_bytes;
101 static struct esp_pio_buffer *free_pio_buf;
103 #define DMA_BUFFER_SZ 1024
105 #define WAKEUP_CHARS 1024
107 static char serial_name[] __initdata = "ESP serial driver";
108 static char serial_version[] __initdata = "2.2";
110 static struct tty_driver *esp_driver;
112 /* serial subtype definitions */
113 #define SERIAL_TYPE_NORMAL 1
116 * Serial driver configuration section. Here are the various options:
118 * SERIAL_PARANOIA_CHECK
119 * Check the magic number for the esp_structure where
123 #undef SERIAL_PARANOIA_CHECK
124 #define SERIAL_DO_RESTART
126 #undef SERIAL_DEBUG_INTR
127 #undef SERIAL_DEBUG_OPEN
128 #undef SERIAL_DEBUG_FLOW
130 #define _INLINE_ inline
132 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
133 #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
134 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)
154 #define MIN(a,b) ((a) < (b) ? (a) : (b))
158 * tmp_buf is used as a temporary buffer by serial_write. We need to
159 * lock it in case the memcpy_fromfs blocks while swapping in a page,
160 * and some other program tries to do a serial write at the same time.
161 * Since the lock will only come under contention when the system is
162 * swapping and available memory is low, it makes sense to share one
163 * buffer across all the serial ports, since it significantly saves
164 * memory if large numbers of serial ports are open.
166 static unsigned char *tmp_buf;
167 static DECLARE_MUTEX(tmp_buf_sem);
169 static inline int serial_paranoia_check(struct esp_struct *info,
170 char *name, const char *routine)
172 #ifdef SERIAL_PARANOIA_CHECK
173 static const char badmagic[] = KERN_WARNING
174 "Warning: bad magic number for serial struct (%s) in %s\n";
175 static const char badinfo[] = KERN_WARNING
176 "Warning: null esp_struct for (%s) in %s\n";
179 printk(badinfo, name, routine);
182 if (info->magic != ESP_MAGIC) {
183 printk(badmagic, name, routine);
190 static inline unsigned int serial_in(struct esp_struct *info, int offset)
192 return inb(info->port + offset);
195 static inline void serial_out(struct esp_struct *info, int offset,
198 outb(value, info->port+offset);
202 * ------------------------------------------------------------
203 * rs_stop() and rs_start()
205 * This routines are called before setting or resetting tty->stopped.
206 * They enable or disable transmitter interrupts, as necessary.
207 * ------------------------------------------------------------
209 static void rs_stop(struct tty_struct *tty)
211 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
214 if (serial_paranoia_check(info, tty->name, "rs_stop"))
217 save_flags(flags); cli();
218 if (info->IER & UART_IER_THRI) {
219 info->IER &= ~UART_IER_THRI;
220 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
221 serial_out(info, UART_ESI_CMD2, info->IER);
224 restore_flags(flags);
227 static void rs_start(struct tty_struct *tty)
229 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
232 if (serial_paranoia_check(info, tty->name, "rs_start"))
235 save_flags(flags); cli();
236 if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
237 info->IER |= UART_IER_THRI;
238 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
239 serial_out(info, UART_ESI_CMD2, info->IER);
241 restore_flags(flags);
245 * ----------------------------------------------------------------------
247 * Here starts the interrupt handling routines. All of the following
248 * subroutines are declared as inline and are folded into
249 * rs_interrupt(). They were separated out for readability's sake.
251 * Note: rs_interrupt() is a "fast" interrupt, which means that it
252 * runs with interrupts turned off. People who may want to modify
253 * rs_interrupt() should try to keep the interrupt handler as fast as
254 * possible. After you are done making modifications, it is not a bad
257 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
259 * and look at the resulting assemble code in serial.s.
261 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
262 * -----------------------------------------------------------------------
266 * This routine is used by the interrupt handler to schedule
267 * processing in the software interrupt portion of the driver.
269 static _INLINE_ void rs_sched_event(struct esp_struct *info,
272 info->event |= 1 << event;
273 schedule_work(&info->tqueue);
276 static _INLINE_ struct esp_pio_buffer *get_pio_buffer(void)
278 struct esp_pio_buffer *buf;
282 free_pio_buf = buf->next;
284 buf = kmalloc(sizeof(struct esp_pio_buffer), GFP_ATOMIC);
290 static _INLINE_ void release_pio_buffer(struct esp_pio_buffer *buf)
292 buf->next = free_pio_buf;
296 static _INLINE_ void receive_chars_pio(struct esp_struct *info, int num_bytes)
298 struct tty_struct *tty = info->tty;
300 struct esp_pio_buffer *pio_buf;
301 struct esp_pio_buffer *err_buf;
302 unsigned char status_mask;
304 pio_buf = get_pio_buffer();
309 err_buf = get_pio_buffer();
312 release_pio_buffer(pio_buf);
318 status_mask = (info->read_status_mask >> 2) & 0x07;
320 for (i = 0; i < num_bytes - 1; i += 2) {
321 *((unsigned short *)(pio_buf->data + i)) =
322 inw(info->port + UART_ESI_RX);
323 err_buf->data[i] = serial_in(info, UART_ESI_RWS);
324 err_buf->data[i + 1] = (err_buf->data[i] >> 3) & status_mask;
325 err_buf->data[i] &= status_mask;
328 if (num_bytes & 0x0001) {
329 pio_buf->data[num_bytes - 1] = serial_in(info, UART_ESI_RX);
330 err_buf->data[num_bytes - 1] =
331 (serial_in(info, UART_ESI_RWS) >> 3) & status_mask;
336 /* make sure everything is still ok since interrupts were enabled */
340 release_pio_buffer(pio_buf);
341 release_pio_buffer(err_buf);
342 info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
346 status_mask = (info->ignore_status_mask >> 2) & 0x07;
348 for (i = 0; i < num_bytes; i++) {
349 if (!(err_buf->data[i] & status_mask)) {
350 *(tty->flip.char_buf_ptr++) = pio_buf->data[i];
352 if (err_buf->data[i] & 0x04) {
353 *(tty->flip.flag_buf_ptr++) = TTY_BREAK;
355 if (info->flags & ASYNC_SAK)
358 else if (err_buf->data[i] & 0x02)
359 *(tty->flip.flag_buf_ptr++) = TTY_FRAME;
360 else if (err_buf->data[i] & 0x01)
361 *(tty->flip.flag_buf_ptr++) = TTY_PARITY;
363 *(tty->flip.flag_buf_ptr++) = 0;
369 schedule_delayed_work(&tty->flip.work, 1);
371 info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
372 release_pio_buffer(pio_buf);
373 release_pio_buffer(err_buf);
376 static _INLINE_ void receive_chars_dma(struct esp_struct *info, int num_bytes)
379 info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
380 dma_bytes = num_bytes;
381 info->stat_flags |= ESP_STAT_DMA_RX;
383 flags=claim_dma_lock();
386 set_dma_mode(dma, DMA_MODE_READ);
387 set_dma_addr(dma, isa_virt_to_bus(dma_buffer));
388 set_dma_count(dma, dma_bytes);
390 release_dma_lock(flags);
392 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_RX);
395 static _INLINE_ void receive_chars_dma_done(struct esp_struct *info,
398 struct tty_struct *tty = info->tty;
403 flags=claim_dma_lock();
407 info->stat_flags &= ~ESP_STAT_DMA_RX;
408 num_bytes = dma_bytes - get_dma_residue(dma);
409 release_dma_lock(flags);
411 info->icount.rx += num_bytes;
413 memcpy(tty->flip.char_buf_ptr, dma_buffer, num_bytes);
414 tty->flip.char_buf_ptr += num_bytes;
415 tty->flip.count += num_bytes;
416 memset(tty->flip.flag_buf_ptr, 0, num_bytes);
417 tty->flip.flag_buf_ptr += num_bytes;
420 tty->flip.flag_buf_ptr--;
422 status &= (0x1c & info->read_status_mask);
424 if (status & info->ignore_status_mask) {
426 tty->flip.char_buf_ptr--;
427 tty->flip.flag_buf_ptr--;
428 } else if (status & 0x10) {
429 *tty->flip.flag_buf_ptr = TTY_BREAK;
430 (info->icount.brk)++;
431 if (info->flags & ASYNC_SAK)
433 } else if (status & 0x08) {
434 *tty->flip.flag_buf_ptr = TTY_FRAME;
435 (info->icount.frame)++;
437 else if (status & 0x04) {
438 *tty->flip.flag_buf_ptr = TTY_PARITY;
439 (info->icount.parity)++;
442 tty->flip.flag_buf_ptr++;
444 schedule_delayed_work(&tty->flip.work, 1);
447 if (dma_bytes != num_bytes) {
448 num_bytes = dma_bytes - num_bytes;
450 receive_chars_dma(info, num_bytes);
455 static _INLINE_ void transmit_chars_pio(struct esp_struct *info,
459 struct esp_pio_buffer *pio_buf;
461 pio_buf = get_pio_buffer();
466 while (space_avail && info->xmit_cnt) {
467 if (info->xmit_tail + space_avail <= ESP_XMIT_SIZE) {
468 memcpy(pio_buf->data,
469 &(info->xmit_buf[info->xmit_tail]),
472 i = ESP_XMIT_SIZE - info->xmit_tail;
473 memcpy(pio_buf->data,
474 &(info->xmit_buf[info->xmit_tail]), i);
475 memcpy(&(pio_buf->data[i]), info->xmit_buf,
479 info->xmit_cnt -= space_avail;
480 info->xmit_tail = (info->xmit_tail + space_avail) &
485 for (i = 0; i < space_avail - 1; i += 2) {
486 outw(*((unsigned short *)(pio_buf->data + i)),
487 info->port + UART_ESI_TX);
490 if (space_avail & 0x0001)
491 serial_out(info, UART_ESI_TX,
492 pio_buf->data[space_avail - 1]);
496 if (info->xmit_cnt) {
497 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
498 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
499 space_avail = serial_in(info, UART_ESI_STAT1) << 8;
500 space_avail |= serial_in(info, UART_ESI_STAT2);
502 if (space_avail > info->xmit_cnt)
503 space_avail = info->xmit_cnt;
507 if (info->xmit_cnt < WAKEUP_CHARS) {
508 rs_sched_event(info, ESP_EVENT_WRITE_WAKEUP);
510 #ifdef SERIAL_DEBUG_INTR
514 if (info->xmit_cnt <= 0) {
515 info->IER &= ~UART_IER_THRI;
516 serial_out(info, UART_ESI_CMD1,
518 serial_out(info, UART_ESI_CMD2, info->IER);
522 release_pio_buffer(pio_buf);
525 static _INLINE_ void transmit_chars_dma(struct esp_struct *info, int num_bytes)
529 dma_bytes = num_bytes;
531 if (info->xmit_tail + dma_bytes <= ESP_XMIT_SIZE) {
532 memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
535 int i = ESP_XMIT_SIZE - info->xmit_tail;
536 memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
538 memcpy(&(dma_buffer[i]), info->xmit_buf, dma_bytes - i);
541 info->xmit_cnt -= dma_bytes;
542 info->xmit_tail = (info->xmit_tail + dma_bytes) & (ESP_XMIT_SIZE - 1);
544 if (info->xmit_cnt < WAKEUP_CHARS) {
545 rs_sched_event(info, ESP_EVENT_WRITE_WAKEUP);
547 #ifdef SERIAL_DEBUG_INTR
551 if (info->xmit_cnt <= 0) {
552 info->IER &= ~UART_IER_THRI;
553 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
554 serial_out(info, UART_ESI_CMD2, info->IER);
558 info->stat_flags |= ESP_STAT_DMA_TX;
560 flags=claim_dma_lock();
563 set_dma_mode(dma, DMA_MODE_WRITE);
564 set_dma_addr(dma, isa_virt_to_bus(dma_buffer));
565 set_dma_count(dma, dma_bytes);
567 release_dma_lock(flags);
569 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
572 static _INLINE_ void transmit_chars_dma_done(struct esp_struct *info)
578 flags=claim_dma_lock();
582 num_bytes = dma_bytes - get_dma_residue(dma);
583 info->icount.tx += dma_bytes;
584 release_dma_lock(flags);
586 if (dma_bytes != num_bytes) {
587 dma_bytes -= num_bytes;
588 memmove(dma_buffer, dma_buffer + num_bytes, dma_bytes);
590 flags=claim_dma_lock();
593 set_dma_mode(dma, DMA_MODE_WRITE);
594 set_dma_addr(dma, isa_virt_to_bus(dma_buffer));
595 set_dma_count(dma, dma_bytes);
597 release_dma_lock(flags);
599 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
602 info->stat_flags &= ~ESP_STAT_DMA_TX;
606 static _INLINE_ void check_modem_status(struct esp_struct *info)
610 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
611 status = serial_in(info, UART_ESI_STAT2);
613 if (status & UART_MSR_ANY_DELTA) {
614 /* update input line counters */
615 if (status & UART_MSR_TERI)
617 if (status & UART_MSR_DDSR)
619 if (status & UART_MSR_DDCD)
621 if (status & UART_MSR_DCTS)
623 wake_up_interruptible(&info->delta_msr_wait);
626 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
627 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
628 printk("ttys%d CD now %s...", info->line,
629 (status & UART_MSR_DCD) ? "on" : "off");
631 if (status & UART_MSR_DCD)
632 wake_up_interruptible(&info->open_wait);
634 #ifdef SERIAL_DEBUG_OPEN
635 printk("scheduling hangup...");
637 schedule_work(&info->tqueue_hangup);
643 * This is the serial driver's interrupt routine
645 static irqreturn_t rs_interrupt_single(int irq, void *dev_id,
646 struct pt_regs *regs)
648 struct esp_struct * info;
650 unsigned int scratch;
652 #ifdef SERIAL_DEBUG_INTR
653 printk("rs_interrupt_single(%d)...", irq);
655 info = (struct esp_struct *)dev_id;
657 scratch = serial_in(info, UART_ESI_SID);
666 if (scratch & 0x04) { /* error */
667 serial_out(info, UART_ESI_CMD1, ESI_GET_ERR_STAT);
668 err_status = serial_in(info, UART_ESI_STAT1);
669 serial_in(info, UART_ESI_STAT2);
671 if (err_status & 0x01)
672 info->stat_flags |= ESP_STAT_RX_TIMEOUT;
674 if (err_status & 0x20) /* UART status */
675 check_modem_status(info);
677 if (err_status & 0x80) /* Start break */
678 wake_up_interruptible(&info->break_wait);
681 if ((scratch & 0x88) || /* DMA completed or timed out */
682 (err_status & 0x1c) /* receive error */) {
683 if (info->stat_flags & ESP_STAT_DMA_RX)
684 receive_chars_dma_done(info, err_status);
685 else if (info->stat_flags & ESP_STAT_DMA_TX)
686 transmit_chars_dma_done(info);
689 if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
690 ((scratch & 0x01) || (info->stat_flags & ESP_STAT_RX_TIMEOUT)) &&
691 (info->IER & UART_IER_RDI)) {
694 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
695 serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
696 num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
697 num_bytes |= serial_in(info, UART_ESI_STAT2);
699 if (num_bytes > (TTY_FLIPBUF_SIZE - info->tty->flip.count))
700 num_bytes = TTY_FLIPBUF_SIZE - info->tty->flip.count;
704 (info->stat_flags & ESP_STAT_USE_PIO) ||
705 (num_bytes <= info->config.pio_threshold))
706 receive_chars_pio(info, num_bytes);
708 receive_chars_dma(info, num_bytes);
712 if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
713 (scratch & 0x02) && (info->IER & UART_IER_THRI)) {
714 if ((info->xmit_cnt <= 0) || info->tty->stopped) {
715 info->IER &= ~UART_IER_THRI;
716 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
717 serial_out(info, UART_ESI_CMD2, info->IER);
721 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
722 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
723 num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
724 num_bytes |= serial_in(info, UART_ESI_STAT2);
726 if (num_bytes > info->xmit_cnt)
727 num_bytes = info->xmit_cnt;
731 (info->stat_flags & ESP_STAT_USE_PIO) ||
732 (num_bytes <= info->config.pio_threshold))
733 transmit_chars_pio(info, num_bytes);
735 transmit_chars_dma(info, num_bytes);
740 info->last_active = jiffies;
742 #ifdef SERIAL_DEBUG_INTR
750 * -------------------------------------------------------------------
751 * Here ends the serial interrupt routines.
752 * -------------------------------------------------------------------
755 static void do_softint(void *private_)
757 struct esp_struct *info = (struct esp_struct *) private_;
758 struct tty_struct *tty;
764 if (test_and_clear_bit(ESP_EVENT_WRITE_WAKEUP, &info->event)) {
765 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
766 tty->ldisc.write_wakeup)
767 (tty->ldisc.write_wakeup)(tty);
768 wake_up_interruptible(&tty->write_wait);
773 * This routine is called from the scheduler tqueue when the interrupt
774 * routine has signalled that a hangup has occurred. The path of
775 * hangup processing is:
777 * serial interrupt routine -> (scheduler tqueue) ->
778 * do_serial_hangup() -> tty->hangup() -> esp_hangup()
781 static void do_serial_hangup(void *private_)
783 struct esp_struct *info = (struct esp_struct *) private_;
784 struct tty_struct *tty;
792 * ---------------------------------------------------------------
793 * Low level utility subroutines for the serial driver: routines to
794 * figure out the appropriate timeout for an interrupt chain, routines
795 * to initialize and startup a serial port, and routines to shutdown a
796 * serial port. Useful stuff like that.
797 * ---------------------------------------------------------------
800 static _INLINE_ void esp_basic_init(struct esp_struct * info)
802 /* put ESPC in enhanced mode */
803 serial_out(info, UART_ESI_CMD1, ESI_SET_MODE);
805 if (info->stat_flags & ESP_STAT_NEVER_DMA)
806 serial_out(info, UART_ESI_CMD2, 0x01);
808 serial_out(info, UART_ESI_CMD2, 0x31);
810 /* disable interrupts for now */
811 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
812 serial_out(info, UART_ESI_CMD2, 0x00);
814 /* set interrupt and DMA channel */
815 serial_out(info, UART_ESI_CMD1, ESI_SET_IRQ);
817 if (info->stat_flags & ESP_STAT_NEVER_DMA)
818 serial_out(info, UART_ESI_CMD2, 0x01);
820 serial_out(info, UART_ESI_CMD2, (dma << 4) | 0x01);
822 serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
824 if (info->line % 8) /* secondary port */
825 serial_out(info, UART_ESI_CMD2, 0x0d); /* shared */
826 else if (info->irq == 9)
827 serial_out(info, UART_ESI_CMD2, 0x02);
829 serial_out(info, UART_ESI_CMD2, info->irq);
831 /* set error status mask (check this) */
832 serial_out(info, UART_ESI_CMD1, ESI_SET_ERR_MASK);
834 if (info->stat_flags & ESP_STAT_NEVER_DMA)
835 serial_out(info, UART_ESI_CMD2, 0xa1);
837 serial_out(info, UART_ESI_CMD2, 0xbd);
839 serial_out(info, UART_ESI_CMD2, 0x00);
841 /* set DMA timeout */
842 serial_out(info, UART_ESI_CMD1, ESI_SET_DMA_TMOUT);
843 serial_out(info, UART_ESI_CMD2, 0xff);
845 /* set FIFO trigger levels */
846 serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
847 serial_out(info, UART_ESI_CMD2, info->config.rx_trigger >> 8);
848 serial_out(info, UART_ESI_CMD2, info->config.rx_trigger);
849 serial_out(info, UART_ESI_CMD2, info->config.tx_trigger >> 8);
850 serial_out(info, UART_ESI_CMD2, info->config.tx_trigger);
852 /* Set clock scaling and wait states */
853 serial_out(info, UART_ESI_CMD1, ESI_SET_PRESCALAR);
854 serial_out(info, UART_ESI_CMD2, 0x04 | ESPC_SCALE);
856 /* set reinterrupt pacing */
857 serial_out(info, UART_ESI_CMD1, ESI_SET_REINTR);
858 serial_out(info, UART_ESI_CMD2, 0xff);
861 static int startup(struct esp_struct * info)
865 unsigned int num_chars;
867 save_flags(flags); cli();
869 if (info->flags & ASYNC_INITIALIZED)
872 if (!info->xmit_buf) {
873 info->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
879 #ifdef SERIAL_DEBUG_OPEN
880 printk("starting up ttys%d (irq %d)...", info->line, info->irq);
883 /* Flush the RX buffer. Using the ESI flush command may cause */
884 /* wild interrupts, so read all the data instead. */
886 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
887 serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
888 num_chars = serial_in(info, UART_ESI_STAT1) << 8;
889 num_chars |= serial_in(info, UART_ESI_STAT2);
891 while (num_chars > 1) {
892 inw(info->port + UART_ESI_RX);
897 serial_in(info, UART_ESI_RX);
899 /* set receive character timeout */
900 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
901 serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
903 /* clear all flags except the "never DMA" flag */
904 info->stat_flags &= ESP_STAT_NEVER_DMA;
906 if (info->stat_flags & ESP_STAT_NEVER_DMA)
907 info->stat_flags |= ESP_STAT_USE_PIO;
913 retval = request_irq(info->irq, rs_interrupt_single, SA_SHIRQ,
917 if (capable(CAP_SYS_ADMIN)) {
919 set_bit(TTY_IO_ERROR,
926 if (!(info->stat_flags & ESP_STAT_USE_PIO) && !dma_buffer) {
927 dma_buffer = (char *)__get_dma_pages(
928 GFP_KERNEL, get_order(DMA_BUFFER_SZ));
930 /* use PIO mode if DMA buf/chan cannot be allocated */
932 info->stat_flags |= ESP_STAT_USE_PIO;
933 else if (request_dma(dma, "esp serial")) {
934 free_pages((unsigned long)dma_buffer,
935 get_order(DMA_BUFFER_SZ));
937 info->stat_flags |= ESP_STAT_USE_PIO;
942 info->MCR = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
943 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
944 serial_out(info, UART_ESI_CMD2, UART_MCR);
945 serial_out(info, UART_ESI_CMD2, info->MCR);
948 * Finally, enable interrupts
950 /* info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI; */
951 info->IER = UART_IER_RLSI | UART_IER_RDI | UART_IER_DMA_TMOUT |
953 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
954 serial_out(info, UART_ESI_CMD2, info->IER);
957 clear_bit(TTY_IO_ERROR, &info->tty->flags);
958 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
961 * Set up the tty->alt_speed kludge
964 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
965 info->tty->alt_speed = 57600;
966 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
967 info->tty->alt_speed = 115200;
968 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
969 info->tty->alt_speed = 230400;
970 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
971 info->tty->alt_speed = 460800;
975 * set the speed of the serial port
979 info->flags |= ASYNC_INITIALIZED;
981 out: restore_flags(flags);
986 * This routine will shutdown a serial port; interrupts are disabled, and
987 * DTR is dropped if the hangup on close termio flag is on.
989 static void shutdown(struct esp_struct * info)
991 unsigned long flags, f;
993 if (!(info->flags & ASYNC_INITIALIZED))
996 #ifdef SERIAL_DEBUG_OPEN
997 printk("Shutting down serial port %d (irq %d)....", info->line,
1001 save_flags(flags); cli(); /* Disable interrupts */
1004 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1005 * here so the queue might never be waken up
1007 wake_up_interruptible(&info->delta_msr_wait);
1008 wake_up_interruptible(&info->break_wait);
1010 /* stop a DMA transfer on the port being closed */
1012 if (info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) {
1016 release_dma_lock(f);
1024 free_irq(info->irq, info);
1027 struct esp_struct *current_port = ports;
1029 while (current_port) {
1030 if ((current_port != info) &&
1031 (current_port->flags & ASYNC_INITIALIZED))
1034 current_port = current_port->next_port;
1037 if (!current_port) {
1039 free_pages((unsigned long)dma_buffer,
1040 get_order(DMA_BUFFER_SZ));
1045 if (info->xmit_buf) {
1046 free_page((unsigned long) info->xmit_buf);
1051 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1052 serial_out(info, UART_ESI_CMD2, 0x00);
1054 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
1055 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1057 info->MCR &= ~UART_MCR_OUT2;
1058 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1059 serial_out(info, UART_ESI_CMD2, UART_MCR);
1060 serial_out(info, UART_ESI_CMD2, info->MCR);
1063 set_bit(TTY_IO_ERROR, &info->tty->flags);
1065 info->flags &= ~ASYNC_INITIALIZED;
1066 restore_flags(flags);
1070 * This routine is called to set the UART divisor registers to match
1071 * the specified baud rate for a serial port.
1073 static void change_speed(struct esp_struct *info)
1075 unsigned short port;
1077 unsigned cflag,cval;
1079 unsigned char flow1 = 0, flow2 = 0;
1080 unsigned long flags;
1082 if (!info->tty || !info->tty->termios)
1084 cflag = info->tty->termios->c_cflag;
1087 /* byte size and parity */
1088 switch (cflag & CSIZE) {
1089 case CS5: cval = 0x00; bits = 7; break;
1090 case CS6: cval = 0x01; bits = 8; break;
1091 case CS7: cval = 0x02; bits = 9; break;
1092 case CS8: cval = 0x03; bits = 10; break;
1093 default: cval = 0x00; bits = 7; break;
1095 if (cflag & CSTOPB) {
1099 if (cflag & PARENB) {
1100 cval |= UART_LCR_PARITY;
1103 if (!(cflag & PARODD))
1104 cval |= UART_LCR_EPAR;
1107 cval |= UART_LCR_SPAR;
1110 baud = tty_get_baud_rate(info->tty);
1111 if (baud == 38400 &&
1112 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1113 quot = info->custom_divisor;
1116 /* Special case since 134 is really 134.5 */
1117 quot = (2*BASE_BAUD / 269);
1119 quot = BASE_BAUD / baud;
1121 /* If the quotient is ever zero, default to 9600 bps */
1123 quot = BASE_BAUD / 9600;
1125 info->timeout = ((1024 * HZ * bits * quot) / BASE_BAUD) + (HZ / 50);
1127 /* CTS flow control flag and modem status interrupts */
1128 /* info->IER &= ~UART_IER_MSI; */
1129 if (cflag & CRTSCTS) {
1130 info->flags |= ASYNC_CTS_FLOW;
1131 /* info->IER |= UART_IER_MSI; */
1135 info->flags &= ~ASYNC_CTS_FLOW;
1137 info->flags &= ~ASYNC_CHECK_CD;
1139 info->flags |= ASYNC_CHECK_CD;
1140 /* info->IER |= UART_IER_MSI; */
1144 * Set up parity check flag
1146 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1148 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1149 if (I_INPCK(info->tty))
1150 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1151 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1152 info->read_status_mask |= UART_LSR_BI;
1154 info->ignore_status_mask = 0;
1156 /* This should be safe, but for some broken bits of hardware... */
1157 if (I_IGNPAR(info->tty)) {
1158 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1159 info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
1162 if (I_IGNBRK(info->tty)) {
1163 info->ignore_status_mask |= UART_LSR_BI;
1164 info->read_status_mask |= UART_LSR_BI;
1166 * If we're ignore parity and break indicators, ignore
1167 * overruns too. (For real raw support).
1169 if (I_IGNPAR(info->tty)) {
1170 info->ignore_status_mask |= UART_LSR_OE | \
1171 UART_LSR_PE | UART_LSR_FE;
1172 info->read_status_mask |= UART_LSR_OE | \
1173 UART_LSR_PE | UART_LSR_FE;
1177 if (I_IXOFF(info->tty))
1180 save_flags(flags); cli();
1182 serial_out(info, UART_ESI_CMD1, ESI_SET_BAUD);
1183 serial_out(info, UART_ESI_CMD2, quot >> 8);
1184 serial_out(info, UART_ESI_CMD2, quot & 0xff);
1186 /* set data bits, parity, etc. */
1187 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1188 serial_out(info, UART_ESI_CMD2, UART_LCR);
1189 serial_out(info, UART_ESI_CMD2, cval);
1191 /* Enable flow control */
1192 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CNTL);
1193 serial_out(info, UART_ESI_CMD2, flow1);
1194 serial_out(info, UART_ESI_CMD2, flow2);
1196 /* set flow control characters (XON/XOFF only) */
1197 if (I_IXOFF(info->tty)) {
1198 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CHARS);
1199 serial_out(info, UART_ESI_CMD2, START_CHAR(info->tty));
1200 serial_out(info, UART_ESI_CMD2, STOP_CHAR(info->tty));
1201 serial_out(info, UART_ESI_CMD2, 0x10);
1202 serial_out(info, UART_ESI_CMD2, 0x21);
1203 switch (cflag & CSIZE) {
1205 serial_out(info, UART_ESI_CMD2, 0x1f);
1208 serial_out(info, UART_ESI_CMD2, 0x3f);
1212 serial_out(info, UART_ESI_CMD2, 0x7f);
1215 serial_out(info, UART_ESI_CMD2, 0xff);
1220 /* Set high/low water */
1221 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1222 serial_out(info, UART_ESI_CMD2, info->config.flow_off >> 8);
1223 serial_out(info, UART_ESI_CMD2, info->config.flow_off);
1224 serial_out(info, UART_ESI_CMD2, info->config.flow_on >> 8);
1225 serial_out(info, UART_ESI_CMD2, info->config.flow_on);
1227 restore_flags(flags);
1230 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
1232 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1233 unsigned long flags;
1235 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
1238 if (!tty || !info->xmit_buf)
1241 save_flags(flags); cli();
1242 if (info->xmit_cnt >= ESP_XMIT_SIZE - 1) {
1243 restore_flags(flags);
1247 info->xmit_buf[info->xmit_head++] = ch;
1248 info->xmit_head &= ESP_XMIT_SIZE-1;
1250 restore_flags(flags);
1253 static void rs_flush_chars(struct tty_struct *tty)
1255 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1256 unsigned long flags;
1258 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
1261 if (info->xmit_cnt <= 0 || tty->stopped || !info->xmit_buf)
1264 save_flags(flags); cli();
1265 if (!(info->IER & UART_IER_THRI)) {
1266 info->IER |= UART_IER_THRI;
1267 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1268 serial_out(info, UART_ESI_CMD2, info->IER);
1270 restore_flags(flags);
1273 static int rs_write(struct tty_struct * tty, int from_user,
1274 const unsigned char *buf, int count)
1277 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1278 unsigned long flags;
1280 if (serial_paranoia_check(info, tty->name, "rs_write"))
1283 if (!tty || !info->xmit_buf || !tmp_buf)
1290 /* Thanks to R. Wolff for suggesting how to do this with */
1291 /* interrupts enabled */
1294 t = ESP_XMIT_SIZE - info->xmit_cnt - 1;
1299 t = ESP_XMIT_SIZE - info->xmit_head;
1308 c -= copy_from_user(tmp_buf, buf, c);
1316 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1318 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1320 info->xmit_head = (info->xmit_head + c) & (ESP_XMIT_SIZE-1);
1321 info->xmit_cnt += c;
1330 save_flags(flags); cli();
1332 if (info->xmit_cnt && !tty->stopped && !(info->IER & UART_IER_THRI)) {
1333 info->IER |= UART_IER_THRI;
1334 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1335 serial_out(info, UART_ESI_CMD2, info->IER);
1338 restore_flags(flags);
1342 static int rs_write_room(struct tty_struct *tty)
1344 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1347 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
1349 ret = ESP_XMIT_SIZE - info->xmit_cnt - 1;
1355 static int rs_chars_in_buffer(struct tty_struct *tty)
1357 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1359 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
1361 return info->xmit_cnt;
1364 static void rs_flush_buffer(struct tty_struct *tty)
1366 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1368 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
1371 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1373 wake_up_interruptible(&tty->write_wait);
1374 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1375 tty->ldisc.write_wakeup)
1376 (tty->ldisc.write_wakeup)(tty);
1380 * ------------------------------------------------------------
1383 * This routine is called by the upper-layer tty layer to signal that
1384 * incoming characters should be throttled.
1385 * ------------------------------------------------------------
1387 static void rs_throttle(struct tty_struct * tty)
1389 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1390 #ifdef SERIAL_DEBUG_THROTTLE
1393 printk("throttle %s: %d....\n", tty_name(tty, buf),
1394 tty->ldisc.chars_in_buffer(tty));
1397 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
1401 info->IER &= ~UART_IER_RDI;
1402 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1403 serial_out(info, UART_ESI_CMD2, info->IER);
1404 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1405 serial_out(info, UART_ESI_CMD2, 0x00);
1409 static void rs_unthrottle(struct tty_struct * tty)
1411 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1412 #ifdef SERIAL_DEBUG_THROTTLE
1415 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1416 tty->ldisc.chars_in_buffer(tty));
1419 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1423 info->IER |= UART_IER_RDI;
1424 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1425 serial_out(info, UART_ESI_CMD2, info->IER);
1426 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1427 serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
1432 * ------------------------------------------------------------
1433 * rs_ioctl() and friends
1434 * ------------------------------------------------------------
1437 static int get_serial_info(struct esp_struct * info,
1438 struct serial_struct * retinfo)
1440 struct serial_struct tmp;
1444 memset(&tmp, 0, sizeof(tmp));
1445 tmp.type = PORT_16550A;
1446 tmp.line = info->line;
1447 tmp.port = info->port;
1448 tmp.irq = info->irq;
1449 tmp.flags = info->flags;
1450 tmp.xmit_fifo_size = 1024;
1451 tmp.baud_base = BASE_BAUD;
1452 tmp.close_delay = info->close_delay;
1453 tmp.closing_wait = info->closing_wait;
1454 tmp.custom_divisor = info->custom_divisor;
1456 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1461 static int get_esp_config(struct esp_struct * info,
1462 struct hayes_esp_config * retinfo)
1464 struct hayes_esp_config tmp;
1469 memset(&tmp, 0, sizeof(tmp));
1470 tmp.rx_timeout = info->config.rx_timeout;
1471 tmp.rx_trigger = info->config.rx_trigger;
1472 tmp.tx_trigger = info->config.tx_trigger;
1473 tmp.flow_off = info->config.flow_off;
1474 tmp.flow_on = info->config.flow_on;
1475 tmp.pio_threshold = info->config.pio_threshold;
1476 tmp.dma_channel = (info->stat_flags & ESP_STAT_NEVER_DMA ? 0 : dma);
1478 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
1481 static int set_serial_info(struct esp_struct * info,
1482 struct serial_struct * new_info)
1484 struct serial_struct new_serial;
1485 struct esp_struct old_info;
1486 unsigned int change_irq;
1488 struct esp_struct *current_async;
1490 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1494 if ((new_serial.type != PORT_16550A) ||
1495 (new_serial.hub6) ||
1496 (info->port != new_serial.port) ||
1497 (new_serial.baud_base != BASE_BAUD) ||
1498 (new_serial.irq > 15) ||
1499 (new_serial.irq < 2) ||
1500 (new_serial.irq == 6) ||
1501 (new_serial.irq == 8) ||
1502 (new_serial.irq == 13))
1505 change_irq = new_serial.irq != info->irq;
1507 if (change_irq && (info->line % 8))
1510 if (!capable(CAP_SYS_ADMIN)) {
1512 (new_serial.close_delay != info->close_delay) ||
1513 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1514 (info->flags & ~ASYNC_USR_MASK)))
1516 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1517 (new_serial.flags & ASYNC_USR_MASK));
1518 info->custom_divisor = new_serial.custom_divisor;
1520 if (new_serial.irq == 2)
1524 current_async = ports;
1526 while (current_async) {
1527 if ((current_async->line >= info->line) &&
1528 (current_async->line < (info->line + 8))) {
1529 if (current_async == info) {
1530 if (current_async->count > 1)
1532 } else if (current_async->count)
1536 current_async = current_async->next_port;
1541 * OK, past this point, all the error checking has been done.
1542 * At this point, we start making changes.....
1545 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1546 (new_serial.flags & ASYNC_FLAGS));
1547 info->custom_divisor = new_serial.custom_divisor;
1548 info->close_delay = new_serial.close_delay * HZ/100;
1549 info->closing_wait = new_serial.closing_wait * HZ/100;
1553 * We need to shutdown the serial port at the old
1554 * port/irq combination.
1558 current_async = ports;
1560 while (current_async) {
1561 if ((current_async->line >= info->line) &&
1562 (current_async->line < (info->line + 8)))
1563 current_async->irq = new_serial.irq;
1565 current_async = current_async->next_port;
1568 serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
1570 serial_out(info, UART_ESI_CMD2, 0x02);
1572 serial_out(info, UART_ESI_CMD2, info->irq);
1576 if (info->flags & ASYNC_INITIALIZED) {
1577 if (((old_info.flags & ASYNC_SPD_MASK) !=
1578 (info->flags & ASYNC_SPD_MASK)) ||
1579 (old_info.custom_divisor != info->custom_divisor)) {
1580 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1581 info->tty->alt_speed = 57600;
1582 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1583 info->tty->alt_speed = 115200;
1584 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1585 info->tty->alt_speed = 230400;
1586 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1587 info->tty->alt_speed = 460800;
1591 retval = startup(info);
1596 static int set_esp_config(struct esp_struct * info,
1597 struct hayes_esp_config * new_info)
1599 struct hayes_esp_config new_config;
1600 unsigned int change_dma;
1602 struct esp_struct *current_async;
1604 /* Perhaps a non-sysadmin user should be able to do some of these */
1605 /* operations. I haven't decided yet. */
1607 if (!capable(CAP_SYS_ADMIN))
1610 if (copy_from_user(&new_config, new_info, sizeof(new_config)))
1613 if ((new_config.flow_on >= new_config.flow_off) ||
1614 (new_config.rx_trigger < 1) ||
1615 (new_config.tx_trigger < 1) ||
1616 (new_config.flow_off < 1) ||
1617 (new_config.flow_on < 1) ||
1618 (new_config.rx_trigger > 1023) ||
1619 (new_config.tx_trigger > 1023) ||
1620 (new_config.flow_off > 1023) ||
1621 (new_config.flow_on > 1023) ||
1622 (new_config.pio_threshold < 0) ||
1623 (new_config.pio_threshold > 1024))
1626 if ((new_config.dma_channel != 1) && (new_config.dma_channel != 3))
1627 new_config.dma_channel = 0;
1629 if (info->stat_flags & ESP_STAT_NEVER_DMA)
1630 change_dma = new_config.dma_channel;
1632 change_dma = (new_config.dma_channel != dma);
1635 if (new_config.dma_channel) {
1636 /* PIO mode to DMA mode transition OR */
1637 /* change current DMA channel */
1639 current_async = ports;
1641 while (current_async) {
1642 if (current_async == info) {
1643 if (current_async->count > 1)
1645 } else if (current_async->count)
1649 current_async->next_port;
1653 dma = new_config.dma_channel;
1654 info->stat_flags &= ~ESP_STAT_NEVER_DMA;
1656 /* all ports must use the same DMA channel */
1658 current_async = ports;
1660 while (current_async) {
1661 esp_basic_init(current_async);
1662 current_async = current_async->next_port;
1665 /* DMA mode to PIO mode only */
1667 if (info->count > 1)
1671 info->stat_flags |= ESP_STAT_NEVER_DMA;
1672 esp_basic_init(info);
1676 info->config.pio_threshold = new_config.pio_threshold;
1678 if ((new_config.flow_off != info->config.flow_off) ||
1679 (new_config.flow_on != info->config.flow_on)) {
1680 unsigned long flags;
1682 info->config.flow_off = new_config.flow_off;
1683 info->config.flow_on = new_config.flow_on;
1684 save_flags(flags); cli();
1685 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1686 serial_out(info, UART_ESI_CMD2, new_config.flow_off >> 8);
1687 serial_out(info, UART_ESI_CMD2, new_config.flow_off);
1688 serial_out(info, UART_ESI_CMD2, new_config.flow_on >> 8);
1689 serial_out(info, UART_ESI_CMD2, new_config.flow_on);
1690 restore_flags(flags);
1693 if ((new_config.rx_trigger != info->config.rx_trigger) ||
1694 (new_config.tx_trigger != info->config.tx_trigger)) {
1695 unsigned long flags;
1697 info->config.rx_trigger = new_config.rx_trigger;
1698 info->config.tx_trigger = new_config.tx_trigger;
1699 save_flags(flags); cli();
1700 serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
1701 serial_out(info, UART_ESI_CMD2,
1702 new_config.rx_trigger >> 8);
1703 serial_out(info, UART_ESI_CMD2, new_config.rx_trigger);
1704 serial_out(info, UART_ESI_CMD2,
1705 new_config.tx_trigger >> 8);
1706 serial_out(info, UART_ESI_CMD2, new_config.tx_trigger);
1707 restore_flags(flags);
1710 if (new_config.rx_timeout != info->config.rx_timeout) {
1711 unsigned long flags;
1713 info->config.rx_timeout = new_config.rx_timeout;
1714 save_flags(flags); cli();
1716 if (info->IER & UART_IER_RDI) {
1717 serial_out(info, UART_ESI_CMD1,
1718 ESI_SET_RX_TIMEOUT);
1719 serial_out(info, UART_ESI_CMD2,
1720 new_config.rx_timeout);
1723 restore_flags(flags);
1726 if (!(info->flags & ASYNC_INITIALIZED))
1727 retval = startup(info);
1733 * get_lsr_info - get line status register info
1735 * Purpose: Let user call ioctl() to get info when the UART physically
1736 * is emptied. On bus types like RS485, the transmitter must
1737 * release the bus after transmitting. This must be done when
1738 * the transmit shift register is empty, not be done when the
1739 * transmit holding register is empty. This functionality
1740 * allows an RS485 driver to be written in user space.
1742 static int get_lsr_info(struct esp_struct * info, unsigned int *value)
1744 unsigned char status;
1745 unsigned int result;
1748 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1749 status = serial_in(info, UART_ESI_STAT1);
1751 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1752 return put_user(result,value);
1756 static int esp_tiocmget(struct tty_struct *tty, struct file *file)
1758 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1759 unsigned char control, status;
1761 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1763 if (tty->flags & (1 << TTY_IO_ERROR))
1766 control = info->MCR;
1768 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1769 status = serial_in(info, UART_ESI_STAT2);
1771 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1772 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1773 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1774 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1775 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1776 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1779 static int esp_tiocmset(struct tty_struct *tty, struct file *file,
1780 unsigned int set, unsigned int clear)
1782 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1784 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1786 if (tty->flags & (1 << TTY_IO_ERROR))
1791 if (set & TIOCM_RTS)
1792 info->MCR |= UART_MCR_RTS;
1793 if (set & TIOCM_DTR)
1794 info->MCR |= UART_MCR_DTR;
1796 if (clear & TIOCM_RTS)
1797 info->MCR &= ~UART_MCR_RTS;
1798 if (clear & TIOCM_DTR)
1799 info->MCR &= ~UART_MCR_DTR;
1801 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1802 serial_out(info, UART_ESI_CMD2, UART_MCR);
1803 serial_out(info, UART_ESI_CMD2, info->MCR);
1809 * rs_break() --- routine which turns the break handling on or off
1811 static void esp_break(struct tty_struct *tty, int break_state)
1813 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1814 unsigned long flags;
1816 if (serial_paranoia_check(info, tty->name, "esp_break"))
1819 save_flags(flags); cli();
1820 if (break_state == -1) {
1821 serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1822 serial_out(info, UART_ESI_CMD2, 0x01);
1824 interruptible_sleep_on(&info->break_wait);
1826 serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1827 serial_out(info, UART_ESI_CMD2, 0x00);
1829 restore_flags(flags);
1832 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1833 unsigned int cmd, unsigned long arg)
1835 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1836 struct async_icount cprev, cnow; /* kernel counter temps */
1837 struct serial_icounter_struct *p_cuser; /* user space */
1839 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1842 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1843 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1844 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT) &&
1845 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT) &&
1846 (cmd != TIOCGHAYESESP) && (cmd != TIOCSHAYESESP)) {
1847 if (tty->flags & (1 << TTY_IO_ERROR))
1853 return get_serial_info(info,
1854 (struct serial_struct *) arg);
1856 return set_serial_info(info,
1857 (struct serial_struct *) arg);
1859 /* do not reconfigure after initial configuration */
1863 return put_user(0L, (unsigned long *) arg);
1865 case TIOCSERGETLSR: /* Get line status register */
1866 return get_lsr_info(info, (unsigned int *) arg);
1869 if (!capable(CAP_SYS_ADMIN))
1874 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1875 * - mask passed in arg for lines of interest
1876 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1877 * Caller should use TIOCGICOUNT to see which one it was
1881 cprev = info->icount; /* note the counters on entry */
1884 interruptible_sleep_on(&info->delta_msr_wait);
1885 /* see if a signal did it */
1886 if (signal_pending(current))
1887 return -ERESTARTSYS;
1889 cnow = info->icount; /* atomic copy */
1891 if (cnow.rng == cprev.rng &&
1892 cnow.dsr == cprev.dsr &&
1893 cnow.dcd == cprev.dcd &&
1894 cnow.cts == cprev.cts)
1895 return -EIO; /* no change => error */
1896 if (((arg & TIOCM_RNG) &&
1897 (cnow.rng != cprev.rng)) ||
1898 ((arg & TIOCM_DSR) &&
1899 (cnow.dsr != cprev.dsr)) ||
1900 ((arg & TIOCM_CD) &&
1901 (cnow.dcd != cprev.dcd)) ||
1902 ((arg & TIOCM_CTS) &&
1903 (cnow.cts != cprev.cts)) ) {
1911 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1912 * Return: write counters to the user passed counter struct
1913 * NB: both 1->0 and 0->1 transitions are counted except for
1914 * RI where only 0->1 is counted.
1918 cnow = info->icount;
1920 p_cuser = (struct serial_icounter_struct *) arg;
1921 if (put_user(cnow.cts, &p_cuser->cts) ||
1922 put_user(cnow.dsr, &p_cuser->dsr) ||
1923 put_user(cnow.rng, &p_cuser->rng) ||
1924 put_user(cnow.dcd, &p_cuser->dcd))
1929 return (get_esp_config(info, (struct hayes_esp_config *)arg));
1931 return (set_esp_config(info, (struct hayes_esp_config *)arg));
1934 return -ENOIOCTLCMD;
1939 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1941 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1943 if ( (tty->termios->c_cflag == old_termios->c_cflag)
1944 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
1945 == RELEVANT_IFLAG(old_termios->c_iflag)))
1950 /* Handle transition to B0 status */
1951 if ((old_termios->c_cflag & CBAUD) &&
1952 !(tty->termios->c_cflag & CBAUD)) {
1953 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1955 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1956 serial_out(info, UART_ESI_CMD2, UART_MCR);
1957 serial_out(info, UART_ESI_CMD2, info->MCR);
1961 /* Handle transition away from B0 status */
1962 if (!(old_termios->c_cflag & CBAUD) &&
1963 (tty->termios->c_cflag & CBAUD)) {
1964 info->MCR |= (UART_MCR_DTR | UART_MCR_RTS);
1966 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1967 serial_out(info, UART_ESI_CMD2, UART_MCR);
1968 serial_out(info, UART_ESI_CMD2, info->MCR);
1972 /* Handle turning of CRTSCTS */
1973 if ((old_termios->c_cflag & CRTSCTS) &&
1974 !(tty->termios->c_cflag & CRTSCTS)) {
1980 * No need to wake up processes in open wait, since they
1981 * sample the CLOCAL flag once, and don't recheck it.
1982 * XXX It's not clear whether the current behavior is correct
1983 * or not. Hence, this may change.....
1985 if (!(old_termios->c_cflag & CLOCAL) &&
1986 (tty->termios->c_cflag & CLOCAL))
1987 wake_up_interruptible(&info->open_wait);
1992 * ------------------------------------------------------------
1995 * This routine is called when the serial port gets closed. First, we
1996 * wait for the last remaining data to be sent. Then, we unlink its
1997 * async structure from the interrupt chain if necessary, and we free
1998 * that IRQ if nothing is left in the chain.
1999 * ------------------------------------------------------------
2001 static void rs_close(struct tty_struct *tty, struct file * filp)
2003 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
2004 unsigned long flags;
2006 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
2009 save_flags(flags); cli();
2011 if (tty_hung_up_p(filp)) {
2012 DBG_CNT("before DEC-hung");
2016 #ifdef SERIAL_DEBUG_OPEN
2017 printk("rs_close ttys%d, count = %d\n", info->line, info->count);
2019 if ((tty->count == 1) && (info->count != 1)) {
2021 * Uh, oh. tty->count is 1, which means that the tty
2022 * structure will be freed. Info->count should always
2023 * be one in these conditions. If it's greater than
2024 * one, we've got real problems, since it means the
2025 * serial port won't be shutdown.
2027 printk("rs_close: bad serial port count; tty->count is 1, "
2028 "info->count is %d\n", info->count);
2031 if (--info->count < 0) {
2032 printk("rs_close: bad serial port count for ttys%d: %d\n",
2033 info->line, info->count);
2037 DBG_CNT("before DEC-2");
2040 info->flags |= ASYNC_CLOSING;
2042 * Now we wait for the transmit buffer to clear; and we notify
2043 * the line discipline to only process XON/XOFF characters.
2046 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
2047 tty_wait_until_sent(tty, info->closing_wait);
2049 * At this point we stop accepting input. To do this, we
2050 * disable the receive line status interrupts, and tell the
2051 * interrupt driver to stop checking the data ready bit in the
2052 * line status register.
2054 /* info->IER &= ~UART_IER_RLSI; */
2055 info->IER &= ~UART_IER_RDI;
2056 info->read_status_mask &= ~UART_LSR_DR;
2057 if (info->flags & ASYNC_INITIALIZED) {
2058 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
2059 serial_out(info, UART_ESI_CMD2, info->IER);
2061 /* disable receive timeout */
2062 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
2063 serial_out(info, UART_ESI_CMD2, 0x00);
2066 * Before we drop DTR, make sure the UART transmitter
2067 * has completely drained; this is especially
2068 * important if there is a transmit FIFO!
2070 rs_wait_until_sent(tty, info->timeout);
2073 if (tty->driver->flush_buffer)
2074 tty->driver->flush_buffer(tty);
2075 if (tty->ldisc.flush_buffer)
2076 tty->ldisc.flush_buffer(tty);
2081 if (info->blocked_open) {
2082 if (info->close_delay) {
2083 set_current_state(TASK_INTERRUPTIBLE);
2084 schedule_timeout(info->close_delay);
2086 wake_up_interruptible(&info->open_wait);
2088 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
2089 wake_up_interruptible(&info->close_wait);
2091 restore_flags(flags);
2094 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
2096 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
2097 unsigned long orig_jiffies, char_time;
2098 unsigned long flags;
2100 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
2103 orig_jiffies = jiffies;
2104 char_time = ((info->timeout - HZ / 50) / 1024) / 5;
2109 save_flags(flags); cli();
2110 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2111 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2113 while ((serial_in(info, UART_ESI_STAT1) != 0x03) ||
2114 (serial_in(info, UART_ESI_STAT2) != 0xff)) {
2115 set_current_state(TASK_INTERRUPTIBLE);
2116 schedule_timeout(char_time);
2118 if (signal_pending(current))
2121 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2124 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2125 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2128 restore_flags(flags);
2129 set_current_state(TASK_RUNNING);
2133 * esp_hangup() --- called by tty_hangup() when a hangup is signaled.
2135 static void esp_hangup(struct tty_struct *tty)
2137 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
2139 if (serial_paranoia_check(info, tty->name, "esp_hangup"))
2142 rs_flush_buffer(tty);
2146 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2148 wake_up_interruptible(&info->open_wait);
2152 * ------------------------------------------------------------
2153 * esp_open() and friends
2154 * ------------------------------------------------------------
2156 static int block_til_ready(struct tty_struct *tty, struct file * filp,
2157 struct esp_struct *info)
2159 DECLARE_WAITQUEUE(wait, current);
2162 unsigned long flags;
2165 * If the device is in the middle of being closed, then block
2166 * until it's done, and then try again.
2168 if (tty_hung_up_p(filp) ||
2169 (info->flags & ASYNC_CLOSING)) {
2170 if (info->flags & ASYNC_CLOSING)
2171 interruptible_sleep_on(&info->close_wait);
2172 #ifdef SERIAL_DO_RESTART
2173 if (info->flags & ASYNC_HUP_NOTIFY)
2176 return -ERESTARTSYS;
2183 * If non-blocking mode is set, or the port is not enabled,
2184 * then make the check up front and then exit.
2186 if ((filp->f_flags & O_NONBLOCK) ||
2187 (tty->flags & (1 << TTY_IO_ERROR))) {
2188 info->flags |= ASYNC_NORMAL_ACTIVE;
2192 if (tty->termios->c_cflag & CLOCAL)
2196 * Block waiting for the carrier detect and the line to become
2197 * free (i.e., not in use by the callout). While we are in
2198 * this loop, info->count is dropped by one, so that
2199 * rs_close() knows when to free things. We restore it upon
2200 * exit, either normal or abnormal.
2203 add_wait_queue(&info->open_wait, &wait);
2204 #ifdef SERIAL_DEBUG_OPEN
2205 printk("block_til_ready before block: ttys%d, count = %d\n",
2206 info->line, info->count);
2210 if (!tty_hung_up_p(filp))
2212 restore_flags(flags);
2213 info->blocked_open++;
2217 if ((tty->termios->c_cflag & CBAUD)) {
2218 unsigned int scratch;
2220 serial_out(info, UART_ESI_CMD1, ESI_READ_UART);
2221 serial_out(info, UART_ESI_CMD2, UART_MCR);
2222 scratch = serial_in(info, UART_ESI_STAT1);
2223 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2224 serial_out(info, UART_ESI_CMD2, UART_MCR);
2225 serial_out(info, UART_ESI_CMD2,
2226 scratch | UART_MCR_DTR | UART_MCR_RTS);
2228 restore_flags(flags);
2229 set_current_state(TASK_INTERRUPTIBLE);
2230 if (tty_hung_up_p(filp) ||
2231 !(info->flags & ASYNC_INITIALIZED)) {
2232 #ifdef SERIAL_DO_RESTART
2233 if (info->flags & ASYNC_HUP_NOTIFY)
2236 retval = -ERESTARTSYS;
2243 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
2244 if (serial_in(info, UART_ESI_STAT2) & UART_MSR_DCD)
2247 if (!(info->flags & ASYNC_CLOSING) &&
2250 if (signal_pending(current)) {
2251 retval = -ERESTARTSYS;
2254 #ifdef SERIAL_DEBUG_OPEN
2255 printk("block_til_ready blocking: ttys%d, count = %d\n",
2256 info->line, info->count);
2260 set_current_state(TASK_RUNNING);
2261 remove_wait_queue(&info->open_wait, &wait);
2262 if (!tty_hung_up_p(filp))
2264 info->blocked_open--;
2265 #ifdef SERIAL_DEBUG_OPEN
2266 printk("block_til_ready after blocking: ttys%d, count = %d\n",
2267 info->line, info->count);
2271 info->flags |= ASYNC_NORMAL_ACTIVE;
2276 * This routine is called whenever a serial port is opened. It
2277 * enables interrupts for a serial port, linking in its async structure into
2278 * the IRQ chain. It also performs the serial-specific
2279 * initialization for the tty structure.
2281 static int esp_open(struct tty_struct *tty, struct file * filp)
2283 struct esp_struct *info;
2287 if ((line < 0) || (line >= NR_PORTS))
2290 /* find the port in the chain */
2294 while (info && (info->line != line))
2295 info = info->next_port;
2298 serial_paranoia_check(info, tty->name, "esp_open");
2302 #ifdef SERIAL_DEBUG_OPEN
2303 printk("esp_open %s, count = %d\n", tty->name, info->count);
2306 tty->driver_data = info;
2310 tmp_buf = (unsigned char *) get_zeroed_page(GFP_KERNEL);
2316 * Start up serial port
2318 retval = startup(info);
2322 retval = block_til_ready(tty, filp, info);
2324 #ifdef SERIAL_DEBUG_OPEN
2325 printk("esp_open returning after block_til_ready with %d\n",
2331 #ifdef SERIAL_DEBUG_OPEN
2332 printk("esp_open %s successful...", tty->name);
2338 * ---------------------------------------------------------------------
2339 * espserial_init() and friends
2341 * espserial_init() is called at boot-time to initialize the serial driver.
2342 * ---------------------------------------------------------------------
2346 * This routine prints out the appropriate serial driver version
2347 * number, and identifies which options were configured into this
2351 static _INLINE_ void show_serial_version(void)
2353 printk(KERN_INFO "%s version %s (DMA %u)\n",
2354 serial_name, serial_version, dma);
2358 * This routine is called by espserial_init() to initialize a specific serial
2361 static _INLINE_ int autoconfig(struct esp_struct * info, int *region_start)
2363 int port_detected = 0;
2364 unsigned long flags;
2366 save_flags(flags); cli();
2369 * Check for ESP card
2372 if (!check_region(info->port, 8) &&
2373 serial_in(info, UART_ESI_BASE) == 0xf3) {
2374 serial_out(info, UART_ESI_CMD1, 0x00);
2375 serial_out(info, UART_ESI_CMD1, 0x01);
2377 if ((serial_in(info, UART_ESI_STAT2) & 0x70) == 0x20) {
2381 serial_out(info, UART_ESI_CMD1, 0x02);
2383 if (serial_in(info, UART_ESI_STAT1) & 0x01)
2389 if (ports && (ports->port == (info->port - 8))) {
2390 release_region(*region_start,
2391 info->port - *region_start);
2393 *region_start = info->port;
2395 if (!request_region(*region_start,
2396 info->port - *region_start + 8,
2399 restore_flags(flags);
2403 /* put card in enhanced mode */
2404 /* this prevents access through */
2405 /* the "old" IO ports */
2406 esp_basic_init(info);
2409 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2410 serial_out(info, UART_ESI_CMD2, UART_MCR);
2411 serial_out(info, UART_ESI_CMD2, 0x00);
2415 restore_flags(flags);
2416 return (port_detected);
2419 static struct tty_operations esp_ops = {
2423 .put_char = rs_put_char,
2424 .flush_chars = rs_flush_chars,
2425 .write_room = rs_write_room,
2426 .chars_in_buffer = rs_chars_in_buffer,
2427 .flush_buffer = rs_flush_buffer,
2429 .throttle = rs_throttle,
2430 .unthrottle = rs_unthrottle,
2431 .set_termios = rs_set_termios,
2434 .hangup = esp_hangup,
2435 .break_ctl = esp_break,
2436 .wait_until_sent = rs_wait_until_sent,
2437 .tiocmget = esp_tiocmget,
2438 .tiocmset = esp_tiocmset,
2442 * The serial driver boot-time initialization code!
2444 int __init espserial_init(void)
2448 struct esp_struct * info;
2449 struct esp_struct *last_primary = 0;
2450 int esp[] = {0x100,0x140,0x180,0x200,0x240,0x280,0x300,0x380};
2452 esp_driver = alloc_tty_driver(NR_PORTS);
2456 for (i = 0; i < NR_PRIMARY; i++) {
2458 if ((irq[i] < 2) || (irq[i] > 15) || (irq[i] == 6) ||
2459 (irq[i] == 8) || (irq[i] == 13))
2461 else if (irq[i] == 2)
2466 if ((dma != 1) && (dma != 3))
2469 if ((rx_trigger < 1) || (rx_trigger > 1023))
2472 if ((tx_trigger < 1) || (tx_trigger > 1023))
2475 if ((flow_off < 1) || (flow_off > 1023))
2478 if ((flow_on < 1) || (flow_on > 1023))
2481 if ((rx_timeout < 0) || (rx_timeout > 255))
2484 if (flow_on >= flow_off)
2485 flow_on = flow_off - 1;
2487 show_serial_version();
2489 /* Initialize the tty_driver structure */
2491 esp_driver->owner = THIS_MODULE;
2492 esp_driver->name = "ttyP";
2493 esp_driver->devfs_name = "tts/P";
2494 esp_driver->major = ESP_IN_MAJOR;
2495 esp_driver->minor_start = 0;
2496 esp_driver->type = TTY_DRIVER_TYPE_SERIAL;
2497 esp_driver->subtype = SERIAL_TYPE_NORMAL;
2498 esp_driver->init_termios = tty_std_termios;
2499 esp_driver->init_termios.c_cflag =
2500 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2501 esp_driver->flags = TTY_DRIVER_REAL_RAW;
2502 tty_set_operations(esp_driver, &esp_ops);
2503 if (tty_register_driver(esp_driver))
2505 printk(KERN_ERR "Couldn't register esp serial driver");
2506 put_tty_driver(esp_driver);
2510 info = kmalloc(sizeof(struct esp_struct), GFP_KERNEL);
2514 printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
2515 tty_unregister_driver(esp_driver);
2516 put_tty_driver(esp_driver);
2520 memset((void *)info, 0, sizeof(struct esp_struct));
2521 /* rx_trigger, tx_trigger are needed by autoconfig */
2522 info->config.rx_trigger = rx_trigger;
2523 info->config.tx_trigger = tx_trigger;
2529 info->port = esp[i] + offset;
2531 info->line = (i * 8) + (offset / 8);
2533 if (!autoconfig(info, ®ion_start)) {
2539 info->custom_divisor = (divisor[i] >> (offset / 2)) & 0xf;
2540 info->flags = STD_COM_FLAGS;
2541 if (info->custom_divisor)
2542 info->flags |= ASYNC_SPD_CUST;
2543 info->magic = ESP_MAGIC;
2544 info->close_delay = 5*HZ/10;
2545 info->closing_wait = 30*HZ;
2546 INIT_WORK(&info->tqueue, do_softint, info);
2547 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
2548 info->config.rx_timeout = rx_timeout;
2549 info->config.flow_on = flow_on;
2550 info->config.flow_off = flow_off;
2551 info->config.pio_threshold = pio_threshold;
2552 info->next_port = ports;
2553 init_waitqueue_head(&info->open_wait);
2554 init_waitqueue_head(&info->close_wait);
2555 init_waitqueue_head(&info->delta_msr_wait);
2556 init_waitqueue_head(&info->break_wait);
2558 printk(KERN_INFO "ttyP%d at 0x%04x (irq = %d) is an ESP ",
2559 info->line, info->port, info->irq);
2561 if (info->line % 8) {
2562 printk("secondary port\n");
2563 /* 8 port cards can't do DMA */
2564 info->stat_flags |= ESP_STAT_NEVER_DMA;
2567 last_primary->stat_flags |= ESP_STAT_NEVER_DMA;
2569 printk("primary port\n");
2570 last_primary = info;
2575 info->stat_flags |= ESP_STAT_NEVER_DMA;
2577 info = kmalloc(sizeof(struct esp_struct), GFP_KERNEL);
2580 printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
2582 /* allow use of the already detected ports */
2586 memset((void *)info, 0, sizeof(struct esp_struct));
2587 /* rx_trigger, tx_trigger are needed by autoconfig */
2588 info->config.rx_trigger = rx_trigger;
2589 info->config.tx_trigger = tx_trigger;
2597 } while (i < NR_PRIMARY);
2599 /* free the last port memory allocation */
2605 static void __exit espserial_exit(void)
2607 unsigned long flags;
2609 unsigned int region_start, region_end;
2610 struct esp_struct *temp_async;
2611 struct esp_pio_buffer *pio_buf;
2613 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2616 if ((e1 = tty_unregister_driver(esp_driver)))
2617 printk("SERIAL: failed to unregister serial driver (%d)\n",
2619 restore_flags(flags);
2620 put_tty_driver(esp_driver);
2624 region_start = region_end = ports->port;
2627 while (temp_async) {
2628 if ((region_start - temp_async->port) == 8) {
2629 region_start = temp_async->port;
2630 temp_async->port = 0;
2632 } else if ((temp_async->port - region_end)
2634 region_end = temp_async->port;
2635 temp_async->port = 0;
2638 temp_async = temp_async->next_port;
2641 release_region(region_start,
2642 region_end - region_start + 8);
2645 temp_async = ports->next_port;
2651 free_pages((unsigned long)dma_buffer,
2652 get_order(DMA_BUFFER_SZ));
2655 free_page((unsigned long)tmp_buf);
2657 while (free_pio_buf) {
2658 pio_buf = free_pio_buf->next;
2659 kfree(free_pio_buf);
2660 free_pio_buf = pio_buf;
2664 module_init(espserial_init);
2665 module_exit(espserial_exit);