- Kai Germaschewski: ymfpci cleanups and resource leak fixes
[davej-history.git] / drivers / char / esp.c
blob6e575c3dd4b015d33709c1d99e0d4ca4f187e2e6
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 MOD_INC_USE_COUNT;
644 if (schedule_task(&info->tqueue_hangup) == 0)
645 MOD_DEC_USE_COUNT;
651 * This is the serial driver's interrupt routine
653 static void rs_interrupt_single(int irq, void *dev_id, struct pt_regs * regs)
655 struct esp_struct * info;
656 unsigned err_status;
657 unsigned int scratch;
659 #ifdef SERIAL_DEBUG_INTR
660 printk("rs_interrupt_single(%d)...", irq);
661 #endif
662 info = (struct esp_struct *)dev_id;
663 err_status = 0;
664 scratch = serial_in(info, UART_ESI_SID);
666 cli();
668 if (!info->tty) {
669 sti();
670 return;
673 if (scratch & 0x04) { /* error */
674 serial_out(info, UART_ESI_CMD1, ESI_GET_ERR_STAT);
675 err_status = serial_in(info, UART_ESI_STAT1);
676 serial_in(info, UART_ESI_STAT2);
678 if (err_status & 0x01)
679 info->stat_flags |= ESP_STAT_RX_TIMEOUT;
681 if (err_status & 0x20) /* UART status */
682 check_modem_status(info);
684 if (err_status & 0x80) /* Start break */
685 wake_up_interruptible(&info->break_wait);
688 if ((scratch & 0x88) || /* DMA completed or timed out */
689 (err_status & 0x1c) /* receive error */) {
690 if (info->stat_flags & ESP_STAT_DMA_RX)
691 receive_chars_dma_done(info, err_status);
692 else if (info->stat_flags & ESP_STAT_DMA_TX)
693 transmit_chars_dma_done(info);
696 if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
697 ((scratch & 0x01) || (info->stat_flags & ESP_STAT_RX_TIMEOUT)) &&
698 (info->IER & UART_IER_RDI)) {
699 int num_bytes;
701 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
702 serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
703 num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
704 num_bytes |= serial_in(info, UART_ESI_STAT2);
706 if (num_bytes > (TTY_FLIPBUF_SIZE - info->tty->flip.count))
707 num_bytes = TTY_FLIPBUF_SIZE - info->tty->flip.count;
709 if (num_bytes) {
710 if (dma_bytes ||
711 (info->stat_flags & ESP_STAT_USE_PIO) ||
712 (num_bytes <= info->config.pio_threshold))
713 receive_chars_pio(info, num_bytes);
714 else
715 receive_chars_dma(info, num_bytes);
719 if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
720 (scratch & 0x02) && (info->IER & UART_IER_THRI)) {
721 if ((info->xmit_cnt <= 0) || info->tty->stopped) {
722 info->IER &= ~UART_IER_THRI;
723 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
724 serial_out(info, UART_ESI_CMD2, info->IER);
725 } else {
726 int num_bytes;
728 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
729 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
730 num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
731 num_bytes |= serial_in(info, UART_ESI_STAT2);
733 if (num_bytes > info->xmit_cnt)
734 num_bytes = info->xmit_cnt;
736 if (num_bytes) {
737 if (dma_bytes ||
738 (info->stat_flags & ESP_STAT_USE_PIO) ||
739 (num_bytes <= info->config.pio_threshold))
740 transmit_chars_pio(info, num_bytes);
741 else
742 transmit_chars_dma(info, num_bytes);
747 info->last_active = jiffies;
749 #ifdef SERIAL_DEBUG_INTR
750 printk("end.\n");
751 #endif
752 sti();
756 * -------------------------------------------------------------------
757 * Here ends the serial interrupt routines.
758 * -------------------------------------------------------------------
762 * This routine is used to handle the "bottom half" processing for the
763 * serial driver, known also the "software interrupt" processing.
764 * This processing is done at the kernel interrupt level, after the
765 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
766 * is where time-consuming activities which can not be done in the
767 * interrupt driver proper are done; the interrupt driver schedules
768 * them using rs_sched_event(), and they get done here.
770 static void do_serial_bh(void)
772 run_task_queue(&tq_esp);
775 static void do_softint(void *private_)
777 struct esp_struct *info = (struct esp_struct *) private_;
778 struct tty_struct *tty;
780 tty = info->tty;
781 if (!tty)
782 return;
784 if (test_and_clear_bit(ESP_EVENT_WRITE_WAKEUP, &info->event)) {
785 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
786 tty->ldisc.write_wakeup)
787 (tty->ldisc.write_wakeup)(tty);
788 wake_up_interruptible(&tty->write_wait);
793 * This routine is called from the scheduler tqueue when the interrupt
794 * routine has signalled that a hangup has occurred. The path of
795 * hangup processing is:
797 * serial interrupt routine -> (scheduler tqueue) ->
798 * do_serial_hangup() -> tty->hangup() -> esp_hangup()
801 static void do_serial_hangup(void *private_)
803 struct esp_struct *info = (struct esp_struct *) private_;
804 struct tty_struct *tty;
806 tty = info->tty;
807 if (tty)
808 tty_hangup(tty);
809 MOD_DEC_USE_COUNT;
813 * ---------------------------------------------------------------
814 * Low level utility subroutines for the serial driver: routines to
815 * figure out the appropriate timeout for an interrupt chain, routines
816 * to initialize and startup a serial port, and routines to shutdown a
817 * serial port. Useful stuff like that.
818 * ---------------------------------------------------------------
821 static _INLINE_ void esp_basic_init(struct esp_struct * info)
823 /* put ESPC in enhanced mode */
824 serial_out(info, UART_ESI_CMD1, ESI_SET_MODE);
826 if (info->stat_flags & ESP_STAT_NEVER_DMA)
827 serial_out(info, UART_ESI_CMD2, 0x01);
828 else
829 serial_out(info, UART_ESI_CMD2, 0x31);
831 /* disable interrupts for now */
832 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
833 serial_out(info, UART_ESI_CMD2, 0x00);
835 /* set interrupt and DMA channel */
836 serial_out(info, UART_ESI_CMD1, ESI_SET_IRQ);
838 if (info->stat_flags & ESP_STAT_NEVER_DMA)
839 serial_out(info, UART_ESI_CMD2, 0x01);
840 else
841 serial_out(info, UART_ESI_CMD2, (dma << 4) | 0x01);
843 serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
845 if (info->line % 8) /* secondary port */
846 serial_out(info, UART_ESI_CMD2, 0x0d); /* shared */
847 else if (info->irq == 9)
848 serial_out(info, UART_ESI_CMD2, 0x02);
849 else
850 serial_out(info, UART_ESI_CMD2, info->irq);
852 /* set error status mask (check this) */
853 serial_out(info, UART_ESI_CMD1, ESI_SET_ERR_MASK);
855 if (info->stat_flags & ESP_STAT_NEVER_DMA)
856 serial_out(info, UART_ESI_CMD2, 0xa1);
857 else
858 serial_out(info, UART_ESI_CMD2, 0xbd);
860 serial_out(info, UART_ESI_CMD2, 0x00);
862 /* set DMA timeout */
863 serial_out(info, UART_ESI_CMD1, ESI_SET_DMA_TMOUT);
864 serial_out(info, UART_ESI_CMD2, 0xff);
866 /* set FIFO trigger levels */
867 serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
868 serial_out(info, UART_ESI_CMD2, info->config.rx_trigger >> 8);
869 serial_out(info, UART_ESI_CMD2, info->config.rx_trigger);
870 serial_out(info, UART_ESI_CMD2, info->config.tx_trigger >> 8);
871 serial_out(info, UART_ESI_CMD2, info->config.tx_trigger);
873 /* Set clock scaling and wait states */
874 serial_out(info, UART_ESI_CMD1, ESI_SET_PRESCALAR);
875 serial_out(info, UART_ESI_CMD2, 0x04 | ESPC_SCALE);
877 /* set reinterrupt pacing */
878 serial_out(info, UART_ESI_CMD1, ESI_SET_REINTR);
879 serial_out(info, UART_ESI_CMD2, 0xff);
882 static int startup(struct esp_struct * info)
884 unsigned long flags;
885 int retval=0;
886 unsigned int num_chars;
888 save_flags(flags); cli();
890 if (info->flags & ASYNC_INITIALIZED) {
891 restore_flags(flags);
892 return retval;
895 if (!info->xmit_buf) {
896 info->xmit_buf = (unsigned char *)get_free_page(GFP_KERNEL);
897 if (!info->xmit_buf) {
898 restore_flags(flags);
899 return -ENOMEM;
903 #ifdef SERIAL_DEBUG_OPEN
904 printk("starting up ttys%d (irq %d)...", info->line, info->irq);
905 #endif
907 /* Flush the RX buffer. Using the ESI flush command may cause */
908 /* wild interrupts, so read all the data instead. */
910 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
911 serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
912 num_chars = serial_in(info, UART_ESI_STAT1) << 8;
913 num_chars |= serial_in(info, UART_ESI_STAT2);
915 while (num_chars > 1) {
916 inw(info->port + UART_ESI_RX);
917 num_chars -= 2;
920 if (num_chars)
921 serial_in(info, UART_ESI_RX);
923 /* set receive character timeout */
924 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
925 serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
927 /* clear all flags except the "never DMA" flag */
928 info->stat_flags &= ESP_STAT_NEVER_DMA;
930 if (info->stat_flags & ESP_STAT_NEVER_DMA)
931 info->stat_flags |= ESP_STAT_USE_PIO;
934 * Allocate the IRQ
937 retval = request_irq(info->irq, rs_interrupt_single, SA_SHIRQ,
938 "esp serial", info);
940 if (retval) {
941 if (capable(CAP_SYS_ADMIN)) {
942 if (info->tty)
943 set_bit(TTY_IO_ERROR,
944 &info->tty->flags);
945 retval = 0;
948 restore_flags(flags);
949 return retval;
952 if (!(info->stat_flags & ESP_STAT_USE_PIO) && !dma_buffer) {
953 dma_buffer = (char *)__get_dma_pages(
954 GFP_KERNEL, get_order(DMA_BUFFER_SZ));
956 /* use PIO mode if DMA buf/chan cannot be allocated */
957 if (!dma_buffer)
958 info->stat_flags |= ESP_STAT_USE_PIO;
959 else if (request_dma(dma, "esp serial")) {
960 free_pages((unsigned int)dma_buffer,
961 get_order(DMA_BUFFER_SZ));
962 dma_buffer = 0;
963 info->stat_flags |= ESP_STAT_USE_PIO;
968 info->MCR = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
969 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
970 serial_out(info, UART_ESI_CMD2, UART_MCR);
971 serial_out(info, UART_ESI_CMD2, info->MCR);
974 * Finally, enable interrupts
976 /* info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI; */
977 info->IER = UART_IER_RLSI | UART_IER_RDI | UART_IER_DMA_TMOUT |
978 UART_IER_DMA_TC;
979 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
980 serial_out(info, UART_ESI_CMD2, info->IER);
982 if (info->tty)
983 clear_bit(TTY_IO_ERROR, &info->tty->flags);
984 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
987 * Set up the tty->alt_speed kludge
989 if (info->tty) {
990 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
991 info->tty->alt_speed = 57600;
992 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
993 info->tty->alt_speed = 115200;
994 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
995 info->tty->alt_speed = 230400;
996 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
997 info->tty->alt_speed = 460800;
1001 * set the speed of the serial port
1003 change_speed(info);
1005 info->flags |= ASYNC_INITIALIZED;
1006 restore_flags(flags);
1007 return 0;
1011 * This routine will shutdown a serial port; interrupts are disabled, and
1012 * DTR is dropped if the hangup on close termio flag is on.
1014 static void shutdown(struct esp_struct * info)
1016 unsigned long flags, f;
1018 if (!(info->flags & ASYNC_INITIALIZED))
1019 return;
1021 #ifdef SERIAL_DEBUG_OPEN
1022 printk("Shutting down serial port %d (irq %d)....", info->line,
1023 info->irq);
1024 #endif
1026 save_flags(flags); cli(); /* Disable interrupts */
1029 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1030 * here so the queue might never be waken up
1032 wake_up_interruptible(&info->delta_msr_wait);
1033 wake_up_interruptible(&info->break_wait);
1035 /* stop a DMA transfer on the port being closed */
1037 if (info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) {
1038 f=claim_dma_lock();
1039 disable_dma(dma);
1040 clear_dma_ff(dma);
1041 release_dma_lock(f);
1043 dma_bytes = 0;
1047 * Free the IRQ
1049 free_irq(info->irq, info);
1051 if (dma_buffer) {
1052 struct esp_struct *current_port = ports;
1054 while (current_port) {
1055 if ((current_port != info) &&
1056 (current_port->flags & ASYNC_INITIALIZED))
1057 break;
1059 current_port = current_port->next_port;
1062 if (!current_port) {
1063 free_dma(dma);
1064 free_pages((unsigned int)dma_buffer,
1065 get_order(DMA_BUFFER_SZ));
1066 dma_buffer = 0;
1070 if (info->xmit_buf) {
1071 free_page((unsigned long) info->xmit_buf);
1072 info->xmit_buf = 0;
1075 info->IER = 0;
1076 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1077 serial_out(info, UART_ESI_CMD2, 0x00);
1079 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
1080 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1082 info->MCR &= ~UART_MCR_OUT2;
1083 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1084 serial_out(info, UART_ESI_CMD2, UART_MCR);
1085 serial_out(info, UART_ESI_CMD2, info->MCR);
1087 if (info->tty)
1088 set_bit(TTY_IO_ERROR, &info->tty->flags);
1090 info->flags &= ~ASYNC_INITIALIZED;
1091 restore_flags(flags);
1095 * This routine is called to set the UART divisor registers to match
1096 * the specified baud rate for a serial port.
1098 static void change_speed(struct esp_struct *info)
1100 unsigned short port;
1101 int quot = 0;
1102 unsigned cflag,cval;
1103 int baud, bits;
1104 unsigned char flow1 = 0, flow2 = 0;
1105 unsigned long flags;
1107 if (!info->tty || !info->tty->termios)
1108 return;
1109 cflag = info->tty->termios->c_cflag;
1110 port = info->port;
1112 /* byte size and parity */
1113 switch (cflag & CSIZE) {
1114 case CS5: cval = 0x00; bits = 7; break;
1115 case CS6: cval = 0x01; bits = 8; break;
1116 case CS7: cval = 0x02; bits = 9; break;
1117 case CS8: cval = 0x03; bits = 10; break;
1118 default: cval = 0x00; bits = 7; break;
1120 if (cflag & CSTOPB) {
1121 cval |= 0x04;
1122 bits++;
1124 if (cflag & PARENB) {
1125 cval |= UART_LCR_PARITY;
1126 bits++;
1128 if (!(cflag & PARODD))
1129 cval |= UART_LCR_EPAR;
1130 #ifdef CMSPAR
1131 if (cflag & CMSPAR)
1132 cval |= UART_LCR_SPAR;
1133 #endif
1135 baud = tty_get_baud_rate(info->tty);
1136 if (baud == 38400 &&
1137 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1138 quot = info->custom_divisor;
1139 else {
1140 if (baud == 134)
1141 /* Special case since 134 is really 134.5 */
1142 quot = (2*BASE_BAUD / 269);
1143 else if (baud)
1144 quot = BASE_BAUD / baud;
1146 /* If the quotient is ever zero, default to 9600 bps */
1147 if (!quot)
1148 quot = BASE_BAUD / 9600;
1150 info->timeout = ((1024 * HZ * bits * quot) / BASE_BAUD) + (HZ / 50);
1152 /* CTS flow control flag and modem status interrupts */
1153 /* info->IER &= ~UART_IER_MSI; */
1154 if (cflag & CRTSCTS) {
1155 info->flags |= ASYNC_CTS_FLOW;
1156 /* info->IER |= UART_IER_MSI; */
1157 flow1 = 0x04;
1158 flow2 = 0x10;
1159 } else
1160 info->flags &= ~ASYNC_CTS_FLOW;
1161 if (cflag & CLOCAL)
1162 info->flags &= ~ASYNC_CHECK_CD;
1163 else {
1164 info->flags |= ASYNC_CHECK_CD;
1165 /* info->IER |= UART_IER_MSI; */
1169 * Set up parity check flag
1171 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1173 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1174 if (I_INPCK(info->tty))
1175 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1176 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1177 info->read_status_mask |= UART_LSR_BI;
1179 info->ignore_status_mask = 0;
1180 #if 0
1181 /* This should be safe, but for some broken bits of hardware... */
1182 if (I_IGNPAR(info->tty)) {
1183 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1184 info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
1186 #endif
1187 if (I_IGNBRK(info->tty)) {
1188 info->ignore_status_mask |= UART_LSR_BI;
1189 info->read_status_mask |= UART_LSR_BI;
1191 * If we're ignore parity and break indicators, ignore
1192 * overruns too. (For real raw support).
1194 if (I_IGNPAR(info->tty)) {
1195 info->ignore_status_mask |= UART_LSR_OE | \
1196 UART_LSR_PE | UART_LSR_FE;
1197 info->read_status_mask |= UART_LSR_OE | \
1198 UART_LSR_PE | UART_LSR_FE;
1202 if (I_IXOFF(info->tty))
1203 flow1 |= 0x81;
1205 save_flags(flags); cli();
1206 /* set baud */
1207 serial_out(info, UART_ESI_CMD1, ESI_SET_BAUD);
1208 serial_out(info, UART_ESI_CMD2, quot >> 8);
1209 serial_out(info, UART_ESI_CMD2, quot & 0xff);
1211 /* set data bits, parity, etc. */
1212 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1213 serial_out(info, UART_ESI_CMD2, UART_LCR);
1214 serial_out(info, UART_ESI_CMD2, cval);
1216 /* Enable flow control */
1217 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CNTL);
1218 serial_out(info, UART_ESI_CMD2, flow1);
1219 serial_out(info, UART_ESI_CMD2, flow2);
1221 /* set flow control characters (XON/XOFF only) */
1222 if (I_IXOFF(info->tty)) {
1223 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CHARS);
1224 serial_out(info, UART_ESI_CMD2, START_CHAR(info->tty));
1225 serial_out(info, UART_ESI_CMD2, STOP_CHAR(info->tty));
1226 serial_out(info, UART_ESI_CMD2, 0x10);
1227 serial_out(info, UART_ESI_CMD2, 0x21);
1228 switch (cflag & CSIZE) {
1229 case CS5:
1230 serial_out(info, UART_ESI_CMD2, 0x1f);
1231 break;
1232 case CS6:
1233 serial_out(info, UART_ESI_CMD2, 0x3f);
1234 break;
1235 case CS7:
1236 case CS8:
1237 serial_out(info, UART_ESI_CMD2, 0x7f);
1238 break;
1239 default:
1240 serial_out(info, UART_ESI_CMD2, 0xff);
1241 break;
1245 /* Set high/low water */
1246 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1247 serial_out(info, UART_ESI_CMD2, info->config.flow_off >> 8);
1248 serial_out(info, UART_ESI_CMD2, info->config.flow_off);
1249 serial_out(info, UART_ESI_CMD2, info->config.flow_on >> 8);
1250 serial_out(info, UART_ESI_CMD2, info->config.flow_on);
1252 restore_flags(flags);
1255 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
1257 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1258 unsigned long flags;
1260 if (serial_paranoia_check(info, tty->device, "rs_put_char"))
1261 return;
1263 if (!tty || !info->xmit_buf)
1264 return;
1266 save_flags(flags); cli();
1267 if (info->xmit_cnt >= ESP_XMIT_SIZE - 1) {
1268 restore_flags(flags);
1269 return;
1272 info->xmit_buf[info->xmit_head++] = ch;
1273 info->xmit_head &= ESP_XMIT_SIZE-1;
1274 info->xmit_cnt++;
1275 restore_flags(flags);
1278 static void rs_flush_chars(struct tty_struct *tty)
1280 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1281 unsigned long flags;
1283 if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
1284 return;
1286 if (info->xmit_cnt <= 0 || tty->stopped || !info->xmit_buf)
1287 return;
1289 save_flags(flags); cli();
1290 if (!(info->IER & UART_IER_THRI)) {
1291 info->IER |= UART_IER_THRI;
1292 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1293 serial_out(info, UART_ESI_CMD2, info->IER);
1295 restore_flags(flags);
1298 static int rs_write(struct tty_struct * tty, int from_user,
1299 const unsigned char *buf, int count)
1301 int c, t, ret = 0;
1302 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1303 unsigned long flags;
1305 if (serial_paranoia_check(info, tty->device, "rs_write"))
1306 return 0;
1308 if (!tty || !info->xmit_buf || !tmp_buf)
1309 return 0;
1311 if (from_user)
1312 down(&tmp_buf_sem);
1314 while (1) {
1315 /* Thanks to R. Wolff for suggesting how to do this with */
1316 /* interrupts enabled */
1318 c = count;
1319 t = ESP_XMIT_SIZE - info->xmit_cnt - 1;
1321 if (t < c)
1322 c = t;
1324 t = ESP_XMIT_SIZE - info->xmit_head;
1326 if (t < c)
1327 c = t;
1329 if (c <= 0)
1330 break;
1332 if (from_user) {
1333 c -= copy_from_user(tmp_buf, buf, c);
1335 if (!c) {
1336 if (!ret)
1337 ret = -EFAULT;
1338 break;
1341 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1342 } else
1343 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1345 info->xmit_head = (info->xmit_head + c) & (ESP_XMIT_SIZE-1);
1346 info->xmit_cnt += c;
1347 buf += c;
1348 count -= c;
1349 ret += c;
1352 if (from_user)
1353 up(&tmp_buf_sem);
1355 save_flags(flags); cli();
1357 if (info->xmit_cnt && !tty->stopped && !(info->IER & UART_IER_THRI)) {
1358 info->IER |= UART_IER_THRI;
1359 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1360 serial_out(info, UART_ESI_CMD2, info->IER);
1363 restore_flags(flags);
1364 return ret;
1367 static int rs_write_room(struct tty_struct *tty)
1369 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1370 int ret;
1372 if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1373 return 0;
1374 ret = ESP_XMIT_SIZE - info->xmit_cnt - 1;
1375 if (ret < 0)
1376 ret = 0;
1377 return ret;
1380 static int rs_chars_in_buffer(struct tty_struct *tty)
1382 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1384 if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1385 return 0;
1386 return info->xmit_cnt;
1389 static void rs_flush_buffer(struct tty_struct *tty)
1391 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1393 if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1394 return;
1395 cli();
1396 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1397 sti();
1398 wake_up_interruptible(&tty->write_wait);
1399 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1400 tty->ldisc.write_wakeup)
1401 (tty->ldisc.write_wakeup)(tty);
1405 * ------------------------------------------------------------
1406 * rs_throttle()
1408 * This routine is called by the upper-layer tty layer to signal that
1409 * incoming characters should be throttled.
1410 * ------------------------------------------------------------
1412 static void rs_throttle(struct tty_struct * tty)
1414 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1415 #ifdef SERIAL_DEBUG_THROTTLE
1416 char buf[64];
1418 printk("throttle %s: %d....\n", tty_name(tty, buf),
1419 tty->ldisc.chars_in_buffer(tty));
1420 #endif
1422 if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1423 return;
1425 cli();
1426 info->IER &= ~UART_IER_RDI;
1427 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1428 serial_out(info, UART_ESI_CMD2, info->IER);
1429 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1430 serial_out(info, UART_ESI_CMD2, 0x00);
1431 sti();
1434 static void rs_unthrottle(struct tty_struct * tty)
1436 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1437 #ifdef SERIAL_DEBUG_THROTTLE
1438 char buf[64];
1440 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1441 tty->ldisc.chars_in_buffer(tty));
1442 #endif
1444 if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1445 return;
1447 cli();
1448 info->IER |= UART_IER_RDI;
1449 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1450 serial_out(info, UART_ESI_CMD2, info->IER);
1451 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1452 serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
1453 sti();
1457 * ------------------------------------------------------------
1458 * rs_ioctl() and friends
1459 * ------------------------------------------------------------
1462 static int get_serial_info(struct esp_struct * info,
1463 struct serial_struct * retinfo)
1465 struct serial_struct tmp;
1467 if (!retinfo)
1468 return -EFAULT;
1469 memset(&tmp, 0, sizeof(tmp));
1470 tmp.type = PORT_16550A;
1471 tmp.line = info->line;
1472 tmp.port = info->port;
1473 tmp.irq = info->irq;
1474 tmp.flags = info->flags;
1475 tmp.xmit_fifo_size = 1024;
1476 tmp.baud_base = BASE_BAUD;
1477 tmp.close_delay = info->close_delay;
1478 tmp.closing_wait = info->closing_wait;
1479 tmp.custom_divisor = info->custom_divisor;
1480 tmp.hub6 = 0;
1481 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1482 return -EFAULT;
1483 return 0;
1486 static int get_esp_config(struct esp_struct * info,
1487 struct hayes_esp_config * retinfo)
1489 struct hayes_esp_config tmp;
1491 if (!retinfo)
1492 return -EFAULT;
1494 memset(&tmp, 0, sizeof(tmp));
1495 tmp.rx_timeout = info->config.rx_timeout;
1496 tmp.rx_trigger = info->config.rx_trigger;
1497 tmp.tx_trigger = info->config.tx_trigger;
1498 tmp.flow_off = info->config.flow_off;
1499 tmp.flow_on = info->config.flow_on;
1500 tmp.pio_threshold = info->config.pio_threshold;
1501 tmp.dma_channel = (info->stat_flags & ESP_STAT_NEVER_DMA ? 0 : dma);
1503 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1504 return -EFAULT;
1506 return 0;
1509 static int set_serial_info(struct esp_struct * info,
1510 struct serial_struct * new_info)
1512 struct serial_struct new_serial;
1513 struct esp_struct old_info;
1514 unsigned int change_irq;
1515 int retval = 0;
1516 struct esp_struct *current_async;
1518 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1519 return -EFAULT;
1520 old_info = *info;
1522 if ((new_serial.type != PORT_16550A) ||
1523 (new_serial.hub6) ||
1524 (info->port != new_serial.port) ||
1525 (new_serial.baud_base != BASE_BAUD) ||
1526 (new_serial.irq > 15) ||
1527 (new_serial.irq < 2) ||
1528 (new_serial.irq == 6) ||
1529 (new_serial.irq == 8) ||
1530 (new_serial.irq == 13))
1531 return -EINVAL;
1533 change_irq = new_serial.irq != info->irq;
1535 if (change_irq && (info->line % 8))
1536 return -EINVAL;
1538 if (!capable(CAP_SYS_ADMIN)) {
1539 if (change_irq ||
1540 (new_serial.close_delay != info->close_delay) ||
1541 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1542 (info->flags & ~ASYNC_USR_MASK)))
1543 return -EPERM;
1544 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1545 (new_serial.flags & ASYNC_USR_MASK));
1546 info->custom_divisor = new_serial.custom_divisor;
1547 } else {
1548 if (new_serial.irq == 2)
1549 new_serial.irq = 9;
1551 if (change_irq) {
1552 current_async = ports;
1554 while (current_async) {
1555 if ((current_async->line >= info->line) &&
1556 (current_async->line < (info->line + 8))) {
1557 if (current_async == info) {
1558 if (current_async->count > 1)
1559 return -EBUSY;
1560 } else if (current_async->count)
1561 return -EBUSY;
1564 current_async = current_async->next_port;
1569 * OK, past this point, all the error checking has been done.
1570 * At this point, we start making changes.....
1573 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1574 (new_serial.flags & ASYNC_FLAGS));
1575 info->custom_divisor = new_serial.custom_divisor;
1576 info->close_delay = new_serial.close_delay * HZ/100;
1577 info->closing_wait = new_serial.closing_wait * HZ/100;
1579 if (change_irq) {
1581 * We need to shutdown the serial port at the old
1582 * port/irq combination.
1584 shutdown(info);
1586 current_async = ports;
1588 while (current_async) {
1589 if ((current_async->line >= info->line) &&
1590 (current_async->line < (info->line + 8)))
1591 current_async->irq = new_serial.irq;
1593 current_async = current_async->next_port;
1596 serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
1597 if (info->irq == 9)
1598 serial_out(info, UART_ESI_CMD2, 0x02);
1599 else
1600 serial_out(info, UART_ESI_CMD2, info->irq);
1604 if (info->flags & ASYNC_INITIALIZED) {
1605 if (((old_info.flags & ASYNC_SPD_MASK) !=
1606 (info->flags & ASYNC_SPD_MASK)) ||
1607 (old_info.custom_divisor != info->custom_divisor)) {
1608 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1609 info->tty->alt_speed = 57600;
1610 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1611 info->tty->alt_speed = 115200;
1612 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1613 info->tty->alt_speed = 230400;
1614 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1615 info->tty->alt_speed = 460800;
1616 change_speed(info);
1618 } else
1619 retval = startup(info);
1621 return retval;
1624 static int set_esp_config(struct esp_struct * info,
1625 struct hayes_esp_config * new_info)
1627 struct hayes_esp_config new_config;
1628 unsigned int change_dma;
1629 int retval = 0;
1630 struct esp_struct *current_async;
1632 /* Perhaps a non-sysadmin user should be able to do some of these */
1633 /* operations. I haven't decided yet. */
1635 if (!capable(CAP_SYS_ADMIN))
1636 return -EPERM;
1638 if (copy_from_user(&new_config, new_info, sizeof(new_config)))
1639 return -EFAULT;
1641 if ((new_config.flow_on >= new_config.flow_off) ||
1642 (new_config.rx_trigger < 1) ||
1643 (new_config.tx_trigger < 1) ||
1644 (new_config.flow_off < 1) ||
1645 (new_config.flow_on < 1) ||
1646 (new_config.rx_trigger > 1023) ||
1647 (new_config.tx_trigger > 1023) ||
1648 (new_config.flow_off > 1023) ||
1649 (new_config.flow_on > 1023) ||
1650 (new_config.pio_threshold < 0) ||
1651 (new_config.pio_threshold > 1024))
1652 return -EINVAL;
1654 if ((new_config.dma_channel != 1) && (new_config.dma_channel != 3))
1655 new_config.dma_channel = 0;
1657 if (info->stat_flags & ESP_STAT_NEVER_DMA)
1658 change_dma = new_config.dma_channel;
1659 else
1660 change_dma = (new_config.dma_channel != dma);
1662 if (change_dma) {
1663 if (new_config.dma_channel) {
1664 /* PIO mode to DMA mode transition OR */
1665 /* change current DMA channel */
1667 current_async = ports;
1669 while (current_async) {
1670 if (current_async == info) {
1671 if (current_async->count > 1)
1672 return -EBUSY;
1673 } else if (current_async->count)
1674 return -EBUSY;
1676 current_async =
1677 current_async->next_port;
1680 shutdown(info);
1681 dma = new_config.dma_channel;
1682 info->stat_flags &= ~ESP_STAT_NEVER_DMA;
1684 /* all ports must use the same DMA channel */
1686 current_async = ports;
1688 while (current_async) {
1689 esp_basic_init(current_async);
1690 current_async = current_async->next_port;
1692 } else {
1693 /* DMA mode to PIO mode only */
1695 if (info->count > 1)
1696 return -EBUSY;
1698 shutdown(info);
1699 info->stat_flags |= ESP_STAT_NEVER_DMA;
1700 esp_basic_init(info);
1704 info->config.pio_threshold = new_config.pio_threshold;
1706 if ((new_config.flow_off != info->config.flow_off) ||
1707 (new_config.flow_on != info->config.flow_on)) {
1708 unsigned long flags;
1710 info->config.flow_off = new_config.flow_off;
1711 info->config.flow_on = new_config.flow_on;
1712 save_flags(flags); cli();
1713 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1714 serial_out(info, UART_ESI_CMD2, new_config.flow_off >> 8);
1715 serial_out(info, UART_ESI_CMD2, new_config.flow_off);
1716 serial_out(info, UART_ESI_CMD2, new_config.flow_on >> 8);
1717 serial_out(info, UART_ESI_CMD2, new_config.flow_on);
1718 restore_flags(flags);
1721 if ((new_config.rx_trigger != info->config.rx_trigger) ||
1722 (new_config.tx_trigger != info->config.tx_trigger)) {
1723 unsigned long flags;
1725 info->config.rx_trigger = new_config.rx_trigger;
1726 info->config.tx_trigger = new_config.tx_trigger;
1727 save_flags(flags); cli();
1728 serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
1729 serial_out(info, UART_ESI_CMD2,
1730 new_config.rx_trigger >> 8);
1731 serial_out(info, UART_ESI_CMD2, new_config.rx_trigger);
1732 serial_out(info, UART_ESI_CMD2,
1733 new_config.tx_trigger >> 8);
1734 serial_out(info, UART_ESI_CMD2, new_config.tx_trigger);
1735 restore_flags(flags);
1738 if (new_config.rx_timeout != info->config.rx_timeout) {
1739 unsigned long flags;
1741 info->config.rx_timeout = new_config.rx_timeout;
1742 save_flags(flags); cli();
1744 if (info->IER & UART_IER_RDI) {
1745 serial_out(info, UART_ESI_CMD1,
1746 ESI_SET_RX_TIMEOUT);
1747 serial_out(info, UART_ESI_CMD2,
1748 new_config.rx_timeout);
1751 restore_flags(flags);
1754 if (!(info->flags & ASYNC_INITIALIZED))
1755 retval = startup(info);
1757 return retval;
1761 * get_lsr_info - get line status register info
1763 * Purpose: Let user call ioctl() to get info when the UART physically
1764 * is emptied. On bus types like RS485, the transmitter must
1765 * release the bus after transmitting. This must be done when
1766 * the transmit shift register is empty, not be done when the
1767 * transmit holding register is empty. This functionality
1768 * allows an RS485 driver to be written in user space.
1770 static int get_lsr_info(struct esp_struct * info, unsigned int *value)
1772 unsigned char status;
1773 unsigned int result;
1775 cli();
1776 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1777 status = serial_in(info, UART_ESI_STAT1);
1778 sti();
1779 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1780 return put_user(result,value);
1784 static int get_modem_info(struct esp_struct * info, unsigned int *value)
1786 unsigned char control, status;
1787 unsigned int result;
1789 control = info->MCR;
1790 cli();
1791 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1792 status = serial_in(info, UART_ESI_STAT2);
1793 sti();
1794 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1795 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1796 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1797 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1798 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1799 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1800 return put_user(result,value);
1803 static int set_modem_info(struct esp_struct * info, unsigned int cmd,
1804 unsigned int *value)
1806 int error;
1807 unsigned int arg;
1809 error = get_user(arg, value);
1810 if (error)
1811 return error;
1813 switch (cmd) {
1814 case TIOCMBIS:
1815 if (arg & TIOCM_RTS)
1816 info->MCR |= UART_MCR_RTS;
1817 if (arg & TIOCM_DTR)
1818 info->MCR |= UART_MCR_DTR;
1819 break;
1820 case TIOCMBIC:
1821 if (arg & TIOCM_RTS)
1822 info->MCR &= ~UART_MCR_RTS;
1823 if (arg & TIOCM_DTR)
1824 info->MCR &= ~UART_MCR_DTR;
1825 break;
1826 case TIOCMSET:
1827 info->MCR = ((info->MCR & ~(UART_MCR_RTS | UART_MCR_DTR))
1828 | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1829 | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1830 break;
1831 default:
1832 return -EINVAL;
1834 cli();
1835 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1836 serial_out(info, UART_ESI_CMD2, UART_MCR);
1837 serial_out(info, UART_ESI_CMD2, info->MCR);
1838 sti();
1839 return 0;
1843 * rs_break() --- routine which turns the break handling on or off
1845 static void esp_break(struct tty_struct *tty, int break_state)
1847 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1848 unsigned long flags;
1850 if (serial_paranoia_check(info, tty->device, "esp_break"))
1851 return;
1853 save_flags(flags); cli();
1854 if (break_state == -1) {
1855 serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1856 serial_out(info, UART_ESI_CMD2, 0x01);
1858 interruptible_sleep_on(&info->break_wait);
1859 } else {
1860 serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1861 serial_out(info, UART_ESI_CMD2, 0x00);
1863 restore_flags(flags);
1866 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1867 unsigned int cmd, unsigned long arg)
1869 int error;
1870 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1871 struct async_icount cprev, cnow; /* kernel counter temps */
1872 struct serial_icounter_struct *p_cuser; /* user space */
1874 if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1875 return -ENODEV;
1877 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1878 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1879 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT) &&
1880 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT) &&
1881 (cmd != TIOCGHAYESESP) && (cmd != TIOCSHAYESESP)) {
1882 if (tty->flags & (1 << TTY_IO_ERROR))
1883 return -EIO;
1886 switch (cmd) {
1887 case TIOCMGET:
1888 return get_modem_info(info, (unsigned int *) arg);
1889 case TIOCMBIS:
1890 case TIOCMBIC:
1891 case TIOCMSET:
1892 return set_modem_info(info, cmd, (unsigned int *) arg);
1893 case TIOCGSERIAL:
1894 return get_serial_info(info,
1895 (struct serial_struct *) arg);
1896 case TIOCSSERIAL:
1897 return set_serial_info(info,
1898 (struct serial_struct *) arg);
1899 case TIOCSERCONFIG:
1900 /* do not reconfigure after initial configuration */
1901 return 0;
1903 case TIOCSERGWILD:
1904 return put_user(0L, (unsigned long *) arg);
1906 case TIOCSERGETLSR: /* Get line status register */
1907 return get_lsr_info(info, (unsigned int *) arg);
1909 case TIOCSERSWILD:
1910 if (!suser())
1911 return -EPERM;
1912 return 0;
1915 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1916 * - mask passed in arg for lines of interest
1917 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1918 * Caller should use TIOCGICOUNT to see which one it was
1920 case TIOCMIWAIT:
1921 cli();
1922 cprev = info->icount; /* note the counters on entry */
1923 sti();
1924 while (1) {
1925 interruptible_sleep_on(&info->delta_msr_wait);
1926 /* see if a signal did it */
1927 if (signal_pending(current))
1928 return -ERESTARTSYS;
1929 cli();
1930 cnow = info->icount; /* atomic copy */
1931 sti();
1932 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1933 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1934 return -EIO; /* no change => error */
1935 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1936 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1937 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1938 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1939 return 0;
1941 cprev = cnow;
1943 /* NOTREACHED */
1946 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1947 * Return: write counters to the user passed counter struct
1948 * NB: both 1->0 and 0->1 transitions are counted except for
1949 * RI where only 0->1 is counted.
1951 case TIOCGICOUNT:
1952 cli();
1953 cnow = info->icount;
1954 sti();
1955 p_cuser = (struct serial_icounter_struct *) arg;
1956 if ((error = put_user(cnow.cts, &p_cuser->cts)))
1957 return error;
1958 if ((error = put_user(cnow.dsr, &p_cuser->dsr)))
1959 return error;
1960 if ((error = put_user(cnow.rng, &p_cuser->rng)))
1961 return error;
1962 if ((error = put_user(cnow.dcd, &p_cuser->dcd)))
1963 return error;
1965 return 0;
1966 case TIOCGHAYESESP:
1967 return (get_esp_config(info,
1968 (struct hayes_esp_config *)arg));
1969 case TIOCSHAYESESP:
1970 return (set_esp_config(info,
1971 (struct hayes_esp_config *)arg));
1973 default:
1974 return -ENOIOCTLCMD;
1976 return 0;
1979 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1981 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1983 if ( (tty->termios->c_cflag == old_termios->c_cflag)
1984 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
1985 == RELEVANT_IFLAG(old_termios->c_iflag)))
1986 return;
1988 change_speed(info);
1990 /* Handle transition to B0 status */
1991 if ((old_termios->c_cflag & CBAUD) &&
1992 !(tty->termios->c_cflag & CBAUD)) {
1993 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1994 cli();
1995 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1996 serial_out(info, UART_ESI_CMD2, UART_MCR);
1997 serial_out(info, UART_ESI_CMD2, info->MCR);
1998 sti();
2001 /* Handle transition away from B0 status */
2002 if (!(old_termios->c_cflag & CBAUD) &&
2003 (tty->termios->c_cflag & CBAUD)) {
2004 info->MCR |= (UART_MCR_DTR | UART_MCR_RTS);
2005 cli();
2006 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2007 serial_out(info, UART_ESI_CMD2, UART_MCR);
2008 serial_out(info, UART_ESI_CMD2, info->MCR);
2009 sti();
2012 /* Handle turning of CRTSCTS */
2013 if ((old_termios->c_cflag & CRTSCTS) &&
2014 !(tty->termios->c_cflag & CRTSCTS)) {
2015 rs_start(tty);
2018 #if 0
2020 * No need to wake up processes in open wait, since they
2021 * sample the CLOCAL flag once, and don't recheck it.
2022 * XXX It's not clear whether the current behavior is correct
2023 * or not. Hence, this may change.....
2025 if (!(old_termios->c_cflag & CLOCAL) &&
2026 (tty->termios->c_cflag & CLOCAL))
2027 wake_up_interruptible(&info->open_wait);
2028 #endif
2032 * ------------------------------------------------------------
2033 * rs_close()
2035 * This routine is called when the serial port gets closed. First, we
2036 * wait for the last remaining data to be sent. Then, we unlink its
2037 * async structure from the interrupt chain if necessary, and we free
2038 * that IRQ if nothing is left in the chain.
2039 * ------------------------------------------------------------
2041 static void rs_close(struct tty_struct *tty, struct file * filp)
2043 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
2044 unsigned long flags;
2046 if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
2047 return;
2049 save_flags(flags); cli();
2051 if (tty_hung_up_p(filp)) {
2052 DBG_CNT("before DEC-hung");
2053 MOD_DEC_USE_COUNT;
2054 restore_flags(flags);
2055 return;
2058 #ifdef SERIAL_DEBUG_OPEN
2059 printk("rs_close ttys%d, count = %d\n", info->line, info->count);
2060 #endif
2061 if ((tty->count == 1) && (info->count != 1)) {
2063 * Uh, oh. tty->count is 1, which means that the tty
2064 * structure will be freed. Info->count should always
2065 * be one in these conditions. If it's greater than
2066 * one, we've got real problems, since it means the
2067 * serial port won't be shutdown.
2069 printk("rs_close: bad serial port count; tty->count is 1, "
2070 "info->count is %d\n", info->count);
2071 info->count = 1;
2073 if (--info->count < 0) {
2074 printk("rs_close: bad serial port count for ttys%d: %d\n",
2075 info->line, info->count);
2076 info->count = 0;
2078 if (info->count) {
2079 DBG_CNT("before DEC-2");
2080 MOD_DEC_USE_COUNT;
2081 restore_flags(flags);
2082 return;
2084 info->flags |= ASYNC_CLOSING;
2086 * Save the termios structure, since this port may have
2087 * separate termios for callout and dialin.
2089 if (info->flags & ASYNC_NORMAL_ACTIVE)
2090 info->normal_termios = *tty->termios;
2091 if (info->flags & ASYNC_CALLOUT_ACTIVE)
2092 info->callout_termios = *tty->termios;
2094 * Now we wait for the transmit buffer to clear; and we notify
2095 * the line discipline to only process XON/XOFF characters.
2097 tty->closing = 1;
2098 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
2099 tty_wait_until_sent(tty, info->closing_wait);
2101 * At this point we stop accepting input. To do this, we
2102 * disable the receive line status interrupts, and tell the
2103 * interrupt driver to stop checking the data ready bit in the
2104 * line status register.
2106 /* info->IER &= ~UART_IER_RLSI; */
2107 info->IER &= ~UART_IER_RDI;
2108 info->read_status_mask &= ~UART_LSR_DR;
2109 if (info->flags & ASYNC_INITIALIZED) {
2110 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
2111 serial_out(info, UART_ESI_CMD2, info->IER);
2113 /* disable receive timeout */
2114 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
2115 serial_out(info, UART_ESI_CMD2, 0x00);
2118 * Before we drop DTR, make sure the UART transmitter
2119 * has completely drained; this is especially
2120 * important if there is a transmit FIFO!
2122 rs_wait_until_sent(tty, info->timeout);
2124 shutdown(info);
2125 if (tty->driver.flush_buffer)
2126 tty->driver.flush_buffer(tty);
2127 if (tty->ldisc.flush_buffer)
2128 tty->ldisc.flush_buffer(tty);
2129 tty->closing = 0;
2130 info->event = 0;
2131 info->tty = 0;
2133 if (info->blocked_open) {
2134 if (info->close_delay) {
2135 current->state = TASK_INTERRUPTIBLE;
2136 schedule_timeout(info->close_delay);
2138 wake_up_interruptible(&info->open_wait);
2140 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
2141 ASYNC_CLOSING);
2142 wake_up_interruptible(&info->close_wait);
2143 MOD_DEC_USE_COUNT;
2144 restore_flags(flags);
2147 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
2149 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
2150 unsigned long orig_jiffies, char_time;
2151 unsigned long flags;
2153 if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
2154 return;
2156 orig_jiffies = jiffies;
2157 char_time = ((info->timeout - HZ / 50) / 1024) / 5;
2159 if (!char_time)
2160 char_time = 1;
2162 save_flags(flags); cli();
2163 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2164 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2166 while ((serial_in(info, UART_ESI_STAT1) != 0x03) ||
2167 (serial_in(info, UART_ESI_STAT2) != 0xff)) {
2168 current->state = TASK_INTERRUPTIBLE;
2169 schedule_timeout(char_time);
2171 if (signal_pending(current))
2172 break;
2174 if (timeout && ((orig_jiffies + timeout) < jiffies))
2175 break;
2177 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2178 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2181 restore_flags(flags);
2182 current->state = TASK_RUNNING;
2186 * esp_hangup() --- called by tty_hangup() when a hangup is signaled.
2188 static void esp_hangup(struct tty_struct *tty)
2190 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
2192 if (serial_paranoia_check(info, tty->device, "esp_hangup"))
2193 return;
2195 rs_flush_buffer(tty);
2196 shutdown(info);
2197 info->event = 0;
2198 info->count = 0;
2199 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
2200 info->tty = 0;
2201 wake_up_interruptible(&info->open_wait);
2205 * ------------------------------------------------------------
2206 * esp_open() and friends
2207 * ------------------------------------------------------------
2209 static int block_til_ready(struct tty_struct *tty, struct file * filp,
2210 struct esp_struct *info)
2212 DECLARE_WAITQUEUE(wait, current);
2213 int retval;
2214 int do_clocal = 0;
2217 * If the device is in the middle of being closed, then block
2218 * until it's done, and then try again.
2220 if (tty_hung_up_p(filp) ||
2221 (info->flags & ASYNC_CLOSING)) {
2222 if (info->flags & ASYNC_CLOSING)
2223 interruptible_sleep_on(&info->close_wait);
2224 #ifdef SERIAL_DO_RESTART
2225 if (info->flags & ASYNC_HUP_NOTIFY)
2226 return -EAGAIN;
2227 else
2228 return -ERESTARTSYS;
2229 #else
2230 return -EAGAIN;
2231 #endif
2235 * If this is a callout device, then just make sure the normal
2236 * device isn't being used.
2238 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
2239 if (info->flags & ASYNC_NORMAL_ACTIVE)
2240 return -EBUSY;
2241 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2242 (info->flags & ASYNC_SESSION_LOCKOUT) &&
2243 (info->session != current->session))
2244 return -EBUSY;
2245 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2246 (info->flags & ASYNC_PGRP_LOCKOUT) &&
2247 (info->pgrp != current->pgrp))
2248 return -EBUSY;
2249 info->flags |= ASYNC_CALLOUT_ACTIVE;
2250 return 0;
2254 * If non-blocking mode is set, or the port is not enabled,
2255 * then make the check up front and then exit.
2257 if ((filp->f_flags & O_NONBLOCK) ||
2258 (tty->flags & (1 << TTY_IO_ERROR))) {
2259 if (info->flags & ASYNC_CALLOUT_ACTIVE)
2260 return -EBUSY;
2261 info->flags |= ASYNC_NORMAL_ACTIVE;
2262 return 0;
2265 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
2266 if (info->normal_termios.c_cflag & CLOCAL)
2267 do_clocal = 1;
2268 } else {
2269 if (tty->termios->c_cflag & CLOCAL)
2270 do_clocal = 1;
2274 * Block waiting for the carrier detect and the line to become
2275 * free (i.e., not in use by the callout). While we are in
2276 * this loop, info->count is dropped by one, so that
2277 * rs_close() knows when to free things. We restore it upon
2278 * exit, either normal or abnormal.
2280 retval = 0;
2281 add_wait_queue(&info->open_wait, &wait);
2282 #ifdef SERIAL_DEBUG_OPEN
2283 printk("block_til_ready before block: ttys%d, count = %d\n",
2284 info->line, info->count);
2285 #endif
2286 cli();
2287 if (!tty_hung_up_p(filp))
2288 info->count--;
2289 sti();
2290 info->blocked_open++;
2291 while (1) {
2292 cli();
2293 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2294 (tty->termios->c_cflag & CBAUD)) {
2295 unsigned int scratch;
2297 serial_out(info, UART_ESI_CMD1, ESI_READ_UART);
2298 serial_out(info, UART_ESI_CMD2, UART_MCR);
2299 scratch = serial_in(info, UART_ESI_STAT1);
2300 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2301 serial_out(info, UART_ESI_CMD2, UART_MCR);
2302 serial_out(info, UART_ESI_CMD2,
2303 scratch | UART_MCR_DTR | UART_MCR_RTS);
2305 sti();
2306 set_current_state(TASK_INTERRUPTIBLE);
2307 if (tty_hung_up_p(filp) ||
2308 !(info->flags & ASYNC_INITIALIZED)) {
2309 #ifdef SERIAL_DO_RESTART
2310 if (info->flags & ASYNC_HUP_NOTIFY)
2311 retval = -EAGAIN;
2312 else
2313 retval = -ERESTARTSYS;
2314 #else
2315 retval = -EAGAIN;
2316 #endif
2317 break;
2320 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
2321 if (serial_in(info, UART_ESI_STAT2) & UART_MSR_DCD)
2322 do_clocal = 1;
2324 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2325 !(info->flags & ASYNC_CLOSING) &&
2326 (do_clocal))
2327 break;
2328 if (signal_pending(current)) {
2329 retval = -ERESTARTSYS;
2330 break;
2332 #ifdef SERIAL_DEBUG_OPEN
2333 printk("block_til_ready blocking: ttys%d, count = %d\n",
2334 info->line, info->count);
2335 #endif
2336 schedule();
2338 current->state = TASK_RUNNING;
2339 remove_wait_queue(&info->open_wait, &wait);
2340 if (!tty_hung_up_p(filp))
2341 info->count++;
2342 info->blocked_open--;
2343 #ifdef SERIAL_DEBUG_OPEN
2344 printk("block_til_ready after blocking: ttys%d, count = %d\n",
2345 info->line, info->count);
2346 #endif
2347 if (retval)
2348 return retval;
2349 info->flags |= ASYNC_NORMAL_ACTIVE;
2350 return 0;
2354 * This routine is called whenever a serial port is opened. It
2355 * enables interrupts for a serial port, linking in its async structure into
2356 * the IRQ chain. It also performs the serial-specific
2357 * initialization for the tty structure.
2359 static int esp_open(struct tty_struct *tty, struct file * filp)
2361 struct esp_struct *info;
2362 int retval, line;
2363 unsigned long page;
2365 line = MINOR(tty->device) - tty->driver.minor_start;
2366 if ((line < 0) || (line >= NR_PORTS))
2367 return -ENODEV;
2369 /* find the port in the chain */
2371 info = ports;
2373 while (info && (info->line != line))
2374 info = info->next_port;
2376 if (!info) {
2377 serial_paranoia_check(info, tty->device, "esp_open");
2378 return -ENODEV;
2381 #ifdef SERIAL_DEBUG_OPEN
2382 printk("esp_open %s%d, count = %d\n", tty->driver.name, info->line,
2383 info->count);
2384 #endif
2385 MOD_INC_USE_COUNT;
2386 info->count++;
2387 tty->driver_data = info;
2388 info->tty = tty;
2390 if (!tmp_buf) {
2391 page = get_free_page(GFP_KERNEL);
2392 if (!page)
2393 return -ENOMEM;
2394 if (tmp_buf)
2395 free_page(page);
2396 else
2397 tmp_buf = (unsigned char *) page;
2401 * Start up serial port
2403 retval = startup(info);
2404 if (retval)
2405 return retval;
2407 retval = block_til_ready(tty, filp, info);
2408 if (retval) {
2409 #ifdef SERIAL_DEBUG_OPEN
2410 printk("esp_open returning after block_til_ready with %d\n",
2411 retval);
2412 #endif
2413 return retval;
2416 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
2417 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2418 *tty->termios = info->normal_termios;
2419 else
2420 *tty->termios = info->callout_termios;
2421 change_speed(info);
2424 info->session = current->session;
2425 info->pgrp = current->pgrp;
2427 #ifdef SERIAL_DEBUG_OPEN
2428 printk("esp_open ttys%d successful...", info->line);
2429 #endif
2430 return 0;
2434 * ---------------------------------------------------------------------
2435 * espserial_init() and friends
2437 * espserial_init() is called at boot-time to initialize the serial driver.
2438 * ---------------------------------------------------------------------
2442 * This routine prints out the appropriate serial driver version
2443 * number, and identifies which options were configured into this
2444 * driver.
2447 static _INLINE_ void show_serial_version(void)
2449 printk(KERN_INFO "%s version %s (DMA %u)\n",
2450 serial_name, serial_version, dma);
2454 * This routine is called by espserial_init() to initialize a specific serial
2455 * port.
2457 static _INLINE_ int autoconfig(struct esp_struct * info, int *region_start)
2459 int port_detected = 0;
2460 unsigned long flags;
2462 save_flags(flags); cli();
2465 * Check for ESP card
2468 if (!check_region(info->port, 8) &&
2469 serial_in(info, UART_ESI_BASE) == 0xf3) {
2470 serial_out(info, UART_ESI_CMD1, 0x00);
2471 serial_out(info, UART_ESI_CMD1, 0x01);
2473 if ((serial_in(info, UART_ESI_STAT2) & 0x70) == 0x20) {
2474 port_detected = 1;
2476 if (!(info->irq)) {
2477 serial_out(info, UART_ESI_CMD1, 0x02);
2479 if (serial_in(info, UART_ESI_STAT1) & 0x01)
2480 info->irq = 3;
2481 else
2482 info->irq = 4;
2485 if (ports && (ports->port == (info->port - 8))) {
2486 release_region(*region_start,
2487 info->port - *region_start);
2488 } else
2489 *region_start = info->port;
2491 request_region(*region_start,
2492 info->port - *region_start + 8,
2493 "esp serial");
2495 /* put card in enhanced mode */
2496 /* this prevents access through */
2497 /* the "old" IO ports */
2498 esp_basic_init(info);
2500 /* clear out MCR */
2501 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2502 serial_out(info, UART_ESI_CMD2, UART_MCR);
2503 serial_out(info, UART_ESI_CMD2, 0x00);
2507 restore_flags(flags);
2508 return (port_detected);
2512 * The serial driver boot-time initialization code!
2514 int __init espserial_init(void)
2516 int i, offset;
2517 int region_start;
2518 struct esp_struct * info;
2519 struct esp_struct *last_primary = 0;
2520 int esp[] = {0x100,0x140,0x180,0x200,0x240,0x280,0x300,0x380};
2522 init_bh(ESP_BH, do_serial_bh);
2524 for (i = 0; i < NR_PRIMARY; i++) {
2525 if (irq[i] != 0) {
2526 if ((irq[i] < 2) || (irq[i] > 15) || (irq[i] == 6) ||
2527 (irq[i] == 8) || (irq[i] == 13))
2528 irq[i] = 0;
2529 else if (irq[i] == 2)
2530 irq[i] = 9;
2534 if ((dma != 1) && (dma != 3))
2535 dma = 0;
2537 if ((rx_trigger < 1) || (rx_trigger > 1023))
2538 rx_trigger = 768;
2540 if ((tx_trigger < 1) || (tx_trigger > 1023))
2541 tx_trigger = 768;
2543 if ((flow_off < 1) || (flow_off > 1023))
2544 flow_off = 1016;
2546 if ((flow_on < 1) || (flow_on > 1023))
2547 flow_on = 944;
2549 if ((rx_timeout < 0) || (rx_timeout > 255))
2550 rx_timeout = 128;
2552 if (flow_on >= flow_off)
2553 flow_on = flow_off - 1;
2555 show_serial_version();
2557 /* Initialize the tty_driver structure */
2559 memset(&esp_driver, 0, sizeof(struct tty_driver));
2560 esp_driver.magic = TTY_DRIVER_MAGIC;
2561 esp_driver.name = "ttyP";
2562 esp_driver.major = ESP_IN_MAJOR;
2563 esp_driver.minor_start = 0;
2564 esp_driver.num = NR_PORTS;
2565 esp_driver.type = TTY_DRIVER_TYPE_SERIAL;
2566 esp_driver.subtype = SERIAL_TYPE_NORMAL;
2567 esp_driver.init_termios = tty_std_termios;
2568 esp_driver.init_termios.c_cflag =
2569 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2570 esp_driver.flags = TTY_DRIVER_REAL_RAW;
2571 esp_driver.refcount = &serial_refcount;
2572 esp_driver.table = serial_table;
2573 esp_driver.termios = serial_termios;
2574 esp_driver.termios_locked = serial_termios_locked;
2576 esp_driver.open = esp_open;
2577 esp_driver.close = rs_close;
2578 esp_driver.write = rs_write;
2579 esp_driver.put_char = rs_put_char;
2580 esp_driver.flush_chars = rs_flush_chars;
2581 esp_driver.write_room = rs_write_room;
2582 esp_driver.chars_in_buffer = rs_chars_in_buffer;
2583 esp_driver.flush_buffer = rs_flush_buffer;
2584 esp_driver.ioctl = rs_ioctl;
2585 esp_driver.throttle = rs_throttle;
2586 esp_driver.unthrottle = rs_unthrottle;
2587 esp_driver.set_termios = rs_set_termios;
2588 esp_driver.stop = rs_stop;
2589 esp_driver.start = rs_start;
2590 esp_driver.hangup = esp_hangup;
2591 esp_driver.break_ctl = esp_break;
2592 esp_driver.wait_until_sent = rs_wait_until_sent;
2595 * The callout device is just like normal device except for
2596 * major number and the subtype code.
2598 esp_callout_driver = esp_driver;
2599 esp_callout_driver.name = "cup";
2600 esp_callout_driver.major = ESP_OUT_MAJOR;
2601 esp_callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2603 if (tty_register_driver(&esp_driver))
2605 printk(KERN_ERR "Couldn't register esp serial driver");
2606 return 1;
2609 if (tty_register_driver(&esp_callout_driver))
2611 printk(KERN_ERR "Couldn't register esp callout driver");
2612 tty_unregister_driver(&esp_driver);
2613 return 1;
2616 info = (struct esp_struct *)kmalloc(sizeof(struct esp_struct),
2617 GFP_KERNEL);
2619 if (!info)
2621 printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
2622 tty_unregister_driver(&esp_driver);
2623 tty_unregister_driver(&esp_callout_driver);
2624 return 1;
2627 memset((void *)info, 0, sizeof(struct esp_struct));
2628 /* rx_trigger, tx_trigger are needed by autoconfig */
2629 info->config.rx_trigger = rx_trigger;
2630 info->config.tx_trigger = tx_trigger;
2632 i = 0;
2633 offset = 0;
2635 do {
2636 info->port = esp[i] + offset;
2637 info->irq = irq[i];
2638 info->line = (i * 8) + (offset / 8);
2640 if (!autoconfig(info, &region_start)) {
2641 i++;
2642 offset = 0;
2643 continue;
2646 info->custom_divisor = (divisor[i] >> (offset / 2)) & 0xf;
2647 info->flags = STD_COM_FLAGS;
2648 if (info->custom_divisor)
2649 info->flags |= ASYNC_SPD_CUST;
2650 info->magic = ESP_MAGIC;
2651 info->close_delay = 5*HZ/10;
2652 info->closing_wait = 30*HZ;
2653 info->tqueue.routine = do_softint;
2654 info->tqueue.data = info;
2655 info->tqueue_hangup.routine = do_serial_hangup;
2656 info->tqueue_hangup.data = info;
2657 info->callout_termios = esp_callout_driver.init_termios;
2658 info->normal_termios = esp_driver.init_termios;
2659 info->config.rx_timeout = rx_timeout;
2660 info->config.flow_on = flow_on;
2661 info->config.flow_off = flow_off;
2662 info->config.pio_threshold = pio_threshold;
2663 info->next_port = ports;
2664 init_waitqueue_head(&info->open_wait);
2665 init_waitqueue_head(&info->close_wait);
2666 init_waitqueue_head(&info->delta_msr_wait);
2667 init_waitqueue_head(&info->break_wait);
2668 ports = info;
2669 printk(KERN_INFO "ttyP%d at 0x%04x (irq = %d) is an ESP ",
2670 info->line, info->port, info->irq);
2672 if (info->line % 8) {
2673 printk("secondary port\n");
2674 /* 8 port cards can't do DMA */
2675 info->stat_flags |= ESP_STAT_NEVER_DMA;
2677 if (last_primary)
2678 last_primary->stat_flags |= ESP_STAT_NEVER_DMA;
2679 } else {
2680 printk("primary port\n");
2681 last_primary = info;
2682 irq[i] = info->irq;
2685 if (!dma)
2686 info->stat_flags |= ESP_STAT_NEVER_DMA;
2688 info = (struct esp_struct *)kmalloc(sizeof(struct esp_struct),
2689 GFP_KERNEL);
2690 if (!info)
2692 printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
2694 /* allow use of the already detected ports */
2695 return 0;
2698 memset((void *)info, 0, sizeof(struct esp_struct));
2699 /* rx_trigger, tx_trigger are needed by autoconfig */
2700 info->config.rx_trigger = rx_trigger;
2701 info->config.tx_trigger = tx_trigger;
2703 if (offset == 56) {
2704 i++;
2705 offset = 0;
2706 } else {
2707 offset += 8;
2709 } while (i < NR_PRIMARY);
2711 /* free the last port memory allocation */
2712 kfree(info);
2714 return 0;
2717 #ifdef MODULE
2719 int init_module(void)
2721 return espserial_init();
2724 void cleanup_module(void)
2726 unsigned long flags;
2727 int e1, e2;
2728 unsigned int region_start, region_end;
2729 struct esp_struct *temp_async;
2730 struct esp_pio_buffer *pio_buf;
2732 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2733 save_flags(flags);
2734 cli();
2735 remove_bh(ESP_BH);
2736 if ((e1 = tty_unregister_driver(&esp_driver)))
2737 printk("SERIAL: failed to unregister serial driver (%d)\n",
2738 e1);
2739 if ((e2 = tty_unregister_driver(&esp_callout_driver)))
2740 printk("SERIAL: failed to unregister callout driver (%d)\n",
2741 e2);
2742 restore_flags(flags);
2744 while (ports) {
2745 if (ports->port) {
2746 region_start = region_end = ports->port;
2747 temp_async = ports;
2749 while (temp_async) {
2750 if ((region_start - temp_async->port) == 8) {
2751 region_start = temp_async->port;
2752 temp_async->port = 0;
2753 temp_async = ports;
2754 } else if ((temp_async->port - region_end)
2755 == 8) {
2756 region_end = temp_async->port;
2757 temp_async->port = 0;
2758 temp_async = ports;
2759 } else
2760 temp_async = temp_async->next_port;
2763 release_region(region_start,
2764 region_end - region_start + 8);
2767 temp_async = ports->next_port;
2768 kfree(ports);
2769 ports = temp_async;
2772 if (dma_buffer)
2773 free_pages((unsigned int)dma_buffer,
2774 get_order(DMA_BUFFER_SZ));
2776 if (tmp_buf)
2777 free_page((unsigned long)tmp_buf);
2779 while (free_pio_buf) {
2780 pio_buf = free_pio_buf->next;
2781 kfree(free_pio_buf);
2782 free_pio_buf = pio_buf;
2785 #endif /* MODULE */