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