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