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