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