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