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