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