2 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
4 * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com).
5 * Copyright (C) 2006-2008 Jiri Slaby <jirislaby@gmail.com>
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
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.
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
19 * - Fixed x86_64 cleanness
22 #include <linux/module.h>
23 #include <linux/errno.h>
24 #include <linux/signal.h>
25 #include <linux/sched.h>
26 #include <linux/timer.h>
27 #include <linux/interrupt.h>
28 #include <linux/tty.h>
29 #include <linux/tty_flip.h>
30 #include <linux/serial.h>
31 #include <linux/serial_reg.h>
32 #include <linux/major.h>
33 #include <linux/string.h>
34 #include <linux/fcntl.h>
35 #include <linux/ptrace.h>
36 #include <linux/ioport.h>
38 #include <linux/delay.h>
39 #include <linux/pci.h>
40 #include <linux/bitops.h>
41 #include <linux/slab.h>
42 #include <linux/ratelimit.h>
46 #include <linux/uaccess.h>
50 #define MXSER_VERSION "2.0.5" /* 1.14 */
51 #define MXSERMAJOR 174
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
58 /*CheckIsMoxaMust return value*/
59 #define MOXA_OTHER_UART 0x00
60 #define MOXA_MUST_MU150_HWID 0x01
61 #define MOXA_MUST_MU860_HWID 0x02
63 #define WAKEUP_CHARS 256
65 #define UART_MCR_AFE 0x20
66 #define UART_LSR_SPECIAL 0x1E
68 #define PCI_DEVICE_ID_POS104UL 0x1044
69 #define PCI_DEVICE_ID_CB108 0x1080
70 #define PCI_DEVICE_ID_CP102UF 0x1023
71 #define PCI_DEVICE_ID_CP112UL 0x1120
72 #define PCI_DEVICE_ID_CB114 0x1142
73 #define PCI_DEVICE_ID_CP114UL 0x1143
74 #define PCI_DEVICE_ID_CB134I 0x1341
75 #define PCI_DEVICE_ID_CP138U 0x1380
78 #define C168_ASIC_ID 1
79 #define C104_ASIC_ID 2
80 #define C102_ASIC_ID 0xB
81 #define CI132_ASIC_ID 4
82 #define CI134_ASIC_ID 3
83 #define CI104J_ASIC_ID 5
85 #define MXSER_HIGHBAUD 1
88 /* This is only for PCI */
98 } Gpci_uart_info[] = {
99 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
100 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
101 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
103 #define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
105 struct mxser_cardinfo {
111 static const struct mxser_cardinfo mxser_cards[] = {
112 /* 0*/ { "C168 series", 8, },
113 { "C104 series", 4, },
114 { "CI-104J series", 4, },
115 { "C168H/PCI series", 8, },
116 { "C104H/PCI series", 4, },
117 /* 5*/ { "C102 series", 4, MXSER_HAS2 }, /* C102-ISA */
118 { "CI-132 series", 4, MXSER_HAS2 },
119 { "CI-134 series", 4, },
120 { "CP-132 series", 2, },
121 { "CP-114 series", 4, },
122 /*10*/ { "CT-114 series", 4, },
123 { "CP-102 series", 2, MXSER_HIGHBAUD },
124 { "CP-104U series", 4, },
125 { "CP-168U series", 8, },
126 { "CP-132U series", 2, },
127 /*15*/ { "CP-134U series", 4, },
128 { "CP-104JU series", 4, },
129 { "Moxa UC7000 Serial", 8, }, /* RC7000 */
130 { "CP-118U series", 8, },
131 { "CP-102UL series", 2, },
132 /*20*/ { "CP-102U series", 2, },
133 { "CP-118EL series", 8, },
134 { "CP-168EL series", 8, },
135 { "CP-104EL series", 4, },
136 { "CB-108 series", 8, },
137 /*25*/ { "CB-114 series", 4, },
138 { "CB-134I series", 4, },
139 { "CP-138U series", 8, },
140 { "POS-104UL series", 4, },
141 { "CP-114UL series", 4, },
142 /*30*/ { "CP-102UF series", 2, },
143 { "CP-112UL series", 2, },
146 /* driver_data correspond to the lines in the structure above
147 see also ISA probe function before you change something */
148 static const struct pci_device_id mxser_pcibrds[] = {
149 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168), .driver_data = 3 },
150 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104), .driver_data = 4 },
151 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132), .driver_data = 8 },
152 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114), .driver_data = 9 },
153 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114), .driver_data = 10 },
154 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102), .driver_data = 11 },
155 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 12 },
156 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 13 },
157 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 14 },
158 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 15 },
159 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 16 },
160 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 17 },
161 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 18 },
162 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 19 },
163 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 20 },
164 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 21 },
165 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 22 },
166 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 23 },
167 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 24 },
168 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 25 },
169 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 26 },
170 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 27 },
171 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 28 },
172 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP114UL), .driver_data = 29 },
173 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP102UF), .driver_data = 30 },
174 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP112UL), .driver_data = 31 },
177 MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
179 static unsigned long ioaddr[MXSER_BOARDS];
180 static int ttymajor = MXSERMAJOR;
182 /* Variables for insmod */
184 MODULE_AUTHOR("Casper Yang");
185 MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
186 module_param_hw_array(ioaddr, ulong, ioport, NULL, 0);
187 MODULE_PARM_DESC(ioaddr, "ISA io addresses to look for a moxa board");
188 module_param(ttymajor, int, 0);
189 MODULE_LICENSE("GPL");
193 unsigned long rxcnt[MXSER_PORTS];
194 unsigned long txcnt[MXSER_PORTS];
200 unsigned long up_rxcnt;
201 unsigned long up_txcnt;
203 unsigned char hold_reason;
206 struct mxser_mon_ext {
207 unsigned long rx_cnt[32];
208 unsigned long tx_cnt[32];
209 unsigned long up_rxcnt[32];
210 unsigned long up_txcnt[32];
211 int modem_status[32];
225 struct tty_port port;
226 struct mxser_board *board;
228 unsigned long ioaddr;
229 unsigned long opmode_ioaddr;
233 int rx_trigger; /* Rx fifo trigger level */
235 int baud_base; /* max. speed */
236 int type; /* UART type */
238 int x_char; /* xon/xoff character */
239 int IER; /* Interrupt Enable Register */
240 int MCR; /* Modem control register */
242 unsigned char stop_rx;
243 unsigned char ldisc_stop_rx;
246 unsigned char err_shadow;
248 struct async_icount icount; /* kernel counters for 4 input interrupts */
249 unsigned int timeout;
251 int read_status_mask;
252 int ignore_status_mask;
253 unsigned int xmit_fifo_size;
259 struct ktermios normal_termios;
261 struct mxser_mon mon_data;
269 const struct mxser_cardinfo *info;
270 unsigned long vector;
271 unsigned long vector_mask;
276 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
279 struct mxser_mstatus {
287 static struct mxser_board mxser_boards[MXSER_BOARDS];
288 static struct tty_driver *mxvar_sdriver;
289 static struct mxser_log mxvar_log;
290 static int mxser_set_baud_method[MXSER_PORTS + 1];
292 static void mxser_enable_must_enchance_mode(unsigned long baseio)
297 oldlcr = inb(baseio + UART_LCR);
298 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
300 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
301 efr |= MOXA_MUST_EFR_EFRB_ENABLE;
303 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
304 outb(oldlcr, baseio + UART_LCR);
308 static void mxser_disable_must_enchance_mode(unsigned long baseio)
313 oldlcr = inb(baseio + UART_LCR);
314 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
316 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
317 efr &= ~MOXA_MUST_EFR_EFRB_ENABLE;
319 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
320 outb(oldlcr, baseio + UART_LCR);
324 static void mxser_set_must_xon1_value(unsigned long baseio, u8 value)
329 oldlcr = inb(baseio + UART_LCR);
330 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
332 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
333 efr &= ~MOXA_MUST_EFR_BANK_MASK;
334 efr |= MOXA_MUST_EFR_BANK0;
336 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
337 outb(value, baseio + MOXA_MUST_XON1_REGISTER);
338 outb(oldlcr, baseio + UART_LCR);
341 static void mxser_set_must_xoff1_value(unsigned long baseio, u8 value)
346 oldlcr = inb(baseio + UART_LCR);
347 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
349 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
350 efr &= ~MOXA_MUST_EFR_BANK_MASK;
351 efr |= MOXA_MUST_EFR_BANK0;
353 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
354 outb(value, baseio + MOXA_MUST_XOFF1_REGISTER);
355 outb(oldlcr, baseio + UART_LCR);
358 static void mxser_set_must_fifo_value(struct mxser_port *info)
363 oldlcr = inb(info->ioaddr + UART_LCR);
364 outb(MOXA_MUST_ENTER_ENCHANCE, info->ioaddr + UART_LCR);
366 efr = inb(info->ioaddr + MOXA_MUST_EFR_REGISTER);
367 efr &= ~MOXA_MUST_EFR_BANK_MASK;
368 efr |= MOXA_MUST_EFR_BANK1;
370 outb(efr, info->ioaddr + MOXA_MUST_EFR_REGISTER);
371 outb((u8)info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTH_REGISTER);
372 outb((u8)info->rx_trigger, info->ioaddr + MOXA_MUST_RBRTI_REGISTER);
373 outb((u8)info->rx_low_water, info->ioaddr + MOXA_MUST_RBRTL_REGISTER);
374 outb(oldlcr, info->ioaddr + UART_LCR);
377 static void mxser_set_must_enum_value(unsigned long baseio, u8 value)
382 oldlcr = inb(baseio + UART_LCR);
383 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
385 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
386 efr &= ~MOXA_MUST_EFR_BANK_MASK;
387 efr |= MOXA_MUST_EFR_BANK2;
389 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
390 outb(value, baseio + MOXA_MUST_ENUM_REGISTER);
391 outb(oldlcr, baseio + UART_LCR);
395 static void mxser_get_must_hardware_id(unsigned long baseio, u8 *pId)
400 oldlcr = inb(baseio + UART_LCR);
401 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
403 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
404 efr &= ~MOXA_MUST_EFR_BANK_MASK;
405 efr |= MOXA_MUST_EFR_BANK2;
407 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
408 *pId = inb(baseio + MOXA_MUST_HWID_REGISTER);
409 outb(oldlcr, baseio + UART_LCR);
413 static void SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(unsigned long baseio)
418 oldlcr = inb(baseio + UART_LCR);
419 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
421 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
422 efr &= ~MOXA_MUST_EFR_SF_MASK;
424 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
425 outb(oldlcr, baseio + UART_LCR);
428 static void mxser_enable_must_tx_software_flow_control(unsigned long baseio)
433 oldlcr = inb(baseio + UART_LCR);
434 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
436 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
437 efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
438 efr |= MOXA_MUST_EFR_SF_TX1;
440 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
441 outb(oldlcr, baseio + UART_LCR);
444 static void mxser_disable_must_tx_software_flow_control(unsigned long baseio)
449 oldlcr = inb(baseio + UART_LCR);
450 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
452 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
453 efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
455 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
456 outb(oldlcr, baseio + UART_LCR);
459 static void mxser_enable_must_rx_software_flow_control(unsigned long baseio)
464 oldlcr = inb(baseio + UART_LCR);
465 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
467 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
468 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
469 efr |= MOXA_MUST_EFR_SF_RX1;
471 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
472 outb(oldlcr, baseio + UART_LCR);
475 static void mxser_disable_must_rx_software_flow_control(unsigned long baseio)
480 oldlcr = inb(baseio + UART_LCR);
481 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
483 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
484 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
486 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
487 outb(oldlcr, baseio + UART_LCR);
491 static int CheckIsMoxaMust(unsigned long io)
496 outb(0, io + UART_LCR);
497 mxser_disable_must_enchance_mode(io);
498 oldmcr = inb(io + UART_MCR);
499 outb(0, io + UART_MCR);
500 mxser_set_must_xon1_value(io, 0x11);
501 if ((hwid = inb(io + UART_MCR)) != 0) {
502 outb(oldmcr, io + UART_MCR);
503 return MOXA_OTHER_UART;
506 mxser_get_must_hardware_id(io, &hwid);
507 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
508 if (hwid == Gpci_uart_info[i].type)
511 return MOXA_OTHER_UART;
515 static void process_txrx_fifo(struct mxser_port *info)
519 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
520 info->rx_trigger = 1;
521 info->rx_high_water = 1;
522 info->rx_low_water = 1;
523 info->xmit_fifo_size = 1;
525 for (i = 0; i < UART_INFO_NUM; i++)
526 if (info->board->chip_flag == Gpci_uart_info[i].type) {
527 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
528 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
529 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
530 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
535 static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
537 static unsigned char mxser_msr[MXSER_PORTS + 1];
538 unsigned char status = 0;
540 status = inb(baseaddr + UART_MSR);
542 mxser_msr[port] &= 0x0F;
543 mxser_msr[port] |= status;
544 status = mxser_msr[port];
551 static int mxser_carrier_raised(struct tty_port *port)
553 struct mxser_port *mp = container_of(port, struct mxser_port, port);
554 return (inb(mp->ioaddr + UART_MSR) & UART_MSR_DCD)?1:0;
557 static void mxser_dtr_rts(struct tty_port *port, int on)
559 struct mxser_port *mp = container_of(port, struct mxser_port, port);
562 spin_lock_irqsave(&mp->slock, flags);
564 outb(inb(mp->ioaddr + UART_MCR) |
565 UART_MCR_DTR | UART_MCR_RTS, mp->ioaddr + UART_MCR);
567 outb(inb(mp->ioaddr + UART_MCR)&~(UART_MCR_DTR | UART_MCR_RTS),
568 mp->ioaddr + UART_MCR);
569 spin_unlock_irqrestore(&mp->slock, flags);
572 static int mxser_set_baud(struct tty_struct *tty, long newspd)
574 struct mxser_port *info = tty->driver_data;
575 unsigned int quot = 0, baud;
582 if (newspd > info->max_baud)
586 quot = 2 * info->baud_base / 269;
587 tty_encode_baud_rate(tty, 134, 134);
589 quot = info->baud_base / newspd;
592 baud = info->baud_base/quot;
593 tty_encode_baud_rate(tty, baud, baud);
599 * worst case (128 * 1000 * 10 * 18432) needs 35 bits, so divide in the
602 timeout = (u64)info->xmit_fifo_size * HZ * 10 * quot;
603 do_div(timeout, info->baud_base);
604 info->timeout = timeout + HZ / 50; /* Add .02 seconds of slop */
607 info->MCR |= UART_MCR_DTR;
608 outb(info->MCR, info->ioaddr + UART_MCR);
610 info->MCR &= ~UART_MCR_DTR;
611 outb(info->MCR, info->ioaddr + UART_MCR);
615 cval = inb(info->ioaddr + UART_LCR);
617 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
619 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
620 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
621 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
624 if (C_BAUD(tty) == BOTHER) {
625 quot = info->baud_base % newspd;
627 if (quot % newspd > newspd / 2) {
633 mxser_set_must_enum_value(info->ioaddr, quot);
636 mxser_set_must_enum_value(info->ioaddr, 0);
642 * This routine is called to set the UART divisor registers to match
643 * the specified baud rate for a serial port.
645 static int mxser_change_speed(struct tty_struct *tty,
646 struct ktermios *old_termios)
648 struct mxser_port *info = tty->driver_data;
649 unsigned cflag, cval, fcr;
651 unsigned char status;
653 cflag = tty->termios.c_cflag;
657 if (mxser_set_baud_method[tty->index] == 0)
658 mxser_set_baud(tty, tty_get_baud_rate(tty));
660 /* byte size and parity */
661 switch (cflag & CSIZE) {
676 break; /* too keep GCC shut... */
681 cval |= UART_LCR_PARITY;
682 if (!(cflag & PARODD))
683 cval |= UART_LCR_EPAR;
685 cval |= UART_LCR_SPAR;
687 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
688 if (info->board->chip_flag) {
689 fcr = UART_FCR_ENABLE_FIFO;
690 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
691 mxser_set_must_fifo_value(info);
695 fcr = UART_FCR_ENABLE_FIFO;
696 if (info->board->chip_flag) {
697 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
698 mxser_set_must_fifo_value(info);
700 switch (info->rx_trigger) {
702 fcr |= UART_FCR_TRIGGER_1;
705 fcr |= UART_FCR_TRIGGER_4;
708 fcr |= UART_FCR_TRIGGER_8;
711 fcr |= UART_FCR_TRIGGER_14;
717 /* CTS flow control flag and modem status interrupts */
718 info->IER &= ~UART_IER_MSI;
719 info->MCR &= ~UART_MCR_AFE;
720 tty_port_set_cts_flow(&info->port, cflag & CRTSCTS);
721 if (cflag & CRTSCTS) {
722 info->IER |= UART_IER_MSI;
723 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
724 info->MCR |= UART_MCR_AFE;
726 status = inb(info->ioaddr + UART_MSR);
727 if (tty->hw_stopped) {
728 if (status & UART_MSR_CTS) {
730 if (info->type != PORT_16550A &&
731 !info->board->chip_flag) {
732 outb(info->IER & ~UART_IER_THRI,
735 info->IER |= UART_IER_THRI;
736 outb(info->IER, info->ioaddr +
742 if (!(status & UART_MSR_CTS)) {
744 if ((info->type != PORT_16550A) &&
745 (!info->board->chip_flag)) {
746 info->IER &= ~UART_IER_THRI;
747 outb(info->IER, info->ioaddr +
754 outb(info->MCR, info->ioaddr + UART_MCR);
755 tty_port_set_check_carrier(&info->port, ~cflag & CLOCAL);
757 info->IER |= UART_IER_MSI;
758 outb(info->IER, info->ioaddr + UART_IER);
761 * Set up parity check flag
763 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
765 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
766 if (I_BRKINT(tty) || I_PARMRK(tty))
767 info->read_status_mask |= UART_LSR_BI;
769 info->ignore_status_mask = 0;
772 info->ignore_status_mask |= UART_LSR_BI;
773 info->read_status_mask |= UART_LSR_BI;
775 * If we're ignore parity and break indicators, ignore
776 * overruns too. (For real raw support).
779 info->ignore_status_mask |=
783 info->read_status_mask |=
789 if (info->board->chip_flag) {
790 mxser_set_must_xon1_value(info->ioaddr, START_CHAR(tty));
791 mxser_set_must_xoff1_value(info->ioaddr, STOP_CHAR(tty));
793 mxser_enable_must_rx_software_flow_control(
796 mxser_disable_must_rx_software_flow_control(
800 mxser_enable_must_tx_software_flow_control(
803 mxser_disable_must_tx_software_flow_control(
809 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
810 outb(cval, info->ioaddr + UART_LCR);
815 static void mxser_check_modem_status(struct tty_struct *tty,
816 struct mxser_port *port, int status)
818 /* update input line counters */
819 if (status & UART_MSR_TERI)
821 if (status & UART_MSR_DDSR)
823 if (status & UART_MSR_DDCD)
825 if (status & UART_MSR_DCTS)
827 port->mon_data.modem_status = status;
828 wake_up_interruptible(&port->port.delta_msr_wait);
830 if (tty_port_check_carrier(&port->port) && (status & UART_MSR_DDCD)) {
831 if (status & UART_MSR_DCD)
832 wake_up_interruptible(&port->port.open_wait);
835 if (tty_port_cts_enabled(&port->port)) {
836 if (tty->hw_stopped) {
837 if (status & UART_MSR_CTS) {
840 if ((port->type != PORT_16550A) &&
841 (!port->board->chip_flag)) {
842 outb(port->IER & ~UART_IER_THRI,
843 port->ioaddr + UART_IER);
844 port->IER |= UART_IER_THRI;
845 outb(port->IER, port->ioaddr +
851 if (!(status & UART_MSR_CTS)) {
853 if (port->type != PORT_16550A &&
854 !port->board->chip_flag) {
855 port->IER &= ~UART_IER_THRI;
856 outb(port->IER, port->ioaddr +
864 static int mxser_activate(struct tty_port *port, struct tty_struct *tty)
866 struct mxser_port *info = container_of(port, struct mxser_port, port);
871 page = __get_free_page(GFP_KERNEL);
875 spin_lock_irqsave(&info->slock, flags);
877 if (!info->ioaddr || !info->type) {
878 set_bit(TTY_IO_ERROR, &tty->flags);
879 spin_unlock_irqrestore(&info->slock, flags);
883 info->port.xmit_buf = (unsigned char *) page;
886 * Clear the FIFO buffers and disable them
887 * (they will be reenabled in mxser_change_speed())
889 if (info->board->chip_flag)
890 outb((UART_FCR_CLEAR_RCVR |
891 UART_FCR_CLEAR_XMIT |
892 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
894 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
895 info->ioaddr + UART_FCR);
898 * At this point there's no way the LSR could still be 0xFF;
899 * if it is, then bail out, because there's likely no UART
902 if (inb(info->ioaddr + UART_LSR) == 0xff) {
903 spin_unlock_irqrestore(&info->slock, flags);
904 if (capable(CAP_SYS_ADMIN)) {
905 set_bit(TTY_IO_ERROR, &tty->flags);
914 * Clear the interrupt registers.
916 (void) inb(info->ioaddr + UART_LSR);
917 (void) inb(info->ioaddr + UART_RX);
918 (void) inb(info->ioaddr + UART_IIR);
919 (void) inb(info->ioaddr + UART_MSR);
922 * Now, initialize the UART
924 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
925 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
926 outb(info->MCR, info->ioaddr + UART_MCR);
929 * Finally, enable interrupts
931 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
933 if (info->board->chip_flag)
934 info->IER |= MOXA_MUST_IER_EGDAI;
935 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
938 * And clear the interrupt registers again for luck.
940 (void) inb(info->ioaddr + UART_LSR);
941 (void) inb(info->ioaddr + UART_RX);
942 (void) inb(info->ioaddr + UART_IIR);
943 (void) inb(info->ioaddr + UART_MSR);
945 clear_bit(TTY_IO_ERROR, &tty->flags);
946 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
949 * and set the speed of the serial port
951 mxser_change_speed(tty, NULL);
952 spin_unlock_irqrestore(&info->slock, flags);
957 info->port.xmit_buf = NULL;
962 * This routine will shutdown a serial port
964 static void mxser_shutdown_port(struct tty_port *port)
966 struct mxser_port *info = container_of(port, struct mxser_port, port);
969 spin_lock_irqsave(&info->slock, flags);
972 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
973 * here so the queue might never be waken up
975 wake_up_interruptible(&info->port.delta_msr_wait);
978 * Free the xmit buffer, if necessary
980 if (info->port.xmit_buf) {
981 free_page((unsigned long) info->port.xmit_buf);
982 info->port.xmit_buf = NULL;
986 outb(0x00, info->ioaddr + UART_IER);
988 /* clear Rx/Tx FIFO's */
989 if (info->board->chip_flag)
990 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
991 MOXA_MUST_FCR_GDA_MODE_ENABLE,
992 info->ioaddr + UART_FCR);
994 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
995 info->ioaddr + UART_FCR);
997 /* read data port to reset things */
998 (void) inb(info->ioaddr + UART_RX);
1001 if (info->board->chip_flag)
1002 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
1004 spin_unlock_irqrestore(&info->slock, flags);
1008 * This routine is called whenever a serial port is opened. It
1009 * enables interrupts for a serial port, linking in its async structure into
1010 * the IRQ chain. It also performs the serial-specific
1011 * initialization for the tty structure.
1013 static int mxser_open(struct tty_struct *tty, struct file *filp)
1015 struct mxser_port *info;
1019 if (line == MXSER_PORTS)
1021 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
1025 tty->driver_data = info;
1026 return tty_port_open(&info->port, tty, filp);
1029 static void mxser_flush_buffer(struct tty_struct *tty)
1031 struct mxser_port *info = tty->driver_data;
1033 unsigned long flags;
1036 spin_lock_irqsave(&info->slock, flags);
1037 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1039 fcr = inb(info->ioaddr + UART_FCR);
1040 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1041 info->ioaddr + UART_FCR);
1042 outb(fcr, info->ioaddr + UART_FCR);
1044 spin_unlock_irqrestore(&info->slock, flags);
1050 static void mxser_close_port(struct tty_port *port)
1052 struct mxser_port *info = container_of(port, struct mxser_port, port);
1053 unsigned long timeout;
1055 * At this point we stop accepting input. To do this, we
1056 * disable the receive line status interrupts, and tell the
1057 * interrupt driver to stop checking the data ready bit in the
1058 * line status register.
1060 info->IER &= ~UART_IER_RLSI;
1061 if (info->board->chip_flag)
1062 info->IER &= ~MOXA_MUST_RECV_ISR;
1064 outb(info->IER, info->ioaddr + UART_IER);
1066 * Before we drop DTR, make sure the UART transmitter
1067 * has completely drained; this is especially
1068 * important if there is a transmit FIFO!
1070 timeout = jiffies + HZ;
1071 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
1072 schedule_timeout_interruptible(5);
1073 if (time_after(jiffies, timeout))
1079 * This routine is called when the serial port gets closed. First, we
1080 * wait for the last remaining data to be sent. Then, we unlink its
1081 * async structure from the interrupt chain if necessary, and we free
1082 * that IRQ if nothing is left in the chain.
1084 static void mxser_close(struct tty_struct *tty, struct file *filp)
1086 struct mxser_port *info = tty->driver_data;
1087 struct tty_port *port = &info->port;
1089 if (tty->index == MXSER_PORTS || info == NULL)
1091 if (tty_port_close_start(port, tty, filp) == 0)
1094 mutex_lock(&port->mutex);
1095 mxser_close_port(port);
1096 mxser_flush_buffer(tty);
1097 if (tty_port_initialized(port) && C_HUPCL(tty))
1098 tty_port_lower_dtr_rts(port);
1099 mxser_shutdown_port(port);
1100 tty_port_set_initialized(port, 0);
1101 mutex_unlock(&port->mutex);
1103 /* Right now the tty_port set is done outside of the close_end helper
1104 as we don't yet have everyone using refcounts */
1105 tty_port_close_end(port, tty);
1106 tty_port_tty_set(port, NULL);
1109 static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1112 struct mxser_port *info = tty->driver_data;
1113 unsigned long flags;
1115 if (!info->port.xmit_buf)
1119 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1120 SERIAL_XMIT_SIZE - info->xmit_head));
1124 memcpy(info->port.xmit_buf + info->xmit_head, buf, c);
1125 spin_lock_irqsave(&info->slock, flags);
1126 info->xmit_head = (info->xmit_head + c) &
1127 (SERIAL_XMIT_SIZE - 1);
1128 info->xmit_cnt += c;
1129 spin_unlock_irqrestore(&info->slock, flags);
1136 if (info->xmit_cnt && !tty->stopped) {
1137 if (!tty->hw_stopped ||
1138 (info->type == PORT_16550A) ||
1139 (info->board->chip_flag)) {
1140 spin_lock_irqsave(&info->slock, flags);
1141 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1143 info->IER |= UART_IER_THRI;
1144 outb(info->IER, info->ioaddr + UART_IER);
1145 spin_unlock_irqrestore(&info->slock, flags);
1151 static int mxser_put_char(struct tty_struct *tty, unsigned char ch)
1153 struct mxser_port *info = tty->driver_data;
1154 unsigned long flags;
1156 if (!info->port.xmit_buf)
1159 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1162 spin_lock_irqsave(&info->slock, flags);
1163 info->port.xmit_buf[info->xmit_head++] = ch;
1164 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1166 spin_unlock_irqrestore(&info->slock, flags);
1167 if (!tty->stopped) {
1168 if (!tty->hw_stopped ||
1169 (info->type == PORT_16550A) ||
1170 info->board->chip_flag) {
1171 spin_lock_irqsave(&info->slock, flags);
1172 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1173 info->IER |= UART_IER_THRI;
1174 outb(info->IER, info->ioaddr + UART_IER);
1175 spin_unlock_irqrestore(&info->slock, flags);
1182 static void mxser_flush_chars(struct tty_struct *tty)
1184 struct mxser_port *info = tty->driver_data;
1185 unsigned long flags;
1187 if (info->xmit_cnt <= 0 || tty->stopped || !info->port.xmit_buf ||
1188 (tty->hw_stopped && info->type != PORT_16550A &&
1189 !info->board->chip_flag))
1192 spin_lock_irqsave(&info->slock, flags);
1194 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1195 info->IER |= UART_IER_THRI;
1196 outb(info->IER, info->ioaddr + UART_IER);
1198 spin_unlock_irqrestore(&info->slock, flags);
1201 static int mxser_write_room(struct tty_struct *tty)
1203 struct mxser_port *info = tty->driver_data;
1206 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1207 return ret < 0 ? 0 : ret;
1210 static int mxser_chars_in_buffer(struct tty_struct *tty)
1212 struct mxser_port *info = tty->driver_data;
1213 return info->xmit_cnt;
1217 * ------------------------------------------------------------
1218 * friends of mxser_ioctl()
1219 * ------------------------------------------------------------
1221 static int mxser_get_serial_info(struct tty_struct *tty,
1222 struct serial_struct __user *retinfo)
1224 struct mxser_port *info = tty->driver_data;
1225 struct serial_struct tmp = {
1228 .port = info->ioaddr,
1229 .irq = info->board->irq,
1230 .flags = info->port.flags,
1231 .baud_base = info->baud_base,
1232 .close_delay = info->port.close_delay,
1233 .closing_wait = info->port.closing_wait,
1234 .custom_divisor = info->custom_divisor,
1236 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1241 static int mxser_set_serial_info(struct tty_struct *tty,
1242 struct serial_struct __user *new_info)
1244 struct mxser_port *info = tty->driver_data;
1245 struct tty_port *port = &info->port;
1246 struct serial_struct new_serial;
1248 unsigned long sl_flags;
1252 if (!new_info || !info->ioaddr)
1254 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1257 if (new_serial.irq != info->board->irq ||
1258 new_serial.port != info->ioaddr)
1261 flags = port->flags & ASYNC_SPD_MASK;
1263 if (!capable(CAP_SYS_ADMIN)) {
1264 if ((new_serial.baud_base != info->baud_base) ||
1265 (new_serial.close_delay != info->port.close_delay) ||
1266 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->port.flags & ~ASYNC_USR_MASK)))
1268 info->port.flags = ((info->port.flags & ~ASYNC_USR_MASK) |
1269 (new_serial.flags & ASYNC_USR_MASK));
1272 * OK, past this point, all the error checking has been done.
1273 * At this point, we start making changes.....
1275 port->flags = ((port->flags & ~ASYNC_FLAGS) |
1276 (new_serial.flags & ASYNC_FLAGS));
1277 port->close_delay = new_serial.close_delay * HZ / 100;
1278 port->closing_wait = new_serial.closing_wait * HZ / 100;
1279 port->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1280 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
1281 (new_serial.baud_base != info->baud_base ||
1282 new_serial.custom_divisor !=
1283 info->custom_divisor)) {
1284 if (new_serial.custom_divisor == 0)
1286 baud = new_serial.baud_base / new_serial.custom_divisor;
1287 tty_encode_baud_rate(tty, baud, baud);
1291 info->type = new_serial.type;
1293 process_txrx_fifo(info);
1295 if (tty_port_initialized(port)) {
1296 if (flags != (port->flags & ASYNC_SPD_MASK)) {
1297 spin_lock_irqsave(&info->slock, sl_flags);
1298 mxser_change_speed(tty, NULL);
1299 spin_unlock_irqrestore(&info->slock, sl_flags);
1302 retval = mxser_activate(port, tty);
1304 tty_port_set_initialized(port, 1);
1310 * mxser_get_lsr_info - get line status register info
1312 * Purpose: Let user call ioctl() to get info when the UART physically
1313 * is emptied. On bus types like RS485, the transmitter must
1314 * release the bus after transmitting. This must be done when
1315 * the transmit shift register is empty, not be done when the
1316 * transmit holding register is empty. This functionality
1317 * allows an RS485 driver to be written in user space.
1319 static int mxser_get_lsr_info(struct mxser_port *info,
1320 unsigned int __user *value)
1322 unsigned char status;
1323 unsigned int result;
1324 unsigned long flags;
1326 spin_lock_irqsave(&info->slock, flags);
1327 status = inb(info->ioaddr + UART_LSR);
1328 spin_unlock_irqrestore(&info->slock, flags);
1329 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1330 return put_user(result, value);
1333 static int mxser_tiocmget(struct tty_struct *tty)
1335 struct mxser_port *info = tty->driver_data;
1336 unsigned char control, status;
1337 unsigned long flags;
1340 if (tty->index == MXSER_PORTS)
1341 return -ENOIOCTLCMD;
1342 if (tty_io_error(tty))
1345 control = info->MCR;
1347 spin_lock_irqsave(&info->slock, flags);
1348 status = inb(info->ioaddr + UART_MSR);
1349 if (status & UART_MSR_ANY_DELTA)
1350 mxser_check_modem_status(tty, info, status);
1351 spin_unlock_irqrestore(&info->slock, flags);
1352 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1353 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1354 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1355 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1356 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1357 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1360 static int mxser_tiocmset(struct tty_struct *tty,
1361 unsigned int set, unsigned int clear)
1363 struct mxser_port *info = tty->driver_data;
1364 unsigned long flags;
1367 if (tty->index == MXSER_PORTS)
1368 return -ENOIOCTLCMD;
1369 if (tty_io_error(tty))
1372 spin_lock_irqsave(&info->slock, flags);
1374 if (set & TIOCM_RTS)
1375 info->MCR |= UART_MCR_RTS;
1376 if (set & TIOCM_DTR)
1377 info->MCR |= UART_MCR_DTR;
1379 if (clear & TIOCM_RTS)
1380 info->MCR &= ~UART_MCR_RTS;
1381 if (clear & TIOCM_DTR)
1382 info->MCR &= ~UART_MCR_DTR;
1384 outb(info->MCR, info->ioaddr + UART_MCR);
1385 spin_unlock_irqrestore(&info->slock, flags);
1389 static int __init mxser_program_mode(int port)
1401 id = inb(port + 1) & 0x1F;
1402 if ((id != C168_ASIC_ID) &&
1403 (id != C104_ASIC_ID) &&
1404 (id != C102_ASIC_ID) &&
1405 (id != CI132_ASIC_ID) &&
1406 (id != CI134_ASIC_ID) &&
1407 (id != CI104J_ASIC_ID))
1409 for (i = 0, j = 0; i < 4; i++) {
1413 } else if ((j == 1) && (n == 1)) {
1424 static void __init mxser_normal_mode(int port)
1428 outb(0xA5, port + 1);
1429 outb(0x80, port + 3);
1430 outb(12, port + 0); /* 9600 bps */
1432 outb(0x03, port + 3); /* 8 data bits */
1433 outb(0x13, port + 4); /* loop back mode */
1434 for (i = 0; i < 16; i++) {
1436 if ((n & 0x61) == 0x60)
1441 outb(0x00, port + 4);
1444 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1445 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1446 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1447 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1448 #define EN_CCMD 0x000 /* Chip's command register */
1449 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1450 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1451 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1452 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1453 #define EN0_DCFG 0x00E /* Data configuration reg WR */
1454 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1455 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1456 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1457 static int __init mxser_read_register(int port, unsigned short *regs)
1459 int i, k, value, id;
1462 id = mxser_program_mode(port);
1465 for (i = 0; i < 14; i++) {
1466 k = (i & 0x3F) | 0x180;
1467 for (j = 0x100; j > 0; j >>= 1) {
1468 outb(CHIP_CS, port);
1470 outb(CHIP_CS | CHIP_DO, port);
1471 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1473 outb(CHIP_CS, port);
1474 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1479 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1480 outb(CHIP_CS, port);
1481 outb(CHIP_CS | CHIP_SK, port);
1482 if (inb(port) & CHIP_DI)
1488 mxser_normal_mode(port);
1492 static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1494 struct mxser_port *ip;
1495 struct tty_port *port;
1496 struct tty_struct *tty;
1502 case MOXA_GET_MAJOR:
1503 printk_ratelimited(KERN_WARNING "mxser: '%s' uses deprecated ioctl "
1504 "%x (GET_MAJOR), fix your userspace\n",
1505 current->comm, cmd);
1506 return put_user(ttymajor, (int __user *)argp);
1508 case MOXA_CHKPORTENABLE:
1510 for (i = 0; i < MXSER_BOARDS; i++)
1511 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1512 if (mxser_boards[i].ports[j].ioaddr)
1514 return put_user(result, (unsigned long __user *)argp);
1515 case MOXA_GETDATACOUNT:
1516 /* The receive side is locked by port->slock but it isn't
1517 clear that an exact snapshot is worth copying here */
1518 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1521 case MOXA_GETMSTATUS: {
1522 struct mxser_mstatus ms, __user *msu = argp;
1523 for (i = 0; i < MXSER_BOARDS; i++)
1524 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1525 ip = &mxser_boards[i].ports[j];
1527 memset(&ms, 0, sizeof(ms));
1529 mutex_lock(&port->mutex);
1533 tty = tty_port_tty_get(port);
1536 ms.cflag = ip->normal_termios.c_cflag;
1538 ms.cflag = tty->termios.c_cflag;
1540 spin_lock_irq(&ip->slock);
1541 status = inb(ip->ioaddr + UART_MSR);
1542 spin_unlock_irq(&ip->slock);
1543 if (status & UART_MSR_DCD)
1545 if (status & UART_MSR_DSR)
1547 if (status & UART_MSR_CTS)
1550 mutex_unlock(&port->mutex);
1551 if (copy_to_user(msu, &ms, sizeof(ms)))
1557 case MOXA_ASPP_MON_EXT: {
1558 struct mxser_mon_ext *me; /* it's 2k, stack unfriendly */
1559 unsigned int cflag, iflag, p;
1562 me = kzalloc(sizeof(*me), GFP_KERNEL);
1566 for (i = 0, p = 0; i < MXSER_BOARDS; i++) {
1567 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++, p++) {
1568 if (p >= ARRAY_SIZE(me->rx_cnt)) {
1572 ip = &mxser_boards[i].ports[j];
1575 mutex_lock(&port->mutex);
1577 mutex_unlock(&port->mutex);
1581 spin_lock_irq(&ip->slock);
1582 status = mxser_get_msr(ip->ioaddr, 0, p);
1584 if (status & UART_MSR_TERI)
1586 if (status & UART_MSR_DDSR)
1588 if (status & UART_MSR_DDCD)
1590 if (status & UART_MSR_DCTS)
1593 ip->mon_data.modem_status = status;
1594 me->rx_cnt[p] = ip->mon_data.rxcnt;
1595 me->tx_cnt[p] = ip->mon_data.txcnt;
1596 me->up_rxcnt[p] = ip->mon_data.up_rxcnt;
1597 me->up_txcnt[p] = ip->mon_data.up_txcnt;
1598 me->modem_status[p] =
1599 ip->mon_data.modem_status;
1600 spin_unlock_irq(&ip->slock);
1602 tty = tty_port_tty_get(&ip->port);
1605 cflag = ip->normal_termios.c_cflag;
1606 iflag = ip->normal_termios.c_iflag;
1607 me->baudrate[p] = tty_termios_baud_rate(&ip->normal_termios);
1609 cflag = tty->termios.c_cflag;
1610 iflag = tty->termios.c_iflag;
1611 me->baudrate[p] = tty_get_baud_rate(tty);
1615 me->databits[p] = cflag & CSIZE;
1616 me->stopbits[p] = cflag & CSTOPB;
1617 me->parity[p] = cflag & (PARENB | PARODD |
1620 if (cflag & CRTSCTS)
1621 me->flowctrl[p] |= 0x03;
1623 if (iflag & (IXON | IXOFF))
1624 me->flowctrl[p] |= 0x0C;
1626 if (ip->type == PORT_16550A)
1629 if (ip->board->chip_flag == MOXA_MUST_MU860_HWID) {
1630 opmode = inb(ip->opmode_ioaddr)>>((p % 4) * 2);
1631 opmode &= OP_MODE_MASK;
1633 opmode = RS232_MODE;
1635 me->iftype[p] = opmode;
1636 mutex_unlock(&port->mutex);
1639 if (copy_to_user(argp, me, sizeof(*me)))
1645 return -ENOIOCTLCMD;
1650 static int mxser_cflags_changed(struct mxser_port *info, unsigned long arg,
1651 struct async_icount *cprev)
1653 struct async_icount cnow;
1654 unsigned long flags;
1657 spin_lock_irqsave(&info->slock, flags);
1658 cnow = info->icount; /* atomic copy */
1659 spin_unlock_irqrestore(&info->slock, flags);
1661 ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1662 ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1663 ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1664 ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
1671 static int mxser_ioctl(struct tty_struct *tty,
1672 unsigned int cmd, unsigned long arg)
1674 struct mxser_port *info = tty->driver_data;
1675 struct tty_port *port = &info->port;
1676 struct async_icount cnow;
1677 unsigned long flags;
1678 void __user *argp = (void __user *)arg;
1681 if (tty->index == MXSER_PORTS)
1682 return mxser_ioctl_special(cmd, argp);
1684 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1686 unsigned long opmode;
1687 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1689 unsigned char val, mask;
1691 if (info->board->chip_flag != MOXA_MUST_MU860_HWID)
1695 if (cmd == MOXA_SET_OP_MODE) {
1696 if (get_user(opmode, (int __user *) argp))
1698 if (opmode != RS232_MODE &&
1699 opmode != RS485_2WIRE_MODE &&
1700 opmode != RS422_MODE &&
1701 opmode != RS485_4WIRE_MODE)
1705 spin_lock_irq(&info->slock);
1706 val = inb(info->opmode_ioaddr);
1708 val |= (opmode << shiftbit);
1709 outb(val, info->opmode_ioaddr);
1710 spin_unlock_irq(&info->slock);
1713 spin_lock_irq(&info->slock);
1714 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1715 spin_unlock_irq(&info->slock);
1716 opmode &= OP_MODE_MASK;
1717 if (put_user(opmode, (int __user *)argp))
1723 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && tty_io_error(tty))
1728 mutex_lock(&port->mutex);
1729 retval = mxser_get_serial_info(tty, argp);
1730 mutex_unlock(&port->mutex);
1733 mutex_lock(&port->mutex);
1734 retval = mxser_set_serial_info(tty, argp);
1735 mutex_unlock(&port->mutex);
1737 case TIOCSERGETLSR: /* Get line status register */
1738 return mxser_get_lsr_info(info, argp);
1740 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1741 * - mask passed in arg for lines of interest
1742 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1743 * Caller should use TIOCGICOUNT to see which one it was
1746 spin_lock_irqsave(&info->slock, flags);
1747 cnow = info->icount; /* note the counters on entry */
1748 spin_unlock_irqrestore(&info->slock, flags);
1750 return wait_event_interruptible(info->port.delta_msr_wait,
1751 mxser_cflags_changed(info, arg, &cnow));
1752 case MOXA_HighSpeedOn:
1753 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1754 case MOXA_SDS_RSTICOUNTER:
1755 spin_lock_irq(&info->slock);
1756 info->mon_data.rxcnt = 0;
1757 info->mon_data.txcnt = 0;
1758 spin_unlock_irq(&info->slock);
1761 case MOXA_ASPP_OQUEUE:{
1764 len = mxser_chars_in_buffer(tty);
1765 spin_lock_irq(&info->slock);
1766 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_THRE;
1767 spin_unlock_irq(&info->slock);
1768 len += (lsr ? 0 : 1);
1770 return put_user(len, (int __user *)argp);
1772 case MOXA_ASPP_MON: {
1775 spin_lock_irq(&info->slock);
1776 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1777 mxser_check_modem_status(tty, info, status);
1779 mcr = inb(info->ioaddr + UART_MCR);
1780 spin_unlock_irq(&info->slock);
1782 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1783 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1785 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1787 if (mcr & MOXA_MUST_MCR_TX_XON)
1788 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1790 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1792 if (tty->hw_stopped)
1793 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1795 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1797 if (copy_to_user(argp, &info->mon_data,
1798 sizeof(struct mxser_mon)))
1803 case MOXA_ASPP_LSTATUS: {
1804 if (put_user(info->err_shadow, (unsigned char __user *)argp))
1807 info->err_shadow = 0;
1810 case MOXA_SET_BAUD_METHOD: {
1813 if (get_user(method, (int __user *)argp))
1815 mxser_set_baud_method[tty->index] = method;
1816 return put_user(method, (int __user *)argp);
1819 return -ENOIOCTLCMD;
1825 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1826 * Return: write counters to the user passed counter struct
1827 * NB: both 1->0 and 0->1 transitions are counted except for
1828 * RI where only 0->1 is counted.
1831 static int mxser_get_icount(struct tty_struct *tty,
1832 struct serial_icounter_struct *icount)
1835 struct mxser_port *info = tty->driver_data;
1836 struct async_icount cnow;
1837 unsigned long flags;
1839 spin_lock_irqsave(&info->slock, flags);
1840 cnow = info->icount;
1841 spin_unlock_irqrestore(&info->slock, flags);
1843 icount->frame = cnow.frame;
1844 icount->brk = cnow.brk;
1845 icount->overrun = cnow.overrun;
1846 icount->buf_overrun = cnow.buf_overrun;
1847 icount->parity = cnow.parity;
1848 icount->rx = cnow.rx;
1849 icount->tx = cnow.tx;
1850 icount->cts = cnow.cts;
1851 icount->dsr = cnow.dsr;
1852 icount->rng = cnow.rng;
1853 icount->dcd = cnow.dcd;
1857 static void mxser_stoprx(struct tty_struct *tty)
1859 struct mxser_port *info = tty->driver_data;
1861 info->ldisc_stop_rx = 1;
1863 if (info->board->chip_flag) {
1864 info->IER &= ~MOXA_MUST_RECV_ISR;
1865 outb(info->IER, info->ioaddr + UART_IER);
1867 info->x_char = STOP_CHAR(tty);
1868 outb(0, info->ioaddr + UART_IER);
1869 info->IER |= UART_IER_THRI;
1870 outb(info->IER, info->ioaddr + UART_IER);
1874 if (C_CRTSCTS(tty)) {
1875 info->MCR &= ~UART_MCR_RTS;
1876 outb(info->MCR, info->ioaddr + UART_MCR);
1881 * This routine is called by the upper-layer tty layer to signal that
1882 * incoming characters should be throttled.
1884 static void mxser_throttle(struct tty_struct *tty)
1889 static void mxser_unthrottle(struct tty_struct *tty)
1891 struct mxser_port *info = tty->driver_data;
1894 info->ldisc_stop_rx = 0;
1899 if (info->board->chip_flag) {
1900 info->IER |= MOXA_MUST_RECV_ISR;
1901 outb(info->IER, info->ioaddr + UART_IER);
1903 info->x_char = START_CHAR(tty);
1904 outb(0, info->ioaddr + UART_IER);
1905 info->IER |= UART_IER_THRI;
1906 outb(info->IER, info->ioaddr + UART_IER);
1911 if (C_CRTSCTS(tty)) {
1912 info->MCR |= UART_MCR_RTS;
1913 outb(info->MCR, info->ioaddr + UART_MCR);
1918 * mxser_stop() and mxser_start()
1920 * This routines are called before setting or resetting tty->stopped.
1921 * They enable or disable transmitter interrupts, as necessary.
1923 static void mxser_stop(struct tty_struct *tty)
1925 struct mxser_port *info = tty->driver_data;
1926 unsigned long flags;
1928 spin_lock_irqsave(&info->slock, flags);
1929 if (info->IER & UART_IER_THRI) {
1930 info->IER &= ~UART_IER_THRI;
1931 outb(info->IER, info->ioaddr + UART_IER);
1933 spin_unlock_irqrestore(&info->slock, flags);
1936 static void mxser_start(struct tty_struct *tty)
1938 struct mxser_port *info = tty->driver_data;
1939 unsigned long flags;
1941 spin_lock_irqsave(&info->slock, flags);
1942 if (info->xmit_cnt && info->port.xmit_buf) {
1943 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1944 info->IER |= UART_IER_THRI;
1945 outb(info->IER, info->ioaddr + UART_IER);
1947 spin_unlock_irqrestore(&info->slock, flags);
1950 static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1952 struct mxser_port *info = tty->driver_data;
1953 unsigned long flags;
1955 spin_lock_irqsave(&info->slock, flags);
1956 mxser_change_speed(tty, old_termios);
1957 spin_unlock_irqrestore(&info->slock, flags);
1959 if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty)) {
1960 tty->hw_stopped = 0;
1964 /* Handle sw stopped */
1965 if ((old_termios->c_iflag & IXON) && !I_IXON(tty)) {
1968 if (info->board->chip_flag) {
1969 spin_lock_irqsave(&info->slock, flags);
1970 mxser_disable_must_rx_software_flow_control(
1972 spin_unlock_irqrestore(&info->slock, flags);
1980 * mxser_wait_until_sent() --- wait until the transmitter is empty
1982 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1984 struct mxser_port *info = tty->driver_data;
1985 unsigned long orig_jiffies, char_time;
1986 unsigned long flags;
1989 if (info->type == PORT_UNKNOWN)
1992 if (info->xmit_fifo_size == 0)
1993 return; /* Just in case.... */
1995 orig_jiffies = jiffies;
1997 * Set the check interval to be 1/5 of the estimated time to
1998 * send a single character, and make it at least 1. The check
1999 * interval should also be less than the timeout.
2001 * Note: we have to use pretty tight timings here to satisfy
2004 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2005 char_time = char_time / 5;
2008 if (timeout && timeout < char_time)
2009 char_time = timeout;
2011 * If the transmitter hasn't cleared in twice the approximate
2012 * amount of time to send the entire FIFO, it probably won't
2013 * ever clear. This assumes the UART isn't doing flow
2014 * control, which is currently the case. Hence, if it ever
2015 * takes longer than info->timeout, this is probably due to a
2016 * UART bug of some kind. So, we clamp the timeout parameter at
2019 if (!timeout || timeout > 2 * info->timeout)
2020 timeout = 2 * info->timeout;
2022 spin_lock_irqsave(&info->slock, flags);
2023 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
2024 spin_unlock_irqrestore(&info->slock, flags);
2025 schedule_timeout_interruptible(char_time);
2026 spin_lock_irqsave(&info->slock, flags);
2027 if (signal_pending(current))
2029 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2032 spin_unlock_irqrestore(&info->slock, flags);
2033 set_current_state(TASK_RUNNING);
2037 * This routine is called by tty_hangup() when a hangup is signaled.
2039 static void mxser_hangup(struct tty_struct *tty)
2041 struct mxser_port *info = tty->driver_data;
2043 mxser_flush_buffer(tty);
2044 tty_port_hangup(&info->port);
2048 * mxser_rs_break() --- routine which turns the break handling on or off
2050 static int mxser_rs_break(struct tty_struct *tty, int break_state)
2052 struct mxser_port *info = tty->driver_data;
2053 unsigned long flags;
2055 spin_lock_irqsave(&info->slock, flags);
2056 if (break_state == -1)
2057 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2058 info->ioaddr + UART_LCR);
2060 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2061 info->ioaddr + UART_LCR);
2062 spin_unlock_irqrestore(&info->slock, flags);
2066 static void mxser_receive_chars(struct tty_struct *tty,
2067 struct mxser_port *port, int *status)
2069 unsigned char ch, gdl;
2075 recv_room = tty->receive_room;
2076 if (recv_room == 0 && !port->ldisc_stop_rx)
2078 if (port->board->chip_flag != MOXA_OTHER_UART) {
2080 if (*status & UART_LSR_SPECIAL)
2082 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
2083 (*status & MOXA_MUST_LSR_RERR))
2085 if (*status & MOXA_MUST_LSR_RERR)
2088 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
2090 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
2091 gdl &= MOXA_MUST_GDL_MASK;
2092 if (gdl >= recv_room) {
2093 if (!port->ldisc_stop_rx)
2097 ch = inb(port->ioaddr + UART_RX);
2098 tty_insert_flip_char(&port->port, ch, 0);
2109 ch = inb(port->ioaddr + UART_RX);
2110 if (port->board->chip_flag && (*status & UART_LSR_OE))
2111 outb(0x23, port->ioaddr + UART_FCR);
2112 *status &= port->read_status_mask;
2113 if (*status & port->ignore_status_mask) {
2114 if (++ignored > 100)
2118 if (*status & UART_LSR_SPECIAL) {
2119 if (*status & UART_LSR_BI) {
2123 if (port->port.flags & ASYNC_SAK)
2125 } else if (*status & UART_LSR_PE) {
2127 port->icount.parity++;
2128 } else if (*status & UART_LSR_FE) {
2130 port->icount.frame++;
2131 } else if (*status & UART_LSR_OE) {
2133 port->icount.overrun++;
2137 tty_insert_flip_char(&port->port, ch, flag);
2139 if (cnt >= recv_room) {
2140 if (!port->ldisc_stop_rx)
2147 if (port->board->chip_flag)
2150 *status = inb(port->ioaddr + UART_LSR);
2151 } while (*status & UART_LSR_DR);
2154 mxvar_log.rxcnt[tty->index] += cnt;
2155 port->mon_data.rxcnt += cnt;
2156 port->mon_data.up_rxcnt += cnt;
2159 * We are called from an interrupt context with &port->slock
2160 * being held. Drop it temporarily in order to prevent
2161 * recursive locking.
2163 spin_unlock(&port->slock);
2164 tty_flip_buffer_push(&port->port);
2165 spin_lock(&port->slock);
2168 static void mxser_transmit_chars(struct tty_struct *tty, struct mxser_port *port)
2173 outb(port->x_char, port->ioaddr + UART_TX);
2175 mxvar_log.txcnt[tty->index]++;
2176 port->mon_data.txcnt++;
2177 port->mon_data.up_txcnt++;
2182 if (port->port.xmit_buf == NULL)
2185 if (port->xmit_cnt <= 0 || tty->stopped ||
2187 (port->type != PORT_16550A) &&
2188 (!port->board->chip_flag))) {
2189 port->IER &= ~UART_IER_THRI;
2190 outb(port->IER, port->ioaddr + UART_IER);
2194 cnt = port->xmit_cnt;
2195 count = port->xmit_fifo_size;
2197 outb(port->port.xmit_buf[port->xmit_tail++],
2198 port->ioaddr + UART_TX);
2199 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2200 if (--port->xmit_cnt <= 0)
2202 } while (--count > 0);
2203 mxvar_log.txcnt[tty->index] += (cnt - port->xmit_cnt);
2205 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2206 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2207 port->icount.tx += (cnt - port->xmit_cnt);
2209 if (port->xmit_cnt < WAKEUP_CHARS)
2212 if (port->xmit_cnt <= 0) {
2213 port->IER &= ~UART_IER_THRI;
2214 outb(port->IER, port->ioaddr + UART_IER);
2219 * This is the serial driver's generic interrupt routine
2221 static irqreturn_t mxser_interrupt(int irq, void *dev_id)
2224 struct mxser_board *brd = NULL;
2225 struct mxser_port *port;
2226 int max, irqbits, bits, msr;
2227 unsigned int int_cnt, pass_counter = 0;
2228 int handled = IRQ_NONE;
2229 struct tty_struct *tty;
2231 for (i = 0; i < MXSER_BOARDS; i++)
2232 if (dev_id == &mxser_boards[i]) {
2237 if (i == MXSER_BOARDS)
2241 max = brd->info->nports;
2242 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
2243 irqbits = inb(brd->vector) & brd->vector_mask;
2244 if (irqbits == brd->vector_mask)
2247 handled = IRQ_HANDLED;
2248 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2249 if (irqbits == brd->vector_mask)
2253 port = &brd->ports[i];
2256 spin_lock(&port->slock);
2258 iir = inb(port->ioaddr + UART_IIR);
2259 if (iir & UART_IIR_NO_INT)
2261 iir &= MOXA_MUST_IIR_MASK;
2262 tty = tty_port_tty_get(&port->port);
2263 if (!tty || port->closing ||
2264 !tty_port_initialized(&port->port)) {
2265 status = inb(port->ioaddr + UART_LSR);
2266 outb(0x27, port->ioaddr + UART_FCR);
2267 inb(port->ioaddr + UART_MSR);
2272 status = inb(port->ioaddr + UART_LSR);
2274 if (status & UART_LSR_PE)
2275 port->err_shadow |= NPPI_NOTIFY_PARITY;
2276 if (status & UART_LSR_FE)
2277 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2278 if (status & UART_LSR_OE)
2280 NPPI_NOTIFY_HW_OVERRUN;
2281 if (status & UART_LSR_BI)
2282 port->err_shadow |= NPPI_NOTIFY_BREAK;
2284 if (port->board->chip_flag) {
2285 if (iir == MOXA_MUST_IIR_GDA ||
2286 iir == MOXA_MUST_IIR_RDA ||
2287 iir == MOXA_MUST_IIR_RTO ||
2288 iir == MOXA_MUST_IIR_LSR)
2289 mxser_receive_chars(tty, port,
2293 status &= port->read_status_mask;
2294 if (status & UART_LSR_DR)
2295 mxser_receive_chars(tty, port,
2298 msr = inb(port->ioaddr + UART_MSR);
2299 if (msr & UART_MSR_ANY_DELTA)
2300 mxser_check_modem_status(tty, port, msr);
2302 if (port->board->chip_flag) {
2303 if (iir == 0x02 && (status &
2305 mxser_transmit_chars(tty, port);
2307 if (status & UART_LSR_THRE)
2308 mxser_transmit_chars(tty, port);
2311 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2312 spin_unlock(&port->slock);
2320 static const struct tty_operations mxser_ops = {
2322 .close = mxser_close,
2323 .write = mxser_write,
2324 .put_char = mxser_put_char,
2325 .flush_chars = mxser_flush_chars,
2326 .write_room = mxser_write_room,
2327 .chars_in_buffer = mxser_chars_in_buffer,
2328 .flush_buffer = mxser_flush_buffer,
2329 .ioctl = mxser_ioctl,
2330 .throttle = mxser_throttle,
2331 .unthrottle = mxser_unthrottle,
2332 .set_termios = mxser_set_termios,
2334 .start = mxser_start,
2335 .hangup = mxser_hangup,
2336 .break_ctl = mxser_rs_break,
2337 .wait_until_sent = mxser_wait_until_sent,
2338 .tiocmget = mxser_tiocmget,
2339 .tiocmset = mxser_tiocmset,
2340 .get_icount = mxser_get_icount,
2343 static const struct tty_port_operations mxser_port_ops = {
2344 .carrier_raised = mxser_carrier_raised,
2345 .dtr_rts = mxser_dtr_rts,
2346 .activate = mxser_activate,
2347 .shutdown = mxser_shutdown_port,
2351 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2354 static bool allow_overlapping_vector;
2355 module_param(allow_overlapping_vector, bool, S_IRUGO);
2356 MODULE_PARM_DESC(allow_overlapping_vector, "whether we allow ISA cards to be configured such that vector overlabs IO ports (default=no)");
2358 static bool mxser_overlapping_vector(struct mxser_board *brd)
2360 return allow_overlapping_vector &&
2361 brd->vector >= brd->ports[0].ioaddr &&
2362 brd->vector < brd->ports[0].ioaddr + 8 * brd->info->nports;
2365 static int mxser_request_vector(struct mxser_board *brd)
2367 if (mxser_overlapping_vector(brd))
2369 return request_region(brd->vector, 1, "mxser(vector)") ? 0 : -EIO;
2372 static void mxser_release_vector(struct mxser_board *brd)
2374 if (mxser_overlapping_vector(brd))
2376 release_region(brd->vector, 1);
2379 static void mxser_release_ISA_res(struct mxser_board *brd)
2381 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2382 mxser_release_vector(brd);
2385 static int mxser_initbrd(struct mxser_board *brd,
2386 struct pci_dev *pdev)
2388 struct mxser_port *info;
2392 printk(KERN_INFO "mxser: max. baud rate = %d bps\n",
2393 brd->ports[0].max_baud);
2395 for (i = 0; i < brd->info->nports; i++) {
2396 info = &brd->ports[i];
2397 tty_port_init(&info->port);
2398 info->port.ops = &mxser_port_ops;
2401 info->ldisc_stop_rx = 0;
2403 /* Enhance mode enabled here */
2404 if (brd->chip_flag != MOXA_OTHER_UART)
2405 mxser_enable_must_enchance_mode(info->ioaddr);
2407 info->type = brd->uart_type;
2409 process_txrx_fifo(info);
2411 info->custom_divisor = info->baud_base * 16;
2412 info->port.close_delay = 5 * HZ / 10;
2413 info->port.closing_wait = 30 * HZ;
2414 info->normal_termios = mxvar_sdriver->init_termios;
2415 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2416 info->err_shadow = 0;
2417 spin_lock_init(&info->slock);
2419 /* before set INT ISR, disable all int */
2420 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2421 info->ioaddr + UART_IER);
2424 retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
2427 for (i = 0; i < brd->info->nports; i++)
2428 tty_port_destroy(&brd->ports[i].port);
2429 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2430 "conflict with another device.\n",
2431 brd->info->name, brd->irq);
2437 static void mxser_board_remove(struct mxser_board *brd)
2441 for (i = 0; i < brd->info->nports; i++) {
2442 tty_unregister_device(mxvar_sdriver, brd->idx + i);
2443 tty_port_destroy(&brd->ports[i].port);
2445 free_irq(brd->irq, brd);
2448 static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
2450 int id, i, bits, ret;
2451 unsigned short regs[16], irq;
2452 unsigned char scratch, scratch2;
2454 brd->chip_flag = MOXA_OTHER_UART;
2456 id = mxser_read_register(cap, regs);
2459 brd->info = &mxser_cards[0];
2462 brd->info = &mxser_cards[1];
2464 case CI104J_ASIC_ID:
2465 brd->info = &mxser_cards[2];
2468 brd->info = &mxser_cards[5];
2471 brd->info = &mxser_cards[6];
2474 brd->info = &mxser_cards[7];
2481 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2482 Flag-hack checks if configuration should be read as 2-port here. */
2483 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
2484 irq = regs[9] & 0xF000;
2485 irq = irq | (irq >> 4);
2486 if (irq != (regs[9] & 0xFF00))
2487 goto err_irqconflict;
2488 } else if (brd->info->nports == 4) {
2489 irq = regs[9] & 0xF000;
2490 irq = irq | (irq >> 4);
2491 irq = irq | (irq >> 8);
2493 goto err_irqconflict;
2494 } else if (brd->info->nports == 8) {
2495 irq = regs[9] & 0xF000;
2496 irq = irq | (irq >> 4);
2497 irq = irq | (irq >> 8);
2498 if ((irq != regs[9]) || (irq != regs[10]))
2499 goto err_irqconflict;
2503 printk(KERN_ERR "mxser: interrupt number unset\n");
2506 brd->irq = ((int)(irq & 0xF000) >> 12);
2507 for (i = 0; i < 8; i++)
2508 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
2509 if ((regs[12] & 0x80) == 0) {
2510 printk(KERN_ERR "mxser: invalid interrupt vector\n");
2513 brd->vector = (int)regs[11]; /* interrupt vector */
2515 brd->vector_mask = 0x00FF;
2517 brd->vector_mask = 0x000F;
2518 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2519 if (regs[12] & bits) {
2520 brd->ports[i].baud_base = 921600;
2521 brd->ports[i].max_baud = 921600;
2523 brd->ports[i].baud_base = 115200;
2524 brd->ports[i].max_baud = 115200;
2527 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2528 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2529 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2530 outb(scratch2, cap + UART_LCR);
2531 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2532 scratch = inb(cap + UART_IIR);
2535 brd->uart_type = PORT_16550A;
2537 brd->uart_type = PORT_16450;
2538 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2540 printk(KERN_ERR "mxser: can't request ports I/O region: "
2541 "0x%.8lx-0x%.8lx\n",
2542 brd->ports[0].ioaddr, brd->ports[0].ioaddr +
2543 8 * brd->info->nports - 1);
2547 ret = mxser_request_vector(brd);
2549 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2550 printk(KERN_ERR "mxser: can't request interrupt vector region: "
2551 "0x%.8lx-0x%.8lx\n",
2552 brd->ports[0].ioaddr, brd->ports[0].ioaddr +
2553 8 * brd->info->nports - 1);
2556 return brd->info->nports;
2559 printk(KERN_ERR "mxser: invalid interrupt number\n");
2563 static int mxser_probe(struct pci_dev *pdev,
2564 const struct pci_device_id *ent)
2567 struct mxser_board *brd;
2569 unsigned long ioaddress;
2570 struct device *tty_dev;
2571 int retval = -EINVAL;
2573 for (i = 0; i < MXSER_BOARDS; i++)
2574 if (mxser_boards[i].info == NULL)
2577 if (i >= MXSER_BOARDS) {
2578 dev_err(&pdev->dev, "too many boards found (maximum %d), board "
2579 "not configured\n", MXSER_BOARDS);
2583 brd = &mxser_boards[i];
2584 brd->idx = i * MXSER_PORTS_PER_BOARD;
2585 dev_info(&pdev->dev, "found MOXA %s board (BusNo=%d, DevNo=%d)\n",
2586 mxser_cards[ent->driver_data].name,
2587 pdev->bus->number, PCI_SLOT(pdev->devfn));
2589 retval = pci_enable_device(pdev);
2591 dev_err(&pdev->dev, "PCI enable failed\n");
2596 ioaddress = pci_resource_start(pdev, 2);
2597 retval = pci_request_region(pdev, 2, "mxser(IO)");
2601 brd->info = &mxser_cards[ent->driver_data];
2602 for (i = 0; i < brd->info->nports; i++)
2603 brd->ports[i].ioaddr = ioaddress + 8 * i;
2606 ioaddress = pci_resource_start(pdev, 3);
2607 retval = pci_request_region(pdev, 3, "mxser(vector)");
2610 brd->vector = ioaddress;
2613 brd->irq = pdev->irq;
2615 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2616 brd->uart_type = PORT_16550A;
2617 brd->vector_mask = 0;
2619 for (i = 0; i < brd->info->nports; i++) {
2620 for (j = 0; j < UART_INFO_NUM; j++) {
2621 if (Gpci_uart_info[j].type == brd->chip_flag) {
2622 brd->ports[i].max_baud =
2623 Gpci_uart_info[j].max_baud;
2625 /* exception....CP-102 */
2626 if (brd->info->flags & MXSER_HIGHBAUD)
2627 brd->ports[i].max_baud = 921600;
2633 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2634 for (i = 0; i < brd->info->nports; i++) {
2636 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2638 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
2640 outb(0, ioaddress + 4); /* default set to RS232 mode */
2641 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
2644 for (i = 0; i < brd->info->nports; i++) {
2645 brd->vector_mask |= (1 << i);
2646 brd->ports[i].baud_base = 921600;
2649 /* mxser_initbrd will hook ISR. */
2650 retval = mxser_initbrd(brd, pdev);
2654 for (i = 0; i < brd->info->nports; i++) {
2655 tty_dev = tty_port_register_device(&brd->ports[i].port,
2656 mxvar_sdriver, brd->idx + i, &pdev->dev);
2657 if (IS_ERR(tty_dev)) {
2658 retval = PTR_ERR(tty_dev);
2660 tty_unregister_device(mxvar_sdriver,
2666 pci_set_drvdata(pdev, brd);
2670 for (i = 0; i < brd->info->nports; i++)
2671 tty_port_destroy(&brd->ports[i].port);
2672 free_irq(brd->irq, brd);
2674 pci_release_region(pdev, 3);
2677 pci_release_region(pdev, 2);
2679 pci_disable_device(pdev);
2687 static void mxser_remove(struct pci_dev *pdev)
2690 struct mxser_board *brd = pci_get_drvdata(pdev);
2692 mxser_board_remove(brd);
2694 pci_release_region(pdev, 2);
2695 pci_release_region(pdev, 3);
2696 pci_disable_device(pdev);
2701 static struct pci_driver mxser_driver = {
2703 .id_table = mxser_pcibrds,
2704 .probe = mxser_probe,
2705 .remove = mxser_remove
2708 static int __init mxser_module_init(void)
2710 struct mxser_board *brd;
2711 struct device *tty_dev;
2712 unsigned int b, i, m;
2715 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2719 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2722 /* Initialize the tty_driver structure */
2723 mxvar_sdriver->name = "ttyMI";
2724 mxvar_sdriver->major = ttymajor;
2725 mxvar_sdriver->minor_start = 0;
2726 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2727 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2728 mxvar_sdriver->init_termios = tty_std_termios;
2729 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2730 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2731 tty_set_operations(mxvar_sdriver, &mxser_ops);
2733 retval = tty_register_driver(mxvar_sdriver);
2735 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2740 /* Start finding ISA boards here */
2741 for (m = 0, b = 0; b < MXSER_BOARDS; b++) {
2745 brd = &mxser_boards[m];
2746 retval = mxser_get_ISA_conf(ioaddr[b], brd);
2752 printk(KERN_INFO "mxser: found MOXA %s board (CAP=0x%lx)\n",
2753 brd->info->name, ioaddr[b]);
2755 /* mxser_initbrd will hook ISR. */
2756 if (mxser_initbrd(brd, NULL) < 0) {
2757 mxser_release_ISA_res(brd);
2762 brd->idx = m * MXSER_PORTS_PER_BOARD;
2763 for (i = 0; i < brd->info->nports; i++) {
2764 tty_dev = tty_port_register_device(&brd->ports[i].port,
2765 mxvar_sdriver, brd->idx + i, NULL);
2766 if (IS_ERR(tty_dev)) {
2768 tty_unregister_device(mxvar_sdriver,
2770 for (i = 0; i < brd->info->nports; i++)
2771 tty_port_destroy(&brd->ports[i].port);
2772 free_irq(brd->irq, brd);
2773 mxser_release_ISA_res(brd);
2778 if (brd->info == NULL)
2784 retval = pci_register_driver(&mxser_driver);
2786 printk(KERN_ERR "mxser: can't register pci driver\n");
2790 } /* else: we have some ISA cards under control */
2795 tty_unregister_driver(mxvar_sdriver);
2797 put_tty_driver(mxvar_sdriver);
2801 static void __exit mxser_module_exit(void)
2805 pci_unregister_driver(&mxser_driver);
2807 for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
2808 if (mxser_boards[i].info != NULL)
2809 mxser_board_remove(&mxser_boards[i]);
2810 tty_unregister_driver(mxvar_sdriver);
2811 put_tty_driver(mxvar_sdriver);
2813 for (i = 0; i < MXSER_BOARDS; i++)
2814 if (mxser_boards[i].info != NULL)
2815 mxser_release_ISA_res(&mxser_boards[i]);
2818 module_init(mxser_module_init);
2819 module_exit(mxser_module_exit);