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