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