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