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