2 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
4 * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com.tw).
6 * This code is loosely based on the Linux serial driver, written by
7 * Linus Torvalds, Theodore T'so and others.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
24 * <alan@redhat.com>. The original 1.8 code is available on www.moxa.com.
25 * - Fixed x86_64 cleanness
26 * - Fixed sleep with spinlock held in mxser_send_break
30 #include <linux/module.h>
31 #include <linux/autoconf.h>
32 #include <linux/errno.h>
33 #include <linux/signal.h>
34 #include <linux/sched.h>
35 #include <linux/timer.h>
36 #include <linux/interrupt.h>
37 #include <linux/tty.h>
38 #include <linux/tty_flip.h>
39 #include <linux/serial.h>
40 #include <linux/serial_reg.h>
41 #include <linux/major.h>
42 #include <linux/string.h>
43 #include <linux/fcntl.h>
44 #include <linux/ptrace.h>
45 #include <linux/gfp.h>
46 #include <linux/ioport.h>
48 #include <linux/smp_lock.h>
49 #include <linux/delay.h>
50 #include <linux/pci.h>
52 #include <asm/system.h>
55 #include <asm/bitops.h>
56 #include <asm/uaccess.h>
58 #include "mxser_new.h"
60 #define MXSER_VERSION "1.9.1"
61 #define MXSERMAJOR 174
62 #define MXSERCUMAJOR 175
64 #define MXSER_EVENT_TXLOW 1
65 #define MXSER_EVENT_HANGUP 2
67 #define MXSER_BOARDS 4 /* Max. boards */
68 #define MXSER_PORTS 32 /* Max. ports */
69 #define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
70 #define MXSER_ISR_PASS_LIMIT 99999L
72 #define MXSER_ERR_IOADDR -1
73 #define MXSER_ERR_IRQ -2
74 #define MXSER_ERR_IRQ_CONFLIT -3
75 #define MXSER_ERR_VECTOR -4
77 #define SERIAL_TYPE_NORMAL 1
78 #define SERIAL_TYPE_CALLOUT 2
80 #define WAKEUP_CHARS 256
82 #define UART_MCR_AFE 0x20
83 #define UART_LSR_SPECIAL 0x1E
85 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|\
88 #define IRQ_T(info) ((info->flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED : IRQF_DISABLED)
90 #define C168_ASIC_ID 1
91 #define C104_ASIC_ID 2
92 #define C102_ASIC_ID 0xB
93 #define CI132_ASIC_ID 4
94 #define CI134_ASIC_ID 3
95 #define CI104J_ASIC_ID 5
98 MXSER_BOARD_C168_ISA = 1,
101 MXSER_BOARD_C168_PCI,
102 MXSER_BOARD_C104_PCI,
103 MXSER_BOARD_C102_ISA,
124 static char *mxser_brdname[] = {
142 "Moxa UC7000 Serial",
151 static int mxser_numports[] = {
178 #define UART_TYPE_NUM 2
180 static const unsigned int Gmoxa_uart_id[UART_TYPE_NUM] = {
181 MOXA_MUST_MU150_HWID,
185 /* This is only for PCI */
186 #define UART_INFO_NUM 3
187 struct mxpciuart_info {
198 static const struct mxpciuart_info Gpci_uart_info[UART_INFO_NUM] = {
199 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
200 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
201 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
207 static struct pci_device_id mxser_pcibrds[] = {
208 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C168),
209 .driver_data = MXSER_BOARD_C168_PCI },
210 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C104),
211 .driver_data = MXSER_BOARD_C104_PCI },
212 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132),
213 .driver_data = MXSER_BOARD_CP132 },
214 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP114),
215 .driver_data = MXSER_BOARD_CP114 },
216 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CT114),
217 .driver_data = MXSER_BOARD_CT114 },
218 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102),
219 .driver_data = MXSER_BOARD_CP102 },
220 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104U),
221 .driver_data = MXSER_BOARD_CP104U },
222 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168U),
223 .driver_data = MXSER_BOARD_CP168U },
224 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132U),
225 .driver_data = MXSER_BOARD_CP132U },
226 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP134U),
227 .driver_data = MXSER_BOARD_CP134U },
228 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104JU),
229 .driver_data = MXSER_BOARD_CP104JU },
230 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_RC7000),
231 .driver_data = MXSER_BOARD_RC7000 },
232 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118U),
233 .driver_data = MXSER_BOARD_CP118U },
234 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102UL),
235 .driver_data = MXSER_BOARD_CP102UL },
236 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102U),
237 .driver_data = MXSER_BOARD_CP102U },
238 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118EL),
239 .driver_data = MXSER_BOARD_CP118EL },
240 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168EL),
241 .driver_data = MXSER_BOARD_CP168EL },
242 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104EL),
243 .driver_data = MXSER_BOARD_CP104EL },
247 MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
252 typedef struct _moxa_pci_info {
253 unsigned short busNum;
254 unsigned short devNum;
255 struct pci_dev *pdev; /* add by Victor Yu. 06-23-2003 */
258 static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
259 static int ttymajor = MXSERMAJOR;
260 static int calloutmajor = MXSERCUMAJOR;
261 static int verbose = 0;
263 /* Variables for insmod */
265 MODULE_AUTHOR("Casper Yang");
266 MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
267 module_param_array(ioaddr, int, NULL, 0);
268 module_param(ttymajor, int, 0);
269 module_param(verbose, bool, 0);
270 MODULE_LICENSE("GPL");
274 unsigned long rxcnt[MXSER_PORTS];
275 unsigned long txcnt[MXSER_PORTS];
282 unsigned long up_rxcnt;
283 unsigned long up_txcnt;
285 unsigned char hold_reason;
288 struct mxser_mon_ext {
289 unsigned long rx_cnt[32];
290 unsigned long tx_cnt[32];
291 unsigned long up_rxcnt[32];
292 unsigned long up_txcnt[32];
293 int modem_status[32];
304 struct mxser_hwconf {
308 unsigned long vector;
309 unsigned long vector_mask;
311 unsigned long ioaddr[MXSER_PORTS_PER_BOARD];
312 int baud_base[MXSER_PORTS_PER_BOARD];
313 moxa_pci_info pciInfo;
314 int IsMoxaMustChipFlag; /* add by Victor Yu. 08-30-2002 */
315 int MaxCanSetBaudRate[MXSER_PORTS_PER_BOARD]; /* add by Victor Yu. 09-04-2002 */
316 unsigned long opmode_ioaddr[MXSER_PORTS_PER_BOARD]; /* add by Victor Yu. 01-05-2004 */
319 struct mxser_struct {
321 unsigned long base; /* port base address */
322 int irq; /* port using irq no. */
323 unsigned long vector; /* port irq vector */
324 unsigned long vectormask; /* port vector mask */
326 int rx_trigger; /* Rx fifo trigger level */
328 int baud_base; /* max. speed */
329 int flags; /* defined in tty.h */
330 int type; /* UART type */
331 struct tty_struct *tty;
332 int read_status_mask;
333 int ignore_status_mask;
336 int x_char; /* xon/xoff character */
338 unsigned short closing_wait;
339 int IER; /* Interrupt Enable Register */
340 int MCR; /* Modem control register */
342 int count; /* # of fd on device */
343 int blocked_open; /* # of blocked opens */
344 long session; /* Session of opening process */
345 long pgrp; /* pgrp of opening process */
346 unsigned char *xmit_buf;
350 struct work_struct tqueue;
351 struct termios normal_termios;
352 struct termios callout_termios;
353 wait_queue_head_t open_wait;
354 wait_queue_head_t close_wait;
355 wait_queue_head_t delta_msr_wait;
356 struct async_icount icount; /* kernel counters for the 4 input interrupts */
358 int IsMoxaMustChipFlag; /* add by Victor Yu. 08-30-2002 */
359 int MaxCanSetBaudRate; /* add by Victor Yu. 09-04-2002 */
360 unsigned long opmode_ioaddr; /* add by Victor Yu. 01-05-2004 */
361 unsigned char stop_rx;
362 unsigned char ldisc_stop_rx;
364 struct mxser_mon mon_data;
365 unsigned char err_shadow;
369 struct mxser_mstatus {
377 static struct mxser_mstatus GMStatus[MXSER_PORTS];
379 static int mxserBoardCAP[MXSER_BOARDS] = {
381 /* 0x180, 0x280, 0x200, 0x320 */
384 static struct tty_driver *mxvar_sdriver;
385 static struct mxser_struct mxvar_table[MXSER_PORTS];
386 static struct tty_struct *mxvar_tty[MXSER_PORTS + 1];
387 static struct termios *mxvar_termios[MXSER_PORTS + 1];
388 static struct termios *mxvar_termios_locked[MXSER_PORTS + 1];
389 static struct mxser_log mxvar_log;
390 static int mxvar_diagflag;
391 static unsigned char mxser_msr[MXSER_PORTS + 1];
392 static struct mxser_mon_ext mon_data_ext;
393 static int mxser_set_baud_method[MXSER_PORTS + 1];
394 static spinlock_t gm_lock;
397 * This is used to figure out the divisor speeds and the timeouts
400 static struct mxser_hwconf mxsercfg[MXSER_BOARDS];
406 static void mxser_getcfg(int board, struct mxser_hwconf *hwconf);
407 static int mxser_init(void);
409 /* static void mxser_poll(unsigned long); */
410 static int mxser_get_ISA_conf(int, struct mxser_hwconf *);
411 static int mxser_get_PCI_conf(int, int, int, struct mxser_hwconf *);
412 static void mxser_do_softint(void *);
413 static int mxser_open(struct tty_struct *, struct file *);
414 static void mxser_close(struct tty_struct *, struct file *);
415 static int mxser_write(struct tty_struct *, const unsigned char *, int);
416 static int mxser_write_room(struct tty_struct *);
417 static void mxser_flush_buffer(struct tty_struct *);
418 static int mxser_chars_in_buffer(struct tty_struct *);
419 static void mxser_flush_chars(struct tty_struct *);
420 static void mxser_put_char(struct tty_struct *, unsigned char);
421 static int mxser_ioctl(struct tty_struct *, struct file *, uint, ulong);
422 static int mxser_ioctl_special(unsigned int, void __user *);
423 static void mxser_throttle(struct tty_struct *);
424 static void mxser_unthrottle(struct tty_struct *);
425 static void mxser_set_termios(struct tty_struct *, struct termios *);
426 static void mxser_stop(struct tty_struct *);
427 static void mxser_start(struct tty_struct *);
428 static void mxser_hangup(struct tty_struct *);
429 static void mxser_rs_break(struct tty_struct *, int);
430 static irqreturn_t mxser_interrupt(int, void *, struct pt_regs *);
431 static void mxser_receive_chars(struct mxser_struct *, int *);
432 static void mxser_transmit_chars(struct mxser_struct *);
433 static void mxser_check_modem_status(struct mxser_struct *, int);
434 static int mxser_block_til_ready(struct tty_struct *, struct file *, struct mxser_struct *);
435 static int mxser_startup(struct mxser_struct *);
436 static void mxser_shutdown(struct mxser_struct *);
437 static int mxser_change_speed(struct mxser_struct *, struct termios *old_termios);
438 static int mxser_get_serial_info(struct mxser_struct *, struct serial_struct __user *);
439 static int mxser_set_serial_info(struct mxser_struct *, struct serial_struct __user *);
440 static int mxser_get_lsr_info(struct mxser_struct *, unsigned int __user *);
441 static void mxser_send_break(struct mxser_struct *, int);
442 static int mxser_tiocmget(struct tty_struct *, struct file *);
443 static int mxser_tiocmset(struct tty_struct *, struct file *, unsigned int, unsigned int);
444 static int mxser_set_baud(struct mxser_struct *info, long newspd);
445 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout);
447 static void mxser_startrx(struct tty_struct *tty);
448 static void mxser_stoprx(struct tty_struct *tty);
451 static int CheckIsMoxaMust(int io)
456 outb(0, io + UART_LCR);
457 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
458 oldmcr = inb(io + UART_MCR);
459 outb(0, io + UART_MCR);
460 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
461 if ((hwid = inb(io + UART_MCR)) != 0) {
462 outb(oldmcr, io + UART_MCR);
463 return MOXA_OTHER_UART;
466 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
467 for (i = 0; i < UART_TYPE_NUM; i++) {
468 if (hwid == Gmoxa_uart_id[i])
471 return MOXA_OTHER_UART;
474 /* above is modified by Victor Yu. 08-15-2002 */
476 static const struct tty_operations mxser_ops = {
478 .close = mxser_close,
479 .write = mxser_write,
480 .put_char = mxser_put_char,
481 .flush_chars = mxser_flush_chars,
482 .write_room = mxser_write_room,
483 .chars_in_buffer = mxser_chars_in_buffer,
484 .flush_buffer = mxser_flush_buffer,
485 .ioctl = mxser_ioctl,
486 .throttle = mxser_throttle,
487 .unthrottle = mxser_unthrottle,
488 .set_termios = mxser_set_termios,
490 .start = mxser_start,
491 .hangup = mxser_hangup,
492 .break_ctl = mxser_rs_break,
493 .wait_until_sent = mxser_wait_until_sent,
494 .tiocmget = mxser_tiocmget,
495 .tiocmset = mxser_tiocmset,
499 * The MOXA Smartio/Industio serial driver boot-time initialization code!
502 static int __init mxser_module_init(void)
507 printk(KERN_DEBUG "Loading module mxser ...\n");
508 printk(KERN_INFO "This is mxser driver version 1.9.1 and needs TESTING."
509 "If your are willing to test this driver, please report to "
510 "jirislaby@gmail.com. Thanks.\n");
513 printk(KERN_DEBUG "Done.\n");
517 static void __exit mxser_module_exit(void)
522 printk(KERN_DEBUG "Unloading module mxser ...\n");
524 err = tty_unregister_driver(mxvar_sdriver);
526 put_tty_driver(mxvar_sdriver);
528 printk(KERN_ERR "Couldn't unregister MOXA Smartio/Industio family serial driver\n");
530 for (i = 0; i < MXSER_BOARDS; i++) {
531 struct pci_dev *pdev;
533 if (mxsercfg[i].board_type == -1)
536 pdev = mxsercfg[i].pciInfo.pdev;
537 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
538 if (pdev != NULL) { /* PCI */
539 release_region(pci_resource_start(pdev, 2), pci_resource_len(pdev, 2));
540 release_region(pci_resource_start(pdev, 3), pci_resource_len(pdev, 3));
543 release_region(mxsercfg[i].ioaddr[0], 8 * mxsercfg[i].ports);
544 release_region(mxsercfg[i].vector, 1);
549 printk(KERN_DEBUG "Done.\n");
552 static void process_txrx_fifo(struct mxser_struct *info)
556 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
557 info->rx_trigger = 1;
558 info->rx_high_water = 1;
559 info->rx_low_water = 1;
560 info->xmit_fifo_size = 1;
562 for (i = 0; i < UART_INFO_NUM; i++) {
563 if (info->IsMoxaMustChipFlag == Gpci_uart_info[i].type) {
564 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
565 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
566 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
567 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
574 static int mxser_initbrd(int board, struct mxser_hwconf *hwconf)
576 struct mxser_struct *info;
580 n = board * MXSER_PORTS_PER_BOARD;
581 info = &mxvar_table[n];
583 printk(KERN_DEBUG " ttyM%d - ttyM%d ",
584 n, n + hwconf->ports - 1);
585 printk(" max. baud rate = %d bps.\n",
586 hwconf->MaxCanSetBaudRate[0]);
589 for (i = 0; i < hwconf->ports; i++, n++, info++) {
591 info->base = hwconf->ioaddr[i];
592 info->irq = hwconf->irq;
593 info->vector = hwconf->vector;
594 info->vectormask = hwconf->vector_mask;
595 info->opmode_ioaddr = hwconf->opmode_ioaddr[i]; /* add by Victor Yu. 01-05-2004 */
597 info->ldisc_stop_rx = 0;
599 info->IsMoxaMustChipFlag = hwconf->IsMoxaMustChipFlag;
600 /* Enhance mode enabled here */
601 if (info->IsMoxaMustChipFlag != MOXA_OTHER_UART) {
602 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->base);
605 info->flags = ASYNC_SHARE_IRQ;
606 info->type = hwconf->uart_type;
607 info->baud_base = hwconf->baud_base[i];
609 info->MaxCanSetBaudRate = hwconf->MaxCanSetBaudRate[i];
611 process_txrx_fifo(info);
614 info->custom_divisor = hwconf->baud_base[i] * 16;
615 info->close_delay = 5 * HZ / 10;
616 info->closing_wait = 30 * HZ;
617 INIT_WORK(&info->tqueue, mxser_do_softint, info);
618 info->normal_termios = mxvar_sdriver->init_termios;
619 init_waitqueue_head(&info->open_wait);
620 init_waitqueue_head(&info->close_wait);
621 init_waitqueue_head(&info->delta_msr_wait);
622 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
623 info->err_shadow = 0;
624 spin_lock_init(&info->slock);
627 * Allocate the IRQ if necessary
631 /* before set INT ISR, disable all int */
632 for (i = 0; i < hwconf->ports; i++) {
633 outb(inb(hwconf->ioaddr[i] + UART_IER) & 0xf0,
634 hwconf->ioaddr[i] + UART_IER);
637 n = board * MXSER_PORTS_PER_BOARD;
638 info = &mxvar_table[n];
640 retval = request_irq(hwconf->irq, mxser_interrupt, IRQ_T(info),
643 printk(KERN_ERR "Board %d: %s",
644 board, mxser_brdname[hwconf->board_type - 1]);
645 printk(" Request irq failed, IRQ (%d) may conflict with"
646 " another device.\n", info->irq);
652 static void mxser_getcfg(int board, struct mxser_hwconf *hwconf)
654 mxsercfg[board] = *hwconf;
658 static int mxser_get_PCI_conf(int busnum, int devnum, int board_type, struct mxser_hwconf *hwconf)
661 /* unsigned int val; */
662 unsigned long ioaddress;
663 struct pci_dev *pdev = hwconf->pciInfo.pdev;
666 hwconf->board_type = board_type;
667 hwconf->ports = mxser_numports[board_type - 1];
668 ioaddress = pci_resource_start(pdev, 2);
669 request_region(pci_resource_start(pdev, 2), pci_resource_len(pdev, 2),
672 for (i = 0; i < hwconf->ports; i++)
673 hwconf->ioaddr[i] = ioaddress + 8 * i;
676 ioaddress = pci_resource_start(pdev, 3);
677 request_region(pci_resource_start(pdev, 3), pci_resource_len(pdev, 3),
679 hwconf->vector = ioaddress;
682 hwconf->irq = hwconf->pciInfo.pdev->irq;
684 hwconf->IsMoxaMustChipFlag = CheckIsMoxaMust(hwconf->ioaddr[0]);
685 hwconf->uart_type = PORT_16550A;
686 hwconf->vector_mask = 0;
689 for (i = 0; i < hwconf->ports; i++) {
690 for (j = 0; j < UART_INFO_NUM; j++) {
691 if (Gpci_uart_info[j].type == hwconf->IsMoxaMustChipFlag) {
692 hwconf->MaxCanSetBaudRate[i] = Gpci_uart_info[j].max_baud;
694 /* exception....CP-102 */
695 if (board_type == MXSER_BOARD_CP102)
696 hwconf->MaxCanSetBaudRate[i] = 921600;
702 if (hwconf->IsMoxaMustChipFlag == MOXA_MUST_MU860_HWID) {
703 for (i = 0; i < hwconf->ports; i++) {
705 hwconf->opmode_ioaddr[i] = ioaddress + 4;
707 hwconf->opmode_ioaddr[i] = ioaddress + 0x0c;
709 outb(0, ioaddress + 4); /* default set to RS232 mode */
710 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
713 for (i = 0; i < hwconf->ports; i++) {
714 hwconf->vector_mask |= (1 << i);
715 hwconf->baud_base[i] = 921600;
721 static int mxser_init(void)
723 int i, m, retval, b, n;
724 struct pci_dev *pdev = NULL;
726 unsigned char busnum, devnum;
727 struct mxser_hwconf hwconf;
729 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
732 spin_lock_init(&gm_lock);
734 for (i = 0; i < MXSER_BOARDS; i++) {
735 mxsercfg[i].board_type = -1;
738 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
741 /* Initialize the tty_driver structure */
742 memset(mxvar_sdriver, 0, sizeof(struct tty_driver));
743 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
744 mxvar_sdriver->name = "ttyM";
745 mxvar_sdriver->major = ttymajor;
746 mxvar_sdriver->minor_start = 0;
747 mxvar_sdriver->num = MXSER_PORTS + 1;
748 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
749 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
750 mxvar_sdriver->init_termios = tty_std_termios;
751 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
752 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
753 tty_set_operations(mxvar_sdriver, &mxser_ops);
754 mxvar_sdriver->ttys = mxvar_tty;
755 mxvar_sdriver->termios = mxvar_termios;
756 mxvar_sdriver->termios_locked = mxvar_termios_locked;
759 memset(mxvar_table, 0, MXSER_PORTS * sizeof(struct mxser_struct));
760 memset(&mxvar_log, 0, sizeof(struct mxser_log));
762 memset(&mxser_msr, 0, sizeof(unsigned char) * (MXSER_PORTS + 1));
763 memset(&mon_data_ext, 0, sizeof(struct mxser_mon_ext));
764 memset(&mxser_set_baud_method, 0, sizeof(int) * (MXSER_PORTS + 1));
765 memset(&hwconf, 0, sizeof(struct mxser_hwconf));
768 /* Start finding ISA boards here */
769 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
772 if (!(cap = mxserBoardCAP[b]))
775 retval = mxser_get_ISA_conf(cap, &hwconf);
778 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
779 mxser_brdname[hwconf.board_type - 1], ioaddr[b]);
782 if (retval == MXSER_ERR_IRQ)
783 printk(KERN_ERR "Invalid interrupt number, "
784 "board not configured\n");
785 else if (retval == MXSER_ERR_IRQ_CONFLIT)
786 printk(KERN_ERR "Invalid interrupt number, "
787 "board not configured\n");
788 else if (retval == MXSER_ERR_VECTOR)
789 printk(KERN_ERR "Invalid interrupt vector, "
790 "board not configured\n");
791 else if (retval == MXSER_ERR_IOADDR)
792 printk(KERN_ERR "Invalid I/O address, "
793 "board not configured\n");
798 hwconf.pciInfo.busNum = 0;
799 hwconf.pciInfo.devNum = 0;
800 hwconf.pciInfo.pdev = NULL;
802 mxser_getcfg(m, &hwconf);
804 * init mxsercfg first,
805 * or mxsercfg data is not correct on ISR.
807 /* mxser_initbrd will hook ISR. */
808 if (mxser_initbrd(m, &hwconf) < 0)
814 /* Start finding ISA boards from module arg */
815 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
818 if (!(cap = ioaddr[b]))
821 retval = mxser_get_ISA_conf(cap, &hwconf);
824 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
825 mxser_brdname[hwconf.board_type - 1], ioaddr[b]);
828 if (retval == MXSER_ERR_IRQ)
829 printk(KERN_ERR "Invalid interrupt number, "
830 "board not configured\n");
831 else if (retval == MXSER_ERR_IRQ_CONFLIT)
832 printk(KERN_ERR "Invalid interrupt number, "
833 "board not configured\n");
834 else if (retval == MXSER_ERR_VECTOR)
835 printk(KERN_ERR "Invalid interrupt vector, "
836 "board not configured\n");
837 else if (retval == MXSER_ERR_IOADDR)
838 printk(KERN_ERR "Invalid I/O address, "
839 "board not configured\n");
844 hwconf.pciInfo.busNum = 0;
845 hwconf.pciInfo.devNum = 0;
846 hwconf.pciInfo.pdev = NULL;
848 mxser_getcfg(m, &hwconf);
850 * init mxsercfg first,
851 * or mxsercfg data is not correct on ISR.
853 /* mxser_initbrd will hook ISR. */
854 if (mxser_initbrd(m, &hwconf) < 0)
860 /* start finding PCI board here */
862 n = ARRAY_SIZE(mxser_pcibrds) - 1;
866 pdev = pci_get_device(mxser_pcibrds[b].vendor,
867 mxser_pcibrds[b].device, pdev);
872 hwconf.pciInfo.busNum = busnum = pdev->bus->number;
873 hwconf.pciInfo.devNum = devnum = PCI_SLOT(pdev->devfn) << 3;
874 hwconf.pciInfo.pdev = pdev;
875 printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
876 mxser_brdname[(int) (mxser_pcibrds[b].driver_data) - 1],
877 busnum, devnum >> 3);
879 if (m >= MXSER_BOARDS)
881 "Too many Smartio/Industio family boards find "
882 "(maximum %d), board not configured\n",
885 if (pci_enable_device(pdev)) {
886 printk(KERN_ERR "Moxa SmartI/O PCI enable "
890 retval = mxser_get_PCI_conf(busnum, devnum,
891 (int)mxser_pcibrds[b].driver_data,
894 if (retval == MXSER_ERR_IRQ)
896 "Invalid interrupt number, "
897 "board not configured\n");
898 else if (retval == MXSER_ERR_IRQ_CONFLIT)
900 "Invalid interrupt number, "
901 "board not configured\n");
902 else if (retval == MXSER_ERR_VECTOR)
904 "Invalid interrupt vector, "
905 "board not configured\n");
906 else if (retval == MXSER_ERR_IOADDR)
908 "Invalid I/O address, "
909 "board not configured\n");
912 mxser_getcfg(m, &hwconf);
913 /* init mxsercfg first,
914 * or mxsercfg data is not correct on ISR.
916 /* mxser_initbrd will hook ISR. */
917 if (mxser_initbrd(m, &hwconf) < 0)
920 /* Keep an extra reference if we succeeded. It will
921 be returned at unload time */
927 retval = tty_register_driver(mxvar_sdriver);
929 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family"
931 put_tty_driver(mxvar_sdriver);
933 for (i = 0; i < MXSER_BOARDS; i++) {
934 if (mxsercfg[i].board_type == -1)
937 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
938 /* todo: release io, vector */
947 static void mxser_do_softint(void *private_)
949 struct mxser_struct *info = private_;
950 struct tty_struct *tty;
954 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
956 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event))
960 static unsigned char mxser_get_msr(int baseaddr, int mode, int port, struct mxser_struct *info)
962 unsigned char status = 0;
964 status = inb(baseaddr + UART_MSR);
966 mxser_msr[port] &= 0x0F;
967 mxser_msr[port] |= status;
968 status = mxser_msr[port];
976 * This routine is called whenever a serial port is opened. It
977 * enables interrupts for a serial port, linking in its async structure into
978 * the IRQ chain. It also performs the serial-specific
979 * initialization for the tty structure.
981 static int mxser_open(struct tty_struct *tty, struct file *filp)
983 struct mxser_struct *info;
986 /* initialize driver_data in case something fails */
987 tty->driver_data = NULL;
990 if (line == MXSER_PORTS)
992 if (line < 0 || line > MXSER_PORTS)
994 info = mxvar_table + line;
998 tty->driver_data = info;
1001 * Start up serial port
1004 retval = mxser_startup(info);
1008 retval = mxser_block_til_ready(tty, filp, info);
1012 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
1013 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
1014 *tty->termios = info->normal_termios;
1016 *tty->termios = info->callout_termios;
1017 mxser_change_speed(info, NULL);
1020 info->session = process_session(current);
1021 info->pgrp = process_group(current);
1024 status = mxser_get_msr(info->base, 0, info->port);
1025 mxser_check_modem_status(info, status);
1028 /* unmark here for very high baud rate (ex. 921600 bps) used */
1029 tty->low_latency = 1;
1034 * This routine is called when the serial port gets closed. First, we
1035 * wait for the last remaining data to be sent. Then, we unlink its
1036 * async structure from the interrupt chain if necessary, and we free
1037 * that IRQ if nothing is left in the chain.
1039 static void mxser_close(struct tty_struct *tty, struct file *filp)
1041 struct mxser_struct *info = tty->driver_data;
1043 unsigned long timeout;
1044 unsigned long flags;
1045 struct tty_ldisc *ld;
1047 if (tty->index == MXSER_PORTS)
1052 spin_lock_irqsave(&info->slock, flags);
1054 if (tty_hung_up_p(filp)) {
1055 spin_unlock_irqrestore(&info->slock, flags);
1058 if ((tty->count == 1) && (info->count != 1)) {
1060 * Uh, oh. tty->count is 1, which means that the tty
1061 * structure will be freed. Info->count should always
1062 * be one in these conditions. If it's greater than
1063 * one, we've got real problems, since it means the
1064 * serial port won't be shutdown.
1066 printk(KERN_ERR "mxser_close: bad serial port count; "
1067 "tty->count is 1, info->count is %d\n", info->count);
1070 if (--info->count < 0) {
1071 printk(KERN_ERR "mxser_close: bad serial port count for "
1072 "ttys%d: %d\n", info->port, info->count);
1076 spin_unlock_irqrestore(&info->slock, flags);
1079 info->flags |= ASYNC_CLOSING;
1080 spin_unlock_irqrestore(&info->slock, flags);
1082 * Save the termios structure, since this port may have
1083 * separate termios for callout and dialin.
1085 if (info->flags & ASYNC_NORMAL_ACTIVE)
1086 info->normal_termios = *tty->termios;
1088 * Now we wait for the transmit buffer to clear; and we notify
1089 * the line discipline to only process XON/XOFF characters.
1092 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1093 tty_wait_until_sent(tty, info->closing_wait);
1095 * At this point we stop accepting input. To do this, we
1096 * disable the receive line status interrupts, and tell the
1097 * interrupt driver to stop checking the data ready bit in the
1098 * line status register.
1100 info->IER &= ~UART_IER_RLSI;
1101 if (info->IsMoxaMustChipFlag)
1102 info->IER &= ~MOXA_MUST_RECV_ISR;
1104 info->read_status_mask &= ~UART_LSR_DR;
1106 if (info->flags & ASYNC_INITIALIZED) {
1107 outb(info->IER, info->base + UART_IER);
1109 * Before we drop DTR, make sure the UART transmitter
1110 * has completely drained; this is especially
1111 * important if there is a transmit FIFO!
1113 timeout = jiffies + HZ;
1114 while (!(inb(info->base + UART_LSR) & UART_LSR_TEMT)) {
1115 schedule_timeout_interruptible(5);
1116 if (time_after(jiffies, timeout))
1120 mxser_shutdown(info);
1122 if (tty->driver->flush_buffer)
1123 tty->driver->flush_buffer(tty);
1125 ld = tty_ldisc_ref(tty);
1127 if (ld->flush_buffer)
1128 ld->flush_buffer(tty);
1129 tty_ldisc_deref(ld);
1135 if (info->blocked_open) {
1136 if (info->close_delay)
1137 schedule_timeout_interruptible(info->close_delay);
1138 wake_up_interruptible(&info->open_wait);
1141 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1142 wake_up_interruptible(&info->close_wait);
1146 static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1149 struct mxser_struct *info = tty->driver_data;
1150 unsigned long flags;
1152 if (!info->xmit_buf)
1156 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1157 SERIAL_XMIT_SIZE - info->xmit_head));
1161 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1162 spin_lock_irqsave(&info->slock, flags);
1163 info->xmit_head = (info->xmit_head + c) &
1164 (SERIAL_XMIT_SIZE - 1);
1165 info->xmit_cnt += c;
1166 spin_unlock_irqrestore(&info->slock, flags);
1173 if (info->xmit_cnt && !tty->stopped
1174 /*&& !(info->IER & UART_IER_THRI)*/) {
1175 if (!tty->hw_stopped ||
1176 (info->type == PORT_16550A) ||
1177 (info->IsMoxaMustChipFlag)) {
1178 spin_lock_irqsave(&info->slock, flags);
1179 outb(info->IER & ~UART_IER_THRI, info->base + UART_IER);
1180 info->IER |= UART_IER_THRI;
1181 outb(info->IER, info->base + UART_IER);
1182 spin_unlock_irqrestore(&info->slock, flags);
1188 static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1190 struct mxser_struct *info = tty->driver_data;
1191 unsigned long flags;
1193 if (!info->xmit_buf)
1196 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1199 spin_lock_irqsave(&info->slock, flags);
1200 info->xmit_buf[info->xmit_head++] = ch;
1201 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1203 spin_unlock_irqrestore(&info->slock, flags);
1204 if (!tty->stopped /*&& !(info->IER & UART_IER_THRI)*/) {
1205 if (!tty->hw_stopped ||
1206 (info->type == PORT_16550A) ||
1207 info->IsMoxaMustChipFlag) {
1208 spin_lock_irqsave(&info->slock, flags);
1209 outb(info->IER & ~UART_IER_THRI, info->base + UART_IER);
1210 info->IER |= UART_IER_THRI;
1211 outb(info->IER, info->base + UART_IER);
1212 spin_unlock_irqrestore(&info->slock, flags);
1218 static void mxser_flush_chars(struct tty_struct *tty)
1220 struct mxser_struct *info = tty->driver_data;
1221 unsigned long flags;
1223 if (info->xmit_cnt <= 0 ||
1227 (info->type != PORT_16550A) &&
1228 (!info->IsMoxaMustChipFlag)
1232 spin_lock_irqsave(&info->slock, flags);
1234 outb(info->IER & ~UART_IER_THRI, info->base + UART_IER);
1235 info->IER |= UART_IER_THRI;
1236 outb(info->IER, info->base + UART_IER);
1238 spin_unlock_irqrestore(&info->slock, flags);
1241 static int mxser_write_room(struct tty_struct *tty)
1243 struct mxser_struct *info = tty->driver_data;
1246 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1252 static int mxser_chars_in_buffer(struct tty_struct *tty)
1254 struct mxser_struct *info = tty->driver_data;
1255 int len = info->xmit_cnt;
1257 if (!(inb(info->base + UART_LSR) & UART_LSR_THRE))
1263 static void mxser_flush_buffer(struct tty_struct *tty)
1265 struct mxser_struct *info = tty->driver_data;
1267 unsigned long flags;
1270 spin_lock_irqsave(&info->slock, flags);
1271 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1273 /* below added by shinhay */
1274 fcr = inb(info->base + UART_FCR);
1275 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1276 info->base + UART_FCR);
1277 outb(fcr, info->base + UART_FCR);
1279 spin_unlock_irqrestore(&info->slock, flags);
1280 /* above added by shinhay */
1282 wake_up_interruptible(&tty->write_wait);
1283 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
1284 (tty->ldisc.write_wakeup) (tty);
1287 static int mxser_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1289 struct mxser_struct *info = tty->driver_data;
1291 struct async_icount cprev, cnow; /* kernel counter temps */
1292 struct serial_icounter_struct __user *p_cuser;
1293 unsigned long templ;
1294 unsigned long flags;
1295 void __user *argp = (void __user *)arg;
1297 if (tty->index == MXSER_PORTS)
1298 return mxser_ioctl_special(cmd, argp);
1300 /* following add by Victor Yu. 01-05-2004 */
1301 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1303 unsigned long opmode;
1304 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1306 unsigned char val, mask;
1309 if (cmd == MOXA_SET_OP_MODE) {
1310 if (get_user(opmode, (int __user *) argp))
1312 if (opmode != RS232_MODE &&
1313 opmode != RS485_2WIRE_MODE &&
1314 opmode != RS422_MODE &&
1315 opmode != RS485_4WIRE_MODE)
1319 val = inb(info->opmode_ioaddr);
1321 val |= (opmode << shiftbit);
1322 outb(val, info->opmode_ioaddr);
1325 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1326 opmode &= OP_MODE_MASK;
1327 if (copy_to_user(argp, &opmode, sizeof(int)))
1332 /* above add by Victor Yu. 01-05-2004 */
1334 if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1335 if (tty->flags & (1 << TTY_IO_ERROR))
1339 case TCSBRK: /* SVID version: non-zero arg --> no break */
1340 retval = tty_check_change(tty);
1343 tty_wait_until_sent(tty, 0);
1345 mxser_send_break(info, HZ / 4); /* 1/4 second */
1347 case TCSBRKP: /* support for POSIX tcsendbreak() */
1348 retval = tty_check_change(tty);
1351 tty_wait_until_sent(tty, 0);
1352 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1355 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1357 if (get_user(templ, (unsigned long __user *) argp))
1360 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1363 return mxser_get_serial_info(info, argp);
1365 return mxser_set_serial_info(info, argp);
1366 case TIOCSERGETLSR: /* Get line status register */
1367 return mxser_get_lsr_info(info, argp);
1369 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1370 * - mask passed in arg for lines of interest
1371 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1372 * Caller should use TIOCGICOUNT to see which one it was
1375 DECLARE_WAITQUEUE(wait, current);
1377 spin_lock_irqsave(&info->slock, flags);
1378 cprev = info->icount; /* note the counters on entry */
1379 spin_unlock_irqrestore(&info->slock, flags);
1381 add_wait_queue(&info->delta_msr_wait, &wait);
1383 spin_lock_irqsave(&info->slock, flags);
1384 cnow = info->icount; /* atomic copy */
1385 spin_unlock_irqrestore(&info->slock, flags);
1387 set_current_state(TASK_INTERRUPTIBLE);
1388 if (((arg & TIOCM_RNG) &&
1389 (cnow.rng != cprev.rng)) ||
1390 ((arg & TIOCM_DSR) &&
1391 (cnow.dsr != cprev.dsr)) ||
1392 ((arg & TIOCM_CD) &&
1393 (cnow.dcd != cprev.dcd)) ||
1394 ((arg & TIOCM_CTS) &&
1395 (cnow.cts != cprev.cts))) {
1399 /* see if a signal did it */
1400 if (signal_pending(current)) {
1406 current->state = TASK_RUNNING;
1407 remove_wait_queue(&info->delta_msr_wait, &wait);
1412 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1413 * Return: write counters to the user passed counter struct
1414 * NB: both 1->0 and 0->1 transitions are counted except for
1415 * RI where only 0->1 is counted.
1418 spin_lock_irqsave(&info->slock, flags);
1419 cnow = info->icount;
1420 spin_unlock_irqrestore(&info->slock, flags);
1422 /* modified by casper 1/11/2000 */
1423 if (put_user(cnow.frame, &p_cuser->frame))
1425 if (put_user(cnow.brk, &p_cuser->brk))
1427 if (put_user(cnow.overrun, &p_cuser->overrun))
1429 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1431 if (put_user(cnow.parity, &p_cuser->parity))
1433 if (put_user(cnow.rx, &p_cuser->rx))
1435 if (put_user(cnow.tx, &p_cuser->tx))
1437 put_user(cnow.cts, &p_cuser->cts);
1438 put_user(cnow.dsr, &p_cuser->dsr);
1439 put_user(cnow.rng, &p_cuser->rng);
1440 put_user(cnow.dcd, &p_cuser->dcd);
1442 case MOXA_HighSpeedOn:
1443 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1444 case MOXA_SDS_RSTICOUNTER: {
1445 info->mon_data.rxcnt = 0;
1446 info->mon_data.txcnt = 0;
1449 /* (above) added by James. */
1450 case MOXA_ASPP_SETBAUD:{
1452 if (get_user(baud, (long __user *)argp))
1454 mxser_set_baud(info, baud);
1457 case MOXA_ASPP_GETBAUD:
1458 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1463 case MOXA_ASPP_OQUEUE:{
1466 len = mxser_chars_in_buffer(tty);
1468 lsr = inb(info->base + UART_LSR) & UART_LSR_TEMT;
1470 len += (lsr ? 0 : 1);
1472 if (copy_to_user(argp, &len, sizeof(int)))
1477 case MOXA_ASPP_MON: {
1480 /* info->mon_data.ser_param = tty->termios->c_cflag; */
1482 status = mxser_get_msr(info->base, 1, info->port, info);
1483 mxser_check_modem_status(info, status);
1485 mcr = inb(info->base + UART_MCR);
1486 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1487 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1489 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1491 if (mcr & MOXA_MUST_MCR_TX_XON)
1492 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1494 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1496 if (info->tty->hw_stopped)
1497 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1499 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1501 if (copy_to_user(argp, &info->mon_data,
1502 sizeof(struct mxser_mon)))
1508 case MOXA_ASPP_LSTATUS: {
1509 if (copy_to_user(argp, &info->err_shadow,
1510 sizeof(unsigned char)))
1513 info->err_shadow = 0;
1516 case MOXA_SET_BAUD_METHOD: {
1519 if (get_user(method, (int __user *)argp))
1521 mxser_set_baud_method[info->port] = method;
1522 if (copy_to_user(argp, &method, sizeof(int)))
1528 return -ENOIOCTLCMD;
1534 #define CMSPAR 010000000000
1537 static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1539 int i, result, status;
1543 if (copy_to_user(argp, mxsercfg,
1544 sizeof(struct mxser_hwconf) * 4))
1547 case MOXA_GET_MAJOR:
1548 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1552 case MOXA_GET_CUMAJOR:
1553 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1557 case MOXA_CHKPORTENABLE:
1559 for (i = 0; i < MXSER_PORTS; i++) {
1560 if (mxvar_table[i].base)
1563 return put_user(result, (unsigned long __user *)argp);
1564 case MOXA_GETDATACOUNT:
1565 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1568 case MOXA_GETMSTATUS:
1569 for (i = 0; i < MXSER_PORTS; i++) {
1571 if (!mxvar_table[i].base) {
1572 GMStatus[i].dcd = 0;
1573 GMStatus[i].dsr = 0;
1574 GMStatus[i].cts = 0;
1578 if (!mxvar_table[i].tty || !mxvar_table[i].tty->termios)
1579 GMStatus[i].cflag = mxvar_table[i].normal_termios.c_cflag;
1581 GMStatus[i].cflag = mxvar_table[i].tty->termios->c_cflag;
1583 status = inb(mxvar_table[i].base + UART_MSR);
1584 if (status & 0x80 /*UART_MSR_DCD */ )
1585 GMStatus[i].dcd = 1;
1587 GMStatus[i].dcd = 0;
1589 if (status & 0x20 /*UART_MSR_DSR */ )
1590 GMStatus[i].dsr = 1;
1592 GMStatus[i].dsr = 0;
1595 if (status & 0x10 /*UART_MSR_CTS */ )
1596 GMStatus[i].cts = 1;
1598 GMStatus[i].cts = 0;
1600 if (copy_to_user(argp, GMStatus,
1601 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1604 case MOXA_ASPP_MON_EXT: {
1605 int status, p, shiftbit;
1606 unsigned long opmode;
1607 unsigned cflag, iflag;
1609 for (i = 0; i < MXSER_PORTS; i++) {
1610 if (!mxvar_table[i].base)
1613 status = mxser_get_msr(mxvar_table[i].base, 0,
1614 i, &(mxvar_table[i]));
1616 mxser_check_modem_status(&mxvar_table[i],
1619 if (status & UART_MSR_TERI)
1620 mxvar_table[i].icount.rng++;
1621 if (status & UART_MSR_DDSR)
1622 mxvar_table[i].icount.dsr++;
1623 if (status & UART_MSR_DDCD)
1624 mxvar_table[i].icount.dcd++;
1625 if (status & UART_MSR_DCTS)
1626 mxvar_table[i].icount.cts++;
1628 mxvar_table[i].mon_data.modem_status = status;
1629 mon_data_ext.rx_cnt[i] = mxvar_table[i].mon_data.rxcnt;
1630 mon_data_ext.tx_cnt[i] = mxvar_table[i].mon_data.txcnt;
1631 mon_data_ext.up_rxcnt[i] = mxvar_table[i].mon_data.up_rxcnt;
1632 mon_data_ext.up_txcnt[i] = mxvar_table[i].mon_data.up_txcnt;
1633 mon_data_ext.modem_status[i] = mxvar_table[i].mon_data.modem_status;
1634 mon_data_ext.baudrate[i] = mxvar_table[i].realbaud;
1636 if (!mxvar_table[i].tty || !mxvar_table[i].tty->termios) {
1637 cflag = mxvar_table[i].normal_termios.c_cflag;
1638 iflag = mxvar_table[i].normal_termios.c_iflag;
1640 cflag = mxvar_table[i].tty->termios->c_cflag;
1641 iflag = mxvar_table[i].tty->termios->c_iflag;
1644 mon_data_ext.databits[i] = cflag & CSIZE;
1646 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1648 mon_data_ext.parity[i] = cflag & (PARENB | PARODD | CMSPAR);
1650 mon_data_ext.flowctrl[i] = 0x00;
1652 if (cflag & CRTSCTS)
1653 mon_data_ext.flowctrl[i] |= 0x03;
1655 if (iflag & (IXON | IXOFF))
1656 mon_data_ext.flowctrl[i] |= 0x0C;
1658 if (mxvar_table[i].type == PORT_16550A)
1659 mon_data_ext.fifo[i] = 1;
1661 mon_data_ext.fifo[i] = 0;
1665 opmode = inb(mxvar_table[i].opmode_ioaddr) >> shiftbit;
1666 opmode &= OP_MODE_MASK;
1668 mon_data_ext.iftype[i] = opmode;
1671 if (copy_to_user(argp, &mon_data_ext, sizeof(struct mxser_mon_ext)))
1678 return -ENOIOCTLCMD;
1683 static void mxser_stoprx(struct tty_struct *tty)
1685 struct mxser_struct *info = tty->driver_data;
1687 info->ldisc_stop_rx = 1;
1689 /* following add by Victor Yu. 09-02-2002 */
1690 if (info->IsMoxaMustChipFlag) {
1691 info->IER &= ~MOXA_MUST_RECV_ISR;
1692 outb(info->IER, info->base + UART_IER);
1693 } else if (!(info->flags & ASYNC_CLOSING)) {
1694 info->x_char = STOP_CHAR(tty);
1695 outb(0, info->base + UART_IER);
1696 info->IER |= UART_IER_THRI;
1697 outb(info->IER, info->base + UART_IER);
1701 if (info->tty->termios->c_cflag & CRTSCTS) {
1702 info->MCR &= ~UART_MCR_RTS;
1703 outb(info->MCR, info->base + UART_MCR);
1707 static void mxser_startrx(struct tty_struct *tty)
1709 struct mxser_struct *info = tty->driver_data;
1711 info->ldisc_stop_rx = 0;
1716 /* following add by Victor Yu. 09-02-2002 */
1717 if (info->IsMoxaMustChipFlag) {
1718 info->IER |= MOXA_MUST_RECV_ISR;
1719 outb(info->IER, info->base + UART_IER);
1720 } else if (!(info->flags & ASYNC_CLOSING)) {
1721 info->x_char = START_CHAR(tty);
1722 outb(0, info->base + UART_IER);
1723 info->IER |= UART_IER_THRI;
1724 outb(info->IER, info->base + UART_IER);
1729 if (info->tty->termios->c_cflag & CRTSCTS) {
1730 info->MCR |= UART_MCR_RTS;
1731 outb(info->MCR, info->base + UART_MCR);
1736 * This routine is called by the upper-layer tty layer to signal that
1737 * incoming characters should be throttled.
1739 static void mxser_throttle(struct tty_struct *tty)
1741 struct mxser_struct *info = tty->driver_data;
1742 unsigned long flags;
1744 spin_lock_irqsave(&info->slock, flags);
1746 spin_unlock_irqrestore(&info->slock, flags);
1749 static void mxser_unthrottle(struct tty_struct *tty)
1751 struct mxser_struct *info = tty->driver_data;
1752 unsigned long flags;
1754 spin_lock_irqsave(&info->slock, flags);
1756 spin_unlock_irqrestore(&info->slock, flags);
1759 static void mxser_set_termios(struct tty_struct *tty, struct termios *old_termios)
1761 struct mxser_struct *info = tty->driver_data;
1762 unsigned long flags;
1764 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1765 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
1767 mxser_change_speed(info, old_termios);
1769 if ((old_termios->c_cflag & CRTSCTS) &&
1770 !(tty->termios->c_cflag & CRTSCTS)) {
1771 tty->hw_stopped = 0;
1776 /* Handle sw stopped */
1777 if ((old_termios->c_iflag & IXON) &&
1778 !(tty->termios->c_iflag & IXON)) {
1781 /* following add by Victor Yu. 09-02-2002 */
1782 if (info->IsMoxaMustChipFlag) {
1783 spin_lock_irqsave(&info->slock, flags);
1784 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->base);
1785 spin_unlock_irqrestore(&info->slock, flags);
1787 /* above add by Victor Yu. 09-02-2002 */
1794 * mxser_stop() and mxser_start()
1796 * This routines are called before setting or resetting tty->stopped.
1797 * They enable or disable transmitter interrupts, as necessary.
1799 static void mxser_stop(struct tty_struct *tty)
1801 struct mxser_struct *info = tty->driver_data;
1802 unsigned long flags;
1804 spin_lock_irqsave(&info->slock, flags);
1805 if (info->IER & UART_IER_THRI) {
1806 info->IER &= ~UART_IER_THRI;
1807 outb(info->IER, info->base + UART_IER);
1809 spin_unlock_irqrestore(&info->slock, flags);
1812 static void mxser_start(struct tty_struct *tty)
1814 struct mxser_struct *info = tty->driver_data;
1815 unsigned long flags;
1817 spin_lock_irqsave(&info->slock, flags);
1818 if (info->xmit_cnt && info->xmit_buf
1819 /* && !(info->IER & UART_IER_THRI) */) {
1820 outb(info->IER & ~UART_IER_THRI, info->base + UART_IER);
1821 info->IER |= UART_IER_THRI;
1822 outb(info->IER, info->base + UART_IER);
1824 spin_unlock_irqrestore(&info->slock, flags);
1828 * mxser_wait_until_sent() --- wait until the transmitter is empty
1830 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1832 struct mxser_struct *info = tty->driver_data;
1833 unsigned long orig_jiffies, char_time;
1836 if (info->type == PORT_UNKNOWN)
1839 if (info->xmit_fifo_size == 0)
1840 return; /* Just in case.... */
1842 orig_jiffies = jiffies;
1844 * Set the check interval to be 1/5 of the estimated time to
1845 * send a single character, and make it at least 1. The check
1846 * interval should also be less than the timeout.
1848 * Note: we have to use pretty tight timings here to satisfy
1851 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1852 char_time = char_time / 5;
1855 if (timeout && timeout < char_time)
1856 char_time = timeout;
1858 * If the transmitter hasn't cleared in twice the approximate
1859 * amount of time to send the entire FIFO, it probably won't
1860 * ever clear. This assumes the UART isn't doing flow
1861 * control, which is currently the case. Hence, if it ever
1862 * takes longer than info->timeout, this is probably due to a
1863 * UART bug of some kind. So, we clamp the timeout parameter at
1866 if (!timeout || timeout > 2 * info->timeout)
1867 timeout = 2 * info->timeout;
1868 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1869 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
1870 timeout, char_time);
1871 printk("jiff=%lu...", jiffies);
1873 while (!((lsr = inb(info->base + UART_LSR)) & UART_LSR_TEMT)) {
1874 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1875 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1877 schedule_timeout_interruptible(char_time);
1878 if (signal_pending(current))
1880 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1883 set_current_state(TASK_RUNNING);
1885 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1886 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1892 * This routine is called by tty_hangup() when a hangup is signaled.
1894 void mxser_hangup(struct tty_struct *tty)
1896 struct mxser_struct *info = tty->driver_data;
1898 mxser_flush_buffer(tty);
1899 mxser_shutdown(info);
1902 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1904 wake_up_interruptible(&info->open_wait);
1908 /* added by James 03-12-2004. */
1910 * mxser_rs_break() --- routine which turns the break handling on or off
1912 static void mxser_rs_break(struct tty_struct *tty, int break_state)
1914 struct mxser_struct *info = tty->driver_data;
1915 unsigned long flags;
1917 spin_lock_irqsave(&info->slock, flags);
1918 if (break_state == -1)
1919 outb(inb(info->base + UART_LCR) | UART_LCR_SBC,
1920 info->base + UART_LCR);
1922 outb(inb(info->base + UART_LCR) & ~UART_LCR_SBC,
1923 info->base + UART_LCR);
1924 spin_unlock_irqrestore(&info->slock, flags);
1927 /* (above) added by James. */
1931 * This is the serial driver's generic interrupt routine
1933 static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1936 struct mxser_struct *info;
1937 struct mxser_struct *port;
1938 int max, irqbits, bits, msr;
1939 int pass_counter = 0;
1940 unsigned int int_cnt;
1941 int handled = IRQ_NONE;
1944 /* spin_lock(&gm_lock); */
1946 for (i = 0; i < MXSER_BOARDS; i++) {
1947 if (dev_id == &(mxvar_table[i * MXSER_PORTS_PER_BOARD])) {
1953 if (i == MXSER_BOARDS)
1957 max = mxser_numports[mxsercfg[i].board_type - 1];
1959 irqbits = inb(port->vector) & port->vectormask;
1960 if (irqbits == port->vectormask)
1963 handled = IRQ_HANDLED;
1964 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
1965 if (irqbits == port->vectormask)
1973 /* following add by Victor Yu. 09-13-2002 */
1974 iir = inb(info->base + UART_IIR);
1975 if (iir & UART_IIR_NO_INT)
1977 iir &= MOXA_MUST_IIR_MASK;
1979 status = inb(info->base + UART_LSR);
1980 outb(0x27, info->base + UART_FCR);
1981 inb(info->base + UART_MSR);
1984 /* above add by Victor Yu. 09-13-2002 */
1986 spin_lock(&info->slock);
1987 /* following add by Victor Yu. 09-02-2002 */
1988 status = inb(info->base + UART_LSR);
1990 if (status & UART_LSR_PE)
1991 info->err_shadow |= NPPI_NOTIFY_PARITY;
1992 if (status & UART_LSR_FE)
1993 info->err_shadow |= NPPI_NOTIFY_FRAMING;
1994 if (status & UART_LSR_OE)
1996 NPPI_NOTIFY_HW_OVERRUN;
1997 if (status & UART_LSR_BI)
1998 info->err_shadow |= NPPI_NOTIFY_BREAK;
2000 if (info->IsMoxaMustChipFlag) {
2002 if ( (status & 0x02) && !(status & 0x01) ) {
2003 outb(info->base+UART_FCR, 0x23);
2007 if (iir == MOXA_MUST_IIR_GDA ||
2008 iir == MOXA_MUST_IIR_RDA ||
2009 iir == MOXA_MUST_IIR_RTO ||
2010 iir == MOXA_MUST_IIR_LSR)
2011 mxser_receive_chars(info,
2015 /* above add by Victor Yu. 09-02-2002 */
2017 status &= info->read_status_mask;
2018 if (status & UART_LSR_DR)
2019 mxser_receive_chars(info,
2022 msr = inb(info->base + UART_MSR);
2023 if (msr & UART_MSR_ANY_DELTA)
2024 mxser_check_modem_status(info, msr);
2026 /* following add by Victor Yu. 09-13-2002 */
2027 if (info->IsMoxaMustChipFlag) {
2028 if (iir == 0x02 && (status &
2030 mxser_transmit_chars(info);
2032 /* above add by Victor Yu. 09-13-2002 */
2034 if (status & UART_LSR_THRE)
2035 mxser_transmit_chars(info);
2037 spin_unlock(&info->slock);
2038 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2040 if (pass_counter++ > MXSER_ISR_PASS_LIMIT)
2041 break; /* Prevent infinite loops */
2045 /* spin_unlock(&gm_lock); */
2049 static void mxser_receive_chars(struct mxser_struct *info, int *status)
2051 struct tty_struct *tty = info->tty;
2052 unsigned char ch, gdl;
2057 unsigned long flags;
2059 spin_lock_irqsave(&info->slock, flags);
2061 recv_room = tty->receive_room;
2062 if ((recv_room == 0) && (!info->ldisc_stop_rx)) {
2063 /* mxser_throttle(tty); */
2068 /* following add by Victor Yu. 09-02-2002 */
2069 if (info->IsMoxaMustChipFlag != MOXA_OTHER_UART) {
2071 if (*status & UART_LSR_SPECIAL)
2073 /* following add by Victor Yu. 02-11-2004 */
2074 if (info->IsMoxaMustChipFlag == MOXA_MUST_MU860_HWID &&
2075 (*status & MOXA_MUST_LSR_RERR))
2077 /* above add by Victor Yu. 02-14-2004 */
2078 if (*status & MOXA_MUST_LSR_RERR)
2081 gdl = inb(info->base + MOXA_MUST_GDL_REGISTER);
2083 /* add by Victor Yu. 02-11-2004 */
2084 if (info->IsMoxaMustChipFlag == MOXA_MUST_MU150_HWID)
2085 gdl &= MOXA_MUST_GDL_MASK;
2086 if (gdl >= recv_room) {
2087 if (!info->ldisc_stop_rx) {
2088 /* mxser_throttle(tty); */
2094 ch = inb(info->base + UART_RX);
2095 tty_insert_flip_char(tty, ch, 0);
2101 /* above add by Victor Yu. 09-02-2002 */
2107 ch = inb(info->base + UART_RX);
2108 /* following add by Victor Yu. 09-02-2002 */
2109 if (info->IsMoxaMustChipFlag && (*status & UART_LSR_OE)
2110 /*&& !(*status&UART_LSR_DR) */)
2111 outb(0x23, info->base + UART_FCR);
2112 *status &= info->read_status_mask;
2113 /* above add by Victor Yu. 09-02-2002 */
2114 if (*status & info->ignore_status_mask) {
2115 if (++ignored > 100)
2119 if (*status & UART_LSR_SPECIAL) {
2120 if (*status & UART_LSR_BI) {
2122 /* added by casper 1/11/2000 */
2125 if (info->flags & ASYNC_SAK)
2127 } else if (*status & UART_LSR_PE) {
2129 /* added by casper 1/11/2000 */
2130 info->icount.parity++;
2131 } else if (*status & UART_LSR_FE) {
2133 /* added by casper 1/11/2000 */
2134 info->icount.frame++;
2135 } else if (*status & UART_LSR_OE) {
2137 /* added by casper 1/11/2000 */
2138 info->icount.overrun++;
2143 tty_insert_flip_char(tty, ch, flag);
2145 if (cnt >= recv_room) {
2146 if (!info->ldisc_stop_rx) {
2147 /* mxser_throttle(tty); */
2155 /* following add by Victor Yu. 09-02-2002 */
2156 if (info->IsMoxaMustChipFlag)
2159 /* mask by Victor Yu. 09-02-2002
2160 *status = inb(info->base + UART_LSR) & info->read_status_mask;
2162 /* following add by Victor Yu. 09-02-2002 */
2163 *status = inb(info->base + UART_LSR);
2164 /* above add by Victor Yu. 09-02-2002 */
2165 } while (*status & UART_LSR_DR);
2167 end_intr: /* add by Victor Yu. 09-02-2002 */
2168 mxvar_log.rxcnt[info->port] += cnt;
2169 info->mon_data.rxcnt += cnt;
2170 info->mon_data.up_rxcnt += cnt;
2171 spin_unlock_irqrestore(&info->slock, flags);
2173 tty_flip_buffer_push(tty);
2176 static void mxser_transmit_chars(struct mxser_struct *info)
2179 unsigned long flags;
2181 spin_lock_irqsave(&info->slock, flags);
2184 outb(info->x_char, info->base + UART_TX);
2186 mxvar_log.txcnt[info->port]++;
2187 info->mon_data.txcnt++;
2188 info->mon_data.up_txcnt++;
2190 /* added by casper 1/11/2000 */
2195 if (info->xmit_buf == 0)
2198 if (info->xmit_cnt == 0) {
2199 if (info->xmit_cnt < WAKEUP_CHARS) { /* XXX what's this for?? */
2200 set_bit(MXSER_EVENT_TXLOW, &info->event);
2201 schedule_work(&info->tqueue);
2205 if (info->tty->stopped || (info->tty->hw_stopped &&
2206 (info->type != PORT_16550A) &&
2207 (!info->IsMoxaMustChipFlag))) {
2208 info->IER &= ~UART_IER_THRI;
2209 outb(info->IER, info->base + UART_IER);
2213 cnt = info->xmit_cnt;
2214 count = info->xmit_fifo_size;
2216 outb(info->xmit_buf[info->xmit_tail++],
2217 info->base + UART_TX);
2218 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2219 if (--info->xmit_cnt <= 0)
2221 } while (--count > 0);
2222 mxvar_log.txcnt[info->port] += (cnt - info->xmit_cnt);
2224 /* added by James 03-12-2004. */
2225 info->mon_data.txcnt += (cnt - info->xmit_cnt);
2226 info->mon_data.up_txcnt += (cnt - info->xmit_cnt);
2228 /* added by casper 1/11/2000 */
2229 info->icount.tx += (cnt - info->xmit_cnt);
2231 if (info->xmit_cnt < WAKEUP_CHARS) {
2232 set_bit(MXSER_EVENT_TXLOW, &info->event);
2233 schedule_work(&info->tqueue);
2235 if (info->xmit_cnt <= 0) {
2236 info->IER &= ~UART_IER_THRI;
2237 outb(info->IER, info->base + UART_IER);
2240 spin_unlock_irqrestore(&info->slock, flags);
2243 static void mxser_check_modem_status(struct mxser_struct *info, int status)
2245 /* update input line counters */
2246 if (status & UART_MSR_TERI)
2248 if (status & UART_MSR_DDSR)
2250 if (status & UART_MSR_DDCD)
2252 if (status & UART_MSR_DCTS)
2254 info->mon_data.modem_status = status;
2255 wake_up_interruptible(&info->delta_msr_wait);
2257 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
2258 if (status & UART_MSR_DCD)
2259 wake_up_interruptible(&info->open_wait);
2260 schedule_work(&info->tqueue);
2263 if (info->flags & ASYNC_CTS_FLOW) {
2264 if (info->tty->hw_stopped) {
2265 if (status & UART_MSR_CTS) {
2266 info->tty->hw_stopped = 0;
2268 if ((info->type != PORT_16550A) &&
2269 (!info->IsMoxaMustChipFlag)) {
2270 outb(info->IER & ~UART_IER_THRI,
2271 info->base + UART_IER);
2272 info->IER |= UART_IER_THRI;
2273 outb(info->IER, info->base + UART_IER);
2275 set_bit(MXSER_EVENT_TXLOW, &info->event);
2276 schedule_work(&info->tqueue);
2279 if (!(status & UART_MSR_CTS)) {
2280 info->tty->hw_stopped = 1;
2281 if (info->type != PORT_16550A &&
2282 !info->IsMoxaMustChipFlag) {
2283 info->IER &= ~UART_IER_THRI;
2284 outb(info->IER, info->base + UART_IER);
2291 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp, struct mxser_struct *info)
2293 DECLARE_WAITQUEUE(wait, current);
2296 unsigned long flags;
2299 * If non-blocking mode is set, or the port is not enabled,
2300 * then make the check up front and then exit.
2302 if ((filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR))) {
2303 info->flags |= ASYNC_NORMAL_ACTIVE;
2307 if (tty->termios->c_cflag & CLOCAL)
2311 * Block waiting for the carrier detect and the line to become
2312 * free (i.e., not in use by the callout). While we are in
2313 * this loop, info->count is dropped by one, so that
2314 * mxser_close() knows when to free things. We restore it upon
2315 * exit, either normal or abnormal.
2318 add_wait_queue(&info->open_wait, &wait);
2320 spin_lock_irqsave(&info->slock, flags);
2321 if (!tty_hung_up_p(filp))
2323 spin_unlock_irqrestore(&info->slock, flags);
2324 info->blocked_open++;
2326 spin_lock_irqsave(&info->slock, flags);
2327 outb(inb(info->base + UART_MCR) |
2328 UART_MCR_DTR | UART_MCR_RTS, info->base + UART_MCR);
2329 spin_unlock_irqrestore(&info->slock, flags);
2330 set_current_state(TASK_INTERRUPTIBLE);
2331 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)) {
2332 if (info->flags & ASYNC_HUP_NOTIFY)
2335 retval = -ERESTARTSYS;
2338 if (!(info->flags & ASYNC_CLOSING) &&
2340 (inb(info->base + UART_MSR) & UART_MSR_DCD)))
2342 if (signal_pending(current)) {
2343 retval = -ERESTARTSYS;
2348 set_current_state(TASK_RUNNING);
2349 remove_wait_queue(&info->open_wait, &wait);
2350 if (!tty_hung_up_p(filp))
2352 info->blocked_open--;
2355 info->flags |= ASYNC_NORMAL_ACTIVE;
2359 static int mxser_startup(struct mxser_struct *info)
2362 unsigned long flags;
2364 page = __get_free_page(GFP_KERNEL);
2368 spin_lock_irqsave(&info->slock, flags);
2370 if (info->flags & ASYNC_INITIALIZED) {
2372 spin_unlock_irqrestore(&info->slock, flags);
2376 if (!info->base || !info->type) {
2378 set_bit(TTY_IO_ERROR, &info->tty->flags);
2380 spin_unlock_irqrestore(&info->slock, flags);
2386 info->xmit_buf = (unsigned char *) page;
2389 * Clear the FIFO buffers and disable them
2390 * (they will be reenabled in mxser_change_speed())
2392 if (info->IsMoxaMustChipFlag)
2393 outb((UART_FCR_CLEAR_RCVR |
2394 UART_FCR_CLEAR_XMIT |
2395 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->base + UART_FCR);
2397 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
2398 info->base + UART_FCR);
2401 * At this point there's no way the LSR could still be 0xFF;
2402 * if it is, then bail out, because there's likely no UART
2405 if (inb(info->base + UART_LSR) == 0xff) {
2406 spin_unlock_irqrestore(&info->slock, flags);
2407 if (capable(CAP_SYS_ADMIN)) {
2409 set_bit(TTY_IO_ERROR, &info->tty->flags);
2416 * Clear the interrupt registers.
2418 (void) inb(info->base + UART_LSR);
2419 (void) inb(info->base + UART_RX);
2420 (void) inb(info->base + UART_IIR);
2421 (void) inb(info->base + UART_MSR);
2424 * Now, initialize the UART
2426 outb(UART_LCR_WLEN8, info->base + UART_LCR); /* reset DLAB */
2427 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
2428 outb(info->MCR, info->base + UART_MCR);
2431 * Finally, enable interrupts
2433 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
2434 /* info->IER = UART_IER_RLSI | UART_IER_RDI; */
2436 /* following add by Victor Yu. 08-30-2002 */
2437 if (info->IsMoxaMustChipFlag)
2438 info->IER |= MOXA_MUST_IER_EGDAI;
2439 /* above add by Victor Yu. 08-30-2002 */
2440 outb(info->IER, info->base + UART_IER); /* enable interrupts */
2443 * And clear the interrupt registers again for luck.
2445 (void) inb(info->base + UART_LSR);
2446 (void) inb(info->base + UART_RX);
2447 (void) inb(info->base + UART_IIR);
2448 (void) inb(info->base + UART_MSR);
2451 clear_bit(TTY_IO_ERROR, &info->tty->flags);
2452 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
2455 * and set the speed of the serial port
2457 spin_unlock_irqrestore(&info->slock, flags);
2458 mxser_change_speed(info, NULL);
2460 info->flags |= ASYNC_INITIALIZED;
2465 * This routine will shutdown a serial port; interrupts maybe disabled, and
2466 * DTR is dropped if the hangup on close termio flag is on.
2468 static void mxser_shutdown(struct mxser_struct *info)
2470 unsigned long flags;
2472 if (!(info->flags & ASYNC_INITIALIZED))
2475 spin_lock_irqsave(&info->slock, flags);
2478 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
2479 * here so the queue might never be waken up
2481 wake_up_interruptible(&info->delta_msr_wait);
2484 * Free the IRQ, if necessary
2486 if (info->xmit_buf) {
2487 free_page((unsigned long) info->xmit_buf);
2488 info->xmit_buf = NULL;
2492 outb(0x00, info->base + UART_IER);
2494 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
2495 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
2496 outb(info->MCR, info->base + UART_MCR);
2498 /* clear Rx/Tx FIFO's */
2499 /* following add by Victor Yu. 08-30-2002 */
2500 if (info->IsMoxaMustChipFlag)
2501 outb((UART_FCR_CLEAR_RCVR |
2502 UART_FCR_CLEAR_XMIT |
2503 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->base + UART_FCR);
2505 /* above add by Victor Yu. 08-30-2002 */
2506 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
2507 info->base + UART_FCR);
2509 /* read data port to reset things */
2510 (void) inb(info->base + UART_RX);
2513 set_bit(TTY_IO_ERROR, &info->tty->flags);
2515 info->flags &= ~ASYNC_INITIALIZED;
2517 /* following add by Victor Yu. 09-23-2002 */
2518 if (info->IsMoxaMustChipFlag)
2519 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->base);
2520 /* above add by Victor Yu. 09-23-2002 */
2522 spin_unlock_irqrestore(&info->slock, flags);
2526 * This routine is called to set the UART divisor registers to match
2527 * the specified baud rate for a serial port.
2529 static int mxser_change_speed(struct mxser_struct *info, struct termios *old_termios)
2531 unsigned cflag, cval, fcr;
2533 unsigned char status;
2535 unsigned long flags;
2537 if (!info->tty || !info->tty->termios)
2539 cflag = info->tty->termios->c_cflag;
2544 #define B921600 (B460800 +1)
2546 if (mxser_set_baud_method[info->port] == 0) {
2547 baud = tty_get_baud_rate(info->tty);
2548 mxser_set_baud(info, baud);
2551 /* byte size and parity */
2552 switch (cflag & CSIZE) {
2567 break; /* too keep GCC shut... */
2572 cval |= UART_LCR_PARITY;
2573 if (!(cflag & PARODD))
2574 cval |= UART_LCR_EPAR;
2576 cval |= UART_LCR_SPAR;
2578 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
2579 if (info->IsMoxaMustChipFlag) {
2580 fcr = UART_FCR_ENABLE_FIFO;
2581 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2582 SET_MOXA_MUST_FIFO_VALUE(info);
2586 fcr = UART_FCR_ENABLE_FIFO;
2587 /* following add by Victor Yu. 08-30-2002 */
2588 if (info->IsMoxaMustChipFlag) {
2589 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2590 SET_MOXA_MUST_FIFO_VALUE(info);
2592 /* above add by Victor Yu. 08-30-2002 */
2593 switch (info->rx_trigger) {
2595 fcr |= UART_FCR_TRIGGER_1;
2598 fcr |= UART_FCR_TRIGGER_4;
2601 fcr |= UART_FCR_TRIGGER_8;
2604 fcr |= UART_FCR_TRIGGER_14;
2610 /* CTS flow control flag and modem status interrupts */
2611 info->IER &= ~UART_IER_MSI;
2612 info->MCR &= ~UART_MCR_AFE;
2613 if (cflag & CRTSCTS) {
2614 info->flags |= ASYNC_CTS_FLOW;
2615 info->IER |= UART_IER_MSI;
2616 if ((info->type == PORT_16550A) || (info->IsMoxaMustChipFlag)) {
2617 info->MCR |= UART_MCR_AFE;
2618 /* status = mxser_get_msr(info->base, 0, info->port); */
2622 status = inb(baseaddr + UART_MSR);
2623 restore_flags(flags);
2625 /* mxser_check_modem_status(info, status); */
2627 /* status = mxser_get_msr(info->base, 0, info->port); */
2628 /* MX_LOCK(&info->slock); */
2629 status = inb(info->base + UART_MSR);
2630 /* MX_UNLOCK(&info->slock); */
2631 if (info->tty->hw_stopped) {
2632 if (status & UART_MSR_CTS) {
2633 info->tty->hw_stopped = 0;
2634 if (info->type != PORT_16550A &&
2635 !info->IsMoxaMustChipFlag) {
2636 outb(info->IER & ~UART_IER_THRI,
2637 info->base + UART_IER);
2638 info->IER |= UART_IER_THRI;
2639 outb(info->IER, info->base + UART_IER);
2641 set_bit(MXSER_EVENT_TXLOW, &info->event);
2642 schedule_work(&info->tqueue); }
2644 if (!(status & UART_MSR_CTS)) {
2645 info->tty->hw_stopped = 1;
2646 if ((info->type != PORT_16550A) &&
2647 (!info->IsMoxaMustChipFlag)) {
2648 info->IER &= ~UART_IER_THRI;
2649 outb(info->IER, info->base + UART_IER);
2655 info->flags &= ~ASYNC_CTS_FLOW;
2657 outb(info->MCR, info->base + UART_MCR);
2658 if (cflag & CLOCAL) {
2659 info->flags &= ~ASYNC_CHECK_CD;
2661 info->flags |= ASYNC_CHECK_CD;
2662 info->IER |= UART_IER_MSI;
2664 outb(info->IER, info->base + UART_IER);
2667 * Set up parity check flag
2669 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2670 if (I_INPCK(info->tty))
2671 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2672 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
2673 info->read_status_mask |= UART_LSR_BI;
2675 info->ignore_status_mask = 0;
2677 if (I_IGNBRK(info->tty)) {
2678 info->ignore_status_mask |= UART_LSR_BI;
2679 info->read_status_mask |= UART_LSR_BI;
2681 * If we're ignore parity and break indicators, ignore
2682 * overruns too. (For real raw support).
2684 if (I_IGNPAR(info->tty)) {
2685 info->ignore_status_mask |=
2689 info->read_status_mask |=
2695 /* following add by Victor Yu. 09-02-2002 */
2696 if (info->IsMoxaMustChipFlag) {
2697 spin_lock_irqsave(&info->slock, flags);
2698 SET_MOXA_MUST_XON1_VALUE(info->base, START_CHAR(info->tty));
2699 SET_MOXA_MUST_XOFF1_VALUE(info->base, STOP_CHAR(info->tty));
2700 if (I_IXON(info->tty)) {
2701 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->base);
2703 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->base);
2705 if (I_IXOFF(info->tty)) {
2706 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->base);
2708 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->base);
2711 if ( I_IXANY(info->tty) ) {
2712 info->MCR |= MOXA_MUST_MCR_XON_ANY;
2713 ENABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->base);
2715 info->MCR &= ~MOXA_MUST_MCR_XON_ANY;
2716 DISABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->base);
2719 spin_unlock_irqrestore(&info->slock, flags);
2721 /* above add by Victor Yu. 09-02-2002 */
2724 outb(fcr, info->base + UART_FCR); /* set fcr */
2725 outb(cval, info->base + UART_LCR);
2731 static int mxser_set_baud(struct mxser_struct *info, long newspd)
2736 unsigned long flags;
2738 if (!info->tty || !info->tty->termios)
2744 if (newspd > info->MaxCanSetBaudRate)
2747 info->realbaud = newspd;
2748 if (newspd == 134) {
2749 quot = (2 * info->baud_base / 269);
2750 } else if (newspd) {
2751 quot = info->baud_base / newspd;
2758 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
2759 info->timeout += HZ / 50; /* Add .02 seconds of slop */
2762 spin_lock_irqsave(&info->slock, flags);
2763 info->MCR |= UART_MCR_DTR;
2764 outb(info->MCR, info->base + UART_MCR);
2765 spin_unlock_irqrestore(&info->slock, flags);
2767 spin_lock_irqsave(&info->slock, flags);
2768 info->MCR &= ~UART_MCR_DTR;
2769 outb(info->MCR, info->base + UART_MCR);
2770 spin_unlock_irqrestore(&info->slock, flags);
2774 cval = inb(info->base + UART_LCR);
2776 outb(cval | UART_LCR_DLAB, info->base + UART_LCR); /* set DLAB */
2778 outb(quot & 0xff, info->base + UART_DLL); /* LS of divisor */
2779 outb(quot >> 8, info->base + UART_DLM); /* MS of divisor */
2780 outb(cval, info->base + UART_LCR); /* reset DLAB */
2787 * ------------------------------------------------------------
2788 * friends of mxser_ioctl()
2789 * ------------------------------------------------------------
2791 static int mxser_get_serial_info(struct mxser_struct *info, struct serial_struct __user *retinfo)
2793 struct serial_struct tmp;
2797 memset(&tmp, 0, sizeof(tmp));
2798 tmp.type = info->type;
2799 tmp.line = info->port;
2800 tmp.port = info->base;
2801 tmp.irq = info->irq;
2802 tmp.flags = info->flags;
2803 tmp.baud_base = info->baud_base;
2804 tmp.close_delay = info->close_delay;
2805 tmp.closing_wait = info->closing_wait;
2806 tmp.custom_divisor = info->custom_divisor;
2808 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2813 static int mxser_set_serial_info(struct mxser_struct *info, struct serial_struct __user *new_info)
2815 struct serial_struct new_serial;
2819 if (!new_info || !info->base)
2821 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2824 if ((new_serial.irq != info->irq) ||
2825 (new_serial.port != info->base) ||
2826 (new_serial.custom_divisor != info->custom_divisor) ||
2827 (new_serial.baud_base != info->baud_base))
2830 flags = info->flags & ASYNC_SPD_MASK;
2832 if (!capable(CAP_SYS_ADMIN)) {
2833 if ((new_serial.baud_base != info->baud_base) ||
2834 (new_serial.close_delay != info->close_delay) ||
2835 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
2837 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
2838 (new_serial.flags & ASYNC_USR_MASK));
2841 * OK, past this point, all the error checking has been done.
2842 * At this point, we start making changes.....
2844 info->flags = ((info->flags & ~ASYNC_FLAGS) |
2845 (new_serial.flags & ASYNC_FLAGS));
2846 info->close_delay = new_serial.close_delay * HZ / 100;
2847 info->closing_wait = new_serial.closing_wait * HZ / 100;
2848 info->tty->low_latency =
2849 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2850 info->tty->low_latency = 0; /* (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0; */
2853 /* added by casper, 3/17/2000, for mouse */
2854 info->type = new_serial.type;
2856 process_txrx_fifo(info);
2858 if (info->flags & ASYNC_INITIALIZED) {
2859 if (flags != (info->flags & ASYNC_SPD_MASK)) {
2860 mxser_change_speed(info, NULL);
2863 retval = mxser_startup(info);
2869 * mxser_get_lsr_info - get line status register info
2871 * Purpose: Let user call ioctl() to get info when the UART physically
2872 * is emptied. On bus types like RS485, the transmitter must
2873 * release the bus after transmitting. This must be done when
2874 * the transmit shift register is empty, not be done when the
2875 * transmit holding register is empty. This functionality
2876 * allows an RS485 driver to be written in user space.
2878 static int mxser_get_lsr_info(struct mxser_struct *info, unsigned int __user *value)
2880 unsigned char status;
2881 unsigned int result;
2882 unsigned long flags;
2884 spin_lock_irqsave(&info->slock, flags);
2885 status = inb(info->base + UART_LSR);
2886 spin_unlock_irqrestore(&info->slock, flags);
2887 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
2888 return put_user(result, value);
2892 * This routine sends a break character out the serial port.
2894 static void mxser_send_break(struct mxser_struct *info, int duration)
2896 unsigned long flags;
2900 set_current_state(TASK_INTERRUPTIBLE);
2901 spin_lock_irqsave(&info->slock, flags);
2902 outb(inb(info->base + UART_LCR) | UART_LCR_SBC,
2903 info->base + UART_LCR);
2904 spin_unlock_irqrestore(&info->slock, flags);
2905 schedule_timeout(duration);
2906 spin_lock_irqsave(&info->slock, flags);
2907 outb(inb(info->base + UART_LCR) & ~UART_LCR_SBC,
2908 info->base + UART_LCR);
2909 spin_unlock_irqrestore(&info->slock, flags);
2912 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
2914 struct mxser_struct *info = tty->driver_data;
2915 unsigned char control, status;
2916 unsigned long flags;
2919 if (tty->index == MXSER_PORTS)
2920 return -ENOIOCTLCMD;
2921 if (tty->flags & (1 << TTY_IO_ERROR))
2924 control = info->MCR;
2926 spin_lock_irqsave(&info->slock, flags);
2927 status = inb(info->base + UART_MSR);
2928 if (status & UART_MSR_ANY_DELTA)
2929 mxser_check_modem_status(info, status);
2930 spin_unlock_irqrestore(&info->slock, flags);
2931 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
2932 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
2933 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
2934 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
2935 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
2936 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
2939 static int mxser_tiocmset(struct tty_struct *tty, struct file *file, unsigned int set, unsigned int clear)
2941 struct mxser_struct *info = tty->driver_data;
2942 unsigned long flags;
2945 if (tty->index == MXSER_PORTS)
2946 return -ENOIOCTLCMD;
2947 if (tty->flags & (1 << TTY_IO_ERROR))
2950 spin_lock_irqsave(&info->slock, flags);
2952 if (set & TIOCM_RTS)
2953 info->MCR |= UART_MCR_RTS;
2954 if (set & TIOCM_DTR)
2955 info->MCR |= UART_MCR_DTR;
2957 if (clear & TIOCM_RTS)
2958 info->MCR &= ~UART_MCR_RTS;
2959 if (clear & TIOCM_DTR)
2960 info->MCR &= ~UART_MCR_DTR;
2962 outb(info->MCR, info->base + UART_MCR);
2963 spin_unlock_irqrestore(&info->slock, flags);
2968 static int mxser_read_register(int, unsigned short *);
2969 static int mxser_program_mode(int);
2970 static void mxser_normal_mode(int);
2972 static int mxser_get_ISA_conf(int cap, struct mxser_hwconf *hwconf)
2975 unsigned short regs[16], irq;
2976 unsigned char scratch, scratch2;
2978 hwconf->IsMoxaMustChipFlag = MOXA_OTHER_UART;
2980 id = mxser_read_register(cap, regs);
2981 if (id == C168_ASIC_ID) {
2982 hwconf->board_type = MXSER_BOARD_C168_ISA;
2984 } else if (id == C104_ASIC_ID) {
2985 hwconf->board_type = MXSER_BOARD_C104_ISA;
2987 } else if (id == C102_ASIC_ID) {
2988 hwconf->board_type = MXSER_BOARD_C102_ISA;
2990 } else if (id == CI132_ASIC_ID) {
2991 hwconf->board_type = MXSER_BOARD_CI132;
2993 } else if (id == CI134_ASIC_ID) {
2994 hwconf->board_type = MXSER_BOARD_CI134;
2996 } else if (id == CI104J_ASIC_ID) {
2997 hwconf->board_type = MXSER_BOARD_CI104J;
3003 if (hwconf->ports == 2) {
3004 irq = regs[9] & 0xF000;
3005 irq = irq | (irq >> 4);
3006 if (irq != (regs[9] & 0xFF00))
3007 return MXSER_ERR_IRQ_CONFLIT;
3008 } else if (hwconf->ports == 4) {
3009 irq = regs[9] & 0xF000;
3010 irq = irq | (irq >> 4);
3011 irq = irq | (irq >> 8);
3013 return MXSER_ERR_IRQ_CONFLIT;
3014 } else if (hwconf->ports == 8) {
3015 irq = regs[9] & 0xF000;
3016 irq = irq | (irq >> 4);
3017 irq = irq | (irq >> 8);
3018 if ((irq != regs[9]) || (irq != regs[10]))
3019 return MXSER_ERR_IRQ_CONFLIT;
3023 return MXSER_ERR_IRQ;
3024 hwconf->irq = ((int)(irq & 0xF000) >> 12);
3025 for (i = 0; i < 8; i++)
3026 hwconf->ioaddr[i] = (int) regs[i + 1] & 0xFFF8;
3027 if ((regs[12] & 0x80) == 0)
3028 return MXSER_ERR_VECTOR;
3029 hwconf->vector = (int)regs[11]; /* interrupt vector */
3031 hwconf->vector_mask = 0x00FF;
3033 hwconf->vector_mask = 0x000F;
3034 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
3035 if (regs[12] & bits) {
3036 hwconf->baud_base[i] = 921600;
3037 hwconf->MaxCanSetBaudRate[i] = 921600; /* add by Victor Yu. 09-04-2002 */
3039 hwconf->baud_base[i] = 115200;
3040 hwconf->MaxCanSetBaudRate[i] = 115200; /* add by Victor Yu. 09-04-2002 */
3043 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
3044 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
3045 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
3046 outb(scratch2, cap + UART_LCR);
3047 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
3048 scratch = inb(cap + UART_IIR);
3051 hwconf->uart_type = PORT_16550A;
3053 hwconf->uart_type = PORT_16450;
3058 request_region(hwconf->ioaddr[0], 8 * hwconf->ports, "mxser(IO)");
3059 request_region(hwconf->vector, 1, "mxser(vector)");
3060 return hwconf->ports;
3063 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
3064 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
3065 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
3066 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
3067 #define EN_CCMD 0x000 /* Chip's command register */
3068 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
3069 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
3070 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
3071 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
3072 #define EN0_DCFG 0x00E /* Data configuration reg WR */
3073 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
3074 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
3075 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
3076 static int mxser_read_register(int port, unsigned short *regs)
3078 int i, k, value, id;
3081 id = mxser_program_mode(port);
3084 for (i = 0; i < 14; i++) {
3085 k = (i & 0x3F) | 0x180;
3086 for (j = 0x100; j > 0; j >>= 1) {
3087 outb(CHIP_CS, port);
3089 outb(CHIP_CS | CHIP_DO, port);
3090 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
3092 outb(CHIP_CS, port);
3093 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
3098 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
3099 outb(CHIP_CS, port);
3100 outb(CHIP_CS | CHIP_SK, port);
3101 if (inb(port) & CHIP_DI)
3107 mxser_normal_mode(port);
3111 static int mxser_program_mode(int port)
3114 /* unsigned long flags; */
3116 spin_lock(&gm_lock);
3124 /* restore_flags(flags); */
3125 spin_unlock(&gm_lock);
3127 id = inb(port + 1) & 0x1F;
3128 if ((id != C168_ASIC_ID) &&
3129 (id != C104_ASIC_ID) &&
3130 (id != C102_ASIC_ID) &&
3131 (id != CI132_ASIC_ID) &&
3132 (id != CI134_ASIC_ID) &&
3133 (id != CI104J_ASIC_ID))
3135 for (i = 0, j = 0; i < 4; i++) {
3139 } else if ((j == 1) && (n == 1)) {
3150 static void mxser_normal_mode(int port)
3154 outb(0xA5, port + 1);
3155 outb(0x80, port + 3);
3156 outb(12, port + 0); /* 9600 bps */
3158 outb(0x03, port + 3); /* 8 data bits */
3159 outb(0x13, port + 4); /* loop back mode */
3160 for (i = 0; i < 16; i++) {
3162 if ((n & 0x61) == 0x60)
3167 outb(0x00, port + 4);
3170 module_init(mxser_module_init);
3171 module_exit(mxser_module_exit);