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 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/autoconf.h>
24 #include <linux/errno.h>
25 #include <linux/signal.h>
26 #include <linux/sched.h>
27 #include <linux/timer.h>
28 #include <linux/interrupt.h>
29 #include <linux/tty.h>
30 #include <linux/tty_flip.h>
31 #include <linux/serial.h>
32 #include <linux/serial_reg.h>
33 #include <linux/major.h>
34 #include <linux/string.h>
35 #include <linux/fcntl.h>
36 #include <linux/ptrace.h>
37 #include <linux/gfp.h>
38 #include <linux/ioport.h>
40 #include <linux/smp_lock.h>
41 #include <linux/delay.h>
42 #include <linux/pci.h>
44 #include <asm/system.h>
47 #include <asm/bitops.h>
48 #include <asm/uaccess.h>
50 #include "mxser_new.h"
52 #define MXSER_VERSION "2.0"
53 #define MXSERMAJOR 174
54 #define MXSERCUMAJOR 175
56 #define MXSER_BOARDS 4 /* Max. boards */
57 #define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
58 #define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
59 #define MXSER_ISR_PASS_LIMIT 99999L
61 #define MXSER_ERR_IOADDR -1
62 #define MXSER_ERR_IRQ -2
63 #define MXSER_ERR_IRQ_CONFLIT -3
64 #define MXSER_ERR_VECTOR -4
66 /*CheckIsMoxaMust return value*/
67 #define MOXA_OTHER_UART 0x00
68 #define MOXA_MUST_MU150_HWID 0x01
69 #define MOXA_MUST_MU860_HWID 0x02
71 #define WAKEUP_CHARS 256
73 #define UART_MCR_AFE 0x20
74 #define UART_LSR_SPECIAL 0x1E
76 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|\
79 #define C168_ASIC_ID 1
80 #define C104_ASIC_ID 2
81 #define C102_ASIC_ID 0xB
82 #define CI132_ASIC_ID 4
83 #define CI134_ASIC_ID 3
84 #define CI104J_ASIC_ID 5
86 #define MXSER_HIGHBAUD 1
89 /* This is only for PCI */
99 } Gpci_uart_info[] = {
100 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
101 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
102 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
104 #define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
106 struct mxser_cardinfo {
112 static const struct mxser_cardinfo mxser_cards[] = {
113 { 8, "C168 series", }, /* C168-ISA */
114 { 4, "C104 series", }, /* C104-ISA */
115 { 4, "CI-104J series", }, /* CI104J */
116 { 8, "C168H/PCI series", }, /* C168-PCI */
117 { 4, "C104H/PCI series", }, /* C104-PCI */
118 { 4, "C102 series", MXSER_HAS2 }, /* C102-ISA */
119 { 4, "CI-132 series", MXSER_HAS2 }, /* CI132 */
120 { 4, "CI-134 series", }, /* CI134 */
121 { 2, "CP-132 series", }, /* CP132 */
122 { 4, "CP-114 series", }, /* CP114 */
123 { 4, "CT-114 series", }, /* CT114 */
124 { 2, "CP-102 series", MXSER_HIGHBAUD }, /* CP102 */
125 { 4, "CP-104U series", }, /* CP104U */
126 { 8, "CP-168U series", }, /* CP168U */
127 { 2, "CP-132U series", }, /* CP132U */
128 { 4, "CP-134U series", }, /* CP134U */
129 { 4, "CP-104JU series", }, /* CP104JU */
130 { 8, "Moxa UC7000 Serial", }, /* RC7000 */
131 { 8, "CP-118U series", }, /* CP118U */
132 { 2, "CP-102UL series", }, /* CP102UL */
133 { 2, "CP-102U series", }, /* CP102U */
134 { 8, "CP-118EL series", }, /* CP118EL */
135 { 8, "CP-168EL series", }, /* CP168EL */
136 { 4, "CP-104EL series", } /* CP104EL */
139 /* driver_data correspond to the lines in the structure above
140 see also ISA probe function before you change something */
141 static struct pci_device_id mxser_pcibrds[] = {
142 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C168),
144 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C104),
146 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132),
148 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP114),
150 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CT114),
152 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102),
154 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104U),
156 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168U),
158 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132U),
160 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP134U),
162 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104JU),
164 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_RC7000),
166 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118U),
168 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102UL),
170 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102U),
172 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118EL),
174 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168EL),
176 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104EL),
180 MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
182 static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
183 static int ttymajor = MXSERMAJOR;
184 static int calloutmajor = MXSERCUMAJOR;
186 /* Variables for insmod */
188 MODULE_AUTHOR("Casper Yang");
189 MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
190 module_param_array(ioaddr, int, NULL, 0);
191 module_param(ttymajor, int, 0);
192 MODULE_LICENSE("GPL");
196 unsigned long rxcnt[MXSER_PORTS];
197 unsigned long txcnt[MXSER_PORTS];
204 unsigned long up_rxcnt;
205 unsigned long up_txcnt;
207 unsigned char hold_reason;
210 struct mxser_mon_ext {
211 unsigned long rx_cnt[32];
212 unsigned long tx_cnt[32];
213 unsigned long up_rxcnt[32];
214 unsigned long up_txcnt[32];
215 int modem_status[32];
229 struct mxser_board *board;
230 struct tty_struct *tty;
232 unsigned long ioaddr;
233 unsigned long opmode_ioaddr;
237 int rx_trigger; /* Rx fifo trigger level */
239 int baud_base; /* max. speed */
241 int type; /* UART type */
242 int flags; /* defined in tty.h */
244 int x_char; /* xon/xoff character */
245 int IER; /* Interrupt Enable Register */
246 int MCR; /* Modem control register */
248 unsigned char stop_rx;
249 unsigned char ldisc_stop_rx;
253 unsigned short closing_wait;
254 unsigned char err_shadow;
257 int count; /* # of fd on device */
258 int blocked_open; /* # of blocked opens */
259 struct async_icount icount; /* kernel counters for 4 input interrupts */
262 int read_status_mask;
263 int ignore_status_mask;
265 unsigned char *xmit_buf;
270 struct ktermios normal_termios;
272 struct mxser_mon mon_data;
275 wait_queue_head_t open_wait;
276 wait_queue_head_t delta_msr_wait;
282 const struct mxser_cardinfo *info;
283 unsigned long vector;
284 unsigned long vector_mask;
289 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
292 struct mxser_mstatus {
300 static struct mxser_mstatus GMStatus[MXSER_PORTS];
302 static int mxserBoardCAP[MXSER_BOARDS] = {
304 /* 0x180, 0x280, 0x200, 0x320 */
307 static struct mxser_board mxser_boards[MXSER_BOARDS];
308 static struct tty_driver *mxvar_sdriver;
309 static struct mxser_log mxvar_log;
310 static int mxvar_diagflag;
311 static unsigned char mxser_msr[MXSER_PORTS + 1];
312 static struct mxser_mon_ext mon_data_ext;
313 static int mxser_set_baud_method[MXSER_PORTS + 1];
316 static int __devinit CheckIsMoxaMust(int io)
321 outb(0, io + UART_LCR);
322 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
323 oldmcr = inb(io + UART_MCR);
324 outb(0, io + UART_MCR);
325 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
326 if ((hwid = inb(io + UART_MCR)) != 0) {
327 outb(oldmcr, io + UART_MCR);
328 return MOXA_OTHER_UART;
331 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
332 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
333 if (hwid == Gpci_uart_info[i].type)
336 return MOXA_OTHER_UART;
340 static void process_txrx_fifo(struct mxser_port *info)
344 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
345 info->rx_trigger = 1;
346 info->rx_high_water = 1;
347 info->rx_low_water = 1;
348 info->xmit_fifo_size = 1;
350 for (i = 0; i < UART_INFO_NUM; i++)
351 if (info->board->chip_flag == Gpci_uart_info[i].type) {
352 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
353 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
354 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
355 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
360 static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
362 unsigned char status = 0;
364 status = inb(baseaddr + UART_MSR);
366 mxser_msr[port] &= 0x0F;
367 mxser_msr[port] |= status;
368 status = mxser_msr[port];
375 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
376 struct mxser_port *port)
378 DECLARE_WAITQUEUE(wait, current);
384 * If non-blocking mode is set, or the port is not enabled,
385 * then make the check up front and then exit.
387 if ((filp->f_flags & O_NONBLOCK) ||
388 test_bit(TTY_IO_ERROR, &tty->flags)) {
389 port->flags |= ASYNC_NORMAL_ACTIVE;
393 if (tty->termios->c_cflag & CLOCAL)
397 * Block waiting for the carrier detect and the line to become
398 * free (i.e., not in use by the callout). While we are in
399 * this loop, port->count is dropped by one, so that
400 * mxser_close() knows when to free things. We restore it upon
401 * exit, either normal or abnormal.
404 add_wait_queue(&port->open_wait, &wait);
406 spin_lock_irqsave(&port->slock, flags);
407 if (!tty_hung_up_p(filp))
409 spin_unlock_irqrestore(&port->slock, flags);
410 port->blocked_open++;
412 spin_lock_irqsave(&port->slock, flags);
413 outb(inb(port->ioaddr + UART_MCR) |
414 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
415 spin_unlock_irqrestore(&port->slock, flags);
416 set_current_state(TASK_INTERRUPTIBLE);
417 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
418 if (port->flags & ASYNC_HUP_NOTIFY)
421 retval = -ERESTARTSYS;
424 if (!(port->flags & ASYNC_CLOSING) &&
426 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
428 if (signal_pending(current)) {
429 retval = -ERESTARTSYS;
434 set_current_state(TASK_RUNNING);
435 remove_wait_queue(&port->open_wait, &wait);
436 if (!tty_hung_up_p(filp))
438 port->blocked_open--;
441 port->flags |= ASYNC_NORMAL_ACTIVE;
445 static int mxser_set_baud(struct mxser_port *info, long newspd)
452 if (!info->tty || !info->tty->termios)
458 if (newspd > info->max_baud)
461 info->realbaud = newspd;
463 quot = (2 * info->baud_base / 269);
465 quot = info->baud_base / newspd;
472 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
473 info->timeout += HZ / 50; /* Add .02 seconds of slop */
476 spin_lock_irqsave(&info->slock, flags);
477 info->MCR |= UART_MCR_DTR;
478 outb(info->MCR, info->ioaddr + UART_MCR);
479 spin_unlock_irqrestore(&info->slock, flags);
481 spin_lock_irqsave(&info->slock, flags);
482 info->MCR &= ~UART_MCR_DTR;
483 outb(info->MCR, info->ioaddr + UART_MCR);
484 spin_unlock_irqrestore(&info->slock, flags);
488 cval = inb(info->ioaddr + UART_LCR);
490 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
492 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
493 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
494 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
501 * This routine is called to set the UART divisor registers to match
502 * the specified baud rate for a serial port.
504 static int mxser_change_speed(struct mxser_port *info,
505 struct ktermios *old_termios)
507 unsigned cflag, cval, fcr;
509 unsigned char status;
513 if (!info->tty || !info->tty->termios)
515 cflag = info->tty->termios->c_cflag;
519 if (mxser_set_baud_method[info->tty->index] == 0) {
520 baud = tty_get_baud_rate(info->tty);
521 mxser_set_baud(info, baud);
524 /* byte size and parity */
525 switch (cflag & CSIZE) {
540 break; /* too keep GCC shut... */
545 cval |= UART_LCR_PARITY;
546 if (!(cflag & PARODD))
547 cval |= UART_LCR_EPAR;
549 cval |= UART_LCR_SPAR;
551 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
552 if (info->board->chip_flag) {
553 fcr = UART_FCR_ENABLE_FIFO;
554 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
555 SET_MOXA_MUST_FIFO_VALUE(info);
559 fcr = UART_FCR_ENABLE_FIFO;
560 if (info->board->chip_flag) {
561 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
562 SET_MOXA_MUST_FIFO_VALUE(info);
564 switch (info->rx_trigger) {
566 fcr |= UART_FCR_TRIGGER_1;
569 fcr |= UART_FCR_TRIGGER_4;
572 fcr |= UART_FCR_TRIGGER_8;
575 fcr |= UART_FCR_TRIGGER_14;
581 /* CTS flow control flag and modem status interrupts */
582 info->IER &= ~UART_IER_MSI;
583 info->MCR &= ~UART_MCR_AFE;
584 if (cflag & CRTSCTS) {
585 info->flags |= ASYNC_CTS_FLOW;
586 info->IER |= UART_IER_MSI;
587 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
588 info->MCR |= UART_MCR_AFE;
590 status = inb(info->ioaddr + UART_MSR);
591 if (info->tty->hw_stopped) {
592 if (status & UART_MSR_CTS) {
593 info->tty->hw_stopped = 0;
594 if (info->type != PORT_16550A &&
595 !info->board->chip_flag) {
596 outb(info->IER & ~UART_IER_THRI,
599 info->IER |= UART_IER_THRI;
600 outb(info->IER, info->ioaddr +
603 tty_wakeup(info->tty);
606 if (!(status & UART_MSR_CTS)) {
607 info->tty->hw_stopped = 1;
608 if ((info->type != PORT_16550A) &&
609 (!info->board->chip_flag)) {
610 info->IER &= ~UART_IER_THRI;
611 outb(info->IER, info->ioaddr +
618 info->flags &= ~ASYNC_CTS_FLOW;
620 outb(info->MCR, info->ioaddr + UART_MCR);
621 if (cflag & CLOCAL) {
622 info->flags &= ~ASYNC_CHECK_CD;
624 info->flags |= ASYNC_CHECK_CD;
625 info->IER |= UART_IER_MSI;
627 outb(info->IER, info->ioaddr + UART_IER);
630 * Set up parity check flag
632 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
633 if (I_INPCK(info->tty))
634 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
635 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
636 info->read_status_mask |= UART_LSR_BI;
638 info->ignore_status_mask = 0;
640 if (I_IGNBRK(info->tty)) {
641 info->ignore_status_mask |= UART_LSR_BI;
642 info->read_status_mask |= UART_LSR_BI;
644 * If we're ignore parity and break indicators, ignore
645 * overruns too. (For real raw support).
647 if (I_IGNPAR(info->tty)) {
648 info->ignore_status_mask |=
652 info->read_status_mask |=
658 if (info->board->chip_flag) {
659 spin_lock_irqsave(&info->slock, flags);
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);
672 spin_unlock_irqrestore(&info->slock, flags);
676 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
677 outb(cval, info->ioaddr + UART_LCR);
682 static void mxser_check_modem_status(struct mxser_port *port, int status)
684 /* update input line counters */
685 if (status & UART_MSR_TERI)
687 if (status & UART_MSR_DDSR)
689 if (status & UART_MSR_DDCD)
691 if (status & UART_MSR_DCTS)
693 port->mon_data.modem_status = status;
694 wake_up_interruptible(&port->delta_msr_wait);
696 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
697 if (status & UART_MSR_DCD)
698 wake_up_interruptible(&port->open_wait);
701 if (port->flags & ASYNC_CTS_FLOW) {
702 if (port->tty->hw_stopped) {
703 if (status & UART_MSR_CTS) {
704 port->tty->hw_stopped = 0;
706 if ((port->type != PORT_16550A) &&
707 (!port->board->chip_flag)) {
708 outb(port->IER & ~UART_IER_THRI,
709 port->ioaddr + UART_IER);
710 port->IER |= UART_IER_THRI;
711 outb(port->IER, port->ioaddr +
714 tty_wakeup(port->tty);
717 if (!(status & UART_MSR_CTS)) {
718 port->tty->hw_stopped = 1;
719 if (port->type != PORT_16550A &&
720 !port->board->chip_flag) {
721 port->IER &= ~UART_IER_THRI;
722 outb(port->IER, port->ioaddr +
730 static int mxser_startup(struct mxser_port *info)
735 page = __get_free_page(GFP_KERNEL);
739 spin_lock_irqsave(&info->slock, flags);
741 if (info->flags & ASYNC_INITIALIZED) {
743 spin_unlock_irqrestore(&info->slock, flags);
747 if (!info->ioaddr || !info->type) {
749 set_bit(TTY_IO_ERROR, &info->tty->flags);
751 spin_unlock_irqrestore(&info->slock, flags);
757 info->xmit_buf = (unsigned char *) page;
760 * Clear the FIFO buffers and disable them
761 * (they will be reenabled in mxser_change_speed())
763 if (info->board->chip_flag)
764 outb((UART_FCR_CLEAR_RCVR |
765 UART_FCR_CLEAR_XMIT |
766 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
768 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
769 info->ioaddr + UART_FCR);
772 * At this point there's no way the LSR could still be 0xFF;
773 * if it is, then bail out, because there's likely no UART
776 if (inb(info->ioaddr + UART_LSR) == 0xff) {
777 spin_unlock_irqrestore(&info->slock, flags);
778 if (capable(CAP_SYS_ADMIN)) {
780 set_bit(TTY_IO_ERROR, &info->tty->flags);
787 * Clear the interrupt registers.
789 (void) inb(info->ioaddr + UART_LSR);
790 (void) inb(info->ioaddr + UART_RX);
791 (void) inb(info->ioaddr + UART_IIR);
792 (void) inb(info->ioaddr + UART_MSR);
795 * Now, initialize the UART
797 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
798 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
799 outb(info->MCR, info->ioaddr + UART_MCR);
802 * Finally, enable interrupts
804 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
806 if (info->board->chip_flag)
807 info->IER |= MOXA_MUST_IER_EGDAI;
808 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
811 * And clear the interrupt registers again for luck.
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 clear_bit(TTY_IO_ERROR, &info->tty->flags);
820 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
823 * and set the speed of the serial port
825 spin_unlock_irqrestore(&info->slock, flags);
826 mxser_change_speed(info, NULL);
828 info->flags |= ASYNC_INITIALIZED;
833 * This routine will shutdown a serial port; interrupts maybe disabled, and
834 * DTR is dropped if the hangup on close termio flag is on.
836 static void mxser_shutdown(struct mxser_port *info)
840 if (!(info->flags & ASYNC_INITIALIZED))
843 spin_lock_irqsave(&info->slock, flags);
846 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
847 * here so the queue might never be waken up
849 wake_up_interruptible(&info->delta_msr_wait);
852 * Free the IRQ, if necessary
854 if (info->xmit_buf) {
855 free_page((unsigned long) info->xmit_buf);
856 info->xmit_buf = NULL;
860 outb(0x00, info->ioaddr + UART_IER);
862 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
863 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
864 outb(info->MCR, info->ioaddr + UART_MCR);
866 /* clear Rx/Tx FIFO's */
867 if (info->board->chip_flag)
868 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
869 MOXA_MUST_FCR_GDA_MODE_ENABLE,
870 info->ioaddr + UART_FCR);
872 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
873 info->ioaddr + UART_FCR);
875 /* read data port to reset things */
876 (void) inb(info->ioaddr + UART_RX);
879 set_bit(TTY_IO_ERROR, &info->tty->flags);
881 info->flags &= ~ASYNC_INITIALIZED;
883 if (info->board->chip_flag)
884 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
886 spin_unlock_irqrestore(&info->slock, flags);
890 * This routine is called whenever a serial port is opened. It
891 * enables interrupts for a serial port, linking in its async structure into
892 * the IRQ chain. It also performs the serial-specific
893 * initialization for the tty structure.
895 static int mxser_open(struct tty_struct *tty, struct file *filp)
897 struct mxser_port *info;
900 /* initialize driver_data in case something fails */
901 tty->driver_data = NULL;
904 if (line == MXSER_PORTS)
906 if (line < 0 || line > MXSER_PORTS)
908 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
912 tty->driver_data = info;
915 * Start up serial port
918 retval = mxser_startup(info);
922 retval = mxser_block_til_ready(tty, filp, info);
926 /* unmark here for very high baud rate (ex. 921600 bps) used */
927 tty->low_latency = 1;
932 * This routine is called when the serial port gets closed. First, we
933 * wait for the last remaining data to be sent. Then, we unlink its
934 * async structure from the interrupt chain if necessary, and we free
935 * that IRQ if nothing is left in the chain.
937 static void mxser_close(struct tty_struct *tty, struct file *filp)
939 struct mxser_port *info = tty->driver_data;
941 unsigned long timeout;
944 if (tty->index == MXSER_PORTS)
949 spin_lock_irqsave(&info->slock, flags);
951 if (tty_hung_up_p(filp)) {
952 spin_unlock_irqrestore(&info->slock, flags);
955 if ((tty->count == 1) && (info->count != 1)) {
957 * Uh, oh. tty->count is 1, which means that the tty
958 * structure will be freed. Info->count should always
959 * be one in these conditions. If it's greater than
960 * one, we've got real problems, since it means the
961 * serial port won't be shutdown.
963 printk(KERN_ERR "mxser_close: bad serial port count; "
964 "tty->count is 1, info->count is %d\n", info->count);
967 if (--info->count < 0) {
968 printk(KERN_ERR "mxser_close: bad serial port count for "
969 "ttys%d: %d\n", tty->index, info->count);
973 spin_unlock_irqrestore(&info->slock, flags);
976 info->flags |= ASYNC_CLOSING;
977 spin_unlock_irqrestore(&info->slock, flags);
979 * Save the termios structure, since this port may have
980 * separate termios for callout and dialin.
982 if (info->flags & ASYNC_NORMAL_ACTIVE)
983 info->normal_termios = *tty->termios;
985 * Now we wait for the transmit buffer to clear; and we notify
986 * the line discipline to only process XON/XOFF characters.
989 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
990 tty_wait_until_sent(tty, info->closing_wait);
992 * At this point we stop accepting input. To do this, we
993 * disable the receive line status interrupts, and tell the
994 * interrupt driver to stop checking the data ready bit in the
995 * line status register.
997 info->IER &= ~UART_IER_RLSI;
998 if (info->board->chip_flag)
999 info->IER &= ~MOXA_MUST_RECV_ISR;
1001 if (info->flags & ASYNC_INITIALIZED) {
1002 outb(info->IER, info->ioaddr + UART_IER);
1004 * Before we drop DTR, make sure the UART transmitter
1005 * has completely drained; this is especially
1006 * important if there is a transmit FIFO!
1008 timeout = jiffies + HZ;
1009 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
1010 schedule_timeout_interruptible(5);
1011 if (time_after(jiffies, timeout))
1015 mxser_shutdown(info);
1017 if (tty->driver->flush_buffer)
1018 tty->driver->flush_buffer(tty);
1020 tty_ldisc_flush(tty);
1025 if (info->blocked_open) {
1026 if (info->close_delay)
1027 schedule_timeout_interruptible(info->close_delay);
1028 wake_up_interruptible(&info->open_wait);
1031 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1034 static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1037 struct mxser_port *info = tty->driver_data;
1038 unsigned long flags;
1040 if (!info->xmit_buf)
1044 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1045 SERIAL_XMIT_SIZE - info->xmit_head));
1049 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1050 spin_lock_irqsave(&info->slock, flags);
1051 info->xmit_head = (info->xmit_head + c) &
1052 (SERIAL_XMIT_SIZE - 1);
1053 info->xmit_cnt += c;
1054 spin_unlock_irqrestore(&info->slock, flags);
1061 if (info->xmit_cnt && !tty->stopped) {
1062 if (!tty->hw_stopped ||
1063 (info->type == PORT_16550A) ||
1064 (info->board->chip_flag)) {
1065 spin_lock_irqsave(&info->slock, flags);
1066 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1068 info->IER |= UART_IER_THRI;
1069 outb(info->IER, info->ioaddr + UART_IER);
1070 spin_unlock_irqrestore(&info->slock, flags);
1076 static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1078 struct mxser_port *info = tty->driver_data;
1079 unsigned long flags;
1081 if (!info->xmit_buf)
1084 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1087 spin_lock_irqsave(&info->slock, flags);
1088 info->xmit_buf[info->xmit_head++] = ch;
1089 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1091 spin_unlock_irqrestore(&info->slock, flags);
1092 if (!tty->stopped) {
1093 if (!tty->hw_stopped ||
1094 (info->type == PORT_16550A) ||
1095 info->board->chip_flag) {
1096 spin_lock_irqsave(&info->slock, flags);
1097 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1098 info->IER |= UART_IER_THRI;
1099 outb(info->IER, info->ioaddr + UART_IER);
1100 spin_unlock_irqrestore(&info->slock, flags);
1106 static void mxser_flush_chars(struct tty_struct *tty)
1108 struct mxser_port *info = tty->driver_data;
1109 unsigned long flags;
1111 if (info->xmit_cnt <= 0 ||
1115 (info->type != PORT_16550A) &&
1116 (!info->board->chip_flag)
1120 spin_lock_irqsave(&info->slock, flags);
1122 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1123 info->IER |= UART_IER_THRI;
1124 outb(info->IER, info->ioaddr + UART_IER);
1126 spin_unlock_irqrestore(&info->slock, flags);
1129 static int mxser_write_room(struct tty_struct *tty)
1131 struct mxser_port *info = tty->driver_data;
1134 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1140 static int mxser_chars_in_buffer(struct tty_struct *tty)
1142 struct mxser_port *info = tty->driver_data;
1143 return info->xmit_cnt;
1146 static void mxser_flush_buffer(struct tty_struct *tty)
1148 struct mxser_port *info = tty->driver_data;
1150 unsigned long flags;
1153 spin_lock_irqsave(&info->slock, flags);
1154 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1156 fcr = inb(info->ioaddr + UART_FCR);
1157 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1158 info->ioaddr + UART_FCR);
1159 outb(fcr, info->ioaddr + UART_FCR);
1161 spin_unlock_irqrestore(&info->slock, flags);
1167 * ------------------------------------------------------------
1168 * friends of mxser_ioctl()
1169 * ------------------------------------------------------------
1171 static int mxser_get_serial_info(struct mxser_port *info,
1172 struct serial_struct __user *retinfo)
1174 struct serial_struct tmp;
1178 memset(&tmp, 0, sizeof(tmp));
1179 tmp.type = info->type;
1180 tmp.line = info->tty->index;
1181 tmp.port = info->ioaddr;
1182 tmp.irq = info->board->irq;
1183 tmp.flags = info->flags;
1184 tmp.baud_base = info->baud_base;
1185 tmp.close_delay = info->close_delay;
1186 tmp.closing_wait = info->closing_wait;
1187 tmp.custom_divisor = info->custom_divisor;
1189 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1194 static int mxser_set_serial_info(struct mxser_port *info,
1195 struct serial_struct __user *new_info)
1197 struct serial_struct new_serial;
1201 if (!new_info || !info->ioaddr)
1203 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1206 if ((new_serial.irq != info->board->irq) ||
1207 (new_serial.port != info->ioaddr) ||
1208 (new_serial.custom_divisor != info->custom_divisor) ||
1209 (new_serial.baud_base != info->baud_base))
1212 flags = info->flags & ASYNC_SPD_MASK;
1214 if (!capable(CAP_SYS_ADMIN)) {
1215 if ((new_serial.baud_base != info->baud_base) ||
1216 (new_serial.close_delay != info->close_delay) ||
1217 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
1219 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1220 (new_serial.flags & ASYNC_USR_MASK));
1223 * OK, past this point, all the error checking has been done.
1224 * At this point, we start making changes.....
1226 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1227 (new_serial.flags & ASYNC_FLAGS));
1228 info->close_delay = new_serial.close_delay * HZ / 100;
1229 info->closing_wait = new_serial.closing_wait * HZ / 100;
1230 info->tty->low_latency =
1231 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1232 info->tty->low_latency = 0;
1235 info->type = new_serial.type;
1237 process_txrx_fifo(info);
1239 if (info->flags & ASYNC_INITIALIZED) {
1240 if (flags != (info->flags & ASYNC_SPD_MASK))
1241 mxser_change_speed(info, NULL);
1243 retval = mxser_startup(info);
1249 * mxser_get_lsr_info - get line status register info
1251 * Purpose: Let user call ioctl() to get info when the UART physically
1252 * is emptied. On bus types like RS485, the transmitter must
1253 * release the bus after transmitting. This must be done when
1254 * the transmit shift register is empty, not be done when the
1255 * transmit holding register is empty. This functionality
1256 * allows an RS485 driver to be written in user space.
1258 static int mxser_get_lsr_info(struct mxser_port *info,
1259 unsigned int __user *value)
1261 unsigned char status;
1262 unsigned int result;
1263 unsigned long flags;
1265 spin_lock_irqsave(&info->slock, flags);
1266 status = inb(info->ioaddr + UART_LSR);
1267 spin_unlock_irqrestore(&info->slock, flags);
1268 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1269 return put_user(result, value);
1273 * This routine sends a break character out the serial port.
1275 static void mxser_send_break(struct mxser_port *info, int duration)
1277 unsigned long flags;
1281 set_current_state(TASK_INTERRUPTIBLE);
1282 spin_lock_irqsave(&info->slock, flags);
1283 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1284 info->ioaddr + UART_LCR);
1285 spin_unlock_irqrestore(&info->slock, flags);
1286 schedule_timeout(duration);
1287 spin_lock_irqsave(&info->slock, flags);
1288 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1289 info->ioaddr + UART_LCR);
1290 spin_unlock_irqrestore(&info->slock, flags);
1293 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
1295 struct mxser_port *info = tty->driver_data;
1296 unsigned char control, status;
1297 unsigned long flags;
1300 if (tty->index == MXSER_PORTS)
1301 return -ENOIOCTLCMD;
1302 if (test_bit(TTY_IO_ERROR, &tty->flags))
1305 control = info->MCR;
1307 spin_lock_irqsave(&info->slock, flags);
1308 status = inb(info->ioaddr + UART_MSR);
1309 if (status & UART_MSR_ANY_DELTA)
1310 mxser_check_modem_status(info, status);
1311 spin_unlock_irqrestore(&info->slock, flags);
1312 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1313 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1314 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1315 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1316 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1317 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1320 static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1321 unsigned int set, unsigned int clear)
1323 struct mxser_port *info = tty->driver_data;
1324 unsigned long flags;
1327 if (tty->index == MXSER_PORTS)
1328 return -ENOIOCTLCMD;
1329 if (test_bit(TTY_IO_ERROR, &tty->flags))
1332 spin_lock_irqsave(&info->slock, flags);
1334 if (set & TIOCM_RTS)
1335 info->MCR |= UART_MCR_RTS;
1336 if (set & TIOCM_DTR)
1337 info->MCR |= UART_MCR_DTR;
1339 if (clear & TIOCM_RTS)
1340 info->MCR &= ~UART_MCR_RTS;
1341 if (clear & TIOCM_DTR)
1342 info->MCR &= ~UART_MCR_DTR;
1344 outb(info->MCR, info->ioaddr + UART_MCR);
1345 spin_unlock_irqrestore(&info->slock, flags);
1349 static int __init mxser_program_mode(int port)
1361 id = inb(port + 1) & 0x1F;
1362 if ((id != C168_ASIC_ID) &&
1363 (id != C104_ASIC_ID) &&
1364 (id != C102_ASIC_ID) &&
1365 (id != CI132_ASIC_ID) &&
1366 (id != CI134_ASIC_ID) &&
1367 (id != CI104J_ASIC_ID))
1369 for (i = 0, j = 0; i < 4; i++) {
1373 } else if ((j == 1) && (n == 1)) {
1384 static void __init mxser_normal_mode(int port)
1388 outb(0xA5, port + 1);
1389 outb(0x80, port + 3);
1390 outb(12, port + 0); /* 9600 bps */
1392 outb(0x03, port + 3); /* 8 data bits */
1393 outb(0x13, port + 4); /* loop back mode */
1394 for (i = 0; i < 16; i++) {
1396 if ((n & 0x61) == 0x60)
1401 outb(0x00, port + 4);
1404 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1405 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1406 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1407 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1408 #define EN_CCMD 0x000 /* Chip's command register */
1409 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1410 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1411 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1412 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1413 #define EN0_DCFG 0x00E /* Data configuration reg WR */
1414 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1415 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1416 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1417 static int __init mxser_read_register(int port, unsigned short *regs)
1419 int i, k, value, id;
1422 id = mxser_program_mode(port);
1425 for (i = 0; i < 14; i++) {
1426 k = (i & 0x3F) | 0x180;
1427 for (j = 0x100; j > 0; j >>= 1) {
1428 outb(CHIP_CS, port);
1430 outb(CHIP_CS | CHIP_DO, port);
1431 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1433 outb(CHIP_CS, port);
1434 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1439 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1440 outb(CHIP_CS, port);
1441 outb(CHIP_CS | CHIP_SK, port);
1442 if (inb(port) & CHIP_DI)
1448 mxser_normal_mode(port);
1452 static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1454 struct mxser_port *port;
1460 /* if (copy_to_user(argp, mxsercfg,
1461 sizeof(struct mxser_hwconf) * 4))
1465 case MOXA_GET_MAJOR:
1466 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1470 case MOXA_GET_CUMAJOR:
1471 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1475 case MOXA_CHKPORTENABLE:
1478 for (i = 0; i < MXSER_BOARDS; i++)
1479 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1480 if (mxser_boards[i].ports[j].ioaddr)
1483 return put_user(result, (unsigned long __user *)argp);
1484 case MOXA_GETDATACOUNT:
1485 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1488 case MOXA_GETMSTATUS:
1489 for (i = 0; i < MXSER_BOARDS; i++)
1490 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1491 port = &mxser_boards[i].ports[j];
1494 if (!port->ioaddr) {
1495 GMStatus[i].dcd = 0;
1496 GMStatus[i].dsr = 0;
1497 GMStatus[i].cts = 0;
1501 if (!port->tty || !port->tty->termios)
1503 port->normal_termios.c_cflag;
1506 port->tty->termios->c_cflag;
1508 status = inb(port->ioaddr + UART_MSR);
1509 if (status & 0x80 /*UART_MSR_DCD */ )
1510 GMStatus[i].dcd = 1;
1512 GMStatus[i].dcd = 0;
1514 if (status & 0x20 /*UART_MSR_DSR */ )
1515 GMStatus[i].dsr = 1;
1517 GMStatus[i].dsr = 0;
1520 if (status & 0x10 /*UART_MSR_CTS */ )
1521 GMStatus[i].cts = 1;
1523 GMStatus[i].cts = 0;
1525 if (copy_to_user(argp, GMStatus,
1526 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1529 case MOXA_ASPP_MON_EXT: {
1530 int status, p, shiftbit;
1531 unsigned long opmode;
1532 unsigned cflag, iflag;
1534 for (i = 0; i < MXSER_BOARDS; i++)
1535 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1536 port = &mxser_boards[i].ports[j];
1540 status = mxser_get_msr(port->ioaddr, 0, i);
1542 if (status & UART_MSR_TERI)
1544 if (status & UART_MSR_DDSR)
1546 if (status & UART_MSR_DDCD)
1548 if (status & UART_MSR_DCTS)
1551 port->mon_data.modem_status = status;
1552 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1553 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1554 mon_data_ext.up_rxcnt[i] =
1555 port->mon_data.up_rxcnt;
1556 mon_data_ext.up_txcnt[i] =
1557 port->mon_data.up_txcnt;
1558 mon_data_ext.modem_status[i] =
1559 port->mon_data.modem_status;
1560 mon_data_ext.baudrate[i] = port->realbaud;
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;
1598 if (copy_to_user(argp, &mon_data_ext,
1599 sizeof(mon_data_ext)))
1605 return -ENOIOCTLCMD;
1610 static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1611 unsigned int cmd, unsigned long arg)
1613 struct mxser_port *info = tty->driver_data;
1614 struct async_icount cprev, cnow; /* kernel counter temps */
1615 struct serial_icounter_struct __user *p_cuser;
1616 unsigned long templ;
1617 unsigned long flags;
1618 void __user *argp = (void __user *)arg;
1621 if (tty->index == MXSER_PORTS)
1622 return mxser_ioctl_special(cmd, argp);
1624 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1626 unsigned long opmode;
1627 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1629 unsigned char val, mask;
1632 if (cmd == MOXA_SET_OP_MODE) {
1633 if (get_user(opmode, (int __user *) argp))
1635 if (opmode != RS232_MODE &&
1636 opmode != RS485_2WIRE_MODE &&
1637 opmode != RS422_MODE &&
1638 opmode != RS485_4WIRE_MODE)
1642 val = inb(info->opmode_ioaddr);
1644 val |= (opmode << shiftbit);
1645 outb(val, info->opmode_ioaddr);
1648 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1649 opmode &= OP_MODE_MASK;
1650 if (copy_to_user(argp, &opmode, sizeof(int)))
1656 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1657 test_bit(TTY_IO_ERROR, &tty->flags))
1661 case TCSBRK: /* SVID version: non-zero arg --> no break */
1662 retval = tty_check_change(tty);
1665 tty_wait_until_sent(tty, 0);
1667 mxser_send_break(info, HZ / 4); /* 1/4 second */
1669 case TCSBRKP: /* support for POSIX tcsendbreak() */
1670 retval = tty_check_change(tty);
1673 tty_wait_until_sent(tty, 0);
1674 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1677 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1679 if (get_user(templ, (unsigned long __user *) argp))
1682 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1685 return mxser_get_serial_info(info, argp);
1687 return mxser_set_serial_info(info, argp);
1688 case TIOCSERGETLSR: /* Get line status register */
1689 return mxser_get_lsr_info(info, argp);
1691 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1692 * - mask passed in arg for lines of interest
1693 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1694 * Caller should use TIOCGICOUNT to see which one it was
1697 DECLARE_WAITQUEUE(wait, current);
1699 spin_lock_irqsave(&info->slock, flags);
1700 cprev = info->icount; /* note the counters on entry */
1701 spin_unlock_irqrestore(&info->slock, flags);
1703 add_wait_queue(&info->delta_msr_wait, &wait);
1705 spin_lock_irqsave(&info->slock, flags);
1706 cnow = info->icount; /* atomic copy */
1707 spin_unlock_irqrestore(&info->slock, flags);
1709 set_current_state(TASK_INTERRUPTIBLE);
1710 if (((arg & TIOCM_RNG) &&
1711 (cnow.rng != cprev.rng)) ||
1712 ((arg & TIOCM_DSR) &&
1713 (cnow.dsr != cprev.dsr)) ||
1714 ((arg & TIOCM_CD) &&
1715 (cnow.dcd != cprev.dcd)) ||
1716 ((arg & TIOCM_CTS) &&
1717 (cnow.cts != cprev.cts))) {
1721 /* see if a signal did it */
1722 if (signal_pending(current)) {
1728 current->state = TASK_RUNNING;
1729 remove_wait_queue(&info->delta_msr_wait, &wait);
1734 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1735 * Return: write counters to the user passed counter struct
1736 * NB: both 1->0 and 0->1 transitions are counted except for
1737 * RI where only 0->1 is counted.
1740 spin_lock_irqsave(&info->slock, flags);
1741 cnow = info->icount;
1742 spin_unlock_irqrestore(&info->slock, flags);
1744 if (put_user(cnow.frame, &p_cuser->frame))
1746 if (put_user(cnow.brk, &p_cuser->brk))
1748 if (put_user(cnow.overrun, &p_cuser->overrun))
1750 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1752 if (put_user(cnow.parity, &p_cuser->parity))
1754 if (put_user(cnow.rx, &p_cuser->rx))
1756 if (put_user(cnow.tx, &p_cuser->tx))
1758 put_user(cnow.cts, &p_cuser->cts);
1759 put_user(cnow.dsr, &p_cuser->dsr);
1760 put_user(cnow.rng, &p_cuser->rng);
1761 put_user(cnow.dcd, &p_cuser->dcd);
1763 case MOXA_HighSpeedOn:
1764 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1765 case MOXA_SDS_RSTICOUNTER:
1766 info->mon_data.rxcnt = 0;
1767 info->mon_data.txcnt = 0;
1769 case MOXA_ASPP_SETBAUD:{
1771 if (get_user(baud, (long __user *)argp))
1773 mxser_set_baud(info, baud);
1776 case MOXA_ASPP_GETBAUD:
1777 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1782 case MOXA_ASPP_OQUEUE:{
1785 len = mxser_chars_in_buffer(tty);
1787 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1789 len += (lsr ? 0 : 1);
1791 if (copy_to_user(argp, &len, sizeof(int)))
1796 case MOXA_ASPP_MON: {
1799 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1800 mxser_check_modem_status(info, status);
1802 mcr = inb(info->ioaddr + UART_MCR);
1803 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1804 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1806 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1808 if (mcr & MOXA_MUST_MCR_TX_XON)
1809 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1811 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1813 if (info->tty->hw_stopped)
1814 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1816 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1818 if (copy_to_user(argp, &info->mon_data,
1819 sizeof(struct mxser_mon)))
1824 case MOXA_ASPP_LSTATUS: {
1825 if (copy_to_user(argp, &info->err_shadow,
1826 sizeof(unsigned char)))
1829 info->err_shadow = 0;
1832 case MOXA_SET_BAUD_METHOD: {
1835 if (get_user(method, (int __user *)argp))
1837 mxser_set_baud_method[tty->index] = method;
1838 if (copy_to_user(argp, &method, sizeof(int)))
1844 return -ENOIOCTLCMD;
1849 static void mxser_stoprx(struct tty_struct *tty)
1851 struct mxser_port *info = tty->driver_data;
1853 info->ldisc_stop_rx = 1;
1855 if (info->board->chip_flag) {
1856 info->IER &= ~MOXA_MUST_RECV_ISR;
1857 outb(info->IER, info->ioaddr + UART_IER);
1859 info->x_char = STOP_CHAR(tty);
1860 outb(0, info->ioaddr + UART_IER);
1861 info->IER |= UART_IER_THRI;
1862 outb(info->IER, info->ioaddr + UART_IER);
1866 if (info->tty->termios->c_cflag & CRTSCTS) {
1867 info->MCR &= ~UART_MCR_RTS;
1868 outb(info->MCR, info->ioaddr + UART_MCR);
1873 * This routine is called by the upper-layer tty layer to signal that
1874 * incoming characters should be throttled.
1876 static void mxser_throttle(struct tty_struct *tty)
1881 static void mxser_unthrottle(struct tty_struct *tty)
1883 struct mxser_port *info = tty->driver_data;
1886 info->ldisc_stop_rx = 0;
1891 if (info->board->chip_flag) {
1892 info->IER |= MOXA_MUST_RECV_ISR;
1893 outb(info->IER, info->ioaddr + UART_IER);
1895 info->x_char = START_CHAR(tty);
1896 outb(0, info->ioaddr + UART_IER);
1897 info->IER |= UART_IER_THRI;
1898 outb(info->IER, info->ioaddr + UART_IER);
1903 if (info->tty->termios->c_cflag & CRTSCTS) {
1904 info->MCR |= UART_MCR_RTS;
1905 outb(info->MCR, info->ioaddr + UART_MCR);
1910 * mxser_stop() and mxser_start()
1912 * This routines are called before setting or resetting tty->stopped.
1913 * They enable or disable transmitter interrupts, as necessary.
1915 static void mxser_stop(struct tty_struct *tty)
1917 struct mxser_port *info = tty->driver_data;
1918 unsigned long flags;
1920 spin_lock_irqsave(&info->slock, flags);
1921 if (info->IER & UART_IER_THRI) {
1922 info->IER &= ~UART_IER_THRI;
1923 outb(info->IER, info->ioaddr + UART_IER);
1925 spin_unlock_irqrestore(&info->slock, flags);
1928 static void mxser_start(struct tty_struct *tty)
1930 struct mxser_port *info = tty->driver_data;
1931 unsigned long flags;
1933 spin_lock_irqsave(&info->slock, flags);
1934 if (info->xmit_cnt && info->xmit_buf) {
1935 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1936 info->IER |= UART_IER_THRI;
1937 outb(info->IER, info->ioaddr + UART_IER);
1939 spin_unlock_irqrestore(&info->slock, flags);
1942 static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1944 struct mxser_port *info = tty->driver_data;
1945 unsigned long flags;
1947 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1948 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
1950 mxser_change_speed(info, old_termios);
1952 if ((old_termios->c_cflag & CRTSCTS) &&
1953 !(tty->termios->c_cflag & CRTSCTS)) {
1954 tty->hw_stopped = 0;
1959 /* Handle sw stopped */
1960 if ((old_termios->c_iflag & IXON) &&
1961 !(tty->termios->c_iflag & IXON)) {
1964 if (info->board->chip_flag) {
1965 spin_lock_irqsave(&info->slock, flags);
1966 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
1967 spin_unlock_irqrestore(&info->slock, flags);
1975 * mxser_wait_until_sent() --- wait until the transmitter is empty
1977 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1979 struct mxser_port *info = tty->driver_data;
1980 unsigned long orig_jiffies, char_time;
1983 if (info->type == PORT_UNKNOWN)
1986 if (info->xmit_fifo_size == 0)
1987 return; /* Just in case.... */
1989 orig_jiffies = jiffies;
1991 * Set the check interval to be 1/5 of the estimated time to
1992 * send a single character, and make it at least 1. The check
1993 * interval should also be less than the timeout.
1995 * Note: we have to use pretty tight timings here to satisfy
1998 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1999 char_time = char_time / 5;
2002 if (timeout && timeout < char_time)
2003 char_time = timeout;
2005 * If the transmitter hasn't cleared in twice the approximate
2006 * amount of time to send the entire FIFO, it probably won't
2007 * ever clear. This assumes the UART isn't doing flow
2008 * control, which is currently the case. Hence, if it ever
2009 * takes longer than info->timeout, this is probably due to a
2010 * UART bug of some kind. So, we clamp the timeout parameter at
2013 if (!timeout || timeout > 2 * info->timeout)
2014 timeout = 2 * info->timeout;
2015 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2016 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2017 timeout, char_time);
2018 printk("jiff=%lu...", jiffies);
2020 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
2021 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2022 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2024 schedule_timeout_interruptible(char_time);
2025 if (signal_pending(current))
2027 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2030 set_current_state(TASK_RUNNING);
2032 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2033 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2038 * This routine is called by tty_hangup() when a hangup is signaled.
2040 static void mxser_hangup(struct tty_struct *tty)
2042 struct mxser_port *info = tty->driver_data;
2044 mxser_flush_buffer(tty);
2045 mxser_shutdown(info);
2048 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2050 wake_up_interruptible(&info->open_wait);
2054 * mxser_rs_break() --- routine which turns the break handling on or off
2056 static void mxser_rs_break(struct tty_struct *tty, int break_state)
2058 struct mxser_port *info = tty->driver_data;
2059 unsigned long flags;
2061 spin_lock_irqsave(&info->slock, flags);
2062 if (break_state == -1)
2063 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2064 info->ioaddr + UART_LCR);
2066 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2067 info->ioaddr + UART_LCR);
2068 spin_unlock_irqrestore(&info->slock, flags);
2071 static void mxser_receive_chars(struct mxser_port *port, int *status)
2073 struct tty_struct *tty = port->tty;
2074 unsigned char ch, gdl;
2080 recv_room = tty->receive_room;
2081 if ((recv_room == 0) && (!port->ldisc_stop_rx))
2084 if (port->board->chip_flag != MOXA_OTHER_UART) {
2086 if (*status & UART_LSR_SPECIAL)
2088 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
2089 (*status & MOXA_MUST_LSR_RERR))
2091 if (*status & MOXA_MUST_LSR_RERR)
2094 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
2096 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
2097 gdl &= MOXA_MUST_GDL_MASK;
2098 if (gdl >= recv_room) {
2099 if (!port->ldisc_stop_rx)
2103 ch = inb(port->ioaddr + UART_RX);
2104 tty_insert_flip_char(tty, ch, 0);
2115 ch = inb(port->ioaddr + UART_RX);
2116 if (port->board->chip_flag && (*status & UART_LSR_OE))
2117 outb(0x23, port->ioaddr + UART_FCR);
2118 *status &= port->read_status_mask;
2119 if (*status & port->ignore_status_mask) {
2120 if (++ignored > 100)
2124 if (*status & UART_LSR_SPECIAL) {
2125 if (*status & UART_LSR_BI) {
2129 if (port->flags & ASYNC_SAK)
2131 } else if (*status & UART_LSR_PE) {
2133 port->icount.parity++;
2134 } else if (*status & UART_LSR_FE) {
2136 port->icount.frame++;
2137 } else if (*status & UART_LSR_OE) {
2139 port->icount.overrun++;
2142 tty_insert_flip_char(tty, ch, flag);
2144 if (cnt >= recv_room) {
2145 if (!port->ldisc_stop_rx)
2152 if (port->board->chip_flag)
2155 *status = inb(port->ioaddr + UART_LSR);
2156 } while (*status & UART_LSR_DR);
2159 mxvar_log.rxcnt[port->tty->index] += cnt;
2160 port->mon_data.rxcnt += cnt;
2161 port->mon_data.up_rxcnt += cnt;
2163 tty_flip_buffer_push(tty);
2166 static void mxser_transmit_chars(struct mxser_port *port)
2171 outb(port->x_char, port->ioaddr + UART_TX);
2173 mxvar_log.txcnt[port->tty->index]++;
2174 port->mon_data.txcnt++;
2175 port->mon_data.up_txcnt++;
2180 if (port->xmit_buf == 0)
2183 if ((port->xmit_cnt <= 0) || port->tty->stopped ||
2184 (port->tty->hw_stopped &&
2185 (port->type != PORT_16550A) &&
2186 (!port->board->chip_flag))) {
2187 port->IER &= ~UART_IER_THRI;
2188 outb(port->IER, port->ioaddr + UART_IER);
2192 cnt = port->xmit_cnt;
2193 count = port->xmit_fifo_size;
2195 outb(port->xmit_buf[port->xmit_tail++],
2196 port->ioaddr + UART_TX);
2197 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2198 if (--port->xmit_cnt <= 0)
2200 } while (--count > 0);
2201 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
2203 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2204 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2205 port->icount.tx += (cnt - port->xmit_cnt);
2207 if (port->xmit_cnt < WAKEUP_CHARS)
2208 tty_wakeup(port->tty);
2210 if (port->xmit_cnt <= 0) {
2211 port->IER &= ~UART_IER_THRI;
2212 outb(port->IER, port->ioaddr + UART_IER);
2217 * This is the serial driver's generic interrupt routine
2219 static irqreturn_t mxser_interrupt(int irq, void *dev_id)
2222 struct mxser_board *brd = NULL;
2223 struct mxser_port *port;
2224 int max, irqbits, bits, msr;
2225 int pass_counter = 0;
2226 unsigned int int_cnt;
2227 int handled = IRQ_NONE;
2229 for (i = 0; i < MXSER_BOARDS; i++)
2230 if (dev_id == &mxser_boards[i]) {
2235 if (i == MXSER_BOARDS)
2239 max = brd->info->nports;
2241 irqbits = inb(brd->vector) & brd->vector_mask;
2242 if (irqbits == brd->vector_mask)
2245 handled = IRQ_HANDLED;
2246 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2247 if (irqbits == brd->vector_mask)
2251 port = &brd->ports[i];
2254 spin_lock(&port->slock);
2256 iir = inb(port->ioaddr + UART_IIR);
2257 if (iir & UART_IIR_NO_INT)
2259 iir &= MOXA_MUST_IIR_MASK;
2261 (port->flags & ASYNC_CLOSING) ||
2263 ASYNC_INITIALIZED)) {
2264 status = inb(port->ioaddr + UART_LSR);
2265 outb(0x27, port->ioaddr + UART_FCR);
2266 inb(port->ioaddr + UART_MSR);
2270 status = inb(port->ioaddr + UART_LSR);
2272 if (status & UART_LSR_PE)
2273 port->err_shadow |= NPPI_NOTIFY_PARITY;
2274 if (status & UART_LSR_FE)
2275 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2276 if (status & UART_LSR_OE)
2278 NPPI_NOTIFY_HW_OVERRUN;
2279 if (status & UART_LSR_BI)
2280 port->err_shadow |= NPPI_NOTIFY_BREAK;
2282 if (port->board->chip_flag) {
2283 if (iir == MOXA_MUST_IIR_GDA ||
2284 iir == MOXA_MUST_IIR_RDA ||
2285 iir == MOXA_MUST_IIR_RTO ||
2286 iir == MOXA_MUST_IIR_LSR)
2287 mxser_receive_chars(port,
2291 status &= port->read_status_mask;
2292 if (status & UART_LSR_DR)
2293 mxser_receive_chars(port,
2296 msr = inb(port->ioaddr + UART_MSR);
2297 if (msr & UART_MSR_ANY_DELTA)
2298 mxser_check_modem_status(port, msr);
2300 if (port->board->chip_flag) {
2301 if (iir == 0x02 && (status &
2303 mxser_transmit_chars(port);
2305 if (status & UART_LSR_THRE)
2306 mxser_transmit_chars(port);
2308 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2309 spin_unlock(&port->slock);
2311 if (pass_counter++ > MXSER_ISR_PASS_LIMIT)
2312 break; /* Prevent infinite loops */
2319 static const struct tty_operations mxser_ops = {
2321 .close = mxser_close,
2322 .write = mxser_write,
2323 .put_char = mxser_put_char,
2324 .flush_chars = mxser_flush_chars,
2325 .write_room = mxser_write_room,
2326 .chars_in_buffer = mxser_chars_in_buffer,
2327 .flush_buffer = mxser_flush_buffer,
2328 .ioctl = mxser_ioctl,
2329 .throttle = mxser_throttle,
2330 .unthrottle = mxser_unthrottle,
2331 .set_termios = mxser_set_termios,
2333 .start = mxser_start,
2334 .hangup = mxser_hangup,
2335 .break_ctl = mxser_rs_break,
2336 .wait_until_sent = mxser_wait_until_sent,
2337 .tiocmget = mxser_tiocmget,
2338 .tiocmset = mxser_tiocmset,
2342 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2345 static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
2349 free_irq(brd->irq, brd);
2350 if (pdev != NULL) { /* PCI */
2352 pci_release_region(pdev, 2);
2353 pci_release_region(pdev, 3);
2357 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2358 release_region(brd->vector, 1);
2362 static int __devinit mxser_initbrd(struct mxser_board *brd,
2363 struct pci_dev *pdev)
2365 struct mxser_port *info;
2369 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
2371 for (i = 0; i < brd->info->nports; i++) {
2372 info = &brd->ports[i];
2375 info->ldisc_stop_rx = 0;
2377 /* Enhance mode enabled here */
2378 if (brd->chip_flag != MOXA_OTHER_UART)
2379 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
2381 info->flags = ASYNC_SHARE_IRQ;
2382 info->type = brd->uart_type;
2384 process_txrx_fifo(info);
2386 info->custom_divisor = info->baud_base * 16;
2387 info->close_delay = 5 * HZ / 10;
2388 info->closing_wait = 30 * HZ;
2389 info->normal_termios = mxvar_sdriver->init_termios;
2390 init_waitqueue_head(&info->open_wait);
2391 init_waitqueue_head(&info->delta_msr_wait);
2392 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2393 info->err_shadow = 0;
2394 spin_lock_init(&info->slock);
2396 /* before set INT ISR, disable all int */
2397 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2398 info->ioaddr + UART_IER);
2401 retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
2404 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2405 "conflict with another device.\n",
2406 brd->info->name, brd->irq);
2407 /* We hold resources, we need to release them. */
2408 mxser_release_res(brd, pdev, 0);
2414 static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
2417 unsigned short regs[16], irq;
2418 unsigned char scratch, scratch2;
2420 brd->chip_flag = MOXA_OTHER_UART;
2422 id = mxser_read_register(cap, regs);
2425 brd->info = &mxser_cards[0];
2428 brd->info = &mxser_cards[1];
2430 case CI104J_ASIC_ID:
2431 brd->info = &mxser_cards[2];
2434 brd->info = &mxser_cards[5];
2437 brd->info = &mxser_cards[6];
2440 brd->info = &mxser_cards[7];
2447 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2448 Flag-hack checks if configuration should be read as 2-port here. */
2449 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
2450 irq = regs[9] & 0xF000;
2451 irq = irq | (irq >> 4);
2452 if (irq != (regs[9] & 0xFF00))
2453 return MXSER_ERR_IRQ_CONFLIT;
2454 } else if (brd->info->nports == 4) {
2455 irq = regs[9] & 0xF000;
2456 irq = irq | (irq >> 4);
2457 irq = irq | (irq >> 8);
2459 return MXSER_ERR_IRQ_CONFLIT;
2460 } else if (brd->info->nports == 8) {
2461 irq = regs[9] & 0xF000;
2462 irq = irq | (irq >> 4);
2463 irq = irq | (irq >> 8);
2464 if ((irq != regs[9]) || (irq != regs[10]))
2465 return MXSER_ERR_IRQ_CONFLIT;
2469 return MXSER_ERR_IRQ;
2470 brd->irq = ((int)(irq & 0xF000) >> 12);
2471 for (i = 0; i < 8; i++)
2472 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
2473 if ((regs[12] & 0x80) == 0)
2474 return MXSER_ERR_VECTOR;
2475 brd->vector = (int)regs[11]; /* interrupt vector */
2477 brd->vector_mask = 0x00FF;
2479 brd->vector_mask = 0x000F;
2480 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2481 if (regs[12] & bits) {
2482 brd->ports[i].baud_base = 921600;
2483 brd->ports[i].max_baud = 921600;
2485 brd->ports[i].baud_base = 115200;
2486 brd->ports[i].max_baud = 115200;
2489 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2490 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2491 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2492 outb(scratch2, cap + UART_LCR);
2493 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2494 scratch = inb(cap + UART_IIR);
2497 brd->uart_type = PORT_16550A;
2499 brd->uart_type = PORT_16450;
2500 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2502 return MXSER_ERR_IOADDR;
2503 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2504 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2505 return MXSER_ERR_VECTOR;
2507 return brd->info->nports;
2510 static int __devinit mxser_probe(struct pci_dev *pdev,
2511 const struct pci_device_id *ent)
2514 struct mxser_board *brd;
2516 unsigned long ioaddress;
2517 int retval = -EINVAL;
2519 for (i = 0; i < MXSER_BOARDS; i++)
2520 if (mxser_boards[i].info == NULL)
2523 if (i >= MXSER_BOARDS) {
2524 printk(KERN_ERR "Too many Smartio/Industio family boards found "
2525 "(maximum %d), board not configured\n", MXSER_BOARDS);
2529 brd = &mxser_boards[i];
2530 brd->idx = i * MXSER_PORTS_PER_BOARD;
2531 printk(KERN_INFO "Found MOXA %s board (BusNo=%d, DevNo=%d)\n",
2532 mxser_cards[ent->driver_data].name,
2533 pdev->bus->number, PCI_SLOT(pdev->devfn));
2535 retval = pci_enable_device(pdev);
2537 printk(KERN_ERR "Moxa SmartI/O PCI enable fail !\n");
2542 ioaddress = pci_resource_start(pdev, 2);
2543 retval = pci_request_region(pdev, 2, "mxser(IO)");
2547 brd->info = &mxser_cards[ent->driver_data];
2548 for (i = 0; i < brd->info->nports; i++)
2549 brd->ports[i].ioaddr = ioaddress + 8 * i;
2552 ioaddress = pci_resource_start(pdev, 3);
2553 retval = pci_request_region(pdev, 3, "mxser(vector)");
2556 brd->vector = ioaddress;
2559 brd->irq = pdev->irq;
2561 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2562 brd->uart_type = PORT_16550A;
2563 brd->vector_mask = 0;
2565 for (i = 0; i < brd->info->nports; i++) {
2566 for (j = 0; j < UART_INFO_NUM; j++) {
2567 if (Gpci_uart_info[j].type == brd->chip_flag) {
2568 brd->ports[i].max_baud =
2569 Gpci_uart_info[j].max_baud;
2571 /* exception....CP-102 */
2572 if (brd->info->flags & MXSER_HIGHBAUD)
2573 brd->ports[i].max_baud = 921600;
2579 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2580 for (i = 0; i < brd->info->nports; i++) {
2582 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2584 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
2586 outb(0, ioaddress + 4); /* default set to RS232 mode */
2587 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
2590 for (i = 0; i < brd->info->nports; i++) {
2591 brd->vector_mask |= (1 << i);
2592 brd->ports[i].baud_base = 921600;
2595 /* mxser_initbrd will hook ISR. */
2596 if (mxser_initbrd(brd, pdev) < 0)
2599 for (i = 0; i < brd->info->nports; i++)
2600 tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
2602 pci_set_drvdata(pdev, brd);
2606 pci_release_region(pdev, 3);
2608 pci_release_region(pdev, 2);
2617 static void __devexit mxser_remove(struct pci_dev *pdev)
2619 struct mxser_board *brd = pci_get_drvdata(pdev);
2622 for (i = 0; i < brd->info->nports; i++)
2623 tty_unregister_device(mxvar_sdriver, brd->idx + i);
2625 mxser_release_res(brd, pdev, 1);
2628 static struct pci_driver mxser_driver = {
2630 .id_table = mxser_pcibrds,
2631 .probe = mxser_probe,
2632 .remove = __devexit_p(mxser_remove)
2635 static int __init mxser_module_init(void)
2637 struct mxser_board *brd;
2639 unsigned int i, m, isaloop;
2642 pr_debug("Loading module mxser ...\n");
2644 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2648 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2651 /* Initialize the tty_driver structure */
2652 mxvar_sdriver->owner = THIS_MODULE;
2653 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2654 mxvar_sdriver->name = "ttyMI";
2655 mxvar_sdriver->major = ttymajor;
2656 mxvar_sdriver->minor_start = 0;
2657 mxvar_sdriver->num = MXSER_PORTS + 1;
2658 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2659 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2660 mxvar_sdriver->init_termios = tty_std_termios;
2661 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2662 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2663 tty_set_operations(mxvar_sdriver, &mxser_ops);
2665 retval = tty_register_driver(mxvar_sdriver);
2667 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2675 /* Start finding ISA boards here */
2676 for (isaloop = 0; isaloop < 2; isaloop++)
2677 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2679 cap = mxserBoardCAP[b]; /* predefined */
2681 cap = ioaddr[b]; /* module param */
2686 brd = &mxser_boards[m];
2687 retval = mxser_get_ISA_conf(cap, brd);
2690 printk(KERN_INFO "Found MOXA %s board "
2692 brd->info->name, ioaddr[b]);
2695 if (retval == MXSER_ERR_IRQ)
2696 printk(KERN_ERR "Invalid interrupt "
2697 "number, board not "
2699 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2700 printk(KERN_ERR "Invalid interrupt "
2701 "number, board not "
2703 else if (retval == MXSER_ERR_VECTOR)
2704 printk(KERN_ERR "Invalid interrupt "
2705 "vector, board not "
2707 else if (retval == MXSER_ERR_IOADDR)
2708 printk(KERN_ERR "Invalid I/O address, "
2709 "board not configured\n");
2715 /* mxser_initbrd will hook ISR. */
2716 if (mxser_initbrd(brd, NULL) < 0) {
2721 brd->idx = m * MXSER_PORTS_PER_BOARD;
2722 for (i = 0; i < brd->info->nports; i++)
2723 tty_register_device(mxvar_sdriver, brd->idx + i,
2729 retval = pci_register_driver(&mxser_driver);
2731 printk(KERN_ERR "Can't register pci driver\n");
2735 } /* else: we have some ISA cards under control */
2738 pr_debug("Done.\n");
2742 tty_unregister_driver(mxvar_sdriver);
2744 put_tty_driver(mxvar_sdriver);
2748 static void __exit mxser_module_exit(void)
2752 pr_debug("Unloading module mxser ...\n");
2754 pci_unregister_driver(&mxser_driver);
2756 for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
2757 if (mxser_boards[i].info != NULL)
2758 for (j = 0; j < mxser_boards[i].info->nports; j++)
2759 tty_unregister_device(mxvar_sdriver,
2760 mxser_boards[i].idx + j);
2761 tty_unregister_driver(mxvar_sdriver);
2762 put_tty_driver(mxvar_sdriver);
2764 for (i = 0; i < MXSER_BOARDS; i++)
2765 if (mxser_boards[i].info != NULL)
2766 mxser_release_res(&mxser_boards[i], NULL, 1);
2768 pr_debug("Done.\n");
2771 module_init(mxser_module_init);
2772 module_exit(mxser_module_exit);