2 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
4 * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com.tw).
5 * Copyright (C) 2006 Jiri Slaby <jirislaby@gmail.com>
7 * This code is loosely based on the 1.8 moxa driver which is based on
8 * Linux serial driver, written by Linus Torvalds, Theodore T'so and
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
17 * <alan@redhat.com>. The original 1.8 code is available on www.moxa.com.
18 * - Fixed x86_64 cleanness
19 * - Fixed sleep with spinlock held in mxser_send_break
22 #include <linux/module.h>
23 #include <linux/autoconf.h>
24 #include <linux/errno.h>
25 #include <linux/signal.h>
26 #include <linux/sched.h>
27 #include <linux/timer.h>
28 #include <linux/interrupt.h>
29 #include <linux/tty.h>
30 #include <linux/tty_flip.h>
31 #include <linux/serial.h>
32 #include <linux/serial_reg.h>
33 #include <linux/major.h>
34 #include <linux/string.h>
35 #include <linux/fcntl.h>
36 #include <linux/ptrace.h>
37 #include <linux/gfp.h>
38 #include <linux/ioport.h>
40 #include <linux/smp_lock.h>
41 #include <linux/delay.h>
42 #include <linux/pci.h>
44 #include <asm/system.h>
47 #include <asm/bitops.h>
48 #include <asm/uaccess.h>
50 #include "mxser_new.h"
52 #define MXSER_VERSION "2.0"
53 #define MXSERMAJOR 174
54 #define MXSERCUMAJOR 175
56 #define MXSER_EVENT_TXLOW 1
58 #define MXSER_BOARDS 4 /* Max. boards */
59 #define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
60 #define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
61 #define MXSER_ISR_PASS_LIMIT 99999L
63 #define MXSER_ERR_IOADDR -1
64 #define MXSER_ERR_IRQ -2
65 #define MXSER_ERR_IRQ_CONFLIT -3
66 #define MXSER_ERR_VECTOR -4
68 #define WAKEUP_CHARS 256
70 #define UART_MCR_AFE 0x20
71 #define UART_LSR_SPECIAL 0x1E
73 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|\
76 #define C168_ASIC_ID 1
77 #define C104_ASIC_ID 2
78 #define C102_ASIC_ID 0xB
79 #define CI132_ASIC_ID 4
80 #define CI134_ASIC_ID 3
81 #define CI104J_ASIC_ID 5
83 #define MXSER_HIGHBAUD 1
86 /* This is only for PCI */
96 } Gpci_uart_info[] = {
97 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
98 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
99 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
101 #define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
103 struct mxser_cardinfo {
109 static const struct mxser_cardinfo mxser_cards[] = {
110 { 8, "C168 series", }, /* C168-ISA */
111 { 4, "C104 series", }, /* C104-ISA */
112 { 4, "CI-104J series", }, /* CI104J */
113 { 8, "C168H/PCI series", }, /* C168-PCI */
114 { 4, "C104H/PCI series", }, /* C104-PCI */
115 { 4, "C102 series", MXSER_HAS2 }, /* C102-ISA */
116 { 4, "CI-132 series", MXSER_HAS2 }, /* CI132 */
117 { 4, "CI-134 series", }, /* CI134 */
118 { 2, "CP-132 series", }, /* CP132 */
119 { 4, "CP-114 series", }, /* CP114 */
120 { 4, "CT-114 series", }, /* CT114 */
121 { 2, "CP-102 series", MXSER_HIGHBAUD }, /* CP102 */
122 { 4, "CP-104U series", }, /* CP104U */
123 { 8, "CP-168U series", }, /* CP168U */
124 { 2, "CP-132U series", }, /* CP132U */
125 { 4, "CP-134U series", }, /* CP134U */
126 { 4, "CP-104JU series", }, /* CP104JU */
127 { 8, "Moxa UC7000 Serial", }, /* RC7000 */
128 { 8, "CP-118U series", }, /* CP118U */
129 { 2, "CP-102UL series", }, /* CP102UL */
130 { 2, "CP-102U series", }, /* CP102U */
131 { 8, "CP-118EL series", }, /* CP118EL */
132 { 8, "CP-168EL series", }, /* CP168EL */
133 { 4, "CP-104EL series", } /* CP104EL */
136 /* driver_data correspond to the lines in the structure above
137 see also ISA probe function before you change something */
138 static struct pci_device_id mxser_pcibrds[] = {
139 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C168),
141 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C104),
143 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132),
145 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP114),
147 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CT114),
149 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102),
151 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104U),
153 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168U),
155 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132U),
157 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP134U),
159 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104JU),
161 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_RC7000),
163 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118U),
165 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102UL),
167 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102U),
169 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118EL),
171 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168EL),
173 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104EL),
177 MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
179 static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
180 static int ttymajor = MXSERMAJOR;
181 static int calloutmajor = MXSERCUMAJOR;
183 /* Variables for insmod */
185 MODULE_AUTHOR("Casper Yang");
186 MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
187 module_param_array(ioaddr, int, NULL, 0);
188 module_param(ttymajor, int, 0);
189 MODULE_LICENSE("GPL");
193 unsigned long rxcnt[MXSER_PORTS];
194 unsigned long txcnt[MXSER_PORTS];
201 unsigned long up_rxcnt;
202 unsigned long up_txcnt;
204 unsigned char hold_reason;
207 struct mxser_mon_ext {
208 unsigned long rx_cnt[32];
209 unsigned long tx_cnt[32];
210 unsigned long up_rxcnt[32];
211 unsigned long up_txcnt[32];
212 int modem_status[32];
226 struct mxser_board *board;
227 struct tty_struct *tty;
229 unsigned long ioaddr;
230 unsigned long opmode_ioaddr;
234 int rx_trigger; /* Rx fifo trigger level */
236 int baud_base; /* max. speed */
238 int type; /* UART type */
239 int flags; /* defined in tty.h */
240 long session; /* Session of opening process */
241 long pgrp; /* pgrp of opening process */
243 int x_char; /* xon/xoff character */
244 int IER; /* Interrupt Enable Register */
245 int MCR; /* Modem control register */
247 unsigned char stop_rx;
248 unsigned char ldisc_stop_rx;
252 unsigned short closing_wait;
253 unsigned char err_shadow;
256 int count; /* # of fd on device */
257 int blocked_open; /* # of blocked opens */
258 struct async_icount icount; /* kernel counters for 4 input interrupts */
261 int read_status_mask;
262 int ignore_status_mask;
264 unsigned char *xmit_buf;
269 struct ktermios normal_termios;
270 struct ktermios callout_termios;
272 struct mxser_mon mon_data;
275 struct work_struct tqueue;
276 wait_queue_head_t open_wait;
277 wait_queue_head_t close_wait;
278 wait_queue_head_t delta_msr_wait;
284 const struct mxser_cardinfo *info;
285 unsigned long vector;
286 unsigned long vector_mask;
291 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
294 struct mxser_mstatus {
302 static struct mxser_mstatus GMStatus[MXSER_PORTS];
304 static int mxserBoardCAP[MXSER_BOARDS] = {
306 /* 0x180, 0x280, 0x200, 0x320 */
309 static struct mxser_board mxser_boards[MXSER_BOARDS];
310 static struct tty_driver *mxvar_sdriver;
311 static struct mxser_log mxvar_log;
312 static int mxvar_diagflag;
313 static unsigned char mxser_msr[MXSER_PORTS + 1];
314 static struct mxser_mon_ext mon_data_ext;
315 static int mxser_set_baud_method[MXSER_PORTS + 1];
316 static spinlock_t gm_lock;
318 static int CheckIsMoxaMust(int io)
323 outb(0, io + UART_LCR);
324 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
325 oldmcr = inb(io + UART_MCR);
326 outb(0, io + UART_MCR);
327 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
328 if ((hwid = inb(io + UART_MCR)) != 0) {
329 outb(oldmcr, io + UART_MCR);
330 return MOXA_OTHER_UART;
333 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
334 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
335 if (hwid == Gpci_uart_info[i].type)
338 return MOXA_OTHER_UART;
341 static void process_txrx_fifo(struct mxser_port *info)
345 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
346 info->rx_trigger = 1;
347 info->rx_high_water = 1;
348 info->rx_low_water = 1;
349 info->xmit_fifo_size = 1;
351 for (i = 0; i < UART_INFO_NUM; i++)
352 if (info->board->chip_flag == Gpci_uart_info[i].type) {
353 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
354 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
355 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
356 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
361 static void mxser_do_softint(struct work_struct *work)
363 struct mxser_port *info = container_of(work, struct mxser_port, tqueue);
364 struct tty_struct *tty = info->tty;
366 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
370 static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
372 unsigned char status = 0;
374 status = inb(baseaddr + UART_MSR);
376 mxser_msr[port] &= 0x0F;
377 mxser_msr[port] |= status;
378 status = mxser_msr[port];
385 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
386 struct mxser_port *port)
388 DECLARE_WAITQUEUE(wait, current);
394 * If non-blocking mode is set, or the port is not enabled,
395 * then make the check up front and then exit.
397 if ((filp->f_flags & O_NONBLOCK) ||
398 test_bit(TTY_IO_ERROR, &tty->flags)) {
399 port->flags |= ASYNC_NORMAL_ACTIVE;
403 if (tty->termios->c_cflag & CLOCAL)
407 * Block waiting for the carrier detect and the line to become
408 * free (i.e., not in use by the callout). While we are in
409 * this loop, port->count is dropped by one, so that
410 * mxser_close() knows when to free things. We restore it upon
411 * exit, either normal or abnormal.
414 add_wait_queue(&port->open_wait, &wait);
416 spin_lock_irqsave(&port->slock, flags);
417 if (!tty_hung_up_p(filp))
419 spin_unlock_irqrestore(&port->slock, flags);
420 port->blocked_open++;
422 spin_lock_irqsave(&port->slock, flags);
423 outb(inb(port->ioaddr + UART_MCR) |
424 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
425 spin_unlock_irqrestore(&port->slock, flags);
426 set_current_state(TASK_INTERRUPTIBLE);
427 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
428 if (port->flags & ASYNC_HUP_NOTIFY)
431 retval = -ERESTARTSYS;
434 if (!(port->flags & ASYNC_CLOSING) &&
436 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
438 if (signal_pending(current)) {
439 retval = -ERESTARTSYS;
444 set_current_state(TASK_RUNNING);
445 remove_wait_queue(&port->open_wait, &wait);
446 if (!tty_hung_up_p(filp))
448 port->blocked_open--;
451 port->flags |= ASYNC_NORMAL_ACTIVE;
455 static int mxser_set_baud(struct mxser_port *info, long newspd)
462 if (!info->tty || !info->tty->termios)
468 if (newspd > info->max_baud)
471 info->realbaud = newspd;
473 quot = (2 * info->baud_base / 269);
475 quot = info->baud_base / newspd;
482 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
483 info->timeout += HZ / 50; /* Add .02 seconds of slop */
486 spin_lock_irqsave(&info->slock, flags);
487 info->MCR |= UART_MCR_DTR;
488 outb(info->MCR, info->ioaddr + UART_MCR);
489 spin_unlock_irqrestore(&info->slock, flags);
491 spin_lock_irqsave(&info->slock, flags);
492 info->MCR &= ~UART_MCR_DTR;
493 outb(info->MCR, info->ioaddr + UART_MCR);
494 spin_unlock_irqrestore(&info->slock, flags);
498 cval = inb(info->ioaddr + UART_LCR);
500 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
502 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
503 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
504 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
511 * This routine is called to set the UART divisor registers to match
512 * the specified baud rate for a serial port.
514 static int mxser_change_speed(struct mxser_port *info,
515 struct ktermios *old_termios)
517 unsigned cflag, cval, fcr;
519 unsigned char status;
523 if (!info->tty || !info->tty->termios)
525 cflag = info->tty->termios->c_cflag;
529 if (mxser_set_baud_method[info->tty->index] == 0) {
530 baud = tty_get_baud_rate(info->tty);
531 mxser_set_baud(info, baud);
534 /* byte size and parity */
535 switch (cflag & CSIZE) {
550 break; /* too keep GCC shut... */
555 cval |= UART_LCR_PARITY;
556 if (!(cflag & PARODD))
557 cval |= UART_LCR_EPAR;
559 cval |= UART_LCR_SPAR;
561 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
562 if (info->board->chip_flag) {
563 fcr = UART_FCR_ENABLE_FIFO;
564 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
565 SET_MOXA_MUST_FIFO_VALUE(info);
569 fcr = UART_FCR_ENABLE_FIFO;
570 if (info->board->chip_flag) {
571 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
572 SET_MOXA_MUST_FIFO_VALUE(info);
574 switch (info->rx_trigger) {
576 fcr |= UART_FCR_TRIGGER_1;
579 fcr |= UART_FCR_TRIGGER_4;
582 fcr |= UART_FCR_TRIGGER_8;
585 fcr |= UART_FCR_TRIGGER_14;
591 /* CTS flow control flag and modem status interrupts */
592 info->IER &= ~UART_IER_MSI;
593 info->MCR &= ~UART_MCR_AFE;
594 if (cflag & CRTSCTS) {
595 info->flags |= ASYNC_CTS_FLOW;
596 info->IER |= UART_IER_MSI;
597 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
598 info->MCR |= UART_MCR_AFE;
600 status = inb(info->ioaddr + UART_MSR);
601 if (info->tty->hw_stopped) {
602 if (status & UART_MSR_CTS) {
603 info->tty->hw_stopped = 0;
604 if (info->type != PORT_16550A &&
605 !info->board->chip_flag) {
606 outb(info->IER & ~UART_IER_THRI,
609 info->IER |= UART_IER_THRI;
610 outb(info->IER, info->ioaddr +
613 set_bit(MXSER_EVENT_TXLOW, &info->event);
614 schedule_work(&info->tqueue); }
616 if (!(status & UART_MSR_CTS)) {
617 info->tty->hw_stopped = 1;
618 if ((info->type != PORT_16550A) &&
619 (!info->board->chip_flag)) {
620 info->IER &= ~UART_IER_THRI;
621 outb(info->IER, info->ioaddr +
628 info->flags &= ~ASYNC_CTS_FLOW;
630 outb(info->MCR, info->ioaddr + UART_MCR);
631 if (cflag & CLOCAL) {
632 info->flags &= ~ASYNC_CHECK_CD;
634 info->flags |= ASYNC_CHECK_CD;
635 info->IER |= UART_IER_MSI;
637 outb(info->IER, info->ioaddr + UART_IER);
640 * Set up parity check flag
642 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
643 if (I_INPCK(info->tty))
644 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
645 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
646 info->read_status_mask |= UART_LSR_BI;
648 info->ignore_status_mask = 0;
650 if (I_IGNBRK(info->tty)) {
651 info->ignore_status_mask |= UART_LSR_BI;
652 info->read_status_mask |= UART_LSR_BI;
654 * If we're ignore parity and break indicators, ignore
655 * overruns too. (For real raw support).
657 if (I_IGNPAR(info->tty)) {
658 info->ignore_status_mask |=
662 info->read_status_mask |=
668 if (info->board->chip_flag) {
669 spin_lock_irqsave(&info->slock, flags);
670 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
671 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
672 if (I_IXON(info->tty)) {
673 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
675 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
677 if (I_IXOFF(info->tty)) {
678 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
680 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
682 spin_unlock_irqrestore(&info->slock, flags);
686 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
687 outb(cval, info->ioaddr + UART_LCR);
692 static void mxser_check_modem_status(struct mxser_port *port, int status)
694 /* update input line counters */
695 if (status & UART_MSR_TERI)
697 if (status & UART_MSR_DDSR)
699 if (status & UART_MSR_DDCD)
701 if (status & UART_MSR_DCTS)
703 port->mon_data.modem_status = status;
704 wake_up_interruptible(&port->delta_msr_wait);
706 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
707 if (status & UART_MSR_DCD)
708 wake_up_interruptible(&port->open_wait);
709 schedule_work(&port->tqueue);
712 if (port->flags & ASYNC_CTS_FLOW) {
713 if (port->tty->hw_stopped) {
714 if (status & UART_MSR_CTS) {
715 port->tty->hw_stopped = 0;
717 if ((port->type != PORT_16550A) &&
718 (!port->board->chip_flag)) {
719 outb(port->IER & ~UART_IER_THRI,
720 port->ioaddr + UART_IER);
721 port->IER |= UART_IER_THRI;
722 outb(port->IER, port->ioaddr +
725 set_bit(MXSER_EVENT_TXLOW, &port->event);
726 schedule_work(&port->tqueue);
729 if (!(status & UART_MSR_CTS)) {
730 port->tty->hw_stopped = 1;
731 if (port->type != PORT_16550A &&
732 !port->board->chip_flag) {
733 port->IER &= ~UART_IER_THRI;
734 outb(port->IER, port->ioaddr +
742 static int mxser_startup(struct mxser_port *info)
747 page = __get_free_page(GFP_KERNEL);
751 spin_lock_irqsave(&info->slock, flags);
753 if (info->flags & ASYNC_INITIALIZED) {
755 spin_unlock_irqrestore(&info->slock, flags);
759 if (!info->ioaddr || !info->type) {
761 set_bit(TTY_IO_ERROR, &info->tty->flags);
763 spin_unlock_irqrestore(&info->slock, flags);
769 info->xmit_buf = (unsigned char *) page;
772 * Clear the FIFO buffers and disable them
773 * (they will be reenabled in mxser_change_speed())
775 if (info->board->chip_flag)
776 outb((UART_FCR_CLEAR_RCVR |
777 UART_FCR_CLEAR_XMIT |
778 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
780 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
781 info->ioaddr + UART_FCR);
784 * At this point there's no way the LSR could still be 0xFF;
785 * if it is, then bail out, because there's likely no UART
788 if (inb(info->ioaddr + UART_LSR) == 0xff) {
789 spin_unlock_irqrestore(&info->slock, flags);
790 if (capable(CAP_SYS_ADMIN)) {
792 set_bit(TTY_IO_ERROR, &info->tty->flags);
799 * Clear the interrupt registers.
801 (void) inb(info->ioaddr + UART_LSR);
802 (void) inb(info->ioaddr + UART_RX);
803 (void) inb(info->ioaddr + UART_IIR);
804 (void) inb(info->ioaddr + UART_MSR);
807 * Now, initialize the UART
809 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
810 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
811 outb(info->MCR, info->ioaddr + UART_MCR);
814 * Finally, enable interrupts
816 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
818 if (info->board->chip_flag)
819 info->IER |= MOXA_MUST_IER_EGDAI;
820 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
823 * And clear the interrupt registers again for luck.
825 (void) inb(info->ioaddr + UART_LSR);
826 (void) inb(info->ioaddr + UART_RX);
827 (void) inb(info->ioaddr + UART_IIR);
828 (void) inb(info->ioaddr + UART_MSR);
831 clear_bit(TTY_IO_ERROR, &info->tty->flags);
832 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
835 * and set the speed of the serial port
837 spin_unlock_irqrestore(&info->slock, flags);
838 mxser_change_speed(info, NULL);
840 info->flags |= ASYNC_INITIALIZED;
845 * This routine will shutdown a serial port; interrupts maybe disabled, and
846 * DTR is dropped if the hangup on close termio flag is on.
848 static void mxser_shutdown(struct mxser_port *info)
852 if (!(info->flags & ASYNC_INITIALIZED))
855 spin_lock_irqsave(&info->slock, flags);
858 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
859 * here so the queue might never be waken up
861 wake_up_interruptible(&info->delta_msr_wait);
864 * Free the IRQ, if necessary
866 if (info->xmit_buf) {
867 free_page((unsigned long) info->xmit_buf);
868 info->xmit_buf = NULL;
872 outb(0x00, info->ioaddr + UART_IER);
874 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
875 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
876 outb(info->MCR, info->ioaddr + UART_MCR);
878 /* clear Rx/Tx FIFO's */
879 if (info->board->chip_flag)
880 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
881 MOXA_MUST_FCR_GDA_MODE_ENABLE,
882 info->ioaddr + UART_FCR);
884 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
885 info->ioaddr + UART_FCR);
887 /* read data port to reset things */
888 (void) inb(info->ioaddr + UART_RX);
891 set_bit(TTY_IO_ERROR, &info->tty->flags);
893 info->flags &= ~ASYNC_INITIALIZED;
895 if (info->board->chip_flag)
896 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
898 spin_unlock_irqrestore(&info->slock, flags);
902 * This routine is called whenever a serial port is opened. It
903 * enables interrupts for a serial port, linking in its async structure into
904 * the IRQ chain. It also performs the serial-specific
905 * initialization for the tty structure.
907 static int mxser_open(struct tty_struct *tty, struct file *filp)
909 struct mxser_port *info;
912 /* initialize driver_data in case something fails */
913 tty->driver_data = NULL;
916 if (line == MXSER_PORTS)
918 if (line < 0 || line > MXSER_PORTS)
920 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
924 tty->driver_data = info;
927 * Start up serial port
930 retval = mxser_startup(info);
934 retval = mxser_block_til_ready(tty, filp, info);
938 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
939 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
940 *tty->termios = info->normal_termios;
942 *tty->termios = info->callout_termios;
943 mxser_change_speed(info, NULL);
946 info->session = process_session(current);
947 info->pgrp = process_group(current);
949 /* unmark here for very high baud rate (ex. 921600 bps) used */
950 tty->low_latency = 1;
955 * This routine is called when the serial port gets closed. First, we
956 * wait for the last remaining data to be sent. Then, we unlink its
957 * async structure from the interrupt chain if necessary, and we free
958 * that IRQ if nothing is left in the chain.
960 static void mxser_close(struct tty_struct *tty, struct file *filp)
962 struct mxser_port *info = tty->driver_data;
964 unsigned long timeout;
967 if (tty->index == MXSER_PORTS)
972 spin_lock_irqsave(&info->slock, flags);
974 if (tty_hung_up_p(filp)) {
975 spin_unlock_irqrestore(&info->slock, flags);
978 if ((tty->count == 1) && (info->count != 1)) {
980 * Uh, oh. tty->count is 1, which means that the tty
981 * structure will be freed. Info->count should always
982 * be one in these conditions. If it's greater than
983 * one, we've got real problems, since it means the
984 * serial port won't be shutdown.
986 printk(KERN_ERR "mxser_close: bad serial port count; "
987 "tty->count is 1, info->count is %d\n", info->count);
990 if (--info->count < 0) {
991 printk(KERN_ERR "mxser_close: bad serial port count for "
992 "ttys%d: %d\n", tty->index, info->count);
996 spin_unlock_irqrestore(&info->slock, flags);
999 info->flags |= ASYNC_CLOSING;
1000 spin_unlock_irqrestore(&info->slock, flags);
1002 * Save the termios structure, since this port may have
1003 * separate termios for callout and dialin.
1005 if (info->flags & ASYNC_NORMAL_ACTIVE)
1006 info->normal_termios = *tty->termios;
1008 * Now we wait for the transmit buffer to clear; and we notify
1009 * the line discipline to only process XON/XOFF characters.
1012 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1013 tty_wait_until_sent(tty, info->closing_wait);
1015 * At this point we stop accepting input. To do this, we
1016 * disable the receive line status interrupts, and tell the
1017 * interrupt driver to stop checking the data ready bit in the
1018 * line status register.
1020 info->IER &= ~UART_IER_RLSI;
1021 if (info->board->chip_flag)
1022 info->IER &= ~MOXA_MUST_RECV_ISR;
1024 if (info->flags & ASYNC_INITIALIZED) {
1025 outb(info->IER, info->ioaddr + UART_IER);
1027 * Before we drop DTR, make sure the UART transmitter
1028 * has completely drained; this is especially
1029 * important if there is a transmit FIFO!
1031 timeout = jiffies + HZ;
1032 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
1033 schedule_timeout_interruptible(5);
1034 if (time_after(jiffies, timeout))
1038 mxser_shutdown(info);
1040 if (tty->driver->flush_buffer)
1041 tty->driver->flush_buffer(tty);
1043 tty_ldisc_flush(tty);
1048 if (info->blocked_open) {
1049 if (info->close_delay)
1050 schedule_timeout_interruptible(info->close_delay);
1051 wake_up_interruptible(&info->open_wait);
1054 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1055 wake_up_interruptible(&info->close_wait);
1059 static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1062 struct mxser_port *info = tty->driver_data;
1063 unsigned long flags;
1065 if (!info->xmit_buf)
1069 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1070 SERIAL_XMIT_SIZE - info->xmit_head));
1074 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1075 spin_lock_irqsave(&info->slock, flags);
1076 info->xmit_head = (info->xmit_head + c) &
1077 (SERIAL_XMIT_SIZE - 1);
1078 info->xmit_cnt += c;
1079 spin_unlock_irqrestore(&info->slock, flags);
1086 if (info->xmit_cnt && !tty->stopped) {
1087 if (!tty->hw_stopped ||
1088 (info->type == PORT_16550A) ||
1089 (info->board->chip_flag)) {
1090 spin_lock_irqsave(&info->slock, flags);
1091 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1093 info->IER |= UART_IER_THRI;
1094 outb(info->IER, info->ioaddr + UART_IER);
1095 spin_unlock_irqrestore(&info->slock, flags);
1101 static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1103 struct mxser_port *info = tty->driver_data;
1104 unsigned long flags;
1106 if (!info->xmit_buf)
1109 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1112 spin_lock_irqsave(&info->slock, flags);
1113 info->xmit_buf[info->xmit_head++] = ch;
1114 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1116 spin_unlock_irqrestore(&info->slock, flags);
1117 if (!tty->stopped) {
1118 if (!tty->hw_stopped ||
1119 (info->type == PORT_16550A) ||
1120 info->board->chip_flag) {
1121 spin_lock_irqsave(&info->slock, flags);
1122 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1123 info->IER |= UART_IER_THRI;
1124 outb(info->IER, info->ioaddr + UART_IER);
1125 spin_unlock_irqrestore(&info->slock, flags);
1131 static void mxser_flush_chars(struct tty_struct *tty)
1133 struct mxser_port *info = tty->driver_data;
1134 unsigned long flags;
1136 if (info->xmit_cnt <= 0 ||
1140 (info->type != PORT_16550A) &&
1141 (!info->board->chip_flag)
1145 spin_lock_irqsave(&info->slock, flags);
1147 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1148 info->IER |= UART_IER_THRI;
1149 outb(info->IER, info->ioaddr + UART_IER);
1151 spin_unlock_irqrestore(&info->slock, flags);
1154 static int mxser_write_room(struct tty_struct *tty)
1156 struct mxser_port *info = tty->driver_data;
1159 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1165 static int mxser_chars_in_buffer(struct tty_struct *tty)
1167 struct mxser_port *info = tty->driver_data;
1168 return info->xmit_cnt;
1171 static void mxser_flush_buffer(struct tty_struct *tty)
1173 struct mxser_port *info = tty->driver_data;
1175 unsigned long flags;
1178 spin_lock_irqsave(&info->slock, flags);
1179 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1181 fcr = inb(info->ioaddr + UART_FCR);
1182 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1183 info->ioaddr + UART_FCR);
1184 outb(fcr, info->ioaddr + UART_FCR);
1186 spin_unlock_irqrestore(&info->slock, flags);
1192 * ------------------------------------------------------------
1193 * friends of mxser_ioctl()
1194 * ------------------------------------------------------------
1196 static int mxser_get_serial_info(struct mxser_port *info,
1197 struct serial_struct __user *retinfo)
1199 struct serial_struct tmp;
1203 memset(&tmp, 0, sizeof(tmp));
1204 tmp.type = info->type;
1205 tmp.line = info->tty->index;
1206 tmp.port = info->ioaddr;
1207 tmp.irq = info->board->irq;
1208 tmp.flags = info->flags;
1209 tmp.baud_base = info->baud_base;
1210 tmp.close_delay = info->close_delay;
1211 tmp.closing_wait = info->closing_wait;
1212 tmp.custom_divisor = info->custom_divisor;
1214 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1219 static int mxser_set_serial_info(struct mxser_port *info,
1220 struct serial_struct __user *new_info)
1222 struct serial_struct new_serial;
1226 if (!new_info || !info->ioaddr)
1228 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1231 if ((new_serial.irq != info->board->irq) ||
1232 (new_serial.port != info->ioaddr) ||
1233 (new_serial.custom_divisor != info->custom_divisor) ||
1234 (new_serial.baud_base != info->baud_base))
1237 flags = info->flags & ASYNC_SPD_MASK;
1239 if (!capable(CAP_SYS_ADMIN)) {
1240 if ((new_serial.baud_base != info->baud_base) ||
1241 (new_serial.close_delay != info->close_delay) ||
1242 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
1244 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1245 (new_serial.flags & ASYNC_USR_MASK));
1248 * OK, past this point, all the error checking has been done.
1249 * At this point, we start making changes.....
1251 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1252 (new_serial.flags & ASYNC_FLAGS));
1253 info->close_delay = new_serial.close_delay * HZ / 100;
1254 info->closing_wait = new_serial.closing_wait * HZ / 100;
1255 info->tty->low_latency =
1256 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1257 info->tty->low_latency = 0;
1260 info->type = new_serial.type;
1262 process_txrx_fifo(info);
1264 if (info->flags & ASYNC_INITIALIZED) {
1265 if (flags != (info->flags & ASYNC_SPD_MASK))
1266 mxser_change_speed(info, NULL);
1268 retval = mxser_startup(info);
1274 * mxser_get_lsr_info - get line status register info
1276 * Purpose: Let user call ioctl() to get info when the UART physically
1277 * is emptied. On bus types like RS485, the transmitter must
1278 * release the bus after transmitting. This must be done when
1279 * the transmit shift register is empty, not be done when the
1280 * transmit holding register is empty. This functionality
1281 * allows an RS485 driver to be written in user space.
1283 static int mxser_get_lsr_info(struct mxser_port *info,
1284 unsigned int __user *value)
1286 unsigned char status;
1287 unsigned int result;
1288 unsigned long flags;
1290 spin_lock_irqsave(&info->slock, flags);
1291 status = inb(info->ioaddr + UART_LSR);
1292 spin_unlock_irqrestore(&info->slock, flags);
1293 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1294 return put_user(result, value);
1298 * This routine sends a break character out the serial port.
1300 static void mxser_send_break(struct mxser_port *info, int duration)
1302 unsigned long flags;
1306 set_current_state(TASK_INTERRUPTIBLE);
1307 spin_lock_irqsave(&info->slock, flags);
1308 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1309 info->ioaddr + UART_LCR);
1310 spin_unlock_irqrestore(&info->slock, flags);
1311 schedule_timeout(duration);
1312 spin_lock_irqsave(&info->slock, flags);
1313 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1314 info->ioaddr + UART_LCR);
1315 spin_unlock_irqrestore(&info->slock, flags);
1318 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
1320 struct mxser_port *info = tty->driver_data;
1321 unsigned char control, status;
1322 unsigned long flags;
1325 if (tty->index == MXSER_PORTS)
1326 return -ENOIOCTLCMD;
1327 if (test_bit(TTY_IO_ERROR, &tty->flags))
1330 control = info->MCR;
1332 spin_lock_irqsave(&info->slock, flags);
1333 status = inb(info->ioaddr + UART_MSR);
1334 if (status & UART_MSR_ANY_DELTA)
1335 mxser_check_modem_status(info, status);
1336 spin_unlock_irqrestore(&info->slock, flags);
1337 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1338 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1339 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1340 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1341 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1342 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1345 static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1346 unsigned int set, unsigned int clear)
1348 struct mxser_port *info = tty->driver_data;
1349 unsigned long flags;
1352 if (tty->index == MXSER_PORTS)
1353 return -ENOIOCTLCMD;
1354 if (test_bit(TTY_IO_ERROR, &tty->flags))
1357 spin_lock_irqsave(&info->slock, flags);
1359 if (set & TIOCM_RTS)
1360 info->MCR |= UART_MCR_RTS;
1361 if (set & TIOCM_DTR)
1362 info->MCR |= UART_MCR_DTR;
1364 if (clear & TIOCM_RTS)
1365 info->MCR &= ~UART_MCR_RTS;
1366 if (clear & TIOCM_DTR)
1367 info->MCR &= ~UART_MCR_DTR;
1369 outb(info->MCR, info->ioaddr + UART_MCR);
1370 spin_unlock_irqrestore(&info->slock, flags);
1374 static int mxser_program_mode(int port)
1378 spin_lock(&gm_lock);
1386 spin_unlock(&gm_lock);
1388 id = inb(port + 1) & 0x1F;
1389 if ((id != C168_ASIC_ID) &&
1390 (id != C104_ASIC_ID) &&
1391 (id != C102_ASIC_ID) &&
1392 (id != CI132_ASIC_ID) &&
1393 (id != CI134_ASIC_ID) &&
1394 (id != CI104J_ASIC_ID))
1396 for (i = 0, j = 0; i < 4; i++) {
1400 } else if ((j == 1) && (n == 1)) {
1411 static void mxser_normal_mode(int port)
1415 outb(0xA5, port + 1);
1416 outb(0x80, port + 3);
1417 outb(12, port + 0); /* 9600 bps */
1419 outb(0x03, port + 3); /* 8 data bits */
1420 outb(0x13, port + 4); /* loop back mode */
1421 for (i = 0; i < 16; i++) {
1423 if ((n & 0x61) == 0x60)
1428 outb(0x00, port + 4);
1431 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1432 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1433 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1434 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1435 #define EN_CCMD 0x000 /* Chip's command register */
1436 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1437 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1438 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1439 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1440 #define EN0_DCFG 0x00E /* Data configuration reg WR */
1441 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1442 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1443 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1444 static int mxser_read_register(int port, unsigned short *regs)
1446 int i, k, value, id;
1449 id = mxser_program_mode(port);
1452 for (i = 0; i < 14; i++) {
1453 k = (i & 0x3F) | 0x180;
1454 for (j = 0x100; j > 0; j >>= 1) {
1455 outb(CHIP_CS, port);
1457 outb(CHIP_CS | CHIP_DO, port);
1458 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1460 outb(CHIP_CS, port);
1461 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1466 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1467 outb(CHIP_CS, port);
1468 outb(CHIP_CS | CHIP_SK, port);
1469 if (inb(port) & CHIP_DI)
1475 mxser_normal_mode(port);
1479 static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1481 struct mxser_port *port;
1487 /* if (copy_to_user(argp, mxsercfg,
1488 sizeof(struct mxser_hwconf) * 4))
1492 case MOXA_GET_MAJOR:
1493 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1497 case MOXA_GET_CUMAJOR:
1498 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1502 case MOXA_CHKPORTENABLE:
1505 for (i = 0; i < MXSER_BOARDS; i++)
1506 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1507 if (mxser_boards[i].ports[j].ioaddr)
1510 return put_user(result, (unsigned long __user *)argp);
1511 case MOXA_GETDATACOUNT:
1512 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1515 case MOXA_GETMSTATUS:
1516 for (i = 0; i < MXSER_BOARDS; i++)
1517 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1518 port = &mxser_boards[i].ports[j];
1521 if (!port->ioaddr) {
1522 GMStatus[i].dcd = 0;
1523 GMStatus[i].dsr = 0;
1524 GMStatus[i].cts = 0;
1528 if (!port->tty || !port->tty->termios)
1530 port->normal_termios.c_cflag;
1533 port->tty->termios->c_cflag;
1535 status = inb(port->ioaddr + UART_MSR);
1536 if (status & 0x80 /*UART_MSR_DCD */ )
1537 GMStatus[i].dcd = 1;
1539 GMStatus[i].dcd = 0;
1541 if (status & 0x20 /*UART_MSR_DSR */ )
1542 GMStatus[i].dsr = 1;
1544 GMStatus[i].dsr = 0;
1547 if (status & 0x10 /*UART_MSR_CTS */ )
1548 GMStatus[i].cts = 1;
1550 GMStatus[i].cts = 0;
1552 if (copy_to_user(argp, GMStatus,
1553 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1556 case MOXA_ASPP_MON_EXT: {
1557 int status, p, shiftbit;
1558 unsigned long opmode;
1559 unsigned cflag, iflag;
1561 for (i = 0; i < MXSER_BOARDS; i++)
1562 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1563 port = &mxser_boards[i].ports[j];
1567 status = mxser_get_msr(port->ioaddr, 0, i);
1569 if (status & UART_MSR_TERI)
1571 if (status & UART_MSR_DDSR)
1573 if (status & UART_MSR_DDCD)
1575 if (status & UART_MSR_DCTS)
1578 port->mon_data.modem_status = status;
1579 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1580 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1581 mon_data_ext.up_rxcnt[i] =
1582 port->mon_data.up_rxcnt;
1583 mon_data_ext.up_txcnt[i] =
1584 port->mon_data.up_txcnt;
1585 mon_data_ext.modem_status[i] =
1586 port->mon_data.modem_status;
1587 mon_data_ext.baudrate[i] = port->realbaud;
1589 if (!port->tty || !port->tty->termios) {
1590 cflag = port->normal_termios.c_cflag;
1591 iflag = port->normal_termios.c_iflag;
1593 cflag = port->tty->termios->c_cflag;
1594 iflag = port->tty->termios->c_iflag;
1597 mon_data_ext.databits[i] = cflag & CSIZE;
1599 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1601 mon_data_ext.parity[i] =
1602 cflag & (PARENB | PARODD | CMSPAR);
1604 mon_data_ext.flowctrl[i] = 0x00;
1606 if (cflag & CRTSCTS)
1607 mon_data_ext.flowctrl[i] |= 0x03;
1609 if (iflag & (IXON | IXOFF))
1610 mon_data_ext.flowctrl[i] |= 0x0C;
1612 if (port->type == PORT_16550A)
1613 mon_data_ext.fifo[i] = 1;
1615 mon_data_ext.fifo[i] = 0;
1619 opmode = inb(port->opmode_ioaddr) >> shiftbit;
1620 opmode &= OP_MODE_MASK;
1622 mon_data_ext.iftype[i] = opmode;
1625 if (copy_to_user(argp, &mon_data_ext,
1626 sizeof(mon_data_ext)))
1632 return -ENOIOCTLCMD;
1637 static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1638 unsigned int cmd, unsigned long arg)
1640 struct mxser_port *info = tty->driver_data;
1641 struct async_icount cprev, cnow; /* kernel counter temps */
1642 struct serial_icounter_struct __user *p_cuser;
1643 unsigned long templ;
1644 unsigned long flags;
1645 void __user *argp = (void __user *)arg;
1648 if (tty->index == MXSER_PORTS)
1649 return mxser_ioctl_special(cmd, argp);
1651 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1653 unsigned long opmode;
1654 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1656 unsigned char val, mask;
1659 if (cmd == MOXA_SET_OP_MODE) {
1660 if (get_user(opmode, (int __user *) argp))
1662 if (opmode != RS232_MODE &&
1663 opmode != RS485_2WIRE_MODE &&
1664 opmode != RS422_MODE &&
1665 opmode != RS485_4WIRE_MODE)
1669 val = inb(info->opmode_ioaddr);
1671 val |= (opmode << shiftbit);
1672 outb(val, info->opmode_ioaddr);
1675 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1676 opmode &= OP_MODE_MASK;
1677 if (copy_to_user(argp, &opmode, sizeof(int)))
1683 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1684 test_bit(TTY_IO_ERROR, &tty->flags))
1688 case TCSBRK: /* SVID version: non-zero arg --> no break */
1689 retval = tty_check_change(tty);
1692 tty_wait_until_sent(tty, 0);
1694 mxser_send_break(info, HZ / 4); /* 1/4 second */
1696 case TCSBRKP: /* support for POSIX tcsendbreak() */
1697 retval = tty_check_change(tty);
1700 tty_wait_until_sent(tty, 0);
1701 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1704 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1706 if (get_user(templ, (unsigned long __user *) argp))
1709 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1712 return mxser_get_serial_info(info, argp);
1714 return mxser_set_serial_info(info, argp);
1715 case TIOCSERGETLSR: /* Get line status register */
1716 return mxser_get_lsr_info(info, argp);
1718 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1719 * - mask passed in arg for lines of interest
1720 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1721 * Caller should use TIOCGICOUNT to see which one it was
1724 DECLARE_WAITQUEUE(wait, current);
1726 spin_lock_irqsave(&info->slock, flags);
1727 cprev = info->icount; /* note the counters on entry */
1728 spin_unlock_irqrestore(&info->slock, flags);
1730 add_wait_queue(&info->delta_msr_wait, &wait);
1732 spin_lock_irqsave(&info->slock, flags);
1733 cnow = info->icount; /* atomic copy */
1734 spin_unlock_irqrestore(&info->slock, flags);
1736 set_current_state(TASK_INTERRUPTIBLE);
1737 if (((arg & TIOCM_RNG) &&
1738 (cnow.rng != cprev.rng)) ||
1739 ((arg & TIOCM_DSR) &&
1740 (cnow.dsr != cprev.dsr)) ||
1741 ((arg & TIOCM_CD) &&
1742 (cnow.dcd != cprev.dcd)) ||
1743 ((arg & TIOCM_CTS) &&
1744 (cnow.cts != cprev.cts))) {
1748 /* see if a signal did it */
1749 if (signal_pending(current)) {
1755 current->state = TASK_RUNNING;
1756 remove_wait_queue(&info->delta_msr_wait, &wait);
1761 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1762 * Return: write counters to the user passed counter struct
1763 * NB: both 1->0 and 0->1 transitions are counted except for
1764 * RI where only 0->1 is counted.
1767 spin_lock_irqsave(&info->slock, flags);
1768 cnow = info->icount;
1769 spin_unlock_irqrestore(&info->slock, flags);
1771 if (put_user(cnow.frame, &p_cuser->frame))
1773 if (put_user(cnow.brk, &p_cuser->brk))
1775 if (put_user(cnow.overrun, &p_cuser->overrun))
1777 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1779 if (put_user(cnow.parity, &p_cuser->parity))
1781 if (put_user(cnow.rx, &p_cuser->rx))
1783 if (put_user(cnow.tx, &p_cuser->tx))
1785 put_user(cnow.cts, &p_cuser->cts);
1786 put_user(cnow.dsr, &p_cuser->dsr);
1787 put_user(cnow.rng, &p_cuser->rng);
1788 put_user(cnow.dcd, &p_cuser->dcd);
1790 case MOXA_HighSpeedOn:
1791 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1792 case MOXA_SDS_RSTICOUNTER:
1793 info->mon_data.rxcnt = 0;
1794 info->mon_data.txcnt = 0;
1796 case MOXA_ASPP_SETBAUD:{
1798 if (get_user(baud, (long __user *)argp))
1800 mxser_set_baud(info, baud);
1803 case MOXA_ASPP_GETBAUD:
1804 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1809 case MOXA_ASPP_OQUEUE:{
1812 len = mxser_chars_in_buffer(tty);
1814 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1816 len += (lsr ? 0 : 1);
1818 if (copy_to_user(argp, &len, sizeof(int)))
1823 case MOXA_ASPP_MON: {
1826 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1827 mxser_check_modem_status(info, status);
1829 mcr = inb(info->ioaddr + UART_MCR);
1830 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1831 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1833 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1835 if (mcr & MOXA_MUST_MCR_TX_XON)
1836 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1838 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1840 if (info->tty->hw_stopped)
1841 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1843 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1845 if (copy_to_user(argp, &info->mon_data,
1846 sizeof(struct mxser_mon)))
1851 case MOXA_ASPP_LSTATUS: {
1852 if (copy_to_user(argp, &info->err_shadow,
1853 sizeof(unsigned char)))
1856 info->err_shadow = 0;
1859 case MOXA_SET_BAUD_METHOD: {
1862 if (get_user(method, (int __user *)argp))
1864 mxser_set_baud_method[tty->index] = method;
1865 if (copy_to_user(argp, &method, sizeof(int)))
1871 return -ENOIOCTLCMD;
1876 static void mxser_stoprx(struct tty_struct *tty)
1878 struct mxser_port *info = tty->driver_data;
1880 info->ldisc_stop_rx = 1;
1882 if (info->board->chip_flag) {
1883 info->IER &= ~MOXA_MUST_RECV_ISR;
1884 outb(info->IER, info->ioaddr + UART_IER);
1886 info->x_char = STOP_CHAR(tty);
1887 outb(0, info->ioaddr + UART_IER);
1888 info->IER |= UART_IER_THRI;
1889 outb(info->IER, info->ioaddr + UART_IER);
1893 if (info->tty->termios->c_cflag & CRTSCTS) {
1894 info->MCR &= ~UART_MCR_RTS;
1895 outb(info->MCR, info->ioaddr + UART_MCR);
1900 * This routine is called by the upper-layer tty layer to signal that
1901 * incoming characters should be throttled.
1903 static void mxser_throttle(struct tty_struct *tty)
1908 static void mxser_unthrottle(struct tty_struct *tty)
1910 struct mxser_port *info = tty->driver_data;
1913 info->ldisc_stop_rx = 0;
1918 if (info->board->chip_flag) {
1919 info->IER |= MOXA_MUST_RECV_ISR;
1920 outb(info->IER, info->ioaddr + UART_IER);
1922 info->x_char = START_CHAR(tty);
1923 outb(0, info->ioaddr + UART_IER);
1924 info->IER |= UART_IER_THRI;
1925 outb(info->IER, info->ioaddr + UART_IER);
1930 if (info->tty->termios->c_cflag & CRTSCTS) {
1931 info->MCR |= UART_MCR_RTS;
1932 outb(info->MCR, info->ioaddr + UART_MCR);
1937 * mxser_stop() and mxser_start()
1939 * This routines are called before setting or resetting tty->stopped.
1940 * They enable or disable transmitter interrupts, as necessary.
1942 static void mxser_stop(struct tty_struct *tty)
1944 struct mxser_port *info = tty->driver_data;
1945 unsigned long flags;
1947 spin_lock_irqsave(&info->slock, flags);
1948 if (info->IER & UART_IER_THRI) {
1949 info->IER &= ~UART_IER_THRI;
1950 outb(info->IER, info->ioaddr + UART_IER);
1952 spin_unlock_irqrestore(&info->slock, flags);
1955 static void mxser_start(struct tty_struct *tty)
1957 struct mxser_port *info = tty->driver_data;
1958 unsigned long flags;
1960 spin_lock_irqsave(&info->slock, flags);
1961 if (info->xmit_cnt && info->xmit_buf) {
1962 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1963 info->IER |= UART_IER_THRI;
1964 outb(info->IER, info->ioaddr + UART_IER);
1966 spin_unlock_irqrestore(&info->slock, flags);
1969 static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1971 struct mxser_port *info = tty->driver_data;
1972 unsigned long flags;
1974 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1975 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
1977 mxser_change_speed(info, old_termios);
1979 if ((old_termios->c_cflag & CRTSCTS) &&
1980 !(tty->termios->c_cflag & CRTSCTS)) {
1981 tty->hw_stopped = 0;
1986 /* Handle sw stopped */
1987 if ((old_termios->c_iflag & IXON) &&
1988 !(tty->termios->c_iflag & IXON)) {
1991 if (info->board->chip_flag) {
1992 spin_lock_irqsave(&info->slock, flags);
1993 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
1994 spin_unlock_irqrestore(&info->slock, flags);
2002 * mxser_wait_until_sent() --- wait until the transmitter is empty
2004 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2006 struct mxser_port *info = tty->driver_data;
2007 unsigned long orig_jiffies, char_time;
2010 if (info->type == PORT_UNKNOWN)
2013 if (info->xmit_fifo_size == 0)
2014 return; /* Just in case.... */
2016 orig_jiffies = jiffies;
2018 * Set the check interval to be 1/5 of the estimated time to
2019 * send a single character, and make it at least 1. The check
2020 * interval should also be less than the timeout.
2022 * Note: we have to use pretty tight timings here to satisfy
2025 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2026 char_time = char_time / 5;
2029 if (timeout && timeout < char_time)
2030 char_time = timeout;
2032 * If the transmitter hasn't cleared in twice the approximate
2033 * amount of time to send the entire FIFO, it probably won't
2034 * ever clear. This assumes the UART isn't doing flow
2035 * control, which is currently the case. Hence, if it ever
2036 * takes longer than info->timeout, this is probably due to a
2037 * UART bug of some kind. So, we clamp the timeout parameter at
2040 if (!timeout || timeout > 2 * info->timeout)
2041 timeout = 2 * info->timeout;
2042 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2043 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2044 timeout, char_time);
2045 printk("jiff=%lu...", jiffies);
2047 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
2048 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2049 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2051 schedule_timeout_interruptible(char_time);
2052 if (signal_pending(current))
2054 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2057 set_current_state(TASK_RUNNING);
2059 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2060 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2065 * This routine is called by tty_hangup() when a hangup is signaled.
2067 void mxser_hangup(struct tty_struct *tty)
2069 struct mxser_port *info = tty->driver_data;
2071 mxser_flush_buffer(tty);
2072 mxser_shutdown(info);
2075 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2077 wake_up_interruptible(&info->open_wait);
2081 * mxser_rs_break() --- routine which turns the break handling on or off
2083 static void mxser_rs_break(struct tty_struct *tty, int break_state)
2085 struct mxser_port *info = tty->driver_data;
2086 unsigned long flags;
2088 spin_lock_irqsave(&info->slock, flags);
2089 if (break_state == -1)
2090 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2091 info->ioaddr + UART_LCR);
2093 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2094 info->ioaddr + UART_LCR);
2095 spin_unlock_irqrestore(&info->slock, flags);
2098 static void mxser_receive_chars(struct mxser_port *port, int *status)
2100 struct tty_struct *tty = port->tty;
2101 unsigned char ch, gdl;
2106 unsigned long flags;
2108 spin_lock_irqsave(&port->slock, flags);
2110 recv_room = tty->receive_room;
2111 if ((recv_room == 0) && (!port->ldisc_stop_rx))
2114 if (port->board->chip_flag != MOXA_OTHER_UART) {
2116 if (*status & UART_LSR_SPECIAL)
2118 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
2119 (*status & MOXA_MUST_LSR_RERR))
2121 if (*status & MOXA_MUST_LSR_RERR)
2124 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
2126 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
2127 gdl &= MOXA_MUST_GDL_MASK;
2128 if (gdl >= recv_room) {
2129 if (!port->ldisc_stop_rx)
2133 ch = inb(port->ioaddr + UART_RX);
2134 tty_insert_flip_char(tty, ch, 0);
2145 ch = inb(port->ioaddr + UART_RX);
2146 if (port->board->chip_flag && (*status & UART_LSR_OE))
2147 outb(0x23, port->ioaddr + UART_FCR);
2148 *status &= port->read_status_mask;
2149 if (*status & port->ignore_status_mask) {
2150 if (++ignored > 100)
2154 if (*status & UART_LSR_SPECIAL) {
2155 if (*status & UART_LSR_BI) {
2159 if (port->flags & ASYNC_SAK)
2161 } else if (*status & UART_LSR_PE) {
2163 port->icount.parity++;
2164 } else if (*status & UART_LSR_FE) {
2166 port->icount.frame++;
2167 } else if (*status & UART_LSR_OE) {
2169 port->icount.overrun++;
2172 tty_insert_flip_char(tty, ch, flag);
2174 if (cnt >= recv_room) {
2175 if (!port->ldisc_stop_rx)
2182 if (port->board->chip_flag)
2185 *status = inb(port->ioaddr + UART_LSR);
2186 } while (*status & UART_LSR_DR);
2189 mxvar_log.rxcnt[port->tty->index] += cnt;
2190 port->mon_data.rxcnt += cnt;
2191 port->mon_data.up_rxcnt += cnt;
2192 spin_unlock_irqrestore(&port->slock, flags);
2194 tty_flip_buffer_push(tty);
2197 static void mxser_transmit_chars(struct mxser_port *port)
2200 unsigned long flags;
2202 spin_lock_irqsave(&port->slock, flags);
2205 outb(port->x_char, port->ioaddr + UART_TX);
2207 mxvar_log.txcnt[port->tty->index]++;
2208 port->mon_data.txcnt++;
2209 port->mon_data.up_txcnt++;
2214 if (port->xmit_buf == 0)
2217 if ((port->xmit_cnt <= 0) || port->tty->stopped ||
2218 (port->tty->hw_stopped &&
2219 (port->type != PORT_16550A) &&
2220 (!port->board->chip_flag))) {
2221 port->IER &= ~UART_IER_THRI;
2222 outb(port->IER, port->ioaddr + UART_IER);
2226 cnt = port->xmit_cnt;
2227 count = port->xmit_fifo_size;
2229 outb(port->xmit_buf[port->xmit_tail++],
2230 port->ioaddr + UART_TX);
2231 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2232 if (--port->xmit_cnt <= 0)
2234 } while (--count > 0);
2235 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
2237 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2238 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2239 port->icount.tx += (cnt - port->xmit_cnt);
2241 if (port->xmit_cnt < WAKEUP_CHARS) {
2242 set_bit(MXSER_EVENT_TXLOW, &port->event);
2243 schedule_work(&port->tqueue);
2245 if (port->xmit_cnt <= 0) {
2246 port->IER &= ~UART_IER_THRI;
2247 outb(port->IER, port->ioaddr + UART_IER);
2250 spin_unlock_irqrestore(&port->slock, flags);
2254 * This is the serial driver's generic interrupt routine
2256 static irqreturn_t mxser_interrupt(int irq, void *dev_id)
2259 struct mxser_board *brd = NULL;
2260 struct mxser_port *port;
2261 int max, irqbits, bits, msr;
2262 int pass_counter = 0;
2263 unsigned int int_cnt;
2264 int handled = IRQ_NONE;
2266 for (i = 0; i < MXSER_BOARDS; i++)
2267 if (dev_id == &mxser_boards[i]) {
2272 if (i == MXSER_BOARDS)
2276 max = brd->info->nports;
2278 irqbits = inb(brd->vector) & brd->vector_mask;
2279 if (irqbits == brd->vector_mask)
2282 handled = IRQ_HANDLED;
2283 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2284 if (irqbits == brd->vector_mask)
2288 port = &brd->ports[i];
2292 iir = inb(port->ioaddr + UART_IIR);
2293 if (iir & UART_IIR_NO_INT)
2295 iir &= MOXA_MUST_IIR_MASK;
2297 status = inb(port->ioaddr + UART_LSR);
2298 outb(0x27, port->ioaddr + UART_FCR);
2299 inb(port->ioaddr + UART_MSR);
2303 status = inb(port->ioaddr + UART_LSR);
2305 if (status & UART_LSR_PE)
2306 port->err_shadow |= NPPI_NOTIFY_PARITY;
2307 if (status & UART_LSR_FE)
2308 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2309 if (status & UART_LSR_OE)
2311 NPPI_NOTIFY_HW_OVERRUN;
2312 if (status & UART_LSR_BI)
2313 port->err_shadow |= NPPI_NOTIFY_BREAK;
2315 if (port->board->chip_flag) {
2316 if (iir == MOXA_MUST_IIR_GDA ||
2317 iir == MOXA_MUST_IIR_RDA ||
2318 iir == MOXA_MUST_IIR_RTO ||
2319 iir == MOXA_MUST_IIR_LSR)
2320 mxser_receive_chars(port,
2324 status &= port->read_status_mask;
2325 if (status & UART_LSR_DR)
2326 mxser_receive_chars(port,
2329 msr = inb(port->ioaddr + UART_MSR);
2330 if (msr & UART_MSR_ANY_DELTA)
2331 mxser_check_modem_status(port, msr);
2333 if (port->board->chip_flag) {
2334 if (iir == 0x02 && (status &
2336 mxser_transmit_chars(port);
2338 if (status & UART_LSR_THRE)
2339 mxser_transmit_chars(port);
2341 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2343 if (pass_counter++ > MXSER_ISR_PASS_LIMIT)
2344 break; /* Prevent infinite loops */
2351 static const struct tty_operations mxser_ops = {
2353 .close = mxser_close,
2354 .write = mxser_write,
2355 .put_char = mxser_put_char,
2356 .flush_chars = mxser_flush_chars,
2357 .write_room = mxser_write_room,
2358 .chars_in_buffer = mxser_chars_in_buffer,
2359 .flush_buffer = mxser_flush_buffer,
2360 .ioctl = mxser_ioctl,
2361 .throttle = mxser_throttle,
2362 .unthrottle = mxser_unthrottle,
2363 .set_termios = mxser_set_termios,
2365 .start = mxser_start,
2366 .hangup = mxser_hangup,
2367 .break_ctl = mxser_rs_break,
2368 .wait_until_sent = mxser_wait_until_sent,
2369 .tiocmget = mxser_tiocmget,
2370 .tiocmset = mxser_tiocmset,
2374 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2377 static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
2381 free_irq(brd->irq, brd);
2382 if (pdev != NULL) { /* PCI */
2383 pci_release_region(pdev, 2);
2384 pci_release_region(pdev, 3);
2387 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2388 release_region(brd->vector, 1);
2392 static int __devinit mxser_initbrd(struct mxser_board *brd,
2393 struct pci_dev *pdev)
2395 struct mxser_port *info;
2399 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
2401 for (i = 0; i < brd->info->nports; i++) {
2402 info = &brd->ports[i];
2405 info->ldisc_stop_rx = 0;
2407 /* Enhance mode enabled here */
2408 if (brd->chip_flag != MOXA_OTHER_UART)
2409 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
2411 info->flags = ASYNC_SHARE_IRQ;
2412 info->type = brd->uart_type;
2414 process_txrx_fifo(info);
2416 info->custom_divisor = info->baud_base * 16;
2417 info->close_delay = 5 * HZ / 10;
2418 info->closing_wait = 30 * HZ;
2419 INIT_WORK(&info->tqueue, mxser_do_softint);
2420 info->normal_termios = mxvar_sdriver->init_termios;
2421 init_waitqueue_head(&info->open_wait);
2422 init_waitqueue_head(&info->close_wait);
2423 init_waitqueue_head(&info->delta_msr_wait);
2424 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2425 info->err_shadow = 0;
2426 spin_lock_init(&info->slock);
2428 /* before set INT ISR, disable all int */
2429 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2430 info->ioaddr + UART_IER);
2433 * Allocate the IRQ if necessary
2436 retval = request_irq(brd->irq, mxser_interrupt,
2437 (brd->ports[0].flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED :
2438 IRQF_DISABLED, "mxser", brd);
2440 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2441 "conflict with another device.\n",
2442 brd->info->name, brd->irq);
2443 /* We hold resources, we need to release them. */
2444 mxser_release_res(brd, pdev, 0);
2450 static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
2453 unsigned short regs[16], irq;
2454 unsigned char scratch, scratch2;
2456 brd->chip_flag = MOXA_OTHER_UART;
2458 id = mxser_read_register(cap, regs);
2461 brd->info = &mxser_cards[0];
2464 brd->info = &mxser_cards[1];
2466 case CI104J_ASIC_ID:
2467 brd->info = &mxser_cards[2];
2470 brd->info = &mxser_cards[5];
2473 brd->info = &mxser_cards[6];
2476 brd->info = &mxser_cards[7];
2483 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2484 Flag-hack checks if configuration should be read as 2-port here. */
2485 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
2486 irq = regs[9] & 0xF000;
2487 irq = irq | (irq >> 4);
2488 if (irq != (regs[9] & 0xFF00))
2489 return MXSER_ERR_IRQ_CONFLIT;
2490 } else if (brd->info->nports == 4) {
2491 irq = regs[9] & 0xF000;
2492 irq = irq | (irq >> 4);
2493 irq = irq | (irq >> 8);
2495 return MXSER_ERR_IRQ_CONFLIT;
2496 } else if (brd->info->nports == 8) {
2497 irq = regs[9] & 0xF000;
2498 irq = irq | (irq >> 4);
2499 irq = irq | (irq >> 8);
2500 if ((irq != regs[9]) || (irq != regs[10]))
2501 return MXSER_ERR_IRQ_CONFLIT;
2505 return MXSER_ERR_IRQ;
2506 brd->irq = ((int)(irq & 0xF000) >> 12);
2507 for (i = 0; i < 8; i++)
2508 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
2509 if ((regs[12] & 0x80) == 0)
2510 return MXSER_ERR_VECTOR;
2511 brd->vector = (int)regs[11]; /* interrupt vector */
2513 brd->vector_mask = 0x00FF;
2515 brd->vector_mask = 0x000F;
2516 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2517 if (regs[12] & bits) {
2518 brd->ports[i].baud_base = 921600;
2519 brd->ports[i].max_baud = 921600;
2521 brd->ports[i].baud_base = 115200;
2522 brd->ports[i].max_baud = 115200;
2525 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2526 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2527 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2528 outb(scratch2, cap + UART_LCR);
2529 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2530 scratch = inb(cap + UART_IIR);
2533 brd->uart_type = PORT_16550A;
2535 brd->uart_type = PORT_16450;
2536 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2538 return MXSER_ERR_IOADDR;
2539 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2540 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2541 return MXSER_ERR_VECTOR;
2543 return brd->info->nports;
2546 static int __devinit mxser_probe(struct pci_dev *pdev,
2547 const struct pci_device_id *ent)
2549 struct mxser_board *brd;
2551 unsigned long ioaddress;
2552 int retval = -EINVAL;
2554 for (i = 0; i < MXSER_BOARDS; i++)
2555 if (mxser_boards[i].info == NULL)
2558 if (i >= MXSER_BOARDS) {
2559 printk(KERN_ERR "Too many Smartio/Industio family boards found "
2560 "(maximum %d), board not configured\n", MXSER_BOARDS);
2564 brd = &mxser_boards[i];
2565 brd->idx = i * MXSER_PORTS_PER_BOARD;
2566 printk(KERN_INFO "Found MOXA %s board (BusNo=%d, DevNo=%d)\n",
2567 mxser_cards[ent->driver_data].name,
2568 pdev->bus->number, PCI_SLOT(pdev->devfn));
2570 retval = pci_enable_device(pdev);
2572 printk(KERN_ERR "Moxa SmartI/O PCI enable fail !\n");
2577 ioaddress = pci_resource_start(pdev, 2);
2578 retval = pci_request_region(pdev, 2, "mxser(IO)");
2582 brd->info = &mxser_cards[ent->driver_data];
2583 for (i = 0; i < brd->info->nports; i++)
2584 brd->ports[i].ioaddr = ioaddress + 8 * i;
2587 ioaddress = pci_resource_start(pdev, 3);
2588 retval = pci_request_region(pdev, 3, "mxser(vector)");
2591 brd->vector = ioaddress;
2594 brd->irq = pdev->irq;
2596 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2597 brd->uart_type = PORT_16550A;
2598 brd->vector_mask = 0;
2600 for (i = 0; i < brd->info->nports; i++) {
2601 for (j = 0; j < UART_INFO_NUM; j++) {
2602 if (Gpci_uart_info[j].type == brd->chip_flag) {
2603 brd->ports[i].max_baud =
2604 Gpci_uart_info[j].max_baud;
2606 /* exception....CP-102 */
2607 if (brd->info->flags & MXSER_HIGHBAUD)
2608 brd->ports[i].max_baud = 921600;
2614 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2615 for (i = 0; i < brd->info->nports; i++) {
2617 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2619 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
2621 outb(0, ioaddress + 4); /* default set to RS232 mode */
2622 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
2625 for (i = 0; i < brd->info->nports; i++) {
2626 brd->vector_mask |= (1 << i);
2627 brd->ports[i].baud_base = 921600;
2630 /* mxser_initbrd will hook ISR. */
2631 if (mxser_initbrd(brd, pdev) < 0)
2634 for (i = 0; i < brd->info->nports; i++)
2635 tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
2637 pci_set_drvdata(pdev, brd);
2641 pci_release_region(pdev, 3);
2643 pci_release_region(pdev, 2);
2649 static void __devexit mxser_remove(struct pci_dev *pdev)
2651 struct mxser_board *brd = pci_get_drvdata(pdev);
2654 for (i = 0; i < brd->info->nports; i++)
2655 tty_unregister_device(mxvar_sdriver, brd->idx + i);
2657 mxser_release_res(brd, pdev, 1);
2660 static struct pci_driver mxser_driver = {
2662 .id_table = mxser_pcibrds,
2663 .probe = mxser_probe,
2664 .remove = __devexit_p(mxser_remove)
2667 static int __init mxser_module_init(void)
2669 struct mxser_board *brd;
2671 unsigned int i, m, isaloop;
2674 pr_debug("Loading module mxser ...\n");
2676 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2679 spin_lock_init(&gm_lock);
2681 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2684 /* Initialize the tty_driver structure */
2685 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2686 mxvar_sdriver->name = "ttyMI";
2687 mxvar_sdriver->major = ttymajor;
2688 mxvar_sdriver->minor_start = 0;
2689 mxvar_sdriver->num = MXSER_PORTS + 1;
2690 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2691 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2692 mxvar_sdriver->init_termios = tty_std_termios;
2693 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2694 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2695 tty_set_operations(mxvar_sdriver, &mxser_ops);
2697 retval = tty_register_driver(mxvar_sdriver);
2699 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2707 /* Start finding ISA boards here */
2708 for (isaloop = 0; isaloop < 2; isaloop++)
2709 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2711 cap = mxserBoardCAP[b]; /* predefined */
2713 cap = ioaddr[b]; /* module param */
2718 brd = &mxser_boards[m];
2719 retval = mxser_get_ISA_conf(cap, brd);
2722 printk(KERN_INFO "Found MOXA %s board "
2724 brd->info->name, ioaddr[b]);
2727 if (retval == MXSER_ERR_IRQ)
2728 printk(KERN_ERR "Invalid interrupt "
2729 "number, board not "
2731 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2732 printk(KERN_ERR "Invalid interrupt "
2733 "number, board not "
2735 else if (retval == MXSER_ERR_VECTOR)
2736 printk(KERN_ERR "Invalid interrupt "
2737 "vector, board not "
2739 else if (retval == MXSER_ERR_IOADDR)
2740 printk(KERN_ERR "Invalid I/O address, "
2741 "board not configured\n");
2747 /* mxser_initbrd will hook ISR. */
2748 if (mxser_initbrd(brd, NULL) < 0) {
2753 brd->idx = m * MXSER_PORTS_PER_BOARD;
2754 for (i = 0; i < brd->info->nports; i++)
2755 tty_register_device(mxvar_sdriver, brd->idx + i,
2761 retval = pci_register_driver(&mxser_driver);
2763 printk(KERN_ERR "Can't register pci driver\n");
2767 } /* else: we have some ISA cards under control */
2770 pr_debug("Done.\n");
2774 tty_unregister_driver(mxvar_sdriver);
2776 put_tty_driver(mxvar_sdriver);
2780 static void __exit mxser_module_exit(void)
2784 pr_debug("Unloading module mxser ...\n");
2786 pci_unregister_driver(&mxser_driver);
2788 for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
2789 if (mxser_boards[i].info != NULL)
2790 for (j = 0; j < mxser_boards[i].info->nports; j++)
2791 tty_unregister_device(mxvar_sdriver,
2792 mxser_boards[i].idx + j);
2793 tty_unregister_driver(mxvar_sdriver);
2794 put_tty_driver(mxvar_sdriver);
2796 for (i = 0; i < MXSER_BOARDS; i++)
2797 if (mxser_boards[i].info != NULL)
2798 mxser_release_res(&mxser_boards[i], NULL, 1);
2800 pr_debug("Done.\n");
2803 module_init(mxser_module_init);
2804 module_exit(mxser_module_exit);