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;
595 if (mxser_set_baud_method[info->tty->index] == 0) {
596 baud = tty_get_baud_rate(info->tty);
597 mxser_set_baud(info, baud);
600 /* byte size and parity */
601 switch (cflag & CSIZE) {
616 break; /* too keep GCC shut... */
621 cval |= UART_LCR_PARITY;
622 if (!(cflag & PARODD))
623 cval |= UART_LCR_EPAR;
625 cval |= UART_LCR_SPAR;
627 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
628 if (info->board->chip_flag) {
629 fcr = UART_FCR_ENABLE_FIFO;
630 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
631 SET_MOXA_MUST_FIFO_VALUE(info);
635 fcr = UART_FCR_ENABLE_FIFO;
636 /* following add by Victor Yu. 08-30-2002 */
637 if (info->board->chip_flag) {
638 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
639 SET_MOXA_MUST_FIFO_VALUE(info);
641 /* above add by Victor Yu. 08-30-2002 */
642 switch (info->rx_trigger) {
644 fcr |= UART_FCR_TRIGGER_1;
647 fcr |= UART_FCR_TRIGGER_4;
650 fcr |= UART_FCR_TRIGGER_8;
653 fcr |= UART_FCR_TRIGGER_14;
659 /* CTS flow control flag and modem status interrupts */
660 info->IER &= ~UART_IER_MSI;
661 info->MCR &= ~UART_MCR_AFE;
662 if (cflag & CRTSCTS) {
663 info->flags |= ASYNC_CTS_FLOW;
664 info->IER |= UART_IER_MSI;
665 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
666 info->MCR |= UART_MCR_AFE;
667 /* status = mxser_get_msr(info->ioaddr, 0, info->port); */
671 status = inb(baseaddr + UART_MSR);
672 restore_flags(flags);
674 /* mxser_check_modem_status(info, status); */
676 /* status = mxser_get_msr(info->ioaddr, 0, info->port); */
677 /* MX_LOCK(&info->slock); */
678 status = inb(info->ioaddr + UART_MSR);
679 /* MX_UNLOCK(&info->slock); */
680 if (info->tty->hw_stopped) {
681 if (status & UART_MSR_CTS) {
682 info->tty->hw_stopped = 0;
683 if (info->type != PORT_16550A &&
684 !info->board->chip_flag) {
685 outb(info->IER & ~UART_IER_THRI,
688 info->IER |= UART_IER_THRI;
689 outb(info->IER, info->ioaddr +
692 set_bit(MXSER_EVENT_TXLOW, &info->event);
693 schedule_work(&info->tqueue); }
695 if (!(status & UART_MSR_CTS)) {
696 info->tty->hw_stopped = 1;
697 if ((info->type != PORT_16550A) &&
698 (!info->board->chip_flag)) {
699 info->IER &= ~UART_IER_THRI;
700 outb(info->IER, info->ioaddr +
707 info->flags &= ~ASYNC_CTS_FLOW;
709 outb(info->MCR, info->ioaddr + UART_MCR);
710 if (cflag & CLOCAL) {
711 info->flags &= ~ASYNC_CHECK_CD;
713 info->flags |= ASYNC_CHECK_CD;
714 info->IER |= UART_IER_MSI;
716 outb(info->IER, info->ioaddr + UART_IER);
719 * Set up parity check flag
721 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
722 if (I_INPCK(info->tty))
723 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
724 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
725 info->read_status_mask |= UART_LSR_BI;
727 info->ignore_status_mask = 0;
729 if (I_IGNBRK(info->tty)) {
730 info->ignore_status_mask |= UART_LSR_BI;
731 info->read_status_mask |= UART_LSR_BI;
733 * If we're ignore parity and break indicators, ignore
734 * overruns too. (For real raw support).
736 if (I_IGNPAR(info->tty)) {
737 info->ignore_status_mask |=
741 info->read_status_mask |=
747 /* following add by Victor Yu. 09-02-2002 */
748 if (info->board->chip_flag) {
749 spin_lock_irqsave(&info->slock, flags);
750 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
751 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
752 if (I_IXON(info->tty)) {
753 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
755 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
757 if (I_IXOFF(info->tty)) {
758 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
760 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
763 if ( I_IXANY(info->tty) ) {
764 info->MCR |= MOXA_MUST_MCR_XON_ANY;
765 ENABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
767 info->MCR &= ~MOXA_MUST_MCR_XON_ANY;
768 DISABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
771 spin_unlock_irqrestore(&info->slock, flags);
773 /* above add by Victor Yu. 09-02-2002 */
776 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
777 outb(cval, info->ioaddr + UART_LCR);
782 static void mxser_check_modem_status(struct mxser_port *port, int status)
784 /* update input line counters */
785 if (status & UART_MSR_TERI)
787 if (status & UART_MSR_DDSR)
789 if (status & UART_MSR_DDCD)
791 if (status & UART_MSR_DCTS)
793 port->mon_data.modem_status = status;
794 wake_up_interruptible(&port->delta_msr_wait);
796 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
797 if (status & UART_MSR_DCD)
798 wake_up_interruptible(&port->open_wait);
799 schedule_work(&port->tqueue);
802 if (port->flags & ASYNC_CTS_FLOW) {
803 if (port->tty->hw_stopped) {
804 if (status & UART_MSR_CTS) {
805 port->tty->hw_stopped = 0;
807 if ((port->type != PORT_16550A) &&
808 (!port->board->chip_flag)) {
809 outb(port->IER & ~UART_IER_THRI,
810 port->ioaddr + UART_IER);
811 port->IER |= UART_IER_THRI;
812 outb(port->IER, port->ioaddr +
815 set_bit(MXSER_EVENT_TXLOW, &port->event);
816 schedule_work(&port->tqueue);
819 if (!(status & UART_MSR_CTS)) {
820 port->tty->hw_stopped = 1;
821 if (port->type != PORT_16550A &&
822 !port->board->chip_flag) {
823 port->IER &= ~UART_IER_THRI;
824 outb(port->IER, port->ioaddr +
832 static int mxser_startup(struct mxser_port *info)
837 page = __get_free_page(GFP_KERNEL);
841 spin_lock_irqsave(&info->slock, flags);
843 if (info->flags & ASYNC_INITIALIZED) {
845 spin_unlock_irqrestore(&info->slock, flags);
849 if (!info->ioaddr || !info->type) {
851 set_bit(TTY_IO_ERROR, &info->tty->flags);
853 spin_unlock_irqrestore(&info->slock, flags);
859 info->xmit_buf = (unsigned char *) page;
862 * Clear the FIFO buffers and disable them
863 * (they will be reenabled in mxser_change_speed())
865 if (info->board->chip_flag)
866 outb((UART_FCR_CLEAR_RCVR |
867 UART_FCR_CLEAR_XMIT |
868 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
870 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
871 info->ioaddr + UART_FCR);
874 * At this point there's no way the LSR could still be 0xFF;
875 * if it is, then bail out, because there's likely no UART
878 if (inb(info->ioaddr + UART_LSR) == 0xff) {
879 spin_unlock_irqrestore(&info->slock, flags);
880 if (capable(CAP_SYS_ADMIN)) {
882 set_bit(TTY_IO_ERROR, &info->tty->flags);
889 * Clear the interrupt registers.
891 (void) inb(info->ioaddr + UART_LSR);
892 (void) inb(info->ioaddr + UART_RX);
893 (void) inb(info->ioaddr + UART_IIR);
894 (void) inb(info->ioaddr + UART_MSR);
897 * Now, initialize the UART
899 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
900 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
901 outb(info->MCR, info->ioaddr + UART_MCR);
904 * Finally, enable interrupts
906 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
907 /* info->IER = UART_IER_RLSI | UART_IER_RDI; */
909 /* following add by Victor Yu. 08-30-2002 */
910 if (info->board->chip_flag)
911 info->IER |= MOXA_MUST_IER_EGDAI;
912 /* above add by Victor Yu. 08-30-2002 */
913 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
916 * And clear the interrupt registers again for luck.
918 (void) inb(info->ioaddr + UART_LSR);
919 (void) inb(info->ioaddr + UART_RX);
920 (void) inb(info->ioaddr + UART_IIR);
921 (void) inb(info->ioaddr + UART_MSR);
924 clear_bit(TTY_IO_ERROR, &info->tty->flags);
925 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
928 * and set the speed of the serial port
930 spin_unlock_irqrestore(&info->slock, flags);
931 mxser_change_speed(info, NULL);
933 info->flags |= ASYNC_INITIALIZED;
938 * This routine will shutdown a serial port; interrupts maybe disabled, and
939 * DTR is dropped if the hangup on close termio flag is on.
941 static void mxser_shutdown(struct mxser_port *info)
945 if (!(info->flags & ASYNC_INITIALIZED))
948 spin_lock_irqsave(&info->slock, flags);
951 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
952 * here so the queue might never be waken up
954 wake_up_interruptible(&info->delta_msr_wait);
957 * Free the IRQ, if necessary
959 if (info->xmit_buf) {
960 free_page((unsigned long) info->xmit_buf);
961 info->xmit_buf = NULL;
965 outb(0x00, info->ioaddr + UART_IER);
967 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
968 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
969 outb(info->MCR, info->ioaddr + UART_MCR);
971 /* clear Rx/Tx FIFO's */
972 /* following add by Victor Yu. 08-30-2002 */
973 if (info->board->chip_flag)
974 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
975 MOXA_MUST_FCR_GDA_MODE_ENABLE,
976 info->ioaddr + UART_FCR);
978 /* above add by Victor Yu. 08-30-2002 */
979 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
980 info->ioaddr + UART_FCR);
982 /* read data port to reset things */
983 (void) inb(info->ioaddr + UART_RX);
986 set_bit(TTY_IO_ERROR, &info->tty->flags);
988 info->flags &= ~ASYNC_INITIALIZED;
990 /* following add by Victor Yu. 09-23-2002 */
991 if (info->board->chip_flag)
992 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
993 /* above add by Victor Yu. 09-23-2002 */
995 spin_unlock_irqrestore(&info->slock, flags);
999 * This routine is called whenever a serial port is opened. It
1000 * enables interrupts for a serial port, linking in its async structure into
1001 * the IRQ chain. It also performs the serial-specific
1002 * initialization for the tty structure.
1004 static int mxser_open(struct tty_struct *tty, struct file *filp)
1006 struct mxser_port *info;
1009 /* initialize driver_data in case something fails */
1010 tty->driver_data = NULL;
1013 if (line == MXSER_PORTS)
1015 if (line < 0 || line > MXSER_PORTS)
1017 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
1021 tty->driver_data = info;
1024 * Start up serial port
1027 retval = mxser_startup(info);
1031 retval = mxser_block_til_ready(tty, filp, info);
1035 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
1036 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
1037 *tty->termios = info->normal_termios;
1039 *tty->termios = info->callout_termios;
1040 mxser_change_speed(info, NULL);
1043 info->session = process_session(current);
1044 info->pgrp = process_group(current);
1047 status = mxser_get_msr(info->base, 0, info->port);
1048 mxser_check_modem_status(info, status);
1051 /* unmark here for very high baud rate (ex. 921600 bps) used */
1052 tty->low_latency = 1;
1057 * This routine is called when the serial port gets closed. First, we
1058 * wait for the last remaining data to be sent. Then, we unlink its
1059 * async structure from the interrupt chain if necessary, and we free
1060 * that IRQ if nothing is left in the chain.
1062 static void mxser_close(struct tty_struct *tty, struct file *filp)
1064 struct mxser_port *info = tty->driver_data;
1066 unsigned long timeout;
1067 unsigned long flags;
1068 struct tty_ldisc *ld;
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 ld = tty_ldisc_ref(tty);
1150 if (ld->flush_buffer)
1151 ld->flush_buffer(tty);
1152 tty_ldisc_deref(ld);
1158 if (info->blocked_open) {
1159 if (info->close_delay)
1160 schedule_timeout_interruptible(info->close_delay);
1161 wake_up_interruptible(&info->open_wait);
1164 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1165 wake_up_interruptible(&info->close_wait);
1169 static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1172 struct mxser_port *info = tty->driver_data;
1173 unsigned long flags;
1175 if (!info->xmit_buf)
1179 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1180 SERIAL_XMIT_SIZE - info->xmit_head));
1184 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1185 spin_lock_irqsave(&info->slock, flags);
1186 info->xmit_head = (info->xmit_head + c) &
1187 (SERIAL_XMIT_SIZE - 1);
1188 info->xmit_cnt += c;
1189 spin_unlock_irqrestore(&info->slock, flags);
1196 if (info->xmit_cnt && !tty->stopped
1197 /*&& !(info->IER & UART_IER_THRI)*/) {
1198 if (!tty->hw_stopped ||
1199 (info->type == PORT_16550A) ||
1200 (info->board->chip_flag)) {
1201 spin_lock_irqsave(&info->slock, flags);
1202 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1204 info->IER |= UART_IER_THRI;
1205 outb(info->IER, info->ioaddr + UART_IER);
1206 spin_unlock_irqrestore(&info->slock, flags);
1212 static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1214 struct mxser_port *info = tty->driver_data;
1215 unsigned long flags;
1217 if (!info->xmit_buf)
1220 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1223 spin_lock_irqsave(&info->slock, flags);
1224 info->xmit_buf[info->xmit_head++] = ch;
1225 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1227 spin_unlock_irqrestore(&info->slock, flags);
1228 if (!tty->stopped /*&& !(info->IER & UART_IER_THRI)*/) {
1229 if (!tty->hw_stopped ||
1230 (info->type == PORT_16550A) ||
1231 info->board->chip_flag) {
1232 spin_lock_irqsave(&info->slock, flags);
1233 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1234 info->IER |= UART_IER_THRI;
1235 outb(info->IER, info->ioaddr + UART_IER);
1236 spin_unlock_irqrestore(&info->slock, flags);
1242 static void mxser_flush_chars(struct tty_struct *tty)
1244 struct mxser_port *info = tty->driver_data;
1245 unsigned long flags;
1247 if (info->xmit_cnt <= 0 ||
1251 (info->type != PORT_16550A) &&
1252 (!info->board->chip_flag)
1256 spin_lock_irqsave(&info->slock, flags);
1258 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1259 info->IER |= UART_IER_THRI;
1260 outb(info->IER, info->ioaddr + UART_IER);
1262 spin_unlock_irqrestore(&info->slock, flags);
1265 static int mxser_write_room(struct tty_struct *tty)
1267 struct mxser_port *info = tty->driver_data;
1270 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1276 static int mxser_chars_in_buffer(struct tty_struct *tty)
1278 struct mxser_port *info = tty->driver_data;
1279 int len = info->xmit_cnt;
1281 if (!(inb(info->ioaddr + UART_LSR) & UART_LSR_THRE))
1287 static void mxser_flush_buffer(struct tty_struct *tty)
1289 struct mxser_port *info = tty->driver_data;
1291 unsigned long flags;
1294 spin_lock_irqsave(&info->slock, flags);
1295 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1297 /* below added by shinhay */
1298 fcr = inb(info->ioaddr + UART_FCR);
1299 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1300 info->ioaddr + UART_FCR);
1301 outb(fcr, info->ioaddr + UART_FCR);
1303 spin_unlock_irqrestore(&info->slock, flags);
1304 /* above added by shinhay */
1306 wake_up_interruptible(&tty->write_wait);
1307 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
1308 (tty->ldisc.write_wakeup) (tty);
1312 * ------------------------------------------------------------
1313 * friends of mxser_ioctl()
1314 * ------------------------------------------------------------
1316 static int mxser_get_serial_info(struct mxser_port *info,
1317 struct serial_struct __user *retinfo)
1319 struct serial_struct tmp;
1323 memset(&tmp, 0, sizeof(tmp));
1324 tmp.type = info->type;
1325 tmp.line = info->tty->index;
1326 tmp.port = info->ioaddr;
1327 tmp.irq = info->board->irq;
1328 tmp.flags = info->flags;
1329 tmp.baud_base = info->baud_base;
1330 tmp.close_delay = info->close_delay;
1331 tmp.closing_wait = info->closing_wait;
1332 tmp.custom_divisor = info->custom_divisor;
1334 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1339 static int mxser_set_serial_info(struct mxser_port *info,
1340 struct serial_struct __user *new_info)
1342 struct serial_struct new_serial;
1346 if (!new_info || !info->ioaddr)
1348 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1351 if ((new_serial.irq != info->board->irq) ||
1352 (new_serial.port != info->ioaddr) ||
1353 (new_serial.custom_divisor != info->custom_divisor) ||
1354 (new_serial.baud_base != info->baud_base))
1357 flags = info->flags & ASYNC_SPD_MASK;
1359 if (!capable(CAP_SYS_ADMIN)) {
1360 if ((new_serial.baud_base != info->baud_base) ||
1361 (new_serial.close_delay != info->close_delay) ||
1362 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
1364 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1365 (new_serial.flags & ASYNC_USR_MASK));
1368 * OK, past this point, all the error checking has been done.
1369 * At this point, we start making changes.....
1371 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1372 (new_serial.flags & ASYNC_FLAGS));
1373 info->close_delay = new_serial.close_delay * HZ / 100;
1374 info->closing_wait = new_serial.closing_wait * HZ / 100;
1375 info->tty->low_latency =
1376 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1377 info->tty->low_latency = 0; /* (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0; */
1380 /* added by casper, 3/17/2000, for mouse */
1381 info->type = new_serial.type;
1383 process_txrx_fifo(info);
1385 if (info->flags & ASYNC_INITIALIZED) {
1386 if (flags != (info->flags & ASYNC_SPD_MASK))
1387 mxser_change_speed(info, NULL);
1389 retval = mxser_startup(info);
1395 * mxser_get_lsr_info - get line status register info
1397 * Purpose: Let user call ioctl() to get info when the UART physically
1398 * is emptied. On bus types like RS485, the transmitter must
1399 * release the bus after transmitting. This must be done when
1400 * the transmit shift register is empty, not be done when the
1401 * transmit holding register is empty. This functionality
1402 * allows an RS485 driver to be written in user space.
1404 static int mxser_get_lsr_info(struct mxser_port *info,
1405 unsigned int __user *value)
1407 unsigned char status;
1408 unsigned int result;
1409 unsigned long flags;
1411 spin_lock_irqsave(&info->slock, flags);
1412 status = inb(info->ioaddr + UART_LSR);
1413 spin_unlock_irqrestore(&info->slock, flags);
1414 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1415 return put_user(result, value);
1419 * This routine sends a break character out the serial port.
1421 static void mxser_send_break(struct mxser_port *info, int duration)
1423 unsigned long flags;
1427 set_current_state(TASK_INTERRUPTIBLE);
1428 spin_lock_irqsave(&info->slock, flags);
1429 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1430 info->ioaddr + UART_LCR);
1431 spin_unlock_irqrestore(&info->slock, flags);
1432 schedule_timeout(duration);
1433 spin_lock_irqsave(&info->slock, flags);
1434 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1435 info->ioaddr + UART_LCR);
1436 spin_unlock_irqrestore(&info->slock, flags);
1439 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
1441 struct mxser_port *info = tty->driver_data;
1442 unsigned char control, status;
1443 unsigned long flags;
1446 if (tty->index == MXSER_PORTS)
1447 return -ENOIOCTLCMD;
1448 if (tty->flags & (1 << TTY_IO_ERROR))
1451 control = info->MCR;
1453 spin_lock_irqsave(&info->slock, flags);
1454 status = inb(info->ioaddr + UART_MSR);
1455 if (status & UART_MSR_ANY_DELTA)
1456 mxser_check_modem_status(info, status);
1457 spin_unlock_irqrestore(&info->slock, flags);
1458 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1459 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1460 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1461 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1462 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1463 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1466 static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1467 unsigned int set, unsigned int clear)
1469 struct mxser_port *info = tty->driver_data;
1470 unsigned long flags;
1473 if (tty->index == MXSER_PORTS)
1474 return -ENOIOCTLCMD;
1475 if (tty->flags & (1 << TTY_IO_ERROR))
1478 spin_lock_irqsave(&info->slock, flags);
1480 if (set & TIOCM_RTS)
1481 info->MCR |= UART_MCR_RTS;
1482 if (set & TIOCM_DTR)
1483 info->MCR |= UART_MCR_DTR;
1485 if (clear & TIOCM_RTS)
1486 info->MCR &= ~UART_MCR_RTS;
1487 if (clear & TIOCM_DTR)
1488 info->MCR &= ~UART_MCR_DTR;
1490 outb(info->MCR, info->ioaddr + UART_MCR);
1491 spin_unlock_irqrestore(&info->slock, flags);
1495 static int mxser_program_mode(int port)
1498 /* unsigned long flags; */
1500 spin_lock(&gm_lock);
1508 /* restore_flags(flags); */
1509 spin_unlock(&gm_lock);
1511 id = inb(port + 1) & 0x1F;
1512 if ((id != C168_ASIC_ID) &&
1513 (id != C104_ASIC_ID) &&
1514 (id != C102_ASIC_ID) &&
1515 (id != CI132_ASIC_ID) &&
1516 (id != CI134_ASIC_ID) &&
1517 (id != CI104J_ASIC_ID))
1519 for (i = 0, j = 0; i < 4; i++) {
1523 } else if ((j == 1) && (n == 1)) {
1534 static void mxser_normal_mode(int port)
1538 outb(0xA5, port + 1);
1539 outb(0x80, port + 3);
1540 outb(12, port + 0); /* 9600 bps */
1542 outb(0x03, port + 3); /* 8 data bits */
1543 outb(0x13, port + 4); /* loop back mode */
1544 for (i = 0; i < 16; i++) {
1546 if ((n & 0x61) == 0x60)
1551 outb(0x00, port + 4);
1554 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1555 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1556 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1557 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1558 #define EN_CCMD 0x000 /* Chip's command register */
1559 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1560 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1561 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1562 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1563 #define EN0_DCFG 0x00E /* Data configuration reg WR */
1564 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1565 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1566 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1567 static int mxser_read_register(int port, unsigned short *regs)
1569 int i, k, value, id;
1572 id = mxser_program_mode(port);
1575 for (i = 0; i < 14; i++) {
1576 k = (i & 0x3F) | 0x180;
1577 for (j = 0x100; j > 0; j >>= 1) {
1578 outb(CHIP_CS, port);
1580 outb(CHIP_CS | CHIP_DO, port);
1581 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1583 outb(CHIP_CS, port);
1584 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1589 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1590 outb(CHIP_CS, port);
1591 outb(CHIP_CS | CHIP_SK, port);
1592 if (inb(port) & CHIP_DI)
1598 mxser_normal_mode(port);
1602 static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1604 struct mxser_port *port;
1610 /* if (copy_to_user(argp, mxsercfg,
1611 sizeof(struct mxser_hwconf) * 4))
1615 case MOXA_GET_MAJOR:
1616 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1620 case MOXA_GET_CUMAJOR:
1621 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1625 case MOXA_CHKPORTENABLE:
1628 for (i = 0; i < MXSER_BOARDS; i++)
1629 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1630 if (mxser_boards[i].ports[j].ioaddr)
1633 return put_user(result, (unsigned long __user *)argp);
1634 case MOXA_GETDATACOUNT:
1635 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1638 case MOXA_GETMSTATUS:
1639 for (i = 0; i < MXSER_BOARDS; i++)
1640 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1641 port = &mxser_boards[i].ports[j];
1644 if (!port->ioaddr) {
1645 GMStatus[i].dcd = 0;
1646 GMStatus[i].dsr = 0;
1647 GMStatus[i].cts = 0;
1651 if (!port->tty || !port->tty->termios)
1653 port->normal_termios.c_cflag;
1656 port->tty->termios->c_cflag;
1658 status = inb(port->ioaddr + UART_MSR);
1659 if (status & 0x80 /*UART_MSR_DCD */ )
1660 GMStatus[i].dcd = 1;
1662 GMStatus[i].dcd = 0;
1664 if (status & 0x20 /*UART_MSR_DSR */ )
1665 GMStatus[i].dsr = 1;
1667 GMStatus[i].dsr = 0;
1670 if (status & 0x10 /*UART_MSR_CTS */ )
1671 GMStatus[i].cts = 1;
1673 GMStatus[i].cts = 0;
1675 if (copy_to_user(argp, GMStatus,
1676 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1679 case MOXA_ASPP_MON_EXT: {
1680 int status, p, shiftbit;
1681 unsigned long opmode;
1682 unsigned cflag, iflag;
1684 for (i = 0; i < MXSER_BOARDS; i++)
1685 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1686 port = &mxser_boards[i].ports[j];
1690 status = mxser_get_msr(port->ioaddr, 0, i);
1691 /* mxser_check_modem_status(port, status); */
1693 if (status & UART_MSR_TERI)
1695 if (status & UART_MSR_DDSR)
1697 if (status & UART_MSR_DDCD)
1699 if (status & UART_MSR_DCTS)
1702 port->mon_data.modem_status = status;
1703 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1704 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1705 mon_data_ext.up_rxcnt[i] =
1706 port->mon_data.up_rxcnt;
1707 mon_data_ext.up_txcnt[i] =
1708 port->mon_data.up_txcnt;
1709 mon_data_ext.modem_status[i] =
1710 port->mon_data.modem_status;
1711 mon_data_ext.baudrate[i] = port->realbaud;
1713 if (!port->tty || !port->tty->termios) {
1714 cflag = port->normal_termios.c_cflag;
1715 iflag = port->normal_termios.c_iflag;
1717 cflag = port->tty->termios->c_cflag;
1718 iflag = port->tty->termios->c_iflag;
1721 mon_data_ext.databits[i] = cflag & CSIZE;
1723 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1725 mon_data_ext.parity[i] =
1726 cflag & (PARENB | PARODD | CMSPAR);
1728 mon_data_ext.flowctrl[i] = 0x00;
1730 if (cflag & CRTSCTS)
1731 mon_data_ext.flowctrl[i] |= 0x03;
1733 if (iflag & (IXON | IXOFF))
1734 mon_data_ext.flowctrl[i] |= 0x0C;
1736 if (port->type == PORT_16550A)
1737 mon_data_ext.fifo[i] = 1;
1739 mon_data_ext.fifo[i] = 0;
1743 opmode = inb(port->opmode_ioaddr) >> shiftbit;
1744 opmode &= OP_MODE_MASK;
1746 mon_data_ext.iftype[i] = opmode;
1749 if (copy_to_user(argp, &mon_data_ext,
1750 sizeof(mon_data_ext)))
1756 return -ENOIOCTLCMD;
1761 static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1762 unsigned int cmd, unsigned long arg)
1764 struct mxser_port *info = tty->driver_data;
1765 struct async_icount cprev, cnow; /* kernel counter temps */
1766 struct serial_icounter_struct __user *p_cuser;
1767 unsigned long templ;
1768 unsigned long flags;
1769 void __user *argp = (void __user *)arg;
1772 if (tty->index == MXSER_PORTS)
1773 return mxser_ioctl_special(cmd, argp);
1775 /* following add by Victor Yu. 01-05-2004 */
1776 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1778 unsigned long opmode;
1779 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1781 unsigned char val, mask;
1784 if (cmd == MOXA_SET_OP_MODE) {
1785 if (get_user(opmode, (int __user *) argp))
1787 if (opmode != RS232_MODE &&
1788 opmode != RS485_2WIRE_MODE &&
1789 opmode != RS422_MODE &&
1790 opmode != RS485_4WIRE_MODE)
1794 val = inb(info->opmode_ioaddr);
1796 val |= (opmode << shiftbit);
1797 outb(val, info->opmode_ioaddr);
1800 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1801 opmode &= OP_MODE_MASK;
1802 if (copy_to_user(argp, &opmode, sizeof(int)))
1807 /* above add by Victor Yu. 01-05-2004 */
1809 if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1810 if (tty->flags & (1 << TTY_IO_ERROR))
1814 case TCSBRK: /* SVID version: non-zero arg --> no break */
1815 retval = tty_check_change(tty);
1818 tty_wait_until_sent(tty, 0);
1820 mxser_send_break(info, HZ / 4); /* 1/4 second */
1822 case TCSBRKP: /* support for POSIX tcsendbreak() */
1823 retval = tty_check_change(tty);
1826 tty_wait_until_sent(tty, 0);
1827 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1830 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1832 if (get_user(templ, (unsigned long __user *) argp))
1835 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1838 return mxser_get_serial_info(info, argp);
1840 return mxser_set_serial_info(info, argp);
1841 case TIOCSERGETLSR: /* Get line status register */
1842 return mxser_get_lsr_info(info, argp);
1844 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1845 * - mask passed in arg for lines of interest
1846 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1847 * Caller should use TIOCGICOUNT to see which one it was
1850 DECLARE_WAITQUEUE(wait, current);
1852 spin_lock_irqsave(&info->slock, flags);
1853 cprev = info->icount; /* note the counters on entry */
1854 spin_unlock_irqrestore(&info->slock, flags);
1856 add_wait_queue(&info->delta_msr_wait, &wait);
1858 spin_lock_irqsave(&info->slock, flags);
1859 cnow = info->icount; /* atomic copy */
1860 spin_unlock_irqrestore(&info->slock, flags);
1862 set_current_state(TASK_INTERRUPTIBLE);
1863 if (((arg & TIOCM_RNG) &&
1864 (cnow.rng != cprev.rng)) ||
1865 ((arg & TIOCM_DSR) &&
1866 (cnow.dsr != cprev.dsr)) ||
1867 ((arg & TIOCM_CD) &&
1868 (cnow.dcd != cprev.dcd)) ||
1869 ((arg & TIOCM_CTS) &&
1870 (cnow.cts != cprev.cts))) {
1874 /* see if a signal did it */
1875 if (signal_pending(current)) {
1881 current->state = TASK_RUNNING;
1882 remove_wait_queue(&info->delta_msr_wait, &wait);
1887 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1888 * Return: write counters to the user passed counter struct
1889 * NB: both 1->0 and 0->1 transitions are counted except for
1890 * RI where only 0->1 is counted.
1893 spin_lock_irqsave(&info->slock, flags);
1894 cnow = info->icount;
1895 spin_unlock_irqrestore(&info->slock, flags);
1897 /* modified by casper 1/11/2000 */
1898 if (put_user(cnow.frame, &p_cuser->frame))
1900 if (put_user(cnow.brk, &p_cuser->brk))
1902 if (put_user(cnow.overrun, &p_cuser->overrun))
1904 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1906 if (put_user(cnow.parity, &p_cuser->parity))
1908 if (put_user(cnow.rx, &p_cuser->rx))
1910 if (put_user(cnow.tx, &p_cuser->tx))
1912 put_user(cnow.cts, &p_cuser->cts);
1913 put_user(cnow.dsr, &p_cuser->dsr);
1914 put_user(cnow.rng, &p_cuser->rng);
1915 put_user(cnow.dcd, &p_cuser->dcd);
1917 case MOXA_HighSpeedOn:
1918 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1919 case MOXA_SDS_RSTICOUNTER:
1920 info->mon_data.rxcnt = 0;
1921 info->mon_data.txcnt = 0;
1923 /* (above) added by James. */
1924 case MOXA_ASPP_SETBAUD:{
1926 if (get_user(baud, (long __user *)argp))
1928 mxser_set_baud(info, baud);
1931 case MOXA_ASPP_GETBAUD:
1932 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1937 case MOXA_ASPP_OQUEUE:{
1940 len = mxser_chars_in_buffer(tty);
1942 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1944 len += (lsr ? 0 : 1);
1946 if (copy_to_user(argp, &len, sizeof(int)))
1951 case MOXA_ASPP_MON: {
1954 /* info->mon_data.ser_param = tty->termios->c_cflag; */
1956 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1957 mxser_check_modem_status(info, status);
1959 mcr = inb(info->ioaddr + UART_MCR);
1960 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1961 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1963 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1965 if (mcr & MOXA_MUST_MCR_TX_XON)
1966 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1968 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1970 if (info->tty->hw_stopped)
1971 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1973 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1975 if (copy_to_user(argp, &info->mon_data,
1976 sizeof(struct mxser_mon)))
1981 case MOXA_ASPP_LSTATUS: {
1982 if (copy_to_user(argp, &info->err_shadow,
1983 sizeof(unsigned char)))
1986 info->err_shadow = 0;
1989 case MOXA_SET_BAUD_METHOD: {
1992 if (get_user(method, (int __user *)argp))
1994 mxser_set_baud_method[tty->index] = method;
1995 if (copy_to_user(argp, &method, sizeof(int)))
2001 return -ENOIOCTLCMD;
2006 static void mxser_stoprx(struct tty_struct *tty)
2008 struct mxser_port *info = tty->driver_data;
2010 info->ldisc_stop_rx = 1;
2012 /* following add by Victor Yu. 09-02-2002 */
2013 if (info->board->chip_flag) {
2014 info->IER &= ~MOXA_MUST_RECV_ISR;
2015 outb(info->IER, info->ioaddr + UART_IER);
2016 } else if (!(info->flags & ASYNC_CLOSING)) {
2017 info->x_char = STOP_CHAR(tty);
2018 outb(0, info->ioaddr + UART_IER);
2019 info->IER |= UART_IER_THRI;
2020 outb(info->IER, info->ioaddr + UART_IER);
2024 if (info->tty->termios->c_cflag & CRTSCTS) {
2025 info->MCR &= ~UART_MCR_RTS;
2026 outb(info->MCR, info->ioaddr + UART_MCR);
2031 * This routine is called by the upper-layer tty layer to signal that
2032 * incoming characters should be throttled.
2034 static void mxser_throttle(struct tty_struct *tty)
2039 static void mxser_unthrottle(struct tty_struct *tty)
2041 struct mxser_port *info = tty->driver_data;
2044 info->ldisc_stop_rx = 0;
2049 /* following add by Victor Yu. 09-02-2002 */
2050 if (info->board->chip_flag) {
2051 info->IER |= MOXA_MUST_RECV_ISR;
2052 outb(info->IER, info->ioaddr + UART_IER);
2053 } else if (!(info->flags & ASYNC_CLOSING)) {
2054 info->x_char = START_CHAR(tty);
2055 outb(0, info->ioaddr + UART_IER);
2056 info->IER |= UART_IER_THRI;
2057 outb(info->IER, info->ioaddr + UART_IER);
2062 if (info->tty->termios->c_cflag & CRTSCTS) {
2063 info->MCR |= UART_MCR_RTS;
2064 outb(info->MCR, info->ioaddr + UART_MCR);
2069 * mxser_stop() and mxser_start()
2071 * This routines are called before setting or resetting tty->stopped.
2072 * They enable or disable transmitter interrupts, as necessary.
2074 static void mxser_stop(struct tty_struct *tty)
2076 struct mxser_port *info = tty->driver_data;
2077 unsigned long flags;
2079 spin_lock_irqsave(&info->slock, flags);
2080 if (info->IER & UART_IER_THRI) {
2081 info->IER &= ~UART_IER_THRI;
2082 outb(info->IER, info->ioaddr + UART_IER);
2084 spin_unlock_irqrestore(&info->slock, flags);
2087 static void mxser_start(struct tty_struct *tty)
2089 struct mxser_port *info = tty->driver_data;
2090 unsigned long flags;
2092 spin_lock_irqsave(&info->slock, flags);
2093 if (info->xmit_cnt && info->xmit_buf
2094 /* && !(info->IER & UART_IER_THRI) */) {
2095 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
2096 info->IER |= UART_IER_THRI;
2097 outb(info->IER, info->ioaddr + UART_IER);
2099 spin_unlock_irqrestore(&info->slock, flags);
2102 static void mxser_set_termios(struct tty_struct *tty, struct termios *old_termios)
2104 struct mxser_port *info = tty->driver_data;
2105 unsigned long flags;
2107 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
2108 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
2110 mxser_change_speed(info, old_termios);
2112 if ((old_termios->c_cflag & CRTSCTS) &&
2113 !(tty->termios->c_cflag & CRTSCTS)) {
2114 tty->hw_stopped = 0;
2119 /* Handle sw stopped */
2120 if ((old_termios->c_iflag & IXON) &&
2121 !(tty->termios->c_iflag & IXON)) {
2124 /* following add by Victor Yu. 09-02-2002 */
2125 if (info->board->chip_flag) {
2126 spin_lock_irqsave(&info->slock, flags);
2127 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2128 spin_unlock_irqrestore(&info->slock, flags);
2130 /* above add by Victor Yu. 09-02-2002 */
2137 * mxser_wait_until_sent() --- wait until the transmitter is empty
2139 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2141 struct mxser_port *info = tty->driver_data;
2142 unsigned long orig_jiffies, char_time;
2145 if (info->type == PORT_UNKNOWN)
2148 if (info->xmit_fifo_size == 0)
2149 return; /* Just in case.... */
2151 orig_jiffies = jiffies;
2153 * Set the check interval to be 1/5 of the estimated time to
2154 * send a single character, and make it at least 1. The check
2155 * interval should also be less than the timeout.
2157 * Note: we have to use pretty tight timings here to satisfy
2160 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2161 char_time = char_time / 5;
2164 if (timeout && timeout < char_time)
2165 char_time = timeout;
2167 * If the transmitter hasn't cleared in twice the approximate
2168 * amount of time to send the entire FIFO, it probably won't
2169 * ever clear. This assumes the UART isn't doing flow
2170 * control, which is currently the case. Hence, if it ever
2171 * takes longer than info->timeout, this is probably due to a
2172 * UART bug of some kind. So, we clamp the timeout parameter at
2175 if (!timeout || timeout > 2 * info->timeout)
2176 timeout = 2 * info->timeout;
2177 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2178 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2179 timeout, char_time);
2180 printk("jiff=%lu...", jiffies);
2182 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
2183 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2184 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2186 schedule_timeout_interruptible(char_time);
2187 if (signal_pending(current))
2189 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2192 set_current_state(TASK_RUNNING);
2194 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2195 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2201 * This routine is called by tty_hangup() when a hangup is signaled.
2203 void mxser_hangup(struct tty_struct *tty)
2205 struct mxser_port *info = tty->driver_data;
2207 mxser_flush_buffer(tty);
2208 mxser_shutdown(info);
2211 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2213 wake_up_interruptible(&info->open_wait);
2217 /* added by James 03-12-2004. */
2219 * mxser_rs_break() --- routine which turns the break handling on or off
2221 static void mxser_rs_break(struct tty_struct *tty, int break_state)
2223 struct mxser_port *info = tty->driver_data;
2224 unsigned long flags;
2226 spin_lock_irqsave(&info->slock, flags);
2227 if (break_state == -1)
2228 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2229 info->ioaddr + UART_LCR);
2231 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2232 info->ioaddr + UART_LCR);
2233 spin_unlock_irqrestore(&info->slock, flags);
2236 /* (above) added by James. */
2238 static void mxser_receive_chars(struct mxser_port *port, int *status)
2240 struct tty_struct *tty = port->tty;
2241 unsigned char ch, gdl;
2246 unsigned long flags;
2248 spin_lock_irqsave(&port->slock, flags);
2250 recv_room = tty->receive_room;
2251 if ((recv_room == 0) && (!port->ldisc_stop_rx)) {
2252 /* mxser_throttle(tty); */
2257 /* following add by Victor Yu. 09-02-2002 */
2258 if (port->board->chip_flag != MOXA_OTHER_UART) {
2260 if (*status & UART_LSR_SPECIAL)
2262 /* following add by Victor Yu. 02-11-2004 */
2263 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
2264 (*status & MOXA_MUST_LSR_RERR))
2266 /* above add by Victor Yu. 02-14-2004 */
2267 if (*status & MOXA_MUST_LSR_RERR)
2270 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
2272 /* add by Victor Yu. 02-11-2004 */
2273 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
2274 gdl &= MOXA_MUST_GDL_MASK;
2275 if (gdl >= recv_room) {
2276 if (!port->ldisc_stop_rx) {
2277 /* mxser_throttle(tty); */
2283 ch = inb(port->ioaddr + UART_RX);
2284 tty_insert_flip_char(tty, ch, 0);
2290 /* above add by Victor Yu. 09-02-2002 */
2296 ch = inb(port->ioaddr + UART_RX);
2297 /* following add by Victor Yu. 09-02-2002 */
2298 if (port->board->chip_flag && (*status & UART_LSR_OE)
2299 /*&& !(*status&UART_LSR_DR) */)
2300 outb(0x23, port->ioaddr + UART_FCR);
2301 *status &= port->read_status_mask;
2302 /* above add by Victor Yu. 09-02-2002 */
2303 if (*status & port->ignore_status_mask) {
2304 if (++ignored > 100)
2308 if (*status & UART_LSR_SPECIAL) {
2309 if (*status & UART_LSR_BI) {
2311 /* added by casper 1/11/2000 */
2314 if (port->flags & ASYNC_SAK)
2316 } else if (*status & UART_LSR_PE) {
2318 /* added by casper 1/11/2000 */
2319 port->icount.parity++;
2320 } else if (*status & UART_LSR_FE) {
2322 /* added by casper 1/11/2000 */
2323 port->icount.frame++;
2324 } else if (*status & UART_LSR_OE) {
2326 /* added by casper 1/11/2000 */
2327 port->icount.overrun++;
2332 tty_insert_flip_char(tty, ch, flag);
2334 if (cnt >= recv_room) {
2335 if (!port->ldisc_stop_rx) {
2336 /* mxser_throttle(tty); */
2344 /* following add by Victor Yu. 09-02-2002 */
2345 if (port->board->chip_flag)
2348 /* mask by Victor Yu. 09-02-2002
2349 *status = inb(port->ioaddr + UART_LSR) & port->read_status_mask;
2351 /* following add by Victor Yu. 09-02-2002 */
2352 *status = inb(port->ioaddr + UART_LSR);
2353 /* above add by Victor Yu. 09-02-2002 */
2354 } while (*status & UART_LSR_DR);
2356 end_intr: /* add by Victor Yu. 09-02-2002 */
2357 mxvar_log.rxcnt[port->tty->index] += cnt;
2358 port->mon_data.rxcnt += cnt;
2359 port->mon_data.up_rxcnt += cnt;
2360 spin_unlock_irqrestore(&port->slock, flags);
2362 tty_flip_buffer_push(tty);
2365 static void mxser_transmit_chars(struct mxser_port *port)
2368 unsigned long flags;
2370 spin_lock_irqsave(&port->slock, flags);
2373 outb(port->x_char, port->ioaddr + UART_TX);
2375 mxvar_log.txcnt[port->tty->index]++;
2376 port->mon_data.txcnt++;
2377 port->mon_data.up_txcnt++;
2379 /* added by casper 1/11/2000 */
2384 if (port->xmit_buf == 0)
2387 if (port->xmit_cnt == 0) {
2388 if (port->xmit_cnt < WAKEUP_CHARS) { /* XXX what's this for?? */
2389 set_bit(MXSER_EVENT_TXLOW, &port->event);
2390 schedule_work(&port->tqueue);
2394 if (port->tty->stopped || (port->tty->hw_stopped &&
2395 (port->type != PORT_16550A) &&
2396 (!port->board->chip_flag))) {
2397 port->IER &= ~UART_IER_THRI;
2398 outb(port->IER, port->ioaddr + UART_IER);
2402 cnt = port->xmit_cnt;
2403 count = port->xmit_fifo_size;
2405 outb(port->xmit_buf[port->xmit_tail++],
2406 port->ioaddr + UART_TX);
2407 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2408 if (--port->xmit_cnt <= 0)
2410 } while (--count > 0);
2411 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
2413 /* added by James 03-12-2004. */
2414 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2415 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2417 /* added by casper 1/11/2000 */
2418 port->icount.tx += (cnt - port->xmit_cnt);
2420 if (port->xmit_cnt < WAKEUP_CHARS) {
2421 set_bit(MXSER_EVENT_TXLOW, &port->event);
2422 schedule_work(&port->tqueue);
2424 if (port->xmit_cnt <= 0) {
2425 port->IER &= ~UART_IER_THRI;
2426 outb(port->IER, port->ioaddr + UART_IER);
2429 spin_unlock_irqrestore(&port->slock, flags);
2433 * This is the serial driver's generic interrupt routine
2435 static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
2438 struct mxser_board *brd = NULL;
2439 struct mxser_port *port;
2440 int max, irqbits, bits, msr;
2441 int pass_counter = 0;
2442 unsigned int int_cnt;
2443 int handled = IRQ_NONE;
2445 /* spin_lock(&gm_lock); */
2447 for (i = 0; i < MXSER_BOARDS; i++)
2448 if (dev_id == &mxser_boards[i]) {
2453 if (i == MXSER_BOARDS)
2457 max = mxser_numports[brd->board_type - 1];
2459 irqbits = inb(brd->vector) & brd->vector_mask;
2460 if (irqbits == brd->vector_mask)
2463 handled = IRQ_HANDLED;
2464 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2465 if (irqbits == brd->vector_mask)
2469 port = &brd->ports[i];
2473 /* following add by Victor Yu. 09-13-2002 */
2474 iir = inb(port->ioaddr + UART_IIR);
2475 if (iir & UART_IIR_NO_INT)
2477 iir &= MOXA_MUST_IIR_MASK;
2479 status = inb(port->ioaddr + UART_LSR);
2480 outb(0x27, port->ioaddr + UART_FCR);
2481 inb(port->ioaddr + UART_MSR);
2484 /* above add by Victor Yu. 09-13-2002 */
2486 /* following add by Victor Yu. 09-02-2002 */
2487 status = inb(port->ioaddr + UART_LSR);
2489 if (status & UART_LSR_PE)
2490 port->err_shadow |= NPPI_NOTIFY_PARITY;
2491 if (status & UART_LSR_FE)
2492 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2493 if (status & UART_LSR_OE)
2495 NPPI_NOTIFY_HW_OVERRUN;
2496 if (status & UART_LSR_BI)
2497 port->err_shadow |= NPPI_NOTIFY_BREAK;
2499 if (port->board->chip_flag) {
2501 if ( (status & 0x02) && !(status & 0x01) ) {
2502 outb(port->ioaddr+UART_FCR, 0x23);
2506 if (iir == MOXA_MUST_IIR_GDA ||
2507 iir == MOXA_MUST_IIR_RDA ||
2508 iir == MOXA_MUST_IIR_RTO ||
2509 iir == MOXA_MUST_IIR_LSR)
2510 mxser_receive_chars(port,
2514 /* above add by Victor Yu. 09-02-2002 */
2516 status &= port->read_status_mask;
2517 if (status & UART_LSR_DR)
2518 mxser_receive_chars(port,
2521 msr = inb(port->ioaddr + UART_MSR);
2522 if (msr & UART_MSR_ANY_DELTA)
2523 mxser_check_modem_status(port, msr);
2525 /* following add by Victor Yu. 09-13-2002 */
2526 if (port->board->chip_flag) {
2527 if (iir == 0x02 && (status &
2529 mxser_transmit_chars(port);
2531 /* above add by Victor Yu. 09-13-2002 */
2533 if (status & UART_LSR_THRE)
2534 mxser_transmit_chars(port);
2536 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2538 if (pass_counter++ > MXSER_ISR_PASS_LIMIT)
2539 break; /* Prevent infinite loops */
2543 /* spin_unlock(&gm_lock); */
2547 static const struct tty_operations mxser_ops = {
2549 .close = mxser_close,
2550 .write = mxser_write,
2551 .put_char = mxser_put_char,
2552 .flush_chars = mxser_flush_chars,
2553 .write_room = mxser_write_room,
2554 .chars_in_buffer = mxser_chars_in_buffer,
2555 .flush_buffer = mxser_flush_buffer,
2556 .ioctl = mxser_ioctl,
2557 .throttle = mxser_throttle,
2558 .unthrottle = mxser_unthrottle,
2559 .set_termios = mxser_set_termios,
2561 .start = mxser_start,
2562 .hangup = mxser_hangup,
2563 .break_ctl = mxser_rs_break,
2564 .wait_until_sent = mxser_wait_until_sent,
2565 .tiocmget = mxser_tiocmget,
2566 .tiocmset = mxser_tiocmset,
2570 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2573 static int __devinit mxser_initbrd(struct mxser_board *brd)
2575 struct mxser_port *info;
2579 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
2581 for (i = 0; i < brd->nports; i++) {
2582 info = &brd->ports[i];
2585 info->ldisc_stop_rx = 0;
2587 /* Enhance mode enabled here */
2588 if (brd->chip_flag != MOXA_OTHER_UART)
2589 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
2591 info->flags = ASYNC_SHARE_IRQ;
2592 info->type = brd->uart_type;
2594 process_txrx_fifo(info);
2596 info->custom_divisor = info->baud_base * 16;
2597 info->close_delay = 5 * HZ / 10;
2598 info->closing_wait = 30 * HZ;
2599 INIT_WORK(&info->tqueue, mxser_do_softint, info);
2600 info->normal_termios = mxvar_sdriver->init_termios;
2601 init_waitqueue_head(&info->open_wait);
2602 init_waitqueue_head(&info->close_wait);
2603 init_waitqueue_head(&info->delta_msr_wait);
2604 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2605 info->err_shadow = 0;
2606 spin_lock_init(&info->slock);
2608 /* before set INT ISR, disable all int */
2609 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2610 info->ioaddr + UART_IER);
2613 * Allocate the IRQ if necessary
2616 retval = request_irq(brd->irq, mxser_interrupt,
2617 (brd->ports[0].flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED :
2618 IRQF_DISABLED, "mxser", brd);
2620 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2621 "conflict with another device.\n",
2622 mxser_brdname[brd->board_type - 1], brd->irq);
2628 static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
2631 unsigned short regs[16], irq;
2632 unsigned char scratch, scratch2;
2634 brd->chip_flag = MOXA_OTHER_UART;
2636 id = mxser_read_register(cap, regs);
2637 if (id == C168_ASIC_ID) {
2638 brd->board_type = MXSER_BOARD_C168_ISA;
2640 } else if (id == C104_ASIC_ID) {
2641 brd->board_type = MXSER_BOARD_C104_ISA;
2643 } else if (id == C102_ASIC_ID) {
2644 brd->board_type = MXSER_BOARD_C102_ISA;
2646 } else if (id == CI132_ASIC_ID) {
2647 brd->board_type = MXSER_BOARD_CI132;
2649 } else if (id == CI134_ASIC_ID) {
2650 brd->board_type = MXSER_BOARD_CI134;
2652 } else if (id == CI104J_ASIC_ID) {
2653 brd->board_type = MXSER_BOARD_CI104J;
2659 if (brd->nports == 2) {
2660 irq = regs[9] & 0xF000;
2661 irq = irq | (irq >> 4);
2662 if (irq != (regs[9] & 0xFF00))
2663 return MXSER_ERR_IRQ_CONFLIT;
2664 } else if (brd->nports == 4) {
2665 irq = regs[9] & 0xF000;
2666 irq = irq | (irq >> 4);
2667 irq = irq | (irq >> 8);
2669 return MXSER_ERR_IRQ_CONFLIT;
2670 } else if (brd->nports == 8) {
2671 irq = regs[9] & 0xF000;
2672 irq = irq | (irq >> 4);
2673 irq = irq | (irq >> 8);
2674 if ((irq != regs[9]) || (irq != regs[10]))
2675 return MXSER_ERR_IRQ_CONFLIT;
2679 return MXSER_ERR_IRQ;
2680 brd->irq = ((int)(irq & 0xF000) >> 12);
2681 for (i = 0; i < 8; i++)
2682 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
2683 if ((regs[12] & 0x80) == 0)
2684 return MXSER_ERR_VECTOR;
2685 brd->vector = (int)regs[11]; /* interrupt vector */
2687 brd->vector_mask = 0x00FF;
2689 brd->vector_mask = 0x000F;
2690 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2691 if (regs[12] & bits) {
2692 brd->ports[i].baud_base = 921600;
2693 brd->ports[i].max_baud = 921600; /* add by Victor Yu. 09-04-2002 */
2695 brd->ports[i].baud_base = 115200;
2696 brd->ports[i].max_baud = 115200; /* add by Victor Yu. 09-04-2002 */
2699 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2700 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2701 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2702 outb(scratch2, cap + UART_LCR);
2703 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2704 scratch = inb(cap + UART_IIR);
2707 brd->uart_type = PORT_16550A;
2709 brd->uart_type = PORT_16450;
2714 if (!request_region(brd->ports[0].ioaddr, 8 * brd->nports, "mxser(IO)"))
2715 return MXSER_ERR_IOADDR;
2716 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2717 release_region(brd->ports[0].ioaddr, 8 * brd->nports);
2718 return MXSER_ERR_VECTOR;
2723 static int __init mxser_get_PCI_conf(int board_type, struct mxser_board *brd,
2724 struct pci_dev *pdev)
2727 unsigned long ioaddress;
2731 brd->board_type = board_type;
2732 brd->nports = mxser_numports[board_type - 1];
2733 ioaddress = pci_resource_start(pdev, 2);
2734 retval = pci_request_region(pdev, 2, "mxser(IO)");
2738 for (i = 0; i < brd->nports; i++)
2739 brd->ports[i].ioaddr = ioaddress + 8 * i;
2742 ioaddress = pci_resource_start(pdev, 3);
2743 retval = pci_request_region(pdev, 3, "mxser(vector)");
2746 brd->vector = ioaddress;
2749 brd->irq = pdev->irq;
2751 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2752 brd->uart_type = PORT_16550A;
2753 brd->vector_mask = 0;
2755 for (i = 0; i < brd->nports; i++) {
2756 for (j = 0; j < UART_INFO_NUM; j++) {
2757 if (Gpci_uart_info[j].type == brd->chip_flag) {
2758 brd->ports[i].max_baud =
2759 Gpci_uart_info[j].max_baud;
2761 /* exception....CP-102 */
2762 if (board_type == MXSER_BOARD_CP102)
2763 brd->ports[i].max_baud = 921600;
2769 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2770 for (i = 0; i < brd->nports; i++) {
2772 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2774 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
2776 outb(0, ioaddress + 4); /* default set to RS232 mode */
2777 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
2780 for (i = 0; i < brd->nports; i++) {
2781 brd->vector_mask |= (1 << i);
2782 brd->ports[i].baud_base = 921600;
2786 pci_release_region(pdev, 2);
2791 static int __init mxser_module_init(void)
2793 struct pci_dev *pdev = NULL;
2794 struct mxser_board *brd;
2798 pr_debug("Loading module mxser ...\n");
2800 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2803 spin_lock_init(&gm_lock);
2805 for (i = 0; i < MXSER_BOARDS; i++)
2806 mxser_boards[i].board_type = -1;
2808 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2811 /* Initialize the tty_driver structure */
2812 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2813 mxvar_sdriver->name = "ttyM";
2814 mxvar_sdriver->major = ttymajor;
2815 mxvar_sdriver->minor_start = 0;
2816 mxvar_sdriver->num = MXSER_PORTS + 1;
2817 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2818 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2819 mxvar_sdriver->init_termios = tty_std_termios;
2820 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2821 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
2822 tty_set_operations(mxvar_sdriver, &mxser_ops);
2823 mxvar_sdriver->ttys = mxvar_tty;
2824 mxvar_sdriver->termios = mxvar_termios;
2825 mxvar_sdriver->termios_locked = mxvar_termios_locked;
2830 /* Start finding ISA boards here */
2831 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2834 if (!(cap = mxserBoardCAP[b]))
2837 brd = &mxser_boards[m];
2838 retval = mxser_get_ISA_conf(cap, brd);
2841 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
2842 mxser_brdname[brd->board_type - 1], ioaddr[b]);
2845 if (retval == MXSER_ERR_IRQ)
2846 printk(KERN_ERR "Invalid interrupt number, "
2847 "board not configured\n");
2848 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2849 printk(KERN_ERR "Invalid interrupt number, "
2850 "board not configured\n");
2851 else if (retval == MXSER_ERR_VECTOR)
2852 printk(KERN_ERR "Invalid interrupt vector, "
2853 "board not configured\n");
2854 else if (retval == MXSER_ERR_IOADDR)
2855 printk(KERN_ERR "Invalid I/O address, "
2856 "board not configured\n");
2863 /* mxser_initbrd will hook ISR. */
2864 if (mxser_initbrd(brd) < 0)
2870 /* Start finding ISA boards from module arg */
2871 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2874 if (!(cap = ioaddr[b]))
2877 brd = &mxser_boards[m];
2878 retval = mxser_get_ISA_conf(cap, &mxser_boards[m]);
2881 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
2882 mxser_brdname[brd->board_type - 1], ioaddr[b]);
2885 if (retval == MXSER_ERR_IRQ)
2886 printk(KERN_ERR "Invalid interrupt number, "
2887 "board not configured\n");
2888 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2889 printk(KERN_ERR "Invalid interrupt number, "
2890 "board not configured\n");
2891 else if (retval == MXSER_ERR_VECTOR)
2892 printk(KERN_ERR "Invalid interrupt vector, "
2893 "board not configured\n");
2894 else if (retval == MXSER_ERR_IOADDR)
2895 printk(KERN_ERR "Invalid I/O address, "
2896 "board not configured\n");
2902 /* mxser_initbrd will hook ISR. */
2903 if (mxser_initbrd(brd) < 0)
2909 /* start finding PCI board here */
2910 n = ARRAY_SIZE(mxser_pcibrds) - 1;
2913 pdev = pci_get_device(mxser_pcibrds[b].vendor,
2914 mxser_pcibrds[b].device, pdev);
2919 printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
2920 mxser_brdname[(int) (mxser_pcibrds[b].driver_data) - 1],
2921 pdev->bus->number, PCI_SLOT(pdev->devfn));
2922 if (m >= MXSER_BOARDS)
2924 "Too many Smartio/Industio family boards find "
2925 "(maximum %d), board not configured\n",
2928 if (pci_enable_device(pdev)) {
2929 printk(KERN_ERR "Moxa SmartI/O PCI enable "
2933 brd = &mxser_boards[m];
2935 retval = mxser_get_PCI_conf(
2936 (int)mxser_pcibrds[b].driver_data,
2939 if (retval == MXSER_ERR_IRQ)
2941 "Invalid interrupt number, "
2942 "board not configured\n");
2943 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2945 "Invalid interrupt number, "
2946 "board not configured\n");
2947 else if (retval == MXSER_ERR_VECTOR)
2949 "Invalid interrupt vector, "
2950 "board not configured\n");
2951 else if (retval == MXSER_ERR_IOADDR)
2953 "Invalid I/O address, "
2954 "board not configured\n");
2957 /* mxser_initbrd will hook ISR. */
2958 if (mxser_initbrd(brd) < 0)
2961 /* Keep an extra reference if we succeeded. It will
2962 be returned at unload time */
2967 retval = tty_register_driver(mxvar_sdriver);
2969 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family"
2971 put_tty_driver(mxvar_sdriver);
2973 for (i = 0; i < MXSER_BOARDS; i++) {
2974 if (mxser_boards[i].board_type == -1)
2977 free_irq(mxser_boards[i].irq, &mxser_boards[i]);
2978 /* todo: release io, vector */
2984 pr_debug("Done.\n");
2989 static void __exit mxser_module_exit(void)
2993 pr_debug("Unloading module mxser ...\n");
2995 err = tty_unregister_driver(mxvar_sdriver);
2997 put_tty_driver(mxvar_sdriver);
2999 printk(KERN_ERR "Couldn't unregister MOXA Smartio/Industio family serial driver\n");
3001 for (i = 0; i < MXSER_BOARDS; i++) {
3002 struct pci_dev *pdev;
3004 if (mxser_boards[i].board_type == -1)
3007 pdev = mxser_boards[i].pdev;
3008 free_irq(mxser_boards[i].irq, &mxser_boards[i]);
3009 if (pdev != NULL) { /* PCI */
3010 pci_release_region(pdev, 2);
3011 pci_release_region(pdev, 3);
3014 release_region(mxser_boards[i].ports[0].ioaddr, 8 * mxser_boards[i].nports);
3015 release_region(mxser_boards[i].vector, 1);
3019 pr_debug("Done.\n");
3022 module_init(mxser_module_init);
3023 module_exit(mxser_module_exit);