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