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