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);
1047 info->xmit_buf = NULL;
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 __user *retinfo)
1440 struct serial_struct tmp;
1442 memset(&tmp, 0, sizeof(tmp));
1443 tmp.type = PORT_16550A;
1444 tmp.line = info->line;
1445 tmp.port = info->port;
1446 tmp.irq = info->irq;
1447 tmp.flags = info->flags;
1448 tmp.xmit_fifo_size = 1024;
1449 tmp.baud_base = BASE_BAUD;
1450 tmp.close_delay = info->close_delay;
1451 tmp.closing_wait = info->closing_wait;
1452 tmp.custom_divisor = info->custom_divisor;
1454 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1459 static int get_esp_config(struct esp_struct * info,
1460 struct hayes_esp_config __user *retinfo)
1462 struct hayes_esp_config tmp;
1467 memset(&tmp, 0, sizeof(tmp));
1468 tmp.rx_timeout = info->config.rx_timeout;
1469 tmp.rx_trigger = info->config.rx_trigger;
1470 tmp.tx_trigger = info->config.tx_trigger;
1471 tmp.flow_off = info->config.flow_off;
1472 tmp.flow_on = info->config.flow_on;
1473 tmp.pio_threshold = info->config.pio_threshold;
1474 tmp.dma_channel = (info->stat_flags & ESP_STAT_NEVER_DMA ? 0 : dma);
1476 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
1479 static int set_serial_info(struct esp_struct * info,
1480 struct serial_struct __user *new_info)
1482 struct serial_struct new_serial;
1483 struct esp_struct old_info;
1484 unsigned int change_irq;
1486 struct esp_struct *current_async;
1488 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1492 if ((new_serial.type != PORT_16550A) ||
1493 (new_serial.hub6) ||
1494 (info->port != new_serial.port) ||
1495 (new_serial.baud_base != BASE_BAUD) ||
1496 (new_serial.irq > 15) ||
1497 (new_serial.irq < 2) ||
1498 (new_serial.irq == 6) ||
1499 (new_serial.irq == 8) ||
1500 (new_serial.irq == 13))
1503 change_irq = new_serial.irq != info->irq;
1505 if (change_irq && (info->line % 8))
1508 if (!capable(CAP_SYS_ADMIN)) {
1510 (new_serial.close_delay != info->close_delay) ||
1511 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1512 (info->flags & ~ASYNC_USR_MASK)))
1514 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1515 (new_serial.flags & ASYNC_USR_MASK));
1516 info->custom_divisor = new_serial.custom_divisor;
1518 if (new_serial.irq == 2)
1522 current_async = ports;
1524 while (current_async) {
1525 if ((current_async->line >= info->line) &&
1526 (current_async->line < (info->line + 8))) {
1527 if (current_async == info) {
1528 if (current_async->count > 1)
1530 } else if (current_async->count)
1534 current_async = current_async->next_port;
1539 * OK, past this point, all the error checking has been done.
1540 * At this point, we start making changes.....
1543 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1544 (new_serial.flags & ASYNC_FLAGS));
1545 info->custom_divisor = new_serial.custom_divisor;
1546 info->close_delay = new_serial.close_delay * HZ/100;
1547 info->closing_wait = new_serial.closing_wait * HZ/100;
1551 * We need to shutdown the serial port at the old
1552 * port/irq combination.
1556 current_async = ports;
1558 while (current_async) {
1559 if ((current_async->line >= info->line) &&
1560 (current_async->line < (info->line + 8)))
1561 current_async->irq = new_serial.irq;
1563 current_async = current_async->next_port;
1566 serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
1568 serial_out(info, UART_ESI_CMD2, 0x02);
1570 serial_out(info, UART_ESI_CMD2, info->irq);
1574 if (info->flags & ASYNC_INITIALIZED) {
1575 if (((old_info.flags & ASYNC_SPD_MASK) !=
1576 (info->flags & ASYNC_SPD_MASK)) ||
1577 (old_info.custom_divisor != info->custom_divisor)) {
1578 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1579 info->tty->alt_speed = 57600;
1580 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1581 info->tty->alt_speed = 115200;
1582 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1583 info->tty->alt_speed = 230400;
1584 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1585 info->tty->alt_speed = 460800;
1589 retval = startup(info);
1594 static int set_esp_config(struct esp_struct * info,
1595 struct hayes_esp_config __user * new_info)
1597 struct hayes_esp_config new_config;
1598 unsigned int change_dma;
1600 struct esp_struct *current_async;
1602 /* Perhaps a non-sysadmin user should be able to do some of these */
1603 /* operations. I haven't decided yet. */
1605 if (!capable(CAP_SYS_ADMIN))
1608 if (copy_from_user(&new_config, new_info, sizeof(new_config)))
1611 if ((new_config.flow_on >= new_config.flow_off) ||
1612 (new_config.rx_trigger < 1) ||
1613 (new_config.tx_trigger < 1) ||
1614 (new_config.flow_off < 1) ||
1615 (new_config.flow_on < 1) ||
1616 (new_config.rx_trigger > 1023) ||
1617 (new_config.tx_trigger > 1023) ||
1618 (new_config.flow_off > 1023) ||
1619 (new_config.flow_on > 1023) ||
1620 (new_config.pio_threshold < 0) ||
1621 (new_config.pio_threshold > 1024))
1624 if ((new_config.dma_channel != 1) && (new_config.dma_channel != 3))
1625 new_config.dma_channel = 0;
1627 if (info->stat_flags & ESP_STAT_NEVER_DMA)
1628 change_dma = new_config.dma_channel;
1630 change_dma = (new_config.dma_channel != dma);
1633 if (new_config.dma_channel) {
1634 /* PIO mode to DMA mode transition OR */
1635 /* change current DMA channel */
1637 current_async = ports;
1639 while (current_async) {
1640 if (current_async == info) {
1641 if (current_async->count > 1)
1643 } else if (current_async->count)
1647 current_async->next_port;
1651 dma = new_config.dma_channel;
1652 info->stat_flags &= ~ESP_STAT_NEVER_DMA;
1654 /* all ports must use the same DMA channel */
1656 current_async = ports;
1658 while (current_async) {
1659 esp_basic_init(current_async);
1660 current_async = current_async->next_port;
1663 /* DMA mode to PIO mode only */
1665 if (info->count > 1)
1669 info->stat_flags |= ESP_STAT_NEVER_DMA;
1670 esp_basic_init(info);
1674 info->config.pio_threshold = new_config.pio_threshold;
1676 if ((new_config.flow_off != info->config.flow_off) ||
1677 (new_config.flow_on != info->config.flow_on)) {
1678 unsigned long flags;
1680 info->config.flow_off = new_config.flow_off;
1681 info->config.flow_on = new_config.flow_on;
1682 save_flags(flags); cli();
1683 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1684 serial_out(info, UART_ESI_CMD2, new_config.flow_off >> 8);
1685 serial_out(info, UART_ESI_CMD2, new_config.flow_off);
1686 serial_out(info, UART_ESI_CMD2, new_config.flow_on >> 8);
1687 serial_out(info, UART_ESI_CMD2, new_config.flow_on);
1688 restore_flags(flags);
1691 if ((new_config.rx_trigger != info->config.rx_trigger) ||
1692 (new_config.tx_trigger != info->config.tx_trigger)) {
1693 unsigned long flags;
1695 info->config.rx_trigger = new_config.rx_trigger;
1696 info->config.tx_trigger = new_config.tx_trigger;
1697 save_flags(flags); cli();
1698 serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
1699 serial_out(info, UART_ESI_CMD2,
1700 new_config.rx_trigger >> 8);
1701 serial_out(info, UART_ESI_CMD2, new_config.rx_trigger);
1702 serial_out(info, UART_ESI_CMD2,
1703 new_config.tx_trigger >> 8);
1704 serial_out(info, UART_ESI_CMD2, new_config.tx_trigger);
1705 restore_flags(flags);
1708 if (new_config.rx_timeout != info->config.rx_timeout) {
1709 unsigned long flags;
1711 info->config.rx_timeout = new_config.rx_timeout;
1712 save_flags(flags); cli();
1714 if (info->IER & UART_IER_RDI) {
1715 serial_out(info, UART_ESI_CMD1,
1716 ESI_SET_RX_TIMEOUT);
1717 serial_out(info, UART_ESI_CMD2,
1718 new_config.rx_timeout);
1721 restore_flags(flags);
1724 if (!(info->flags & ASYNC_INITIALIZED))
1725 retval = startup(info);
1731 * get_lsr_info - get line status register info
1733 * Purpose: Let user call ioctl() to get info when the UART physically
1734 * is emptied. On bus types like RS485, the transmitter must
1735 * release the bus after transmitting. This must be done when
1736 * the transmit shift register is empty, not be done when the
1737 * transmit holding register is empty. This functionality
1738 * allows an RS485 driver to be written in user space.
1740 static int get_lsr_info(struct esp_struct * info, unsigned int __user *value)
1742 unsigned char status;
1743 unsigned int result;
1746 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1747 status = serial_in(info, UART_ESI_STAT1);
1749 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1750 return put_user(result,value);
1754 static int esp_tiocmget(struct tty_struct *tty, struct file *file)
1756 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1757 unsigned char control, status;
1759 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1761 if (tty->flags & (1 << TTY_IO_ERROR))
1764 control = info->MCR;
1766 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1767 status = serial_in(info, UART_ESI_STAT2);
1769 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1770 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1771 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1772 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1773 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1774 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1777 static int esp_tiocmset(struct tty_struct *tty, struct file *file,
1778 unsigned int set, unsigned int clear)
1780 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1782 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1784 if (tty->flags & (1 << TTY_IO_ERROR))
1789 if (set & TIOCM_RTS)
1790 info->MCR |= UART_MCR_RTS;
1791 if (set & TIOCM_DTR)
1792 info->MCR |= UART_MCR_DTR;
1794 if (clear & TIOCM_RTS)
1795 info->MCR &= ~UART_MCR_RTS;
1796 if (clear & TIOCM_DTR)
1797 info->MCR &= ~UART_MCR_DTR;
1799 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1800 serial_out(info, UART_ESI_CMD2, UART_MCR);
1801 serial_out(info, UART_ESI_CMD2, info->MCR);
1807 * rs_break() --- routine which turns the break handling on or off
1809 static void esp_break(struct tty_struct *tty, int break_state)
1811 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1812 unsigned long flags;
1814 if (serial_paranoia_check(info, tty->name, "esp_break"))
1817 save_flags(flags); cli();
1818 if (break_state == -1) {
1819 serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1820 serial_out(info, UART_ESI_CMD2, 0x01);
1822 interruptible_sleep_on(&info->break_wait);
1824 serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1825 serial_out(info, UART_ESI_CMD2, 0x00);
1827 restore_flags(flags);
1830 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1831 unsigned int cmd, unsigned long arg)
1833 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1834 struct async_icount cprev, cnow; /* kernel counter temps */
1835 struct serial_icounter_struct __user *p_cuser; /* user space */
1836 void __user *argp = (void __user *)arg;
1838 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1841 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1842 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1843 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT) &&
1844 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT) &&
1845 (cmd != TIOCGHAYESESP) && (cmd != TIOCSHAYESESP)) {
1846 if (tty->flags & (1 << TTY_IO_ERROR))
1852 return get_serial_info(info, argp);
1854 return set_serial_info(info, argp);
1856 /* do not reconfigure after initial configuration */
1860 return put_user(0L, (unsigned long __user *)argp);
1862 case TIOCSERGETLSR: /* Get line status register */
1863 return get_lsr_info(info, argp);
1866 if (!capable(CAP_SYS_ADMIN))
1871 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1872 * - mask passed in arg for lines of interest
1873 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1874 * Caller should use TIOCGICOUNT to see which one it was
1878 cprev = info->icount; /* note the counters on entry */
1881 interruptible_sleep_on(&info->delta_msr_wait);
1882 /* see if a signal did it */
1883 if (signal_pending(current))
1884 return -ERESTARTSYS;
1886 cnow = info->icount; /* atomic copy */
1888 if (cnow.rng == cprev.rng &&
1889 cnow.dsr == cprev.dsr &&
1890 cnow.dcd == cprev.dcd &&
1891 cnow.cts == cprev.cts)
1892 return -EIO; /* no change => error */
1893 if (((arg & TIOCM_RNG) &&
1894 (cnow.rng != cprev.rng)) ||
1895 ((arg & TIOCM_DSR) &&
1896 (cnow.dsr != cprev.dsr)) ||
1897 ((arg & TIOCM_CD) &&
1898 (cnow.dcd != cprev.dcd)) ||
1899 ((arg & TIOCM_CTS) &&
1900 (cnow.cts != cprev.cts)) ) {
1908 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1909 * Return: write counters to the user passed counter struct
1910 * NB: both 1->0 and 0->1 transitions are counted except for
1911 * RI where only 0->1 is counted.
1915 cnow = info->icount;
1918 if (put_user(cnow.cts, &p_cuser->cts) ||
1919 put_user(cnow.dsr, &p_cuser->dsr) ||
1920 put_user(cnow.rng, &p_cuser->rng) ||
1921 put_user(cnow.dcd, &p_cuser->dcd))
1926 return get_esp_config(info, argp);
1928 return set_esp_config(info, argp);
1931 return -ENOIOCTLCMD;
1936 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1938 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1940 if ( (tty->termios->c_cflag == old_termios->c_cflag)
1941 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
1942 == RELEVANT_IFLAG(old_termios->c_iflag)))
1947 /* Handle transition to B0 status */
1948 if ((old_termios->c_cflag & CBAUD) &&
1949 !(tty->termios->c_cflag & CBAUD)) {
1950 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1952 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1953 serial_out(info, UART_ESI_CMD2, UART_MCR);
1954 serial_out(info, UART_ESI_CMD2, info->MCR);
1958 /* Handle transition away from B0 status */
1959 if (!(old_termios->c_cflag & CBAUD) &&
1960 (tty->termios->c_cflag & CBAUD)) {
1961 info->MCR |= (UART_MCR_DTR | UART_MCR_RTS);
1963 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1964 serial_out(info, UART_ESI_CMD2, UART_MCR);
1965 serial_out(info, UART_ESI_CMD2, info->MCR);
1969 /* Handle turning of CRTSCTS */
1970 if ((old_termios->c_cflag & CRTSCTS) &&
1971 !(tty->termios->c_cflag & CRTSCTS)) {
1977 * No need to wake up processes in open wait, since they
1978 * sample the CLOCAL flag once, and don't recheck it.
1979 * XXX It's not clear whether the current behavior is correct
1980 * or not. Hence, this may change.....
1982 if (!(old_termios->c_cflag & CLOCAL) &&
1983 (tty->termios->c_cflag & CLOCAL))
1984 wake_up_interruptible(&info->open_wait);
1989 * ------------------------------------------------------------
1992 * This routine is called when the serial port gets closed. First, we
1993 * wait for the last remaining data to be sent. Then, we unlink its
1994 * async structure from the interrupt chain if necessary, and we free
1995 * that IRQ if nothing is left in the chain.
1996 * ------------------------------------------------------------
1998 static void rs_close(struct tty_struct *tty, struct file * filp)
2000 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
2001 unsigned long flags;
2003 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
2006 save_flags(flags); cli();
2008 if (tty_hung_up_p(filp)) {
2009 DBG_CNT("before DEC-hung");
2013 #ifdef SERIAL_DEBUG_OPEN
2014 printk("rs_close ttys%d, count = %d\n", info->line, info->count);
2016 if ((tty->count == 1) && (info->count != 1)) {
2018 * Uh, oh. tty->count is 1, which means that the tty
2019 * structure will be freed. Info->count should always
2020 * be one in these conditions. If it's greater than
2021 * one, we've got real problems, since it means the
2022 * serial port won't be shutdown.
2024 printk("rs_close: bad serial port count; tty->count is 1, "
2025 "info->count is %d\n", info->count);
2028 if (--info->count < 0) {
2029 printk("rs_close: bad serial port count for ttys%d: %d\n",
2030 info->line, info->count);
2034 DBG_CNT("before DEC-2");
2037 info->flags |= ASYNC_CLOSING;
2039 * Now we wait for the transmit buffer to clear; and we notify
2040 * the line discipline to only process XON/XOFF characters.
2043 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
2044 tty_wait_until_sent(tty, info->closing_wait);
2046 * At this point we stop accepting input. To do this, we
2047 * disable the receive line status interrupts, and tell the
2048 * interrupt driver to stop checking the data ready bit in the
2049 * line status register.
2051 /* info->IER &= ~UART_IER_RLSI; */
2052 info->IER &= ~UART_IER_RDI;
2053 info->read_status_mask &= ~UART_LSR_DR;
2054 if (info->flags & ASYNC_INITIALIZED) {
2055 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
2056 serial_out(info, UART_ESI_CMD2, info->IER);
2058 /* disable receive timeout */
2059 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
2060 serial_out(info, UART_ESI_CMD2, 0x00);
2063 * Before we drop DTR, make sure the UART transmitter
2064 * has completely drained; this is especially
2065 * important if there is a transmit FIFO!
2067 rs_wait_until_sent(tty, info->timeout);
2070 if (tty->driver->flush_buffer)
2071 tty->driver->flush_buffer(tty);
2072 if (tty->ldisc.flush_buffer)
2073 tty->ldisc.flush_buffer(tty);
2078 if (info->blocked_open) {
2079 if (info->close_delay) {
2080 set_current_state(TASK_INTERRUPTIBLE);
2081 schedule_timeout(info->close_delay);
2083 wake_up_interruptible(&info->open_wait);
2085 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
2086 wake_up_interruptible(&info->close_wait);
2088 restore_flags(flags);
2091 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
2093 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
2094 unsigned long orig_jiffies, char_time;
2095 unsigned long flags;
2097 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
2100 orig_jiffies = jiffies;
2101 char_time = ((info->timeout - HZ / 50) / 1024) / 5;
2106 save_flags(flags); cli();
2107 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2108 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2110 while ((serial_in(info, UART_ESI_STAT1) != 0x03) ||
2111 (serial_in(info, UART_ESI_STAT2) != 0xff)) {
2112 set_current_state(TASK_INTERRUPTIBLE);
2113 schedule_timeout(char_time);
2115 if (signal_pending(current))
2118 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2121 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2122 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2125 restore_flags(flags);
2126 set_current_state(TASK_RUNNING);
2130 * esp_hangup() --- called by tty_hangup() when a hangup is signaled.
2132 static void esp_hangup(struct tty_struct *tty)
2134 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
2136 if (serial_paranoia_check(info, tty->name, "esp_hangup"))
2139 rs_flush_buffer(tty);
2143 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2145 wake_up_interruptible(&info->open_wait);
2149 * ------------------------------------------------------------
2150 * esp_open() and friends
2151 * ------------------------------------------------------------
2153 static int block_til_ready(struct tty_struct *tty, struct file * filp,
2154 struct esp_struct *info)
2156 DECLARE_WAITQUEUE(wait, current);
2159 unsigned long flags;
2162 * If the device is in the middle of being closed, then block
2163 * until it's done, and then try again.
2165 if (tty_hung_up_p(filp) ||
2166 (info->flags & ASYNC_CLOSING)) {
2167 if (info->flags & ASYNC_CLOSING)
2168 interruptible_sleep_on(&info->close_wait);
2169 #ifdef SERIAL_DO_RESTART
2170 if (info->flags & ASYNC_HUP_NOTIFY)
2173 return -ERESTARTSYS;
2180 * If non-blocking mode is set, or the port is not enabled,
2181 * then make the check up front and then exit.
2183 if ((filp->f_flags & O_NONBLOCK) ||
2184 (tty->flags & (1 << TTY_IO_ERROR))) {
2185 info->flags |= ASYNC_NORMAL_ACTIVE;
2189 if (tty->termios->c_cflag & CLOCAL)
2193 * Block waiting for the carrier detect and the line to become
2194 * free (i.e., not in use by the callout). While we are in
2195 * this loop, info->count is dropped by one, so that
2196 * rs_close() knows when to free things. We restore it upon
2197 * exit, either normal or abnormal.
2200 add_wait_queue(&info->open_wait, &wait);
2201 #ifdef SERIAL_DEBUG_OPEN
2202 printk("block_til_ready before block: ttys%d, count = %d\n",
2203 info->line, info->count);
2207 if (!tty_hung_up_p(filp))
2209 restore_flags(flags);
2210 info->blocked_open++;
2214 if ((tty->termios->c_cflag & CBAUD)) {
2215 unsigned int scratch;
2217 serial_out(info, UART_ESI_CMD1, ESI_READ_UART);
2218 serial_out(info, UART_ESI_CMD2, UART_MCR);
2219 scratch = serial_in(info, UART_ESI_STAT1);
2220 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2221 serial_out(info, UART_ESI_CMD2, UART_MCR);
2222 serial_out(info, UART_ESI_CMD2,
2223 scratch | UART_MCR_DTR | UART_MCR_RTS);
2225 restore_flags(flags);
2226 set_current_state(TASK_INTERRUPTIBLE);
2227 if (tty_hung_up_p(filp) ||
2228 !(info->flags & ASYNC_INITIALIZED)) {
2229 #ifdef SERIAL_DO_RESTART
2230 if (info->flags & ASYNC_HUP_NOTIFY)
2233 retval = -ERESTARTSYS;
2240 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
2241 if (serial_in(info, UART_ESI_STAT2) & UART_MSR_DCD)
2244 if (!(info->flags & ASYNC_CLOSING) &&
2247 if (signal_pending(current)) {
2248 retval = -ERESTARTSYS;
2251 #ifdef SERIAL_DEBUG_OPEN
2252 printk("block_til_ready blocking: ttys%d, count = %d\n",
2253 info->line, info->count);
2257 set_current_state(TASK_RUNNING);
2258 remove_wait_queue(&info->open_wait, &wait);
2259 if (!tty_hung_up_p(filp))
2261 info->blocked_open--;
2262 #ifdef SERIAL_DEBUG_OPEN
2263 printk("block_til_ready after blocking: ttys%d, count = %d\n",
2264 info->line, info->count);
2268 info->flags |= ASYNC_NORMAL_ACTIVE;
2273 * This routine is called whenever a serial port is opened. It
2274 * enables interrupts for a serial port, linking in its async structure into
2275 * the IRQ chain. It also performs the serial-specific
2276 * initialization for the tty structure.
2278 static int esp_open(struct tty_struct *tty, struct file * filp)
2280 struct esp_struct *info;
2284 if ((line < 0) || (line >= NR_PORTS))
2287 /* find the port in the chain */
2291 while (info && (info->line != line))
2292 info = info->next_port;
2295 serial_paranoia_check(info, tty->name, "esp_open");
2299 #ifdef SERIAL_DEBUG_OPEN
2300 printk("esp_open %s, count = %d\n", tty->name, info->count);
2303 tty->driver_data = info;
2307 tmp_buf = (unsigned char *) get_zeroed_page(GFP_KERNEL);
2313 * Start up serial port
2315 retval = startup(info);
2319 retval = block_til_ready(tty, filp, info);
2321 #ifdef SERIAL_DEBUG_OPEN
2322 printk("esp_open returning after block_til_ready with %d\n",
2328 #ifdef SERIAL_DEBUG_OPEN
2329 printk("esp_open %s successful...", tty->name);
2335 * ---------------------------------------------------------------------
2336 * espserial_init() and friends
2338 * espserial_init() is called at boot-time to initialize the serial driver.
2339 * ---------------------------------------------------------------------
2343 * This routine prints out the appropriate serial driver version
2344 * number, and identifies which options were configured into this
2348 static _INLINE_ void show_serial_version(void)
2350 printk(KERN_INFO "%s version %s (DMA %u)\n",
2351 serial_name, serial_version, dma);
2355 * This routine is called by espserial_init() to initialize a specific serial
2358 static _INLINE_ int autoconfig(struct esp_struct * info, int *region_start)
2360 int port_detected = 0;
2361 unsigned long flags;
2363 save_flags(flags); cli();
2366 * Check for ESP card
2369 if (!check_region(info->port, 8) &&
2370 serial_in(info, UART_ESI_BASE) == 0xf3) {
2371 serial_out(info, UART_ESI_CMD1, 0x00);
2372 serial_out(info, UART_ESI_CMD1, 0x01);
2374 if ((serial_in(info, UART_ESI_STAT2) & 0x70) == 0x20) {
2378 serial_out(info, UART_ESI_CMD1, 0x02);
2380 if (serial_in(info, UART_ESI_STAT1) & 0x01)
2386 if (ports && (ports->port == (info->port - 8))) {
2387 release_region(*region_start,
2388 info->port - *region_start);
2390 *region_start = info->port;
2392 if (!request_region(*region_start,
2393 info->port - *region_start + 8,
2396 restore_flags(flags);
2400 /* put card in enhanced mode */
2401 /* this prevents access through */
2402 /* the "old" IO ports */
2403 esp_basic_init(info);
2406 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2407 serial_out(info, UART_ESI_CMD2, UART_MCR);
2408 serial_out(info, UART_ESI_CMD2, 0x00);
2412 restore_flags(flags);
2413 return (port_detected);
2416 static struct tty_operations esp_ops = {
2420 .put_char = rs_put_char,
2421 .flush_chars = rs_flush_chars,
2422 .write_room = rs_write_room,
2423 .chars_in_buffer = rs_chars_in_buffer,
2424 .flush_buffer = rs_flush_buffer,
2426 .throttle = rs_throttle,
2427 .unthrottle = rs_unthrottle,
2428 .set_termios = rs_set_termios,
2431 .hangup = esp_hangup,
2432 .break_ctl = esp_break,
2433 .wait_until_sent = rs_wait_until_sent,
2434 .tiocmget = esp_tiocmget,
2435 .tiocmset = esp_tiocmset,
2439 * The serial driver boot-time initialization code!
2441 int __init espserial_init(void)
2445 struct esp_struct * info;
2446 struct esp_struct *last_primary = NULL;
2447 int esp[] = {0x100,0x140,0x180,0x200,0x240,0x280,0x300,0x380};
2449 esp_driver = alloc_tty_driver(NR_PORTS);
2453 for (i = 0; i < NR_PRIMARY; i++) {
2455 if ((irq[i] < 2) || (irq[i] > 15) || (irq[i] == 6) ||
2456 (irq[i] == 8) || (irq[i] == 13))
2458 else if (irq[i] == 2)
2463 if ((dma != 1) && (dma != 3))
2466 if ((rx_trigger < 1) || (rx_trigger > 1023))
2469 if ((tx_trigger < 1) || (tx_trigger > 1023))
2472 if ((flow_off < 1) || (flow_off > 1023))
2475 if ((flow_on < 1) || (flow_on > 1023))
2478 if ((rx_timeout < 0) || (rx_timeout > 255))
2481 if (flow_on >= flow_off)
2482 flow_on = flow_off - 1;
2484 show_serial_version();
2486 /* Initialize the tty_driver structure */
2488 esp_driver->owner = THIS_MODULE;
2489 esp_driver->name = "ttyP";
2490 esp_driver->devfs_name = "tts/P";
2491 esp_driver->major = ESP_IN_MAJOR;
2492 esp_driver->minor_start = 0;
2493 esp_driver->type = TTY_DRIVER_TYPE_SERIAL;
2494 esp_driver->subtype = SERIAL_TYPE_NORMAL;
2495 esp_driver->init_termios = tty_std_termios;
2496 esp_driver->init_termios.c_cflag =
2497 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2498 esp_driver->flags = TTY_DRIVER_REAL_RAW;
2499 tty_set_operations(esp_driver, &esp_ops);
2500 if (tty_register_driver(esp_driver))
2502 printk(KERN_ERR "Couldn't register esp serial driver");
2503 put_tty_driver(esp_driver);
2507 info = kmalloc(sizeof(struct esp_struct), GFP_KERNEL);
2511 printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
2512 tty_unregister_driver(esp_driver);
2513 put_tty_driver(esp_driver);
2517 memset((void *)info, 0, sizeof(struct esp_struct));
2518 /* rx_trigger, tx_trigger are needed by autoconfig */
2519 info->config.rx_trigger = rx_trigger;
2520 info->config.tx_trigger = tx_trigger;
2526 info->port = esp[i] + offset;
2528 info->line = (i * 8) + (offset / 8);
2530 if (!autoconfig(info, ®ion_start)) {
2536 info->custom_divisor = (divisor[i] >> (offset / 2)) & 0xf;
2537 info->flags = STD_COM_FLAGS;
2538 if (info->custom_divisor)
2539 info->flags |= ASYNC_SPD_CUST;
2540 info->magic = ESP_MAGIC;
2541 info->close_delay = 5*HZ/10;
2542 info->closing_wait = 30*HZ;
2543 INIT_WORK(&info->tqueue, do_softint, info);
2544 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
2545 info->config.rx_timeout = rx_timeout;
2546 info->config.flow_on = flow_on;
2547 info->config.flow_off = flow_off;
2548 info->config.pio_threshold = pio_threshold;
2549 info->next_port = ports;
2550 init_waitqueue_head(&info->open_wait);
2551 init_waitqueue_head(&info->close_wait);
2552 init_waitqueue_head(&info->delta_msr_wait);
2553 init_waitqueue_head(&info->break_wait);
2555 printk(KERN_INFO "ttyP%d at 0x%04x (irq = %d) is an ESP ",
2556 info->line, info->port, info->irq);
2558 if (info->line % 8) {
2559 printk("secondary port\n");
2560 /* 8 port cards can't do DMA */
2561 info->stat_flags |= ESP_STAT_NEVER_DMA;
2564 last_primary->stat_flags |= ESP_STAT_NEVER_DMA;
2566 printk("primary port\n");
2567 last_primary = info;
2572 info->stat_flags |= ESP_STAT_NEVER_DMA;
2574 info = kmalloc(sizeof(struct esp_struct), GFP_KERNEL);
2577 printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
2579 /* allow use of the already detected ports */
2583 memset((void *)info, 0, sizeof(struct esp_struct));
2584 /* rx_trigger, tx_trigger are needed by autoconfig */
2585 info->config.rx_trigger = rx_trigger;
2586 info->config.tx_trigger = tx_trigger;
2594 } while (i < NR_PRIMARY);
2596 /* free the last port memory allocation */
2602 static void __exit espserial_exit(void)
2604 unsigned long flags;
2606 unsigned int region_start, region_end;
2607 struct esp_struct *temp_async;
2608 struct esp_pio_buffer *pio_buf;
2610 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2613 if ((e1 = tty_unregister_driver(esp_driver)))
2614 printk("SERIAL: failed to unregister serial driver (%d)\n",
2616 restore_flags(flags);
2617 put_tty_driver(esp_driver);
2621 region_start = region_end = ports->port;
2624 while (temp_async) {
2625 if ((region_start - temp_async->port) == 8) {
2626 region_start = temp_async->port;
2627 temp_async->port = 0;
2629 } else if ((temp_async->port - region_end)
2631 region_end = temp_async->port;
2632 temp_async->port = 0;
2635 temp_async = temp_async->next_port;
2638 release_region(region_start,
2639 region_end - region_start + 8);
2642 temp_async = ports->next_port;
2648 free_pages((unsigned long)dma_buffer,
2649 get_order(DMA_BUFFER_SZ));
2652 free_page((unsigned long)tmp_buf);
2654 while (free_pio_buf) {
2655 pio_buf = free_pio_buf->next;
2656 kfree(free_pio_buf);
2657 free_pio_buf = pio_buf;
2661 module_init(espserial_init);
2662 module_exit(espserial_exit);