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];
293 board->basemem = pci_iomap(pdev, 2, 0x4000);
294 if (board->basemem == NULL) {
295 dev_err(&pdev->dev, "can't remap io space 2\n");
299 board->boardType = board_type;
300 switch (board_type) {
301 case MOXA_BOARD_C218_ISA:
302 case MOXA_BOARD_C218_PCI:
306 case MOXA_BOARD_CP204J:
313 board->busType = MOXA_BUS_TYPE_PCI;
315 pci_set_drvdata(pdev, board);
322 static void __devexit moxa_pci_remove(struct pci_dev *pdev)
324 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
326 pci_iounmap(pdev, brd->basemem);
330 static struct pci_driver moxa_pci_driver = {
332 .id_table = moxa_pcibrds,
333 .probe = moxa_pci_probe,
334 .remove = __devexit_p(moxa_pci_remove)
336 #endif /* CONFIG_PCI */
338 static int __init moxa_init(void)
340 struct moxa_port *ch;
341 unsigned int i, isabrds = 0;
344 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
346 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
350 moxaDriver->owner = THIS_MODULE;
351 moxaDriver->name = "ttyMX";
352 moxaDriver->major = ttymajor;
353 moxaDriver->minor_start = 0;
354 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
355 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
356 moxaDriver->init_termios = tty_std_termios;
357 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
358 moxaDriver->init_termios.c_ispeed = 9600;
359 moxaDriver->init_termios.c_ospeed = 9600;
360 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
361 tty_set_operations(moxaDriver, &moxa_ops);
363 for (i = 0, ch = moxa_ports; i < MAX_PORTS; i++, ch++) {
364 ch->type = PORT_16550A;
366 ch->close_delay = 5 * HZ / 10;
367 ch->closing_wait = 30 * HZ;
368 ch->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
369 init_waitqueue_head(&ch->open_wait);
370 init_completion(&ch->close_wait);
372 setup_timer(&ch->emptyTimer, moxa_check_xmit_empty,
376 pr_debug("Moxa tty devices major number = %d\n", ttymajor);
378 if (tty_register_driver(moxaDriver)) {
379 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
380 put_tty_driver(moxaDriver);
384 mod_timer(&moxaTimer, jiffies + HZ / 50);
386 /* Find the boards defined from module args. */
389 struct moxa_board_conf *brd = moxa_boards;
390 for (i = 0; i < MAX_BOARDS; i++) {
393 if (type[i] == MOXA_BOARD_C218_ISA ||
394 type[i] == MOXA_BOARD_C320_ISA) {
395 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
396 isabrds + 1, moxa_brdname[type[i] - 1],
398 brd->boardType = type[i];
399 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
401 brd->busType = MOXA_BUS_TYPE_ISA;
402 brd->basemem = ioremap(baseaddr[i], 0x4000);
404 printk(KERN_ERR "moxa: can't remap %lx\n",
417 retval = pci_register_driver(&moxa_pci_driver);
419 printk(KERN_ERR "Can't register moxa pci driver!\n");
428 static void __exit moxa_exit(void)
432 del_timer_sync(&moxaTimer);
434 for (i = 0; i < MAX_PORTS; i++)
435 del_timer_sync(&moxa_ports[i].emptyTimer);
437 if (tty_unregister_driver(moxaDriver))
438 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
440 put_tty_driver(moxaDriver);
443 pci_unregister_driver(&moxa_pci_driver);
446 for (i = 0; i < MAX_BOARDS; i++)
447 if (moxa_boards[i].basemem)
448 iounmap(moxa_boards[i].basemem);
451 module_init(moxa_init);
452 module_exit(moxa_exit);
454 static int moxa_open(struct tty_struct *tty, struct file *filp)
456 struct moxa_port *ch;
461 if (port == MAX_PORTS) {
464 if (!MoxaPortIsValid(port)) {
465 tty->driver_data = NULL;
469 ch = &moxa_ports[port];
471 tty->driver_data = ch;
473 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
475 moxa_set_tty_param(tty, tty->termios);
476 MoxaPortLineCtrl(ch->port, 1, 1);
477 MoxaPortEnable(ch->port);
478 ch->asyncflags |= ASYNC_INITIALIZED;
480 retval = moxa_block_till_ready(tty, filp, ch);
482 moxa_unthrottle(tty);
484 if (ch->type == PORT_16550A) {
485 MoxaSetFifo(ch->port, 1);
487 MoxaSetFifo(ch->port, 0);
493 static void moxa_close(struct tty_struct *tty, struct file *filp)
495 struct moxa_port *ch;
499 if (port == MAX_PORTS) {
502 if (!MoxaPortIsValid(port)) {
503 pr_debug("Invalid portno in moxa_close\n");
504 tty->driver_data = NULL;
507 if (tty->driver_data == NULL) {
510 if (tty_hung_up_p(filp)) {
513 ch = (struct moxa_port *) tty->driver_data;
515 if ((tty->count == 1) && (ch->count != 1)) {
516 printk(KERN_WARNING "moxa_close: bad serial port count; "
517 "tty->count is 1, ch->count is %d\n", ch->count);
520 if (--ch->count < 0) {
521 printk(KERN_WARNING "moxa_close: bad serial port count, "
522 "device=%s\n", tty->name);
528 ch->asyncflags |= ASYNC_CLOSING;
530 ch->cflag = tty->termios->c_cflag;
531 if (ch->asyncflags & ASYNC_INITIALIZED) {
532 moxa_setup_empty_event(tty);
533 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
534 del_timer_sync(&moxa_ports[ch->port].emptyTimer);
537 MoxaPortFlushData(port, 2);
539 if (tty->driver->flush_buffer)
540 tty->driver->flush_buffer(tty);
541 tty_ldisc_flush(tty);
546 if (ch->blocked_open) {
547 if (ch->close_delay) {
548 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
550 wake_up_interruptible(&ch->open_wait);
552 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
553 complete_all(&ch->close_wait);
556 static int moxa_write(struct tty_struct *tty,
557 const unsigned char *buf, int count)
559 struct moxa_port *ch;
563 ch = (struct moxa_port *) tty->driver_data;
568 spin_lock_irqsave(&moxa_lock, flags);
569 len = MoxaPortWriteData(port, (unsigned char *) buf, count);
570 spin_unlock_irqrestore(&moxa_lock, flags);
572 /*********************************************
573 if ( !(ch->statusflags & LOWWAIT) &&
574 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
575 ************************************************/
576 ch->statusflags |= LOWWAIT;
580 static int moxa_write_room(struct tty_struct *tty)
582 struct moxa_port *ch;
586 ch = (struct moxa_port *) tty->driver_data;
589 return (MoxaPortTxFree(ch->port));
592 static void moxa_flush_buffer(struct tty_struct *tty)
594 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
598 MoxaPortFlushData(ch->port, 1);
602 static int moxa_chars_in_buffer(struct tty_struct *tty)
605 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
608 * Sigh...I have to check if driver_data is NULL here, because
609 * if an open() fails, the TTY subsystem eventually calls
610 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
611 * routine. And since the open() failed, we return 0 here. TDJ
615 chars = MoxaPortTxQueue(ch->port);
618 * Make it possible to wakeup anything waiting for output
619 * in tty_ioctl.c, etc.
621 if (!(ch->statusflags & EMPTYWAIT))
622 moxa_setup_empty_event(tty);
627 static void moxa_flush_chars(struct tty_struct *tty)
630 * Don't think I need this, because this is called to empty the TX
631 * buffer for the 16450, 16550, etc.
635 static void moxa_put_char(struct tty_struct *tty, unsigned char c)
637 struct moxa_port *ch;
641 ch = (struct moxa_port *) tty->driver_data;
645 spin_lock_irqsave(&moxa_lock, flags);
646 MoxaPortWriteData(port, &c, 1);
647 spin_unlock_irqrestore(&moxa_lock, flags);
648 /************************************************
649 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
650 *************************************************/
651 ch->statusflags |= LOWWAIT;
654 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
656 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
658 int flag = 0, dtr, rts;
661 if ((port != MAX_PORTS) && (!ch))
664 MoxaPortGetLineOut(ch->port, &dtr, &rts);
669 dtr = MoxaPortLineStatus(ch->port);
679 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
680 unsigned int set, unsigned int clear)
682 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
687 if ((port != MAX_PORTS) && (!ch))
690 MoxaPortGetLineOut(ch->port, &dtr, &rts);
695 if (clear & TIOCM_RTS)
697 if (clear & TIOCM_DTR)
699 MoxaPortLineCtrl(ch->port, dtr, rts);
703 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
704 unsigned int cmd, unsigned long arg)
706 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
708 void __user *argp = (void __user *)arg;
712 if ((port != MAX_PORTS) && (!ch))
716 case TCSBRK: /* SVID version: non-zero arg --> no break */
717 retval = tty_check_change(tty);
720 moxa_setup_empty_event(tty);
721 tty_wait_until_sent(tty, 0);
723 MoxaPortSendBreak(ch->port, 0);
725 case TCSBRKP: /* support for POSIX tcsendbreak() */
726 retval = tty_check_change(tty);
729 moxa_setup_empty_event(tty);
730 tty_wait_until_sent(tty, 0);
731 MoxaPortSendBreak(ch->port, arg);
734 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) argp);
736 if(get_user(retval, (unsigned long __user *) argp))
739 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
742 ch->asyncflags &= ~ASYNC_CHECK_CD;
744 ch->asyncflags |= ASYNC_CHECK_CD;
747 return moxa_get_serial_info(ch, argp);
750 return moxa_set_serial_info(ch, argp);
752 retval = MoxaDriverIoctl(cmd, arg, port);
757 static void moxa_throttle(struct tty_struct *tty)
759 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
761 ch->statusflags |= THROTTLE;
764 static void moxa_unthrottle(struct tty_struct *tty)
766 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
768 ch->statusflags &= ~THROTTLE;
771 static void moxa_set_termios(struct tty_struct *tty,
772 struct ktermios *old_termios)
774 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
778 moxa_set_tty_param(tty, old_termios);
779 if (!(old_termios->c_cflag & CLOCAL) &&
780 (tty->termios->c_cflag & CLOCAL))
781 wake_up_interruptible(&ch->open_wait);
784 static void moxa_stop(struct tty_struct *tty)
786 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
790 MoxaPortTxDisable(ch->port);
791 ch->statusflags |= TXSTOPPED;
795 static void moxa_start(struct tty_struct *tty)
797 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
802 if (!(ch->statusflags & TXSTOPPED))
805 MoxaPortTxEnable(ch->port);
806 ch->statusflags &= ~TXSTOPPED;
809 static void moxa_hangup(struct tty_struct *tty)
811 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
813 moxa_flush_buffer(tty);
817 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
819 wake_up_interruptible(&ch->open_wait);
822 static void moxa_poll(unsigned long ignored)
825 struct moxa_port *ch;
826 struct tty_struct *tp;
829 del_timer(&moxaTimer);
831 if (MoxaDriverPoll() < 0) {
832 mod_timer(&moxaTimer, jiffies + HZ / 50);
835 for (card = 0; card < MAX_BOARDS; card++) {
836 if ((ports = MoxaPortsOfCard(card)) <= 0)
838 ch = &moxa_ports[card * MAX_PORTS_PER_BOARD];
839 for (i = 0; i < ports; i++, ch++) {
840 if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
842 if (!(ch->statusflags & THROTTLE) &&
843 (MoxaPortRxQueue(ch->port) > 0))
844 moxa_receive_data(ch);
845 if ((tp = ch->tty) == 0)
847 if (ch->statusflags & LOWWAIT) {
848 if (MoxaPortTxQueue(ch->port) <= WAKEUP_CHARS) {
850 ch->statusflags &= ~LOWWAIT;
855 if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch->port) > 0)) {
856 tty_insert_flip_char(tp, 0, TTY_BREAK);
857 tty_schedule_flip(tp);
859 if (MoxaPortDCDChange(ch->port)) {
860 if (ch->asyncflags & ASYNC_CHECK_CD) {
861 if (MoxaPortDCDON(ch->port))
862 wake_up_interruptible(&ch->open_wait);
865 wake_up_interruptible(&ch->open_wait);
866 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
873 mod_timer(&moxaTimer, jiffies + HZ / 50);
876 /******************************************************************************/
878 static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
880 register struct ktermios *ts;
881 struct moxa_port *ch;
882 int rts, cts, txflow, rxflow, xany, baud;
884 ch = (struct moxa_port *) tty->driver_data;
886 if (ts->c_cflag & CLOCAL)
887 ch->asyncflags &= ~ASYNC_CHECK_CD;
889 ch->asyncflags |= ASYNC_CHECK_CD;
890 rts = cts = txflow = rxflow = xany = 0;
891 if (ts->c_cflag & CRTSCTS)
893 if (ts->c_iflag & IXON)
895 if (ts->c_iflag & IXOFF)
897 if (ts->c_iflag & IXANY)
900 /* Clear the features we don't support */
901 ts->c_cflag &= ~CMSPAR;
902 MoxaPortFlowCtrl(ch->port, rts, cts, txflow, rxflow, xany);
903 baud = MoxaPortSetTermio(ch->port, ts, tty_get_baud_rate(tty));
905 baud = tty_termios_baud_rate(old_termios);
906 /* Not put the baud rate into the termios data */
907 tty_encode_baud_rate(tty, baud, baud);
910 static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
911 struct moxa_port *ch)
913 DECLARE_WAITQUEUE(wait,current);
916 int do_clocal = C_CLOCAL(tty);
919 * If the device is in the middle of being closed, then block
920 * until it's done, and then try again.
922 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
923 if (ch->asyncflags & ASYNC_CLOSING)
924 wait_for_completion_interruptible(&ch->close_wait);
925 #ifdef SERIAL_DO_RESTART
926 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
929 return (-ERESTARTSYS);
935 * If non-blocking mode is set, then make the check up front
938 if (filp->f_flags & O_NONBLOCK) {
939 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
943 * Block waiting for the carrier detect and the line to become free
946 add_wait_queue(&ch->open_wait, &wait);
947 pr_debug("block_til_ready before block: ttys%d, count = %d\n",
948 ch->port, ch->count);
949 spin_lock_irqsave(&moxa_lock, flags);
950 if (!tty_hung_up_p(filp))
953 spin_unlock_irqrestore(&moxa_lock, flags);
956 set_current_state(TASK_INTERRUPTIBLE);
957 if (tty_hung_up_p(filp) ||
958 !(ch->asyncflags & ASYNC_INITIALIZED)) {
959 #ifdef SERIAL_DO_RESTART
960 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
963 retval = -ERESTARTSYS;
969 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
970 MoxaPortDCDON(ch->port)))
973 if (signal_pending(current)) {
974 retval = -ERESTARTSYS;
979 set_current_state(TASK_RUNNING);
980 remove_wait_queue(&ch->open_wait, &wait);
982 spin_lock_irqsave(&moxa_lock, flags);
983 if (!tty_hung_up_p(filp))
986 spin_unlock_irqrestore(&moxa_lock, flags);
987 pr_debug("block_til_ready after blocking: ttys%d, count = %d\n",
988 ch->port, ch->count);
991 /* FIXME: review to see if we need to use set_bit on these */
992 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
996 static void moxa_setup_empty_event(struct tty_struct *tty)
998 struct moxa_port *ch = tty->driver_data;
1001 spin_lock_irqsave(&moxa_lock, flags);
1002 ch->statusflags |= EMPTYWAIT;
1003 mod_timer(&moxa_ports[ch->port].emptyTimer, jiffies + HZ);
1004 spin_unlock_irqrestore(&moxa_lock, flags);
1007 static void moxa_check_xmit_empty(unsigned long data)
1009 struct moxa_port *ch;
1011 ch = (struct moxa_port *) data;
1012 if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
1013 if (MoxaPortTxQueue(ch->port) == 0) {
1014 ch->statusflags &= ~EMPTYWAIT;
1015 tty_wakeup(ch->tty);
1018 mod_timer(&moxa_ports[ch->port].emptyTimer,
1019 round_jiffies(jiffies + HZ));
1021 ch->statusflags &= ~EMPTYWAIT;
1024 static void moxa_shut_down(struct moxa_port *ch)
1026 struct tty_struct *tp;
1028 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1033 MoxaPortDisable(ch->port);
1036 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1038 if (tp->termios->c_cflag & HUPCL)
1039 MoxaPortLineCtrl(ch->port, 0, 0);
1041 ch->asyncflags &= ~ASYNC_INITIALIZED;
1044 static void moxa_receive_data(struct moxa_port *ch)
1046 struct tty_struct *tp;
1047 struct ktermios *ts;
1048 unsigned long flags;
1054 /**************************************************
1055 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1056 *****************************************************/
1058 MoxaPortFlushData(ch->port, 0);
1061 spin_lock_irqsave(&moxa_lock, flags);
1062 MoxaPortReadData(ch->port, tp);
1063 spin_unlock_irqrestore(&moxa_lock, flags);
1064 tty_schedule_flip(tp);
1067 #define Magic_code 0x404
1070 * System Configuration
1073 * for C218 BIOS initialization
1075 #define C218_ConfBase 0x800
1076 #define C218_status (C218_ConfBase + 0) /* BIOS running status */
1077 #define C218_diag (C218_ConfBase + 2) /* diagnostic status */
1078 #define C218_key (C218_ConfBase + 4) /* WORD (0x218 for C218) */
1079 #define C218DLoad_len (C218_ConfBase + 6) /* WORD */
1080 #define C218check_sum (C218_ConfBase + 8) /* BYTE */
1081 #define C218chksum_ok (C218_ConfBase + 0x0a) /* BYTE (1:ok) */
1082 #define C218_TestRx (C218_ConfBase + 0x10) /* 8 bytes for 8 ports */
1083 #define C218_TestTx (C218_ConfBase + 0x18) /* 8 bytes for 8 ports */
1084 #define C218_RXerr (C218_ConfBase + 0x20) /* 8 bytes for 8 ports */
1085 #define C218_ErrFlag (C218_ConfBase + 0x28) /* 8 bytes for 8 ports */
1087 #define C218_LoadBuf 0x0F00
1088 #define C218_KeyCode 0x218
1089 #define CP204J_KeyCode 0x204
1092 * for C320 BIOS initialization
1094 #define C320_ConfBase 0x800
1095 #define C320_LoadBuf 0x0f00
1096 #define STS_init 0x05 /* for C320_status */
1098 #define C320_status C320_ConfBase + 0 /* BIOS running status */
1099 #define C320_diag C320_ConfBase + 2 /* diagnostic status */
1100 #define C320_key C320_ConfBase + 4 /* WORD (0320H for C320) */
1101 #define C320DLoad_len C320_ConfBase + 6 /* WORD */
1102 #define C320check_sum C320_ConfBase + 8 /* WORD */
1103 #define C320chksum_ok C320_ConfBase + 0x0a /* WORD (1:ok) */
1104 #define C320bapi_len C320_ConfBase + 0x0c /* WORD */
1105 #define C320UART_no C320_ConfBase + 0x0e /* WORD */
1107 #define C320_KeyCode 0x320
1109 #define FixPage_addr 0x0000 /* starting addr of static page */
1110 #define DynPage_addr 0x2000 /* starting addr of dynamic page */
1111 #define C218_start 0x3000 /* starting addr of C218 BIOS prg */
1112 #define Control_reg 0x1ff0 /* select page and reset control */
1113 #define HW_reset 0x80
1118 #define FC_CardReset 0x80
1119 #define FC_ChannelReset 1 /* C320 firmware not supported */
1120 #define FC_EnableCH 2
1121 #define FC_DisableCH 3
1122 #define FC_SetParam 4
1123 #define FC_SetMode 5
1124 #define FC_SetRate 6
1125 #define FC_LineControl 7
1126 #define FC_LineStatus 8
1127 #define FC_XmitControl 9
1128 #define FC_FlushQueue 10
1129 #define FC_SendBreak 11
1130 #define FC_StopBreak 12
1131 #define FC_LoopbackON 13
1132 #define FC_LoopbackOFF 14
1133 #define FC_ClrIrqTable 15
1134 #define FC_SendXon 16
1135 #define FC_SetTermIrq 17 /* C320 firmware not supported */
1136 #define FC_SetCntIrq 18 /* C320 firmware not supported */
1137 #define FC_SetBreakIrq 19
1138 #define FC_SetLineIrq 20
1139 #define FC_SetFlowCtl 21
1140 #define FC_GenIrq 22
1141 #define FC_InCD180 23
1142 #define FC_OutCD180 24
1143 #define FC_InUARTreg 23
1144 #define FC_OutUARTreg 24
1145 #define FC_SetXonXoff 25
1146 #define FC_OutCD180CCR 26
1147 #define FC_ExtIQueue 27
1148 #define FC_ExtOQueue 28
1149 #define FC_ClrLineIrq 29
1150 #define FC_HWFlowCtl 30
1151 #define FC_GetClockRate 35
1152 #define FC_SetBaud 36
1153 #define FC_SetDataMode 41
1154 #define FC_GetCCSR 43
1155 #define FC_GetDataError 45
1156 #define FC_RxControl 50
1157 #define FC_ImmSend 51
1158 #define FC_SetXonState 52
1159 #define FC_SetXoffState 53
1160 #define FC_SetRxFIFOTrig 54
1161 #define FC_SetTxFIFOCnt 55
1162 #define FC_UnixRate 56
1163 #define FC_UnixResetTimer 57
1165 #define RxFIFOTrig1 0
1166 #define RxFIFOTrig4 1
1167 #define RxFIFOTrig8 2
1168 #define RxFIFOTrig14 3
1173 #define DRAM_global 0
1174 #define INT_data (DRAM_global + 0)
1175 #define Config_base (DRAM_global + 0x108)
1177 #define IRQindex (INT_data + 0)
1178 #define IRQpending (INT_data + 4)
1179 #define IRQtable (INT_data + 8)
1184 #define IntrRx 0x01 /* receiver data O.K. */
1185 #define IntrTx 0x02 /* transmit buffer empty */
1186 #define IntrFunc 0x04 /* function complete */
1187 #define IntrBreak 0x08 /* received break */
1188 #define IntrLine 0x10 /* line status change
1190 #define IntrIntr 0x20 /* received INTR code */
1191 #define IntrQuit 0x40 /* received QUIT code */
1192 #define IntrEOF 0x80 /* received EOF code */
1194 #define IntrRxTrigger 0x100 /* rx data count reach tigger value */
1195 #define IntrTxTrigger 0x200 /* tx data count below trigger value */
1197 #define Magic_no (Config_base + 0)
1198 #define Card_model_no (Config_base + 2)
1199 #define Total_ports (Config_base + 4)
1200 #define Module_cnt (Config_base + 8)
1201 #define Module_no (Config_base + 10)
1202 #define Timer_10ms (Config_base + 14)
1203 #define Disable_IRQ (Config_base + 20)
1204 #define TMS320_PORT1 (Config_base + 22)
1205 #define TMS320_PORT2 (Config_base + 24)
1206 #define TMS320_CLOCK (Config_base + 26)
1209 * DATA BUFFER in DRAM
1211 #define Extern_table 0x400 /* Base address of the external table
1212 (24 words * 64) total 3K bytes
1213 (24 words * 128) total 6K bytes */
1214 #define Extern_size 0x60 /* 96 bytes */
1215 #define RXrptr 0x00 /* read pointer for RX buffer */
1216 #define RXwptr 0x02 /* write pointer for RX buffer */
1217 #define TXrptr 0x04 /* read pointer for TX buffer */
1218 #define TXwptr 0x06 /* write pointer for TX buffer */
1219 #define HostStat 0x08 /* IRQ flag and general flag */
1220 #define FlagStat 0x0A
1221 #define FlowControl 0x0C /* B7 B6 B5 B4 B3 B2 B1 B0 */
1222 /* x x x x | | | | */
1223 /* | | | + CTS flow */
1224 /* | | +--- RTS flow */
1225 /* | +------ TX Xon/Xoff */
1226 /* +--------- RX Xon/Xoff */
1227 #define Break_cnt 0x0E /* received break count */
1228 #define CD180TXirq 0x10 /* if non-0: enable TX irq */
1229 #define RX_mask 0x12
1230 #define TX_mask 0x14
1231 #define Ofs_rxb 0x16
1232 #define Ofs_txb 0x18
1233 #define Page_rxb 0x1A
1234 #define Page_txb 0x1C
1235 #define EndPage_rxb 0x1E
1236 #define EndPage_txb 0x20
1237 #define Data_error 0x22
1238 #define RxTrigger 0x28
1239 #define TxTrigger 0x2a
1241 #define rRXwptr 0x34
1242 #define Low_water 0x36
1244 #define FuncCode 0x40
1245 #define FuncArg 0x42
1246 #define FuncArg1 0x44
1248 #define C218rx_size 0x2000 /* 8K bytes */
1249 #define C218tx_size 0x8000 /* 32K bytes */
1251 #define C218rx_mask (C218rx_size - 1)
1252 #define C218tx_mask (C218tx_size - 1)
1254 #define C320p8rx_size 0x2000
1255 #define C320p8tx_size 0x8000
1256 #define C320p8rx_mask (C320p8rx_size - 1)
1257 #define C320p8tx_mask (C320p8tx_size - 1)
1259 #define C320p16rx_size 0x2000
1260 #define C320p16tx_size 0x4000
1261 #define C320p16rx_mask (C320p16rx_size - 1)
1262 #define C320p16tx_mask (C320p16tx_size - 1)
1264 #define C320p24rx_size 0x2000
1265 #define C320p24tx_size 0x2000
1266 #define C320p24rx_mask (C320p24rx_size - 1)
1267 #define C320p24tx_mask (C320p24tx_size - 1)
1269 #define C320p32rx_size 0x1000
1270 #define C320p32tx_size 0x1000
1271 #define C320p32rx_mask (C320p32rx_size - 1)
1272 #define C320p32tx_mask (C320p32tx_size - 1)
1274 #define Page_size 0x2000
1275 #define Page_mask (Page_size - 1)
1276 #define C218rx_spage 3
1277 #define C218tx_spage 4
1278 #define C218rx_pageno 1
1279 #define C218tx_pageno 4
1280 #define C218buf_pageno 5
1282 #define C320p8rx_spage 3
1283 #define C320p8tx_spage 4
1284 #define C320p8rx_pgno 1
1285 #define C320p8tx_pgno 4
1286 #define C320p8buf_pgno 5
1288 #define C320p16rx_spage 3
1289 #define C320p16tx_spage 4
1290 #define C320p16rx_pgno 1
1291 #define C320p16tx_pgno 2
1292 #define C320p16buf_pgno 3
1294 #define C320p24rx_spage 3
1295 #define C320p24tx_spage 4
1296 #define C320p24rx_pgno 1
1297 #define C320p24tx_pgno 1
1298 #define C320p24buf_pgno 2
1300 #define C320p32rx_spage 3
1301 #define C320p32tx_ofs C320p32rx_size
1302 #define C320p32tx_spage 3
1303 #define C320p32buf_pgno 1
1308 #define WakeupRx 0x01
1309 #define WakeupTx 0x02
1310 #define WakeupBreak 0x08
1311 #define WakeupLine 0x10
1312 #define WakeupIntr 0x20
1313 #define WakeupQuit 0x40
1314 #define WakeupEOF 0x80 /* used in VTIME control */
1315 #define WakeupRxTrigger 0x100
1316 #define WakeupTxTrigger 0x200
1320 #define Rx_over 0x01
1321 #define Xoff_state 0x02
1322 #define Tx_flowOff 0x04
1323 #define Tx_enable 0x08
1324 #define CTS_state 0x10
1325 #define DSR_state 0x20
1326 #define DCD_state 0x80
1330 #define CTS_FlowCtl 1
1331 #define RTS_FlowCtl 2
1332 #define Tx_FlowCtl 4
1333 #define Rx_FlowCtl 8
1334 #define IXM_IXANY 0x10
1336 #define LowWater 128
1344 /* mode definition */
1350 #define MX_STOP1 0x00
1351 #define MX_STOP15 0x04
1352 #define MX_STOP2 0x08
1354 #define MX_PARNONE 0x00
1355 #define MX_PAREVEN 0x40
1356 #define MX_PARODD 0xC0
1364 int rxcnt[MAX_PORTS];
1365 int txcnt[MAX_PORTS];
1368 #define DCD_changed 0x01
1369 #define DCD_oldstate 0x80
1371 static unsigned char moxaBuff[10240];
1372 static int moxaLowWaterChk;
1373 static int moxaCard;
1374 static struct mon_str moxaLog;
1375 static int moxaFuncTout = HZ / 2;
1377 static void moxafunc(void __iomem *, int, ushort);
1378 static void moxa_wait_finish(void __iomem *);
1379 static void moxa_low_water_check(void __iomem *);
1380 static int moxaloadbios(int, unsigned char __user *, int);
1381 static int moxafindcard(int);
1382 static int moxaload320b(int, unsigned char __user *, int);
1383 static int moxaloadcode(int, unsigned char __user *, int);
1384 static int moxaloadc218(int, void __iomem *, int);
1385 static int moxaloadc320(int, void __iomem *, int, int *);
1387 /*****************************************************************************
1388 * Driver level functions: *
1389 * 1. MoxaDriverInit(void); *
1390 * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
1391 * 3. MoxaDriverPoll(void); *
1392 *****************************************************************************/
1393 void MoxaDriverInit(void)
1395 struct moxa_port *p;
1398 moxaFuncTout = HZ / 2; /* 500 mini-seconds */
1401 moxaLowWaterChk = 0;
1402 for (i = 0; i < MAX_PORTS; i++) {
1407 moxaLog.rxcnt[i] = 0;
1408 moxaLog.txcnt[i] = 0;
1413 #define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1414 #define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
1415 #define MOXA_INIT_DRIVER (MOXA + 6) /* moxaCard=0 */
1416 #define MOXA_LOAD_BIOS (MOXA + 9) /* download BIOS */
1417 #define MOXA_FIND_BOARD (MOXA + 10) /* Check if MOXA card exist? */
1418 #define MOXA_LOAD_C320B (MOXA + 11) /* download 320B firmware */
1419 #define MOXA_LOAD_CODE (MOXA + 12) /* download firmware */
1420 #define MOXA_GETDATACOUNT (MOXA + 23)
1421 #define MOXA_GET_IOQUEUE (MOXA + 27)
1422 #define MOXA_FLUSH_QUEUE (MOXA + 28)
1423 #define MOXA_GET_CONF (MOXA + 35) /* configuration */
1424 #define MOXA_GET_MAJOR (MOXA + 63)
1425 #define MOXA_GET_CUMAJOR (MOXA + 64)
1426 #define MOXA_GETMSTATUS (MOXA + 65)
1434 static struct dl_str dltmp;
1436 void MoxaPortFlushData(int port, int mode)
1438 void __iomem *ofsAddr;
1439 if ((mode < 0) || (mode > 2))
1441 ofsAddr = moxa_ports[port].tableAddr;
1442 moxafunc(ofsAddr, FC_FlushQueue, mode);
1444 moxa_ports[port].lowChkFlag = 0;
1445 moxa_low_water_check(ofsAddr);
1449 int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port)
1453 int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
1454 void __user *argp = (void __user *)arg;
1456 if (port == MAX_PORTS) {
1457 if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_INIT_DRIVER) &&
1458 (cmd != MOXA_LOAD_BIOS) && (cmd != MOXA_FIND_BOARD) && (cmd != MOXA_LOAD_C320B) &&
1459 (cmd != MOXA_LOAD_CODE) && (cmd != MOXA_GETDATACOUNT) &&
1460 (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
1461 (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
1466 if(copy_to_user(argp, &moxa_boards, MAX_BOARDS *
1467 sizeof(struct moxa_board_conf)))
1470 case MOXA_INIT_DRIVER:
1471 if ((int) arg == 0x404)
1474 case MOXA_GETDATACOUNT:
1475 moxaLog.tick = jiffies;
1476 if(copy_to_user(argp, &moxaLog, sizeof(struct mon_str)))
1479 case MOXA_FLUSH_QUEUE:
1480 MoxaPortFlushData(port, arg);
1482 case MOXA_GET_IOQUEUE: {
1483 struct moxaq_str __user *argm = argp;
1484 struct moxaq_str tmp;
1486 for (i = 0; i < MAX_PORTS; i++, argm++) {
1487 memset(&tmp, 0, sizeof(tmp));
1488 if (moxa_ports[i].chkPort) {
1489 tmp.inq = MoxaPortRxQueue(i);
1490 tmp.outq = MoxaPortTxQueue(i);
1492 if (copy_to_user(argm, &tmp, sizeof(tmp)))
1496 } case MOXA_GET_OQUEUE:
1497 i = MoxaPortTxQueue(port);
1498 return put_user(i, (unsigned long __user *)argp);
1499 case MOXA_GET_IQUEUE:
1500 i = MoxaPortRxQueue(port);
1501 return put_user(i, (unsigned long __user *)argp);
1502 case MOXA_GET_MAJOR:
1503 if(copy_to_user(argp, &ttymajor, sizeof(int)))
1506 case MOXA_GET_CUMAJOR:
1508 if(copy_to_user(argp, &i, sizeof(int)))
1511 case MOXA_GETMSTATUS: {
1512 struct mxser_mstatus __user *argm = argp;
1513 struct mxser_mstatus tmp;
1514 struct moxa_port *p;
1516 for (i = 0; i < MAX_PORTS; i++, argm++) {
1518 memset(&tmp, 0, sizeof(tmp));
1522 status = MoxaPortLineStatus(p->port);
1531 if (!p->tty || !p->tty->termios)
1532 tmp.cflag = p->cflag;
1534 tmp.cflag = p->tty->termios->c_cflag;
1536 if (copy_to_user(argm, &tmp, sizeof(tmp)))
1541 return (-ENOIOCTLCMD);
1542 case MOXA_LOAD_BIOS:
1543 case MOXA_FIND_BOARD:
1544 case MOXA_LOAD_C320B:
1545 case MOXA_LOAD_CODE:
1546 if (!capable(CAP_SYS_RAWIO))
1551 if(copy_from_user(&dltmp, argp, sizeof(struct dl_str)))
1553 if(dltmp.cardno < 0 || dltmp.cardno >= MAX_BOARDS || dltmp.len < 0)
1558 case MOXA_LOAD_BIOS:
1559 i = moxaloadbios(dltmp.cardno, dltmp.buf, dltmp.len);
1561 case MOXA_FIND_BOARD:
1562 return moxafindcard(dltmp.cardno);
1563 case MOXA_LOAD_C320B:
1564 moxaload320b(dltmp.cardno, dltmp.buf, dltmp.len);
1565 default: /* to keep gcc happy */
1567 case MOXA_LOAD_CODE:
1568 i = moxaloadcode(dltmp.cardno, dltmp.buf, dltmp.len);
1576 int MoxaDriverPoll(void)
1578 struct moxa_board_conf *brd;
1579 register ushort temp;
1581 void __iomem *ofsAddr;
1587 for (card = 0; card < MAX_BOARDS; card++) {
1588 brd = &moxa_boards[card];
1589 if (brd->loadstat == 0)
1591 if ((ports = brd->numPorts) == 0)
1593 if (readb(brd->intPend) == 0xff) {
1594 ip = brd->intTable + readb(brd->intNdx);
1595 p = card * MAX_PORTS_PER_BOARD;
1597 for (port = 0; port < ports; port += 2, p++) {
1598 if ((temp = readw(ip + port)) != 0) {
1599 writew(0, ip + port);
1600 ofsAddr = moxa_ports[p].tableAddr;
1602 writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
1603 if (temp & IntrBreak) {
1604 moxa_ports[p].breakCnt++;
1606 if (temp & IntrLine) {
1607 if (readb(ofsAddr + FlagStat) & DCD_state) {
1608 if ((moxa_ports[p].DCDState & DCD_oldstate) == 0)
1609 moxa_ports[p].DCDState = (DCD_oldstate |
1612 if (moxa_ports[p].DCDState & DCD_oldstate)
1613 moxa_ports[p].DCDState = DCD_changed;
1618 writeb(0, brd->intPend);
1620 if (moxaLowWaterChk) {
1621 p = card * MAX_PORTS_PER_BOARD;
1622 for (port = 0; port < ports; port++, p++) {
1623 if (moxa_ports[p].lowChkFlag) {
1624 moxa_ports[p].lowChkFlag = 0;
1625 ofsAddr = moxa_ports[p].tableAddr;
1626 moxa_low_water_check(ofsAddr);
1631 moxaLowWaterChk = 0;
1635 /*****************************************************************************
1636 * Card level function: *
1637 * 1. MoxaPortsOfCard(int cardno); *
1638 *****************************************************************************/
1639 int MoxaPortsOfCard(int cardno)
1642 if (moxa_boards[cardno].boardType == 0)
1644 return (moxa_boards[cardno].numPorts);
1647 /*****************************************************************************
1648 * Port level functions: *
1649 * 1. MoxaPortIsValid(int port); *
1650 * 2. MoxaPortEnable(int port); *
1651 * 3. MoxaPortDisable(int port); *
1652 * 4. MoxaPortGetMaxBaud(int port); *
1653 * 6. MoxaPortSetBaud(int port, long baud); *
1654 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1655 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1656 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1657 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1658 * 12. MoxaPortLineStatus(int port); *
1659 * 13. MoxaPortDCDChange(int port); *
1660 * 14. MoxaPortDCDON(int port); *
1661 * 15. MoxaPortFlushData(int port, int mode); *
1662 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1663 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
1664 * 20. MoxaPortTxQueue(int port); *
1665 * 21. MoxaPortTxFree(int port); *
1666 * 22. MoxaPortRxQueue(int port); *
1667 * 24. MoxaPortTxDisable(int port); *
1668 * 25. MoxaPortTxEnable(int port); *
1669 * 27. MoxaPortResetBrkCnt(int port); *
1670 * 30. MoxaPortSendBreak(int port, int ticks); *
1671 *****************************************************************************/
1673 * Moxa Port Number Description:
1675 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1676 * the port number using in MOXA driver functions will be 0 to 31 for
1677 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1678 * to 127 for fourth. For example, if you setup three MOXA boards,
1679 * first board is C218, second board is C320-16 and third board is
1680 * C320-32. The port number of first board (C218 - 8 ports) is from
1681 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1682 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1683 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1684 * 127 will be invalid.
1687 * Moxa Functions Description:
1689 * Function 1: Driver initialization routine, this routine must be
1690 * called when initialized driver.
1692 * void MoxaDriverInit();
1695 * Function 2: Moxa driver private IOCTL command processing.
1697 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1699 * unsigned int cmd : IOCTL command
1700 * unsigned long arg : IOCTL argument
1701 * int port : port number (0 - 127)
1708 * Function 3: Moxa driver polling process routine.
1710 * int MoxaDriverPoll(void);
1712 * return: 0 ; polling O.K.
1713 * -1 : no any Moxa card.
1716 * Function 4: Get the ports of this card.
1718 * int MoxaPortsOfCard(int cardno);
1720 * int cardno : card number (0 - 3)
1722 * return: 0 : this card is invalid
1726 * Function 5: Check this port is valid or invalid
1728 * int MoxaPortIsValid(int port);
1729 * int port : port number (0 - 127, ref port description)
1731 * return: 0 : this port is invalid
1732 * 1 : this port is valid
1735 * Function 6: Enable this port to start Tx/Rx data.
1737 * void MoxaPortEnable(int port);
1738 * int port : port number (0 - 127)
1741 * Function 7: Disable this port
1743 * void MoxaPortDisable(int port);
1744 * int port : port number (0 - 127)
1747 * Function 8: Get the maximun available baud rate of this port.
1749 * long MoxaPortGetMaxBaud(int port);
1750 * int port : port number (0 - 127)
1752 * return: 0 : this port is invalid
1753 * 38400/57600/115200 bps
1756 * Function 10: Setting baud rate of this port.
1758 * long MoxaPortSetBaud(int port, long baud);
1759 * int port : port number (0 - 127)
1760 * long baud : baud rate (50 - 115200)
1762 * return: 0 : this port is invalid or baud < 50
1763 * 50 - 115200 : the real baud rate set to the port, if
1764 * the argument baud is large than maximun
1765 * available baud rate, the real setting
1766 * baud rate will be the maximun baud rate.
1769 * Function 12: Configure the port.
1771 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1772 * int port : port number (0 - 127)
1773 * struct ktermios * termio : termio structure pointer
1774 * speed_t baud : baud rate
1776 * return: -1 : this port is invalid or termio == NULL
1780 * Function 13: Get the DTR/RTS state of this port.
1782 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1783 * int port : port number (0 - 127)
1784 * int * dtrState : pointer to INT to receive the current DTR
1785 * state. (if NULL, this function will not
1786 * write to this address)
1787 * int * rtsState : pointer to INT to receive the current RTS
1788 * state. (if NULL, this function will not
1789 * write to this address)
1791 * return: -1 : this port is invalid
1795 * Function 14: Setting the DTR/RTS output state of this port.
1797 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1798 * int port : port number (0 - 127)
1799 * int dtrState : DTR output state (0: off, 1: on)
1800 * int rtsState : RTS output state (0: off, 1: on)
1803 * Function 15: Setting the flow control of this port.
1805 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1806 * int txFlow,int xany);
1807 * int port : port number (0 - 127)
1808 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1809 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1810 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1811 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1812 * int xany : S/W XANY flow control (0: no, 1: yes)
1815 * Function 16: Get ths line status of this port
1817 * int MoxaPortLineStatus(int port);
1818 * int port : port number (0 - 127)
1820 * return: Bit 0 - CTS state (0: off, 1: on)
1821 * Bit 1 - DSR state (0: off, 1: on)
1822 * Bit 2 - DCD state (0: off, 1: on)
1825 * Function 17: Check the DCD state has changed since the last read
1828 * int MoxaPortDCDChange(int port);
1829 * int port : port number (0 - 127)
1831 * return: 0 : no changed
1832 * 1 : DCD has changed
1835 * Function 18: Check ths current DCD state is ON or not.
1837 * int MoxaPortDCDON(int port);
1838 * int port : port number (0 - 127)
1840 * return: 0 : DCD off
1844 * Function 19: Flush the Rx/Tx buffer data of this port.
1846 * void MoxaPortFlushData(int port, int mode);
1847 * int port : port number (0 - 127)
1849 * 0 : flush the Rx buffer
1850 * 1 : flush the Tx buffer
1851 * 2 : flush the Rx and Tx buffer
1854 * Function 20: Write data.
1856 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1857 * int port : port number (0 - 127)
1858 * unsigned char * buffer : pointer to write data buffer.
1859 * int length : write data length
1861 * return: 0 - length : real write data length
1864 * Function 21: Read data.
1866 * int MoxaPortReadData(int port, struct tty_struct *tty);
1867 * int port : port number (0 - 127)
1868 * struct tty_struct *tty : tty for data
1870 * return: 0 - length : real read data length
1873 * Function 24: Get the Tx buffer current queued data bytes
1875 * int MoxaPortTxQueue(int port);
1876 * int port : port number (0 - 127)
1878 * return: .. : Tx buffer current queued data bytes
1881 * Function 25: Get the Tx buffer current free space
1883 * int MoxaPortTxFree(int port);
1884 * int port : port number (0 - 127)
1886 * return: .. : Tx buffer current free space
1889 * Function 26: Get the Rx buffer current queued data bytes
1891 * int MoxaPortRxQueue(int port);
1892 * int port : port number (0 - 127)
1894 * return: .. : Rx buffer current queued data bytes
1897 * Function 28: Disable port data transmission.
1899 * void MoxaPortTxDisable(int port);
1900 * int port : port number (0 - 127)
1903 * Function 29: Enable port data transmission.
1905 * void MoxaPortTxEnable(int port);
1906 * int port : port number (0 - 127)
1909 * Function 31: Get the received BREAK signal count and reset it.
1911 * int MoxaPortResetBrkCnt(int port);
1912 * int port : port number (0 - 127)
1914 * return: 0 - .. : BREAK signal count
1917 * Function 34: Send out a BREAK signal.
1919 * void MoxaPortSendBreak(int port, int ms100);
1920 * int port : port number (0 - 127)
1921 * int ms100 : break signal time interval.
1922 * unit: 100 mini-second. if ms100 == 0, it will
1923 * send out a about 250 ms BREAK signal.
1926 int MoxaPortIsValid(int port)
1931 if (moxa_ports[port].chkPort == 0)
1936 void MoxaPortEnable(int port)
1938 void __iomem *ofsAddr;
1939 int MoxaPortLineStatus(int);
1940 short lowwater = 512;
1942 ofsAddr = moxa_ports[port].tableAddr;
1943 writew(lowwater, ofsAddr + Low_water);
1944 moxa_ports[port].breakCnt = 0;
1945 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
1946 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
1947 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1949 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
1952 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1953 moxafunc(ofsAddr, FC_FlushQueue, 2);
1955 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1956 MoxaPortLineStatus(port);
1959 void MoxaPortDisable(int port)
1961 void __iomem *ofsAddr = moxa_ports[port].tableAddr;
1963 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1964 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1965 writew(0, ofsAddr + HostStat);
1966 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1969 long MoxaPortGetMaxBaud(int port)
1971 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
1972 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI))
1979 long MoxaPortSetBaud(int port, long baud)
1981 void __iomem *ofsAddr;
1985 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
1987 ofsAddr = moxa_ports[port].tableAddr;
1991 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
1992 else if (max == 57600L)
1993 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
1995 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
1997 moxafunc(ofsAddr, FC_SetBaud, val);
1999 moxa_ports[port].curBaud = baud;
2003 int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud)
2005 void __iomem *ofsAddr;
2009 if (moxa_ports[port].chkPort == 0 || termio == 0)
2011 ofsAddr = moxa_ports[port].tableAddr;
2012 cflag = termio->c_cflag; /* termio->c_cflag */
2014 mode = termio->c_cflag & CSIZE;
2017 else if (mode == CS6)
2019 else if (mode == CS7)
2021 else if (mode == CS8)
2024 if (termio->c_cflag & CSTOPB) {
2032 if (termio->c_cflag & PARENB) {
2033 if (termio->c_cflag & PARODD)
2040 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
2042 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2043 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2044 if (baud >= 921600L)
2047 baud = MoxaPortSetBaud(port, baud);
2049 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
2050 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
2051 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
2052 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
2053 moxa_wait_finish(ofsAddr);
2059 int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState)
2062 if (!MoxaPortIsValid(port))
2065 if (moxa_ports[port].lineCtrl & DTR_ON)
2071 if (moxa_ports[port].lineCtrl & RTS_ON)
2079 void MoxaPortLineCtrl(int port, int dtr, int rts)
2081 void __iomem *ofsAddr;
2084 ofsAddr = moxa_ports[port].tableAddr;
2090 moxa_ports[port].lineCtrl = mode;
2091 moxafunc(ofsAddr, FC_LineControl, mode);
2094 void MoxaPortFlowCtrl(int port, int rts, int cts, int txflow, int rxflow, int txany)
2096 void __iomem *ofsAddr;
2099 ofsAddr = moxa_ports[port].tableAddr;
2102 mode |= RTS_FlowCtl;
2104 mode |= CTS_FlowCtl;
2111 moxafunc(ofsAddr, FC_SetFlowCtl, mode);
2114 int MoxaPortLineStatus(int port)
2116 void __iomem *ofsAddr;
2119 ofsAddr = moxa_ports[port].tableAddr;
2120 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2121 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2122 moxafunc(ofsAddr, FC_LineStatus, 0);
2123 val = readw(ofsAddr + FuncArg);
2125 val = readw(ofsAddr + FlagStat) >> 4;
2130 if ((moxa_ports[port].DCDState & DCD_oldstate) == 0)
2131 moxa_ports[port].DCDState = (DCD_oldstate | DCD_changed);
2133 if (moxa_ports[port].DCDState & DCD_oldstate)
2134 moxa_ports[port].DCDState = DCD_changed;
2140 int MoxaPortDCDChange(int port)
2144 if (moxa_ports[port].chkPort == 0)
2146 n = moxa_ports[port].DCDState;
2147 moxa_ports[port].DCDState &= ~DCD_changed;
2152 int MoxaPortDCDON(int port)
2156 if (moxa_ports[port].chkPort == 0)
2158 if (moxa_ports[port].DCDState & DCD_oldstate)
2165 int MoxaPortWriteData(int port, unsigned char * buffer, int len)
2170 ushort head, tx_mask, spage, epage;
2171 ushort pageno, pageofs, bufhead;
2172 void __iomem *baseAddr, *ofsAddr, *ofs;
2174 ofsAddr = moxa_ports[port].tableAddr;
2175 baseAddr = moxa_boards[port / MAX_PORTS_PER_BOARD].basemem;
2176 tx_mask = readw(ofsAddr + TX_mask);
2177 spage = readw(ofsAddr + Page_txb);
2178 epage = readw(ofsAddr + EndPage_txb);
2179 tail = readw(ofsAddr + TXwptr);
2180 head = readw(ofsAddr + TXrptr);
2181 c = (head > tail) ? (head - tail - 1)
2182 : (head - tail + tx_mask);
2185 moxaLog.txcnt[port] += c;
2187 if (spage == epage) {
2188 bufhead = readw(ofsAddr + Ofs_txb);
2189 writew(spage, baseAddr + Control_reg);
2192 len = head - tail - 1;
2194 len = tx_mask + 1 - tail;
2195 len = (c > len) ? len : c;
2196 ofs = baseAddr + DynPage_addr + bufhead + tail;
2197 for (i = 0; i < len; i++)
2198 writeb(*buffer++, ofs + i);
2199 tail = (tail + len) & tx_mask;
2202 writew(tail, ofsAddr + TXwptr);
2205 pageno = spage + (tail >> 13);
2206 pageofs = tail & Page_mask;
2208 cnt = Page_size - pageofs;
2212 writeb(pageno, baseAddr + Control_reg);
2213 ofs = baseAddr + DynPage_addr + pageofs;
2214 for (i = 0; i < cnt; i++)
2215 writeb(*buffer++, ofs + i);
2217 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2220 if (++pageno == epage)
2225 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2229 int MoxaPortReadData(int port, struct tty_struct *tty)
2231 register ushort head, pageofs;
2232 int i, count, cnt, len, total, remain;
2233 ushort tail, rx_mask, spage, epage;
2234 ushort pageno, bufhead;
2235 void __iomem *baseAddr, *ofsAddr, *ofs;
2237 ofsAddr = moxa_ports[port].tableAddr;
2238 baseAddr = moxa_boards[port / MAX_PORTS_PER_BOARD].basemem;
2239 head = readw(ofsAddr + RXrptr);
2240 tail = readw(ofsAddr + RXwptr);
2241 rx_mask = readw(ofsAddr + RX_mask);
2242 spage = readw(ofsAddr + Page_rxb);
2243 epage = readw(ofsAddr + EndPage_rxb);
2244 count = (tail >= head) ? (tail - head)
2245 : (tail - head + rx_mask + 1);
2250 remain = count - total;
2251 moxaLog.rxcnt[port] += total;
2253 if (spage == epage) {
2254 bufhead = readw(ofsAddr + Ofs_rxb);
2255 writew(spage, baseAddr + Control_reg);
2260 len = rx_mask + 1 - head;
2261 len = (count > len) ? len : count;
2262 ofs = baseAddr + DynPage_addr + bufhead + head;
2263 for (i = 0; i < len; i++)
2264 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2265 head = (head + len) & rx_mask;
2268 writew(head, ofsAddr + RXrptr);
2271 pageno = spage + (head >> 13);
2272 pageofs = head & Page_mask;
2274 cnt = Page_size - pageofs;
2278 writew(pageno, baseAddr + Control_reg);
2279 ofs = baseAddr + DynPage_addr + pageofs;
2280 for (i = 0; i < cnt; i++)
2281 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2283 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2286 if (++pageno == epage)
2291 if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2292 moxaLowWaterChk = 1;
2293 moxa_ports[port].lowChkFlag = 1;
2299 int MoxaPortTxQueue(int port)
2301 void __iomem *ofsAddr;
2302 ushort rptr, wptr, mask;
2305 ofsAddr = moxa_ports[port].tableAddr;
2306 rptr = readw(ofsAddr + TXrptr);
2307 wptr = readw(ofsAddr + TXwptr);
2308 mask = readw(ofsAddr + TX_mask);
2309 len = (wptr - rptr) & mask;
2313 int MoxaPortTxFree(int port)
2315 void __iomem *ofsAddr;
2316 ushort rptr, wptr, mask;
2319 ofsAddr = moxa_ports[port].tableAddr;
2320 rptr = readw(ofsAddr + TXrptr);
2321 wptr = readw(ofsAddr + TXwptr);
2322 mask = readw(ofsAddr + TX_mask);
2323 len = mask - ((wptr - rptr) & mask);
2327 int MoxaPortRxQueue(int port)
2329 void __iomem *ofsAddr;
2330 ushort rptr, wptr, mask;
2333 ofsAddr = moxa_ports[port].tableAddr;
2334 rptr = readw(ofsAddr + RXrptr);
2335 wptr = readw(ofsAddr + RXwptr);
2336 mask = readw(ofsAddr + RX_mask);
2337 len = (wptr - rptr) & mask;
2342 void MoxaPortTxDisable(int port)
2344 void __iomem *ofsAddr;
2346 ofsAddr = moxa_ports[port].tableAddr;
2347 moxafunc(ofsAddr, FC_SetXoffState, Magic_code);
2350 void MoxaPortTxEnable(int port)
2352 void __iomem *ofsAddr;
2354 ofsAddr = moxa_ports[port].tableAddr;
2355 moxafunc(ofsAddr, FC_SetXonState, Magic_code);
2359 int MoxaPortResetBrkCnt(int port)
2362 cnt = moxa_ports[port].breakCnt;
2363 moxa_ports[port].breakCnt = 0;
2368 void MoxaPortSendBreak(int port, int ms100)
2370 void __iomem *ofsAddr;
2372 ofsAddr = moxa_ports[port].tableAddr;
2374 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2377 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2380 moxafunc(ofsAddr, FC_StopBreak, Magic_code);
2383 static int moxa_get_serial_info(struct moxa_port *info,
2384 struct serial_struct __user *retinfo)
2386 struct serial_struct tmp;
2388 memset(&tmp, 0, sizeof(tmp));
2389 tmp.type = info->type;
2390 tmp.line = info->port;
2393 tmp.flags = info->asyncflags;
2394 tmp.baud_base = 921600;
2395 tmp.close_delay = info->close_delay;
2396 tmp.closing_wait = info->closing_wait;
2397 tmp.custom_divisor = 0;
2399 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2405 static int moxa_set_serial_info(struct moxa_port *info,
2406 struct serial_struct __user *new_info)
2408 struct serial_struct new_serial;
2410 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2413 if ((new_serial.irq != 0) ||
2414 (new_serial.port != 0) ||
2415 // (new_serial.type != info->type) ||
2416 (new_serial.custom_divisor != 0) ||
2417 (new_serial.baud_base != 921600))
2420 if (!capable(CAP_SYS_ADMIN)) {
2421 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2422 (info->asyncflags & ~ASYNC_USR_MASK)))
2425 info->close_delay = new_serial.close_delay * HZ / 100;
2426 info->closing_wait = new_serial.closing_wait * HZ / 100;
2429 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2430 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2432 if (new_serial.type == PORT_16550A) {
2433 MoxaSetFifo(info->port, 1);
2435 MoxaSetFifo(info->port, 0);
2438 info->type = new_serial.type;
2444 /*****************************************************************************
2445 * Static local functions: *
2446 *****************************************************************************/
2447 static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
2450 writew(arg, ofsAddr + FuncArg);
2451 writew(cmd, ofsAddr + FuncCode);
2452 moxa_wait_finish(ofsAddr);
2455 static void moxa_wait_finish(void __iomem *ofsAddr)
2460 while (readw(ofsAddr + FuncCode) != 0) {
2462 if ((j - i) > moxaFuncTout) {
2468 static void moxa_low_water_check(void __iomem *ofsAddr)
2471 ushort rptr, wptr, mask;
2473 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2474 rptr = readw(ofsAddr + RXrptr);
2475 wptr = readw(ofsAddr + RXwptr);
2476 mask = readw(ofsAddr + RX_mask);
2477 len = (wptr - rptr) & mask;
2478 if (len <= Low_water)
2479 moxafunc(ofsAddr, FC_SendXon, 0);
2483 static int moxaloadbios(int cardno, unsigned char __user *tmp, int len)
2485 void __iomem *baseAddr;
2488 if(len < 0 || len > sizeof(moxaBuff))
2490 if(copy_from_user(moxaBuff, tmp, len))
2492 baseAddr = moxa_boards[cardno].basemem;
2493 writeb(HW_reset, baseAddr + Control_reg); /* reset */
2495 for (i = 0; i < 4096; i++)
2496 writeb(0, baseAddr + i); /* clear fix page */
2497 for (i = 0; i < len; i++)
2498 writeb(moxaBuff[i], baseAddr + i); /* download BIOS */
2499 writeb(0, baseAddr + Control_reg); /* restart */
2503 static int moxafindcard(int cardno)
2505 void __iomem *baseAddr;
2508 baseAddr = moxa_boards[cardno].basemem;
2509 switch (moxa_boards[cardno].boardType) {
2510 case MOXA_BOARD_C218_ISA:
2511 case MOXA_BOARD_C218_PCI:
2512 if ((tmp = readw(baseAddr + C218_key)) != C218_KeyCode) {
2516 case MOXA_BOARD_CP204J:
2517 if ((tmp = readw(baseAddr + C218_key)) != CP204J_KeyCode) {
2522 if ((tmp = readw(baseAddr + C320_key)) != C320_KeyCode) {
2525 if ((tmp = readw(baseAddr + C320_status)) != STS_init) {
2532 static int moxaload320b(int cardno, unsigned char __user *tmp, int len)
2534 void __iomem *baseAddr;
2537 if(len < 0 || len > sizeof(moxaBuff))
2539 if(copy_from_user(moxaBuff, tmp, len))
2541 baseAddr = moxa_boards[cardno].basemem;
2542 writew(len - 7168 - 2, baseAddr + C320bapi_len);
2543 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
2544 for (i = 0; i < 7168; i++)
2545 writeb(moxaBuff[i], baseAddr + DynPage_addr + i);
2546 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
2547 for (i = 0; i < (len - 7168); i++)
2548 writeb(moxaBuff[i + 7168], baseAddr + DynPage_addr + i);
2552 static int moxaloadcode(int cardno, unsigned char __user *tmp, int len)
2554 void __iomem *baseAddr, *ofsAddr;
2555 int retval, port, i;
2557 if(len < 0 || len > sizeof(moxaBuff))
2559 if(copy_from_user(moxaBuff, tmp, len))
2561 baseAddr = moxa_boards[cardno].basemem;
2562 switch (moxa_boards[cardno].boardType) {
2563 case MOXA_BOARD_C218_ISA:
2564 case MOXA_BOARD_C218_PCI:
2565 case MOXA_BOARD_CP204J:
2566 retval = moxaloadc218(cardno, baseAddr, len);
2569 port = cardno * MAX_PORTS_PER_BOARD;
2570 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2571 struct moxa_port *p = &moxa_ports[port];
2576 p->tableAddr = baseAddr + Extern_table + Extern_size * i;
2577 ofsAddr = p->tableAddr;
2578 writew(C218rx_mask, ofsAddr + RX_mask);
2579 writew(C218tx_mask, ofsAddr + TX_mask);
2580 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
2581 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
2583 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
2584 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
2589 retval = moxaloadc320(cardno, baseAddr, len,
2590 &moxa_boards[cardno].numPorts);
2593 port = cardno * MAX_PORTS_PER_BOARD;
2594 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2595 struct moxa_port *p = &moxa_ports[port];
2600 p->tableAddr = baseAddr + Extern_table + Extern_size * i;
2601 ofsAddr = p->tableAddr;
2602 if (moxa_boards[cardno].numPorts == 8) {
2603 writew(C320p8rx_mask, ofsAddr + RX_mask);
2604 writew(C320p8tx_mask, ofsAddr + TX_mask);
2605 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
2606 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
2607 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
2608 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
2610 } else if (moxa_boards[cardno].numPorts == 16) {
2611 writew(C320p16rx_mask, ofsAddr + RX_mask);
2612 writew(C320p16tx_mask, ofsAddr + TX_mask);
2613 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
2614 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
2615 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
2616 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
2618 } else if (moxa_boards[cardno].numPorts == 24) {
2619 writew(C320p24rx_mask, ofsAddr + RX_mask);
2620 writew(C320p24tx_mask, ofsAddr + TX_mask);
2621 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
2622 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
2623 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
2624 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2625 } else if (moxa_boards[cardno].numPorts == 32) {
2626 writew(C320p32rx_mask, ofsAddr + RX_mask);
2627 writew(C320p32tx_mask, ofsAddr + TX_mask);
2628 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
2629 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
2630 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
2631 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
2632 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2637 moxa_boards[cardno].loadstat = 1;
2641 static int moxaloadc218(int cardno, void __iomem *baseAddr, int len)
2644 int i, j, len1, len2;
2645 ushort usum, *ptr, keycode;
2647 if (moxa_boards[cardno].boardType == MOXA_BOARD_CP204J)
2648 keycode = CP204J_KeyCode;
2650 keycode = C218_KeyCode;
2653 ptr = (ushort *) moxaBuff;
2654 for (i = 0; i < len1; i++)
2655 usum += le16_to_cpu(*(ptr + i));
2661 len2 = (len1 > 2048) ? 2048 : len1;
2663 for (i = 0; i < len2 << 1; i++)
2664 writeb(moxaBuff[i + j], baseAddr + C218_LoadBuf + i);
2667 writew(len2, baseAddr + C218DLoad_len);
2668 writew(0, baseAddr + C218_key);
2669 for (i = 0; i < 100; i++) {
2670 if (readw(baseAddr + C218_key) == keycode)
2674 if (readw(baseAddr + C218_key) != keycode) {
2678 writew(0, baseAddr + C218DLoad_len);
2679 writew(usum, baseAddr + C218check_sum);
2680 writew(0, baseAddr + C218_key);
2681 for (i = 0; i < 100; i++) {
2682 if (readw(baseAddr + C218_key) == keycode)
2687 } while ((readb(baseAddr + C218chksum_ok) != 1) && (retry < 3));
2688 if (readb(baseAddr + C218chksum_ok) != 1) {
2691 writew(0, baseAddr + C218_key);
2692 for (i = 0; i < 100; i++) {
2693 if (readw(baseAddr + Magic_no) == Magic_code)
2697 if (readw(baseAddr + Magic_no) != Magic_code) {
2700 writew(1, baseAddr + Disable_IRQ);
2701 writew(0, baseAddr + Magic_no);
2702 for (i = 0; i < 100; i++) {
2703 if (readw(baseAddr + Magic_no) == Magic_code)
2707 if (readw(baseAddr + Magic_no) != Magic_code) {
2711 moxa_boards[cardno].intNdx = baseAddr + IRQindex;
2712 moxa_boards[cardno].intPend = baseAddr + IRQpending;
2713 moxa_boards[cardno].intTable = baseAddr + IRQtable;
2717 static int moxaloadc320(int cardno, void __iomem *baseAddr, int len, int *numPorts)
2720 int i, j, wlen, len2, retry;
2725 uptr = (ushort *) moxaBuff;
2726 for (i = 0; i < wlen; i++)
2727 usum += le16_to_cpu(uptr[i]);
2738 for (i = 0; i < len2; i++)
2739 writeb(moxaBuff[j + i], baseAddr + C320_LoadBuf + i);
2742 writew(len2, baseAddr + C320DLoad_len);
2743 writew(0, baseAddr + C320_key);
2744 for (i = 0; i < 10; i++) {
2745 if (readw(baseAddr + C320_key) == C320_KeyCode)
2749 if (readw(baseAddr + C320_key) != C320_KeyCode)
2752 writew(0, baseAddr + C320DLoad_len);
2753 writew(usum, baseAddr + C320check_sum);
2754 writew(0, baseAddr + C320_key);
2755 for (i = 0; i < 10; i++) {
2756 if (readw(baseAddr + C320_key) == C320_KeyCode)
2761 } while ((readb(baseAddr + C320chksum_ok) != 1) && (retry < 3));
2762 if (readb(baseAddr + C320chksum_ok) != 1)
2764 writew(0, baseAddr + C320_key);
2765 for (i = 0; i < 600; i++) {
2766 if (readw(baseAddr + Magic_no) == Magic_code)
2770 if (readw(baseAddr + Magic_no) != Magic_code)
2773 if (moxa_boards[cardno].busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
2774 writew(0x3800, baseAddr + TMS320_PORT1);
2775 writew(0x3900, baseAddr + TMS320_PORT2);
2776 writew(28499, baseAddr + TMS320_CLOCK);
2778 writew(0x3200, baseAddr + TMS320_PORT1);
2779 writew(0x3400, baseAddr + TMS320_PORT2);
2780 writew(19999, baseAddr + TMS320_CLOCK);
2782 writew(1, baseAddr + Disable_IRQ);
2783 writew(0, baseAddr + Magic_no);
2784 for (i = 0; i < 500; i++) {
2785 if (readw(baseAddr + Magic_no) == Magic_code)
2789 if (readw(baseAddr + Magic_no) != Magic_code)
2792 j = readw(baseAddr + Module_cnt);
2796 writew(j, baseAddr + Module_no);
2797 writew(0, baseAddr + Magic_no);
2798 for (i = 0; i < 600; i++) {
2799 if (readw(baseAddr + Magic_no) == Magic_code)
2803 if (readw(baseAddr + Magic_no) != Magic_code)
2806 moxa_boards[cardno].intNdx = baseAddr + IRQindex;
2807 moxa_boards[cardno].intPend = baseAddr + IRQpending;
2808 moxa_boards[cardno].intTable = baseAddr + IRQtable;
2812 static void MoxaSetFifo(int port, int enable)
2814 void __iomem *ofsAddr = moxa_ports[port].tableAddr;
2817 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2818 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2820 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2821 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);