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