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