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