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