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