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