2 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
4 * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com.tw).
6 * This code is loosely based on the Linux serial driver, written by
7 * Linus Torvalds, Theodore T'so and others.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
24 * <alan@redhat.com>. The original 1.8 code is available on www.moxa.com.
25 * - Fixed x86_64 cleanness
26 * - Fixed sleep with spinlock held in mxser_send_break
30 #include <linux/module.h>
31 #include <linux/autoconf.h>
32 #include <linux/errno.h>
33 #include <linux/signal.h>
34 #include <linux/sched.h>
35 #include <linux/timer.h>
36 #include <linux/interrupt.h>
37 #include <linux/tty.h>
38 #include <linux/tty_flip.h>
39 #include <linux/serial.h>
40 #include <linux/serial_reg.h>
41 #include <linux/major.h>
42 #include <linux/string.h>
43 #include <linux/fcntl.h>
44 #include <linux/ptrace.h>
45 #include <linux/gfp.h>
46 #include <linux/ioport.h>
48 #include <linux/smp_lock.h>
49 #include <linux/delay.h>
50 #include <linux/pci.h>
52 #include <asm/system.h>
55 #include <asm/bitops.h>
56 #include <asm/uaccess.h>
58 #include "mxser_new.h"
60 #define MXSER_VERSION "2.0"
61 #define MXSERMAJOR 174
62 #define MXSERCUMAJOR 175
64 #define MXSER_EVENT_TXLOW 1
65 #define MXSER_EVENT_HANGUP 2
67 #define MXSER_BOARDS 4 /* Max. boards */
68 #define MXSER_PORTS 32 /* Max. ports */
69 #define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
70 #define MXSER_ISR_PASS_LIMIT 99999L
72 #define MXSER_ERR_IOADDR -1
73 #define MXSER_ERR_IRQ -2
74 #define MXSER_ERR_IRQ_CONFLIT -3
75 #define MXSER_ERR_VECTOR -4
77 #define SERIAL_TYPE_NORMAL 1
78 #define SERIAL_TYPE_CALLOUT 2
80 #define WAKEUP_CHARS 256
82 #define UART_MCR_AFE 0x20
83 #define UART_LSR_SPECIAL 0x1E
85 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|\
88 #define C168_ASIC_ID 1
89 #define C104_ASIC_ID 2
90 #define C102_ASIC_ID 0xB
91 #define CI132_ASIC_ID 4
92 #define CI134_ASIC_ID 3
93 #define CI104J_ASIC_ID 5
96 MXSER_BOARD_C168_ISA = 1,
100 MXSER_BOARD_C104_PCI,
101 MXSER_BOARD_C102_ISA,
122 static char *mxser_brdname[] = {
140 "Moxa UC7000 Serial",
149 static int mxser_numports[] = {
176 #define UART_TYPE_NUM 2
178 static const unsigned int Gmoxa_uart_id[UART_TYPE_NUM] = {
179 MOXA_MUST_MU150_HWID,
183 /* This is only for PCI */
184 #define UART_INFO_NUM 3
185 struct mxpciuart_info {
196 static const struct mxpciuart_info Gpci_uart_info[UART_INFO_NUM] = {
197 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
198 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
199 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
203 static struct pci_device_id mxser_pcibrds[] = {
204 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C168),
205 .driver_data = MXSER_BOARD_C168_PCI },
206 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C104),
207 .driver_data = MXSER_BOARD_C104_PCI },
208 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132),
209 .driver_data = MXSER_BOARD_CP132 },
210 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP114),
211 .driver_data = MXSER_BOARD_CP114 },
212 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CT114),
213 .driver_data = MXSER_BOARD_CT114 },
214 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102),
215 .driver_data = MXSER_BOARD_CP102 },
216 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104U),
217 .driver_data = MXSER_BOARD_CP104U },
218 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168U),
219 .driver_data = MXSER_BOARD_CP168U },
220 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132U),
221 .driver_data = MXSER_BOARD_CP132U },
222 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP134U),
223 .driver_data = MXSER_BOARD_CP134U },
224 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104JU),
225 .driver_data = MXSER_BOARD_CP104JU },
226 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_RC7000),
227 .driver_data = MXSER_BOARD_RC7000 },
228 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118U),
229 .driver_data = MXSER_BOARD_CP118U },
230 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102UL),
231 .driver_data = MXSER_BOARD_CP102UL },
232 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102U),
233 .driver_data = MXSER_BOARD_CP102U },
234 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118EL),
235 .driver_data = MXSER_BOARD_CP118EL },
236 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168EL),
237 .driver_data = MXSER_BOARD_CP168EL },
238 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104EL),
239 .driver_data = MXSER_BOARD_CP104EL },
242 MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
244 static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
245 static int ttymajor = MXSERMAJOR;
246 static int calloutmajor = MXSERCUMAJOR;
248 /* Variables for insmod */
250 MODULE_AUTHOR("Casper Yang");
251 MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
252 module_param_array(ioaddr, int, NULL, 0);
253 module_param(ttymajor, int, 0);
254 MODULE_LICENSE("GPL");
258 unsigned long rxcnt[MXSER_PORTS];
259 unsigned long txcnt[MXSER_PORTS];
266 unsigned long up_rxcnt;
267 unsigned long up_txcnt;
269 unsigned char hold_reason;
272 struct mxser_mon_ext {
273 unsigned long rx_cnt[32];
274 unsigned long tx_cnt[32];
275 unsigned long up_rxcnt[32];
276 unsigned long up_txcnt[32];
277 int modem_status[32];
291 struct mxser_board *board;
292 struct tty_struct *tty;
294 unsigned long ioaddr;
295 unsigned long opmode_ioaddr;
299 int rx_trigger; /* Rx fifo trigger level */
301 int baud_base; /* max. speed */
303 int type; /* UART type */
304 int flags; /* defined in tty.h */
305 long session; /* Session of opening process */
306 long pgrp; /* pgrp of opening process */
308 int x_char; /* xon/xoff character */
309 int IER; /* Interrupt Enable Register */
310 int MCR; /* Modem control register */
312 unsigned char stop_rx;
313 unsigned char ldisc_stop_rx;
317 unsigned short closing_wait;
318 unsigned char err_shadow;
321 int count; /* # of fd on device */
322 int blocked_open; /* # of blocked opens */
323 struct async_icount icount; /* kernel counters for 4 input interrupts */
326 int read_status_mask;
327 int ignore_status_mask;
329 unsigned char *xmit_buf;
334 struct termios normal_termios;
335 struct termios callout_termios;
337 struct mxser_mon mon_data;
340 struct work_struct tqueue;
341 wait_queue_head_t open_wait;
342 wait_queue_head_t close_wait;
343 wait_queue_head_t delta_msr_wait;
347 struct pci_dev *pdev; /* temporary (until pci probing) */
352 unsigned long vector;
353 unsigned long vector_mask;
358 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
361 struct mxser_mstatus {
369 static struct mxser_mstatus GMStatus[MXSER_PORTS];
371 static int mxserBoardCAP[MXSER_BOARDS] = {
373 /* 0x180, 0x280, 0x200, 0x320 */
376 static struct mxser_board mxser_boards[MXSER_BOARDS];
377 static struct tty_driver *mxvar_sdriver;
378 static struct tty_struct *mxvar_tty[MXSER_PORTS + 1];
379 static struct termios *mxvar_termios[MXSER_PORTS + 1];
380 static struct termios *mxvar_termios_locked[MXSER_PORTS + 1];
381 static struct mxser_log mxvar_log;
382 static int mxvar_diagflag;
383 static unsigned char mxser_msr[MXSER_PORTS + 1];
384 static struct mxser_mon_ext mon_data_ext;
385 static int mxser_set_baud_method[MXSER_PORTS + 1];
386 static spinlock_t gm_lock;
392 static int mxser_init(void);
394 /* static void mxser_poll(unsigned long); */
395 static int mxser_get_ISA_conf(int, struct mxser_board *);
396 static void mxser_do_softint(void *);
397 static int mxser_open(struct tty_struct *, struct file *);
398 static void mxser_close(struct tty_struct *, struct file *);
399 static int mxser_write(struct tty_struct *, const unsigned char *, int);
400 static int mxser_write_room(struct tty_struct *);
401 static void mxser_flush_buffer(struct tty_struct *);
402 static int mxser_chars_in_buffer(struct tty_struct *);
403 static void mxser_flush_chars(struct tty_struct *);
404 static void mxser_put_char(struct tty_struct *, unsigned char);
405 static int mxser_ioctl(struct tty_struct *, struct file *, uint, ulong);
406 static int mxser_ioctl_special(unsigned int, void __user *);
407 static void mxser_throttle(struct tty_struct *);
408 static void mxser_unthrottle(struct tty_struct *);
409 static void mxser_set_termios(struct tty_struct *, struct termios *);
410 static void mxser_stop(struct tty_struct *);
411 static void mxser_start(struct tty_struct *);
412 static void mxser_hangup(struct tty_struct *);
413 static void mxser_rs_break(struct tty_struct *, int);
414 static irqreturn_t mxser_interrupt(int, void *, struct pt_regs *);
415 static void mxser_receive_chars(struct mxser_port *, int *);
416 static void mxser_transmit_chars(struct mxser_port *);
417 static void mxser_check_modem_status(struct mxser_port *, int);
418 static int mxser_block_til_ready(struct tty_struct *, struct file *,
419 struct mxser_port *);
420 static int mxser_startup(struct mxser_port *);
421 static void mxser_shutdown(struct mxser_port *);
422 static int mxser_change_speed(struct mxser_port *, struct termios *);
423 static int mxser_get_serial_info(struct mxser_port *,
424 struct serial_struct __user *);
425 static int mxser_set_serial_info(struct mxser_port *,
426 struct serial_struct __user *);
427 static int mxser_get_lsr_info(struct mxser_port *, unsigned int __user *);
428 static void mxser_send_break(struct mxser_port *, int);
429 static int mxser_tiocmget(struct tty_struct *, struct file *);
430 static int mxser_tiocmset(struct tty_struct *, struct file *, unsigned int,
432 static int mxser_set_baud(struct mxser_port *, long);
433 static void mxser_wait_until_sent(struct tty_struct *, int);
435 static void mxser_startrx(struct tty_struct *);
436 static void mxser_stoprx(struct tty_struct *);
439 static int CheckIsMoxaMust(int io)
444 outb(0, io + UART_LCR);
445 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
446 oldmcr = inb(io + UART_MCR);
447 outb(0, io + UART_MCR);
448 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
449 if ((hwid = inb(io + UART_MCR)) != 0) {
450 outb(oldmcr, io + UART_MCR);
451 return MOXA_OTHER_UART;
454 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
455 for (i = 0; i < UART_TYPE_NUM; i++) {
456 if (hwid == Gmoxa_uart_id[i])
459 return MOXA_OTHER_UART;
462 /* above is modified by Victor Yu. 08-15-2002 */
464 static const struct tty_operations mxser_ops = {
466 .close = mxser_close,
467 .write = mxser_write,
468 .put_char = mxser_put_char,
469 .flush_chars = mxser_flush_chars,
470 .write_room = mxser_write_room,
471 .chars_in_buffer = mxser_chars_in_buffer,
472 .flush_buffer = mxser_flush_buffer,
473 .ioctl = mxser_ioctl,
474 .throttle = mxser_throttle,
475 .unthrottle = mxser_unthrottle,
476 .set_termios = mxser_set_termios,
478 .start = mxser_start,
479 .hangup = mxser_hangup,
480 .break_ctl = mxser_rs_break,
481 .wait_until_sent = mxser_wait_until_sent,
482 .tiocmget = mxser_tiocmget,
483 .tiocmset = mxser_tiocmset,
487 * The MOXA Smartio/Industio serial driver boot-time initialization code!
490 static int __init mxser_module_init(void)
494 pr_debug("Loading module mxser ...\n");
500 static void __exit mxser_module_exit(void)
504 pr_debug("Unloading module mxser ...\n");
506 err = tty_unregister_driver(mxvar_sdriver);
508 put_tty_driver(mxvar_sdriver);
510 printk(KERN_ERR "Couldn't unregister MOXA Smartio/Industio family serial driver\n");
512 for (i = 0; i < MXSER_BOARDS; i++) {
513 struct pci_dev *pdev;
515 if (mxser_boards[i].board_type == -1)
518 pdev = mxser_boards[i].pdev;
519 free_irq(mxser_boards[i].irq, &mxser_boards[i]);
520 if (pdev != NULL) { /* PCI */
521 pci_release_region(pdev, 2);
522 pci_release_region(pdev, 3);
525 release_region(mxser_boards[i].ports[0].ioaddr, 8 * mxser_boards[i].nports);
526 release_region(mxser_boards[i].vector, 1);
533 static void process_txrx_fifo(struct mxser_port *info)
537 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
538 info->rx_trigger = 1;
539 info->rx_high_water = 1;
540 info->rx_low_water = 1;
541 info->xmit_fifo_size = 1;
543 for (i = 0; i < UART_INFO_NUM; i++)
544 if (info->board->chip_flag == Gpci_uart_info[i].type) {
545 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
546 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
547 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
548 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
553 static int __devinit mxser_initbrd(struct mxser_board *brd)
555 struct mxser_port *info;
559 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
561 for (i = 0; i < brd->nports; i++) {
562 info = &brd->ports[i];
565 info->ldisc_stop_rx = 0;
567 /* Enhance mode enabled here */
568 if (brd->chip_flag != MOXA_OTHER_UART)
569 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
571 info->flags = ASYNC_SHARE_IRQ;
572 info->type = brd->uart_type;
574 process_txrx_fifo(info);
576 info->custom_divisor = info->baud_base * 16;
577 info->close_delay = 5 * HZ / 10;
578 info->closing_wait = 30 * HZ;
579 INIT_WORK(&info->tqueue, mxser_do_softint, info);
580 info->normal_termios = mxvar_sdriver->init_termios;
581 init_waitqueue_head(&info->open_wait);
582 init_waitqueue_head(&info->close_wait);
583 init_waitqueue_head(&info->delta_msr_wait);
584 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
585 info->err_shadow = 0;
586 spin_lock_init(&info->slock);
588 /* before set INT ISR, disable all int */
589 outb(inb(info->ioaddr + UART_IER) & 0xf0,
590 info->ioaddr + UART_IER);
593 * Allocate the IRQ if necessary
596 retval = request_irq(brd->irq, mxser_interrupt,
597 (brd->ports[0].flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED :
598 IRQF_DISABLED, "mxser", brd);
600 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
601 "conflict with another device.\n",
602 mxser_brdname[brd->board_type - 1], brd->irq);
608 static int __init mxser_get_PCI_conf(int board_type, struct mxser_board *brd,
609 struct pci_dev *pdev)
612 unsigned long ioaddress;
616 brd->board_type = board_type;
617 brd->nports = mxser_numports[board_type - 1];
618 ioaddress = pci_resource_start(pdev, 2);
619 retval = pci_request_region(pdev, 2, "mxser(IO)");
623 for (i = 0; i < brd->nports; i++)
624 brd->ports[i].ioaddr = ioaddress + 8 * i;
627 ioaddress = pci_resource_start(pdev, 3);
628 retval = pci_request_region(pdev, 3, "mxser(vector)");
631 brd->vector = ioaddress;
634 brd->irq = pdev->irq;
636 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
637 brd->uart_type = PORT_16550A;
638 brd->vector_mask = 0;
640 for (i = 0; i < brd->nports; i++) {
641 for (j = 0; j < UART_INFO_NUM; j++) {
642 if (Gpci_uart_info[j].type == brd->chip_flag) {
643 brd->ports[i].max_baud =
644 Gpci_uart_info[j].max_baud;
646 /* exception....CP-102 */
647 if (board_type == MXSER_BOARD_CP102)
648 brd->ports[i].max_baud = 921600;
654 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
655 for (i = 0; i < brd->nports; i++) {
657 brd->ports[i].opmode_ioaddr = ioaddress + 4;
659 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
661 outb(0, ioaddress + 4); /* default set to RS232 mode */
662 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
665 for (i = 0; i < brd->nports; i++) {
666 brd->vector_mask |= (1 << i);
667 brd->ports[i].baud_base = 921600;
671 pci_release_region(pdev, 2);
676 static int __init mxser_init(void)
678 struct pci_dev *pdev = NULL;
679 struct mxser_board *brd;
683 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
686 spin_lock_init(&gm_lock);
688 for (i = 0; i < MXSER_BOARDS; i++)
689 mxser_boards[i].board_type = -1;
691 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
694 /* Initialize the tty_driver structure */
695 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
696 mxvar_sdriver->name = "ttyM";
697 mxvar_sdriver->major = ttymajor;
698 mxvar_sdriver->minor_start = 0;
699 mxvar_sdriver->num = MXSER_PORTS + 1;
700 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
701 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
702 mxvar_sdriver->init_termios = tty_std_termios;
703 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
704 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
705 tty_set_operations(mxvar_sdriver, &mxser_ops);
706 mxvar_sdriver->ttys = mxvar_tty;
707 mxvar_sdriver->termios = mxvar_termios;
708 mxvar_sdriver->termios_locked = mxvar_termios_locked;
713 /* Start finding ISA boards here */
714 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
717 if (!(cap = mxserBoardCAP[b]))
720 brd = &mxser_boards[m];
721 retval = mxser_get_ISA_conf(cap, brd);
724 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
725 mxser_brdname[brd->board_type - 1], ioaddr[b]);
728 if (retval == MXSER_ERR_IRQ)
729 printk(KERN_ERR "Invalid interrupt number, "
730 "board not configured\n");
731 else if (retval == MXSER_ERR_IRQ_CONFLIT)
732 printk(KERN_ERR "Invalid interrupt number, "
733 "board not configured\n");
734 else if (retval == MXSER_ERR_VECTOR)
735 printk(KERN_ERR "Invalid interrupt vector, "
736 "board not configured\n");
737 else if (retval == MXSER_ERR_IOADDR)
738 printk(KERN_ERR "Invalid I/O address, "
739 "board not configured\n");
746 /* mxser_initbrd will hook ISR. */
747 if (mxser_initbrd(brd) < 0)
753 /* Start finding ISA boards from module arg */
754 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
757 if (!(cap = ioaddr[b]))
760 brd = &mxser_boards[m];
761 retval = mxser_get_ISA_conf(cap, &mxser_boards[m]);
764 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
765 mxser_brdname[brd->board_type - 1], ioaddr[b]);
768 if (retval == MXSER_ERR_IRQ)
769 printk(KERN_ERR "Invalid interrupt number, "
770 "board not configured\n");
771 else if (retval == MXSER_ERR_IRQ_CONFLIT)
772 printk(KERN_ERR "Invalid interrupt number, "
773 "board not configured\n");
774 else if (retval == MXSER_ERR_VECTOR)
775 printk(KERN_ERR "Invalid interrupt vector, "
776 "board not configured\n");
777 else if (retval == MXSER_ERR_IOADDR)
778 printk(KERN_ERR "Invalid I/O address, "
779 "board not configured\n");
785 /* mxser_initbrd will hook ISR. */
786 if (mxser_initbrd(brd) < 0)
792 /* start finding PCI board here */
793 n = ARRAY_SIZE(mxser_pcibrds) - 1;
796 pdev = pci_get_device(mxser_pcibrds[b].vendor,
797 mxser_pcibrds[b].device, pdev);
802 printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
803 mxser_brdname[(int) (mxser_pcibrds[b].driver_data) - 1],
804 pdev->bus->number, PCI_SLOT(pdev->devfn));
805 if (m >= MXSER_BOARDS)
807 "Too many Smartio/Industio family boards find "
808 "(maximum %d), board not configured\n",
811 if (pci_enable_device(pdev)) {
812 printk(KERN_ERR "Moxa SmartI/O PCI enable "
816 brd = &mxser_boards[m];
818 retval = mxser_get_PCI_conf(
819 (int)mxser_pcibrds[b].driver_data,
822 if (retval == MXSER_ERR_IRQ)
824 "Invalid interrupt number, "
825 "board not configured\n");
826 else if (retval == MXSER_ERR_IRQ_CONFLIT)
828 "Invalid interrupt number, "
829 "board not configured\n");
830 else if (retval == MXSER_ERR_VECTOR)
832 "Invalid interrupt vector, "
833 "board not configured\n");
834 else if (retval == MXSER_ERR_IOADDR)
836 "Invalid I/O address, "
837 "board not configured\n");
840 /* mxser_initbrd will hook ISR. */
841 if (mxser_initbrd(brd) < 0)
844 /* Keep an extra reference if we succeeded. It will
845 be returned at unload time */
850 retval = tty_register_driver(mxvar_sdriver);
852 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family"
854 put_tty_driver(mxvar_sdriver);
856 for (i = 0; i < MXSER_BOARDS; i++) {
857 if (mxser_boards[i].board_type == -1)
860 free_irq(mxser_boards[i].irq, &mxser_boards[i]);
861 /* todo: release io, vector */
870 static void mxser_do_softint(void *private_)
872 struct mxser_port *info = private_;
873 struct tty_struct *tty;
877 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
879 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event))
883 static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
885 unsigned char status = 0;
887 status = inb(baseaddr + UART_MSR);
889 mxser_msr[port] &= 0x0F;
890 mxser_msr[port] |= status;
891 status = mxser_msr[port];
899 * This routine is called whenever a serial port is opened. It
900 * enables interrupts for a serial port, linking in its async structure into
901 * the IRQ chain. It also performs the serial-specific
902 * initialization for the tty structure.
904 static int mxser_open(struct tty_struct *tty, struct file *filp)
906 struct mxser_port *info;
909 /* initialize driver_data in case something fails */
910 tty->driver_data = NULL;
913 if (line == MXSER_PORTS)
915 if (line < 0 || line > MXSER_PORTS)
917 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
921 tty->driver_data = info;
924 * Start up serial port
927 retval = mxser_startup(info);
931 retval = mxser_block_til_ready(tty, filp, info);
935 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
936 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
937 *tty->termios = info->normal_termios;
939 *tty->termios = info->callout_termios;
940 mxser_change_speed(info, NULL);
943 info->session = process_session(current);
944 info->pgrp = process_group(current);
947 status = mxser_get_msr(info->base, 0, info->port);
948 mxser_check_modem_status(info, status);
951 /* unmark here for very high baud rate (ex. 921600 bps) used */
952 tty->low_latency = 1;
957 * This routine is called when the serial port gets closed. First, we
958 * wait for the last remaining data to be sent. Then, we unlink its
959 * async structure from the interrupt chain if necessary, and we free
960 * that IRQ if nothing is left in the chain.
962 static void mxser_close(struct tty_struct *tty, struct file *filp)
964 struct mxser_port *info = tty->driver_data;
966 unsigned long timeout;
968 struct tty_ldisc *ld;
970 if (tty->index == MXSER_PORTS)
975 spin_lock_irqsave(&info->slock, flags);
977 if (tty_hung_up_p(filp)) {
978 spin_unlock_irqrestore(&info->slock, flags);
981 if ((tty->count == 1) && (info->count != 1)) {
983 * Uh, oh. tty->count is 1, which means that the tty
984 * structure will be freed. Info->count should always
985 * be one in these conditions. If it's greater than
986 * one, we've got real problems, since it means the
987 * serial port won't be shutdown.
989 printk(KERN_ERR "mxser_close: bad serial port count; "
990 "tty->count is 1, info->count is %d\n", info->count);
993 if (--info->count < 0) {
994 printk(KERN_ERR "mxser_close: bad serial port count for "
995 "ttys%d: %d\n", tty->index, info->count);
999 spin_unlock_irqrestore(&info->slock, flags);
1002 info->flags |= ASYNC_CLOSING;
1003 spin_unlock_irqrestore(&info->slock, flags);
1005 * Save the termios structure, since this port may have
1006 * separate termios for callout and dialin.
1008 if (info->flags & ASYNC_NORMAL_ACTIVE)
1009 info->normal_termios = *tty->termios;
1011 * Now we wait for the transmit buffer to clear; and we notify
1012 * the line discipline to only process XON/XOFF characters.
1015 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1016 tty_wait_until_sent(tty, info->closing_wait);
1018 * At this point we stop accepting input. To do this, we
1019 * disable the receive line status interrupts, and tell the
1020 * interrupt driver to stop checking the data ready bit in the
1021 * line status register.
1023 info->IER &= ~UART_IER_RLSI;
1024 if (info->board->chip_flag)
1025 info->IER &= ~MOXA_MUST_RECV_ISR;
1027 info->read_status_mask &= ~UART_LSR_DR;
1029 if (info->flags & ASYNC_INITIALIZED) {
1030 outb(info->IER, info->ioaddr + UART_IER);
1032 * Before we drop DTR, make sure the UART transmitter
1033 * has completely drained; this is especially
1034 * important if there is a transmit FIFO!
1036 timeout = jiffies + HZ;
1037 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
1038 schedule_timeout_interruptible(5);
1039 if (time_after(jiffies, timeout))
1043 mxser_shutdown(info);
1045 if (tty->driver->flush_buffer)
1046 tty->driver->flush_buffer(tty);
1048 ld = tty_ldisc_ref(tty);
1050 if (ld->flush_buffer)
1051 ld->flush_buffer(tty);
1052 tty_ldisc_deref(ld);
1058 if (info->blocked_open) {
1059 if (info->close_delay)
1060 schedule_timeout_interruptible(info->close_delay);
1061 wake_up_interruptible(&info->open_wait);
1064 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1065 wake_up_interruptible(&info->close_wait);
1069 static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1072 struct mxser_port *info = tty->driver_data;
1073 unsigned long flags;
1075 if (!info->xmit_buf)
1079 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1080 SERIAL_XMIT_SIZE - info->xmit_head));
1084 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1085 spin_lock_irqsave(&info->slock, flags);
1086 info->xmit_head = (info->xmit_head + c) &
1087 (SERIAL_XMIT_SIZE - 1);
1088 info->xmit_cnt += c;
1089 spin_unlock_irqrestore(&info->slock, flags);
1096 if (info->xmit_cnt && !tty->stopped
1097 /*&& !(info->IER & UART_IER_THRI)*/) {
1098 if (!tty->hw_stopped ||
1099 (info->type == PORT_16550A) ||
1100 (info->board->chip_flag)) {
1101 spin_lock_irqsave(&info->slock, flags);
1102 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1104 info->IER |= UART_IER_THRI;
1105 outb(info->IER, info->ioaddr + UART_IER);
1106 spin_unlock_irqrestore(&info->slock, flags);
1112 static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1114 struct mxser_port *info = tty->driver_data;
1115 unsigned long flags;
1117 if (!info->xmit_buf)
1120 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1123 spin_lock_irqsave(&info->slock, flags);
1124 info->xmit_buf[info->xmit_head++] = ch;
1125 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1127 spin_unlock_irqrestore(&info->slock, flags);
1128 if (!tty->stopped /*&& !(info->IER & UART_IER_THRI)*/) {
1129 if (!tty->hw_stopped ||
1130 (info->type == PORT_16550A) ||
1131 info->board->chip_flag) {
1132 spin_lock_irqsave(&info->slock, flags);
1133 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1134 info->IER |= UART_IER_THRI;
1135 outb(info->IER, info->ioaddr + UART_IER);
1136 spin_unlock_irqrestore(&info->slock, flags);
1142 static void mxser_flush_chars(struct tty_struct *tty)
1144 struct mxser_port *info = tty->driver_data;
1145 unsigned long flags;
1147 if (info->xmit_cnt <= 0 ||
1151 (info->type != PORT_16550A) &&
1152 (!info->board->chip_flag)
1156 spin_lock_irqsave(&info->slock, flags);
1158 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1159 info->IER |= UART_IER_THRI;
1160 outb(info->IER, info->ioaddr + UART_IER);
1162 spin_unlock_irqrestore(&info->slock, flags);
1165 static int mxser_write_room(struct tty_struct *tty)
1167 struct mxser_port *info = tty->driver_data;
1170 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1176 static int mxser_chars_in_buffer(struct tty_struct *tty)
1178 struct mxser_port *info = tty->driver_data;
1179 int len = info->xmit_cnt;
1181 if (!(inb(info->ioaddr + UART_LSR) & UART_LSR_THRE))
1187 static void mxser_flush_buffer(struct tty_struct *tty)
1189 struct mxser_port *info = tty->driver_data;
1191 unsigned long flags;
1194 spin_lock_irqsave(&info->slock, flags);
1195 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1197 /* below added by shinhay */
1198 fcr = inb(info->ioaddr + UART_FCR);
1199 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1200 info->ioaddr + UART_FCR);
1201 outb(fcr, info->ioaddr + UART_FCR);
1203 spin_unlock_irqrestore(&info->slock, flags);
1204 /* above added by shinhay */
1206 wake_up_interruptible(&tty->write_wait);
1207 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
1208 (tty->ldisc.write_wakeup) (tty);
1211 static int mxser_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1213 struct mxser_port *info = tty->driver_data;
1215 struct async_icount cprev, cnow; /* kernel counter temps */
1216 struct serial_icounter_struct __user *p_cuser;
1217 unsigned long templ;
1218 unsigned long flags;
1219 void __user *argp = (void __user *)arg;
1221 if (tty->index == MXSER_PORTS)
1222 return mxser_ioctl_special(cmd, argp);
1224 /* following add by Victor Yu. 01-05-2004 */
1225 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1227 unsigned long opmode;
1228 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1230 unsigned char val, mask;
1233 if (cmd == MOXA_SET_OP_MODE) {
1234 if (get_user(opmode, (int __user *) argp))
1236 if (opmode != RS232_MODE &&
1237 opmode != RS485_2WIRE_MODE &&
1238 opmode != RS422_MODE &&
1239 opmode != RS485_4WIRE_MODE)
1243 val = inb(info->opmode_ioaddr);
1245 val |= (opmode << shiftbit);
1246 outb(val, info->opmode_ioaddr);
1249 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1250 opmode &= OP_MODE_MASK;
1251 if (copy_to_user(argp, &opmode, sizeof(int)))
1256 /* above add by Victor Yu. 01-05-2004 */
1258 if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1259 if (tty->flags & (1 << TTY_IO_ERROR))
1263 case TCSBRK: /* SVID version: non-zero arg --> no break */
1264 retval = tty_check_change(tty);
1267 tty_wait_until_sent(tty, 0);
1269 mxser_send_break(info, HZ / 4); /* 1/4 second */
1271 case TCSBRKP: /* support for POSIX tcsendbreak() */
1272 retval = tty_check_change(tty);
1275 tty_wait_until_sent(tty, 0);
1276 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1279 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1281 if (get_user(templ, (unsigned long __user *) argp))
1284 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1287 return mxser_get_serial_info(info, argp);
1289 return mxser_set_serial_info(info, argp);
1290 case TIOCSERGETLSR: /* Get line status register */
1291 return mxser_get_lsr_info(info, argp);
1293 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1294 * - mask passed in arg for lines of interest
1295 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1296 * Caller should use TIOCGICOUNT to see which one it was
1299 DECLARE_WAITQUEUE(wait, current);
1301 spin_lock_irqsave(&info->slock, flags);
1302 cprev = info->icount; /* note the counters on entry */
1303 spin_unlock_irqrestore(&info->slock, flags);
1305 add_wait_queue(&info->delta_msr_wait, &wait);
1307 spin_lock_irqsave(&info->slock, flags);
1308 cnow = info->icount; /* atomic copy */
1309 spin_unlock_irqrestore(&info->slock, flags);
1311 set_current_state(TASK_INTERRUPTIBLE);
1312 if (((arg & TIOCM_RNG) &&
1313 (cnow.rng != cprev.rng)) ||
1314 ((arg & TIOCM_DSR) &&
1315 (cnow.dsr != cprev.dsr)) ||
1316 ((arg & TIOCM_CD) &&
1317 (cnow.dcd != cprev.dcd)) ||
1318 ((arg & TIOCM_CTS) &&
1319 (cnow.cts != cprev.cts))) {
1323 /* see if a signal did it */
1324 if (signal_pending(current)) {
1330 current->state = TASK_RUNNING;
1331 remove_wait_queue(&info->delta_msr_wait, &wait);
1336 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1337 * Return: write counters to the user passed counter struct
1338 * NB: both 1->0 and 0->1 transitions are counted except for
1339 * RI where only 0->1 is counted.
1342 spin_lock_irqsave(&info->slock, flags);
1343 cnow = info->icount;
1344 spin_unlock_irqrestore(&info->slock, flags);
1346 /* modified by casper 1/11/2000 */
1347 if (put_user(cnow.frame, &p_cuser->frame))
1349 if (put_user(cnow.brk, &p_cuser->brk))
1351 if (put_user(cnow.overrun, &p_cuser->overrun))
1353 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1355 if (put_user(cnow.parity, &p_cuser->parity))
1357 if (put_user(cnow.rx, &p_cuser->rx))
1359 if (put_user(cnow.tx, &p_cuser->tx))
1361 put_user(cnow.cts, &p_cuser->cts);
1362 put_user(cnow.dsr, &p_cuser->dsr);
1363 put_user(cnow.rng, &p_cuser->rng);
1364 put_user(cnow.dcd, &p_cuser->dcd);
1366 case MOXA_HighSpeedOn:
1367 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1368 case MOXA_SDS_RSTICOUNTER: {
1369 info->mon_data.rxcnt = 0;
1370 info->mon_data.txcnt = 0;
1373 /* (above) added by James. */
1374 case MOXA_ASPP_SETBAUD:{
1376 if (get_user(baud, (long __user *)argp))
1378 mxser_set_baud(info, baud);
1381 case MOXA_ASPP_GETBAUD:
1382 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1387 case MOXA_ASPP_OQUEUE:{
1390 len = mxser_chars_in_buffer(tty);
1392 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1394 len += (lsr ? 0 : 1);
1396 if (copy_to_user(argp, &len, sizeof(int)))
1401 case MOXA_ASPP_MON: {
1404 /* info->mon_data.ser_param = tty->termios->c_cflag; */
1406 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1407 mxser_check_modem_status(info, status);
1409 mcr = inb(info->ioaddr + UART_MCR);
1410 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1411 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1413 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1415 if (mcr & MOXA_MUST_MCR_TX_XON)
1416 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1418 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1420 if (info->tty->hw_stopped)
1421 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1423 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1425 if (copy_to_user(argp, &info->mon_data,
1426 sizeof(struct mxser_mon)))
1432 case MOXA_ASPP_LSTATUS: {
1433 if (copy_to_user(argp, &info->err_shadow,
1434 sizeof(unsigned char)))
1437 info->err_shadow = 0;
1440 case MOXA_SET_BAUD_METHOD: {
1443 if (get_user(method, (int __user *)argp))
1445 mxser_set_baud_method[tty->index] = method;
1446 if (copy_to_user(argp, &method, sizeof(int)))
1452 return -ENOIOCTLCMD;
1458 #define CMSPAR 010000000000
1461 static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1463 struct mxser_port *port;
1469 /* if (copy_to_user(argp, mxsercfg,
1470 sizeof(struct mxser_hwconf) * 4))
1474 case MOXA_GET_MAJOR:
1475 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1479 case MOXA_GET_CUMAJOR:
1480 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1484 case MOXA_CHKPORTENABLE:
1487 for (i = 0; i < MXSER_BOARDS; i++)
1488 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1489 if (mxser_boards[i].ports[j].ioaddr)
1492 return put_user(result, (unsigned long __user *)argp);
1493 case MOXA_GETDATACOUNT:
1494 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1497 case MOXA_GETMSTATUS:
1498 for (i = 0; i < MXSER_BOARDS; i++)
1499 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1500 port = &mxser_boards[i].ports[j];
1503 if (!port->ioaddr) {
1504 GMStatus[i].dcd = 0;
1505 GMStatus[i].dsr = 0;
1506 GMStatus[i].cts = 0;
1510 if (!port->tty || !port->tty->termios)
1512 port->normal_termios.c_cflag;
1515 port->tty->termios->c_cflag;
1517 status = inb(port->ioaddr + UART_MSR);
1518 if (status & 0x80 /*UART_MSR_DCD */ )
1519 GMStatus[i].dcd = 1;
1521 GMStatus[i].dcd = 0;
1523 if (status & 0x20 /*UART_MSR_DSR */ )
1524 GMStatus[i].dsr = 1;
1526 GMStatus[i].dsr = 0;
1529 if (status & 0x10 /*UART_MSR_CTS */ )
1530 GMStatus[i].cts = 1;
1532 GMStatus[i].cts = 0;
1534 if (copy_to_user(argp, GMStatus,
1535 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1538 case MOXA_ASPP_MON_EXT: {
1539 int status, p, shiftbit;
1540 unsigned long opmode;
1541 unsigned cflag, iflag;
1543 for (i = 0; i < MXSER_BOARDS; i++)
1544 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1545 port = &mxser_boards[i].ports[j];
1549 status = mxser_get_msr(port->ioaddr, 0, i);
1550 /* mxser_check_modem_status(port, status); */
1552 if (status & UART_MSR_TERI)
1554 if (status & UART_MSR_DDSR)
1556 if (status & UART_MSR_DDCD)
1558 if (status & UART_MSR_DCTS)
1561 port->mon_data.modem_status = status;
1562 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1563 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1564 mon_data_ext.up_rxcnt[i] =
1565 port->mon_data.up_rxcnt;
1566 mon_data_ext.up_txcnt[i] =
1567 port->mon_data.up_txcnt;
1568 mon_data_ext.modem_status[i] =
1569 port->mon_data.modem_status;
1570 mon_data_ext.baudrate[i] = port->realbaud;
1572 if (!port->tty || !port->tty->termios) {
1573 cflag = port->normal_termios.c_cflag;
1574 iflag = port->normal_termios.c_iflag;
1576 cflag = port->tty->termios->c_cflag;
1577 iflag = port->tty->termios->c_iflag;
1580 mon_data_ext.databits[i] = cflag & CSIZE;
1582 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1584 mon_data_ext.parity[i] =
1585 cflag & (PARENB | PARODD | CMSPAR);
1587 mon_data_ext.flowctrl[i] = 0x00;
1589 if (cflag & CRTSCTS)
1590 mon_data_ext.flowctrl[i] |= 0x03;
1592 if (iflag & (IXON | IXOFF))
1593 mon_data_ext.flowctrl[i] |= 0x0C;
1595 if (port->type == PORT_16550A)
1596 mon_data_ext.fifo[i] = 1;
1598 mon_data_ext.fifo[i] = 0;
1602 opmode = inb(port->opmode_ioaddr) >> shiftbit;
1603 opmode &= OP_MODE_MASK;
1605 mon_data_ext.iftype[i] = opmode;
1608 if (copy_to_user(argp, &mon_data_ext,
1609 sizeof(mon_data_ext)))
1615 return -ENOIOCTLCMD;
1620 static void mxser_stoprx(struct tty_struct *tty)
1622 struct mxser_port *info = tty->driver_data;
1624 info->ldisc_stop_rx = 1;
1626 /* following add by Victor Yu. 09-02-2002 */
1627 if (info->board->chip_flag) {
1628 info->IER &= ~MOXA_MUST_RECV_ISR;
1629 outb(info->IER, info->ioaddr + UART_IER);
1630 } else if (!(info->flags & ASYNC_CLOSING)) {
1631 info->x_char = STOP_CHAR(tty);
1632 outb(0, info->ioaddr + UART_IER);
1633 info->IER |= UART_IER_THRI;
1634 outb(info->IER, info->ioaddr + UART_IER);
1638 if (info->tty->termios->c_cflag & CRTSCTS) {
1639 info->MCR &= ~UART_MCR_RTS;
1640 outb(info->MCR, info->ioaddr + UART_MCR);
1644 static void mxser_startrx(struct tty_struct *tty)
1646 struct mxser_port *info = tty->driver_data;
1648 info->ldisc_stop_rx = 0;
1653 /* following add by Victor Yu. 09-02-2002 */
1654 if (info->board->chip_flag) {
1655 info->IER |= MOXA_MUST_RECV_ISR;
1656 outb(info->IER, info->ioaddr + UART_IER);
1657 } else if (!(info->flags & ASYNC_CLOSING)) {
1658 info->x_char = START_CHAR(tty);
1659 outb(0, info->ioaddr + UART_IER);
1660 info->IER |= UART_IER_THRI;
1661 outb(info->IER, info->ioaddr + UART_IER);
1666 if (info->tty->termios->c_cflag & CRTSCTS) {
1667 info->MCR |= UART_MCR_RTS;
1668 outb(info->MCR, info->ioaddr + UART_MCR);
1673 * This routine is called by the upper-layer tty layer to signal that
1674 * incoming characters should be throttled.
1676 static void mxser_throttle(struct tty_struct *tty)
1681 static void mxser_unthrottle(struct tty_struct *tty)
1686 static void mxser_set_termios(struct tty_struct *tty, struct termios *old_termios)
1688 struct mxser_port *info = tty->driver_data;
1689 unsigned long flags;
1691 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1692 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
1694 mxser_change_speed(info, old_termios);
1696 if ((old_termios->c_cflag & CRTSCTS) &&
1697 !(tty->termios->c_cflag & CRTSCTS)) {
1698 tty->hw_stopped = 0;
1703 /* Handle sw stopped */
1704 if ((old_termios->c_iflag & IXON) &&
1705 !(tty->termios->c_iflag & IXON)) {
1708 /* following add by Victor Yu. 09-02-2002 */
1709 if (info->board->chip_flag) {
1710 spin_lock_irqsave(&info->slock, flags);
1711 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
1712 spin_unlock_irqrestore(&info->slock, flags);
1714 /* above add by Victor Yu. 09-02-2002 */
1721 * mxser_stop() and mxser_start()
1723 * This routines are called before setting or resetting tty->stopped.
1724 * They enable or disable transmitter interrupts, as necessary.
1726 static void mxser_stop(struct tty_struct *tty)
1728 struct mxser_port *info = tty->driver_data;
1729 unsigned long flags;
1731 spin_lock_irqsave(&info->slock, flags);
1732 if (info->IER & UART_IER_THRI) {
1733 info->IER &= ~UART_IER_THRI;
1734 outb(info->IER, info->ioaddr + UART_IER);
1736 spin_unlock_irqrestore(&info->slock, flags);
1739 static void mxser_start(struct tty_struct *tty)
1741 struct mxser_port *info = tty->driver_data;
1742 unsigned long flags;
1744 spin_lock_irqsave(&info->slock, flags);
1745 if (info->xmit_cnt && info->xmit_buf
1746 /* && !(info->IER & UART_IER_THRI) */) {
1747 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1748 info->IER |= UART_IER_THRI;
1749 outb(info->IER, info->ioaddr + UART_IER);
1751 spin_unlock_irqrestore(&info->slock, flags);
1755 * mxser_wait_until_sent() --- wait until the transmitter is empty
1757 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1759 struct mxser_port *info = tty->driver_data;
1760 unsigned long orig_jiffies, char_time;
1763 if (info->type == PORT_UNKNOWN)
1766 if (info->xmit_fifo_size == 0)
1767 return; /* Just in case.... */
1769 orig_jiffies = jiffies;
1771 * Set the check interval to be 1/5 of the estimated time to
1772 * send a single character, and make it at least 1. The check
1773 * interval should also be less than the timeout.
1775 * Note: we have to use pretty tight timings here to satisfy
1778 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1779 char_time = char_time / 5;
1782 if (timeout && timeout < char_time)
1783 char_time = timeout;
1785 * If the transmitter hasn't cleared in twice the approximate
1786 * amount of time to send the entire FIFO, it probably won't
1787 * ever clear. This assumes the UART isn't doing flow
1788 * control, which is currently the case. Hence, if it ever
1789 * takes longer than info->timeout, this is probably due to a
1790 * UART bug of some kind. So, we clamp the timeout parameter at
1793 if (!timeout || timeout > 2 * info->timeout)
1794 timeout = 2 * info->timeout;
1795 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1796 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
1797 timeout, char_time);
1798 printk("jiff=%lu...", jiffies);
1800 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
1801 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1802 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1804 schedule_timeout_interruptible(char_time);
1805 if (signal_pending(current))
1807 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1810 set_current_state(TASK_RUNNING);
1812 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1813 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1819 * This routine is called by tty_hangup() when a hangup is signaled.
1821 void mxser_hangup(struct tty_struct *tty)
1823 struct mxser_port *info = tty->driver_data;
1825 mxser_flush_buffer(tty);
1826 mxser_shutdown(info);
1829 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1831 wake_up_interruptible(&info->open_wait);
1835 /* added by James 03-12-2004. */
1837 * mxser_rs_break() --- routine which turns the break handling on or off
1839 static void mxser_rs_break(struct tty_struct *tty, int break_state)
1841 struct mxser_port *info = tty->driver_data;
1842 unsigned long flags;
1844 spin_lock_irqsave(&info->slock, flags);
1845 if (break_state == -1)
1846 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1847 info->ioaddr + UART_LCR);
1849 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1850 info->ioaddr + UART_LCR);
1851 spin_unlock_irqrestore(&info->slock, flags);
1854 /* (above) added by James. */
1858 * This is the serial driver's generic interrupt routine
1860 static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1863 struct mxser_board *brd = NULL;
1864 struct mxser_port *port;
1865 int max, irqbits, bits, msr;
1866 int pass_counter = 0;
1867 unsigned int int_cnt;
1868 int handled = IRQ_NONE;
1870 /* spin_lock(&gm_lock); */
1872 for (i = 0; i < MXSER_BOARDS; i++)
1873 if (dev_id == &mxser_boards[i]) {
1878 if (i == MXSER_BOARDS)
1882 max = mxser_numports[brd->board_type - 1];
1884 irqbits = inb(brd->vector) & brd->vector_mask;
1885 if (irqbits == brd->vector_mask)
1888 handled = IRQ_HANDLED;
1889 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
1890 if (irqbits == brd->vector_mask)
1894 port = &brd->ports[i];
1898 /* following add by Victor Yu. 09-13-2002 */
1899 iir = inb(port->ioaddr + UART_IIR);
1900 if (iir & UART_IIR_NO_INT)
1902 iir &= MOXA_MUST_IIR_MASK;
1904 status = inb(port->ioaddr + UART_LSR);
1905 outb(0x27, port->ioaddr + UART_FCR);
1906 inb(port->ioaddr + UART_MSR);
1909 /* above add by Victor Yu. 09-13-2002 */
1911 /* following add by Victor Yu. 09-02-2002 */
1912 status = inb(port->ioaddr + UART_LSR);
1914 if (status & UART_LSR_PE)
1915 port->err_shadow |= NPPI_NOTIFY_PARITY;
1916 if (status & UART_LSR_FE)
1917 port->err_shadow |= NPPI_NOTIFY_FRAMING;
1918 if (status & UART_LSR_OE)
1920 NPPI_NOTIFY_HW_OVERRUN;
1921 if (status & UART_LSR_BI)
1922 port->err_shadow |= NPPI_NOTIFY_BREAK;
1924 if (port->board->chip_flag) {
1926 if ( (status & 0x02) && !(status & 0x01) ) {
1927 outb(port->ioaddr+UART_FCR, 0x23);
1931 if (iir == MOXA_MUST_IIR_GDA ||
1932 iir == MOXA_MUST_IIR_RDA ||
1933 iir == MOXA_MUST_IIR_RTO ||
1934 iir == MOXA_MUST_IIR_LSR)
1935 mxser_receive_chars(port,
1939 /* above add by Victor Yu. 09-02-2002 */
1941 status &= port->read_status_mask;
1942 if (status & UART_LSR_DR)
1943 mxser_receive_chars(port,
1946 msr = inb(port->ioaddr + UART_MSR);
1947 if (msr & UART_MSR_ANY_DELTA)
1948 mxser_check_modem_status(port, msr);
1950 /* following add by Victor Yu. 09-13-2002 */
1951 if (port->board->chip_flag) {
1952 if (iir == 0x02 && (status &
1954 mxser_transmit_chars(port);
1956 /* above add by Victor Yu. 09-13-2002 */
1958 if (status & UART_LSR_THRE)
1959 mxser_transmit_chars(port);
1961 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
1963 if (pass_counter++ > MXSER_ISR_PASS_LIMIT)
1964 break; /* Prevent infinite loops */
1968 /* spin_unlock(&gm_lock); */
1972 static void mxser_receive_chars(struct mxser_port *port, int *status)
1974 struct tty_struct *tty = port->tty;
1975 unsigned char ch, gdl;
1980 unsigned long flags;
1982 spin_lock_irqsave(&port->slock, flags);
1984 recv_room = tty->receive_room;
1985 if ((recv_room == 0) && (!port->ldisc_stop_rx)) {
1986 /* mxser_throttle(tty); */
1991 /* following add by Victor Yu. 09-02-2002 */
1992 if (port->board->chip_flag != MOXA_OTHER_UART) {
1994 if (*status & UART_LSR_SPECIAL)
1996 /* following add by Victor Yu. 02-11-2004 */
1997 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
1998 (*status & MOXA_MUST_LSR_RERR))
2000 /* above add by Victor Yu. 02-14-2004 */
2001 if (*status & MOXA_MUST_LSR_RERR)
2004 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
2006 /* add by Victor Yu. 02-11-2004 */
2007 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
2008 gdl &= MOXA_MUST_GDL_MASK;
2009 if (gdl >= recv_room) {
2010 if (!port->ldisc_stop_rx) {
2011 /* mxser_throttle(tty); */
2017 ch = inb(port->ioaddr + UART_RX);
2018 tty_insert_flip_char(tty, ch, 0);
2024 /* above add by Victor Yu. 09-02-2002 */
2030 ch = inb(port->ioaddr + UART_RX);
2031 /* following add by Victor Yu. 09-02-2002 */
2032 if (port->board->chip_flag && (*status & UART_LSR_OE)
2033 /*&& !(*status&UART_LSR_DR) */)
2034 outb(0x23, port->ioaddr + UART_FCR);
2035 *status &= port->read_status_mask;
2036 /* above add by Victor Yu. 09-02-2002 */
2037 if (*status & port->ignore_status_mask) {
2038 if (++ignored > 100)
2042 if (*status & UART_LSR_SPECIAL) {
2043 if (*status & UART_LSR_BI) {
2045 /* added by casper 1/11/2000 */
2048 if (port->flags & ASYNC_SAK)
2050 } else if (*status & UART_LSR_PE) {
2052 /* added by casper 1/11/2000 */
2053 port->icount.parity++;
2054 } else if (*status & UART_LSR_FE) {
2056 /* added by casper 1/11/2000 */
2057 port->icount.frame++;
2058 } else if (*status & UART_LSR_OE) {
2060 /* added by casper 1/11/2000 */
2061 port->icount.overrun++;
2066 tty_insert_flip_char(tty, ch, flag);
2068 if (cnt >= recv_room) {
2069 if (!port->ldisc_stop_rx) {
2070 /* mxser_throttle(tty); */
2078 /* following add by Victor Yu. 09-02-2002 */
2079 if (port->board->chip_flag)
2082 /* mask by Victor Yu. 09-02-2002
2083 *status = inb(port->ioaddr + UART_LSR) & port->read_status_mask;
2085 /* following add by Victor Yu. 09-02-2002 */
2086 *status = inb(port->ioaddr + UART_LSR);
2087 /* above add by Victor Yu. 09-02-2002 */
2088 } while (*status & UART_LSR_DR);
2090 end_intr: /* add by Victor Yu. 09-02-2002 */
2091 mxvar_log.rxcnt[port->tty->index] += cnt;
2092 port->mon_data.rxcnt += cnt;
2093 port->mon_data.up_rxcnt += cnt;
2094 spin_unlock_irqrestore(&port->slock, flags);
2096 tty_flip_buffer_push(tty);
2099 static void mxser_transmit_chars(struct mxser_port *port)
2102 unsigned long flags;
2104 spin_lock_irqsave(&port->slock, flags);
2107 outb(port->x_char, port->ioaddr + UART_TX);
2109 mxvar_log.txcnt[port->tty->index]++;
2110 port->mon_data.txcnt++;
2111 port->mon_data.up_txcnt++;
2113 /* added by casper 1/11/2000 */
2118 if (port->xmit_buf == 0)
2121 if (port->xmit_cnt == 0) {
2122 if (port->xmit_cnt < WAKEUP_CHARS) { /* XXX what's this for?? */
2123 set_bit(MXSER_EVENT_TXLOW, &port->event);
2124 schedule_work(&port->tqueue);
2128 if (port->tty->stopped || (port->tty->hw_stopped &&
2129 (port->type != PORT_16550A) &&
2130 (!port->board->chip_flag))) {
2131 port->IER &= ~UART_IER_THRI;
2132 outb(port->IER, port->ioaddr + UART_IER);
2136 cnt = port->xmit_cnt;
2137 count = port->xmit_fifo_size;
2139 outb(port->xmit_buf[port->xmit_tail++],
2140 port->ioaddr + UART_TX);
2141 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2142 if (--port->xmit_cnt <= 0)
2144 } while (--count > 0);
2145 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
2147 /* added by James 03-12-2004. */
2148 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2149 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2151 /* added by casper 1/11/2000 */
2152 port->icount.tx += (cnt - port->xmit_cnt);
2154 if (port->xmit_cnt < WAKEUP_CHARS) {
2155 set_bit(MXSER_EVENT_TXLOW, &port->event);
2156 schedule_work(&port->tqueue);
2158 if (port->xmit_cnt <= 0) {
2159 port->IER &= ~UART_IER_THRI;
2160 outb(port->IER, port->ioaddr + UART_IER);
2163 spin_unlock_irqrestore(&port->slock, flags);
2166 static void mxser_check_modem_status(struct mxser_port *port, int status)
2168 /* update input line counters */
2169 if (status & UART_MSR_TERI)
2171 if (status & UART_MSR_DDSR)
2173 if (status & UART_MSR_DDCD)
2175 if (status & UART_MSR_DCTS)
2177 port->mon_data.modem_status = status;
2178 wake_up_interruptible(&port->delta_msr_wait);
2180 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
2181 if (status & UART_MSR_DCD)
2182 wake_up_interruptible(&port->open_wait);
2183 schedule_work(&port->tqueue);
2186 if (port->flags & ASYNC_CTS_FLOW) {
2187 if (port->tty->hw_stopped) {
2188 if (status & UART_MSR_CTS) {
2189 port->tty->hw_stopped = 0;
2191 if ((port->type != PORT_16550A) &&
2192 (!port->board->chip_flag)) {
2193 outb(port->IER & ~UART_IER_THRI,
2194 port->ioaddr + UART_IER);
2195 port->IER |= UART_IER_THRI;
2196 outb(port->IER, port->ioaddr +
2199 set_bit(MXSER_EVENT_TXLOW, &port->event);
2200 schedule_work(&port->tqueue);
2203 if (!(status & UART_MSR_CTS)) {
2204 port->tty->hw_stopped = 1;
2205 if (port->type != PORT_16550A &&
2206 !port->board->chip_flag) {
2207 port->IER &= ~UART_IER_THRI;
2208 outb(port->IER, port->ioaddr +
2216 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp, struct mxser_port *port)
2218 DECLARE_WAITQUEUE(wait, current);
2221 unsigned long flags;
2224 * If non-blocking mode is set, or the port is not enabled,
2225 * then make the check up front and then exit.
2227 if ((filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR))) {
2228 port->flags |= ASYNC_NORMAL_ACTIVE;
2232 if (tty->termios->c_cflag & CLOCAL)
2236 * Block waiting for the carrier detect and the line to become
2237 * free (i.e., not in use by the callout). While we are in
2238 * this loop, port->count is dropped by one, so that
2239 * mxser_close() knows when to free things. We restore it upon
2240 * exit, either normal or abnormal.
2243 add_wait_queue(&port->open_wait, &wait);
2245 spin_lock_irqsave(&port->slock, flags);
2246 if (!tty_hung_up_p(filp))
2248 spin_unlock_irqrestore(&port->slock, flags);
2249 port->blocked_open++;
2251 spin_lock_irqsave(&port->slock, flags);
2252 outb(inb(port->ioaddr + UART_MCR) |
2253 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
2254 spin_unlock_irqrestore(&port->slock, flags);
2255 set_current_state(TASK_INTERRUPTIBLE);
2256 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
2257 if (port->flags & ASYNC_HUP_NOTIFY)
2260 retval = -ERESTARTSYS;
2263 if (!(port->flags & ASYNC_CLOSING) &&
2265 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
2267 if (signal_pending(current)) {
2268 retval = -ERESTARTSYS;
2273 set_current_state(TASK_RUNNING);
2274 remove_wait_queue(&port->open_wait, &wait);
2275 if (!tty_hung_up_p(filp))
2277 port->blocked_open--;
2280 port->flags |= ASYNC_NORMAL_ACTIVE;
2284 static int mxser_startup(struct mxser_port *info)
2287 unsigned long flags;
2289 page = __get_free_page(GFP_KERNEL);
2293 spin_lock_irqsave(&info->slock, flags);
2295 if (info->flags & ASYNC_INITIALIZED) {
2297 spin_unlock_irqrestore(&info->slock, flags);
2301 if (!info->ioaddr || !info->type) {
2303 set_bit(TTY_IO_ERROR, &info->tty->flags);
2305 spin_unlock_irqrestore(&info->slock, flags);
2311 info->xmit_buf = (unsigned char *) page;
2314 * Clear the FIFO buffers and disable them
2315 * (they will be reenabled in mxser_change_speed())
2317 if (info->board->chip_flag)
2318 outb((UART_FCR_CLEAR_RCVR |
2319 UART_FCR_CLEAR_XMIT |
2320 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
2322 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
2323 info->ioaddr + UART_FCR);
2326 * At this point there's no way the LSR could still be 0xFF;
2327 * if it is, then bail out, because there's likely no UART
2330 if (inb(info->ioaddr + UART_LSR) == 0xff) {
2331 spin_unlock_irqrestore(&info->slock, flags);
2332 if (capable(CAP_SYS_ADMIN)) {
2334 set_bit(TTY_IO_ERROR, &info->tty->flags);
2341 * Clear the interrupt registers.
2343 (void) inb(info->ioaddr + UART_LSR);
2344 (void) inb(info->ioaddr + UART_RX);
2345 (void) inb(info->ioaddr + UART_IIR);
2346 (void) inb(info->ioaddr + UART_MSR);
2349 * Now, initialize the UART
2351 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
2352 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
2353 outb(info->MCR, info->ioaddr + UART_MCR);
2356 * Finally, enable interrupts
2358 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
2359 /* info->IER = UART_IER_RLSI | UART_IER_RDI; */
2361 /* following add by Victor Yu. 08-30-2002 */
2362 if (info->board->chip_flag)
2363 info->IER |= MOXA_MUST_IER_EGDAI;
2364 /* above add by Victor Yu. 08-30-2002 */
2365 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
2368 * And clear the interrupt registers again for luck.
2370 (void) inb(info->ioaddr + UART_LSR);
2371 (void) inb(info->ioaddr + UART_RX);
2372 (void) inb(info->ioaddr + UART_IIR);
2373 (void) inb(info->ioaddr + UART_MSR);
2376 clear_bit(TTY_IO_ERROR, &info->tty->flags);
2377 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
2380 * and set the speed of the serial port
2382 spin_unlock_irqrestore(&info->slock, flags);
2383 mxser_change_speed(info, NULL);
2385 info->flags |= ASYNC_INITIALIZED;
2390 * This routine will shutdown a serial port; interrupts maybe disabled, and
2391 * DTR is dropped if the hangup on close termio flag is on.
2393 static void mxser_shutdown(struct mxser_port *info)
2395 unsigned long flags;
2397 if (!(info->flags & ASYNC_INITIALIZED))
2400 spin_lock_irqsave(&info->slock, flags);
2403 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
2404 * here so the queue might never be waken up
2406 wake_up_interruptible(&info->delta_msr_wait);
2409 * Free the IRQ, if necessary
2411 if (info->xmit_buf) {
2412 free_page((unsigned long) info->xmit_buf);
2413 info->xmit_buf = NULL;
2417 outb(0x00, info->ioaddr + UART_IER);
2419 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
2420 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
2421 outb(info->MCR, info->ioaddr + UART_MCR);
2423 /* clear Rx/Tx FIFO's */
2424 /* following add by Victor Yu. 08-30-2002 */
2425 if (info->board->chip_flag)
2426 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
2427 MOXA_MUST_FCR_GDA_MODE_ENABLE,
2428 info->ioaddr + UART_FCR);
2430 /* above add by Victor Yu. 08-30-2002 */
2431 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
2432 info->ioaddr + UART_FCR);
2434 /* read data port to reset things */
2435 (void) inb(info->ioaddr + UART_RX);
2438 set_bit(TTY_IO_ERROR, &info->tty->flags);
2440 info->flags &= ~ASYNC_INITIALIZED;
2442 /* following add by Victor Yu. 09-23-2002 */
2443 if (info->board->chip_flag)
2444 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2445 /* above add by Victor Yu. 09-23-2002 */
2447 spin_unlock_irqrestore(&info->slock, flags);
2451 * This routine is called to set the UART divisor registers to match
2452 * the specified baud rate for a serial port.
2454 static int mxser_change_speed(struct mxser_port *info,
2455 struct termios *old_termios)
2457 unsigned cflag, cval, fcr;
2459 unsigned char status;
2461 unsigned long flags;
2463 if (!info->tty || !info->tty->termios)
2465 cflag = info->tty->termios->c_cflag;
2466 if (!(info->ioaddr))
2470 #define B921600 (B460800 +1)
2472 if (mxser_set_baud_method[info->tty->index] == 0) {
2473 baud = tty_get_baud_rate(info->tty);
2474 mxser_set_baud(info, baud);
2477 /* byte size and parity */
2478 switch (cflag & CSIZE) {
2493 break; /* too keep GCC shut... */
2498 cval |= UART_LCR_PARITY;
2499 if (!(cflag & PARODD))
2500 cval |= UART_LCR_EPAR;
2502 cval |= UART_LCR_SPAR;
2504 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
2505 if (info->board->chip_flag) {
2506 fcr = UART_FCR_ENABLE_FIFO;
2507 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2508 SET_MOXA_MUST_FIFO_VALUE(info);
2512 fcr = UART_FCR_ENABLE_FIFO;
2513 /* following add by Victor Yu. 08-30-2002 */
2514 if (info->board->chip_flag) {
2515 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2516 SET_MOXA_MUST_FIFO_VALUE(info);
2518 /* above add by Victor Yu. 08-30-2002 */
2519 switch (info->rx_trigger) {
2521 fcr |= UART_FCR_TRIGGER_1;
2524 fcr |= UART_FCR_TRIGGER_4;
2527 fcr |= UART_FCR_TRIGGER_8;
2530 fcr |= UART_FCR_TRIGGER_14;
2536 /* CTS flow control flag and modem status interrupts */
2537 info->IER &= ~UART_IER_MSI;
2538 info->MCR &= ~UART_MCR_AFE;
2539 if (cflag & CRTSCTS) {
2540 info->flags |= ASYNC_CTS_FLOW;
2541 info->IER |= UART_IER_MSI;
2542 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
2543 info->MCR |= UART_MCR_AFE;
2544 /* status = mxser_get_msr(info->ioaddr, 0, info->port); */
2548 status = inb(baseaddr + UART_MSR);
2549 restore_flags(flags);
2551 /* mxser_check_modem_status(info, status); */
2553 /* status = mxser_get_msr(info->ioaddr, 0, info->port); */
2554 /* MX_LOCK(&info->slock); */
2555 status = inb(info->ioaddr + UART_MSR);
2556 /* MX_UNLOCK(&info->slock); */
2557 if (info->tty->hw_stopped) {
2558 if (status & UART_MSR_CTS) {
2559 info->tty->hw_stopped = 0;
2560 if (info->type != PORT_16550A &&
2561 !info->board->chip_flag) {
2562 outb(info->IER & ~UART_IER_THRI,
2565 info->IER |= UART_IER_THRI;
2566 outb(info->IER, info->ioaddr +
2569 set_bit(MXSER_EVENT_TXLOW, &info->event);
2570 schedule_work(&info->tqueue); }
2572 if (!(status & UART_MSR_CTS)) {
2573 info->tty->hw_stopped = 1;
2574 if ((info->type != PORT_16550A) &&
2575 (!info->board->chip_flag)) {
2576 info->IER &= ~UART_IER_THRI;
2577 outb(info->IER, info->ioaddr +
2584 info->flags &= ~ASYNC_CTS_FLOW;
2586 outb(info->MCR, info->ioaddr + UART_MCR);
2587 if (cflag & CLOCAL) {
2588 info->flags &= ~ASYNC_CHECK_CD;
2590 info->flags |= ASYNC_CHECK_CD;
2591 info->IER |= UART_IER_MSI;
2593 outb(info->IER, info->ioaddr + UART_IER);
2596 * Set up parity check flag
2598 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2599 if (I_INPCK(info->tty))
2600 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2601 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
2602 info->read_status_mask |= UART_LSR_BI;
2604 info->ignore_status_mask = 0;
2606 if (I_IGNBRK(info->tty)) {
2607 info->ignore_status_mask |= UART_LSR_BI;
2608 info->read_status_mask |= UART_LSR_BI;
2610 * If we're ignore parity and break indicators, ignore
2611 * overruns too. (For real raw support).
2613 if (I_IGNPAR(info->tty)) {
2614 info->ignore_status_mask |=
2618 info->read_status_mask |=
2624 /* following add by Victor Yu. 09-02-2002 */
2625 if (info->board->chip_flag) {
2626 spin_lock_irqsave(&info->slock, flags);
2627 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
2628 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
2629 if (I_IXON(info->tty)) {
2630 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2632 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2634 if (I_IXOFF(info->tty)) {
2635 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2637 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2640 if ( I_IXANY(info->tty) ) {
2641 info->MCR |= MOXA_MUST_MCR_XON_ANY;
2642 ENABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
2644 info->MCR &= ~MOXA_MUST_MCR_XON_ANY;
2645 DISABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
2648 spin_unlock_irqrestore(&info->slock, flags);
2650 /* above add by Victor Yu. 09-02-2002 */
2653 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
2654 outb(cval, info->ioaddr + UART_LCR);
2660 static int mxser_set_baud(struct mxser_port *info, long newspd)
2665 unsigned long flags;
2667 if (!info->tty || !info->tty->termios)
2670 if (!(info->ioaddr))
2673 if (newspd > info->max_baud)
2676 info->realbaud = newspd;
2677 if (newspd == 134) {
2678 quot = (2 * info->baud_base / 269);
2679 } else if (newspd) {
2680 quot = info->baud_base / newspd;
2687 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
2688 info->timeout += HZ / 50; /* Add .02 seconds of slop */
2691 spin_lock_irqsave(&info->slock, flags);
2692 info->MCR |= UART_MCR_DTR;
2693 outb(info->MCR, info->ioaddr + UART_MCR);
2694 spin_unlock_irqrestore(&info->slock, flags);
2696 spin_lock_irqsave(&info->slock, flags);
2697 info->MCR &= ~UART_MCR_DTR;
2698 outb(info->MCR, info->ioaddr + UART_MCR);
2699 spin_unlock_irqrestore(&info->slock, flags);
2703 cval = inb(info->ioaddr + UART_LCR);
2705 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
2707 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
2708 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
2709 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
2716 * ------------------------------------------------------------
2717 * friends of mxser_ioctl()
2718 * ------------------------------------------------------------
2720 static int mxser_get_serial_info(struct mxser_port *info,
2721 struct serial_struct __user *retinfo)
2723 struct serial_struct tmp;
2727 memset(&tmp, 0, sizeof(tmp));
2728 tmp.type = info->type;
2729 tmp.line = info->tty->index;
2730 tmp.port = info->ioaddr;
2731 tmp.irq = info->board->irq;
2732 tmp.flags = info->flags;
2733 tmp.baud_base = info->baud_base;
2734 tmp.close_delay = info->close_delay;
2735 tmp.closing_wait = info->closing_wait;
2736 tmp.custom_divisor = info->custom_divisor;
2738 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2743 static int mxser_set_serial_info(struct mxser_port *info,
2744 struct serial_struct __user *new_info)
2746 struct serial_struct new_serial;
2750 if (!new_info || !info->ioaddr)
2752 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2755 if ((new_serial.irq != info->board->irq) ||
2756 (new_serial.port != info->ioaddr) ||
2757 (new_serial.custom_divisor != info->custom_divisor) ||
2758 (new_serial.baud_base != info->baud_base))
2761 flags = info->flags & ASYNC_SPD_MASK;
2763 if (!capable(CAP_SYS_ADMIN)) {
2764 if ((new_serial.baud_base != info->baud_base) ||
2765 (new_serial.close_delay != info->close_delay) ||
2766 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
2768 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
2769 (new_serial.flags & ASYNC_USR_MASK));
2772 * OK, past this point, all the error checking has been done.
2773 * At this point, we start making changes.....
2775 info->flags = ((info->flags & ~ASYNC_FLAGS) |
2776 (new_serial.flags & ASYNC_FLAGS));
2777 info->close_delay = new_serial.close_delay * HZ / 100;
2778 info->closing_wait = new_serial.closing_wait * HZ / 100;
2779 info->tty->low_latency =
2780 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2781 info->tty->low_latency = 0; /* (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0; */
2784 /* added by casper, 3/17/2000, for mouse */
2785 info->type = new_serial.type;
2787 process_txrx_fifo(info);
2789 if (info->flags & ASYNC_INITIALIZED) {
2790 if (flags != (info->flags & ASYNC_SPD_MASK))
2791 mxser_change_speed(info, NULL);
2793 retval = mxser_startup(info);
2799 * mxser_get_lsr_info - get line status register info
2801 * Purpose: Let user call ioctl() to get info when the UART physically
2802 * is emptied. On bus types like RS485, the transmitter must
2803 * release the bus after transmitting. This must be done when
2804 * the transmit shift register is empty, not be done when the
2805 * transmit holding register is empty. This functionality
2806 * allows an RS485 driver to be written in user space.
2808 static int mxser_get_lsr_info(struct mxser_port *info,
2809 unsigned int __user *value)
2811 unsigned char status;
2812 unsigned int result;
2813 unsigned long flags;
2815 spin_lock_irqsave(&info->slock, flags);
2816 status = inb(info->ioaddr + UART_LSR);
2817 spin_unlock_irqrestore(&info->slock, flags);
2818 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
2819 return put_user(result, value);
2823 * This routine sends a break character out the serial port.
2825 static void mxser_send_break(struct mxser_port *info, int duration)
2827 unsigned long flags;
2831 set_current_state(TASK_INTERRUPTIBLE);
2832 spin_lock_irqsave(&info->slock, flags);
2833 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2834 info->ioaddr + UART_LCR);
2835 spin_unlock_irqrestore(&info->slock, flags);
2836 schedule_timeout(duration);
2837 spin_lock_irqsave(&info->slock, flags);
2838 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2839 info->ioaddr + UART_LCR);
2840 spin_unlock_irqrestore(&info->slock, flags);
2843 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
2845 struct mxser_port *info = tty->driver_data;
2846 unsigned char control, status;
2847 unsigned long flags;
2850 if (tty->index == MXSER_PORTS)
2851 return -ENOIOCTLCMD;
2852 if (tty->flags & (1 << TTY_IO_ERROR))
2855 control = info->MCR;
2857 spin_lock_irqsave(&info->slock, flags);
2858 status = inb(info->ioaddr + UART_MSR);
2859 if (status & UART_MSR_ANY_DELTA)
2860 mxser_check_modem_status(info, status);
2861 spin_unlock_irqrestore(&info->slock, flags);
2862 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
2863 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
2864 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
2865 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
2866 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
2867 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
2870 static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
2871 unsigned int set, unsigned int clear)
2873 struct mxser_port *info = tty->driver_data;
2874 unsigned long flags;
2877 if (tty->index == MXSER_PORTS)
2878 return -ENOIOCTLCMD;
2879 if (tty->flags & (1 << TTY_IO_ERROR))
2882 spin_lock_irqsave(&info->slock, flags);
2884 if (set & TIOCM_RTS)
2885 info->MCR |= UART_MCR_RTS;
2886 if (set & TIOCM_DTR)
2887 info->MCR |= UART_MCR_DTR;
2889 if (clear & TIOCM_RTS)
2890 info->MCR &= ~UART_MCR_RTS;
2891 if (clear & TIOCM_DTR)
2892 info->MCR &= ~UART_MCR_DTR;
2894 outb(info->MCR, info->ioaddr + UART_MCR);
2895 spin_unlock_irqrestore(&info->slock, flags);
2900 static int mxser_read_register(int, unsigned short *);
2901 static int mxser_program_mode(int);
2902 static void mxser_normal_mode(int);
2904 static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
2907 unsigned short regs[16], irq;
2908 unsigned char scratch, scratch2;
2910 brd->chip_flag = MOXA_OTHER_UART;
2912 id = mxser_read_register(cap, regs);
2913 if (id == C168_ASIC_ID) {
2914 brd->board_type = MXSER_BOARD_C168_ISA;
2916 } else if (id == C104_ASIC_ID) {
2917 brd->board_type = MXSER_BOARD_C104_ISA;
2919 } else if (id == C102_ASIC_ID) {
2920 brd->board_type = MXSER_BOARD_C102_ISA;
2922 } else if (id == CI132_ASIC_ID) {
2923 brd->board_type = MXSER_BOARD_CI132;
2925 } else if (id == CI134_ASIC_ID) {
2926 brd->board_type = MXSER_BOARD_CI134;
2928 } else if (id == CI104J_ASIC_ID) {
2929 brd->board_type = MXSER_BOARD_CI104J;
2935 if (brd->nports == 2) {
2936 irq = regs[9] & 0xF000;
2937 irq = irq | (irq >> 4);
2938 if (irq != (regs[9] & 0xFF00))
2939 return MXSER_ERR_IRQ_CONFLIT;
2940 } else if (brd->nports == 4) {
2941 irq = regs[9] & 0xF000;
2942 irq = irq | (irq >> 4);
2943 irq = irq | (irq >> 8);
2945 return MXSER_ERR_IRQ_CONFLIT;
2946 } else if (brd->nports == 8) {
2947 irq = regs[9] & 0xF000;
2948 irq = irq | (irq >> 4);
2949 irq = irq | (irq >> 8);
2950 if ((irq != regs[9]) || (irq != regs[10]))
2951 return MXSER_ERR_IRQ_CONFLIT;
2955 return MXSER_ERR_IRQ;
2956 brd->irq = ((int)(irq & 0xF000) >> 12);
2957 for (i = 0; i < 8; i++)
2958 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
2959 if ((regs[12] & 0x80) == 0)
2960 return MXSER_ERR_VECTOR;
2961 brd->vector = (int)regs[11]; /* interrupt vector */
2963 brd->vector_mask = 0x00FF;
2965 brd->vector_mask = 0x000F;
2966 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2967 if (regs[12] & bits) {
2968 brd->ports[i].baud_base = 921600;
2969 brd->ports[i].max_baud = 921600; /* add by Victor Yu. 09-04-2002 */
2971 brd->ports[i].baud_base = 115200;
2972 brd->ports[i].max_baud = 115200; /* add by Victor Yu. 09-04-2002 */
2975 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2976 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2977 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2978 outb(scratch2, cap + UART_LCR);
2979 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2980 scratch = inb(cap + UART_IIR);
2983 brd->uart_type = PORT_16550A;
2985 brd->uart_type = PORT_16450;
2990 if (!request_region(brd->ports[0].ioaddr, 8 * brd->nports, "mxser(IO)"))
2991 return MXSER_ERR_IOADDR;
2992 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2993 release_region(brd->ports[0].ioaddr, 8 * brd->nports);
2994 return MXSER_ERR_VECTOR;
2999 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
3000 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
3001 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
3002 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
3003 #define EN_CCMD 0x000 /* Chip's command register */
3004 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
3005 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
3006 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
3007 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
3008 #define EN0_DCFG 0x00E /* Data configuration reg WR */
3009 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
3010 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
3011 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
3012 static int mxser_read_register(int port, unsigned short *regs)
3014 int i, k, value, id;
3017 id = mxser_program_mode(port);
3020 for (i = 0; i < 14; i++) {
3021 k = (i & 0x3F) | 0x180;
3022 for (j = 0x100; j > 0; j >>= 1) {
3023 outb(CHIP_CS, port);
3025 outb(CHIP_CS | CHIP_DO, port);
3026 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
3028 outb(CHIP_CS, port);
3029 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
3034 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
3035 outb(CHIP_CS, port);
3036 outb(CHIP_CS | CHIP_SK, port);
3037 if (inb(port) & CHIP_DI)
3043 mxser_normal_mode(port);
3047 static int mxser_program_mode(int port)
3050 /* unsigned long flags; */
3052 spin_lock(&gm_lock);
3060 /* restore_flags(flags); */
3061 spin_unlock(&gm_lock);
3063 id = inb(port + 1) & 0x1F;
3064 if ((id != C168_ASIC_ID) &&
3065 (id != C104_ASIC_ID) &&
3066 (id != C102_ASIC_ID) &&
3067 (id != CI132_ASIC_ID) &&
3068 (id != CI134_ASIC_ID) &&
3069 (id != CI104J_ASIC_ID))
3071 for (i = 0, j = 0; i < 4; i++) {
3075 } else if ((j == 1) && (n == 1)) {
3086 static void mxser_normal_mode(int port)
3090 outb(0xA5, port + 1);
3091 outb(0x80, port + 3);
3092 outb(12, port + 0); /* 9600 bps */
3094 outb(0x03, port + 3); /* 8 data bits */
3095 outb(0x13, port + 4); /* loop back mode */
3096 for (i = 0; i < 16; i++) {
3098 if ((n & 0x61) == 0x60)
3103 outb(0x00, port + 4);
3106 module_init(mxser_module_init);
3107 module_exit(mxser_module_exit);