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>
60 #include <linux/delay.h>
61 #include <linux/bitops.h>
63 #include <asm/system.h>
67 #include <linux/slab.h>
68 #include <asm/uaccess.h>
70 #include <linux/hayesesp.h>
72 #define NR_PORTS 64 /* maximum number of ports */
73 #define NR_PRIMARY 8 /* maximum number of primary ports */
74 #define REGION_SIZE 8 /* size of io region to request */
76 /* The following variables can be set by giving module options */
77 static int irq[NR_PRIMARY]; /* IRQ for each base port */
78 static unsigned int divisor[NR_PRIMARY]; /* custom divisor for each port */
79 static unsigned int dma = ESP_DMA_CHANNEL; /* DMA channel */
80 static unsigned int rx_trigger = ESP_RX_TRIGGER;
81 static unsigned int tx_trigger = ESP_TX_TRIGGER;
82 static unsigned int flow_off = ESP_FLOW_OFF;
83 static unsigned int flow_on = ESP_FLOW_ON;
84 static unsigned int rx_timeout = ESP_RX_TMOUT;
85 static unsigned int pio_threshold = ESP_PIO_THRESHOLD;
87 MODULE_LICENSE("GPL");
89 module_param_array(irq, int, NULL, 0);
90 module_param_array(divisor, uint, NULL, 0);
91 module_param(dma, uint, 0);
92 module_param(rx_trigger, uint, 0);
93 module_param(tx_trigger, uint, 0);
94 module_param(flow_off, uint, 0);
95 module_param(flow_on, uint, 0);
96 module_param(rx_timeout, uint, 0);
97 module_param(pio_threshold, uint, 0);
101 static char *dma_buffer;
102 static int dma_bytes;
103 static struct esp_pio_buffer *free_pio_buf;
105 #define DMA_BUFFER_SZ 1024
107 #define WAKEUP_CHARS 1024
109 static char serial_name[] __initdata = "ESP serial driver";
110 static char serial_version[] __initdata = "2.2";
112 static struct tty_driver *esp_driver;
115 * Serial driver configuration section. Here are the various options:
117 * SERIAL_PARANOIA_CHECK
118 * Check the magic number for the esp_structure where
122 #undef SERIAL_PARANOIA_CHECK
123 #define SERIAL_DO_RESTART
125 #undef SERIAL_DEBUG_INTR
126 #undef SERIAL_DEBUG_OPEN
127 #undef SERIAL_DEBUG_FLOW
129 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
130 #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
131 tty->name, (info->flags), serial_driver.refcount,info->count,tty->count,s)
136 static struct esp_struct *ports;
138 static void change_speed(struct esp_struct *info);
139 static void rs_wait_until_sent(struct tty_struct *, int);
142 * The ESP card has a clock rate of 14.7456 MHz (that is, 2**ESPC_SCALE
143 * times the normal 1.8432 Mhz clock of most serial boards).
145 #define BASE_BAUD ((1843200 / 16) * (1 << ESPC_SCALE))
147 /* Standard COM flags (except for COM4, because of the 8514 problem) */
148 #define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
150 static inline int serial_paranoia_check(struct esp_struct *info,
151 char *name, const char *routine)
153 #ifdef SERIAL_PARANOIA_CHECK
154 static const char badmagic[] = KERN_WARNING
155 "Warning: bad magic number for serial struct (%s) in %s\n";
156 static const char badinfo[] = KERN_WARNING
157 "Warning: null esp_struct for (%s) in %s\n";
160 printk(badinfo, name, routine);
163 if (info->magic != ESP_MAGIC) {
164 printk(badmagic, name, routine);
171 static inline unsigned int serial_in(struct esp_struct *info, int offset)
173 return inb(info->port + offset);
176 static inline void serial_out(struct esp_struct *info, int offset,
179 outb(value, info->port+offset);
183 * ------------------------------------------------------------
184 * rs_stop() and rs_start()
186 * This routines are called before setting or resetting tty->stopped.
187 * They enable or disable transmitter interrupts, as necessary.
188 * ------------------------------------------------------------
190 static void rs_stop(struct tty_struct *tty)
192 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
195 if (serial_paranoia_check(info, tty->name, "rs_stop"))
198 spin_lock_irqsave(&info->lock, flags);
199 if (info->IER & UART_IER_THRI) {
200 info->IER &= ~UART_IER_THRI;
201 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
202 serial_out(info, UART_ESI_CMD2, info->IER);
204 spin_unlock_irqrestore(&info->lock, flags);
207 static void rs_start(struct tty_struct *tty)
209 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
212 if (serial_paranoia_check(info, tty->name, "rs_start"))
215 spin_lock_irqsave(&info->lock, flags);
216 if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
217 info->IER |= UART_IER_THRI;
218 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
219 serial_out(info, UART_ESI_CMD2, info->IER);
221 spin_unlock_irqrestore(&info->lock, flags);
225 * ----------------------------------------------------------------------
227 * Here starts the interrupt handling routines. All of the following
228 * subroutines are declared as inline and are folded into
229 * rs_interrupt(). They were separated out for readability's sake.
231 * Note: rs_interrupt() is a "fast" interrupt, which means that it
232 * runs with interrupts turned off. People who may want to modify
233 * rs_interrupt() should try to keep the interrupt handler as fast as
234 * possible. After you are done making modifications, it is not a bad
237 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
239 * and look at the resulting assemble code in serial.s.
241 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
242 * -----------------------------------------------------------------------
245 static DEFINE_SPINLOCK(pio_lock);
247 static inline struct esp_pio_buffer *get_pio_buffer(void)
249 struct esp_pio_buffer *buf;
252 spin_lock_irqsave(&pio_lock, flags);
255 free_pio_buf = buf->next;
257 buf = kmalloc(sizeof(struct esp_pio_buffer), GFP_ATOMIC);
259 spin_unlock_irqrestore(&pio_lock, flags);
263 static inline void release_pio_buffer(struct esp_pio_buffer *buf)
266 spin_lock_irqsave(&pio_lock, flags);
267 buf->next = free_pio_buf;
269 spin_unlock_irqrestore(&pio_lock, flags);
272 static inline void receive_chars_pio(struct esp_struct *info, int num_bytes)
274 struct tty_struct *tty = info->tty;
276 struct esp_pio_buffer *pio_buf;
277 struct esp_pio_buffer *err_buf;
278 unsigned char status_mask;
280 pio_buf = get_pio_buffer();
285 err_buf = get_pio_buffer();
288 release_pio_buffer(pio_buf);
292 status_mask = (info->read_status_mask >> 2) & 0x07;
294 for (i = 0; i < num_bytes - 1; i += 2) {
295 *((unsigned short *)(pio_buf->data + i)) =
296 inw(info->port + UART_ESI_RX);
297 err_buf->data[i] = serial_in(info, UART_ESI_RWS);
298 err_buf->data[i + 1] = (err_buf->data[i] >> 3) & status_mask;
299 err_buf->data[i] &= status_mask;
302 if (num_bytes & 0x0001) {
303 pio_buf->data[num_bytes - 1] = serial_in(info, UART_ESI_RX);
304 err_buf->data[num_bytes - 1] =
305 (serial_in(info, UART_ESI_RWS) >> 3) & status_mask;
308 /* make sure everything is still ok since interrupts were enabled */
312 release_pio_buffer(pio_buf);
313 release_pio_buffer(err_buf);
314 info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
318 status_mask = (info->ignore_status_mask >> 2) & 0x07;
320 for (i = 0; i < num_bytes; i++) {
321 if (!(err_buf->data[i] & status_mask)) {
324 if (err_buf->data[i] & 0x04) {
326 if (info->flags & ASYNC_SAK)
329 else if (err_buf->data[i] & 0x02)
331 else if (err_buf->data[i] & 0x01)
333 tty_insert_flip_char(tty, pio_buf->data[i], flag);
337 tty_schedule_flip(tty);
339 info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
340 release_pio_buffer(pio_buf);
341 release_pio_buffer(err_buf);
344 static inline void receive_chars_dma(struct esp_struct *info, int num_bytes)
347 info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
348 dma_bytes = num_bytes;
349 info->stat_flags |= ESP_STAT_DMA_RX;
351 flags=claim_dma_lock();
354 set_dma_mode(dma, DMA_MODE_READ);
355 set_dma_addr(dma, isa_virt_to_bus(dma_buffer));
356 set_dma_count(dma, dma_bytes);
358 release_dma_lock(flags);
360 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_RX);
363 static inline void receive_chars_dma_done(struct esp_struct *info,
366 struct tty_struct *tty = info->tty;
370 flags=claim_dma_lock();
374 info->stat_flags &= ~ESP_STAT_DMA_RX;
375 num_bytes = dma_bytes - get_dma_residue(dma);
376 release_dma_lock(flags);
378 info->icount.rx += num_bytes;
381 tty_insert_flip_string(tty, dma_buffer, num_bytes - 1);
383 status &= (0x1c & info->read_status_mask);
385 /* Is the status significant or do we throw the last byte ? */
386 if (!(status & info->ignore_status_mask)) {
390 statflag = TTY_BREAK;
391 (info->icount.brk)++;
392 if (info->flags & ASYNC_SAK)
394 } else if (status & 0x08) {
395 statflag = TTY_FRAME;
396 (info->icount.frame)++;
398 else if (status & 0x04) {
399 statflag = TTY_PARITY;
400 (info->icount.parity)++;
402 tty_insert_flip_char(tty, dma_buffer[num_bytes - 1], statflag);
404 tty_schedule_flip(tty);
407 if (dma_bytes != num_bytes) {
408 num_bytes = dma_bytes - num_bytes;
410 receive_chars_dma(info, num_bytes);
415 /* Caller must hold info->lock */
417 static inline void transmit_chars_pio(struct esp_struct *info,
421 struct esp_pio_buffer *pio_buf;
423 pio_buf = get_pio_buffer();
428 while (space_avail && info->xmit_cnt) {
429 if (info->xmit_tail + space_avail <= ESP_XMIT_SIZE) {
430 memcpy(pio_buf->data,
431 &(info->xmit_buf[info->xmit_tail]),
434 i = ESP_XMIT_SIZE - info->xmit_tail;
435 memcpy(pio_buf->data,
436 &(info->xmit_buf[info->xmit_tail]), i);
437 memcpy(&(pio_buf->data[i]), info->xmit_buf,
441 info->xmit_cnt -= space_avail;
442 info->xmit_tail = (info->xmit_tail + space_avail) &
445 for (i = 0; i < space_avail - 1; i += 2) {
446 outw(*((unsigned short *)(pio_buf->data + i)),
447 info->port + UART_ESI_TX);
450 if (space_avail & 0x0001)
451 serial_out(info, UART_ESI_TX,
452 pio_buf->data[space_avail - 1]);
454 if (info->xmit_cnt) {
455 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
456 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
457 space_avail = serial_in(info, UART_ESI_STAT1) << 8;
458 space_avail |= serial_in(info, UART_ESI_STAT2);
460 if (space_avail > info->xmit_cnt)
461 space_avail = info->xmit_cnt;
465 if (info->xmit_cnt < WAKEUP_CHARS) {
467 tty_wakeup(info->tty);
469 #ifdef SERIAL_DEBUG_INTR
473 if (info->xmit_cnt <= 0) {
474 info->IER &= ~UART_IER_THRI;
475 serial_out(info, UART_ESI_CMD1,
477 serial_out(info, UART_ESI_CMD2, info->IER);
481 release_pio_buffer(pio_buf);
484 /* Caller must hold info->lock */
485 static inline void transmit_chars_dma(struct esp_struct *info, int num_bytes)
489 dma_bytes = num_bytes;
491 if (info->xmit_tail + dma_bytes <= ESP_XMIT_SIZE) {
492 memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
495 int i = ESP_XMIT_SIZE - info->xmit_tail;
496 memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
498 memcpy(&(dma_buffer[i]), info->xmit_buf, dma_bytes - i);
501 info->xmit_cnt -= dma_bytes;
502 info->xmit_tail = (info->xmit_tail + dma_bytes) & (ESP_XMIT_SIZE - 1);
504 if (info->xmit_cnt < WAKEUP_CHARS) {
506 tty_wakeup(info->tty);
508 #ifdef SERIAL_DEBUG_INTR
512 if (info->xmit_cnt <= 0) {
513 info->IER &= ~UART_IER_THRI;
514 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
515 serial_out(info, UART_ESI_CMD2, info->IER);
519 info->stat_flags |= ESP_STAT_DMA_TX;
521 flags=claim_dma_lock();
524 set_dma_mode(dma, DMA_MODE_WRITE);
525 set_dma_addr(dma, isa_virt_to_bus(dma_buffer));
526 set_dma_count(dma, dma_bytes);
528 release_dma_lock(flags);
530 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
533 static inline void transmit_chars_dma_done(struct esp_struct *info)
539 flags=claim_dma_lock();
543 num_bytes = dma_bytes - get_dma_residue(dma);
544 info->icount.tx += dma_bytes;
545 release_dma_lock(flags);
547 if (dma_bytes != num_bytes) {
548 dma_bytes -= num_bytes;
549 memmove(dma_buffer, dma_buffer + num_bytes, dma_bytes);
551 flags=claim_dma_lock();
554 set_dma_mode(dma, DMA_MODE_WRITE);
555 set_dma_addr(dma, isa_virt_to_bus(dma_buffer));
556 set_dma_count(dma, dma_bytes);
558 release_dma_lock(flags);
560 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
563 info->stat_flags &= ~ESP_STAT_DMA_TX;
567 static inline void check_modem_status(struct esp_struct *info)
571 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
572 status = serial_in(info, UART_ESI_STAT2);
574 if (status & UART_MSR_ANY_DELTA) {
575 /* update input line counters */
576 if (status & UART_MSR_TERI)
578 if (status & UART_MSR_DDSR)
580 if (status & UART_MSR_DDCD)
582 if (status & UART_MSR_DCTS)
584 wake_up_interruptible(&info->delta_msr_wait);
587 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
588 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
589 printk("ttys%d CD now %s...", info->line,
590 (status & UART_MSR_DCD) ? "on" : "off");
592 if (status & UART_MSR_DCD)
593 wake_up_interruptible(&info->open_wait);
595 #ifdef SERIAL_DEBUG_OPEN
596 printk("scheduling hangup...");
598 tty_hangup(info->tty);
604 * This is the serial driver's interrupt routine
606 static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
608 struct esp_struct * info;
610 unsigned int scratch;
612 #ifdef SERIAL_DEBUG_INTR
613 printk("rs_interrupt_single(%d)...", irq);
615 info = (struct esp_struct *)dev_id;
617 scratch = serial_in(info, UART_ESI_SID);
619 spin_lock(&info->lock);
622 spin_unlock(&info->lock);
626 if (scratch & 0x04) { /* error */
627 serial_out(info, UART_ESI_CMD1, ESI_GET_ERR_STAT);
628 err_status = serial_in(info, UART_ESI_STAT1);
629 serial_in(info, UART_ESI_STAT2);
631 if (err_status & 0x01)
632 info->stat_flags |= ESP_STAT_RX_TIMEOUT;
634 if (err_status & 0x20) /* UART status */
635 check_modem_status(info);
637 if (err_status & 0x80) /* Start break */
638 wake_up_interruptible(&info->break_wait);
641 if ((scratch & 0x88) || /* DMA completed or timed out */
642 (err_status & 0x1c) /* receive error */) {
643 if (info->stat_flags & ESP_STAT_DMA_RX)
644 receive_chars_dma_done(info, err_status);
645 else if (info->stat_flags & ESP_STAT_DMA_TX)
646 transmit_chars_dma_done(info);
649 if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
650 ((scratch & 0x01) || (info->stat_flags & ESP_STAT_RX_TIMEOUT)) &&
651 (info->IER & UART_IER_RDI)) {
654 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
655 serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
656 num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
657 num_bytes |= serial_in(info, UART_ESI_STAT2);
659 num_bytes = tty_buffer_request_room(info->tty, num_bytes);
663 (info->stat_flags & ESP_STAT_USE_PIO) ||
664 (num_bytes <= info->config.pio_threshold))
665 receive_chars_pio(info, num_bytes);
667 receive_chars_dma(info, num_bytes);
671 if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
672 (scratch & 0x02) && (info->IER & UART_IER_THRI)) {
673 if ((info->xmit_cnt <= 0) || info->tty->stopped) {
674 info->IER &= ~UART_IER_THRI;
675 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
676 serial_out(info, UART_ESI_CMD2, info->IER);
680 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
681 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
682 num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
683 num_bytes |= serial_in(info, UART_ESI_STAT2);
685 if (num_bytes > info->xmit_cnt)
686 num_bytes = info->xmit_cnt;
690 (info->stat_flags & ESP_STAT_USE_PIO) ||
691 (num_bytes <= info->config.pio_threshold))
692 transmit_chars_pio(info, num_bytes);
694 transmit_chars_dma(info, num_bytes);
699 info->last_active = jiffies;
701 #ifdef SERIAL_DEBUG_INTR
704 spin_unlock(&info->lock);
709 * -------------------------------------------------------------------
710 * Here ends the serial interrupt routines.
711 * -------------------------------------------------------------------
715 * ---------------------------------------------------------------
716 * Low level utility subroutines for the serial driver: routines to
717 * figure out the appropriate timeout for an interrupt chain, routines
718 * to initialize and startup a serial port, and routines to shutdown a
719 * serial port. Useful stuff like that.
721 * Caller should hold lock
722 * ---------------------------------------------------------------
725 static inline void esp_basic_init(struct esp_struct * info)
727 /* put ESPC in enhanced mode */
728 serial_out(info, UART_ESI_CMD1, ESI_SET_MODE);
730 if (info->stat_flags & ESP_STAT_NEVER_DMA)
731 serial_out(info, UART_ESI_CMD2, 0x01);
733 serial_out(info, UART_ESI_CMD2, 0x31);
735 /* disable interrupts for now */
736 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
737 serial_out(info, UART_ESI_CMD2, 0x00);
739 /* set interrupt and DMA channel */
740 serial_out(info, UART_ESI_CMD1, ESI_SET_IRQ);
742 if (info->stat_flags & ESP_STAT_NEVER_DMA)
743 serial_out(info, UART_ESI_CMD2, 0x01);
745 serial_out(info, UART_ESI_CMD2, (dma << 4) | 0x01);
747 serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
749 if (info->line % 8) /* secondary port */
750 serial_out(info, UART_ESI_CMD2, 0x0d); /* shared */
751 else if (info->irq == 9)
752 serial_out(info, UART_ESI_CMD2, 0x02);
754 serial_out(info, UART_ESI_CMD2, info->irq);
756 /* set error status mask (check this) */
757 serial_out(info, UART_ESI_CMD1, ESI_SET_ERR_MASK);
759 if (info->stat_flags & ESP_STAT_NEVER_DMA)
760 serial_out(info, UART_ESI_CMD2, 0xa1);
762 serial_out(info, UART_ESI_CMD2, 0xbd);
764 serial_out(info, UART_ESI_CMD2, 0x00);
766 /* set DMA timeout */
767 serial_out(info, UART_ESI_CMD1, ESI_SET_DMA_TMOUT);
768 serial_out(info, UART_ESI_CMD2, 0xff);
770 /* set FIFO trigger levels */
771 serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
772 serial_out(info, UART_ESI_CMD2, info->config.rx_trigger >> 8);
773 serial_out(info, UART_ESI_CMD2, info->config.rx_trigger);
774 serial_out(info, UART_ESI_CMD2, info->config.tx_trigger >> 8);
775 serial_out(info, UART_ESI_CMD2, info->config.tx_trigger);
777 /* Set clock scaling and wait states */
778 serial_out(info, UART_ESI_CMD1, ESI_SET_PRESCALAR);
779 serial_out(info, UART_ESI_CMD2, 0x04 | ESPC_SCALE);
781 /* set reinterrupt pacing */
782 serial_out(info, UART_ESI_CMD1, ESI_SET_REINTR);
783 serial_out(info, UART_ESI_CMD2, 0xff);
786 static int startup(struct esp_struct * info)
790 unsigned int num_chars;
792 spin_lock_irqsave(&info->lock, flags);
794 if (info->flags & ASYNC_INITIALIZED)
797 if (!info->xmit_buf) {
798 info->xmit_buf = (unsigned char *)get_zeroed_page(GFP_ATOMIC);
804 #ifdef SERIAL_DEBUG_OPEN
805 printk("starting up ttys%d (irq %d)...", info->line, info->irq);
808 /* Flush the RX buffer. Using the ESI flush command may cause */
809 /* wild interrupts, so read all the data instead. */
811 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
812 serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
813 num_chars = serial_in(info, UART_ESI_STAT1) << 8;
814 num_chars |= serial_in(info, UART_ESI_STAT2);
816 while (num_chars > 1) {
817 inw(info->port + UART_ESI_RX);
822 serial_in(info, UART_ESI_RX);
824 /* set receive character timeout */
825 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
826 serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
828 /* clear all flags except the "never DMA" flag */
829 info->stat_flags &= ESP_STAT_NEVER_DMA;
831 if (info->stat_flags & ESP_STAT_NEVER_DMA)
832 info->stat_flags |= ESP_STAT_USE_PIO;
834 spin_unlock_irqrestore(&info->lock, flags);
840 retval = request_irq(info->irq, rs_interrupt_single, IRQF_SHARED,
844 if (capable(CAP_SYS_ADMIN)) {
846 set_bit(TTY_IO_ERROR,
853 if (!(info->stat_flags & ESP_STAT_USE_PIO) && !dma_buffer) {
854 dma_buffer = (char *)__get_dma_pages(
855 GFP_KERNEL, get_order(DMA_BUFFER_SZ));
857 /* use PIO mode if DMA buf/chan cannot be allocated */
859 info->stat_flags |= ESP_STAT_USE_PIO;
860 else if (request_dma(dma, "esp serial")) {
861 free_pages((unsigned long)dma_buffer,
862 get_order(DMA_BUFFER_SZ));
864 info->stat_flags |= ESP_STAT_USE_PIO;
869 info->MCR = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
871 spin_lock_irqsave(&info->lock, flags);
872 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
873 serial_out(info, UART_ESI_CMD2, UART_MCR);
874 serial_out(info, UART_ESI_CMD2, info->MCR);
877 * Finally, enable interrupts
879 /* info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI; */
880 info->IER = UART_IER_RLSI | UART_IER_RDI | UART_IER_DMA_TMOUT |
882 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
883 serial_out(info, UART_ESI_CMD2, info->IER);
886 clear_bit(TTY_IO_ERROR, &info->tty->flags);
887 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
888 spin_unlock_irqrestore(&info->lock, flags);
891 * Set up the tty->alt_speed kludge
894 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
895 info->tty->alt_speed = 57600;
896 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
897 info->tty->alt_speed = 115200;
898 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
899 info->tty->alt_speed = 230400;
900 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
901 info->tty->alt_speed = 460800;
905 * set the speed of the serial port
908 info->flags |= ASYNC_INITIALIZED;
912 spin_unlock_irqrestore(&info->lock, flags);
918 * This routine will shutdown a serial port; interrupts are disabled, and
919 * DTR is dropped if the hangup on close termio flag is on.
921 static void shutdown(struct esp_struct * info)
923 unsigned long flags, f;
925 if (!(info->flags & ASYNC_INITIALIZED))
928 #ifdef SERIAL_DEBUG_OPEN
929 printk("Shutting down serial port %d (irq %d)....", info->line,
933 spin_lock_irqsave(&info->lock, flags);
935 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
936 * here so the queue might never be waken up
938 wake_up_interruptible(&info->delta_msr_wait);
939 wake_up_interruptible(&info->break_wait);
941 /* stop a DMA transfer on the port being closed */
942 /* DMA lock is higher priority always */
943 if (info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) {
955 free_irq(info->irq, info);
958 struct esp_struct *current_port = ports;
960 while (current_port) {
961 if ((current_port != info) &&
962 (current_port->flags & ASYNC_INITIALIZED))
965 current_port = current_port->next_port;
970 free_pages((unsigned long)dma_buffer,
971 get_order(DMA_BUFFER_SZ));
976 if (info->xmit_buf) {
977 free_page((unsigned long) info->xmit_buf);
978 info->xmit_buf = NULL;
982 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
983 serial_out(info, UART_ESI_CMD2, 0x00);
985 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
986 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
988 info->MCR &= ~UART_MCR_OUT2;
989 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
990 serial_out(info, UART_ESI_CMD2, UART_MCR);
991 serial_out(info, UART_ESI_CMD2, info->MCR);
994 set_bit(TTY_IO_ERROR, &info->tty->flags);
996 info->flags &= ~ASYNC_INITIALIZED;
997 spin_unlock_irqrestore(&info->lock, flags);
1001 * This routine is called to set the UART divisor registers to match
1002 * the specified baud rate for a serial port.
1004 static void change_speed(struct esp_struct *info)
1006 unsigned short port;
1008 unsigned cflag,cval;
1010 unsigned char flow1 = 0, flow2 = 0;
1011 unsigned long flags;
1013 if (!info->tty || !info->tty->termios)
1015 cflag = info->tty->termios->c_cflag;
1018 /* byte size and parity */
1019 switch (cflag & CSIZE) {
1020 case CS5: cval = 0x00; bits = 7; break;
1021 case CS6: cval = 0x01; bits = 8; break;
1022 case CS7: cval = 0x02; bits = 9; break;
1023 case CS8: cval = 0x03; bits = 10; break;
1024 default: cval = 0x00; bits = 7; break;
1026 if (cflag & CSTOPB) {
1030 if (cflag & PARENB) {
1031 cval |= UART_LCR_PARITY;
1034 if (!(cflag & PARODD))
1035 cval |= UART_LCR_EPAR;
1038 cval |= UART_LCR_SPAR;
1041 baud = tty_get_baud_rate(info->tty);
1042 if (baud == 38400 &&
1043 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1044 quot = info->custom_divisor;
1047 /* Special case since 134 is really 134.5 */
1048 quot = (2*BASE_BAUD / 269);
1050 quot = BASE_BAUD / baud;
1052 /* If the quotient is ever zero, default to 9600 bps */
1054 quot = BASE_BAUD / 9600;
1056 info->timeout = ((1024 * HZ * bits * quot) / BASE_BAUD) + (HZ / 50);
1058 /* CTS flow control flag and modem status interrupts */
1059 /* info->IER &= ~UART_IER_MSI; */
1060 if (cflag & CRTSCTS) {
1061 info->flags |= ASYNC_CTS_FLOW;
1062 /* info->IER |= UART_IER_MSI; */
1066 info->flags &= ~ASYNC_CTS_FLOW;
1068 info->flags &= ~ASYNC_CHECK_CD;
1070 info->flags |= ASYNC_CHECK_CD;
1071 /* info->IER |= UART_IER_MSI; */
1075 * Set up parity check flag
1077 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1078 if (I_INPCK(info->tty))
1079 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1080 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1081 info->read_status_mask |= UART_LSR_BI;
1083 info->ignore_status_mask = 0;
1085 /* This should be safe, but for some broken bits of hardware... */
1086 if (I_IGNPAR(info->tty)) {
1087 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1088 info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
1091 if (I_IGNBRK(info->tty)) {
1092 info->ignore_status_mask |= UART_LSR_BI;
1093 info->read_status_mask |= UART_LSR_BI;
1095 * If we're ignore parity and break indicators, ignore
1096 * overruns too. (For real raw support).
1098 if (I_IGNPAR(info->tty)) {
1099 info->ignore_status_mask |= UART_LSR_OE | \
1100 UART_LSR_PE | UART_LSR_FE;
1101 info->read_status_mask |= UART_LSR_OE | \
1102 UART_LSR_PE | UART_LSR_FE;
1106 if (I_IXOFF(info->tty))
1109 spin_lock_irqsave(&info->lock, flags);
1111 serial_out(info, UART_ESI_CMD1, ESI_SET_BAUD);
1112 serial_out(info, UART_ESI_CMD2, quot >> 8);
1113 serial_out(info, UART_ESI_CMD2, quot & 0xff);
1115 /* set data bits, parity, etc. */
1116 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1117 serial_out(info, UART_ESI_CMD2, UART_LCR);
1118 serial_out(info, UART_ESI_CMD2, cval);
1120 /* Enable flow control */
1121 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CNTL);
1122 serial_out(info, UART_ESI_CMD2, flow1);
1123 serial_out(info, UART_ESI_CMD2, flow2);
1125 /* set flow control characters (XON/XOFF only) */
1126 if (I_IXOFF(info->tty)) {
1127 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CHARS);
1128 serial_out(info, UART_ESI_CMD2, START_CHAR(info->tty));
1129 serial_out(info, UART_ESI_CMD2, STOP_CHAR(info->tty));
1130 serial_out(info, UART_ESI_CMD2, 0x10);
1131 serial_out(info, UART_ESI_CMD2, 0x21);
1132 switch (cflag & CSIZE) {
1134 serial_out(info, UART_ESI_CMD2, 0x1f);
1137 serial_out(info, UART_ESI_CMD2, 0x3f);
1141 serial_out(info, UART_ESI_CMD2, 0x7f);
1144 serial_out(info, UART_ESI_CMD2, 0xff);
1149 /* Set high/low water */
1150 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1151 serial_out(info, UART_ESI_CMD2, info->config.flow_off >> 8);
1152 serial_out(info, UART_ESI_CMD2, info->config.flow_off);
1153 serial_out(info, UART_ESI_CMD2, info->config.flow_on >> 8);
1154 serial_out(info, UART_ESI_CMD2, info->config.flow_on);
1156 spin_unlock_irqrestore(&info->lock, flags);
1159 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
1161 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1162 unsigned long flags;
1164 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
1167 if (!info->xmit_buf)
1170 spin_lock_irqsave(&info->lock, flags);
1171 if (info->xmit_cnt < ESP_XMIT_SIZE - 1) {
1172 info->xmit_buf[info->xmit_head++] = ch;
1173 info->xmit_head &= ESP_XMIT_SIZE-1;
1176 spin_unlock_irqrestore(&info->lock, flags);
1179 static void rs_flush_chars(struct tty_struct *tty)
1181 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1182 unsigned long flags;
1184 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
1187 spin_lock_irqsave(&info->lock, flags);
1189 if (info->xmit_cnt <= 0 || tty->stopped || !info->xmit_buf)
1192 if (!(info->IER & UART_IER_THRI)) {
1193 info->IER |= UART_IER_THRI;
1194 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1195 serial_out(info, UART_ESI_CMD2, info->IER);
1198 spin_unlock_irqrestore(&info->lock, flags);
1201 static int rs_write(struct tty_struct * tty,
1202 const unsigned char *buf, int count)
1205 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1206 unsigned long flags;
1208 if (serial_paranoia_check(info, tty->name, "rs_write"))
1211 if (!info->xmit_buf)
1215 /* Thanks to R. Wolff for suggesting how to do this with */
1216 /* interrupts enabled */
1219 t = ESP_XMIT_SIZE - info->xmit_cnt - 1;
1224 t = ESP_XMIT_SIZE - info->xmit_head;
1232 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1234 info->xmit_head = (info->xmit_head + c) & (ESP_XMIT_SIZE-1);
1235 info->xmit_cnt += c;
1241 spin_lock_irqsave(&info->lock, flags);
1243 if (info->xmit_cnt && !tty->stopped && !(info->IER & UART_IER_THRI)) {
1244 info->IER |= UART_IER_THRI;
1245 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1246 serial_out(info, UART_ESI_CMD2, info->IER);
1249 spin_unlock_irqrestore(&info->lock, flags);
1253 static int rs_write_room(struct tty_struct *tty)
1255 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1257 unsigned long flags;
1259 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
1262 spin_lock_irqsave(&info->lock, flags);
1264 ret = ESP_XMIT_SIZE - info->xmit_cnt - 1;
1267 spin_unlock_irqrestore(&info->lock, flags);
1271 static int rs_chars_in_buffer(struct tty_struct *tty)
1273 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1275 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
1277 return info->xmit_cnt;
1280 static void rs_flush_buffer(struct tty_struct *tty)
1282 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1283 unsigned long flags;
1285 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
1287 spin_lock_irqsave(&info->lock, flags);
1288 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1289 spin_unlock_irqrestore(&info->lock, flags);
1294 * ------------------------------------------------------------
1297 * This routine is called by the upper-layer tty layer to signal that
1298 * incoming characters should be throttled.
1299 * ------------------------------------------------------------
1301 static void rs_throttle(struct tty_struct * tty)
1303 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1304 unsigned long flags;
1305 #ifdef SERIAL_DEBUG_THROTTLE
1308 printk("throttle %s: %d....\n", tty_name(tty, buf),
1309 tty->ldisc.chars_in_buffer(tty));
1312 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
1315 spin_lock_irqsave(&info->lock, flags);
1316 info->IER &= ~UART_IER_RDI;
1317 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1318 serial_out(info, UART_ESI_CMD2, info->IER);
1319 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1320 serial_out(info, UART_ESI_CMD2, 0x00);
1321 spin_unlock_irqrestore(&info->lock, flags);
1324 static void rs_unthrottle(struct tty_struct * tty)
1326 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1327 unsigned long flags;
1328 #ifdef SERIAL_DEBUG_THROTTLE
1331 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1332 tty->ldisc.chars_in_buffer(tty));
1335 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1338 spin_lock_irqsave(&info->lock, flags);
1339 info->IER |= UART_IER_RDI;
1340 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1341 serial_out(info, UART_ESI_CMD2, info->IER);
1342 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1343 serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
1344 spin_unlock_irqrestore(&info->lock, flags);
1348 * ------------------------------------------------------------
1349 * rs_ioctl() and friends
1350 * ------------------------------------------------------------
1353 static int get_serial_info(struct esp_struct * info,
1354 struct serial_struct __user *retinfo)
1356 struct serial_struct tmp;
1359 memset(&tmp, 0, sizeof(tmp));
1360 tmp.type = PORT_16550A;
1361 tmp.line = info->line;
1362 tmp.port = info->port;
1363 tmp.irq = info->irq;
1364 tmp.flags = info->flags;
1365 tmp.xmit_fifo_size = 1024;
1366 tmp.baud_base = BASE_BAUD;
1367 tmp.close_delay = info->close_delay;
1368 tmp.closing_wait = info->closing_wait;
1369 tmp.custom_divisor = info->custom_divisor;
1372 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1377 static int get_esp_config(struct esp_struct * info,
1378 struct hayes_esp_config __user *retinfo)
1380 struct hayes_esp_config tmp;
1385 memset(&tmp, 0, sizeof(tmp));
1387 tmp.rx_timeout = info->config.rx_timeout;
1388 tmp.rx_trigger = info->config.rx_trigger;
1389 tmp.tx_trigger = info->config.tx_trigger;
1390 tmp.flow_off = info->config.flow_off;
1391 tmp.flow_on = info->config.flow_on;
1392 tmp.pio_threshold = info->config.pio_threshold;
1393 tmp.dma_channel = (info->stat_flags & ESP_STAT_NEVER_DMA ? 0 : dma);
1396 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
1399 static int set_serial_info(struct esp_struct * info,
1400 struct serial_struct __user *new_info)
1402 struct serial_struct new_serial;
1403 struct esp_struct old_info;
1404 unsigned int change_irq;
1406 struct esp_struct *current_async;
1408 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1412 if ((new_serial.type != PORT_16550A) ||
1413 (new_serial.hub6) ||
1414 (info->port != new_serial.port) ||
1415 (new_serial.baud_base != BASE_BAUD) ||
1416 (new_serial.irq > 15) ||
1417 (new_serial.irq < 2) ||
1418 (new_serial.irq == 6) ||
1419 (new_serial.irq == 8) ||
1420 (new_serial.irq == 13))
1423 change_irq = new_serial.irq != info->irq;
1425 if (change_irq && (info->line % 8))
1428 if (!capable(CAP_SYS_ADMIN)) {
1430 (new_serial.close_delay != info->close_delay) ||
1431 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1432 (info->flags & ~ASYNC_USR_MASK)))
1434 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1435 (new_serial.flags & ASYNC_USR_MASK));
1436 info->custom_divisor = new_serial.custom_divisor;
1438 if (new_serial.irq == 2)
1442 current_async = ports;
1444 while (current_async) {
1445 if ((current_async->line >= info->line) &&
1446 (current_async->line < (info->line + 8))) {
1447 if (current_async == info) {
1448 if (current_async->count > 1)
1450 } else if (current_async->count)
1454 current_async = current_async->next_port;
1459 * OK, past this point, all the error checking has been done.
1460 * At this point, we start making changes.....
1463 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1464 (new_serial.flags & ASYNC_FLAGS));
1465 info->custom_divisor = new_serial.custom_divisor;
1466 info->close_delay = new_serial.close_delay * HZ/100;
1467 info->closing_wait = new_serial.closing_wait * HZ/100;
1471 * We need to shutdown the serial port at the old
1472 * port/irq combination.
1476 current_async = ports;
1478 while (current_async) {
1479 if ((current_async->line >= info->line) &&
1480 (current_async->line < (info->line + 8)))
1481 current_async->irq = new_serial.irq;
1483 current_async = current_async->next_port;
1486 serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
1488 serial_out(info, UART_ESI_CMD2, 0x02);
1490 serial_out(info, UART_ESI_CMD2, info->irq);
1494 if (info->flags & ASYNC_INITIALIZED) {
1495 if (((old_info.flags & ASYNC_SPD_MASK) !=
1496 (info->flags & ASYNC_SPD_MASK)) ||
1497 (old_info.custom_divisor != info->custom_divisor)) {
1498 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1499 info->tty->alt_speed = 57600;
1500 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1501 info->tty->alt_speed = 115200;
1502 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1503 info->tty->alt_speed = 230400;
1504 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1505 info->tty->alt_speed = 460800;
1509 retval = startup(info);
1514 static int set_esp_config(struct esp_struct * info,
1515 struct hayes_esp_config __user * new_info)
1517 struct hayes_esp_config new_config;
1518 unsigned int change_dma;
1520 struct esp_struct *current_async;
1521 unsigned long flags;
1523 /* Perhaps a non-sysadmin user should be able to do some of these */
1524 /* operations. I haven't decided yet. */
1526 if (!capable(CAP_SYS_ADMIN))
1529 if (copy_from_user(&new_config, new_info, sizeof(new_config)))
1532 if ((new_config.flow_on >= new_config.flow_off) ||
1533 (new_config.rx_trigger < 1) ||
1534 (new_config.tx_trigger < 1) ||
1535 (new_config.flow_off < 1) ||
1536 (new_config.flow_on < 1) ||
1537 (new_config.rx_trigger > 1023) ||
1538 (new_config.tx_trigger > 1023) ||
1539 (new_config.flow_off > 1023) ||
1540 (new_config.flow_on > 1023) ||
1541 (new_config.pio_threshold < 0) ||
1542 (new_config.pio_threshold > 1024))
1545 if ((new_config.dma_channel != 1) && (new_config.dma_channel != 3))
1546 new_config.dma_channel = 0;
1548 if (info->stat_flags & ESP_STAT_NEVER_DMA)
1549 change_dma = new_config.dma_channel;
1551 change_dma = (new_config.dma_channel != dma);
1554 if (new_config.dma_channel) {
1555 /* PIO mode to DMA mode transition OR */
1556 /* change current DMA channel */
1558 current_async = ports;
1560 while (current_async) {
1561 if (current_async == info) {
1562 if (current_async->count > 1)
1564 } else if (current_async->count)
1568 current_async->next_port;
1572 dma = new_config.dma_channel;
1573 info->stat_flags &= ~ESP_STAT_NEVER_DMA;
1575 /* all ports must use the same DMA channel */
1577 spin_lock_irqsave(&info->lock, flags);
1578 current_async = ports;
1580 while (current_async) {
1581 esp_basic_init(current_async);
1582 current_async = current_async->next_port;
1584 spin_unlock_irqrestore(&info->lock, flags);
1586 /* DMA mode to PIO mode only */
1588 if (info->count > 1)
1592 spin_lock_irqsave(&info->lock, flags);
1593 info->stat_flags |= ESP_STAT_NEVER_DMA;
1594 esp_basic_init(info);
1595 spin_unlock_irqrestore(&info->lock, flags);
1599 info->config.pio_threshold = new_config.pio_threshold;
1601 if ((new_config.flow_off != info->config.flow_off) ||
1602 (new_config.flow_on != info->config.flow_on)) {
1603 unsigned long flags;
1605 info->config.flow_off = new_config.flow_off;
1606 info->config.flow_on = new_config.flow_on;
1608 spin_lock_irqsave(&info->lock, flags);
1609 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1610 serial_out(info, UART_ESI_CMD2, new_config.flow_off >> 8);
1611 serial_out(info, UART_ESI_CMD2, new_config.flow_off);
1612 serial_out(info, UART_ESI_CMD2, new_config.flow_on >> 8);
1613 serial_out(info, UART_ESI_CMD2, new_config.flow_on);
1614 spin_unlock_irqrestore(&info->lock, flags);
1617 if ((new_config.rx_trigger != info->config.rx_trigger) ||
1618 (new_config.tx_trigger != info->config.tx_trigger)) {
1619 unsigned long flags;
1621 info->config.rx_trigger = new_config.rx_trigger;
1622 info->config.tx_trigger = new_config.tx_trigger;
1623 spin_lock_irqsave(&info->lock, flags);
1624 serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
1625 serial_out(info, UART_ESI_CMD2,
1626 new_config.rx_trigger >> 8);
1627 serial_out(info, UART_ESI_CMD2, new_config.rx_trigger);
1628 serial_out(info, UART_ESI_CMD2,
1629 new_config.tx_trigger >> 8);
1630 serial_out(info, UART_ESI_CMD2, new_config.tx_trigger);
1631 spin_unlock_irqrestore(&info->lock, flags);
1634 if (new_config.rx_timeout != info->config.rx_timeout) {
1635 unsigned long flags;
1637 info->config.rx_timeout = new_config.rx_timeout;
1638 spin_lock_irqsave(&info->lock, flags);
1640 if (info->IER & UART_IER_RDI) {
1641 serial_out(info, UART_ESI_CMD1,
1642 ESI_SET_RX_TIMEOUT);
1643 serial_out(info, UART_ESI_CMD2,
1644 new_config.rx_timeout);
1647 spin_unlock_irqrestore(&info->lock, flags);
1650 if (!(info->flags & ASYNC_INITIALIZED))
1651 retval = startup(info);
1657 * get_lsr_info - get line status register info
1659 * Purpose: Let user call ioctl() to get info when the UART physically
1660 * is emptied. On bus types like RS485, the transmitter must
1661 * release the bus after transmitting. This must be done when
1662 * the transmit shift register is empty, not be done when the
1663 * transmit holding register is empty. This functionality
1664 * allows an RS485 driver to be written in user space.
1666 static int get_lsr_info(struct esp_struct * info, unsigned int __user *value)
1668 unsigned char status;
1669 unsigned int result;
1670 unsigned long flags;
1672 spin_lock_irqsave(&info->lock, flags);
1673 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1674 status = serial_in(info, UART_ESI_STAT1);
1675 spin_unlock_irqrestore(&info->lock, flags);
1676 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1677 return put_user(result,value);
1681 static int esp_tiocmget(struct tty_struct *tty, struct file *file)
1683 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1684 unsigned char control, status;
1685 unsigned long flags;
1687 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1689 if (tty->flags & (1 << TTY_IO_ERROR))
1692 control = info->MCR;
1694 spin_lock_irqsave(&info->lock, flags);
1695 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1696 status = serial_in(info, UART_ESI_STAT2);
1697 spin_unlock_irqrestore(&info->lock, flags);
1699 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1700 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1701 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1702 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1703 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1704 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1707 static int esp_tiocmset(struct tty_struct *tty, struct file *file,
1708 unsigned int set, unsigned int clear)
1710 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1711 unsigned long flags;
1713 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1715 if (tty->flags & (1 << TTY_IO_ERROR))
1718 spin_lock_irqsave(&info->lock, flags);
1720 if (set & TIOCM_RTS)
1721 info->MCR |= UART_MCR_RTS;
1722 if (set & TIOCM_DTR)
1723 info->MCR |= UART_MCR_DTR;
1725 if (clear & TIOCM_RTS)
1726 info->MCR &= ~UART_MCR_RTS;
1727 if (clear & TIOCM_DTR)
1728 info->MCR &= ~UART_MCR_DTR;
1730 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1731 serial_out(info, UART_ESI_CMD2, UART_MCR);
1732 serial_out(info, UART_ESI_CMD2, info->MCR);
1734 spin_unlock_irqrestore(&info->lock, flags);
1739 * rs_break() --- routine which turns the break handling on or off
1741 static void esp_break(struct tty_struct *tty, int break_state)
1743 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1744 unsigned long flags;
1746 if (serial_paranoia_check(info, tty->name, "esp_break"))
1749 if (break_state == -1) {
1750 spin_lock_irqsave(&info->lock, flags);
1751 serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1752 serial_out(info, UART_ESI_CMD2, 0x01);
1753 spin_unlock_irqrestore(&info->lock, flags);
1755 /* FIXME - new style wait needed here */
1756 interruptible_sleep_on(&info->break_wait);
1758 spin_lock_irqsave(&info->lock, flags);
1759 serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1760 serial_out(info, UART_ESI_CMD2, 0x00);
1761 spin_unlock_irqrestore(&info->lock, flags);
1765 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1766 unsigned int cmd, unsigned long arg)
1768 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1769 struct async_icount cprev, cnow; /* kernel counter temps */
1770 struct serial_icounter_struct __user *p_cuser; /* user space */
1771 void __user *argp = (void __user *)arg;
1772 unsigned long flags;
1775 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1778 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1779 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1780 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT) &&
1781 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT) &&
1782 (cmd != TIOCGHAYESESP) && (cmd != TIOCSHAYESESP)) {
1783 if (tty->flags & (1 << TTY_IO_ERROR))
1789 return get_serial_info(info, argp);
1792 ret = set_serial_info(info, argp);
1796 /* do not reconfigure after initial configuration */
1800 return put_user(0L, (unsigned long __user *)argp);
1802 case TIOCSERGETLSR: /* Get line status register */
1803 return get_lsr_info(info, argp);
1806 if (!capable(CAP_SYS_ADMIN))
1811 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1812 * - mask passed in arg for lines of interest
1813 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1814 * Caller should use TIOCGICOUNT to see which one it was
1817 spin_lock_irqsave(&info->lock, flags);
1818 cprev = info->icount; /* note the counters on entry */
1819 spin_unlock_irqrestore(&info->lock, flags);
1821 /* FIXME: convert to new style wakeup */
1822 interruptible_sleep_on(&info->delta_msr_wait);
1823 /* see if a signal did it */
1824 if (signal_pending(current))
1825 return -ERESTARTSYS;
1826 spin_lock_irqsave(&info->lock, flags);
1827 cnow = info->icount; /* atomic copy */
1828 spin_unlock_irqrestore(&info->lock, flags);
1829 if (cnow.rng == cprev.rng &&
1830 cnow.dsr == cprev.dsr &&
1831 cnow.dcd == cprev.dcd &&
1832 cnow.cts == cprev.cts)
1833 return -EIO; /* no change => error */
1834 if (((arg & TIOCM_RNG) &&
1835 (cnow.rng != cprev.rng)) ||
1836 ((arg & TIOCM_DSR) &&
1837 (cnow.dsr != cprev.dsr)) ||
1838 ((arg & TIOCM_CD) &&
1839 (cnow.dcd != cprev.dcd)) ||
1840 ((arg & TIOCM_CTS) &&
1841 (cnow.cts != cprev.cts)) ) {
1849 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1850 * Return: write counters to the user passed counter struct
1851 * NB: both 1->0 and 0->1 transitions are counted except for
1852 * RI where only 0->1 is counted.
1855 spin_lock_irqsave(&info->lock, flags);
1856 cnow = info->icount;
1857 spin_unlock_irqrestore(&info->lock, flags);
1859 if (put_user(cnow.cts, &p_cuser->cts) ||
1860 put_user(cnow.dsr, &p_cuser->dsr) ||
1861 put_user(cnow.rng, &p_cuser->rng) ||
1862 put_user(cnow.dcd, &p_cuser->dcd))
1867 return get_esp_config(info, argp);
1870 ret = set_esp_config(info, argp);
1874 return -ENOIOCTLCMD;
1879 static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1881 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1882 unsigned long flags;
1886 spin_lock_irqsave(&info->lock, flags);
1888 /* Handle transition to B0 status */
1889 if ((old_termios->c_cflag & CBAUD) &&
1890 !(tty->termios->c_cflag & CBAUD)) {
1891 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1892 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1893 serial_out(info, UART_ESI_CMD2, UART_MCR);
1894 serial_out(info, UART_ESI_CMD2, info->MCR);
1897 /* Handle transition away from B0 status */
1898 if (!(old_termios->c_cflag & CBAUD) &&
1899 (tty->termios->c_cflag & CBAUD)) {
1900 info->MCR |= (UART_MCR_DTR | UART_MCR_RTS);
1901 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1902 serial_out(info, UART_ESI_CMD2, UART_MCR);
1903 serial_out(info, UART_ESI_CMD2, info->MCR);
1906 spin_unlock_irqrestore(&info->lock, flags);
1908 /* Handle turning of CRTSCTS */
1909 if ((old_termios->c_cflag & CRTSCTS) &&
1910 !(tty->termios->c_cflag & CRTSCTS)) {
1916 * ------------------------------------------------------------
1919 * This routine is called when the serial port gets closed. First, we
1920 * wait for the last remaining data to be sent. Then, we unlink its
1921 * async structure from the interrupt chain if necessary, and we free
1922 * that IRQ if nothing is left in the chain.
1923 * ------------------------------------------------------------
1925 static void rs_close(struct tty_struct *tty, struct file * filp)
1927 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1928 unsigned long flags;
1930 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1933 spin_lock_irqsave(&info->lock, flags);
1935 if (tty_hung_up_p(filp)) {
1936 DBG_CNT("before DEC-hung");
1940 #ifdef SERIAL_DEBUG_OPEN
1941 printk("rs_close ttys%d, count = %d\n", info->line, info->count);
1943 if ((tty->count == 1) && (info->count != 1)) {
1945 * Uh, oh. tty->count is 1, which means that the tty
1946 * structure will be freed. Info->count should always
1947 * be one in these conditions. If it's greater than
1948 * one, we've got real problems, since it means the
1949 * serial port won't be shutdown.
1951 printk("rs_close: bad serial port count; tty->count is 1, "
1952 "info->count is %d\n", info->count);
1955 if (--info->count < 0) {
1956 printk("rs_close: bad serial port count for ttys%d: %d\n",
1957 info->line, info->count);
1961 DBG_CNT("before DEC-2");
1964 info->flags |= ASYNC_CLOSING;
1966 spin_unlock_irqrestore(&info->lock, flags);
1968 * Now we wait for the transmit buffer to clear; and we notify
1969 * the line discipline to only process XON/XOFF characters.
1972 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1973 tty_wait_until_sent(tty, info->closing_wait);
1975 * At this point we stop accepting input. To do this, we
1976 * disable the receive line status interrupts, and tell the
1977 * interrupt driver to stop checking the data ready bit in the
1978 * line status register.
1980 /* info->IER &= ~UART_IER_RLSI; */
1981 info->IER &= ~UART_IER_RDI;
1982 info->read_status_mask &= ~UART_LSR_DR;
1983 if (info->flags & ASYNC_INITIALIZED) {
1985 spin_lock_irqsave(&info->lock, flags);
1986 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1987 serial_out(info, UART_ESI_CMD2, info->IER);
1989 /* disable receive timeout */
1990 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1991 serial_out(info, UART_ESI_CMD2, 0x00);
1993 spin_unlock_irqrestore(&info->lock, flags);
1996 * Before we drop DTR, make sure the UART transmitter
1997 * has completely drained; this is especially
1998 * important if there is a transmit FIFO!
2000 rs_wait_until_sent(tty, info->timeout);
2003 if (tty->driver->flush_buffer)
2004 tty->driver->flush_buffer(tty);
2005 tty_ldisc_flush(tty);
2009 if (info->blocked_open) {
2010 if (info->close_delay) {
2011 msleep_interruptible(jiffies_to_msecs(info->close_delay));
2013 wake_up_interruptible(&info->open_wait);
2015 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
2016 wake_up_interruptible(&info->close_wait);
2020 spin_unlock_irqrestore(&info->lock, flags);
2023 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
2025 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
2026 unsigned long orig_jiffies, char_time;
2027 unsigned long flags;
2029 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
2032 orig_jiffies = jiffies;
2033 char_time = ((info->timeout - HZ / 50) / 1024) / 5;
2038 spin_lock_irqsave(&info->lock, flags);
2039 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2040 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2042 while ((serial_in(info, UART_ESI_STAT1) != 0x03) ||
2043 (serial_in(info, UART_ESI_STAT2) != 0xff)) {
2045 spin_unlock_irqrestore(&info->lock, flags);
2046 msleep_interruptible(jiffies_to_msecs(char_time));
2048 if (signal_pending(current))
2051 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2054 spin_lock_irqsave(&info->lock, flags);
2055 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2056 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2058 spin_unlock_irqrestore(&info->lock, flags);
2059 set_current_state(TASK_RUNNING);
2063 * esp_hangup() --- called by tty_hangup() when a hangup is signaled.
2065 static void esp_hangup(struct tty_struct *tty)
2067 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
2069 if (serial_paranoia_check(info, tty->name, "esp_hangup"))
2072 rs_flush_buffer(tty);
2075 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2077 wake_up_interruptible(&info->open_wait);
2081 * ------------------------------------------------------------
2082 * esp_open() and friends
2083 * ------------------------------------------------------------
2085 static int block_til_ready(struct tty_struct *tty, struct file * filp,
2086 struct esp_struct *info)
2088 DECLARE_WAITQUEUE(wait, current);
2091 unsigned long flags;
2094 * If the device is in the middle of being closed, then block
2095 * until it's done, and then try again.
2097 if (tty_hung_up_p(filp) ||
2098 (info->flags & ASYNC_CLOSING)) {
2099 if (info->flags & ASYNC_CLOSING)
2100 interruptible_sleep_on(&info->close_wait);
2101 #ifdef SERIAL_DO_RESTART
2102 if (info->flags & ASYNC_HUP_NOTIFY)
2105 return -ERESTARTSYS;
2112 * If non-blocking mode is set, or the port is not enabled,
2113 * then make the check up front and then exit.
2115 if ((filp->f_flags & O_NONBLOCK) ||
2116 (tty->flags & (1 << TTY_IO_ERROR))) {
2117 info->flags |= ASYNC_NORMAL_ACTIVE;
2121 if (tty->termios->c_cflag & CLOCAL)
2125 * Block waiting for the carrier detect and the line to become
2126 * free (i.e., not in use by the callout). While we are in
2127 * this loop, info->count is dropped by one, so that
2128 * rs_close() knows when to free things. We restore it upon
2129 * exit, either normal or abnormal.
2132 add_wait_queue(&info->open_wait, &wait);
2133 #ifdef SERIAL_DEBUG_OPEN
2134 printk("block_til_ready before block: ttys%d, count = %d\n",
2135 info->line, info->count);
2137 spin_lock_irqsave(&info->lock, flags);
2138 if (!tty_hung_up_p(filp))
2140 info->blocked_open++;
2142 if ((tty->termios->c_cflag & CBAUD)) {
2143 unsigned int scratch;
2145 serial_out(info, UART_ESI_CMD1, ESI_READ_UART);
2146 serial_out(info, UART_ESI_CMD2, UART_MCR);
2147 scratch = serial_in(info, UART_ESI_STAT1);
2148 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2149 serial_out(info, UART_ESI_CMD2, UART_MCR);
2150 serial_out(info, UART_ESI_CMD2,
2151 scratch | UART_MCR_DTR | UART_MCR_RTS);
2153 set_current_state(TASK_INTERRUPTIBLE);
2154 if (tty_hung_up_p(filp) ||
2155 !(info->flags & ASYNC_INITIALIZED)) {
2156 #ifdef SERIAL_DO_RESTART
2157 if (info->flags & ASYNC_HUP_NOTIFY)
2160 retval = -ERESTARTSYS;
2167 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
2168 if (serial_in(info, UART_ESI_STAT2) & UART_MSR_DCD)
2171 if (!(info->flags & ASYNC_CLOSING) &&
2174 if (signal_pending(current)) {
2175 retval = -ERESTARTSYS;
2178 #ifdef SERIAL_DEBUG_OPEN
2179 printk("block_til_ready blocking: ttys%d, count = %d\n",
2180 info->line, info->count);
2182 spin_unlock_irqrestore(&info->lock, flags);
2184 spin_lock_irqsave(&info->lock, flags);
2186 set_current_state(TASK_RUNNING);
2187 remove_wait_queue(&info->open_wait, &wait);
2188 if (!tty_hung_up_p(filp))
2190 info->blocked_open--;
2191 spin_unlock_irqrestore(&info->lock, flags);
2192 #ifdef SERIAL_DEBUG_OPEN
2193 printk("block_til_ready after blocking: ttys%d, count = %d\n",
2194 info->line, info->count);
2198 info->flags |= ASYNC_NORMAL_ACTIVE;
2203 * This routine is called whenever a serial port is opened. It
2204 * enables interrupts for a serial port, linking in its async structure into
2205 * the IRQ chain. It also performs the serial-specific
2206 * initialization for the tty structure.
2208 static int esp_open(struct tty_struct *tty, struct file * filp)
2210 struct esp_struct *info;
2212 unsigned long flags;
2215 if ((line < 0) || (line >= NR_PORTS))
2218 /* find the port in the chain */
2222 while (info && (info->line != line))
2223 info = info->next_port;
2226 serial_paranoia_check(info, tty->name, "esp_open");
2230 #ifdef SERIAL_DEBUG_OPEN
2231 printk("esp_open %s, count = %d\n", tty->name, info->count);
2233 spin_lock_irqsave(&info->lock, flags);
2235 tty->driver_data = info;
2238 spin_unlock_irqrestore(&info->lock, flags);
2241 * Start up serial port
2243 retval = startup(info);
2247 retval = block_til_ready(tty, filp, info);
2249 #ifdef SERIAL_DEBUG_OPEN
2250 printk("esp_open returning after block_til_ready with %d\n",
2256 #ifdef SERIAL_DEBUG_OPEN
2257 printk("esp_open %s successful...", tty->name);
2263 * ---------------------------------------------------------------------
2264 * espserial_init() and friends
2266 * espserial_init() is called at boot-time to initialize the serial driver.
2267 * ---------------------------------------------------------------------
2271 * This routine prints out the appropriate serial driver version
2272 * number, and identifies which options were configured into this
2276 static inline void show_serial_version(void)
2278 printk(KERN_INFO "%s version %s (DMA %u)\n",
2279 serial_name, serial_version, dma);
2283 * This routine is called by espserial_init() to initialize a specific serial
2286 static inline int autoconfig(struct esp_struct * info)
2288 int port_detected = 0;
2289 unsigned long flags;
2291 if (!request_region(info->port, REGION_SIZE, "esp serial"))
2294 spin_lock_irqsave(&info->lock, flags);
2296 * Check for ESP card
2299 if (serial_in(info, UART_ESI_BASE) == 0xf3) {
2300 serial_out(info, UART_ESI_CMD1, 0x00);
2301 serial_out(info, UART_ESI_CMD1, 0x01);
2303 if ((serial_in(info, UART_ESI_STAT2) & 0x70) == 0x20) {
2307 serial_out(info, UART_ESI_CMD1, 0x02);
2309 if (serial_in(info, UART_ESI_STAT1) & 0x01)
2316 /* put card in enhanced mode */
2317 /* this prevents access through */
2318 /* the "old" IO ports */
2319 esp_basic_init(info);
2322 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2323 serial_out(info, UART_ESI_CMD2, UART_MCR);
2324 serial_out(info, UART_ESI_CMD2, 0x00);
2328 release_region(info->port, REGION_SIZE);
2330 spin_unlock_irqrestore(&info->lock, flags);
2331 return (port_detected);
2334 static const struct tty_operations esp_ops = {
2338 .put_char = rs_put_char,
2339 .flush_chars = rs_flush_chars,
2340 .write_room = rs_write_room,
2341 .chars_in_buffer = rs_chars_in_buffer,
2342 .flush_buffer = rs_flush_buffer,
2344 .throttle = rs_throttle,
2345 .unthrottle = rs_unthrottle,
2346 .set_termios = rs_set_termios,
2349 .hangup = esp_hangup,
2350 .break_ctl = esp_break,
2351 .wait_until_sent = rs_wait_until_sent,
2352 .tiocmget = esp_tiocmget,
2353 .tiocmset = esp_tiocmset,
2357 * The serial driver boot-time initialization code!
2359 static int __init espserial_init(void)
2362 struct esp_struct * info;
2363 struct esp_struct *last_primary = NULL;
2364 int esp[] = {0x100,0x140,0x180,0x200,0x240,0x280,0x300,0x380};
2366 esp_driver = alloc_tty_driver(NR_PORTS);
2370 for (i = 0; i < NR_PRIMARY; i++) {
2372 if ((irq[i] < 2) || (irq[i] > 15) || (irq[i] == 6) ||
2373 (irq[i] == 8) || (irq[i] == 13))
2375 else if (irq[i] == 2)
2380 if ((dma != 1) && (dma != 3))
2383 if ((rx_trigger < 1) || (rx_trigger > 1023))
2386 if ((tx_trigger < 1) || (tx_trigger > 1023))
2389 if ((flow_off < 1) || (flow_off > 1023))
2392 if ((flow_on < 1) || (flow_on > 1023))
2395 if ((rx_timeout < 0) || (rx_timeout > 255))
2398 if (flow_on >= flow_off)
2399 flow_on = flow_off - 1;
2401 show_serial_version();
2403 /* Initialize the tty_driver structure */
2405 esp_driver->owner = THIS_MODULE;
2406 esp_driver->name = "ttyP";
2407 esp_driver->major = ESP_IN_MAJOR;
2408 esp_driver->minor_start = 0;
2409 esp_driver->type = TTY_DRIVER_TYPE_SERIAL;
2410 esp_driver->subtype = SERIAL_TYPE_NORMAL;
2411 esp_driver->init_termios = tty_std_termios;
2412 esp_driver->init_termios.c_cflag =
2413 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2414 esp_driver->flags = TTY_DRIVER_REAL_RAW;
2415 tty_set_operations(esp_driver, &esp_ops);
2416 if (tty_register_driver(esp_driver))
2418 printk(KERN_ERR "Couldn't register esp serial driver");
2419 put_tty_driver(esp_driver);
2423 info = kzalloc(sizeof(struct esp_struct), GFP_KERNEL);
2427 printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
2428 tty_unregister_driver(esp_driver);
2429 put_tty_driver(esp_driver);
2433 spin_lock_init(&info->lock);
2434 /* rx_trigger, tx_trigger are needed by autoconfig */
2435 info->config.rx_trigger = rx_trigger;
2436 info->config.tx_trigger = tx_trigger;
2442 info->port = esp[i] + offset;
2444 info->line = (i * 8) + (offset / 8);
2446 if (!autoconfig(info)) {
2452 info->custom_divisor = (divisor[i] >> (offset / 2)) & 0xf;
2453 info->flags = STD_COM_FLAGS;
2454 if (info->custom_divisor)
2455 info->flags |= ASYNC_SPD_CUST;
2456 info->magic = ESP_MAGIC;
2457 info->close_delay = 5*HZ/10;
2458 info->closing_wait = 30*HZ;
2459 info->config.rx_timeout = rx_timeout;
2460 info->config.flow_on = flow_on;
2461 info->config.flow_off = flow_off;
2462 info->config.pio_threshold = pio_threshold;
2463 info->next_port = ports;
2464 init_waitqueue_head(&info->open_wait);
2465 init_waitqueue_head(&info->close_wait);
2466 init_waitqueue_head(&info->delta_msr_wait);
2467 init_waitqueue_head(&info->break_wait);
2469 printk(KERN_INFO "ttyP%d at 0x%04x (irq = %d) is an ESP ",
2470 info->line, info->port, info->irq);
2472 if (info->line % 8) {
2473 printk("secondary port\n");
2474 /* 8 port cards can't do DMA */
2475 info->stat_flags |= ESP_STAT_NEVER_DMA;
2478 last_primary->stat_flags |= ESP_STAT_NEVER_DMA;
2480 printk("primary port\n");
2481 last_primary = info;
2486 info->stat_flags |= ESP_STAT_NEVER_DMA;
2488 info = kzalloc(sizeof(struct esp_struct), GFP_KERNEL);
2491 printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
2493 /* allow use of the already detected ports */
2497 spin_lock_init(&info->lock);
2498 /* rx_trigger, tx_trigger are needed by autoconfig */
2499 info->config.rx_trigger = rx_trigger;
2500 info->config.tx_trigger = tx_trigger;
2508 } while (i < NR_PRIMARY);
2510 /* free the last port memory allocation */
2516 static void __exit espserial_exit(void)
2519 struct esp_struct *temp_async;
2520 struct esp_pio_buffer *pio_buf;
2522 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2523 if ((e1 = tty_unregister_driver(esp_driver)))
2524 printk("SERIAL: failed to unregister serial driver (%d)\n",
2526 put_tty_driver(esp_driver);
2530 release_region(ports->port, REGION_SIZE);
2532 temp_async = ports->next_port;
2538 free_pages((unsigned long)dma_buffer,
2539 get_order(DMA_BUFFER_SZ));
2541 while (free_pio_buf) {
2542 pio_buf = free_pio_buf->next;
2543 kfree(free_pio_buf);
2544 free_pio_buf = pio_buf;
2548 module_init(espserial_init);
2549 module_exit(espserial_exit);