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