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