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
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>
50 #define MXSER_VERSION "2.0.3" /* 1.11 */
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_CP114UL 0x1143
77 #define PCI_DEVICE_ID_CB134I 0x1341
78 #define PCI_DEVICE_ID_CP138U 0x1380
79 #define PCI_DEVICE_ID_POS104UL 0x1044
82 #define C168_ASIC_ID 1
83 #define C104_ASIC_ID 2
84 #define C102_ASIC_ID 0xB
85 #define CI132_ASIC_ID 4
86 #define CI134_ASIC_ID 3
87 #define CI104J_ASIC_ID 5
89 #define MXSER_HIGHBAUD 1
92 /* This is only for PCI */
102 } Gpci_uart_info[] = {
103 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
104 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
105 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
107 #define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
109 struct mxser_cardinfo {
115 static const struct mxser_cardinfo mxser_cards[] = {
116 /* 0*/ { "C168 series", 8, },
117 { "C104 series", 4, },
118 { "CI-104J series", 4, },
119 { "C168H/PCI series", 8, },
120 { "C104H/PCI series", 4, },
121 /* 5*/ { "C102 series", 4, MXSER_HAS2 }, /* C102-ISA */
122 { "CI-132 series", 4, MXSER_HAS2 },
123 { "CI-134 series", 4, },
124 { "CP-132 series", 2, },
125 { "CP-114 series", 4, },
126 /*10*/ { "CT-114 series", 4, },
127 { "CP-102 series", 2, MXSER_HIGHBAUD },
128 { "CP-104U series", 4, },
129 { "CP-168U series", 8, },
130 { "CP-132U series", 2, },
131 /*15*/ { "CP-134U series", 4, },
132 { "CP-104JU series", 4, },
133 { "Moxa UC7000 Serial", 8, }, /* RC7000 */
134 { "CP-118U series", 8, },
135 { "CP-102UL series", 2, },
136 /*20*/ { "CP-102U series", 2, },
137 { "CP-118EL series", 8, },
138 { "CP-168EL series", 8, },
139 { "CP-104EL series", 4, },
140 { "CB-108 series", 8, },
141 /*25*/ { "CB-114 series", 4, },
142 { "CB-134I series", 4, },
143 { "CP-138U series", 8, },
144 { "POS-104UL series", 4, },
145 { "CP-114UL series", 4, }
148 /* driver_data correspond to the lines in the structure above
149 see also ISA probe function before you change something */
150 static struct pci_device_id mxser_pcibrds[] = {
151 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168), .driver_data = 3 },
152 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104), .driver_data = 4 },
153 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132), .driver_data = 8 },
154 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114), .driver_data = 9 },
155 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114), .driver_data = 10 },
156 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102), .driver_data = 11 },
157 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 12 },
158 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 13 },
159 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 14 },
160 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 15 },
161 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 16 },
162 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 17 },
163 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 18 },
164 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 19 },
165 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 20 },
166 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 21 },
167 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 22 },
168 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 23 },
169 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 24 },
170 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 25 },
171 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 26 },
172 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 27 },
173 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 28 },
174 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP114UL), .driver_data = 29 },
177 MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
179 static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
180 static int ttymajor = MXSERMAJOR;
182 /* Variables for insmod */
184 MODULE_AUTHOR("Casper Yang");
185 MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
186 module_param_array(ioaddr, int, NULL, 0);
187 module_param(ttymajor, int, 0);
188 MODULE_LICENSE("GPL");
192 unsigned long rxcnt[MXSER_PORTS];
193 unsigned long txcnt[MXSER_PORTS];
200 unsigned long up_rxcnt;
201 unsigned long up_txcnt;
203 unsigned char hold_reason;
206 struct mxser_mon_ext {
207 unsigned long rx_cnt[32];
208 unsigned long tx_cnt[32];
209 unsigned long up_rxcnt[32];
210 unsigned long up_txcnt[32];
211 int modem_status[32];
225 struct mxser_board *board;
226 struct tty_struct *tty;
228 unsigned long ioaddr;
229 unsigned long opmode_ioaddr;
233 int rx_trigger; /* Rx fifo trigger level */
235 int baud_base; /* max. speed */
236 int type; /* UART type */
237 int flags; /* defined in tty.h */
239 int x_char; /* xon/xoff character */
240 int IER; /* Interrupt Enable Register */
241 int MCR; /* Modem control register */
243 unsigned char stop_rx;
244 unsigned char ldisc_stop_rx;
248 unsigned short closing_wait;
249 unsigned char err_shadow;
252 int count; /* # of fd on device */
253 int blocked_open; /* # of blocked opens */
254 struct async_icount icount; /* kernel counters for 4 input interrupts */
257 int read_status_mask;
258 int ignore_status_mask;
260 unsigned char *xmit_buf;
265 struct ktermios normal_termios;
267 struct mxser_mon mon_data;
270 wait_queue_head_t open_wait;
271 wait_queue_head_t delta_msr_wait;
277 const struct mxser_cardinfo *info;
278 unsigned long vector;
279 unsigned long vector_mask;
284 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
287 struct mxser_mstatus {
295 static struct mxser_mstatus GMStatus[MXSER_PORTS];
297 static int mxserBoardCAP[MXSER_BOARDS] = {
299 /* 0x180, 0x280, 0x200, 0x320 */
302 static struct mxser_board mxser_boards[MXSER_BOARDS];
303 static struct tty_driver *mxvar_sdriver;
304 static struct mxser_log mxvar_log;
305 static int mxvar_diagflag;
306 static unsigned char mxser_msr[MXSER_PORTS + 1];
307 static struct mxser_mon_ext mon_data_ext;
308 static int mxser_set_baud_method[MXSER_PORTS + 1];
311 static int __devinit CheckIsMoxaMust(unsigned long io)
316 outb(0, io + UART_LCR);
317 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
318 oldmcr = inb(io + UART_MCR);
319 outb(0, io + UART_MCR);
320 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
321 if ((hwid = inb(io + UART_MCR)) != 0) {
322 outb(oldmcr, io + UART_MCR);
323 return MOXA_OTHER_UART;
326 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
327 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
328 if (hwid == Gpci_uart_info[i].type)
331 return MOXA_OTHER_UART;
335 static void process_txrx_fifo(struct mxser_port *info)
339 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
340 info->rx_trigger = 1;
341 info->rx_high_water = 1;
342 info->rx_low_water = 1;
343 info->xmit_fifo_size = 1;
345 for (i = 0; i < UART_INFO_NUM; i++)
346 if (info->board->chip_flag == Gpci_uart_info[i].type) {
347 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
348 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
349 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
350 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
355 static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
357 unsigned char status = 0;
359 status = inb(baseaddr + UART_MSR);
361 mxser_msr[port] &= 0x0F;
362 mxser_msr[port] |= status;
363 status = mxser_msr[port];
370 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
371 struct mxser_port *port)
373 DECLARE_WAITQUEUE(wait, current);
379 * If non-blocking mode is set, or the port is not enabled,
380 * then make the check up front and then exit.
382 if ((filp->f_flags & O_NONBLOCK) ||
383 test_bit(TTY_IO_ERROR, &tty->flags)) {
384 port->flags |= ASYNC_NORMAL_ACTIVE;
388 if (tty->termios->c_cflag & CLOCAL)
392 * Block waiting for the carrier detect and the line to become
393 * free (i.e., not in use by the callout). While we are in
394 * this loop, port->count is dropped by one, so that
395 * mxser_close() knows when to free things. We restore it upon
396 * exit, either normal or abnormal.
399 add_wait_queue(&port->open_wait, &wait);
401 spin_lock_irqsave(&port->slock, flags);
402 if (!tty_hung_up_p(filp))
404 spin_unlock_irqrestore(&port->slock, flags);
405 port->blocked_open++;
407 spin_lock_irqsave(&port->slock, flags);
408 outb(inb(port->ioaddr + UART_MCR) |
409 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
410 spin_unlock_irqrestore(&port->slock, flags);
411 set_current_state(TASK_INTERRUPTIBLE);
412 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
413 if (port->flags & ASYNC_HUP_NOTIFY)
416 retval = -ERESTARTSYS;
419 if (!(port->flags & ASYNC_CLOSING) &&
421 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
423 if (signal_pending(current)) {
424 retval = -ERESTARTSYS;
429 set_current_state(TASK_RUNNING);
430 remove_wait_queue(&port->open_wait, &wait);
431 if (!tty_hung_up_p(filp))
433 port->blocked_open--;
436 port->flags |= ASYNC_NORMAL_ACTIVE;
440 static int mxser_set_baud(struct mxser_port *info, long newspd)
445 if (!info->tty || !info->tty->termios)
451 if (newspd > info->max_baud)
455 quot = 2 * info->baud_base / 269;
456 tty_encode_baud_rate(info->tty, 134, 134);
458 quot = info->baud_base / newspd;
461 baud = info->baud_base/quot;
462 tty_encode_baud_rate(info->tty, baud, baud);
467 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
468 info->timeout += HZ / 50; /* Add .02 seconds of slop */
471 info->MCR |= UART_MCR_DTR;
472 outb(info->MCR, info->ioaddr + UART_MCR);
474 info->MCR &= ~UART_MCR_DTR;
475 outb(info->MCR, info->ioaddr + UART_MCR);
479 cval = inb(info->ioaddr + UART_LCR);
481 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
483 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
484 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
485 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
488 if (C_BAUD(info->tty) == BOTHER) {
489 quot = info->baud_base % newspd;
491 if (quot % newspd > newspd / 2) {
497 SET_MOXA_MUST_ENUM_VALUE(info->ioaddr, quot);
500 SET_MOXA_MUST_ENUM_VALUE(info->ioaddr, 0);
506 * This routine is called to set the UART divisor registers to match
507 * the specified baud rate for a serial port.
509 static int mxser_change_speed(struct mxser_port *info,
510 struct ktermios *old_termios)
512 unsigned cflag, cval, fcr;
514 unsigned char status;
516 if (!info->tty || !info->tty->termios)
518 cflag = info->tty->termios->c_cflag;
522 if (mxser_set_baud_method[info->tty->index] == 0)
523 mxser_set_baud(info, tty_get_baud_rate(info->tty));
525 /* byte size and parity */
526 switch (cflag & CSIZE) {
541 break; /* too keep GCC shut... */
546 cval |= UART_LCR_PARITY;
547 if (!(cflag & PARODD))
548 cval |= UART_LCR_EPAR;
550 cval |= UART_LCR_SPAR;
552 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
553 if (info->board->chip_flag) {
554 fcr = UART_FCR_ENABLE_FIFO;
555 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
556 SET_MOXA_MUST_FIFO_VALUE(info);
560 fcr = UART_FCR_ENABLE_FIFO;
561 if (info->board->chip_flag) {
562 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
563 SET_MOXA_MUST_FIFO_VALUE(info);
565 switch (info->rx_trigger) {
567 fcr |= UART_FCR_TRIGGER_1;
570 fcr |= UART_FCR_TRIGGER_4;
573 fcr |= UART_FCR_TRIGGER_8;
576 fcr |= UART_FCR_TRIGGER_14;
582 /* CTS flow control flag and modem status interrupts */
583 info->IER &= ~UART_IER_MSI;
584 info->MCR &= ~UART_MCR_AFE;
585 if (cflag & CRTSCTS) {
586 info->flags |= ASYNC_CTS_FLOW;
587 info->IER |= UART_IER_MSI;
588 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
589 info->MCR |= UART_MCR_AFE;
591 status = inb(info->ioaddr + UART_MSR);
592 if (info->tty->hw_stopped) {
593 if (status & UART_MSR_CTS) {
594 info->tty->hw_stopped = 0;
595 if (info->type != PORT_16550A &&
596 !info->board->chip_flag) {
597 outb(info->IER & ~UART_IER_THRI,
600 info->IER |= UART_IER_THRI;
601 outb(info->IER, info->ioaddr +
604 tty_wakeup(info->tty);
607 if (!(status & UART_MSR_CTS)) {
608 info->tty->hw_stopped = 1;
609 if ((info->type != PORT_16550A) &&
610 (!info->board->chip_flag)) {
611 info->IER &= ~UART_IER_THRI;
612 outb(info->IER, info->ioaddr +
619 info->flags &= ~ASYNC_CTS_FLOW;
621 outb(info->MCR, info->ioaddr + UART_MCR);
622 if (cflag & CLOCAL) {
623 info->flags &= ~ASYNC_CHECK_CD;
625 info->flags |= ASYNC_CHECK_CD;
626 info->IER |= UART_IER_MSI;
628 outb(info->IER, info->ioaddr + UART_IER);
631 * Set up parity check flag
633 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
634 if (I_INPCK(info->tty))
635 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
636 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
637 info->read_status_mask |= UART_LSR_BI;
639 info->ignore_status_mask = 0;
641 if (I_IGNBRK(info->tty)) {
642 info->ignore_status_mask |= UART_LSR_BI;
643 info->read_status_mask |= UART_LSR_BI;
645 * If we're ignore parity and break indicators, ignore
646 * overruns too. (For real raw support).
648 if (I_IGNPAR(info->tty)) {
649 info->ignore_status_mask |=
653 info->read_status_mask |=
659 if (info->board->chip_flag) {
660 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
661 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
662 if (I_IXON(info->tty)) {
663 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
665 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
667 if (I_IXOFF(info->tty)) {
668 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
670 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
675 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
676 outb(cval, info->ioaddr + UART_LCR);
681 static void mxser_check_modem_status(struct mxser_port *port, int status)
683 /* update input line counters */
684 if (status & UART_MSR_TERI)
686 if (status & UART_MSR_DDSR)
688 if (status & UART_MSR_DDCD)
690 if (status & UART_MSR_DCTS)
692 port->mon_data.modem_status = status;
693 wake_up_interruptible(&port->delta_msr_wait);
695 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
696 if (status & UART_MSR_DCD)
697 wake_up_interruptible(&port->open_wait);
700 if (port->flags & ASYNC_CTS_FLOW) {
701 if (port->tty->hw_stopped) {
702 if (status & UART_MSR_CTS) {
703 port->tty->hw_stopped = 0;
705 if ((port->type != PORT_16550A) &&
706 (!port->board->chip_flag)) {
707 outb(port->IER & ~UART_IER_THRI,
708 port->ioaddr + UART_IER);
709 port->IER |= UART_IER_THRI;
710 outb(port->IER, port->ioaddr +
713 tty_wakeup(port->tty);
716 if (!(status & UART_MSR_CTS)) {
717 port->tty->hw_stopped = 1;
718 if (port->type != PORT_16550A &&
719 !port->board->chip_flag) {
720 port->IER &= ~UART_IER_THRI;
721 outb(port->IER, port->ioaddr +
729 static int mxser_startup(struct mxser_port *info)
734 page = __get_free_page(GFP_KERNEL);
738 spin_lock_irqsave(&info->slock, flags);
740 if (info->flags & ASYNC_INITIALIZED) {
742 spin_unlock_irqrestore(&info->slock, flags);
746 if (!info->ioaddr || !info->type) {
748 set_bit(TTY_IO_ERROR, &info->tty->flags);
750 spin_unlock_irqrestore(&info->slock, flags);
756 info->xmit_buf = (unsigned char *) page;
759 * Clear the FIFO buffers and disable them
760 * (they will be reenabled in mxser_change_speed())
762 if (info->board->chip_flag)
763 outb((UART_FCR_CLEAR_RCVR |
764 UART_FCR_CLEAR_XMIT |
765 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
767 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
768 info->ioaddr + UART_FCR);
771 * At this point there's no way the LSR could still be 0xFF;
772 * if it is, then bail out, because there's likely no UART
775 if (inb(info->ioaddr + UART_LSR) == 0xff) {
776 spin_unlock_irqrestore(&info->slock, flags);
777 if (capable(CAP_SYS_ADMIN)) {
779 set_bit(TTY_IO_ERROR, &info->tty->flags);
786 * Clear the interrupt registers.
788 (void) inb(info->ioaddr + UART_LSR);
789 (void) inb(info->ioaddr + UART_RX);
790 (void) inb(info->ioaddr + UART_IIR);
791 (void) inb(info->ioaddr + UART_MSR);
794 * Now, initialize the UART
796 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
797 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
798 outb(info->MCR, info->ioaddr + UART_MCR);
801 * Finally, enable interrupts
803 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
805 if (info->board->chip_flag)
806 info->IER |= MOXA_MUST_IER_EGDAI;
807 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
810 * And clear the interrupt registers again for luck.
812 (void) inb(info->ioaddr + UART_LSR);
813 (void) inb(info->ioaddr + UART_RX);
814 (void) inb(info->ioaddr + UART_IIR);
815 (void) inb(info->ioaddr + UART_MSR);
818 clear_bit(TTY_IO_ERROR, &info->tty->flags);
819 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
822 * and set the speed of the serial port
824 mxser_change_speed(info, NULL);
825 info->flags |= ASYNC_INITIALIZED;
826 spin_unlock_irqrestore(&info->slock, flags);
832 * This routine will shutdown a serial port; interrupts maybe disabled, and
833 * DTR is dropped if the hangup on close termio flag is on.
835 static void mxser_shutdown(struct mxser_port *info)
839 if (!(info->flags & ASYNC_INITIALIZED))
842 spin_lock_irqsave(&info->slock, flags);
845 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
846 * here so the queue might never be waken up
848 wake_up_interruptible(&info->delta_msr_wait);
851 * Free the IRQ, if necessary
853 if (info->xmit_buf) {
854 free_page((unsigned long) info->xmit_buf);
855 info->xmit_buf = NULL;
859 outb(0x00, info->ioaddr + UART_IER);
861 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
862 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
863 outb(info->MCR, info->ioaddr + UART_MCR);
865 /* clear Rx/Tx FIFO's */
866 if (info->board->chip_flag)
867 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
868 MOXA_MUST_FCR_GDA_MODE_ENABLE,
869 info->ioaddr + UART_FCR);
871 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
872 info->ioaddr + UART_FCR);
874 /* read data port to reset things */
875 (void) inb(info->ioaddr + UART_RX);
878 set_bit(TTY_IO_ERROR, &info->tty->flags);
880 info->flags &= ~ASYNC_INITIALIZED;
882 if (info->board->chip_flag)
883 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
885 spin_unlock_irqrestore(&info->slock, flags);
889 * This routine is called whenever a serial port is opened. It
890 * enables interrupts for a serial port, linking in its async structure into
891 * the IRQ chain. It also performs the serial-specific
892 * initialization for the tty structure.
894 static int mxser_open(struct tty_struct *tty, struct file *filp)
896 struct mxser_port *info;
901 if (line == MXSER_PORTS)
903 if (line < 0 || line > MXSER_PORTS)
905 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
909 tty->driver_data = info;
912 * Start up serial port
914 spin_lock_irqsave(&info->slock, flags);
916 spin_unlock_irqrestore(&info->slock, flags);
917 retval = mxser_startup(info);
921 retval = mxser_block_til_ready(tty, filp, info);
925 /* unmark here for very high baud rate (ex. 921600 bps) used */
926 tty->low_latency = 1;
930 static void mxser_flush_buffer(struct tty_struct *tty)
932 struct mxser_port *info = tty->driver_data;
937 spin_lock_irqsave(&info->slock, flags);
938 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
940 fcr = inb(info->ioaddr + UART_FCR);
941 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
942 info->ioaddr + UART_FCR);
943 outb(fcr, info->ioaddr + UART_FCR);
945 spin_unlock_irqrestore(&info->slock, flags);
952 * This routine is called when the serial port gets closed. First, we
953 * wait for the last remaining data to be sent. Then, we unlink its
954 * async structure from the interrupt chain if necessary, and we free
955 * that IRQ if nothing is left in the chain.
957 static void mxser_close(struct tty_struct *tty, struct file *filp)
959 struct mxser_port *info = tty->driver_data;
961 unsigned long timeout;
964 if (tty->index == MXSER_PORTS)
969 spin_lock_irqsave(&info->slock, flags);
971 if (tty_hung_up_p(filp)) {
972 spin_unlock_irqrestore(&info->slock, flags);
975 if ((tty->count == 1) && (info->count != 1)) {
977 * Uh, oh. tty->count is 1, which means that the tty
978 * structure will be freed. Info->count should always
979 * be one in these conditions. If it's greater than
980 * one, we've got real problems, since it means the
981 * serial port won't be shutdown.
983 printk(KERN_ERR "mxser_close: bad serial port count; "
984 "tty->count is 1, info->count is %d\n", info->count);
987 if (--info->count < 0) {
988 printk(KERN_ERR "mxser_close: bad serial port count for "
989 "ttys%d: %d\n", tty->index, info->count);
993 spin_unlock_irqrestore(&info->slock, flags);
996 info->flags |= ASYNC_CLOSING;
997 spin_unlock_irqrestore(&info->slock, flags);
999 * Save the termios structure, since this port may have
1000 * separate termios for callout and dialin.
1002 if (info->flags & ASYNC_NORMAL_ACTIVE)
1003 info->normal_termios = *tty->termios;
1005 * Now we wait for the transmit buffer to clear; and we notify
1006 * the line discipline to only process XON/XOFF characters.
1009 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1010 tty_wait_until_sent(tty, info->closing_wait);
1012 * At this point we stop accepting input. To do this, we
1013 * disable the receive line status interrupts, and tell the
1014 * interrupt driver to stop checking the data ready bit in the
1015 * line status register.
1017 info->IER &= ~UART_IER_RLSI;
1018 if (info->board->chip_flag)
1019 info->IER &= ~MOXA_MUST_RECV_ISR;
1021 if (info->flags & ASYNC_INITIALIZED) {
1022 outb(info->IER, info->ioaddr + UART_IER);
1024 * Before we drop DTR, make sure the UART transmitter
1025 * has completely drained; this is especially
1026 * important if there is a transmit FIFO!
1028 timeout = jiffies + HZ;
1029 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
1030 schedule_timeout_interruptible(5);
1031 if (time_after(jiffies, timeout))
1035 mxser_shutdown(info);
1037 mxser_flush_buffer(tty);
1038 tty_ldisc_flush(tty);
1043 if (info->blocked_open) {
1044 if (info->close_delay)
1045 schedule_timeout_interruptible(info->close_delay);
1046 wake_up_interruptible(&info->open_wait);
1049 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1052 static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1055 struct mxser_port *info = tty->driver_data;
1056 unsigned long flags;
1058 if (!info->xmit_buf)
1062 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1063 SERIAL_XMIT_SIZE - info->xmit_head));
1067 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1068 spin_lock_irqsave(&info->slock, flags);
1069 info->xmit_head = (info->xmit_head + c) &
1070 (SERIAL_XMIT_SIZE - 1);
1071 info->xmit_cnt += c;
1072 spin_unlock_irqrestore(&info->slock, flags);
1079 if (info->xmit_cnt && !tty->stopped) {
1080 if (!tty->hw_stopped ||
1081 (info->type == PORT_16550A) ||
1082 (info->board->chip_flag)) {
1083 spin_lock_irqsave(&info->slock, flags);
1084 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1086 info->IER |= UART_IER_THRI;
1087 outb(info->IER, info->ioaddr + UART_IER);
1088 spin_unlock_irqrestore(&info->slock, flags);
1094 static int mxser_put_char(struct tty_struct *tty, unsigned char ch)
1096 struct mxser_port *info = tty->driver_data;
1097 unsigned long flags;
1099 if (!info->xmit_buf)
1102 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1105 spin_lock_irqsave(&info->slock, flags);
1106 info->xmit_buf[info->xmit_head++] = ch;
1107 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1109 spin_unlock_irqrestore(&info->slock, flags);
1110 if (!tty->stopped) {
1111 if (!tty->hw_stopped ||
1112 (info->type == PORT_16550A) ||
1113 info->board->chip_flag) {
1114 spin_lock_irqsave(&info->slock, flags);
1115 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1116 info->IER |= UART_IER_THRI;
1117 outb(info->IER, info->ioaddr + UART_IER);
1118 spin_unlock_irqrestore(&info->slock, flags);
1125 static void mxser_flush_chars(struct tty_struct *tty)
1127 struct mxser_port *info = tty->driver_data;
1128 unsigned long flags;
1130 if (info->xmit_cnt <= 0 ||
1134 (info->type != PORT_16550A) &&
1135 (!info->board->chip_flag)
1139 spin_lock_irqsave(&info->slock, flags);
1141 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1142 info->IER |= UART_IER_THRI;
1143 outb(info->IER, info->ioaddr + UART_IER);
1145 spin_unlock_irqrestore(&info->slock, flags);
1148 static int mxser_write_room(struct tty_struct *tty)
1150 struct mxser_port *info = tty->driver_data;
1153 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1159 static int mxser_chars_in_buffer(struct tty_struct *tty)
1161 struct mxser_port *info = tty->driver_data;
1162 return info->xmit_cnt;
1166 * ------------------------------------------------------------
1167 * friends of mxser_ioctl()
1168 * ------------------------------------------------------------
1170 static int mxser_get_serial_info(struct mxser_port *info,
1171 struct serial_struct __user *retinfo)
1173 struct serial_struct tmp = {
1175 .line = info->tty->index,
1176 .port = info->ioaddr,
1177 .irq = info->board->irq,
1178 .flags = info->flags,
1179 .baud_base = info->baud_base,
1180 .close_delay = info->close_delay,
1181 .closing_wait = info->closing_wait,
1182 .custom_divisor = info->custom_divisor,
1185 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1190 static int mxser_set_serial_info(struct mxser_port *info,
1191 struct serial_struct __user *new_info)
1193 struct serial_struct new_serial;
1195 unsigned long sl_flags;
1199 if (!new_info || !info->ioaddr)
1201 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1204 if (new_serial.irq != info->board->irq ||
1205 new_serial.port != info->ioaddr)
1208 flags = info->flags & ASYNC_SPD_MASK;
1210 if (!capable(CAP_SYS_ADMIN)) {
1211 if ((new_serial.baud_base != info->baud_base) ||
1212 (new_serial.close_delay != info->close_delay) ||
1213 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
1215 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1216 (new_serial.flags & ASYNC_USR_MASK));
1219 * OK, past this point, all the error checking has been done.
1220 * At this point, we start making changes.....
1222 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1223 (new_serial.flags & ASYNC_FLAGS));
1224 info->close_delay = new_serial.close_delay * HZ / 100;
1225 info->closing_wait = new_serial.closing_wait * HZ / 100;
1226 info->tty->low_latency =
1227 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1228 info->tty->low_latency = 0;
1229 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
1230 (new_serial.baud_base != info->baud_base ||
1231 new_serial.custom_divisor !=
1232 info->custom_divisor)) {
1233 baud = new_serial.baud_base / new_serial.custom_divisor;
1234 tty_encode_baud_rate(info->tty, baud, baud);
1238 info->type = new_serial.type;
1240 process_txrx_fifo(info);
1242 if (info->flags & ASYNC_INITIALIZED) {
1243 if (flags != (info->flags & ASYNC_SPD_MASK)) {
1244 spin_lock_irqsave(&info->slock, sl_flags);
1245 mxser_change_speed(info, NULL);
1246 spin_unlock_irqrestore(&info->slock, sl_flags);
1249 retval = mxser_startup(info);
1255 * mxser_get_lsr_info - get line status register info
1257 * Purpose: Let user call ioctl() to get info when the UART physically
1258 * is emptied. On bus types like RS485, the transmitter must
1259 * release the bus after transmitting. This must be done when
1260 * the transmit shift register is empty, not be done when the
1261 * transmit holding register is empty. This functionality
1262 * allows an RS485 driver to be written in user space.
1264 static int mxser_get_lsr_info(struct mxser_port *info,
1265 unsigned int __user *value)
1267 unsigned char status;
1268 unsigned int result;
1269 unsigned long flags;
1271 spin_lock_irqsave(&info->slock, flags);
1272 status = inb(info->ioaddr + UART_LSR);
1273 spin_unlock_irqrestore(&info->slock, flags);
1274 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1275 return put_user(result, value);
1279 * This routine sends a break character out the serial port.
1281 static void mxser_send_break(struct mxser_port *info, int duration)
1283 unsigned long flags;
1287 set_current_state(TASK_INTERRUPTIBLE);
1288 spin_lock_irqsave(&info->slock, flags);
1289 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1290 info->ioaddr + UART_LCR);
1291 spin_unlock_irqrestore(&info->slock, flags);
1292 schedule_timeout(duration);
1293 spin_lock_irqsave(&info->slock, flags);
1294 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1295 info->ioaddr + UART_LCR);
1296 spin_unlock_irqrestore(&info->slock, flags);
1299 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
1301 struct mxser_port *info = tty->driver_data;
1302 unsigned char control, status;
1303 unsigned long flags;
1306 if (tty->index == MXSER_PORTS)
1307 return -ENOIOCTLCMD;
1308 if (test_bit(TTY_IO_ERROR, &tty->flags))
1311 control = info->MCR;
1313 spin_lock_irqsave(&info->slock, flags);
1314 status = inb(info->ioaddr + UART_MSR);
1315 if (status & UART_MSR_ANY_DELTA)
1316 mxser_check_modem_status(info, status);
1317 spin_unlock_irqrestore(&info->slock, flags);
1318 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1319 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1320 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1321 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1322 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1323 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1326 static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1327 unsigned int set, unsigned int clear)
1329 struct mxser_port *info = tty->driver_data;
1330 unsigned long flags;
1333 if (tty->index == MXSER_PORTS)
1334 return -ENOIOCTLCMD;
1335 if (test_bit(TTY_IO_ERROR, &tty->flags))
1338 spin_lock_irqsave(&info->slock, flags);
1340 if (set & TIOCM_RTS)
1341 info->MCR |= UART_MCR_RTS;
1342 if (set & TIOCM_DTR)
1343 info->MCR |= UART_MCR_DTR;
1345 if (clear & TIOCM_RTS)
1346 info->MCR &= ~UART_MCR_RTS;
1347 if (clear & TIOCM_DTR)
1348 info->MCR &= ~UART_MCR_DTR;
1350 outb(info->MCR, info->ioaddr + UART_MCR);
1351 spin_unlock_irqrestore(&info->slock, flags);
1355 static int __init mxser_program_mode(int port)
1367 id = inb(port + 1) & 0x1F;
1368 if ((id != C168_ASIC_ID) &&
1369 (id != C104_ASIC_ID) &&
1370 (id != C102_ASIC_ID) &&
1371 (id != CI132_ASIC_ID) &&
1372 (id != CI134_ASIC_ID) &&
1373 (id != CI104J_ASIC_ID))
1375 for (i = 0, j = 0; i < 4; i++) {
1379 } else if ((j == 1) && (n == 1)) {
1390 static void __init mxser_normal_mode(int port)
1394 outb(0xA5, port + 1);
1395 outb(0x80, port + 3);
1396 outb(12, port + 0); /* 9600 bps */
1398 outb(0x03, port + 3); /* 8 data bits */
1399 outb(0x13, port + 4); /* loop back mode */
1400 for (i = 0; i < 16; i++) {
1402 if ((n & 0x61) == 0x60)
1407 outb(0x00, port + 4);
1410 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1411 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1412 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1413 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1414 #define EN_CCMD 0x000 /* Chip's command register */
1415 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1416 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1417 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1418 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1419 #define EN0_DCFG 0x00E /* Data configuration reg WR */
1420 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1421 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1422 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1423 static int __init mxser_read_register(int port, unsigned short *regs)
1425 int i, k, value, id;
1428 id = mxser_program_mode(port);
1431 for (i = 0; i < 14; i++) {
1432 k = (i & 0x3F) | 0x180;
1433 for (j = 0x100; j > 0; j >>= 1) {
1434 outb(CHIP_CS, port);
1436 outb(CHIP_CS | CHIP_DO, port);
1437 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1439 outb(CHIP_CS, port);
1440 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1445 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1446 outb(CHIP_CS, port);
1447 outb(CHIP_CS | CHIP_SK, port);
1448 if (inb(port) & CHIP_DI)
1454 mxser_normal_mode(port);
1458 static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1460 struct mxser_port *port;
1466 case MOXA_GET_MAJOR:
1467 return put_user(ttymajor, (int __user *)argp);
1469 case MOXA_CHKPORTENABLE:
1472 for (i = 0; i < MXSER_BOARDS; i++)
1473 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1474 if (mxser_boards[i].ports[j].ioaddr)
1477 return put_user(result, (unsigned long __user *)argp);
1478 case MOXA_GETDATACOUNT:
1480 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1484 case MOXA_GETMSTATUS:
1486 for (i = 0; i < MXSER_BOARDS; i++)
1487 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1488 port = &mxser_boards[i].ports[j];
1491 if (!port->ioaddr) {
1492 GMStatus[i].dcd = 0;
1493 GMStatus[i].dsr = 0;
1494 GMStatus[i].cts = 0;
1498 if (!port->tty || !port->tty->termios)
1500 port->normal_termios.c_cflag;
1503 port->tty->termios->c_cflag;
1505 status = inb(port->ioaddr + UART_MSR);
1506 if (status & 0x80 /*UART_MSR_DCD */ )
1507 GMStatus[i].dcd = 1;
1509 GMStatus[i].dcd = 0;
1511 if (status & 0x20 /*UART_MSR_DSR */ )
1512 GMStatus[i].dsr = 1;
1514 GMStatus[i].dsr = 0;
1517 if (status & 0x10 /*UART_MSR_CTS */ )
1518 GMStatus[i].cts = 1;
1520 GMStatus[i].cts = 0;
1523 if (copy_to_user(argp, GMStatus,
1524 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1527 case MOXA_ASPP_MON_EXT: {
1529 unsigned long opmode;
1530 unsigned cflag, iflag;
1533 for (i = 0; i < MXSER_BOARDS; i++) {
1534 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1535 port = &mxser_boards[i].ports[j];
1539 status = mxser_get_msr(port->ioaddr, 0, i);
1541 if (status & UART_MSR_TERI)
1543 if (status & UART_MSR_DDSR)
1545 if (status & UART_MSR_DDCD)
1547 if (status & UART_MSR_DCTS)
1550 port->mon_data.modem_status = status;
1551 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1552 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1553 mon_data_ext.up_rxcnt[i] =
1554 port->mon_data.up_rxcnt;
1555 mon_data_ext.up_txcnt[i] =
1556 port->mon_data.up_txcnt;
1557 mon_data_ext.modem_status[i] =
1558 port->mon_data.modem_status;
1559 mon_data_ext.baudrate[i] =
1560 tty_get_baud_rate(port->tty);
1562 if (!port->tty || !port->tty->termios) {
1563 cflag = port->normal_termios.c_cflag;
1564 iflag = port->normal_termios.c_iflag;
1566 cflag = port->tty->termios->c_cflag;
1567 iflag = port->tty->termios->c_iflag;
1570 mon_data_ext.databits[i] = cflag & CSIZE;
1572 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1574 mon_data_ext.parity[i] =
1575 cflag & (PARENB | PARODD | CMSPAR);
1577 mon_data_ext.flowctrl[i] = 0x00;
1579 if (cflag & CRTSCTS)
1580 mon_data_ext.flowctrl[i] |= 0x03;
1582 if (iflag & (IXON | IXOFF))
1583 mon_data_ext.flowctrl[i] |= 0x0C;
1585 if (port->type == PORT_16550A)
1586 mon_data_ext.fifo[i] = 1;
1588 mon_data_ext.fifo[i] = 0;
1592 opmode = inb(port->opmode_ioaddr) >> shiftbit;
1593 opmode &= OP_MODE_MASK;
1595 mon_data_ext.iftype[i] = opmode;
1600 if (copy_to_user(argp, &mon_data_ext,
1601 sizeof(mon_data_ext)))
1606 return -ENOIOCTLCMD;
1611 static int mxser_cflags_changed(struct mxser_port *info, unsigned long arg,
1612 struct async_icount *cprev)
1614 struct async_icount cnow;
1615 unsigned long flags;
1618 spin_lock_irqsave(&info->slock, flags);
1619 cnow = info->icount; /* atomic copy */
1620 spin_unlock_irqrestore(&info->slock, flags);
1622 ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1623 ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1624 ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1625 ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
1632 static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1633 unsigned int cmd, unsigned long arg)
1635 struct mxser_port *info = tty->driver_data;
1636 struct async_icount cnow;
1637 struct serial_icounter_struct __user *p_cuser;
1638 unsigned long flags;
1639 void __user *argp = (void __user *)arg;
1642 if (tty->index == MXSER_PORTS)
1643 return mxser_ioctl_special(cmd, argp);
1645 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1647 unsigned long opmode;
1648 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1650 unsigned char val, mask;
1653 if (cmd == MOXA_SET_OP_MODE) {
1654 if (get_user(opmode, (int __user *) argp))
1656 if (opmode != RS232_MODE &&
1657 opmode != RS485_2WIRE_MODE &&
1658 opmode != RS422_MODE &&
1659 opmode != RS485_4WIRE_MODE)
1664 val = inb(info->opmode_ioaddr);
1666 val |= (opmode << shiftbit);
1667 outb(val, info->opmode_ioaddr);
1672 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1673 opmode &= OP_MODE_MASK;
1675 if (put_user(opmode, (int __user *)argp))
1681 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1682 test_bit(TTY_IO_ERROR, &tty->flags))
1686 case TCSBRK: /* SVID version: non-zero arg --> no break */
1687 retval = tty_check_change(tty);
1690 tty_wait_until_sent(tty, 0);
1692 mxser_send_break(info, HZ / 4); /* 1/4 second */
1694 case TCSBRKP: /* support for POSIX tcsendbreak() */
1695 retval = tty_check_change(tty);
1698 tty_wait_until_sent(tty, 0);
1699 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1703 retval = mxser_get_serial_info(info, argp);
1708 retval = mxser_set_serial_info(info, argp);
1711 case TIOCSERGETLSR: /* Get line status register */
1712 return mxser_get_lsr_info(info, argp);
1714 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1715 * - mask passed in arg for lines of interest
1716 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1717 * Caller should use TIOCGICOUNT to see which one it was
1720 spin_lock_irqsave(&info->slock, flags);
1721 cnow = info->icount; /* note the counters on entry */
1722 spin_unlock_irqrestore(&info->slock, flags);
1724 return wait_event_interruptible(info->delta_msr_wait,
1725 mxser_cflags_changed(info, arg, &cnow));
1727 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1728 * Return: write counters to the user passed counter struct
1729 * NB: both 1->0 and 0->1 transitions are counted except for
1730 * RI where only 0->1 is counted.
1733 spin_lock_irqsave(&info->slock, flags);
1734 cnow = info->icount;
1735 spin_unlock_irqrestore(&info->slock, flags);
1737 if (put_user(cnow.frame, &p_cuser->frame))
1739 if (put_user(cnow.brk, &p_cuser->brk))
1741 if (put_user(cnow.overrun, &p_cuser->overrun))
1743 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1745 if (put_user(cnow.parity, &p_cuser->parity))
1747 if (put_user(cnow.rx, &p_cuser->rx))
1749 if (put_user(cnow.tx, &p_cuser->tx))
1751 put_user(cnow.cts, &p_cuser->cts);
1752 put_user(cnow.dsr, &p_cuser->dsr);
1753 put_user(cnow.rng, &p_cuser->rng);
1754 put_user(cnow.dcd, &p_cuser->dcd);
1756 case MOXA_HighSpeedOn:
1757 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1758 case MOXA_SDS_RSTICOUNTER:
1760 info->mon_data.rxcnt = 0;
1761 info->mon_data.txcnt = 0;
1765 case MOXA_ASPP_OQUEUE:{
1769 len = mxser_chars_in_buffer(tty);
1770 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1771 len += (lsr ? 0 : 1);
1774 return put_user(len, (int __user *)argp);
1776 case MOXA_ASPP_MON: {
1780 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1781 mxser_check_modem_status(info, status);
1783 mcr = inb(info->ioaddr + UART_MCR);
1784 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1785 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1787 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1789 if (mcr & MOXA_MUST_MCR_TX_XON)
1790 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1792 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1794 if (info->tty->hw_stopped)
1795 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1797 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1799 if (copy_to_user(argp, &info->mon_data,
1800 sizeof(struct mxser_mon)))
1805 case MOXA_ASPP_LSTATUS: {
1806 if (put_user(info->err_shadow, (unsigned char __user *)argp))
1809 info->err_shadow = 0;
1812 case MOXA_SET_BAUD_METHOD: {
1815 if (get_user(method, (int __user *)argp))
1817 mxser_set_baud_method[tty->index] = method;
1818 return put_user(method, (int __user *)argp);
1821 return -ENOIOCTLCMD;
1826 static void mxser_stoprx(struct tty_struct *tty)
1828 struct mxser_port *info = tty->driver_data;
1830 info->ldisc_stop_rx = 1;
1832 if (info->board->chip_flag) {
1833 info->IER &= ~MOXA_MUST_RECV_ISR;
1834 outb(info->IER, info->ioaddr + UART_IER);
1836 info->x_char = STOP_CHAR(tty);
1837 outb(0, info->ioaddr + UART_IER);
1838 info->IER |= UART_IER_THRI;
1839 outb(info->IER, info->ioaddr + UART_IER);
1843 if (info->tty->termios->c_cflag & CRTSCTS) {
1844 info->MCR &= ~UART_MCR_RTS;
1845 outb(info->MCR, info->ioaddr + UART_MCR);
1850 * This routine is called by the upper-layer tty layer to signal that
1851 * incoming characters should be throttled.
1853 static void mxser_throttle(struct tty_struct *tty)
1858 static void mxser_unthrottle(struct tty_struct *tty)
1860 struct mxser_port *info = tty->driver_data;
1863 info->ldisc_stop_rx = 0;
1868 if (info->board->chip_flag) {
1869 info->IER |= MOXA_MUST_RECV_ISR;
1870 outb(info->IER, info->ioaddr + UART_IER);
1872 info->x_char = START_CHAR(tty);
1873 outb(0, info->ioaddr + UART_IER);
1874 info->IER |= UART_IER_THRI;
1875 outb(info->IER, info->ioaddr + UART_IER);
1880 if (info->tty->termios->c_cflag & CRTSCTS) {
1881 info->MCR |= UART_MCR_RTS;
1882 outb(info->MCR, info->ioaddr + UART_MCR);
1887 * mxser_stop() and mxser_start()
1889 * This routines are called before setting or resetting tty->stopped.
1890 * They enable or disable transmitter interrupts, as necessary.
1892 static void mxser_stop(struct tty_struct *tty)
1894 struct mxser_port *info = tty->driver_data;
1895 unsigned long flags;
1897 spin_lock_irqsave(&info->slock, flags);
1898 if (info->IER & UART_IER_THRI) {
1899 info->IER &= ~UART_IER_THRI;
1900 outb(info->IER, info->ioaddr + UART_IER);
1902 spin_unlock_irqrestore(&info->slock, flags);
1905 static void mxser_start(struct tty_struct *tty)
1907 struct mxser_port *info = tty->driver_data;
1908 unsigned long flags;
1910 spin_lock_irqsave(&info->slock, flags);
1911 if (info->xmit_cnt && info->xmit_buf) {
1912 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1913 info->IER |= UART_IER_THRI;
1914 outb(info->IER, info->ioaddr + UART_IER);
1916 spin_unlock_irqrestore(&info->slock, flags);
1919 static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1921 struct mxser_port *info = tty->driver_data;
1922 unsigned long flags;
1924 spin_lock_irqsave(&info->slock, flags);
1925 mxser_change_speed(info, old_termios);
1926 spin_unlock_irqrestore(&info->slock, flags);
1928 if ((old_termios->c_cflag & CRTSCTS) &&
1929 !(tty->termios->c_cflag & CRTSCTS)) {
1930 tty->hw_stopped = 0;
1934 /* Handle sw stopped */
1935 if ((old_termios->c_iflag & IXON) &&
1936 !(tty->termios->c_iflag & IXON)) {
1939 if (info->board->chip_flag) {
1940 spin_lock_irqsave(&info->slock, flags);
1941 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
1942 spin_unlock_irqrestore(&info->slock, flags);
1950 * mxser_wait_until_sent() --- wait until the transmitter is empty
1952 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1954 struct mxser_port *info = tty->driver_data;
1955 unsigned long orig_jiffies, char_time;
1958 if (info->type == PORT_UNKNOWN)
1961 if (info->xmit_fifo_size == 0)
1962 return; /* Just in case.... */
1964 orig_jiffies = jiffies;
1966 * Set the check interval to be 1/5 of the estimated time to
1967 * send a single character, and make it at least 1. The check
1968 * interval should also be less than the timeout.
1970 * Note: we have to use pretty tight timings here to satisfy
1973 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1974 char_time = char_time / 5;
1977 if (timeout && timeout < char_time)
1978 char_time = timeout;
1980 * If the transmitter hasn't cleared in twice the approximate
1981 * amount of time to send the entire FIFO, it probably won't
1982 * ever clear. This assumes the UART isn't doing flow
1983 * control, which is currently the case. Hence, if it ever
1984 * takes longer than info->timeout, this is probably due to a
1985 * UART bug of some kind. So, we clamp the timeout parameter at
1988 if (!timeout || timeout > 2 * info->timeout)
1989 timeout = 2 * info->timeout;
1990 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1991 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
1992 timeout, char_time);
1993 printk("jiff=%lu...", jiffies);
1996 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
1997 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1998 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2000 schedule_timeout_interruptible(char_time);
2001 if (signal_pending(current))
2003 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2006 set_current_state(TASK_RUNNING);
2009 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2010 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2015 * This routine is called by tty_hangup() when a hangup is signaled.
2017 static void mxser_hangup(struct tty_struct *tty)
2019 struct mxser_port *info = tty->driver_data;
2021 mxser_flush_buffer(tty);
2022 mxser_shutdown(info);
2025 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2027 wake_up_interruptible(&info->open_wait);
2031 * mxser_rs_break() --- routine which turns the break handling on or off
2033 static void mxser_rs_break(struct tty_struct *tty, int break_state)
2035 struct mxser_port *info = tty->driver_data;
2036 unsigned long flags;
2038 spin_lock_irqsave(&info->slock, flags);
2039 if (break_state == -1)
2040 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2041 info->ioaddr + UART_LCR);
2043 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2044 info->ioaddr + UART_LCR);
2045 spin_unlock_irqrestore(&info->slock, flags);
2048 static void mxser_receive_chars(struct mxser_port *port, int *status)
2050 struct tty_struct *tty = port->tty;
2051 unsigned char ch, gdl;
2057 recv_room = tty->receive_room;
2058 if ((recv_room == 0) && (!port->ldisc_stop_rx))
2061 if (port->board->chip_flag != MOXA_OTHER_UART) {
2063 if (*status & UART_LSR_SPECIAL)
2065 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
2066 (*status & MOXA_MUST_LSR_RERR))
2068 if (*status & MOXA_MUST_LSR_RERR)
2071 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
2073 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
2074 gdl &= MOXA_MUST_GDL_MASK;
2075 if (gdl >= recv_room) {
2076 if (!port->ldisc_stop_rx)
2080 ch = inb(port->ioaddr + UART_RX);
2081 tty_insert_flip_char(tty, ch, 0);
2092 ch = inb(port->ioaddr + UART_RX);
2093 if (port->board->chip_flag && (*status & UART_LSR_OE))
2094 outb(0x23, port->ioaddr + UART_FCR);
2095 *status &= port->read_status_mask;
2096 if (*status & port->ignore_status_mask) {
2097 if (++ignored > 100)
2101 if (*status & UART_LSR_SPECIAL) {
2102 if (*status & UART_LSR_BI) {
2106 if (port->flags & ASYNC_SAK)
2108 } else if (*status & UART_LSR_PE) {
2110 port->icount.parity++;
2111 } else if (*status & UART_LSR_FE) {
2113 port->icount.frame++;
2114 } else if (*status & UART_LSR_OE) {
2116 port->icount.overrun++;
2120 tty_insert_flip_char(tty, ch, flag);
2122 if (cnt >= recv_room) {
2123 if (!port->ldisc_stop_rx)
2130 if (port->board->chip_flag)
2133 *status = inb(port->ioaddr + UART_LSR);
2134 } while (*status & UART_LSR_DR);
2137 mxvar_log.rxcnt[port->tty->index] += cnt;
2138 port->mon_data.rxcnt += cnt;
2139 port->mon_data.up_rxcnt += cnt;
2142 * We are called from an interrupt context with &port->slock
2143 * being held. Drop it temporarily in order to prevent
2144 * recursive locking.
2146 spin_unlock(&port->slock);
2147 tty_flip_buffer_push(tty);
2148 spin_lock(&port->slock);
2151 static void mxser_transmit_chars(struct mxser_port *port)
2156 outb(port->x_char, port->ioaddr + UART_TX);
2158 mxvar_log.txcnt[port->tty->index]++;
2159 port->mon_data.txcnt++;
2160 port->mon_data.up_txcnt++;
2165 if (port->xmit_buf == NULL)
2168 if ((port->xmit_cnt <= 0) || port->tty->stopped ||
2169 (port->tty->hw_stopped &&
2170 (port->type != PORT_16550A) &&
2171 (!port->board->chip_flag))) {
2172 port->IER &= ~UART_IER_THRI;
2173 outb(port->IER, port->ioaddr + UART_IER);
2177 cnt = port->xmit_cnt;
2178 count = port->xmit_fifo_size;
2180 outb(port->xmit_buf[port->xmit_tail++],
2181 port->ioaddr + UART_TX);
2182 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2183 if (--port->xmit_cnt <= 0)
2185 } while (--count > 0);
2186 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
2188 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2189 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2190 port->icount.tx += (cnt - port->xmit_cnt);
2192 if (port->xmit_cnt < WAKEUP_CHARS)
2193 tty_wakeup(port->tty);
2195 if (port->xmit_cnt <= 0) {
2196 port->IER &= ~UART_IER_THRI;
2197 outb(port->IER, port->ioaddr + UART_IER);
2202 * This is the serial driver's generic interrupt routine
2204 static irqreturn_t mxser_interrupt(int irq, void *dev_id)
2207 struct mxser_board *brd = NULL;
2208 struct mxser_port *port;
2209 int max, irqbits, bits, msr;
2210 unsigned int int_cnt, pass_counter = 0;
2211 int handled = IRQ_NONE;
2213 for (i = 0; i < MXSER_BOARDS; i++)
2214 if (dev_id == &mxser_boards[i]) {
2219 if (i == MXSER_BOARDS)
2223 max = brd->info->nports;
2224 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
2225 irqbits = inb(brd->vector) & brd->vector_mask;
2226 if (irqbits == brd->vector_mask)
2229 handled = IRQ_HANDLED;
2230 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2231 if (irqbits == brd->vector_mask)
2235 port = &brd->ports[i];
2238 spin_lock(&port->slock);
2240 iir = inb(port->ioaddr + UART_IIR);
2241 if (iir & UART_IIR_NO_INT)
2243 iir &= MOXA_MUST_IIR_MASK;
2245 (port->flags & ASYNC_CLOSING) ||
2247 ASYNC_INITIALIZED)) {
2248 status = inb(port->ioaddr + UART_LSR);
2249 outb(0x27, port->ioaddr + UART_FCR);
2250 inb(port->ioaddr + UART_MSR);
2254 status = inb(port->ioaddr + UART_LSR);
2256 if (status & UART_LSR_PE)
2257 port->err_shadow |= NPPI_NOTIFY_PARITY;
2258 if (status & UART_LSR_FE)
2259 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2260 if (status & UART_LSR_OE)
2262 NPPI_NOTIFY_HW_OVERRUN;
2263 if (status & UART_LSR_BI)
2264 port->err_shadow |= NPPI_NOTIFY_BREAK;
2266 if (port->board->chip_flag) {
2267 if (iir == MOXA_MUST_IIR_GDA ||
2268 iir == MOXA_MUST_IIR_RDA ||
2269 iir == MOXA_MUST_IIR_RTO ||
2270 iir == MOXA_MUST_IIR_LSR)
2271 mxser_receive_chars(port,
2275 status &= port->read_status_mask;
2276 if (status & UART_LSR_DR)
2277 mxser_receive_chars(port,
2280 msr = inb(port->ioaddr + UART_MSR);
2281 if (msr & UART_MSR_ANY_DELTA)
2282 mxser_check_modem_status(port, msr);
2284 if (port->board->chip_flag) {
2285 if (iir == 0x02 && (status &
2287 mxser_transmit_chars(port);
2289 if (status & UART_LSR_THRE)
2290 mxser_transmit_chars(port);
2292 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2293 spin_unlock(&port->slock);
2301 static const struct tty_operations mxser_ops = {
2303 .close = mxser_close,
2304 .write = mxser_write,
2305 .put_char = mxser_put_char,
2306 .flush_chars = mxser_flush_chars,
2307 .write_room = mxser_write_room,
2308 .chars_in_buffer = mxser_chars_in_buffer,
2309 .flush_buffer = mxser_flush_buffer,
2310 .ioctl = mxser_ioctl,
2311 .throttle = mxser_throttle,
2312 .unthrottle = mxser_unthrottle,
2313 .set_termios = mxser_set_termios,
2315 .start = mxser_start,
2316 .hangup = mxser_hangup,
2317 .break_ctl = mxser_rs_break,
2318 .wait_until_sent = mxser_wait_until_sent,
2319 .tiocmget = mxser_tiocmget,
2320 .tiocmset = mxser_tiocmset,
2324 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2327 static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
2331 free_irq(brd->irq, brd);
2332 if (pdev != NULL) { /* PCI */
2334 pci_release_region(pdev, 2);
2335 pci_release_region(pdev, 3);
2338 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2339 release_region(brd->vector, 1);
2343 static int __devinit mxser_initbrd(struct mxser_board *brd,
2344 struct pci_dev *pdev)
2346 struct mxser_port *info;
2350 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
2352 for (i = 0; i < brd->info->nports; i++) {
2353 info = &brd->ports[i];
2356 info->ldisc_stop_rx = 0;
2358 /* Enhance mode enabled here */
2359 if (brd->chip_flag != MOXA_OTHER_UART)
2360 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
2362 info->flags = ASYNC_SHARE_IRQ;
2363 info->type = brd->uart_type;
2365 process_txrx_fifo(info);
2367 info->custom_divisor = info->baud_base * 16;
2368 info->close_delay = 5 * HZ / 10;
2369 info->closing_wait = 30 * HZ;
2370 info->normal_termios = mxvar_sdriver->init_termios;
2371 init_waitqueue_head(&info->open_wait);
2372 init_waitqueue_head(&info->delta_msr_wait);
2373 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2374 info->err_shadow = 0;
2375 spin_lock_init(&info->slock);
2377 /* before set INT ISR, disable all int */
2378 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2379 info->ioaddr + UART_IER);
2382 retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
2385 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2386 "conflict with another device.\n",
2387 brd->info->name, brd->irq);
2388 /* We hold resources, we need to release them. */
2389 mxser_release_res(brd, pdev, 0);
2394 static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
2397 unsigned short regs[16], irq;
2398 unsigned char scratch, scratch2;
2400 brd->chip_flag = MOXA_OTHER_UART;
2402 id = mxser_read_register(cap, regs);
2405 brd->info = &mxser_cards[0];
2408 brd->info = &mxser_cards[1];
2410 case CI104J_ASIC_ID:
2411 brd->info = &mxser_cards[2];
2414 brd->info = &mxser_cards[5];
2417 brd->info = &mxser_cards[6];
2420 brd->info = &mxser_cards[7];
2427 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2428 Flag-hack checks if configuration should be read as 2-port here. */
2429 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
2430 irq = regs[9] & 0xF000;
2431 irq = irq | (irq >> 4);
2432 if (irq != (regs[9] & 0xFF00))
2433 return MXSER_ERR_IRQ_CONFLIT;
2434 } else if (brd->info->nports == 4) {
2435 irq = regs[9] & 0xF000;
2436 irq = irq | (irq >> 4);
2437 irq = irq | (irq >> 8);
2439 return MXSER_ERR_IRQ_CONFLIT;
2440 } else if (brd->info->nports == 8) {
2441 irq = regs[9] & 0xF000;
2442 irq = irq | (irq >> 4);
2443 irq = irq | (irq >> 8);
2444 if ((irq != regs[9]) || (irq != regs[10]))
2445 return MXSER_ERR_IRQ_CONFLIT;
2449 return MXSER_ERR_IRQ;
2450 brd->irq = ((int)(irq & 0xF000) >> 12);
2451 for (i = 0; i < 8; i++)
2452 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
2453 if ((regs[12] & 0x80) == 0)
2454 return MXSER_ERR_VECTOR;
2455 brd->vector = (int)regs[11]; /* interrupt vector */
2457 brd->vector_mask = 0x00FF;
2459 brd->vector_mask = 0x000F;
2460 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2461 if (regs[12] & bits) {
2462 brd->ports[i].baud_base = 921600;
2463 brd->ports[i].max_baud = 921600;
2465 brd->ports[i].baud_base = 115200;
2466 brd->ports[i].max_baud = 115200;
2469 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2470 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2471 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2472 outb(scratch2, cap + UART_LCR);
2473 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2474 scratch = inb(cap + UART_IIR);
2477 brd->uart_type = PORT_16550A;
2479 brd->uart_type = PORT_16450;
2480 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2482 return MXSER_ERR_IOADDR;
2483 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2484 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2485 return MXSER_ERR_VECTOR;
2487 return brd->info->nports;
2490 static int __devinit mxser_probe(struct pci_dev *pdev,
2491 const struct pci_device_id *ent)
2494 struct mxser_board *brd;
2496 unsigned long ioaddress;
2497 int retval = -EINVAL;
2499 for (i = 0; i < MXSER_BOARDS; i++)
2500 if (mxser_boards[i].info == NULL)
2503 if (i >= MXSER_BOARDS) {
2504 printk(KERN_ERR "Too many Smartio/Industio family boards found "
2505 "(maximum %d), board not configured\n", MXSER_BOARDS);
2509 brd = &mxser_boards[i];
2510 brd->idx = i * MXSER_PORTS_PER_BOARD;
2511 printk(KERN_INFO "Found MOXA %s board (BusNo=%d, DevNo=%d)\n",
2512 mxser_cards[ent->driver_data].name,
2513 pdev->bus->number, PCI_SLOT(pdev->devfn));
2515 retval = pci_enable_device(pdev);
2517 printk(KERN_ERR "Moxa SmartI/O PCI enable fail !\n");
2522 ioaddress = pci_resource_start(pdev, 2);
2523 retval = pci_request_region(pdev, 2, "mxser(IO)");
2527 brd->info = &mxser_cards[ent->driver_data];
2528 for (i = 0; i < brd->info->nports; i++)
2529 brd->ports[i].ioaddr = ioaddress + 8 * i;
2532 ioaddress = pci_resource_start(pdev, 3);
2533 retval = pci_request_region(pdev, 3, "mxser(vector)");
2536 brd->vector = ioaddress;
2539 brd->irq = pdev->irq;
2541 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2542 brd->uart_type = PORT_16550A;
2543 brd->vector_mask = 0;
2545 for (i = 0; i < brd->info->nports; i++) {
2546 for (j = 0; j < UART_INFO_NUM; j++) {
2547 if (Gpci_uart_info[j].type == brd->chip_flag) {
2548 brd->ports[i].max_baud =
2549 Gpci_uart_info[j].max_baud;
2551 /* exception....CP-102 */
2552 if (brd->info->flags & MXSER_HIGHBAUD)
2553 brd->ports[i].max_baud = 921600;
2559 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2560 for (i = 0; i < brd->info->nports; i++) {
2562 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2564 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
2566 outb(0, ioaddress + 4); /* default set to RS232 mode */
2567 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
2570 for (i = 0; i < brd->info->nports; i++) {
2571 brd->vector_mask |= (1 << i);
2572 brd->ports[i].baud_base = 921600;
2575 /* mxser_initbrd will hook ISR. */
2576 retval = mxser_initbrd(brd, pdev);
2580 for (i = 0; i < brd->info->nports; i++)
2581 tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
2583 pci_set_drvdata(pdev, brd);
2587 pci_release_region(pdev, 2);
2597 static void __devexit mxser_remove(struct pci_dev *pdev)
2599 struct mxser_board *brd = pci_get_drvdata(pdev);
2602 for (i = 0; i < brd->info->nports; i++)
2603 tty_unregister_device(mxvar_sdriver, brd->idx + i);
2605 mxser_release_res(brd, pdev, 1);
2609 static struct pci_driver mxser_driver = {
2611 .id_table = mxser_pcibrds,
2612 .probe = mxser_probe,
2613 .remove = __devexit_p(mxser_remove)
2616 static int __init mxser_module_init(void)
2618 struct mxser_board *brd;
2620 unsigned int i, m, isaloop;
2623 pr_debug("Loading module mxser ...\n");
2625 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2629 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2632 /* Initialize the tty_driver structure */
2633 mxvar_sdriver->owner = THIS_MODULE;
2634 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2635 mxvar_sdriver->name = "ttyMI";
2636 mxvar_sdriver->major = ttymajor;
2637 mxvar_sdriver->minor_start = 0;
2638 mxvar_sdriver->num = MXSER_PORTS + 1;
2639 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2640 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2641 mxvar_sdriver->init_termios = tty_std_termios;
2642 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2643 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2644 tty_set_operations(mxvar_sdriver, &mxser_ops);
2646 retval = tty_register_driver(mxvar_sdriver);
2648 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2656 /* Start finding ISA boards here */
2657 for (isaloop = 0; isaloop < 2; isaloop++)
2658 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2660 cap = mxserBoardCAP[b]; /* predefined */
2662 cap = ioaddr[b]; /* module param */
2667 brd = &mxser_boards[m];
2668 retval = mxser_get_ISA_conf(cap, brd);
2671 printk(KERN_INFO "Found MOXA %s board "
2673 brd->info->name, ioaddr[b]);
2676 if (retval == MXSER_ERR_IRQ)
2677 printk(KERN_ERR "Invalid interrupt "
2678 "number, board not "
2680 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2681 printk(KERN_ERR "Invalid interrupt "
2682 "number, board not "
2684 else if (retval == MXSER_ERR_VECTOR)
2685 printk(KERN_ERR "Invalid interrupt "
2686 "vector, board not "
2688 else if (retval == MXSER_ERR_IOADDR)
2689 printk(KERN_ERR "Invalid I/O address, "
2690 "board not configured\n");
2696 /* mxser_initbrd will hook ISR. */
2697 if (mxser_initbrd(brd, NULL) < 0) {
2702 brd->idx = m * MXSER_PORTS_PER_BOARD;
2703 for (i = 0; i < brd->info->nports; i++)
2704 tty_register_device(mxvar_sdriver, brd->idx + i,
2710 retval = pci_register_driver(&mxser_driver);
2712 printk(KERN_ERR "Can't register pci driver\n");
2716 } /* else: we have some ISA cards under control */
2719 pr_debug("Done.\n");
2723 tty_unregister_driver(mxvar_sdriver);
2725 put_tty_driver(mxvar_sdriver);
2729 static void __exit mxser_module_exit(void)
2733 pr_debug("Unloading module mxser ...\n");
2735 pci_unregister_driver(&mxser_driver);
2737 for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
2738 if (mxser_boards[i].info != NULL)
2739 for (j = 0; j < mxser_boards[i].info->nports; j++)
2740 tty_unregister_device(mxvar_sdriver,
2741 mxser_boards[i].idx + j);
2742 tty_unregister_driver(mxvar_sdriver);
2743 put_tty_driver(mxvar_sdriver);
2745 for (i = 0; i < MXSER_BOARDS; i++)
2746 if (mxser_boards[i].info != NULL)
2747 mxser_release_res(&mxser_boards[i], NULL, 1);
2749 pr_debug("Done.\n");
2752 module_init(mxser_module_init);
2753 module_exit(mxser_module_exit);