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;
149 struct mxser_mstatus GMStatus;
150 struct moxaq_str temp_queue;
154 void __iomem *tableAddr;
163 #define TXSTOPPED 0x1
165 #define EMPTYWAIT 0x4
168 #define SERIAL_DO_RESTART
170 #define WAKEUP_CHARS 256
172 static int verbose = 0;
173 static int ttymajor = MOXAMAJOR;
174 /* Variables for insmod */
176 static int baseaddr[4];
178 static int numports[4];
181 MODULE_AUTHOR("William Chen");
182 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
183 MODULE_LICENSE("GPL");
185 module_param_array(type, int, NULL, 0);
186 module_param_array(baseaddr, int, NULL, 0);
187 module_param_array(numports, int, NULL, 0);
189 module_param(ttymajor, int, 0);
190 module_param(verbose, bool, 0644);
195 static int moxa_open(struct tty_struct *, struct file *);
196 static void moxa_close(struct tty_struct *, struct file *);
197 static int moxa_write(struct tty_struct *, const unsigned char *, int);
198 static int moxa_write_room(struct tty_struct *);
199 static void moxa_flush_buffer(struct tty_struct *);
200 static int moxa_chars_in_buffer(struct tty_struct *);
201 static void moxa_flush_chars(struct tty_struct *);
202 static void moxa_put_char(struct tty_struct *, unsigned char);
203 static int moxa_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);
204 static void moxa_throttle(struct tty_struct *);
205 static void moxa_unthrottle(struct tty_struct *);
206 static void moxa_set_termios(struct tty_struct *, struct ktermios *);
207 static void moxa_stop(struct tty_struct *);
208 static void moxa_start(struct tty_struct *);
209 static void moxa_hangup(struct tty_struct *);
210 static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
211 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
212 unsigned int set, unsigned int clear);
213 static void moxa_poll(unsigned long);
214 static void set_tty_param(struct tty_struct *);
215 static int block_till_ready(struct tty_struct *, struct file *,
217 static void setup_empty_event(struct tty_struct *);
218 static void check_xmit_empty(unsigned long);
219 static void shut_down(struct moxa_port *);
220 static void receive_data(struct moxa_port *);
222 * moxa board interface functions:
224 static void MoxaDriverInit(void);
225 static int MoxaDriverIoctl(unsigned int, unsigned long, int);
226 static int MoxaDriverPoll(void);
227 static int MoxaPortsOfCard(int);
228 static int MoxaPortIsValid(int);
229 static void MoxaPortEnable(int);
230 static void MoxaPortDisable(int);
231 static long MoxaPortGetMaxBaud(int);
232 static long MoxaPortSetBaud(int, long);
233 static int MoxaPortSetTermio(int, struct ktermios *, speed_t);
234 static int MoxaPortGetLineOut(int, int *, int *);
235 static void MoxaPortLineCtrl(int, int, int);
236 static void MoxaPortFlowCtrl(int, int, int, int, int, int);
237 static int MoxaPortLineStatus(int);
238 static int MoxaPortDCDChange(int);
239 static int MoxaPortDCDON(int);
240 static void MoxaPortFlushData(int, int);
241 static int MoxaPortWriteData(int, unsigned char *, int);
242 static int MoxaPortReadData(int, struct tty_struct *tty);
243 static int MoxaPortTxQueue(int);
244 static int MoxaPortRxQueue(int);
245 static int MoxaPortTxFree(int);
246 static void MoxaPortTxDisable(int);
247 static void MoxaPortTxEnable(int);
248 static int MoxaPortResetBrkCnt(int);
249 static void MoxaPortSendBreak(int, int);
250 static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
251 static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
252 static void MoxaSetFifo(int port, int enable);
254 static const struct tty_operations moxa_ops = {
258 .write_room = moxa_write_room,
259 .flush_buffer = moxa_flush_buffer,
260 .chars_in_buffer = moxa_chars_in_buffer,
261 .flush_chars = moxa_flush_chars,
262 .put_char = moxa_put_char,
264 .throttle = moxa_throttle,
265 .unthrottle = moxa_unthrottle,
266 .set_termios = moxa_set_termios,
269 .hangup = moxa_hangup,
270 .tiocmget = moxa_tiocmget,
271 .tiocmset = moxa_tiocmset,
274 static struct tty_driver *moxaDriver;
275 static struct moxa_port moxa_ports[MAX_PORTS];
276 static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
277 static DEFINE_SPINLOCK(moxa_lock);
280 static int moxa_get_PCI_conf(struct pci_dev *p, int board_type,
281 struct moxa_board_conf *board)
283 board->baseAddr = pci_resource_start (p, 2);
284 board->boardType = board_type;
285 switch (board_type) {
286 case MOXA_BOARD_C218_ISA:
287 case MOXA_BOARD_C218_PCI:
291 case MOXA_BOARD_CP204J:
298 board->busType = MOXA_BUS_TYPE_PCI;
299 /* don't lose the reference in the next pci_get_device iteration */
300 board->pdev = pci_dev_get(p);
304 #endif /* CONFIG_PCI */
306 static int __init moxa_init(void)
309 struct moxa_port *ch;
311 printk(KERN_INFO "MOXA Intellio family driver version %s\n", MOXA_VERSION);
312 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
316 moxaDriver->owner = THIS_MODULE;
317 moxaDriver->name = "ttyMX";
318 moxaDriver->major = ttymajor;
319 moxaDriver->minor_start = 0;
320 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
321 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
322 moxaDriver->init_termios = tty_std_termios;
323 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
324 moxaDriver->init_termios.c_ispeed = 9600;
325 moxaDriver->init_termios.c_ospeed = 9600;
326 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
327 tty_set_operations(moxaDriver, &moxa_ops);
329 for (i = 0, ch = moxa_ports; i < MAX_PORTS; i++, ch++) {
330 ch->type = PORT_16550A;
332 ch->close_delay = 5 * HZ / 10;
333 ch->closing_wait = 30 * HZ;
334 ch->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
335 init_waitqueue_head(&ch->open_wait);
336 init_waitqueue_head(&ch->close_wait);
338 setup_timer(&ch->emptyTimer, check_xmit_empty,
342 printk("Tty devices major number = %d\n", ttymajor);
344 if (tty_register_driver(moxaDriver)) {
345 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
346 put_tty_driver(moxaDriver);
350 mod_timer(&moxaTimer, jiffies + HZ / 50);
352 /* Find the boards defined in source code */
354 for (i = 0; i < MAX_BOARDS; i++) {
355 if ((moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA) ||
356 (moxa_isa_boards[i].boardType == MOXA_BOARD_C320_ISA)) {
357 moxa_boards[numBoards].boardType = moxa_isa_boards[i].boardType;
358 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
359 moxa_boards[numBoards].numPorts = 8;
361 moxa_boards[numBoards].numPorts = moxa_isa_boards[i].numPorts;
362 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
363 moxa_boards[numBoards].baseAddr = moxa_isa_boards[i].baseAddr;
365 printk("Board %2d: %s board(baseAddr=%lx)\n",
367 moxa_brdname[moxa_boards[numBoards].boardType - 1],
368 moxa_boards[numBoards].baseAddr);
372 /* Find the boards defined form module args. */
374 for (i = 0; i < MAX_BOARDS; i++) {
375 if ((type[i] == MOXA_BOARD_C218_ISA) ||
376 (type[i] == MOXA_BOARD_C320_ISA)) {
378 printk("Board %2d: %s board(baseAddr=%lx)\n",
380 moxa_brdname[type[i] - 1],
381 (unsigned long) baseaddr[i]);
382 if (numBoards >= MAX_BOARDS) {
384 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
387 moxa_boards[numBoards].boardType = type[i];
388 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
389 moxa_boards[numBoards].numPorts = 8;
391 moxa_boards[numBoards].numPorts = numports[i];
392 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
393 moxa_boards[numBoards].baseAddr = baseaddr[i];
398 /* Find PCI boards here */
401 struct pci_dev *p = NULL;
402 int n = ARRAY_SIZE(moxa_pcibrds) - 1;
405 while ((p = pci_get_device(moxa_pcibrds[i].vendor, moxa_pcibrds[i].device, p))!=NULL)
407 if (pci_enable_device(p))
409 if (numBoards >= MAX_BOARDS) {
411 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
413 moxa_get_PCI_conf(p, moxa_pcibrds[i].driver_data,
414 &moxa_boards[numBoards]);
422 for (i = 0; i < numBoards; i++) {
423 moxa_boards[i].basemem = ioremap(moxa_boards[i].baseAddr,
430 static void __exit moxa_exit(void)
435 printk("Unloading module moxa ...\n");
437 del_timer_sync(&moxaTimer);
439 for (i = 0; i < MAX_PORTS; i++)
440 del_timer_sync(&moxa_ports[i].emptyTimer);
442 if (tty_unregister_driver(moxaDriver))
443 printk("Couldn't unregister MOXA Intellio family serial driver\n");
444 put_tty_driver(moxaDriver);
446 for (i = 0; i < MAX_BOARDS; i++) {
447 if (moxa_boards[i].basemem)
448 iounmap(moxa_boards[i].basemem);
449 if (moxa_boards[i].busType == MOXA_BUS_TYPE_PCI)
450 pci_dev_put(moxa_boards[i].pdev);
457 module_init(moxa_init);
458 module_exit(moxa_exit);
460 static int moxa_open(struct tty_struct *tty, struct file *filp)
462 struct moxa_port *ch;
467 if (port == MAX_PORTS) {
470 if (!MoxaPortIsValid(port)) {
471 tty->driver_data = NULL;
475 ch = &moxa_ports[port];
477 tty->driver_data = ch;
479 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
482 MoxaPortLineCtrl(ch->port, 1, 1);
483 MoxaPortEnable(ch->port);
484 ch->asyncflags |= ASYNC_INITIALIZED;
486 retval = block_till_ready(tty, filp, ch);
488 moxa_unthrottle(tty);
490 if (ch->type == PORT_16550A) {
491 MoxaSetFifo(ch->port, 1);
493 MoxaSetFifo(ch->port, 0);
499 static void moxa_close(struct tty_struct *tty, struct file *filp)
501 struct moxa_port *ch;
505 if (port == MAX_PORTS) {
508 if (!MoxaPortIsValid(port)) {
509 #ifdef SERIAL_DEBUG_CLOSE
510 printk("Invalid portno in moxa_close\n");
512 tty->driver_data = NULL;
515 if (tty->driver_data == NULL) {
518 if (tty_hung_up_p(filp)) {
521 ch = (struct moxa_port *) tty->driver_data;
523 if ((tty->count == 1) && (ch->count != 1)) {
524 printk("moxa_close: bad serial port count; tty->count is 1, "
525 "ch->count is %d\n", ch->count);
528 if (--ch->count < 0) {
529 printk("moxa_close: bad serial port count, device=%s\n",
536 ch->asyncflags |= ASYNC_CLOSING;
538 ch->cflag = tty->termios->c_cflag;
539 if (ch->asyncflags & ASYNC_INITIALIZED) {
540 setup_empty_event(tty);
541 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
542 del_timer_sync(&moxa_ports[ch->port].emptyTimer);
545 MoxaPortFlushData(port, 2);
547 if (tty->driver->flush_buffer)
548 tty->driver->flush_buffer(tty);
549 tty_ldisc_flush(tty);
554 if (ch->blocked_open) {
555 if (ch->close_delay) {
556 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
558 wake_up_interruptible(&ch->open_wait);
560 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
561 wake_up_interruptible(&ch->close_wait);
564 static int moxa_write(struct tty_struct *tty,
565 const unsigned char *buf, int count)
567 struct moxa_port *ch;
571 ch = (struct moxa_port *) tty->driver_data;
576 spin_lock_irqsave(&moxa_lock, flags);
577 len = MoxaPortWriteData(port, (unsigned char *) buf, count);
578 spin_unlock_irqrestore(&moxa_lock, flags);
580 /*********************************************
581 if ( !(ch->statusflags & LOWWAIT) &&
582 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
583 ************************************************/
584 ch->statusflags |= LOWWAIT;
588 static int moxa_write_room(struct tty_struct *tty)
590 struct moxa_port *ch;
594 ch = (struct moxa_port *) tty->driver_data;
597 return (MoxaPortTxFree(ch->port));
600 static void moxa_flush_buffer(struct tty_struct *tty)
602 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
606 MoxaPortFlushData(ch->port, 1);
610 static int moxa_chars_in_buffer(struct tty_struct *tty)
613 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
616 * Sigh...I have to check if driver_data is NULL here, because
617 * if an open() fails, the TTY subsystem eventually calls
618 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
619 * routine. And since the open() failed, we return 0 here. TDJ
623 chars = MoxaPortTxQueue(ch->port);
626 * Make it possible to wakeup anything waiting for output
627 * in tty_ioctl.c, etc.
629 if (!(ch->statusflags & EMPTYWAIT))
630 setup_empty_event(tty);
635 static void moxa_flush_chars(struct tty_struct *tty)
638 * Don't think I need this, because this is called to empty the TX
639 * buffer for the 16450, 16550, etc.
643 static void moxa_put_char(struct tty_struct *tty, unsigned char c)
645 struct moxa_port *ch;
649 ch = (struct moxa_port *) tty->driver_data;
653 spin_lock_irqsave(&moxa_lock, flags);
654 MoxaPortWriteData(port, &c, 1);
655 spin_unlock_irqrestore(&moxa_lock, flags);
656 /************************************************
657 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
658 *************************************************/
659 ch->statusflags |= LOWWAIT;
662 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
664 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
666 int flag = 0, dtr, rts;
669 if ((port != MAX_PORTS) && (!ch))
672 MoxaPortGetLineOut(ch->port, &dtr, &rts);
677 dtr = MoxaPortLineStatus(ch->port);
687 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
688 unsigned int set, unsigned int clear)
690 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
695 if ((port != MAX_PORTS) && (!ch))
698 MoxaPortGetLineOut(ch->port, &dtr, &rts);
703 if (clear & TIOCM_RTS)
705 if (clear & TIOCM_DTR)
707 MoxaPortLineCtrl(ch->port, dtr, rts);
711 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
712 unsigned int cmd, unsigned long arg)
714 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
716 void __user *argp = (void __user *)arg;
720 if ((port != MAX_PORTS) && (!ch))
724 case TCSBRK: /* SVID version: non-zero arg --> no break */
725 retval = tty_check_change(tty);
728 setup_empty_event(tty);
729 tty_wait_until_sent(tty, 0);
731 MoxaPortSendBreak(ch->port, 0);
733 case TCSBRKP: /* support for POSIX tcsendbreak() */
734 retval = tty_check_change(tty);
737 setup_empty_event(tty);
738 tty_wait_until_sent(tty, 0);
739 MoxaPortSendBreak(ch->port, arg);
742 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) argp);
744 if(get_user(retval, (unsigned long __user *) argp))
747 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
750 ch->asyncflags &= ~ASYNC_CHECK_CD;
752 ch->asyncflags |= ASYNC_CHECK_CD;
755 return moxa_get_serial_info(ch, argp);
758 return moxa_set_serial_info(ch, argp);
760 retval = MoxaDriverIoctl(cmd, arg, port);
765 static void moxa_throttle(struct tty_struct *tty)
767 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
769 ch->statusflags |= THROTTLE;
772 static void moxa_unthrottle(struct tty_struct *tty)
774 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
776 ch->statusflags &= ~THROTTLE;
779 static void moxa_set_termios(struct tty_struct *tty,
780 struct ktermios *old_termios)
782 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
787 if (!(old_termios->c_cflag & CLOCAL) &&
788 (tty->termios->c_cflag & CLOCAL))
789 wake_up_interruptible(&ch->open_wait);
792 static void moxa_stop(struct tty_struct *tty)
794 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
798 MoxaPortTxDisable(ch->port);
799 ch->statusflags |= TXSTOPPED;
803 static void moxa_start(struct tty_struct *tty)
805 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
810 if (!(ch->statusflags & TXSTOPPED))
813 MoxaPortTxEnable(ch->port);
814 ch->statusflags &= ~TXSTOPPED;
817 static void moxa_hangup(struct tty_struct *tty)
819 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
821 moxa_flush_buffer(tty);
825 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
827 wake_up_interruptible(&ch->open_wait);
830 static void moxa_poll(unsigned long ignored)
833 struct moxa_port *ch;
834 struct tty_struct *tp;
837 del_timer(&moxaTimer);
839 if (MoxaDriverPoll() < 0) {
840 mod_timer(&moxaTimer, jiffies + HZ / 50);
843 for (card = 0; card < MAX_BOARDS; card++) {
844 if ((ports = MoxaPortsOfCard(card)) <= 0)
846 ch = &moxa_ports[card * MAX_PORTS_PER_BOARD];
847 for (i = 0; i < ports; i++, ch++) {
848 if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
850 if (!(ch->statusflags & THROTTLE) &&
851 (MoxaPortRxQueue(ch->port) > 0))
853 if ((tp = ch->tty) == 0)
855 if (ch->statusflags & LOWWAIT) {
856 if (MoxaPortTxQueue(ch->port) <= WAKEUP_CHARS) {
858 ch->statusflags &= ~LOWWAIT;
863 if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch->port) > 0)) {
864 tty_insert_flip_char(tp, 0, TTY_BREAK);
865 tty_schedule_flip(tp);
867 if (MoxaPortDCDChange(ch->port)) {
868 if (ch->asyncflags & ASYNC_CHECK_CD) {
869 if (MoxaPortDCDON(ch->port))
870 wake_up_interruptible(&ch->open_wait);
873 wake_up_interruptible(&ch->open_wait);
874 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
881 mod_timer(&moxaTimer, jiffies + HZ / 50);
884 /******************************************************************************/
886 static void set_tty_param(struct tty_struct *tty)
888 register struct ktermios *ts;
889 struct moxa_port *ch;
890 int rts, cts, txflow, rxflow, xany;
892 ch = (struct moxa_port *) tty->driver_data;
894 if (ts->c_cflag & CLOCAL)
895 ch->asyncflags &= ~ASYNC_CHECK_CD;
897 ch->asyncflags |= ASYNC_CHECK_CD;
898 rts = cts = txflow = rxflow = xany = 0;
899 if (ts->c_cflag & CRTSCTS)
901 if (ts->c_iflag & IXON)
903 if (ts->c_iflag & IXOFF)
905 if (ts->c_iflag & IXANY)
907 MoxaPortFlowCtrl(ch->port, rts, cts, txflow, rxflow, xany);
908 MoxaPortSetTermio(ch->port, ts, tty_get_baud_rate(tty));
911 static int block_till_ready(struct tty_struct *tty, struct file *filp,
912 struct moxa_port *ch)
914 DECLARE_WAITQUEUE(wait,current);
917 int do_clocal = C_CLOCAL(tty);
920 * If the device is in the middle of being closed, then block
921 * until it's done, and then try again.
923 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
924 if (ch->asyncflags & ASYNC_CLOSING)
925 interruptible_sleep_on(&ch->close_wait);
926 #ifdef SERIAL_DO_RESTART
927 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
930 return (-ERESTARTSYS);
936 * If non-blocking mode is set, then make the check up front
939 if (filp->f_flags & O_NONBLOCK) {
940 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
944 * Block waiting for the carrier detect and the line to become free
947 add_wait_queue(&ch->open_wait, &wait);
948 #ifdef SERIAL_DEBUG_OPEN
949 printk("block_til_ready before block: ttys%d, count = %d\n",
950 ch->line, ch->count);
952 spin_lock_irqsave(&moxa_lock, flags);
953 if (!tty_hung_up_p(filp))
956 spin_unlock_irqrestore(&moxa_lock, flags);
959 set_current_state(TASK_INTERRUPTIBLE);
960 if (tty_hung_up_p(filp) ||
961 !(ch->asyncflags & ASYNC_INITIALIZED)) {
962 #ifdef SERIAL_DO_RESTART
963 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
966 retval = -ERESTARTSYS;
972 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
973 MoxaPortDCDON(ch->port)))
976 if (signal_pending(current)) {
977 retval = -ERESTARTSYS;
982 set_current_state(TASK_RUNNING);
983 remove_wait_queue(&ch->open_wait, &wait);
985 spin_lock_irqsave(&moxa_lock, flags);
986 if (!tty_hung_up_p(filp))
989 spin_unlock_irqrestore(&moxa_lock, flags);
990 #ifdef SERIAL_DEBUG_OPEN
991 printk("block_til_ready after blocking: ttys%d, count = %d\n",
992 ch->line, ch->count);
996 /* FIXME: review to see if we need to use set_bit on these */
997 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1001 static void setup_empty_event(struct tty_struct *tty)
1003 struct moxa_port *ch = tty->driver_data;
1004 unsigned long flags;
1006 spin_lock_irqsave(&moxa_lock, flags);
1007 ch->statusflags |= EMPTYWAIT;
1008 mod_timer(&moxa_ports[ch->port].emptyTimer, jiffies + HZ);
1009 spin_unlock_irqrestore(&moxa_lock, flags);
1012 static void check_xmit_empty(unsigned long data)
1014 struct moxa_port *ch;
1016 ch = (struct moxa_port *) data;
1017 del_timer_sync(&moxa_ports[ch->port].emptyTimer);
1018 if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
1019 if (MoxaPortTxQueue(ch->port) == 0) {
1020 ch->statusflags &= ~EMPTYWAIT;
1021 tty_wakeup(ch->tty);
1024 mod_timer(&moxa_ports[ch->port].emptyTimer, jiffies + HZ);
1026 ch->statusflags &= ~EMPTYWAIT;
1029 static void shut_down(struct moxa_port *ch)
1031 struct tty_struct *tp;
1033 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1038 MoxaPortDisable(ch->port);
1041 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1043 if (tp->termios->c_cflag & HUPCL)
1044 MoxaPortLineCtrl(ch->port, 0, 0);
1046 ch->asyncflags &= ~ASYNC_INITIALIZED;
1049 static void receive_data(struct moxa_port *ch)
1051 struct tty_struct *tp;
1052 struct ktermios *ts;
1053 unsigned long flags;
1059 /**************************************************
1060 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1061 *****************************************************/
1063 MoxaPortFlushData(ch->port, 0);
1066 spin_lock_irqsave(&moxa_lock, flags);
1067 MoxaPortReadData(ch->port, tp);
1068 spin_unlock_irqrestore(&moxa_lock, flags);
1069 tty_schedule_flip(tp);
1072 #define Magic_code 0x404
1075 * System Configuration
1078 * for C218 BIOS initialization
1080 #define C218_ConfBase 0x800
1081 #define C218_status (C218_ConfBase + 0) /* BIOS running status */
1082 #define C218_diag (C218_ConfBase + 2) /* diagnostic status */
1083 #define C218_key (C218_ConfBase + 4) /* WORD (0x218 for C218) */
1084 #define C218DLoad_len (C218_ConfBase + 6) /* WORD */
1085 #define C218check_sum (C218_ConfBase + 8) /* BYTE */
1086 #define C218chksum_ok (C218_ConfBase + 0x0a) /* BYTE (1:ok) */
1087 #define C218_TestRx (C218_ConfBase + 0x10) /* 8 bytes for 8 ports */
1088 #define C218_TestTx (C218_ConfBase + 0x18) /* 8 bytes for 8 ports */
1089 #define C218_RXerr (C218_ConfBase + 0x20) /* 8 bytes for 8 ports */
1090 #define C218_ErrFlag (C218_ConfBase + 0x28) /* 8 bytes for 8 ports */
1092 #define C218_LoadBuf 0x0F00
1093 #define C218_KeyCode 0x218
1094 #define CP204J_KeyCode 0x204
1097 * for C320 BIOS initialization
1099 #define C320_ConfBase 0x800
1100 #define C320_LoadBuf 0x0f00
1101 #define STS_init 0x05 /* for C320_status */
1103 #define C320_status C320_ConfBase + 0 /* BIOS running status */
1104 #define C320_diag C320_ConfBase + 2 /* diagnostic status */
1105 #define C320_key C320_ConfBase + 4 /* WORD (0320H for C320) */
1106 #define C320DLoad_len C320_ConfBase + 6 /* WORD */
1107 #define C320check_sum C320_ConfBase + 8 /* WORD */
1108 #define C320chksum_ok C320_ConfBase + 0x0a /* WORD (1:ok) */
1109 #define C320bapi_len C320_ConfBase + 0x0c /* WORD */
1110 #define C320UART_no C320_ConfBase + 0x0e /* WORD */
1112 #define C320_KeyCode 0x320
1114 #define FixPage_addr 0x0000 /* starting addr of static page */
1115 #define DynPage_addr 0x2000 /* starting addr of dynamic page */
1116 #define C218_start 0x3000 /* starting addr of C218 BIOS prg */
1117 #define Control_reg 0x1ff0 /* select page and reset control */
1118 #define HW_reset 0x80
1123 #define FC_CardReset 0x80
1124 #define FC_ChannelReset 1 /* C320 firmware not supported */
1125 #define FC_EnableCH 2
1126 #define FC_DisableCH 3
1127 #define FC_SetParam 4
1128 #define FC_SetMode 5
1129 #define FC_SetRate 6
1130 #define FC_LineControl 7
1131 #define FC_LineStatus 8
1132 #define FC_XmitControl 9
1133 #define FC_FlushQueue 10
1134 #define FC_SendBreak 11
1135 #define FC_StopBreak 12
1136 #define FC_LoopbackON 13
1137 #define FC_LoopbackOFF 14
1138 #define FC_ClrIrqTable 15
1139 #define FC_SendXon 16
1140 #define FC_SetTermIrq 17 /* C320 firmware not supported */
1141 #define FC_SetCntIrq 18 /* C320 firmware not supported */
1142 #define FC_SetBreakIrq 19
1143 #define FC_SetLineIrq 20
1144 #define FC_SetFlowCtl 21
1145 #define FC_GenIrq 22
1146 #define FC_InCD180 23
1147 #define FC_OutCD180 24
1148 #define FC_InUARTreg 23
1149 #define FC_OutUARTreg 24
1150 #define FC_SetXonXoff 25
1151 #define FC_OutCD180CCR 26
1152 #define FC_ExtIQueue 27
1153 #define FC_ExtOQueue 28
1154 #define FC_ClrLineIrq 29
1155 #define FC_HWFlowCtl 30
1156 #define FC_GetClockRate 35
1157 #define FC_SetBaud 36
1158 #define FC_SetDataMode 41
1159 #define FC_GetCCSR 43
1160 #define FC_GetDataError 45
1161 #define FC_RxControl 50
1162 #define FC_ImmSend 51
1163 #define FC_SetXonState 52
1164 #define FC_SetXoffState 53
1165 #define FC_SetRxFIFOTrig 54
1166 #define FC_SetTxFIFOCnt 55
1167 #define FC_UnixRate 56
1168 #define FC_UnixResetTimer 57
1170 #define RxFIFOTrig1 0
1171 #define RxFIFOTrig4 1
1172 #define RxFIFOTrig8 2
1173 #define RxFIFOTrig14 3
1178 #define DRAM_global 0
1179 #define INT_data (DRAM_global + 0)
1180 #define Config_base (DRAM_global + 0x108)
1182 #define IRQindex (INT_data + 0)
1183 #define IRQpending (INT_data + 4)
1184 #define IRQtable (INT_data + 8)
1189 #define IntrRx 0x01 /* receiver data O.K. */
1190 #define IntrTx 0x02 /* transmit buffer empty */
1191 #define IntrFunc 0x04 /* function complete */
1192 #define IntrBreak 0x08 /* received break */
1193 #define IntrLine 0x10 /* line status change
1195 #define IntrIntr 0x20 /* received INTR code */
1196 #define IntrQuit 0x40 /* received QUIT code */
1197 #define IntrEOF 0x80 /* received EOF code */
1199 #define IntrRxTrigger 0x100 /* rx data count reach tigger value */
1200 #define IntrTxTrigger 0x200 /* tx data count below trigger value */
1202 #define Magic_no (Config_base + 0)
1203 #define Card_model_no (Config_base + 2)
1204 #define Total_ports (Config_base + 4)
1205 #define Module_cnt (Config_base + 8)
1206 #define Module_no (Config_base + 10)
1207 #define Timer_10ms (Config_base + 14)
1208 #define Disable_IRQ (Config_base + 20)
1209 #define TMS320_PORT1 (Config_base + 22)
1210 #define TMS320_PORT2 (Config_base + 24)
1211 #define TMS320_CLOCK (Config_base + 26)
1214 * DATA BUFFER in DRAM
1216 #define Extern_table 0x400 /* Base address of the external table
1217 (24 words * 64) total 3K bytes
1218 (24 words * 128) total 6K bytes */
1219 #define Extern_size 0x60 /* 96 bytes */
1220 #define RXrptr 0x00 /* read pointer for RX buffer */
1221 #define RXwptr 0x02 /* write pointer for RX buffer */
1222 #define TXrptr 0x04 /* read pointer for TX buffer */
1223 #define TXwptr 0x06 /* write pointer for TX buffer */
1224 #define HostStat 0x08 /* IRQ flag and general flag */
1225 #define FlagStat 0x0A
1226 #define FlowControl 0x0C /* B7 B6 B5 B4 B3 B2 B1 B0 */
1227 /* x x x x | | | | */
1228 /* | | | + CTS flow */
1229 /* | | +--- RTS flow */
1230 /* | +------ TX Xon/Xoff */
1231 /* +--------- RX Xon/Xoff */
1232 #define Break_cnt 0x0E /* received break count */
1233 #define CD180TXirq 0x10 /* if non-0: enable TX irq */
1234 #define RX_mask 0x12
1235 #define TX_mask 0x14
1236 #define Ofs_rxb 0x16
1237 #define Ofs_txb 0x18
1238 #define Page_rxb 0x1A
1239 #define Page_txb 0x1C
1240 #define EndPage_rxb 0x1E
1241 #define EndPage_txb 0x20
1242 #define Data_error 0x22
1243 #define RxTrigger 0x28
1244 #define TxTrigger 0x2a
1246 #define rRXwptr 0x34
1247 #define Low_water 0x36
1249 #define FuncCode 0x40
1250 #define FuncArg 0x42
1251 #define FuncArg1 0x44
1253 #define C218rx_size 0x2000 /* 8K bytes */
1254 #define C218tx_size 0x8000 /* 32K bytes */
1256 #define C218rx_mask (C218rx_size - 1)
1257 #define C218tx_mask (C218tx_size - 1)
1259 #define C320p8rx_size 0x2000
1260 #define C320p8tx_size 0x8000
1261 #define C320p8rx_mask (C320p8rx_size - 1)
1262 #define C320p8tx_mask (C320p8tx_size - 1)
1264 #define C320p16rx_size 0x2000
1265 #define C320p16tx_size 0x4000
1266 #define C320p16rx_mask (C320p16rx_size - 1)
1267 #define C320p16tx_mask (C320p16tx_size - 1)
1269 #define C320p24rx_size 0x2000
1270 #define C320p24tx_size 0x2000
1271 #define C320p24rx_mask (C320p24rx_size - 1)
1272 #define C320p24tx_mask (C320p24tx_size - 1)
1274 #define C320p32rx_size 0x1000
1275 #define C320p32tx_size 0x1000
1276 #define C320p32rx_mask (C320p32rx_size - 1)
1277 #define C320p32tx_mask (C320p32tx_size - 1)
1279 #define Page_size 0x2000
1280 #define Page_mask (Page_size - 1)
1281 #define C218rx_spage 3
1282 #define C218tx_spage 4
1283 #define C218rx_pageno 1
1284 #define C218tx_pageno 4
1285 #define C218buf_pageno 5
1287 #define C320p8rx_spage 3
1288 #define C320p8tx_spage 4
1289 #define C320p8rx_pgno 1
1290 #define C320p8tx_pgno 4
1291 #define C320p8buf_pgno 5
1293 #define C320p16rx_spage 3
1294 #define C320p16tx_spage 4
1295 #define C320p16rx_pgno 1
1296 #define C320p16tx_pgno 2
1297 #define C320p16buf_pgno 3
1299 #define C320p24rx_spage 3
1300 #define C320p24tx_spage 4
1301 #define C320p24rx_pgno 1
1302 #define C320p24tx_pgno 1
1303 #define C320p24buf_pgno 2
1305 #define C320p32rx_spage 3
1306 #define C320p32tx_ofs C320p32rx_size
1307 #define C320p32tx_spage 3
1308 #define C320p32buf_pgno 1
1313 #define WakeupRx 0x01
1314 #define WakeupTx 0x02
1315 #define WakeupBreak 0x08
1316 #define WakeupLine 0x10
1317 #define WakeupIntr 0x20
1318 #define WakeupQuit 0x40
1319 #define WakeupEOF 0x80 /* used in VTIME control */
1320 #define WakeupRxTrigger 0x100
1321 #define WakeupTxTrigger 0x200
1325 #define Rx_over 0x01
1326 #define Xoff_state 0x02
1327 #define Tx_flowOff 0x04
1328 #define Tx_enable 0x08
1329 #define CTS_state 0x10
1330 #define DSR_state 0x20
1331 #define DCD_state 0x80
1335 #define CTS_FlowCtl 1
1336 #define RTS_FlowCtl 2
1337 #define Tx_FlowCtl 4
1338 #define Rx_FlowCtl 8
1339 #define IXM_IXANY 0x10
1341 #define LowWater 128
1349 /* mode definition */
1355 #define MX_STOP1 0x00
1356 #define MX_STOP15 0x04
1357 #define MX_STOP2 0x08
1359 #define MX_PARNONE 0x00
1360 #define MX_PAREVEN 0x40
1361 #define MX_PARODD 0xC0
1369 int rxcnt[MAX_PORTS];
1370 int txcnt[MAX_PORTS];
1373 #define DCD_changed 0x01
1374 #define DCD_oldstate 0x80
1376 static unsigned char moxaBuff[10240];
1377 static int moxaLowWaterChk;
1378 static int moxaCard;
1379 static struct mon_str moxaLog;
1380 static int moxaFuncTout = HZ / 2;
1382 static void moxadelay(int);
1383 static void moxafunc(void __iomem *, int, ushort);
1384 static void wait_finish(void __iomem *);
1385 static void low_water_check(void __iomem *);
1386 static int moxaloadbios(int, unsigned char __user *, int);
1387 static int moxafindcard(int);
1388 static int moxaload320b(int, unsigned char __user *, int);
1389 static int moxaloadcode(int, unsigned char __user *, int);
1390 static int moxaloadc218(int, void __iomem *, int);
1391 static int moxaloadc320(int, void __iomem *, int, int *);
1393 /*****************************************************************************
1394 * Driver level functions: *
1395 * 1. MoxaDriverInit(void); *
1396 * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
1397 * 3. MoxaDriverPoll(void); *
1398 *****************************************************************************/
1399 void MoxaDriverInit(void)
1401 struct moxa_port *p;
1404 moxaFuncTout = HZ / 2; /* 500 mini-seconds */
1407 moxaLowWaterChk = 0;
1408 for (i = 0; i < MAX_PORTS; i++) {
1413 moxaLog.rxcnt[i] = 0;
1414 moxaLog.txcnt[i] = 0;
1419 #define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1420 #define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
1421 #define MOXA_INIT_DRIVER (MOXA + 6) /* moxaCard=0 */
1422 #define MOXA_LOAD_BIOS (MOXA + 9) /* download BIOS */
1423 #define MOXA_FIND_BOARD (MOXA + 10) /* Check if MOXA card exist? */
1424 #define MOXA_LOAD_C320B (MOXA + 11) /* download 320B firmware */
1425 #define MOXA_LOAD_CODE (MOXA + 12) /* download firmware */
1426 #define MOXA_GETDATACOUNT (MOXA + 23)
1427 #define MOXA_GET_IOQUEUE (MOXA + 27)
1428 #define MOXA_FLUSH_QUEUE (MOXA + 28)
1429 #define MOXA_GET_CONF (MOXA + 35) /* configuration */
1430 #define MOXA_GET_MAJOR (MOXA + 63)
1431 #define MOXA_GET_CUMAJOR (MOXA + 64)
1432 #define MOXA_GETMSTATUS (MOXA + 65)
1440 static struct dl_str dltmp;
1442 void MoxaPortFlushData(int port, int mode)
1444 void __iomem *ofsAddr;
1445 if ((mode < 0) || (mode > 2))
1447 ofsAddr = moxa_ports[port].tableAddr;
1448 moxafunc(ofsAddr, FC_FlushQueue, mode);
1450 moxa_ports[port].lowChkFlag = 0;
1451 low_water_check(ofsAddr);
1455 int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port)
1459 int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
1460 void __user *argp = (void __user *)arg;
1462 if (port == MAX_PORTS) {
1463 if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_INIT_DRIVER) &&
1464 (cmd != MOXA_LOAD_BIOS) && (cmd != MOXA_FIND_BOARD) && (cmd != MOXA_LOAD_C320B) &&
1465 (cmd != MOXA_LOAD_CODE) && (cmd != MOXA_GETDATACOUNT) &&
1466 (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
1467 (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
1472 if(copy_to_user(argp, &moxa_boards, MAX_BOARDS *
1473 sizeof(struct moxa_board_conf)))
1476 case MOXA_INIT_DRIVER:
1477 if ((int) arg == 0x404)
1480 case MOXA_GETDATACOUNT:
1481 moxaLog.tick = jiffies;
1482 if(copy_to_user(argp, &moxaLog, sizeof(struct mon_str)))
1485 case MOXA_FLUSH_QUEUE:
1486 MoxaPortFlushData(port, arg);
1488 case MOXA_GET_IOQUEUE: {
1489 struct moxaq_str __user *argm = argp;
1490 struct moxa_port *p;
1492 for (i = 0; i < MAX_PORTS; i++, argm++) {
1494 memset(&p->temp_queue, 0, sizeof(p->temp_queue));
1496 p->temp_queue.inq = MoxaPortRxQueue(i);
1497 p->temp_queue.outq = MoxaPortTxQueue(i);
1499 if (copy_to_user(argm, &p->temp_queue,
1500 sizeof(p->temp_queue)))
1504 } case MOXA_GET_OQUEUE:
1505 i = MoxaPortTxQueue(port);
1506 return put_user(i, (unsigned long __user *)argp);
1507 case MOXA_GET_IQUEUE:
1508 i = MoxaPortRxQueue(port);
1509 return put_user(i, (unsigned long __user *)argp);
1510 case MOXA_GET_MAJOR:
1511 if(copy_to_user(argp, &ttymajor, sizeof(int)))
1514 case MOXA_GET_CUMAJOR:
1516 if(copy_to_user(argp, &i, sizeof(int)))
1519 case MOXA_GETMSTATUS: {
1520 struct mxser_mstatus __user *argm = argp;
1521 struct moxa_port *p;
1523 for (i = 0; i < MAX_PORTS; i++, argm++) {
1526 p->GMStatus.dcd = 0;
1527 p->GMStatus.dsr = 0;
1528 p->GMStatus.cts = 0;
1532 status = MoxaPortLineStatus(p->port);
1534 p->GMStatus.cts = 1;
1536 p->GMStatus.dsr = 1;
1538 p->GMStatus.dcd = 1;
1541 if (!p->tty || !p->tty->termios)
1542 p->GMStatus.cflag = p->cflag;
1544 p->GMStatus.cflag = p->tty->termios->c_cflag;
1546 if (copy_to_user(argm, &p->GMStatus,
1547 sizeof(p->GMStatus)))
1552 return (-ENOIOCTLCMD);
1553 case MOXA_LOAD_BIOS:
1554 case MOXA_FIND_BOARD:
1555 case MOXA_LOAD_C320B:
1556 case MOXA_LOAD_CODE:
1557 if (!capable(CAP_SYS_RAWIO))
1562 if(copy_from_user(&dltmp, argp, sizeof(struct dl_str)))
1564 if(dltmp.cardno < 0 || dltmp.cardno >= MAX_BOARDS)
1569 case MOXA_LOAD_BIOS:
1570 i = moxaloadbios(dltmp.cardno, dltmp.buf, dltmp.len);
1572 case MOXA_FIND_BOARD:
1573 return moxafindcard(dltmp.cardno);
1574 case MOXA_LOAD_C320B:
1575 moxaload320b(dltmp.cardno, dltmp.buf, dltmp.len);
1576 default: /* to keep gcc happy */
1578 case MOXA_LOAD_CODE:
1579 i = moxaloadcode(dltmp.cardno, dltmp.buf, dltmp.len);
1587 int MoxaDriverPoll(void)
1589 struct moxa_board_conf *brd;
1590 register ushort temp;
1592 void __iomem *ofsAddr;
1598 for (card = 0; card < MAX_BOARDS; card++) {
1599 brd = &moxa_boards[card];
1600 if (brd->loadstat == 0)
1602 if ((ports = brd->numPorts) == 0)
1604 if (readb(brd->intPend) == 0xff) {
1605 ip = brd->intTable + readb(brd->intNdx);
1606 p = card * MAX_PORTS_PER_BOARD;
1608 for (port = 0; port < ports; port += 2, p++) {
1609 if ((temp = readw(ip + port)) != 0) {
1610 writew(0, ip + port);
1611 ofsAddr = moxa_ports[p].tableAddr;
1613 writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
1614 if (temp & IntrBreak) {
1615 moxa_ports[p].breakCnt++;
1617 if (temp & IntrLine) {
1618 if (readb(ofsAddr + FlagStat) & DCD_state) {
1619 if ((moxa_ports[p].DCDState & DCD_oldstate) == 0)
1620 moxa_ports[p].DCDState = (DCD_oldstate |
1623 if (moxa_ports[p].DCDState & DCD_oldstate)
1624 moxa_ports[p].DCDState = DCD_changed;
1629 writeb(0, brd->intPend);
1631 if (moxaLowWaterChk) {
1632 p = card * MAX_PORTS_PER_BOARD;
1633 for (port = 0; port < ports; port++, p++) {
1634 if (moxa_ports[p].lowChkFlag) {
1635 moxa_ports[p].lowChkFlag = 0;
1636 ofsAddr = moxa_ports[p].tableAddr;
1637 low_water_check(ofsAddr);
1642 moxaLowWaterChk = 0;
1646 /*****************************************************************************
1647 * Card level function: *
1648 * 1. MoxaPortsOfCard(int cardno); *
1649 *****************************************************************************/
1650 int MoxaPortsOfCard(int cardno)
1653 if (moxa_boards[cardno].boardType == 0)
1655 return (moxa_boards[cardno].numPorts);
1658 /*****************************************************************************
1659 * Port level functions: *
1660 * 1. MoxaPortIsValid(int port); *
1661 * 2. MoxaPortEnable(int port); *
1662 * 3. MoxaPortDisable(int port); *
1663 * 4. MoxaPortGetMaxBaud(int port); *
1664 * 6. MoxaPortSetBaud(int port, long baud); *
1665 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1666 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1667 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1668 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1669 * 12. MoxaPortLineStatus(int port); *
1670 * 13. MoxaPortDCDChange(int port); *
1671 * 14. MoxaPortDCDON(int port); *
1672 * 15. MoxaPortFlushData(int port, int mode); *
1673 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1674 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
1675 * 20. MoxaPortTxQueue(int port); *
1676 * 21. MoxaPortTxFree(int port); *
1677 * 22. MoxaPortRxQueue(int port); *
1678 * 24. MoxaPortTxDisable(int port); *
1679 * 25. MoxaPortTxEnable(int port); *
1680 * 27. MoxaPortResetBrkCnt(int port); *
1681 * 30. MoxaPortSendBreak(int port, int ticks); *
1682 *****************************************************************************/
1684 * Moxa Port Number Description:
1686 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1687 * the port number using in MOXA driver functions will be 0 to 31 for
1688 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1689 * to 127 for fourth. For example, if you setup three MOXA boards,
1690 * first board is C218, second board is C320-16 and third board is
1691 * C320-32. The port number of first board (C218 - 8 ports) is from
1692 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1693 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1694 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1695 * 127 will be invalid.
1698 * Moxa Functions Description:
1700 * Function 1: Driver initialization routine, this routine must be
1701 * called when initialized driver.
1703 * void MoxaDriverInit();
1706 * Function 2: Moxa driver private IOCTL command processing.
1708 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1710 * unsigned int cmd : IOCTL command
1711 * unsigned long arg : IOCTL argument
1712 * int port : port number (0 - 127)
1719 * Function 3: Moxa driver polling process routine.
1721 * int MoxaDriverPoll(void);
1723 * return: 0 ; polling O.K.
1724 * -1 : no any Moxa card.
1727 * Function 4: Get the ports of this card.
1729 * int MoxaPortsOfCard(int cardno);
1731 * int cardno : card number (0 - 3)
1733 * return: 0 : this card is invalid
1737 * Function 5: Check this port is valid or invalid
1739 * int MoxaPortIsValid(int port);
1740 * int port : port number (0 - 127, ref port description)
1742 * return: 0 : this port is invalid
1743 * 1 : this port is valid
1746 * Function 6: Enable this port to start Tx/Rx data.
1748 * void MoxaPortEnable(int port);
1749 * int port : port number (0 - 127)
1752 * Function 7: Disable this port
1754 * void MoxaPortDisable(int port);
1755 * int port : port number (0 - 127)
1758 * Function 8: Get the maximun available baud rate of this port.
1760 * long MoxaPortGetMaxBaud(int port);
1761 * int port : port number (0 - 127)
1763 * return: 0 : this port is invalid
1764 * 38400/57600/115200 bps
1767 * Function 10: Setting baud rate of this port.
1769 * long MoxaPortSetBaud(int port, long baud);
1770 * int port : port number (0 - 127)
1771 * long baud : baud rate (50 - 115200)
1773 * return: 0 : this port is invalid or baud < 50
1774 * 50 - 115200 : the real baud rate set to the port, if
1775 * the argument baud is large than maximun
1776 * available baud rate, the real setting
1777 * baud rate will be the maximun baud rate.
1780 * Function 12: Configure the port.
1782 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1783 * int port : port number (0 - 127)
1784 * struct ktermios * termio : termio structure pointer
1785 * speed_t baud : baud rate
1787 * return: -1 : this port is invalid or termio == NULL
1791 * Function 13: Get the DTR/RTS state of this port.
1793 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1794 * int port : port number (0 - 127)
1795 * int * dtrState : pointer to INT to receive the current DTR
1796 * state. (if NULL, this function will not
1797 * write to this address)
1798 * int * rtsState : pointer to INT to receive the current RTS
1799 * state. (if NULL, this function will not
1800 * write to this address)
1802 * return: -1 : this port is invalid
1806 * Function 14: Setting the DTR/RTS output state of this port.
1808 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1809 * int port : port number (0 - 127)
1810 * int dtrState : DTR output state (0: off, 1: on)
1811 * int rtsState : RTS output state (0: off, 1: on)
1814 * Function 15: Setting the flow control of this port.
1816 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1817 * int txFlow,int xany);
1818 * int port : port number (0 - 127)
1819 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1820 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1821 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1822 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1823 * int xany : S/W XANY flow control (0: no, 1: yes)
1826 * Function 16: Get ths line status of this port
1828 * int MoxaPortLineStatus(int port);
1829 * int port : port number (0 - 127)
1831 * return: Bit 0 - CTS state (0: off, 1: on)
1832 * Bit 1 - DSR state (0: off, 1: on)
1833 * Bit 2 - DCD state (0: off, 1: on)
1836 * Function 17: Check the DCD state has changed since the last read
1839 * int MoxaPortDCDChange(int port);
1840 * int port : port number (0 - 127)
1842 * return: 0 : no changed
1843 * 1 : DCD has changed
1846 * Function 18: Check ths current DCD state is ON or not.
1848 * int MoxaPortDCDON(int port);
1849 * int port : port number (0 - 127)
1851 * return: 0 : DCD off
1855 * Function 19: Flush the Rx/Tx buffer data of this port.
1857 * void MoxaPortFlushData(int port, int mode);
1858 * int port : port number (0 - 127)
1860 * 0 : flush the Rx buffer
1861 * 1 : flush the Tx buffer
1862 * 2 : flush the Rx and Tx buffer
1865 * Function 20: Write data.
1867 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1868 * int port : port number (0 - 127)
1869 * unsigned char * buffer : pointer to write data buffer.
1870 * int length : write data length
1872 * return: 0 - length : real write data length
1875 * Function 21: Read data.
1877 * int MoxaPortReadData(int port, struct tty_struct *tty);
1878 * int port : port number (0 - 127)
1879 * struct tty_struct *tty : tty for data
1881 * return: 0 - length : real read data length
1884 * Function 24: Get the Tx buffer current queued data bytes
1886 * int MoxaPortTxQueue(int port);
1887 * int port : port number (0 - 127)
1889 * return: .. : Tx buffer current queued data bytes
1892 * Function 25: Get the Tx buffer current free space
1894 * int MoxaPortTxFree(int port);
1895 * int port : port number (0 - 127)
1897 * return: .. : Tx buffer current free space
1900 * Function 26: Get the Rx buffer current queued data bytes
1902 * int MoxaPortRxQueue(int port);
1903 * int port : port number (0 - 127)
1905 * return: .. : Rx buffer current queued data bytes
1908 * Function 28: Disable port data transmission.
1910 * void MoxaPortTxDisable(int port);
1911 * int port : port number (0 - 127)
1914 * Function 29: Enable port data transmission.
1916 * void MoxaPortTxEnable(int port);
1917 * int port : port number (0 - 127)
1920 * Function 31: Get the received BREAK signal count and reset it.
1922 * int MoxaPortResetBrkCnt(int port);
1923 * int port : port number (0 - 127)
1925 * return: 0 - .. : BREAK signal count
1928 * Function 34: Send out a BREAK signal.
1930 * void MoxaPortSendBreak(int port, int ms100);
1931 * int port : port number (0 - 127)
1932 * int ms100 : break signal time interval.
1933 * unit: 100 mini-second. if ms100 == 0, it will
1934 * send out a about 250 ms BREAK signal.
1937 int MoxaPortIsValid(int port)
1942 if (moxa_ports[port].chkPort == 0)
1947 void MoxaPortEnable(int port)
1949 void __iomem *ofsAddr;
1950 int MoxaPortLineStatus(int);
1951 short lowwater = 512;
1953 ofsAddr = moxa_ports[port].tableAddr;
1954 writew(lowwater, ofsAddr + Low_water);
1955 moxa_ports[port].breakCnt = 0;
1956 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
1957 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
1958 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1960 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
1963 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1964 moxafunc(ofsAddr, FC_FlushQueue, 2);
1966 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1967 MoxaPortLineStatus(port);
1970 void MoxaPortDisable(int port)
1972 void __iomem *ofsAddr = moxa_ports[port].tableAddr;
1974 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1975 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1976 writew(0, ofsAddr + HostStat);
1977 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1980 long MoxaPortGetMaxBaud(int port)
1982 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
1983 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI))
1990 long MoxaPortSetBaud(int port, long baud)
1992 void __iomem *ofsAddr;
1996 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
1998 ofsAddr = moxa_ports[port].tableAddr;
2002 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
2003 else if (max == 57600L)
2004 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
2006 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
2008 moxafunc(ofsAddr, FC_SetBaud, val);
2010 moxa_ports[port].curBaud = baud;
2014 int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud)
2016 void __iomem *ofsAddr;
2020 if (moxa_ports[port].chkPort == 0 || termio == 0)
2022 ofsAddr = moxa_ports[port].tableAddr;
2023 cflag = termio->c_cflag; /* termio->c_cflag */
2025 mode = termio->c_cflag & CSIZE;
2028 else if (mode == CS6)
2030 else if (mode == CS7)
2032 else if (mode == CS8)
2035 if (termio->c_cflag & CSTOPB) {
2043 if (termio->c_cflag & PARENB) {
2044 if (termio->c_cflag & PARODD)
2051 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
2053 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2054 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2055 if (baud >= 921600L)
2058 MoxaPortSetBaud(port, baud);
2060 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
2061 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
2062 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
2063 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
2064 wait_finish(ofsAddr);
2070 int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState)
2073 if (!MoxaPortIsValid(port))
2076 if (moxa_ports[port].lineCtrl & DTR_ON)
2082 if (moxa_ports[port].lineCtrl & RTS_ON)
2090 void MoxaPortLineCtrl(int port, int dtr, int rts)
2092 void __iomem *ofsAddr;
2095 ofsAddr = moxa_ports[port].tableAddr;
2101 moxa_ports[port].lineCtrl = mode;
2102 moxafunc(ofsAddr, FC_LineControl, mode);
2105 void MoxaPortFlowCtrl(int port, int rts, int cts, int txflow, int rxflow, int txany)
2107 void __iomem *ofsAddr;
2110 ofsAddr = moxa_ports[port].tableAddr;
2113 mode |= RTS_FlowCtl;
2115 mode |= CTS_FlowCtl;
2122 moxafunc(ofsAddr, FC_SetFlowCtl, mode);
2125 int MoxaPortLineStatus(int port)
2127 void __iomem *ofsAddr;
2130 ofsAddr = moxa_ports[port].tableAddr;
2131 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2132 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2133 moxafunc(ofsAddr, FC_LineStatus, 0);
2134 val = readw(ofsAddr + FuncArg);
2136 val = readw(ofsAddr + FlagStat) >> 4;
2141 if ((moxa_ports[port].DCDState & DCD_oldstate) == 0)
2142 moxa_ports[port].DCDState = (DCD_oldstate | DCD_changed);
2144 if (moxa_ports[port].DCDState & DCD_oldstate)
2145 moxa_ports[port].DCDState = DCD_changed;
2151 int MoxaPortDCDChange(int port)
2155 if (moxa_ports[port].chkPort == 0)
2157 n = moxa_ports[port].DCDState;
2158 moxa_ports[port].DCDState &= ~DCD_changed;
2163 int MoxaPortDCDON(int port)
2167 if (moxa_ports[port].chkPort == 0)
2169 if (moxa_ports[port].DCDState & DCD_oldstate)
2176 int MoxaPortWriteData(int port, unsigned char * buffer, int len)
2181 ushort head, tx_mask, spage, epage;
2182 ushort pageno, pageofs, bufhead;
2183 void __iomem *baseAddr, *ofsAddr, *ofs;
2185 ofsAddr = moxa_ports[port].tableAddr;
2186 baseAddr = moxa_boards[port / MAX_PORTS_PER_BOARD].basemem;
2187 tx_mask = readw(ofsAddr + TX_mask);
2188 spage = readw(ofsAddr + Page_txb);
2189 epage = readw(ofsAddr + EndPage_txb);
2190 tail = readw(ofsAddr + TXwptr);
2191 head = readw(ofsAddr + TXrptr);
2192 c = (head > tail) ? (head - tail - 1)
2193 : (head - tail + tx_mask);
2196 moxaLog.txcnt[port] += c;
2198 if (spage == epage) {
2199 bufhead = readw(ofsAddr + Ofs_txb);
2200 writew(spage, baseAddr + Control_reg);
2203 len = head - tail - 1;
2205 len = tx_mask + 1 - tail;
2206 len = (c > len) ? len : c;
2207 ofs = baseAddr + DynPage_addr + bufhead + tail;
2208 for (i = 0; i < len; i++)
2209 writeb(*buffer++, ofs + i);
2210 tail = (tail + len) & tx_mask;
2213 writew(tail, ofsAddr + TXwptr);
2216 pageno = spage + (tail >> 13);
2217 pageofs = tail & Page_mask;
2219 cnt = Page_size - pageofs;
2223 writeb(pageno, baseAddr + Control_reg);
2224 ofs = baseAddr + DynPage_addr + pageofs;
2225 for (i = 0; i < cnt; i++)
2226 writeb(*buffer++, ofs + i);
2228 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2231 if (++pageno == epage)
2236 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2240 int MoxaPortReadData(int port, struct tty_struct *tty)
2242 register ushort head, pageofs;
2243 int i, count, cnt, len, total, remain;
2244 ushort tail, rx_mask, spage, epage;
2245 ushort pageno, bufhead;
2246 void __iomem *baseAddr, *ofsAddr, *ofs;
2248 ofsAddr = moxa_ports[port].tableAddr;
2249 baseAddr = moxa_boards[port / MAX_PORTS_PER_BOARD].basemem;
2250 head = readw(ofsAddr + RXrptr);
2251 tail = readw(ofsAddr + RXwptr);
2252 rx_mask = readw(ofsAddr + RX_mask);
2253 spage = readw(ofsAddr + Page_rxb);
2254 epage = readw(ofsAddr + EndPage_rxb);
2255 count = (tail >= head) ? (tail - head)
2256 : (tail - head + rx_mask + 1);
2261 remain = count - total;
2262 moxaLog.rxcnt[port] += total;
2264 if (spage == epage) {
2265 bufhead = readw(ofsAddr + Ofs_rxb);
2266 writew(spage, baseAddr + Control_reg);
2271 len = rx_mask + 1 - head;
2272 len = (count > len) ? len : count;
2273 ofs = baseAddr + DynPage_addr + bufhead + head;
2274 for (i = 0; i < len; i++)
2275 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2276 head = (head + len) & rx_mask;
2279 writew(head, ofsAddr + RXrptr);
2282 pageno = spage + (head >> 13);
2283 pageofs = head & Page_mask;
2285 cnt = Page_size - pageofs;
2289 writew(pageno, baseAddr + Control_reg);
2290 ofs = baseAddr + DynPage_addr + pageofs;
2291 for (i = 0; i < cnt; i++)
2292 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2294 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2297 if (++pageno == epage)
2302 if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2303 moxaLowWaterChk = 1;
2304 moxa_ports[port].lowChkFlag = 1;
2310 int MoxaPortTxQueue(int port)
2312 void __iomem *ofsAddr;
2313 ushort rptr, wptr, mask;
2316 ofsAddr = moxa_ports[port].tableAddr;
2317 rptr = readw(ofsAddr + TXrptr);
2318 wptr = readw(ofsAddr + TXwptr);
2319 mask = readw(ofsAddr + TX_mask);
2320 len = (wptr - rptr) & mask;
2324 int MoxaPortTxFree(int port)
2326 void __iomem *ofsAddr;
2327 ushort rptr, wptr, mask;
2330 ofsAddr = moxa_ports[port].tableAddr;
2331 rptr = readw(ofsAddr + TXrptr);
2332 wptr = readw(ofsAddr + TXwptr);
2333 mask = readw(ofsAddr + TX_mask);
2334 len = mask - ((wptr - rptr) & mask);
2338 int MoxaPortRxQueue(int port)
2340 void __iomem *ofsAddr;
2341 ushort rptr, wptr, mask;
2344 ofsAddr = moxa_ports[port].tableAddr;
2345 rptr = readw(ofsAddr + RXrptr);
2346 wptr = readw(ofsAddr + RXwptr);
2347 mask = readw(ofsAddr + RX_mask);
2348 len = (wptr - rptr) & mask;
2353 void MoxaPortTxDisable(int port)
2355 void __iomem *ofsAddr;
2357 ofsAddr = moxa_ports[port].tableAddr;
2358 moxafunc(ofsAddr, FC_SetXoffState, Magic_code);
2361 void MoxaPortTxEnable(int port)
2363 void __iomem *ofsAddr;
2365 ofsAddr = moxa_ports[port].tableAddr;
2366 moxafunc(ofsAddr, FC_SetXonState, Magic_code);
2370 int MoxaPortResetBrkCnt(int port)
2373 cnt = moxa_ports[port].breakCnt;
2374 moxa_ports[port].breakCnt = 0;
2379 void MoxaPortSendBreak(int port, int ms100)
2381 void __iomem *ofsAddr;
2383 ofsAddr = moxa_ports[port].tableAddr;
2385 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2386 moxadelay(ms100 * (HZ / 10));
2388 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2389 moxadelay(HZ / 4); /* 250 ms */
2391 moxafunc(ofsAddr, FC_StopBreak, Magic_code);
2394 static int moxa_get_serial_info(struct moxa_port *info,
2395 struct serial_struct __user *retinfo)
2397 struct serial_struct tmp;
2399 memset(&tmp, 0, sizeof(tmp));
2400 tmp.type = info->type;
2401 tmp.line = info->port;
2404 tmp.flags = info->asyncflags;
2405 tmp.baud_base = 921600;
2406 tmp.close_delay = info->close_delay;
2407 tmp.closing_wait = info->closing_wait;
2408 tmp.custom_divisor = 0;
2410 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2416 static int moxa_set_serial_info(struct moxa_port *info,
2417 struct serial_struct __user *new_info)
2419 struct serial_struct new_serial;
2421 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2424 if ((new_serial.irq != 0) ||
2425 (new_serial.port != 0) ||
2426 // (new_serial.type != info->type) ||
2427 (new_serial.custom_divisor != 0) ||
2428 (new_serial.baud_base != 921600))
2431 if (!capable(CAP_SYS_ADMIN)) {
2432 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2433 (info->asyncflags & ~ASYNC_USR_MASK)))
2436 info->close_delay = new_serial.close_delay * HZ / 100;
2437 info->closing_wait = new_serial.closing_wait * HZ / 100;
2440 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2441 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2443 if (new_serial.type == PORT_16550A) {
2444 MoxaSetFifo(info->port, 1);
2446 MoxaSetFifo(info->port, 0);
2449 info->type = new_serial.type;
2455 /*****************************************************************************
2456 * Static local functions: *
2457 *****************************************************************************/
2459 * moxadelay - delays a specified number ticks
2461 static void moxadelay(int tick)
2463 unsigned long st, et;
2467 while (time_before(jiffies, et));
2470 static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
2473 writew(arg, ofsAddr + FuncArg);
2474 writew(cmd, ofsAddr + FuncCode);
2475 wait_finish(ofsAddr);
2478 static void wait_finish(void __iomem *ofsAddr)
2483 while (readw(ofsAddr + FuncCode) != 0) {
2485 if ((j - i) > moxaFuncTout) {
2491 static void low_water_check(void __iomem *ofsAddr)
2494 ushort rptr, wptr, mask;
2496 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2497 rptr = readw(ofsAddr + RXrptr);
2498 wptr = readw(ofsAddr + RXwptr);
2499 mask = readw(ofsAddr + RX_mask);
2500 len = (wptr - rptr) & mask;
2501 if (len <= Low_water)
2502 moxafunc(ofsAddr, FC_SendXon, 0);
2506 static int moxaloadbios(int cardno, unsigned char __user *tmp, int len)
2508 void __iomem *baseAddr;
2511 if(copy_from_user(moxaBuff, tmp, len))
2513 baseAddr = moxa_boards[cardno].basemem;
2514 writeb(HW_reset, baseAddr + Control_reg); /* reset */
2515 moxadelay(1); /* delay 10 ms */
2516 for (i = 0; i < 4096; i++)
2517 writeb(0, baseAddr + i); /* clear fix page */
2518 for (i = 0; i < len; i++)
2519 writeb(moxaBuff[i], baseAddr + i); /* download BIOS */
2520 writeb(0, baseAddr + Control_reg); /* restart */
2524 static int moxafindcard(int cardno)
2526 void __iomem *baseAddr;
2529 baseAddr = moxa_boards[cardno].basemem;
2530 switch (moxa_boards[cardno].boardType) {
2531 case MOXA_BOARD_C218_ISA:
2532 case MOXA_BOARD_C218_PCI:
2533 if ((tmp = readw(baseAddr + C218_key)) != C218_KeyCode) {
2537 case MOXA_BOARD_CP204J:
2538 if ((tmp = readw(baseAddr + C218_key)) != CP204J_KeyCode) {
2543 if ((tmp = readw(baseAddr + C320_key)) != C320_KeyCode) {
2546 if ((tmp = readw(baseAddr + C320_status)) != STS_init) {
2553 static int moxaload320b(int cardno, unsigned char __user *tmp, int len)
2555 void __iomem *baseAddr;
2558 if(len > sizeof(moxaBuff))
2560 if(copy_from_user(moxaBuff, tmp, len))
2562 baseAddr = moxa_boards[cardno].basemem;
2563 writew(len - 7168 - 2, baseAddr + C320bapi_len);
2564 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
2565 for (i = 0; i < 7168; i++)
2566 writeb(moxaBuff[i], baseAddr + DynPage_addr + i);
2567 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
2568 for (i = 0; i < (len - 7168); i++)
2569 writeb(moxaBuff[i + 7168], baseAddr + DynPage_addr + i);
2573 static int moxaloadcode(int cardno, unsigned char __user *tmp, int len)
2575 void __iomem *baseAddr, *ofsAddr;
2576 int retval, port, i;
2578 if(copy_from_user(moxaBuff, tmp, len))
2580 baseAddr = moxa_boards[cardno].basemem;
2581 switch (moxa_boards[cardno].boardType) {
2582 case MOXA_BOARD_C218_ISA:
2583 case MOXA_BOARD_C218_PCI:
2584 case MOXA_BOARD_CP204J:
2585 retval = moxaloadc218(cardno, baseAddr, len);
2588 port = cardno * MAX_PORTS_PER_BOARD;
2589 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2590 struct moxa_port *p = &moxa_ports[port];
2595 p->tableAddr = baseAddr + Extern_table + Extern_size * i;
2596 ofsAddr = p->tableAddr;
2597 writew(C218rx_mask, ofsAddr + RX_mask);
2598 writew(C218tx_mask, ofsAddr + TX_mask);
2599 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
2600 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
2602 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
2603 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
2608 retval = moxaloadc320(cardno, baseAddr, len,
2609 &moxa_boards[cardno].numPorts);
2612 port = cardno * MAX_PORTS_PER_BOARD;
2613 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2614 struct moxa_port *p = &moxa_ports[port];
2619 p->tableAddr = baseAddr + Extern_table + Extern_size * i;
2620 ofsAddr = p->tableAddr;
2621 if (moxa_boards[cardno].numPorts == 8) {
2622 writew(C320p8rx_mask, ofsAddr + RX_mask);
2623 writew(C320p8tx_mask, ofsAddr + TX_mask);
2624 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
2625 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
2626 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
2627 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
2629 } else if (moxa_boards[cardno].numPorts == 16) {
2630 writew(C320p16rx_mask, ofsAddr + RX_mask);
2631 writew(C320p16tx_mask, ofsAddr + TX_mask);
2632 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
2633 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
2634 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
2635 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
2637 } else if (moxa_boards[cardno].numPorts == 24) {
2638 writew(C320p24rx_mask, ofsAddr + RX_mask);
2639 writew(C320p24tx_mask, ofsAddr + TX_mask);
2640 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
2641 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
2642 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
2643 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2644 } else if (moxa_boards[cardno].numPorts == 32) {
2645 writew(C320p32rx_mask, ofsAddr + RX_mask);
2646 writew(C320p32tx_mask, ofsAddr + TX_mask);
2647 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
2648 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
2649 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
2650 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
2651 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2656 moxa_boards[cardno].loadstat = 1;
2660 static int moxaloadc218(int cardno, void __iomem *baseAddr, int len)
2663 int i, j, len1, len2;
2664 ushort usum, *ptr, keycode;
2666 if (moxa_boards[cardno].boardType == MOXA_BOARD_CP204J)
2667 keycode = CP204J_KeyCode;
2669 keycode = C218_KeyCode;
2672 ptr = (ushort *) moxaBuff;
2673 for (i = 0; i < len1; i++)
2674 usum += le16_to_cpu(*(ptr + i));
2680 len2 = (len1 > 2048) ? 2048 : len1;
2682 for (i = 0; i < len2 << 1; i++)
2683 writeb(moxaBuff[i + j], baseAddr + C218_LoadBuf + i);
2686 writew(len2, baseAddr + C218DLoad_len);
2687 writew(0, baseAddr + C218_key);
2688 for (i = 0; i < 100; i++) {
2689 if (readw(baseAddr + C218_key) == keycode)
2691 moxadelay(1); /* delay 10 ms */
2693 if (readw(baseAddr + C218_key) != keycode) {
2697 writew(0, baseAddr + C218DLoad_len);
2698 writew(usum, baseAddr + C218check_sum);
2699 writew(0, baseAddr + C218_key);
2700 for (i = 0; i < 100; i++) {
2701 if (readw(baseAddr + C218_key) == keycode)
2703 moxadelay(1); /* delay 10 ms */
2706 } while ((readb(baseAddr + C218chksum_ok) != 1) && (retry < 3));
2707 if (readb(baseAddr + C218chksum_ok) != 1) {
2710 writew(0, baseAddr + C218_key);
2711 for (i = 0; i < 100; i++) {
2712 if (readw(baseAddr + Magic_no) == Magic_code)
2714 moxadelay(1); /* delay 10 ms */
2716 if (readw(baseAddr + Magic_no) != Magic_code) {
2719 writew(1, baseAddr + Disable_IRQ);
2720 writew(0, baseAddr + Magic_no);
2721 for (i = 0; i < 100; i++) {
2722 if (readw(baseAddr + Magic_no) == Magic_code)
2724 moxadelay(1); /* delay 10 ms */
2726 if (readw(baseAddr + Magic_no) != Magic_code) {
2730 moxa_boards[cardno].intNdx = baseAddr + IRQindex;
2731 moxa_boards[cardno].intPend = baseAddr + IRQpending;
2732 moxa_boards[cardno].intTable = baseAddr + IRQtable;
2736 static int moxaloadc320(int cardno, void __iomem *baseAddr, int len, int *numPorts)
2739 int i, j, wlen, len2, retry;
2744 uptr = (ushort *) moxaBuff;
2745 for (i = 0; i < wlen; i++)
2746 usum += le16_to_cpu(uptr[i]);
2757 for (i = 0; i < len2; i++)
2758 writeb(moxaBuff[j + i], baseAddr + C320_LoadBuf + i);
2761 writew(len2, baseAddr + C320DLoad_len);
2762 writew(0, baseAddr + C320_key);
2763 for (i = 0; i < 10; i++) {
2764 if (readw(baseAddr + C320_key) == C320_KeyCode)
2768 if (readw(baseAddr + C320_key) != C320_KeyCode)
2771 writew(0, baseAddr + C320DLoad_len);
2772 writew(usum, baseAddr + C320check_sum);
2773 writew(0, baseAddr + C320_key);
2774 for (i = 0; i < 10; i++) {
2775 if (readw(baseAddr + C320_key) == C320_KeyCode)
2780 } while ((readb(baseAddr + C320chksum_ok) != 1) && (retry < 3));
2781 if (readb(baseAddr + C320chksum_ok) != 1)
2783 writew(0, baseAddr + C320_key);
2784 for (i = 0; i < 600; i++) {
2785 if (readw(baseAddr + Magic_no) == Magic_code)
2789 if (readw(baseAddr + Magic_no) != Magic_code)
2792 if (moxa_boards[cardno].busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
2793 writew(0x3800, baseAddr + TMS320_PORT1);
2794 writew(0x3900, baseAddr + TMS320_PORT2);
2795 writew(28499, baseAddr + TMS320_CLOCK);
2797 writew(0x3200, baseAddr + TMS320_PORT1);
2798 writew(0x3400, baseAddr + TMS320_PORT2);
2799 writew(19999, baseAddr + TMS320_CLOCK);
2801 writew(1, baseAddr + Disable_IRQ);
2802 writew(0, baseAddr + Magic_no);
2803 for (i = 0; i < 500; i++) {
2804 if (readw(baseAddr + Magic_no) == Magic_code)
2808 if (readw(baseAddr + Magic_no) != Magic_code)
2811 j = readw(baseAddr + Module_cnt);
2815 writew(j, baseAddr + Module_no);
2816 writew(0, baseAddr + Magic_no);
2817 for (i = 0; i < 600; i++) {
2818 if (readw(baseAddr + Magic_no) == Magic_code)
2822 if (readw(baseAddr + Magic_no) != Magic_code)
2825 moxa_boards[cardno].intNdx = baseAddr + IRQindex;
2826 moxa_boards[cardno].intPend = baseAddr + IRQpending;
2827 moxa_boards[cardno].intTable = baseAddr + IRQtable;
2831 static void MoxaSetFifo(int port, int enable)
2833 void __iomem *ofsAddr = moxa_ports[port].tableAddr;
2836 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2837 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2839 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2840 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);