1 /*****************************************************************************/
3 * moxa.c -- MOXA Intellio family multiport serial driver.
5 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com.tw).
7 * This code is loosely based on the Linux serial driver, written by
8 * Linus Torvalds, Theodore T'so and others.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
17 * MOXA Intellio Series Driver
23 #include <linux/module.h>
24 #include <linux/types.h>
26 #include <linux/ioport.h>
27 #include <linux/errno.h>
28 #include <linux/signal.h>
29 #include <linux/sched.h>
30 #include <linux/timer.h>
31 #include <linux/interrupt.h>
32 #include <linux/tty.h>
33 #include <linux/tty_flip.h>
34 #include <linux/major.h>
35 #include <linux/string.h>
36 #include <linux/fcntl.h>
37 #include <linux/ptrace.h>
38 #include <linux/serial.h>
39 #include <linux/tty_driver.h>
40 #include <linux/delay.h>
41 #include <linux/pci.h>
42 #include <linux/init.h>
43 #include <linux/bitops.h>
45 #include <asm/system.h>
47 #include <asm/uaccess.h>
49 #define MOXA_VERSION "5.1k"
52 #define MOXACUMAJOR 173
54 #define MAX_BOARDS 4 /* Don't change this value */
55 #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
56 #define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
59 * Define the Moxa PCI vendor and device IDs.
61 #define MOXA_BUS_TYPE_ISA 0
62 #define MOXA_BUS_TYPE_PCI 1
65 MOXA_BOARD_C218_PCI = 1,
72 static char *moxa_brdname[] =
74 "C218 Turbo PCI series",
75 "C218 Turbo ISA series",
76 "C320 Turbo PCI series",
77 "C320 Turbo ISA series",
82 static struct pci_device_id moxa_pcibrds[] = {
83 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
84 .driver_data = MOXA_BOARD_C218_PCI },
85 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
86 .driver_data = MOXA_BOARD_C320_PCI },
87 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
88 .driver_data = MOXA_BOARD_CP204J },
91 MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
92 #endif /* CONFIG_PCI */
94 struct moxa_isa_board_conf {
97 unsigned long baseAddr;
100 static struct moxa_isa_board_conf moxa_isa_boards[] =
102 /* {MOXA_BOARD_C218_ISA,8,0xDC000}, */
105 static struct moxa_board_conf {
108 unsigned long baseAddr;
113 void __iomem *basemem;
114 void __iomem *intNdx;
115 void __iomem *intPend;
116 void __iomem *intTable;
117 } moxa_boards[MAX_BOARDS];
119 struct mxser_mstatus {
136 unsigned short closing_wait;
139 long event; /* long req'd for set_bit --RR */
141 unsigned long statusflags;
142 struct tty_struct *tty;
144 wait_queue_head_t open_wait;
145 wait_queue_head_t close_wait;
147 struct timer_list emptyTimer;
151 void __iomem *tableAddr;
160 #define TXSTOPPED 0x1
162 #define EMPTYWAIT 0x4
165 #define SERIAL_DO_RESTART
167 #define WAKEUP_CHARS 256
169 static int ttymajor = MOXAMAJOR;
170 /* Variables for insmod */
172 static int baseaddr[4];
174 static int numports[4];
177 MODULE_AUTHOR("William Chen");
178 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
179 MODULE_LICENSE("GPL");
181 module_param_array(type, int, NULL, 0);
182 module_param_array(baseaddr, int, NULL, 0);
183 module_param_array(numports, int, NULL, 0);
185 module_param(ttymajor, int, 0);
190 static int moxa_open(struct tty_struct *, struct file *);
191 static void moxa_close(struct tty_struct *, struct file *);
192 static int moxa_write(struct tty_struct *, const unsigned char *, int);
193 static int moxa_write_room(struct tty_struct *);
194 static void moxa_flush_buffer(struct tty_struct *);
195 static int moxa_chars_in_buffer(struct tty_struct *);
196 static void moxa_flush_chars(struct tty_struct *);
197 static void moxa_put_char(struct tty_struct *, unsigned char);
198 static int moxa_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);
199 static void moxa_throttle(struct tty_struct *);
200 static void moxa_unthrottle(struct tty_struct *);
201 static void moxa_set_termios(struct tty_struct *, struct ktermios *);
202 static void moxa_stop(struct tty_struct *);
203 static void moxa_start(struct tty_struct *);
204 static void moxa_hangup(struct tty_struct *);
205 static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
206 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
207 unsigned int set, unsigned int clear);
208 static void moxa_poll(unsigned long);
209 static void set_tty_param(struct tty_struct *);
210 static int block_till_ready(struct tty_struct *, struct file *,
212 static void setup_empty_event(struct tty_struct *);
213 static void check_xmit_empty(unsigned long);
214 static void shut_down(struct moxa_port *);
215 static void receive_data(struct moxa_port *);
217 * moxa board interface functions:
219 static void MoxaDriverInit(void);
220 static int MoxaDriverIoctl(unsigned int, unsigned long, int);
221 static int MoxaDriverPoll(void);
222 static int MoxaPortsOfCard(int);
223 static int MoxaPortIsValid(int);
224 static void MoxaPortEnable(int);
225 static void MoxaPortDisable(int);
226 static long MoxaPortGetMaxBaud(int);
227 static long MoxaPortSetBaud(int, long);
228 static int MoxaPortSetTermio(int, struct ktermios *, speed_t);
229 static int MoxaPortGetLineOut(int, int *, int *);
230 static void MoxaPortLineCtrl(int, int, int);
231 static void MoxaPortFlowCtrl(int, int, int, int, int, int);
232 static int MoxaPortLineStatus(int);
233 static int MoxaPortDCDChange(int);
234 static int MoxaPortDCDON(int);
235 static void MoxaPortFlushData(int, int);
236 static int MoxaPortWriteData(int, unsigned char *, int);
237 static int MoxaPortReadData(int, struct tty_struct *tty);
238 static int MoxaPortTxQueue(int);
239 static int MoxaPortRxQueue(int);
240 static int MoxaPortTxFree(int);
241 static void MoxaPortTxDisable(int);
242 static void MoxaPortTxEnable(int);
243 static int MoxaPortResetBrkCnt(int);
244 static void MoxaPortSendBreak(int, int);
245 static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
246 static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
247 static void MoxaSetFifo(int port, int enable);
249 static const struct tty_operations moxa_ops = {
253 .write_room = moxa_write_room,
254 .flush_buffer = moxa_flush_buffer,
255 .chars_in_buffer = moxa_chars_in_buffer,
256 .flush_chars = moxa_flush_chars,
257 .put_char = moxa_put_char,
259 .throttle = moxa_throttle,
260 .unthrottle = moxa_unthrottle,
261 .set_termios = moxa_set_termios,
264 .hangup = moxa_hangup,
265 .tiocmget = moxa_tiocmget,
266 .tiocmset = moxa_tiocmset,
269 static struct tty_driver *moxaDriver;
270 static struct moxa_port moxa_ports[MAX_PORTS];
271 static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
272 static DEFINE_SPINLOCK(moxa_lock);
275 static int __devinit moxa_pci_probe(struct pci_dev *pdev,
276 const struct pci_device_id *ent)
278 struct moxa_board_conf *board;
280 int board_type = ent->driver_data;
283 retval = pci_enable_device(pdev);
285 dev_err(&pdev->dev, "can't enable pci device\n");
289 for (i = 0; i < MAX_BOARDS; i++)
290 if (moxa_boards[i].basemem == NULL)
294 if (i >= MAX_BOARDS) {
295 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
296 "found. Board is ignored.\n", MAX_BOARDS);
300 board = &moxa_boards[i];
301 board->basemem = pci_iomap(pdev, 2, 0x4000);
302 if (board->basemem == NULL) {
303 dev_err(&pdev->dev, "can't remap io space 2\n");
307 board->boardType = board_type;
308 switch (board_type) {
309 case MOXA_BOARD_C218_ISA:
310 case MOXA_BOARD_C218_PCI:
314 case MOXA_BOARD_CP204J:
321 board->busType = MOXA_BUS_TYPE_PCI;
323 pci_set_drvdata(pdev, board);
330 static void __devexit moxa_pci_remove(struct pci_dev *pdev)
332 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
334 pci_iounmap(pdev, brd->basemem);
338 static struct pci_driver moxa_pci_driver = {
340 .id_table = moxa_pcibrds,
341 .probe = moxa_pci_probe,
342 .remove = __devexit_p(moxa_pci_remove)
344 #endif /* CONFIG_PCI */
346 static int __init moxa_init(void)
348 int i, numBoards, retval = 0;
349 struct moxa_port *ch;
351 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
353 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
357 moxaDriver->owner = THIS_MODULE;
358 moxaDriver->name = "ttyMX";
359 moxaDriver->major = ttymajor;
360 moxaDriver->minor_start = 0;
361 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
362 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
363 moxaDriver->init_termios = tty_std_termios;
364 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
365 moxaDriver->init_termios.c_ispeed = 9600;
366 moxaDriver->init_termios.c_ospeed = 9600;
367 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
368 tty_set_operations(moxaDriver, &moxa_ops);
370 for (i = 0, ch = moxa_ports; i < MAX_PORTS; i++, ch++) {
371 ch->type = PORT_16550A;
373 ch->close_delay = 5 * HZ / 10;
374 ch->closing_wait = 30 * HZ;
375 ch->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
376 init_waitqueue_head(&ch->open_wait);
377 init_waitqueue_head(&ch->close_wait);
379 setup_timer(&ch->emptyTimer, check_xmit_empty,
383 pr_debug("Moxa tty devices major number = %d\n", ttymajor);
385 if (tty_register_driver(moxaDriver)) {
386 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
387 put_tty_driver(moxaDriver);
391 mod_timer(&moxaTimer, jiffies + HZ / 50);
393 /* Find the boards defined in source code */
395 for (i = 0; i < MAX_BOARDS; i++) {
396 if ((moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA) ||
397 (moxa_isa_boards[i].boardType == MOXA_BOARD_C320_ISA)) {
398 moxa_boards[numBoards].boardType = moxa_isa_boards[i].boardType;
399 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
400 moxa_boards[numBoards].numPorts = 8;
402 moxa_boards[numBoards].numPorts = moxa_isa_boards[i].numPorts;
403 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
404 moxa_boards[numBoards].baseAddr = moxa_isa_boards[i].baseAddr;
405 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
407 moxa_brdname[moxa_boards[numBoards].boardType-1],
408 moxa_boards[numBoards].baseAddr);
412 /* Find the boards defined form module args. */
414 for (i = 0; i < MAX_BOARDS; i++) {
415 if ((type[i] == MOXA_BOARD_C218_ISA) ||
416 (type[i] == MOXA_BOARD_C320_ISA)) {
417 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
418 numBoards + 1, moxa_brdname[type[i] - 1],
419 (unsigned long)baseaddr[i]);
420 if (numBoards >= MAX_BOARDS) {
421 printk(KERN_WARNING "More than %d MOXA "
422 "Intellio family boards found. Board "
423 "is ignored.\n", MAX_BOARDS);
426 moxa_boards[numBoards].boardType = type[i];
427 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
428 moxa_boards[numBoards].numPorts = 8;
430 moxa_boards[numBoards].numPorts = numports[i];
431 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
432 moxa_boards[numBoards].baseAddr = baseaddr[i];
439 retval = pci_register_driver(&moxa_pci_driver);
441 printk(KERN_ERR "Can't register moxa pci driver!\n");
447 for (i = 0; i < numBoards; i++) {
448 moxa_boards[i].basemem = ioremap(moxa_boards[i].baseAddr,
455 static void __exit moxa_exit(void)
459 del_timer_sync(&moxaTimer);
461 for (i = 0; i < MAX_PORTS; i++)
462 del_timer_sync(&moxa_ports[i].emptyTimer);
464 if (tty_unregister_driver(moxaDriver))
465 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
467 put_tty_driver(moxaDriver);
470 pci_unregister_driver(&moxa_pci_driver);
473 for (i = 0; i < MAX_BOARDS; i++)
474 if (moxa_boards[i].basemem)
475 iounmap(moxa_boards[i].basemem);
478 module_init(moxa_init);
479 module_exit(moxa_exit);
481 static int moxa_open(struct tty_struct *tty, struct file *filp)
483 struct moxa_port *ch;
488 if (port == MAX_PORTS) {
491 if (!MoxaPortIsValid(port)) {
492 tty->driver_data = NULL;
496 ch = &moxa_ports[port];
498 tty->driver_data = ch;
500 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
503 MoxaPortLineCtrl(ch->port, 1, 1);
504 MoxaPortEnable(ch->port);
505 ch->asyncflags |= ASYNC_INITIALIZED;
507 retval = block_till_ready(tty, filp, ch);
509 moxa_unthrottle(tty);
511 if (ch->type == PORT_16550A) {
512 MoxaSetFifo(ch->port, 1);
514 MoxaSetFifo(ch->port, 0);
520 static void moxa_close(struct tty_struct *tty, struct file *filp)
522 struct moxa_port *ch;
526 if (port == MAX_PORTS) {
529 if (!MoxaPortIsValid(port)) {
530 pr_debug("Invalid portno in moxa_close\n");
531 tty->driver_data = NULL;
534 if (tty->driver_data == NULL) {
537 if (tty_hung_up_p(filp)) {
540 ch = (struct moxa_port *) tty->driver_data;
542 if ((tty->count == 1) && (ch->count != 1)) {
543 printk(KERN_WARNING "moxa_close: bad serial port count; "
544 "tty->count is 1, ch->count is %d\n", ch->count);
547 if (--ch->count < 0) {
548 printk(KERN_WARNING "moxa_close: bad serial port count, "
549 "device=%s\n", tty->name);
555 ch->asyncflags |= ASYNC_CLOSING;
557 ch->cflag = tty->termios->c_cflag;
558 if (ch->asyncflags & ASYNC_INITIALIZED) {
559 setup_empty_event(tty);
560 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
561 del_timer_sync(&moxa_ports[ch->port].emptyTimer);
564 MoxaPortFlushData(port, 2);
566 if (tty->driver->flush_buffer)
567 tty->driver->flush_buffer(tty);
568 tty_ldisc_flush(tty);
573 if (ch->blocked_open) {
574 if (ch->close_delay) {
575 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
577 wake_up_interruptible(&ch->open_wait);
579 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
580 wake_up_interruptible(&ch->close_wait);
583 static int moxa_write(struct tty_struct *tty,
584 const unsigned char *buf, int count)
586 struct moxa_port *ch;
590 ch = (struct moxa_port *) tty->driver_data;
595 spin_lock_irqsave(&moxa_lock, flags);
596 len = MoxaPortWriteData(port, (unsigned char *) buf, count);
597 spin_unlock_irqrestore(&moxa_lock, flags);
599 /*********************************************
600 if ( !(ch->statusflags & LOWWAIT) &&
601 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
602 ************************************************/
603 ch->statusflags |= LOWWAIT;
607 static int moxa_write_room(struct tty_struct *tty)
609 struct moxa_port *ch;
613 ch = (struct moxa_port *) tty->driver_data;
616 return (MoxaPortTxFree(ch->port));
619 static void moxa_flush_buffer(struct tty_struct *tty)
621 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
625 MoxaPortFlushData(ch->port, 1);
629 static int moxa_chars_in_buffer(struct tty_struct *tty)
632 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
635 * Sigh...I have to check if driver_data is NULL here, because
636 * if an open() fails, the TTY subsystem eventually calls
637 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
638 * routine. And since the open() failed, we return 0 here. TDJ
642 chars = MoxaPortTxQueue(ch->port);
645 * Make it possible to wakeup anything waiting for output
646 * in tty_ioctl.c, etc.
648 if (!(ch->statusflags & EMPTYWAIT))
649 setup_empty_event(tty);
654 static void moxa_flush_chars(struct tty_struct *tty)
657 * Don't think I need this, because this is called to empty the TX
658 * buffer for the 16450, 16550, etc.
662 static void moxa_put_char(struct tty_struct *tty, unsigned char c)
664 struct moxa_port *ch;
668 ch = (struct moxa_port *) tty->driver_data;
672 spin_lock_irqsave(&moxa_lock, flags);
673 MoxaPortWriteData(port, &c, 1);
674 spin_unlock_irqrestore(&moxa_lock, flags);
675 /************************************************
676 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
677 *************************************************/
678 ch->statusflags |= LOWWAIT;
681 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
683 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
685 int flag = 0, dtr, rts;
688 if ((port != MAX_PORTS) && (!ch))
691 MoxaPortGetLineOut(ch->port, &dtr, &rts);
696 dtr = MoxaPortLineStatus(ch->port);
706 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
707 unsigned int set, unsigned int clear)
709 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
714 if ((port != MAX_PORTS) && (!ch))
717 MoxaPortGetLineOut(ch->port, &dtr, &rts);
722 if (clear & TIOCM_RTS)
724 if (clear & TIOCM_DTR)
726 MoxaPortLineCtrl(ch->port, dtr, rts);
730 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
731 unsigned int cmd, unsigned long arg)
733 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
735 void __user *argp = (void __user *)arg;
739 if ((port != MAX_PORTS) && (!ch))
743 case TCSBRK: /* SVID version: non-zero arg --> no break */
744 retval = tty_check_change(tty);
747 setup_empty_event(tty);
748 tty_wait_until_sent(tty, 0);
750 MoxaPortSendBreak(ch->port, 0);
752 case TCSBRKP: /* support for POSIX tcsendbreak() */
753 retval = tty_check_change(tty);
756 setup_empty_event(tty);
757 tty_wait_until_sent(tty, 0);
758 MoxaPortSendBreak(ch->port, arg);
761 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) argp);
763 if(get_user(retval, (unsigned long __user *) argp))
766 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
769 ch->asyncflags &= ~ASYNC_CHECK_CD;
771 ch->asyncflags |= ASYNC_CHECK_CD;
774 return moxa_get_serial_info(ch, argp);
777 return moxa_set_serial_info(ch, argp);
779 retval = MoxaDriverIoctl(cmd, arg, port);
784 static void moxa_throttle(struct tty_struct *tty)
786 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
788 ch->statusflags |= THROTTLE;
791 static void moxa_unthrottle(struct tty_struct *tty)
793 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
795 ch->statusflags &= ~THROTTLE;
798 static void moxa_set_termios(struct tty_struct *tty,
799 struct ktermios *old_termios)
801 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
806 if (!(old_termios->c_cflag & CLOCAL) &&
807 (tty->termios->c_cflag & CLOCAL))
808 wake_up_interruptible(&ch->open_wait);
811 static void moxa_stop(struct tty_struct *tty)
813 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
817 MoxaPortTxDisable(ch->port);
818 ch->statusflags |= TXSTOPPED;
822 static void moxa_start(struct tty_struct *tty)
824 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
829 if (!(ch->statusflags & TXSTOPPED))
832 MoxaPortTxEnable(ch->port);
833 ch->statusflags &= ~TXSTOPPED;
836 static void moxa_hangup(struct tty_struct *tty)
838 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
840 moxa_flush_buffer(tty);
844 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
846 wake_up_interruptible(&ch->open_wait);
849 static void moxa_poll(unsigned long ignored)
852 struct moxa_port *ch;
853 struct tty_struct *tp;
856 del_timer(&moxaTimer);
858 if (MoxaDriverPoll() < 0) {
859 mod_timer(&moxaTimer, jiffies + HZ / 50);
862 for (card = 0; card < MAX_BOARDS; card++) {
863 if ((ports = MoxaPortsOfCard(card)) <= 0)
865 ch = &moxa_ports[card * MAX_PORTS_PER_BOARD];
866 for (i = 0; i < ports; i++, ch++) {
867 if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
869 if (!(ch->statusflags & THROTTLE) &&
870 (MoxaPortRxQueue(ch->port) > 0))
872 if ((tp = ch->tty) == 0)
874 if (ch->statusflags & LOWWAIT) {
875 if (MoxaPortTxQueue(ch->port) <= WAKEUP_CHARS) {
877 ch->statusflags &= ~LOWWAIT;
882 if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch->port) > 0)) {
883 tty_insert_flip_char(tp, 0, TTY_BREAK);
884 tty_schedule_flip(tp);
886 if (MoxaPortDCDChange(ch->port)) {
887 if (ch->asyncflags & ASYNC_CHECK_CD) {
888 if (MoxaPortDCDON(ch->port))
889 wake_up_interruptible(&ch->open_wait);
892 wake_up_interruptible(&ch->open_wait);
893 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
900 mod_timer(&moxaTimer, jiffies + HZ / 50);
903 /******************************************************************************/
905 static void set_tty_param(struct tty_struct *tty)
907 register struct ktermios *ts;
908 struct moxa_port *ch;
909 int rts, cts, txflow, rxflow, xany;
911 ch = (struct moxa_port *) tty->driver_data;
913 if (ts->c_cflag & CLOCAL)
914 ch->asyncflags &= ~ASYNC_CHECK_CD;
916 ch->asyncflags |= ASYNC_CHECK_CD;
917 rts = cts = txflow = rxflow = xany = 0;
918 if (ts->c_cflag & CRTSCTS)
920 if (ts->c_iflag & IXON)
922 if (ts->c_iflag & IXOFF)
924 if (ts->c_iflag & IXANY)
926 MoxaPortFlowCtrl(ch->port, rts, cts, txflow, rxflow, xany);
927 MoxaPortSetTermio(ch->port, ts, tty_get_baud_rate(tty));
930 static int block_till_ready(struct tty_struct *tty, struct file *filp,
931 struct moxa_port *ch)
933 DECLARE_WAITQUEUE(wait,current);
936 int do_clocal = C_CLOCAL(tty);
939 * If the device is in the middle of being closed, then block
940 * until it's done, and then try again.
942 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
943 if (ch->asyncflags & ASYNC_CLOSING)
944 interruptible_sleep_on(&ch->close_wait);
945 #ifdef SERIAL_DO_RESTART
946 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
949 return (-ERESTARTSYS);
955 * If non-blocking mode is set, then make the check up front
958 if (filp->f_flags & O_NONBLOCK) {
959 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
963 * Block waiting for the carrier detect and the line to become free
966 add_wait_queue(&ch->open_wait, &wait);
967 pr_debug("block_til_ready before block: ttys%d, count = %d\n",
968 ch->port, ch->count);
969 spin_lock_irqsave(&moxa_lock, flags);
970 if (!tty_hung_up_p(filp))
973 spin_unlock_irqrestore(&moxa_lock, flags);
976 set_current_state(TASK_INTERRUPTIBLE);
977 if (tty_hung_up_p(filp) ||
978 !(ch->asyncflags & ASYNC_INITIALIZED)) {
979 #ifdef SERIAL_DO_RESTART
980 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
983 retval = -ERESTARTSYS;
989 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
990 MoxaPortDCDON(ch->port)))
993 if (signal_pending(current)) {
994 retval = -ERESTARTSYS;
999 set_current_state(TASK_RUNNING);
1000 remove_wait_queue(&ch->open_wait, &wait);
1002 spin_lock_irqsave(&moxa_lock, flags);
1003 if (!tty_hung_up_p(filp))
1006 spin_unlock_irqrestore(&moxa_lock, flags);
1007 pr_debug("block_til_ready after blocking: ttys%d, count = %d\n",
1008 ch->port, ch->count);
1011 /* FIXME: review to see if we need to use set_bit on these */
1012 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1016 static void setup_empty_event(struct tty_struct *tty)
1018 struct moxa_port *ch = tty->driver_data;
1019 unsigned long flags;
1021 spin_lock_irqsave(&moxa_lock, flags);
1022 ch->statusflags |= EMPTYWAIT;
1023 mod_timer(&moxa_ports[ch->port].emptyTimer, jiffies + HZ);
1024 spin_unlock_irqrestore(&moxa_lock, flags);
1027 static void check_xmit_empty(unsigned long data)
1029 struct moxa_port *ch;
1031 ch = (struct moxa_port *) data;
1032 if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
1033 if (MoxaPortTxQueue(ch->port) == 0) {
1034 ch->statusflags &= ~EMPTYWAIT;
1035 tty_wakeup(ch->tty);
1038 mod_timer(&moxa_ports[ch->port].emptyTimer,
1039 round_jiffies(jiffies + HZ));
1041 ch->statusflags &= ~EMPTYWAIT;
1044 static void shut_down(struct moxa_port *ch)
1046 struct tty_struct *tp;
1048 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1053 MoxaPortDisable(ch->port);
1056 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1058 if (tp->termios->c_cflag & HUPCL)
1059 MoxaPortLineCtrl(ch->port, 0, 0);
1061 ch->asyncflags &= ~ASYNC_INITIALIZED;
1064 static void receive_data(struct moxa_port *ch)
1066 struct tty_struct *tp;
1067 struct ktermios *ts;
1068 unsigned long flags;
1074 /**************************************************
1075 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1076 *****************************************************/
1078 MoxaPortFlushData(ch->port, 0);
1081 spin_lock_irqsave(&moxa_lock, flags);
1082 MoxaPortReadData(ch->port, tp);
1083 spin_unlock_irqrestore(&moxa_lock, flags);
1084 tty_schedule_flip(tp);
1087 #define Magic_code 0x404
1090 * System Configuration
1093 * for C218 BIOS initialization
1095 #define C218_ConfBase 0x800
1096 #define C218_status (C218_ConfBase + 0) /* BIOS running status */
1097 #define C218_diag (C218_ConfBase + 2) /* diagnostic status */
1098 #define C218_key (C218_ConfBase + 4) /* WORD (0x218 for C218) */
1099 #define C218DLoad_len (C218_ConfBase + 6) /* WORD */
1100 #define C218check_sum (C218_ConfBase + 8) /* BYTE */
1101 #define C218chksum_ok (C218_ConfBase + 0x0a) /* BYTE (1:ok) */
1102 #define C218_TestRx (C218_ConfBase + 0x10) /* 8 bytes for 8 ports */
1103 #define C218_TestTx (C218_ConfBase + 0x18) /* 8 bytes for 8 ports */
1104 #define C218_RXerr (C218_ConfBase + 0x20) /* 8 bytes for 8 ports */
1105 #define C218_ErrFlag (C218_ConfBase + 0x28) /* 8 bytes for 8 ports */
1107 #define C218_LoadBuf 0x0F00
1108 #define C218_KeyCode 0x218
1109 #define CP204J_KeyCode 0x204
1112 * for C320 BIOS initialization
1114 #define C320_ConfBase 0x800
1115 #define C320_LoadBuf 0x0f00
1116 #define STS_init 0x05 /* for C320_status */
1118 #define C320_status C320_ConfBase + 0 /* BIOS running status */
1119 #define C320_diag C320_ConfBase + 2 /* diagnostic status */
1120 #define C320_key C320_ConfBase + 4 /* WORD (0320H for C320) */
1121 #define C320DLoad_len C320_ConfBase + 6 /* WORD */
1122 #define C320check_sum C320_ConfBase + 8 /* WORD */
1123 #define C320chksum_ok C320_ConfBase + 0x0a /* WORD (1:ok) */
1124 #define C320bapi_len C320_ConfBase + 0x0c /* WORD */
1125 #define C320UART_no C320_ConfBase + 0x0e /* WORD */
1127 #define C320_KeyCode 0x320
1129 #define FixPage_addr 0x0000 /* starting addr of static page */
1130 #define DynPage_addr 0x2000 /* starting addr of dynamic page */
1131 #define C218_start 0x3000 /* starting addr of C218 BIOS prg */
1132 #define Control_reg 0x1ff0 /* select page and reset control */
1133 #define HW_reset 0x80
1138 #define FC_CardReset 0x80
1139 #define FC_ChannelReset 1 /* C320 firmware not supported */
1140 #define FC_EnableCH 2
1141 #define FC_DisableCH 3
1142 #define FC_SetParam 4
1143 #define FC_SetMode 5
1144 #define FC_SetRate 6
1145 #define FC_LineControl 7
1146 #define FC_LineStatus 8
1147 #define FC_XmitControl 9
1148 #define FC_FlushQueue 10
1149 #define FC_SendBreak 11
1150 #define FC_StopBreak 12
1151 #define FC_LoopbackON 13
1152 #define FC_LoopbackOFF 14
1153 #define FC_ClrIrqTable 15
1154 #define FC_SendXon 16
1155 #define FC_SetTermIrq 17 /* C320 firmware not supported */
1156 #define FC_SetCntIrq 18 /* C320 firmware not supported */
1157 #define FC_SetBreakIrq 19
1158 #define FC_SetLineIrq 20
1159 #define FC_SetFlowCtl 21
1160 #define FC_GenIrq 22
1161 #define FC_InCD180 23
1162 #define FC_OutCD180 24
1163 #define FC_InUARTreg 23
1164 #define FC_OutUARTreg 24
1165 #define FC_SetXonXoff 25
1166 #define FC_OutCD180CCR 26
1167 #define FC_ExtIQueue 27
1168 #define FC_ExtOQueue 28
1169 #define FC_ClrLineIrq 29
1170 #define FC_HWFlowCtl 30
1171 #define FC_GetClockRate 35
1172 #define FC_SetBaud 36
1173 #define FC_SetDataMode 41
1174 #define FC_GetCCSR 43
1175 #define FC_GetDataError 45
1176 #define FC_RxControl 50
1177 #define FC_ImmSend 51
1178 #define FC_SetXonState 52
1179 #define FC_SetXoffState 53
1180 #define FC_SetRxFIFOTrig 54
1181 #define FC_SetTxFIFOCnt 55
1182 #define FC_UnixRate 56
1183 #define FC_UnixResetTimer 57
1185 #define RxFIFOTrig1 0
1186 #define RxFIFOTrig4 1
1187 #define RxFIFOTrig8 2
1188 #define RxFIFOTrig14 3
1193 #define DRAM_global 0
1194 #define INT_data (DRAM_global + 0)
1195 #define Config_base (DRAM_global + 0x108)
1197 #define IRQindex (INT_data + 0)
1198 #define IRQpending (INT_data + 4)
1199 #define IRQtable (INT_data + 8)
1204 #define IntrRx 0x01 /* receiver data O.K. */
1205 #define IntrTx 0x02 /* transmit buffer empty */
1206 #define IntrFunc 0x04 /* function complete */
1207 #define IntrBreak 0x08 /* received break */
1208 #define IntrLine 0x10 /* line status change
1210 #define IntrIntr 0x20 /* received INTR code */
1211 #define IntrQuit 0x40 /* received QUIT code */
1212 #define IntrEOF 0x80 /* received EOF code */
1214 #define IntrRxTrigger 0x100 /* rx data count reach tigger value */
1215 #define IntrTxTrigger 0x200 /* tx data count below trigger value */
1217 #define Magic_no (Config_base + 0)
1218 #define Card_model_no (Config_base + 2)
1219 #define Total_ports (Config_base + 4)
1220 #define Module_cnt (Config_base + 8)
1221 #define Module_no (Config_base + 10)
1222 #define Timer_10ms (Config_base + 14)
1223 #define Disable_IRQ (Config_base + 20)
1224 #define TMS320_PORT1 (Config_base + 22)
1225 #define TMS320_PORT2 (Config_base + 24)
1226 #define TMS320_CLOCK (Config_base + 26)
1229 * DATA BUFFER in DRAM
1231 #define Extern_table 0x400 /* Base address of the external table
1232 (24 words * 64) total 3K bytes
1233 (24 words * 128) total 6K bytes */
1234 #define Extern_size 0x60 /* 96 bytes */
1235 #define RXrptr 0x00 /* read pointer for RX buffer */
1236 #define RXwptr 0x02 /* write pointer for RX buffer */
1237 #define TXrptr 0x04 /* read pointer for TX buffer */
1238 #define TXwptr 0x06 /* write pointer for TX buffer */
1239 #define HostStat 0x08 /* IRQ flag and general flag */
1240 #define FlagStat 0x0A
1241 #define FlowControl 0x0C /* B7 B6 B5 B4 B3 B2 B1 B0 */
1242 /* x x x x | | | | */
1243 /* | | | + CTS flow */
1244 /* | | +--- RTS flow */
1245 /* | +------ TX Xon/Xoff */
1246 /* +--------- RX Xon/Xoff */
1247 #define Break_cnt 0x0E /* received break count */
1248 #define CD180TXirq 0x10 /* if non-0: enable TX irq */
1249 #define RX_mask 0x12
1250 #define TX_mask 0x14
1251 #define Ofs_rxb 0x16
1252 #define Ofs_txb 0x18
1253 #define Page_rxb 0x1A
1254 #define Page_txb 0x1C
1255 #define EndPage_rxb 0x1E
1256 #define EndPage_txb 0x20
1257 #define Data_error 0x22
1258 #define RxTrigger 0x28
1259 #define TxTrigger 0x2a
1261 #define rRXwptr 0x34
1262 #define Low_water 0x36
1264 #define FuncCode 0x40
1265 #define FuncArg 0x42
1266 #define FuncArg1 0x44
1268 #define C218rx_size 0x2000 /* 8K bytes */
1269 #define C218tx_size 0x8000 /* 32K bytes */
1271 #define C218rx_mask (C218rx_size - 1)
1272 #define C218tx_mask (C218tx_size - 1)
1274 #define C320p8rx_size 0x2000
1275 #define C320p8tx_size 0x8000
1276 #define C320p8rx_mask (C320p8rx_size - 1)
1277 #define C320p8tx_mask (C320p8tx_size - 1)
1279 #define C320p16rx_size 0x2000
1280 #define C320p16tx_size 0x4000
1281 #define C320p16rx_mask (C320p16rx_size - 1)
1282 #define C320p16tx_mask (C320p16tx_size - 1)
1284 #define C320p24rx_size 0x2000
1285 #define C320p24tx_size 0x2000
1286 #define C320p24rx_mask (C320p24rx_size - 1)
1287 #define C320p24tx_mask (C320p24tx_size - 1)
1289 #define C320p32rx_size 0x1000
1290 #define C320p32tx_size 0x1000
1291 #define C320p32rx_mask (C320p32rx_size - 1)
1292 #define C320p32tx_mask (C320p32tx_size - 1)
1294 #define Page_size 0x2000
1295 #define Page_mask (Page_size - 1)
1296 #define C218rx_spage 3
1297 #define C218tx_spage 4
1298 #define C218rx_pageno 1
1299 #define C218tx_pageno 4
1300 #define C218buf_pageno 5
1302 #define C320p8rx_spage 3
1303 #define C320p8tx_spage 4
1304 #define C320p8rx_pgno 1
1305 #define C320p8tx_pgno 4
1306 #define C320p8buf_pgno 5
1308 #define C320p16rx_spage 3
1309 #define C320p16tx_spage 4
1310 #define C320p16rx_pgno 1
1311 #define C320p16tx_pgno 2
1312 #define C320p16buf_pgno 3
1314 #define C320p24rx_spage 3
1315 #define C320p24tx_spage 4
1316 #define C320p24rx_pgno 1
1317 #define C320p24tx_pgno 1
1318 #define C320p24buf_pgno 2
1320 #define C320p32rx_spage 3
1321 #define C320p32tx_ofs C320p32rx_size
1322 #define C320p32tx_spage 3
1323 #define C320p32buf_pgno 1
1328 #define WakeupRx 0x01
1329 #define WakeupTx 0x02
1330 #define WakeupBreak 0x08
1331 #define WakeupLine 0x10
1332 #define WakeupIntr 0x20
1333 #define WakeupQuit 0x40
1334 #define WakeupEOF 0x80 /* used in VTIME control */
1335 #define WakeupRxTrigger 0x100
1336 #define WakeupTxTrigger 0x200
1340 #define Rx_over 0x01
1341 #define Xoff_state 0x02
1342 #define Tx_flowOff 0x04
1343 #define Tx_enable 0x08
1344 #define CTS_state 0x10
1345 #define DSR_state 0x20
1346 #define DCD_state 0x80
1350 #define CTS_FlowCtl 1
1351 #define RTS_FlowCtl 2
1352 #define Tx_FlowCtl 4
1353 #define Rx_FlowCtl 8
1354 #define IXM_IXANY 0x10
1356 #define LowWater 128
1364 /* mode definition */
1370 #define MX_STOP1 0x00
1371 #define MX_STOP15 0x04
1372 #define MX_STOP2 0x08
1374 #define MX_PARNONE 0x00
1375 #define MX_PAREVEN 0x40
1376 #define MX_PARODD 0xC0
1384 int rxcnt[MAX_PORTS];
1385 int txcnt[MAX_PORTS];
1388 #define DCD_changed 0x01
1389 #define DCD_oldstate 0x80
1391 static unsigned char moxaBuff[10240];
1392 static int moxaLowWaterChk;
1393 static int moxaCard;
1394 static struct mon_str moxaLog;
1395 static int moxaFuncTout = HZ / 2;
1397 static void moxafunc(void __iomem *, int, ushort);
1398 static void wait_finish(void __iomem *);
1399 static void low_water_check(void __iomem *);
1400 static int moxaloadbios(int, unsigned char __user *, int);
1401 static int moxafindcard(int);
1402 static int moxaload320b(int, unsigned char __user *, int);
1403 static int moxaloadcode(int, unsigned char __user *, int);
1404 static int moxaloadc218(int, void __iomem *, int);
1405 static int moxaloadc320(int, void __iomem *, int, int *);
1407 /*****************************************************************************
1408 * Driver level functions: *
1409 * 1. MoxaDriverInit(void); *
1410 * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
1411 * 3. MoxaDriverPoll(void); *
1412 *****************************************************************************/
1413 void MoxaDriverInit(void)
1415 struct moxa_port *p;
1418 moxaFuncTout = HZ / 2; /* 500 mini-seconds */
1421 moxaLowWaterChk = 0;
1422 for (i = 0; i < MAX_PORTS; i++) {
1427 moxaLog.rxcnt[i] = 0;
1428 moxaLog.txcnt[i] = 0;
1433 #define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1434 #define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
1435 #define MOXA_INIT_DRIVER (MOXA + 6) /* moxaCard=0 */
1436 #define MOXA_LOAD_BIOS (MOXA + 9) /* download BIOS */
1437 #define MOXA_FIND_BOARD (MOXA + 10) /* Check if MOXA card exist? */
1438 #define MOXA_LOAD_C320B (MOXA + 11) /* download 320B firmware */
1439 #define MOXA_LOAD_CODE (MOXA + 12) /* download firmware */
1440 #define MOXA_GETDATACOUNT (MOXA + 23)
1441 #define MOXA_GET_IOQUEUE (MOXA + 27)
1442 #define MOXA_FLUSH_QUEUE (MOXA + 28)
1443 #define MOXA_GET_CONF (MOXA + 35) /* configuration */
1444 #define MOXA_GET_MAJOR (MOXA + 63)
1445 #define MOXA_GET_CUMAJOR (MOXA + 64)
1446 #define MOXA_GETMSTATUS (MOXA + 65)
1454 static struct dl_str dltmp;
1456 void MoxaPortFlushData(int port, int mode)
1458 void __iomem *ofsAddr;
1459 if ((mode < 0) || (mode > 2))
1461 ofsAddr = moxa_ports[port].tableAddr;
1462 moxafunc(ofsAddr, FC_FlushQueue, mode);
1464 moxa_ports[port].lowChkFlag = 0;
1465 low_water_check(ofsAddr);
1469 int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port)
1473 int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
1474 void __user *argp = (void __user *)arg;
1476 if (port == MAX_PORTS) {
1477 if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_INIT_DRIVER) &&
1478 (cmd != MOXA_LOAD_BIOS) && (cmd != MOXA_FIND_BOARD) && (cmd != MOXA_LOAD_C320B) &&
1479 (cmd != MOXA_LOAD_CODE) && (cmd != MOXA_GETDATACOUNT) &&
1480 (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
1481 (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
1486 if(copy_to_user(argp, &moxa_boards, MAX_BOARDS *
1487 sizeof(struct moxa_board_conf)))
1490 case MOXA_INIT_DRIVER:
1491 if ((int) arg == 0x404)
1494 case MOXA_GETDATACOUNT:
1495 moxaLog.tick = jiffies;
1496 if(copy_to_user(argp, &moxaLog, sizeof(struct mon_str)))
1499 case MOXA_FLUSH_QUEUE:
1500 MoxaPortFlushData(port, arg);
1502 case MOXA_GET_IOQUEUE: {
1503 struct moxaq_str __user *argm = argp;
1504 struct moxaq_str tmp;
1506 for (i = 0; i < MAX_PORTS; i++, argm++) {
1507 memset(&tmp, 0, sizeof(tmp));
1508 if (moxa_ports[i].chkPort) {
1509 tmp.inq = MoxaPortRxQueue(i);
1510 tmp.outq = MoxaPortTxQueue(i);
1512 if (copy_to_user(argm, &tmp, sizeof(tmp)))
1516 } case MOXA_GET_OQUEUE:
1517 i = MoxaPortTxQueue(port);
1518 return put_user(i, (unsigned long __user *)argp);
1519 case MOXA_GET_IQUEUE:
1520 i = MoxaPortRxQueue(port);
1521 return put_user(i, (unsigned long __user *)argp);
1522 case MOXA_GET_MAJOR:
1523 if(copy_to_user(argp, &ttymajor, sizeof(int)))
1526 case MOXA_GET_CUMAJOR:
1528 if(copy_to_user(argp, &i, sizeof(int)))
1531 case MOXA_GETMSTATUS: {
1532 struct mxser_mstatus __user *argm = argp;
1533 struct mxser_mstatus tmp;
1534 struct moxa_port *p;
1536 for (i = 0; i < MAX_PORTS; i++, argm++) {
1538 memset(&tmp, 0, sizeof(tmp));
1542 status = MoxaPortLineStatus(p->port);
1551 if (!p->tty || !p->tty->termios)
1552 tmp.cflag = p->cflag;
1554 tmp.cflag = p->tty->termios->c_cflag;
1556 if (copy_to_user(argm, &tmp, sizeof(tmp)))
1561 return (-ENOIOCTLCMD);
1562 case MOXA_LOAD_BIOS:
1563 case MOXA_FIND_BOARD:
1564 case MOXA_LOAD_C320B:
1565 case MOXA_LOAD_CODE:
1566 if (!capable(CAP_SYS_RAWIO))
1571 if(copy_from_user(&dltmp, argp, sizeof(struct dl_str)))
1573 if(dltmp.cardno < 0 || dltmp.cardno >= MAX_BOARDS || dltmp.len < 0)
1578 case MOXA_LOAD_BIOS:
1579 i = moxaloadbios(dltmp.cardno, dltmp.buf, dltmp.len);
1581 case MOXA_FIND_BOARD:
1582 return moxafindcard(dltmp.cardno);
1583 case MOXA_LOAD_C320B:
1584 moxaload320b(dltmp.cardno, dltmp.buf, dltmp.len);
1585 default: /* to keep gcc happy */
1587 case MOXA_LOAD_CODE:
1588 i = moxaloadcode(dltmp.cardno, dltmp.buf, dltmp.len);
1596 int MoxaDriverPoll(void)
1598 struct moxa_board_conf *brd;
1599 register ushort temp;
1601 void __iomem *ofsAddr;
1607 for (card = 0; card < MAX_BOARDS; card++) {
1608 brd = &moxa_boards[card];
1609 if (brd->loadstat == 0)
1611 if ((ports = brd->numPorts) == 0)
1613 if (readb(brd->intPend) == 0xff) {
1614 ip = brd->intTable + readb(brd->intNdx);
1615 p = card * MAX_PORTS_PER_BOARD;
1617 for (port = 0; port < ports; port += 2, p++) {
1618 if ((temp = readw(ip + port)) != 0) {
1619 writew(0, ip + port);
1620 ofsAddr = moxa_ports[p].tableAddr;
1622 writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
1623 if (temp & IntrBreak) {
1624 moxa_ports[p].breakCnt++;
1626 if (temp & IntrLine) {
1627 if (readb(ofsAddr + FlagStat) & DCD_state) {
1628 if ((moxa_ports[p].DCDState & DCD_oldstate) == 0)
1629 moxa_ports[p].DCDState = (DCD_oldstate |
1632 if (moxa_ports[p].DCDState & DCD_oldstate)
1633 moxa_ports[p].DCDState = DCD_changed;
1638 writeb(0, brd->intPend);
1640 if (moxaLowWaterChk) {
1641 p = card * MAX_PORTS_PER_BOARD;
1642 for (port = 0; port < ports; port++, p++) {
1643 if (moxa_ports[p].lowChkFlag) {
1644 moxa_ports[p].lowChkFlag = 0;
1645 ofsAddr = moxa_ports[p].tableAddr;
1646 low_water_check(ofsAddr);
1651 moxaLowWaterChk = 0;
1655 /*****************************************************************************
1656 * Card level function: *
1657 * 1. MoxaPortsOfCard(int cardno); *
1658 *****************************************************************************/
1659 int MoxaPortsOfCard(int cardno)
1662 if (moxa_boards[cardno].boardType == 0)
1664 return (moxa_boards[cardno].numPorts);
1667 /*****************************************************************************
1668 * Port level functions: *
1669 * 1. MoxaPortIsValid(int port); *
1670 * 2. MoxaPortEnable(int port); *
1671 * 3. MoxaPortDisable(int port); *
1672 * 4. MoxaPortGetMaxBaud(int port); *
1673 * 6. MoxaPortSetBaud(int port, long baud); *
1674 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1675 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1676 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1677 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1678 * 12. MoxaPortLineStatus(int port); *
1679 * 13. MoxaPortDCDChange(int port); *
1680 * 14. MoxaPortDCDON(int port); *
1681 * 15. MoxaPortFlushData(int port, int mode); *
1682 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1683 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
1684 * 20. MoxaPortTxQueue(int port); *
1685 * 21. MoxaPortTxFree(int port); *
1686 * 22. MoxaPortRxQueue(int port); *
1687 * 24. MoxaPortTxDisable(int port); *
1688 * 25. MoxaPortTxEnable(int port); *
1689 * 27. MoxaPortResetBrkCnt(int port); *
1690 * 30. MoxaPortSendBreak(int port, int ticks); *
1691 *****************************************************************************/
1693 * Moxa Port Number Description:
1695 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1696 * the port number using in MOXA driver functions will be 0 to 31 for
1697 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1698 * to 127 for fourth. For example, if you setup three MOXA boards,
1699 * first board is C218, second board is C320-16 and third board is
1700 * C320-32. The port number of first board (C218 - 8 ports) is from
1701 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1702 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1703 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1704 * 127 will be invalid.
1707 * Moxa Functions Description:
1709 * Function 1: Driver initialization routine, this routine must be
1710 * called when initialized driver.
1712 * void MoxaDriverInit();
1715 * Function 2: Moxa driver private IOCTL command processing.
1717 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1719 * unsigned int cmd : IOCTL command
1720 * unsigned long arg : IOCTL argument
1721 * int port : port number (0 - 127)
1728 * Function 3: Moxa driver polling process routine.
1730 * int MoxaDriverPoll(void);
1732 * return: 0 ; polling O.K.
1733 * -1 : no any Moxa card.
1736 * Function 4: Get the ports of this card.
1738 * int MoxaPortsOfCard(int cardno);
1740 * int cardno : card number (0 - 3)
1742 * return: 0 : this card is invalid
1746 * Function 5: Check this port is valid or invalid
1748 * int MoxaPortIsValid(int port);
1749 * int port : port number (0 - 127, ref port description)
1751 * return: 0 : this port is invalid
1752 * 1 : this port is valid
1755 * Function 6: Enable this port to start Tx/Rx data.
1757 * void MoxaPortEnable(int port);
1758 * int port : port number (0 - 127)
1761 * Function 7: Disable this port
1763 * void MoxaPortDisable(int port);
1764 * int port : port number (0 - 127)
1767 * Function 8: Get the maximun available baud rate of this port.
1769 * long MoxaPortGetMaxBaud(int port);
1770 * int port : port number (0 - 127)
1772 * return: 0 : this port is invalid
1773 * 38400/57600/115200 bps
1776 * Function 10: Setting baud rate of this port.
1778 * long MoxaPortSetBaud(int port, long baud);
1779 * int port : port number (0 - 127)
1780 * long baud : baud rate (50 - 115200)
1782 * return: 0 : this port is invalid or baud < 50
1783 * 50 - 115200 : the real baud rate set to the port, if
1784 * the argument baud is large than maximun
1785 * available baud rate, the real setting
1786 * baud rate will be the maximun baud rate.
1789 * Function 12: Configure the port.
1791 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1792 * int port : port number (0 - 127)
1793 * struct ktermios * termio : termio structure pointer
1794 * speed_t baud : baud rate
1796 * return: -1 : this port is invalid or termio == NULL
1800 * Function 13: Get the DTR/RTS state of this port.
1802 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1803 * int port : port number (0 - 127)
1804 * int * dtrState : pointer to INT to receive the current DTR
1805 * state. (if NULL, this function will not
1806 * write to this address)
1807 * int * rtsState : pointer to INT to receive the current RTS
1808 * state. (if NULL, this function will not
1809 * write to this address)
1811 * return: -1 : this port is invalid
1815 * Function 14: Setting the DTR/RTS output state of this port.
1817 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1818 * int port : port number (0 - 127)
1819 * int dtrState : DTR output state (0: off, 1: on)
1820 * int rtsState : RTS output state (0: off, 1: on)
1823 * Function 15: Setting the flow control of this port.
1825 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1826 * int txFlow,int xany);
1827 * int port : port number (0 - 127)
1828 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1829 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1830 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1831 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1832 * int xany : S/W XANY flow control (0: no, 1: yes)
1835 * Function 16: Get ths line status of this port
1837 * int MoxaPortLineStatus(int port);
1838 * int port : port number (0 - 127)
1840 * return: Bit 0 - CTS state (0: off, 1: on)
1841 * Bit 1 - DSR state (0: off, 1: on)
1842 * Bit 2 - DCD state (0: off, 1: on)
1845 * Function 17: Check the DCD state has changed since the last read
1848 * int MoxaPortDCDChange(int port);
1849 * int port : port number (0 - 127)
1851 * return: 0 : no changed
1852 * 1 : DCD has changed
1855 * Function 18: Check ths current DCD state is ON or not.
1857 * int MoxaPortDCDON(int port);
1858 * int port : port number (0 - 127)
1860 * return: 0 : DCD off
1864 * Function 19: Flush the Rx/Tx buffer data of this port.
1866 * void MoxaPortFlushData(int port, int mode);
1867 * int port : port number (0 - 127)
1869 * 0 : flush the Rx buffer
1870 * 1 : flush the Tx buffer
1871 * 2 : flush the Rx and Tx buffer
1874 * Function 20: Write data.
1876 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1877 * int port : port number (0 - 127)
1878 * unsigned char * buffer : pointer to write data buffer.
1879 * int length : write data length
1881 * return: 0 - length : real write data length
1884 * Function 21: Read data.
1886 * int MoxaPortReadData(int port, struct tty_struct *tty);
1887 * int port : port number (0 - 127)
1888 * struct tty_struct *tty : tty for data
1890 * return: 0 - length : real read data length
1893 * Function 24: Get the Tx buffer current queued data bytes
1895 * int MoxaPortTxQueue(int port);
1896 * int port : port number (0 - 127)
1898 * return: .. : Tx buffer current queued data bytes
1901 * Function 25: Get the Tx buffer current free space
1903 * int MoxaPortTxFree(int port);
1904 * int port : port number (0 - 127)
1906 * return: .. : Tx buffer current free space
1909 * Function 26: Get the Rx buffer current queued data bytes
1911 * int MoxaPortRxQueue(int port);
1912 * int port : port number (0 - 127)
1914 * return: .. : Rx buffer current queued data bytes
1917 * Function 28: Disable port data transmission.
1919 * void MoxaPortTxDisable(int port);
1920 * int port : port number (0 - 127)
1923 * Function 29: Enable port data transmission.
1925 * void MoxaPortTxEnable(int port);
1926 * int port : port number (0 - 127)
1929 * Function 31: Get the received BREAK signal count and reset it.
1931 * int MoxaPortResetBrkCnt(int port);
1932 * int port : port number (0 - 127)
1934 * return: 0 - .. : BREAK signal count
1937 * Function 34: Send out a BREAK signal.
1939 * void MoxaPortSendBreak(int port, int ms100);
1940 * int port : port number (0 - 127)
1941 * int ms100 : break signal time interval.
1942 * unit: 100 mini-second. if ms100 == 0, it will
1943 * send out a about 250 ms BREAK signal.
1946 int MoxaPortIsValid(int port)
1951 if (moxa_ports[port].chkPort == 0)
1956 void MoxaPortEnable(int port)
1958 void __iomem *ofsAddr;
1959 int MoxaPortLineStatus(int);
1960 short lowwater = 512;
1962 ofsAddr = moxa_ports[port].tableAddr;
1963 writew(lowwater, ofsAddr + Low_water);
1964 moxa_ports[port].breakCnt = 0;
1965 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
1966 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
1967 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1969 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
1972 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1973 moxafunc(ofsAddr, FC_FlushQueue, 2);
1975 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1976 MoxaPortLineStatus(port);
1979 void MoxaPortDisable(int port)
1981 void __iomem *ofsAddr = moxa_ports[port].tableAddr;
1983 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1984 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1985 writew(0, ofsAddr + HostStat);
1986 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1989 long MoxaPortGetMaxBaud(int port)
1991 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
1992 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI))
1999 long MoxaPortSetBaud(int port, long baud)
2001 void __iomem *ofsAddr;
2005 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
2007 ofsAddr = moxa_ports[port].tableAddr;
2011 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
2012 else if (max == 57600L)
2013 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
2015 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
2017 moxafunc(ofsAddr, FC_SetBaud, val);
2019 moxa_ports[port].curBaud = baud;
2023 int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud)
2025 void __iomem *ofsAddr;
2029 if (moxa_ports[port].chkPort == 0 || termio == 0)
2031 ofsAddr = moxa_ports[port].tableAddr;
2032 cflag = termio->c_cflag; /* termio->c_cflag */
2034 mode = termio->c_cflag & CSIZE;
2037 else if (mode == CS6)
2039 else if (mode == CS7)
2041 else if (mode == CS8)
2044 if (termio->c_cflag & CSTOPB) {
2052 if (termio->c_cflag & PARENB) {
2053 if (termio->c_cflag & PARODD)
2060 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
2062 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2063 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2064 if (baud >= 921600L)
2067 MoxaPortSetBaud(port, baud);
2069 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
2070 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
2071 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
2072 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
2073 wait_finish(ofsAddr);
2079 int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState)
2082 if (!MoxaPortIsValid(port))
2085 if (moxa_ports[port].lineCtrl & DTR_ON)
2091 if (moxa_ports[port].lineCtrl & RTS_ON)
2099 void MoxaPortLineCtrl(int port, int dtr, int rts)
2101 void __iomem *ofsAddr;
2104 ofsAddr = moxa_ports[port].tableAddr;
2110 moxa_ports[port].lineCtrl = mode;
2111 moxafunc(ofsAddr, FC_LineControl, mode);
2114 void MoxaPortFlowCtrl(int port, int rts, int cts, int txflow, int rxflow, int txany)
2116 void __iomem *ofsAddr;
2119 ofsAddr = moxa_ports[port].tableAddr;
2122 mode |= RTS_FlowCtl;
2124 mode |= CTS_FlowCtl;
2131 moxafunc(ofsAddr, FC_SetFlowCtl, mode);
2134 int MoxaPortLineStatus(int port)
2136 void __iomem *ofsAddr;
2139 ofsAddr = moxa_ports[port].tableAddr;
2140 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2141 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2142 moxafunc(ofsAddr, FC_LineStatus, 0);
2143 val = readw(ofsAddr + FuncArg);
2145 val = readw(ofsAddr + FlagStat) >> 4;
2150 if ((moxa_ports[port].DCDState & DCD_oldstate) == 0)
2151 moxa_ports[port].DCDState = (DCD_oldstate | DCD_changed);
2153 if (moxa_ports[port].DCDState & DCD_oldstate)
2154 moxa_ports[port].DCDState = DCD_changed;
2160 int MoxaPortDCDChange(int port)
2164 if (moxa_ports[port].chkPort == 0)
2166 n = moxa_ports[port].DCDState;
2167 moxa_ports[port].DCDState &= ~DCD_changed;
2172 int MoxaPortDCDON(int port)
2176 if (moxa_ports[port].chkPort == 0)
2178 if (moxa_ports[port].DCDState & DCD_oldstate)
2185 int MoxaPortWriteData(int port, unsigned char * buffer, int len)
2190 ushort head, tx_mask, spage, epage;
2191 ushort pageno, pageofs, bufhead;
2192 void __iomem *baseAddr, *ofsAddr, *ofs;
2194 ofsAddr = moxa_ports[port].tableAddr;
2195 baseAddr = moxa_boards[port / MAX_PORTS_PER_BOARD].basemem;
2196 tx_mask = readw(ofsAddr + TX_mask);
2197 spage = readw(ofsAddr + Page_txb);
2198 epage = readw(ofsAddr + EndPage_txb);
2199 tail = readw(ofsAddr + TXwptr);
2200 head = readw(ofsAddr + TXrptr);
2201 c = (head > tail) ? (head - tail - 1)
2202 : (head - tail + tx_mask);
2205 moxaLog.txcnt[port] += c;
2207 if (spage == epage) {
2208 bufhead = readw(ofsAddr + Ofs_txb);
2209 writew(spage, baseAddr + Control_reg);
2212 len = head - tail - 1;
2214 len = tx_mask + 1 - tail;
2215 len = (c > len) ? len : c;
2216 ofs = baseAddr + DynPage_addr + bufhead + tail;
2217 for (i = 0; i < len; i++)
2218 writeb(*buffer++, ofs + i);
2219 tail = (tail + len) & tx_mask;
2222 writew(tail, ofsAddr + TXwptr);
2225 pageno = spage + (tail >> 13);
2226 pageofs = tail & Page_mask;
2228 cnt = Page_size - pageofs;
2232 writeb(pageno, baseAddr + Control_reg);
2233 ofs = baseAddr + DynPage_addr + pageofs;
2234 for (i = 0; i < cnt; i++)
2235 writeb(*buffer++, ofs + i);
2237 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2240 if (++pageno == epage)
2245 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2249 int MoxaPortReadData(int port, struct tty_struct *tty)
2251 register ushort head, pageofs;
2252 int i, count, cnt, len, total, remain;
2253 ushort tail, rx_mask, spage, epage;
2254 ushort pageno, bufhead;
2255 void __iomem *baseAddr, *ofsAddr, *ofs;
2257 ofsAddr = moxa_ports[port].tableAddr;
2258 baseAddr = moxa_boards[port / MAX_PORTS_PER_BOARD].basemem;
2259 head = readw(ofsAddr + RXrptr);
2260 tail = readw(ofsAddr + RXwptr);
2261 rx_mask = readw(ofsAddr + RX_mask);
2262 spage = readw(ofsAddr + Page_rxb);
2263 epage = readw(ofsAddr + EndPage_rxb);
2264 count = (tail >= head) ? (tail - head)
2265 : (tail - head + rx_mask + 1);
2270 remain = count - total;
2271 moxaLog.rxcnt[port] += total;
2273 if (spage == epage) {
2274 bufhead = readw(ofsAddr + Ofs_rxb);
2275 writew(spage, baseAddr + Control_reg);
2280 len = rx_mask + 1 - head;
2281 len = (count > len) ? len : count;
2282 ofs = baseAddr + DynPage_addr + bufhead + head;
2283 for (i = 0; i < len; i++)
2284 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2285 head = (head + len) & rx_mask;
2288 writew(head, ofsAddr + RXrptr);
2291 pageno = spage + (head >> 13);
2292 pageofs = head & Page_mask;
2294 cnt = Page_size - pageofs;
2298 writew(pageno, baseAddr + Control_reg);
2299 ofs = baseAddr + DynPage_addr + pageofs;
2300 for (i = 0; i < cnt; i++)
2301 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2303 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2306 if (++pageno == epage)
2311 if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2312 moxaLowWaterChk = 1;
2313 moxa_ports[port].lowChkFlag = 1;
2319 int MoxaPortTxQueue(int port)
2321 void __iomem *ofsAddr;
2322 ushort rptr, wptr, mask;
2325 ofsAddr = moxa_ports[port].tableAddr;
2326 rptr = readw(ofsAddr + TXrptr);
2327 wptr = readw(ofsAddr + TXwptr);
2328 mask = readw(ofsAddr + TX_mask);
2329 len = (wptr - rptr) & mask;
2333 int MoxaPortTxFree(int port)
2335 void __iomem *ofsAddr;
2336 ushort rptr, wptr, mask;
2339 ofsAddr = moxa_ports[port].tableAddr;
2340 rptr = readw(ofsAddr + TXrptr);
2341 wptr = readw(ofsAddr + TXwptr);
2342 mask = readw(ofsAddr + TX_mask);
2343 len = mask - ((wptr - rptr) & mask);
2347 int MoxaPortRxQueue(int port)
2349 void __iomem *ofsAddr;
2350 ushort rptr, wptr, mask;
2353 ofsAddr = moxa_ports[port].tableAddr;
2354 rptr = readw(ofsAddr + RXrptr);
2355 wptr = readw(ofsAddr + RXwptr);
2356 mask = readw(ofsAddr + RX_mask);
2357 len = (wptr - rptr) & mask;
2362 void MoxaPortTxDisable(int port)
2364 void __iomem *ofsAddr;
2366 ofsAddr = moxa_ports[port].tableAddr;
2367 moxafunc(ofsAddr, FC_SetXoffState, Magic_code);
2370 void MoxaPortTxEnable(int port)
2372 void __iomem *ofsAddr;
2374 ofsAddr = moxa_ports[port].tableAddr;
2375 moxafunc(ofsAddr, FC_SetXonState, Magic_code);
2379 int MoxaPortResetBrkCnt(int port)
2382 cnt = moxa_ports[port].breakCnt;
2383 moxa_ports[port].breakCnt = 0;
2388 void MoxaPortSendBreak(int port, int ms100)
2390 void __iomem *ofsAddr;
2392 ofsAddr = moxa_ports[port].tableAddr;
2394 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2397 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2400 moxafunc(ofsAddr, FC_StopBreak, Magic_code);
2403 static int moxa_get_serial_info(struct moxa_port *info,
2404 struct serial_struct __user *retinfo)
2406 struct serial_struct tmp;
2408 memset(&tmp, 0, sizeof(tmp));
2409 tmp.type = info->type;
2410 tmp.line = info->port;
2413 tmp.flags = info->asyncflags;
2414 tmp.baud_base = 921600;
2415 tmp.close_delay = info->close_delay;
2416 tmp.closing_wait = info->closing_wait;
2417 tmp.custom_divisor = 0;
2419 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2425 static int moxa_set_serial_info(struct moxa_port *info,
2426 struct serial_struct __user *new_info)
2428 struct serial_struct new_serial;
2430 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2433 if ((new_serial.irq != 0) ||
2434 (new_serial.port != 0) ||
2435 // (new_serial.type != info->type) ||
2436 (new_serial.custom_divisor != 0) ||
2437 (new_serial.baud_base != 921600))
2440 if (!capable(CAP_SYS_ADMIN)) {
2441 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2442 (info->asyncflags & ~ASYNC_USR_MASK)))
2445 info->close_delay = new_serial.close_delay * HZ / 100;
2446 info->closing_wait = new_serial.closing_wait * HZ / 100;
2449 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2450 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2452 if (new_serial.type == PORT_16550A) {
2453 MoxaSetFifo(info->port, 1);
2455 MoxaSetFifo(info->port, 0);
2458 info->type = new_serial.type;
2464 /*****************************************************************************
2465 * Static local functions: *
2466 *****************************************************************************/
2467 static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
2470 writew(arg, ofsAddr + FuncArg);
2471 writew(cmd, ofsAddr + FuncCode);
2472 wait_finish(ofsAddr);
2475 static void wait_finish(void __iomem *ofsAddr)
2480 while (readw(ofsAddr + FuncCode) != 0) {
2482 if ((j - i) > moxaFuncTout) {
2488 static void low_water_check(void __iomem *ofsAddr)
2491 ushort rptr, wptr, mask;
2493 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2494 rptr = readw(ofsAddr + RXrptr);
2495 wptr = readw(ofsAddr + RXwptr);
2496 mask = readw(ofsAddr + RX_mask);
2497 len = (wptr - rptr) & mask;
2498 if (len <= Low_water)
2499 moxafunc(ofsAddr, FC_SendXon, 0);
2503 static int moxaloadbios(int cardno, unsigned char __user *tmp, int len)
2505 void __iomem *baseAddr;
2508 if(len < 0 || len > sizeof(moxaBuff))
2510 if(copy_from_user(moxaBuff, tmp, len))
2512 baseAddr = moxa_boards[cardno].basemem;
2513 writeb(HW_reset, baseAddr + Control_reg); /* reset */
2515 for (i = 0; i < 4096; i++)
2516 writeb(0, baseAddr + i); /* clear fix page */
2517 for (i = 0; i < len; i++)
2518 writeb(moxaBuff[i], baseAddr + i); /* download BIOS */
2519 writeb(0, baseAddr + Control_reg); /* restart */
2523 static int moxafindcard(int cardno)
2525 void __iomem *baseAddr;
2528 baseAddr = moxa_boards[cardno].basemem;
2529 switch (moxa_boards[cardno].boardType) {
2530 case MOXA_BOARD_C218_ISA:
2531 case MOXA_BOARD_C218_PCI:
2532 if ((tmp = readw(baseAddr + C218_key)) != C218_KeyCode) {
2536 case MOXA_BOARD_CP204J:
2537 if ((tmp = readw(baseAddr + C218_key)) != CP204J_KeyCode) {
2542 if ((tmp = readw(baseAddr + C320_key)) != C320_KeyCode) {
2545 if ((tmp = readw(baseAddr + C320_status)) != STS_init) {
2552 static int moxaload320b(int cardno, unsigned char __user *tmp, int len)
2554 void __iomem *baseAddr;
2557 if(len < 0 || len > sizeof(moxaBuff))
2559 if(copy_from_user(moxaBuff, tmp, len))
2561 baseAddr = moxa_boards[cardno].basemem;
2562 writew(len - 7168 - 2, baseAddr + C320bapi_len);
2563 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
2564 for (i = 0; i < 7168; i++)
2565 writeb(moxaBuff[i], baseAddr + DynPage_addr + i);
2566 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
2567 for (i = 0; i < (len - 7168); i++)
2568 writeb(moxaBuff[i + 7168], baseAddr + DynPage_addr + i);
2572 static int moxaloadcode(int cardno, unsigned char __user *tmp, int len)
2574 void __iomem *baseAddr, *ofsAddr;
2575 int retval, port, i;
2577 if(len < 0 || len > sizeof(moxaBuff))
2579 if(copy_from_user(moxaBuff, tmp, len))
2581 baseAddr = moxa_boards[cardno].basemem;
2582 switch (moxa_boards[cardno].boardType) {
2583 case MOXA_BOARD_C218_ISA:
2584 case MOXA_BOARD_C218_PCI:
2585 case MOXA_BOARD_CP204J:
2586 retval = moxaloadc218(cardno, baseAddr, len);
2589 port = cardno * MAX_PORTS_PER_BOARD;
2590 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2591 struct moxa_port *p = &moxa_ports[port];
2596 p->tableAddr = baseAddr + Extern_table + Extern_size * i;
2597 ofsAddr = p->tableAddr;
2598 writew(C218rx_mask, ofsAddr + RX_mask);
2599 writew(C218tx_mask, ofsAddr + TX_mask);
2600 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
2601 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
2603 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
2604 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
2609 retval = moxaloadc320(cardno, baseAddr, len,
2610 &moxa_boards[cardno].numPorts);
2613 port = cardno * MAX_PORTS_PER_BOARD;
2614 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2615 struct moxa_port *p = &moxa_ports[port];
2620 p->tableAddr = baseAddr + Extern_table + Extern_size * i;
2621 ofsAddr = p->tableAddr;
2622 if (moxa_boards[cardno].numPorts == 8) {
2623 writew(C320p8rx_mask, ofsAddr + RX_mask);
2624 writew(C320p8tx_mask, ofsAddr + TX_mask);
2625 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
2626 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
2627 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
2628 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
2630 } else if (moxa_boards[cardno].numPorts == 16) {
2631 writew(C320p16rx_mask, ofsAddr + RX_mask);
2632 writew(C320p16tx_mask, ofsAddr + TX_mask);
2633 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
2634 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
2635 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
2636 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
2638 } else if (moxa_boards[cardno].numPorts == 24) {
2639 writew(C320p24rx_mask, ofsAddr + RX_mask);
2640 writew(C320p24tx_mask, ofsAddr + TX_mask);
2641 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
2642 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
2643 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
2644 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2645 } else if (moxa_boards[cardno].numPorts == 32) {
2646 writew(C320p32rx_mask, ofsAddr + RX_mask);
2647 writew(C320p32tx_mask, ofsAddr + TX_mask);
2648 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
2649 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
2650 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
2651 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
2652 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2657 moxa_boards[cardno].loadstat = 1;
2661 static int moxaloadc218(int cardno, void __iomem *baseAddr, int len)
2664 int i, j, len1, len2;
2665 ushort usum, *ptr, keycode;
2667 if (moxa_boards[cardno].boardType == MOXA_BOARD_CP204J)
2668 keycode = CP204J_KeyCode;
2670 keycode = C218_KeyCode;
2673 ptr = (ushort *) moxaBuff;
2674 for (i = 0; i < len1; i++)
2675 usum += le16_to_cpu(*(ptr + i));
2681 len2 = (len1 > 2048) ? 2048 : len1;
2683 for (i = 0; i < len2 << 1; i++)
2684 writeb(moxaBuff[i + j], baseAddr + C218_LoadBuf + i);
2687 writew(len2, baseAddr + C218DLoad_len);
2688 writew(0, baseAddr + C218_key);
2689 for (i = 0; i < 100; i++) {
2690 if (readw(baseAddr + C218_key) == keycode)
2694 if (readw(baseAddr + C218_key) != keycode) {
2698 writew(0, baseAddr + C218DLoad_len);
2699 writew(usum, baseAddr + C218check_sum);
2700 writew(0, baseAddr + C218_key);
2701 for (i = 0; i < 100; i++) {
2702 if (readw(baseAddr + C218_key) == keycode)
2707 } while ((readb(baseAddr + C218chksum_ok) != 1) && (retry < 3));
2708 if (readb(baseAddr + C218chksum_ok) != 1) {
2711 writew(0, baseAddr + C218_key);
2712 for (i = 0; i < 100; i++) {
2713 if (readw(baseAddr + Magic_no) == Magic_code)
2717 if (readw(baseAddr + Magic_no) != Magic_code) {
2720 writew(1, baseAddr + Disable_IRQ);
2721 writew(0, baseAddr + Magic_no);
2722 for (i = 0; i < 100; i++) {
2723 if (readw(baseAddr + Magic_no) == Magic_code)
2727 if (readw(baseAddr + Magic_no) != Magic_code) {
2731 moxa_boards[cardno].intNdx = baseAddr + IRQindex;
2732 moxa_boards[cardno].intPend = baseAddr + IRQpending;
2733 moxa_boards[cardno].intTable = baseAddr + IRQtable;
2737 static int moxaloadc320(int cardno, void __iomem *baseAddr, int len, int *numPorts)
2740 int i, j, wlen, len2, retry;
2745 uptr = (ushort *) moxaBuff;
2746 for (i = 0; i < wlen; i++)
2747 usum += le16_to_cpu(uptr[i]);
2758 for (i = 0; i < len2; i++)
2759 writeb(moxaBuff[j + i], baseAddr + C320_LoadBuf + i);
2762 writew(len2, baseAddr + C320DLoad_len);
2763 writew(0, baseAddr + C320_key);
2764 for (i = 0; i < 10; i++) {
2765 if (readw(baseAddr + C320_key) == C320_KeyCode)
2769 if (readw(baseAddr + C320_key) != C320_KeyCode)
2772 writew(0, baseAddr + C320DLoad_len);
2773 writew(usum, baseAddr + C320check_sum);
2774 writew(0, baseAddr + C320_key);
2775 for (i = 0; i < 10; i++) {
2776 if (readw(baseAddr + C320_key) == C320_KeyCode)
2781 } while ((readb(baseAddr + C320chksum_ok) != 1) && (retry < 3));
2782 if (readb(baseAddr + C320chksum_ok) != 1)
2784 writew(0, baseAddr + C320_key);
2785 for (i = 0; i < 600; i++) {
2786 if (readw(baseAddr + Magic_no) == Magic_code)
2790 if (readw(baseAddr + Magic_no) != Magic_code)
2793 if (moxa_boards[cardno].busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
2794 writew(0x3800, baseAddr + TMS320_PORT1);
2795 writew(0x3900, baseAddr + TMS320_PORT2);
2796 writew(28499, baseAddr + TMS320_CLOCK);
2798 writew(0x3200, baseAddr + TMS320_PORT1);
2799 writew(0x3400, baseAddr + TMS320_PORT2);
2800 writew(19999, baseAddr + TMS320_CLOCK);
2802 writew(1, baseAddr + Disable_IRQ);
2803 writew(0, baseAddr + Magic_no);
2804 for (i = 0; i < 500; i++) {
2805 if (readw(baseAddr + Magic_no) == Magic_code)
2809 if (readw(baseAddr + Magic_no) != Magic_code)
2812 j = readw(baseAddr + Module_cnt);
2816 writew(j, baseAddr + Module_no);
2817 writew(0, baseAddr + Magic_no);
2818 for (i = 0; i < 600; i++) {
2819 if (readw(baseAddr + Magic_no) == Magic_code)
2823 if (readw(baseAddr + Magic_no) != Magic_code)
2826 moxa_boards[cardno].intNdx = baseAddr + IRQindex;
2827 moxa_boards[cardno].intPend = baseAddr + IRQpending;
2828 moxa_boards[cardno].intTable = baseAddr + IRQtable;
2832 static void MoxaSetFifo(int port, int enable)
2834 void __iomem *ofsAddr = moxa_ports[port].tableAddr;
2837 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2838 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2840 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2841 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);