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