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