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}
195 static struct pci_device_id mxser_pcibrds[] = {
196 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C168),
197 .driver_data = MXSER_BOARD_C168_PCI },
198 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C104),
199 .driver_data = MXSER_BOARD_C104_PCI },
200 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132),
201 .driver_data = MXSER_BOARD_CP132 },
202 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP114),
203 .driver_data = MXSER_BOARD_CP114 },
204 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CT114),
205 .driver_data = MXSER_BOARD_CT114 },
206 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102),
207 .driver_data = MXSER_BOARD_CP102 },
208 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104U),
209 .driver_data = MXSER_BOARD_CP104U },
210 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168U),
211 .driver_data = MXSER_BOARD_CP168U },
212 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132U),
213 .driver_data = MXSER_BOARD_CP132U },
214 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP134U),
215 .driver_data = MXSER_BOARD_CP134U },
216 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104JU),
217 .driver_data = MXSER_BOARD_CP104JU },
218 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_RC7000),
219 .driver_data = MXSER_BOARD_RC7000 },
220 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118U),
221 .driver_data = MXSER_BOARD_CP118U },
222 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102UL),
223 .driver_data = MXSER_BOARD_CP102UL },
224 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102U),
225 .driver_data = MXSER_BOARD_CP102U },
226 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118EL),
227 .driver_data = MXSER_BOARD_CP118EL },
228 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168EL),
229 .driver_data = MXSER_BOARD_CP168EL },
230 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104EL),
231 .driver_data = MXSER_BOARD_CP104EL },
234 MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
236 static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
237 static int ttymajor = MXSERMAJOR;
238 static int calloutmajor = MXSERCUMAJOR;
240 /* Variables for insmod */
242 MODULE_AUTHOR("Casper Yang");
243 MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
244 module_param_array(ioaddr, int, NULL, 0);
245 module_param(ttymajor, int, 0);
246 MODULE_LICENSE("GPL");
250 unsigned long rxcnt[MXSER_PORTS];
251 unsigned long txcnt[MXSER_PORTS];
258 unsigned long up_rxcnt;
259 unsigned long up_txcnt;
261 unsigned char hold_reason;
264 struct mxser_mon_ext {
265 unsigned long rx_cnt[32];
266 unsigned long tx_cnt[32];
267 unsigned long up_rxcnt[32];
268 unsigned long up_txcnt[32];
269 int modem_status[32];
283 struct mxser_board *board;
284 struct tty_struct *tty;
286 unsigned long ioaddr;
287 unsigned long opmode_ioaddr;
291 int rx_trigger; /* Rx fifo trigger level */
293 int baud_base; /* max. speed */
295 int type; /* UART type */
296 int flags; /* defined in tty.h */
297 long session; /* Session of opening process */
298 long pgrp; /* pgrp of opening process */
300 int x_char; /* xon/xoff character */
301 int IER; /* Interrupt Enable Register */
302 int MCR; /* Modem control register */
304 unsigned char stop_rx;
305 unsigned char ldisc_stop_rx;
309 unsigned short closing_wait;
310 unsigned char err_shadow;
313 int count; /* # of fd on device */
314 int blocked_open; /* # of blocked opens */
315 struct async_icount icount; /* kernel counters for 4 input interrupts */
318 int read_status_mask;
319 int ignore_status_mask;
321 unsigned char *xmit_buf;
326 struct termios normal_termios;
327 struct termios callout_termios;
329 struct mxser_mon mon_data;
332 struct work_struct tqueue;
333 wait_queue_head_t open_wait;
334 wait_queue_head_t close_wait;
335 wait_queue_head_t delta_msr_wait;
339 struct pci_dev *pdev; /* temporary (until pci probing) */
344 unsigned long vector;
345 unsigned long vector_mask;
350 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
353 struct mxser_mstatus {
361 static struct mxser_mstatus GMStatus[MXSER_PORTS];
363 static int mxserBoardCAP[MXSER_BOARDS] = {
365 /* 0x180, 0x280, 0x200, 0x320 */
368 static struct mxser_board mxser_boards[MXSER_BOARDS];
369 static struct tty_driver *mxvar_sdriver;
370 static struct tty_struct *mxvar_tty[MXSER_PORTS + 1];
371 static struct termios *mxvar_termios[MXSER_PORTS + 1];
372 static struct termios *mxvar_termios_locked[MXSER_PORTS + 1];
373 static struct mxser_log mxvar_log;
374 static int mxvar_diagflag;
375 static unsigned char mxser_msr[MXSER_PORTS + 1];
376 static struct mxser_mon_ext mon_data_ext;
377 static int mxser_set_baud_method[MXSER_PORTS + 1];
378 static spinlock_t gm_lock;
384 static int mxser_init(void);
386 /* static void mxser_poll(unsigned long); */
387 static int mxser_get_ISA_conf(int, struct mxser_board *);
388 static void mxser_do_softint(void *);
389 static int mxser_open(struct tty_struct *, struct file *);
390 static void mxser_close(struct tty_struct *, struct file *);
391 static int mxser_write(struct tty_struct *, const unsigned char *, int);
392 static int mxser_write_room(struct tty_struct *);
393 static void mxser_flush_buffer(struct tty_struct *);
394 static int mxser_chars_in_buffer(struct tty_struct *);
395 static void mxser_flush_chars(struct tty_struct *);
396 static void mxser_put_char(struct tty_struct *, unsigned char);
397 static int mxser_ioctl(struct tty_struct *, struct file *, uint, ulong);
398 static int mxser_ioctl_special(unsigned int, void __user *);
399 static void mxser_throttle(struct tty_struct *);
400 static void mxser_unthrottle(struct tty_struct *);
401 static void mxser_set_termios(struct tty_struct *, struct termios *);
402 static void mxser_stop(struct tty_struct *);
403 static void mxser_start(struct tty_struct *);
404 static void mxser_hangup(struct tty_struct *);
405 static void mxser_rs_break(struct tty_struct *, int);
406 static irqreturn_t mxser_interrupt(int, void *, struct pt_regs *);
407 static void mxser_receive_chars(struct mxser_port *, int *);
408 static void mxser_transmit_chars(struct mxser_port *);
409 static void mxser_check_modem_status(struct mxser_port *, int);
410 static int mxser_block_til_ready(struct tty_struct *, struct file *,
411 struct mxser_port *);
412 static int mxser_startup(struct mxser_port *);
413 static void mxser_shutdown(struct mxser_port *);
414 static int mxser_change_speed(struct mxser_port *, struct termios *);
415 static int mxser_get_serial_info(struct mxser_port *,
416 struct serial_struct __user *);
417 static int mxser_set_serial_info(struct mxser_port *,
418 struct serial_struct __user *);
419 static int mxser_get_lsr_info(struct mxser_port *, unsigned int __user *);
420 static void mxser_send_break(struct mxser_port *, int);
421 static int mxser_tiocmget(struct tty_struct *, struct file *);
422 static int mxser_tiocmset(struct tty_struct *, struct file *, unsigned int,
424 static int mxser_set_baud(struct mxser_port *, long);
425 static void mxser_wait_until_sent(struct tty_struct *, int);
427 static void mxser_startrx(struct tty_struct *);
428 static void mxser_stoprx(struct tty_struct *);
431 static int CheckIsMoxaMust(int io)
436 outb(0, io + UART_LCR);
437 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
438 oldmcr = inb(io + UART_MCR);
439 outb(0, io + UART_MCR);
440 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
441 if ((hwid = inb(io + UART_MCR)) != 0) {
442 outb(oldmcr, io + UART_MCR);
443 return MOXA_OTHER_UART;
446 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
447 for (i = 0; i < UART_TYPE_NUM; i++) {
448 if (hwid == Gmoxa_uart_id[i])
451 return MOXA_OTHER_UART;
454 /* above is modified by Victor Yu. 08-15-2002 */
456 static const struct tty_operations mxser_ops = {
458 .close = mxser_close,
459 .write = mxser_write,
460 .put_char = mxser_put_char,
461 .flush_chars = mxser_flush_chars,
462 .write_room = mxser_write_room,
463 .chars_in_buffer = mxser_chars_in_buffer,
464 .flush_buffer = mxser_flush_buffer,
465 .ioctl = mxser_ioctl,
466 .throttle = mxser_throttle,
467 .unthrottle = mxser_unthrottle,
468 .set_termios = mxser_set_termios,
470 .start = mxser_start,
471 .hangup = mxser_hangup,
472 .break_ctl = mxser_rs_break,
473 .wait_until_sent = mxser_wait_until_sent,
474 .tiocmget = mxser_tiocmget,
475 .tiocmset = mxser_tiocmset,
479 * The MOXA Smartio/Industio serial driver boot-time initialization code!
482 static int __init mxser_module_init(void)
486 pr_debug("Loading module mxser ...\n");
492 static void __exit mxser_module_exit(void)
496 pr_debug("Unloading module mxser ...\n");
498 err = tty_unregister_driver(mxvar_sdriver);
500 put_tty_driver(mxvar_sdriver);
502 printk(KERN_ERR "Couldn't unregister MOXA Smartio/Industio family serial driver\n");
504 for (i = 0; i < MXSER_BOARDS; i++) {
505 struct pci_dev *pdev;
507 if (mxser_boards[i].board_type == -1)
510 pdev = mxser_boards[i].pdev;
511 free_irq(mxser_boards[i].irq, &mxser_boards[i]);
512 if (pdev != NULL) { /* PCI */
513 pci_release_region(pdev, 2);
514 pci_release_region(pdev, 3);
517 release_region(mxser_boards[i].ports[0].ioaddr, 8 * mxser_boards[i].nports);
518 release_region(mxser_boards[i].vector, 1);
525 static void process_txrx_fifo(struct mxser_port *info)
529 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
530 info->rx_trigger = 1;
531 info->rx_high_water = 1;
532 info->rx_low_water = 1;
533 info->xmit_fifo_size = 1;
535 for (i = 0; i < UART_INFO_NUM; i++)
536 if (info->board->chip_flag == Gpci_uart_info[i].type) {
537 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
538 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
539 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
540 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
545 static int __devinit mxser_initbrd(struct mxser_board *brd)
547 struct mxser_port *info;
551 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
553 for (i = 0; i < brd->nports; i++) {
554 info = &brd->ports[i];
557 info->ldisc_stop_rx = 0;
559 /* Enhance mode enabled here */
560 if (brd->chip_flag != MOXA_OTHER_UART)
561 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
563 info->flags = ASYNC_SHARE_IRQ;
564 info->type = brd->uart_type;
566 process_txrx_fifo(info);
568 info->custom_divisor = info->baud_base * 16;
569 info->close_delay = 5 * HZ / 10;
570 info->closing_wait = 30 * HZ;
571 INIT_WORK(&info->tqueue, mxser_do_softint, info);
572 info->normal_termios = mxvar_sdriver->init_termios;
573 init_waitqueue_head(&info->open_wait);
574 init_waitqueue_head(&info->close_wait);
575 init_waitqueue_head(&info->delta_msr_wait);
576 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
577 info->err_shadow = 0;
578 spin_lock_init(&info->slock);
580 /* before set INT ISR, disable all int */
581 outb(inb(info->ioaddr + UART_IER) & 0xf0,
582 info->ioaddr + UART_IER);
585 * Allocate the IRQ if necessary
588 retval = request_irq(brd->irq, mxser_interrupt,
589 (brd->ports[0].flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED :
590 IRQF_DISABLED, "mxser", brd);
592 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
593 "conflict with another device.\n",
594 mxser_brdname[brd->board_type - 1], brd->irq);
600 static int __init mxser_get_PCI_conf(int board_type, struct mxser_board *brd,
601 struct pci_dev *pdev)
604 unsigned long ioaddress;
608 brd->board_type = board_type;
609 brd->nports = mxser_numports[board_type - 1];
610 ioaddress = pci_resource_start(pdev, 2);
611 retval = pci_request_region(pdev, 2, "mxser(IO)");
615 for (i = 0; i < brd->nports; i++)
616 brd->ports[i].ioaddr = ioaddress + 8 * i;
619 ioaddress = pci_resource_start(pdev, 3);
620 retval = pci_request_region(pdev, 3, "mxser(vector)");
623 brd->vector = ioaddress;
626 brd->irq = pdev->irq;
628 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
629 brd->uart_type = PORT_16550A;
630 brd->vector_mask = 0;
632 for (i = 0; i < brd->nports; i++) {
633 for (j = 0; j < UART_INFO_NUM; j++) {
634 if (Gpci_uart_info[j].type == brd->chip_flag) {
635 brd->ports[i].max_baud =
636 Gpci_uart_info[j].max_baud;
638 /* exception....CP-102 */
639 if (board_type == MXSER_BOARD_CP102)
640 brd->ports[i].max_baud = 921600;
646 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
647 for (i = 0; i < brd->nports; i++) {
649 brd->ports[i].opmode_ioaddr = ioaddress + 4;
651 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
653 outb(0, ioaddress + 4); /* default set to RS232 mode */
654 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
657 for (i = 0; i < brd->nports; i++) {
658 brd->vector_mask |= (1 << i);
659 brd->ports[i].baud_base = 921600;
663 pci_release_region(pdev, 2);
668 static int __init mxser_init(void)
670 struct pci_dev *pdev = NULL;
671 struct mxser_board *brd;
675 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
678 spin_lock_init(&gm_lock);
680 for (i = 0; i < MXSER_BOARDS; i++)
681 mxser_boards[i].board_type = -1;
683 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
686 /* Initialize the tty_driver structure */
687 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
688 mxvar_sdriver->name = "ttyM";
689 mxvar_sdriver->major = ttymajor;
690 mxvar_sdriver->minor_start = 0;
691 mxvar_sdriver->num = MXSER_PORTS + 1;
692 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
693 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
694 mxvar_sdriver->init_termios = tty_std_termios;
695 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
696 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
697 tty_set_operations(mxvar_sdriver, &mxser_ops);
698 mxvar_sdriver->ttys = mxvar_tty;
699 mxvar_sdriver->termios = mxvar_termios;
700 mxvar_sdriver->termios_locked = mxvar_termios_locked;
705 /* Start finding ISA boards here */
706 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
709 if (!(cap = mxserBoardCAP[b]))
712 brd = &mxser_boards[m];
713 retval = mxser_get_ISA_conf(cap, brd);
716 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
717 mxser_brdname[brd->board_type - 1], ioaddr[b]);
720 if (retval == MXSER_ERR_IRQ)
721 printk(KERN_ERR "Invalid interrupt number, "
722 "board not configured\n");
723 else if (retval == MXSER_ERR_IRQ_CONFLIT)
724 printk(KERN_ERR "Invalid interrupt number, "
725 "board not configured\n");
726 else if (retval == MXSER_ERR_VECTOR)
727 printk(KERN_ERR "Invalid interrupt vector, "
728 "board not configured\n");
729 else if (retval == MXSER_ERR_IOADDR)
730 printk(KERN_ERR "Invalid I/O address, "
731 "board not configured\n");
738 /* mxser_initbrd will hook ISR. */
739 if (mxser_initbrd(brd) < 0)
745 /* Start finding ISA boards from module arg */
746 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
749 if (!(cap = ioaddr[b]))
752 brd = &mxser_boards[m];
753 retval = mxser_get_ISA_conf(cap, &mxser_boards[m]);
756 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
757 mxser_brdname[brd->board_type - 1], ioaddr[b]);
760 if (retval == MXSER_ERR_IRQ)
761 printk(KERN_ERR "Invalid interrupt number, "
762 "board not configured\n");
763 else if (retval == MXSER_ERR_IRQ_CONFLIT)
764 printk(KERN_ERR "Invalid interrupt number, "
765 "board not configured\n");
766 else if (retval == MXSER_ERR_VECTOR)
767 printk(KERN_ERR "Invalid interrupt vector, "
768 "board not configured\n");
769 else if (retval == MXSER_ERR_IOADDR)
770 printk(KERN_ERR "Invalid I/O address, "
771 "board not configured\n");
777 /* mxser_initbrd will hook ISR. */
778 if (mxser_initbrd(brd) < 0)
784 /* start finding PCI board here */
785 n = ARRAY_SIZE(mxser_pcibrds) - 1;
788 pdev = pci_get_device(mxser_pcibrds[b].vendor,
789 mxser_pcibrds[b].device, pdev);
794 printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
795 mxser_brdname[(int) (mxser_pcibrds[b].driver_data) - 1],
796 pdev->bus->number, PCI_SLOT(pdev->devfn));
797 if (m >= MXSER_BOARDS)
799 "Too many Smartio/Industio family boards find "
800 "(maximum %d), board not configured\n",
803 if (pci_enable_device(pdev)) {
804 printk(KERN_ERR "Moxa SmartI/O PCI enable "
808 brd = &mxser_boards[m];
810 retval = mxser_get_PCI_conf(
811 (int)mxser_pcibrds[b].driver_data,
814 if (retval == MXSER_ERR_IRQ)
816 "Invalid interrupt number, "
817 "board not configured\n");
818 else if (retval == MXSER_ERR_IRQ_CONFLIT)
820 "Invalid interrupt number, "
821 "board not configured\n");
822 else if (retval == MXSER_ERR_VECTOR)
824 "Invalid interrupt vector, "
825 "board not configured\n");
826 else if (retval == MXSER_ERR_IOADDR)
828 "Invalid I/O address, "
829 "board not configured\n");
832 /* mxser_initbrd will hook ISR. */
833 if (mxser_initbrd(brd) < 0)
836 /* Keep an extra reference if we succeeded. It will
837 be returned at unload time */
842 retval = tty_register_driver(mxvar_sdriver);
844 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family"
846 put_tty_driver(mxvar_sdriver);
848 for (i = 0; i < MXSER_BOARDS; i++) {
849 if (mxser_boards[i].board_type == -1)
852 free_irq(mxser_boards[i].irq, &mxser_boards[i]);
853 /* todo: release io, vector */
862 static void mxser_do_softint(void *private_)
864 struct mxser_port *info = private_;
865 struct tty_struct *tty;
869 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
871 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event))
875 static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
877 unsigned char status = 0;
879 status = inb(baseaddr + UART_MSR);
881 mxser_msr[port] &= 0x0F;
882 mxser_msr[port] |= status;
883 status = mxser_msr[port];
891 * This routine is called whenever a serial port is opened. It
892 * enables interrupts for a serial port, linking in its async structure into
893 * the IRQ chain. It also performs the serial-specific
894 * initialization for the tty structure.
896 static int mxser_open(struct tty_struct *tty, struct file *filp)
898 struct mxser_port *info;
901 /* initialize driver_data in case something fails */
902 tty->driver_data = NULL;
905 if (line == MXSER_PORTS)
907 if (line < 0 || line > MXSER_PORTS)
909 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
913 tty->driver_data = info;
916 * Start up serial port
919 retval = mxser_startup(info);
923 retval = mxser_block_til_ready(tty, filp, info);
927 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
928 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
929 *tty->termios = info->normal_termios;
931 *tty->termios = info->callout_termios;
932 mxser_change_speed(info, NULL);
935 info->session = process_session(current);
936 info->pgrp = process_group(current);
939 status = mxser_get_msr(info->base, 0, info->port);
940 mxser_check_modem_status(info, status);
943 /* unmark here for very high baud rate (ex. 921600 bps) used */
944 tty->low_latency = 1;
949 * This routine is called when the serial port gets closed. First, we
950 * wait for the last remaining data to be sent. Then, we unlink its
951 * async structure from the interrupt chain if necessary, and we free
952 * that IRQ if nothing is left in the chain.
954 static void mxser_close(struct tty_struct *tty, struct file *filp)
956 struct mxser_port *info = tty->driver_data;
958 unsigned long timeout;
960 struct tty_ldisc *ld;
962 if (tty->index == MXSER_PORTS)
967 spin_lock_irqsave(&info->slock, flags);
969 if (tty_hung_up_p(filp)) {
970 spin_unlock_irqrestore(&info->slock, flags);
973 if ((tty->count == 1) && (info->count != 1)) {
975 * Uh, oh. tty->count is 1, which means that the tty
976 * structure will be freed. Info->count should always
977 * be one in these conditions. If it's greater than
978 * one, we've got real problems, since it means the
979 * serial port won't be shutdown.
981 printk(KERN_ERR "mxser_close: bad serial port count; "
982 "tty->count is 1, info->count is %d\n", info->count);
985 if (--info->count < 0) {
986 printk(KERN_ERR "mxser_close: bad serial port count for "
987 "ttys%d: %d\n", tty->index, info->count);
991 spin_unlock_irqrestore(&info->slock, flags);
994 info->flags |= ASYNC_CLOSING;
995 spin_unlock_irqrestore(&info->slock, flags);
997 * Save the termios structure, since this port may have
998 * separate termios for callout and dialin.
1000 if (info->flags & ASYNC_NORMAL_ACTIVE)
1001 info->normal_termios = *tty->termios;
1003 * Now we wait for the transmit buffer to clear; and we notify
1004 * the line discipline to only process XON/XOFF characters.
1007 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1008 tty_wait_until_sent(tty, info->closing_wait);
1010 * At this point we stop accepting input. To do this, we
1011 * disable the receive line status interrupts, and tell the
1012 * interrupt driver to stop checking the data ready bit in the
1013 * line status register.
1015 info->IER &= ~UART_IER_RLSI;
1016 if (info->board->chip_flag)
1017 info->IER &= ~MOXA_MUST_RECV_ISR;
1019 info->read_status_mask &= ~UART_LSR_DR;
1021 if (info->flags & ASYNC_INITIALIZED) {
1022 outb(info->IER, info->ioaddr + UART_IER);
1024 * Before we drop DTR, make sure the UART transmitter
1025 * has completely drained; this is especially
1026 * important if there is a transmit FIFO!
1028 timeout = jiffies + HZ;
1029 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
1030 schedule_timeout_interruptible(5);
1031 if (time_after(jiffies, timeout))
1035 mxser_shutdown(info);
1037 if (tty->driver->flush_buffer)
1038 tty->driver->flush_buffer(tty);
1040 ld = tty_ldisc_ref(tty);
1042 if (ld->flush_buffer)
1043 ld->flush_buffer(tty);
1044 tty_ldisc_deref(ld);
1050 if (info->blocked_open) {
1051 if (info->close_delay)
1052 schedule_timeout_interruptible(info->close_delay);
1053 wake_up_interruptible(&info->open_wait);
1056 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1057 wake_up_interruptible(&info->close_wait);
1061 static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1064 struct mxser_port *info = tty->driver_data;
1065 unsigned long flags;
1067 if (!info->xmit_buf)
1071 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1072 SERIAL_XMIT_SIZE - info->xmit_head));
1076 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1077 spin_lock_irqsave(&info->slock, flags);
1078 info->xmit_head = (info->xmit_head + c) &
1079 (SERIAL_XMIT_SIZE - 1);
1080 info->xmit_cnt += c;
1081 spin_unlock_irqrestore(&info->slock, flags);
1088 if (info->xmit_cnt && !tty->stopped
1089 /*&& !(info->IER & UART_IER_THRI)*/) {
1090 if (!tty->hw_stopped ||
1091 (info->type == PORT_16550A) ||
1092 (info->board->chip_flag)) {
1093 spin_lock_irqsave(&info->slock, flags);
1094 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1096 info->IER |= UART_IER_THRI;
1097 outb(info->IER, info->ioaddr + UART_IER);
1098 spin_unlock_irqrestore(&info->slock, flags);
1104 static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1106 struct mxser_port *info = tty->driver_data;
1107 unsigned long flags;
1109 if (!info->xmit_buf)
1112 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1115 spin_lock_irqsave(&info->slock, flags);
1116 info->xmit_buf[info->xmit_head++] = ch;
1117 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1119 spin_unlock_irqrestore(&info->slock, flags);
1120 if (!tty->stopped /*&& !(info->IER & UART_IER_THRI)*/) {
1121 if (!tty->hw_stopped ||
1122 (info->type == PORT_16550A) ||
1123 info->board->chip_flag) {
1124 spin_lock_irqsave(&info->slock, flags);
1125 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1126 info->IER |= UART_IER_THRI;
1127 outb(info->IER, info->ioaddr + UART_IER);
1128 spin_unlock_irqrestore(&info->slock, flags);
1134 static void mxser_flush_chars(struct tty_struct *tty)
1136 struct mxser_port *info = tty->driver_data;
1137 unsigned long flags;
1139 if (info->xmit_cnt <= 0 ||
1143 (info->type != PORT_16550A) &&
1144 (!info->board->chip_flag)
1148 spin_lock_irqsave(&info->slock, flags);
1150 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1151 info->IER |= UART_IER_THRI;
1152 outb(info->IER, info->ioaddr + UART_IER);
1154 spin_unlock_irqrestore(&info->slock, flags);
1157 static int mxser_write_room(struct tty_struct *tty)
1159 struct mxser_port *info = tty->driver_data;
1162 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1168 static int mxser_chars_in_buffer(struct tty_struct *tty)
1170 struct mxser_port *info = tty->driver_data;
1171 int len = info->xmit_cnt;
1173 if (!(inb(info->ioaddr + UART_LSR) & UART_LSR_THRE))
1179 static void mxser_flush_buffer(struct tty_struct *tty)
1181 struct mxser_port *info = tty->driver_data;
1183 unsigned long flags;
1186 spin_lock_irqsave(&info->slock, flags);
1187 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1189 /* below added by shinhay */
1190 fcr = inb(info->ioaddr + UART_FCR);
1191 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1192 info->ioaddr + UART_FCR);
1193 outb(fcr, info->ioaddr + UART_FCR);
1195 spin_unlock_irqrestore(&info->slock, flags);
1196 /* above added by shinhay */
1198 wake_up_interruptible(&tty->write_wait);
1199 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
1200 (tty->ldisc.write_wakeup) (tty);
1203 static int mxser_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1205 struct mxser_port *info = tty->driver_data;
1207 struct async_icount cprev, cnow; /* kernel counter temps */
1208 struct serial_icounter_struct __user *p_cuser;
1209 unsigned long templ;
1210 unsigned long flags;
1211 void __user *argp = (void __user *)arg;
1213 if (tty->index == MXSER_PORTS)
1214 return mxser_ioctl_special(cmd, argp);
1216 /* following add by Victor Yu. 01-05-2004 */
1217 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1219 unsigned long opmode;
1220 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1222 unsigned char val, mask;
1225 if (cmd == MOXA_SET_OP_MODE) {
1226 if (get_user(opmode, (int __user *) argp))
1228 if (opmode != RS232_MODE &&
1229 opmode != RS485_2WIRE_MODE &&
1230 opmode != RS422_MODE &&
1231 opmode != RS485_4WIRE_MODE)
1235 val = inb(info->opmode_ioaddr);
1237 val |= (opmode << shiftbit);
1238 outb(val, info->opmode_ioaddr);
1241 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1242 opmode &= OP_MODE_MASK;
1243 if (copy_to_user(argp, &opmode, sizeof(int)))
1248 /* above add by Victor Yu. 01-05-2004 */
1250 if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1251 if (tty->flags & (1 << TTY_IO_ERROR))
1255 case TCSBRK: /* SVID version: non-zero arg --> no break */
1256 retval = tty_check_change(tty);
1259 tty_wait_until_sent(tty, 0);
1261 mxser_send_break(info, HZ / 4); /* 1/4 second */
1263 case TCSBRKP: /* support for POSIX tcsendbreak() */
1264 retval = tty_check_change(tty);
1267 tty_wait_until_sent(tty, 0);
1268 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1271 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1273 if (get_user(templ, (unsigned long __user *) argp))
1276 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1279 return mxser_get_serial_info(info, argp);
1281 return mxser_set_serial_info(info, argp);
1282 case TIOCSERGETLSR: /* Get line status register */
1283 return mxser_get_lsr_info(info, argp);
1285 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1286 * - mask passed in arg for lines of interest
1287 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1288 * Caller should use TIOCGICOUNT to see which one it was
1291 DECLARE_WAITQUEUE(wait, current);
1293 spin_lock_irqsave(&info->slock, flags);
1294 cprev = info->icount; /* note the counters on entry */
1295 spin_unlock_irqrestore(&info->slock, flags);
1297 add_wait_queue(&info->delta_msr_wait, &wait);
1299 spin_lock_irqsave(&info->slock, flags);
1300 cnow = info->icount; /* atomic copy */
1301 spin_unlock_irqrestore(&info->slock, flags);
1303 set_current_state(TASK_INTERRUPTIBLE);
1304 if (((arg & TIOCM_RNG) &&
1305 (cnow.rng != cprev.rng)) ||
1306 ((arg & TIOCM_DSR) &&
1307 (cnow.dsr != cprev.dsr)) ||
1308 ((arg & TIOCM_CD) &&
1309 (cnow.dcd != cprev.dcd)) ||
1310 ((arg & TIOCM_CTS) &&
1311 (cnow.cts != cprev.cts))) {
1315 /* see if a signal did it */
1316 if (signal_pending(current)) {
1322 current->state = TASK_RUNNING;
1323 remove_wait_queue(&info->delta_msr_wait, &wait);
1328 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1329 * Return: write counters to the user passed counter struct
1330 * NB: both 1->0 and 0->1 transitions are counted except for
1331 * RI where only 0->1 is counted.
1334 spin_lock_irqsave(&info->slock, flags);
1335 cnow = info->icount;
1336 spin_unlock_irqrestore(&info->slock, flags);
1338 /* modified by casper 1/11/2000 */
1339 if (put_user(cnow.frame, &p_cuser->frame))
1341 if (put_user(cnow.brk, &p_cuser->brk))
1343 if (put_user(cnow.overrun, &p_cuser->overrun))
1345 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1347 if (put_user(cnow.parity, &p_cuser->parity))
1349 if (put_user(cnow.rx, &p_cuser->rx))
1351 if (put_user(cnow.tx, &p_cuser->tx))
1353 put_user(cnow.cts, &p_cuser->cts);
1354 put_user(cnow.dsr, &p_cuser->dsr);
1355 put_user(cnow.rng, &p_cuser->rng);
1356 put_user(cnow.dcd, &p_cuser->dcd);
1358 case MOXA_HighSpeedOn:
1359 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1360 case MOXA_SDS_RSTICOUNTER: {
1361 info->mon_data.rxcnt = 0;
1362 info->mon_data.txcnt = 0;
1365 /* (above) added by James. */
1366 case MOXA_ASPP_SETBAUD:{
1368 if (get_user(baud, (long __user *)argp))
1370 mxser_set_baud(info, baud);
1373 case MOXA_ASPP_GETBAUD:
1374 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1379 case MOXA_ASPP_OQUEUE:{
1382 len = mxser_chars_in_buffer(tty);
1384 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1386 len += (lsr ? 0 : 1);
1388 if (copy_to_user(argp, &len, sizeof(int)))
1393 case MOXA_ASPP_MON: {
1396 /* info->mon_data.ser_param = tty->termios->c_cflag; */
1398 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1399 mxser_check_modem_status(info, status);
1401 mcr = inb(info->ioaddr + UART_MCR);
1402 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1403 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1405 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1407 if (mcr & MOXA_MUST_MCR_TX_XON)
1408 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1410 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1412 if (info->tty->hw_stopped)
1413 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1415 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1417 if (copy_to_user(argp, &info->mon_data,
1418 sizeof(struct mxser_mon)))
1424 case MOXA_ASPP_LSTATUS: {
1425 if (copy_to_user(argp, &info->err_shadow,
1426 sizeof(unsigned char)))
1429 info->err_shadow = 0;
1432 case MOXA_SET_BAUD_METHOD: {
1435 if (get_user(method, (int __user *)argp))
1437 mxser_set_baud_method[tty->index] = method;
1438 if (copy_to_user(argp, &method, sizeof(int)))
1444 return -ENOIOCTLCMD;
1450 #define CMSPAR 010000000000
1453 static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1455 struct mxser_port *port;
1461 /* if (copy_to_user(argp, mxsercfg,
1462 sizeof(struct mxser_hwconf) * 4))
1466 case MOXA_GET_MAJOR:
1467 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1471 case MOXA_GET_CUMAJOR:
1472 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1476 case MOXA_CHKPORTENABLE:
1479 for (i = 0; i < MXSER_BOARDS; i++)
1480 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1481 if (mxser_boards[i].ports[j].ioaddr)
1484 return put_user(result, (unsigned long __user *)argp);
1485 case MOXA_GETDATACOUNT:
1486 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1489 case MOXA_GETMSTATUS:
1490 for (i = 0; i < MXSER_BOARDS; i++)
1491 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1492 port = &mxser_boards[i].ports[j];
1495 if (!port->ioaddr) {
1496 GMStatus[i].dcd = 0;
1497 GMStatus[i].dsr = 0;
1498 GMStatus[i].cts = 0;
1502 if (!port->tty || !port->tty->termios)
1504 port->normal_termios.c_cflag;
1507 port->tty->termios->c_cflag;
1509 status = inb(port->ioaddr + UART_MSR);
1510 if (status & 0x80 /*UART_MSR_DCD */ )
1511 GMStatus[i].dcd = 1;
1513 GMStatus[i].dcd = 0;
1515 if (status & 0x20 /*UART_MSR_DSR */ )
1516 GMStatus[i].dsr = 1;
1518 GMStatus[i].dsr = 0;
1521 if (status & 0x10 /*UART_MSR_CTS */ )
1522 GMStatus[i].cts = 1;
1524 GMStatus[i].cts = 0;
1526 if (copy_to_user(argp, GMStatus,
1527 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1530 case MOXA_ASPP_MON_EXT: {
1531 int status, p, shiftbit;
1532 unsigned long opmode;
1533 unsigned cflag, iflag;
1535 for (i = 0; i < MXSER_BOARDS; i++)
1536 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1537 port = &mxser_boards[i].ports[j];
1541 status = mxser_get_msr(port->ioaddr, 0, i);
1542 /* mxser_check_modem_status(port, status); */
1544 if (status & UART_MSR_TERI)
1546 if (status & UART_MSR_DDSR)
1548 if (status & UART_MSR_DDCD)
1550 if (status & UART_MSR_DCTS)
1553 port->mon_data.modem_status = status;
1554 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1555 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1556 mon_data_ext.up_rxcnt[i] =
1557 port->mon_data.up_rxcnt;
1558 mon_data_ext.up_txcnt[i] =
1559 port->mon_data.up_txcnt;
1560 mon_data_ext.modem_status[i] =
1561 port->mon_data.modem_status;
1562 mon_data_ext.baudrate[i] = port->realbaud;
1564 if (!port->tty || !port->tty->termios) {
1565 cflag = port->normal_termios.c_cflag;
1566 iflag = port->normal_termios.c_iflag;
1568 cflag = port->tty->termios->c_cflag;
1569 iflag = port->tty->termios->c_iflag;
1572 mon_data_ext.databits[i] = cflag & CSIZE;
1574 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1576 mon_data_ext.parity[i] =
1577 cflag & (PARENB | PARODD | CMSPAR);
1579 mon_data_ext.flowctrl[i] = 0x00;
1581 if (cflag & CRTSCTS)
1582 mon_data_ext.flowctrl[i] |= 0x03;
1584 if (iflag & (IXON | IXOFF))
1585 mon_data_ext.flowctrl[i] |= 0x0C;
1587 if (port->type == PORT_16550A)
1588 mon_data_ext.fifo[i] = 1;
1590 mon_data_ext.fifo[i] = 0;
1594 opmode = inb(port->opmode_ioaddr) >> shiftbit;
1595 opmode &= OP_MODE_MASK;
1597 mon_data_ext.iftype[i] = opmode;
1600 if (copy_to_user(argp, &mon_data_ext,
1601 sizeof(mon_data_ext)))
1607 return -ENOIOCTLCMD;
1612 static void mxser_stoprx(struct tty_struct *tty)
1614 struct mxser_port *info = tty->driver_data;
1616 info->ldisc_stop_rx = 1;
1618 /* following add by Victor Yu. 09-02-2002 */
1619 if (info->board->chip_flag) {
1620 info->IER &= ~MOXA_MUST_RECV_ISR;
1621 outb(info->IER, info->ioaddr + UART_IER);
1622 } else if (!(info->flags & ASYNC_CLOSING)) {
1623 info->x_char = STOP_CHAR(tty);
1624 outb(0, info->ioaddr + UART_IER);
1625 info->IER |= UART_IER_THRI;
1626 outb(info->IER, info->ioaddr + UART_IER);
1630 if (info->tty->termios->c_cflag & CRTSCTS) {
1631 info->MCR &= ~UART_MCR_RTS;
1632 outb(info->MCR, info->ioaddr + UART_MCR);
1636 static void mxser_startrx(struct tty_struct *tty)
1638 struct mxser_port *info = tty->driver_data;
1640 info->ldisc_stop_rx = 0;
1645 /* following add by Victor Yu. 09-02-2002 */
1646 if (info->board->chip_flag) {
1647 info->IER |= MOXA_MUST_RECV_ISR;
1648 outb(info->IER, info->ioaddr + UART_IER);
1649 } else if (!(info->flags & ASYNC_CLOSING)) {
1650 info->x_char = START_CHAR(tty);
1651 outb(0, info->ioaddr + UART_IER);
1652 info->IER |= UART_IER_THRI;
1653 outb(info->IER, info->ioaddr + UART_IER);
1658 if (info->tty->termios->c_cflag & CRTSCTS) {
1659 info->MCR |= UART_MCR_RTS;
1660 outb(info->MCR, info->ioaddr + UART_MCR);
1665 * This routine is called by the upper-layer tty layer to signal that
1666 * incoming characters should be throttled.
1668 static void mxser_throttle(struct tty_struct *tty)
1673 static void mxser_unthrottle(struct tty_struct *tty)
1678 static void mxser_set_termios(struct tty_struct *tty, struct termios *old_termios)
1680 struct mxser_port *info = tty->driver_data;
1681 unsigned long flags;
1683 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1684 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
1686 mxser_change_speed(info, old_termios);
1688 if ((old_termios->c_cflag & CRTSCTS) &&
1689 !(tty->termios->c_cflag & CRTSCTS)) {
1690 tty->hw_stopped = 0;
1695 /* Handle sw stopped */
1696 if ((old_termios->c_iflag & IXON) &&
1697 !(tty->termios->c_iflag & IXON)) {
1700 /* following add by Victor Yu. 09-02-2002 */
1701 if (info->board->chip_flag) {
1702 spin_lock_irqsave(&info->slock, flags);
1703 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
1704 spin_unlock_irqrestore(&info->slock, flags);
1706 /* above add by Victor Yu. 09-02-2002 */
1713 * mxser_stop() and mxser_start()
1715 * This routines are called before setting or resetting tty->stopped.
1716 * They enable or disable transmitter interrupts, as necessary.
1718 static void mxser_stop(struct tty_struct *tty)
1720 struct mxser_port *info = tty->driver_data;
1721 unsigned long flags;
1723 spin_lock_irqsave(&info->slock, flags);
1724 if (info->IER & UART_IER_THRI) {
1725 info->IER &= ~UART_IER_THRI;
1726 outb(info->IER, info->ioaddr + UART_IER);
1728 spin_unlock_irqrestore(&info->slock, flags);
1731 static void mxser_start(struct tty_struct *tty)
1733 struct mxser_port *info = tty->driver_data;
1734 unsigned long flags;
1736 spin_lock_irqsave(&info->slock, flags);
1737 if (info->xmit_cnt && info->xmit_buf
1738 /* && !(info->IER & UART_IER_THRI) */) {
1739 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
1740 info->IER |= UART_IER_THRI;
1741 outb(info->IER, info->ioaddr + UART_IER);
1743 spin_unlock_irqrestore(&info->slock, flags);
1747 * mxser_wait_until_sent() --- wait until the transmitter is empty
1749 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1751 struct mxser_port *info = tty->driver_data;
1752 unsigned long orig_jiffies, char_time;
1755 if (info->type == PORT_UNKNOWN)
1758 if (info->xmit_fifo_size == 0)
1759 return; /* Just in case.... */
1761 orig_jiffies = jiffies;
1763 * Set the check interval to be 1/5 of the estimated time to
1764 * send a single character, and make it at least 1. The check
1765 * interval should also be less than the timeout.
1767 * Note: we have to use pretty tight timings here to satisfy
1770 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1771 char_time = char_time / 5;
1774 if (timeout && timeout < char_time)
1775 char_time = timeout;
1777 * If the transmitter hasn't cleared in twice the approximate
1778 * amount of time to send the entire FIFO, it probably won't
1779 * ever clear. This assumes the UART isn't doing flow
1780 * control, which is currently the case. Hence, if it ever
1781 * takes longer than info->timeout, this is probably due to a
1782 * UART bug of some kind. So, we clamp the timeout parameter at
1785 if (!timeout || timeout > 2 * info->timeout)
1786 timeout = 2 * info->timeout;
1787 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1788 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
1789 timeout, char_time);
1790 printk("jiff=%lu...", jiffies);
1792 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
1793 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1794 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1796 schedule_timeout_interruptible(char_time);
1797 if (signal_pending(current))
1799 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1802 set_current_state(TASK_RUNNING);
1804 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1805 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1811 * This routine is called by tty_hangup() when a hangup is signaled.
1813 void mxser_hangup(struct tty_struct *tty)
1815 struct mxser_port *info = tty->driver_data;
1817 mxser_flush_buffer(tty);
1818 mxser_shutdown(info);
1821 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1823 wake_up_interruptible(&info->open_wait);
1827 /* added by James 03-12-2004. */
1829 * mxser_rs_break() --- routine which turns the break handling on or off
1831 static void mxser_rs_break(struct tty_struct *tty, int break_state)
1833 struct mxser_port *info = tty->driver_data;
1834 unsigned long flags;
1836 spin_lock_irqsave(&info->slock, flags);
1837 if (break_state == -1)
1838 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1839 info->ioaddr + UART_LCR);
1841 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1842 info->ioaddr + UART_LCR);
1843 spin_unlock_irqrestore(&info->slock, flags);
1846 /* (above) added by James. */
1850 * This is the serial driver's generic interrupt routine
1852 static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1855 struct mxser_board *brd = NULL;
1856 struct mxser_port *port;
1857 int max, irqbits, bits, msr;
1858 int pass_counter = 0;
1859 unsigned int int_cnt;
1860 int handled = IRQ_NONE;
1862 /* spin_lock(&gm_lock); */
1864 for (i = 0; i < MXSER_BOARDS; i++)
1865 if (dev_id == &mxser_boards[i]) {
1870 if (i == MXSER_BOARDS)
1874 max = mxser_numports[brd->board_type - 1];
1876 irqbits = inb(brd->vector) & brd->vector_mask;
1877 if (irqbits == brd->vector_mask)
1880 handled = IRQ_HANDLED;
1881 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
1882 if (irqbits == brd->vector_mask)
1886 port = &brd->ports[i];
1890 /* following add by Victor Yu. 09-13-2002 */
1891 iir = inb(port->ioaddr + UART_IIR);
1892 if (iir & UART_IIR_NO_INT)
1894 iir &= MOXA_MUST_IIR_MASK;
1896 status = inb(port->ioaddr + UART_LSR);
1897 outb(0x27, port->ioaddr + UART_FCR);
1898 inb(port->ioaddr + UART_MSR);
1901 /* above add by Victor Yu. 09-13-2002 */
1903 /* following add by Victor Yu. 09-02-2002 */
1904 status = inb(port->ioaddr + UART_LSR);
1906 if (status & UART_LSR_PE)
1907 port->err_shadow |= NPPI_NOTIFY_PARITY;
1908 if (status & UART_LSR_FE)
1909 port->err_shadow |= NPPI_NOTIFY_FRAMING;
1910 if (status & UART_LSR_OE)
1912 NPPI_NOTIFY_HW_OVERRUN;
1913 if (status & UART_LSR_BI)
1914 port->err_shadow |= NPPI_NOTIFY_BREAK;
1916 if (port->board->chip_flag) {
1918 if ( (status & 0x02) && !(status & 0x01) ) {
1919 outb(port->ioaddr+UART_FCR, 0x23);
1923 if (iir == MOXA_MUST_IIR_GDA ||
1924 iir == MOXA_MUST_IIR_RDA ||
1925 iir == MOXA_MUST_IIR_RTO ||
1926 iir == MOXA_MUST_IIR_LSR)
1927 mxser_receive_chars(port,
1931 /* above add by Victor Yu. 09-02-2002 */
1933 status &= port->read_status_mask;
1934 if (status & UART_LSR_DR)
1935 mxser_receive_chars(port,
1938 msr = inb(port->ioaddr + UART_MSR);
1939 if (msr & UART_MSR_ANY_DELTA)
1940 mxser_check_modem_status(port, msr);
1942 /* following add by Victor Yu. 09-13-2002 */
1943 if (port->board->chip_flag) {
1944 if (iir == 0x02 && (status &
1946 mxser_transmit_chars(port);
1948 /* above add by Victor Yu. 09-13-2002 */
1950 if (status & UART_LSR_THRE)
1951 mxser_transmit_chars(port);
1953 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
1955 if (pass_counter++ > MXSER_ISR_PASS_LIMIT)
1956 break; /* Prevent infinite loops */
1960 /* spin_unlock(&gm_lock); */
1964 static void mxser_receive_chars(struct mxser_port *port, int *status)
1966 struct tty_struct *tty = port->tty;
1967 unsigned char ch, gdl;
1972 unsigned long flags;
1974 spin_lock_irqsave(&port->slock, flags);
1976 recv_room = tty->receive_room;
1977 if ((recv_room == 0) && (!port->ldisc_stop_rx)) {
1978 /* mxser_throttle(tty); */
1983 /* following add by Victor Yu. 09-02-2002 */
1984 if (port->board->chip_flag != MOXA_OTHER_UART) {
1986 if (*status & UART_LSR_SPECIAL)
1988 /* following add by Victor Yu. 02-11-2004 */
1989 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
1990 (*status & MOXA_MUST_LSR_RERR))
1992 /* above add by Victor Yu. 02-14-2004 */
1993 if (*status & MOXA_MUST_LSR_RERR)
1996 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
1998 /* add by Victor Yu. 02-11-2004 */
1999 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
2000 gdl &= MOXA_MUST_GDL_MASK;
2001 if (gdl >= recv_room) {
2002 if (!port->ldisc_stop_rx) {
2003 /* mxser_throttle(tty); */
2009 ch = inb(port->ioaddr + UART_RX);
2010 tty_insert_flip_char(tty, ch, 0);
2016 /* above add by Victor Yu. 09-02-2002 */
2022 ch = inb(port->ioaddr + UART_RX);
2023 /* following add by Victor Yu. 09-02-2002 */
2024 if (port->board->chip_flag && (*status & UART_LSR_OE)
2025 /*&& !(*status&UART_LSR_DR) */)
2026 outb(0x23, port->ioaddr + UART_FCR);
2027 *status &= port->read_status_mask;
2028 /* above add by Victor Yu. 09-02-2002 */
2029 if (*status & port->ignore_status_mask) {
2030 if (++ignored > 100)
2034 if (*status & UART_LSR_SPECIAL) {
2035 if (*status & UART_LSR_BI) {
2037 /* added by casper 1/11/2000 */
2040 if (port->flags & ASYNC_SAK)
2042 } else if (*status & UART_LSR_PE) {
2044 /* added by casper 1/11/2000 */
2045 port->icount.parity++;
2046 } else if (*status & UART_LSR_FE) {
2048 /* added by casper 1/11/2000 */
2049 port->icount.frame++;
2050 } else if (*status & UART_LSR_OE) {
2052 /* added by casper 1/11/2000 */
2053 port->icount.overrun++;
2058 tty_insert_flip_char(tty, ch, flag);
2060 if (cnt >= recv_room) {
2061 if (!port->ldisc_stop_rx) {
2062 /* mxser_throttle(tty); */
2070 /* following add by Victor Yu. 09-02-2002 */
2071 if (port->board->chip_flag)
2074 /* mask by Victor Yu. 09-02-2002
2075 *status = inb(port->ioaddr + UART_LSR) & port->read_status_mask;
2077 /* following add by Victor Yu. 09-02-2002 */
2078 *status = inb(port->ioaddr + UART_LSR);
2079 /* above add by Victor Yu. 09-02-2002 */
2080 } while (*status & UART_LSR_DR);
2082 end_intr: /* add by Victor Yu. 09-02-2002 */
2083 mxvar_log.rxcnt[port->tty->index] += cnt;
2084 port->mon_data.rxcnt += cnt;
2085 port->mon_data.up_rxcnt += cnt;
2086 spin_unlock_irqrestore(&port->slock, flags);
2088 tty_flip_buffer_push(tty);
2091 static void mxser_transmit_chars(struct mxser_port *port)
2094 unsigned long flags;
2096 spin_lock_irqsave(&port->slock, flags);
2099 outb(port->x_char, port->ioaddr + UART_TX);
2101 mxvar_log.txcnt[port->tty->index]++;
2102 port->mon_data.txcnt++;
2103 port->mon_data.up_txcnt++;
2105 /* added by casper 1/11/2000 */
2110 if (port->xmit_buf == 0)
2113 if (port->xmit_cnt == 0) {
2114 if (port->xmit_cnt < WAKEUP_CHARS) { /* XXX what's this for?? */
2115 set_bit(MXSER_EVENT_TXLOW, &port->event);
2116 schedule_work(&port->tqueue);
2120 if (port->tty->stopped || (port->tty->hw_stopped &&
2121 (port->type != PORT_16550A) &&
2122 (!port->board->chip_flag))) {
2123 port->IER &= ~UART_IER_THRI;
2124 outb(port->IER, port->ioaddr + UART_IER);
2128 cnt = port->xmit_cnt;
2129 count = port->xmit_fifo_size;
2131 outb(port->xmit_buf[port->xmit_tail++],
2132 port->ioaddr + UART_TX);
2133 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2134 if (--port->xmit_cnt <= 0)
2136 } while (--count > 0);
2137 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
2139 /* added by James 03-12-2004. */
2140 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2141 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2143 /* added by casper 1/11/2000 */
2144 port->icount.tx += (cnt - port->xmit_cnt);
2146 if (port->xmit_cnt < WAKEUP_CHARS) {
2147 set_bit(MXSER_EVENT_TXLOW, &port->event);
2148 schedule_work(&port->tqueue);
2150 if (port->xmit_cnt <= 0) {
2151 port->IER &= ~UART_IER_THRI;
2152 outb(port->IER, port->ioaddr + UART_IER);
2155 spin_unlock_irqrestore(&port->slock, flags);
2158 static void mxser_check_modem_status(struct mxser_port *port, int status)
2160 /* update input line counters */
2161 if (status & UART_MSR_TERI)
2163 if (status & UART_MSR_DDSR)
2165 if (status & UART_MSR_DDCD)
2167 if (status & UART_MSR_DCTS)
2169 port->mon_data.modem_status = status;
2170 wake_up_interruptible(&port->delta_msr_wait);
2172 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
2173 if (status & UART_MSR_DCD)
2174 wake_up_interruptible(&port->open_wait);
2175 schedule_work(&port->tqueue);
2178 if (port->flags & ASYNC_CTS_FLOW) {
2179 if (port->tty->hw_stopped) {
2180 if (status & UART_MSR_CTS) {
2181 port->tty->hw_stopped = 0;
2183 if ((port->type != PORT_16550A) &&
2184 (!port->board->chip_flag)) {
2185 outb(port->IER & ~UART_IER_THRI,
2186 port->ioaddr + UART_IER);
2187 port->IER |= UART_IER_THRI;
2188 outb(port->IER, port->ioaddr +
2191 set_bit(MXSER_EVENT_TXLOW, &port->event);
2192 schedule_work(&port->tqueue);
2195 if (!(status & UART_MSR_CTS)) {
2196 port->tty->hw_stopped = 1;
2197 if (port->type != PORT_16550A &&
2198 !port->board->chip_flag) {
2199 port->IER &= ~UART_IER_THRI;
2200 outb(port->IER, port->ioaddr +
2208 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp, struct mxser_port *port)
2210 DECLARE_WAITQUEUE(wait, current);
2213 unsigned long flags;
2216 * If non-blocking mode is set, or the port is not enabled,
2217 * then make the check up front and then exit.
2219 if ((filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR))) {
2220 port->flags |= ASYNC_NORMAL_ACTIVE;
2224 if (tty->termios->c_cflag & CLOCAL)
2228 * Block waiting for the carrier detect and the line to become
2229 * free (i.e., not in use by the callout). While we are in
2230 * this loop, port->count is dropped by one, so that
2231 * mxser_close() knows when to free things. We restore it upon
2232 * exit, either normal or abnormal.
2235 add_wait_queue(&port->open_wait, &wait);
2237 spin_lock_irqsave(&port->slock, flags);
2238 if (!tty_hung_up_p(filp))
2240 spin_unlock_irqrestore(&port->slock, flags);
2241 port->blocked_open++;
2243 spin_lock_irqsave(&port->slock, flags);
2244 outb(inb(port->ioaddr + UART_MCR) |
2245 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
2246 spin_unlock_irqrestore(&port->slock, flags);
2247 set_current_state(TASK_INTERRUPTIBLE);
2248 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
2249 if (port->flags & ASYNC_HUP_NOTIFY)
2252 retval = -ERESTARTSYS;
2255 if (!(port->flags & ASYNC_CLOSING) &&
2257 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
2259 if (signal_pending(current)) {
2260 retval = -ERESTARTSYS;
2265 set_current_state(TASK_RUNNING);
2266 remove_wait_queue(&port->open_wait, &wait);
2267 if (!tty_hung_up_p(filp))
2269 port->blocked_open--;
2272 port->flags |= ASYNC_NORMAL_ACTIVE;
2276 static int mxser_startup(struct mxser_port *info)
2279 unsigned long flags;
2281 page = __get_free_page(GFP_KERNEL);
2285 spin_lock_irqsave(&info->slock, flags);
2287 if (info->flags & ASYNC_INITIALIZED) {
2289 spin_unlock_irqrestore(&info->slock, flags);
2293 if (!info->ioaddr || !info->type) {
2295 set_bit(TTY_IO_ERROR, &info->tty->flags);
2297 spin_unlock_irqrestore(&info->slock, flags);
2303 info->xmit_buf = (unsigned char *) page;
2306 * Clear the FIFO buffers and disable them
2307 * (they will be reenabled in mxser_change_speed())
2309 if (info->board->chip_flag)
2310 outb((UART_FCR_CLEAR_RCVR |
2311 UART_FCR_CLEAR_XMIT |
2312 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
2314 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
2315 info->ioaddr + UART_FCR);
2318 * At this point there's no way the LSR could still be 0xFF;
2319 * if it is, then bail out, because there's likely no UART
2322 if (inb(info->ioaddr + UART_LSR) == 0xff) {
2323 spin_unlock_irqrestore(&info->slock, flags);
2324 if (capable(CAP_SYS_ADMIN)) {
2326 set_bit(TTY_IO_ERROR, &info->tty->flags);
2333 * Clear the interrupt registers.
2335 (void) inb(info->ioaddr + UART_LSR);
2336 (void) inb(info->ioaddr + UART_RX);
2337 (void) inb(info->ioaddr + UART_IIR);
2338 (void) inb(info->ioaddr + UART_MSR);
2341 * Now, initialize the UART
2343 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
2344 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
2345 outb(info->MCR, info->ioaddr + UART_MCR);
2348 * Finally, enable interrupts
2350 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
2351 /* info->IER = UART_IER_RLSI | UART_IER_RDI; */
2353 /* following add by Victor Yu. 08-30-2002 */
2354 if (info->board->chip_flag)
2355 info->IER |= MOXA_MUST_IER_EGDAI;
2356 /* above add by Victor Yu. 08-30-2002 */
2357 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
2360 * And clear the interrupt registers again for luck.
2362 (void) inb(info->ioaddr + UART_LSR);
2363 (void) inb(info->ioaddr + UART_RX);
2364 (void) inb(info->ioaddr + UART_IIR);
2365 (void) inb(info->ioaddr + UART_MSR);
2368 clear_bit(TTY_IO_ERROR, &info->tty->flags);
2369 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
2372 * and set the speed of the serial port
2374 spin_unlock_irqrestore(&info->slock, flags);
2375 mxser_change_speed(info, NULL);
2377 info->flags |= ASYNC_INITIALIZED;
2382 * This routine will shutdown a serial port; interrupts maybe disabled, and
2383 * DTR is dropped if the hangup on close termio flag is on.
2385 static void mxser_shutdown(struct mxser_port *info)
2387 unsigned long flags;
2389 if (!(info->flags & ASYNC_INITIALIZED))
2392 spin_lock_irqsave(&info->slock, flags);
2395 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
2396 * here so the queue might never be waken up
2398 wake_up_interruptible(&info->delta_msr_wait);
2401 * Free the IRQ, if necessary
2403 if (info->xmit_buf) {
2404 free_page((unsigned long) info->xmit_buf);
2405 info->xmit_buf = NULL;
2409 outb(0x00, info->ioaddr + UART_IER);
2411 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
2412 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
2413 outb(info->MCR, info->ioaddr + UART_MCR);
2415 /* clear Rx/Tx FIFO's */
2416 /* following add by Victor Yu. 08-30-2002 */
2417 if (info->board->chip_flag)
2418 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
2419 MOXA_MUST_FCR_GDA_MODE_ENABLE,
2420 info->ioaddr + UART_FCR);
2422 /* above add by Victor Yu. 08-30-2002 */
2423 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
2424 info->ioaddr + UART_FCR);
2426 /* read data port to reset things */
2427 (void) inb(info->ioaddr + UART_RX);
2430 set_bit(TTY_IO_ERROR, &info->tty->flags);
2432 info->flags &= ~ASYNC_INITIALIZED;
2434 /* following add by Victor Yu. 09-23-2002 */
2435 if (info->board->chip_flag)
2436 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2437 /* above add by Victor Yu. 09-23-2002 */
2439 spin_unlock_irqrestore(&info->slock, flags);
2443 * This routine is called to set the UART divisor registers to match
2444 * the specified baud rate for a serial port.
2446 static int mxser_change_speed(struct mxser_port *info,
2447 struct termios *old_termios)
2449 unsigned cflag, cval, fcr;
2451 unsigned char status;
2453 unsigned long flags;
2455 if (!info->tty || !info->tty->termios)
2457 cflag = info->tty->termios->c_cflag;
2458 if (!(info->ioaddr))
2462 #define B921600 (B460800 +1)
2464 if (mxser_set_baud_method[info->tty->index] == 0) {
2465 baud = tty_get_baud_rate(info->tty);
2466 mxser_set_baud(info, baud);
2469 /* byte size and parity */
2470 switch (cflag & CSIZE) {
2485 break; /* too keep GCC shut... */
2490 cval |= UART_LCR_PARITY;
2491 if (!(cflag & PARODD))
2492 cval |= UART_LCR_EPAR;
2494 cval |= UART_LCR_SPAR;
2496 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
2497 if (info->board->chip_flag) {
2498 fcr = UART_FCR_ENABLE_FIFO;
2499 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2500 SET_MOXA_MUST_FIFO_VALUE(info);
2504 fcr = UART_FCR_ENABLE_FIFO;
2505 /* following add by Victor Yu. 08-30-2002 */
2506 if (info->board->chip_flag) {
2507 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2508 SET_MOXA_MUST_FIFO_VALUE(info);
2510 /* above add by Victor Yu. 08-30-2002 */
2511 switch (info->rx_trigger) {
2513 fcr |= UART_FCR_TRIGGER_1;
2516 fcr |= UART_FCR_TRIGGER_4;
2519 fcr |= UART_FCR_TRIGGER_8;
2522 fcr |= UART_FCR_TRIGGER_14;
2528 /* CTS flow control flag and modem status interrupts */
2529 info->IER &= ~UART_IER_MSI;
2530 info->MCR &= ~UART_MCR_AFE;
2531 if (cflag & CRTSCTS) {
2532 info->flags |= ASYNC_CTS_FLOW;
2533 info->IER |= UART_IER_MSI;
2534 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
2535 info->MCR |= UART_MCR_AFE;
2536 /* status = mxser_get_msr(info->ioaddr, 0, info->port); */
2540 status = inb(baseaddr + UART_MSR);
2541 restore_flags(flags);
2543 /* mxser_check_modem_status(info, status); */
2545 /* status = mxser_get_msr(info->ioaddr, 0, info->port); */
2546 /* MX_LOCK(&info->slock); */
2547 status = inb(info->ioaddr + UART_MSR);
2548 /* MX_UNLOCK(&info->slock); */
2549 if (info->tty->hw_stopped) {
2550 if (status & UART_MSR_CTS) {
2551 info->tty->hw_stopped = 0;
2552 if (info->type != PORT_16550A &&
2553 !info->board->chip_flag) {
2554 outb(info->IER & ~UART_IER_THRI,
2557 info->IER |= UART_IER_THRI;
2558 outb(info->IER, info->ioaddr +
2561 set_bit(MXSER_EVENT_TXLOW, &info->event);
2562 schedule_work(&info->tqueue); }
2564 if (!(status & UART_MSR_CTS)) {
2565 info->tty->hw_stopped = 1;
2566 if ((info->type != PORT_16550A) &&
2567 (!info->board->chip_flag)) {
2568 info->IER &= ~UART_IER_THRI;
2569 outb(info->IER, info->ioaddr +
2576 info->flags &= ~ASYNC_CTS_FLOW;
2578 outb(info->MCR, info->ioaddr + UART_MCR);
2579 if (cflag & CLOCAL) {
2580 info->flags &= ~ASYNC_CHECK_CD;
2582 info->flags |= ASYNC_CHECK_CD;
2583 info->IER |= UART_IER_MSI;
2585 outb(info->IER, info->ioaddr + UART_IER);
2588 * Set up parity check flag
2590 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2591 if (I_INPCK(info->tty))
2592 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2593 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
2594 info->read_status_mask |= UART_LSR_BI;
2596 info->ignore_status_mask = 0;
2598 if (I_IGNBRK(info->tty)) {
2599 info->ignore_status_mask |= UART_LSR_BI;
2600 info->read_status_mask |= UART_LSR_BI;
2602 * If we're ignore parity and break indicators, ignore
2603 * overruns too. (For real raw support).
2605 if (I_IGNPAR(info->tty)) {
2606 info->ignore_status_mask |=
2610 info->read_status_mask |=
2616 /* following add by Victor Yu. 09-02-2002 */
2617 if (info->board->chip_flag) {
2618 spin_lock_irqsave(&info->slock, flags);
2619 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
2620 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
2621 if (I_IXON(info->tty)) {
2622 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2624 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2626 if (I_IXOFF(info->tty)) {
2627 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2629 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2632 if ( I_IXANY(info->tty) ) {
2633 info->MCR |= MOXA_MUST_MCR_XON_ANY;
2634 ENABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
2636 info->MCR &= ~MOXA_MUST_MCR_XON_ANY;
2637 DISABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
2640 spin_unlock_irqrestore(&info->slock, flags);
2642 /* above add by Victor Yu. 09-02-2002 */
2645 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
2646 outb(cval, info->ioaddr + UART_LCR);
2652 static int mxser_set_baud(struct mxser_port *info, long newspd)
2657 unsigned long flags;
2659 if (!info->tty || !info->tty->termios)
2662 if (!(info->ioaddr))
2665 if (newspd > info->max_baud)
2668 info->realbaud = newspd;
2669 if (newspd == 134) {
2670 quot = (2 * info->baud_base / 269);
2671 } else if (newspd) {
2672 quot = info->baud_base / newspd;
2679 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
2680 info->timeout += HZ / 50; /* Add .02 seconds of slop */
2683 spin_lock_irqsave(&info->slock, flags);
2684 info->MCR |= UART_MCR_DTR;
2685 outb(info->MCR, info->ioaddr + UART_MCR);
2686 spin_unlock_irqrestore(&info->slock, flags);
2688 spin_lock_irqsave(&info->slock, flags);
2689 info->MCR &= ~UART_MCR_DTR;
2690 outb(info->MCR, info->ioaddr + UART_MCR);
2691 spin_unlock_irqrestore(&info->slock, flags);
2695 cval = inb(info->ioaddr + UART_LCR);
2697 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
2699 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
2700 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
2701 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
2708 * ------------------------------------------------------------
2709 * friends of mxser_ioctl()
2710 * ------------------------------------------------------------
2712 static int mxser_get_serial_info(struct mxser_port *info,
2713 struct serial_struct __user *retinfo)
2715 struct serial_struct tmp;
2719 memset(&tmp, 0, sizeof(tmp));
2720 tmp.type = info->type;
2721 tmp.line = info->tty->index;
2722 tmp.port = info->ioaddr;
2723 tmp.irq = info->board->irq;
2724 tmp.flags = info->flags;
2725 tmp.baud_base = info->baud_base;
2726 tmp.close_delay = info->close_delay;
2727 tmp.closing_wait = info->closing_wait;
2728 tmp.custom_divisor = info->custom_divisor;
2730 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2735 static int mxser_set_serial_info(struct mxser_port *info,
2736 struct serial_struct __user *new_info)
2738 struct serial_struct new_serial;
2742 if (!new_info || !info->ioaddr)
2744 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2747 if ((new_serial.irq != info->board->irq) ||
2748 (new_serial.port != info->ioaddr) ||
2749 (new_serial.custom_divisor != info->custom_divisor) ||
2750 (new_serial.baud_base != info->baud_base))
2753 flags = info->flags & ASYNC_SPD_MASK;
2755 if (!capable(CAP_SYS_ADMIN)) {
2756 if ((new_serial.baud_base != info->baud_base) ||
2757 (new_serial.close_delay != info->close_delay) ||
2758 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
2760 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
2761 (new_serial.flags & ASYNC_USR_MASK));
2764 * OK, past this point, all the error checking has been done.
2765 * At this point, we start making changes.....
2767 info->flags = ((info->flags & ~ASYNC_FLAGS) |
2768 (new_serial.flags & ASYNC_FLAGS));
2769 info->close_delay = new_serial.close_delay * HZ / 100;
2770 info->closing_wait = new_serial.closing_wait * HZ / 100;
2771 info->tty->low_latency =
2772 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2773 info->tty->low_latency = 0; /* (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0; */
2776 /* added by casper, 3/17/2000, for mouse */
2777 info->type = new_serial.type;
2779 process_txrx_fifo(info);
2781 if (info->flags & ASYNC_INITIALIZED) {
2782 if (flags != (info->flags & ASYNC_SPD_MASK))
2783 mxser_change_speed(info, NULL);
2785 retval = mxser_startup(info);
2791 * mxser_get_lsr_info - get line status register info
2793 * Purpose: Let user call ioctl() to get info when the UART physically
2794 * is emptied. On bus types like RS485, the transmitter must
2795 * release the bus after transmitting. This must be done when
2796 * the transmit shift register is empty, not be done when the
2797 * transmit holding register is empty. This functionality
2798 * allows an RS485 driver to be written in user space.
2800 static int mxser_get_lsr_info(struct mxser_port *info,
2801 unsigned int __user *value)
2803 unsigned char status;
2804 unsigned int result;
2805 unsigned long flags;
2807 spin_lock_irqsave(&info->slock, flags);
2808 status = inb(info->ioaddr + UART_LSR);
2809 spin_unlock_irqrestore(&info->slock, flags);
2810 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
2811 return put_user(result, value);
2815 * This routine sends a break character out the serial port.
2817 static void mxser_send_break(struct mxser_port *info, int duration)
2819 unsigned long flags;
2823 set_current_state(TASK_INTERRUPTIBLE);
2824 spin_lock_irqsave(&info->slock, flags);
2825 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2826 info->ioaddr + UART_LCR);
2827 spin_unlock_irqrestore(&info->slock, flags);
2828 schedule_timeout(duration);
2829 spin_lock_irqsave(&info->slock, flags);
2830 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2831 info->ioaddr + UART_LCR);
2832 spin_unlock_irqrestore(&info->slock, flags);
2835 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
2837 struct mxser_port *info = tty->driver_data;
2838 unsigned char control, status;
2839 unsigned long flags;
2842 if (tty->index == MXSER_PORTS)
2843 return -ENOIOCTLCMD;
2844 if (tty->flags & (1 << TTY_IO_ERROR))
2847 control = info->MCR;
2849 spin_lock_irqsave(&info->slock, flags);
2850 status = inb(info->ioaddr + UART_MSR);
2851 if (status & UART_MSR_ANY_DELTA)
2852 mxser_check_modem_status(info, status);
2853 spin_unlock_irqrestore(&info->slock, flags);
2854 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
2855 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
2856 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
2857 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
2858 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
2859 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
2862 static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
2863 unsigned int set, unsigned int clear)
2865 struct mxser_port *info = tty->driver_data;
2866 unsigned long flags;
2869 if (tty->index == MXSER_PORTS)
2870 return -ENOIOCTLCMD;
2871 if (tty->flags & (1 << TTY_IO_ERROR))
2874 spin_lock_irqsave(&info->slock, flags);
2876 if (set & TIOCM_RTS)
2877 info->MCR |= UART_MCR_RTS;
2878 if (set & TIOCM_DTR)
2879 info->MCR |= UART_MCR_DTR;
2881 if (clear & TIOCM_RTS)
2882 info->MCR &= ~UART_MCR_RTS;
2883 if (clear & TIOCM_DTR)
2884 info->MCR &= ~UART_MCR_DTR;
2886 outb(info->MCR, info->ioaddr + UART_MCR);
2887 spin_unlock_irqrestore(&info->slock, flags);
2892 static int mxser_read_register(int, unsigned short *);
2893 static int mxser_program_mode(int);
2894 static void mxser_normal_mode(int);
2896 static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
2899 unsigned short regs[16], irq;
2900 unsigned char scratch, scratch2;
2902 brd->chip_flag = MOXA_OTHER_UART;
2904 id = mxser_read_register(cap, regs);
2905 if (id == C168_ASIC_ID) {
2906 brd->board_type = MXSER_BOARD_C168_ISA;
2908 } else if (id == C104_ASIC_ID) {
2909 brd->board_type = MXSER_BOARD_C104_ISA;
2911 } else if (id == C102_ASIC_ID) {
2912 brd->board_type = MXSER_BOARD_C102_ISA;
2914 } else if (id == CI132_ASIC_ID) {
2915 brd->board_type = MXSER_BOARD_CI132;
2917 } else if (id == CI134_ASIC_ID) {
2918 brd->board_type = MXSER_BOARD_CI134;
2920 } else if (id == CI104J_ASIC_ID) {
2921 brd->board_type = MXSER_BOARD_CI104J;
2927 if (brd->nports == 2) {
2928 irq = regs[9] & 0xF000;
2929 irq = irq | (irq >> 4);
2930 if (irq != (regs[9] & 0xFF00))
2931 return MXSER_ERR_IRQ_CONFLIT;
2932 } else if (brd->nports == 4) {
2933 irq = regs[9] & 0xF000;
2934 irq = irq | (irq >> 4);
2935 irq = irq | (irq >> 8);
2937 return MXSER_ERR_IRQ_CONFLIT;
2938 } else if (brd->nports == 8) {
2939 irq = regs[9] & 0xF000;
2940 irq = irq | (irq >> 4);
2941 irq = irq | (irq >> 8);
2942 if ((irq != regs[9]) || (irq != regs[10]))
2943 return MXSER_ERR_IRQ_CONFLIT;
2947 return MXSER_ERR_IRQ;
2948 brd->irq = ((int)(irq & 0xF000) >> 12);
2949 for (i = 0; i < 8; i++)
2950 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
2951 if ((regs[12] & 0x80) == 0)
2952 return MXSER_ERR_VECTOR;
2953 brd->vector = (int)regs[11]; /* interrupt vector */
2955 brd->vector_mask = 0x00FF;
2957 brd->vector_mask = 0x000F;
2958 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2959 if (regs[12] & bits) {
2960 brd->ports[i].baud_base = 921600;
2961 brd->ports[i].max_baud = 921600; /* add by Victor Yu. 09-04-2002 */
2963 brd->ports[i].baud_base = 115200;
2964 brd->ports[i].max_baud = 115200; /* add by Victor Yu. 09-04-2002 */
2967 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2968 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2969 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2970 outb(scratch2, cap + UART_LCR);
2971 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2972 scratch = inb(cap + UART_IIR);
2975 brd->uart_type = PORT_16550A;
2977 brd->uart_type = PORT_16450;
2982 if (!request_region(brd->ports[0].ioaddr, 8 * brd->nports, "mxser(IO)"))
2983 return MXSER_ERR_IOADDR;
2984 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2985 release_region(brd->ports[0].ioaddr, 8 * brd->nports);
2986 return MXSER_ERR_VECTOR;
2991 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
2992 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
2993 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
2994 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
2995 #define EN_CCMD 0x000 /* Chip's command register */
2996 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
2997 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
2998 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
2999 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
3000 #define EN0_DCFG 0x00E /* Data configuration reg WR */
3001 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
3002 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
3003 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
3004 static int mxser_read_register(int port, unsigned short *regs)
3006 int i, k, value, id;
3009 id = mxser_program_mode(port);
3012 for (i = 0; i < 14; i++) {
3013 k = (i & 0x3F) | 0x180;
3014 for (j = 0x100; j > 0; j >>= 1) {
3015 outb(CHIP_CS, port);
3017 outb(CHIP_CS | CHIP_DO, port);
3018 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
3020 outb(CHIP_CS, port);
3021 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
3026 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
3027 outb(CHIP_CS, port);
3028 outb(CHIP_CS | CHIP_SK, port);
3029 if (inb(port) & CHIP_DI)
3035 mxser_normal_mode(port);
3039 static int mxser_program_mode(int port)
3042 /* unsigned long flags; */
3044 spin_lock(&gm_lock);
3052 /* restore_flags(flags); */
3053 spin_unlock(&gm_lock);
3055 id = inb(port + 1) & 0x1F;
3056 if ((id != C168_ASIC_ID) &&
3057 (id != C104_ASIC_ID) &&
3058 (id != C102_ASIC_ID) &&
3059 (id != CI132_ASIC_ID) &&
3060 (id != CI134_ASIC_ID) &&
3061 (id != CI104J_ASIC_ID))
3063 for (i = 0, j = 0; i < 4; i++) {
3067 } else if ((j == 1) && (n == 1)) {
3078 static void mxser_normal_mode(int port)
3082 outb(0xA5, port + 1);
3083 outb(0x80, port + 3);
3084 outb(12, port + 0); /* 9600 bps */
3086 outb(0x03, port + 3); /* 8 data bits */
3087 outb(0x13, port + 4); /* loop back mode */
3088 for (i = 0; i < 16; i++) {
3090 if ((n & 0x61) == 0x60)
3095 outb(0x00, port + 4);
3098 module_init(mxser_module_init);
3099 module_exit(mxser_module_exit);