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