]> err.no Git - linux-2.6/blob - drivers/char/mxser_new.c
Char: mxser_new, upgrade to 1.10
[linux-2.6] / drivers / char / mxser_new.c
1 /*
2  *          mxser.c  -- MOXA Smartio/Industio family multiport serial driver.
3  *
4  *      Copyright (C) 1999-2006  Moxa Technologies (support@moxa.com.tw).
5  *      Copyright (C) 2006-2007  Jiri Slaby <jirislaby@gmail.com>
6  *
7  *      This code is loosely based on the 1.8 moxa driver which is based on
8  *      Linux serial driver, written by Linus Torvalds, Theodore T'so and
9  *      others.
10  *
11  *      This program is free software; you can redistribute it and/or modify
12  *      it under the terms of the GNU General Public License as published by
13  *      the Free Software Foundation; either version 2 of the License, or
14  *      (at your option) any later version.
15  *
16  *      Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
17  *      <alan@redhat.com>. The original 1.8 code is available on www.moxa.com.
18  *      - Fixed x86_64 cleanness
19  *      - Fixed sleep with spinlock held in mxser_send_break
20  */
21
22 #include <linux/module.h>
23 #include <linux/autoconf.h>
24 #include <linux/errno.h>
25 #include <linux/signal.h>
26 #include <linux/sched.h>
27 #include <linux/timer.h>
28 #include <linux/interrupt.h>
29 #include <linux/tty.h>
30 #include <linux/tty_flip.h>
31 #include <linux/serial.h>
32 #include <linux/serial_reg.h>
33 #include <linux/major.h>
34 #include <linux/string.h>
35 #include <linux/fcntl.h>
36 #include <linux/ptrace.h>
37 #include <linux/gfp.h>
38 #include <linux/ioport.h>
39 #include <linux/mm.h>
40 #include <linux/delay.h>
41 #include <linux/pci.h>
42
43 #include <asm/system.h>
44 #include <asm/io.h>
45 #include <asm/irq.h>
46 #include <asm/bitops.h>
47 #include <asm/uaccess.h>
48
49 #include "mxser_new.h"
50
51 #define MXSER_VERSION   "2.0.2"         /* 1.10 */
52 #define MXSERMAJOR       174
53 #define MXSERCUMAJOR     175
54
55 #define MXSER_BOARDS            4       /* Max. boards */
56 #define MXSER_PORTS_PER_BOARD   8       /* Max. ports per board */
57 #define MXSER_PORTS             (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
58 #define MXSER_ISR_PASS_LIMIT    100
59
60 #define MXSER_ERR_IOADDR        -1
61 #define MXSER_ERR_IRQ           -2
62 #define MXSER_ERR_IRQ_CONFLIT   -3
63 #define MXSER_ERR_VECTOR        -4
64
65 /*CheckIsMoxaMust return value*/
66 #define MOXA_OTHER_UART         0x00
67 #define MOXA_MUST_MU150_HWID    0x01
68 #define MOXA_MUST_MU860_HWID    0x02
69
70 #define WAKEUP_CHARS            256
71
72 #define UART_MCR_AFE            0x20
73 #define UART_LSR_SPECIAL        0x1E
74
75 #define PCI_DEVICE_ID_CB108     0x1080
76 #define PCI_DEVICE_ID_CB114     0x1142
77 #define PCI_DEVICE_ID_CB134I    0x1341
78 #define PCI_DEVICE_ID_CP138U    0x1380
79 #define PCI_DEVICE_ID_POS104UL  0x1044
80
81
82 #define C168_ASIC_ID    1
83 #define C104_ASIC_ID    2
84 #define C102_ASIC_ID    0xB
85 #define CI132_ASIC_ID   4
86 #define CI134_ASIC_ID   3
87 #define CI104J_ASIC_ID  5
88
89 #define MXSER_HIGHBAUD  1
90 #define MXSER_HAS2      2
91
92 /* This is only for PCI */
93 static const struct {
94         int type;
95         int tx_fifo;
96         int rx_fifo;
97         int xmit_fifo_size;
98         int rx_high_water;
99         int rx_trigger;
100         int rx_low_water;
101         long max_baud;
102 } Gpci_uart_info[] = {
103         {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
104         {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
105         {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
106 };
107 #define UART_INFO_NUM   ARRAY_SIZE(Gpci_uart_info)
108
109 struct mxser_cardinfo {
110         unsigned int nports;
111         char *name;
112         unsigned int flags;
113 };
114
115 static const struct mxser_cardinfo mxser_cards[] = {
116 /* 0*/  { 8, "C168 series", },                  /* C168-ISA */
117         { 4, "C104 series", },                  /* C104-ISA */
118         { 4, "CI-104J series", },               /* CI104J */
119         { 8, "C168H/PCI series", },             /* C168-PCI */
120         { 4, "C104H/PCI series", },             /* C104-PCI */
121 /* 5*/  { 4, "C102 series", MXSER_HAS2 },       /* C102-ISA */
122         { 4, "CI-132 series", MXSER_HAS2 },     /* CI132 */
123         { 4, "CI-134 series", },                /* CI134 */
124         { 2, "CP-132 series", },                /* CP132 */
125         { 4, "CP-114 series", },                /* CP114 */
126 /*10*/  { 4, "CT-114 series", },                /* CT114 */
127         { 2, "CP-102 series", MXSER_HIGHBAUD }, /* CP102 */
128         { 4, "CP-104U series", },               /* CP104U */
129         { 8, "CP-168U series", },               /* CP168U */
130         { 2, "CP-132U series", },               /* CP132U */
131 /*15*/  { 4, "CP-134U series", },               /* CP134U */
132         { 4, "CP-104JU series", },              /* CP104JU */
133         { 8, "Moxa UC7000 Serial", },           /* RC7000 */
134         { 8, "CP-118U series", },               /* CP118U */
135         { 2, "CP-102UL series", },              /* CP102UL */
136 /*20*/  { 2, "CP-102U series", },               /* CP102U */
137         { 8, "CP-118EL series", },              /* CP118EL */
138         { 8, "CP-168EL series", },              /* CP168EL */
139         { 4, "CP-104EL series", },
140         { 8, "CB-108 series", },
141 /*25*/  { 4, "CB-114 series", },
142         { 4, "CB-134I series", },
143         { 8, "CP-138U series", },
144         { 4, "POS-104UL series", }
145 };
146
147 /* driver_data correspond to the lines in the structure above
148    see also ISA probe function before you change something */
149 static struct pci_device_id mxser_pcibrds[] = {
150         { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C168),
151                 .driver_data = 3 },
152         { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C104),
153                 .driver_data = 4 },
154         { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132),
155                 .driver_data = 8 },
156         { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP114),
157                 .driver_data = 9 },
158         { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CT114),
159                 .driver_data = 10 },
160         { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102),
161                 .driver_data = 11 },
162         { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104U),
163                 .driver_data = 12 },
164         { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168U),
165                 .driver_data = 13 },
166         { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132U),
167                 .driver_data = 14 },
168         { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP134U),
169                 .driver_data = 15 },
170         { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104JU),
171                 .driver_data = 16 },
172         { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_RC7000),
173                 .driver_data = 17 },
174         { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118U),
175                 .driver_data = 18 },
176         { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102UL),
177                 .driver_data = 19 },
178         { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102U),
179                 .driver_data = 20 },
180         { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118EL),
181                 .driver_data = 21 },
182         { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168EL),
183                 .driver_data = 22 },
184         { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104EL),
185                 .driver_data = 23 },
186         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108),       .driver_data = 24 },
187         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114),       .driver_data = 25 },
188         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I),      .driver_data = 26 },
189         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U),      .driver_data = 27 },
190         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL),    .driver_data = 28 },
191         { }
192 };
193 MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
194
195 static int mxvar_baud_table[] = {
196         0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
197         4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
198 };
199 static unsigned int mxvar_baud_table1[] = {
200         0, B50, B75, B110, B134, B150, B200, B300, B600, B1200, B1800, B2400,
201         B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B921600
202 };
203 #define BAUD_TABLE_NO ARRAY_SIZE(mxvar_baud_table)
204
205 #define B_SPEC  B2000000
206
207 static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
208 static int ttymajor = MXSERMAJOR;
209 static int calloutmajor = MXSERCUMAJOR;
210
211 /* Variables for insmod */
212
213 MODULE_AUTHOR("Casper Yang");
214 MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
215 module_param_array(ioaddr, int, NULL, 0);
216 module_param(ttymajor, int, 0);
217 MODULE_LICENSE("GPL");
218
219 struct mxser_log {
220         int tick;
221         unsigned long rxcnt[MXSER_PORTS];
222         unsigned long txcnt[MXSER_PORTS];
223 };
224
225
226 struct mxser_mon {
227         unsigned long rxcnt;
228         unsigned long txcnt;
229         unsigned long up_rxcnt;
230         unsigned long up_txcnt;
231         int modem_status;
232         unsigned char hold_reason;
233 };
234
235 struct mxser_mon_ext {
236         unsigned long rx_cnt[32];
237         unsigned long tx_cnt[32];
238         unsigned long up_rxcnt[32];
239         unsigned long up_txcnt[32];
240         int modem_status[32];
241
242         long baudrate[32];
243         int databits[32];
244         int stopbits[32];
245         int parity[32];
246         int flowctrl[32];
247         int fifo[32];
248         int iftype[32];
249 };
250
251 struct mxser_board;
252
253 struct mxser_port {
254         struct mxser_board *board;
255         struct tty_struct *tty;
256
257         unsigned long ioaddr;
258         unsigned long opmode_ioaddr;
259         int max_baud;
260
261         int rx_high_water;
262         int rx_trigger;         /* Rx fifo trigger level */
263         int rx_low_water;
264         int baud_base;          /* max. speed */
265         long realbaud;
266         int type;               /* UART type */
267         int flags;              /* defined in tty.h */
268         int speed;
269
270         int x_char;             /* xon/xoff character */
271         int IER;                /* Interrupt Enable Register */
272         int MCR;                /* Modem control register */
273
274         unsigned char stop_rx;
275         unsigned char ldisc_stop_rx;
276
277         int custom_divisor;
278         int close_delay;
279         unsigned short closing_wait;
280         unsigned char err_shadow;
281         unsigned long event;
282
283         int count;              /* # of fd on device */
284         int blocked_open;       /* # of blocked opens */
285         struct async_icount icount; /* kernel counters for 4 input interrupts */
286         int timeout;
287
288         int read_status_mask;
289         int ignore_status_mask;
290         int xmit_fifo_size;
291         unsigned char *xmit_buf;
292         int xmit_head;
293         int xmit_tail;
294         int xmit_cnt;
295
296         struct ktermios normal_termios;
297
298         struct mxser_mon mon_data;
299
300         spinlock_t slock;
301         wait_queue_head_t open_wait;
302         wait_queue_head_t delta_msr_wait;
303 };
304
305 struct mxser_board {
306         unsigned int idx;
307         int irq;
308         const struct mxser_cardinfo *info;
309         unsigned long vector;
310         unsigned long vector_mask;
311
312         int chip_flag;
313         int uart_type;
314
315         struct mxser_port ports[MXSER_PORTS_PER_BOARD];
316 };
317
318 struct mxser_mstatus {
319         tcflag_t cflag;
320         int cts;
321         int dsr;
322         int ri;
323         int dcd;
324 };
325
326 static struct mxser_mstatus GMStatus[MXSER_PORTS];
327
328 static int mxserBoardCAP[MXSER_BOARDS] = {
329         0, 0, 0, 0
330         /*  0x180, 0x280, 0x200, 0x320 */
331 };
332
333 static struct mxser_board mxser_boards[MXSER_BOARDS];
334 static struct tty_driver *mxvar_sdriver;
335 static struct mxser_log mxvar_log;
336 static int mxvar_diagflag;
337 static unsigned char mxser_msr[MXSER_PORTS + 1];
338 static struct mxser_mon_ext mon_data_ext;
339 static int mxser_set_baud_method[MXSER_PORTS + 1];
340
341 #ifdef CONFIG_PCI
342 static int __devinit CheckIsMoxaMust(int io)
343 {
344         u8 oldmcr, hwid;
345         int i;
346
347         outb(0, io + UART_LCR);
348         DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
349         oldmcr = inb(io + UART_MCR);
350         outb(0, io + UART_MCR);
351         SET_MOXA_MUST_XON1_VALUE(io, 0x11);
352         if ((hwid = inb(io + UART_MCR)) != 0) {
353                 outb(oldmcr, io + UART_MCR);
354                 return MOXA_OTHER_UART;
355         }
356
357         GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
358         for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
359                 if (hwid == Gpci_uart_info[i].type)
360                         return (int)hwid;
361         }
362         return MOXA_OTHER_UART;
363 }
364 #endif
365
366 static void process_txrx_fifo(struct mxser_port *info)
367 {
368         int i;
369
370         if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
371                 info->rx_trigger = 1;
372                 info->rx_high_water = 1;
373                 info->rx_low_water = 1;
374                 info->xmit_fifo_size = 1;
375         } else
376                 for (i = 0; i < UART_INFO_NUM; i++)
377                         if (info->board->chip_flag == Gpci_uart_info[i].type) {
378                                 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
379                                 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
380                                 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
381                                 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
382                                 break;
383                         }
384 }
385
386 static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
387 {
388         unsigned char status = 0;
389
390         status = inb(baseaddr + UART_MSR);
391
392         mxser_msr[port] &= 0x0F;
393         mxser_msr[port] |= status;
394         status = mxser_msr[port];
395         if (mode)
396                 mxser_msr[port] = 0;
397
398         return status;
399 }
400
401 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
402                 struct mxser_port *port)
403 {
404         DECLARE_WAITQUEUE(wait, current);
405         int retval;
406         int do_clocal = 0;
407         unsigned long flags;
408
409         /*
410          * If non-blocking mode is set, or the port is not enabled,
411          * then make the check up front and then exit.
412          */
413         if ((filp->f_flags & O_NONBLOCK) ||
414                         test_bit(TTY_IO_ERROR, &tty->flags)) {
415                 port->flags |= ASYNC_NORMAL_ACTIVE;
416                 return 0;
417         }
418
419         if (tty->termios->c_cflag & CLOCAL)
420                 do_clocal = 1;
421
422         /*
423          * Block waiting for the carrier detect and the line to become
424          * free (i.e., not in use by the callout).  While we are in
425          * this loop, port->count is dropped by one, so that
426          * mxser_close() knows when to free things.  We restore it upon
427          * exit, either normal or abnormal.
428          */
429         retval = 0;
430         add_wait_queue(&port->open_wait, &wait);
431
432         spin_lock_irqsave(&port->slock, flags);
433         if (!tty_hung_up_p(filp))
434                 port->count--;
435         spin_unlock_irqrestore(&port->slock, flags);
436         port->blocked_open++;
437         while (1) {
438                 spin_lock_irqsave(&port->slock, flags);
439                 outb(inb(port->ioaddr + UART_MCR) |
440                         UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
441                 spin_unlock_irqrestore(&port->slock, flags);
442                 set_current_state(TASK_INTERRUPTIBLE);
443                 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
444                         if (port->flags & ASYNC_HUP_NOTIFY)
445                                 retval = -EAGAIN;
446                         else
447                                 retval = -ERESTARTSYS;
448                         break;
449                 }
450                 if (!(port->flags & ASYNC_CLOSING) &&
451                                 (do_clocal ||
452                                 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
453                         break;
454                 if (signal_pending(current)) {
455                         retval = -ERESTARTSYS;
456                         break;
457                 }
458                 schedule();
459         }
460         set_current_state(TASK_RUNNING);
461         remove_wait_queue(&port->open_wait, &wait);
462         if (!tty_hung_up_p(filp))
463                 port->count++;
464         port->blocked_open--;
465         if (retval)
466                 return retval;
467         port->flags |= ASYNC_NORMAL_ACTIVE;
468         return 0;
469 }
470
471 static int mxser_set_baud(struct mxser_port *info, long newspd)
472 {
473         unsigned int i;
474         int quot = 0;
475         unsigned char cval;
476         int ret = 0;
477
478         if (!info->tty || !info->tty->termios)
479                 return ret;
480
481         if (!(info->ioaddr))
482                 return ret;
483
484         if (newspd > info->max_baud)
485                 return 0;
486
487         info->realbaud = newspd;
488         for (i = 0; i < BAUD_TABLE_NO; i++)
489                if (newspd == mxvar_baud_table[i])
490                        break;
491         if (i == BAUD_TABLE_NO) {
492                 quot = info->baud_base / info->speed;
493                 if (info->speed <= 0 || info->speed > info->max_baud)
494                         quot = 0;
495         } else {
496                 if (newspd == 134) {
497                         quot = (2 * info->baud_base / 269);
498                 } else if (newspd) {
499                         quot = info->baud_base / newspd;
500                         if (quot == 0)
501                                 quot = 1;
502                 } else {
503                         quot = 0;
504                 }
505         }
506
507         info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
508         info->timeout += HZ / 50;       /* Add .02 seconds of slop */
509
510         if (quot) {
511                 info->MCR |= UART_MCR_DTR;
512                 outb(info->MCR, info->ioaddr + UART_MCR);
513         } else {
514                 info->MCR &= ~UART_MCR_DTR;
515                 outb(info->MCR, info->ioaddr + UART_MCR);
516                 return ret;
517         }
518
519         cval = inb(info->ioaddr + UART_LCR);
520
521         outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR);    /* set DLAB */
522
523         outb(quot & 0xff, info->ioaddr + UART_DLL);     /* LS of divisor */
524         outb(quot >> 8, info->ioaddr + UART_DLM);       /* MS of divisor */
525         outb(cval, info->ioaddr + UART_LCR);    /* reset DLAB */
526
527         if (i == BAUD_TABLE_NO) {
528                 quot = info->baud_base % info->speed;
529                 quot *= 8;
530                 if ((quot % info->speed) > (info->speed / 2)) {
531                         quot /= info->speed;
532                         quot++;
533                 } else {
534                         quot /= info->speed;
535                 }
536                 SET_MOXA_MUST_ENUM_VALUE(info->ioaddr, quot);
537         } else
538                 SET_MOXA_MUST_ENUM_VALUE(info->ioaddr, 0);
539
540         return ret;
541 }
542
543 /*
544  * This routine is called to set the UART divisor registers to match
545  * the specified baud rate for a serial port.
546  */
547 static int mxser_change_speed(struct mxser_port *info,
548                 struct ktermios *old_termios)
549 {
550         unsigned cflag, cval, fcr;
551         int ret = 0;
552         unsigned char status;
553         long baud;
554
555         if (!info->tty || !info->tty->termios)
556                 return ret;
557         cflag = info->tty->termios->c_cflag;
558         if (!(info->ioaddr))
559                 return ret;
560
561         if (mxser_set_baud_method[info->tty->index] == 0) {
562                 if ((cflag & CBAUD) == B_SPEC)
563                         baud = info->speed;
564                 else
565                         baud = tty_get_baud_rate(info->tty);
566                 mxser_set_baud(info, baud);
567         }
568
569         /* byte size and parity */
570         switch (cflag & CSIZE) {
571         case CS5:
572                 cval = 0x00;
573                 break;
574         case CS6:
575                 cval = 0x01;
576                 break;
577         case CS7:
578                 cval = 0x02;
579                 break;
580         case CS8:
581                 cval = 0x03;
582                 break;
583         default:
584                 cval = 0x00;
585                 break;          /* too keep GCC shut... */
586         }
587         if (cflag & CSTOPB)
588                 cval |= 0x04;
589         if (cflag & PARENB)
590                 cval |= UART_LCR_PARITY;
591         if (!(cflag & PARODD))
592                 cval |= UART_LCR_EPAR;
593         if (cflag & CMSPAR)
594                 cval |= UART_LCR_SPAR;
595
596         if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
597                 if (info->board->chip_flag) {
598                         fcr = UART_FCR_ENABLE_FIFO;
599                         fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
600                         SET_MOXA_MUST_FIFO_VALUE(info);
601                 } else
602                         fcr = 0;
603         } else {
604                 fcr = UART_FCR_ENABLE_FIFO;
605                 if (info->board->chip_flag) {
606                         fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
607                         SET_MOXA_MUST_FIFO_VALUE(info);
608                 } else {
609                         switch (info->rx_trigger) {
610                         case 1:
611                                 fcr |= UART_FCR_TRIGGER_1;
612                                 break;
613                         case 4:
614                                 fcr |= UART_FCR_TRIGGER_4;
615                                 break;
616                         case 8:
617                                 fcr |= UART_FCR_TRIGGER_8;
618                                 break;
619                         default:
620                                 fcr |= UART_FCR_TRIGGER_14;
621                                 break;
622                         }
623                 }
624         }
625
626         /* CTS flow control flag and modem status interrupts */
627         info->IER &= ~UART_IER_MSI;
628         info->MCR &= ~UART_MCR_AFE;
629         if (cflag & CRTSCTS) {
630                 info->flags |= ASYNC_CTS_FLOW;
631                 info->IER |= UART_IER_MSI;
632                 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
633                         info->MCR |= UART_MCR_AFE;
634                 } else {
635                         status = inb(info->ioaddr + UART_MSR);
636                         if (info->tty->hw_stopped) {
637                                 if (status & UART_MSR_CTS) {
638                                         info->tty->hw_stopped = 0;
639                                         if (info->type != PORT_16550A &&
640                                                         !info->board->chip_flag) {
641                                                 outb(info->IER & ~UART_IER_THRI,
642                                                         info->ioaddr +
643                                                         UART_IER);
644                                                 info->IER |= UART_IER_THRI;
645                                                 outb(info->IER, info->ioaddr +
646                                                                 UART_IER);
647                                         }
648                                         tty_wakeup(info->tty);
649                                 }
650                         } else {
651                                 if (!(status & UART_MSR_CTS)) {
652                                         info->tty->hw_stopped = 1;
653                                         if ((info->type != PORT_16550A) &&
654                                                         (!info->board->chip_flag)) {
655                                                 info->IER &= ~UART_IER_THRI;
656                                                 outb(info->IER, info->ioaddr +
657                                                                 UART_IER);
658                                         }
659                                 }
660                         }
661                 }
662         } else {
663                 info->flags &= ~ASYNC_CTS_FLOW;
664         }
665         outb(info->MCR, info->ioaddr + UART_MCR);
666         if (cflag & CLOCAL) {
667                 info->flags &= ~ASYNC_CHECK_CD;
668         } else {
669                 info->flags |= ASYNC_CHECK_CD;
670                 info->IER |= UART_IER_MSI;
671         }
672         outb(info->IER, info->ioaddr + UART_IER);
673
674         /*
675          * Set up parity check flag
676          */
677         info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
678         if (I_INPCK(info->tty))
679                 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
680         if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
681                 info->read_status_mask |= UART_LSR_BI;
682
683         info->ignore_status_mask = 0;
684
685         if (I_IGNBRK(info->tty)) {
686                 info->ignore_status_mask |= UART_LSR_BI;
687                 info->read_status_mask |= UART_LSR_BI;
688                 /*
689                  * If we're ignore parity and break indicators, ignore
690                  * overruns too.  (For real raw support).
691                  */
692                 if (I_IGNPAR(info->tty)) {
693                         info->ignore_status_mask |=
694                                                 UART_LSR_OE |
695                                                 UART_LSR_PE |
696                                                 UART_LSR_FE;
697                         info->read_status_mask |=
698                                                 UART_LSR_OE |
699                                                 UART_LSR_PE |
700                                                 UART_LSR_FE;
701                 }
702         }
703         if (info->board->chip_flag) {
704                 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
705                 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
706                 if (I_IXON(info->tty)) {
707                         ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
708                 } else {
709                         DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
710                 }
711                 if (I_IXOFF(info->tty)) {
712                         ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
713                 } else {
714                         DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
715                 }
716         }
717
718
719         outb(fcr, info->ioaddr + UART_FCR);     /* set fcr */
720         outb(cval, info->ioaddr + UART_LCR);
721
722         return ret;
723 }
724
725 static void mxser_check_modem_status(struct mxser_port *port, int status)
726 {
727         /* update input line counters */
728         if (status & UART_MSR_TERI)
729                 port->icount.rng++;
730         if (status & UART_MSR_DDSR)
731                 port->icount.dsr++;
732         if (status & UART_MSR_DDCD)
733                 port->icount.dcd++;
734         if (status & UART_MSR_DCTS)
735                 port->icount.cts++;
736         port->mon_data.modem_status = status;
737         wake_up_interruptible(&port->delta_msr_wait);
738
739         if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
740                 if (status & UART_MSR_DCD)
741                         wake_up_interruptible(&port->open_wait);
742         }
743
744         if (port->flags & ASYNC_CTS_FLOW) {
745                 if (port->tty->hw_stopped) {
746                         if (status & UART_MSR_CTS) {
747                                 port->tty->hw_stopped = 0;
748
749                                 if ((port->type != PORT_16550A) &&
750                                                 (!port->board->chip_flag)) {
751                                         outb(port->IER & ~UART_IER_THRI,
752                                                 port->ioaddr + UART_IER);
753                                         port->IER |= UART_IER_THRI;
754                                         outb(port->IER, port->ioaddr +
755                                                         UART_IER);
756                                 }
757                                 tty_wakeup(port->tty);
758                         }
759                 } else {
760                         if (!(status & UART_MSR_CTS)) {
761                                 port->tty->hw_stopped = 1;
762                                 if (port->type != PORT_16550A &&
763                                                 !port->board->chip_flag) {
764                                         port->IER &= ~UART_IER_THRI;
765                                         outb(port->IER, port->ioaddr +
766                                                         UART_IER);
767                                 }
768                         }
769                 }
770         }
771 }
772
773 static int mxser_startup(struct mxser_port *info)
774 {
775         unsigned long page;
776         unsigned long flags;
777
778         page = __get_free_page(GFP_KERNEL);
779         if (!page)
780                 return -ENOMEM;
781
782         spin_lock_irqsave(&info->slock, flags);
783
784         if (info->flags & ASYNC_INITIALIZED) {
785                 free_page(page);
786                 spin_unlock_irqrestore(&info->slock, flags);
787                 return 0;
788         }
789
790         if (!info->ioaddr || !info->type) {
791                 if (info->tty)
792                         set_bit(TTY_IO_ERROR, &info->tty->flags);
793                 free_page(page);
794                 spin_unlock_irqrestore(&info->slock, flags);
795                 return 0;
796         }
797         if (info->xmit_buf)
798                 free_page(page);
799         else
800                 info->xmit_buf = (unsigned char *) page;
801
802         /*
803          * Clear the FIFO buffers and disable them
804          * (they will be reenabled in mxser_change_speed())
805          */
806         if (info->board->chip_flag)
807                 outb((UART_FCR_CLEAR_RCVR |
808                         UART_FCR_CLEAR_XMIT |
809                         MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
810         else
811                 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
812                         info->ioaddr + UART_FCR);
813
814         /*
815          * At this point there's no way the LSR could still be 0xFF;
816          * if it is, then bail out, because there's likely no UART
817          * here.
818          */
819         if (inb(info->ioaddr + UART_LSR) == 0xff) {
820                 spin_unlock_irqrestore(&info->slock, flags);
821                 if (capable(CAP_SYS_ADMIN)) {
822                         if (info->tty)
823                                 set_bit(TTY_IO_ERROR, &info->tty->flags);
824                         return 0;
825                 } else
826                         return -ENODEV;
827         }
828
829         /*
830          * Clear the interrupt registers.
831          */
832         (void) inb(info->ioaddr + UART_LSR);
833         (void) inb(info->ioaddr + UART_RX);
834         (void) inb(info->ioaddr + UART_IIR);
835         (void) inb(info->ioaddr + UART_MSR);
836
837         /*
838          * Now, initialize the UART
839          */
840         outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR);  /* reset DLAB */
841         info->MCR = UART_MCR_DTR | UART_MCR_RTS;
842         outb(info->MCR, info->ioaddr + UART_MCR);
843
844         /*
845          * Finally, enable interrupts
846          */
847         info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
848
849         if (info->board->chip_flag)
850                 info->IER |= MOXA_MUST_IER_EGDAI;
851         outb(info->IER, info->ioaddr + UART_IER);       /* enable interrupts */
852
853         /*
854          * And clear the interrupt registers again for luck.
855          */
856         (void) inb(info->ioaddr + UART_LSR);
857         (void) inb(info->ioaddr + UART_RX);
858         (void) inb(info->ioaddr + UART_IIR);
859         (void) inb(info->ioaddr + UART_MSR);
860
861         if (info->tty)
862                 clear_bit(TTY_IO_ERROR, &info->tty->flags);
863         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
864
865         /*
866          * and set the speed of the serial port
867          */
868         mxser_change_speed(info, NULL);
869         info->flags |= ASYNC_INITIALIZED;
870         spin_unlock_irqrestore(&info->slock, flags);
871
872         return 0;
873 }
874
875 /*
876  * This routine will shutdown a serial port; interrupts maybe disabled, and
877  * DTR is dropped if the hangup on close termio flag is on.
878  */
879 static void mxser_shutdown(struct mxser_port *info)
880 {
881         unsigned long flags;
882
883         if (!(info->flags & ASYNC_INITIALIZED))
884                 return;
885
886         spin_lock_irqsave(&info->slock, flags);
887
888         /*
889          * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
890          * here so the queue might never be waken up
891          */
892         wake_up_interruptible(&info->delta_msr_wait);
893
894         /*
895          * Free the IRQ, if necessary
896          */
897         if (info->xmit_buf) {
898                 free_page((unsigned long) info->xmit_buf);
899                 info->xmit_buf = NULL;
900         }
901
902         info->IER = 0;
903         outb(0x00, info->ioaddr + UART_IER);
904
905         if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
906                 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
907         outb(info->MCR, info->ioaddr + UART_MCR);
908
909         /* clear Rx/Tx FIFO's */
910         if (info->board->chip_flag)
911                 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
912                                 MOXA_MUST_FCR_GDA_MODE_ENABLE,
913                                 info->ioaddr + UART_FCR);
914         else
915                 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
916                         info->ioaddr + UART_FCR);
917
918         /* read data port to reset things */
919         (void) inb(info->ioaddr + UART_RX);
920
921         if (info->tty)
922                 set_bit(TTY_IO_ERROR, &info->tty->flags);
923
924         info->flags &= ~ASYNC_INITIALIZED;
925
926         if (info->board->chip_flag)
927                 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
928
929         spin_unlock_irqrestore(&info->slock, flags);
930 }
931
932 /*
933  * This routine is called whenever a serial port is opened.  It
934  * enables interrupts for a serial port, linking in its async structure into
935  * the IRQ chain.   It also performs the serial-specific
936  * initialization for the tty structure.
937  */
938 static int mxser_open(struct tty_struct *tty, struct file *filp)
939 {
940         struct mxser_port *info;
941         unsigned long flags;
942         int retval, line;
943
944         line = tty->index;
945         if (line == MXSER_PORTS)
946                 return 0;
947         if (line < 0 || line > MXSER_PORTS)
948                 return -ENODEV;
949         info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
950         if (!info->ioaddr)
951                 return -ENODEV;
952
953         tty->driver_data = info;
954         info->tty = tty;
955         /*
956          * Start up serial port
957          */
958         spin_lock_irqsave(&info->slock, flags);
959         info->count++;
960         spin_unlock_irqrestore(&info->slock, flags);
961         retval = mxser_startup(info);
962         if (retval)
963                 return retval;
964
965         retval = mxser_block_til_ready(tty, filp, info);
966         if (retval)
967                 return retval;
968
969         /* unmark here for very high baud rate (ex. 921600 bps) used */
970         tty->low_latency = 1;
971         return 0;
972 }
973
974 /*
975  * This routine is called when the serial port gets closed.  First, we
976  * wait for the last remaining data to be sent.  Then, we unlink its
977  * async structure from the interrupt chain if necessary, and we free
978  * that IRQ if nothing is left in the chain.
979  */
980 static void mxser_close(struct tty_struct *tty, struct file *filp)
981 {
982         struct mxser_port *info = tty->driver_data;
983
984         unsigned long timeout;
985         unsigned long flags;
986
987         if (tty->index == MXSER_PORTS)
988                 return;
989         if (!info)
990                 return;
991
992         spin_lock_irqsave(&info->slock, flags);
993
994         if (tty_hung_up_p(filp)) {
995                 spin_unlock_irqrestore(&info->slock, flags);
996                 return;
997         }
998         if ((tty->count == 1) && (info->count != 1)) {
999                 /*
1000                  * Uh, oh.  tty->count is 1, which means that the tty
1001                  * structure will be freed.  Info->count should always
1002                  * be one in these conditions.  If it's greater than
1003                  * one, we've got real problems, since it means the
1004                  * serial port won't be shutdown.
1005                  */
1006                 printk(KERN_ERR "mxser_close: bad serial port count; "
1007                         "tty->count is 1, info->count is %d\n", info->count);
1008                 info->count = 1;
1009         }
1010         if (--info->count < 0) {
1011                 printk(KERN_ERR "mxser_close: bad serial port count for "
1012                         "ttys%d: %d\n", tty->index, info->count);
1013                 info->count = 0;
1014         }
1015         if (info->count) {
1016                 spin_unlock_irqrestore(&info->slock, flags);
1017                 return;
1018         }
1019         info->flags |= ASYNC_CLOSING;
1020         spin_unlock_irqrestore(&info->slock, flags);
1021         /*
1022          * Save the termios structure, since this port may have
1023          * separate termios for callout and dialin.
1024          */
1025         if (info->flags & ASYNC_NORMAL_ACTIVE)
1026                 info->normal_termios = *tty->termios;
1027         /*
1028          * Now we wait for the transmit buffer to clear; and we notify
1029          * the line discipline to only process XON/XOFF characters.
1030          */
1031         tty->closing = 1;
1032         if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1033                 tty_wait_until_sent(tty, info->closing_wait);
1034         /*
1035          * At this point we stop accepting input.  To do this, we
1036          * disable the receive line status interrupts, and tell the
1037          * interrupt driver to stop checking the data ready bit in the
1038          * line status register.
1039          */
1040         info->IER &= ~UART_IER_RLSI;
1041         if (info->board->chip_flag)
1042                 info->IER &= ~MOXA_MUST_RECV_ISR;
1043
1044         if (info->flags & ASYNC_INITIALIZED) {
1045                 outb(info->IER, info->ioaddr + UART_IER);
1046                 /*
1047                  * Before we drop DTR, make sure the UART transmitter
1048                  * has completely drained; this is especially
1049                  * important if there is a transmit FIFO!
1050                  */
1051                 timeout = jiffies + HZ;
1052                 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
1053                         schedule_timeout_interruptible(5);
1054                         if (time_after(jiffies, timeout))
1055                                 break;
1056                 }
1057         }
1058         mxser_shutdown(info);
1059
1060         if (tty->driver->flush_buffer)
1061                 tty->driver->flush_buffer(tty);
1062
1063         tty_ldisc_flush(tty);
1064
1065         tty->closing = 0;
1066         info->event = 0;
1067         info->tty = NULL;
1068         if (info->blocked_open) {
1069                 if (info->close_delay)
1070                         schedule_timeout_interruptible(info->close_delay);
1071                 wake_up_interruptible(&info->open_wait);
1072         }
1073
1074         info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1075 }
1076
1077 static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1078 {
1079         int c, total = 0;
1080         struct mxser_port *info = tty->driver_data;
1081         unsigned long flags;
1082
1083         if (!info->xmit_buf)
1084                 return 0;
1085
1086         while (1) {
1087                 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1088                                           SERIAL_XMIT_SIZE - info->xmit_head));
1089                 if (c <= 0)
1090                         break;
1091
1092                 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1093                 spin_lock_irqsave(&info->slock, flags);
1094                 info->xmit_head = (info->xmit_head + c) &
1095                                   (SERIAL_XMIT_SIZE - 1);
1096                 info->xmit_cnt += c;
1097                 spin_unlock_irqrestore(&info->slock, flags);
1098
1099                 buf += c;
1100                 count -= c;
1101                 total += c;
1102         }
1103
1104         if (info->xmit_cnt && !tty->stopped) {
1105                 if (!tty->hw_stopped ||
1106                                 (info->type == PORT_16550A) ||
1107                                 (info->board->chip_flag)) {
1108                         spin_lock_irqsave(&info->slock, flags);
1109                         outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1110                                         UART_IER);
1111                         info->IER |= UART_IER_THRI;
1112                         outb(info->IER, info->ioaddr + UART_IER);
1113                         spin_unlock_irqrestore(&info->slock, flags);
1114                 }
1115         }
1116         return total;
1117 }
1118
1119 static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1120 {
1121         struct mxser_port *info = tty->driver_data;
1122         unsigned long flags;
1123
1124         if (!info->xmit_buf)
1125                 return;
1126
1127         if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1128                 return;
1129
1130         spin_lock_irqsave(&info->slock, flags);
1131         info->xmit_buf[info->xmit_head++] = ch;
1132         info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1133         info->xmit_cnt++;
1134         spin_unlock_irqrestore(&info->slock, flags);
1135         if (!tty->stopped) {
1136                 if (!tty->hw_stopped ||
1137                                 (info->type == PORT_16550A) ||
1138                                 info->board->chip_flag) {
1139                         spin_lock_irqsave(&info->slock, flags);
1140                         outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1141                         info->IER |= UART_IER_THRI;
1142                         outb(info->IER, info->ioaddr + UART_IER);
1143                         spin_unlock_irqrestore(&info->slock, flags);
1144                 }
1145         }
1146 }
1147
1148
1149 static void mxser_flush_chars(struct tty_struct *tty)
1150 {
1151         struct mxser_port *info = tty->driver_data;
1152         unsigned long flags;
1153
1154         if (info->xmit_cnt <= 0 ||
1155                         tty->stopped ||
1156                         !info->xmit_buf ||
1157                         (tty->hw_stopped &&
1158                          (info->type != PORT_16550A) &&
1159                          (!info->board->chip_flag)
1160                         ))
1161                 return;
1162
1163         spin_lock_irqsave(&info->slock, flags);
1164
1165         outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1166         info->IER |= UART_IER_THRI;
1167         outb(info->IER, info->ioaddr + UART_IER);
1168
1169         spin_unlock_irqrestore(&info->slock, flags);
1170 }
1171
1172 static int mxser_write_room(struct tty_struct *tty)
1173 {
1174         struct mxser_port *info = tty->driver_data;
1175         int ret;
1176
1177         ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1178         if (ret < 0)
1179                 ret = 0;
1180         return ret;
1181 }
1182
1183 static int mxser_chars_in_buffer(struct tty_struct *tty)
1184 {
1185         struct mxser_port *info = tty->driver_data;
1186         return info->xmit_cnt;
1187 }
1188
1189 static void mxser_flush_buffer(struct tty_struct *tty)
1190 {
1191         struct mxser_port *info = tty->driver_data;
1192         char fcr;
1193         unsigned long flags;
1194
1195
1196         spin_lock_irqsave(&info->slock, flags);
1197         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1198
1199         fcr = inb(info->ioaddr + UART_FCR);
1200         outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1201                 info->ioaddr + UART_FCR);
1202         outb(fcr, info->ioaddr + UART_FCR);
1203
1204         spin_unlock_irqrestore(&info->slock, flags);
1205
1206         tty_wakeup(tty);
1207 }
1208
1209 /*
1210  * ------------------------------------------------------------
1211  * friends of mxser_ioctl()
1212  * ------------------------------------------------------------
1213  */
1214 static int mxser_get_serial_info(struct mxser_port *info,
1215                 struct serial_struct __user *retinfo)
1216 {
1217         struct serial_struct tmp;
1218
1219         if (!retinfo)
1220                 return -EFAULT;
1221         memset(&tmp, 0, sizeof(tmp));
1222         tmp.type = info->type;
1223         tmp.line = info->tty->index;
1224         tmp.port = info->ioaddr;
1225         tmp.irq = info->board->irq;
1226         tmp.flags = info->flags;
1227         tmp.baud_base = info->baud_base;
1228         tmp.close_delay = info->close_delay;
1229         tmp.closing_wait = info->closing_wait;
1230         tmp.custom_divisor = info->custom_divisor;
1231         tmp.hub6 = 0;
1232         if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1233                 return -EFAULT;
1234         return 0;
1235 }
1236
1237 static int mxser_set_serial_info(struct mxser_port *info,
1238                 struct serial_struct __user *new_info)
1239 {
1240         struct serial_struct new_serial;
1241         unsigned long sl_flags;
1242         unsigned int flags;
1243         int retval = 0;
1244
1245         if (!new_info || !info->ioaddr)
1246                 return -EFAULT;
1247         if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1248                 return -EFAULT;
1249
1250         if ((new_serial.irq != info->board->irq) ||
1251                         (new_serial.port != info->ioaddr) ||
1252                         (new_serial.custom_divisor != info->custom_divisor) ||
1253                         (new_serial.baud_base != info->baud_base))
1254                 return -EPERM;
1255
1256         flags = info->flags & ASYNC_SPD_MASK;
1257
1258         if (!capable(CAP_SYS_ADMIN)) {
1259                 if ((new_serial.baud_base != info->baud_base) ||
1260                                 (new_serial.close_delay != info->close_delay) ||
1261                                 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
1262                         return -EPERM;
1263                 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1264                                 (new_serial.flags & ASYNC_USR_MASK));
1265         } else {
1266                 /*
1267                  * OK, past this point, all the error checking has been done.
1268                  * At this point, we start making changes.....
1269                  */
1270                 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1271                                 (new_serial.flags & ASYNC_FLAGS));
1272                 info->close_delay = new_serial.close_delay * HZ / 100;
1273                 info->closing_wait = new_serial.closing_wait * HZ / 100;
1274                 info->tty->low_latency =
1275                                 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1276                 info->tty->low_latency = 0;
1277         }
1278
1279         info->type = new_serial.type;
1280
1281         process_txrx_fifo(info);
1282
1283         if (info->flags & ASYNC_INITIALIZED) {
1284                 if (flags != (info->flags & ASYNC_SPD_MASK)) {
1285                         spin_lock_irqsave(&info->slock, sl_flags);
1286                         mxser_change_speed(info, NULL);
1287                         spin_unlock_irqrestore(&info->slock, sl_flags);
1288                 }
1289         } else
1290                 retval = mxser_startup(info);
1291
1292         return retval;
1293 }
1294
1295 /*
1296  * mxser_get_lsr_info - get line status register info
1297  *
1298  * Purpose: Let user call ioctl() to get info when the UART physically
1299  *          is emptied.  On bus types like RS485, the transmitter must
1300  *          release the bus after transmitting. This must be done when
1301  *          the transmit shift register is empty, not be done when the
1302  *          transmit holding register is empty.  This functionality
1303  *          allows an RS485 driver to be written in user space.
1304  */
1305 static int mxser_get_lsr_info(struct mxser_port *info,
1306                 unsigned int __user *value)
1307 {
1308         unsigned char status;
1309         unsigned int result;
1310         unsigned long flags;
1311
1312         spin_lock_irqsave(&info->slock, flags);
1313         status = inb(info->ioaddr + UART_LSR);
1314         spin_unlock_irqrestore(&info->slock, flags);
1315         result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1316         return put_user(result, value);
1317 }
1318
1319 /*
1320  * This routine sends a break character out the serial port.
1321  */
1322 static void mxser_send_break(struct mxser_port *info, int duration)
1323 {
1324         unsigned long flags;
1325
1326         if (!info->ioaddr)
1327                 return;
1328         set_current_state(TASK_INTERRUPTIBLE);
1329         spin_lock_irqsave(&info->slock, flags);
1330         outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1331                 info->ioaddr + UART_LCR);
1332         spin_unlock_irqrestore(&info->slock, flags);
1333         schedule_timeout(duration);
1334         spin_lock_irqsave(&info->slock, flags);
1335         outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1336                 info->ioaddr + UART_LCR);
1337         spin_unlock_irqrestore(&info->slock, flags);
1338 }
1339
1340 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
1341 {
1342         struct mxser_port *info = tty->driver_data;
1343         unsigned char control, status;
1344         unsigned long flags;
1345
1346
1347         if (tty->index == MXSER_PORTS)
1348                 return -ENOIOCTLCMD;
1349         if (test_bit(TTY_IO_ERROR, &tty->flags))
1350                 return -EIO;
1351
1352         control = info->MCR;
1353
1354         spin_lock_irqsave(&info->slock, flags);
1355         status = inb(info->ioaddr + UART_MSR);
1356         if (status & UART_MSR_ANY_DELTA)
1357                 mxser_check_modem_status(info, status);
1358         spin_unlock_irqrestore(&info->slock, flags);
1359         return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1360                     ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1361                     ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1362                     ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1363                     ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1364                     ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1365 }
1366
1367 static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1368                 unsigned int set, unsigned int clear)
1369 {
1370         struct mxser_port *info = tty->driver_data;
1371         unsigned long flags;
1372
1373
1374         if (tty->index == MXSER_PORTS)
1375                 return -ENOIOCTLCMD;
1376         if (test_bit(TTY_IO_ERROR, &tty->flags))
1377                 return -EIO;
1378
1379         spin_lock_irqsave(&info->slock, flags);
1380
1381         if (set & TIOCM_RTS)
1382                 info->MCR |= UART_MCR_RTS;
1383         if (set & TIOCM_DTR)
1384                 info->MCR |= UART_MCR_DTR;
1385
1386         if (clear & TIOCM_RTS)
1387                 info->MCR &= ~UART_MCR_RTS;
1388         if (clear & TIOCM_DTR)
1389                 info->MCR &= ~UART_MCR_DTR;
1390
1391         outb(info->MCR, info->ioaddr + UART_MCR);
1392         spin_unlock_irqrestore(&info->slock, flags);
1393         return 0;
1394 }
1395
1396 static int __init mxser_program_mode(int port)
1397 {
1398         int id, i, j, n;
1399
1400         outb(0, port);
1401         outb(0, port);
1402         outb(0, port);
1403         (void)inb(port);
1404         (void)inb(port);
1405         outb(0, port);
1406         (void)inb(port);
1407
1408         id = inb(port + 1) & 0x1F;
1409         if ((id != C168_ASIC_ID) &&
1410                         (id != C104_ASIC_ID) &&
1411                         (id != C102_ASIC_ID) &&
1412                         (id != CI132_ASIC_ID) &&
1413                         (id != CI134_ASIC_ID) &&
1414                         (id != CI104J_ASIC_ID))
1415                 return -1;
1416         for (i = 0, j = 0; i < 4; i++) {
1417                 n = inb(port + 2);
1418                 if (n == 'M') {
1419                         j = 1;
1420                 } else if ((j == 1) && (n == 1)) {
1421                         j = 2;
1422                         break;
1423                 } else
1424                         j = 0;
1425         }
1426         if (j != 2)
1427                 id = -2;
1428         return id;
1429 }
1430
1431 static void __init mxser_normal_mode(int port)
1432 {
1433         int i, n;
1434
1435         outb(0xA5, port + 1);
1436         outb(0x80, port + 3);
1437         outb(12, port + 0);     /* 9600 bps */
1438         outb(0, port + 1);
1439         outb(0x03, port + 3);   /* 8 data bits */
1440         outb(0x13, port + 4);   /* loop back mode */
1441         for (i = 0; i < 16; i++) {
1442                 n = inb(port + 5);
1443                 if ((n & 0x61) == 0x60)
1444                         break;
1445                 if ((n & 1) == 1)
1446                         (void)inb(port);
1447         }
1448         outb(0x00, port + 4);
1449 }
1450
1451 #define CHIP_SK         0x01    /* Serial Data Clock  in Eprom */
1452 #define CHIP_DO         0x02    /* Serial Data Output in Eprom */
1453 #define CHIP_CS         0x04    /* Serial Chip Select in Eprom */
1454 #define CHIP_DI         0x08    /* Serial Data Input  in Eprom */
1455 #define EN_CCMD         0x000   /* Chip's command register     */
1456 #define EN0_RSARLO      0x008   /* Remote start address reg 0  */
1457 #define EN0_RSARHI      0x009   /* Remote start address reg 1  */
1458 #define EN0_RCNTLO      0x00A   /* Remote byte count reg WR    */
1459 #define EN0_RCNTHI      0x00B   /* Remote byte count reg WR    */
1460 #define EN0_DCFG        0x00E   /* Data configuration reg WR   */
1461 #define EN0_PORT        0x010   /* Rcv missed frame error counter RD */
1462 #define ENC_PAGE0       0x000   /* Select page 0 of chip registers   */
1463 #define ENC_PAGE3       0x0C0   /* Select page 3 of chip registers   */
1464 static int __init mxser_read_register(int port, unsigned short *regs)
1465 {
1466         int i, k, value, id;
1467         unsigned int j;
1468
1469         id = mxser_program_mode(port);
1470         if (id < 0)
1471                 return id;
1472         for (i = 0; i < 14; i++) {
1473                 k = (i & 0x3F) | 0x180;
1474                 for (j = 0x100; j > 0; j >>= 1) {
1475                         outb(CHIP_CS, port);
1476                         if (k & j) {
1477                                 outb(CHIP_CS | CHIP_DO, port);
1478                                 outb(CHIP_CS | CHIP_DO | CHIP_SK, port);        /* A? bit of read */
1479                         } else {
1480                                 outb(CHIP_CS, port);
1481                                 outb(CHIP_CS | CHIP_SK, port);  /* A? bit of read */
1482                         }
1483                 }
1484                 (void)inb(port);
1485                 value = 0;
1486                 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1487                         outb(CHIP_CS, port);
1488                         outb(CHIP_CS | CHIP_SK, port);
1489                         if (inb(port) & CHIP_DI)
1490                                 value |= j;
1491                 }
1492                 regs[i] = value;
1493                 outb(0, port);
1494         }
1495         mxser_normal_mode(port);
1496         return id;
1497 }
1498
1499 static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1500 {
1501         struct mxser_port *port;
1502         int result, status;
1503         unsigned int i, j;
1504
1505         switch (cmd) {
1506         case MOXA_GET_CONF:
1507 /*              if (copy_to_user(argp, mxsercfg,
1508                                 sizeof(struct mxser_hwconf) * 4))
1509                         return -EFAULT;
1510                 return 0;*/
1511                 return -ENXIO;
1512         case MOXA_GET_MAJOR:
1513                 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1514                         return -EFAULT;
1515                 return 0;
1516
1517         case MOXA_GET_CUMAJOR:
1518                 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1519                         return -EFAULT;
1520                 return 0;
1521
1522         case MOXA_CHKPORTENABLE:
1523                 result = 0;
1524
1525                 for (i = 0; i < MXSER_BOARDS; i++)
1526                         for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1527                                 if (mxser_boards[i].ports[j].ioaddr)
1528                                         result |= (1 << i);
1529
1530                 return put_user(result, (unsigned long __user *)argp);
1531         case MOXA_GETDATACOUNT:
1532                 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1533                         return -EFAULT;
1534                 return 0;
1535         case MOXA_GETMSTATUS:
1536                 for (i = 0; i < MXSER_BOARDS; i++)
1537                         for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1538                                 port = &mxser_boards[i].ports[j];
1539
1540                                 GMStatus[i].ri = 0;
1541                                 if (!port->ioaddr) {
1542                                         GMStatus[i].dcd = 0;
1543                                         GMStatus[i].dsr = 0;
1544                                         GMStatus[i].cts = 0;
1545                                         continue;
1546                                 }
1547
1548                                 if (!port->tty || !port->tty->termios)
1549                                         GMStatus[i].cflag =
1550                                                 port->normal_termios.c_cflag;
1551                                 else
1552                                         GMStatus[i].cflag =
1553                                                 port->tty->termios->c_cflag;
1554
1555                                 status = inb(port->ioaddr + UART_MSR);
1556                                 if (status & 0x80 /*UART_MSR_DCD */ )
1557                                         GMStatus[i].dcd = 1;
1558                                 else
1559                                         GMStatus[i].dcd = 0;
1560
1561                                 if (status & 0x20 /*UART_MSR_DSR */ )
1562                                         GMStatus[i].dsr = 1;
1563                                 else
1564                                         GMStatus[i].dsr = 0;
1565
1566
1567                                 if (status & 0x10 /*UART_MSR_CTS */ )
1568                                         GMStatus[i].cts = 1;
1569                                 else
1570                                         GMStatus[i].cts = 0;
1571                         }
1572                 if (copy_to_user(argp, GMStatus,
1573                                 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1574                         return -EFAULT;
1575                 return 0;
1576         case MOXA_ASPP_MON_EXT: {
1577                 int p, shiftbit;
1578                 unsigned long opmode;
1579                 unsigned cflag, iflag;
1580
1581                 for (i = 0; i < MXSER_BOARDS; i++)
1582                         for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1583                                 port = &mxser_boards[i].ports[j];
1584                                 if (!port->ioaddr)
1585                                         continue;
1586
1587                                 status = mxser_get_msr(port->ioaddr, 0, i);
1588
1589                                 if (status & UART_MSR_TERI)
1590                                         port->icount.rng++;
1591                                 if (status & UART_MSR_DDSR)
1592                                         port->icount.dsr++;
1593                                 if (status & UART_MSR_DDCD)
1594                                         port->icount.dcd++;
1595                                 if (status & UART_MSR_DCTS)
1596                                         port->icount.cts++;
1597
1598                                 port->mon_data.modem_status = status;
1599                                 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1600                                 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1601                                 mon_data_ext.up_rxcnt[i] =
1602                                         port->mon_data.up_rxcnt;
1603                                 mon_data_ext.up_txcnt[i] =
1604                                         port->mon_data.up_txcnt;
1605                                 mon_data_ext.modem_status[i] =
1606                                         port->mon_data.modem_status;
1607                                 mon_data_ext.baudrate[i] = port->realbaud;
1608
1609                                 if (!port->tty || !port->tty->termios) {
1610                                         cflag = port->normal_termios.c_cflag;
1611                                         iflag = port->normal_termios.c_iflag;
1612                                 } else {
1613                                         cflag = port->tty->termios->c_cflag;
1614                                         iflag = port->tty->termios->c_iflag;
1615                                 }
1616
1617                                 mon_data_ext.databits[i] = cflag & CSIZE;
1618
1619                                 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1620
1621                                 mon_data_ext.parity[i] =
1622                                         cflag & (PARENB | PARODD | CMSPAR);
1623
1624                                 mon_data_ext.flowctrl[i] = 0x00;
1625
1626                                 if (cflag & CRTSCTS)
1627                                         mon_data_ext.flowctrl[i] |= 0x03;
1628
1629                                 if (iflag & (IXON | IXOFF))
1630                                         mon_data_ext.flowctrl[i] |= 0x0C;
1631
1632                                 if (port->type == PORT_16550A)
1633                                         mon_data_ext.fifo[i] = 1;
1634                                 else
1635                                         mon_data_ext.fifo[i] = 0;
1636
1637                                 p = i % 4;
1638                                 shiftbit = p * 2;
1639                                 opmode = inb(port->opmode_ioaddr) >> shiftbit;
1640                                 opmode &= OP_MODE_MASK;
1641
1642                                 mon_data_ext.iftype[i] = opmode;
1643
1644                         }
1645                         if (copy_to_user(argp, &mon_data_ext,
1646                                                 sizeof(mon_data_ext)))
1647                                 return -EFAULT;
1648
1649                         return 0;
1650
1651         } default:
1652                 return -ENOIOCTLCMD;
1653         }
1654         return 0;
1655 }
1656
1657 static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1658                 unsigned int cmd, unsigned long arg)
1659 {
1660         struct mxser_port *info = tty->driver_data;
1661         struct async_icount cprev, cnow;        /* kernel counter temps */
1662         struct serial_icounter_struct __user *p_cuser;
1663         unsigned long templ;
1664         unsigned long flags;
1665         unsigned int i;
1666         void __user *argp = (void __user *)arg;
1667         int retval;
1668
1669         if (tty->index == MXSER_PORTS)
1670                 return mxser_ioctl_special(cmd, argp);
1671
1672         if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1673                 int p;
1674                 unsigned long opmode;
1675                 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1676                 int shiftbit;
1677                 unsigned char val, mask;
1678
1679                 p = tty->index % 4;
1680                 if (cmd == MOXA_SET_OP_MODE) {
1681                         if (get_user(opmode, (int __user *) argp))
1682                                 return -EFAULT;
1683                         if (opmode != RS232_MODE &&
1684                                         opmode != RS485_2WIRE_MODE &&
1685                                         opmode != RS422_MODE &&
1686                                         opmode != RS485_4WIRE_MODE)
1687                                 return -EFAULT;
1688                         mask = ModeMask[p];
1689                         shiftbit = p * 2;
1690                         val = inb(info->opmode_ioaddr);
1691                         val &= mask;
1692                         val |= (opmode << shiftbit);
1693                         outb(val, info->opmode_ioaddr);
1694                 } else {
1695                         shiftbit = p * 2;
1696                         opmode = inb(info->opmode_ioaddr) >> shiftbit;
1697                         opmode &= OP_MODE_MASK;
1698                         if (copy_to_user(argp, &opmode, sizeof(int)))
1699                                 return -EFAULT;
1700                 }
1701                 return 0;
1702         }
1703
1704         if (cmd == MOXA_SET_SPECIAL_BAUD_RATE) {
1705                 int speed;
1706
1707                 if (get_user(speed, (int __user *)argp))
1708                         return -EFAULT;
1709                 if (speed <= 0 || speed > info->max_baud)
1710                         return -EFAULT;
1711                 if (!info->tty || !info->tty->termios || !info->ioaddr)
1712                         return 0;
1713                 info->tty->termios->c_cflag &= ~(CBAUD | CBAUDEX);
1714                 for (i = 0; i < BAUD_TABLE_NO; i++)
1715                         if (speed == mxvar_baud_table[i])
1716                                 break;
1717                 if (i == BAUD_TABLE_NO) {
1718                         info->tty->termios->c_cflag |= B_SPEC;
1719                 } else if (speed != 0)
1720                         info->tty->termios->c_cflag |= mxvar_baud_table1[i];
1721
1722                 info->speed = speed;
1723                 spin_lock_irqsave(&info->slock, flags);
1724                 mxser_change_speed(info, NULL);
1725                 spin_unlock_irqrestore(&info->slock, flags);
1726
1727                 return 0;
1728         } else if (cmd == MOXA_GET_SPECIAL_BAUD_RATE) {
1729                 if (copy_to_user(argp, &info->speed, sizeof(int)))
1730                      return -EFAULT;
1731                 return 0;
1732         }
1733
1734         if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1735                         test_bit(TTY_IO_ERROR, &tty->flags))
1736                 return -EIO;
1737
1738         switch (cmd) {
1739         case TCSBRK:            /* SVID version: non-zero arg --> no break */
1740                 retval = tty_check_change(tty);
1741                 if (retval)
1742                         return retval;
1743                 tty_wait_until_sent(tty, 0);
1744                 if (!arg)
1745                         mxser_send_break(info, HZ / 4); /* 1/4 second */
1746                 return 0;
1747         case TCSBRKP:           /* support for POSIX tcsendbreak() */
1748                 retval = tty_check_change(tty);
1749                 if (retval)
1750                         return retval;
1751                 tty_wait_until_sent(tty, 0);
1752                 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1753                 return 0;
1754         case TIOCGSOFTCAR:
1755                 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1756         case TIOCSSOFTCAR:
1757                 if (get_user(templ, (unsigned long __user *) argp))
1758                         return -EFAULT;
1759                 arg = templ;
1760                 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1761                 return 0;
1762         case TIOCGSERIAL:
1763                 return mxser_get_serial_info(info, argp);
1764         case TIOCSSERIAL:
1765                 return mxser_set_serial_info(info, argp);
1766         case TIOCSERGETLSR:     /* Get line status register */
1767                 return mxser_get_lsr_info(info, argp);
1768                 /*
1769                  * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1770                  * - mask passed in arg for lines of interest
1771                  *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1772                  * Caller should use TIOCGICOUNT to see which one it was
1773                  */
1774         case TIOCMIWAIT:
1775                 spin_lock_irqsave(&info->slock, flags);
1776                 cnow = info->icount;    /* note the counters on entry */
1777                 spin_unlock_irqrestore(&info->slock, flags);
1778
1779                 wait_event_interruptible(info->delta_msr_wait, ({
1780                         cprev = cnow;
1781                         spin_lock_irqsave(&info->slock, flags);
1782                         cnow = info->icount;    /* atomic copy */
1783                         spin_unlock_irqrestore(&info->slock, flags);
1784
1785                         ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1786                         ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1787                         ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
1788                         ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts));
1789                 }));
1790                 break;
1791         /*
1792          * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1793          * Return: write counters to the user passed counter struct
1794          * NB: both 1->0 and 0->1 transitions are counted except for
1795          *     RI where only 0->1 is counted.
1796          */
1797         case TIOCGICOUNT:
1798                 spin_lock_irqsave(&info->slock, flags);
1799                 cnow = info->icount;
1800                 spin_unlock_irqrestore(&info->slock, flags);
1801                 p_cuser = argp;
1802                 if (put_user(cnow.frame, &p_cuser->frame))
1803                         return -EFAULT;
1804                 if (put_user(cnow.brk, &p_cuser->brk))
1805                         return -EFAULT;
1806                 if (put_user(cnow.overrun, &p_cuser->overrun))
1807                         return -EFAULT;
1808                 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1809                         return -EFAULT;
1810                 if (put_user(cnow.parity, &p_cuser->parity))
1811                         return -EFAULT;
1812                 if (put_user(cnow.rx, &p_cuser->rx))
1813                         return -EFAULT;
1814                 if (put_user(cnow.tx, &p_cuser->tx))
1815                         return -EFAULT;
1816                 put_user(cnow.cts, &p_cuser->cts);
1817                 put_user(cnow.dsr, &p_cuser->dsr);
1818                 put_user(cnow.rng, &p_cuser->rng);
1819                 put_user(cnow.dcd, &p_cuser->dcd);
1820                 return 0;
1821         case MOXA_HighSpeedOn:
1822                 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1823         case MOXA_SDS_RSTICOUNTER:
1824                 info->mon_data.rxcnt = 0;
1825                 info->mon_data.txcnt = 0;
1826                 return 0;
1827         case MOXA_ASPP_SETBAUD:{
1828                 long baud;
1829                 if (get_user(baud, (long __user *)argp))
1830                         return -EFAULT;
1831                 spin_lock_irqsave(&info->slock, flags);
1832                 mxser_set_baud(info, baud);
1833                 spin_unlock_irqrestore(&info->slock, flags);
1834                 return 0;
1835         }
1836         case MOXA_ASPP_GETBAUD:
1837                 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1838                         return -EFAULT;
1839
1840                 return 0;
1841
1842         case MOXA_ASPP_OQUEUE:{
1843                 int len, lsr;
1844
1845                 len = mxser_chars_in_buffer(tty);
1846
1847                 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1848
1849                 len += (lsr ? 0 : 1);
1850
1851                 if (copy_to_user(argp, &len, sizeof(int)))
1852                         return -EFAULT;
1853
1854                 return 0;
1855         }
1856         case MOXA_ASPP_MON: {
1857                 int mcr, status;
1858
1859                 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1860                 mxser_check_modem_status(info, status);
1861
1862                 mcr = inb(info->ioaddr + UART_MCR);
1863                 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1864                         info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1865                 else
1866                         info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1867
1868                 if (mcr & MOXA_MUST_MCR_TX_XON)
1869                         info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1870                 else
1871                         info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1872
1873                 if (info->tty->hw_stopped)
1874                         info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1875                 else
1876                         info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1877
1878                 if (copy_to_user(argp, &info->mon_data,
1879                                 sizeof(struct mxser_mon)))
1880                         return -EFAULT;
1881
1882                 return 0;
1883         }
1884         case MOXA_ASPP_LSTATUS: {
1885                 if (copy_to_user(argp, &info->err_shadow,
1886                                 sizeof(unsigned char)))
1887                         return -EFAULT;
1888
1889                 info->err_shadow = 0;
1890                 return 0;
1891         }
1892         case MOXA_SET_BAUD_METHOD: {
1893                 int method;
1894
1895                 if (get_user(method, (int __user *)argp))
1896                         return -EFAULT;
1897                 mxser_set_baud_method[tty->index] = method;
1898                 if (copy_to_user(argp, &method, sizeof(int)))
1899                         return -EFAULT;
1900
1901                 return 0;
1902         }
1903         default:
1904                 return -ENOIOCTLCMD;
1905         }
1906         return 0;
1907 }
1908
1909 static void mxser_stoprx(struct tty_struct *tty)
1910 {
1911         struct mxser_port *info = tty->driver_data;
1912
1913         info->ldisc_stop_rx = 1;
1914         if (I_IXOFF(tty)) {
1915                 if (info->board->chip_flag) {
1916                         info->IER &= ~MOXA_MUST_RECV_ISR;
1917                         outb(info->IER, info->ioaddr + UART_IER);
1918                 } else {
1919                         info->x_char = STOP_CHAR(tty);
1920                         outb(0, info->ioaddr + UART_IER);
1921                         info->IER |= UART_IER_THRI;
1922                         outb(info->IER, info->ioaddr + UART_IER);
1923                 }
1924         }
1925
1926         if (info->tty->termios->c_cflag & CRTSCTS) {
1927                 info->MCR &= ~UART_MCR_RTS;
1928                 outb(info->MCR, info->ioaddr + UART_MCR);
1929         }
1930 }
1931
1932 /*
1933  * This routine is called by the upper-layer tty layer to signal that
1934  * incoming characters should be throttled.
1935  */
1936 static void mxser_throttle(struct tty_struct *tty)
1937 {
1938         mxser_stoprx(tty);
1939 }
1940
1941 static void mxser_unthrottle(struct tty_struct *tty)
1942 {
1943         struct mxser_port *info = tty->driver_data;
1944
1945         /* startrx */
1946         info->ldisc_stop_rx = 0;
1947         if (I_IXOFF(tty)) {
1948                 if (info->x_char)
1949                         info->x_char = 0;
1950                 else {
1951                         if (info->board->chip_flag) {
1952                                 info->IER |= MOXA_MUST_RECV_ISR;
1953                                 outb(info->IER, info->ioaddr + UART_IER);
1954                         } else {
1955                                 info->x_char = START_CHAR(tty);
1956                                 outb(0, info->ioaddr + UART_IER);
1957                                 info->IER |= UART_IER_THRI;
1958                                 outb(info->IER, info->ioaddr + UART_IER);
1959                         }
1960                 }
1961         }
1962
1963         if (info->tty->termios->c_cflag & CRTSCTS) {
1964                 info->MCR |= UART_MCR_RTS;
1965                 outb(info->MCR, info->ioaddr + UART_MCR);
1966         }
1967 }
1968
1969 /*
1970  * mxser_stop() and mxser_start()
1971  *
1972  * This routines are called before setting or resetting tty->stopped.
1973  * They enable or disable transmitter interrupts, as necessary.
1974  */
1975 static void mxser_stop(struct tty_struct *tty)
1976 {
1977         struct mxser_port *info = tty->driver_data;
1978         unsigned long flags;
1979
1980         spin_lock_irqsave(&info->slock, flags);
1981         if (info->IER & UART_IER_THRI) {
1982                 info->IER &= ~UART_IER_THRI;
1983                 outb(info->IER, info->ioaddr + UART_IER);
1984         }
1985         spin_unlock_irqrestore(&info->slock, flags);
1986 }
1987
1988 static void mxser_start(struct tty_struct *tty)
1989 {
1990         struct mxser_port *info = tty->driver_data;
1991         unsigned long flags;
1992
1993         spin_lock_irqsave(&info->slock, flags);
1994         if (info->xmit_cnt && info->xmit_buf) {
1995                 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1996                 info->IER |= UART_IER_THRI;
1997                 outb(info->IER, info->ioaddr + UART_IER);
1998         }
1999         spin_unlock_irqrestore(&info->slock, flags);
2000 }
2001
2002 static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2003 {
2004         struct mxser_port *info = tty->driver_data;
2005         unsigned long flags;
2006
2007         spin_lock_irqsave(&info->slock, flags);
2008         mxser_change_speed(info, old_termios);
2009         spin_unlock_irqrestore(&info->slock, flags);
2010
2011         if ((old_termios->c_cflag & CRTSCTS) &&
2012                         !(tty->termios->c_cflag & CRTSCTS)) {
2013                 tty->hw_stopped = 0;
2014                 mxser_start(tty);
2015         }
2016
2017         /* Handle sw stopped */
2018         if ((old_termios->c_iflag & IXON) &&
2019                         !(tty->termios->c_iflag & IXON)) {
2020                 tty->stopped = 0;
2021
2022                 if (info->board->chip_flag) {
2023                         spin_lock_irqsave(&info->slock, flags);
2024                         DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2025                         spin_unlock_irqrestore(&info->slock, flags);
2026                 }
2027
2028                 mxser_start(tty);
2029         }
2030 }
2031
2032 /*
2033  * mxser_wait_until_sent() --- wait until the transmitter is empty
2034  */
2035 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2036 {
2037         struct mxser_port *info = tty->driver_data;
2038         unsigned long orig_jiffies, char_time;
2039         int lsr;
2040
2041         if (info->type == PORT_UNKNOWN)
2042                 return;
2043
2044         if (info->xmit_fifo_size == 0)
2045                 return;         /* Just in case.... */
2046
2047         orig_jiffies = jiffies;
2048         /*
2049          * Set the check interval to be 1/5 of the estimated time to
2050          * send a single character, and make it at least 1.  The check
2051          * interval should also be less than the timeout.
2052          *
2053          * Note: we have to use pretty tight timings here to satisfy
2054          * the NIST-PCTS.
2055          */
2056         char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2057         char_time = char_time / 5;
2058         if (char_time == 0)
2059                 char_time = 1;
2060         if (timeout && timeout < char_time)
2061                 char_time = timeout;
2062         /*
2063          * If the transmitter hasn't cleared in twice the approximate
2064          * amount of time to send the entire FIFO, it probably won't
2065          * ever clear.  This assumes the UART isn't doing flow
2066          * control, which is currently the case.  Hence, if it ever
2067          * takes longer than info->timeout, this is probably due to a
2068          * UART bug of some kind.  So, we clamp the timeout parameter at
2069          * 2*info->timeout.
2070          */
2071         if (!timeout || timeout > 2 * info->timeout)
2072                 timeout = 2 * info->timeout;
2073 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2074         printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2075                 timeout, char_time);
2076         printk("jiff=%lu...", jiffies);
2077 #endif
2078         while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
2079 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2080                 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2081 #endif
2082                 schedule_timeout_interruptible(char_time);
2083                 if (signal_pending(current))
2084                         break;
2085                 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2086                         break;
2087         }
2088         set_current_state(TASK_RUNNING);
2089
2090 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2091         printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2092 #endif
2093 }
2094
2095 /*
2096  * This routine is called by tty_hangup() when a hangup is signaled.
2097  */
2098 static void mxser_hangup(struct tty_struct *tty)
2099 {
2100         struct mxser_port *info = tty->driver_data;
2101
2102         mxser_flush_buffer(tty);
2103         mxser_shutdown(info);
2104         info->event = 0;
2105         info->count = 0;
2106         info->flags &= ~ASYNC_NORMAL_ACTIVE;
2107         info->tty = NULL;
2108         wake_up_interruptible(&info->open_wait);
2109 }
2110
2111 /*
2112  * mxser_rs_break() --- routine which turns the break handling on or off
2113  */
2114 static void mxser_rs_break(struct tty_struct *tty, int break_state)
2115 {
2116         struct mxser_port *info = tty->driver_data;
2117         unsigned long flags;
2118
2119         spin_lock_irqsave(&info->slock, flags);
2120         if (break_state == -1)
2121                 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2122                         info->ioaddr + UART_LCR);
2123         else
2124                 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2125                         info->ioaddr + UART_LCR);
2126         spin_unlock_irqrestore(&info->slock, flags);
2127 }
2128
2129 static void mxser_receive_chars(struct mxser_port *port, int *status)
2130 {
2131         struct tty_struct *tty = port->tty;
2132         unsigned char ch, gdl;
2133         int ignored = 0;
2134         int cnt = 0;
2135         int recv_room;
2136         int max = 256;
2137
2138         recv_room = tty->receive_room;
2139         if ((recv_room == 0) && (!port->ldisc_stop_rx))
2140                 mxser_stoprx(tty);
2141
2142         if (port->board->chip_flag != MOXA_OTHER_UART) {
2143
2144                 if (*status & UART_LSR_SPECIAL)
2145                         goto intr_old;
2146                 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
2147                                 (*status & MOXA_MUST_LSR_RERR))
2148                         goto intr_old;
2149                 if (*status & MOXA_MUST_LSR_RERR)
2150                         goto intr_old;
2151
2152                 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
2153
2154                 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
2155                         gdl &= MOXA_MUST_GDL_MASK;
2156                 if (gdl >= recv_room) {
2157                         if (!port->ldisc_stop_rx)
2158                                 mxser_stoprx(tty);
2159                 }
2160                 while (gdl--) {
2161                         ch = inb(port->ioaddr + UART_RX);
2162                         tty_insert_flip_char(tty, ch, 0);
2163                         cnt++;
2164                 }
2165                 goto end_intr;
2166         }
2167 intr_old:
2168
2169         do {
2170                 if (max-- < 0)
2171                         break;
2172
2173                 ch = inb(port->ioaddr + UART_RX);
2174                 if (port->board->chip_flag && (*status & UART_LSR_OE))
2175                         outb(0x23, port->ioaddr + UART_FCR);
2176                 *status &= port->read_status_mask;
2177                 if (*status & port->ignore_status_mask) {
2178                         if (++ignored > 100)
2179                                 break;
2180                 } else {
2181                         char flag = 0;
2182                         if (*status & UART_LSR_SPECIAL) {
2183                                 if (*status & UART_LSR_BI) {
2184                                         flag = TTY_BREAK;
2185                                         port->icount.brk++;
2186
2187                                         if (port->flags & ASYNC_SAK)
2188                                                 do_SAK(tty);
2189                                 } else if (*status & UART_LSR_PE) {
2190                                         flag = TTY_PARITY;
2191                                         port->icount.parity++;
2192                                 } else if (*status & UART_LSR_FE) {
2193                                         flag = TTY_FRAME;
2194                                         port->icount.frame++;
2195                                 } else if (*status & UART_LSR_OE) {
2196                                         flag = TTY_OVERRUN;
2197                                         port->icount.overrun++;
2198                                 } else
2199                                         flag = TTY_BREAK;
2200                         }
2201                         tty_insert_flip_char(tty, ch, flag);
2202                         cnt++;
2203                         if (cnt >= recv_room) {
2204                                 if (!port->ldisc_stop_rx)
2205                                         mxser_stoprx(tty);
2206                                 break;
2207                         }
2208
2209                 }
2210
2211                 if (port->board->chip_flag)
2212                         break;
2213
2214                 *status = inb(port->ioaddr + UART_LSR);
2215         } while (*status & UART_LSR_DR);
2216
2217 end_intr:
2218         mxvar_log.rxcnt[port->tty->index] += cnt;
2219         port->mon_data.rxcnt += cnt;
2220         port->mon_data.up_rxcnt += cnt;
2221
2222         /*
2223          * We are called from an interrupt context with &port->slock
2224          * being held. Drop it temporarily in order to prevent
2225          * recursive locking.
2226          */
2227         spin_unlock(&port->slock);
2228         tty_flip_buffer_push(tty);
2229         spin_lock(&port->slock);
2230 }
2231
2232 static void mxser_transmit_chars(struct mxser_port *port)
2233 {
2234         int count, cnt;
2235
2236         if (port->x_char) {
2237                 outb(port->x_char, port->ioaddr + UART_TX);
2238                 port->x_char = 0;
2239                 mxvar_log.txcnt[port->tty->index]++;
2240                 port->mon_data.txcnt++;
2241                 port->mon_data.up_txcnt++;
2242                 port->icount.tx++;
2243                 return;
2244         }
2245
2246         if (port->xmit_buf == 0)
2247                 return;
2248
2249         if ((port->xmit_cnt <= 0) || port->tty->stopped ||
2250                         (port->tty->hw_stopped &&
2251                         (port->type != PORT_16550A) &&
2252                         (!port->board->chip_flag))) {
2253                 port->IER &= ~UART_IER_THRI;
2254                 outb(port->IER, port->ioaddr + UART_IER);
2255                 return;
2256         }
2257
2258         cnt = port->xmit_cnt;
2259         count = port->xmit_fifo_size;
2260         do {
2261                 outb(port->xmit_buf[port->xmit_tail++],
2262                         port->ioaddr + UART_TX);
2263                 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2264                 if (--port->xmit_cnt <= 0)
2265                         break;
2266         } while (--count > 0);
2267         mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
2268
2269         port->mon_data.txcnt += (cnt - port->xmit_cnt);
2270         port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2271         port->icount.tx += (cnt - port->xmit_cnt);
2272
2273         if (port->xmit_cnt < WAKEUP_CHARS)
2274                 tty_wakeup(port->tty);
2275
2276         if (port->xmit_cnt <= 0) {
2277                 port->IER &= ~UART_IER_THRI;
2278                 outb(port->IER, port->ioaddr + UART_IER);
2279         }
2280 }
2281
2282 /*
2283  * This is the serial driver's generic interrupt routine
2284  */
2285 static irqreturn_t mxser_interrupt(int irq, void *dev_id)
2286 {
2287         int status, iir, i;
2288         struct mxser_board *brd = NULL;
2289         struct mxser_port *port;
2290         int max, irqbits, bits, msr;
2291         unsigned int int_cnt, pass_counter = 0;
2292         int handled = IRQ_NONE;
2293
2294         for (i = 0; i < MXSER_BOARDS; i++)
2295                 if (dev_id == &mxser_boards[i]) {
2296                         brd = dev_id;
2297                         break;
2298                 }
2299
2300         if (i == MXSER_BOARDS)
2301                 goto irq_stop;
2302         if (brd == NULL)
2303                 goto irq_stop;
2304         max = brd->info->nports;
2305         while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
2306                 irqbits = inb(brd->vector) & brd->vector_mask;
2307                 if (irqbits == brd->vector_mask)
2308                         break;
2309
2310                 handled = IRQ_HANDLED;
2311                 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2312                         if (irqbits == brd->vector_mask)
2313                                 break;
2314                         if (bits & irqbits)
2315                                 continue;
2316                         port = &brd->ports[i];
2317
2318                         int_cnt = 0;
2319                         spin_lock(&port->slock);
2320                         do {
2321                                 iir = inb(port->ioaddr + UART_IIR);
2322                                 if (iir & UART_IIR_NO_INT)
2323                                         break;
2324                                 iir &= MOXA_MUST_IIR_MASK;
2325                                 if (!port->tty ||
2326                                                 (port->flags & ASYNC_CLOSING) ||
2327                                                 !(port->flags &
2328                                                         ASYNC_INITIALIZED)) {
2329                                         status = inb(port->ioaddr + UART_LSR);
2330                                         outb(0x27, port->ioaddr + UART_FCR);
2331                                         inb(port->ioaddr + UART_MSR);
2332                                         break;
2333                                 }
2334
2335                                 status = inb(port->ioaddr + UART_LSR);
2336
2337                                 if (status & UART_LSR_PE)
2338                                         port->err_shadow |= NPPI_NOTIFY_PARITY;
2339                                 if (status & UART_LSR_FE)
2340                                         port->err_shadow |= NPPI_NOTIFY_FRAMING;
2341                                 if (status & UART_LSR_OE)
2342                                         port->err_shadow |=
2343                                                 NPPI_NOTIFY_HW_OVERRUN;
2344                                 if (status & UART_LSR_BI)
2345                                         port->err_shadow |= NPPI_NOTIFY_BREAK;
2346
2347                                 if (port->board->chip_flag) {
2348                                         if (iir == MOXA_MUST_IIR_GDA ||
2349                                             iir == MOXA_MUST_IIR_RDA ||
2350                                             iir == MOXA_MUST_IIR_RTO ||
2351                                             iir == MOXA_MUST_IIR_LSR)
2352                                                 mxser_receive_chars(port,
2353                                                                 &status);
2354
2355                                 } else {
2356                                         status &= port->read_status_mask;
2357                                         if (status & UART_LSR_DR)
2358                                                 mxser_receive_chars(port,
2359                                                                 &status);
2360                                 }
2361                                 msr = inb(port->ioaddr + UART_MSR);
2362                                 if (msr & UART_MSR_ANY_DELTA)
2363                                         mxser_check_modem_status(port, msr);
2364
2365                                 if (port->board->chip_flag) {
2366                                         if (iir == 0x02 && (status &
2367                                                                 UART_LSR_THRE))
2368                                                 mxser_transmit_chars(port);
2369                                 } else {
2370                                         if (status & UART_LSR_THRE)
2371                                                 mxser_transmit_chars(port);
2372                                 }
2373                         } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2374                         spin_unlock(&port->slock);
2375                 }
2376         }
2377
2378 irq_stop:
2379         return handled;
2380 }
2381
2382 static const struct tty_operations mxser_ops = {
2383         .open = mxser_open,
2384         .close = mxser_close,
2385         .write = mxser_write,
2386         .put_char = mxser_put_char,
2387         .flush_chars = mxser_flush_chars,
2388         .write_room = mxser_write_room,
2389         .chars_in_buffer = mxser_chars_in_buffer,
2390         .flush_buffer = mxser_flush_buffer,
2391         .ioctl = mxser_ioctl,
2392         .throttle = mxser_throttle,
2393         .unthrottle = mxser_unthrottle,
2394         .set_termios = mxser_set_termios,
2395         .stop = mxser_stop,
2396         .start = mxser_start,
2397         .hangup = mxser_hangup,
2398         .break_ctl = mxser_rs_break,
2399         .wait_until_sent = mxser_wait_until_sent,
2400         .tiocmget = mxser_tiocmget,
2401         .tiocmset = mxser_tiocmset,
2402 };
2403
2404 /*
2405  * The MOXA Smartio/Industio serial driver boot-time initialization code!
2406  */
2407
2408 static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
2409                 unsigned int irq)
2410 {
2411         if (irq)
2412                 free_irq(brd->irq, brd);
2413         if (pdev != NULL) {     /* PCI */
2414 #ifdef CONFIG_PCI
2415                 pci_release_region(pdev, 2);
2416                 pci_release_region(pdev, 3);
2417 #endif
2418         } else {
2419                 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2420                 release_region(brd->vector, 1);
2421         }
2422 }
2423
2424 static int __devinit mxser_initbrd(struct mxser_board *brd,
2425                 struct pci_dev *pdev)
2426 {
2427         struct mxser_port *info;
2428         unsigned int i;
2429         int retval;
2430
2431         printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
2432
2433         for (i = 0; i < brd->info->nports; i++) {
2434                 info = &brd->ports[i];
2435                 info->board = brd;
2436                 info->stop_rx = 0;
2437                 info->ldisc_stop_rx = 0;
2438
2439                 /* Enhance mode enabled here */
2440                 if (brd->chip_flag != MOXA_OTHER_UART)
2441                         ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
2442
2443                 info->flags = ASYNC_SHARE_IRQ;
2444                 info->type = brd->uart_type;
2445
2446                 process_txrx_fifo(info);
2447
2448                 info->custom_divisor = info->baud_base * 16;
2449                 info->close_delay = 5 * HZ / 10;
2450                 info->closing_wait = 30 * HZ;
2451                 info->normal_termios = mxvar_sdriver->init_termios;
2452                 init_waitqueue_head(&info->open_wait);
2453                 init_waitqueue_head(&info->delta_msr_wait);
2454                 info->speed = 9600;
2455                 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2456                 info->err_shadow = 0;
2457                 spin_lock_init(&info->slock);
2458
2459                 /* before set INT ISR, disable all int */
2460                 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2461                         info->ioaddr + UART_IER);
2462         }
2463
2464         retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
2465                         brd);
2466         if (retval) {
2467                 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2468                         "conflict with another device.\n",
2469                         brd->info->name, brd->irq);
2470                 /* We hold resources, we need to release them. */
2471                 mxser_release_res(brd, pdev, 0);
2472         }
2473         return retval;
2474 }
2475
2476 static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
2477 {
2478         int id, i, bits;
2479         unsigned short regs[16], irq;
2480         unsigned char scratch, scratch2;
2481
2482         brd->chip_flag = MOXA_OTHER_UART;
2483
2484         id = mxser_read_register(cap, regs);
2485         switch (id) {
2486         case C168_ASIC_ID:
2487                 brd->info = &mxser_cards[0];
2488                 break;
2489         case C104_ASIC_ID:
2490                 brd->info = &mxser_cards[1];
2491                 break;
2492         case CI104J_ASIC_ID:
2493                 brd->info = &mxser_cards[2];
2494                 break;
2495         case C102_ASIC_ID:
2496                 brd->info = &mxser_cards[5];
2497                 break;
2498         case CI132_ASIC_ID:
2499                 brd->info = &mxser_cards[6];
2500                 break;
2501         case CI134_ASIC_ID:
2502                 brd->info = &mxser_cards[7];
2503                 break;
2504         default:
2505                 return 0;
2506         }
2507
2508         irq = 0;
2509         /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2510            Flag-hack checks if configuration should be read as 2-port here. */
2511         if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
2512                 irq = regs[9] & 0xF000;
2513                 irq = irq | (irq >> 4);
2514                 if (irq != (regs[9] & 0xFF00))
2515                         return MXSER_ERR_IRQ_CONFLIT;
2516         } else if (brd->info->nports == 4) {
2517                 irq = regs[9] & 0xF000;
2518                 irq = irq | (irq >> 4);
2519                 irq = irq | (irq >> 8);
2520                 if (irq != regs[9])
2521                         return MXSER_ERR_IRQ_CONFLIT;
2522         } else if (brd->info->nports == 8) {
2523                 irq = regs[9] & 0xF000;
2524                 irq = irq | (irq >> 4);
2525                 irq = irq | (irq >> 8);
2526                 if ((irq != regs[9]) || (irq != regs[10]))
2527                         return MXSER_ERR_IRQ_CONFLIT;
2528         }
2529
2530         if (!irq)
2531                 return MXSER_ERR_IRQ;
2532         brd->irq = ((int)(irq & 0xF000) >> 12);
2533         for (i = 0; i < 8; i++)
2534                 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
2535         if ((regs[12] & 0x80) == 0)
2536                 return MXSER_ERR_VECTOR;
2537         brd->vector = (int)regs[11];    /* interrupt vector */
2538         if (id == 1)
2539                 brd->vector_mask = 0x00FF;
2540         else
2541                 brd->vector_mask = 0x000F;
2542         for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2543                 if (regs[12] & bits) {
2544                         brd->ports[i].baud_base = 921600;
2545                         brd->ports[i].max_baud = 921600;
2546                 } else {
2547                         brd->ports[i].baud_base = 115200;
2548                         brd->ports[i].max_baud = 115200;
2549                 }
2550         }
2551         scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2552         outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2553         outb(0, cap + UART_EFR);        /* EFR is the same as FCR */
2554         outb(scratch2, cap + UART_LCR);
2555         outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2556         scratch = inb(cap + UART_IIR);
2557
2558         if (scratch & 0xC0)
2559                 brd->uart_type = PORT_16550A;
2560         else
2561                 brd->uart_type = PORT_16450;
2562         if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2563                         "mxser(IO)"))
2564                 return MXSER_ERR_IOADDR;
2565         if (!request_region(brd->vector, 1, "mxser(vector)")) {
2566                 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2567                 return MXSER_ERR_VECTOR;
2568         }
2569         return brd->info->nports;
2570 }
2571
2572 static int __devinit mxser_probe(struct pci_dev *pdev,
2573                 const struct pci_device_id *ent)
2574 {
2575 #ifdef CONFIG_PCI
2576         struct mxser_board *brd;
2577         unsigned int i, j;
2578         unsigned long ioaddress;
2579         int retval = -EINVAL;
2580
2581         for (i = 0; i < MXSER_BOARDS; i++)
2582                 if (mxser_boards[i].info == NULL)
2583                         break;
2584
2585         if (i >= MXSER_BOARDS) {
2586                 printk(KERN_ERR "Too many Smartio/Industio family boards found "
2587                         "(maximum %d), board not configured\n", MXSER_BOARDS);
2588                 goto err;
2589         }
2590
2591         brd = &mxser_boards[i];
2592         brd->idx = i * MXSER_PORTS_PER_BOARD;
2593         printk(KERN_INFO "Found MOXA %s board (BusNo=%d, DevNo=%d)\n",
2594                 mxser_cards[ent->driver_data].name,
2595                 pdev->bus->number, PCI_SLOT(pdev->devfn));
2596
2597         retval = pci_enable_device(pdev);
2598         if (retval) {
2599                 printk(KERN_ERR "Moxa SmartI/O PCI enable fail !\n");
2600                 goto err;
2601         }
2602
2603         /* io address */
2604         ioaddress = pci_resource_start(pdev, 2);
2605         retval = pci_request_region(pdev, 2, "mxser(IO)");
2606         if (retval)
2607                 goto err;
2608
2609         brd->info = &mxser_cards[ent->driver_data];
2610         for (i = 0; i < brd->info->nports; i++)
2611                 brd->ports[i].ioaddr = ioaddress + 8 * i;
2612
2613         /* vector */
2614         ioaddress = pci_resource_start(pdev, 3);
2615         retval = pci_request_region(pdev, 3, "mxser(vector)");
2616         if (retval)
2617                 goto err_relio;
2618         brd->vector = ioaddress;
2619
2620         /* irq */
2621         brd->irq = pdev->irq;
2622
2623         brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2624         brd->uart_type = PORT_16550A;
2625         brd->vector_mask = 0;
2626
2627         for (i = 0; i < brd->info->nports; i++) {
2628                 for (j = 0; j < UART_INFO_NUM; j++) {
2629                         if (Gpci_uart_info[j].type == brd->chip_flag) {
2630                                 brd->ports[i].max_baud =
2631                                         Gpci_uart_info[j].max_baud;
2632
2633                                 /* exception....CP-102 */
2634                                 if (brd->info->flags & MXSER_HIGHBAUD)
2635                                         brd->ports[i].max_baud = 921600;
2636                                 break;
2637                         }
2638                 }
2639         }
2640
2641         if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2642                 for (i = 0; i < brd->info->nports; i++) {
2643                         if (i < 4)
2644                                 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2645                         else
2646                                 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
2647                 }
2648                 outb(0, ioaddress + 4); /* default set to RS232 mode */
2649                 outb(0, ioaddress + 0x0c);      /* default set to RS232 mode */
2650         }
2651
2652         for (i = 0; i < brd->info->nports; i++) {
2653                 brd->vector_mask |= (1 << i);
2654                 brd->ports[i].baud_base = 921600;
2655         }
2656
2657         /* mxser_initbrd will hook ISR. */
2658         retval = mxser_initbrd(brd, pdev);
2659         if (retval)
2660                 goto err_null;
2661
2662         for (i = 0; i < brd->info->nports; i++)
2663                 tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
2664
2665         pci_set_drvdata(pdev, brd);
2666
2667         return 0;
2668 err_relio:
2669         pci_release_region(pdev, 2);
2670 err_null:
2671         brd->info = NULL;
2672 err:
2673         return retval;
2674 #else
2675         return -ENODEV;
2676 #endif
2677 }
2678
2679 static void __devexit mxser_remove(struct pci_dev *pdev)
2680 {
2681         struct mxser_board *brd = pci_get_drvdata(pdev);
2682         unsigned int i;
2683
2684         for (i = 0; i < brd->info->nports; i++)
2685                 tty_unregister_device(mxvar_sdriver, brd->idx + i);
2686
2687         mxser_release_res(brd, pdev, 1);
2688         brd->info = NULL;
2689 }
2690
2691 static struct pci_driver mxser_driver = {
2692         .name = "mxser",
2693         .id_table = mxser_pcibrds,
2694         .probe = mxser_probe,
2695         .remove = __devexit_p(mxser_remove)
2696 };
2697
2698 static int __init mxser_module_init(void)
2699 {
2700         struct mxser_board *brd;
2701         unsigned long cap;
2702         unsigned int i, m, isaloop;
2703         int retval, b;
2704
2705         pr_debug("Loading module mxser ...\n");
2706
2707         mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2708         if (!mxvar_sdriver)
2709                 return -ENOMEM;
2710
2711         printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2712                 MXSER_VERSION);
2713
2714         /* Initialize the tty_driver structure */
2715         mxvar_sdriver->owner = THIS_MODULE;
2716         mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2717         mxvar_sdriver->name = "ttyMI";
2718         mxvar_sdriver->major = ttymajor;
2719         mxvar_sdriver->minor_start = 0;
2720         mxvar_sdriver->num = MXSER_PORTS + 1;
2721         mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2722         mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2723         mxvar_sdriver->init_termios = tty_std_termios;
2724         mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2725         mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2726         tty_set_operations(mxvar_sdriver, &mxser_ops);
2727
2728         retval = tty_register_driver(mxvar_sdriver);
2729         if (retval) {
2730                 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2731                                 "tty driver !\n");
2732                 goto err_put;
2733         }
2734
2735         mxvar_diagflag = 0;
2736
2737         m = 0;
2738         /* Start finding ISA boards here */
2739         for (isaloop = 0; isaloop < 2; isaloop++)
2740                 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2741                         if (!isaloop)
2742                                 cap = mxserBoardCAP[b]; /* predefined */
2743                         else
2744                                 cap = ioaddr[b]; /* module param */
2745
2746                         if (!cap)
2747                                 continue;
2748
2749                         brd = &mxser_boards[m];
2750                         retval = mxser_get_ISA_conf(cap, brd);
2751
2752                         if (retval != 0)
2753                                 printk(KERN_INFO "Found MOXA %s board "
2754                                         "(CAP=0x%x)\n",
2755                                         brd->info->name, ioaddr[b]);
2756
2757                         if (retval <= 0) {
2758                                 if (retval == MXSER_ERR_IRQ)
2759                                         printk(KERN_ERR "Invalid interrupt "
2760                                                 "number, board not "
2761                                                 "configured\n");
2762                                 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2763                                         printk(KERN_ERR "Invalid interrupt "
2764                                                 "number, board not "
2765                                                 "configured\n");
2766                                 else if (retval == MXSER_ERR_VECTOR)
2767                                         printk(KERN_ERR "Invalid interrupt "
2768                                                 "vector, board not "
2769                                                 "configured\n");
2770                                 else if (retval == MXSER_ERR_IOADDR)
2771                                         printk(KERN_ERR "Invalid I/O address, "
2772                                                 "board not configured\n");
2773
2774                                 brd->info = NULL;
2775                                 continue;
2776                         }
2777
2778                         /* mxser_initbrd will hook ISR. */
2779                         if (mxser_initbrd(brd, NULL) < 0) {
2780                                 brd->info = NULL;
2781                                 continue;
2782                         }
2783
2784                         brd->idx = m * MXSER_PORTS_PER_BOARD;
2785                         for (i = 0; i < brd->info->nports; i++)
2786                                 tty_register_device(mxvar_sdriver, brd->idx + i,
2787                                                 NULL);
2788
2789                         m++;
2790                 }
2791
2792         retval = pci_register_driver(&mxser_driver);
2793         if (retval) {
2794                 printk(KERN_ERR "Can't register pci driver\n");
2795                 if (!m) {
2796                         retval = -ENODEV;
2797                         goto err_unr;
2798                 } /* else: we have some ISA cards under control */
2799         }
2800
2801         pr_debug("Done.\n");
2802
2803         return 0;
2804 err_unr:
2805         tty_unregister_driver(mxvar_sdriver);
2806 err_put:
2807         put_tty_driver(mxvar_sdriver);
2808         return retval;
2809 }
2810
2811 static void __exit mxser_module_exit(void)
2812 {
2813         unsigned int i, j;
2814
2815         pr_debug("Unloading module mxser ...\n");
2816
2817         pci_unregister_driver(&mxser_driver);
2818
2819         for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
2820                 if (mxser_boards[i].info != NULL)
2821                         for (j = 0; j < mxser_boards[i].info->nports; j++)
2822                                 tty_unregister_device(mxvar_sdriver,
2823                                                 mxser_boards[i].idx + j);
2824         tty_unregister_driver(mxvar_sdriver);
2825         put_tty_driver(mxvar_sdriver);
2826
2827         for (i = 0; i < MXSER_BOARDS; i++)
2828                 if (mxser_boards[i].info != NULL)
2829                         mxser_release_res(&mxser_boards[i], NULL, 1);
2830
2831         pr_debug("Done.\n");
2832 }
2833
2834 module_init(mxser_module_init);
2835 module_exit(mxser_module_exit);