[SCSI] zfcp: fix device registration issues
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / char / esp.c
blob09dc4b01232c018e95733b303a86ee8c2f9ed1ba
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�pl 05/17/96.
24 * --- End of notices from serial.c ---
26 * Support for the ESP serial card by Andrew J. Robinson
27 * <arobinso@nyx.net> (Card detection routine taken from a patch
28 * by Dennis J. Boylan). Patches to allow use with 2.1.x contributed
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 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> -
36 * several cleanups, use module_init/module_exit, etc
38 * This module exports the following rs232 io functions:
40 * int espserial_init(void);
43 #include <linux/module.h>
44 #include <linux/errno.h>
45 #include <linux/signal.h>
46 #include <linux/sched.h>
47 #include <linux/interrupt.h>
48 #include <linux/tty.h>
49 #include <linux/tty_flip.h>
50 #include <linux/serial.h>
51 #include <linux/serialP.h>
52 #include <linux/serial_reg.h>
53 #include <linux/major.h>
54 #include <linux/string.h>
55 #include <linux/fcntl.h>
56 #include <linux/ptrace.h>
57 #include <linux/ioport.h>
58 #include <linux/mm.h>
59 #include <linux/init.h>
60 #include <linux/delay.h>
62 #include <asm/system.h>
63 #include <asm/io.h>
64 #include <asm/bitops.h>
66 #include <asm/dma.h>
67 #include <linux/slab.h>
68 #include <asm/uaccess.h>
70 #include <linux/hayesesp.h>
72 #define NR_PORTS 64 /* maximum number of ports */
73 #define NR_PRIMARY 8 /* maximum number of primary ports */
74 #define REGION_SIZE 8 /* size of io region to request */
76 /* The following variables can be set by giving module options */
77 static int irq[NR_PRIMARY]; /* IRQ for each base port */
78 static unsigned int divisor[NR_PRIMARY]; /* custom divisor for each port */
79 static unsigned int dma = ESP_DMA_CHANNEL; /* DMA channel */
80 static unsigned int rx_trigger = ESP_RX_TRIGGER;
81 static unsigned int tx_trigger = ESP_TX_TRIGGER;
82 static unsigned int flow_off = ESP_FLOW_OFF;
83 static unsigned int flow_on = ESP_FLOW_ON;
84 static unsigned int rx_timeout = ESP_RX_TMOUT;
85 static unsigned int pio_threshold = ESP_PIO_THRESHOLD;
87 MODULE_LICENSE("GPL");
89 module_param_array(irq, int, NULL, 0);
90 module_param_array(divisor, uint, NULL, 0);
91 module_param(dma, uint, 0);
92 module_param(rx_trigger, uint, 0);
93 module_param(tx_trigger, uint, 0);
94 module_param(flow_off, uint, 0);
95 module_param(flow_on, uint, 0);
96 module_param(rx_timeout, uint, 0);
97 module_param(pio_threshold, uint, 0);
99 /* END */
101 static char *dma_buffer;
102 static int dma_bytes;
103 static struct esp_pio_buffer *free_pio_buf;
105 #define DMA_BUFFER_SZ 1024
107 #define WAKEUP_CHARS 1024
109 static char serial_name[] __initdata = "ESP serial driver";
110 static char serial_version[] __initdata = "2.2";
112 static struct tty_driver *esp_driver;
114 /* serial subtype definitions */
115 #define SERIAL_TYPE_NORMAL 1
118 * Serial driver configuration section. Here are the various options:
120 * SERIAL_PARANOIA_CHECK
121 * Check the magic number for the esp_structure where
122 * ever possible.
125 #undef SERIAL_PARANOIA_CHECK
126 #define SERIAL_DO_RESTART
128 #undef SERIAL_DEBUG_INTR
129 #undef SERIAL_DEBUG_OPEN
130 #undef SERIAL_DEBUG_FLOW
132 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
133 #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
134 tty->name, (info->flags), serial_driver.refcount,info->count,tty->count,s)
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 inline int serial_paranoia_check(struct esp_struct *info,
154 char *name, const char *routine)
156 #ifdef SERIAL_PARANOIA_CHECK
157 static const char badmagic[] = KERN_WARNING
158 "Warning: bad magic number for serial struct (%s) in %s\n";
159 static const char badinfo[] = KERN_WARNING
160 "Warning: null esp_struct for (%s) in %s\n";
162 if (!info) {
163 printk(badinfo, name, routine);
164 return 1;
166 if (info->magic != ESP_MAGIC) {
167 printk(badmagic, name, routine);
168 return 1;
170 #endif
171 return 0;
174 static inline unsigned int serial_in(struct esp_struct *info, int offset)
176 return inb(info->port + offset);
179 static inline void serial_out(struct esp_struct *info, int offset,
180 unsigned char value)
182 outb(value, info->port+offset);
186 * ------------------------------------------------------------
187 * rs_stop() and rs_start()
189 * This routines are called before setting or resetting tty->stopped.
190 * They enable or disable transmitter interrupts, as necessary.
191 * ------------------------------------------------------------
193 static void rs_stop(struct tty_struct *tty)
195 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
196 unsigned long flags;
198 if (serial_paranoia_check(info, tty->name, "rs_stop"))
199 return;
201 spin_lock_irqsave(&info->lock, flags);
202 if (info->IER & UART_IER_THRI) {
203 info->IER &= ~UART_IER_THRI;
204 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
205 serial_out(info, UART_ESI_CMD2, info->IER);
207 spin_unlock_irqrestore(&info->lock, flags);
210 static void rs_start(struct tty_struct *tty)
212 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
213 unsigned long flags;
215 if (serial_paranoia_check(info, tty->name, "rs_start"))
216 return;
218 spin_lock_irqsave(&info->lock, flags);
219 if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
220 info->IER |= UART_IER_THRI;
221 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
222 serial_out(info, UART_ESI_CMD2, info->IER);
224 spin_unlock_irqrestore(&info->lock, flags);
228 * ----------------------------------------------------------------------
230 * Here starts the interrupt handling routines. All of the following
231 * subroutines are declared as inline and are folded into
232 * rs_interrupt(). They were separated out for readability's sake.
234 * Note: rs_interrupt() is a "fast" interrupt, which means that it
235 * runs with interrupts turned off. People who may want to modify
236 * rs_interrupt() should try to keep the interrupt handler as fast as
237 * possible. After you are done making modifications, it is not a bad
238 * idea to do:
240 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
242 * and look at the resulting assemble code in serial.s.
244 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
245 * -----------------------------------------------------------------------
249 * This routine is used by the interrupt handler to schedule
250 * processing in the software interrupt portion of the driver.
252 static inline void rs_sched_event(struct esp_struct *info,
253 int event)
255 info->event |= 1 << event;
256 schedule_work(&info->tqueue);
259 static DEFINE_SPINLOCK(pio_lock);
261 static inline struct esp_pio_buffer *get_pio_buffer(void)
263 struct esp_pio_buffer *buf;
264 unsigned long flags;
266 spin_lock_irqsave(&pio_lock, flags);
267 if (free_pio_buf) {
268 buf = free_pio_buf;
269 free_pio_buf = buf->next;
270 } else {
271 buf = kmalloc(sizeof(struct esp_pio_buffer), GFP_ATOMIC);
273 spin_unlock_irqrestore(&pio_lock, flags);
274 return buf;
277 static inline void release_pio_buffer(struct esp_pio_buffer *buf)
279 unsigned long flags;
280 spin_lock_irqsave(&pio_lock, flags);
281 buf->next = free_pio_buf;
282 free_pio_buf = buf;
283 spin_unlock_irqrestore(&pio_lock, flags);
286 static inline void receive_chars_pio(struct esp_struct *info, int num_bytes)
288 struct tty_struct *tty = info->tty;
289 int i;
290 struct esp_pio_buffer *pio_buf;
291 struct esp_pio_buffer *err_buf;
292 unsigned char status_mask;
294 pio_buf = get_pio_buffer();
296 if (!pio_buf)
297 return;
299 err_buf = get_pio_buffer();
301 if (!err_buf) {
302 release_pio_buffer(pio_buf);
303 return;
306 status_mask = (info->read_status_mask >> 2) & 0x07;
308 for (i = 0; i < num_bytes - 1; i += 2) {
309 *((unsigned short *)(pio_buf->data + i)) =
310 inw(info->port + UART_ESI_RX);
311 err_buf->data[i] = serial_in(info, UART_ESI_RWS);
312 err_buf->data[i + 1] = (err_buf->data[i] >> 3) & status_mask;
313 err_buf->data[i] &= status_mask;
316 if (num_bytes & 0x0001) {
317 pio_buf->data[num_bytes - 1] = serial_in(info, UART_ESI_RX);
318 err_buf->data[num_bytes - 1] =
319 (serial_in(info, UART_ESI_RWS) >> 3) & status_mask;
322 /* make sure everything is still ok since interrupts were enabled */
323 tty = info->tty;
325 if (!tty) {
326 release_pio_buffer(pio_buf);
327 release_pio_buffer(err_buf);
328 info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
329 return;
332 status_mask = (info->ignore_status_mask >> 2) & 0x07;
334 for (i = 0; i < num_bytes; i++) {
335 if (!(err_buf->data[i] & status_mask)) {
336 int flag = 0;
338 if (err_buf->data[i] & 0x04) {
339 flag = TTY_BREAK;
340 if (info->flags & ASYNC_SAK)
341 do_SAK(tty);
343 else if (err_buf->data[i] & 0x02)
344 flag = TTY_FRAME;
345 else if (err_buf->data[i] & 0x01)
346 flag = TTY_PARITY;
347 tty_insert_flip_char(tty, pio_buf->data[i], flag);
351 tty_schedule_flip(tty);
353 info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
354 release_pio_buffer(pio_buf);
355 release_pio_buffer(err_buf);
358 static inline void receive_chars_dma(struct esp_struct *info, int num_bytes)
360 unsigned long flags;
361 info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
362 dma_bytes = num_bytes;
363 info->stat_flags |= ESP_STAT_DMA_RX;
365 flags=claim_dma_lock();
366 disable_dma(dma);
367 clear_dma_ff(dma);
368 set_dma_mode(dma, DMA_MODE_READ);
369 set_dma_addr(dma, isa_virt_to_bus(dma_buffer));
370 set_dma_count(dma, dma_bytes);
371 enable_dma(dma);
372 release_dma_lock(flags);
374 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_RX);
377 static inline void receive_chars_dma_done(struct esp_struct *info,
378 int status)
380 struct tty_struct *tty = info->tty;
381 int num_bytes;
382 unsigned long flags;
384 flags=claim_dma_lock();
385 disable_dma(dma);
386 clear_dma_ff(dma);
388 info->stat_flags &= ~ESP_STAT_DMA_RX;
389 num_bytes = dma_bytes - get_dma_residue(dma);
390 release_dma_lock(flags);
392 info->icount.rx += num_bytes;
394 if (num_bytes > 0) {
395 tty_insert_flip_string(tty, dma_buffer, num_bytes - 1);
397 status &= (0x1c & info->read_status_mask);
399 /* Is the status significant or do we throw the last byte ? */
400 if (!(status & info->ignore_status_mask)) {
401 int statflag = 0;
403 if (status & 0x10) {
404 statflag = TTY_BREAK;
405 (info->icount.brk)++;
406 if (info->flags & ASYNC_SAK)
407 do_SAK(tty);
408 } else if (status & 0x08) {
409 statflag = TTY_FRAME;
410 (info->icount.frame)++;
412 else if (status & 0x04) {
413 statflag = TTY_PARITY;
414 (info->icount.parity)++;
416 tty_insert_flip_char(tty, dma_buffer[num_bytes - 1], statflag);
418 tty_schedule_flip(tty);
421 if (dma_bytes != num_bytes) {
422 num_bytes = dma_bytes - num_bytes;
423 dma_bytes = 0;
424 receive_chars_dma(info, num_bytes);
425 } else
426 dma_bytes = 0;
429 /* Caller must hold info->lock */
431 static inline void transmit_chars_pio(struct esp_struct *info,
432 int space_avail)
434 int i;
435 struct esp_pio_buffer *pio_buf;
437 pio_buf = get_pio_buffer();
439 if (!pio_buf)
440 return;
442 while (space_avail && info->xmit_cnt) {
443 if (info->xmit_tail + space_avail <= ESP_XMIT_SIZE) {
444 memcpy(pio_buf->data,
445 &(info->xmit_buf[info->xmit_tail]),
446 space_avail);
447 } else {
448 i = ESP_XMIT_SIZE - info->xmit_tail;
449 memcpy(pio_buf->data,
450 &(info->xmit_buf[info->xmit_tail]), i);
451 memcpy(&(pio_buf->data[i]), info->xmit_buf,
452 space_avail - i);
455 info->xmit_cnt -= space_avail;
456 info->xmit_tail = (info->xmit_tail + space_avail) &
457 (ESP_XMIT_SIZE - 1);
459 for (i = 0; i < space_avail - 1; i += 2) {
460 outw(*((unsigned short *)(pio_buf->data + i)),
461 info->port + UART_ESI_TX);
464 if (space_avail & 0x0001)
465 serial_out(info, UART_ESI_TX,
466 pio_buf->data[space_avail - 1]);
468 if (info->xmit_cnt) {
469 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
470 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
471 space_avail = serial_in(info, UART_ESI_STAT1) << 8;
472 space_avail |= serial_in(info, UART_ESI_STAT2);
474 if (space_avail > info->xmit_cnt)
475 space_avail = info->xmit_cnt;
479 if (info->xmit_cnt < WAKEUP_CHARS) {
480 rs_sched_event(info, ESP_EVENT_WRITE_WAKEUP);
482 #ifdef SERIAL_DEBUG_INTR
483 printk("THRE...");
484 #endif
486 if (info->xmit_cnt <= 0) {
487 info->IER &= ~UART_IER_THRI;
488 serial_out(info, UART_ESI_CMD1,
489 ESI_SET_SRV_MASK);
490 serial_out(info, UART_ESI_CMD2, info->IER);
494 release_pio_buffer(pio_buf);
497 /* Caller must hold info->lock */
498 static inline void transmit_chars_dma(struct esp_struct *info, int num_bytes)
500 unsigned long flags;
502 dma_bytes = num_bytes;
504 if (info->xmit_tail + dma_bytes <= ESP_XMIT_SIZE) {
505 memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
506 dma_bytes);
507 } else {
508 int i = ESP_XMIT_SIZE - info->xmit_tail;
509 memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
511 memcpy(&(dma_buffer[i]), info->xmit_buf, dma_bytes - i);
514 info->xmit_cnt -= dma_bytes;
515 info->xmit_tail = (info->xmit_tail + dma_bytes) & (ESP_XMIT_SIZE - 1);
517 if (info->xmit_cnt < WAKEUP_CHARS) {
518 rs_sched_event(info, ESP_EVENT_WRITE_WAKEUP);
520 #ifdef SERIAL_DEBUG_INTR
521 printk("THRE...");
522 #endif
524 if (info->xmit_cnt <= 0) {
525 info->IER &= ~UART_IER_THRI;
526 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
527 serial_out(info, UART_ESI_CMD2, info->IER);
531 info->stat_flags |= ESP_STAT_DMA_TX;
533 flags=claim_dma_lock();
534 disable_dma(dma);
535 clear_dma_ff(dma);
536 set_dma_mode(dma, DMA_MODE_WRITE);
537 set_dma_addr(dma, isa_virt_to_bus(dma_buffer));
538 set_dma_count(dma, dma_bytes);
539 enable_dma(dma);
540 release_dma_lock(flags);
542 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
545 static inline void transmit_chars_dma_done(struct esp_struct *info)
547 int num_bytes;
548 unsigned long flags;
551 flags=claim_dma_lock();
552 disable_dma(dma);
553 clear_dma_ff(dma);
555 num_bytes = dma_bytes - get_dma_residue(dma);
556 info->icount.tx += dma_bytes;
557 release_dma_lock(flags);
559 if (dma_bytes != num_bytes) {
560 dma_bytes -= num_bytes;
561 memmove(dma_buffer, dma_buffer + num_bytes, dma_bytes);
563 flags=claim_dma_lock();
564 disable_dma(dma);
565 clear_dma_ff(dma);
566 set_dma_mode(dma, DMA_MODE_WRITE);
567 set_dma_addr(dma, isa_virt_to_bus(dma_buffer));
568 set_dma_count(dma, dma_bytes);
569 enable_dma(dma);
570 release_dma_lock(flags);
572 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
573 } else {
574 dma_bytes = 0;
575 info->stat_flags &= ~ESP_STAT_DMA_TX;
579 static inline void check_modem_status(struct esp_struct *info)
581 int status;
583 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
584 status = serial_in(info, UART_ESI_STAT2);
586 if (status & UART_MSR_ANY_DELTA) {
587 /* update input line counters */
588 if (status & UART_MSR_TERI)
589 info->icount.rng++;
590 if (status & UART_MSR_DDSR)
591 info->icount.dsr++;
592 if (status & UART_MSR_DDCD)
593 info->icount.dcd++;
594 if (status & UART_MSR_DCTS)
595 info->icount.cts++;
596 wake_up_interruptible(&info->delta_msr_wait);
599 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
600 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
601 printk("ttys%d CD now %s...", info->line,
602 (status & UART_MSR_DCD) ? "on" : "off");
603 #endif
604 if (status & UART_MSR_DCD)
605 wake_up_interruptible(&info->open_wait);
606 else {
607 #ifdef SERIAL_DEBUG_OPEN
608 printk("scheduling hangup...");
609 #endif
610 schedule_work(&info->tqueue_hangup);
616 * This is the serial driver's interrupt routine
618 static irqreturn_t rs_interrupt_single(int irq, void *dev_id,
619 struct pt_regs *regs)
621 struct esp_struct * info;
622 unsigned err_status;
623 unsigned int scratch;
625 #ifdef SERIAL_DEBUG_INTR
626 printk("rs_interrupt_single(%d)...", irq);
627 #endif
628 info = (struct esp_struct *)dev_id;
629 err_status = 0;
630 scratch = serial_in(info, UART_ESI_SID);
632 spin_lock(&info->lock);
634 if (!info->tty) {
635 spin_unlock(&info->lock);
636 return IRQ_NONE;
639 if (scratch & 0x04) { /* error */
640 serial_out(info, UART_ESI_CMD1, ESI_GET_ERR_STAT);
641 err_status = serial_in(info, UART_ESI_STAT1);
642 serial_in(info, UART_ESI_STAT2);
644 if (err_status & 0x01)
645 info->stat_flags |= ESP_STAT_RX_TIMEOUT;
647 if (err_status & 0x20) /* UART status */
648 check_modem_status(info);
650 if (err_status & 0x80) /* Start break */
651 wake_up_interruptible(&info->break_wait);
654 if ((scratch & 0x88) || /* DMA completed or timed out */
655 (err_status & 0x1c) /* receive error */) {
656 if (info->stat_flags & ESP_STAT_DMA_RX)
657 receive_chars_dma_done(info, err_status);
658 else if (info->stat_flags & ESP_STAT_DMA_TX)
659 transmit_chars_dma_done(info);
662 if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
663 ((scratch & 0x01) || (info->stat_flags & ESP_STAT_RX_TIMEOUT)) &&
664 (info->IER & UART_IER_RDI)) {
665 int num_bytes;
667 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
668 serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
669 num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
670 num_bytes |= serial_in(info, UART_ESI_STAT2);
672 num_bytes = tty_buffer_request_room(info->tty, num_bytes);
674 if (num_bytes) {
675 if (dma_bytes ||
676 (info->stat_flags & ESP_STAT_USE_PIO) ||
677 (num_bytes <= info->config.pio_threshold))
678 receive_chars_pio(info, num_bytes);
679 else
680 receive_chars_dma(info, num_bytes);
684 if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
685 (scratch & 0x02) && (info->IER & UART_IER_THRI)) {
686 if ((info->xmit_cnt <= 0) || info->tty->stopped) {
687 info->IER &= ~UART_IER_THRI;
688 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
689 serial_out(info, UART_ESI_CMD2, info->IER);
690 } else {
691 int num_bytes;
693 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
694 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
695 num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
696 num_bytes |= serial_in(info, UART_ESI_STAT2);
698 if (num_bytes > info->xmit_cnt)
699 num_bytes = info->xmit_cnt;
701 if (num_bytes) {
702 if (dma_bytes ||
703 (info->stat_flags & ESP_STAT_USE_PIO) ||
704 (num_bytes <= info->config.pio_threshold))
705 transmit_chars_pio(info, num_bytes);
706 else
707 transmit_chars_dma(info, num_bytes);
712 info->last_active = jiffies;
714 #ifdef SERIAL_DEBUG_INTR
715 printk("end.\n");
716 #endif
717 spin_unlock(&info->lock);
718 return IRQ_HANDLED;
722 * -------------------------------------------------------------------
723 * Here ends the serial interrupt routines.
724 * -------------------------------------------------------------------
727 static void do_softint(void *private_)
729 struct esp_struct *info = (struct esp_struct *) private_;
730 struct tty_struct *tty;
732 tty = info->tty;
733 if (!tty)
734 return;
736 if (test_and_clear_bit(ESP_EVENT_WRITE_WAKEUP, &info->event)) {
737 tty_wakeup(tty);
742 * This routine is called from the scheduler tqueue when the interrupt
743 * routine has signalled that a hangup has occurred. The path of
744 * hangup processing is:
746 * serial interrupt routine -> (scheduler tqueue) ->
747 * do_serial_hangup() -> tty->hangup() -> esp_hangup()
750 static void do_serial_hangup(void *private_)
752 struct esp_struct *info = (struct esp_struct *) private_;
753 struct tty_struct *tty;
755 tty = info->tty;
756 if (tty)
757 tty_hangup(tty);
761 * ---------------------------------------------------------------
762 * Low level utility subroutines for the serial driver: routines to
763 * figure out the appropriate timeout for an interrupt chain, routines
764 * to initialize and startup a serial port, and routines to shutdown a
765 * serial port. Useful stuff like that.
767 * Caller should hold lock
768 * ---------------------------------------------------------------
771 static inline void esp_basic_init(struct esp_struct * info)
773 /* put ESPC in enhanced mode */
774 serial_out(info, UART_ESI_CMD1, ESI_SET_MODE);
776 if (info->stat_flags & ESP_STAT_NEVER_DMA)
777 serial_out(info, UART_ESI_CMD2, 0x01);
778 else
779 serial_out(info, UART_ESI_CMD2, 0x31);
781 /* disable interrupts for now */
782 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
783 serial_out(info, UART_ESI_CMD2, 0x00);
785 /* set interrupt and DMA channel */
786 serial_out(info, UART_ESI_CMD1, ESI_SET_IRQ);
788 if (info->stat_flags & ESP_STAT_NEVER_DMA)
789 serial_out(info, UART_ESI_CMD2, 0x01);
790 else
791 serial_out(info, UART_ESI_CMD2, (dma << 4) | 0x01);
793 serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
795 if (info->line % 8) /* secondary port */
796 serial_out(info, UART_ESI_CMD2, 0x0d); /* shared */
797 else if (info->irq == 9)
798 serial_out(info, UART_ESI_CMD2, 0x02);
799 else
800 serial_out(info, UART_ESI_CMD2, info->irq);
802 /* set error status mask (check this) */
803 serial_out(info, UART_ESI_CMD1, ESI_SET_ERR_MASK);
805 if (info->stat_flags & ESP_STAT_NEVER_DMA)
806 serial_out(info, UART_ESI_CMD2, 0xa1);
807 else
808 serial_out(info, UART_ESI_CMD2, 0xbd);
810 serial_out(info, UART_ESI_CMD2, 0x00);
812 /* set DMA timeout */
813 serial_out(info, UART_ESI_CMD1, ESI_SET_DMA_TMOUT);
814 serial_out(info, UART_ESI_CMD2, 0xff);
816 /* set FIFO trigger levels */
817 serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
818 serial_out(info, UART_ESI_CMD2, info->config.rx_trigger >> 8);
819 serial_out(info, UART_ESI_CMD2, info->config.rx_trigger);
820 serial_out(info, UART_ESI_CMD2, info->config.tx_trigger >> 8);
821 serial_out(info, UART_ESI_CMD2, info->config.tx_trigger);
823 /* Set clock scaling and wait states */
824 serial_out(info, UART_ESI_CMD1, ESI_SET_PRESCALAR);
825 serial_out(info, UART_ESI_CMD2, 0x04 | ESPC_SCALE);
827 /* set reinterrupt pacing */
828 serial_out(info, UART_ESI_CMD1, ESI_SET_REINTR);
829 serial_out(info, UART_ESI_CMD2, 0xff);
832 static int startup(struct esp_struct * info)
834 unsigned long flags;
835 int retval=0;
836 unsigned int num_chars;
838 spin_lock_irqsave(&info->lock, flags);
840 if (info->flags & ASYNC_INITIALIZED)
841 goto out;
843 if (!info->xmit_buf) {
844 info->xmit_buf = (unsigned char *)get_zeroed_page(GFP_ATOMIC);
845 retval = -ENOMEM;
846 if (!info->xmit_buf)
847 goto out;
850 #ifdef SERIAL_DEBUG_OPEN
851 printk("starting up ttys%d (irq %d)...", info->line, info->irq);
852 #endif
854 /* Flush the RX buffer. Using the ESI flush command may cause */
855 /* wild interrupts, so read all the data instead. */
857 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
858 serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
859 num_chars = serial_in(info, UART_ESI_STAT1) << 8;
860 num_chars |= serial_in(info, UART_ESI_STAT2);
862 while (num_chars > 1) {
863 inw(info->port + UART_ESI_RX);
864 num_chars -= 2;
867 if (num_chars)
868 serial_in(info, UART_ESI_RX);
870 /* set receive character timeout */
871 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
872 serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
874 /* clear all flags except the "never DMA" flag */
875 info->stat_flags &= ESP_STAT_NEVER_DMA;
877 if (info->stat_flags & ESP_STAT_NEVER_DMA)
878 info->stat_flags |= ESP_STAT_USE_PIO;
880 spin_unlock_irqrestore(&info->lock, flags);
883 * Allocate the IRQ
886 retval = request_irq(info->irq, rs_interrupt_single, SA_SHIRQ,
887 "esp serial", info);
889 if (retval) {
890 if (capable(CAP_SYS_ADMIN)) {
891 if (info->tty)
892 set_bit(TTY_IO_ERROR,
893 &info->tty->flags);
894 retval = 0;
896 goto out_unlocked;
899 if (!(info->stat_flags & ESP_STAT_USE_PIO) && !dma_buffer) {
900 dma_buffer = (char *)__get_dma_pages(
901 GFP_KERNEL, get_order(DMA_BUFFER_SZ));
903 /* use PIO mode if DMA buf/chan cannot be allocated */
904 if (!dma_buffer)
905 info->stat_flags |= ESP_STAT_USE_PIO;
906 else if (request_dma(dma, "esp serial")) {
907 free_pages((unsigned long)dma_buffer,
908 get_order(DMA_BUFFER_SZ));
909 dma_buffer = NULL;
910 info->stat_flags |= ESP_STAT_USE_PIO;
915 info->MCR = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
917 spin_lock_irqsave(&info->lock, flags);
918 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
919 serial_out(info, UART_ESI_CMD2, UART_MCR);
920 serial_out(info, UART_ESI_CMD2, info->MCR);
923 * Finally, enable interrupts
925 /* info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI; */
926 info->IER = UART_IER_RLSI | UART_IER_RDI | UART_IER_DMA_TMOUT |
927 UART_IER_DMA_TC;
928 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
929 serial_out(info, UART_ESI_CMD2, info->IER);
931 if (info->tty)
932 clear_bit(TTY_IO_ERROR, &info->tty->flags);
933 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
934 spin_unlock_irqrestore(&info->lock, flags);
937 * Set up the tty->alt_speed kludge
939 if (info->tty) {
940 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
941 info->tty->alt_speed = 57600;
942 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
943 info->tty->alt_speed = 115200;
944 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
945 info->tty->alt_speed = 230400;
946 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
947 info->tty->alt_speed = 460800;
951 * set the speed of the serial port
953 change_speed(info);
954 info->flags |= ASYNC_INITIALIZED;
955 return 0;
957 out:
958 spin_unlock_irqrestore(&info->lock, flags);
959 out_unlocked:
960 return retval;
964 * This routine will shutdown a serial port; interrupts are disabled, and
965 * DTR is dropped if the hangup on close termio flag is on.
967 static void shutdown(struct esp_struct * info)
969 unsigned long flags, f;
971 if (!(info->flags & ASYNC_INITIALIZED))
972 return;
974 #ifdef SERIAL_DEBUG_OPEN
975 printk("Shutting down serial port %d (irq %d)....", info->line,
976 info->irq);
977 #endif
979 spin_lock_irqsave(&info->lock, flags);
981 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
982 * here so the queue might never be waken up
984 wake_up_interruptible(&info->delta_msr_wait);
985 wake_up_interruptible(&info->break_wait);
987 /* stop a DMA transfer on the port being closed */
988 /* DMA lock is higher priority always */
989 if (info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) {
990 f=claim_dma_lock();
991 disable_dma(dma);
992 clear_dma_ff(dma);
993 release_dma_lock(f);
995 dma_bytes = 0;
999 * Free the IRQ
1001 free_irq(info->irq, info);
1003 if (dma_buffer) {
1004 struct esp_struct *current_port = ports;
1006 while (current_port) {
1007 if ((current_port != info) &&
1008 (current_port->flags & ASYNC_INITIALIZED))
1009 break;
1011 current_port = current_port->next_port;
1014 if (!current_port) {
1015 free_dma(dma);
1016 free_pages((unsigned long)dma_buffer,
1017 get_order(DMA_BUFFER_SZ));
1018 dma_buffer = NULL;
1022 if (info->xmit_buf) {
1023 free_page((unsigned long) info->xmit_buf);
1024 info->xmit_buf = NULL;
1027 info->IER = 0;
1028 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1029 serial_out(info, UART_ESI_CMD2, 0x00);
1031 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
1032 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1034 info->MCR &= ~UART_MCR_OUT2;
1035 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1036 serial_out(info, UART_ESI_CMD2, UART_MCR);
1037 serial_out(info, UART_ESI_CMD2, info->MCR);
1039 if (info->tty)
1040 set_bit(TTY_IO_ERROR, &info->tty->flags);
1042 info->flags &= ~ASYNC_INITIALIZED;
1043 spin_unlock_irqrestore(&info->lock, flags);
1047 * This routine is called to set the UART divisor registers to match
1048 * the specified baud rate for a serial port.
1050 static void change_speed(struct esp_struct *info)
1052 unsigned short port;
1053 int quot = 0;
1054 unsigned cflag,cval;
1055 int baud, bits;
1056 unsigned char flow1 = 0, flow2 = 0;
1057 unsigned long flags;
1059 if (!info->tty || !info->tty->termios)
1060 return;
1061 cflag = info->tty->termios->c_cflag;
1062 port = info->port;
1064 /* byte size and parity */
1065 switch (cflag & CSIZE) {
1066 case CS5: cval = 0x00; bits = 7; break;
1067 case CS6: cval = 0x01; bits = 8; break;
1068 case CS7: cval = 0x02; bits = 9; break;
1069 case CS8: cval = 0x03; bits = 10; break;
1070 default: cval = 0x00; bits = 7; break;
1072 if (cflag & CSTOPB) {
1073 cval |= 0x04;
1074 bits++;
1076 if (cflag & PARENB) {
1077 cval |= UART_LCR_PARITY;
1078 bits++;
1080 if (!(cflag & PARODD))
1081 cval |= UART_LCR_EPAR;
1082 #ifdef CMSPAR
1083 if (cflag & CMSPAR)
1084 cval |= UART_LCR_SPAR;
1085 #endif
1087 baud = tty_get_baud_rate(info->tty);
1088 if (baud == 38400 &&
1089 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1090 quot = info->custom_divisor;
1091 else {
1092 if (baud == 134)
1093 /* Special case since 134 is really 134.5 */
1094 quot = (2*BASE_BAUD / 269);
1095 else if (baud)
1096 quot = BASE_BAUD / baud;
1098 /* If the quotient is ever zero, default to 9600 bps */
1099 if (!quot)
1100 quot = BASE_BAUD / 9600;
1102 info->timeout = ((1024 * HZ * bits * quot) / BASE_BAUD) + (HZ / 50);
1104 /* CTS flow control flag and modem status interrupts */
1105 /* info->IER &= ~UART_IER_MSI; */
1106 if (cflag & CRTSCTS) {
1107 info->flags |= ASYNC_CTS_FLOW;
1108 /* info->IER |= UART_IER_MSI; */
1109 flow1 = 0x04;
1110 flow2 = 0x10;
1111 } else
1112 info->flags &= ~ASYNC_CTS_FLOW;
1113 if (cflag & CLOCAL)
1114 info->flags &= ~ASYNC_CHECK_CD;
1115 else {
1116 info->flags |= ASYNC_CHECK_CD;
1117 /* info->IER |= UART_IER_MSI; */
1121 * Set up parity check flag
1123 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1125 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1126 if (I_INPCK(info->tty))
1127 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1128 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1129 info->read_status_mask |= UART_LSR_BI;
1131 info->ignore_status_mask = 0;
1132 #if 0
1133 /* This should be safe, but for some broken bits of hardware... */
1134 if (I_IGNPAR(info->tty)) {
1135 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1136 info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
1138 #endif
1139 if (I_IGNBRK(info->tty)) {
1140 info->ignore_status_mask |= UART_LSR_BI;
1141 info->read_status_mask |= UART_LSR_BI;
1143 * If we're ignore parity and break indicators, ignore
1144 * overruns too. (For real raw support).
1146 if (I_IGNPAR(info->tty)) {
1147 info->ignore_status_mask |= UART_LSR_OE | \
1148 UART_LSR_PE | UART_LSR_FE;
1149 info->read_status_mask |= UART_LSR_OE | \
1150 UART_LSR_PE | UART_LSR_FE;
1154 if (I_IXOFF(info->tty))
1155 flow1 |= 0x81;
1157 spin_lock_irqsave(&info->lock, flags);
1158 /* set baud */
1159 serial_out(info, UART_ESI_CMD1, ESI_SET_BAUD);
1160 serial_out(info, UART_ESI_CMD2, quot >> 8);
1161 serial_out(info, UART_ESI_CMD2, quot & 0xff);
1163 /* set data bits, parity, etc. */
1164 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1165 serial_out(info, UART_ESI_CMD2, UART_LCR);
1166 serial_out(info, UART_ESI_CMD2, cval);
1168 /* Enable flow control */
1169 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CNTL);
1170 serial_out(info, UART_ESI_CMD2, flow1);
1171 serial_out(info, UART_ESI_CMD2, flow2);
1173 /* set flow control characters (XON/XOFF only) */
1174 if (I_IXOFF(info->tty)) {
1175 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CHARS);
1176 serial_out(info, UART_ESI_CMD2, START_CHAR(info->tty));
1177 serial_out(info, UART_ESI_CMD2, STOP_CHAR(info->tty));
1178 serial_out(info, UART_ESI_CMD2, 0x10);
1179 serial_out(info, UART_ESI_CMD2, 0x21);
1180 switch (cflag & CSIZE) {
1181 case CS5:
1182 serial_out(info, UART_ESI_CMD2, 0x1f);
1183 break;
1184 case CS6:
1185 serial_out(info, UART_ESI_CMD2, 0x3f);
1186 break;
1187 case CS7:
1188 case CS8:
1189 serial_out(info, UART_ESI_CMD2, 0x7f);
1190 break;
1191 default:
1192 serial_out(info, UART_ESI_CMD2, 0xff);
1193 break;
1197 /* Set high/low water */
1198 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1199 serial_out(info, UART_ESI_CMD2, info->config.flow_off >> 8);
1200 serial_out(info, UART_ESI_CMD2, info->config.flow_off);
1201 serial_out(info, UART_ESI_CMD2, info->config.flow_on >> 8);
1202 serial_out(info, UART_ESI_CMD2, info->config.flow_on);
1204 spin_unlock_irqrestore(&info->lock, flags);
1207 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
1209 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1210 unsigned long flags;
1212 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
1213 return;
1215 if (!tty || !info->xmit_buf)
1216 return;
1218 spin_lock_irqsave(&info->lock, flags);
1219 if (info->xmit_cnt < ESP_XMIT_SIZE - 1) {
1220 info->xmit_buf[info->xmit_head++] = ch;
1221 info->xmit_head &= ESP_XMIT_SIZE-1;
1222 info->xmit_cnt++;
1224 spin_unlock_irqrestore(&info->lock, flags);
1227 static void rs_flush_chars(struct tty_struct *tty)
1229 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1230 unsigned long flags;
1232 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
1233 return;
1235 spin_lock_irqsave(&info->lock, flags);
1237 if (info->xmit_cnt <= 0 || tty->stopped || !info->xmit_buf)
1238 goto out;
1240 if (!(info->IER & UART_IER_THRI)) {
1241 info->IER |= UART_IER_THRI;
1242 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1243 serial_out(info, UART_ESI_CMD2, info->IER);
1245 out:
1246 spin_unlock_irqrestore(&info->lock, flags);
1249 static int rs_write(struct tty_struct * tty,
1250 const unsigned char *buf, int count)
1252 int c, t, ret = 0;
1253 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1254 unsigned long flags;
1256 if (serial_paranoia_check(info, tty->name, "rs_write"))
1257 return 0;
1259 if (!tty || !info->xmit_buf)
1260 return 0;
1262 while (1) {
1263 /* Thanks to R. Wolff for suggesting how to do this with */
1264 /* interrupts enabled */
1266 c = count;
1267 t = ESP_XMIT_SIZE - info->xmit_cnt - 1;
1269 if (t < c)
1270 c = t;
1272 t = ESP_XMIT_SIZE - info->xmit_head;
1274 if (t < c)
1275 c = t;
1277 if (c <= 0)
1278 break;
1280 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1282 info->xmit_head = (info->xmit_head + c) & (ESP_XMIT_SIZE-1);
1283 info->xmit_cnt += c;
1284 buf += c;
1285 count -= c;
1286 ret += c;
1289 spin_lock_irqsave(&info->lock, flags);
1291 if (info->xmit_cnt && !tty->stopped && !(info->IER & UART_IER_THRI)) {
1292 info->IER |= UART_IER_THRI;
1293 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1294 serial_out(info, UART_ESI_CMD2, info->IER);
1297 spin_unlock_irqrestore(&info->lock, flags);
1298 return ret;
1301 static int rs_write_room(struct tty_struct *tty)
1303 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1304 int ret;
1305 unsigned long flags;
1307 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
1308 return 0;
1310 spin_lock_irqsave(&info->lock, flags);
1312 ret = ESP_XMIT_SIZE - info->xmit_cnt - 1;
1313 if (ret < 0)
1314 ret = 0;
1315 spin_unlock_irqrestore(&info->lock, flags);
1316 return ret;
1319 static int rs_chars_in_buffer(struct tty_struct *tty)
1321 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1323 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
1324 return 0;
1325 return info->xmit_cnt;
1328 static void rs_flush_buffer(struct tty_struct *tty)
1330 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1331 unsigned long flags;
1333 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
1334 return;
1335 spin_lock_irqsave(&info->lock, flags);
1336 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1337 spin_unlock_irqrestore(&info->lock, flags);
1338 tty_wakeup(tty);
1342 * ------------------------------------------------------------
1343 * rs_throttle()
1345 * This routine is called by the upper-layer tty layer to signal that
1346 * incoming characters should be throttled.
1347 * ------------------------------------------------------------
1349 static void rs_throttle(struct tty_struct * tty)
1351 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1352 unsigned long flags;
1353 #ifdef SERIAL_DEBUG_THROTTLE
1354 char buf[64];
1356 printk("throttle %s: %d....\n", tty_name(tty, buf),
1357 tty->ldisc.chars_in_buffer(tty));
1358 #endif
1360 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
1361 return;
1363 spin_lock_irqsave(&info->lock, flags);
1364 info->IER &= ~UART_IER_RDI;
1365 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1366 serial_out(info, UART_ESI_CMD2, info->IER);
1367 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1368 serial_out(info, UART_ESI_CMD2, 0x00);
1369 spin_unlock_irqrestore(&info->lock, flags);
1372 static void rs_unthrottle(struct tty_struct * tty)
1374 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1375 unsigned long flags;
1376 #ifdef SERIAL_DEBUG_THROTTLE
1377 char buf[64];
1379 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1380 tty->ldisc.chars_in_buffer(tty));
1381 #endif
1383 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1384 return;
1386 spin_lock_irqsave(&info->lock, flags);
1387 info->IER |= UART_IER_RDI;
1388 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1389 serial_out(info, UART_ESI_CMD2, info->IER);
1390 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1391 serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
1392 spin_unlock_irqrestore(&info->lock, flags);
1396 * ------------------------------------------------------------
1397 * rs_ioctl() and friends
1398 * ------------------------------------------------------------
1401 static int get_serial_info(struct esp_struct * info,
1402 struct serial_struct __user *retinfo)
1404 struct serial_struct tmp;
1406 memset(&tmp, 0, sizeof(tmp));
1407 tmp.type = PORT_16550A;
1408 tmp.line = info->line;
1409 tmp.port = info->port;
1410 tmp.irq = info->irq;
1411 tmp.flags = info->flags;
1412 tmp.xmit_fifo_size = 1024;
1413 tmp.baud_base = BASE_BAUD;
1414 tmp.close_delay = info->close_delay;
1415 tmp.closing_wait = info->closing_wait;
1416 tmp.custom_divisor = info->custom_divisor;
1417 tmp.hub6 = 0;
1418 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1419 return -EFAULT;
1420 return 0;
1423 static int get_esp_config(struct esp_struct * info,
1424 struct hayes_esp_config __user *retinfo)
1426 struct hayes_esp_config tmp;
1428 if (!retinfo)
1429 return -EFAULT;
1431 memset(&tmp, 0, sizeof(tmp));
1432 tmp.rx_timeout = info->config.rx_timeout;
1433 tmp.rx_trigger = info->config.rx_trigger;
1434 tmp.tx_trigger = info->config.tx_trigger;
1435 tmp.flow_off = info->config.flow_off;
1436 tmp.flow_on = info->config.flow_on;
1437 tmp.pio_threshold = info->config.pio_threshold;
1438 tmp.dma_channel = (info->stat_flags & ESP_STAT_NEVER_DMA ? 0 : dma);
1440 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
1443 static int set_serial_info(struct esp_struct * info,
1444 struct serial_struct __user *new_info)
1446 struct serial_struct new_serial;
1447 struct esp_struct old_info;
1448 unsigned int change_irq;
1449 int retval = 0;
1450 struct esp_struct *current_async;
1452 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1453 return -EFAULT;
1454 old_info = *info;
1456 if ((new_serial.type != PORT_16550A) ||
1457 (new_serial.hub6) ||
1458 (info->port != new_serial.port) ||
1459 (new_serial.baud_base != BASE_BAUD) ||
1460 (new_serial.irq > 15) ||
1461 (new_serial.irq < 2) ||
1462 (new_serial.irq == 6) ||
1463 (new_serial.irq == 8) ||
1464 (new_serial.irq == 13))
1465 return -EINVAL;
1467 change_irq = new_serial.irq != info->irq;
1469 if (change_irq && (info->line % 8))
1470 return -EINVAL;
1472 if (!capable(CAP_SYS_ADMIN)) {
1473 if (change_irq ||
1474 (new_serial.close_delay != info->close_delay) ||
1475 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1476 (info->flags & ~ASYNC_USR_MASK)))
1477 return -EPERM;
1478 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1479 (new_serial.flags & ASYNC_USR_MASK));
1480 info->custom_divisor = new_serial.custom_divisor;
1481 } else {
1482 if (new_serial.irq == 2)
1483 new_serial.irq = 9;
1485 if (change_irq) {
1486 current_async = ports;
1488 while (current_async) {
1489 if ((current_async->line >= info->line) &&
1490 (current_async->line < (info->line + 8))) {
1491 if (current_async == info) {
1492 if (current_async->count > 1)
1493 return -EBUSY;
1494 } else if (current_async->count)
1495 return -EBUSY;
1498 current_async = current_async->next_port;
1503 * OK, past this point, all the error checking has been done.
1504 * At this point, we start making changes.....
1507 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1508 (new_serial.flags & ASYNC_FLAGS));
1509 info->custom_divisor = new_serial.custom_divisor;
1510 info->close_delay = new_serial.close_delay * HZ/100;
1511 info->closing_wait = new_serial.closing_wait * HZ/100;
1513 if (change_irq) {
1515 * We need to shutdown the serial port at the old
1516 * port/irq combination.
1518 shutdown(info);
1520 current_async = ports;
1522 while (current_async) {
1523 if ((current_async->line >= info->line) &&
1524 (current_async->line < (info->line + 8)))
1525 current_async->irq = new_serial.irq;
1527 current_async = current_async->next_port;
1530 serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
1531 if (info->irq == 9)
1532 serial_out(info, UART_ESI_CMD2, 0x02);
1533 else
1534 serial_out(info, UART_ESI_CMD2, info->irq);
1538 if (info->flags & ASYNC_INITIALIZED) {
1539 if (((old_info.flags & ASYNC_SPD_MASK) !=
1540 (info->flags & ASYNC_SPD_MASK)) ||
1541 (old_info.custom_divisor != info->custom_divisor)) {
1542 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1543 info->tty->alt_speed = 57600;
1544 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1545 info->tty->alt_speed = 115200;
1546 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1547 info->tty->alt_speed = 230400;
1548 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1549 info->tty->alt_speed = 460800;
1550 change_speed(info);
1552 } else
1553 retval = startup(info);
1555 return retval;
1558 static int set_esp_config(struct esp_struct * info,
1559 struct hayes_esp_config __user * new_info)
1561 struct hayes_esp_config new_config;
1562 unsigned int change_dma;
1563 int retval = 0;
1564 struct esp_struct *current_async;
1565 unsigned long flags;
1567 /* Perhaps a non-sysadmin user should be able to do some of these */
1568 /* operations. I haven't decided yet. */
1570 if (!capable(CAP_SYS_ADMIN))
1571 return -EPERM;
1573 if (copy_from_user(&new_config, new_info, sizeof(new_config)))
1574 return -EFAULT;
1576 if ((new_config.flow_on >= new_config.flow_off) ||
1577 (new_config.rx_trigger < 1) ||
1578 (new_config.tx_trigger < 1) ||
1579 (new_config.flow_off < 1) ||
1580 (new_config.flow_on < 1) ||
1581 (new_config.rx_trigger > 1023) ||
1582 (new_config.tx_trigger > 1023) ||
1583 (new_config.flow_off > 1023) ||
1584 (new_config.flow_on > 1023) ||
1585 (new_config.pio_threshold < 0) ||
1586 (new_config.pio_threshold > 1024))
1587 return -EINVAL;
1589 if ((new_config.dma_channel != 1) && (new_config.dma_channel != 3))
1590 new_config.dma_channel = 0;
1592 if (info->stat_flags & ESP_STAT_NEVER_DMA)
1593 change_dma = new_config.dma_channel;
1594 else
1595 change_dma = (new_config.dma_channel != dma);
1597 if (change_dma) {
1598 if (new_config.dma_channel) {
1599 /* PIO mode to DMA mode transition OR */
1600 /* change current DMA channel */
1602 current_async = ports;
1604 while (current_async) {
1605 if (current_async == info) {
1606 if (current_async->count > 1)
1607 return -EBUSY;
1608 } else if (current_async->count)
1609 return -EBUSY;
1611 current_async =
1612 current_async->next_port;
1615 shutdown(info);
1616 dma = new_config.dma_channel;
1617 info->stat_flags &= ~ESP_STAT_NEVER_DMA;
1619 /* all ports must use the same DMA channel */
1621 spin_lock_irqsave(&info->lock, flags);
1622 current_async = ports;
1624 while (current_async) {
1625 esp_basic_init(current_async);
1626 current_async = current_async->next_port;
1628 spin_unlock_irqrestore(&info->lock, flags);
1629 } else {
1630 /* DMA mode to PIO mode only */
1632 if (info->count > 1)
1633 return -EBUSY;
1635 shutdown(info);
1636 spin_lock_irqsave(&info->lock, flags);
1637 info->stat_flags |= ESP_STAT_NEVER_DMA;
1638 esp_basic_init(info);
1639 spin_unlock_irqrestore(&info->lock, flags);
1643 info->config.pio_threshold = new_config.pio_threshold;
1645 if ((new_config.flow_off != info->config.flow_off) ||
1646 (new_config.flow_on != info->config.flow_on)) {
1647 unsigned long flags;
1649 info->config.flow_off = new_config.flow_off;
1650 info->config.flow_on = new_config.flow_on;
1652 spin_lock_irqsave(&info->lock, flags);
1653 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1654 serial_out(info, UART_ESI_CMD2, new_config.flow_off >> 8);
1655 serial_out(info, UART_ESI_CMD2, new_config.flow_off);
1656 serial_out(info, UART_ESI_CMD2, new_config.flow_on >> 8);
1657 serial_out(info, UART_ESI_CMD2, new_config.flow_on);
1658 spin_unlock_irqrestore(&info->lock, flags);
1661 if ((new_config.rx_trigger != info->config.rx_trigger) ||
1662 (new_config.tx_trigger != info->config.tx_trigger)) {
1663 unsigned long flags;
1665 info->config.rx_trigger = new_config.rx_trigger;
1666 info->config.tx_trigger = new_config.tx_trigger;
1667 spin_lock_irqsave(&info->lock, flags);
1668 serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
1669 serial_out(info, UART_ESI_CMD2,
1670 new_config.rx_trigger >> 8);
1671 serial_out(info, UART_ESI_CMD2, new_config.rx_trigger);
1672 serial_out(info, UART_ESI_CMD2,
1673 new_config.tx_trigger >> 8);
1674 serial_out(info, UART_ESI_CMD2, new_config.tx_trigger);
1675 spin_unlock_irqrestore(&info->lock, flags);
1678 if (new_config.rx_timeout != info->config.rx_timeout) {
1679 unsigned long flags;
1681 info->config.rx_timeout = new_config.rx_timeout;
1682 spin_lock_irqsave(&info->lock, flags);
1684 if (info->IER & UART_IER_RDI) {
1685 serial_out(info, UART_ESI_CMD1,
1686 ESI_SET_RX_TIMEOUT);
1687 serial_out(info, UART_ESI_CMD2,
1688 new_config.rx_timeout);
1691 spin_unlock_irqrestore(&info->lock, flags);
1694 if (!(info->flags & ASYNC_INITIALIZED))
1695 retval = startup(info);
1697 return retval;
1701 * get_lsr_info - get line status register info
1703 * Purpose: Let user call ioctl() to get info when the UART physically
1704 * is emptied. On bus types like RS485, the transmitter must
1705 * release the bus after transmitting. This must be done when
1706 * the transmit shift register is empty, not be done when the
1707 * transmit holding register is empty. This functionality
1708 * allows an RS485 driver to be written in user space.
1710 static int get_lsr_info(struct esp_struct * info, unsigned int __user *value)
1712 unsigned char status;
1713 unsigned int result;
1714 unsigned long flags;
1716 spin_lock_irqsave(&info->lock, flags);
1717 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1718 status = serial_in(info, UART_ESI_STAT1);
1719 spin_unlock_irqrestore(&info->lock, flags);
1720 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1721 return put_user(result,value);
1725 static int esp_tiocmget(struct tty_struct *tty, struct file *file)
1727 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1728 unsigned char control, status;
1729 unsigned long flags;
1731 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1732 return -ENODEV;
1733 if (tty->flags & (1 << TTY_IO_ERROR))
1734 return -EIO;
1736 control = info->MCR;
1738 spin_lock_irqsave(&info->lock, flags);
1739 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1740 status = serial_in(info, UART_ESI_STAT2);
1741 spin_unlock_irqrestore(&info->lock, flags);
1743 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1744 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1745 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1746 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1747 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1748 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1751 static int esp_tiocmset(struct tty_struct *tty, struct file *file,
1752 unsigned int set, unsigned int clear)
1754 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1755 unsigned long flags;
1757 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1758 return -ENODEV;
1759 if (tty->flags & (1 << TTY_IO_ERROR))
1760 return -EIO;
1762 spin_lock_irqsave(&info->lock, flags);
1764 if (set & TIOCM_RTS)
1765 info->MCR |= UART_MCR_RTS;
1766 if (set & TIOCM_DTR)
1767 info->MCR |= UART_MCR_DTR;
1769 if (clear & TIOCM_RTS)
1770 info->MCR &= ~UART_MCR_RTS;
1771 if (clear & TIOCM_DTR)
1772 info->MCR &= ~UART_MCR_DTR;
1774 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1775 serial_out(info, UART_ESI_CMD2, UART_MCR);
1776 serial_out(info, UART_ESI_CMD2, info->MCR);
1778 spin_unlock_irqrestore(&info->lock, flags);
1779 return 0;
1783 * rs_break() --- routine which turns the break handling on or off
1785 static void esp_break(struct tty_struct *tty, int break_state)
1787 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1788 unsigned long flags;
1790 if (serial_paranoia_check(info, tty->name, "esp_break"))
1791 return;
1793 if (break_state == -1) {
1794 spin_lock_irqsave(&info->lock, flags);
1795 serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1796 serial_out(info, UART_ESI_CMD2, 0x01);
1797 spin_unlock_irqrestore(&info->lock, flags);
1799 /* FIXME - new style wait needed here */
1800 interruptible_sleep_on(&info->break_wait);
1801 } else {
1802 spin_lock_irqsave(&info->lock, flags);
1803 serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1804 serial_out(info, UART_ESI_CMD2, 0x00);
1805 spin_unlock_irqrestore(&info->lock, flags);
1809 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1810 unsigned int cmd, unsigned long arg)
1812 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1813 struct async_icount cprev, cnow; /* kernel counter temps */
1814 struct serial_icounter_struct __user *p_cuser; /* user space */
1815 void __user *argp = (void __user *)arg;
1816 unsigned long flags;
1818 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1819 return -ENODEV;
1821 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1822 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1823 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT) &&
1824 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT) &&
1825 (cmd != TIOCGHAYESESP) && (cmd != TIOCSHAYESESP)) {
1826 if (tty->flags & (1 << TTY_IO_ERROR))
1827 return -EIO;
1830 switch (cmd) {
1831 case TIOCGSERIAL:
1832 return get_serial_info(info, argp);
1833 case TIOCSSERIAL:
1834 return set_serial_info(info, argp);
1835 case TIOCSERCONFIG:
1836 /* do not reconfigure after initial configuration */
1837 return 0;
1839 case TIOCSERGWILD:
1840 return put_user(0L, (unsigned long __user *)argp);
1842 case TIOCSERGETLSR: /* Get line status register */
1843 return get_lsr_info(info, argp);
1845 case TIOCSERSWILD:
1846 if (!capable(CAP_SYS_ADMIN))
1847 return -EPERM;
1848 return 0;
1851 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1852 * - mask passed in arg for lines of interest
1853 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1854 * Caller should use TIOCGICOUNT to see which one it was
1856 case TIOCMIWAIT:
1857 spin_lock_irqsave(&info->lock, flags);
1858 cprev = info->icount; /* note the counters on entry */
1859 spin_unlock_irqrestore(&info->lock, flags);
1860 while (1) {
1861 /* FIXME: convert to new style wakeup */
1862 interruptible_sleep_on(&info->delta_msr_wait);
1863 /* see if a signal did it */
1864 if (signal_pending(current))
1865 return -ERESTARTSYS;
1866 spin_lock_irqsave(&info->lock, flags);
1867 cnow = info->icount; /* atomic copy */
1868 spin_unlock_irqrestore(&info->lock, flags);
1869 if (cnow.rng == cprev.rng &&
1870 cnow.dsr == cprev.dsr &&
1871 cnow.dcd == cprev.dcd &&
1872 cnow.cts == cprev.cts)
1873 return -EIO; /* no change => error */
1874 if (((arg & TIOCM_RNG) &&
1875 (cnow.rng != cprev.rng)) ||
1876 ((arg & TIOCM_DSR) &&
1877 (cnow.dsr != cprev.dsr)) ||
1878 ((arg & TIOCM_CD) &&
1879 (cnow.dcd != cprev.dcd)) ||
1880 ((arg & TIOCM_CTS) &&
1881 (cnow.cts != cprev.cts)) ) {
1882 return 0;
1884 cprev = cnow;
1886 /* NOTREACHED */
1889 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1890 * Return: write counters to the user passed counter struct
1891 * NB: both 1->0 and 0->1 transitions are counted except for
1892 * RI where only 0->1 is counted.
1894 case TIOCGICOUNT:
1895 spin_lock_irqsave(&info->lock, flags);
1896 cnow = info->icount;
1897 spin_unlock_irqrestore(&info->lock, flags);
1898 p_cuser = argp;
1899 if (put_user(cnow.cts, &p_cuser->cts) ||
1900 put_user(cnow.dsr, &p_cuser->dsr) ||
1901 put_user(cnow.rng, &p_cuser->rng) ||
1902 put_user(cnow.dcd, &p_cuser->dcd))
1903 return -EFAULT;
1905 return 0;
1906 case TIOCGHAYESESP:
1907 return get_esp_config(info, argp);
1908 case TIOCSHAYESESP:
1909 return set_esp_config(info, argp);
1911 default:
1912 return -ENOIOCTLCMD;
1914 return 0;
1917 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1919 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1920 unsigned long flags;
1922 if ( (tty->termios->c_cflag == old_termios->c_cflag)
1923 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
1924 == RELEVANT_IFLAG(old_termios->c_iflag)))
1925 return;
1927 change_speed(info);
1929 spin_lock_irqsave(&info->lock, flags);
1931 /* Handle transition to B0 status */
1932 if ((old_termios->c_cflag & CBAUD) &&
1933 !(tty->termios->c_cflag & CBAUD)) {
1934 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1935 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1936 serial_out(info, UART_ESI_CMD2, UART_MCR);
1937 serial_out(info, UART_ESI_CMD2, info->MCR);
1940 /* Handle transition away from B0 status */
1941 if (!(old_termios->c_cflag & CBAUD) &&
1942 (tty->termios->c_cflag & CBAUD)) {
1943 info->MCR |= (UART_MCR_DTR | UART_MCR_RTS);
1944 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1945 serial_out(info, UART_ESI_CMD2, UART_MCR);
1946 serial_out(info, UART_ESI_CMD2, info->MCR);
1949 spin_unlock_irqrestore(&info->lock, flags);
1951 /* Handle turning of CRTSCTS */
1952 if ((old_termios->c_cflag & CRTSCTS) &&
1953 !(tty->termios->c_cflag & CRTSCTS)) {
1954 rs_start(tty);
1959 * ------------------------------------------------------------
1960 * rs_close()
1962 * This routine is called when the serial port gets closed. First, we
1963 * wait for the last remaining data to be sent. Then, we unlink its
1964 * async structure from the interrupt chain if necessary, and we free
1965 * that IRQ if nothing is left in the chain.
1966 * ------------------------------------------------------------
1968 static void rs_close(struct tty_struct *tty, struct file * filp)
1970 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1971 unsigned long flags;
1973 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1974 return;
1976 spin_lock_irqsave(&info->lock, flags);
1978 if (tty_hung_up_p(filp)) {
1979 DBG_CNT("before DEC-hung");
1980 goto out;
1983 #ifdef SERIAL_DEBUG_OPEN
1984 printk("rs_close ttys%d, count = %d\n", info->line, info->count);
1985 #endif
1986 if ((tty->count == 1) && (info->count != 1)) {
1988 * Uh, oh. tty->count is 1, which means that the tty
1989 * structure will be freed. Info->count should always
1990 * be one in these conditions. If it's greater than
1991 * one, we've got real problems, since it means the
1992 * serial port won't be shutdown.
1994 printk("rs_close: bad serial port count; tty->count is 1, "
1995 "info->count is %d\n", info->count);
1996 info->count = 1;
1998 if (--info->count < 0) {
1999 printk("rs_close: bad serial port count for ttys%d: %d\n",
2000 info->line, info->count);
2001 info->count = 0;
2003 if (info->count) {
2004 DBG_CNT("before DEC-2");
2005 goto out;
2007 info->flags |= ASYNC_CLOSING;
2009 spin_unlock_irqrestore(&info->lock, flags);
2011 * Now we wait for the transmit buffer to clear; and we notify
2012 * the line discipline to only process XON/XOFF characters.
2014 tty->closing = 1;
2015 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
2016 tty_wait_until_sent(tty, info->closing_wait);
2018 * At this point we stop accepting input. To do this, we
2019 * disable the receive line status interrupts, and tell the
2020 * interrupt driver to stop checking the data ready bit in the
2021 * line status register.
2023 /* info->IER &= ~UART_IER_RLSI; */
2024 info->IER &= ~UART_IER_RDI;
2025 info->read_status_mask &= ~UART_LSR_DR;
2026 if (info->flags & ASYNC_INITIALIZED) {
2028 spin_lock_irqsave(&info->lock, flags);
2029 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
2030 serial_out(info, UART_ESI_CMD2, info->IER);
2032 /* disable receive timeout */
2033 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
2034 serial_out(info, UART_ESI_CMD2, 0x00);
2036 spin_unlock_irqrestore(&info->lock, flags);
2039 * Before we drop DTR, make sure the UART transmitter
2040 * has completely drained; this is especially
2041 * important if there is a transmit FIFO!
2043 rs_wait_until_sent(tty, info->timeout);
2045 shutdown(info);
2046 if (tty->driver->flush_buffer)
2047 tty->driver->flush_buffer(tty);
2048 tty_ldisc_flush(tty);
2049 tty->closing = 0;
2050 info->event = 0;
2051 info->tty = NULL;
2053 if (info->blocked_open) {
2054 if (info->close_delay) {
2055 msleep_interruptible(jiffies_to_msecs(info->close_delay));
2057 wake_up_interruptible(&info->open_wait);
2059 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
2060 wake_up_interruptible(&info->close_wait);
2061 return;
2063 out:
2064 spin_unlock_irqrestore(&info->lock, flags);
2067 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
2069 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
2070 unsigned long orig_jiffies, char_time;
2071 unsigned long flags;
2073 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
2074 return;
2076 orig_jiffies = jiffies;
2077 char_time = ((info->timeout - HZ / 50) / 1024) / 5;
2079 if (!char_time)
2080 char_time = 1;
2082 spin_lock_irqsave(&info->lock, flags);
2083 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2084 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2086 while ((serial_in(info, UART_ESI_STAT1) != 0x03) ||
2087 (serial_in(info, UART_ESI_STAT2) != 0xff)) {
2089 spin_unlock_irqrestore(&info->lock, flags);
2090 msleep_interruptible(jiffies_to_msecs(char_time));
2092 if (signal_pending(current))
2093 break;
2095 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2096 break;
2098 spin_lock_irqsave(&info->lock, flags);
2099 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2100 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2102 spin_unlock_irqrestore(&info->lock, flags);
2103 set_current_state(TASK_RUNNING);
2107 * esp_hangup() --- called by tty_hangup() when a hangup is signaled.
2109 static void esp_hangup(struct tty_struct *tty)
2111 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
2113 if (serial_paranoia_check(info, tty->name, "esp_hangup"))
2114 return;
2116 rs_flush_buffer(tty);
2117 shutdown(info);
2118 info->event = 0;
2119 info->count = 0;
2120 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2121 info->tty = NULL;
2122 wake_up_interruptible(&info->open_wait);
2126 * ------------------------------------------------------------
2127 * esp_open() and friends
2128 * ------------------------------------------------------------
2130 static int block_til_ready(struct tty_struct *tty, struct file * filp,
2131 struct esp_struct *info)
2133 DECLARE_WAITQUEUE(wait, current);
2134 int retval;
2135 int do_clocal = 0;
2136 unsigned long flags;
2139 * If the device is in the middle of being closed, then block
2140 * until it's done, and then try again.
2142 if (tty_hung_up_p(filp) ||
2143 (info->flags & ASYNC_CLOSING)) {
2144 if (info->flags & ASYNC_CLOSING)
2145 interruptible_sleep_on(&info->close_wait);
2146 #ifdef SERIAL_DO_RESTART
2147 if (info->flags & ASYNC_HUP_NOTIFY)
2148 return -EAGAIN;
2149 else
2150 return -ERESTARTSYS;
2151 #else
2152 return -EAGAIN;
2153 #endif
2157 * If non-blocking mode is set, or the port is not enabled,
2158 * then make the check up front and then exit.
2160 if ((filp->f_flags & O_NONBLOCK) ||
2161 (tty->flags & (1 << TTY_IO_ERROR))) {
2162 info->flags |= ASYNC_NORMAL_ACTIVE;
2163 return 0;
2166 if (tty->termios->c_cflag & CLOCAL)
2167 do_clocal = 1;
2170 * Block waiting for the carrier detect and the line to become
2171 * free (i.e., not in use by the callout). While we are in
2172 * this loop, info->count is dropped by one, so that
2173 * rs_close() knows when to free things. We restore it upon
2174 * exit, either normal or abnormal.
2176 retval = 0;
2177 add_wait_queue(&info->open_wait, &wait);
2178 #ifdef SERIAL_DEBUG_OPEN
2179 printk("block_til_ready before block: ttys%d, count = %d\n",
2180 info->line, info->count);
2181 #endif
2182 spin_lock_irqsave(&info->lock, flags);
2183 if (!tty_hung_up_p(filp))
2184 info->count--;
2185 info->blocked_open++;
2186 while (1) {
2187 if ((tty->termios->c_cflag & CBAUD)) {
2188 unsigned int scratch;
2190 serial_out(info, UART_ESI_CMD1, ESI_READ_UART);
2191 serial_out(info, UART_ESI_CMD2, UART_MCR);
2192 scratch = serial_in(info, UART_ESI_STAT1);
2193 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2194 serial_out(info, UART_ESI_CMD2, UART_MCR);
2195 serial_out(info, UART_ESI_CMD2,
2196 scratch | UART_MCR_DTR | UART_MCR_RTS);
2198 set_current_state(TASK_INTERRUPTIBLE);
2199 if (tty_hung_up_p(filp) ||
2200 !(info->flags & ASYNC_INITIALIZED)) {
2201 #ifdef SERIAL_DO_RESTART
2202 if (info->flags & ASYNC_HUP_NOTIFY)
2203 retval = -EAGAIN;
2204 else
2205 retval = -ERESTARTSYS;
2206 #else
2207 retval = -EAGAIN;
2208 #endif
2209 break;
2212 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
2213 if (serial_in(info, UART_ESI_STAT2) & UART_MSR_DCD)
2214 do_clocal = 1;
2216 if (!(info->flags & ASYNC_CLOSING) &&
2217 (do_clocal))
2218 break;
2219 if (signal_pending(current)) {
2220 retval = -ERESTARTSYS;
2221 break;
2223 #ifdef SERIAL_DEBUG_OPEN
2224 printk("block_til_ready blocking: ttys%d, count = %d\n",
2225 info->line, info->count);
2226 #endif
2227 spin_unlock_irqrestore(&info->lock, flags);
2228 schedule();
2229 spin_lock_irqsave(&info->lock, flags);
2231 set_current_state(TASK_RUNNING);
2232 remove_wait_queue(&info->open_wait, &wait);
2233 if (!tty_hung_up_p(filp))
2234 info->count++;
2235 info->blocked_open--;
2236 spin_unlock_irqrestore(&info->lock, flags);
2237 #ifdef SERIAL_DEBUG_OPEN
2238 printk("block_til_ready after blocking: ttys%d, count = %d\n",
2239 info->line, info->count);
2240 #endif
2241 if (retval)
2242 return retval;
2243 info->flags |= ASYNC_NORMAL_ACTIVE;
2244 return 0;
2248 * This routine is called whenever a serial port is opened. It
2249 * enables interrupts for a serial port, linking in its async structure into
2250 * the IRQ chain. It also performs the serial-specific
2251 * initialization for the tty structure.
2253 static int esp_open(struct tty_struct *tty, struct file * filp)
2255 struct esp_struct *info;
2256 int retval, line;
2257 unsigned long flags;
2259 line = tty->index;
2260 if ((line < 0) || (line >= NR_PORTS))
2261 return -ENODEV;
2263 /* find the port in the chain */
2265 info = ports;
2267 while (info && (info->line != line))
2268 info = info->next_port;
2270 if (!info) {
2271 serial_paranoia_check(info, tty->name, "esp_open");
2272 return -ENODEV;
2275 #ifdef SERIAL_DEBUG_OPEN
2276 printk("esp_open %s, count = %d\n", tty->name, info->count);
2277 #endif
2278 spin_lock_irqsave(&info->lock, flags);
2279 info->count++;
2280 tty->driver_data = info;
2281 info->tty = tty;
2283 spin_unlock_irqrestore(&info->lock, flags);
2286 * Start up serial port
2288 retval = startup(info);
2289 if (retval)
2290 return retval;
2292 retval = block_til_ready(tty, filp, info);
2293 if (retval) {
2294 #ifdef SERIAL_DEBUG_OPEN
2295 printk("esp_open returning after block_til_ready with %d\n",
2296 retval);
2297 #endif
2298 return retval;
2301 #ifdef SERIAL_DEBUG_OPEN
2302 printk("esp_open %s successful...", tty->name);
2303 #endif
2304 return 0;
2308 * ---------------------------------------------------------------------
2309 * espserial_init() and friends
2311 * espserial_init() is called at boot-time to initialize the serial driver.
2312 * ---------------------------------------------------------------------
2316 * This routine prints out the appropriate serial driver version
2317 * number, and identifies which options were configured into this
2318 * driver.
2321 static inline void show_serial_version(void)
2323 printk(KERN_INFO "%s version %s (DMA %u)\n",
2324 serial_name, serial_version, dma);
2328 * This routine is called by espserial_init() to initialize a specific serial
2329 * port.
2331 static inline int autoconfig(struct esp_struct * info)
2333 int port_detected = 0;
2334 unsigned long flags;
2336 if (!request_region(info->port, REGION_SIZE, "esp serial"))
2337 return -EIO;
2339 spin_lock_irqsave(&info->lock, flags);
2341 * Check for ESP card
2344 if (serial_in(info, UART_ESI_BASE) == 0xf3) {
2345 serial_out(info, UART_ESI_CMD1, 0x00);
2346 serial_out(info, UART_ESI_CMD1, 0x01);
2348 if ((serial_in(info, UART_ESI_STAT2) & 0x70) == 0x20) {
2349 port_detected = 1;
2351 if (!(info->irq)) {
2352 serial_out(info, UART_ESI_CMD1, 0x02);
2354 if (serial_in(info, UART_ESI_STAT1) & 0x01)
2355 info->irq = 3;
2356 else
2357 info->irq = 4;
2361 /* put card in enhanced mode */
2362 /* this prevents access through */
2363 /* the "old" IO ports */
2364 esp_basic_init(info);
2366 /* clear out MCR */
2367 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2368 serial_out(info, UART_ESI_CMD2, UART_MCR);
2369 serial_out(info, UART_ESI_CMD2, 0x00);
2372 if (!port_detected)
2373 release_region(info->port, REGION_SIZE);
2375 spin_unlock_irqrestore(&info->lock, flags);
2376 return (port_detected);
2379 static struct tty_operations esp_ops = {
2380 .open = esp_open,
2381 .close = rs_close,
2382 .write = rs_write,
2383 .put_char = rs_put_char,
2384 .flush_chars = rs_flush_chars,
2385 .write_room = rs_write_room,
2386 .chars_in_buffer = rs_chars_in_buffer,
2387 .flush_buffer = rs_flush_buffer,
2388 .ioctl = rs_ioctl,
2389 .throttle = rs_throttle,
2390 .unthrottle = rs_unthrottle,
2391 .set_termios = rs_set_termios,
2392 .stop = rs_stop,
2393 .start = rs_start,
2394 .hangup = esp_hangup,
2395 .break_ctl = esp_break,
2396 .wait_until_sent = rs_wait_until_sent,
2397 .tiocmget = esp_tiocmget,
2398 .tiocmset = esp_tiocmset,
2402 * The serial driver boot-time initialization code!
2404 static int __init espserial_init(void)
2406 int i, offset;
2407 struct esp_struct * info;
2408 struct esp_struct *last_primary = NULL;
2409 int esp[] = {0x100,0x140,0x180,0x200,0x240,0x280,0x300,0x380};
2411 esp_driver = alloc_tty_driver(NR_PORTS);
2412 if (!esp_driver)
2413 return -ENOMEM;
2415 for (i = 0; i < NR_PRIMARY; i++) {
2416 if (irq[i] != 0) {
2417 if ((irq[i] < 2) || (irq[i] > 15) || (irq[i] == 6) ||
2418 (irq[i] == 8) || (irq[i] == 13))
2419 irq[i] = 0;
2420 else if (irq[i] == 2)
2421 irq[i] = 9;
2425 if ((dma != 1) && (dma != 3))
2426 dma = 0;
2428 if ((rx_trigger < 1) || (rx_trigger > 1023))
2429 rx_trigger = 768;
2431 if ((tx_trigger < 1) || (tx_trigger > 1023))
2432 tx_trigger = 768;
2434 if ((flow_off < 1) || (flow_off > 1023))
2435 flow_off = 1016;
2437 if ((flow_on < 1) || (flow_on > 1023))
2438 flow_on = 944;
2440 if ((rx_timeout < 0) || (rx_timeout > 255))
2441 rx_timeout = 128;
2443 if (flow_on >= flow_off)
2444 flow_on = flow_off - 1;
2446 show_serial_version();
2448 /* Initialize the tty_driver structure */
2450 esp_driver->owner = THIS_MODULE;
2451 esp_driver->name = "ttyP";
2452 esp_driver->devfs_name = "tts/P";
2453 esp_driver->major = ESP_IN_MAJOR;
2454 esp_driver->minor_start = 0;
2455 esp_driver->type = TTY_DRIVER_TYPE_SERIAL;
2456 esp_driver->subtype = SERIAL_TYPE_NORMAL;
2457 esp_driver->init_termios = tty_std_termios;
2458 esp_driver->init_termios.c_cflag =
2459 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2460 esp_driver->flags = TTY_DRIVER_REAL_RAW;
2461 tty_set_operations(esp_driver, &esp_ops);
2462 if (tty_register_driver(esp_driver))
2464 printk(KERN_ERR "Couldn't register esp serial driver");
2465 put_tty_driver(esp_driver);
2466 return 1;
2469 info = kmalloc(sizeof(struct esp_struct), GFP_KERNEL);
2471 if (!info)
2473 printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
2474 tty_unregister_driver(esp_driver);
2475 put_tty_driver(esp_driver);
2476 return 1;
2479 memset((void *)info, 0, sizeof(struct esp_struct));
2480 spin_lock_init(&info->lock);
2481 /* rx_trigger, tx_trigger are needed by autoconfig */
2482 info->config.rx_trigger = rx_trigger;
2483 info->config.tx_trigger = tx_trigger;
2485 i = 0;
2486 offset = 0;
2488 do {
2489 info->port = esp[i] + offset;
2490 info->irq = irq[i];
2491 info->line = (i * 8) + (offset / 8);
2493 if (!autoconfig(info)) {
2494 i++;
2495 offset = 0;
2496 continue;
2499 info->custom_divisor = (divisor[i] >> (offset / 2)) & 0xf;
2500 info->flags = STD_COM_FLAGS;
2501 if (info->custom_divisor)
2502 info->flags |= ASYNC_SPD_CUST;
2503 info->magic = ESP_MAGIC;
2504 info->close_delay = 5*HZ/10;
2505 info->closing_wait = 30*HZ;
2506 INIT_WORK(&info->tqueue, do_softint, info);
2507 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
2508 info->config.rx_timeout = rx_timeout;
2509 info->config.flow_on = flow_on;
2510 info->config.flow_off = flow_off;
2511 info->config.pio_threshold = pio_threshold;
2512 info->next_port = ports;
2513 init_waitqueue_head(&info->open_wait);
2514 init_waitqueue_head(&info->close_wait);
2515 init_waitqueue_head(&info->delta_msr_wait);
2516 init_waitqueue_head(&info->break_wait);
2517 ports = info;
2518 printk(KERN_INFO "ttyP%d at 0x%04x (irq = %d) is an ESP ",
2519 info->line, info->port, info->irq);
2521 if (info->line % 8) {
2522 printk("secondary port\n");
2523 /* 8 port cards can't do DMA */
2524 info->stat_flags |= ESP_STAT_NEVER_DMA;
2526 if (last_primary)
2527 last_primary->stat_flags |= ESP_STAT_NEVER_DMA;
2528 } else {
2529 printk("primary port\n");
2530 last_primary = info;
2531 irq[i] = info->irq;
2534 if (!dma)
2535 info->stat_flags |= ESP_STAT_NEVER_DMA;
2537 info = kmalloc(sizeof(struct esp_struct), GFP_KERNEL);
2538 if (!info)
2540 printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
2542 /* allow use of the already detected ports */
2543 return 0;
2546 memset((void *)info, 0, sizeof(struct esp_struct));
2547 /* rx_trigger, tx_trigger are needed by autoconfig */
2548 info->config.rx_trigger = rx_trigger;
2549 info->config.tx_trigger = tx_trigger;
2551 if (offset == 56) {
2552 i++;
2553 offset = 0;
2554 } else {
2555 offset += 8;
2557 } while (i < NR_PRIMARY);
2559 /* free the last port memory allocation */
2560 kfree(info);
2562 return 0;
2565 static void __exit espserial_exit(void)
2567 int e1;
2568 struct esp_struct *temp_async;
2569 struct esp_pio_buffer *pio_buf;
2571 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2572 if ((e1 = tty_unregister_driver(esp_driver)))
2573 printk("SERIAL: failed to unregister serial driver (%d)\n",
2574 e1);
2575 put_tty_driver(esp_driver);
2577 while (ports) {
2578 if (ports->port) {
2579 release_region(ports->port, REGION_SIZE);
2581 temp_async = ports->next_port;
2582 kfree(ports);
2583 ports = temp_async;
2586 if (dma_buffer)
2587 free_pages((unsigned long)dma_buffer,
2588 get_order(DMA_BUFFER_SZ));
2590 while (free_pio_buf) {
2591 pio_buf = free_pio_buf->next;
2592 kfree(free_pio_buf);
2593 free_pio_buf = pio_buf;
2597 module_init(espserial_init);
2598 module_exit(espserial_exit);