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