2 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
4 * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com.tw).
5 * Copyright (C) 2006-2007 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
19 * - Fixed sleep with spinlock held in mxser_send_break
22 #include <linux/module.h>
23 #include <linux/errno.h>
24 #include <linux/signal.h>
25 #include <linux/sched.h>
26 #include <linux/timer.h>
27 #include <linux/interrupt.h>
28 #include <linux/tty.h>
29 #include <linux/tty_flip.h>
30 #include <linux/serial.h>
31 #include <linux/serial_reg.h>
32 #include <linux/major.h>
33 #include <linux/string.h>
34 #include <linux/fcntl.h>
35 #include <linux/ptrace.h>
36 #include <linux/gfp.h>
37 #include <linux/ioport.h>
39 #include <linux/delay.h>
40 #include <linux/pci.h>
41 #include <linux/bitops.h>
43 #include <asm/system.h>
46 #include <asm/uaccess.h>
48 #include "mxser_new.h"
50 #define MXSER_VERSION "2.0.2" /* 1.10 */
51 #define MXSERMAJOR 174
52 #define MXSERCUMAJOR 175
54 #define MXSER_BOARDS 4 /* Max. boards */
55 #define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
56 #define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
57 #define MXSER_ISR_PASS_LIMIT 100
59 #define MXSER_ERR_IOADDR -1
60 #define MXSER_ERR_IRQ -2
61 #define MXSER_ERR_IRQ_CONFLIT -3
62 #define MXSER_ERR_VECTOR -4
64 /*CheckIsMoxaMust return value*/
65 #define MOXA_OTHER_UART 0x00
66 #define MOXA_MUST_MU150_HWID 0x01
67 #define MOXA_MUST_MU860_HWID 0x02
69 #define WAKEUP_CHARS 256
71 #define UART_MCR_AFE 0x20
72 #define UART_LSR_SPECIAL 0x1E
74 #define PCI_DEVICE_ID_CB108 0x1080
75 #define PCI_DEVICE_ID_CB114 0x1142
76 #define PCI_DEVICE_ID_CB134I 0x1341
77 #define PCI_DEVICE_ID_CP138U 0x1380
78 #define PCI_DEVICE_ID_POS104UL 0x1044
81 #define C168_ASIC_ID 1
82 #define C104_ASIC_ID 2
83 #define C102_ASIC_ID 0xB
84 #define CI132_ASIC_ID 4
85 #define CI134_ASIC_ID 3
86 #define CI104J_ASIC_ID 5
88 #define MXSER_HIGHBAUD 1
91 /* This is only for PCI */
101 } Gpci_uart_info[] = {
102 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
103 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
104 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
106 #define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
108 struct mxser_cardinfo {
114 static const struct mxser_cardinfo mxser_cards[] = {
115 /* 0*/ { 8, "C168 series", },
116 { 4, "C104 series", },
117 { 4, "CI-104J series", },
118 { 8, "C168H/PCI series", },
119 { 4, "C104H/PCI series", },
120 /* 5*/ { 4, "C102 series", MXSER_HAS2 }, /* C102-ISA */
121 { 4, "CI-132 series", MXSER_HAS2 },
122 { 4, "CI-134 series", },
123 { 2, "CP-132 series", },
124 { 4, "CP-114 series", },
125 /*10*/ { 4, "CT-114 series", },
126 { 2, "CP-102 series", MXSER_HIGHBAUD },
127 { 4, "CP-104U series", },
128 { 8, "CP-168U series", },
129 { 2, "CP-132U series", },
130 /*15*/ { 4, "CP-134U series", },
131 { 4, "CP-104JU series", },
132 { 8, "Moxa UC7000 Serial", }, /* RC7000 */
133 { 8, "CP-118U series", },
134 { 2, "CP-102UL series", },
135 /*20*/ { 2, "CP-102U series", },
136 { 8, "CP-118EL series", },
137 { 8, "CP-168EL series", },
138 { 4, "CP-104EL series", },
139 { 8, "CB-108 series", },
140 /*25*/ { 4, "CB-114 series", },
141 { 4, "CB-134I series", },
142 { 8, "CP-138U series", },
143 { 4, "POS-104UL series", }
146 /* driver_data correspond to the lines in the structure above
147 see also ISA probe function before you change something */
148 static struct pci_device_id mxser_pcibrds[] = {
149 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168), .driver_data = 3 },
150 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104), .driver_data = 4 },
151 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132), .driver_data = 8 },
152 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114), .driver_data = 9 },
153 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114), .driver_data = 10 },
154 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102), .driver_data = 11 },
155 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 12 },
156 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 13 },
157 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 14 },
158 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 15 },
159 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 16 },
160 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 17 },
161 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 18 },
162 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 19 },
163 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 20 },
164 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 21 },
165 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 22 },
166 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 23 },
167 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 24 },
168 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 25 },
169 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 26 },
170 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 27 },
171 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 28 },
174 MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
176 static int mxvar_baud_table[] = {
177 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
178 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
180 static unsigned int mxvar_baud_table1[] = {
181 0, B50, B75, B110, B134, B150, B200, B300, B600, B1200, B1800, B2400,
182 B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B921600
184 #define BAUD_TABLE_NO ARRAY_SIZE(mxvar_baud_table)
186 #define B_SPEC B2000000
188 static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
189 static int ttymajor = MXSERMAJOR;
190 static int calloutmajor = MXSERCUMAJOR;
192 /* Variables for insmod */
194 MODULE_AUTHOR("Casper Yang");
195 MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
196 module_param_array(ioaddr, int, NULL, 0);
197 module_param(ttymajor, int, 0);
198 MODULE_LICENSE("GPL");
202 unsigned long rxcnt[MXSER_PORTS];
203 unsigned long txcnt[MXSER_PORTS];
210 unsigned long up_rxcnt;
211 unsigned long up_txcnt;
213 unsigned char hold_reason;
216 struct mxser_mon_ext {
217 unsigned long rx_cnt[32];
218 unsigned long tx_cnt[32];
219 unsigned long up_rxcnt[32];
220 unsigned long up_txcnt[32];
221 int modem_status[32];
235 struct mxser_board *board;
236 struct tty_struct *tty;
238 unsigned long ioaddr;
239 unsigned long opmode_ioaddr;
243 int rx_trigger; /* Rx fifo trigger level */
245 int baud_base; /* max. speed */
247 int type; /* UART type */
248 int flags; /* defined in tty.h */
251 int x_char; /* xon/xoff character */
252 int IER; /* Interrupt Enable Register */
253 int MCR; /* Modem control register */
255 unsigned char stop_rx;
256 unsigned char ldisc_stop_rx;
260 unsigned short closing_wait;
261 unsigned char err_shadow;
264 int count; /* # of fd on device */
265 int blocked_open; /* # of blocked opens */
266 struct async_icount icount; /* kernel counters for 4 input interrupts */
269 int read_status_mask;
270 int ignore_status_mask;
272 unsigned char *xmit_buf;
277 struct ktermios normal_termios;
279 struct mxser_mon mon_data;
282 wait_queue_head_t open_wait;
283 wait_queue_head_t delta_msr_wait;
289 const struct mxser_cardinfo *info;
290 unsigned long vector;
291 unsigned long vector_mask;
296 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
299 struct mxser_mstatus {
307 static struct mxser_mstatus GMStatus[MXSER_PORTS];
309 static int mxserBoardCAP[MXSER_BOARDS] = {
311 /* 0x180, 0x280, 0x200, 0x320 */
314 static struct mxser_board mxser_boards[MXSER_BOARDS];
315 static struct tty_driver *mxvar_sdriver;
316 static struct mxser_log mxvar_log;
317 static int mxvar_diagflag;
318 static unsigned char mxser_msr[MXSER_PORTS + 1];
319 static struct mxser_mon_ext mon_data_ext;
320 static int mxser_set_baud_method[MXSER_PORTS + 1];
323 static int __devinit CheckIsMoxaMust(int io)
328 outb(0, io + UART_LCR);
329 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
330 oldmcr = inb(io + UART_MCR);
331 outb(0, io + UART_MCR);
332 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
333 if ((hwid = inb(io + UART_MCR)) != 0) {
334 outb(oldmcr, io + UART_MCR);
335 return MOXA_OTHER_UART;
338 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
339 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
340 if (hwid == Gpci_uart_info[i].type)
343 return MOXA_OTHER_UART;
347 static void process_txrx_fifo(struct mxser_port *info)
351 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
352 info->rx_trigger = 1;
353 info->rx_high_water = 1;
354 info->rx_low_water = 1;
355 info->xmit_fifo_size = 1;
357 for (i = 0; i < UART_INFO_NUM; i++)
358 if (info->board->chip_flag == Gpci_uart_info[i].type) {
359 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
360 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
361 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
362 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
367 static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
369 unsigned char status = 0;
371 status = inb(baseaddr + UART_MSR);
373 mxser_msr[port] &= 0x0F;
374 mxser_msr[port] |= status;
375 status = mxser_msr[port];
382 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
383 struct mxser_port *port)
385 DECLARE_WAITQUEUE(wait, current);
391 * If non-blocking mode is set, or the port is not enabled,
392 * then make the check up front and then exit.
394 if ((filp->f_flags & O_NONBLOCK) ||
395 test_bit(TTY_IO_ERROR, &tty->flags)) {
396 port->flags |= ASYNC_NORMAL_ACTIVE;
400 if (tty->termios->c_cflag & CLOCAL)
404 * Block waiting for the carrier detect and the line to become
405 * free (i.e., not in use by the callout). While we are in
406 * this loop, port->count is dropped by one, so that
407 * mxser_close() knows when to free things. We restore it upon
408 * exit, either normal or abnormal.
411 add_wait_queue(&port->open_wait, &wait);
413 spin_lock_irqsave(&port->slock, flags);
414 if (!tty_hung_up_p(filp))
416 spin_unlock_irqrestore(&port->slock, flags);
417 port->blocked_open++;
419 spin_lock_irqsave(&port->slock, flags);
420 outb(inb(port->ioaddr + UART_MCR) |
421 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
422 spin_unlock_irqrestore(&port->slock, flags);
423 set_current_state(TASK_INTERRUPTIBLE);
424 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
425 if (port->flags & ASYNC_HUP_NOTIFY)
428 retval = -ERESTARTSYS;
431 if (!(port->flags & ASYNC_CLOSING) &&
433 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
435 if (signal_pending(current)) {
436 retval = -ERESTARTSYS;
441 set_current_state(TASK_RUNNING);
442 remove_wait_queue(&port->open_wait, &wait);
443 if (!tty_hung_up_p(filp))
445 port->blocked_open--;
448 port->flags |= ASYNC_NORMAL_ACTIVE;
452 static int mxser_set_baud(struct mxser_port *info, long newspd)
459 if (!info->tty || !info->tty->termios)
465 if (newspd > info->max_baud)
468 info->realbaud = newspd;
469 for (i = 0; i < BAUD_TABLE_NO; i++)
470 if (newspd == mxvar_baud_table[i])
472 if (i == BAUD_TABLE_NO) {
473 quot = info->baud_base / info->speed;
474 if (info->speed <= 0 || info->speed > info->max_baud)
478 quot = (2 * info->baud_base / 269);
480 quot = info->baud_base / newspd;
488 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
489 info->timeout += HZ / 50; /* Add .02 seconds of slop */
492 info->MCR |= UART_MCR_DTR;
493 outb(info->MCR, info->ioaddr + UART_MCR);
495 info->MCR &= ~UART_MCR_DTR;
496 outb(info->MCR, info->ioaddr + UART_MCR);
500 cval = inb(info->ioaddr + UART_LCR);
502 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
504 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
505 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
506 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
508 if (i == BAUD_TABLE_NO) {
509 quot = info->baud_base % info->speed;
511 if ((quot % info->speed) > (info->speed / 2)) {
517 SET_MOXA_MUST_ENUM_VALUE(info->ioaddr, quot);
519 SET_MOXA_MUST_ENUM_VALUE(info->ioaddr, 0);
525 * This routine is called to set the UART divisor registers to match
526 * the specified baud rate for a serial port.
528 static int mxser_change_speed(struct mxser_port *info,
529 struct ktermios *old_termios)
531 unsigned cflag, cval, fcr;
533 unsigned char status;
536 if (!info->tty || !info->tty->termios)
538 cflag = info->tty->termios->c_cflag;
542 if (mxser_set_baud_method[info->tty->index] == 0) {
543 if ((cflag & CBAUD) == B_SPEC)
546 baud = tty_get_baud_rate(info->tty);
547 mxser_set_baud(info, baud);
550 /* byte size and parity */
551 switch (cflag & CSIZE) {
566 break; /* too keep GCC shut... */
571 cval |= UART_LCR_PARITY;
572 if (!(cflag & PARODD))
573 cval |= UART_LCR_EPAR;
575 cval |= UART_LCR_SPAR;
577 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
578 if (info->board->chip_flag) {
579 fcr = UART_FCR_ENABLE_FIFO;
580 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
581 SET_MOXA_MUST_FIFO_VALUE(info);
585 fcr = UART_FCR_ENABLE_FIFO;
586 if (info->board->chip_flag) {
587 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
588 SET_MOXA_MUST_FIFO_VALUE(info);
590 switch (info->rx_trigger) {
592 fcr |= UART_FCR_TRIGGER_1;
595 fcr |= UART_FCR_TRIGGER_4;
598 fcr |= UART_FCR_TRIGGER_8;
601 fcr |= UART_FCR_TRIGGER_14;
607 /* CTS flow control flag and modem status interrupts */
608 info->IER &= ~UART_IER_MSI;
609 info->MCR &= ~UART_MCR_AFE;
610 if (cflag & CRTSCTS) {
611 info->flags |= ASYNC_CTS_FLOW;
612 info->IER |= UART_IER_MSI;
613 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
614 info->MCR |= UART_MCR_AFE;
616 status = inb(info->ioaddr + UART_MSR);
617 if (info->tty->hw_stopped) {
618 if (status & UART_MSR_CTS) {
619 info->tty->hw_stopped = 0;
620 if (info->type != PORT_16550A &&
621 !info->board->chip_flag) {
622 outb(info->IER & ~UART_IER_THRI,
625 info->IER |= UART_IER_THRI;
626 outb(info->IER, info->ioaddr +
629 tty_wakeup(info->tty);
632 if (!(status & UART_MSR_CTS)) {
633 info->tty->hw_stopped = 1;
634 if ((info->type != PORT_16550A) &&
635 (!info->board->chip_flag)) {
636 info->IER &= ~UART_IER_THRI;
637 outb(info->IER, info->ioaddr +
644 info->flags &= ~ASYNC_CTS_FLOW;
646 outb(info->MCR, info->ioaddr + UART_MCR);
647 if (cflag & CLOCAL) {
648 info->flags &= ~ASYNC_CHECK_CD;
650 info->flags |= ASYNC_CHECK_CD;
651 info->IER |= UART_IER_MSI;
653 outb(info->IER, info->ioaddr + UART_IER);
656 * Set up parity check flag
658 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
659 if (I_INPCK(info->tty))
660 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
661 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
662 info->read_status_mask |= UART_LSR_BI;
664 info->ignore_status_mask = 0;
666 if (I_IGNBRK(info->tty)) {
667 info->ignore_status_mask |= UART_LSR_BI;
668 info->read_status_mask |= UART_LSR_BI;
670 * If we're ignore parity and break indicators, ignore
671 * overruns too. (For real raw support).
673 if (I_IGNPAR(info->tty)) {
674 info->ignore_status_mask |=
678 info->read_status_mask |=
684 if (info->board->chip_flag) {
685 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
686 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
687 if (I_IXON(info->tty)) {
688 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
690 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
692 if (I_IXOFF(info->tty)) {
693 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
695 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
700 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
701 outb(cval, info->ioaddr + UART_LCR);
706 static void mxser_check_modem_status(struct mxser_port *port, int status)
708 /* update input line counters */
709 if (status & UART_MSR_TERI)
711 if (status & UART_MSR_DDSR)
713 if (status & UART_MSR_DDCD)
715 if (status & UART_MSR_DCTS)
717 port->mon_data.modem_status = status;
718 wake_up_interruptible(&port->delta_msr_wait);
720 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
721 if (status & UART_MSR_DCD)
722 wake_up_interruptible(&port->open_wait);
725 if (port->flags & ASYNC_CTS_FLOW) {
726 if (port->tty->hw_stopped) {
727 if (status & UART_MSR_CTS) {
728 port->tty->hw_stopped = 0;
730 if ((port->type != PORT_16550A) &&
731 (!port->board->chip_flag)) {
732 outb(port->IER & ~UART_IER_THRI,
733 port->ioaddr + UART_IER);
734 port->IER |= UART_IER_THRI;
735 outb(port->IER, port->ioaddr +
738 tty_wakeup(port->tty);
741 if (!(status & UART_MSR_CTS)) {
742 port->tty->hw_stopped = 1;
743 if (port->type != PORT_16550A &&
744 !port->board->chip_flag) {
745 port->IER &= ~UART_IER_THRI;
746 outb(port->IER, port->ioaddr +
754 static int mxser_startup(struct mxser_port *info)
759 page = __get_free_page(GFP_KERNEL);
763 spin_lock_irqsave(&info->slock, flags);
765 if (info->flags & ASYNC_INITIALIZED) {
767 spin_unlock_irqrestore(&info->slock, flags);
771 if (!info->ioaddr || !info->type) {
773 set_bit(TTY_IO_ERROR, &info->tty->flags);
775 spin_unlock_irqrestore(&info->slock, flags);
781 info->xmit_buf = (unsigned char *) page;
784 * Clear the FIFO buffers and disable them
785 * (they will be reenabled in mxser_change_speed())
787 if (info->board->chip_flag)
788 outb((UART_FCR_CLEAR_RCVR |
789 UART_FCR_CLEAR_XMIT |
790 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
792 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
793 info->ioaddr + UART_FCR);
796 * At this point there's no way the LSR could still be 0xFF;
797 * if it is, then bail out, because there's likely no UART
800 if (inb(info->ioaddr + UART_LSR) == 0xff) {
801 spin_unlock_irqrestore(&info->slock, flags);
802 if (capable(CAP_SYS_ADMIN)) {
804 set_bit(TTY_IO_ERROR, &info->tty->flags);
811 * Clear the interrupt registers.
813 (void) inb(info->ioaddr + UART_LSR);
814 (void) inb(info->ioaddr + UART_RX);
815 (void) inb(info->ioaddr + UART_IIR);
816 (void) inb(info->ioaddr + UART_MSR);
819 * Now, initialize the UART
821 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
822 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
823 outb(info->MCR, info->ioaddr + UART_MCR);
826 * Finally, enable interrupts
828 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
830 if (info->board->chip_flag)
831 info->IER |= MOXA_MUST_IER_EGDAI;
832 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
835 * And clear the interrupt registers again for luck.
837 (void) inb(info->ioaddr + UART_LSR);
838 (void) inb(info->ioaddr + UART_RX);
839 (void) inb(info->ioaddr + UART_IIR);
840 (void) inb(info->ioaddr + UART_MSR);
843 clear_bit(TTY_IO_ERROR, &info->tty->flags);
844 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
847 * and set the speed of the serial port
849 mxser_change_speed(info, NULL);
850 info->flags |= ASYNC_INITIALIZED;
851 spin_unlock_irqrestore(&info->slock, flags);
857 * This routine will shutdown a serial port; interrupts maybe disabled, and
858 * DTR is dropped if the hangup on close termio flag is on.
860 static void mxser_shutdown(struct mxser_port *info)
864 if (!(info->flags & ASYNC_INITIALIZED))
867 spin_lock_irqsave(&info->slock, flags);
870 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
871 * here so the queue might never be waken up
873 wake_up_interruptible(&info->delta_msr_wait);
876 * Free the IRQ, if necessary
878 if (info->xmit_buf) {
879 free_page((unsigned long) info->xmit_buf);
880 info->xmit_buf = NULL;
884 outb(0x00, info->ioaddr + UART_IER);
886 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
887 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
888 outb(info->MCR, info->ioaddr + UART_MCR);
890 /* clear Rx/Tx FIFO's */
891 if (info->board->chip_flag)
892 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
893 MOXA_MUST_FCR_GDA_MODE_ENABLE,
894 info->ioaddr + UART_FCR);
896 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
897 info->ioaddr + UART_FCR);
899 /* read data port to reset things */
900 (void) inb(info->ioaddr + UART_RX);
903 set_bit(TTY_IO_ERROR, &info->tty->flags);
905 info->flags &= ~ASYNC_INITIALIZED;
907 if (info->board->chip_flag)
908 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
910 spin_unlock_irqrestore(&info->slock, flags);
914 * This routine is called whenever a serial port is opened. It
915 * enables interrupts for a serial port, linking in its async structure into
916 * the IRQ chain. It also performs the serial-specific
917 * initialization for the tty structure.
919 static int mxser_open(struct tty_struct *tty, struct file *filp)
921 struct mxser_port *info;
926 if (line == MXSER_PORTS)
928 if (line < 0 || line > MXSER_PORTS)
930 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
934 tty->driver_data = info;
937 * Start up serial port
939 spin_lock_irqsave(&info->slock, flags);
941 spin_unlock_irqrestore(&info->slock, flags);
942 retval = mxser_startup(info);
946 retval = mxser_block_til_ready(tty, filp, info);
950 /* unmark here for very high baud rate (ex. 921600 bps) used */
951 tty->low_latency = 1;
956 * This routine is called when the serial port gets closed. First, we
957 * wait for the last remaining data to be sent. Then, we unlink its
958 * async structure from the interrupt chain if necessary, and we free
959 * that IRQ if nothing is left in the chain.
961 static void mxser_close(struct tty_struct *tty, struct file *filp)
963 struct mxser_port *info = tty->driver_data;
965 unsigned long timeout;
968 if (tty->index == MXSER_PORTS)
973 spin_lock_irqsave(&info->slock, flags);
975 if (tty_hung_up_p(filp)) {
976 spin_unlock_irqrestore(&info->slock, flags);
979 if ((tty->count == 1) && (info->count != 1)) {
981 * Uh, oh. tty->count is 1, which means that the tty
982 * structure will be freed. Info->count should always
983 * be one in these conditions. If it's greater than
984 * one, we've got real problems, since it means the
985 * serial port won't be shutdown.
987 printk(KERN_ERR "mxser_close: bad serial port count; "
988 "tty->count is 1, info->count is %d\n", info->count);
991 if (--info->count < 0) {
992 printk(KERN_ERR "mxser_close: bad serial port count for "
993 "ttys%d: %d\n", tty->index, info->count);
997 spin_unlock_irqrestore(&info->slock, flags);
1000 info->flags |= ASYNC_CLOSING;
1001 spin_unlock_irqrestore(&info->slock, flags);
1003 * Save the termios structure, since this port may have
1004 * separate termios for callout and dialin.
1006 if (info->flags & ASYNC_NORMAL_ACTIVE)
1007 info->normal_termios = *tty->termios;
1009 * Now we wait for the transmit buffer to clear; and we notify
1010 * the line discipline to only process XON/XOFF characters.
1013 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1014 tty_wait_until_sent(tty, info->closing_wait);
1016 * At this point we stop accepting input. To do this, we
1017 * disable the receive line status interrupts, and tell the
1018 * interrupt driver to stop checking the data ready bit in the
1019 * line status register.
1021 info->IER &= ~UART_IER_RLSI;
1022 if (info->board->chip_flag)
1023 info->IER &= ~MOXA_MUST_RECV_ISR;
1025 if (info->flags & ASYNC_INITIALIZED) {
1026 outb(info->IER, info->ioaddr + UART_IER);
1028 * Before we drop DTR, make sure the UART transmitter
1029 * has completely drained; this is especially
1030 * important if there is a transmit FIFO!
1032 timeout = jiffies + HZ;
1033 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
1034 schedule_timeout_interruptible(5);
1035 if (time_after(jiffies, timeout))
1039 mxser_shutdown(info);
1041 if (tty->driver->flush_buffer)
1042 tty->driver->flush_buffer(tty);
1044 tty_ldisc_flush(tty);
1049 if (info->blocked_open) {
1050 if (info->close_delay)
1051 schedule_timeout_interruptible(info->close_delay);
1052 wake_up_interruptible(&info->open_wait);
1055 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1058 static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1061 struct mxser_port *info = tty->driver_data;
1062 unsigned long flags;
1064 if (!info->xmit_buf)
1068 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1069 SERIAL_XMIT_SIZE - info->xmit_head));
1073 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1074 spin_lock_irqsave(&info->slock, flags);
1075 info->xmit_head = (info->xmit_head + c) &
1076 (SERIAL_XMIT_SIZE - 1);
1077 info->xmit_cnt += c;
1078 spin_unlock_irqrestore(&info->slock, flags);
1085 if (info->xmit_cnt && !tty->stopped) {
1086 if (!tty->hw_stopped ||
1087 (info->type == PORT_16550A) ||
1088 (info->board->chip_flag)) {
1089 spin_lock_irqsave(&info->slock, flags);
1090 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1092 info->IER |= UART_IER_THRI;
1093 outb(info->IER, info->ioaddr + UART_IER);
1094 spin_unlock_irqrestore(&info->slock, flags);
1100 static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1102 struct mxser_port *info = tty->driver_data;
1103 unsigned long flags;
1105 if (!info->xmit_buf)
1108 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1111 spin_lock_irqsave(&info->slock, flags);
1112 info->xmit_buf[info->xmit_head++] = ch;
1113 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1115 spin_unlock_irqrestore(&info->slock, flags);
1116 if (!tty->stopped) {
1117 if (!tty->hw_stopped ||
1118 (info->type == PORT_16550A) ||
1119 info->board->chip_flag) {
1120 spin_lock_irqsave(&info->slock, flags);
1121 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1122 info->IER |= UART_IER_THRI;
1123 outb(info->IER, info->ioaddr + UART_IER);
1124 spin_unlock_irqrestore(&info->slock, flags);
1130 static void mxser_flush_chars(struct tty_struct *tty)
1132 struct mxser_port *info = tty->driver_data;
1133 unsigned long flags;
1135 if (info->xmit_cnt <= 0 ||
1139 (info->type != PORT_16550A) &&
1140 (!info->board->chip_flag)
1144 spin_lock_irqsave(&info->slock, flags);
1146 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1147 info->IER |= UART_IER_THRI;
1148 outb(info->IER, info->ioaddr + UART_IER);
1150 spin_unlock_irqrestore(&info->slock, flags);
1153 static int mxser_write_room(struct tty_struct *tty)
1155 struct mxser_port *info = tty->driver_data;
1158 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1164 static int mxser_chars_in_buffer(struct tty_struct *tty)
1166 struct mxser_port *info = tty->driver_data;
1167 return info->xmit_cnt;
1170 static void mxser_flush_buffer(struct tty_struct *tty)
1172 struct mxser_port *info = tty->driver_data;
1174 unsigned long flags;
1177 spin_lock_irqsave(&info->slock, flags);
1178 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1180 fcr = inb(info->ioaddr + UART_FCR);
1181 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1182 info->ioaddr + UART_FCR);
1183 outb(fcr, info->ioaddr + UART_FCR);
1185 spin_unlock_irqrestore(&info->slock, flags);
1191 * ------------------------------------------------------------
1192 * friends of mxser_ioctl()
1193 * ------------------------------------------------------------
1195 static int mxser_get_serial_info(struct mxser_port *info,
1196 struct serial_struct __user *retinfo)
1198 struct serial_struct tmp;
1202 memset(&tmp, 0, sizeof(tmp));
1203 tmp.type = info->type;
1204 tmp.line = info->tty->index;
1205 tmp.port = info->ioaddr;
1206 tmp.irq = info->board->irq;
1207 tmp.flags = info->flags;
1208 tmp.baud_base = info->baud_base;
1209 tmp.close_delay = info->close_delay;
1210 tmp.closing_wait = info->closing_wait;
1211 tmp.custom_divisor = info->custom_divisor;
1213 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1218 static int mxser_set_serial_info(struct mxser_port *info,
1219 struct serial_struct __user *new_info)
1221 struct serial_struct new_serial;
1222 unsigned long sl_flags;
1226 if (!new_info || !info->ioaddr)
1228 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1231 if ((new_serial.irq != info->board->irq) ||
1232 (new_serial.port != info->ioaddr) ||
1233 (new_serial.custom_divisor != info->custom_divisor) ||
1234 (new_serial.baud_base != info->baud_base))
1237 flags = info->flags & ASYNC_SPD_MASK;
1239 if (!capable(CAP_SYS_ADMIN)) {
1240 if ((new_serial.baud_base != info->baud_base) ||
1241 (new_serial.close_delay != info->close_delay) ||
1242 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
1244 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1245 (new_serial.flags & ASYNC_USR_MASK));
1248 * OK, past this point, all the error checking has been done.
1249 * At this point, we start making changes.....
1251 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1252 (new_serial.flags & ASYNC_FLAGS));
1253 info->close_delay = new_serial.close_delay * HZ / 100;
1254 info->closing_wait = new_serial.closing_wait * HZ / 100;
1255 info->tty->low_latency =
1256 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1257 info->tty->low_latency = 0;
1260 info->type = new_serial.type;
1262 process_txrx_fifo(info);
1264 if (info->flags & ASYNC_INITIALIZED) {
1265 if (flags != (info->flags & ASYNC_SPD_MASK)) {
1266 spin_lock_irqsave(&info->slock, sl_flags);
1267 mxser_change_speed(info, NULL);
1268 spin_unlock_irqrestore(&info->slock, sl_flags);
1271 retval = mxser_startup(info);
1277 * mxser_get_lsr_info - get line status register info
1279 * Purpose: Let user call ioctl() to get info when the UART physically
1280 * is emptied. On bus types like RS485, the transmitter must
1281 * release the bus after transmitting. This must be done when
1282 * the transmit shift register is empty, not be done when the
1283 * transmit holding register is empty. This functionality
1284 * allows an RS485 driver to be written in user space.
1286 static int mxser_get_lsr_info(struct mxser_port *info,
1287 unsigned int __user *value)
1289 unsigned char status;
1290 unsigned int result;
1291 unsigned long flags;
1293 spin_lock_irqsave(&info->slock, flags);
1294 status = inb(info->ioaddr + UART_LSR);
1295 spin_unlock_irqrestore(&info->slock, flags);
1296 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1297 return put_user(result, value);
1301 * This routine sends a break character out the serial port.
1303 static void mxser_send_break(struct mxser_port *info, int duration)
1305 unsigned long flags;
1309 set_current_state(TASK_INTERRUPTIBLE);
1310 spin_lock_irqsave(&info->slock, flags);
1311 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1312 info->ioaddr + UART_LCR);
1313 spin_unlock_irqrestore(&info->slock, flags);
1314 schedule_timeout(duration);
1315 spin_lock_irqsave(&info->slock, flags);
1316 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1317 info->ioaddr + UART_LCR);
1318 spin_unlock_irqrestore(&info->slock, flags);
1321 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
1323 struct mxser_port *info = tty->driver_data;
1324 unsigned char control, status;
1325 unsigned long flags;
1328 if (tty->index == MXSER_PORTS)
1329 return -ENOIOCTLCMD;
1330 if (test_bit(TTY_IO_ERROR, &tty->flags))
1333 control = info->MCR;
1335 spin_lock_irqsave(&info->slock, flags);
1336 status = inb(info->ioaddr + UART_MSR);
1337 if (status & UART_MSR_ANY_DELTA)
1338 mxser_check_modem_status(info, status);
1339 spin_unlock_irqrestore(&info->slock, flags);
1340 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1341 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1342 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1343 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1344 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1345 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1348 static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1349 unsigned int set, unsigned int clear)
1351 struct mxser_port *info = tty->driver_data;
1352 unsigned long flags;
1355 if (tty->index == MXSER_PORTS)
1356 return -ENOIOCTLCMD;
1357 if (test_bit(TTY_IO_ERROR, &tty->flags))
1360 spin_lock_irqsave(&info->slock, flags);
1362 if (set & TIOCM_RTS)
1363 info->MCR |= UART_MCR_RTS;
1364 if (set & TIOCM_DTR)
1365 info->MCR |= UART_MCR_DTR;
1367 if (clear & TIOCM_RTS)
1368 info->MCR &= ~UART_MCR_RTS;
1369 if (clear & TIOCM_DTR)
1370 info->MCR &= ~UART_MCR_DTR;
1372 outb(info->MCR, info->ioaddr + UART_MCR);
1373 spin_unlock_irqrestore(&info->slock, flags);
1377 static int __init mxser_program_mode(int port)
1389 id = inb(port + 1) & 0x1F;
1390 if ((id != C168_ASIC_ID) &&
1391 (id != C104_ASIC_ID) &&
1392 (id != C102_ASIC_ID) &&
1393 (id != CI132_ASIC_ID) &&
1394 (id != CI134_ASIC_ID) &&
1395 (id != CI104J_ASIC_ID))
1397 for (i = 0, j = 0; i < 4; i++) {
1401 } else if ((j == 1) && (n == 1)) {
1412 static void __init mxser_normal_mode(int port)
1416 outb(0xA5, port + 1);
1417 outb(0x80, port + 3);
1418 outb(12, port + 0); /* 9600 bps */
1420 outb(0x03, port + 3); /* 8 data bits */
1421 outb(0x13, port + 4); /* loop back mode */
1422 for (i = 0; i < 16; i++) {
1424 if ((n & 0x61) == 0x60)
1429 outb(0x00, port + 4);
1432 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1433 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1434 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1435 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1436 #define EN_CCMD 0x000 /* Chip's command register */
1437 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1438 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1439 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1440 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1441 #define EN0_DCFG 0x00E /* Data configuration reg WR */
1442 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1443 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1444 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1445 static int __init mxser_read_register(int port, unsigned short *regs)
1447 int i, k, value, id;
1450 id = mxser_program_mode(port);
1453 for (i = 0; i < 14; i++) {
1454 k = (i & 0x3F) | 0x180;
1455 for (j = 0x100; j > 0; j >>= 1) {
1456 outb(CHIP_CS, port);
1458 outb(CHIP_CS | CHIP_DO, port);
1459 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1461 outb(CHIP_CS, port);
1462 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1467 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1468 outb(CHIP_CS, port);
1469 outb(CHIP_CS | CHIP_SK, port);
1470 if (inb(port) & CHIP_DI)
1476 mxser_normal_mode(port);
1480 static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1482 struct mxser_port *port;
1488 /* if (copy_to_user(argp, mxsercfg,
1489 sizeof(struct mxser_hwconf) * 4))
1493 case MOXA_GET_MAJOR:
1494 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1498 case MOXA_GET_CUMAJOR:
1499 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1503 case MOXA_CHKPORTENABLE:
1506 for (i = 0; i < MXSER_BOARDS; i++)
1507 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1508 if (mxser_boards[i].ports[j].ioaddr)
1511 return put_user(result, (unsigned long __user *)argp);
1512 case MOXA_GETDATACOUNT:
1513 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1516 case MOXA_GETMSTATUS:
1517 for (i = 0; i < MXSER_BOARDS; i++)
1518 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1519 port = &mxser_boards[i].ports[j];
1522 if (!port->ioaddr) {
1523 GMStatus[i].dcd = 0;
1524 GMStatus[i].dsr = 0;
1525 GMStatus[i].cts = 0;
1529 if (!port->tty || !port->tty->termios)
1531 port->normal_termios.c_cflag;
1534 port->tty->termios->c_cflag;
1536 status = inb(port->ioaddr + UART_MSR);
1537 if (status & 0x80 /*UART_MSR_DCD */ )
1538 GMStatus[i].dcd = 1;
1540 GMStatus[i].dcd = 0;
1542 if (status & 0x20 /*UART_MSR_DSR */ )
1543 GMStatus[i].dsr = 1;
1545 GMStatus[i].dsr = 0;
1548 if (status & 0x10 /*UART_MSR_CTS */ )
1549 GMStatus[i].cts = 1;
1551 GMStatus[i].cts = 0;
1553 if (copy_to_user(argp, GMStatus,
1554 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1557 case MOXA_ASPP_MON_EXT: {
1559 unsigned long opmode;
1560 unsigned cflag, iflag;
1562 for (i = 0; i < MXSER_BOARDS; i++)
1563 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1564 port = &mxser_boards[i].ports[j];
1568 status = mxser_get_msr(port->ioaddr, 0, i);
1570 if (status & UART_MSR_TERI)
1572 if (status & UART_MSR_DDSR)
1574 if (status & UART_MSR_DDCD)
1576 if (status & UART_MSR_DCTS)
1579 port->mon_data.modem_status = status;
1580 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1581 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1582 mon_data_ext.up_rxcnt[i] =
1583 port->mon_data.up_rxcnt;
1584 mon_data_ext.up_txcnt[i] =
1585 port->mon_data.up_txcnt;
1586 mon_data_ext.modem_status[i] =
1587 port->mon_data.modem_status;
1588 mon_data_ext.baudrate[i] = port->realbaud;
1590 if (!port->tty || !port->tty->termios) {
1591 cflag = port->normal_termios.c_cflag;
1592 iflag = port->normal_termios.c_iflag;
1594 cflag = port->tty->termios->c_cflag;
1595 iflag = port->tty->termios->c_iflag;
1598 mon_data_ext.databits[i] = cflag & CSIZE;
1600 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1602 mon_data_ext.parity[i] =
1603 cflag & (PARENB | PARODD | CMSPAR);
1605 mon_data_ext.flowctrl[i] = 0x00;
1607 if (cflag & CRTSCTS)
1608 mon_data_ext.flowctrl[i] |= 0x03;
1610 if (iflag & (IXON | IXOFF))
1611 mon_data_ext.flowctrl[i] |= 0x0C;
1613 if (port->type == PORT_16550A)
1614 mon_data_ext.fifo[i] = 1;
1616 mon_data_ext.fifo[i] = 0;
1620 opmode = inb(port->opmode_ioaddr) >> shiftbit;
1621 opmode &= OP_MODE_MASK;
1623 mon_data_ext.iftype[i] = opmode;
1626 if (copy_to_user(argp, &mon_data_ext,
1627 sizeof(mon_data_ext)))
1633 return -ENOIOCTLCMD;
1638 static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1639 unsigned int cmd, unsigned long arg)
1641 struct mxser_port *info = tty->driver_data;
1642 struct async_icount cprev, cnow; /* kernel counter temps */
1643 struct serial_icounter_struct __user *p_cuser;
1644 unsigned long templ;
1645 unsigned long flags;
1647 void __user *argp = (void __user *)arg;
1650 if (tty->index == MXSER_PORTS)
1651 return mxser_ioctl_special(cmd, argp);
1653 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1655 unsigned long opmode;
1656 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1658 unsigned char val, mask;
1661 if (cmd == MOXA_SET_OP_MODE) {
1662 if (get_user(opmode, (int __user *) argp))
1664 if (opmode != RS232_MODE &&
1665 opmode != RS485_2WIRE_MODE &&
1666 opmode != RS422_MODE &&
1667 opmode != RS485_4WIRE_MODE)
1671 val = inb(info->opmode_ioaddr);
1673 val |= (opmode << shiftbit);
1674 outb(val, info->opmode_ioaddr);
1677 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1678 opmode &= OP_MODE_MASK;
1679 if (copy_to_user(argp, &opmode, sizeof(int)))
1685 if (cmd == MOXA_SET_SPECIAL_BAUD_RATE) {
1688 if (get_user(speed, (int __user *)argp))
1690 if (speed <= 0 || speed > info->max_baud)
1692 if (!info->tty || !info->tty->termios || !info->ioaddr)
1694 info->tty->termios->c_cflag &= ~(CBAUD | CBAUDEX);
1695 for (i = 0; i < BAUD_TABLE_NO; i++)
1696 if (speed == mxvar_baud_table[i])
1698 if (i == BAUD_TABLE_NO) {
1699 info->tty->termios->c_cflag |= B_SPEC;
1700 } else if (speed != 0)
1701 info->tty->termios->c_cflag |= mxvar_baud_table1[i];
1703 info->speed = speed;
1704 spin_lock_irqsave(&info->slock, flags);
1705 mxser_change_speed(info, NULL);
1706 spin_unlock_irqrestore(&info->slock, flags);
1709 } else if (cmd == MOXA_GET_SPECIAL_BAUD_RATE) {
1710 if (copy_to_user(argp, &info->speed, sizeof(int)))
1715 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1716 test_bit(TTY_IO_ERROR, &tty->flags))
1720 case TCSBRK: /* SVID version: non-zero arg --> no break */
1721 retval = tty_check_change(tty);
1724 tty_wait_until_sent(tty, 0);
1726 mxser_send_break(info, HZ / 4); /* 1/4 second */
1728 case TCSBRKP: /* support for POSIX tcsendbreak() */
1729 retval = tty_check_change(tty);
1732 tty_wait_until_sent(tty, 0);
1733 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1736 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1738 if (get_user(templ, (unsigned long __user *) argp))
1741 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1744 return mxser_get_serial_info(info, argp);
1746 return mxser_set_serial_info(info, argp);
1747 case TIOCSERGETLSR: /* Get line status register */
1748 return mxser_get_lsr_info(info, argp);
1750 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1751 * - mask passed in arg for lines of interest
1752 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1753 * Caller should use TIOCGICOUNT to see which one it was
1756 spin_lock_irqsave(&info->slock, flags);
1757 cnow = info->icount; /* note the counters on entry */
1758 spin_unlock_irqrestore(&info->slock, flags);
1760 wait_event_interruptible(info->delta_msr_wait, ({
1762 spin_lock_irqsave(&info->slock, flags);
1763 cnow = info->icount; /* atomic copy */
1764 spin_unlock_irqrestore(&info->slock, flags);
1766 ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1767 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1768 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1769 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts));
1773 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1774 * Return: write counters to the user passed counter struct
1775 * NB: both 1->0 and 0->1 transitions are counted except for
1776 * RI where only 0->1 is counted.
1779 spin_lock_irqsave(&info->slock, flags);
1780 cnow = info->icount;
1781 spin_unlock_irqrestore(&info->slock, flags);
1783 if (put_user(cnow.frame, &p_cuser->frame))
1785 if (put_user(cnow.brk, &p_cuser->brk))
1787 if (put_user(cnow.overrun, &p_cuser->overrun))
1789 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1791 if (put_user(cnow.parity, &p_cuser->parity))
1793 if (put_user(cnow.rx, &p_cuser->rx))
1795 if (put_user(cnow.tx, &p_cuser->tx))
1797 put_user(cnow.cts, &p_cuser->cts);
1798 put_user(cnow.dsr, &p_cuser->dsr);
1799 put_user(cnow.rng, &p_cuser->rng);
1800 put_user(cnow.dcd, &p_cuser->dcd);
1802 case MOXA_HighSpeedOn:
1803 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1804 case MOXA_SDS_RSTICOUNTER:
1805 info->mon_data.rxcnt = 0;
1806 info->mon_data.txcnt = 0;
1808 case MOXA_ASPP_SETBAUD:{
1810 if (get_user(baud, (long __user *)argp))
1812 spin_lock_irqsave(&info->slock, flags);
1813 mxser_set_baud(info, baud);
1814 spin_unlock_irqrestore(&info->slock, flags);
1817 case MOXA_ASPP_GETBAUD:
1818 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1823 case MOXA_ASPP_OQUEUE:{
1826 len = mxser_chars_in_buffer(tty);
1828 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1830 len += (lsr ? 0 : 1);
1832 if (copy_to_user(argp, &len, sizeof(int)))
1837 case MOXA_ASPP_MON: {
1840 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1841 mxser_check_modem_status(info, status);
1843 mcr = inb(info->ioaddr + UART_MCR);
1844 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1845 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1847 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1849 if (mcr & MOXA_MUST_MCR_TX_XON)
1850 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1852 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1854 if (info->tty->hw_stopped)
1855 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1857 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1859 if (copy_to_user(argp, &info->mon_data,
1860 sizeof(struct mxser_mon)))
1865 case MOXA_ASPP_LSTATUS: {
1866 if (copy_to_user(argp, &info->err_shadow,
1867 sizeof(unsigned char)))
1870 info->err_shadow = 0;
1873 case MOXA_SET_BAUD_METHOD: {
1876 if (get_user(method, (int __user *)argp))
1878 mxser_set_baud_method[tty->index] = method;
1879 if (copy_to_user(argp, &method, sizeof(int)))
1885 return -ENOIOCTLCMD;
1890 static void mxser_stoprx(struct tty_struct *tty)
1892 struct mxser_port *info = tty->driver_data;
1894 info->ldisc_stop_rx = 1;
1896 if (info->board->chip_flag) {
1897 info->IER &= ~MOXA_MUST_RECV_ISR;
1898 outb(info->IER, info->ioaddr + UART_IER);
1900 info->x_char = STOP_CHAR(tty);
1901 outb(0, info->ioaddr + UART_IER);
1902 info->IER |= UART_IER_THRI;
1903 outb(info->IER, info->ioaddr + UART_IER);
1907 if (info->tty->termios->c_cflag & CRTSCTS) {
1908 info->MCR &= ~UART_MCR_RTS;
1909 outb(info->MCR, info->ioaddr + UART_MCR);
1914 * This routine is called by the upper-layer tty layer to signal that
1915 * incoming characters should be throttled.
1917 static void mxser_throttle(struct tty_struct *tty)
1922 static void mxser_unthrottle(struct tty_struct *tty)
1924 struct mxser_port *info = tty->driver_data;
1927 info->ldisc_stop_rx = 0;
1932 if (info->board->chip_flag) {
1933 info->IER |= MOXA_MUST_RECV_ISR;
1934 outb(info->IER, info->ioaddr + UART_IER);
1936 info->x_char = START_CHAR(tty);
1937 outb(0, info->ioaddr + UART_IER);
1938 info->IER |= UART_IER_THRI;
1939 outb(info->IER, info->ioaddr + UART_IER);
1944 if (info->tty->termios->c_cflag & CRTSCTS) {
1945 info->MCR |= UART_MCR_RTS;
1946 outb(info->MCR, info->ioaddr + UART_MCR);
1951 * mxser_stop() and mxser_start()
1953 * This routines are called before setting or resetting tty->stopped.
1954 * They enable or disable transmitter interrupts, as necessary.
1956 static void mxser_stop(struct tty_struct *tty)
1958 struct mxser_port *info = tty->driver_data;
1959 unsigned long flags;
1961 spin_lock_irqsave(&info->slock, flags);
1962 if (info->IER & UART_IER_THRI) {
1963 info->IER &= ~UART_IER_THRI;
1964 outb(info->IER, info->ioaddr + UART_IER);
1966 spin_unlock_irqrestore(&info->slock, flags);
1969 static void mxser_start(struct tty_struct *tty)
1971 struct mxser_port *info = tty->driver_data;
1972 unsigned long flags;
1974 spin_lock_irqsave(&info->slock, flags);
1975 if (info->xmit_cnt && info->xmit_buf) {
1976 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1977 info->IER |= UART_IER_THRI;
1978 outb(info->IER, info->ioaddr + UART_IER);
1980 spin_unlock_irqrestore(&info->slock, flags);
1983 static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1985 struct mxser_port *info = tty->driver_data;
1986 unsigned long flags;
1988 spin_lock_irqsave(&info->slock, flags);
1989 mxser_change_speed(info, old_termios);
1990 spin_unlock_irqrestore(&info->slock, flags);
1992 if ((old_termios->c_cflag & CRTSCTS) &&
1993 !(tty->termios->c_cflag & CRTSCTS)) {
1994 tty->hw_stopped = 0;
1998 /* Handle sw stopped */
1999 if ((old_termios->c_iflag & IXON) &&
2000 !(tty->termios->c_iflag & IXON)) {
2003 if (info->board->chip_flag) {
2004 spin_lock_irqsave(&info->slock, flags);
2005 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2006 spin_unlock_irqrestore(&info->slock, flags);
2014 * mxser_wait_until_sent() --- wait until the transmitter is empty
2016 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2018 struct mxser_port *info = tty->driver_data;
2019 unsigned long orig_jiffies, char_time;
2022 if (info->type == PORT_UNKNOWN)
2025 if (info->xmit_fifo_size == 0)
2026 return; /* Just in case.... */
2028 orig_jiffies = jiffies;
2030 * Set the check interval to be 1/5 of the estimated time to
2031 * send a single character, and make it at least 1. The check
2032 * interval should also be less than the timeout.
2034 * Note: we have to use pretty tight timings here to satisfy
2037 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2038 char_time = char_time / 5;
2041 if (timeout && timeout < char_time)
2042 char_time = timeout;
2044 * If the transmitter hasn't cleared in twice the approximate
2045 * amount of time to send the entire FIFO, it probably won't
2046 * ever clear. This assumes the UART isn't doing flow
2047 * control, which is currently the case. Hence, if it ever
2048 * takes longer than info->timeout, this is probably due to a
2049 * UART bug of some kind. So, we clamp the timeout parameter at
2052 if (!timeout || timeout > 2 * info->timeout)
2053 timeout = 2 * info->timeout;
2054 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2055 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2056 timeout, char_time);
2057 printk("jiff=%lu...", jiffies);
2059 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
2060 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2061 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2063 schedule_timeout_interruptible(char_time);
2064 if (signal_pending(current))
2066 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2069 set_current_state(TASK_RUNNING);
2071 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2072 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2077 * This routine is called by tty_hangup() when a hangup is signaled.
2079 static void mxser_hangup(struct tty_struct *tty)
2081 struct mxser_port *info = tty->driver_data;
2083 mxser_flush_buffer(tty);
2084 mxser_shutdown(info);
2087 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2089 wake_up_interruptible(&info->open_wait);
2093 * mxser_rs_break() --- routine which turns the break handling on or off
2095 static void mxser_rs_break(struct tty_struct *tty, int break_state)
2097 struct mxser_port *info = tty->driver_data;
2098 unsigned long flags;
2100 spin_lock_irqsave(&info->slock, flags);
2101 if (break_state == -1)
2102 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2103 info->ioaddr + UART_LCR);
2105 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2106 info->ioaddr + UART_LCR);
2107 spin_unlock_irqrestore(&info->slock, flags);
2110 static void mxser_receive_chars(struct mxser_port *port, int *status)
2112 struct tty_struct *tty = port->tty;
2113 unsigned char ch, gdl;
2119 recv_room = tty->receive_room;
2120 if ((recv_room == 0) && (!port->ldisc_stop_rx))
2123 if (port->board->chip_flag != MOXA_OTHER_UART) {
2125 if (*status & UART_LSR_SPECIAL)
2127 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
2128 (*status & MOXA_MUST_LSR_RERR))
2130 if (*status & MOXA_MUST_LSR_RERR)
2133 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
2135 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
2136 gdl &= MOXA_MUST_GDL_MASK;
2137 if (gdl >= recv_room) {
2138 if (!port->ldisc_stop_rx)
2142 ch = inb(port->ioaddr + UART_RX);
2143 tty_insert_flip_char(tty, ch, 0);
2154 ch = inb(port->ioaddr + UART_RX);
2155 if (port->board->chip_flag && (*status & UART_LSR_OE))
2156 outb(0x23, port->ioaddr + UART_FCR);
2157 *status &= port->read_status_mask;
2158 if (*status & port->ignore_status_mask) {
2159 if (++ignored > 100)
2163 if (*status & UART_LSR_SPECIAL) {
2164 if (*status & UART_LSR_BI) {
2168 if (port->flags & ASYNC_SAK)
2170 } else if (*status & UART_LSR_PE) {
2172 port->icount.parity++;
2173 } else if (*status & UART_LSR_FE) {
2175 port->icount.frame++;
2176 } else if (*status & UART_LSR_OE) {
2178 port->icount.overrun++;
2182 tty_insert_flip_char(tty, ch, flag);
2184 if (cnt >= recv_room) {
2185 if (!port->ldisc_stop_rx)
2192 if (port->board->chip_flag)
2195 *status = inb(port->ioaddr + UART_LSR);
2196 } while (*status & UART_LSR_DR);
2199 mxvar_log.rxcnt[port->tty->index] += cnt;
2200 port->mon_data.rxcnt += cnt;
2201 port->mon_data.up_rxcnt += cnt;
2204 * We are called from an interrupt context with &port->slock
2205 * being held. Drop it temporarily in order to prevent
2206 * recursive locking.
2208 spin_unlock(&port->slock);
2209 tty_flip_buffer_push(tty);
2210 spin_lock(&port->slock);
2213 static void mxser_transmit_chars(struct mxser_port *port)
2218 outb(port->x_char, port->ioaddr + UART_TX);
2220 mxvar_log.txcnt[port->tty->index]++;
2221 port->mon_data.txcnt++;
2222 port->mon_data.up_txcnt++;
2227 if (port->xmit_buf == 0)
2230 if ((port->xmit_cnt <= 0) || port->tty->stopped ||
2231 (port->tty->hw_stopped &&
2232 (port->type != PORT_16550A) &&
2233 (!port->board->chip_flag))) {
2234 port->IER &= ~UART_IER_THRI;
2235 outb(port->IER, port->ioaddr + UART_IER);
2239 cnt = port->xmit_cnt;
2240 count = port->xmit_fifo_size;
2242 outb(port->xmit_buf[port->xmit_tail++],
2243 port->ioaddr + UART_TX);
2244 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2245 if (--port->xmit_cnt <= 0)
2247 } while (--count > 0);
2248 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
2250 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2251 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2252 port->icount.tx += (cnt - port->xmit_cnt);
2254 if (port->xmit_cnt < WAKEUP_CHARS)
2255 tty_wakeup(port->tty);
2257 if (port->xmit_cnt <= 0) {
2258 port->IER &= ~UART_IER_THRI;
2259 outb(port->IER, port->ioaddr + UART_IER);
2264 * This is the serial driver's generic interrupt routine
2266 static irqreturn_t mxser_interrupt(int irq, void *dev_id)
2269 struct mxser_board *brd = NULL;
2270 struct mxser_port *port;
2271 int max, irqbits, bits, msr;
2272 unsigned int int_cnt, pass_counter = 0;
2273 int handled = IRQ_NONE;
2275 for (i = 0; i < MXSER_BOARDS; i++)
2276 if (dev_id == &mxser_boards[i]) {
2281 if (i == MXSER_BOARDS)
2285 max = brd->info->nports;
2286 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
2287 irqbits = inb(brd->vector) & brd->vector_mask;
2288 if (irqbits == brd->vector_mask)
2291 handled = IRQ_HANDLED;
2292 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2293 if (irqbits == brd->vector_mask)
2297 port = &brd->ports[i];
2300 spin_lock(&port->slock);
2302 iir = inb(port->ioaddr + UART_IIR);
2303 if (iir & UART_IIR_NO_INT)
2305 iir &= MOXA_MUST_IIR_MASK;
2307 (port->flags & ASYNC_CLOSING) ||
2309 ASYNC_INITIALIZED)) {
2310 status = inb(port->ioaddr + UART_LSR);
2311 outb(0x27, port->ioaddr + UART_FCR);
2312 inb(port->ioaddr + UART_MSR);
2316 status = inb(port->ioaddr + UART_LSR);
2318 if (status & UART_LSR_PE)
2319 port->err_shadow |= NPPI_NOTIFY_PARITY;
2320 if (status & UART_LSR_FE)
2321 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2322 if (status & UART_LSR_OE)
2324 NPPI_NOTIFY_HW_OVERRUN;
2325 if (status & UART_LSR_BI)
2326 port->err_shadow |= NPPI_NOTIFY_BREAK;
2328 if (port->board->chip_flag) {
2329 if (iir == MOXA_MUST_IIR_GDA ||
2330 iir == MOXA_MUST_IIR_RDA ||
2331 iir == MOXA_MUST_IIR_RTO ||
2332 iir == MOXA_MUST_IIR_LSR)
2333 mxser_receive_chars(port,
2337 status &= port->read_status_mask;
2338 if (status & UART_LSR_DR)
2339 mxser_receive_chars(port,
2342 msr = inb(port->ioaddr + UART_MSR);
2343 if (msr & UART_MSR_ANY_DELTA)
2344 mxser_check_modem_status(port, msr);
2346 if (port->board->chip_flag) {
2347 if (iir == 0x02 && (status &
2349 mxser_transmit_chars(port);
2351 if (status & UART_LSR_THRE)
2352 mxser_transmit_chars(port);
2354 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2355 spin_unlock(&port->slock);
2363 static const struct tty_operations mxser_ops = {
2365 .close = mxser_close,
2366 .write = mxser_write,
2367 .put_char = mxser_put_char,
2368 .flush_chars = mxser_flush_chars,
2369 .write_room = mxser_write_room,
2370 .chars_in_buffer = mxser_chars_in_buffer,
2371 .flush_buffer = mxser_flush_buffer,
2372 .ioctl = mxser_ioctl,
2373 .throttle = mxser_throttle,
2374 .unthrottle = mxser_unthrottle,
2375 .set_termios = mxser_set_termios,
2377 .start = mxser_start,
2378 .hangup = mxser_hangup,
2379 .break_ctl = mxser_rs_break,
2380 .wait_until_sent = mxser_wait_until_sent,
2381 .tiocmget = mxser_tiocmget,
2382 .tiocmset = mxser_tiocmset,
2386 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2389 static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
2393 free_irq(brd->irq, brd);
2394 if (pdev != NULL) { /* PCI */
2396 pci_release_region(pdev, 2);
2397 pci_release_region(pdev, 3);
2400 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2401 release_region(brd->vector, 1);
2405 static int __devinit mxser_initbrd(struct mxser_board *brd,
2406 struct pci_dev *pdev)
2408 struct mxser_port *info;
2412 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
2414 for (i = 0; i < brd->info->nports; i++) {
2415 info = &brd->ports[i];
2418 info->ldisc_stop_rx = 0;
2420 /* Enhance mode enabled here */
2421 if (brd->chip_flag != MOXA_OTHER_UART)
2422 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
2424 info->flags = ASYNC_SHARE_IRQ;
2425 info->type = brd->uart_type;
2427 process_txrx_fifo(info);
2429 info->custom_divisor = info->baud_base * 16;
2430 info->close_delay = 5 * HZ / 10;
2431 info->closing_wait = 30 * HZ;
2432 info->normal_termios = mxvar_sdriver->init_termios;
2433 init_waitqueue_head(&info->open_wait);
2434 init_waitqueue_head(&info->delta_msr_wait);
2436 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2437 info->err_shadow = 0;
2438 spin_lock_init(&info->slock);
2440 /* before set INT ISR, disable all int */
2441 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2442 info->ioaddr + UART_IER);
2445 retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
2448 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2449 "conflict with another device.\n",
2450 brd->info->name, brd->irq);
2451 /* We hold resources, we need to release them. */
2452 mxser_release_res(brd, pdev, 0);
2457 static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
2460 unsigned short regs[16], irq;
2461 unsigned char scratch, scratch2;
2463 brd->chip_flag = MOXA_OTHER_UART;
2465 id = mxser_read_register(cap, regs);
2468 brd->info = &mxser_cards[0];
2471 brd->info = &mxser_cards[1];
2473 case CI104J_ASIC_ID:
2474 brd->info = &mxser_cards[2];
2477 brd->info = &mxser_cards[5];
2480 brd->info = &mxser_cards[6];
2483 brd->info = &mxser_cards[7];
2490 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2491 Flag-hack checks if configuration should be read as 2-port here. */
2492 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
2493 irq = regs[9] & 0xF000;
2494 irq = irq | (irq >> 4);
2495 if (irq != (regs[9] & 0xFF00))
2496 return MXSER_ERR_IRQ_CONFLIT;
2497 } else if (brd->info->nports == 4) {
2498 irq = regs[9] & 0xF000;
2499 irq = irq | (irq >> 4);
2500 irq = irq | (irq >> 8);
2502 return MXSER_ERR_IRQ_CONFLIT;
2503 } else if (brd->info->nports == 8) {
2504 irq = regs[9] & 0xF000;
2505 irq = irq | (irq >> 4);
2506 irq = irq | (irq >> 8);
2507 if ((irq != regs[9]) || (irq != regs[10]))
2508 return MXSER_ERR_IRQ_CONFLIT;
2512 return MXSER_ERR_IRQ;
2513 brd->irq = ((int)(irq & 0xF000) >> 12);
2514 for (i = 0; i < 8; i++)
2515 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
2516 if ((regs[12] & 0x80) == 0)
2517 return MXSER_ERR_VECTOR;
2518 brd->vector = (int)regs[11]; /* interrupt vector */
2520 brd->vector_mask = 0x00FF;
2522 brd->vector_mask = 0x000F;
2523 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2524 if (regs[12] & bits) {
2525 brd->ports[i].baud_base = 921600;
2526 brd->ports[i].max_baud = 921600;
2528 brd->ports[i].baud_base = 115200;
2529 brd->ports[i].max_baud = 115200;
2532 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2533 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2534 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2535 outb(scratch2, cap + UART_LCR);
2536 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2537 scratch = inb(cap + UART_IIR);
2540 brd->uart_type = PORT_16550A;
2542 brd->uart_type = PORT_16450;
2543 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2545 return MXSER_ERR_IOADDR;
2546 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2547 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2548 return MXSER_ERR_VECTOR;
2550 return brd->info->nports;
2553 static int __devinit mxser_probe(struct pci_dev *pdev,
2554 const struct pci_device_id *ent)
2557 struct mxser_board *brd;
2559 unsigned long ioaddress;
2560 int retval = -EINVAL;
2562 for (i = 0; i < MXSER_BOARDS; i++)
2563 if (mxser_boards[i].info == NULL)
2566 if (i >= MXSER_BOARDS) {
2567 printk(KERN_ERR "Too many Smartio/Industio family boards found "
2568 "(maximum %d), board not configured\n", MXSER_BOARDS);
2572 brd = &mxser_boards[i];
2573 brd->idx = i * MXSER_PORTS_PER_BOARD;
2574 printk(KERN_INFO "Found MOXA %s board (BusNo=%d, DevNo=%d)\n",
2575 mxser_cards[ent->driver_data].name,
2576 pdev->bus->number, PCI_SLOT(pdev->devfn));
2578 retval = pci_enable_device(pdev);
2580 printk(KERN_ERR "Moxa SmartI/O PCI enable fail !\n");
2585 ioaddress = pci_resource_start(pdev, 2);
2586 retval = pci_request_region(pdev, 2, "mxser(IO)");
2590 brd->info = &mxser_cards[ent->driver_data];
2591 for (i = 0; i < brd->info->nports; i++)
2592 brd->ports[i].ioaddr = ioaddress + 8 * i;
2595 ioaddress = pci_resource_start(pdev, 3);
2596 retval = pci_request_region(pdev, 3, "mxser(vector)");
2599 brd->vector = ioaddress;
2602 brd->irq = pdev->irq;
2604 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2605 brd->uart_type = PORT_16550A;
2606 brd->vector_mask = 0;
2608 for (i = 0; i < brd->info->nports; i++) {
2609 for (j = 0; j < UART_INFO_NUM; j++) {
2610 if (Gpci_uart_info[j].type == brd->chip_flag) {
2611 brd->ports[i].max_baud =
2612 Gpci_uart_info[j].max_baud;
2614 /* exception....CP-102 */
2615 if (brd->info->flags & MXSER_HIGHBAUD)
2616 brd->ports[i].max_baud = 921600;
2622 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2623 for (i = 0; i < brd->info->nports; i++) {
2625 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2627 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
2629 outb(0, ioaddress + 4); /* default set to RS232 mode */
2630 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
2633 for (i = 0; i < brd->info->nports; i++) {
2634 brd->vector_mask |= (1 << i);
2635 brd->ports[i].baud_base = 921600;
2638 /* mxser_initbrd will hook ISR. */
2639 retval = mxser_initbrd(brd, pdev);
2643 for (i = 0; i < brd->info->nports; i++)
2644 tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
2646 pci_set_drvdata(pdev, brd);
2650 pci_release_region(pdev, 2);
2660 static void __devexit mxser_remove(struct pci_dev *pdev)
2662 struct mxser_board *brd = pci_get_drvdata(pdev);
2665 for (i = 0; i < brd->info->nports; i++)
2666 tty_unregister_device(mxvar_sdriver, brd->idx + i);
2668 mxser_release_res(brd, pdev, 1);
2672 static struct pci_driver mxser_driver = {
2674 .id_table = mxser_pcibrds,
2675 .probe = mxser_probe,
2676 .remove = __devexit_p(mxser_remove)
2679 static int __init mxser_module_init(void)
2681 struct mxser_board *brd;
2683 unsigned int i, m, isaloop;
2686 pr_debug("Loading module mxser ...\n");
2688 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2692 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2695 /* Initialize the tty_driver structure */
2696 mxvar_sdriver->owner = THIS_MODULE;
2697 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2698 mxvar_sdriver->name = "ttyMI";
2699 mxvar_sdriver->major = ttymajor;
2700 mxvar_sdriver->minor_start = 0;
2701 mxvar_sdriver->num = MXSER_PORTS + 1;
2702 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2703 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2704 mxvar_sdriver->init_termios = tty_std_termios;
2705 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2706 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2707 tty_set_operations(mxvar_sdriver, &mxser_ops);
2709 retval = tty_register_driver(mxvar_sdriver);
2711 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2719 /* Start finding ISA boards here */
2720 for (isaloop = 0; isaloop < 2; isaloop++)
2721 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2723 cap = mxserBoardCAP[b]; /* predefined */
2725 cap = ioaddr[b]; /* module param */
2730 brd = &mxser_boards[m];
2731 retval = mxser_get_ISA_conf(cap, brd);
2734 printk(KERN_INFO "Found MOXA %s board "
2736 brd->info->name, ioaddr[b]);
2739 if (retval == MXSER_ERR_IRQ)
2740 printk(KERN_ERR "Invalid interrupt "
2741 "number, board not "
2743 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2744 printk(KERN_ERR "Invalid interrupt "
2745 "number, board not "
2747 else if (retval == MXSER_ERR_VECTOR)
2748 printk(KERN_ERR "Invalid interrupt "
2749 "vector, board not "
2751 else if (retval == MXSER_ERR_IOADDR)
2752 printk(KERN_ERR "Invalid I/O address, "
2753 "board not configured\n");
2759 /* mxser_initbrd will hook ISR. */
2760 if (mxser_initbrd(brd, NULL) < 0) {
2765 brd->idx = m * MXSER_PORTS_PER_BOARD;
2766 for (i = 0; i < brd->info->nports; i++)
2767 tty_register_device(mxvar_sdriver, brd->idx + i,
2773 retval = pci_register_driver(&mxser_driver);
2775 printk(KERN_ERR "Can't register pci driver\n");
2779 } /* else: we have some ISA cards under control */
2782 pr_debug("Done.\n");
2786 tty_unregister_driver(mxvar_sdriver);
2788 put_tty_driver(mxvar_sdriver);
2792 static void __exit mxser_module_exit(void)
2796 pr_debug("Unloading module mxser ...\n");
2798 pci_unregister_driver(&mxser_driver);
2800 for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
2801 if (mxser_boards[i].info != NULL)
2802 for (j = 0; j < mxser_boards[i].info->nports; j++)
2803 tty_unregister_device(mxvar_sdriver,
2804 mxser_boards[i].idx + j);
2805 tty_unregister_driver(mxvar_sdriver);
2806 put_tty_driver(mxvar_sdriver);
2808 for (i = 0; i < MXSER_BOARDS; i++)
2809 if (mxser_boards[i].info != NULL)
2810 mxser_release_res(&mxser_boards[i], NULL, 1);
2812 pr_debug("Done.\n");
2815 module_init(mxser_module_init);
2816 module_exit(mxser_module_exit);