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