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