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