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
88 MXSER_BOARD_C168_ISA = 1,
114 static char *mxser_brdname[] = {
132 "Moxa UC7000 Serial",
141 static int mxser_numports[] = {
168 #define UART_TYPE_NUM 2
170 static const unsigned int Gmoxa_uart_id[UART_TYPE_NUM] = {
171 MOXA_MUST_MU150_HWID,
175 /* This is only for PCI */
176 #define UART_INFO_NUM 3
177 struct mxpciuart_info {
188 static const struct mxpciuart_info Gpci_uart_info[UART_INFO_NUM] = {
189 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
190 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
191 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
194 static struct pci_device_id mxser_pcibrds[] = {
195 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C168),
196 .driver_data = MXSER_BOARD_C168_PCI },
197 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C104),
198 .driver_data = MXSER_BOARD_C104_PCI },
199 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132),
200 .driver_data = MXSER_BOARD_CP132 },
201 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP114),
202 .driver_data = MXSER_BOARD_CP114 },
203 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CT114),
204 .driver_data = MXSER_BOARD_CT114 },
205 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102),
206 .driver_data = MXSER_BOARD_CP102 },
207 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104U),
208 .driver_data = MXSER_BOARD_CP104U },
209 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168U),
210 .driver_data = MXSER_BOARD_CP168U },
211 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132U),
212 .driver_data = MXSER_BOARD_CP132U },
213 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP134U),
214 .driver_data = MXSER_BOARD_CP134U },
215 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104JU),
216 .driver_data = MXSER_BOARD_CP104JU },
217 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_RC7000),
218 .driver_data = MXSER_BOARD_RC7000 },
219 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118U),
220 .driver_data = MXSER_BOARD_CP118U },
221 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102UL),
222 .driver_data = MXSER_BOARD_CP102UL },
223 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102U),
224 .driver_data = MXSER_BOARD_CP102U },
225 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118EL),
226 .driver_data = MXSER_BOARD_CP118EL },
227 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168EL),
228 .driver_data = MXSER_BOARD_CP168EL },
229 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104EL),
230 .driver_data = MXSER_BOARD_CP104EL },
233 MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
235 static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
236 static int ttymajor = MXSERMAJOR;
237 static int calloutmajor = MXSERCUMAJOR;
239 /* Variables for insmod */
241 MODULE_AUTHOR("Casper Yang");
242 MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
243 module_param_array(ioaddr, int, NULL, 0);
244 module_param(ttymajor, int, 0);
245 MODULE_LICENSE("GPL");
249 unsigned long rxcnt[MXSER_PORTS];
250 unsigned long txcnt[MXSER_PORTS];
257 unsigned long up_rxcnt;
258 unsigned long up_txcnt;
260 unsigned char hold_reason;
263 struct mxser_mon_ext {
264 unsigned long rx_cnt[32];
265 unsigned long tx_cnt[32];
266 unsigned long up_rxcnt[32];
267 unsigned long up_txcnt[32];
268 int modem_status[32];
282 struct mxser_board *board;
283 struct tty_struct *tty;
285 unsigned long ioaddr;
286 unsigned long opmode_ioaddr;
290 int rx_trigger; /* Rx fifo trigger level */
292 int baud_base; /* max. speed */
294 int type; /* UART type */
295 int flags; /* defined in tty.h */
296 long session; /* Session of opening process */
297 long pgrp; /* pgrp of opening process */
299 int x_char; /* xon/xoff character */
300 int IER; /* Interrupt Enable Register */
301 int MCR; /* Modem control register */
303 unsigned char stop_rx;
304 unsigned char ldisc_stop_rx;
308 unsigned short closing_wait;
309 unsigned char err_shadow;
312 int count; /* # of fd on device */
313 int blocked_open; /* # of blocked opens */
314 struct async_icount icount; /* kernel counters for 4 input interrupts */
317 int read_status_mask;
318 int ignore_status_mask;
320 unsigned char *xmit_buf;
325 struct termios normal_termios;
326 struct termios callout_termios;
328 struct mxser_mon mon_data;
331 struct work_struct tqueue;
332 wait_queue_head_t open_wait;
333 wait_queue_head_t close_wait;
334 wait_queue_head_t delta_msr_wait;
338 struct pci_dev *pdev; /* temporary (until pci probing) */
343 unsigned long vector;
344 unsigned long vector_mask;
349 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
352 struct mxser_mstatus {
360 static struct mxser_mstatus GMStatus[MXSER_PORTS];
362 static int mxserBoardCAP[MXSER_BOARDS] = {
364 /* 0x180, 0x280, 0x200, 0x320 */
367 static struct mxser_board mxser_boards[MXSER_BOARDS];
368 static struct tty_driver *mxvar_sdriver;
369 static struct tty_struct *mxvar_tty[MXSER_PORTS + 1];
370 static struct termios *mxvar_termios[MXSER_PORTS + 1];
371 static struct termios *mxvar_termios_locked[MXSER_PORTS + 1];
372 static struct mxser_log mxvar_log;
373 static int mxvar_diagflag;
374 static unsigned char mxser_msr[MXSER_PORTS + 1];
375 static struct mxser_mon_ext mon_data_ext;
376 static int mxser_set_baud_method[MXSER_PORTS + 1];
377 static spinlock_t gm_lock;
379 static int CheckIsMoxaMust(int io)
384 outb(0, io + UART_LCR);
385 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
386 oldmcr = inb(io + UART_MCR);
387 outb(0, io + UART_MCR);
388 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
389 if ((hwid = inb(io + UART_MCR)) != 0) {
390 outb(oldmcr, io + UART_MCR);
391 return MOXA_OTHER_UART;
394 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
395 for (i = 0; i < UART_TYPE_NUM; i++) {
396 if (hwid == Gmoxa_uart_id[i])
399 return MOXA_OTHER_UART;
402 /* above is modified by Victor Yu. 08-15-2002 */
404 static void process_txrx_fifo(struct mxser_port *info)
408 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
409 info->rx_trigger = 1;
410 info->rx_high_water = 1;
411 info->rx_low_water = 1;
412 info->xmit_fifo_size = 1;
414 for (i = 0; i < UART_INFO_NUM; i++)
415 if (info->board->chip_flag == Gpci_uart_info[i].type) {
416 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
417 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
418 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
419 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
424 static void mxser_do_softint(void *private_)
426 struct mxser_port *info = private_;
427 struct tty_struct *tty;
431 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
433 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event))
437 static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
439 unsigned char status = 0;
441 status = inb(baseaddr + UART_MSR);
443 mxser_msr[port] &= 0x0F;
444 mxser_msr[port] |= status;
445 status = mxser_msr[port];
452 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
453 struct mxser_port *port)
455 DECLARE_WAITQUEUE(wait, current);
461 * If non-blocking mode is set, or the port is not enabled,
462 * then make the check up front and then exit.
464 if ((filp->f_flags & O_NONBLOCK) ||
465 test_bit(TTY_IO_ERROR, &tty->flags)) {
466 port->flags |= ASYNC_NORMAL_ACTIVE;
470 if (tty->termios->c_cflag & CLOCAL)
474 * Block waiting for the carrier detect and the line to become
475 * free (i.e., not in use by the callout). While we are in
476 * this loop, port->count is dropped by one, so that
477 * mxser_close() knows when to free things. We restore it upon
478 * exit, either normal or abnormal.
481 add_wait_queue(&port->open_wait, &wait);
483 spin_lock_irqsave(&port->slock, flags);
484 if (!tty_hung_up_p(filp))
486 spin_unlock_irqrestore(&port->slock, flags);
487 port->blocked_open++;
489 spin_lock_irqsave(&port->slock, flags);
490 outb(inb(port->ioaddr + UART_MCR) |
491 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
492 spin_unlock_irqrestore(&port->slock, flags);
493 set_current_state(TASK_INTERRUPTIBLE);
494 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
495 if (port->flags & ASYNC_HUP_NOTIFY)
498 retval = -ERESTARTSYS;
501 if (!(port->flags & ASYNC_CLOSING) &&
503 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
505 if (signal_pending(current)) {
506 retval = -ERESTARTSYS;
511 set_current_state(TASK_RUNNING);
512 remove_wait_queue(&port->open_wait, &wait);
513 if (!tty_hung_up_p(filp))
515 port->blocked_open--;
518 port->flags |= ASYNC_NORMAL_ACTIVE;
522 static int mxser_set_baud(struct mxser_port *info, long newspd)
529 if (!info->tty || !info->tty->termios)
535 if (newspd > info->max_baud)
538 info->realbaud = newspd;
540 quot = (2 * info->baud_base / 269);
542 quot = info->baud_base / newspd;
549 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
550 info->timeout += HZ / 50; /* Add .02 seconds of slop */
553 spin_lock_irqsave(&info->slock, flags);
554 info->MCR |= UART_MCR_DTR;
555 outb(info->MCR, info->ioaddr + UART_MCR);
556 spin_unlock_irqrestore(&info->slock, flags);
558 spin_lock_irqsave(&info->slock, flags);
559 info->MCR &= ~UART_MCR_DTR;
560 outb(info->MCR, info->ioaddr + UART_MCR);
561 spin_unlock_irqrestore(&info->slock, flags);
565 cval = inb(info->ioaddr + UART_LCR);
567 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
569 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
570 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
571 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
578 * This routine is called to set the UART divisor registers to match
579 * the specified baud rate for a serial port.
581 static int mxser_change_speed(struct mxser_port *info,
582 struct termios *old_termios)
584 unsigned cflag, cval, fcr;
586 unsigned char status;
590 if (!info->tty || !info->tty->termios)
592 cflag = info->tty->termios->c_cflag;
596 if (mxser_set_baud_method[info->tty->index] == 0) {
597 baud = tty_get_baud_rate(info->tty);
598 mxser_set_baud(info, baud);
601 /* byte size and parity */
602 switch (cflag & CSIZE) {
617 break; /* too keep GCC shut... */
622 cval |= UART_LCR_PARITY;
623 if (!(cflag & PARODD))
624 cval |= UART_LCR_EPAR;
626 cval |= UART_LCR_SPAR;
628 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
629 if (info->board->chip_flag) {
630 fcr = UART_FCR_ENABLE_FIFO;
631 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
632 SET_MOXA_MUST_FIFO_VALUE(info);
636 fcr = UART_FCR_ENABLE_FIFO;
637 /* following add by Victor Yu. 08-30-2002 */
638 if (info->board->chip_flag) {
639 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
640 SET_MOXA_MUST_FIFO_VALUE(info);
642 /* above add by Victor Yu. 08-30-2002 */
643 switch (info->rx_trigger) {
645 fcr |= UART_FCR_TRIGGER_1;
648 fcr |= UART_FCR_TRIGGER_4;
651 fcr |= UART_FCR_TRIGGER_8;
654 fcr |= UART_FCR_TRIGGER_14;
660 /* CTS flow control flag and modem status interrupts */
661 info->IER &= ~UART_IER_MSI;
662 info->MCR &= ~UART_MCR_AFE;
663 if (cflag & CRTSCTS) {
664 info->flags |= ASYNC_CTS_FLOW;
665 info->IER |= UART_IER_MSI;
666 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
667 info->MCR |= UART_MCR_AFE;
668 /* status = mxser_get_msr(info->ioaddr, 0, info->port); */
672 status = inb(baseaddr + UART_MSR);
673 restore_flags(flags);
675 /* mxser_check_modem_status(info, status); */
677 /* status = mxser_get_msr(info->ioaddr, 0, info->port); */
678 /* MX_LOCK(&info->slock); */
679 status = inb(info->ioaddr + UART_MSR);
680 /* MX_UNLOCK(&info->slock); */
681 if (info->tty->hw_stopped) {
682 if (status & UART_MSR_CTS) {
683 info->tty->hw_stopped = 0;
684 if (info->type != PORT_16550A &&
685 !info->board->chip_flag) {
686 outb(info->IER & ~UART_IER_THRI,
689 info->IER |= UART_IER_THRI;
690 outb(info->IER, info->ioaddr +
693 set_bit(MXSER_EVENT_TXLOW, &info->event);
694 schedule_work(&info->tqueue); }
696 if (!(status & UART_MSR_CTS)) {
697 info->tty->hw_stopped = 1;
698 if ((info->type != PORT_16550A) &&
699 (!info->board->chip_flag)) {
700 info->IER &= ~UART_IER_THRI;
701 outb(info->IER, info->ioaddr +
708 info->flags &= ~ASYNC_CTS_FLOW;
710 outb(info->MCR, info->ioaddr + UART_MCR);
711 if (cflag & CLOCAL) {
712 info->flags &= ~ASYNC_CHECK_CD;
714 info->flags |= ASYNC_CHECK_CD;
715 info->IER |= UART_IER_MSI;
717 outb(info->IER, info->ioaddr + UART_IER);
720 * Set up parity check flag
722 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
723 if (I_INPCK(info->tty))
724 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
725 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
726 info->read_status_mask |= UART_LSR_BI;
728 info->ignore_status_mask = 0;
730 if (I_IGNBRK(info->tty)) {
731 info->ignore_status_mask |= UART_LSR_BI;
732 info->read_status_mask |= UART_LSR_BI;
734 * If we're ignore parity and break indicators, ignore
735 * overruns too. (For real raw support).
737 if (I_IGNPAR(info->tty)) {
738 info->ignore_status_mask |=
742 info->read_status_mask |=
748 /* following add by Victor Yu. 09-02-2002 */
749 if (info->board->chip_flag) {
750 spin_lock_irqsave(&info->slock, flags);
751 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
752 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
753 if (I_IXON(info->tty)) {
754 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
756 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
758 if (I_IXOFF(info->tty)) {
759 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
761 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
764 if ( I_IXANY(info->tty) ) {
765 info->MCR |= MOXA_MUST_MCR_XON_ANY;
766 ENABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
768 info->MCR &= ~MOXA_MUST_MCR_XON_ANY;
769 DISABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
772 spin_unlock_irqrestore(&info->slock, flags);
774 /* above add by Victor Yu. 09-02-2002 */
777 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
778 outb(cval, info->ioaddr + UART_LCR);
783 static void mxser_check_modem_status(struct mxser_port *port, int status)
785 /* update input line counters */
786 if (status & UART_MSR_TERI)
788 if (status & UART_MSR_DDSR)
790 if (status & UART_MSR_DDCD)
792 if (status & UART_MSR_DCTS)
794 port->mon_data.modem_status = status;
795 wake_up_interruptible(&port->delta_msr_wait);
797 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
798 if (status & UART_MSR_DCD)
799 wake_up_interruptible(&port->open_wait);
800 schedule_work(&port->tqueue);
803 if (port->flags & ASYNC_CTS_FLOW) {
804 if (port->tty->hw_stopped) {
805 if (status & UART_MSR_CTS) {
806 port->tty->hw_stopped = 0;
808 if ((port->type != PORT_16550A) &&
809 (!port->board->chip_flag)) {
810 outb(port->IER & ~UART_IER_THRI,
811 port->ioaddr + UART_IER);
812 port->IER |= UART_IER_THRI;
813 outb(port->IER, port->ioaddr +
816 set_bit(MXSER_EVENT_TXLOW, &port->event);
817 schedule_work(&port->tqueue);
820 if (!(status & UART_MSR_CTS)) {
821 port->tty->hw_stopped = 1;
822 if (port->type != PORT_16550A &&
823 !port->board->chip_flag) {
824 port->IER &= ~UART_IER_THRI;
825 outb(port->IER, port->ioaddr +
833 static int mxser_startup(struct mxser_port *info)
838 page = __get_free_page(GFP_KERNEL);
842 spin_lock_irqsave(&info->slock, flags);
844 if (info->flags & ASYNC_INITIALIZED) {
846 spin_unlock_irqrestore(&info->slock, flags);
850 if (!info->ioaddr || !info->type) {
852 set_bit(TTY_IO_ERROR, &info->tty->flags);
854 spin_unlock_irqrestore(&info->slock, flags);
860 info->xmit_buf = (unsigned char *) page;
863 * Clear the FIFO buffers and disable them
864 * (they will be reenabled in mxser_change_speed())
866 if (info->board->chip_flag)
867 outb((UART_FCR_CLEAR_RCVR |
868 UART_FCR_CLEAR_XMIT |
869 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
871 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
872 info->ioaddr + UART_FCR);
875 * At this point there's no way the LSR could still be 0xFF;
876 * if it is, then bail out, because there's likely no UART
879 if (inb(info->ioaddr + UART_LSR) == 0xff) {
880 spin_unlock_irqrestore(&info->slock, flags);
881 if (capable(CAP_SYS_ADMIN)) {
883 set_bit(TTY_IO_ERROR, &info->tty->flags);
890 * Clear the interrupt registers.
892 (void) inb(info->ioaddr + UART_LSR);
893 (void) inb(info->ioaddr + UART_RX);
894 (void) inb(info->ioaddr + UART_IIR);
895 (void) inb(info->ioaddr + UART_MSR);
898 * Now, initialize the UART
900 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
901 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
902 outb(info->MCR, info->ioaddr + UART_MCR);
905 * Finally, enable interrupts
907 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
908 /* info->IER = UART_IER_RLSI | UART_IER_RDI; */
910 /* following add by Victor Yu. 08-30-2002 */
911 if (info->board->chip_flag)
912 info->IER |= MOXA_MUST_IER_EGDAI;
913 /* above add by Victor Yu. 08-30-2002 */
914 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
917 * And clear the interrupt registers again for luck.
919 (void) inb(info->ioaddr + UART_LSR);
920 (void) inb(info->ioaddr + UART_RX);
921 (void) inb(info->ioaddr + UART_IIR);
922 (void) inb(info->ioaddr + UART_MSR);
925 clear_bit(TTY_IO_ERROR, &info->tty->flags);
926 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
929 * and set the speed of the serial port
931 spin_unlock_irqrestore(&info->slock, flags);
932 mxser_change_speed(info, NULL);
934 info->flags |= ASYNC_INITIALIZED;
939 * This routine will shutdown a serial port; interrupts maybe disabled, and
940 * DTR is dropped if the hangup on close termio flag is on.
942 static void mxser_shutdown(struct mxser_port *info)
946 if (!(info->flags & ASYNC_INITIALIZED))
949 spin_lock_irqsave(&info->slock, flags);
952 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
953 * here so the queue might never be waken up
955 wake_up_interruptible(&info->delta_msr_wait);
958 * Free the IRQ, if necessary
960 if (info->xmit_buf) {
961 free_page((unsigned long) info->xmit_buf);
962 info->xmit_buf = NULL;
966 outb(0x00, info->ioaddr + UART_IER);
968 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
969 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
970 outb(info->MCR, info->ioaddr + UART_MCR);
972 /* clear Rx/Tx FIFO's */
973 /* following add by Victor Yu. 08-30-2002 */
974 if (info->board->chip_flag)
975 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
976 MOXA_MUST_FCR_GDA_MODE_ENABLE,
977 info->ioaddr + UART_FCR);
979 /* above add by Victor Yu. 08-30-2002 */
980 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
981 info->ioaddr + UART_FCR);
983 /* read data port to reset things */
984 (void) inb(info->ioaddr + UART_RX);
987 set_bit(TTY_IO_ERROR, &info->tty->flags);
989 info->flags &= ~ASYNC_INITIALIZED;
991 /* following add by Victor Yu. 09-23-2002 */
992 if (info->board->chip_flag)
993 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
994 /* above add by Victor Yu. 09-23-2002 */
996 spin_unlock_irqrestore(&info->slock, flags);
1000 * This routine is called whenever a serial port is opened. It
1001 * enables interrupts for a serial port, linking in its async structure into
1002 * the IRQ chain. It also performs the serial-specific
1003 * initialization for the tty structure.
1005 static int mxser_open(struct tty_struct *tty, struct file *filp)
1007 struct mxser_port *info;
1010 /* initialize driver_data in case something fails */
1011 tty->driver_data = NULL;
1014 if (line == MXSER_PORTS)
1016 if (line < 0 || line > MXSER_PORTS)
1018 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
1022 tty->driver_data = info;
1025 * Start up serial port
1028 retval = mxser_startup(info);
1032 retval = mxser_block_til_ready(tty, filp, info);
1036 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
1037 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
1038 *tty->termios = info->normal_termios;
1040 *tty->termios = info->callout_termios;
1041 mxser_change_speed(info, NULL);
1044 info->session = process_session(current);
1045 info->pgrp = process_group(current);
1048 status = mxser_get_msr(info->base, 0, info->port);
1049 mxser_check_modem_status(info, status);
1052 /* unmark here for very high baud rate (ex. 921600 bps) used */
1053 tty->low_latency = 1;
1058 * This routine is called when the serial port gets closed. First, we
1059 * wait for the last remaining data to be sent. Then, we unlink its
1060 * async structure from the interrupt chain if necessary, and we free
1061 * that IRQ if nothing is left in the chain.
1063 static void mxser_close(struct tty_struct *tty, struct file *filp)
1065 struct mxser_port *info = tty->driver_data;
1067 unsigned long timeout;
1068 unsigned long flags;
1070 if (tty->index == MXSER_PORTS)
1075 spin_lock_irqsave(&info->slock, flags);
1077 if (tty_hung_up_p(filp)) {
1078 spin_unlock_irqrestore(&info->slock, flags);
1081 if ((tty->count == 1) && (info->count != 1)) {
1083 * Uh, oh. tty->count is 1, which means that the tty
1084 * structure will be freed. Info->count should always
1085 * be one in these conditions. If it's greater than
1086 * one, we've got real problems, since it means the
1087 * serial port won't be shutdown.
1089 printk(KERN_ERR "mxser_close: bad serial port count; "
1090 "tty->count is 1, info->count is %d\n", info->count);
1093 if (--info->count < 0) {
1094 printk(KERN_ERR "mxser_close: bad serial port count for "
1095 "ttys%d: %d\n", tty->index, info->count);
1099 spin_unlock_irqrestore(&info->slock, flags);
1102 info->flags |= ASYNC_CLOSING;
1103 spin_unlock_irqrestore(&info->slock, flags);
1105 * Save the termios structure, since this port may have
1106 * separate termios for callout and dialin.
1108 if (info->flags & ASYNC_NORMAL_ACTIVE)
1109 info->normal_termios = *tty->termios;
1111 * Now we wait for the transmit buffer to clear; and we notify
1112 * the line discipline to only process XON/XOFF characters.
1115 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1116 tty_wait_until_sent(tty, info->closing_wait);
1118 * At this point we stop accepting input. To do this, we
1119 * disable the receive line status interrupts, and tell the
1120 * interrupt driver to stop checking the data ready bit in the
1121 * line status register.
1123 info->IER &= ~UART_IER_RLSI;
1124 if (info->board->chip_flag)
1125 info->IER &= ~MOXA_MUST_RECV_ISR;
1127 info->read_status_mask &= ~UART_LSR_DR;
1129 if (info->flags & ASYNC_INITIALIZED) {
1130 outb(info->IER, info->ioaddr + UART_IER);
1132 * Before we drop DTR, make sure the UART transmitter
1133 * has completely drained; this is especially
1134 * important if there is a transmit FIFO!
1136 timeout = jiffies + HZ;
1137 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
1138 schedule_timeout_interruptible(5);
1139 if (time_after(jiffies, timeout))
1143 mxser_shutdown(info);
1145 if (tty->driver->flush_buffer)
1146 tty->driver->flush_buffer(tty);
1148 tty_ldisc_flush(tty);
1153 if (info->blocked_open) {
1154 if (info->close_delay)
1155 schedule_timeout_interruptible(info->close_delay);
1156 wake_up_interruptible(&info->open_wait);
1159 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1160 wake_up_interruptible(&info->close_wait);
1164 static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1167 struct mxser_port *info = tty->driver_data;
1168 unsigned long flags;
1170 if (!info->xmit_buf)
1174 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1175 SERIAL_XMIT_SIZE - info->xmit_head));
1179 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1180 spin_lock_irqsave(&info->slock, flags);
1181 info->xmit_head = (info->xmit_head + c) &
1182 (SERIAL_XMIT_SIZE - 1);
1183 info->xmit_cnt += c;
1184 spin_unlock_irqrestore(&info->slock, flags);
1191 if (info->xmit_cnt && !tty->stopped
1192 /*&& !(info->IER & UART_IER_THRI)*/) {
1193 if (!tty->hw_stopped ||
1194 (info->type == PORT_16550A) ||
1195 (info->board->chip_flag)) {
1196 spin_lock_irqsave(&info->slock, flags);
1197 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1199 info->IER |= UART_IER_THRI;
1200 outb(info->IER, info->ioaddr + UART_IER);
1201 spin_unlock_irqrestore(&info->slock, flags);
1207 static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1209 struct mxser_port *info = tty->driver_data;
1210 unsigned long flags;
1212 if (!info->xmit_buf)
1215 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1218 spin_lock_irqsave(&info->slock, flags);
1219 info->xmit_buf[info->xmit_head++] = ch;
1220 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1222 spin_unlock_irqrestore(&info->slock, flags);
1223 if (!tty->stopped /*&& !(info->IER & UART_IER_THRI)*/) {
1224 if (!tty->hw_stopped ||
1225 (info->type == PORT_16550A) ||
1226 info->board->chip_flag) {
1227 spin_lock_irqsave(&info->slock, flags);
1228 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1229 info->IER |= UART_IER_THRI;
1230 outb(info->IER, info->ioaddr + UART_IER);
1231 spin_unlock_irqrestore(&info->slock, flags);
1237 static void mxser_flush_chars(struct tty_struct *tty)
1239 struct mxser_port *info = tty->driver_data;
1240 unsigned long flags;
1242 if (info->xmit_cnt <= 0 ||
1246 (info->type != PORT_16550A) &&
1247 (!info->board->chip_flag)
1251 spin_lock_irqsave(&info->slock, flags);
1253 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1254 info->IER |= UART_IER_THRI;
1255 outb(info->IER, info->ioaddr + UART_IER);
1257 spin_unlock_irqrestore(&info->slock, flags);
1260 static int mxser_write_room(struct tty_struct *tty)
1262 struct mxser_port *info = tty->driver_data;
1265 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1271 static int mxser_chars_in_buffer(struct tty_struct *tty)
1273 struct mxser_port *info = tty->driver_data;
1274 int len = info->xmit_cnt;
1276 if (!(inb(info->ioaddr + UART_LSR) & UART_LSR_THRE))
1282 static void mxser_flush_buffer(struct tty_struct *tty)
1284 struct mxser_port *info = tty->driver_data;
1286 unsigned long flags;
1289 spin_lock_irqsave(&info->slock, flags);
1290 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1292 /* below added by shinhay */
1293 fcr = inb(info->ioaddr + UART_FCR);
1294 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1295 info->ioaddr + UART_FCR);
1296 outb(fcr, info->ioaddr + UART_FCR);
1298 spin_unlock_irqrestore(&info->slock, flags);
1299 /* above added by shinhay */
1305 * ------------------------------------------------------------
1306 * friends of mxser_ioctl()
1307 * ------------------------------------------------------------
1309 static int mxser_get_serial_info(struct mxser_port *info,
1310 struct serial_struct __user *retinfo)
1312 struct serial_struct tmp;
1316 memset(&tmp, 0, sizeof(tmp));
1317 tmp.type = info->type;
1318 tmp.line = info->tty->index;
1319 tmp.port = info->ioaddr;
1320 tmp.irq = info->board->irq;
1321 tmp.flags = info->flags;
1322 tmp.baud_base = info->baud_base;
1323 tmp.close_delay = info->close_delay;
1324 tmp.closing_wait = info->closing_wait;
1325 tmp.custom_divisor = info->custom_divisor;
1327 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1332 static int mxser_set_serial_info(struct mxser_port *info,
1333 struct serial_struct __user *new_info)
1335 struct serial_struct new_serial;
1339 if (!new_info || !info->ioaddr)
1341 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1344 if ((new_serial.irq != info->board->irq) ||
1345 (new_serial.port != info->ioaddr) ||
1346 (new_serial.custom_divisor != info->custom_divisor) ||
1347 (new_serial.baud_base != info->baud_base))
1350 flags = info->flags & ASYNC_SPD_MASK;
1352 if (!capable(CAP_SYS_ADMIN)) {
1353 if ((new_serial.baud_base != info->baud_base) ||
1354 (new_serial.close_delay != info->close_delay) ||
1355 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
1357 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1358 (new_serial.flags & ASYNC_USR_MASK));
1361 * OK, past this point, all the error checking has been done.
1362 * At this point, we start making changes.....
1364 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1365 (new_serial.flags & ASYNC_FLAGS));
1366 info->close_delay = new_serial.close_delay * HZ / 100;
1367 info->closing_wait = new_serial.closing_wait * HZ / 100;
1368 info->tty->low_latency =
1369 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1370 info->tty->low_latency = 0; /* (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0; */
1373 /* added by casper, 3/17/2000, for mouse */
1374 info->type = new_serial.type;
1376 process_txrx_fifo(info);
1378 if (info->flags & ASYNC_INITIALIZED) {
1379 if (flags != (info->flags & ASYNC_SPD_MASK))
1380 mxser_change_speed(info, NULL);
1382 retval = mxser_startup(info);
1388 * mxser_get_lsr_info - get line status register info
1390 * Purpose: Let user call ioctl() to get info when the UART physically
1391 * is emptied. On bus types like RS485, the transmitter must
1392 * release the bus after transmitting. This must be done when
1393 * the transmit shift register is empty, not be done when the
1394 * transmit holding register is empty. This functionality
1395 * allows an RS485 driver to be written in user space.
1397 static int mxser_get_lsr_info(struct mxser_port *info,
1398 unsigned int __user *value)
1400 unsigned char status;
1401 unsigned int result;
1402 unsigned long flags;
1404 spin_lock_irqsave(&info->slock, flags);
1405 status = inb(info->ioaddr + UART_LSR);
1406 spin_unlock_irqrestore(&info->slock, flags);
1407 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1408 return put_user(result, value);
1412 * This routine sends a break character out the serial port.
1414 static void mxser_send_break(struct mxser_port *info, int duration)
1416 unsigned long flags;
1420 set_current_state(TASK_INTERRUPTIBLE);
1421 spin_lock_irqsave(&info->slock, flags);
1422 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1423 info->ioaddr + UART_LCR);
1424 spin_unlock_irqrestore(&info->slock, flags);
1425 schedule_timeout(duration);
1426 spin_lock_irqsave(&info->slock, flags);
1427 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1428 info->ioaddr + UART_LCR);
1429 spin_unlock_irqrestore(&info->slock, flags);
1432 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
1434 struct mxser_port *info = tty->driver_data;
1435 unsigned char control, status;
1436 unsigned long flags;
1439 if (tty->index == MXSER_PORTS)
1440 return -ENOIOCTLCMD;
1441 if (test_bit(TTY_IO_ERROR, &tty->flags))
1444 control = info->MCR;
1446 spin_lock_irqsave(&info->slock, flags);
1447 status = inb(info->ioaddr + UART_MSR);
1448 if (status & UART_MSR_ANY_DELTA)
1449 mxser_check_modem_status(info, status);
1450 spin_unlock_irqrestore(&info->slock, flags);
1451 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1452 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1453 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1454 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1455 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1456 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1459 static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1460 unsigned int set, unsigned int clear)
1462 struct mxser_port *info = tty->driver_data;
1463 unsigned long flags;
1466 if (tty->index == MXSER_PORTS)
1467 return -ENOIOCTLCMD;
1468 if (test_bit(TTY_IO_ERROR, &tty->flags))
1471 spin_lock_irqsave(&info->slock, flags);
1473 if (set & TIOCM_RTS)
1474 info->MCR |= UART_MCR_RTS;
1475 if (set & TIOCM_DTR)
1476 info->MCR |= UART_MCR_DTR;
1478 if (clear & TIOCM_RTS)
1479 info->MCR &= ~UART_MCR_RTS;
1480 if (clear & TIOCM_DTR)
1481 info->MCR &= ~UART_MCR_DTR;
1483 outb(info->MCR, info->ioaddr + UART_MCR);
1484 spin_unlock_irqrestore(&info->slock, flags);
1488 static int mxser_program_mode(int port)
1491 /* unsigned long flags; */
1493 spin_lock(&gm_lock);
1501 /* restore_flags(flags); */
1502 spin_unlock(&gm_lock);
1504 id = inb(port + 1) & 0x1F;
1505 if ((id != C168_ASIC_ID) &&
1506 (id != C104_ASIC_ID) &&
1507 (id != C102_ASIC_ID) &&
1508 (id != CI132_ASIC_ID) &&
1509 (id != CI134_ASIC_ID) &&
1510 (id != CI104J_ASIC_ID))
1512 for (i = 0, j = 0; i < 4; i++) {
1516 } else if ((j == 1) && (n == 1)) {
1527 static void mxser_normal_mode(int port)
1531 outb(0xA5, port + 1);
1532 outb(0x80, port + 3);
1533 outb(12, port + 0); /* 9600 bps */
1535 outb(0x03, port + 3); /* 8 data bits */
1536 outb(0x13, port + 4); /* loop back mode */
1537 for (i = 0; i < 16; i++) {
1539 if ((n & 0x61) == 0x60)
1544 outb(0x00, port + 4);
1547 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1548 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1549 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1550 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1551 #define EN_CCMD 0x000 /* Chip's command register */
1552 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1553 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1554 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1555 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1556 #define EN0_DCFG 0x00E /* Data configuration reg WR */
1557 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1558 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1559 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1560 static int mxser_read_register(int port, unsigned short *regs)
1562 int i, k, value, id;
1565 id = mxser_program_mode(port);
1568 for (i = 0; i < 14; i++) {
1569 k = (i & 0x3F) | 0x180;
1570 for (j = 0x100; j > 0; j >>= 1) {
1571 outb(CHIP_CS, port);
1573 outb(CHIP_CS | CHIP_DO, port);
1574 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1576 outb(CHIP_CS, port);
1577 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1582 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1583 outb(CHIP_CS, port);
1584 outb(CHIP_CS | CHIP_SK, port);
1585 if (inb(port) & CHIP_DI)
1591 mxser_normal_mode(port);
1595 static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1597 struct mxser_port *port;
1603 /* if (copy_to_user(argp, mxsercfg,
1604 sizeof(struct mxser_hwconf) * 4))
1608 case MOXA_GET_MAJOR:
1609 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1613 case MOXA_GET_CUMAJOR:
1614 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1618 case MOXA_CHKPORTENABLE:
1621 for (i = 0; i < MXSER_BOARDS; i++)
1622 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1623 if (mxser_boards[i].ports[j].ioaddr)
1626 return put_user(result, (unsigned long __user *)argp);
1627 case MOXA_GETDATACOUNT:
1628 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1631 case MOXA_GETMSTATUS:
1632 for (i = 0; i < MXSER_BOARDS; i++)
1633 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1634 port = &mxser_boards[i].ports[j];
1637 if (!port->ioaddr) {
1638 GMStatus[i].dcd = 0;
1639 GMStatus[i].dsr = 0;
1640 GMStatus[i].cts = 0;
1644 if (!port->tty || !port->tty->termios)
1646 port->normal_termios.c_cflag;
1649 port->tty->termios->c_cflag;
1651 status = inb(port->ioaddr + UART_MSR);
1652 if (status & 0x80 /*UART_MSR_DCD */ )
1653 GMStatus[i].dcd = 1;
1655 GMStatus[i].dcd = 0;
1657 if (status & 0x20 /*UART_MSR_DSR */ )
1658 GMStatus[i].dsr = 1;
1660 GMStatus[i].dsr = 0;
1663 if (status & 0x10 /*UART_MSR_CTS */ )
1664 GMStatus[i].cts = 1;
1666 GMStatus[i].cts = 0;
1668 if (copy_to_user(argp, GMStatus,
1669 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1672 case MOXA_ASPP_MON_EXT: {
1673 int status, p, shiftbit;
1674 unsigned long opmode;
1675 unsigned cflag, iflag;
1677 for (i = 0; i < MXSER_BOARDS; i++)
1678 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1679 port = &mxser_boards[i].ports[j];
1683 status = mxser_get_msr(port->ioaddr, 0, i);
1684 /* mxser_check_modem_status(port, status); */
1686 if (status & UART_MSR_TERI)
1688 if (status & UART_MSR_DDSR)
1690 if (status & UART_MSR_DDCD)
1692 if (status & UART_MSR_DCTS)
1695 port->mon_data.modem_status = status;
1696 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1697 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1698 mon_data_ext.up_rxcnt[i] =
1699 port->mon_data.up_rxcnt;
1700 mon_data_ext.up_txcnt[i] =
1701 port->mon_data.up_txcnt;
1702 mon_data_ext.modem_status[i] =
1703 port->mon_data.modem_status;
1704 mon_data_ext.baudrate[i] = port->realbaud;
1706 if (!port->tty || !port->tty->termios) {
1707 cflag = port->normal_termios.c_cflag;
1708 iflag = port->normal_termios.c_iflag;
1710 cflag = port->tty->termios->c_cflag;
1711 iflag = port->tty->termios->c_iflag;
1714 mon_data_ext.databits[i] = cflag & CSIZE;
1716 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1718 mon_data_ext.parity[i] =
1719 cflag & (PARENB | PARODD | CMSPAR);
1721 mon_data_ext.flowctrl[i] = 0x00;
1723 if (cflag & CRTSCTS)
1724 mon_data_ext.flowctrl[i] |= 0x03;
1726 if (iflag & (IXON | IXOFF))
1727 mon_data_ext.flowctrl[i] |= 0x0C;
1729 if (port->type == PORT_16550A)
1730 mon_data_ext.fifo[i] = 1;
1732 mon_data_ext.fifo[i] = 0;
1736 opmode = inb(port->opmode_ioaddr) >> shiftbit;
1737 opmode &= OP_MODE_MASK;
1739 mon_data_ext.iftype[i] = opmode;
1742 if (copy_to_user(argp, &mon_data_ext,
1743 sizeof(mon_data_ext)))
1749 return -ENOIOCTLCMD;
1754 static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1755 unsigned int cmd, unsigned long arg)
1757 struct mxser_port *info = tty->driver_data;
1758 struct async_icount cprev, cnow; /* kernel counter temps */
1759 struct serial_icounter_struct __user *p_cuser;
1760 unsigned long templ;
1761 unsigned long flags;
1762 void __user *argp = (void __user *)arg;
1765 if (tty->index == MXSER_PORTS)
1766 return mxser_ioctl_special(cmd, argp);
1768 /* following add by Victor Yu. 01-05-2004 */
1769 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1771 unsigned long opmode;
1772 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1774 unsigned char val, mask;
1777 if (cmd == MOXA_SET_OP_MODE) {
1778 if (get_user(opmode, (int __user *) argp))
1780 if (opmode != RS232_MODE &&
1781 opmode != RS485_2WIRE_MODE &&
1782 opmode != RS422_MODE &&
1783 opmode != RS485_4WIRE_MODE)
1787 val = inb(info->opmode_ioaddr);
1789 val |= (opmode << shiftbit);
1790 outb(val, info->opmode_ioaddr);
1793 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1794 opmode &= OP_MODE_MASK;
1795 if (copy_to_user(argp, &opmode, sizeof(int)))
1800 /* above add by Victor Yu. 01-05-2004 */
1802 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1803 test_bit(TTY_IO_ERROR, &tty->flags))
1807 case TCSBRK: /* SVID version: non-zero arg --> no break */
1808 retval = tty_check_change(tty);
1811 tty_wait_until_sent(tty, 0);
1813 mxser_send_break(info, HZ / 4); /* 1/4 second */
1815 case TCSBRKP: /* support for POSIX tcsendbreak() */
1816 retval = tty_check_change(tty);
1819 tty_wait_until_sent(tty, 0);
1820 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1823 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1825 if (get_user(templ, (unsigned long __user *) argp))
1828 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1831 return mxser_get_serial_info(info, argp);
1833 return mxser_set_serial_info(info, argp);
1834 case TIOCSERGETLSR: /* Get line status register */
1835 return mxser_get_lsr_info(info, argp);
1837 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1838 * - mask passed in arg for lines of interest
1839 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1840 * Caller should use TIOCGICOUNT to see which one it was
1843 DECLARE_WAITQUEUE(wait, current);
1845 spin_lock_irqsave(&info->slock, flags);
1846 cprev = info->icount; /* note the counters on entry */
1847 spin_unlock_irqrestore(&info->slock, flags);
1849 add_wait_queue(&info->delta_msr_wait, &wait);
1851 spin_lock_irqsave(&info->slock, flags);
1852 cnow = info->icount; /* atomic copy */
1853 spin_unlock_irqrestore(&info->slock, flags);
1855 set_current_state(TASK_INTERRUPTIBLE);
1856 if (((arg & TIOCM_RNG) &&
1857 (cnow.rng != cprev.rng)) ||
1858 ((arg & TIOCM_DSR) &&
1859 (cnow.dsr != cprev.dsr)) ||
1860 ((arg & TIOCM_CD) &&
1861 (cnow.dcd != cprev.dcd)) ||
1862 ((arg & TIOCM_CTS) &&
1863 (cnow.cts != cprev.cts))) {
1867 /* see if a signal did it */
1868 if (signal_pending(current)) {
1874 current->state = TASK_RUNNING;
1875 remove_wait_queue(&info->delta_msr_wait, &wait);
1880 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1881 * Return: write counters to the user passed counter struct
1882 * NB: both 1->0 and 0->1 transitions are counted except for
1883 * RI where only 0->1 is counted.
1886 spin_lock_irqsave(&info->slock, flags);
1887 cnow = info->icount;
1888 spin_unlock_irqrestore(&info->slock, flags);
1890 /* modified by casper 1/11/2000 */
1891 if (put_user(cnow.frame, &p_cuser->frame))
1893 if (put_user(cnow.brk, &p_cuser->brk))
1895 if (put_user(cnow.overrun, &p_cuser->overrun))
1897 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1899 if (put_user(cnow.parity, &p_cuser->parity))
1901 if (put_user(cnow.rx, &p_cuser->rx))
1903 if (put_user(cnow.tx, &p_cuser->tx))
1905 put_user(cnow.cts, &p_cuser->cts);
1906 put_user(cnow.dsr, &p_cuser->dsr);
1907 put_user(cnow.rng, &p_cuser->rng);
1908 put_user(cnow.dcd, &p_cuser->dcd);
1910 case MOXA_HighSpeedOn:
1911 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1912 case MOXA_SDS_RSTICOUNTER:
1913 info->mon_data.rxcnt = 0;
1914 info->mon_data.txcnt = 0;
1916 /* (above) added by James. */
1917 case MOXA_ASPP_SETBAUD:{
1919 if (get_user(baud, (long __user *)argp))
1921 mxser_set_baud(info, baud);
1924 case MOXA_ASPP_GETBAUD:
1925 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1930 case MOXA_ASPP_OQUEUE:{
1933 len = mxser_chars_in_buffer(tty);
1935 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1937 len += (lsr ? 0 : 1);
1939 if (copy_to_user(argp, &len, sizeof(int)))
1944 case MOXA_ASPP_MON: {
1947 /* info->mon_data.ser_param = tty->termios->c_cflag; */
1949 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1950 mxser_check_modem_status(info, status);
1952 mcr = inb(info->ioaddr + UART_MCR);
1953 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1954 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1956 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1958 if (mcr & MOXA_MUST_MCR_TX_XON)
1959 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1961 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1963 if (info->tty->hw_stopped)
1964 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1966 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1968 if (copy_to_user(argp, &info->mon_data,
1969 sizeof(struct mxser_mon)))
1974 case MOXA_ASPP_LSTATUS: {
1975 if (copy_to_user(argp, &info->err_shadow,
1976 sizeof(unsigned char)))
1979 info->err_shadow = 0;
1982 case MOXA_SET_BAUD_METHOD: {
1985 if (get_user(method, (int __user *)argp))
1987 mxser_set_baud_method[tty->index] = method;
1988 if (copy_to_user(argp, &method, sizeof(int)))
1994 return -ENOIOCTLCMD;
1999 static void mxser_stoprx(struct tty_struct *tty)
2001 struct mxser_port *info = tty->driver_data;
2003 info->ldisc_stop_rx = 1;
2005 /* following add by Victor Yu. 09-02-2002 */
2006 if (info->board->chip_flag) {
2007 info->IER &= ~MOXA_MUST_RECV_ISR;
2008 outb(info->IER, info->ioaddr + UART_IER);
2009 } else if (!(info->flags & ASYNC_CLOSING)) {
2010 info->x_char = STOP_CHAR(tty);
2011 outb(0, info->ioaddr + UART_IER);
2012 info->IER |= UART_IER_THRI;
2013 outb(info->IER, info->ioaddr + UART_IER);
2017 if (info->tty->termios->c_cflag & CRTSCTS) {
2018 info->MCR &= ~UART_MCR_RTS;
2019 outb(info->MCR, info->ioaddr + UART_MCR);
2024 * This routine is called by the upper-layer tty layer to signal that
2025 * incoming characters should be throttled.
2027 static void mxser_throttle(struct tty_struct *tty)
2032 static void mxser_unthrottle(struct tty_struct *tty)
2034 struct mxser_port *info = tty->driver_data;
2037 info->ldisc_stop_rx = 0;
2042 /* following add by Victor Yu. 09-02-2002 */
2043 if (info->board->chip_flag) {
2044 info->IER |= MOXA_MUST_RECV_ISR;
2045 outb(info->IER, info->ioaddr + UART_IER);
2046 } else if (!(info->flags & ASYNC_CLOSING)) {
2047 info->x_char = START_CHAR(tty);
2048 outb(0, info->ioaddr + UART_IER);
2049 info->IER |= UART_IER_THRI;
2050 outb(info->IER, info->ioaddr + UART_IER);
2055 if (info->tty->termios->c_cflag & CRTSCTS) {
2056 info->MCR |= UART_MCR_RTS;
2057 outb(info->MCR, info->ioaddr + UART_MCR);
2062 * mxser_stop() and mxser_start()
2064 * This routines are called before setting or resetting tty->stopped.
2065 * They enable or disable transmitter interrupts, as necessary.
2067 static void mxser_stop(struct tty_struct *tty)
2069 struct mxser_port *info = tty->driver_data;
2070 unsigned long flags;
2072 spin_lock_irqsave(&info->slock, flags);
2073 if (info->IER & UART_IER_THRI) {
2074 info->IER &= ~UART_IER_THRI;
2075 outb(info->IER, info->ioaddr + UART_IER);
2077 spin_unlock_irqrestore(&info->slock, flags);
2080 static void mxser_start(struct tty_struct *tty)
2082 struct mxser_port *info = tty->driver_data;
2083 unsigned long flags;
2085 spin_lock_irqsave(&info->slock, flags);
2086 if (info->xmit_cnt && info->xmit_buf
2087 /* && !(info->IER & UART_IER_THRI) */) {
2088 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
2089 info->IER |= UART_IER_THRI;
2090 outb(info->IER, info->ioaddr + UART_IER);
2092 spin_unlock_irqrestore(&info->slock, flags);
2095 static void mxser_set_termios(struct tty_struct *tty, struct termios *old_termios)
2097 struct mxser_port *info = tty->driver_data;
2098 unsigned long flags;
2100 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
2101 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
2103 mxser_change_speed(info, old_termios);
2105 if ((old_termios->c_cflag & CRTSCTS) &&
2106 !(tty->termios->c_cflag & CRTSCTS)) {
2107 tty->hw_stopped = 0;
2112 /* Handle sw stopped */
2113 if ((old_termios->c_iflag & IXON) &&
2114 !(tty->termios->c_iflag & IXON)) {
2117 /* following add by Victor Yu. 09-02-2002 */
2118 if (info->board->chip_flag) {
2119 spin_lock_irqsave(&info->slock, flags);
2120 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2121 spin_unlock_irqrestore(&info->slock, flags);
2123 /* above add by Victor Yu. 09-02-2002 */
2130 * mxser_wait_until_sent() --- wait until the transmitter is empty
2132 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2134 struct mxser_port *info = tty->driver_data;
2135 unsigned long orig_jiffies, char_time;
2138 if (info->type == PORT_UNKNOWN)
2141 if (info->xmit_fifo_size == 0)
2142 return; /* Just in case.... */
2144 orig_jiffies = jiffies;
2146 * Set the check interval to be 1/5 of the estimated time to
2147 * send a single character, and make it at least 1. The check
2148 * interval should also be less than the timeout.
2150 * Note: we have to use pretty tight timings here to satisfy
2153 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2154 char_time = char_time / 5;
2157 if (timeout && timeout < char_time)
2158 char_time = timeout;
2160 * If the transmitter hasn't cleared in twice the approximate
2161 * amount of time to send the entire FIFO, it probably won't
2162 * ever clear. This assumes the UART isn't doing flow
2163 * control, which is currently the case. Hence, if it ever
2164 * takes longer than info->timeout, this is probably due to a
2165 * UART bug of some kind. So, we clamp the timeout parameter at
2168 if (!timeout || timeout > 2 * info->timeout)
2169 timeout = 2 * info->timeout;
2170 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2171 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2172 timeout, char_time);
2173 printk("jiff=%lu...", jiffies);
2175 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
2176 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2177 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2179 schedule_timeout_interruptible(char_time);
2180 if (signal_pending(current))
2182 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2185 set_current_state(TASK_RUNNING);
2187 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2188 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2194 * This routine is called by tty_hangup() when a hangup is signaled.
2196 void mxser_hangup(struct tty_struct *tty)
2198 struct mxser_port *info = tty->driver_data;
2200 mxser_flush_buffer(tty);
2201 mxser_shutdown(info);
2204 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2206 wake_up_interruptible(&info->open_wait);
2210 /* added by James 03-12-2004. */
2212 * mxser_rs_break() --- routine which turns the break handling on or off
2214 static void mxser_rs_break(struct tty_struct *tty, int break_state)
2216 struct mxser_port *info = tty->driver_data;
2217 unsigned long flags;
2219 spin_lock_irqsave(&info->slock, flags);
2220 if (break_state == -1)
2221 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2222 info->ioaddr + UART_LCR);
2224 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2225 info->ioaddr + UART_LCR);
2226 spin_unlock_irqrestore(&info->slock, flags);
2229 /* (above) added by James. */
2231 static void mxser_receive_chars(struct mxser_port *port, int *status)
2233 struct tty_struct *tty = port->tty;
2234 unsigned char ch, gdl;
2239 unsigned long flags;
2241 spin_lock_irqsave(&port->slock, flags);
2243 recv_room = tty->receive_room;
2244 if ((recv_room == 0) && (!port->ldisc_stop_rx)) {
2245 /* mxser_throttle(tty); */
2250 /* following add by Victor Yu. 09-02-2002 */
2251 if (port->board->chip_flag != MOXA_OTHER_UART) {
2253 if (*status & UART_LSR_SPECIAL)
2255 /* following add by Victor Yu. 02-11-2004 */
2256 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
2257 (*status & MOXA_MUST_LSR_RERR))
2259 /* above add by Victor Yu. 02-14-2004 */
2260 if (*status & MOXA_MUST_LSR_RERR)
2263 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
2265 /* add by Victor Yu. 02-11-2004 */
2266 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
2267 gdl &= MOXA_MUST_GDL_MASK;
2268 if (gdl >= recv_room) {
2269 if (!port->ldisc_stop_rx) {
2270 /* mxser_throttle(tty); */
2276 ch = inb(port->ioaddr + UART_RX);
2277 tty_insert_flip_char(tty, ch, 0);
2283 /* above add by Victor Yu. 09-02-2002 */
2289 ch = inb(port->ioaddr + UART_RX);
2290 /* following add by Victor Yu. 09-02-2002 */
2291 if (port->board->chip_flag && (*status & UART_LSR_OE)
2292 /*&& !(*status&UART_LSR_DR) */)
2293 outb(0x23, port->ioaddr + UART_FCR);
2294 *status &= port->read_status_mask;
2295 /* above add by Victor Yu. 09-02-2002 */
2296 if (*status & port->ignore_status_mask) {
2297 if (++ignored > 100)
2301 if (*status & UART_LSR_SPECIAL) {
2302 if (*status & UART_LSR_BI) {
2304 /* added by casper 1/11/2000 */
2307 if (port->flags & ASYNC_SAK)
2309 } else if (*status & UART_LSR_PE) {
2311 /* added by casper 1/11/2000 */
2312 port->icount.parity++;
2313 } else if (*status & UART_LSR_FE) {
2315 /* added by casper 1/11/2000 */
2316 port->icount.frame++;
2317 } else if (*status & UART_LSR_OE) {
2319 /* added by casper 1/11/2000 */
2320 port->icount.overrun++;
2325 tty_insert_flip_char(tty, ch, flag);
2327 if (cnt >= recv_room) {
2328 if (!port->ldisc_stop_rx) {
2329 /* mxser_throttle(tty); */
2337 /* following add by Victor Yu. 09-02-2002 */
2338 if (port->board->chip_flag)
2341 /* mask by Victor Yu. 09-02-2002
2342 *status = inb(port->ioaddr + UART_LSR) & port->read_status_mask;
2344 /* following add by Victor Yu. 09-02-2002 */
2345 *status = inb(port->ioaddr + UART_LSR);
2346 /* above add by Victor Yu. 09-02-2002 */
2347 } while (*status & UART_LSR_DR);
2349 end_intr: /* add by Victor Yu. 09-02-2002 */
2350 mxvar_log.rxcnt[port->tty->index] += cnt;
2351 port->mon_data.rxcnt += cnt;
2352 port->mon_data.up_rxcnt += cnt;
2353 spin_unlock_irqrestore(&port->slock, flags);
2355 tty_flip_buffer_push(tty);
2358 static void mxser_transmit_chars(struct mxser_port *port)
2361 unsigned long flags;
2363 spin_lock_irqsave(&port->slock, flags);
2366 outb(port->x_char, port->ioaddr + UART_TX);
2368 mxvar_log.txcnt[port->tty->index]++;
2369 port->mon_data.txcnt++;
2370 port->mon_data.up_txcnt++;
2372 /* added by casper 1/11/2000 */
2377 if (port->xmit_buf == 0)
2380 if (port->xmit_cnt == 0) {
2381 if (port->xmit_cnt < WAKEUP_CHARS) { /* XXX what's this for?? */
2382 set_bit(MXSER_EVENT_TXLOW, &port->event);
2383 schedule_work(&port->tqueue);
2387 if (port->tty->stopped || (port->tty->hw_stopped &&
2388 (port->type != PORT_16550A) &&
2389 (!port->board->chip_flag))) {
2390 port->IER &= ~UART_IER_THRI;
2391 outb(port->IER, port->ioaddr + UART_IER);
2395 cnt = port->xmit_cnt;
2396 count = port->xmit_fifo_size;
2398 outb(port->xmit_buf[port->xmit_tail++],
2399 port->ioaddr + UART_TX);
2400 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2401 if (--port->xmit_cnt <= 0)
2403 } while (--count > 0);
2404 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
2406 /* added by James 03-12-2004. */
2407 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2408 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2410 /* added by casper 1/11/2000 */
2411 port->icount.tx += (cnt - port->xmit_cnt);
2413 if (port->xmit_cnt < WAKEUP_CHARS) {
2414 set_bit(MXSER_EVENT_TXLOW, &port->event);
2415 schedule_work(&port->tqueue);
2417 if (port->xmit_cnt <= 0) {
2418 port->IER &= ~UART_IER_THRI;
2419 outb(port->IER, port->ioaddr + UART_IER);
2422 spin_unlock_irqrestore(&port->slock, flags);
2426 * This is the serial driver's generic interrupt routine
2428 static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
2431 struct mxser_board *brd = NULL;
2432 struct mxser_port *port;
2433 int max, irqbits, bits, msr;
2434 int pass_counter = 0;
2435 unsigned int int_cnt;
2436 int handled = IRQ_NONE;
2438 /* spin_lock(&gm_lock); */
2440 for (i = 0; i < MXSER_BOARDS; i++)
2441 if (dev_id == &mxser_boards[i]) {
2446 if (i == MXSER_BOARDS)
2450 max = mxser_numports[brd->board_type - 1];
2452 irqbits = inb(brd->vector) & brd->vector_mask;
2453 if (irqbits == brd->vector_mask)
2456 handled = IRQ_HANDLED;
2457 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2458 if (irqbits == brd->vector_mask)
2462 port = &brd->ports[i];
2466 /* following add by Victor Yu. 09-13-2002 */
2467 iir = inb(port->ioaddr + UART_IIR);
2468 if (iir & UART_IIR_NO_INT)
2470 iir &= MOXA_MUST_IIR_MASK;
2472 status = inb(port->ioaddr + UART_LSR);
2473 outb(0x27, port->ioaddr + UART_FCR);
2474 inb(port->ioaddr + UART_MSR);
2477 /* above add by Victor Yu. 09-13-2002 */
2479 /* following add by Victor Yu. 09-02-2002 */
2480 status = inb(port->ioaddr + UART_LSR);
2482 if (status & UART_LSR_PE)
2483 port->err_shadow |= NPPI_NOTIFY_PARITY;
2484 if (status & UART_LSR_FE)
2485 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2486 if (status & UART_LSR_OE)
2488 NPPI_NOTIFY_HW_OVERRUN;
2489 if (status & UART_LSR_BI)
2490 port->err_shadow |= NPPI_NOTIFY_BREAK;
2492 if (port->board->chip_flag) {
2494 if ( (status & 0x02) && !(status & 0x01) ) {
2495 outb(port->ioaddr+UART_FCR, 0x23);
2499 if (iir == MOXA_MUST_IIR_GDA ||
2500 iir == MOXA_MUST_IIR_RDA ||
2501 iir == MOXA_MUST_IIR_RTO ||
2502 iir == MOXA_MUST_IIR_LSR)
2503 mxser_receive_chars(port,
2507 /* above add by Victor Yu. 09-02-2002 */
2509 status &= port->read_status_mask;
2510 if (status & UART_LSR_DR)
2511 mxser_receive_chars(port,
2514 msr = inb(port->ioaddr + UART_MSR);
2515 if (msr & UART_MSR_ANY_DELTA)
2516 mxser_check_modem_status(port, msr);
2518 /* following add by Victor Yu. 09-13-2002 */
2519 if (port->board->chip_flag) {
2520 if (iir == 0x02 && (status &
2522 mxser_transmit_chars(port);
2524 /* above add by Victor Yu. 09-13-2002 */
2526 if (status & UART_LSR_THRE)
2527 mxser_transmit_chars(port);
2529 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2531 if (pass_counter++ > MXSER_ISR_PASS_LIMIT)
2532 break; /* Prevent infinite loops */
2536 /* spin_unlock(&gm_lock); */
2540 static const struct tty_operations mxser_ops = {
2542 .close = mxser_close,
2543 .write = mxser_write,
2544 .put_char = mxser_put_char,
2545 .flush_chars = mxser_flush_chars,
2546 .write_room = mxser_write_room,
2547 .chars_in_buffer = mxser_chars_in_buffer,
2548 .flush_buffer = mxser_flush_buffer,
2549 .ioctl = mxser_ioctl,
2550 .throttle = mxser_throttle,
2551 .unthrottle = mxser_unthrottle,
2552 .set_termios = mxser_set_termios,
2554 .start = mxser_start,
2555 .hangup = mxser_hangup,
2556 .break_ctl = mxser_rs_break,
2557 .wait_until_sent = mxser_wait_until_sent,
2558 .tiocmget = mxser_tiocmget,
2559 .tiocmset = mxser_tiocmset,
2563 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2566 static void mxser_release_res(struct mxser_board *brd, unsigned int irq)
2568 struct pci_dev *pdev = brd->pdev;
2571 free_irq(brd->irq, brd);
2572 if (pdev != NULL) { /* PCI */
2573 pci_release_region(pdev, 2);
2574 pci_release_region(pdev, 3);
2577 release_region(brd->ports[0].ioaddr, 8 * brd->nports);
2578 release_region(brd->vector, 1);
2582 static int __devinit mxser_initbrd(struct mxser_board *brd)
2584 struct mxser_port *info;
2588 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
2590 for (i = 0; i < brd->nports; i++) {
2591 info = &brd->ports[i];
2594 info->ldisc_stop_rx = 0;
2596 /* Enhance mode enabled here */
2597 if (brd->chip_flag != MOXA_OTHER_UART)
2598 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
2600 info->flags = ASYNC_SHARE_IRQ;
2601 info->type = brd->uart_type;
2603 process_txrx_fifo(info);
2605 info->custom_divisor = info->baud_base * 16;
2606 info->close_delay = 5 * HZ / 10;
2607 info->closing_wait = 30 * HZ;
2608 INIT_WORK(&info->tqueue, mxser_do_softint, info);
2609 info->normal_termios = mxvar_sdriver->init_termios;
2610 init_waitqueue_head(&info->open_wait);
2611 init_waitqueue_head(&info->close_wait);
2612 init_waitqueue_head(&info->delta_msr_wait);
2613 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2614 info->err_shadow = 0;
2615 spin_lock_init(&info->slock);
2617 /* before set INT ISR, disable all int */
2618 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2619 info->ioaddr + UART_IER);
2622 * Allocate the IRQ if necessary
2625 retval = request_irq(brd->irq, mxser_interrupt,
2626 (brd->ports[0].flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED :
2627 IRQF_DISABLED, "mxser", brd);
2629 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2630 "conflict with another device.\n",
2631 mxser_brdname[brd->board_type - 1], brd->irq);
2632 /* We hold resources, we need to release them. */
2633 mxser_release_res(brd, 0);
2639 static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
2642 unsigned short regs[16], irq;
2643 unsigned char scratch, scratch2;
2645 brd->chip_flag = MOXA_OTHER_UART;
2647 id = mxser_read_register(cap, regs);
2648 if (id == C168_ASIC_ID) {
2649 brd->board_type = MXSER_BOARD_C168_ISA;
2651 } else if (id == C104_ASIC_ID) {
2652 brd->board_type = MXSER_BOARD_C104_ISA;
2654 } else if (id == C102_ASIC_ID) {
2655 brd->board_type = MXSER_BOARD_C102_ISA;
2657 } else if (id == CI132_ASIC_ID) {
2658 brd->board_type = MXSER_BOARD_CI132;
2660 } else if (id == CI134_ASIC_ID) {
2661 brd->board_type = MXSER_BOARD_CI134;
2663 } else if (id == CI104J_ASIC_ID) {
2664 brd->board_type = MXSER_BOARD_CI104J;
2670 if (brd->nports == 2) {
2671 irq = regs[9] & 0xF000;
2672 irq = irq | (irq >> 4);
2673 if (irq != (regs[9] & 0xFF00))
2674 return MXSER_ERR_IRQ_CONFLIT;
2675 } else if (brd->nports == 4) {
2676 irq = regs[9] & 0xF000;
2677 irq = irq | (irq >> 4);
2678 irq = irq | (irq >> 8);
2680 return MXSER_ERR_IRQ_CONFLIT;
2681 } else if (brd->nports == 8) {
2682 irq = regs[9] & 0xF000;
2683 irq = irq | (irq >> 4);
2684 irq = irq | (irq >> 8);
2685 if ((irq != regs[9]) || (irq != regs[10]))
2686 return MXSER_ERR_IRQ_CONFLIT;
2690 return MXSER_ERR_IRQ;
2691 brd->irq = ((int)(irq & 0xF000) >> 12);
2692 for (i = 0; i < 8; i++)
2693 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
2694 if ((regs[12] & 0x80) == 0)
2695 return MXSER_ERR_VECTOR;
2696 brd->vector = (int)regs[11]; /* interrupt vector */
2698 brd->vector_mask = 0x00FF;
2700 brd->vector_mask = 0x000F;
2701 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2702 if (regs[12] & bits) {
2703 brd->ports[i].baud_base = 921600;
2704 brd->ports[i].max_baud = 921600; /* add by Victor Yu. 09-04-2002 */
2706 brd->ports[i].baud_base = 115200;
2707 brd->ports[i].max_baud = 115200; /* add by Victor Yu. 09-04-2002 */
2710 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2711 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2712 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2713 outb(scratch2, cap + UART_LCR);
2714 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2715 scratch = inb(cap + UART_IIR);
2718 brd->uart_type = PORT_16550A;
2720 brd->uart_type = PORT_16450;
2725 if (!request_region(brd->ports[0].ioaddr, 8 * brd->nports, "mxser(IO)"))
2726 return MXSER_ERR_IOADDR;
2727 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2728 release_region(brd->ports[0].ioaddr, 8 * brd->nports);
2729 return MXSER_ERR_VECTOR;
2734 static int __init mxser_get_PCI_conf(int board_type, struct mxser_board *brd,
2735 struct pci_dev *pdev)
2738 unsigned long ioaddress;
2742 brd->board_type = board_type;
2743 brd->nports = mxser_numports[board_type - 1];
2744 ioaddress = pci_resource_start(pdev, 2);
2745 retval = pci_request_region(pdev, 2, "mxser(IO)");
2749 for (i = 0; i < brd->nports; i++)
2750 brd->ports[i].ioaddr = ioaddress + 8 * i;
2753 ioaddress = pci_resource_start(pdev, 3);
2754 retval = pci_request_region(pdev, 3, "mxser(vector)");
2757 brd->vector = ioaddress;
2760 brd->irq = pdev->irq;
2762 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2763 brd->uart_type = PORT_16550A;
2764 brd->vector_mask = 0;
2766 for (i = 0; i < brd->nports; i++) {
2767 for (j = 0; j < UART_INFO_NUM; j++) {
2768 if (Gpci_uart_info[j].type == brd->chip_flag) {
2769 brd->ports[i].max_baud =
2770 Gpci_uart_info[j].max_baud;
2772 /* exception....CP-102 */
2773 if (board_type == MXSER_BOARD_CP102)
2774 brd->ports[i].max_baud = 921600;
2780 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2781 for (i = 0; i < brd->nports; i++) {
2783 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2785 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
2787 outb(0, ioaddress + 4); /* default set to RS232 mode */
2788 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
2791 for (i = 0; i < brd->nports; i++) {
2792 brd->vector_mask |= (1 << i);
2793 brd->ports[i].baud_base = 921600;
2797 pci_release_region(pdev, 2);
2802 static int __init mxser_module_init(void)
2804 struct pci_dev *pdev = NULL;
2805 struct mxser_board *brd;
2807 unsigned int i, m, isaloop;
2810 pr_debug("Loading module mxser ...\n");
2812 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2815 spin_lock_init(&gm_lock);
2817 for (i = 0; i < MXSER_BOARDS; i++)
2818 mxser_boards[i].board_type = -1;
2820 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2823 /* Initialize the tty_driver structure */
2824 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2825 mxvar_sdriver->name = "ttyM";
2826 mxvar_sdriver->major = ttymajor;
2827 mxvar_sdriver->minor_start = 0;
2828 mxvar_sdriver->num = MXSER_PORTS + 1;
2829 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2830 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2831 mxvar_sdriver->init_termios = tty_std_termios;
2832 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2833 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2834 tty_set_operations(mxvar_sdriver, &mxser_ops);
2835 mxvar_sdriver->ttys = mxvar_tty;
2836 mxvar_sdriver->termios = mxvar_termios;
2837 mxvar_sdriver->termios_locked = mxvar_termios_locked;
2839 retval = tty_register_driver(mxvar_sdriver);
2841 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2849 /* Start finding ISA boards here */
2850 for (isaloop = 0; isaloop < 2; isaloop++)
2851 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2853 cap = mxserBoardCAP[b]; /* predefined */
2855 cap = ioaddr[b]; /* module param */
2860 brd = &mxser_boards[m];
2861 retval = mxser_get_ISA_conf(cap, brd);
2864 printk(KERN_INFO "Found MOXA %s board "
2866 mxser_brdname[brd->board_type - 1],
2870 if (retval == MXSER_ERR_IRQ)
2871 printk(KERN_ERR "Invalid interrupt "
2872 "number, board not "
2874 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2875 printk(KERN_ERR "Invalid interrupt "
2876 "number, board not "
2878 else if (retval == MXSER_ERR_VECTOR)
2879 printk(KERN_ERR "Invalid interrupt "
2880 "vector, board not "
2882 else if (retval == MXSER_ERR_IOADDR)
2883 printk(KERN_ERR "Invalid I/O address, "
2884 "board not configured\n");
2891 /* mxser_initbrd will hook ISR. */
2892 if (mxser_initbrd(brd) < 0)
2895 for (i = 0; i < brd->nports; i++)
2896 tty_register_device(mxvar_sdriver,
2897 m * MXSER_PORTS_PER_BOARD + i, NULL);
2902 /* start finding PCI board here */
2903 n = ARRAY_SIZE(mxser_pcibrds) - 1;
2906 pdev = pci_get_device(mxser_pcibrds[b].vendor,
2907 mxser_pcibrds[b].device, pdev);
2912 printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
2913 mxser_brdname[(int) (mxser_pcibrds[b].driver_data) - 1],
2914 pdev->bus->number, PCI_SLOT(pdev->devfn));
2915 if (m >= MXSER_BOARDS)
2917 "Too many Smartio/Industio family boards find "
2918 "(maximum %d), board not configured\n",
2921 if (pci_enable_device(pdev)) {
2922 printk(KERN_ERR "Moxa SmartI/O PCI enable "
2926 brd = &mxser_boards[m];
2928 retval = mxser_get_PCI_conf(
2929 (int)mxser_pcibrds[b].driver_data,
2932 if (retval == MXSER_ERR_IRQ)
2934 "Invalid interrupt number, "
2935 "board not configured\n");
2936 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2938 "Invalid interrupt number, "
2939 "board not configured\n");
2940 else if (retval == MXSER_ERR_VECTOR)
2942 "Invalid interrupt vector, "
2943 "board not configured\n");
2944 else if (retval == MXSER_ERR_IOADDR)
2946 "Invalid I/O address, "
2947 "board not configured\n");
2950 /* mxser_initbrd will hook ISR. */
2951 if (mxser_initbrd(brd) < 0)
2953 for (i = 0; i < brd->nports; i++)
2954 tty_register_device(mxvar_sdriver,
2955 m * MXSER_PORTS_PER_BOARD + i,
2959 /* Keep an extra reference if we succeeded. It will
2960 be returned at unload time */
2970 pr_debug("Done.\n");
2974 tty_unregister_driver(mxvar_sdriver);
2976 put_tty_driver(mxvar_sdriver);
2980 static void __exit mxser_module_exit(void)
2984 pr_debug("Unloading module mxser ...\n");
2986 for (i = 0; i < MXSER_PORTS; i++)
2987 tty_unregister_device(mxvar_sdriver, i);
2988 tty_unregister_driver(mxvar_sdriver);
2989 put_tty_driver(mxvar_sdriver);
2991 for (i = 0; i < MXSER_BOARDS; i++)
2992 if (mxser_boards[i].board_type != -1)
2993 mxser_release_res(&mxser_boards[i], 1);
2995 pr_debug("Done.\n");
2998 module_init(mxser_module_init);
2999 module_exit(mxser_module_exit);