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@redhat.com>. The original 1.8 code is available on www.moxa.com.
18 * - Fixed x86_64 cleanness
21 #include <linux/module.h>
22 #include <linux/errno.h>
23 #include <linux/signal.h>
24 #include <linux/sched.h>
25 #include <linux/timer.h>
26 #include <linux/interrupt.h>
27 #include <linux/tty.h>
28 #include <linux/tty_flip.h>
29 #include <linux/serial.h>
30 #include <linux/serial_reg.h>
31 #include <linux/major.h>
32 #include <linux/string.h>
33 #include <linux/fcntl.h>
34 #include <linux/ptrace.h>
35 #include <linux/gfp.h>
36 #include <linux/ioport.h>
38 #include <linux/delay.h>
39 #include <linux/pci.h>
40 #include <linux/bitops.h>
42 #include <asm/system.h>
45 #include <asm/uaccess.h>
49 #define MXSER_VERSION "2.0.4" /* 1.12 */
50 #define MXSERMAJOR 174
52 #define MXSER_BOARDS 4 /* Max. boards */
53 #define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
54 #define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
55 #define MXSER_ISR_PASS_LIMIT 100
57 /*CheckIsMoxaMust return value*/
58 #define MOXA_OTHER_UART 0x00
59 #define MOXA_MUST_MU150_HWID 0x01
60 #define MOXA_MUST_MU860_HWID 0x02
62 #define WAKEUP_CHARS 256
64 #define UART_MCR_AFE 0x20
65 #define UART_LSR_SPECIAL 0x1E
67 #define PCI_DEVICE_ID_POS104UL 0x1044
68 #define PCI_DEVICE_ID_CB108 0x1080
69 #define PCI_DEVICE_ID_CP102UF 0x1023
70 #define PCI_DEVICE_ID_CB114 0x1142
71 #define PCI_DEVICE_ID_CP114UL 0x1143
72 #define PCI_DEVICE_ID_CB134I 0x1341
73 #define PCI_DEVICE_ID_CP138U 0x1380
76 #define C168_ASIC_ID 1
77 #define C104_ASIC_ID 2
78 #define C102_ASIC_ID 0xB
79 #define CI132_ASIC_ID 4
80 #define CI134_ASIC_ID 3
81 #define CI104J_ASIC_ID 5
83 #define MXSER_HIGHBAUD 1
86 /* This is only for PCI */
96 } Gpci_uart_info[] = {
97 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
98 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
99 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
101 #define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
103 struct mxser_cardinfo {
109 static const struct mxser_cardinfo mxser_cards[] = {
110 /* 0*/ { "C168 series", 8, },
111 { "C104 series", 4, },
112 { "CI-104J series", 4, },
113 { "C168H/PCI series", 8, },
114 { "C104H/PCI series", 4, },
115 /* 5*/ { "C102 series", 4, MXSER_HAS2 }, /* C102-ISA */
116 { "CI-132 series", 4, MXSER_HAS2 },
117 { "CI-134 series", 4, },
118 { "CP-132 series", 2, },
119 { "CP-114 series", 4, },
120 /*10*/ { "CT-114 series", 4, },
121 { "CP-102 series", 2, MXSER_HIGHBAUD },
122 { "CP-104U series", 4, },
123 { "CP-168U series", 8, },
124 { "CP-132U series", 2, },
125 /*15*/ { "CP-134U series", 4, },
126 { "CP-104JU series", 4, },
127 { "Moxa UC7000 Serial", 8, }, /* RC7000 */
128 { "CP-118U series", 8, },
129 { "CP-102UL series", 2, },
130 /*20*/ { "CP-102U series", 2, },
131 { "CP-118EL series", 8, },
132 { "CP-168EL series", 8, },
133 { "CP-104EL series", 4, },
134 { "CB-108 series", 8, },
135 /*25*/ { "CB-114 series", 4, },
136 { "CB-134I series", 4, },
137 { "CP-138U series", 8, },
138 { "POS-104UL series", 4, },
139 { "CP-114UL series", 4, },
140 /*30*/ { "CP-102UF series", 2, }
143 /* driver_data correspond to the lines in the structure above
144 see also ISA probe function before you change something */
145 static struct pci_device_id mxser_pcibrds[] = {
146 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168), .driver_data = 3 },
147 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104), .driver_data = 4 },
148 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132), .driver_data = 8 },
149 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114), .driver_data = 9 },
150 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114), .driver_data = 10 },
151 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102), .driver_data = 11 },
152 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 12 },
153 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 13 },
154 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 14 },
155 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 15 },
156 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 16 },
157 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 17 },
158 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 18 },
159 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 19 },
160 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 20 },
161 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 21 },
162 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 22 },
163 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 23 },
164 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 24 },
165 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 25 },
166 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 26 },
167 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 27 },
168 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 28 },
169 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP114UL), .driver_data = 29 },
170 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP102UF), .driver_data = 30 },
173 MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
175 static unsigned long ioaddr[MXSER_BOARDS];
176 static int ttymajor = MXSERMAJOR;
178 /* Variables for insmod */
180 MODULE_AUTHOR("Casper Yang");
181 MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
182 module_param_array(ioaddr, ulong, NULL, 0);
183 MODULE_PARM_DESC(ioaddr, "ISA io addresses to look for a moxa board");
184 module_param(ttymajor, int, 0);
185 MODULE_LICENSE("GPL");
189 unsigned long rxcnt[MXSER_PORTS];
190 unsigned long txcnt[MXSER_PORTS];
196 unsigned long up_rxcnt;
197 unsigned long up_txcnt;
199 unsigned char hold_reason;
202 struct mxser_mon_ext {
203 unsigned long rx_cnt[32];
204 unsigned long tx_cnt[32];
205 unsigned long up_rxcnt[32];
206 unsigned long up_txcnt[32];
207 int modem_status[32];
221 struct tty_port port;
222 struct mxser_board *board;
224 unsigned long ioaddr;
225 unsigned long opmode_ioaddr;
229 int rx_trigger; /* Rx fifo trigger level */
231 int baud_base; /* max. speed */
232 int type; /* UART type */
234 int x_char; /* xon/xoff character */
235 int IER; /* Interrupt Enable Register */
236 int MCR; /* Modem control register */
238 unsigned char stop_rx;
239 unsigned char ldisc_stop_rx;
242 unsigned char err_shadow;
244 struct async_icount icount; /* kernel counters for 4 input interrupts */
247 int read_status_mask;
248 int ignore_status_mask;
254 struct ktermios normal_termios;
256 struct mxser_mon mon_data;
259 wait_queue_head_t delta_msr_wait;
265 const struct mxser_cardinfo *info;
266 unsigned long vector;
267 unsigned long vector_mask;
272 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
275 struct mxser_mstatus {
283 static struct mxser_board mxser_boards[MXSER_BOARDS];
284 static struct tty_driver *mxvar_sdriver;
285 static struct mxser_log mxvar_log;
286 static int mxser_set_baud_method[MXSER_PORTS + 1];
288 static void mxser_enable_must_enchance_mode(unsigned long baseio)
293 oldlcr = inb(baseio + UART_LCR);
294 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
296 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
297 efr |= MOXA_MUST_EFR_EFRB_ENABLE;
299 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
300 outb(oldlcr, baseio + UART_LCR);
303 static void mxser_disable_must_enchance_mode(unsigned long baseio)
308 oldlcr = inb(baseio + UART_LCR);
309 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
311 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
312 efr &= ~MOXA_MUST_EFR_EFRB_ENABLE;
314 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
315 outb(oldlcr, baseio + UART_LCR);
318 static void mxser_set_must_xon1_value(unsigned long baseio, u8 value)
323 oldlcr = inb(baseio + UART_LCR);
324 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
326 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
327 efr &= ~MOXA_MUST_EFR_BANK_MASK;
328 efr |= MOXA_MUST_EFR_BANK0;
330 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
331 outb(value, baseio + MOXA_MUST_XON1_REGISTER);
332 outb(oldlcr, baseio + UART_LCR);
335 static void mxser_set_must_xoff1_value(unsigned long baseio, u8 value)
340 oldlcr = inb(baseio + UART_LCR);
341 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
343 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
344 efr &= ~MOXA_MUST_EFR_BANK_MASK;
345 efr |= MOXA_MUST_EFR_BANK0;
347 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
348 outb(value, baseio + MOXA_MUST_XOFF1_REGISTER);
349 outb(oldlcr, baseio + UART_LCR);
352 static void mxser_set_must_fifo_value(struct mxser_port *info)
357 oldlcr = inb(info->ioaddr + UART_LCR);
358 outb(MOXA_MUST_ENTER_ENCHANCE, info->ioaddr + UART_LCR);
360 efr = inb(info->ioaddr + MOXA_MUST_EFR_REGISTER);
361 efr &= ~MOXA_MUST_EFR_BANK_MASK;
362 efr |= MOXA_MUST_EFR_BANK1;
364 outb(efr, info->ioaddr + MOXA_MUST_EFR_REGISTER);
365 outb((u8)info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTH_REGISTER);
366 outb((u8)info->rx_trigger, info->ioaddr + MOXA_MUST_RBRTI_REGISTER);
367 outb((u8)info->rx_low_water, info->ioaddr + MOXA_MUST_RBRTL_REGISTER);
368 outb(oldlcr, info->ioaddr + UART_LCR);
371 static void mxser_set_must_enum_value(unsigned long baseio, u8 value)
376 oldlcr = inb(baseio + UART_LCR);
377 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
379 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
380 efr &= ~MOXA_MUST_EFR_BANK_MASK;
381 efr |= MOXA_MUST_EFR_BANK2;
383 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
384 outb(value, baseio + MOXA_MUST_ENUM_REGISTER);
385 outb(oldlcr, baseio + UART_LCR);
388 static void mxser_get_must_hardware_id(unsigned long baseio, u8 *pId)
393 oldlcr = inb(baseio + UART_LCR);
394 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
396 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
397 efr &= ~MOXA_MUST_EFR_BANK_MASK;
398 efr |= MOXA_MUST_EFR_BANK2;
400 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
401 *pId = inb(baseio + MOXA_MUST_HWID_REGISTER);
402 outb(oldlcr, baseio + UART_LCR);
405 static void SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(unsigned long baseio)
410 oldlcr = inb(baseio + UART_LCR);
411 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
413 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
414 efr &= ~MOXA_MUST_EFR_SF_MASK;
416 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
417 outb(oldlcr, baseio + UART_LCR);
420 static void mxser_enable_must_tx_software_flow_control(unsigned long baseio)
425 oldlcr = inb(baseio + UART_LCR);
426 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
428 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
429 efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
430 efr |= MOXA_MUST_EFR_SF_TX1;
432 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
433 outb(oldlcr, baseio + UART_LCR);
436 static void mxser_disable_must_tx_software_flow_control(unsigned long baseio)
441 oldlcr = inb(baseio + UART_LCR);
442 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
444 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
445 efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
447 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
448 outb(oldlcr, baseio + UART_LCR);
451 static void mxser_enable_must_rx_software_flow_control(unsigned long baseio)
456 oldlcr = inb(baseio + UART_LCR);
457 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
459 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
460 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
461 efr |= MOXA_MUST_EFR_SF_RX1;
463 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
464 outb(oldlcr, baseio + UART_LCR);
467 static void mxser_disable_must_rx_software_flow_control(unsigned long baseio)
472 oldlcr = inb(baseio + UART_LCR);
473 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
475 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
476 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
478 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
479 outb(oldlcr, baseio + UART_LCR);
483 static int __devinit CheckIsMoxaMust(unsigned long io)
488 outb(0, io + UART_LCR);
489 mxser_disable_must_enchance_mode(io);
490 oldmcr = inb(io + UART_MCR);
491 outb(0, io + UART_MCR);
492 mxser_set_must_xon1_value(io, 0x11);
493 if ((hwid = inb(io + UART_MCR)) != 0) {
494 outb(oldmcr, io + UART_MCR);
495 return MOXA_OTHER_UART;
498 mxser_get_must_hardware_id(io, &hwid);
499 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
500 if (hwid == Gpci_uart_info[i].type)
503 return MOXA_OTHER_UART;
507 static void process_txrx_fifo(struct mxser_port *info)
511 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
512 info->rx_trigger = 1;
513 info->rx_high_water = 1;
514 info->rx_low_water = 1;
515 info->xmit_fifo_size = 1;
517 for (i = 0; i < UART_INFO_NUM; i++)
518 if (info->board->chip_flag == Gpci_uart_info[i].type) {
519 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
520 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
521 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
522 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
527 static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
529 static unsigned char mxser_msr[MXSER_PORTS + 1];
530 unsigned char status = 0;
532 status = inb(baseaddr + UART_MSR);
534 mxser_msr[port] &= 0x0F;
535 mxser_msr[port] |= status;
536 status = mxser_msr[port];
543 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
544 struct mxser_port *port)
546 DECLARE_WAITQUEUE(wait, current);
552 * If non-blocking mode is set, or the port is not enabled,
553 * then make the check up front and then exit.
555 if ((filp->f_flags & O_NONBLOCK) ||
556 test_bit(TTY_IO_ERROR, &tty->flags)) {
557 port->port.flags |= ASYNC_NORMAL_ACTIVE;
561 if (tty->termios->c_cflag & CLOCAL)
565 * Block waiting for the carrier detect and the line to become
566 * free (i.e., not in use by the callout). While we are in
567 * this loop, port->port.count is dropped by one, so that
568 * mxser_close() knows when to free things. We restore it upon
569 * exit, either normal or abnormal.
572 add_wait_queue(&port->port.open_wait, &wait);
574 spin_lock_irqsave(&port->slock, flags);
575 if (!tty_hung_up_p(filp))
577 spin_unlock_irqrestore(&port->slock, flags);
578 port->port.blocked_open++;
580 spin_lock_irqsave(&port->slock, flags);
581 outb(inb(port->ioaddr + UART_MCR) |
582 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
583 spin_unlock_irqrestore(&port->slock, flags);
584 set_current_state(TASK_INTERRUPTIBLE);
585 if (tty_hung_up_p(filp) || !(port->port.flags & ASYNC_INITIALIZED)) {
586 if (port->port.flags & ASYNC_HUP_NOTIFY)
589 retval = -ERESTARTSYS;
592 if (!(port->port.flags & ASYNC_CLOSING) &&
594 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
596 if (signal_pending(current)) {
597 retval = -ERESTARTSYS;
602 set_current_state(TASK_RUNNING);
603 remove_wait_queue(&port->port.open_wait, &wait);
604 if (!tty_hung_up_p(filp))
606 port->port.blocked_open--;
609 port->port.flags |= ASYNC_NORMAL_ACTIVE;
613 static int mxser_set_baud(struct tty_struct *tty, long newspd)
615 struct mxser_port *info = tty->driver_data;
625 if (newspd > info->max_baud)
629 quot = 2 * info->baud_base / 269;
630 tty_encode_baud_rate(tty, 134, 134);
632 quot = info->baud_base / newspd;
635 baud = info->baud_base/quot;
636 tty_encode_baud_rate(tty, baud, baud);
641 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
642 info->timeout += HZ / 50; /* Add .02 seconds of slop */
645 info->MCR |= UART_MCR_DTR;
646 outb(info->MCR, info->ioaddr + UART_MCR);
648 info->MCR &= ~UART_MCR_DTR;
649 outb(info->MCR, info->ioaddr + UART_MCR);
653 cval = inb(info->ioaddr + UART_LCR);
655 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
657 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
658 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
659 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
662 if (C_BAUD(tty) == BOTHER) {
663 quot = info->baud_base % newspd;
665 if (quot % newspd > newspd / 2) {
671 mxser_set_must_enum_value(info->ioaddr, quot);
674 mxser_set_must_enum_value(info->ioaddr, 0);
680 * This routine is called to set the UART divisor registers to match
681 * the specified baud rate for a serial port.
683 static int mxser_change_speed(struct tty_struct *tty,
684 struct ktermios *old_termios)
686 struct mxser_port *info = tty->driver_data;
687 unsigned cflag, cval, fcr;
689 unsigned char status;
693 cflag = tty->termios->c_cflag;
697 if (mxser_set_baud_method[tty->index] == 0)
698 mxser_set_baud(tty, tty_get_baud_rate(tty));
700 /* byte size and parity */
701 switch (cflag & CSIZE) {
716 break; /* too keep GCC shut... */
721 cval |= UART_LCR_PARITY;
722 if (!(cflag & PARODD))
723 cval |= UART_LCR_EPAR;
725 cval |= UART_LCR_SPAR;
727 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
728 if (info->board->chip_flag) {
729 fcr = UART_FCR_ENABLE_FIFO;
730 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
731 mxser_set_must_fifo_value(info);
735 fcr = UART_FCR_ENABLE_FIFO;
736 if (info->board->chip_flag) {
737 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
738 mxser_set_must_fifo_value(info);
740 switch (info->rx_trigger) {
742 fcr |= UART_FCR_TRIGGER_1;
745 fcr |= UART_FCR_TRIGGER_4;
748 fcr |= UART_FCR_TRIGGER_8;
751 fcr |= UART_FCR_TRIGGER_14;
757 /* CTS flow control flag and modem status interrupts */
758 info->IER &= ~UART_IER_MSI;
759 info->MCR &= ~UART_MCR_AFE;
760 if (cflag & CRTSCTS) {
761 info->port.flags |= ASYNC_CTS_FLOW;
762 info->IER |= UART_IER_MSI;
763 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
764 info->MCR |= UART_MCR_AFE;
766 status = inb(info->ioaddr + UART_MSR);
767 if (tty->hw_stopped) {
768 if (status & UART_MSR_CTS) {
770 if (info->type != PORT_16550A &&
771 !info->board->chip_flag) {
772 outb(info->IER & ~UART_IER_THRI,
775 info->IER |= UART_IER_THRI;
776 outb(info->IER, info->ioaddr +
782 if (!(status & UART_MSR_CTS)) {
784 if ((info->type != PORT_16550A) &&
785 (!info->board->chip_flag)) {
786 info->IER &= ~UART_IER_THRI;
787 outb(info->IER, info->ioaddr +
794 info->port.flags &= ~ASYNC_CTS_FLOW;
796 outb(info->MCR, info->ioaddr + UART_MCR);
797 if (cflag & CLOCAL) {
798 info->port.flags &= ~ASYNC_CHECK_CD;
800 info->port.flags |= ASYNC_CHECK_CD;
801 info->IER |= UART_IER_MSI;
803 outb(info->IER, info->ioaddr + UART_IER);
806 * Set up parity check flag
808 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
810 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
811 if (I_BRKINT(tty) || I_PARMRK(tty))
812 info->read_status_mask |= UART_LSR_BI;
814 info->ignore_status_mask = 0;
817 info->ignore_status_mask |= UART_LSR_BI;
818 info->read_status_mask |= UART_LSR_BI;
820 * If we're ignore parity and break indicators, ignore
821 * overruns too. (For real raw support).
824 info->ignore_status_mask |=
828 info->read_status_mask |=
834 if (info->board->chip_flag) {
835 mxser_set_must_xon1_value(info->ioaddr, START_CHAR(tty));
836 mxser_set_must_xoff1_value(info->ioaddr, STOP_CHAR(tty));
838 mxser_enable_must_rx_software_flow_control(
841 mxser_disable_must_rx_software_flow_control(
845 mxser_enable_must_tx_software_flow_control(
848 mxser_disable_must_tx_software_flow_control(
854 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
855 outb(cval, info->ioaddr + UART_LCR);
860 static void mxser_check_modem_status(struct tty_struct *tty,
861 struct mxser_port *port, int status)
863 /* update input line counters */
864 if (status & UART_MSR_TERI)
866 if (status & UART_MSR_DDSR)
868 if (status & UART_MSR_DDCD)
870 if (status & UART_MSR_DCTS)
872 port->mon_data.modem_status = status;
873 wake_up_interruptible(&port->delta_msr_wait);
875 if ((port->port.flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
876 if (status & UART_MSR_DCD)
877 wake_up_interruptible(&port->port.open_wait);
880 tty = tty_port_tty_get(&port->port);
881 if (port->port.flags & ASYNC_CTS_FLOW) {
882 if (tty->hw_stopped) {
883 if (status & UART_MSR_CTS) {
886 if ((port->type != PORT_16550A) &&
887 (!port->board->chip_flag)) {
888 outb(port->IER & ~UART_IER_THRI,
889 port->ioaddr + UART_IER);
890 port->IER |= UART_IER_THRI;
891 outb(port->IER, port->ioaddr +
897 if (!(status & UART_MSR_CTS)) {
899 if (port->type != PORT_16550A &&
900 !port->board->chip_flag) {
901 port->IER &= ~UART_IER_THRI;
902 outb(port->IER, port->ioaddr +
910 static int mxser_startup(struct tty_struct *tty)
912 struct mxser_port *info = tty->driver_data;
916 page = __get_free_page(GFP_KERNEL);
920 spin_lock_irqsave(&info->slock, flags);
922 if (info->port.flags & ASYNC_INITIALIZED) {
924 spin_unlock_irqrestore(&info->slock, flags);
928 if (!info->ioaddr || !info->type) {
929 set_bit(TTY_IO_ERROR, &tty->flags);
931 spin_unlock_irqrestore(&info->slock, flags);
934 if (info->port.xmit_buf)
937 info->port.xmit_buf = (unsigned char *) page;
940 * Clear the FIFO buffers and disable them
941 * (they will be reenabled in mxser_change_speed())
943 if (info->board->chip_flag)
944 outb((UART_FCR_CLEAR_RCVR |
945 UART_FCR_CLEAR_XMIT |
946 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
948 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
949 info->ioaddr + UART_FCR);
952 * At this point there's no way the LSR could still be 0xFF;
953 * if it is, then bail out, because there's likely no UART
956 if (inb(info->ioaddr + UART_LSR) == 0xff) {
957 spin_unlock_irqrestore(&info->slock, flags);
958 if (capable(CAP_SYS_ADMIN)) {
960 set_bit(TTY_IO_ERROR, &tty->flags);
967 * Clear the interrupt registers.
969 (void) inb(info->ioaddr + UART_LSR);
970 (void) inb(info->ioaddr + UART_RX);
971 (void) inb(info->ioaddr + UART_IIR);
972 (void) inb(info->ioaddr + UART_MSR);
975 * Now, initialize the UART
977 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
978 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
979 outb(info->MCR, info->ioaddr + UART_MCR);
982 * Finally, enable interrupts
984 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
986 if (info->board->chip_flag)
987 info->IER |= MOXA_MUST_IER_EGDAI;
988 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
991 * And clear the interrupt registers again for luck.
993 (void) inb(info->ioaddr + UART_LSR);
994 (void) inb(info->ioaddr + UART_RX);
995 (void) inb(info->ioaddr + UART_IIR);
996 (void) inb(info->ioaddr + UART_MSR);
998 clear_bit(TTY_IO_ERROR, &tty->flags);
999 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1002 * and set the speed of the serial port
1004 mxser_change_speed(tty, NULL);
1005 info->port.flags |= ASYNC_INITIALIZED;
1006 spin_unlock_irqrestore(&info->slock, flags);
1012 * This routine will shutdown a serial port; interrupts maybe disabled, and
1013 * DTR is dropped if the hangup on close termio flag is on.
1015 static void mxser_shutdown(struct tty_struct *tty)
1017 struct mxser_port *info = tty->driver_data;
1018 unsigned long flags;
1020 if (!(info->port.flags & ASYNC_INITIALIZED))
1023 spin_lock_irqsave(&info->slock, flags);
1026 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1027 * here so the queue might never be waken up
1029 wake_up_interruptible(&info->delta_msr_wait);
1032 * Free the IRQ, if necessary
1034 if (info->port.xmit_buf) {
1035 free_page((unsigned long) info->port.xmit_buf);
1036 info->port.xmit_buf = NULL;
1040 outb(0x00, info->ioaddr + UART_IER);
1042 if (tty->termios->c_cflag & HUPCL)
1043 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
1044 outb(info->MCR, info->ioaddr + UART_MCR);
1046 /* clear Rx/Tx FIFO's */
1047 if (info->board->chip_flag)
1048 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
1049 MOXA_MUST_FCR_GDA_MODE_ENABLE,
1050 info->ioaddr + UART_FCR);
1052 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
1053 info->ioaddr + UART_FCR);
1055 /* read data port to reset things */
1056 (void) inb(info->ioaddr + UART_RX);
1058 set_bit(TTY_IO_ERROR, &tty->flags);
1060 info->port.flags &= ~ASYNC_INITIALIZED;
1062 if (info->board->chip_flag)
1063 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
1065 spin_unlock_irqrestore(&info->slock, flags);
1069 * This routine is called whenever a serial port is opened. It
1070 * enables interrupts for a serial port, linking in its async structure into
1071 * the IRQ chain. It also performs the serial-specific
1072 * initialization for the tty structure.
1074 static int mxser_open(struct tty_struct *tty, struct file *filp)
1076 struct mxser_port *info;
1077 unsigned long flags;
1081 if (line == MXSER_PORTS)
1083 if (line < 0 || line > MXSER_PORTS)
1085 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
1089 tty->driver_data = info;
1090 tty_port_tty_set(&info->port, tty);
1092 * Start up serial port
1094 spin_lock_irqsave(&info->slock, flags);
1096 spin_unlock_irqrestore(&info->slock, flags);
1097 retval = mxser_startup(tty);
1101 retval = mxser_block_til_ready(tty, filp, info);
1105 /* unmark here for very high baud rate (ex. 921600 bps) used */
1106 tty->low_latency = 1;
1110 static void mxser_flush_buffer(struct tty_struct *tty)
1112 struct mxser_port *info = tty->driver_data;
1114 unsigned long flags;
1117 spin_lock_irqsave(&info->slock, flags);
1118 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1120 fcr = inb(info->ioaddr + UART_FCR);
1121 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1122 info->ioaddr + UART_FCR);
1123 outb(fcr, info->ioaddr + UART_FCR);
1125 spin_unlock_irqrestore(&info->slock, flags);
1132 * This routine is called when the serial port gets closed. First, we
1133 * wait for the last remaining data to be sent. Then, we unlink its
1134 * async structure from the interrupt chain if necessary, and we free
1135 * that IRQ if nothing is left in the chain.
1137 static void mxser_close(struct tty_struct *tty, struct file *filp)
1139 struct mxser_port *info = tty->driver_data;
1141 unsigned long timeout;
1142 unsigned long flags;
1144 if (tty->index == MXSER_PORTS)
1149 spin_lock_irqsave(&info->slock, flags);
1151 if (tty_hung_up_p(filp)) {
1152 spin_unlock_irqrestore(&info->slock, flags);
1155 if ((tty->count == 1) && (info->port.count != 1)) {
1157 * Uh, oh. tty->count is 1, which means that the tty
1158 * structure will be freed. Info->port.count should always
1159 * be one in these conditions. If it's greater than
1160 * one, we've got real problems, since it means the
1161 * serial port won't be shutdown.
1163 printk(KERN_ERR "mxser_close: bad serial port count; "
1164 "tty->count is 1, info->port.count is %d\n", info->port.count);
1165 info->port.count = 1;
1167 if (--info->port.count < 0) {
1168 printk(KERN_ERR "mxser_close: bad serial port count for "
1169 "ttys%d: %d\n", tty->index, info->port.count);
1170 info->port.count = 0;
1172 if (info->port.count) {
1173 spin_unlock_irqrestore(&info->slock, flags);
1176 info->port.flags |= ASYNC_CLOSING;
1177 spin_unlock_irqrestore(&info->slock, flags);
1179 * Save the termios structure, since this port may have
1180 * separate termios for callout and dialin.
1182 if (info->port.flags & ASYNC_NORMAL_ACTIVE)
1183 info->normal_termios = *tty->termios;
1185 * Now we wait for the transmit buffer to clear; and we notify
1186 * the line discipline to only process XON/XOFF characters.
1189 if (info->port.closing_wait != ASYNC_CLOSING_WAIT_NONE)
1190 tty_wait_until_sent(tty, info->port.closing_wait);
1192 * At this point we stop accepting input. To do this, we
1193 * disable the receive line status interrupts, and tell the
1194 * interrupt driver to stop checking the data ready bit in the
1195 * line status register.
1197 info->IER &= ~UART_IER_RLSI;
1198 if (info->board->chip_flag)
1199 info->IER &= ~MOXA_MUST_RECV_ISR;
1201 if (info->port.flags & ASYNC_INITIALIZED) {
1202 outb(info->IER, info->ioaddr + UART_IER);
1204 * Before we drop DTR, make sure the UART transmitter
1205 * has completely drained; this is especially
1206 * important if there is a transmit FIFO!
1208 timeout = jiffies + HZ;
1209 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
1210 schedule_timeout_interruptible(5);
1211 if (time_after(jiffies, timeout))
1215 mxser_shutdown(tty);
1217 mxser_flush_buffer(tty);
1218 tty_ldisc_flush(tty);
1221 tty_port_tty_set(&info->port, NULL);
1222 if (info->port.blocked_open) {
1223 if (info->port.close_delay)
1224 schedule_timeout_interruptible(info->port.close_delay);
1225 wake_up_interruptible(&info->port.open_wait);
1228 info->port.flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1231 static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1234 struct mxser_port *info = tty->driver_data;
1235 unsigned long flags;
1237 if (!info->port.xmit_buf)
1241 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1242 SERIAL_XMIT_SIZE - info->xmit_head));
1246 memcpy(info->port.xmit_buf + info->xmit_head, buf, c);
1247 spin_lock_irqsave(&info->slock, flags);
1248 info->xmit_head = (info->xmit_head + c) &
1249 (SERIAL_XMIT_SIZE - 1);
1250 info->xmit_cnt += c;
1251 spin_unlock_irqrestore(&info->slock, flags);
1258 if (info->xmit_cnt && !tty->stopped) {
1259 if (!tty->hw_stopped ||
1260 (info->type == PORT_16550A) ||
1261 (info->board->chip_flag)) {
1262 spin_lock_irqsave(&info->slock, flags);
1263 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1265 info->IER |= UART_IER_THRI;
1266 outb(info->IER, info->ioaddr + UART_IER);
1267 spin_unlock_irqrestore(&info->slock, flags);
1273 static int mxser_put_char(struct tty_struct *tty, unsigned char ch)
1275 struct mxser_port *info = tty->driver_data;
1276 unsigned long flags;
1278 if (!info->port.xmit_buf)
1281 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1284 spin_lock_irqsave(&info->slock, flags);
1285 info->port.xmit_buf[info->xmit_head++] = ch;
1286 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1288 spin_unlock_irqrestore(&info->slock, flags);
1289 if (!tty->stopped) {
1290 if (!tty->hw_stopped ||
1291 (info->type == PORT_16550A) ||
1292 info->board->chip_flag) {
1293 spin_lock_irqsave(&info->slock, flags);
1294 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1295 info->IER |= UART_IER_THRI;
1296 outb(info->IER, info->ioaddr + UART_IER);
1297 spin_unlock_irqrestore(&info->slock, flags);
1304 static void mxser_flush_chars(struct tty_struct *tty)
1306 struct mxser_port *info = tty->driver_data;
1307 unsigned long flags;
1309 if (info->xmit_cnt <= 0 || tty->stopped || !info->port.xmit_buf ||
1310 (tty->hw_stopped && info->type != PORT_16550A &&
1311 !info->board->chip_flag))
1314 spin_lock_irqsave(&info->slock, flags);
1316 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1317 info->IER |= UART_IER_THRI;
1318 outb(info->IER, info->ioaddr + UART_IER);
1320 spin_unlock_irqrestore(&info->slock, flags);
1323 static int mxser_write_room(struct tty_struct *tty)
1325 struct mxser_port *info = tty->driver_data;
1328 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1329 return ret < 0 ? 0 : ret;
1332 static int mxser_chars_in_buffer(struct tty_struct *tty)
1334 struct mxser_port *info = tty->driver_data;
1335 return info->xmit_cnt;
1339 * ------------------------------------------------------------
1340 * friends of mxser_ioctl()
1341 * ------------------------------------------------------------
1343 static int mxser_get_serial_info(struct tty_struct *tty,
1344 struct serial_struct __user *retinfo)
1346 struct mxser_port *info = tty->driver_data;
1347 struct serial_struct tmp = {
1350 .port = info->ioaddr,
1351 .irq = info->board->irq,
1352 .flags = info->port.flags,
1353 .baud_base = info->baud_base,
1354 .close_delay = info->port.close_delay,
1355 .closing_wait = info->port.closing_wait,
1356 .custom_divisor = info->custom_divisor,
1359 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1364 static int mxser_set_serial_info(struct tty_struct *tty,
1365 struct serial_struct __user *new_info)
1367 struct mxser_port *info = tty->driver_data;
1368 struct serial_struct new_serial;
1370 unsigned long sl_flags;
1374 if (!new_info || !info->ioaddr)
1376 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1379 if (new_serial.irq != info->board->irq ||
1380 new_serial.port != info->ioaddr)
1383 flags = info->port.flags & ASYNC_SPD_MASK;
1385 if (!capable(CAP_SYS_ADMIN)) {
1386 if ((new_serial.baud_base != info->baud_base) ||
1387 (new_serial.close_delay != info->port.close_delay) ||
1388 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->port.flags & ~ASYNC_USR_MASK)))
1390 info->port.flags = ((info->port.flags & ~ASYNC_USR_MASK) |
1391 (new_serial.flags & ASYNC_USR_MASK));
1394 * OK, past this point, all the error checking has been done.
1395 * At this point, we start making changes.....
1397 info->port.flags = ((info->port.flags & ~ASYNC_FLAGS) |
1398 (new_serial.flags & ASYNC_FLAGS));
1399 info->port.close_delay = new_serial.close_delay * HZ / 100;
1400 info->port.closing_wait = new_serial.closing_wait * HZ / 100;
1401 tty->low_latency = (info->port.flags & ASYNC_LOW_LATENCY)
1403 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
1404 (new_serial.baud_base != info->baud_base ||
1405 new_serial.custom_divisor !=
1406 info->custom_divisor)) {
1407 baud = new_serial.baud_base / new_serial.custom_divisor;
1408 tty_encode_baud_rate(tty, baud, baud);
1412 info->type = new_serial.type;
1414 process_txrx_fifo(info);
1416 if (info->port.flags & ASYNC_INITIALIZED) {
1417 if (flags != (info->port.flags & ASYNC_SPD_MASK)) {
1418 spin_lock_irqsave(&info->slock, sl_flags);
1419 mxser_change_speed(tty, NULL);
1420 spin_unlock_irqrestore(&info->slock, sl_flags);
1423 retval = mxser_startup(tty);
1429 * mxser_get_lsr_info - get line status register info
1431 * Purpose: Let user call ioctl() to get info when the UART physically
1432 * is emptied. On bus types like RS485, the transmitter must
1433 * release the bus after transmitting. This must be done when
1434 * the transmit shift register is empty, not be done when the
1435 * transmit holding register is empty. This functionality
1436 * allows an RS485 driver to be written in user space.
1438 static int mxser_get_lsr_info(struct mxser_port *info,
1439 unsigned int __user *value)
1441 unsigned char status;
1442 unsigned int result;
1443 unsigned long flags;
1445 spin_lock_irqsave(&info->slock, flags);
1446 status = inb(info->ioaddr + UART_LSR);
1447 spin_unlock_irqrestore(&info->slock, flags);
1448 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1449 return put_user(result, value);
1452 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
1454 struct mxser_port *info = tty->driver_data;
1455 unsigned char control, status;
1456 unsigned long flags;
1459 if (tty->index == MXSER_PORTS)
1460 return -ENOIOCTLCMD;
1461 if (test_bit(TTY_IO_ERROR, &tty->flags))
1464 control = info->MCR;
1466 spin_lock_irqsave(&info->slock, flags);
1467 status = inb(info->ioaddr + UART_MSR);
1468 if (status & UART_MSR_ANY_DELTA)
1469 mxser_check_modem_status(tty, info, status);
1470 spin_unlock_irqrestore(&info->slock, flags);
1471 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1472 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1473 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1474 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1475 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1476 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1479 static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1480 unsigned int set, unsigned int clear)
1482 struct mxser_port *info = tty->driver_data;
1483 unsigned long flags;
1486 if (tty->index == MXSER_PORTS)
1487 return -ENOIOCTLCMD;
1488 if (test_bit(TTY_IO_ERROR, &tty->flags))
1491 spin_lock_irqsave(&info->slock, flags);
1493 if (set & TIOCM_RTS)
1494 info->MCR |= UART_MCR_RTS;
1495 if (set & TIOCM_DTR)
1496 info->MCR |= UART_MCR_DTR;
1498 if (clear & TIOCM_RTS)
1499 info->MCR &= ~UART_MCR_RTS;
1500 if (clear & TIOCM_DTR)
1501 info->MCR &= ~UART_MCR_DTR;
1503 outb(info->MCR, info->ioaddr + UART_MCR);
1504 spin_unlock_irqrestore(&info->slock, flags);
1508 static int __init mxser_program_mode(int port)
1520 id = inb(port + 1) & 0x1F;
1521 if ((id != C168_ASIC_ID) &&
1522 (id != C104_ASIC_ID) &&
1523 (id != C102_ASIC_ID) &&
1524 (id != CI132_ASIC_ID) &&
1525 (id != CI134_ASIC_ID) &&
1526 (id != CI104J_ASIC_ID))
1528 for (i = 0, j = 0; i < 4; i++) {
1532 } else if ((j == 1) && (n == 1)) {
1543 static void __init mxser_normal_mode(int port)
1547 outb(0xA5, port + 1);
1548 outb(0x80, port + 3);
1549 outb(12, port + 0); /* 9600 bps */
1551 outb(0x03, port + 3); /* 8 data bits */
1552 outb(0x13, port + 4); /* loop back mode */
1553 for (i = 0; i < 16; i++) {
1555 if ((n & 0x61) == 0x60)
1560 outb(0x00, port + 4);
1563 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1564 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1565 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1566 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1567 #define EN_CCMD 0x000 /* Chip's command register */
1568 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1569 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1570 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1571 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1572 #define EN0_DCFG 0x00E /* Data configuration reg WR */
1573 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1574 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1575 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1576 static int __init mxser_read_register(int port, unsigned short *regs)
1578 int i, k, value, id;
1581 id = mxser_program_mode(port);
1584 for (i = 0; i < 14; i++) {
1585 k = (i & 0x3F) | 0x180;
1586 for (j = 0x100; j > 0; j >>= 1) {
1587 outb(CHIP_CS, port);
1589 outb(CHIP_CS | CHIP_DO, port);
1590 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1592 outb(CHIP_CS, port);
1593 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1598 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1599 outb(CHIP_CS, port);
1600 outb(CHIP_CS | CHIP_SK, port);
1601 if (inb(port) & CHIP_DI)
1607 mxser_normal_mode(port);
1611 static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1613 struct mxser_port *port;
1614 struct tty_struct *tty;
1620 case MOXA_GET_MAJOR:
1621 if (printk_ratelimit())
1622 printk(KERN_WARNING "mxser: '%s' uses deprecated ioctl "
1623 "%x (GET_MAJOR), fix your userspace\n",
1624 current->comm, cmd);
1625 return put_user(ttymajor, (int __user *)argp);
1627 case MOXA_CHKPORTENABLE:
1630 for (i = 0; i < MXSER_BOARDS; i++)
1631 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1632 if (mxser_boards[i].ports[j].ioaddr)
1635 return put_user(result, (unsigned long __user *)argp);
1636 case MOXA_GETDATACOUNT:
1638 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1642 case MOXA_GETMSTATUS: {
1643 struct mxser_mstatus ms, __user *msu = argp;
1645 for (i = 0; i < MXSER_BOARDS; i++)
1646 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1647 port = &mxser_boards[i].ports[j];
1648 memset(&ms, 0, sizeof(ms));
1653 tty = tty_port_tty_get(&port->port);
1655 if (!tty || !tty->termios)
1656 ms.cflag = port->normal_termios.c_cflag;
1658 ms.cflag = tty->termios->c_cflag;
1660 status = inb(port->ioaddr + UART_MSR);
1661 if (status & UART_MSR_DCD)
1663 if (status & UART_MSR_DSR)
1665 if (status & UART_MSR_CTS)
1668 if (copy_to_user(msu, &ms, sizeof(ms))) {
1677 case MOXA_ASPP_MON_EXT: {
1678 struct mxser_mon_ext *me; /* it's 2k, stack unfriendly */
1679 unsigned int cflag, iflag, p;
1682 me = kzalloc(sizeof(*me), GFP_KERNEL);
1687 for (i = 0, p = 0; i < MXSER_BOARDS; i++) {
1688 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++, p++) {
1689 if (p >= ARRAY_SIZE(me->rx_cnt)) {
1693 port = &mxser_boards[i].ports[j];
1697 status = mxser_get_msr(port->ioaddr, 0, p);
1699 if (status & UART_MSR_TERI)
1701 if (status & UART_MSR_DDSR)
1703 if (status & UART_MSR_DDCD)
1705 if (status & UART_MSR_DCTS)
1708 port->mon_data.modem_status = status;
1709 me->rx_cnt[p] = port->mon_data.rxcnt;
1710 me->tx_cnt[p] = port->mon_data.txcnt;
1711 me->up_rxcnt[p] = port->mon_data.up_rxcnt;
1712 me->up_txcnt[p] = port->mon_data.up_txcnt;
1713 me->modem_status[p] =
1714 port->mon_data.modem_status;
1715 tty = tty_port_tty_get(&port->port);
1717 if (!tty || !tty->termios) {
1718 cflag = port->normal_termios.c_cflag;
1719 iflag = port->normal_termios.c_iflag;
1720 me->baudrate[p] = tty_termios_baud_rate(&port->normal_termios);
1722 cflag = tty->termios->c_cflag;
1723 iflag = tty->termios->c_iflag;
1724 me->baudrate[p] = tty_get_baud_rate(tty);
1728 me->databits[p] = cflag & CSIZE;
1729 me->stopbits[p] = cflag & CSTOPB;
1730 me->parity[p] = cflag & (PARENB | PARODD |
1733 if (cflag & CRTSCTS)
1734 me->flowctrl[p] |= 0x03;
1736 if (iflag & (IXON | IXOFF))
1737 me->flowctrl[p] |= 0x0C;
1739 if (port->type == PORT_16550A)
1742 opmode = inb(port->opmode_ioaddr) >>
1744 opmode &= OP_MODE_MASK;
1745 me->iftype[p] = opmode;
1749 if (copy_to_user(argp, me, sizeof(*me)))
1755 return -ENOIOCTLCMD;
1760 static int mxser_cflags_changed(struct mxser_port *info, unsigned long arg,
1761 struct async_icount *cprev)
1763 struct async_icount cnow;
1764 unsigned long flags;
1767 spin_lock_irqsave(&info->slock, flags);
1768 cnow = info->icount; /* atomic copy */
1769 spin_unlock_irqrestore(&info->slock, flags);
1771 ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1772 ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1773 ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1774 ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
1781 static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1782 unsigned int cmd, unsigned long arg)
1784 struct mxser_port *info = tty->driver_data;
1785 struct async_icount cnow;
1786 unsigned long flags;
1787 void __user *argp = (void __user *)arg;
1790 if (tty->index == MXSER_PORTS)
1791 return mxser_ioctl_special(cmd, argp);
1793 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1795 unsigned long opmode;
1796 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1798 unsigned char val, mask;
1801 if (cmd == MOXA_SET_OP_MODE) {
1802 if (get_user(opmode, (int __user *) argp))
1804 if (opmode != RS232_MODE &&
1805 opmode != RS485_2WIRE_MODE &&
1806 opmode != RS422_MODE &&
1807 opmode != RS485_4WIRE_MODE)
1812 val = inb(info->opmode_ioaddr);
1814 val |= (opmode << shiftbit);
1815 outb(val, info->opmode_ioaddr);
1820 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1821 opmode &= OP_MODE_MASK;
1823 if (put_user(opmode, (int __user *)argp))
1829 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1830 test_bit(TTY_IO_ERROR, &tty->flags))
1836 retval = mxser_get_serial_info(tty, argp);
1841 retval = mxser_set_serial_info(tty, argp);
1844 case TIOCSERGETLSR: /* Get line status register */
1845 return mxser_get_lsr_info(info, argp);
1847 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1848 * - mask passed in arg for lines of interest
1849 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1850 * Caller should use TIOCGICOUNT to see which one it was
1853 spin_lock_irqsave(&info->slock, flags);
1854 cnow = info->icount; /* note the counters on entry */
1855 spin_unlock_irqrestore(&info->slock, flags);
1857 return wait_event_interruptible(info->delta_msr_wait,
1858 mxser_cflags_changed(info, arg, &cnow));
1860 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1861 * Return: write counters to the user passed counter struct
1862 * NB: both 1->0 and 0->1 transitions are counted except for
1863 * RI where only 0->1 is counted.
1866 struct serial_icounter_struct icnt = { 0 };
1867 spin_lock_irqsave(&info->slock, flags);
1868 cnow = info->icount;
1869 spin_unlock_irqrestore(&info->slock, flags);
1871 icnt.frame = cnow.frame;
1872 icnt.brk = cnow.brk;
1873 icnt.overrun = cnow.overrun;
1874 icnt.buf_overrun = cnow.buf_overrun;
1875 icnt.parity = cnow.parity;
1878 icnt.cts = cnow.cts;
1879 icnt.dsr = cnow.dsr;
1880 icnt.rng = cnow.rng;
1881 icnt.dcd = cnow.dcd;
1883 return copy_to_user(argp, &icnt, sizeof(icnt)) ? -EFAULT : 0;
1885 case MOXA_HighSpeedOn:
1886 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1887 case MOXA_SDS_RSTICOUNTER:
1889 info->mon_data.rxcnt = 0;
1890 info->mon_data.txcnt = 0;
1894 case MOXA_ASPP_OQUEUE:{
1898 len = mxser_chars_in_buffer(tty);
1899 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1900 len += (lsr ? 0 : 1);
1903 return put_user(len, (int __user *)argp);
1905 case MOXA_ASPP_MON: {
1909 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1910 mxser_check_modem_status(tty, info, status);
1912 mcr = inb(info->ioaddr + UART_MCR);
1913 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1914 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1916 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1918 if (mcr & MOXA_MUST_MCR_TX_XON)
1919 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1921 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1923 if (tty->hw_stopped)
1924 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1926 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1928 if (copy_to_user(argp, &info->mon_data,
1929 sizeof(struct mxser_mon)))
1934 case MOXA_ASPP_LSTATUS: {
1935 if (put_user(info->err_shadow, (unsigned char __user *)argp))
1938 info->err_shadow = 0;
1941 case MOXA_SET_BAUD_METHOD: {
1944 if (get_user(method, (int __user *)argp))
1946 mxser_set_baud_method[tty->index] = method;
1947 return put_user(method, (int __user *)argp);
1950 return -ENOIOCTLCMD;
1955 static void mxser_stoprx(struct tty_struct *tty)
1957 struct mxser_port *info = tty->driver_data;
1959 info->ldisc_stop_rx = 1;
1961 if (info->board->chip_flag) {
1962 info->IER &= ~MOXA_MUST_RECV_ISR;
1963 outb(info->IER, info->ioaddr + UART_IER);
1965 info->x_char = STOP_CHAR(tty);
1966 outb(0, info->ioaddr + UART_IER);
1967 info->IER |= UART_IER_THRI;
1968 outb(info->IER, info->ioaddr + UART_IER);
1972 if (tty->termios->c_cflag & CRTSCTS) {
1973 info->MCR &= ~UART_MCR_RTS;
1974 outb(info->MCR, info->ioaddr + UART_MCR);
1979 * This routine is called by the upper-layer tty layer to signal that
1980 * incoming characters should be throttled.
1982 static void mxser_throttle(struct tty_struct *tty)
1987 static void mxser_unthrottle(struct tty_struct *tty)
1989 struct mxser_port *info = tty->driver_data;
1992 info->ldisc_stop_rx = 0;
1997 if (info->board->chip_flag) {
1998 info->IER |= MOXA_MUST_RECV_ISR;
1999 outb(info->IER, info->ioaddr + UART_IER);
2001 info->x_char = START_CHAR(tty);
2002 outb(0, info->ioaddr + UART_IER);
2003 info->IER |= UART_IER_THRI;
2004 outb(info->IER, info->ioaddr + UART_IER);
2009 if (tty->termios->c_cflag & CRTSCTS) {
2010 info->MCR |= UART_MCR_RTS;
2011 outb(info->MCR, info->ioaddr + UART_MCR);
2016 * mxser_stop() and mxser_start()
2018 * This routines are called before setting or resetting tty->stopped.
2019 * They enable or disable transmitter interrupts, as necessary.
2021 static void mxser_stop(struct tty_struct *tty)
2023 struct mxser_port *info = tty->driver_data;
2024 unsigned long flags;
2026 spin_lock_irqsave(&info->slock, flags);
2027 if (info->IER & UART_IER_THRI) {
2028 info->IER &= ~UART_IER_THRI;
2029 outb(info->IER, info->ioaddr + UART_IER);
2031 spin_unlock_irqrestore(&info->slock, flags);
2034 static void mxser_start(struct tty_struct *tty)
2036 struct mxser_port *info = tty->driver_data;
2037 unsigned long flags;
2039 spin_lock_irqsave(&info->slock, flags);
2040 if (info->xmit_cnt && info->port.xmit_buf) {
2041 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
2042 info->IER |= UART_IER_THRI;
2043 outb(info->IER, info->ioaddr + UART_IER);
2045 spin_unlock_irqrestore(&info->slock, flags);
2048 static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2050 struct mxser_port *info = tty->driver_data;
2051 unsigned long flags;
2053 spin_lock_irqsave(&info->slock, flags);
2054 mxser_change_speed(tty, old_termios);
2055 spin_unlock_irqrestore(&info->slock, flags);
2057 if ((old_termios->c_cflag & CRTSCTS) &&
2058 !(tty->termios->c_cflag & CRTSCTS)) {
2059 tty->hw_stopped = 0;
2063 /* Handle sw stopped */
2064 if ((old_termios->c_iflag & IXON) &&
2065 !(tty->termios->c_iflag & IXON)) {
2068 if (info->board->chip_flag) {
2069 spin_lock_irqsave(&info->slock, flags);
2070 mxser_disable_must_rx_software_flow_control(
2072 spin_unlock_irqrestore(&info->slock, flags);
2080 * mxser_wait_until_sent() --- wait until the transmitter is empty
2082 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2084 struct mxser_port *info = tty->driver_data;
2085 unsigned long orig_jiffies, char_time;
2088 if (info->type == PORT_UNKNOWN)
2091 if (info->xmit_fifo_size == 0)
2092 return; /* Just in case.... */
2094 orig_jiffies = jiffies;
2096 * Set the check interval to be 1/5 of the estimated time to
2097 * send a single character, and make it at least 1. The check
2098 * interval should also be less than the timeout.
2100 * Note: we have to use pretty tight timings here to satisfy
2103 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2104 char_time = char_time / 5;
2107 if (timeout && timeout < char_time)
2108 char_time = timeout;
2110 * If the transmitter hasn't cleared in twice the approximate
2111 * amount of time to send the entire FIFO, it probably won't
2112 * ever clear. This assumes the UART isn't doing flow
2113 * control, which is currently the case. Hence, if it ever
2114 * takes longer than info->timeout, this is probably due to a
2115 * UART bug of some kind. So, we clamp the timeout parameter at
2118 if (!timeout || timeout > 2 * info->timeout)
2119 timeout = 2 * info->timeout;
2120 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2121 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2122 timeout, char_time);
2123 printk("jiff=%lu...", jiffies);
2126 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
2127 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2128 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2130 schedule_timeout_interruptible(char_time);
2131 if (signal_pending(current))
2133 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2136 set_current_state(TASK_RUNNING);
2139 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2140 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2145 * This routine is called by tty_hangup() when a hangup is signaled.
2147 static void mxser_hangup(struct tty_struct *tty)
2149 struct mxser_port *info = tty->driver_data;
2151 mxser_flush_buffer(tty);
2152 mxser_shutdown(tty);
2153 info->port.count = 0;
2154 info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
2155 tty_port_tty_set(&info->port, NULL);
2156 wake_up_interruptible(&info->port.open_wait);
2160 * mxser_rs_break() --- routine which turns the break handling on or off
2162 static int mxser_rs_break(struct tty_struct *tty, int break_state)
2164 struct mxser_port *info = tty->driver_data;
2165 unsigned long flags;
2167 spin_lock_irqsave(&info->slock, flags);
2168 if (break_state == -1)
2169 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2170 info->ioaddr + UART_LCR);
2172 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2173 info->ioaddr + UART_LCR);
2174 spin_unlock_irqrestore(&info->slock, flags);
2178 static void mxser_receive_chars(struct tty_struct *tty,
2179 struct mxser_port *port, int *status)
2181 unsigned char ch, gdl;
2187 recv_room = tty->receive_room;
2188 if (recv_room == 0 && !port->ldisc_stop_rx)
2190 if (port->board->chip_flag != MOXA_OTHER_UART) {
2192 if (*status & UART_LSR_SPECIAL)
2194 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
2195 (*status & MOXA_MUST_LSR_RERR))
2197 if (*status & MOXA_MUST_LSR_RERR)
2200 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
2202 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
2203 gdl &= MOXA_MUST_GDL_MASK;
2204 if (gdl >= recv_room) {
2205 if (!port->ldisc_stop_rx)
2209 ch = inb(port->ioaddr + UART_RX);
2210 tty_insert_flip_char(tty, ch, 0);
2221 ch = inb(port->ioaddr + UART_RX);
2222 if (port->board->chip_flag && (*status & UART_LSR_OE))
2223 outb(0x23, port->ioaddr + UART_FCR);
2224 *status &= port->read_status_mask;
2225 if (*status & port->ignore_status_mask) {
2226 if (++ignored > 100)
2230 if (*status & UART_LSR_SPECIAL) {
2231 if (*status & UART_LSR_BI) {
2235 if (port->port.flags & ASYNC_SAK)
2237 } else if (*status & UART_LSR_PE) {
2239 port->icount.parity++;
2240 } else if (*status & UART_LSR_FE) {
2242 port->icount.frame++;
2243 } else if (*status & UART_LSR_OE) {
2245 port->icount.overrun++;
2249 tty_insert_flip_char(tty, ch, flag);
2251 if (cnt >= recv_room) {
2252 if (!port->ldisc_stop_rx)
2259 if (port->board->chip_flag)
2262 *status = inb(port->ioaddr + UART_LSR);
2263 } while (*status & UART_LSR_DR);
2266 mxvar_log.rxcnt[tty->index] += cnt;
2267 port->mon_data.rxcnt += cnt;
2268 port->mon_data.up_rxcnt += cnt;
2271 * We are called from an interrupt context with &port->slock
2272 * being held. Drop it temporarily in order to prevent
2273 * recursive locking.
2275 spin_unlock(&port->slock);
2276 tty_flip_buffer_push(tty);
2277 spin_lock(&port->slock);
2280 static void mxser_transmit_chars(struct tty_struct *tty, struct mxser_port *port)
2285 outb(port->x_char, port->ioaddr + UART_TX);
2287 mxvar_log.txcnt[tty->index]++;
2288 port->mon_data.txcnt++;
2289 port->mon_data.up_txcnt++;
2294 if (port->port.xmit_buf == NULL)
2297 if (port->xmit_cnt <= 0 || tty->stopped ||
2299 (port->type != PORT_16550A) &&
2300 (!port->board->chip_flag))) {
2301 port->IER &= ~UART_IER_THRI;
2302 outb(port->IER, port->ioaddr + UART_IER);
2306 cnt = port->xmit_cnt;
2307 count = port->xmit_fifo_size;
2309 outb(port->port.xmit_buf[port->xmit_tail++],
2310 port->ioaddr + UART_TX);
2311 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2312 if (--port->xmit_cnt <= 0)
2314 } while (--count > 0);
2315 mxvar_log.txcnt[tty->index] += (cnt - port->xmit_cnt);
2317 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2318 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2319 port->icount.tx += (cnt - port->xmit_cnt);
2321 if (port->xmit_cnt < WAKEUP_CHARS && tty)
2324 if (port->xmit_cnt <= 0) {
2325 port->IER &= ~UART_IER_THRI;
2326 outb(port->IER, port->ioaddr + UART_IER);
2331 * This is the serial driver's generic interrupt routine
2333 static irqreturn_t mxser_interrupt(int irq, void *dev_id)
2336 struct mxser_board *brd = NULL;
2337 struct mxser_port *port;
2338 int max, irqbits, bits, msr;
2339 unsigned int int_cnt, pass_counter = 0;
2340 int handled = IRQ_NONE;
2341 struct tty_struct *tty;
2343 for (i = 0; i < MXSER_BOARDS; i++)
2344 if (dev_id == &mxser_boards[i]) {
2349 if (i == MXSER_BOARDS)
2353 max = brd->info->nports;
2354 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
2355 irqbits = inb(brd->vector) & brd->vector_mask;
2356 if (irqbits == brd->vector_mask)
2359 handled = IRQ_HANDLED;
2360 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2361 if (irqbits == brd->vector_mask)
2365 port = &brd->ports[i];
2368 spin_lock(&port->slock);
2370 iir = inb(port->ioaddr + UART_IIR);
2371 if (iir & UART_IIR_NO_INT)
2373 iir &= MOXA_MUST_IIR_MASK;
2374 tty = tty_port_tty_get(&port->port);
2376 (port->port.flags & ASYNC_CLOSING) ||
2377 !(port->port.flags &
2378 ASYNC_INITIALIZED)) {
2379 status = inb(port->ioaddr + UART_LSR);
2380 outb(0x27, port->ioaddr + UART_FCR);
2381 inb(port->ioaddr + UART_MSR);
2386 status = inb(port->ioaddr + UART_LSR);
2388 if (status & UART_LSR_PE)
2389 port->err_shadow |= NPPI_NOTIFY_PARITY;
2390 if (status & UART_LSR_FE)
2391 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2392 if (status & UART_LSR_OE)
2394 NPPI_NOTIFY_HW_OVERRUN;
2395 if (status & UART_LSR_BI)
2396 port->err_shadow |= NPPI_NOTIFY_BREAK;
2398 if (port->board->chip_flag) {
2399 if (iir == MOXA_MUST_IIR_GDA ||
2400 iir == MOXA_MUST_IIR_RDA ||
2401 iir == MOXA_MUST_IIR_RTO ||
2402 iir == MOXA_MUST_IIR_LSR)
2403 mxser_receive_chars(tty, port,
2407 status &= port->read_status_mask;
2408 if (status & UART_LSR_DR)
2409 mxser_receive_chars(tty, port,
2412 msr = inb(port->ioaddr + UART_MSR);
2413 if (msr & UART_MSR_ANY_DELTA)
2414 mxser_check_modem_status(tty, port, msr);
2416 if (port->board->chip_flag) {
2417 if (iir == 0x02 && (status &
2419 mxser_transmit_chars(tty, port);
2421 if (status & UART_LSR_THRE)
2422 mxser_transmit_chars(tty, port);
2425 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2426 spin_unlock(&port->slock);
2434 static const struct tty_operations mxser_ops = {
2436 .close = mxser_close,
2437 .write = mxser_write,
2438 .put_char = mxser_put_char,
2439 .flush_chars = mxser_flush_chars,
2440 .write_room = mxser_write_room,
2441 .chars_in_buffer = mxser_chars_in_buffer,
2442 .flush_buffer = mxser_flush_buffer,
2443 .ioctl = mxser_ioctl,
2444 .throttle = mxser_throttle,
2445 .unthrottle = mxser_unthrottle,
2446 .set_termios = mxser_set_termios,
2448 .start = mxser_start,
2449 .hangup = mxser_hangup,
2450 .break_ctl = mxser_rs_break,
2451 .wait_until_sent = mxser_wait_until_sent,
2452 .tiocmget = mxser_tiocmget,
2453 .tiocmset = mxser_tiocmset,
2457 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2460 static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
2464 free_irq(brd->irq, brd);
2465 if (pdev != NULL) { /* PCI */
2467 pci_release_region(pdev, 2);
2468 pci_release_region(pdev, 3);
2471 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2472 release_region(brd->vector, 1);
2476 static int __devinit mxser_initbrd(struct mxser_board *brd,
2477 struct pci_dev *pdev)
2479 struct mxser_port *info;
2483 printk(KERN_INFO "mxser: max. baud rate = %d bps\n",
2484 brd->ports[0].max_baud);
2486 for (i = 0; i < brd->info->nports; i++) {
2487 info = &brd->ports[i];
2488 tty_port_init(&info->port);
2491 info->ldisc_stop_rx = 0;
2493 /* Enhance mode enabled here */
2494 if (brd->chip_flag != MOXA_OTHER_UART)
2495 mxser_enable_must_enchance_mode(info->ioaddr);
2497 info->port.flags = ASYNC_SHARE_IRQ;
2498 info->type = brd->uart_type;
2500 process_txrx_fifo(info);
2502 info->custom_divisor = info->baud_base * 16;
2503 info->port.close_delay = 5 * HZ / 10;
2504 info->port.closing_wait = 30 * HZ;
2505 info->normal_termios = mxvar_sdriver->init_termios;
2506 init_waitqueue_head(&info->delta_msr_wait);
2507 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2508 info->err_shadow = 0;
2509 spin_lock_init(&info->slock);
2511 /* before set INT ISR, disable all int */
2512 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2513 info->ioaddr + UART_IER);
2516 retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
2519 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2520 "conflict with another device.\n",
2521 brd->info->name, brd->irq);
2522 /* We hold resources, we need to release them. */
2523 mxser_release_res(brd, pdev, 0);
2528 static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
2531 unsigned short regs[16], irq;
2532 unsigned char scratch, scratch2;
2534 brd->chip_flag = MOXA_OTHER_UART;
2536 id = mxser_read_register(cap, regs);
2539 brd->info = &mxser_cards[0];
2542 brd->info = &mxser_cards[1];
2544 case CI104J_ASIC_ID:
2545 brd->info = &mxser_cards[2];
2548 brd->info = &mxser_cards[5];
2551 brd->info = &mxser_cards[6];
2554 brd->info = &mxser_cards[7];
2561 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2562 Flag-hack checks if configuration should be read as 2-port here. */
2563 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
2564 irq = regs[9] & 0xF000;
2565 irq = irq | (irq >> 4);
2566 if (irq != (regs[9] & 0xFF00))
2567 goto err_irqconflict;
2568 } else if (brd->info->nports == 4) {
2569 irq = regs[9] & 0xF000;
2570 irq = irq | (irq >> 4);
2571 irq = irq | (irq >> 8);
2573 goto err_irqconflict;
2574 } else if (brd->info->nports == 8) {
2575 irq = regs[9] & 0xF000;
2576 irq = irq | (irq >> 4);
2577 irq = irq | (irq >> 8);
2578 if ((irq != regs[9]) || (irq != regs[10]))
2579 goto err_irqconflict;
2583 printk(KERN_ERR "mxser: interrupt number unset\n");
2586 brd->irq = ((int)(irq & 0xF000) >> 12);
2587 for (i = 0; i < 8; i++)
2588 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
2589 if ((regs[12] & 0x80) == 0) {
2590 printk(KERN_ERR "mxser: invalid interrupt vector\n");
2593 brd->vector = (int)regs[11]; /* interrupt vector */
2595 brd->vector_mask = 0x00FF;
2597 brd->vector_mask = 0x000F;
2598 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2599 if (regs[12] & bits) {
2600 brd->ports[i].baud_base = 921600;
2601 brd->ports[i].max_baud = 921600;
2603 brd->ports[i].baud_base = 115200;
2604 brd->ports[i].max_baud = 115200;
2607 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2608 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2609 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2610 outb(scratch2, cap + UART_LCR);
2611 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2612 scratch = inb(cap + UART_IIR);
2615 brd->uart_type = PORT_16550A;
2617 brd->uart_type = PORT_16450;
2618 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2620 printk(KERN_ERR "mxser: can't request ports I/O region: "
2621 "0x%.8lx-0x%.8lx\n",
2622 brd->ports[0].ioaddr, brd->ports[0].ioaddr +
2623 8 * brd->info->nports - 1);
2626 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2627 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2628 printk(KERN_ERR "mxser: can't request interrupt vector region: "
2629 "0x%.8lx-0x%.8lx\n",
2630 brd->ports[0].ioaddr, brd->ports[0].ioaddr +
2631 8 * brd->info->nports - 1);
2634 return brd->info->nports;
2637 printk(KERN_ERR "mxser: invalid interrupt number\n");
2641 static int __devinit mxser_probe(struct pci_dev *pdev,
2642 const struct pci_device_id *ent)
2645 struct mxser_board *brd;
2647 unsigned long ioaddress;
2648 int retval = -EINVAL;
2650 for (i = 0; i < MXSER_BOARDS; i++)
2651 if (mxser_boards[i].info == NULL)
2654 if (i >= MXSER_BOARDS) {
2655 dev_err(&pdev->dev, "too many boards found (maximum %d), board "
2656 "not configured\n", MXSER_BOARDS);
2660 brd = &mxser_boards[i];
2661 brd->idx = i * MXSER_PORTS_PER_BOARD;
2662 dev_info(&pdev->dev, "found MOXA %s board (BusNo=%d, DevNo=%d)\n",
2663 mxser_cards[ent->driver_data].name,
2664 pdev->bus->number, PCI_SLOT(pdev->devfn));
2666 retval = pci_enable_device(pdev);
2668 dev_err(&pdev->dev, "PCI enable failed\n");
2673 ioaddress = pci_resource_start(pdev, 2);
2674 retval = pci_request_region(pdev, 2, "mxser(IO)");
2678 brd->info = &mxser_cards[ent->driver_data];
2679 for (i = 0; i < brd->info->nports; i++)
2680 brd->ports[i].ioaddr = ioaddress + 8 * i;
2683 ioaddress = pci_resource_start(pdev, 3);
2684 retval = pci_request_region(pdev, 3, "mxser(vector)");
2687 brd->vector = ioaddress;
2690 brd->irq = pdev->irq;
2692 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2693 brd->uart_type = PORT_16550A;
2694 brd->vector_mask = 0;
2696 for (i = 0; i < brd->info->nports; i++) {
2697 for (j = 0; j < UART_INFO_NUM; j++) {
2698 if (Gpci_uart_info[j].type == brd->chip_flag) {
2699 brd->ports[i].max_baud =
2700 Gpci_uart_info[j].max_baud;
2702 /* exception....CP-102 */
2703 if (brd->info->flags & MXSER_HIGHBAUD)
2704 brd->ports[i].max_baud = 921600;
2710 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2711 for (i = 0; i < brd->info->nports; i++) {
2713 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2715 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
2717 outb(0, ioaddress + 4); /* default set to RS232 mode */
2718 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
2721 for (i = 0; i < brd->info->nports; i++) {
2722 brd->vector_mask |= (1 << i);
2723 brd->ports[i].baud_base = 921600;
2726 /* mxser_initbrd will hook ISR. */
2727 retval = mxser_initbrd(brd, pdev);
2731 for (i = 0; i < brd->info->nports; i++)
2732 tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
2734 pci_set_drvdata(pdev, brd);
2738 pci_release_region(pdev, 2);
2748 static void __devexit mxser_remove(struct pci_dev *pdev)
2750 struct mxser_board *brd = pci_get_drvdata(pdev);
2753 for (i = 0; i < brd->info->nports; i++)
2754 tty_unregister_device(mxvar_sdriver, brd->idx + i);
2756 mxser_release_res(brd, pdev, 1);
2760 static struct pci_driver mxser_driver = {
2762 .id_table = mxser_pcibrds,
2763 .probe = mxser_probe,
2764 .remove = __devexit_p(mxser_remove)
2767 static int __init mxser_module_init(void)
2769 struct mxser_board *brd;
2770 unsigned int b, i, m;
2773 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2777 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2780 /* Initialize the tty_driver structure */
2781 mxvar_sdriver->owner = THIS_MODULE;
2782 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2783 mxvar_sdriver->name = "ttyMI";
2784 mxvar_sdriver->major = ttymajor;
2785 mxvar_sdriver->minor_start = 0;
2786 mxvar_sdriver->num = MXSER_PORTS + 1;
2787 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2788 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2789 mxvar_sdriver->init_termios = tty_std_termios;
2790 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2791 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2792 tty_set_operations(mxvar_sdriver, &mxser_ops);
2794 retval = tty_register_driver(mxvar_sdriver);
2796 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2801 /* Start finding ISA boards here */
2802 for (m = 0, b = 0; b < MXSER_BOARDS; b++) {
2806 brd = &mxser_boards[m];
2807 retval = mxser_get_ISA_conf(!ioaddr[b], brd);
2813 printk(KERN_INFO "mxser: found MOXA %s board (CAP=0x%lx)\n",
2814 brd->info->name, ioaddr[b]);
2816 /* mxser_initbrd will hook ISR. */
2817 if (mxser_initbrd(brd, NULL) < 0) {
2822 brd->idx = m * MXSER_PORTS_PER_BOARD;
2823 for (i = 0; i < brd->info->nports; i++)
2824 tty_register_device(mxvar_sdriver, brd->idx + i, NULL);
2829 retval = pci_register_driver(&mxser_driver);
2831 printk(KERN_ERR "mxser: can't register pci driver\n");
2835 } /* else: we have some ISA cards under control */
2840 tty_unregister_driver(mxvar_sdriver);
2842 put_tty_driver(mxvar_sdriver);
2846 static void __exit mxser_module_exit(void)
2850 pci_unregister_driver(&mxser_driver);
2852 for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
2853 if (mxser_boards[i].info != NULL)
2854 for (j = 0; j < mxser_boards[i].info->nports; j++)
2855 tty_unregister_device(mxvar_sdriver,
2856 mxser_boards[i].idx + j);
2857 tty_unregister_driver(mxvar_sdriver);
2858 put_tty_driver(mxvar_sdriver);
2860 for (i = 0; i < MXSER_BOARDS; i++)
2861 if (mxser_boards[i].info != NULL)
2862 mxser_release_res(&mxser_boards[i], NULL, 1);
2865 module_init(mxser_module_init);
2866 module_exit(mxser_module_exit);