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.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * MOXA Intellio Series Driver
32 #include <linux/module.h>
33 #include <linux/types.h>
35 #include <linux/ioport.h>
36 #include <linux/errno.h>
37 #include <linux/signal.h>
38 #include <linux/sched.h>
39 #include <linux/timer.h>
40 #include <linux/interrupt.h>
41 #include <linux/tty.h>
42 #include <linux/tty_flip.h>
43 #include <linux/major.h>
44 #include <linux/string.h>
45 #include <linux/fcntl.h>
46 #include <linux/ptrace.h>
47 #include <linux/serial.h>
48 #include <linux/tty_driver.h>
49 #include <linux/delay.h>
50 #include <linux/pci.h>
51 #include <linux/init.h>
52 #include <linux/bitops.h>
54 #include <asm/system.h>
56 #include <asm/uaccess.h>
58 #define MOXA_VERSION "5.1k"
61 #define MOXACUMAJOR 173
63 #define put_to_user(arg1, arg2) put_user(arg1, (unsigned long *)arg2)
64 #define get_from_user(arg1, arg2) get_user(arg1, (unsigned int *)arg2)
66 #define MAX_BOARDS 4 /* Don't change this value */
67 #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
68 #define MAX_PORTS 128 /* Don't change this value */
71 * Define the Moxa PCI vendor and device IDs.
73 #define MOXA_BUS_TYPE_ISA 0
74 #define MOXA_BUS_TYPE_PCI 1
76 #ifndef PCI_VENDOR_ID_MOXA
77 #define PCI_VENDOR_ID_MOXA 0x1393
79 #ifndef PCI_DEVICE_ID_CP204J
80 #define PCI_DEVICE_ID_CP204J 0x2040
82 #ifndef PCI_DEVICE_ID_C218
83 #define PCI_DEVICE_ID_C218 0x2180
85 #ifndef PCI_DEVICE_ID_C320
86 #define PCI_DEVICE_ID_C320 0x3200
90 MOXA_BOARD_C218_PCI = 1,
97 static char *moxa_brdname[] =
99 "C218 Turbo PCI series",
100 "C218 Turbo ISA series",
101 "C320 Turbo PCI series",
102 "C320 Turbo ISA series",
107 static struct pci_device_id moxa_pcibrds[] = {
108 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C218, PCI_ANY_ID, PCI_ANY_ID,
109 0, 0, MOXA_BOARD_C218_PCI },
110 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C320, PCI_ANY_ID, PCI_ANY_ID,
111 0, 0, MOXA_BOARD_C320_PCI },
112 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP204J, PCI_ANY_ID, PCI_ANY_ID,
113 0, 0, MOXA_BOARD_CP204J },
116 MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
117 #endif /* CONFIG_PCI */
119 typedef struct _moxa_isa_board_conf {
122 unsigned long baseAddr;
123 } moxa_isa_board_conf;
125 static moxa_isa_board_conf moxa_isa_boards[] =
127 /* {MOXA_BOARD_C218_ISA,8,0xDC000}, */
130 typedef struct _moxa_pci_devinfo {
133 struct pci_dev *pdev;
136 typedef struct _moxa_board_conf {
139 unsigned long baseAddr;
141 moxa_pci_devinfo pciInfo;
144 static moxa_board_conf moxa_boards[MAX_BOARDS];
145 static void __iomem *moxaBaseAddr[MAX_BOARDS];
146 static int loadstat[MAX_BOARDS];
152 unsigned short closing_wait;
155 long event; /* long req'd for set_bit --RR */
157 unsigned long statusflags;
158 struct tty_struct *tty;
160 wait_queue_head_t open_wait;
161 wait_queue_head_t close_wait;
164 struct mxser_mstatus {
172 static struct mxser_mstatus GMStatus[MAX_PORTS];
175 #define TXSTOPPED 0x1
177 #define EMPTYWAIT 0x4
180 #define SERIAL_DO_RESTART
183 #define SERIAL_TYPE_NORMAL 1
185 #define WAKEUP_CHARS 256
187 #define PORTNO(x) ((x)->index)
189 static int verbose = 0;
190 static int ttymajor = MOXAMAJOR;
191 /* Variables for insmod */
193 static int baseaddr[4];
195 static int numports[4];
198 MODULE_AUTHOR("William Chen");
199 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
200 MODULE_LICENSE("GPL");
202 module_param_array(type, int, NULL, 0);
203 module_param_array(baseaddr, int, NULL, 0);
204 module_param_array(numports, int, NULL, 0);
206 module_param(ttymajor, int, 0);
207 module_param(verbose, bool, 0644);
212 static int moxa_open(struct tty_struct *, struct file *);
213 static void moxa_close(struct tty_struct *, struct file *);
214 static int moxa_write(struct tty_struct *, const unsigned char *, int);
215 static int moxa_write_room(struct tty_struct *);
216 static void moxa_flush_buffer(struct tty_struct *);
217 static int moxa_chars_in_buffer(struct tty_struct *);
218 static void moxa_flush_chars(struct tty_struct *);
219 static void moxa_put_char(struct tty_struct *, unsigned char);
220 static int moxa_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);
221 static void moxa_throttle(struct tty_struct *);
222 static void moxa_unthrottle(struct tty_struct *);
223 static void moxa_set_termios(struct tty_struct *, struct ktermios *);
224 static void moxa_stop(struct tty_struct *);
225 static void moxa_start(struct tty_struct *);
226 static void moxa_hangup(struct tty_struct *);
227 static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
228 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
229 unsigned int set, unsigned int clear);
230 static void moxa_poll(unsigned long);
231 static void set_tty_param(struct tty_struct *);
232 static int block_till_ready(struct tty_struct *, struct file *,
234 static void setup_empty_event(struct tty_struct *);
235 static void check_xmit_empty(unsigned long);
236 static void shut_down(struct moxa_str *);
237 static void receive_data(struct moxa_str *);
239 * moxa board interface functions:
241 static void MoxaDriverInit(void);
242 static int MoxaDriverIoctl(unsigned int, unsigned long, int);
243 static int MoxaDriverPoll(void);
244 static int MoxaPortsOfCard(int);
245 static int MoxaPortIsValid(int);
246 static void MoxaPortEnable(int);
247 static void MoxaPortDisable(int);
248 static long MoxaPortGetMaxBaud(int);
249 static long MoxaPortSetBaud(int, long);
250 static int MoxaPortSetTermio(int, struct ktermios *, speed_t);
251 static int MoxaPortGetLineOut(int, int *, int *);
252 static void MoxaPortLineCtrl(int, int, int);
253 static void MoxaPortFlowCtrl(int, int, int, int, int, int);
254 static int MoxaPortLineStatus(int);
255 static int MoxaPortDCDChange(int);
256 static int MoxaPortDCDON(int);
257 static void MoxaPortFlushData(int, int);
258 static int MoxaPortWriteData(int, unsigned char *, int);
259 static int MoxaPortReadData(int, struct tty_struct *tty);
260 static int MoxaPortTxQueue(int);
261 static int MoxaPortRxQueue(int);
262 static int MoxaPortTxFree(int);
263 static void MoxaPortTxDisable(int);
264 static void MoxaPortTxEnable(int);
265 static int MoxaPortResetBrkCnt(int);
266 static void MoxaPortSendBreak(int, int);
267 static int moxa_get_serial_info(struct moxa_str *, struct serial_struct __user *);
268 static int moxa_set_serial_info(struct moxa_str *, struct serial_struct __user *);
269 static void MoxaSetFifo(int port, int enable);
271 static const struct tty_operations moxa_ops = {
275 .write_room = moxa_write_room,
276 .flush_buffer = moxa_flush_buffer,
277 .chars_in_buffer = moxa_chars_in_buffer,
278 .flush_chars = moxa_flush_chars,
279 .put_char = moxa_put_char,
281 .throttle = moxa_throttle,
282 .unthrottle = moxa_unthrottle,
283 .set_termios = moxa_set_termios,
286 .hangup = moxa_hangup,
287 .tiocmget = moxa_tiocmget,
288 .tiocmset = moxa_tiocmset,
291 static struct tty_driver *moxaDriver;
292 static struct moxa_str moxaChannels[MAX_PORTS];
293 static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
294 static struct timer_list moxaEmptyTimer[MAX_PORTS];
295 static DEFINE_SPINLOCK(moxa_lock);
298 static int moxa_get_PCI_conf(struct pci_dev *p, int board_type, moxa_board_conf * board)
300 board->baseAddr = pci_resource_start (p, 2);
301 board->boardType = board_type;
302 switch (board_type) {
303 case MOXA_BOARD_C218_ISA:
304 case MOXA_BOARD_C218_PCI:
308 case MOXA_BOARD_CP204J:
315 board->busType = MOXA_BUS_TYPE_PCI;
316 board->pciInfo.busNum = p->bus->number;
317 board->pciInfo.devNum = p->devfn >> 3;
318 board->pciInfo.pdev = p;
319 /* don't lose the reference in the next pci_get_device iteration */
324 #endif /* CONFIG_PCI */
326 static int __init moxa_init(void)
331 printk(KERN_INFO "MOXA Intellio family driver version %s\n", MOXA_VERSION);
332 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
336 moxaDriver->owner = THIS_MODULE;
337 moxaDriver->name = "ttyMX";
338 moxaDriver->major = ttymajor;
339 moxaDriver->minor_start = 0;
340 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
341 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
342 moxaDriver->init_termios = tty_std_termios;
343 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
344 moxaDriver->init_termios.c_ispeed = 9600;
345 moxaDriver->init_termios.c_ospeed = 9600;
346 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
347 tty_set_operations(moxaDriver, &moxa_ops);
349 for (i = 0, ch = moxaChannels; i < MAX_PORTS; i++, ch++) {
350 ch->type = PORT_16550A;
352 ch->close_delay = 5 * HZ / 10;
353 ch->closing_wait = 30 * HZ;
354 ch->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
355 init_waitqueue_head(&ch->open_wait);
356 init_waitqueue_head(&ch->close_wait);
359 printk("Tty devices major number = %d\n", ttymajor);
361 if (tty_register_driver(moxaDriver)) {
362 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
363 put_tty_driver(moxaDriver);
366 for (i = 0; i < MAX_PORTS; i++)
367 setup_timer(&moxaEmptyTimer[i], check_xmit_empty,
368 (unsigned long)&moxaChannels[i]);
370 mod_timer(&moxaTimer, jiffies + HZ / 50);
372 /* Find the boards defined in source code */
374 for (i = 0; i < MAX_BOARDS; i++) {
375 if ((moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA) ||
376 (moxa_isa_boards[i].boardType == MOXA_BOARD_C320_ISA)) {
377 moxa_boards[numBoards].boardType = moxa_isa_boards[i].boardType;
378 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
379 moxa_boards[numBoards].numPorts = 8;
381 moxa_boards[numBoards].numPorts = moxa_isa_boards[i].numPorts;
382 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
383 moxa_boards[numBoards].baseAddr = moxa_isa_boards[i].baseAddr;
385 printk("Board %2d: %s board(baseAddr=%lx)\n",
387 moxa_brdname[moxa_boards[numBoards].boardType - 1],
388 moxa_boards[numBoards].baseAddr);
392 /* Find the boards defined form module args. */
394 for (i = 0; i < MAX_BOARDS; i++) {
395 if ((type[i] == MOXA_BOARD_C218_ISA) ||
396 (type[i] == MOXA_BOARD_C320_ISA)) {
398 printk("Board %2d: %s board(baseAddr=%lx)\n",
400 moxa_brdname[type[i] - 1],
401 (unsigned long) baseaddr[i]);
402 if (numBoards >= MAX_BOARDS) {
404 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
407 moxa_boards[numBoards].boardType = type[i];
408 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
409 moxa_boards[numBoards].numPorts = 8;
411 moxa_boards[numBoards].numPorts = numports[i];
412 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
413 moxa_boards[numBoards].baseAddr = baseaddr[i];
418 /* Find PCI boards here */
421 struct pci_dev *p = NULL;
422 int n = ARRAY_SIZE(moxa_pcibrds) - 1;
425 while ((p = pci_get_device(moxa_pcibrds[i].vendor, moxa_pcibrds[i].device, p))!=NULL)
427 if (pci_enable_device(p))
429 if (numBoards >= MAX_BOARDS) {
431 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
433 moxa_get_PCI_conf(p, moxa_pcibrds[i].driver_data,
434 &moxa_boards[numBoards]);
442 for (i = 0; i < numBoards; i++) {
443 moxaBaseAddr[i] = ioremap((unsigned long) moxa_boards[i].baseAddr, 0x4000);
449 static void __exit moxa_exit(void)
454 printk("Unloading module moxa ...\n");
456 del_timer(&moxaTimer);
458 for (i = 0; i < MAX_PORTS; i++)
459 del_timer(&moxaEmptyTimer[i]);
461 if (tty_unregister_driver(moxaDriver))
462 printk("Couldn't unregister MOXA Intellio family serial driver\n");
463 put_tty_driver(moxaDriver);
465 for (i = 0; i < MAX_BOARDS; i++) {
467 iounmap(moxaBaseAddr[i]);
468 if (moxa_boards[i].busType == MOXA_BUS_TYPE_PCI)
469 pci_dev_put(moxa_boards[i].pciInfo.pdev);
476 module_init(moxa_init);
477 module_exit(moxa_exit);
479 static int moxa_open(struct tty_struct *tty, struct file *filp)
486 if (port == MAX_PORTS) {
489 if (!MoxaPortIsValid(port)) {
490 tty->driver_data = NULL;
494 ch = &moxaChannels[port];
496 tty->driver_data = ch;
498 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
501 MoxaPortLineCtrl(ch->port, 1, 1);
502 MoxaPortEnable(ch->port);
503 ch->asyncflags |= ASYNC_INITIALIZED;
505 retval = block_till_ready(tty, filp, ch);
507 moxa_unthrottle(tty);
509 if (ch->type == PORT_16550A) {
510 MoxaSetFifo(ch->port, 1);
512 MoxaSetFifo(ch->port, 0);
518 static void moxa_close(struct tty_struct *tty, struct file *filp)
524 if (port == MAX_PORTS) {
527 if (!MoxaPortIsValid(port)) {
528 #ifdef SERIAL_DEBUG_CLOSE
529 printk("Invalid portno in moxa_close\n");
531 tty->driver_data = NULL;
534 if (tty->driver_data == NULL) {
537 if (tty_hung_up_p(filp)) {
540 ch = (struct moxa_str *) tty->driver_data;
542 if ((tty->count == 1) && (ch->count != 1)) {
543 printk("moxa_close: bad serial port count; tty->count is 1, "
544 "ch->count is %d\n", ch->count);
547 if (--ch->count < 0) {
548 printk("moxa_close: bad serial port count, device=%s\n",
555 ch->asyncflags |= ASYNC_CLOSING;
557 ch->cflag = tty->termios->c_cflag;
558 if (ch->asyncflags & ASYNC_INITIALIZED) {
559 setup_empty_event(tty);
560 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
561 del_timer(&moxaEmptyTimer[ch->port]);
564 MoxaPortFlushData(port, 2);
566 if (tty->driver->flush_buffer)
567 tty->driver->flush_buffer(tty);
568 tty_ldisc_flush(tty);
573 if (ch->blocked_open) {
574 if (ch->close_delay) {
575 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
577 wake_up_interruptible(&ch->open_wait);
579 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
580 wake_up_interruptible(&ch->close_wait);
583 static int moxa_write(struct tty_struct *tty,
584 const unsigned char *buf, int count)
590 ch = (struct moxa_str *) tty->driver_data;
595 spin_lock_irqsave(&moxa_lock, flags);
596 len = MoxaPortWriteData(port, (unsigned char *) buf, count);
597 spin_unlock_irqrestore(&moxa_lock, flags);
599 /*********************************************
600 if ( !(ch->statusflags & LOWWAIT) &&
601 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
602 ************************************************/
603 ch->statusflags |= LOWWAIT;
607 static int moxa_write_room(struct tty_struct *tty)
613 ch = (struct moxa_str *) tty->driver_data;
616 return (MoxaPortTxFree(ch->port));
619 static void moxa_flush_buffer(struct tty_struct *tty)
621 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
625 MoxaPortFlushData(ch->port, 1);
629 static int moxa_chars_in_buffer(struct tty_struct *tty)
632 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
635 * Sigh...I have to check if driver_data is NULL here, because
636 * if an open() fails, the TTY subsystem eventually calls
637 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
638 * routine. And since the open() failed, we return 0 here. TDJ
642 chars = MoxaPortTxQueue(ch->port);
645 * Make it possible to wakeup anything waiting for output
646 * in tty_ioctl.c, etc.
648 if (!(ch->statusflags & EMPTYWAIT))
649 setup_empty_event(tty);
654 static void moxa_flush_chars(struct tty_struct *tty)
657 * Don't think I need this, because this is called to empty the TX
658 * buffer for the 16450, 16550, etc.
662 static void moxa_put_char(struct tty_struct *tty, unsigned char c)
668 ch = (struct moxa_str *) tty->driver_data;
672 spin_lock_irqsave(&moxa_lock, flags);
673 MoxaPortWriteData(port, &c, 1);
674 spin_unlock_irqrestore(&moxa_lock, flags);
675 /************************************************
676 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
677 *************************************************/
678 ch->statusflags |= LOWWAIT;
681 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
683 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
685 int flag = 0, dtr, rts;
688 if ((port != MAX_PORTS) && (!ch))
691 MoxaPortGetLineOut(ch->port, &dtr, &rts);
696 dtr = MoxaPortLineStatus(ch->port);
706 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
707 unsigned int set, unsigned int clear)
709 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
714 if ((port != MAX_PORTS) && (!ch))
717 MoxaPortGetLineOut(ch->port, &dtr, &rts);
722 if (clear & TIOCM_RTS)
724 if (clear & TIOCM_DTR)
726 MoxaPortLineCtrl(ch->port, dtr, rts);
730 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
731 unsigned int cmd, unsigned long arg)
733 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
735 void __user *argp = (void __user *)arg;
739 if ((port != MAX_PORTS) && (!ch))
743 case TCSBRK: /* SVID version: non-zero arg --> no break */
744 retval = tty_check_change(tty);
747 setup_empty_event(tty);
748 tty_wait_until_sent(tty, 0);
750 MoxaPortSendBreak(ch->port, 0);
752 case TCSBRKP: /* support for POSIX tcsendbreak() */
753 retval = tty_check_change(tty);
756 setup_empty_event(tty);
757 tty_wait_until_sent(tty, 0);
758 MoxaPortSendBreak(ch->port, arg);
761 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) argp);
763 if(get_user(retval, (unsigned long __user *) argp))
766 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
769 ch->asyncflags &= ~ASYNC_CHECK_CD;
771 ch->asyncflags |= ASYNC_CHECK_CD;
774 return moxa_get_serial_info(ch, argp);
777 return moxa_set_serial_info(ch, argp);
779 retval = MoxaDriverIoctl(cmd, arg, port);
784 static void moxa_throttle(struct tty_struct *tty)
786 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
788 ch->statusflags |= THROTTLE;
791 static void moxa_unthrottle(struct tty_struct *tty)
793 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
795 ch->statusflags &= ~THROTTLE;
798 static void moxa_set_termios(struct tty_struct *tty,
799 struct ktermios *old_termios)
801 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
806 if (!(old_termios->c_cflag & CLOCAL) &&
807 (tty->termios->c_cflag & CLOCAL))
808 wake_up_interruptible(&ch->open_wait);
811 static void moxa_stop(struct tty_struct *tty)
813 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
817 MoxaPortTxDisable(ch->port);
818 ch->statusflags |= TXSTOPPED;
822 static void moxa_start(struct tty_struct *tty)
824 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
829 if (!(ch->statusflags & TXSTOPPED))
832 MoxaPortTxEnable(ch->port);
833 ch->statusflags &= ~TXSTOPPED;
836 static void moxa_hangup(struct tty_struct *tty)
838 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
840 moxa_flush_buffer(tty);
844 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
846 wake_up_interruptible(&ch->open_wait);
849 static void moxa_poll(unsigned long ignored)
853 struct tty_struct *tp;
856 del_timer(&moxaTimer);
858 if (MoxaDriverPoll() < 0) {
859 mod_timer(&moxaTimer, jiffies + HZ / 50);
862 for (card = 0; card < MAX_BOARDS; card++) {
863 if ((ports = MoxaPortsOfCard(card)) <= 0)
865 ch = &moxaChannels[card * MAX_PORTS_PER_BOARD];
866 for (i = 0; i < ports; i++, ch++) {
867 if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
869 if (!(ch->statusflags & THROTTLE) &&
870 (MoxaPortRxQueue(ch->port) > 0))
872 if ((tp = ch->tty) == 0)
874 if (ch->statusflags & LOWWAIT) {
875 if (MoxaPortTxQueue(ch->port) <= WAKEUP_CHARS) {
877 ch->statusflags &= ~LOWWAIT;
882 if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch->port) > 0)) {
883 tty_insert_flip_char(tp, 0, TTY_BREAK);
884 tty_schedule_flip(tp);
886 if (MoxaPortDCDChange(ch->port)) {
887 if (ch->asyncflags & ASYNC_CHECK_CD) {
888 if (MoxaPortDCDON(ch->port))
889 wake_up_interruptible(&ch->open_wait);
892 wake_up_interruptible(&ch->open_wait);
893 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
900 mod_timer(&moxaTimer, jiffies + HZ / 50);
903 /******************************************************************************/
905 static void set_tty_param(struct tty_struct *tty)
907 register struct ktermios *ts;
909 int rts, cts, txflow, rxflow, xany;
911 ch = (struct moxa_str *) tty->driver_data;
913 if (ts->c_cflag & CLOCAL)
914 ch->asyncflags &= ~ASYNC_CHECK_CD;
916 ch->asyncflags |= ASYNC_CHECK_CD;
917 rts = cts = txflow = rxflow = xany = 0;
918 if (ts->c_cflag & CRTSCTS)
920 if (ts->c_iflag & IXON)
922 if (ts->c_iflag & IXOFF)
924 if (ts->c_iflag & IXANY)
926 MoxaPortFlowCtrl(ch->port, rts, cts, txflow, rxflow, xany);
927 MoxaPortSetTermio(ch->port, ts, tty_get_baud_rate(tty));
930 static int block_till_ready(struct tty_struct *tty, struct file *filp,
933 DECLARE_WAITQUEUE(wait,current);
936 int do_clocal = C_CLOCAL(tty);
939 * If the device is in the middle of being closed, then block
940 * until it's done, and then try again.
942 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
943 if (ch->asyncflags & ASYNC_CLOSING)
944 interruptible_sleep_on(&ch->close_wait);
945 #ifdef SERIAL_DO_RESTART
946 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
949 return (-ERESTARTSYS);
955 * If non-blocking mode is set, then make the check up front
958 if (filp->f_flags & O_NONBLOCK) {
959 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
963 * Block waiting for the carrier detect and the line to become free
966 add_wait_queue(&ch->open_wait, &wait);
967 #ifdef SERIAL_DEBUG_OPEN
968 printk("block_til_ready before block: ttys%d, count = %d\n",
969 ch->line, ch->count);
971 spin_lock_irqsave(&moxa_lock, flags);
972 if (!tty_hung_up_p(filp))
975 spin_unlock_irqrestore(&moxa_lock, flags);
978 set_current_state(TASK_INTERRUPTIBLE);
979 if (tty_hung_up_p(filp) ||
980 !(ch->asyncflags & ASYNC_INITIALIZED)) {
981 #ifdef SERIAL_DO_RESTART
982 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
985 retval = -ERESTARTSYS;
991 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
992 MoxaPortDCDON(ch->port)))
995 if (signal_pending(current)) {
996 retval = -ERESTARTSYS;
1001 set_current_state(TASK_RUNNING);
1002 remove_wait_queue(&ch->open_wait, &wait);
1004 spin_lock_irqsave(&moxa_lock, flags);
1005 if (!tty_hung_up_p(filp))
1008 spin_unlock_irqrestore(&moxa_lock, flags);
1009 #ifdef SERIAL_DEBUG_OPEN
1010 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1011 ch->line, ch->count);
1015 /* FIXME: review to see if we need to use set_bit on these */
1016 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1020 static void setup_empty_event(struct tty_struct *tty)
1022 struct moxa_str *ch = tty->driver_data;
1023 unsigned long flags;
1025 spin_lock_irqsave(&moxa_lock, flags);
1026 ch->statusflags |= EMPTYWAIT;
1027 mod_timer(&moxaEmptyTimer[ch->port], jiffies + HZ);
1028 spin_unlock_irqrestore(&moxa_lock, flags);
1031 static void check_xmit_empty(unsigned long data)
1033 struct moxa_str *ch;
1035 ch = (struct moxa_str *) data;
1036 del_timer(&moxaEmptyTimer[ch->port]);
1037 if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
1038 if (MoxaPortTxQueue(ch->port) == 0) {
1039 ch->statusflags &= ~EMPTYWAIT;
1040 tty_wakeup(ch->tty);
1043 mod_timer(&moxaEmptyTimer[ch->port], jiffies + HZ);
1045 ch->statusflags &= ~EMPTYWAIT;
1048 static void shut_down(struct moxa_str *ch)
1050 struct tty_struct *tp;
1052 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1057 MoxaPortDisable(ch->port);
1060 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1062 if (tp->termios->c_cflag & HUPCL)
1063 MoxaPortLineCtrl(ch->port, 0, 0);
1065 ch->asyncflags &= ~ASYNC_INITIALIZED;
1068 static void receive_data(struct moxa_str *ch)
1070 struct tty_struct *tp;
1071 struct ktermios *ts;
1072 unsigned long flags;
1078 /**************************************************
1079 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1080 *****************************************************/
1082 MoxaPortFlushData(ch->port, 0);
1085 spin_lock_irqsave(&moxa_lock, flags);
1086 MoxaPortReadData(ch->port, tp);
1087 spin_unlock_irqrestore(&moxa_lock, flags);
1088 tty_schedule_flip(tp);
1091 #define Magic_code 0x404
1094 * System Configuration
1097 * for C218 BIOS initialization
1099 #define C218_ConfBase 0x800
1100 #define C218_status (C218_ConfBase + 0) /* BIOS running status */
1101 #define C218_diag (C218_ConfBase + 2) /* diagnostic status */
1102 #define C218_key (C218_ConfBase + 4) /* WORD (0x218 for C218) */
1103 #define C218DLoad_len (C218_ConfBase + 6) /* WORD */
1104 #define C218check_sum (C218_ConfBase + 8) /* BYTE */
1105 #define C218chksum_ok (C218_ConfBase + 0x0a) /* BYTE (1:ok) */
1106 #define C218_TestRx (C218_ConfBase + 0x10) /* 8 bytes for 8 ports */
1107 #define C218_TestTx (C218_ConfBase + 0x18) /* 8 bytes for 8 ports */
1108 #define C218_RXerr (C218_ConfBase + 0x20) /* 8 bytes for 8 ports */
1109 #define C218_ErrFlag (C218_ConfBase + 0x28) /* 8 bytes for 8 ports */
1111 #define C218_LoadBuf 0x0F00
1112 #define C218_KeyCode 0x218
1113 #define CP204J_KeyCode 0x204
1116 * for C320 BIOS initialization
1118 #define C320_ConfBase 0x800
1119 #define C320_LoadBuf 0x0f00
1120 #define STS_init 0x05 /* for C320_status */
1122 #define C320_status C320_ConfBase + 0 /* BIOS running status */
1123 #define C320_diag C320_ConfBase + 2 /* diagnostic status */
1124 #define C320_key C320_ConfBase + 4 /* WORD (0320H for C320) */
1125 #define C320DLoad_len C320_ConfBase + 6 /* WORD */
1126 #define C320check_sum C320_ConfBase + 8 /* WORD */
1127 #define C320chksum_ok C320_ConfBase + 0x0a /* WORD (1:ok) */
1128 #define C320bapi_len C320_ConfBase + 0x0c /* WORD */
1129 #define C320UART_no C320_ConfBase + 0x0e /* WORD */
1131 #define C320_KeyCode 0x320
1133 #define FixPage_addr 0x0000 /* starting addr of static page */
1134 #define DynPage_addr 0x2000 /* starting addr of dynamic page */
1135 #define C218_start 0x3000 /* starting addr of C218 BIOS prg */
1136 #define Control_reg 0x1ff0 /* select page and reset control */
1137 #define HW_reset 0x80
1142 #define FC_CardReset 0x80
1143 #define FC_ChannelReset 1 /* C320 firmware not supported */
1144 #define FC_EnableCH 2
1145 #define FC_DisableCH 3
1146 #define FC_SetParam 4
1147 #define FC_SetMode 5
1148 #define FC_SetRate 6
1149 #define FC_LineControl 7
1150 #define FC_LineStatus 8
1151 #define FC_XmitControl 9
1152 #define FC_FlushQueue 10
1153 #define FC_SendBreak 11
1154 #define FC_StopBreak 12
1155 #define FC_LoopbackON 13
1156 #define FC_LoopbackOFF 14
1157 #define FC_ClrIrqTable 15
1158 #define FC_SendXon 16
1159 #define FC_SetTermIrq 17 /* C320 firmware not supported */
1160 #define FC_SetCntIrq 18 /* C320 firmware not supported */
1161 #define FC_SetBreakIrq 19
1162 #define FC_SetLineIrq 20
1163 #define FC_SetFlowCtl 21
1164 #define FC_GenIrq 22
1165 #define FC_InCD180 23
1166 #define FC_OutCD180 24
1167 #define FC_InUARTreg 23
1168 #define FC_OutUARTreg 24
1169 #define FC_SetXonXoff 25
1170 #define FC_OutCD180CCR 26
1171 #define FC_ExtIQueue 27
1172 #define FC_ExtOQueue 28
1173 #define FC_ClrLineIrq 29
1174 #define FC_HWFlowCtl 30
1175 #define FC_GetClockRate 35
1176 #define FC_SetBaud 36
1177 #define FC_SetDataMode 41
1178 #define FC_GetCCSR 43
1179 #define FC_GetDataError 45
1180 #define FC_RxControl 50
1181 #define FC_ImmSend 51
1182 #define FC_SetXonState 52
1183 #define FC_SetXoffState 53
1184 #define FC_SetRxFIFOTrig 54
1185 #define FC_SetTxFIFOCnt 55
1186 #define FC_UnixRate 56
1187 #define FC_UnixResetTimer 57
1189 #define RxFIFOTrig1 0
1190 #define RxFIFOTrig4 1
1191 #define RxFIFOTrig8 2
1192 #define RxFIFOTrig14 3
1197 #define DRAM_global 0
1198 #define INT_data (DRAM_global + 0)
1199 #define Config_base (DRAM_global + 0x108)
1201 #define IRQindex (INT_data + 0)
1202 #define IRQpending (INT_data + 4)
1203 #define IRQtable (INT_data + 8)
1208 #define IntrRx 0x01 /* receiver data O.K. */
1209 #define IntrTx 0x02 /* transmit buffer empty */
1210 #define IntrFunc 0x04 /* function complete */
1211 #define IntrBreak 0x08 /* received break */
1212 #define IntrLine 0x10 /* line status change
1214 #define IntrIntr 0x20 /* received INTR code */
1215 #define IntrQuit 0x40 /* received QUIT code */
1216 #define IntrEOF 0x80 /* received EOF code */
1218 #define IntrRxTrigger 0x100 /* rx data count reach tigger value */
1219 #define IntrTxTrigger 0x200 /* tx data count below trigger value */
1221 #define Magic_no (Config_base + 0)
1222 #define Card_model_no (Config_base + 2)
1223 #define Total_ports (Config_base + 4)
1224 #define Module_cnt (Config_base + 8)
1225 #define Module_no (Config_base + 10)
1226 #define Timer_10ms (Config_base + 14)
1227 #define Disable_IRQ (Config_base + 20)
1228 #define TMS320_PORT1 (Config_base + 22)
1229 #define TMS320_PORT2 (Config_base + 24)
1230 #define TMS320_CLOCK (Config_base + 26)
1233 * DATA BUFFER in DRAM
1235 #define Extern_table 0x400 /* Base address of the external table
1236 (24 words * 64) total 3K bytes
1237 (24 words * 128) total 6K bytes */
1238 #define Extern_size 0x60 /* 96 bytes */
1239 #define RXrptr 0x00 /* read pointer for RX buffer */
1240 #define RXwptr 0x02 /* write pointer for RX buffer */
1241 #define TXrptr 0x04 /* read pointer for TX buffer */
1242 #define TXwptr 0x06 /* write pointer for TX buffer */
1243 #define HostStat 0x08 /* IRQ flag and general flag */
1244 #define FlagStat 0x0A
1245 #define FlowControl 0x0C /* B7 B6 B5 B4 B3 B2 B1 B0 */
1246 /* x x x x | | | | */
1247 /* | | | + CTS flow */
1248 /* | | +--- RTS flow */
1249 /* | +------ TX Xon/Xoff */
1250 /* +--------- RX Xon/Xoff */
1251 #define Break_cnt 0x0E /* received break count */
1252 #define CD180TXirq 0x10 /* if non-0: enable TX irq */
1253 #define RX_mask 0x12
1254 #define TX_mask 0x14
1255 #define Ofs_rxb 0x16
1256 #define Ofs_txb 0x18
1257 #define Page_rxb 0x1A
1258 #define Page_txb 0x1C
1259 #define EndPage_rxb 0x1E
1260 #define EndPage_txb 0x20
1261 #define Data_error 0x22
1262 #define RxTrigger 0x28
1263 #define TxTrigger 0x2a
1265 #define rRXwptr 0x34
1266 #define Low_water 0x36
1268 #define FuncCode 0x40
1269 #define FuncArg 0x42
1270 #define FuncArg1 0x44
1272 #define C218rx_size 0x2000 /* 8K bytes */
1273 #define C218tx_size 0x8000 /* 32K bytes */
1275 #define C218rx_mask (C218rx_size - 1)
1276 #define C218tx_mask (C218tx_size - 1)
1278 #define C320p8rx_size 0x2000
1279 #define C320p8tx_size 0x8000
1280 #define C320p8rx_mask (C320p8rx_size - 1)
1281 #define C320p8tx_mask (C320p8tx_size - 1)
1283 #define C320p16rx_size 0x2000
1284 #define C320p16tx_size 0x4000
1285 #define C320p16rx_mask (C320p16rx_size - 1)
1286 #define C320p16tx_mask (C320p16tx_size - 1)
1288 #define C320p24rx_size 0x2000
1289 #define C320p24tx_size 0x2000
1290 #define C320p24rx_mask (C320p24rx_size - 1)
1291 #define C320p24tx_mask (C320p24tx_size - 1)
1293 #define C320p32rx_size 0x1000
1294 #define C320p32tx_size 0x1000
1295 #define C320p32rx_mask (C320p32rx_size - 1)
1296 #define C320p32tx_mask (C320p32tx_size - 1)
1298 #define Page_size 0x2000
1299 #define Page_mask (Page_size - 1)
1300 #define C218rx_spage 3
1301 #define C218tx_spage 4
1302 #define C218rx_pageno 1
1303 #define C218tx_pageno 4
1304 #define C218buf_pageno 5
1306 #define C320p8rx_spage 3
1307 #define C320p8tx_spage 4
1308 #define C320p8rx_pgno 1
1309 #define C320p8tx_pgno 4
1310 #define C320p8buf_pgno 5
1312 #define C320p16rx_spage 3
1313 #define C320p16tx_spage 4
1314 #define C320p16rx_pgno 1
1315 #define C320p16tx_pgno 2
1316 #define C320p16buf_pgno 3
1318 #define C320p24rx_spage 3
1319 #define C320p24tx_spage 4
1320 #define C320p24rx_pgno 1
1321 #define C320p24tx_pgno 1
1322 #define C320p24buf_pgno 2
1324 #define C320p32rx_spage 3
1325 #define C320p32tx_ofs C320p32rx_size
1326 #define C320p32tx_spage 3
1327 #define C320p32buf_pgno 1
1332 #define WakeupRx 0x01
1333 #define WakeupTx 0x02
1334 #define WakeupBreak 0x08
1335 #define WakeupLine 0x10
1336 #define WakeupIntr 0x20
1337 #define WakeupQuit 0x40
1338 #define WakeupEOF 0x80 /* used in VTIME control */
1339 #define WakeupRxTrigger 0x100
1340 #define WakeupTxTrigger 0x200
1344 #define Rx_over 0x01
1345 #define Xoff_state 0x02
1346 #define Tx_flowOff 0x04
1347 #define Tx_enable 0x08
1348 #define CTS_state 0x10
1349 #define DSR_state 0x20
1350 #define DCD_state 0x80
1354 #define CTS_FlowCtl 1
1355 #define RTS_FlowCtl 2
1356 #define Tx_FlowCtl 4
1357 #define Rx_FlowCtl 8
1358 #define IXM_IXANY 0x10
1360 #define LowWater 128
1368 /* mode definition */
1374 #define MX_STOP1 0x00
1375 #define MX_STOP15 0x04
1376 #define MX_STOP2 0x08
1378 #define MX_PARNONE 0x00
1379 #define MX_PAREVEN 0x40
1380 #define MX_PARODD 0xC0
1385 #define QueryPort MAX_PORTS
1391 int rxcnt[MAX_PORTS];
1392 int txcnt[MAX_PORTS];
1394 typedef struct mon_str mon_st;
1396 #define DCD_changed 0x01
1397 #define DCD_oldstate 0x80
1399 static unsigned char moxaBuff[10240];
1400 static void __iomem *moxaIntNdx[MAX_BOARDS];
1401 static void __iomem *moxaIntPend[MAX_BOARDS];
1402 static void __iomem *moxaIntTable[MAX_BOARDS];
1403 static char moxaChkPort[MAX_PORTS];
1404 static char moxaLineCtrl[MAX_PORTS];
1405 static void __iomem *moxaTableAddr[MAX_PORTS];
1406 static long moxaCurBaud[MAX_PORTS];
1407 static char moxaDCDState[MAX_PORTS];
1408 static char moxaLowChkFlag[MAX_PORTS];
1409 static int moxaLowWaterChk;
1410 static int moxaCard;
1411 static mon_st moxaLog;
1412 static int moxaFuncTout = HZ / 2;
1413 static ushort moxaBreakCnt[MAX_PORTS];
1415 static void moxadelay(int);
1416 static void moxafunc(void __iomem *, int, ushort);
1417 static void wait_finish(void __iomem *);
1418 static void low_water_check(void __iomem *);
1419 static int moxaloadbios(int, unsigned char __user *, int);
1420 static int moxafindcard(int);
1421 static int moxaload320b(int, unsigned char __user *, int);
1422 static int moxaloadcode(int, unsigned char __user *, int);
1423 static int moxaloadc218(int, void __iomem *, int);
1424 static int moxaloadc320(int, void __iomem *, int, int *);
1426 /*****************************************************************************
1427 * Driver level functions: *
1428 * 1. MoxaDriverInit(void); *
1429 * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
1430 * 3. MoxaDriverPoll(void); *
1431 *****************************************************************************/
1432 void MoxaDriverInit(void)
1436 moxaFuncTout = HZ / 2; /* 500 mini-seconds */
1439 moxaLowWaterChk = 0;
1440 for (i = 0; i < MAX_PORTS; i++) {
1442 moxaLowChkFlag[i] = 0;
1443 moxaLineCtrl[i] = 0;
1444 moxaLog.rxcnt[i] = 0;
1445 moxaLog.txcnt[i] = 0;
1450 #define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1451 #define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
1452 #define MOXA_INIT_DRIVER (MOXA + 6) /* moxaCard=0 */
1453 #define MOXA_LOAD_BIOS (MOXA + 9) /* download BIOS */
1454 #define MOXA_FIND_BOARD (MOXA + 10) /* Check if MOXA card exist? */
1455 #define MOXA_LOAD_C320B (MOXA + 11) /* download 320B firmware */
1456 #define MOXA_LOAD_CODE (MOXA + 12) /* download firmware */
1457 #define MOXA_GETDATACOUNT (MOXA + 23)
1458 #define MOXA_GET_IOQUEUE (MOXA + 27)
1459 #define MOXA_FLUSH_QUEUE (MOXA + 28)
1460 #define MOXA_GET_CONF (MOXA + 35) /* configuration */
1461 #define MOXA_GET_MAJOR (MOXA + 63)
1462 #define MOXA_GET_CUMAJOR (MOXA + 64)
1463 #define MOXA_GETMSTATUS (MOXA + 65)
1477 static struct moxaq_str temp_queue[MAX_PORTS];
1478 static struct dl_str dltmp;
1480 void MoxaPortFlushData(int port, int mode)
1482 void __iomem *ofsAddr;
1483 if ((mode < 0) || (mode > 2))
1485 ofsAddr = moxaTableAddr[port];
1486 moxafunc(ofsAddr, FC_FlushQueue, mode);
1488 moxaLowChkFlag[port] = 0;
1489 low_water_check(ofsAddr);
1493 int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port)
1497 int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
1498 void __user *argp = (void __user *)arg;
1500 if (port == QueryPort) {
1501 if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_INIT_DRIVER) &&
1502 (cmd != MOXA_LOAD_BIOS) && (cmd != MOXA_FIND_BOARD) && (cmd != MOXA_LOAD_C320B) &&
1503 (cmd != MOXA_LOAD_CODE) && (cmd != MOXA_GETDATACOUNT) &&
1504 (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
1505 (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
1510 if(copy_to_user(argp, &moxa_boards, MAX_BOARDS * sizeof(moxa_board_conf)))
1513 case MOXA_INIT_DRIVER:
1514 if ((int) arg == 0x404)
1517 case MOXA_GETDATACOUNT:
1518 moxaLog.tick = jiffies;
1519 if(copy_to_user(argp, &moxaLog, sizeof(mon_st)))
1522 case MOXA_FLUSH_QUEUE:
1523 MoxaPortFlushData(port, arg);
1525 case MOXA_GET_IOQUEUE:
1526 for (i = 0; i < MAX_PORTS; i++) {
1527 if (moxaChkPort[i]) {
1528 temp_queue[i].inq = MoxaPortRxQueue(i);
1529 temp_queue[i].outq = MoxaPortTxQueue(i);
1532 if(copy_to_user(argp, temp_queue, sizeof(struct moxaq_str) * MAX_PORTS))
1535 case MOXA_GET_OQUEUE:
1536 i = MoxaPortTxQueue(port);
1537 return put_user(i, (unsigned long __user *)argp);
1538 case MOXA_GET_IQUEUE:
1539 i = MoxaPortRxQueue(port);
1540 return put_user(i, (unsigned long __user *)argp);
1541 case MOXA_GET_MAJOR:
1542 if(copy_to_user(argp, &ttymajor, sizeof(int)))
1545 case MOXA_GET_CUMAJOR:
1547 if(copy_to_user(argp, &i, sizeof(int)))
1550 case MOXA_GETMSTATUS:
1551 for (i = 0; i < MAX_PORTS; i++) {
1553 GMStatus[i].dcd = 0;
1554 GMStatus[i].dsr = 0;
1555 GMStatus[i].cts = 0;
1556 if (!moxaChkPort[i]) {
1559 status = MoxaPortLineStatus(moxaChannels[i].port);
1561 GMStatus[i].cts = 1;
1563 GMStatus[i].dsr = 1;
1565 GMStatus[i].dcd = 1;
1568 if (!moxaChannels[i].tty || !moxaChannels[i].tty->termios)
1569 GMStatus[i].cflag = moxaChannels[i].cflag;
1571 GMStatus[i].cflag = moxaChannels[i].tty->termios->c_cflag;
1573 if(copy_to_user(argp, GMStatus, sizeof(struct mxser_mstatus) * MAX_PORTS))
1577 return (-ENOIOCTLCMD);
1578 case MOXA_LOAD_BIOS:
1579 case MOXA_FIND_BOARD:
1580 case MOXA_LOAD_C320B:
1581 case MOXA_LOAD_CODE:
1582 if (!capable(CAP_SYS_RAWIO))
1587 if(copy_from_user(&dltmp, argp, sizeof(struct dl_str)))
1589 if(dltmp.cardno < 0 || dltmp.cardno >= MAX_BOARDS)
1594 case MOXA_LOAD_BIOS:
1595 i = moxaloadbios(dltmp.cardno, dltmp.buf, dltmp.len);
1597 case MOXA_FIND_BOARD:
1598 return moxafindcard(dltmp.cardno);
1599 case MOXA_LOAD_C320B:
1600 moxaload320b(dltmp.cardno, dltmp.buf, dltmp.len);
1601 default: /* to keep gcc happy */
1603 case MOXA_LOAD_CODE:
1604 i = moxaloadcode(dltmp.cardno, dltmp.buf, dltmp.len);
1612 int MoxaDriverPoll(void)
1614 register ushort temp;
1616 void __iomem *ofsAddr;
1622 for (card = 0; card < MAX_BOARDS; card++) {
1623 if (loadstat[card] == 0)
1625 if ((ports = moxa_boards[card].numPorts) == 0)
1627 if (readb(moxaIntPend[card]) == 0xff) {
1628 ip = moxaIntTable[card] + readb(moxaIntNdx[card]);
1629 p = card * MAX_PORTS_PER_BOARD;
1631 for (port = 0; port < ports; port += 2, p++) {
1632 if ((temp = readw(ip + port)) != 0) {
1633 writew(0, ip + port);
1634 ofsAddr = moxaTableAddr[p];
1636 writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
1637 if (temp & IntrBreak) {
1640 if (temp & IntrLine) {
1641 if (readb(ofsAddr + FlagStat) & DCD_state) {
1642 if ((moxaDCDState[p] & DCD_oldstate) == 0)
1643 moxaDCDState[p] = (DCD_oldstate |
1646 if (moxaDCDState[p] & DCD_oldstate)
1647 moxaDCDState[p] = DCD_changed;
1652 writeb(0, moxaIntPend[card]);
1654 if (moxaLowWaterChk) {
1655 p = card * MAX_PORTS_PER_BOARD;
1656 for (port = 0; port < ports; port++, p++) {
1657 if (moxaLowChkFlag[p]) {
1658 moxaLowChkFlag[p] = 0;
1659 ofsAddr = moxaTableAddr[p];
1660 low_water_check(ofsAddr);
1665 moxaLowWaterChk = 0;
1669 /*****************************************************************************
1670 * Card level function: *
1671 * 1. MoxaPortsOfCard(int cardno); *
1672 *****************************************************************************/
1673 int MoxaPortsOfCard(int cardno)
1676 if (moxa_boards[cardno].boardType == 0)
1678 return (moxa_boards[cardno].numPorts);
1681 /*****************************************************************************
1682 * Port level functions: *
1683 * 1. MoxaPortIsValid(int port); *
1684 * 2. MoxaPortEnable(int port); *
1685 * 3. MoxaPortDisable(int port); *
1686 * 4. MoxaPortGetMaxBaud(int port); *
1687 * 5. MoxaPortGetCurBaud(int port); *
1688 * 6. MoxaPortSetBaud(int port, long baud); *
1689 * 7. MoxaPortSetMode(int port, int databit, int stopbit, int parity); *
1690 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1691 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1692 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1693 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1694 * 12. MoxaPortLineStatus(int port); *
1695 * 13. MoxaPortDCDChange(int port); *
1696 * 14. MoxaPortDCDON(int port); *
1697 * 15. MoxaPortFlushData(int port, int mode); *
1698 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1699 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
1700 * 18. MoxaPortTxBufSize(int port); *
1701 * 19. MoxaPortRxBufSize(int port); *
1702 * 20. MoxaPortTxQueue(int port); *
1703 * 21. MoxaPortTxFree(int port); *
1704 * 22. MoxaPortRxQueue(int port); *
1705 * 23. MoxaPortRxFree(int port); *
1706 * 24. MoxaPortTxDisable(int port); *
1707 * 25. MoxaPortTxEnable(int port); *
1708 * 26. MoxaPortGetBrkCnt(int port); *
1709 * 27. MoxaPortResetBrkCnt(int port); *
1710 * 28. MoxaPortSetXonXoff(int port, int xonValue, int xoffValue); *
1711 * 29. MoxaPortIsTxHold(int port); *
1712 * 30. MoxaPortSendBreak(int port, int ticks); *
1713 *****************************************************************************/
1715 * Moxa Port Number Description:
1717 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1718 * the port number using in MOXA driver functions will be 0 to 31 for
1719 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1720 * to 127 for fourth. For example, if you setup three MOXA boards,
1721 * first board is C218, second board is C320-16 and third board is
1722 * C320-32. The port number of first board (C218 - 8 ports) is from
1723 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1724 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1725 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1726 * 127 will be invalid.
1729 * Moxa Functions Description:
1731 * Function 1: Driver initialization routine, this routine must be
1732 * called when initialized driver.
1734 * void MoxaDriverInit();
1737 * Function 2: Moxa driver private IOCTL command processing.
1739 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1741 * unsigned int cmd : IOCTL command
1742 * unsigned long arg : IOCTL argument
1743 * int port : port number (0 - 127)
1750 * Function 3: Moxa driver polling process routine.
1752 * int MoxaDriverPoll(void);
1754 * return: 0 ; polling O.K.
1755 * -1 : no any Moxa card.
1758 * Function 4: Get the ports of this card.
1760 * int MoxaPortsOfCard(int cardno);
1762 * int cardno : card number (0 - 3)
1764 * return: 0 : this card is invalid
1768 * Function 5: Check this port is valid or invalid
1770 * int MoxaPortIsValid(int port);
1771 * int port : port number (0 - 127, ref port description)
1773 * return: 0 : this port is invalid
1774 * 1 : this port is valid
1777 * Function 6: Enable this port to start Tx/Rx data.
1779 * void MoxaPortEnable(int port);
1780 * int port : port number (0 - 127)
1783 * Function 7: Disable this port
1785 * void MoxaPortDisable(int port);
1786 * int port : port number (0 - 127)
1789 * Function 8: Get the maximun available baud rate of this port.
1791 * long MoxaPortGetMaxBaud(int port);
1792 * int port : port number (0 - 127)
1794 * return: 0 : this port is invalid
1795 * 38400/57600/115200 bps
1798 * Function 9: Get the current baud rate of this port.
1800 * long MoxaPortGetCurBaud(int port);
1801 * int port : port number (0 - 127)
1803 * return: 0 : this port is invalid
1807 * Function 10: Setting baud rate of this port.
1809 * long MoxaPortSetBaud(int port, long baud);
1810 * int port : port number (0 - 127)
1811 * long baud : baud rate (50 - 115200)
1813 * return: 0 : this port is invalid or baud < 50
1814 * 50 - 115200 : the real baud rate set to the port, if
1815 * the argument baud is large than maximun
1816 * available baud rate, the real setting
1817 * baud rate will be the maximun baud rate.
1820 * Function 11: Setting the data-bits/stop-bits/parity of this port
1822 * int MoxaPortSetMode(int port, int databits, int stopbits, int parity);
1823 * int port : port number (0 - 127)
1824 * int databits : data bits (8/7/6/5)
1825 * int stopbits : stop bits (2/1/0, 0 show 1.5 stop bits)
1826 int parity : parity (0:None,1:Odd,2:Even,3:Mark,4:Space)
1828 * return: -1 : invalid parameter
1832 * Function 12: Configure the port.
1834 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1835 * int port : port number (0 - 127)
1836 * struct ktermios * termio : termio structure pointer
1837 * speed_t baud : baud rate
1839 * return: -1 : this port is invalid or termio == NULL
1843 * Function 13: Get the DTR/RTS state of this port.
1845 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1846 * int port : port number (0 - 127)
1847 * int * dtrState : pointer to INT to receive the current DTR
1848 * state. (if NULL, this function will not
1849 * write to this address)
1850 * int * rtsState : pointer to INT to receive the current RTS
1851 * state. (if NULL, this function will not
1852 * write to this address)
1854 * return: -1 : this port is invalid
1858 * Function 14: Setting the DTR/RTS output state of this port.
1860 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1861 * int port : port number (0 - 127)
1862 * int dtrState : DTR output state (0: off, 1: on)
1863 * int rtsState : RTS output state (0: off, 1: on)
1866 * Function 15: Setting the flow control of this port.
1868 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1869 * int txFlow,int xany);
1870 * int port : port number (0 - 127)
1871 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1872 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1873 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1874 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1875 * int xany : S/W XANY flow control (0: no, 1: yes)
1878 * Function 16: Get ths line status of this port
1880 * int MoxaPortLineStatus(int port);
1881 * int port : port number (0 - 127)
1883 * return: Bit 0 - CTS state (0: off, 1: on)
1884 * Bit 1 - DSR state (0: off, 1: on)
1885 * Bit 2 - DCD state (0: off, 1: on)
1888 * Function 17: Check the DCD state has changed since the last read
1891 * int MoxaPortDCDChange(int port);
1892 * int port : port number (0 - 127)
1894 * return: 0 : no changed
1895 * 1 : DCD has changed
1898 * Function 18: Check ths current DCD state is ON or not.
1900 * int MoxaPortDCDON(int port);
1901 * int port : port number (0 - 127)
1903 * return: 0 : DCD off
1907 * Function 19: Flush the Rx/Tx buffer data of this port.
1909 * void MoxaPortFlushData(int port, int mode);
1910 * int port : port number (0 - 127)
1912 * 0 : flush the Rx buffer
1913 * 1 : flush the Tx buffer
1914 * 2 : flush the Rx and Tx buffer
1917 * Function 20: Write data.
1919 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1920 * int port : port number (0 - 127)
1921 * unsigned char * buffer : pointer to write data buffer.
1922 * int length : write data length
1924 * return: 0 - length : real write data length
1927 * Function 21: Read data.
1929 * int MoxaPortReadData(int port, struct tty_struct *tty);
1930 * int port : port number (0 - 127)
1931 * struct tty_struct *tty : tty for data
1933 * return: 0 - length : real read data length
1936 * Function 22: Get the Tx buffer size of this port
1938 * int MoxaPortTxBufSize(int port);
1939 * int port : port number (0 - 127)
1941 * return: .. : Tx buffer size
1944 * Function 23: Get the Rx buffer size of this port
1946 * int MoxaPortRxBufSize(int port);
1947 * int port : port number (0 - 127)
1949 * return: .. : Rx buffer size
1952 * Function 24: Get the Tx buffer current queued data bytes
1954 * int MoxaPortTxQueue(int port);
1955 * int port : port number (0 - 127)
1957 * return: .. : Tx buffer current queued data bytes
1960 * Function 25: Get the Tx buffer current free space
1962 * int MoxaPortTxFree(int port);
1963 * int port : port number (0 - 127)
1965 * return: .. : Tx buffer current free space
1968 * Function 26: Get the Rx buffer current queued data bytes
1970 * int MoxaPortRxQueue(int port);
1971 * int port : port number (0 - 127)
1973 * return: .. : Rx buffer current queued data bytes
1976 * Function 27: Get the Rx buffer current free space
1978 * int MoxaPortRxFree(int port);
1979 * int port : port number (0 - 127)
1981 * return: .. : Rx buffer current free space
1984 * Function 28: Disable port data transmission.
1986 * void MoxaPortTxDisable(int port);
1987 * int port : port number (0 - 127)
1990 * Function 29: Enable port data transmission.
1992 * void MoxaPortTxEnable(int port);
1993 * int port : port number (0 - 127)
1996 * Function 30: Get the received BREAK signal count.
1998 * int MoxaPortGetBrkCnt(int port);
1999 * int port : port number (0 - 127)
2001 * return: 0 - .. : BREAK signal count
2004 * Function 31: Get the received BREAK signal count and reset it.
2006 * int MoxaPortResetBrkCnt(int port);
2007 * int port : port number (0 - 127)
2009 * return: 0 - .. : BREAK signal count
2012 * Function 32: Set the S/W flow control new XON/XOFF value, default
2013 * XON is 0x11 & XOFF is 0x13.
2015 * void MoxaPortSetXonXoff(int port, int xonValue, int xoffValue);
2016 * int port : port number (0 - 127)
2017 * int xonValue : new XON value (0 - 255)
2018 * int xoffValue : new XOFF value (0 - 255)
2021 * Function 33: Check this port's transmission is hold by remote site
2022 * because the flow control.
2024 * int MoxaPortIsTxHold(int port);
2025 * int port : port number (0 - 127)
2027 * return: 0 : normal
2028 * 1 : hold by remote site
2031 * Function 34: Send out a BREAK signal.
2033 * void MoxaPortSendBreak(int port, int ms100);
2034 * int port : port number (0 - 127)
2035 * int ms100 : break signal time interval.
2036 * unit: 100 mini-second. if ms100 == 0, it will
2037 * send out a about 250 ms BREAK signal.
2040 int MoxaPortIsValid(int port)
2045 if (moxaChkPort[port] == 0)
2050 void MoxaPortEnable(int port)
2052 void __iomem *ofsAddr;
2053 int MoxaPortLineStatus(int);
2054 short lowwater = 512;
2056 ofsAddr = moxaTableAddr[port];
2057 writew(lowwater, ofsAddr + Low_water);
2058 moxaBreakCnt[port] = 0;
2059 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2060 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2061 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
2063 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
2066 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
2067 moxafunc(ofsAddr, FC_FlushQueue, 2);
2069 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
2070 MoxaPortLineStatus(port);
2073 void MoxaPortDisable(int port)
2075 void __iomem *ofsAddr = moxaTableAddr[port];
2077 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
2078 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
2079 writew(0, ofsAddr + HostStat);
2080 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
2083 long MoxaPortGetMaxBaud(int port)
2085 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2086 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI))
2093 long MoxaPortSetBaud(int port, long baud)
2095 void __iomem *ofsAddr;
2099 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
2101 ofsAddr = moxaTableAddr[port];
2105 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
2106 else if (max == 57600L)
2107 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
2109 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
2111 moxafunc(ofsAddr, FC_SetBaud, val);
2113 moxaCurBaud[port] = baud;
2117 int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud)
2119 void __iomem *ofsAddr;
2123 if (moxaChkPort[port] == 0 || termio == 0)
2125 ofsAddr = moxaTableAddr[port];
2126 cflag = termio->c_cflag; /* termio->c_cflag */
2128 mode = termio->c_cflag & CSIZE;
2131 else if (mode == CS6)
2133 else if (mode == CS7)
2135 else if (mode == CS8)
2138 if (termio->c_cflag & CSTOPB) {
2146 if (termio->c_cflag & PARENB) {
2147 if (termio->c_cflag & PARODD)
2154 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
2156 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2157 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2158 if (baud >= 921600L)
2161 MoxaPortSetBaud(port, baud);
2163 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
2164 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
2165 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
2166 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
2167 wait_finish(ofsAddr);
2173 int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState)
2176 if (!MoxaPortIsValid(port))
2179 if (moxaLineCtrl[port] & DTR_ON)
2185 if (moxaLineCtrl[port] & RTS_ON)
2193 void MoxaPortLineCtrl(int port, int dtr, int rts)
2195 void __iomem *ofsAddr;
2198 ofsAddr = moxaTableAddr[port];
2204 moxaLineCtrl[port] = mode;
2205 moxafunc(ofsAddr, FC_LineControl, mode);
2208 void MoxaPortFlowCtrl(int port, int rts, int cts, int txflow, int rxflow, int txany)
2210 void __iomem *ofsAddr;
2213 ofsAddr = moxaTableAddr[port];
2216 mode |= RTS_FlowCtl;
2218 mode |= CTS_FlowCtl;
2225 moxafunc(ofsAddr, FC_SetFlowCtl, mode);
2228 int MoxaPortLineStatus(int port)
2230 void __iomem *ofsAddr;
2233 ofsAddr = moxaTableAddr[port];
2234 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2235 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2236 moxafunc(ofsAddr, FC_LineStatus, 0);
2237 val = readw(ofsAddr + FuncArg);
2239 val = readw(ofsAddr + FlagStat) >> 4;
2244 if ((moxaDCDState[port] & DCD_oldstate) == 0)
2245 moxaDCDState[port] = (DCD_oldstate | DCD_changed);
2247 if (moxaDCDState[port] & DCD_oldstate)
2248 moxaDCDState[port] = DCD_changed;
2254 int MoxaPortDCDChange(int port)
2258 if (moxaChkPort[port] == 0)
2260 n = moxaDCDState[port];
2261 moxaDCDState[port] &= ~DCD_changed;
2266 int MoxaPortDCDON(int port)
2270 if (moxaChkPort[port] == 0)
2272 if (moxaDCDState[port] & DCD_oldstate)
2281 int MoxaDumpMem(int port, unsigned char * buffer, int len)
2284 unsigned long baseAddr,ofsAddr,ofs;
2286 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2287 ofs = baseAddr + DynPage_addr + pageofs;
2290 for (i = 0; i < len; i++)
2291 buffer[i] = readb(ofs+i);
2296 int MoxaPortWriteData(int port, unsigned char * buffer, int len)
2301 ushort head, tx_mask, spage, epage;
2302 ushort pageno, pageofs, bufhead;
2303 void __iomem *baseAddr, *ofsAddr, *ofs;
2305 ofsAddr = moxaTableAddr[port];
2306 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2307 tx_mask = readw(ofsAddr + TX_mask);
2308 spage = readw(ofsAddr + Page_txb);
2309 epage = readw(ofsAddr + EndPage_txb);
2310 tail = readw(ofsAddr + TXwptr);
2311 head = readw(ofsAddr + TXrptr);
2312 c = (head > tail) ? (head - tail - 1)
2313 : (head - tail + tx_mask);
2316 moxaLog.txcnt[port] += c;
2318 if (spage == epage) {
2319 bufhead = readw(ofsAddr + Ofs_txb);
2320 writew(spage, baseAddr + Control_reg);
2323 len = head - tail - 1;
2325 len = tx_mask + 1 - tail;
2326 len = (c > len) ? len : c;
2327 ofs = baseAddr + DynPage_addr + bufhead + tail;
2328 for (i = 0; i < len; i++)
2329 writeb(*buffer++, ofs + i);
2330 tail = (tail + len) & tx_mask;
2333 writew(tail, ofsAddr + TXwptr);
2336 pageno = spage + (tail >> 13);
2337 pageofs = tail & Page_mask;
2339 cnt = Page_size - pageofs;
2343 writeb(pageno, baseAddr + Control_reg);
2344 ofs = baseAddr + DynPage_addr + pageofs;
2345 for (i = 0; i < cnt; i++)
2346 writeb(*buffer++, ofs + i);
2348 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2351 if (++pageno == epage)
2356 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2360 int MoxaPortReadData(int port, struct tty_struct *tty)
2362 register ushort head, pageofs;
2363 int i, count, cnt, len, total, remain;
2364 ushort tail, rx_mask, spage, epage;
2365 ushort pageno, bufhead;
2366 void __iomem *baseAddr, *ofsAddr, *ofs;
2368 ofsAddr = moxaTableAddr[port];
2369 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2370 head = readw(ofsAddr + RXrptr);
2371 tail = readw(ofsAddr + RXwptr);
2372 rx_mask = readw(ofsAddr + RX_mask);
2373 spage = readw(ofsAddr + Page_rxb);
2374 epage = readw(ofsAddr + EndPage_rxb);
2375 count = (tail >= head) ? (tail - head)
2376 : (tail - head + rx_mask + 1);
2381 remain = count - total;
2382 moxaLog.rxcnt[port] += total;
2384 if (spage == epage) {
2385 bufhead = readw(ofsAddr + Ofs_rxb);
2386 writew(spage, baseAddr + Control_reg);
2391 len = rx_mask + 1 - head;
2392 len = (count > len) ? len : count;
2393 ofs = baseAddr + DynPage_addr + bufhead + head;
2394 for (i = 0; i < len; i++)
2395 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2396 head = (head + len) & rx_mask;
2399 writew(head, ofsAddr + RXrptr);
2402 pageno = spage + (head >> 13);
2403 pageofs = head & Page_mask;
2405 cnt = Page_size - pageofs;
2409 writew(pageno, baseAddr + Control_reg);
2410 ofs = baseAddr + DynPage_addr + pageofs;
2411 for (i = 0; i < cnt; i++)
2412 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2414 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2417 if (++pageno == epage)
2422 if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2423 moxaLowWaterChk = 1;
2424 moxaLowChkFlag[port] = 1;
2430 int MoxaPortTxQueue(int port)
2432 void __iomem *ofsAddr;
2433 ushort rptr, wptr, mask;
2436 ofsAddr = moxaTableAddr[port];
2437 rptr = readw(ofsAddr + TXrptr);
2438 wptr = readw(ofsAddr + TXwptr);
2439 mask = readw(ofsAddr + TX_mask);
2440 len = (wptr - rptr) & mask;
2444 int MoxaPortTxFree(int port)
2446 void __iomem *ofsAddr;
2447 ushort rptr, wptr, mask;
2450 ofsAddr = moxaTableAddr[port];
2451 rptr = readw(ofsAddr + TXrptr);
2452 wptr = readw(ofsAddr + TXwptr);
2453 mask = readw(ofsAddr + TX_mask);
2454 len = mask - ((wptr - rptr) & mask);
2458 int MoxaPortRxQueue(int port)
2460 void __iomem *ofsAddr;
2461 ushort rptr, wptr, mask;
2464 ofsAddr = moxaTableAddr[port];
2465 rptr = readw(ofsAddr + RXrptr);
2466 wptr = readw(ofsAddr + RXwptr);
2467 mask = readw(ofsAddr + RX_mask);
2468 len = (wptr - rptr) & mask;
2473 void MoxaPortTxDisable(int port)
2475 void __iomem *ofsAddr;
2477 ofsAddr = moxaTableAddr[port];
2478 moxafunc(ofsAddr, FC_SetXoffState, Magic_code);
2481 void MoxaPortTxEnable(int port)
2483 void __iomem *ofsAddr;
2485 ofsAddr = moxaTableAddr[port];
2486 moxafunc(ofsAddr, FC_SetXonState, Magic_code);
2490 int MoxaPortResetBrkCnt(int port)
2493 cnt = moxaBreakCnt[port];
2494 moxaBreakCnt[port] = 0;
2499 void MoxaPortSendBreak(int port, int ms100)
2501 void __iomem *ofsAddr;
2503 ofsAddr = moxaTableAddr[port];
2505 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2506 moxadelay(ms100 * (HZ / 10));
2508 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2509 moxadelay(HZ / 4); /* 250 ms */
2511 moxafunc(ofsAddr, FC_StopBreak, Magic_code);
2514 static int moxa_get_serial_info(struct moxa_str *info,
2515 struct serial_struct __user *retinfo)
2517 struct serial_struct tmp;
2519 memset(&tmp, 0, sizeof(tmp));
2520 tmp.type = info->type;
2521 tmp.line = info->port;
2524 tmp.flags = info->asyncflags;
2525 tmp.baud_base = 921600;
2526 tmp.close_delay = info->close_delay;
2527 tmp.closing_wait = info->closing_wait;
2528 tmp.custom_divisor = 0;
2530 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2536 static int moxa_set_serial_info(struct moxa_str *info,
2537 struct serial_struct __user *new_info)
2539 struct serial_struct new_serial;
2541 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2544 if ((new_serial.irq != 0) ||
2545 (new_serial.port != 0) ||
2546 // (new_serial.type != info->type) ||
2547 (new_serial.custom_divisor != 0) ||
2548 (new_serial.baud_base != 921600))
2551 if (!capable(CAP_SYS_ADMIN)) {
2552 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2553 (info->asyncflags & ~ASYNC_USR_MASK)))
2556 info->close_delay = new_serial.close_delay * HZ / 100;
2557 info->closing_wait = new_serial.closing_wait * HZ / 100;
2560 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2561 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2563 if (new_serial.type == PORT_16550A) {
2564 MoxaSetFifo(info->port, 1);
2566 MoxaSetFifo(info->port, 0);
2569 info->type = new_serial.type;
2575 /*****************************************************************************
2576 * Static local functions: *
2577 *****************************************************************************/
2579 * moxadelay - delays a specified number ticks
2581 static void moxadelay(int tick)
2583 unsigned long st, et;
2587 while (time_before(jiffies, et));
2590 static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
2593 writew(arg, ofsAddr + FuncArg);
2594 writew(cmd, ofsAddr + FuncCode);
2595 wait_finish(ofsAddr);
2598 static void wait_finish(void __iomem *ofsAddr)
2603 while (readw(ofsAddr + FuncCode) != 0) {
2605 if ((j - i) > moxaFuncTout) {
2611 static void low_water_check(void __iomem *ofsAddr)
2614 ushort rptr, wptr, mask;
2616 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2617 rptr = readw(ofsAddr + RXrptr);
2618 wptr = readw(ofsAddr + RXwptr);
2619 mask = readw(ofsAddr + RX_mask);
2620 len = (wptr - rptr) & mask;
2621 if (len <= Low_water)
2622 moxafunc(ofsAddr, FC_SendXon, 0);
2626 static int moxaloadbios(int cardno, unsigned char __user *tmp, int len)
2628 void __iomem *baseAddr;
2631 if(copy_from_user(moxaBuff, tmp, len))
2633 baseAddr = moxaBaseAddr[cardno];
2634 writeb(HW_reset, baseAddr + Control_reg); /* reset */
2635 moxadelay(1); /* delay 10 ms */
2636 for (i = 0; i < 4096; i++)
2637 writeb(0, baseAddr + i); /* clear fix page */
2638 for (i = 0; i < len; i++)
2639 writeb(moxaBuff[i], baseAddr + i); /* download BIOS */
2640 writeb(0, baseAddr + Control_reg); /* restart */
2644 static int moxafindcard(int cardno)
2646 void __iomem *baseAddr;
2649 baseAddr = moxaBaseAddr[cardno];
2650 switch (moxa_boards[cardno].boardType) {
2651 case MOXA_BOARD_C218_ISA:
2652 case MOXA_BOARD_C218_PCI:
2653 if ((tmp = readw(baseAddr + C218_key)) != C218_KeyCode) {
2657 case MOXA_BOARD_CP204J:
2658 if ((tmp = readw(baseAddr + C218_key)) != CP204J_KeyCode) {
2663 if ((tmp = readw(baseAddr + C320_key)) != C320_KeyCode) {
2666 if ((tmp = readw(baseAddr + C320_status)) != STS_init) {
2673 static int moxaload320b(int cardno, unsigned char __user *tmp, int len)
2675 void __iomem *baseAddr;
2678 if(len > sizeof(moxaBuff))
2680 if(copy_from_user(moxaBuff, tmp, len))
2682 baseAddr = moxaBaseAddr[cardno];
2683 writew(len - 7168 - 2, baseAddr + C320bapi_len);
2684 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
2685 for (i = 0; i < 7168; i++)
2686 writeb(moxaBuff[i], baseAddr + DynPage_addr + i);
2687 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
2688 for (i = 0; i < (len - 7168); i++)
2689 writeb(moxaBuff[i + 7168], baseAddr + DynPage_addr + i);
2693 static int moxaloadcode(int cardno, unsigned char __user *tmp, int len)
2695 void __iomem *baseAddr, *ofsAddr;
2696 int retval, port, i;
2698 if(copy_from_user(moxaBuff, tmp, len))
2700 baseAddr = moxaBaseAddr[cardno];
2701 switch (moxa_boards[cardno].boardType) {
2702 case MOXA_BOARD_C218_ISA:
2703 case MOXA_BOARD_C218_PCI:
2704 case MOXA_BOARD_CP204J:
2705 retval = moxaloadc218(cardno, baseAddr, len);
2708 port = cardno * MAX_PORTS_PER_BOARD;
2709 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2710 moxaChkPort[port] = 1;
2711 moxaCurBaud[port] = 9600L;
2712 moxaDCDState[port] = 0;
2713 moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
2714 ofsAddr = moxaTableAddr[port];
2715 writew(C218rx_mask, ofsAddr + RX_mask);
2716 writew(C218tx_mask, ofsAddr + TX_mask);
2717 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
2718 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
2720 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
2721 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
2726 retval = moxaloadc320(cardno, baseAddr, len,
2727 &moxa_boards[cardno].numPorts);
2730 port = cardno * MAX_PORTS_PER_BOARD;
2731 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2732 moxaChkPort[port] = 1;
2733 moxaCurBaud[port] = 9600L;
2734 moxaDCDState[port] = 0;
2735 moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
2736 ofsAddr = moxaTableAddr[port];
2737 if (moxa_boards[cardno].numPorts == 8) {
2738 writew(C320p8rx_mask, ofsAddr + RX_mask);
2739 writew(C320p8tx_mask, ofsAddr + TX_mask);
2740 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
2741 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
2742 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
2743 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
2745 } else if (moxa_boards[cardno].numPorts == 16) {
2746 writew(C320p16rx_mask, ofsAddr + RX_mask);
2747 writew(C320p16tx_mask, ofsAddr + TX_mask);
2748 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
2749 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
2750 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
2751 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
2753 } else if (moxa_boards[cardno].numPorts == 24) {
2754 writew(C320p24rx_mask, ofsAddr + RX_mask);
2755 writew(C320p24tx_mask, ofsAddr + TX_mask);
2756 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
2757 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
2758 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
2759 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2760 } else if (moxa_boards[cardno].numPorts == 32) {
2761 writew(C320p32rx_mask, ofsAddr + RX_mask);
2762 writew(C320p32tx_mask, ofsAddr + TX_mask);
2763 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
2764 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
2765 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
2766 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
2767 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2772 loadstat[cardno] = 1;
2776 static int moxaloadc218(int cardno, void __iomem *baseAddr, int len)
2779 int i, j, len1, len2;
2780 ushort usum, *ptr, keycode;
2782 if (moxa_boards[cardno].boardType == MOXA_BOARD_CP204J)
2783 keycode = CP204J_KeyCode;
2785 keycode = C218_KeyCode;
2788 ptr = (ushort *) moxaBuff;
2789 for (i = 0; i < len1; i++)
2790 usum += le16_to_cpu(*(ptr + i));
2796 len2 = (len1 > 2048) ? 2048 : len1;
2798 for (i = 0; i < len2 << 1; i++)
2799 writeb(moxaBuff[i + j], baseAddr + C218_LoadBuf + i);
2802 writew(len2, baseAddr + C218DLoad_len);
2803 writew(0, baseAddr + C218_key);
2804 for (i = 0; i < 100; i++) {
2805 if (readw(baseAddr + C218_key) == keycode)
2807 moxadelay(1); /* delay 10 ms */
2809 if (readw(baseAddr + C218_key) != keycode) {
2813 writew(0, baseAddr + C218DLoad_len);
2814 writew(usum, baseAddr + C218check_sum);
2815 writew(0, baseAddr + C218_key);
2816 for (i = 0; i < 100; i++) {
2817 if (readw(baseAddr + C218_key) == keycode)
2819 moxadelay(1); /* delay 10 ms */
2822 } while ((readb(baseAddr + C218chksum_ok) != 1) && (retry < 3));
2823 if (readb(baseAddr + C218chksum_ok) != 1) {
2826 writew(0, baseAddr + C218_key);
2827 for (i = 0; i < 100; i++) {
2828 if (readw(baseAddr + Magic_no) == Magic_code)
2830 moxadelay(1); /* delay 10 ms */
2832 if (readw(baseAddr + Magic_no) != Magic_code) {
2835 writew(1, baseAddr + Disable_IRQ);
2836 writew(0, baseAddr + Magic_no);
2837 for (i = 0; i < 100; i++) {
2838 if (readw(baseAddr + Magic_no) == Magic_code)
2840 moxadelay(1); /* delay 10 ms */
2842 if (readw(baseAddr + Magic_no) != Magic_code) {
2846 moxaIntNdx[cardno] = baseAddr + IRQindex;
2847 moxaIntPend[cardno] = baseAddr + IRQpending;
2848 moxaIntTable[cardno] = baseAddr + IRQtable;
2852 static int moxaloadc320(int cardno, void __iomem *baseAddr, int len, int *numPorts)
2855 int i, j, wlen, len2, retry;
2860 uptr = (ushort *) moxaBuff;
2861 for (i = 0; i < wlen; i++)
2862 usum += le16_to_cpu(uptr[i]);
2873 for (i = 0; i < len2; i++)
2874 writeb(moxaBuff[j + i], baseAddr + C320_LoadBuf + i);
2877 writew(len2, baseAddr + C320DLoad_len);
2878 writew(0, baseAddr + C320_key);
2879 for (i = 0; i < 10; i++) {
2880 if (readw(baseAddr + C320_key) == C320_KeyCode)
2884 if (readw(baseAddr + C320_key) != C320_KeyCode)
2887 writew(0, baseAddr + C320DLoad_len);
2888 writew(usum, baseAddr + C320check_sum);
2889 writew(0, baseAddr + C320_key);
2890 for (i = 0; i < 10; i++) {
2891 if (readw(baseAddr + C320_key) == C320_KeyCode)
2896 } while ((readb(baseAddr + C320chksum_ok) != 1) && (retry < 3));
2897 if (readb(baseAddr + C320chksum_ok) != 1)
2899 writew(0, baseAddr + C320_key);
2900 for (i = 0; i < 600; i++) {
2901 if (readw(baseAddr + Magic_no) == Magic_code)
2905 if (readw(baseAddr + Magic_no) != Magic_code)
2908 if (moxa_boards[cardno].busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
2909 writew(0x3800, baseAddr + TMS320_PORT1);
2910 writew(0x3900, baseAddr + TMS320_PORT2);
2911 writew(28499, baseAddr + TMS320_CLOCK);
2913 writew(0x3200, baseAddr + TMS320_PORT1);
2914 writew(0x3400, baseAddr + TMS320_PORT2);
2915 writew(19999, baseAddr + TMS320_CLOCK);
2917 writew(1, baseAddr + Disable_IRQ);
2918 writew(0, baseAddr + Magic_no);
2919 for (i = 0; i < 500; i++) {
2920 if (readw(baseAddr + Magic_no) == Magic_code)
2924 if (readw(baseAddr + Magic_no) != Magic_code)
2927 j = readw(baseAddr + Module_cnt);
2931 writew(j, baseAddr + Module_no);
2932 writew(0, baseAddr + Magic_no);
2933 for (i = 0; i < 600; i++) {
2934 if (readw(baseAddr + Magic_no) == Magic_code)
2938 if (readw(baseAddr + Magic_no) != Magic_code)
2941 moxaIntNdx[cardno] = baseAddr + IRQindex;
2942 moxaIntPend[cardno] = baseAddr + IRQpending;
2943 moxaIntTable[cardno] = baseAddr + IRQtable;
2948 long MoxaPortGetCurBaud(int port)
2951 if (moxaChkPort[port] == 0)
2953 return (moxaCurBaud[port]);
2957 static void MoxaSetFifo(int port, int enable)
2959 void __iomem *ofsAddr = moxaTableAddr[port];
2962 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2963 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2965 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2966 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
2971 int MoxaPortSetMode(int port, int databits, int stopbits, int parity)
2973 void __iomem *ofsAddr;
2996 break; /* stop bits 1.5 */
3025 ofsAddr = moxaTableAddr[port];
3026 moxafunc(ofsAddr, FC_SetMode, val);
3030 int MoxaPortTxBufSize(int port)
3032 void __iomem *ofsAddr;
3035 ofsAddr = moxaTableAddr[port];
3036 size = readw(ofsAddr + TX_mask);
3040 int MoxaPortRxBufSize(int port)
3042 void __iomem *ofsAddr;
3045 ofsAddr = moxaTableAddr[port];
3046 size = readw(ofsAddr + RX_mask);
3050 int MoxaPortRxFree(int port)
3052 void __iomem *ofsAddr;
3053 ushort rptr, wptr, mask;
3056 ofsAddr = moxaTableAddr[port];
3057 rptr = readw(ofsAddr + RXrptr);
3058 wptr = readw(ofsAddr + RXwptr);
3059 mask = readw(ofsAddr + RX_mask);
3060 len = mask - ((wptr - rptr) & mask);
3063 int MoxaPortGetBrkCnt(int port)
3065 return (moxaBreakCnt[port]);
3068 void MoxaPortSetXonXoff(int port, int xonValue, int xoffValue)
3070 void __iomem *ofsAddr;
3072 ofsAddr = moxaTableAddr[port];
3073 writew(xonValue, ofsAddr + FuncArg);
3074 writew(xoffValue, ofsAddr + FuncArg1);
3075 writew(FC_SetXonXoff, ofsAddr + FuncCode);
3076 wait_finish(ofsAddr);
3079 int MoxaPortIsTxHold(int port)
3081 void __iomem *ofsAddr;
3084 ofsAddr = moxaTableAddr[port];
3085 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
3086 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
3087 moxafunc(ofsAddr, FC_GetCCSR, 0);
3088 val = readw(ofsAddr + FuncArg);
3092 if (readw(ofsAddr + FlagStat) & Tx_flowOff)