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