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