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.1" /* 1.9.15 */
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 100
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 mxvar_baud_table[] = {
183 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
184 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
186 static unsigned int mxvar_baud_table1[] = {
187 0, B50, B75, B110, B134, B150, B200, B300, B600, B1200, B1800, B2400,
188 B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B921600
190 #define BAUD_TABLE_NO ARRAY_SIZE(mxvar_baud_table)
192 #define B_SPEC B2000000
194 static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
195 static int ttymajor = MXSERMAJOR;
196 static int calloutmajor = MXSERCUMAJOR;
198 /* Variables for insmod */
200 MODULE_AUTHOR("Casper Yang");
201 MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
202 module_param_array(ioaddr, int, NULL, 0);
203 module_param(ttymajor, int, 0);
204 MODULE_LICENSE("GPL");
208 unsigned long rxcnt[MXSER_PORTS];
209 unsigned long txcnt[MXSER_PORTS];
216 unsigned long up_rxcnt;
217 unsigned long up_txcnt;
219 unsigned char hold_reason;
222 struct mxser_mon_ext {
223 unsigned long rx_cnt[32];
224 unsigned long tx_cnt[32];
225 unsigned long up_rxcnt[32];
226 unsigned long up_txcnt[32];
227 int modem_status[32];
241 struct mxser_board *board;
242 struct tty_struct *tty;
244 unsigned long ioaddr;
245 unsigned long opmode_ioaddr;
249 int rx_trigger; /* Rx fifo trigger level */
251 int baud_base; /* max. speed */
253 int type; /* UART type */
254 int flags; /* defined in tty.h */
257 int x_char; /* xon/xoff character */
258 int IER; /* Interrupt Enable Register */
259 int MCR; /* Modem control register */
261 unsigned char stop_rx;
262 unsigned char ldisc_stop_rx;
266 unsigned short closing_wait;
267 unsigned char err_shadow;
270 int count; /* # of fd on device */
271 int blocked_open; /* # of blocked opens */
272 struct async_icount icount; /* kernel counters for 4 input interrupts */
275 int read_status_mask;
276 int ignore_status_mask;
278 unsigned char *xmit_buf;
283 struct ktermios normal_termios;
285 struct mxser_mon mon_data;
288 wait_queue_head_t open_wait;
289 wait_queue_head_t delta_msr_wait;
295 const struct mxser_cardinfo *info;
296 unsigned long vector;
297 unsigned long vector_mask;
302 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
305 struct mxser_mstatus {
313 static struct mxser_mstatus GMStatus[MXSER_PORTS];
315 static int mxserBoardCAP[MXSER_BOARDS] = {
317 /* 0x180, 0x280, 0x200, 0x320 */
320 static struct mxser_board mxser_boards[MXSER_BOARDS];
321 static struct tty_driver *mxvar_sdriver;
322 static struct mxser_log mxvar_log;
323 static int mxvar_diagflag;
324 static unsigned char mxser_msr[MXSER_PORTS + 1];
325 static struct mxser_mon_ext mon_data_ext;
326 static int mxser_set_baud_method[MXSER_PORTS + 1];
329 static int __devinit CheckIsMoxaMust(int io)
334 outb(0, io + UART_LCR);
335 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
336 oldmcr = inb(io + UART_MCR);
337 outb(0, io + UART_MCR);
338 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
339 if ((hwid = inb(io + UART_MCR)) != 0) {
340 outb(oldmcr, io + UART_MCR);
341 return MOXA_OTHER_UART;
344 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
345 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
346 if (hwid == Gpci_uart_info[i].type)
349 return MOXA_OTHER_UART;
353 static void process_txrx_fifo(struct mxser_port *info)
357 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
358 info->rx_trigger = 1;
359 info->rx_high_water = 1;
360 info->rx_low_water = 1;
361 info->xmit_fifo_size = 1;
363 for (i = 0; i < UART_INFO_NUM; i++)
364 if (info->board->chip_flag == Gpci_uart_info[i].type) {
365 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
366 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
367 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
368 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
373 static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
375 unsigned char status = 0;
377 status = inb(baseaddr + UART_MSR);
379 mxser_msr[port] &= 0x0F;
380 mxser_msr[port] |= status;
381 status = mxser_msr[port];
388 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
389 struct mxser_port *port)
391 DECLARE_WAITQUEUE(wait, current);
397 * If non-blocking mode is set, or the port is not enabled,
398 * then make the check up front and then exit.
400 if ((filp->f_flags & O_NONBLOCK) ||
401 test_bit(TTY_IO_ERROR, &tty->flags)) {
402 port->flags |= ASYNC_NORMAL_ACTIVE;
406 if (tty->termios->c_cflag & CLOCAL)
410 * Block waiting for the carrier detect and the line to become
411 * free (i.e., not in use by the callout). While we are in
412 * this loop, port->count is dropped by one, so that
413 * mxser_close() knows when to free things. We restore it upon
414 * exit, either normal or abnormal.
417 add_wait_queue(&port->open_wait, &wait);
419 spin_lock_irqsave(&port->slock, flags);
420 if (!tty_hung_up_p(filp))
422 spin_unlock_irqrestore(&port->slock, flags);
423 port->blocked_open++;
425 spin_lock_irqsave(&port->slock, flags);
426 outb(inb(port->ioaddr + UART_MCR) |
427 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
428 spin_unlock_irqrestore(&port->slock, flags);
429 set_current_state(TASK_INTERRUPTIBLE);
430 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
431 if (port->flags & ASYNC_HUP_NOTIFY)
434 retval = -ERESTARTSYS;
437 if (!(port->flags & ASYNC_CLOSING) &&
439 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
441 if (signal_pending(current)) {
442 retval = -ERESTARTSYS;
447 set_current_state(TASK_RUNNING);
448 remove_wait_queue(&port->open_wait, &wait);
449 if (!tty_hung_up_p(filp))
451 port->blocked_open--;
454 port->flags |= ASYNC_NORMAL_ACTIVE;
458 static int mxser_set_baud(struct mxser_port *info, long newspd)
465 if (!info->tty || !info->tty->termios)
471 if (newspd > info->max_baud)
474 info->realbaud = newspd;
475 for (i = 0; i < BAUD_TABLE_NO; i++)
476 if (newspd == mxvar_baud_table[i])
478 if (i == BAUD_TABLE_NO) {
479 quot = info->baud_base / info->speed;
480 if (info->speed <= 0 || info->speed > info->max_baud)
484 quot = (2 * info->baud_base / 269);
486 quot = info->baud_base / newspd;
494 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
495 info->timeout += HZ / 50; /* Add .02 seconds of slop */
498 info->MCR |= UART_MCR_DTR;
499 outb(info->MCR, info->ioaddr + UART_MCR);
501 info->MCR &= ~UART_MCR_DTR;
502 outb(info->MCR, info->ioaddr + UART_MCR);
506 cval = inb(info->ioaddr + UART_LCR);
508 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
510 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
511 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
512 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
514 if (i == BAUD_TABLE_NO) {
515 quot = info->baud_base % info->speed;
517 if ((quot % info->speed) > (info->speed / 2)) {
523 SET_MOXA_MUST_ENUM_VALUE(info->ioaddr, quot);
525 SET_MOXA_MUST_ENUM_VALUE(info->ioaddr, 0);
531 * This routine is called to set the UART divisor registers to match
532 * the specified baud rate for a serial port.
534 static int mxser_change_speed(struct mxser_port *info,
535 struct ktermios *old_termios)
537 unsigned cflag, cval, fcr;
539 unsigned char status;
542 if (!info->tty || !info->tty->termios)
544 cflag = info->tty->termios->c_cflag;
548 if (mxser_set_baud_method[info->tty->index] == 0) {
549 if ((cflag & CBAUD) == B_SPEC)
552 baud = tty_get_baud_rate(info->tty);
553 mxser_set_baud(info, baud);
556 /* byte size and parity */
557 switch (cflag & CSIZE) {
572 break; /* too keep GCC shut... */
577 cval |= UART_LCR_PARITY;
578 if (!(cflag & PARODD))
579 cval |= UART_LCR_EPAR;
581 cval |= UART_LCR_SPAR;
583 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
584 if (info->board->chip_flag) {
585 fcr = UART_FCR_ENABLE_FIFO;
586 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
587 SET_MOXA_MUST_FIFO_VALUE(info);
591 fcr = UART_FCR_ENABLE_FIFO;
592 if (info->board->chip_flag) {
593 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
594 SET_MOXA_MUST_FIFO_VALUE(info);
596 switch (info->rx_trigger) {
598 fcr |= UART_FCR_TRIGGER_1;
601 fcr |= UART_FCR_TRIGGER_4;
604 fcr |= UART_FCR_TRIGGER_8;
607 fcr |= UART_FCR_TRIGGER_14;
613 /* CTS flow control flag and modem status interrupts */
614 info->IER &= ~UART_IER_MSI;
615 info->MCR &= ~UART_MCR_AFE;
616 if (cflag & CRTSCTS) {
617 info->flags |= ASYNC_CTS_FLOW;
618 info->IER |= UART_IER_MSI;
619 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
620 info->MCR |= UART_MCR_AFE;
622 status = inb(info->ioaddr + UART_MSR);
623 if (info->tty->hw_stopped) {
624 if (status & UART_MSR_CTS) {
625 info->tty->hw_stopped = 0;
626 if (info->type != PORT_16550A &&
627 !info->board->chip_flag) {
628 outb(info->IER & ~UART_IER_THRI,
631 info->IER |= UART_IER_THRI;
632 outb(info->IER, info->ioaddr +
635 tty_wakeup(info->tty);
638 if (!(status & UART_MSR_CTS)) {
639 info->tty->hw_stopped = 1;
640 if ((info->type != PORT_16550A) &&
641 (!info->board->chip_flag)) {
642 info->IER &= ~UART_IER_THRI;
643 outb(info->IER, info->ioaddr +
650 info->flags &= ~ASYNC_CTS_FLOW;
652 outb(info->MCR, info->ioaddr + UART_MCR);
653 if (cflag & CLOCAL) {
654 info->flags &= ~ASYNC_CHECK_CD;
656 info->flags |= ASYNC_CHECK_CD;
657 info->IER |= UART_IER_MSI;
659 outb(info->IER, info->ioaddr + UART_IER);
662 * Set up parity check flag
664 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
665 if (I_INPCK(info->tty))
666 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
667 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
668 info->read_status_mask |= UART_LSR_BI;
670 info->ignore_status_mask = 0;
672 if (I_IGNBRK(info->tty)) {
673 info->ignore_status_mask |= UART_LSR_BI;
674 info->read_status_mask |= UART_LSR_BI;
676 * If we're ignore parity and break indicators, ignore
677 * overruns too. (For real raw support).
679 if (I_IGNPAR(info->tty)) {
680 info->ignore_status_mask |=
684 info->read_status_mask |=
690 if (info->board->chip_flag) {
691 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
692 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
693 if (I_IXON(info->tty)) {
694 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
696 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
698 if (I_IXOFF(info->tty)) {
699 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
701 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
706 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
707 outb(cval, info->ioaddr + UART_LCR);
712 static void mxser_check_modem_status(struct mxser_port *port, int status)
714 /* update input line counters */
715 if (status & UART_MSR_TERI)
717 if (status & UART_MSR_DDSR)
719 if (status & UART_MSR_DDCD)
721 if (status & UART_MSR_DCTS)
723 port->mon_data.modem_status = status;
724 wake_up_interruptible(&port->delta_msr_wait);
726 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
727 if (status & UART_MSR_DCD)
728 wake_up_interruptible(&port->open_wait);
731 if (port->flags & ASYNC_CTS_FLOW) {
732 if (port->tty->hw_stopped) {
733 if (status & UART_MSR_CTS) {
734 port->tty->hw_stopped = 0;
736 if ((port->type != PORT_16550A) &&
737 (!port->board->chip_flag)) {
738 outb(port->IER & ~UART_IER_THRI,
739 port->ioaddr + UART_IER);
740 port->IER |= UART_IER_THRI;
741 outb(port->IER, port->ioaddr +
744 tty_wakeup(port->tty);
747 if (!(status & UART_MSR_CTS)) {
748 port->tty->hw_stopped = 1;
749 if (port->type != PORT_16550A &&
750 !port->board->chip_flag) {
751 port->IER &= ~UART_IER_THRI;
752 outb(port->IER, port->ioaddr +
760 static int mxser_startup(struct mxser_port *info)
765 page = __get_free_page(GFP_KERNEL);
769 spin_lock_irqsave(&info->slock, flags);
771 if (info->flags & ASYNC_INITIALIZED) {
773 spin_unlock_irqrestore(&info->slock, flags);
777 if (!info->ioaddr || !info->type) {
779 set_bit(TTY_IO_ERROR, &info->tty->flags);
781 spin_unlock_irqrestore(&info->slock, flags);
787 info->xmit_buf = (unsigned char *) page;
790 * Clear the FIFO buffers and disable them
791 * (they will be reenabled in mxser_change_speed())
793 if (info->board->chip_flag)
794 outb((UART_FCR_CLEAR_RCVR |
795 UART_FCR_CLEAR_XMIT |
796 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
798 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
799 info->ioaddr + UART_FCR);
802 * At this point there's no way the LSR could still be 0xFF;
803 * if it is, then bail out, because there's likely no UART
806 if (inb(info->ioaddr + UART_LSR) == 0xff) {
807 spin_unlock_irqrestore(&info->slock, flags);
808 if (capable(CAP_SYS_ADMIN)) {
810 set_bit(TTY_IO_ERROR, &info->tty->flags);
817 * Clear the interrupt registers.
819 (void) inb(info->ioaddr + UART_LSR);
820 (void) inb(info->ioaddr + UART_RX);
821 (void) inb(info->ioaddr + UART_IIR);
822 (void) inb(info->ioaddr + UART_MSR);
825 * Now, initialize the UART
827 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
828 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
829 outb(info->MCR, info->ioaddr + UART_MCR);
832 * Finally, enable interrupts
834 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
836 if (info->board->chip_flag)
837 info->IER |= MOXA_MUST_IER_EGDAI;
838 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
841 * And clear the interrupt registers again for luck.
843 (void) inb(info->ioaddr + UART_LSR);
844 (void) inb(info->ioaddr + UART_RX);
845 (void) inb(info->ioaddr + UART_IIR);
846 (void) inb(info->ioaddr + UART_MSR);
849 clear_bit(TTY_IO_ERROR, &info->tty->flags);
850 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
853 * and set the speed of the serial port
855 mxser_change_speed(info, NULL);
856 info->flags |= ASYNC_INITIALIZED;
857 spin_unlock_irqrestore(&info->slock, flags);
863 * This routine will shutdown a serial port; interrupts maybe disabled, and
864 * DTR is dropped if the hangup on close termio flag is on.
866 static void mxser_shutdown(struct mxser_port *info)
870 if (!(info->flags & ASYNC_INITIALIZED))
873 spin_lock_irqsave(&info->slock, flags);
876 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
877 * here so the queue might never be waken up
879 wake_up_interruptible(&info->delta_msr_wait);
882 * Free the IRQ, if necessary
884 if (info->xmit_buf) {
885 free_page((unsigned long) info->xmit_buf);
886 info->xmit_buf = NULL;
890 outb(0x00, info->ioaddr + UART_IER);
892 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
893 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
894 outb(info->MCR, info->ioaddr + UART_MCR);
896 /* clear Rx/Tx FIFO's */
897 if (info->board->chip_flag)
898 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
899 MOXA_MUST_FCR_GDA_MODE_ENABLE,
900 info->ioaddr + UART_FCR);
902 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
903 info->ioaddr + UART_FCR);
905 /* read data port to reset things */
906 (void) inb(info->ioaddr + UART_RX);
909 set_bit(TTY_IO_ERROR, &info->tty->flags);
911 info->flags &= ~ASYNC_INITIALIZED;
913 if (info->board->chip_flag)
914 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
916 spin_unlock_irqrestore(&info->slock, flags);
920 * This routine is called whenever a serial port is opened. It
921 * enables interrupts for a serial port, linking in its async structure into
922 * the IRQ chain. It also performs the serial-specific
923 * initialization for the tty structure.
925 static int mxser_open(struct tty_struct *tty, struct file *filp)
927 struct mxser_port *info;
932 if (line == MXSER_PORTS)
934 if (line < 0 || line > MXSER_PORTS)
936 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
940 tty->driver_data = info;
943 * Start up serial port
945 spin_lock_irqsave(&info->slock, flags);
947 spin_unlock_irqrestore(&info->slock, flags);
948 retval = mxser_startup(info);
952 retval = mxser_block_til_ready(tty, filp, info);
956 /* unmark here for very high baud rate (ex. 921600 bps) used */
957 tty->low_latency = 1;
962 * This routine is called when the serial port gets closed. First, we
963 * wait for the last remaining data to be sent. Then, we unlink its
964 * async structure from the interrupt chain if necessary, and we free
965 * that IRQ if nothing is left in the chain.
967 static void mxser_close(struct tty_struct *tty, struct file *filp)
969 struct mxser_port *info = tty->driver_data;
971 unsigned long timeout;
974 if (tty->index == MXSER_PORTS)
979 spin_lock_irqsave(&info->slock, flags);
981 if (tty_hung_up_p(filp)) {
982 spin_unlock_irqrestore(&info->slock, flags);
985 if ((tty->count == 1) && (info->count != 1)) {
987 * Uh, oh. tty->count is 1, which means that the tty
988 * structure will be freed. Info->count should always
989 * be one in these conditions. If it's greater than
990 * one, we've got real problems, since it means the
991 * serial port won't be shutdown.
993 printk(KERN_ERR "mxser_close: bad serial port count; "
994 "tty->count is 1, info->count is %d\n", info->count);
997 if (--info->count < 0) {
998 printk(KERN_ERR "mxser_close: bad serial port count for "
999 "ttys%d: %d\n", tty->index, info->count);
1003 spin_unlock_irqrestore(&info->slock, flags);
1006 info->flags |= ASYNC_CLOSING;
1007 spin_unlock_irqrestore(&info->slock, flags);
1009 * Save the termios structure, since this port may have
1010 * separate termios for callout and dialin.
1012 if (info->flags & ASYNC_NORMAL_ACTIVE)
1013 info->normal_termios = *tty->termios;
1015 * Now we wait for the transmit buffer to clear; and we notify
1016 * the line discipline to only process XON/XOFF characters.
1019 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1020 tty_wait_until_sent(tty, info->closing_wait);
1022 * At this point we stop accepting input. To do this, we
1023 * disable the receive line status interrupts, and tell the
1024 * interrupt driver to stop checking the data ready bit in the
1025 * line status register.
1027 info->IER &= ~UART_IER_RLSI;
1028 if (info->board->chip_flag)
1029 info->IER &= ~MOXA_MUST_RECV_ISR;
1031 if (info->flags & ASYNC_INITIALIZED) {
1032 outb(info->IER, info->ioaddr + UART_IER);
1034 * Before we drop DTR, make sure the UART transmitter
1035 * has completely drained; this is especially
1036 * important if there is a transmit FIFO!
1038 timeout = jiffies + HZ;
1039 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
1040 schedule_timeout_interruptible(5);
1041 if (time_after(jiffies, timeout))
1045 mxser_shutdown(info);
1047 if (tty->driver->flush_buffer)
1048 tty->driver->flush_buffer(tty);
1050 tty_ldisc_flush(tty);
1055 if (info->blocked_open) {
1056 if (info->close_delay)
1057 schedule_timeout_interruptible(info->close_delay);
1058 wake_up_interruptible(&info->open_wait);
1061 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1064 static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1067 struct mxser_port *info = tty->driver_data;
1068 unsigned long flags;
1070 if (!info->xmit_buf)
1074 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1075 SERIAL_XMIT_SIZE - info->xmit_head));
1079 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1080 spin_lock_irqsave(&info->slock, flags);
1081 info->xmit_head = (info->xmit_head + c) &
1082 (SERIAL_XMIT_SIZE - 1);
1083 info->xmit_cnt += c;
1084 spin_unlock_irqrestore(&info->slock, flags);
1091 if (info->xmit_cnt && !tty->stopped) {
1092 if (!tty->hw_stopped ||
1093 (info->type == PORT_16550A) ||
1094 (info->board->chip_flag)) {
1095 spin_lock_irqsave(&info->slock, flags);
1096 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
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_put_char(struct tty_struct *tty, unsigned char ch)
1108 struct mxser_port *info = tty->driver_data;
1109 unsigned long flags;
1111 if (!info->xmit_buf)
1114 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1117 spin_lock_irqsave(&info->slock, flags);
1118 info->xmit_buf[info->xmit_head++] = ch;
1119 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1121 spin_unlock_irqrestore(&info->slock, flags);
1122 if (!tty->stopped) {
1123 if (!tty->hw_stopped ||
1124 (info->type == PORT_16550A) ||
1125 info->board->chip_flag) {
1126 spin_lock_irqsave(&info->slock, flags);
1127 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1128 info->IER |= UART_IER_THRI;
1129 outb(info->IER, info->ioaddr + UART_IER);
1130 spin_unlock_irqrestore(&info->slock, flags);
1136 static void mxser_flush_chars(struct tty_struct *tty)
1138 struct mxser_port *info = tty->driver_data;
1139 unsigned long flags;
1141 if (info->xmit_cnt <= 0 ||
1145 (info->type != PORT_16550A) &&
1146 (!info->board->chip_flag)
1150 spin_lock_irqsave(&info->slock, flags);
1152 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1153 info->IER |= UART_IER_THRI;
1154 outb(info->IER, info->ioaddr + UART_IER);
1156 spin_unlock_irqrestore(&info->slock, flags);
1159 static int mxser_write_room(struct tty_struct *tty)
1161 struct mxser_port *info = tty->driver_data;
1164 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1170 static int mxser_chars_in_buffer(struct tty_struct *tty)
1172 struct mxser_port *info = tty->driver_data;
1173 return info->xmit_cnt;
1176 static void mxser_flush_buffer(struct tty_struct *tty)
1178 struct mxser_port *info = tty->driver_data;
1180 unsigned long flags;
1183 spin_lock_irqsave(&info->slock, flags);
1184 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1186 fcr = inb(info->ioaddr + UART_FCR);
1187 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1188 info->ioaddr + UART_FCR);
1189 outb(fcr, info->ioaddr + UART_FCR);
1191 spin_unlock_irqrestore(&info->slock, flags);
1197 * ------------------------------------------------------------
1198 * friends of mxser_ioctl()
1199 * ------------------------------------------------------------
1201 static int mxser_get_serial_info(struct mxser_port *info,
1202 struct serial_struct __user *retinfo)
1204 struct serial_struct tmp;
1208 memset(&tmp, 0, sizeof(tmp));
1209 tmp.type = info->type;
1210 tmp.line = info->tty->index;
1211 tmp.port = info->ioaddr;
1212 tmp.irq = info->board->irq;
1213 tmp.flags = info->flags;
1214 tmp.baud_base = info->baud_base;
1215 tmp.close_delay = info->close_delay;
1216 tmp.closing_wait = info->closing_wait;
1217 tmp.custom_divisor = info->custom_divisor;
1219 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1224 static int mxser_set_serial_info(struct mxser_port *info,
1225 struct serial_struct __user *new_info)
1227 struct serial_struct new_serial;
1228 unsigned long sl_flags;
1232 if (!new_info || !info->ioaddr)
1234 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1237 if ((new_serial.irq != info->board->irq) ||
1238 (new_serial.port != info->ioaddr) ||
1239 (new_serial.custom_divisor != info->custom_divisor) ||
1240 (new_serial.baud_base != info->baud_base))
1243 flags = info->flags & ASYNC_SPD_MASK;
1245 if (!capable(CAP_SYS_ADMIN)) {
1246 if ((new_serial.baud_base != info->baud_base) ||
1247 (new_serial.close_delay != info->close_delay) ||
1248 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
1250 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1251 (new_serial.flags & ASYNC_USR_MASK));
1254 * OK, past this point, all the error checking has been done.
1255 * At this point, we start making changes.....
1257 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1258 (new_serial.flags & ASYNC_FLAGS));
1259 info->close_delay = new_serial.close_delay * HZ / 100;
1260 info->closing_wait = new_serial.closing_wait * HZ / 100;
1261 info->tty->low_latency =
1262 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1263 info->tty->low_latency = 0;
1266 info->type = new_serial.type;
1268 process_txrx_fifo(info);
1270 if (info->flags & ASYNC_INITIALIZED) {
1271 if (flags != (info->flags & ASYNC_SPD_MASK)) {
1272 spin_lock_irqsave(&info->slock, sl_flags);
1273 mxser_change_speed(info, NULL);
1274 spin_unlock_irqrestore(&info->slock, sl_flags);
1277 retval = mxser_startup(info);
1283 * mxser_get_lsr_info - get line status register info
1285 * Purpose: Let user call ioctl() to get info when the UART physically
1286 * is emptied. On bus types like RS485, the transmitter must
1287 * release the bus after transmitting. This must be done when
1288 * the transmit shift register is empty, not be done when the
1289 * transmit holding register is empty. This functionality
1290 * allows an RS485 driver to be written in user space.
1292 static int mxser_get_lsr_info(struct mxser_port *info,
1293 unsigned int __user *value)
1295 unsigned char status;
1296 unsigned int result;
1297 unsigned long flags;
1299 spin_lock_irqsave(&info->slock, flags);
1300 status = inb(info->ioaddr + UART_LSR);
1301 spin_unlock_irqrestore(&info->slock, flags);
1302 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1303 return put_user(result, value);
1307 * This routine sends a break character out the serial port.
1309 static void mxser_send_break(struct mxser_port *info, int duration)
1311 unsigned long flags;
1315 set_current_state(TASK_INTERRUPTIBLE);
1316 spin_lock_irqsave(&info->slock, flags);
1317 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1318 info->ioaddr + UART_LCR);
1319 spin_unlock_irqrestore(&info->slock, flags);
1320 schedule_timeout(duration);
1321 spin_lock_irqsave(&info->slock, flags);
1322 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1323 info->ioaddr + UART_LCR);
1324 spin_unlock_irqrestore(&info->slock, flags);
1327 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
1329 struct mxser_port *info = tty->driver_data;
1330 unsigned char control, status;
1331 unsigned long flags;
1334 if (tty->index == MXSER_PORTS)
1335 return -ENOIOCTLCMD;
1336 if (test_bit(TTY_IO_ERROR, &tty->flags))
1339 control = info->MCR;
1341 spin_lock_irqsave(&info->slock, flags);
1342 status = inb(info->ioaddr + UART_MSR);
1343 if (status & UART_MSR_ANY_DELTA)
1344 mxser_check_modem_status(info, status);
1345 spin_unlock_irqrestore(&info->slock, flags);
1346 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1347 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1348 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1349 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1350 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1351 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1354 static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1355 unsigned int set, unsigned int clear)
1357 struct mxser_port *info = tty->driver_data;
1358 unsigned long flags;
1361 if (tty->index == MXSER_PORTS)
1362 return -ENOIOCTLCMD;
1363 if (test_bit(TTY_IO_ERROR, &tty->flags))
1366 spin_lock_irqsave(&info->slock, flags);
1368 if (set & TIOCM_RTS)
1369 info->MCR |= UART_MCR_RTS;
1370 if (set & TIOCM_DTR)
1371 info->MCR |= UART_MCR_DTR;
1373 if (clear & TIOCM_RTS)
1374 info->MCR &= ~UART_MCR_RTS;
1375 if (clear & TIOCM_DTR)
1376 info->MCR &= ~UART_MCR_DTR;
1378 outb(info->MCR, info->ioaddr + UART_MCR);
1379 spin_unlock_irqrestore(&info->slock, flags);
1383 static int __init mxser_program_mode(int port)
1395 id = inb(port + 1) & 0x1F;
1396 if ((id != C168_ASIC_ID) &&
1397 (id != C104_ASIC_ID) &&
1398 (id != C102_ASIC_ID) &&
1399 (id != CI132_ASIC_ID) &&
1400 (id != CI134_ASIC_ID) &&
1401 (id != CI104J_ASIC_ID))
1403 for (i = 0, j = 0; i < 4; i++) {
1407 } else if ((j == 1) && (n == 1)) {
1418 static void __init mxser_normal_mode(int port)
1422 outb(0xA5, port + 1);
1423 outb(0x80, port + 3);
1424 outb(12, port + 0); /* 9600 bps */
1426 outb(0x03, port + 3); /* 8 data bits */
1427 outb(0x13, port + 4); /* loop back mode */
1428 for (i = 0; i < 16; i++) {
1430 if ((n & 0x61) == 0x60)
1435 outb(0x00, port + 4);
1438 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1439 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1440 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1441 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1442 #define EN_CCMD 0x000 /* Chip's command register */
1443 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1444 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1445 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1446 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1447 #define EN0_DCFG 0x00E /* Data configuration reg WR */
1448 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1449 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1450 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1451 static int __init mxser_read_register(int port, unsigned short *regs)
1453 int i, k, value, id;
1456 id = mxser_program_mode(port);
1459 for (i = 0; i < 14; i++) {
1460 k = (i & 0x3F) | 0x180;
1461 for (j = 0x100; j > 0; j >>= 1) {
1462 outb(CHIP_CS, port);
1464 outb(CHIP_CS | CHIP_DO, port);
1465 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1467 outb(CHIP_CS, port);
1468 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1473 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1474 outb(CHIP_CS, port);
1475 outb(CHIP_CS | CHIP_SK, port);
1476 if (inb(port) & CHIP_DI)
1482 mxser_normal_mode(port);
1486 static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1488 struct mxser_port *port;
1494 /* if (copy_to_user(argp, mxsercfg,
1495 sizeof(struct mxser_hwconf) * 4))
1499 case MOXA_GET_MAJOR:
1500 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1504 case MOXA_GET_CUMAJOR:
1505 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1509 case MOXA_CHKPORTENABLE:
1512 for (i = 0; i < MXSER_BOARDS; i++)
1513 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1514 if (mxser_boards[i].ports[j].ioaddr)
1517 return put_user(result, (unsigned long __user *)argp);
1518 case MOXA_GETDATACOUNT:
1519 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1522 case MOXA_GETMSTATUS:
1523 for (i = 0; i < MXSER_BOARDS; i++)
1524 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1525 port = &mxser_boards[i].ports[j];
1528 if (!port->ioaddr) {
1529 GMStatus[i].dcd = 0;
1530 GMStatus[i].dsr = 0;
1531 GMStatus[i].cts = 0;
1535 if (!port->tty || !port->tty->termios)
1537 port->normal_termios.c_cflag;
1540 port->tty->termios->c_cflag;
1542 status = inb(port->ioaddr + UART_MSR);
1543 if (status & 0x80 /*UART_MSR_DCD */ )
1544 GMStatus[i].dcd = 1;
1546 GMStatus[i].dcd = 0;
1548 if (status & 0x20 /*UART_MSR_DSR */ )
1549 GMStatus[i].dsr = 1;
1551 GMStatus[i].dsr = 0;
1554 if (status & 0x10 /*UART_MSR_CTS */ )
1555 GMStatus[i].cts = 1;
1557 GMStatus[i].cts = 0;
1559 if (copy_to_user(argp, GMStatus,
1560 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1563 case MOXA_ASPP_MON_EXT: {
1564 int status, p, shiftbit;
1565 unsigned long opmode;
1566 unsigned cflag, iflag;
1568 for (i = 0; i < MXSER_BOARDS; i++)
1569 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1570 port = &mxser_boards[i].ports[j];
1574 status = mxser_get_msr(port->ioaddr, 0, i);
1576 if (status & UART_MSR_TERI)
1578 if (status & UART_MSR_DDSR)
1580 if (status & UART_MSR_DDCD)
1582 if (status & UART_MSR_DCTS)
1585 port->mon_data.modem_status = status;
1586 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1587 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1588 mon_data_ext.up_rxcnt[i] =
1589 port->mon_data.up_rxcnt;
1590 mon_data_ext.up_txcnt[i] =
1591 port->mon_data.up_txcnt;
1592 mon_data_ext.modem_status[i] =
1593 port->mon_data.modem_status;
1594 mon_data_ext.baudrate[i] = port->realbaud;
1596 if (!port->tty || !port->tty->termios) {
1597 cflag = port->normal_termios.c_cflag;
1598 iflag = port->normal_termios.c_iflag;
1600 cflag = port->tty->termios->c_cflag;
1601 iflag = port->tty->termios->c_iflag;
1604 mon_data_ext.databits[i] = cflag & CSIZE;
1606 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1608 mon_data_ext.parity[i] =
1609 cflag & (PARENB | PARODD | CMSPAR);
1611 mon_data_ext.flowctrl[i] = 0x00;
1613 if (cflag & CRTSCTS)
1614 mon_data_ext.flowctrl[i] |= 0x03;
1616 if (iflag & (IXON | IXOFF))
1617 mon_data_ext.flowctrl[i] |= 0x0C;
1619 if (port->type == PORT_16550A)
1620 mon_data_ext.fifo[i] = 1;
1622 mon_data_ext.fifo[i] = 0;
1626 opmode = inb(port->opmode_ioaddr) >> shiftbit;
1627 opmode &= OP_MODE_MASK;
1629 mon_data_ext.iftype[i] = opmode;
1632 if (copy_to_user(argp, &mon_data_ext,
1633 sizeof(mon_data_ext)))
1639 return -ENOIOCTLCMD;
1644 static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1645 unsigned int cmd, unsigned long arg)
1647 struct mxser_port *info = tty->driver_data;
1648 struct async_icount cprev, cnow; /* kernel counter temps */
1649 struct serial_icounter_struct __user *p_cuser;
1650 unsigned long templ;
1651 unsigned long flags;
1653 void __user *argp = (void __user *)arg;
1656 if (tty->index == MXSER_PORTS)
1657 return mxser_ioctl_special(cmd, argp);
1659 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1661 unsigned long opmode;
1662 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1664 unsigned char val, mask;
1667 if (cmd == MOXA_SET_OP_MODE) {
1668 if (get_user(opmode, (int __user *) argp))
1670 if (opmode != RS232_MODE &&
1671 opmode != RS485_2WIRE_MODE &&
1672 opmode != RS422_MODE &&
1673 opmode != RS485_4WIRE_MODE)
1677 val = inb(info->opmode_ioaddr);
1679 val |= (opmode << shiftbit);
1680 outb(val, info->opmode_ioaddr);
1683 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1684 opmode &= OP_MODE_MASK;
1685 if (copy_to_user(argp, &opmode, sizeof(int)))
1691 if (cmd == MOXA_SET_SPECIAL_BAUD_RATE) {
1694 if (get_user(speed, (int __user *)argp))
1696 if (speed <= 0 || speed > info->max_baud)
1698 if (!info->tty || !info->tty->termios || !info->ioaddr)
1700 info->tty->termios->c_cflag &= ~(CBAUD | CBAUDEX);
1701 for (i = 0; i < BAUD_TABLE_NO; i++)
1702 if (speed == mxvar_baud_table[i])
1704 if (i == BAUD_TABLE_NO) {
1705 info->tty->termios->c_cflag |= B_SPEC;
1706 } else if (speed != 0)
1707 info->tty->termios->c_cflag |= mxvar_baud_table1[i];
1709 info->speed = speed;
1710 spin_lock_irqsave(&info->slock, flags);
1711 mxser_change_speed(info, NULL);
1712 spin_unlock_irqrestore(&info->slock, flags);
1715 } else if (cmd == MOXA_GET_SPECIAL_BAUD_RATE) {
1716 if (copy_to_user(argp, &info->speed, sizeof(int)))
1721 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1722 test_bit(TTY_IO_ERROR, &tty->flags))
1726 case TCSBRK: /* SVID version: non-zero arg --> no break */
1727 retval = tty_check_change(tty);
1730 tty_wait_until_sent(tty, 0);
1732 mxser_send_break(info, HZ / 4); /* 1/4 second */
1734 case TCSBRKP: /* support for POSIX tcsendbreak() */
1735 retval = tty_check_change(tty);
1738 tty_wait_until_sent(tty, 0);
1739 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1742 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1744 if (get_user(templ, (unsigned long __user *) argp))
1747 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1750 return mxser_get_serial_info(info, argp);
1752 return mxser_set_serial_info(info, argp);
1753 case TIOCSERGETLSR: /* Get line status register */
1754 return mxser_get_lsr_info(info, argp);
1756 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1757 * - mask passed in arg for lines of interest
1758 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1759 * Caller should use TIOCGICOUNT to see which one it was
1762 spin_lock_irqsave(&info->slock, flags);
1763 cnow = info->icount; /* note the counters on entry */
1764 spin_unlock_irqrestore(&info->slock, flags);
1766 wait_event_interruptible(info->delta_msr_wait, ({
1768 spin_lock_irqsave(&info->slock, flags);
1769 cnow = info->icount; /* atomic copy */
1770 spin_unlock_irqrestore(&info->slock, flags);
1772 ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1773 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1774 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1775 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts));
1779 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1780 * Return: write counters to the user passed counter struct
1781 * NB: both 1->0 and 0->1 transitions are counted except for
1782 * RI where only 0->1 is counted.
1785 spin_lock_irqsave(&info->slock, flags);
1786 cnow = info->icount;
1787 spin_unlock_irqrestore(&info->slock, flags);
1789 if (put_user(cnow.frame, &p_cuser->frame))
1791 if (put_user(cnow.brk, &p_cuser->brk))
1793 if (put_user(cnow.overrun, &p_cuser->overrun))
1795 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1797 if (put_user(cnow.parity, &p_cuser->parity))
1799 if (put_user(cnow.rx, &p_cuser->rx))
1801 if (put_user(cnow.tx, &p_cuser->tx))
1803 put_user(cnow.cts, &p_cuser->cts);
1804 put_user(cnow.dsr, &p_cuser->dsr);
1805 put_user(cnow.rng, &p_cuser->rng);
1806 put_user(cnow.dcd, &p_cuser->dcd);
1808 case MOXA_HighSpeedOn:
1809 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1810 case MOXA_SDS_RSTICOUNTER:
1811 info->mon_data.rxcnt = 0;
1812 info->mon_data.txcnt = 0;
1814 case MOXA_ASPP_SETBAUD:{
1816 if (get_user(baud, (long __user *)argp))
1818 spin_lock_irqsave(&info->slock, flags);
1819 mxser_set_baud(info, baud);
1820 spin_unlock_irqrestore(&info->slock, flags);
1823 case MOXA_ASPP_GETBAUD:
1824 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1829 case MOXA_ASPP_OQUEUE:{
1832 len = mxser_chars_in_buffer(tty);
1834 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1836 len += (lsr ? 0 : 1);
1838 if (copy_to_user(argp, &len, sizeof(int)))
1843 case MOXA_ASPP_MON: {
1846 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1847 mxser_check_modem_status(info, status);
1849 mcr = inb(info->ioaddr + UART_MCR);
1850 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1851 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1853 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1855 if (mcr & MOXA_MUST_MCR_TX_XON)
1856 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1858 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1860 if (info->tty->hw_stopped)
1861 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1863 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1865 if (copy_to_user(argp, &info->mon_data,
1866 sizeof(struct mxser_mon)))
1871 case MOXA_ASPP_LSTATUS: {
1872 if (copy_to_user(argp, &info->err_shadow,
1873 sizeof(unsigned char)))
1876 info->err_shadow = 0;
1879 case MOXA_SET_BAUD_METHOD: {
1882 if (get_user(method, (int __user *)argp))
1884 mxser_set_baud_method[tty->index] = method;
1885 if (copy_to_user(argp, &method, sizeof(int)))
1891 return -ENOIOCTLCMD;
1896 static void mxser_stoprx(struct tty_struct *tty)
1898 struct mxser_port *info = tty->driver_data;
1900 info->ldisc_stop_rx = 1;
1902 if (info->board->chip_flag) {
1903 info->IER &= ~MOXA_MUST_RECV_ISR;
1904 outb(info->IER, info->ioaddr + UART_IER);
1906 info->x_char = STOP_CHAR(tty);
1907 outb(0, info->ioaddr + UART_IER);
1908 info->IER |= UART_IER_THRI;
1909 outb(info->IER, info->ioaddr + UART_IER);
1913 if (info->tty->termios->c_cflag & CRTSCTS) {
1914 info->MCR &= ~UART_MCR_RTS;
1915 outb(info->MCR, info->ioaddr + UART_MCR);
1920 * This routine is called by the upper-layer tty layer to signal that
1921 * incoming characters should be throttled.
1923 static void mxser_throttle(struct tty_struct *tty)
1928 static void mxser_unthrottle(struct tty_struct *tty)
1930 struct mxser_port *info = tty->driver_data;
1933 info->ldisc_stop_rx = 0;
1938 if (info->board->chip_flag) {
1939 info->IER |= MOXA_MUST_RECV_ISR;
1940 outb(info->IER, info->ioaddr + UART_IER);
1942 info->x_char = START_CHAR(tty);
1943 outb(0, info->ioaddr + UART_IER);
1944 info->IER |= UART_IER_THRI;
1945 outb(info->IER, info->ioaddr + UART_IER);
1950 if (info->tty->termios->c_cflag & CRTSCTS) {
1951 info->MCR |= UART_MCR_RTS;
1952 outb(info->MCR, info->ioaddr + UART_MCR);
1957 * mxser_stop() and mxser_start()
1959 * This routines are called before setting or resetting tty->stopped.
1960 * They enable or disable transmitter interrupts, as necessary.
1962 static void mxser_stop(struct tty_struct *tty)
1964 struct mxser_port *info = tty->driver_data;
1965 unsigned long flags;
1967 spin_lock_irqsave(&info->slock, flags);
1968 if (info->IER & UART_IER_THRI) {
1969 info->IER &= ~UART_IER_THRI;
1970 outb(info->IER, info->ioaddr + UART_IER);
1972 spin_unlock_irqrestore(&info->slock, flags);
1975 static void mxser_start(struct tty_struct *tty)
1977 struct mxser_port *info = tty->driver_data;
1978 unsigned long flags;
1980 spin_lock_irqsave(&info->slock, flags);
1981 if (info->xmit_cnt && info->xmit_buf) {
1982 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1983 info->IER |= UART_IER_THRI;
1984 outb(info->IER, info->ioaddr + UART_IER);
1986 spin_unlock_irqrestore(&info->slock, flags);
1989 static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1991 struct mxser_port *info = tty->driver_data;
1992 unsigned long flags;
1994 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1995 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
1997 spin_lock_irqsave(&info->slock, flags);
1998 mxser_change_speed(info, old_termios);
1999 spin_unlock_irqrestore(&info->slock, flags);
2001 if ((old_termios->c_cflag & CRTSCTS) &&
2002 !(tty->termios->c_cflag & CRTSCTS)) {
2003 tty->hw_stopped = 0;
2008 /* Handle sw stopped */
2009 if ((old_termios->c_iflag & IXON) &&
2010 !(tty->termios->c_iflag & IXON)) {
2013 if (info->board->chip_flag) {
2014 spin_lock_irqsave(&info->slock, flags);
2015 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2016 spin_unlock_irqrestore(&info->slock, flags);
2024 * mxser_wait_until_sent() --- wait until the transmitter is empty
2026 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2028 struct mxser_port *info = tty->driver_data;
2029 unsigned long orig_jiffies, char_time;
2032 if (info->type == PORT_UNKNOWN)
2035 if (info->xmit_fifo_size == 0)
2036 return; /* Just in case.... */
2038 orig_jiffies = jiffies;
2040 * Set the check interval to be 1/5 of the estimated time to
2041 * send a single character, and make it at least 1. The check
2042 * interval should also be less than the timeout.
2044 * Note: we have to use pretty tight timings here to satisfy
2047 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2048 char_time = char_time / 5;
2051 if (timeout && timeout < char_time)
2052 char_time = timeout;
2054 * If the transmitter hasn't cleared in twice the approximate
2055 * amount of time to send the entire FIFO, it probably won't
2056 * ever clear. This assumes the UART isn't doing flow
2057 * control, which is currently the case. Hence, if it ever
2058 * takes longer than info->timeout, this is probably due to a
2059 * UART bug of some kind. So, we clamp the timeout parameter at
2062 if (!timeout || timeout > 2 * info->timeout)
2063 timeout = 2 * info->timeout;
2064 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2065 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2066 timeout, char_time);
2067 printk("jiff=%lu...", jiffies);
2069 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
2070 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2071 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2073 schedule_timeout_interruptible(char_time);
2074 if (signal_pending(current))
2076 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2079 set_current_state(TASK_RUNNING);
2081 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2082 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2087 * This routine is called by tty_hangup() when a hangup is signaled.
2089 static void mxser_hangup(struct tty_struct *tty)
2091 struct mxser_port *info = tty->driver_data;
2093 mxser_flush_buffer(tty);
2094 mxser_shutdown(info);
2097 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2099 wake_up_interruptible(&info->open_wait);
2103 * mxser_rs_break() --- routine which turns the break handling on or off
2105 static void mxser_rs_break(struct tty_struct *tty, int break_state)
2107 struct mxser_port *info = tty->driver_data;
2108 unsigned long flags;
2110 spin_lock_irqsave(&info->slock, flags);
2111 if (break_state == -1)
2112 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2113 info->ioaddr + UART_LCR);
2115 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2116 info->ioaddr + UART_LCR);
2117 spin_unlock_irqrestore(&info->slock, flags);
2120 static void mxser_receive_chars(struct mxser_port *port, int *status)
2122 struct tty_struct *tty = port->tty;
2123 unsigned char ch, gdl;
2129 recv_room = tty->receive_room;
2130 if ((recv_room == 0) && (!port->ldisc_stop_rx))
2133 if (port->board->chip_flag != MOXA_OTHER_UART) {
2135 if (*status & UART_LSR_SPECIAL)
2137 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
2138 (*status & MOXA_MUST_LSR_RERR))
2140 if (*status & MOXA_MUST_LSR_RERR)
2143 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
2145 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
2146 gdl &= MOXA_MUST_GDL_MASK;
2147 if (gdl >= recv_room) {
2148 if (!port->ldisc_stop_rx)
2152 ch = inb(port->ioaddr + UART_RX);
2153 tty_insert_flip_char(tty, ch, 0);
2164 ch = inb(port->ioaddr + UART_RX);
2165 if (port->board->chip_flag && (*status & UART_LSR_OE))
2166 outb(0x23, port->ioaddr + UART_FCR);
2167 *status &= port->read_status_mask;
2168 if (*status & port->ignore_status_mask) {
2169 if (++ignored > 100)
2173 if (*status & UART_LSR_SPECIAL) {
2174 if (*status & UART_LSR_BI) {
2178 if (port->flags & ASYNC_SAK)
2180 } else if (*status & UART_LSR_PE) {
2182 port->icount.parity++;
2183 } else if (*status & UART_LSR_FE) {
2185 port->icount.frame++;
2186 } else if (*status & UART_LSR_OE) {
2188 port->icount.overrun++;
2192 tty_insert_flip_char(tty, ch, flag);
2194 if (cnt >= recv_room) {
2195 if (!port->ldisc_stop_rx)
2202 if (port->board->chip_flag)
2205 *status = inb(port->ioaddr + UART_LSR);
2206 } while (*status & UART_LSR_DR);
2209 mxvar_log.rxcnt[port->tty->index] += cnt;
2210 port->mon_data.rxcnt += cnt;
2211 port->mon_data.up_rxcnt += cnt;
2214 * We are called from an interrupt context with &port->slock
2215 * being held. Drop it temporarily in order to prevent
2216 * recursive locking.
2218 spin_unlock(&port->slock);
2219 tty_flip_buffer_push(tty);
2220 spin_lock(&port->slock);
2223 static void mxser_transmit_chars(struct mxser_port *port)
2228 outb(port->x_char, port->ioaddr + UART_TX);
2230 mxvar_log.txcnt[port->tty->index]++;
2231 port->mon_data.txcnt++;
2232 port->mon_data.up_txcnt++;
2237 if (port->xmit_buf == 0)
2240 if ((port->xmit_cnt <= 0) || port->tty->stopped ||
2241 (port->tty->hw_stopped &&
2242 (port->type != PORT_16550A) &&
2243 (!port->board->chip_flag))) {
2244 port->IER &= ~UART_IER_THRI;
2245 outb(port->IER, port->ioaddr + UART_IER);
2249 cnt = port->xmit_cnt;
2250 count = port->xmit_fifo_size;
2252 outb(port->xmit_buf[port->xmit_tail++],
2253 port->ioaddr + UART_TX);
2254 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2255 if (--port->xmit_cnt <= 0)
2257 } while (--count > 0);
2258 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
2260 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2261 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2262 port->icount.tx += (cnt - port->xmit_cnt);
2264 if (port->xmit_cnt < WAKEUP_CHARS)
2265 tty_wakeup(port->tty);
2267 if (port->xmit_cnt <= 0) {
2268 port->IER &= ~UART_IER_THRI;
2269 outb(port->IER, port->ioaddr + UART_IER);
2274 * This is the serial driver's generic interrupt routine
2276 static irqreturn_t mxser_interrupt(int irq, void *dev_id)
2279 struct mxser_board *brd = NULL;
2280 struct mxser_port *port;
2281 int max, irqbits, bits, msr;
2282 unsigned int int_cnt, pass_counter = 0;
2283 int handled = IRQ_NONE;
2285 for (i = 0; i < MXSER_BOARDS; i++)
2286 if (dev_id == &mxser_boards[i]) {
2291 if (i == MXSER_BOARDS)
2295 max = brd->info->nports;
2296 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
2297 irqbits = inb(brd->vector) & brd->vector_mask;
2298 if (irqbits == brd->vector_mask)
2301 handled = IRQ_HANDLED;
2302 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2303 if (irqbits == brd->vector_mask)
2307 port = &brd->ports[i];
2310 spin_lock(&port->slock);
2312 iir = inb(port->ioaddr + UART_IIR);
2313 if (iir & UART_IIR_NO_INT)
2315 iir &= MOXA_MUST_IIR_MASK;
2317 (port->flags & ASYNC_CLOSING) ||
2319 ASYNC_INITIALIZED)) {
2320 status = inb(port->ioaddr + UART_LSR);
2321 outb(0x27, port->ioaddr + UART_FCR);
2322 inb(port->ioaddr + UART_MSR);
2326 status = inb(port->ioaddr + UART_LSR);
2328 if (status & UART_LSR_PE)
2329 port->err_shadow |= NPPI_NOTIFY_PARITY;
2330 if (status & UART_LSR_FE)
2331 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2332 if (status & UART_LSR_OE)
2334 NPPI_NOTIFY_HW_OVERRUN;
2335 if (status & UART_LSR_BI)
2336 port->err_shadow |= NPPI_NOTIFY_BREAK;
2338 if (port->board->chip_flag) {
2339 if (iir == MOXA_MUST_IIR_GDA ||
2340 iir == MOXA_MUST_IIR_RDA ||
2341 iir == MOXA_MUST_IIR_RTO ||
2342 iir == MOXA_MUST_IIR_LSR)
2343 mxser_receive_chars(port,
2347 status &= port->read_status_mask;
2348 if (status & UART_LSR_DR)
2349 mxser_receive_chars(port,
2352 msr = inb(port->ioaddr + UART_MSR);
2353 if (msr & UART_MSR_ANY_DELTA)
2354 mxser_check_modem_status(port, msr);
2356 if (port->board->chip_flag) {
2357 if (iir == 0x02 && (status &
2359 mxser_transmit_chars(port);
2361 if (status & UART_LSR_THRE)
2362 mxser_transmit_chars(port);
2364 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2365 spin_unlock(&port->slock);
2373 static const struct tty_operations mxser_ops = {
2375 .close = mxser_close,
2376 .write = mxser_write,
2377 .put_char = mxser_put_char,
2378 .flush_chars = mxser_flush_chars,
2379 .write_room = mxser_write_room,
2380 .chars_in_buffer = mxser_chars_in_buffer,
2381 .flush_buffer = mxser_flush_buffer,
2382 .ioctl = mxser_ioctl,
2383 .throttle = mxser_throttle,
2384 .unthrottle = mxser_unthrottle,
2385 .set_termios = mxser_set_termios,
2387 .start = mxser_start,
2388 .hangup = mxser_hangup,
2389 .break_ctl = mxser_rs_break,
2390 .wait_until_sent = mxser_wait_until_sent,
2391 .tiocmget = mxser_tiocmget,
2392 .tiocmset = mxser_tiocmset,
2396 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2399 static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
2403 free_irq(brd->irq, brd);
2404 if (pdev != NULL) { /* PCI */
2406 pci_release_region(pdev, 2);
2407 pci_release_region(pdev, 3);
2410 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2411 release_region(brd->vector, 1);
2415 static int __devinit mxser_initbrd(struct mxser_board *brd,
2416 struct pci_dev *pdev)
2418 struct mxser_port *info;
2422 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
2424 for (i = 0; i < brd->info->nports; i++) {
2425 info = &brd->ports[i];
2428 info->ldisc_stop_rx = 0;
2430 /* Enhance mode enabled here */
2431 if (brd->chip_flag != MOXA_OTHER_UART)
2432 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
2434 info->flags = ASYNC_SHARE_IRQ;
2435 info->type = brd->uart_type;
2437 process_txrx_fifo(info);
2439 info->custom_divisor = info->baud_base * 16;
2440 info->close_delay = 5 * HZ / 10;
2441 info->closing_wait = 30 * HZ;
2442 info->normal_termios = mxvar_sdriver->init_termios;
2443 init_waitqueue_head(&info->open_wait);
2444 init_waitqueue_head(&info->delta_msr_wait);
2446 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2447 info->err_shadow = 0;
2448 spin_lock_init(&info->slock);
2450 /* before set INT ISR, disable all int */
2451 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2452 info->ioaddr + UART_IER);
2455 retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
2458 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2459 "conflict with another device.\n",
2460 brd->info->name, brd->irq);
2461 /* We hold resources, we need to release them. */
2462 mxser_release_res(brd, pdev, 0);
2467 static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
2470 unsigned short regs[16], irq;
2471 unsigned char scratch, scratch2;
2473 brd->chip_flag = MOXA_OTHER_UART;
2475 id = mxser_read_register(cap, regs);
2478 brd->info = &mxser_cards[0];
2481 brd->info = &mxser_cards[1];
2483 case CI104J_ASIC_ID:
2484 brd->info = &mxser_cards[2];
2487 brd->info = &mxser_cards[5];
2490 brd->info = &mxser_cards[6];
2493 brd->info = &mxser_cards[7];
2500 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2501 Flag-hack checks if configuration should be read as 2-port here. */
2502 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
2503 irq = regs[9] & 0xF000;
2504 irq = irq | (irq >> 4);
2505 if (irq != (regs[9] & 0xFF00))
2506 return MXSER_ERR_IRQ_CONFLIT;
2507 } else if (brd->info->nports == 4) {
2508 irq = regs[9] & 0xF000;
2509 irq = irq | (irq >> 4);
2510 irq = irq | (irq >> 8);
2512 return MXSER_ERR_IRQ_CONFLIT;
2513 } else if (brd->info->nports == 8) {
2514 irq = regs[9] & 0xF000;
2515 irq = irq | (irq >> 4);
2516 irq = irq | (irq >> 8);
2517 if ((irq != regs[9]) || (irq != regs[10]))
2518 return MXSER_ERR_IRQ_CONFLIT;
2522 return MXSER_ERR_IRQ;
2523 brd->irq = ((int)(irq & 0xF000) >> 12);
2524 for (i = 0; i < 8; i++)
2525 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
2526 if ((regs[12] & 0x80) == 0)
2527 return MXSER_ERR_VECTOR;
2528 brd->vector = (int)regs[11]; /* interrupt vector */
2530 brd->vector_mask = 0x00FF;
2532 brd->vector_mask = 0x000F;
2533 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2534 if (regs[12] & bits) {
2535 brd->ports[i].baud_base = 921600;
2536 brd->ports[i].max_baud = 921600;
2538 brd->ports[i].baud_base = 115200;
2539 brd->ports[i].max_baud = 115200;
2542 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2543 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2544 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2545 outb(scratch2, cap + UART_LCR);
2546 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2547 scratch = inb(cap + UART_IIR);
2550 brd->uart_type = PORT_16550A;
2552 brd->uart_type = PORT_16450;
2553 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2555 return MXSER_ERR_IOADDR;
2556 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2557 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2558 return MXSER_ERR_VECTOR;
2560 return brd->info->nports;
2563 static int __devinit mxser_probe(struct pci_dev *pdev,
2564 const struct pci_device_id *ent)
2567 struct mxser_board *brd;
2569 unsigned long ioaddress;
2570 int retval = -EINVAL;
2572 for (i = 0; i < MXSER_BOARDS; i++)
2573 if (mxser_boards[i].info == NULL)
2576 if (i >= MXSER_BOARDS) {
2577 printk(KERN_ERR "Too many Smartio/Industio family boards found "
2578 "(maximum %d), board not configured\n", MXSER_BOARDS);
2582 brd = &mxser_boards[i];
2583 brd->idx = i * MXSER_PORTS_PER_BOARD;
2584 printk(KERN_INFO "Found MOXA %s board (BusNo=%d, DevNo=%d)\n",
2585 mxser_cards[ent->driver_data].name,
2586 pdev->bus->number, PCI_SLOT(pdev->devfn));
2588 retval = pci_enable_device(pdev);
2590 printk(KERN_ERR "Moxa SmartI/O PCI enable fail !\n");
2595 ioaddress = pci_resource_start(pdev, 2);
2596 retval = pci_request_region(pdev, 2, "mxser(IO)");
2600 brd->info = &mxser_cards[ent->driver_data];
2601 for (i = 0; i < brd->info->nports; i++)
2602 brd->ports[i].ioaddr = ioaddress + 8 * i;
2605 ioaddress = pci_resource_start(pdev, 3);
2606 retval = pci_request_region(pdev, 3, "mxser(vector)");
2609 brd->vector = ioaddress;
2612 brd->irq = pdev->irq;
2614 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2615 brd->uart_type = PORT_16550A;
2616 brd->vector_mask = 0;
2618 for (i = 0; i < brd->info->nports; i++) {
2619 for (j = 0; j < UART_INFO_NUM; j++) {
2620 if (Gpci_uart_info[j].type == brd->chip_flag) {
2621 brd->ports[i].max_baud =
2622 Gpci_uart_info[j].max_baud;
2624 /* exception....CP-102 */
2625 if (brd->info->flags & MXSER_HIGHBAUD)
2626 brd->ports[i].max_baud = 921600;
2632 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2633 for (i = 0; i < brd->info->nports; i++) {
2635 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2637 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
2639 outb(0, ioaddress + 4); /* default set to RS232 mode */
2640 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
2643 for (i = 0; i < brd->info->nports; i++) {
2644 brd->vector_mask |= (1 << i);
2645 brd->ports[i].baud_base = 921600;
2648 /* mxser_initbrd will hook ISR. */
2649 retval = mxser_initbrd(brd, pdev);
2653 for (i = 0; i < brd->info->nports; i++)
2654 tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
2656 pci_set_drvdata(pdev, brd);
2660 pci_release_region(pdev, 2);
2670 static void __devexit mxser_remove(struct pci_dev *pdev)
2672 struct mxser_board *brd = pci_get_drvdata(pdev);
2675 for (i = 0; i < brd->info->nports; i++)
2676 tty_unregister_device(mxvar_sdriver, brd->idx + i);
2678 mxser_release_res(brd, pdev, 1);
2682 static struct pci_driver mxser_driver = {
2684 .id_table = mxser_pcibrds,
2685 .probe = mxser_probe,
2686 .remove = __devexit_p(mxser_remove)
2689 static int __init mxser_module_init(void)
2691 struct mxser_board *brd;
2693 unsigned int i, m, isaloop;
2696 pr_debug("Loading module mxser ...\n");
2698 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2702 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2705 /* Initialize the tty_driver structure */
2706 mxvar_sdriver->owner = THIS_MODULE;
2707 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2708 mxvar_sdriver->name = "ttyMI";
2709 mxvar_sdriver->major = ttymajor;
2710 mxvar_sdriver->minor_start = 0;
2711 mxvar_sdriver->num = MXSER_PORTS + 1;
2712 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2713 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2714 mxvar_sdriver->init_termios = tty_std_termios;
2715 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2716 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2717 tty_set_operations(mxvar_sdriver, &mxser_ops);
2719 retval = tty_register_driver(mxvar_sdriver);
2721 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2729 /* Start finding ISA boards here */
2730 for (isaloop = 0; isaloop < 2; isaloop++)
2731 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2733 cap = mxserBoardCAP[b]; /* predefined */
2735 cap = ioaddr[b]; /* module param */
2740 brd = &mxser_boards[m];
2741 retval = mxser_get_ISA_conf(cap, brd);
2744 printk(KERN_INFO "Found MOXA %s board "
2746 brd->info->name, ioaddr[b]);
2749 if (retval == MXSER_ERR_IRQ)
2750 printk(KERN_ERR "Invalid interrupt "
2751 "number, board not "
2753 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2754 printk(KERN_ERR "Invalid interrupt "
2755 "number, board not "
2757 else if (retval == MXSER_ERR_VECTOR)
2758 printk(KERN_ERR "Invalid interrupt "
2759 "vector, board not "
2761 else if (retval == MXSER_ERR_IOADDR)
2762 printk(KERN_ERR "Invalid I/O address, "
2763 "board not configured\n");
2769 /* mxser_initbrd will hook ISR. */
2770 if (mxser_initbrd(brd, NULL) < 0) {
2775 brd->idx = m * MXSER_PORTS_PER_BOARD;
2776 for (i = 0; i < brd->info->nports; i++)
2777 tty_register_device(mxvar_sdriver, brd->idx + i,
2783 retval = pci_register_driver(&mxser_driver);
2785 printk(KERN_ERR "Can't register pci driver\n");
2789 } /* else: we have some ISA cards under control */
2792 pr_debug("Done.\n");
2796 tty_unregister_driver(mxvar_sdriver);
2798 put_tty_driver(mxvar_sdriver);
2802 static void __exit mxser_module_exit(void)
2806 pr_debug("Unloading module mxser ...\n");
2808 pci_unregister_driver(&mxser_driver);
2810 for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
2811 if (mxser_boards[i].info != NULL)
2812 for (j = 0; j < mxser_boards[i].info->nports; j++)
2813 tty_unregister_device(mxvar_sdriver,
2814 mxser_boards[i].idx + j);
2815 tty_unregister_driver(mxvar_sdriver);
2816 put_tty_driver(mxvar_sdriver);
2818 for (i = 0; i < MXSER_BOARDS; i++)
2819 if (mxser_boards[i].info != NULL)
2820 mxser_release_res(&mxser_boards[i], NULL, 1);
2822 pr_debug("Done.\n");
2825 module_init(mxser_module_init);
2826 module_exit(mxser_module_exit);