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