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