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 mxser_log mxvar_log;
317 static int mxvar_diagflag;
318 static unsigned char mxser_msr[MXSER_PORTS + 1];
319 static struct mxser_mon_ext mon_data_ext;
320 static int mxser_set_baud_method[MXSER_PORTS + 1];
321 static spinlock_t gm_lock;
323 static int CheckIsMoxaMust(int io)
328 outb(0, io + UART_LCR);
329 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
330 oldmcr = inb(io + UART_MCR);
331 outb(0, io + UART_MCR);
332 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
333 if ((hwid = inb(io + UART_MCR)) != 0) {
334 outb(oldmcr, io + UART_MCR);
335 return MOXA_OTHER_UART;
338 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
339 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
340 if (hwid == Gpci_uart_info[i].type)
343 return MOXA_OTHER_UART;
346 static void process_txrx_fifo(struct mxser_port *info)
350 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
351 info->rx_trigger = 1;
352 info->rx_high_water = 1;
353 info->rx_low_water = 1;
354 info->xmit_fifo_size = 1;
356 for (i = 0; i < UART_INFO_NUM; i++)
357 if (info->board->chip_flag == Gpci_uart_info[i].type) {
358 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
359 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
360 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
361 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
366 static void mxser_do_softint(void *private_)
368 struct mxser_port *info = private_;
369 struct tty_struct *tty;
373 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
375 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event))
379 static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
381 unsigned char status = 0;
383 status = inb(baseaddr + UART_MSR);
385 mxser_msr[port] &= 0x0F;
386 mxser_msr[port] |= status;
387 status = mxser_msr[port];
394 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
395 struct mxser_port *port)
397 DECLARE_WAITQUEUE(wait, current);
403 * If non-blocking mode is set, or the port is not enabled,
404 * then make the check up front and then exit.
406 if ((filp->f_flags & O_NONBLOCK) ||
407 test_bit(TTY_IO_ERROR, &tty->flags)) {
408 port->flags |= ASYNC_NORMAL_ACTIVE;
412 if (tty->termios->c_cflag & CLOCAL)
416 * Block waiting for the carrier detect and the line to become
417 * free (i.e., not in use by the callout). While we are in
418 * this loop, port->count is dropped by one, so that
419 * mxser_close() knows when to free things. We restore it upon
420 * exit, either normal or abnormal.
423 add_wait_queue(&port->open_wait, &wait);
425 spin_lock_irqsave(&port->slock, flags);
426 if (!tty_hung_up_p(filp))
428 spin_unlock_irqrestore(&port->slock, flags);
429 port->blocked_open++;
431 spin_lock_irqsave(&port->slock, flags);
432 outb(inb(port->ioaddr + UART_MCR) |
433 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
434 spin_unlock_irqrestore(&port->slock, flags);
435 set_current_state(TASK_INTERRUPTIBLE);
436 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
437 if (port->flags & ASYNC_HUP_NOTIFY)
440 retval = -ERESTARTSYS;
443 if (!(port->flags & ASYNC_CLOSING) &&
445 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
447 if (signal_pending(current)) {
448 retval = -ERESTARTSYS;
453 set_current_state(TASK_RUNNING);
454 remove_wait_queue(&port->open_wait, &wait);
455 if (!tty_hung_up_p(filp))
457 port->blocked_open--;
460 port->flags |= ASYNC_NORMAL_ACTIVE;
464 static int mxser_set_baud(struct mxser_port *info, long newspd)
471 if (!info->tty || !info->tty->termios)
477 if (newspd > info->max_baud)
480 info->realbaud = newspd;
482 quot = (2 * info->baud_base / 269);
484 quot = info->baud_base / newspd;
491 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
492 info->timeout += HZ / 50; /* Add .02 seconds of slop */
495 spin_lock_irqsave(&info->slock, flags);
496 info->MCR |= UART_MCR_DTR;
497 outb(info->MCR, info->ioaddr + UART_MCR);
498 spin_unlock_irqrestore(&info->slock, flags);
500 spin_lock_irqsave(&info->slock, flags);
501 info->MCR &= ~UART_MCR_DTR;
502 outb(info->MCR, info->ioaddr + UART_MCR);
503 spin_unlock_irqrestore(&info->slock, flags);
507 cval = inb(info->ioaddr + UART_LCR);
509 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
511 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
512 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
513 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
520 * This routine is called to set the UART divisor registers to match
521 * the specified baud rate for a serial port.
523 static int mxser_change_speed(struct mxser_port *info,
524 struct termios *old_termios)
526 unsigned cflag, cval, fcr;
528 unsigned char status;
532 if (!info->tty || !info->tty->termios)
534 cflag = info->tty->termios->c_cflag;
538 if (mxser_set_baud_method[info->tty->index] == 0) {
539 baud = tty_get_baud_rate(info->tty);
540 mxser_set_baud(info, baud);
543 /* byte size and parity */
544 switch (cflag & CSIZE) {
559 break; /* too keep GCC shut... */
564 cval |= UART_LCR_PARITY;
565 if (!(cflag & PARODD))
566 cval |= UART_LCR_EPAR;
568 cval |= UART_LCR_SPAR;
570 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
571 if (info->board->chip_flag) {
572 fcr = UART_FCR_ENABLE_FIFO;
573 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
574 SET_MOXA_MUST_FIFO_VALUE(info);
578 fcr = UART_FCR_ENABLE_FIFO;
579 if (info->board->chip_flag) {
580 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
581 SET_MOXA_MUST_FIFO_VALUE(info);
583 switch (info->rx_trigger) {
585 fcr |= UART_FCR_TRIGGER_1;
588 fcr |= UART_FCR_TRIGGER_4;
591 fcr |= UART_FCR_TRIGGER_8;
594 fcr |= UART_FCR_TRIGGER_14;
600 /* CTS flow control flag and modem status interrupts */
601 info->IER &= ~UART_IER_MSI;
602 info->MCR &= ~UART_MCR_AFE;
603 if (cflag & CRTSCTS) {
604 info->flags |= ASYNC_CTS_FLOW;
605 info->IER |= UART_IER_MSI;
606 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
607 info->MCR |= UART_MCR_AFE;
609 status = inb(info->ioaddr + UART_MSR);
610 if (info->tty->hw_stopped) {
611 if (status & UART_MSR_CTS) {
612 info->tty->hw_stopped = 0;
613 if (info->type != PORT_16550A &&
614 !info->board->chip_flag) {
615 outb(info->IER & ~UART_IER_THRI,
618 info->IER |= UART_IER_THRI;
619 outb(info->IER, info->ioaddr +
622 set_bit(MXSER_EVENT_TXLOW, &info->event);
623 schedule_work(&info->tqueue); }
625 if (!(status & UART_MSR_CTS)) {
626 info->tty->hw_stopped = 1;
627 if ((info->type != PORT_16550A) &&
628 (!info->board->chip_flag)) {
629 info->IER &= ~UART_IER_THRI;
630 outb(info->IER, info->ioaddr +
637 info->flags &= ~ASYNC_CTS_FLOW;
639 outb(info->MCR, info->ioaddr + UART_MCR);
640 if (cflag & CLOCAL) {
641 info->flags &= ~ASYNC_CHECK_CD;
643 info->flags |= ASYNC_CHECK_CD;
644 info->IER |= UART_IER_MSI;
646 outb(info->IER, info->ioaddr + UART_IER);
649 * Set up parity check flag
651 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
652 if (I_INPCK(info->tty))
653 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
654 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
655 info->read_status_mask |= UART_LSR_BI;
657 info->ignore_status_mask = 0;
659 if (I_IGNBRK(info->tty)) {
660 info->ignore_status_mask |= UART_LSR_BI;
661 info->read_status_mask |= UART_LSR_BI;
663 * If we're ignore parity and break indicators, ignore
664 * overruns too. (For real raw support).
666 if (I_IGNPAR(info->tty)) {
667 info->ignore_status_mask |=
671 info->read_status_mask |=
677 if (info->board->chip_flag) {
678 spin_lock_irqsave(&info->slock, flags);
679 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
680 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
681 if (I_IXON(info->tty)) {
682 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
684 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
686 if (I_IXOFF(info->tty)) {
687 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
689 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
691 spin_unlock_irqrestore(&info->slock, flags);
695 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
696 outb(cval, info->ioaddr + UART_LCR);
701 static void mxser_check_modem_status(struct mxser_port *port, int status)
703 /* update input line counters */
704 if (status & UART_MSR_TERI)
706 if (status & UART_MSR_DDSR)
708 if (status & UART_MSR_DDCD)
710 if (status & UART_MSR_DCTS)
712 port->mon_data.modem_status = status;
713 wake_up_interruptible(&port->delta_msr_wait);
715 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
716 if (status & UART_MSR_DCD)
717 wake_up_interruptible(&port->open_wait);
718 schedule_work(&port->tqueue);
721 if (port->flags & ASYNC_CTS_FLOW) {
722 if (port->tty->hw_stopped) {
723 if (status & UART_MSR_CTS) {
724 port->tty->hw_stopped = 0;
726 if ((port->type != PORT_16550A) &&
727 (!port->board->chip_flag)) {
728 outb(port->IER & ~UART_IER_THRI,
729 port->ioaddr + UART_IER);
730 port->IER |= UART_IER_THRI;
731 outb(port->IER, port->ioaddr +
734 set_bit(MXSER_EVENT_TXLOW, &port->event);
735 schedule_work(&port->tqueue);
738 if (!(status & UART_MSR_CTS)) {
739 port->tty->hw_stopped = 1;
740 if (port->type != PORT_16550A &&
741 !port->board->chip_flag) {
742 port->IER &= ~UART_IER_THRI;
743 outb(port->IER, port->ioaddr +
751 static int mxser_startup(struct mxser_port *info)
756 page = __get_free_page(GFP_KERNEL);
760 spin_lock_irqsave(&info->slock, flags);
762 if (info->flags & ASYNC_INITIALIZED) {
764 spin_unlock_irqrestore(&info->slock, flags);
768 if (!info->ioaddr || !info->type) {
770 set_bit(TTY_IO_ERROR, &info->tty->flags);
772 spin_unlock_irqrestore(&info->slock, flags);
778 info->xmit_buf = (unsigned char *) page;
781 * Clear the FIFO buffers and disable them
782 * (they will be reenabled in mxser_change_speed())
784 if (info->board->chip_flag)
785 outb((UART_FCR_CLEAR_RCVR |
786 UART_FCR_CLEAR_XMIT |
787 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
789 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
790 info->ioaddr + UART_FCR);
793 * At this point there's no way the LSR could still be 0xFF;
794 * if it is, then bail out, because there's likely no UART
797 if (inb(info->ioaddr + UART_LSR) == 0xff) {
798 spin_unlock_irqrestore(&info->slock, flags);
799 if (capable(CAP_SYS_ADMIN)) {
801 set_bit(TTY_IO_ERROR, &info->tty->flags);
808 * Clear the interrupt registers.
810 (void) inb(info->ioaddr + UART_LSR);
811 (void) inb(info->ioaddr + UART_RX);
812 (void) inb(info->ioaddr + UART_IIR);
813 (void) inb(info->ioaddr + UART_MSR);
816 * Now, initialize the UART
818 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
819 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
820 outb(info->MCR, info->ioaddr + UART_MCR);
823 * Finally, enable interrupts
825 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
827 if (info->board->chip_flag)
828 info->IER |= MOXA_MUST_IER_EGDAI;
829 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
832 * And clear the interrupt registers again for luck.
834 (void) inb(info->ioaddr + UART_LSR);
835 (void) inb(info->ioaddr + UART_RX);
836 (void) inb(info->ioaddr + UART_IIR);
837 (void) inb(info->ioaddr + UART_MSR);
840 clear_bit(TTY_IO_ERROR, &info->tty->flags);
841 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
844 * and set the speed of the serial port
846 spin_unlock_irqrestore(&info->slock, flags);
847 mxser_change_speed(info, NULL);
849 info->flags |= ASYNC_INITIALIZED;
854 * This routine will shutdown a serial port; interrupts maybe disabled, and
855 * DTR is dropped if the hangup on close termio flag is on.
857 static void mxser_shutdown(struct mxser_port *info)
861 if (!(info->flags & ASYNC_INITIALIZED))
864 spin_lock_irqsave(&info->slock, flags);
867 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
868 * here so the queue might never be waken up
870 wake_up_interruptible(&info->delta_msr_wait);
873 * Free the IRQ, if necessary
875 if (info->xmit_buf) {
876 free_page((unsigned long) info->xmit_buf);
877 info->xmit_buf = NULL;
881 outb(0x00, info->ioaddr + UART_IER);
883 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
884 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
885 outb(info->MCR, info->ioaddr + UART_MCR);
887 /* clear Rx/Tx FIFO's */
888 if (info->board->chip_flag)
889 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
890 MOXA_MUST_FCR_GDA_MODE_ENABLE,
891 info->ioaddr + UART_FCR);
893 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
894 info->ioaddr + UART_FCR);
896 /* read data port to reset things */
897 (void) inb(info->ioaddr + UART_RX);
900 set_bit(TTY_IO_ERROR, &info->tty->flags);
902 info->flags &= ~ASYNC_INITIALIZED;
904 if (info->board->chip_flag)
905 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
907 spin_unlock_irqrestore(&info->slock, flags);
911 * This routine is called whenever a serial port is opened. It
912 * enables interrupts for a serial port, linking in its async structure into
913 * the IRQ chain. It also performs the serial-specific
914 * initialization for the tty structure.
916 static int mxser_open(struct tty_struct *tty, struct file *filp)
918 struct mxser_port *info;
921 /* initialize driver_data in case something fails */
922 tty->driver_data = NULL;
925 if (line == MXSER_PORTS)
927 if (line < 0 || line > MXSER_PORTS)
929 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
933 tty->driver_data = info;
936 * Start up serial port
939 retval = mxser_startup(info);
943 retval = mxser_block_til_ready(tty, filp, info);
947 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
948 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
949 *tty->termios = info->normal_termios;
951 *tty->termios = info->callout_termios;
952 mxser_change_speed(info, NULL);
955 info->session = process_session(current);
956 info->pgrp = process_group(current);
958 /* unmark here for very high baud rate (ex. 921600 bps) used */
959 tty->low_latency = 1;
964 * This routine is called when the serial port gets closed. First, we
965 * wait for the last remaining data to be sent. Then, we unlink its
966 * async structure from the interrupt chain if necessary, and we free
967 * that IRQ if nothing is left in the chain.
969 static void mxser_close(struct tty_struct *tty, struct file *filp)
971 struct mxser_port *info = tty->driver_data;
973 unsigned long timeout;
976 if (tty->index == MXSER_PORTS)
981 spin_lock_irqsave(&info->slock, flags);
983 if (tty_hung_up_p(filp)) {
984 spin_unlock_irqrestore(&info->slock, flags);
987 if ((tty->count == 1) && (info->count != 1)) {
989 * Uh, oh. tty->count is 1, which means that the tty
990 * structure will be freed. Info->count should always
991 * be one in these conditions. If it's greater than
992 * one, we've got real problems, since it means the
993 * serial port won't be shutdown.
995 printk(KERN_ERR "mxser_close: bad serial port count; "
996 "tty->count is 1, info->count is %d\n", info->count);
999 if (--info->count < 0) {
1000 printk(KERN_ERR "mxser_close: bad serial port count for "
1001 "ttys%d: %d\n", tty->index, info->count);
1005 spin_unlock_irqrestore(&info->slock, flags);
1008 info->flags |= ASYNC_CLOSING;
1009 spin_unlock_irqrestore(&info->slock, flags);
1011 * Save the termios structure, since this port may have
1012 * separate termios for callout and dialin.
1014 if (info->flags & ASYNC_NORMAL_ACTIVE)
1015 info->normal_termios = *tty->termios;
1017 * Now we wait for the transmit buffer to clear; and we notify
1018 * the line discipline to only process XON/XOFF characters.
1021 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1022 tty_wait_until_sent(tty, info->closing_wait);
1024 * At this point we stop accepting input. To do this, we
1025 * disable the receive line status interrupts, and tell the
1026 * interrupt driver to stop checking the data ready bit in the
1027 * line status register.
1029 info->IER &= ~UART_IER_RLSI;
1030 if (info->board->chip_flag)
1031 info->IER &= ~MOXA_MUST_RECV_ISR;
1033 if (info->flags & ASYNC_INITIALIZED) {
1034 outb(info->IER, info->ioaddr + UART_IER);
1036 * Before we drop DTR, make sure the UART transmitter
1037 * has completely drained; this is especially
1038 * important if there is a transmit FIFO!
1040 timeout = jiffies + HZ;
1041 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
1042 schedule_timeout_interruptible(5);
1043 if (time_after(jiffies, timeout))
1047 mxser_shutdown(info);
1049 if (tty->driver->flush_buffer)
1050 tty->driver->flush_buffer(tty);
1052 tty_ldisc_flush(tty);
1057 if (info->blocked_open) {
1058 if (info->close_delay)
1059 schedule_timeout_interruptible(info->close_delay);
1060 wake_up_interruptible(&info->open_wait);
1063 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1064 wake_up_interruptible(&info->close_wait);
1068 static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1071 struct mxser_port *info = tty->driver_data;
1072 unsigned long flags;
1074 if (!info->xmit_buf)
1078 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1079 SERIAL_XMIT_SIZE - info->xmit_head));
1083 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1084 spin_lock_irqsave(&info->slock, flags);
1085 info->xmit_head = (info->xmit_head + c) &
1086 (SERIAL_XMIT_SIZE - 1);
1087 info->xmit_cnt += c;
1088 spin_unlock_irqrestore(&info->slock, flags);
1095 if (info->xmit_cnt && !tty->stopped) {
1096 if (!tty->hw_stopped ||
1097 (info->type == PORT_16550A) ||
1098 (info->board->chip_flag)) {
1099 spin_lock_irqsave(&info->slock, flags);
1100 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1102 info->IER |= UART_IER_THRI;
1103 outb(info->IER, info->ioaddr + UART_IER);
1104 spin_unlock_irqrestore(&info->slock, flags);
1110 static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1112 struct mxser_port *info = tty->driver_data;
1113 unsigned long flags;
1115 if (!info->xmit_buf)
1118 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1121 spin_lock_irqsave(&info->slock, flags);
1122 info->xmit_buf[info->xmit_head++] = ch;
1123 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1125 spin_unlock_irqrestore(&info->slock, flags);
1126 if (!tty->stopped) {
1127 if (!tty->hw_stopped ||
1128 (info->type == PORT_16550A) ||
1129 info->board->chip_flag) {
1130 spin_lock_irqsave(&info->slock, flags);
1131 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1132 info->IER |= UART_IER_THRI;
1133 outb(info->IER, info->ioaddr + UART_IER);
1134 spin_unlock_irqrestore(&info->slock, flags);
1140 static void mxser_flush_chars(struct tty_struct *tty)
1142 struct mxser_port *info = tty->driver_data;
1143 unsigned long flags;
1145 if (info->xmit_cnt <= 0 ||
1149 (info->type != PORT_16550A) &&
1150 (!info->board->chip_flag)
1154 spin_lock_irqsave(&info->slock, flags);
1156 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1157 info->IER |= UART_IER_THRI;
1158 outb(info->IER, info->ioaddr + UART_IER);
1160 spin_unlock_irqrestore(&info->slock, flags);
1163 static int mxser_write_room(struct tty_struct *tty)
1165 struct mxser_port *info = tty->driver_data;
1168 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1174 static int mxser_chars_in_buffer(struct tty_struct *tty)
1176 struct mxser_port *info = tty->driver_data;
1177 return info->xmit_cnt;
1180 static void mxser_flush_buffer(struct tty_struct *tty)
1182 struct mxser_port *info = tty->driver_data;
1184 unsigned long flags;
1187 spin_lock_irqsave(&info->slock, flags);
1188 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1190 fcr = inb(info->ioaddr + UART_FCR);
1191 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1192 info->ioaddr + UART_FCR);
1193 outb(fcr, info->ioaddr + UART_FCR);
1195 spin_unlock_irqrestore(&info->slock, flags);
1201 * ------------------------------------------------------------
1202 * friends of mxser_ioctl()
1203 * ------------------------------------------------------------
1205 static int mxser_get_serial_info(struct mxser_port *info,
1206 struct serial_struct __user *retinfo)
1208 struct serial_struct tmp;
1212 memset(&tmp, 0, sizeof(tmp));
1213 tmp.type = info->type;
1214 tmp.line = info->tty->index;
1215 tmp.port = info->ioaddr;
1216 tmp.irq = info->board->irq;
1217 tmp.flags = info->flags;
1218 tmp.baud_base = info->baud_base;
1219 tmp.close_delay = info->close_delay;
1220 tmp.closing_wait = info->closing_wait;
1221 tmp.custom_divisor = info->custom_divisor;
1223 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1228 static int mxser_set_serial_info(struct mxser_port *info,
1229 struct serial_struct __user *new_info)
1231 struct serial_struct new_serial;
1235 if (!new_info || !info->ioaddr)
1237 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1240 if ((new_serial.irq != info->board->irq) ||
1241 (new_serial.port != info->ioaddr) ||
1242 (new_serial.custom_divisor != info->custom_divisor) ||
1243 (new_serial.baud_base != info->baud_base))
1246 flags = info->flags & ASYNC_SPD_MASK;
1248 if (!capable(CAP_SYS_ADMIN)) {
1249 if ((new_serial.baud_base != info->baud_base) ||
1250 (new_serial.close_delay != info->close_delay) ||
1251 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
1253 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1254 (new_serial.flags & ASYNC_USR_MASK));
1257 * OK, past this point, all the error checking has been done.
1258 * At this point, we start making changes.....
1260 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1261 (new_serial.flags & ASYNC_FLAGS));
1262 info->close_delay = new_serial.close_delay * HZ / 100;
1263 info->closing_wait = new_serial.closing_wait * HZ / 100;
1264 info->tty->low_latency =
1265 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1266 info->tty->low_latency = 0;
1269 info->type = new_serial.type;
1271 process_txrx_fifo(info);
1273 if (info->flags & ASYNC_INITIALIZED) {
1274 if (flags != (info->flags & ASYNC_SPD_MASK))
1275 mxser_change_speed(info, NULL);
1277 retval = mxser_startup(info);
1283 * mxser_get_lsr_info - get line status register info
1285 * Purpose: Let user call ioctl() to get info when the UART physically
1286 * is emptied. On bus types like RS485, the transmitter must
1287 * release the bus after transmitting. This must be done when
1288 * the transmit shift register is empty, not be done when the
1289 * transmit holding register is empty. This functionality
1290 * allows an RS485 driver to be written in user space.
1292 static int mxser_get_lsr_info(struct mxser_port *info,
1293 unsigned int __user *value)
1295 unsigned char status;
1296 unsigned int result;
1297 unsigned long flags;
1299 spin_lock_irqsave(&info->slock, flags);
1300 status = inb(info->ioaddr + UART_LSR);
1301 spin_unlock_irqrestore(&info->slock, flags);
1302 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1303 return put_user(result, value);
1307 * This routine sends a break character out the serial port.
1309 static void mxser_send_break(struct mxser_port *info, int duration)
1311 unsigned long flags;
1315 set_current_state(TASK_INTERRUPTIBLE);
1316 spin_lock_irqsave(&info->slock, flags);
1317 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1318 info->ioaddr + UART_LCR);
1319 spin_unlock_irqrestore(&info->slock, flags);
1320 schedule_timeout(duration);
1321 spin_lock_irqsave(&info->slock, flags);
1322 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1323 info->ioaddr + UART_LCR);
1324 spin_unlock_irqrestore(&info->slock, flags);
1327 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
1329 struct mxser_port *info = tty->driver_data;
1330 unsigned char control, status;
1331 unsigned long flags;
1334 if (tty->index == MXSER_PORTS)
1335 return -ENOIOCTLCMD;
1336 if (test_bit(TTY_IO_ERROR, &tty->flags))
1339 control = info->MCR;
1341 spin_lock_irqsave(&info->slock, flags);
1342 status = inb(info->ioaddr + UART_MSR);
1343 if (status & UART_MSR_ANY_DELTA)
1344 mxser_check_modem_status(info, status);
1345 spin_unlock_irqrestore(&info->slock, flags);
1346 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1347 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1348 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1349 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1350 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1351 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1354 static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1355 unsigned int set, unsigned int clear)
1357 struct mxser_port *info = tty->driver_data;
1358 unsigned long flags;
1361 if (tty->index == MXSER_PORTS)
1362 return -ENOIOCTLCMD;
1363 if (test_bit(TTY_IO_ERROR, &tty->flags))
1366 spin_lock_irqsave(&info->slock, flags);
1368 if (set & TIOCM_RTS)
1369 info->MCR |= UART_MCR_RTS;
1370 if (set & TIOCM_DTR)
1371 info->MCR |= UART_MCR_DTR;
1373 if (clear & TIOCM_RTS)
1374 info->MCR &= ~UART_MCR_RTS;
1375 if (clear & TIOCM_DTR)
1376 info->MCR &= ~UART_MCR_DTR;
1378 outb(info->MCR, info->ioaddr + UART_MCR);
1379 spin_unlock_irqrestore(&info->slock, flags);
1383 static int mxser_program_mode(int port)
1387 spin_lock(&gm_lock);
1395 spin_unlock(&gm_lock);
1397 id = inb(port + 1) & 0x1F;
1398 if ((id != C168_ASIC_ID) &&
1399 (id != C104_ASIC_ID) &&
1400 (id != C102_ASIC_ID) &&
1401 (id != CI132_ASIC_ID) &&
1402 (id != CI134_ASIC_ID) &&
1403 (id != CI104J_ASIC_ID))
1405 for (i = 0, j = 0; i < 4; i++) {
1409 } else if ((j == 1) && (n == 1)) {
1420 static void mxser_normal_mode(int port)
1424 outb(0xA5, port + 1);
1425 outb(0x80, port + 3);
1426 outb(12, port + 0); /* 9600 bps */
1428 outb(0x03, port + 3); /* 8 data bits */
1429 outb(0x13, port + 4); /* loop back mode */
1430 for (i = 0; i < 16; i++) {
1432 if ((n & 0x61) == 0x60)
1437 outb(0x00, port + 4);
1440 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1441 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1442 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1443 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1444 #define EN_CCMD 0x000 /* Chip's command register */
1445 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1446 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1447 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1448 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1449 #define EN0_DCFG 0x00E /* Data configuration reg WR */
1450 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1451 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1452 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1453 static int mxser_read_register(int port, unsigned short *regs)
1455 int i, k, value, id;
1458 id = mxser_program_mode(port);
1461 for (i = 0; i < 14; i++) {
1462 k = (i & 0x3F) | 0x180;
1463 for (j = 0x100; j > 0; j >>= 1) {
1464 outb(CHIP_CS, port);
1466 outb(CHIP_CS | CHIP_DO, port);
1467 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1469 outb(CHIP_CS, port);
1470 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1475 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1476 outb(CHIP_CS, port);
1477 outb(CHIP_CS | CHIP_SK, port);
1478 if (inb(port) & CHIP_DI)
1484 mxser_normal_mode(port);
1488 static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1490 struct mxser_port *port;
1496 /* if (copy_to_user(argp, mxsercfg,
1497 sizeof(struct mxser_hwconf) * 4))
1501 case MOXA_GET_MAJOR:
1502 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1506 case MOXA_GET_CUMAJOR:
1507 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1511 case MOXA_CHKPORTENABLE:
1514 for (i = 0; i < MXSER_BOARDS; i++)
1515 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1516 if (mxser_boards[i].ports[j].ioaddr)
1519 return put_user(result, (unsigned long __user *)argp);
1520 case MOXA_GETDATACOUNT:
1521 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1524 case MOXA_GETMSTATUS:
1525 for (i = 0; i < MXSER_BOARDS; i++)
1526 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1527 port = &mxser_boards[i].ports[j];
1530 if (!port->ioaddr) {
1531 GMStatus[i].dcd = 0;
1532 GMStatus[i].dsr = 0;
1533 GMStatus[i].cts = 0;
1537 if (!port->tty || !port->tty->termios)
1539 port->normal_termios.c_cflag;
1542 port->tty->termios->c_cflag;
1544 status = inb(port->ioaddr + UART_MSR);
1545 if (status & 0x80 /*UART_MSR_DCD */ )
1546 GMStatus[i].dcd = 1;
1548 GMStatus[i].dcd = 0;
1550 if (status & 0x20 /*UART_MSR_DSR */ )
1551 GMStatus[i].dsr = 1;
1553 GMStatus[i].dsr = 0;
1556 if (status & 0x10 /*UART_MSR_CTS */ )
1557 GMStatus[i].cts = 1;
1559 GMStatus[i].cts = 0;
1561 if (copy_to_user(argp, GMStatus,
1562 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1565 case MOXA_ASPP_MON_EXT: {
1566 int status, p, shiftbit;
1567 unsigned long opmode;
1568 unsigned cflag, iflag;
1570 for (i = 0; i < MXSER_BOARDS; i++)
1571 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1572 port = &mxser_boards[i].ports[j];
1576 status = mxser_get_msr(port->ioaddr, 0, i);
1578 if (status & UART_MSR_TERI)
1580 if (status & UART_MSR_DDSR)
1582 if (status & UART_MSR_DDCD)
1584 if (status & UART_MSR_DCTS)
1587 port->mon_data.modem_status = status;
1588 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1589 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1590 mon_data_ext.up_rxcnt[i] =
1591 port->mon_data.up_rxcnt;
1592 mon_data_ext.up_txcnt[i] =
1593 port->mon_data.up_txcnt;
1594 mon_data_ext.modem_status[i] =
1595 port->mon_data.modem_status;
1596 mon_data_ext.baudrate[i] = port->realbaud;
1598 if (!port->tty || !port->tty->termios) {
1599 cflag = port->normal_termios.c_cflag;
1600 iflag = port->normal_termios.c_iflag;
1602 cflag = port->tty->termios->c_cflag;
1603 iflag = port->tty->termios->c_iflag;
1606 mon_data_ext.databits[i] = cflag & CSIZE;
1608 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1610 mon_data_ext.parity[i] =
1611 cflag & (PARENB | PARODD | CMSPAR);
1613 mon_data_ext.flowctrl[i] = 0x00;
1615 if (cflag & CRTSCTS)
1616 mon_data_ext.flowctrl[i] |= 0x03;
1618 if (iflag & (IXON | IXOFF))
1619 mon_data_ext.flowctrl[i] |= 0x0C;
1621 if (port->type == PORT_16550A)
1622 mon_data_ext.fifo[i] = 1;
1624 mon_data_ext.fifo[i] = 0;
1628 opmode = inb(port->opmode_ioaddr) >> shiftbit;
1629 opmode &= OP_MODE_MASK;
1631 mon_data_ext.iftype[i] = opmode;
1634 if (copy_to_user(argp, &mon_data_ext,
1635 sizeof(mon_data_ext)))
1641 return -ENOIOCTLCMD;
1646 static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1647 unsigned int cmd, unsigned long arg)
1649 struct mxser_port *info = tty->driver_data;
1650 struct async_icount cprev, cnow; /* kernel counter temps */
1651 struct serial_icounter_struct __user *p_cuser;
1652 unsigned long templ;
1653 unsigned long flags;
1654 void __user *argp = (void __user *)arg;
1657 if (tty->index == MXSER_PORTS)
1658 return mxser_ioctl_special(cmd, argp);
1660 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1662 unsigned long opmode;
1663 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1665 unsigned char val, mask;
1668 if (cmd == MOXA_SET_OP_MODE) {
1669 if (get_user(opmode, (int __user *) argp))
1671 if (opmode != RS232_MODE &&
1672 opmode != RS485_2WIRE_MODE &&
1673 opmode != RS422_MODE &&
1674 opmode != RS485_4WIRE_MODE)
1678 val = inb(info->opmode_ioaddr);
1680 val |= (opmode << shiftbit);
1681 outb(val, info->opmode_ioaddr);
1684 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1685 opmode &= OP_MODE_MASK;
1686 if (copy_to_user(argp, &opmode, sizeof(int)))
1692 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1693 test_bit(TTY_IO_ERROR, &tty->flags))
1697 case TCSBRK: /* SVID version: non-zero arg --> no break */
1698 retval = tty_check_change(tty);
1701 tty_wait_until_sent(tty, 0);
1703 mxser_send_break(info, HZ / 4); /* 1/4 second */
1705 case TCSBRKP: /* support for POSIX tcsendbreak() */
1706 retval = tty_check_change(tty);
1709 tty_wait_until_sent(tty, 0);
1710 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1713 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1715 if (get_user(templ, (unsigned long __user *) argp))
1718 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1721 return mxser_get_serial_info(info, argp);
1723 return mxser_set_serial_info(info, argp);
1724 case TIOCSERGETLSR: /* Get line status register */
1725 return mxser_get_lsr_info(info, argp);
1727 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1728 * - mask passed in arg for lines of interest
1729 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1730 * Caller should use TIOCGICOUNT to see which one it was
1733 DECLARE_WAITQUEUE(wait, current);
1735 spin_lock_irqsave(&info->slock, flags);
1736 cprev = info->icount; /* note the counters on entry */
1737 spin_unlock_irqrestore(&info->slock, flags);
1739 add_wait_queue(&info->delta_msr_wait, &wait);
1741 spin_lock_irqsave(&info->slock, flags);
1742 cnow = info->icount; /* atomic copy */
1743 spin_unlock_irqrestore(&info->slock, flags);
1745 set_current_state(TASK_INTERRUPTIBLE);
1746 if (((arg & TIOCM_RNG) &&
1747 (cnow.rng != cprev.rng)) ||
1748 ((arg & TIOCM_DSR) &&
1749 (cnow.dsr != cprev.dsr)) ||
1750 ((arg & TIOCM_CD) &&
1751 (cnow.dcd != cprev.dcd)) ||
1752 ((arg & TIOCM_CTS) &&
1753 (cnow.cts != cprev.cts))) {
1757 /* see if a signal did it */
1758 if (signal_pending(current)) {
1764 current->state = TASK_RUNNING;
1765 remove_wait_queue(&info->delta_msr_wait, &wait);
1770 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1771 * Return: write counters to the user passed counter struct
1772 * NB: both 1->0 and 0->1 transitions are counted except for
1773 * RI where only 0->1 is counted.
1776 spin_lock_irqsave(&info->slock, flags);
1777 cnow = info->icount;
1778 spin_unlock_irqrestore(&info->slock, flags);
1780 if (put_user(cnow.frame, &p_cuser->frame))
1782 if (put_user(cnow.brk, &p_cuser->brk))
1784 if (put_user(cnow.overrun, &p_cuser->overrun))
1786 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1788 if (put_user(cnow.parity, &p_cuser->parity))
1790 if (put_user(cnow.rx, &p_cuser->rx))
1792 if (put_user(cnow.tx, &p_cuser->tx))
1794 put_user(cnow.cts, &p_cuser->cts);
1795 put_user(cnow.dsr, &p_cuser->dsr);
1796 put_user(cnow.rng, &p_cuser->rng);
1797 put_user(cnow.dcd, &p_cuser->dcd);
1799 case MOXA_HighSpeedOn:
1800 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1801 case MOXA_SDS_RSTICOUNTER:
1802 info->mon_data.rxcnt = 0;
1803 info->mon_data.txcnt = 0;
1805 case MOXA_ASPP_SETBAUD:{
1807 if (get_user(baud, (long __user *)argp))
1809 mxser_set_baud(info, baud);
1812 case MOXA_ASPP_GETBAUD:
1813 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1818 case MOXA_ASPP_OQUEUE:{
1821 len = mxser_chars_in_buffer(tty);
1823 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1825 len += (lsr ? 0 : 1);
1827 if (copy_to_user(argp, &len, sizeof(int)))
1832 case MOXA_ASPP_MON: {
1835 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1836 mxser_check_modem_status(info, status);
1838 mcr = inb(info->ioaddr + UART_MCR);
1839 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1840 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1842 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1844 if (mcr & MOXA_MUST_MCR_TX_XON)
1845 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1847 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1849 if (info->tty->hw_stopped)
1850 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1852 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1854 if (copy_to_user(argp, &info->mon_data,
1855 sizeof(struct mxser_mon)))
1860 case MOXA_ASPP_LSTATUS: {
1861 if (copy_to_user(argp, &info->err_shadow,
1862 sizeof(unsigned char)))
1865 info->err_shadow = 0;
1868 case MOXA_SET_BAUD_METHOD: {
1871 if (get_user(method, (int __user *)argp))
1873 mxser_set_baud_method[tty->index] = method;
1874 if (copy_to_user(argp, &method, sizeof(int)))
1880 return -ENOIOCTLCMD;
1885 static void mxser_stoprx(struct tty_struct *tty)
1887 struct mxser_port *info = tty->driver_data;
1889 info->ldisc_stop_rx = 1;
1891 if (info->board->chip_flag) {
1892 info->IER &= ~MOXA_MUST_RECV_ISR;
1893 outb(info->IER, info->ioaddr + UART_IER);
1895 info->x_char = STOP_CHAR(tty);
1896 outb(0, info->ioaddr + UART_IER);
1897 info->IER |= UART_IER_THRI;
1898 outb(info->IER, info->ioaddr + UART_IER);
1902 if (info->tty->termios->c_cflag & CRTSCTS) {
1903 info->MCR &= ~UART_MCR_RTS;
1904 outb(info->MCR, info->ioaddr + UART_MCR);
1909 * This routine is called by the upper-layer tty layer to signal that
1910 * incoming characters should be throttled.
1912 static void mxser_throttle(struct tty_struct *tty)
1917 static void mxser_unthrottle(struct tty_struct *tty)
1919 struct mxser_port *info = tty->driver_data;
1922 info->ldisc_stop_rx = 0;
1927 if (info->board->chip_flag) {
1928 info->IER |= MOXA_MUST_RECV_ISR;
1929 outb(info->IER, info->ioaddr + UART_IER);
1931 info->x_char = START_CHAR(tty);
1932 outb(0, info->ioaddr + UART_IER);
1933 info->IER |= UART_IER_THRI;
1934 outb(info->IER, info->ioaddr + UART_IER);
1939 if (info->tty->termios->c_cflag & CRTSCTS) {
1940 info->MCR |= UART_MCR_RTS;
1941 outb(info->MCR, info->ioaddr + UART_MCR);
1946 * mxser_stop() and mxser_start()
1948 * This routines are called before setting or resetting tty->stopped.
1949 * They enable or disable transmitter interrupts, as necessary.
1951 static void mxser_stop(struct tty_struct *tty)
1953 struct mxser_port *info = tty->driver_data;
1954 unsigned long flags;
1956 spin_lock_irqsave(&info->slock, flags);
1957 if (info->IER & UART_IER_THRI) {
1958 info->IER &= ~UART_IER_THRI;
1959 outb(info->IER, info->ioaddr + UART_IER);
1961 spin_unlock_irqrestore(&info->slock, flags);
1964 static void mxser_start(struct tty_struct *tty)
1966 struct mxser_port *info = tty->driver_data;
1967 unsigned long flags;
1969 spin_lock_irqsave(&info->slock, flags);
1970 if (info->xmit_cnt && info->xmit_buf) {
1971 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1972 info->IER |= UART_IER_THRI;
1973 outb(info->IER, info->ioaddr + UART_IER);
1975 spin_unlock_irqrestore(&info->slock, flags);
1978 static void mxser_set_termios(struct tty_struct *tty, struct termios *old_termios)
1980 struct mxser_port *info = tty->driver_data;
1981 unsigned long flags;
1983 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1984 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
1986 mxser_change_speed(info, old_termios);
1988 if ((old_termios->c_cflag & CRTSCTS) &&
1989 !(tty->termios->c_cflag & CRTSCTS)) {
1990 tty->hw_stopped = 0;
1995 /* Handle sw stopped */
1996 if ((old_termios->c_iflag & IXON) &&
1997 !(tty->termios->c_iflag & IXON)) {
2000 if (info->board->chip_flag) {
2001 spin_lock_irqsave(&info->slock, flags);
2002 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2003 spin_unlock_irqrestore(&info->slock, flags);
2011 * mxser_wait_until_sent() --- wait until the transmitter is empty
2013 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2015 struct mxser_port *info = tty->driver_data;
2016 unsigned long orig_jiffies, char_time;
2019 if (info->type == PORT_UNKNOWN)
2022 if (info->xmit_fifo_size == 0)
2023 return; /* Just in case.... */
2025 orig_jiffies = jiffies;
2027 * Set the check interval to be 1/5 of the estimated time to
2028 * send a single character, and make it at least 1. The check
2029 * interval should also be less than the timeout.
2031 * Note: we have to use pretty tight timings here to satisfy
2034 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2035 char_time = char_time / 5;
2038 if (timeout && timeout < char_time)
2039 char_time = timeout;
2041 * If the transmitter hasn't cleared in twice the approximate
2042 * amount of time to send the entire FIFO, it probably won't
2043 * ever clear. This assumes the UART isn't doing flow
2044 * control, which is currently the case. Hence, if it ever
2045 * takes longer than info->timeout, this is probably due to a
2046 * UART bug of some kind. So, we clamp the timeout parameter at
2049 if (!timeout || timeout > 2 * info->timeout)
2050 timeout = 2 * info->timeout;
2051 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2052 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2053 timeout, char_time);
2054 printk("jiff=%lu...", jiffies);
2056 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
2057 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2058 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2060 schedule_timeout_interruptible(char_time);
2061 if (signal_pending(current))
2063 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2066 set_current_state(TASK_RUNNING);
2068 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2069 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2074 * This routine is called by tty_hangup() when a hangup is signaled.
2076 void mxser_hangup(struct tty_struct *tty)
2078 struct mxser_port *info = tty->driver_data;
2080 mxser_flush_buffer(tty);
2081 mxser_shutdown(info);
2084 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2086 wake_up_interruptible(&info->open_wait);
2090 * mxser_rs_break() --- routine which turns the break handling on or off
2092 static void mxser_rs_break(struct tty_struct *tty, int break_state)
2094 struct mxser_port *info = tty->driver_data;
2095 unsigned long flags;
2097 spin_lock_irqsave(&info->slock, flags);
2098 if (break_state == -1)
2099 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2100 info->ioaddr + UART_LCR);
2102 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2103 info->ioaddr + UART_LCR);
2104 spin_unlock_irqrestore(&info->slock, flags);
2107 static void mxser_receive_chars(struct mxser_port *port, int *status)
2109 struct tty_struct *tty = port->tty;
2110 unsigned char ch, gdl;
2115 unsigned long flags;
2117 spin_lock_irqsave(&port->slock, flags);
2119 recv_room = tty->receive_room;
2120 if ((recv_room == 0) && (!port->ldisc_stop_rx))
2123 if (port->board->chip_flag != MOXA_OTHER_UART) {
2125 if (*status & UART_LSR_SPECIAL)
2127 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
2128 (*status & MOXA_MUST_LSR_RERR))
2130 if (*status & MOXA_MUST_LSR_RERR)
2133 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
2135 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
2136 gdl &= MOXA_MUST_GDL_MASK;
2137 if (gdl >= recv_room) {
2138 if (!port->ldisc_stop_rx)
2142 ch = inb(port->ioaddr + UART_RX);
2143 tty_insert_flip_char(tty, ch, 0);
2154 ch = inb(port->ioaddr + UART_RX);
2155 if (port->board->chip_flag && (*status & UART_LSR_OE))
2156 outb(0x23, port->ioaddr + UART_FCR);
2157 *status &= port->read_status_mask;
2158 if (*status & port->ignore_status_mask) {
2159 if (++ignored > 100)
2163 if (*status & UART_LSR_SPECIAL) {
2164 if (*status & UART_LSR_BI) {
2168 if (port->flags & ASYNC_SAK)
2170 } else if (*status & UART_LSR_PE) {
2172 port->icount.parity++;
2173 } else if (*status & UART_LSR_FE) {
2175 port->icount.frame++;
2176 } else if (*status & UART_LSR_OE) {
2178 port->icount.overrun++;
2181 tty_insert_flip_char(tty, ch, flag);
2183 if (cnt >= recv_room) {
2184 if (!port->ldisc_stop_rx)
2191 if (port->board->chip_flag)
2194 *status = inb(port->ioaddr + UART_LSR);
2195 } while (*status & UART_LSR_DR);
2198 mxvar_log.rxcnt[port->tty->index] += cnt;
2199 port->mon_data.rxcnt += cnt;
2200 port->mon_data.up_rxcnt += cnt;
2201 spin_unlock_irqrestore(&port->slock, flags);
2203 tty_flip_buffer_push(tty);
2206 static void mxser_transmit_chars(struct mxser_port *port)
2209 unsigned long flags;
2211 spin_lock_irqsave(&port->slock, flags);
2214 outb(port->x_char, port->ioaddr + UART_TX);
2216 mxvar_log.txcnt[port->tty->index]++;
2217 port->mon_data.txcnt++;
2218 port->mon_data.up_txcnt++;
2223 if (port->xmit_buf == 0)
2226 if ((port->xmit_cnt <= 0) || port->tty->stopped ||
2227 (port->tty->hw_stopped &&
2228 (port->type != PORT_16550A) &&
2229 (!port->board->chip_flag))) {
2230 port->IER &= ~UART_IER_THRI;
2231 outb(port->IER, port->ioaddr + UART_IER);
2235 cnt = port->xmit_cnt;
2236 count = port->xmit_fifo_size;
2238 outb(port->xmit_buf[port->xmit_tail++],
2239 port->ioaddr + UART_TX);
2240 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2241 if (--port->xmit_cnt <= 0)
2243 } while (--count > 0);
2244 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
2246 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2247 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2248 port->icount.tx += (cnt - port->xmit_cnt);
2250 if (port->xmit_cnt < WAKEUP_CHARS) {
2251 set_bit(MXSER_EVENT_TXLOW, &port->event);
2252 schedule_work(&port->tqueue);
2254 if (port->xmit_cnt <= 0) {
2255 port->IER &= ~UART_IER_THRI;
2256 outb(port->IER, port->ioaddr + UART_IER);
2259 spin_unlock_irqrestore(&port->slock, flags);
2263 * This is the serial driver's generic interrupt routine
2265 static irqreturn_t mxser_interrupt(int irq, void *dev_id)
2268 struct mxser_board *brd = NULL;
2269 struct mxser_port *port;
2270 int max, irqbits, bits, msr;
2271 int pass_counter = 0;
2272 unsigned int int_cnt;
2273 int handled = IRQ_NONE;
2275 for (i = 0; i < MXSER_BOARDS; i++)
2276 if (dev_id == &mxser_boards[i]) {
2281 if (i == MXSER_BOARDS)
2285 max = brd->info->nports;
2287 irqbits = inb(brd->vector) & brd->vector_mask;
2288 if (irqbits == brd->vector_mask)
2291 handled = IRQ_HANDLED;
2292 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2293 if (irqbits == brd->vector_mask)
2297 port = &brd->ports[i];
2301 iir = inb(port->ioaddr + UART_IIR);
2302 if (iir & UART_IIR_NO_INT)
2304 iir &= MOXA_MUST_IIR_MASK;
2306 status = inb(port->ioaddr + UART_LSR);
2307 outb(0x27, port->ioaddr + UART_FCR);
2308 inb(port->ioaddr + UART_MSR);
2312 status = inb(port->ioaddr + UART_LSR);
2314 if (status & UART_LSR_PE)
2315 port->err_shadow |= NPPI_NOTIFY_PARITY;
2316 if (status & UART_LSR_FE)
2317 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2318 if (status & UART_LSR_OE)
2320 NPPI_NOTIFY_HW_OVERRUN;
2321 if (status & UART_LSR_BI)
2322 port->err_shadow |= NPPI_NOTIFY_BREAK;
2324 if (port->board->chip_flag) {
2325 if (iir == MOXA_MUST_IIR_GDA ||
2326 iir == MOXA_MUST_IIR_RDA ||
2327 iir == MOXA_MUST_IIR_RTO ||
2328 iir == MOXA_MUST_IIR_LSR)
2329 mxser_receive_chars(port,
2333 status &= port->read_status_mask;
2334 if (status & UART_LSR_DR)
2335 mxser_receive_chars(port,
2338 msr = inb(port->ioaddr + UART_MSR);
2339 if (msr & UART_MSR_ANY_DELTA)
2340 mxser_check_modem_status(port, msr);
2342 if (port->board->chip_flag) {
2343 if (iir == 0x02 && (status &
2345 mxser_transmit_chars(port);
2347 if (status & UART_LSR_THRE)
2348 mxser_transmit_chars(port);
2350 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2352 if (pass_counter++ > MXSER_ISR_PASS_LIMIT)
2353 break; /* Prevent infinite loops */
2360 static const struct tty_operations mxser_ops = {
2362 .close = mxser_close,
2363 .write = mxser_write,
2364 .put_char = mxser_put_char,
2365 .flush_chars = mxser_flush_chars,
2366 .write_room = mxser_write_room,
2367 .chars_in_buffer = mxser_chars_in_buffer,
2368 .flush_buffer = mxser_flush_buffer,
2369 .ioctl = mxser_ioctl,
2370 .throttle = mxser_throttle,
2371 .unthrottle = mxser_unthrottle,
2372 .set_termios = mxser_set_termios,
2374 .start = mxser_start,
2375 .hangup = mxser_hangup,
2376 .break_ctl = mxser_rs_break,
2377 .wait_until_sent = mxser_wait_until_sent,
2378 .tiocmget = mxser_tiocmget,
2379 .tiocmset = mxser_tiocmset,
2383 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2386 static void mxser_release_res(struct mxser_board *brd, unsigned int irq)
2388 struct pci_dev *pdev = brd->pdev;
2391 free_irq(brd->irq, brd);
2392 if (pdev != NULL) { /* PCI */
2393 pci_release_region(pdev, 2);
2394 pci_release_region(pdev, 3);
2397 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2398 release_region(brd->vector, 1);
2402 static int __devinit mxser_initbrd(struct mxser_board *brd)
2404 struct mxser_port *info;
2408 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
2410 for (i = 0; i < brd->info->nports; i++) {
2411 info = &brd->ports[i];
2414 info->ldisc_stop_rx = 0;
2416 /* Enhance mode enabled here */
2417 if (brd->chip_flag != MOXA_OTHER_UART)
2418 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
2420 info->flags = ASYNC_SHARE_IRQ;
2421 info->type = brd->uart_type;
2423 process_txrx_fifo(info);
2425 info->custom_divisor = info->baud_base * 16;
2426 info->close_delay = 5 * HZ / 10;
2427 info->closing_wait = 30 * HZ;
2428 INIT_WORK(&info->tqueue, mxser_do_softint, info);
2429 info->normal_termios = mxvar_sdriver->init_termios;
2430 init_waitqueue_head(&info->open_wait);
2431 init_waitqueue_head(&info->close_wait);
2432 init_waitqueue_head(&info->delta_msr_wait);
2433 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2434 info->err_shadow = 0;
2435 spin_lock_init(&info->slock);
2437 /* before set INT ISR, disable all int */
2438 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2439 info->ioaddr + UART_IER);
2442 * Allocate the IRQ if necessary
2445 retval = request_irq(brd->irq, mxser_interrupt,
2446 (brd->ports[0].flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED :
2447 IRQF_DISABLED, "mxser", brd);
2449 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2450 "conflict with another device.\n",
2451 brd->info->name, brd->irq);
2452 /* We hold resources, we need to release them. */
2453 mxser_release_res(brd, 0);
2459 static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
2462 unsigned short regs[16], irq;
2463 unsigned char scratch, scratch2;
2465 brd->chip_flag = MOXA_OTHER_UART;
2467 id = mxser_read_register(cap, regs);
2470 brd->info = &mxser_cards[0];
2473 brd->info = &mxser_cards[1];
2475 case CI104J_ASIC_ID:
2476 brd->info = &mxser_cards[2];
2479 brd->info = &mxser_cards[5];
2482 brd->info = &mxser_cards[6];
2485 brd->info = &mxser_cards[7];
2492 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2493 Flag-hack checks if configuration should be read as 2-port here. */
2494 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
2495 irq = regs[9] & 0xF000;
2496 irq = irq | (irq >> 4);
2497 if (irq != (regs[9] & 0xFF00))
2498 return MXSER_ERR_IRQ_CONFLIT;
2499 } else if (brd->info->nports == 4) {
2500 irq = regs[9] & 0xF000;
2501 irq = irq | (irq >> 4);
2502 irq = irq | (irq >> 8);
2504 return MXSER_ERR_IRQ_CONFLIT;
2505 } else if (brd->info->nports == 8) {
2506 irq = regs[9] & 0xF000;
2507 irq = irq | (irq >> 4);
2508 irq = irq | (irq >> 8);
2509 if ((irq != regs[9]) || (irq != regs[10]))
2510 return MXSER_ERR_IRQ_CONFLIT;
2514 return MXSER_ERR_IRQ;
2515 brd->irq = ((int)(irq & 0xF000) >> 12);
2516 for (i = 0; i < 8; i++)
2517 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
2518 if ((regs[12] & 0x80) == 0)
2519 return MXSER_ERR_VECTOR;
2520 brd->vector = (int)regs[11]; /* interrupt vector */
2522 brd->vector_mask = 0x00FF;
2524 brd->vector_mask = 0x000F;
2525 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2526 if (regs[12] & bits) {
2527 brd->ports[i].baud_base = 921600;
2528 brd->ports[i].max_baud = 921600;
2530 brd->ports[i].baud_base = 115200;
2531 brd->ports[i].max_baud = 115200;
2534 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2535 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2536 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2537 outb(scratch2, cap + UART_LCR);
2538 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2539 scratch = inb(cap + UART_IIR);
2542 brd->uart_type = PORT_16550A;
2544 brd->uart_type = PORT_16450;
2545 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2547 return MXSER_ERR_IOADDR;
2548 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2549 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2550 return MXSER_ERR_VECTOR;
2552 return brd->info->nports;
2555 static int __init mxser_get_PCI_conf(const struct pci_device_id *ent,
2556 struct mxser_board *brd, struct pci_dev *pdev)
2559 unsigned long ioaddress;
2563 brd->info = &mxser_cards[ent->driver_data];
2564 ioaddress = pci_resource_start(pdev, 2);
2565 retval = pci_request_region(pdev, 2, "mxser(IO)");
2569 for (i = 0; i < brd->info->nports; i++)
2570 brd->ports[i].ioaddr = ioaddress + 8 * i;
2573 ioaddress = pci_resource_start(pdev, 3);
2574 retval = pci_request_region(pdev, 3, "mxser(vector)");
2577 brd->vector = ioaddress;
2580 brd->irq = pdev->irq;
2582 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2583 brd->uart_type = PORT_16550A;
2584 brd->vector_mask = 0;
2586 for (i = 0; i < brd->info->nports; i++) {
2587 for (j = 0; j < UART_INFO_NUM; j++) {
2588 if (Gpci_uart_info[j].type == brd->chip_flag) {
2589 brd->ports[i].max_baud =
2590 Gpci_uart_info[j].max_baud;
2592 /* exception....CP-102 */
2593 if (brd->info->flags & MXSER_HIGHBAUD)
2594 brd->ports[i].max_baud = 921600;
2600 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2601 for (i = 0; i < brd->info->nports; i++) {
2603 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2605 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
2607 outb(0, ioaddress + 4); /* default set to RS232 mode */
2608 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
2611 for (i = 0; i < brd->info->nports; i++) {
2612 brd->vector_mask |= (1 << i);
2613 brd->ports[i].baud_base = 921600;
2617 pci_release_region(pdev, 2);
2622 static int __init mxser_module_init(void)
2624 struct pci_dev *pdev = NULL;
2625 struct mxser_board *brd;
2627 unsigned int i, m, isaloop;
2630 pr_debug("Loading module mxser ...\n");
2632 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2635 spin_lock_init(&gm_lock);
2637 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2640 /* Initialize the tty_driver structure */
2641 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2642 mxvar_sdriver->name = "ttyM";
2643 mxvar_sdriver->major = ttymajor;
2644 mxvar_sdriver->minor_start = 0;
2645 mxvar_sdriver->num = MXSER_PORTS + 1;
2646 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2647 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2648 mxvar_sdriver->init_termios = tty_std_termios;
2649 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2650 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2651 tty_set_operations(mxvar_sdriver, &mxser_ops);
2653 retval = tty_register_driver(mxvar_sdriver);
2655 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2663 /* Start finding ISA boards here */
2664 for (isaloop = 0; isaloop < 2; isaloop++)
2665 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2667 cap = mxserBoardCAP[b]; /* predefined */
2669 cap = ioaddr[b]; /* module param */
2674 brd = &mxser_boards[m];
2675 retval = mxser_get_ISA_conf(cap, brd);
2678 printk(KERN_INFO "Found MOXA %s board "
2680 brd->info->name, ioaddr[b]);
2683 if (retval == MXSER_ERR_IRQ)
2684 printk(KERN_ERR "Invalid interrupt "
2685 "number, board not "
2687 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2688 printk(KERN_ERR "Invalid interrupt "
2689 "number, board not "
2691 else if (retval == MXSER_ERR_VECTOR)
2692 printk(KERN_ERR "Invalid interrupt "
2693 "vector, board not "
2695 else if (retval == MXSER_ERR_IOADDR)
2696 printk(KERN_ERR "Invalid I/O address, "
2697 "board not configured\n");
2704 /* mxser_initbrd will hook ISR. */
2705 if (mxser_initbrd(brd) < 0)
2708 for (i = 0; i < brd->info->nports; i++)
2709 tty_register_device(mxvar_sdriver,
2710 m * MXSER_PORTS_PER_BOARD + i, NULL);
2715 /* start finding PCI board here */
2716 n = ARRAY_SIZE(mxser_pcibrds) - 1;
2719 pdev = pci_get_device(mxser_pcibrds[b].vendor,
2720 mxser_pcibrds[b].device, pdev);
2725 printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
2726 mxser_cards[mxser_pcibrds[b].driver_data].name,
2727 pdev->bus->number, PCI_SLOT(pdev->devfn));
2728 if (m >= MXSER_BOARDS)
2730 "Too many Smartio/Industio family boards find "
2731 "(maximum %d), board not configured\n",
2734 if (pci_enable_device(pdev)) {
2735 printk(KERN_ERR "Moxa SmartI/O PCI enable "
2739 brd = &mxser_boards[m];
2741 retval = mxser_get_PCI_conf(&mxser_pcibrds[b],
2744 if (retval == MXSER_ERR_IRQ)
2746 "Invalid interrupt number, "
2747 "board not configured\n");
2748 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2750 "Invalid interrupt number, "
2751 "board not configured\n");
2752 else if (retval == MXSER_ERR_VECTOR)
2754 "Invalid interrupt vector, "
2755 "board not configured\n");
2756 else if (retval == MXSER_ERR_IOADDR)
2758 "Invalid I/O address, "
2759 "board not configured\n");
2762 /* mxser_initbrd will hook ISR. */
2763 if (mxser_initbrd(brd) < 0)
2765 for (i = 0; i < brd->info->nports; i++)
2766 tty_register_device(mxvar_sdriver,
2767 m * MXSER_PORTS_PER_BOARD + i,
2771 /* Keep an extra reference if we succeeded. It will
2772 be returned at unload time */
2782 pr_debug("Done.\n");
2786 tty_unregister_driver(mxvar_sdriver);
2788 put_tty_driver(mxvar_sdriver);
2792 static void __exit mxser_module_exit(void)
2796 pr_debug("Unloading module mxser ...\n");
2798 for (i = 0; i < MXSER_PORTS; i++)
2799 tty_unregister_device(mxvar_sdriver, i);
2800 tty_unregister_driver(mxvar_sdriver);
2801 put_tty_driver(mxvar_sdriver);
2803 for (i = 0; i < MXSER_BOARDS; i++)
2804 if (mxser_boards[i].info != NULL)
2805 mxser_release_res(&mxser_boards[i], 1);
2807 pr_debug("Done.\n");
2810 module_init(mxser_module_init);
2811 module_exit(mxser_module_exit);