Import 2.1.15
[davej-history.git] / drivers / char / esp.c
blobeea6ab2409bbec47b9819f475181ad894f2f8260
1 /*
2 * esp.c - driver for Hayes ESP serial cards
4 * --- Notices from serial.c, upon which this driver is based ---
6 * Copyright (C) 1991, 1992 Linus Torvalds
8 * Extensively rewritten by Theodore Ts'o, 8/16/92 -- 9/14/92. Now
9 * much more extensible to support other serial cards based on the
10 * 16450/16550A UART's. Added support for the AST FourPort and the
11 * Accent Async board.
13 * set_serial_info fixed to set the flags, custom divisor, and uart
14 * type fields. Fix suggested by Michael K. Johnson 12/12/92.
16 * 11/95: TIOCMIWAIT, TIOCGICOUNT by Angelo Haritsis <ah@doc.ic.ac.uk>
18 * 03/96: Modularised by Angelo Haritsis <ah@doc.ic.ac.uk>
20 * rs_set_termios fixed to look also for changes of the input
21 * flags INPCK, BRKINT, PARMRK, IGNPAR and IGNBRK.
22 * Bernd Anhäupl 05/17/96.
24 * --- End of notices from serial.c ---
26 * Support for the ESP serial card by Andrew J. Robinson
27 * <arobinso@nyx.net> (Card detection routine taken from a patch
28 * by Dennis J. Boylan). Patches to allow use with 2.1.x contributed
29 * by Chris Faylor.
31 * This module exports the following rs232 io functions:
33 * int esp_init(void);
36 #include <linux/module.h>
37 #include <linux/errno.h>
38 #include <linux/signal.h>
39 #include <linux/sched.h>
40 #include <linux/interrupt.h>
41 #include <linux/tty.h>
42 #include <linux/tty_flip.h>
43 #include <linux/serial.h>
44 #include <linux/serial_reg.h>
45 #include <linux/config.h>
46 #include <linux/major.h>
47 #include <linux/string.h>
48 #include <linux/fcntl.h>
49 #include <linux/ptrace.h>
50 #include <linux/ioport.h>
51 #include <linux/mm.h>
53 #include <asm/system.h>
54 #include <asm/io.h>
55 #include <asm/segment.h>
56 #include <asm/bitops.h>
58 #include <asm/dma.h>
59 #include <linux/malloc.h>
60 #include <asm/uaccess.h>
62 #include"esp.h"
64 #define NR_PORTS 64/* maximum number of ports */
65 #define NR_PRIMARY 8/* maximum number of primary ports */
67 /* The following variables can be set by giving module options */
68 static int irq[NR_PRIMARY] = {0,0,0,0,0,0,0,0};/* IRQ for each base port */
69 static unsigned int divisor[NR_PRIMARY] = {0,0,0,0,0,0,0,0};
70 /* custom divisor for each port */
71 static unsigned int dma = CONFIG_ESP_DMA_CHANNEL;/* DMA channel */
72 static unsigned int trigger = CONFIG_ESP_TRIGGER_LEVEL;/* FIFO trigger level */
73 /* END */
75 static char*dma_buffer;
77 #define DMA_BUFFER_SZ 1024
79 #define WAKEUP_CHARS 1024
81 static char*serial_name ="ESP driver";
82 static char*serial_version ="1.0";
84 DECLARE_TASK_QUEUE(tq_esp);
86 static struct tty_driver esp_driver, esp_callout_driver;
87 static int serial_refcount;
89 /* serial subtype definitions */
90 #define SERIAL_TYPE_NORMAL 1
91 #define SERIAL_TYPE_CALLOUT 2
94 * Serial driver configuration section. Here are the various options:
96 * SERIAL_PARANOIA_CHECK
97 * Check the magic number for the esp_structure where
98 * ever possible.
101 #undef SERIAL_PARANOIA_CHECK
102 #define SERIAL_DO_RESTART
104 #undef SERIAL_DEBUG_INTR
105 #undef SERIAL_DEBUG_OPEN
106 #undef SERIAL_DEBUG_FLOW
108 #define _INLINE_ inline
110 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
111 #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
112 kdevname(tty->device), (info->flags), serial_refcount,info->count,tty->count,s)
113 #else
114 #define DBG_CNT(s)
115 #endif
117 static struct esp_struct *IRQ_ports[16];
119 static voidautoconfig(struct esp_struct * info);
120 static voidchange_speed(struct esp_struct *info);
123 * This assumes you have a 1.8432 MHz clock for your UART.
125 * It'd be nice if someone built a serial card with a 24.576 MHz
126 * clock, since the 16550A is capable of handling a top speed of 1.5
127 * megabits/second; but this requires the faster clock.
129 #define BASE_BAUD ((1843200 / 16) * (1 << ESPC_SCALE))
131 /* Standard COM flags (except for COM4, because of the 8514 problem) */
132 #define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
134 static struct tty_struct *serial_table[NR_PORTS];
135 static struct termios *serial_termios[NR_PORTS];
136 static struct termios *serial_termios_locked[NR_PORTS];
138 #ifndef MIN
139 #define MIN(a,b) ((a) < (b) ? (a) : (b))
140 #endif
143 * tmp_buf is used as a temporary buffer by serial_write. We need to
144 * lock it in case the memcpy_fromfs blocks while swapping in a page,
145 * and some other program tries to do a serial write at the same time.
146 * Since the lock will only come under contention when the system is
147 * swapping and available memory is low, it makes sense to share one
148 * buffer across all the serial ports, since it significantly saves
149 * memory if large numbers of serial ports are open.
151 static unsigned char*tmp_buf =0;
152 static struct semaphore tmp_buf_sem = MUTEX;
154 staticinlineintserial_paranoia_check(struct esp_struct *info,
155 kdev_t device,const char*routine)
157 #ifdef SERIAL_PARANOIA_CHECK
158 static const char*badmagic =
159 "Warning: bad magic number for serial struct (%s) in %s\n";
160 static const char*badinfo =
161 "Warning: null esp_struct for (%s) in %s\n";
163 if(!info) {
164 printk(badinfo,kdevname(device), routine);
165 return1;
167 if(info->magic != ESP_MAGIC) {
168 printk(badmagic,kdevname(device), routine);
169 return1;
171 #endif
172 return0;
176 * This is used to figure out the divisor speeds and the timeouts
178 static int baud_table[] = {
179 0,50,75,110,134,150,200,300,600,1200,1800,2400,4800,
180 9600,19200,38400,57600,115200,230400,460800,0};
182 staticinlineunsigned intserial_in(struct esp_struct *info,int offset)
184 returninb(info->port + offset);
187 staticinlinevoidserial_out(struct esp_struct *info,int offset,int value)
189 outb(value, info->port+offset);
192 staticinlineint__get_order(unsigned long size)
194 int order;
196 size = (size + PAGE_SIZE -1) >> PAGE_SHIFT;
197 order = -1;
199 size >>=1;
200 order++;
201 }while(size);
203 return order;
207 * ------------------------------------------------------------
208 * rs_stop() and rs_start()
210 * This routines are called before setting or resetting tty->stopped.
211 * They enable or disable transmitter interrupts, as necessary.
212 * ------------------------------------------------------------
214 static voidrs_stop(struct tty_struct *tty)
216 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
217 unsigned long flags;
219 if(serial_paranoia_check(info, tty->device,"rs_stop"))
220 return;
222 save_flags(flags);cli();
223 if(info->IER & UART_IER_THRI) {
224 info->IER &= ~UART_IER_THRI;
225 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
226 serial_out(info, UART_ESI_CMD2, info->IER);
229 restore_flags(flags);
232 static voidrs_start(struct tty_struct *tty)
234 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
235 unsigned long flags;
237 if(serial_paranoia_check(info, tty->device,"rs_start"))
238 return;
240 save_flags(flags);cli();
241 if(info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
242 info->IER |= UART_IER_THRI;
243 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
244 serial_out(info, UART_ESI_CMD2, info->IER);
246 restore_flags(flags);
250 * ----------------------------------------------------------------------
252 * Here starts the interrupt handling routines. All of the following
253 * subroutines are declared as inline and are folded into
254 * rs_interrupt(). They were separated out for readability's sake.
256 * Note: rs_interrupt() is a "fast" interrupt, which means that it
257 * runs with interrupts turned off. People who may want to modify
258 * rs_interrupt() should try to keep the interrupt handler as fast as
259 * possible. After you are done making modifications, it is not a bad
260 * idea to do:
262 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
264 * and look at the resulting assemble code in serial.s.
266 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
267 * -----------------------------------------------------------------------
271 * This routine is used by the interrupt handler to schedule
272 * processing in the software interrupt portion of the driver.
274 static _INLINE_ voidrs_sched_event(struct esp_struct *info,
275 int event)
277 info->event |=1<< event;
278 queue_task_irq_off(&info->tqueue, &tq_esp);
279 mark_bh(ESP_BH);
282 static _INLINE_ voidreceive_chars_dma(struct esp_struct *info,int*dma_bytes,
283 int*dma_direction,unsigned int*who_dma)
285 unsigned int num_chars;
287 if(*dma_bytes) {
288 info->stat_flags |= STAT_NEED_DMA;
289 return;
292 info->stat_flags &= ~(STAT_RX_TIMEOUT | STAT_NEED_DMA);
294 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
295 serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
296 num_chars =serial_in(info, UART_ESI_STAT1) <<8;
297 num_chars |=serial_in(info, UART_ESI_STAT2);
299 if(!num_chars)
300 return;
302 *dma_bytes = num_chars;
303 *dma_direction = DMA_MODE_READ;
304 *who_dma = info->port;
305 disable_dma(dma);
306 clear_dma_ff(dma);
307 set_dma_mode(dma, DMA_MODE_READ);
308 set_dma_addr(dma,virt_to_bus(dma_buffer));
309 set_dma_count(dma, num_chars);
310 enable_dma(dma);
311 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_RX);
314 static voiddo_ttybuf(void*private_)
316 struct esp_struct *info = (struct esp_struct *) private_;
317 struct tty_struct *tty;
318 int avail_bytes, x_bytes;
319 unsigned long int flags;
321 save_flags(flags);cli();
322 tty = info->tty;
324 if(!tty) {
325 restore_flags(flags);
326 return;
329 avail_bytes = TTY_FLIPBUF_SIZE - tty->flip.count;
331 if(avail_bytes) {
332 if(info->tty_buf->count < avail_bytes)
333 x_bytes = info->tty_buf->count;
334 else
335 x_bytes = avail_bytes;
337 tty->flip.count += x_bytes;
338 memcpy(tty->flip.char_buf_ptr, info->tty_buf->char_buf,
339 x_bytes);
340 memcpy(tty->flip.flag_buf_ptr, info->tty_buf->flag_buf,
341 x_bytes);
342 tty->flip.char_buf_ptr += x_bytes;
343 tty->flip.flag_buf_ptr += x_bytes;
344 info->tty_buf->count -= x_bytes;
345 info->tty_buf->char_buf_ptr -= x_bytes;
346 info->tty_buf->flag_buf_ptr -= x_bytes;
348 if(info->tty_buf->count) {
349 memmove(info->tty_buf->char_buf,
350 info->tty_buf->char_buf + x_bytes,
351 info->tty_buf->count);
352 queue_task_irq_off(&info->tty_buf->tqueue,
353 &tq_timer);
356 queue_task_irq_off(&tty->flip.tqueue, &tq_timer);
357 }else{
358 queue_task_irq_off(&info->tty_buf->tqueue, &tq_timer);
361 restore_flags(flags);
364 static _INLINE_ voidreceive_chars_dma_done(struct esp_struct *info,
365 int*dma_bytes,int*dma_direction,unsigned int*who_dma,int status)
367 struct tty_struct *tty = info->tty;
368 int num_bytes, bytes_left, x_bytes;
369 struct tty_flip_buffer *buffer;
371 if(!(*dma_bytes) || (*who_dma != info->port))
372 return;
374 disable_dma(dma);
375 clear_dma_ff(dma);
377 num_bytes = *dma_bytes -get_dma_residue(dma);
379 buffer = &(tty->flip);
380 bytes_left = num_bytes;
382 if(info->tty_buf->count && (tty->flip.count < TTY_FLIPBUF_SIZE))
383 do_ttybuf(info);
385 while(bytes_left >0) {
386 if((buffer->count + bytes_left) > TTY_FLIPBUF_SIZE)
387 x_bytes = TTY_FLIPBUF_SIZE - buffer->count;
388 else
389 x_bytes = bytes_left;
391 memcpy(buffer->char_buf_ptr,
392 dma_buffer + (num_bytes - bytes_left), x_bytes);
393 buffer->char_buf_ptr += x_bytes;
394 buffer->count += x_bytes;
395 memset(buffer->flag_buf_ptr,0, x_bytes);
396 buffer->flag_buf_ptr += x_bytes;
397 bytes_left -= x_bytes;
399 if(bytes_left >0) {
400 if(buffer == info->tty_buf)
401 break;
402 else
403 buffer = info->tty_buf;
407 if(num_bytes >0) {
408 buffer->flag_buf_ptr--;
410 status >>=8;
411 status &= (0x1c& info->read_status_mask);
413 if(status & info->ignore_status_mask) {
414 buffer->count--;
415 buffer->char_buf_ptr--;
416 buffer->flag_buf_ptr--;
417 }else if(status &0x10) {
418 *buffer->flag_buf_ptr = TTY_BREAK;
419 if(info->flags & ASYNC_SAK)
420 do_SAK(tty);
421 }else if(status &0x08)
422 *buffer->flag_buf_ptr = TTY_FRAME;
423 else if(status &0x04)
424 *buffer->flag_buf_ptr = TTY_PARITY;
426 buffer->flag_buf_ptr++;
428 if(buffer == info->tty_buf)
429 queue_task_irq_off(&info->tty_buf->tqueue, &tq_timer);
431 queue_task_irq_off(&tty->flip.tqueue, &tq_timer);
434 if(*dma_bytes != num_bytes) {
435 *dma_bytes =0;
436 receive_chars_dma(info, dma_bytes, dma_direction, who_dma);
437 }else
438 *dma_bytes =0;
441 static _INLINE_ voidtransmit_chars_dma(struct esp_struct *info,int*dma_bytes,
442 int*dma_direction,unsigned int*who_dma)
444 int count;
446 if(*dma_bytes) {
447 info->stat_flags |= STAT_NEED_DMA;
448 return;
451 info->stat_flags &= ~STAT_NEED_DMA;
453 if((info->xmit_cnt <=0) || info->tty->stopped ||
454 info->tty->hw_stopped) {
455 info->IER &= ~UART_IER_THRI;
456 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
457 serial_out(info, UART_ESI_CMD2, info->IER);
458 return;
461 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
462 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
463 count =serial_in(info, UART_ESI_STAT1) <<8;
464 count |=serial_in(info, UART_ESI_STAT2);
466 if(!count)
467 return;
469 count = ((count < info->xmit_cnt) ? count : info->xmit_cnt);
471 if(info->xmit_tail + count <= ESP_XMIT_SIZE) {
472 memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
473 count);
474 }else{
475 int i = ESP_XMIT_SIZE - info->xmit_tail;
476 memcpy(dma_buffer, &(info->xmit_buf[info->xmit_tail]),
478 memcpy(&(dma_buffer[i]), info->xmit_buf, count - i);
481 info->xmit_cnt -= count;
482 info->xmit_tail = (info->xmit_tail + count) & (ESP_XMIT_SIZE -1);
484 if(info->xmit_cnt < WAKEUP_CHARS)
485 rs_sched_event(info, ESP_EVENT_WRITE_WAKEUP);
487 #ifdef SERIAL_DEBUG_INTR
488 printk("THRE...");
489 #endif
491 if(info->xmit_cnt <=0) {
492 info->IER &= ~UART_IER_THRI;
493 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
494 serial_out(info, UART_ESI_CMD2, info->IER);
497 *dma_bytes = count;
498 *dma_direction = DMA_MODE_WRITE;
499 *who_dma = info->port;
500 disable_dma(dma);
501 clear_dma_ff(dma);
502 set_dma_mode(dma, DMA_MODE_WRITE);
503 set_dma_addr(dma,virt_to_bus(dma_buffer));
504 set_dma_count(dma, count);
505 enable_dma(dma);
506 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
509 static _INLINE_ voidtransmit_chars_dma_done(struct esp_struct *info,
510 int*dma_bytes,int*dma_direction,unsigned int*who_dma)
512 int num_bytes;
514 if(!(*dma_bytes) || (*who_dma != info->port))
515 return;
517 disable_dma(dma);
518 clear_dma_ff(dma);
520 num_bytes = *dma_bytes -get_dma_residue(dma);
522 if(*dma_bytes != num_bytes)
524 *dma_bytes -= num_bytes;
525 memmove(dma_buffer, dma_buffer + num_bytes, *dma_bytes);
526 *dma_direction = DMA_MODE_WRITE;
527 disable_dma(dma);
528 clear_dma_ff(dma);
529 set_dma_mode(dma, DMA_MODE_WRITE);
530 set_dma_addr(dma,virt_to_bus(dma_buffer));
531 set_dma_count(dma, *dma_bytes);
532 enable_dma(dma);
533 serial_out(info, UART_ESI_CMD1, ESI_START_DMA_TX);
534 }else
535 *dma_bytes =0;
538 static _INLINE_ voidcheck_modem_status(struct esp_struct *info)
540 int status;
542 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
543 status =serial_in(info, UART_ESI_STAT2);
545 if(status & UART_MSR_ANY_DELTA) {
546 /* update input line counters */
547 if(status & UART_MSR_TERI)
548 info->icount.rng++;
549 if(status & UART_MSR_DDSR)
550 info->icount.dsr++;
551 if(status & UART_MSR_DDCD)
552 info->icount.dcd++;
553 if(status & UART_MSR_DCTS)
554 info->icount.cts++;
555 wake_up_interruptible(&info->delta_msr_wait);
558 if((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
559 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
560 printk("ttys%d CD now %s...", info->line,
561 (status & UART_MSR_DCD) ?"on":"off");
562 #endif
563 if(status & UART_MSR_DCD)
564 wake_up_interruptible(&info->open_wait);
565 else if(!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
566 (info->flags & ASYNC_CALLOUT_NOHUP))) {
567 #ifdef SERIAL_DEBUG_OPEN
568 printk("scheduling hangup...");
569 #endif
570 queue_task_irq_off(&info->tqueue_hangup,
571 &tq_scheduler);
576 static _INLINE_ voidtx_flowed_on(struct esp_struct *info)
578 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
579 printk("CTS tx start...");
580 #endif
581 info->tty->hw_stopped =0;
582 info->IER |= UART_IER_THRI;
583 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);/* set mask */
584 serial_out(info, UART_ESI_CMD2, info->IER);
585 rs_sched_event(info, ESP_EVENT_WRITE_WAKEUP);
588 static _INLINE_ voidtx_flowed_off(struct esp_struct *info)
590 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
591 printk("CTS tx stop...");
592 #endif
593 info->tty->hw_stopped =1;
594 info->IER &= ~UART_IER_THRI;
595 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);/* set mask */
596 serial_out(info, UART_ESI_CMD2, info->IER);
600 * This is the serial driver's interrupt routine for a single port
602 static voidrs_interrupt_single(int irq,void*dev_id,struct pt_regs * regs)
604 struct esp_struct * info, *stop_port;
605 unsigned err_status;
606 unsigned int scratch;
607 int pre_bytes;
608 int check_dma_only =0;
609 static int dma_bytes;
610 static int dma_direction;
611 static int who_dma;
613 #ifdef SERIAL_DEBUG_INTR
614 printk("rs_interrupt_single(%d)...", irq);
615 #endif
617 /* This routine will currently check ALL ports when an interrupt */
618 /* is received from ANY port */
620 stop_port = info = IRQ_ports[irq];
622 if(!info)
623 return;
626 if(!info->tty || (check_dma_only &&
627 !(info->stat_flags & STAT_NEED_DMA))) {
628 info = info->next_port;
629 continue;
632 pre_bytes = dma_bytes;
633 err_status =0;
634 scratch =serial_in(info, UART_ESI_SID);
635 if(scratch &0x04) {/* error - check for rx timeout */
636 serial_out(info, UART_ESI_CMD1, ESI_GET_ERR_STAT);
637 err_status =serial_in(info, UART_ESI_STAT1) <<8;
638 err_status |=serial_in(info, UART_ESI_STAT2);
640 if(err_status &0x0100)
641 info->stat_flags |= STAT_RX_TIMEOUT;
643 if(err_status &0x2000)/* UART status */
644 check_modem_status(info);
646 if(err_status &0x8000)/* Start break */
647 wake_up_interruptible(&info->break_wait);
649 if(err_status &0x0002)/* tx off */
650 tx_flowed_off(info);
652 if(err_status &0x0004)/* tx on */
653 tx_flowed_on(info);
656 if((scratch &0x88) ||/* DMA completed or timed out */
657 (err_status &0x1c00)/* receive error */)
658 if(dma_direction == DMA_MODE_READ)
659 receive_chars_dma_done(info, &dma_bytes,
660 &dma_direction, &who_dma, err_status);
661 else
662 transmit_chars_dma_done(info, &dma_bytes,
663 &dma_direction, &who_dma);
665 if(((scratch &0x01) ||
666 (info->stat_flags & STAT_RX_TIMEOUT)) &&
667 (info->IER & UART_IER_RDI))
668 receive_chars_dma(info, &dma_bytes, &dma_direction,
669 &who_dma);
671 if((scratch &0x02) && (info->IER & UART_IER_THRI))
672 transmit_chars_dma(info, &dma_bytes, &dma_direction,
673 &who_dma);
675 info->last_active = jiffies;
677 if(pre_bytes && !dma_bytes)/* released DMA */
678 stop_port = info;
680 info = info->next_port;
682 if((info->irq != irq) || (info == IRQ_ports[irq]))
683 check_dma_only =1;
685 if(check_dma_only && dma_bytes)
686 info = stop_port;
687 }while(info != stop_port);
689 #ifdef SERIAL_DEBUG_INTR
690 printk("end.\n");
691 #endif
695 * -------------------------------------------------------------------
696 * Here ends the serial interrupt routines.
697 * -------------------------------------------------------------------
701 * This routine is used to handle the "bottom half" processing for the
702 * serial driver, known also the "software interrupt" processing.
703 * This processing is done at the kernel interrupt level, after the
704 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
705 * is where time-consuming activities which can not be done in the
706 * interrupt driver proper are done; the interrupt driver schedules
707 * them using rs_sched_event(), and they get done here.
709 static voiddo_serial_bh(void)
711 run_task_queue(&tq_esp);
714 static voiddo_softint(void*private_)
716 struct esp_struct *info = (struct esp_struct *) private_;
717 struct tty_struct *tty;
719 tty = info->tty;
720 if(!tty)
721 return;
723 if(clear_bit(ESP_EVENT_WRITE_WAKEUP, &info->event)) {
724 if((tty->flags & (1<< TTY_DO_WRITE_WAKEUP)) &&
725 tty->ldisc.write_wakeup)
726 (tty->ldisc.write_wakeup)(tty);
727 wake_up_interruptible(&tty->write_wait);
732 * This routine is called from the scheduler tqueue when the interrupt
733 * routine has signalled that a hangup has occurred. The path of
734 * hangup processing is:
736 * serial interrupt routine -> (scheduler tqueue) ->
737 * do_serial_hangup() -> tty->hangup() -> esp_hangup()
740 static voiddo_serial_hangup(void*private_)
742 struct esp_struct *info = (struct esp_struct *) private_;
743 struct tty_struct *tty;
745 tty = info->tty;
746 if(!tty)
747 return;
749 tty_hangup(tty);
753 * ---------------------------------------------------------------
754 * Low level utility subroutines for the serial driver: routines to
755 * figure out the appropriate timeout for an interrupt chain, routines
756 * to initialize and startup a serial port, and routines to shutdown a
757 * serial port. Useful stuff like that.
758 * ---------------------------------------------------------------
761 static voidesp_basic_init(struct esp_struct * info)
763 /* put ESPC in enhanced mode */
764 serial_out(info, UART_ESI_CMD1, ESI_SET_MODE);
765 serial_out(info, UART_ESI_CMD2,0x31);
767 /* disable interrupts for now */
768 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
769 serial_out(info, UART_ESI_CMD2,0x00);
771 /* set interrupt and DMA channel */
772 serial_out(info, UART_ESI_CMD1, ESI_SET_IRQ);
773 serial_out(info, UART_ESI_CMD2, (dma <<4) |0x01);
774 serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
775 if(info->line %8)/* secondary port */
776 serial_out(info, UART_ESI_CMD2,0x0d);/* shared */
777 else if(info->irq ==9)
778 serial_out(info, UART_ESI_CMD2,0x02);
779 else
780 serial_out(info, UART_ESI_CMD2, info->irq);
782 /* set error status mask (check this) */
783 serial_out(info, UART_ESI_CMD1, ESI_SET_ERR_MASK);
784 serial_out(info, UART_ESI_CMD2,0xbd);
785 serial_out(info, UART_ESI_CMD2,0x06);
787 /* set DMA timeout */
788 serial_out(info, UART_ESI_CMD1, ESI_SET_DMA_TMOUT);
789 serial_out(info, UART_ESI_CMD2,0xff);
791 /* set FIFO trigger levels */
792 serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
793 serial_out(info, UART_ESI_CMD2, trigger /256);
794 serial_out(info, UART_ESI_CMD2, trigger %256);
795 serial_out(info, UART_ESI_CMD2, trigger /256);
796 serial_out(info, UART_ESI_CMD2, trigger %256);
798 /* Set clock scaling */
799 serial_out(info, UART_ESI_CMD1, ESI_SET_PRESCALAR);
800 serial_out(info, UART_ESI_CMD2, ESPC_SCALE);
803 static intstartup(struct esp_struct * info)
805 unsigned long flags;
806 int retval;
807 int next_irq;
808 struct esp_struct *next_info =0;
809 unsigned int num_chars;
811 save_flags(flags);cli();
813 if(info->flags & ASYNC_INITIALIZED) {
814 restore_flags(flags);
815 return0;
818 if(!info->port || !info->type) {
819 if(info->tty)
820 set_bit(TTY_IO_ERROR, &info->tty->flags);
821 restore_flags(flags);
822 return0;
825 if(!info->xmit_buf) {
826 info->xmit_buf = (unsigned char*)get_free_page(GFP_KERNEL);
827 if(!info->xmit_buf) {
828 restore_flags(flags);
829 return-ENOMEM;
833 if(!info->tty_buf) {
834 info->tty_buf = (struct tty_flip_buffer *)kmalloc(
835 sizeof(struct tty_flip_buffer), GFP_KERNEL);
837 if(!info->tty_buf) {
838 free_page((unsigned long) info->xmit_buf);
839 info->xmit_buf =0;
840 restore_flags(flags);
841 return-ENOMEM;
844 memset(info->tty_buf,0,sizeof(struct tty_flip_buffer));
845 info->tty_buf->tqueue.routine = do_ttybuf;
846 info->tty_buf->tqueue.data = info;
849 info->tty_buf->count =0;
850 info->tty_buf->char_buf_ptr = info->tty_buf->char_buf;
851 info->tty_buf->flag_buf_ptr = info->tty_buf->flag_buf;
853 #ifdef SERIAL_DEBUG_OPEN
854 printk("starting up ttys%d (irq %d)...", info->line, info->irq);
855 #endif
857 /* Flush the RX buffer. Using the ESI flush command may cause */
858 /* wild interrupts, so read all the data instead. */
860 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
861 serial_out(info, UART_ESI_CMD1, ESI_GET_RX_AVAIL);
862 num_chars =serial_in(info, UART_ESI_STAT1) <<8;
863 num_chars |=serial_in(info, UART_ESI_STAT2);
865 while(num_chars >1) {
866 inw(info->port + UART_ESI_RX);
867 num_chars -=2;
870 if(num_chars)
871 serial_in(info, UART_ESI_RX);
873 /* set receive character timeout */
874 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
875 serial_out(info, UART_ESI_CMD2,0xff);
877 info->stat_flags =0;
880 * Allocate the IRQ if necessary
882 if(!IRQ_ports[info->irq]) {
883 retval =request_irq(info->irq, rs_interrupt_single,
884 SA_INTERRUPT,"esp", NULL);
886 if(!retval) {
887 int i =1;
889 while((i <16) && !IRQ_ports[i])
890 i++;
892 if(i ==16) {
893 dma_buffer = (char*)__get_dma_pages(GFP_KERNEL,
894 __get_order(DMA_BUFFER_SZ));
896 if(!dma_buffer)
897 retval = -ENOMEM;
898 else
899 retval =request_dma(dma,"esp");
901 if(retval)
902 free_irq(info->irq, NULL);
906 if(retval) {
907 restore_flags(flags);
908 if(suser()) {
909 if(info->tty)
910 set_bit(TTY_IO_ERROR,
911 &info->tty->flags);
912 return0;
913 }else
914 return retval;
918 info->MCR = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
919 info->MCR_noint = UART_MCR_DTR | UART_MCR_RTS;
920 #if defined(__alpha__) && !defined(CONFIG_PCI)
921 info->MCR |= UART_MCR_OUT1 | UART_MCR_OUT2;
922 info->MCR_noint |= UART_MCR_OUT1 | UART_MCR_OUT2;
923 #endif
925 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
926 serial_out(info, UART_ESI_CMD2, UART_MCR);
927 serial_out(info, UART_ESI_CMD2, info->MCR);
930 * Finally, enable interrupts
932 /* info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI; */
933 info->IER = UART_IER_RLSI | UART_IER_RDI | UART_IER_DMA_TMOUT |
934 UART_IER_DMA_TC;
935 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
936 serial_out(info, UART_ESI_CMD2, info->IER);
938 if(info->tty)
939 clear_bit(TTY_IO_ERROR, &info->tty->flags);
940 info->xmit_cnt = info->xmit_head = info->xmit_tail =0;
942 /* Remove port from "closed" chain */
943 if(info->next_port)
944 info->next_port->prev_port = info->prev_port;
945 if(info->prev_port)
946 info->prev_port->next_port = info->next_port;
947 else
948 IRQ_ports[0] = info->next_port;
951 * Insert serial port into IRQ chain.
953 next_irq = info->irq;
956 next_info = IRQ_ports[next_irq];
958 if(++next_irq >15)
959 next_irq =1;
960 }while(!next_info && (next_irq != info->irq));
962 if(!next_info) {
963 info->next_port = info;
964 info->prev_port = info;
965 }else{
966 info->next_port = next_info;
967 info->prev_port = next_info->prev_port;
968 next_info->prev_port->next_port = info;
969 next_info->prev_port = info;
972 IRQ_ports[info->irq] = info;
975 * set the speed of the serial port
977 change_speed(info);
979 info->flags |= ASYNC_INITIALIZED;
980 restore_flags(flags);
981 return0;
985 * This routine will shutdown a serial port; interrupts are disabled, and
986 * DTR is dropped if the hangup on close termio flag is on.
988 static voidshutdown(struct esp_struct * info)
990 unsigned long flags;
992 if(!(info->flags & ASYNC_INITIALIZED))
993 return;
995 #ifdef SERIAL_DEBUG_OPEN
996 printk("Shutting down serial port %d (irq %d)....", info->line,
997 info->irq);
998 #endif
1000 save_flags(flags);cli();/* Disable interrupts */
1003 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1004 * here so the queue might never be waken up
1006 wake_up_interruptible(&info->delta_msr_wait);
1007 wake_up_interruptible(&info->break_wait);
1010 * First unlink the serial port from the IRQ chain...
1012 info->next_port->prev_port = info->prev_port;
1013 info->prev_port->next_port = info->next_port;
1015 if(IRQ_ports[info->irq] == info) {
1016 if((info->next_port == info) ||
1017 (info->next_port->irq != info->irq))
1018 IRQ_ports[info->irq] =0;
1019 else
1020 IRQ_ports[info->irq] = info->next_port;
1023 /* Stick it on the "closed" chain */
1024 info->next_port = IRQ_ports[0];
1025 if(info->next_port)
1026 info->next_port->prev_port = info;
1027 info->prev_port =0;
1028 IRQ_ports[0] = info;
1031 * Free the IRQ, if necessary
1033 if(!IRQ_ports[info->irq]) {
1034 int i =1;
1036 while((i <16) && !IRQ_ports[i])
1037 i++;
1039 if(i ==16) {
1040 free_dma(dma);
1041 free_pages((unsigned int)dma_buffer,
1042 __get_order(DMA_BUFFER_SZ));
1043 dma_buffer =0;
1046 free_irq(info->irq, NULL);
1049 if(info->xmit_buf) {
1050 free_page((unsigned long) info->xmit_buf);
1051 info->xmit_buf =0;
1054 if(info->tty_buf && !info->tty_buf->tqueue.sync) {
1055 kfree(info->tty_buf);
1056 info->tty_buf =0;
1059 info->IER =0;
1060 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1061 serial_out(info, UART_ESI_CMD2,0x00);
1063 if(!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
1064 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1065 info->MCR_noint &= ~(UART_MCR_DTR|UART_MCR_RTS);
1068 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1069 serial_out(info, UART_ESI_CMD2, UART_MCR);
1070 serial_out(info, UART_ESI_CMD2, info->MCR_noint);
1072 if(info->tty)
1073 set_bit(TTY_IO_ERROR, &info->tty->flags);
1075 info->flags &= ~ASYNC_INITIALIZED;
1076 restore_flags(flags);
1080 * This routine is called to set the UART divisor registers to match
1081 * the specified baud rate for a serial port.
1083 static voidchange_speed(struct esp_struct *info)
1085 unsigned short port;
1086 int quot =0;
1087 unsigned cflag,cval;
1088 int i;
1089 unsigned char flow1 =0, flow2 =0;
1091 if(!info->tty || !info->tty->termios)
1092 return;
1093 cflag = info->tty->termios->c_cflag;
1094 if(!(port = info->port))
1095 return;
1096 i = cflag & CBAUD;
1097 if(i & CBAUDEX) {
1098 i &= ~CBAUDEX;
1099 if(i <1|| i >2)
1100 info->tty->termios->c_cflag &= ~CBAUDEX;
1101 else
1102 i +=15;
1104 if(i ==15) {
1105 if((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1106 i +=1;
1107 if((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1108 i +=2;
1109 if((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1110 i +=3;
1111 if((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1112 i +=4;
1113 if((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
1114 quot = info->custom_divisor;
1116 if(quot) {
1117 info->timeout = ((1024*HZ*15*quot) /
1118 info->baud_base) +2;
1119 }else if(baud_table[i] ==134) {
1120 quot = (2*info->baud_base /269);
1121 info->timeout = (1024*HZ*30/269) +2;
1122 }else if(baud_table[i]) {
1123 quot = info->baud_base / baud_table[i];
1124 info->timeout = (1024*HZ*15/baud_table[i]) +2;
1125 }else{
1126 quot =0;
1127 info->timeout =0;
1129 if(quot) {
1130 info->MCR |= UART_MCR_DTR;
1131 info->MCR_noint |= UART_MCR_DTR;
1132 cli();
1133 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1134 serial_out(info, UART_ESI_CMD2, UART_MCR);
1135 serial_out(info, UART_ESI_CMD2, info->MCR);
1136 sti();
1137 }else{
1138 info->MCR &= ~UART_MCR_DTR;
1139 info->MCR_noint &= ~UART_MCR_DTR;
1140 cli();
1141 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1142 serial_out(info, UART_ESI_CMD2, UART_MCR);
1143 serial_out(info, UART_ESI_CMD2, info->MCR);
1144 sti();
1145 return;
1147 /* byte size and parity */
1148 switch(cflag & CSIZE) {
1149 case CS5: cval =0x00;break;
1150 case CS6: cval =0x01;break;
1151 case CS7: cval =0x02;break;
1152 case CS8: cval =0x03;break;
1153 default: cval =0x00;break;/* too keep GCC shut... */
1155 if(cflag & CSTOPB) {
1156 cval |=0x04;
1158 if(cflag & PARENB)
1159 cval |= UART_LCR_PARITY;
1160 if(!(cflag & PARODD))
1161 cval |= UART_LCR_EPAR;
1163 /* CTS flow control flag and modem status interrupts */
1164 /* info->IER &= ~UART_IER_MSI; */
1165 if(cflag & CRTSCTS) {
1166 info->flags |= ASYNC_CTS_FLOW;
1167 /* info->IER |= UART_IER_MSI; */
1168 flow1 =0x04;
1169 flow2 =0x10;
1170 }else
1171 info->flags &= ~ASYNC_CTS_FLOW;
1172 if(cflag & CLOCAL)
1173 info->flags &= ~ASYNC_CHECK_CD;
1174 else{
1175 info->flags |= ASYNC_CHECK_CD;
1176 /* info->IER |= UART_IER_MSI; */
1180 * Set up parity check flag
1182 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1184 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1185 if(I_INPCK(info->tty))
1186 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1187 if(I_BRKINT(info->tty) ||I_PARMRK(info->tty))
1188 info->read_status_mask |= UART_LSR_BI;
1190 info->ignore_status_mask =0;
1191 #if 0
1192 /* This should be safe, but for some broken bits of hardware... */
1193 if(I_IGNPAR(info->tty)) {
1194 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1195 info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
1197 #endif
1198 if(I_IGNBRK(info->tty)) {
1199 info->ignore_status_mask |= UART_LSR_BI;
1200 info->read_status_mask |= UART_LSR_BI;
1202 * If we're ignore parity and break indicators, ignore
1203 * overruns too. (For real raw support).
1205 if(I_IGNPAR(info->tty)) {
1206 info->ignore_status_mask |= UART_LSR_OE | \
1207 UART_LSR_PE | UART_LSR_FE;
1208 info->read_status_mask |= UART_LSR_OE | \
1209 UART_LSR_PE | UART_LSR_FE;
1213 if(I_IXOFF(info->tty))
1214 flow1 |=0x81;
1216 cli();
1217 /* set baud */
1218 serial_out(info, UART_ESI_CMD1, ESI_SET_BAUD);
1219 serial_out(info, UART_ESI_CMD2, quot >>8);
1220 serial_out(info, UART_ESI_CMD2, quot &0xff);
1222 /* set data bits, parity, etc. */
1223 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1224 serial_out(info, UART_ESI_CMD2, UART_LCR);
1225 serial_out(info, UART_ESI_CMD2, cval);
1227 /* Enable flow control */
1228 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CNTL);
1229 serial_out(info, UART_ESI_CMD2, flow1);
1230 serial_out(info, UART_ESI_CMD2, flow2);
1232 /* set flow control characters (XON/XOFF only) */
1233 if(I_IXOFF(info->tty)) {
1234 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_CHARS);
1235 serial_out(info, UART_ESI_CMD2,START_CHAR(info->tty));
1236 serial_out(info, UART_ESI_CMD2,STOP_CHAR(info->tty));
1237 serial_out(info, UART_ESI_CMD2,0x10);
1238 serial_out(info, UART_ESI_CMD2,0x21);
1239 switch(cflag & CSIZE) {
1240 case CS5:
1241 serial_out(info, UART_ESI_CMD2,0x1f);
1242 break;
1243 case CS6:
1244 serial_out(info, UART_ESI_CMD2,0x3f);
1245 break;
1246 case CS7:
1247 case CS8:
1248 serial_out(info, UART_ESI_CMD2,0x7f);
1249 break;
1250 default:
1251 serial_out(info, UART_ESI_CMD2,0xff);
1252 break;
1256 /* Set high/low water */
1257 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1258 serial_out(info, UART_ESI_CMD2,0x03);
1259 serial_out(info, UART_ESI_CMD2,0xfc);
1260 serial_out(info, UART_ESI_CMD2, (trigger +4) /256);
1261 serial_out(info, UART_ESI_CMD2, (trigger +4) %256);
1263 sti();
1266 static voidrs_put_char(struct tty_struct *tty,unsigned char ch)
1268 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1269 unsigned long flags;
1271 if(serial_paranoia_check(info, tty->device,"rs_put_char"))
1272 return;
1274 if(!tty || !info->xmit_buf)
1275 return;
1277 save_flags(flags);cli();
1278 if(info->xmit_cnt >= ESP_XMIT_SIZE -1) {
1279 restore_flags(flags);
1280 return;
1283 info->xmit_buf[info->xmit_head++] = ch;
1284 info->xmit_head &= ESP_XMIT_SIZE-1;
1285 info->xmit_cnt++;
1286 restore_flags(flags);
1289 static voidrs_flush_chars(struct tty_struct *tty)
1291 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1292 unsigned long flags;
1294 if(serial_paranoia_check(info, tty->device,"rs_flush_chars"))
1295 return;
1297 if(info->xmit_cnt <=0|| tty->stopped || tty->hw_stopped ||
1298 !info->xmit_buf)
1299 return;
1301 save_flags(flags);cli();
1302 if(!(info->IER & UART_IER_THRI)) {
1303 info->IER |= UART_IER_THRI;
1304 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1305 serial_out(info, UART_ESI_CMD2, info->IER);
1307 restore_flags(flags);
1310 static intrs_write(struct tty_struct * tty,int from_user,
1311 const unsigned char*buf,int count)
1313 int c, total =0;
1314 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1315 unsigned long flags;
1317 if(serial_paranoia_check(info, tty->device,"rs_write"))
1318 return0;
1320 if(!tty || !info->xmit_buf || !tmp_buf)
1321 return0;
1323 if(from_user)
1324 down(&tmp_buf_sem);
1325 save_flags(flags);
1326 while(1) {
1327 cli();
1328 c =MIN(count,MIN(ESP_XMIT_SIZE - info->xmit_cnt -1,
1329 ESP_XMIT_SIZE - info->xmit_head));
1330 if(c <=0)
1331 break;
1333 if(from_user) {
1334 copy_from_user(tmp_buf, buf, c);
1335 c =MIN(c,MIN(ESP_XMIT_SIZE - info->xmit_cnt -1,
1336 ESP_XMIT_SIZE - info->xmit_head));
1337 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1338 }else
1339 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1340 info->xmit_head = (info->xmit_head + c) & (ESP_XMIT_SIZE-1);
1341 info->xmit_cnt += c;
1342 restore_flags(flags);
1343 buf += c;
1344 count -= c;
1345 total += c;
1347 if(from_user)
1348 up(&tmp_buf_sem);
1349 if(info->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1350 !(info->IER & UART_IER_THRI)) {
1351 info->IER |= UART_IER_THRI;
1352 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);/* set mask */
1353 serial_out(info, UART_ESI_CMD2, info->IER);
1355 restore_flags(flags);
1356 return total;
1359 static intrs_write_room(struct tty_struct *tty)
1361 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1362 int ret;
1364 if(serial_paranoia_check(info, tty->device,"rs_write_room"))
1365 return0;
1366 ret = ESP_XMIT_SIZE - info->xmit_cnt -1;
1367 if(ret <0)
1368 ret =0;
1369 return ret;
1372 static intrs_chars_in_buffer(struct tty_struct *tty)
1374 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1376 if(serial_paranoia_check(info, tty->device,"rs_chars_in_buffer"))
1377 return0;
1378 return info->xmit_cnt;
1381 static voidrs_flush_buffer(struct tty_struct *tty)
1383 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1385 if(serial_paranoia_check(info, tty->device,"rs_flush_buffer"))
1386 return;
1387 cli();
1388 info->xmit_cnt = info->xmit_head = info->xmit_tail =0;
1389 sti();
1390 wake_up_interruptible(&tty->write_wait);
1391 if((tty->flags & (1<< TTY_DO_WRITE_WAKEUP)) &&
1392 tty->ldisc.write_wakeup)
1393 (tty->ldisc.write_wakeup)(tty);
1397 * ------------------------------------------------------------
1398 * rs_throttle()
1400 * This routine is called by the upper-layer tty layer to signal that
1401 * incoming characters should be throttled.
1402 * ------------------------------------------------------------
1404 static voidrs_throttle(struct tty_struct * tty)
1406 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1407 #ifdef SERIAL_DEBUG_THROTTLE
1408 char buf[64];
1410 printk("throttle %s: %d....\n",_tty_name(tty, buf),
1411 tty->ldisc.chars_in_buffer(tty));
1412 #endif
1414 if(serial_paranoia_check(info, tty->device,"rs_throttle"))
1415 return;
1417 info->IER &= ~UART_IER_RDI;
1418 cli();
1419 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1420 serial_out(info, UART_ESI_CMD2, info->IER);
1421 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1422 serial_out(info, UART_ESI_CMD2,0x00);
1423 sti();
1426 static voidrs_unthrottle(struct tty_struct * tty)
1428 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1429 #ifdef SERIAL_DEBUG_THROTTLE
1430 char buf[64];
1432 printk("unthrottle %s: %d....\n",_tty_name(tty, buf),
1433 tty->ldisc.chars_in_buffer(tty));
1434 #endif
1436 if(serial_paranoia_check(info, tty->device,"rs_unthrottle"))
1437 return;
1439 info->IER |= UART_IER_RDI;
1440 cli();
1441 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1442 serial_out(info, UART_ESI_CMD2, info->IER);
1443 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1444 serial_out(info, UART_ESI_CMD2,0xff);
1445 sti();
1449 * ------------------------------------------------------------
1450 * rs_ioctl() and friends
1451 * ------------------------------------------------------------
1454 static intget_serial_info(struct esp_struct * info,
1455 struct serial_struct * retinfo)
1457 struct serial_struct tmp;
1459 if(!retinfo)
1460 return-EFAULT;
1461 memset(&tmp,0,sizeof(tmp));
1462 tmp.type = info->type;
1463 tmp.line = info->line;
1464 tmp.port = info->port;
1465 tmp.irq = info->irq;
1466 tmp.flags = info->flags;
1467 tmp.baud_base = info->baud_base;
1468 tmp.close_delay = info->close_delay;
1469 tmp.closing_wait = info->closing_wait;
1470 tmp.custom_divisor = info->custom_divisor;
1471 tmp.hub6 =0;
1472 copy_to_user(retinfo,&tmp,sizeof(*retinfo));
1473 return0;
1476 static intset_serial_info(struct esp_struct * info,
1477 struct serial_struct * new_info)
1479 struct serial_struct new_serial;
1480 struct esp_struct old_info;
1481 unsigned int change_irq;
1482 int retval =0;
1483 struct esp_struct *current_async;
1484 unsigned long flags =0;
1486 if(!new_info)
1487 return-EFAULT;
1488 copy_from_user(&new_serial,new_info,sizeof(new_serial));
1489 old_info = *info;
1491 if((info->type != new_serial.type) ||
1492 (new_serial.hub6) ||
1493 (info->port != new_serial.port) ||
1494 (info->baud_base != new_serial.baud_base) ||
1495 (new_serial.irq >15) ||
1496 (new_serial.irq <1))
1497 return-EINVAL;
1499 change_irq = new_serial.irq != info->irq;
1501 if(change_irq && (info->line %8))
1502 return-EINVAL;
1504 if(!suser()) {
1505 if(change_irq ||
1506 (new_serial.baud_base != info->baud_base) ||
1507 (new_serial.close_delay != info->close_delay) ||
1508 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1509 (info->flags & ~ASYNC_USR_MASK)))
1510 return-EPERM;
1511 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1512 (new_serial.flags & ASYNC_USR_MASK));
1513 info->custom_divisor = new_serial.custom_divisor;
1514 goto check_and_exit;
1517 if(new_serial.irq ==2)
1518 new_serial.irq =9;
1520 if(change_irq) {
1521 save_flags(flags);cli();
1523 current_async = info;
1525 if((current_async->line >= info->line) &&
1526 (current_async->line < (info->line +8))) {
1527 if(current_async == info) {
1528 if(current_async->count >1) {
1529 restore_flags(flags);
1530 return-EBUSY;
1532 }else{
1533 restore_flags(flags);
1534 return-EBUSY;
1538 current_async = current_async->next_port;
1539 }while(current_async != info);
1543 * OK, past this point, all the error checking has been done.
1544 * At this point, we start making changes.....
1547 info->baud_base = new_serial.baud_base;
1548 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1549 (new_serial.flags & ASYNC_FLAGS));
1550 info->custom_divisor = new_serial.custom_divisor;
1551 info->close_delay = new_serial.close_delay * HZ/100;
1552 info->closing_wait = new_serial.closing_wait * HZ/100;
1554 release_region(info->port,8);
1555 if(change_irq) {
1557 * We need to shutdown the serial port at the old
1558 * port/irq combination.
1560 shutdown(info);
1562 current_async = IRQ_ports[0];
1563 while(current_async !=0) {
1564 if((current_async->line >= info->line) &&
1565 (current_async->line < (info->line +8)))
1566 current_async->irq = new_serial.irq;
1568 current_async = current_async->next_port;
1571 serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
1572 if(info->irq ==9)
1573 serial_out(info, UART_ESI_CMD2,0x02);
1574 else
1575 serial_out(info, UART_ESI_CMD2, info->irq);
1577 restore_flags(flags);
1579 if(info->type != PORT_UNKNOWN)
1580 request_region(info->port,8,"esp(set)");
1583 check_and_exit:
1584 if(!info->port || !info->type)
1585 return0;
1586 if(info->flags & ASYNC_INITIALIZED) {
1587 if(((old_info.flags & ASYNC_SPD_MASK) !=
1588 (info->flags & ASYNC_SPD_MASK)) ||
1589 (old_info.custom_divisor != info->custom_divisor))
1590 change_speed(info);
1591 }else
1592 retval =startup(info);
1593 return retval;
1598 * get_lsr_info - get line status register info
1600 * Purpose: Let user call ioctl() to get info when the UART physically
1601 * is emptied. On bus types like RS485, the transmitter must
1602 * release the bus after transmitting. This must be done when
1603 * the transmit shift register is empty, not be done when the
1604 * transmit holding register is empty. This functionality
1605 * allows an RS485 driver to be written in user space.
1607 static intget_lsr_info(struct esp_struct * info,unsigned int*value)
1609 unsigned char status;
1610 unsigned int result;
1612 cli();
1613 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1614 status =serial_in(info, UART_ESI_STAT1);
1615 sti();
1616 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT :0);
1617 returnput_user(result,value);
1621 static intget_modem_info(struct esp_struct * info,unsigned int*value)
1623 unsigned char control, status;
1624 unsigned int result;
1626 control = info->MCR;
1627 cli();
1628 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1629 status =serial_in(info, UART_ESI_STAT2);
1630 sti();
1631 result = ((control & UART_MCR_RTS) ? TIOCM_RTS :0)
1632 | ((control & UART_MCR_DTR) ? TIOCM_DTR :0)
1633 | ((status & UART_MSR_DCD) ? TIOCM_CAR :0)
1634 | ((status & UART_MSR_RI) ? TIOCM_RNG :0)
1635 | ((status & UART_MSR_DSR) ? TIOCM_DSR :0)
1636 | ((status & UART_MSR_CTS) ? TIOCM_CTS :0);
1637 returnput_user(result,value);
1640 static intset_modem_info(struct esp_struct * info,unsigned int cmd,
1641 unsigned int*value)
1643 int error;
1644 unsigned int arg;
1646 error =get_user(arg, value);
1647 if(error)
1648 return error;
1650 switch(cmd) {
1651 case TIOCMBIS:
1652 if(arg & TIOCM_RTS) {
1653 info->MCR |= UART_MCR_RTS;
1654 info->MCR_noint |= UART_MCR_RTS;
1656 if(arg & TIOCM_DTR) {
1657 info->MCR |= UART_MCR_DTR;
1658 info->MCR_noint |= UART_MCR_DTR;
1660 break;
1661 case TIOCMBIC:
1662 if(arg & TIOCM_RTS) {
1663 info->MCR &= ~UART_MCR_RTS;
1664 info->MCR_noint &= ~UART_MCR_RTS;
1666 if(arg & TIOCM_DTR) {
1667 info->MCR &= ~UART_MCR_DTR;
1668 info->MCR_noint &= ~UART_MCR_DTR;
1670 break;
1671 case TIOCMSET:
1672 info->MCR = ((info->MCR & ~(UART_MCR_RTS | UART_MCR_DTR))
1673 | ((arg & TIOCM_RTS) ? UART_MCR_RTS :0)
1674 | ((arg & TIOCM_DTR) ? UART_MCR_DTR :0));
1675 info->MCR_noint = ((info->MCR_noint
1676 & ~(UART_MCR_RTS | UART_MCR_DTR))
1677 | ((arg & TIOCM_RTS) ? UART_MCR_RTS :0)
1678 | ((arg & TIOCM_DTR) ? UART_MCR_DTR :0));
1679 break;
1680 default:
1681 return-EINVAL;
1683 cli();
1684 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1685 serial_out(info, UART_ESI_CMD2, UART_MCR);
1686 serial_out(info, UART_ESI_CMD2, info->MCR);
1687 sti();
1688 return0;
1691 static intdo_autoconfig(struct esp_struct * info)
1693 int retval;
1695 if(!suser())
1696 return-EPERM;
1698 if(info->count >1)
1699 return-EBUSY;
1701 shutdown(info);
1703 cli();
1704 autoconfig(info);
1705 sti();
1707 retval =startup(info);
1708 if(retval)
1709 return retval;
1710 return0;
1715 * This routine sends a break character out the serial port.
1717 static voidsend_break(struct esp_struct * info,int duration)
1719 if(!info->port)
1720 return;
1721 cli();
1722 serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1723 serial_out(info, UART_ESI_CMD2,0x01);
1725 interruptible_sleep_on(&info->break_wait);
1727 current->state = TASK_INTERRUPTIBLE;
1728 current->timeout = jiffies + duration;
1729 schedule();
1731 serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1732 serial_out(info, UART_ESI_CMD2,0x00);
1733 sti();
1736 static intrs_ioctl(struct tty_struct *tty,struct file * file,
1737 unsigned int cmd,unsigned long arg)
1739 int error;
1740 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1741 int retval;
1742 struct async_icount cprev, cnow;/* kernel counter temps */
1743 struct serial_icounter_struct *p_cuser;/* user space */
1745 if(serial_paranoia_check(info, tty->device,"rs_ioctl"))
1746 return-ENODEV;
1748 if((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1749 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1750 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT) &&
1751 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1752 if(tty->flags & (1<< TTY_IO_ERROR))
1753 return-EIO;
1756 switch(cmd) {
1757 case TCSBRK:/* SVID version: non-zero arg --> no break */
1758 retval =tty_check_change(tty);
1759 if(retval)
1760 return retval;
1761 tty_wait_until_sent(tty,0);
1762 if(!arg)
1763 send_break(info, HZ/4);/* 1/4 second */
1764 return0;
1765 case TCSBRKP:/* support for POSIX tcsendbreak() */
1766 retval =tty_check_change(tty);
1767 if(retval)
1768 return retval;
1769 tty_wait_until_sent(tty,0);
1770 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1771 return0;
1772 case TIOCGSOFTCAR:
1773 returnput_user(C_CLOCAL(tty) ?1:0,
1774 (int*) arg);
1775 case TIOCSSOFTCAR:
1776 error =get_user(arg, (unsigned int*)arg);
1777 if(error)
1778 return error;
1779 tty->termios->c_cflag =
1780 ((tty->termios->c_cflag & ~CLOCAL) |
1781 (arg ? CLOCAL :0));
1782 return0;
1783 case TIOCMGET:
1784 returnget_modem_info(info, (unsigned int*) arg);
1785 case TIOCMBIS:
1786 case TIOCMBIC:
1787 case TIOCMSET:
1788 returnset_modem_info(info, cmd, (unsigned int*) arg);
1789 case TIOCGSERIAL:
1790 error =verify_area(VERIFY_WRITE, (void*) arg,
1791 sizeof(struct serial_struct));
1792 if(error)
1793 return error;
1794 returnget_serial_info(info,
1795 (struct serial_struct *) arg);
1796 case TIOCSSERIAL:
1797 error =verify_area(VERIFY_READ, (void*) arg,
1798 sizeof(struct serial_struct));
1799 if(error)
1800 return error;
1801 returnset_serial_info(info,
1802 (struct serial_struct *) arg);
1803 case TIOCSERCONFIG:
1804 returndo_autoconfig(info);
1806 case TIOCSERGWILD:
1807 returnput_user(0L, (unsigned long*) arg);
1809 case TIOCSERGETLSR:/* Get line status register */
1810 returnget_lsr_info(info, (unsigned int*) arg);
1812 case TIOCSERSWILD:
1813 if(!suser())
1814 return-EPERM;
1815 return0;
1818 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1819 * - mask passed in arg for lines of interest
1820 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1821 * Caller should use TIOCGICOUNT to see which one it was
1823 case TIOCMIWAIT:
1824 cli();
1825 cprev = info->icount;/* note the counters on entry */
1826 sti();
1827 while(1) {
1828 interruptible_sleep_on(&info->delta_msr_wait);
1829 /* see if a signal did it */
1830 if(current->signal & ~current->blocked)
1831 return-ERESTARTSYS;
1832 cli();
1833 cnow = info->icount;/* atomic copy */
1834 sti();
1835 if(cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1836 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1837 return-EIO;/* no change => error */
1838 if( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1839 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1840 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1841 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1842 return0;
1844 cprev = cnow;
1846 /* NOTREACHED */
1849 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1850 * Return: write counters to the user passed counter struct
1851 * NB: both 1->0 and 0->1 transitions are counted except for
1852 * RI where only 0->1 is counted.
1854 case TIOCGICOUNT:
1855 cli();
1856 cnow = info->icount;
1857 sti();
1858 p_cuser = (struct serial_icounter_struct *) arg;
1859 if((error =put_user(cnow.cts, &p_cuser->cts)))
1860 return error;
1861 if((error =put_user(cnow.dsr, &p_cuser->dsr)))
1862 return error;
1863 if((error =put_user(cnow.rng, &p_cuser->rng)))
1864 return error;
1865 if((error =put_user(cnow.dcd, &p_cuser->dcd)))
1866 return error;
1868 return0;
1870 default:
1871 return-ENOIOCTLCMD;
1873 return0;
1876 static voidrs_set_termios(struct tty_struct *tty,struct termios *old_termios)
1878 struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1880 if( (tty->termios->c_cflag == old_termios->c_cflag)
1881 && (RELEVANT_IFLAG(tty->termios->c_iflag)
1882 ==RELEVANT_IFLAG(old_termios->c_iflag)))
1883 return;
1885 change_speed(info);
1887 if((old_termios->c_cflag & CRTSCTS) &&
1888 !(tty->termios->c_cflag & CRTSCTS)) {
1889 tty->hw_stopped =0;
1890 rs_start(tty);
1893 #if 0
1895 * No need to wake up processes in open wait, since they
1896 * sample the CLOCAL flag once, and don't recheck it.
1897 * XXX It's not clear whether the current behavior is correct
1898 * or not. Hence, this may change.....
1900 if(!(old_termios->c_cflag & CLOCAL) &&
1901 (tty->termios->c_cflag & CLOCAL))
1902 wake_up_interruptible(&info->open_wait);
1903 #endif
1907 * ------------------------------------------------------------
1908 * rs_close()
1910 * This routine is called when the serial port gets closed. First, we
1911 * wait for the last remaining data to be sent. Then, we unlink its
1912 * async structure from the interrupt chain if necessary, and we free
1913 * that IRQ if nothing is left in the chain.
1914 * ------------------------------------------------------------
1916 static voidrs_close(struct tty_struct *tty,struct file * filp)
1918 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1919 unsigned long flags;
1920 unsigned long timeout;
1922 if(!info ||serial_paranoia_check(info, tty->device,"rs_close"))
1923 return;
1925 save_flags(flags);cli();
1927 if(tty_hung_up_p(filp)) {
1928 DBG_CNT("before DEC-hung");
1929 MOD_DEC_USE_COUNT;
1930 restore_flags(flags);
1931 return;
1934 #ifdef SERIAL_DEBUG_OPEN
1935 printk("rs_close ttys%d, count = %d\n", info->line, info->count);
1936 #endif
1937 if((tty->count ==1) && (info->count !=1)) {
1939 * Uh, oh. tty->count is 1, which means that the tty
1940 * structure will be freed. Info->count should always
1941 * be one in these conditions. If it's greater than
1942 * one, we've got real problems, since it means the
1943 * serial port won't be shutdown.
1945 printk("rs_close: bad serial port count; tty->count is 1, "
1946 "info->count is %d\n", info->count);
1947 info->count =1;
1949 if(--info->count <0) {
1950 printk("rs_close: bad serial port count for ttys%d: %d\n",
1951 info->line, info->count);
1952 info->count =0;
1954 if(info->count) {
1955 DBG_CNT("before DEC-2");
1956 MOD_DEC_USE_COUNT;
1957 restore_flags(flags);
1958 return;
1960 info->flags |= ASYNC_CLOSING;
1962 * Save the termios structure, since this port may have
1963 * separate termios for callout and dialin.
1965 if(info->flags & ASYNC_NORMAL_ACTIVE)
1966 info->normal_termios = *tty->termios;
1967 if(info->flags & ASYNC_CALLOUT_ACTIVE)
1968 info->callout_termios = *tty->termios;
1970 * Now we wait for the transmit buffer to clear; and we notify
1971 * the line discipline to only process XON/XOFF characters.
1973 tty->closing =1;
1974 if(info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1975 tty_wait_until_sent(tty, info->closing_wait);
1977 * At this point we stop accepting input. To do this, we
1978 * disable the receive line status interrupts, and tell the
1979 * interrupt driver to stop checking the data ready bit in the
1980 * line status register.
1982 /* info->IER &= ~UART_IER_RLSI; */
1983 info->IER &= ~UART_IER_RDI;
1984 info->read_status_mask &= ~UART_LSR_DR;
1985 if(info->flags & ASYNC_INITIALIZED) {
1986 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
1987 serial_out(info, UART_ESI_CMD2, info->IER);
1989 /* disable receive timeout */
1990 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
1991 serial_out(info, UART_ESI_CMD2,0x00);
1994 * Before we drop DTR, make sure the UART transmitter
1995 * has completely drained; this is especially
1996 * important if there is a transmit FIFO!
1998 timeout = jiffies+HZ;
1999 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2000 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2001 while((serial_in(info, UART_ESI_STAT1) !=0x03) ||
2002 (serial_in(info, UART_ESI_STAT2) !=0xff)) {
2003 current->state = TASK_INTERRUPTIBLE;
2004 current->timeout = jiffies + info->timeout;
2005 schedule();
2006 if(jiffies > timeout)
2007 break;
2008 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2009 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2012 shutdown(info);
2013 if(tty->driver.flush_buffer)
2014 tty->driver.flush_buffer(tty);
2015 if(tty->ldisc.flush_buffer)
2016 tty->ldisc.flush_buffer(tty);
2017 tty->closing =0;
2018 info->event =0;
2019 info->tty =0;
2021 if(info->tty_buf) {
2022 while(info->tty_buf->tqueue.sync) {
2023 current->state = TASK_INTERRUPTIBLE;
2024 current->timeout = jiffies + HZ /10;
2025 schedule();
2028 kfree(info->tty_buf);
2029 info->tty_buf =0;
2032 if(info->blocked_open) {
2033 if(info->close_delay) {
2034 current->state = TASK_INTERRUPTIBLE;
2035 current->timeout = jiffies + info->close_delay;
2036 schedule();
2038 wake_up_interruptible(&info->open_wait);
2040 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
2041 ASYNC_CLOSING);
2042 wake_up_interruptible(&info->close_wait);
2043 MOD_DEC_USE_COUNT;
2044 restore_flags(flags);
2048 * esp_hangup() --- called by tty_hangup() when a hangup is signaled.
2050 static voidesp_hangup(struct tty_struct *tty)
2052 struct esp_struct * info = (struct esp_struct *)tty->driver_data;
2054 if(serial_paranoia_check(info, tty->device,"esp_hangup"))
2055 return;
2057 rs_flush_buffer(tty);
2058 shutdown(info);
2059 info->event =0;
2060 info->count =0;
2061 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
2062 info->tty =0;
2063 wake_up_interruptible(&info->open_wait);
2067 * ------------------------------------------------------------
2068 * esp_open() and friends
2069 * ------------------------------------------------------------
2071 static intblock_til_ready(struct tty_struct *tty,struct file * filp,
2072 struct esp_struct *info)
2074 struct wait_queue wait = { current, NULL };
2075 int retval;
2076 int do_clocal =0;
2079 * If the device is in the middle of being closed, then block
2080 * until it's done, and then try again.
2082 if(tty_hung_up_p(filp) ||
2083 (info->flags & ASYNC_CLOSING)) {
2084 if(info->flags & ASYNC_CLOSING)
2085 interruptible_sleep_on(&info->close_wait);
2086 #ifdef SERIAL_DO_RESTART
2087 if(info->flags & ASYNC_HUP_NOTIFY)
2088 return-EAGAIN;
2089 else
2090 return-ERESTARTSYS;
2091 #else
2092 return-EAGAIN;
2093 #endif
2097 * If this is a callout device, then just make sure the normal
2098 * device isn't being used.
2100 if(tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
2101 if(info->flags & ASYNC_NORMAL_ACTIVE)
2102 return-EBUSY;
2103 if((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2104 (info->flags & ASYNC_SESSION_LOCKOUT) &&
2105 (info->session != current->session))
2106 return-EBUSY;
2107 if((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2108 (info->flags & ASYNC_PGRP_LOCKOUT) &&
2109 (info->pgrp != current->pgrp))
2110 return-EBUSY;
2111 info->flags |= ASYNC_CALLOUT_ACTIVE;
2112 return0;
2116 * If non-blocking mode is set, or the port is not enabled,
2117 * then make the check up front and then exit.
2119 if((filp->f_flags & O_NONBLOCK) ||
2120 (tty->flags & (1<< TTY_IO_ERROR))) {
2121 if(info->flags & ASYNC_CALLOUT_ACTIVE)
2122 return-EBUSY;
2123 info->flags |= ASYNC_NORMAL_ACTIVE;
2124 return0;
2127 if(info->flags & ASYNC_CALLOUT_ACTIVE) {
2128 if(info->normal_termios.c_cflag & CLOCAL)
2129 do_clocal =1;
2130 }else{
2131 if(tty->termios->c_cflag & CLOCAL)
2132 do_clocal =1;
2136 * Block waiting for the carrier detect and the line to become
2137 * free (i.e., not in use by the callout). While we are in
2138 * this loop, info->count is dropped by one, so that
2139 * rs_close() knows when to free things. We restore it upon
2140 * exit, either normal or abnormal.
2142 retval =0;
2143 add_wait_queue(&info->open_wait, &wait);
2144 #ifdef SERIAL_DEBUG_OPEN
2145 printk("block_til_ready before block: ttys%d, count = %d\n",
2146 info->line, info->count);
2147 #endif
2148 cli();
2149 if(!tty_hung_up_p(filp))
2150 info->count--;
2151 sti();
2152 info->blocked_open++;
2153 while(1) {
2154 cli();
2155 if(!(info->flags & ASYNC_CALLOUT_ACTIVE)) {
2156 unsigned int scratch;
2158 serial_out(info, UART_ESI_CMD1, ESI_READ_UART);
2159 serial_out(info, UART_ESI_CMD2, UART_MCR);
2160 scratch =serial_in(info, UART_ESI_STAT1);
2161 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2162 serial_out(info, UART_ESI_CMD2, UART_MCR);
2163 serial_out(info, UART_ESI_CMD2,
2164 scratch | UART_MCR_DTR | UART_MCR_RTS);
2166 sti();
2167 current->state = TASK_INTERRUPTIBLE;
2168 if(tty_hung_up_p(filp) ||
2169 !(info->flags & ASYNC_INITIALIZED)) {
2170 #ifdef SERIAL_DO_RESTART
2171 if(info->flags & ASYNC_HUP_NOTIFY)
2172 retval = -EAGAIN;
2173 else
2174 retval = -ERESTARTSYS;
2175 #else
2176 retval = -EAGAIN;
2177 #endif
2178 break;
2181 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
2182 if(serial_in(info, UART_ESI_STAT2) & UART_MSR_DCD)
2183 do_clocal =1;
2185 if(!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2186 !(info->flags & ASYNC_CLOSING) &&
2187 (do_clocal))
2188 break;
2189 if(current->signal & ~current->blocked) {
2190 retval = -ERESTARTSYS;
2191 break;
2193 #ifdef SERIAL_DEBUG_OPEN
2194 printk("block_til_ready blocking: ttys%d, count = %d\n",
2195 info->line, info->count);
2196 #endif
2197 schedule();
2199 current->state = TASK_RUNNING;
2200 remove_wait_queue(&info->open_wait, &wait);
2201 if(!tty_hung_up_p(filp))
2202 info->count++;
2203 info->blocked_open--;
2204 #ifdef SERIAL_DEBUG_OPEN
2205 printk("block_til_ready after blocking: ttys%d, count = %d\n",
2206 info->line, info->count);
2207 #endif
2208 if(retval)
2209 return retval;
2210 info->flags |= ASYNC_NORMAL_ACTIVE;
2211 return0;
2215 * This routine is called whenever a serial port is opened. It
2216 * enables interrupts for a serial port, linking in its async structure into
2217 * the IRQ chain. It also performs the serial-specific
2218 * initialization for the tty structure.
2220 static intesp_open(struct tty_struct *tty,struct file * filp)
2222 struct esp_struct *info;
2223 int i, retval, line;
2224 unsigned long page;
2226 line =MINOR(tty->device) - tty->driver.minor_start;
2227 if((line <0) || (line >= NR_PORTS))
2228 return-ENODEV;
2230 /* check whether or not the port is on the closed chain */
2232 info = IRQ_ports[0];
2234 while(info && (info->line != line))
2235 info = info->next_port;
2237 /* if the port is not on the closed chain, look for it on the */
2238 /* open chain */
2240 if(!info) {
2241 i =1;
2243 while((i <16) && !IRQ_ports[i])
2244 i++;
2246 if(i <16) {
2247 info = IRQ_ports[i];
2250 if(info->line == line)
2251 break;
2252 info = info->next_port;
2253 }while(info != IRQ_ports[i]);
2257 if(!info || (info->line != line) ||
2258 serial_paranoia_check(info, tty->device,"esp_open"))
2259 return-ENODEV;
2261 #ifdef SERIAL_DEBUG_OPEN
2262 printk("esp_open %s%d, count = %d\n", tty->driver.name, info->line,
2263 info->count);
2264 #endif
2265 MOD_INC_USE_COUNT;
2266 info->count++;
2267 tty->driver_data = info;
2268 info->tty = tty;
2270 if(!tmp_buf) {
2271 page =get_free_page(GFP_KERNEL);
2272 if(!page)
2273 return-ENOMEM;
2274 if(tmp_buf)
2275 free_page(page);
2276 else
2277 tmp_buf = (unsigned char*) page;
2281 * Start up serial port
2283 retval =startup(info);
2284 if(retval)
2285 return retval;
2287 retval =block_til_ready(tty, filp, info);
2288 if(retval) {
2289 #ifdef SERIAL_DEBUG_OPEN
2290 printk("esp_open returning after block_til_ready with %d\n",
2291 retval);
2292 #endif
2293 return retval;
2296 if((info->count ==1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
2297 if(tty->driver.subtype == SERIAL_TYPE_NORMAL)
2298 *tty->termios = info->normal_termios;
2299 else
2300 *tty->termios = info->callout_termios;
2301 change_speed(info);
2304 info->session = current->session;
2305 info->pgrp = current->pgrp;
2307 #ifdef SERIAL_DEBUG_OPEN
2308 printk("esp_open ttys%d successful...", info->line);
2309 #endif
2310 return0;
2314 * ---------------------------------------------------------------------
2315 * esp_init() and friends
2317 * esp_init() is called at boot-time to initialize the serial driver.
2318 * ---------------------------------------------------------------------
2322 * This routine prints out the appropriate serial driver version
2323 * number, and identifies which options were configured into this
2324 * driver.
2326 static voidshow_serial_version(void)
2328 printk(KERN_INFO "%s version %s (DMA %u, trigger level %u)\n",
2329 serial_name, serial_version, dma, trigger);
2333 * This routine is called by esp_init() to initialize a specific serial
2334 * port. It determines what type of UART chip this serial port is
2335 * using: 8250, 16450, 16550, 16550A. The important question is
2336 * whether or not this UART is a 16550A or not, since this will
2337 * determine whether or not we can use its FIFO features or not.
2339 static voidautoconfig(struct esp_struct * info)
2341 unsigned char status1, status2, scratch;
2342 unsigned port = info->port;
2343 unsigned long flags;
2345 info->type = PORT_UNKNOWN;
2347 if(!port)
2348 return;
2350 save_flags(flags);cli();
2353 * Check for ESP card
2355 scratch =serial_in(info, UART_ESI_BASE);
2356 if(scratch ==0xf3) {
2357 serial_out(info, UART_ESI_CMD1,0x00);
2358 serial_out(info, UART_ESI_CMD1,0x01);
2359 status1 =serial_in(info, UART_ESI_STAT2);
2360 status2 = status1 &0x70;
2361 if(status2 !=0x20) {
2362 printk(" Old ESP found at %x\n",info->port);
2363 }else{
2364 serial_out(info, UART_ESI_CMD1,0x02);
2365 status1 =serial_in(info, UART_ESI_STAT1) &0x03;
2366 if(!(info->irq)) {
2367 if((status1 ==0x00) || (status1 ==0x02))
2368 info->irq =4;
2369 else
2370 info->irq =3;
2372 info->type = PORT_16550A;
2373 request_region(port,8,"esp(auto)");
2375 /* put card in enhanced mode */
2376 /* this prevents access through */
2377 /* the "old" IO ports */
2378 esp_basic_init(info);
2380 /* clear out MCR */
2381 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2382 serial_out(info, UART_ESI_CMD2, UART_MCR);
2383 serial_out(info, UART_ESI_CMD2,0x00);
2387 restore_flags(flags);
2391 * The serial driver boot-time initialization code!
2393 intesp_init(void)
2395 int i, offset;
2396 struct esp_struct * info;
2397 int esp[] = {0x100,0x140,0x180,0x200,0x240,0x280,0x300,0x380};
2399 init_bh(ESP_BH, do_serial_bh);
2401 for(i =0; i <16; i++) {
2402 IRQ_ports[i] =0;
2405 if((dma !=1) && (dma !=3))
2406 dma = CONFIG_ESP_DMA_CHANNEL;
2408 if((trigger <1) || (trigger >1015))
2409 trigger =768;
2411 show_serial_version();
2413 /* Initialize the tty_driver structure */
2415 memset(&esp_driver,0,sizeof(struct tty_driver));
2416 esp_driver.magic = TTY_DRIVER_MAGIC;
2417 esp_driver.name ="ttyP";
2418 esp_driver.major = ESP_IN_MAJOR;
2419 esp_driver.minor_start =0;
2420 esp_driver.num = NR_PORTS;
2421 esp_driver.type = TTY_DRIVER_TYPE_SERIAL;
2422 esp_driver.subtype = SERIAL_TYPE_NORMAL;
2423 esp_driver.init_termios = tty_std_termios;
2424 esp_driver.init_termios.c_cflag =
2425 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2426 esp_driver.flags = TTY_DRIVER_REAL_RAW;
2427 esp_driver.refcount = &serial_refcount;
2428 esp_driver.table = serial_table;
2429 esp_driver.termios = serial_termios;
2430 esp_driver.termios_locked = serial_termios_locked;
2432 esp_driver.open = esp_open;
2433 esp_driver.close = rs_close;
2434 esp_driver.write = rs_write;
2435 esp_driver.put_char = rs_put_char;
2436 esp_driver.flush_chars = rs_flush_chars;
2437 esp_driver.write_room = rs_write_room;
2438 esp_driver.chars_in_buffer = rs_chars_in_buffer;
2439 esp_driver.flush_buffer = rs_flush_buffer;
2440 esp_driver.ioctl = rs_ioctl;
2441 esp_driver.throttle = rs_throttle;
2442 esp_driver.unthrottle = rs_unthrottle;
2443 esp_driver.set_termios = rs_set_termios;
2444 esp_driver.stop = rs_stop;
2445 esp_driver.start = rs_start;
2446 esp_driver.hangup = esp_hangup;
2449 * The callout device is just like normal device except for
2450 * major number and the subtype code.
2452 esp_callout_driver = esp_driver;
2453 esp_callout_driver.name ="cup";
2454 esp_callout_driver.major = ESP_OUT_MAJOR;
2455 esp_callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2457 if(tty_register_driver(&esp_driver))
2458 panic("Couldn't register serial driver\n");
2459 if(tty_register_driver(&esp_callout_driver))
2460 panic("Couldn't register callout driver\n");
2462 info = (struct esp_struct *)kmalloc(sizeof(struct esp_struct),
2463 GFP_KERNEL);
2464 if(!info)
2465 panic("Could not allocate memory for device information\n");
2466 memset((void*)info,0,sizeof(struct esp_struct));
2468 i =0;
2469 offset =0;
2472 info->port = esp[i] + offset;
2473 info->baud_base = BASE_BAUD;
2474 info->custom_divisor = (divisor[i] >> (offset /2)) &0xf;
2475 info->flags = STD_COM_FLAGS;
2476 if(info->custom_divisor)
2477 info->flags |= ASYNC_SPD_CUST;
2478 info->irq = irq[i];
2480 info->magic = ESP_MAGIC;
2481 info->line = (i *8) + (offset /8);
2482 info->tty =0;
2483 info->type = PORT_UNKNOWN;
2484 info->close_delay =5*HZ/10;
2485 info->closing_wait =30*HZ;
2486 info->tqueue.routine = do_softint;
2487 info->tqueue.data = info;
2488 info->tqueue_hangup.routine = do_serial_hangup;
2489 info->tqueue_hangup.data = info;
2490 info->callout_termios = esp_callout_driver.init_termios;
2491 info->normal_termios = esp_driver.init_termios;
2493 if(info->irq ==2)
2494 info->irq =9;
2496 autoconfig(info);
2497 if(info->type == PORT_UNKNOWN) {
2498 i++;
2499 offset =0;
2500 continue;
2502 if(IRQ_ports[0])
2503 IRQ_ports[0]->prev_port = info;
2504 info->next_port = IRQ_ports[0];
2505 info->prev_port =0;
2506 IRQ_ports[0] = info;
2507 printk(KERN_INFO "ttyP%d at 0x%04x (irq = %d) is an ESP ",
2508 info->line, info->port, info->irq);
2509 if(info->line %8)
2510 printk("secondary port\n");
2511 else{
2512 printk("primary port\n");
2513 irq[i] = info->irq;
2516 info = (struct esp_struct *)kmalloc(sizeof(struct esp_struct),
2517 GFP_KERNEL);
2518 if(!info)
2519 panic("Could not allocate memory for device information\n");
2520 memset((void*)info,0,sizeof(struct esp_struct));
2522 if(offset ==56) {
2523 i++;
2524 offset =0;
2525 }else{
2526 offset +=8;
2528 }while(i < NR_PRIMARY);
2530 /* free the last port memory allocation */
2531 kfree(info);
2533 return0;
2536 #ifdef MODULE
2538 intinit_module(void)
2540 returnesp_init();
2543 voidcleanup_module(void)
2545 unsigned long flags;
2546 int e1, e2;
2547 struct esp_struct *current_async, *temp_async;
2549 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2550 save_flags(flags);
2551 cli();
2552 if((e1 =tty_unregister_driver(&esp_driver)))
2553 printk("SERIAL: failed to unregister serial driver (%d)\n",
2554 e1);
2555 if((e2 =tty_unregister_driver(&esp_callout_driver)))
2556 printk("SERIAL: failed to unregister callout driver (%d)\n",
2557 e2);
2558 restore_flags(flags);
2560 current_async = IRQ_ports[0];
2561 while(current_async !=0) {
2562 if(current_async->type != PORT_UNKNOWN)
2563 release_region(current_async->port,8);
2564 current_async = current_async->next_port;
2567 if(dma_buffer)
2568 free_pages((unsigned int)dma_buffer,
2569 __get_order(DMA_BUFFER_SZ));
2571 if(tmp_buf)
2572 free_page((unsigned long)tmp_buf);
2574 /* free the port information */
2575 current_async = IRQ_ports[0];
2576 while(current_async !=0) {
2577 temp_async = current_async->next_port;
2578 kfree(current_async);
2579 current_async = temp_async;
2582 #endif/* MODULE */
close