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