ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / char / mxser.c
1 /*****************************************************************************/
2 /*
3  *          mxser.c  -- MOXA Smartio family multiport serial driver.
4  *
5  *      Copyright (C) 1999-2000  Moxa Technologies (support@moxa.com.tw).
6  *
7  *      This code is loosely based on the Linux serial driver, written by
8  *      Linus Torvalds, Theodore T'so and others.
9  *
10  *      This program is free software; you can redistribute it and/or modify
11  *      it under the terms of the GNU General Public License as published by
12  *      the Free Software Foundation; either version 2 of the License, or
13  *      (at your option) any later version.
14  *
15  *      This program is distributed in the hope that it will be useful,
16  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *      GNU General Public License for more details.
19  *
20  *      You should have received a copy of the GNU General Public License
21  *      along with this program; if not, write to the Free Software
22  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 /*
26  *    MOXA Smartio Family Serial Driver
27  *
28  *      Copyright (C) 1999,2000  Moxa Technologies Co., LTD.
29  *
30  *      for             : LINUX 2.0.X, 2.2.X, 2.4.X
31  *      date            : 2001/05/01
32  *      version         : 1.2 
33  *      
34  *    Fixes for C104H/PCI by Tim Hockin <thockin@sun.com>
35  *    Added support for: C102, CI-132, CI-134, CP-132, CP-114, CT-114 cards
36  *                        by Damian Wrobel <dwrobel@ertel.com.pl>
37  *
38  */
39
40 #include <linux/config.h>
41 #include <linux/module.h>
42 #include <linux/errno.h>
43 #include <linux/signal.h>
44 #include <linux/sched.h>
45 #include <linux/timer.h>
46 #include <linux/interrupt.h>
47 #include <linux/tty.h>
48 #include <linux/tty_flip.h>
49 #include <linux/serial.h>
50 #include <linux/serial_reg.h>
51 #include <linux/major.h>
52 #include <linux/string.h>
53 #include <linux/fcntl.h>
54 #include <linux/ptrace.h>
55 #include <linux/ioport.h>
56 #include <linux/mm.h>
57 #include <linux/smp_lock.h>
58 #include <linux/pci.h>
59 #include <linux/init.h>
60
61 #include <asm/system.h>
62 #include <asm/io.h>
63 #include <asm/irq.h>
64 #include <asm/bitops.h>
65 #include <asm/uaccess.h>
66
67 #define         MXSER_VERSION                   "1.2.1"
68
69 #define         MXSERMAJOR              174
70 #define         MXSERCUMAJOR            175
71
72
73 #define MXSER_EVENT_TXLOW        1
74 #define MXSER_EVENT_HANGUP       2
75
76
77 #define         SERIAL_DO_RESTART
78
79 #define         MXSER_BOARDS            4       /* Max. boards */
80 #define         MXSER_PORTS             32      /* Max. ports */
81 #define         MXSER_PORTS_PER_BOARD   8       /* Max. ports per board */
82 #define         MXSER_ISR_PASS_LIMIT    256
83
84 #define         MXSER_ERR_IOADDR        -1
85 #define         MXSER_ERR_IRQ           -2
86 #define         MXSER_ERR_IRQ_CONFLIT   -3
87 #define         MXSER_ERR_VECTOR        -4
88
89 #define         SERIAL_TYPE_NORMAL      1
90
91 #define         WAKEUP_CHARS            256
92
93 #define         UART_MCR_AFE            0x20
94 #define         UART_LSR_SPECIAL        0x1E
95
96 #define PORTNO(x)               ((x)->index)
97
98 #define RELEVANT_IFLAG(iflag)   (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
99
100 #define IRQ_T(info) ((info->flags & ASYNC_SHARE_IRQ) ? SA_SHIRQ : SA_INTERRUPT)
101
102 #ifndef MIN
103 #define MIN(a,b)        ((a) < (b) ? (a) : (b))
104 #endif
105
106 /*
107  *    Define the Moxa PCI vendor and device IDs.
108  */
109
110 #ifndef PCI_VENDOR_ID_MOXA
111 #define PCI_VENDOR_ID_MOXA      0x1393
112 #endif
113 #ifndef PCI_DEVICE_ID_C168
114 #define PCI_DEVICE_ID_C168      0x1680
115 #endif
116 #ifndef PCI_DEVICE_ID_C104
117 #define PCI_DEVICE_ID_C104      0x1040
118 #endif
119 #ifndef PCI_DEVICE_ID_CP132
120 #define PCI_DEVICE_ID_CP132     0x1320
121 #endif
122 #ifndef PCI_DEVICE_ID_CP114
123 #define PCI_DEVICE_ID_CP114     0x1141
124 #endif
125 #ifndef PCI_DEVICE_ID_CT114
126 #define PCI_DEVICE_ID_CT114     0x1140
127 #endif
128
129 #define C168_ASIC_ID    1
130 #define C104_ASIC_ID    2
131 #define CI134_ASIC_ID   3
132 #define CI132_ASIC_ID   4
133 #define CI104J_ASIC_ID  5
134 #define C102_ASIC_ID    0xB
135
136 enum {
137         MXSER_BOARD_C168_ISA = 0,
138         MXSER_BOARD_C104_ISA,
139         MXSER_BOARD_CI104J,
140         MXSER_BOARD_C168_PCI,
141         MXSER_BOARD_C104_PCI,
142         MXSER_BOARD_C102_ISA,
143         MXSER_BOARD_CI132,
144         MXSER_BOARD_CI134,
145         MXSER_BOARD_CP132_PCI,
146         MXSER_BOARD_CP114_PCI,
147         MXSER_BOARD_CT114_PCI
148 };
149
150 static char *mxser_brdname[] =
151 {
152         "C168 series",
153         "C104 series",
154         "CI-104J series",
155         "C168H/PCI series",
156         "C104H/PCI series",
157         "C102 series",
158         "CI-132 series",
159         "CI-134 series",
160         "CP-132 series",
161         "CP-114 series",
162         "CT-114 series"
163 };
164
165 static int mxser_numports[] =
166 {
167         8,
168         4,
169         4,
170         8,
171         4,
172         2,
173         2,
174         4,
175         2,
176         4,
177         4
178 };
179
180 /*
181  *    MOXA ioctls
182  */
183 #define         MOXA            0x400
184 #define         MOXA_GETDATACOUNT     (MOXA + 23)
185 #define         MOXA_GET_CONF         (MOXA + 35)
186 #define         MOXA_DIAGNOSE         (MOXA + 50)
187 #define         MOXA_CHKPORTENABLE    (MOXA + 60)
188 #define         MOXA_HighSpeedOn      (MOXA + 61)
189 #define         MOXA_GET_MAJOR        (MOXA + 63)
190 #define         MOXA_GET_CUMAJOR      (MOXA + 64)
191 #define         MOXA_GETMSTATUS       (MOXA + 65)
192
193 static struct pci_device_id mxser_pcibrds[] = {
194         { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C168, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 
195           MXSER_BOARD_C168_PCI },
196         { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C104, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 
197           MXSER_BOARD_C104_PCI },
198         { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP132, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
199           MXSER_BOARD_CP132_PCI },
200         { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP114, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
201           MXSER_BOARD_CP114_PCI },
202         { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CT114, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
203           MXSER_BOARD_CT114_PCI },
204         { 0 }
205 };
206 MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
207
208 static int ioaddr[MXSER_BOARDS];
209 static int ttymajor = MXSERMAJOR;
210 static int verbose;
211
212 /* Variables for insmod */
213
214 MODULE_AUTHOR("William Chen");
215 MODULE_DESCRIPTION("MOXA Smartio Family Multiport Board Device Driver");
216 MODULE_LICENSE("GPL");
217 MODULE_PARM(ioaddr, "1-4i");
218 MODULE_PARM(ttymajor, "i");
219 MODULE_PARM(verbose, "i");
220
221 struct mxser_hwconf {
222         int board_type;
223         int ports;
224         int irq;
225         int vector;
226         int vector_mask;
227         int uart_type;
228         int ioaddr[MXSER_PORTS_PER_BOARD];
229         int baud_base[MXSER_PORTS_PER_BOARD];
230         struct pci_dev *pdev;
231 };
232
233 struct mxser_struct {
234         int port;
235         int base;               /* port base address */
236         int irq;                /* port using irq no. */
237         int vector;             /* port irq vector */
238         int vectormask;         /* port vector mask */
239         int rx_trigger;         /* Rx fifo trigger level */
240         int baud_base;          /* max. speed */
241         int flags;              /* defined in tty.h */
242         int type;               /* UART type */
243         struct tty_struct *tty;
244         int read_status_mask;
245         int ignore_status_mask;
246         int xmit_fifo_size;
247         int custom_divisor;
248         int x_char;             /* xon/xoff character */
249         int close_delay;
250         unsigned short closing_wait;
251         int IER;                /* Interrupt Enable Register */
252         int MCR;                /* Modem control register */
253         unsigned long event;
254         int count;              /* # of fd on device */
255         int blocked_open;       /* # of blocked opens */
256         unsigned char *xmit_buf;
257         int xmit_head;
258         int xmit_tail;
259         int xmit_cnt;
260         struct work_struct tqueue;
261         int cflag;
262         wait_queue_head_t open_wait;
263         wait_queue_head_t close_wait;
264         wait_queue_head_t delta_msr_wait;
265         struct async_icount icount;     /* kernel counters for the 4 input interrupts */
266 };
267
268 struct mxser_log {
269         int tick;
270         int rxcnt[MXSER_PORTS];
271         int txcnt[MXSER_PORTS];
272 };
273
274 struct mxser_mstatus {
275         tcflag_t cflag;
276         int cts;
277         int dsr;
278         int ri;
279         int dcd;
280 };
281
282 static struct mxser_mstatus GMStatus[MXSER_PORTS];
283
284 static int mxserBoardCAP[MXSER_BOARDS] =
285 {
286         0, 0, 0, 0
287        /*  0x180, 0x280, 0x200, 0x320   */
288 };
289
290
291 static struct tty_driver *mxvar_sdriver;
292 static struct mxser_struct mxvar_table[MXSER_PORTS];
293 static struct mxser_log mxvar_log;
294 static int mxvar_diagflag;
295 /*
296  * mxvar_tmp_buf is used as a temporary buffer by serial_write. We need
297  * to lock it in case the memcpy_fromfs blocks while swapping in a page,
298  * and some other program tries to do a serial write at the same time.
299  * Since the lock will only come under contention when the system is
300  * swapping and available memory is low, it makes sense to share one
301  * buffer across all the serial ports, since it significantly saves
302  * memory if large numbers of serial ports are open.
303  */
304 static unsigned char *mxvar_tmp_buf;
305 static struct semaphore mxvar_tmp_buf_sem;
306
307 /*
308  * This is used to figure out the divisor speeds and the timeouts
309  */
310 static int mxvar_baud_table[] =
311 {
312         0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
313         9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600, 0};
314
315 struct mxser_hwconf mxsercfg[MXSER_BOARDS];
316
317 /*
318  * static functions:
319  */
320
321 static void mxser_getcfg(int board, struct mxser_hwconf *hwconf);
322 static int mxser_get_ISA_conf(int, struct mxser_hwconf *);
323 static int mxser_get_PCI_conf(struct pci_dev *, int, struct mxser_hwconf *);
324 static void mxser_do_softint(void *);
325 static int mxser_open(struct tty_struct *, struct file *);
326 static void mxser_close(struct tty_struct *, struct file *);
327 static int mxser_write(struct tty_struct *, int, const unsigned char *, int);
328 static int mxser_write_room(struct tty_struct *);
329 static void mxser_flush_buffer(struct tty_struct *);
330 static int mxser_chars_in_buffer(struct tty_struct *);
331 static void mxser_flush_chars(struct tty_struct *);
332 static void mxser_put_char(struct tty_struct *, unsigned char);
333 static int mxser_ioctl(struct tty_struct *, struct file *, uint, ulong);
334 static int mxser_ioctl_special(unsigned int, unsigned long);
335 static void mxser_throttle(struct tty_struct *);
336 static void mxser_unthrottle(struct tty_struct *);
337 static void mxser_set_termios(struct tty_struct *, struct termios *);
338 static void mxser_stop(struct tty_struct *);
339 static void mxser_start(struct tty_struct *);
340 static void mxser_hangup(struct tty_struct *);
341 static irqreturn_t mxser_interrupt(int, void *, struct pt_regs *);
342 static inline void mxser_receive_chars(struct mxser_struct *, int *);
343 static inline void mxser_transmit_chars(struct mxser_struct *);
344 static inline void mxser_check_modem_status(struct mxser_struct *, int);
345 static int mxser_block_til_ready(struct tty_struct *, struct file *, struct mxser_struct *);
346 static int mxser_startup(struct mxser_struct *);
347 static void mxser_shutdown(struct mxser_struct *);
348 static int mxser_change_speed(struct mxser_struct *, struct termios *old_termios);
349 static int mxser_get_serial_info(struct mxser_struct *, struct serial_struct *);
350 static int mxser_set_serial_info(struct mxser_struct *, struct serial_struct *);
351 static int mxser_get_lsr_info(struct mxser_struct *, unsigned int *);
352 static void mxser_send_break(struct mxser_struct *, int);
353 static int mxser_tiocmget(struct tty_struct *, struct file *);
354 static int mxser_tiocmset(struct tty_struct *, struct file *, unsigned int, unsigned int);
355
356 /*
357  * The MOXA C168/C104 serial driver boot-time initialization code!
358  */
359
360 static void __exit mxser_module_exit(void)
361 {
362         int i, err = 0;
363
364
365         if (verbose)
366                 printk("Unloading module mxser ...\n");
367         if ((err |= tty_unregister_driver(mxvar_sdriver)))
368                 printk("Couldn't unregister MOXA Smartio family serial driver\n");
369         put_tty_driver(mxvar_sdriver);
370
371         for (i = 0; i < MXSER_BOARDS; i++) {
372                 if (mxsercfg[i].board_type == -1)
373                         continue;
374                 else {
375                         free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
376                 }
377         }
378
379         if (verbose)
380                 printk("Done.\n");
381
382 }
383
384 int mxser_initbrd(int board, struct mxser_hwconf *hwconf)
385 {
386         struct mxser_struct *info;
387         unsigned long flags;
388         int retval;
389         int i, n;
390
391         init_MUTEX(&mxvar_tmp_buf_sem);
392         
393         n = board * MXSER_PORTS_PER_BOARD;
394         info = &mxvar_table[n];
395         for (i = 0; i < hwconf->ports; i++, n++, info++) {
396                 if (verbose) {
397                         printk("        ttyM%d/cum%d at 0x%04x ", n, n, hwconf->ioaddr[i]);
398                         if (hwconf->baud_base[i] == 115200)
399                                 printk(" max. baud rate up to 115200 bps.\n");
400                         else
401                                 printk(" max. baud rate up to 921600 bps.\n");
402                 }
403                 info->port = n;
404                 info->base = hwconf->ioaddr[i];
405                 info->irq = hwconf->irq;
406                 info->vector = hwconf->vector;
407                 info->vectormask = hwconf->vector_mask;
408                 info->rx_trigger = 14;
409                 info->baud_base = hwconf->baud_base[i];
410                 info->flags = ASYNC_SHARE_IRQ;
411                 info->type = hwconf->uart_type;
412                 if ((info->type == PORT_16450) || (info->type == PORT_8250))
413                         info->xmit_fifo_size = 1;
414                 else
415                         info->xmit_fifo_size = 16;
416                 info->custom_divisor = hwconf->baud_base[i] * 16;
417                 info->close_delay = 5 * HZ / 10;
418                 info->closing_wait = 30 * HZ;
419                 INIT_WORK(&info->tqueue, mxser_do_softint, info);
420                 info->cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
421                 init_waitqueue_head(&info->open_wait);
422                 init_waitqueue_head(&info->close_wait);
423                 init_waitqueue_head(&info->delta_msr_wait);
424         }
425
426         /*
427          * Allocate the IRQ if necessary
428          */
429         save_flags(flags);
430
431         n = board * MXSER_PORTS_PER_BOARD;
432         info = &mxvar_table[n];
433
434         cli();
435         retval = request_irq(hwconf->irq, mxser_interrupt, IRQ_T(info),
436                              "mxser", info);
437         if (retval) {
438                 restore_flags(flags);
439                 printk("Board %d: %s", board, mxser_brdname[hwconf->board_type]);
440                 printk("  Request irq fail,IRQ (%d) may be conflit with another device.\n", info->irq);
441                 return (retval);
442         }
443         restore_flags(flags);
444
445         return 0;
446 }
447
448
449 static void mxser_getcfg(int board, struct mxser_hwconf *hwconf)
450 {
451         mxsercfg[board] = *hwconf;
452 }
453
454 static int mxser_get_PCI_conf(struct pci_dev *pdev, int board_type, struct mxser_hwconf *hwconf)
455 {
456         int i;
457         unsigned int ioaddress;
458
459         hwconf->board_type = board_type;
460         hwconf->ports = mxser_numports[board_type];
461         ioaddress = pci_resource_start (pdev, 2);
462         for (i = 0; i < hwconf->ports; i++)
463                 hwconf->ioaddr[i] = ioaddress + 8 * i;
464
465         ioaddress = pci_resource_start (pdev, 3);
466         hwconf->vector = ioaddress;
467
468         hwconf->irq = pdev->irq;
469
470         hwconf->uart_type = PORT_16550A;
471         hwconf->vector_mask = 0;
472         for (i = 0; i < hwconf->ports; i++) {
473                 hwconf->vector_mask |= (1 << i);
474                 hwconf->baud_base[i] = 921600;
475         }
476         return (0);
477 }
478
479 static struct tty_operations mxser_ops = {
480         .open = mxser_open,
481         .close = mxser_close,
482         .write = mxser_write,
483         .put_char = mxser_put_char,
484         .flush_chars = mxser_flush_chars,
485         .write_room = mxser_write_room,
486         .chars_in_buffer = mxser_chars_in_buffer,
487         .flush_buffer = mxser_flush_buffer,
488         .ioctl = mxser_ioctl,
489         .throttle = mxser_throttle,
490         .unthrottle = mxser_unthrottle,
491         .set_termios = mxser_set_termios,
492         .stop = mxser_stop,
493         .start = mxser_start,
494         .hangup = mxser_hangup,
495         .tiocmget = mxser_tiocmget,
496         .tiocmset = mxser_tiocmset,
497 };
498
499 static int __init mxser_module_init(void)
500 {
501         int i, m, retval, b;
502         struct mxser_hwconf hwconf;
503
504         mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
505         if (!mxvar_sdriver)
506                 return -ENOMEM;
507
508         printk("MOXA Smartio family driver version %s\n", MXSER_VERSION);
509
510         /* Initialize the tty_driver structure */
511
512         mxvar_sdriver->owner = THIS_MODULE;
513         mxvar_sdriver->name = "ttyM";
514         mxvar_sdriver->devfs_name = "tts/M";
515         mxvar_sdriver->major = ttymajor;
516         mxvar_sdriver->minor_start = 0;
517         mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
518         mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
519         mxvar_sdriver->init_termios = tty_std_termios;
520         mxvar_sdriver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
521         mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
522         tty_set_operations(mxvar_sdriver, &mxser_ops);
523         printk("Tty devices major number = %d\n", ttymajor);
524
525         mxvar_diagflag = 0;
526         memset(mxvar_table, 0, MXSER_PORTS * sizeof(struct mxser_struct));
527         memset(&mxvar_log, 0, sizeof(struct mxser_log));
528
529
530         m = 0;
531         /* Start finding ISA boards here */
532         for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
533                 int cap;
534                 if (!(cap = mxserBoardCAP[b]))
535                         continue;
536
537                 retval = mxser_get_ISA_conf(cap, &hwconf);
538
539                 if (retval != 0)
540                         printk("Found MOXA %s board (CAP=0x%x)\n",
541                                mxser_brdname[hwconf.board_type],
542                                ioaddr[b]);
543
544                 if (retval <= 0) {
545                         if (retval == MXSER_ERR_IRQ)
546                                 printk("Invalid interrupt number,board not configured\n");
547                         else if (retval == MXSER_ERR_IRQ_CONFLIT)
548                                 printk("Invalid interrupt number,board not configured\n");
549                         else if (retval == MXSER_ERR_VECTOR)
550                                 printk("Invalid interrupt vector,board not configured\n");
551                         else if (retval == MXSER_ERR_IOADDR)
552                                 printk("Invalid I/O address,board not configured\n");
553
554                         continue;
555                 }
556                 hwconf.pdev = NULL;
557
558                 if (mxser_initbrd(m, &hwconf) < 0)
559                         continue;
560
561                 mxser_getcfg(m, &hwconf);
562
563                 m++;
564         }
565
566         /* Start finding ISA boards from module arg */
567         for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
568                 int cap;
569                 if (!(cap = ioaddr[b]))
570                         continue;
571
572                 retval = mxser_get_ISA_conf(cap, &hwconf);
573
574                 if (retval != 0)
575                         printk("Found MOXA %s board (CAP=0x%x)\n",
576                                mxser_brdname[hwconf.board_type],
577                                ioaddr[b]);
578
579                 if (retval <= 0) {
580                         if (retval == MXSER_ERR_IRQ)
581                                 printk("Invalid interrupt number,board not configured\n");
582                         else if (retval == MXSER_ERR_IRQ_CONFLIT)
583                                 printk("Invalid interrupt number,board not configured\n");
584                         else if (retval == MXSER_ERR_VECTOR)
585                                 printk("Invalid interrupt vector,board not configured\n");
586                         else if (retval == MXSER_ERR_IOADDR)
587                                 printk("Invalid I/O address,board not configured\n");
588
589                         continue;
590                 }
591                 hwconf.pdev = NULL;
592
593                 if (mxser_initbrd(m, &hwconf) < 0)
594                         continue;
595
596                 mxser_getcfg(m, &hwconf);
597
598                 m++;
599         }
600
601         /* start finding PCI board here */
602
603 #ifdef CONFIG_PCI
604         {
605                 struct pci_dev *pdev = NULL;
606                 int n = (sizeof(mxser_pcibrds) / sizeof(mxser_pcibrds[0])) - 1;
607                 int index = 0;
608                 for (b = 0; b < n; b++) {
609                         while ((pdev = pci_find_device(mxser_pcibrds[b].vendor, mxser_pcibrds[b].device, pdev)))
610                         {
611                                 if (pci_enable_device(pdev))
612                                         continue;
613                                 hwconf.pdev = pdev;
614                                 printk("Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
615                                         mxser_brdname[mxser_pcibrds[b].driver_data],
616                                 pdev->bus->number, PCI_SLOT(pdev->devfn));
617                                 if (m >= MXSER_BOARDS) {
618                                         printk("Too many Smartio family boards found (maximum %d),board not configured\n", MXSER_BOARDS);
619                                 } else {
620                                         retval = mxser_get_PCI_conf(pdev, mxser_pcibrds[b].driver_data, &hwconf);
621                                         if (retval < 0) {
622                                                 if (retval == MXSER_ERR_IRQ)
623                                                         printk("Invalid interrupt number,board not configured\n");
624                                                 else if (retval == MXSER_ERR_IRQ_CONFLIT)
625                                                         printk("Invalid interrupt number,board not configured\n");      
626                                                 else if (retval == MXSER_ERR_VECTOR)
627                                                         printk("Invalid interrupt vector,board not configured\n");
628                                                 else if (retval == MXSER_ERR_IOADDR)
629                                                         printk("Invalid I/O address,board not configured\n");
630                                                 continue;
631                                         }
632                                         if (mxser_initbrd(m, &hwconf) < 0)
633                                                 continue;
634                                         mxser_getcfg(m, &hwconf);
635                                         m++;
636                                 }
637                         }
638                 }
639         }
640 #endif
641
642         for (i = m; i < MXSER_BOARDS; i++) {
643                 mxsercfg[i].board_type = -1;
644         }
645
646
647         if (!tty_register_driver(mxvar_sdriver))
648                 return 0;
649
650         put_tty_driver(mxvar_sdriver);
651         printk("Couldn't install MOXA Smartio family driver !\n");
652
653         for (i = 0; i < MXSER_BOARDS; i++) {
654                 if (mxsercfg[i].board_type == -1)
655                         continue;
656                 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
657         }
658         return -1;
659 }
660
661 static void mxser_do_softint(void *private_)
662 {
663         struct mxser_struct *info = (struct mxser_struct *) private_;
664         struct tty_struct *tty;
665
666         tty = info->tty;
667         if (tty) {
668                 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event)) {
669                         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
670                             tty->ldisc.write_wakeup)
671                                 (tty->ldisc.write_wakeup) (tty);
672                         wake_up_interruptible(&tty->write_wait);
673                 }
674                 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event)) {
675                         tty_hangup(tty);        /* FIXME: module removal race here - AKPM */
676                 }
677         }
678 }
679
680 /*
681  * This routine is called whenever a serial port is opened.  It
682  * enables interrupts for a serial port, linking in its async structure into
683  * the IRQ chain.   It also performs the serial-specific
684  * initialization for the tty structure.
685  */
686
687 static int mxser_open(struct tty_struct *tty, struct file *filp)
688 {
689         struct mxser_struct *info;
690         int retval, line;
691         unsigned long page;
692
693         line = PORTNO(tty);
694         if (line == MXSER_PORTS)
695                 return (0);
696         if ((line < 0) || (line > MXSER_PORTS))
697                 return (-ENODEV);
698         info = mxvar_table + line;
699         if (!info->base)
700                 return (-ENODEV);
701
702         info->count++;
703         tty->driver_data = info;
704         info->tty = tty;
705
706         if (!mxvar_tmp_buf) {
707                 page = get_zeroed_page(GFP_KERNEL);
708                 if (!page)
709                         return (-ENOMEM);
710                 if (mxvar_tmp_buf)
711                         free_page(page);
712                 else
713                         mxvar_tmp_buf = (unsigned char *) page;
714         }
715         /*
716          * Start up serial port
717          */
718         retval = mxser_startup(info);
719         if (retval)
720                 return (retval);
721
722         return mxser_block_til_ready(tty, filp, info);
723 }
724
725 /*
726  * This routine is called when the serial port gets closed.  First, we
727  * wait for the last remaining data to be sent.  Then, we unlink its
728  * async structure from the interrupt chain if necessary, and we free
729  * that IRQ if nothing is left in the chain.
730  */
731
732 static void mxser_close(struct tty_struct *tty, struct file *filp)
733 {
734         struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
735         unsigned long flags;
736         unsigned long timeout;
737
738         if (PORTNO(tty) == MXSER_PORTS)
739                 return;
740         if (!info)
741                 return;
742
743         save_flags(flags);
744         cli();
745
746         if (tty_hung_up_p(filp)) {
747                 restore_flags(flags);
748                 return;
749         }
750         if ((tty->count == 1) && (info->count != 1)) {
751                 /*
752                  * Uh, oh.        tty->count is 1, which means that the tty
753                  * structure will be freed.  Info->count should always
754                  * be one in these conditions.  If it's greater than
755                  * one, we've got real problems, since it means the
756                  * serial port won't be shutdown.
757                  */
758                 printk("mxser_close: bad serial port count; tty->count is 1, "
759                        "info->count is %d\n", info->count);
760                 info->count = 1;
761         }
762         if (--info->count < 0) {
763                 printk("mxser_close: bad serial port count for ttys%d: %d\n",
764                        info->port, info->count);
765                 info->count = 0;
766         }
767         if (info->count) {
768                 restore_flags(flags);
769                 return;
770         }
771         info->flags |= ASYNC_CLOSING;
772         info->cflag = tty->termios->c_cflag;
773         /*
774          * Now we wait for the transmit buffer to clear; and we notify
775          * the line discipline to only process XON/XOFF characters.
776          */
777         tty->closing = 1;
778         if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
779                 tty_wait_until_sent(tty, info->closing_wait);
780         /*
781          * At this point we stop accepting input.  To do this, we
782          * disable the receive line status interrupts, and tell the
783          * interrupt driver to stop checking the data ready bit in the
784          * line status register.
785          */
786         info->IER &= ~UART_IER_RLSI;
787         /* by William
788            info->read_status_mask &= ~UART_LSR_DR;
789          */
790         if (info->flags & ASYNC_INITIALIZED) {
791                 outb(info->IER, info->base + UART_IER);
792                 /*
793                  * Before we drop DTR, make sure the UART transmitter
794                  * has completely drained; this is especially
795                  * important if there is a transmit FIFO!
796                  */
797                 timeout = jiffies + HZ;
798                 while (!(inb(info->base + UART_LSR) & UART_LSR_TEMT)) {
799                         set_current_state(TASK_INTERRUPTIBLE);
800                         schedule_timeout(5);
801                         if (time_after(jiffies, timeout))
802                                 break;
803                 }
804         }
805         mxser_shutdown(info);
806         if (tty->driver->flush_buffer)
807                 tty->driver->flush_buffer(tty);
808         if (tty->ldisc.flush_buffer)
809                 tty->ldisc.flush_buffer(tty);
810         tty->closing = 0;
811         info->event = 0;
812         info->tty = 0;
813         if (info->blocked_open) {
814                 if (info->close_delay) {
815                         set_current_state(TASK_INTERRUPTIBLE);
816                         schedule_timeout(info->close_delay);
817                 }
818                 wake_up_interruptible(&info->open_wait);
819         }
820         info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
821         wake_up_interruptible(&info->close_wait);
822         restore_flags(flags);
823
824 }
825
826 static int mxser_write(struct tty_struct *tty, int from_user,
827                        const unsigned char *buf, int count)
828 {
829         int c, total = 0;
830         struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
831         unsigned long flags;
832
833         if (!tty || !info->xmit_buf || !mxvar_tmp_buf)
834                 return (0);
835
836         save_flags(flags);
837         if (from_user) {
838                 down(&mxvar_tmp_buf_sem);
839                 while (1) {
840                         c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
841                                            SERIAL_XMIT_SIZE - info->xmit_head));
842                         if (c <= 0)
843                                 break;
844
845                         c -= copy_from_user(mxvar_tmp_buf, buf, c);
846                         if (!c) {
847                                 if (!total)
848                                         total = -EFAULT;
849                                 break;
850                         }
851
852                         cli();
853                         c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
854                                        SERIAL_XMIT_SIZE - info->xmit_head));
855                         memcpy(info->xmit_buf + info->xmit_head, mxvar_tmp_buf, c);
856                         info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE - 1);
857                         info->xmit_cnt += c;
858                         restore_flags(flags);
859
860                         buf += c;
861                         count -= c;
862                         total += c;
863                 }
864                 up(&mxvar_tmp_buf_sem);
865         } else {
866                 while (1) {
867                         cli();
868                         c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
869                                            SERIAL_XMIT_SIZE - info->xmit_head));
870                         if (c <= 0) {
871                                 restore_flags(flags);
872                                 break;
873                         }
874
875                         memcpy(info->xmit_buf + info->xmit_head, buf, c);
876                         info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE - 1);
877                         info->xmit_cnt += c;
878                         restore_flags(flags);
879
880                         buf += c;
881                         count -= c;
882                         total += c;
883                 }
884         }
885
886         cli();
887         if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
888             !(info->IER & UART_IER_THRI)) {
889                 info->IER |= UART_IER_THRI;
890                 outb(info->IER, info->base + UART_IER);
891         }
892         restore_flags(flags);
893         return (total);
894 }
895
896 static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
897 {
898         struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
899         unsigned long flags;
900
901         if (!tty || !info->xmit_buf)
902                 return;
903
904         save_flags(flags);
905         cli();
906         if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
907                 restore_flags(flags);
908                 return;
909         }
910         info->xmit_buf[info->xmit_head++] = ch;
911         info->xmit_head &= SERIAL_XMIT_SIZE - 1;
912         info->xmit_cnt++;
913         /********************************************** why ??? ***********
914         if ( !tty->stopped && !tty->hw_stopped &&
915              !(info->IER & UART_IER_THRI) ) {
916             info->IER |= UART_IER_THRI;
917             outb(info->IER, info->base + UART_IER);
918         }
919         *****************************************************************/
920         restore_flags(flags);
921 }
922
923 static void mxser_flush_chars(struct tty_struct *tty)
924 {
925         struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
926         unsigned long flags;
927
928         if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
929             !info->xmit_buf)
930                 return;
931
932         save_flags(flags);
933         cli();
934         info->IER |= UART_IER_THRI;
935         outb(info->IER, info->base + UART_IER);
936         restore_flags(flags);
937 }
938
939 static int mxser_write_room(struct tty_struct *tty)
940 {
941         struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
942         int ret;
943
944         ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
945         if (ret < 0)
946                 ret = 0;
947         return (ret);
948 }
949
950 static int mxser_chars_in_buffer(struct tty_struct *tty)
951 {
952         struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
953
954         return (info->xmit_cnt);
955 }
956
957 static void mxser_flush_buffer(struct tty_struct *tty)
958 {
959         struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
960         unsigned long flags;
961
962         save_flags(flags);
963         cli();
964         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
965         restore_flags(flags);
966         wake_up_interruptible(&tty->write_wait);
967         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
968             tty->ldisc.write_wakeup)
969                 (tty->ldisc.write_wakeup) (tty);
970 }
971
972 static int mxser_ioctl(struct tty_struct *tty, struct file *file,
973                        unsigned int cmd, unsigned long arg)
974 {
975         unsigned long flags;
976         struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
977         int retval;
978         struct async_icount cprev, cnow;        /* kernel counter temps */
979         struct serial_icounter_struct *p_cuser;         /* user space */
980         unsigned long templ;
981
982         if (PORTNO(tty) == MXSER_PORTS)
983                 return (mxser_ioctl_special(cmd, arg));
984         if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) &&
985             (cmd != TIOCGICOUNT)) {
986                 if (tty->flags & (1 << TTY_IO_ERROR))
987                         return (-EIO);
988         }
989         switch (cmd) {
990         case TCSBRK:            /* SVID version: non-zero arg --> no break */
991                 retval = tty_check_change(tty);
992                 if (retval)
993                         return (retval);
994                 tty_wait_until_sent(tty, 0);
995                 if (!arg)
996                         mxser_send_break(info, HZ / 4);         /* 1/4 second */
997                 return (0);
998         case TCSBRKP:           /* support for POSIX tcsendbreak() */
999                 retval = tty_check_change(tty);
1000                 if (retval)
1001                         return (retval);
1002                 tty_wait_until_sent(tty, 0);
1003                 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1004                 return (0);
1005         case TIOCGSOFTCAR:
1006                 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long *) arg);
1007         case TIOCSSOFTCAR:
1008                 if(get_user(templ, (unsigned long *) arg))
1009                         return -EFAULT;
1010                 arg = templ;
1011                 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
1012                                          (arg ? CLOCAL : 0));
1013                 return (0);
1014         case TIOCGSERIAL:
1015                 return (mxser_get_serial_info(info, (struct serial_struct *) arg));
1016         case TIOCSSERIAL:
1017                 return (mxser_set_serial_info(info, (struct serial_struct *) arg));
1018         case TIOCSERGETLSR:     /* Get line status register */
1019                 return (mxser_get_lsr_info(info, (unsigned int *) arg));
1020                 /*
1021                  * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1022                  * - mask passed in arg for lines of interest
1023                  *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1024                  * Caller should use TIOCGICOUNT to see which one it was
1025                  */
1026         case TIOCMIWAIT:
1027                 save_flags(flags);
1028                 cli();
1029                 cprev = info->icount;   /* note the counters on entry */
1030                 restore_flags(flags);
1031                 while (1) {
1032                         interruptible_sleep_on(&info->delta_msr_wait);
1033                         /* see if a signal did it */
1034                         if (signal_pending(current))
1035                                 return (-ERESTARTSYS);
1036                         save_flags(flags);
1037                         cli();
1038                         cnow = info->icount;    /* atomic copy */
1039                         restore_flags(flags);
1040                         if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1041                           cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1042                                 return (-EIO);  /* no change => error */
1043                         if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1044                         ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1045                          ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1046                         ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1047                                 return (0);
1048                         }
1049                         cprev = cnow;
1050                 }
1051                 /* NOTREACHED */
1052                 /*
1053                  * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1054                  * Return: write counters to the user passed counter struct
1055                  * NB: both 1->0 and 0->1 transitions are counted except for
1056                  *     RI where only 0->1 is counted.
1057                  */
1058         case TIOCGICOUNT:
1059                 save_flags(flags);
1060                 cli();
1061                 cnow = info->icount;
1062                 restore_flags(flags);
1063                 p_cuser = (struct serial_icounter_struct *) arg;
1064                 if(put_user(cnow.cts, &p_cuser->cts))
1065                         return -EFAULT;
1066                 if(put_user(cnow.dsr, &p_cuser->dsr))
1067                         return -EFAULT;
1068                 if(put_user(cnow.rng, &p_cuser->rng))
1069                         return -EFAULT;
1070                 return put_user(cnow.dcd, &p_cuser->dcd);
1071         case MOXA_HighSpeedOn:
1072                 return put_user(info->baud_base != 115200 ? 1 : 0, (int *) arg);
1073         default:
1074                 return (-ENOIOCTLCMD);
1075         }
1076         return (0);
1077 }
1078
1079 static int mxser_ioctl_special(unsigned int cmd, unsigned long arg)
1080 {
1081         int i, result, status;
1082
1083         switch (cmd) {
1084         case MOXA_GET_CONF:
1085                 if(copy_to_user((struct mxser_hwconf *) arg, mxsercfg,
1086                              sizeof(struct mxser_hwconf) * 4))
1087                                 return -EFAULT;
1088                 return 0;
1089         case MOXA_GET_MAJOR:
1090                 if(copy_to_user((int *) arg, &ttymajor, sizeof(int)))
1091                         return -EFAULT;
1092                 return 0;
1093
1094         case MOXA_GET_CUMAJOR:
1095                 result = 0;
1096                 if(copy_to_user((int *) arg, &result, sizeof(int)))
1097                         return -EFAULT;
1098                 return 0;
1099
1100         case MOXA_CHKPORTENABLE:
1101                 result = 0;
1102                 for (i = 0; i < MXSER_PORTS; i++) {
1103                         if (mxvar_table[i].base)
1104                                 result |= (1 << i);
1105                 }
1106                 return put_user(result, (unsigned long *) arg);
1107         case MOXA_GETDATACOUNT:
1108                 if(copy_to_user((struct mxser_log *) arg, &mxvar_log, sizeof(mxvar_log)))
1109                         return -EFAULT;
1110                 return (0);
1111         case MOXA_GETMSTATUS:
1112                 for (i = 0; i < MXSER_PORTS; i++) {
1113                         GMStatus[i].ri = 0;
1114                         if (!mxvar_table[i].base) {
1115                                 GMStatus[i].dcd = 0;
1116                                 GMStatus[i].dsr = 0;
1117                                 GMStatus[i].cts = 0;
1118                                 continue;
1119                         }
1120                         if (!mxvar_table[i].tty || !mxvar_table[i].tty->termios)
1121                                 GMStatus[i].cflag = mxvar_table[i].cflag;
1122                         else
1123                                 GMStatus[i].cflag = mxvar_table[i].tty->termios->c_cflag;
1124
1125                         status = inb(mxvar_table[i].base + UART_MSR);
1126                         if (status & 0x80 /*UART_MSR_DCD */ )
1127                                 GMStatus[i].dcd = 1;
1128                         else
1129                                 GMStatus[i].dcd = 0;
1130
1131                         if (status & 0x20 /*UART_MSR_DSR */ )
1132                                 GMStatus[i].dsr = 1;
1133                         else
1134                                 GMStatus[i].dsr = 0;
1135
1136
1137                         if (status & 0x10 /*UART_MSR_CTS */ )
1138                                 GMStatus[i].cts = 1;
1139                         else
1140                                 GMStatus[i].cts = 0;
1141                 }
1142                 if(copy_to_user((struct mxser_mstatus *) arg, GMStatus,
1143                              sizeof(struct mxser_mstatus) * MXSER_PORTS))
1144                         return -EFAULT;
1145                 return 0;
1146         default:
1147                 return (-ENOIOCTLCMD);
1148         }
1149         return (0);
1150 }
1151
1152 /*
1153  * This routine is called by the upper-layer tty layer to signal that
1154  * incoming characters should be throttled.
1155  */
1156 static void mxser_throttle(struct tty_struct *tty)
1157 {
1158         struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1159         unsigned long flags;
1160
1161         if (I_IXOFF(tty)) {
1162                 info->x_char = STOP_CHAR(tty);
1163                 save_flags(flags);
1164                 cli();
1165                 outb(info->IER, 0);
1166                 info->IER |= UART_IER_THRI;
1167                 outb(info->IER, info->base + UART_IER);         /* force Tx interrupt */
1168                 restore_flags(flags);
1169         }
1170         if (info->tty->termios->c_cflag & CRTSCTS) {
1171                 info->MCR &= ~UART_MCR_RTS;
1172                 save_flags(flags);
1173                 cli();
1174                 outb(info->MCR, info->base + UART_MCR);
1175                 restore_flags(flags);
1176         }
1177 }
1178
1179 static void mxser_unthrottle(struct tty_struct *tty)
1180 {
1181         struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1182         unsigned long flags;
1183
1184         if (I_IXOFF(tty)) {
1185                 if (info->x_char)
1186                         info->x_char = 0;
1187                 else {
1188                         info->x_char = START_CHAR(tty);
1189                         save_flags(flags);
1190                         cli();
1191                         outb(info->IER, 0);
1192                         info->IER |= UART_IER_THRI;     /* force Tx interrupt */
1193                         outb(info->IER, info->base + UART_IER);
1194                         restore_flags(flags);
1195                 }
1196         }
1197         if (info->tty->termios->c_cflag & CRTSCTS) {
1198                 info->MCR |= UART_MCR_RTS;
1199                 save_flags(flags);
1200                 cli();
1201                 outb(info->MCR, info->base + UART_MCR);
1202                 restore_flags(flags);
1203         }
1204 }
1205
1206 static void mxser_set_termios(struct tty_struct *tty,
1207                               struct termios *old_termios)
1208 {
1209         struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1210
1211 /* 8-2-99 by William
1212    if ( (tty->termios->c_cflag == old_termios->c_cflag) &&
1213    (RELEVANT_IFLAG(tty->termios->c_iflag) ==
1214    RELEVANT_IFLAG(old_termios->c_iflag)) )
1215    return;
1216
1217    mxser_change_speed(info, old_termios);
1218
1219    if ( (old_termios->c_cflag & CRTSCTS) &&
1220    !(tty->termios->c_cflag & CRTSCTS) ) {
1221    tty->hw_stopped = 0;
1222    mxser_start(tty);
1223    }
1224  */
1225         if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1226             (RELEVANT_IFLAG(tty->termios->c_iflag) !=
1227              RELEVANT_IFLAG(old_termios->c_iflag))) {
1228
1229                 mxser_change_speed(info, old_termios);
1230
1231                 if ((old_termios->c_cflag & CRTSCTS) &&
1232                     !(tty->termios->c_cflag & CRTSCTS)) {
1233                         tty->hw_stopped = 0;
1234                         mxser_start(tty);
1235                 }
1236         }
1237 /* Handle sw stopped */
1238         if ((old_termios->c_iflag & IXON) &&
1239             !(tty->termios->c_iflag & IXON)) {
1240                 tty->stopped = 0;
1241                 mxser_start(tty);
1242         }
1243 }
1244
1245 /*
1246  * mxser_stop() and mxser_start()
1247  *
1248  * This routines are called before setting or resetting tty->stopped.
1249  * They enable or disable transmitter interrupts, as necessary.
1250  */
1251 static void mxser_stop(struct tty_struct *tty)
1252 {
1253         struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1254         unsigned long flags;
1255
1256         save_flags(flags);
1257         cli();
1258         if (info->IER & UART_IER_THRI) {
1259                 info->IER &= ~UART_IER_THRI;
1260                 outb(info->IER, info->base + UART_IER);
1261         }
1262         restore_flags(flags);
1263 }
1264
1265 static void mxser_start(struct tty_struct *tty)
1266 {
1267         struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1268         unsigned long flags;
1269
1270         save_flags(flags);
1271         cli();
1272         if (info->xmit_cnt && info->xmit_buf &&
1273             !(info->IER & UART_IER_THRI)) {
1274                 info->IER |= UART_IER_THRI;
1275                 outb(info->IER, info->base + UART_IER);
1276         }
1277         restore_flags(flags);
1278 }
1279
1280 /*
1281  * This routine is called by tty_hangup() when a hangup is signaled.
1282  */
1283 void mxser_hangup(struct tty_struct *tty)
1284 {
1285         struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1286
1287         mxser_flush_buffer(tty);
1288         mxser_shutdown(info);
1289         info->event = 0;
1290         info->count = 0;
1291         info->flags &= ~ASYNC_NORMAL_ACTIVE;
1292         info->tty = 0;
1293         wake_up_interruptible(&info->open_wait);
1294 }
1295
1296 /*
1297  * This is the serial driver's generic interrupt routine
1298  */
1299 static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1300 {
1301         int status, i;
1302         struct mxser_struct *info;
1303         struct mxser_struct *port;
1304         int max, irqbits, bits, msr;
1305         int pass_counter = 0;
1306         int handled = 0;
1307
1308         port = 0;
1309         for (i = 0; i < MXSER_BOARDS; i++) {
1310                 if (dev_id == &(mxvar_table[i * MXSER_PORTS_PER_BOARD])) {
1311                         port = dev_id;
1312                         break;
1313                 }
1314         }
1315
1316         if (i == MXSER_BOARDS)
1317                 return IRQ_NONE;
1318         if (port == 0)
1319                 return IRQ_NONE;
1320         max = mxser_numports[mxsercfg[i].board_type];
1321
1322         while (1) {
1323                 irqbits = inb(port->vector) & port->vectormask;
1324                 if (irqbits == port->vectormask)
1325                         break;
1326                 handled = 1;
1327                 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
1328                         if (irqbits == port->vectormask)
1329                                 break;
1330                         if (bits & irqbits)
1331                                 continue;
1332                         info = port + i;
1333                         if (!info->tty ||
1334                           (inb(info->base + UART_IIR) & UART_IIR_NO_INT))
1335                                 continue;
1336                         status = inb(info->base + UART_LSR) & info->read_status_mask;
1337                         if (status & UART_LSR_DR)
1338                                 mxser_receive_chars(info, &status);
1339                         msr = inb(info->base + UART_MSR);
1340                         if (msr & UART_MSR_ANY_DELTA)
1341                                 mxser_check_modem_status(info, msr);
1342                         if (status & UART_LSR_THRE) {
1343 /* 8-2-99 by William
1344    if ( info->x_char || (info->xmit_cnt > 0) )
1345  */
1346                                 mxser_transmit_chars(info);
1347                         }
1348                 }
1349                 if (pass_counter++ > MXSER_ISR_PASS_LIMIT) {
1350 #if 0
1351                         printk("MOXA Smartio/Indusrtio family driver interrupt loop break\n");
1352 #endif
1353                         break;  /* Prevent infinite loops */
1354                 }
1355         }
1356         return IRQ_RETVAL(handled);
1357 }
1358
1359 static inline void mxser_receive_chars(struct mxser_struct *info,
1360                                          int *status)
1361 {
1362         struct tty_struct *tty = info->tty;
1363         unsigned char ch;
1364         int ignored = 0;
1365         int cnt = 0;
1366
1367         do {
1368                 ch = inb(info->base + UART_RX);
1369                 if (*status & info->ignore_status_mask) {
1370                         if (++ignored > 100)
1371                                 break;
1372                 } else {
1373                         if (tty->flip.count >= TTY_FLIPBUF_SIZE)
1374                                 break;
1375                         tty->flip.count++;
1376                         if (*status & UART_LSR_SPECIAL) {
1377                                 if (*status & UART_LSR_BI) {
1378                                         *tty->flip.flag_buf_ptr++ = TTY_BREAK;
1379                                         if (info->flags & ASYNC_SAK)
1380                                                 do_SAK(tty);
1381                                 } else if (*status & UART_LSR_PE) {
1382                                         *tty->flip.flag_buf_ptr++ = TTY_PARITY;
1383                                 } else if (*status & UART_LSR_FE) {
1384                                         *tty->flip.flag_buf_ptr++ = TTY_FRAME;
1385                                 } else if (*status & UART_LSR_OE) {
1386                                         *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
1387                                 } else
1388                                         *tty->flip.flag_buf_ptr++ = 0;
1389                         } else
1390                                 *tty->flip.flag_buf_ptr++ = 0;
1391                         *tty->flip.char_buf_ptr++ = ch;
1392                         cnt++;
1393                 }
1394                 *status = inb(info->base + UART_LSR) & info->read_status_mask;
1395         } while (*status & UART_LSR_DR);
1396         mxvar_log.rxcnt[info->port] += cnt;
1397         schedule_delayed_work(&tty->flip.work, 1);
1398
1399 }
1400
1401 static inline void mxser_transmit_chars(struct mxser_struct *info)
1402 {
1403         int count, cnt;
1404
1405         if (info->x_char) {
1406                 outb(info->x_char, info->base + UART_TX);
1407                 info->x_char = 0;
1408                 mxvar_log.txcnt[info->port]++;
1409                 return;
1410         }
1411         if ((info->xmit_cnt <= 0) || info->tty->stopped ||
1412             info->tty->hw_stopped) {
1413                 info->IER &= ~UART_IER_THRI;
1414                 outb(info->IER, info->base + UART_IER);
1415                 return;
1416         }
1417         cnt = info->xmit_cnt;
1418         count = info->xmit_fifo_size;
1419         do {
1420                 outb(info->xmit_buf[info->xmit_tail++], info->base + UART_TX);
1421                 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1);
1422                 if (--info->xmit_cnt <= 0)
1423                         break;
1424         } while (--count > 0);
1425         mxvar_log.txcnt[info->port] += (cnt - info->xmit_cnt);
1426
1427         if (info->xmit_cnt < WAKEUP_CHARS) {
1428                 set_bit(MXSER_EVENT_TXLOW, &info->event);
1429                 schedule_work(&info->tqueue);
1430         }
1431         if (info->xmit_cnt <= 0) {
1432                 info->IER &= ~UART_IER_THRI;
1433                 outb(info->IER, info->base + UART_IER);
1434         }
1435 }
1436
1437 static inline void mxser_check_modem_status(struct mxser_struct *info,
1438                                               int status)
1439 {
1440
1441         /* update input line counters */
1442         if (status & UART_MSR_TERI)
1443                 info->icount.rng++;
1444         if (status & UART_MSR_DDSR)
1445                 info->icount.dsr++;
1446         if (status & UART_MSR_DDCD)
1447                 info->icount.dcd++;
1448         if (status & UART_MSR_DCTS)
1449                 info->icount.cts++;
1450         wake_up_interruptible(&info->delta_msr_wait);
1451
1452         if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
1453                 if (status & UART_MSR_DCD)
1454                         wake_up_interruptible(&info->open_wait);
1455                 else
1456                         set_bit(MXSER_EVENT_HANGUP, &info->event);
1457                 schedule_work(&info->tqueue);
1458         }
1459         if (info->flags & ASYNC_CTS_FLOW) {
1460                 if (info->tty->hw_stopped) {
1461                         if (status & UART_MSR_CTS) {
1462                                 info->tty->hw_stopped = 0;
1463                                 info->IER |= UART_IER_THRI;
1464                                 outb(info->IER, info->base + UART_IER);
1465
1466                                 set_bit(MXSER_EVENT_TXLOW, &info->event);
1467                                 schedule_work(&info->tqueue);
1468                         }
1469                 } else {
1470                         if (!(status & UART_MSR_CTS)) {
1471                                 info->tty->hw_stopped = 1;
1472                                 info->IER &= ~UART_IER_THRI;
1473                                 outb(info->IER, info->base + UART_IER);
1474                         }
1475                 }
1476         }
1477 }
1478
1479 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
1480                                  struct mxser_struct *info)
1481 {
1482         DECLARE_WAITQUEUE(wait, current);
1483         unsigned long flags;
1484         int retval;
1485         int do_clocal = 0;
1486
1487         /*
1488          * If the device is in the middle of being closed, then block
1489          * until it's done, and then try again.
1490          */
1491         if (tty_hung_up_p(filp) || (info->flags & ASYNC_CLOSING)) {
1492                 if (info->flags & ASYNC_CLOSING)
1493                         interruptible_sleep_on(&info->close_wait);
1494 #ifdef SERIAL_DO_RESTART
1495                 if (info->flags & ASYNC_HUP_NOTIFY)
1496                         return (-EAGAIN);
1497                 else
1498                         return (-ERESTARTSYS);
1499 #else
1500                 return (-EAGAIN);
1501 #endif
1502         }
1503         /*
1504          * If non-blocking mode is set, or the port is not enabled,
1505          * then make the check up front and then exit.
1506          */
1507         if ((filp->f_flags & O_NONBLOCK) ||
1508             (tty->flags & (1 << TTY_IO_ERROR))) {
1509                 info->flags |= ASYNC_NORMAL_ACTIVE;
1510                 return (0);
1511         }
1512         if (tty->termios->c_cflag & CLOCAL)
1513                 do_clocal = 1;
1514
1515         /*
1516          * Block waiting for the carrier detect and the line to become
1517          * free (i.e., not in use by the callout).  While we are in
1518          * this loop, info->count is dropped by one, so that
1519          * mxser_close() knows when to free things.  We restore it upon
1520          * exit, either normal or abnormal.
1521          */
1522         retval = 0;
1523         add_wait_queue(&info->open_wait, &wait);
1524         save_flags(flags);
1525         cli();
1526         if (!tty_hung_up_p(filp))
1527                 info->count--;
1528         restore_flags(flags);
1529         info->blocked_open++;
1530         while (1) {
1531                 save_flags(flags);
1532                 cli();
1533                 outb(inb(info->base + UART_MCR) | UART_MCR_DTR | UART_MCR_RTS,
1534                      info->base + UART_MCR);
1535                 restore_flags(flags);
1536                 set_current_state(TASK_INTERRUPTIBLE);
1537                 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)) {
1538 #ifdef SERIAL_DO_RESTART
1539                         if (info->flags & ASYNC_HUP_NOTIFY)
1540                                 retval = -EAGAIN;
1541                         else
1542                                 retval = -ERESTARTSYS;
1543 #else
1544                         retval = -EAGAIN;
1545 #endif
1546                         break;
1547                 }
1548                 if (!(info->flags & ASYNC_CLOSING) &&
1549                     (do_clocal || (inb(info->base + UART_MSR) & UART_MSR_DCD)))
1550                         break;
1551                 if (signal_pending(current)) {
1552                         retval = -ERESTARTSYS;
1553                         break;
1554                 }
1555                 schedule();
1556         }
1557         set_current_state(TASK_RUNNING);
1558         remove_wait_queue(&info->open_wait, &wait);
1559         if (!tty_hung_up_p(filp))
1560                 info->count++;
1561         info->blocked_open--;
1562         if (retval)
1563                 return (retval);
1564         info->flags |= ASYNC_NORMAL_ACTIVE;
1565         return (0);
1566 }
1567
1568 static int mxser_startup(struct mxser_struct *info)
1569 {
1570         unsigned long flags;
1571         unsigned long page;
1572
1573         page = get_zeroed_page(GFP_KERNEL);
1574         if (!page)
1575                 return (-ENOMEM);
1576
1577         save_flags(flags);
1578         cli();
1579
1580         if (info->flags & ASYNC_INITIALIZED) {
1581                 free_page(page);
1582                 restore_flags(flags);
1583                 return (0);
1584         }
1585         if (!info->base || !info->type) {
1586                 if (info->tty)
1587                         set_bit(TTY_IO_ERROR, &info->tty->flags);
1588                 free_page(page);
1589                 restore_flags(flags);
1590                 return (0);
1591         }
1592         if (info->xmit_buf)
1593                 free_page(page);
1594         else
1595                 info->xmit_buf = (unsigned char *) page;
1596
1597         /*
1598          * Clear the FIFO buffers and disable them
1599          * (they will be reenabled in mxser_change_speed())
1600          */
1601         if (info->xmit_fifo_size == 16)
1602                 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1603                      info->base + UART_FCR);
1604
1605         /*
1606          * At this point there's no way the LSR could still be 0xFF;
1607          * if it is, then bail out, because there's likely no UART
1608          * here.
1609          */
1610         if (inb(info->base + UART_LSR) == 0xff) {
1611                 restore_flags(flags);
1612                 if (capable(CAP_SYS_ADMIN)) {
1613                         if (info->tty)
1614                                 set_bit(TTY_IO_ERROR, &info->tty->flags);
1615                         return (0);
1616                 } else
1617                         return (-ENODEV);
1618         }
1619         /*
1620          * Clear the interrupt registers.
1621          */
1622         (void) inb(info->base + UART_LSR);
1623         (void) inb(info->base + UART_RX);
1624         (void) inb(info->base + UART_IIR);
1625         (void) inb(info->base + UART_MSR);
1626
1627         /*
1628          * Now, initialize the UART
1629          */
1630         outb(UART_LCR_WLEN8, info->base + UART_LCR);    /* reset DLAB */
1631         info->MCR = UART_MCR_DTR | UART_MCR_RTS;
1632         outb(info->MCR, info->base + UART_MCR);
1633
1634         /*
1635          * Finally, enable interrupts
1636          */
1637         info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
1638         outb(info->IER, info->base + UART_IER);         /* enable interrupts */
1639
1640         /*
1641          * And clear the interrupt registers again for luck.
1642          */
1643         (void) inb(info->base + UART_LSR);
1644         (void) inb(info->base + UART_RX);
1645         (void) inb(info->base + UART_IIR);
1646         (void) inb(info->base + UART_MSR);
1647
1648         if (info->tty)
1649                 test_and_clear_bit(TTY_IO_ERROR, &info->tty->flags);
1650
1651         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1652
1653         /*
1654          * and set the speed of the serial port
1655          */
1656         mxser_change_speed(info, 0);
1657
1658         info->flags |= ASYNC_INITIALIZED;
1659         restore_flags(flags);
1660         return (0);
1661 }
1662
1663 /*
1664  * This routine will shutdown a serial port; interrupts maybe disabled, and
1665  * DTR is dropped if the hangup on close termio flag is on.
1666  */
1667 static void mxser_shutdown(struct mxser_struct *info)
1668 {
1669         unsigned long flags;
1670
1671         if (!(info->flags & ASYNC_INITIALIZED))
1672                 return;
1673
1674         save_flags(flags);
1675         cli();                  /* Disable interrupts */
1676
1677         /*
1678          * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1679          * here so the queue might never be waken up
1680          */
1681         wake_up_interruptible(&info->delta_msr_wait);
1682
1683         /*
1684          * Free the IRQ, if necessary
1685          */
1686         if (info->xmit_buf) {
1687                 free_page((unsigned long) info->xmit_buf);
1688                 info->xmit_buf = 0;
1689         }
1690         info->IER = 0;
1691         outb(0x00, info->base + UART_IER);      /* disable all intrs */
1692
1693         if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
1694                 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
1695         outb(info->MCR, info->base + UART_MCR);
1696
1697         /* clear Rx/Tx FIFO's */
1698         outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT), info->base + UART_FCR);
1699         /* read data port to reset things */
1700         (void) inb(info->base + UART_RX);
1701
1702         if (info->tty)
1703                 set_bit(TTY_IO_ERROR, &info->tty->flags);
1704
1705         info->flags &= ~ASYNC_INITIALIZED;
1706         restore_flags(flags);
1707 }
1708
1709 /*
1710  * This routine is called to set the UART divisor registers to match
1711  * the specified baud rate for a serial port.
1712  */
1713 static int mxser_change_speed(struct mxser_struct *info,
1714                               struct termios *old_termios)
1715 {
1716         int quot = 0;
1717         unsigned cflag, cval, fcr;
1718         int i;
1719         int ret = 0;
1720         unsigned long flags;
1721
1722         if (!info->tty || !info->tty->termios)
1723                 return ret;
1724         cflag = info->tty->termios->c_cflag;
1725         if (!(info->base))
1726                 return ret;
1727
1728 #ifndef B921600
1729 #define B921600 (B460800 +1)
1730 #endif
1731         switch (cflag & (CBAUD | CBAUDEX)) {
1732         case B921600:
1733                 i = 20;
1734                 break;
1735         case B460800:
1736                 i = 19;
1737                 break;
1738         case B230400:
1739                 i = 18;
1740                 break;
1741         case B115200:
1742                 i = 17;
1743                 break;
1744         case B57600:
1745                 i = 16;
1746                 break;
1747         case B38400:
1748                 i = 15;
1749                 break;
1750         case B19200:
1751                 i = 14;
1752                 break;
1753         case B9600:
1754                 i = 13;
1755                 break;
1756         case B4800:
1757                 i = 12;
1758                 break;
1759         case B2400:
1760                 i = 11;
1761                 break;
1762         case B1800:
1763                 i = 10;
1764                 break;
1765         case B1200:
1766                 i = 9;
1767                 break;
1768         case B600:
1769                 i = 8;
1770                 break;
1771         case B300:
1772                 i = 7;
1773                 break;
1774         case B200:
1775                 i = 6;
1776                 break;
1777         case B150:
1778                 i = 5;
1779                 break;
1780         case B134:
1781                 i = 4;
1782                 break;
1783         case B110:
1784                 i = 3;
1785                 break;
1786         case B75:
1787                 i = 2;
1788                 break;
1789         case B50:
1790                 i = 1;
1791                 break;
1792         default:
1793                 i = 0;
1794                 break;
1795         }
1796
1797         if (i == 15) {
1798                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1799                         i = 16; /* 57600 bps */
1800                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1801                         i = 17; /* 115200 bps */
1802
1803 #ifdef ASYNC_SPD_SHI
1804                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1805                         i = 18;
1806 #endif
1807
1808 #ifdef ASYNC_SPD_WARP
1809                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1810                         i = 19;
1811 #endif
1812         }
1813         if (mxvar_baud_table[i] == 134) {
1814                 quot = (2 * info->baud_base / 269);
1815         } else if (mxvar_baud_table[i]) {
1816                 quot = info->baud_base / mxvar_baud_table[i];
1817                 if (!quot && old_termios) {
1818                         /* re-calculate */
1819                         info->tty->termios->c_cflag &= ~CBAUD;
1820                         info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
1821                         switch (info->tty->termios->c_cflag & (CBAUD | CBAUDEX)) {
1822                         case B921600:
1823                                 i = 20;
1824                                 break;
1825                         case B460800:
1826                                 i = 19;
1827                                 break;
1828                         case B230400:
1829                                 i = 18;
1830                                 break;
1831                         case B115200:
1832                                 i = 17;
1833                                 break;
1834                         case B57600:
1835                                 i = 16;
1836                                 break;
1837                         case B38400:
1838                                 i = 15;
1839                                 break;
1840                         case B19200:
1841                                 i = 14;
1842                                 break;
1843                         case B9600:
1844                                 i = 13;
1845                                 break;
1846                         case B4800:
1847                                 i = 12;
1848                                 break;
1849                         case B2400:
1850                                 i = 11;
1851                                 break;
1852                         case B1800:
1853                                 i = 10;
1854                                 break;
1855                         case B1200:
1856                                 i = 9;
1857                                 break;
1858                         case B600:
1859                                 i = 8;
1860                                 break;
1861                         case B300:
1862                                 i = 7;
1863                                 break;
1864                         case B200:
1865                                 i = 6;
1866                                 break;
1867                         case B150:
1868                                 i = 5;
1869                                 break;
1870                         case B134:
1871                                 i = 4;
1872                                 break;
1873                         case B110:
1874                                 i = 3;
1875                                 break;
1876                         case B75:
1877                                 i = 2;
1878                                 break;
1879                         case B50:
1880                                 i = 1;
1881                                 break;
1882                         default:
1883                                 i = 0;
1884                                 break;
1885                         }
1886                         if (i == 15) {
1887                                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1888                                         i = 16;         /* 57600 bps */
1889                                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1890                                         i = 17;         /* 115200 bps */
1891 #ifdef ASYNC_SPD_SHI
1892                                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1893                                         i = 18;
1894 #endif
1895 #ifdef ASYNC_SPD_WARP
1896                                 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1897                                         i = 19;
1898 #endif
1899                         }
1900                         if (mxvar_baud_table[i] == 134) {
1901                                 quot = (2 * info->baud_base / 269);
1902                         } else if (mxvar_baud_table[i]) {
1903                                 quot = info->baud_base / mxvar_baud_table[i];
1904                                 if (quot == 0)
1905                                         quot = 1;
1906                         } else {
1907                                 quot = 0;
1908                         }
1909                 } else if (quot == 0)
1910                         quot = 1;
1911         } else {
1912                 quot = 0;
1913         }
1914
1915         if (quot) {
1916                 info->MCR |= UART_MCR_DTR;
1917                 save_flags(flags);
1918                 cli();
1919                 outb(info->MCR, info->base + UART_MCR);
1920                 restore_flags(flags);
1921         } else {
1922                 info->MCR &= ~UART_MCR_DTR;
1923                 save_flags(flags);
1924                 cli();
1925                 outb(info->MCR, info->base + UART_MCR);
1926                 restore_flags(flags);
1927                 return ret;
1928         }
1929         /* byte size and parity */
1930         switch (cflag & CSIZE) {
1931         case CS5:
1932                 cval = 0x00;
1933                 break;
1934         case CS6:
1935                 cval = 0x01;
1936                 break;
1937         case CS7:
1938                 cval = 0x02;
1939                 break;
1940         case CS8:
1941                 cval = 0x03;
1942                 break;
1943         default:
1944                 cval = 0x00;
1945                 break;          /* too keep GCC shut... */
1946         }
1947         if (cflag & CSTOPB)
1948                 cval |= 0x04;
1949         if (cflag & PARENB)
1950                 cval |= UART_LCR_PARITY;
1951         if (!(cflag & PARODD))
1952                 cval |= UART_LCR_EPAR;
1953         if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
1954                 fcr = 0;
1955         } else {
1956                 fcr = UART_FCR_ENABLE_FIFO;
1957                 switch (info->rx_trigger) {
1958                 case 1:
1959                         fcr |= UART_FCR_TRIGGER_1;
1960                         break;
1961                 case 4:
1962                         fcr |= UART_FCR_TRIGGER_4;
1963                         break;
1964                 case 8:
1965                         fcr |= UART_FCR_TRIGGER_8;
1966                         break;
1967                 default:
1968                         fcr |= UART_FCR_TRIGGER_14;
1969                 }
1970         }
1971
1972         /* CTS flow control flag and modem status interrupts */
1973         info->IER &= ~UART_IER_MSI;
1974         info->MCR &= ~UART_MCR_AFE;
1975         if (cflag & CRTSCTS) {
1976                 info->flags |= ASYNC_CTS_FLOW;
1977                 info->IER |= UART_IER_MSI;
1978                 if (info->type == PORT_16550A)
1979                         info->MCR |= UART_MCR_AFE;
1980         } else {
1981                 info->flags &= ~ASYNC_CTS_FLOW;
1982         }
1983         outb(info->MCR, info->base + UART_MCR);
1984         if (cflag & CLOCAL)
1985                 info->flags &= ~ASYNC_CHECK_CD;
1986         else {
1987                 info->flags |= ASYNC_CHECK_CD;
1988                 info->IER |= UART_IER_MSI;
1989         }
1990         outb(info->IER, info->base + UART_IER);
1991
1992         /*
1993          * Set up parity check flag
1994          */
1995         info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1996         if (I_INPCK(info->tty))
1997                 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1998         if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1999                 info->read_status_mask |= UART_LSR_BI;
2000
2001         info->ignore_status_mask = 0;
2002 #if 0
2003         /* This should be safe, but for some broken bits of hardware... */
2004         if (I_IGNPAR(info->tty)) {
2005                 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2006                 info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
2007         }
2008 #endif
2009         if (I_IGNBRK(info->tty)) {
2010                 info->ignore_status_mask |= UART_LSR_BI;
2011                 info->read_status_mask |= UART_LSR_BI;
2012                 /*
2013                  * If we're ignore parity and break indicators, ignore
2014                  * overruns too.  (For real raw support).
2015                  */
2016                 if (I_IGNPAR(info->tty)) {
2017                         info->ignore_status_mask |= UART_LSR_OE | UART_LSR_PE | UART_LSR_FE;
2018                         info->read_status_mask |= UART_LSR_OE | UART_LSR_PE | UART_LSR_FE;
2019                 }
2020         }
2021         save_flags(flags);
2022         cli();
2023         outb(cval | UART_LCR_DLAB, info->base + UART_LCR);      /* set DLAB */
2024         outb(quot & 0xff, info->base + UART_DLL);       /* LS of divisor */
2025         outb(quot >> 8, info->base + UART_DLM);         /* MS of divisor */
2026         outb(cval, info->base + UART_LCR);      /* reset DLAB */
2027         outb(fcr, info->base + UART_FCR);       /* set fcr */
2028         restore_flags(flags);
2029
2030         return ret;
2031 }
2032
2033 /*
2034  * ------------------------------------------------------------
2035  * friends of mxser_ioctl()
2036  * ------------------------------------------------------------
2037  */
2038 static int mxser_get_serial_info(struct mxser_struct *info,
2039                                  struct serial_struct *retinfo)
2040 {
2041         struct serial_struct tmp;
2042
2043         if (!retinfo)
2044                 return (-EFAULT);
2045         memset(&tmp, 0, sizeof(tmp));
2046         tmp.type = info->type;
2047         tmp.line = info->port;
2048         tmp.port = info->base;
2049         tmp.irq = info->irq;
2050         tmp.flags = info->flags;
2051         tmp.baud_base = info->baud_base;
2052         tmp.close_delay = info->close_delay;
2053         tmp.closing_wait = info->closing_wait;
2054         tmp.custom_divisor = info->custom_divisor;
2055         tmp.hub6 = 0;
2056         return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
2057 }
2058
2059 static int mxser_set_serial_info(struct mxser_struct *info,
2060                                  struct serial_struct *new_info)
2061 {
2062         struct serial_struct new_serial;
2063         unsigned int flags;
2064         int retval = 0;
2065
2066         if (!new_info || !info->base)
2067                 return (-EFAULT);
2068         if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2069                 return -EFAULT;
2070
2071         if ((new_serial.irq != info->irq) ||
2072             (new_serial.port != info->base) ||
2073             (new_serial.type != info->type) ||
2074             (new_serial.custom_divisor != info->custom_divisor) ||
2075             (new_serial.baud_base != info->baud_base))
2076                 return (-EPERM);
2077
2078         flags = info->flags & ASYNC_SPD_MASK;
2079
2080         if (!capable(CAP_SYS_ADMIN)) {
2081                 if ((new_serial.baud_base != info->baud_base) ||
2082                     (new_serial.close_delay != info->close_delay) ||
2083                     ((new_serial.flags & ~ASYNC_USR_MASK) !=
2084                      (info->flags & ~ASYNC_USR_MASK)))
2085                         return (-EPERM);
2086                 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
2087                                (new_serial.flags & ASYNC_USR_MASK));
2088         } else {
2089                 /*
2090                  * OK, past this point, all the error checking has been done.
2091                  * At this point, we start making changes.....
2092                  */
2093                 info->flags = ((info->flags & ~ASYNC_FLAGS) |
2094                                (new_serial.flags & ASYNC_FLAGS));
2095                 info->close_delay = new_serial.close_delay * HZ / 100;
2096                 info->closing_wait = new_serial.closing_wait * HZ / 100;
2097         }
2098
2099         if (info->flags & ASYNC_INITIALIZED) {
2100                 if (flags != (info->flags & ASYNC_SPD_MASK)) {
2101                         mxser_change_speed(info, 0);
2102                 }
2103         } else
2104                 retval = mxser_startup(info);
2105         return (retval);
2106 }
2107
2108 /*
2109  * mxser_get_lsr_info - get line status register info
2110  *
2111  * Purpose: Let user call ioctl() to get info when the UART physically
2112  *          is emptied.  On bus types like RS485, the transmitter must
2113  *          release the bus after transmitting. This must be done when
2114  *          the transmit shift register is empty, not be done when the
2115  *          transmit holding register is empty.  This functionality
2116  *          allows an RS485 driver to be written in user space.
2117  */
2118 static int mxser_get_lsr_info(struct mxser_struct *info, unsigned int *value)
2119 {
2120         unsigned char status;
2121         unsigned int result;
2122         unsigned long flags;
2123
2124         save_flags(flags);
2125         cli();
2126         status = inb(info->base + UART_LSR);
2127         restore_flags(flags);
2128         result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
2129         return put_user(result, value);
2130 }
2131
2132 /*
2133  * This routine sends a break character out the serial port.
2134  */
2135 static void mxser_send_break(struct mxser_struct *info, int duration)
2136 {
2137         unsigned long flags;
2138         if (!info->base)
2139                 return;
2140         set_current_state(TASK_INTERRUPTIBLE);
2141         save_flags(flags);
2142         cli();
2143         outb(inb(info->base + UART_LCR) | UART_LCR_SBC, info->base + UART_LCR);
2144         schedule_timeout(duration);
2145         outb(inb(info->base + UART_LCR) & ~UART_LCR_SBC, info->base + UART_LCR);
2146         restore_flags(flags);
2147 }
2148
2149 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
2150 {
2151         struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
2152         unsigned char control, status;
2153         unsigned long flags;
2154
2155         if (PORTNO(tty) == MXSER_PORTS)
2156                 return (-ENOIOCTLCMD);
2157         if (tty->flags & (1 << TTY_IO_ERROR))
2158                 return (-EIO);
2159
2160         control = info->MCR;
2161         save_flags(flags);
2162         cli();
2163         status = inb(info->base + UART_MSR);
2164         if (status & UART_MSR_ANY_DELTA)
2165                 mxser_check_modem_status(info, status);
2166         restore_flags(flags);
2167         return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
2168             ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
2169             ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
2170             ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
2171             ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
2172             ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
2173 }
2174
2175 static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
2176                           unsigned int set, unsigned int clear)
2177 {
2178         struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
2179         unsigned long flags;
2180
2181         if (PORTNO(tty) == MXSER_PORTS)
2182                 return (-ENOIOCTLCMD);
2183         if (tty->flags & (1 << TTY_IO_ERROR))
2184                 return (-EIO);
2185
2186         save_flags(flags);
2187         cli();
2188         if (set & TIOCM_RTS)
2189                 info->MCR |= UART_MCR_RTS;
2190         if (set & TIOCM_DTR)
2191                 info->MCR |= UART_MCR_DTR;
2192
2193         if (clear & TIOCM_RTS)
2194                 info->MCR &= ~UART_MCR_RTS;
2195         if (clear & TIOCM_DTR)
2196                 info->MCR &= ~UART_MCR_DTR;
2197
2198         outb(info->MCR, info->base + UART_MCR);
2199         restore_flags(flags);
2200         return (0);
2201 }
2202
2203 static int mxser_read_register(int, unsigned short *);
2204 static int mxser_program_mode(int);
2205 static void mxser_normal_mode(int);
2206
2207 static int mxser_get_ISA_conf(int cap, struct mxser_hwconf *hwconf)
2208 {
2209         int id, i, bits;
2210         unsigned short regs[16], irq;
2211         unsigned char scratch, scratch2;
2212
2213         id = mxser_read_register(cap, regs);
2214         if (id == C168_ASIC_ID)
2215                 hwconf->board_type = MXSER_BOARD_C168_ISA;
2216         else if (id == C104_ASIC_ID)
2217                 hwconf->board_type = MXSER_BOARD_C104_ISA;
2218         else if (id == C102_ASIC_ID)
2219                 hwconf->board_type = MXSER_BOARD_C102_ISA;
2220         else if (id == CI132_ASIC_ID)
2221                 hwconf->board_type = MXSER_BOARD_CI132;
2222         else if (id == CI134_ASIC_ID)
2223                 hwconf->board_type = MXSER_BOARD_CI134;
2224         else if (id == CI104J_ASIC_ID)
2225                 hwconf->board_type = MXSER_BOARD_CI104J;
2226         else
2227                 return (0);
2228         irq = regs[9] & 0x0F;
2229         irq = irq | (irq << 4);
2230         irq = irq | (irq << 8);
2231         if ((irq != regs[9]) || ((id == 1) && (irq != regs[10]))) {
2232                 return (MXSER_ERR_IRQ_CONFLIT);
2233         }
2234         if (!irq) {
2235                 return (MXSER_ERR_IRQ);
2236         }
2237         for (i = 0; i < 8; i++)
2238                 hwconf->ioaddr[i] = (int) regs[i + 1] & 0xFFF8;
2239         hwconf->irq = (int) (irq & 0x0F);
2240         if ((regs[12] & 0x80) == 0) {
2241                 return (MXSER_ERR_VECTOR);
2242         }
2243         hwconf->vector = (int) regs[11];        /* interrupt vector */
2244         if (id == 1)
2245                 hwconf->vector_mask = 0x00FF;
2246         else
2247                 hwconf->vector_mask = 0x000F;
2248         for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2249                 if (regs[12] & bits)
2250                         hwconf->baud_base[i] = 921600;
2251                 else
2252                         hwconf->baud_base[i] = 115200;
2253         }
2254         scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2255         outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2256         outb(0, cap + UART_EFR);        /* EFR is the same as FCR */
2257         outb(scratch2, cap + UART_LCR);
2258         outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2259         scratch = inb(cap + UART_IIR);
2260         if (scratch & 0xC0)
2261                 hwconf->uart_type = PORT_16550A;
2262         else
2263                 hwconf->uart_type = PORT_16450;
2264         if (id == 1)
2265                 hwconf->ports = 8;
2266         else
2267                 hwconf->ports = 4;
2268         return (hwconf->ports);
2269 }
2270
2271 #define CHIP_SK         0x01    /* Serial Data Clock  in Eprom */
2272 #define CHIP_DO         0x02    /* Serial Data Output in Eprom */
2273 #define CHIP_CS         0x04    /* Serial Chip Select in Eprom */
2274 #define CHIP_DI         0x08    /* Serial Data Input  in Eprom */
2275 #define EN_CCMD         0x000   /* Chip's command register     */
2276 #define EN0_RSARLO      0x008   /* Remote start address reg 0  */
2277 #define EN0_RSARHI      0x009   /* Remote start address reg 1  */
2278 #define EN0_RCNTLO      0x00A   /* Remote byte count reg WR    */
2279 #define EN0_RCNTHI      0x00B   /* Remote byte count reg WR    */
2280 #define EN0_DCFG        0x00E   /* Data configuration reg WR   */
2281 #define EN0_PORT        0x010   /* Rcv missed frame error counter RD */
2282 #define ENC_PAGE0       0x000   /* Select page 0 of chip registers   */
2283 #define ENC_PAGE3       0x0C0   /* Select page 3 of chip registers   */
2284 static int mxser_read_register(int port, unsigned short *regs)
2285 {
2286         int i, k, value, id;
2287         unsigned int j;
2288
2289         id = mxser_program_mode(port);
2290         if (id < 0)
2291                 return (id);
2292         for (i = 0; i < 14; i++) {
2293                 k = (i & 0x3F) | 0x180;
2294                 for (j = 0x100; j > 0; j >>= 1) {
2295                         outb(CHIP_CS, port);
2296                         if (k & j) {
2297                                 outb(CHIP_CS | CHIP_DO, port);
2298                                 outb(CHIP_CS | CHIP_DO | CHIP_SK, port);        /* A? bit of read */
2299                         } else {
2300                                 outb(CHIP_CS, port);
2301                                 outb(CHIP_CS | CHIP_SK, port);  /* A? bit of read */
2302                         }
2303                 }
2304                 (void) inb(port);
2305                 value = 0;
2306                 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
2307                         outb(CHIP_CS, port);
2308                         outb(CHIP_CS | CHIP_SK, port);
2309                         if (inb(port) & CHIP_DI)
2310                                 value |= j;
2311                 }
2312                 regs[i] = value;
2313                 outb(0, port);
2314         }
2315         mxser_normal_mode(port);
2316         return (id);
2317 }
2318
2319 static int mxser_program_mode(int port)
2320 {
2321         int id, i, j, n;
2322         unsigned long flags;
2323
2324         save_flags(flags);
2325         cli();
2326         outb(0, port);
2327         outb(0, port);
2328         outb(0, port);
2329         (void) inb(port);
2330         (void) inb(port);
2331         outb(0, port);
2332         (void) inb(port);
2333         restore_flags(flags);
2334         id = inb(port + 1) & 0x1F;
2335         if ((id != C168_ASIC_ID) && (id != C104_ASIC_ID) && (id != CI104J_ASIC_ID) &&
2336                 (id != C102_ASIC_ID) && (id != CI132_ASIC_ID) && (id != CI134_ASIC_ID))
2337                 return (-1);
2338         for (i = 0, j = 0; i < 4; i++) {
2339                 n = inb(port + 2);
2340                 if (n == 'M') {
2341                         j = 1;
2342                 } else if ((j == 1) && (n == 1)) {
2343                         j = 2;
2344                         break;
2345                 } else
2346                         j = 0;
2347         }
2348         if (j != 2)
2349                 id = -2;
2350         return (id);
2351 }
2352
2353 static void mxser_normal_mode(int port)
2354 {
2355         int i, n;
2356
2357         outb(0xA5, port + 1);
2358         outb(0x80, port + 3);
2359         outb(12, port + 0);     /* 9600 bps */
2360         outb(0, port + 1);
2361         outb(0x03, port + 3);   /* 8 data bits */
2362         outb(0x13, port + 4);   /* loop back mode */
2363         for (i = 0; i < 16; i++) {
2364                 n = inb(port + 5);
2365                 if ((n & 0x61) == 0x60)
2366                         break;
2367                 if ((n & 1) == 1)
2368                         (void) inb(port);
2369         }
2370         outb(0x00, port + 4);
2371 }
2372
2373 module_init(mxser_module_init);
2374 module_exit(mxser_module_exit);