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 /* above is modified by Victor Yu. 08-15-2002 */
351 static void process_txrx_fifo(struct mxser_port *info)
355 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
356 info->rx_trigger = 1;
357 info->rx_high_water = 1;
358 info->rx_low_water = 1;
359 info->xmit_fifo_size = 1;
361 for (i = 0; i < UART_INFO_NUM; i++)
362 if (info->board->chip_flag == Gpci_uart_info[i].type) {
363 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
364 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
365 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
366 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
371 static void mxser_do_softint(void *private_)
373 struct mxser_port *info = private_;
374 struct tty_struct *tty;
378 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
380 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event))
384 static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
386 unsigned char status = 0;
388 status = inb(baseaddr + UART_MSR);
390 mxser_msr[port] &= 0x0F;
391 mxser_msr[port] |= status;
392 status = mxser_msr[port];
399 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
400 struct mxser_port *port)
402 DECLARE_WAITQUEUE(wait, current);
408 * If non-blocking mode is set, or the port is not enabled,
409 * then make the check up front and then exit.
411 if ((filp->f_flags & O_NONBLOCK) ||
412 test_bit(TTY_IO_ERROR, &tty->flags)) {
413 port->flags |= ASYNC_NORMAL_ACTIVE;
417 if (tty->termios->c_cflag & CLOCAL)
421 * Block waiting for the carrier detect and the line to become
422 * free (i.e., not in use by the callout). While we are in
423 * this loop, port->count is dropped by one, so that
424 * mxser_close() knows when to free things. We restore it upon
425 * exit, either normal or abnormal.
428 add_wait_queue(&port->open_wait, &wait);
430 spin_lock_irqsave(&port->slock, flags);
431 if (!tty_hung_up_p(filp))
433 spin_unlock_irqrestore(&port->slock, flags);
434 port->blocked_open++;
436 spin_lock_irqsave(&port->slock, flags);
437 outb(inb(port->ioaddr + UART_MCR) |
438 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
439 spin_unlock_irqrestore(&port->slock, flags);
440 set_current_state(TASK_INTERRUPTIBLE);
441 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
442 if (port->flags & ASYNC_HUP_NOTIFY)
445 retval = -ERESTARTSYS;
448 if (!(port->flags & ASYNC_CLOSING) &&
450 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
452 if (signal_pending(current)) {
453 retval = -ERESTARTSYS;
458 set_current_state(TASK_RUNNING);
459 remove_wait_queue(&port->open_wait, &wait);
460 if (!tty_hung_up_p(filp))
462 port->blocked_open--;
465 port->flags |= ASYNC_NORMAL_ACTIVE;
469 static int mxser_set_baud(struct mxser_port *info, long newspd)
476 if (!info->tty || !info->tty->termios)
482 if (newspd > info->max_baud)
485 info->realbaud = newspd;
487 quot = (2 * info->baud_base / 269);
489 quot = info->baud_base / newspd;
496 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
497 info->timeout += HZ / 50; /* Add .02 seconds of slop */
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);
505 spin_lock_irqsave(&info->slock, flags);
506 info->MCR &= ~UART_MCR_DTR;
507 outb(info->MCR, info->ioaddr + UART_MCR);
508 spin_unlock_irqrestore(&info->slock, flags);
512 cval = inb(info->ioaddr + UART_LCR);
514 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
516 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
517 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
518 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
525 * This routine is called to set the UART divisor registers to match
526 * the specified baud rate for a serial port.
528 static int mxser_change_speed(struct mxser_port *info,
529 struct termios *old_termios)
531 unsigned cflag, cval, fcr;
533 unsigned char status;
537 if (!info->tty || !info->tty->termios)
539 cflag = info->tty->termios->c_cflag;
543 if (mxser_set_baud_method[info->tty->index] == 0) {
544 baud = tty_get_baud_rate(info->tty);
545 mxser_set_baud(info, baud);
548 /* byte size and parity */
549 switch (cflag & CSIZE) {
564 break; /* too keep GCC shut... */
569 cval |= UART_LCR_PARITY;
570 if (!(cflag & PARODD))
571 cval |= UART_LCR_EPAR;
573 cval |= UART_LCR_SPAR;
575 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
576 if (info->board->chip_flag) {
577 fcr = UART_FCR_ENABLE_FIFO;
578 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
579 SET_MOXA_MUST_FIFO_VALUE(info);
583 fcr = UART_FCR_ENABLE_FIFO;
584 /* following add by Victor Yu. 08-30-2002 */
585 if (info->board->chip_flag) {
586 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
587 SET_MOXA_MUST_FIFO_VALUE(info);
589 /* above add by Victor Yu. 08-30-2002 */
590 switch (info->rx_trigger) {
592 fcr |= UART_FCR_TRIGGER_1;
595 fcr |= UART_FCR_TRIGGER_4;
598 fcr |= UART_FCR_TRIGGER_8;
601 fcr |= UART_FCR_TRIGGER_14;
607 /* CTS flow control flag and modem status interrupts */
608 info->IER &= ~UART_IER_MSI;
609 info->MCR &= ~UART_MCR_AFE;
610 if (cflag & CRTSCTS) {
611 info->flags |= ASYNC_CTS_FLOW;
612 info->IER |= UART_IER_MSI;
613 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
614 info->MCR |= UART_MCR_AFE;
615 /* status = mxser_get_msr(info->ioaddr, 0, info->port); */
619 status = inb(baseaddr + UART_MSR);
620 restore_flags(flags);
622 /* mxser_check_modem_status(info, status); */
624 /* status = mxser_get_msr(info->ioaddr, 0, info->port); */
625 /* MX_LOCK(&info->slock); */
626 status = inb(info->ioaddr + UART_MSR);
627 /* MX_UNLOCK(&info->slock); */
628 if (info->tty->hw_stopped) {
629 if (status & UART_MSR_CTS) {
630 info->tty->hw_stopped = 0;
631 if (info->type != PORT_16550A &&
632 !info->board->chip_flag) {
633 outb(info->IER & ~UART_IER_THRI,
636 info->IER |= UART_IER_THRI;
637 outb(info->IER, info->ioaddr +
640 set_bit(MXSER_EVENT_TXLOW, &info->event);
641 schedule_work(&info->tqueue); }
643 if (!(status & UART_MSR_CTS)) {
644 info->tty->hw_stopped = 1;
645 if ((info->type != PORT_16550A) &&
646 (!info->board->chip_flag)) {
647 info->IER &= ~UART_IER_THRI;
648 outb(info->IER, info->ioaddr +
655 info->flags &= ~ASYNC_CTS_FLOW;
657 outb(info->MCR, info->ioaddr + UART_MCR);
658 if (cflag & CLOCAL) {
659 info->flags &= ~ASYNC_CHECK_CD;
661 info->flags |= ASYNC_CHECK_CD;
662 info->IER |= UART_IER_MSI;
664 outb(info->IER, info->ioaddr + UART_IER);
667 * Set up parity check flag
669 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
670 if (I_INPCK(info->tty))
671 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
672 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
673 info->read_status_mask |= UART_LSR_BI;
675 info->ignore_status_mask = 0;
677 if (I_IGNBRK(info->tty)) {
678 info->ignore_status_mask |= UART_LSR_BI;
679 info->read_status_mask |= UART_LSR_BI;
681 * If we're ignore parity and break indicators, ignore
682 * overruns too. (For real raw support).
684 if (I_IGNPAR(info->tty)) {
685 info->ignore_status_mask |=
689 info->read_status_mask |=
695 /* following add by Victor Yu. 09-02-2002 */
696 if (info->board->chip_flag) {
697 spin_lock_irqsave(&info->slock, flags);
698 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
699 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
700 if (I_IXON(info->tty)) {
701 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
703 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
705 if (I_IXOFF(info->tty)) {
706 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
708 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
711 if ( I_IXANY(info->tty) ) {
712 info->MCR |= MOXA_MUST_MCR_XON_ANY;
713 ENABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
715 info->MCR &= ~MOXA_MUST_MCR_XON_ANY;
716 DISABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
719 spin_unlock_irqrestore(&info->slock, flags);
721 /* above add by Victor Yu. 09-02-2002 */
724 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
725 outb(cval, info->ioaddr + UART_LCR);
730 static void mxser_check_modem_status(struct mxser_port *port, int status)
732 /* update input line counters */
733 if (status & UART_MSR_TERI)
735 if (status & UART_MSR_DDSR)
737 if (status & UART_MSR_DDCD)
739 if (status & UART_MSR_DCTS)
741 port->mon_data.modem_status = status;
742 wake_up_interruptible(&port->delta_msr_wait);
744 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
745 if (status & UART_MSR_DCD)
746 wake_up_interruptible(&port->open_wait);
747 schedule_work(&port->tqueue);
750 if (port->flags & ASYNC_CTS_FLOW) {
751 if (port->tty->hw_stopped) {
752 if (status & UART_MSR_CTS) {
753 port->tty->hw_stopped = 0;
755 if ((port->type != PORT_16550A) &&
756 (!port->board->chip_flag)) {
757 outb(port->IER & ~UART_IER_THRI,
758 port->ioaddr + UART_IER);
759 port->IER |= UART_IER_THRI;
760 outb(port->IER, port->ioaddr +
763 set_bit(MXSER_EVENT_TXLOW, &port->event);
764 schedule_work(&port->tqueue);
767 if (!(status & UART_MSR_CTS)) {
768 port->tty->hw_stopped = 1;
769 if (port->type != PORT_16550A &&
770 !port->board->chip_flag) {
771 port->IER &= ~UART_IER_THRI;
772 outb(port->IER, port->ioaddr +
780 static int mxser_startup(struct mxser_port *info)
785 page = __get_free_page(GFP_KERNEL);
789 spin_lock_irqsave(&info->slock, flags);
791 if (info->flags & ASYNC_INITIALIZED) {
793 spin_unlock_irqrestore(&info->slock, flags);
797 if (!info->ioaddr || !info->type) {
799 set_bit(TTY_IO_ERROR, &info->tty->flags);
801 spin_unlock_irqrestore(&info->slock, flags);
807 info->xmit_buf = (unsigned char *) page;
810 * Clear the FIFO buffers and disable them
811 * (they will be reenabled in mxser_change_speed())
813 if (info->board->chip_flag)
814 outb((UART_FCR_CLEAR_RCVR |
815 UART_FCR_CLEAR_XMIT |
816 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
818 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
819 info->ioaddr + UART_FCR);
822 * At this point there's no way the LSR could still be 0xFF;
823 * if it is, then bail out, because there's likely no UART
826 if (inb(info->ioaddr + UART_LSR) == 0xff) {
827 spin_unlock_irqrestore(&info->slock, flags);
828 if (capable(CAP_SYS_ADMIN)) {
830 set_bit(TTY_IO_ERROR, &info->tty->flags);
837 * Clear the interrupt registers.
839 (void) inb(info->ioaddr + UART_LSR);
840 (void) inb(info->ioaddr + UART_RX);
841 (void) inb(info->ioaddr + UART_IIR);
842 (void) inb(info->ioaddr + UART_MSR);
845 * Now, initialize the UART
847 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
848 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
849 outb(info->MCR, info->ioaddr + UART_MCR);
852 * Finally, enable interrupts
854 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
855 /* info->IER = UART_IER_RLSI | UART_IER_RDI; */
857 /* following add by Victor Yu. 08-30-2002 */
858 if (info->board->chip_flag)
859 info->IER |= MOXA_MUST_IER_EGDAI;
860 /* above add by Victor Yu. 08-30-2002 */
861 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
864 * And clear the interrupt registers again for luck.
866 (void) inb(info->ioaddr + UART_LSR);
867 (void) inb(info->ioaddr + UART_RX);
868 (void) inb(info->ioaddr + UART_IIR);
869 (void) inb(info->ioaddr + UART_MSR);
872 clear_bit(TTY_IO_ERROR, &info->tty->flags);
873 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
876 * and set the speed of the serial port
878 spin_unlock_irqrestore(&info->slock, flags);
879 mxser_change_speed(info, NULL);
881 info->flags |= ASYNC_INITIALIZED;
886 * This routine will shutdown a serial port; interrupts maybe disabled, and
887 * DTR is dropped if the hangup on close termio flag is on.
889 static void mxser_shutdown(struct mxser_port *info)
893 if (!(info->flags & ASYNC_INITIALIZED))
896 spin_lock_irqsave(&info->slock, flags);
899 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
900 * here so the queue might never be waken up
902 wake_up_interruptible(&info->delta_msr_wait);
905 * Free the IRQ, if necessary
907 if (info->xmit_buf) {
908 free_page((unsigned long) info->xmit_buf);
909 info->xmit_buf = NULL;
913 outb(0x00, info->ioaddr + UART_IER);
915 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
916 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
917 outb(info->MCR, info->ioaddr + UART_MCR);
919 /* clear Rx/Tx FIFO's */
920 /* following add by Victor Yu. 08-30-2002 */
921 if (info->board->chip_flag)
922 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
923 MOXA_MUST_FCR_GDA_MODE_ENABLE,
924 info->ioaddr + UART_FCR);
926 /* above add by Victor Yu. 08-30-2002 */
927 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
928 info->ioaddr + UART_FCR);
930 /* read data port to reset things */
931 (void) inb(info->ioaddr + UART_RX);
934 set_bit(TTY_IO_ERROR, &info->tty->flags);
936 info->flags &= ~ASYNC_INITIALIZED;
938 /* following add by Victor Yu. 09-23-2002 */
939 if (info->board->chip_flag)
940 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
941 /* above add by Victor Yu. 09-23-2002 */
943 spin_unlock_irqrestore(&info->slock, flags);
947 * This routine is called whenever a serial port is opened. It
948 * enables interrupts for a serial port, linking in its async structure into
949 * the IRQ chain. It also performs the serial-specific
950 * initialization for the tty structure.
952 static int mxser_open(struct tty_struct *tty, struct file *filp)
954 struct mxser_port *info;
957 /* initialize driver_data in case something fails */
958 tty->driver_data = NULL;
961 if (line == MXSER_PORTS)
963 if (line < 0 || line > MXSER_PORTS)
965 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
969 tty->driver_data = info;
972 * Start up serial port
975 retval = mxser_startup(info);
979 retval = mxser_block_til_ready(tty, filp, info);
983 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
984 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
985 *tty->termios = info->normal_termios;
987 *tty->termios = info->callout_termios;
988 mxser_change_speed(info, NULL);
991 info->session = process_session(current);
992 info->pgrp = process_group(current);
995 status = mxser_get_msr(info->base, 0, info->port);
996 mxser_check_modem_status(info, status);
999 /* unmark here for very high baud rate (ex. 921600 bps) used */
1000 tty->low_latency = 1;
1005 * This routine is called when the serial port gets closed. First, we
1006 * wait for the last remaining data to be sent. Then, we unlink its
1007 * async structure from the interrupt chain if necessary, and we free
1008 * that IRQ if nothing is left in the chain.
1010 static void mxser_close(struct tty_struct *tty, struct file *filp)
1012 struct mxser_port *info = tty->driver_data;
1014 unsigned long timeout;
1015 unsigned long flags;
1017 if (tty->index == MXSER_PORTS)
1022 spin_lock_irqsave(&info->slock, flags);
1024 if (tty_hung_up_p(filp)) {
1025 spin_unlock_irqrestore(&info->slock, flags);
1028 if ((tty->count == 1) && (info->count != 1)) {
1030 * Uh, oh. tty->count is 1, which means that the tty
1031 * structure will be freed. Info->count should always
1032 * be one in these conditions. If it's greater than
1033 * one, we've got real problems, since it means the
1034 * serial port won't be shutdown.
1036 printk(KERN_ERR "mxser_close: bad serial port count; "
1037 "tty->count is 1, info->count is %d\n", info->count);
1040 if (--info->count < 0) {
1041 printk(KERN_ERR "mxser_close: bad serial port count for "
1042 "ttys%d: %d\n", tty->index, info->count);
1046 spin_unlock_irqrestore(&info->slock, flags);
1049 info->flags |= ASYNC_CLOSING;
1050 spin_unlock_irqrestore(&info->slock, flags);
1052 * Save the termios structure, since this port may have
1053 * separate termios for callout and dialin.
1055 if (info->flags & ASYNC_NORMAL_ACTIVE)
1056 info->normal_termios = *tty->termios;
1058 * Now we wait for the transmit buffer to clear; and we notify
1059 * the line discipline to only process XON/XOFF characters.
1062 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1063 tty_wait_until_sent(tty, info->closing_wait);
1065 * At this point we stop accepting input. To do this, we
1066 * disable the receive line status interrupts, and tell the
1067 * interrupt driver to stop checking the data ready bit in the
1068 * line status register.
1070 info->IER &= ~UART_IER_RLSI;
1071 if (info->board->chip_flag)
1072 info->IER &= ~MOXA_MUST_RECV_ISR;
1074 info->read_status_mask &= ~UART_LSR_DR;
1076 if (info->flags & ASYNC_INITIALIZED) {
1077 outb(info->IER, info->ioaddr + UART_IER);
1079 * Before we drop DTR, make sure the UART transmitter
1080 * has completely drained; this is especially
1081 * important if there is a transmit FIFO!
1083 timeout = jiffies + HZ;
1084 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
1085 schedule_timeout_interruptible(5);
1086 if (time_after(jiffies, timeout))
1090 mxser_shutdown(info);
1092 if (tty->driver->flush_buffer)
1093 tty->driver->flush_buffer(tty);
1095 tty_ldisc_flush(tty);
1100 if (info->blocked_open) {
1101 if (info->close_delay)
1102 schedule_timeout_interruptible(info->close_delay);
1103 wake_up_interruptible(&info->open_wait);
1106 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1107 wake_up_interruptible(&info->close_wait);
1111 static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1114 struct mxser_port *info = tty->driver_data;
1115 unsigned long flags;
1117 if (!info->xmit_buf)
1121 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1122 SERIAL_XMIT_SIZE - info->xmit_head));
1126 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1127 spin_lock_irqsave(&info->slock, flags);
1128 info->xmit_head = (info->xmit_head + c) &
1129 (SERIAL_XMIT_SIZE - 1);
1130 info->xmit_cnt += c;
1131 spin_unlock_irqrestore(&info->slock, flags);
1138 if (info->xmit_cnt && !tty->stopped
1139 /*&& !(info->IER & UART_IER_THRI)*/) {
1140 if (!tty->hw_stopped ||
1141 (info->type == PORT_16550A) ||
1142 (info->board->chip_flag)) {
1143 spin_lock_irqsave(&info->slock, flags);
1144 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1146 info->IER |= UART_IER_THRI;
1147 outb(info->IER, info->ioaddr + UART_IER);
1148 spin_unlock_irqrestore(&info->slock, flags);
1154 static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1156 struct mxser_port *info = tty->driver_data;
1157 unsigned long flags;
1159 if (!info->xmit_buf)
1162 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1165 spin_lock_irqsave(&info->slock, flags);
1166 info->xmit_buf[info->xmit_head++] = ch;
1167 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1169 spin_unlock_irqrestore(&info->slock, flags);
1170 if (!tty->stopped /*&& !(info->IER & UART_IER_THRI)*/) {
1171 if (!tty->hw_stopped ||
1172 (info->type == PORT_16550A) ||
1173 info->board->chip_flag) {
1174 spin_lock_irqsave(&info->slock, flags);
1175 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1176 info->IER |= UART_IER_THRI;
1177 outb(info->IER, info->ioaddr + UART_IER);
1178 spin_unlock_irqrestore(&info->slock, flags);
1184 static void mxser_flush_chars(struct tty_struct *tty)
1186 struct mxser_port *info = tty->driver_data;
1187 unsigned long flags;
1189 if (info->xmit_cnt <= 0 ||
1193 (info->type != PORT_16550A) &&
1194 (!info->board->chip_flag)
1198 spin_lock_irqsave(&info->slock, flags);
1200 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1201 info->IER |= UART_IER_THRI;
1202 outb(info->IER, info->ioaddr + UART_IER);
1204 spin_unlock_irqrestore(&info->slock, flags);
1207 static int mxser_write_room(struct tty_struct *tty)
1209 struct mxser_port *info = tty->driver_data;
1212 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1218 static int mxser_chars_in_buffer(struct tty_struct *tty)
1220 struct mxser_port *info = tty->driver_data;
1221 return info->xmit_cnt;
1224 static void mxser_flush_buffer(struct tty_struct *tty)
1226 struct mxser_port *info = tty->driver_data;
1228 unsigned long flags;
1231 spin_lock_irqsave(&info->slock, flags);
1232 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1234 /* below added by shinhay */
1235 fcr = inb(info->ioaddr + UART_FCR);
1236 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1237 info->ioaddr + UART_FCR);
1238 outb(fcr, info->ioaddr + UART_FCR);
1240 spin_unlock_irqrestore(&info->slock, flags);
1241 /* above added by shinhay */
1247 * ------------------------------------------------------------
1248 * friends of mxser_ioctl()
1249 * ------------------------------------------------------------
1251 static int mxser_get_serial_info(struct mxser_port *info,
1252 struct serial_struct __user *retinfo)
1254 struct serial_struct tmp;
1258 memset(&tmp, 0, sizeof(tmp));
1259 tmp.type = info->type;
1260 tmp.line = info->tty->index;
1261 tmp.port = info->ioaddr;
1262 tmp.irq = info->board->irq;
1263 tmp.flags = info->flags;
1264 tmp.baud_base = info->baud_base;
1265 tmp.close_delay = info->close_delay;
1266 tmp.closing_wait = info->closing_wait;
1267 tmp.custom_divisor = info->custom_divisor;
1269 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1274 static int mxser_set_serial_info(struct mxser_port *info,
1275 struct serial_struct __user *new_info)
1277 struct serial_struct new_serial;
1281 if (!new_info || !info->ioaddr)
1283 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1286 if ((new_serial.irq != info->board->irq) ||
1287 (new_serial.port != info->ioaddr) ||
1288 (new_serial.custom_divisor != info->custom_divisor) ||
1289 (new_serial.baud_base != info->baud_base))
1292 flags = info->flags & ASYNC_SPD_MASK;
1294 if (!capable(CAP_SYS_ADMIN)) {
1295 if ((new_serial.baud_base != info->baud_base) ||
1296 (new_serial.close_delay != info->close_delay) ||
1297 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
1299 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1300 (new_serial.flags & ASYNC_USR_MASK));
1303 * OK, past this point, all the error checking has been done.
1304 * At this point, we start making changes.....
1306 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1307 (new_serial.flags & ASYNC_FLAGS));
1308 info->close_delay = new_serial.close_delay * HZ / 100;
1309 info->closing_wait = new_serial.closing_wait * HZ / 100;
1310 info->tty->low_latency =
1311 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1312 info->tty->low_latency = 0; /* (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0; */
1315 /* added by casper, 3/17/2000, for mouse */
1316 info->type = new_serial.type;
1318 process_txrx_fifo(info);
1320 if (info->flags & ASYNC_INITIALIZED) {
1321 if (flags != (info->flags & ASYNC_SPD_MASK))
1322 mxser_change_speed(info, NULL);
1324 retval = mxser_startup(info);
1330 * mxser_get_lsr_info - get line status register info
1332 * Purpose: Let user call ioctl() to get info when the UART physically
1333 * is emptied. On bus types like RS485, the transmitter must
1334 * release the bus after transmitting. This must be done when
1335 * the transmit shift register is empty, not be done when the
1336 * transmit holding register is empty. This functionality
1337 * allows an RS485 driver to be written in user space.
1339 static int mxser_get_lsr_info(struct mxser_port *info,
1340 unsigned int __user *value)
1342 unsigned char status;
1343 unsigned int result;
1344 unsigned long flags;
1346 spin_lock_irqsave(&info->slock, flags);
1347 status = inb(info->ioaddr + UART_LSR);
1348 spin_unlock_irqrestore(&info->slock, flags);
1349 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1350 return put_user(result, value);
1354 * This routine sends a break character out the serial port.
1356 static void mxser_send_break(struct mxser_port *info, int duration)
1358 unsigned long flags;
1362 set_current_state(TASK_INTERRUPTIBLE);
1363 spin_lock_irqsave(&info->slock, flags);
1364 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1365 info->ioaddr + UART_LCR);
1366 spin_unlock_irqrestore(&info->slock, flags);
1367 schedule_timeout(duration);
1368 spin_lock_irqsave(&info->slock, flags);
1369 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1370 info->ioaddr + UART_LCR);
1371 spin_unlock_irqrestore(&info->slock, flags);
1374 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
1376 struct mxser_port *info = tty->driver_data;
1377 unsigned char control, status;
1378 unsigned long flags;
1381 if (tty->index == MXSER_PORTS)
1382 return -ENOIOCTLCMD;
1383 if (test_bit(TTY_IO_ERROR, &tty->flags))
1386 control = info->MCR;
1388 spin_lock_irqsave(&info->slock, flags);
1389 status = inb(info->ioaddr + UART_MSR);
1390 if (status & UART_MSR_ANY_DELTA)
1391 mxser_check_modem_status(info, status);
1392 spin_unlock_irqrestore(&info->slock, flags);
1393 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1394 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1395 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1396 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1397 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1398 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1401 static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1402 unsigned int set, unsigned int clear)
1404 struct mxser_port *info = tty->driver_data;
1405 unsigned long flags;
1408 if (tty->index == MXSER_PORTS)
1409 return -ENOIOCTLCMD;
1410 if (test_bit(TTY_IO_ERROR, &tty->flags))
1413 spin_lock_irqsave(&info->slock, flags);
1415 if (set & TIOCM_RTS)
1416 info->MCR |= UART_MCR_RTS;
1417 if (set & TIOCM_DTR)
1418 info->MCR |= UART_MCR_DTR;
1420 if (clear & TIOCM_RTS)
1421 info->MCR &= ~UART_MCR_RTS;
1422 if (clear & TIOCM_DTR)
1423 info->MCR &= ~UART_MCR_DTR;
1425 outb(info->MCR, info->ioaddr + UART_MCR);
1426 spin_unlock_irqrestore(&info->slock, flags);
1430 static int mxser_program_mode(int port)
1433 /* unsigned long flags; */
1435 spin_lock(&gm_lock);
1443 /* restore_flags(flags); */
1444 spin_unlock(&gm_lock);
1446 id = inb(port + 1) & 0x1F;
1447 if ((id != C168_ASIC_ID) &&
1448 (id != C104_ASIC_ID) &&
1449 (id != C102_ASIC_ID) &&
1450 (id != CI132_ASIC_ID) &&
1451 (id != CI134_ASIC_ID) &&
1452 (id != CI104J_ASIC_ID))
1454 for (i = 0, j = 0; i < 4; i++) {
1458 } else if ((j == 1) && (n == 1)) {
1469 static void mxser_normal_mode(int port)
1473 outb(0xA5, port + 1);
1474 outb(0x80, port + 3);
1475 outb(12, port + 0); /* 9600 bps */
1477 outb(0x03, port + 3); /* 8 data bits */
1478 outb(0x13, port + 4); /* loop back mode */
1479 for (i = 0; i < 16; i++) {
1481 if ((n & 0x61) == 0x60)
1486 outb(0x00, port + 4);
1489 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1490 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1491 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1492 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1493 #define EN_CCMD 0x000 /* Chip's command register */
1494 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1495 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1496 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1497 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1498 #define EN0_DCFG 0x00E /* Data configuration reg WR */
1499 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1500 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1501 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1502 static int mxser_read_register(int port, unsigned short *regs)
1504 int i, k, value, id;
1507 id = mxser_program_mode(port);
1510 for (i = 0; i < 14; i++) {
1511 k = (i & 0x3F) | 0x180;
1512 for (j = 0x100; j > 0; j >>= 1) {
1513 outb(CHIP_CS, port);
1515 outb(CHIP_CS | CHIP_DO, port);
1516 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1518 outb(CHIP_CS, port);
1519 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1524 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1525 outb(CHIP_CS, port);
1526 outb(CHIP_CS | CHIP_SK, port);
1527 if (inb(port) & CHIP_DI)
1533 mxser_normal_mode(port);
1537 static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1539 struct mxser_port *port;
1545 /* if (copy_to_user(argp, mxsercfg,
1546 sizeof(struct mxser_hwconf) * 4))
1550 case MOXA_GET_MAJOR:
1551 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1555 case MOXA_GET_CUMAJOR:
1556 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1560 case MOXA_CHKPORTENABLE:
1563 for (i = 0; i < MXSER_BOARDS; i++)
1564 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1565 if (mxser_boards[i].ports[j].ioaddr)
1568 return put_user(result, (unsigned long __user *)argp);
1569 case MOXA_GETDATACOUNT:
1570 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1573 case MOXA_GETMSTATUS:
1574 for (i = 0; i < MXSER_BOARDS; i++)
1575 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1576 port = &mxser_boards[i].ports[j];
1579 if (!port->ioaddr) {
1580 GMStatus[i].dcd = 0;
1581 GMStatus[i].dsr = 0;
1582 GMStatus[i].cts = 0;
1586 if (!port->tty || !port->tty->termios)
1588 port->normal_termios.c_cflag;
1591 port->tty->termios->c_cflag;
1593 status = inb(port->ioaddr + UART_MSR);
1594 if (status & 0x80 /*UART_MSR_DCD */ )
1595 GMStatus[i].dcd = 1;
1597 GMStatus[i].dcd = 0;
1599 if (status & 0x20 /*UART_MSR_DSR */ )
1600 GMStatus[i].dsr = 1;
1602 GMStatus[i].dsr = 0;
1605 if (status & 0x10 /*UART_MSR_CTS */ )
1606 GMStatus[i].cts = 1;
1608 GMStatus[i].cts = 0;
1610 if (copy_to_user(argp, GMStatus,
1611 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1614 case MOXA_ASPP_MON_EXT: {
1615 int status, p, shiftbit;
1616 unsigned long opmode;
1617 unsigned cflag, iflag;
1619 for (i = 0; i < MXSER_BOARDS; i++)
1620 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1621 port = &mxser_boards[i].ports[j];
1625 status = mxser_get_msr(port->ioaddr, 0, i);
1626 /* mxser_check_modem_status(port, status); */
1628 if (status & UART_MSR_TERI)
1630 if (status & UART_MSR_DDSR)
1632 if (status & UART_MSR_DDCD)
1634 if (status & UART_MSR_DCTS)
1637 port->mon_data.modem_status = status;
1638 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1639 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1640 mon_data_ext.up_rxcnt[i] =
1641 port->mon_data.up_rxcnt;
1642 mon_data_ext.up_txcnt[i] =
1643 port->mon_data.up_txcnt;
1644 mon_data_ext.modem_status[i] =
1645 port->mon_data.modem_status;
1646 mon_data_ext.baudrate[i] = port->realbaud;
1648 if (!port->tty || !port->tty->termios) {
1649 cflag = port->normal_termios.c_cflag;
1650 iflag = port->normal_termios.c_iflag;
1652 cflag = port->tty->termios->c_cflag;
1653 iflag = port->tty->termios->c_iflag;
1656 mon_data_ext.databits[i] = cflag & CSIZE;
1658 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1660 mon_data_ext.parity[i] =
1661 cflag & (PARENB | PARODD | CMSPAR);
1663 mon_data_ext.flowctrl[i] = 0x00;
1665 if (cflag & CRTSCTS)
1666 mon_data_ext.flowctrl[i] |= 0x03;
1668 if (iflag & (IXON | IXOFF))
1669 mon_data_ext.flowctrl[i] |= 0x0C;
1671 if (port->type == PORT_16550A)
1672 mon_data_ext.fifo[i] = 1;
1674 mon_data_ext.fifo[i] = 0;
1678 opmode = inb(port->opmode_ioaddr) >> shiftbit;
1679 opmode &= OP_MODE_MASK;
1681 mon_data_ext.iftype[i] = opmode;
1684 if (copy_to_user(argp, &mon_data_ext,
1685 sizeof(mon_data_ext)))
1691 return -ENOIOCTLCMD;
1696 static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1697 unsigned int cmd, unsigned long arg)
1699 struct mxser_port *info = tty->driver_data;
1700 struct async_icount cprev, cnow; /* kernel counter temps */
1701 struct serial_icounter_struct __user *p_cuser;
1702 unsigned long templ;
1703 unsigned long flags;
1704 void __user *argp = (void __user *)arg;
1707 if (tty->index == MXSER_PORTS)
1708 return mxser_ioctl_special(cmd, argp);
1710 /* following add by Victor Yu. 01-05-2004 */
1711 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1713 unsigned long opmode;
1714 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1716 unsigned char val, mask;
1719 if (cmd == MOXA_SET_OP_MODE) {
1720 if (get_user(opmode, (int __user *) argp))
1722 if (opmode != RS232_MODE &&
1723 opmode != RS485_2WIRE_MODE &&
1724 opmode != RS422_MODE &&
1725 opmode != RS485_4WIRE_MODE)
1729 val = inb(info->opmode_ioaddr);
1731 val |= (opmode << shiftbit);
1732 outb(val, info->opmode_ioaddr);
1735 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1736 opmode &= OP_MODE_MASK;
1737 if (copy_to_user(argp, &opmode, sizeof(int)))
1742 /* above add by Victor Yu. 01-05-2004 */
1744 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1745 test_bit(TTY_IO_ERROR, &tty->flags))
1749 case TCSBRK: /* SVID version: non-zero arg --> no break */
1750 retval = tty_check_change(tty);
1753 tty_wait_until_sent(tty, 0);
1755 mxser_send_break(info, HZ / 4); /* 1/4 second */
1757 case TCSBRKP: /* support for POSIX tcsendbreak() */
1758 retval = tty_check_change(tty);
1761 tty_wait_until_sent(tty, 0);
1762 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1765 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1767 if (get_user(templ, (unsigned long __user *) argp))
1770 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1773 return mxser_get_serial_info(info, argp);
1775 return mxser_set_serial_info(info, argp);
1776 case TIOCSERGETLSR: /* Get line status register */
1777 return mxser_get_lsr_info(info, argp);
1779 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1780 * - mask passed in arg for lines of interest
1781 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1782 * Caller should use TIOCGICOUNT to see which one it was
1785 DECLARE_WAITQUEUE(wait, current);
1787 spin_lock_irqsave(&info->slock, flags);
1788 cprev = info->icount; /* note the counters on entry */
1789 spin_unlock_irqrestore(&info->slock, flags);
1791 add_wait_queue(&info->delta_msr_wait, &wait);
1793 spin_lock_irqsave(&info->slock, flags);
1794 cnow = info->icount; /* atomic copy */
1795 spin_unlock_irqrestore(&info->slock, flags);
1797 set_current_state(TASK_INTERRUPTIBLE);
1798 if (((arg & TIOCM_RNG) &&
1799 (cnow.rng != cprev.rng)) ||
1800 ((arg & TIOCM_DSR) &&
1801 (cnow.dsr != cprev.dsr)) ||
1802 ((arg & TIOCM_CD) &&
1803 (cnow.dcd != cprev.dcd)) ||
1804 ((arg & TIOCM_CTS) &&
1805 (cnow.cts != cprev.cts))) {
1809 /* see if a signal did it */
1810 if (signal_pending(current)) {
1816 current->state = TASK_RUNNING;
1817 remove_wait_queue(&info->delta_msr_wait, &wait);
1822 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1823 * Return: write counters to the user passed counter struct
1824 * NB: both 1->0 and 0->1 transitions are counted except for
1825 * RI where only 0->1 is counted.
1828 spin_lock_irqsave(&info->slock, flags);
1829 cnow = info->icount;
1830 spin_unlock_irqrestore(&info->slock, flags);
1832 /* modified by casper 1/11/2000 */
1833 if (put_user(cnow.frame, &p_cuser->frame))
1835 if (put_user(cnow.brk, &p_cuser->brk))
1837 if (put_user(cnow.overrun, &p_cuser->overrun))
1839 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1841 if (put_user(cnow.parity, &p_cuser->parity))
1843 if (put_user(cnow.rx, &p_cuser->rx))
1845 if (put_user(cnow.tx, &p_cuser->tx))
1847 put_user(cnow.cts, &p_cuser->cts);
1848 put_user(cnow.dsr, &p_cuser->dsr);
1849 put_user(cnow.rng, &p_cuser->rng);
1850 put_user(cnow.dcd, &p_cuser->dcd);
1852 case MOXA_HighSpeedOn:
1853 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1854 case MOXA_SDS_RSTICOUNTER:
1855 info->mon_data.rxcnt = 0;
1856 info->mon_data.txcnt = 0;
1858 /* (above) added by James. */
1859 case MOXA_ASPP_SETBAUD:{
1861 if (get_user(baud, (long __user *)argp))
1863 mxser_set_baud(info, baud);
1866 case MOXA_ASPP_GETBAUD:
1867 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1872 case MOXA_ASPP_OQUEUE:{
1875 len = mxser_chars_in_buffer(tty);
1877 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1879 len += (lsr ? 0 : 1);
1881 if (copy_to_user(argp, &len, sizeof(int)))
1886 case MOXA_ASPP_MON: {
1889 /* info->mon_data.ser_param = tty->termios->c_cflag; */
1891 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1892 mxser_check_modem_status(info, status);
1894 mcr = inb(info->ioaddr + UART_MCR);
1895 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1896 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1898 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1900 if (mcr & MOXA_MUST_MCR_TX_XON)
1901 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1903 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1905 if (info->tty->hw_stopped)
1906 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1908 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1910 if (copy_to_user(argp, &info->mon_data,
1911 sizeof(struct mxser_mon)))
1916 case MOXA_ASPP_LSTATUS: {
1917 if (copy_to_user(argp, &info->err_shadow,
1918 sizeof(unsigned char)))
1921 info->err_shadow = 0;
1924 case MOXA_SET_BAUD_METHOD: {
1927 if (get_user(method, (int __user *)argp))
1929 mxser_set_baud_method[tty->index] = method;
1930 if (copy_to_user(argp, &method, sizeof(int)))
1936 return -ENOIOCTLCMD;
1941 static void mxser_stoprx(struct tty_struct *tty)
1943 struct mxser_port *info = tty->driver_data;
1945 info->ldisc_stop_rx = 1;
1947 /* following add by Victor Yu. 09-02-2002 */
1948 if (info->board->chip_flag) {
1949 info->IER &= ~MOXA_MUST_RECV_ISR;
1950 outb(info->IER, info->ioaddr + UART_IER);
1952 info->x_char = STOP_CHAR(tty);
1953 outb(0, info->ioaddr + UART_IER);
1954 info->IER |= UART_IER_THRI;
1955 outb(info->IER, info->ioaddr + UART_IER);
1959 if (info->tty->termios->c_cflag & CRTSCTS) {
1960 info->MCR &= ~UART_MCR_RTS;
1961 outb(info->MCR, info->ioaddr + UART_MCR);
1966 * This routine is called by the upper-layer tty layer to signal that
1967 * incoming characters should be throttled.
1969 static void mxser_throttle(struct tty_struct *tty)
1974 static void mxser_unthrottle(struct tty_struct *tty)
1976 struct mxser_port *info = tty->driver_data;
1979 info->ldisc_stop_rx = 0;
1984 /* following add by Victor Yu. 09-02-2002 */
1985 if (info->board->chip_flag) {
1986 info->IER |= MOXA_MUST_RECV_ISR;
1987 outb(info->IER, info->ioaddr + UART_IER);
1989 info->x_char = START_CHAR(tty);
1990 outb(0, info->ioaddr + UART_IER);
1991 info->IER |= UART_IER_THRI;
1992 outb(info->IER, info->ioaddr + UART_IER);
1997 if (info->tty->termios->c_cflag & CRTSCTS) {
1998 info->MCR |= UART_MCR_RTS;
1999 outb(info->MCR, info->ioaddr + UART_MCR);
2004 * mxser_stop() and mxser_start()
2006 * This routines are called before setting or resetting tty->stopped.
2007 * They enable or disable transmitter interrupts, as necessary.
2009 static void mxser_stop(struct tty_struct *tty)
2011 struct mxser_port *info = tty->driver_data;
2012 unsigned long flags;
2014 spin_lock_irqsave(&info->slock, flags);
2015 if (info->IER & UART_IER_THRI) {
2016 info->IER &= ~UART_IER_THRI;
2017 outb(info->IER, info->ioaddr + UART_IER);
2019 spin_unlock_irqrestore(&info->slock, flags);
2022 static void mxser_start(struct tty_struct *tty)
2024 struct mxser_port *info = tty->driver_data;
2025 unsigned long flags;
2027 spin_lock_irqsave(&info->slock, flags);
2028 if (info->xmit_cnt && info->xmit_buf
2029 /* && !(info->IER & UART_IER_THRI) */) {
2030 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
2031 info->IER |= UART_IER_THRI;
2032 outb(info->IER, info->ioaddr + UART_IER);
2034 spin_unlock_irqrestore(&info->slock, flags);
2037 static void mxser_set_termios(struct tty_struct *tty, struct termios *old_termios)
2039 struct mxser_port *info = tty->driver_data;
2040 unsigned long flags;
2042 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
2043 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
2045 mxser_change_speed(info, old_termios);
2047 if ((old_termios->c_cflag & CRTSCTS) &&
2048 !(tty->termios->c_cflag & CRTSCTS)) {
2049 tty->hw_stopped = 0;
2054 /* Handle sw stopped */
2055 if ((old_termios->c_iflag & IXON) &&
2056 !(tty->termios->c_iflag & IXON)) {
2059 /* following add by Victor Yu. 09-02-2002 */
2060 if (info->board->chip_flag) {
2061 spin_lock_irqsave(&info->slock, flags);
2062 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2063 spin_unlock_irqrestore(&info->slock, flags);
2065 /* above add by Victor Yu. 09-02-2002 */
2072 * mxser_wait_until_sent() --- wait until the transmitter is empty
2074 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2076 struct mxser_port *info = tty->driver_data;
2077 unsigned long orig_jiffies, char_time;
2080 if (info->type == PORT_UNKNOWN)
2083 if (info->xmit_fifo_size == 0)
2084 return; /* Just in case.... */
2086 orig_jiffies = jiffies;
2088 * Set the check interval to be 1/5 of the estimated time to
2089 * send a single character, and make it at least 1. The check
2090 * interval should also be less than the timeout.
2092 * Note: we have to use pretty tight timings here to satisfy
2095 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2096 char_time = char_time / 5;
2099 if (timeout && timeout < char_time)
2100 char_time = timeout;
2102 * If the transmitter hasn't cleared in twice the approximate
2103 * amount of time to send the entire FIFO, it probably won't
2104 * ever clear. This assumes the UART isn't doing flow
2105 * control, which is currently the case. Hence, if it ever
2106 * takes longer than info->timeout, this is probably due to a
2107 * UART bug of some kind. So, we clamp the timeout parameter at
2110 if (!timeout || timeout > 2 * info->timeout)
2111 timeout = 2 * info->timeout;
2112 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2113 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2114 timeout, char_time);
2115 printk("jiff=%lu...", jiffies);
2117 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
2118 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2119 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2121 schedule_timeout_interruptible(char_time);
2122 if (signal_pending(current))
2124 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2127 set_current_state(TASK_RUNNING);
2129 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2130 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2136 * This routine is called by tty_hangup() when a hangup is signaled.
2138 void mxser_hangup(struct tty_struct *tty)
2140 struct mxser_port *info = tty->driver_data;
2142 mxser_flush_buffer(tty);
2143 mxser_shutdown(info);
2146 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2148 wake_up_interruptible(&info->open_wait);
2152 /* added by James 03-12-2004. */
2154 * mxser_rs_break() --- routine which turns the break handling on or off
2156 static void mxser_rs_break(struct tty_struct *tty, int break_state)
2158 struct mxser_port *info = tty->driver_data;
2159 unsigned long flags;
2161 spin_lock_irqsave(&info->slock, flags);
2162 if (break_state == -1)
2163 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2164 info->ioaddr + UART_LCR);
2166 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2167 info->ioaddr + UART_LCR);
2168 spin_unlock_irqrestore(&info->slock, flags);
2171 /* (above) added by James. */
2173 static void mxser_receive_chars(struct mxser_port *port, int *status)
2175 struct tty_struct *tty = port->tty;
2176 unsigned char ch, gdl;
2181 unsigned long flags;
2183 spin_lock_irqsave(&port->slock, flags);
2185 recv_room = tty->receive_room;
2186 if ((recv_room == 0) && (!port->ldisc_stop_rx)) {
2187 /* mxser_throttle(tty); */
2192 /* following add by Victor Yu. 09-02-2002 */
2193 if (port->board->chip_flag != MOXA_OTHER_UART) {
2195 if (*status & UART_LSR_SPECIAL)
2197 /* following add by Victor Yu. 02-11-2004 */
2198 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
2199 (*status & MOXA_MUST_LSR_RERR))
2201 /* above add by Victor Yu. 02-14-2004 */
2202 if (*status & MOXA_MUST_LSR_RERR)
2205 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
2207 /* add by Victor Yu. 02-11-2004 */
2208 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
2209 gdl &= MOXA_MUST_GDL_MASK;
2210 if (gdl >= recv_room) {
2211 if (!port->ldisc_stop_rx) {
2212 /* mxser_throttle(tty); */
2218 ch = inb(port->ioaddr + UART_RX);
2219 tty_insert_flip_char(tty, ch, 0);
2225 /* above add by Victor Yu. 09-02-2002 */
2231 ch = inb(port->ioaddr + UART_RX);
2232 /* following add by Victor Yu. 09-02-2002 */
2233 if (port->board->chip_flag && (*status & UART_LSR_OE)
2234 /*&& !(*status&UART_LSR_DR) */)
2235 outb(0x23, port->ioaddr + UART_FCR);
2236 *status &= port->read_status_mask;
2237 /* above add by Victor Yu. 09-02-2002 */
2238 if (*status & port->ignore_status_mask) {
2239 if (++ignored > 100)
2243 if (*status & UART_LSR_SPECIAL) {
2244 if (*status & UART_LSR_BI) {
2246 /* added by casper 1/11/2000 */
2249 if (port->flags & ASYNC_SAK)
2251 } else if (*status & UART_LSR_PE) {
2253 /* added by casper 1/11/2000 */
2254 port->icount.parity++;
2255 } else if (*status & UART_LSR_FE) {
2257 /* added by casper 1/11/2000 */
2258 port->icount.frame++;
2259 } else if (*status & UART_LSR_OE) {
2261 /* added by casper 1/11/2000 */
2262 port->icount.overrun++;
2265 tty_insert_flip_char(tty, ch, flag);
2267 if (cnt >= recv_room) {
2268 if (!port->ldisc_stop_rx) {
2269 /* mxser_throttle(tty); */
2277 /* following add by Victor Yu. 09-02-2002 */
2278 if (port->board->chip_flag)
2281 /* mask by Victor Yu. 09-02-2002
2282 *status = inb(port->ioaddr + UART_LSR) & port->read_status_mask;
2284 /* following add by Victor Yu. 09-02-2002 */
2285 *status = inb(port->ioaddr + UART_LSR);
2286 /* above add by Victor Yu. 09-02-2002 */
2287 } while (*status & UART_LSR_DR);
2289 end_intr: /* add by Victor Yu. 09-02-2002 */
2290 mxvar_log.rxcnt[port->tty->index] += cnt;
2291 port->mon_data.rxcnt += cnt;
2292 port->mon_data.up_rxcnt += cnt;
2293 spin_unlock_irqrestore(&port->slock, flags);
2295 tty_flip_buffer_push(tty);
2298 static void mxser_transmit_chars(struct mxser_port *port)
2301 unsigned long flags;
2303 spin_lock_irqsave(&port->slock, flags);
2306 outb(port->x_char, port->ioaddr + UART_TX);
2308 mxvar_log.txcnt[port->tty->index]++;
2309 port->mon_data.txcnt++;
2310 port->mon_data.up_txcnt++;
2312 /* added by casper 1/11/2000 */
2317 if (port->xmit_buf == 0)
2320 if ((port->xmit_cnt <= 0) || port->tty->stopped ||
2321 (port->tty->hw_stopped &&
2322 (port->type != PORT_16550A) &&
2323 (!port->board->chip_flag))) {
2324 port->IER &= ~UART_IER_THRI;
2325 outb(port->IER, port->ioaddr + UART_IER);
2329 cnt = port->xmit_cnt;
2330 count = port->xmit_fifo_size;
2332 outb(port->xmit_buf[port->xmit_tail++],
2333 port->ioaddr + UART_TX);
2334 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2335 if (--port->xmit_cnt <= 0)
2337 } while (--count > 0);
2338 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
2340 /* added by James 03-12-2004. */
2341 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2342 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2344 /* added by casper 1/11/2000 */
2345 port->icount.tx += (cnt - port->xmit_cnt);
2347 if (port->xmit_cnt < WAKEUP_CHARS) {
2348 set_bit(MXSER_EVENT_TXLOW, &port->event);
2349 schedule_work(&port->tqueue);
2351 if (port->xmit_cnt <= 0) {
2352 port->IER &= ~UART_IER_THRI;
2353 outb(port->IER, port->ioaddr + UART_IER);
2356 spin_unlock_irqrestore(&port->slock, flags);
2360 * This is the serial driver's generic interrupt routine
2362 static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
2365 struct mxser_board *brd = NULL;
2366 struct mxser_port *port;
2367 int max, irqbits, bits, msr;
2368 int pass_counter = 0;
2369 unsigned int int_cnt;
2370 int handled = IRQ_NONE;
2372 /* spin_lock(&gm_lock); */
2374 for (i = 0; i < MXSER_BOARDS; i++)
2375 if (dev_id == &mxser_boards[i]) {
2380 if (i == MXSER_BOARDS)
2384 max = brd->info->nports;
2386 irqbits = inb(brd->vector) & brd->vector_mask;
2387 if (irqbits == brd->vector_mask)
2390 handled = IRQ_HANDLED;
2391 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2392 if (irqbits == brd->vector_mask)
2396 port = &brd->ports[i];
2400 /* following add by Victor Yu. 09-13-2002 */
2401 iir = inb(port->ioaddr + UART_IIR);
2402 if (iir & UART_IIR_NO_INT)
2404 iir &= MOXA_MUST_IIR_MASK;
2406 status = inb(port->ioaddr + UART_LSR);
2407 outb(0x27, port->ioaddr + UART_FCR);
2408 inb(port->ioaddr + UART_MSR);
2411 /* above add by Victor Yu. 09-13-2002 */
2413 /* following add by Victor Yu. 09-02-2002 */
2414 status = inb(port->ioaddr + UART_LSR);
2416 if (status & UART_LSR_PE)
2417 port->err_shadow |= NPPI_NOTIFY_PARITY;
2418 if (status & UART_LSR_FE)
2419 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2420 if (status & UART_LSR_OE)
2422 NPPI_NOTIFY_HW_OVERRUN;
2423 if (status & UART_LSR_BI)
2424 port->err_shadow |= NPPI_NOTIFY_BREAK;
2426 if (port->board->chip_flag) {
2428 if ( (status & 0x02) && !(status & 0x01) ) {
2429 outb(port->ioaddr+UART_FCR, 0x23);
2433 if (iir == MOXA_MUST_IIR_GDA ||
2434 iir == MOXA_MUST_IIR_RDA ||
2435 iir == MOXA_MUST_IIR_RTO ||
2436 iir == MOXA_MUST_IIR_LSR)
2437 mxser_receive_chars(port,
2441 /* above add by Victor Yu. 09-02-2002 */
2443 status &= port->read_status_mask;
2444 if (status & UART_LSR_DR)
2445 mxser_receive_chars(port,
2448 msr = inb(port->ioaddr + UART_MSR);
2449 if (msr & UART_MSR_ANY_DELTA)
2450 mxser_check_modem_status(port, msr);
2452 /* following add by Victor Yu. 09-13-2002 */
2453 if (port->board->chip_flag) {
2454 if (iir == 0x02 && (status &
2456 mxser_transmit_chars(port);
2458 /* above add by Victor Yu. 09-13-2002 */
2460 if (status & UART_LSR_THRE)
2461 mxser_transmit_chars(port);
2463 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2465 if (pass_counter++ > MXSER_ISR_PASS_LIMIT)
2466 break; /* Prevent infinite loops */
2470 /* spin_unlock(&gm_lock); */
2474 static const struct tty_operations mxser_ops = {
2476 .close = mxser_close,
2477 .write = mxser_write,
2478 .put_char = mxser_put_char,
2479 .flush_chars = mxser_flush_chars,
2480 .write_room = mxser_write_room,
2481 .chars_in_buffer = mxser_chars_in_buffer,
2482 .flush_buffer = mxser_flush_buffer,
2483 .ioctl = mxser_ioctl,
2484 .throttle = mxser_throttle,
2485 .unthrottle = mxser_unthrottle,
2486 .set_termios = mxser_set_termios,
2488 .start = mxser_start,
2489 .hangup = mxser_hangup,
2490 .break_ctl = mxser_rs_break,
2491 .wait_until_sent = mxser_wait_until_sent,
2492 .tiocmget = mxser_tiocmget,
2493 .tiocmset = mxser_tiocmset,
2497 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2500 static void mxser_release_res(struct mxser_board *brd, unsigned int irq)
2502 struct pci_dev *pdev = brd->pdev;
2505 free_irq(brd->irq, brd);
2506 if (pdev != NULL) { /* PCI */
2507 pci_release_region(pdev, 2);
2508 pci_release_region(pdev, 3);
2511 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2512 release_region(brd->vector, 1);
2516 static int __devinit mxser_initbrd(struct mxser_board *brd)
2518 struct mxser_port *info;
2522 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
2524 for (i = 0; i < brd->info->nports; i++) {
2525 info = &brd->ports[i];
2528 info->ldisc_stop_rx = 0;
2530 /* Enhance mode enabled here */
2531 if (brd->chip_flag != MOXA_OTHER_UART)
2532 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
2534 info->flags = ASYNC_SHARE_IRQ;
2535 info->type = brd->uart_type;
2537 process_txrx_fifo(info);
2539 info->custom_divisor = info->baud_base * 16;
2540 info->close_delay = 5 * HZ / 10;
2541 info->closing_wait = 30 * HZ;
2542 INIT_WORK(&info->tqueue, mxser_do_softint, info);
2543 info->normal_termios = mxvar_sdriver->init_termios;
2544 init_waitqueue_head(&info->open_wait);
2545 init_waitqueue_head(&info->close_wait);
2546 init_waitqueue_head(&info->delta_msr_wait);
2547 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2548 info->err_shadow = 0;
2549 spin_lock_init(&info->slock);
2551 /* before set INT ISR, disable all int */
2552 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2553 info->ioaddr + UART_IER);
2556 * Allocate the IRQ if necessary
2559 retval = request_irq(brd->irq, mxser_interrupt,
2560 (brd->ports[0].flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED :
2561 IRQF_DISABLED, "mxser", brd);
2563 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2564 "conflict with another device.\n",
2565 brd->info->name, brd->irq);
2566 /* We hold resources, we need to release them. */
2567 mxser_release_res(brd, 0);
2573 static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
2576 unsigned short regs[16], irq;
2577 unsigned char scratch, scratch2;
2579 brd->chip_flag = MOXA_OTHER_UART;
2581 id = mxser_read_register(cap, regs);
2584 brd->info = &mxser_cards[0];
2587 brd->info = &mxser_cards[1];
2589 case CI104J_ASIC_ID:
2590 brd->info = &mxser_cards[2];
2593 brd->info = &mxser_cards[5];
2596 brd->info = &mxser_cards[6];
2599 brd->info = &mxser_cards[7];
2606 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2607 Flag-hack checks if configuration should be read as 2-port here. */
2608 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
2609 irq = regs[9] & 0xF000;
2610 irq = irq | (irq >> 4);
2611 if (irq != (regs[9] & 0xFF00))
2612 return MXSER_ERR_IRQ_CONFLIT;
2613 } else if (brd->info->nports == 4) {
2614 irq = regs[9] & 0xF000;
2615 irq = irq | (irq >> 4);
2616 irq = irq | (irq >> 8);
2618 return MXSER_ERR_IRQ_CONFLIT;
2619 } else if (brd->info->nports == 8) {
2620 irq = regs[9] & 0xF000;
2621 irq = irq | (irq >> 4);
2622 irq = irq | (irq >> 8);
2623 if ((irq != regs[9]) || (irq != regs[10]))
2624 return MXSER_ERR_IRQ_CONFLIT;
2628 return MXSER_ERR_IRQ;
2629 brd->irq = ((int)(irq & 0xF000) >> 12);
2630 for (i = 0; i < 8; i++)
2631 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
2632 if ((regs[12] & 0x80) == 0)
2633 return MXSER_ERR_VECTOR;
2634 brd->vector = (int)regs[11]; /* interrupt vector */
2636 brd->vector_mask = 0x00FF;
2638 brd->vector_mask = 0x000F;
2639 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2640 if (regs[12] & bits) {
2641 brd->ports[i].baud_base = 921600;
2642 brd->ports[i].max_baud = 921600; /* add by Victor Yu. 09-04-2002 */
2644 brd->ports[i].baud_base = 115200;
2645 brd->ports[i].max_baud = 115200; /* add by Victor Yu. 09-04-2002 */
2648 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2649 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2650 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2651 outb(scratch2, cap + UART_LCR);
2652 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2653 scratch = inb(cap + UART_IIR);
2656 brd->uart_type = PORT_16550A;
2658 brd->uart_type = PORT_16450;
2659 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2661 return MXSER_ERR_IOADDR;
2662 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2663 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2664 return MXSER_ERR_VECTOR;
2666 return brd->info->nports;
2669 static int __init mxser_get_PCI_conf(const struct pci_device_id *ent,
2670 struct mxser_board *brd, struct pci_dev *pdev)
2673 unsigned long ioaddress;
2677 brd->info = &mxser_cards[ent->driver_data];
2678 ioaddress = pci_resource_start(pdev, 2);
2679 retval = pci_request_region(pdev, 2, "mxser(IO)");
2683 for (i = 0; i < brd->info->nports; i++)
2684 brd->ports[i].ioaddr = ioaddress + 8 * i;
2687 ioaddress = pci_resource_start(pdev, 3);
2688 retval = pci_request_region(pdev, 3, "mxser(vector)");
2691 brd->vector = ioaddress;
2694 brd->irq = pdev->irq;
2696 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2697 brd->uart_type = PORT_16550A;
2698 brd->vector_mask = 0;
2700 for (i = 0; i < brd->info->nports; i++) {
2701 for (j = 0; j < UART_INFO_NUM; j++) {
2702 if (Gpci_uart_info[j].type == brd->chip_flag) {
2703 brd->ports[i].max_baud =
2704 Gpci_uart_info[j].max_baud;
2706 /* exception....CP-102 */
2707 if (brd->info->flags & MXSER_HIGHBAUD)
2708 brd->ports[i].max_baud = 921600;
2714 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2715 for (i = 0; i < brd->info->nports; i++) {
2717 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2719 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
2721 outb(0, ioaddress + 4); /* default set to RS232 mode */
2722 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
2725 for (i = 0; i < brd->info->nports; i++) {
2726 brd->vector_mask |= (1 << i);
2727 brd->ports[i].baud_base = 921600;
2731 pci_release_region(pdev, 2);
2736 static int __init mxser_module_init(void)
2738 struct pci_dev *pdev = NULL;
2739 struct mxser_board *brd;
2741 unsigned int i, m, isaloop;
2744 pr_debug("Loading module mxser ...\n");
2746 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2749 spin_lock_init(&gm_lock);
2751 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2754 /* Initialize the tty_driver structure */
2755 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2756 mxvar_sdriver->name = "ttyM";
2757 mxvar_sdriver->major = ttymajor;
2758 mxvar_sdriver->minor_start = 0;
2759 mxvar_sdriver->num = MXSER_PORTS + 1;
2760 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2761 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2762 mxvar_sdriver->init_termios = tty_std_termios;
2763 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2764 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2765 tty_set_operations(mxvar_sdriver, &mxser_ops);
2766 mxvar_sdriver->ttys = mxvar_tty;
2767 mxvar_sdriver->termios = mxvar_termios;
2768 mxvar_sdriver->termios_locked = mxvar_termios_locked;
2770 retval = tty_register_driver(mxvar_sdriver);
2772 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2780 /* Start finding ISA boards here */
2781 for (isaloop = 0; isaloop < 2; isaloop++)
2782 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2784 cap = mxserBoardCAP[b]; /* predefined */
2786 cap = ioaddr[b]; /* module param */
2791 brd = &mxser_boards[m];
2792 retval = mxser_get_ISA_conf(cap, brd);
2795 printk(KERN_INFO "Found MOXA %s board "
2797 brd->info->name, ioaddr[b]);
2800 if (retval == MXSER_ERR_IRQ)
2801 printk(KERN_ERR "Invalid interrupt "
2802 "number, board not "
2804 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2805 printk(KERN_ERR "Invalid interrupt "
2806 "number, board not "
2808 else if (retval == MXSER_ERR_VECTOR)
2809 printk(KERN_ERR "Invalid interrupt "
2810 "vector, board not "
2812 else if (retval == MXSER_ERR_IOADDR)
2813 printk(KERN_ERR "Invalid I/O address, "
2814 "board not configured\n");
2821 /* mxser_initbrd will hook ISR. */
2822 if (mxser_initbrd(brd) < 0)
2825 for (i = 0; i < brd->info->nports; i++)
2826 tty_register_device(mxvar_sdriver,
2827 m * MXSER_PORTS_PER_BOARD + i, NULL);
2832 /* start finding PCI board here */
2833 n = ARRAY_SIZE(mxser_pcibrds) - 1;
2836 pdev = pci_get_device(mxser_pcibrds[b].vendor,
2837 mxser_pcibrds[b].device, pdev);
2842 printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
2843 mxser_cards[mxser_pcibrds[b].driver_data].name,
2844 pdev->bus->number, PCI_SLOT(pdev->devfn));
2845 if (m >= MXSER_BOARDS)
2847 "Too many Smartio/Industio family boards find "
2848 "(maximum %d), board not configured\n",
2851 if (pci_enable_device(pdev)) {
2852 printk(KERN_ERR "Moxa SmartI/O PCI enable "
2856 brd = &mxser_boards[m];
2858 retval = mxser_get_PCI_conf(&mxser_pcibrds[b],
2861 if (retval == MXSER_ERR_IRQ)
2863 "Invalid interrupt number, "
2864 "board not configured\n");
2865 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2867 "Invalid interrupt number, "
2868 "board not configured\n");
2869 else if (retval == MXSER_ERR_VECTOR)
2871 "Invalid interrupt vector, "
2872 "board not configured\n");
2873 else if (retval == MXSER_ERR_IOADDR)
2875 "Invalid I/O address, "
2876 "board not configured\n");
2879 /* mxser_initbrd will hook ISR. */
2880 if (mxser_initbrd(brd) < 0)
2882 for (i = 0; i < brd->info->nports; i++)
2883 tty_register_device(mxvar_sdriver,
2884 m * MXSER_PORTS_PER_BOARD + i,
2888 /* Keep an extra reference if we succeeded. It will
2889 be returned at unload time */
2899 pr_debug("Done.\n");
2903 tty_unregister_driver(mxvar_sdriver);
2905 put_tty_driver(mxvar_sdriver);
2909 static void __exit mxser_module_exit(void)
2913 pr_debug("Unloading module mxser ...\n");
2915 for (i = 0; i < MXSER_PORTS; i++)
2916 tty_unregister_device(mxvar_sdriver, i);
2917 tty_unregister_driver(mxvar_sdriver);
2918 put_tty_driver(mxvar_sdriver);
2920 for (i = 0; i < MXSER_BOARDS; i++)
2921 if (mxser_boards[i].info != NULL)
2922 mxser_release_res(&mxser_boards[i], 1);
2924 pr_debug("Done.\n");
2927 module_init(mxser_module_init);
2928 module_exit(mxser_module_exit);