1 /*****************************************************************************/
3 * moxa.c -- MOXA Intellio family multiport serial driver.
5 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com.tw).
7 * This code is loosely based on the Linux serial driver, written by
8 * Linus Torvalds, Theodore T'so and others.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
17 * MOXA Intellio Series Driver
23 #include <linux/module.h>
24 #include <linux/types.h>
26 #include <linux/ioport.h>
27 #include <linux/errno.h>
28 #include <linux/signal.h>
29 #include <linux/sched.h>
30 #include <linux/timer.h>
31 #include <linux/interrupt.h>
32 #include <linux/tty.h>
33 #include <linux/tty_flip.h>
34 #include <linux/major.h>
35 #include <linux/string.h>
36 #include <linux/fcntl.h>
37 #include <linux/ptrace.h>
38 #include <linux/serial.h>
39 #include <linux/tty_driver.h>
40 #include <linux/delay.h>
41 #include <linux/pci.h>
42 #include <linux/init.h>
43 #include <linux/bitops.h>
45 #include <asm/system.h>
47 #include <asm/uaccess.h>
49 #define MOXA_VERSION "5.1k"
52 #define MOXACUMAJOR 173
54 #define MAX_BOARDS 4 /* Don't change this value */
55 #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
56 #define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
59 * Define the Moxa PCI vendor and device IDs.
61 #define MOXA_BUS_TYPE_ISA 0
62 #define MOXA_BUS_TYPE_PCI 1
65 MOXA_BOARD_C218_PCI = 1,
72 static char *moxa_brdname[] =
74 "C218 Turbo PCI series",
75 "C218 Turbo ISA series",
76 "C320 Turbo PCI series",
77 "C320 Turbo ISA series",
82 static struct pci_device_id moxa_pcibrds[] = {
83 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
84 .driver_data = MOXA_BOARD_C218_PCI },
85 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
86 .driver_data = MOXA_BOARD_C320_PCI },
87 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
88 .driver_data = MOXA_BOARD_CP204J },
91 MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
92 #endif /* CONFIG_PCI */
94 struct moxa_isa_board_conf {
97 unsigned long baseAddr;
100 static struct moxa_isa_board_conf moxa_isa_boards[] =
102 /* {MOXA_BOARD_C218_ISA,8,0xDC000}, */
105 static struct moxa_board_conf {
108 unsigned long baseAddr;
110 struct pci_dev *pdev;
114 void __iomem *basemem;
115 void __iomem *intNdx;
116 void __iomem *intPend;
117 void __iomem *intTable;
118 } moxa_boards[MAX_BOARDS];
120 struct mxser_mstatus {
137 unsigned short closing_wait;
140 long event; /* long req'd for set_bit --RR */
142 unsigned long statusflags;
143 struct tty_struct *tty;
145 wait_queue_head_t open_wait;
146 wait_queue_head_t close_wait;
148 struct timer_list emptyTimer;
152 void __iomem *tableAddr;
161 #define TXSTOPPED 0x1
163 #define EMPTYWAIT 0x4
166 #define SERIAL_DO_RESTART
168 #define WAKEUP_CHARS 256
170 static int verbose = 0;
171 static int ttymajor = MOXAMAJOR;
172 /* Variables for insmod */
174 static int baseaddr[4];
176 static int numports[4];
179 MODULE_AUTHOR("William Chen");
180 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
181 MODULE_LICENSE("GPL");
183 module_param_array(type, int, NULL, 0);
184 module_param_array(baseaddr, int, NULL, 0);
185 module_param_array(numports, int, NULL, 0);
187 module_param(ttymajor, int, 0);
188 module_param(verbose, bool, 0644);
193 static int moxa_open(struct tty_struct *, struct file *);
194 static void moxa_close(struct tty_struct *, struct file *);
195 static int moxa_write(struct tty_struct *, const unsigned char *, int);
196 static int moxa_write_room(struct tty_struct *);
197 static void moxa_flush_buffer(struct tty_struct *);
198 static int moxa_chars_in_buffer(struct tty_struct *);
199 static void moxa_flush_chars(struct tty_struct *);
200 static void moxa_put_char(struct tty_struct *, unsigned char);
201 static int moxa_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);
202 static void moxa_throttle(struct tty_struct *);
203 static void moxa_unthrottle(struct tty_struct *);
204 static void moxa_set_termios(struct tty_struct *, struct ktermios *);
205 static void moxa_stop(struct tty_struct *);
206 static void moxa_start(struct tty_struct *);
207 static void moxa_hangup(struct tty_struct *);
208 static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
209 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
210 unsigned int set, unsigned int clear);
211 static void moxa_poll(unsigned long);
212 static void set_tty_param(struct tty_struct *);
213 static int block_till_ready(struct tty_struct *, struct file *,
215 static void setup_empty_event(struct tty_struct *);
216 static void check_xmit_empty(unsigned long);
217 static void shut_down(struct moxa_port *);
218 static void receive_data(struct moxa_port *);
220 * moxa board interface functions:
222 static void MoxaDriverInit(void);
223 static int MoxaDriverIoctl(unsigned int, unsigned long, int);
224 static int MoxaDriverPoll(void);
225 static int MoxaPortsOfCard(int);
226 static int MoxaPortIsValid(int);
227 static void MoxaPortEnable(int);
228 static void MoxaPortDisable(int);
229 static long MoxaPortGetMaxBaud(int);
230 static long MoxaPortSetBaud(int, long);
231 static int MoxaPortSetTermio(int, struct ktermios *, speed_t);
232 static int MoxaPortGetLineOut(int, int *, int *);
233 static void MoxaPortLineCtrl(int, int, int);
234 static void MoxaPortFlowCtrl(int, int, int, int, int, int);
235 static int MoxaPortLineStatus(int);
236 static int MoxaPortDCDChange(int);
237 static int MoxaPortDCDON(int);
238 static void MoxaPortFlushData(int, int);
239 static int MoxaPortWriteData(int, unsigned char *, int);
240 static int MoxaPortReadData(int, struct tty_struct *tty);
241 static int MoxaPortTxQueue(int);
242 static int MoxaPortRxQueue(int);
243 static int MoxaPortTxFree(int);
244 static void MoxaPortTxDisable(int);
245 static void MoxaPortTxEnable(int);
246 static int MoxaPortResetBrkCnt(int);
247 static void MoxaPortSendBreak(int, int);
248 static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
249 static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
250 static void MoxaSetFifo(int port, int enable);
252 static const struct tty_operations moxa_ops = {
256 .write_room = moxa_write_room,
257 .flush_buffer = moxa_flush_buffer,
258 .chars_in_buffer = moxa_chars_in_buffer,
259 .flush_chars = moxa_flush_chars,
260 .put_char = moxa_put_char,
262 .throttle = moxa_throttle,
263 .unthrottle = moxa_unthrottle,
264 .set_termios = moxa_set_termios,
267 .hangup = moxa_hangup,
268 .tiocmget = moxa_tiocmget,
269 .tiocmset = moxa_tiocmset,
272 static struct tty_driver *moxaDriver;
273 static struct moxa_port moxa_ports[MAX_PORTS];
274 static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
275 static DEFINE_SPINLOCK(moxa_lock);
278 static int moxa_get_PCI_conf(struct pci_dev *p, int board_type,
279 struct moxa_board_conf *board)
281 board->baseAddr = pci_resource_start (p, 2);
282 board->boardType = board_type;
283 switch (board_type) {
284 case MOXA_BOARD_C218_ISA:
285 case MOXA_BOARD_C218_PCI:
289 case MOXA_BOARD_CP204J:
296 board->busType = MOXA_BUS_TYPE_PCI;
297 /* don't lose the reference in the next pci_get_device iteration */
298 board->pdev = pci_dev_get(p);
302 #endif /* CONFIG_PCI */
304 static int __init moxa_init(void)
307 struct moxa_port *ch;
309 printk(KERN_INFO "MOXA Intellio family driver version %s\n", MOXA_VERSION);
310 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
314 moxaDriver->owner = THIS_MODULE;
315 moxaDriver->name = "ttyMX";
316 moxaDriver->major = ttymajor;
317 moxaDriver->minor_start = 0;
318 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
319 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
320 moxaDriver->init_termios = tty_std_termios;
321 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
322 moxaDriver->init_termios.c_ispeed = 9600;
323 moxaDriver->init_termios.c_ospeed = 9600;
324 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
325 tty_set_operations(moxaDriver, &moxa_ops);
327 for (i = 0, ch = moxa_ports; i < MAX_PORTS; i++, ch++) {
328 ch->type = PORT_16550A;
330 ch->close_delay = 5 * HZ / 10;
331 ch->closing_wait = 30 * HZ;
332 ch->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
333 init_waitqueue_head(&ch->open_wait);
334 init_waitqueue_head(&ch->close_wait);
336 setup_timer(&ch->emptyTimer, check_xmit_empty,
340 printk("Tty devices major number = %d\n", ttymajor);
342 if (tty_register_driver(moxaDriver)) {
343 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
344 put_tty_driver(moxaDriver);
348 mod_timer(&moxaTimer, jiffies + HZ / 50);
350 /* Find the boards defined in source code */
352 for (i = 0; i < MAX_BOARDS; i++) {
353 if ((moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA) ||
354 (moxa_isa_boards[i].boardType == MOXA_BOARD_C320_ISA)) {
355 moxa_boards[numBoards].boardType = moxa_isa_boards[i].boardType;
356 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
357 moxa_boards[numBoards].numPorts = 8;
359 moxa_boards[numBoards].numPorts = moxa_isa_boards[i].numPorts;
360 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
361 moxa_boards[numBoards].baseAddr = moxa_isa_boards[i].baseAddr;
363 printk("Board %2d: %s board(baseAddr=%lx)\n",
365 moxa_brdname[moxa_boards[numBoards].boardType - 1],
366 moxa_boards[numBoards].baseAddr);
370 /* Find the boards defined form module args. */
372 for (i = 0; i < MAX_BOARDS; i++) {
373 if ((type[i] == MOXA_BOARD_C218_ISA) ||
374 (type[i] == MOXA_BOARD_C320_ISA)) {
376 printk("Board %2d: %s board(baseAddr=%lx)\n",
378 moxa_brdname[type[i] - 1],
379 (unsigned long) baseaddr[i]);
380 if (numBoards >= MAX_BOARDS) {
382 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
385 moxa_boards[numBoards].boardType = type[i];
386 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
387 moxa_boards[numBoards].numPorts = 8;
389 moxa_boards[numBoards].numPorts = numports[i];
390 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
391 moxa_boards[numBoards].baseAddr = baseaddr[i];
396 /* Find PCI boards here */
399 struct pci_dev *p = NULL;
400 int n = ARRAY_SIZE(moxa_pcibrds) - 1;
403 while ((p = pci_get_device(moxa_pcibrds[i].vendor, moxa_pcibrds[i].device, p))!=NULL)
405 if (pci_enable_device(p))
407 if (numBoards >= MAX_BOARDS) {
409 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
411 moxa_get_PCI_conf(p, moxa_pcibrds[i].driver_data,
412 &moxa_boards[numBoards]);
420 for (i = 0; i < numBoards; i++) {
421 moxa_boards[i].basemem = ioremap(moxa_boards[i].baseAddr,
428 static void __exit moxa_exit(void)
433 printk("Unloading module moxa ...\n");
435 del_timer_sync(&moxaTimer);
437 for (i = 0; i < MAX_PORTS; i++)
438 del_timer_sync(&moxa_ports[i].emptyTimer);
440 if (tty_unregister_driver(moxaDriver))
441 printk("Couldn't unregister MOXA Intellio family serial driver\n");
442 put_tty_driver(moxaDriver);
444 for (i = 0; i < MAX_BOARDS; i++) {
445 if (moxa_boards[i].basemem)
446 iounmap(moxa_boards[i].basemem);
447 if (moxa_boards[i].busType == MOXA_BUS_TYPE_PCI)
448 pci_dev_put(moxa_boards[i].pdev);
455 module_init(moxa_init);
456 module_exit(moxa_exit);
458 static int moxa_open(struct tty_struct *tty, struct file *filp)
460 struct moxa_port *ch;
465 if (port == MAX_PORTS) {
468 if (!MoxaPortIsValid(port)) {
469 tty->driver_data = NULL;
473 ch = &moxa_ports[port];
475 tty->driver_data = ch;
477 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
480 MoxaPortLineCtrl(ch->port, 1, 1);
481 MoxaPortEnable(ch->port);
482 ch->asyncflags |= ASYNC_INITIALIZED;
484 retval = block_till_ready(tty, filp, ch);
486 moxa_unthrottle(tty);
488 if (ch->type == PORT_16550A) {
489 MoxaSetFifo(ch->port, 1);
491 MoxaSetFifo(ch->port, 0);
497 static void moxa_close(struct tty_struct *tty, struct file *filp)
499 struct moxa_port *ch;
503 if (port == MAX_PORTS) {
506 if (!MoxaPortIsValid(port)) {
507 #ifdef SERIAL_DEBUG_CLOSE
508 printk("Invalid portno in moxa_close\n");
510 tty->driver_data = NULL;
513 if (tty->driver_data == NULL) {
516 if (tty_hung_up_p(filp)) {
519 ch = (struct moxa_port *) tty->driver_data;
521 if ((tty->count == 1) && (ch->count != 1)) {
522 printk("moxa_close: bad serial port count; tty->count is 1, "
523 "ch->count is %d\n", ch->count);
526 if (--ch->count < 0) {
527 printk("moxa_close: bad serial port count, device=%s\n",
534 ch->asyncflags |= ASYNC_CLOSING;
536 ch->cflag = tty->termios->c_cflag;
537 if (ch->asyncflags & ASYNC_INITIALIZED) {
538 setup_empty_event(tty);
539 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
540 del_timer_sync(&moxa_ports[ch->port].emptyTimer);
543 MoxaPortFlushData(port, 2);
545 if (tty->driver->flush_buffer)
546 tty->driver->flush_buffer(tty);
547 tty_ldisc_flush(tty);
552 if (ch->blocked_open) {
553 if (ch->close_delay) {
554 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
556 wake_up_interruptible(&ch->open_wait);
558 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
559 wake_up_interruptible(&ch->close_wait);
562 static int moxa_write(struct tty_struct *tty,
563 const unsigned char *buf, int count)
565 struct moxa_port *ch;
569 ch = (struct moxa_port *) tty->driver_data;
574 spin_lock_irqsave(&moxa_lock, flags);
575 len = MoxaPortWriteData(port, (unsigned char *) buf, count);
576 spin_unlock_irqrestore(&moxa_lock, flags);
578 /*********************************************
579 if ( !(ch->statusflags & LOWWAIT) &&
580 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
581 ************************************************/
582 ch->statusflags |= LOWWAIT;
586 static int moxa_write_room(struct tty_struct *tty)
588 struct moxa_port *ch;
592 ch = (struct moxa_port *) tty->driver_data;
595 return (MoxaPortTxFree(ch->port));
598 static void moxa_flush_buffer(struct tty_struct *tty)
600 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
604 MoxaPortFlushData(ch->port, 1);
608 static int moxa_chars_in_buffer(struct tty_struct *tty)
611 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
614 * Sigh...I have to check if driver_data is NULL here, because
615 * if an open() fails, the TTY subsystem eventually calls
616 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
617 * routine. And since the open() failed, we return 0 here. TDJ
621 chars = MoxaPortTxQueue(ch->port);
624 * Make it possible to wakeup anything waiting for output
625 * in tty_ioctl.c, etc.
627 if (!(ch->statusflags & EMPTYWAIT))
628 setup_empty_event(tty);
633 static void moxa_flush_chars(struct tty_struct *tty)
636 * Don't think I need this, because this is called to empty the TX
637 * buffer for the 16450, 16550, etc.
641 static void moxa_put_char(struct tty_struct *tty, unsigned char c)
643 struct moxa_port *ch;
647 ch = (struct moxa_port *) tty->driver_data;
651 spin_lock_irqsave(&moxa_lock, flags);
652 MoxaPortWriteData(port, &c, 1);
653 spin_unlock_irqrestore(&moxa_lock, flags);
654 /************************************************
655 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
656 *************************************************/
657 ch->statusflags |= LOWWAIT;
660 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
662 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
664 int flag = 0, dtr, rts;
667 if ((port != MAX_PORTS) && (!ch))
670 MoxaPortGetLineOut(ch->port, &dtr, &rts);
675 dtr = MoxaPortLineStatus(ch->port);
685 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
686 unsigned int set, unsigned int clear)
688 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
693 if ((port != MAX_PORTS) && (!ch))
696 MoxaPortGetLineOut(ch->port, &dtr, &rts);
701 if (clear & TIOCM_RTS)
703 if (clear & TIOCM_DTR)
705 MoxaPortLineCtrl(ch->port, dtr, rts);
709 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
710 unsigned int cmd, unsigned long arg)
712 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
714 void __user *argp = (void __user *)arg;
718 if ((port != MAX_PORTS) && (!ch))
722 case TCSBRK: /* SVID version: non-zero arg --> no break */
723 retval = tty_check_change(tty);
726 setup_empty_event(tty);
727 tty_wait_until_sent(tty, 0);
729 MoxaPortSendBreak(ch->port, 0);
731 case TCSBRKP: /* support for POSIX tcsendbreak() */
732 retval = tty_check_change(tty);
735 setup_empty_event(tty);
736 tty_wait_until_sent(tty, 0);
737 MoxaPortSendBreak(ch->port, arg);
740 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) argp);
742 if(get_user(retval, (unsigned long __user *) argp))
745 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
748 ch->asyncflags &= ~ASYNC_CHECK_CD;
750 ch->asyncflags |= ASYNC_CHECK_CD;
753 return moxa_get_serial_info(ch, argp);
756 return moxa_set_serial_info(ch, argp);
758 retval = MoxaDriverIoctl(cmd, arg, port);
763 static void moxa_throttle(struct tty_struct *tty)
765 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
767 ch->statusflags |= THROTTLE;
770 static void moxa_unthrottle(struct tty_struct *tty)
772 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
774 ch->statusflags &= ~THROTTLE;
777 static void moxa_set_termios(struct tty_struct *tty,
778 struct ktermios *old_termios)
780 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
785 if (!(old_termios->c_cflag & CLOCAL) &&
786 (tty->termios->c_cflag & CLOCAL))
787 wake_up_interruptible(&ch->open_wait);
790 static void moxa_stop(struct tty_struct *tty)
792 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
796 MoxaPortTxDisable(ch->port);
797 ch->statusflags |= TXSTOPPED;
801 static void moxa_start(struct tty_struct *tty)
803 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
808 if (!(ch->statusflags & TXSTOPPED))
811 MoxaPortTxEnable(ch->port);
812 ch->statusflags &= ~TXSTOPPED;
815 static void moxa_hangup(struct tty_struct *tty)
817 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
819 moxa_flush_buffer(tty);
823 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
825 wake_up_interruptible(&ch->open_wait);
828 static void moxa_poll(unsigned long ignored)
831 struct moxa_port *ch;
832 struct tty_struct *tp;
835 del_timer(&moxaTimer);
837 if (MoxaDriverPoll() < 0) {
838 mod_timer(&moxaTimer, jiffies + HZ / 50);
841 for (card = 0; card < MAX_BOARDS; card++) {
842 if ((ports = MoxaPortsOfCard(card)) <= 0)
844 ch = &moxa_ports[card * MAX_PORTS_PER_BOARD];
845 for (i = 0; i < ports; i++, ch++) {
846 if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
848 if (!(ch->statusflags & THROTTLE) &&
849 (MoxaPortRxQueue(ch->port) > 0))
851 if ((tp = ch->tty) == 0)
853 if (ch->statusflags & LOWWAIT) {
854 if (MoxaPortTxQueue(ch->port) <= WAKEUP_CHARS) {
856 ch->statusflags &= ~LOWWAIT;
861 if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch->port) > 0)) {
862 tty_insert_flip_char(tp, 0, TTY_BREAK);
863 tty_schedule_flip(tp);
865 if (MoxaPortDCDChange(ch->port)) {
866 if (ch->asyncflags & ASYNC_CHECK_CD) {
867 if (MoxaPortDCDON(ch->port))
868 wake_up_interruptible(&ch->open_wait);
871 wake_up_interruptible(&ch->open_wait);
872 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
879 mod_timer(&moxaTimer, jiffies + HZ / 50);
882 /******************************************************************************/
884 static void set_tty_param(struct tty_struct *tty)
886 register struct ktermios *ts;
887 struct moxa_port *ch;
888 int rts, cts, txflow, rxflow, xany;
890 ch = (struct moxa_port *) tty->driver_data;
892 if (ts->c_cflag & CLOCAL)
893 ch->asyncflags &= ~ASYNC_CHECK_CD;
895 ch->asyncflags |= ASYNC_CHECK_CD;
896 rts = cts = txflow = rxflow = xany = 0;
897 if (ts->c_cflag & CRTSCTS)
899 if (ts->c_iflag & IXON)
901 if (ts->c_iflag & IXOFF)
903 if (ts->c_iflag & IXANY)
905 MoxaPortFlowCtrl(ch->port, rts, cts, txflow, rxflow, xany);
906 MoxaPortSetTermio(ch->port, ts, tty_get_baud_rate(tty));
909 static int block_till_ready(struct tty_struct *tty, struct file *filp,
910 struct moxa_port *ch)
912 DECLARE_WAITQUEUE(wait,current);
915 int do_clocal = C_CLOCAL(tty);
918 * If the device is in the middle of being closed, then block
919 * until it's done, and then try again.
921 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
922 if (ch->asyncflags & ASYNC_CLOSING)
923 interruptible_sleep_on(&ch->close_wait);
924 #ifdef SERIAL_DO_RESTART
925 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
928 return (-ERESTARTSYS);
934 * If non-blocking mode is set, then make the check up front
937 if (filp->f_flags & O_NONBLOCK) {
938 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
942 * Block waiting for the carrier detect and the line to become free
945 add_wait_queue(&ch->open_wait, &wait);
946 #ifdef SERIAL_DEBUG_OPEN
947 printk("block_til_ready before block: ttys%d, count = %d\n",
948 ch->line, ch->count);
950 spin_lock_irqsave(&moxa_lock, flags);
951 if (!tty_hung_up_p(filp))
954 spin_unlock_irqrestore(&moxa_lock, flags);
957 set_current_state(TASK_INTERRUPTIBLE);
958 if (tty_hung_up_p(filp) ||
959 !(ch->asyncflags & ASYNC_INITIALIZED)) {
960 #ifdef SERIAL_DO_RESTART
961 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
964 retval = -ERESTARTSYS;
970 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
971 MoxaPortDCDON(ch->port)))
974 if (signal_pending(current)) {
975 retval = -ERESTARTSYS;
980 set_current_state(TASK_RUNNING);
981 remove_wait_queue(&ch->open_wait, &wait);
983 spin_lock_irqsave(&moxa_lock, flags);
984 if (!tty_hung_up_p(filp))
987 spin_unlock_irqrestore(&moxa_lock, flags);
988 #ifdef SERIAL_DEBUG_OPEN
989 printk("block_til_ready after blocking: ttys%d, count = %d\n",
990 ch->line, ch->count);
994 /* FIXME: review to see if we need to use set_bit on these */
995 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
999 static void setup_empty_event(struct tty_struct *tty)
1001 struct moxa_port *ch = tty->driver_data;
1002 unsigned long flags;
1004 spin_lock_irqsave(&moxa_lock, flags);
1005 ch->statusflags |= EMPTYWAIT;
1006 mod_timer(&moxa_ports[ch->port].emptyTimer, jiffies + HZ);
1007 spin_unlock_irqrestore(&moxa_lock, flags);
1010 static void check_xmit_empty(unsigned long data)
1012 struct moxa_port *ch;
1014 ch = (struct moxa_port *) data;
1015 del_timer_sync(&moxa_ports[ch->port].emptyTimer);
1016 if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
1017 if (MoxaPortTxQueue(ch->port) == 0) {
1018 ch->statusflags &= ~EMPTYWAIT;
1019 tty_wakeup(ch->tty);
1022 mod_timer(&moxa_ports[ch->port].emptyTimer, jiffies + HZ);
1024 ch->statusflags &= ~EMPTYWAIT;
1027 static void shut_down(struct moxa_port *ch)
1029 struct tty_struct *tp;
1031 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1036 MoxaPortDisable(ch->port);
1039 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1041 if (tp->termios->c_cflag & HUPCL)
1042 MoxaPortLineCtrl(ch->port, 0, 0);
1044 ch->asyncflags &= ~ASYNC_INITIALIZED;
1047 static void receive_data(struct moxa_port *ch)
1049 struct tty_struct *tp;
1050 struct ktermios *ts;
1051 unsigned long flags;
1057 /**************************************************
1058 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1059 *****************************************************/
1061 MoxaPortFlushData(ch->port, 0);
1064 spin_lock_irqsave(&moxa_lock, flags);
1065 MoxaPortReadData(ch->port, tp);
1066 spin_unlock_irqrestore(&moxa_lock, flags);
1067 tty_schedule_flip(tp);
1070 #define Magic_code 0x404
1073 * System Configuration
1076 * for C218 BIOS initialization
1078 #define C218_ConfBase 0x800
1079 #define C218_status (C218_ConfBase + 0) /* BIOS running status */
1080 #define C218_diag (C218_ConfBase + 2) /* diagnostic status */
1081 #define C218_key (C218_ConfBase + 4) /* WORD (0x218 for C218) */
1082 #define C218DLoad_len (C218_ConfBase + 6) /* WORD */
1083 #define C218check_sum (C218_ConfBase + 8) /* BYTE */
1084 #define C218chksum_ok (C218_ConfBase + 0x0a) /* BYTE (1:ok) */
1085 #define C218_TestRx (C218_ConfBase + 0x10) /* 8 bytes for 8 ports */
1086 #define C218_TestTx (C218_ConfBase + 0x18) /* 8 bytes for 8 ports */
1087 #define C218_RXerr (C218_ConfBase + 0x20) /* 8 bytes for 8 ports */
1088 #define C218_ErrFlag (C218_ConfBase + 0x28) /* 8 bytes for 8 ports */
1090 #define C218_LoadBuf 0x0F00
1091 #define C218_KeyCode 0x218
1092 #define CP204J_KeyCode 0x204
1095 * for C320 BIOS initialization
1097 #define C320_ConfBase 0x800
1098 #define C320_LoadBuf 0x0f00
1099 #define STS_init 0x05 /* for C320_status */
1101 #define C320_status C320_ConfBase + 0 /* BIOS running status */
1102 #define C320_diag C320_ConfBase + 2 /* diagnostic status */
1103 #define C320_key C320_ConfBase + 4 /* WORD (0320H for C320) */
1104 #define C320DLoad_len C320_ConfBase + 6 /* WORD */
1105 #define C320check_sum C320_ConfBase + 8 /* WORD */
1106 #define C320chksum_ok C320_ConfBase + 0x0a /* WORD (1:ok) */
1107 #define C320bapi_len C320_ConfBase + 0x0c /* WORD */
1108 #define C320UART_no C320_ConfBase + 0x0e /* WORD */
1110 #define C320_KeyCode 0x320
1112 #define FixPage_addr 0x0000 /* starting addr of static page */
1113 #define DynPage_addr 0x2000 /* starting addr of dynamic page */
1114 #define C218_start 0x3000 /* starting addr of C218 BIOS prg */
1115 #define Control_reg 0x1ff0 /* select page and reset control */
1116 #define HW_reset 0x80
1121 #define FC_CardReset 0x80
1122 #define FC_ChannelReset 1 /* C320 firmware not supported */
1123 #define FC_EnableCH 2
1124 #define FC_DisableCH 3
1125 #define FC_SetParam 4
1126 #define FC_SetMode 5
1127 #define FC_SetRate 6
1128 #define FC_LineControl 7
1129 #define FC_LineStatus 8
1130 #define FC_XmitControl 9
1131 #define FC_FlushQueue 10
1132 #define FC_SendBreak 11
1133 #define FC_StopBreak 12
1134 #define FC_LoopbackON 13
1135 #define FC_LoopbackOFF 14
1136 #define FC_ClrIrqTable 15
1137 #define FC_SendXon 16
1138 #define FC_SetTermIrq 17 /* C320 firmware not supported */
1139 #define FC_SetCntIrq 18 /* C320 firmware not supported */
1140 #define FC_SetBreakIrq 19
1141 #define FC_SetLineIrq 20
1142 #define FC_SetFlowCtl 21
1143 #define FC_GenIrq 22
1144 #define FC_InCD180 23
1145 #define FC_OutCD180 24
1146 #define FC_InUARTreg 23
1147 #define FC_OutUARTreg 24
1148 #define FC_SetXonXoff 25
1149 #define FC_OutCD180CCR 26
1150 #define FC_ExtIQueue 27
1151 #define FC_ExtOQueue 28
1152 #define FC_ClrLineIrq 29
1153 #define FC_HWFlowCtl 30
1154 #define FC_GetClockRate 35
1155 #define FC_SetBaud 36
1156 #define FC_SetDataMode 41
1157 #define FC_GetCCSR 43
1158 #define FC_GetDataError 45
1159 #define FC_RxControl 50
1160 #define FC_ImmSend 51
1161 #define FC_SetXonState 52
1162 #define FC_SetXoffState 53
1163 #define FC_SetRxFIFOTrig 54
1164 #define FC_SetTxFIFOCnt 55
1165 #define FC_UnixRate 56
1166 #define FC_UnixResetTimer 57
1168 #define RxFIFOTrig1 0
1169 #define RxFIFOTrig4 1
1170 #define RxFIFOTrig8 2
1171 #define RxFIFOTrig14 3
1176 #define DRAM_global 0
1177 #define INT_data (DRAM_global + 0)
1178 #define Config_base (DRAM_global + 0x108)
1180 #define IRQindex (INT_data + 0)
1181 #define IRQpending (INT_data + 4)
1182 #define IRQtable (INT_data + 8)
1187 #define IntrRx 0x01 /* receiver data O.K. */
1188 #define IntrTx 0x02 /* transmit buffer empty */
1189 #define IntrFunc 0x04 /* function complete */
1190 #define IntrBreak 0x08 /* received break */
1191 #define IntrLine 0x10 /* line status change
1193 #define IntrIntr 0x20 /* received INTR code */
1194 #define IntrQuit 0x40 /* received QUIT code */
1195 #define IntrEOF 0x80 /* received EOF code */
1197 #define IntrRxTrigger 0x100 /* rx data count reach tigger value */
1198 #define IntrTxTrigger 0x200 /* tx data count below trigger value */
1200 #define Magic_no (Config_base + 0)
1201 #define Card_model_no (Config_base + 2)
1202 #define Total_ports (Config_base + 4)
1203 #define Module_cnt (Config_base + 8)
1204 #define Module_no (Config_base + 10)
1205 #define Timer_10ms (Config_base + 14)
1206 #define Disable_IRQ (Config_base + 20)
1207 #define TMS320_PORT1 (Config_base + 22)
1208 #define TMS320_PORT2 (Config_base + 24)
1209 #define TMS320_CLOCK (Config_base + 26)
1212 * DATA BUFFER in DRAM
1214 #define Extern_table 0x400 /* Base address of the external table
1215 (24 words * 64) total 3K bytes
1216 (24 words * 128) total 6K bytes */
1217 #define Extern_size 0x60 /* 96 bytes */
1218 #define RXrptr 0x00 /* read pointer for RX buffer */
1219 #define RXwptr 0x02 /* write pointer for RX buffer */
1220 #define TXrptr 0x04 /* read pointer for TX buffer */
1221 #define TXwptr 0x06 /* write pointer for TX buffer */
1222 #define HostStat 0x08 /* IRQ flag and general flag */
1223 #define FlagStat 0x0A
1224 #define FlowControl 0x0C /* B7 B6 B5 B4 B3 B2 B1 B0 */
1225 /* x x x x | | | | */
1226 /* | | | + CTS flow */
1227 /* | | +--- RTS flow */
1228 /* | +------ TX Xon/Xoff */
1229 /* +--------- RX Xon/Xoff */
1230 #define Break_cnt 0x0E /* received break count */
1231 #define CD180TXirq 0x10 /* if non-0: enable TX irq */
1232 #define RX_mask 0x12
1233 #define TX_mask 0x14
1234 #define Ofs_rxb 0x16
1235 #define Ofs_txb 0x18
1236 #define Page_rxb 0x1A
1237 #define Page_txb 0x1C
1238 #define EndPage_rxb 0x1E
1239 #define EndPage_txb 0x20
1240 #define Data_error 0x22
1241 #define RxTrigger 0x28
1242 #define TxTrigger 0x2a
1244 #define rRXwptr 0x34
1245 #define Low_water 0x36
1247 #define FuncCode 0x40
1248 #define FuncArg 0x42
1249 #define FuncArg1 0x44
1251 #define C218rx_size 0x2000 /* 8K bytes */
1252 #define C218tx_size 0x8000 /* 32K bytes */
1254 #define C218rx_mask (C218rx_size - 1)
1255 #define C218tx_mask (C218tx_size - 1)
1257 #define C320p8rx_size 0x2000
1258 #define C320p8tx_size 0x8000
1259 #define C320p8rx_mask (C320p8rx_size - 1)
1260 #define C320p8tx_mask (C320p8tx_size - 1)
1262 #define C320p16rx_size 0x2000
1263 #define C320p16tx_size 0x4000
1264 #define C320p16rx_mask (C320p16rx_size - 1)
1265 #define C320p16tx_mask (C320p16tx_size - 1)
1267 #define C320p24rx_size 0x2000
1268 #define C320p24tx_size 0x2000
1269 #define C320p24rx_mask (C320p24rx_size - 1)
1270 #define C320p24tx_mask (C320p24tx_size - 1)
1272 #define C320p32rx_size 0x1000
1273 #define C320p32tx_size 0x1000
1274 #define C320p32rx_mask (C320p32rx_size - 1)
1275 #define C320p32tx_mask (C320p32tx_size - 1)
1277 #define Page_size 0x2000
1278 #define Page_mask (Page_size - 1)
1279 #define C218rx_spage 3
1280 #define C218tx_spage 4
1281 #define C218rx_pageno 1
1282 #define C218tx_pageno 4
1283 #define C218buf_pageno 5
1285 #define C320p8rx_spage 3
1286 #define C320p8tx_spage 4
1287 #define C320p8rx_pgno 1
1288 #define C320p8tx_pgno 4
1289 #define C320p8buf_pgno 5
1291 #define C320p16rx_spage 3
1292 #define C320p16tx_spage 4
1293 #define C320p16rx_pgno 1
1294 #define C320p16tx_pgno 2
1295 #define C320p16buf_pgno 3
1297 #define C320p24rx_spage 3
1298 #define C320p24tx_spage 4
1299 #define C320p24rx_pgno 1
1300 #define C320p24tx_pgno 1
1301 #define C320p24buf_pgno 2
1303 #define C320p32rx_spage 3
1304 #define C320p32tx_ofs C320p32rx_size
1305 #define C320p32tx_spage 3
1306 #define C320p32buf_pgno 1
1311 #define WakeupRx 0x01
1312 #define WakeupTx 0x02
1313 #define WakeupBreak 0x08
1314 #define WakeupLine 0x10
1315 #define WakeupIntr 0x20
1316 #define WakeupQuit 0x40
1317 #define WakeupEOF 0x80 /* used in VTIME control */
1318 #define WakeupRxTrigger 0x100
1319 #define WakeupTxTrigger 0x200
1323 #define Rx_over 0x01
1324 #define Xoff_state 0x02
1325 #define Tx_flowOff 0x04
1326 #define Tx_enable 0x08
1327 #define CTS_state 0x10
1328 #define DSR_state 0x20
1329 #define DCD_state 0x80
1333 #define CTS_FlowCtl 1
1334 #define RTS_FlowCtl 2
1335 #define Tx_FlowCtl 4
1336 #define Rx_FlowCtl 8
1337 #define IXM_IXANY 0x10
1339 #define LowWater 128
1347 /* mode definition */
1353 #define MX_STOP1 0x00
1354 #define MX_STOP15 0x04
1355 #define MX_STOP2 0x08
1357 #define MX_PARNONE 0x00
1358 #define MX_PAREVEN 0x40
1359 #define MX_PARODD 0xC0
1367 int rxcnt[MAX_PORTS];
1368 int txcnt[MAX_PORTS];
1371 #define DCD_changed 0x01
1372 #define DCD_oldstate 0x80
1374 static unsigned char moxaBuff[10240];
1375 static int moxaLowWaterChk;
1376 static int moxaCard;
1377 static struct mon_str moxaLog;
1378 static int moxaFuncTout = HZ / 2;
1380 static void moxadelay(int);
1381 static void moxafunc(void __iomem *, int, ushort);
1382 static void wait_finish(void __iomem *);
1383 static void low_water_check(void __iomem *);
1384 static int moxaloadbios(int, unsigned char __user *, int);
1385 static int moxafindcard(int);
1386 static int moxaload320b(int, unsigned char __user *, int);
1387 static int moxaloadcode(int, unsigned char __user *, int);
1388 static int moxaloadc218(int, void __iomem *, int);
1389 static int moxaloadc320(int, void __iomem *, int, int *);
1391 /*****************************************************************************
1392 * Driver level functions: *
1393 * 1. MoxaDriverInit(void); *
1394 * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
1395 * 3. MoxaDriverPoll(void); *
1396 *****************************************************************************/
1397 void MoxaDriverInit(void)
1399 struct moxa_port *p;
1402 moxaFuncTout = HZ / 2; /* 500 mini-seconds */
1405 moxaLowWaterChk = 0;
1406 for (i = 0; i < MAX_PORTS; i++) {
1411 moxaLog.rxcnt[i] = 0;
1412 moxaLog.txcnt[i] = 0;
1417 #define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1418 #define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
1419 #define MOXA_INIT_DRIVER (MOXA + 6) /* moxaCard=0 */
1420 #define MOXA_LOAD_BIOS (MOXA + 9) /* download BIOS */
1421 #define MOXA_FIND_BOARD (MOXA + 10) /* Check if MOXA card exist? */
1422 #define MOXA_LOAD_C320B (MOXA + 11) /* download 320B firmware */
1423 #define MOXA_LOAD_CODE (MOXA + 12) /* download firmware */
1424 #define MOXA_GETDATACOUNT (MOXA + 23)
1425 #define MOXA_GET_IOQUEUE (MOXA + 27)
1426 #define MOXA_FLUSH_QUEUE (MOXA + 28)
1427 #define MOXA_GET_CONF (MOXA + 35) /* configuration */
1428 #define MOXA_GET_MAJOR (MOXA + 63)
1429 #define MOXA_GET_CUMAJOR (MOXA + 64)
1430 #define MOXA_GETMSTATUS (MOXA + 65)
1438 static struct dl_str dltmp;
1440 void MoxaPortFlushData(int port, int mode)
1442 void __iomem *ofsAddr;
1443 if ((mode < 0) || (mode > 2))
1445 ofsAddr = moxa_ports[port].tableAddr;
1446 moxafunc(ofsAddr, FC_FlushQueue, mode);
1448 moxa_ports[port].lowChkFlag = 0;
1449 low_water_check(ofsAddr);
1453 int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port)
1457 int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
1458 void __user *argp = (void __user *)arg;
1460 if (port == MAX_PORTS) {
1461 if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_INIT_DRIVER) &&
1462 (cmd != MOXA_LOAD_BIOS) && (cmd != MOXA_FIND_BOARD) && (cmd != MOXA_LOAD_C320B) &&
1463 (cmd != MOXA_LOAD_CODE) && (cmd != MOXA_GETDATACOUNT) &&
1464 (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
1465 (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
1470 if(copy_to_user(argp, &moxa_boards, MAX_BOARDS *
1471 sizeof(struct moxa_board_conf)))
1474 case MOXA_INIT_DRIVER:
1475 if ((int) arg == 0x404)
1478 case MOXA_GETDATACOUNT:
1479 moxaLog.tick = jiffies;
1480 if(copy_to_user(argp, &moxaLog, sizeof(struct mon_str)))
1483 case MOXA_FLUSH_QUEUE:
1484 MoxaPortFlushData(port, arg);
1486 case MOXA_GET_IOQUEUE: {
1487 struct moxaq_str __user *argm = argp;
1488 struct moxaq_str tmp;
1490 for (i = 0; i < MAX_PORTS; i++, argm++) {
1491 memset(&tmp, 0, sizeof(tmp));
1492 if (moxa_ports[i].chkPort) {
1493 tmp.inq = MoxaPortRxQueue(i);
1494 tmp.outq = MoxaPortTxQueue(i);
1496 if (copy_to_user(argm, &tmp, sizeof(tmp)))
1500 } case MOXA_GET_OQUEUE:
1501 i = MoxaPortTxQueue(port);
1502 return put_user(i, (unsigned long __user *)argp);
1503 case MOXA_GET_IQUEUE:
1504 i = MoxaPortRxQueue(port);
1505 return put_user(i, (unsigned long __user *)argp);
1506 case MOXA_GET_MAJOR:
1507 if(copy_to_user(argp, &ttymajor, sizeof(int)))
1510 case MOXA_GET_CUMAJOR:
1512 if(copy_to_user(argp, &i, sizeof(int)))
1515 case MOXA_GETMSTATUS: {
1516 struct mxser_mstatus __user *argm = argp;
1517 struct mxser_mstatus tmp;
1518 struct moxa_port *p;
1520 for (i = 0; i < MAX_PORTS; i++, argm++) {
1522 memset(&tmp, 0, sizeof(tmp));
1526 status = MoxaPortLineStatus(p->port);
1535 if (!p->tty || !p->tty->termios)
1536 tmp.cflag = p->cflag;
1538 tmp.cflag = p->tty->termios->c_cflag;
1540 if (copy_to_user(argm, &tmp, sizeof(tmp)))
1545 return (-ENOIOCTLCMD);
1546 case MOXA_LOAD_BIOS:
1547 case MOXA_FIND_BOARD:
1548 case MOXA_LOAD_C320B:
1549 case MOXA_LOAD_CODE:
1550 if (!capable(CAP_SYS_RAWIO))
1555 if(copy_from_user(&dltmp, argp, sizeof(struct dl_str)))
1557 if(dltmp.cardno < 0 || dltmp.cardno >= MAX_BOARDS)
1562 case MOXA_LOAD_BIOS:
1563 i = moxaloadbios(dltmp.cardno, dltmp.buf, dltmp.len);
1565 case MOXA_FIND_BOARD:
1566 return moxafindcard(dltmp.cardno);
1567 case MOXA_LOAD_C320B:
1568 moxaload320b(dltmp.cardno, dltmp.buf, dltmp.len);
1569 default: /* to keep gcc happy */
1571 case MOXA_LOAD_CODE:
1572 i = moxaloadcode(dltmp.cardno, dltmp.buf, dltmp.len);
1580 int MoxaDriverPoll(void)
1582 struct moxa_board_conf *brd;
1583 register ushort temp;
1585 void __iomem *ofsAddr;
1591 for (card = 0; card < MAX_BOARDS; card++) {
1592 brd = &moxa_boards[card];
1593 if (brd->loadstat == 0)
1595 if ((ports = brd->numPorts) == 0)
1597 if (readb(brd->intPend) == 0xff) {
1598 ip = brd->intTable + readb(brd->intNdx);
1599 p = card * MAX_PORTS_PER_BOARD;
1601 for (port = 0; port < ports; port += 2, p++) {
1602 if ((temp = readw(ip + port)) != 0) {
1603 writew(0, ip + port);
1604 ofsAddr = moxa_ports[p].tableAddr;
1606 writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
1607 if (temp & IntrBreak) {
1608 moxa_ports[p].breakCnt++;
1610 if (temp & IntrLine) {
1611 if (readb(ofsAddr + FlagStat) & DCD_state) {
1612 if ((moxa_ports[p].DCDState & DCD_oldstate) == 0)
1613 moxa_ports[p].DCDState = (DCD_oldstate |
1616 if (moxa_ports[p].DCDState & DCD_oldstate)
1617 moxa_ports[p].DCDState = DCD_changed;
1622 writeb(0, brd->intPend);
1624 if (moxaLowWaterChk) {
1625 p = card * MAX_PORTS_PER_BOARD;
1626 for (port = 0; port < ports; port++, p++) {
1627 if (moxa_ports[p].lowChkFlag) {
1628 moxa_ports[p].lowChkFlag = 0;
1629 ofsAddr = moxa_ports[p].tableAddr;
1630 low_water_check(ofsAddr);
1635 moxaLowWaterChk = 0;
1639 /*****************************************************************************
1640 * Card level function: *
1641 * 1. MoxaPortsOfCard(int cardno); *
1642 *****************************************************************************/
1643 int MoxaPortsOfCard(int cardno)
1646 if (moxa_boards[cardno].boardType == 0)
1648 return (moxa_boards[cardno].numPorts);
1651 /*****************************************************************************
1652 * Port level functions: *
1653 * 1. MoxaPortIsValid(int port); *
1654 * 2. MoxaPortEnable(int port); *
1655 * 3. MoxaPortDisable(int port); *
1656 * 4. MoxaPortGetMaxBaud(int port); *
1657 * 6. MoxaPortSetBaud(int port, long baud); *
1658 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1659 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1660 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1661 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1662 * 12. MoxaPortLineStatus(int port); *
1663 * 13. MoxaPortDCDChange(int port); *
1664 * 14. MoxaPortDCDON(int port); *
1665 * 15. MoxaPortFlushData(int port, int mode); *
1666 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1667 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
1668 * 20. MoxaPortTxQueue(int port); *
1669 * 21. MoxaPortTxFree(int port); *
1670 * 22. MoxaPortRxQueue(int port); *
1671 * 24. MoxaPortTxDisable(int port); *
1672 * 25. MoxaPortTxEnable(int port); *
1673 * 27. MoxaPortResetBrkCnt(int port); *
1674 * 30. MoxaPortSendBreak(int port, int ticks); *
1675 *****************************************************************************/
1677 * Moxa Port Number Description:
1679 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1680 * the port number using in MOXA driver functions will be 0 to 31 for
1681 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1682 * to 127 for fourth. For example, if you setup three MOXA boards,
1683 * first board is C218, second board is C320-16 and third board is
1684 * C320-32. The port number of first board (C218 - 8 ports) is from
1685 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1686 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1687 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1688 * 127 will be invalid.
1691 * Moxa Functions Description:
1693 * Function 1: Driver initialization routine, this routine must be
1694 * called when initialized driver.
1696 * void MoxaDriverInit();
1699 * Function 2: Moxa driver private IOCTL command processing.
1701 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1703 * unsigned int cmd : IOCTL command
1704 * unsigned long arg : IOCTL argument
1705 * int port : port number (0 - 127)
1712 * Function 3: Moxa driver polling process routine.
1714 * int MoxaDriverPoll(void);
1716 * return: 0 ; polling O.K.
1717 * -1 : no any Moxa card.
1720 * Function 4: Get the ports of this card.
1722 * int MoxaPortsOfCard(int cardno);
1724 * int cardno : card number (0 - 3)
1726 * return: 0 : this card is invalid
1730 * Function 5: Check this port is valid or invalid
1732 * int MoxaPortIsValid(int port);
1733 * int port : port number (0 - 127, ref port description)
1735 * return: 0 : this port is invalid
1736 * 1 : this port is valid
1739 * Function 6: Enable this port to start Tx/Rx data.
1741 * void MoxaPortEnable(int port);
1742 * int port : port number (0 - 127)
1745 * Function 7: Disable this port
1747 * void MoxaPortDisable(int port);
1748 * int port : port number (0 - 127)
1751 * Function 8: Get the maximun available baud rate of this port.
1753 * long MoxaPortGetMaxBaud(int port);
1754 * int port : port number (0 - 127)
1756 * return: 0 : this port is invalid
1757 * 38400/57600/115200 bps
1760 * Function 10: Setting baud rate of this port.
1762 * long MoxaPortSetBaud(int port, long baud);
1763 * int port : port number (0 - 127)
1764 * long baud : baud rate (50 - 115200)
1766 * return: 0 : this port is invalid or baud < 50
1767 * 50 - 115200 : the real baud rate set to the port, if
1768 * the argument baud is large than maximun
1769 * available baud rate, the real setting
1770 * baud rate will be the maximun baud rate.
1773 * Function 12: Configure the port.
1775 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1776 * int port : port number (0 - 127)
1777 * struct ktermios * termio : termio structure pointer
1778 * speed_t baud : baud rate
1780 * return: -1 : this port is invalid or termio == NULL
1784 * Function 13: Get the DTR/RTS state of this port.
1786 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1787 * int port : port number (0 - 127)
1788 * int * dtrState : pointer to INT to receive the current DTR
1789 * state. (if NULL, this function will not
1790 * write to this address)
1791 * int * rtsState : pointer to INT to receive the current RTS
1792 * state. (if NULL, this function will not
1793 * write to this address)
1795 * return: -1 : this port is invalid
1799 * Function 14: Setting the DTR/RTS output state of this port.
1801 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1802 * int port : port number (0 - 127)
1803 * int dtrState : DTR output state (0: off, 1: on)
1804 * int rtsState : RTS output state (0: off, 1: on)
1807 * Function 15: Setting the flow control of this port.
1809 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1810 * int txFlow,int xany);
1811 * int port : port number (0 - 127)
1812 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1813 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1814 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1815 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1816 * int xany : S/W XANY flow control (0: no, 1: yes)
1819 * Function 16: Get ths line status of this port
1821 * int MoxaPortLineStatus(int port);
1822 * int port : port number (0 - 127)
1824 * return: Bit 0 - CTS state (0: off, 1: on)
1825 * Bit 1 - DSR state (0: off, 1: on)
1826 * Bit 2 - DCD state (0: off, 1: on)
1829 * Function 17: Check the DCD state has changed since the last read
1832 * int MoxaPortDCDChange(int port);
1833 * int port : port number (0 - 127)
1835 * return: 0 : no changed
1836 * 1 : DCD has changed
1839 * Function 18: Check ths current DCD state is ON or not.
1841 * int MoxaPortDCDON(int port);
1842 * int port : port number (0 - 127)
1844 * return: 0 : DCD off
1848 * Function 19: Flush the Rx/Tx buffer data of this port.
1850 * void MoxaPortFlushData(int port, int mode);
1851 * int port : port number (0 - 127)
1853 * 0 : flush the Rx buffer
1854 * 1 : flush the Tx buffer
1855 * 2 : flush the Rx and Tx buffer
1858 * Function 20: Write data.
1860 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1861 * int port : port number (0 - 127)
1862 * unsigned char * buffer : pointer to write data buffer.
1863 * int length : write data length
1865 * return: 0 - length : real write data length
1868 * Function 21: Read data.
1870 * int MoxaPortReadData(int port, struct tty_struct *tty);
1871 * int port : port number (0 - 127)
1872 * struct tty_struct *tty : tty for data
1874 * return: 0 - length : real read data length
1877 * Function 24: Get the Tx buffer current queued data bytes
1879 * int MoxaPortTxQueue(int port);
1880 * int port : port number (0 - 127)
1882 * return: .. : Tx buffer current queued data bytes
1885 * Function 25: Get the Tx buffer current free space
1887 * int MoxaPortTxFree(int port);
1888 * int port : port number (0 - 127)
1890 * return: .. : Tx buffer current free space
1893 * Function 26: Get the Rx buffer current queued data bytes
1895 * int MoxaPortRxQueue(int port);
1896 * int port : port number (0 - 127)
1898 * return: .. : Rx buffer current queued data bytes
1901 * Function 28: Disable port data transmission.
1903 * void MoxaPortTxDisable(int port);
1904 * int port : port number (0 - 127)
1907 * Function 29: Enable port data transmission.
1909 * void MoxaPortTxEnable(int port);
1910 * int port : port number (0 - 127)
1913 * Function 31: Get the received BREAK signal count and reset it.
1915 * int MoxaPortResetBrkCnt(int port);
1916 * int port : port number (0 - 127)
1918 * return: 0 - .. : BREAK signal count
1921 * Function 34: Send out a BREAK signal.
1923 * void MoxaPortSendBreak(int port, int ms100);
1924 * int port : port number (0 - 127)
1925 * int ms100 : break signal time interval.
1926 * unit: 100 mini-second. if ms100 == 0, it will
1927 * send out a about 250 ms BREAK signal.
1930 int MoxaPortIsValid(int port)
1935 if (moxa_ports[port].chkPort == 0)
1940 void MoxaPortEnable(int port)
1942 void __iomem *ofsAddr;
1943 int MoxaPortLineStatus(int);
1944 short lowwater = 512;
1946 ofsAddr = moxa_ports[port].tableAddr;
1947 writew(lowwater, ofsAddr + Low_water);
1948 moxa_ports[port].breakCnt = 0;
1949 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
1950 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
1951 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1953 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
1956 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1957 moxafunc(ofsAddr, FC_FlushQueue, 2);
1959 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1960 MoxaPortLineStatus(port);
1963 void MoxaPortDisable(int port)
1965 void __iomem *ofsAddr = moxa_ports[port].tableAddr;
1967 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1968 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1969 writew(0, ofsAddr + HostStat);
1970 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1973 long MoxaPortGetMaxBaud(int port)
1975 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
1976 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI))
1983 long MoxaPortSetBaud(int port, long baud)
1985 void __iomem *ofsAddr;
1989 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
1991 ofsAddr = moxa_ports[port].tableAddr;
1995 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
1996 else if (max == 57600L)
1997 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
1999 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
2001 moxafunc(ofsAddr, FC_SetBaud, val);
2003 moxa_ports[port].curBaud = baud;
2007 int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud)
2009 void __iomem *ofsAddr;
2013 if (moxa_ports[port].chkPort == 0 || termio == 0)
2015 ofsAddr = moxa_ports[port].tableAddr;
2016 cflag = termio->c_cflag; /* termio->c_cflag */
2018 mode = termio->c_cflag & CSIZE;
2021 else if (mode == CS6)
2023 else if (mode == CS7)
2025 else if (mode == CS8)
2028 if (termio->c_cflag & CSTOPB) {
2036 if (termio->c_cflag & PARENB) {
2037 if (termio->c_cflag & PARODD)
2044 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
2046 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2047 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2048 if (baud >= 921600L)
2051 MoxaPortSetBaud(port, baud);
2053 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
2054 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
2055 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
2056 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
2057 wait_finish(ofsAddr);
2063 int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState)
2066 if (!MoxaPortIsValid(port))
2069 if (moxa_ports[port].lineCtrl & DTR_ON)
2075 if (moxa_ports[port].lineCtrl & RTS_ON)
2083 void MoxaPortLineCtrl(int port, int dtr, int rts)
2085 void __iomem *ofsAddr;
2088 ofsAddr = moxa_ports[port].tableAddr;
2094 moxa_ports[port].lineCtrl = mode;
2095 moxafunc(ofsAddr, FC_LineControl, mode);
2098 void MoxaPortFlowCtrl(int port, int rts, int cts, int txflow, int rxflow, int txany)
2100 void __iomem *ofsAddr;
2103 ofsAddr = moxa_ports[port].tableAddr;
2106 mode |= RTS_FlowCtl;
2108 mode |= CTS_FlowCtl;
2115 moxafunc(ofsAddr, FC_SetFlowCtl, mode);
2118 int MoxaPortLineStatus(int port)
2120 void __iomem *ofsAddr;
2123 ofsAddr = moxa_ports[port].tableAddr;
2124 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2125 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2126 moxafunc(ofsAddr, FC_LineStatus, 0);
2127 val = readw(ofsAddr + FuncArg);
2129 val = readw(ofsAddr + FlagStat) >> 4;
2134 if ((moxa_ports[port].DCDState & DCD_oldstate) == 0)
2135 moxa_ports[port].DCDState = (DCD_oldstate | DCD_changed);
2137 if (moxa_ports[port].DCDState & DCD_oldstate)
2138 moxa_ports[port].DCDState = DCD_changed;
2144 int MoxaPortDCDChange(int port)
2148 if (moxa_ports[port].chkPort == 0)
2150 n = moxa_ports[port].DCDState;
2151 moxa_ports[port].DCDState &= ~DCD_changed;
2156 int MoxaPortDCDON(int port)
2160 if (moxa_ports[port].chkPort == 0)
2162 if (moxa_ports[port].DCDState & DCD_oldstate)
2169 int MoxaPortWriteData(int port, unsigned char * buffer, int len)
2174 ushort head, tx_mask, spage, epage;
2175 ushort pageno, pageofs, bufhead;
2176 void __iomem *baseAddr, *ofsAddr, *ofs;
2178 ofsAddr = moxa_ports[port].tableAddr;
2179 baseAddr = moxa_boards[port / MAX_PORTS_PER_BOARD].basemem;
2180 tx_mask = readw(ofsAddr + TX_mask);
2181 spage = readw(ofsAddr + Page_txb);
2182 epage = readw(ofsAddr + EndPage_txb);
2183 tail = readw(ofsAddr + TXwptr);
2184 head = readw(ofsAddr + TXrptr);
2185 c = (head > tail) ? (head - tail - 1)
2186 : (head - tail + tx_mask);
2189 moxaLog.txcnt[port] += c;
2191 if (spage == epage) {
2192 bufhead = readw(ofsAddr + Ofs_txb);
2193 writew(spage, baseAddr + Control_reg);
2196 len = head - tail - 1;
2198 len = tx_mask + 1 - tail;
2199 len = (c > len) ? len : c;
2200 ofs = baseAddr + DynPage_addr + bufhead + tail;
2201 for (i = 0; i < len; i++)
2202 writeb(*buffer++, ofs + i);
2203 tail = (tail + len) & tx_mask;
2206 writew(tail, ofsAddr + TXwptr);
2209 pageno = spage + (tail >> 13);
2210 pageofs = tail & Page_mask;
2212 cnt = Page_size - pageofs;
2216 writeb(pageno, baseAddr + Control_reg);
2217 ofs = baseAddr + DynPage_addr + pageofs;
2218 for (i = 0; i < cnt; i++)
2219 writeb(*buffer++, ofs + i);
2221 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2224 if (++pageno == epage)
2229 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2233 int MoxaPortReadData(int port, struct tty_struct *tty)
2235 register ushort head, pageofs;
2236 int i, count, cnt, len, total, remain;
2237 ushort tail, rx_mask, spage, epage;
2238 ushort pageno, bufhead;
2239 void __iomem *baseAddr, *ofsAddr, *ofs;
2241 ofsAddr = moxa_ports[port].tableAddr;
2242 baseAddr = moxa_boards[port / MAX_PORTS_PER_BOARD].basemem;
2243 head = readw(ofsAddr + RXrptr);
2244 tail = readw(ofsAddr + RXwptr);
2245 rx_mask = readw(ofsAddr + RX_mask);
2246 spage = readw(ofsAddr + Page_rxb);
2247 epage = readw(ofsAddr + EndPage_rxb);
2248 count = (tail >= head) ? (tail - head)
2249 : (tail - head + rx_mask + 1);
2254 remain = count - total;
2255 moxaLog.rxcnt[port] += total;
2257 if (spage == epage) {
2258 bufhead = readw(ofsAddr + Ofs_rxb);
2259 writew(spage, baseAddr + Control_reg);
2264 len = rx_mask + 1 - head;
2265 len = (count > len) ? len : count;
2266 ofs = baseAddr + DynPage_addr + bufhead + head;
2267 for (i = 0; i < len; i++)
2268 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2269 head = (head + len) & rx_mask;
2272 writew(head, ofsAddr + RXrptr);
2275 pageno = spage + (head >> 13);
2276 pageofs = head & Page_mask;
2278 cnt = Page_size - pageofs;
2282 writew(pageno, baseAddr + Control_reg);
2283 ofs = baseAddr + DynPage_addr + pageofs;
2284 for (i = 0; i < cnt; i++)
2285 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2287 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2290 if (++pageno == epage)
2295 if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2296 moxaLowWaterChk = 1;
2297 moxa_ports[port].lowChkFlag = 1;
2303 int MoxaPortTxQueue(int port)
2305 void __iomem *ofsAddr;
2306 ushort rptr, wptr, mask;
2309 ofsAddr = moxa_ports[port].tableAddr;
2310 rptr = readw(ofsAddr + TXrptr);
2311 wptr = readw(ofsAddr + TXwptr);
2312 mask = readw(ofsAddr + TX_mask);
2313 len = (wptr - rptr) & mask;
2317 int MoxaPortTxFree(int port)
2319 void __iomem *ofsAddr;
2320 ushort rptr, wptr, mask;
2323 ofsAddr = moxa_ports[port].tableAddr;
2324 rptr = readw(ofsAddr + TXrptr);
2325 wptr = readw(ofsAddr + TXwptr);
2326 mask = readw(ofsAddr + TX_mask);
2327 len = mask - ((wptr - rptr) & mask);
2331 int MoxaPortRxQueue(int port)
2333 void __iomem *ofsAddr;
2334 ushort rptr, wptr, mask;
2337 ofsAddr = moxa_ports[port].tableAddr;
2338 rptr = readw(ofsAddr + RXrptr);
2339 wptr = readw(ofsAddr + RXwptr);
2340 mask = readw(ofsAddr + RX_mask);
2341 len = (wptr - rptr) & mask;
2346 void MoxaPortTxDisable(int port)
2348 void __iomem *ofsAddr;
2350 ofsAddr = moxa_ports[port].tableAddr;
2351 moxafunc(ofsAddr, FC_SetXoffState, Magic_code);
2354 void MoxaPortTxEnable(int port)
2356 void __iomem *ofsAddr;
2358 ofsAddr = moxa_ports[port].tableAddr;
2359 moxafunc(ofsAddr, FC_SetXonState, Magic_code);
2363 int MoxaPortResetBrkCnt(int port)
2366 cnt = moxa_ports[port].breakCnt;
2367 moxa_ports[port].breakCnt = 0;
2372 void MoxaPortSendBreak(int port, int ms100)
2374 void __iomem *ofsAddr;
2376 ofsAddr = moxa_ports[port].tableAddr;
2378 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2379 moxadelay(ms100 * (HZ / 10));
2381 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2382 moxadelay(HZ / 4); /* 250 ms */
2384 moxafunc(ofsAddr, FC_StopBreak, Magic_code);
2387 static int moxa_get_serial_info(struct moxa_port *info,
2388 struct serial_struct __user *retinfo)
2390 struct serial_struct tmp;
2392 memset(&tmp, 0, sizeof(tmp));
2393 tmp.type = info->type;
2394 tmp.line = info->port;
2397 tmp.flags = info->asyncflags;
2398 tmp.baud_base = 921600;
2399 tmp.close_delay = info->close_delay;
2400 tmp.closing_wait = info->closing_wait;
2401 tmp.custom_divisor = 0;
2403 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2409 static int moxa_set_serial_info(struct moxa_port *info,
2410 struct serial_struct __user *new_info)
2412 struct serial_struct new_serial;
2414 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2417 if ((new_serial.irq != 0) ||
2418 (new_serial.port != 0) ||
2419 // (new_serial.type != info->type) ||
2420 (new_serial.custom_divisor != 0) ||
2421 (new_serial.baud_base != 921600))
2424 if (!capable(CAP_SYS_ADMIN)) {
2425 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2426 (info->asyncflags & ~ASYNC_USR_MASK)))
2429 info->close_delay = new_serial.close_delay * HZ / 100;
2430 info->closing_wait = new_serial.closing_wait * HZ / 100;
2433 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2434 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2436 if (new_serial.type == PORT_16550A) {
2437 MoxaSetFifo(info->port, 1);
2439 MoxaSetFifo(info->port, 0);
2442 info->type = new_serial.type;
2448 /*****************************************************************************
2449 * Static local functions: *
2450 *****************************************************************************/
2452 * moxadelay - delays a specified number ticks
2454 static void moxadelay(int tick)
2456 unsigned long st, et;
2460 while (time_before(jiffies, et));
2463 static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
2466 writew(arg, ofsAddr + FuncArg);
2467 writew(cmd, ofsAddr + FuncCode);
2468 wait_finish(ofsAddr);
2471 static void wait_finish(void __iomem *ofsAddr)
2476 while (readw(ofsAddr + FuncCode) != 0) {
2478 if ((j - i) > moxaFuncTout) {
2484 static void low_water_check(void __iomem *ofsAddr)
2487 ushort rptr, wptr, mask;
2489 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2490 rptr = readw(ofsAddr + RXrptr);
2491 wptr = readw(ofsAddr + RXwptr);
2492 mask = readw(ofsAddr + RX_mask);
2493 len = (wptr - rptr) & mask;
2494 if (len <= Low_water)
2495 moxafunc(ofsAddr, FC_SendXon, 0);
2499 static int moxaloadbios(int cardno, unsigned char __user *tmp, int len)
2501 void __iomem *baseAddr;
2504 if(copy_from_user(moxaBuff, tmp, len))
2506 baseAddr = moxa_boards[cardno].basemem;
2507 writeb(HW_reset, baseAddr + Control_reg); /* reset */
2508 moxadelay(1); /* delay 10 ms */
2509 for (i = 0; i < 4096; i++)
2510 writeb(0, baseAddr + i); /* clear fix page */
2511 for (i = 0; i < len; i++)
2512 writeb(moxaBuff[i], baseAddr + i); /* download BIOS */
2513 writeb(0, baseAddr + Control_reg); /* restart */
2517 static int moxafindcard(int cardno)
2519 void __iomem *baseAddr;
2522 baseAddr = moxa_boards[cardno].basemem;
2523 switch (moxa_boards[cardno].boardType) {
2524 case MOXA_BOARD_C218_ISA:
2525 case MOXA_BOARD_C218_PCI:
2526 if ((tmp = readw(baseAddr + C218_key)) != C218_KeyCode) {
2530 case MOXA_BOARD_CP204J:
2531 if ((tmp = readw(baseAddr + C218_key)) != CP204J_KeyCode) {
2536 if ((tmp = readw(baseAddr + C320_key)) != C320_KeyCode) {
2539 if ((tmp = readw(baseAddr + C320_status)) != STS_init) {
2546 static int moxaload320b(int cardno, unsigned char __user *tmp, int len)
2548 void __iomem *baseAddr;
2551 if(len > sizeof(moxaBuff))
2553 if(copy_from_user(moxaBuff, tmp, len))
2555 baseAddr = moxa_boards[cardno].basemem;
2556 writew(len - 7168 - 2, baseAddr + C320bapi_len);
2557 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
2558 for (i = 0; i < 7168; i++)
2559 writeb(moxaBuff[i], baseAddr + DynPage_addr + i);
2560 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
2561 for (i = 0; i < (len - 7168); i++)
2562 writeb(moxaBuff[i + 7168], baseAddr + DynPage_addr + i);
2566 static int moxaloadcode(int cardno, unsigned char __user *tmp, int len)
2568 void __iomem *baseAddr, *ofsAddr;
2569 int retval, port, i;
2571 if(copy_from_user(moxaBuff, tmp, len))
2573 baseAddr = moxa_boards[cardno].basemem;
2574 switch (moxa_boards[cardno].boardType) {
2575 case MOXA_BOARD_C218_ISA:
2576 case MOXA_BOARD_C218_PCI:
2577 case MOXA_BOARD_CP204J:
2578 retval = moxaloadc218(cardno, baseAddr, len);
2581 port = cardno * MAX_PORTS_PER_BOARD;
2582 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2583 struct moxa_port *p = &moxa_ports[port];
2588 p->tableAddr = baseAddr + Extern_table + Extern_size * i;
2589 ofsAddr = p->tableAddr;
2590 writew(C218rx_mask, ofsAddr + RX_mask);
2591 writew(C218tx_mask, ofsAddr + TX_mask);
2592 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
2593 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
2595 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
2596 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
2601 retval = moxaloadc320(cardno, baseAddr, len,
2602 &moxa_boards[cardno].numPorts);
2605 port = cardno * MAX_PORTS_PER_BOARD;
2606 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2607 struct moxa_port *p = &moxa_ports[port];
2612 p->tableAddr = baseAddr + Extern_table + Extern_size * i;
2613 ofsAddr = p->tableAddr;
2614 if (moxa_boards[cardno].numPorts == 8) {
2615 writew(C320p8rx_mask, ofsAddr + RX_mask);
2616 writew(C320p8tx_mask, ofsAddr + TX_mask);
2617 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
2618 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
2619 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
2620 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
2622 } else if (moxa_boards[cardno].numPorts == 16) {
2623 writew(C320p16rx_mask, ofsAddr + RX_mask);
2624 writew(C320p16tx_mask, ofsAddr + TX_mask);
2625 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
2626 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
2627 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
2628 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
2630 } else if (moxa_boards[cardno].numPorts == 24) {
2631 writew(C320p24rx_mask, ofsAddr + RX_mask);
2632 writew(C320p24tx_mask, ofsAddr + TX_mask);
2633 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
2634 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
2635 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
2636 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2637 } else if (moxa_boards[cardno].numPorts == 32) {
2638 writew(C320p32rx_mask, ofsAddr + RX_mask);
2639 writew(C320p32tx_mask, ofsAddr + TX_mask);
2640 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
2641 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
2642 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
2643 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
2644 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2649 moxa_boards[cardno].loadstat = 1;
2653 static int moxaloadc218(int cardno, void __iomem *baseAddr, int len)
2656 int i, j, len1, len2;
2657 ushort usum, *ptr, keycode;
2659 if (moxa_boards[cardno].boardType == MOXA_BOARD_CP204J)
2660 keycode = CP204J_KeyCode;
2662 keycode = C218_KeyCode;
2665 ptr = (ushort *) moxaBuff;
2666 for (i = 0; i < len1; i++)
2667 usum += le16_to_cpu(*(ptr + i));
2673 len2 = (len1 > 2048) ? 2048 : len1;
2675 for (i = 0; i < len2 << 1; i++)
2676 writeb(moxaBuff[i + j], baseAddr + C218_LoadBuf + i);
2679 writew(len2, baseAddr + C218DLoad_len);
2680 writew(0, baseAddr + C218_key);
2681 for (i = 0; i < 100; i++) {
2682 if (readw(baseAddr + C218_key) == keycode)
2684 moxadelay(1); /* delay 10 ms */
2686 if (readw(baseAddr + C218_key) != keycode) {
2690 writew(0, baseAddr + C218DLoad_len);
2691 writew(usum, baseAddr + C218check_sum);
2692 writew(0, baseAddr + C218_key);
2693 for (i = 0; i < 100; i++) {
2694 if (readw(baseAddr + C218_key) == keycode)
2696 moxadelay(1); /* delay 10 ms */
2699 } while ((readb(baseAddr + C218chksum_ok) != 1) && (retry < 3));
2700 if (readb(baseAddr + C218chksum_ok) != 1) {
2703 writew(0, baseAddr + C218_key);
2704 for (i = 0; i < 100; i++) {
2705 if (readw(baseAddr + Magic_no) == Magic_code)
2707 moxadelay(1); /* delay 10 ms */
2709 if (readw(baseAddr + Magic_no) != Magic_code) {
2712 writew(1, baseAddr + Disable_IRQ);
2713 writew(0, baseAddr + Magic_no);
2714 for (i = 0; i < 100; i++) {
2715 if (readw(baseAddr + Magic_no) == Magic_code)
2717 moxadelay(1); /* delay 10 ms */
2719 if (readw(baseAddr + Magic_no) != Magic_code) {
2723 moxa_boards[cardno].intNdx = baseAddr + IRQindex;
2724 moxa_boards[cardno].intPend = baseAddr + IRQpending;
2725 moxa_boards[cardno].intTable = baseAddr + IRQtable;
2729 static int moxaloadc320(int cardno, void __iomem *baseAddr, int len, int *numPorts)
2732 int i, j, wlen, len2, retry;
2737 uptr = (ushort *) moxaBuff;
2738 for (i = 0; i < wlen; i++)
2739 usum += le16_to_cpu(uptr[i]);
2750 for (i = 0; i < len2; i++)
2751 writeb(moxaBuff[j + i], baseAddr + C320_LoadBuf + i);
2754 writew(len2, baseAddr + C320DLoad_len);
2755 writew(0, baseAddr + C320_key);
2756 for (i = 0; i < 10; i++) {
2757 if (readw(baseAddr + C320_key) == C320_KeyCode)
2761 if (readw(baseAddr + C320_key) != C320_KeyCode)
2764 writew(0, baseAddr + C320DLoad_len);
2765 writew(usum, baseAddr + C320check_sum);
2766 writew(0, baseAddr + C320_key);
2767 for (i = 0; i < 10; i++) {
2768 if (readw(baseAddr + C320_key) == C320_KeyCode)
2773 } while ((readb(baseAddr + C320chksum_ok) != 1) && (retry < 3));
2774 if (readb(baseAddr + C320chksum_ok) != 1)
2776 writew(0, baseAddr + C320_key);
2777 for (i = 0; i < 600; i++) {
2778 if (readw(baseAddr + Magic_no) == Magic_code)
2782 if (readw(baseAddr + Magic_no) != Magic_code)
2785 if (moxa_boards[cardno].busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
2786 writew(0x3800, baseAddr + TMS320_PORT1);
2787 writew(0x3900, baseAddr + TMS320_PORT2);
2788 writew(28499, baseAddr + TMS320_CLOCK);
2790 writew(0x3200, baseAddr + TMS320_PORT1);
2791 writew(0x3400, baseAddr + TMS320_PORT2);
2792 writew(19999, baseAddr + TMS320_CLOCK);
2794 writew(1, baseAddr + Disable_IRQ);
2795 writew(0, baseAddr + Magic_no);
2796 for (i = 0; i < 500; i++) {
2797 if (readw(baseAddr + Magic_no) == Magic_code)
2801 if (readw(baseAddr + Magic_no) != Magic_code)
2804 j = readw(baseAddr + Module_cnt);
2808 writew(j, baseAddr + Module_no);
2809 writew(0, baseAddr + Magic_no);
2810 for (i = 0; i < 600; i++) {
2811 if (readw(baseAddr + Magic_no) == Magic_code)
2815 if (readw(baseAddr + Magic_no) != Magic_code)
2818 moxa_boards[cardno].intNdx = baseAddr + IRQindex;
2819 moxa_boards[cardno].intPend = baseAddr + IRQpending;
2820 moxa_boards[cardno].intTable = baseAddr + IRQtable;
2824 static void MoxaSetFifo(int port, int enable)
2826 void __iomem *ofsAddr = moxa_ports[port].tableAddr;
2829 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2830 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2832 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2833 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);