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
57 #define MXSER_EVENT_HANGUP 2
59 #define MXSER_BOARDS 4 /* Max. boards */
60 #define MXSER_PORTS 32 /* Max. ports */
61 #define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
62 #define MXSER_ISR_PASS_LIMIT 99999L
64 #define MXSER_ERR_IOADDR -1
65 #define MXSER_ERR_IRQ -2
66 #define MXSER_ERR_IRQ_CONFLIT -3
67 #define MXSER_ERR_VECTOR -4
69 #define SERIAL_TYPE_NORMAL 1
70 #define SERIAL_TYPE_CALLOUT 2
72 #define WAKEUP_CHARS 256
74 #define UART_MCR_AFE 0x20
75 #define UART_LSR_SPECIAL 0x1E
77 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|\
80 #define C168_ASIC_ID 1
81 #define C104_ASIC_ID 2
82 #define C102_ASIC_ID 0xB
83 #define CI132_ASIC_ID 4
84 #define CI134_ASIC_ID 3
85 #define CI104J_ASIC_ID 5
87 #define MXSER_HIGHBAUD 1
90 /* This is only for PCI */
100 } Gpci_uart_info[] = {
101 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
102 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
103 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
105 #define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
107 struct mxser_cardinfo {
113 static const struct mxser_cardinfo mxser_cards[] = {
114 { 8, "C168 series", }, /* C168-ISA */
115 { 4, "C104 series", }, /* C104-ISA */
116 { 4, "CI-104J series", }, /* CI104J */
117 { 8, "C168H/PCI series", }, /* C168-PCI */
118 { 4, "C104H/PCI series", }, /* C104-PCI */
119 { 4, "C102 series", MXSER_HAS2 }, /* C102-ISA */
120 { 4, "CI-132 series", MXSER_HAS2 }, /* CI132 */
121 { 4, "CI-134 series", }, /* CI134 */
122 { 2, "CP-132 series", }, /* CP132 */
123 { 4, "CP-114 series", }, /* CP114 */
124 { 4, "CT-114 series", }, /* CT114 */
125 { 2, "CP-102 series", MXSER_HIGHBAUD }, /* CP102 */
126 { 4, "CP-104U series", }, /* CP104U */
127 { 8, "CP-168U series", }, /* CP168U */
128 { 2, "CP-132U series", }, /* CP132U */
129 { 4, "CP-134U series", }, /* CP134U */
130 { 4, "CP-104JU series", }, /* CP104JU */
131 { 8, "Moxa UC7000 Serial", }, /* RC7000 */
132 { 8, "CP-118U series", }, /* CP118U */
133 { 2, "CP-102UL series", }, /* CP102UL */
134 { 2, "CP-102U series", }, /* CP102U */
135 { 8, "CP-118EL series", }, /* CP118EL */
136 { 8, "CP-168EL series", }, /* CP168EL */
137 { 4, "CP-104EL series", } /* CP104EL */
140 /* driver_data correspond to the lines in the structure above
141 see also ISA probe function before you change something */
142 static struct pci_device_id mxser_pcibrds[] = {
143 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C168),
145 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C104),
147 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132),
149 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP114),
151 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CT114),
153 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102),
155 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104U),
157 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168U),
159 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132U),
161 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP134U),
163 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104JU),
165 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_RC7000),
167 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118U),
169 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102UL),
171 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102U),
173 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118EL),
175 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168EL),
177 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104EL),
181 MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
183 static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
184 static int ttymajor = MXSERMAJOR;
185 static int calloutmajor = MXSERCUMAJOR;
187 /* Variables for insmod */
189 MODULE_AUTHOR("Casper Yang");
190 MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
191 module_param_array(ioaddr, int, NULL, 0);
192 module_param(ttymajor, int, 0);
193 MODULE_LICENSE("GPL");
197 unsigned long rxcnt[MXSER_PORTS];
198 unsigned long txcnt[MXSER_PORTS];
205 unsigned long up_rxcnt;
206 unsigned long up_txcnt;
208 unsigned char hold_reason;
211 struct mxser_mon_ext {
212 unsigned long rx_cnt[32];
213 unsigned long tx_cnt[32];
214 unsigned long up_rxcnt[32];
215 unsigned long up_txcnt[32];
216 int modem_status[32];
230 struct mxser_board *board;
231 struct tty_struct *tty;
233 unsigned long ioaddr;
234 unsigned long opmode_ioaddr;
238 int rx_trigger; /* Rx fifo trigger level */
240 int baud_base; /* max. speed */
242 int type; /* UART type */
243 int flags; /* defined in tty.h */
244 long session; /* Session of opening process */
245 long pgrp; /* pgrp of opening process */
247 int x_char; /* xon/xoff character */
248 int IER; /* Interrupt Enable Register */
249 int MCR; /* Modem control register */
251 unsigned char stop_rx;
252 unsigned char ldisc_stop_rx;
256 unsigned short closing_wait;
257 unsigned char err_shadow;
260 int count; /* # of fd on device */
261 int blocked_open; /* # of blocked opens */
262 struct async_icount icount; /* kernel counters for 4 input interrupts */
265 int read_status_mask;
266 int ignore_status_mask;
268 unsigned char *xmit_buf;
273 struct termios normal_termios;
274 struct termios callout_termios;
276 struct mxser_mon mon_data;
279 struct work_struct tqueue;
280 wait_queue_head_t open_wait;
281 wait_queue_head_t close_wait;
282 wait_queue_head_t delta_msr_wait;
288 const struct mxser_cardinfo *info;
289 unsigned long vector;
290 unsigned long vector_mask;
295 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
298 struct mxser_mstatus {
306 static struct mxser_mstatus GMStatus[MXSER_PORTS];
308 static int mxserBoardCAP[MXSER_BOARDS] = {
310 /* 0x180, 0x280, 0x200, 0x320 */
313 static struct mxser_board mxser_boards[MXSER_BOARDS];
314 static struct tty_driver *mxvar_sdriver;
315 static struct mxser_log mxvar_log;
316 static int mxvar_diagflag;
317 static unsigned char mxser_msr[MXSER_PORTS + 1];
318 static struct mxser_mon_ext mon_data_ext;
319 static int mxser_set_baud_method[MXSER_PORTS + 1];
320 static spinlock_t gm_lock;
322 static int CheckIsMoxaMust(int io)
327 outb(0, io + UART_LCR);
328 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
329 oldmcr = inb(io + UART_MCR);
330 outb(0, io + UART_MCR);
331 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
332 if ((hwid = inb(io + UART_MCR)) != 0) {
333 outb(oldmcr, io + UART_MCR);
334 return MOXA_OTHER_UART;
337 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
338 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
339 if (hwid == Gpci_uart_info[i].type)
342 return MOXA_OTHER_UART;
345 static void process_txrx_fifo(struct mxser_port *info)
349 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
350 info->rx_trigger = 1;
351 info->rx_high_water = 1;
352 info->rx_low_water = 1;
353 info->xmit_fifo_size = 1;
355 for (i = 0; i < UART_INFO_NUM; i++)
356 if (info->board->chip_flag == Gpci_uart_info[i].type) {
357 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
358 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
359 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
360 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
365 static void mxser_do_softint(void *private_)
367 struct mxser_port *info = private_;
368 struct tty_struct *tty;
372 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
374 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event))
378 static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
380 unsigned char status = 0;
382 status = inb(baseaddr + UART_MSR);
384 mxser_msr[port] &= 0x0F;
385 mxser_msr[port] |= status;
386 status = mxser_msr[port];
393 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
394 struct mxser_port *port)
396 DECLARE_WAITQUEUE(wait, current);
402 * If non-blocking mode is set, or the port is not enabled,
403 * then make the check up front and then exit.
405 if ((filp->f_flags & O_NONBLOCK) ||
406 test_bit(TTY_IO_ERROR, &tty->flags)) {
407 port->flags |= ASYNC_NORMAL_ACTIVE;
411 if (tty->termios->c_cflag & CLOCAL)
415 * Block waiting for the carrier detect and the line to become
416 * free (i.e., not in use by the callout). While we are in
417 * this loop, port->count is dropped by one, so that
418 * mxser_close() knows when to free things. We restore it upon
419 * exit, either normal or abnormal.
422 add_wait_queue(&port->open_wait, &wait);
424 spin_lock_irqsave(&port->slock, flags);
425 if (!tty_hung_up_p(filp))
427 spin_unlock_irqrestore(&port->slock, flags);
428 port->blocked_open++;
430 spin_lock_irqsave(&port->slock, flags);
431 outb(inb(port->ioaddr + UART_MCR) |
432 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
433 spin_unlock_irqrestore(&port->slock, flags);
434 set_current_state(TASK_INTERRUPTIBLE);
435 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
436 if (port->flags & ASYNC_HUP_NOTIFY)
439 retval = -ERESTARTSYS;
442 if (!(port->flags & ASYNC_CLOSING) &&
444 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
446 if (signal_pending(current)) {
447 retval = -ERESTARTSYS;
452 set_current_state(TASK_RUNNING);
453 remove_wait_queue(&port->open_wait, &wait);
454 if (!tty_hung_up_p(filp))
456 port->blocked_open--;
459 port->flags |= ASYNC_NORMAL_ACTIVE;
463 static int mxser_set_baud(struct mxser_port *info, long newspd)
470 if (!info->tty || !info->tty->termios)
476 if (newspd > info->max_baud)
479 info->realbaud = newspd;
481 quot = (2 * info->baud_base / 269);
483 quot = info->baud_base / newspd;
490 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
491 info->timeout += HZ / 50; /* Add .02 seconds of slop */
494 spin_lock_irqsave(&info->slock, flags);
495 info->MCR |= UART_MCR_DTR;
496 outb(info->MCR, info->ioaddr + UART_MCR);
497 spin_unlock_irqrestore(&info->slock, flags);
499 spin_lock_irqsave(&info->slock, flags);
500 info->MCR &= ~UART_MCR_DTR;
501 outb(info->MCR, info->ioaddr + UART_MCR);
502 spin_unlock_irqrestore(&info->slock, flags);
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 */
519 * This routine is called to set the UART divisor registers to match
520 * the specified baud rate for a serial port.
522 static int mxser_change_speed(struct mxser_port *info,
523 struct termios *old_termios)
525 unsigned cflag, cval, fcr;
527 unsigned char status;
531 if (!info->tty || !info->tty->termios)
533 cflag = info->tty->termios->c_cflag;
537 if (mxser_set_baud_method[info->tty->index] == 0) {
538 baud = tty_get_baud_rate(info->tty);
539 mxser_set_baud(info, baud);
542 /* byte size and parity */
543 switch (cflag & CSIZE) {
558 break; /* too keep GCC shut... */
563 cval |= UART_LCR_PARITY;
564 if (!(cflag & PARODD))
565 cval |= UART_LCR_EPAR;
567 cval |= UART_LCR_SPAR;
569 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
570 if (info->board->chip_flag) {
571 fcr = UART_FCR_ENABLE_FIFO;
572 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
573 SET_MOXA_MUST_FIFO_VALUE(info);
577 fcr = UART_FCR_ENABLE_FIFO;
578 if (info->board->chip_flag) {
579 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
580 SET_MOXA_MUST_FIFO_VALUE(info);
582 switch (info->rx_trigger) {
584 fcr |= UART_FCR_TRIGGER_1;
587 fcr |= UART_FCR_TRIGGER_4;
590 fcr |= UART_FCR_TRIGGER_8;
593 fcr |= UART_FCR_TRIGGER_14;
599 /* CTS flow control flag and modem status interrupts */
600 info->IER &= ~UART_IER_MSI;
601 info->MCR &= ~UART_MCR_AFE;
602 if (cflag & CRTSCTS) {
603 info->flags |= ASYNC_CTS_FLOW;
604 info->IER |= UART_IER_MSI;
605 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
606 info->MCR |= UART_MCR_AFE;
608 status = inb(info->ioaddr + UART_MSR);
609 if (info->tty->hw_stopped) {
610 if (status & UART_MSR_CTS) {
611 info->tty->hw_stopped = 0;
612 if (info->type != PORT_16550A &&
613 !info->board->chip_flag) {
614 outb(info->IER & ~UART_IER_THRI,
617 info->IER |= UART_IER_THRI;
618 outb(info->IER, info->ioaddr +
621 set_bit(MXSER_EVENT_TXLOW, &info->event);
622 schedule_work(&info->tqueue); }
624 if (!(status & UART_MSR_CTS)) {
625 info->tty->hw_stopped = 1;
626 if ((info->type != PORT_16550A) &&
627 (!info->board->chip_flag)) {
628 info->IER &= ~UART_IER_THRI;
629 outb(info->IER, info->ioaddr +
636 info->flags &= ~ASYNC_CTS_FLOW;
638 outb(info->MCR, info->ioaddr + UART_MCR);
639 if (cflag & CLOCAL) {
640 info->flags &= ~ASYNC_CHECK_CD;
642 info->flags |= ASYNC_CHECK_CD;
643 info->IER |= UART_IER_MSI;
645 outb(info->IER, info->ioaddr + UART_IER);
648 * Set up parity check flag
650 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
651 if (I_INPCK(info->tty))
652 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
653 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
654 info->read_status_mask |= UART_LSR_BI;
656 info->ignore_status_mask = 0;
658 if (I_IGNBRK(info->tty)) {
659 info->ignore_status_mask |= UART_LSR_BI;
660 info->read_status_mask |= UART_LSR_BI;
662 * If we're ignore parity and break indicators, ignore
663 * overruns too. (For real raw support).
665 if (I_IGNPAR(info->tty)) {
666 info->ignore_status_mask |=
670 info->read_status_mask |=
676 if (info->board->chip_flag) {
677 spin_lock_irqsave(&info->slock, flags);
678 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
679 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
680 if (I_IXON(info->tty)) {
681 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
683 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
685 if (I_IXOFF(info->tty)) {
686 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
688 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
690 spin_unlock_irqrestore(&info->slock, flags);
694 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
695 outb(cval, info->ioaddr + UART_LCR);
700 static void mxser_check_modem_status(struct mxser_port *port, int status)
702 /* update input line counters */
703 if (status & UART_MSR_TERI)
705 if (status & UART_MSR_DDSR)
707 if (status & UART_MSR_DDCD)
709 if (status & UART_MSR_DCTS)
711 port->mon_data.modem_status = status;
712 wake_up_interruptible(&port->delta_msr_wait);
714 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
715 if (status & UART_MSR_DCD)
716 wake_up_interruptible(&port->open_wait);
717 schedule_work(&port->tqueue);
720 if (port->flags & ASYNC_CTS_FLOW) {
721 if (port->tty->hw_stopped) {
722 if (status & UART_MSR_CTS) {
723 port->tty->hw_stopped = 0;
725 if ((port->type != PORT_16550A) &&
726 (!port->board->chip_flag)) {
727 outb(port->IER & ~UART_IER_THRI,
728 port->ioaddr + UART_IER);
729 port->IER |= UART_IER_THRI;
730 outb(port->IER, port->ioaddr +
733 set_bit(MXSER_EVENT_TXLOW, &port->event);
734 schedule_work(&port->tqueue);
737 if (!(status & UART_MSR_CTS)) {
738 port->tty->hw_stopped = 1;
739 if (port->type != PORT_16550A &&
740 !port->board->chip_flag) {
741 port->IER &= ~UART_IER_THRI;
742 outb(port->IER, port->ioaddr +
750 static int mxser_startup(struct mxser_port *info)
755 page = __get_free_page(GFP_KERNEL);
759 spin_lock_irqsave(&info->slock, flags);
761 if (info->flags & ASYNC_INITIALIZED) {
763 spin_unlock_irqrestore(&info->slock, flags);
767 if (!info->ioaddr || !info->type) {
769 set_bit(TTY_IO_ERROR, &info->tty->flags);
771 spin_unlock_irqrestore(&info->slock, flags);
777 info->xmit_buf = (unsigned char *) page;
780 * Clear the FIFO buffers and disable them
781 * (they will be reenabled in mxser_change_speed())
783 if (info->board->chip_flag)
784 outb((UART_FCR_CLEAR_RCVR |
785 UART_FCR_CLEAR_XMIT |
786 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
788 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
789 info->ioaddr + UART_FCR);
792 * At this point there's no way the LSR could still be 0xFF;
793 * if it is, then bail out, because there's likely no UART
796 if (inb(info->ioaddr + UART_LSR) == 0xff) {
797 spin_unlock_irqrestore(&info->slock, flags);
798 if (capable(CAP_SYS_ADMIN)) {
800 set_bit(TTY_IO_ERROR, &info->tty->flags);
807 * Clear the interrupt registers.
809 (void) inb(info->ioaddr + UART_LSR);
810 (void) inb(info->ioaddr + UART_RX);
811 (void) inb(info->ioaddr + UART_IIR);
812 (void) inb(info->ioaddr + UART_MSR);
815 * Now, initialize the UART
817 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
818 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
819 outb(info->MCR, info->ioaddr + UART_MCR);
822 * Finally, enable interrupts
824 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
826 if (info->board->chip_flag)
827 info->IER |= MOXA_MUST_IER_EGDAI;
828 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
831 * And clear the interrupt registers again for luck.
833 (void) inb(info->ioaddr + UART_LSR);
834 (void) inb(info->ioaddr + UART_RX);
835 (void) inb(info->ioaddr + UART_IIR);
836 (void) inb(info->ioaddr + UART_MSR);
839 clear_bit(TTY_IO_ERROR, &info->tty->flags);
840 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
843 * and set the speed of the serial port
845 spin_unlock_irqrestore(&info->slock, flags);
846 mxser_change_speed(info, NULL);
848 info->flags |= ASYNC_INITIALIZED;
853 * This routine will shutdown a serial port; interrupts maybe disabled, and
854 * DTR is dropped if the hangup on close termio flag is on.
856 static void mxser_shutdown(struct mxser_port *info)
860 if (!(info->flags & ASYNC_INITIALIZED))
863 spin_lock_irqsave(&info->slock, flags);
866 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
867 * here so the queue might never be waken up
869 wake_up_interruptible(&info->delta_msr_wait);
872 * Free the IRQ, if necessary
874 if (info->xmit_buf) {
875 free_page((unsigned long) info->xmit_buf);
876 info->xmit_buf = NULL;
880 outb(0x00, info->ioaddr + UART_IER);
882 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
883 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
884 outb(info->MCR, info->ioaddr + UART_MCR);
886 /* clear Rx/Tx FIFO's */
887 if (info->board->chip_flag)
888 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
889 MOXA_MUST_FCR_GDA_MODE_ENABLE,
890 info->ioaddr + UART_FCR);
892 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
893 info->ioaddr + UART_FCR);
895 /* read data port to reset things */
896 (void) inb(info->ioaddr + UART_RX);
899 set_bit(TTY_IO_ERROR, &info->tty->flags);
901 info->flags &= ~ASYNC_INITIALIZED;
903 if (info->board->chip_flag)
904 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
906 spin_unlock_irqrestore(&info->slock, flags);
910 * This routine is called whenever a serial port is opened. It
911 * enables interrupts for a serial port, linking in its async structure into
912 * the IRQ chain. It also performs the serial-specific
913 * initialization for the tty structure.
915 static int mxser_open(struct tty_struct *tty, struct file *filp)
917 struct mxser_port *info;
920 /* initialize driver_data in case something fails */
921 tty->driver_data = NULL;
924 if (line == MXSER_PORTS)
926 if (line < 0 || line > MXSER_PORTS)
928 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
932 tty->driver_data = info;
935 * Start up serial port
938 retval = mxser_startup(info);
942 retval = mxser_block_til_ready(tty, filp, info);
946 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
947 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
948 *tty->termios = info->normal_termios;
950 *tty->termios = info->callout_termios;
951 mxser_change_speed(info, NULL);
954 info->session = process_session(current);
955 info->pgrp = process_group(current);
957 /* unmark here for very high baud rate (ex. 921600 bps) used */
958 tty->low_latency = 1;
963 * This routine is called when the serial port gets closed. First, we
964 * wait for the last remaining data to be sent. Then, we unlink its
965 * async structure from the interrupt chain if necessary, and we free
966 * that IRQ if nothing is left in the chain.
968 static void mxser_close(struct tty_struct *tty, struct file *filp)
970 struct mxser_port *info = tty->driver_data;
972 unsigned long timeout;
975 if (tty->index == MXSER_PORTS)
980 spin_lock_irqsave(&info->slock, flags);
982 if (tty_hung_up_p(filp)) {
983 spin_unlock_irqrestore(&info->slock, flags);
986 if ((tty->count == 1) && (info->count != 1)) {
988 * Uh, oh. tty->count is 1, which means that the tty
989 * structure will be freed. Info->count should always
990 * be one in these conditions. If it's greater than
991 * one, we've got real problems, since it means the
992 * serial port won't be shutdown.
994 printk(KERN_ERR "mxser_close: bad serial port count; "
995 "tty->count is 1, info->count is %d\n", info->count);
998 if (--info->count < 0) {
999 printk(KERN_ERR "mxser_close: bad serial port count for "
1000 "ttys%d: %d\n", tty->index, info->count);
1004 spin_unlock_irqrestore(&info->slock, flags);
1007 info->flags |= ASYNC_CLOSING;
1008 spin_unlock_irqrestore(&info->slock, flags);
1010 * Save the termios structure, since this port may have
1011 * separate termios for callout and dialin.
1013 if (info->flags & ASYNC_NORMAL_ACTIVE)
1014 info->normal_termios = *tty->termios;
1016 * Now we wait for the transmit buffer to clear; and we notify
1017 * the line discipline to only process XON/XOFF characters.
1020 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1021 tty_wait_until_sent(tty, info->closing_wait);
1023 * At this point we stop accepting input. To do this, we
1024 * disable the receive line status interrupts, and tell the
1025 * interrupt driver to stop checking the data ready bit in the
1026 * line status register.
1028 info->IER &= ~UART_IER_RLSI;
1029 if (info->board->chip_flag)
1030 info->IER &= ~MOXA_MUST_RECV_ISR;
1032 if (info->flags & ASYNC_INITIALIZED) {
1033 outb(info->IER, info->ioaddr + UART_IER);
1035 * Before we drop DTR, make sure the UART transmitter
1036 * has completely drained; this is especially
1037 * important if there is a transmit FIFO!
1039 timeout = jiffies + HZ;
1040 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
1041 schedule_timeout_interruptible(5);
1042 if (time_after(jiffies, timeout))
1046 mxser_shutdown(info);
1048 if (tty->driver->flush_buffer)
1049 tty->driver->flush_buffer(tty);
1051 tty_ldisc_flush(tty);
1056 if (info->blocked_open) {
1057 if (info->close_delay)
1058 schedule_timeout_interruptible(info->close_delay);
1059 wake_up_interruptible(&info->open_wait);
1062 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1063 wake_up_interruptible(&info->close_wait);
1067 static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1070 struct mxser_port *info = tty->driver_data;
1071 unsigned long flags;
1073 if (!info->xmit_buf)
1077 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1078 SERIAL_XMIT_SIZE - info->xmit_head));
1082 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1083 spin_lock_irqsave(&info->slock, flags);
1084 info->xmit_head = (info->xmit_head + c) &
1085 (SERIAL_XMIT_SIZE - 1);
1086 info->xmit_cnt += c;
1087 spin_unlock_irqrestore(&info->slock, flags);
1094 if (info->xmit_cnt && !tty->stopped) {
1095 if (!tty->hw_stopped ||
1096 (info->type == PORT_16550A) ||
1097 (info->board->chip_flag)) {
1098 spin_lock_irqsave(&info->slock, flags);
1099 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1101 info->IER |= UART_IER_THRI;
1102 outb(info->IER, info->ioaddr + UART_IER);
1103 spin_unlock_irqrestore(&info->slock, flags);
1109 static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1111 struct mxser_port *info = tty->driver_data;
1112 unsigned long flags;
1114 if (!info->xmit_buf)
1117 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1120 spin_lock_irqsave(&info->slock, flags);
1121 info->xmit_buf[info->xmit_head++] = ch;
1122 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1124 spin_unlock_irqrestore(&info->slock, flags);
1125 if (!tty->stopped) {
1126 if (!tty->hw_stopped ||
1127 (info->type == PORT_16550A) ||
1128 info->board->chip_flag) {
1129 spin_lock_irqsave(&info->slock, flags);
1130 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1131 info->IER |= UART_IER_THRI;
1132 outb(info->IER, info->ioaddr + UART_IER);
1133 spin_unlock_irqrestore(&info->slock, flags);
1139 static void mxser_flush_chars(struct tty_struct *tty)
1141 struct mxser_port *info = tty->driver_data;
1142 unsigned long flags;
1144 if (info->xmit_cnt <= 0 ||
1148 (info->type != PORT_16550A) &&
1149 (!info->board->chip_flag)
1153 spin_lock_irqsave(&info->slock, flags);
1155 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1156 info->IER |= UART_IER_THRI;
1157 outb(info->IER, info->ioaddr + UART_IER);
1159 spin_unlock_irqrestore(&info->slock, flags);
1162 static int mxser_write_room(struct tty_struct *tty)
1164 struct mxser_port *info = tty->driver_data;
1167 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1173 static int mxser_chars_in_buffer(struct tty_struct *tty)
1175 struct mxser_port *info = tty->driver_data;
1176 return info->xmit_cnt;
1179 static void mxser_flush_buffer(struct tty_struct *tty)
1181 struct mxser_port *info = tty->driver_data;
1183 unsigned long flags;
1186 spin_lock_irqsave(&info->slock, flags);
1187 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1189 fcr = inb(info->ioaddr + UART_FCR);
1190 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1191 info->ioaddr + UART_FCR);
1192 outb(fcr, info->ioaddr + UART_FCR);
1194 spin_unlock_irqrestore(&info->slock, flags);
1200 * ------------------------------------------------------------
1201 * friends of mxser_ioctl()
1202 * ------------------------------------------------------------
1204 static int mxser_get_serial_info(struct mxser_port *info,
1205 struct serial_struct __user *retinfo)
1207 struct serial_struct tmp;
1211 memset(&tmp, 0, sizeof(tmp));
1212 tmp.type = info->type;
1213 tmp.line = info->tty->index;
1214 tmp.port = info->ioaddr;
1215 tmp.irq = info->board->irq;
1216 tmp.flags = info->flags;
1217 tmp.baud_base = info->baud_base;
1218 tmp.close_delay = info->close_delay;
1219 tmp.closing_wait = info->closing_wait;
1220 tmp.custom_divisor = info->custom_divisor;
1222 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1227 static int mxser_set_serial_info(struct mxser_port *info,
1228 struct serial_struct __user *new_info)
1230 struct serial_struct new_serial;
1234 if (!new_info || !info->ioaddr)
1236 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1239 if ((new_serial.irq != info->board->irq) ||
1240 (new_serial.port != info->ioaddr) ||
1241 (new_serial.custom_divisor != info->custom_divisor) ||
1242 (new_serial.baud_base != info->baud_base))
1245 flags = info->flags & ASYNC_SPD_MASK;
1247 if (!capable(CAP_SYS_ADMIN)) {
1248 if ((new_serial.baud_base != info->baud_base) ||
1249 (new_serial.close_delay != info->close_delay) ||
1250 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
1252 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1253 (new_serial.flags & ASYNC_USR_MASK));
1256 * OK, past this point, all the error checking has been done.
1257 * At this point, we start making changes.....
1259 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1260 (new_serial.flags & ASYNC_FLAGS));
1261 info->close_delay = new_serial.close_delay * HZ / 100;
1262 info->closing_wait = new_serial.closing_wait * HZ / 100;
1263 info->tty->low_latency =
1264 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1265 info->tty->low_latency = 0;
1268 info->type = new_serial.type;
1270 process_txrx_fifo(info);
1272 if (info->flags & ASYNC_INITIALIZED) {
1273 if (flags != (info->flags & ASYNC_SPD_MASK))
1274 mxser_change_speed(info, NULL);
1276 retval = mxser_startup(info);
1282 * mxser_get_lsr_info - get line status register info
1284 * Purpose: Let user call ioctl() to get info when the UART physically
1285 * is emptied. On bus types like RS485, the transmitter must
1286 * release the bus after transmitting. This must be done when
1287 * the transmit shift register is empty, not be done when the
1288 * transmit holding register is empty. This functionality
1289 * allows an RS485 driver to be written in user space.
1291 static int mxser_get_lsr_info(struct mxser_port *info,
1292 unsigned int __user *value)
1294 unsigned char status;
1295 unsigned int result;
1296 unsigned long flags;
1298 spin_lock_irqsave(&info->slock, flags);
1299 status = inb(info->ioaddr + UART_LSR);
1300 spin_unlock_irqrestore(&info->slock, flags);
1301 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1302 return put_user(result, value);
1306 * This routine sends a break character out the serial port.
1308 static void mxser_send_break(struct mxser_port *info, int duration)
1310 unsigned long flags;
1314 set_current_state(TASK_INTERRUPTIBLE);
1315 spin_lock_irqsave(&info->slock, flags);
1316 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1317 info->ioaddr + UART_LCR);
1318 spin_unlock_irqrestore(&info->slock, flags);
1319 schedule_timeout(duration);
1320 spin_lock_irqsave(&info->slock, flags);
1321 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1322 info->ioaddr + UART_LCR);
1323 spin_unlock_irqrestore(&info->slock, flags);
1326 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
1328 struct mxser_port *info = tty->driver_data;
1329 unsigned char control, status;
1330 unsigned long flags;
1333 if (tty->index == MXSER_PORTS)
1334 return -ENOIOCTLCMD;
1335 if (test_bit(TTY_IO_ERROR, &tty->flags))
1338 control = info->MCR;
1340 spin_lock_irqsave(&info->slock, flags);
1341 status = inb(info->ioaddr + UART_MSR);
1342 if (status & UART_MSR_ANY_DELTA)
1343 mxser_check_modem_status(info, status);
1344 spin_unlock_irqrestore(&info->slock, flags);
1345 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1346 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1347 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1348 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1349 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1350 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1353 static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1354 unsigned int set, unsigned int clear)
1356 struct mxser_port *info = tty->driver_data;
1357 unsigned long flags;
1360 if (tty->index == MXSER_PORTS)
1361 return -ENOIOCTLCMD;
1362 if (test_bit(TTY_IO_ERROR, &tty->flags))
1365 spin_lock_irqsave(&info->slock, flags);
1367 if (set & TIOCM_RTS)
1368 info->MCR |= UART_MCR_RTS;
1369 if (set & TIOCM_DTR)
1370 info->MCR |= UART_MCR_DTR;
1372 if (clear & TIOCM_RTS)
1373 info->MCR &= ~UART_MCR_RTS;
1374 if (clear & TIOCM_DTR)
1375 info->MCR &= ~UART_MCR_DTR;
1377 outb(info->MCR, info->ioaddr + UART_MCR);
1378 spin_unlock_irqrestore(&info->slock, flags);
1382 static int mxser_program_mode(int port)
1386 spin_lock(&gm_lock);
1394 spin_unlock(&gm_lock);
1396 id = inb(port + 1) & 0x1F;
1397 if ((id != C168_ASIC_ID) &&
1398 (id != C104_ASIC_ID) &&
1399 (id != C102_ASIC_ID) &&
1400 (id != CI132_ASIC_ID) &&
1401 (id != CI134_ASIC_ID) &&
1402 (id != CI104J_ASIC_ID))
1404 for (i = 0, j = 0; i < 4; i++) {
1408 } else if ((j == 1) && (n == 1)) {
1419 static void mxser_normal_mode(int port)
1423 outb(0xA5, port + 1);
1424 outb(0x80, port + 3);
1425 outb(12, port + 0); /* 9600 bps */
1427 outb(0x03, port + 3); /* 8 data bits */
1428 outb(0x13, port + 4); /* loop back mode */
1429 for (i = 0; i < 16; i++) {
1431 if ((n & 0x61) == 0x60)
1436 outb(0x00, port + 4);
1439 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1440 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1441 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1442 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1443 #define EN_CCMD 0x000 /* Chip's command register */
1444 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1445 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1446 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1447 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1448 #define EN0_DCFG 0x00E /* Data configuration reg WR */
1449 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1450 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1451 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1452 static int mxser_read_register(int port, unsigned short *regs)
1454 int i, k, value, id;
1457 id = mxser_program_mode(port);
1460 for (i = 0; i < 14; i++) {
1461 k = (i & 0x3F) | 0x180;
1462 for (j = 0x100; j > 0; j >>= 1) {
1463 outb(CHIP_CS, port);
1465 outb(CHIP_CS | CHIP_DO, port);
1466 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1468 outb(CHIP_CS, port);
1469 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1474 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1475 outb(CHIP_CS, port);
1476 outb(CHIP_CS | CHIP_SK, port);
1477 if (inb(port) & CHIP_DI)
1483 mxser_normal_mode(port);
1487 static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1489 struct mxser_port *port;
1495 /* if (copy_to_user(argp, mxsercfg,
1496 sizeof(struct mxser_hwconf) * 4))
1500 case MOXA_GET_MAJOR:
1501 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1505 case MOXA_GET_CUMAJOR:
1506 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1510 case MOXA_CHKPORTENABLE:
1513 for (i = 0; i < MXSER_BOARDS; i++)
1514 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1515 if (mxser_boards[i].ports[j].ioaddr)
1518 return put_user(result, (unsigned long __user *)argp);
1519 case MOXA_GETDATACOUNT:
1520 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1523 case MOXA_GETMSTATUS:
1524 for (i = 0; i < MXSER_BOARDS; i++)
1525 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1526 port = &mxser_boards[i].ports[j];
1529 if (!port->ioaddr) {
1530 GMStatus[i].dcd = 0;
1531 GMStatus[i].dsr = 0;
1532 GMStatus[i].cts = 0;
1536 if (!port->tty || !port->tty->termios)
1538 port->normal_termios.c_cflag;
1541 port->tty->termios->c_cflag;
1543 status = inb(port->ioaddr + UART_MSR);
1544 if (status & 0x80 /*UART_MSR_DCD */ )
1545 GMStatus[i].dcd = 1;
1547 GMStatus[i].dcd = 0;
1549 if (status & 0x20 /*UART_MSR_DSR */ )
1550 GMStatus[i].dsr = 1;
1552 GMStatus[i].dsr = 0;
1555 if (status & 0x10 /*UART_MSR_CTS */ )
1556 GMStatus[i].cts = 1;
1558 GMStatus[i].cts = 0;
1560 if (copy_to_user(argp, GMStatus,
1561 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1564 case MOXA_ASPP_MON_EXT: {
1565 int status, p, shiftbit;
1566 unsigned long opmode;
1567 unsigned cflag, iflag;
1569 for (i = 0; i < MXSER_BOARDS; i++)
1570 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1571 port = &mxser_boards[i].ports[j];
1575 status = mxser_get_msr(port->ioaddr, 0, i);
1577 if (status & UART_MSR_TERI)
1579 if (status & UART_MSR_DDSR)
1581 if (status & UART_MSR_DDCD)
1583 if (status & UART_MSR_DCTS)
1586 port->mon_data.modem_status = status;
1587 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1588 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1589 mon_data_ext.up_rxcnt[i] =
1590 port->mon_data.up_rxcnt;
1591 mon_data_ext.up_txcnt[i] =
1592 port->mon_data.up_txcnt;
1593 mon_data_ext.modem_status[i] =
1594 port->mon_data.modem_status;
1595 mon_data_ext.baudrate[i] = port->realbaud;
1597 if (!port->tty || !port->tty->termios) {
1598 cflag = port->normal_termios.c_cflag;
1599 iflag = port->normal_termios.c_iflag;
1601 cflag = port->tty->termios->c_cflag;
1602 iflag = port->tty->termios->c_iflag;
1605 mon_data_ext.databits[i] = cflag & CSIZE;
1607 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1609 mon_data_ext.parity[i] =
1610 cflag & (PARENB | PARODD | CMSPAR);
1612 mon_data_ext.flowctrl[i] = 0x00;
1614 if (cflag & CRTSCTS)
1615 mon_data_ext.flowctrl[i] |= 0x03;
1617 if (iflag & (IXON | IXOFF))
1618 mon_data_ext.flowctrl[i] |= 0x0C;
1620 if (port->type == PORT_16550A)
1621 mon_data_ext.fifo[i] = 1;
1623 mon_data_ext.fifo[i] = 0;
1627 opmode = inb(port->opmode_ioaddr) >> shiftbit;
1628 opmode &= OP_MODE_MASK;
1630 mon_data_ext.iftype[i] = opmode;
1633 if (copy_to_user(argp, &mon_data_ext,
1634 sizeof(mon_data_ext)))
1640 return -ENOIOCTLCMD;
1645 static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1646 unsigned int cmd, unsigned long arg)
1648 struct mxser_port *info = tty->driver_data;
1649 struct async_icount cprev, cnow; /* kernel counter temps */
1650 struct serial_icounter_struct __user *p_cuser;
1651 unsigned long templ;
1652 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 != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1692 test_bit(TTY_IO_ERROR, &tty->flags))
1696 case TCSBRK: /* SVID version: non-zero arg --> no break */
1697 retval = tty_check_change(tty);
1700 tty_wait_until_sent(tty, 0);
1702 mxser_send_break(info, HZ / 4); /* 1/4 second */
1704 case TCSBRKP: /* support for POSIX tcsendbreak() */
1705 retval = tty_check_change(tty);
1708 tty_wait_until_sent(tty, 0);
1709 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1712 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1714 if (get_user(templ, (unsigned long __user *) argp))
1717 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1720 return mxser_get_serial_info(info, argp);
1722 return mxser_set_serial_info(info, argp);
1723 case TIOCSERGETLSR: /* Get line status register */
1724 return mxser_get_lsr_info(info, argp);
1726 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1727 * - mask passed in arg for lines of interest
1728 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1729 * Caller should use TIOCGICOUNT to see which one it was
1732 DECLARE_WAITQUEUE(wait, current);
1734 spin_lock_irqsave(&info->slock, flags);
1735 cprev = info->icount; /* note the counters on entry */
1736 spin_unlock_irqrestore(&info->slock, flags);
1738 add_wait_queue(&info->delta_msr_wait, &wait);
1740 spin_lock_irqsave(&info->slock, flags);
1741 cnow = info->icount; /* atomic copy */
1742 spin_unlock_irqrestore(&info->slock, flags);
1744 set_current_state(TASK_INTERRUPTIBLE);
1745 if (((arg & TIOCM_RNG) &&
1746 (cnow.rng != cprev.rng)) ||
1747 ((arg & TIOCM_DSR) &&
1748 (cnow.dsr != cprev.dsr)) ||
1749 ((arg & TIOCM_CD) &&
1750 (cnow.dcd != cprev.dcd)) ||
1751 ((arg & TIOCM_CTS) &&
1752 (cnow.cts != cprev.cts))) {
1756 /* see if a signal did it */
1757 if (signal_pending(current)) {
1763 current->state = TASK_RUNNING;
1764 remove_wait_queue(&info->delta_msr_wait, &wait);
1769 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1770 * Return: write counters to the user passed counter struct
1771 * NB: both 1->0 and 0->1 transitions are counted except for
1772 * RI where only 0->1 is counted.
1775 spin_lock_irqsave(&info->slock, flags);
1776 cnow = info->icount;
1777 spin_unlock_irqrestore(&info->slock, flags);
1779 if (put_user(cnow.frame, &p_cuser->frame))
1781 if (put_user(cnow.brk, &p_cuser->brk))
1783 if (put_user(cnow.overrun, &p_cuser->overrun))
1785 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1787 if (put_user(cnow.parity, &p_cuser->parity))
1789 if (put_user(cnow.rx, &p_cuser->rx))
1791 if (put_user(cnow.tx, &p_cuser->tx))
1793 put_user(cnow.cts, &p_cuser->cts);
1794 put_user(cnow.dsr, &p_cuser->dsr);
1795 put_user(cnow.rng, &p_cuser->rng);
1796 put_user(cnow.dcd, &p_cuser->dcd);
1798 case MOXA_HighSpeedOn:
1799 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1800 case MOXA_SDS_RSTICOUNTER:
1801 info->mon_data.rxcnt = 0;
1802 info->mon_data.txcnt = 0;
1804 case MOXA_ASPP_SETBAUD:{
1806 if (get_user(baud, (long __user *)argp))
1808 mxser_set_baud(info, baud);
1811 case MOXA_ASPP_GETBAUD:
1812 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1817 case MOXA_ASPP_OQUEUE:{
1820 len = mxser_chars_in_buffer(tty);
1822 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1824 len += (lsr ? 0 : 1);
1826 if (copy_to_user(argp, &len, sizeof(int)))
1831 case MOXA_ASPP_MON: {
1834 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1835 mxser_check_modem_status(info, status);
1837 mcr = inb(info->ioaddr + UART_MCR);
1838 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1839 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1841 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1843 if (mcr & MOXA_MUST_MCR_TX_XON)
1844 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1846 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1848 if (info->tty->hw_stopped)
1849 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1851 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1853 if (copy_to_user(argp, &info->mon_data,
1854 sizeof(struct mxser_mon)))
1859 case MOXA_ASPP_LSTATUS: {
1860 if (copy_to_user(argp, &info->err_shadow,
1861 sizeof(unsigned char)))
1864 info->err_shadow = 0;
1867 case MOXA_SET_BAUD_METHOD: {
1870 if (get_user(method, (int __user *)argp))
1872 mxser_set_baud_method[tty->index] = method;
1873 if (copy_to_user(argp, &method, sizeof(int)))
1879 return -ENOIOCTLCMD;
1884 static void mxser_stoprx(struct tty_struct *tty)
1886 struct mxser_port *info = tty->driver_data;
1888 info->ldisc_stop_rx = 1;
1890 if (info->board->chip_flag) {
1891 info->IER &= ~MOXA_MUST_RECV_ISR;
1892 outb(info->IER, info->ioaddr + UART_IER);
1894 info->x_char = STOP_CHAR(tty);
1895 outb(0, info->ioaddr + UART_IER);
1896 info->IER |= UART_IER_THRI;
1897 outb(info->IER, info->ioaddr + UART_IER);
1901 if (info->tty->termios->c_cflag & CRTSCTS) {
1902 info->MCR &= ~UART_MCR_RTS;
1903 outb(info->MCR, info->ioaddr + UART_MCR);
1908 * This routine is called by the upper-layer tty layer to signal that
1909 * incoming characters should be throttled.
1911 static void mxser_throttle(struct tty_struct *tty)
1916 static void mxser_unthrottle(struct tty_struct *tty)
1918 struct mxser_port *info = tty->driver_data;
1921 info->ldisc_stop_rx = 0;
1926 if (info->board->chip_flag) {
1927 info->IER |= MOXA_MUST_RECV_ISR;
1928 outb(info->IER, info->ioaddr + UART_IER);
1930 info->x_char = START_CHAR(tty);
1931 outb(0, info->ioaddr + UART_IER);
1932 info->IER |= UART_IER_THRI;
1933 outb(info->IER, info->ioaddr + UART_IER);
1938 if (info->tty->termios->c_cflag & CRTSCTS) {
1939 info->MCR |= UART_MCR_RTS;
1940 outb(info->MCR, info->ioaddr + UART_MCR);
1945 * mxser_stop() and mxser_start()
1947 * This routines are called before setting or resetting tty->stopped.
1948 * They enable or disable transmitter interrupts, as necessary.
1950 static void mxser_stop(struct tty_struct *tty)
1952 struct mxser_port *info = tty->driver_data;
1953 unsigned long flags;
1955 spin_lock_irqsave(&info->slock, flags);
1956 if (info->IER & UART_IER_THRI) {
1957 info->IER &= ~UART_IER_THRI;
1958 outb(info->IER, info->ioaddr + UART_IER);
1960 spin_unlock_irqrestore(&info->slock, flags);
1963 static void mxser_start(struct tty_struct *tty)
1965 struct mxser_port *info = tty->driver_data;
1966 unsigned long flags;
1968 spin_lock_irqsave(&info->slock, flags);
1969 if (info->xmit_cnt && info->xmit_buf) {
1970 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1971 info->IER |= UART_IER_THRI;
1972 outb(info->IER, info->ioaddr + UART_IER);
1974 spin_unlock_irqrestore(&info->slock, flags);
1977 static void mxser_set_termios(struct tty_struct *tty, struct termios *old_termios)
1979 struct mxser_port *info = tty->driver_data;
1980 unsigned long flags;
1982 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1983 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
1985 mxser_change_speed(info, old_termios);
1987 if ((old_termios->c_cflag & CRTSCTS) &&
1988 !(tty->termios->c_cflag & CRTSCTS)) {
1989 tty->hw_stopped = 0;
1994 /* Handle sw stopped */
1995 if ((old_termios->c_iflag & IXON) &&
1996 !(tty->termios->c_iflag & IXON)) {
1999 if (info->board->chip_flag) {
2000 spin_lock_irqsave(&info->slock, flags);
2001 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2002 spin_unlock_irqrestore(&info->slock, flags);
2010 * mxser_wait_until_sent() --- wait until the transmitter is empty
2012 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2014 struct mxser_port *info = tty->driver_data;
2015 unsigned long orig_jiffies, char_time;
2018 if (info->type == PORT_UNKNOWN)
2021 if (info->xmit_fifo_size == 0)
2022 return; /* Just in case.... */
2024 orig_jiffies = jiffies;
2026 * Set the check interval to be 1/5 of the estimated time to
2027 * send a single character, and make it at least 1. The check
2028 * interval should also be less than the timeout.
2030 * Note: we have to use pretty tight timings here to satisfy
2033 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2034 char_time = char_time / 5;
2037 if (timeout && timeout < char_time)
2038 char_time = timeout;
2040 * If the transmitter hasn't cleared in twice the approximate
2041 * amount of time to send the entire FIFO, it probably won't
2042 * ever clear. This assumes the UART isn't doing flow
2043 * control, which is currently the case. Hence, if it ever
2044 * takes longer than info->timeout, this is probably due to a
2045 * UART bug of some kind. So, we clamp the timeout parameter at
2048 if (!timeout || timeout > 2 * info->timeout)
2049 timeout = 2 * info->timeout;
2050 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2051 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2052 timeout, char_time);
2053 printk("jiff=%lu...", jiffies);
2055 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
2056 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2057 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2059 schedule_timeout_interruptible(char_time);
2060 if (signal_pending(current))
2062 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2065 set_current_state(TASK_RUNNING);
2067 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2068 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2073 * This routine is called by tty_hangup() when a hangup is signaled.
2075 void mxser_hangup(struct tty_struct *tty)
2077 struct mxser_port *info = tty->driver_data;
2079 mxser_flush_buffer(tty);
2080 mxser_shutdown(info);
2083 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2085 wake_up_interruptible(&info->open_wait);
2089 * mxser_rs_break() --- routine which turns the break handling on or off
2091 static void mxser_rs_break(struct tty_struct *tty, int break_state)
2093 struct mxser_port *info = tty->driver_data;
2094 unsigned long flags;
2096 spin_lock_irqsave(&info->slock, flags);
2097 if (break_state == -1)
2098 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2099 info->ioaddr + UART_LCR);
2101 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2102 info->ioaddr + UART_LCR);
2103 spin_unlock_irqrestore(&info->slock, flags);
2106 static void mxser_receive_chars(struct mxser_port *port, int *status)
2108 struct tty_struct *tty = port->tty;
2109 unsigned char ch, gdl;
2114 unsigned long flags;
2116 spin_lock_irqsave(&port->slock, flags);
2118 recv_room = tty->receive_room;
2119 if ((recv_room == 0) && (!port->ldisc_stop_rx))
2122 if (port->board->chip_flag != MOXA_OTHER_UART) {
2124 if (*status & UART_LSR_SPECIAL)
2126 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
2127 (*status & MOXA_MUST_LSR_RERR))
2129 if (*status & MOXA_MUST_LSR_RERR)
2132 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
2134 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
2135 gdl &= MOXA_MUST_GDL_MASK;
2136 if (gdl >= recv_room) {
2137 if (!port->ldisc_stop_rx)
2141 ch = inb(port->ioaddr + UART_RX);
2142 tty_insert_flip_char(tty, ch, 0);
2153 ch = inb(port->ioaddr + UART_RX);
2154 if (port->board->chip_flag && (*status & UART_LSR_OE))
2155 outb(0x23, port->ioaddr + UART_FCR);
2156 *status &= port->read_status_mask;
2157 if (*status & port->ignore_status_mask) {
2158 if (++ignored > 100)
2162 if (*status & UART_LSR_SPECIAL) {
2163 if (*status & UART_LSR_BI) {
2167 if (port->flags & ASYNC_SAK)
2169 } else if (*status & UART_LSR_PE) {
2171 port->icount.parity++;
2172 } else if (*status & UART_LSR_FE) {
2174 port->icount.frame++;
2175 } else if (*status & UART_LSR_OE) {
2177 port->icount.overrun++;
2180 tty_insert_flip_char(tty, ch, flag);
2182 if (cnt >= recv_room) {
2183 if (!port->ldisc_stop_rx)
2190 if (port->board->chip_flag)
2193 *status = inb(port->ioaddr + UART_LSR);
2194 } while (*status & UART_LSR_DR);
2197 mxvar_log.rxcnt[port->tty->index] += cnt;
2198 port->mon_data.rxcnt += cnt;
2199 port->mon_data.up_rxcnt += cnt;
2200 spin_unlock_irqrestore(&port->slock, flags);
2202 tty_flip_buffer_push(tty);
2205 static void mxser_transmit_chars(struct mxser_port *port)
2208 unsigned long flags;
2210 spin_lock_irqsave(&port->slock, flags);
2213 outb(port->x_char, port->ioaddr + UART_TX);
2215 mxvar_log.txcnt[port->tty->index]++;
2216 port->mon_data.txcnt++;
2217 port->mon_data.up_txcnt++;
2222 if (port->xmit_buf == 0)
2225 if ((port->xmit_cnt <= 0) || port->tty->stopped ||
2226 (port->tty->hw_stopped &&
2227 (port->type != PORT_16550A) &&
2228 (!port->board->chip_flag))) {
2229 port->IER &= ~UART_IER_THRI;
2230 outb(port->IER, port->ioaddr + UART_IER);
2234 cnt = port->xmit_cnt;
2235 count = port->xmit_fifo_size;
2237 outb(port->xmit_buf[port->xmit_tail++],
2238 port->ioaddr + UART_TX);
2239 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2240 if (--port->xmit_cnt <= 0)
2242 } while (--count > 0);
2243 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
2245 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2246 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2247 port->icount.tx += (cnt - port->xmit_cnt);
2249 if (port->xmit_cnt < WAKEUP_CHARS) {
2250 set_bit(MXSER_EVENT_TXLOW, &port->event);
2251 schedule_work(&port->tqueue);
2253 if (port->xmit_cnt <= 0) {
2254 port->IER &= ~UART_IER_THRI;
2255 outb(port->IER, port->ioaddr + UART_IER);
2258 spin_unlock_irqrestore(&port->slock, flags);
2262 * This is the serial driver's generic interrupt routine
2264 static irqreturn_t mxser_interrupt(int irq, void *dev_id)
2267 struct mxser_board *brd = NULL;
2268 struct mxser_port *port;
2269 int max, irqbits, bits, msr;
2270 int pass_counter = 0;
2271 unsigned int int_cnt;
2272 int handled = IRQ_NONE;
2274 for (i = 0; i < MXSER_BOARDS; i++)
2275 if (dev_id == &mxser_boards[i]) {
2280 if (i == MXSER_BOARDS)
2284 max = brd->info->nports;
2286 irqbits = inb(brd->vector) & brd->vector_mask;
2287 if (irqbits == brd->vector_mask)
2290 handled = IRQ_HANDLED;
2291 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2292 if (irqbits == brd->vector_mask)
2296 port = &brd->ports[i];
2300 iir = inb(port->ioaddr + UART_IIR);
2301 if (iir & UART_IIR_NO_INT)
2303 iir &= MOXA_MUST_IIR_MASK;
2305 status = inb(port->ioaddr + UART_LSR);
2306 outb(0x27, port->ioaddr + UART_FCR);
2307 inb(port->ioaddr + UART_MSR);
2311 status = inb(port->ioaddr + UART_LSR);
2313 if (status & UART_LSR_PE)
2314 port->err_shadow |= NPPI_NOTIFY_PARITY;
2315 if (status & UART_LSR_FE)
2316 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2317 if (status & UART_LSR_OE)
2319 NPPI_NOTIFY_HW_OVERRUN;
2320 if (status & UART_LSR_BI)
2321 port->err_shadow |= NPPI_NOTIFY_BREAK;
2323 if (port->board->chip_flag) {
2324 if (iir == MOXA_MUST_IIR_GDA ||
2325 iir == MOXA_MUST_IIR_RDA ||
2326 iir == MOXA_MUST_IIR_RTO ||
2327 iir == MOXA_MUST_IIR_LSR)
2328 mxser_receive_chars(port,
2332 status &= port->read_status_mask;
2333 if (status & UART_LSR_DR)
2334 mxser_receive_chars(port,
2337 msr = inb(port->ioaddr + UART_MSR);
2338 if (msr & UART_MSR_ANY_DELTA)
2339 mxser_check_modem_status(port, msr);
2341 if (port->board->chip_flag) {
2342 if (iir == 0x02 && (status &
2344 mxser_transmit_chars(port);
2346 if (status & UART_LSR_THRE)
2347 mxser_transmit_chars(port);
2349 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2351 if (pass_counter++ > MXSER_ISR_PASS_LIMIT)
2352 break; /* Prevent infinite loops */
2359 static const struct tty_operations mxser_ops = {
2361 .close = mxser_close,
2362 .write = mxser_write,
2363 .put_char = mxser_put_char,
2364 .flush_chars = mxser_flush_chars,
2365 .write_room = mxser_write_room,
2366 .chars_in_buffer = mxser_chars_in_buffer,
2367 .flush_buffer = mxser_flush_buffer,
2368 .ioctl = mxser_ioctl,
2369 .throttle = mxser_throttle,
2370 .unthrottle = mxser_unthrottle,
2371 .set_termios = mxser_set_termios,
2373 .start = mxser_start,
2374 .hangup = mxser_hangup,
2375 .break_ctl = mxser_rs_break,
2376 .wait_until_sent = mxser_wait_until_sent,
2377 .tiocmget = mxser_tiocmget,
2378 .tiocmset = mxser_tiocmset,
2382 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2385 static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
2389 free_irq(brd->irq, brd);
2390 if (pdev != NULL) { /* PCI */
2391 pci_release_region(pdev, 2);
2392 pci_release_region(pdev, 3);
2395 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2396 release_region(brd->vector, 1);
2400 static int __devinit mxser_initbrd(struct mxser_board *brd,
2401 struct pci_dev *pdev)
2403 struct mxser_port *info;
2407 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
2409 for (i = 0; i < brd->info->nports; i++) {
2410 info = &brd->ports[i];
2413 info->ldisc_stop_rx = 0;
2415 /* Enhance mode enabled here */
2416 if (brd->chip_flag != MOXA_OTHER_UART)
2417 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
2419 info->flags = ASYNC_SHARE_IRQ;
2420 info->type = brd->uart_type;
2422 process_txrx_fifo(info);
2424 info->custom_divisor = info->baud_base * 16;
2425 info->close_delay = 5 * HZ / 10;
2426 info->closing_wait = 30 * HZ;
2427 INIT_WORK(&info->tqueue, mxser_do_softint, info);
2428 info->normal_termios = mxvar_sdriver->init_termios;
2429 init_waitqueue_head(&info->open_wait);
2430 init_waitqueue_head(&info->close_wait);
2431 init_waitqueue_head(&info->delta_msr_wait);
2432 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2433 info->err_shadow = 0;
2434 spin_lock_init(&info->slock);
2436 /* before set INT ISR, disable all int */
2437 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2438 info->ioaddr + UART_IER);
2441 * Allocate the IRQ if necessary
2444 retval = request_irq(brd->irq, mxser_interrupt,
2445 (brd->ports[0].flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED :
2446 IRQF_DISABLED, "mxser", brd);
2448 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2449 "conflict with another device.\n",
2450 brd->info->name, brd->irq);
2451 /* We hold resources, we need to release them. */
2452 mxser_release_res(brd, pdev, 0);
2458 static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
2461 unsigned short regs[16], irq;
2462 unsigned char scratch, scratch2;
2464 brd->chip_flag = MOXA_OTHER_UART;
2466 id = mxser_read_register(cap, regs);
2469 brd->info = &mxser_cards[0];
2472 brd->info = &mxser_cards[1];
2474 case CI104J_ASIC_ID:
2475 brd->info = &mxser_cards[2];
2478 brd->info = &mxser_cards[5];
2481 brd->info = &mxser_cards[6];
2484 brd->info = &mxser_cards[7];
2491 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2492 Flag-hack checks if configuration should be read as 2-port here. */
2493 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
2494 irq = regs[9] & 0xF000;
2495 irq = irq | (irq >> 4);
2496 if (irq != (regs[9] & 0xFF00))
2497 return MXSER_ERR_IRQ_CONFLIT;
2498 } else if (brd->info->nports == 4) {
2499 irq = regs[9] & 0xF000;
2500 irq = irq | (irq >> 4);
2501 irq = irq | (irq >> 8);
2503 return MXSER_ERR_IRQ_CONFLIT;
2504 } else if (brd->info->nports == 8) {
2505 irq = regs[9] & 0xF000;
2506 irq = irq | (irq >> 4);
2507 irq = irq | (irq >> 8);
2508 if ((irq != regs[9]) || (irq != regs[10]))
2509 return MXSER_ERR_IRQ_CONFLIT;
2513 return MXSER_ERR_IRQ;
2514 brd->irq = ((int)(irq & 0xF000) >> 12);
2515 for (i = 0; i < 8; i++)
2516 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
2517 if ((regs[12] & 0x80) == 0)
2518 return MXSER_ERR_VECTOR;
2519 brd->vector = (int)regs[11]; /* interrupt vector */
2521 brd->vector_mask = 0x00FF;
2523 brd->vector_mask = 0x000F;
2524 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2525 if (regs[12] & bits) {
2526 brd->ports[i].baud_base = 921600;
2527 brd->ports[i].max_baud = 921600;
2529 brd->ports[i].baud_base = 115200;
2530 brd->ports[i].max_baud = 115200;
2533 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2534 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2535 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2536 outb(scratch2, cap + UART_LCR);
2537 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2538 scratch = inb(cap + UART_IIR);
2541 brd->uart_type = PORT_16550A;
2543 brd->uart_type = PORT_16450;
2544 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2546 return MXSER_ERR_IOADDR;
2547 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2548 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2549 return MXSER_ERR_VECTOR;
2551 return brd->info->nports;
2554 static int __devinit mxser_probe(struct pci_dev *pdev,
2555 const struct pci_device_id *ent)
2557 struct mxser_board *brd;
2559 unsigned long ioaddress;
2560 int retval = -EINVAL;
2562 for (i = 0; i < MXSER_BOARDS; i++)
2563 if (mxser_boards[i].info == NULL)
2566 if (i >= MXSER_BOARDS) {
2567 printk(KERN_ERR "Too many Smartio/Industio family boards found "
2568 "(maximum %d), board not configured\n", MXSER_BOARDS);
2572 brd = &mxser_boards[i];
2573 brd->idx = i * MXSER_PORTS_PER_BOARD;
2574 printk(KERN_INFO "Found MOXA %s board (BusNo=%d, DevNo=%d)\n",
2575 mxser_cards[ent->driver_data].name,
2576 pdev->bus->number, PCI_SLOT(pdev->devfn));
2578 retval = pci_enable_device(pdev);
2580 printk(KERN_ERR "Moxa SmartI/O PCI enable fail !\n");
2585 ioaddress = pci_resource_start(pdev, 2);
2586 retval = pci_request_region(pdev, 2, "mxser(IO)");
2590 brd->info = &mxser_cards[ent->driver_data];
2591 for (i = 0; i < brd->info->nports; i++)
2592 brd->ports[i].ioaddr = ioaddress + 8 * i;
2595 ioaddress = pci_resource_start(pdev, 3);
2596 retval = pci_request_region(pdev, 3, "mxser(vector)");
2599 brd->vector = ioaddress;
2602 brd->irq = pdev->irq;
2604 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2605 brd->uart_type = PORT_16550A;
2606 brd->vector_mask = 0;
2608 for (i = 0; i < brd->info->nports; i++) {
2609 for (j = 0; j < UART_INFO_NUM; j++) {
2610 if (Gpci_uart_info[j].type == brd->chip_flag) {
2611 brd->ports[i].max_baud =
2612 Gpci_uart_info[j].max_baud;
2614 /* exception....CP-102 */
2615 if (brd->info->flags & MXSER_HIGHBAUD)
2616 brd->ports[i].max_baud = 921600;
2622 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2623 for (i = 0; i < brd->info->nports; i++) {
2625 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2627 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
2629 outb(0, ioaddress + 4); /* default set to RS232 mode */
2630 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
2633 for (i = 0; i < brd->info->nports; i++) {
2634 brd->vector_mask |= (1 << i);
2635 brd->ports[i].baud_base = 921600;
2638 /* mxser_initbrd will hook ISR. */
2639 if (mxser_initbrd(brd, pdev) < 0)
2642 for (i = 0; i < brd->info->nports; i++)
2643 tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
2645 pci_set_drvdata(pdev, brd);
2649 pci_release_region(pdev, 3);
2651 pci_release_region(pdev, 2);
2657 static void __devexit mxser_remove(struct pci_dev *pdev)
2659 struct mxser_board *brd = pci_get_drvdata(pdev);
2662 for (i = 0; i < brd->info->nports; i++)
2663 tty_unregister_device(mxvar_sdriver, brd->idx + i);
2665 mxser_release_res(brd, pdev, 1);
2668 static struct pci_driver mxser_driver = {
2670 .id_table = mxser_pcibrds,
2671 .probe = mxser_probe,
2672 .remove = __devexit_p(mxser_remove)
2675 static int __init mxser_module_init(void)
2677 struct mxser_board *brd;
2679 unsigned int i, m, isaloop;
2682 pr_debug("Loading module mxser ...\n");
2684 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2687 spin_lock_init(&gm_lock);
2689 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2692 /* Initialize the tty_driver structure */
2693 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2694 mxvar_sdriver->name = "ttyM";
2695 mxvar_sdriver->major = ttymajor;
2696 mxvar_sdriver->minor_start = 0;
2697 mxvar_sdriver->num = MXSER_PORTS + 1;
2698 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2699 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2700 mxvar_sdriver->init_termios = tty_std_termios;
2701 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2702 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2703 tty_set_operations(mxvar_sdriver, &mxser_ops);
2705 retval = tty_register_driver(mxvar_sdriver);
2707 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2715 /* Start finding ISA boards here */
2716 for (isaloop = 0; isaloop < 2; isaloop++)
2717 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2719 cap = mxserBoardCAP[b]; /* predefined */
2721 cap = ioaddr[b]; /* module param */
2726 brd = &mxser_boards[m];
2727 retval = mxser_get_ISA_conf(cap, brd);
2730 printk(KERN_INFO "Found MOXA %s board "
2732 brd->info->name, ioaddr[b]);
2735 if (retval == MXSER_ERR_IRQ)
2736 printk(KERN_ERR "Invalid interrupt "
2737 "number, board not "
2739 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2740 printk(KERN_ERR "Invalid interrupt "
2741 "number, board not "
2743 else if (retval == MXSER_ERR_VECTOR)
2744 printk(KERN_ERR "Invalid interrupt "
2745 "vector, board not "
2747 else if (retval == MXSER_ERR_IOADDR)
2748 printk(KERN_ERR "Invalid I/O address, "
2749 "board not configured\n");
2755 /* mxser_initbrd will hook ISR. */
2756 if (mxser_initbrd(brd, NULL) < 0) {
2761 brd->idx = m * MXSER_PORTS_PER_BOARD;
2762 for (i = 0; i < brd->info->nports; i++)
2763 tty_register_device(mxvar_sdriver, brd->idx + i,
2769 retval = pci_register_driver(&mxser_driver);
2771 printk(KERN_ERR "Can't register pci driver\n");
2775 } /* else: we have some ISA cards under control */
2778 pr_debug("Done.\n");
2782 tty_unregister_driver(mxvar_sdriver);
2784 put_tty_driver(mxvar_sdriver);
2788 static void __exit mxser_module_exit(void)
2792 pr_debug("Unloading module mxser ...\n");
2794 pci_unregister_driver(&mxser_driver);
2796 for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
2797 if (mxser_boards[i].info != NULL)
2798 for (j = 0; j < mxser_boards[i].info->nports; j++)
2799 tty_unregister_device(mxvar_sdriver,
2800 mxser_boards[i].idx + j);
2801 tty_unregister_driver(mxvar_sdriver);
2802 put_tty_driver(mxvar_sdriver);
2804 for (i = 0; i < MXSER_BOARDS; i++)
2805 if (mxser_boards[i].info != NULL)
2806 mxser_release_res(&mxser_boards[i], NULL, 1);
2808 pr_debug("Done.\n");
2811 module_init(mxser_module_init);
2812 module_exit(mxser_module_exit);