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>
44 #include <linux/completion.h>
46 #include <asm/system.h>
48 #include <asm/uaccess.h>
50 #define MOXA_VERSION "5.1k"
53 #define MOXACUMAJOR 173
55 #define MAX_BOARDS 4 /* Don't change this value */
56 #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
57 #define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
60 * Define the Moxa PCI vendor and device IDs.
62 #define MOXA_BUS_TYPE_ISA 0
63 #define MOXA_BUS_TYPE_PCI 1
66 MOXA_BOARD_C218_PCI = 1,
73 static char *moxa_brdname[] =
75 "C218 Turbo PCI series",
76 "C218 Turbo ISA series",
77 "C320 Turbo PCI series",
78 "C320 Turbo ISA series",
83 static struct pci_device_id moxa_pcibrds[] = {
84 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
85 .driver_data = MOXA_BOARD_C218_PCI },
86 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
87 .driver_data = MOXA_BOARD_C320_PCI },
88 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
89 .driver_data = MOXA_BOARD_CP204J },
92 MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
93 #endif /* CONFIG_PCI */
95 static struct moxa_board_conf {
102 void __iomem *basemem;
103 void __iomem *intNdx;
104 void __iomem *intPend;
105 void __iomem *intTable;
106 } moxa_boards[MAX_BOARDS];
108 struct mxser_mstatus {
125 unsigned short closing_wait;
128 long event; /* long req'd for set_bit --RR */
130 unsigned long statusflags;
131 struct tty_struct *tty;
133 wait_queue_head_t open_wait;
134 struct completion close_wait;
136 struct timer_list emptyTimer;
140 void __iomem *tableAddr;
149 #define TXSTOPPED 0x1
151 #define EMPTYWAIT 0x4
154 #define SERIAL_DO_RESTART
156 #define WAKEUP_CHARS 256
158 static int ttymajor = MOXAMAJOR;
159 /* Variables for insmod */
161 static unsigned long baseaddr[MAX_BOARDS];
162 static unsigned int type[MAX_BOARDS];
163 static unsigned int numports[MAX_BOARDS];
166 MODULE_AUTHOR("William Chen");
167 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
168 MODULE_LICENSE("GPL");
170 module_param_array(type, uint, NULL, 0);
171 MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
172 module_param_array(baseaddr, ulong, NULL, 0);
173 MODULE_PARM_DESC(baseaddr, "base address");
174 module_param_array(numports, uint, NULL, 0);
175 MODULE_PARM_DESC(numports, "numports (ignored for C218)");
177 module_param(ttymajor, int, 0);
182 static int moxa_open(struct tty_struct *, struct file *);
183 static void moxa_close(struct tty_struct *, struct file *);
184 static int moxa_write(struct tty_struct *, const unsigned char *, int);
185 static int moxa_write_room(struct tty_struct *);
186 static void moxa_flush_buffer(struct tty_struct *);
187 static int moxa_chars_in_buffer(struct tty_struct *);
188 static void moxa_flush_chars(struct tty_struct *);
189 static void moxa_put_char(struct tty_struct *, unsigned char);
190 static int moxa_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);
191 static void moxa_throttle(struct tty_struct *);
192 static void moxa_unthrottle(struct tty_struct *);
193 static void moxa_set_termios(struct tty_struct *, struct ktermios *);
194 static void moxa_stop(struct tty_struct *);
195 static void moxa_start(struct tty_struct *);
196 static void moxa_hangup(struct tty_struct *);
197 static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
198 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
199 unsigned int set, unsigned int clear);
200 static void moxa_poll(unsigned long);
201 static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
202 static int moxa_block_till_ready(struct tty_struct *, struct file *,
204 static void moxa_setup_empty_event(struct tty_struct *);
205 static void moxa_check_xmit_empty(unsigned long);
206 static void moxa_shut_down(struct moxa_port *);
207 static void moxa_receive_data(struct moxa_port *);
209 * moxa board interface functions:
211 static void MoxaDriverInit(void);
212 static int MoxaDriverIoctl(unsigned int, unsigned long, int);
213 static int MoxaDriverPoll(void);
214 static int MoxaPortsOfCard(int);
215 static int MoxaPortIsValid(int);
216 static void MoxaPortEnable(int);
217 static void MoxaPortDisable(int);
218 static long MoxaPortGetMaxBaud(int);
219 static long MoxaPortSetBaud(int, long);
220 static int MoxaPortSetTermio(int, struct ktermios *, speed_t);
221 static int MoxaPortGetLineOut(int, int *, int *);
222 static void MoxaPortLineCtrl(int, int, int);
223 static void MoxaPortFlowCtrl(int, int, int, int, int, int);
224 static int MoxaPortLineStatus(int);
225 static int MoxaPortDCDChange(int);
226 static int MoxaPortDCDON(int);
227 static void MoxaPortFlushData(int, int);
228 static int MoxaPortWriteData(int, unsigned char *, int);
229 static int MoxaPortReadData(int, struct tty_struct *tty);
230 static int MoxaPortTxQueue(int);
231 static int MoxaPortRxQueue(int);
232 static int MoxaPortTxFree(int);
233 static void MoxaPortTxDisable(int);
234 static void MoxaPortTxEnable(int);
235 static int MoxaPortResetBrkCnt(int);
236 static void MoxaPortSendBreak(int, int);
237 static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
238 static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
239 static void MoxaSetFifo(int port, int enable);
241 static const struct tty_operations moxa_ops = {
245 .write_room = moxa_write_room,
246 .flush_buffer = moxa_flush_buffer,
247 .chars_in_buffer = moxa_chars_in_buffer,
248 .flush_chars = moxa_flush_chars,
249 .put_char = moxa_put_char,
251 .throttle = moxa_throttle,
252 .unthrottle = moxa_unthrottle,
253 .set_termios = moxa_set_termios,
256 .hangup = moxa_hangup,
257 .tiocmget = moxa_tiocmget,
258 .tiocmset = moxa_tiocmset,
261 static struct tty_driver *moxaDriver;
262 static struct moxa_port moxa_ports[MAX_PORTS];
263 static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
264 static DEFINE_SPINLOCK(moxa_lock);
267 static int __devinit moxa_pci_probe(struct pci_dev *pdev,
268 const struct pci_device_id *ent)
270 struct moxa_board_conf *board;
272 int board_type = ent->driver_data;
275 retval = pci_enable_device(pdev);
277 dev_err(&pdev->dev, "can't enable pci device\n");
281 for (i = 0; i < MAX_BOARDS; i++)
282 if (moxa_boards[i].basemem == NULL)
286 if (i >= MAX_BOARDS) {
287 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
288 "found. Board is ignored.\n", MAX_BOARDS);
292 board = &moxa_boards[i];
294 retval = pci_request_region(pdev, 2, "moxa-base");
296 dev_err(&pdev->dev, "can't request pci region 2\n");
300 board->basemem = ioremap(pci_resource_start(pdev, 2), 0x4000);
301 if (board->basemem == NULL) {
302 dev_err(&pdev->dev, "can't remap io space 2\n");
306 board->boardType = board_type;
307 switch (board_type) {
308 case MOXA_BOARD_C218_ISA:
309 case MOXA_BOARD_C218_PCI:
313 case MOXA_BOARD_CP204J:
320 board->busType = MOXA_BUS_TYPE_PCI;
322 pci_set_drvdata(pdev, board);
326 pci_release_region(pdev, 2);
331 static void __devexit moxa_pci_remove(struct pci_dev *pdev)
333 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
335 iounmap(brd->basemem);
337 pci_release_region(pdev, 2);
340 static struct pci_driver moxa_pci_driver = {
342 .id_table = moxa_pcibrds,
343 .probe = moxa_pci_probe,
344 .remove = __devexit_p(moxa_pci_remove)
346 #endif /* CONFIG_PCI */
348 static int __init moxa_init(void)
350 struct moxa_port *ch;
351 unsigned int i, isabrds = 0;
354 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
356 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
360 moxaDriver->owner = THIS_MODULE;
361 moxaDriver->name = "ttyMX";
362 moxaDriver->major = ttymajor;
363 moxaDriver->minor_start = 0;
364 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
365 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
366 moxaDriver->init_termios = tty_std_termios;
367 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
368 moxaDriver->init_termios.c_ispeed = 9600;
369 moxaDriver->init_termios.c_ospeed = 9600;
370 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
371 tty_set_operations(moxaDriver, &moxa_ops);
373 for (i = 0, ch = moxa_ports; i < MAX_PORTS; i++, ch++) {
374 ch->type = PORT_16550A;
376 ch->close_delay = 5 * HZ / 10;
377 ch->closing_wait = 30 * HZ;
378 ch->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
379 init_waitqueue_head(&ch->open_wait);
380 init_completion(&ch->close_wait);
382 setup_timer(&ch->emptyTimer, moxa_check_xmit_empty,
386 pr_debug("Moxa tty devices major number = %d\n", ttymajor);
388 if (tty_register_driver(moxaDriver)) {
389 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
390 put_tty_driver(moxaDriver);
394 mod_timer(&moxaTimer, jiffies + HZ / 50);
396 /* Find the boards defined from module args. */
399 struct moxa_board_conf *brd = moxa_boards;
400 for (i = 0; i < MAX_BOARDS; i++) {
403 if (type[i] == MOXA_BOARD_C218_ISA ||
404 type[i] == MOXA_BOARD_C320_ISA) {
405 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
406 isabrds + 1, moxa_brdname[type[i] - 1],
408 brd->boardType = type[i];
409 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
411 brd->busType = MOXA_BUS_TYPE_ISA;
412 brd->basemem = ioremap(baseaddr[i], 0x4000);
414 printk(KERN_ERR "moxa: can't remap %lx\n",
427 retval = pci_register_driver(&moxa_pci_driver);
429 printk(KERN_ERR "Can't register moxa pci driver!\n");
438 static void __exit moxa_exit(void)
442 del_timer_sync(&moxaTimer);
444 for (i = 0; i < MAX_PORTS; i++)
445 del_timer_sync(&moxa_ports[i].emptyTimer);
447 if (tty_unregister_driver(moxaDriver))
448 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
450 put_tty_driver(moxaDriver);
453 pci_unregister_driver(&moxa_pci_driver);
456 for (i = 0; i < MAX_BOARDS; i++)
457 if (moxa_boards[i].basemem)
458 iounmap(moxa_boards[i].basemem);
461 module_init(moxa_init);
462 module_exit(moxa_exit);
464 static int moxa_open(struct tty_struct *tty, struct file *filp)
466 struct moxa_port *ch;
471 if (port == MAX_PORTS) {
474 if (!MoxaPortIsValid(port)) {
475 tty->driver_data = NULL;
479 ch = &moxa_ports[port];
481 tty->driver_data = ch;
483 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
485 moxa_set_tty_param(tty, tty->termios);
486 MoxaPortLineCtrl(ch->port, 1, 1);
487 MoxaPortEnable(ch->port);
488 ch->asyncflags |= ASYNC_INITIALIZED;
490 retval = moxa_block_till_ready(tty, filp, ch);
492 moxa_unthrottle(tty);
494 if (ch->type == PORT_16550A) {
495 MoxaSetFifo(ch->port, 1);
497 MoxaSetFifo(ch->port, 0);
503 static void moxa_close(struct tty_struct *tty, struct file *filp)
505 struct moxa_port *ch;
509 if (port == MAX_PORTS) {
512 if (!MoxaPortIsValid(port)) {
513 pr_debug("Invalid portno in moxa_close\n");
514 tty->driver_data = NULL;
517 if (tty->driver_data == NULL) {
520 if (tty_hung_up_p(filp)) {
523 ch = (struct moxa_port *) tty->driver_data;
525 if ((tty->count == 1) && (ch->count != 1)) {
526 printk(KERN_WARNING "moxa_close: bad serial port count; "
527 "tty->count is 1, ch->count is %d\n", ch->count);
530 if (--ch->count < 0) {
531 printk(KERN_WARNING "moxa_close: bad serial port count, "
532 "device=%s\n", tty->name);
538 ch->asyncflags |= ASYNC_CLOSING;
540 ch->cflag = tty->termios->c_cflag;
541 if (ch->asyncflags & ASYNC_INITIALIZED) {
542 moxa_setup_empty_event(tty);
543 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
544 del_timer_sync(&moxa_ports[ch->port].emptyTimer);
547 MoxaPortFlushData(port, 2);
549 if (tty->driver->flush_buffer)
550 tty->driver->flush_buffer(tty);
551 tty_ldisc_flush(tty);
556 if (ch->blocked_open) {
557 if (ch->close_delay) {
558 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
560 wake_up_interruptible(&ch->open_wait);
562 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
563 complete_all(&ch->close_wait);
566 static int moxa_write(struct tty_struct *tty,
567 const unsigned char *buf, int count)
569 struct moxa_port *ch;
573 ch = (struct moxa_port *) tty->driver_data;
578 spin_lock_irqsave(&moxa_lock, flags);
579 len = MoxaPortWriteData(port, (unsigned char *) buf, count);
580 spin_unlock_irqrestore(&moxa_lock, flags);
582 /*********************************************
583 if ( !(ch->statusflags & LOWWAIT) &&
584 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
585 ************************************************/
586 ch->statusflags |= LOWWAIT;
590 static int moxa_write_room(struct tty_struct *tty)
592 struct moxa_port *ch;
596 ch = (struct moxa_port *) tty->driver_data;
599 return (MoxaPortTxFree(ch->port));
602 static void moxa_flush_buffer(struct tty_struct *tty)
604 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
608 MoxaPortFlushData(ch->port, 1);
612 static int moxa_chars_in_buffer(struct tty_struct *tty)
615 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
618 * Sigh...I have to check if driver_data is NULL here, because
619 * if an open() fails, the TTY subsystem eventually calls
620 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
621 * routine. And since the open() failed, we return 0 here. TDJ
625 chars = MoxaPortTxQueue(ch->port);
628 * Make it possible to wakeup anything waiting for output
629 * in tty_ioctl.c, etc.
631 if (!(ch->statusflags & EMPTYWAIT))
632 moxa_setup_empty_event(tty);
637 static void moxa_flush_chars(struct tty_struct *tty)
640 * Don't think I need this, because this is called to empty the TX
641 * buffer for the 16450, 16550, etc.
645 static void moxa_put_char(struct tty_struct *tty, unsigned char c)
647 struct moxa_port *ch;
651 ch = (struct moxa_port *) tty->driver_data;
655 spin_lock_irqsave(&moxa_lock, flags);
656 MoxaPortWriteData(port, &c, 1);
657 spin_unlock_irqrestore(&moxa_lock, flags);
658 /************************************************
659 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
660 *************************************************/
661 ch->statusflags |= LOWWAIT;
664 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
666 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
668 int flag = 0, dtr, rts;
671 if ((port != MAX_PORTS) && (!ch))
674 MoxaPortGetLineOut(ch->port, &dtr, &rts);
679 dtr = MoxaPortLineStatus(ch->port);
689 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
690 unsigned int set, unsigned int clear)
692 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
697 if ((port != MAX_PORTS) && (!ch))
700 MoxaPortGetLineOut(ch->port, &dtr, &rts);
705 if (clear & TIOCM_RTS)
707 if (clear & TIOCM_DTR)
709 MoxaPortLineCtrl(ch->port, dtr, rts);
713 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
714 unsigned int cmd, unsigned long arg)
716 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
718 void __user *argp = (void __user *)arg;
722 if ((port != MAX_PORTS) && (!ch))
726 case TCSBRK: /* SVID version: non-zero arg --> no break */
727 retval = tty_check_change(tty);
730 moxa_setup_empty_event(tty);
731 tty_wait_until_sent(tty, 0);
733 MoxaPortSendBreak(ch->port, 0);
735 case TCSBRKP: /* support for POSIX tcsendbreak() */
736 retval = tty_check_change(tty);
739 moxa_setup_empty_event(tty);
740 tty_wait_until_sent(tty, 0);
741 MoxaPortSendBreak(ch->port, arg);
744 return put_user(C_CLOCAL(tty) ? 1 : 0, (int __user *)argp);
746 if (get_user(retval, (int __user *)argp))
749 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
752 ch->asyncflags &= ~ASYNC_CHECK_CD;
754 ch->asyncflags |= ASYNC_CHECK_CD;
757 return moxa_get_serial_info(ch, argp);
760 return moxa_set_serial_info(ch, argp);
762 retval = MoxaDriverIoctl(cmd, arg, port);
767 static void moxa_throttle(struct tty_struct *tty)
769 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
771 ch->statusflags |= THROTTLE;
774 static void moxa_unthrottle(struct tty_struct *tty)
776 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
778 ch->statusflags &= ~THROTTLE;
781 static void moxa_set_termios(struct tty_struct *tty,
782 struct ktermios *old_termios)
784 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
788 moxa_set_tty_param(tty, old_termios);
789 if (!(old_termios->c_cflag & CLOCAL) &&
790 (tty->termios->c_cflag & CLOCAL))
791 wake_up_interruptible(&ch->open_wait);
794 static void moxa_stop(struct tty_struct *tty)
796 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
800 MoxaPortTxDisable(ch->port);
801 ch->statusflags |= TXSTOPPED;
805 static void moxa_start(struct tty_struct *tty)
807 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
812 if (!(ch->statusflags & TXSTOPPED))
815 MoxaPortTxEnable(ch->port);
816 ch->statusflags &= ~TXSTOPPED;
819 static void moxa_hangup(struct tty_struct *tty)
821 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
823 moxa_flush_buffer(tty);
827 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
829 wake_up_interruptible(&ch->open_wait);
832 static void moxa_poll(unsigned long ignored)
835 struct moxa_port *ch;
836 struct tty_struct *tp;
839 del_timer(&moxaTimer);
841 if (MoxaDriverPoll() < 0) {
842 mod_timer(&moxaTimer, jiffies + HZ / 50);
845 for (card = 0; card < MAX_BOARDS; card++) {
846 if ((ports = MoxaPortsOfCard(card)) <= 0)
848 ch = &moxa_ports[card * MAX_PORTS_PER_BOARD];
849 for (i = 0; i < ports; i++, ch++) {
850 if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
852 if (!(ch->statusflags & THROTTLE) &&
853 (MoxaPortRxQueue(ch->port) > 0))
854 moxa_receive_data(ch);
855 if ((tp = ch->tty) == 0)
857 if (ch->statusflags & LOWWAIT) {
858 if (MoxaPortTxQueue(ch->port) <= WAKEUP_CHARS) {
860 ch->statusflags &= ~LOWWAIT;
865 if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch->port) > 0)) {
866 tty_insert_flip_char(tp, 0, TTY_BREAK);
867 tty_schedule_flip(tp);
869 if (MoxaPortDCDChange(ch->port)) {
870 if (ch->asyncflags & ASYNC_CHECK_CD) {
871 if (MoxaPortDCDON(ch->port))
872 wake_up_interruptible(&ch->open_wait);
875 wake_up_interruptible(&ch->open_wait);
876 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
883 mod_timer(&moxaTimer, jiffies + HZ / 50);
886 /******************************************************************************/
888 static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
890 register struct ktermios *ts;
891 struct moxa_port *ch;
892 int rts, cts, txflow, rxflow, xany, baud;
894 ch = (struct moxa_port *) tty->driver_data;
896 if (ts->c_cflag & CLOCAL)
897 ch->asyncflags &= ~ASYNC_CHECK_CD;
899 ch->asyncflags |= ASYNC_CHECK_CD;
900 rts = cts = txflow = rxflow = xany = 0;
901 if (ts->c_cflag & CRTSCTS)
903 if (ts->c_iflag & IXON)
905 if (ts->c_iflag & IXOFF)
907 if (ts->c_iflag & IXANY)
910 /* Clear the features we don't support */
911 ts->c_cflag &= ~CMSPAR;
912 MoxaPortFlowCtrl(ch->port, rts, cts, txflow, rxflow, xany);
913 baud = MoxaPortSetTermio(ch->port, ts, tty_get_baud_rate(tty));
915 baud = tty_termios_baud_rate(old_termios);
916 /* Not put the baud rate into the termios data */
917 tty_encode_baud_rate(tty, baud, baud);
920 static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
921 struct moxa_port *ch)
923 DECLARE_WAITQUEUE(wait,current);
926 int do_clocal = C_CLOCAL(tty);
929 * If the device is in the middle of being closed, then block
930 * until it's done, and then try again.
932 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
933 if (ch->asyncflags & ASYNC_CLOSING)
934 wait_for_completion_interruptible(&ch->close_wait);
935 #ifdef SERIAL_DO_RESTART
936 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
939 return (-ERESTARTSYS);
945 * If non-blocking mode is set, then make the check up front
948 if (filp->f_flags & O_NONBLOCK) {
949 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
953 * Block waiting for the carrier detect and the line to become free
956 add_wait_queue(&ch->open_wait, &wait);
957 pr_debug("block_til_ready before block: ttys%d, count = %d\n",
958 ch->port, ch->count);
959 spin_lock_irqsave(&moxa_lock, flags);
960 if (!tty_hung_up_p(filp))
963 spin_unlock_irqrestore(&moxa_lock, flags);
966 set_current_state(TASK_INTERRUPTIBLE);
967 if (tty_hung_up_p(filp) ||
968 !(ch->asyncflags & ASYNC_INITIALIZED)) {
969 #ifdef SERIAL_DO_RESTART
970 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
973 retval = -ERESTARTSYS;
979 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
980 MoxaPortDCDON(ch->port)))
983 if (signal_pending(current)) {
984 retval = -ERESTARTSYS;
989 set_current_state(TASK_RUNNING);
990 remove_wait_queue(&ch->open_wait, &wait);
992 spin_lock_irqsave(&moxa_lock, flags);
993 if (!tty_hung_up_p(filp))
996 spin_unlock_irqrestore(&moxa_lock, flags);
997 pr_debug("block_til_ready after blocking: ttys%d, count = %d\n",
998 ch->port, ch->count);
1001 /* FIXME: review to see if we need to use set_bit on these */
1002 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1006 static void moxa_setup_empty_event(struct tty_struct *tty)
1008 struct moxa_port *ch = tty->driver_data;
1009 unsigned long flags;
1011 spin_lock_irqsave(&moxa_lock, flags);
1012 ch->statusflags |= EMPTYWAIT;
1013 mod_timer(&moxa_ports[ch->port].emptyTimer, jiffies + HZ);
1014 spin_unlock_irqrestore(&moxa_lock, flags);
1017 static void moxa_check_xmit_empty(unsigned long data)
1019 struct moxa_port *ch;
1021 ch = (struct moxa_port *) data;
1022 if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
1023 if (MoxaPortTxQueue(ch->port) == 0) {
1024 ch->statusflags &= ~EMPTYWAIT;
1025 tty_wakeup(ch->tty);
1028 mod_timer(&moxa_ports[ch->port].emptyTimer,
1029 round_jiffies(jiffies + HZ));
1031 ch->statusflags &= ~EMPTYWAIT;
1034 static void moxa_shut_down(struct moxa_port *ch)
1036 struct tty_struct *tp;
1038 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1043 MoxaPortDisable(ch->port);
1046 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1048 if (tp->termios->c_cflag & HUPCL)
1049 MoxaPortLineCtrl(ch->port, 0, 0);
1051 ch->asyncflags &= ~ASYNC_INITIALIZED;
1054 static void moxa_receive_data(struct moxa_port *ch)
1056 struct tty_struct *tp;
1057 struct ktermios *ts;
1058 unsigned long flags;
1064 /**************************************************
1065 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1066 *****************************************************/
1068 MoxaPortFlushData(ch->port, 0);
1071 spin_lock_irqsave(&moxa_lock, flags);
1072 MoxaPortReadData(ch->port, tp);
1073 spin_unlock_irqrestore(&moxa_lock, flags);
1074 tty_schedule_flip(tp);
1077 #define Magic_code 0x404
1080 * System Configuration
1083 * for C218 BIOS initialization
1085 #define C218_ConfBase 0x800
1086 #define C218_status (C218_ConfBase + 0) /* BIOS running status */
1087 #define C218_diag (C218_ConfBase + 2) /* diagnostic status */
1088 #define C218_key (C218_ConfBase + 4) /* WORD (0x218 for C218) */
1089 #define C218DLoad_len (C218_ConfBase + 6) /* WORD */
1090 #define C218check_sum (C218_ConfBase + 8) /* BYTE */
1091 #define C218chksum_ok (C218_ConfBase + 0x0a) /* BYTE (1:ok) */
1092 #define C218_TestRx (C218_ConfBase + 0x10) /* 8 bytes for 8 ports */
1093 #define C218_TestTx (C218_ConfBase + 0x18) /* 8 bytes for 8 ports */
1094 #define C218_RXerr (C218_ConfBase + 0x20) /* 8 bytes for 8 ports */
1095 #define C218_ErrFlag (C218_ConfBase + 0x28) /* 8 bytes for 8 ports */
1097 #define C218_LoadBuf 0x0F00
1098 #define C218_KeyCode 0x218
1099 #define CP204J_KeyCode 0x204
1102 * for C320 BIOS initialization
1104 #define C320_ConfBase 0x800
1105 #define C320_LoadBuf 0x0f00
1106 #define STS_init 0x05 /* for C320_status */
1108 #define C320_status C320_ConfBase + 0 /* BIOS running status */
1109 #define C320_diag C320_ConfBase + 2 /* diagnostic status */
1110 #define C320_key C320_ConfBase + 4 /* WORD (0320H for C320) */
1111 #define C320DLoad_len C320_ConfBase + 6 /* WORD */
1112 #define C320check_sum C320_ConfBase + 8 /* WORD */
1113 #define C320chksum_ok C320_ConfBase + 0x0a /* WORD (1:ok) */
1114 #define C320bapi_len C320_ConfBase + 0x0c /* WORD */
1115 #define C320UART_no C320_ConfBase + 0x0e /* WORD */
1117 #define C320_KeyCode 0x320
1119 #define FixPage_addr 0x0000 /* starting addr of static page */
1120 #define DynPage_addr 0x2000 /* starting addr of dynamic page */
1121 #define C218_start 0x3000 /* starting addr of C218 BIOS prg */
1122 #define Control_reg 0x1ff0 /* select page and reset control */
1123 #define HW_reset 0x80
1128 #define FC_CardReset 0x80
1129 #define FC_ChannelReset 1 /* C320 firmware not supported */
1130 #define FC_EnableCH 2
1131 #define FC_DisableCH 3
1132 #define FC_SetParam 4
1133 #define FC_SetMode 5
1134 #define FC_SetRate 6
1135 #define FC_LineControl 7
1136 #define FC_LineStatus 8
1137 #define FC_XmitControl 9
1138 #define FC_FlushQueue 10
1139 #define FC_SendBreak 11
1140 #define FC_StopBreak 12
1141 #define FC_LoopbackON 13
1142 #define FC_LoopbackOFF 14
1143 #define FC_ClrIrqTable 15
1144 #define FC_SendXon 16
1145 #define FC_SetTermIrq 17 /* C320 firmware not supported */
1146 #define FC_SetCntIrq 18 /* C320 firmware not supported */
1147 #define FC_SetBreakIrq 19
1148 #define FC_SetLineIrq 20
1149 #define FC_SetFlowCtl 21
1150 #define FC_GenIrq 22
1151 #define FC_InCD180 23
1152 #define FC_OutCD180 24
1153 #define FC_InUARTreg 23
1154 #define FC_OutUARTreg 24
1155 #define FC_SetXonXoff 25
1156 #define FC_OutCD180CCR 26
1157 #define FC_ExtIQueue 27
1158 #define FC_ExtOQueue 28
1159 #define FC_ClrLineIrq 29
1160 #define FC_HWFlowCtl 30
1161 #define FC_GetClockRate 35
1162 #define FC_SetBaud 36
1163 #define FC_SetDataMode 41
1164 #define FC_GetCCSR 43
1165 #define FC_GetDataError 45
1166 #define FC_RxControl 50
1167 #define FC_ImmSend 51
1168 #define FC_SetXonState 52
1169 #define FC_SetXoffState 53
1170 #define FC_SetRxFIFOTrig 54
1171 #define FC_SetTxFIFOCnt 55
1172 #define FC_UnixRate 56
1173 #define FC_UnixResetTimer 57
1175 #define RxFIFOTrig1 0
1176 #define RxFIFOTrig4 1
1177 #define RxFIFOTrig8 2
1178 #define RxFIFOTrig14 3
1183 #define DRAM_global 0
1184 #define INT_data (DRAM_global + 0)
1185 #define Config_base (DRAM_global + 0x108)
1187 #define IRQindex (INT_data + 0)
1188 #define IRQpending (INT_data + 4)
1189 #define IRQtable (INT_data + 8)
1194 #define IntrRx 0x01 /* receiver data O.K. */
1195 #define IntrTx 0x02 /* transmit buffer empty */
1196 #define IntrFunc 0x04 /* function complete */
1197 #define IntrBreak 0x08 /* received break */
1198 #define IntrLine 0x10 /* line status change
1200 #define IntrIntr 0x20 /* received INTR code */
1201 #define IntrQuit 0x40 /* received QUIT code */
1202 #define IntrEOF 0x80 /* received EOF code */
1204 #define IntrRxTrigger 0x100 /* rx data count reach tigger value */
1205 #define IntrTxTrigger 0x200 /* tx data count below trigger value */
1207 #define Magic_no (Config_base + 0)
1208 #define Card_model_no (Config_base + 2)
1209 #define Total_ports (Config_base + 4)
1210 #define Module_cnt (Config_base + 8)
1211 #define Module_no (Config_base + 10)
1212 #define Timer_10ms (Config_base + 14)
1213 #define Disable_IRQ (Config_base + 20)
1214 #define TMS320_PORT1 (Config_base + 22)
1215 #define TMS320_PORT2 (Config_base + 24)
1216 #define TMS320_CLOCK (Config_base + 26)
1219 * DATA BUFFER in DRAM
1221 #define Extern_table 0x400 /* Base address of the external table
1222 (24 words * 64) total 3K bytes
1223 (24 words * 128) total 6K bytes */
1224 #define Extern_size 0x60 /* 96 bytes */
1225 #define RXrptr 0x00 /* read pointer for RX buffer */
1226 #define RXwptr 0x02 /* write pointer for RX buffer */
1227 #define TXrptr 0x04 /* read pointer for TX buffer */
1228 #define TXwptr 0x06 /* write pointer for TX buffer */
1229 #define HostStat 0x08 /* IRQ flag and general flag */
1230 #define FlagStat 0x0A
1231 #define FlowControl 0x0C /* B7 B6 B5 B4 B3 B2 B1 B0 */
1232 /* x x x x | | | | */
1233 /* | | | + CTS flow */
1234 /* | | +--- RTS flow */
1235 /* | +------ TX Xon/Xoff */
1236 /* +--------- RX Xon/Xoff */
1237 #define Break_cnt 0x0E /* received break count */
1238 #define CD180TXirq 0x10 /* if non-0: enable TX irq */
1239 #define RX_mask 0x12
1240 #define TX_mask 0x14
1241 #define Ofs_rxb 0x16
1242 #define Ofs_txb 0x18
1243 #define Page_rxb 0x1A
1244 #define Page_txb 0x1C
1245 #define EndPage_rxb 0x1E
1246 #define EndPage_txb 0x20
1247 #define Data_error 0x22
1248 #define RxTrigger 0x28
1249 #define TxTrigger 0x2a
1251 #define rRXwptr 0x34
1252 #define Low_water 0x36
1254 #define FuncCode 0x40
1255 #define FuncArg 0x42
1256 #define FuncArg1 0x44
1258 #define C218rx_size 0x2000 /* 8K bytes */
1259 #define C218tx_size 0x8000 /* 32K bytes */
1261 #define C218rx_mask (C218rx_size - 1)
1262 #define C218tx_mask (C218tx_size - 1)
1264 #define C320p8rx_size 0x2000
1265 #define C320p8tx_size 0x8000
1266 #define C320p8rx_mask (C320p8rx_size - 1)
1267 #define C320p8tx_mask (C320p8tx_size - 1)
1269 #define C320p16rx_size 0x2000
1270 #define C320p16tx_size 0x4000
1271 #define C320p16rx_mask (C320p16rx_size - 1)
1272 #define C320p16tx_mask (C320p16tx_size - 1)
1274 #define C320p24rx_size 0x2000
1275 #define C320p24tx_size 0x2000
1276 #define C320p24rx_mask (C320p24rx_size - 1)
1277 #define C320p24tx_mask (C320p24tx_size - 1)
1279 #define C320p32rx_size 0x1000
1280 #define C320p32tx_size 0x1000
1281 #define C320p32rx_mask (C320p32rx_size - 1)
1282 #define C320p32tx_mask (C320p32tx_size - 1)
1284 #define Page_size 0x2000
1285 #define Page_mask (Page_size - 1)
1286 #define C218rx_spage 3
1287 #define C218tx_spage 4
1288 #define C218rx_pageno 1
1289 #define C218tx_pageno 4
1290 #define C218buf_pageno 5
1292 #define C320p8rx_spage 3
1293 #define C320p8tx_spage 4
1294 #define C320p8rx_pgno 1
1295 #define C320p8tx_pgno 4
1296 #define C320p8buf_pgno 5
1298 #define C320p16rx_spage 3
1299 #define C320p16tx_spage 4
1300 #define C320p16rx_pgno 1
1301 #define C320p16tx_pgno 2
1302 #define C320p16buf_pgno 3
1304 #define C320p24rx_spage 3
1305 #define C320p24tx_spage 4
1306 #define C320p24rx_pgno 1
1307 #define C320p24tx_pgno 1
1308 #define C320p24buf_pgno 2
1310 #define C320p32rx_spage 3
1311 #define C320p32tx_ofs C320p32rx_size
1312 #define C320p32tx_spage 3
1313 #define C320p32buf_pgno 1
1318 #define WakeupRx 0x01
1319 #define WakeupTx 0x02
1320 #define WakeupBreak 0x08
1321 #define WakeupLine 0x10
1322 #define WakeupIntr 0x20
1323 #define WakeupQuit 0x40
1324 #define WakeupEOF 0x80 /* used in VTIME control */
1325 #define WakeupRxTrigger 0x100
1326 #define WakeupTxTrigger 0x200
1330 #define Rx_over 0x01
1331 #define Xoff_state 0x02
1332 #define Tx_flowOff 0x04
1333 #define Tx_enable 0x08
1334 #define CTS_state 0x10
1335 #define DSR_state 0x20
1336 #define DCD_state 0x80
1340 #define CTS_FlowCtl 1
1341 #define RTS_FlowCtl 2
1342 #define Tx_FlowCtl 4
1343 #define Rx_FlowCtl 8
1344 #define IXM_IXANY 0x10
1346 #define LowWater 128
1354 /* mode definition */
1360 #define MX_STOP1 0x00
1361 #define MX_STOP15 0x04
1362 #define MX_STOP2 0x08
1364 #define MX_PARNONE 0x00
1365 #define MX_PAREVEN 0x40
1366 #define MX_PARODD 0xC0
1374 int rxcnt[MAX_PORTS];
1375 int txcnt[MAX_PORTS];
1378 #define DCD_changed 0x01
1379 #define DCD_oldstate 0x80
1381 static unsigned char moxaBuff[10240];
1382 static int moxaLowWaterChk;
1383 static int moxaCard;
1384 static struct mon_str moxaLog;
1385 static int moxaFuncTout = HZ / 2;
1387 static void moxafunc(void __iomem *, int, ushort);
1388 static void moxa_wait_finish(void __iomem *);
1389 static void moxa_low_water_check(void __iomem *);
1390 static int moxaloadbios(int, unsigned char __user *, int);
1391 static int moxafindcard(int);
1392 static int moxaload320b(int, unsigned char __user *, int);
1393 static int moxaloadcode(int, unsigned char __user *, int);
1394 static int moxaloadc218(int, void __iomem *, int);
1395 static int moxaloadc320(int, void __iomem *, int, int *);
1397 /*****************************************************************************
1398 * Driver level functions: *
1399 * 1. MoxaDriverInit(void); *
1400 * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
1401 * 3. MoxaDriverPoll(void); *
1402 *****************************************************************************/
1403 void MoxaDriverInit(void)
1405 struct moxa_port *p;
1408 moxaFuncTout = HZ / 2; /* 500 mini-seconds */
1411 moxaLowWaterChk = 0;
1412 for (i = 0; i < MAX_PORTS; i++) {
1417 moxaLog.rxcnt[i] = 0;
1418 moxaLog.txcnt[i] = 0;
1423 #define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1424 #define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
1425 #define MOXA_INIT_DRIVER (MOXA + 6) /* moxaCard=0 */
1426 #define MOXA_LOAD_BIOS (MOXA + 9) /* download BIOS */
1427 #define MOXA_FIND_BOARD (MOXA + 10) /* Check if MOXA card exist? */
1428 #define MOXA_LOAD_C320B (MOXA + 11) /* download 320B firmware */
1429 #define MOXA_LOAD_CODE (MOXA + 12) /* download firmware */
1430 #define MOXA_GETDATACOUNT (MOXA + 23)
1431 #define MOXA_GET_IOQUEUE (MOXA + 27)
1432 #define MOXA_FLUSH_QUEUE (MOXA + 28)
1433 #define MOXA_GET_CONF (MOXA + 35) /* configuration */
1434 #define MOXA_GET_MAJOR (MOXA + 63)
1435 #define MOXA_GET_CUMAJOR (MOXA + 64)
1436 #define MOXA_GETMSTATUS (MOXA + 65)
1444 static struct dl_str dltmp;
1446 void MoxaPortFlushData(int port, int mode)
1448 void __iomem *ofsAddr;
1449 if ((mode < 0) || (mode > 2))
1451 ofsAddr = moxa_ports[port].tableAddr;
1452 moxafunc(ofsAddr, FC_FlushQueue, mode);
1454 moxa_ports[port].lowChkFlag = 0;
1455 moxa_low_water_check(ofsAddr);
1459 int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port)
1463 int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
1464 void __user *argp = (void __user *)arg;
1466 if (port == MAX_PORTS) {
1467 if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_INIT_DRIVER) &&
1468 (cmd != MOXA_LOAD_BIOS) && (cmd != MOXA_FIND_BOARD) && (cmd != MOXA_LOAD_C320B) &&
1469 (cmd != MOXA_LOAD_CODE) && (cmd != MOXA_GETDATACOUNT) &&
1470 (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
1471 (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
1476 if(copy_to_user(argp, &moxa_boards, MAX_BOARDS *
1477 sizeof(struct moxa_board_conf)))
1480 case MOXA_INIT_DRIVER:
1481 if ((int) arg == 0x404)
1484 case MOXA_GETDATACOUNT:
1485 moxaLog.tick = jiffies;
1486 if(copy_to_user(argp, &moxaLog, sizeof(struct mon_str)))
1489 case MOXA_FLUSH_QUEUE:
1490 MoxaPortFlushData(port, arg);
1492 case MOXA_GET_IOQUEUE: {
1493 struct moxaq_str __user *argm = argp;
1494 struct moxaq_str tmp;
1496 for (i = 0; i < MAX_PORTS; i++, argm++) {
1497 memset(&tmp, 0, sizeof(tmp));
1498 if (moxa_ports[i].chkPort) {
1499 tmp.inq = MoxaPortRxQueue(i);
1500 tmp.outq = MoxaPortTxQueue(i);
1502 if (copy_to_user(argm, &tmp, sizeof(tmp)))
1506 } case MOXA_GET_OQUEUE:
1507 i = MoxaPortTxQueue(port);
1508 return put_user(i, (unsigned long __user *)argp);
1509 case MOXA_GET_IQUEUE:
1510 i = MoxaPortRxQueue(port);
1511 return put_user(i, (unsigned long __user *)argp);
1512 case MOXA_GET_MAJOR:
1513 if(copy_to_user(argp, &ttymajor, sizeof(int)))
1516 case MOXA_GET_CUMAJOR:
1518 if(copy_to_user(argp, &i, sizeof(int)))
1521 case MOXA_GETMSTATUS: {
1522 struct mxser_mstatus __user *argm = argp;
1523 struct mxser_mstatus tmp;
1524 struct moxa_port *p;
1526 for (i = 0; i < MAX_PORTS; i++, argm++) {
1528 memset(&tmp, 0, sizeof(tmp));
1532 status = MoxaPortLineStatus(p->port);
1541 if (!p->tty || !p->tty->termios)
1542 tmp.cflag = p->cflag;
1544 tmp.cflag = p->tty->termios->c_cflag;
1546 if (copy_to_user(argm, &tmp, sizeof(tmp)))
1551 return (-ENOIOCTLCMD);
1552 case MOXA_LOAD_BIOS:
1553 case MOXA_FIND_BOARD:
1554 case MOXA_LOAD_C320B:
1555 case MOXA_LOAD_CODE:
1556 if (!capable(CAP_SYS_RAWIO))
1561 if(copy_from_user(&dltmp, argp, sizeof(struct dl_str)))
1563 if(dltmp.cardno < 0 || dltmp.cardno >= MAX_BOARDS || dltmp.len < 0)
1568 case MOXA_LOAD_BIOS:
1569 i = moxaloadbios(dltmp.cardno, dltmp.buf, dltmp.len);
1571 case MOXA_FIND_BOARD:
1572 return moxafindcard(dltmp.cardno);
1573 case MOXA_LOAD_C320B:
1574 moxaload320b(dltmp.cardno, dltmp.buf, dltmp.len);
1575 default: /* to keep gcc happy */
1577 case MOXA_LOAD_CODE:
1578 i = moxaloadcode(dltmp.cardno, dltmp.buf, dltmp.len);
1586 int MoxaDriverPoll(void)
1588 struct moxa_board_conf *brd;
1589 register ushort temp;
1591 void __iomem *ofsAddr;
1597 for (card = 0; card < MAX_BOARDS; card++) {
1598 brd = &moxa_boards[card];
1599 if (brd->loadstat == 0)
1601 if ((ports = brd->numPorts) == 0)
1603 if (readb(brd->intPend) == 0xff) {
1604 ip = brd->intTable + readb(brd->intNdx);
1605 p = card * MAX_PORTS_PER_BOARD;
1607 for (port = 0; port < ports; port += 2, p++) {
1608 if ((temp = readw(ip + port)) != 0) {
1609 writew(0, ip + port);
1610 ofsAddr = moxa_ports[p].tableAddr;
1612 writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
1613 if (temp & IntrBreak) {
1614 moxa_ports[p].breakCnt++;
1616 if (temp & IntrLine) {
1617 if (readb(ofsAddr + FlagStat) & DCD_state) {
1618 if ((moxa_ports[p].DCDState & DCD_oldstate) == 0)
1619 moxa_ports[p].DCDState = (DCD_oldstate |
1622 if (moxa_ports[p].DCDState & DCD_oldstate)
1623 moxa_ports[p].DCDState = DCD_changed;
1628 writeb(0, brd->intPend);
1630 if (moxaLowWaterChk) {
1631 p = card * MAX_PORTS_PER_BOARD;
1632 for (port = 0; port < ports; port++, p++) {
1633 if (moxa_ports[p].lowChkFlag) {
1634 moxa_ports[p].lowChkFlag = 0;
1635 ofsAddr = moxa_ports[p].tableAddr;
1636 moxa_low_water_check(ofsAddr);
1641 moxaLowWaterChk = 0;
1645 /*****************************************************************************
1646 * Card level function: *
1647 * 1. MoxaPortsOfCard(int cardno); *
1648 *****************************************************************************/
1649 int MoxaPortsOfCard(int cardno)
1652 if (moxa_boards[cardno].boardType == 0)
1654 return (moxa_boards[cardno].numPorts);
1657 /*****************************************************************************
1658 * Port level functions: *
1659 * 1. MoxaPortIsValid(int port); *
1660 * 2. MoxaPortEnable(int port); *
1661 * 3. MoxaPortDisable(int port); *
1662 * 4. MoxaPortGetMaxBaud(int port); *
1663 * 6. MoxaPortSetBaud(int port, long baud); *
1664 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1665 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1666 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1667 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1668 * 12. MoxaPortLineStatus(int port); *
1669 * 13. MoxaPortDCDChange(int port); *
1670 * 14. MoxaPortDCDON(int port); *
1671 * 15. MoxaPortFlushData(int port, int mode); *
1672 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1673 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
1674 * 20. MoxaPortTxQueue(int port); *
1675 * 21. MoxaPortTxFree(int port); *
1676 * 22. MoxaPortRxQueue(int port); *
1677 * 24. MoxaPortTxDisable(int port); *
1678 * 25. MoxaPortTxEnable(int port); *
1679 * 27. MoxaPortResetBrkCnt(int port); *
1680 * 30. MoxaPortSendBreak(int port, int ticks); *
1681 *****************************************************************************/
1683 * Moxa Port Number Description:
1685 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1686 * the port number using in MOXA driver functions will be 0 to 31 for
1687 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1688 * to 127 for fourth. For example, if you setup three MOXA boards,
1689 * first board is C218, second board is C320-16 and third board is
1690 * C320-32. The port number of first board (C218 - 8 ports) is from
1691 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1692 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1693 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1694 * 127 will be invalid.
1697 * Moxa Functions Description:
1699 * Function 1: Driver initialization routine, this routine must be
1700 * called when initialized driver.
1702 * void MoxaDriverInit();
1705 * Function 2: Moxa driver private IOCTL command processing.
1707 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1709 * unsigned int cmd : IOCTL command
1710 * unsigned long arg : IOCTL argument
1711 * int port : port number (0 - 127)
1718 * Function 3: Moxa driver polling process routine.
1720 * int MoxaDriverPoll(void);
1722 * return: 0 ; polling O.K.
1723 * -1 : no any Moxa card.
1726 * Function 4: Get the ports of this card.
1728 * int MoxaPortsOfCard(int cardno);
1730 * int cardno : card number (0 - 3)
1732 * return: 0 : this card is invalid
1736 * Function 5: Check this port is valid or invalid
1738 * int MoxaPortIsValid(int port);
1739 * int port : port number (0 - 127, ref port description)
1741 * return: 0 : this port is invalid
1742 * 1 : this port is valid
1745 * Function 6: Enable this port to start Tx/Rx data.
1747 * void MoxaPortEnable(int port);
1748 * int port : port number (0 - 127)
1751 * Function 7: Disable this port
1753 * void MoxaPortDisable(int port);
1754 * int port : port number (0 - 127)
1757 * Function 8: Get the maximun available baud rate of this port.
1759 * long MoxaPortGetMaxBaud(int port);
1760 * int port : port number (0 - 127)
1762 * return: 0 : this port is invalid
1763 * 38400/57600/115200 bps
1766 * Function 10: Setting baud rate of this port.
1768 * long MoxaPortSetBaud(int port, long baud);
1769 * int port : port number (0 - 127)
1770 * long baud : baud rate (50 - 115200)
1772 * return: 0 : this port is invalid or baud < 50
1773 * 50 - 115200 : the real baud rate set to the port, if
1774 * the argument baud is large than maximun
1775 * available baud rate, the real setting
1776 * baud rate will be the maximun baud rate.
1779 * Function 12: Configure the port.
1781 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1782 * int port : port number (0 - 127)
1783 * struct ktermios * termio : termio structure pointer
1784 * speed_t baud : baud rate
1786 * return: -1 : this port is invalid or termio == NULL
1790 * Function 13: Get the DTR/RTS state of this port.
1792 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1793 * int port : port number (0 - 127)
1794 * int * dtrState : pointer to INT to receive the current DTR
1795 * state. (if NULL, this function will not
1796 * write to this address)
1797 * int * rtsState : pointer to INT to receive the current RTS
1798 * state. (if NULL, this function will not
1799 * write to this address)
1801 * return: -1 : this port is invalid
1805 * Function 14: Setting the DTR/RTS output state of this port.
1807 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1808 * int port : port number (0 - 127)
1809 * int dtrState : DTR output state (0: off, 1: on)
1810 * int rtsState : RTS output state (0: off, 1: on)
1813 * Function 15: Setting the flow control of this port.
1815 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1816 * int txFlow,int xany);
1817 * int port : port number (0 - 127)
1818 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1819 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1820 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1821 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1822 * int xany : S/W XANY flow control (0: no, 1: yes)
1825 * Function 16: Get ths line status of this port
1827 * int MoxaPortLineStatus(int port);
1828 * int port : port number (0 - 127)
1830 * return: Bit 0 - CTS state (0: off, 1: on)
1831 * Bit 1 - DSR state (0: off, 1: on)
1832 * Bit 2 - DCD state (0: off, 1: on)
1835 * Function 17: Check the DCD state has changed since the last read
1838 * int MoxaPortDCDChange(int port);
1839 * int port : port number (0 - 127)
1841 * return: 0 : no changed
1842 * 1 : DCD has changed
1845 * Function 18: Check ths current DCD state is ON or not.
1847 * int MoxaPortDCDON(int port);
1848 * int port : port number (0 - 127)
1850 * return: 0 : DCD off
1854 * Function 19: Flush the Rx/Tx buffer data of this port.
1856 * void MoxaPortFlushData(int port, int mode);
1857 * int port : port number (0 - 127)
1859 * 0 : flush the Rx buffer
1860 * 1 : flush the Tx buffer
1861 * 2 : flush the Rx and Tx buffer
1864 * Function 20: Write data.
1866 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1867 * int port : port number (0 - 127)
1868 * unsigned char * buffer : pointer to write data buffer.
1869 * int length : write data length
1871 * return: 0 - length : real write data length
1874 * Function 21: Read data.
1876 * int MoxaPortReadData(int port, struct tty_struct *tty);
1877 * int port : port number (0 - 127)
1878 * struct tty_struct *tty : tty for data
1880 * return: 0 - length : real read data length
1883 * Function 24: Get the Tx buffer current queued data bytes
1885 * int MoxaPortTxQueue(int port);
1886 * int port : port number (0 - 127)
1888 * return: .. : Tx buffer current queued data bytes
1891 * Function 25: Get the Tx buffer current free space
1893 * int MoxaPortTxFree(int port);
1894 * int port : port number (0 - 127)
1896 * return: .. : Tx buffer current free space
1899 * Function 26: Get the Rx buffer current queued data bytes
1901 * int MoxaPortRxQueue(int port);
1902 * int port : port number (0 - 127)
1904 * return: .. : Rx buffer current queued data bytes
1907 * Function 28: Disable port data transmission.
1909 * void MoxaPortTxDisable(int port);
1910 * int port : port number (0 - 127)
1913 * Function 29: Enable port data transmission.
1915 * void MoxaPortTxEnable(int port);
1916 * int port : port number (0 - 127)
1919 * Function 31: Get the received BREAK signal count and reset it.
1921 * int MoxaPortResetBrkCnt(int port);
1922 * int port : port number (0 - 127)
1924 * return: 0 - .. : BREAK signal count
1927 * Function 34: Send out a BREAK signal.
1929 * void MoxaPortSendBreak(int port, int ms100);
1930 * int port : port number (0 - 127)
1931 * int ms100 : break signal time interval.
1932 * unit: 100 mini-second. if ms100 == 0, it will
1933 * send out a about 250 ms BREAK signal.
1936 int MoxaPortIsValid(int port)
1941 if (moxa_ports[port].chkPort == 0)
1946 void MoxaPortEnable(int port)
1948 void __iomem *ofsAddr;
1949 int MoxaPortLineStatus(int);
1950 short lowwater = 512;
1952 ofsAddr = moxa_ports[port].tableAddr;
1953 writew(lowwater, ofsAddr + Low_water);
1954 moxa_ports[port].breakCnt = 0;
1955 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
1956 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
1957 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1959 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
1962 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1963 moxafunc(ofsAddr, FC_FlushQueue, 2);
1965 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1966 MoxaPortLineStatus(port);
1969 void MoxaPortDisable(int port)
1971 void __iomem *ofsAddr = moxa_ports[port].tableAddr;
1973 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1974 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1975 writew(0, ofsAddr + HostStat);
1976 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1979 long MoxaPortGetMaxBaud(int port)
1981 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
1982 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI))
1989 long MoxaPortSetBaud(int port, long baud)
1991 void __iomem *ofsAddr;
1995 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
1997 ofsAddr = moxa_ports[port].tableAddr;
2001 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
2002 else if (max == 57600L)
2003 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
2005 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
2007 moxafunc(ofsAddr, FC_SetBaud, val);
2009 moxa_ports[port].curBaud = baud;
2013 int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud)
2015 void __iomem *ofsAddr;
2019 if (moxa_ports[port].chkPort == 0 || termio == 0)
2021 ofsAddr = moxa_ports[port].tableAddr;
2022 cflag = termio->c_cflag; /* termio->c_cflag */
2024 mode = termio->c_cflag & CSIZE;
2027 else if (mode == CS6)
2029 else if (mode == CS7)
2031 else if (mode == CS8)
2034 if (termio->c_cflag & CSTOPB) {
2042 if (termio->c_cflag & PARENB) {
2043 if (termio->c_cflag & PARODD)
2050 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
2052 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2053 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2054 if (baud >= 921600L)
2057 baud = MoxaPortSetBaud(port, baud);
2059 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
2060 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
2061 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
2062 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
2063 moxa_wait_finish(ofsAddr);
2069 int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState)
2072 if (!MoxaPortIsValid(port))
2075 if (moxa_ports[port].lineCtrl & DTR_ON)
2081 if (moxa_ports[port].lineCtrl & RTS_ON)
2089 void MoxaPortLineCtrl(int port, int dtr, int rts)
2091 void __iomem *ofsAddr;
2094 ofsAddr = moxa_ports[port].tableAddr;
2100 moxa_ports[port].lineCtrl = mode;
2101 moxafunc(ofsAddr, FC_LineControl, mode);
2104 void MoxaPortFlowCtrl(int port, int rts, int cts, int txflow, int rxflow, int txany)
2106 void __iomem *ofsAddr;
2109 ofsAddr = moxa_ports[port].tableAddr;
2112 mode |= RTS_FlowCtl;
2114 mode |= CTS_FlowCtl;
2121 moxafunc(ofsAddr, FC_SetFlowCtl, mode);
2124 int MoxaPortLineStatus(int port)
2126 void __iomem *ofsAddr;
2129 ofsAddr = moxa_ports[port].tableAddr;
2130 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2131 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2132 moxafunc(ofsAddr, FC_LineStatus, 0);
2133 val = readw(ofsAddr + FuncArg);
2135 val = readw(ofsAddr + FlagStat) >> 4;
2140 if ((moxa_ports[port].DCDState & DCD_oldstate) == 0)
2141 moxa_ports[port].DCDState = (DCD_oldstate | DCD_changed);
2143 if (moxa_ports[port].DCDState & DCD_oldstate)
2144 moxa_ports[port].DCDState = DCD_changed;
2150 int MoxaPortDCDChange(int port)
2154 if (moxa_ports[port].chkPort == 0)
2156 n = moxa_ports[port].DCDState;
2157 moxa_ports[port].DCDState &= ~DCD_changed;
2162 int MoxaPortDCDON(int port)
2166 if (moxa_ports[port].chkPort == 0)
2168 if (moxa_ports[port].DCDState & DCD_oldstate)
2175 int MoxaPortWriteData(int port, unsigned char * buffer, int len)
2180 ushort head, tx_mask, spage, epage;
2181 ushort pageno, pageofs, bufhead;
2182 void __iomem *baseAddr, *ofsAddr, *ofs;
2184 ofsAddr = moxa_ports[port].tableAddr;
2185 baseAddr = moxa_boards[port / MAX_PORTS_PER_BOARD].basemem;
2186 tx_mask = readw(ofsAddr + TX_mask);
2187 spage = readw(ofsAddr + Page_txb);
2188 epage = readw(ofsAddr + EndPage_txb);
2189 tail = readw(ofsAddr + TXwptr);
2190 head = readw(ofsAddr + TXrptr);
2191 c = (head > tail) ? (head - tail - 1)
2192 : (head - tail + tx_mask);
2195 moxaLog.txcnt[port] += c;
2197 if (spage == epage) {
2198 bufhead = readw(ofsAddr + Ofs_txb);
2199 writew(spage, baseAddr + Control_reg);
2202 len = head - tail - 1;
2204 len = tx_mask + 1 - tail;
2205 len = (c > len) ? len : c;
2206 ofs = baseAddr + DynPage_addr + bufhead + tail;
2207 for (i = 0; i < len; i++)
2208 writeb(*buffer++, ofs + i);
2209 tail = (tail + len) & tx_mask;
2212 writew(tail, ofsAddr + TXwptr);
2215 pageno = spage + (tail >> 13);
2216 pageofs = tail & Page_mask;
2218 cnt = Page_size - pageofs;
2222 writeb(pageno, baseAddr + Control_reg);
2223 ofs = baseAddr + DynPage_addr + pageofs;
2224 for (i = 0; i < cnt; i++)
2225 writeb(*buffer++, ofs + i);
2227 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2230 if (++pageno == epage)
2235 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2239 int MoxaPortReadData(int port, struct tty_struct *tty)
2241 register ushort head, pageofs;
2242 int i, count, cnt, len, total, remain;
2243 ushort tail, rx_mask, spage, epage;
2244 ushort pageno, bufhead;
2245 void __iomem *baseAddr, *ofsAddr, *ofs;
2247 ofsAddr = moxa_ports[port].tableAddr;
2248 baseAddr = moxa_boards[port / MAX_PORTS_PER_BOARD].basemem;
2249 head = readw(ofsAddr + RXrptr);
2250 tail = readw(ofsAddr + RXwptr);
2251 rx_mask = readw(ofsAddr + RX_mask);
2252 spage = readw(ofsAddr + Page_rxb);
2253 epage = readw(ofsAddr + EndPage_rxb);
2254 count = (tail >= head) ? (tail - head)
2255 : (tail - head + rx_mask + 1);
2260 remain = count - total;
2261 moxaLog.rxcnt[port] += total;
2263 if (spage == epage) {
2264 bufhead = readw(ofsAddr + Ofs_rxb);
2265 writew(spage, baseAddr + Control_reg);
2270 len = rx_mask + 1 - head;
2271 len = (count > len) ? len : count;
2272 ofs = baseAddr + DynPage_addr + bufhead + head;
2273 for (i = 0; i < len; i++)
2274 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2275 head = (head + len) & rx_mask;
2278 writew(head, ofsAddr + RXrptr);
2281 pageno = spage + (head >> 13);
2282 pageofs = head & Page_mask;
2284 cnt = Page_size - pageofs;
2288 writew(pageno, baseAddr + Control_reg);
2289 ofs = baseAddr + DynPage_addr + pageofs;
2290 for (i = 0; i < cnt; i++)
2291 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2293 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2296 if (++pageno == epage)
2301 if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2302 moxaLowWaterChk = 1;
2303 moxa_ports[port].lowChkFlag = 1;
2309 int MoxaPortTxQueue(int port)
2311 void __iomem *ofsAddr;
2312 ushort rptr, wptr, mask;
2315 ofsAddr = moxa_ports[port].tableAddr;
2316 rptr = readw(ofsAddr + TXrptr);
2317 wptr = readw(ofsAddr + TXwptr);
2318 mask = readw(ofsAddr + TX_mask);
2319 len = (wptr - rptr) & mask;
2323 int MoxaPortTxFree(int port)
2325 void __iomem *ofsAddr;
2326 ushort rptr, wptr, mask;
2329 ofsAddr = moxa_ports[port].tableAddr;
2330 rptr = readw(ofsAddr + TXrptr);
2331 wptr = readw(ofsAddr + TXwptr);
2332 mask = readw(ofsAddr + TX_mask);
2333 len = mask - ((wptr - rptr) & mask);
2337 int MoxaPortRxQueue(int port)
2339 void __iomem *ofsAddr;
2340 ushort rptr, wptr, mask;
2343 ofsAddr = moxa_ports[port].tableAddr;
2344 rptr = readw(ofsAddr + RXrptr);
2345 wptr = readw(ofsAddr + RXwptr);
2346 mask = readw(ofsAddr + RX_mask);
2347 len = (wptr - rptr) & mask;
2352 void MoxaPortTxDisable(int port)
2354 void __iomem *ofsAddr;
2356 ofsAddr = moxa_ports[port].tableAddr;
2357 moxafunc(ofsAddr, FC_SetXoffState, Magic_code);
2360 void MoxaPortTxEnable(int port)
2362 void __iomem *ofsAddr;
2364 ofsAddr = moxa_ports[port].tableAddr;
2365 moxafunc(ofsAddr, FC_SetXonState, Magic_code);
2369 int MoxaPortResetBrkCnt(int port)
2372 cnt = moxa_ports[port].breakCnt;
2373 moxa_ports[port].breakCnt = 0;
2378 void MoxaPortSendBreak(int port, int ms100)
2380 void __iomem *ofsAddr;
2382 ofsAddr = moxa_ports[port].tableAddr;
2384 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2387 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2390 moxafunc(ofsAddr, FC_StopBreak, Magic_code);
2393 static int moxa_get_serial_info(struct moxa_port *info,
2394 struct serial_struct __user *retinfo)
2396 struct serial_struct tmp;
2398 memset(&tmp, 0, sizeof(tmp));
2399 tmp.type = info->type;
2400 tmp.line = info->port;
2403 tmp.flags = info->asyncflags;
2404 tmp.baud_base = 921600;
2405 tmp.close_delay = info->close_delay;
2406 tmp.closing_wait = info->closing_wait;
2407 tmp.custom_divisor = 0;
2409 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2415 static int moxa_set_serial_info(struct moxa_port *info,
2416 struct serial_struct __user *new_info)
2418 struct serial_struct new_serial;
2420 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2423 if ((new_serial.irq != 0) ||
2424 (new_serial.port != 0) ||
2425 // (new_serial.type != info->type) ||
2426 (new_serial.custom_divisor != 0) ||
2427 (new_serial.baud_base != 921600))
2430 if (!capable(CAP_SYS_ADMIN)) {
2431 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2432 (info->asyncflags & ~ASYNC_USR_MASK)))
2435 info->close_delay = new_serial.close_delay * HZ / 100;
2436 info->closing_wait = new_serial.closing_wait * HZ / 100;
2439 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2440 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2442 if (new_serial.type == PORT_16550A) {
2443 MoxaSetFifo(info->port, 1);
2445 MoxaSetFifo(info->port, 0);
2448 info->type = new_serial.type;
2454 /*****************************************************************************
2455 * Static local functions: *
2456 *****************************************************************************/
2457 static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
2460 writew(arg, ofsAddr + FuncArg);
2461 writew(cmd, ofsAddr + FuncCode);
2462 moxa_wait_finish(ofsAddr);
2465 static void moxa_wait_finish(void __iomem *ofsAddr)
2470 while (readw(ofsAddr + FuncCode) != 0) {
2472 if ((j - i) > moxaFuncTout) {
2478 static void moxa_low_water_check(void __iomem *ofsAddr)
2481 ushort rptr, wptr, mask;
2483 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2484 rptr = readw(ofsAddr + RXrptr);
2485 wptr = readw(ofsAddr + RXwptr);
2486 mask = readw(ofsAddr + RX_mask);
2487 len = (wptr - rptr) & mask;
2488 if (len <= Low_water)
2489 moxafunc(ofsAddr, FC_SendXon, 0);
2493 static int moxaloadbios(int cardno, unsigned char __user *tmp, int len)
2495 void __iomem *baseAddr;
2498 if(len < 0 || len > sizeof(moxaBuff))
2500 if(copy_from_user(moxaBuff, tmp, len))
2502 baseAddr = moxa_boards[cardno].basemem;
2503 writeb(HW_reset, baseAddr + Control_reg); /* reset */
2505 for (i = 0; i < 4096; i++)
2506 writeb(0, baseAddr + i); /* clear fix page */
2507 for (i = 0; i < len; i++)
2508 writeb(moxaBuff[i], baseAddr + i); /* download BIOS */
2509 writeb(0, baseAddr + Control_reg); /* restart */
2513 static int moxafindcard(int cardno)
2515 void __iomem *baseAddr;
2518 baseAddr = moxa_boards[cardno].basemem;
2519 switch (moxa_boards[cardno].boardType) {
2520 case MOXA_BOARD_C218_ISA:
2521 case MOXA_BOARD_C218_PCI:
2522 if ((tmp = readw(baseAddr + C218_key)) != C218_KeyCode) {
2526 case MOXA_BOARD_CP204J:
2527 if ((tmp = readw(baseAddr + C218_key)) != CP204J_KeyCode) {
2532 if ((tmp = readw(baseAddr + C320_key)) != C320_KeyCode) {
2535 if ((tmp = readw(baseAddr + C320_status)) != STS_init) {
2542 static int moxaload320b(int cardno, unsigned char __user *tmp, int len)
2544 void __iomem *baseAddr;
2547 if(len < 0 || len > sizeof(moxaBuff))
2549 if(copy_from_user(moxaBuff, tmp, len))
2551 baseAddr = moxa_boards[cardno].basemem;
2552 writew(len - 7168 - 2, baseAddr + C320bapi_len);
2553 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
2554 for (i = 0; i < 7168; i++)
2555 writeb(moxaBuff[i], baseAddr + DynPage_addr + i);
2556 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
2557 for (i = 0; i < (len - 7168); i++)
2558 writeb(moxaBuff[i + 7168], baseAddr + DynPage_addr + i);
2562 static int moxaloadcode(int cardno, unsigned char __user *tmp, int len)
2564 void __iomem *baseAddr, *ofsAddr;
2565 int retval, port, i;
2567 if(len < 0 || len > sizeof(moxaBuff))
2569 if(copy_from_user(moxaBuff, tmp, len))
2571 baseAddr = moxa_boards[cardno].basemem;
2572 switch (moxa_boards[cardno].boardType) {
2573 case MOXA_BOARD_C218_ISA:
2574 case MOXA_BOARD_C218_PCI:
2575 case MOXA_BOARD_CP204J:
2576 retval = moxaloadc218(cardno, baseAddr, len);
2579 port = cardno * MAX_PORTS_PER_BOARD;
2580 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2581 struct moxa_port *p = &moxa_ports[port];
2586 p->tableAddr = baseAddr + Extern_table + Extern_size * i;
2587 ofsAddr = p->tableAddr;
2588 writew(C218rx_mask, ofsAddr + RX_mask);
2589 writew(C218tx_mask, ofsAddr + TX_mask);
2590 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
2591 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
2593 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
2594 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
2599 retval = moxaloadc320(cardno, baseAddr, len,
2600 &moxa_boards[cardno].numPorts);
2603 port = cardno * MAX_PORTS_PER_BOARD;
2604 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2605 struct moxa_port *p = &moxa_ports[port];
2610 p->tableAddr = baseAddr + Extern_table + Extern_size * i;
2611 ofsAddr = p->tableAddr;
2612 if (moxa_boards[cardno].numPorts == 8) {
2613 writew(C320p8rx_mask, ofsAddr + RX_mask);
2614 writew(C320p8tx_mask, ofsAddr + TX_mask);
2615 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
2616 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
2617 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
2618 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
2620 } else if (moxa_boards[cardno].numPorts == 16) {
2621 writew(C320p16rx_mask, ofsAddr + RX_mask);
2622 writew(C320p16tx_mask, ofsAddr + TX_mask);
2623 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
2624 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
2625 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
2626 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
2628 } else if (moxa_boards[cardno].numPorts == 24) {
2629 writew(C320p24rx_mask, ofsAddr + RX_mask);
2630 writew(C320p24tx_mask, ofsAddr + TX_mask);
2631 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
2632 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
2633 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
2634 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2635 } else if (moxa_boards[cardno].numPorts == 32) {
2636 writew(C320p32rx_mask, ofsAddr + RX_mask);
2637 writew(C320p32tx_mask, ofsAddr + TX_mask);
2638 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
2639 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
2640 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
2641 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
2642 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2647 moxa_boards[cardno].loadstat = 1;
2651 static int moxaloadc218(int cardno, void __iomem *baseAddr, int len)
2654 int i, j, len1, len2;
2655 ushort usum, *ptr, keycode;
2657 if (moxa_boards[cardno].boardType == MOXA_BOARD_CP204J)
2658 keycode = CP204J_KeyCode;
2660 keycode = C218_KeyCode;
2663 ptr = (ushort *) moxaBuff;
2664 for (i = 0; i < len1; i++)
2665 usum += le16_to_cpu(*(ptr + i));
2671 len2 = (len1 > 2048) ? 2048 : len1;
2673 for (i = 0; i < len2 << 1; i++)
2674 writeb(moxaBuff[i + j], baseAddr + C218_LoadBuf + i);
2677 writew(len2, baseAddr + C218DLoad_len);
2678 writew(0, baseAddr + C218_key);
2679 for (i = 0; i < 100; i++) {
2680 if (readw(baseAddr + C218_key) == keycode)
2684 if (readw(baseAddr + C218_key) != keycode) {
2688 writew(0, baseAddr + C218DLoad_len);
2689 writew(usum, baseAddr + C218check_sum);
2690 writew(0, baseAddr + C218_key);
2691 for (i = 0; i < 100; i++) {
2692 if (readw(baseAddr + C218_key) == keycode)
2697 } while ((readb(baseAddr + C218chksum_ok) != 1) && (retry < 3));
2698 if (readb(baseAddr + C218chksum_ok) != 1) {
2701 writew(0, baseAddr + C218_key);
2702 for (i = 0; i < 100; i++) {
2703 if (readw(baseAddr + Magic_no) == Magic_code)
2707 if (readw(baseAddr + Magic_no) != Magic_code) {
2710 writew(1, baseAddr + Disable_IRQ);
2711 writew(0, baseAddr + Magic_no);
2712 for (i = 0; i < 100; i++) {
2713 if (readw(baseAddr + Magic_no) == Magic_code)
2717 if (readw(baseAddr + Magic_no) != Magic_code) {
2721 moxa_boards[cardno].intNdx = baseAddr + IRQindex;
2722 moxa_boards[cardno].intPend = baseAddr + IRQpending;
2723 moxa_boards[cardno].intTable = baseAddr + IRQtable;
2727 static int moxaloadc320(int cardno, void __iomem *baseAddr, int len, int *numPorts)
2730 int i, j, wlen, len2, retry;
2735 uptr = (ushort *) moxaBuff;
2736 for (i = 0; i < wlen; i++)
2737 usum += le16_to_cpu(uptr[i]);
2748 for (i = 0; i < len2; i++)
2749 writeb(moxaBuff[j + i], baseAddr + C320_LoadBuf + i);
2752 writew(len2, baseAddr + C320DLoad_len);
2753 writew(0, baseAddr + C320_key);
2754 for (i = 0; i < 10; i++) {
2755 if (readw(baseAddr + C320_key) == C320_KeyCode)
2759 if (readw(baseAddr + C320_key) != C320_KeyCode)
2762 writew(0, baseAddr + C320DLoad_len);
2763 writew(usum, baseAddr + C320check_sum);
2764 writew(0, baseAddr + C320_key);
2765 for (i = 0; i < 10; i++) {
2766 if (readw(baseAddr + C320_key) == C320_KeyCode)
2771 } while ((readb(baseAddr + C320chksum_ok) != 1) && (retry < 3));
2772 if (readb(baseAddr + C320chksum_ok) != 1)
2774 writew(0, baseAddr + C320_key);
2775 for (i = 0; i < 600; i++) {
2776 if (readw(baseAddr + Magic_no) == Magic_code)
2780 if (readw(baseAddr + Magic_no) != Magic_code)
2783 if (moxa_boards[cardno].busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
2784 writew(0x3800, baseAddr + TMS320_PORT1);
2785 writew(0x3900, baseAddr + TMS320_PORT2);
2786 writew(28499, baseAddr + TMS320_CLOCK);
2788 writew(0x3200, baseAddr + TMS320_PORT1);
2789 writew(0x3400, baseAddr + TMS320_PORT2);
2790 writew(19999, baseAddr + TMS320_CLOCK);
2792 writew(1, baseAddr + Disable_IRQ);
2793 writew(0, baseAddr + Magic_no);
2794 for (i = 0; i < 500; i++) {
2795 if (readw(baseAddr + Magic_no) == Magic_code)
2799 if (readw(baseAddr + Magic_no) != Magic_code)
2802 j = readw(baseAddr + Module_cnt);
2806 writew(j, baseAddr + Module_no);
2807 writew(0, baseAddr + Magic_no);
2808 for (i = 0; i < 600; i++) {
2809 if (readw(baseAddr + Magic_no) == Magic_code)
2813 if (readw(baseAddr + Magic_no) != Magic_code)
2816 moxa_boards[cardno].intNdx = baseAddr + IRQindex;
2817 moxa_boards[cardno].intPend = baseAddr + IRQpending;
2818 moxa_boards[cardno].intTable = baseAddr + IRQtable;
2822 static void MoxaSetFifo(int port, int enable)
2824 void __iomem *ofsAddr = moxa_ports[port].tableAddr;
2827 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2828 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2830 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2831 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);