ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 = 0;
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 = 0;
1042                 }               
1043         }
1044
1045         if (info->xmit_buf) {
1046                 free_page((unsigned long) info->xmit_buf);
1047                 info->xmit_buf = 0;
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 * retinfo)
1439 {
1440         struct serial_struct tmp;
1441   
1442         if (!retinfo)
1443                 return -EFAULT;
1444         memset(&tmp, 0, sizeof(tmp));
1445         tmp.type = PORT_16550A;
1446         tmp.line = info->line;
1447         tmp.port = info->port;
1448         tmp.irq = info->irq;
1449         tmp.flags = info->flags;
1450         tmp.xmit_fifo_size = 1024;
1451         tmp.baud_base = BASE_BAUD;
1452         tmp.close_delay = info->close_delay;
1453         tmp.closing_wait = info->closing_wait;
1454         tmp.custom_divisor = info->custom_divisor;
1455         tmp.hub6 = 0;
1456         if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1457                 return -EFAULT;
1458         return 0;
1459 }
1460
1461 static int get_esp_config(struct esp_struct * info,
1462                           struct hayes_esp_config * retinfo)
1463 {
1464         struct hayes_esp_config tmp;
1465   
1466         if (!retinfo)
1467                 return -EFAULT;
1468
1469         memset(&tmp, 0, sizeof(tmp));
1470         tmp.rx_timeout = info->config.rx_timeout;
1471         tmp.rx_trigger = info->config.rx_trigger;
1472         tmp.tx_trigger = info->config.tx_trigger;
1473         tmp.flow_off = info->config.flow_off;
1474         tmp.flow_on = info->config.flow_on;
1475         tmp.pio_threshold = info->config.pio_threshold;
1476         tmp.dma_channel = (info->stat_flags & ESP_STAT_NEVER_DMA ? 0 : dma);
1477
1478         return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
1479 }
1480
1481 static int set_serial_info(struct esp_struct * info,
1482                            struct serial_struct * new_info)
1483 {
1484         struct serial_struct new_serial;
1485         struct esp_struct old_info;
1486         unsigned int change_irq;
1487         int retval = 0;
1488         struct esp_struct *current_async;
1489
1490         if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1491                 return -EFAULT;
1492         old_info = *info;
1493
1494         if ((new_serial.type != PORT_16550A) ||
1495             (new_serial.hub6) ||
1496             (info->port != new_serial.port) ||
1497             (new_serial.baud_base != BASE_BAUD) ||
1498             (new_serial.irq > 15) ||
1499             (new_serial.irq < 2) ||
1500             (new_serial.irq == 6) ||
1501             (new_serial.irq == 8) ||
1502             (new_serial.irq == 13))
1503                 return -EINVAL;
1504
1505         change_irq = new_serial.irq != info->irq;
1506
1507         if (change_irq && (info->line % 8))
1508                 return -EINVAL;
1509
1510         if (!capable(CAP_SYS_ADMIN)) {
1511                 if (change_irq || 
1512                     (new_serial.close_delay != info->close_delay) ||
1513                     ((new_serial.flags & ~ASYNC_USR_MASK) !=
1514                      (info->flags & ~ASYNC_USR_MASK)))
1515                         return -EPERM;
1516                 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1517                                (new_serial.flags & ASYNC_USR_MASK));
1518                 info->custom_divisor = new_serial.custom_divisor;
1519         } else {
1520                 if (new_serial.irq == 2)
1521                         new_serial.irq = 9;
1522
1523                 if (change_irq) {
1524                         current_async = ports;
1525
1526                         while (current_async) {
1527                                 if ((current_async->line >= info->line) &&
1528                                     (current_async->line < (info->line + 8))) {
1529                                         if (current_async == info) {
1530                                                 if (current_async->count > 1)
1531                                                         return -EBUSY;
1532                                         } else if (current_async->count)
1533                                                 return -EBUSY;
1534                                 }
1535
1536                                 current_async = current_async->next_port;
1537                         }
1538                 }
1539
1540                 /*
1541                  * OK, past this point, all the error checking has been done.
1542                  * At this point, we start making changes.....
1543                  */
1544
1545                 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1546                                (new_serial.flags & ASYNC_FLAGS));
1547                 info->custom_divisor = new_serial.custom_divisor;
1548                 info->close_delay = new_serial.close_delay * HZ/100;
1549                 info->closing_wait = new_serial.closing_wait * HZ/100;
1550
1551                 if (change_irq) {
1552                         /*
1553                          * We need to shutdown the serial port at the old
1554                          * port/irq combination.
1555                          */
1556                         shutdown(info);
1557
1558                         current_async = ports;
1559
1560                         while (current_async) {
1561                                 if ((current_async->line >= info->line) &&
1562                                     (current_async->line < (info->line + 8)))
1563                                         current_async->irq = new_serial.irq;
1564
1565                                 current_async = current_async->next_port;
1566                         }
1567
1568                         serial_out(info, UART_ESI_CMD1, ESI_SET_ENH_IRQ);
1569                         if (info->irq == 9)
1570                                 serial_out(info, UART_ESI_CMD2, 0x02);
1571                         else
1572                                 serial_out(info, UART_ESI_CMD2, info->irq);
1573                 }
1574         }
1575
1576         if (info->flags & ASYNC_INITIALIZED) {
1577                 if (((old_info.flags & ASYNC_SPD_MASK) !=
1578                      (info->flags & ASYNC_SPD_MASK)) ||
1579                     (old_info.custom_divisor != info->custom_divisor)) {
1580                         if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1581                                 info->tty->alt_speed = 57600;
1582                         if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1583                                 info->tty->alt_speed = 115200;
1584                         if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1585                                 info->tty->alt_speed = 230400;
1586                         if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1587                                 info->tty->alt_speed = 460800;
1588                         change_speed(info);
1589                 }
1590         } else
1591                 retval = startup(info);
1592
1593         return retval;
1594 }
1595
1596 static int set_esp_config(struct esp_struct * info,
1597                           struct hayes_esp_config * new_info)
1598 {
1599         struct hayes_esp_config new_config;
1600         unsigned int change_dma;
1601         int retval = 0;
1602         struct esp_struct *current_async;
1603
1604         /* Perhaps a non-sysadmin user should be able to do some of these */
1605         /* operations.  I haven't decided yet. */
1606
1607         if (!capable(CAP_SYS_ADMIN))
1608                 return -EPERM;
1609
1610         if (copy_from_user(&new_config, new_info, sizeof(new_config)))
1611                 return -EFAULT;
1612
1613         if ((new_config.flow_on >= new_config.flow_off) ||
1614             (new_config.rx_trigger < 1) ||
1615             (new_config.tx_trigger < 1) ||
1616             (new_config.flow_off < 1) ||
1617             (new_config.flow_on < 1) ||
1618             (new_config.rx_trigger > 1023) ||
1619             (new_config.tx_trigger > 1023) ||
1620             (new_config.flow_off > 1023) ||
1621             (new_config.flow_on > 1023) ||
1622             (new_config.pio_threshold < 0) ||
1623             (new_config.pio_threshold > 1024))
1624                 return -EINVAL;
1625
1626         if ((new_config.dma_channel != 1) && (new_config.dma_channel != 3))
1627                 new_config.dma_channel = 0;
1628
1629         if (info->stat_flags & ESP_STAT_NEVER_DMA)
1630                 change_dma = new_config.dma_channel;
1631         else
1632                 change_dma = (new_config.dma_channel != dma);
1633
1634         if (change_dma) {
1635                 if (new_config.dma_channel) {
1636                         /* PIO mode to DMA mode transition OR */
1637                         /* change current DMA channel */
1638                         
1639                         current_async = ports;
1640
1641                         while (current_async) {
1642                                 if (current_async == info) {
1643                                         if (current_async->count > 1)
1644                                                 return -EBUSY;
1645                                 } else if (current_async->count)
1646                                         return -EBUSY;
1647                                         
1648                                 current_async =
1649                                         current_async->next_port;
1650                         }
1651
1652                         shutdown(info);
1653                         dma = new_config.dma_channel;
1654                         info->stat_flags &= ~ESP_STAT_NEVER_DMA;
1655                         
1656                         /* all ports must use the same DMA channel */
1657
1658                         current_async = ports;
1659
1660                         while (current_async) {
1661                                 esp_basic_init(current_async);
1662                                 current_async = current_async->next_port;
1663                         }
1664                 } else {
1665                         /* DMA mode to PIO mode only */
1666                         
1667                         if (info->count > 1)
1668                                 return -EBUSY;
1669
1670                         shutdown(info);
1671                         info->stat_flags |= ESP_STAT_NEVER_DMA;
1672                         esp_basic_init(info);
1673                 }
1674         }
1675
1676         info->config.pio_threshold = new_config.pio_threshold;
1677
1678         if ((new_config.flow_off != info->config.flow_off) ||
1679             (new_config.flow_on != info->config.flow_on)) {
1680                 unsigned long flags;
1681
1682                 info->config.flow_off = new_config.flow_off;
1683                 info->config.flow_on = new_config.flow_on;
1684                 save_flags(flags); cli();
1685                 serial_out(info, UART_ESI_CMD1, ESI_SET_FLOW_LVL);
1686                 serial_out(info, UART_ESI_CMD2, new_config.flow_off >> 8);
1687                 serial_out(info, UART_ESI_CMD2, new_config.flow_off);
1688                 serial_out(info, UART_ESI_CMD2, new_config.flow_on >> 8);
1689                 serial_out(info, UART_ESI_CMD2, new_config.flow_on);
1690                 restore_flags(flags);
1691         }
1692
1693         if ((new_config.rx_trigger != info->config.rx_trigger) ||
1694             (new_config.tx_trigger != info->config.tx_trigger)) {
1695                 unsigned long flags;
1696
1697                 info->config.rx_trigger = new_config.rx_trigger;
1698                 info->config.tx_trigger = new_config.tx_trigger;
1699                 save_flags(flags); cli();
1700                 serial_out(info, UART_ESI_CMD1, ESI_SET_TRIGGER);
1701                 serial_out(info, UART_ESI_CMD2,
1702                            new_config.rx_trigger >> 8);
1703                 serial_out(info, UART_ESI_CMD2, new_config.rx_trigger);
1704                 serial_out(info, UART_ESI_CMD2,
1705                            new_config.tx_trigger >> 8);
1706                 serial_out(info, UART_ESI_CMD2, new_config.tx_trigger);
1707                 restore_flags(flags);
1708         }
1709
1710         if (new_config.rx_timeout != info->config.rx_timeout) {
1711                 unsigned long flags;
1712
1713                 info->config.rx_timeout = new_config.rx_timeout;
1714                 save_flags(flags); cli();
1715
1716                 if (info->IER & UART_IER_RDI) {
1717                         serial_out(info, UART_ESI_CMD1,
1718                                    ESI_SET_RX_TIMEOUT);
1719                         serial_out(info, UART_ESI_CMD2,
1720                                    new_config.rx_timeout);
1721                 }
1722
1723                 restore_flags(flags);
1724         }
1725
1726         if (!(info->flags & ASYNC_INITIALIZED))
1727                 retval = startup(info);
1728
1729         return retval;
1730 }
1731
1732 /*
1733  * get_lsr_info - get line status register info
1734  *
1735  * Purpose: Let user call ioctl() to get info when the UART physically
1736  *          is emptied.  On bus types like RS485, the transmitter must
1737  *          release the bus after transmitting. This must be done when
1738  *          the transmit shift register is empty, not be done when the
1739  *          transmit holding register is empty.  This functionality
1740  *          allows an RS485 driver to be written in user space. 
1741  */
1742 static int get_lsr_info(struct esp_struct * info, unsigned int *value)
1743 {
1744         unsigned char status;
1745         unsigned int result;
1746
1747         cli();
1748         serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1749         status = serial_in(info, UART_ESI_STAT1);
1750         sti();
1751         result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1752         return put_user(result,value);
1753 }
1754
1755
1756 static int esp_tiocmget(struct tty_struct *tty, struct file *file)
1757 {
1758         struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1759         unsigned char control, status;
1760
1761         if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1762                 return -ENODEV;
1763         if (tty->flags & (1 << TTY_IO_ERROR))
1764                 return -EIO;
1765
1766         control = info->MCR;
1767         cli();
1768         serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
1769         status = serial_in(info, UART_ESI_STAT2);
1770         sti();
1771         return    ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1772                 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1773                 | ((status  & UART_MSR_DCD) ? TIOCM_CAR : 0)
1774                 | ((status  & UART_MSR_RI) ? TIOCM_RNG : 0)
1775                 | ((status  & UART_MSR_DSR) ? TIOCM_DSR : 0)
1776                 | ((status  & UART_MSR_CTS) ? TIOCM_CTS : 0);
1777 }
1778
1779 static int esp_tiocmset(struct tty_struct *tty, struct file *file,
1780                         unsigned int set, unsigned int clear)
1781 {
1782         struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1783         unsigned int arg;
1784
1785         if (serial_paranoia_check(info, tty->name, __FUNCTION__))
1786                 return -ENODEV;
1787         if (tty->flags & (1 << TTY_IO_ERROR))
1788                 return -EIO;
1789
1790         cli();
1791
1792         if (set & TIOCM_RTS)
1793                 info->MCR |= UART_MCR_RTS;
1794         if (set & TIOCM_DTR)
1795                 info->MCR |= UART_MCR_DTR;
1796
1797         if (clear & TIOCM_RTS)
1798                 info->MCR &= ~UART_MCR_RTS;
1799         if (clear & TIOCM_DTR)
1800                 info->MCR &= ~UART_MCR_DTR;
1801
1802         serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1803         serial_out(info, UART_ESI_CMD2, UART_MCR);
1804         serial_out(info, UART_ESI_CMD2, info->MCR);
1805         sti();
1806         return 0;
1807 }
1808
1809 /*
1810  * rs_break() --- routine which turns the break handling on or off
1811  */
1812 static void esp_break(struct tty_struct *tty, int break_state)
1813 {
1814         struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1815         unsigned long flags;
1816         
1817         if (serial_paranoia_check(info, tty->name, "esp_break"))
1818                 return;
1819
1820         save_flags(flags); cli();
1821         if (break_state == -1) {
1822                 serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1823                 serial_out(info, UART_ESI_CMD2, 0x01);
1824
1825                 interruptible_sleep_on(&info->break_wait);
1826         } else {
1827                 serial_out(info, UART_ESI_CMD1, ESI_ISSUE_BREAK);
1828                 serial_out(info, UART_ESI_CMD2, 0x00);
1829         }
1830         restore_flags(flags);
1831 }
1832
1833 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1834                     unsigned int cmd, unsigned long arg)
1835 {
1836         struct esp_struct * info = (struct esp_struct *)tty->driver_data;
1837         struct async_icount cprev, cnow;        /* kernel counter temps */
1838         struct serial_icounter_struct *p_cuser; /* user space */
1839
1840         if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1841                 return -ENODEV;
1842
1843         if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1844             (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
1845             (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT) &&
1846             (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT) &&
1847             (cmd != TIOCGHAYESESP) && (cmd != TIOCSHAYESESP)) {
1848                 if (tty->flags & (1 << TTY_IO_ERROR))
1849                     return -EIO;
1850         }
1851         
1852         switch (cmd) {
1853                 case TIOCGSERIAL:
1854                         return get_serial_info(info,
1855                                                (struct serial_struct *) arg);
1856                 case TIOCSSERIAL:
1857                         return set_serial_info(info,
1858                                                (struct serial_struct *) arg);
1859                 case TIOCSERCONFIG:
1860                         /* do not reconfigure after initial configuration */
1861                         return 0;
1862
1863                 case TIOCSERGWILD:
1864                         return put_user(0L, (unsigned long *) arg);
1865
1866                 case TIOCSERGETLSR: /* Get line status register */
1867                             return get_lsr_info(info, (unsigned int *) arg);
1868
1869                 case TIOCSERSWILD:
1870                         if (!capable(CAP_SYS_ADMIN))
1871                                 return -EPERM;
1872                         return 0;
1873
1874                 /*
1875                  * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1876                  * - mask passed in arg for lines of interest
1877                  *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1878                  * Caller should use TIOCGICOUNT to see which one it was
1879                  */
1880                  case TIOCMIWAIT:
1881                         cli();
1882                         cprev = info->icount;   /* note the counters on entry */
1883                         sti();
1884                         while (1) {
1885                                 interruptible_sleep_on(&info->delta_msr_wait);
1886                                 /* see if a signal did it */
1887                                 if (signal_pending(current))
1888                                         return -ERESTARTSYS;
1889                                 cli();
1890                                 cnow = info->icount;    /* atomic copy */
1891                                 sti();
1892                                 if (cnow.rng == cprev.rng &&
1893                                     cnow.dsr == cprev.dsr && 
1894                                     cnow.dcd == cprev.dcd &&
1895                                     cnow.cts == cprev.cts)
1896                                         return -EIO; /* no change => error */
1897                                 if (((arg & TIOCM_RNG) &&
1898                                      (cnow.rng != cprev.rng)) ||
1899                                      ((arg & TIOCM_DSR) &&
1900                                       (cnow.dsr != cprev.dsr)) ||
1901                                      ((arg & TIOCM_CD) &&
1902                                       (cnow.dcd != cprev.dcd)) ||
1903                                      ((arg & TIOCM_CTS) &&
1904                                       (cnow.cts != cprev.cts)) ) {
1905                                         return 0;
1906                                 }
1907                                 cprev = cnow;
1908                         }
1909                         /* NOTREACHED */
1910
1911                 /* 
1912                  * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1913                  * Return: write counters to the user passed counter struct
1914                  * NB: both 1->0 and 0->1 transitions are counted except for
1915                  *     RI where only 0->1 is counted.
1916                  */
1917                 case TIOCGICOUNT:
1918                         cli();
1919                         cnow = info->icount;
1920                         sti();
1921                         p_cuser = (struct serial_icounter_struct *) arg;
1922                         if (put_user(cnow.cts, &p_cuser->cts) ||
1923                             put_user(cnow.dsr, &p_cuser->dsr) ||
1924                             put_user(cnow.rng, &p_cuser->rng) ||
1925                             put_user(cnow.dcd, &p_cuser->dcd))
1926                                 return -EFAULT;
1927
1928                         return 0;
1929         case TIOCGHAYESESP:
1930                 return (get_esp_config(info, (struct hayes_esp_config *)arg));
1931         case TIOCSHAYESESP:
1932                 return (set_esp_config(info, (struct hayes_esp_config *)arg));
1933
1934                 default:
1935                         return -ENOIOCTLCMD;
1936                 }
1937         return 0;
1938 }
1939
1940 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1941 {
1942         struct esp_struct *info = (struct esp_struct *)tty->driver_data;
1943
1944         if (   (tty->termios->c_cflag == old_termios->c_cflag)
1945             && (   RELEVANT_IFLAG(tty->termios->c_iflag) 
1946                 == RELEVANT_IFLAG(old_termios->c_iflag)))
1947           return;
1948
1949         change_speed(info);
1950
1951         /* Handle transition to B0 status */
1952         if ((old_termios->c_cflag & CBAUD) &&
1953                 !(tty->termios->c_cflag & CBAUD)) {
1954                 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1955                 cli();
1956                 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1957                 serial_out(info, UART_ESI_CMD2, UART_MCR);
1958                 serial_out(info, UART_ESI_CMD2, info->MCR);
1959                 sti();
1960         }
1961
1962         /* Handle transition away from B0 status */
1963         if (!(old_termios->c_cflag & CBAUD) &&
1964                 (tty->termios->c_cflag & CBAUD)) {
1965                 info->MCR |= (UART_MCR_DTR | UART_MCR_RTS);
1966                 cli();
1967                 serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
1968                 serial_out(info, UART_ESI_CMD2, UART_MCR);
1969                 serial_out(info, UART_ESI_CMD2, info->MCR);
1970                 sti();
1971         }
1972
1973         /* Handle turning of CRTSCTS */
1974         if ((old_termios->c_cflag & CRTSCTS) &&
1975             !(tty->termios->c_cflag & CRTSCTS)) {
1976                 rs_start(tty);
1977         }
1978
1979 #if 0
1980         /*
1981          * No need to wake up processes in open wait, since they
1982          * sample the CLOCAL flag once, and don't recheck it.
1983          * XXX  It's not clear whether the current behavior is correct
1984          * or not.  Hence, this may change.....
1985          */
1986         if (!(old_termios->c_cflag & CLOCAL) &&
1987             (tty->termios->c_cflag & CLOCAL))
1988                 wake_up_interruptible(&info->open_wait);
1989 #endif
1990 }
1991
1992 /*
1993  * ------------------------------------------------------------
1994  * rs_close()
1995  * 
1996  * This routine is called when the serial port gets closed.  First, we
1997  * wait for the last remaining data to be sent.  Then, we unlink its
1998  * async structure from the interrupt chain if necessary, and we free
1999  * that IRQ if nothing is left in the chain.
2000  * ------------------------------------------------------------
2001  */
2002 static void rs_close(struct tty_struct *tty, struct file * filp)
2003 {
2004         struct esp_struct * info = (struct esp_struct *)tty->driver_data;
2005         unsigned long flags;
2006
2007         if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
2008                 return;
2009         
2010         save_flags(flags); cli();
2011         
2012         if (tty_hung_up_p(filp)) {
2013                 DBG_CNT("before DEC-hung");
2014                 goto out;
2015         }
2016         
2017 #ifdef SERIAL_DEBUG_OPEN
2018         printk("rs_close ttys%d, count = %d\n", info->line, info->count);
2019 #endif
2020         if ((tty->count == 1) && (info->count != 1)) {
2021                 /*
2022                  * Uh, oh.  tty->count is 1, which means that the tty
2023                  * structure will be freed.  Info->count should always
2024                  * be one in these conditions.  If it's greater than
2025                  * one, we've got real problems, since it means the
2026                  * serial port won't be shutdown.
2027                  */
2028                 printk("rs_close: bad serial port count; tty->count is 1, "
2029                        "info->count is %d\n", info->count);
2030                 info->count = 1;
2031         }
2032         if (--info->count < 0) {
2033                 printk("rs_close: bad serial port count for ttys%d: %d\n",
2034                        info->line, info->count);
2035                 info->count = 0;
2036         }
2037         if (info->count) {
2038                 DBG_CNT("before DEC-2");
2039                 goto out;
2040         }
2041         info->flags |= ASYNC_CLOSING;
2042         /*
2043          * Now we wait for the transmit buffer to clear; and we notify 
2044          * the line discipline to only process XON/XOFF characters.
2045          */
2046         tty->closing = 1;
2047         if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
2048                 tty_wait_until_sent(tty, info->closing_wait);
2049         /*
2050          * At this point we stop accepting input.  To do this, we
2051          * disable the receive line status interrupts, and tell the
2052          * interrupt driver to stop checking the data ready bit in the
2053          * line status register.
2054          */
2055         /* info->IER &= ~UART_IER_RLSI; */
2056         info->IER &= ~UART_IER_RDI;
2057         info->read_status_mask &= ~UART_LSR_DR;
2058         if (info->flags & ASYNC_INITIALIZED) {
2059                 serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK);
2060                 serial_out(info, UART_ESI_CMD2, info->IER);
2061
2062                 /* disable receive timeout */
2063                 serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT);
2064                 serial_out(info, UART_ESI_CMD2, 0x00);
2065
2066                 /*
2067                  * Before we drop DTR, make sure the UART transmitter
2068                  * has completely drained; this is especially
2069                  * important if there is a transmit FIFO!
2070                  */
2071                 rs_wait_until_sent(tty, info->timeout);
2072         }
2073         shutdown(info);
2074         if (tty->driver->flush_buffer)
2075                 tty->driver->flush_buffer(tty);
2076         if (tty->ldisc.flush_buffer)
2077                 tty->ldisc.flush_buffer(tty);
2078         tty->closing = 0;
2079         info->event = 0;
2080         info->tty = 0;
2081
2082         if (info->blocked_open) {
2083                 if (info->close_delay) {
2084                         set_current_state(TASK_INTERRUPTIBLE);
2085                         schedule_timeout(info->close_delay);
2086                 }
2087                 wake_up_interruptible(&info->open_wait);
2088         }
2089         info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
2090         wake_up_interruptible(&info->close_wait);
2091 out:
2092         restore_flags(flags);
2093 }
2094
2095 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
2096 {
2097         struct esp_struct *info = (struct esp_struct *)tty->driver_data;
2098         unsigned long orig_jiffies, char_time;
2099         unsigned long flags;
2100
2101         if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
2102                 return;
2103
2104         orig_jiffies = jiffies;
2105         char_time = ((info->timeout - HZ / 50) / 1024) / 5;
2106
2107         if (!char_time)
2108                 char_time = 1;
2109
2110         save_flags(flags); cli();
2111         serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2112         serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2113
2114         while ((serial_in(info, UART_ESI_STAT1) != 0x03) ||
2115                 (serial_in(info, UART_ESI_STAT2) != 0xff)) {
2116                 set_current_state(TASK_INTERRUPTIBLE);
2117                 schedule_timeout(char_time);
2118
2119                 if (signal_pending(current))
2120                         break;
2121
2122                 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2123                         break;
2124
2125                 serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND);
2126                 serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL);
2127         }
2128         
2129         restore_flags(flags);
2130         set_current_state(TASK_RUNNING);
2131 }
2132
2133 /*
2134  * esp_hangup() --- called by tty_hangup() when a hangup is signaled.
2135  */
2136 static void esp_hangup(struct tty_struct *tty)
2137 {
2138         struct esp_struct * info = (struct esp_struct *)tty->driver_data;
2139         
2140         if (serial_paranoia_check(info, tty->name, "esp_hangup"))
2141                 return;
2142         
2143         rs_flush_buffer(tty);
2144         shutdown(info);
2145         info->event = 0;
2146         info->count = 0;
2147         info->flags &= ~ASYNC_NORMAL_ACTIVE;
2148         info->tty = 0;
2149         wake_up_interruptible(&info->open_wait);
2150 }
2151
2152 /*
2153  * ------------------------------------------------------------
2154  * esp_open() and friends
2155  * ------------------------------------------------------------
2156  */
2157 static int block_til_ready(struct tty_struct *tty, struct file * filp,
2158                            struct esp_struct *info)
2159 {
2160         DECLARE_WAITQUEUE(wait, current);
2161         int             retval;
2162         int             do_clocal = 0;
2163         unsigned long   flags;
2164
2165         /*
2166          * If the device is in the middle of being closed, then block
2167          * until it's done, and then try again.
2168          */
2169         if (tty_hung_up_p(filp) ||
2170             (info->flags & ASYNC_CLOSING)) {
2171                 if (info->flags & ASYNC_CLOSING)
2172                         interruptible_sleep_on(&info->close_wait);
2173 #ifdef SERIAL_DO_RESTART
2174                 if (info->flags & ASYNC_HUP_NOTIFY)
2175                         return -EAGAIN;
2176                 else
2177                         return -ERESTARTSYS;
2178 #else
2179                 return -EAGAIN;
2180 #endif
2181         }
2182
2183         /*
2184          * If non-blocking mode is set, or the port is not enabled,
2185          * then make the check up front and then exit.
2186          */
2187         if ((filp->f_flags & O_NONBLOCK) ||
2188             (tty->flags & (1 << TTY_IO_ERROR))) {
2189                 info->flags |= ASYNC_NORMAL_ACTIVE;
2190                 return 0;
2191         }
2192
2193         if (tty->termios->c_cflag & CLOCAL)
2194                 do_clocal = 1;
2195
2196         /*
2197          * Block waiting for the carrier detect and the line to become
2198          * free (i.e., not in use by the callout).  While we are in
2199          * this loop, info->count is dropped by one, so that
2200          * rs_close() knows when to free things.  We restore it upon
2201          * exit, either normal or abnormal.
2202          */
2203         retval = 0;
2204         add_wait_queue(&info->open_wait, &wait);
2205 #ifdef SERIAL_DEBUG_OPEN
2206         printk("block_til_ready before block: ttys%d, count = %d\n",
2207                info->line, info->count);
2208 #endif
2209         save_flags(flags);
2210         cli();
2211         if (!tty_hung_up_p(filp)) 
2212                 info->count--;
2213         restore_flags(flags);
2214         info->blocked_open++;
2215         while (1) {
2216                 save_flags(flags);
2217                 cli();
2218                 if ((tty->termios->c_cflag & CBAUD)) {
2219                         unsigned int scratch;
2220
2221                         serial_out(info, UART_ESI_CMD1, ESI_READ_UART);
2222                         serial_out(info, UART_ESI_CMD2, UART_MCR);
2223                         scratch = serial_in(info, UART_ESI_STAT1);
2224                         serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2225                         serial_out(info, UART_ESI_CMD2, UART_MCR);
2226                         serial_out(info, UART_ESI_CMD2,
2227                                 scratch | UART_MCR_DTR | UART_MCR_RTS);
2228                 }
2229                 restore_flags(flags);
2230                 set_current_state(TASK_INTERRUPTIBLE);
2231                 if (tty_hung_up_p(filp) ||
2232                     !(info->flags & ASYNC_INITIALIZED)) {
2233 #ifdef SERIAL_DO_RESTART
2234                         if (info->flags & ASYNC_HUP_NOTIFY)
2235                                 retval = -EAGAIN;
2236                         else
2237                                 retval = -ERESTARTSYS;  
2238 #else
2239                         retval = -EAGAIN;
2240 #endif
2241                         break;
2242                 }
2243
2244                 serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT);
2245                 if (serial_in(info, UART_ESI_STAT2) & UART_MSR_DCD)
2246                         do_clocal = 1;
2247
2248                 if (!(info->flags & ASYNC_CLOSING) &&
2249                     (do_clocal))
2250                         break;
2251                 if (signal_pending(current)) {
2252                         retval = -ERESTARTSYS;
2253                         break;
2254                 }
2255 #ifdef SERIAL_DEBUG_OPEN
2256                 printk("block_til_ready blocking: ttys%d, count = %d\n",
2257                        info->line, info->count);
2258 #endif
2259                 schedule();
2260         }
2261         set_current_state(TASK_RUNNING);
2262         remove_wait_queue(&info->open_wait, &wait);
2263         if (!tty_hung_up_p(filp))
2264                 info->count++;
2265         info->blocked_open--;
2266 #ifdef SERIAL_DEBUG_OPEN
2267         printk("block_til_ready after blocking: ttys%d, count = %d\n",
2268                info->line, info->count);
2269 #endif
2270         if (retval)
2271                 return retval;
2272         info->flags |= ASYNC_NORMAL_ACTIVE;
2273         return 0;
2274 }       
2275
2276 /*
2277  * This routine is called whenever a serial port is opened.  It
2278  * enables interrupts for a serial port, linking in its async structure into
2279  * the IRQ chain.   It also performs the serial-specific
2280  * initialization for the tty structure.
2281  */
2282 static int esp_open(struct tty_struct *tty, struct file * filp)
2283 {
2284         struct esp_struct       *info;
2285         int                     retval, line;
2286
2287         line = tty->index;
2288         if ((line < 0) || (line >= NR_PORTS))
2289                 return -ENODEV;
2290
2291         /* find the port in the chain */
2292
2293         info = ports;
2294
2295         while (info && (info->line != line))
2296                 info = info->next_port;
2297
2298         if (!info) {
2299                 serial_paranoia_check(info, tty->name, "esp_open");
2300                 return -ENODEV;
2301         }
2302
2303 #ifdef SERIAL_DEBUG_OPEN
2304         printk("esp_open %s, count = %d\n", tty->name, info->count);
2305 #endif
2306         info->count++;
2307         tty->driver_data = info;
2308         info->tty = tty;
2309
2310         if (!tmp_buf) {
2311                 tmp_buf = (unsigned char *) get_zeroed_page(GFP_KERNEL);
2312                 if (!tmp_buf)
2313                         return -ENOMEM;
2314         }
2315         
2316         /*
2317          * Start up serial port
2318          */
2319         retval = startup(info);
2320         if (retval)
2321                 return retval;
2322
2323         retval = block_til_ready(tty, filp, info);
2324         if (retval) {
2325 #ifdef SERIAL_DEBUG_OPEN
2326                 printk("esp_open returning after block_til_ready with %d\n",
2327                        retval);
2328 #endif
2329                 return retval;
2330         }
2331
2332 #ifdef SERIAL_DEBUG_OPEN
2333         printk("esp_open %s successful...", tty->name);
2334 #endif
2335         return 0;
2336 }
2337
2338 /*
2339  * ---------------------------------------------------------------------
2340  * espserial_init() and friends
2341  *
2342  * espserial_init() is called at boot-time to initialize the serial driver.
2343  * ---------------------------------------------------------------------
2344  */
2345
2346 /*
2347  * This routine prints out the appropriate serial driver version
2348  * number, and identifies which options were configured into this
2349  * driver.
2350  */
2351  
2352 static _INLINE_ void show_serial_version(void)
2353 {
2354         printk(KERN_INFO "%s version %s (DMA %u)\n",
2355                 serial_name, serial_version, dma);
2356 }
2357
2358 /*
2359  * This routine is called by espserial_init() to initialize a specific serial
2360  * port.
2361  */
2362 static _INLINE_ int autoconfig(struct esp_struct * info, int *region_start)
2363 {
2364         int port_detected = 0;
2365         unsigned long flags;
2366
2367         save_flags(flags); cli();
2368         
2369         /*
2370          * Check for ESP card
2371          */
2372
2373         if (!check_region(info->port, 8) && 
2374             serial_in(info, UART_ESI_BASE) == 0xf3) {
2375                 serial_out(info, UART_ESI_CMD1, 0x00);
2376                 serial_out(info, UART_ESI_CMD1, 0x01);
2377
2378                 if ((serial_in(info, UART_ESI_STAT2) & 0x70) == 0x20) {
2379                         port_detected = 1;
2380
2381                         if (!(info->irq)) {
2382                                 serial_out(info, UART_ESI_CMD1, 0x02);
2383
2384                                 if (serial_in(info, UART_ESI_STAT1) & 0x01)
2385                                         info->irq = 3;
2386                                 else
2387                                         info->irq = 4;
2388                         }
2389
2390                         if (ports && (ports->port == (info->port - 8))) {
2391                                 release_region(*region_start,
2392                                                info->port - *region_start);
2393                         } else
2394                                 *region_start = info->port;
2395
2396                         if (!request_region(*region_start,
2397                                        info->port - *region_start + 8,
2398                                        "esp serial"))
2399                         {
2400                                 restore_flags(flags);
2401                                 return -EIO;
2402                         }
2403
2404                         /* put card in enhanced mode */
2405                         /* this prevents access through */
2406                         /* the "old" IO ports */
2407                         esp_basic_init(info);
2408
2409                         /* clear out MCR */
2410                         serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART);
2411                         serial_out(info, UART_ESI_CMD2, UART_MCR);
2412                         serial_out(info, UART_ESI_CMD2, 0x00);
2413                 }
2414         }
2415
2416         restore_flags(flags);
2417         return (port_detected);
2418 }
2419
2420 static struct tty_operations esp_ops = {
2421         .open = esp_open,
2422         .close = rs_close,
2423         .write = rs_write,
2424         .put_char = rs_put_char,
2425         .flush_chars = rs_flush_chars,
2426         .write_room = rs_write_room,
2427         .chars_in_buffer = rs_chars_in_buffer,
2428         .flush_buffer = rs_flush_buffer,
2429         .ioctl = rs_ioctl,
2430         .throttle = rs_throttle,
2431         .unthrottle = rs_unthrottle,
2432         .set_termios = rs_set_termios,
2433         .stop = rs_stop,
2434         .start = rs_start,
2435         .hangup = esp_hangup,
2436         .break_ctl = esp_break,
2437         .wait_until_sent = rs_wait_until_sent,
2438         .tiocmget = esp_tiocmget,
2439         .tiocmset = esp_tiocmset,
2440 };
2441
2442 /*
2443  * The serial driver boot-time initialization code!
2444  */
2445 int __init espserial_init(void)
2446 {
2447         int i, offset;
2448         int region_start;
2449         struct esp_struct * info;
2450         struct esp_struct *last_primary = 0;
2451         int esp[] = {0x100,0x140,0x180,0x200,0x240,0x280,0x300,0x380};
2452
2453         esp_driver = alloc_tty_driver(NR_PORTS);
2454         if (!esp_driver)
2455                 return -ENOMEM;
2456         
2457         for (i = 0; i < NR_PRIMARY; i++) {
2458                 if (irq[i] != 0) {
2459                         if ((irq[i] < 2) || (irq[i] > 15) || (irq[i] == 6) ||
2460                             (irq[i] == 8) || (irq[i] == 13))
2461                                 irq[i] = 0;
2462                         else if (irq[i] == 2)
2463                                 irq[i] = 9;
2464                 }
2465         }
2466
2467         if ((dma != 1) && (dma != 3))
2468                 dma = 0;
2469
2470         if ((rx_trigger < 1) || (rx_trigger > 1023))
2471                 rx_trigger = 768;
2472
2473         if ((tx_trigger < 1) || (tx_trigger > 1023))
2474                 tx_trigger = 768;
2475
2476         if ((flow_off < 1) || (flow_off > 1023))
2477                 flow_off = 1016;
2478         
2479         if ((flow_on < 1) || (flow_on > 1023))
2480                 flow_on = 944;
2481
2482         if ((rx_timeout < 0) || (rx_timeout > 255))
2483                 rx_timeout = 128;
2484         
2485         if (flow_on >= flow_off)
2486                 flow_on = flow_off - 1;
2487
2488         show_serial_version();
2489
2490         /* Initialize the tty_driver structure */
2491         
2492         esp_driver->owner = THIS_MODULE;
2493         esp_driver->name = "ttyP";
2494         esp_driver->devfs_name = "tts/P";
2495         esp_driver->major = ESP_IN_MAJOR;
2496         esp_driver->minor_start = 0;
2497         esp_driver->type = TTY_DRIVER_TYPE_SERIAL;
2498         esp_driver->subtype = SERIAL_TYPE_NORMAL;
2499         esp_driver->init_termios = tty_std_termios;
2500         esp_driver->init_termios.c_cflag =
2501                 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2502         esp_driver->flags = TTY_DRIVER_REAL_RAW;
2503         tty_set_operations(esp_driver, &esp_ops);
2504         if (tty_register_driver(esp_driver))
2505         {
2506                 printk(KERN_ERR "Couldn't register esp serial driver");
2507                 put_tty_driver(esp_driver);
2508                 return 1;
2509         }
2510
2511         info = kmalloc(sizeof(struct esp_struct), GFP_KERNEL);
2512
2513         if (!info)
2514         {
2515                 printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n");
2516                 tty_unregister_driver(esp_driver);
2517                 put_tty_driver(esp_driver);
2518                 return 1;
2519         }
2520
2521         memset((void *)info, 0, sizeof(struct esp_struct));
2522         /* rx_trigger, tx_trigger are needed by autoconfig */
2523         info->config.rx_trigger = rx_trigger;
2524         info->config.tx_trigger = tx_trigger;
2525
2526         i = 0;
2527         offset = 0;
2528
2529         do {
2530                 info->port = esp[i] + offset;
2531                 info->irq = irq[i];
2532                 info->line = (i * 8) + (offset / 8);
2533
2534                 if (!autoconfig(info, &region_start)) {
2535                         i++;
2536                         offset = 0;
2537                         continue;
2538                 }
2539
2540                 info->custom_divisor = (divisor[i] >> (offset / 2)) & 0xf;
2541                 info->flags = STD_COM_FLAGS;
2542                 if (info->custom_divisor)
2543                         info->flags |= ASYNC_SPD_CUST;
2544                 info->magic = ESP_MAGIC;
2545                 info->close_delay = 5*HZ/10;
2546                 info->closing_wait = 30*HZ;
2547                 INIT_WORK(&info->tqueue, do_softint, info);
2548                 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
2549                 info->config.rx_timeout = rx_timeout;
2550                 info->config.flow_on = flow_on;
2551                 info->config.flow_off = flow_off;
2552                 info->config.pio_threshold = pio_threshold;
2553                 info->next_port = ports;
2554                 init_waitqueue_head(&info->open_wait);
2555                 init_waitqueue_head(&info->close_wait);
2556                 init_waitqueue_head(&info->delta_msr_wait);
2557                 init_waitqueue_head(&info->break_wait);
2558                 ports = info;
2559                 printk(KERN_INFO "ttyP%d at 0x%04x (irq = %d) is an ESP ",
2560                         info->line, info->port, info->irq);
2561
2562                 if (info->line % 8) {
2563                         printk("secondary port\n");
2564                         /* 8 port cards can't do DMA */
2565                         info->stat_flags |= ESP_STAT_NEVER_DMA;
2566
2567                         if (last_primary)
2568                                 last_primary->stat_flags |= ESP_STAT_NEVER_DMA;
2569                 } else {
2570                         printk("primary port\n");
2571                         last_primary = info;
2572                         irq[i] = info->irq;
2573                 }
2574
2575                 if (!dma)
2576                         info->stat_flags |= ESP_STAT_NEVER_DMA;
2577
2578                 info = kmalloc(sizeof(struct esp_struct), GFP_KERNEL);
2579                 if (!info)
2580                 {
2581                         printk(KERN_ERR "Couldn't allocate memory for esp serial device information\n"); 
2582
2583                         /* allow use of the already detected ports */
2584                         return 0;
2585                 }
2586
2587                 memset((void *)info, 0, sizeof(struct esp_struct));
2588                 /* rx_trigger, tx_trigger are needed by autoconfig */
2589                 info->config.rx_trigger = rx_trigger;
2590                 info->config.tx_trigger = tx_trigger;
2591
2592                 if (offset == 56) {
2593                         i++;
2594                         offset = 0;
2595                 } else {
2596                         offset += 8;
2597                 }
2598         } while (i < NR_PRIMARY);
2599
2600         /* free the last port memory allocation */
2601         kfree(info);
2602
2603         return 0;
2604 }
2605
2606 static void __exit espserial_exit(void) 
2607 {
2608         unsigned long flags;
2609         int e1;
2610         unsigned int region_start, region_end;
2611         struct esp_struct *temp_async;
2612         struct esp_pio_buffer *pio_buf;
2613
2614         /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2615         save_flags(flags);
2616         cli();
2617         if ((e1 = tty_unregister_driver(esp_driver)))
2618                 printk("SERIAL: failed to unregister serial driver (%d)\n",
2619                        e1);
2620         restore_flags(flags);
2621         put_tty_driver(esp_driver);
2622
2623         while (ports) {
2624                 if (ports->port) {
2625                         region_start = region_end = ports->port;
2626                         temp_async = ports;
2627
2628                         while (temp_async) {
2629                                 if ((region_start - temp_async->port) == 8) {
2630                                         region_start = temp_async->port;
2631                                         temp_async->port = 0;
2632                                         temp_async = ports;
2633                                 } else if ((temp_async->port - region_end)
2634                                            == 8) {
2635                                         region_end = temp_async->port;
2636                                         temp_async->port = 0;
2637                                         temp_async = ports;
2638                                 } else
2639                                         temp_async = temp_async->next_port;
2640                         }
2641                         
2642                         release_region(region_start,
2643                                        region_end - region_start + 8);
2644                 }
2645
2646                 temp_async = ports->next_port;
2647                 kfree(ports);
2648                 ports = temp_async;
2649         }
2650
2651         if (dma_buffer)
2652                 free_pages((unsigned long)dma_buffer,
2653                         get_order(DMA_BUFFER_SZ));
2654
2655         if (tmp_buf)
2656                 free_page((unsigned long)tmp_buf);
2657
2658         while (free_pio_buf) {
2659                 pio_buf = free_pio_buf->next;
2660                 kfree(free_pio_buf);
2661                 free_pio_buf = pio_buf;
2662         }
2663 }
2664
2665 module_init(espserial_init);
2666 module_exit(espserial_exit);