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 struct moxa_pci_devinfo {
108 struct pci_dev *pdev;
111 struct moxa_board_conf {
114 unsigned long baseAddr;
116 struct moxa_pci_devinfo pciInfo;
119 static struct moxa_board_conf moxa_boards[MAX_BOARDS];
120 static void __iomem *moxaBaseAddr[MAX_BOARDS];
121 static int loadstat[MAX_BOARDS];
127 unsigned short closing_wait;
130 long event; /* long req'd for set_bit --RR */
132 unsigned long statusflags;
133 struct tty_struct *tty;
135 wait_queue_head_t open_wait;
136 wait_queue_head_t close_wait;
139 struct mxser_mstatus {
147 static struct mxser_mstatus GMStatus[MAX_PORTS];
150 #define TXSTOPPED 0x1
152 #define EMPTYWAIT 0x4
155 #define SERIAL_DO_RESTART
157 #define WAKEUP_CHARS 256
159 static int verbose = 0;
160 static int ttymajor = MOXAMAJOR;
161 /* Variables for insmod */
163 static int baseaddr[4];
165 static int numports[4];
168 MODULE_AUTHOR("William Chen");
169 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
170 MODULE_LICENSE("GPL");
172 module_param_array(type, int, NULL, 0);
173 module_param_array(baseaddr, int, NULL, 0);
174 module_param_array(numports, int, NULL, 0);
176 module_param(ttymajor, int, 0);
177 module_param(verbose, bool, 0644);
182 static int moxa_open(struct tty_struct *, struct file *);
183 static void moxa_close(struct tty_struct *, struct file *);
184 static int moxa_write(struct tty_struct *, const unsigned char *, int);
185 static int moxa_write_room(struct tty_struct *);
186 static void moxa_flush_buffer(struct tty_struct *);
187 static int moxa_chars_in_buffer(struct tty_struct *);
188 static void moxa_flush_chars(struct tty_struct *);
189 static void moxa_put_char(struct tty_struct *, unsigned char);
190 static int moxa_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);
191 static void moxa_throttle(struct tty_struct *);
192 static void moxa_unthrottle(struct tty_struct *);
193 static void moxa_set_termios(struct tty_struct *, struct ktermios *);
194 static void moxa_stop(struct tty_struct *);
195 static void moxa_start(struct tty_struct *);
196 static void moxa_hangup(struct tty_struct *);
197 static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
198 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
199 unsigned int set, unsigned int clear);
200 static void moxa_poll(unsigned long);
201 static void set_tty_param(struct tty_struct *);
202 static int block_till_ready(struct tty_struct *, struct file *,
204 static void setup_empty_event(struct tty_struct *);
205 static void check_xmit_empty(unsigned long);
206 static void shut_down(struct moxa_str *);
207 static void receive_data(struct moxa_str *);
209 * moxa board interface functions:
211 static void MoxaDriverInit(void);
212 static int MoxaDriverIoctl(unsigned int, unsigned long, int);
213 static int MoxaDriverPoll(void);
214 static int MoxaPortsOfCard(int);
215 static int MoxaPortIsValid(int);
216 static void MoxaPortEnable(int);
217 static void MoxaPortDisable(int);
218 static long MoxaPortGetMaxBaud(int);
219 static long MoxaPortSetBaud(int, long);
220 static int MoxaPortSetTermio(int, struct ktermios *, speed_t);
221 static int MoxaPortGetLineOut(int, int *, int *);
222 static void MoxaPortLineCtrl(int, int, int);
223 static void MoxaPortFlowCtrl(int, int, int, int, int, int);
224 static int MoxaPortLineStatus(int);
225 static int MoxaPortDCDChange(int);
226 static int MoxaPortDCDON(int);
227 static void MoxaPortFlushData(int, int);
228 static int MoxaPortWriteData(int, unsigned char *, int);
229 static int MoxaPortReadData(int, struct tty_struct *tty);
230 static int MoxaPortTxQueue(int);
231 static int MoxaPortRxQueue(int);
232 static int MoxaPortTxFree(int);
233 static void MoxaPortTxDisable(int);
234 static void MoxaPortTxEnable(int);
235 static int MoxaPortResetBrkCnt(int);
236 static void MoxaPortSendBreak(int, int);
237 static int moxa_get_serial_info(struct moxa_str *, struct serial_struct __user *);
238 static int moxa_set_serial_info(struct moxa_str *, struct serial_struct __user *);
239 static void MoxaSetFifo(int port, int enable);
241 static const struct tty_operations moxa_ops = {
245 .write_room = moxa_write_room,
246 .flush_buffer = moxa_flush_buffer,
247 .chars_in_buffer = moxa_chars_in_buffer,
248 .flush_chars = moxa_flush_chars,
249 .put_char = moxa_put_char,
251 .throttle = moxa_throttle,
252 .unthrottle = moxa_unthrottle,
253 .set_termios = moxa_set_termios,
256 .hangup = moxa_hangup,
257 .tiocmget = moxa_tiocmget,
258 .tiocmset = moxa_tiocmset,
261 static struct tty_driver *moxaDriver;
262 static struct moxa_str moxaChannels[MAX_PORTS];
263 static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
264 static struct timer_list moxaEmptyTimer[MAX_PORTS];
265 static DEFINE_SPINLOCK(moxa_lock);
268 static int moxa_get_PCI_conf(struct pci_dev *p, int board_type,
269 struct moxa_board_conf *board)
271 board->baseAddr = pci_resource_start (p, 2);
272 board->boardType = board_type;
273 switch (board_type) {
274 case MOXA_BOARD_C218_ISA:
275 case MOXA_BOARD_C218_PCI:
279 case MOXA_BOARD_CP204J:
286 board->busType = MOXA_BUS_TYPE_PCI;
287 board->pciInfo.busNum = p->bus->number;
288 board->pciInfo.devNum = p->devfn >> 3;
289 board->pciInfo.pdev = p;
290 /* don't lose the reference in the next pci_get_device iteration */
295 #endif /* CONFIG_PCI */
297 static int __init moxa_init(void)
302 printk(KERN_INFO "MOXA Intellio family driver version %s\n", MOXA_VERSION);
303 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
307 moxaDriver->owner = THIS_MODULE;
308 moxaDriver->name = "ttyMX";
309 moxaDriver->major = ttymajor;
310 moxaDriver->minor_start = 0;
311 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
312 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
313 moxaDriver->init_termios = tty_std_termios;
314 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
315 moxaDriver->init_termios.c_ispeed = 9600;
316 moxaDriver->init_termios.c_ospeed = 9600;
317 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
318 tty_set_operations(moxaDriver, &moxa_ops);
320 for (i = 0, ch = moxaChannels; i < MAX_PORTS; i++, ch++) {
321 ch->type = PORT_16550A;
323 ch->close_delay = 5 * HZ / 10;
324 ch->closing_wait = 30 * HZ;
325 ch->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
326 init_waitqueue_head(&ch->open_wait);
327 init_waitqueue_head(&ch->close_wait);
330 printk("Tty devices major number = %d\n", ttymajor);
332 if (tty_register_driver(moxaDriver)) {
333 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
334 put_tty_driver(moxaDriver);
337 for (i = 0; i < MAX_PORTS; i++)
338 setup_timer(&moxaEmptyTimer[i], check_xmit_empty,
339 (unsigned long)&moxaChannels[i]);
341 mod_timer(&moxaTimer, jiffies + HZ / 50);
343 /* Find the boards defined in source code */
345 for (i = 0; i < MAX_BOARDS; i++) {
346 if ((moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA) ||
347 (moxa_isa_boards[i].boardType == MOXA_BOARD_C320_ISA)) {
348 moxa_boards[numBoards].boardType = moxa_isa_boards[i].boardType;
349 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
350 moxa_boards[numBoards].numPorts = 8;
352 moxa_boards[numBoards].numPorts = moxa_isa_boards[i].numPorts;
353 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
354 moxa_boards[numBoards].baseAddr = moxa_isa_boards[i].baseAddr;
356 printk("Board %2d: %s board(baseAddr=%lx)\n",
358 moxa_brdname[moxa_boards[numBoards].boardType - 1],
359 moxa_boards[numBoards].baseAddr);
363 /* Find the boards defined form module args. */
365 for (i = 0; i < MAX_BOARDS; i++) {
366 if ((type[i] == MOXA_BOARD_C218_ISA) ||
367 (type[i] == MOXA_BOARD_C320_ISA)) {
369 printk("Board %2d: %s board(baseAddr=%lx)\n",
371 moxa_brdname[type[i] - 1],
372 (unsigned long) baseaddr[i]);
373 if (numBoards >= MAX_BOARDS) {
375 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
378 moxa_boards[numBoards].boardType = type[i];
379 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
380 moxa_boards[numBoards].numPorts = 8;
382 moxa_boards[numBoards].numPorts = numports[i];
383 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
384 moxa_boards[numBoards].baseAddr = baseaddr[i];
389 /* Find PCI boards here */
392 struct pci_dev *p = NULL;
393 int n = ARRAY_SIZE(moxa_pcibrds) - 1;
396 while ((p = pci_get_device(moxa_pcibrds[i].vendor, moxa_pcibrds[i].device, p))!=NULL)
398 if (pci_enable_device(p))
400 if (numBoards >= MAX_BOARDS) {
402 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
404 moxa_get_PCI_conf(p, moxa_pcibrds[i].driver_data,
405 &moxa_boards[numBoards]);
413 for (i = 0; i < numBoards; i++) {
414 moxaBaseAddr[i] = ioremap((unsigned long) moxa_boards[i].baseAddr, 0x4000);
420 static void __exit moxa_exit(void)
425 printk("Unloading module moxa ...\n");
427 del_timer_sync(&moxaTimer);
429 for (i = 0; i < MAX_PORTS; i++)
430 del_timer_sync(&moxaEmptyTimer[i]);
432 if (tty_unregister_driver(moxaDriver))
433 printk("Couldn't unregister MOXA Intellio family serial driver\n");
434 put_tty_driver(moxaDriver);
436 for (i = 0; i < MAX_BOARDS; i++) {
438 iounmap(moxaBaseAddr[i]);
439 if (moxa_boards[i].busType == MOXA_BUS_TYPE_PCI)
440 pci_dev_put(moxa_boards[i].pciInfo.pdev);
447 module_init(moxa_init);
448 module_exit(moxa_exit);
450 static int moxa_open(struct tty_struct *tty, struct file *filp)
457 if (port == MAX_PORTS) {
460 if (!MoxaPortIsValid(port)) {
461 tty->driver_data = NULL;
465 ch = &moxaChannels[port];
467 tty->driver_data = ch;
469 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
472 MoxaPortLineCtrl(ch->port, 1, 1);
473 MoxaPortEnable(ch->port);
474 ch->asyncflags |= ASYNC_INITIALIZED;
476 retval = block_till_ready(tty, filp, ch);
478 moxa_unthrottle(tty);
480 if (ch->type == PORT_16550A) {
481 MoxaSetFifo(ch->port, 1);
483 MoxaSetFifo(ch->port, 0);
489 static void moxa_close(struct tty_struct *tty, struct file *filp)
495 if (port == MAX_PORTS) {
498 if (!MoxaPortIsValid(port)) {
499 #ifdef SERIAL_DEBUG_CLOSE
500 printk("Invalid portno in moxa_close\n");
502 tty->driver_data = NULL;
505 if (tty->driver_data == NULL) {
508 if (tty_hung_up_p(filp)) {
511 ch = (struct moxa_str *) tty->driver_data;
513 if ((tty->count == 1) && (ch->count != 1)) {
514 printk("moxa_close: bad serial port count; tty->count is 1, "
515 "ch->count is %d\n", ch->count);
518 if (--ch->count < 0) {
519 printk("moxa_close: bad serial port count, device=%s\n",
526 ch->asyncflags |= ASYNC_CLOSING;
528 ch->cflag = tty->termios->c_cflag;
529 if (ch->asyncflags & ASYNC_INITIALIZED) {
530 setup_empty_event(tty);
531 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
532 del_timer_sync(&moxaEmptyTimer[ch->port]);
535 MoxaPortFlushData(port, 2);
537 if (tty->driver->flush_buffer)
538 tty->driver->flush_buffer(tty);
539 tty_ldisc_flush(tty);
544 if (ch->blocked_open) {
545 if (ch->close_delay) {
546 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
548 wake_up_interruptible(&ch->open_wait);
550 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
551 wake_up_interruptible(&ch->close_wait);
554 static int moxa_write(struct tty_struct *tty,
555 const unsigned char *buf, int count)
561 ch = (struct moxa_str *) tty->driver_data;
566 spin_lock_irqsave(&moxa_lock, flags);
567 len = MoxaPortWriteData(port, (unsigned char *) buf, count);
568 spin_unlock_irqrestore(&moxa_lock, flags);
570 /*********************************************
571 if ( !(ch->statusflags & LOWWAIT) &&
572 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
573 ************************************************/
574 ch->statusflags |= LOWWAIT;
578 static int moxa_write_room(struct tty_struct *tty)
584 ch = (struct moxa_str *) tty->driver_data;
587 return (MoxaPortTxFree(ch->port));
590 static void moxa_flush_buffer(struct tty_struct *tty)
592 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
596 MoxaPortFlushData(ch->port, 1);
600 static int moxa_chars_in_buffer(struct tty_struct *tty)
603 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
606 * Sigh...I have to check if driver_data is NULL here, because
607 * if an open() fails, the TTY subsystem eventually calls
608 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
609 * routine. And since the open() failed, we return 0 here. TDJ
613 chars = MoxaPortTxQueue(ch->port);
616 * Make it possible to wakeup anything waiting for output
617 * in tty_ioctl.c, etc.
619 if (!(ch->statusflags & EMPTYWAIT))
620 setup_empty_event(tty);
625 static void moxa_flush_chars(struct tty_struct *tty)
628 * Don't think I need this, because this is called to empty the TX
629 * buffer for the 16450, 16550, etc.
633 static void moxa_put_char(struct tty_struct *tty, unsigned char c)
639 ch = (struct moxa_str *) tty->driver_data;
643 spin_lock_irqsave(&moxa_lock, flags);
644 MoxaPortWriteData(port, &c, 1);
645 spin_unlock_irqrestore(&moxa_lock, flags);
646 /************************************************
647 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
648 *************************************************/
649 ch->statusflags |= LOWWAIT;
652 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
654 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
656 int flag = 0, dtr, rts;
659 if ((port != MAX_PORTS) && (!ch))
662 MoxaPortGetLineOut(ch->port, &dtr, &rts);
667 dtr = MoxaPortLineStatus(ch->port);
677 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
678 unsigned int set, unsigned int clear)
680 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
685 if ((port != MAX_PORTS) && (!ch))
688 MoxaPortGetLineOut(ch->port, &dtr, &rts);
693 if (clear & TIOCM_RTS)
695 if (clear & TIOCM_DTR)
697 MoxaPortLineCtrl(ch->port, dtr, rts);
701 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
702 unsigned int cmd, unsigned long arg)
704 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
706 void __user *argp = (void __user *)arg;
710 if ((port != MAX_PORTS) && (!ch))
714 case TCSBRK: /* SVID version: non-zero arg --> no break */
715 retval = tty_check_change(tty);
718 setup_empty_event(tty);
719 tty_wait_until_sent(tty, 0);
721 MoxaPortSendBreak(ch->port, 0);
723 case TCSBRKP: /* support for POSIX tcsendbreak() */
724 retval = tty_check_change(tty);
727 setup_empty_event(tty);
728 tty_wait_until_sent(tty, 0);
729 MoxaPortSendBreak(ch->port, arg);
732 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) argp);
734 if(get_user(retval, (unsigned long __user *) argp))
737 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
740 ch->asyncflags &= ~ASYNC_CHECK_CD;
742 ch->asyncflags |= ASYNC_CHECK_CD;
745 return moxa_get_serial_info(ch, argp);
748 return moxa_set_serial_info(ch, argp);
750 retval = MoxaDriverIoctl(cmd, arg, port);
755 static void moxa_throttle(struct tty_struct *tty)
757 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
759 ch->statusflags |= THROTTLE;
762 static void moxa_unthrottle(struct tty_struct *tty)
764 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
766 ch->statusflags &= ~THROTTLE;
769 static void moxa_set_termios(struct tty_struct *tty,
770 struct ktermios *old_termios)
772 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
777 if (!(old_termios->c_cflag & CLOCAL) &&
778 (tty->termios->c_cflag & CLOCAL))
779 wake_up_interruptible(&ch->open_wait);
782 static void moxa_stop(struct tty_struct *tty)
784 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
788 MoxaPortTxDisable(ch->port);
789 ch->statusflags |= TXSTOPPED;
793 static void moxa_start(struct tty_struct *tty)
795 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
800 if (!(ch->statusflags & TXSTOPPED))
803 MoxaPortTxEnable(ch->port);
804 ch->statusflags &= ~TXSTOPPED;
807 static void moxa_hangup(struct tty_struct *tty)
809 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
811 moxa_flush_buffer(tty);
815 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
817 wake_up_interruptible(&ch->open_wait);
820 static void moxa_poll(unsigned long ignored)
824 struct tty_struct *tp;
827 del_timer(&moxaTimer);
829 if (MoxaDriverPoll() < 0) {
830 mod_timer(&moxaTimer, jiffies + HZ / 50);
833 for (card = 0; card < MAX_BOARDS; card++) {
834 if ((ports = MoxaPortsOfCard(card)) <= 0)
836 ch = &moxaChannels[card * MAX_PORTS_PER_BOARD];
837 for (i = 0; i < ports; i++, ch++) {
838 if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
840 if (!(ch->statusflags & THROTTLE) &&
841 (MoxaPortRxQueue(ch->port) > 0))
843 if ((tp = ch->tty) == 0)
845 if (ch->statusflags & LOWWAIT) {
846 if (MoxaPortTxQueue(ch->port) <= WAKEUP_CHARS) {
848 ch->statusflags &= ~LOWWAIT;
853 if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch->port) > 0)) {
854 tty_insert_flip_char(tp, 0, TTY_BREAK);
855 tty_schedule_flip(tp);
857 if (MoxaPortDCDChange(ch->port)) {
858 if (ch->asyncflags & ASYNC_CHECK_CD) {
859 if (MoxaPortDCDON(ch->port))
860 wake_up_interruptible(&ch->open_wait);
863 wake_up_interruptible(&ch->open_wait);
864 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
871 mod_timer(&moxaTimer, jiffies + HZ / 50);
874 /******************************************************************************/
876 static void set_tty_param(struct tty_struct *tty)
878 register struct ktermios *ts;
880 int rts, cts, txflow, rxflow, xany;
882 ch = (struct moxa_str *) tty->driver_data;
884 if (ts->c_cflag & CLOCAL)
885 ch->asyncflags &= ~ASYNC_CHECK_CD;
887 ch->asyncflags |= ASYNC_CHECK_CD;
888 rts = cts = txflow = rxflow = xany = 0;
889 if (ts->c_cflag & CRTSCTS)
891 if (ts->c_iflag & IXON)
893 if (ts->c_iflag & IXOFF)
895 if (ts->c_iflag & IXANY)
897 MoxaPortFlowCtrl(ch->port, rts, cts, txflow, rxflow, xany);
898 MoxaPortSetTermio(ch->port, ts, tty_get_baud_rate(tty));
901 static int block_till_ready(struct tty_struct *tty, struct file *filp,
904 DECLARE_WAITQUEUE(wait,current);
907 int do_clocal = C_CLOCAL(tty);
910 * If the device is in the middle of being closed, then block
911 * until it's done, and then try again.
913 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
914 if (ch->asyncflags & ASYNC_CLOSING)
915 interruptible_sleep_on(&ch->close_wait);
916 #ifdef SERIAL_DO_RESTART
917 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
920 return (-ERESTARTSYS);
926 * If non-blocking mode is set, then make the check up front
929 if (filp->f_flags & O_NONBLOCK) {
930 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
934 * Block waiting for the carrier detect and the line to become free
937 add_wait_queue(&ch->open_wait, &wait);
938 #ifdef SERIAL_DEBUG_OPEN
939 printk("block_til_ready before block: ttys%d, count = %d\n",
940 ch->line, ch->count);
942 spin_lock_irqsave(&moxa_lock, flags);
943 if (!tty_hung_up_p(filp))
946 spin_unlock_irqrestore(&moxa_lock, flags);
949 set_current_state(TASK_INTERRUPTIBLE);
950 if (tty_hung_up_p(filp) ||
951 !(ch->asyncflags & ASYNC_INITIALIZED)) {
952 #ifdef SERIAL_DO_RESTART
953 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
956 retval = -ERESTARTSYS;
962 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
963 MoxaPortDCDON(ch->port)))
966 if (signal_pending(current)) {
967 retval = -ERESTARTSYS;
972 set_current_state(TASK_RUNNING);
973 remove_wait_queue(&ch->open_wait, &wait);
975 spin_lock_irqsave(&moxa_lock, flags);
976 if (!tty_hung_up_p(filp))
979 spin_unlock_irqrestore(&moxa_lock, flags);
980 #ifdef SERIAL_DEBUG_OPEN
981 printk("block_til_ready after blocking: ttys%d, count = %d\n",
982 ch->line, ch->count);
986 /* FIXME: review to see if we need to use set_bit on these */
987 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
991 static void setup_empty_event(struct tty_struct *tty)
993 struct moxa_str *ch = tty->driver_data;
996 spin_lock_irqsave(&moxa_lock, flags);
997 ch->statusflags |= EMPTYWAIT;
998 mod_timer(&moxaEmptyTimer[ch->port], jiffies + HZ);
999 spin_unlock_irqrestore(&moxa_lock, flags);
1002 static void check_xmit_empty(unsigned long data)
1004 struct moxa_str *ch;
1006 ch = (struct moxa_str *) data;
1007 del_timer_sync(&moxaEmptyTimer[ch->port]);
1008 if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
1009 if (MoxaPortTxQueue(ch->port) == 0) {
1010 ch->statusflags &= ~EMPTYWAIT;
1011 tty_wakeup(ch->tty);
1014 mod_timer(&moxaEmptyTimer[ch->port], jiffies + HZ);
1016 ch->statusflags &= ~EMPTYWAIT;
1019 static void shut_down(struct moxa_str *ch)
1021 struct tty_struct *tp;
1023 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1028 MoxaPortDisable(ch->port);
1031 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1033 if (tp->termios->c_cflag & HUPCL)
1034 MoxaPortLineCtrl(ch->port, 0, 0);
1036 ch->asyncflags &= ~ASYNC_INITIALIZED;
1039 static void receive_data(struct moxa_str *ch)
1041 struct tty_struct *tp;
1042 struct ktermios *ts;
1043 unsigned long flags;
1049 /**************************************************
1050 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1051 *****************************************************/
1053 MoxaPortFlushData(ch->port, 0);
1056 spin_lock_irqsave(&moxa_lock, flags);
1057 MoxaPortReadData(ch->port, tp);
1058 spin_unlock_irqrestore(&moxa_lock, flags);
1059 tty_schedule_flip(tp);
1062 #define Magic_code 0x404
1065 * System Configuration
1068 * for C218 BIOS initialization
1070 #define C218_ConfBase 0x800
1071 #define C218_status (C218_ConfBase + 0) /* BIOS running status */
1072 #define C218_diag (C218_ConfBase + 2) /* diagnostic status */
1073 #define C218_key (C218_ConfBase + 4) /* WORD (0x218 for C218) */
1074 #define C218DLoad_len (C218_ConfBase + 6) /* WORD */
1075 #define C218check_sum (C218_ConfBase + 8) /* BYTE */
1076 #define C218chksum_ok (C218_ConfBase + 0x0a) /* BYTE (1:ok) */
1077 #define C218_TestRx (C218_ConfBase + 0x10) /* 8 bytes for 8 ports */
1078 #define C218_TestTx (C218_ConfBase + 0x18) /* 8 bytes for 8 ports */
1079 #define C218_RXerr (C218_ConfBase + 0x20) /* 8 bytes for 8 ports */
1080 #define C218_ErrFlag (C218_ConfBase + 0x28) /* 8 bytes for 8 ports */
1082 #define C218_LoadBuf 0x0F00
1083 #define C218_KeyCode 0x218
1084 #define CP204J_KeyCode 0x204
1087 * for C320 BIOS initialization
1089 #define C320_ConfBase 0x800
1090 #define C320_LoadBuf 0x0f00
1091 #define STS_init 0x05 /* for C320_status */
1093 #define C320_status C320_ConfBase + 0 /* BIOS running status */
1094 #define C320_diag C320_ConfBase + 2 /* diagnostic status */
1095 #define C320_key C320_ConfBase + 4 /* WORD (0320H for C320) */
1096 #define C320DLoad_len C320_ConfBase + 6 /* WORD */
1097 #define C320check_sum C320_ConfBase + 8 /* WORD */
1098 #define C320chksum_ok C320_ConfBase + 0x0a /* WORD (1:ok) */
1099 #define C320bapi_len C320_ConfBase + 0x0c /* WORD */
1100 #define C320UART_no C320_ConfBase + 0x0e /* WORD */
1102 #define C320_KeyCode 0x320
1104 #define FixPage_addr 0x0000 /* starting addr of static page */
1105 #define DynPage_addr 0x2000 /* starting addr of dynamic page */
1106 #define C218_start 0x3000 /* starting addr of C218 BIOS prg */
1107 #define Control_reg 0x1ff0 /* select page and reset control */
1108 #define HW_reset 0x80
1113 #define FC_CardReset 0x80
1114 #define FC_ChannelReset 1 /* C320 firmware not supported */
1115 #define FC_EnableCH 2
1116 #define FC_DisableCH 3
1117 #define FC_SetParam 4
1118 #define FC_SetMode 5
1119 #define FC_SetRate 6
1120 #define FC_LineControl 7
1121 #define FC_LineStatus 8
1122 #define FC_XmitControl 9
1123 #define FC_FlushQueue 10
1124 #define FC_SendBreak 11
1125 #define FC_StopBreak 12
1126 #define FC_LoopbackON 13
1127 #define FC_LoopbackOFF 14
1128 #define FC_ClrIrqTable 15
1129 #define FC_SendXon 16
1130 #define FC_SetTermIrq 17 /* C320 firmware not supported */
1131 #define FC_SetCntIrq 18 /* C320 firmware not supported */
1132 #define FC_SetBreakIrq 19
1133 #define FC_SetLineIrq 20
1134 #define FC_SetFlowCtl 21
1135 #define FC_GenIrq 22
1136 #define FC_InCD180 23
1137 #define FC_OutCD180 24
1138 #define FC_InUARTreg 23
1139 #define FC_OutUARTreg 24
1140 #define FC_SetXonXoff 25
1141 #define FC_OutCD180CCR 26
1142 #define FC_ExtIQueue 27
1143 #define FC_ExtOQueue 28
1144 #define FC_ClrLineIrq 29
1145 #define FC_HWFlowCtl 30
1146 #define FC_GetClockRate 35
1147 #define FC_SetBaud 36
1148 #define FC_SetDataMode 41
1149 #define FC_GetCCSR 43
1150 #define FC_GetDataError 45
1151 #define FC_RxControl 50
1152 #define FC_ImmSend 51
1153 #define FC_SetXonState 52
1154 #define FC_SetXoffState 53
1155 #define FC_SetRxFIFOTrig 54
1156 #define FC_SetTxFIFOCnt 55
1157 #define FC_UnixRate 56
1158 #define FC_UnixResetTimer 57
1160 #define RxFIFOTrig1 0
1161 #define RxFIFOTrig4 1
1162 #define RxFIFOTrig8 2
1163 #define RxFIFOTrig14 3
1168 #define DRAM_global 0
1169 #define INT_data (DRAM_global + 0)
1170 #define Config_base (DRAM_global + 0x108)
1172 #define IRQindex (INT_data + 0)
1173 #define IRQpending (INT_data + 4)
1174 #define IRQtable (INT_data + 8)
1179 #define IntrRx 0x01 /* receiver data O.K. */
1180 #define IntrTx 0x02 /* transmit buffer empty */
1181 #define IntrFunc 0x04 /* function complete */
1182 #define IntrBreak 0x08 /* received break */
1183 #define IntrLine 0x10 /* line status change
1185 #define IntrIntr 0x20 /* received INTR code */
1186 #define IntrQuit 0x40 /* received QUIT code */
1187 #define IntrEOF 0x80 /* received EOF code */
1189 #define IntrRxTrigger 0x100 /* rx data count reach tigger value */
1190 #define IntrTxTrigger 0x200 /* tx data count below trigger value */
1192 #define Magic_no (Config_base + 0)
1193 #define Card_model_no (Config_base + 2)
1194 #define Total_ports (Config_base + 4)
1195 #define Module_cnt (Config_base + 8)
1196 #define Module_no (Config_base + 10)
1197 #define Timer_10ms (Config_base + 14)
1198 #define Disable_IRQ (Config_base + 20)
1199 #define TMS320_PORT1 (Config_base + 22)
1200 #define TMS320_PORT2 (Config_base + 24)
1201 #define TMS320_CLOCK (Config_base + 26)
1204 * DATA BUFFER in DRAM
1206 #define Extern_table 0x400 /* Base address of the external table
1207 (24 words * 64) total 3K bytes
1208 (24 words * 128) total 6K bytes */
1209 #define Extern_size 0x60 /* 96 bytes */
1210 #define RXrptr 0x00 /* read pointer for RX buffer */
1211 #define RXwptr 0x02 /* write pointer for RX buffer */
1212 #define TXrptr 0x04 /* read pointer for TX buffer */
1213 #define TXwptr 0x06 /* write pointer for TX buffer */
1214 #define HostStat 0x08 /* IRQ flag and general flag */
1215 #define FlagStat 0x0A
1216 #define FlowControl 0x0C /* B7 B6 B5 B4 B3 B2 B1 B0 */
1217 /* x x x x | | | | */
1218 /* | | | + CTS flow */
1219 /* | | +--- RTS flow */
1220 /* | +------ TX Xon/Xoff */
1221 /* +--------- RX Xon/Xoff */
1222 #define Break_cnt 0x0E /* received break count */
1223 #define CD180TXirq 0x10 /* if non-0: enable TX irq */
1224 #define RX_mask 0x12
1225 #define TX_mask 0x14
1226 #define Ofs_rxb 0x16
1227 #define Ofs_txb 0x18
1228 #define Page_rxb 0x1A
1229 #define Page_txb 0x1C
1230 #define EndPage_rxb 0x1E
1231 #define EndPage_txb 0x20
1232 #define Data_error 0x22
1233 #define RxTrigger 0x28
1234 #define TxTrigger 0x2a
1236 #define rRXwptr 0x34
1237 #define Low_water 0x36
1239 #define FuncCode 0x40
1240 #define FuncArg 0x42
1241 #define FuncArg1 0x44
1243 #define C218rx_size 0x2000 /* 8K bytes */
1244 #define C218tx_size 0x8000 /* 32K bytes */
1246 #define C218rx_mask (C218rx_size - 1)
1247 #define C218tx_mask (C218tx_size - 1)
1249 #define C320p8rx_size 0x2000
1250 #define C320p8tx_size 0x8000
1251 #define C320p8rx_mask (C320p8rx_size - 1)
1252 #define C320p8tx_mask (C320p8tx_size - 1)
1254 #define C320p16rx_size 0x2000
1255 #define C320p16tx_size 0x4000
1256 #define C320p16rx_mask (C320p16rx_size - 1)
1257 #define C320p16tx_mask (C320p16tx_size - 1)
1259 #define C320p24rx_size 0x2000
1260 #define C320p24tx_size 0x2000
1261 #define C320p24rx_mask (C320p24rx_size - 1)
1262 #define C320p24tx_mask (C320p24tx_size - 1)
1264 #define C320p32rx_size 0x1000
1265 #define C320p32tx_size 0x1000
1266 #define C320p32rx_mask (C320p32rx_size - 1)
1267 #define C320p32tx_mask (C320p32tx_size - 1)
1269 #define Page_size 0x2000
1270 #define Page_mask (Page_size - 1)
1271 #define C218rx_spage 3
1272 #define C218tx_spage 4
1273 #define C218rx_pageno 1
1274 #define C218tx_pageno 4
1275 #define C218buf_pageno 5
1277 #define C320p8rx_spage 3
1278 #define C320p8tx_spage 4
1279 #define C320p8rx_pgno 1
1280 #define C320p8tx_pgno 4
1281 #define C320p8buf_pgno 5
1283 #define C320p16rx_spage 3
1284 #define C320p16tx_spage 4
1285 #define C320p16rx_pgno 1
1286 #define C320p16tx_pgno 2
1287 #define C320p16buf_pgno 3
1289 #define C320p24rx_spage 3
1290 #define C320p24tx_spage 4
1291 #define C320p24rx_pgno 1
1292 #define C320p24tx_pgno 1
1293 #define C320p24buf_pgno 2
1295 #define C320p32rx_spage 3
1296 #define C320p32tx_ofs C320p32rx_size
1297 #define C320p32tx_spage 3
1298 #define C320p32buf_pgno 1
1303 #define WakeupRx 0x01
1304 #define WakeupTx 0x02
1305 #define WakeupBreak 0x08
1306 #define WakeupLine 0x10
1307 #define WakeupIntr 0x20
1308 #define WakeupQuit 0x40
1309 #define WakeupEOF 0x80 /* used in VTIME control */
1310 #define WakeupRxTrigger 0x100
1311 #define WakeupTxTrigger 0x200
1315 #define Rx_over 0x01
1316 #define Xoff_state 0x02
1317 #define Tx_flowOff 0x04
1318 #define Tx_enable 0x08
1319 #define CTS_state 0x10
1320 #define DSR_state 0x20
1321 #define DCD_state 0x80
1325 #define CTS_FlowCtl 1
1326 #define RTS_FlowCtl 2
1327 #define Tx_FlowCtl 4
1328 #define Rx_FlowCtl 8
1329 #define IXM_IXANY 0x10
1331 #define LowWater 128
1339 /* mode definition */
1345 #define MX_STOP1 0x00
1346 #define MX_STOP15 0x04
1347 #define MX_STOP2 0x08
1349 #define MX_PARNONE 0x00
1350 #define MX_PAREVEN 0x40
1351 #define MX_PARODD 0xC0
1359 int rxcnt[MAX_PORTS];
1360 int txcnt[MAX_PORTS];
1363 #define DCD_changed 0x01
1364 #define DCD_oldstate 0x80
1366 static unsigned char moxaBuff[10240];
1367 static void __iomem *moxaIntNdx[MAX_BOARDS];
1368 static void __iomem *moxaIntPend[MAX_BOARDS];
1369 static void __iomem *moxaIntTable[MAX_BOARDS];
1370 static char moxaChkPort[MAX_PORTS];
1371 static char moxaLineCtrl[MAX_PORTS];
1372 static void __iomem *moxaTableAddr[MAX_PORTS];
1373 static long moxaCurBaud[MAX_PORTS];
1374 static char moxaDCDState[MAX_PORTS];
1375 static char moxaLowChkFlag[MAX_PORTS];
1376 static int moxaLowWaterChk;
1377 static int moxaCard;
1378 static struct mon_str moxaLog;
1379 static int moxaFuncTout = HZ / 2;
1380 static ushort moxaBreakCnt[MAX_PORTS];
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)
1403 moxaFuncTout = HZ / 2; /* 500 mini-seconds */
1406 moxaLowWaterChk = 0;
1407 for (i = 0; i < MAX_PORTS; i++) {
1409 moxaLowChkFlag[i] = 0;
1410 moxaLineCtrl[i] = 0;
1411 moxaLog.rxcnt[i] = 0;
1412 moxaLog.txcnt[i] = 0;
1417 #define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1418 #define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
1419 #define MOXA_INIT_DRIVER (MOXA + 6) /* moxaCard=0 */
1420 #define MOXA_LOAD_BIOS (MOXA + 9) /* download BIOS */
1421 #define MOXA_FIND_BOARD (MOXA + 10) /* Check if MOXA card exist? */
1422 #define MOXA_LOAD_C320B (MOXA + 11) /* download 320B firmware */
1423 #define MOXA_LOAD_CODE (MOXA + 12) /* download firmware */
1424 #define MOXA_GETDATACOUNT (MOXA + 23)
1425 #define MOXA_GET_IOQUEUE (MOXA + 27)
1426 #define MOXA_FLUSH_QUEUE (MOXA + 28)
1427 #define MOXA_GET_CONF (MOXA + 35) /* configuration */
1428 #define MOXA_GET_MAJOR (MOXA + 63)
1429 #define MOXA_GET_CUMAJOR (MOXA + 64)
1430 #define MOXA_GETMSTATUS (MOXA + 65)
1444 static struct moxaq_str temp_queue[MAX_PORTS];
1445 static struct dl_str dltmp;
1447 void MoxaPortFlushData(int port, int mode)
1449 void __iomem *ofsAddr;
1450 if ((mode < 0) || (mode > 2))
1452 ofsAddr = moxaTableAddr[port];
1453 moxafunc(ofsAddr, FC_FlushQueue, mode);
1455 moxaLowChkFlag[port] = 0;
1456 low_water_check(ofsAddr);
1460 int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port)
1464 int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
1465 void __user *argp = (void __user *)arg;
1467 if (port == MAX_PORTS) {
1468 if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_INIT_DRIVER) &&
1469 (cmd != MOXA_LOAD_BIOS) && (cmd != MOXA_FIND_BOARD) && (cmd != MOXA_LOAD_C320B) &&
1470 (cmd != MOXA_LOAD_CODE) && (cmd != MOXA_GETDATACOUNT) &&
1471 (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
1472 (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
1477 if(copy_to_user(argp, &moxa_boards, MAX_BOARDS *
1478 sizeof(struct moxa_board_conf)))
1481 case MOXA_INIT_DRIVER:
1482 if ((int) arg == 0x404)
1485 case MOXA_GETDATACOUNT:
1486 moxaLog.tick = jiffies;
1487 if(copy_to_user(argp, &moxaLog, sizeof(struct mon_str)))
1490 case MOXA_FLUSH_QUEUE:
1491 MoxaPortFlushData(port, arg);
1493 case MOXA_GET_IOQUEUE:
1494 for (i = 0; i < MAX_PORTS; i++) {
1495 if (moxaChkPort[i]) {
1496 temp_queue[i].inq = MoxaPortRxQueue(i);
1497 temp_queue[i].outq = MoxaPortTxQueue(i);
1500 if(copy_to_user(argp, temp_queue, sizeof(struct moxaq_str) * MAX_PORTS))
1503 case MOXA_GET_OQUEUE:
1504 i = MoxaPortTxQueue(port);
1505 return put_user(i, (unsigned long __user *)argp);
1506 case MOXA_GET_IQUEUE:
1507 i = MoxaPortRxQueue(port);
1508 return put_user(i, (unsigned long __user *)argp);
1509 case MOXA_GET_MAJOR:
1510 if(copy_to_user(argp, &ttymajor, sizeof(int)))
1513 case MOXA_GET_CUMAJOR:
1515 if(copy_to_user(argp, &i, sizeof(int)))
1518 case MOXA_GETMSTATUS:
1519 for (i = 0; i < MAX_PORTS; i++) {
1521 GMStatus[i].dcd = 0;
1522 GMStatus[i].dsr = 0;
1523 GMStatus[i].cts = 0;
1524 if (!moxaChkPort[i]) {
1527 status = MoxaPortLineStatus(moxaChannels[i].port);
1529 GMStatus[i].cts = 1;
1531 GMStatus[i].dsr = 1;
1533 GMStatus[i].dcd = 1;
1536 if (!moxaChannels[i].tty || !moxaChannels[i].tty->termios)
1537 GMStatus[i].cflag = moxaChannels[i].cflag;
1539 GMStatus[i].cflag = moxaChannels[i].tty->termios->c_cflag;
1541 if(copy_to_user(argp, GMStatus, sizeof(struct mxser_mstatus) * MAX_PORTS))
1545 return (-ENOIOCTLCMD);
1546 case MOXA_LOAD_BIOS:
1547 case MOXA_FIND_BOARD:
1548 case MOXA_LOAD_C320B:
1549 case MOXA_LOAD_CODE:
1550 if (!capable(CAP_SYS_RAWIO))
1555 if(copy_from_user(&dltmp, argp, sizeof(struct dl_str)))
1557 if(dltmp.cardno < 0 || dltmp.cardno >= MAX_BOARDS)
1562 case MOXA_LOAD_BIOS:
1563 i = moxaloadbios(dltmp.cardno, dltmp.buf, dltmp.len);
1565 case MOXA_FIND_BOARD:
1566 return moxafindcard(dltmp.cardno);
1567 case MOXA_LOAD_C320B:
1568 moxaload320b(dltmp.cardno, dltmp.buf, dltmp.len);
1569 default: /* to keep gcc happy */
1571 case MOXA_LOAD_CODE:
1572 i = moxaloadcode(dltmp.cardno, dltmp.buf, dltmp.len);
1580 int MoxaDriverPoll(void)
1582 register ushort temp;
1584 void __iomem *ofsAddr;
1590 for (card = 0; card < MAX_BOARDS; card++) {
1591 if (loadstat[card] == 0)
1593 if ((ports = moxa_boards[card].numPorts) == 0)
1595 if (readb(moxaIntPend[card]) == 0xff) {
1596 ip = moxaIntTable[card] + readb(moxaIntNdx[card]);
1597 p = card * MAX_PORTS_PER_BOARD;
1599 for (port = 0; port < ports; port += 2, p++) {
1600 if ((temp = readw(ip + port)) != 0) {
1601 writew(0, ip + port);
1602 ofsAddr = moxaTableAddr[p];
1604 writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
1605 if (temp & IntrBreak) {
1608 if (temp & IntrLine) {
1609 if (readb(ofsAddr + FlagStat) & DCD_state) {
1610 if ((moxaDCDState[p] & DCD_oldstate) == 0)
1611 moxaDCDState[p] = (DCD_oldstate |
1614 if (moxaDCDState[p] & DCD_oldstate)
1615 moxaDCDState[p] = DCD_changed;
1620 writeb(0, moxaIntPend[card]);
1622 if (moxaLowWaterChk) {
1623 p = card * MAX_PORTS_PER_BOARD;
1624 for (port = 0; port < ports; port++, p++) {
1625 if (moxaLowChkFlag[p]) {
1626 moxaLowChkFlag[p] = 0;
1627 ofsAddr = moxaTableAddr[p];
1628 low_water_check(ofsAddr);
1633 moxaLowWaterChk = 0;
1637 /*****************************************************************************
1638 * Card level function: *
1639 * 1. MoxaPortsOfCard(int cardno); *
1640 *****************************************************************************/
1641 int MoxaPortsOfCard(int cardno)
1644 if (moxa_boards[cardno].boardType == 0)
1646 return (moxa_boards[cardno].numPorts);
1649 /*****************************************************************************
1650 * Port level functions: *
1651 * 1. MoxaPortIsValid(int port); *
1652 * 2. MoxaPortEnable(int port); *
1653 * 3. MoxaPortDisable(int port); *
1654 * 4. MoxaPortGetMaxBaud(int port); *
1655 * 6. MoxaPortSetBaud(int port, long baud); *
1656 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1657 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1658 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1659 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1660 * 12. MoxaPortLineStatus(int port); *
1661 * 13. MoxaPortDCDChange(int port); *
1662 * 14. MoxaPortDCDON(int port); *
1663 * 15. MoxaPortFlushData(int port, int mode); *
1664 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1665 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
1666 * 20. MoxaPortTxQueue(int port); *
1667 * 21. MoxaPortTxFree(int port); *
1668 * 22. MoxaPortRxQueue(int port); *
1669 * 24. MoxaPortTxDisable(int port); *
1670 * 25. MoxaPortTxEnable(int port); *
1671 * 27. MoxaPortResetBrkCnt(int port); *
1672 * 30. MoxaPortSendBreak(int port, int ticks); *
1673 *****************************************************************************/
1675 * Moxa Port Number Description:
1677 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1678 * the port number using in MOXA driver functions will be 0 to 31 for
1679 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1680 * to 127 for fourth. For example, if you setup three MOXA boards,
1681 * first board is C218, second board is C320-16 and third board is
1682 * C320-32. The port number of first board (C218 - 8 ports) is from
1683 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1684 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1685 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1686 * 127 will be invalid.
1689 * Moxa Functions Description:
1691 * Function 1: Driver initialization routine, this routine must be
1692 * called when initialized driver.
1694 * void MoxaDriverInit();
1697 * Function 2: Moxa driver private IOCTL command processing.
1699 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1701 * unsigned int cmd : IOCTL command
1702 * unsigned long arg : IOCTL argument
1703 * int port : port number (0 - 127)
1710 * Function 3: Moxa driver polling process routine.
1712 * int MoxaDriverPoll(void);
1714 * return: 0 ; polling O.K.
1715 * -1 : no any Moxa card.
1718 * Function 4: Get the ports of this card.
1720 * int MoxaPortsOfCard(int cardno);
1722 * int cardno : card number (0 - 3)
1724 * return: 0 : this card is invalid
1728 * Function 5: Check this port is valid or invalid
1730 * int MoxaPortIsValid(int port);
1731 * int port : port number (0 - 127, ref port description)
1733 * return: 0 : this port is invalid
1734 * 1 : this port is valid
1737 * Function 6: Enable this port to start Tx/Rx data.
1739 * void MoxaPortEnable(int port);
1740 * int port : port number (0 - 127)
1743 * Function 7: Disable this port
1745 * void MoxaPortDisable(int port);
1746 * int port : port number (0 - 127)
1749 * Function 8: Get the maximun available baud rate of this port.
1751 * long MoxaPortGetMaxBaud(int port);
1752 * int port : port number (0 - 127)
1754 * return: 0 : this port is invalid
1755 * 38400/57600/115200 bps
1758 * Function 10: Setting baud rate of this port.
1760 * long MoxaPortSetBaud(int port, long baud);
1761 * int port : port number (0 - 127)
1762 * long baud : baud rate (50 - 115200)
1764 * return: 0 : this port is invalid or baud < 50
1765 * 50 - 115200 : the real baud rate set to the port, if
1766 * the argument baud is large than maximun
1767 * available baud rate, the real setting
1768 * baud rate will be the maximun baud rate.
1771 * Function 12: Configure the port.
1773 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1774 * int port : port number (0 - 127)
1775 * struct ktermios * termio : termio structure pointer
1776 * speed_t baud : baud rate
1778 * return: -1 : this port is invalid or termio == NULL
1782 * Function 13: Get the DTR/RTS state of this port.
1784 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1785 * int port : port number (0 - 127)
1786 * int * dtrState : pointer to INT to receive the current DTR
1787 * state. (if NULL, this function will not
1788 * write to this address)
1789 * int * rtsState : pointer to INT to receive the current RTS
1790 * state. (if NULL, this function will not
1791 * write to this address)
1793 * return: -1 : this port is invalid
1797 * Function 14: Setting the DTR/RTS output state of this port.
1799 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1800 * int port : port number (0 - 127)
1801 * int dtrState : DTR output state (0: off, 1: on)
1802 * int rtsState : RTS output state (0: off, 1: on)
1805 * Function 15: Setting the flow control of this port.
1807 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1808 * int txFlow,int xany);
1809 * int port : port number (0 - 127)
1810 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1811 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1812 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1813 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1814 * int xany : S/W XANY flow control (0: no, 1: yes)
1817 * Function 16: Get ths line status of this port
1819 * int MoxaPortLineStatus(int port);
1820 * int port : port number (0 - 127)
1822 * return: Bit 0 - CTS state (0: off, 1: on)
1823 * Bit 1 - DSR state (0: off, 1: on)
1824 * Bit 2 - DCD state (0: off, 1: on)
1827 * Function 17: Check the DCD state has changed since the last read
1830 * int MoxaPortDCDChange(int port);
1831 * int port : port number (0 - 127)
1833 * return: 0 : no changed
1834 * 1 : DCD has changed
1837 * Function 18: Check ths current DCD state is ON or not.
1839 * int MoxaPortDCDON(int port);
1840 * int port : port number (0 - 127)
1842 * return: 0 : DCD off
1846 * Function 19: Flush the Rx/Tx buffer data of this port.
1848 * void MoxaPortFlushData(int port, int mode);
1849 * int port : port number (0 - 127)
1851 * 0 : flush the Rx buffer
1852 * 1 : flush the Tx buffer
1853 * 2 : flush the Rx and Tx buffer
1856 * Function 20: Write data.
1858 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1859 * int port : port number (0 - 127)
1860 * unsigned char * buffer : pointer to write data buffer.
1861 * int length : write data length
1863 * return: 0 - length : real write data length
1866 * Function 21: Read data.
1868 * int MoxaPortReadData(int port, struct tty_struct *tty);
1869 * int port : port number (0 - 127)
1870 * struct tty_struct *tty : tty for data
1872 * return: 0 - length : real read data length
1875 * Function 24: Get the Tx buffer current queued data bytes
1877 * int MoxaPortTxQueue(int port);
1878 * int port : port number (0 - 127)
1880 * return: .. : Tx buffer current queued data bytes
1883 * Function 25: Get the Tx buffer current free space
1885 * int MoxaPortTxFree(int port);
1886 * int port : port number (0 - 127)
1888 * return: .. : Tx buffer current free space
1891 * Function 26: Get the Rx buffer current queued data bytes
1893 * int MoxaPortRxQueue(int port);
1894 * int port : port number (0 - 127)
1896 * return: .. : Rx buffer current queued data bytes
1899 * Function 28: Disable port data transmission.
1901 * void MoxaPortTxDisable(int port);
1902 * int port : port number (0 - 127)
1905 * Function 29: Enable port data transmission.
1907 * void MoxaPortTxEnable(int port);
1908 * int port : port number (0 - 127)
1911 * Function 31: Get the received BREAK signal count and reset it.
1913 * int MoxaPortResetBrkCnt(int port);
1914 * int port : port number (0 - 127)
1916 * return: 0 - .. : BREAK signal count
1919 * Function 34: Send out a BREAK signal.
1921 * void MoxaPortSendBreak(int port, int ms100);
1922 * int port : port number (0 - 127)
1923 * int ms100 : break signal time interval.
1924 * unit: 100 mini-second. if ms100 == 0, it will
1925 * send out a about 250 ms BREAK signal.
1928 int MoxaPortIsValid(int port)
1933 if (moxaChkPort[port] == 0)
1938 void MoxaPortEnable(int port)
1940 void __iomem *ofsAddr;
1941 int MoxaPortLineStatus(int);
1942 short lowwater = 512;
1944 ofsAddr = moxaTableAddr[port];
1945 writew(lowwater, ofsAddr + Low_water);
1946 moxaBreakCnt[port] = 0;
1947 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
1948 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
1949 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1951 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
1954 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1955 moxafunc(ofsAddr, FC_FlushQueue, 2);
1957 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1958 MoxaPortLineStatus(port);
1961 void MoxaPortDisable(int port)
1963 void __iomem *ofsAddr = moxaTableAddr[port];
1965 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1966 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1967 writew(0, ofsAddr + HostStat);
1968 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1971 long MoxaPortGetMaxBaud(int port)
1973 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
1974 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI))
1981 long MoxaPortSetBaud(int port, long baud)
1983 void __iomem *ofsAddr;
1987 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
1989 ofsAddr = moxaTableAddr[port];
1993 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
1994 else if (max == 57600L)
1995 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
1997 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
1999 moxafunc(ofsAddr, FC_SetBaud, val);
2001 moxaCurBaud[port] = baud;
2005 int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud)
2007 void __iomem *ofsAddr;
2011 if (moxaChkPort[port] == 0 || termio == 0)
2013 ofsAddr = moxaTableAddr[port];
2014 cflag = termio->c_cflag; /* termio->c_cflag */
2016 mode = termio->c_cflag & CSIZE;
2019 else if (mode == CS6)
2021 else if (mode == CS7)
2023 else if (mode == CS8)
2026 if (termio->c_cflag & CSTOPB) {
2034 if (termio->c_cflag & PARENB) {
2035 if (termio->c_cflag & PARODD)
2042 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
2044 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2045 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2046 if (baud >= 921600L)
2049 MoxaPortSetBaud(port, baud);
2051 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
2052 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
2053 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
2054 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
2055 wait_finish(ofsAddr);
2061 int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState)
2064 if (!MoxaPortIsValid(port))
2067 if (moxaLineCtrl[port] & DTR_ON)
2073 if (moxaLineCtrl[port] & RTS_ON)
2081 void MoxaPortLineCtrl(int port, int dtr, int rts)
2083 void __iomem *ofsAddr;
2086 ofsAddr = moxaTableAddr[port];
2092 moxaLineCtrl[port] = mode;
2093 moxafunc(ofsAddr, FC_LineControl, mode);
2096 void MoxaPortFlowCtrl(int port, int rts, int cts, int txflow, int rxflow, int txany)
2098 void __iomem *ofsAddr;
2101 ofsAddr = moxaTableAddr[port];
2104 mode |= RTS_FlowCtl;
2106 mode |= CTS_FlowCtl;
2113 moxafunc(ofsAddr, FC_SetFlowCtl, mode);
2116 int MoxaPortLineStatus(int port)
2118 void __iomem *ofsAddr;
2121 ofsAddr = moxaTableAddr[port];
2122 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2123 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2124 moxafunc(ofsAddr, FC_LineStatus, 0);
2125 val = readw(ofsAddr + FuncArg);
2127 val = readw(ofsAddr + FlagStat) >> 4;
2132 if ((moxaDCDState[port] & DCD_oldstate) == 0)
2133 moxaDCDState[port] = (DCD_oldstate | DCD_changed);
2135 if (moxaDCDState[port] & DCD_oldstate)
2136 moxaDCDState[port] = DCD_changed;
2142 int MoxaPortDCDChange(int port)
2146 if (moxaChkPort[port] == 0)
2148 n = moxaDCDState[port];
2149 moxaDCDState[port] &= ~DCD_changed;
2154 int MoxaPortDCDON(int port)
2158 if (moxaChkPort[port] == 0)
2160 if (moxaDCDState[port] & DCD_oldstate)
2167 int MoxaPortWriteData(int port, unsigned char * buffer, int len)
2172 ushort head, tx_mask, spage, epage;
2173 ushort pageno, pageofs, bufhead;
2174 void __iomem *baseAddr, *ofsAddr, *ofs;
2176 ofsAddr = moxaTableAddr[port];
2177 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2178 tx_mask = readw(ofsAddr + TX_mask);
2179 spage = readw(ofsAddr + Page_txb);
2180 epage = readw(ofsAddr + EndPage_txb);
2181 tail = readw(ofsAddr + TXwptr);
2182 head = readw(ofsAddr + TXrptr);
2183 c = (head > tail) ? (head - tail - 1)
2184 : (head - tail + tx_mask);
2187 moxaLog.txcnt[port] += c;
2189 if (spage == epage) {
2190 bufhead = readw(ofsAddr + Ofs_txb);
2191 writew(spage, baseAddr + Control_reg);
2194 len = head - tail - 1;
2196 len = tx_mask + 1 - tail;
2197 len = (c > len) ? len : c;
2198 ofs = baseAddr + DynPage_addr + bufhead + tail;
2199 for (i = 0; i < len; i++)
2200 writeb(*buffer++, ofs + i);
2201 tail = (tail + len) & tx_mask;
2204 writew(tail, ofsAddr + TXwptr);
2207 pageno = spage + (tail >> 13);
2208 pageofs = tail & Page_mask;
2210 cnt = Page_size - pageofs;
2214 writeb(pageno, baseAddr + Control_reg);
2215 ofs = baseAddr + DynPage_addr + pageofs;
2216 for (i = 0; i < cnt; i++)
2217 writeb(*buffer++, ofs + i);
2219 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2222 if (++pageno == epage)
2227 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2231 int MoxaPortReadData(int port, struct tty_struct *tty)
2233 register ushort head, pageofs;
2234 int i, count, cnt, len, total, remain;
2235 ushort tail, rx_mask, spage, epage;
2236 ushort pageno, bufhead;
2237 void __iomem *baseAddr, *ofsAddr, *ofs;
2239 ofsAddr = moxaTableAddr[port];
2240 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2241 head = readw(ofsAddr + RXrptr);
2242 tail = readw(ofsAddr + RXwptr);
2243 rx_mask = readw(ofsAddr + RX_mask);
2244 spage = readw(ofsAddr + Page_rxb);
2245 epage = readw(ofsAddr + EndPage_rxb);
2246 count = (tail >= head) ? (tail - head)
2247 : (tail - head + rx_mask + 1);
2252 remain = count - total;
2253 moxaLog.rxcnt[port] += total;
2255 if (spage == epage) {
2256 bufhead = readw(ofsAddr + Ofs_rxb);
2257 writew(spage, baseAddr + Control_reg);
2262 len = rx_mask + 1 - head;
2263 len = (count > len) ? len : count;
2264 ofs = baseAddr + DynPage_addr + bufhead + head;
2265 for (i = 0; i < len; i++)
2266 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2267 head = (head + len) & rx_mask;
2270 writew(head, ofsAddr + RXrptr);
2273 pageno = spage + (head >> 13);
2274 pageofs = head & Page_mask;
2276 cnt = Page_size - pageofs;
2280 writew(pageno, baseAddr + Control_reg);
2281 ofs = baseAddr + DynPage_addr + pageofs;
2282 for (i = 0; i < cnt; i++)
2283 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2285 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2288 if (++pageno == epage)
2293 if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2294 moxaLowWaterChk = 1;
2295 moxaLowChkFlag[port] = 1;
2301 int MoxaPortTxQueue(int port)
2303 void __iomem *ofsAddr;
2304 ushort rptr, wptr, mask;
2307 ofsAddr = moxaTableAddr[port];
2308 rptr = readw(ofsAddr + TXrptr);
2309 wptr = readw(ofsAddr + TXwptr);
2310 mask = readw(ofsAddr + TX_mask);
2311 len = (wptr - rptr) & mask;
2315 int MoxaPortTxFree(int port)
2317 void __iomem *ofsAddr;
2318 ushort rptr, wptr, mask;
2321 ofsAddr = moxaTableAddr[port];
2322 rptr = readw(ofsAddr + TXrptr);
2323 wptr = readw(ofsAddr + TXwptr);
2324 mask = readw(ofsAddr + TX_mask);
2325 len = mask - ((wptr - rptr) & mask);
2329 int MoxaPortRxQueue(int port)
2331 void __iomem *ofsAddr;
2332 ushort rptr, wptr, mask;
2335 ofsAddr = moxaTableAddr[port];
2336 rptr = readw(ofsAddr + RXrptr);
2337 wptr = readw(ofsAddr + RXwptr);
2338 mask = readw(ofsAddr + RX_mask);
2339 len = (wptr - rptr) & mask;
2344 void MoxaPortTxDisable(int port)
2346 void __iomem *ofsAddr;
2348 ofsAddr = moxaTableAddr[port];
2349 moxafunc(ofsAddr, FC_SetXoffState, Magic_code);
2352 void MoxaPortTxEnable(int port)
2354 void __iomem *ofsAddr;
2356 ofsAddr = moxaTableAddr[port];
2357 moxafunc(ofsAddr, FC_SetXonState, Magic_code);
2361 int MoxaPortResetBrkCnt(int port)
2364 cnt = moxaBreakCnt[port];
2365 moxaBreakCnt[port] = 0;
2370 void MoxaPortSendBreak(int port, int ms100)
2372 void __iomem *ofsAddr;
2374 ofsAddr = moxaTableAddr[port];
2376 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2377 moxadelay(ms100 * (HZ / 10));
2379 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2380 moxadelay(HZ / 4); /* 250 ms */
2382 moxafunc(ofsAddr, FC_StopBreak, Magic_code);
2385 static int moxa_get_serial_info(struct moxa_str *info,
2386 struct serial_struct __user *retinfo)
2388 struct serial_struct tmp;
2390 memset(&tmp, 0, sizeof(tmp));
2391 tmp.type = info->type;
2392 tmp.line = info->port;
2395 tmp.flags = info->asyncflags;
2396 tmp.baud_base = 921600;
2397 tmp.close_delay = info->close_delay;
2398 tmp.closing_wait = info->closing_wait;
2399 tmp.custom_divisor = 0;
2401 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2407 static int moxa_set_serial_info(struct moxa_str *info,
2408 struct serial_struct __user *new_info)
2410 struct serial_struct new_serial;
2412 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2415 if ((new_serial.irq != 0) ||
2416 (new_serial.port != 0) ||
2417 // (new_serial.type != info->type) ||
2418 (new_serial.custom_divisor != 0) ||
2419 (new_serial.baud_base != 921600))
2422 if (!capable(CAP_SYS_ADMIN)) {
2423 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2424 (info->asyncflags & ~ASYNC_USR_MASK)))
2427 info->close_delay = new_serial.close_delay * HZ / 100;
2428 info->closing_wait = new_serial.closing_wait * HZ / 100;
2431 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2432 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2434 if (new_serial.type == PORT_16550A) {
2435 MoxaSetFifo(info->port, 1);
2437 MoxaSetFifo(info->port, 0);
2440 info->type = new_serial.type;
2446 /*****************************************************************************
2447 * Static local functions: *
2448 *****************************************************************************/
2450 * moxadelay - delays a specified number ticks
2452 static void moxadelay(int tick)
2454 unsigned long st, et;
2458 while (time_before(jiffies, et));
2461 static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
2464 writew(arg, ofsAddr + FuncArg);
2465 writew(cmd, ofsAddr + FuncCode);
2466 wait_finish(ofsAddr);
2469 static void wait_finish(void __iomem *ofsAddr)
2474 while (readw(ofsAddr + FuncCode) != 0) {
2476 if ((j - i) > moxaFuncTout) {
2482 static void low_water_check(void __iomem *ofsAddr)
2485 ushort rptr, wptr, mask;
2487 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2488 rptr = readw(ofsAddr + RXrptr);
2489 wptr = readw(ofsAddr + RXwptr);
2490 mask = readw(ofsAddr + RX_mask);
2491 len = (wptr - rptr) & mask;
2492 if (len <= Low_water)
2493 moxafunc(ofsAddr, FC_SendXon, 0);
2497 static int moxaloadbios(int cardno, unsigned char __user *tmp, int len)
2499 void __iomem *baseAddr;
2502 if(copy_from_user(moxaBuff, tmp, len))
2504 baseAddr = moxaBaseAddr[cardno];
2505 writeb(HW_reset, baseAddr + Control_reg); /* reset */
2506 moxadelay(1); /* delay 10 ms */
2507 for (i = 0; i < 4096; i++)
2508 writeb(0, baseAddr + i); /* clear fix page */
2509 for (i = 0; i < len; i++)
2510 writeb(moxaBuff[i], baseAddr + i); /* download BIOS */
2511 writeb(0, baseAddr + Control_reg); /* restart */
2515 static int moxafindcard(int cardno)
2517 void __iomem *baseAddr;
2520 baseAddr = moxaBaseAddr[cardno];
2521 switch (moxa_boards[cardno].boardType) {
2522 case MOXA_BOARD_C218_ISA:
2523 case MOXA_BOARD_C218_PCI:
2524 if ((tmp = readw(baseAddr + C218_key)) != C218_KeyCode) {
2528 case MOXA_BOARD_CP204J:
2529 if ((tmp = readw(baseAddr + C218_key)) != CP204J_KeyCode) {
2534 if ((tmp = readw(baseAddr + C320_key)) != C320_KeyCode) {
2537 if ((tmp = readw(baseAddr + C320_status)) != STS_init) {
2544 static int moxaload320b(int cardno, unsigned char __user *tmp, int len)
2546 void __iomem *baseAddr;
2549 if(len > sizeof(moxaBuff))
2551 if(copy_from_user(moxaBuff, tmp, len))
2553 baseAddr = moxaBaseAddr[cardno];
2554 writew(len - 7168 - 2, baseAddr + C320bapi_len);
2555 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
2556 for (i = 0; i < 7168; i++)
2557 writeb(moxaBuff[i], baseAddr + DynPage_addr + i);
2558 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
2559 for (i = 0; i < (len - 7168); i++)
2560 writeb(moxaBuff[i + 7168], baseAddr + DynPage_addr + i);
2564 static int moxaloadcode(int cardno, unsigned char __user *tmp, int len)
2566 void __iomem *baseAddr, *ofsAddr;
2567 int retval, port, i;
2569 if(copy_from_user(moxaBuff, tmp, len))
2571 baseAddr = moxaBaseAddr[cardno];
2572 switch (moxa_boards[cardno].boardType) {
2573 case MOXA_BOARD_C218_ISA:
2574 case MOXA_BOARD_C218_PCI:
2575 case MOXA_BOARD_CP204J:
2576 retval = moxaloadc218(cardno, baseAddr, len);
2579 port = cardno * MAX_PORTS_PER_BOARD;
2580 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2581 moxaChkPort[port] = 1;
2582 moxaCurBaud[port] = 9600L;
2583 moxaDCDState[port] = 0;
2584 moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
2585 ofsAddr = moxaTableAddr[port];
2586 writew(C218rx_mask, ofsAddr + RX_mask);
2587 writew(C218tx_mask, ofsAddr + TX_mask);
2588 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
2589 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
2591 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
2592 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
2597 retval = moxaloadc320(cardno, baseAddr, len,
2598 &moxa_boards[cardno].numPorts);
2601 port = cardno * MAX_PORTS_PER_BOARD;
2602 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2603 moxaChkPort[port] = 1;
2604 moxaCurBaud[port] = 9600L;
2605 moxaDCDState[port] = 0;
2606 moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
2607 ofsAddr = moxaTableAddr[port];
2608 if (moxa_boards[cardno].numPorts == 8) {
2609 writew(C320p8rx_mask, ofsAddr + RX_mask);
2610 writew(C320p8tx_mask, ofsAddr + TX_mask);
2611 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
2612 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
2613 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
2614 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
2616 } else if (moxa_boards[cardno].numPorts == 16) {
2617 writew(C320p16rx_mask, ofsAddr + RX_mask);
2618 writew(C320p16tx_mask, ofsAddr + TX_mask);
2619 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
2620 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
2621 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
2622 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
2624 } else if (moxa_boards[cardno].numPorts == 24) {
2625 writew(C320p24rx_mask, ofsAddr + RX_mask);
2626 writew(C320p24tx_mask, ofsAddr + TX_mask);
2627 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
2628 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
2629 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
2630 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2631 } else if (moxa_boards[cardno].numPorts == 32) {
2632 writew(C320p32rx_mask, ofsAddr + RX_mask);
2633 writew(C320p32tx_mask, ofsAddr + TX_mask);
2634 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
2635 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
2636 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
2637 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
2638 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2643 loadstat[cardno] = 1;
2647 static int moxaloadc218(int cardno, void __iomem *baseAddr, int len)
2650 int i, j, len1, len2;
2651 ushort usum, *ptr, keycode;
2653 if (moxa_boards[cardno].boardType == MOXA_BOARD_CP204J)
2654 keycode = CP204J_KeyCode;
2656 keycode = C218_KeyCode;
2659 ptr = (ushort *) moxaBuff;
2660 for (i = 0; i < len1; i++)
2661 usum += le16_to_cpu(*(ptr + i));
2667 len2 = (len1 > 2048) ? 2048 : len1;
2669 for (i = 0; i < len2 << 1; i++)
2670 writeb(moxaBuff[i + j], baseAddr + C218_LoadBuf + i);
2673 writew(len2, baseAddr + C218DLoad_len);
2674 writew(0, baseAddr + C218_key);
2675 for (i = 0; i < 100; i++) {
2676 if (readw(baseAddr + C218_key) == keycode)
2678 moxadelay(1); /* delay 10 ms */
2680 if (readw(baseAddr + C218_key) != keycode) {
2684 writew(0, baseAddr + C218DLoad_len);
2685 writew(usum, baseAddr + C218check_sum);
2686 writew(0, baseAddr + C218_key);
2687 for (i = 0; i < 100; i++) {
2688 if (readw(baseAddr + C218_key) == keycode)
2690 moxadelay(1); /* delay 10 ms */
2693 } while ((readb(baseAddr + C218chksum_ok) != 1) && (retry < 3));
2694 if (readb(baseAddr + C218chksum_ok) != 1) {
2697 writew(0, baseAddr + C218_key);
2698 for (i = 0; i < 100; i++) {
2699 if (readw(baseAddr + Magic_no) == Magic_code)
2701 moxadelay(1); /* delay 10 ms */
2703 if (readw(baseAddr + Magic_no) != Magic_code) {
2706 writew(1, baseAddr + Disable_IRQ);
2707 writew(0, baseAddr + Magic_no);
2708 for (i = 0; i < 100; i++) {
2709 if (readw(baseAddr + Magic_no) == Magic_code)
2711 moxadelay(1); /* delay 10 ms */
2713 if (readw(baseAddr + Magic_no) != Magic_code) {
2717 moxaIntNdx[cardno] = baseAddr + IRQindex;
2718 moxaIntPend[cardno] = baseAddr + IRQpending;
2719 moxaIntTable[cardno] = baseAddr + IRQtable;
2723 static int moxaloadc320(int cardno, void __iomem *baseAddr, int len, int *numPorts)
2726 int i, j, wlen, len2, retry;
2731 uptr = (ushort *) moxaBuff;
2732 for (i = 0; i < wlen; i++)
2733 usum += le16_to_cpu(uptr[i]);
2744 for (i = 0; i < len2; i++)
2745 writeb(moxaBuff[j + i], baseAddr + C320_LoadBuf + i);
2748 writew(len2, baseAddr + C320DLoad_len);
2749 writew(0, baseAddr + C320_key);
2750 for (i = 0; i < 10; i++) {
2751 if (readw(baseAddr + C320_key) == C320_KeyCode)
2755 if (readw(baseAddr + C320_key) != C320_KeyCode)
2758 writew(0, baseAddr + C320DLoad_len);
2759 writew(usum, baseAddr + C320check_sum);
2760 writew(0, baseAddr + C320_key);
2761 for (i = 0; i < 10; i++) {
2762 if (readw(baseAddr + C320_key) == C320_KeyCode)
2767 } while ((readb(baseAddr + C320chksum_ok) != 1) && (retry < 3));
2768 if (readb(baseAddr + C320chksum_ok) != 1)
2770 writew(0, baseAddr + C320_key);
2771 for (i = 0; i < 600; i++) {
2772 if (readw(baseAddr + Magic_no) == Magic_code)
2776 if (readw(baseAddr + Magic_no) != Magic_code)
2779 if (moxa_boards[cardno].busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
2780 writew(0x3800, baseAddr + TMS320_PORT1);
2781 writew(0x3900, baseAddr + TMS320_PORT2);
2782 writew(28499, baseAddr + TMS320_CLOCK);
2784 writew(0x3200, baseAddr + TMS320_PORT1);
2785 writew(0x3400, baseAddr + TMS320_PORT2);
2786 writew(19999, baseAddr + TMS320_CLOCK);
2788 writew(1, baseAddr + Disable_IRQ);
2789 writew(0, baseAddr + Magic_no);
2790 for (i = 0; i < 500; i++) {
2791 if (readw(baseAddr + Magic_no) == Magic_code)
2795 if (readw(baseAddr + Magic_no) != Magic_code)
2798 j = readw(baseAddr + Module_cnt);
2802 writew(j, baseAddr + Module_no);
2803 writew(0, baseAddr + Magic_no);
2804 for (i = 0; i < 600; i++) {
2805 if (readw(baseAddr + Magic_no) == Magic_code)
2809 if (readw(baseAddr + Magic_no) != Magic_code)
2812 moxaIntNdx[cardno] = baseAddr + IRQindex;
2813 moxaIntPend[cardno] = baseAddr + IRQpending;
2814 moxaIntTable[cardno] = baseAddr + IRQtable;
2818 static void MoxaSetFifo(int port, int enable)
2820 void __iomem *ofsAddr = moxaTableAddr[port];
2823 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2824 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2826 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2827 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);