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