Linux 2.4.0-test10pre4
[davej-history.git] / drivers / char / esp.c
blob478b05aa55873ed00c0ad7c9fef4056722555da2
1 /*
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
11 * Accent Async board.
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
29 * by Chris Faylor.
31 * Most recent changes: (Andrew J. Robinson)
32 * Support for PIO mode. This allows the driver to work properly with
33 * multiport cards.
35 * This module exports the following rs232 io functions:
37 * int espserial_init(void);
40 #include <linux/module.h>
41 #include <linux/errno.h>
42 #include <linux/signal.h>
43 #include <linux/sched.h>
44 #include <linux/interrupt.h>
45 #include <linux/tty.h>
46 #include <linux/tty_flip.h>
47 #include <linux/serial.h>
48 #include <linux/serialP.h>
49 #include <linux/serial_reg.h>
50 #include <linux/major.h>
51 #include <linux/string.h>
52 #include <linux/fcntl.h>
53 #include <linux/ptrace.h>
54 #include <linux/ioport.h>
55 #include <linux/mm.h>
56 #include <linux/init.h>
58 #include <asm/system.h>
59 #include <asm/io.h>
60 #include <asm/segment.h>
61 #include <asm/bitops.h>
63 #include <asm/dma.h>
64 #include <linux/malloc.h>
65 #include <asm/uaccess.h>
67 #include <linux/hayesesp.h>
69 #define NR_PORTS 64 /* maximum number of ports */
70 #define NR_PRIMARY 8 /* maximum number of primary ports */
72 /* The following variables can be set by giving module options */
73 static int irq[NR_PRIMARY]; /* IRQ for each base port */
74 static unsigned int divisor[NR_PRIMARY]; /* custom divisor for each port */
75 static unsigned int dma = ESP_DMA_CHANNEL; /* DMA channel */
76 static unsigned int rx_trigger = ESP_RX_TRIGGER;
77 static unsigned int tx_trigger = ESP_TX_TRIGGER;
78 static unsigned int flow_off = ESP_FLOW_OFF;
79 static unsigned int flow_on = ESP_FLOW_ON;
80 static unsigned int rx_timeout = ESP_RX_TMOUT;
81 static unsigned int pio_threshold = ESP_PIO_THRESHOLD;
83 MODULE_PARM(irq, "1-8i");
84 MODULE_PARM(divisor, "1-8i");
85 MODULE_PARM(dma, "i");
86 MODULE_PARM(rx_trigger, "i");
87 MODULE_PARM(tx_trigger, "i");
88 MODULE_PARM(flow_off, "i");
89 MODULE_PARM(flow_on, "i");
90 MODULE_PARM(rx_timeout, "i");
91 MODULE_PARM(pio_threshold, "i");
93 /* END */
95 static char *dma_buffer;
96 static int dma_bytes;
97 static struct esp_pio_buffer *free_pio_buf;
99 #define DMA_BUFFER_SZ 1024
101 #define WAKEUP_CHARS 1024
103 static char *serial_name = "ESP serial driver";
104 static char *serial_version = "2.2";
106 static DECLARE_TASK_QUEUE(tq_esp);
108 static struct tty_driver esp_driver, esp_callout_driver;
109 static int serial_refcount;
111 /* serial subtype definitions */
112 #define SERIAL_TYPE_NORMAL 1
113 #define SERIAL_TYPE_CALLOUT 2
116 * Serial driver configuration section. Here are the various options:
118 * SERIAL_PARANOIA_CHECK
119 * Check the magic number for the esp_structure where
120 * ever possible.
123 #undef SERIAL_PARANOIA_CHECK
124 #define SERIAL_DO_RESTART
126 #undef SERIAL_DEBUG_INTR
127 #undef SERIAL_DEBUG_OPEN
128 #undef SERIAL_DEBUG_FLOW
130 #define _INLINE_ inline
132 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
133 #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
134 kdevname(tty->device), (info->flags), serial_refcount,info->count,tty->count,s)
135 #else
136 #define DBG_CNT(s)
137 #endif
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)
153 static struct tty_struct *serial_table[NR_PORTS];
154 static struct termios *serial_termios[NR_PORTS];
155 static struct termios *serial_termios_locked[NR_PORTS];
157 #ifndef MIN
158 #define MIN(a,b) ((a) < (b) ? (a) : (b))
159 #endif
162 * tmp_buf is used as a temporary buffer by serial_write. We need to
163 * lock it in case the memcpy_fromfs blocks while swapping in a page,
164 * and some other program tries to do a serial write at the same time.
165 * Since the lock will only come under contention when the system is
166 * swapping and available memory is low, it makes sense to share one
167 * buffer across all the serial ports, since it significantly saves
168 * memory if large numbers of serial ports are open.
170 static unsigned char *tmp_buf;
171 static DECLARE_MUTEX(tmp_buf_sem);
173 static inline int serial_paranoia_check(struct esp_struct *info,
174 kdev_t device, const char *routine)
176 #ifdef SERIAL_PARANOIA_CHECK
177 static const char *badmagic =
178 "Warning: bad magic number for serial struct (%s) in %s\n";
179 static const char *badinfo =
180 "Warning: null esp_struct for (%s) in %s\n";
182 if (!info) {
183 printk(badinfo, kdevname(device), routine);
184 return 1;
186 if (info->magic != ESP_MAGIC) {
187 printk(badmagic, kdevname(device), routine);
188 return 1;
190 #endif
191 return 0;
194 static inline unsigned int serial_in(struct esp_struct *info, int offset)
196 return inb(info->port + offset);
199 static inline void serial_out(struct esp_struct *info, int offset,
200 unsigned char value)
202 outb(value, info->port+offset);
206 * ------------------------------------------------------------
207 * rs_stop() and rs_start()
209 * This routines are called before setting or resetting tty->stopped.
210 * They enable or disable transmitter interrupts, as necessary.
211 * ------------------------------------------------------------
213 static void rs_stop(struct tty_struct *tty)
215 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
216 unsigned long flags;
218 if (serial_paranoia_check(info, tty->device, "rs_stop"))
219 return;
221 save_flags(flags); cli();
222 if (info->IER & UART_IER_THRI) {
223 info->IER &= ~UART_IER_THRI;
224 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
225 serial_out(info, UART_ESI_CMD2, info->IER);
228 restore_flags(flags);
231 static void rs_start(struct tty_struct *tty)
233 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
234 unsigned long flags;
236 if (serial_paranoia_check(info, tty->device, "rs_start"))
237 return;
239 save_flags(flags); cli();
240 if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
241 info->IER |= UART_IER_THRI;
242 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
243 serial_out(info, UART_ESI_CMD2, info->IER);
245 restore_flags(flags);
249 * ----------------------------------------------------------------------
251 * Here starts the interrupt handling routines. All of the following
252 * subroutines are declared as inline and are folded into
253 * rs_interrupt(). They were separated out for readability's sake.
255 * Note: rs_interrupt() is a "fast" interrupt, which means that it
256 * runs with interrupts turned off. People who may want to modify
257 * rs_interrupt() should try to keep the interrupt handler as fast as
258 * possible. After you are done making modifications, it is not a bad
259 * idea to do:
261 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
263 * and look at the resulting assemble code in serial.s.
265 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
266 * -----------------------------------------------------------------------
270 * This routine is used by the interrupt handler to schedule
271 * processing in the software interrupt portion of the driver.
273 static _INLINE_ void rs_sched_event(struct esp_struct *info,
274 int event)
276 info->event |= 1 << event;
277 queue_task(&info->tqueue, &tq_esp);
278 mark_bh(ESP_BH);
280 static _INLINE_ struct esp_pio_buffer *get_pio_buffer(void)
282 struct esp_pio_buffer *buf;
284 if (free_pio_buf) {
285 buf = free_pio_buf;
286 free_pio_buf = buf->next;
287 } else {
288 buf = (struct esp_pio_buffer *)
289 kmalloc(sizeof(struct esp_pio_buffer), GFP_ATOMIC);
292 return buf;
295 static _INLINE_ void release_pio_buffer(struct esp_pio_buffer *buf)
297 buf->next = free_pio_buf;
298 free_pio_buf = buf;
301 static _INLINE_ void receive_chars_pio(struct esp_struct *info, int num_bytes)
303 struct tty_struct *tty = info->tty;
304 int i;
305 struct esp_pio_buffer *pio_buf;
306 struct esp_pio_buffer *err_buf;
307 unsigned char status_mask;
309 pio_buf = get_pio_buffer();
311 if (!pio_buf)
312 return;
314 err_buf = get_pio_buffer();
316 if (!err_buf) {
317 release_pio_buffer(pio_buf);
318 return;
321 sti();
323 status_mask = (info->read_status_mask >> 2) & 0x07;
325 for (i = 0; i < num_bytes - 1; i += 2) {
326 *((unsigned short *)(pio_buf->data + i)) =
327 inw(info->port + UART_ESI_RX);
328 err_buf->data[i] = serial_in(info, UART_ESI_RWS);
329 err_buf->data[i + 1] = (err_buf->data[i] >> 3) & status_mask;
330 err_buf->data[i] &= status_mask;
333 if (num_bytes & 0x0001) {
334 pio_buf->data[num_bytes - 1] = serial_in(info, UART_ESI_RX);
335 err_buf->data[num_bytes - 1] =
336 (serial_in(info, UART_ESI_RWS) >> 3) & status_mask;
339 cli();
341 /* make sure everything is still ok since interrupts were enabled */
342 tty = info->tty;
344 if (!tty) {
345 release_pio_buffer(pio_buf);
346 release_pio_buffer(err_buf);
347 info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
348 return;
351 status_mask = (info->ignore_status_mask >> 2) & 0x07;
353 for (i = 0; i < num_bytes; i++) {
354 if (!(err_buf->data[i] & status_mask)) {
355 *(tty->flip.char_buf_ptr++) = pio_buf->data[i];
357 if (err_buf->data[i] & 0x04) {
358 *(tty->flip.flag_buf_ptr++) = TTY_BREAK;
360 if (info->flags & ASYNC_SAK)
361 do_SAK(tty);
363 else if (err_buf->data[i] & 0x02)
364 *(tty->flip.flag_buf_ptr++) = TTY_FRAME;
365 else if (err_buf->data[i] & 0x01)
366 *(tty->flip.flag_buf_ptr++) = TTY_PARITY;
367 else
368 *(tty->flip.flag_buf_ptr++) = 0;
370 tty->flip.count++;
374 queue_task(&tty->flip.tqueue, &tq_timer);
376 info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
377 release_pio_buffer(pio_buf);
378 release_pio_buffer(err_buf);
381 static _INLINE_ void receive_chars_dma(struct esp_struct *info, int num_bytes)
383 unsigned long flags;
384 info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
385 dma_bytes = num_bytes;
386 info->stat_flags |= ESP_STAT_DMA_RX;
388 flags=claim_dma_lock();
389 disable_dma(dma);
390 clear_dma_ff(dma);
391 set_dma_mode(dma, DMA_MODE_READ);
392 set_dma_addr(dma, virt_to_bus(dma_buffer));
393 set_dma_count(dma, dma_bytes);
394 enable_dma(dma);
395 release_dma_lock(flags);
397 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_RX);
400 static _INLINE_ void receive_chars_dma_done(struct esp_struct *info,
401 int status)
403 struct tty_struct *tty = info->tty;
404 int num_bytes;
405 unsigned long flags;
408 flags=claim_dma_lock();
409 disable_dma(dma);
410 clear_dma_ff(dma);
412 info->stat_flags &= ~ESP_STAT_DMA_RX;
413 num_bytes = dma_bytes - get_dma_residue(dma);
414 release_dma_lock(flags);
416 info->icount.rx += num_bytes;
418 memcpy(tty->flip.char_buf_ptr, dma_buffer, num_bytes);
419 tty->flip.char_buf_ptr += num_bytes;
420 tty->flip.count += num_bytes;
421 memset(tty->flip.flag_buf_ptr, 0, num_bytes);
422 tty->flip.flag_buf_ptr += num_bytes;
424 if (num_bytes > 0) {
425 tty->flip.flag_buf_ptr--;
427 status &= (0x1c & info->read_status_mask);
429 if (status & info->ignore_status_mask) {
430 tty->flip.count--;
431 tty->flip.char_buf_ptr--;
432 tty->flip.flag_buf_ptr--;
433 } else if (status & 0x10) {
434 *tty->flip.flag_buf_ptr = TTY_BREAK;
435 (info->icount.brk)++;
436 if (info->flags & ASYNC_SAK)
437 do_SAK(tty);
438 } else if (status & 0x08) {
439 *tty->flip.flag_buf_ptr = TTY_FRAME;
440 (info->icount.frame)++;
442 else if (status & 0x04) {
443 *tty->flip.flag_buf_ptr = TTY_PARITY;
444 (info->icount.parity)++;
447 tty->flip.flag_buf_ptr++;
449 queue_task(&tty->flip.tqueue, &tq_timer);
452 if (dma_bytes != num_bytes) {
453 num_bytes = dma_bytes - num_bytes;
454 dma_bytes = 0;
455 receive_chars_dma(info, num_bytes);
456 } else
457 dma_bytes = 0;
460 static _INLINE_ void transmit_chars_pio(struct esp_struct *info,
461 int space_avail)
463 int i;
464 struct esp_pio_buffer *pio_buf;
466 pio_buf = get_pio_buffer();
468 if (!pio_buf)
469 return;
471 while (space_avail && info->xmit_cnt) {
472 if (info->xmit_tail + space_avail <= ESP_XMIT_SIZE) {
473 memcpy(pio_buf->data,
474 &(info->xmit_buf[info->xmit_tail]),
475 space_avail);
476 } else {
477 i = ESP_XMIT_SIZE - info->xmit_tail;
478 memcpy(pio_buf->data,
479 &(info->xmit_buf[info->xmit_tail]), i);
480 memcpy(&(pio_buf->data[i]), info->xmit_buf,
481 space_avail - i);
484 info->xmit_cnt -= space_avail;
485 info->xmit_tail = (info->xmit_tail + space_avail) &
486 (ESP_XMIT_SIZE - 1);
488 sti();
490 for (i = 0; i < space_avail - 1; i += 2) {
491 outw(*((unsigned short *)(pio_buf->data + i)),
492 info->port + UART_ESI_TX);
495 if (space_avail & 0x0001)
496 serial_out(info, UART_ESI_TX,
497 pio_buf->data[space_avail - 1]);
499 cli();
501 if (info->xmit_cnt) {
502 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
503 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
504 space_avail = serial_in(info, UART_ESI_STAT1) << 8;
505 space_avail |= serial_in(info, UART_ESI_STAT2);
507 if (space_avail > info->xmit_cnt)
508 space_avail = info->xmit_cnt;
512 if (info->xmit_cnt < WAKEUP_CHARS) {
513 rs_sched_event(info, ESP_EVENT_WRITE_WAKEUP);
515 #ifdef SERIAL_DEBUG_INTR
516 printk("THRE...");
517 #endif
519 if (info->xmit_cnt <= 0) {
520 info->IER &= ~UART_IER_THRI;
521 serial_out(info, UART_ESI_CMD1,
522 ESI_SET_SRV_MASK);
523 serial_out(info, UART_ESI_CMD2, info->IER);
527 release_pio_buffer(pio_buf);
530 static _INLINE_ void transmit_chars_dma(struct esp_struct *info, int num_bytes)
532 unsigned long flags;
534 dma_bytes = num_bytes;
536 if (info->xmit_tail + dma_bytes <= ESP_XMIT_SIZE) {
537 memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
538 dma_bytes);
539 } else {
540 int i = ESP_XMIT_SIZE - info->xmit_tail;
541 memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
543 memcpy(&(dma_buffer[i]), info->xmit_buf, dma_bytes - i);
546 info->xmit_cnt -= dma_bytes;
547 info->xmit_tail = (info->xmit_tail + dma_bytes) & (ESP_XMIT_SIZE - 1);
549 if (info->xmit_cnt < WAKEUP_CHARS) {
550 rs_sched_event(info, ESP_EVENT_WRITE_WAKEUP);
552 #ifdef SERIAL_DEBUG_INTR
553 printk("THRE...");
554 #endif
556 if (info->xmit_cnt <= 0) {
557 info->IER &= ~UART_IER_THRI;
558 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
559 serial_out(info, UART_ESI_CMD2, info->IER);
563 info->stat_flags |= ESP_STAT_DMA_TX;
565 flags=claim_dma_lock();
566 disable_dma(dma);
567 clear_dma_ff(dma);
568 set_dma_mode(dma, DMA_MODE_WRITE);
569 set_dma_addr(dma, virt_to_bus(dma_buffer));
570 set_dma_count(dma, dma_bytes);
571 enable_dma(dma);
572 release_dma_lock(flags);
574 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
577 static _INLINE_ void transmit_chars_dma_done(struct esp_struct *info)
579 int num_bytes;
580 unsigned long flags;
583 flags=claim_dma_lock();
584 disable_dma(dma);
585 clear_dma_ff(dma);
587 num_bytes = dma_bytes - get_dma_residue(dma);
588 info->icount.tx += dma_bytes;
589 release_dma_lock(flags);
591 if (dma_bytes != num_bytes) {
592 dma_bytes -= num_bytes;
593 memmove(dma_buffer, dma_buffer + num_bytes, dma_bytes);
595 flags=claim_dma_lock();
596 disable_dma(dma);
597 clear_dma_ff(dma);
598 set_dma_mode(dma, DMA_MODE_WRITE);
599 set_dma_addr(dma, virt_to_bus(dma_buffer));
600 set_dma_count(dma, dma_bytes);
601 enable_dma(dma);
602 release_dma_lock(flags);
604 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
605 } else {
606 dma_bytes = 0;
607 info->stat_flags &= ~ESP_STAT_DMA_TX;
611 static _INLINE_ void check_modem_status(struct esp_struct *info)
613 int status;
615 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
616 status = serial_in(info, UART_ESI_STAT2);
618 if (status & UART_MSR_ANY_DELTA) {
619 /* update input line counters */
620 if (status & UART_MSR_TERI)
621 info->icount.rng++;
622 if (status & UART_MSR_DDSR)
623 info->icount.dsr++;
624 if (status & UART_MSR_DDCD)
625 info->icount.dcd++;
626 if (status & UART_MSR_DCTS)
627 info->icount.cts++;
628 wake_up_interruptible(&info->delta_msr_wait);
631 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
632 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
633 printk("ttys%d CD now %s...", info->line,
634 (status & UART_MSR_DCD) ? "on" : "off");
635 #endif
636 if (status & UART_MSR_DCD)
637 wake_up_interruptible(&info->open_wait);
638 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
639 (info->flags & ASYNC_CALLOUT_NOHUP))) {
640 #ifdef SERIAL_DEBUG_OPEN
641 printk("scheduling hangup...");
642 #endif
643 queue_task(&info->tqueue_hangup, &tq_scheduler);
649 * This is the serial driver's interrupt routine
651 static void rs_interrupt_single(int irq, void *dev_id, struct pt_regs * regs)
653 struct esp_struct * info;
654 unsigned err_status;
655 unsigned int scratch;
657 #ifdef SERIAL_DEBUG_INTR
658 printk("rs_interrupt_single(%d)...", irq);
659 #endif
660 info = (struct esp_struct *)dev_id;
661 err_status = 0;
662 scratch = serial_in(info, UART_ESI_SID);
664 cli();
666 if (!info->tty) {
667 sti();
668 return;
671 if (scratch & 0x04) { /* error */
672 serial_out(info, UART_ESI_CMD1, ESI_GET_ERR_STAT);
673 err_status = serial_in(info, UART_ESI_STAT1);
674 serial_in(info, UART_ESI_STAT2);
676 if (err_status & 0x01)
677 info->stat_flags |= ESP_STAT_RX_TIMEOUT;
679 if (err_status & 0x20) /* UART status */
680 check_modem_status(info);
682 if (err_status & 0x80) /* Start break */
683 wake_up_interruptible(&info->break_wait);
686 if ((scratch & 0x88) || /* DMA completed or timed out */
687 (err_status & 0x1c) /* receive error */) {
688 if (info->stat_flags & ESP_STAT_DMA_RX)
689 receive_chars_dma_done(info, err_status);
690 else if (info->stat_flags & ESP_STAT_DMA_TX)
691 transmit_chars_dma_done(info);
694 if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
695 ((scratch & 0x01) || (info->stat_flags & ESP_STAT_RX_TIMEOUT)) &&
696 (info->IER & UART_IER_RDI)) {
697 int num_bytes;
699 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
700 serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
701 num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
702 num_bytes |= serial_in(info, UART_ESI_STAT2);
704 if (num_bytes > (TTY_FLIPBUF_SIZE - info->tty->flip.count))
705 num_bytes = TTY_FLIPBUF_SIZE - info->tty->flip.count;
707 if (num_bytes) {
708 if (dma_bytes ||
709 (info->stat_flags & ESP_STAT_USE_PIO) ||
710 (num_bytes <= info->config.pio_threshold))
711 receive_chars_pio(info, num_bytes);
712 else
713 receive_chars_dma(info, num_bytes);
717 if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
718 (scratch & 0x02) && (info->IER & UART_IER_THRI)) {
719 if ((info->xmit_cnt <= 0) || info->tty->stopped) {
720 info->IER &= ~UART_IER_THRI;
721 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
722 serial_out(info, UART_ESI_CMD2, info->IER);
723 } else {
724 int num_bytes;
726 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
727 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
728 num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
729 num_bytes |= serial_in(info, UART_ESI_STAT2);
731 if (num_bytes > info->xmit_cnt)
732 num_bytes = info->xmit_cnt;
734 if (num_bytes) {
735 if (dma_bytes ||
736 (info->stat_flags & ESP_STAT_USE_PIO) ||
737 (num_bytes <= info->config.pio_threshold))
738 transmit_chars_pio(info, num_bytes);
739 else
740 transmit_chars_dma(info, num_bytes);
745 info->last_active = jiffies;
747 #ifdef SERIAL_DEBUG_INTR
748 printk("end.\n");
749 #endif
750 sti();
754 * -------------------------------------------------------------------
755 * Here ends the serial interrupt routines.
756 * -------------------------------------------------------------------
760 * This routine is used to handle the "bottom half" processing for the
761 * serial driver, known also the "software interrupt" processing.
762 * This processing is done at the kernel interrupt level, after the
763 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
764 * is where time-consuming activities which can not be done in the
765 * interrupt driver proper are done; the interrupt driver schedules
766 * them using rs_sched_event(), and they get done here.
768 static void do_serial_bh(void)
770 run_task_queue(&tq_esp);
773 static void do_softint(void *private_)
775 struct esp_struct *info = (struct esp_struct *) private_;
776 struct tty_struct *tty;
778 tty = info->tty;
779 if (!tty)
780 return;
782 if (test_and_clear_bit(ESP_EVENT_WRITE_WAKEUP, &info->event)) {
783 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
784 tty->ldisc.write_wakeup)
785 (tty->ldisc.write_wakeup)(tty);
786 wake_up_interruptible(&tty->write_wait);
791 * This routine is called from the scheduler tqueue when the interrupt
792 * routine has signalled that a hangup has occurred. The path of
793 * hangup processing is:
795 * serial interrupt routine -> (scheduler tqueue) ->
796 * do_serial_hangup() -> tty->hangup() -> esp_hangup()
799 static void do_serial_hangup(void *private_)
801 struct esp_struct *info = (struct esp_struct *) private_;
802 struct tty_struct *tty;
804 tty = info->tty;
805 if (!tty)
806 return;
808 tty_hangup(tty);
812 * ---------------------------------------------------------------
813 * Low level utility subroutines for the serial driver: routines to
814 * figure out the appropriate timeout for an interrupt chain, routines
815 * to initialize and startup a serial port, and routines to shutdown a
816 * serial port. Useful stuff like that.
817 * ---------------------------------------------------------------
820 static _INLINE_ void esp_basic_init(struct esp_struct * info)
822 /* put ESPC in enhanced mode */
823 serial_out(info, UART_ESI_CMD1, ESI_SET_MODE);
825 if (info->stat_flags & ESP_STAT_NEVER_DMA)
826 serial_out(info, UART_ESI_CMD2, 0x01);
827 else
828 serial_out(info, UART_ESI_CMD2, 0x31);
830 /* disable interrupts for now */
831 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
832 serial_out(info, UART_ESI_CMD2, 0x00);
834 /* set interrupt and DMA channel */
835 serial_out(info, UART_ESI_CMD1, ESI_SET_IRQ);
837 if (info->stat_flags & ESP_STAT_NEVER_DMA)
838 serial_out(info, UART_ESI_CMD2, 0x01);
839 else
840 serial_out(info, UART_ESI_CMD2, (dma << 4) | 0x01);
842 serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
844 if (info->line % 8) /* secondary port */
845 serial_out(info, UART_ESI_CMD2, 0x0d); /* shared */
846 else if (info->irq == 9)
847 serial_out(info, UART_ESI_CMD2, 0x02);
848 else
849 serial_out(info, UART_ESI_CMD2, info->irq);
851 /* set error status mask (check this) */
852 serial_out(info, UART_ESI_CMD1, ESI_SET_ERR_MASK);
854 if (info->stat_flags & ESP_STAT_NEVER_DMA)
855 serial_out(info, UART_ESI_CMD2, 0xa1);
856 else
857 serial_out(info, UART_ESI_CMD2, 0xbd);
859 serial_out(info, UART_ESI_CMD2, 0x00);
861 /* set DMA timeout */
862 serial_out(info, UART_ESI_CMD1, ESI_SET_DMA_TMOUT);
863 serial_out(info, UART_ESI_CMD2, 0xff);
865 /* set FIFO trigger levels */
866 serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
867 serial_out(info, UART_ESI_CMD2, info->config.rx_trigger >> 8);
868 serial_out(info, UART_ESI_CMD2, info->config.rx_trigger);
869 serial_out(info, UART_ESI_CMD2, info->config.tx_trigger >> 8);
870 serial_out(info, UART_ESI_CMD2, info->config.tx_trigger);
872 /* Set clock scaling and wait states */
873 serial_out(info, UART_ESI_CMD1, ESI_SET_PRESCALAR);
874 serial_out(info, UART_ESI_CMD2, 0x04 | ESPC_SCALE);
876 /* set reinterrupt pacing */
877 serial_out(info, UART_ESI_CMD1, ESI_SET_REINTR);
878 serial_out(info, UART_ESI_CMD2, 0xff);
881 static int startup(struct esp_struct * info)
883 unsigned long flags;
884 int retval=0;
885 unsigned int num_chars;
887 save_flags(flags); cli();
889 if (info->flags & ASYNC_INITIALIZED) {
890 restore_flags(flags);
891 return retval;
894 if (!info->xmit_buf) {
895 info->xmit_buf = (unsigned char *)get_free_page(GFP_KERNEL);
896 if (!info->xmit_buf) {
897 restore_flags(flags);
898 return -ENOMEM;
902 #ifdef SERIAL_DEBUG_OPEN
903 printk("starting up ttys%d (irq %d)...", info->line, info->irq);
904 #endif
906 /* Flush the RX buffer. Using the ESI flush command may cause */
907 /* wild interrupts, so read all the data instead. */
909 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
910 serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
911 num_chars = serial_in(info, UART_ESI_STAT1) << 8;
912 num_chars |= serial_in(info, UART_ESI_STAT2);
914 while (num_chars > 1) {
915 inw(info->port + UART_ESI_RX);
916 num_chars -= 2;
919 if (num_chars)
920 serial_in(info, UART_ESI_RX);
922 /* set receive character timeout */
923 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
924 serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
926 /* clear all flags except the "never DMA" flag */
927 info->stat_flags &= ESP_STAT_NEVER_DMA;
929 if (info->stat_flags & ESP_STAT_NEVER_DMA)
930 info->stat_flags |= ESP_STAT_USE_PIO;
933 * Allocate the IRQ
936 retval = request_irq(info->irq, rs_interrupt_single, SA_SHIRQ,
937 "esp serial", info);
939 if (retval) {
940 if (capable(CAP_SYS_ADMIN)) {
941 if (info->tty)
942 set_bit(TTY_IO_ERROR,
943 &info->tty->flags);
944 retval = 0;
947 restore_flags(flags);
948 return retval;
951 if (!(info->stat_flags & ESP_STAT_USE_PIO) && !dma_buffer) {
952 dma_buffer = (char *)__get_dma_pages(
953 GFP_KERNEL, get_order(DMA_BUFFER_SZ));
955 /* use PIO mode if DMA buf/chan cannot be allocated */
956 if (!dma_buffer)
957 info->stat_flags |= ESP_STAT_USE_PIO;
958 else if (request_dma(dma, "esp serial")) {
959 free_pages((unsigned int)dma_buffer,
960 get_order(DMA_BUFFER_SZ));
961 dma_buffer = 0;
962 info->stat_flags |= ESP_STAT_USE_PIO;
967 info->MCR = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
968 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
969 serial_out(info, UART_ESI_CMD2, UART_MCR);
970 serial_out(info, UART_ESI_CMD2, info->MCR);
973 * Finally, enable interrupts
975 /* info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI; */
976 info->IER = UART_IER_RLSI | UART_IER_RDI | UART_IER_DMA_TMOUT |
977 UART_IER_DMA_TC;
978 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
979 serial_out(info, UART_ESI_CMD2, info->IER);
981 if (info->tty)
982 clear_bit(TTY_IO_ERROR, &info->tty->flags);
983 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
986 * Set up the tty->alt_speed kludge
988 if (info->tty) {
989 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
990 info->tty->alt_speed = 57600;
991 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
992 info->tty->alt_speed = 115200;
993 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
994 info->tty->alt_speed = 230400;
995 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
996 info->tty->alt_speed = 460800;
1000 * set the speed of the serial port
1002 change_speed(info);
1004 info->flags |= ASYNC_INITIALIZED;
1005 restore_flags(flags);
1006 return 0;
1010 * This routine will shutdown a serial port; interrupts are disabled, and
1011 * DTR is dropped if the hangup on close termio flag is on.
1013 static void shutdown(struct esp_struct * info)
1015 unsigned long flags, f;
1017 if (!(info->flags & ASYNC_INITIALIZED))
1018 return;
1020 #ifdef SERIAL_DEBUG_OPEN
1021 printk("Shutting down serial port %d (irq %d)....", info->line,
1022 info->irq);
1023 #endif
1025 save_flags(flags); cli(); /* Disable interrupts */
1028 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1029 * here so the queue might never be waken up
1031 wake_up_interruptible(&info->delta_msr_wait);
1032 wake_up_interruptible(&info->break_wait);
1034 /* stop a DMA transfer on the port being closed */
1036 if (info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) {
1037 f=claim_dma_lock();
1038 disable_dma(dma);
1039 clear_dma_ff(dma);
1040 release_dma_lock(f);
1042 dma_bytes = 0;
1046 * Free the IRQ
1048 free_irq(info->irq, info);
1050 if (dma_buffer) {
1051 struct esp_struct *current_port = ports;
1053 while (current_port) {
1054 if ((current_port != info) &&
1055 (current_port->flags & ASYNC_INITIALIZED))
1056 break;
1058 current_port = current_port->next_port;
1061 if (!current_port) {
1062 free_dma(dma);
1063 free_pages((unsigned int)dma_buffer,
1064 get_order(DMA_BUFFER_SZ));
1065 dma_buffer = 0;
1069 if (info->xmit_buf) {
1070 free_page((unsigned long) info->xmit_buf);
1071 info->xmit_buf = 0;
1074 info->IER = 0;
1075 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1076 serial_out(info, UART_ESI_CMD2, 0x00);
1078 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
1079 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1081 info->MCR &= ~UART_MCR_OUT2;
1082 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1083 serial_out(info, UART_ESI_CMD2, UART_MCR);
1084 serial_out(info, UART_ESI_CMD2, info->MCR);
1086 if (info->tty)
1087 set_bit(TTY_IO_ERROR, &info->tty->flags);
1089 info->flags &= ~ASYNC_INITIALIZED;
1090 restore_flags(flags);
1094 * This routine is called to set the UART divisor registers to match
1095 * the specified baud rate for a serial port.
1097 static void change_speed(struct esp_struct *info)
1099 unsigned short port;
1100 int quot = 0;
1101 unsigned cflag,cval;
1102 int baud, bits;
1103 unsigned char flow1 = 0, flow2 = 0;
1104 unsigned long flags;
1106 if (!info->tty || !info->tty->termios)
1107 return;
1108 cflag = info->tty->termios->c_cflag;
1109 port = info->port;
1111 /* byte size and parity */
1112 switch (cflag & CSIZE) {
1113 case CS5: cval = 0x00; bits = 7; break;
1114 case CS6: cval = 0x01; bits = 8; break;
1115 case CS7: cval = 0x02; bits = 9; break;
1116 case CS8: cval = 0x03; bits = 10; break;
1117 default: cval = 0x00; bits = 7; break;
1119 if (cflag & CSTOPB) {
1120 cval |= 0x04;
1121 bits++;
1123 if (cflag & PARENB) {
1124 cval |= UART_LCR_PARITY;
1125 bits++;
1127 if (!(cflag & PARODD))
1128 cval |= UART_LCR_EPAR;
1129 #ifdef CMSPAR
1130 if (cflag & CMSPAR)
1131 cval |= UART_LCR_SPAR;
1132 #endif
1134 baud = tty_get_baud_rate(info->tty);
1135 if (baud == 38400 &&
1136 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1137 quot = info->custom_divisor;
1138 else {
1139 if (baud == 134)
1140 /* Special case since 134 is really 134.5 */
1141 quot = (2*BASE_BAUD / 269);
1142 else if (baud)
1143 quot = BASE_BAUD / baud;
1145 /* If the quotient is ever zero, default to 9600 bps */
1146 if (!quot)
1147 quot = BASE_BAUD / 9600;
1149 info->timeout = ((1024 * HZ * bits * quot) / BASE_BAUD) + (HZ / 50);
1151 /* CTS flow control flag and modem status interrupts */
1152 /* info->IER &= ~UART_IER_MSI; */
1153 if (cflag & CRTSCTS) {
1154 info->flags |= ASYNC_CTS_FLOW;
1155 /* info->IER |= UART_IER_MSI; */
1156 flow1 = 0x04;
1157 flow2 = 0x10;
1158 } else
1159 info->flags &= ~ASYNC_CTS_FLOW;
1160 if (cflag & CLOCAL)
1161 info->flags &= ~ASYNC_CHECK_CD;
1162 else {
1163 info->flags |= ASYNC_CHECK_CD;
1164 /* info->IER |= UART_IER_MSI; */
1168 * Set up parity check flag
1170 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1172 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1173 if (I_INPCK(info->tty))
1174 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1175 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1176 info->read_status_mask |= UART_LSR_BI;
1178 info->ignore_status_mask = 0;
1179 #if 0
1180 /* This should be safe, but for some broken bits of hardware... */
1181 if (I_IGNPAR(info->tty)) {
1182 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1183 info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
1185 #endif
1186 if (I_IGNBRK(info->tty)) {
1187 info->ignore_status_mask |= UART_LSR_BI;
1188 info->read_status_mask |= UART_LSR_BI;
1190 * If we're ignore parity and break indicators, ignore
1191 * overruns too. (For real raw support).
1193 if (I_IGNPAR(info->tty)) {
1194 info->ignore_status_mask |= UART_LSR_OE | \
1195 UART_LSR_PE | UART_LSR_FE;
1196 info->read_status_mask |= UART_LSR_OE | \
1197 UART_LSR_PE | UART_LSR_FE;
1201 if (I_IXOFF(info->tty))
1202 flow1 |= 0x81;
1204 save_flags(flags); cli();
1205 /* set baud */
1206 serial_out(info, UART_ESI_CMD1, ESI_SET_BAUD);
1207 serial_out(info, UART_ESI_CMD2, quot >> 8);
1208 serial_out(info, UART_ESI_CMD2, quot & 0xff);
1210 /* set data bits, parity, etc. */
1211 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1212 serial_out(info, UART_ESI_CMD2, UART_LCR);
1213 serial_out(info, UART_ESI_CMD2, cval);
1215 /* Enable flow control */
1216 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CNTL);
1217 serial_out(info, UART_ESI_CMD2, flow1);
1218 serial_out(info, UART_ESI_CMD2, flow2);
1220 /* set flow control characters (XON/XOFF only) */
1221 if (I_IXOFF(info->tty)) {
1222 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CHARS);
1223 serial_out(info, UART_ESI_CMD2, START_CHAR(info->tty));
1224 serial_out(info, UART_ESI_CMD2, STOP_CHAR(info->tty));
1225 serial_out(info, UART_ESI_CMD2, 0x10);
1226 serial_out(info, UART_ESI_CMD2, 0x21);
1227 switch (cflag & CSIZE) {
1228 case CS5:
1229 serial_out(info, UART_ESI_CMD2, 0x1f);
1230 break;
1231 case CS6:
1232 serial_out(info, UART_ESI_CMD2, 0x3f);
1233 break;
1234 case CS7:
1235 case CS8:
1236 serial_out(info, UART_ESI_CMD2, 0x7f);
1237 break;
1238 default:
1239 serial_out(info, UART_ESI_CMD2, 0xff);
1240 break;
1244 /* Set high/low water */
1245 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1246 serial_out(info, UART_ESI_CMD2, info->config.flow_off >> 8);
1247 serial_out(info, UART_ESI_CMD2, info->config.flow_off);
1248 serial_out(info, UART_ESI_CMD2, info->config.flow_on >> 8);
1249 serial_out(info, UART_ESI_CMD2, info->config.flow_on);
1251 restore_flags(flags);
1254 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
1256 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1257 unsigned long flags;
1259 if (serial_paranoia_check(info, tty->device, "rs_put_char"))
1260 return;
1262 if (!tty || !info->xmit_buf)
1263 return;
1265 save_flags(flags); cli();
1266 if (info->xmit_cnt >= ESP_XMIT_SIZE - 1) {
1267 restore_flags(flags);
1268 return;
1271 info->xmit_buf[info->xmit_head++] = ch;
1272 info->xmit_head &= ESP_XMIT_SIZE-1;
1273 info->xmit_cnt++;
1274 restore_flags(flags);
1277 static void rs_flush_chars(struct tty_struct *tty)
1279 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1280 unsigned long flags;
1282 if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
1283 return;
1285 if (info->xmit_cnt <= 0 || tty->stopped || !info->xmit_buf)
1286 return;
1288 save_flags(flags); cli();
1289 if (!(info->IER & UART_IER_THRI)) {
1290 info->IER |= UART_IER_THRI;
1291 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1292 serial_out(info, UART_ESI_CMD2, info->IER);
1294 restore_flags(flags);
1297 static int rs_write(struct tty_struct * tty, int from_user,
1298 const unsigned char *buf, int count)
1300 int c, t, ret = 0;
1301 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1302 unsigned long flags;
1304 if (serial_paranoia_check(info, tty->device, "rs_write"))
1305 return 0;
1307 if (!tty || !info->xmit_buf || !tmp_buf)
1308 return 0;
1310 if (from_user)
1311 down(&tmp_buf_sem);
1313 while (1) {
1314 /* Thanks to R. Wolff for suggesting how to do this with */
1315 /* interrupts enabled */
1317 c = count;
1318 t = ESP_XMIT_SIZE - info->xmit_cnt - 1;
1320 if (t < c)
1321 c = t;
1323 t = ESP_XMIT_SIZE - info->xmit_head;
1325 if (t < c)
1326 c = t;
1328 if (c <= 0)
1329 break;
1331 if (from_user) {
1332 c -= copy_from_user(tmp_buf, buf, c);
1334 if (!c) {
1335 if (!ret)
1336 ret = -EFAULT;
1337 break;
1340 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1341 } else
1342 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1344 info->xmit_head = (info->xmit_head + c) & (ESP_XMIT_SIZE-1);
1345 info->xmit_cnt += c;
1346 buf += c;
1347 count -= c;
1348 ret += c;
1351 if (from_user)
1352 up(&tmp_buf_sem);
1354 save_flags(flags); cli();
1356 if (info->xmit_cnt && !tty->stopped && !(info->IER & UART_IER_THRI)) {
1357 info->IER |= UART_IER_THRI;
1358 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1359 serial_out(info, UART_ESI_CMD2, info->IER);
1362 restore_flags(flags);
1363 return ret;
1366 static int rs_write_room(struct tty_struct *tty)
1368 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1369 int ret;
1371 if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1372 return 0;
1373 ret = ESP_XMIT_SIZE - info->xmit_cnt - 1;
1374 if (ret < 0)
1375 ret = 0;
1376 return ret;
1379 static int rs_chars_in_buffer(struct tty_struct *tty)
1381 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1383 if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1384 return 0;
1385 return info->xmit_cnt;
1388 static void rs_flush_buffer(struct tty_struct *tty)
1390 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1392 if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1393 return;
1394 cli();
1395 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1396 sti();
1397 wake_up_interruptible(&tty->write_wait);
1398 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1399 tty->ldisc.write_wakeup)
1400 (tty->ldisc.write_wakeup)(tty);
1404 * ------------------------------------------------------------
1405 * rs_throttle()
1407 * This routine is called by the upper-layer tty layer to signal that
1408 * incoming characters should be throttled.
1409 * ------------------------------------------------------------
1411 static void rs_throttle(struct tty_struct * tty)
1413 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1414 #ifdef SERIAL_DEBUG_THROTTLE
1415 char buf[64];
1417 printk("throttle %s: %d....\n", tty_name(tty, buf),
1418 tty->ldisc.chars_in_buffer(tty));
1419 #endif
1421 if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1422 return;
1424 cli();
1425 info->IER &= ~UART_IER_RDI;
1426 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1427 serial_out(info, UART_ESI_CMD2, info->IER);
1428 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1429 serial_out(info, UART_ESI_CMD2, 0x00);
1430 sti();
1433 static void rs_unthrottle(struct tty_struct * tty)
1435 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1436 #ifdef SERIAL_DEBUG_THROTTLE
1437 char buf[64];
1439 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1440 tty->ldisc.chars_in_buffer(tty));
1441 #endif
1443 if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1444 return;
1446 cli();
1447 info->IER |= UART_IER_RDI;
1448 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1449 serial_out(info, UART_ESI_CMD2, info->IER);
1450 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1451 serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
1452 sti();
1456 * ------------------------------------------------------------
1457 * rs_ioctl() and friends
1458 * ------------------------------------------------------------
1461 static int get_serial_info(struct esp_struct * info,
1462 struct serial_struct * retinfo)
1464 struct serial_struct tmp;
1466 if (!retinfo)
1467 return -EFAULT;
1468 memset(&tmp, 0, sizeof(tmp));
1469 tmp.type = PORT_16550A;
1470 tmp.line = info->line;
1471 tmp.port = info->port;
1472 tmp.irq = info->irq;
1473 tmp.flags = info->flags;
1474 tmp.xmit_fifo_size = 1024;
1475 tmp.baud_base = BASE_BAUD;
1476 tmp.close_delay = info->close_delay;
1477 tmp.closing_wait = info->closing_wait;
1478 tmp.custom_divisor = info->custom_divisor;
1479 tmp.hub6 = 0;
1480 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1481 return -EFAULT;
1482 return 0;
1485 static int get_esp_config(struct esp_struct * info,
1486 struct hayes_esp_config * retinfo)
1488 struct hayes_esp_config tmp;
1490 if (!retinfo)
1491 return -EFAULT;
1493 memset(&tmp, 0, sizeof(tmp));
1494 tmp.rx_timeout = info->config.rx_timeout;
1495 tmp.rx_trigger = info->config.rx_trigger;
1496 tmp.tx_trigger = info->config.tx_trigger;
1497 tmp.flow_off = info->config.flow_off;
1498 tmp.flow_on = info->config.flow_on;
1499 tmp.pio_threshold = info->config.pio_threshold;
1500 tmp.dma_channel = (info->stat_flags & ESP_STAT_NEVER_DMA ? 0 : dma);
1502 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1503 return -EFAULT;
1505 return 0;
1508 static int set_serial_info(struct esp_struct * info,
1509 struct serial_struct * new_info)
1511 struct serial_struct new_serial;
1512 struct esp_struct old_info;
1513 unsigned int change_irq;
1514 int retval = 0;
1515 struct esp_struct *current_async;
1517 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1518 return -EFAULT;
1519 old_info = *info;
1521 if ((new_serial.type != PORT_16550A) ||
1522 (new_serial.hub6) ||
1523 (info->port != new_serial.port) ||
1524 (new_serial.baud_base != BASE_BAUD) ||
1525 (new_serial.irq > 15) ||
1526 (new_serial.irq < 2) ||
1527 (new_serial.irq == 6) ||
1528 (new_serial.irq == 8) ||
1529 (new_serial.irq == 13))
1530 return -EINVAL;
1532 change_irq = new_serial.irq != info->irq;
1534 if (change_irq && (info->line % 8))
1535 return -EINVAL;
1537 if (!capable(CAP_SYS_ADMIN)) {
1538 if (change_irq ||
1539 (new_serial.close_delay != info->close_delay) ||
1540 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1541 (info->flags & ~ASYNC_USR_MASK)))
1542 return -EPERM;
1543 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1544 (new_serial.flags & ASYNC_USR_MASK));
1545 info->custom_divisor = new_serial.custom_divisor;
1546 } else {
1547 if (new_serial.irq == 2)
1548 new_serial.irq = 9;
1550 if (change_irq) {
1551 current_async = ports;
1553 while (current_async) {
1554 if ((current_async->line >= info->line) &&
1555 (current_async->line < (info->line + 8))) {
1556 if (current_async == info) {
1557 if (current_async->count > 1)
1558 return -EBUSY;
1559 } else if (current_async->count)
1560 return -EBUSY;
1563 current_async = current_async->next_port;
1568 * OK, past this point, all the error checking has been done.
1569 * At this point, we start making changes.....
1572 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1573 (new_serial.flags & ASYNC_FLAGS));
1574 info->custom_divisor = new_serial.custom_divisor;
1575 info->close_delay = new_serial.close_delay * HZ/100;
1576 info->closing_wait = new_serial.closing_wait * HZ/100;
1578 if (change_irq) {
1580 * We need to shutdown the serial port at the old
1581 * port/irq combination.
1583 shutdown(info);
1585 current_async = ports;
1587 while (current_async) {
1588 if ((current_async->line >= info->line) &&
1589 (current_async->line < (info->line + 8)))
1590 current_async->irq = new_serial.irq;
1592 current_async = current_async->next_port;
1595 serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
1596 if (info->irq == 9)
1597 serial_out(info, UART_ESI_CMD2, 0x02);
1598 else
1599 serial_out(info, UART_ESI_CMD2, info->irq);
1603 if (info->flags & ASYNC_INITIALIZED) {
1604 if (((old_info.flags & ASYNC_SPD_MASK) !=
1605 (info->flags & ASYNC_SPD_MASK)) ||
1606 (old_info.custom_divisor != info->custom_divisor)) {
1607 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1608 info->tty->alt_speed = 57600;
1609 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1610 info->tty->alt_speed = 115200;
1611 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1612 info->tty->alt_speed = 230400;
1613 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1614 info->tty->alt_speed = 460800;
1615 change_speed(info);
1617 } else
1618 retval = startup(info);
1620 return retval;
1623 static int set_esp_config(struct esp_struct * info,
1624 struct hayes_esp_config * new_info)
1626 struct hayes_esp_config new_config;
1627 unsigned int change_dma;
1628 int retval = 0;
1629 struct esp_struct *current_async;
1631 /* Perhaps a non-sysadmin user should be able to do some of these */
1632 /* operations. I haven't decided yet. */
1634 if (!capable(CAP_SYS_ADMIN))
1635 return -EPERM;
1637 if (copy_from_user(&new_config, new_info, sizeof(new_config)))
1638 return -EFAULT;
1640 if ((new_config.flow_on >= new_config.flow_off) ||
1641 (new_config.rx_trigger < 1) ||
1642 (new_config.tx_trigger < 1) ||
1643 (new_config.flow_off < 1) ||
1644 (new_config.flow_on < 1) ||
1645 (new_config.rx_trigger > 1023) ||
1646 (new_config.tx_trigger > 1023) ||
1647 (new_config.flow_off > 1023) ||
1648 (new_config.flow_on > 1023) ||
1649 (new_config.pio_threshold < 0) ||
1650 (new_config.pio_threshold > 1024))
1651 return -EINVAL;
1653 if ((new_config.dma_channel != 1) && (new_config.dma_channel != 3))
1654 new_config.dma_channel = 0;
1656 if (info->stat_flags & ESP_STAT_NEVER_DMA)
1657 change_dma = new_config.dma_channel;
1658 else
1659 change_dma = (new_config.dma_channel != dma);
1661 if (change_dma) {
1662 if (new_config.dma_channel) {
1663 /* PIO mode to DMA mode transition OR */
1664 /* change current DMA channel */
1666 current_async = ports;
1668 while (current_async) {
1669 if (current_async == info) {
1670 if (current_async->count > 1)
1671 return -EBUSY;
1672 } else if (current_async->count)
1673 return -EBUSY;
1675 current_async =
1676 current_async->next_port;
1679 shutdown(info);
1680 dma = new_config.dma_channel;
1681 info->stat_flags &= ~ESP_STAT_NEVER_DMA;
1683 /* all ports must use the same DMA channel */
1685 current_async = ports;
1687 while (current_async) {
1688 esp_basic_init(current_async);
1689 current_async = current_async->next_port;
1691 } else {
1692 /* DMA mode to PIO mode only */
1694 if (info->count > 1)
1695 return -EBUSY;
1697 shutdown(info);
1698 info->stat_flags |= ESP_STAT_NEVER_DMA;
1699 esp_basic_init(info);
1703 info->config.pio_threshold = new_config.pio_threshold;
1705 if ((new_config.flow_off != info->config.flow_off) ||
1706 (new_config.flow_on != info->config.flow_on)) {
1707 unsigned long flags;
1709 info->config.flow_off = new_config.flow_off;
1710 info->config.flow_on = new_config.flow_on;
1711 save_flags(flags); cli();
1712 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1713 serial_out(info, UART_ESI_CMD2, new_config.flow_off >> 8);
1714 serial_out(info, UART_ESI_CMD2, new_config.flow_off);
1715 serial_out(info, UART_ESI_CMD2, new_config.flow_on >> 8);
1716 serial_out(info, UART_ESI_CMD2, new_config.flow_on);
1717 restore_flags(flags);
1720 if ((new_config.rx_trigger != info->config.rx_trigger) ||
1721 (new_config.tx_trigger != info->config.tx_trigger)) {
1722 unsigned long flags;
1724 info->config.rx_trigger = new_config.rx_trigger;
1725 info->config.tx_trigger = new_config.tx_trigger;
1726 save_flags(flags); cli();
1727 serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
1728 serial_out(info, UART_ESI_CMD2,
1729 new_config.rx_trigger >> 8);
1730 serial_out(info, UART_ESI_CMD2, new_config.rx_trigger);
1731 serial_out(info, UART_ESI_CMD2,
1732 new_config.tx_trigger >> 8);
1733 serial_out(info, UART_ESI_CMD2, new_config.tx_trigger);
1734 restore_flags(flags);
1737 if (new_config.rx_timeout != info->config.rx_timeout) {
1738 unsigned long flags;
1740 info->config.rx_timeout = new_config.rx_timeout;
1741 save_flags(flags); cli();
1743 if (info->IER & UART_IER_RDI) {
1744 serial_out(info, UART_ESI_CMD1,
1745 ESI_SET_RX_TIMEOUT);
1746 serial_out(info, UART_ESI_CMD2,
1747 new_config.rx_timeout);
1750 restore_flags(flags);
1753 if (!(info->flags & ASYNC_INITIALIZED))
1754 retval = startup(info);
1756 return retval;
1760 * get_lsr_info - get line status register info
1762 * Purpose: Let user call ioctl() to get info when the UART physically
1763 * is emptied. On bus types like RS485, the transmitter must
1764 * release the bus after transmitting. This must be done when
1765 * the transmit shift register is empty, not be done when the
1766 * transmit holding register is empty. This functionality
1767 * allows an RS485 driver to be written in user space.
1769 static int get_lsr_info(struct esp_struct * info, unsigned int *value)
1771 unsigned char status;
1772 unsigned int result;
1774 cli();
1775 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1776 status = serial_in(info, UART_ESI_STAT1);
1777 sti();
1778 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1779 return put_user(result,value);
1783 static int get_modem_info(struct esp_struct * info, unsigned int *value)
1785 unsigned char control, status;
1786 unsigned int result;
1788 control = info->MCR;
1789 cli();
1790 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1791 status = serial_in(info, UART_ESI_STAT2);
1792 sti();
1793 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1794 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1795 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1796 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1797 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1798 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1799 return put_user(result,value);
1802 static int set_modem_info(struct esp_struct * info, unsigned int cmd,
1803 unsigned int *value)
1805 int error;
1806 unsigned int arg;
1808 error = get_user(arg, value);
1809 if (error)
1810 return error;
1812 switch (cmd) {
1813 case TIOCMBIS:
1814 if (arg & TIOCM_RTS)
1815 info->MCR |= UART_MCR_RTS;
1816 if (arg & TIOCM_DTR)
1817 info->MCR |= UART_MCR_DTR;
1818 break;
1819 case TIOCMBIC:
1820 if (arg & TIOCM_RTS)
1821 info->MCR &= ~UART_MCR_RTS;
1822 if (arg & TIOCM_DTR)
1823 info->MCR &= ~UART_MCR_DTR;
1824 break;
1825 case TIOCMSET:
1826 info->MCR = ((info->MCR & ~(UART_MCR_RTS | UART_MCR_DTR))
1827 | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1828 | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1829 break;
1830 default:
1831 return -EINVAL;
1833 cli();
1834 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1835 serial_out(info, UART_ESI_CMD2, UART_MCR);
1836 serial_out(info, UART_ESI_CMD2, info->MCR);
1837 sti();
1838 return 0;
1842 * rs_break() --- routine which turns the break handling on or off
1844 static void esp_break(struct tty_struct *tty, int break_state)
1846 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1847 unsigned long flags;
1849 if (serial_paranoia_check(info, tty->device, "esp_break"))
1850 return;
1852 save_flags(flags); cli();
1853 if (break_state == -1) {
1854 serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1855 serial_out(info, UART_ESI_CMD2, 0x01);
1857 interruptible_sleep_on(&info->break_wait);
1858 } else {
1859 serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1860 serial_out(info, UART_ESI_CMD2, 0x00);
1862 restore_flags(flags);
1865 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1866 unsigned int cmd, unsigned long arg)
1868 int error;
1869 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1870 struct async_icount cprev, cnow; /* kernel counter temps */
1871 struct serial_icounter_struct *p_cuser; /* user space */
1873 if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1874 return -ENODEV;
1876 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1877 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1878 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT) &&
1879 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT) &&
1880 (cmd != TIOCGHAYESESP) && (cmd != TIOCSHAYESESP)) {
1881 if (tty->flags & (1 << TTY_IO_ERROR))
1882 return -EIO;
1885 switch (cmd) {
1886 case TIOCMGET:
1887 return get_modem_info(info, (unsigned int *) arg);
1888 case TIOCMBIS:
1889 case TIOCMBIC:
1890 case TIOCMSET:
1891 return set_modem_info(info, cmd, (unsigned int *) arg);
1892 case TIOCGSERIAL:
1893 return get_serial_info(info,
1894 (struct serial_struct *) arg);
1895 case TIOCSSERIAL:
1896 return set_serial_info(info,
1897 (struct serial_struct *) arg);
1898 case TIOCSERCONFIG:
1899 /* do not reconfigure after initial configuration */
1900 return 0;
1902 case TIOCSERGWILD:
1903 return put_user(0L, (unsigned long *) arg);
1905 case TIOCSERGETLSR: /* Get line status register */
1906 return get_lsr_info(info, (unsigned int *) arg);
1908 case TIOCSERSWILD:
1909 if (!suser())
1910 return -EPERM;
1911 return 0;
1914 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1915 * - mask passed in arg for lines of interest
1916 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1917 * Caller should use TIOCGICOUNT to see which one it was
1919 case TIOCMIWAIT:
1920 cli();
1921 cprev = info->icount; /* note the counters on entry */
1922 sti();
1923 while (1) {
1924 interruptible_sleep_on(&info->delta_msr_wait);
1925 /* see if a signal did it */
1926 if (signal_pending(current))
1927 return -ERESTARTSYS;
1928 cli();
1929 cnow = info->icount; /* atomic copy */
1930 sti();
1931 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1932 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1933 return -EIO; /* no change => error */
1934 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1935 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1936 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1937 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1938 return 0;
1940 cprev = cnow;
1942 /* NOTREACHED */
1945 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1946 * Return: write counters to the user passed counter struct
1947 * NB: both 1->0 and 0->1 transitions are counted except for
1948 * RI where only 0->1 is counted.
1950 case TIOCGICOUNT:
1951 cli();
1952 cnow = info->icount;
1953 sti();
1954 p_cuser = (struct serial_icounter_struct *) arg;
1955 if ((error = put_user(cnow.cts, &p_cuser->cts)))
1956 return error;
1957 if ((error = put_user(cnow.dsr, &p_cuser->dsr)))
1958 return error;
1959 if ((error = put_user(cnow.rng, &p_cuser->rng)))
1960 return error;
1961 if ((error = put_user(cnow.dcd, &p_cuser->dcd)))
1962 return error;
1964 return 0;
1965 case TIOCGHAYESESP:
1966 return (get_esp_config(info,
1967 (struct hayes_esp_config *)arg));
1968 case TIOCSHAYESESP:
1969 return (set_esp_config(info,
1970 (struct hayes_esp_config *)arg));
1972 default:
1973 return -ENOIOCTLCMD;
1975 return 0;
1978 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1980 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1982 if ( (tty->termios->c_cflag == old_termios->c_cflag)
1983 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
1984 == RELEVANT_IFLAG(old_termios->c_iflag)))
1985 return;
1987 change_speed(info);
1989 /* Handle transition to B0 status */
1990 if ((old_termios->c_cflag & CBAUD) &&
1991 !(tty->termios->c_cflag & CBAUD)) {
1992 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1993 cli();
1994 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1995 serial_out(info, UART_ESI_CMD2, UART_MCR);
1996 serial_out(info, UART_ESI_CMD2, info->MCR);
1997 sti();
2000 /* Handle transition away from B0 status */
2001 if (!(old_termios->c_cflag & CBAUD) &&
2002 (tty->termios->c_cflag & CBAUD)) {
2003 info->MCR |= (UART_MCR_DTR | UART_MCR_RTS);
2004 cli();
2005 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2006 serial_out(info, UART_ESI_CMD2, UART_MCR);
2007 serial_out(info, UART_ESI_CMD2, info->MCR);
2008 sti();
2011 /* Handle turning of CRTSCTS */
2012 if ((old_termios->c_cflag & CRTSCTS) &&
2013 !(tty->termios->c_cflag & CRTSCTS)) {
2014 rs_start(tty);
2017 #if 0
2019 * No need to wake up processes in open wait, since they
2020 * sample the CLOCAL flag once, and don't recheck it.
2021 * XXX It's not clear whether the current behavior is correct
2022 * or not. Hence, this may change.....
2024 if (!(old_termios->c_cflag & CLOCAL) &&
2025 (tty->termios->c_cflag & CLOCAL))
2026 wake_up_interruptible(&info->open_wait);
2027 #endif
2031 * ------------------------------------------------------------
2032 * rs_close()
2034 * This routine is called when the serial port gets closed. First, we
2035 * wait for the last remaining data to be sent. Then, we unlink its
2036 * async structure from the interrupt chain if necessary, and we free
2037 * that IRQ if nothing is left in the chain.
2038 * ------------------------------------------------------------
2040 static void rs_close(struct tty_struct *tty, struct file * filp)
2042 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
2043 unsigned long flags;
2045 if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
2046 return;
2048 save_flags(flags); cli();
2050 if (tty_hung_up_p(filp)) {
2051 DBG_CNT("before DEC-hung");
2052 MOD_DEC_USE_COUNT;
2053 restore_flags(flags);
2054 return;
2057 #ifdef SERIAL_DEBUG_OPEN
2058 printk("rs_close ttys%d, count = %d\n", info->line, info->count);
2059 #endif
2060 if ((tty->count == 1) && (info->count != 1)) {
2062 * Uh, oh. tty->count is 1, which means that the tty
2063 * structure will be freed. Info->count should always
2064 * be one in these conditions. If it's greater than
2065 * one, we've got real problems, since it means the
2066 * serial port won't be shutdown.
2068 printk("rs_close: bad serial port count; tty->count is 1, "
2069 "info->count is %d\n", info->count);
2070 info->count = 1;
2072 if (--info->count < 0) {
2073 printk("rs_close: bad serial port count for ttys%d: %d\n",
2074 info->line, info->count);
2075 info->count = 0;
2077 if (info->count) {
2078 DBG_CNT("before DEC-2");
2079 MOD_DEC_USE_COUNT;
2080 restore_flags(flags);
2081 return;
2083 info->flags |= ASYNC_CLOSING;
2085 * Save the termios structure, since this port may have
2086 * separate termios for callout and dialin.
2088 if (info->flags & ASYNC_NORMAL_ACTIVE)
2089 info->normal_termios = *tty->termios;
2090 if (info->flags & ASYNC_CALLOUT_ACTIVE)
2091 info->callout_termios = *tty->termios;
2093 * Now we wait for the transmit buffer to clear; and we notify
2094 * the line discipline to only process XON/XOFF characters.
2096 tty->closing = 1;
2097 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
2098 tty_wait_until_sent(tty, info->closing_wait);
2100 * At this point we stop accepting input. To do this, we
2101 * disable the receive line status interrupts, and tell the
2102 * interrupt driver to stop checking the data ready bit in the
2103 * line status register.
2105 /* info->IER &= ~UART_IER_RLSI; */
2106 info->IER &= ~UART_IER_RDI;
2107 info->read_status_mask &= ~UART_LSR_DR;
2108 if (info->flags & ASYNC_INITIALIZED) {
2109 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
2110 serial_out(info, UART_ESI_CMD2, info->IER);
2112 /* disable receive timeout */
2113 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
2114 serial_out(info, UART_ESI_CMD2, 0x00);
2117 * Before we drop DTR, make sure the UART transmitter
2118 * has completely drained; this is especially
2119 * important if there is a transmit FIFO!
2121 rs_wait_until_sent(tty, info->timeout);
2123 shutdown(info);
2124 if (tty->driver.flush_buffer)
2125 tty->driver.flush_buffer(tty);
2126 if (tty->ldisc.flush_buffer)
2127 tty->ldisc.flush_buffer(tty);
2128 tty->closing = 0;
2129 info->event = 0;
2130 info->tty = 0;
2132 if (info->blocked_open) {
2133 if (info->close_delay) {
2134 current->state = TASK_INTERRUPTIBLE;
2135 schedule_timeout(info->close_delay);
2137 wake_up_interruptible(&info->open_wait);
2139 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
2140 ASYNC_CLOSING);
2141 wake_up_interruptible(&info->close_wait);
2142 MOD_DEC_USE_COUNT;
2143 restore_flags(flags);
2146 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
2148 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
2149 unsigned long orig_jiffies, char_time;
2150 unsigned long flags;
2152 if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
2153 return;
2155 orig_jiffies = jiffies;
2156 char_time = ((info->timeout - HZ / 50) / 1024) / 5;
2158 if (!char_time)
2159 char_time = 1;
2161 save_flags(flags); cli();
2162 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2163 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2165 while ((serial_in(info, UART_ESI_STAT1) != 0x03) ||
2166 (serial_in(info, UART_ESI_STAT2) != 0xff)) {
2167 current->state = TASK_INTERRUPTIBLE;
2168 schedule_timeout(char_time);
2170 if (signal_pending(current))
2171 break;
2173 if (timeout && ((orig_jiffies + timeout) < jiffies))
2174 break;
2176 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2177 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2180 restore_flags(flags);
2181 current->state = TASK_RUNNING;
2185 * esp_hangup() --- called by tty_hangup() when a hangup is signaled.
2187 static void esp_hangup(struct tty_struct *tty)
2189 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
2191 if (serial_paranoia_check(info, tty->device, "esp_hangup"))
2192 return;
2194 rs_flush_buffer(tty);
2195 shutdown(info);
2196 info->event = 0;
2197 info->count = 0;
2198 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
2199 info->tty = 0;
2200 wake_up_interruptible(&info->open_wait);
2204 * ------------------------------------------------------------
2205 * esp_open() and friends
2206 * ------------------------------------------------------------
2208 static int block_til_ready(struct tty_struct *tty, struct file * filp,
2209 struct esp_struct *info)
2211 DECLARE_WAITQUEUE(wait, current);
2212 int retval;
2213 int do_clocal = 0;
2216 * If the device is in the middle of being closed, then block
2217 * until it's done, and then try again.
2219 if (tty_hung_up_p(filp) ||
2220 (info->flags & ASYNC_CLOSING)) {
2221 if (info->flags & ASYNC_CLOSING)
2222 interruptible_sleep_on(&info->close_wait);
2223 #ifdef SERIAL_DO_RESTART
2224 if (info->flags & ASYNC_HUP_NOTIFY)
2225 return -EAGAIN;
2226 else
2227 return -ERESTARTSYS;
2228 #else
2229 return -EAGAIN;
2230 #endif
2234 * If this is a callout device, then just make sure the normal
2235 * device isn't being used.
2237 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
2238 if (info->flags & ASYNC_NORMAL_ACTIVE)
2239 return -EBUSY;
2240 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2241 (info->flags & ASYNC_SESSION_LOCKOUT) &&
2242 (info->session != current->session))
2243 return -EBUSY;
2244 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2245 (info->flags & ASYNC_PGRP_LOCKOUT) &&
2246 (info->pgrp != current->pgrp))
2247 return -EBUSY;
2248 info->flags |= ASYNC_CALLOUT_ACTIVE;
2249 return 0;
2253 * If non-blocking mode is set, or the port is not enabled,
2254 * then make the check up front and then exit.
2256 if ((filp->f_flags & O_NONBLOCK) ||
2257 (tty->flags & (1 << TTY_IO_ERROR))) {
2258 if (info->flags & ASYNC_CALLOUT_ACTIVE)
2259 return -EBUSY;
2260 info->flags |= ASYNC_NORMAL_ACTIVE;
2261 return 0;
2264 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
2265 if (info->normal_termios.c_cflag & CLOCAL)
2266 do_clocal = 1;
2267 } else {
2268 if (tty->termios->c_cflag & CLOCAL)
2269 do_clocal = 1;
2273 * Block waiting for the carrier detect and the line to become
2274 * free (i.e., not in use by the callout). While we are in
2275 * this loop, info->count is dropped by one, so that
2276 * rs_close() knows when to free things. We restore it upon
2277 * exit, either normal or abnormal.
2279 retval = 0;
2280 add_wait_queue(&info->open_wait, &wait);
2281 #ifdef SERIAL_DEBUG_OPEN
2282 printk("block_til_ready before block: ttys%d, count = %d\n",
2283 info->line, info->count);
2284 #endif
2285 cli();
2286 if (!tty_hung_up_p(filp))
2287 info->count--;
2288 sti();
2289 info->blocked_open++;
2290 while (1) {
2291 cli();
2292 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2293 (tty->termios->c_cflag & CBAUD)) {
2294 unsigned int scratch;
2296 serial_out(info, UART_ESI_CMD1, ESI_READ_UART);
2297 serial_out(info, UART_ESI_CMD2, UART_MCR);
2298 scratch = serial_in(info, UART_ESI_STAT1);
2299 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2300 serial_out(info, UART_ESI_CMD2, UART_MCR);
2301 serial_out(info, UART_ESI_CMD2,
2302 scratch | UART_MCR_DTR | UART_MCR_RTS);
2304 sti();
2305 set_current_state(TASK_INTERRUPTIBLE);
2306 if (tty_hung_up_p(filp) ||
2307 !(info->flags & ASYNC_INITIALIZED)) {
2308 #ifdef SERIAL_DO_RESTART
2309 if (info->flags & ASYNC_HUP_NOTIFY)
2310 retval = -EAGAIN;
2311 else
2312 retval = -ERESTARTSYS;
2313 #else
2314 retval = -EAGAIN;
2315 #endif
2316 break;
2319 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
2320 if (serial_in(info, UART_ESI_STAT2) & UART_MSR_DCD)
2321 do_clocal = 1;
2323 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2324 !(info->flags & ASYNC_CLOSING) &&
2325 (do_clocal))
2326 break;
2327 if (signal_pending(current)) {
2328 retval = -ERESTARTSYS;
2329 break;
2331 #ifdef SERIAL_DEBUG_OPEN
2332 printk("block_til_ready blocking: ttys%d, count = %d\n",
2333 info->line, info->count);
2334 #endif
2335 schedule();
2337 current->state = TASK_RUNNING;
2338 remove_wait_queue(&info->open_wait, &wait);
2339 if (!tty_hung_up_p(filp))
2340 info->count++;
2341 info->blocked_open--;
2342 #ifdef SERIAL_DEBUG_OPEN
2343 printk("block_til_ready after blocking: ttys%d, count = %d\n",
2344 info->line, info->count);
2345 #endif
2346 if (retval)
2347 return retval;
2348 info->flags |= ASYNC_NORMAL_ACTIVE;
2349 return 0;
2353 * This routine is called whenever a serial port is opened. It
2354 * enables interrupts for a serial port, linking in its async structure into
2355 * the IRQ chain. It also performs the serial-specific
2356 * initialization for the tty structure.
2358 static int esp_open(struct tty_struct *tty, struct file * filp)
2360 struct esp_struct *info;
2361 int retval, line;
2362 unsigned long page;
2364 line = MINOR(tty->device) - tty->driver.minor_start;
2365 if ((line < 0) || (line >= NR_PORTS))
2366 return -ENODEV;
2368 /* find the port in the chain */
2370 info = ports;
2372 while (info && (info->line != line))
2373 info = info->next_port;
2375 if (!info) {
2376 serial_paranoia_check(info, tty->device, "esp_open");
2377 return -ENODEV;
2380 #ifdef SERIAL_DEBUG_OPEN
2381 printk("esp_open %s%d, count = %d\n", tty->driver.name, info->line,
2382 info->count);
2383 #endif
2384 MOD_INC_USE_COUNT;
2385 info->count++;
2386 tty->driver_data = info;
2387 info->tty = tty;
2389 if (!tmp_buf) {
2390 page = get_free_page(GFP_KERNEL);
2391 if (!page)
2392 return -ENOMEM;
2393 if (tmp_buf)
2394 free_page(page);
2395 else
2396 tmp_buf = (unsigned char *) page;
2400 * Start up serial port
2402 retval = startup(info);
2403 if (retval)
2404 return retval;
2406 retval = block_til_ready(tty, filp, info);
2407 if (retval) {
2408 #ifdef SERIAL_DEBUG_OPEN
2409 printk("esp_open returning after block_til_ready with %d\n",
2410 retval);
2411 #endif
2412 return retval;
2415 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
2416 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2417 *tty->termios = info->normal_termios;
2418 else
2419 *tty->termios = info->callout_termios;
2420 change_speed(info);
2423 info->session = current->session;
2424 info->pgrp = current->pgrp;
2426 #ifdef SERIAL_DEBUG_OPEN
2427 printk("esp_open ttys%d successful...", info->line);
2428 #endif
2429 return 0;
2433 * ---------------------------------------------------------------------
2434 * espserial_init() and friends
2436 * espserial_init() is called at boot-time to initialize the serial driver.
2437 * ---------------------------------------------------------------------
2441 * This routine prints out the appropriate serial driver version
2442 * number, and identifies which options were configured into this
2443 * driver.
2446 static _INLINE_ void show_serial_version(void)
2448 printk(KERN_INFO "%s version %s (DMA %u)\n",
2449 serial_name, serial_version, dma);
2453 * This routine is called by espserial_init() to initialize a specific serial
2454 * port.
2456 static _INLINE_ int autoconfig(struct esp_struct * info, int *region_start)
2458 int port_detected = 0;
2459 unsigned long flags;
2461 save_flags(flags); cli();
2464 * Check for ESP card
2467 if (!check_region(info->port, 8) &&
2468 serial_in(info, UART_ESI_BASE) == 0xf3) {
2469 serial_out(info, UART_ESI_CMD1, 0x00);
2470 serial_out(info, UART_ESI_CMD1, 0x01);
2472 if ((serial_in(info, UART_ESI_STAT2) & 0x70) == 0x20) {
2473 port_detected = 1;
2475 if (!(info->irq)) {
2476 serial_out(info, UART_ESI_CMD1, 0x02);
2478 if (serial_in(info, UART_ESI_STAT1) & 0x01)
2479 info->irq = 3;
2480 else
2481 info->irq = 4;
2484 if (ports && (ports->port == (info->port - 8))) {
2485 release_region(*region_start,
2486 info->port - *region_start);
2487 } else
2488 *region_start = info->port;
2490 request_region(*region_start,
2491 info->port - *region_start + 8,
2492 "esp serial");
2494 /* put card in enhanced mode */
2495 /* this prevents access through */
2496 /* the "old" IO ports */
2497 esp_basic_init(info);
2499 /* clear out MCR */
2500 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2501 serial_out(info, UART_ESI_CMD2, UART_MCR);
2502 serial_out(info, UART_ESI_CMD2, 0x00);
2506 restore_flags(flags);
2507 return (port_detected);
2511 * The serial driver boot-time initialization code!
2513 int __init espserial_init(void)
2515 int i, offset;
2516 int region_start;
2517 struct esp_struct * info;
2518 struct esp_struct *last_primary = 0;
2519 int esp[] = {0x100,0x140,0x180,0x200,0x240,0x280,0x300,0x380};
2521 init_bh(ESP_BH, do_serial_bh);
2523 for (i = 0; i < NR_PRIMARY; i++) {
2524 if (irq[i] != 0) {
2525 if ((irq[i] < 2) || (irq[i] > 15) || (irq[i] == 6) ||
2526 (irq[i] == 8) || (irq[i] == 13))
2527 irq[i] = 0;
2528 else if (irq[i] == 2)
2529 irq[i] = 9;
2533 if ((dma != 1) && (dma != 3))
2534 dma = 0;
2536 if ((rx_trigger < 1) || (rx_trigger > 1023))
2537 rx_trigger = 768;
2539 if ((tx_trigger < 1) || (tx_trigger > 1023))
2540 tx_trigger = 768;
2542 if ((flow_off < 1) || (flow_off > 1023))
2543 flow_off = 1016;
2545 if ((flow_on < 1) || (flow_on > 1023))
2546 flow_on = 944;
2548 if ((rx_timeout < 0) || (rx_timeout > 255))
2549 rx_timeout = 128;
2551 if (flow_on >= flow_off)
2552 flow_on = flow_off - 1;
2554 show_serial_version();
2556 /* Initialize the tty_driver structure */
2558 memset(&esp_driver, 0, sizeof(struct tty_driver));
2559 esp_driver.magic = TTY_DRIVER_MAGIC;
2560 esp_driver.name = "ttyP";
2561 esp_driver.major = ESP_IN_MAJOR;
2562 esp_driver.minor_start = 0;
2563 esp_driver.num = NR_PORTS;
2564 esp_driver.type = TTY_DRIVER_TYPE_SERIAL;
2565 esp_driver.subtype = SERIAL_TYPE_NORMAL;
2566 esp_driver.init_termios = tty_std_termios;
2567 esp_driver.init_termios.c_cflag =
2568 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2569 esp_driver.flags = TTY_DRIVER_REAL_RAW;
2570 esp_driver.refcount = &serial_refcount;
2571 esp_driver.table = serial_table;
2572 esp_driver.termios = serial_termios;
2573 esp_driver.termios_locked = serial_termios_locked;
2575 esp_driver.open = esp_open;
2576 esp_driver.close = rs_close;
2577 esp_driver.write = rs_write;
2578 esp_driver.put_char = rs_put_char;
2579 esp_driver.flush_chars = rs_flush_chars;
2580 esp_driver.write_room = rs_write_room;
2581 esp_driver.chars_in_buffer = rs_chars_in_buffer;
2582 esp_driver.flush_buffer = rs_flush_buffer;
2583 esp_driver.ioctl = rs_ioctl;
2584 esp_driver.throttle = rs_throttle;
2585 esp_driver.unthrottle = rs_unthrottle;
2586 esp_driver.set_termios = rs_set_termios;
2587 esp_driver.stop = rs_stop;
2588 esp_driver.start = rs_start;
2589 esp_driver.hangup = esp_hangup;
2590 esp_driver.break_ctl = esp_break;
2591 esp_driver.wait_until_sent = rs_wait_until_sent;
2594 * The callout device is just like normal device except for
2595 * major number and the subtype code.
2597 esp_callout_driver = esp_driver;
2598 esp_callout_driver.name = "cup";
2599 esp_callout_driver.major = ESP_OUT_MAJOR;
2600 esp_callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2602 if (tty_register_driver(&esp_driver))
2604 printk(KERN_ERR "Couldn't register esp serial driver");
2605 return 1;
2608 if (tty_register_driver(&esp_callout_driver))
2610 printk(KERN_ERR "Couldn't register esp callout driver");
2611 tty_unregister_driver(&esp_driver);
2612 return 1;
2615 info = (struct esp_struct *)kmalloc(sizeof(struct esp_struct),
2616 GFP_KERNEL);
2618 if (!info)
2620 printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
2621 tty_unregister_driver(&esp_driver);
2622 tty_unregister_driver(&esp_callout_driver);
2623 return 1;
2626 memset((void *)info, 0, sizeof(struct esp_struct));
2627 /* rx_trigger, tx_trigger are needed by autoconfig */
2628 info->config.rx_trigger = rx_trigger;
2629 info->config.tx_trigger = tx_trigger;
2631 i = 0;
2632 offset = 0;
2634 do {
2635 info->port = esp[i] + offset;
2636 info->irq = irq[i];
2637 info->line = (i * 8) + (offset / 8);
2639 if (!autoconfig(info, &region_start)) {
2640 i++;
2641 offset = 0;
2642 continue;
2645 info->custom_divisor = (divisor[i] >> (offset / 2)) & 0xf;
2646 info->flags = STD_COM_FLAGS;
2647 if (info->custom_divisor)
2648 info->flags |= ASYNC_SPD_CUST;
2649 info->magic = ESP_MAGIC;
2650 info->close_delay = 5*HZ/10;
2651 info->closing_wait = 30*HZ;
2652 info->tqueue.routine = do_softint;
2653 info->tqueue.data = info;
2654 info->tqueue_hangup.routine = do_serial_hangup;
2655 info->tqueue_hangup.data = info;
2656 info->callout_termios = esp_callout_driver.init_termios;
2657 info->normal_termios = esp_driver.init_termios;
2658 info->config.rx_timeout = rx_timeout;
2659 info->config.flow_on = flow_on;
2660 info->config.flow_off = flow_off;
2661 info->config.pio_threshold = pio_threshold;
2662 info->next_port = ports;
2663 init_waitqueue_head(&info->open_wait);
2664 init_waitqueue_head(&info->close_wait);
2665 init_waitqueue_head(&info->delta_msr_wait);
2666 init_waitqueue_head(&info->break_wait);
2667 ports = info;
2668 printk(KERN_INFO "ttyP%d at 0x%04x (irq = %d) is an ESP ",
2669 info->line, info->port, info->irq);
2671 if (info->line % 8) {
2672 printk("secondary port\n");
2673 /* 8 port cards can't do DMA */
2674 info->stat_flags |= ESP_STAT_NEVER_DMA;
2676 if (last_primary)
2677 last_primary->stat_flags |= ESP_STAT_NEVER_DMA;
2678 } else {
2679 printk("primary port\n");
2680 last_primary = info;
2681 irq[i] = info->irq;
2684 if (!dma)
2685 info->stat_flags |= ESP_STAT_NEVER_DMA;
2687 info = (struct esp_struct *)kmalloc(sizeof(struct esp_struct),
2688 GFP_KERNEL);
2689 if (!info)
2691 printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
2693 /* allow use of the already detected ports */
2694 return 0;
2697 memset((void *)info, 0, sizeof(struct esp_struct));
2698 /* rx_trigger, tx_trigger are needed by autoconfig */
2699 info->config.rx_trigger = rx_trigger;
2700 info->config.tx_trigger = tx_trigger;
2702 if (offset == 56) {
2703 i++;
2704 offset = 0;
2705 } else {
2706 offset += 8;
2708 } while (i < NR_PRIMARY);
2710 /* free the last port memory allocation */
2711 kfree(info);
2713 return 0;
2716 #ifdef MODULE
2718 int init_module(void)
2720 return espserial_init();
2723 void cleanup_module(void)
2725 unsigned long flags;
2726 int e1, e2;
2727 unsigned int region_start, region_end;
2728 struct esp_struct *temp_async;
2729 struct esp_pio_buffer *pio_buf;
2731 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2732 save_flags(flags);
2733 cli();
2734 remove_bh(ESP_BH);
2735 if ((e1 = tty_unregister_driver(&esp_driver)))
2736 printk("SERIAL: failed to unregister serial driver (%d)\n",
2737 e1);
2738 if ((e2 = tty_unregister_driver(&esp_callout_driver)))
2739 printk("SERIAL: failed to unregister callout driver (%d)\n",
2740 e2);
2741 restore_flags(flags);
2743 while (ports) {
2744 if (ports->port) {
2745 region_start = region_end = ports->port;
2746 temp_async = ports;
2748 while (temp_async) {
2749 if ((region_start - temp_async->port) == 8) {
2750 region_start = temp_async->port;
2751 temp_async->port = 0;
2752 temp_async = ports;
2753 } else if ((temp_async->port - region_end)
2754 == 8) {
2755 region_end = temp_async->port;
2756 temp_async->port = 0;
2757 temp_async = ports;
2758 } else
2759 temp_async = temp_async->next_port;
2762 release_region(region_start,
2763 region_end - region_start + 8);
2766 temp_async = ports->next_port;
2767 kfree(ports);
2768 ports = temp_async;
2771 if (dma_buffer)
2772 free_pages((unsigned int)dma_buffer,
2773 get_order(DMA_BUFFER_SZ));
2775 if (tmp_buf)
2776 free_page((unsigned long)tmp_buf);
2778 while (free_pio_buf) {
2779 pio_buf = free_pio_buf->next;
2780 kfree(free_pio_buf);
2781 free_pio_buf = pio_buf;
2784 #endif /* MODULE */