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