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/config.h>
33 #include <linux/module.h>
34 #include <linux/types.h>
36 #include <linux/ioport.h>
37 #include <linux/errno.h>
38 #include <linux/signal.h>
39 #include <linux/sched.h>
40 #include <linux/timer.h>
41 #include <linux/interrupt.h>
42 #include <linux/tty.h>
43 #include <linux/tty_flip.h>
44 #include <linux/major.h>
45 #include <linux/string.h>
46 #include <linux/fcntl.h>
47 #include <linux/ptrace.h>
48 #include <linux/serial.h>
49 #include <linux/tty_driver.h>
50 #include <linux/delay.h>
51 #include <linux/pci.h>
52 #include <linux/init.h>
54 #include <asm/system.h>
56 #include <asm/bitops.h>
57 #include <asm/uaccess.h>
59 #define MOXA_VERSION "5.1k"
62 #define MOXACUMAJOR 173
64 #define put_to_user(arg1, arg2) put_user(arg1, (unsigned long *)arg2)
65 #define get_from_user(arg1, arg2) get_user(arg1, (unsigned int *)arg2)
67 #define MAX_BOARDS 4 /* Don't change this value */
68 #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
69 #define MAX_PORTS 128 /* Don't change this value */
72 * Define the Moxa PCI vendor and device IDs.
74 #define MOXA_BUS_TYPE_ISA 0
75 #define MOXA_BUS_TYPE_PCI 1
77 #ifndef PCI_VENDOR_ID_MOXA
78 #define PCI_VENDOR_ID_MOXA 0x1393
80 #ifndef PCI_DEVICE_ID_CP204J
81 #define PCI_DEVICE_ID_CP204J 0x2040
83 #ifndef PCI_DEVICE_ID_C218
84 #define PCI_DEVICE_ID_C218 0x2180
86 #ifndef PCI_DEVICE_ID_C320
87 #define PCI_DEVICE_ID_C320 0x3200
91 MOXA_BOARD_C218_PCI = 1,
98 static char *moxa_brdname[] =
100 "C218 Turbo PCI series",
101 "C218 Turbo ISA series",
102 "C320 Turbo PCI series",
103 "C320 Turbo ISA series",
108 static struct pci_device_id moxa_pcibrds[] = {
109 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C218, PCI_ANY_ID, PCI_ANY_ID,
110 0, 0, MOXA_BOARD_C218_PCI },
111 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C320, PCI_ANY_ID, PCI_ANY_ID,
112 0, 0, MOXA_BOARD_C320_PCI },
113 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP204J, PCI_ANY_ID, PCI_ANY_ID,
114 0, 0, MOXA_BOARD_CP204J },
117 MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
118 #endif /* CONFIG_PCI */
120 typedef struct _moxa_isa_board_conf {
123 unsigned long baseAddr;
124 } moxa_isa_board_conf;
126 static moxa_isa_board_conf moxa_isa_boards[] =
128 /* {MOXA_BOARD_C218_ISA,8,0xDC000}, */
131 typedef struct _moxa_pci_devinfo {
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 unsigned long moxaBaseAddr[MAX_BOARDS];
151 unsigned short closing_wait;
154 long event; /* long req'd for set_bit --RR */
156 unsigned long statusflags;
157 struct tty_struct *tty;
159 wait_queue_head_t open_wait;
160 wait_queue_head_t close_wait;
161 struct work_struct tqueue;
164 struct mxser_mstatus {
172 static struct mxser_mstatus GMStatus[MAX_PORTS];
175 #define TXSTOPPED 0x1
177 #define EMPTYWAIT 0x4
181 #define MOXA_EVENT_HANGUP 1
183 #define SERIAL_DO_RESTART
186 #define SERIAL_TYPE_NORMAL 1
188 #define WAKEUP_CHARS 256
190 #define PORTNO(x) ((x)->index)
192 static int verbose = 0;
193 static int ttymajor = MOXAMAJOR;
194 /* Variables for insmod */
196 static int baseaddr[] = {0, 0, 0, 0};
197 static int type[] = {0, 0, 0, 0};
198 static int numports[] = {0, 0, 0, 0};
201 MODULE_AUTHOR("William Chen");
202 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
203 MODULE_LICENSE("GPL");
204 MODULE_PARM(type, "1-4i");
205 MODULE_PARM(baseaddr, "1-4i");
206 MODULE_PARM(numports, "1-4i");
207 MODULE_PARM(ttymajor, "i");
208 MODULE_PARM(verbose, "i");
210 static struct tty_driver *moxaDriver;
211 static struct moxa_str moxaChannels[MAX_PORTS];
212 static unsigned char *moxaXmitBuff;
213 static int moxaTimer_on;
214 static struct timer_list moxaTimer;
215 static int moxaEmptyTimer_on[MAX_PORTS];
216 static struct timer_list moxaEmptyTimer[MAX_PORTS];
217 static struct semaphore moxaBuffSem;
222 static void do_moxa_softint(void *);
223 static int moxa_open(struct tty_struct *, struct file *);
224 static void moxa_close(struct tty_struct *, struct file *);
225 static int moxa_write(struct tty_struct *, int, const unsigned char *, int);
226 static int moxa_write_room(struct tty_struct *);
227 static void moxa_flush_buffer(struct tty_struct *);
228 static int moxa_chars_in_buffer(struct tty_struct *);
229 static void moxa_flush_chars(struct tty_struct *);
230 static void moxa_put_char(struct tty_struct *, unsigned char);
231 static int moxa_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);
232 static void moxa_throttle(struct tty_struct *);
233 static void moxa_unthrottle(struct tty_struct *);
234 static void moxa_set_termios(struct tty_struct *, struct termios *);
235 static void moxa_stop(struct tty_struct *);
236 static void moxa_start(struct tty_struct *);
237 static void moxa_hangup(struct tty_struct *);
238 static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
239 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
240 unsigned int set, unsigned int clear);
241 static void moxa_poll(unsigned long);
242 static void set_tty_param(struct tty_struct *);
243 static int block_till_ready(struct tty_struct *, struct file *,
245 static void setup_empty_event(struct tty_struct *);
246 static void check_xmit_empty(unsigned long);
247 static void shut_down(struct moxa_str *);
248 static void receive_data(struct moxa_str *);
250 * moxa board interface functions:
252 static void MoxaDriverInit(void);
253 static int MoxaDriverIoctl(unsigned int, unsigned long, int);
254 static int MoxaDriverPoll(void);
255 static int MoxaPortsOfCard(int);
256 static int MoxaPortIsValid(int);
257 static void MoxaPortEnable(int);
258 static void MoxaPortDisable(int);
259 static long MoxaPortGetMaxBaud(int);
260 static long MoxaPortSetBaud(int, long);
261 static int MoxaPortSetTermio(int, struct termios *);
262 static int MoxaPortGetLineOut(int, int *, int *);
263 static void MoxaPortLineCtrl(int, int, int);
264 static void MoxaPortFlowCtrl(int, int, int, int, int, int);
265 static int MoxaPortLineStatus(int);
266 static int MoxaPortDCDChange(int);
267 static int MoxaPortDCDON(int);
268 static void MoxaPortFlushData(int, int);
269 static int MoxaPortWriteData(int, unsigned char *, int);
270 static int MoxaPortReadData(int, unsigned char *, int);
271 static int MoxaPortTxQueue(int);
272 static int MoxaPortRxQueue(int);
273 static int MoxaPortTxFree(int);
274 static void MoxaPortTxDisable(int);
275 static void MoxaPortTxEnable(int);
276 static int MoxaPortResetBrkCnt(int);
277 static void MoxaPortSendBreak(int, int);
278 static int moxa_get_serial_info(struct moxa_str *, struct serial_struct __user *);
279 static int moxa_set_serial_info(struct moxa_str *, struct serial_struct __user *);
280 static void MoxaSetFifo(int port, int enable);
282 static struct tty_operations moxa_ops = {
286 .write_room = moxa_write_room,
287 .flush_buffer = moxa_flush_buffer,
288 .chars_in_buffer = moxa_chars_in_buffer,
289 .flush_chars = moxa_flush_chars,
290 .put_char = moxa_put_char,
292 .throttle = moxa_throttle,
293 .unthrottle = moxa_unthrottle,
294 .set_termios = moxa_set_termios,
297 .hangup = moxa_hangup,
298 .tiocmget = moxa_tiocmget,
299 .tiocmset = moxa_tiocmset,
303 static int moxa_get_PCI_conf(struct pci_dev *p, int board_type, moxa_board_conf * board)
305 board->baseAddr = pci_resource_start (p, 2);
306 board->boardType = board_type;
307 switch (board_type) {
308 case MOXA_BOARD_C218_ISA:
309 case MOXA_BOARD_C218_PCI:
313 case MOXA_BOARD_CP204J:
320 board->busType = MOXA_BUS_TYPE_PCI;
321 board->pciInfo.busNum = p->bus->number;
322 board->pciInfo.devNum = p->devfn >> 3;
326 #endif /* CONFIG_PCI */
328 static int __init moxa_init(void)
333 printk(KERN_INFO "MOXA Intellio family driver version %s\n", MOXA_VERSION);
334 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
338 init_MUTEX(&moxaBuffSem);
339 moxaDriver->owner = THIS_MODULE;
340 moxaDriver->name = "ttya";
341 moxaDriver->devfs_name = "tts/a";
342 moxaDriver->major = ttymajor;
343 moxaDriver->minor_start = 0;
344 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
345 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
346 moxaDriver->init_termios = tty_std_termios;
347 moxaDriver->init_termios.c_iflag = 0;
348 moxaDriver->init_termios.c_oflag = 0;
349 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
350 moxaDriver->init_termios.c_lflag = 0;
351 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
352 tty_set_operations(moxaDriver, &moxa_ops);
356 for (i = 0, ch = moxaChannels; i < MAX_PORTS; i++, ch++) {
357 ch->type = PORT_16550A;
359 INIT_WORK(&ch->tqueue, do_moxa_softint, ch);
361 ch->close_delay = 5 * HZ / 10;
362 ch->closing_wait = 30 * HZ;
364 ch->blocked_open = 0;
365 ch->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
366 init_waitqueue_head(&ch->open_wait);
367 init_waitqueue_head(&ch->close_wait);
370 for (i = 0; i < MAX_BOARDS; i++) {
371 moxa_boards[i].boardType = 0;
372 moxa_boards[i].numPorts = 0;
373 moxa_boards[i].baseAddr = 0;
374 moxa_boards[i].busType = 0;
375 moxa_boards[i].pciInfo.busNum = 0;
376 moxa_boards[i].pciInfo.devNum = 0;
379 printk("Tty devices major number = %d\n", ttymajor);
381 if (tty_register_driver(moxaDriver)) {
382 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
383 put_tty_driver(moxaDriver);
386 for (i = 0; i < MAX_PORTS; i++) {
387 init_timer(&moxaEmptyTimer[i]);
388 moxaEmptyTimer[i].function = check_xmit_empty;
389 moxaEmptyTimer[i].data = (unsigned long) & moxaChannels[i];
390 moxaEmptyTimer_on[i] = 0;
393 init_timer(&moxaTimer);
394 moxaTimer.function = moxa_poll;
395 moxaTimer.expires = jiffies + (HZ / 50);
397 add_timer(&moxaTimer);
399 /* Find the boards defined in source code */
401 for (i = 0; i < MAX_BOARDS; i++) {
402 if ((moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA) ||
403 (moxa_isa_boards[i].boardType == MOXA_BOARD_C320_ISA)) {
404 moxa_boards[numBoards].boardType = moxa_isa_boards[i].boardType;
405 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
406 moxa_boards[numBoards].numPorts = 8;
408 moxa_boards[numBoards].numPorts = moxa_isa_boards[i].numPorts;
409 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
410 moxa_boards[numBoards].baseAddr = moxa_isa_boards[i].baseAddr;
412 printk("Board %2d: %s board(baseAddr=%lx)\n",
414 moxa_brdname[moxa_boards[numBoards].boardType - 1],
415 moxa_boards[numBoards].baseAddr);
419 /* Find the boards defined form module args. */
421 for (i = 0; i < MAX_BOARDS; i++) {
422 if ((type[i] == MOXA_BOARD_C218_ISA) ||
423 (type[i] == MOXA_BOARD_C320_ISA)) {
425 printk("Board %2d: %s board(baseAddr=%lx)\n",
427 moxa_brdname[type[i] - 1],
428 (unsigned long) baseaddr[i]);
429 if (numBoards >= MAX_BOARDS) {
431 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
434 moxa_boards[numBoards].boardType = type[i];
435 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
436 moxa_boards[numBoards].numPorts = 8;
438 moxa_boards[numBoards].numPorts = numports[i];
439 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
440 moxa_boards[numBoards].baseAddr = baseaddr[i];
445 /* Find PCI boards here */
448 struct pci_dev *p = NULL;
449 int n = (sizeof(moxa_pcibrds) / sizeof(moxa_pcibrds[0])) - 1;
452 while ((p = pci_find_device(moxa_pcibrds[i].vendor, moxa_pcibrds[i].device, p))!=NULL)
454 if (pci_enable_device(p))
456 if (numBoards >= MAX_BOARDS) {
458 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
460 moxa_get_PCI_conf(p, moxa_pcibrds[i].driver_data,
461 &moxa_boards[numBoards]);
469 for (i = 0; i < numBoards; i++) {
470 moxaBaseAddr[i] = (unsigned long) ioremap((unsigned long) moxa_boards[i].baseAddr, 0x4000);
476 static void __exit moxa_exit(void)
481 printk("Unloading module moxa ...\n");
484 del_timer(&moxaTimer);
486 for (i = 0; i < MAX_PORTS; i++)
487 if (moxaEmptyTimer_on[i])
488 del_timer(&moxaEmptyTimer[i]);
490 if (tty_unregister_driver(moxaDriver))
491 printk("Couldn't unregister MOXA Intellio family serial driver\n");
492 put_tty_driver(moxaDriver);
498 module_init(moxa_init);
499 module_exit(moxa_exit);
501 static void do_moxa_softint(void *private_)
503 struct moxa_str *ch = (struct moxa_str *) private_;
504 struct tty_struct *tty;
506 if (ch && (tty = ch->tty)) {
507 if (test_and_clear_bit(MOXA_EVENT_HANGUP, &ch->event)) {
508 tty_hangup(tty); /* FIXME: module removal race here - AKPM */
509 wake_up_interruptible(&ch->open_wait);
510 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
515 static int moxa_open(struct tty_struct *tty, struct file *filp)
523 if (port == MAX_PORTS) {
526 if (!MoxaPortIsValid(port)) {
527 tty->driver_data = NULL;
532 page = get_zeroed_page(GFP_KERNEL);
537 /* This test is guarded by the BuffSem so no longer needed
542 moxaXmitBuff = (unsigned char *) page;
546 ch = &moxaChannels[port];
548 tty->driver_data = ch;
550 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
553 MoxaPortLineCtrl(ch->port, 1, 1);
554 MoxaPortEnable(ch->port);
555 ch->asyncflags |= ASYNC_INITIALIZED;
557 retval = block_till_ready(tty, filp, ch);
559 moxa_unthrottle(tty);
561 if (ch->type == PORT_16550A) {
562 MoxaSetFifo(ch->port, 1);
564 MoxaSetFifo(ch->port, 0);
570 static void moxa_close(struct tty_struct *tty, struct file *filp)
576 if (port == MAX_PORTS) {
579 if (!MoxaPortIsValid(port)) {
580 #ifdef SERIAL_DEBUG_CLOSE
581 printk("Invalid portno in moxa_close\n");
583 tty->driver_data = NULL;
586 if (tty->driver_data == NULL) {
589 if (tty_hung_up_p(filp)) {
592 ch = (struct moxa_str *) tty->driver_data;
594 if ((tty->count == 1) && (ch->count != 1)) {
595 printk("moxa_close: bad serial port count; tty->count is 1, "
596 "ch->count is %d\n", ch->count);
599 if (--ch->count < 0) {
600 printk("moxa_close: bad serial port count, device=%s\n",
607 ch->asyncflags |= ASYNC_CLOSING;
609 ch->cflag = tty->termios->c_cflag;
610 if (ch->asyncflags & ASYNC_INITIALIZED) {
611 setup_empty_event(tty);
612 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
613 moxaEmptyTimer_on[ch->port] = 0;
614 del_timer(&moxaEmptyTimer[ch->port]);
617 MoxaPortFlushData(port, 2);
619 if (tty->driver->flush_buffer)
620 tty->driver->flush_buffer(tty);
621 if (tty->ldisc.flush_buffer)
622 tty->ldisc.flush_buffer(tty);
626 if (ch->blocked_open) {
627 if (ch->close_delay) {
628 set_current_state(TASK_INTERRUPTIBLE);
629 schedule_timeout(ch->close_delay);
631 wake_up_interruptible(&ch->open_wait);
633 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
634 wake_up_interruptible(&ch->close_wait);
637 static int moxa_write(struct tty_struct *tty, int from_user,
638 const unsigned char *buf, int count)
644 ch = (struct moxa_str *) tty->driver_data;
650 if (count > PAGE_SIZE)
653 if (copy_from_user(moxaXmitBuff, buf, count)) {
657 len = MoxaPortWriteData(port, moxaXmitBuff, count);
658 restore_flags(flags);
665 len = MoxaPortWriteData(port, (unsigned char *) buf, count);
666 restore_flags(flags);
669 /*********************************************
670 if ( !(ch->statusflags & LOWWAIT) &&
671 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
672 ************************************************/
673 ch->statusflags |= LOWWAIT;
677 static int moxa_write_room(struct tty_struct *tty)
683 ch = (struct moxa_str *) tty->driver_data;
686 return (MoxaPortTxFree(ch->port));
689 static void moxa_flush_buffer(struct tty_struct *tty)
691 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
695 MoxaPortFlushData(ch->port, 1);
696 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
697 tty->ldisc.write_wakeup)
698 (tty->ldisc.write_wakeup) (tty);
699 wake_up_interruptible(&tty->write_wait);
702 static int moxa_chars_in_buffer(struct tty_struct *tty)
705 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
708 * Sigh...I have to check if driver_data is NULL here, because
709 * if an open() fails, the TTY subsystem eventually calls
710 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
711 * routine. And since the open() failed, we return 0 here. TDJ
715 chars = MoxaPortTxQueue(ch->port);
718 * Make it possible to wakeup anything waiting for output
719 * in tty_ioctl.c, etc.
721 if (!(ch->statusflags & EMPTYWAIT))
722 setup_empty_event(tty);
727 static void moxa_flush_chars(struct tty_struct *tty)
730 * Don't think I need this, because this is called to empty the TX
731 * buffer for the 16450, 16550, etc.
735 static void moxa_put_char(struct tty_struct *tty, unsigned char c)
741 ch = (struct moxa_str *) tty->driver_data;
748 MoxaPortWriteData(port, moxaXmitBuff, 1);
749 restore_flags(flags);
750 /************************************************
751 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
752 *************************************************/
753 ch->statusflags |= LOWWAIT;
756 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
758 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
760 int flag = 0, dtr, rts;
763 if ((port != MAX_PORTS) && (!ch))
766 MoxaPortGetLineOut(ch->port, &dtr, &rts);
771 dtr = MoxaPortLineStatus(ch->port);
781 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
782 unsigned int set, unsigned int clear)
784 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
789 if ((port != MAX_PORTS) && (!ch))
792 MoxaPortGetLineOut(ch->port, &dtr, &rts);
797 if (clear & TIOCM_RTS)
799 if (clear & TIOCM_DTR)
801 MoxaPortLineCtrl(ch->port, dtr, rts);
805 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
806 unsigned int cmd, unsigned long arg)
808 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
810 void __user *argp = (void __user *)arg;
814 if ((port != MAX_PORTS) && (!ch))
818 case TCSBRK: /* SVID version: non-zero arg --> no break */
819 retval = tty_check_change(tty);
822 setup_empty_event(tty);
823 tty_wait_until_sent(tty, 0);
825 MoxaPortSendBreak(ch->port, 0);
827 case TCSBRKP: /* support for POSIX tcsendbreak() */
828 retval = tty_check_change(tty);
831 setup_empty_event(tty);
832 tty_wait_until_sent(tty, 0);
833 MoxaPortSendBreak(ch->port, arg);
836 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) argp);
838 if(get_user(retval, (unsigned long __user *) argp))
841 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
844 ch->asyncflags &= ~ASYNC_CHECK_CD;
846 ch->asyncflags |= ASYNC_CHECK_CD;
849 return moxa_get_serial_info(ch, argp);
852 return moxa_set_serial_info(ch, argp);
854 retval = MoxaDriverIoctl(cmd, arg, port);
859 static void moxa_throttle(struct tty_struct *tty)
861 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
863 ch->statusflags |= THROTTLE;
866 static void moxa_unthrottle(struct tty_struct *tty)
868 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
870 ch->statusflags &= ~THROTTLE;
873 static void moxa_set_termios(struct tty_struct *tty,
874 struct termios *old_termios)
876 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
881 if (!(old_termios->c_cflag & CLOCAL) &&
882 (tty->termios->c_cflag & CLOCAL))
883 wake_up_interruptible(&ch->open_wait);
886 static void moxa_stop(struct tty_struct *tty)
888 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
892 MoxaPortTxDisable(ch->port);
893 ch->statusflags |= TXSTOPPED;
897 static void moxa_start(struct tty_struct *tty)
899 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
904 if (!(ch->statusflags & TXSTOPPED))
907 MoxaPortTxEnable(ch->port);
908 ch->statusflags &= ~TXSTOPPED;
911 static void moxa_hangup(struct tty_struct *tty)
913 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
915 moxa_flush_buffer(tty);
919 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
921 wake_up_interruptible(&ch->open_wait);
924 static void moxa_poll(unsigned long ignored)
928 struct tty_struct *tp;
932 del_timer(&moxaTimer);
934 if (MoxaDriverPoll() < 0) {
935 moxaTimer.function = moxa_poll;
936 moxaTimer.expires = jiffies + (HZ / 50);
938 add_timer(&moxaTimer);
941 for (card = 0; card < MAX_BOARDS; card++) {
942 if ((ports = MoxaPortsOfCard(card)) <= 0)
944 ch = &moxaChannels[card * MAX_PORTS_PER_BOARD];
945 for (i = 0; i < ports; i++, ch++) {
946 if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
948 if (!(ch->statusflags & THROTTLE) &&
949 (MoxaPortRxQueue(ch->port) > 0))
951 if ((tp = ch->tty) == 0)
953 if (ch->statusflags & LOWWAIT) {
954 if (MoxaPortTxQueue(ch->port) <= WAKEUP_CHARS) {
956 ch->statusflags &= ~LOWWAIT;
957 if ((tp->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
958 tp->ldisc.write_wakeup)
959 (tp->ldisc.write_wakeup) (tp);
960 wake_up_interruptible(&tp->write_wait);
964 if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch->port) > 0)) {
965 tty_insert_flip_char(tp, 0, TTY_BREAK);
966 tty_schedule_flip(tp);
968 if (MoxaPortDCDChange(ch->port)) {
969 if (ch->asyncflags & ASYNC_CHECK_CD) {
970 if (MoxaPortDCDON(ch->port))
971 wake_up_interruptible(&ch->open_wait);
973 set_bit(MOXA_EVENT_HANGUP, &ch->event);
974 schedule_work(&ch->tqueue);
981 moxaTimer.function = moxa_poll;
982 moxaTimer.expires = jiffies + (HZ / 50);
984 add_timer(&moxaTimer);
987 /******************************************************************************/
989 static void set_tty_param(struct tty_struct *tty)
991 register struct termios *ts;
993 int rts, cts, txflow, rxflow, xany;
995 ch = (struct moxa_str *) tty->driver_data;
997 if (ts->c_cflag & CLOCAL)
998 ch->asyncflags &= ~ASYNC_CHECK_CD;
1000 ch->asyncflags |= ASYNC_CHECK_CD;
1001 rts = cts = txflow = rxflow = xany = 0;
1002 if (ts->c_cflag & CRTSCTS)
1004 if (ts->c_iflag & IXON)
1006 if (ts->c_iflag & IXOFF)
1008 if (ts->c_iflag & IXANY)
1010 MoxaPortFlowCtrl(ch->port, rts, cts, txflow, rxflow, xany);
1011 MoxaPortSetTermio(ch->port, ts);
1014 static int block_till_ready(struct tty_struct *tty, struct file *filp,
1015 struct moxa_str *ch)
1017 DECLARE_WAITQUEUE(wait,current);
1018 unsigned long flags;
1020 int do_clocal = C_CLOCAL(tty);
1023 * If the device is in the middle of being closed, then block
1024 * until it's done, and then try again.
1026 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
1027 if (ch->asyncflags & ASYNC_CLOSING)
1028 interruptible_sleep_on(&ch->close_wait);
1029 #ifdef SERIAL_DO_RESTART
1030 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1033 return (-ERESTARTSYS);
1039 * If non-blocking mode is set, then make the check up front
1042 if (filp->f_flags & O_NONBLOCK) {
1043 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1047 * Block waiting for the carrier detect and the line to become free
1050 add_wait_queue(&ch->open_wait, &wait);
1051 #ifdef SERIAL_DEBUG_OPEN
1052 printk("block_til_ready before block: ttys%d, count = %d\n",
1053 ch->line, ch->count);
1057 if (!tty_hung_up_p(filp))
1059 restore_flags(flags);
1062 set_current_state(TASK_INTERRUPTIBLE);
1063 if (tty_hung_up_p(filp) ||
1064 !(ch->asyncflags & ASYNC_INITIALIZED)) {
1065 #ifdef SERIAL_DO_RESTART
1066 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1069 retval = -ERESTARTSYS;
1075 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
1076 MoxaPortDCDON(ch->port)))
1079 if (signal_pending(current)) {
1080 retval = -ERESTARTSYS;
1085 set_current_state(TASK_RUNNING);
1086 remove_wait_queue(&ch->open_wait, &wait);
1087 if (!tty_hung_up_p(filp))
1090 #ifdef SERIAL_DEBUG_OPEN
1091 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1092 ch->line, ch->count);
1096 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1100 static void setup_empty_event(struct tty_struct *tty)
1102 struct moxa_str *ch = tty->driver_data;
1103 unsigned long flags;
1107 ch->statusflags |= EMPTYWAIT;
1108 moxaEmptyTimer_on[ch->port] = 0;
1109 del_timer(&moxaEmptyTimer[ch->port]);
1110 moxaEmptyTimer[ch->port].expires = jiffies + HZ;
1111 moxaEmptyTimer_on[ch->port] = 1;
1112 add_timer(&moxaEmptyTimer[ch->port]);
1113 restore_flags(flags);
1116 static void check_xmit_empty(unsigned long data)
1118 struct moxa_str *ch;
1120 ch = (struct moxa_str *) data;
1121 moxaEmptyTimer_on[ch->port] = 0;
1122 del_timer(&moxaEmptyTimer[ch->port]);
1123 if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
1124 if (MoxaPortTxQueue(ch->port) == 0) {
1125 ch->statusflags &= ~EMPTYWAIT;
1126 if ((ch->tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1127 ch->tty->ldisc.write_wakeup)
1128 (ch->tty->ldisc.write_wakeup) (ch->tty);
1129 wake_up_interruptible(&ch->tty->write_wait);
1132 moxaEmptyTimer[ch->port].expires = jiffies + HZ;
1133 moxaEmptyTimer_on[ch->port] = 1;
1134 add_timer(&moxaEmptyTimer[ch->port]);
1136 ch->statusflags &= ~EMPTYWAIT;
1139 static void shut_down(struct moxa_str *ch)
1141 struct tty_struct *tp;
1143 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1148 MoxaPortDisable(ch->port);
1151 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1153 if (tp->termios->c_cflag & HUPCL)
1154 MoxaPortLineCtrl(ch->port, 0, 0);
1156 ch->asyncflags &= ~ASYNC_INITIALIZED;
1159 static void receive_data(struct moxa_str *ch)
1161 struct tty_struct *tp;
1163 int i, count, rc, space;
1164 unsigned char *charptr, *flagptr;
1165 unsigned long flags;
1171 /**************************************************
1172 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1173 *****************************************************/
1175 MoxaPortFlushData(ch->port, 0);
1178 space = TTY_FLIPBUF_SIZE - tp->flip.count;
1181 charptr = tp->flip.char_buf_ptr;
1182 flagptr = tp->flip.flag_buf_ptr;
1183 rc = tp->flip.count;
1186 count = MoxaPortReadData(ch->port, charptr, space);
1187 restore_flags(flags);
1188 for (i = 0; i < count; i++)
1192 tp->flip.count = rc;
1193 tp->flip.char_buf_ptr = charptr;
1194 tp->flip.flag_buf_ptr = flagptr;
1195 tty_schedule_flip(ch->tty);
1198 #define Magic_code 0x404
1201 * System Configuration
1204 * for C218 BIOS initialization
1206 #define C218_ConfBase 0x800
1207 #define C218_status (C218_ConfBase + 0) /* BIOS running status */
1208 #define C218_diag (C218_ConfBase + 2) /* diagnostic status */
1209 #define C218_key (C218_ConfBase + 4) /* WORD (0x218 for C218) */
1210 #define C218DLoad_len (C218_ConfBase + 6) /* WORD */
1211 #define C218check_sum (C218_ConfBase + 8) /* BYTE */
1212 #define C218chksum_ok (C218_ConfBase + 0x0a) /* BYTE (1:ok) */
1213 #define C218_TestRx (C218_ConfBase + 0x10) /* 8 bytes for 8 ports */
1214 #define C218_TestTx (C218_ConfBase + 0x18) /* 8 bytes for 8 ports */
1215 #define C218_RXerr (C218_ConfBase + 0x20) /* 8 bytes for 8 ports */
1216 #define C218_ErrFlag (C218_ConfBase + 0x28) /* 8 bytes for 8 ports */
1218 #define C218_LoadBuf 0x0F00
1219 #define C218_KeyCode 0x218
1220 #define CP204J_KeyCode 0x204
1223 * for C320 BIOS initialization
1225 #define C320_ConfBase 0x800
1226 #define C320_LoadBuf 0x0f00
1227 #define STS_init 0x05 /* for C320_status */
1229 #define C320_status C320_ConfBase + 0 /* BIOS running status */
1230 #define C320_diag C320_ConfBase + 2 /* diagnostic status */
1231 #define C320_key C320_ConfBase + 4 /* WORD (0320H for C320) */
1232 #define C320DLoad_len C320_ConfBase + 6 /* WORD */
1233 #define C320check_sum C320_ConfBase + 8 /* WORD */
1234 #define C320chksum_ok C320_ConfBase + 0x0a /* WORD (1:ok) */
1235 #define C320bapi_len C320_ConfBase + 0x0c /* WORD */
1236 #define C320UART_no C320_ConfBase + 0x0e /* WORD */
1238 #define C320_KeyCode 0x320
1240 #define FixPage_addr 0x0000 /* starting addr of static page */
1241 #define DynPage_addr 0x2000 /* starting addr of dynamic page */
1242 #define C218_start 0x3000 /* starting addr of C218 BIOS prg */
1243 #define Control_reg 0x1ff0 /* select page and reset control */
1244 #define HW_reset 0x80
1249 #define FC_CardReset 0x80
1250 #define FC_ChannelReset 1 /* C320 firmware not supported */
1251 #define FC_EnableCH 2
1252 #define FC_DisableCH 3
1253 #define FC_SetParam 4
1254 #define FC_SetMode 5
1255 #define FC_SetRate 6
1256 #define FC_LineControl 7
1257 #define FC_LineStatus 8
1258 #define FC_XmitControl 9
1259 #define FC_FlushQueue 10
1260 #define FC_SendBreak 11
1261 #define FC_StopBreak 12
1262 #define FC_LoopbackON 13
1263 #define FC_LoopbackOFF 14
1264 #define FC_ClrIrqTable 15
1265 #define FC_SendXon 16
1266 #define FC_SetTermIrq 17 /* C320 firmware not supported */
1267 #define FC_SetCntIrq 18 /* C320 firmware not supported */
1268 #define FC_SetBreakIrq 19
1269 #define FC_SetLineIrq 20
1270 #define FC_SetFlowCtl 21
1271 #define FC_GenIrq 22
1272 #define FC_InCD180 23
1273 #define FC_OutCD180 24
1274 #define FC_InUARTreg 23
1275 #define FC_OutUARTreg 24
1276 #define FC_SetXonXoff 25
1277 #define FC_OutCD180CCR 26
1278 #define FC_ExtIQueue 27
1279 #define FC_ExtOQueue 28
1280 #define FC_ClrLineIrq 29
1281 #define FC_HWFlowCtl 30
1282 #define FC_GetClockRate 35
1283 #define FC_SetBaud 36
1284 #define FC_SetDataMode 41
1285 #define FC_GetCCSR 43
1286 #define FC_GetDataError 45
1287 #define FC_RxControl 50
1288 #define FC_ImmSend 51
1289 #define FC_SetXonState 52
1290 #define FC_SetXoffState 53
1291 #define FC_SetRxFIFOTrig 54
1292 #define FC_SetTxFIFOCnt 55
1293 #define FC_UnixRate 56
1294 #define FC_UnixResetTimer 57
1296 #define RxFIFOTrig1 0
1297 #define RxFIFOTrig4 1
1298 #define RxFIFOTrig8 2
1299 #define RxFIFOTrig14 3
1304 #define DRAM_global 0
1305 #define INT_data (DRAM_global + 0)
1306 #define Config_base (DRAM_global + 0x108)
1308 #define IRQindex (INT_data + 0)
1309 #define IRQpending (INT_data + 4)
1310 #define IRQtable (INT_data + 8)
1315 #define IntrRx 0x01 /* receiver data O.K. */
1316 #define IntrTx 0x02 /* transmit buffer empty */
1317 #define IntrFunc 0x04 /* function complete */
1318 #define IntrBreak 0x08 /* received break */
1319 #define IntrLine 0x10 /* line status change
1321 #define IntrIntr 0x20 /* received INTR code */
1322 #define IntrQuit 0x40 /* received QUIT code */
1323 #define IntrEOF 0x80 /* received EOF code */
1325 #define IntrRxTrigger 0x100 /* rx data count reach tigger value */
1326 #define IntrTxTrigger 0x200 /* tx data count below trigger value */
1328 #define Magic_no (Config_base + 0)
1329 #define Card_model_no (Config_base + 2)
1330 #define Total_ports (Config_base + 4)
1331 #define Module_cnt (Config_base + 8)
1332 #define Module_no (Config_base + 10)
1333 #define Timer_10ms (Config_base + 14)
1334 #define Disable_IRQ (Config_base + 20)
1335 #define TMS320_PORT1 (Config_base + 22)
1336 #define TMS320_PORT2 (Config_base + 24)
1337 #define TMS320_CLOCK (Config_base + 26)
1340 * DATA BUFFER in DRAM
1342 #define Extern_table 0x400 /* Base address of the external table
1343 (24 words * 64) total 3K bytes
1344 (24 words * 128) total 6K bytes */
1345 #define Extern_size 0x60 /* 96 bytes */
1346 #define RXrptr 0x00 /* read pointer for RX buffer */
1347 #define RXwptr 0x02 /* write pointer for RX buffer */
1348 #define TXrptr 0x04 /* read pointer for TX buffer */
1349 #define TXwptr 0x06 /* write pointer for TX buffer */
1350 #define HostStat 0x08 /* IRQ flag and general flag */
1351 #define FlagStat 0x0A
1352 #define FlowControl 0x0C /* B7 B6 B5 B4 B3 B2 B1 B0 */
1353 /* x x x x | | | | */
1354 /* | | | + CTS flow */
1355 /* | | +--- RTS flow */
1356 /* | +------ TX Xon/Xoff */
1357 /* +--------- RX Xon/Xoff */
1358 #define Break_cnt 0x0E /* received break count */
1359 #define CD180TXirq 0x10 /* if non-0: enable TX irq */
1360 #define RX_mask 0x12
1361 #define TX_mask 0x14
1362 #define Ofs_rxb 0x16
1363 #define Ofs_txb 0x18
1364 #define Page_rxb 0x1A
1365 #define Page_txb 0x1C
1366 #define EndPage_rxb 0x1E
1367 #define EndPage_txb 0x20
1368 #define Data_error 0x22
1369 #define RxTrigger 0x28
1370 #define TxTrigger 0x2a
1372 #define rRXwptr 0x34
1373 #define Low_water 0x36
1375 #define FuncCode 0x40
1376 #define FuncArg 0x42
1377 #define FuncArg1 0x44
1379 #define C218rx_size 0x2000 /* 8K bytes */
1380 #define C218tx_size 0x8000 /* 32K bytes */
1382 #define C218rx_mask (C218rx_size - 1)
1383 #define C218tx_mask (C218tx_size - 1)
1385 #define C320p8rx_size 0x2000
1386 #define C320p8tx_size 0x8000
1387 #define C320p8rx_mask (C320p8rx_size - 1)
1388 #define C320p8tx_mask (C320p8tx_size - 1)
1390 #define C320p16rx_size 0x2000
1391 #define C320p16tx_size 0x4000
1392 #define C320p16rx_mask (C320p16rx_size - 1)
1393 #define C320p16tx_mask (C320p16tx_size - 1)
1395 #define C320p24rx_size 0x2000
1396 #define C320p24tx_size 0x2000
1397 #define C320p24rx_mask (C320p24rx_size - 1)
1398 #define C320p24tx_mask (C320p24tx_size - 1)
1400 #define C320p32rx_size 0x1000
1401 #define C320p32tx_size 0x1000
1402 #define C320p32rx_mask (C320p32rx_size - 1)
1403 #define C320p32tx_mask (C320p32tx_size - 1)
1405 #define Page_size 0x2000
1406 #define Page_mask (Page_size - 1)
1407 #define C218rx_spage 3
1408 #define C218tx_spage 4
1409 #define C218rx_pageno 1
1410 #define C218tx_pageno 4
1411 #define C218buf_pageno 5
1413 #define C320p8rx_spage 3
1414 #define C320p8tx_spage 4
1415 #define C320p8rx_pgno 1
1416 #define C320p8tx_pgno 4
1417 #define C320p8buf_pgno 5
1419 #define C320p16rx_spage 3
1420 #define C320p16tx_spage 4
1421 #define C320p16rx_pgno 1
1422 #define C320p16tx_pgno 2
1423 #define C320p16buf_pgno 3
1425 #define C320p24rx_spage 3
1426 #define C320p24tx_spage 4
1427 #define C320p24rx_pgno 1
1428 #define C320p24tx_pgno 1
1429 #define C320p24buf_pgno 2
1431 #define C320p32rx_spage 3
1432 #define C320p32tx_ofs C320p32rx_size
1433 #define C320p32tx_spage 3
1434 #define C320p32buf_pgno 1
1439 #define WakeupRx 0x01
1440 #define WakeupTx 0x02
1441 #define WakeupBreak 0x08
1442 #define WakeupLine 0x10
1443 #define WakeupIntr 0x20
1444 #define WakeupQuit 0x40
1445 #define WakeupEOF 0x80 /* used in VTIME control */
1446 #define WakeupRxTrigger 0x100
1447 #define WakeupTxTrigger 0x200
1451 #define Rx_over 0x01
1452 #define Xoff_state 0x02
1453 #define Tx_flowOff 0x04
1454 #define Tx_enable 0x08
1455 #define CTS_state 0x10
1456 #define DSR_state 0x20
1457 #define DCD_state 0x80
1461 #define CTS_FlowCtl 1
1462 #define RTS_FlowCtl 2
1463 #define Tx_FlowCtl 4
1464 #define Rx_FlowCtl 8
1465 #define IXM_IXANY 0x10
1467 #define LowWater 128
1475 /* mode definition */
1481 #define MX_STOP1 0x00
1482 #define MX_STOP15 0x04
1483 #define MX_STOP2 0x08
1485 #define MX_PARNONE 0x00
1486 #define MX_PAREVEN 0x40
1487 #define MX_PARODD 0xC0
1492 #define QueryPort MAX_PORTS
1498 int rxcnt[MAX_PORTS];
1499 int txcnt[MAX_PORTS];
1501 typedef struct mon_str mon_st;
1503 #define DCD_changed 0x01
1504 #define DCD_oldstate 0x80
1506 static unsigned char moxaBuff[10240];
1507 static unsigned long moxaIntNdx[MAX_BOARDS];
1508 static unsigned long moxaIntPend[MAX_BOARDS];
1509 static unsigned long moxaIntTable[MAX_BOARDS];
1510 static char moxaChkPort[MAX_PORTS];
1511 static char moxaLineCtrl[MAX_PORTS];
1512 static unsigned long moxaTableAddr[MAX_PORTS];
1513 static long moxaCurBaud[MAX_PORTS];
1514 static char moxaDCDState[MAX_PORTS];
1515 static char moxaLowChkFlag[MAX_PORTS];
1516 static int moxaLowWaterChk;
1517 static int moxaCard;
1518 static mon_st moxaLog;
1519 static int moxaFuncTout;
1520 static ushort moxaBreakCnt[MAX_PORTS];
1522 static void moxadelay(int);
1523 static void moxafunc(unsigned long, int, ushort);
1524 static void wait_finish(unsigned long);
1525 static void low_water_check(unsigned long);
1526 static int moxaloadbios(int, unsigned char __user *, int);
1527 static int moxafindcard(int);
1528 static int moxaload320b(int, unsigned char __user *, int);
1529 static int moxaloadcode(int, unsigned char __user *, int);
1530 static int moxaloadc218(int, unsigned long, int);
1531 static int moxaloadc320(int, unsigned long, int, int *);
1533 /*****************************************************************************
1534 * Driver level functions: *
1535 * 1. MoxaDriverInit(void); *
1536 * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
1537 * 3. MoxaDriverPoll(void); *
1538 *****************************************************************************/
1539 void MoxaDriverInit(void)
1543 moxaFuncTout = HZ / 2; /* 500 mini-seconds */
1546 moxaLowWaterChk = 0;
1547 for (i = 0; i < MAX_PORTS; i++) {
1549 moxaLowChkFlag[i] = 0;
1550 moxaLineCtrl[i] = 0;
1551 moxaLog.rxcnt[i] = 0;
1552 moxaLog.txcnt[i] = 0;
1557 #define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1558 #define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
1559 #define MOXA_INIT_DRIVER (MOXA + 6) /* moxaCard=0 */
1560 #define MOXA_LOAD_BIOS (MOXA + 9) /* download BIOS */
1561 #define MOXA_FIND_BOARD (MOXA + 10) /* Check if MOXA card exist? */
1562 #define MOXA_LOAD_C320B (MOXA + 11) /* download 320B firmware */
1563 #define MOXA_LOAD_CODE (MOXA + 12) /* download firmware */
1564 #define MOXA_GETDATACOUNT (MOXA + 23)
1565 #define MOXA_GET_IOQUEUE (MOXA + 27)
1566 #define MOXA_FLUSH_QUEUE (MOXA + 28)
1567 #define MOXA_GET_CONF (MOXA + 35) /* configuration */
1568 #define MOXA_GET_MAJOR (MOXA + 63)
1569 #define MOXA_GET_CUMAJOR (MOXA + 64)
1570 #define MOXA_GETMSTATUS (MOXA + 65)
1584 static struct moxaq_str temp_queue[MAX_PORTS];
1585 static struct dl_str dltmp;
1587 void MoxaPortFlushData(int port, int mode)
1589 unsigned long ofsAddr;
1590 if ((mode < 0) || (mode > 2))
1592 ofsAddr = moxaTableAddr[port];
1593 moxafunc(ofsAddr, FC_FlushQueue, mode);
1595 moxaLowChkFlag[port] = 0;
1596 low_water_check(ofsAddr);
1600 int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port)
1604 int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
1605 void __user *argp = (void __user *)arg;
1607 if (port == QueryPort) {
1608 if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_INIT_DRIVER) &&
1609 (cmd != MOXA_LOAD_BIOS) && (cmd != MOXA_FIND_BOARD) && (cmd != MOXA_LOAD_C320B) &&
1610 (cmd != MOXA_LOAD_CODE) && (cmd != MOXA_GETDATACOUNT) &&
1611 (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
1612 (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
1617 if(copy_to_user(argp, &moxa_boards, MAX_BOARDS * sizeof(moxa_board_conf)))
1620 case MOXA_INIT_DRIVER:
1621 if ((int) arg == 0x404)
1624 case MOXA_GETDATACOUNT:
1625 moxaLog.tick = jiffies;
1626 if(copy_to_user(argp, &moxaLog, sizeof(mon_st)))
1629 case MOXA_FLUSH_QUEUE:
1630 MoxaPortFlushData(port, arg);
1632 case MOXA_GET_IOQUEUE:
1633 for (i = 0; i < MAX_PORTS; i++) {
1634 if (moxaChkPort[i]) {
1635 temp_queue[i].inq = MoxaPortRxQueue(i);
1636 temp_queue[i].outq = MoxaPortTxQueue(i);
1639 if(copy_to_user(argp, temp_queue, sizeof(struct moxaq_str) * MAX_PORTS))
1642 case MOXA_GET_OQUEUE:
1643 i = MoxaPortTxQueue(port);
1644 return put_user(i, (unsigned long __user *)argp);
1645 case MOXA_GET_IQUEUE:
1646 i = MoxaPortRxQueue(port);
1647 return put_user(i, (unsigned long __user *)argp);
1648 case MOXA_GET_MAJOR:
1649 if(copy_to_user(argp, &ttymajor, sizeof(int)))
1652 case MOXA_GET_CUMAJOR:
1654 if(copy_to_user(argp, &i, sizeof(int)))
1657 case MOXA_GETMSTATUS:
1658 for (i = 0; i < MAX_PORTS; i++) {
1660 GMStatus[i].dcd = 0;
1661 GMStatus[i].dsr = 0;
1662 GMStatus[i].cts = 0;
1663 if (!moxaChkPort[i]) {
1666 status = MoxaPortLineStatus(moxaChannels[i].port);
1668 GMStatus[i].cts = 1;
1670 GMStatus[i].dsr = 1;
1672 GMStatus[i].dcd = 1;
1675 if (!moxaChannels[i].tty || !moxaChannels[i].tty->termios)
1676 GMStatus[i].cflag = moxaChannels[i].cflag;
1678 GMStatus[i].cflag = moxaChannels[i].tty->termios->c_cflag;
1680 if(copy_to_user(argp, GMStatus, sizeof(struct mxser_mstatus) * MAX_PORTS))
1684 return (-ENOIOCTLCMD);
1685 case MOXA_LOAD_BIOS:
1686 case MOXA_FIND_BOARD:
1687 case MOXA_LOAD_C320B:
1688 case MOXA_LOAD_CODE:
1692 if(copy_from_user(&dltmp, argp, sizeof(struct dl_str)))
1694 if(dltmp.cardno < 0 || dltmp.cardno >= MAX_BOARDS)
1699 case MOXA_LOAD_BIOS:
1700 i = moxaloadbios(dltmp.cardno, dltmp.buf, dltmp.len);
1702 case MOXA_FIND_BOARD:
1703 return moxafindcard(dltmp.cardno);
1704 case MOXA_LOAD_C320B:
1705 moxaload320b(dltmp.cardno, dltmp.buf, dltmp.len);
1706 default: /* to keep gcc happy */
1708 case MOXA_LOAD_CODE:
1709 i = moxaloadcode(dltmp.cardno, dltmp.buf, dltmp.len);
1717 int MoxaDriverPoll(void)
1719 register ushort temp;
1721 unsigned long ip, ofsAddr;
1726 for (card = 0; card < MAX_BOARDS; card++) {
1727 if ((ports = moxa_boards[card].numPorts) == 0)
1729 if (readb(moxaIntPend[card]) == 0xff) {
1730 ip = moxaIntTable[card] + readb(moxaIntNdx[card]);
1731 p = card * MAX_PORTS_PER_BOARD;
1733 for (port = 0; port < ports; port += 2, p++) {
1734 if ((temp = readw(ip + port)) != 0) {
1735 writew(0, ip + port);
1736 ofsAddr = moxaTableAddr[p];
1738 writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
1739 if (temp & IntrBreak) {
1742 if (temp & IntrLine) {
1743 if (readb(ofsAddr + FlagStat) & DCD_state) {
1744 if ((moxaDCDState[p] & DCD_oldstate) == 0)
1745 moxaDCDState[p] = (DCD_oldstate |
1748 if (moxaDCDState[p] & DCD_oldstate)
1749 moxaDCDState[p] = DCD_changed;
1754 writeb(0, moxaIntPend[card]);
1756 if (moxaLowWaterChk) {
1757 p = card * MAX_PORTS_PER_BOARD;
1758 for (port = 0; port < ports; port++, p++) {
1759 if (moxaLowChkFlag[p]) {
1760 moxaLowChkFlag[p] = 0;
1761 ofsAddr = moxaTableAddr[p];
1762 low_water_check(ofsAddr);
1767 moxaLowWaterChk = 0;
1771 /*****************************************************************************
1772 * Card level function: *
1773 * 1. MoxaPortsOfCard(int cardno); *
1774 *****************************************************************************/
1775 int MoxaPortsOfCard(int cardno)
1778 if (moxa_boards[cardno].boardType == 0)
1780 return (moxa_boards[cardno].numPorts);
1783 /*****************************************************************************
1784 * Port level functions: *
1785 * 1. MoxaPortIsValid(int port); *
1786 * 2. MoxaPortEnable(int port); *
1787 * 3. MoxaPortDisable(int port); *
1788 * 4. MoxaPortGetMaxBaud(int port); *
1789 * 5. MoxaPortGetCurBaud(int port); *
1790 * 6. MoxaPortSetBaud(int port, long baud); *
1791 * 7. MoxaPortSetMode(int port, int databit, int stopbit, int parity); *
1792 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1793 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1794 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1795 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1796 * 12. MoxaPortLineStatus(int port); *
1797 * 13. MoxaPortDCDChange(int port); *
1798 * 14. MoxaPortDCDON(int port); *
1799 * 15. MoxaPortFlushData(int port, int mode); *
1800 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1801 * 17. MoxaPortReadData(int port, unsigned char * buffer, int length); *
1802 * 18. MoxaPortTxBufSize(int port); *
1803 * 19. MoxaPortRxBufSize(int port); *
1804 * 20. MoxaPortTxQueue(int port); *
1805 * 21. MoxaPortTxFree(int port); *
1806 * 22. MoxaPortRxQueue(int port); *
1807 * 23. MoxaPortRxFree(int port); *
1808 * 24. MoxaPortTxDisable(int port); *
1809 * 25. MoxaPortTxEnable(int port); *
1810 * 26. MoxaPortGetBrkCnt(int port); *
1811 * 27. MoxaPortResetBrkCnt(int port); *
1812 * 28. MoxaPortSetXonXoff(int port, int xonValue, int xoffValue); *
1813 * 29. MoxaPortIsTxHold(int port); *
1814 * 30. MoxaPortSendBreak(int port, int ticks); *
1815 *****************************************************************************/
1817 * Moxa Port Number Description:
1819 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1820 * the port number using in MOXA driver functions will be 0 to 31 for
1821 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1822 * to 127 for fourth. For example, if you setup three MOXA boards,
1823 * first board is C218, second board is C320-16 and third board is
1824 * C320-32. The port number of first board (C218 - 8 ports) is from
1825 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1826 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1827 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1828 * 127 will be invalid.
1831 * Moxa Functions Description:
1833 * Function 1: Driver initialization routine, this routine must be
1834 * called when initialized driver.
1836 * void MoxaDriverInit();
1839 * Function 2: Moxa driver private IOCTL command processing.
1841 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1843 * unsigned int cmd : IOCTL command
1844 * unsigned long arg : IOCTL argument
1845 * int port : port number (0 - 127)
1852 * Function 3: Moxa driver polling process routine.
1854 * int MoxaDriverPoll(void);
1856 * return: 0 ; polling O.K.
1857 * -1 : no any Moxa card.
1860 * Function 4: Get the ports of this card.
1862 * int MoxaPortsOfCard(int cardno);
1864 * int cardno : card number (0 - 3)
1866 * return: 0 : this card is invalid
1870 * Function 5: Check this port is valid or invalid
1872 * int MoxaPortIsValid(int port);
1873 * int port : port number (0 - 127, ref port description)
1875 * return: 0 : this port is invalid
1876 * 1 : this port is valid
1879 * Function 6: Enable this port to start Tx/Rx data.
1881 * void MoxaPortEnable(int port);
1882 * int port : port number (0 - 127)
1885 * Function 7: Disable this port
1887 * void MoxaPortDisable(int port);
1888 * int port : port number (0 - 127)
1891 * Function 8: Get the maximun available baud rate of this port.
1893 * long MoxaPortGetMaxBaud(int port);
1894 * int port : port number (0 - 127)
1896 * return: 0 : this port is invalid
1897 * 38400/57600/115200 bps
1900 * Function 9: Get the current baud rate of this port.
1902 * long MoxaPortGetCurBaud(int port);
1903 * int port : port number (0 - 127)
1905 * return: 0 : this port is invalid
1909 * Function 10: Setting baud rate of this port.
1911 * long MoxaPortSetBaud(int port, long baud);
1912 * int port : port number (0 - 127)
1913 * long baud : baud rate (50 - 115200)
1915 * return: 0 : this port is invalid or baud < 50
1916 * 50 - 115200 : the real baud rate set to the port, if
1917 * the argument baud is large than maximun
1918 * available baud rate, the real setting
1919 * baud rate will be the maximun baud rate.
1922 * Function 11: Setting the data-bits/stop-bits/parity of this port
1924 * int MoxaPortSetMode(int port, int databits, int stopbits, int parity);
1925 * int port : port number (0 - 127)
1926 * int databits : data bits (8/7/6/5)
1927 * int stopbits : stop bits (2/1/0, 0 show 1.5 stop bits)
1928 int parity : parity (0:None,1:Odd,2:Even,3:Mark,4:Space)
1930 * return: -1 : invalid parameter
1934 * Function 12: Configure the port.
1936 * int MoxaPortSetTermio(int port, struct termios *termio);
1937 * int port : port number (0 - 127)
1938 * struct termios * termio : termio structure pointer
1940 * return: -1 : this port is invalid or termio == NULL
1944 * Function 13: Get the DTR/RTS state of this port.
1946 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1947 * int port : port number (0 - 127)
1948 * int * dtrState : pointer to INT to receive the current DTR
1949 * state. (if NULL, this function will not
1950 * write to this address)
1951 * int * rtsState : pointer to INT to receive the current RTS
1952 * state. (if NULL, this function will not
1953 * write to this address)
1955 * return: -1 : this port is invalid
1959 * Function 14: Setting the DTR/RTS output state of this port.
1961 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1962 * int port : port number (0 - 127)
1963 * int dtrState : DTR output state (0: off, 1: on)
1964 * int rtsState : RTS output state (0: off, 1: on)
1967 * Function 15: Setting the flow control of this port.
1969 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1970 * int txFlow,int xany);
1971 * int port : port number (0 - 127)
1972 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1973 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1974 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1975 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1976 * int xany : S/W XANY flow control (0: no, 1: yes)
1979 * Function 16: Get ths line status of this port
1981 * int MoxaPortLineStatus(int port);
1982 * int port : port number (0 - 127)
1984 * return: Bit 0 - CTS state (0: off, 1: on)
1985 * Bit 1 - DSR state (0: off, 1: on)
1986 * Bit 2 - DCD state (0: off, 1: on)
1989 * Function 17: Check the DCD state has changed since the last read
1992 * int MoxaPortDCDChange(int port);
1993 * int port : port number (0 - 127)
1995 * return: 0 : no changed
1996 * 1 : DCD has changed
1999 * Function 18: Check ths current DCD state is ON or not.
2001 * int MoxaPortDCDON(int port);
2002 * int port : port number (0 - 127)
2004 * return: 0 : DCD off
2008 * Function 19: Flush the Rx/Tx buffer data of this port.
2010 * void MoxaPortFlushData(int port, int mode);
2011 * int port : port number (0 - 127)
2013 * 0 : flush the Rx buffer
2014 * 1 : flush the Tx buffer
2015 * 2 : flush the Rx and Tx buffer
2018 * Function 20: Write data.
2020 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
2021 * int port : port number (0 - 127)
2022 * unsigned char * buffer : pointer to write data buffer.
2023 * int length : write data length
2025 * return: 0 - length : real write data length
2028 * Function 21: Read data.
2030 * int MoxaPortReadData(int port, unsigned char * buffer, int length);
2031 * int port : port number (0 - 127)
2032 * unsigned char * buffer : pointer to read data buffer.
2033 * int length : read data buffer length
2035 * return: 0 - length : real read data length
2038 * Function 22: Get the Tx buffer size of this port
2040 * int MoxaPortTxBufSize(int port);
2041 * int port : port number (0 - 127)
2043 * return: .. : Tx buffer size
2046 * Function 23: Get the Rx buffer size of this port
2048 * int MoxaPortRxBufSize(int port);
2049 * int port : port number (0 - 127)
2051 * return: .. : Rx buffer size
2054 * Function 24: Get the Tx buffer current queued data bytes
2056 * int MoxaPortTxQueue(int port);
2057 * int port : port number (0 - 127)
2059 * return: .. : Tx buffer current queued data bytes
2062 * Function 25: Get the Tx buffer current free space
2064 * int MoxaPortTxFree(int port);
2065 * int port : port number (0 - 127)
2067 * return: .. : Tx buffer current free space
2070 * Function 26: Get the Rx buffer current queued data bytes
2072 * int MoxaPortRxQueue(int port);
2073 * int port : port number (0 - 127)
2075 * return: .. : Rx buffer current queued data bytes
2078 * Function 27: Get the Rx buffer current free space
2080 * int MoxaPortRxFree(int port);
2081 * int port : port number (0 - 127)
2083 * return: .. : Rx buffer current free space
2086 * Function 28: Disable port data transmission.
2088 * void MoxaPortTxDisable(int port);
2089 * int port : port number (0 - 127)
2092 * Function 29: Enable port data transmission.
2094 * void MoxaPortTxEnable(int port);
2095 * int port : port number (0 - 127)
2098 * Function 30: Get the received BREAK signal count.
2100 * int MoxaPortGetBrkCnt(int port);
2101 * int port : port number (0 - 127)
2103 * return: 0 - .. : BREAK signal count
2106 * Function 31: Get the received BREAK signal count and reset it.
2108 * int MoxaPortResetBrkCnt(int port);
2109 * int port : port number (0 - 127)
2111 * return: 0 - .. : BREAK signal count
2114 * Function 32: Set the S/W flow control new XON/XOFF value, default
2115 * XON is 0x11 & XOFF is 0x13.
2117 * void MoxaPortSetXonXoff(int port, int xonValue, int xoffValue);
2118 * int port : port number (0 - 127)
2119 * int xonValue : new XON value (0 - 255)
2120 * int xoffValue : new XOFF value (0 - 255)
2123 * Function 33: Check this port's transmission is hold by remote site
2124 * because the flow control.
2126 * int MoxaPortIsTxHold(int port);
2127 * int port : port number (0 - 127)
2129 * return: 0 : normal
2130 * 1 : hold by remote site
2133 * Function 34: Send out a BREAK signal.
2135 * void MoxaPortSendBreak(int port, int ms100);
2136 * int port : port number (0 - 127)
2137 * int ms100 : break signal time interval.
2138 * unit: 100 mini-second. if ms100 == 0, it will
2139 * send out a about 250 ms BREAK signal.
2142 int MoxaPortIsValid(int port)
2147 if (moxaChkPort[port] == 0)
2152 void MoxaPortEnable(int port)
2154 unsigned long ofsAddr;
2155 int MoxaPortLineStatus(int);
2156 short lowwater = 512;
2158 ofsAddr = moxaTableAddr[port];
2159 writew(lowwater, ofsAddr + Low_water);
2160 moxaBreakCnt[port] = 0;
2161 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2162 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2163 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
2165 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
2168 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
2169 moxafunc(ofsAddr, FC_FlushQueue, 2);
2171 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
2172 MoxaPortLineStatus(port);
2175 void MoxaPortDisable(int port)
2177 unsigned long ofsAddr;
2179 ofsAddr = moxaTableAddr[port];
2180 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
2181 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
2182 writew(0, ofsAddr + HostStat);
2183 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
2186 long MoxaPortGetMaxBaud(int port)
2188 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2189 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI))
2196 long MoxaPortSetBaud(int port, long baud)
2198 unsigned long ofsAddr;
2202 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
2204 ofsAddr = moxaTableAddr[port];
2208 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
2209 else if (max == 57600L)
2210 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
2212 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
2214 moxafunc(ofsAddr, FC_SetBaud, val);
2216 moxaCurBaud[port] = baud;
2220 int MoxaPortSetTermio(int port, struct termios *termio)
2222 unsigned long ofsAddr;
2227 if (moxaChkPort[port] == 0 || termio == 0)
2229 ofsAddr = moxaTableAddr[port];
2230 cflag = termio->c_cflag; /* termio->c_cflag */
2232 mode = termio->c_cflag & CSIZE;
2235 else if (mode == CS6)
2237 else if (mode == CS7)
2239 else if (mode == CS8)
2242 if (termio->c_cflag & CSTOPB) {
2250 if (termio->c_cflag & PARENB) {
2251 if (termio->c_cflag & PARODD)
2258 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
2260 cflag &= (CBAUD | CBAUDEX);
2262 #define B921600 (B460800+1)
2328 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2329 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2330 if (baud == 921600L)
2333 MoxaPortSetBaud(port, baud);
2335 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
2336 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
2337 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
2338 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
2339 wait_finish(ofsAddr);
2345 int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState)
2348 if (!MoxaPortIsValid(port))
2351 if (moxaLineCtrl[port] & DTR_ON)
2357 if (moxaLineCtrl[port] & RTS_ON)
2365 void MoxaPortLineCtrl(int port, int dtr, int rts)
2367 unsigned long ofsAddr;
2370 ofsAddr = moxaTableAddr[port];
2376 moxaLineCtrl[port] = mode;
2377 moxafunc(ofsAddr, FC_LineControl, mode);
2380 void MoxaPortFlowCtrl(int port, int rts, int cts, int txflow, int rxflow, int txany)
2382 unsigned long ofsAddr;
2385 ofsAddr = moxaTableAddr[port];
2388 mode |= RTS_FlowCtl;
2390 mode |= CTS_FlowCtl;
2397 moxafunc(ofsAddr, FC_SetFlowCtl, mode);
2400 int MoxaPortLineStatus(int port)
2402 unsigned long ofsAddr;
2405 ofsAddr = moxaTableAddr[port];
2406 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2407 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2408 moxafunc(ofsAddr, FC_LineStatus, 0);
2409 val = readw(ofsAddr + FuncArg);
2411 val = readw(ofsAddr + FlagStat) >> 4;
2416 if ((moxaDCDState[port] & DCD_oldstate) == 0)
2417 moxaDCDState[port] = (DCD_oldstate | DCD_changed);
2419 if (moxaDCDState[port] & DCD_oldstate)
2420 moxaDCDState[port] = DCD_changed;
2426 int MoxaPortDCDChange(int port)
2430 if (moxaChkPort[port] == 0)
2432 n = moxaDCDState[port];
2433 moxaDCDState[port] &= ~DCD_changed;
2438 int MoxaPortDCDON(int port)
2442 if (moxaChkPort[port] == 0)
2444 if (moxaDCDState[port] & DCD_oldstate)
2453 int MoxaDumpMem(int port, unsigned char * buffer, int len)
2456 unsigned long baseAddr,ofsAddr,ofs;
2458 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2459 ofs = baseAddr + DynPage_addr + pageofs;
2462 for (i = 0; i < len; i++)
2463 buffer[i] = readb(ofs+i);
2468 int MoxaPortWriteData(int port, unsigned char * buffer, int len)
2473 ushort head, tx_mask, spage, epage;
2474 ushort pageno, pageofs, bufhead;
2475 unsigned long baseAddr, ofsAddr, ofs;
2477 ofsAddr = moxaTableAddr[port];
2478 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2479 tx_mask = readw(ofsAddr + TX_mask);
2480 spage = readw(ofsAddr + Page_txb);
2481 epage = readw(ofsAddr + EndPage_txb);
2482 tail = readw(ofsAddr + TXwptr);
2483 head = readw(ofsAddr + TXrptr);
2484 c = (head > tail) ? (head - tail - 1)
2485 : (head - tail + tx_mask);
2488 moxaLog.txcnt[port] += c;
2490 if (spage == epage) {
2491 bufhead = readw(ofsAddr + Ofs_txb);
2492 writew(spage, baseAddr + Control_reg);
2495 len = head - tail - 1;
2497 len = tx_mask + 1 - tail;
2498 len = (c > len) ? len : c;
2499 ofs = baseAddr + DynPage_addr + bufhead + tail;
2500 for (i = 0; i < len; i++)
2501 writeb(*buffer++, ofs + i);
2502 tail = (tail + len) & tx_mask;
2505 writew(tail, ofsAddr + TXwptr);
2508 pageno = spage + (tail >> 13);
2509 pageofs = tail & Page_mask;
2511 cnt = Page_size - pageofs;
2515 writeb(pageno, baseAddr + Control_reg);
2516 ofs = baseAddr + DynPage_addr + pageofs;
2517 for (i = 0; i < cnt; i++)
2518 writeb(*buffer++, ofs + i);
2520 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2523 if (++pageno == epage)
2528 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2532 int MoxaPortReadData(int port, unsigned char * buffer, int space)
2534 register ushort head, pageofs;
2535 int i, count, cnt, len, total, remain;
2536 ushort tail, rx_mask, spage, epage;
2537 ushort pageno, bufhead;
2538 unsigned long baseAddr, ofsAddr, ofs;
2540 ofsAddr = moxaTableAddr[port];
2541 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2542 head = readw(ofsAddr + RXrptr);
2543 tail = readw(ofsAddr + RXwptr);
2544 rx_mask = readw(ofsAddr + RX_mask);
2545 spage = readw(ofsAddr + Page_rxb);
2546 epage = readw(ofsAddr + EndPage_rxb);
2547 count = (tail >= head) ? (tail - head)
2548 : (tail - head + rx_mask + 1);
2552 total = (space > count) ? count : space;
2553 remain = count - total;
2554 moxaLog.rxcnt[port] += total;
2556 if (spage == epage) {
2557 bufhead = readw(ofsAddr + Ofs_rxb);
2558 writew(spage, baseAddr + Control_reg);
2563 len = rx_mask + 1 - head;
2564 len = (count > len) ? len : count;
2565 ofs = baseAddr + DynPage_addr + bufhead + head;
2566 for (i = 0; i < len; i++)
2567 *buffer++ = readb(ofs + i);
2568 head = (head + len) & rx_mask;
2571 writew(head, ofsAddr + RXrptr);
2574 pageno = spage + (head >> 13);
2575 pageofs = head & Page_mask;
2577 cnt = Page_size - pageofs;
2581 writew(pageno, baseAddr + Control_reg);
2582 ofs = baseAddr + DynPage_addr + pageofs;
2583 for (i = 0; i < cnt; i++)
2584 *buffer++ = readb(ofs + i);
2586 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2589 if (++pageno == epage)
2594 if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2595 moxaLowWaterChk = 1;
2596 moxaLowChkFlag[port] = 1;
2602 int MoxaPortTxQueue(int port)
2604 unsigned long ofsAddr;
2605 ushort rptr, wptr, mask;
2608 ofsAddr = moxaTableAddr[port];
2609 rptr = readw(ofsAddr + TXrptr);
2610 wptr = readw(ofsAddr + TXwptr);
2611 mask = readw(ofsAddr + TX_mask);
2612 len = (wptr - rptr) & mask;
2616 int MoxaPortTxFree(int port)
2618 unsigned long ofsAddr;
2619 ushort rptr, wptr, mask;
2622 ofsAddr = moxaTableAddr[port];
2623 rptr = readw(ofsAddr + TXrptr);
2624 wptr = readw(ofsAddr + TXwptr);
2625 mask = readw(ofsAddr + TX_mask);
2626 len = mask - ((wptr - rptr) & mask);
2630 int MoxaPortRxQueue(int port)
2632 unsigned long ofsAddr;
2633 ushort rptr, wptr, mask;
2636 ofsAddr = moxaTableAddr[port];
2637 rptr = readw(ofsAddr + RXrptr);
2638 wptr = readw(ofsAddr + RXwptr);
2639 mask = readw(ofsAddr + RX_mask);
2640 len = (wptr - rptr) & mask;
2645 void MoxaPortTxDisable(int port)
2647 unsigned long ofsAddr;
2649 ofsAddr = moxaTableAddr[port];
2650 moxafunc(ofsAddr, FC_SetXoffState, Magic_code);
2653 void MoxaPortTxEnable(int port)
2655 unsigned long ofsAddr;
2657 ofsAddr = moxaTableAddr[port];
2658 moxafunc(ofsAddr, FC_SetXonState, Magic_code);
2662 int MoxaPortResetBrkCnt(int port)
2665 cnt = moxaBreakCnt[port];
2666 moxaBreakCnt[port] = 0;
2671 void MoxaPortSendBreak(int port, int ms100)
2673 unsigned long ofsAddr;
2675 ofsAddr = moxaTableAddr[port];
2677 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2678 moxadelay(ms100 * (HZ / 10));
2680 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2681 moxadelay(HZ / 4); /* 250 ms */
2683 moxafunc(ofsAddr, FC_StopBreak, Magic_code);
2686 static int moxa_get_serial_info(struct moxa_str *info,
2687 struct serial_struct __user *retinfo)
2689 struct serial_struct tmp;
2691 memset(&tmp, 0, sizeof(tmp));
2692 tmp.type = info->type;
2693 tmp.line = info->port;
2696 tmp.flags = info->asyncflags;
2697 tmp.baud_base = 921600;
2698 tmp.close_delay = info->close_delay;
2699 tmp.closing_wait = info->closing_wait;
2700 tmp.custom_divisor = 0;
2702 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2708 static int moxa_set_serial_info(struct moxa_str *info,
2709 struct serial_struct __user *new_info)
2711 struct serial_struct new_serial;
2713 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2716 if ((new_serial.irq != 0) ||
2717 (new_serial.port != 0) ||
2718 // (new_serial.type != info->type) ||
2719 (new_serial.custom_divisor != 0) ||
2720 (new_serial.baud_base != 921600))
2723 if (!capable(CAP_SYS_ADMIN)) {
2724 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2725 (info->asyncflags & ~ASYNC_USR_MASK)))
2728 info->close_delay = new_serial.close_delay * HZ / 100;
2729 info->closing_wait = new_serial.closing_wait * HZ / 100;
2732 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2733 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2735 if (new_serial.type == PORT_16550A) {
2736 MoxaSetFifo(info->port, 1);
2738 MoxaSetFifo(info->port, 0);
2741 info->type = new_serial.type;
2747 /*****************************************************************************
2748 * Static local functions: *
2749 *****************************************************************************/
2751 * moxadelay - delays a specified number ticks
2753 static void moxadelay(int tick)
2755 unsigned long st, et;
2759 while (time_before(jiffies, et));
2762 static void moxafunc(unsigned long ofsAddr, int cmd, ushort arg)
2765 writew(arg, ofsAddr + FuncArg);
2766 writew(cmd, ofsAddr + FuncCode);
2767 wait_finish(ofsAddr);
2770 static void wait_finish(unsigned long ofsAddr)
2775 while (readw(ofsAddr + FuncCode) != 0) {
2777 if ((j - i) > moxaFuncTout) {
2783 static void low_water_check(unsigned long ofsAddr)
2786 ushort rptr, wptr, mask;
2788 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2789 rptr = readw(ofsAddr + RXrptr);
2790 wptr = readw(ofsAddr + RXwptr);
2791 mask = readw(ofsAddr + RX_mask);
2792 len = (wptr - rptr) & mask;
2793 if (len <= Low_water)
2794 moxafunc(ofsAddr, FC_SendXon, 0);
2798 static int moxaloadbios(int cardno, unsigned char __user *tmp, int len)
2800 unsigned long baseAddr;
2803 if(copy_from_user(moxaBuff, tmp, len))
2805 baseAddr = moxaBaseAddr[cardno];
2806 writeb(HW_reset, baseAddr + Control_reg); /* reset */
2807 moxadelay(1); /* delay 10 ms */
2808 for (i = 0; i < 4096; i++)
2809 writeb(0, baseAddr + i); /* clear fix page */
2810 for (i = 0; i < len; i++)
2811 writeb(moxaBuff[i], baseAddr + i); /* download BIOS */
2812 writeb(0, baseAddr + Control_reg); /* restart */
2816 static int moxafindcard(int cardno)
2818 unsigned long baseAddr;
2821 baseAddr = moxaBaseAddr[cardno];
2822 switch (moxa_boards[cardno].boardType) {
2823 case MOXA_BOARD_C218_ISA:
2824 case MOXA_BOARD_C218_PCI:
2825 if ((tmp = readw(baseAddr + C218_key)) != C218_KeyCode) {
2829 case MOXA_BOARD_CP204J:
2830 if ((tmp = readw(baseAddr + C218_key)) != CP204J_KeyCode) {
2835 if ((tmp = readw(baseAddr + C320_key)) != C320_KeyCode) {
2838 if ((tmp = readw(baseAddr + C320_status)) != STS_init) {
2845 static int moxaload320b(int cardno, unsigned char __user *tmp, int len)
2847 unsigned long baseAddr;
2850 if(len > sizeof(moxaBuff))
2852 if(copy_from_user(moxaBuff, tmp, len))
2854 baseAddr = moxaBaseAddr[cardno];
2855 writew(len - 7168 - 2, baseAddr + C320bapi_len);
2856 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
2857 for (i = 0; i < 7168; i++)
2858 writeb(moxaBuff[i], baseAddr + DynPage_addr + i);
2859 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
2860 for (i = 0; i < (len - 7168); i++)
2861 writeb(moxaBuff[i + 7168], baseAddr + DynPage_addr + i);
2865 static int moxaloadcode(int cardno, unsigned char __user *tmp, int len)
2867 unsigned long baseAddr, ofsAddr;
2868 int retval, port, i;
2870 if(copy_from_user(moxaBuff, tmp, len))
2872 baseAddr = moxaBaseAddr[cardno];
2873 switch (moxa_boards[cardno].boardType) {
2874 case MOXA_BOARD_C218_ISA:
2875 case MOXA_BOARD_C218_PCI:
2876 case MOXA_BOARD_CP204J:
2877 retval = moxaloadc218(cardno, baseAddr, len);
2880 port = cardno * MAX_PORTS_PER_BOARD;
2881 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2882 moxaChkPort[port] = 1;
2883 moxaCurBaud[port] = 9600L;
2884 moxaDCDState[port] = 0;
2885 moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
2886 ofsAddr = moxaTableAddr[port];
2887 writew(C218rx_mask, ofsAddr + RX_mask);
2888 writew(C218tx_mask, ofsAddr + TX_mask);
2889 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
2890 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
2892 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
2893 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
2898 retval = moxaloadc320(cardno, baseAddr, len,
2899 &moxa_boards[cardno].numPorts);
2902 port = cardno * MAX_PORTS_PER_BOARD;
2903 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2904 moxaChkPort[port] = 1;
2905 moxaCurBaud[port] = 9600L;
2906 moxaDCDState[port] = 0;
2907 moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
2908 ofsAddr = moxaTableAddr[port];
2909 if (moxa_boards[cardno].numPorts == 8) {
2910 writew(C320p8rx_mask, ofsAddr + RX_mask);
2911 writew(C320p8tx_mask, ofsAddr + TX_mask);
2912 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
2913 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
2914 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
2915 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
2917 } else if (moxa_boards[cardno].numPorts == 16) {
2918 writew(C320p16rx_mask, ofsAddr + RX_mask);
2919 writew(C320p16tx_mask, ofsAddr + TX_mask);
2920 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
2921 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
2922 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
2923 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
2925 } else if (moxa_boards[cardno].numPorts == 24) {
2926 writew(C320p24rx_mask, ofsAddr + RX_mask);
2927 writew(C320p24tx_mask, ofsAddr + TX_mask);
2928 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
2929 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
2930 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
2931 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2932 } else if (moxa_boards[cardno].numPorts == 32) {
2933 writew(C320p32rx_mask, ofsAddr + RX_mask);
2934 writew(C320p32tx_mask, ofsAddr + TX_mask);
2935 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
2936 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
2937 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
2938 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
2939 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2947 static int moxaloadc218(int cardno, unsigned long baseAddr, int len)
2950 int i, j, len1, len2;
2951 ushort usum, *ptr, keycode;
2953 if (moxa_boards[cardno].boardType == MOXA_BOARD_CP204J)
2954 keycode = CP204J_KeyCode;
2956 keycode = C218_KeyCode;
2959 ptr = (ushort *) moxaBuff;
2960 for (i = 0; i < len1; i++)
2967 len2 = (len1 > 2048) ? 2048 : len1;
2969 for (i = 0; i < len2 << 1; i++)
2970 writeb(moxaBuff[i + j], baseAddr + C218_LoadBuf + i);
2973 writew(len2, baseAddr + C218DLoad_len);
2974 writew(0, baseAddr + C218_key);
2975 for (i = 0; i < 100; i++) {
2976 if (readw(baseAddr + C218_key) == keycode)
2978 moxadelay(1); /* delay 10 ms */
2980 if (readw(baseAddr + C218_key) != keycode) {
2984 writew(0, baseAddr + C218DLoad_len);
2985 writew(usum, baseAddr + C218check_sum);
2986 writew(0, baseAddr + C218_key);
2987 for (i = 0; i < 100; i++) {
2988 if (readw(baseAddr + C218_key) == keycode)
2990 moxadelay(1); /* delay 10 ms */
2993 } while ((readb(baseAddr + C218chksum_ok) != 1) && (retry < 3));
2994 if (readb(baseAddr + C218chksum_ok) != 1) {
2997 writew(0, baseAddr + C218_key);
2998 for (i = 0; i < 100; i++) {
2999 if (readw(baseAddr + Magic_no) == Magic_code)
3001 moxadelay(1); /* delay 10 ms */
3003 if (readw(baseAddr + Magic_no) != Magic_code) {
3006 writew(1, baseAddr + Disable_IRQ);
3007 writew(0, baseAddr + Magic_no);
3008 for (i = 0; i < 100; i++) {
3009 if (readw(baseAddr + Magic_no) == Magic_code)
3011 moxadelay(1); /* delay 10 ms */
3013 if (readw(baseAddr + Magic_no) != Magic_code) {
3017 moxaIntNdx[cardno] = baseAddr + IRQindex;
3018 moxaIntPend[cardno] = baseAddr + IRQpending;
3019 moxaIntTable[cardno] = baseAddr + IRQtable;
3023 static int moxaloadc320(int cardno, unsigned long baseAddr, int len, int *numPorts)
3026 int i, j, wlen, len2, retry;
3031 uptr = (ushort *) moxaBuff;
3032 for (i = 0; i < wlen; i++)
3044 for (i = 0; i < len2; i++)
3045 writeb(moxaBuff[j + i], baseAddr + C320_LoadBuf + i);
3048 writew(len2, baseAddr + C320DLoad_len);
3049 writew(0, baseAddr + C320_key);
3050 for (i = 0; i < 10; i++) {
3051 if (readw(baseAddr + C320_key) == C320_KeyCode)
3055 if (readw(baseAddr + C320_key) != C320_KeyCode)
3058 writew(0, baseAddr + C320DLoad_len);
3059 writew(usum, baseAddr + C320check_sum);
3060 writew(0, baseAddr + C320_key);
3061 for (i = 0; i < 10; i++) {
3062 if (readw(baseAddr + C320_key) == C320_KeyCode)
3067 } while ((readb(baseAddr + C320chksum_ok) != 1) && (retry < 3));
3068 if (readb(baseAddr + C320chksum_ok) != 1)
3070 writew(0, baseAddr + C320_key);
3071 for (i = 0; i < 600; i++) {
3072 if (readw(baseAddr + Magic_no) == Magic_code)
3076 if (readw(baseAddr + Magic_no) != Magic_code)
3079 if (moxa_boards[cardno].busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
3080 writew(0x3800, baseAddr + TMS320_PORT1);
3081 writew(0x3900, baseAddr + TMS320_PORT2);
3082 writew(28499, baseAddr + TMS320_CLOCK);
3084 writew(0x3200, baseAddr + TMS320_PORT1);
3085 writew(0x3400, baseAddr + TMS320_PORT2);
3086 writew(19999, baseAddr + TMS320_CLOCK);
3088 writew(1, baseAddr + Disable_IRQ);
3089 writew(0, baseAddr + Magic_no);
3090 for (i = 0; i < 500; i++) {
3091 if (readw(baseAddr + Magic_no) == Magic_code)
3095 if (readw(baseAddr + Magic_no) != Magic_code)
3098 j = readw(baseAddr + Module_cnt);
3102 writew(j, baseAddr + Module_no);
3103 writew(0, baseAddr + Magic_no);
3104 for (i = 0; i < 600; i++) {
3105 if (readw(baseAddr + Magic_no) == Magic_code)
3109 if (readw(baseAddr + Magic_no) != Magic_code)
3112 moxaIntNdx[cardno] = baseAddr + IRQindex;
3113 moxaIntPend[cardno] = baseAddr + IRQpending;
3114 moxaIntTable[cardno] = baseAddr + IRQtable;
3118 long MoxaPortGetCurBaud(int port)
3121 if (moxaChkPort[port] == 0)
3123 return (moxaCurBaud[port]);
3126 static void MoxaSetFifo(int port, int enable)
3128 unsigned long ofsAddr = moxaTableAddr[port];
3131 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
3132 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
3134 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
3135 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
3140 int MoxaPortSetMode(int port, int databits, int stopbits, int parity)
3142 unsigned long ofsAddr;
3165 break; /* stop bits 1.5 */
3194 ofsAddr = moxaTableAddr[port];
3195 moxafunc(ofsAddr, FC_SetMode, val);
3199 int MoxaPortTxBufSize(int port)
3201 unsigned long ofsAddr;
3204 ofsAddr = moxaTableAddr[port];
3205 size = readw(ofsAddr + TX_mask);
3209 int MoxaPortRxBufSize(int port)
3211 unsigned long ofsAddr;
3214 ofsAddr = moxaTableAddr[port];
3215 size = readw(ofsAddr + RX_mask);
3219 int MoxaPortRxFree(int port)
3221 unsigned long ofsAddr;
3222 ushort rptr, wptr, mask;
3225 ofsAddr = moxaTableAddr[port];
3226 rptr = readw(ofsAddr + RXrptr);
3227 wptr = readw(ofsAddr + RXwptr);
3228 mask = readw(ofsAddr + RX_mask);
3229 len = mask - ((wptr - rptr) & mask);
3232 int MoxaPortGetBrkCnt(int port)
3234 return (moxaBreakCnt[port]);
3237 void MoxaPortSetXonXoff(int port, int xonValue, int xoffValue)
3239 unsigned long ofsAddr;
3241 ofsAddr = moxaTableAddr[port];
3242 writew(xonValue, ofsAddr + FuncArg);
3243 writew(xoffValue, ofsAddr + FuncArg1);
3244 writew(FC_SetXonXoff, ofsAddr + FuncCode);
3245 wait_finish(ofsAddr);
3248 int MoxaPortIsTxHold(int port)
3250 unsigned long ofsAddr;
3253 ofsAddr = moxaTableAddr[port];
3254 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
3255 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
3256 moxafunc(ofsAddr, FC_GetCCSR, 0);
3257 val = readw(ofsAddr + FuncArg);
3261 if (readw(ofsAddr + FlagStat) & Tx_flowOff)