2 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
4 * Copyright (C) 1999-2001 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 * Original release 10/26/00
25 * 02/06/01 Support MOXA Industio family boards.
26 * 02/06/01 Support TIOCGICOUNT.
27 * 02/06/01 Fix the problem for connecting to serial mouse.
28 * 02/06/01 Fix the problem for H/W flow control.
29 * 02/06/01 Fix the compling warning when CONFIG_PCI
32 * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
33 * <alan@redhat.com>. The original 1.8 code is available on www.moxa.com.
34 * - Fixed x86_64 cleanness
35 * - Fixed sleep with spinlock held in mxser_send_break
39 #include <linux/config.h>
40 #include <linux/module.h>
41 #include <linux/version.h>
42 #include <linux/autoconf.h>
43 #include <linux/errno.h>
44 #include <linux/signal.h>
45 #include <linux/sched.h>
46 #include <linux/timer.h>
47 #include <linux/interrupt.h>
48 #include <linux/tty.h>
49 #include <linux/tty_flip.h>
50 #include <linux/serial.h>
51 #include <linux/serial_reg.h>
52 #include <linux/major.h>
53 #include <linux/string.h>
54 #include <linux/fcntl.h>
55 #include <linux/ptrace.h>
56 #include <linux/gfp.h>
57 #include <linux/ioport.h>
59 #include <linux/smp_lock.h>
60 #include <linux/delay.h>
61 #include <linux/pci.h>
63 #include <asm/system.h>
66 #include <asm/bitops.h>
67 #include <asm/uaccess.h>
71 #define MXSER_VERSION "1.8"
72 #define MXSERMAJOR 174
73 #define MXSERCUMAJOR 175
75 #define MXSER_EVENT_TXLOW 1
76 #define MXSER_EVENT_HANGUP 2
78 #define MXSER_BOARDS 4 /* Max. boards */
79 #define MXSER_PORTS 32 /* Max. ports */
80 #define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
81 #define MXSER_ISR_PASS_LIMIT 256
83 #define MXSER_ERR_IOADDR -1
84 #define MXSER_ERR_IRQ -2
85 #define MXSER_ERR_IRQ_CONFLIT -3
86 #define MXSER_ERR_VECTOR -4
88 #define SERIAL_TYPE_NORMAL 1
89 #define SERIAL_TYPE_CALLOUT 2
91 #define WAKEUP_CHARS 256
93 #define UART_MCR_AFE 0x20
94 #define UART_LSR_SPECIAL 0x1E
96 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|IXON|IXOFF))
98 #define IRQ_T(info) ((info->flags & ASYNC_SHARE_IRQ) ? SA_SHIRQ : SA_INTERRUPT)
100 #define C168_ASIC_ID 1
101 #define C104_ASIC_ID 2
102 #define C102_ASIC_ID 0xB
103 #define CI132_ASIC_ID 4
104 #define CI134_ASIC_ID 3
105 #define CI104J_ASIC_ID 5
108 MXSER_BOARD_C168_ISA = 1,
109 MXSER_BOARD_C104_ISA,
111 MXSER_BOARD_C168_PCI,
112 MXSER_BOARD_C104_PCI,
113 MXSER_BOARD_C102_ISA,
131 static char *mxser_brdname[] = {
149 "Moxa UC7000 Serial",
155 static int mxser_numports[] = {
179 #define UART_TYPE_NUM 2
181 static const unsigned int Gmoxa_uart_id[UART_TYPE_NUM] = {
182 MOXA_MUST_MU150_HWID,
186 // This is only for PCI
187 #define UART_INFO_NUM 3
188 struct mxpciuart_info {
199 static const struct mxpciuart_info Gpci_uart_info[UART_INFO_NUM] = {
200 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
201 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
202 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
208 static struct pci_device_id mxser_pcibrds[] = {
209 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C168, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_C168_PCI},
210 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C104, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_C104_PCI},
211 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP132},
212 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP114, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP114},
213 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CT114, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CT114},
214 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP102},
215 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP104U},
216 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP168U},
217 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP132U},
218 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP134U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP134U},
219 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104JU, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP104JU},
220 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_RC7000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_RC7000},
221 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP118U},
222 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102UL, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP102UL},
223 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP102U},
227 MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
232 typedef struct _moxa_pci_info {
233 unsigned short busNum;
234 unsigned short devNum;
235 struct pci_dev *pdev; // add by Victor Yu. 06-23-2003
238 static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
239 static int ttymajor = MXSERMAJOR;
240 static int calloutmajor = MXSERCUMAJOR;
241 static int verbose = 0;
243 /* Variables for insmod */
245 MODULE_AUTHOR("Casper Yang");
246 MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
247 MODULE_PARM(ioaddr, "1-4i");
248 MODULE_PARM(ttymajor, "i");
249 MODULE_PARM(calloutmajor, "i");
250 MODULE_PARM(verbose, "i");
251 MODULE_LICENSE("GPL");
255 unsigned long rxcnt[MXSER_PORTS];
256 unsigned long txcnt[MXSER_PORTS];
263 unsigned long up_rxcnt;
264 unsigned long up_txcnt;
266 unsigned char hold_reason;
269 struct mxser_mon_ext {
270 unsigned long rx_cnt[32];
271 unsigned long tx_cnt[32];
272 unsigned long up_rxcnt[32];
273 unsigned long up_txcnt[32];
274 int modem_status[32];
284 struct mxser_hwconf {
291 int ioaddr[MXSER_PORTS_PER_BOARD];
292 int baud_base[MXSER_PORTS_PER_BOARD];
293 moxa_pci_info pciInfo;
294 int IsMoxaMustChipFlag; // add by Victor Yu. 08-30-2002
295 int MaxCanSetBaudRate[MXSER_PORTS_PER_BOARD]; // add by Victor Yu. 09-04-2002
296 int opmode_ioaddr[MXSER_PORTS_PER_BOARD]; // add by Victor Yu. 01-05-2004
299 struct mxser_struct {
301 int base; /* port base address */
302 int irq; /* port using irq no. */
303 int vector; /* port irq vector */
304 int vectormask; /* port vector mask */
306 int rx_trigger; /* Rx fifo trigger level */
308 int baud_base; /* max. speed */
309 int flags; /* defined in tty.h */
310 int type; /* UART type */
311 struct tty_struct *tty;
312 int read_status_mask;
313 int ignore_status_mask;
316 int x_char; /* xon/xoff character */
318 unsigned short closing_wait;
319 int IER; /* Interrupt Enable Register */
320 int MCR; /* Modem control register */
322 int count; /* # of fd on device */
323 int blocked_open; /* # of blocked opens */
324 long session; /* Session of opening process */
325 long pgrp; /* pgrp of opening process */
326 unsigned char *xmit_buf;
330 struct work_struct tqueue;
331 struct termios normal_termios;
332 struct termios callout_termios;
333 wait_queue_head_t open_wait;
334 wait_queue_head_t close_wait;
335 wait_queue_head_t delta_msr_wait;
336 struct async_icount icount; /* kernel counters for the 4 input interrupts */
338 int IsMoxaMustChipFlag; // add by Victor Yu. 08-30-2002
339 int MaxCanSetBaudRate; // add by Victor Yu. 09-04-2002
340 int opmode_ioaddr; // add by Victor Yu. 01-05-2004
341 unsigned char stop_rx;
342 unsigned char ldisc_stop_rx;
344 struct mxser_mon mon_data;
345 unsigned char err_shadow;
350 struct mxser_mstatus {
358 static struct mxser_mstatus GMStatus[MXSER_PORTS];
360 static int mxserBoardCAP[MXSER_BOARDS] = {
362 /* 0x180, 0x280, 0x200, 0x320 */
365 static struct tty_driver *mxvar_sdriver;
366 static struct mxser_struct mxvar_table[MXSER_PORTS];
367 static struct tty_struct *mxvar_tty[MXSER_PORTS + 1];
368 static struct termios *mxvar_termios[MXSER_PORTS + 1];
369 static struct termios *mxvar_termios_locked[MXSER_PORTS + 1];
370 static struct mxser_log mxvar_log;
371 static int mxvar_diagflag;
372 static unsigned char mxser_msr[MXSER_PORTS + 1];
373 static struct mxser_mon_ext mon_data_ext;
374 static int mxser_set_baud_method[MXSER_PORTS + 1];
375 static spinlock_t gm_lock;
378 * This is used to figure out the divisor speeds and the timeouts
381 static struct mxser_hwconf mxsercfg[MXSER_BOARDS];
387 static void mxser_getcfg(int board, struct mxser_hwconf *hwconf);
388 static int mxser_init(void);
390 //static void mxser_poll(unsigned long);
391 static int mxser_get_ISA_conf(int, struct mxser_hwconf *);
392 static int mxser_get_PCI_conf(int, int, int, struct mxser_hwconf *);
393 static void mxser_do_softint(void *);
394 static int mxser_open(struct tty_struct *, struct file *);
395 static void mxser_close(struct tty_struct *, struct file *);
396 static int mxser_write(struct tty_struct *, const unsigned char *, int);
397 static int mxser_write_room(struct tty_struct *);
398 static void mxser_flush_buffer(struct tty_struct *);
399 static int mxser_chars_in_buffer(struct tty_struct *);
400 static void mxser_flush_chars(struct tty_struct *);
401 static void mxser_put_char(struct tty_struct *, unsigned char);
402 static int mxser_ioctl(struct tty_struct *, struct file *, uint, ulong);
403 static int mxser_ioctl_special(unsigned int, void __user *);
404 static void mxser_throttle(struct tty_struct *);
405 static void mxser_unthrottle(struct tty_struct *);
406 static void mxser_set_termios(struct tty_struct *, struct termios *);
407 static void mxser_stop(struct tty_struct *);
408 static void mxser_start(struct tty_struct *);
409 static void mxser_hangup(struct tty_struct *);
410 static void mxser_rs_break(struct tty_struct *, int);
411 static irqreturn_t mxser_interrupt(int, void *, struct pt_regs *);
412 static void mxser_receive_chars(struct mxser_struct *, int *);
413 static void mxser_transmit_chars(struct mxser_struct *);
414 static void mxser_check_modem_status(struct mxser_struct *, int);
415 static int mxser_block_til_ready(struct tty_struct *, struct file *, struct mxser_struct *);
416 static int mxser_startup(struct mxser_struct *);
417 static void mxser_shutdown(struct mxser_struct *);
418 static int mxser_change_speed(struct mxser_struct *, struct termios *old_termios);
419 static int mxser_get_serial_info(struct mxser_struct *, struct serial_struct __user *);
420 static int mxser_set_serial_info(struct mxser_struct *, struct serial_struct __user *);
421 static int mxser_get_lsr_info(struct mxser_struct *, unsigned int __user *);
422 static void mxser_send_break(struct mxser_struct *, int);
423 static int mxser_tiocmget(struct tty_struct *, struct file *);
424 static int mxser_tiocmset(struct tty_struct *, struct file *, unsigned int, unsigned int);
425 static int mxser_set_baud(struct mxser_struct *info, long newspd);
426 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout);
428 static void mxser_startrx(struct tty_struct *tty);
429 static void mxser_stoprx(struct tty_struct *tty);
432 static int CheckIsMoxaMust(int io)
437 outb(0, io + UART_LCR);
438 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
439 oldmcr = inb(io + UART_MCR);
440 outb(0, io + UART_MCR);
441 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
442 if ((hwid = inb(io + UART_MCR)) != 0) {
443 outb(oldmcr, io + UART_MCR);
444 return (MOXA_OTHER_UART);
447 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
448 for (i = 0; i < UART_TYPE_NUM; i++) {
449 if (hwid == Gmoxa_uart_id[i])
452 return MOXA_OTHER_UART;
455 // above is modified by Victor Yu. 08-15-2002
457 static struct tty_operations mxser_ops = {
459 .close = mxser_close,
460 .write = mxser_write,
461 .put_char = mxser_put_char,
462 .flush_chars = mxser_flush_chars,
463 .write_room = mxser_write_room,
464 .chars_in_buffer = mxser_chars_in_buffer,
465 .flush_buffer = mxser_flush_buffer,
466 .ioctl = mxser_ioctl,
467 .throttle = mxser_throttle,
468 .unthrottle = mxser_unthrottle,
469 .set_termios = mxser_set_termios,
471 .start = mxser_start,
472 .hangup = mxser_hangup,
473 .break_ctl = mxser_rs_break,
474 .wait_until_sent = mxser_wait_until_sent,
475 .tiocmget = mxser_tiocmget,
476 .tiocmset = mxser_tiocmset,
480 * The MOXA Smartio/Industio serial driver boot-time initialization code!
483 static int __init mxser_module_init(void)
488 printk(KERN_DEBUG "Loading module mxser ...\n");
491 printk(KERN_DEBUG "Done.\n");
495 static void __exit mxser_module_exit(void)
500 printk(KERN_DEBUG "Unloading module mxser ...\n");
502 err = tty_unregister_driver(mxvar_sdriver);
504 put_tty_driver(mxvar_sdriver);
506 printk(KERN_ERR "Couldn't unregister MOXA Smartio/Industio family serial driver\n");
509 for (i = 0; i < MXSER_BOARDS; i++) {
510 struct pci_dev *pdev;
512 if (mxsercfg[i].board_type == -1)
515 pdev = mxsercfg[i].pciInfo.pdev;
516 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
517 if (pdev != NULL) { //PCI
518 release_region(pci_resource_start(pdev, 2), pci_resource_len(pdev, 2));
519 release_region(pci_resource_start(pdev, 3), pci_resource_len(pdev, 3));
521 release_region(mxsercfg[i].ioaddr[0], 8 * mxsercfg[i].ports);
522 release_region(mxsercfg[i].vector, 1);
527 printk(KERN_DEBUG "Done.\n");
531 static void process_txrx_fifo(struct mxser_struct *info)
535 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
536 info->rx_trigger = 1;
537 info->rx_high_water = 1;
538 info->rx_low_water = 1;
539 info->xmit_fifo_size = 1;
541 for (i = 0; i < UART_INFO_NUM; i++) {
542 if (info->IsMoxaMustChipFlag == Gpci_uart_info[i].type) {
543 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
544 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
545 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
546 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
553 static int mxser_initbrd(int board, struct mxser_hwconf *hwconf)
555 struct mxser_struct *info;
559 n = board * MXSER_PORTS_PER_BOARD;
560 info = &mxvar_table[n];
562 printk(KERN_DEBUG " ttyM%d - ttyM%d ", n, n + hwconf->ports - 1);
563 printk(" max. baud rate = %d bps.\n", hwconf->MaxCanSetBaudRate[0]);
566 for (i = 0; i < hwconf->ports; i++, n++, info++) {
568 info->base = hwconf->ioaddr[i];
569 info->irq = hwconf->irq;
570 info->vector = hwconf->vector;
571 info->vectormask = hwconf->vector_mask;
572 info->opmode_ioaddr = hwconf->opmode_ioaddr[i]; // add by Victor Yu. 01-05-2004
574 info->ldisc_stop_rx = 0;
576 info->IsMoxaMustChipFlag = hwconf->IsMoxaMustChipFlag;
577 //Enhance mode enabled here
578 if (info->IsMoxaMustChipFlag != MOXA_OTHER_UART) {
579 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->base);
582 info->flags = ASYNC_SHARE_IRQ;
583 info->type = hwconf->uart_type;
584 info->baud_base = hwconf->baud_base[i];
586 info->MaxCanSetBaudRate = hwconf->MaxCanSetBaudRate[i];
588 process_txrx_fifo(info);
591 info->custom_divisor = hwconf->baud_base[i] * 16;
592 info->close_delay = 5 * HZ / 10;
593 info->closing_wait = 30 * HZ;
594 INIT_WORK(&info->tqueue, mxser_do_softint, info);
595 info->normal_termios = mxvar_sdriver->init_termios;
596 init_waitqueue_head(&info->open_wait);
597 init_waitqueue_head(&info->close_wait);
598 init_waitqueue_head(&info->delta_msr_wait);
599 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
600 info->err_shadow = 0;
601 spin_lock_init(&info->slock);
604 * Allocate the IRQ if necessary
608 /* before set INT ISR, disable all int */
609 for (i = 0; i < hwconf->ports; i++) {
610 outb(inb(hwconf->ioaddr[i] + UART_IER) & 0xf0, hwconf->ioaddr[i] + UART_IER);
613 n = board * MXSER_PORTS_PER_BOARD;
614 info = &mxvar_table[n];
616 retval = request_irq(hwconf->irq, mxser_interrupt, IRQ_T(info), "mxser", info);
618 printk(KERN_ERR "Board %d: %s", board, mxser_brdname[hwconf->board_type - 1]);
619 printk(" Request irq fail,IRQ (%d) may be conflit with another device.\n", info->irq);
626 static void mxser_getcfg(int board, struct mxser_hwconf *hwconf)
628 mxsercfg[board] = *hwconf;
632 static int mxser_get_PCI_conf(int busnum, int devnum, int board_type, struct mxser_hwconf *hwconf)
636 unsigned int ioaddress;
637 struct pci_dev *pdev = hwconf->pciInfo.pdev;
640 hwconf->board_type = board_type;
641 hwconf->ports = mxser_numports[board_type - 1];
642 ioaddress = pci_resource_start(pdev, 2);
643 request_region(pci_resource_start(pdev, 2), pci_resource_len(pdev, 2), "mxser(IO)");
645 for (i = 0; i < hwconf->ports; i++) {
646 hwconf->ioaddr[i] = ioaddress + 8 * i;
650 ioaddress = pci_resource_start(pdev, 3);
651 request_region(pci_resource_start(pdev, 3), pci_resource_len(pdev, 3), "mxser(vector)");
652 hwconf->vector = ioaddress;
655 hwconf->irq = hwconf->pciInfo.pdev->irq;
657 hwconf->IsMoxaMustChipFlag = CheckIsMoxaMust(hwconf->ioaddr[0]);
658 hwconf->uart_type = PORT_16550A;
659 hwconf->vector_mask = 0;
662 for (i = 0; i < hwconf->ports; i++) {
663 for (j = 0; j < UART_INFO_NUM; j++) {
664 if (Gpci_uart_info[j].type == hwconf->IsMoxaMustChipFlag) {
665 hwconf->MaxCanSetBaudRate[i] = Gpci_uart_info[j].max_baud;
667 //exception....CP-102
668 if (board_type == MXSER_BOARD_CP102)
669 hwconf->MaxCanSetBaudRate[i] = 921600;
675 if (hwconf->IsMoxaMustChipFlag == MOXA_MUST_MU860_HWID) {
676 for (i = 0; i < hwconf->ports; i++) {
678 hwconf->opmode_ioaddr[i] = ioaddress + 4;
680 hwconf->opmode_ioaddr[i] = ioaddress + 0x0c;
682 outb(0, ioaddress + 4); // default set to RS232 mode
683 outb(0, ioaddress + 0x0c); //default set to RS232 mode
686 for (i = 0; i < hwconf->ports; i++) {
687 hwconf->vector_mask |= (1 << i);
688 hwconf->baud_base[i] = 921600;
694 static int mxser_init(void)
696 int i, m, retval, b, n;
697 struct pci_dev *pdev = NULL;
699 unsigned char busnum, devnum;
700 struct mxser_hwconf hwconf;
702 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
705 spin_lock_init(&gm_lock);
707 for (i = 0; i < MXSER_BOARDS; i++) {
708 mxsercfg[i].board_type = -1;
711 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n", MXSER_VERSION);
713 /* Initialize the tty_driver structure */
714 memset(mxvar_sdriver, 0, sizeof(struct tty_driver));
715 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
716 mxvar_sdriver->name = "ttyM";
717 mxvar_sdriver->major = ttymajor;
718 mxvar_sdriver->minor_start = 0;
719 mxvar_sdriver->num = MXSER_PORTS + 1;
720 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
721 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
722 mxvar_sdriver->init_termios = tty_std_termios;
723 mxvar_sdriver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
724 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
725 tty_set_operations(mxvar_sdriver, &mxser_ops);
726 mxvar_sdriver->ttys = mxvar_tty;
727 mxvar_sdriver->termios = mxvar_termios;
728 mxvar_sdriver->termios_locked = mxvar_termios_locked;
731 memset(mxvar_table, 0, MXSER_PORTS * sizeof(struct mxser_struct));
732 memset(&mxvar_log, 0, sizeof(struct mxser_log));
734 memset(&mxser_msr, 0, sizeof(unsigned char) * (MXSER_PORTS + 1));
735 memset(&mon_data_ext, 0, sizeof(struct mxser_mon_ext));
736 memset(&mxser_set_baud_method, 0, sizeof(int) * (MXSER_PORTS + 1));
737 memset(&hwconf, 0, sizeof(struct mxser_hwconf));
740 /* Start finding ISA boards here */
741 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
743 if (!(cap = mxserBoardCAP[b]))
746 retval = mxser_get_ISA_conf(cap, &hwconf);
749 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n", mxser_brdname[hwconf.board_type - 1], ioaddr[b]);
752 if (retval == MXSER_ERR_IRQ)
753 printk(KERN_ERR "Invalid interrupt number,board not configured\n");
754 else if (retval == MXSER_ERR_IRQ_CONFLIT)
755 printk(KERN_ERR "Invalid interrupt number,board not configured\n");
756 else if (retval == MXSER_ERR_VECTOR)
757 printk(KERN_ERR "Invalid interrupt vector,board not configured\n");
758 else if (retval == MXSER_ERR_IOADDR)
759 printk(KERN_ERR "Invalid I/O address,board not configured\n");
764 hwconf.pciInfo.busNum = 0;
765 hwconf.pciInfo.devNum = 0;
766 hwconf.pciInfo.pdev = NULL;
768 mxser_getcfg(m, &hwconf);
769 //init mxsercfg first, or mxsercfg data is not correct on ISR.
770 //mxser_initbrd will hook ISR.
771 if (mxser_initbrd(m, &hwconf) < 0)
778 /* Start finding ISA boards from module arg */
779 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
781 if (!(cap = ioaddr[b]))
784 retval = mxser_get_ISA_conf(cap, &hwconf);
787 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n", mxser_brdname[hwconf.board_type - 1], ioaddr[b]);
790 if (retval == MXSER_ERR_IRQ)
791 printk(KERN_ERR "Invalid interrupt number,board not configured\n");
792 else if (retval == MXSER_ERR_IRQ_CONFLIT)
793 printk(KERN_ERR "Invalid interrupt number,board not configured\n");
794 else if (retval == MXSER_ERR_VECTOR)
795 printk(KERN_ERR "Invalid interrupt vector,board not configured\n");
796 else if (retval == MXSER_ERR_IOADDR)
797 printk(KERN_ERR "Invalid I/O address,board not configured\n");
802 hwconf.pciInfo.busNum = 0;
803 hwconf.pciInfo.devNum = 0;
804 hwconf.pciInfo.pdev = NULL;
806 mxser_getcfg(m, &hwconf);
807 //init mxsercfg first, or mxsercfg data is not correct on ISR.
808 //mxser_initbrd will hook ISR.
809 if (mxser_initbrd(m, &hwconf) < 0)
815 /* start finding PCI board here */
817 n = (sizeof(mxser_pcibrds) / sizeof(mxser_pcibrds[0])) - 1;
821 pdev = pci_find_device(mxser_pcibrds[b].vendor, mxser_pcibrds[b].device, pdev);
826 hwconf.pciInfo.busNum = busnum = pdev->bus->number;
827 hwconf.pciInfo.devNum = devnum = PCI_SLOT(pdev->devfn) << 3;
828 hwconf.pciInfo.pdev = pdev;
829 printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n", mxser_brdname[(int) (mxser_pcibrds[b].driver_data) - 1], busnum, devnum >> 3);
831 if (m >= MXSER_BOARDS) {
832 printk(KERN_ERR "Too many Smartio/Industio family boards find (maximum %d),board not configured\n", MXSER_BOARDS);
834 if (pci_enable_device(pdev)) {
835 printk(KERN_ERR "Moxa SmartI/O PCI enable fail !\n");
838 retval = mxser_get_PCI_conf(busnum, devnum, (int) mxser_pcibrds[b].driver_data, &hwconf);
840 if (retval == MXSER_ERR_IRQ)
841 printk(KERN_ERR "Invalid interrupt number,board not configured\n");
842 else if (retval == MXSER_ERR_IRQ_CONFLIT)
843 printk(KERN_ERR "Invalid interrupt number,board not configured\n");
844 else if (retval == MXSER_ERR_VECTOR)
845 printk(KERN_ERR "Invalid interrupt vector,board not configured\n");
846 else if (retval == MXSER_ERR_IOADDR)
847 printk(KERN_ERR "Invalid I/O address,board not configured\n");
850 mxser_getcfg(m, &hwconf);
851 //init mxsercfg first, or mxsercfg data is not correct on ISR.
852 //mxser_initbrd will hook ISR.
853 if (mxser_initbrd(m, &hwconf) < 0)
860 retval = tty_register_driver(mxvar_sdriver);
862 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family driver !\n");
863 put_tty_driver(mxvar_sdriver);
865 for (i = 0; i < MXSER_BOARDS; i++) {
866 if (mxsercfg[i].board_type == -1)
869 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
870 //todo: release io, vector
879 static void mxser_do_softint(void *private_)
881 struct mxser_struct *info = (struct mxser_struct *) private_;
882 struct tty_struct *tty;
887 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
889 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event))
894 static unsigned char mxser_get_msr(int baseaddr, int mode, int port, struct mxser_struct *info)
896 unsigned char status = 0;
898 status = inb(baseaddr + UART_MSR);
900 mxser_msr[port] &= 0x0F;
901 mxser_msr[port] |= status;
902 status = mxser_msr[port];
910 * This routine is called whenever a serial port is opened. It
911 * enables interrupts for a serial port, linking in its async structure into
912 * the IRQ chain. It also performs the serial-specific
913 * initialization for the tty structure.
915 static int mxser_open(struct tty_struct *tty, struct file *filp)
917 struct mxser_struct *info;
920 /* initialize driver_data in case something fails */
921 tty->driver_data = NULL;
924 if (line == MXSER_PORTS)
926 if (line < 0 || line > MXSER_PORTS)
928 info = mxvar_table + line;
932 tty->driver_data = info;
935 * Start up serial port
937 retval = mxser_startup(info);
941 retval = mxser_block_til_ready(tty, filp, info);
947 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
948 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
949 *tty->termios = info->normal_termios;
951 *tty->termios = info->callout_termios;
952 mxser_change_speed(info, NULL);
955 info->session = current->signal->session;
956 info->pgrp = process_group(current);
957 clear_bit(TTY_DONT_FLIP, &tty->flags);
959 //status = mxser_get_msr(info->base, 0, info->port);
960 //mxser_check_modem_status(info, status);
962 /* unmark here for very high baud rate (ex. 921600 bps) used
964 tty->low_latency = 1;
969 * This routine is called when the serial port gets closed. First, we
970 * wait for the last remaining data to be sent. Then, we unlink its
971 * async structure from the interrupt chain if necessary, and we free
972 * that IRQ if nothing is left in the chain.
974 static void mxser_close(struct tty_struct *tty, struct file *filp)
976 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
978 unsigned long timeout;
980 struct tty_ldisc *ld;
982 if (tty->index == MXSER_PORTS)
987 spin_lock_irqsave(&info->slock, flags);
989 if (tty_hung_up_p(filp)) {
990 spin_unlock_irqrestore(&info->slock, flags);
993 if ((tty->count == 1) && (info->count != 1)) {
995 * Uh, oh. tty->count is 1, which means that the tty
996 * structure will be freed. Info->count should always
997 * be one in these conditions. If it's greater than
998 * one, we've got real problems, since it means the
999 * serial port won't be shutdown.
1001 printk(KERN_ERR "mxser_close: bad serial port count; tty->count is 1, " "info->count is %d\n", info->count);
1004 if (--info->count < 0) {
1005 printk(KERN_ERR "mxser_close: bad serial port count for ttys%d: %d\n", info->port, info->count);
1009 spin_unlock_irqrestore(&info->slock, flags);
1012 info->flags |= ASYNC_CLOSING;
1013 spin_unlock_irqrestore(&info->slock, flags);
1015 * Save the termios structure, since this port may have
1016 * separate termios for callout and dialin.
1018 if (info->flags & ASYNC_NORMAL_ACTIVE)
1019 info->normal_termios = *tty->termios;
1021 * Now we wait for the transmit buffer to clear; and we notify
1022 * the line discipline to only process XON/XOFF characters.
1025 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1026 tty_wait_until_sent(tty, info->closing_wait);
1028 * At this point we stop accepting input. To do this, we
1029 * disable the receive line status interrupts, and tell the
1030 * interrupt driver to stop checking the data ready bit in the
1031 * line status register.
1033 info->IER &= ~UART_IER_RLSI;
1034 if (info->IsMoxaMustChipFlag)
1035 info->IER &= ~MOXA_MUST_RECV_ISR;
1037 info->read_status_mask &= ~UART_LSR_DR;
1039 if (info->flags & ASYNC_INITIALIZED) {
1040 outb(info->IER, info->base + UART_IER);
1042 * Before we drop DTR, make sure the UART transmitter
1043 * has completely drained; this is especially
1044 * important if there is a transmit FIFO!
1046 timeout = jiffies + HZ;
1047 while (!(inb(info->base + UART_LSR) & UART_LSR_TEMT)) {
1048 schedule_timeout_interruptible(5);
1049 if (time_after(jiffies, timeout))
1053 mxser_shutdown(info);
1055 if (tty->driver->flush_buffer)
1056 tty->driver->flush_buffer(tty);
1058 ld = tty_ldisc_ref(tty);
1060 if(ld->flush_buffer)
1061 ld->flush_buffer(tty);
1062 tty_ldisc_deref(ld);
1068 if (info->blocked_open) {
1069 if (info->close_delay)
1070 schedule_timeout_interruptible(info->close_delay);
1071 wake_up_interruptible(&info->open_wait);
1074 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1075 wake_up_interruptible(&info->close_wait);
1079 static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1082 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1083 unsigned long flags;
1085 if (!tty || !info->xmit_buf)
1089 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1, SERIAL_XMIT_SIZE - info->xmit_head));
1093 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1094 spin_lock_irqsave(&info->slock, flags);
1095 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE - 1);
1096 info->xmit_cnt += c;
1097 spin_unlock_irqrestore(&info->slock, flags);
1105 if (info->xmit_cnt && !tty->stopped && !(info->IER & UART_IER_THRI)) {
1106 if (!tty->hw_stopped || (info->type == PORT_16550A) || (info->IsMoxaMustChipFlag)) {
1107 spin_lock_irqsave(&info->slock, flags);
1108 info->IER |= UART_IER_THRI;
1109 outb(info->IER, info->base + UART_IER);
1110 spin_unlock_irqrestore(&info->slock, flags);
1116 static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1118 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1119 unsigned long flags;
1121 if (!tty || !info->xmit_buf)
1124 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1127 spin_lock_irqsave(&info->slock, flags);
1128 info->xmit_buf[info->xmit_head++] = ch;
1129 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1131 spin_unlock_irqrestore(&info->slock, flags);
1132 if (!tty->stopped && !(info->IER & UART_IER_THRI)) {
1133 if (!tty->hw_stopped || (info->type == PORT_16550A) || info->IsMoxaMustChipFlag) {
1134 spin_lock_irqsave(&info->slock, flags);
1135 info->IER |= UART_IER_THRI;
1136 outb(info->IER, info->base + UART_IER);
1137 spin_unlock_irqrestore(&info->slock, flags);
1143 static void mxser_flush_chars(struct tty_struct *tty)
1145 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1146 unsigned long flags;
1148 if (info->xmit_cnt <= 0 || tty->stopped || !info->xmit_buf || (tty->hw_stopped && (info->type != PORT_16550A) && (!info->IsMoxaMustChipFlag)))
1151 spin_lock_irqsave(&info->slock, flags);
1153 info->IER |= UART_IER_THRI;
1154 outb(info->IER, info->base + UART_IER);
1156 spin_unlock_irqrestore(&info->slock, flags);
1159 static int mxser_write_room(struct tty_struct *tty)
1161 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1164 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1170 static int mxser_chars_in_buffer(struct tty_struct *tty)
1172 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1173 return info->xmit_cnt;
1176 static void mxser_flush_buffer(struct tty_struct *tty)
1178 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1180 unsigned long flags;
1183 spin_lock_irqsave(&info->slock, flags);
1184 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1186 /* below added by shinhay */
1187 fcr = inb(info->base + UART_FCR);
1188 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT), info->base + UART_FCR);
1189 outb(fcr, info->base + UART_FCR);
1191 spin_unlock_irqrestore(&info->slock, flags);
1192 /* above added by shinhay */
1194 wake_up_interruptible(&tty->write_wait);
1195 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
1196 (tty->ldisc.write_wakeup) (tty);
1199 static int mxser_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1201 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1203 struct async_icount cprev, cnow; /* kernel counter temps */
1204 struct serial_icounter_struct __user *p_cuser;
1205 unsigned long templ;
1206 unsigned long flags;
1207 void __user *argp = (void __user *)arg;
1209 if (tty->index == MXSER_PORTS)
1210 return (mxser_ioctl_special(cmd, argp));
1212 // following add by Victor Yu. 01-05-2004
1213 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1215 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1217 unsigned char val, mask;
1220 if (cmd == MOXA_SET_OP_MODE) {
1221 if (get_user(opmode, (int __user *) argp))
1223 if (opmode != RS232_MODE && opmode != RS485_2WIRE_MODE && opmode != RS422_MODE && opmode != RS485_4WIRE_MODE)
1227 val = inb(info->opmode_ioaddr);
1229 val |= (opmode << shiftbit);
1230 outb(val, info->opmode_ioaddr);
1233 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1234 opmode &= OP_MODE_MASK;
1235 if (copy_to_user(argp, &opmode, sizeof(int)))
1240 // above add by Victor Yu. 01-05-2004
1242 if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1243 if (tty->flags & (1 << TTY_IO_ERROR))
1247 case TCSBRK: /* SVID version: non-zero arg --> no break */
1248 retval = tty_check_change(tty);
1251 tty_wait_until_sent(tty, 0);
1253 mxser_send_break(info, HZ / 4); /* 1/4 second */
1255 case TCSBRKP: /* support for POSIX tcsendbreak() */
1256 retval = tty_check_change(tty);
1259 tty_wait_until_sent(tty, 0);
1260 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1263 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) argp);
1265 if (get_user(templ, (unsigned long __user *) argp))
1268 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1271 return mxser_get_serial_info(info, argp);
1273 return mxser_set_serial_info(info, argp);
1274 case TIOCSERGETLSR: /* Get line status register */
1275 return mxser_get_lsr_info(info, argp);
1277 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1278 * - mask passed in arg for lines of interest
1279 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1280 * Caller should use TIOCGICOUNT to see which one it was
1283 DECLARE_WAITQUEUE(wait, current);
1285 spin_lock_irqsave(&info->slock, flags);
1286 cprev = info->icount; /* note the counters on entry */
1287 spin_unlock_irqrestore(&info->slock, flags);
1289 add_wait_queue(&info->delta_msr_wait, &wait);
1291 spin_lock_irqsave(&info->slock, flags);
1292 cnow = info->icount; /* atomic copy */
1293 spin_unlock_irqrestore(&info->slock, flags);
1295 set_current_state(TASK_INTERRUPTIBLE);
1296 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) || ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1300 /* see if a signal did it */
1301 if (signal_pending(current)) {
1307 current->state = TASK_RUNNING;
1308 remove_wait_queue(&info->delta_msr_wait, &wait);
1313 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1314 * Return: write counters to the user passed counter struct
1315 * NB: both 1->0 and 0->1 transitions are counted except for
1316 * RI where only 0->1 is counted.
1319 spin_lock_irqsave(&info->slock, flags);
1320 cnow = info->icount;
1321 spin_unlock_irqrestore(&info->slock, flags);
1323 /* modified by casper 1/11/2000 */
1324 if (put_user(cnow.frame, &p_cuser->frame))
1326 if (put_user(cnow.brk, &p_cuser->brk))
1328 if (put_user(cnow.overrun, &p_cuser->overrun))
1330 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1332 if (put_user(cnow.parity, &p_cuser->parity))
1334 if (put_user(cnow.rx, &p_cuser->rx))
1336 if (put_user(cnow.tx, &p_cuser->tx))
1338 put_user(cnow.cts, &p_cuser->cts);
1339 put_user(cnow.dsr, &p_cuser->dsr);
1340 put_user(cnow.rng, &p_cuser->rng);
1341 put_user(cnow.dcd, &p_cuser->dcd);
1345 case MOXA_HighSpeedOn:
1346 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *) argp);
1348 case MOXA_SDS_RSTICOUNTER:{
1349 info->mon_data.rxcnt = 0;
1350 info->mon_data.txcnt = 0;
1353 // (above) added by James.
1354 case MOXA_ASPP_SETBAUD:{
1356 if (get_user(baud, (long __user *) argp))
1358 mxser_set_baud(info, baud);
1361 case MOXA_ASPP_GETBAUD:
1362 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1367 case MOXA_ASPP_OQUEUE:{
1370 len = mxser_chars_in_buffer(tty);
1372 lsr = inb(info->base + UART_LSR) & UART_LSR_TEMT;
1374 len += (lsr ? 0 : 1);
1376 if (copy_to_user(argp, &len, sizeof(int)))
1381 case MOXA_ASPP_MON:{
1383 // info->mon_data.ser_param = tty->termios->c_cflag;
1385 status = mxser_get_msr(info->base, 1, info->port, info);
1386 mxser_check_modem_status(info, status);
1388 mcr = inb(info->base + UART_MCR);
1389 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1390 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1392 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1394 if (mcr & MOXA_MUST_MCR_TX_XON)
1395 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1397 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1399 if (info->tty->hw_stopped)
1400 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1402 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1405 if (copy_to_user(argp, &info->mon_data, sizeof(struct mxser_mon)))
1412 case MOXA_ASPP_LSTATUS:{
1413 if (copy_to_user(argp, &info->err_shadow, sizeof(unsigned char)))
1416 info->err_shadow = 0;
1420 case MOXA_SET_BAUD_METHOD:{
1422 if (get_user(method, (int __user *) argp))
1424 mxser_set_baud_method[info->port] = method;
1425 if (copy_to_user(argp, &method, sizeof(int)))
1431 return -ENOIOCTLCMD;
1437 #define CMSPAR 010000000000
1440 static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1442 int i, result, status;
1446 if (copy_to_user(argp, mxsercfg, sizeof(struct mxser_hwconf) * 4))
1449 case MOXA_GET_MAJOR:
1450 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1454 case MOXA_GET_CUMAJOR:
1455 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1459 case MOXA_CHKPORTENABLE:
1461 for (i = 0; i < MXSER_PORTS; i++) {
1462 if (mxvar_table[i].base)
1465 return put_user(result, (unsigned long __user *) argp);
1466 case MOXA_GETDATACOUNT:
1467 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1470 case MOXA_GETMSTATUS:
1471 for (i = 0; i < MXSER_PORTS; i++) {
1473 if (!mxvar_table[i].base) {
1474 GMStatus[i].dcd = 0;
1475 GMStatus[i].dsr = 0;
1476 GMStatus[i].cts = 0;
1480 if (!mxvar_table[i].tty || !mxvar_table[i].tty->termios)
1481 GMStatus[i].cflag = mxvar_table[i].normal_termios.c_cflag;
1483 GMStatus[i].cflag = mxvar_table[i].tty->termios->c_cflag;
1485 status = inb(mxvar_table[i].base + UART_MSR);
1486 if (status & 0x80 /*UART_MSR_DCD */ )
1487 GMStatus[i].dcd = 1;
1489 GMStatus[i].dcd = 0;
1491 if (status & 0x20 /*UART_MSR_DSR */ )
1492 GMStatus[i].dsr = 1;
1494 GMStatus[i].dsr = 0;
1497 if (status & 0x10 /*UART_MSR_CTS */ )
1498 GMStatus[i].cts = 1;
1500 GMStatus[i].cts = 0;
1502 if (copy_to_user(argp, GMStatus, sizeof(struct mxser_mstatus) * MXSER_PORTS))
1505 case MOXA_ASPP_MON_EXT:{
1509 unsigned cflag, iflag;
1511 for (i = 0; i < MXSER_PORTS; i++) {
1513 if (!mxvar_table[i].base)
1516 status = mxser_get_msr(mxvar_table[i].base, 0, i, &(mxvar_table[i]));
1517 // mxser_check_modem_status(&mxvar_table[i], status);
1518 if (status & UART_MSR_TERI)
1519 mxvar_table[i].icount.rng++;
1520 if (status & UART_MSR_DDSR)
1521 mxvar_table[i].icount.dsr++;
1522 if (status & UART_MSR_DDCD)
1523 mxvar_table[i].icount.dcd++;
1524 if (status & UART_MSR_DCTS)
1525 mxvar_table[i].icount.cts++;
1527 mxvar_table[i].mon_data.modem_status = status;
1528 mon_data_ext.rx_cnt[i] = mxvar_table[i].mon_data.rxcnt;
1529 mon_data_ext.tx_cnt[i] = mxvar_table[i].mon_data.txcnt;
1530 mon_data_ext.up_rxcnt[i] = mxvar_table[i].mon_data.up_rxcnt;
1531 mon_data_ext.up_txcnt[i] = mxvar_table[i].mon_data.up_txcnt;
1532 mon_data_ext.modem_status[i] = mxvar_table[i].mon_data.modem_status;
1533 mon_data_ext.baudrate[i] = mxvar_table[i].realbaud;
1535 if (!mxvar_table[i].tty || !mxvar_table[i].tty->termios) {
1536 cflag = mxvar_table[i].normal_termios.c_cflag;
1537 iflag = mxvar_table[i].normal_termios.c_iflag;
1539 cflag = mxvar_table[i].tty->termios->c_cflag;
1540 iflag = mxvar_table[i].tty->termios->c_iflag;
1543 mon_data_ext.databits[i] = cflag & CSIZE;
1545 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1547 mon_data_ext.parity[i] = cflag & (PARENB | PARODD | CMSPAR);
1549 mon_data_ext.flowctrl[i] = 0x00;
1551 if (cflag & CRTSCTS)
1552 mon_data_ext.flowctrl[i] |= 0x03;
1554 if (iflag & (IXON | IXOFF))
1555 mon_data_ext.flowctrl[i] |= 0x0C;
1557 if (mxvar_table[i].type == PORT_16550A)
1558 mon_data_ext.fifo[i] = 1;
1560 mon_data_ext.fifo[i] = 0;
1564 opmode = inb(mxvar_table[i].opmode_ioaddr) >> shiftbit;
1565 opmode &= OP_MODE_MASK;
1567 mon_data_ext.iftype[i] = opmode;
1570 if (copy_to_user(argp, &mon_data_ext, sizeof(struct mxser_mon_ext)))
1577 return -ENOIOCTLCMD;
1583 static void mxser_stoprx(struct tty_struct *tty)
1585 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1586 //unsigned long flags;
1589 info->ldisc_stop_rx = 1;
1592 //MX_LOCK(&info->slock);
1593 // following add by Victor Yu. 09-02-2002
1594 if (info->IsMoxaMustChipFlag) {
1595 info->IER &= ~MOXA_MUST_RECV_ISR;
1596 outb(info->IER, info->base + UART_IER);
1598 // above add by Victor Yu. 09-02-2002
1600 info->x_char = STOP_CHAR(tty);
1601 // outb(info->IER, 0); // mask by Victor Yu. 09-02-2002
1602 outb(0, info->base + UART_IER);
1603 info->IER |= UART_IER_THRI;
1604 outb(info->IER, info->base + UART_IER); /* force Tx interrupt */
1605 } // add by Victor Yu. 09-02-2002
1606 //MX_UNLOCK(&info->slock);
1609 if (info->tty->termios->c_cflag & CRTSCTS) {
1610 //MX_LOCK(&info->slock);
1611 info->MCR &= ~UART_MCR_RTS;
1612 outb(info->MCR, info->base + UART_MCR);
1613 //MX_UNLOCK(&info->slock);
1617 static void mxser_startrx(struct tty_struct *tty)
1619 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1620 //unsigned long flags;
1622 info->ldisc_stop_rx = 0;
1627 //MX_LOCK(&info->slock);
1629 // following add by Victor Yu. 09-02-2002
1630 if (info->IsMoxaMustChipFlag) {
1631 info->IER |= MOXA_MUST_RECV_ISR;
1632 outb(info->IER, info->base + UART_IER);
1634 // above add by Victor Yu. 09-02-2002
1636 info->x_char = START_CHAR(tty);
1637 // outb(info->IER, 0); // mask by Victor Yu. 09-02-2002
1638 outb(0, info->base + UART_IER); // add by Victor Yu. 09-02-2002
1639 info->IER |= UART_IER_THRI; /* force Tx interrupt */
1640 outb(info->IER, info->base + UART_IER);
1641 } // add by Victor Yu. 09-02-2002
1642 //MX_UNLOCK(&info->slock);
1646 if (info->tty->termios->c_cflag & CRTSCTS) {
1647 //MX_LOCK(&info->slock);
1648 info->MCR |= UART_MCR_RTS;
1649 outb(info->MCR, info->base + UART_MCR);
1650 //MX_UNLOCK(&info->slock);
1655 * This routine is called by the upper-layer tty layer to signal that
1656 * incoming characters should be throttled.
1658 static void mxser_throttle(struct tty_struct *tty)
1660 //struct mxser_struct *info = (struct mxser_struct *)tty->driver_data;
1661 //unsigned long flags;
1662 //MX_LOCK(&info->slock);
1664 //MX_UNLOCK(&info->slock);
1667 static void mxser_unthrottle(struct tty_struct *tty)
1669 //struct mxser_struct *info = (struct mxser_struct *)tty->driver_data;
1670 //unsigned long flags;
1671 //MX_LOCK(&info->slock);
1673 //MX_UNLOCK(&info->slock);
1676 static void mxser_set_termios(struct tty_struct *tty, struct termios *old_termios)
1678 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1679 unsigned long flags;
1681 if ((tty->termios->c_cflag != old_termios->c_cflag) || (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
1683 mxser_change_speed(info, old_termios);
1685 if ((old_termios->c_cflag & CRTSCTS) && !(tty->termios->c_cflag & CRTSCTS)) {
1686 tty->hw_stopped = 0;
1691 /* Handle sw stopped */
1692 if ((old_termios->c_iflag & IXON) && !(tty->termios->c_iflag & IXON)) {
1695 // following add by Victor Yu. 09-02-2002
1696 if (info->IsMoxaMustChipFlag) {
1697 spin_lock_irqsave(&info->slock, flags);
1698 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->base);
1699 spin_unlock_irqrestore(&info->slock, flags);
1701 // above add by Victor Yu. 09-02-2002
1708 * mxser_stop() and mxser_start()
1710 * This routines are called before setting or resetting tty->stopped.
1711 * They enable or disable transmitter interrupts, as necessary.
1713 static void mxser_stop(struct tty_struct *tty)
1715 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1716 unsigned long flags;
1718 spin_lock_irqsave(&info->slock, flags);
1719 if (info->IER & UART_IER_THRI) {
1720 info->IER &= ~UART_IER_THRI;
1721 outb(info->IER, info->base + UART_IER);
1723 spin_unlock_irqrestore(&info->slock, flags);
1726 static void mxser_start(struct tty_struct *tty)
1728 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1729 unsigned long flags;
1731 spin_lock_irqsave(&info->slock, flags);
1732 if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
1733 info->IER |= UART_IER_THRI;
1734 outb(info->IER, info->base + UART_IER);
1736 spin_unlock_irqrestore(&info->slock, flags);
1740 * mxser_wait_until_sent() --- wait until the transmitter is empty
1742 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1744 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1745 unsigned long orig_jiffies, char_time;
1748 if (info->type == PORT_UNKNOWN)
1751 if (info->xmit_fifo_size == 0)
1752 return; /* Just in case.... */
1754 orig_jiffies = jiffies;
1756 * Set the check interval to be 1/5 of the estimated time to
1757 * send a single character, and make it at least 1. The check
1758 * interval should also be less than the timeout.
1760 * Note: we have to use pretty tight timings here to satisfy
1763 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1764 char_time = char_time / 5;
1767 if (timeout && timeout < char_time)
1768 char_time = timeout;
1770 * If the transmitter hasn't cleared in twice the approximate
1771 * amount of time to send the entire FIFO, it probably won't
1772 * ever clear. This assumes the UART isn't doing flow
1773 * control, which is currently the case. Hence, if it ever
1774 * takes longer than info->timeout, this is probably due to a
1775 * UART bug of some kind. So, we clamp the timeout parameter at
1778 if (!timeout || timeout > 2 * info->timeout)
1779 timeout = 2 * info->timeout;
1780 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1781 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1782 printk("jiff=%lu...", jiffies);
1784 while (!((lsr = inb(info->base + UART_LSR)) & UART_LSR_TEMT)) {
1785 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1786 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1788 schedule_timeout_interruptible(char_time);
1789 if (signal_pending(current))
1791 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1794 set_current_state(TASK_RUNNING);
1796 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1797 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1803 * This routine is called by tty_hangup() when a hangup is signaled.
1805 void mxser_hangup(struct tty_struct *tty)
1807 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1809 mxser_flush_buffer(tty);
1810 mxser_shutdown(info);
1813 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1815 wake_up_interruptible(&info->open_wait);
1819 // added by James 03-12-2004.
1821 * mxser_rs_break() --- routine which turns the break handling on or off
1823 static void mxser_rs_break(struct tty_struct *tty, int break_state)
1825 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1826 unsigned long flags;
1828 spin_lock_irqsave(&info->slock, flags);
1829 if (break_state == -1)
1830 outb(inb(info->base + UART_LCR) | UART_LCR_SBC, info->base + UART_LCR);
1832 outb(inb(info->base + UART_LCR) & ~UART_LCR_SBC, info->base + UART_LCR);
1833 spin_unlock_irqrestore(&info->slock, flags);
1836 // (above) added by James.
1840 * This is the serial driver's generic interrupt routine
1842 static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1845 struct mxser_struct *info;
1846 struct mxser_struct *port;
1847 int max, irqbits, bits, msr;
1848 int pass_counter = 0;
1849 int handled = IRQ_NONE;
1852 //spin_lock(&gm_lock);
1854 for (i = 0; i < MXSER_BOARDS; i++) {
1855 if (dev_id == &(mxvar_table[i * MXSER_PORTS_PER_BOARD])) {
1861 if (i == MXSER_BOARDS) {
1867 max = mxser_numports[mxsercfg[i].board_type - 1];
1869 irqbits = inb(port->vector) & port->vectormask;
1870 if (irqbits == port->vectormask) {
1874 handled = IRQ_HANDLED;
1875 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
1876 if (irqbits == port->vectormask) {
1883 // following add by Victor Yu. 09-13-2002
1884 iir = inb(info->base + UART_IIR);
1885 if (iir & UART_IIR_NO_INT)
1887 iir &= MOXA_MUST_IIR_MASK;
1889 status = inb(info->base + UART_LSR);
1890 outb(0x27, info->base + UART_FCR);
1891 inb(info->base + UART_MSR);
1894 // above add by Victor Yu. 09-13-2002
1896 if ( info->tty->flip.count < TTY_FLIPBUF_SIZE/4 ){
1897 info->IER |= MOXA_MUST_RECV_ISR;
1898 outb(info->IER, info->base + UART_IER);
1903 /* mask by Victor Yu. 09-13-2002
1905 (inb(info->base + UART_IIR) & UART_IIR_NO_INT) )
1908 /* mask by Victor Yu. 09-02-2002
1909 status = inb(info->base + UART_LSR) & info->read_status_mask;
1912 // following add by Victor Yu. 09-02-2002
1913 status = inb(info->base + UART_LSR);
1915 if (status & UART_LSR_PE) {
1916 info->err_shadow |= NPPI_NOTIFY_PARITY;
1918 if (status & UART_LSR_FE) {
1919 info->err_shadow |= NPPI_NOTIFY_FRAMING;
1921 if (status & UART_LSR_OE) {
1922 info->err_shadow |= NPPI_NOTIFY_HW_OVERRUN;
1924 if (status & UART_LSR_BI)
1925 info->err_shadow |= NPPI_NOTIFY_BREAK;
1927 if (info->IsMoxaMustChipFlag) {
1929 if ( (status & 0x02) && !(status & 0x01) ) {
1930 outb(info->base+UART_FCR, 0x23);
1934 if (iir == MOXA_MUST_IIR_GDA || iir == MOXA_MUST_IIR_RDA || iir == MOXA_MUST_IIR_RTO || iir == MOXA_MUST_IIR_LSR)
1935 mxser_receive_chars(info, &status);
1938 // above add by Victor Yu. 09-02-2002
1940 status &= info->read_status_mask;
1941 if (status & UART_LSR_DR)
1942 mxser_receive_chars(info, &status);
1944 msr = inb(info->base + UART_MSR);
1945 if (msr & UART_MSR_ANY_DELTA) {
1946 mxser_check_modem_status(info, msr);
1948 // following add by Victor Yu. 09-13-2002
1949 if (info->IsMoxaMustChipFlag) {
1950 if ((iir == 0x02) && (status & UART_LSR_THRE)) {
1951 mxser_transmit_chars(info);
1954 // above add by Victor Yu. 09-13-2002
1956 if (status & UART_LSR_THRE) {
1957 /* 8-2-99 by William
1958 if ( info->x_char || (info->xmit_cnt > 0) )
1960 mxser_transmit_chars(info);
1964 if (pass_counter++ > MXSER_ISR_PASS_LIMIT) {
1965 break; /* Prevent infinite loops */
1970 //spin_unlock(&gm_lock);
1974 static void mxser_receive_chars(struct mxser_struct *info, int *status)
1976 struct tty_struct *tty = info->tty;
1977 unsigned char ch, gdl;
1982 unsigned long flags;
1984 spin_lock_irqsave(&info->slock, flags);
1986 recv_room = tty->ldisc.receive_room(tty);
1987 if ((recv_room == 0) && (!info->ldisc_stop_rx)) {
1988 //mxser_throttle(tty);
1993 // following add by Victor Yu. 09-02-2002
1994 if (info->IsMoxaMustChipFlag != MOXA_OTHER_UART) {
1996 if (*status & UART_LSR_SPECIAL) {
1999 // following add by Victor Yu. 02-11-2004
2000 if (info->IsMoxaMustChipFlag == MOXA_MUST_MU860_HWID && (*status & MOXA_MUST_LSR_RERR))
2002 // above add by Victor Yu. 02-14-2004
2003 if (*status & MOXA_MUST_LSR_RERR)
2006 gdl = inb(info->base + MOXA_MUST_GDL_REGISTER);
2008 if (info->IsMoxaMustChipFlag == MOXA_MUST_MU150_HWID) // add by Victor Yu. 02-11-2004
2009 gdl &= MOXA_MUST_GDL_MASK;
2010 if (gdl >= recv_room) {
2011 if (!info->ldisc_stop_rx) {
2012 //mxser_throttle(tty);
2018 ch = inb(info->base + UART_RX);
2019 tty_insert_flip_char(tty, ch, 0);
2022 if((cnt>=HI_WATER) && (info->stop_rx==0)){
2031 // above add by Victor Yu. 09-02-2002
2037 if((cnt>=HI_WATER) && (info->stop_rx==0)){
2044 ch = inb(info->base + UART_RX);
2045 // following add by Victor Yu. 09-02-2002
2046 if (info->IsMoxaMustChipFlag && (*status & UART_LSR_OE) /*&& !(*status&UART_LSR_DR) */ )
2047 outb(0x23, info->base + UART_FCR);
2048 *status &= info->read_status_mask;
2049 // above add by Victor Yu. 09-02-2002
2050 if (*status & info->ignore_status_mask) {
2051 if (++ignored > 100)
2055 if (*status & UART_LSR_SPECIAL) {
2056 if (*status & UART_LSR_BI) {
2058 /* added by casper 1/11/2000 */
2061 if (info->flags & ASYNC_SAK)
2063 } else if (*status & UART_LSR_PE) {
2065 /* added by casper 1/11/2000 */
2066 info->icount.parity++;
2068 } else if (*status & UART_LSR_FE) {
2070 /* added by casper 1/11/2000 */
2071 info->icount.frame++;
2073 } else if (*status & UART_LSR_OE) {
2075 /* added by casper 1/11/2000 */
2076 info->icount.overrun++;
2080 tty_insert_flip_char(tty, ch, flag);
2082 if (cnt >= recv_room) {
2083 if (!info->ldisc_stop_rx) {
2084 //mxser_throttle(tty);
2092 // following add by Victor Yu. 09-02-2002
2093 if (info->IsMoxaMustChipFlag)
2095 // above add by Victor Yu. 09-02-2002
2097 /* mask by Victor Yu. 09-02-2002
2098 *status = inb(info->base + UART_LSR) & info->read_status_mask;
2100 // following add by Victor Yu. 09-02-2002
2101 *status = inb(info->base + UART_LSR);
2102 // above add by Victor Yu. 09-02-2002
2103 } while (*status & UART_LSR_DR);
2105 end_intr: // add by Victor Yu. 09-02-2002
2107 mxvar_log.rxcnt[info->port] += cnt;
2108 info->mon_data.rxcnt += cnt;
2109 info->mon_data.up_rxcnt += cnt;
2110 spin_unlock_irqrestore(&info->slock, flags);
2112 tty_flip_buffer_push(tty);
2115 static void mxser_transmit_chars(struct mxser_struct *info)
2118 unsigned long flags;
2120 spin_lock_irqsave(&info->slock, flags);
2123 outb(info->x_char, info->base + UART_TX);
2125 mxvar_log.txcnt[info->port]++;
2126 info->mon_data.txcnt++;
2127 info->mon_data.up_txcnt++;
2129 /* added by casper 1/11/2000 */
2132 spin_unlock_irqrestore(&info->slock, flags);
2136 if (info->xmit_buf == 0) {
2137 spin_unlock_irqrestore(&info->slock, flags);
2141 if ((info->xmit_cnt <= 0) || info->tty->stopped || (info->tty->hw_stopped && (info->type != PORT_16550A) && (!info->IsMoxaMustChipFlag))) {
2142 info->IER &= ~UART_IER_THRI;
2143 outb(info->IER, info->base + UART_IER);
2144 spin_unlock_irqrestore(&info->slock, flags);
2148 cnt = info->xmit_cnt;
2149 count = info->xmit_fifo_size;
2151 outb(info->xmit_buf[info->xmit_tail++], info->base + UART_TX);
2152 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2153 if (--info->xmit_cnt <= 0)
2155 } while (--count > 0);
2156 mxvar_log.txcnt[info->port] += (cnt - info->xmit_cnt);
2158 // added by James 03-12-2004.
2159 info->mon_data.txcnt += (cnt - info->xmit_cnt);
2160 info->mon_data.up_txcnt += (cnt - info->xmit_cnt);
2161 // (above) added by James.
2163 /* added by casper 1/11/2000 */
2164 info->icount.tx += (cnt - info->xmit_cnt);
2167 if (info->xmit_cnt < WAKEUP_CHARS) {
2168 set_bit(MXSER_EVENT_TXLOW, &info->event);
2169 schedule_work(&info->tqueue);
2171 if (info->xmit_cnt <= 0) {
2172 info->IER &= ~UART_IER_THRI;
2173 outb(info->IER, info->base + UART_IER);
2175 spin_unlock_irqrestore(&info->slock, flags);
2178 static void mxser_check_modem_status(struct mxser_struct *info, int status)
2180 /* update input line counters */
2181 if (status & UART_MSR_TERI)
2183 if (status & UART_MSR_DDSR)
2185 if (status & UART_MSR_DDCD)
2187 if (status & UART_MSR_DCTS)
2189 info->mon_data.modem_status = status;
2190 wake_up_interruptible(&info->delta_msr_wait);
2193 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
2194 if (status & UART_MSR_DCD)
2195 wake_up_interruptible(&info->open_wait);
2196 schedule_work(&info->tqueue);
2199 if (info->flags & ASYNC_CTS_FLOW) {
2200 if (info->tty->hw_stopped) {
2201 if (status & UART_MSR_CTS) {
2202 info->tty->hw_stopped = 0;
2204 if ((info->type != PORT_16550A) && (!info->IsMoxaMustChipFlag)) {
2205 info->IER |= UART_IER_THRI;
2206 outb(info->IER, info->base + UART_IER);
2208 set_bit(MXSER_EVENT_TXLOW, &info->event);
2209 schedule_work(&info->tqueue); }
2211 if (!(status & UART_MSR_CTS)) {
2212 info->tty->hw_stopped = 1;
2213 if ((info->type != PORT_16550A) && (!info->IsMoxaMustChipFlag)) {
2214 info->IER &= ~UART_IER_THRI;
2215 outb(info->IER, info->base + UART_IER);
2222 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp, struct mxser_struct *info)
2224 DECLARE_WAITQUEUE(wait, current);
2227 unsigned long flags;
2230 * If non-blocking mode is set, or the port is not enabled,
2231 * then make the check up front and then exit.
2233 if ((filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR))) {
2234 info->flags |= ASYNC_NORMAL_ACTIVE;
2238 if (tty->termios->c_cflag & CLOCAL)
2242 * Block waiting for the carrier detect and the line to become
2243 * free (i.e., not in use by the callout). While we are in
2244 * this loop, info->count is dropped by one, so that
2245 * mxser_close() knows when to free things. We restore it upon
2246 * exit, either normal or abnormal.
2249 add_wait_queue(&info->open_wait, &wait);
2251 spin_lock_irqsave(&info->slock, flags);
2252 if (!tty_hung_up_p(filp))
2254 spin_unlock_irqrestore(&info->slock, flags);
2255 info->blocked_open++;
2257 spin_lock_irqsave(&info->slock, flags);
2258 outb(inb(info->base + UART_MCR) | UART_MCR_DTR | UART_MCR_RTS, info->base + UART_MCR);
2259 spin_unlock_irqrestore(&info->slock, flags);
2260 set_current_state(TASK_INTERRUPTIBLE);
2261 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)) {
2262 if (info->flags & ASYNC_HUP_NOTIFY)
2265 retval = -ERESTARTSYS;
2268 if (!(info->flags & ASYNC_CLOSING) && (do_clocal || (inb(info->base + UART_MSR) & UART_MSR_DCD)))
2270 if (signal_pending(current)) {
2271 retval = -ERESTARTSYS;
2276 set_current_state(TASK_RUNNING);
2277 remove_wait_queue(&info->open_wait, &wait);
2278 if (!tty_hung_up_p(filp))
2280 info->blocked_open--;
2283 info->flags |= ASYNC_NORMAL_ACTIVE;
2287 static int mxser_startup(struct mxser_struct *info)
2291 unsigned long flags;
2293 page = __get_free_page(GFP_KERNEL);
2297 spin_lock_irqsave(&info->slock, flags);
2299 if (info->flags & ASYNC_INITIALIZED) {
2301 spin_unlock_irqrestore(&info->slock, flags);
2305 if (!info->base || !info->type) {
2307 set_bit(TTY_IO_ERROR, &info->tty->flags);
2309 spin_unlock_irqrestore(&info->slock, flags);
2315 info->xmit_buf = (unsigned char *) page;
2318 * Clear the FIFO buffers and disable them
2319 * (they will be reenabled in mxser_change_speed())
2321 if (info->IsMoxaMustChipFlag)
2322 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT | MOXA_MUST_FCR_GDA_MODE_ENABLE), info->base + UART_FCR);
2324 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT), info->base + UART_FCR);
2327 * At this point there's no way the LSR could still be 0xFF;
2328 * if it is, then bail out, because there's likely no UART
2331 if (inb(info->base + UART_LSR) == 0xff) {
2332 spin_unlock_irqrestore(&info->slock, flags);
2333 if (capable(CAP_SYS_ADMIN)) {
2335 set_bit(TTY_IO_ERROR, &info->tty->flags);
2342 * Clear the interrupt registers.
2344 (void) inb(info->base + UART_LSR);
2345 (void) inb(info->base + UART_RX);
2346 (void) inb(info->base + UART_IIR);
2347 (void) inb(info->base + UART_MSR);
2350 * Now, initialize the UART
2352 outb(UART_LCR_WLEN8, info->base + UART_LCR); /* reset DLAB */
2353 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
2354 outb(info->MCR, info->base + UART_MCR);
2357 * Finally, enable interrupts
2359 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
2360 // info->IER = UART_IER_RLSI | UART_IER_RDI;
2362 // following add by Victor Yu. 08-30-2002
2363 if (info->IsMoxaMustChipFlag)
2364 info->IER |= MOXA_MUST_IER_EGDAI;
2365 // above add by Victor Yu. 08-30-2002
2366 outb(info->IER, info->base + UART_IER); /* enable interrupts */
2369 * And clear the interrupt registers again for luck.
2371 (void) inb(info->base + UART_LSR);
2372 (void) inb(info->base + UART_RX);
2373 (void) inb(info->base + UART_IIR);
2374 (void) inb(info->base + UART_MSR);
2377 clear_bit(TTY_IO_ERROR, &info->tty->flags);
2378 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
2381 * and set the speed of the serial port
2383 spin_unlock_irqrestore(&info->slock, flags);
2384 mxser_change_speed(info, NULL);
2386 info->flags |= ASYNC_INITIALIZED;
2391 * This routine will shutdown a serial port; interrupts maybe disabled, and
2392 * DTR is dropped if the hangup on close termio flag is on.
2394 static void mxser_shutdown(struct mxser_struct *info)
2396 unsigned long flags;
2398 if (!(info->flags & ASYNC_INITIALIZED))
2401 spin_lock_irqsave(&info->slock, flags);
2404 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
2405 * here so the queue might never be waken up
2407 wake_up_interruptible(&info->delta_msr_wait);
2410 * Free the IRQ, if necessary
2412 if (info->xmit_buf) {
2413 free_page((unsigned long) info->xmit_buf);
2414 info->xmit_buf = NULL;
2418 outb(0x00, info->base + UART_IER);
2420 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
2421 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
2422 outb(info->MCR, info->base + UART_MCR);
2424 /* clear Rx/Tx FIFO's */
2425 // following add by Victor Yu. 08-30-2002
2426 if (info->IsMoxaMustChipFlag)
2427 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT | MOXA_MUST_FCR_GDA_MODE_ENABLE), info->base + UART_FCR);
2429 // above add by Victor Yu. 08-30-2002
2430 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT), info->base + UART_FCR);
2432 /* read data port to reset things */
2433 (void) inb(info->base + UART_RX);
2436 set_bit(TTY_IO_ERROR, &info->tty->flags);
2438 info->flags &= ~ASYNC_INITIALIZED;
2440 // following add by Victor Yu. 09-23-2002
2441 if (info->IsMoxaMustChipFlag) {
2442 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->base);
2444 // above add by Victor Yu. 09-23-2002
2446 spin_unlock_irqrestore(&info->slock, flags);
2450 * This routine is called to set the UART divisor registers to match
2451 * the specified baud rate for a serial port.
2453 static int mxser_change_speed(struct mxser_struct *info, struct termios *old_termios)
2455 unsigned cflag, cval, fcr;
2457 unsigned char status;
2459 unsigned long flags;
2462 if (!info->tty || !info->tty->termios)
2464 cflag = info->tty->termios->c_cflag;
2470 #define B921600 (B460800 +1)
2472 if (mxser_set_baud_method[info->port] == 0) {
2473 switch (cflag & (CBAUD | CBAUDEX)) {
2538 mxser_set_baud(info, baud);
2541 /* byte size and parity */
2542 switch (cflag & CSIZE) {
2557 break; /* too keep GCC shut... */
2562 cval |= UART_LCR_PARITY;
2563 if (!(cflag & PARODD)) {
2564 cval |= UART_LCR_EPAR;
2567 cval |= UART_LCR_SPAR;
2569 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
2570 if (info->IsMoxaMustChipFlag) {
2571 fcr = UART_FCR_ENABLE_FIFO;
2572 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2573 SET_MOXA_MUST_FIFO_VALUE(info);
2577 fcr = UART_FCR_ENABLE_FIFO;
2578 // following add by Victor Yu. 08-30-2002
2579 if (info->IsMoxaMustChipFlag) {
2580 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2581 SET_MOXA_MUST_FIFO_VALUE(info);
2583 // above add by Victor Yu. 08-30-2002
2585 switch (info->rx_trigger) {
2587 fcr |= UART_FCR_TRIGGER_1;
2590 fcr |= UART_FCR_TRIGGER_4;
2593 fcr |= UART_FCR_TRIGGER_8;
2596 fcr |= UART_FCR_TRIGGER_14;
2602 /* CTS flow control flag and modem status interrupts */
2603 info->IER &= ~UART_IER_MSI;
2604 info->MCR &= ~UART_MCR_AFE;
2605 if (cflag & CRTSCTS) {
2606 info->flags |= ASYNC_CTS_FLOW;
2607 info->IER |= UART_IER_MSI;
2608 if ((info->type == PORT_16550A) || (info->IsMoxaMustChipFlag)) {
2609 info->MCR |= UART_MCR_AFE;
2610 //status = mxser_get_msr(info->base, 0, info->port);
2611 /* save_flags(flags);
2613 status = inb(baseaddr + UART_MSR);
2614 restore_flags(flags);*/
2615 //mxser_check_modem_status(info, status);
2617 //status = mxser_get_msr(info->base, 0, info->port);
2619 //MX_LOCK(&info->slock);
2620 status = inb(info->base + UART_MSR);
2621 //MX_UNLOCK(&info->slock);
2622 if (info->tty->hw_stopped) {
2623 if (status & UART_MSR_CTS) {
2624 info->tty->hw_stopped = 0;
2625 if ((info->type != PORT_16550A) && (!info->IsMoxaMustChipFlag)) {
2626 info->IER |= UART_IER_THRI;
2627 outb(info->IER, info->base + UART_IER);
2629 set_bit(MXSER_EVENT_TXLOW, &info->event);
2630 schedule_work(&info->tqueue); }
2632 if (!(status & UART_MSR_CTS)) {
2633 info->tty->hw_stopped = 1;
2634 if ((info->type != PORT_16550A) && (!info->IsMoxaMustChipFlag)) {
2635 info->IER &= ~UART_IER_THRI;
2636 outb(info->IER, info->base + UART_IER);
2642 info->flags &= ~ASYNC_CTS_FLOW;
2644 outb(info->MCR, info->base + UART_MCR);
2645 if (cflag & CLOCAL) {
2646 info->flags &= ~ASYNC_CHECK_CD;
2648 info->flags |= ASYNC_CHECK_CD;
2649 info->IER |= UART_IER_MSI;
2651 outb(info->IER, info->base + UART_IER);
2654 * Set up parity check flag
2656 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2657 if (I_INPCK(info->tty))
2658 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2659 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
2660 info->read_status_mask |= UART_LSR_BI;
2662 info->ignore_status_mask = 0;
2664 if (I_IGNBRK(info->tty)) {
2665 info->ignore_status_mask |= UART_LSR_BI;
2666 info->read_status_mask |= UART_LSR_BI;
2668 * If we're ignore parity and break indicators, ignore
2669 * overruns too. (For real raw support).
2671 if (I_IGNPAR(info->tty)) {
2672 info->ignore_status_mask |= UART_LSR_OE | UART_LSR_PE | UART_LSR_FE;
2673 info->read_status_mask |= UART_LSR_OE | UART_LSR_PE | UART_LSR_FE;
2676 // following add by Victor Yu. 09-02-2002
2677 if (info->IsMoxaMustChipFlag) {
2678 spin_lock_irqsave(&info->slock, flags);
2679 SET_MOXA_MUST_XON1_VALUE(info->base, START_CHAR(info->tty));
2680 SET_MOXA_MUST_XOFF1_VALUE(info->base, STOP_CHAR(info->tty));
2681 if (I_IXON(info->tty)) {
2682 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->base);
2684 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->base);
2686 if (I_IXOFF(info->tty)) {
2687 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->base);
2689 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->base);
2692 if ( I_IXANY(info->tty) ) {
2693 info->MCR |= MOXA_MUST_MCR_XON_ANY;
2694 ENABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->base);
2696 info->MCR &= ~MOXA_MUST_MCR_XON_ANY;
2697 DISABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->base);
2700 spin_unlock_irqrestore(&info->slock, flags);
2702 // above add by Victor Yu. 09-02-2002
2705 outb(fcr, info->base + UART_FCR); /* set fcr */
2706 outb(cval, info->base + UART_LCR);
2712 static int mxser_set_baud(struct mxser_struct *info, long newspd)
2717 unsigned long flags;
2719 if (!info->tty || !info->tty->termios)
2725 if (newspd > info->MaxCanSetBaudRate)
2728 info->realbaud = newspd;
2729 if (newspd == 134) {
2730 quot = (2 * info->baud_base / 269);
2731 } else if (newspd) {
2732 quot = info->baud_base / newspd;
2741 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
2742 info->timeout += HZ / 50; /* Add .02 seconds of slop */
2745 spin_lock_irqsave(&info->slock, flags);
2746 info->MCR |= UART_MCR_DTR;
2747 outb(info->MCR, info->base + UART_MCR);
2748 spin_unlock_irqrestore(&info->slock, flags);
2750 spin_lock_irqsave(&info->slock, flags);
2751 info->MCR &= ~UART_MCR_DTR;
2752 outb(info->MCR, info->base + UART_MCR);
2753 spin_unlock_irqrestore(&info->slock, flags);
2757 cval = inb(info->base + UART_LCR);
2759 outb(cval | UART_LCR_DLAB, info->base + UART_LCR); /* set DLAB */
2761 outb(quot & 0xff, info->base + UART_DLL); /* LS of divisor */
2762 outb(quot >> 8, info->base + UART_DLM); /* MS of divisor */
2763 outb(cval, info->base + UART_LCR); /* reset DLAB */
2772 * ------------------------------------------------------------
2773 * friends of mxser_ioctl()
2774 * ------------------------------------------------------------
2776 static int mxser_get_serial_info(struct mxser_struct *info, struct serial_struct __user *retinfo)
2778 struct serial_struct tmp;
2782 memset(&tmp, 0, sizeof(tmp));
2783 tmp.type = info->type;
2784 tmp.line = info->port;
2785 tmp.port = info->base;
2786 tmp.irq = info->irq;
2787 tmp.flags = info->flags;
2788 tmp.baud_base = info->baud_base;
2789 tmp.close_delay = info->close_delay;
2790 tmp.closing_wait = info->closing_wait;
2791 tmp.custom_divisor = info->custom_divisor;
2793 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2798 static int mxser_set_serial_info(struct mxser_struct *info, struct serial_struct __user *new_info)
2800 struct serial_struct new_serial;
2804 if (!new_info || !info->base)
2806 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2809 if ((new_serial.irq != info->irq) || (new_serial.port != info->base) || (new_serial.custom_divisor != info->custom_divisor) || (new_serial.baud_base != info->baud_base))
2812 flags = info->flags & ASYNC_SPD_MASK;
2814 if (!capable(CAP_SYS_ADMIN)) {
2815 if ((new_serial.baud_base != info->baud_base) || (new_serial.close_delay != info->close_delay) || ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
2817 info->flags = ((info->flags & ~ASYNC_USR_MASK) | (new_serial.flags & ASYNC_USR_MASK));
2820 * OK, past this point, all the error checking has been done.
2821 * At this point, we start making changes.....
2823 info->flags = ((info->flags & ~ASYNC_FLAGS) | (new_serial.flags & ASYNC_FLAGS));
2824 info->close_delay = new_serial.close_delay * HZ / 100;
2825 info->closing_wait = new_serial.closing_wait * HZ / 100;
2826 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2827 info->tty->low_latency = 0; //(info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2830 /* added by casper, 3/17/2000, for mouse */
2831 info->type = new_serial.type;
2833 process_txrx_fifo(info);
2836 if (info->flags & ASYNC_INITIALIZED) {
2837 if (flags != (info->flags & ASYNC_SPD_MASK)) {
2838 mxser_change_speed(info, NULL);
2841 retval = mxser_startup(info);
2847 * mxser_get_lsr_info - get line status register info
2849 * Purpose: Let user call ioctl() to get info when the UART physically
2850 * is emptied. On bus types like RS485, the transmitter must
2851 * release the bus after transmitting. This must be done when
2852 * the transmit shift register is empty, not be done when the
2853 * transmit holding register is empty. This functionality
2854 * allows an RS485 driver to be written in user space.
2856 static int mxser_get_lsr_info(struct mxser_struct *info, unsigned int __user *value)
2858 unsigned char status;
2859 unsigned int result;
2860 unsigned long flags;
2862 spin_lock_irqsave(&info->slock, flags);
2863 status = inb(info->base + UART_LSR);
2864 spin_unlock_irqrestore(&info->slock, flags);
2865 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
2866 return put_user(result, value);
2870 * This routine sends a break character out the serial port.
2872 static void mxser_send_break(struct mxser_struct *info, int duration)
2874 unsigned long flags;
2878 set_current_state(TASK_INTERRUPTIBLE);
2879 spin_lock_irqsave(&info->slock, flags);
2880 outb(inb(info->base + UART_LCR) | UART_LCR_SBC, info->base + UART_LCR);
2881 spin_unlock_irqrestore(&info->slock, flags);
2882 schedule_timeout(duration);
2883 spin_lock_irqsave(&info->slock, flags);
2884 outb(inb(info->base + UART_LCR) & ~UART_LCR_SBC, info->base + UART_LCR);
2885 spin_unlock_irqrestore(&info->slock, flags);
2888 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
2890 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
2891 unsigned char control, status;
2892 unsigned long flags;
2895 if (tty->index == MXSER_PORTS)
2896 return (-ENOIOCTLCMD);
2897 if (tty->flags & (1 << TTY_IO_ERROR))
2900 control = info->MCR;
2902 spin_lock_irqsave(&info->slock, flags);
2903 status = inb(info->base + UART_MSR);
2904 if (status & UART_MSR_ANY_DELTA)
2905 mxser_check_modem_status(info, status);
2906 spin_unlock_irqrestore(&info->slock, flags);
2907 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
2908 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) | ((status & UART_MSR_RI) ? TIOCM_RNG : 0) | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
2911 static int mxser_tiocmset(struct tty_struct *tty, struct file *file, unsigned int set, unsigned int clear)
2913 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
2914 unsigned long flags;
2917 if (tty->index == MXSER_PORTS)
2918 return -ENOIOCTLCMD;
2919 if (tty->flags & (1 << TTY_IO_ERROR))
2922 spin_lock_irqsave(&info->slock, flags);
2924 if (set & TIOCM_RTS)
2925 info->MCR |= UART_MCR_RTS;
2926 if (set & TIOCM_DTR)
2927 info->MCR |= UART_MCR_DTR;
2929 if (clear & TIOCM_RTS)
2930 info->MCR &= ~UART_MCR_RTS;
2931 if (clear & TIOCM_DTR)
2932 info->MCR &= ~UART_MCR_DTR;
2934 outb(info->MCR, info->base + UART_MCR);
2935 spin_unlock_irqrestore(&info->slock, flags);
2940 static int mxser_read_register(int, unsigned short *);
2941 static int mxser_program_mode(int);
2942 static void mxser_normal_mode(int);
2944 static int mxser_get_ISA_conf(int cap, struct mxser_hwconf *hwconf)
2947 unsigned short regs[16], irq;
2948 unsigned char scratch, scratch2;
2950 hwconf->IsMoxaMustChipFlag = MOXA_OTHER_UART;
2952 id = mxser_read_register(cap, regs);
2953 if (id == C168_ASIC_ID) {
2954 hwconf->board_type = MXSER_BOARD_C168_ISA;
2956 } else if (id == C104_ASIC_ID) {
2957 hwconf->board_type = MXSER_BOARD_C104_ISA;
2959 } else if (id == C102_ASIC_ID) {
2960 hwconf->board_type = MXSER_BOARD_C102_ISA;
2962 } else if (id == CI132_ASIC_ID) {
2963 hwconf->board_type = MXSER_BOARD_CI132;
2965 } else if (id == CI134_ASIC_ID) {
2966 hwconf->board_type = MXSER_BOARD_CI134;
2968 } else if (id == CI104J_ASIC_ID) {
2969 hwconf->board_type = MXSER_BOARD_CI104J;
2975 if (hwconf->ports == 2) {
2976 irq = regs[9] & 0xF000;
2977 irq = irq | (irq >> 4);
2978 if (irq != (regs[9] & 0xFF00))
2979 return (MXSER_ERR_IRQ_CONFLIT);
2980 } else if (hwconf->ports == 4) {
2981 irq = regs[9] & 0xF000;
2982 irq = irq | (irq >> 4);
2983 irq = irq | (irq >> 8);
2985 return (MXSER_ERR_IRQ_CONFLIT);
2986 } else if (hwconf->ports == 8) {
2987 irq = regs[9] & 0xF000;
2988 irq = irq | (irq >> 4);
2989 irq = irq | (irq >> 8);
2990 if ((irq != regs[9]) || (irq != regs[10]))
2991 return (MXSER_ERR_IRQ_CONFLIT);
2995 return (MXSER_ERR_IRQ);
2997 hwconf->irq = ((int) (irq & 0xF000) >> 12);
2998 for (i = 0; i < 8; i++)
2999 hwconf->ioaddr[i] = (int) regs[i + 1] & 0xFFF8;
3000 if ((regs[12] & 0x80) == 0) {
3001 return (MXSER_ERR_VECTOR);
3003 hwconf->vector = (int) regs[11]; /* interrupt vector */
3005 hwconf->vector_mask = 0x00FF;
3007 hwconf->vector_mask = 0x000F;
3008 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
3009 if (regs[12] & bits) {
3010 hwconf->baud_base[i] = 921600;
3011 hwconf->MaxCanSetBaudRate[i] = 921600; // add by Victor Yu. 09-04-2002
3013 hwconf->baud_base[i] = 115200;
3014 hwconf->MaxCanSetBaudRate[i] = 115200; // add by Victor Yu. 09-04-2002
3017 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
3018 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
3019 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
3020 outb(scratch2, cap + UART_LCR);
3021 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
3022 scratch = inb(cap + UART_IIR);
3025 hwconf->uart_type = PORT_16550A;
3027 hwconf->uart_type = PORT_16450;
3032 request_region(hwconf->ioaddr[0], 8 * hwconf->ports, "mxser(IO)");
3033 request_region(hwconf->vector, 1, "mxser(vector)");
3034 return (hwconf->ports);
3037 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
3038 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
3039 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
3040 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
3041 #define EN_CCMD 0x000 /* Chip's command register */
3042 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
3043 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
3044 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
3045 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
3046 #define EN0_DCFG 0x00E /* Data configuration reg WR */
3047 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
3048 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
3049 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
3050 static int mxser_read_register(int port, unsigned short *regs)
3052 int i, k, value, id;
3055 id = mxser_program_mode(port);
3058 for (i = 0; i < 14; i++) {
3059 k = (i & 0x3F) | 0x180;
3060 for (j = 0x100; j > 0; j >>= 1) {
3061 outb(CHIP_CS, port);
3063 outb(CHIP_CS | CHIP_DO, port);
3064 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
3066 outb(CHIP_CS, port);
3067 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
3072 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
3073 outb(CHIP_CS, port);
3074 outb(CHIP_CS | CHIP_SK, port);
3075 if (inb(port) & CHIP_DI)
3081 mxser_normal_mode(port);
3085 static int mxser_program_mode(int port)
3088 //unsigned long flags;
3090 spin_lock(&gm_lock);
3098 //restore_flags(flags);
3099 spin_unlock(&gm_lock);
3101 id = inb(port + 1) & 0x1F;
3102 if ((id != C168_ASIC_ID) && (id != C104_ASIC_ID) && (id != C102_ASIC_ID) && (id != CI132_ASIC_ID) && (id != CI134_ASIC_ID) && (id != CI104J_ASIC_ID))
3104 for (i = 0, j = 0; i < 4; i++) {
3108 } else if ((j == 1) && (n == 1)) {
3119 static void mxser_normal_mode(int port)
3123 outb(0xA5, port + 1);
3124 outb(0x80, port + 3);
3125 outb(12, port + 0); /* 9600 bps */
3127 outb(0x03, port + 3); /* 8 data bits */
3128 outb(0x13, port + 4); /* loop back mode */
3129 for (i = 0; i < 16; i++) {
3131 if ((n & 0x61) == 0x60)
3136 outb(0x00, port + 4);
3139 module_init(mxser_module_init);
3140 module_exit(mxser_module_exit);