2 * esp.c - driver for Hayes ESP serial cards
4 * --- Notices from serial.c, upon which this driver is based ---
6 * Copyright (C) 1991, 1992 Linus Torvalds
8 * Extensively rewritten by Theodore Ts'o, 8/16/92 -- 9/14/92. Now
9 * much more extensible to support other serial cards based on the
10 * 16450/16550A UART's. Added support for the AST FourPort and the
13 * set_serial_info fixed to set the flags, custom divisor, and uart
14 * type fields. Fix suggested by Michael K. Johnson 12/12/92.
16 * 11/95: TIOCMIWAIT, TIOCGICOUNT by Angelo Haritsis <ah@doc.ic.ac.uk>
18 * 03/96: Modularised by Angelo Haritsis <ah@doc.ic.ac.uk>
20 * rs_set_termios fixed to look also for changes of the input
21 * flags INPCK, BRKINT, PARMRK, IGNPAR and IGNBRK.
22 * Bernd Anh�pl 05/17/96.
24 * --- End of notices from serial.c ---
26 * Support for the ESP serial card by Andrew J. Robinson
27 * <arobinso@nyx.net> (Card detection routine taken from a patch
28 * by Dennis J. Boylan). Patches to allow use with 2.1.x contributed
31 * Most recent changes: (Andrew J. Robinson)
32 * Support for PIO mode. This allows the driver to work properly with
35 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> -
36 * several cleanups, use module_init/module_exit, etc
38 * This module exports the following rs232 io functions:
40 * int espserial_init(void);
43 #include <linux/module.h>
44 #include <linux/errno.h>
45 #include <linux/signal.h>
46 #include <linux/sched.h>
47 #include <linux/interrupt.h>
48 #include <linux/tty.h>
49 #include <linux/tty_flip.h>
50 #include <linux/serial.h>
51 #include <linux/serialP.h>
52 #include <linux/serial_reg.h>
53 #include <linux/major.h>
54 #include <linux/string.h>
55 #include <linux/fcntl.h>
56 #include <linux/ptrace.h>
57 #include <linux/ioport.h>
59 #include <linux/init.h>
60 #include <linux/delay.h>
62 #include <asm/system.h>
64 #include <asm/bitops.h>
67 #include <linux/slab.h>
68 #include <asm/uaccess.h>
70 #include <linux/hayesesp.h>
72 #define NR_PORTS 64 /* maximum number of ports */
73 #define NR_PRIMARY 8 /* maximum number of primary ports */
74 #define REGION_SIZE 8 /* size of io region to request */
76 /* The following variables can be set by giving module options */
77 static int irq[NR_PRIMARY]; /* IRQ for each base port */
78 static unsigned int divisor[NR_PRIMARY]; /* custom divisor for each port */
79 static unsigned int dma = ESP_DMA_CHANNEL; /* DMA channel */
80 static unsigned int rx_trigger = ESP_RX_TRIGGER;
81 static unsigned int tx_trigger = ESP_TX_TRIGGER;
82 static unsigned int flow_off = ESP_FLOW_OFF;
83 static unsigned int flow_on = ESP_FLOW_ON;
84 static unsigned int rx_timeout = ESP_RX_TMOUT;
85 static unsigned int pio_threshold = ESP_PIO_THRESHOLD;
87 MODULE_LICENSE("GPL");
89 module_param_array(irq, int, NULL, 0);
90 module_param_array(divisor, uint, NULL, 0);
91 module_param(dma, uint, 0);
92 module_param(rx_trigger, uint, 0);
93 module_param(tx_trigger, uint, 0);
94 module_param(flow_off, uint, 0);
95 module_param(flow_on, uint, 0);
96 module_param(rx_timeout, uint, 0);
97 module_param(pio_threshold, uint, 0);
101 static char *dma_buffer;
102 static int dma_bytes;
103 static struct esp_pio_buffer *free_pio_buf;
105 #define DMA_BUFFER_SZ 1024
107 #define WAKEUP_CHARS 1024
109 static char serial_name[] __initdata = "ESP serial driver";
110 static char serial_version[] __initdata = "2.2";
112 static struct tty_driver *esp_driver;
114 /* serial subtype definitions */
115 #define SERIAL_TYPE_NORMAL 1
118 * Serial driver configuration section. Here are the various options:
120 * SERIAL_PARANOIA_CHECK
121 * Check the magic number for the esp_structure where
125 #undef SERIAL_PARANOIA_CHECK
126 #define SERIAL_DO_RESTART
128 #undef SERIAL_DEBUG_INTR
129 #undef SERIAL_DEBUG_OPEN
130 #undef SERIAL_DEBUG_FLOW
132 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
133 #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
134 tty->name, (info->flags), serial_driver.refcount,info->count,tty->count,s)
139 static struct esp_struct *ports;
141 static void change_speed(struct esp_struct *info);
142 static void rs_wait_until_sent(struct tty_struct *, int);
145 * The ESP card has a clock rate of 14.7456 MHz (that is, 2**ESPC_SCALE
146 * times the normal 1.8432 Mhz clock of most serial boards).
148 #define BASE_BAUD ((1843200 / 16) * (1 << ESPC_SCALE))
150 /* Standard COM flags (except for COM4, because of the 8514 problem) */
151 #define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
154 * tmp_buf is used as a temporary buffer by serial_write. We need to
155 * lock it in case the memcpy_fromfs blocks while swapping in a page,
156 * and some other program tries to do a serial write at the same time.
157 * Since the lock will only come under contention when the system is
158 * swapping and available memory is low, it makes sense to share one
159 * buffer across all the serial ports, since it significantly saves
160 * memory if large numbers of serial ports are open.
162 static unsigned char *tmp_buf;
164 static inline int serial_paranoia_check(struct esp_struct *info,
165 char *name, const char *routine)
167 #ifdef SERIAL_PARANOIA_CHECK
168 static const char badmagic[] = KERN_WARNING
169 "Warning: bad magic number for serial struct (%s) in %s\n";
170 static const char badinfo[] = KERN_WARNING
171 "Warning: null esp_struct for (%s) in %s\n";
174 printk(badinfo, name, routine);
177 if (info->magic != ESP_MAGIC) {
178 printk(badmagic, name, routine);
185 static inline unsigned int serial_in(struct esp_struct *info, int offset)
187 return inb(info->port + offset);
190 static inline void serial_out(struct esp_struct *info, int offset,
193 outb(value, info->port+offset);
197 * ------------------------------------------------------------
198 * rs_stop() and rs_start()
200 * This routines are called before setting or resetting tty->stopped.
201 * They enable or disable transmitter interrupts, as necessary.
202 * ------------------------------------------------------------
204 static void rs_stop(struct tty_struct *tty)
206 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
209 if (serial_paranoia_check(info, tty->name, "rs_stop"))
212 spin_lock_irqsave(&info->lock, flags);
213 if (info->IER & UART_IER_THRI) {
214 info->IER &= ~UART_IER_THRI;
215 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
216 serial_out(info, UART_ESI_CMD2, info->IER);
218 spin_unlock_irqrestore(&info->lock, flags);
221 static void rs_start(struct tty_struct *tty)
223 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
226 if (serial_paranoia_check(info, tty->name, "rs_start"))
229 spin_lock_irqsave(&info->lock, flags);
230 if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
231 info->IER |= UART_IER_THRI;
232 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
233 serial_out(info, UART_ESI_CMD2, info->IER);
235 spin_unlock_irqrestore(&info->lock, flags);
239 * ----------------------------------------------------------------------
241 * Here starts the interrupt handling routines. All of the following
242 * subroutines are declared as inline and are folded into
243 * rs_interrupt(). They were separated out for readability's sake.
245 * Note: rs_interrupt() is a "fast" interrupt, which means that it
246 * runs with interrupts turned off. People who may want to modify
247 * rs_interrupt() should try to keep the interrupt handler as fast as
248 * possible. After you are done making modifications, it is not a bad
251 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
253 * and look at the resulting assemble code in serial.s.
255 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
256 * -----------------------------------------------------------------------
260 * This routine is used by the interrupt handler to schedule
261 * processing in the software interrupt portion of the driver.
263 static inline void rs_sched_event(struct esp_struct *info,
266 info->event |= 1 << event;
267 schedule_work(&info->tqueue);
270 static DEFINE_SPINLOCK(pio_lock);
272 static inline struct esp_pio_buffer *get_pio_buffer(void)
274 struct esp_pio_buffer *buf;
277 spin_lock_irqsave(&pio_lock, flags);
280 free_pio_buf = buf->next;
282 buf = kmalloc(sizeof(struct esp_pio_buffer), GFP_ATOMIC);
284 spin_unlock_irqrestore(&pio_lock, flags);
288 static inline void release_pio_buffer(struct esp_pio_buffer *buf)
291 spin_lock_irqsave(&pio_lock, flags);
292 buf->next = free_pio_buf;
294 spin_unlock_irqrestore(&pio_lock, flags);
297 static inline void receive_chars_pio(struct esp_struct *info, int num_bytes)
299 struct tty_struct *tty = info->tty;
301 struct esp_pio_buffer *pio_buf;
302 struct esp_pio_buffer *err_buf;
303 unsigned char status_mask;
305 pio_buf = get_pio_buffer();
310 err_buf = get_pio_buffer();
313 release_pio_buffer(pio_buf);
317 status_mask = (info->read_status_mask >> 2) & 0x07;
319 for (i = 0; i < num_bytes - 1; i += 2) {
320 *((unsigned short *)(pio_buf->data + i)) =
321 inw(info->port + UART_ESI_RX);
322 err_buf->data[i] = serial_in(info, UART_ESI_RWS);
323 err_buf->data[i + 1] = (err_buf->data[i] >> 3) & status_mask;
324 err_buf->data[i] &= status_mask;
327 if (num_bytes & 0x0001) {
328 pio_buf->data[num_bytes - 1] = serial_in(info, UART_ESI_RX);
329 err_buf->data[num_bytes - 1] =
330 (serial_in(info, UART_ESI_RWS) >> 3) & status_mask;
333 /* make sure everything is still ok since interrupts were enabled */
337 release_pio_buffer(pio_buf);
338 release_pio_buffer(err_buf);
339 info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
343 status_mask = (info->ignore_status_mask >> 2) & 0x07;
345 for (i = 0; i < num_bytes; i++) {
346 if (!(err_buf->data[i] & status_mask)) {
349 if (err_buf->data[i] & 0x04) {
351 if (info->flags & ASYNC_SAK)
354 else if (err_buf->data[i] & 0x02)
356 else if (err_buf->data[i] & 0x01)
358 tty_insert_flip_char(tty, pio_buf->data[i], flag);
362 tty_schedule_flip(tty);
364 info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
365 release_pio_buffer(pio_buf);
366 release_pio_buffer(err_buf);
369 static inline void receive_chars_dma(struct esp_struct *info, int num_bytes)
372 info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
373 dma_bytes = num_bytes;
374 info->stat_flags |= ESP_STAT_DMA_RX;
376 flags=claim_dma_lock();
379 set_dma_mode(dma, DMA_MODE_READ);
380 set_dma_addr(dma, isa_virt_to_bus(dma_buffer));
381 set_dma_count(dma, dma_bytes);
383 release_dma_lock(flags);
385 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_RX);
388 static inline void receive_chars_dma_done(struct esp_struct *info,
391 struct tty_struct *tty = info->tty;
395 flags=claim_dma_lock();
399 info->stat_flags &= ~ESP_STAT_DMA_RX;
400 num_bytes = dma_bytes - get_dma_residue(dma);
401 release_dma_lock(flags);
403 info->icount.rx += num_bytes;
406 tty_insert_flip_string(tty, dma_buffer, num_bytes - 1);
408 status &= (0x1c & info->read_status_mask);
410 /* Is the status significant or do we throw the last byte ? */
411 if (!(status & info->ignore_status_mask)) {
415 statflag = TTY_BREAK;
416 (info->icount.brk)++;
417 if (info->flags & ASYNC_SAK)
419 } else if (status & 0x08) {
420 statflag = TTY_FRAME;
421 (info->icount.frame)++;
423 else if (status & 0x04) {
424 statflag = TTY_PARITY;
425 (info->icount.parity)++;
427 tty_insert_flip_char(tty, dma_buffer[num_bytes - 1], statflag);
429 tty_schedule_flip(tty);
432 if (dma_bytes != num_bytes) {
433 num_bytes = dma_bytes - num_bytes;
435 receive_chars_dma(info, num_bytes);
440 /* Caller must hold info->lock */
442 static inline void transmit_chars_pio(struct esp_struct *info,
446 struct esp_pio_buffer *pio_buf;
448 pio_buf = get_pio_buffer();
453 while (space_avail && info->xmit_cnt) {
454 if (info->xmit_tail + space_avail <= ESP_XMIT_SIZE) {
455 memcpy(pio_buf->data,
456 &(info->xmit_buf[info->xmit_tail]),
459 i = ESP_XMIT_SIZE - info->xmit_tail;
460 memcpy(pio_buf->data,
461 &(info->xmit_buf[info->xmit_tail]), i);
462 memcpy(&(pio_buf->data[i]), info->xmit_buf,
466 info->xmit_cnt -= space_avail;
467 info->xmit_tail = (info->xmit_tail + space_avail) &
470 for (i = 0; i < space_avail - 1; i += 2) {
471 outw(*((unsigned short *)(pio_buf->data + i)),
472 info->port + UART_ESI_TX);
475 if (space_avail & 0x0001)
476 serial_out(info, UART_ESI_TX,
477 pio_buf->data[space_avail - 1]);
479 if (info->xmit_cnt) {
480 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
481 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
482 space_avail = serial_in(info, UART_ESI_STAT1) << 8;
483 space_avail |= serial_in(info, UART_ESI_STAT2);
485 if (space_avail > info->xmit_cnt)
486 space_avail = info->xmit_cnt;
490 if (info->xmit_cnt < WAKEUP_CHARS) {
491 rs_sched_event(info, ESP_EVENT_WRITE_WAKEUP);
493 #ifdef SERIAL_DEBUG_INTR
497 if (info->xmit_cnt <= 0) {
498 info->IER &= ~UART_IER_THRI;
499 serial_out(info, UART_ESI_CMD1,
501 serial_out(info, UART_ESI_CMD2, info->IER);
505 release_pio_buffer(pio_buf);
508 /* Caller must hold info->lock */
509 static inline void transmit_chars_dma(struct esp_struct *info, int num_bytes)
513 dma_bytes = num_bytes;
515 if (info->xmit_tail + dma_bytes <= ESP_XMIT_SIZE) {
516 memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
519 int i = ESP_XMIT_SIZE - info->xmit_tail;
520 memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
522 memcpy(&(dma_buffer[i]), info->xmit_buf, dma_bytes - i);
525 info->xmit_cnt -= dma_bytes;
526 info->xmit_tail = (info->xmit_tail + dma_bytes) & (ESP_XMIT_SIZE - 1);
528 if (info->xmit_cnt < WAKEUP_CHARS) {
529 rs_sched_event(info, ESP_EVENT_WRITE_WAKEUP);
531 #ifdef SERIAL_DEBUG_INTR
535 if (info->xmit_cnt <= 0) {
536 info->IER &= ~UART_IER_THRI;
537 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
538 serial_out(info, UART_ESI_CMD2, info->IER);
542 info->stat_flags |= ESP_STAT_DMA_TX;
544 flags=claim_dma_lock();
547 set_dma_mode(dma, DMA_MODE_WRITE);
548 set_dma_addr(dma, isa_virt_to_bus(dma_buffer));
549 set_dma_count(dma, dma_bytes);
551 release_dma_lock(flags);
553 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
556 static inline void transmit_chars_dma_done(struct esp_struct *info)
562 flags=claim_dma_lock();
566 num_bytes = dma_bytes - get_dma_residue(dma);
567 info->icount.tx += dma_bytes;
568 release_dma_lock(flags);
570 if (dma_bytes != num_bytes) {
571 dma_bytes -= num_bytes;
572 memmove(dma_buffer, dma_buffer + num_bytes, dma_bytes);
574 flags=claim_dma_lock();
577 set_dma_mode(dma, DMA_MODE_WRITE);
578 set_dma_addr(dma, isa_virt_to_bus(dma_buffer));
579 set_dma_count(dma, dma_bytes);
581 release_dma_lock(flags);
583 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
586 info->stat_flags &= ~ESP_STAT_DMA_TX;
590 static inline void check_modem_status(struct esp_struct *info)
594 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
595 status = serial_in(info, UART_ESI_STAT2);
597 if (status & UART_MSR_ANY_DELTA) {
598 /* update input line counters */
599 if (status & UART_MSR_TERI)
601 if (status & UART_MSR_DDSR)
603 if (status & UART_MSR_DDCD)
605 if (status & UART_MSR_DCTS)
607 wake_up_interruptible(&info->delta_msr_wait);
610 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
611 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
612 printk("ttys%d CD now %s...", info->line,
613 (status & UART_MSR_DCD) ? "on" : "off");
615 if (status & UART_MSR_DCD)
616 wake_up_interruptible(&info->open_wait);
618 #ifdef SERIAL_DEBUG_OPEN
619 printk("scheduling hangup...");
621 schedule_work(&info->tqueue_hangup);
627 * This is the serial driver's interrupt routine
629 static irqreturn_t rs_interrupt_single(int irq, void *dev_id,
630 struct pt_regs *regs)
632 struct esp_struct * info;
634 unsigned int scratch;
636 #ifdef SERIAL_DEBUG_INTR
637 printk("rs_interrupt_single(%d)...", irq);
639 info = (struct esp_struct *)dev_id;
641 scratch = serial_in(info, UART_ESI_SID);
643 spin_lock(&info->lock);
646 spin_unlock(&info->lock);
650 if (scratch & 0x04) { /* error */
651 serial_out(info, UART_ESI_CMD1, ESI_GET_ERR_STAT);
652 err_status = serial_in(info, UART_ESI_STAT1);
653 serial_in(info, UART_ESI_STAT2);
655 if (err_status & 0x01)
656 info->stat_flags |= ESP_STAT_RX_TIMEOUT;
658 if (err_status & 0x20) /* UART status */
659 check_modem_status(info);
661 if (err_status & 0x80) /* Start break */
662 wake_up_interruptible(&info->break_wait);
665 if ((scratch & 0x88) || /* DMA completed or timed out */
666 (err_status & 0x1c) /* receive error */) {
667 if (info->stat_flags & ESP_STAT_DMA_RX)
668 receive_chars_dma_done(info, err_status);
669 else if (info->stat_flags & ESP_STAT_DMA_TX)
670 transmit_chars_dma_done(info);
673 if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
674 ((scratch & 0x01) || (info->stat_flags & ESP_STAT_RX_TIMEOUT)) &&
675 (info->IER & UART_IER_RDI)) {
678 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
679 serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
680 num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
681 num_bytes |= serial_in(info, UART_ESI_STAT2);
683 num_bytes = tty_buffer_request_room(info->tty, num_bytes);
687 (info->stat_flags & ESP_STAT_USE_PIO) ||
688 (num_bytes <= info->config.pio_threshold))
689 receive_chars_pio(info, num_bytes);
691 receive_chars_dma(info, num_bytes);
695 if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
696 (scratch & 0x02) && (info->IER & UART_IER_THRI)) {
697 if ((info->xmit_cnt <= 0) || info->tty->stopped) {
698 info->IER &= ~UART_IER_THRI;
699 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
700 serial_out(info, UART_ESI_CMD2, info->IER);
704 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
705 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
706 num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
707 num_bytes |= serial_in(info, UART_ESI_STAT2);
709 if (num_bytes > info->xmit_cnt)
710 num_bytes = info->xmit_cnt;
714 (info->stat_flags & ESP_STAT_USE_PIO) ||
715 (num_bytes <= info->config.pio_threshold))
716 transmit_chars_pio(info, num_bytes);
718 transmit_chars_dma(info, num_bytes);
723 info->last_active = jiffies;
725 #ifdef SERIAL_DEBUG_INTR
728 spin_unlock(&info->lock);
733 * -------------------------------------------------------------------
734 * Here ends the serial interrupt routines.
735 * -------------------------------------------------------------------
738 static void do_softint(void *private_)
740 struct esp_struct *info = (struct esp_struct *) private_;
741 struct tty_struct *tty;
747 if (test_and_clear_bit(ESP_EVENT_WRITE_WAKEUP, &info->event)) {
753 * This routine is called from the scheduler tqueue when the interrupt
754 * routine has signalled that a hangup has occurred. The path of
755 * hangup processing is:
757 * serial interrupt routine -> (scheduler tqueue) ->
758 * do_serial_hangup() -> tty->hangup() -> esp_hangup()
761 static void do_serial_hangup(void *private_)
763 struct esp_struct *info = (struct esp_struct *) private_;
764 struct tty_struct *tty;
772 * ---------------------------------------------------------------
773 * Low level utility subroutines for the serial driver: routines to
774 * figure out the appropriate timeout for an interrupt chain, routines
775 * to initialize and startup a serial port, and routines to shutdown a
776 * serial port. Useful stuff like that.
778 * Caller should hold lock
779 * ---------------------------------------------------------------
782 static inline void esp_basic_init(struct esp_struct * info)
784 /* put ESPC in enhanced mode */
785 serial_out(info, UART_ESI_CMD1, ESI_SET_MODE);
787 if (info->stat_flags & ESP_STAT_NEVER_DMA)
788 serial_out(info, UART_ESI_CMD2, 0x01);
790 serial_out(info, UART_ESI_CMD2, 0x31);
792 /* disable interrupts for now */
793 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
794 serial_out(info, UART_ESI_CMD2, 0x00);
796 /* set interrupt and DMA channel */
797 serial_out(info, UART_ESI_CMD1, ESI_SET_IRQ);
799 if (info->stat_flags & ESP_STAT_NEVER_DMA)
800 serial_out(info, UART_ESI_CMD2, 0x01);
802 serial_out(info, UART_ESI_CMD2, (dma << 4) | 0x01);
804 serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
806 if (info->line % 8) /* secondary port */
807 serial_out(info, UART_ESI_CMD2, 0x0d); /* shared */
808 else if (info->irq == 9)
809 serial_out(info, UART_ESI_CMD2, 0x02);
811 serial_out(info, UART_ESI_CMD2, info->irq);
813 /* set error status mask (check this) */
814 serial_out(info, UART_ESI_CMD1, ESI_SET_ERR_MASK);
816 if (info->stat_flags & ESP_STAT_NEVER_DMA)
817 serial_out(info, UART_ESI_CMD2, 0xa1);
819 serial_out(info, UART_ESI_CMD2, 0xbd);
821 serial_out(info, UART_ESI_CMD2, 0x00);
823 /* set DMA timeout */
824 serial_out(info, UART_ESI_CMD1, ESI_SET_DMA_TMOUT);
825 serial_out(info, UART_ESI_CMD2, 0xff);
827 /* set FIFO trigger levels */
828 serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
829 serial_out(info, UART_ESI_CMD2, info->config.rx_trigger >> 8);
830 serial_out(info, UART_ESI_CMD2, info->config.rx_trigger);
831 serial_out(info, UART_ESI_CMD2, info->config.tx_trigger >> 8);
832 serial_out(info, UART_ESI_CMD2, info->config.tx_trigger);
834 /* Set clock scaling and wait states */
835 serial_out(info, UART_ESI_CMD1, ESI_SET_PRESCALAR);
836 serial_out(info, UART_ESI_CMD2, 0x04 | ESPC_SCALE);
838 /* set reinterrupt pacing */
839 serial_out(info, UART_ESI_CMD1, ESI_SET_REINTR);
840 serial_out(info, UART_ESI_CMD2, 0xff);
843 static int startup(struct esp_struct * info)
847 unsigned int num_chars;
849 spin_lock_irqsave(&info->lock, flags);
851 if (info->flags & ASYNC_INITIALIZED)
854 if (!info->xmit_buf) {
855 info->xmit_buf = (unsigned char *)get_zeroed_page(GFP_ATOMIC);
861 #ifdef SERIAL_DEBUG_OPEN
862 printk("starting up ttys%d (irq %d)...", info->line, info->irq);
865 /* Flush the RX buffer. Using the ESI flush command may cause */
866 /* wild interrupts, so read all the data instead. */
868 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
869 serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
870 num_chars = serial_in(info, UART_ESI_STAT1) << 8;
871 num_chars |= serial_in(info, UART_ESI_STAT2);
873 while (num_chars > 1) {
874 inw(info->port + UART_ESI_RX);
879 serial_in(info, UART_ESI_RX);
881 /* set receive character timeout */
882 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
883 serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
885 /* clear all flags except the "never DMA" flag */
886 info->stat_flags &= ESP_STAT_NEVER_DMA;
888 if (info->stat_flags & ESP_STAT_NEVER_DMA)
889 info->stat_flags |= ESP_STAT_USE_PIO;
891 spin_unlock_irqrestore(&info->lock, flags);
897 retval = request_irq(info->irq, rs_interrupt_single, SA_SHIRQ,
901 if (capable(CAP_SYS_ADMIN)) {
903 set_bit(TTY_IO_ERROR,
910 if (!(info->stat_flags & ESP_STAT_USE_PIO) && !dma_buffer) {
911 dma_buffer = (char *)__get_dma_pages(
912 GFP_KERNEL, get_order(DMA_BUFFER_SZ));
914 /* use PIO mode if DMA buf/chan cannot be allocated */
916 info->stat_flags |= ESP_STAT_USE_PIO;
917 else if (request_dma(dma, "esp serial")) {
918 free_pages((unsigned long)dma_buffer,
919 get_order(DMA_BUFFER_SZ));
921 info->stat_flags |= ESP_STAT_USE_PIO;
926 info->MCR = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
928 spin_lock_irqsave(&info->lock, flags);
929 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
930 serial_out(info, UART_ESI_CMD2, UART_MCR);
931 serial_out(info, UART_ESI_CMD2, info->MCR);
934 * Finally, enable interrupts
936 /* info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI; */
937 info->IER = UART_IER_RLSI | UART_IER_RDI | UART_IER_DMA_TMOUT |
939 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
940 serial_out(info, UART_ESI_CMD2, info->IER);
943 clear_bit(TTY_IO_ERROR, &info->tty->flags);
944 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
945 spin_unlock_irqrestore(&info->lock, flags);
948 * Set up the tty->alt_speed kludge
951 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
952 info->tty->alt_speed = 57600;
953 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
954 info->tty->alt_speed = 115200;
955 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
956 info->tty->alt_speed = 230400;
957 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
958 info->tty->alt_speed = 460800;
962 * set the speed of the serial port
965 info->flags |= ASYNC_INITIALIZED;
969 spin_unlock_irqrestore(&info->lock, flags);
975 * This routine will shutdown a serial port; interrupts are disabled, and
976 * DTR is dropped if the hangup on close termio flag is on.
978 static void shutdown(struct esp_struct * info)
980 unsigned long flags, f;
982 if (!(info->flags & ASYNC_INITIALIZED))
985 #ifdef SERIAL_DEBUG_OPEN
986 printk("Shutting down serial port %d (irq %d)....", info->line,
990 spin_lock_irqsave(&info->lock, flags);
992 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
993 * here so the queue might never be waken up
995 wake_up_interruptible(&info->delta_msr_wait);
996 wake_up_interruptible(&info->break_wait);
998 /* stop a DMA transfer on the port being closed */
999 /* DMA lock is higher priority always */
1000 if (info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) {
1004 release_dma_lock(f);
1012 free_irq(info->irq, info);
1015 struct esp_struct *current_port = ports;
1017 while (current_port) {
1018 if ((current_port != info) &&
1019 (current_port->flags & ASYNC_INITIALIZED))
1022 current_port = current_port->next_port;
1025 if (!current_port) {
1027 free_pages((unsigned long)dma_buffer,
1028 get_order(DMA_BUFFER_SZ));
1033 if (info->xmit_buf) {
1034 free_page((unsigned long) info->xmit_buf);
1035 info->xmit_buf = NULL;
1039 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1040 serial_out(info, UART_ESI_CMD2, 0x00);
1042 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
1043 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1045 info->MCR &= ~UART_MCR_OUT2;
1046 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1047 serial_out(info, UART_ESI_CMD2, UART_MCR);
1048 serial_out(info, UART_ESI_CMD2, info->MCR);
1051 set_bit(TTY_IO_ERROR, &info->tty->flags);
1053 info->flags &= ~ASYNC_INITIALIZED;
1054 spin_unlock_irqrestore(&info->lock, flags);
1058 * This routine is called to set the UART divisor registers to match
1059 * the specified baud rate for a serial port.
1061 static void change_speed(struct esp_struct *info)
1063 unsigned short port;
1065 unsigned cflag,cval;
1067 unsigned char flow1 = 0, flow2 = 0;
1068 unsigned long flags;
1070 if (!info->tty || !info->tty->termios)
1072 cflag = info->tty->termios->c_cflag;
1075 /* byte size and parity */
1076 switch (cflag & CSIZE) {
1077 case CS5: cval = 0x00; bits = 7; break;
1078 case CS6: cval = 0x01; bits = 8; break;
1079 case CS7: cval = 0x02; bits = 9; break;
1080 case CS8: cval = 0x03; bits = 10; break;
1081 default: cval = 0x00; bits = 7; break;
1083 if (cflag & CSTOPB) {
1087 if (cflag & PARENB) {
1088 cval |= UART_LCR_PARITY;
1091 if (!(cflag & PARODD))
1092 cval |= UART_LCR_EPAR;
1095 cval |= UART_LCR_SPAR;
1098 baud = tty_get_baud_rate(info->tty);
1099 if (baud == 38400 &&
1100 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1101 quot = info->custom_divisor;
1104 /* Special case since 134 is really 134.5 */
1105 quot = (2*BASE_BAUD / 269);
1107 quot = BASE_BAUD / baud;
1109 /* If the quotient is ever zero, default to 9600 bps */
1111 quot = BASE_BAUD / 9600;
1113 info->timeout = ((1024 * HZ * bits * quot) / BASE_BAUD) + (HZ / 50);
1115 /* CTS flow control flag and modem status interrupts */
1116 /* info->IER &= ~UART_IER_MSI; */
1117 if (cflag & CRTSCTS) {
1118 info->flags |= ASYNC_CTS_FLOW;
1119 /* info->IER |= UART_IER_MSI; */
1123 info->flags &= ~ASYNC_CTS_FLOW;
1125 info->flags &= ~ASYNC_CHECK_CD;
1127 info->flags |= ASYNC_CHECK_CD;
1128 /* info->IER |= UART_IER_MSI; */
1132 * Set up parity check flag
1134 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1136 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1137 if (I_INPCK(info->tty))
1138 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1139 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1140 info->read_status_mask |= UART_LSR_BI;
1142 info->ignore_status_mask = 0;
1144 /* This should be safe, but for some broken bits of hardware... */
1145 if (I_IGNPAR(info->tty)) {
1146 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1147 info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
1150 if (I_IGNBRK(info->tty)) {
1151 info->ignore_status_mask |= UART_LSR_BI;
1152 info->read_status_mask |= UART_LSR_BI;
1154 * If we're ignore parity and break indicators, ignore
1155 * overruns too. (For real raw support).
1157 if (I_IGNPAR(info->tty)) {
1158 info->ignore_status_mask |= UART_LSR_OE | \
1159 UART_LSR_PE | UART_LSR_FE;
1160 info->read_status_mask |= UART_LSR_OE | \
1161 UART_LSR_PE | UART_LSR_FE;
1165 if (I_IXOFF(info->tty))
1168 spin_lock_irqsave(&info->lock, flags);
1170 serial_out(info, UART_ESI_CMD1, ESI_SET_BAUD);
1171 serial_out(info, UART_ESI_CMD2, quot >> 8);
1172 serial_out(info, UART_ESI_CMD2, quot & 0xff);
1174 /* set data bits, parity, etc. */
1175 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1176 serial_out(info, UART_ESI_CMD2, UART_LCR);
1177 serial_out(info, UART_ESI_CMD2, cval);
1179 /* Enable flow control */
1180 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CNTL);
1181 serial_out(info, UART_ESI_CMD2, flow1);
1182 serial_out(info, UART_ESI_CMD2, flow2);
1184 /* set flow control characters (XON/XOFF only) */
1185 if (I_IXOFF(info->tty)) {
1186 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CHARS);
1187 serial_out(info, UART_ESI_CMD2, START_CHAR(info->tty));
1188 serial_out(info, UART_ESI_CMD2, STOP_CHAR(info->tty));
1189 serial_out(info, UART_ESI_CMD2, 0x10);
1190 serial_out(info, UART_ESI_CMD2, 0x21);
1191 switch (cflag & CSIZE) {
1193 serial_out(info, UART_ESI_CMD2, 0x1f);
1196 serial_out(info, UART_ESI_CMD2, 0x3f);
1200 serial_out(info, UART_ESI_CMD2, 0x7f);
1203 serial_out(info, UART_ESI_CMD2, 0xff);
1208 /* Set high/low water */
1209 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1210 serial_out(info, UART_ESI_CMD2, info->config.flow_off >> 8);
1211 serial_out(info, UART_ESI_CMD2, info->config.flow_off);
1212 serial_out(info, UART_ESI_CMD2, info->config.flow_on >> 8);
1213 serial_out(info, UART_ESI_CMD2, info->config.flow_on);
1215 spin_unlock_irqrestore(&info->lock, flags);
1218 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
1220 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1221 unsigned long flags;
1223 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
1226 if (!tty || !info->xmit_buf)
1229 spin_lock_irqsave(&info->lock, flags);
1230 if (info->xmit_cnt < ESP_XMIT_SIZE - 1) {
1231 info->xmit_buf[info->xmit_head++] = ch;
1232 info->xmit_head &= ESP_XMIT_SIZE-1;
1235 spin_unlock_irqrestore(&info->lock, flags);
1238 static void rs_flush_chars(struct tty_struct *tty)
1240 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1241 unsigned long flags;
1243 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
1246 spin_lock_irqsave(&info->lock, flags);
1248 if (info->xmit_cnt <= 0 || tty->stopped || !info->xmit_buf)
1251 if (!(info->IER & UART_IER_THRI)) {
1252 info->IER |= UART_IER_THRI;
1253 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1254 serial_out(info, UART_ESI_CMD2, info->IER);
1257 spin_unlock_irqrestore(&info->lock, flags);
1260 static int rs_write(struct tty_struct * tty,
1261 const unsigned char *buf, int count)
1264 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1265 unsigned long flags;
1267 if (serial_paranoia_check(info, tty->name, "rs_write"))
1270 if (!tty || !info->xmit_buf || !tmp_buf)
1274 /* Thanks to R. Wolff for suggesting how to do this with */
1275 /* interrupts enabled */
1278 t = ESP_XMIT_SIZE - info->xmit_cnt - 1;
1283 t = ESP_XMIT_SIZE - info->xmit_head;
1291 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1293 info->xmit_head = (info->xmit_head + c) & (ESP_XMIT_SIZE-1);
1294 info->xmit_cnt += c;
1300 spin_lock_irqsave(&info->lock, flags);
1302 if (info->xmit_cnt && !tty->stopped && !(info->IER & UART_IER_THRI)) {
1303 info->IER |= UART_IER_THRI;
1304 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1305 serial_out(info, UART_ESI_CMD2, info->IER);
1308 spin_unlock_irqrestore(&info->lock, flags);
1312 static int rs_write_room(struct tty_struct *tty)
1314 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1316 unsigned long flags;
1318 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
1321 spin_lock_irqsave(&info->lock, flags);
1323 ret = ESP_XMIT_SIZE - info->xmit_cnt - 1;
1326 spin_unlock_irqrestore(&info->lock, flags);
1330 static int rs_chars_in_buffer(struct tty_struct *tty)
1332 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1334 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
1336 return info->xmit_cnt;
1339 static void rs_flush_buffer(struct tty_struct *tty)
1341 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1342 unsigned long flags;
1344 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
1346 spin_lock_irqsave(&info->lock, flags);
1347 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1348 spin_unlock_irqrestore(&info->lock, flags);
1353 * ------------------------------------------------------------
1356 * This routine is called by the upper-layer tty layer to signal that
1357 * incoming characters should be throttled.
1358 * ------------------------------------------------------------
1360 static void rs_throttle(struct tty_struct * tty)
1362 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1363 unsigned long flags;
1364 #ifdef SERIAL_DEBUG_THROTTLE
1367 printk("throttle %s: %d....\n", tty_name(tty, buf),
1368 tty->ldisc.chars_in_buffer(tty));
1371 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
1374 spin_lock_irqsave(&info->lock, flags);
1375 info->IER &= ~UART_IER_RDI;
1376 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1377 serial_out(info, UART_ESI_CMD2, info->IER);
1378 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1379 serial_out(info, UART_ESI_CMD2, 0x00);
1380 spin_unlock_irqrestore(&info->lock, flags);
1383 static void rs_unthrottle(struct tty_struct * tty)
1385 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1386 unsigned long flags;
1387 #ifdef SERIAL_DEBUG_THROTTLE
1390 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1391 tty->ldisc.chars_in_buffer(tty));
1394 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1397 spin_lock_irqsave(&info->lock, flags);
1398 info->IER |= UART_IER_RDI;
1399 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1400 serial_out(info, UART_ESI_CMD2, info->IER);
1401 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1402 serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
1403 spin_unlock_irqrestore(&info->lock, flags);
1407 * ------------------------------------------------------------
1408 * rs_ioctl() and friends
1409 * ------------------------------------------------------------
1412 static int get_serial_info(struct esp_struct * info,
1413 struct serial_struct __user *retinfo)
1415 struct serial_struct tmp;
1417 memset(&tmp, 0, sizeof(tmp));
1418 tmp.type = PORT_16550A;
1419 tmp.line = info->line;
1420 tmp.port = info->port;
1421 tmp.irq = info->irq;
1422 tmp.flags = info->flags;
1423 tmp.xmit_fifo_size = 1024;
1424 tmp.baud_base = BASE_BAUD;
1425 tmp.close_delay = info->close_delay;
1426 tmp.closing_wait = info->closing_wait;
1427 tmp.custom_divisor = info->custom_divisor;
1429 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1434 static int get_esp_config(struct esp_struct * info,
1435 struct hayes_esp_config __user *retinfo)
1437 struct hayes_esp_config tmp;
1442 memset(&tmp, 0, sizeof(tmp));
1443 tmp.rx_timeout = info->config.rx_timeout;
1444 tmp.rx_trigger = info->config.rx_trigger;
1445 tmp.tx_trigger = info->config.tx_trigger;
1446 tmp.flow_off = info->config.flow_off;
1447 tmp.flow_on = info->config.flow_on;
1448 tmp.pio_threshold = info->config.pio_threshold;
1449 tmp.dma_channel = (info->stat_flags & ESP_STAT_NEVER_DMA ? 0 : dma);
1451 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
1454 static int set_serial_info(struct esp_struct * info,
1455 struct serial_struct __user *new_info)
1457 struct serial_struct new_serial;
1458 struct esp_struct old_info;
1459 unsigned int change_irq;
1461 struct esp_struct *current_async;
1463 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1467 if ((new_serial.type != PORT_16550A) ||
1468 (new_serial.hub6) ||
1469 (info->port != new_serial.port) ||
1470 (new_serial.baud_base != BASE_BAUD) ||
1471 (new_serial.irq > 15) ||
1472 (new_serial.irq < 2) ||
1473 (new_serial.irq == 6) ||
1474 (new_serial.irq == 8) ||
1475 (new_serial.irq == 13))
1478 change_irq = new_serial.irq != info->irq;
1480 if (change_irq && (info->line % 8))
1483 if (!capable(CAP_SYS_ADMIN)) {
1485 (new_serial.close_delay != info->close_delay) ||
1486 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1487 (info->flags & ~ASYNC_USR_MASK)))
1489 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1490 (new_serial.flags & ASYNC_USR_MASK));
1491 info->custom_divisor = new_serial.custom_divisor;
1493 if (new_serial.irq == 2)
1497 current_async = ports;
1499 while (current_async) {
1500 if ((current_async->line >= info->line) &&
1501 (current_async->line < (info->line + 8))) {
1502 if (current_async == info) {
1503 if (current_async->count > 1)
1505 } else if (current_async->count)
1509 current_async = current_async->next_port;
1514 * OK, past this point, all the error checking has been done.
1515 * At this point, we start making changes.....
1518 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1519 (new_serial.flags & ASYNC_FLAGS));
1520 info->custom_divisor = new_serial.custom_divisor;
1521 info->close_delay = new_serial.close_delay * HZ/100;
1522 info->closing_wait = new_serial.closing_wait * HZ/100;
1526 * We need to shutdown the serial port at the old
1527 * port/irq combination.
1531 current_async = ports;
1533 while (current_async) {
1534 if ((current_async->line >= info->line) &&
1535 (current_async->line < (info->line + 8)))
1536 current_async->irq = new_serial.irq;
1538 current_async = current_async->next_port;
1541 serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
1543 serial_out(info, UART_ESI_CMD2, 0x02);
1545 serial_out(info, UART_ESI_CMD2, info->irq);
1549 if (info->flags & ASYNC_INITIALIZED) {
1550 if (((old_info.flags & ASYNC_SPD_MASK) !=
1551 (info->flags & ASYNC_SPD_MASK)) ||
1552 (old_info.custom_divisor != info->custom_divisor)) {
1553 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1554 info->tty->alt_speed = 57600;
1555 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1556 info->tty->alt_speed = 115200;
1557 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1558 info->tty->alt_speed = 230400;
1559 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1560 info->tty->alt_speed = 460800;
1564 retval = startup(info);
1569 static int set_esp_config(struct esp_struct * info,
1570 struct hayes_esp_config __user * new_info)
1572 struct hayes_esp_config new_config;
1573 unsigned int change_dma;
1575 struct esp_struct *current_async;
1576 unsigned long flags;
1578 /* Perhaps a non-sysadmin user should be able to do some of these */
1579 /* operations. I haven't decided yet. */
1581 if (!capable(CAP_SYS_ADMIN))
1584 if (copy_from_user(&new_config, new_info, sizeof(new_config)))
1587 if ((new_config.flow_on >= new_config.flow_off) ||
1588 (new_config.rx_trigger < 1) ||
1589 (new_config.tx_trigger < 1) ||
1590 (new_config.flow_off < 1) ||
1591 (new_config.flow_on < 1) ||
1592 (new_config.rx_trigger > 1023) ||
1593 (new_config.tx_trigger > 1023) ||
1594 (new_config.flow_off > 1023) ||
1595 (new_config.flow_on > 1023) ||
1596 (new_config.pio_threshold < 0) ||
1597 (new_config.pio_threshold > 1024))
1600 if ((new_config.dma_channel != 1) && (new_config.dma_channel != 3))
1601 new_config.dma_channel = 0;
1603 if (info->stat_flags & ESP_STAT_NEVER_DMA)
1604 change_dma = new_config.dma_channel;
1606 change_dma = (new_config.dma_channel != dma);
1609 if (new_config.dma_channel) {
1610 /* PIO mode to DMA mode transition OR */
1611 /* change current DMA channel */
1613 current_async = ports;
1615 while (current_async) {
1616 if (current_async == info) {
1617 if (current_async->count > 1)
1619 } else if (current_async->count)
1623 current_async->next_port;
1627 dma = new_config.dma_channel;
1628 info->stat_flags &= ~ESP_STAT_NEVER_DMA;
1630 /* all ports must use the same DMA channel */
1632 spin_lock_irqsave(&info->lock, flags);
1633 current_async = ports;
1635 while (current_async) {
1636 esp_basic_init(current_async);
1637 current_async = current_async->next_port;
1639 spin_unlock_irqrestore(&info->lock, flags);
1641 /* DMA mode to PIO mode only */
1643 if (info->count > 1)
1647 spin_lock_irqsave(&info->lock, flags);
1648 info->stat_flags |= ESP_STAT_NEVER_DMA;
1649 esp_basic_init(info);
1650 spin_unlock_irqrestore(&info->lock, flags);
1654 info->config.pio_threshold = new_config.pio_threshold;
1656 if ((new_config.flow_off != info->config.flow_off) ||
1657 (new_config.flow_on != info->config.flow_on)) {
1658 unsigned long flags;
1660 info->config.flow_off = new_config.flow_off;
1661 info->config.flow_on = new_config.flow_on;
1663 spin_lock_irqsave(&info->lock, flags);
1664 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1665 serial_out(info, UART_ESI_CMD2, new_config.flow_off >> 8);
1666 serial_out(info, UART_ESI_CMD2, new_config.flow_off);
1667 serial_out(info, UART_ESI_CMD2, new_config.flow_on >> 8);
1668 serial_out(info, UART_ESI_CMD2, new_config.flow_on);
1669 spin_unlock_irqrestore(&info->lock, flags);
1672 if ((new_config.rx_trigger != info->config.rx_trigger) ||
1673 (new_config.tx_trigger != info->config.tx_trigger)) {
1674 unsigned long flags;
1676 info->config.rx_trigger = new_config.rx_trigger;
1677 info->config.tx_trigger = new_config.tx_trigger;
1678 spin_lock_irqsave(&info->lock, flags);
1679 serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
1680 serial_out(info, UART_ESI_CMD2,
1681 new_config.rx_trigger >> 8);
1682 serial_out(info, UART_ESI_CMD2, new_config.rx_trigger);
1683 serial_out(info, UART_ESI_CMD2,
1684 new_config.tx_trigger >> 8);
1685 serial_out(info, UART_ESI_CMD2, new_config.tx_trigger);
1686 spin_unlock_irqrestore(&info->lock, flags);
1689 if (new_config.rx_timeout != info->config.rx_timeout) {
1690 unsigned long flags;
1692 info->config.rx_timeout = new_config.rx_timeout;
1693 spin_lock_irqsave(&info->lock, flags);
1695 if (info->IER & UART_IER_RDI) {
1696 serial_out(info, UART_ESI_CMD1,
1697 ESI_SET_RX_TIMEOUT);
1698 serial_out(info, UART_ESI_CMD2,
1699 new_config.rx_timeout);
1702 spin_unlock_irqrestore(&info->lock, flags);
1705 if (!(info->flags & ASYNC_INITIALIZED))
1706 retval = startup(info);
1712 * get_lsr_info - get line status register info
1714 * Purpose: Let user call ioctl() to get info when the UART physically
1715 * is emptied. On bus types like RS485, the transmitter must
1716 * release the bus after transmitting. This must be done when
1717 * the transmit shift register is empty, not be done when the
1718 * transmit holding register is empty. This functionality
1719 * allows an RS485 driver to be written in user space.
1721 static int get_lsr_info(struct esp_struct * info, unsigned int __user *value)
1723 unsigned char status;
1724 unsigned int result;
1725 unsigned long flags;
1727 spin_lock_irqsave(&info->lock, flags);
1728 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1729 status = serial_in(info, UART_ESI_STAT1);
1730 spin_unlock_irqrestore(&info->lock, flags);
1731 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1732 return put_user(result,value);
1736 static int esp_tiocmget(struct tty_struct *tty, struct file *file)
1738 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1739 unsigned char control, status;
1740 unsigned long flags;
1742 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1744 if (tty->flags & (1 << TTY_IO_ERROR))
1747 control = info->MCR;
1749 spin_lock_irqsave(&info->lock, flags);
1750 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1751 status = serial_in(info, UART_ESI_STAT2);
1752 spin_unlock_irqrestore(&info->lock, flags);
1754 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1755 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1756 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1757 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1758 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1759 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1762 static int esp_tiocmset(struct tty_struct *tty, struct file *file,
1763 unsigned int set, unsigned int clear)
1765 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1766 unsigned long flags;
1768 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1770 if (tty->flags & (1 << TTY_IO_ERROR))
1773 spin_lock_irqsave(&info->lock, flags);
1775 if (set & TIOCM_RTS)
1776 info->MCR |= UART_MCR_RTS;
1777 if (set & TIOCM_DTR)
1778 info->MCR |= UART_MCR_DTR;
1780 if (clear & TIOCM_RTS)
1781 info->MCR &= ~UART_MCR_RTS;
1782 if (clear & TIOCM_DTR)
1783 info->MCR &= ~UART_MCR_DTR;
1785 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1786 serial_out(info, UART_ESI_CMD2, UART_MCR);
1787 serial_out(info, UART_ESI_CMD2, info->MCR);
1789 spin_unlock_irqrestore(&info->lock, flags);
1794 * rs_break() --- routine which turns the break handling on or off
1796 static void esp_break(struct tty_struct *tty, int break_state)
1798 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1799 unsigned long flags;
1801 if (serial_paranoia_check(info, tty->name, "esp_break"))
1804 if (break_state == -1) {
1805 spin_lock_irqsave(&info->lock, flags);
1806 serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1807 serial_out(info, UART_ESI_CMD2, 0x01);
1808 spin_unlock_irqrestore(&info->lock, flags);
1810 /* FIXME - new style wait needed here */
1811 interruptible_sleep_on(&info->break_wait);
1813 spin_lock_irqsave(&info->lock, flags);
1814 serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1815 serial_out(info, UART_ESI_CMD2, 0x00);
1816 spin_unlock_irqrestore(&info->lock, flags);
1820 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1821 unsigned int cmd, unsigned long arg)
1823 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1824 struct async_icount cprev, cnow; /* kernel counter temps */
1825 struct serial_icounter_struct __user *p_cuser; /* user space */
1826 void __user *argp = (void __user *)arg;
1827 unsigned long flags;
1829 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1832 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1833 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1834 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT) &&
1835 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT) &&
1836 (cmd != TIOCGHAYESESP) && (cmd != TIOCSHAYESESP)) {
1837 if (tty->flags & (1 << TTY_IO_ERROR))
1843 return get_serial_info(info, argp);
1845 return set_serial_info(info, argp);
1847 /* do not reconfigure after initial configuration */
1851 return put_user(0L, (unsigned long __user *)argp);
1853 case TIOCSERGETLSR: /* Get line status register */
1854 return get_lsr_info(info, argp);
1857 if (!capable(CAP_SYS_ADMIN))
1862 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1863 * - mask passed in arg for lines of interest
1864 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1865 * Caller should use TIOCGICOUNT to see which one it was
1868 spin_lock_irqsave(&info->lock, flags);
1869 cprev = info->icount; /* note the counters on entry */
1870 spin_unlock_irqrestore(&info->lock, flags);
1872 /* FIXME: convert to new style wakeup */
1873 interruptible_sleep_on(&info->delta_msr_wait);
1874 /* see if a signal did it */
1875 if (signal_pending(current))
1876 return -ERESTARTSYS;
1877 spin_lock_irqsave(&info->lock, flags);
1878 cnow = info->icount; /* atomic copy */
1879 spin_unlock_irqrestore(&info->lock, flags);
1880 if (cnow.rng == cprev.rng &&
1881 cnow.dsr == cprev.dsr &&
1882 cnow.dcd == cprev.dcd &&
1883 cnow.cts == cprev.cts)
1884 return -EIO; /* no change => error */
1885 if (((arg & TIOCM_RNG) &&
1886 (cnow.rng != cprev.rng)) ||
1887 ((arg & TIOCM_DSR) &&
1888 (cnow.dsr != cprev.dsr)) ||
1889 ((arg & TIOCM_CD) &&
1890 (cnow.dcd != cprev.dcd)) ||
1891 ((arg & TIOCM_CTS) &&
1892 (cnow.cts != cprev.cts)) ) {
1900 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1901 * Return: write counters to the user passed counter struct
1902 * NB: both 1->0 and 0->1 transitions are counted except for
1903 * RI where only 0->1 is counted.
1906 spin_lock_irqsave(&info->lock, flags);
1907 cnow = info->icount;
1908 spin_unlock_irqrestore(&info->lock, flags);
1910 if (put_user(cnow.cts, &p_cuser->cts) ||
1911 put_user(cnow.dsr, &p_cuser->dsr) ||
1912 put_user(cnow.rng, &p_cuser->rng) ||
1913 put_user(cnow.dcd, &p_cuser->dcd))
1918 return get_esp_config(info, argp);
1920 return set_esp_config(info, argp);
1923 return -ENOIOCTLCMD;
1928 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1930 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1931 unsigned long flags;
1933 if ( (tty->termios->c_cflag == old_termios->c_cflag)
1934 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
1935 == RELEVANT_IFLAG(old_termios->c_iflag)))
1940 spin_lock_irqsave(&info->lock, flags);
1942 /* Handle transition to B0 status */
1943 if ((old_termios->c_cflag & CBAUD) &&
1944 !(tty->termios->c_cflag & CBAUD)) {
1945 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1946 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1947 serial_out(info, UART_ESI_CMD2, UART_MCR);
1948 serial_out(info, UART_ESI_CMD2, info->MCR);
1951 /* Handle transition away from B0 status */
1952 if (!(old_termios->c_cflag & CBAUD) &&
1953 (tty->termios->c_cflag & CBAUD)) {
1954 info->MCR |= (UART_MCR_DTR | UART_MCR_RTS);
1955 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1956 serial_out(info, UART_ESI_CMD2, UART_MCR);
1957 serial_out(info, UART_ESI_CMD2, info->MCR);
1960 spin_unlock_irqrestore(&info->lock, flags);
1962 /* Handle turning of CRTSCTS */
1963 if ((old_termios->c_cflag & CRTSCTS) &&
1964 !(tty->termios->c_cflag & CRTSCTS)) {
1970 * ------------------------------------------------------------
1973 * This routine is called when the serial port gets closed. First, we
1974 * wait for the last remaining data to be sent. Then, we unlink its
1975 * async structure from the interrupt chain if necessary, and we free
1976 * that IRQ if nothing is left in the chain.
1977 * ------------------------------------------------------------
1979 static void rs_close(struct tty_struct *tty, struct file * filp)
1981 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1982 unsigned long flags;
1984 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1987 spin_lock_irqsave(&info->lock, flags);
1989 if (tty_hung_up_p(filp)) {
1990 DBG_CNT("before DEC-hung");
1994 #ifdef SERIAL_DEBUG_OPEN
1995 printk("rs_close ttys%d, count = %d\n", info->line, info->count);
1997 if ((tty->count == 1) && (info->count != 1)) {
1999 * Uh, oh. tty->count is 1, which means that the tty
2000 * structure will be freed. Info->count should always
2001 * be one in these conditions. If it's greater than
2002 * one, we've got real problems, since it means the
2003 * serial port won't be shutdown.
2005 printk("rs_close: bad serial port count; tty->count is 1, "
2006 "info->count is %d\n", info->count);
2009 if (--info->count < 0) {
2010 printk("rs_close: bad serial port count for ttys%d: %d\n",
2011 info->line, info->count);
2015 DBG_CNT("before DEC-2");
2018 info->flags |= ASYNC_CLOSING;
2020 spin_unlock_irqrestore(&info->lock, flags);
2022 * Now we wait for the transmit buffer to clear; and we notify
2023 * the line discipline to only process XON/XOFF characters.
2026 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
2027 tty_wait_until_sent(tty, info->closing_wait);
2029 * At this point we stop accepting input. To do this, we
2030 * disable the receive line status interrupts, and tell the
2031 * interrupt driver to stop checking the data ready bit in the
2032 * line status register.
2034 /* info->IER &= ~UART_IER_RLSI; */
2035 info->IER &= ~UART_IER_RDI;
2036 info->read_status_mask &= ~UART_LSR_DR;
2037 if (info->flags & ASYNC_INITIALIZED) {
2039 spin_lock_irqsave(&info->lock, flags);
2040 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
2041 serial_out(info, UART_ESI_CMD2, info->IER);
2043 /* disable receive timeout */
2044 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
2045 serial_out(info, UART_ESI_CMD2, 0x00);
2047 spin_unlock_irqrestore(&info->lock, flags);
2050 * Before we drop DTR, make sure the UART transmitter
2051 * has completely drained; this is especially
2052 * important if there is a transmit FIFO!
2054 rs_wait_until_sent(tty, info->timeout);
2057 if (tty->driver->flush_buffer)
2058 tty->driver->flush_buffer(tty);
2059 tty_ldisc_flush(tty);
2064 if (info->blocked_open) {
2065 if (info->close_delay) {
2066 msleep_interruptible(jiffies_to_msecs(info->close_delay));
2068 wake_up_interruptible(&info->open_wait);
2070 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
2071 wake_up_interruptible(&info->close_wait);
2075 spin_unlock_irqrestore(&info->lock, flags);
2078 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
2080 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
2081 unsigned long orig_jiffies, char_time;
2082 unsigned long flags;
2084 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
2087 orig_jiffies = jiffies;
2088 char_time = ((info->timeout - HZ / 50) / 1024) / 5;
2093 spin_lock_irqsave(&info->lock, flags);
2094 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2095 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2097 while ((serial_in(info, UART_ESI_STAT1) != 0x03) ||
2098 (serial_in(info, UART_ESI_STAT2) != 0xff)) {
2100 spin_unlock_irqrestore(&info->lock, flags);
2101 msleep_interruptible(jiffies_to_msecs(char_time));
2103 if (signal_pending(current))
2106 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2109 spin_lock_irqsave(&info->lock, flags);
2110 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2111 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2113 spin_unlock_irqrestore(&info->lock, flags);
2114 set_current_state(TASK_RUNNING);
2118 * esp_hangup() --- called by tty_hangup() when a hangup is signaled.
2120 static void esp_hangup(struct tty_struct *tty)
2122 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
2124 if (serial_paranoia_check(info, tty->name, "esp_hangup"))
2127 rs_flush_buffer(tty);
2131 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2133 wake_up_interruptible(&info->open_wait);
2137 * ------------------------------------------------------------
2138 * esp_open() and friends
2139 * ------------------------------------------------------------
2141 static int block_til_ready(struct tty_struct *tty, struct file * filp,
2142 struct esp_struct *info)
2144 DECLARE_WAITQUEUE(wait, current);
2147 unsigned long flags;
2150 * If the device is in the middle of being closed, then block
2151 * until it's done, and then try again.
2153 if (tty_hung_up_p(filp) ||
2154 (info->flags & ASYNC_CLOSING)) {
2155 if (info->flags & ASYNC_CLOSING)
2156 interruptible_sleep_on(&info->close_wait);
2157 #ifdef SERIAL_DO_RESTART
2158 if (info->flags & ASYNC_HUP_NOTIFY)
2161 return -ERESTARTSYS;
2168 * If non-blocking mode is set, or the port is not enabled,
2169 * then make the check up front and then exit.
2171 if ((filp->f_flags & O_NONBLOCK) ||
2172 (tty->flags & (1 << TTY_IO_ERROR))) {
2173 info->flags |= ASYNC_NORMAL_ACTIVE;
2177 if (tty->termios->c_cflag & CLOCAL)
2181 * Block waiting for the carrier detect and the line to become
2182 * free (i.e., not in use by the callout). While we are in
2183 * this loop, info->count is dropped by one, so that
2184 * rs_close() knows when to free things. We restore it upon
2185 * exit, either normal or abnormal.
2188 add_wait_queue(&info->open_wait, &wait);
2189 #ifdef SERIAL_DEBUG_OPEN
2190 printk("block_til_ready before block: ttys%d, count = %d\n",
2191 info->line, info->count);
2193 spin_lock_irqsave(&info->lock, flags);
2194 if (!tty_hung_up_p(filp))
2196 info->blocked_open++;
2198 if ((tty->termios->c_cflag & CBAUD)) {
2199 unsigned int scratch;
2201 serial_out(info, UART_ESI_CMD1, ESI_READ_UART);
2202 serial_out(info, UART_ESI_CMD2, UART_MCR);
2203 scratch = serial_in(info, UART_ESI_STAT1);
2204 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2205 serial_out(info, UART_ESI_CMD2, UART_MCR);
2206 serial_out(info, UART_ESI_CMD2,
2207 scratch | UART_MCR_DTR | UART_MCR_RTS);
2209 set_current_state(TASK_INTERRUPTIBLE);
2210 if (tty_hung_up_p(filp) ||
2211 !(info->flags & ASYNC_INITIALIZED)) {
2212 #ifdef SERIAL_DO_RESTART
2213 if (info->flags & ASYNC_HUP_NOTIFY)
2216 retval = -ERESTARTSYS;
2223 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
2224 if (serial_in(info, UART_ESI_STAT2) & UART_MSR_DCD)
2227 if (!(info->flags & ASYNC_CLOSING) &&
2230 if (signal_pending(current)) {
2231 retval = -ERESTARTSYS;
2234 #ifdef SERIAL_DEBUG_OPEN
2235 printk("block_til_ready blocking: ttys%d, count = %d\n",
2236 info->line, info->count);
2238 spin_unlock_irqrestore(&info->lock, flags);
2240 spin_lock_irqsave(&info->lock, flags);
2242 set_current_state(TASK_RUNNING);
2243 remove_wait_queue(&info->open_wait, &wait);
2244 if (!tty_hung_up_p(filp))
2246 info->blocked_open--;
2247 spin_unlock_irqrestore(&info->lock, flags);
2248 #ifdef SERIAL_DEBUG_OPEN
2249 printk("block_til_ready after blocking: ttys%d, count = %d\n",
2250 info->line, info->count);
2254 info->flags |= ASYNC_NORMAL_ACTIVE;
2259 * This routine is called whenever a serial port is opened. It
2260 * enables interrupts for a serial port, linking in its async structure into
2261 * the IRQ chain. It also performs the serial-specific
2262 * initialization for the tty structure.
2264 static int esp_open(struct tty_struct *tty, struct file * filp)
2266 struct esp_struct *info;
2268 unsigned long flags;
2271 if ((line < 0) || (line >= NR_PORTS))
2274 /* find the port in the chain */
2278 while (info && (info->line != line))
2279 info = info->next_port;
2282 serial_paranoia_check(info, tty->name, "esp_open");
2286 #ifdef SERIAL_DEBUG_OPEN
2287 printk("esp_open %s, count = %d\n", tty->name, info->count);
2289 spin_lock_irqsave(&info->lock, flags);
2291 tty->driver_data = info;
2295 tmp_buf = (unsigned char *) get_zeroed_page(GFP_KERNEL);
2301 * Start up serial port
2303 retval = startup(info);
2307 retval = block_til_ready(tty, filp, info);
2309 #ifdef SERIAL_DEBUG_OPEN
2310 printk("esp_open returning after block_til_ready with %d\n",
2316 #ifdef SERIAL_DEBUG_OPEN
2317 printk("esp_open %s successful...", tty->name);
2323 * ---------------------------------------------------------------------
2324 * espserial_init() and friends
2326 * espserial_init() is called at boot-time to initialize the serial driver.
2327 * ---------------------------------------------------------------------
2331 * This routine prints out the appropriate serial driver version
2332 * number, and identifies which options were configured into this
2336 static inline void show_serial_version(void)
2338 printk(KERN_INFO "%s version %s (DMA %u)\n",
2339 serial_name, serial_version, dma);
2343 * This routine is called by espserial_init() to initialize a specific serial
2346 static inline int autoconfig(struct esp_struct * info)
2348 int port_detected = 0;
2349 unsigned long flags;
2351 if (!request_region(info->port, REGION_SIZE, "esp serial"))
2354 spin_lock_irqsave(&info->lock, flags);
2356 * Check for ESP card
2359 if (serial_in(info, UART_ESI_BASE) == 0xf3) {
2360 serial_out(info, UART_ESI_CMD1, 0x00);
2361 serial_out(info, UART_ESI_CMD1, 0x01);
2363 if ((serial_in(info, UART_ESI_STAT2) & 0x70) == 0x20) {
2367 serial_out(info, UART_ESI_CMD1, 0x02);
2369 if (serial_in(info, UART_ESI_STAT1) & 0x01)
2376 /* put card in enhanced mode */
2377 /* this prevents access through */
2378 /* the "old" IO ports */
2379 esp_basic_init(info);
2382 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2383 serial_out(info, UART_ESI_CMD2, UART_MCR);
2384 serial_out(info, UART_ESI_CMD2, 0x00);
2388 release_region(info->port, REGION_SIZE);
2390 spin_unlock_irqrestore(&info->lock, flags);
2391 return (port_detected);
2394 static struct tty_operations esp_ops = {
2398 .put_char = rs_put_char,
2399 .flush_chars = rs_flush_chars,
2400 .write_room = rs_write_room,
2401 .chars_in_buffer = rs_chars_in_buffer,
2402 .flush_buffer = rs_flush_buffer,
2404 .throttle = rs_throttle,
2405 .unthrottle = rs_unthrottle,
2406 .set_termios = rs_set_termios,
2409 .hangup = esp_hangup,
2410 .break_ctl = esp_break,
2411 .wait_until_sent = rs_wait_until_sent,
2412 .tiocmget = esp_tiocmget,
2413 .tiocmset = esp_tiocmset,
2417 * The serial driver boot-time initialization code!
2419 static int __init espserial_init(void)
2422 struct esp_struct * info;
2423 struct esp_struct *last_primary = NULL;
2424 int esp[] = {0x100,0x140,0x180,0x200,0x240,0x280,0x300,0x380};
2426 esp_driver = alloc_tty_driver(NR_PORTS);
2430 for (i = 0; i < NR_PRIMARY; i++) {
2432 if ((irq[i] < 2) || (irq[i] > 15) || (irq[i] == 6) ||
2433 (irq[i] == 8) || (irq[i] == 13))
2435 else if (irq[i] == 2)
2440 if ((dma != 1) && (dma != 3))
2443 if ((rx_trigger < 1) || (rx_trigger > 1023))
2446 if ((tx_trigger < 1) || (tx_trigger > 1023))
2449 if ((flow_off < 1) || (flow_off > 1023))
2452 if ((flow_on < 1) || (flow_on > 1023))
2455 if ((rx_timeout < 0) || (rx_timeout > 255))
2458 if (flow_on >= flow_off)
2459 flow_on = flow_off - 1;
2461 show_serial_version();
2463 /* Initialize the tty_driver structure */
2465 esp_driver->owner = THIS_MODULE;
2466 esp_driver->name = "ttyP";
2467 esp_driver->devfs_name = "tts/P";
2468 esp_driver->major = ESP_IN_MAJOR;
2469 esp_driver->minor_start = 0;
2470 esp_driver->type = TTY_DRIVER_TYPE_SERIAL;
2471 esp_driver->subtype = SERIAL_TYPE_NORMAL;
2472 esp_driver->init_termios = tty_std_termios;
2473 esp_driver->init_termios.c_cflag =
2474 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2475 esp_driver->flags = TTY_DRIVER_REAL_RAW;
2476 tty_set_operations(esp_driver, &esp_ops);
2477 if (tty_register_driver(esp_driver))
2479 printk(KERN_ERR "Couldn't register esp serial driver");
2480 put_tty_driver(esp_driver);
2484 info = kmalloc(sizeof(struct esp_struct), GFP_KERNEL);
2488 printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
2489 tty_unregister_driver(esp_driver);
2490 put_tty_driver(esp_driver);
2494 memset((void *)info, 0, sizeof(struct esp_struct));
2495 spin_lock_init(&info->lock);
2496 /* rx_trigger, tx_trigger are needed by autoconfig */
2497 info->config.rx_trigger = rx_trigger;
2498 info->config.tx_trigger = tx_trigger;
2504 info->port = esp[i] + offset;
2506 info->line = (i * 8) + (offset / 8);
2508 if (!autoconfig(info)) {
2514 info->custom_divisor = (divisor[i] >> (offset / 2)) & 0xf;
2515 info->flags = STD_COM_FLAGS;
2516 if (info->custom_divisor)
2517 info->flags |= ASYNC_SPD_CUST;
2518 info->magic = ESP_MAGIC;
2519 info->close_delay = 5*HZ/10;
2520 info->closing_wait = 30*HZ;
2521 INIT_WORK(&info->tqueue, do_softint, info);
2522 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
2523 info->config.rx_timeout = rx_timeout;
2524 info->config.flow_on = flow_on;
2525 info->config.flow_off = flow_off;
2526 info->config.pio_threshold = pio_threshold;
2527 info->next_port = ports;
2528 init_waitqueue_head(&info->open_wait);
2529 init_waitqueue_head(&info->close_wait);
2530 init_waitqueue_head(&info->delta_msr_wait);
2531 init_waitqueue_head(&info->break_wait);
2533 printk(KERN_INFO "ttyP%d at 0x%04x (irq = %d) is an ESP ",
2534 info->line, info->port, info->irq);
2536 if (info->line % 8) {
2537 printk("secondary port\n");
2538 /* 8 port cards can't do DMA */
2539 info->stat_flags |= ESP_STAT_NEVER_DMA;
2542 last_primary->stat_flags |= ESP_STAT_NEVER_DMA;
2544 printk("primary port\n");
2545 last_primary = info;
2550 info->stat_flags |= ESP_STAT_NEVER_DMA;
2552 info = kmalloc(sizeof(struct esp_struct), GFP_KERNEL);
2555 printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
2557 /* allow use of the already detected ports */
2561 memset((void *)info, 0, sizeof(struct esp_struct));
2562 /* rx_trigger, tx_trigger are needed by autoconfig */
2563 info->config.rx_trigger = rx_trigger;
2564 info->config.tx_trigger = tx_trigger;
2572 } while (i < NR_PRIMARY);
2574 /* free the last port memory allocation */
2580 static void __exit espserial_exit(void)
2583 struct esp_struct *temp_async;
2584 struct esp_pio_buffer *pio_buf;
2586 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2587 if ((e1 = tty_unregister_driver(esp_driver)))
2588 printk("SERIAL: failed to unregister serial driver (%d)\n",
2590 put_tty_driver(esp_driver);
2594 release_region(ports->port, REGION_SIZE);
2596 temp_async = ports->next_port;
2602 free_pages((unsigned long)dma_buffer,
2603 get_order(DMA_BUFFER_SZ));
2606 free_page((unsigned long)tmp_buf);
2608 while (free_pio_buf) {
2609 pio_buf = free_pio_buf->next;
2610 kfree(free_pio_buf);
2611 free_pio_buf = pio_buf;
2615 module_init(espserial_init);
2616 module_exit(espserial_exit);