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_EVENT_TXLOW 1
58 #define MXSER_BOARDS 4 /* Max. boards */
59 #define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
60 #define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
61 #define MXSER_ISR_PASS_LIMIT 99999L
63 #define MXSER_ERR_IOADDR -1
64 #define MXSER_ERR_IRQ -2
65 #define MXSER_ERR_IRQ_CONFLIT -3
66 #define MXSER_ERR_VECTOR -4
68 #define WAKEUP_CHARS 256
70 #define UART_MCR_AFE 0x20
71 #define UART_LSR_SPECIAL 0x1E
73 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|\
76 #define C168_ASIC_ID 1
77 #define C104_ASIC_ID 2
78 #define C102_ASIC_ID 0xB
79 #define CI132_ASIC_ID 4
80 #define CI134_ASIC_ID 3
81 #define CI104J_ASIC_ID 5
83 #define MXSER_HIGHBAUD 1
86 /* This is only for PCI */
96 } Gpci_uart_info[] = {
97 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
98 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
99 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
101 #define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
103 struct mxser_cardinfo {
109 static const struct mxser_cardinfo mxser_cards[] = {
110 { 8, "C168 series", }, /* C168-ISA */
111 { 4, "C104 series", }, /* C104-ISA */
112 { 4, "CI-104J series", }, /* CI104J */
113 { 8, "C168H/PCI series", }, /* C168-PCI */
114 { 4, "C104H/PCI series", }, /* C104-PCI */
115 { 4, "C102 series", MXSER_HAS2 }, /* C102-ISA */
116 { 4, "CI-132 series", MXSER_HAS2 }, /* CI132 */
117 { 4, "CI-134 series", }, /* CI134 */
118 { 2, "CP-132 series", }, /* CP132 */
119 { 4, "CP-114 series", }, /* CP114 */
120 { 4, "CT-114 series", }, /* CT114 */
121 { 2, "CP-102 series", MXSER_HIGHBAUD }, /* CP102 */
122 { 4, "CP-104U series", }, /* CP104U */
123 { 8, "CP-168U series", }, /* CP168U */
124 { 2, "CP-132U series", }, /* CP132U */
125 { 4, "CP-134U series", }, /* CP134U */
126 { 4, "CP-104JU series", }, /* CP104JU */
127 { 8, "Moxa UC7000 Serial", }, /* RC7000 */
128 { 8, "CP-118U series", }, /* CP118U */
129 { 2, "CP-102UL series", }, /* CP102UL */
130 { 2, "CP-102U series", }, /* CP102U */
131 { 8, "CP-118EL series", }, /* CP118EL */
132 { 8, "CP-168EL series", }, /* CP168EL */
133 { 4, "CP-104EL series", } /* CP104EL */
136 /* driver_data correspond to the lines in the structure above
137 see also ISA probe function before you change something */
138 static struct pci_device_id mxser_pcibrds[] = {
139 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C168),
141 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C104),
143 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132),
145 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP114),
147 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CT114),
149 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102),
151 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104U),
153 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168U),
155 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132U),
157 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP134U),
159 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104JU),
161 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_RC7000),
163 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118U),
165 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102UL),
167 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102U),
169 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118EL),
171 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168EL),
173 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104EL),
177 MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
179 static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
180 static int ttymajor = MXSERMAJOR;
181 static int calloutmajor = MXSERCUMAJOR;
183 /* Variables for insmod */
185 MODULE_AUTHOR("Casper Yang");
186 MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
187 module_param_array(ioaddr, int, NULL, 0);
188 module_param(ttymajor, int, 0);
189 MODULE_LICENSE("GPL");
193 unsigned long rxcnt[MXSER_PORTS];
194 unsigned long txcnt[MXSER_PORTS];
201 unsigned long up_rxcnt;
202 unsigned long up_txcnt;
204 unsigned char hold_reason;
207 struct mxser_mon_ext {
208 unsigned long rx_cnt[32];
209 unsigned long tx_cnt[32];
210 unsigned long up_rxcnt[32];
211 unsigned long up_txcnt[32];
212 int modem_status[32];
226 struct mxser_board *board;
227 struct tty_struct *tty;
229 unsigned long ioaddr;
230 unsigned long opmode_ioaddr;
234 int rx_trigger; /* Rx fifo trigger level */
236 int baud_base; /* max. speed */
238 int type; /* UART type */
239 int flags; /* defined in tty.h */
241 int x_char; /* xon/xoff character */
242 int IER; /* Interrupt Enable Register */
243 int MCR; /* Modem control register */
245 unsigned char stop_rx;
246 unsigned char ldisc_stop_rx;
250 unsigned short closing_wait;
251 unsigned char err_shadow;
254 int count; /* # of fd on device */
255 int blocked_open; /* # of blocked opens */
256 struct async_icount icount; /* kernel counters for 4 input interrupts */
259 int read_status_mask;
260 int ignore_status_mask;
262 unsigned char *xmit_buf;
267 struct ktermios normal_termios;
269 struct mxser_mon mon_data;
272 struct work_struct tqueue;
273 wait_queue_head_t open_wait;
274 wait_queue_head_t delta_msr_wait;
280 const struct mxser_cardinfo *info;
281 unsigned long vector;
282 unsigned long vector_mask;
287 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
290 struct mxser_mstatus {
298 static struct mxser_mstatus GMStatus[MXSER_PORTS];
300 static int mxserBoardCAP[MXSER_BOARDS] = {
302 /* 0x180, 0x280, 0x200, 0x320 */
305 static struct mxser_board mxser_boards[MXSER_BOARDS];
306 static struct tty_driver *mxvar_sdriver;
307 static struct mxser_log mxvar_log;
308 static int mxvar_diagflag;
309 static unsigned char mxser_msr[MXSER_PORTS + 1];
310 static struct mxser_mon_ext mon_data_ext;
311 static int mxser_set_baud_method[MXSER_PORTS + 1];
314 static int __devinit CheckIsMoxaMust(int io)
319 outb(0, io + UART_LCR);
320 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
321 oldmcr = inb(io + UART_MCR);
322 outb(0, io + UART_MCR);
323 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
324 if ((hwid = inb(io + UART_MCR)) != 0) {
325 outb(oldmcr, io + UART_MCR);
326 return MOXA_OTHER_UART;
329 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
330 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
331 if (hwid == Gpci_uart_info[i].type)
334 return MOXA_OTHER_UART;
338 static void process_txrx_fifo(struct mxser_port *info)
342 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
343 info->rx_trigger = 1;
344 info->rx_high_water = 1;
345 info->rx_low_water = 1;
346 info->xmit_fifo_size = 1;
348 for (i = 0; i < UART_INFO_NUM; i++)
349 if (info->board->chip_flag == Gpci_uart_info[i].type) {
350 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
351 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
352 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
353 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
358 static void mxser_do_softint(struct work_struct *work)
360 struct mxser_port *info = container_of(work, struct mxser_port, tqueue);
361 struct tty_struct *tty = info->tty;
363 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
367 static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
369 unsigned char status = 0;
371 status = inb(baseaddr + UART_MSR);
373 mxser_msr[port] &= 0x0F;
374 mxser_msr[port] |= status;
375 status = mxser_msr[port];
382 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
383 struct mxser_port *port)
385 DECLARE_WAITQUEUE(wait, current);
391 * If non-blocking mode is set, or the port is not enabled,
392 * then make the check up front and then exit.
394 if ((filp->f_flags & O_NONBLOCK) ||
395 test_bit(TTY_IO_ERROR, &tty->flags)) {
396 port->flags |= ASYNC_NORMAL_ACTIVE;
400 if (tty->termios->c_cflag & CLOCAL)
404 * Block waiting for the carrier detect and the line to become
405 * free (i.e., not in use by the callout). While we are in
406 * this loop, port->count is dropped by one, so that
407 * mxser_close() knows when to free things. We restore it upon
408 * exit, either normal or abnormal.
411 add_wait_queue(&port->open_wait, &wait);
413 spin_lock_irqsave(&port->slock, flags);
414 if (!tty_hung_up_p(filp))
416 spin_unlock_irqrestore(&port->slock, flags);
417 port->blocked_open++;
419 spin_lock_irqsave(&port->slock, flags);
420 outb(inb(port->ioaddr + UART_MCR) |
421 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
422 spin_unlock_irqrestore(&port->slock, flags);
423 set_current_state(TASK_INTERRUPTIBLE);
424 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
425 if (port->flags & ASYNC_HUP_NOTIFY)
428 retval = -ERESTARTSYS;
431 if (!(port->flags & ASYNC_CLOSING) &&
433 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
435 if (signal_pending(current)) {
436 retval = -ERESTARTSYS;
441 set_current_state(TASK_RUNNING);
442 remove_wait_queue(&port->open_wait, &wait);
443 if (!tty_hung_up_p(filp))
445 port->blocked_open--;
448 port->flags |= ASYNC_NORMAL_ACTIVE;
452 static int mxser_set_baud(struct mxser_port *info, long newspd)
459 if (!info->tty || !info->tty->termios)
465 if (newspd > info->max_baud)
468 info->realbaud = newspd;
470 quot = (2 * info->baud_base / 269);
472 quot = info->baud_base / newspd;
479 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
480 info->timeout += HZ / 50; /* Add .02 seconds of slop */
483 spin_lock_irqsave(&info->slock, flags);
484 info->MCR |= UART_MCR_DTR;
485 outb(info->MCR, info->ioaddr + UART_MCR);
486 spin_unlock_irqrestore(&info->slock, flags);
488 spin_lock_irqsave(&info->slock, flags);
489 info->MCR &= ~UART_MCR_DTR;
490 outb(info->MCR, info->ioaddr + UART_MCR);
491 spin_unlock_irqrestore(&info->slock, flags);
495 cval = inb(info->ioaddr + UART_LCR);
497 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
499 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
500 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
501 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
508 * This routine is called to set the UART divisor registers to match
509 * the specified baud rate for a serial port.
511 static int mxser_change_speed(struct mxser_port *info,
512 struct ktermios *old_termios)
514 unsigned cflag, cval, fcr;
516 unsigned char status;
520 if (!info->tty || !info->tty->termios)
522 cflag = info->tty->termios->c_cflag;
526 if (mxser_set_baud_method[info->tty->index] == 0) {
527 baud = tty_get_baud_rate(info->tty);
528 mxser_set_baud(info, baud);
531 /* byte size and parity */
532 switch (cflag & CSIZE) {
547 break; /* too keep GCC shut... */
552 cval |= UART_LCR_PARITY;
553 if (!(cflag & PARODD))
554 cval |= UART_LCR_EPAR;
556 cval |= UART_LCR_SPAR;
558 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
559 if (info->board->chip_flag) {
560 fcr = UART_FCR_ENABLE_FIFO;
561 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
562 SET_MOXA_MUST_FIFO_VALUE(info);
566 fcr = UART_FCR_ENABLE_FIFO;
567 if (info->board->chip_flag) {
568 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
569 SET_MOXA_MUST_FIFO_VALUE(info);
571 switch (info->rx_trigger) {
573 fcr |= UART_FCR_TRIGGER_1;
576 fcr |= UART_FCR_TRIGGER_4;
579 fcr |= UART_FCR_TRIGGER_8;
582 fcr |= UART_FCR_TRIGGER_14;
588 /* CTS flow control flag and modem status interrupts */
589 info->IER &= ~UART_IER_MSI;
590 info->MCR &= ~UART_MCR_AFE;
591 if (cflag & CRTSCTS) {
592 info->flags |= ASYNC_CTS_FLOW;
593 info->IER |= UART_IER_MSI;
594 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
595 info->MCR |= UART_MCR_AFE;
597 status = inb(info->ioaddr + UART_MSR);
598 if (info->tty->hw_stopped) {
599 if (status & UART_MSR_CTS) {
600 info->tty->hw_stopped = 0;
601 if (info->type != PORT_16550A &&
602 !info->board->chip_flag) {
603 outb(info->IER & ~UART_IER_THRI,
606 info->IER |= UART_IER_THRI;
607 outb(info->IER, info->ioaddr +
610 set_bit(MXSER_EVENT_TXLOW, &info->event);
611 schedule_work(&info->tqueue); }
613 if (!(status & UART_MSR_CTS)) {
614 info->tty->hw_stopped = 1;
615 if ((info->type != PORT_16550A) &&
616 (!info->board->chip_flag)) {
617 info->IER &= ~UART_IER_THRI;
618 outb(info->IER, info->ioaddr +
625 info->flags &= ~ASYNC_CTS_FLOW;
627 outb(info->MCR, info->ioaddr + UART_MCR);
628 if (cflag & CLOCAL) {
629 info->flags &= ~ASYNC_CHECK_CD;
631 info->flags |= ASYNC_CHECK_CD;
632 info->IER |= UART_IER_MSI;
634 outb(info->IER, info->ioaddr + UART_IER);
637 * Set up parity check flag
639 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
640 if (I_INPCK(info->tty))
641 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
642 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
643 info->read_status_mask |= UART_LSR_BI;
645 info->ignore_status_mask = 0;
647 if (I_IGNBRK(info->tty)) {
648 info->ignore_status_mask |= UART_LSR_BI;
649 info->read_status_mask |= UART_LSR_BI;
651 * If we're ignore parity and break indicators, ignore
652 * overruns too. (For real raw support).
654 if (I_IGNPAR(info->tty)) {
655 info->ignore_status_mask |=
659 info->read_status_mask |=
665 if (info->board->chip_flag) {
666 spin_lock_irqsave(&info->slock, flags);
667 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
668 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
669 if (I_IXON(info->tty)) {
670 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
672 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
674 if (I_IXOFF(info->tty)) {
675 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
677 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
679 spin_unlock_irqrestore(&info->slock, flags);
683 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
684 outb(cval, info->ioaddr + UART_LCR);
689 static void mxser_check_modem_status(struct mxser_port *port, int status)
691 /* update input line counters */
692 if (status & UART_MSR_TERI)
694 if (status & UART_MSR_DDSR)
696 if (status & UART_MSR_DDCD)
698 if (status & UART_MSR_DCTS)
700 port->mon_data.modem_status = status;
701 wake_up_interruptible(&port->delta_msr_wait);
703 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
704 if (status & UART_MSR_DCD)
705 wake_up_interruptible(&port->open_wait);
706 schedule_work(&port->tqueue);
709 if (port->flags & ASYNC_CTS_FLOW) {
710 if (port->tty->hw_stopped) {
711 if (status & UART_MSR_CTS) {
712 port->tty->hw_stopped = 0;
714 if ((port->type != PORT_16550A) &&
715 (!port->board->chip_flag)) {
716 outb(port->IER & ~UART_IER_THRI,
717 port->ioaddr + UART_IER);
718 port->IER |= UART_IER_THRI;
719 outb(port->IER, port->ioaddr +
722 set_bit(MXSER_EVENT_TXLOW, &port->event);
723 schedule_work(&port->tqueue);
726 if (!(status & UART_MSR_CTS)) {
727 port->tty->hw_stopped = 1;
728 if (port->type != PORT_16550A &&
729 !port->board->chip_flag) {
730 port->IER &= ~UART_IER_THRI;
731 outb(port->IER, port->ioaddr +
739 static int mxser_startup(struct mxser_port *info)
744 page = __get_free_page(GFP_KERNEL);
748 spin_lock_irqsave(&info->slock, flags);
750 if (info->flags & ASYNC_INITIALIZED) {
752 spin_unlock_irqrestore(&info->slock, flags);
756 if (!info->ioaddr || !info->type) {
758 set_bit(TTY_IO_ERROR, &info->tty->flags);
760 spin_unlock_irqrestore(&info->slock, flags);
766 info->xmit_buf = (unsigned char *) page;
769 * Clear the FIFO buffers and disable them
770 * (they will be reenabled in mxser_change_speed())
772 if (info->board->chip_flag)
773 outb((UART_FCR_CLEAR_RCVR |
774 UART_FCR_CLEAR_XMIT |
775 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
777 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
778 info->ioaddr + UART_FCR);
781 * At this point there's no way the LSR could still be 0xFF;
782 * if it is, then bail out, because there's likely no UART
785 if (inb(info->ioaddr + UART_LSR) == 0xff) {
786 spin_unlock_irqrestore(&info->slock, flags);
787 if (capable(CAP_SYS_ADMIN)) {
789 set_bit(TTY_IO_ERROR, &info->tty->flags);
796 * Clear the interrupt registers.
798 (void) inb(info->ioaddr + UART_LSR);
799 (void) inb(info->ioaddr + UART_RX);
800 (void) inb(info->ioaddr + UART_IIR);
801 (void) inb(info->ioaddr + UART_MSR);
804 * Now, initialize the UART
806 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
807 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
808 outb(info->MCR, info->ioaddr + UART_MCR);
811 * Finally, enable interrupts
813 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
815 if (info->board->chip_flag)
816 info->IER |= MOXA_MUST_IER_EGDAI;
817 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
820 * And clear the interrupt registers again for luck.
822 (void) inb(info->ioaddr + UART_LSR);
823 (void) inb(info->ioaddr + UART_RX);
824 (void) inb(info->ioaddr + UART_IIR);
825 (void) inb(info->ioaddr + UART_MSR);
828 clear_bit(TTY_IO_ERROR, &info->tty->flags);
829 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
832 * and set the speed of the serial port
834 spin_unlock_irqrestore(&info->slock, flags);
835 mxser_change_speed(info, NULL);
837 info->flags |= ASYNC_INITIALIZED;
842 * This routine will shutdown a serial port; interrupts maybe disabled, and
843 * DTR is dropped if the hangup on close termio flag is on.
845 static void mxser_shutdown(struct mxser_port *info)
849 if (!(info->flags & ASYNC_INITIALIZED))
852 spin_lock_irqsave(&info->slock, flags);
855 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
856 * here so the queue might never be waken up
858 wake_up_interruptible(&info->delta_msr_wait);
861 * Free the IRQ, if necessary
863 if (info->xmit_buf) {
864 free_page((unsigned long) info->xmit_buf);
865 info->xmit_buf = NULL;
869 outb(0x00, info->ioaddr + UART_IER);
871 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
872 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
873 outb(info->MCR, info->ioaddr + UART_MCR);
875 /* clear Rx/Tx FIFO's */
876 if (info->board->chip_flag)
877 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
878 MOXA_MUST_FCR_GDA_MODE_ENABLE,
879 info->ioaddr + UART_FCR);
881 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
882 info->ioaddr + UART_FCR);
884 /* read data port to reset things */
885 (void) inb(info->ioaddr + UART_RX);
888 set_bit(TTY_IO_ERROR, &info->tty->flags);
890 info->flags &= ~ASYNC_INITIALIZED;
892 if (info->board->chip_flag)
893 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
895 spin_unlock_irqrestore(&info->slock, flags);
899 * This routine is called whenever a serial port is opened. It
900 * enables interrupts for a serial port, linking in its async structure into
901 * the IRQ chain. It also performs the serial-specific
902 * initialization for the tty structure.
904 static int mxser_open(struct tty_struct *tty, struct file *filp)
906 struct mxser_port *info;
909 /* initialize driver_data in case something fails */
910 tty->driver_data = NULL;
913 if (line == MXSER_PORTS)
915 if (line < 0 || line > MXSER_PORTS)
917 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
921 tty->driver_data = info;
924 * Start up serial port
927 retval = mxser_startup(info);
931 retval = mxser_block_til_ready(tty, filp, info);
935 /* unmark here for very high baud rate (ex. 921600 bps) used */
936 tty->low_latency = 1;
941 * This routine is called when the serial port gets closed. First, we
942 * wait for the last remaining data to be sent. Then, we unlink its
943 * async structure from the interrupt chain if necessary, and we free
944 * that IRQ if nothing is left in the chain.
946 static void mxser_close(struct tty_struct *tty, struct file *filp)
948 struct mxser_port *info = tty->driver_data;
950 unsigned long timeout;
953 if (tty->index == MXSER_PORTS)
958 spin_lock_irqsave(&info->slock, flags);
960 if (tty_hung_up_p(filp)) {
961 spin_unlock_irqrestore(&info->slock, flags);
964 if ((tty->count == 1) && (info->count != 1)) {
966 * Uh, oh. tty->count is 1, which means that the tty
967 * structure will be freed. Info->count should always
968 * be one in these conditions. If it's greater than
969 * one, we've got real problems, since it means the
970 * serial port won't be shutdown.
972 printk(KERN_ERR "mxser_close: bad serial port count; "
973 "tty->count is 1, info->count is %d\n", info->count);
976 if (--info->count < 0) {
977 printk(KERN_ERR "mxser_close: bad serial port count for "
978 "ttys%d: %d\n", tty->index, info->count);
982 spin_unlock_irqrestore(&info->slock, flags);
985 info->flags |= ASYNC_CLOSING;
986 spin_unlock_irqrestore(&info->slock, flags);
988 * Save the termios structure, since this port may have
989 * separate termios for callout and dialin.
991 if (info->flags & ASYNC_NORMAL_ACTIVE)
992 info->normal_termios = *tty->termios;
994 * Now we wait for the transmit buffer to clear; and we notify
995 * the line discipline to only process XON/XOFF characters.
998 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
999 tty_wait_until_sent(tty, info->closing_wait);
1001 * At this point we stop accepting input. To do this, we
1002 * disable the receive line status interrupts, and tell the
1003 * interrupt driver to stop checking the data ready bit in the
1004 * line status register.
1006 info->IER &= ~UART_IER_RLSI;
1007 if (info->board->chip_flag)
1008 info->IER &= ~MOXA_MUST_RECV_ISR;
1010 if (info->flags & ASYNC_INITIALIZED) {
1011 outb(info->IER, info->ioaddr + UART_IER);
1013 * Before we drop DTR, make sure the UART transmitter
1014 * has completely drained; this is especially
1015 * important if there is a transmit FIFO!
1017 timeout = jiffies + HZ;
1018 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
1019 schedule_timeout_interruptible(5);
1020 if (time_after(jiffies, timeout))
1024 mxser_shutdown(info);
1026 if (tty->driver->flush_buffer)
1027 tty->driver->flush_buffer(tty);
1029 tty_ldisc_flush(tty);
1034 if (info->blocked_open) {
1035 if (info->close_delay)
1036 schedule_timeout_interruptible(info->close_delay);
1037 wake_up_interruptible(&info->open_wait);
1040 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1043 static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1046 struct mxser_port *info = tty->driver_data;
1047 unsigned long flags;
1049 if (!info->xmit_buf)
1053 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1054 SERIAL_XMIT_SIZE - info->xmit_head));
1058 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1059 spin_lock_irqsave(&info->slock, flags);
1060 info->xmit_head = (info->xmit_head + c) &
1061 (SERIAL_XMIT_SIZE - 1);
1062 info->xmit_cnt += c;
1063 spin_unlock_irqrestore(&info->slock, flags);
1070 if (info->xmit_cnt && !tty->stopped) {
1071 if (!tty->hw_stopped ||
1072 (info->type == PORT_16550A) ||
1073 (info->board->chip_flag)) {
1074 spin_lock_irqsave(&info->slock, flags);
1075 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1077 info->IER |= UART_IER_THRI;
1078 outb(info->IER, info->ioaddr + UART_IER);
1079 spin_unlock_irqrestore(&info->slock, flags);
1085 static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1087 struct mxser_port *info = tty->driver_data;
1088 unsigned long flags;
1090 if (!info->xmit_buf)
1093 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1096 spin_lock_irqsave(&info->slock, flags);
1097 info->xmit_buf[info->xmit_head++] = ch;
1098 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1100 spin_unlock_irqrestore(&info->slock, flags);
1101 if (!tty->stopped) {
1102 if (!tty->hw_stopped ||
1103 (info->type == PORT_16550A) ||
1104 info->board->chip_flag) {
1105 spin_lock_irqsave(&info->slock, flags);
1106 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1107 info->IER |= UART_IER_THRI;
1108 outb(info->IER, info->ioaddr + UART_IER);
1109 spin_unlock_irqrestore(&info->slock, flags);
1115 static void mxser_flush_chars(struct tty_struct *tty)
1117 struct mxser_port *info = tty->driver_data;
1118 unsigned long flags;
1120 if (info->xmit_cnt <= 0 ||
1124 (info->type != PORT_16550A) &&
1125 (!info->board->chip_flag)
1129 spin_lock_irqsave(&info->slock, flags);
1131 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1132 info->IER |= UART_IER_THRI;
1133 outb(info->IER, info->ioaddr + UART_IER);
1135 spin_unlock_irqrestore(&info->slock, flags);
1138 static int mxser_write_room(struct tty_struct *tty)
1140 struct mxser_port *info = tty->driver_data;
1143 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1149 static int mxser_chars_in_buffer(struct tty_struct *tty)
1151 struct mxser_port *info = tty->driver_data;
1152 return info->xmit_cnt;
1155 static void mxser_flush_buffer(struct tty_struct *tty)
1157 struct mxser_port *info = tty->driver_data;
1159 unsigned long flags;
1162 spin_lock_irqsave(&info->slock, flags);
1163 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1165 fcr = inb(info->ioaddr + UART_FCR);
1166 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1167 info->ioaddr + UART_FCR);
1168 outb(fcr, info->ioaddr + UART_FCR);
1170 spin_unlock_irqrestore(&info->slock, flags);
1176 * ------------------------------------------------------------
1177 * friends of mxser_ioctl()
1178 * ------------------------------------------------------------
1180 static int mxser_get_serial_info(struct mxser_port *info,
1181 struct serial_struct __user *retinfo)
1183 struct serial_struct tmp;
1187 memset(&tmp, 0, sizeof(tmp));
1188 tmp.type = info->type;
1189 tmp.line = info->tty->index;
1190 tmp.port = info->ioaddr;
1191 tmp.irq = info->board->irq;
1192 tmp.flags = info->flags;
1193 tmp.baud_base = info->baud_base;
1194 tmp.close_delay = info->close_delay;
1195 tmp.closing_wait = info->closing_wait;
1196 tmp.custom_divisor = info->custom_divisor;
1198 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1203 static int mxser_set_serial_info(struct mxser_port *info,
1204 struct serial_struct __user *new_info)
1206 struct serial_struct new_serial;
1210 if (!new_info || !info->ioaddr)
1212 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1215 if ((new_serial.irq != info->board->irq) ||
1216 (new_serial.port != info->ioaddr) ||
1217 (new_serial.custom_divisor != info->custom_divisor) ||
1218 (new_serial.baud_base != info->baud_base))
1221 flags = info->flags & ASYNC_SPD_MASK;
1223 if (!capable(CAP_SYS_ADMIN)) {
1224 if ((new_serial.baud_base != info->baud_base) ||
1225 (new_serial.close_delay != info->close_delay) ||
1226 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
1228 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1229 (new_serial.flags & ASYNC_USR_MASK));
1232 * OK, past this point, all the error checking has been done.
1233 * At this point, we start making changes.....
1235 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1236 (new_serial.flags & ASYNC_FLAGS));
1237 info->close_delay = new_serial.close_delay * HZ / 100;
1238 info->closing_wait = new_serial.closing_wait * HZ / 100;
1239 info->tty->low_latency =
1240 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1241 info->tty->low_latency = 0;
1244 info->type = new_serial.type;
1246 process_txrx_fifo(info);
1248 if (info->flags & ASYNC_INITIALIZED) {
1249 if (flags != (info->flags & ASYNC_SPD_MASK))
1250 mxser_change_speed(info, NULL);
1252 retval = mxser_startup(info);
1258 * mxser_get_lsr_info - get line status register info
1260 * Purpose: Let user call ioctl() to get info when the UART physically
1261 * is emptied. On bus types like RS485, the transmitter must
1262 * release the bus after transmitting. This must be done when
1263 * the transmit shift register is empty, not be done when the
1264 * transmit holding register is empty. This functionality
1265 * allows an RS485 driver to be written in user space.
1267 static int mxser_get_lsr_info(struct mxser_port *info,
1268 unsigned int __user *value)
1270 unsigned char status;
1271 unsigned int result;
1272 unsigned long flags;
1274 spin_lock_irqsave(&info->slock, flags);
1275 status = inb(info->ioaddr + UART_LSR);
1276 spin_unlock_irqrestore(&info->slock, flags);
1277 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1278 return put_user(result, value);
1282 * This routine sends a break character out the serial port.
1284 static void mxser_send_break(struct mxser_port *info, int duration)
1286 unsigned long flags;
1290 set_current_state(TASK_INTERRUPTIBLE);
1291 spin_lock_irqsave(&info->slock, flags);
1292 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1293 info->ioaddr + UART_LCR);
1294 spin_unlock_irqrestore(&info->slock, flags);
1295 schedule_timeout(duration);
1296 spin_lock_irqsave(&info->slock, flags);
1297 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1298 info->ioaddr + UART_LCR);
1299 spin_unlock_irqrestore(&info->slock, flags);
1302 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
1304 struct mxser_port *info = tty->driver_data;
1305 unsigned char control, status;
1306 unsigned long flags;
1309 if (tty->index == MXSER_PORTS)
1310 return -ENOIOCTLCMD;
1311 if (test_bit(TTY_IO_ERROR, &tty->flags))
1314 control = info->MCR;
1316 spin_lock_irqsave(&info->slock, flags);
1317 status = inb(info->ioaddr + UART_MSR);
1318 if (status & UART_MSR_ANY_DELTA)
1319 mxser_check_modem_status(info, status);
1320 spin_unlock_irqrestore(&info->slock, flags);
1321 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1322 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1323 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1324 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1325 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1326 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1329 static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1330 unsigned int set, unsigned int clear)
1332 struct mxser_port *info = tty->driver_data;
1333 unsigned long flags;
1336 if (tty->index == MXSER_PORTS)
1337 return -ENOIOCTLCMD;
1338 if (test_bit(TTY_IO_ERROR, &tty->flags))
1341 spin_lock_irqsave(&info->slock, flags);
1343 if (set & TIOCM_RTS)
1344 info->MCR |= UART_MCR_RTS;
1345 if (set & TIOCM_DTR)
1346 info->MCR |= UART_MCR_DTR;
1348 if (clear & TIOCM_RTS)
1349 info->MCR &= ~UART_MCR_RTS;
1350 if (clear & TIOCM_DTR)
1351 info->MCR &= ~UART_MCR_DTR;
1353 outb(info->MCR, info->ioaddr + UART_MCR);
1354 spin_unlock_irqrestore(&info->slock, flags);
1358 static int __init mxser_program_mode(int port)
1370 id = inb(port + 1) & 0x1F;
1371 if ((id != C168_ASIC_ID) &&
1372 (id != C104_ASIC_ID) &&
1373 (id != C102_ASIC_ID) &&
1374 (id != CI132_ASIC_ID) &&
1375 (id != CI134_ASIC_ID) &&
1376 (id != CI104J_ASIC_ID))
1378 for (i = 0, j = 0; i < 4; i++) {
1382 } else if ((j == 1) && (n == 1)) {
1393 static void __init mxser_normal_mode(int port)
1397 outb(0xA5, port + 1);
1398 outb(0x80, port + 3);
1399 outb(12, port + 0); /* 9600 bps */
1401 outb(0x03, port + 3); /* 8 data bits */
1402 outb(0x13, port + 4); /* loop back mode */
1403 for (i = 0; i < 16; i++) {
1405 if ((n & 0x61) == 0x60)
1410 outb(0x00, port + 4);
1413 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1414 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1415 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1416 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1417 #define EN_CCMD 0x000 /* Chip's command register */
1418 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1419 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1420 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1421 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1422 #define EN0_DCFG 0x00E /* Data configuration reg WR */
1423 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1424 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1425 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1426 static int __init mxser_read_register(int port, unsigned short *regs)
1428 int i, k, value, id;
1431 id = mxser_program_mode(port);
1434 for (i = 0; i < 14; i++) {
1435 k = (i & 0x3F) | 0x180;
1436 for (j = 0x100; j > 0; j >>= 1) {
1437 outb(CHIP_CS, port);
1439 outb(CHIP_CS | CHIP_DO, port);
1440 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1442 outb(CHIP_CS, port);
1443 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1448 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1449 outb(CHIP_CS, port);
1450 outb(CHIP_CS | CHIP_SK, port);
1451 if (inb(port) & CHIP_DI)
1457 mxser_normal_mode(port);
1461 static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1463 struct mxser_port *port;
1469 /* if (copy_to_user(argp, mxsercfg,
1470 sizeof(struct mxser_hwconf) * 4))
1474 case MOXA_GET_MAJOR:
1475 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1479 case MOXA_GET_CUMAJOR:
1480 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1484 case MOXA_CHKPORTENABLE:
1487 for (i = 0; i < MXSER_BOARDS; i++)
1488 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1489 if (mxser_boards[i].ports[j].ioaddr)
1492 return put_user(result, (unsigned long __user *)argp);
1493 case MOXA_GETDATACOUNT:
1494 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1497 case MOXA_GETMSTATUS:
1498 for (i = 0; i < MXSER_BOARDS; i++)
1499 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1500 port = &mxser_boards[i].ports[j];
1503 if (!port->ioaddr) {
1504 GMStatus[i].dcd = 0;
1505 GMStatus[i].dsr = 0;
1506 GMStatus[i].cts = 0;
1510 if (!port->tty || !port->tty->termios)
1512 port->normal_termios.c_cflag;
1515 port->tty->termios->c_cflag;
1517 status = inb(port->ioaddr + UART_MSR);
1518 if (status & 0x80 /*UART_MSR_DCD */ )
1519 GMStatus[i].dcd = 1;
1521 GMStatus[i].dcd = 0;
1523 if (status & 0x20 /*UART_MSR_DSR */ )
1524 GMStatus[i].dsr = 1;
1526 GMStatus[i].dsr = 0;
1529 if (status & 0x10 /*UART_MSR_CTS */ )
1530 GMStatus[i].cts = 1;
1532 GMStatus[i].cts = 0;
1534 if (copy_to_user(argp, GMStatus,
1535 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1538 case MOXA_ASPP_MON_EXT: {
1539 int status, p, shiftbit;
1540 unsigned long opmode;
1541 unsigned cflag, iflag;
1543 for (i = 0; i < MXSER_BOARDS; i++)
1544 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1545 port = &mxser_boards[i].ports[j];
1549 status = mxser_get_msr(port->ioaddr, 0, i);
1551 if (status & UART_MSR_TERI)
1553 if (status & UART_MSR_DDSR)
1555 if (status & UART_MSR_DDCD)
1557 if (status & UART_MSR_DCTS)
1560 port->mon_data.modem_status = status;
1561 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1562 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1563 mon_data_ext.up_rxcnt[i] =
1564 port->mon_data.up_rxcnt;
1565 mon_data_ext.up_txcnt[i] =
1566 port->mon_data.up_txcnt;
1567 mon_data_ext.modem_status[i] =
1568 port->mon_data.modem_status;
1569 mon_data_ext.baudrate[i] = port->realbaud;
1571 if (!port->tty || !port->tty->termios) {
1572 cflag = port->normal_termios.c_cflag;
1573 iflag = port->normal_termios.c_iflag;
1575 cflag = port->tty->termios->c_cflag;
1576 iflag = port->tty->termios->c_iflag;
1579 mon_data_ext.databits[i] = cflag & CSIZE;
1581 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1583 mon_data_ext.parity[i] =
1584 cflag & (PARENB | PARODD | CMSPAR);
1586 mon_data_ext.flowctrl[i] = 0x00;
1588 if (cflag & CRTSCTS)
1589 mon_data_ext.flowctrl[i] |= 0x03;
1591 if (iflag & (IXON | IXOFF))
1592 mon_data_ext.flowctrl[i] |= 0x0C;
1594 if (port->type == PORT_16550A)
1595 mon_data_ext.fifo[i] = 1;
1597 mon_data_ext.fifo[i] = 0;
1601 opmode = inb(port->opmode_ioaddr) >> shiftbit;
1602 opmode &= OP_MODE_MASK;
1604 mon_data_ext.iftype[i] = opmode;
1607 if (copy_to_user(argp, &mon_data_ext,
1608 sizeof(mon_data_ext)))
1614 return -ENOIOCTLCMD;
1619 static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1620 unsigned int cmd, unsigned long arg)
1622 struct mxser_port *info = tty->driver_data;
1623 struct async_icount cprev, cnow; /* kernel counter temps */
1624 struct serial_icounter_struct __user *p_cuser;
1625 unsigned long templ;
1626 unsigned long flags;
1627 void __user *argp = (void __user *)arg;
1630 if (tty->index == MXSER_PORTS)
1631 return mxser_ioctl_special(cmd, argp);
1633 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1635 unsigned long opmode;
1636 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1638 unsigned char val, mask;
1641 if (cmd == MOXA_SET_OP_MODE) {
1642 if (get_user(opmode, (int __user *) argp))
1644 if (opmode != RS232_MODE &&
1645 opmode != RS485_2WIRE_MODE &&
1646 opmode != RS422_MODE &&
1647 opmode != RS485_4WIRE_MODE)
1651 val = inb(info->opmode_ioaddr);
1653 val |= (opmode << shiftbit);
1654 outb(val, info->opmode_ioaddr);
1657 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1658 opmode &= OP_MODE_MASK;
1659 if (copy_to_user(argp, &opmode, sizeof(int)))
1665 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1666 test_bit(TTY_IO_ERROR, &tty->flags))
1670 case TCSBRK: /* SVID version: non-zero arg --> no break */
1671 retval = tty_check_change(tty);
1674 tty_wait_until_sent(tty, 0);
1676 mxser_send_break(info, HZ / 4); /* 1/4 second */
1678 case TCSBRKP: /* support for POSIX tcsendbreak() */
1679 retval = tty_check_change(tty);
1682 tty_wait_until_sent(tty, 0);
1683 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1686 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1688 if (get_user(templ, (unsigned long __user *) argp))
1691 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1694 return mxser_get_serial_info(info, argp);
1696 return mxser_set_serial_info(info, argp);
1697 case TIOCSERGETLSR: /* Get line status register */
1698 return mxser_get_lsr_info(info, argp);
1700 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1701 * - mask passed in arg for lines of interest
1702 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1703 * Caller should use TIOCGICOUNT to see which one it was
1706 DECLARE_WAITQUEUE(wait, current);
1708 spin_lock_irqsave(&info->slock, flags);
1709 cprev = info->icount; /* note the counters on entry */
1710 spin_unlock_irqrestore(&info->slock, flags);
1712 add_wait_queue(&info->delta_msr_wait, &wait);
1714 spin_lock_irqsave(&info->slock, flags);
1715 cnow = info->icount; /* atomic copy */
1716 spin_unlock_irqrestore(&info->slock, flags);
1718 set_current_state(TASK_INTERRUPTIBLE);
1719 if (((arg & TIOCM_RNG) &&
1720 (cnow.rng != cprev.rng)) ||
1721 ((arg & TIOCM_DSR) &&
1722 (cnow.dsr != cprev.dsr)) ||
1723 ((arg & TIOCM_CD) &&
1724 (cnow.dcd != cprev.dcd)) ||
1725 ((arg & TIOCM_CTS) &&
1726 (cnow.cts != cprev.cts))) {
1730 /* see if a signal did it */
1731 if (signal_pending(current)) {
1737 current->state = TASK_RUNNING;
1738 remove_wait_queue(&info->delta_msr_wait, &wait);
1743 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1744 * Return: write counters to the user passed counter struct
1745 * NB: both 1->0 and 0->1 transitions are counted except for
1746 * RI where only 0->1 is counted.
1749 spin_lock_irqsave(&info->slock, flags);
1750 cnow = info->icount;
1751 spin_unlock_irqrestore(&info->slock, flags);
1753 if (put_user(cnow.frame, &p_cuser->frame))
1755 if (put_user(cnow.brk, &p_cuser->brk))
1757 if (put_user(cnow.overrun, &p_cuser->overrun))
1759 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1761 if (put_user(cnow.parity, &p_cuser->parity))
1763 if (put_user(cnow.rx, &p_cuser->rx))
1765 if (put_user(cnow.tx, &p_cuser->tx))
1767 put_user(cnow.cts, &p_cuser->cts);
1768 put_user(cnow.dsr, &p_cuser->dsr);
1769 put_user(cnow.rng, &p_cuser->rng);
1770 put_user(cnow.dcd, &p_cuser->dcd);
1772 case MOXA_HighSpeedOn:
1773 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1774 case MOXA_SDS_RSTICOUNTER:
1775 info->mon_data.rxcnt = 0;
1776 info->mon_data.txcnt = 0;
1778 case MOXA_ASPP_SETBAUD:{
1780 if (get_user(baud, (long __user *)argp))
1782 mxser_set_baud(info, baud);
1785 case MOXA_ASPP_GETBAUD:
1786 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1791 case MOXA_ASPP_OQUEUE:{
1794 len = mxser_chars_in_buffer(tty);
1796 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1798 len += (lsr ? 0 : 1);
1800 if (copy_to_user(argp, &len, sizeof(int)))
1805 case MOXA_ASPP_MON: {
1808 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1809 mxser_check_modem_status(info, status);
1811 mcr = inb(info->ioaddr + UART_MCR);
1812 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1813 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1815 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1817 if (mcr & MOXA_MUST_MCR_TX_XON)
1818 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1820 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1822 if (info->tty->hw_stopped)
1823 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1825 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1827 if (copy_to_user(argp, &info->mon_data,
1828 sizeof(struct mxser_mon)))
1833 case MOXA_ASPP_LSTATUS: {
1834 if (copy_to_user(argp, &info->err_shadow,
1835 sizeof(unsigned char)))
1838 info->err_shadow = 0;
1841 case MOXA_SET_BAUD_METHOD: {
1844 if (get_user(method, (int __user *)argp))
1846 mxser_set_baud_method[tty->index] = method;
1847 if (copy_to_user(argp, &method, sizeof(int)))
1853 return -ENOIOCTLCMD;
1858 static void mxser_stoprx(struct tty_struct *tty)
1860 struct mxser_port *info = tty->driver_data;
1862 info->ldisc_stop_rx = 1;
1864 if (info->board->chip_flag) {
1865 info->IER &= ~MOXA_MUST_RECV_ISR;
1866 outb(info->IER, info->ioaddr + UART_IER);
1868 info->x_char = STOP_CHAR(tty);
1869 outb(0, info->ioaddr + UART_IER);
1870 info->IER |= UART_IER_THRI;
1871 outb(info->IER, info->ioaddr + UART_IER);
1875 if (info->tty->termios->c_cflag & CRTSCTS) {
1876 info->MCR &= ~UART_MCR_RTS;
1877 outb(info->MCR, info->ioaddr + UART_MCR);
1882 * This routine is called by the upper-layer tty layer to signal that
1883 * incoming characters should be throttled.
1885 static void mxser_throttle(struct tty_struct *tty)
1890 static void mxser_unthrottle(struct tty_struct *tty)
1892 struct mxser_port *info = tty->driver_data;
1895 info->ldisc_stop_rx = 0;
1900 if (info->board->chip_flag) {
1901 info->IER |= MOXA_MUST_RECV_ISR;
1902 outb(info->IER, info->ioaddr + UART_IER);
1904 info->x_char = START_CHAR(tty);
1905 outb(0, info->ioaddr + UART_IER);
1906 info->IER |= UART_IER_THRI;
1907 outb(info->IER, info->ioaddr + UART_IER);
1912 if (info->tty->termios->c_cflag & CRTSCTS) {
1913 info->MCR |= UART_MCR_RTS;
1914 outb(info->MCR, info->ioaddr + UART_MCR);
1919 * mxser_stop() and mxser_start()
1921 * This routines are called before setting or resetting tty->stopped.
1922 * They enable or disable transmitter interrupts, as necessary.
1924 static void mxser_stop(struct tty_struct *tty)
1926 struct mxser_port *info = tty->driver_data;
1927 unsigned long flags;
1929 spin_lock_irqsave(&info->slock, flags);
1930 if (info->IER & UART_IER_THRI) {
1931 info->IER &= ~UART_IER_THRI;
1932 outb(info->IER, info->ioaddr + UART_IER);
1934 spin_unlock_irqrestore(&info->slock, flags);
1937 static void mxser_start(struct tty_struct *tty)
1939 struct mxser_port *info = tty->driver_data;
1940 unsigned long flags;
1942 spin_lock_irqsave(&info->slock, flags);
1943 if (info->xmit_cnt && info->xmit_buf) {
1944 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1945 info->IER |= UART_IER_THRI;
1946 outb(info->IER, info->ioaddr + UART_IER);
1948 spin_unlock_irqrestore(&info->slock, flags);
1951 static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1953 struct mxser_port *info = tty->driver_data;
1954 unsigned long flags;
1956 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1957 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
1959 mxser_change_speed(info, old_termios);
1961 if ((old_termios->c_cflag & CRTSCTS) &&
1962 !(tty->termios->c_cflag & CRTSCTS)) {
1963 tty->hw_stopped = 0;
1968 /* Handle sw stopped */
1969 if ((old_termios->c_iflag & IXON) &&
1970 !(tty->termios->c_iflag & IXON)) {
1973 if (info->board->chip_flag) {
1974 spin_lock_irqsave(&info->slock, flags);
1975 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
1976 spin_unlock_irqrestore(&info->slock, flags);
1984 * mxser_wait_until_sent() --- wait until the transmitter is empty
1986 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1988 struct mxser_port *info = tty->driver_data;
1989 unsigned long orig_jiffies, char_time;
1992 if (info->type == PORT_UNKNOWN)
1995 if (info->xmit_fifo_size == 0)
1996 return; /* Just in case.... */
1998 orig_jiffies = jiffies;
2000 * Set the check interval to be 1/5 of the estimated time to
2001 * send a single character, and make it at least 1. The check
2002 * interval should also be less than the timeout.
2004 * Note: we have to use pretty tight timings here to satisfy
2007 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2008 char_time = char_time / 5;
2011 if (timeout && timeout < char_time)
2012 char_time = timeout;
2014 * If the transmitter hasn't cleared in twice the approximate
2015 * amount of time to send the entire FIFO, it probably won't
2016 * ever clear. This assumes the UART isn't doing flow
2017 * control, which is currently the case. Hence, if it ever
2018 * takes longer than info->timeout, this is probably due to a
2019 * UART bug of some kind. So, we clamp the timeout parameter at
2022 if (!timeout || timeout > 2 * info->timeout)
2023 timeout = 2 * info->timeout;
2024 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2025 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2026 timeout, char_time);
2027 printk("jiff=%lu...", jiffies);
2029 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
2030 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2031 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2033 schedule_timeout_interruptible(char_time);
2034 if (signal_pending(current))
2036 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2039 set_current_state(TASK_RUNNING);
2041 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2042 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2047 * This routine is called by tty_hangup() when a hangup is signaled.
2049 static void mxser_hangup(struct tty_struct *tty)
2051 struct mxser_port *info = tty->driver_data;
2053 mxser_flush_buffer(tty);
2054 mxser_shutdown(info);
2057 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2059 wake_up_interruptible(&info->open_wait);
2063 * mxser_rs_break() --- routine which turns the break handling on or off
2065 static void mxser_rs_break(struct tty_struct *tty, int break_state)
2067 struct mxser_port *info = tty->driver_data;
2068 unsigned long flags;
2070 spin_lock_irqsave(&info->slock, flags);
2071 if (break_state == -1)
2072 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2073 info->ioaddr + UART_LCR);
2075 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2076 info->ioaddr + UART_LCR);
2077 spin_unlock_irqrestore(&info->slock, flags);
2080 static void mxser_receive_chars(struct mxser_port *port, int *status)
2082 struct tty_struct *tty = port->tty;
2083 unsigned char ch, gdl;
2088 unsigned long flags;
2090 spin_lock_irqsave(&port->slock, flags);
2092 recv_room = tty->receive_room;
2093 if ((recv_room == 0) && (!port->ldisc_stop_rx))
2096 if (port->board->chip_flag != MOXA_OTHER_UART) {
2098 if (*status & UART_LSR_SPECIAL)
2100 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
2101 (*status & MOXA_MUST_LSR_RERR))
2103 if (*status & MOXA_MUST_LSR_RERR)
2106 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
2108 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
2109 gdl &= MOXA_MUST_GDL_MASK;
2110 if (gdl >= recv_room) {
2111 if (!port->ldisc_stop_rx)
2115 ch = inb(port->ioaddr + UART_RX);
2116 tty_insert_flip_char(tty, ch, 0);
2127 ch = inb(port->ioaddr + UART_RX);
2128 if (port->board->chip_flag && (*status & UART_LSR_OE))
2129 outb(0x23, port->ioaddr + UART_FCR);
2130 *status &= port->read_status_mask;
2131 if (*status & port->ignore_status_mask) {
2132 if (++ignored > 100)
2136 if (*status & UART_LSR_SPECIAL) {
2137 if (*status & UART_LSR_BI) {
2141 if (port->flags & ASYNC_SAK)
2143 } else if (*status & UART_LSR_PE) {
2145 port->icount.parity++;
2146 } else if (*status & UART_LSR_FE) {
2148 port->icount.frame++;
2149 } else if (*status & UART_LSR_OE) {
2151 port->icount.overrun++;
2154 tty_insert_flip_char(tty, ch, flag);
2156 if (cnt >= recv_room) {
2157 if (!port->ldisc_stop_rx)
2164 if (port->board->chip_flag)
2167 *status = inb(port->ioaddr + UART_LSR);
2168 } while (*status & UART_LSR_DR);
2171 mxvar_log.rxcnt[port->tty->index] += cnt;
2172 port->mon_data.rxcnt += cnt;
2173 port->mon_data.up_rxcnt += cnt;
2174 spin_unlock_irqrestore(&port->slock, flags);
2176 tty_flip_buffer_push(tty);
2179 static void mxser_transmit_chars(struct mxser_port *port)
2182 unsigned long flags;
2184 spin_lock_irqsave(&port->slock, flags);
2187 outb(port->x_char, port->ioaddr + UART_TX);
2189 mxvar_log.txcnt[port->tty->index]++;
2190 port->mon_data.txcnt++;
2191 port->mon_data.up_txcnt++;
2196 if (port->xmit_buf == 0)
2199 if ((port->xmit_cnt <= 0) || port->tty->stopped ||
2200 (port->tty->hw_stopped &&
2201 (port->type != PORT_16550A) &&
2202 (!port->board->chip_flag))) {
2203 port->IER &= ~UART_IER_THRI;
2204 outb(port->IER, port->ioaddr + UART_IER);
2208 cnt = port->xmit_cnt;
2209 count = port->xmit_fifo_size;
2211 outb(port->xmit_buf[port->xmit_tail++],
2212 port->ioaddr + UART_TX);
2213 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2214 if (--port->xmit_cnt <= 0)
2216 } while (--count > 0);
2217 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
2219 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2220 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2221 port->icount.tx += (cnt - port->xmit_cnt);
2223 if (port->xmit_cnt < WAKEUP_CHARS) {
2224 set_bit(MXSER_EVENT_TXLOW, &port->event);
2225 schedule_work(&port->tqueue);
2227 if (port->xmit_cnt <= 0) {
2228 port->IER &= ~UART_IER_THRI;
2229 outb(port->IER, port->ioaddr + UART_IER);
2232 spin_unlock_irqrestore(&port->slock, flags);
2236 * This is the serial driver's generic interrupt routine
2238 static irqreturn_t mxser_interrupt(int irq, void *dev_id)
2241 struct mxser_board *brd = NULL;
2242 struct mxser_port *port;
2243 int max, irqbits, bits, msr;
2244 int pass_counter = 0;
2245 unsigned int int_cnt;
2246 int handled = IRQ_NONE;
2248 for (i = 0; i < MXSER_BOARDS; i++)
2249 if (dev_id == &mxser_boards[i]) {
2254 if (i == MXSER_BOARDS)
2258 max = brd->info->nports;
2260 irqbits = inb(brd->vector) & brd->vector_mask;
2261 if (irqbits == brd->vector_mask)
2264 handled = IRQ_HANDLED;
2265 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2266 if (irqbits == brd->vector_mask)
2270 port = &brd->ports[i];
2274 iir = inb(port->ioaddr + UART_IIR);
2275 if (iir & UART_IIR_NO_INT)
2277 iir &= MOXA_MUST_IIR_MASK;
2279 status = inb(port->ioaddr + UART_LSR);
2280 outb(0x27, port->ioaddr + UART_FCR);
2281 inb(port->ioaddr + UART_MSR);
2285 status = inb(port->ioaddr + UART_LSR);
2287 if (status & UART_LSR_PE)
2288 port->err_shadow |= NPPI_NOTIFY_PARITY;
2289 if (status & UART_LSR_FE)
2290 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2291 if (status & UART_LSR_OE)
2293 NPPI_NOTIFY_HW_OVERRUN;
2294 if (status & UART_LSR_BI)
2295 port->err_shadow |= NPPI_NOTIFY_BREAK;
2297 if (port->board->chip_flag) {
2298 if (iir == MOXA_MUST_IIR_GDA ||
2299 iir == MOXA_MUST_IIR_RDA ||
2300 iir == MOXA_MUST_IIR_RTO ||
2301 iir == MOXA_MUST_IIR_LSR)
2302 mxser_receive_chars(port,
2306 status &= port->read_status_mask;
2307 if (status & UART_LSR_DR)
2308 mxser_receive_chars(port,
2311 msr = inb(port->ioaddr + UART_MSR);
2312 if (msr & UART_MSR_ANY_DELTA)
2313 mxser_check_modem_status(port, msr);
2315 if (port->board->chip_flag) {
2316 if (iir == 0x02 && (status &
2318 mxser_transmit_chars(port);
2320 if (status & UART_LSR_THRE)
2321 mxser_transmit_chars(port);
2323 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2325 if (pass_counter++ > MXSER_ISR_PASS_LIMIT)
2326 break; /* Prevent infinite loops */
2333 static const struct tty_operations mxser_ops = {
2335 .close = mxser_close,
2336 .write = mxser_write,
2337 .put_char = mxser_put_char,
2338 .flush_chars = mxser_flush_chars,
2339 .write_room = mxser_write_room,
2340 .chars_in_buffer = mxser_chars_in_buffer,
2341 .flush_buffer = mxser_flush_buffer,
2342 .ioctl = mxser_ioctl,
2343 .throttle = mxser_throttle,
2344 .unthrottle = mxser_unthrottle,
2345 .set_termios = mxser_set_termios,
2347 .start = mxser_start,
2348 .hangup = mxser_hangup,
2349 .break_ctl = mxser_rs_break,
2350 .wait_until_sent = mxser_wait_until_sent,
2351 .tiocmget = mxser_tiocmget,
2352 .tiocmset = mxser_tiocmset,
2356 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2359 static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
2363 free_irq(brd->irq, brd);
2364 if (pdev != NULL) { /* PCI */
2366 pci_release_region(pdev, 2);
2367 pci_release_region(pdev, 3);
2371 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2372 release_region(brd->vector, 1);
2376 static int __devinit mxser_initbrd(struct mxser_board *brd,
2377 struct pci_dev *pdev)
2379 struct mxser_port *info;
2383 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
2385 for (i = 0; i < brd->info->nports; i++) {
2386 info = &brd->ports[i];
2389 info->ldisc_stop_rx = 0;
2391 /* Enhance mode enabled here */
2392 if (brd->chip_flag != MOXA_OTHER_UART)
2393 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
2395 info->flags = ASYNC_SHARE_IRQ;
2396 info->type = brd->uart_type;
2398 process_txrx_fifo(info);
2400 info->custom_divisor = info->baud_base * 16;
2401 info->close_delay = 5 * HZ / 10;
2402 info->closing_wait = 30 * HZ;
2403 INIT_WORK(&info->tqueue, mxser_do_softint);
2404 info->normal_termios = mxvar_sdriver->init_termios;
2405 init_waitqueue_head(&info->open_wait);
2406 init_waitqueue_head(&info->delta_msr_wait);
2407 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2408 info->err_shadow = 0;
2409 spin_lock_init(&info->slock);
2411 /* before set INT ISR, disable all int */
2412 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2413 info->ioaddr + UART_IER);
2416 * Allocate the IRQ if necessary
2419 retval = request_irq(brd->irq, mxser_interrupt,
2420 (brd->ports[0].flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED :
2421 IRQF_DISABLED, "mxser", brd);
2423 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2424 "conflict with another device.\n",
2425 brd->info->name, brd->irq);
2426 /* We hold resources, we need to release them. */
2427 mxser_release_res(brd, pdev, 0);
2433 static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
2436 unsigned short regs[16], irq;
2437 unsigned char scratch, scratch2;
2439 brd->chip_flag = MOXA_OTHER_UART;
2441 id = mxser_read_register(cap, regs);
2444 brd->info = &mxser_cards[0];
2447 brd->info = &mxser_cards[1];
2449 case CI104J_ASIC_ID:
2450 brd->info = &mxser_cards[2];
2453 brd->info = &mxser_cards[5];
2456 brd->info = &mxser_cards[6];
2459 brd->info = &mxser_cards[7];
2466 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2467 Flag-hack checks if configuration should be read as 2-port here. */
2468 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
2469 irq = regs[9] & 0xF000;
2470 irq = irq | (irq >> 4);
2471 if (irq != (regs[9] & 0xFF00))
2472 return MXSER_ERR_IRQ_CONFLIT;
2473 } else if (brd->info->nports == 4) {
2474 irq = regs[9] & 0xF000;
2475 irq = irq | (irq >> 4);
2476 irq = irq | (irq >> 8);
2478 return MXSER_ERR_IRQ_CONFLIT;
2479 } else if (brd->info->nports == 8) {
2480 irq = regs[9] & 0xF000;
2481 irq = irq | (irq >> 4);
2482 irq = irq | (irq >> 8);
2483 if ((irq != regs[9]) || (irq != regs[10]))
2484 return MXSER_ERR_IRQ_CONFLIT;
2488 return MXSER_ERR_IRQ;
2489 brd->irq = ((int)(irq & 0xF000) >> 12);
2490 for (i = 0; i < 8; i++)
2491 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
2492 if ((regs[12] & 0x80) == 0)
2493 return MXSER_ERR_VECTOR;
2494 brd->vector = (int)regs[11]; /* interrupt vector */
2496 brd->vector_mask = 0x00FF;
2498 brd->vector_mask = 0x000F;
2499 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2500 if (regs[12] & bits) {
2501 brd->ports[i].baud_base = 921600;
2502 brd->ports[i].max_baud = 921600;
2504 brd->ports[i].baud_base = 115200;
2505 brd->ports[i].max_baud = 115200;
2508 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2509 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2510 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2511 outb(scratch2, cap + UART_LCR);
2512 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2513 scratch = inb(cap + UART_IIR);
2516 brd->uart_type = PORT_16550A;
2518 brd->uart_type = PORT_16450;
2519 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2521 return MXSER_ERR_IOADDR;
2522 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2523 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2524 return MXSER_ERR_VECTOR;
2526 return brd->info->nports;
2529 static int __devinit mxser_probe(struct pci_dev *pdev,
2530 const struct pci_device_id *ent)
2533 struct mxser_board *brd;
2535 unsigned long ioaddress;
2536 int retval = -EINVAL;
2538 for (i = 0; i < MXSER_BOARDS; i++)
2539 if (mxser_boards[i].info == NULL)
2542 if (i >= MXSER_BOARDS) {
2543 printk(KERN_ERR "Too many Smartio/Industio family boards found "
2544 "(maximum %d), board not configured\n", MXSER_BOARDS);
2548 brd = &mxser_boards[i];
2549 brd->idx = i * MXSER_PORTS_PER_BOARD;
2550 printk(KERN_INFO "Found MOXA %s board (BusNo=%d, DevNo=%d)\n",
2551 mxser_cards[ent->driver_data].name,
2552 pdev->bus->number, PCI_SLOT(pdev->devfn));
2554 retval = pci_enable_device(pdev);
2556 printk(KERN_ERR "Moxa SmartI/O PCI enable fail !\n");
2561 ioaddress = pci_resource_start(pdev, 2);
2562 retval = pci_request_region(pdev, 2, "mxser(IO)");
2566 brd->info = &mxser_cards[ent->driver_data];
2567 for (i = 0; i < brd->info->nports; i++)
2568 brd->ports[i].ioaddr = ioaddress + 8 * i;
2571 ioaddress = pci_resource_start(pdev, 3);
2572 retval = pci_request_region(pdev, 3, "mxser(vector)");
2575 brd->vector = ioaddress;
2578 brd->irq = pdev->irq;
2580 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2581 brd->uart_type = PORT_16550A;
2582 brd->vector_mask = 0;
2584 for (i = 0; i < brd->info->nports; i++) {
2585 for (j = 0; j < UART_INFO_NUM; j++) {
2586 if (Gpci_uart_info[j].type == brd->chip_flag) {
2587 brd->ports[i].max_baud =
2588 Gpci_uart_info[j].max_baud;
2590 /* exception....CP-102 */
2591 if (brd->info->flags & MXSER_HIGHBAUD)
2592 brd->ports[i].max_baud = 921600;
2598 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2599 for (i = 0; i < brd->info->nports; i++) {
2601 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2603 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
2605 outb(0, ioaddress + 4); /* default set to RS232 mode */
2606 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
2609 for (i = 0; i < brd->info->nports; i++) {
2610 brd->vector_mask |= (1 << i);
2611 brd->ports[i].baud_base = 921600;
2614 /* mxser_initbrd will hook ISR. */
2615 if (mxser_initbrd(brd, pdev) < 0)
2618 for (i = 0; i < brd->info->nports; i++)
2619 tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
2621 pci_set_drvdata(pdev, brd);
2625 pci_release_region(pdev, 3);
2627 pci_release_region(pdev, 2);
2636 static void __devexit mxser_remove(struct pci_dev *pdev)
2638 struct mxser_board *brd = pci_get_drvdata(pdev);
2641 for (i = 0; i < brd->info->nports; i++)
2642 tty_unregister_device(mxvar_sdriver, brd->idx + i);
2644 mxser_release_res(brd, pdev, 1);
2647 static struct pci_driver mxser_driver = {
2649 .id_table = mxser_pcibrds,
2650 .probe = mxser_probe,
2651 .remove = __devexit_p(mxser_remove)
2654 static int __init mxser_module_init(void)
2656 struct mxser_board *brd;
2658 unsigned int i, m, isaloop;
2661 pr_debug("Loading module mxser ...\n");
2663 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2667 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2670 /* Initialize the tty_driver structure */
2671 mxvar_sdriver->owner = THIS_MODULE;
2672 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2673 mxvar_sdriver->name = "ttyMI";
2674 mxvar_sdriver->major = ttymajor;
2675 mxvar_sdriver->minor_start = 0;
2676 mxvar_sdriver->num = MXSER_PORTS + 1;
2677 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2678 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2679 mxvar_sdriver->init_termios = tty_std_termios;
2680 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2681 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2682 tty_set_operations(mxvar_sdriver, &mxser_ops);
2684 retval = tty_register_driver(mxvar_sdriver);
2686 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2694 /* Start finding ISA boards here */
2695 for (isaloop = 0; isaloop < 2; isaloop++)
2696 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2698 cap = mxserBoardCAP[b]; /* predefined */
2700 cap = ioaddr[b]; /* module param */
2705 brd = &mxser_boards[m];
2706 retval = mxser_get_ISA_conf(cap, brd);
2709 printk(KERN_INFO "Found MOXA %s board "
2711 brd->info->name, ioaddr[b]);
2714 if (retval == MXSER_ERR_IRQ)
2715 printk(KERN_ERR "Invalid interrupt "
2716 "number, board not "
2718 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2719 printk(KERN_ERR "Invalid interrupt "
2720 "number, board not "
2722 else if (retval == MXSER_ERR_VECTOR)
2723 printk(KERN_ERR "Invalid interrupt "
2724 "vector, board not "
2726 else if (retval == MXSER_ERR_IOADDR)
2727 printk(KERN_ERR "Invalid I/O address, "
2728 "board not configured\n");
2734 /* mxser_initbrd will hook ISR. */
2735 if (mxser_initbrd(brd, NULL) < 0) {
2740 brd->idx = m * MXSER_PORTS_PER_BOARD;
2741 for (i = 0; i < brd->info->nports; i++)
2742 tty_register_device(mxvar_sdriver, brd->idx + i,
2748 retval = pci_register_driver(&mxser_driver);
2750 printk(KERN_ERR "Can't register pci driver\n");
2754 } /* else: we have some ISA cards under control */
2757 pr_debug("Done.\n");
2761 tty_unregister_driver(mxvar_sdriver);
2763 put_tty_driver(mxvar_sdriver);
2767 static void __exit mxser_module_exit(void)
2771 pr_debug("Unloading module mxser ...\n");
2773 pci_unregister_driver(&mxser_driver);
2775 for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
2776 if (mxser_boards[i].info != NULL)
2777 for (j = 0; j < mxser_boards[i].info->nports; j++)
2778 tty_unregister_device(mxvar_sdriver,
2779 mxser_boards[i].idx + j);
2780 tty_unregister_driver(mxvar_sdriver);
2781 put_tty_driver(mxvar_sdriver);
2783 for (i = 0; i < MXSER_BOARDS; i++)
2784 if (mxser_boards[i].info != NULL)
2785 mxser_release_res(&mxser_boards[i], NULL, 1);
2787 pr_debug("Done.\n");
2790 module_init(mxser_module_init);
2791 module_exit(mxser_module_exit);