1 // SPDX-License-Identifier: GPL-2.0+
3 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
5 * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com).
6 * Copyright (C) 2006-2008 Jiri Slaby <jirislaby@gmail.com>
8 * This code is loosely based on the 1.8 moxa driver which is based on
9 * Linux serial driver, written by Linus Torvalds, Theodore T'so and
12 * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
13 * <alan@lxorguk.ukuu.org.uk>. The original 1.8 code is available on
15 * - Fixed x86_64 cleanness
18 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/signal.h>
21 #include <linux/sched.h>
22 #include <linux/timer.h>
23 #include <linux/interrupt.h>
24 #include <linux/tty.h>
25 #include <linux/tty_flip.h>
26 #include <linux/serial.h>
27 #include <linux/serial_reg.h>
28 #include <linux/major.h>
29 #include <linux/string.h>
30 #include <linux/fcntl.h>
31 #include <linux/ptrace.h>
32 #include <linux/ioport.h>
34 #include <linux/delay.h>
35 #include <linux/pci.h>
36 #include <linux/bitops.h>
37 #include <linux/slab.h>
38 #include <linux/ratelimit.h>
42 #include <linux/uaccess.h>
45 * Semi-public control interfaces
53 #define MOXA_SET_OP_MODE (MOXA + 66)
54 #define MOXA_GET_OP_MODE (MOXA + 67)
57 #define RS485_2WIRE_MODE 1
59 #define RS485_4WIRE_MODE 3
60 #define OP_MODE_MASK 3
62 /* --------------------------------------------------- */
65 * Follow just what Moxa Must chip defines.
67 * When LCR register (offset 0x03) writes the following value, the Must chip
68 * will enter enchance mode. And write value on EFR (offset 0x02) bit 6,7 to
71 #define MOXA_MUST_ENTER_ENCHANCE 0xBF
73 /* when enhance mode enabled, access on general bank register */
74 #define MOXA_MUST_GDL_REGISTER 0x07
75 #define MOXA_MUST_GDL_MASK 0x7F
76 #define MOXA_MUST_GDL_HAS_BAD_DATA 0x80
78 #define MOXA_MUST_LSR_RERR 0x80 /* error in receive FIFO */
79 /* enchance register bank select and enchance mode setting register */
80 /* when LCR register equals to 0xBF */
81 #define MOXA_MUST_EFR_REGISTER 0x02
82 #define MOXA_MUST_EFR_EFRB_ENABLE 0x10 /* enchance mode enable */
83 /* enchance register bank set 0, 1, 2 */
84 #define MOXA_MUST_EFR_BANK0 0x00
85 #define MOXA_MUST_EFR_BANK1 0x40
86 #define MOXA_MUST_EFR_BANK2 0x80
87 #define MOXA_MUST_EFR_BANK3 0xC0
88 #define MOXA_MUST_EFR_BANK_MASK 0xC0
90 /* set XON1 value register, when LCR=0xBF and change to bank0 */
91 #define MOXA_MUST_XON1_REGISTER 0x04
93 /* set XON2 value register, when LCR=0xBF and change to bank0 */
94 #define MOXA_MUST_XON2_REGISTER 0x05
96 /* set XOFF1 value register, when LCR=0xBF and change to bank0 */
97 #define MOXA_MUST_XOFF1_REGISTER 0x06
99 /* set XOFF2 value register, when LCR=0xBF and change to bank0 */
100 #define MOXA_MUST_XOFF2_REGISTER 0x07
102 #define MOXA_MUST_RBRTL_REGISTER 0x04
103 #define MOXA_MUST_RBRTH_REGISTER 0x05
104 #define MOXA_MUST_RBRTI_REGISTER 0x06
105 #define MOXA_MUST_THRTL_REGISTER 0x07
106 #define MOXA_MUST_ENUM_REGISTER 0x04
107 #define MOXA_MUST_HWID_REGISTER 0x05
108 #define MOXA_MUST_ECR_REGISTER 0x06
109 #define MOXA_MUST_CSR_REGISTER 0x07
111 #define MOXA_MUST_FCR_GDA_MODE_ENABLE 0x20 /* good data mode enable */
112 #define MOXA_MUST_FCR_GDA_ONLY_ENABLE 0x10 /* only good data put into RxFIFO */
114 #define MOXA_MUST_IER_ECTSI 0x80 /* enable CTS interrupt */
115 #define MOXA_MUST_IER_ERTSI 0x40 /* enable RTS interrupt */
116 #define MOXA_MUST_IER_XINT 0x20 /* enable Xon/Xoff interrupt */
117 #define MOXA_MUST_IER_EGDAI 0x10 /* enable GDA interrupt */
119 #define MOXA_MUST_RECV_ISR (UART_IER_RDI | MOXA_MUST_IER_EGDAI)
121 /* GDA interrupt pending */
122 #define MOXA_MUST_IIR_GDA 0x1C
123 #define MOXA_MUST_IIR_RDA 0x04
124 #define MOXA_MUST_IIR_RTO 0x0C
125 #define MOXA_MUST_IIR_LSR 0x06
127 /* received Xon/Xoff or specical interrupt pending */
128 #define MOXA_MUST_IIR_XSC 0x10
130 /* RTS/CTS change state interrupt pending */
131 #define MOXA_MUST_IIR_RTSCTS 0x20
132 #define MOXA_MUST_IIR_MASK 0x3E
134 #define MOXA_MUST_MCR_XON_FLAG 0x40
135 #define MOXA_MUST_MCR_XON_ANY 0x80
136 #define MOXA_MUST_MCR_TX_XON 0x08
138 #define MOXA_MUST_EFR_SF_MASK 0x0F /* software flow control on chip mask value */
139 #define MOXA_MUST_EFR_SF_TX1 0x08 /* send Xon1/Xoff1 */
140 #define MOXA_MUST_EFR_SF_TX2 0x04 /* send Xon2/Xoff2 */
141 #define MOXA_MUST_EFR_SF_TX12 0x0C /* send Xon1,Xon2/Xoff1,Xoff2 */
142 #define MOXA_MUST_EFR_SF_TX_NO 0x00 /* don't send Xon/Xoff */
143 #define MOXA_MUST_EFR_SF_TX_MASK 0x0C /* Tx software flow control mask */
144 #define MOXA_MUST_EFR_SF_RX_NO 0x00 /* don't receive Xon/Xoff */
145 #define MOXA_MUST_EFR_SF_RX1 0x02 /* receive Xon1/Xoff1 */
146 #define MOXA_MUST_EFR_SF_RX2 0x01 /* receive Xon2/Xoff2 */
147 #define MOXA_MUST_EFR_SF_RX12 0x03 /* receive Xon1,Xon2/Xoff1,Xoff2 */
148 #define MOXA_MUST_EFR_SF_RX_MASK 0x03 /* Rx software flow control mask */
150 #define MXSERMAJOR 174
152 #define MXSER_BOARDS 4 /* Max. boards */
153 #define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
154 #define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
155 #define MXSER_ISR_PASS_LIMIT 100
157 #define WAKEUP_CHARS 256
159 #define MXSER_BAUD_BASE 921600
160 #define MXSER_CUSTOM_DIVISOR (MXSER_BAUD_BASE * 16)
162 #define PCI_DEVICE_ID_POS104UL 0x1044
163 #define PCI_DEVICE_ID_CB108 0x1080
164 #define PCI_DEVICE_ID_CP102UF 0x1023
165 #define PCI_DEVICE_ID_CP112UL 0x1120
166 #define PCI_DEVICE_ID_CB114 0x1142
167 #define PCI_DEVICE_ID_CP114UL 0x1143
168 #define PCI_DEVICE_ID_CB134I 0x1341
169 #define PCI_DEVICE_ID_CP138U 0x1380
171 #define MXSER_NPORTS(ddata) ((ddata) & 0xffU)
172 #define MXSER_HIGHBAUD 0x0100
174 enum mxser_must_hwid {
175 MOXA_OTHER_UART = 0x00,
176 MOXA_MUST_MU150_HWID = 0x01,
177 MOXA_MUST_MU860_HWID = 0x02,
180 static const struct {
186 } Gpci_uart_info[] = {
187 { MOXA_OTHER_UART, 16, 14, 1, 921600 },
188 { MOXA_MUST_MU150_HWID, 64, 48, 16, 230400 },
189 { MOXA_MUST_MU860_HWID, 128, 96, 32, 921600 }
191 #define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
194 /* driver_data correspond to the lines in the structure above
195 see also ISA probe function before you change something */
196 static const struct pci_device_id mxser_pcibrds[] = {
197 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168), .driver_data = 8 },
198 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104), .driver_data = 4 },
199 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132), .driver_data = 2 },
200 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114), .driver_data = 4 },
201 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114), .driver_data = 4 },
202 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102), .driver_data = 2 | MXSER_HIGHBAUD },
203 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 4 },
204 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 8 },
205 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 2 },
206 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 4 },
207 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 4 },
208 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 8 }, /* RC7000 */
209 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 8 },
210 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 2 },
211 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 2 },
212 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 8 },
213 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 8 },
214 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 4 },
215 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 8 },
216 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 4 },
217 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 4 },
218 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 8 },
219 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 4 },
220 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP114UL), .driver_data = 4 },
221 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP102UF), .driver_data = 2 },
222 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP112UL), .driver_data = 2 },
225 MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
227 static int ttymajor = MXSERMAJOR;
229 /* Variables for insmod */
231 MODULE_AUTHOR("Casper Yang");
232 MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
233 module_param(ttymajor, int, 0);
234 MODULE_LICENSE("GPL");
239 struct tty_port port;
240 struct mxser_board *board;
242 unsigned long ioaddr;
243 unsigned long opmode_ioaddr;
247 int type; /* UART type */
249 unsigned char x_char; /* xon/xoff character */
250 u8 IER; /* Interrupt Enable Register */
251 u8 MCR; /* Modem control register */
253 unsigned char ldisc_stop_rx;
255 struct async_icount icount; /* kernel counters for 4 input interrupts */
256 unsigned int timeout;
259 u8 ignore_status_mask;
261 unsigned int xmit_head;
262 unsigned int xmit_tail;
263 unsigned int xmit_cnt;
271 unsigned short nports;
273 unsigned long vector;
275 enum mxser_must_hwid must_hwid;
278 struct mxser_port ports[];
281 static DECLARE_BITMAP(mxser_boards, MXSER_BOARDS);
282 static struct tty_driver *mxvar_sdriver;
284 static u8 __mxser_must_set_EFR(unsigned long baseio, u8 clear, u8 set,
289 oldlcr = inb(baseio + UART_LCR);
290 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
292 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
296 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
299 outb(oldlcr, baseio + UART_LCR);
304 static u8 mxser_must_select_bank(unsigned long baseio, u8 bank)
306 return __mxser_must_set_EFR(baseio, MOXA_MUST_EFR_BANK_MASK, bank,
310 static void mxser_set_must_xon1_value(unsigned long baseio, u8 value)
312 u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK0);
313 outb(value, baseio + MOXA_MUST_XON1_REGISTER);
314 outb(oldlcr, baseio + UART_LCR);
317 static void mxser_set_must_xoff1_value(unsigned long baseio, u8 value)
319 u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK0);
320 outb(value, baseio + MOXA_MUST_XOFF1_REGISTER);
321 outb(oldlcr, baseio + UART_LCR);
324 static void mxser_set_must_fifo_value(struct mxser_port *info)
326 u8 oldlcr = mxser_must_select_bank(info->ioaddr, MOXA_MUST_EFR_BANK1);
327 outb(info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTH_REGISTER);
328 outb(info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTI_REGISTER);
329 outb(info->rx_low_water, info->ioaddr + MOXA_MUST_RBRTL_REGISTER);
330 outb(oldlcr, info->ioaddr + UART_LCR);
333 static void mxser_set_must_enum_value(unsigned long baseio, u8 value)
335 u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK2);
336 outb(value, baseio + MOXA_MUST_ENUM_REGISTER);
337 outb(oldlcr, baseio + UART_LCR);
340 static u8 mxser_get_must_hardware_id(unsigned long baseio)
342 u8 oldlcr = mxser_must_select_bank(baseio, MOXA_MUST_EFR_BANK2);
343 u8 id = inb(baseio + MOXA_MUST_HWID_REGISTER);
344 outb(oldlcr, baseio + UART_LCR);
349 static void mxser_must_set_EFR(unsigned long baseio, u8 clear, u8 set)
351 __mxser_must_set_EFR(baseio, clear, set, true);
354 static void mxser_must_set_enhance_mode(unsigned long baseio, bool enable)
356 mxser_must_set_EFR(baseio,
357 enable ? 0 : MOXA_MUST_EFR_EFRB_ENABLE,
358 enable ? MOXA_MUST_EFR_EFRB_ENABLE : 0);
361 static void mxser_must_no_sw_flow_control(unsigned long baseio)
363 mxser_must_set_EFR(baseio, MOXA_MUST_EFR_SF_MASK, 0);
366 static void mxser_must_set_tx_sw_flow_control(unsigned long baseio, bool enable)
368 mxser_must_set_EFR(baseio, MOXA_MUST_EFR_SF_TX_MASK,
369 enable ? MOXA_MUST_EFR_SF_TX1 : 0);
372 static void mxser_must_set_rx_sw_flow_control(unsigned long baseio, bool enable)
374 mxser_must_set_EFR(baseio, MOXA_MUST_EFR_SF_RX_MASK,
375 enable ? MOXA_MUST_EFR_SF_RX1 : 0);
378 static enum mxser_must_hwid mxser_must_get_hwid(unsigned long io)
383 outb(0, io + UART_LCR);
384 mxser_must_set_enhance_mode(io, false);
385 oldmcr = inb(io + UART_MCR);
386 outb(0, io + UART_MCR);
387 mxser_set_must_xon1_value(io, 0x11);
388 if ((hwid = inb(io + UART_MCR)) != 0) {
389 outb(oldmcr, io + UART_MCR);
390 return MOXA_OTHER_UART;
393 hwid = mxser_get_must_hardware_id(io);
394 for (i = 1; i < UART_INFO_NUM; i++) /* 0 = OTHER_UART */
395 if (hwid == Gpci_uart_info[i].type)
398 return MOXA_OTHER_UART;
401 static void mxser_process_txrx_fifo(struct mxser_port *info)
405 if (info->type == PORT_16450 || info->type == PORT_8250) {
406 info->rx_high_water = 1;
407 info->rx_low_water = 1;
408 info->xmit_fifo_size = 1;
412 for (i = 0; i < UART_INFO_NUM; i++)
413 if (info->board->must_hwid == Gpci_uart_info[i].type) {
414 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
415 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
416 info->xmit_fifo_size = Gpci_uart_info[i].fifo_size;
421 static int mxser_carrier_raised(struct tty_port *port)
423 struct mxser_port *mp = container_of(port, struct mxser_port, port);
424 return (inb(mp->ioaddr + UART_MSR) & UART_MSR_DCD)?1:0;
427 static void mxser_dtr_rts(struct tty_port *port, int on)
429 struct mxser_port *mp = container_of(port, struct mxser_port, port);
433 spin_lock_irqsave(&mp->slock, flags);
434 mcr = inb(mp->ioaddr + UART_MCR);
436 mcr |= UART_MCR_DTR | UART_MCR_RTS;
438 mcr &= ~(UART_MCR_DTR | UART_MCR_RTS);
439 outb(mcr, mp->ioaddr + UART_MCR);
440 spin_unlock_irqrestore(&mp->slock, flags);
443 static int mxser_set_baud(struct tty_struct *tty, speed_t newspd)
445 struct mxser_port *info = tty->driver_data;
446 unsigned int quot = 0, baud;
450 if (newspd > info->board->max_baud)
454 quot = 2 * MXSER_BAUD_BASE / 269;
455 tty_encode_baud_rate(tty, 134, 134);
457 quot = MXSER_BAUD_BASE / newspd;
460 baud = MXSER_BAUD_BASE / quot;
461 tty_encode_baud_rate(tty, baud, baud);
467 * worst case (128 * 1000 * 10 * 18432) needs 35 bits, so divide in the
470 timeout = (u64)info->xmit_fifo_size * HZ * 10 * quot;
471 do_div(timeout, MXSER_BAUD_BASE);
472 info->timeout = timeout + HZ / 50; /* Add .02 seconds of slop */
475 info->MCR |= UART_MCR_DTR;
476 outb(info->MCR, info->ioaddr + UART_MCR);
478 info->MCR &= ~UART_MCR_DTR;
479 outb(info->MCR, info->ioaddr + UART_MCR);
483 cval = inb(info->ioaddr + UART_LCR);
485 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
487 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
488 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
489 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
492 if (C_BAUD(tty) == BOTHER) {
493 quot = MXSER_BAUD_BASE % newspd;
495 if (quot % newspd > newspd / 2) {
501 mxser_set_must_enum_value(info->ioaddr, quot);
504 mxser_set_must_enum_value(info->ioaddr, 0);
510 * This routine is called to set the UART divisor registers to match
511 * the specified baud rate for a serial port.
513 static void mxser_change_speed(struct tty_struct *tty)
515 struct mxser_port *info = tty->driver_data;
516 unsigned cflag, cval, fcr;
517 unsigned char status;
519 cflag = tty->termios.c_cflag;
521 mxser_set_baud(tty, tty_get_baud_rate(tty));
523 /* byte size and parity */
524 switch (cflag & CSIZE) {
527 cval = UART_LCR_WLEN5;
530 cval = UART_LCR_WLEN6;
533 cval = UART_LCR_WLEN7;
536 cval = UART_LCR_WLEN8;
541 cval |= UART_LCR_STOP;
543 cval |= UART_LCR_PARITY;
544 if (!(cflag & PARODD))
545 cval |= UART_LCR_EPAR;
547 cval |= UART_LCR_SPAR;
549 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
550 if (info->board->must_hwid) {
551 fcr = UART_FCR_ENABLE_FIFO;
552 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
553 mxser_set_must_fifo_value(info);
557 fcr = UART_FCR_ENABLE_FIFO;
558 if (info->board->must_hwid) {
559 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
560 mxser_set_must_fifo_value(info);
562 switch (info->rx_high_water) {
564 fcr |= UART_FCR_TRIGGER_1;
567 fcr |= UART_FCR_TRIGGER_4;
570 fcr |= UART_FCR_TRIGGER_8;
573 fcr |= UART_FCR_TRIGGER_14;
579 /* CTS flow control flag and modem status interrupts */
580 info->IER &= ~UART_IER_MSI;
581 info->MCR &= ~UART_MCR_AFE;
582 tty_port_set_cts_flow(&info->port, cflag & CRTSCTS);
583 if (cflag & CRTSCTS) {
584 info->IER |= UART_IER_MSI;
585 if ((info->type == PORT_16550A) || (info->board->must_hwid)) {
586 info->MCR |= UART_MCR_AFE;
588 status = inb(info->ioaddr + UART_MSR);
589 if (tty->hw_stopped) {
590 if (status & UART_MSR_CTS) {
592 if (info->type != PORT_16550A &&
593 !info->board->must_hwid) {
594 outb(info->IER & ~UART_IER_THRI,
597 info->IER |= UART_IER_THRI;
598 outb(info->IER, info->ioaddr +
604 if (!(status & UART_MSR_CTS)) {
606 if ((info->type != PORT_16550A) &&
607 (!info->board->must_hwid)) {
608 info->IER &= ~UART_IER_THRI;
609 outb(info->IER, info->ioaddr +
616 outb(info->MCR, info->ioaddr + UART_MCR);
617 tty_port_set_check_carrier(&info->port, ~cflag & CLOCAL);
619 info->IER |= UART_IER_MSI;
620 outb(info->IER, info->ioaddr + UART_IER);
623 * Set up parity check flag
625 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
627 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
628 if (I_BRKINT(tty) || I_PARMRK(tty))
629 info->read_status_mask |= UART_LSR_BI;
631 info->ignore_status_mask = 0;
634 info->ignore_status_mask |= UART_LSR_BI;
635 info->read_status_mask |= UART_LSR_BI;
637 * If we're ignore parity and break indicators, ignore
638 * overruns too. (For real raw support).
641 info->ignore_status_mask |=
645 info->read_status_mask |=
651 if (info->board->must_hwid) {
652 mxser_set_must_xon1_value(info->ioaddr, START_CHAR(tty));
653 mxser_set_must_xoff1_value(info->ioaddr, STOP_CHAR(tty));
654 mxser_must_set_rx_sw_flow_control(info->ioaddr, I_IXON(tty));
655 mxser_must_set_tx_sw_flow_control(info->ioaddr, I_IXOFF(tty));
659 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
660 outb(cval, info->ioaddr + UART_LCR);
663 static void mxser_check_modem_status(struct tty_struct *tty,
664 struct mxser_port *port, int status)
666 /* update input line counters */
667 if (status & UART_MSR_TERI)
669 if (status & UART_MSR_DDSR)
671 if (status & UART_MSR_DDCD)
673 if (status & UART_MSR_DCTS)
675 wake_up_interruptible(&port->port.delta_msr_wait);
677 if (tty_port_check_carrier(&port->port) && (status & UART_MSR_DDCD)) {
678 if (status & UART_MSR_DCD)
679 wake_up_interruptible(&port->port.open_wait);
682 if (tty_port_cts_enabled(&port->port)) {
683 if (tty->hw_stopped) {
684 if (status & UART_MSR_CTS) {
687 if ((port->type != PORT_16550A) &&
688 (!port->board->must_hwid)) {
689 outb(port->IER & ~UART_IER_THRI,
690 port->ioaddr + UART_IER);
691 port->IER |= UART_IER_THRI;
692 outb(port->IER, port->ioaddr +
698 if (!(status & UART_MSR_CTS)) {
700 if (port->type != PORT_16550A &&
701 !port->board->must_hwid) {
702 port->IER &= ~UART_IER_THRI;
703 outb(port->IER, port->ioaddr +
711 static int mxser_activate(struct tty_port *port, struct tty_struct *tty)
713 struct mxser_port *info = container_of(port, struct mxser_port, port);
717 page = __get_free_page(GFP_KERNEL);
721 spin_lock_irqsave(&info->slock, flags);
724 set_bit(TTY_IO_ERROR, &tty->flags);
726 spin_unlock_irqrestore(&info->slock, flags);
729 info->port.xmit_buf = (unsigned char *) page;
732 * Clear the FIFO buffers and disable them
733 * (they will be reenabled in mxser_change_speed())
735 if (info->board->must_hwid)
736 outb((UART_FCR_CLEAR_RCVR |
737 UART_FCR_CLEAR_XMIT |
738 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
740 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
741 info->ioaddr + UART_FCR);
744 * At this point there's no way the LSR could still be 0xFF;
745 * if it is, then bail out, because there's likely no UART
748 if (inb(info->ioaddr + UART_LSR) == 0xff) {
749 spin_unlock_irqrestore(&info->slock, flags);
750 if (capable(CAP_SYS_ADMIN)) {
751 set_bit(TTY_IO_ERROR, &tty->flags);
758 * Clear the interrupt registers.
760 (void) inb(info->ioaddr + UART_LSR);
761 (void) inb(info->ioaddr + UART_RX);
762 (void) inb(info->ioaddr + UART_IIR);
763 (void) inb(info->ioaddr + UART_MSR);
766 * Now, initialize the UART
768 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
769 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
770 outb(info->MCR, info->ioaddr + UART_MCR);
773 * Finally, enable interrupts
775 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
777 if (info->board->must_hwid)
778 info->IER |= MOXA_MUST_IER_EGDAI;
779 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
782 * And clear the interrupt registers again for luck.
784 (void) inb(info->ioaddr + UART_LSR);
785 (void) inb(info->ioaddr + UART_RX);
786 (void) inb(info->ioaddr + UART_IIR);
787 (void) inb(info->ioaddr + UART_MSR);
789 clear_bit(TTY_IO_ERROR, &tty->flags);
790 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
793 * and set the speed of the serial port
795 mxser_change_speed(tty);
796 spin_unlock_irqrestore(&info->slock, flags);
802 * This routine will shutdown a serial port
804 static void mxser_shutdown_port(struct tty_port *port)
806 struct mxser_port *info = container_of(port, struct mxser_port, port);
809 spin_lock_irqsave(&info->slock, flags);
812 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
813 * here so the queue might never be waken up
815 wake_up_interruptible(&info->port.delta_msr_wait);
818 * Free the xmit buffer, if necessary
820 if (info->port.xmit_buf) {
821 free_page((unsigned long) info->port.xmit_buf);
822 info->port.xmit_buf = NULL;
826 outb(0x00, info->ioaddr + UART_IER);
828 /* clear Rx/Tx FIFO's */
829 if (info->board->must_hwid)
830 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
831 MOXA_MUST_FCR_GDA_MODE_ENABLE,
832 info->ioaddr + UART_FCR);
834 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
835 info->ioaddr + UART_FCR);
837 /* read data port to reset things */
838 (void) inb(info->ioaddr + UART_RX);
841 if (info->board->must_hwid)
842 mxser_must_no_sw_flow_control(info->ioaddr);
844 spin_unlock_irqrestore(&info->slock, flags);
848 * This routine is called whenever a serial port is opened. It
849 * enables interrupts for a serial port, linking in its async structure into
850 * the IRQ chain. It also performs the serial-specific
851 * initialization for the tty structure.
853 static int mxser_open(struct tty_struct *tty, struct file *filp)
855 struct tty_port *tport = tty->port;
856 struct mxser_port *port = container_of(tport, struct mxser_port, port);
858 tty->driver_data = port;
860 return tty_port_open(tport, tty, filp);
863 static void mxser_flush_buffer(struct tty_struct *tty)
865 struct mxser_port *info = tty->driver_data;
870 spin_lock_irqsave(&info->slock, flags);
871 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
873 fcr = inb(info->ioaddr + UART_FCR);
874 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
875 info->ioaddr + UART_FCR);
876 outb(fcr, info->ioaddr + UART_FCR);
878 spin_unlock_irqrestore(&info->slock, flags);
884 static void mxser_close_port(struct tty_port *port)
886 struct mxser_port *info = container_of(port, struct mxser_port, port);
887 unsigned long timeout;
889 * At this point we stop accepting input. To do this, we
890 * disable the receive line status interrupts, and tell the
891 * interrupt driver to stop checking the data ready bit in the
892 * line status register.
894 info->IER &= ~UART_IER_RLSI;
895 if (info->board->must_hwid)
896 info->IER &= ~MOXA_MUST_RECV_ISR;
898 outb(info->IER, info->ioaddr + UART_IER);
900 * Before we drop DTR, make sure the UART transmitter
901 * has completely drained; this is especially
902 * important if there is a transmit FIFO!
904 timeout = jiffies + HZ;
905 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
906 schedule_timeout_interruptible(5);
907 if (time_after(jiffies, timeout))
913 * This routine is called when the serial port gets closed. First, we
914 * wait for the last remaining data to be sent. Then, we unlink its
915 * async structure from the interrupt chain if necessary, and we free
916 * that IRQ if nothing is left in the chain.
918 static void mxser_close(struct tty_struct *tty, struct file *filp)
920 struct mxser_port *info = tty->driver_data;
921 struct tty_port *port = &info->port;
925 if (tty_port_close_start(port, tty, filp) == 0)
928 mutex_lock(&port->mutex);
929 mxser_close_port(port);
930 mxser_flush_buffer(tty);
931 if (tty_port_initialized(port) && C_HUPCL(tty))
932 tty_port_lower_dtr_rts(port);
933 mxser_shutdown_port(port);
934 tty_port_set_initialized(port, 0);
935 mutex_unlock(&port->mutex);
937 /* Right now the tty_port set is done outside of the close_end helper
938 as we don't yet have everyone using refcounts */
939 tty_port_close_end(port, tty);
940 tty_port_tty_set(port, NULL);
943 static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
946 struct mxser_port *info = tty->driver_data;
949 if (!info->port.xmit_buf)
953 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
954 SERIAL_XMIT_SIZE - info->xmit_head));
958 memcpy(info->port.xmit_buf + info->xmit_head, buf, c);
959 spin_lock_irqsave(&info->slock, flags);
960 info->xmit_head = (info->xmit_head + c) &
961 (SERIAL_XMIT_SIZE - 1);
963 spin_unlock_irqrestore(&info->slock, flags);
970 if (info->xmit_cnt && !tty->flow.stopped) {
971 if (!tty->hw_stopped ||
972 (info->type == PORT_16550A) ||
973 (info->board->must_hwid)) {
974 spin_lock_irqsave(&info->slock, flags);
975 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
977 info->IER |= UART_IER_THRI;
978 outb(info->IER, info->ioaddr + UART_IER);
979 spin_unlock_irqrestore(&info->slock, flags);
985 static int mxser_put_char(struct tty_struct *tty, unsigned char ch)
987 struct mxser_port *info = tty->driver_data;
990 if (!info->port.xmit_buf)
993 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
996 spin_lock_irqsave(&info->slock, flags);
997 info->port.xmit_buf[info->xmit_head++] = ch;
998 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1000 spin_unlock_irqrestore(&info->slock, flags);
1006 static void mxser_flush_chars(struct tty_struct *tty)
1008 struct mxser_port *info = tty->driver_data;
1009 unsigned long flags;
1011 if (info->xmit_cnt <= 0 || tty->flow.stopped || !info->port.xmit_buf ||
1012 (tty->hw_stopped && info->type != PORT_16550A &&
1013 !info->board->must_hwid))
1016 spin_lock_irqsave(&info->slock, flags);
1018 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1019 info->IER |= UART_IER_THRI;
1020 outb(info->IER, info->ioaddr + UART_IER);
1022 spin_unlock_irqrestore(&info->slock, flags);
1025 static unsigned int mxser_write_room(struct tty_struct *tty)
1027 struct mxser_port *info = tty->driver_data;
1030 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1031 return ret < 0 ? 0 : ret;
1034 static unsigned int mxser_chars_in_buffer(struct tty_struct *tty)
1036 struct mxser_port *info = tty->driver_data;
1037 return info->xmit_cnt;
1041 * ------------------------------------------------------------
1042 * friends of mxser_ioctl()
1043 * ------------------------------------------------------------
1045 static int mxser_get_serial_info(struct tty_struct *tty,
1046 struct serial_struct *ss)
1048 struct mxser_port *info = tty->driver_data;
1049 struct tty_port *port = &info->port;
1050 unsigned int closing_wait, close_delay;
1052 mutex_lock(&port->mutex);
1054 close_delay = jiffies_to_msecs(info->port.close_delay) / 10;
1055 closing_wait = info->port.closing_wait;
1056 if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
1057 closing_wait = jiffies_to_msecs(closing_wait) / 10;
1059 ss->type = info->type,
1060 ss->line = tty->index,
1061 ss->port = info->ioaddr,
1062 ss->irq = info->board->irq,
1063 ss->flags = info->port.flags,
1064 ss->baud_base = MXSER_BAUD_BASE,
1065 ss->close_delay = close_delay;
1066 ss->closing_wait = closing_wait;
1067 ss->custom_divisor = MXSER_CUSTOM_DIVISOR,
1068 mutex_unlock(&port->mutex);
1072 static int mxser_set_serial_info(struct tty_struct *tty,
1073 struct serial_struct *ss)
1075 struct mxser_port *info = tty->driver_data;
1076 struct tty_port *port = &info->port;
1078 unsigned long sl_flags;
1079 unsigned int flags, close_delay, closing_wait;
1082 if (tty_io_error(tty))
1085 mutex_lock(&port->mutex);
1087 if (ss->irq != info->board->irq ||
1088 ss->port != info->ioaddr) {
1089 mutex_unlock(&port->mutex);
1093 flags = port->flags & ASYNC_SPD_MASK;
1095 close_delay = msecs_to_jiffies(ss->close_delay * 10);
1096 closing_wait = ss->closing_wait;
1097 if (closing_wait != ASYNC_CLOSING_WAIT_NONE)
1098 closing_wait = msecs_to_jiffies(closing_wait * 10);
1100 if (!capable(CAP_SYS_ADMIN)) {
1101 if ((ss->baud_base != MXSER_BAUD_BASE) ||
1102 (close_delay != info->port.close_delay) ||
1103 (closing_wait != info->port.closing_wait) ||
1104 ((ss->flags & ~ASYNC_USR_MASK) != (info->port.flags & ~ASYNC_USR_MASK))) {
1105 mutex_unlock(&port->mutex);
1108 info->port.flags = ((info->port.flags & ~ASYNC_USR_MASK) |
1109 (ss->flags & ASYNC_USR_MASK));
1112 * OK, past this point, all the error checking has been done.
1113 * At this point, we start making changes.....
1115 port->flags = ((port->flags & ~ASYNC_FLAGS) |
1116 (ss->flags & ASYNC_FLAGS));
1117 port->close_delay = close_delay;
1118 port->closing_wait = closing_wait;
1119 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
1120 (ss->baud_base != MXSER_BAUD_BASE ||
1121 ss->custom_divisor !=
1122 MXSER_CUSTOM_DIVISOR)) {
1123 if (ss->custom_divisor == 0) {
1124 mutex_unlock(&port->mutex);
1127 baud = ss->baud_base / ss->custom_divisor;
1128 tty_encode_baud_rate(tty, baud, baud);
1131 info->type = ss->type;
1133 mxser_process_txrx_fifo(info);
1136 if (tty_port_initialized(port)) {
1137 if (flags != (port->flags & ASYNC_SPD_MASK)) {
1138 spin_lock_irqsave(&info->slock, sl_flags);
1139 mxser_change_speed(tty);
1140 spin_unlock_irqrestore(&info->slock, sl_flags);
1143 retval = mxser_activate(port, tty);
1145 tty_port_set_initialized(port, 1);
1147 mutex_unlock(&port->mutex);
1152 * mxser_get_lsr_info - get line status register info
1154 * Purpose: Let user call ioctl() to get info when the UART physically
1155 * is emptied. On bus types like RS485, the transmitter must
1156 * release the bus after transmitting. This must be done when
1157 * the transmit shift register is empty, not be done when the
1158 * transmit holding register is empty. This functionality
1159 * allows an RS485 driver to be written in user space.
1161 static int mxser_get_lsr_info(struct mxser_port *info,
1162 unsigned int __user *value)
1164 unsigned char status;
1165 unsigned int result;
1166 unsigned long flags;
1168 spin_lock_irqsave(&info->slock, flags);
1169 status = inb(info->ioaddr + UART_LSR);
1170 spin_unlock_irqrestore(&info->slock, flags);
1171 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1172 return put_user(result, value);
1175 static int mxser_tiocmget(struct tty_struct *tty)
1177 struct mxser_port *info = tty->driver_data;
1178 unsigned char control, status;
1179 unsigned long flags;
1181 if (tty_io_error(tty))
1184 control = info->MCR;
1186 spin_lock_irqsave(&info->slock, flags);
1187 status = inb(info->ioaddr + UART_MSR);
1188 if (status & UART_MSR_ANY_DELTA)
1189 mxser_check_modem_status(tty, info, status);
1190 spin_unlock_irqrestore(&info->slock, flags);
1191 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1192 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1193 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1194 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1195 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1196 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1199 static int mxser_tiocmset(struct tty_struct *tty,
1200 unsigned int set, unsigned int clear)
1202 struct mxser_port *info = tty->driver_data;
1203 unsigned long flags;
1205 if (tty_io_error(tty))
1208 spin_lock_irqsave(&info->slock, flags);
1210 if (set & TIOCM_RTS)
1211 info->MCR |= UART_MCR_RTS;
1212 if (set & TIOCM_DTR)
1213 info->MCR |= UART_MCR_DTR;
1215 if (clear & TIOCM_RTS)
1216 info->MCR &= ~UART_MCR_RTS;
1217 if (clear & TIOCM_DTR)
1218 info->MCR &= ~UART_MCR_DTR;
1220 outb(info->MCR, info->ioaddr + UART_MCR);
1221 spin_unlock_irqrestore(&info->slock, flags);
1225 static int mxser_cflags_changed(struct mxser_port *info, unsigned long arg,
1226 struct async_icount *cprev)
1228 struct async_icount cnow;
1229 unsigned long flags;
1232 spin_lock_irqsave(&info->slock, flags);
1233 cnow = info->icount; /* atomic copy */
1234 spin_unlock_irqrestore(&info->slock, flags);
1236 ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1237 ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1238 ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1239 ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
1246 /* We should likely switch to TIOCGRS485/TIOCSRS485. */
1247 static int mxser_ioctl_op_mode(struct mxser_port *port, int index, bool set,
1248 int __user *u_opmode)
1250 int opmode, p = index % 4;
1251 int shiftbit = p * 2;
1254 if (port->board->must_hwid != MOXA_MUST_MU860_HWID)
1258 if (get_user(opmode, u_opmode))
1261 if (opmode & ~OP_MODE_MASK)
1264 spin_lock_irq(&port->slock);
1265 val = inb(port->opmode_ioaddr);
1266 val &= ~(OP_MODE_MASK << shiftbit);
1267 val |= (opmode << shiftbit);
1268 outb(val, port->opmode_ioaddr);
1269 spin_unlock_irq(&port->slock);
1274 spin_lock_irq(&port->slock);
1275 opmode = inb(port->opmode_ioaddr) >> shiftbit;
1276 spin_unlock_irq(&port->slock);
1278 return put_user(opmode & OP_MODE_MASK, u_opmode);
1281 static int mxser_ioctl(struct tty_struct *tty,
1282 unsigned int cmd, unsigned long arg)
1284 struct mxser_port *info = tty->driver_data;
1285 struct async_icount cnow;
1286 unsigned long flags;
1287 void __user *argp = (void __user *)arg;
1289 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE)
1290 return mxser_ioctl_op_mode(info, tty->index,
1291 cmd == MOXA_SET_OP_MODE, argp);
1293 if (cmd != TIOCMIWAIT && tty_io_error(tty))
1297 case TIOCSERGETLSR: /* Get line status register */
1298 return mxser_get_lsr_info(info, argp);
1300 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1301 * - mask passed in arg for lines of interest
1302 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1303 * Caller should use TIOCGICOUNT to see which one it was
1306 spin_lock_irqsave(&info->slock, flags);
1307 cnow = info->icount; /* note the counters on entry */
1308 spin_unlock_irqrestore(&info->slock, flags);
1310 return wait_event_interruptible(info->port.delta_msr_wait,
1311 mxser_cflags_changed(info, arg, &cnow));
1313 return -ENOIOCTLCMD;
1319 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1320 * Return: write counters to the user passed counter struct
1321 * NB: both 1->0 and 0->1 transitions are counted except for
1322 * RI where only 0->1 is counted.
1325 static int mxser_get_icount(struct tty_struct *tty,
1326 struct serial_icounter_struct *icount)
1329 struct mxser_port *info = tty->driver_data;
1330 struct async_icount cnow;
1331 unsigned long flags;
1333 spin_lock_irqsave(&info->slock, flags);
1334 cnow = info->icount;
1335 spin_unlock_irqrestore(&info->slock, flags);
1337 icount->frame = cnow.frame;
1338 icount->brk = cnow.brk;
1339 icount->overrun = cnow.overrun;
1340 icount->buf_overrun = cnow.buf_overrun;
1341 icount->parity = cnow.parity;
1342 icount->rx = cnow.rx;
1343 icount->tx = cnow.tx;
1344 icount->cts = cnow.cts;
1345 icount->dsr = cnow.dsr;
1346 icount->rng = cnow.rng;
1347 icount->dcd = cnow.dcd;
1351 static void mxser_stoprx(struct tty_struct *tty)
1353 struct mxser_port *info = tty->driver_data;
1355 info->ldisc_stop_rx = 1;
1357 if (info->board->must_hwid) {
1358 info->IER &= ~MOXA_MUST_RECV_ISR;
1359 outb(info->IER, info->ioaddr + UART_IER);
1361 info->x_char = STOP_CHAR(tty);
1362 outb(0, info->ioaddr + UART_IER);
1363 info->IER |= UART_IER_THRI;
1364 outb(info->IER, info->ioaddr + UART_IER);
1368 if (C_CRTSCTS(tty)) {
1369 info->MCR &= ~UART_MCR_RTS;
1370 outb(info->MCR, info->ioaddr + UART_MCR);
1375 * This routine is called by the upper-layer tty layer to signal that
1376 * incoming characters should be throttled.
1378 static void mxser_throttle(struct tty_struct *tty)
1383 static void mxser_unthrottle(struct tty_struct *tty)
1385 struct mxser_port *info = tty->driver_data;
1388 info->ldisc_stop_rx = 0;
1393 if (info->board->must_hwid) {
1394 info->IER |= MOXA_MUST_RECV_ISR;
1395 outb(info->IER, info->ioaddr + UART_IER);
1397 info->x_char = START_CHAR(tty);
1398 outb(0, info->ioaddr + UART_IER);
1399 info->IER |= UART_IER_THRI;
1400 outb(info->IER, info->ioaddr + UART_IER);
1405 if (C_CRTSCTS(tty)) {
1406 info->MCR |= UART_MCR_RTS;
1407 outb(info->MCR, info->ioaddr + UART_MCR);
1412 * mxser_stop() and mxser_start()
1414 * This routines are called before setting or resetting tty->flow.stopped.
1415 * They enable or disable transmitter interrupts, as necessary.
1417 static void mxser_stop(struct tty_struct *tty)
1419 struct mxser_port *info = tty->driver_data;
1420 unsigned long flags;
1422 spin_lock_irqsave(&info->slock, flags);
1423 if (info->IER & UART_IER_THRI) {
1424 info->IER &= ~UART_IER_THRI;
1425 outb(info->IER, info->ioaddr + UART_IER);
1427 spin_unlock_irqrestore(&info->slock, flags);
1430 static void mxser_start(struct tty_struct *tty)
1432 struct mxser_port *info = tty->driver_data;
1433 unsigned long flags;
1435 spin_lock_irqsave(&info->slock, flags);
1436 if (info->xmit_cnt && info->port.xmit_buf) {
1437 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1438 info->IER |= UART_IER_THRI;
1439 outb(info->IER, info->ioaddr + UART_IER);
1441 spin_unlock_irqrestore(&info->slock, flags);
1444 static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1446 struct mxser_port *info = tty->driver_data;
1447 unsigned long flags;
1449 spin_lock_irqsave(&info->slock, flags);
1450 mxser_change_speed(tty);
1451 spin_unlock_irqrestore(&info->slock, flags);
1453 if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty)) {
1454 tty->hw_stopped = 0;
1458 /* Handle sw stopped */
1459 if ((old_termios->c_iflag & IXON) && !I_IXON(tty)) {
1460 tty->flow.stopped = 0;
1462 if (info->board->must_hwid) {
1463 spin_lock_irqsave(&info->slock, flags);
1464 mxser_must_set_rx_sw_flow_control(info->ioaddr, false);
1465 spin_unlock_irqrestore(&info->slock, flags);
1473 * mxser_wait_until_sent() --- wait until the transmitter is empty
1475 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1477 struct mxser_port *info = tty->driver_data;
1478 unsigned long orig_jiffies, char_time;
1479 unsigned long flags;
1482 if (info->type == PORT_UNKNOWN)
1485 if (info->xmit_fifo_size == 0)
1486 return; /* Just in case.... */
1488 orig_jiffies = jiffies;
1490 * Set the check interval to be 1/5 of the estimated time to
1491 * send a single character, and make it at least 1. The check
1492 * interval should also be less than the timeout.
1494 * Note: we have to use pretty tight timings here to satisfy
1497 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1498 char_time = char_time / 5;
1501 if (timeout && timeout < char_time)
1502 char_time = timeout;
1504 * If the transmitter hasn't cleared in twice the approximate
1505 * amount of time to send the entire FIFO, it probably won't
1506 * ever clear. This assumes the UART isn't doing flow
1507 * control, which is currently the case. Hence, if it ever
1508 * takes longer than info->timeout, this is probably due to a
1509 * UART bug of some kind. So, we clamp the timeout parameter at
1512 if (!timeout || timeout > 2 * info->timeout)
1513 timeout = 2 * info->timeout;
1515 spin_lock_irqsave(&info->slock, flags);
1516 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
1517 spin_unlock_irqrestore(&info->slock, flags);
1518 schedule_timeout_interruptible(char_time);
1519 spin_lock_irqsave(&info->slock, flags);
1520 if (signal_pending(current))
1522 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1525 spin_unlock_irqrestore(&info->slock, flags);
1526 set_current_state(TASK_RUNNING);
1530 * This routine is called by tty_hangup() when a hangup is signaled.
1532 static void mxser_hangup(struct tty_struct *tty)
1534 struct mxser_port *info = tty->driver_data;
1536 mxser_flush_buffer(tty);
1537 tty_port_hangup(&info->port);
1541 * mxser_rs_break() --- routine which turns the break handling on or off
1543 static int mxser_rs_break(struct tty_struct *tty, int break_state)
1545 struct mxser_port *info = tty->driver_data;
1546 unsigned long flags;
1549 spin_lock_irqsave(&info->slock, flags);
1550 lcr = inb(info->ioaddr + UART_LCR);
1551 if (break_state == -1)
1552 lcr |= UART_LCR_SBC;
1554 lcr &= ~UART_LCR_SBC;
1555 outb(lcr, info->ioaddr + UART_LCR);
1556 spin_unlock_irqrestore(&info->slock, flags);
1561 static bool mxser_receive_chars_new(struct tty_struct *tty,
1562 struct mxser_port *port, u8 status)
1564 enum mxser_must_hwid hwid = port->board->must_hwid;
1567 if (hwid == MOXA_OTHER_UART)
1569 if (status & UART_LSR_BRK_ERROR_BITS)
1571 if (hwid == MOXA_MUST_MU860_HWID && (status & MOXA_MUST_LSR_RERR))
1573 if (status & MOXA_MUST_LSR_RERR)
1576 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
1577 if (hwid == MOXA_MUST_MU150_HWID)
1578 gdl &= MOXA_MUST_GDL_MASK;
1580 if (gdl >= tty->receive_room && !port->ldisc_stop_rx)
1584 u8 ch = inb(port->ioaddr + UART_RX);
1585 tty_insert_flip_char(&port->port, ch, 0);
1591 static u8 mxser_receive_chars_old(struct tty_struct *tty,
1592 struct mxser_port *port, u8 status)
1594 enum mxser_must_hwid hwid = port->board->must_hwid;
1595 int recv_room = tty->receive_room;
1605 ch = inb(port->ioaddr + UART_RX);
1606 if (hwid && (status & UART_LSR_OE))
1607 outb(0x23, port->ioaddr + UART_FCR);
1608 status &= port->read_status_mask;
1609 if (status & port->ignore_status_mask) {
1610 if (++ignored > 100)
1614 if (status & UART_LSR_BRK_ERROR_BITS) {
1615 if (status & UART_LSR_BI) {
1619 if (port->port.flags & ASYNC_SAK)
1621 } else if (status & UART_LSR_PE) {
1623 port->icount.parity++;
1624 } else if (status & UART_LSR_FE) {
1626 port->icount.frame++;
1627 } else if (status & UART_LSR_OE) {
1629 port->icount.overrun++;
1632 tty_insert_flip_char(&port->port, ch, flag);
1634 if (cnt >= recv_room) {
1635 if (!port->ldisc_stop_rx)
1645 status = inb(port->ioaddr + UART_LSR);
1646 } while (status & UART_LSR_DR);
1651 static u8 mxser_receive_chars(struct tty_struct *tty,
1652 struct mxser_port *port, u8 status)
1654 if (tty->receive_room == 0 && !port->ldisc_stop_rx)
1657 if (!mxser_receive_chars_new(tty, port, status))
1658 status = mxser_receive_chars_old(tty, port, status);
1660 tty_flip_buffer_push(&port->port);
1665 static void mxser_transmit_chars(struct tty_struct *tty, struct mxser_port *port)
1670 outb(port->x_char, port->ioaddr + UART_TX);
1676 if (port->port.xmit_buf == NULL)
1679 if (port->xmit_cnt <= 0 || tty->flow.stopped ||
1681 (port->type != PORT_16550A) &&
1682 (!port->board->must_hwid))) {
1683 port->IER &= ~UART_IER_THRI;
1684 outb(port->IER, port->ioaddr + UART_IER);
1688 cnt = port->xmit_cnt;
1689 count = port->xmit_fifo_size;
1691 outb(port->port.xmit_buf[port->xmit_tail++],
1692 port->ioaddr + UART_TX);
1693 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
1694 if (--port->xmit_cnt <= 0)
1696 } while (--count > 0);
1698 port->icount.tx += (cnt - port->xmit_cnt);
1700 if (port->xmit_cnt < WAKEUP_CHARS)
1703 if (port->xmit_cnt <= 0) {
1704 port->IER &= ~UART_IER_THRI;
1705 outb(port->IER, port->ioaddr + UART_IER);
1709 static bool mxser_port_isr(struct mxser_port *port)
1711 struct tty_struct *tty;
1712 u8 iir, msr, status;
1715 iir = inb(port->ioaddr + UART_IIR);
1716 if (iir & UART_IIR_NO_INT)
1719 iir &= MOXA_MUST_IIR_MASK;
1720 tty = tty_port_tty_get(&port->port);
1721 if (!tty || port->closing || !tty_port_initialized(&port->port)) {
1722 status = inb(port->ioaddr + UART_LSR);
1723 outb(0x27, port->ioaddr + UART_FCR);
1724 inb(port->ioaddr + UART_MSR);
1730 status = inb(port->ioaddr + UART_LSR);
1732 if (port->board->must_hwid) {
1733 if (iir == MOXA_MUST_IIR_GDA ||
1734 iir == MOXA_MUST_IIR_RDA ||
1735 iir == MOXA_MUST_IIR_RTO ||
1736 iir == MOXA_MUST_IIR_LSR)
1737 status = mxser_receive_chars(tty, port, status);
1739 status &= port->read_status_mask;
1740 if (status & UART_LSR_DR)
1741 status = mxser_receive_chars(tty, port, status);
1744 msr = inb(port->ioaddr + UART_MSR);
1745 if (msr & UART_MSR_ANY_DELTA)
1746 mxser_check_modem_status(tty, port, msr);
1748 if (port->board->must_hwid) {
1749 if (iir == 0x02 && (status & UART_LSR_THRE))
1750 mxser_transmit_chars(tty, port);
1752 if (status & UART_LSR_THRE)
1753 mxser_transmit_chars(tty, port);
1763 * This is the serial driver's generic interrupt routine
1765 static irqreturn_t mxser_interrupt(int irq, void *dev_id)
1767 struct mxser_board *brd = dev_id;
1768 struct mxser_port *port;
1769 unsigned int int_cnt, pass_counter = 0;
1770 unsigned int i, max = brd->nports;
1771 int handled = IRQ_NONE;
1772 u8 irqbits, bits, mask = BIT(max) - 1;
1774 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
1775 irqbits = inb(brd->vector) & mask;
1776 if (irqbits == mask)
1779 handled = IRQ_HANDLED;
1780 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
1781 if (irqbits == mask)
1785 port = &brd->ports[i];
1788 spin_lock(&port->slock);
1790 if (mxser_port_isr(port))
1792 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
1793 spin_unlock(&port->slock);
1800 static const struct tty_operations mxser_ops = {
1802 .close = mxser_close,
1803 .write = mxser_write,
1804 .put_char = mxser_put_char,
1805 .flush_chars = mxser_flush_chars,
1806 .write_room = mxser_write_room,
1807 .chars_in_buffer = mxser_chars_in_buffer,
1808 .flush_buffer = mxser_flush_buffer,
1809 .ioctl = mxser_ioctl,
1810 .throttle = mxser_throttle,
1811 .unthrottle = mxser_unthrottle,
1812 .set_termios = mxser_set_termios,
1814 .start = mxser_start,
1815 .hangup = mxser_hangup,
1816 .break_ctl = mxser_rs_break,
1817 .wait_until_sent = mxser_wait_until_sent,
1818 .tiocmget = mxser_tiocmget,
1819 .tiocmset = mxser_tiocmset,
1820 .set_serial = mxser_set_serial_info,
1821 .get_serial = mxser_get_serial_info,
1822 .get_icount = mxser_get_icount,
1825 static const struct tty_port_operations mxser_port_ops = {
1826 .carrier_raised = mxser_carrier_raised,
1827 .dtr_rts = mxser_dtr_rts,
1828 .activate = mxser_activate,
1829 .shutdown = mxser_shutdown_port,
1833 * The MOXA Smartio/Industio serial driver boot-time initialization code!
1836 static void mxser_initbrd(struct mxser_board *brd, bool high_baud)
1838 struct mxser_port *info;
1842 brd->must_hwid = mxser_must_get_hwid(brd->ports[0].ioaddr);
1843 is_mu860 = brd->must_hwid == MOXA_MUST_MU860_HWID;
1845 for (i = 0; i < UART_INFO_NUM; i++) {
1846 if (Gpci_uart_info[i].type == brd->must_hwid) {
1847 brd->max_baud = Gpci_uart_info[i].max_baud;
1849 /* exception....CP-102 */
1851 brd->max_baud = 921600;
1857 /* set to RS232 mode by default */
1858 outb(0, brd->vector + 4);
1859 outb(0, brd->vector + 0x0c);
1862 for (i = 0; i < brd->nports; i++) {
1863 info = &brd->ports[i];
1866 info->opmode_ioaddr = brd->vector + 4;
1868 info->opmode_ioaddr = brd->vector + 0x0c;
1870 tty_port_init(&info->port);
1871 info->port.ops = &mxser_port_ops;
1873 info->ldisc_stop_rx = 0;
1875 /* Enhance mode enabled here */
1876 if (brd->must_hwid != MOXA_OTHER_UART)
1877 mxser_must_set_enhance_mode(info->ioaddr, true);
1879 info->type = PORT_16550A;
1881 mxser_process_txrx_fifo(info);
1883 info->port.close_delay = 5 * HZ / 10;
1884 info->port.closing_wait = 30 * HZ;
1885 spin_lock_init(&info->slock);
1887 /* before set INT ISR, disable all int */
1888 outb(inb(info->ioaddr + UART_IER) & 0xf0,
1889 info->ioaddr + UART_IER);
1893 static int mxser_probe(struct pci_dev *pdev,
1894 const struct pci_device_id *ent)
1896 struct mxser_board *brd;
1897 unsigned int i, base;
1898 unsigned long ioaddress;
1899 unsigned short nports = MXSER_NPORTS(ent->driver_data);
1900 struct device *tty_dev;
1901 int retval = -EINVAL;
1903 i = find_first_zero_bit(mxser_boards, MXSER_BOARDS);
1904 if (i >= MXSER_BOARDS) {
1905 dev_err(&pdev->dev, "too many boards found (maximum %d), board "
1906 "not configured\n", MXSER_BOARDS);
1910 brd = devm_kzalloc(&pdev->dev, struct_size(brd, ports, nports),
1916 __set_bit(brd->idx, mxser_boards);
1917 base = i * MXSER_PORTS_PER_BOARD;
1919 retval = pcim_enable_device(pdev);
1921 dev_err(&pdev->dev, "PCI enable failed\n");
1926 ioaddress = pci_resource_start(pdev, 2);
1927 retval = pci_request_region(pdev, 2, "mxser(IO)");
1931 brd->nports = nports;
1932 for (i = 0; i < nports; i++)
1933 brd->ports[i].ioaddr = ioaddress + 8 * i;
1936 ioaddress = pci_resource_start(pdev, 3);
1937 retval = pci_request_region(pdev, 3, "mxser(vector)");
1940 brd->vector = ioaddress;
1943 brd->irq = pdev->irq;
1945 mxser_initbrd(brd, ent->driver_data & MXSER_HIGHBAUD);
1947 retval = devm_request_irq(&pdev->dev, brd->irq, mxser_interrupt,
1948 IRQF_SHARED, "mxser", brd);
1950 dev_err(&pdev->dev, "request irq failed");
1954 for (i = 0; i < nports; i++) {
1955 tty_dev = tty_port_register_device(&brd->ports[i].port,
1956 mxvar_sdriver, base + i, &pdev->dev);
1957 if (IS_ERR(tty_dev)) {
1958 retval = PTR_ERR(tty_dev);
1960 tty_unregister_device(mxvar_sdriver,
1966 pci_set_drvdata(pdev, brd);
1970 for (i = 0; i < nports; i++)
1971 tty_port_destroy(&brd->ports[i].port);
1973 __clear_bit(brd->idx, mxser_boards);
1978 static void mxser_remove(struct pci_dev *pdev)
1980 struct mxser_board *brd = pci_get_drvdata(pdev);
1981 unsigned int i, base = brd->idx * MXSER_PORTS_PER_BOARD;
1983 for (i = 0; i < brd->nports; i++) {
1984 tty_unregister_device(mxvar_sdriver, base + i);
1985 tty_port_destroy(&brd->ports[i].port);
1988 __clear_bit(brd->idx, mxser_boards);
1991 static struct pci_driver mxser_driver = {
1993 .id_table = mxser_pcibrds,
1994 .probe = mxser_probe,
1995 .remove = mxser_remove
1998 static int __init mxser_module_init(void)
2002 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS);
2006 /* Initialize the tty_driver structure */
2007 mxvar_sdriver->name = "ttyMI";
2008 mxvar_sdriver->major = ttymajor;
2009 mxvar_sdriver->minor_start = 0;
2010 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2011 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2012 mxvar_sdriver->init_termios = tty_std_termios;
2013 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2014 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2015 tty_set_operations(mxvar_sdriver, &mxser_ops);
2017 retval = tty_register_driver(mxvar_sdriver);
2019 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2024 retval = pci_register_driver(&mxser_driver);
2026 printk(KERN_ERR "mxser: can't register pci driver\n");
2032 tty_unregister_driver(mxvar_sdriver);
2034 put_tty_driver(mxvar_sdriver);
2038 static void __exit mxser_module_exit(void)
2040 pci_unregister_driver(&mxser_driver);
2041 tty_unregister_driver(mxvar_sdriver);
2042 put_tty_driver(mxvar_sdriver);
2045 module_init(mxser_module_init);
2046 module_exit(mxser_module_exit);