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