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;
286 struct pci_dev *pdev; /* temporary (until pci probing) */
289 const struct mxser_cardinfo *info;
290 unsigned long vector;
291 unsigned long vector_mask;
296 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
299 struct mxser_mstatus {
307 static struct mxser_mstatus GMStatus[MXSER_PORTS];
309 static int mxserBoardCAP[MXSER_BOARDS] = {
311 /* 0x180, 0x280, 0x200, 0x320 */
314 static struct mxser_board mxser_boards[MXSER_BOARDS];
315 static struct tty_driver *mxvar_sdriver;
316 static struct tty_struct *mxvar_tty[MXSER_PORTS + 1];
317 static struct termios *mxvar_termios[MXSER_PORTS + 1];
318 static struct termios *mxvar_termios_locked[MXSER_PORTS + 1];
319 static struct mxser_log mxvar_log;
320 static int mxvar_diagflag;
321 static unsigned char mxser_msr[MXSER_PORTS + 1];
322 static struct mxser_mon_ext mon_data_ext;
323 static int mxser_set_baud_method[MXSER_PORTS + 1];
324 static spinlock_t gm_lock;
326 static int CheckIsMoxaMust(int io)
331 outb(0, io + UART_LCR);
332 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
333 oldmcr = inb(io + UART_MCR);
334 outb(0, io + UART_MCR);
335 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
336 if ((hwid = inb(io + UART_MCR)) != 0) {
337 outb(oldmcr, io + UART_MCR);
338 return MOXA_OTHER_UART;
341 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
342 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
343 if (hwid == Gpci_uart_info[i].type)
346 return MOXA_OTHER_UART;
349 static void process_txrx_fifo(struct mxser_port *info)
353 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
354 info->rx_trigger = 1;
355 info->rx_high_water = 1;
356 info->rx_low_water = 1;
357 info->xmit_fifo_size = 1;
359 for (i = 0; i < UART_INFO_NUM; i++)
360 if (info->board->chip_flag == Gpci_uart_info[i].type) {
361 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
362 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
363 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
364 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
369 static void mxser_do_softint(void *private_)
371 struct mxser_port *info = private_;
372 struct tty_struct *tty;
376 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
378 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event))
382 static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
384 unsigned char status = 0;
386 status = inb(baseaddr + UART_MSR);
388 mxser_msr[port] &= 0x0F;
389 mxser_msr[port] |= status;
390 status = mxser_msr[port];
397 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
398 struct mxser_port *port)
400 DECLARE_WAITQUEUE(wait, current);
406 * If non-blocking mode is set, or the port is not enabled,
407 * then make the check up front and then exit.
409 if ((filp->f_flags & O_NONBLOCK) ||
410 test_bit(TTY_IO_ERROR, &tty->flags)) {
411 port->flags |= ASYNC_NORMAL_ACTIVE;
415 if (tty->termios->c_cflag & CLOCAL)
419 * Block waiting for the carrier detect and the line to become
420 * free (i.e., not in use by the callout). While we are in
421 * this loop, port->count is dropped by one, so that
422 * mxser_close() knows when to free things. We restore it upon
423 * exit, either normal or abnormal.
426 add_wait_queue(&port->open_wait, &wait);
428 spin_lock_irqsave(&port->slock, flags);
429 if (!tty_hung_up_p(filp))
431 spin_unlock_irqrestore(&port->slock, flags);
432 port->blocked_open++;
434 spin_lock_irqsave(&port->slock, flags);
435 outb(inb(port->ioaddr + UART_MCR) |
436 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
437 spin_unlock_irqrestore(&port->slock, flags);
438 set_current_state(TASK_INTERRUPTIBLE);
439 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
440 if (port->flags & ASYNC_HUP_NOTIFY)
443 retval = -ERESTARTSYS;
446 if (!(port->flags & ASYNC_CLOSING) &&
448 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
450 if (signal_pending(current)) {
451 retval = -ERESTARTSYS;
456 set_current_state(TASK_RUNNING);
457 remove_wait_queue(&port->open_wait, &wait);
458 if (!tty_hung_up_p(filp))
460 port->blocked_open--;
463 port->flags |= ASYNC_NORMAL_ACTIVE;
467 static int mxser_set_baud(struct mxser_port *info, long newspd)
474 if (!info->tty || !info->tty->termios)
480 if (newspd > info->max_baud)
483 info->realbaud = newspd;
485 quot = (2 * info->baud_base / 269);
487 quot = info->baud_base / newspd;
494 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
495 info->timeout += HZ / 50; /* Add .02 seconds of slop */
498 spin_lock_irqsave(&info->slock, flags);
499 info->MCR |= UART_MCR_DTR;
500 outb(info->MCR, info->ioaddr + UART_MCR);
501 spin_unlock_irqrestore(&info->slock, flags);
503 spin_lock_irqsave(&info->slock, flags);
504 info->MCR &= ~UART_MCR_DTR;
505 outb(info->MCR, info->ioaddr + UART_MCR);
506 spin_unlock_irqrestore(&info->slock, flags);
510 cval = inb(info->ioaddr + UART_LCR);
512 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
514 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
515 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
516 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
523 * This routine is called to set the UART divisor registers to match
524 * the specified baud rate for a serial port.
526 static int mxser_change_speed(struct mxser_port *info,
527 struct termios *old_termios)
529 unsigned cflag, cval, fcr;
531 unsigned char status;
535 if (!info->tty || !info->tty->termios)
537 cflag = info->tty->termios->c_cflag;
541 if (mxser_set_baud_method[info->tty->index] == 0) {
542 baud = tty_get_baud_rate(info->tty);
543 mxser_set_baud(info, baud);
546 /* byte size and parity */
547 switch (cflag & CSIZE) {
562 break; /* too keep GCC shut... */
567 cval |= UART_LCR_PARITY;
568 if (!(cflag & PARODD))
569 cval |= UART_LCR_EPAR;
571 cval |= UART_LCR_SPAR;
573 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
574 if (info->board->chip_flag) {
575 fcr = UART_FCR_ENABLE_FIFO;
576 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
577 SET_MOXA_MUST_FIFO_VALUE(info);
581 fcr = UART_FCR_ENABLE_FIFO;
582 if (info->board->chip_flag) {
583 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
584 SET_MOXA_MUST_FIFO_VALUE(info);
586 switch (info->rx_trigger) {
588 fcr |= UART_FCR_TRIGGER_1;
591 fcr |= UART_FCR_TRIGGER_4;
594 fcr |= UART_FCR_TRIGGER_8;
597 fcr |= UART_FCR_TRIGGER_14;
603 /* CTS flow control flag and modem status interrupts */
604 info->IER &= ~UART_IER_MSI;
605 info->MCR &= ~UART_MCR_AFE;
606 if (cflag & CRTSCTS) {
607 info->flags |= ASYNC_CTS_FLOW;
608 info->IER |= UART_IER_MSI;
609 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
610 info->MCR |= UART_MCR_AFE;
612 status = inb(info->ioaddr + UART_MSR);
613 if (info->tty->hw_stopped) {
614 if (status & UART_MSR_CTS) {
615 info->tty->hw_stopped = 0;
616 if (info->type != PORT_16550A &&
617 !info->board->chip_flag) {
618 outb(info->IER & ~UART_IER_THRI,
621 info->IER |= UART_IER_THRI;
622 outb(info->IER, info->ioaddr +
625 set_bit(MXSER_EVENT_TXLOW, &info->event);
626 schedule_work(&info->tqueue); }
628 if (!(status & UART_MSR_CTS)) {
629 info->tty->hw_stopped = 1;
630 if ((info->type != PORT_16550A) &&
631 (!info->board->chip_flag)) {
632 info->IER &= ~UART_IER_THRI;
633 outb(info->IER, info->ioaddr +
640 info->flags &= ~ASYNC_CTS_FLOW;
642 outb(info->MCR, info->ioaddr + UART_MCR);
643 if (cflag & CLOCAL) {
644 info->flags &= ~ASYNC_CHECK_CD;
646 info->flags |= ASYNC_CHECK_CD;
647 info->IER |= UART_IER_MSI;
649 outb(info->IER, info->ioaddr + UART_IER);
652 * Set up parity check flag
654 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
655 if (I_INPCK(info->tty))
656 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
657 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
658 info->read_status_mask |= UART_LSR_BI;
660 info->ignore_status_mask = 0;
662 if (I_IGNBRK(info->tty)) {
663 info->ignore_status_mask |= UART_LSR_BI;
664 info->read_status_mask |= UART_LSR_BI;
666 * If we're ignore parity and break indicators, ignore
667 * overruns too. (For real raw support).
669 if (I_IGNPAR(info->tty)) {
670 info->ignore_status_mask |=
674 info->read_status_mask |=
680 if (info->board->chip_flag) {
681 spin_lock_irqsave(&info->slock, flags);
682 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
683 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
684 if (I_IXON(info->tty)) {
685 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
687 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
689 if (I_IXOFF(info->tty)) {
690 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
692 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
694 spin_unlock_irqrestore(&info->slock, flags);
698 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
699 outb(cval, info->ioaddr + UART_LCR);
704 static void mxser_check_modem_status(struct mxser_port *port, int status)
706 /* update input line counters */
707 if (status & UART_MSR_TERI)
709 if (status & UART_MSR_DDSR)
711 if (status & UART_MSR_DDCD)
713 if (status & UART_MSR_DCTS)
715 port->mon_data.modem_status = status;
716 wake_up_interruptible(&port->delta_msr_wait);
718 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
719 if (status & UART_MSR_DCD)
720 wake_up_interruptible(&port->open_wait);
721 schedule_work(&port->tqueue);
724 if (port->flags & ASYNC_CTS_FLOW) {
725 if (port->tty->hw_stopped) {
726 if (status & UART_MSR_CTS) {
727 port->tty->hw_stopped = 0;
729 if ((port->type != PORT_16550A) &&
730 (!port->board->chip_flag)) {
731 outb(port->IER & ~UART_IER_THRI,
732 port->ioaddr + UART_IER);
733 port->IER |= UART_IER_THRI;
734 outb(port->IER, port->ioaddr +
737 set_bit(MXSER_EVENT_TXLOW, &port->event);
738 schedule_work(&port->tqueue);
741 if (!(status & UART_MSR_CTS)) {
742 port->tty->hw_stopped = 1;
743 if (port->type != PORT_16550A &&
744 !port->board->chip_flag) {
745 port->IER &= ~UART_IER_THRI;
746 outb(port->IER, port->ioaddr +
754 static int mxser_startup(struct mxser_port *info)
759 page = __get_free_page(GFP_KERNEL);
763 spin_lock_irqsave(&info->slock, flags);
765 if (info->flags & ASYNC_INITIALIZED) {
767 spin_unlock_irqrestore(&info->slock, flags);
771 if (!info->ioaddr || !info->type) {
773 set_bit(TTY_IO_ERROR, &info->tty->flags);
775 spin_unlock_irqrestore(&info->slock, flags);
781 info->xmit_buf = (unsigned char *) page;
784 * Clear the FIFO buffers and disable them
785 * (they will be reenabled in mxser_change_speed())
787 if (info->board->chip_flag)
788 outb((UART_FCR_CLEAR_RCVR |
789 UART_FCR_CLEAR_XMIT |
790 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
792 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
793 info->ioaddr + UART_FCR);
796 * At this point there's no way the LSR could still be 0xFF;
797 * if it is, then bail out, because there's likely no UART
800 if (inb(info->ioaddr + UART_LSR) == 0xff) {
801 spin_unlock_irqrestore(&info->slock, flags);
802 if (capable(CAP_SYS_ADMIN)) {
804 set_bit(TTY_IO_ERROR, &info->tty->flags);
811 * Clear the interrupt registers.
813 (void) inb(info->ioaddr + UART_LSR);
814 (void) inb(info->ioaddr + UART_RX);
815 (void) inb(info->ioaddr + UART_IIR);
816 (void) inb(info->ioaddr + UART_MSR);
819 * Now, initialize the UART
821 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
822 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
823 outb(info->MCR, info->ioaddr + UART_MCR);
826 * Finally, enable interrupts
828 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
830 if (info->board->chip_flag)
831 info->IER |= MOXA_MUST_IER_EGDAI;
832 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
835 * And clear the interrupt registers again for luck.
837 (void) inb(info->ioaddr + UART_LSR);
838 (void) inb(info->ioaddr + UART_RX);
839 (void) inb(info->ioaddr + UART_IIR);
840 (void) inb(info->ioaddr + UART_MSR);
843 clear_bit(TTY_IO_ERROR, &info->tty->flags);
844 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
847 * and set the speed of the serial port
849 spin_unlock_irqrestore(&info->slock, flags);
850 mxser_change_speed(info, NULL);
852 info->flags |= ASYNC_INITIALIZED;
857 * This routine will shutdown a serial port; interrupts maybe disabled, and
858 * DTR is dropped if the hangup on close termio flag is on.
860 static void mxser_shutdown(struct mxser_port *info)
864 if (!(info->flags & ASYNC_INITIALIZED))
867 spin_lock_irqsave(&info->slock, flags);
870 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
871 * here so the queue might never be waken up
873 wake_up_interruptible(&info->delta_msr_wait);
876 * Free the IRQ, if necessary
878 if (info->xmit_buf) {
879 free_page((unsigned long) info->xmit_buf);
880 info->xmit_buf = NULL;
884 outb(0x00, info->ioaddr + UART_IER);
886 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
887 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
888 outb(info->MCR, info->ioaddr + UART_MCR);
890 /* clear Rx/Tx FIFO's */
891 if (info->board->chip_flag)
892 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
893 MOXA_MUST_FCR_GDA_MODE_ENABLE,
894 info->ioaddr + UART_FCR);
896 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
897 info->ioaddr + UART_FCR);
899 /* read data port to reset things */
900 (void) inb(info->ioaddr + UART_RX);
903 set_bit(TTY_IO_ERROR, &info->tty->flags);
905 info->flags &= ~ASYNC_INITIALIZED;
907 if (info->board->chip_flag)
908 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
910 spin_unlock_irqrestore(&info->slock, flags);
914 * This routine is called whenever a serial port is opened. It
915 * enables interrupts for a serial port, linking in its async structure into
916 * the IRQ chain. It also performs the serial-specific
917 * initialization for the tty structure.
919 static int mxser_open(struct tty_struct *tty, struct file *filp)
921 struct mxser_port *info;
924 /* initialize driver_data in case something fails */
925 tty->driver_data = NULL;
928 if (line == MXSER_PORTS)
930 if (line < 0 || line > MXSER_PORTS)
932 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
936 tty->driver_data = info;
939 * Start up serial port
942 retval = mxser_startup(info);
946 retval = mxser_block_til_ready(tty, filp, info);
950 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
951 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
952 *tty->termios = info->normal_termios;
954 *tty->termios = info->callout_termios;
955 mxser_change_speed(info, NULL);
958 info->session = process_session(current);
959 info->pgrp = process_group(current);
961 /* unmark here for very high baud rate (ex. 921600 bps) used */
962 tty->low_latency = 1;
967 * This routine is called when the serial port gets closed. First, we
968 * wait for the last remaining data to be sent. Then, we unlink its
969 * async structure from the interrupt chain if necessary, and we free
970 * that IRQ if nothing is left in the chain.
972 static void mxser_close(struct tty_struct *tty, struct file *filp)
974 struct mxser_port *info = tty->driver_data;
976 unsigned long timeout;
979 if (tty->index == MXSER_PORTS)
984 spin_lock_irqsave(&info->slock, flags);
986 if (tty_hung_up_p(filp)) {
987 spin_unlock_irqrestore(&info->slock, flags);
990 if ((tty->count == 1) && (info->count != 1)) {
992 * Uh, oh. tty->count is 1, which means that the tty
993 * structure will be freed. Info->count should always
994 * be one in these conditions. If it's greater than
995 * one, we've got real problems, since it means the
996 * serial port won't be shutdown.
998 printk(KERN_ERR "mxser_close: bad serial port count; "
999 "tty->count is 1, info->count is %d\n", info->count);
1002 if (--info->count < 0) {
1003 printk(KERN_ERR "mxser_close: bad serial port count for "
1004 "ttys%d: %d\n", tty->index, info->count);
1008 spin_unlock_irqrestore(&info->slock, flags);
1011 info->flags |= ASYNC_CLOSING;
1012 spin_unlock_irqrestore(&info->slock, flags);
1014 * Save the termios structure, since this port may have
1015 * separate termios for callout and dialin.
1017 if (info->flags & ASYNC_NORMAL_ACTIVE)
1018 info->normal_termios = *tty->termios;
1020 * Now we wait for the transmit buffer to clear; and we notify
1021 * the line discipline to only process XON/XOFF characters.
1024 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1025 tty_wait_until_sent(tty, info->closing_wait);
1027 * At this point we stop accepting input. To do this, we
1028 * disable the receive line status interrupts, and tell the
1029 * interrupt driver to stop checking the data ready bit in the
1030 * line status register.
1032 info->IER &= ~UART_IER_RLSI;
1033 if (info->board->chip_flag)
1034 info->IER &= ~MOXA_MUST_RECV_ISR;
1036 if (info->flags & ASYNC_INITIALIZED) {
1037 outb(info->IER, info->ioaddr + UART_IER);
1039 * Before we drop DTR, make sure the UART transmitter
1040 * has completely drained; this is especially
1041 * important if there is a transmit FIFO!
1043 timeout = jiffies + HZ;
1044 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
1045 schedule_timeout_interruptible(5);
1046 if (time_after(jiffies, timeout))
1050 mxser_shutdown(info);
1052 if (tty->driver->flush_buffer)
1053 tty->driver->flush_buffer(tty);
1055 tty_ldisc_flush(tty);
1060 if (info->blocked_open) {
1061 if (info->close_delay)
1062 schedule_timeout_interruptible(info->close_delay);
1063 wake_up_interruptible(&info->open_wait);
1066 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1067 wake_up_interruptible(&info->close_wait);
1071 static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1074 struct mxser_port *info = tty->driver_data;
1075 unsigned long flags;
1077 if (!info->xmit_buf)
1081 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1082 SERIAL_XMIT_SIZE - info->xmit_head));
1086 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1087 spin_lock_irqsave(&info->slock, flags);
1088 info->xmit_head = (info->xmit_head + c) &
1089 (SERIAL_XMIT_SIZE - 1);
1090 info->xmit_cnt += c;
1091 spin_unlock_irqrestore(&info->slock, flags);
1098 if (info->xmit_cnt && !tty->stopped) {
1099 if (!tty->hw_stopped ||
1100 (info->type == PORT_16550A) ||
1101 (info->board->chip_flag)) {
1102 spin_lock_irqsave(&info->slock, flags);
1103 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1105 info->IER |= UART_IER_THRI;
1106 outb(info->IER, info->ioaddr + UART_IER);
1107 spin_unlock_irqrestore(&info->slock, flags);
1113 static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1115 struct mxser_port *info = tty->driver_data;
1116 unsigned long flags;
1118 if (!info->xmit_buf)
1121 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1124 spin_lock_irqsave(&info->slock, flags);
1125 info->xmit_buf[info->xmit_head++] = ch;
1126 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1128 spin_unlock_irqrestore(&info->slock, flags);
1129 if (!tty->stopped) {
1130 if (!tty->hw_stopped ||
1131 (info->type == PORT_16550A) ||
1132 info->board->chip_flag) {
1133 spin_lock_irqsave(&info->slock, flags);
1134 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1135 info->IER |= UART_IER_THRI;
1136 outb(info->IER, info->ioaddr + UART_IER);
1137 spin_unlock_irqrestore(&info->slock, flags);
1143 static void mxser_flush_chars(struct tty_struct *tty)
1145 struct mxser_port *info = tty->driver_data;
1146 unsigned long flags;
1148 if (info->xmit_cnt <= 0 ||
1152 (info->type != PORT_16550A) &&
1153 (!info->board->chip_flag)
1157 spin_lock_irqsave(&info->slock, flags);
1159 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1160 info->IER |= UART_IER_THRI;
1161 outb(info->IER, info->ioaddr + UART_IER);
1163 spin_unlock_irqrestore(&info->slock, flags);
1166 static int mxser_write_room(struct tty_struct *tty)
1168 struct mxser_port *info = tty->driver_data;
1171 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1177 static int mxser_chars_in_buffer(struct tty_struct *tty)
1179 struct mxser_port *info = tty->driver_data;
1180 return info->xmit_cnt;
1183 static void mxser_flush_buffer(struct tty_struct *tty)
1185 struct mxser_port *info = tty->driver_data;
1187 unsigned long flags;
1190 spin_lock_irqsave(&info->slock, flags);
1191 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1193 fcr = inb(info->ioaddr + UART_FCR);
1194 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1195 info->ioaddr + UART_FCR);
1196 outb(fcr, info->ioaddr + UART_FCR);
1198 spin_unlock_irqrestore(&info->slock, flags);
1204 * ------------------------------------------------------------
1205 * friends of mxser_ioctl()
1206 * ------------------------------------------------------------
1208 static int mxser_get_serial_info(struct mxser_port *info,
1209 struct serial_struct __user *retinfo)
1211 struct serial_struct tmp;
1215 memset(&tmp, 0, sizeof(tmp));
1216 tmp.type = info->type;
1217 tmp.line = info->tty->index;
1218 tmp.port = info->ioaddr;
1219 tmp.irq = info->board->irq;
1220 tmp.flags = info->flags;
1221 tmp.baud_base = info->baud_base;
1222 tmp.close_delay = info->close_delay;
1223 tmp.closing_wait = info->closing_wait;
1224 tmp.custom_divisor = info->custom_divisor;
1226 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1231 static int mxser_set_serial_info(struct mxser_port *info,
1232 struct serial_struct __user *new_info)
1234 struct serial_struct new_serial;
1238 if (!new_info || !info->ioaddr)
1240 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1243 if ((new_serial.irq != info->board->irq) ||
1244 (new_serial.port != info->ioaddr) ||
1245 (new_serial.custom_divisor != info->custom_divisor) ||
1246 (new_serial.baud_base != info->baud_base))
1249 flags = info->flags & ASYNC_SPD_MASK;
1251 if (!capable(CAP_SYS_ADMIN)) {
1252 if ((new_serial.baud_base != info->baud_base) ||
1253 (new_serial.close_delay != info->close_delay) ||
1254 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
1256 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1257 (new_serial.flags & ASYNC_USR_MASK));
1260 * OK, past this point, all the error checking has been done.
1261 * At this point, we start making changes.....
1263 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1264 (new_serial.flags & ASYNC_FLAGS));
1265 info->close_delay = new_serial.close_delay * HZ / 100;
1266 info->closing_wait = new_serial.closing_wait * HZ / 100;
1267 info->tty->low_latency =
1268 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1269 info->tty->low_latency = 0;
1272 info->type = new_serial.type;
1274 process_txrx_fifo(info);
1276 if (info->flags & ASYNC_INITIALIZED) {
1277 if (flags != (info->flags & ASYNC_SPD_MASK))
1278 mxser_change_speed(info, NULL);
1280 retval = mxser_startup(info);
1286 * mxser_get_lsr_info - get line status register info
1288 * Purpose: Let user call ioctl() to get info when the UART physically
1289 * is emptied. On bus types like RS485, the transmitter must
1290 * release the bus after transmitting. This must be done when
1291 * the transmit shift register is empty, not be done when the
1292 * transmit holding register is empty. This functionality
1293 * allows an RS485 driver to be written in user space.
1295 static int mxser_get_lsr_info(struct mxser_port *info,
1296 unsigned int __user *value)
1298 unsigned char status;
1299 unsigned int result;
1300 unsigned long flags;
1302 spin_lock_irqsave(&info->slock, flags);
1303 status = inb(info->ioaddr + UART_LSR);
1304 spin_unlock_irqrestore(&info->slock, flags);
1305 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1306 return put_user(result, value);
1310 * This routine sends a break character out the serial port.
1312 static void mxser_send_break(struct mxser_port *info, int duration)
1314 unsigned long flags;
1318 set_current_state(TASK_INTERRUPTIBLE);
1319 spin_lock_irqsave(&info->slock, flags);
1320 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1321 info->ioaddr + UART_LCR);
1322 spin_unlock_irqrestore(&info->slock, flags);
1323 schedule_timeout(duration);
1324 spin_lock_irqsave(&info->slock, flags);
1325 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1326 info->ioaddr + UART_LCR);
1327 spin_unlock_irqrestore(&info->slock, flags);
1330 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
1332 struct mxser_port *info = tty->driver_data;
1333 unsigned char control, status;
1334 unsigned long flags;
1337 if (tty->index == MXSER_PORTS)
1338 return -ENOIOCTLCMD;
1339 if (test_bit(TTY_IO_ERROR, &tty->flags))
1342 control = info->MCR;
1344 spin_lock_irqsave(&info->slock, flags);
1345 status = inb(info->ioaddr + UART_MSR);
1346 if (status & UART_MSR_ANY_DELTA)
1347 mxser_check_modem_status(info, status);
1348 spin_unlock_irqrestore(&info->slock, flags);
1349 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1350 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1351 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1352 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1353 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1354 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1357 static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1358 unsigned int set, unsigned int clear)
1360 struct mxser_port *info = tty->driver_data;
1361 unsigned long flags;
1364 if (tty->index == MXSER_PORTS)
1365 return -ENOIOCTLCMD;
1366 if (test_bit(TTY_IO_ERROR, &tty->flags))
1369 spin_lock_irqsave(&info->slock, flags);
1371 if (set & TIOCM_RTS)
1372 info->MCR |= UART_MCR_RTS;
1373 if (set & TIOCM_DTR)
1374 info->MCR |= UART_MCR_DTR;
1376 if (clear & TIOCM_RTS)
1377 info->MCR &= ~UART_MCR_RTS;
1378 if (clear & TIOCM_DTR)
1379 info->MCR &= ~UART_MCR_DTR;
1381 outb(info->MCR, info->ioaddr + UART_MCR);
1382 spin_unlock_irqrestore(&info->slock, flags);
1386 static int mxser_program_mode(int port)
1390 spin_lock(&gm_lock);
1398 spin_unlock(&gm_lock);
1400 id = inb(port + 1) & 0x1F;
1401 if ((id != C168_ASIC_ID) &&
1402 (id != C104_ASIC_ID) &&
1403 (id != C102_ASIC_ID) &&
1404 (id != CI132_ASIC_ID) &&
1405 (id != CI134_ASIC_ID) &&
1406 (id != CI104J_ASIC_ID))
1408 for (i = 0, j = 0; i < 4; i++) {
1412 } else if ((j == 1) && (n == 1)) {
1423 static void mxser_normal_mode(int port)
1427 outb(0xA5, port + 1);
1428 outb(0x80, port + 3);
1429 outb(12, port + 0); /* 9600 bps */
1431 outb(0x03, port + 3); /* 8 data bits */
1432 outb(0x13, port + 4); /* loop back mode */
1433 for (i = 0; i < 16; i++) {
1435 if ((n & 0x61) == 0x60)
1440 outb(0x00, port + 4);
1443 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1444 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1445 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1446 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1447 #define EN_CCMD 0x000 /* Chip's command register */
1448 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1449 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1450 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1451 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1452 #define EN0_DCFG 0x00E /* Data configuration reg WR */
1453 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1454 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1455 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1456 static int mxser_read_register(int port, unsigned short *regs)
1458 int i, k, value, id;
1461 id = mxser_program_mode(port);
1464 for (i = 0; i < 14; i++) {
1465 k = (i & 0x3F) | 0x180;
1466 for (j = 0x100; j > 0; j >>= 1) {
1467 outb(CHIP_CS, port);
1469 outb(CHIP_CS | CHIP_DO, port);
1470 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1472 outb(CHIP_CS, port);
1473 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1478 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1479 outb(CHIP_CS, port);
1480 outb(CHIP_CS | CHIP_SK, port);
1481 if (inb(port) & CHIP_DI)
1487 mxser_normal_mode(port);
1491 static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1493 struct mxser_port *port;
1499 /* if (copy_to_user(argp, mxsercfg,
1500 sizeof(struct mxser_hwconf) * 4))
1504 case MOXA_GET_MAJOR:
1505 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1509 case MOXA_GET_CUMAJOR:
1510 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1514 case MOXA_CHKPORTENABLE:
1517 for (i = 0; i < MXSER_BOARDS; i++)
1518 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1519 if (mxser_boards[i].ports[j].ioaddr)
1522 return put_user(result, (unsigned long __user *)argp);
1523 case MOXA_GETDATACOUNT:
1524 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1527 case MOXA_GETMSTATUS:
1528 for (i = 0; i < MXSER_BOARDS; i++)
1529 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1530 port = &mxser_boards[i].ports[j];
1533 if (!port->ioaddr) {
1534 GMStatus[i].dcd = 0;
1535 GMStatus[i].dsr = 0;
1536 GMStatus[i].cts = 0;
1540 if (!port->tty || !port->tty->termios)
1542 port->normal_termios.c_cflag;
1545 port->tty->termios->c_cflag;
1547 status = inb(port->ioaddr + UART_MSR);
1548 if (status & 0x80 /*UART_MSR_DCD */ )
1549 GMStatus[i].dcd = 1;
1551 GMStatus[i].dcd = 0;
1553 if (status & 0x20 /*UART_MSR_DSR */ )
1554 GMStatus[i].dsr = 1;
1556 GMStatus[i].dsr = 0;
1559 if (status & 0x10 /*UART_MSR_CTS */ )
1560 GMStatus[i].cts = 1;
1562 GMStatus[i].cts = 0;
1564 if (copy_to_user(argp, GMStatus,
1565 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1568 case MOXA_ASPP_MON_EXT: {
1569 int status, p, shiftbit;
1570 unsigned long opmode;
1571 unsigned cflag, iflag;
1573 for (i = 0; i < MXSER_BOARDS; i++)
1574 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1575 port = &mxser_boards[i].ports[j];
1579 status = mxser_get_msr(port->ioaddr, 0, i);
1581 if (status & UART_MSR_TERI)
1583 if (status & UART_MSR_DDSR)
1585 if (status & UART_MSR_DDCD)
1587 if (status & UART_MSR_DCTS)
1590 port->mon_data.modem_status = status;
1591 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1592 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1593 mon_data_ext.up_rxcnt[i] =
1594 port->mon_data.up_rxcnt;
1595 mon_data_ext.up_txcnt[i] =
1596 port->mon_data.up_txcnt;
1597 mon_data_ext.modem_status[i] =
1598 port->mon_data.modem_status;
1599 mon_data_ext.baudrate[i] = port->realbaud;
1601 if (!port->tty || !port->tty->termios) {
1602 cflag = port->normal_termios.c_cflag;
1603 iflag = port->normal_termios.c_iflag;
1605 cflag = port->tty->termios->c_cflag;
1606 iflag = port->tty->termios->c_iflag;
1609 mon_data_ext.databits[i] = cflag & CSIZE;
1611 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1613 mon_data_ext.parity[i] =
1614 cflag & (PARENB | PARODD | CMSPAR);
1616 mon_data_ext.flowctrl[i] = 0x00;
1618 if (cflag & CRTSCTS)
1619 mon_data_ext.flowctrl[i] |= 0x03;
1621 if (iflag & (IXON | IXOFF))
1622 mon_data_ext.flowctrl[i] |= 0x0C;
1624 if (port->type == PORT_16550A)
1625 mon_data_ext.fifo[i] = 1;
1627 mon_data_ext.fifo[i] = 0;
1631 opmode = inb(port->opmode_ioaddr) >> shiftbit;
1632 opmode &= OP_MODE_MASK;
1634 mon_data_ext.iftype[i] = opmode;
1637 if (copy_to_user(argp, &mon_data_ext,
1638 sizeof(mon_data_ext)))
1644 return -ENOIOCTLCMD;
1649 static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1650 unsigned int cmd, unsigned long arg)
1652 struct mxser_port *info = tty->driver_data;
1653 struct async_icount cprev, cnow; /* kernel counter temps */
1654 struct serial_icounter_struct __user *p_cuser;
1655 unsigned long templ;
1656 unsigned long flags;
1657 void __user *argp = (void __user *)arg;
1660 if (tty->index == MXSER_PORTS)
1661 return mxser_ioctl_special(cmd, argp);
1663 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1665 unsigned long opmode;
1666 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1668 unsigned char val, mask;
1671 if (cmd == MOXA_SET_OP_MODE) {
1672 if (get_user(opmode, (int __user *) argp))
1674 if (opmode != RS232_MODE &&
1675 opmode != RS485_2WIRE_MODE &&
1676 opmode != RS422_MODE &&
1677 opmode != RS485_4WIRE_MODE)
1681 val = inb(info->opmode_ioaddr);
1683 val |= (opmode << shiftbit);
1684 outb(val, info->opmode_ioaddr);
1687 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1688 opmode &= OP_MODE_MASK;
1689 if (copy_to_user(argp, &opmode, sizeof(int)))
1695 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1696 test_bit(TTY_IO_ERROR, &tty->flags))
1700 case TCSBRK: /* SVID version: non-zero arg --> no break */
1701 retval = tty_check_change(tty);
1704 tty_wait_until_sent(tty, 0);
1706 mxser_send_break(info, HZ / 4); /* 1/4 second */
1708 case TCSBRKP: /* support for POSIX tcsendbreak() */
1709 retval = tty_check_change(tty);
1712 tty_wait_until_sent(tty, 0);
1713 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1716 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1718 if (get_user(templ, (unsigned long __user *) argp))
1721 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1724 return mxser_get_serial_info(info, argp);
1726 return mxser_set_serial_info(info, argp);
1727 case TIOCSERGETLSR: /* Get line status register */
1728 return mxser_get_lsr_info(info, argp);
1730 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1731 * - mask passed in arg for lines of interest
1732 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1733 * Caller should use TIOCGICOUNT to see which one it was
1736 DECLARE_WAITQUEUE(wait, current);
1738 spin_lock_irqsave(&info->slock, flags);
1739 cprev = info->icount; /* note the counters on entry */
1740 spin_unlock_irqrestore(&info->slock, flags);
1742 add_wait_queue(&info->delta_msr_wait, &wait);
1744 spin_lock_irqsave(&info->slock, flags);
1745 cnow = info->icount; /* atomic copy */
1746 spin_unlock_irqrestore(&info->slock, flags);
1748 set_current_state(TASK_INTERRUPTIBLE);
1749 if (((arg & TIOCM_RNG) &&
1750 (cnow.rng != cprev.rng)) ||
1751 ((arg & TIOCM_DSR) &&
1752 (cnow.dsr != cprev.dsr)) ||
1753 ((arg & TIOCM_CD) &&
1754 (cnow.dcd != cprev.dcd)) ||
1755 ((arg & TIOCM_CTS) &&
1756 (cnow.cts != cprev.cts))) {
1760 /* see if a signal did it */
1761 if (signal_pending(current)) {
1767 current->state = TASK_RUNNING;
1768 remove_wait_queue(&info->delta_msr_wait, &wait);
1773 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1774 * Return: write counters to the user passed counter struct
1775 * NB: both 1->0 and 0->1 transitions are counted except for
1776 * RI where only 0->1 is counted.
1779 spin_lock_irqsave(&info->slock, flags);
1780 cnow = info->icount;
1781 spin_unlock_irqrestore(&info->slock, flags);
1783 if (put_user(cnow.frame, &p_cuser->frame))
1785 if (put_user(cnow.brk, &p_cuser->brk))
1787 if (put_user(cnow.overrun, &p_cuser->overrun))
1789 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1791 if (put_user(cnow.parity, &p_cuser->parity))
1793 if (put_user(cnow.rx, &p_cuser->rx))
1795 if (put_user(cnow.tx, &p_cuser->tx))
1797 put_user(cnow.cts, &p_cuser->cts);
1798 put_user(cnow.dsr, &p_cuser->dsr);
1799 put_user(cnow.rng, &p_cuser->rng);
1800 put_user(cnow.dcd, &p_cuser->dcd);
1802 case MOXA_HighSpeedOn:
1803 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1804 case MOXA_SDS_RSTICOUNTER:
1805 info->mon_data.rxcnt = 0;
1806 info->mon_data.txcnt = 0;
1808 case MOXA_ASPP_SETBAUD:{
1810 if (get_user(baud, (long __user *)argp))
1812 mxser_set_baud(info, baud);
1815 case MOXA_ASPP_GETBAUD:
1816 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1821 case MOXA_ASPP_OQUEUE:{
1824 len = mxser_chars_in_buffer(tty);
1826 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1828 len += (lsr ? 0 : 1);
1830 if (copy_to_user(argp, &len, sizeof(int)))
1835 case MOXA_ASPP_MON: {
1838 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1839 mxser_check_modem_status(info, status);
1841 mcr = inb(info->ioaddr + UART_MCR);
1842 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1843 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1845 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1847 if (mcr & MOXA_MUST_MCR_TX_XON)
1848 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1850 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1852 if (info->tty->hw_stopped)
1853 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1855 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1857 if (copy_to_user(argp, &info->mon_data,
1858 sizeof(struct mxser_mon)))
1863 case MOXA_ASPP_LSTATUS: {
1864 if (copy_to_user(argp, &info->err_shadow,
1865 sizeof(unsigned char)))
1868 info->err_shadow = 0;
1871 case MOXA_SET_BAUD_METHOD: {
1874 if (get_user(method, (int __user *)argp))
1876 mxser_set_baud_method[tty->index] = method;
1877 if (copy_to_user(argp, &method, sizeof(int)))
1883 return -ENOIOCTLCMD;
1888 static void mxser_stoprx(struct tty_struct *tty)
1890 struct mxser_port *info = tty->driver_data;
1892 info->ldisc_stop_rx = 1;
1894 if (info->board->chip_flag) {
1895 info->IER &= ~MOXA_MUST_RECV_ISR;
1896 outb(info->IER, info->ioaddr + UART_IER);
1898 info->x_char = STOP_CHAR(tty);
1899 outb(0, info->ioaddr + UART_IER);
1900 info->IER |= UART_IER_THRI;
1901 outb(info->IER, info->ioaddr + UART_IER);
1905 if (info->tty->termios->c_cflag & CRTSCTS) {
1906 info->MCR &= ~UART_MCR_RTS;
1907 outb(info->MCR, info->ioaddr + UART_MCR);
1912 * This routine is called by the upper-layer tty layer to signal that
1913 * incoming characters should be throttled.
1915 static void mxser_throttle(struct tty_struct *tty)
1920 static void mxser_unthrottle(struct tty_struct *tty)
1922 struct mxser_port *info = tty->driver_data;
1925 info->ldisc_stop_rx = 0;
1930 if (info->board->chip_flag) {
1931 info->IER |= MOXA_MUST_RECV_ISR;
1932 outb(info->IER, info->ioaddr + UART_IER);
1934 info->x_char = START_CHAR(tty);
1935 outb(0, info->ioaddr + UART_IER);
1936 info->IER |= UART_IER_THRI;
1937 outb(info->IER, info->ioaddr + UART_IER);
1942 if (info->tty->termios->c_cflag & CRTSCTS) {
1943 info->MCR |= UART_MCR_RTS;
1944 outb(info->MCR, info->ioaddr + UART_MCR);
1949 * mxser_stop() and mxser_start()
1951 * This routines are called before setting or resetting tty->stopped.
1952 * They enable or disable transmitter interrupts, as necessary.
1954 static void mxser_stop(struct tty_struct *tty)
1956 struct mxser_port *info = tty->driver_data;
1957 unsigned long flags;
1959 spin_lock_irqsave(&info->slock, flags);
1960 if (info->IER & UART_IER_THRI) {
1961 info->IER &= ~UART_IER_THRI;
1962 outb(info->IER, info->ioaddr + UART_IER);
1964 spin_unlock_irqrestore(&info->slock, flags);
1967 static void mxser_start(struct tty_struct *tty)
1969 struct mxser_port *info = tty->driver_data;
1970 unsigned long flags;
1972 spin_lock_irqsave(&info->slock, flags);
1973 if (info->xmit_cnt && info->xmit_buf) {
1974 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1975 info->IER |= UART_IER_THRI;
1976 outb(info->IER, info->ioaddr + UART_IER);
1978 spin_unlock_irqrestore(&info->slock, flags);
1981 static void mxser_set_termios(struct tty_struct *tty, struct termios *old_termios)
1983 struct mxser_port *info = tty->driver_data;
1984 unsigned long flags;
1986 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1987 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
1989 mxser_change_speed(info, old_termios);
1991 if ((old_termios->c_cflag & CRTSCTS) &&
1992 !(tty->termios->c_cflag & CRTSCTS)) {
1993 tty->hw_stopped = 0;
1998 /* Handle sw stopped */
1999 if ((old_termios->c_iflag & IXON) &&
2000 !(tty->termios->c_iflag & IXON)) {
2003 if (info->board->chip_flag) {
2004 spin_lock_irqsave(&info->slock, flags);
2005 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2006 spin_unlock_irqrestore(&info->slock, flags);
2014 * mxser_wait_until_sent() --- wait until the transmitter is empty
2016 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2018 struct mxser_port *info = tty->driver_data;
2019 unsigned long orig_jiffies, char_time;
2022 if (info->type == PORT_UNKNOWN)
2025 if (info->xmit_fifo_size == 0)
2026 return; /* Just in case.... */
2028 orig_jiffies = jiffies;
2030 * Set the check interval to be 1/5 of the estimated time to
2031 * send a single character, and make it at least 1. The check
2032 * interval should also be less than the timeout.
2034 * Note: we have to use pretty tight timings here to satisfy
2037 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2038 char_time = char_time / 5;
2041 if (timeout && timeout < char_time)
2042 char_time = timeout;
2044 * If the transmitter hasn't cleared in twice the approximate
2045 * amount of time to send the entire FIFO, it probably won't
2046 * ever clear. This assumes the UART isn't doing flow
2047 * control, which is currently the case. Hence, if it ever
2048 * takes longer than info->timeout, this is probably due to a
2049 * UART bug of some kind. So, we clamp the timeout parameter at
2052 if (!timeout || timeout > 2 * info->timeout)
2053 timeout = 2 * info->timeout;
2054 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2055 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2056 timeout, char_time);
2057 printk("jiff=%lu...", jiffies);
2059 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
2060 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2061 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2063 schedule_timeout_interruptible(char_time);
2064 if (signal_pending(current))
2066 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2069 set_current_state(TASK_RUNNING);
2071 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2072 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2077 * This routine is called by tty_hangup() when a hangup is signaled.
2079 void mxser_hangup(struct tty_struct *tty)
2081 struct mxser_port *info = tty->driver_data;
2083 mxser_flush_buffer(tty);
2084 mxser_shutdown(info);
2087 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2089 wake_up_interruptible(&info->open_wait);
2093 * mxser_rs_break() --- routine which turns the break handling on or off
2095 static void mxser_rs_break(struct tty_struct *tty, int break_state)
2097 struct mxser_port *info = tty->driver_data;
2098 unsigned long flags;
2100 spin_lock_irqsave(&info->slock, flags);
2101 if (break_state == -1)
2102 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2103 info->ioaddr + UART_LCR);
2105 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2106 info->ioaddr + UART_LCR);
2107 spin_unlock_irqrestore(&info->slock, flags);
2110 static void mxser_receive_chars(struct mxser_port *port, int *status)
2112 struct tty_struct *tty = port->tty;
2113 unsigned char ch, gdl;
2118 unsigned long flags;
2120 spin_lock_irqsave(&port->slock, flags);
2122 recv_room = tty->receive_room;
2123 if ((recv_room == 0) && (!port->ldisc_stop_rx))
2126 if (port->board->chip_flag != MOXA_OTHER_UART) {
2128 if (*status & UART_LSR_SPECIAL)
2130 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
2131 (*status & MOXA_MUST_LSR_RERR))
2133 if (*status & MOXA_MUST_LSR_RERR)
2136 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
2138 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
2139 gdl &= MOXA_MUST_GDL_MASK;
2140 if (gdl >= recv_room) {
2141 if (!port->ldisc_stop_rx)
2145 ch = inb(port->ioaddr + UART_RX);
2146 tty_insert_flip_char(tty, ch, 0);
2157 ch = inb(port->ioaddr + UART_RX);
2158 if (port->board->chip_flag && (*status & UART_LSR_OE))
2159 outb(0x23, port->ioaddr + UART_FCR);
2160 *status &= port->read_status_mask;
2161 if (*status & port->ignore_status_mask) {
2162 if (++ignored > 100)
2166 if (*status & UART_LSR_SPECIAL) {
2167 if (*status & UART_LSR_BI) {
2171 if (port->flags & ASYNC_SAK)
2173 } else if (*status & UART_LSR_PE) {
2175 port->icount.parity++;
2176 } else if (*status & UART_LSR_FE) {
2178 port->icount.frame++;
2179 } else if (*status & UART_LSR_OE) {
2181 port->icount.overrun++;
2184 tty_insert_flip_char(tty, ch, flag);
2186 if (cnt >= recv_room) {
2187 if (!port->ldisc_stop_rx)
2194 if (port->board->chip_flag)
2197 *status = inb(port->ioaddr + UART_LSR);
2198 } while (*status & UART_LSR_DR);
2201 mxvar_log.rxcnt[port->tty->index] += cnt;
2202 port->mon_data.rxcnt += cnt;
2203 port->mon_data.up_rxcnt += cnt;
2204 spin_unlock_irqrestore(&port->slock, flags);
2206 tty_flip_buffer_push(tty);
2209 static void mxser_transmit_chars(struct mxser_port *port)
2212 unsigned long flags;
2214 spin_lock_irqsave(&port->slock, flags);
2217 outb(port->x_char, port->ioaddr + UART_TX);
2219 mxvar_log.txcnt[port->tty->index]++;
2220 port->mon_data.txcnt++;
2221 port->mon_data.up_txcnt++;
2226 if (port->xmit_buf == 0)
2229 if ((port->xmit_cnt <= 0) || port->tty->stopped ||
2230 (port->tty->hw_stopped &&
2231 (port->type != PORT_16550A) &&
2232 (!port->board->chip_flag))) {
2233 port->IER &= ~UART_IER_THRI;
2234 outb(port->IER, port->ioaddr + UART_IER);
2238 cnt = port->xmit_cnt;
2239 count = port->xmit_fifo_size;
2241 outb(port->xmit_buf[port->xmit_tail++],
2242 port->ioaddr + UART_TX);
2243 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2244 if (--port->xmit_cnt <= 0)
2246 } while (--count > 0);
2247 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
2249 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2250 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2251 port->icount.tx += (cnt - port->xmit_cnt);
2253 if (port->xmit_cnt < WAKEUP_CHARS) {
2254 set_bit(MXSER_EVENT_TXLOW, &port->event);
2255 schedule_work(&port->tqueue);
2257 if (port->xmit_cnt <= 0) {
2258 port->IER &= ~UART_IER_THRI;
2259 outb(port->IER, port->ioaddr + UART_IER);
2262 spin_unlock_irqrestore(&port->slock, flags);
2266 * This is the serial driver's generic interrupt routine
2268 static irqreturn_t mxser_interrupt(int irq, void *dev_id)
2271 struct mxser_board *brd = NULL;
2272 struct mxser_port *port;
2273 int max, irqbits, bits, msr;
2274 int pass_counter = 0;
2275 unsigned int int_cnt;
2276 int handled = IRQ_NONE;
2278 for (i = 0; i < MXSER_BOARDS; i++)
2279 if (dev_id == &mxser_boards[i]) {
2284 if (i == MXSER_BOARDS)
2288 max = brd->info->nports;
2290 irqbits = inb(brd->vector) & brd->vector_mask;
2291 if (irqbits == brd->vector_mask)
2294 handled = IRQ_HANDLED;
2295 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2296 if (irqbits == brd->vector_mask)
2300 port = &brd->ports[i];
2304 iir = inb(port->ioaddr + UART_IIR);
2305 if (iir & UART_IIR_NO_INT)
2307 iir &= MOXA_MUST_IIR_MASK;
2309 status = inb(port->ioaddr + UART_LSR);
2310 outb(0x27, port->ioaddr + UART_FCR);
2311 inb(port->ioaddr + UART_MSR);
2315 status = inb(port->ioaddr + UART_LSR);
2317 if (status & UART_LSR_PE)
2318 port->err_shadow |= NPPI_NOTIFY_PARITY;
2319 if (status & UART_LSR_FE)
2320 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2321 if (status & UART_LSR_OE)
2323 NPPI_NOTIFY_HW_OVERRUN;
2324 if (status & UART_LSR_BI)
2325 port->err_shadow |= NPPI_NOTIFY_BREAK;
2327 if (port->board->chip_flag) {
2328 if (iir == MOXA_MUST_IIR_GDA ||
2329 iir == MOXA_MUST_IIR_RDA ||
2330 iir == MOXA_MUST_IIR_RTO ||
2331 iir == MOXA_MUST_IIR_LSR)
2332 mxser_receive_chars(port,
2336 status &= port->read_status_mask;
2337 if (status & UART_LSR_DR)
2338 mxser_receive_chars(port,
2341 msr = inb(port->ioaddr + UART_MSR);
2342 if (msr & UART_MSR_ANY_DELTA)
2343 mxser_check_modem_status(port, msr);
2345 if (port->board->chip_flag) {
2346 if (iir == 0x02 && (status &
2348 mxser_transmit_chars(port);
2350 if (status & UART_LSR_THRE)
2351 mxser_transmit_chars(port);
2353 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2355 if (pass_counter++ > MXSER_ISR_PASS_LIMIT)
2356 break; /* Prevent infinite loops */
2363 static const struct tty_operations mxser_ops = {
2365 .close = mxser_close,
2366 .write = mxser_write,
2367 .put_char = mxser_put_char,
2368 .flush_chars = mxser_flush_chars,
2369 .write_room = mxser_write_room,
2370 .chars_in_buffer = mxser_chars_in_buffer,
2371 .flush_buffer = mxser_flush_buffer,
2372 .ioctl = mxser_ioctl,
2373 .throttle = mxser_throttle,
2374 .unthrottle = mxser_unthrottle,
2375 .set_termios = mxser_set_termios,
2377 .start = mxser_start,
2378 .hangup = mxser_hangup,
2379 .break_ctl = mxser_rs_break,
2380 .wait_until_sent = mxser_wait_until_sent,
2381 .tiocmget = mxser_tiocmget,
2382 .tiocmset = mxser_tiocmset,
2386 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2389 static void mxser_release_res(struct mxser_board *brd, unsigned int irq)
2391 struct pci_dev *pdev = brd->pdev;
2394 free_irq(brd->irq, brd);
2395 if (pdev != NULL) { /* PCI */
2396 pci_release_region(pdev, 2);
2397 pci_release_region(pdev, 3);
2400 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2401 release_region(brd->vector, 1);
2405 static int __devinit mxser_initbrd(struct mxser_board *brd)
2407 struct mxser_port *info;
2411 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
2413 for (i = 0; i < brd->info->nports; i++) {
2414 info = &brd->ports[i];
2417 info->ldisc_stop_rx = 0;
2419 /* Enhance mode enabled here */
2420 if (brd->chip_flag != MOXA_OTHER_UART)
2421 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
2423 info->flags = ASYNC_SHARE_IRQ;
2424 info->type = brd->uart_type;
2426 process_txrx_fifo(info);
2428 info->custom_divisor = info->baud_base * 16;
2429 info->close_delay = 5 * HZ / 10;
2430 info->closing_wait = 30 * HZ;
2431 INIT_WORK(&info->tqueue, mxser_do_softint, info);
2432 info->normal_termios = mxvar_sdriver->init_termios;
2433 init_waitqueue_head(&info->open_wait);
2434 init_waitqueue_head(&info->close_wait);
2435 init_waitqueue_head(&info->delta_msr_wait);
2436 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2437 info->err_shadow = 0;
2438 spin_lock_init(&info->slock);
2440 /* before set INT ISR, disable all int */
2441 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2442 info->ioaddr + UART_IER);
2445 * Allocate the IRQ if necessary
2448 retval = request_irq(brd->irq, mxser_interrupt,
2449 (brd->ports[0].flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED :
2450 IRQF_DISABLED, "mxser", brd);
2452 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2453 "conflict with another device.\n",
2454 brd->info->name, brd->irq);
2455 /* We hold resources, we need to release them. */
2456 mxser_release_res(brd, 0);
2462 static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
2465 unsigned short regs[16], irq;
2466 unsigned char scratch, scratch2;
2468 brd->chip_flag = MOXA_OTHER_UART;
2470 id = mxser_read_register(cap, regs);
2473 brd->info = &mxser_cards[0];
2476 brd->info = &mxser_cards[1];
2478 case CI104J_ASIC_ID:
2479 brd->info = &mxser_cards[2];
2482 brd->info = &mxser_cards[5];
2485 brd->info = &mxser_cards[6];
2488 brd->info = &mxser_cards[7];
2495 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2496 Flag-hack checks if configuration should be read as 2-port here. */
2497 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
2498 irq = regs[9] & 0xF000;
2499 irq = irq | (irq >> 4);
2500 if (irq != (regs[9] & 0xFF00))
2501 return MXSER_ERR_IRQ_CONFLIT;
2502 } else if (brd->info->nports == 4) {
2503 irq = regs[9] & 0xF000;
2504 irq = irq | (irq >> 4);
2505 irq = irq | (irq >> 8);
2507 return MXSER_ERR_IRQ_CONFLIT;
2508 } else if (brd->info->nports == 8) {
2509 irq = regs[9] & 0xF000;
2510 irq = irq | (irq >> 4);
2511 irq = irq | (irq >> 8);
2512 if ((irq != regs[9]) || (irq != regs[10]))
2513 return MXSER_ERR_IRQ_CONFLIT;
2517 return MXSER_ERR_IRQ;
2518 brd->irq = ((int)(irq & 0xF000) >> 12);
2519 for (i = 0; i < 8; i++)
2520 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
2521 if ((regs[12] & 0x80) == 0)
2522 return MXSER_ERR_VECTOR;
2523 brd->vector = (int)regs[11]; /* interrupt vector */
2525 brd->vector_mask = 0x00FF;
2527 brd->vector_mask = 0x000F;
2528 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2529 if (regs[12] & bits) {
2530 brd->ports[i].baud_base = 921600;
2531 brd->ports[i].max_baud = 921600;
2533 brd->ports[i].baud_base = 115200;
2534 brd->ports[i].max_baud = 115200;
2537 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2538 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2539 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2540 outb(scratch2, cap + UART_LCR);
2541 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2542 scratch = inb(cap + UART_IIR);
2545 brd->uart_type = PORT_16550A;
2547 brd->uart_type = PORT_16450;
2548 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2550 return MXSER_ERR_IOADDR;
2551 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2552 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2553 return MXSER_ERR_VECTOR;
2555 return brd->info->nports;
2558 static int __init mxser_get_PCI_conf(const struct pci_device_id *ent,
2559 struct mxser_board *brd, struct pci_dev *pdev)
2562 unsigned long ioaddress;
2566 brd->info = &mxser_cards[ent->driver_data];
2567 ioaddress = pci_resource_start(pdev, 2);
2568 retval = pci_request_region(pdev, 2, "mxser(IO)");
2572 for (i = 0; i < brd->info->nports; i++)
2573 brd->ports[i].ioaddr = ioaddress + 8 * i;
2576 ioaddress = pci_resource_start(pdev, 3);
2577 retval = pci_request_region(pdev, 3, "mxser(vector)");
2580 brd->vector = ioaddress;
2583 brd->irq = pdev->irq;
2585 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2586 brd->uart_type = PORT_16550A;
2587 brd->vector_mask = 0;
2589 for (i = 0; i < brd->info->nports; i++) {
2590 for (j = 0; j < UART_INFO_NUM; j++) {
2591 if (Gpci_uart_info[j].type == brd->chip_flag) {
2592 brd->ports[i].max_baud =
2593 Gpci_uart_info[j].max_baud;
2595 /* exception....CP-102 */
2596 if (brd->info->flags & MXSER_HIGHBAUD)
2597 brd->ports[i].max_baud = 921600;
2603 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2604 for (i = 0; i < brd->info->nports; i++) {
2606 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2608 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
2610 outb(0, ioaddress + 4); /* default set to RS232 mode */
2611 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
2614 for (i = 0; i < brd->info->nports; i++) {
2615 brd->vector_mask |= (1 << i);
2616 brd->ports[i].baud_base = 921600;
2620 pci_release_region(pdev, 2);
2625 static int __init mxser_module_init(void)
2627 struct pci_dev *pdev = NULL;
2628 struct mxser_board *brd;
2630 unsigned int i, m, isaloop;
2633 pr_debug("Loading module mxser ...\n");
2635 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2638 spin_lock_init(&gm_lock);
2640 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2643 /* Initialize the tty_driver structure */
2644 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2645 mxvar_sdriver->name = "ttyM";
2646 mxvar_sdriver->major = ttymajor;
2647 mxvar_sdriver->minor_start = 0;
2648 mxvar_sdriver->num = MXSER_PORTS + 1;
2649 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2650 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2651 mxvar_sdriver->init_termios = tty_std_termios;
2652 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2653 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2654 tty_set_operations(mxvar_sdriver, &mxser_ops);
2655 mxvar_sdriver->ttys = mxvar_tty;
2656 mxvar_sdriver->termios = mxvar_termios;
2657 mxvar_sdriver->termios_locked = mxvar_termios_locked;
2659 retval = tty_register_driver(mxvar_sdriver);
2661 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2669 /* Start finding ISA boards here */
2670 for (isaloop = 0; isaloop < 2; isaloop++)
2671 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2673 cap = mxserBoardCAP[b]; /* predefined */
2675 cap = ioaddr[b]; /* module param */
2680 brd = &mxser_boards[m];
2681 retval = mxser_get_ISA_conf(cap, brd);
2684 printk(KERN_INFO "Found MOXA %s board "
2686 brd->info->name, ioaddr[b]);
2689 if (retval == MXSER_ERR_IRQ)
2690 printk(KERN_ERR "Invalid interrupt "
2691 "number, board not "
2693 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2694 printk(KERN_ERR "Invalid interrupt "
2695 "number, board not "
2697 else if (retval == MXSER_ERR_VECTOR)
2698 printk(KERN_ERR "Invalid interrupt "
2699 "vector, board not "
2701 else if (retval == MXSER_ERR_IOADDR)
2702 printk(KERN_ERR "Invalid I/O address, "
2703 "board not configured\n");
2710 /* mxser_initbrd will hook ISR. */
2711 if (mxser_initbrd(brd) < 0)
2714 for (i = 0; i < brd->info->nports; i++)
2715 tty_register_device(mxvar_sdriver,
2716 m * MXSER_PORTS_PER_BOARD + i, NULL);
2721 /* start finding PCI board here */
2722 n = ARRAY_SIZE(mxser_pcibrds) - 1;
2725 pdev = pci_get_device(mxser_pcibrds[b].vendor,
2726 mxser_pcibrds[b].device, pdev);
2731 printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
2732 mxser_cards[mxser_pcibrds[b].driver_data].name,
2733 pdev->bus->number, PCI_SLOT(pdev->devfn));
2734 if (m >= MXSER_BOARDS)
2736 "Too many Smartio/Industio family boards find "
2737 "(maximum %d), board not configured\n",
2740 if (pci_enable_device(pdev)) {
2741 printk(KERN_ERR "Moxa SmartI/O PCI enable "
2745 brd = &mxser_boards[m];
2747 retval = mxser_get_PCI_conf(&mxser_pcibrds[b],
2750 if (retval == MXSER_ERR_IRQ)
2752 "Invalid interrupt number, "
2753 "board not configured\n");
2754 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2756 "Invalid interrupt number, "
2757 "board not configured\n");
2758 else if (retval == MXSER_ERR_VECTOR)
2760 "Invalid interrupt vector, "
2761 "board not configured\n");
2762 else if (retval == MXSER_ERR_IOADDR)
2764 "Invalid I/O address, "
2765 "board not configured\n");
2768 /* mxser_initbrd will hook ISR. */
2769 if (mxser_initbrd(brd) < 0)
2771 for (i = 0; i < brd->info->nports; i++)
2772 tty_register_device(mxvar_sdriver,
2773 m * MXSER_PORTS_PER_BOARD + i,
2777 /* Keep an extra reference if we succeeded. It will
2778 be returned at unload time */
2788 pr_debug("Done.\n");
2792 tty_unregister_driver(mxvar_sdriver);
2794 put_tty_driver(mxvar_sdriver);
2798 static void __exit mxser_module_exit(void)
2802 pr_debug("Unloading module mxser ...\n");
2804 for (i = 0; i < MXSER_PORTS; i++)
2805 tty_unregister_device(mxvar_sdriver, i);
2806 tty_unregister_driver(mxvar_sdriver);
2807 put_tty_driver(mxvar_sdriver);
2809 for (i = 0; i < MXSER_BOARDS; i++)
2810 if (mxser_boards[i].info != NULL)
2811 mxser_release_res(&mxser_boards[i], 1);
2813 pr_debug("Done.\n");
2816 module_init(mxser_module_init);
2817 module_exit(mxser_module_exit);