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) || (tty->flags & (1 << TTY_IO_ERROR))) {
465 port->flags |= ASYNC_NORMAL_ACTIVE;
469 if (tty->termios->c_cflag & CLOCAL)
473 * Block waiting for the carrier detect and the line to become
474 * free (i.e., not in use by the callout). While we are in
475 * this loop, port->count is dropped by one, so that
476 * mxser_close() knows when to free things. We restore it upon
477 * exit, either normal or abnormal.
480 add_wait_queue(&port->open_wait, &wait);
482 spin_lock_irqsave(&port->slock, flags);
483 if (!tty_hung_up_p(filp))
485 spin_unlock_irqrestore(&port->slock, flags);
486 port->blocked_open++;
488 spin_lock_irqsave(&port->slock, flags);
489 outb(inb(port->ioaddr + UART_MCR) |
490 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
491 spin_unlock_irqrestore(&port->slock, flags);
492 set_current_state(TASK_INTERRUPTIBLE);
493 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
494 if (port->flags & ASYNC_HUP_NOTIFY)
497 retval = -ERESTARTSYS;
500 if (!(port->flags & ASYNC_CLOSING) &&
502 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
504 if (signal_pending(current)) {
505 retval = -ERESTARTSYS;
510 set_current_state(TASK_RUNNING);
511 remove_wait_queue(&port->open_wait, &wait);
512 if (!tty_hung_up_p(filp))
514 port->blocked_open--;
517 port->flags |= ASYNC_NORMAL_ACTIVE;
521 static int mxser_set_baud(struct mxser_port *info, long newspd)
528 if (!info->tty || !info->tty->termios)
534 if (newspd > info->max_baud)
537 info->realbaud = newspd;
539 quot = (2 * info->baud_base / 269);
541 quot = info->baud_base / newspd;
548 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
549 info->timeout += HZ / 50; /* Add .02 seconds of slop */
552 spin_lock_irqsave(&info->slock, flags);
553 info->MCR |= UART_MCR_DTR;
554 outb(info->MCR, info->ioaddr + UART_MCR);
555 spin_unlock_irqrestore(&info->slock, flags);
557 spin_lock_irqsave(&info->slock, flags);
558 info->MCR &= ~UART_MCR_DTR;
559 outb(info->MCR, info->ioaddr + UART_MCR);
560 spin_unlock_irqrestore(&info->slock, flags);
564 cval = inb(info->ioaddr + UART_LCR);
566 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
568 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
569 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
570 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
577 * This routine is called to set the UART divisor registers to match
578 * the specified baud rate for a serial port.
580 static int mxser_change_speed(struct mxser_port *info,
581 struct termios *old_termios)
583 unsigned cflag, cval, fcr;
585 unsigned char status;
589 if (!info->tty || !info->tty->termios)
591 cflag = info->tty->termios->c_cflag;
596 #define B921600 (B460800 +1)
598 if (mxser_set_baud_method[info->tty->index] == 0) {
599 baud = tty_get_baud_rate(info->tty);
600 mxser_set_baud(info, baud);
603 /* byte size and parity */
604 switch (cflag & CSIZE) {
619 break; /* too keep GCC shut... */
624 cval |= UART_LCR_PARITY;
625 if (!(cflag & PARODD))
626 cval |= UART_LCR_EPAR;
628 cval |= UART_LCR_SPAR;
630 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
631 if (info->board->chip_flag) {
632 fcr = UART_FCR_ENABLE_FIFO;
633 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
634 SET_MOXA_MUST_FIFO_VALUE(info);
638 fcr = UART_FCR_ENABLE_FIFO;
639 /* following add by Victor Yu. 08-30-2002 */
640 if (info->board->chip_flag) {
641 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
642 SET_MOXA_MUST_FIFO_VALUE(info);
644 /* above add by Victor Yu. 08-30-2002 */
645 switch (info->rx_trigger) {
647 fcr |= UART_FCR_TRIGGER_1;
650 fcr |= UART_FCR_TRIGGER_4;
653 fcr |= UART_FCR_TRIGGER_8;
656 fcr |= UART_FCR_TRIGGER_14;
662 /* CTS flow control flag and modem status interrupts */
663 info->IER &= ~UART_IER_MSI;
664 info->MCR &= ~UART_MCR_AFE;
665 if (cflag & CRTSCTS) {
666 info->flags |= ASYNC_CTS_FLOW;
667 info->IER |= UART_IER_MSI;
668 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
669 info->MCR |= UART_MCR_AFE;
670 /* status = mxser_get_msr(info->ioaddr, 0, info->port); */
674 status = inb(baseaddr + UART_MSR);
675 restore_flags(flags);
677 /* mxser_check_modem_status(info, status); */
679 /* status = mxser_get_msr(info->ioaddr, 0, info->port); */
680 /* MX_LOCK(&info->slock); */
681 status = inb(info->ioaddr + UART_MSR);
682 /* MX_UNLOCK(&info->slock); */
683 if (info->tty->hw_stopped) {
684 if (status & UART_MSR_CTS) {
685 info->tty->hw_stopped = 0;
686 if (info->type != PORT_16550A &&
687 !info->board->chip_flag) {
688 outb(info->IER & ~UART_IER_THRI,
691 info->IER |= UART_IER_THRI;
692 outb(info->IER, info->ioaddr +
695 set_bit(MXSER_EVENT_TXLOW, &info->event);
696 schedule_work(&info->tqueue); }
698 if (!(status & UART_MSR_CTS)) {
699 info->tty->hw_stopped = 1;
700 if ((info->type != PORT_16550A) &&
701 (!info->board->chip_flag)) {
702 info->IER &= ~UART_IER_THRI;
703 outb(info->IER, info->ioaddr +
710 info->flags &= ~ASYNC_CTS_FLOW;
712 outb(info->MCR, info->ioaddr + UART_MCR);
713 if (cflag & CLOCAL) {
714 info->flags &= ~ASYNC_CHECK_CD;
716 info->flags |= ASYNC_CHECK_CD;
717 info->IER |= UART_IER_MSI;
719 outb(info->IER, info->ioaddr + UART_IER);
722 * Set up parity check flag
724 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
725 if (I_INPCK(info->tty))
726 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
727 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
728 info->read_status_mask |= UART_LSR_BI;
730 info->ignore_status_mask = 0;
732 if (I_IGNBRK(info->tty)) {
733 info->ignore_status_mask |= UART_LSR_BI;
734 info->read_status_mask |= UART_LSR_BI;
736 * If we're ignore parity and break indicators, ignore
737 * overruns too. (For real raw support).
739 if (I_IGNPAR(info->tty)) {
740 info->ignore_status_mask |=
744 info->read_status_mask |=
750 /* following add by Victor Yu. 09-02-2002 */
751 if (info->board->chip_flag) {
752 spin_lock_irqsave(&info->slock, flags);
753 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
754 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
755 if (I_IXON(info->tty)) {
756 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
758 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
760 if (I_IXOFF(info->tty)) {
761 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
763 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
766 if ( I_IXANY(info->tty) ) {
767 info->MCR |= MOXA_MUST_MCR_XON_ANY;
768 ENABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
770 info->MCR &= ~MOXA_MUST_MCR_XON_ANY;
771 DISABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
774 spin_unlock_irqrestore(&info->slock, flags);
776 /* above add by Victor Yu. 09-02-2002 */
779 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
780 outb(cval, info->ioaddr + UART_LCR);
785 static void mxser_check_modem_status(struct mxser_port *port, int status)
787 /* update input line counters */
788 if (status & UART_MSR_TERI)
790 if (status & UART_MSR_DDSR)
792 if (status & UART_MSR_DDCD)
794 if (status & UART_MSR_DCTS)
796 port->mon_data.modem_status = status;
797 wake_up_interruptible(&port->delta_msr_wait);
799 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
800 if (status & UART_MSR_DCD)
801 wake_up_interruptible(&port->open_wait);
802 schedule_work(&port->tqueue);
805 if (port->flags & ASYNC_CTS_FLOW) {
806 if (port->tty->hw_stopped) {
807 if (status & UART_MSR_CTS) {
808 port->tty->hw_stopped = 0;
810 if ((port->type != PORT_16550A) &&
811 (!port->board->chip_flag)) {
812 outb(port->IER & ~UART_IER_THRI,
813 port->ioaddr + UART_IER);
814 port->IER |= UART_IER_THRI;
815 outb(port->IER, port->ioaddr +
818 set_bit(MXSER_EVENT_TXLOW, &port->event);
819 schedule_work(&port->tqueue);
822 if (!(status & UART_MSR_CTS)) {
823 port->tty->hw_stopped = 1;
824 if (port->type != PORT_16550A &&
825 !port->board->chip_flag) {
826 port->IER &= ~UART_IER_THRI;
827 outb(port->IER, port->ioaddr +
835 static int mxser_startup(struct mxser_port *info)
840 page = __get_free_page(GFP_KERNEL);
844 spin_lock_irqsave(&info->slock, flags);
846 if (info->flags & ASYNC_INITIALIZED) {
848 spin_unlock_irqrestore(&info->slock, flags);
852 if (!info->ioaddr || !info->type) {
854 set_bit(TTY_IO_ERROR, &info->tty->flags);
856 spin_unlock_irqrestore(&info->slock, flags);
862 info->xmit_buf = (unsigned char *) page;
865 * Clear the FIFO buffers and disable them
866 * (they will be reenabled in mxser_change_speed())
868 if (info->board->chip_flag)
869 outb((UART_FCR_CLEAR_RCVR |
870 UART_FCR_CLEAR_XMIT |
871 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
873 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
874 info->ioaddr + UART_FCR);
877 * At this point there's no way the LSR could still be 0xFF;
878 * if it is, then bail out, because there's likely no UART
881 if (inb(info->ioaddr + UART_LSR) == 0xff) {
882 spin_unlock_irqrestore(&info->slock, flags);
883 if (capable(CAP_SYS_ADMIN)) {
885 set_bit(TTY_IO_ERROR, &info->tty->flags);
892 * Clear the interrupt registers.
894 (void) inb(info->ioaddr + UART_LSR);
895 (void) inb(info->ioaddr + UART_RX);
896 (void) inb(info->ioaddr + UART_IIR);
897 (void) inb(info->ioaddr + UART_MSR);
900 * Now, initialize the UART
902 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
903 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
904 outb(info->MCR, info->ioaddr + UART_MCR);
907 * Finally, enable interrupts
909 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
910 /* info->IER = UART_IER_RLSI | UART_IER_RDI; */
912 /* following add by Victor Yu. 08-30-2002 */
913 if (info->board->chip_flag)
914 info->IER |= MOXA_MUST_IER_EGDAI;
915 /* above add by Victor Yu. 08-30-2002 */
916 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
919 * And clear the interrupt registers again for luck.
921 (void) inb(info->ioaddr + UART_LSR);
922 (void) inb(info->ioaddr + UART_RX);
923 (void) inb(info->ioaddr + UART_IIR);
924 (void) inb(info->ioaddr + UART_MSR);
927 clear_bit(TTY_IO_ERROR, &info->tty->flags);
928 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
931 * and set the speed of the serial port
933 spin_unlock_irqrestore(&info->slock, flags);
934 mxser_change_speed(info, NULL);
936 info->flags |= ASYNC_INITIALIZED;
941 * This routine will shutdown a serial port; interrupts maybe disabled, and
942 * DTR is dropped if the hangup on close termio flag is on.
944 static void mxser_shutdown(struct mxser_port *info)
948 if (!(info->flags & ASYNC_INITIALIZED))
951 spin_lock_irqsave(&info->slock, flags);
954 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
955 * here so the queue might never be waken up
957 wake_up_interruptible(&info->delta_msr_wait);
960 * Free the IRQ, if necessary
962 if (info->xmit_buf) {
963 free_page((unsigned long) info->xmit_buf);
964 info->xmit_buf = NULL;
968 outb(0x00, info->ioaddr + UART_IER);
970 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
971 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
972 outb(info->MCR, info->ioaddr + UART_MCR);
974 /* clear Rx/Tx FIFO's */
975 /* following add by Victor Yu. 08-30-2002 */
976 if (info->board->chip_flag)
977 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
978 MOXA_MUST_FCR_GDA_MODE_ENABLE,
979 info->ioaddr + UART_FCR);
981 /* above add by Victor Yu. 08-30-2002 */
982 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
983 info->ioaddr + UART_FCR);
985 /* read data port to reset things */
986 (void) inb(info->ioaddr + UART_RX);
989 set_bit(TTY_IO_ERROR, &info->tty->flags);
991 info->flags &= ~ASYNC_INITIALIZED;
993 /* following add by Victor Yu. 09-23-2002 */
994 if (info->board->chip_flag)
995 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
996 /* above add by Victor Yu. 09-23-2002 */
998 spin_unlock_irqrestore(&info->slock, flags);
1002 * This routine is called whenever a serial port is opened. It
1003 * enables interrupts for a serial port, linking in its async structure into
1004 * the IRQ chain. It also performs the serial-specific
1005 * initialization for the tty structure.
1007 static int mxser_open(struct tty_struct *tty, struct file *filp)
1009 struct mxser_port *info;
1012 /* initialize driver_data in case something fails */
1013 tty->driver_data = NULL;
1016 if (line == MXSER_PORTS)
1018 if (line < 0 || line > MXSER_PORTS)
1020 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
1024 tty->driver_data = info;
1027 * Start up serial port
1030 retval = mxser_startup(info);
1034 retval = mxser_block_til_ready(tty, filp, info);
1038 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
1039 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
1040 *tty->termios = info->normal_termios;
1042 *tty->termios = info->callout_termios;
1043 mxser_change_speed(info, NULL);
1046 info->session = process_session(current);
1047 info->pgrp = process_group(current);
1050 status = mxser_get_msr(info->base, 0, info->port);
1051 mxser_check_modem_status(info, status);
1054 /* unmark here for very high baud rate (ex. 921600 bps) used */
1055 tty->low_latency = 1;
1060 * This routine is called when the serial port gets closed. First, we
1061 * wait for the last remaining data to be sent. Then, we unlink its
1062 * async structure from the interrupt chain if necessary, and we free
1063 * that IRQ if nothing is left in the chain.
1065 static void mxser_close(struct tty_struct *tty, struct file *filp)
1067 struct mxser_port *info = tty->driver_data;
1069 unsigned long timeout;
1070 unsigned long flags;
1071 struct tty_ldisc *ld;
1073 if (tty->index == MXSER_PORTS)
1078 spin_lock_irqsave(&info->slock, flags);
1080 if (tty_hung_up_p(filp)) {
1081 spin_unlock_irqrestore(&info->slock, flags);
1084 if ((tty->count == 1) && (info->count != 1)) {
1086 * Uh, oh. tty->count is 1, which means that the tty
1087 * structure will be freed. Info->count should always
1088 * be one in these conditions. If it's greater than
1089 * one, we've got real problems, since it means the
1090 * serial port won't be shutdown.
1092 printk(KERN_ERR "mxser_close: bad serial port count; "
1093 "tty->count is 1, info->count is %d\n", info->count);
1096 if (--info->count < 0) {
1097 printk(KERN_ERR "mxser_close: bad serial port count for "
1098 "ttys%d: %d\n", tty->index, info->count);
1102 spin_unlock_irqrestore(&info->slock, flags);
1105 info->flags |= ASYNC_CLOSING;
1106 spin_unlock_irqrestore(&info->slock, flags);
1108 * Save the termios structure, since this port may have
1109 * separate termios for callout and dialin.
1111 if (info->flags & ASYNC_NORMAL_ACTIVE)
1112 info->normal_termios = *tty->termios;
1114 * Now we wait for the transmit buffer to clear; and we notify
1115 * the line discipline to only process XON/XOFF characters.
1118 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1119 tty_wait_until_sent(tty, info->closing_wait);
1121 * At this point we stop accepting input. To do this, we
1122 * disable the receive line status interrupts, and tell the
1123 * interrupt driver to stop checking the data ready bit in the
1124 * line status register.
1126 info->IER &= ~UART_IER_RLSI;
1127 if (info->board->chip_flag)
1128 info->IER &= ~MOXA_MUST_RECV_ISR;
1130 info->read_status_mask &= ~UART_LSR_DR;
1132 if (info->flags & ASYNC_INITIALIZED) {
1133 outb(info->IER, info->ioaddr + UART_IER);
1135 * Before we drop DTR, make sure the UART transmitter
1136 * has completely drained; this is especially
1137 * important if there is a transmit FIFO!
1139 timeout = jiffies + HZ;
1140 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
1141 schedule_timeout_interruptible(5);
1142 if (time_after(jiffies, timeout))
1146 mxser_shutdown(info);
1148 if (tty->driver->flush_buffer)
1149 tty->driver->flush_buffer(tty);
1151 ld = tty_ldisc_ref(tty);
1153 if (ld->flush_buffer)
1154 ld->flush_buffer(tty);
1155 tty_ldisc_deref(ld);
1161 if (info->blocked_open) {
1162 if (info->close_delay)
1163 schedule_timeout_interruptible(info->close_delay);
1164 wake_up_interruptible(&info->open_wait);
1167 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1168 wake_up_interruptible(&info->close_wait);
1172 static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1175 struct mxser_port *info = tty->driver_data;
1176 unsigned long flags;
1178 if (!info->xmit_buf)
1182 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1183 SERIAL_XMIT_SIZE - info->xmit_head));
1187 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1188 spin_lock_irqsave(&info->slock, flags);
1189 info->xmit_head = (info->xmit_head + c) &
1190 (SERIAL_XMIT_SIZE - 1);
1191 info->xmit_cnt += c;
1192 spin_unlock_irqrestore(&info->slock, flags);
1199 if (info->xmit_cnt && !tty->stopped
1200 /*&& !(info->IER & UART_IER_THRI)*/) {
1201 if (!tty->hw_stopped ||
1202 (info->type == PORT_16550A) ||
1203 (info->board->chip_flag)) {
1204 spin_lock_irqsave(&info->slock, flags);
1205 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1207 info->IER |= UART_IER_THRI;
1208 outb(info->IER, info->ioaddr + UART_IER);
1209 spin_unlock_irqrestore(&info->slock, flags);
1215 static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1217 struct mxser_port *info = tty->driver_data;
1218 unsigned long flags;
1220 if (!info->xmit_buf)
1223 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1226 spin_lock_irqsave(&info->slock, flags);
1227 info->xmit_buf[info->xmit_head++] = ch;
1228 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1230 spin_unlock_irqrestore(&info->slock, flags);
1231 if (!tty->stopped /*&& !(info->IER & UART_IER_THRI)*/) {
1232 if (!tty->hw_stopped ||
1233 (info->type == PORT_16550A) ||
1234 info->board->chip_flag) {
1235 spin_lock_irqsave(&info->slock, flags);
1236 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1237 info->IER |= UART_IER_THRI;
1238 outb(info->IER, info->ioaddr + UART_IER);
1239 spin_unlock_irqrestore(&info->slock, flags);
1245 static void mxser_flush_chars(struct tty_struct *tty)
1247 struct mxser_port *info = tty->driver_data;
1248 unsigned long flags;
1250 if (info->xmit_cnt <= 0 ||
1254 (info->type != PORT_16550A) &&
1255 (!info->board->chip_flag)
1259 spin_lock_irqsave(&info->slock, flags);
1261 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1262 info->IER |= UART_IER_THRI;
1263 outb(info->IER, info->ioaddr + UART_IER);
1265 spin_unlock_irqrestore(&info->slock, flags);
1268 static int mxser_write_room(struct tty_struct *tty)
1270 struct mxser_port *info = tty->driver_data;
1273 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1279 static int mxser_chars_in_buffer(struct tty_struct *tty)
1281 struct mxser_port *info = tty->driver_data;
1282 int len = info->xmit_cnt;
1284 if (!(inb(info->ioaddr + UART_LSR) & UART_LSR_THRE))
1290 static void mxser_flush_buffer(struct tty_struct *tty)
1292 struct mxser_port *info = tty->driver_data;
1294 unsigned long flags;
1297 spin_lock_irqsave(&info->slock, flags);
1298 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1300 /* below added by shinhay */
1301 fcr = inb(info->ioaddr + UART_FCR);
1302 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1303 info->ioaddr + UART_FCR);
1304 outb(fcr, info->ioaddr + UART_FCR);
1306 spin_unlock_irqrestore(&info->slock, flags);
1307 /* above added by shinhay */
1309 wake_up_interruptible(&tty->write_wait);
1310 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
1311 (tty->ldisc.write_wakeup) (tty);
1315 * ------------------------------------------------------------
1316 * friends of mxser_ioctl()
1317 * ------------------------------------------------------------
1319 static int mxser_get_serial_info(struct mxser_port *info,
1320 struct serial_struct __user *retinfo)
1322 struct serial_struct tmp;
1326 memset(&tmp, 0, sizeof(tmp));
1327 tmp.type = info->type;
1328 tmp.line = info->tty->index;
1329 tmp.port = info->ioaddr;
1330 tmp.irq = info->board->irq;
1331 tmp.flags = info->flags;
1332 tmp.baud_base = info->baud_base;
1333 tmp.close_delay = info->close_delay;
1334 tmp.closing_wait = info->closing_wait;
1335 tmp.custom_divisor = info->custom_divisor;
1337 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1342 static int mxser_set_serial_info(struct mxser_port *info,
1343 struct serial_struct __user *new_info)
1345 struct serial_struct new_serial;
1349 if (!new_info || !info->ioaddr)
1351 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1354 if ((new_serial.irq != info->board->irq) ||
1355 (new_serial.port != info->ioaddr) ||
1356 (new_serial.custom_divisor != info->custom_divisor) ||
1357 (new_serial.baud_base != info->baud_base))
1360 flags = info->flags & ASYNC_SPD_MASK;
1362 if (!capable(CAP_SYS_ADMIN)) {
1363 if ((new_serial.baud_base != info->baud_base) ||
1364 (new_serial.close_delay != info->close_delay) ||
1365 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
1367 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1368 (new_serial.flags & ASYNC_USR_MASK));
1371 * OK, past this point, all the error checking has been done.
1372 * At this point, we start making changes.....
1374 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1375 (new_serial.flags & ASYNC_FLAGS));
1376 info->close_delay = new_serial.close_delay * HZ / 100;
1377 info->closing_wait = new_serial.closing_wait * HZ / 100;
1378 info->tty->low_latency =
1379 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1380 info->tty->low_latency = 0; /* (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0; */
1383 /* added by casper, 3/17/2000, for mouse */
1384 info->type = new_serial.type;
1386 process_txrx_fifo(info);
1388 if (info->flags & ASYNC_INITIALIZED) {
1389 if (flags != (info->flags & ASYNC_SPD_MASK))
1390 mxser_change_speed(info, NULL);
1392 retval = mxser_startup(info);
1398 * mxser_get_lsr_info - get line status register info
1400 * Purpose: Let user call ioctl() to get info when the UART physically
1401 * is emptied. On bus types like RS485, the transmitter must
1402 * release the bus after transmitting. This must be done when
1403 * the transmit shift register is empty, not be done when the
1404 * transmit holding register is empty. This functionality
1405 * allows an RS485 driver to be written in user space.
1407 static int mxser_get_lsr_info(struct mxser_port *info,
1408 unsigned int __user *value)
1410 unsigned char status;
1411 unsigned int result;
1412 unsigned long flags;
1414 spin_lock_irqsave(&info->slock, flags);
1415 status = inb(info->ioaddr + UART_LSR);
1416 spin_unlock_irqrestore(&info->slock, flags);
1417 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1418 return put_user(result, value);
1422 * This routine sends a break character out the serial port.
1424 static void mxser_send_break(struct mxser_port *info, int duration)
1426 unsigned long flags;
1430 set_current_state(TASK_INTERRUPTIBLE);
1431 spin_lock_irqsave(&info->slock, flags);
1432 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1433 info->ioaddr + UART_LCR);
1434 spin_unlock_irqrestore(&info->slock, flags);
1435 schedule_timeout(duration);
1436 spin_lock_irqsave(&info->slock, flags);
1437 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1438 info->ioaddr + UART_LCR);
1439 spin_unlock_irqrestore(&info->slock, flags);
1442 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
1444 struct mxser_port *info = tty->driver_data;
1445 unsigned char control, status;
1446 unsigned long flags;
1449 if (tty->index == MXSER_PORTS)
1450 return -ENOIOCTLCMD;
1451 if (tty->flags & (1 << TTY_IO_ERROR))
1454 control = info->MCR;
1456 spin_lock_irqsave(&info->slock, flags);
1457 status = inb(info->ioaddr + UART_MSR);
1458 if (status & UART_MSR_ANY_DELTA)
1459 mxser_check_modem_status(info, status);
1460 spin_unlock_irqrestore(&info->slock, flags);
1461 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1462 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1463 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1464 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1465 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1466 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1469 static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1470 unsigned int set, unsigned int clear)
1472 struct mxser_port *info = tty->driver_data;
1473 unsigned long flags;
1476 if (tty->index == MXSER_PORTS)
1477 return -ENOIOCTLCMD;
1478 if (tty->flags & (1 << TTY_IO_ERROR))
1481 spin_lock_irqsave(&info->slock, flags);
1483 if (set & TIOCM_RTS)
1484 info->MCR |= UART_MCR_RTS;
1485 if (set & TIOCM_DTR)
1486 info->MCR |= UART_MCR_DTR;
1488 if (clear & TIOCM_RTS)
1489 info->MCR &= ~UART_MCR_RTS;
1490 if (clear & TIOCM_DTR)
1491 info->MCR &= ~UART_MCR_DTR;
1493 outb(info->MCR, info->ioaddr + UART_MCR);
1494 spin_unlock_irqrestore(&info->slock, flags);
1498 static int mxser_program_mode(int port)
1501 /* unsigned long flags; */
1503 spin_lock(&gm_lock);
1511 /* restore_flags(flags); */
1512 spin_unlock(&gm_lock);
1514 id = inb(port + 1) & 0x1F;
1515 if ((id != C168_ASIC_ID) &&
1516 (id != C104_ASIC_ID) &&
1517 (id != C102_ASIC_ID) &&
1518 (id != CI132_ASIC_ID) &&
1519 (id != CI134_ASIC_ID) &&
1520 (id != CI104J_ASIC_ID))
1522 for (i = 0, j = 0; i < 4; i++) {
1526 } else if ((j == 1) && (n == 1)) {
1537 static void mxser_normal_mode(int port)
1541 outb(0xA5, port + 1);
1542 outb(0x80, port + 3);
1543 outb(12, port + 0); /* 9600 bps */
1545 outb(0x03, port + 3); /* 8 data bits */
1546 outb(0x13, port + 4); /* loop back mode */
1547 for (i = 0; i < 16; i++) {
1549 if ((n & 0x61) == 0x60)
1554 outb(0x00, port + 4);
1557 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1558 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1559 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1560 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1561 #define EN_CCMD 0x000 /* Chip's command register */
1562 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1563 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1564 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1565 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1566 #define EN0_DCFG 0x00E /* Data configuration reg WR */
1567 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1568 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1569 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1570 static int mxser_read_register(int port, unsigned short *regs)
1572 int i, k, value, id;
1575 id = mxser_program_mode(port);
1578 for (i = 0; i < 14; i++) {
1579 k = (i & 0x3F) | 0x180;
1580 for (j = 0x100; j > 0; j >>= 1) {
1581 outb(CHIP_CS, port);
1583 outb(CHIP_CS | CHIP_DO, port);
1584 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1586 outb(CHIP_CS, port);
1587 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1592 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1593 outb(CHIP_CS, port);
1594 outb(CHIP_CS | CHIP_SK, port);
1595 if (inb(port) & CHIP_DI)
1601 mxser_normal_mode(port);
1605 static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1607 struct mxser_port *port;
1613 /* if (copy_to_user(argp, mxsercfg,
1614 sizeof(struct mxser_hwconf) * 4))
1618 case MOXA_GET_MAJOR:
1619 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1623 case MOXA_GET_CUMAJOR:
1624 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1628 case MOXA_CHKPORTENABLE:
1631 for (i = 0; i < MXSER_BOARDS; i++)
1632 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1633 if (mxser_boards[i].ports[j].ioaddr)
1636 return put_user(result, (unsigned long __user *)argp);
1637 case MOXA_GETDATACOUNT:
1638 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1641 case MOXA_GETMSTATUS:
1642 for (i = 0; i < MXSER_BOARDS; i++)
1643 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1644 port = &mxser_boards[i].ports[j];
1647 if (!port->ioaddr) {
1648 GMStatus[i].dcd = 0;
1649 GMStatus[i].dsr = 0;
1650 GMStatus[i].cts = 0;
1654 if (!port->tty || !port->tty->termios)
1656 port->normal_termios.c_cflag;
1659 port->tty->termios->c_cflag;
1661 status = inb(port->ioaddr + UART_MSR);
1662 if (status & 0x80 /*UART_MSR_DCD */ )
1663 GMStatus[i].dcd = 1;
1665 GMStatus[i].dcd = 0;
1667 if (status & 0x20 /*UART_MSR_DSR */ )
1668 GMStatus[i].dsr = 1;
1670 GMStatus[i].dsr = 0;
1673 if (status & 0x10 /*UART_MSR_CTS */ )
1674 GMStatus[i].cts = 1;
1676 GMStatus[i].cts = 0;
1678 if (copy_to_user(argp, GMStatus,
1679 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1682 case MOXA_ASPP_MON_EXT: {
1683 int status, p, shiftbit;
1684 unsigned long opmode;
1685 unsigned cflag, iflag;
1687 for (i = 0; i < MXSER_BOARDS; i++)
1688 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1689 port = &mxser_boards[i].ports[j];
1693 status = mxser_get_msr(port->ioaddr, 0, i);
1694 /* mxser_check_modem_status(port, status); */
1696 if (status & UART_MSR_TERI)
1698 if (status & UART_MSR_DDSR)
1700 if (status & UART_MSR_DDCD)
1702 if (status & UART_MSR_DCTS)
1705 port->mon_data.modem_status = status;
1706 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1707 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1708 mon_data_ext.up_rxcnt[i] =
1709 port->mon_data.up_rxcnt;
1710 mon_data_ext.up_txcnt[i] =
1711 port->mon_data.up_txcnt;
1712 mon_data_ext.modem_status[i] =
1713 port->mon_data.modem_status;
1714 mon_data_ext.baudrate[i] = port->realbaud;
1716 if (!port->tty || !port->tty->termios) {
1717 cflag = port->normal_termios.c_cflag;
1718 iflag = port->normal_termios.c_iflag;
1720 cflag = port->tty->termios->c_cflag;
1721 iflag = port->tty->termios->c_iflag;
1724 mon_data_ext.databits[i] = cflag & CSIZE;
1726 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1728 mon_data_ext.parity[i] =
1729 cflag & (PARENB | PARODD | CMSPAR);
1731 mon_data_ext.flowctrl[i] = 0x00;
1733 if (cflag & CRTSCTS)
1734 mon_data_ext.flowctrl[i] |= 0x03;
1736 if (iflag & (IXON | IXOFF))
1737 mon_data_ext.flowctrl[i] |= 0x0C;
1739 if (port->type == PORT_16550A)
1740 mon_data_ext.fifo[i] = 1;
1742 mon_data_ext.fifo[i] = 0;
1746 opmode = inb(port->opmode_ioaddr) >> shiftbit;
1747 opmode &= OP_MODE_MASK;
1749 mon_data_ext.iftype[i] = opmode;
1752 if (copy_to_user(argp, &mon_data_ext,
1753 sizeof(mon_data_ext)))
1759 return -ENOIOCTLCMD;
1764 static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1765 unsigned int cmd, unsigned long arg)
1767 struct mxser_port *info = tty->driver_data;
1768 struct async_icount cprev, cnow; /* kernel counter temps */
1769 struct serial_icounter_struct __user *p_cuser;
1770 unsigned long templ;
1771 unsigned long flags;
1772 void __user *argp = (void __user *)arg;
1775 if (tty->index == MXSER_PORTS)
1776 return mxser_ioctl_special(cmd, argp);
1778 /* following add by Victor Yu. 01-05-2004 */
1779 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1781 unsigned long opmode;
1782 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1784 unsigned char val, mask;
1787 if (cmd == MOXA_SET_OP_MODE) {
1788 if (get_user(opmode, (int __user *) argp))
1790 if (opmode != RS232_MODE &&
1791 opmode != RS485_2WIRE_MODE &&
1792 opmode != RS422_MODE &&
1793 opmode != RS485_4WIRE_MODE)
1797 val = inb(info->opmode_ioaddr);
1799 val |= (opmode << shiftbit);
1800 outb(val, info->opmode_ioaddr);
1803 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1804 opmode &= OP_MODE_MASK;
1805 if (copy_to_user(argp, &opmode, sizeof(int)))
1810 /* above add by Victor Yu. 01-05-2004 */
1812 if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1813 if (tty->flags & (1 << TTY_IO_ERROR))
1817 case TCSBRK: /* SVID version: non-zero arg --> no break */
1818 retval = tty_check_change(tty);
1821 tty_wait_until_sent(tty, 0);
1823 mxser_send_break(info, HZ / 4); /* 1/4 second */
1825 case TCSBRKP: /* support for POSIX tcsendbreak() */
1826 retval = tty_check_change(tty);
1829 tty_wait_until_sent(tty, 0);
1830 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1833 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1835 if (get_user(templ, (unsigned long __user *) argp))
1838 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1841 return mxser_get_serial_info(info, argp);
1843 return mxser_set_serial_info(info, argp);
1844 case TIOCSERGETLSR: /* Get line status register */
1845 return mxser_get_lsr_info(info, argp);
1847 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1848 * - mask passed in arg for lines of interest
1849 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1850 * Caller should use TIOCGICOUNT to see which one it was
1853 DECLARE_WAITQUEUE(wait, current);
1855 spin_lock_irqsave(&info->slock, flags);
1856 cprev = info->icount; /* note the counters on entry */
1857 spin_unlock_irqrestore(&info->slock, flags);
1859 add_wait_queue(&info->delta_msr_wait, &wait);
1861 spin_lock_irqsave(&info->slock, flags);
1862 cnow = info->icount; /* atomic copy */
1863 spin_unlock_irqrestore(&info->slock, flags);
1865 set_current_state(TASK_INTERRUPTIBLE);
1866 if (((arg & TIOCM_RNG) &&
1867 (cnow.rng != cprev.rng)) ||
1868 ((arg & TIOCM_DSR) &&
1869 (cnow.dsr != cprev.dsr)) ||
1870 ((arg & TIOCM_CD) &&
1871 (cnow.dcd != cprev.dcd)) ||
1872 ((arg & TIOCM_CTS) &&
1873 (cnow.cts != cprev.cts))) {
1877 /* see if a signal did it */
1878 if (signal_pending(current)) {
1884 current->state = TASK_RUNNING;
1885 remove_wait_queue(&info->delta_msr_wait, &wait);
1890 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1891 * Return: write counters to the user passed counter struct
1892 * NB: both 1->0 and 0->1 transitions are counted except for
1893 * RI where only 0->1 is counted.
1896 spin_lock_irqsave(&info->slock, flags);
1897 cnow = info->icount;
1898 spin_unlock_irqrestore(&info->slock, flags);
1900 /* modified by casper 1/11/2000 */
1901 if (put_user(cnow.frame, &p_cuser->frame))
1903 if (put_user(cnow.brk, &p_cuser->brk))
1905 if (put_user(cnow.overrun, &p_cuser->overrun))
1907 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1909 if (put_user(cnow.parity, &p_cuser->parity))
1911 if (put_user(cnow.rx, &p_cuser->rx))
1913 if (put_user(cnow.tx, &p_cuser->tx))
1915 put_user(cnow.cts, &p_cuser->cts);
1916 put_user(cnow.dsr, &p_cuser->dsr);
1917 put_user(cnow.rng, &p_cuser->rng);
1918 put_user(cnow.dcd, &p_cuser->dcd);
1920 case MOXA_HighSpeedOn:
1921 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1922 case MOXA_SDS_RSTICOUNTER:
1923 info->mon_data.rxcnt = 0;
1924 info->mon_data.txcnt = 0;
1926 /* (above) added by James. */
1927 case MOXA_ASPP_SETBAUD:{
1929 if (get_user(baud, (long __user *)argp))
1931 mxser_set_baud(info, baud);
1934 case MOXA_ASPP_GETBAUD:
1935 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1940 case MOXA_ASPP_OQUEUE:{
1943 len = mxser_chars_in_buffer(tty);
1945 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1947 len += (lsr ? 0 : 1);
1949 if (copy_to_user(argp, &len, sizeof(int)))
1954 case MOXA_ASPP_MON: {
1957 /* info->mon_data.ser_param = tty->termios->c_cflag; */
1959 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1960 mxser_check_modem_status(info, status);
1962 mcr = inb(info->ioaddr + UART_MCR);
1963 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1964 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1966 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1968 if (mcr & MOXA_MUST_MCR_TX_XON)
1969 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1971 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1973 if (info->tty->hw_stopped)
1974 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1976 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1978 if (copy_to_user(argp, &info->mon_data,
1979 sizeof(struct mxser_mon)))
1984 case MOXA_ASPP_LSTATUS: {
1985 if (copy_to_user(argp, &info->err_shadow,
1986 sizeof(unsigned char)))
1989 info->err_shadow = 0;
1992 case MOXA_SET_BAUD_METHOD: {
1995 if (get_user(method, (int __user *)argp))
1997 mxser_set_baud_method[tty->index] = method;
1998 if (copy_to_user(argp, &method, sizeof(int)))
2004 return -ENOIOCTLCMD;
2009 static void mxser_stoprx(struct tty_struct *tty)
2011 struct mxser_port *info = tty->driver_data;
2013 info->ldisc_stop_rx = 1;
2015 /* following add by Victor Yu. 09-02-2002 */
2016 if (info->board->chip_flag) {
2017 info->IER &= ~MOXA_MUST_RECV_ISR;
2018 outb(info->IER, info->ioaddr + UART_IER);
2019 } else if (!(info->flags & ASYNC_CLOSING)) {
2020 info->x_char = STOP_CHAR(tty);
2021 outb(0, info->ioaddr + UART_IER);
2022 info->IER |= UART_IER_THRI;
2023 outb(info->IER, info->ioaddr + UART_IER);
2027 if (info->tty->termios->c_cflag & CRTSCTS) {
2028 info->MCR &= ~UART_MCR_RTS;
2029 outb(info->MCR, info->ioaddr + UART_MCR);
2034 * This routine is called by the upper-layer tty layer to signal that
2035 * incoming characters should be throttled.
2037 static void mxser_throttle(struct tty_struct *tty)
2042 static void mxser_unthrottle(struct tty_struct *tty)
2044 struct mxser_port *info = tty->driver_data;
2047 info->ldisc_stop_rx = 0;
2052 /* following add by Victor Yu. 09-02-2002 */
2053 if (info->board->chip_flag) {
2054 info->IER |= MOXA_MUST_RECV_ISR;
2055 outb(info->IER, info->ioaddr + UART_IER);
2056 } else if (!(info->flags & ASYNC_CLOSING)) {
2057 info->x_char = START_CHAR(tty);
2058 outb(0, info->ioaddr + UART_IER);
2059 info->IER |= UART_IER_THRI;
2060 outb(info->IER, info->ioaddr + UART_IER);
2065 if (info->tty->termios->c_cflag & CRTSCTS) {
2066 info->MCR |= UART_MCR_RTS;
2067 outb(info->MCR, info->ioaddr + UART_MCR);
2072 * mxser_stop() and mxser_start()
2074 * This routines are called before setting or resetting tty->stopped.
2075 * They enable or disable transmitter interrupts, as necessary.
2077 static void mxser_stop(struct tty_struct *tty)
2079 struct mxser_port *info = tty->driver_data;
2080 unsigned long flags;
2082 spin_lock_irqsave(&info->slock, flags);
2083 if (info->IER & UART_IER_THRI) {
2084 info->IER &= ~UART_IER_THRI;
2085 outb(info->IER, info->ioaddr + UART_IER);
2087 spin_unlock_irqrestore(&info->slock, flags);
2090 static void mxser_start(struct tty_struct *tty)
2092 struct mxser_port *info = tty->driver_data;
2093 unsigned long flags;
2095 spin_lock_irqsave(&info->slock, flags);
2096 if (info->xmit_cnt && info->xmit_buf
2097 /* && !(info->IER & UART_IER_THRI) */) {
2098 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
2099 info->IER |= UART_IER_THRI;
2100 outb(info->IER, info->ioaddr + UART_IER);
2102 spin_unlock_irqrestore(&info->slock, flags);
2105 static void mxser_set_termios(struct tty_struct *tty, struct termios *old_termios)
2107 struct mxser_port *info = tty->driver_data;
2108 unsigned long flags;
2110 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
2111 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
2113 mxser_change_speed(info, old_termios);
2115 if ((old_termios->c_cflag & CRTSCTS) &&
2116 !(tty->termios->c_cflag & CRTSCTS)) {
2117 tty->hw_stopped = 0;
2122 /* Handle sw stopped */
2123 if ((old_termios->c_iflag & IXON) &&
2124 !(tty->termios->c_iflag & IXON)) {
2127 /* following add by Victor Yu. 09-02-2002 */
2128 if (info->board->chip_flag) {
2129 spin_lock_irqsave(&info->slock, flags);
2130 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2131 spin_unlock_irqrestore(&info->slock, flags);
2133 /* above add by Victor Yu. 09-02-2002 */
2140 * mxser_wait_until_sent() --- wait until the transmitter is empty
2142 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2144 struct mxser_port *info = tty->driver_data;
2145 unsigned long orig_jiffies, char_time;
2148 if (info->type == PORT_UNKNOWN)
2151 if (info->xmit_fifo_size == 0)
2152 return; /* Just in case.... */
2154 orig_jiffies = jiffies;
2156 * Set the check interval to be 1/5 of the estimated time to
2157 * send a single character, and make it at least 1. The check
2158 * interval should also be less than the timeout.
2160 * Note: we have to use pretty tight timings here to satisfy
2163 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2164 char_time = char_time / 5;
2167 if (timeout && timeout < char_time)
2168 char_time = timeout;
2170 * If the transmitter hasn't cleared in twice the approximate
2171 * amount of time to send the entire FIFO, it probably won't
2172 * ever clear. This assumes the UART isn't doing flow
2173 * control, which is currently the case. Hence, if it ever
2174 * takes longer than info->timeout, this is probably due to a
2175 * UART bug of some kind. So, we clamp the timeout parameter at
2178 if (!timeout || timeout > 2 * info->timeout)
2179 timeout = 2 * info->timeout;
2180 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2181 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2182 timeout, char_time);
2183 printk("jiff=%lu...", jiffies);
2185 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
2186 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2187 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2189 schedule_timeout_interruptible(char_time);
2190 if (signal_pending(current))
2192 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2195 set_current_state(TASK_RUNNING);
2197 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2198 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2204 * This routine is called by tty_hangup() when a hangup is signaled.
2206 void mxser_hangup(struct tty_struct *tty)
2208 struct mxser_port *info = tty->driver_data;
2210 mxser_flush_buffer(tty);
2211 mxser_shutdown(info);
2214 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2216 wake_up_interruptible(&info->open_wait);
2220 /* added by James 03-12-2004. */
2222 * mxser_rs_break() --- routine which turns the break handling on or off
2224 static void mxser_rs_break(struct tty_struct *tty, int break_state)
2226 struct mxser_port *info = tty->driver_data;
2227 unsigned long flags;
2229 spin_lock_irqsave(&info->slock, flags);
2230 if (break_state == -1)
2231 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2232 info->ioaddr + UART_LCR);
2234 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2235 info->ioaddr + UART_LCR);
2236 spin_unlock_irqrestore(&info->slock, flags);
2239 /* (above) added by James. */
2241 static void mxser_receive_chars(struct mxser_port *port, int *status)
2243 struct tty_struct *tty = port->tty;
2244 unsigned char ch, gdl;
2249 unsigned long flags;
2251 spin_lock_irqsave(&port->slock, flags);
2253 recv_room = tty->receive_room;
2254 if ((recv_room == 0) && (!port->ldisc_stop_rx)) {
2255 /* mxser_throttle(tty); */
2260 /* following add by Victor Yu. 09-02-2002 */
2261 if (port->board->chip_flag != MOXA_OTHER_UART) {
2263 if (*status & UART_LSR_SPECIAL)
2265 /* following add by Victor Yu. 02-11-2004 */
2266 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
2267 (*status & MOXA_MUST_LSR_RERR))
2269 /* above add by Victor Yu. 02-14-2004 */
2270 if (*status & MOXA_MUST_LSR_RERR)
2273 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
2275 /* add by Victor Yu. 02-11-2004 */
2276 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
2277 gdl &= MOXA_MUST_GDL_MASK;
2278 if (gdl >= recv_room) {
2279 if (!port->ldisc_stop_rx) {
2280 /* mxser_throttle(tty); */
2286 ch = inb(port->ioaddr + UART_RX);
2287 tty_insert_flip_char(tty, ch, 0);
2293 /* above add by Victor Yu. 09-02-2002 */
2299 ch = inb(port->ioaddr + UART_RX);
2300 /* following add by Victor Yu. 09-02-2002 */
2301 if (port->board->chip_flag && (*status & UART_LSR_OE)
2302 /*&& !(*status&UART_LSR_DR) */)
2303 outb(0x23, port->ioaddr + UART_FCR);
2304 *status &= port->read_status_mask;
2305 /* above add by Victor Yu. 09-02-2002 */
2306 if (*status & port->ignore_status_mask) {
2307 if (++ignored > 100)
2311 if (*status & UART_LSR_SPECIAL) {
2312 if (*status & UART_LSR_BI) {
2314 /* added by casper 1/11/2000 */
2317 if (port->flags & ASYNC_SAK)
2319 } else if (*status & UART_LSR_PE) {
2321 /* added by casper 1/11/2000 */
2322 port->icount.parity++;
2323 } else if (*status & UART_LSR_FE) {
2325 /* added by casper 1/11/2000 */
2326 port->icount.frame++;
2327 } else if (*status & UART_LSR_OE) {
2329 /* added by casper 1/11/2000 */
2330 port->icount.overrun++;
2335 tty_insert_flip_char(tty, ch, flag);
2337 if (cnt >= recv_room) {
2338 if (!port->ldisc_stop_rx) {
2339 /* mxser_throttle(tty); */
2347 /* following add by Victor Yu. 09-02-2002 */
2348 if (port->board->chip_flag)
2351 /* mask by Victor Yu. 09-02-2002
2352 *status = inb(port->ioaddr + UART_LSR) & port->read_status_mask;
2354 /* following add by Victor Yu. 09-02-2002 */
2355 *status = inb(port->ioaddr + UART_LSR);
2356 /* above add by Victor Yu. 09-02-2002 */
2357 } while (*status & UART_LSR_DR);
2359 end_intr: /* add by Victor Yu. 09-02-2002 */
2360 mxvar_log.rxcnt[port->tty->index] += cnt;
2361 port->mon_data.rxcnt += cnt;
2362 port->mon_data.up_rxcnt += cnt;
2363 spin_unlock_irqrestore(&port->slock, flags);
2365 tty_flip_buffer_push(tty);
2368 static void mxser_transmit_chars(struct mxser_port *port)
2371 unsigned long flags;
2373 spin_lock_irqsave(&port->slock, flags);
2376 outb(port->x_char, port->ioaddr + UART_TX);
2378 mxvar_log.txcnt[port->tty->index]++;
2379 port->mon_data.txcnt++;
2380 port->mon_data.up_txcnt++;
2382 /* added by casper 1/11/2000 */
2387 if (port->xmit_buf == 0)
2390 if (port->xmit_cnt == 0) {
2391 if (port->xmit_cnt < WAKEUP_CHARS) { /* XXX what's this for?? */
2392 set_bit(MXSER_EVENT_TXLOW, &port->event);
2393 schedule_work(&port->tqueue);
2397 if (port->tty->stopped || (port->tty->hw_stopped &&
2398 (port->type != PORT_16550A) &&
2399 (!port->board->chip_flag))) {
2400 port->IER &= ~UART_IER_THRI;
2401 outb(port->IER, port->ioaddr + UART_IER);
2405 cnt = port->xmit_cnt;
2406 count = port->xmit_fifo_size;
2408 outb(port->xmit_buf[port->xmit_tail++],
2409 port->ioaddr + UART_TX);
2410 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2411 if (--port->xmit_cnt <= 0)
2413 } while (--count > 0);
2414 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
2416 /* added by James 03-12-2004. */
2417 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2418 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2420 /* added by casper 1/11/2000 */
2421 port->icount.tx += (cnt - port->xmit_cnt);
2423 if (port->xmit_cnt < WAKEUP_CHARS) {
2424 set_bit(MXSER_EVENT_TXLOW, &port->event);
2425 schedule_work(&port->tqueue);
2427 if (port->xmit_cnt <= 0) {
2428 port->IER &= ~UART_IER_THRI;
2429 outb(port->IER, port->ioaddr + UART_IER);
2432 spin_unlock_irqrestore(&port->slock, flags);
2436 * This is the serial driver's generic interrupt routine
2438 static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
2441 struct mxser_board *brd = NULL;
2442 struct mxser_port *port;
2443 int max, irqbits, bits, msr;
2444 int pass_counter = 0;
2445 unsigned int int_cnt;
2446 int handled = IRQ_NONE;
2448 /* spin_lock(&gm_lock); */
2450 for (i = 0; i < MXSER_BOARDS; i++)
2451 if (dev_id == &mxser_boards[i]) {
2456 if (i == MXSER_BOARDS)
2460 max = mxser_numports[brd->board_type - 1];
2462 irqbits = inb(brd->vector) & brd->vector_mask;
2463 if (irqbits == brd->vector_mask)
2466 handled = IRQ_HANDLED;
2467 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2468 if (irqbits == brd->vector_mask)
2472 port = &brd->ports[i];
2476 /* following add by Victor Yu. 09-13-2002 */
2477 iir = inb(port->ioaddr + UART_IIR);
2478 if (iir & UART_IIR_NO_INT)
2480 iir &= MOXA_MUST_IIR_MASK;
2482 status = inb(port->ioaddr + UART_LSR);
2483 outb(0x27, port->ioaddr + UART_FCR);
2484 inb(port->ioaddr + UART_MSR);
2487 /* above add by Victor Yu. 09-13-2002 */
2489 /* following add by Victor Yu. 09-02-2002 */
2490 status = inb(port->ioaddr + UART_LSR);
2492 if (status & UART_LSR_PE)
2493 port->err_shadow |= NPPI_NOTIFY_PARITY;
2494 if (status & UART_LSR_FE)
2495 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2496 if (status & UART_LSR_OE)
2498 NPPI_NOTIFY_HW_OVERRUN;
2499 if (status & UART_LSR_BI)
2500 port->err_shadow |= NPPI_NOTIFY_BREAK;
2502 if (port->board->chip_flag) {
2504 if ( (status & 0x02) && !(status & 0x01) ) {
2505 outb(port->ioaddr+UART_FCR, 0x23);
2509 if (iir == MOXA_MUST_IIR_GDA ||
2510 iir == MOXA_MUST_IIR_RDA ||
2511 iir == MOXA_MUST_IIR_RTO ||
2512 iir == MOXA_MUST_IIR_LSR)
2513 mxser_receive_chars(port,
2517 /* above add by Victor Yu. 09-02-2002 */
2519 status &= port->read_status_mask;
2520 if (status & UART_LSR_DR)
2521 mxser_receive_chars(port,
2524 msr = inb(port->ioaddr + UART_MSR);
2525 if (msr & UART_MSR_ANY_DELTA)
2526 mxser_check_modem_status(port, msr);
2528 /* following add by Victor Yu. 09-13-2002 */
2529 if (port->board->chip_flag) {
2530 if (iir == 0x02 && (status &
2532 mxser_transmit_chars(port);
2534 /* above add by Victor Yu. 09-13-2002 */
2536 if (status & UART_LSR_THRE)
2537 mxser_transmit_chars(port);
2539 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2541 if (pass_counter++ > MXSER_ISR_PASS_LIMIT)
2542 break; /* Prevent infinite loops */
2546 /* spin_unlock(&gm_lock); */
2550 static const struct tty_operations mxser_ops = {
2552 .close = mxser_close,
2553 .write = mxser_write,
2554 .put_char = mxser_put_char,
2555 .flush_chars = mxser_flush_chars,
2556 .write_room = mxser_write_room,
2557 .chars_in_buffer = mxser_chars_in_buffer,
2558 .flush_buffer = mxser_flush_buffer,
2559 .ioctl = mxser_ioctl,
2560 .throttle = mxser_throttle,
2561 .unthrottle = mxser_unthrottle,
2562 .set_termios = mxser_set_termios,
2564 .start = mxser_start,
2565 .hangup = mxser_hangup,
2566 .break_ctl = mxser_rs_break,
2567 .wait_until_sent = mxser_wait_until_sent,
2568 .tiocmget = mxser_tiocmget,
2569 .tiocmset = mxser_tiocmset,
2573 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2576 static int __devinit mxser_initbrd(struct mxser_board *brd)
2578 struct mxser_port *info;
2582 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
2584 for (i = 0; i < brd->nports; i++) {
2585 info = &brd->ports[i];
2588 info->ldisc_stop_rx = 0;
2590 /* Enhance mode enabled here */
2591 if (brd->chip_flag != MOXA_OTHER_UART)
2592 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
2594 info->flags = ASYNC_SHARE_IRQ;
2595 info->type = brd->uart_type;
2597 process_txrx_fifo(info);
2599 info->custom_divisor = info->baud_base * 16;
2600 info->close_delay = 5 * HZ / 10;
2601 info->closing_wait = 30 * HZ;
2602 INIT_WORK(&info->tqueue, mxser_do_softint, info);
2603 info->normal_termios = mxvar_sdriver->init_termios;
2604 init_waitqueue_head(&info->open_wait);
2605 init_waitqueue_head(&info->close_wait);
2606 init_waitqueue_head(&info->delta_msr_wait);
2607 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2608 info->err_shadow = 0;
2609 spin_lock_init(&info->slock);
2611 /* before set INT ISR, disable all int */
2612 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2613 info->ioaddr + UART_IER);
2616 * Allocate the IRQ if necessary
2619 retval = request_irq(brd->irq, mxser_interrupt,
2620 (brd->ports[0].flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED :
2621 IRQF_DISABLED, "mxser", brd);
2623 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2624 "conflict with another device.\n",
2625 mxser_brdname[brd->board_type - 1], brd->irq);
2631 static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
2634 unsigned short regs[16], irq;
2635 unsigned char scratch, scratch2;
2637 brd->chip_flag = MOXA_OTHER_UART;
2639 id = mxser_read_register(cap, regs);
2640 if (id == C168_ASIC_ID) {
2641 brd->board_type = MXSER_BOARD_C168_ISA;
2643 } else if (id == C104_ASIC_ID) {
2644 brd->board_type = MXSER_BOARD_C104_ISA;
2646 } else if (id == C102_ASIC_ID) {
2647 brd->board_type = MXSER_BOARD_C102_ISA;
2649 } else if (id == CI132_ASIC_ID) {
2650 brd->board_type = MXSER_BOARD_CI132;
2652 } else if (id == CI134_ASIC_ID) {
2653 brd->board_type = MXSER_BOARD_CI134;
2655 } else if (id == CI104J_ASIC_ID) {
2656 brd->board_type = MXSER_BOARD_CI104J;
2662 if (brd->nports == 2) {
2663 irq = regs[9] & 0xF000;
2664 irq = irq | (irq >> 4);
2665 if (irq != (regs[9] & 0xFF00))
2666 return MXSER_ERR_IRQ_CONFLIT;
2667 } else if (brd->nports == 4) {
2668 irq = regs[9] & 0xF000;
2669 irq = irq | (irq >> 4);
2670 irq = irq | (irq >> 8);
2672 return MXSER_ERR_IRQ_CONFLIT;
2673 } else if (brd->nports == 8) {
2674 irq = regs[9] & 0xF000;
2675 irq = irq | (irq >> 4);
2676 irq = irq | (irq >> 8);
2677 if ((irq != regs[9]) || (irq != regs[10]))
2678 return MXSER_ERR_IRQ_CONFLIT;
2682 return MXSER_ERR_IRQ;
2683 brd->irq = ((int)(irq & 0xF000) >> 12);
2684 for (i = 0; i < 8; i++)
2685 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
2686 if ((regs[12] & 0x80) == 0)
2687 return MXSER_ERR_VECTOR;
2688 brd->vector = (int)regs[11]; /* interrupt vector */
2690 brd->vector_mask = 0x00FF;
2692 brd->vector_mask = 0x000F;
2693 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2694 if (regs[12] & bits) {
2695 brd->ports[i].baud_base = 921600;
2696 brd->ports[i].max_baud = 921600; /* add by Victor Yu. 09-04-2002 */
2698 brd->ports[i].baud_base = 115200;
2699 brd->ports[i].max_baud = 115200; /* add by Victor Yu. 09-04-2002 */
2702 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2703 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2704 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2705 outb(scratch2, cap + UART_LCR);
2706 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2707 scratch = inb(cap + UART_IIR);
2710 brd->uart_type = PORT_16550A;
2712 brd->uart_type = PORT_16450;
2717 if (!request_region(brd->ports[0].ioaddr, 8 * brd->nports, "mxser(IO)"))
2718 return MXSER_ERR_IOADDR;
2719 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2720 release_region(brd->ports[0].ioaddr, 8 * brd->nports);
2721 return MXSER_ERR_VECTOR;
2726 static int __init mxser_get_PCI_conf(int board_type, struct mxser_board *brd,
2727 struct pci_dev *pdev)
2730 unsigned long ioaddress;
2734 brd->board_type = board_type;
2735 brd->nports = mxser_numports[board_type - 1];
2736 ioaddress = pci_resource_start(pdev, 2);
2737 retval = pci_request_region(pdev, 2, "mxser(IO)");
2741 for (i = 0; i < brd->nports; i++)
2742 brd->ports[i].ioaddr = ioaddress + 8 * i;
2745 ioaddress = pci_resource_start(pdev, 3);
2746 retval = pci_request_region(pdev, 3, "mxser(vector)");
2749 brd->vector = ioaddress;
2752 brd->irq = pdev->irq;
2754 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2755 brd->uart_type = PORT_16550A;
2756 brd->vector_mask = 0;
2758 for (i = 0; i < brd->nports; i++) {
2759 for (j = 0; j < UART_INFO_NUM; j++) {
2760 if (Gpci_uart_info[j].type == brd->chip_flag) {
2761 brd->ports[i].max_baud =
2762 Gpci_uart_info[j].max_baud;
2764 /* exception....CP-102 */
2765 if (board_type == MXSER_BOARD_CP102)
2766 brd->ports[i].max_baud = 921600;
2772 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2773 for (i = 0; i < brd->nports; i++) {
2775 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2777 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
2779 outb(0, ioaddress + 4); /* default set to RS232 mode */
2780 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
2783 for (i = 0; i < brd->nports; i++) {
2784 brd->vector_mask |= (1 << i);
2785 brd->ports[i].baud_base = 921600;
2789 pci_release_region(pdev, 2);
2794 static int __init mxser_module_init(void)
2796 struct pci_dev *pdev = NULL;
2797 struct mxser_board *brd;
2801 pr_debug("Loading module mxser ...\n");
2803 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2806 spin_lock_init(&gm_lock);
2808 for (i = 0; i < MXSER_BOARDS; i++)
2809 mxser_boards[i].board_type = -1;
2811 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2814 /* Initialize the tty_driver structure */
2815 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2816 mxvar_sdriver->name = "ttyM";
2817 mxvar_sdriver->major = ttymajor;
2818 mxvar_sdriver->minor_start = 0;
2819 mxvar_sdriver->num = MXSER_PORTS + 1;
2820 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2821 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2822 mxvar_sdriver->init_termios = tty_std_termios;
2823 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2824 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
2825 tty_set_operations(mxvar_sdriver, &mxser_ops);
2826 mxvar_sdriver->ttys = mxvar_tty;
2827 mxvar_sdriver->termios = mxvar_termios;
2828 mxvar_sdriver->termios_locked = mxvar_termios_locked;
2833 /* Start finding ISA boards here */
2834 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2837 if (!(cap = mxserBoardCAP[b]))
2840 brd = &mxser_boards[m];
2841 retval = mxser_get_ISA_conf(cap, brd);
2844 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
2845 mxser_brdname[brd->board_type - 1], ioaddr[b]);
2848 if (retval == MXSER_ERR_IRQ)
2849 printk(KERN_ERR "Invalid interrupt number, "
2850 "board not configured\n");
2851 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2852 printk(KERN_ERR "Invalid interrupt number, "
2853 "board not configured\n");
2854 else if (retval == MXSER_ERR_VECTOR)
2855 printk(KERN_ERR "Invalid interrupt vector, "
2856 "board not configured\n");
2857 else if (retval == MXSER_ERR_IOADDR)
2858 printk(KERN_ERR "Invalid I/O address, "
2859 "board not configured\n");
2866 /* mxser_initbrd will hook ISR. */
2867 if (mxser_initbrd(brd) < 0)
2873 /* Start finding ISA boards from module arg */
2874 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2877 if (!(cap = ioaddr[b]))
2880 brd = &mxser_boards[m];
2881 retval = mxser_get_ISA_conf(cap, &mxser_boards[m]);
2884 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
2885 mxser_brdname[brd->board_type - 1], ioaddr[b]);
2888 if (retval == MXSER_ERR_IRQ)
2889 printk(KERN_ERR "Invalid interrupt number, "
2890 "board not configured\n");
2891 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2892 printk(KERN_ERR "Invalid interrupt number, "
2893 "board not configured\n");
2894 else if (retval == MXSER_ERR_VECTOR)
2895 printk(KERN_ERR "Invalid interrupt vector, "
2896 "board not configured\n");
2897 else if (retval == MXSER_ERR_IOADDR)
2898 printk(KERN_ERR "Invalid I/O address, "
2899 "board not configured\n");
2905 /* mxser_initbrd will hook ISR. */
2906 if (mxser_initbrd(brd) < 0)
2912 /* start finding PCI board here */
2913 n = ARRAY_SIZE(mxser_pcibrds) - 1;
2916 pdev = pci_get_device(mxser_pcibrds[b].vendor,
2917 mxser_pcibrds[b].device, pdev);
2922 printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
2923 mxser_brdname[(int) (mxser_pcibrds[b].driver_data) - 1],
2924 pdev->bus->number, PCI_SLOT(pdev->devfn));
2925 if (m >= MXSER_BOARDS)
2927 "Too many Smartio/Industio family boards find "
2928 "(maximum %d), board not configured\n",
2931 if (pci_enable_device(pdev)) {
2932 printk(KERN_ERR "Moxa SmartI/O PCI enable "
2936 brd = &mxser_boards[m];
2938 retval = mxser_get_PCI_conf(
2939 (int)mxser_pcibrds[b].driver_data,
2942 if (retval == MXSER_ERR_IRQ)
2944 "Invalid interrupt number, "
2945 "board not configured\n");
2946 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2948 "Invalid interrupt number, "
2949 "board not configured\n");
2950 else if (retval == MXSER_ERR_VECTOR)
2952 "Invalid interrupt vector, "
2953 "board not configured\n");
2954 else if (retval == MXSER_ERR_IOADDR)
2956 "Invalid I/O address, "
2957 "board not configured\n");
2960 /* mxser_initbrd will hook ISR. */
2961 if (mxser_initbrd(brd) < 0)
2964 /* Keep an extra reference if we succeeded. It will
2965 be returned at unload time */
2970 retval = tty_register_driver(mxvar_sdriver);
2972 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family"
2974 put_tty_driver(mxvar_sdriver);
2976 for (i = 0; i < MXSER_BOARDS; i++) {
2977 if (mxser_boards[i].board_type == -1)
2980 free_irq(mxser_boards[i].irq, &mxser_boards[i]);
2981 /* todo: release io, vector */
2987 pr_debug("Done.\n");
2992 static void __exit mxser_module_exit(void)
2996 pr_debug("Unloading module mxser ...\n");
2998 err = tty_unregister_driver(mxvar_sdriver);
3000 put_tty_driver(mxvar_sdriver);
3002 printk(KERN_ERR "Couldn't unregister MOXA Smartio/Industio family serial driver\n");
3004 for (i = 0; i < MXSER_BOARDS; i++) {
3005 struct pci_dev *pdev;
3007 if (mxser_boards[i].board_type == -1)
3010 pdev = mxser_boards[i].pdev;
3011 free_irq(mxser_boards[i].irq, &mxser_boards[i]);
3012 if (pdev != NULL) { /* PCI */
3013 pci_release_region(pdev, 2);
3014 pci_release_region(pdev, 3);
3017 release_region(mxser_boards[i].ports[0].ioaddr, 8 * mxser_boards[i].nports);
3018 release_region(mxser_boards[i].vector, 1);
3022 pr_debug("Done.\n");
3025 module_init(mxser_module_init);
3026 module_exit(mxser_module_exit);