1 /*
2 * esp.c - driver for Hayes ESP serial cards
3 *
4 * --- Notices from serial.c, upon which this driver is based ---
5 *
6 * Copyright (C) 1991, 1992 Linus Torvalds
7 *
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.
12 *
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.
15 *
16 * 11/95: TIOCMIWAIT, TIOCGICOUNT by Angelo Haritsis <ah@doc.ic.ac.uk>
17 *
18 * 03/96: Modularised by Angelo Haritsis <ah@doc.ic.ac.uk>
19 *
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.
23 *
24 * --- End of notices from serial.c ---
25 *
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.
30 *
31 * Most recent changes: (Andrew J. Robinson)
32 * Support for PIO mode. This allows the driver to work properly with
33 * multiport cards.
34 *
35 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> -
36 * several cleanups, use module_init/module_exit, etc
37 *
38 * This module exports the following rs232 io functions:
39 *
40 * int espserial_init(void);
41 */
42
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>
61
62 #include <asm/system.h>
63 #include <asm/io.h>
64 #include <asm/bitops.h>
65
66 #include <asm/dma.h>
67 #include <linux/slab.h>
68 #include <asm/uaccess.h>
69
70 #include <linux/hayesesp.h>
71
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 */
75
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;
86
87 MODULE_LICENSE("GPL");
88
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);
98
99 /* END */
100
101 static char *dma_buffer;
102 static int dma_bytes;
103 static struct esp_pio_buffer *free_pio_buf;
104
105 #define DMA_BUFFER_SZ 1024
106
107 #define WAKEUP_CHARS 1024
108
109 static char serial_name[] __initdata = "ESP serial driver";
110 static char serial_version[] __initdata = "2.2";
111
112 static struct tty_driver *esp_driver;
113
114 /* serial subtype definitions */
115 #define SERIAL_TYPE_NORMAL 1
116
117 /*
118 * Serial driver configuration section. Here are the various options:
119 *
120 * SERIAL_PARANOIA_CHECK
121 * Check the magic number for the esp_structure where
122 * ever possible.
123 */
124
125 #undef SERIAL_PARANOIA_CHECK
126 #define SERIAL_DO_RESTART
127
128 #undef SERIAL_DEBUG_INTR
129 #undef SERIAL_DEBUG_OPEN
130 #undef SERIAL_DEBUG_FLOW
131
132 #define _INLINE_ inline
133
134 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
135 #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
136 tty->name, (info->flags), serial_driver.refcount,info->count,tty->count,s)
137 #else
138 #define DBG_CNT(s)
139 #endif
140
141 static struct esp_struct *ports;
142
143 static void change_speed(struct esp_struct *info);
144 static void rs_wait_until_sent(struct tty_struct *, int);
145
146 /*
147 * The ESP card has a clock rate of 14.7456 MHz (that is, 2**ESPC_SCALE
148 * times the normal 1.8432 Mhz clock of most serial boards).
149 */
150 #define BASE_BAUD ((1843200 / 16) * (1 << ESPC_SCALE))
151
152 /* Standard COM flags (except for COM4, because of the 8514 problem) */
153 #define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
154
155 /*
156 * tmp_buf is used as a temporary buffer by serial_write. We need to
157 * lock it in case the memcpy_fromfs blocks while swapping in a page,
158 * and some other program tries to do a serial write at the same time.
159 * Since the lock will only come under contention when the system is
160 * swapping and available memory is low, it makes sense to share one
161 * buffer across all the serial ports, since it significantly saves
162 * memory if large numbers of serial ports are open.
163 */
164 static unsigned char *tmp_buf;
165 static DECLARE_MUTEX(tmp_buf_sem);
166
167 static inline int serial_paranoia_check(struct esp_struct *info,
168 char *name, const char *routine)
169 {
170 #ifdef SERIAL_PARANOIA_CHECK
171 static const char badmagic[] = KERN_WARNING
172 "Warning: bad magic number for serial struct (%s) in %s\n";
173 static const char badinfo[] = KERN_WARNING
174 "Warning: null esp_struct for (%s) in %s\n";
175
176 if (!info) {
177 printk(badinfo, name, routine);
178 return 1;
179 }
180 if (info->magic != ESP_MAGIC) {
181 printk(badmagic, name, routine);
182 return 1;
183 }
184 #endif
185 return 0;
186 }
187
188 static inline unsigned int serial_in(struct esp_struct *info, int offset)
189 {
190 return inb(info->port + offset);
191 }
192
193 static inline void serial_out(struct esp_struct *info, int offset,
194 unsigned char value)
195 {
196 outb(value, info->port+offset);
197 }
198
199 /*
200 * ------------------------------------------------------------
201 * rs_stop() and rs_start()
202 *
203 * This routines are called before setting or resetting tty->stopped.
204 * They enable or disable transmitter interrupts, as necessary.
205 * ------------------------------------------------------------
206 */
207 static void rs_stop(struct tty_struct *tty)
208 {
209 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
210 unsigned long flags;
211
212 if (serial_paranoia_check(info, tty->name, "rs_stop"))
213 return;
214
215 save_flags(flags); cli();
216 if (info->IER & UART_IER_THRI) {
217 info->IER &= ~UART_IER_THRI;
218 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
219 serial_out(info, UART_ESI_CMD2, info->IER);
220 }
221
222 restore_flags(flags);
223 }
224
225 static void rs_start(struct tty_struct *tty)
226 {
227 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
228 unsigned long flags;
229
230 if (serial_paranoia_check(info, tty->name, "rs_start"))
231 return;
232
233 save_flags(flags); cli();
234 if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
235 info->IER |= UART_IER_THRI;
236 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
237 serial_out(info, UART_ESI_CMD2, info->IER);
238 }
239 restore_flags(flags);
240 }
241
242 /*
243 * ----------------------------------------------------------------------
244 *
245 * Here starts the interrupt handling routines. All of the following
246 * subroutines are declared as inline and are folded into
247 * rs_interrupt(). They were separated out for readability's sake.
248 *
249 * Note: rs_interrupt() is a "fast" interrupt, which means that it
250 * runs with interrupts turned off. People who may want to modify
251 * rs_interrupt() should try to keep the interrupt handler as fast as
252 * possible. After you are done making modifications, it is not a bad
253 * idea to do:
254 *
255 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
256 *
257 * and look at the resulting assemble code in serial.s.
258 *
259 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
260 * -----------------------------------------------------------------------
261 */
262
263 /*
264 * This routine is used by the interrupt handler to schedule
265 * processing in the software interrupt portion of the driver.
266 */
267 static _INLINE_ void rs_sched_event(struct esp_struct *info,
268 int event)
269 {
270 info->event |= 1 << event;
271 schedule_work(&info->tqueue);
272 }
273
274 static _INLINE_ struct esp_pio_buffer *get_pio_buffer(void)
275 {
276 struct esp_pio_buffer *buf;
277
278 if (free_pio_buf) {
279 buf = free_pio_buf;
280 free_pio_buf = buf->next;
281 } else {
282 buf = kmalloc(sizeof(struct esp_pio_buffer), GFP_ATOMIC);
283 }
284
285 return buf;
286 }
287
288 static _INLINE_ void release_pio_buffer(struct esp_pio_buffer *buf)
289 {
290 buf->next = free_pio_buf;
291 free_pio_buf = buf;
292 }
293
294 static _INLINE_ void receive_chars_pio(struct esp_struct *info, int num_bytes)
295 {
296 struct tty_struct *tty = info->tty;
297 int i;
298 struct esp_pio_buffer *pio_buf;
299 struct esp_pio_buffer *err_buf;
300 unsigned char status_mask;
301
302 pio_buf = get_pio_buffer();
303
304 if (!pio_buf)
305 return;
306
307 err_buf = get_pio_buffer();
308
309 if (!err_buf) {
310 release_pio_buffer(pio_buf);
311 return;
312 }
313
314 sti();
315
316 status_mask = (info->read_status_mask >> 2) & 0x07;
317
318 for (i = 0; i < num_bytes - 1; i += 2) {
319 *((unsigned short *)(pio_buf->data + i)) =
320 inw(info->port + UART_ESI_RX);
321 err_buf->data[i] = serial_in(info, UART_ESI_RWS);
322 err_buf->data[i + 1] = (err_buf->data[i] >> 3) & status_mask;
323 err_buf->data[i] &= status_mask;
324 }
325
326 if (num_bytes & 0x0001) {
327 pio_buf->data[num_bytes - 1] = serial_in(info, UART_ESI_RX);
328 err_buf->data[num_bytes - 1] =
329 (serial_in(info, UART_ESI_RWS) >> 3) & status_mask;
330 }
331
332 cli();
333
334 /* make sure everything is still ok since interrupts were enabled */
335 tty = info->tty;
336
337 if (!tty) {
338 release_pio_buffer(pio_buf);
339 release_pio_buffer(err_buf);
340 info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
341 return;
342 }
343
344 status_mask = (info->ignore_status_mask >> 2) & 0x07;
345
346 for (i = 0; i < num_bytes; i++) {
347 if (!(err_buf->data[i] & status_mask)) {
348 *(tty->flip.char_buf_ptr++) = pio_buf->data[i];
349
350 if (err_buf->data[i] & 0x04) {
351 *(tty->flip.flag_buf_ptr++) = TTY_BREAK;
352
353 if (info->flags & ASYNC_SAK)
354 do_SAK(tty);
355 }
356 else if (err_buf->data[i] & 0x02)
357 *(tty->flip.flag_buf_ptr++) = TTY_FRAME;
358 else if (err_buf->data[i] & 0x01)
359 *(tty->flip.flag_buf_ptr++) = TTY_PARITY;
360 else
361 *(tty->flip.flag_buf_ptr++) = 0;
362
363 tty->flip.count++;
364 }
365 }
366
367 schedule_delayed_work(&tty->flip.work, 1);
368
369 info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
370 release_pio_buffer(pio_buf);
371 release_pio_buffer(err_buf);
372 }
373
374 static _INLINE_ void receive_chars_dma(struct esp_struct *info, int num_bytes)
375 {
376 unsigned long flags;
377 info->stat_flags &= ~ESP_STAT_RX_TIMEOUT;
378 dma_bytes = num_bytes;
379 info->stat_flags |= ESP_STAT_DMA_RX;
380
381 flags=claim_dma_lock();
382 disable_dma(dma);
383 clear_dma_ff(dma);
384 set_dma_mode(dma, DMA_MODE_READ);
385 set_dma_addr(dma, isa_virt_to_bus(dma_buffer));
386 set_dma_count(dma, dma_bytes);
387 enable_dma(dma);
388 release_dma_lock(flags);
389
390 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_RX);
391 }
392
393 static _INLINE_ void receive_chars_dma_done(struct esp_struct *info,
394 int status)
395 {
396 struct tty_struct *tty = info->tty;
397 int num_bytes;
398 unsigned long flags;
399
400
401 flags=claim_dma_lock();
402 disable_dma(dma);
403 clear_dma_ff(dma);
404
405 info->stat_flags &= ~ESP_STAT_DMA_RX;
406 num_bytes = dma_bytes - get_dma_residue(dma);
407 release_dma_lock(flags);
408
409 info->icount.rx += num_bytes;
410
411 memcpy(tty->flip.char_buf_ptr, dma_buffer, num_bytes);
412 tty->flip.char_buf_ptr += num_bytes;
413 tty->flip.count += num_bytes;
414 memset(tty->flip.flag_buf_ptr, 0, num_bytes);
415 tty->flip.flag_buf_ptr += num_bytes;
416
417 if (num_bytes > 0) {
418 tty->flip.flag_buf_ptr--;
419
420 status &= (0x1c & info->read_status_mask);
421
422 if (status & info->ignore_status_mask) {
423 tty->flip.count--;
424 tty->flip.char_buf_ptr--;
425 tty->flip.flag_buf_ptr--;
426 } else if (status & 0x10) {
427 *tty->flip.flag_buf_ptr = TTY_BREAK;
428 (info->icount.brk)++;
429 if (info->flags & ASYNC_SAK)
430 do_SAK(tty);
431 } else if (status & 0x08) {
432 *tty->flip.flag_buf_ptr = TTY_FRAME;
433 (info->icount.frame)++;
434 }
435 else if (status & 0x04) {
436 *tty->flip.flag_buf_ptr = TTY_PARITY;
437 (info->icount.parity)++;
438 }
439
440 tty->flip.flag_buf_ptr++;
441
442 schedule_delayed_work(&tty->flip.work, 1);
443 }
444
445 if (dma_bytes != num_bytes) {
446 num_bytes = dma_bytes - num_bytes;
447 dma_bytes = 0;
448 receive_chars_dma(info, num_bytes);
449 } else
450 dma_bytes = 0;
451 }
452
453 static _INLINE_ void transmit_chars_pio(struct esp_struct *info,
454 int space_avail)
455 {
456 int i;
457 struct esp_pio_buffer *pio_buf;
458
459 pio_buf = get_pio_buffer();
460
461 if (!pio_buf)
462 return;
463
464 while (space_avail && info->xmit_cnt) {
465 if (info->xmit_tail + space_avail <= ESP_XMIT_SIZE) {
466 memcpy(pio_buf->data,
467 &(info->xmit_buf[info->xmit_tail]),
468 space_avail);
469 } else {
470 i = ESP_XMIT_SIZE - info->xmit_tail;
471 memcpy(pio_buf->data,
472 &(info->xmit_buf[info->xmit_tail]), i);
473 memcpy(&(pio_buf->data[i]), info->xmit_buf,
474 space_avail - i);
475 }
476
477 info->xmit_cnt -= space_avail;
478 info->xmit_tail = (info->xmit_tail + space_avail) &
479 (ESP_XMIT_SIZE - 1);
480
481 sti();
482
483 for (i = 0; i < space_avail - 1; i += 2) {
484 outw(*((unsigned short *)(pio_buf->data + i)),
485 info->port + UART_ESI_TX);
486 }
487
488 if (space_avail & 0x0001)
489 serial_out(info, UART_ESI_TX,
490 pio_buf->data[space_avail - 1]);
491
492 cli();
493
494 if (info->xmit_cnt) {
495 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
496 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
497 space_avail = serial_in(info, UART_ESI_STAT1) << 8;
498 space_avail |= serial_in(info, UART_ESI_STAT2);
499
500 if (space_avail > info->xmit_cnt)
501 space_avail = info->xmit_cnt;
502 }
503 }
504
505 if (info->xmit_cnt < WAKEUP_CHARS) {
506 rs_sched_event(info, ESP_EVENT_WRITE_WAKEUP);
507
508 #ifdef SERIAL_DEBUG_INTR
509 printk("THRE...");
510 #endif
511
512 if (info->xmit_cnt <= 0) {
513 info->IER &= ~UART_IER_THRI;
514 serial_out(info, UART_ESI_CMD1,
515 ESI_SET_SRV_MASK);
516 serial_out(info, UART_ESI_CMD2, info->IER);
517 }
518 }
519
520 release_pio_buffer(pio_buf);
521 }
522
523 static _INLINE_ void transmit_chars_dma(struct esp_struct *info, int num_bytes)
524 {
525 unsigned long flags;
526
527 dma_bytes = num_bytes;
528
529 if (info->xmit_tail + dma_bytes <= ESP_XMIT_SIZE) {
530 memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
531 dma_bytes);
532 } else {
533 int i = ESP_XMIT_SIZE - info->xmit_tail;
534 memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
535 i);
536 memcpy(&(dma_buffer[i]), info->xmit_buf, dma_bytes - i);
537 }
538
539 info->xmit_cnt -= dma_bytes;
540 info->xmit_tail = (info->xmit_tail + dma_bytes) & (ESP_XMIT_SIZE - 1);
541
542 if (info->xmit_cnt < WAKEUP_CHARS) {
543 rs_sched_event(info, ESP_EVENT_WRITE_WAKEUP);
544
545 #ifdef SERIAL_DEBUG_INTR
546 printk("THRE...");
547 #endif
548
549 if (info->xmit_cnt <= 0) {
550 info->IER &= ~UART_IER_THRI;
551 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
552 serial_out(info, UART_ESI_CMD2, info->IER);
553 }
554 }
555
556 info->stat_flags |= ESP_STAT_DMA_TX;
557
558 flags=claim_dma_lock();
559 disable_dma(dma);
560 clear_dma_ff(dma);
561 set_dma_mode(dma, DMA_MODE_WRITE);
562 set_dma_addr(dma, isa_virt_to_bus(dma_buffer));
563 set_dma_count(dma, dma_bytes);
564 enable_dma(dma);
565 release_dma_lock(flags);
566
567 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
568 }
569
570 static _INLINE_ void transmit_chars_dma_done(struct esp_struct *info)
571 {
572 int num_bytes;
573 unsigned long flags;
574
575
576 flags=claim_dma_lock();
577 disable_dma(dma);
578 clear_dma_ff(dma);
579
580 num_bytes = dma_bytes - get_dma_residue(dma);
581 info->icount.tx += dma_bytes;
582 release_dma_lock(flags);
583
584 if (dma_bytes != num_bytes) {
585 dma_bytes -= num_bytes;
586 memmove(dma_buffer, dma_buffer + num_bytes, dma_bytes);
587
588 flags=claim_dma_lock();
589 disable_dma(dma);
590 clear_dma_ff(dma);
591 set_dma_mode(dma, DMA_MODE_WRITE);
592 set_dma_addr(dma, isa_virt_to_bus(dma_buffer));
593 set_dma_count(dma, dma_bytes);
594 enable_dma(dma);
595 release_dma_lock(flags);
596
597 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
598 } else {
599 dma_bytes = 0;
600 info->stat_flags &= ~ESP_STAT_DMA_TX;
601 }
602 }
603
604 static _INLINE_ void check_modem_status(struct esp_struct *info)
605 {
606 int status;
607
608 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
609 status = serial_in(info, UART_ESI_STAT2);
610
611 if (status & UART_MSR_ANY_DELTA) {
612 /* update input line counters */
613 if (status & UART_MSR_TERI)
614 info->icount.rng++;
615 if (status & UART_MSR_DDSR)
616 info->icount.dsr++;
617 if (status & UART_MSR_DDCD)
618 info->icount.dcd++;
619 if (status & UART_MSR_DCTS)
620 info->icount.cts++;
621 wake_up_interruptible(&info->delta_msr_wait);
622 }
623
624 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
625 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
626 printk("ttys%d CD now %s...", info->line,
627 (status & UART_MSR_DCD) ? "on" : "off");
628 #endif
629 if (status & UART_MSR_DCD)
630 wake_up_interruptible(&info->open_wait);
631 else {
632 #ifdef SERIAL_DEBUG_OPEN
633 printk("scheduling hangup...");
634 #endif
635 schedule_work(&info->tqueue_hangup);
636 }
637 }
638 }
639
640 /*
641 * This is the serial driver's interrupt routine
642 */
643 static irqreturn_t rs_interrupt_single(int irq, void *dev_id,
644 struct pt_regs *regs)
645 {
646 struct esp_struct * info;
647 unsigned err_status;
648 unsigned int scratch;
649
650 #ifdef SERIAL_DEBUG_INTR
651 printk("rs_interrupt_single(%d)...", irq);
652 #endif
653 info = (struct esp_struct *)dev_id;
654 err_status = 0;
655 scratch = serial_in(info, UART_ESI_SID);
656
657 cli();
658
659 if (!info->tty) {
660 sti();
661 return IRQ_NONE;
662 }
663
664 if (scratch & 0x04) { /* error */
665 serial_out(info, UART_ESI_CMD1, ESI_GET_ERR_STAT);
666 err_status = serial_in(info, UART_ESI_STAT1);
667 serial_in(info, UART_ESI_STAT2);
668
669 if (err_status & 0x01)
670 info->stat_flags |= ESP_STAT_RX_TIMEOUT;
671
672 if (err_status & 0x20) /* UART status */
673 check_modem_status(info);
674
675 if (err_status & 0x80) /* Start break */
676 wake_up_interruptible(&info->break_wait);
677 }
678
679 if ((scratch & 0x88) || /* DMA completed or timed out */
680 (err_status & 0x1c) /* receive error */) {
681 if (info->stat_flags & ESP_STAT_DMA_RX)
682 receive_chars_dma_done(info, err_status);
683 else if (info->stat_flags & ESP_STAT_DMA_TX)
684 transmit_chars_dma_done(info);
685 }
686
687 if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
688 ((scratch & 0x01) || (info->stat_flags & ESP_STAT_RX_TIMEOUT)) &&
689 (info->IER & UART_IER_RDI)) {
690 int num_bytes;
691
692 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
693 serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
694 num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
695 num_bytes |= serial_in(info, UART_ESI_STAT2);
696
697 if (num_bytes > (TTY_FLIPBUF_SIZE - info->tty->flip.count))
698 num_bytes = TTY_FLIPBUF_SIZE - info->tty->flip.count;
699
700 if (num_bytes) {
701 if (dma_bytes ||
702 (info->stat_flags & ESP_STAT_USE_PIO) ||
703 (num_bytes <= info->config.pio_threshold))
704 receive_chars_pio(info, num_bytes);
705 else
706 receive_chars_dma(info, num_bytes);
707 }
708 }
709
710 if (!(info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) &&
711 (scratch & 0x02) && (info->IER & UART_IER_THRI)) {
712 if ((info->xmit_cnt <= 0) || info->tty->stopped) {
713 info->IER &= ~UART_IER_THRI;
714 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
715 serial_out(info, UART_ESI_CMD2, info->IER);
716 } else {
717 int num_bytes;
718
719 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
720 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
721 num_bytes = serial_in(info, UART_ESI_STAT1) << 8;
722 num_bytes |= serial_in(info, UART_ESI_STAT2);
723
724 if (num_bytes > info->xmit_cnt)
725 num_bytes = info->xmit_cnt;
726
727 if (num_bytes) {
728 if (dma_bytes ||
729 (info->stat_flags & ESP_STAT_USE_PIO) ||
730 (num_bytes <= info->config.pio_threshold))
731 transmit_chars_pio(info, num_bytes);
732 else
733 transmit_chars_dma(info, num_bytes);
734 }
735 }
736 }
737
738 info->last_active = jiffies;
739
740 #ifdef SERIAL_DEBUG_INTR
741 printk("end.\n");
742 #endif
743 sti();
744 return IRQ_HANDLED;
745 }
746
747 /*
748 * -------------------------------------------------------------------
749 * Here ends the serial interrupt routines.
750 * -------------------------------------------------------------------
751 */
752
753 static void do_softint(void *private_)
754 {
755 struct esp_struct *info = (struct esp_struct *) private_;
756 struct tty_struct *tty;
757
758 tty = info->tty;
759 if (!tty)
760 return;
761
762 if (test_and_clear_bit(ESP_EVENT_WRITE_WAKEUP, &info->event)) {
763 tty_wakeup(tty);
764 }
765 }
766
767 /*
768 * This routine is called from the scheduler tqueue when the interrupt
769 * routine has signalled that a hangup has occurred. The path of
770 * hangup processing is:
771 *
772 * serial interrupt routine -> (scheduler tqueue) ->
773 * do_serial_hangup() -> tty->hangup() -> esp_hangup()
774 *
775 */
776 static void do_serial_hangup(void *private_)
777 {
778 struct esp_struct *info = (struct esp_struct *) private_;
779 struct tty_struct *tty;
780
781 tty = info->tty;
782 if (tty)
783 tty_hangup(tty);
784 }
785
786 /*
787 * ---------------------------------------------------------------
788 * Low level utility subroutines for the serial driver: routines to
789 * figure out the appropriate timeout for an interrupt chain, routines
790 * to initialize and startup a serial port, and routines to shutdown a
791 * serial port. Useful stuff like that.
792 * ---------------------------------------------------------------
793 */
794
795 static _INLINE_ void esp_basic_init(struct esp_struct * info)
796 {
797 /* put ESPC in enhanced mode */
798 serial_out(info, UART_ESI_CMD1, ESI_SET_MODE);
799
800 if (info->stat_flags & ESP_STAT_NEVER_DMA)
801 serial_out(info, UART_ESI_CMD2, 0x01);
802 else
803 serial_out(info, UART_ESI_CMD2, 0x31);
804
805 /* disable interrupts for now */
806 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
807 serial_out(info, UART_ESI_CMD2, 0x00);
808
809 /* set interrupt and DMA channel */
810 serial_out(info, UART_ESI_CMD1, ESI_SET_IRQ);
811
812 if (info->stat_flags & ESP_STAT_NEVER_DMA)
813 serial_out(info, UART_ESI_CMD2, 0x01);
814 else
815 serial_out(info, UART_ESI_CMD2, (dma << 4) | 0x01);
816
817 serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
818
819 if (info->line % 8) /* secondary port */
820 serial_out(info, UART_ESI_CMD2, 0x0d); /* shared */
821 else if (info->irq == 9)
822 serial_out(info, UART_ESI_CMD2, 0x02);
823 else
824 serial_out(info, UART_ESI_CMD2, info->irq);
825
826 /* set error status mask (check this) */
827 serial_out(info, UART_ESI_CMD1, ESI_SET_ERR_MASK);
828
829 if (info->stat_flags & ESP_STAT_NEVER_DMA)
830 serial_out(info, UART_ESI_CMD2, 0xa1);
831 else
832 serial_out(info, UART_ESI_CMD2, 0xbd);
833
834 serial_out(info, UART_ESI_CMD2, 0x00);
835
836 /* set DMA timeout */
837 serial_out(info, UART_ESI_CMD1, ESI_SET_DMA_TMOUT);
838 serial_out(info, UART_ESI_CMD2, 0xff);
839
840 /* set FIFO trigger levels */
841 serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
842 serial_out(info, UART_ESI_CMD2, info->config.rx_trigger >> 8);
843 serial_out(info, UART_ESI_CMD2, info->config.rx_trigger);
844 serial_out(info, UART_ESI_CMD2, info->config.tx_trigger >> 8);
845 serial_out(info, UART_ESI_CMD2, info->config.tx_trigger);
846
847 /* Set clock scaling and wait states */
848 serial_out(info, UART_ESI_CMD1, ESI_SET_PRESCALAR);
849 serial_out(info, UART_ESI_CMD2, 0x04 | ESPC_SCALE);
850
851 /* set reinterrupt pacing */
852 serial_out(info, UART_ESI_CMD1, ESI_SET_REINTR);
853 serial_out(info, UART_ESI_CMD2, 0xff);
854 }
855
856 static int startup(struct esp_struct * info)
857 {
858 unsigned long flags;
859 int retval=0;
860 unsigned int num_chars;
861
862 save_flags(flags); cli();
863
864 if (info->flags & ASYNC_INITIALIZED)
865 goto out;
866
867 if (!info->xmit_buf) {
868 info->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
869 retval = -ENOMEM;
870 if (!info->xmit_buf)
871 goto out;
872 }
873
874 #ifdef SERIAL_DEBUG_OPEN
875 printk("starting up ttys%d (irq %d)...", info->line, info->irq);
876 #endif
877
878 /* Flush the RX buffer. Using the ESI flush command may cause */
879 /* wild interrupts, so read all the data instead. */
880
881 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
882 serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
883 num_chars = serial_in(info, UART_ESI_STAT1) << 8;
884 num_chars |= serial_in(info, UART_ESI_STAT2);
885
886 while (num_chars > 1) {
887 inw(info->port + UART_ESI_RX);
888 num_chars -= 2;
889 }
890
891 if (num_chars)
892 serial_in(info, UART_ESI_RX);
893
894 /* set receive character timeout */
895 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
896 serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
897
898 /* clear all flags except the "never DMA" flag */
899 info->stat_flags &= ESP_STAT_NEVER_DMA;
900
901 if (info->stat_flags & ESP_STAT_NEVER_DMA)
902 info->stat_flags |= ESP_STAT_USE_PIO;
903
904 /*
905 * Allocate the IRQ
906 */
907
908 retval = request_irq(info->irq, rs_interrupt_single, SA_SHIRQ,
909 "esp serial", info);
910
911 if (retval) {
912 if (capable(CAP_SYS_ADMIN)) {
913 if (info->tty)
914 set_bit(TTY_IO_ERROR,
915 &info->tty->flags);
916 retval = 0;
917 }
918 goto out;
919 }
920
921 if (!(info->stat_flags & ESP_STAT_USE_PIO) && !dma_buffer) {
922 dma_buffer = (char *)__get_dma_pages(
923 GFP_KERNEL, get_order(DMA_BUFFER_SZ));
924
925 /* use PIO mode if DMA buf/chan cannot be allocated */
926 if (!dma_buffer)
927 info->stat_flags |= ESP_STAT_USE_PIO;
928 else if (request_dma(dma, "esp serial")) {
929 free_pages((unsigned long)dma_buffer,
930 get_order(DMA_BUFFER_SZ));
931 dma_buffer = NULL;
932 info->stat_flags |= ESP_STAT_USE_PIO;
933 }
934
935 }
936
937 info->MCR = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
938 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
939 serial_out(info, UART_ESI_CMD2, UART_MCR);
940 serial_out(info, UART_ESI_CMD2, info->MCR);
941
942 /*
943 * Finally, enable interrupts
944 */
945 /* info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI; */
946 info->IER = UART_IER_RLSI | UART_IER_RDI | UART_IER_DMA_TMOUT |
947 UART_IER_DMA_TC;
948 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
949 serial_out(info, UART_ESI_CMD2, info->IER);
950
951 if (info->tty)
952 clear_bit(TTY_IO_ERROR, &info->tty->flags);
953 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
954
955 /*
956 * Set up the tty->alt_speed kludge
957 */
958 if (info->tty) {
959 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
960 info->tty->alt_speed = 57600;
961 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
962 info->tty->alt_speed = 115200;
963 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
964 info->tty->alt_speed = 230400;
965 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
966 info->tty->alt_speed = 460800;
967 }
968
969 /*
970 * set the speed of the serial port
971 */
972 change_speed(info);
973
974 info->flags |= ASYNC_INITIALIZED;
975 retval = 0;
976 out: restore_flags(flags);
977 return retval;
978 }
979
980 /*
981 * This routine will shutdown a serial port; interrupts are disabled, and
982 * DTR is dropped if the hangup on close termio flag is on.
983 */
984 static void shutdown(struct esp_struct * info)
985 {
986 unsigned long flags, f;
987
988 if (!(info->flags & ASYNC_INITIALIZED))
989 return;
990
991 #ifdef SERIAL_DEBUG_OPEN
992 printk("Shutting down serial port %d (irq %d)....", info->line,
993 info->irq);
994 #endif
995
996 save_flags(flags); cli(); /* Disable interrupts */
997
998 /*
999 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1000 * here so the queue might never be waken up
1001 */
1002 wake_up_interruptible(&info->delta_msr_wait);
1003 wake_up_interruptible(&info->break_wait);
1004
1005 /* stop a DMA transfer on the port being closed */
1006
1007 if (info->stat_flags & (ESP_STAT_DMA_RX | ESP_STAT_DMA_TX)) {
1008 f=claim_dma_lock();
1009 disable_dma(dma);
1010 clear_dma_ff(dma);
1011 release_dma_lock(f);
1012
1013 dma_bytes = 0;
1014 }
1015
1016 /*
1017 * Free the IRQ
1018 */
1019 free_irq(info->irq, info);
1020
1021 if (dma_buffer) {
1022 struct esp_struct *current_port = ports;
1023
1024 while (current_port) {
1025 if ((current_port != info) &&
1026 (current_port->flags & ASYNC_INITIALIZED))
1027 break;
1028
1029 current_port = current_port->next_port;
1030 }
1031
1032 if (!current_port) {
1033 free_dma(dma);
1034 free_pages((unsigned long)dma_buffer,
1035 get_order(DMA_BUFFER_SZ));
1036 dma_buffer = NULL;
1037 }
1038 }
1039
1040 if (info->xmit_buf) {
1041 free_page((unsigned long) info->xmit_buf);
1042 info->xmit_buf = NULL;
1043 }
1044
1045 info->IER = 0;
1046 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1047 serial_out(info, UART_ESI_CMD2, 0x00);
1048
1049 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
1050 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1051
1052 info->MCR &= ~UART_MCR_OUT2;
1053 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1054 serial_out(info, UART_ESI_CMD2, UART_MCR);
1055 serial_out(info, UART_ESI_CMD2, info->MCR);
1056
1057 if (info->tty)
1058 set_bit(TTY_IO_ERROR, &info->tty->flags);
1059
1060 info->flags &= ~ASYNC_INITIALIZED;
1061 restore_flags(flags);
1062 }
1063
1064 /*
1065 * This routine is called to set the UART divisor registers to match
1066 * the specified baud rate for a serial port.
1067 */
1068 static void change_speed(struct esp_struct *info)
1069 {
1070 unsigned short port;
1071 int quot = 0;
1072 unsigned cflag,cval;
1073 int baud, bits;
1074 unsigned char flow1 = 0, flow2 = 0;
1075 unsigned long flags;
1076
1077 if (!info->tty || !info->tty->termios)
1078 return;
1079 cflag = info->tty->termios->c_cflag;
1080 port = info->port;
1081
1082 /* byte size and parity */
1083 switch (cflag & CSIZE) {
1084 case CS5: cval = 0x00; bits = 7; break;
1085 case CS6: cval = 0x01; bits = 8; break;
1086 case CS7: cval = 0x02; bits = 9; break;
1087 case CS8: cval = 0x03; bits = 10; break;
1088 default: cval = 0x00; bits = 7; break;
1089 }
1090 if (cflag & CSTOPB) {
1091 cval |= 0x04;
1092 bits++;
1093 }
1094 if (cflag & PARENB) {
1095 cval |= UART_LCR_PARITY;
1096 bits++;
1097 }
1098 if (!(cflag & PARODD))
1099 cval |= UART_LCR_EPAR;
1100 #ifdef CMSPAR
1101 if (cflag & CMSPAR)
1102 cval |= UART_LCR_SPAR;
1103 #endif
1104
1105 baud = tty_get_baud_rate(info->tty);
1106 if (baud == 38400 &&
1107 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1108 quot = info->custom_divisor;
1109 else {
1110 if (baud == 134)
1111 /* Special case since 134 is really 134.5 */
1112 quot = (2*BASE_BAUD / 269);
1113 else if (baud)
1114 quot = BASE_BAUD / baud;
1115 }
1116 /* If the quotient is ever zero, default to 9600 bps */
1117 if (!quot)
1118 quot = BASE_BAUD / 9600;
1119
1120 info->timeout = ((1024 * HZ * bits * quot) / BASE_BAUD) + (HZ / 50);
1121
1122 /* CTS flow control flag and modem status interrupts */
1123 /* info->IER &= ~UART_IER_MSI; */
1124 if (cflag & CRTSCTS) {
1125 info->flags |= ASYNC_CTS_FLOW;
1126 /* info->IER |= UART_IER_MSI; */
1127 flow1 = 0x04;
1128 flow2 = 0x10;
1129 } else
1130 info->flags &= ~ASYNC_CTS_FLOW;
1131 if (cflag & CLOCAL)
1132 info->flags &= ~ASYNC_CHECK_CD;
1133 else {
1134 info->flags |= ASYNC_CHECK_CD;
1135 /* info->IER |= UART_IER_MSI; */
1136 }
1137
1138 /*
1139 * Set up parity check flag
1140 */
1141 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1142
1143 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1144 if (I_INPCK(info->tty))
1145 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1146 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1147 info->read_status_mask |= UART_LSR_BI;
1148
1149 info->ignore_status_mask = 0;
1150 #if 0
1151 /* This should be safe, but for some broken bits of hardware... */
1152 if (I_IGNPAR(info->tty)) {
1153 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1154 info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
1155 }
1156 #endif
1157 if (I_IGNBRK(info->tty)) {
1158 info->ignore_status_mask |= UART_LSR_BI;
1159 info->read_status_mask |= UART_LSR_BI;
1160 /*
1161 * If we're ignore parity and break indicators, ignore
1162 * overruns too. (For real raw support).
1163 */
1164 if (I_IGNPAR(info->tty)) {
1165 info->ignore_status_mask |= UART_LSR_OE | \
1166 UART_LSR_PE | UART_LSR_FE;
1167 info->read_status_mask |= UART_LSR_OE | \
1168 UART_LSR_PE | UART_LSR_FE;
1169 }
1170 }
1171
1172 if (I_IXOFF(info->tty))
1173 flow1 |= 0x81;
1174
1175 save_flags(flags); cli();
1176 /* set baud */
1177 serial_out(info, UART_ESI_CMD1, ESI_SET_BAUD);
1178 serial_out(info, UART_ESI_CMD2, quot >> 8);
1179 serial_out(info, UART_ESI_CMD2, quot & 0xff);
1180
1181 /* set data bits, parity, etc. */
1182 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1183 serial_out(info, UART_ESI_CMD2, UART_LCR);
1184 serial_out(info, UART_ESI_CMD2, cval);
1185
1186 /* Enable flow control */
1187 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CNTL);
1188 serial_out(info, UART_ESI_CMD2, flow1);
1189 serial_out(info, UART_ESI_CMD2, flow2);
1190
1191 /* set flow control characters (XON/XOFF only) */
1192 if (I_IXOFF(info->tty)) {
1193 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CHARS);
1194 serial_out(info, UART_ESI_CMD2, START_CHAR(info->tty));
1195 serial_out(info, UART_ESI_CMD2, STOP_CHAR(info->tty));
1196 serial_out(info, UART_ESI_CMD2, 0x10);
1197 serial_out(info, UART_ESI_CMD2, 0x21);
1198 switch (cflag & CSIZE) {
1199 case CS5:
1200 serial_out(info, UART_ESI_CMD2, 0x1f);
1201 break;
1202 case CS6:
1203 serial_out(info, UART_ESI_CMD2, 0x3f);
1204 break;
1205 case CS7:
1206 case CS8:
1207 serial_out(info, UART_ESI_CMD2, 0x7f);
1208 break;
1209 default:
1210 serial_out(info, UART_ESI_CMD2, 0xff);
1211 break;
1212 }
1213 }
1214
1215 /* Set high/low water */
1216 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1217 serial_out(info, UART_ESI_CMD2, info->config.flow_off >> 8);
1218 serial_out(info, UART_ESI_CMD2, info->config.flow_off);
1219 serial_out(info, UART_ESI_CMD2, info->config.flow_on >> 8);
1220 serial_out(info, UART_ESI_CMD2, info->config.flow_on);
1221
1222 restore_flags(flags);
1223 }
1224
1225 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
1226 {
1227 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1228 unsigned long flags;
1229
1230 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
1231 return;
1232
1233 if (!tty || !info->xmit_buf)
1234 return;
1235
1236 save_flags(flags); cli();
1237 if (info->xmit_cnt >= ESP_XMIT_SIZE - 1) {
1238 restore_flags(flags);
1239 return;
1240 }
1241
1242 info->xmit_buf[info->xmit_head++] = ch;
1243 info->xmit_head &= ESP_XMIT_SIZE-1;
1244 info->xmit_cnt++;
1245 restore_flags(flags);
1246 }
1247
1248 static void rs_flush_chars(struct tty_struct *tty)
1249 {
1250 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1251 unsigned long flags;
1252
1253 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
1254 return;
1255
1256 if (info->xmit_cnt <= 0 || tty->stopped || !info->xmit_buf)
1257 return;
1258
1259 save_flags(flags); cli();
1260 if (!(info->IER & UART_IER_THRI)) {
1261 info->IER |= UART_IER_THRI;
1262 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1263 serial_out(info, UART_ESI_CMD2, info->IER);
1264 }
1265 restore_flags(flags);
1266 }
1267
1268 static int rs_write(struct tty_struct * tty,
1269 const unsigned char *buf, int count)
1270 {
1271 int c, t, ret = 0;
1272 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1273 unsigned long flags;
1274
1275 if (serial_paranoia_check(info, tty->name, "rs_write"))
1276 return 0;
1277
1278 if (!tty || !info->xmit_buf || !tmp_buf)
1279 return 0;
1280
1281 while (1) {
1282 /* Thanks to R. Wolff for suggesting how to do this with */
1283 /* interrupts enabled */
1284
1285 c = count;
1286 t = ESP_XMIT_SIZE - info->xmit_cnt - 1;
1287
1288 if (t < c)
1289 c = t;
1290
1291 t = ESP_XMIT_SIZE - info->xmit_head;
1292
1293 if (t < c)
1294 c = t;
1295
1296 if (c <= 0)
1297 break;
1298
1299 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1300
1301 info->xmit_head = (info->xmit_head + c) & (ESP_XMIT_SIZE-1);
1302 info->xmit_cnt += c;
1303 buf += c;
1304 count -= c;
1305 ret += c;
1306 }
1307
1308 save_flags(flags); cli();
1309
1310 if (info->xmit_cnt && !tty->stopped && !(info->IER & UART_IER_THRI)) {
1311 info->IER |= UART_IER_THRI;
1312 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1313 serial_out(info, UART_ESI_CMD2, info->IER);
1314 }
1315
1316 restore_flags(flags);
1317 return ret;
1318 }
1319
1320 static int rs_write_room(struct tty_struct *tty)
1321 {
1322 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1323 int ret;
1324
1325 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
1326 return 0;
1327 ret = ESP_XMIT_SIZE - info->xmit_cnt - 1;
1328 if (ret < 0)
1329 ret = 0;
1330 return ret;
1331 }
1332
1333 static int rs_chars_in_buffer(struct tty_struct *tty)
1334 {
1335 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1336
1337 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
1338 return 0;
1339 return info->xmit_cnt;
1340 }
1341
1342 static void rs_flush_buffer(struct tty_struct *tty)
1343 {
1344 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1345
1346 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
1347 return;
1348 cli();
1349 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1350 sti();
1351 tty_wakeup(tty);
1352 }
1353
1354 /*
1355 * ------------------------------------------------------------
1356 * rs_throttle()
1357 *
1358 * This routine is called by the upper-layer tty layer to signal that
1359 * incoming characters should be throttled.
1360 * ------------------------------------------------------------
1361 */
1362 static void rs_throttle(struct tty_struct * tty)
1363 {
1364 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1365 #ifdef SERIAL_DEBUG_THROTTLE
1366 char buf[64];
1367
1368 printk("throttle %s: %d....\n", tty_name(tty, buf),
1369 tty->ldisc.chars_in_buffer(tty));
1370 #endif
1371
1372 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
1373 return;
1374
1375 cli();
1376 info->IER &= ~UART_IER_RDI;
1377 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1378 serial_out(info, UART_ESI_CMD2, info->IER);
1379 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1380 serial_out(info, UART_ESI_CMD2, 0x00);
1381 sti();
1382 }
1383
1384 static void rs_unthrottle(struct tty_struct * tty)
1385 {
1386 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1387 #ifdef SERIAL_DEBUG_THROTTLE
1388 char buf[64];
1389
1390 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1391 tty->ldisc.chars_in_buffer(tty));
1392 #endif
1393
1394 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1395 return;
1396
1397 cli();
1398 info->IER |= UART_IER_RDI;
1399 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1400 serial_out(info, UART_ESI_CMD2, info->IER);
1401 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1402 serial_out(info, UART_ESI_CMD2, info->config.rx_timeout);
1403 sti();
1404 }
1405
1406 /*
1407 * ------------------------------------------------------------
1408 * rs_ioctl() and friends
1409 * ------------------------------------------------------------
1410 */
1411
1412 static int get_serial_info(struct esp_struct * info,
1413 struct serial_struct __user *retinfo)
1414 {
1415 struct serial_struct tmp;
1416
1417 memset(&tmp, 0, sizeof(tmp));
1418 tmp.type = PORT_16550A;
1419 tmp.line = info->line;
1420 tmp.port = info->port;
1421 tmp.irq = info->irq;
1422 tmp.flags = info->flags;
1423 tmp.xmit_fifo_size = 1024;
1424 tmp.baud_base = BASE_BAUD;
1425 tmp.close_delay = info->close_delay;
1426 tmp.closing_wait = info->closing_wait;
1427 tmp.custom_divisor = info->custom_divisor;
1428 tmp.hub6 = 0;
1429 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1430 return -EFAULT;
1431 return 0;
1432 }
1433
1434 static int get_esp_config(struct esp_struct * info,
1435 struct hayes_esp_config __user *retinfo)
1436 {
1437 struct hayes_esp_config tmp;
1438
1439 if (!retinfo)
1440 return -EFAULT;
1441
1442 memset(&tmp, 0, sizeof(tmp));
1443 tmp.rx_timeout = info->config.rx_timeout;
1444 tmp.rx_trigger = info->config.rx_trigger;
1445 tmp.tx_trigger = info->config.tx_trigger;
1446 tmp.flow_off = info->config.flow_off;
1447 tmp.flow_on = info->config.flow_on;
1448 tmp.pio_threshold = info->config.pio_threshold;
1449 tmp.dma_channel = (info->stat_flags & ESP_STAT_NEVER_DMA ? 0 : dma);
1450
1451 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
1452 }
1453
1454 static int set_serial_info(struct esp_struct * info,
1455 struct serial_struct __user *new_info)
1456 {
1457 struct serial_struct new_serial;
1458 struct esp_struct old_info;
1459 unsigned int change_irq;
1460 int retval = 0;
1461 struct esp_struct *current_async;
1462
1463 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1464 return -EFAULT;
1465 old_info = *info;
1466
1467 if ((new_serial.type != PORT_16550A) ||
1468 (new_serial.hub6) ||
1469 (info->port != new_serial.port) ||
1470 (new_serial.baud_base != BASE_BAUD) ||
1471 (new_serial.irq > 15) ||
1472 (new_serial.irq < 2) ||
1473 (new_serial.irq == 6) ||
1474 (new_serial.irq == 8) ||
1475 (new_serial.irq == 13))
1476 return -EINVAL;
1477
1478 change_irq = new_serial.irq != info->irq;
1479
1480 if (change_irq && (info->line % 8))
1481 return -EINVAL;
1482
1483 if (!capable(CAP_SYS_ADMIN)) {
1484 if (change_irq ||
1485 (new_serial.close_delay != info->close_delay) ||
1486 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1487 (info->flags & ~ASYNC_USR_MASK)))
1488 return -EPERM;
1489 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1490 (new_serial.flags & ASYNC_USR_MASK));
1491 info->custom_divisor = new_serial.custom_divisor;
1492 } else {
1493 if (new_serial.irq == 2)
1494 new_serial.irq = 9;
1495
1496 if (change_irq) {
1497 current_async = ports;
1498
1499 while (current_async) {
1500 if ((current_async->line >= info->line) &&
1501 (current_async->line < (info->line + 8))) {
1502 if (current_async == info) {
1503 if (current_async->count > 1)
1504 return -EBUSY;
1505 } else if (current_async->count)
1506 return -EBUSY;
1507 }
1508
1509 current_async = current_async->next_port;
1510 }
1511 }
1512
1513 /*
1514 * OK, past this point, all the error checking has been done.
1515 * At this point, we start making changes.....
1516 */
1517
1518 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1519 (new_serial.flags & ASYNC_FLAGS));
1520 info->custom_divisor = new_serial.custom_divisor;
1521 info->close_delay = new_serial.close_delay * HZ/100;
1522 info->closing_wait = new_serial.closing_wait * HZ/100;
1523
1524 if (change_irq) {
1525 /*
1526 * We need to shutdown the serial port at the old
1527 * port/irq combination.
1528 */
1529 shutdown(info);
1530
1531 current_async = ports;
1532
1533 while (current_async) {
1534 if ((current_async->line >= info->line) &&
1535 (current_async->line < (info->line + 8)))
1536 current_async->irq = new_serial.irq;
1537
1538 current_async = current_async->next_port;
1539 }
1540
1541 serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
1542 if (info->irq == 9)
1543 serial_out(info, UART_ESI_CMD2, 0x02);
1544 else
1545 serial_out(info, UART_ESI_CMD2, info->irq);
1546 }
1547 }
1548
1549 if (info->flags & ASYNC_INITIALIZED) {
1550 if (((old_info.flags & ASYNC_SPD_MASK) !=
1551 (info->flags & ASYNC_SPD_MASK)) ||
1552 (old_info.custom_divisor != info->custom_divisor)) {
1553 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1554 info->tty->alt_speed = 57600;
1555 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1556 info->tty->alt_speed = 115200;
1557 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1558 info->tty->alt_speed = 230400;
1559 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1560 info->tty->alt_speed = 460800;
1561 change_speed(info);
1562 }
1563 } else
1564 retval = startup(info);
1565
1566 return retval;
1567 }
1568
1569 static int set_esp_config(struct esp_struct * info,
1570 struct hayes_esp_config __user * new_info)
1571 {
1572 struct hayes_esp_config new_config;
1573 unsigned int change_dma;
1574 int retval = 0;
1575 struct esp_struct *current_async;
1576
1577 /* Perhaps a non-sysadmin user should be able to do some of these */
1578 /* operations. I haven't decided yet. */
1579
1580 if (!capable(CAP_SYS_ADMIN))
1581 return -EPERM;
1582
1583 if (copy_from_user(&new_config, new_info, sizeof(new_config)))
1584 return -EFAULT;
1585
1586 if ((new_config.flow_on >= new_config.flow_off) ||
1587 (new_config.rx_trigger < 1) ||
1588 (new_config.tx_trigger < 1) ||
1589 (new_config.flow_off < 1) ||
1590 (new_config.flow_on < 1) ||
1591 (new_config.rx_trigger > 1023) ||
1592 (new_config.tx_trigger > 1023) ||
1593 (new_config.flow_off > 1023) ||
1594 (new_config.flow_on > 1023) ||
1595 (new_config.pio_threshold < 0) ||
1596 (new_config.pio_threshold > 1024))
1597 return -EINVAL;
1598
1599 if ((new_config.dma_channel != 1) && (new_config.dma_channel != 3))
1600 new_config.dma_channel = 0;
1601
1602 if (info->stat_flags & ESP_STAT_NEVER_DMA)
1603 change_dma = new_config.dma_channel;
1604 else
1605 change_dma = (new_config.dma_channel != dma);
1606
1607 if (change_dma) {
1608 if (new_config.dma_channel) {
1609 /* PIO mode to DMA mode transition OR */
1610 /* change current DMA channel */
1611
1612 current_async = ports;
1613
1614 while (current_async) {
1615 if (current_async == info) {
1616 if (current_async->count > 1)
1617 return -EBUSY;
1618 } else if (current_async->count)
1619 return -EBUSY;
1620
1621 current_async =
1622 current_async->next_port;
1623 }
1624
1625 shutdown(info);
1626 dma = new_config.dma_channel;
1627 info->stat_flags &= ~ESP_STAT_NEVER_DMA;
1628
1629 /* all ports must use the same DMA channel */
1630
1631 current_async = ports;
1632
1633 while (current_async) {
1634 esp_basic_init(current_async);
1635 current_async = current_async->next_port;
1636 }
1637 } else {
1638 /* DMA mode to PIO mode only */
1639
1640 if (info->count > 1)
1641 return -EBUSY;
1642
1643 shutdown(info);
1644 info->stat_flags |= ESP_STAT_NEVER_DMA;
1645 esp_basic_init(info);
1646 }
1647 }
1648
1649 info->config.pio_threshold = new_config.pio_threshold;
1650
1651 if ((new_config.flow_off != info->config.flow_off) ||
1652 (new_config.flow_on != info->config.flow_on)) {
1653 unsigned long flags;
1654
1655 info->config.flow_off = new_config.flow_off;
1656 info->config.flow_on = new_config.flow_on;
1657 save_flags(flags); cli();
1658 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1659 serial_out(info, UART_ESI_CMD2, new_config.flow_off >> 8);
1660 serial_out(info, UART_ESI_CMD2, new_config.flow_off);
1661 serial_out(info, UART_ESI_CMD2, new_config.flow_on >> 8);
1662 serial_out(info, UART_ESI_CMD2, new_config.flow_on);
1663 restore_flags(flags);
1664 }
1665
1666 if ((new_config.rx_trigger != info->config.rx_trigger) ||
1667 (new_config.tx_trigger != info->config.tx_trigger)) {
1668 unsigned long flags;
1669
1670 info->config.rx_trigger = new_config.rx_trigger;
1671 info->config.tx_trigger = new_config.tx_trigger;
1672 save_flags(flags); cli();
1673 serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
1674 serial_out(info, UART_ESI_CMD2,
1675 new_config.rx_trigger >> 8);
1676 serial_out(info, UART_ESI_CMD2, new_config.rx_trigger);
1677 serial_out(info, UART_ESI_CMD2,
1678 new_config.tx_trigger >> 8);
1679 serial_out(info, UART_ESI_CMD2, new_config.tx_trigger);
1680 restore_flags(flags);
1681 }
1682
1683 if (new_config.rx_timeout != info->config.rx_timeout) {
1684 unsigned long flags;
1685
1686 info->config.rx_timeout = new_config.rx_timeout;
1687 save_flags(flags); cli();
1688
1689 if (info->IER & UART_IER_RDI) {
1690 serial_out(info, UART_ESI_CMD1,
1691 ESI_SET_RX_TIMEOUT);
1692 serial_out(info, UART_ESI_CMD2,
1693 new_config.rx_timeout);
1694 }
1695
1696 restore_flags(flags);
1697 }
1698
1699 if (!(info->flags & ASYNC_INITIALIZED))
1700 retval = startup(info);
1701
1702 return retval;
1703 }
1704
1705 /*
1706 * get_lsr_info - get line status register info
1707 *
1708 * Purpose: Let user call ioctl() to get info when the UART physically
1709 * is emptied. On bus types like RS485, the transmitter must
1710 * release the bus after transmitting. This must be done when
1711 * the transmit shift register is empty, not be done when the
1712 * transmit holding register is empty. This functionality
1713 * allows an RS485 driver to be written in user space.
1714 */
1715 static int get_lsr_info(struct esp_struct * info, unsigned int __user *value)
1716 {
1717 unsigned char status;
1718 unsigned int result;
1719
1720 cli();
1721 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1722 status = serial_in(info, UART_ESI_STAT1);
1723 sti();
1724 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1725 return put_user(result,value);
1726 }
1727
1728
1729 static int esp_tiocmget(struct tty_struct *tty, struct file *file)
1730 {
1731 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1732 unsigned char control, status;
1733
1734 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1735 return -ENODEV;
1736 if (tty->flags & (1 << TTY_IO_ERROR))
1737 return -EIO;
1738
1739 control = info->MCR;
1740 cli();
1741 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1742 status = serial_in(info, UART_ESI_STAT2);
1743 sti();
1744 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1745 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1746 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1747 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1748 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1749 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1750 }
1751
1752 static int esp_tiocmset(struct tty_struct *tty, struct file *file,
1753 unsigned int set, unsigned int clear)
1754 {
1755 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1756
1757 if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1758 return -ENODEV;
1759 if (tty->flags & (1 << TTY_IO_ERROR))
1760 return -EIO;
1761
1762 cli();
1763
1764 if (set & TIOCM_RTS)
1765 info->MCR |= UART_MCR_RTS;
1766 if (set & TIOCM_DTR)
1767 info->MCR |= UART_MCR_DTR;
1768
1769 if (clear & TIOCM_RTS)
1770 info->MCR &= ~UART_MCR_RTS;
1771 if (clear & TIOCM_DTR)
1772 info->MCR &= ~UART_MCR_DTR;
1773
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);
1777 sti();
1778 return 0;
1779 }
1780
1781 /*
1782 * rs_break() --- routine which turns the break handling on or off
1783 */
1784 static void esp_break(struct tty_struct *tty, int break_state)
1785 {
1786 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1787 unsigned long flags;
1788
1789 if (serial_paranoia_check(info, tty->name, "esp_break"))
1790 return;
1791
1792 save_flags(flags); cli();
1793 if (break_state == -1) {
1794 serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1795 serial_out(info, UART_ESI_CMD2, 0x01);
1796
1797 interruptible_sleep_on(&info->break_wait);
1798 } else {
1799 serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1800 serial_out(info, UART_ESI_CMD2, 0x00);
1801 }
1802 restore_flags(flags);
1803 }
1804
1805 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1806 unsigned int cmd, unsigned long arg)
1807 {
1808 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1809 struct async_icount cprev, cnow; /* kernel counter temps */
1810 struct serial_icounter_struct __user *p_cuser; /* user space */
1811 void __user *argp = (void __user *)arg;
1812
1813 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1814 return -ENODEV;
1815
1816 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1817 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1818 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT) &&
1819 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT) &&
1820 (cmd != TIOCGHAYESESP) && (cmd != TIOCSHAYESESP)) {
1821 if (tty->flags & (1 << TTY_IO_ERROR))
1822 return -EIO;
1823 }
1824
1825 switch (cmd) {
1826 case TIOCGSERIAL:
1827 return get_serial_info(info, argp);
1828 case TIOCSSERIAL:
1829 return set_serial_info(info, argp);
1830 case TIOCSERCONFIG:
1831 /* do not reconfigure after initial configuration */
1832 return 0;
1833
1834 case TIOCSERGWILD:
1835 return put_user(0L, (unsigned long __user *)argp);
1836
1837 case TIOCSERGETLSR: /* Get line status register */
1838 return get_lsr_info(info, argp);
1839
1840 case TIOCSERSWILD:
1841 if (!capable(CAP_SYS_ADMIN))
1842 return -EPERM;
1843 return 0;
1844
1845 /*
1846 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1847 * - mask passed in arg for lines of interest
1848 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1849 * Caller should use TIOCGICOUNT to see which one it was
1850 */
1851 case TIOCMIWAIT:
1852 cli();
1853 cprev = info->icount; /* note the counters on entry */
1854 sti();
1855 while (1) {
1856 interruptible_sleep_on(&info->delta_msr_wait);
1857 /* see if a signal did it */
1858 if (signal_pending(current))
1859 return -ERESTARTSYS;
1860 cli();
1861 cnow = info->icount; /* atomic copy */
1862 sti();
1863 if (cnow.rng == cprev.rng &&
1864 cnow.dsr == cprev.dsr &&
1865 cnow.dcd == cprev.dcd &&
1866 cnow.cts == cprev.cts)
1867 return -EIO; /* no change => error */
1868 if (((arg & TIOCM_RNG) &&
1869 (cnow.rng != cprev.rng)) ||
1870 ((arg & TIOCM_DSR) &&
1871 (cnow.dsr != cprev.dsr)) ||
1872 ((arg & TIOCM_CD) &&
1873 (cnow.dcd != cprev.dcd)) ||
1874 ((arg & TIOCM_CTS) &&
1875 (cnow.cts != cprev.cts)) ) {
1876 return 0;
1877 }
1878 cprev = cnow;
1879 }
1880 /* NOTREACHED */
1881
1882 /*
1883 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1884 * Return: write counters to the user passed counter struct
1885 * NB: both 1->0 and 0->1 transitions are counted except for
1886 * RI where only 0->1 is counted.
1887 */
1888 case TIOCGICOUNT:
1889 cli();
1890 cnow = info->icount;
1891 sti();
1892 p_cuser = argp;
1893 if (put_user(cnow.cts, &p_cuser->cts) ||
1894 put_user(cnow.dsr, &p_cuser->dsr) ||
1895 put_user(cnow.rng, &p_cuser->rng) ||
1896 put_user(cnow.dcd, &p_cuser->dcd))
1897 return -EFAULT;
1898
1899 return 0;
1900 case TIOCGHAYESESP:
1901 return get_esp_config(info, argp);
1902 case TIOCSHAYESESP:
1903 return set_esp_config(info, argp);
1904
1905 default:
1906 return -ENOIOCTLCMD;
1907 }
1908 return 0;
1909 }
1910
1911 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1912 {
1913 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1914
1915 if ( (tty->termios->c_cflag == old_termios->c_cflag)
1916 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
1917 == RELEVANT_IFLAG(old_termios->c_iflag)))
1918 return;
1919
1920 change_speed(info);
1921
1922 /* Handle transition to B0 status */
1923 if ((old_termios->c_cflag & CBAUD) &&
1924 !(tty->termios->c_cflag & CBAUD)) {
1925 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1926 cli();
1927 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1928 serial_out(info, UART_ESI_CMD2, UART_MCR);
1929 serial_out(info, UART_ESI_CMD2, info->MCR);
1930 sti();
1931 }
1932
1933 /* Handle transition away from B0 status */
1934 if (!(old_termios->c_cflag & CBAUD) &&
1935 (tty->termios->c_cflag & CBAUD)) {
1936 info->MCR |= (UART_MCR_DTR | UART_MCR_RTS);
1937 cli();
1938 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1939 serial_out(info, UART_ESI_CMD2, UART_MCR);
1940 serial_out(info, UART_ESI_CMD2, info->MCR);
1941 sti();
1942 }
1943
1944 /* Handle turning of CRTSCTS */
1945 if ((old_termios->c_cflag & CRTSCTS) &&
1946 !(tty->termios->c_cflag & CRTSCTS)) {
1947 rs_start(tty);
1948 }
1949
1950 #if 0
1951 /*
1952 * No need to wake up processes in open wait, since they
1953 * sample the CLOCAL flag once, and don't recheck it.
1954 * XXX It's not clear whether the current behavior is correct
1955 * or not. Hence, this may change.....
1956 */
1957 if (!(old_termios->c_cflag & CLOCAL) &&
1958 (tty->termios->c_cflag & CLOCAL))
1959 wake_up_interruptible(&info->open_wait);
1960 #endif
1961 }
1962
1963 /*
1964 * ------------------------------------------------------------
1965 * rs_close()
1966 *
1967 * This routine is called when the serial port gets closed. First, we
1968 * wait for the last remaining data to be sent. Then, we unlink its
1969 * async structure from the interrupt chain if necessary, and we free
1970 * that IRQ if nothing is left in the chain.
1971 * ------------------------------------------------------------
1972 */
1973 static void rs_close(struct tty_struct *tty, struct file * filp)
1974 {
1975 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1976 unsigned long flags;
1977
1978 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1979 return;
1980
1981 save_flags(flags); cli();
1982
1983 if (tty_hung_up_p(filp)) {
1984 DBG_CNT("before DEC-hung");
1985 goto out;
1986 }
1987
1988 #ifdef SERIAL_DEBUG_OPEN
1989 printk("rs_close ttys%d, count = %d\n", info->line, info->count);
1990 #endif
1991 if ((tty->count == 1) && (info->count != 1)) {
1992 /*
1993 * Uh, oh. tty->count is 1, which means that the tty
1994 * structure will be freed. Info->count should always
1995 * be one in these conditions. If it's greater than
1996 * one, we've got real problems, since it means the
1997 * serial port won't be shutdown.
1998 */
1999 printk("rs_close: bad serial port count; tty->count is 1, "
2000 "info->count is %d\n", info->count);
2001 info->count = 1;
2002 }
2003 if (--info->count < 0) {
2004 printk("rs_close: bad serial port count for ttys%d: %d\n",
2005 info->line, info->count);
2006 info->count = 0;
2007 }
2008 if (info->count) {
2009 DBG_CNT("before DEC-2");
2010 goto out;
2011 }
2012 info->flags |= ASYNC_CLOSING;
2013 /*
2014 * Now we wait for the transmit buffer to clear; and we notify
2015 * the line discipline to only process XON/XOFF characters.
2016 */
2017 tty->closing = 1;
2018 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
2019 tty_wait_until_sent(tty, info->closing_wait);
2020 /*
2021 * At this point we stop accepting input. To do this, we
2022 * disable the receive line status interrupts, and tell the
2023 * interrupt driver to stop checking the data ready bit in the
2024 * line status register.
2025 */
2026 /* info->IER &= ~UART_IER_RLSI; */
2027 info->IER &= ~UART_IER_RDI;
2028 info->read_status_mask &= ~UART_LSR_DR;
2029 if (info->flags & ASYNC_INITIALIZED) {
2030 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
2031 serial_out(info, UART_ESI_CMD2, info->IER);
2032
2033 /* disable receive timeout */
2034 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
2035 serial_out(info, UART_ESI_CMD2, 0x00);
2036
2037 /*
2038 * Before we drop DTR, make sure the UART transmitter
2039 * has completely drained; this is especially
2040 * important if there is a transmit FIFO!
2041 */
2042 rs_wait_until_sent(tty, info->timeout);
2043 }
2044 shutdown(info);
2045 if (tty->driver->flush_buffer)
2046 tty->driver->flush_buffer(tty);
2047 tty_ldisc_flush(tty);
2048 tty->closing = 0;
2049 info->event = 0;
2050 info->tty = NULL;
2051
2052 if (info->blocked_open) {
2053 if (info->close_delay) {
2054 msleep_interruptible(jiffies_to_msecs(info->close_delay));
2055 }
2056 wake_up_interruptible(&info->open_wait);
2057 }
2058 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
2059 wake_up_interruptible(&info->close_wait);
2060 out:
2061 restore_flags(flags);
2062 }
2063
2064 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
2065 {
2066 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
2067 unsigned long orig_jiffies, char_time;
2068 unsigned long flags;
2069
2070 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
2071 return;
2072
2073 orig_jiffies = jiffies;
2074 char_time = ((info->timeout - HZ / 50) / 1024) / 5;
2075
2076 if (!char_time)
2077 char_time = 1;
2078
2079 save_flags(flags); cli();
2080 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2081 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2082
2083 while ((serial_in(info, UART_ESI_STAT1) != 0x03) ||
2084 (serial_in(info, UART_ESI_STAT2) != 0xff)) {
2085 msleep_interruptible(jiffies_to_msecs(char_time));
2086
2087 if (signal_pending(current))
2088 break;
2089
2090 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2091 break;
2092
2093 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2094 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2095 }
2096
2097 restore_flags(flags);
2098 set_current_state(TASK_RUNNING);
2099 }
2100
2101 /*
2102 * esp_hangup() --- called by tty_hangup() when a hangup is signaled.
2103 */
2104 static void esp_hangup(struct tty_struct *tty)
2105 {
2106 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
2107
2108 if (serial_paranoia_check(info, tty->name, "esp_hangup"))
2109 return;
2110
2111 rs_flush_buffer(tty);
2112 shutdown(info);
2113 info->event = 0;
2114 info->count = 0;
2115 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2116 info->tty = NULL;
2117 wake_up_interruptible(&info->open_wait);
2118 }
2119
2120 /*
2121 * ------------------------------------------------------------
2122 * esp_open() and friends
2123 * ------------------------------------------------------------
2124 */
2125 static int block_til_ready(struct tty_struct *tty, struct file * filp,
2126 struct esp_struct *info)
2127 {
2128 DECLARE_WAITQUEUE(wait, current);
2129 int retval;
2130 int do_clocal = 0;
2131 unsigned long flags;
2132
2133 /*
2134 * If the device is in the middle of being closed, then block
2135 * until it's done, and then try again.
2136 */
2137 if (tty_hung_up_p(filp) ||
2138 (info->flags & ASYNC_CLOSING)) {
2139 if (info->flags & ASYNC_CLOSING)
2140 interruptible_sleep_on(&info->close_wait);
2141 #ifdef SERIAL_DO_RESTART
2142 if (info->flags & ASYNC_HUP_NOTIFY)
2143 return -EAGAIN;
2144 else
2145 return -ERESTARTSYS;
2146 #else
2147 return -EAGAIN;
2148 #endif
2149 }
2150
2151 /*
2152 * If non-blocking mode is set, or the port is not enabled,
2153 * then make the check up front and then exit.
2154 */
2155 if ((filp->f_flags & O_NONBLOCK) ||
2156 (tty->flags & (1 << TTY_IO_ERROR))) {
2157 info->flags |= ASYNC_NORMAL_ACTIVE;
2158 return 0;
2159 }
2160
2161 if (tty->termios->c_cflag & CLOCAL)
2162 do_clocal = 1;
2163
2164 /*
2165 * Block waiting for the carrier detect and the line to become
2166 * free (i.e., not in use by the callout). While we are in
2167 * this loop, info->count is dropped by one, so that
2168 * rs_close() knows when to free things. We restore it upon
2169 * exit, either normal or abnormal.
2170 */
2171 retval = 0;
2172 add_wait_queue(&info->open_wait, &wait);
2173 #ifdef SERIAL_DEBUG_OPEN
2174 printk("block_til_ready before block: ttys%d, count = %d\n",
2175 info->line, info->count);
2176 #endif
2177 save_flags(flags);
2178 cli();
2179 if (!tty_hung_up_p(filp))
2180 info->count--;
2181 restore_flags(flags);
2182 info->blocked_open++;
2183 while (1) {
2184 save_flags(flags);
2185 cli();
2186 if ((tty->termios->c_cflag & CBAUD)) {
2187 unsigned int scratch;
2188
2189 serial_out(info, UART_ESI_CMD1, ESI_READ_UART);
2190 serial_out(info, UART_ESI_CMD2, UART_MCR);
2191 scratch = serial_in(info, UART_ESI_STAT1);
2192 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2193 serial_out(info, UART_ESI_CMD2, UART_MCR);
2194 serial_out(info, UART_ESI_CMD2,
2195 scratch | UART_MCR_DTR | UART_MCR_RTS);
2196 }
2197 restore_flags(flags);
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;
2210 }
2211
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;
2215
2216 if (!(info->flags & ASYNC_CLOSING) &&
2217 (do_clocal))
2218 break;
2219 if (signal_pending(current)) {
2220 retval = -ERESTARTSYS;
2221 break;
2222 }
2223 #ifdef SERIAL_DEBUG_OPEN
2224 printk("block_til_ready blocking: ttys%d, count = %d\n",
2225 info->line, info->count);
2226 #endif
2227 schedule();
2228 }
2229 set_current_state(TASK_RUNNING);
2230 remove_wait_queue(&info->open_wait, &wait);
2231 if (!tty_hung_up_p(filp))
2232 info->count++;
2233 info->blocked_open--;
2234 #ifdef SERIAL_DEBUG_OPEN
2235 printk("block_til_ready after blocking: ttys%d, count = %d\n",
2236 info->line, info->count);
2237 #endif
2238 if (retval)
2239 return retval;
2240 info->flags |= ASYNC_NORMAL_ACTIVE;
2241 return 0;
2242 }
2243
2244 /*
2245 * This routine is called whenever a serial port is opened. It
2246 * enables interrupts for a serial port, linking in its async structure into
2247 * the IRQ chain. It also performs the serial-specific
2248 * initialization for the tty structure.
2249 */
2250 static int esp_open(struct tty_struct *tty, struct file * filp)
2251 {
2252 struct esp_struct *info;
2253 int retval, line;
2254
2255 line = tty->index;
2256 if ((line < 0) || (line >= NR_PORTS))
2257 return -ENODEV;
2258
2259 /* find the port in the chain */
2260
2261 info = ports;
2262
2263 while (info && (info->line != line))
2264 info = info->next_port;
2265
2266 if (!info) {
2267 serial_paranoia_check(info, tty->name, "esp_open");
2268 return -ENODEV;
2269 }
2270
2271 #ifdef SERIAL_DEBUG_OPEN
2272 printk("esp_open %s, count = %d\n", tty->name, info->count);
2273 #endif
2274 info->count++;
2275 tty->driver_data = info;
2276 info->tty = tty;
2277
2278 if (!tmp_buf) {
2279 tmp_buf = (unsigned char *) get_zeroed_page(GFP_KERNEL);
2280 if (!tmp_buf)
2281 return -ENOMEM;
2282 }
2283
2284 /*
2285 * Start up serial port
2286 */
2287 retval = startup(info);
2288 if (retval)
2289 return retval;
2290
2291 retval = block_til_ready(tty, filp, info);
2292 if (retval) {
2293 #ifdef SERIAL_DEBUG_OPEN
2294 printk("esp_open returning after block_til_ready with %d\n",
2295 retval);
2296 #endif
2297 return retval;
2298 }
2299
2300 #ifdef SERIAL_DEBUG_OPEN
2301 printk("esp_open %s successful...", tty->name);
2302 #endif
2303 return 0;
2304 }
2305
2306 /*
2307 * ---------------------------------------------------------------------
2308 * espserial_init() and friends
2309 *
2310 * espserial_init() is called at boot-time to initialize the serial driver.
2311 * ---------------------------------------------------------------------
2312 */
2313
2314 /*
2315 * This routine prints out the appropriate serial driver version
2316 * number, and identifies which options were configured into this
2317 * driver.
2318 */
2319
2320 static _INLINE_ void show_serial_version(void)
2321 {
2322 printk(KERN_INFO "%s version %s (DMA %u)\n",
2323 serial_name, serial_version, dma);
2324 }
2325
2326 /*
2327 * This routine is called by espserial_init() to initialize a specific serial
2328 * port.
2329 */
2330 static _INLINE_ int autoconfig(struct esp_struct * info)
2331 {
2332 int port_detected = 0;
2333 unsigned long flags;
2334
2335 if (!request_region(info->port, REGION_SIZE, "esp serial"))
2336 return -EIO;
2337
2338 save_flags(flags); cli();
2339
2340 /*
2341 * Check for ESP card
2342 */
2343
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);
2347
2348 if ((serial_in(info, UART_ESI_STAT2) & 0x70) == 0x20) {
2349 port_detected = 1;
2350
2351 if (!(info->irq)) {
2352 serial_out(info, UART_ESI_CMD1, 0x02);
2353
2354 if (serial_in(info, UART_ESI_STAT1) & 0x01)
2355 info->irq = 3;
2356 else
2357 info->irq = 4;
2358 }
2359
2360
2361 /* put card in enhanced mode */
2362 /* this prevents access through */
2363 /* the "old" IO ports */
2364 esp_basic_init(info);
2365
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);
2370 }
2371 }
2372 if (!port_detected)
2373 release_region(info->port, REGION_SIZE);
2374
2375 restore_flags(flags);
2376 return (port_detected);
2377 }
2378
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,
2399 };
2400
2401 /*
2402 * The serial driver boot-time initialization code!
2403 */
2404 static int __init espserial_init(void)
2405 {
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};
2410
2411 esp_driver = alloc_tty_driver(NR_PORTS);
2412 if (!esp_driver)
2413 return -ENOMEM;
2414
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;
2422 }
2423 }
2424
2425 if ((dma != 1) && (dma != 3))
2426 dma = 0;
2427
2428 if ((rx_trigger < 1) || (rx_trigger > 1023))
2429 rx_trigger = 768;
2430
2431 if ((tx_trigger < 1) || (tx_trigger > 1023))
2432 tx_trigger = 768;
2433
2434 if ((flow_off < 1) || (flow_off > 1023))
2435 flow_off = 1016;
2436
2437 if ((flow_on < 1) || (flow_on > 1023))
2438 flow_on = 944;
2439
2440 if ((rx_timeout < 0) || (rx_timeout > 255))
2441 rx_timeout = 128;
2442
2443 if (flow_on >= flow_off)
2444 flow_on = flow_off - 1;
2445
2446 show_serial_version();
2447
2448 /* Initialize the tty_driver structure */
2449
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))
2463 {
2464 printk(KERN_ERR "Couldn't register esp serial driver");
2465 put_tty_driver(esp_driver);
2466 return 1;
2467 }
2468
2469 info = kmalloc(sizeof(struct esp_struct), GFP_KERNEL);
2470
2471 if (!info)
2472 {
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;
2477 }
2478
2479 memset((void *)info, 0, sizeof(struct esp_struct));
2480 /* rx_trigger, tx_trigger are needed by autoconfig */
2481 info->config.rx_trigger = rx_trigger;
2482 info->config.tx_trigger = tx_trigger;
2483
2484 i = 0;
2485 offset = 0;
2486
2487 do {
2488 info->port = esp[i] + offset;
2489 info->irq = irq[i];
2490 info->line = (i * 8) + (offset / 8);
2491
2492 if (!autoconfig(info)) {
2493 i++;
2494 offset = 0;
2495 continue;
2496 }
2497
2498 info->custom_divisor = (divisor[i] >> (offset / 2)) & 0xf;
2499 info->flags = STD_COM_FLAGS;
2500 if (info->custom_divisor)
2501 info->flags |= ASYNC_SPD_CUST;
2502 info->magic = ESP_MAGIC;
2503 info->close_delay = 5*HZ/10;
2504 info->closing_wait = 30*HZ;
2505 INIT_WORK(&info->tqueue, do_softint, info);
2506 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
2507 info->config.rx_timeout = rx_timeout;
2508 info->config.flow_on = flow_on;
2509 info->config.flow_off = flow_off;
2510 info->config.pio_threshold = pio_threshold;
2511 info->next_port = ports;
2512 init_waitqueue_head(&info->open_wait);
2513 init_waitqueue_head(&info->close_wait);
2514 init_waitqueue_head(&info->delta_msr_wait);
2515 init_waitqueue_head(&info->break_wait);
2516 ports = info;
2517 printk(KERN_INFO "ttyP%d at 0x%04x (irq = %d) is an ESP ",
2518 info->line, info->port, info->irq);
2519
2520 if (info->line % 8) {
2521 printk("secondary port\n");
2522 /* 8 port cards can't do DMA */
2523 info->stat_flags |= ESP_STAT_NEVER_DMA;
2524
2525 if (last_primary)
2526 last_primary->stat_flags |= ESP_STAT_NEVER_DMA;
2527 } else {
2528 printk("primary port\n");
2529 last_primary = info;
2530 irq[i] = info->irq;
2531 }
2532
2533 if (!dma)
2534 info->stat_flags |= ESP_STAT_NEVER_DMA;
2535
2536 info = kmalloc(sizeof(struct esp_struct), GFP_KERNEL);
2537 if (!info)
2538 {
2539 printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
2540
2541 /* allow use of the already detected ports */
2542 return 0;
2543 }
2544
2545 memset((void *)info, 0, sizeof(struct esp_struct));
2546 /* rx_trigger, tx_trigger are needed by autoconfig */
2547 info->config.rx_trigger = rx_trigger;
2548 info->config.tx_trigger = tx_trigger;
2549
2550 if (offset == 56) {
2551 i++;
2552 offset = 0;
2553 } else {
2554 offset += 8;
2555 }
2556 } while (i < NR_PRIMARY);
2557
2558 /* free the last port memory allocation */
2559 kfree(info);
2560
2561 return 0;
2562 }
2563
2564 static void __exit espserial_exit(void)
2565 {
2566 unsigned long flags;
2567 int e1;
2568 struct esp_struct *temp_async;
2569 struct esp_pio_buffer *pio_buf;
2570
2571 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2572 save_flags(flags);
2573 cli();
2574 if ((e1 = tty_unregister_driver(esp_driver)))
2575 printk("SERIAL: failed to unregister serial driver (%d)\n",
2576 e1);
2577 restore_flags(flags);
2578 put_tty_driver(esp_driver);
2579
2580 while (ports) {
2581 if (ports->port) {
2582 release_region(ports->port, REGION_SIZE);
2583 }
2584 temp_async = ports->next_port;
2585 kfree(ports);
2586 ports = temp_async;
2587 }
2588
2589 if (dma_buffer)
2590 free_pages((unsigned long)dma_buffer,
2591 get_order(DMA_BUFFER_SZ));
2592
2593 if (tmp_buf)
2594 free_page((unsigned long)tmp_buf);
2595
2596 while (free_pio_buf) {
2597 pio_buf = free_pio_buf->next;
2598 kfree(free_pio_buf);
2599 free_pio_buf = pio_buf;
2600 }
2601 }
2602
2603 module_init(espserial_init);
2604 module_exit(espserial_exit);
2605
|
This page was automatically generated by the
LXR engine.
|