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