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