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