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