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 put_to_user(arg1, arg2) put_user(arg1, (unsigned long *)arg2)
55 #define get_from_user(arg1, arg2) get_user(arg1, (unsigned int *)arg2)
57 #define MAX_BOARDS 4 /* Don't change this value */
58 #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
59 #define MAX_PORTS 128 /* Don't change this value */
62 * Define the Moxa PCI vendor and device IDs.
64 #define MOXA_BUS_TYPE_ISA 0
65 #define MOXA_BUS_TYPE_PCI 1
68 MOXA_BOARD_C218_PCI = 1,
75 static char *moxa_brdname[] =
77 "C218 Turbo PCI series",
78 "C218 Turbo ISA series",
79 "C320 Turbo PCI series",
80 "C320 Turbo ISA series",
85 static struct pci_device_id moxa_pcibrds[] = {
86 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218, PCI_ANY_ID, PCI_ANY_ID,
87 0, 0, MOXA_BOARD_C218_PCI },
88 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320, PCI_ANY_ID, PCI_ANY_ID,
89 0, 0, MOXA_BOARD_C320_PCI },
90 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J, PCI_ANY_ID, PCI_ANY_ID,
91 0, 0, MOXA_BOARD_CP204J },
94 MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
95 #endif /* CONFIG_PCI */
97 typedef struct _moxa_isa_board_conf {
100 unsigned long baseAddr;
101 } moxa_isa_board_conf;
103 static moxa_isa_board_conf moxa_isa_boards[] =
105 /* {MOXA_BOARD_C218_ISA,8,0xDC000}, */
108 typedef struct _moxa_pci_devinfo {
111 struct pci_dev *pdev;
114 typedef struct _moxa_board_conf {
117 unsigned long baseAddr;
119 moxa_pci_devinfo pciInfo;
122 static moxa_board_conf moxa_boards[MAX_BOARDS];
123 static void __iomem *moxaBaseAddr[MAX_BOARDS];
124 static int loadstat[MAX_BOARDS];
130 unsigned short closing_wait;
133 long event; /* long req'd for set_bit --RR */
135 unsigned long statusflags;
136 struct tty_struct *tty;
138 wait_queue_head_t open_wait;
139 wait_queue_head_t close_wait;
142 struct mxser_mstatus {
150 static struct mxser_mstatus GMStatus[MAX_PORTS];
153 #define TXSTOPPED 0x1
155 #define EMPTYWAIT 0x4
158 #define SERIAL_DO_RESTART
161 #define SERIAL_TYPE_NORMAL 1
163 #define WAKEUP_CHARS 256
165 #define PORTNO(x) ((x)->index)
167 static int verbose = 0;
168 static int ttymajor = MOXAMAJOR;
169 /* Variables for insmod */
171 static int baseaddr[4];
173 static int numports[4];
176 MODULE_AUTHOR("William Chen");
177 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
178 MODULE_LICENSE("GPL");
180 module_param_array(type, int, NULL, 0);
181 module_param_array(baseaddr, int, NULL, 0);
182 module_param_array(numports, int, NULL, 0);
184 module_param(ttymajor, int, 0);
185 module_param(verbose, bool, 0644);
190 static int moxa_open(struct tty_struct *, struct file *);
191 static void moxa_close(struct tty_struct *, struct file *);
192 static int moxa_write(struct tty_struct *, const unsigned char *, int);
193 static int moxa_write_room(struct tty_struct *);
194 static void moxa_flush_buffer(struct tty_struct *);
195 static int moxa_chars_in_buffer(struct tty_struct *);
196 static void moxa_flush_chars(struct tty_struct *);
197 static void moxa_put_char(struct tty_struct *, unsigned char);
198 static int moxa_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);
199 static void moxa_throttle(struct tty_struct *);
200 static void moxa_unthrottle(struct tty_struct *);
201 static void moxa_set_termios(struct tty_struct *, struct ktermios *);
202 static void moxa_stop(struct tty_struct *);
203 static void moxa_start(struct tty_struct *);
204 static void moxa_hangup(struct tty_struct *);
205 static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
206 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
207 unsigned int set, unsigned int clear);
208 static void moxa_poll(unsigned long);
209 static void set_tty_param(struct tty_struct *);
210 static int block_till_ready(struct tty_struct *, struct file *,
212 static void setup_empty_event(struct tty_struct *);
213 static void check_xmit_empty(unsigned long);
214 static void shut_down(struct moxa_str *);
215 static void receive_data(struct moxa_str *);
217 * moxa board interface functions:
219 static void MoxaDriverInit(void);
220 static int MoxaDriverIoctl(unsigned int, unsigned long, int);
221 static int MoxaDriverPoll(void);
222 static int MoxaPortsOfCard(int);
223 static int MoxaPortIsValid(int);
224 static void MoxaPortEnable(int);
225 static void MoxaPortDisable(int);
226 static long MoxaPortGetMaxBaud(int);
227 static long MoxaPortSetBaud(int, long);
228 static int MoxaPortSetTermio(int, struct ktermios *, speed_t);
229 static int MoxaPortGetLineOut(int, int *, int *);
230 static void MoxaPortLineCtrl(int, int, int);
231 static void MoxaPortFlowCtrl(int, int, int, int, int, int);
232 static int MoxaPortLineStatus(int);
233 static int MoxaPortDCDChange(int);
234 static int MoxaPortDCDON(int);
235 static void MoxaPortFlushData(int, int);
236 static int MoxaPortWriteData(int, unsigned char *, int);
237 static int MoxaPortReadData(int, struct tty_struct *tty);
238 static int MoxaPortTxQueue(int);
239 static int MoxaPortRxQueue(int);
240 static int MoxaPortTxFree(int);
241 static void MoxaPortTxDisable(int);
242 static void MoxaPortTxEnable(int);
243 static int MoxaPortResetBrkCnt(int);
244 static void MoxaPortSendBreak(int, int);
245 static int moxa_get_serial_info(struct moxa_str *, struct serial_struct __user *);
246 static int moxa_set_serial_info(struct moxa_str *, struct serial_struct __user *);
247 static void MoxaSetFifo(int port, int enable);
249 static const struct tty_operations moxa_ops = {
253 .write_room = moxa_write_room,
254 .flush_buffer = moxa_flush_buffer,
255 .chars_in_buffer = moxa_chars_in_buffer,
256 .flush_chars = moxa_flush_chars,
257 .put_char = moxa_put_char,
259 .throttle = moxa_throttle,
260 .unthrottle = moxa_unthrottle,
261 .set_termios = moxa_set_termios,
264 .hangup = moxa_hangup,
265 .tiocmget = moxa_tiocmget,
266 .tiocmset = moxa_tiocmset,
269 static struct tty_driver *moxaDriver;
270 static struct moxa_str moxaChannels[MAX_PORTS];
271 static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
272 static struct timer_list moxaEmptyTimer[MAX_PORTS];
273 static DEFINE_SPINLOCK(moxa_lock);
276 static int moxa_get_PCI_conf(struct pci_dev *p, int board_type, moxa_board_conf * board)
278 board->baseAddr = pci_resource_start (p, 2);
279 board->boardType = board_type;
280 switch (board_type) {
281 case MOXA_BOARD_C218_ISA:
282 case MOXA_BOARD_C218_PCI:
286 case MOXA_BOARD_CP204J:
293 board->busType = MOXA_BUS_TYPE_PCI;
294 board->pciInfo.busNum = p->bus->number;
295 board->pciInfo.devNum = p->devfn >> 3;
296 board->pciInfo.pdev = p;
297 /* don't lose the reference in the next pci_get_device iteration */
302 #endif /* CONFIG_PCI */
304 static int __init moxa_init(void)
309 printk(KERN_INFO "MOXA Intellio family driver version %s\n", MOXA_VERSION);
310 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
314 moxaDriver->owner = THIS_MODULE;
315 moxaDriver->name = "ttyMX";
316 moxaDriver->major = ttymajor;
317 moxaDriver->minor_start = 0;
318 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
319 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
320 moxaDriver->init_termios = tty_std_termios;
321 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
322 moxaDriver->init_termios.c_ispeed = 9600;
323 moxaDriver->init_termios.c_ospeed = 9600;
324 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
325 tty_set_operations(moxaDriver, &moxa_ops);
327 for (i = 0, ch = moxaChannels; i < MAX_PORTS; i++, ch++) {
328 ch->type = PORT_16550A;
330 ch->close_delay = 5 * HZ / 10;
331 ch->closing_wait = 30 * HZ;
332 ch->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
333 init_waitqueue_head(&ch->open_wait);
334 init_waitqueue_head(&ch->close_wait);
337 printk("Tty devices major number = %d\n", ttymajor);
339 if (tty_register_driver(moxaDriver)) {
340 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
341 put_tty_driver(moxaDriver);
344 for (i = 0; i < MAX_PORTS; i++)
345 setup_timer(&moxaEmptyTimer[i], check_xmit_empty,
346 (unsigned long)&moxaChannels[i]);
348 mod_timer(&moxaTimer, jiffies + HZ / 50);
350 /* Find the boards defined in source code */
352 for (i = 0; i < MAX_BOARDS; i++) {
353 if ((moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA) ||
354 (moxa_isa_boards[i].boardType == MOXA_BOARD_C320_ISA)) {
355 moxa_boards[numBoards].boardType = moxa_isa_boards[i].boardType;
356 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
357 moxa_boards[numBoards].numPorts = 8;
359 moxa_boards[numBoards].numPorts = moxa_isa_boards[i].numPorts;
360 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
361 moxa_boards[numBoards].baseAddr = moxa_isa_boards[i].baseAddr;
363 printk("Board %2d: %s board(baseAddr=%lx)\n",
365 moxa_brdname[moxa_boards[numBoards].boardType - 1],
366 moxa_boards[numBoards].baseAddr);
370 /* Find the boards defined form module args. */
372 for (i = 0; i < MAX_BOARDS; i++) {
373 if ((type[i] == MOXA_BOARD_C218_ISA) ||
374 (type[i] == MOXA_BOARD_C320_ISA)) {
376 printk("Board %2d: %s board(baseAddr=%lx)\n",
378 moxa_brdname[type[i] - 1],
379 (unsigned long) baseaddr[i]);
380 if (numBoards >= MAX_BOARDS) {
382 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
385 moxa_boards[numBoards].boardType = type[i];
386 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
387 moxa_boards[numBoards].numPorts = 8;
389 moxa_boards[numBoards].numPorts = numports[i];
390 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
391 moxa_boards[numBoards].baseAddr = baseaddr[i];
396 /* Find PCI boards here */
399 struct pci_dev *p = NULL;
400 int n = ARRAY_SIZE(moxa_pcibrds) - 1;
403 while ((p = pci_get_device(moxa_pcibrds[i].vendor, moxa_pcibrds[i].device, p))!=NULL)
405 if (pci_enable_device(p))
407 if (numBoards >= MAX_BOARDS) {
409 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
411 moxa_get_PCI_conf(p, moxa_pcibrds[i].driver_data,
412 &moxa_boards[numBoards]);
420 for (i = 0; i < numBoards; i++) {
421 moxaBaseAddr[i] = ioremap((unsigned long) moxa_boards[i].baseAddr, 0x4000);
427 static void __exit moxa_exit(void)
432 printk("Unloading module moxa ...\n");
434 del_timer(&moxaTimer);
436 for (i = 0; i < MAX_PORTS; i++)
437 del_timer(&moxaEmptyTimer[i]);
439 if (tty_unregister_driver(moxaDriver))
440 printk("Couldn't unregister MOXA Intellio family serial driver\n");
441 put_tty_driver(moxaDriver);
443 for (i = 0; i < MAX_BOARDS; i++) {
445 iounmap(moxaBaseAddr[i]);
446 if (moxa_boards[i].busType == MOXA_BUS_TYPE_PCI)
447 pci_dev_put(moxa_boards[i].pciInfo.pdev);
454 module_init(moxa_init);
455 module_exit(moxa_exit);
457 static int moxa_open(struct tty_struct *tty, struct file *filp)
464 if (port == MAX_PORTS) {
467 if (!MoxaPortIsValid(port)) {
468 tty->driver_data = NULL;
472 ch = &moxaChannels[port];
474 tty->driver_data = ch;
476 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
479 MoxaPortLineCtrl(ch->port, 1, 1);
480 MoxaPortEnable(ch->port);
481 ch->asyncflags |= ASYNC_INITIALIZED;
483 retval = block_till_ready(tty, filp, ch);
485 moxa_unthrottle(tty);
487 if (ch->type == PORT_16550A) {
488 MoxaSetFifo(ch->port, 1);
490 MoxaSetFifo(ch->port, 0);
496 static void moxa_close(struct tty_struct *tty, struct file *filp)
502 if (port == MAX_PORTS) {
505 if (!MoxaPortIsValid(port)) {
506 #ifdef SERIAL_DEBUG_CLOSE
507 printk("Invalid portno in moxa_close\n");
509 tty->driver_data = NULL;
512 if (tty->driver_data == NULL) {
515 if (tty_hung_up_p(filp)) {
518 ch = (struct moxa_str *) tty->driver_data;
520 if ((tty->count == 1) && (ch->count != 1)) {
521 printk("moxa_close: bad serial port count; tty->count is 1, "
522 "ch->count is %d\n", ch->count);
525 if (--ch->count < 0) {
526 printk("moxa_close: bad serial port count, device=%s\n",
533 ch->asyncflags |= ASYNC_CLOSING;
535 ch->cflag = tty->termios->c_cflag;
536 if (ch->asyncflags & ASYNC_INITIALIZED) {
537 setup_empty_event(tty);
538 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
539 del_timer(&moxaEmptyTimer[ch->port]);
542 MoxaPortFlushData(port, 2);
544 if (tty->driver->flush_buffer)
545 tty->driver->flush_buffer(tty);
546 tty_ldisc_flush(tty);
551 if (ch->blocked_open) {
552 if (ch->close_delay) {
553 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
555 wake_up_interruptible(&ch->open_wait);
557 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
558 wake_up_interruptible(&ch->close_wait);
561 static int moxa_write(struct tty_struct *tty,
562 const unsigned char *buf, int count)
568 ch = (struct moxa_str *) tty->driver_data;
573 spin_lock_irqsave(&moxa_lock, flags);
574 len = MoxaPortWriteData(port, (unsigned char *) buf, count);
575 spin_unlock_irqrestore(&moxa_lock, flags);
577 /*********************************************
578 if ( !(ch->statusflags & LOWWAIT) &&
579 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
580 ************************************************/
581 ch->statusflags |= LOWWAIT;
585 static int moxa_write_room(struct tty_struct *tty)
591 ch = (struct moxa_str *) tty->driver_data;
594 return (MoxaPortTxFree(ch->port));
597 static void moxa_flush_buffer(struct tty_struct *tty)
599 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
603 MoxaPortFlushData(ch->port, 1);
607 static int moxa_chars_in_buffer(struct tty_struct *tty)
610 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
613 * Sigh...I have to check if driver_data is NULL here, because
614 * if an open() fails, the TTY subsystem eventually calls
615 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
616 * routine. And since the open() failed, we return 0 here. TDJ
620 chars = MoxaPortTxQueue(ch->port);
623 * Make it possible to wakeup anything waiting for output
624 * in tty_ioctl.c, etc.
626 if (!(ch->statusflags & EMPTYWAIT))
627 setup_empty_event(tty);
632 static void moxa_flush_chars(struct tty_struct *tty)
635 * Don't think I need this, because this is called to empty the TX
636 * buffer for the 16450, 16550, etc.
640 static void moxa_put_char(struct tty_struct *tty, unsigned char c)
646 ch = (struct moxa_str *) tty->driver_data;
650 spin_lock_irqsave(&moxa_lock, flags);
651 MoxaPortWriteData(port, &c, 1);
652 spin_unlock_irqrestore(&moxa_lock, flags);
653 /************************************************
654 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
655 *************************************************/
656 ch->statusflags |= LOWWAIT;
659 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
661 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
663 int flag = 0, dtr, rts;
666 if ((port != MAX_PORTS) && (!ch))
669 MoxaPortGetLineOut(ch->port, &dtr, &rts);
674 dtr = MoxaPortLineStatus(ch->port);
684 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
685 unsigned int set, unsigned int clear)
687 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
692 if ((port != MAX_PORTS) && (!ch))
695 MoxaPortGetLineOut(ch->port, &dtr, &rts);
700 if (clear & TIOCM_RTS)
702 if (clear & TIOCM_DTR)
704 MoxaPortLineCtrl(ch->port, dtr, rts);
708 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
709 unsigned int cmd, unsigned long arg)
711 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
713 void __user *argp = (void __user *)arg;
717 if ((port != MAX_PORTS) && (!ch))
721 case TCSBRK: /* SVID version: non-zero arg --> no break */
722 retval = tty_check_change(tty);
725 setup_empty_event(tty);
726 tty_wait_until_sent(tty, 0);
728 MoxaPortSendBreak(ch->port, 0);
730 case TCSBRKP: /* support for POSIX tcsendbreak() */
731 retval = tty_check_change(tty);
734 setup_empty_event(tty);
735 tty_wait_until_sent(tty, 0);
736 MoxaPortSendBreak(ch->port, arg);
739 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) argp);
741 if(get_user(retval, (unsigned long __user *) argp))
744 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
747 ch->asyncflags &= ~ASYNC_CHECK_CD;
749 ch->asyncflags |= ASYNC_CHECK_CD;
752 return moxa_get_serial_info(ch, argp);
755 return moxa_set_serial_info(ch, argp);
757 retval = MoxaDriverIoctl(cmd, arg, port);
762 static void moxa_throttle(struct tty_struct *tty)
764 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
766 ch->statusflags |= THROTTLE;
769 static void moxa_unthrottle(struct tty_struct *tty)
771 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
773 ch->statusflags &= ~THROTTLE;
776 static void moxa_set_termios(struct tty_struct *tty,
777 struct ktermios *old_termios)
779 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
784 if (!(old_termios->c_cflag & CLOCAL) &&
785 (tty->termios->c_cflag & CLOCAL))
786 wake_up_interruptible(&ch->open_wait);
789 static void moxa_stop(struct tty_struct *tty)
791 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
795 MoxaPortTxDisable(ch->port);
796 ch->statusflags |= TXSTOPPED;
800 static void moxa_start(struct tty_struct *tty)
802 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
807 if (!(ch->statusflags & TXSTOPPED))
810 MoxaPortTxEnable(ch->port);
811 ch->statusflags &= ~TXSTOPPED;
814 static void moxa_hangup(struct tty_struct *tty)
816 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
818 moxa_flush_buffer(tty);
822 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
824 wake_up_interruptible(&ch->open_wait);
827 static void moxa_poll(unsigned long ignored)
831 struct tty_struct *tp;
834 del_timer(&moxaTimer);
836 if (MoxaDriverPoll() < 0) {
837 mod_timer(&moxaTimer, jiffies + HZ / 50);
840 for (card = 0; card < MAX_BOARDS; card++) {
841 if ((ports = MoxaPortsOfCard(card)) <= 0)
843 ch = &moxaChannels[card * MAX_PORTS_PER_BOARD];
844 for (i = 0; i < ports; i++, ch++) {
845 if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
847 if (!(ch->statusflags & THROTTLE) &&
848 (MoxaPortRxQueue(ch->port) > 0))
850 if ((tp = ch->tty) == 0)
852 if (ch->statusflags & LOWWAIT) {
853 if (MoxaPortTxQueue(ch->port) <= WAKEUP_CHARS) {
855 ch->statusflags &= ~LOWWAIT;
860 if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch->port) > 0)) {
861 tty_insert_flip_char(tp, 0, TTY_BREAK);
862 tty_schedule_flip(tp);
864 if (MoxaPortDCDChange(ch->port)) {
865 if (ch->asyncflags & ASYNC_CHECK_CD) {
866 if (MoxaPortDCDON(ch->port))
867 wake_up_interruptible(&ch->open_wait);
870 wake_up_interruptible(&ch->open_wait);
871 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
878 mod_timer(&moxaTimer, jiffies + HZ / 50);
881 /******************************************************************************/
883 static void set_tty_param(struct tty_struct *tty)
885 register struct ktermios *ts;
887 int rts, cts, txflow, rxflow, xany;
889 ch = (struct moxa_str *) tty->driver_data;
891 if (ts->c_cflag & CLOCAL)
892 ch->asyncflags &= ~ASYNC_CHECK_CD;
894 ch->asyncflags |= ASYNC_CHECK_CD;
895 rts = cts = txflow = rxflow = xany = 0;
896 if (ts->c_cflag & CRTSCTS)
898 if (ts->c_iflag & IXON)
900 if (ts->c_iflag & IXOFF)
902 if (ts->c_iflag & IXANY)
904 MoxaPortFlowCtrl(ch->port, rts, cts, txflow, rxflow, xany);
905 MoxaPortSetTermio(ch->port, ts, tty_get_baud_rate(tty));
908 static int block_till_ready(struct tty_struct *tty, struct file *filp,
911 DECLARE_WAITQUEUE(wait,current);
914 int do_clocal = C_CLOCAL(tty);
917 * If the device is in the middle of being closed, then block
918 * until it's done, and then try again.
920 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
921 if (ch->asyncflags & ASYNC_CLOSING)
922 interruptible_sleep_on(&ch->close_wait);
923 #ifdef SERIAL_DO_RESTART
924 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
927 return (-ERESTARTSYS);
933 * If non-blocking mode is set, then make the check up front
936 if (filp->f_flags & O_NONBLOCK) {
937 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
941 * Block waiting for the carrier detect and the line to become free
944 add_wait_queue(&ch->open_wait, &wait);
945 #ifdef SERIAL_DEBUG_OPEN
946 printk("block_til_ready before block: ttys%d, count = %d\n",
947 ch->line, ch->count);
949 spin_lock_irqsave(&moxa_lock, flags);
950 if (!tty_hung_up_p(filp))
953 spin_unlock_irqrestore(&moxa_lock, flags);
956 set_current_state(TASK_INTERRUPTIBLE);
957 if (tty_hung_up_p(filp) ||
958 !(ch->asyncflags & ASYNC_INITIALIZED)) {
959 #ifdef SERIAL_DO_RESTART
960 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
963 retval = -ERESTARTSYS;
969 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
970 MoxaPortDCDON(ch->port)))
973 if (signal_pending(current)) {
974 retval = -ERESTARTSYS;
979 set_current_state(TASK_RUNNING);
980 remove_wait_queue(&ch->open_wait, &wait);
982 spin_lock_irqsave(&moxa_lock, flags);
983 if (!tty_hung_up_p(filp))
986 spin_unlock_irqrestore(&moxa_lock, flags);
987 #ifdef SERIAL_DEBUG_OPEN
988 printk("block_til_ready after blocking: ttys%d, count = %d\n",
989 ch->line, ch->count);
993 /* FIXME: review to see if we need to use set_bit on these */
994 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
998 static void setup_empty_event(struct tty_struct *tty)
1000 struct moxa_str *ch = tty->driver_data;
1001 unsigned long flags;
1003 spin_lock_irqsave(&moxa_lock, flags);
1004 ch->statusflags |= EMPTYWAIT;
1005 mod_timer(&moxaEmptyTimer[ch->port], jiffies + HZ);
1006 spin_unlock_irqrestore(&moxa_lock, flags);
1009 static void check_xmit_empty(unsigned long data)
1011 struct moxa_str *ch;
1013 ch = (struct moxa_str *) data;
1014 del_timer(&moxaEmptyTimer[ch->port]);
1015 if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
1016 if (MoxaPortTxQueue(ch->port) == 0) {
1017 ch->statusflags &= ~EMPTYWAIT;
1018 tty_wakeup(ch->tty);
1021 mod_timer(&moxaEmptyTimer[ch->port], jiffies + HZ);
1023 ch->statusflags &= ~EMPTYWAIT;
1026 static void shut_down(struct moxa_str *ch)
1028 struct tty_struct *tp;
1030 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1035 MoxaPortDisable(ch->port);
1038 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1040 if (tp->termios->c_cflag & HUPCL)
1041 MoxaPortLineCtrl(ch->port, 0, 0);
1043 ch->asyncflags &= ~ASYNC_INITIALIZED;
1046 static void receive_data(struct moxa_str *ch)
1048 struct tty_struct *tp;
1049 struct ktermios *ts;
1050 unsigned long flags;
1056 /**************************************************
1057 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1058 *****************************************************/
1060 MoxaPortFlushData(ch->port, 0);
1063 spin_lock_irqsave(&moxa_lock, flags);
1064 MoxaPortReadData(ch->port, tp);
1065 spin_unlock_irqrestore(&moxa_lock, flags);
1066 tty_schedule_flip(tp);
1069 #define Magic_code 0x404
1072 * System Configuration
1075 * for C218 BIOS initialization
1077 #define C218_ConfBase 0x800
1078 #define C218_status (C218_ConfBase + 0) /* BIOS running status */
1079 #define C218_diag (C218_ConfBase + 2) /* diagnostic status */
1080 #define C218_key (C218_ConfBase + 4) /* WORD (0x218 for C218) */
1081 #define C218DLoad_len (C218_ConfBase + 6) /* WORD */
1082 #define C218check_sum (C218_ConfBase + 8) /* BYTE */
1083 #define C218chksum_ok (C218_ConfBase + 0x0a) /* BYTE (1:ok) */
1084 #define C218_TestRx (C218_ConfBase + 0x10) /* 8 bytes for 8 ports */
1085 #define C218_TestTx (C218_ConfBase + 0x18) /* 8 bytes for 8 ports */
1086 #define C218_RXerr (C218_ConfBase + 0x20) /* 8 bytes for 8 ports */
1087 #define C218_ErrFlag (C218_ConfBase + 0x28) /* 8 bytes for 8 ports */
1089 #define C218_LoadBuf 0x0F00
1090 #define C218_KeyCode 0x218
1091 #define CP204J_KeyCode 0x204
1094 * for C320 BIOS initialization
1096 #define C320_ConfBase 0x800
1097 #define C320_LoadBuf 0x0f00
1098 #define STS_init 0x05 /* for C320_status */
1100 #define C320_status C320_ConfBase + 0 /* BIOS running status */
1101 #define C320_diag C320_ConfBase + 2 /* diagnostic status */
1102 #define C320_key C320_ConfBase + 4 /* WORD (0320H for C320) */
1103 #define C320DLoad_len C320_ConfBase + 6 /* WORD */
1104 #define C320check_sum C320_ConfBase + 8 /* WORD */
1105 #define C320chksum_ok C320_ConfBase + 0x0a /* WORD (1:ok) */
1106 #define C320bapi_len C320_ConfBase + 0x0c /* WORD */
1107 #define C320UART_no C320_ConfBase + 0x0e /* WORD */
1109 #define C320_KeyCode 0x320
1111 #define FixPage_addr 0x0000 /* starting addr of static page */
1112 #define DynPage_addr 0x2000 /* starting addr of dynamic page */
1113 #define C218_start 0x3000 /* starting addr of C218 BIOS prg */
1114 #define Control_reg 0x1ff0 /* select page and reset control */
1115 #define HW_reset 0x80
1120 #define FC_CardReset 0x80
1121 #define FC_ChannelReset 1 /* C320 firmware not supported */
1122 #define FC_EnableCH 2
1123 #define FC_DisableCH 3
1124 #define FC_SetParam 4
1125 #define FC_SetMode 5
1126 #define FC_SetRate 6
1127 #define FC_LineControl 7
1128 #define FC_LineStatus 8
1129 #define FC_XmitControl 9
1130 #define FC_FlushQueue 10
1131 #define FC_SendBreak 11
1132 #define FC_StopBreak 12
1133 #define FC_LoopbackON 13
1134 #define FC_LoopbackOFF 14
1135 #define FC_ClrIrqTable 15
1136 #define FC_SendXon 16
1137 #define FC_SetTermIrq 17 /* C320 firmware not supported */
1138 #define FC_SetCntIrq 18 /* C320 firmware not supported */
1139 #define FC_SetBreakIrq 19
1140 #define FC_SetLineIrq 20
1141 #define FC_SetFlowCtl 21
1142 #define FC_GenIrq 22
1143 #define FC_InCD180 23
1144 #define FC_OutCD180 24
1145 #define FC_InUARTreg 23
1146 #define FC_OutUARTreg 24
1147 #define FC_SetXonXoff 25
1148 #define FC_OutCD180CCR 26
1149 #define FC_ExtIQueue 27
1150 #define FC_ExtOQueue 28
1151 #define FC_ClrLineIrq 29
1152 #define FC_HWFlowCtl 30
1153 #define FC_GetClockRate 35
1154 #define FC_SetBaud 36
1155 #define FC_SetDataMode 41
1156 #define FC_GetCCSR 43
1157 #define FC_GetDataError 45
1158 #define FC_RxControl 50
1159 #define FC_ImmSend 51
1160 #define FC_SetXonState 52
1161 #define FC_SetXoffState 53
1162 #define FC_SetRxFIFOTrig 54
1163 #define FC_SetTxFIFOCnt 55
1164 #define FC_UnixRate 56
1165 #define FC_UnixResetTimer 57
1167 #define RxFIFOTrig1 0
1168 #define RxFIFOTrig4 1
1169 #define RxFIFOTrig8 2
1170 #define RxFIFOTrig14 3
1175 #define DRAM_global 0
1176 #define INT_data (DRAM_global + 0)
1177 #define Config_base (DRAM_global + 0x108)
1179 #define IRQindex (INT_data + 0)
1180 #define IRQpending (INT_data + 4)
1181 #define IRQtable (INT_data + 8)
1186 #define IntrRx 0x01 /* receiver data O.K. */
1187 #define IntrTx 0x02 /* transmit buffer empty */
1188 #define IntrFunc 0x04 /* function complete */
1189 #define IntrBreak 0x08 /* received break */
1190 #define IntrLine 0x10 /* line status change
1192 #define IntrIntr 0x20 /* received INTR code */
1193 #define IntrQuit 0x40 /* received QUIT code */
1194 #define IntrEOF 0x80 /* received EOF code */
1196 #define IntrRxTrigger 0x100 /* rx data count reach tigger value */
1197 #define IntrTxTrigger 0x200 /* tx data count below trigger value */
1199 #define Magic_no (Config_base + 0)
1200 #define Card_model_no (Config_base + 2)
1201 #define Total_ports (Config_base + 4)
1202 #define Module_cnt (Config_base + 8)
1203 #define Module_no (Config_base + 10)
1204 #define Timer_10ms (Config_base + 14)
1205 #define Disable_IRQ (Config_base + 20)
1206 #define TMS320_PORT1 (Config_base + 22)
1207 #define TMS320_PORT2 (Config_base + 24)
1208 #define TMS320_CLOCK (Config_base + 26)
1211 * DATA BUFFER in DRAM
1213 #define Extern_table 0x400 /* Base address of the external table
1214 (24 words * 64) total 3K bytes
1215 (24 words * 128) total 6K bytes */
1216 #define Extern_size 0x60 /* 96 bytes */
1217 #define RXrptr 0x00 /* read pointer for RX buffer */
1218 #define RXwptr 0x02 /* write pointer for RX buffer */
1219 #define TXrptr 0x04 /* read pointer for TX buffer */
1220 #define TXwptr 0x06 /* write pointer for TX buffer */
1221 #define HostStat 0x08 /* IRQ flag and general flag */
1222 #define FlagStat 0x0A
1223 #define FlowControl 0x0C /* B7 B6 B5 B4 B3 B2 B1 B0 */
1224 /* x x x x | | | | */
1225 /* | | | + CTS flow */
1226 /* | | +--- RTS flow */
1227 /* | +------ TX Xon/Xoff */
1228 /* +--------- RX Xon/Xoff */
1229 #define Break_cnt 0x0E /* received break count */
1230 #define CD180TXirq 0x10 /* if non-0: enable TX irq */
1231 #define RX_mask 0x12
1232 #define TX_mask 0x14
1233 #define Ofs_rxb 0x16
1234 #define Ofs_txb 0x18
1235 #define Page_rxb 0x1A
1236 #define Page_txb 0x1C
1237 #define EndPage_rxb 0x1E
1238 #define EndPage_txb 0x20
1239 #define Data_error 0x22
1240 #define RxTrigger 0x28
1241 #define TxTrigger 0x2a
1243 #define rRXwptr 0x34
1244 #define Low_water 0x36
1246 #define FuncCode 0x40
1247 #define FuncArg 0x42
1248 #define FuncArg1 0x44
1250 #define C218rx_size 0x2000 /* 8K bytes */
1251 #define C218tx_size 0x8000 /* 32K bytes */
1253 #define C218rx_mask (C218rx_size - 1)
1254 #define C218tx_mask (C218tx_size - 1)
1256 #define C320p8rx_size 0x2000
1257 #define C320p8tx_size 0x8000
1258 #define C320p8rx_mask (C320p8rx_size - 1)
1259 #define C320p8tx_mask (C320p8tx_size - 1)
1261 #define C320p16rx_size 0x2000
1262 #define C320p16tx_size 0x4000
1263 #define C320p16rx_mask (C320p16rx_size - 1)
1264 #define C320p16tx_mask (C320p16tx_size - 1)
1266 #define C320p24rx_size 0x2000
1267 #define C320p24tx_size 0x2000
1268 #define C320p24rx_mask (C320p24rx_size - 1)
1269 #define C320p24tx_mask (C320p24tx_size - 1)
1271 #define C320p32rx_size 0x1000
1272 #define C320p32tx_size 0x1000
1273 #define C320p32rx_mask (C320p32rx_size - 1)
1274 #define C320p32tx_mask (C320p32tx_size - 1)
1276 #define Page_size 0x2000
1277 #define Page_mask (Page_size - 1)
1278 #define C218rx_spage 3
1279 #define C218tx_spage 4
1280 #define C218rx_pageno 1
1281 #define C218tx_pageno 4
1282 #define C218buf_pageno 5
1284 #define C320p8rx_spage 3
1285 #define C320p8tx_spage 4
1286 #define C320p8rx_pgno 1
1287 #define C320p8tx_pgno 4
1288 #define C320p8buf_pgno 5
1290 #define C320p16rx_spage 3
1291 #define C320p16tx_spage 4
1292 #define C320p16rx_pgno 1
1293 #define C320p16tx_pgno 2
1294 #define C320p16buf_pgno 3
1296 #define C320p24rx_spage 3
1297 #define C320p24tx_spage 4
1298 #define C320p24rx_pgno 1
1299 #define C320p24tx_pgno 1
1300 #define C320p24buf_pgno 2
1302 #define C320p32rx_spage 3
1303 #define C320p32tx_ofs C320p32rx_size
1304 #define C320p32tx_spage 3
1305 #define C320p32buf_pgno 1
1310 #define WakeupRx 0x01
1311 #define WakeupTx 0x02
1312 #define WakeupBreak 0x08
1313 #define WakeupLine 0x10
1314 #define WakeupIntr 0x20
1315 #define WakeupQuit 0x40
1316 #define WakeupEOF 0x80 /* used in VTIME control */
1317 #define WakeupRxTrigger 0x100
1318 #define WakeupTxTrigger 0x200
1322 #define Rx_over 0x01
1323 #define Xoff_state 0x02
1324 #define Tx_flowOff 0x04
1325 #define Tx_enable 0x08
1326 #define CTS_state 0x10
1327 #define DSR_state 0x20
1328 #define DCD_state 0x80
1332 #define CTS_FlowCtl 1
1333 #define RTS_FlowCtl 2
1334 #define Tx_FlowCtl 4
1335 #define Rx_FlowCtl 8
1336 #define IXM_IXANY 0x10
1338 #define LowWater 128
1346 /* mode definition */
1352 #define MX_STOP1 0x00
1353 #define MX_STOP15 0x04
1354 #define MX_STOP2 0x08
1356 #define MX_PARNONE 0x00
1357 #define MX_PAREVEN 0x40
1358 #define MX_PARODD 0xC0
1363 #define QueryPort MAX_PORTS
1369 int rxcnt[MAX_PORTS];
1370 int txcnt[MAX_PORTS];
1372 typedef struct mon_str mon_st;
1374 #define DCD_changed 0x01
1375 #define DCD_oldstate 0x80
1377 static unsigned char moxaBuff[10240];
1378 static void __iomem *moxaIntNdx[MAX_BOARDS];
1379 static void __iomem *moxaIntPend[MAX_BOARDS];
1380 static void __iomem *moxaIntTable[MAX_BOARDS];
1381 static char moxaChkPort[MAX_PORTS];
1382 static char moxaLineCtrl[MAX_PORTS];
1383 static void __iomem *moxaTableAddr[MAX_PORTS];
1384 static long moxaCurBaud[MAX_PORTS];
1385 static char moxaDCDState[MAX_PORTS];
1386 static char moxaLowChkFlag[MAX_PORTS];
1387 static int moxaLowWaterChk;
1388 static int moxaCard;
1389 static mon_st moxaLog;
1390 static int moxaFuncTout = HZ / 2;
1391 static ushort moxaBreakCnt[MAX_PORTS];
1393 static void moxadelay(int);
1394 static void moxafunc(void __iomem *, int, ushort);
1395 static void wait_finish(void __iomem *);
1396 static void low_water_check(void __iomem *);
1397 static int moxaloadbios(int, unsigned char __user *, int);
1398 static int moxafindcard(int);
1399 static int moxaload320b(int, unsigned char __user *, int);
1400 static int moxaloadcode(int, unsigned char __user *, int);
1401 static int moxaloadc218(int, void __iomem *, int);
1402 static int moxaloadc320(int, void __iomem *, int, int *);
1404 /*****************************************************************************
1405 * Driver level functions: *
1406 * 1. MoxaDriverInit(void); *
1407 * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
1408 * 3. MoxaDriverPoll(void); *
1409 *****************************************************************************/
1410 void MoxaDriverInit(void)
1414 moxaFuncTout = HZ / 2; /* 500 mini-seconds */
1417 moxaLowWaterChk = 0;
1418 for (i = 0; i < MAX_PORTS; i++) {
1420 moxaLowChkFlag[i] = 0;
1421 moxaLineCtrl[i] = 0;
1422 moxaLog.rxcnt[i] = 0;
1423 moxaLog.txcnt[i] = 0;
1428 #define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1429 #define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
1430 #define MOXA_INIT_DRIVER (MOXA + 6) /* moxaCard=0 */
1431 #define MOXA_LOAD_BIOS (MOXA + 9) /* download BIOS */
1432 #define MOXA_FIND_BOARD (MOXA + 10) /* Check if MOXA card exist? */
1433 #define MOXA_LOAD_C320B (MOXA + 11) /* download 320B firmware */
1434 #define MOXA_LOAD_CODE (MOXA + 12) /* download firmware */
1435 #define MOXA_GETDATACOUNT (MOXA + 23)
1436 #define MOXA_GET_IOQUEUE (MOXA + 27)
1437 #define MOXA_FLUSH_QUEUE (MOXA + 28)
1438 #define MOXA_GET_CONF (MOXA + 35) /* configuration */
1439 #define MOXA_GET_MAJOR (MOXA + 63)
1440 #define MOXA_GET_CUMAJOR (MOXA + 64)
1441 #define MOXA_GETMSTATUS (MOXA + 65)
1455 static struct moxaq_str temp_queue[MAX_PORTS];
1456 static struct dl_str dltmp;
1458 void MoxaPortFlushData(int port, int mode)
1460 void __iomem *ofsAddr;
1461 if ((mode < 0) || (mode > 2))
1463 ofsAddr = moxaTableAddr[port];
1464 moxafunc(ofsAddr, FC_FlushQueue, mode);
1466 moxaLowChkFlag[port] = 0;
1467 low_water_check(ofsAddr);
1471 int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port)
1475 int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
1476 void __user *argp = (void __user *)arg;
1478 if (port == QueryPort) {
1479 if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_INIT_DRIVER) &&
1480 (cmd != MOXA_LOAD_BIOS) && (cmd != MOXA_FIND_BOARD) && (cmd != MOXA_LOAD_C320B) &&
1481 (cmd != MOXA_LOAD_CODE) && (cmd != MOXA_GETDATACOUNT) &&
1482 (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
1483 (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
1488 if(copy_to_user(argp, &moxa_boards, MAX_BOARDS * sizeof(moxa_board_conf)))
1491 case MOXA_INIT_DRIVER:
1492 if ((int) arg == 0x404)
1495 case MOXA_GETDATACOUNT:
1496 moxaLog.tick = jiffies;
1497 if(copy_to_user(argp, &moxaLog, sizeof(mon_st)))
1500 case MOXA_FLUSH_QUEUE:
1501 MoxaPortFlushData(port, arg);
1503 case MOXA_GET_IOQUEUE:
1504 for (i = 0; i < MAX_PORTS; i++) {
1505 if (moxaChkPort[i]) {
1506 temp_queue[i].inq = MoxaPortRxQueue(i);
1507 temp_queue[i].outq = MoxaPortTxQueue(i);
1510 if(copy_to_user(argp, temp_queue, sizeof(struct moxaq_str) * MAX_PORTS))
1513 case MOXA_GET_OQUEUE:
1514 i = MoxaPortTxQueue(port);
1515 return put_user(i, (unsigned long __user *)argp);
1516 case MOXA_GET_IQUEUE:
1517 i = MoxaPortRxQueue(port);
1518 return put_user(i, (unsigned long __user *)argp);
1519 case MOXA_GET_MAJOR:
1520 if(copy_to_user(argp, &ttymajor, sizeof(int)))
1523 case MOXA_GET_CUMAJOR:
1525 if(copy_to_user(argp, &i, sizeof(int)))
1528 case MOXA_GETMSTATUS:
1529 for (i = 0; i < MAX_PORTS; i++) {
1531 GMStatus[i].dcd = 0;
1532 GMStatus[i].dsr = 0;
1533 GMStatus[i].cts = 0;
1534 if (!moxaChkPort[i]) {
1537 status = MoxaPortLineStatus(moxaChannels[i].port);
1539 GMStatus[i].cts = 1;
1541 GMStatus[i].dsr = 1;
1543 GMStatus[i].dcd = 1;
1546 if (!moxaChannels[i].tty || !moxaChannels[i].tty->termios)
1547 GMStatus[i].cflag = moxaChannels[i].cflag;
1549 GMStatus[i].cflag = moxaChannels[i].tty->termios->c_cflag;
1551 if(copy_to_user(argp, GMStatus, sizeof(struct mxser_mstatus) * MAX_PORTS))
1555 return (-ENOIOCTLCMD);
1556 case MOXA_LOAD_BIOS:
1557 case MOXA_FIND_BOARD:
1558 case MOXA_LOAD_C320B:
1559 case MOXA_LOAD_CODE:
1560 if (!capable(CAP_SYS_RAWIO))
1565 if(copy_from_user(&dltmp, argp, sizeof(struct dl_str)))
1567 if(dltmp.cardno < 0 || dltmp.cardno >= MAX_BOARDS)
1572 case MOXA_LOAD_BIOS:
1573 i = moxaloadbios(dltmp.cardno, dltmp.buf, dltmp.len);
1575 case MOXA_FIND_BOARD:
1576 return moxafindcard(dltmp.cardno);
1577 case MOXA_LOAD_C320B:
1578 moxaload320b(dltmp.cardno, dltmp.buf, dltmp.len);
1579 default: /* to keep gcc happy */
1581 case MOXA_LOAD_CODE:
1582 i = moxaloadcode(dltmp.cardno, dltmp.buf, dltmp.len);
1590 int MoxaDriverPoll(void)
1592 register ushort temp;
1594 void __iomem *ofsAddr;
1600 for (card = 0; card < MAX_BOARDS; card++) {
1601 if (loadstat[card] == 0)
1603 if ((ports = moxa_boards[card].numPorts) == 0)
1605 if (readb(moxaIntPend[card]) == 0xff) {
1606 ip = moxaIntTable[card] + readb(moxaIntNdx[card]);
1607 p = card * MAX_PORTS_PER_BOARD;
1609 for (port = 0; port < ports; port += 2, p++) {
1610 if ((temp = readw(ip + port)) != 0) {
1611 writew(0, ip + port);
1612 ofsAddr = moxaTableAddr[p];
1614 writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
1615 if (temp & IntrBreak) {
1618 if (temp & IntrLine) {
1619 if (readb(ofsAddr + FlagStat) & DCD_state) {
1620 if ((moxaDCDState[p] & DCD_oldstate) == 0)
1621 moxaDCDState[p] = (DCD_oldstate |
1624 if (moxaDCDState[p] & DCD_oldstate)
1625 moxaDCDState[p] = DCD_changed;
1630 writeb(0, moxaIntPend[card]);
1632 if (moxaLowWaterChk) {
1633 p = card * MAX_PORTS_PER_BOARD;
1634 for (port = 0; port < ports; port++, p++) {
1635 if (moxaLowChkFlag[p]) {
1636 moxaLowChkFlag[p] = 0;
1637 ofsAddr = moxaTableAddr[p];
1638 low_water_check(ofsAddr);
1643 moxaLowWaterChk = 0;
1647 /*****************************************************************************
1648 * Card level function: *
1649 * 1. MoxaPortsOfCard(int cardno); *
1650 *****************************************************************************/
1651 int MoxaPortsOfCard(int cardno)
1654 if (moxa_boards[cardno].boardType == 0)
1656 return (moxa_boards[cardno].numPorts);
1659 /*****************************************************************************
1660 * Port level functions: *
1661 * 1. MoxaPortIsValid(int port); *
1662 * 2. MoxaPortEnable(int port); *
1663 * 3. MoxaPortDisable(int port); *
1664 * 4. MoxaPortGetMaxBaud(int port); *
1665 * 6. MoxaPortSetBaud(int port, long baud); *
1666 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1667 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1668 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1669 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1670 * 12. MoxaPortLineStatus(int port); *
1671 * 13. MoxaPortDCDChange(int port); *
1672 * 14. MoxaPortDCDON(int port); *
1673 * 15. MoxaPortFlushData(int port, int mode); *
1674 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1675 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
1676 * 20. MoxaPortTxQueue(int port); *
1677 * 21. MoxaPortTxFree(int port); *
1678 * 22. MoxaPortRxQueue(int port); *
1679 * 24. MoxaPortTxDisable(int port); *
1680 * 25. MoxaPortTxEnable(int port); *
1681 * 27. MoxaPortResetBrkCnt(int port); *
1682 * 30. MoxaPortSendBreak(int port, int ticks); *
1683 *****************************************************************************/
1685 * Moxa Port Number Description:
1687 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1688 * the port number using in MOXA driver functions will be 0 to 31 for
1689 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1690 * to 127 for fourth. For example, if you setup three MOXA boards,
1691 * first board is C218, second board is C320-16 and third board is
1692 * C320-32. The port number of first board (C218 - 8 ports) is from
1693 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1694 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1695 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1696 * 127 will be invalid.
1699 * Moxa Functions Description:
1701 * Function 1: Driver initialization routine, this routine must be
1702 * called when initialized driver.
1704 * void MoxaDriverInit();
1707 * Function 2: Moxa driver private IOCTL command processing.
1709 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1711 * unsigned int cmd : IOCTL command
1712 * unsigned long arg : IOCTL argument
1713 * int port : port number (0 - 127)
1720 * Function 3: Moxa driver polling process routine.
1722 * int MoxaDriverPoll(void);
1724 * return: 0 ; polling O.K.
1725 * -1 : no any Moxa card.
1728 * Function 4: Get the ports of this card.
1730 * int MoxaPortsOfCard(int cardno);
1732 * int cardno : card number (0 - 3)
1734 * return: 0 : this card is invalid
1738 * Function 5: Check this port is valid or invalid
1740 * int MoxaPortIsValid(int port);
1741 * int port : port number (0 - 127, ref port description)
1743 * return: 0 : this port is invalid
1744 * 1 : this port is valid
1747 * Function 6: Enable this port to start Tx/Rx data.
1749 * void MoxaPortEnable(int port);
1750 * int port : port number (0 - 127)
1753 * Function 7: Disable this port
1755 * void MoxaPortDisable(int port);
1756 * int port : port number (0 - 127)
1759 * Function 8: Get the maximun available baud rate of this port.
1761 * long MoxaPortGetMaxBaud(int port);
1762 * int port : port number (0 - 127)
1764 * return: 0 : this port is invalid
1765 * 38400/57600/115200 bps
1768 * Function 10: Setting baud rate of this port.
1770 * long MoxaPortSetBaud(int port, long baud);
1771 * int port : port number (0 - 127)
1772 * long baud : baud rate (50 - 115200)
1774 * return: 0 : this port is invalid or baud < 50
1775 * 50 - 115200 : the real baud rate set to the port, if
1776 * the argument baud is large than maximun
1777 * available baud rate, the real setting
1778 * baud rate will be the maximun baud rate.
1781 * Function 12: Configure the port.
1783 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1784 * int port : port number (0 - 127)
1785 * struct ktermios * termio : termio structure pointer
1786 * speed_t baud : baud rate
1788 * return: -1 : this port is invalid or termio == NULL
1792 * Function 13: Get the DTR/RTS state of this port.
1794 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1795 * int port : port number (0 - 127)
1796 * int * dtrState : pointer to INT to receive the current DTR
1797 * state. (if NULL, this function will not
1798 * write to this address)
1799 * int * rtsState : pointer to INT to receive the current RTS
1800 * state. (if NULL, this function will not
1801 * write to this address)
1803 * return: -1 : this port is invalid
1807 * Function 14: Setting the DTR/RTS output state of this port.
1809 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1810 * int port : port number (0 - 127)
1811 * int dtrState : DTR output state (0: off, 1: on)
1812 * int rtsState : RTS output state (0: off, 1: on)
1815 * Function 15: Setting the flow control of this port.
1817 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1818 * int txFlow,int xany);
1819 * int port : port number (0 - 127)
1820 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1821 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1822 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1823 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1824 * int xany : S/W XANY flow control (0: no, 1: yes)
1827 * Function 16: Get ths line status of this port
1829 * int MoxaPortLineStatus(int port);
1830 * int port : port number (0 - 127)
1832 * return: Bit 0 - CTS state (0: off, 1: on)
1833 * Bit 1 - DSR state (0: off, 1: on)
1834 * Bit 2 - DCD state (0: off, 1: on)
1837 * Function 17: Check the DCD state has changed since the last read
1840 * int MoxaPortDCDChange(int port);
1841 * int port : port number (0 - 127)
1843 * return: 0 : no changed
1844 * 1 : DCD has changed
1847 * Function 18: Check ths current DCD state is ON or not.
1849 * int MoxaPortDCDON(int port);
1850 * int port : port number (0 - 127)
1852 * return: 0 : DCD off
1856 * Function 19: Flush the Rx/Tx buffer data of this port.
1858 * void MoxaPortFlushData(int port, int mode);
1859 * int port : port number (0 - 127)
1861 * 0 : flush the Rx buffer
1862 * 1 : flush the Tx buffer
1863 * 2 : flush the Rx and Tx buffer
1866 * Function 20: Write data.
1868 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1869 * int port : port number (0 - 127)
1870 * unsigned char * buffer : pointer to write data buffer.
1871 * int length : write data length
1873 * return: 0 - length : real write data length
1876 * Function 21: Read data.
1878 * int MoxaPortReadData(int port, struct tty_struct *tty);
1879 * int port : port number (0 - 127)
1880 * struct tty_struct *tty : tty for data
1882 * return: 0 - length : real read data length
1885 * Function 24: Get the Tx buffer current queued data bytes
1887 * int MoxaPortTxQueue(int port);
1888 * int port : port number (0 - 127)
1890 * return: .. : Tx buffer current queued data bytes
1893 * Function 25: Get the Tx buffer current free space
1895 * int MoxaPortTxFree(int port);
1896 * int port : port number (0 - 127)
1898 * return: .. : Tx buffer current free space
1901 * Function 26: Get the Rx buffer current queued data bytes
1903 * int MoxaPortRxQueue(int port);
1904 * int port : port number (0 - 127)
1906 * return: .. : Rx buffer current queued data bytes
1909 * Function 28: Disable port data transmission.
1911 * void MoxaPortTxDisable(int port);
1912 * int port : port number (0 - 127)
1915 * Function 29: Enable port data transmission.
1917 * void MoxaPortTxEnable(int port);
1918 * int port : port number (0 - 127)
1921 * Function 31: Get the received BREAK signal count and reset it.
1923 * int MoxaPortResetBrkCnt(int port);
1924 * int port : port number (0 - 127)
1926 * return: 0 - .. : BREAK signal count
1929 * Function 34: Send out a BREAK signal.
1931 * void MoxaPortSendBreak(int port, int ms100);
1932 * int port : port number (0 - 127)
1933 * int ms100 : break signal time interval.
1934 * unit: 100 mini-second. if ms100 == 0, it will
1935 * send out a about 250 ms BREAK signal.
1938 int MoxaPortIsValid(int port)
1943 if (moxaChkPort[port] == 0)
1948 void MoxaPortEnable(int port)
1950 void __iomem *ofsAddr;
1951 int MoxaPortLineStatus(int);
1952 short lowwater = 512;
1954 ofsAddr = moxaTableAddr[port];
1955 writew(lowwater, ofsAddr + Low_water);
1956 moxaBreakCnt[port] = 0;
1957 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
1958 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
1959 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1961 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
1964 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1965 moxafunc(ofsAddr, FC_FlushQueue, 2);
1967 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1968 MoxaPortLineStatus(port);
1971 void MoxaPortDisable(int port)
1973 void __iomem *ofsAddr = moxaTableAddr[port];
1975 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1976 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1977 writew(0, ofsAddr + HostStat);
1978 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1981 long MoxaPortGetMaxBaud(int port)
1983 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
1984 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI))
1991 long MoxaPortSetBaud(int port, long baud)
1993 void __iomem *ofsAddr;
1997 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
1999 ofsAddr = moxaTableAddr[port];
2003 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
2004 else if (max == 57600L)
2005 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
2007 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
2009 moxafunc(ofsAddr, FC_SetBaud, val);
2011 moxaCurBaud[port] = baud;
2015 int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud)
2017 void __iomem *ofsAddr;
2021 if (moxaChkPort[port] == 0 || termio == 0)
2023 ofsAddr = moxaTableAddr[port];
2024 cflag = termio->c_cflag; /* termio->c_cflag */
2026 mode = termio->c_cflag & CSIZE;
2029 else if (mode == CS6)
2031 else if (mode == CS7)
2033 else if (mode == CS8)
2036 if (termio->c_cflag & CSTOPB) {
2044 if (termio->c_cflag & PARENB) {
2045 if (termio->c_cflag & PARODD)
2052 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
2054 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2055 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2056 if (baud >= 921600L)
2059 MoxaPortSetBaud(port, baud);
2061 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
2062 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
2063 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
2064 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
2065 wait_finish(ofsAddr);
2071 int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState)
2074 if (!MoxaPortIsValid(port))
2077 if (moxaLineCtrl[port] & DTR_ON)
2083 if (moxaLineCtrl[port] & RTS_ON)
2091 void MoxaPortLineCtrl(int port, int dtr, int rts)
2093 void __iomem *ofsAddr;
2096 ofsAddr = moxaTableAddr[port];
2102 moxaLineCtrl[port] = mode;
2103 moxafunc(ofsAddr, FC_LineControl, mode);
2106 void MoxaPortFlowCtrl(int port, int rts, int cts, int txflow, int rxflow, int txany)
2108 void __iomem *ofsAddr;
2111 ofsAddr = moxaTableAddr[port];
2114 mode |= RTS_FlowCtl;
2116 mode |= CTS_FlowCtl;
2123 moxafunc(ofsAddr, FC_SetFlowCtl, mode);
2126 int MoxaPortLineStatus(int port)
2128 void __iomem *ofsAddr;
2131 ofsAddr = moxaTableAddr[port];
2132 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2133 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2134 moxafunc(ofsAddr, FC_LineStatus, 0);
2135 val = readw(ofsAddr + FuncArg);
2137 val = readw(ofsAddr + FlagStat) >> 4;
2142 if ((moxaDCDState[port] & DCD_oldstate) == 0)
2143 moxaDCDState[port] = (DCD_oldstate | DCD_changed);
2145 if (moxaDCDState[port] & DCD_oldstate)
2146 moxaDCDState[port] = DCD_changed;
2152 int MoxaPortDCDChange(int port)
2156 if (moxaChkPort[port] == 0)
2158 n = moxaDCDState[port];
2159 moxaDCDState[port] &= ~DCD_changed;
2164 int MoxaPortDCDON(int port)
2168 if (moxaChkPort[port] == 0)
2170 if (moxaDCDState[port] & DCD_oldstate)
2177 int MoxaPortWriteData(int port, unsigned char * buffer, int len)
2182 ushort head, tx_mask, spage, epage;
2183 ushort pageno, pageofs, bufhead;
2184 void __iomem *baseAddr, *ofsAddr, *ofs;
2186 ofsAddr = moxaTableAddr[port];
2187 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2188 tx_mask = readw(ofsAddr + TX_mask);
2189 spage = readw(ofsAddr + Page_txb);
2190 epage = readw(ofsAddr + EndPage_txb);
2191 tail = readw(ofsAddr + TXwptr);
2192 head = readw(ofsAddr + TXrptr);
2193 c = (head > tail) ? (head - tail - 1)
2194 : (head - tail + tx_mask);
2197 moxaLog.txcnt[port] += c;
2199 if (spage == epage) {
2200 bufhead = readw(ofsAddr + Ofs_txb);
2201 writew(spage, baseAddr + Control_reg);
2204 len = head - tail - 1;
2206 len = tx_mask + 1 - tail;
2207 len = (c > len) ? len : c;
2208 ofs = baseAddr + DynPage_addr + bufhead + tail;
2209 for (i = 0; i < len; i++)
2210 writeb(*buffer++, ofs + i);
2211 tail = (tail + len) & tx_mask;
2214 writew(tail, ofsAddr + TXwptr);
2217 pageno = spage + (tail >> 13);
2218 pageofs = tail & Page_mask;
2220 cnt = Page_size - pageofs;
2224 writeb(pageno, baseAddr + Control_reg);
2225 ofs = baseAddr + DynPage_addr + pageofs;
2226 for (i = 0; i < cnt; i++)
2227 writeb(*buffer++, ofs + i);
2229 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2232 if (++pageno == epage)
2237 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2241 int MoxaPortReadData(int port, struct tty_struct *tty)
2243 register ushort head, pageofs;
2244 int i, count, cnt, len, total, remain;
2245 ushort tail, rx_mask, spage, epage;
2246 ushort pageno, bufhead;
2247 void __iomem *baseAddr, *ofsAddr, *ofs;
2249 ofsAddr = moxaTableAddr[port];
2250 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2251 head = readw(ofsAddr + RXrptr);
2252 tail = readw(ofsAddr + RXwptr);
2253 rx_mask = readw(ofsAddr + RX_mask);
2254 spage = readw(ofsAddr + Page_rxb);
2255 epage = readw(ofsAddr + EndPage_rxb);
2256 count = (tail >= head) ? (tail - head)
2257 : (tail - head + rx_mask + 1);
2262 remain = count - total;
2263 moxaLog.rxcnt[port] += total;
2265 if (spage == epage) {
2266 bufhead = readw(ofsAddr + Ofs_rxb);
2267 writew(spage, baseAddr + Control_reg);
2272 len = rx_mask + 1 - head;
2273 len = (count > len) ? len : count;
2274 ofs = baseAddr + DynPage_addr + bufhead + head;
2275 for (i = 0; i < len; i++)
2276 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2277 head = (head + len) & rx_mask;
2280 writew(head, ofsAddr + RXrptr);
2283 pageno = spage + (head >> 13);
2284 pageofs = head & Page_mask;
2286 cnt = Page_size - pageofs;
2290 writew(pageno, baseAddr + Control_reg);
2291 ofs = baseAddr + DynPage_addr + pageofs;
2292 for (i = 0; i < cnt; i++)
2293 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2295 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2298 if (++pageno == epage)
2303 if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2304 moxaLowWaterChk = 1;
2305 moxaLowChkFlag[port] = 1;
2311 int MoxaPortTxQueue(int port)
2313 void __iomem *ofsAddr;
2314 ushort rptr, wptr, mask;
2317 ofsAddr = moxaTableAddr[port];
2318 rptr = readw(ofsAddr + TXrptr);
2319 wptr = readw(ofsAddr + TXwptr);
2320 mask = readw(ofsAddr + TX_mask);
2321 len = (wptr - rptr) & mask;
2325 int MoxaPortTxFree(int port)
2327 void __iomem *ofsAddr;
2328 ushort rptr, wptr, mask;
2331 ofsAddr = moxaTableAddr[port];
2332 rptr = readw(ofsAddr + TXrptr);
2333 wptr = readw(ofsAddr + TXwptr);
2334 mask = readw(ofsAddr + TX_mask);
2335 len = mask - ((wptr - rptr) & mask);
2339 int MoxaPortRxQueue(int port)
2341 void __iomem *ofsAddr;
2342 ushort rptr, wptr, mask;
2345 ofsAddr = moxaTableAddr[port];
2346 rptr = readw(ofsAddr + RXrptr);
2347 wptr = readw(ofsAddr + RXwptr);
2348 mask = readw(ofsAddr + RX_mask);
2349 len = (wptr - rptr) & mask;
2354 void MoxaPortTxDisable(int port)
2356 void __iomem *ofsAddr;
2358 ofsAddr = moxaTableAddr[port];
2359 moxafunc(ofsAddr, FC_SetXoffState, Magic_code);
2362 void MoxaPortTxEnable(int port)
2364 void __iomem *ofsAddr;
2366 ofsAddr = moxaTableAddr[port];
2367 moxafunc(ofsAddr, FC_SetXonState, Magic_code);
2371 int MoxaPortResetBrkCnt(int port)
2374 cnt = moxaBreakCnt[port];
2375 moxaBreakCnt[port] = 0;
2380 void MoxaPortSendBreak(int port, int ms100)
2382 void __iomem *ofsAddr;
2384 ofsAddr = moxaTableAddr[port];
2386 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2387 moxadelay(ms100 * (HZ / 10));
2389 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2390 moxadelay(HZ / 4); /* 250 ms */
2392 moxafunc(ofsAddr, FC_StopBreak, Magic_code);
2395 static int moxa_get_serial_info(struct moxa_str *info,
2396 struct serial_struct __user *retinfo)
2398 struct serial_struct tmp;
2400 memset(&tmp, 0, sizeof(tmp));
2401 tmp.type = info->type;
2402 tmp.line = info->port;
2405 tmp.flags = info->asyncflags;
2406 tmp.baud_base = 921600;
2407 tmp.close_delay = info->close_delay;
2408 tmp.closing_wait = info->closing_wait;
2409 tmp.custom_divisor = 0;
2411 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2417 static int moxa_set_serial_info(struct moxa_str *info,
2418 struct serial_struct __user *new_info)
2420 struct serial_struct new_serial;
2422 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2425 if ((new_serial.irq != 0) ||
2426 (new_serial.port != 0) ||
2427 // (new_serial.type != info->type) ||
2428 (new_serial.custom_divisor != 0) ||
2429 (new_serial.baud_base != 921600))
2432 if (!capable(CAP_SYS_ADMIN)) {
2433 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2434 (info->asyncflags & ~ASYNC_USR_MASK)))
2437 info->close_delay = new_serial.close_delay * HZ / 100;
2438 info->closing_wait = new_serial.closing_wait * HZ / 100;
2441 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2442 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2444 if (new_serial.type == PORT_16550A) {
2445 MoxaSetFifo(info->port, 1);
2447 MoxaSetFifo(info->port, 0);
2450 info->type = new_serial.type;
2456 /*****************************************************************************
2457 * Static local functions: *
2458 *****************************************************************************/
2460 * moxadelay - delays a specified number ticks
2462 static void moxadelay(int tick)
2464 unsigned long st, et;
2468 while (time_before(jiffies, et));
2471 static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
2474 writew(arg, ofsAddr + FuncArg);
2475 writew(cmd, ofsAddr + FuncCode);
2476 wait_finish(ofsAddr);
2479 static void wait_finish(void __iomem *ofsAddr)
2484 while (readw(ofsAddr + FuncCode) != 0) {
2486 if ((j - i) > moxaFuncTout) {
2492 static void low_water_check(void __iomem *ofsAddr)
2495 ushort rptr, wptr, mask;
2497 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2498 rptr = readw(ofsAddr + RXrptr);
2499 wptr = readw(ofsAddr + RXwptr);
2500 mask = readw(ofsAddr + RX_mask);
2501 len = (wptr - rptr) & mask;
2502 if (len <= Low_water)
2503 moxafunc(ofsAddr, FC_SendXon, 0);
2507 static int moxaloadbios(int cardno, unsigned char __user *tmp, int len)
2509 void __iomem *baseAddr;
2512 if(copy_from_user(moxaBuff, tmp, len))
2514 baseAddr = moxaBaseAddr[cardno];
2515 writeb(HW_reset, baseAddr + Control_reg); /* reset */
2516 moxadelay(1); /* delay 10 ms */
2517 for (i = 0; i < 4096; i++)
2518 writeb(0, baseAddr + i); /* clear fix page */
2519 for (i = 0; i < len; i++)
2520 writeb(moxaBuff[i], baseAddr + i); /* download BIOS */
2521 writeb(0, baseAddr + Control_reg); /* restart */
2525 static int moxafindcard(int cardno)
2527 void __iomem *baseAddr;
2530 baseAddr = moxaBaseAddr[cardno];
2531 switch (moxa_boards[cardno].boardType) {
2532 case MOXA_BOARD_C218_ISA:
2533 case MOXA_BOARD_C218_PCI:
2534 if ((tmp = readw(baseAddr + C218_key)) != C218_KeyCode) {
2538 case MOXA_BOARD_CP204J:
2539 if ((tmp = readw(baseAddr + C218_key)) != CP204J_KeyCode) {
2544 if ((tmp = readw(baseAddr + C320_key)) != C320_KeyCode) {
2547 if ((tmp = readw(baseAddr + C320_status)) != STS_init) {
2554 static int moxaload320b(int cardno, unsigned char __user *tmp, int len)
2556 void __iomem *baseAddr;
2559 if(len > sizeof(moxaBuff))
2561 if(copy_from_user(moxaBuff, tmp, len))
2563 baseAddr = moxaBaseAddr[cardno];
2564 writew(len - 7168 - 2, baseAddr + C320bapi_len);
2565 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
2566 for (i = 0; i < 7168; i++)
2567 writeb(moxaBuff[i], baseAddr + DynPage_addr + i);
2568 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
2569 for (i = 0; i < (len - 7168); i++)
2570 writeb(moxaBuff[i + 7168], baseAddr + DynPage_addr + i);
2574 static int moxaloadcode(int cardno, unsigned char __user *tmp, int len)
2576 void __iomem *baseAddr, *ofsAddr;
2577 int retval, port, i;
2579 if(copy_from_user(moxaBuff, tmp, len))
2581 baseAddr = moxaBaseAddr[cardno];
2582 switch (moxa_boards[cardno].boardType) {
2583 case MOXA_BOARD_C218_ISA:
2584 case MOXA_BOARD_C218_PCI:
2585 case MOXA_BOARD_CP204J:
2586 retval = moxaloadc218(cardno, baseAddr, len);
2589 port = cardno * MAX_PORTS_PER_BOARD;
2590 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2591 moxaChkPort[port] = 1;
2592 moxaCurBaud[port] = 9600L;
2593 moxaDCDState[port] = 0;
2594 moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
2595 ofsAddr = moxaTableAddr[port];
2596 writew(C218rx_mask, ofsAddr + RX_mask);
2597 writew(C218tx_mask, ofsAddr + TX_mask);
2598 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
2599 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
2601 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
2602 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
2607 retval = moxaloadc320(cardno, baseAddr, len,
2608 &moxa_boards[cardno].numPorts);
2611 port = cardno * MAX_PORTS_PER_BOARD;
2612 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2613 moxaChkPort[port] = 1;
2614 moxaCurBaud[port] = 9600L;
2615 moxaDCDState[port] = 0;
2616 moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
2617 ofsAddr = moxaTableAddr[port];
2618 if (moxa_boards[cardno].numPorts == 8) {
2619 writew(C320p8rx_mask, ofsAddr + RX_mask);
2620 writew(C320p8tx_mask, ofsAddr + TX_mask);
2621 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
2622 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
2623 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
2624 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
2626 } else if (moxa_boards[cardno].numPorts == 16) {
2627 writew(C320p16rx_mask, ofsAddr + RX_mask);
2628 writew(C320p16tx_mask, ofsAddr + TX_mask);
2629 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
2630 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
2631 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
2632 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
2634 } else if (moxa_boards[cardno].numPorts == 24) {
2635 writew(C320p24rx_mask, ofsAddr + RX_mask);
2636 writew(C320p24tx_mask, ofsAddr + TX_mask);
2637 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
2638 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
2639 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
2640 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2641 } else if (moxa_boards[cardno].numPorts == 32) {
2642 writew(C320p32rx_mask, ofsAddr + RX_mask);
2643 writew(C320p32tx_mask, ofsAddr + TX_mask);
2644 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
2645 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
2646 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
2647 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
2648 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2653 loadstat[cardno] = 1;
2657 static int moxaloadc218(int cardno, void __iomem *baseAddr, int len)
2660 int i, j, len1, len2;
2661 ushort usum, *ptr, keycode;
2663 if (moxa_boards[cardno].boardType == MOXA_BOARD_CP204J)
2664 keycode = CP204J_KeyCode;
2666 keycode = C218_KeyCode;
2669 ptr = (ushort *) moxaBuff;
2670 for (i = 0; i < len1; i++)
2671 usum += le16_to_cpu(*(ptr + i));
2677 len2 = (len1 > 2048) ? 2048 : len1;
2679 for (i = 0; i < len2 << 1; i++)
2680 writeb(moxaBuff[i + j], baseAddr + C218_LoadBuf + i);
2683 writew(len2, baseAddr + C218DLoad_len);
2684 writew(0, baseAddr + C218_key);
2685 for (i = 0; i < 100; i++) {
2686 if (readw(baseAddr + C218_key) == keycode)
2688 moxadelay(1); /* delay 10 ms */
2690 if (readw(baseAddr + C218_key) != keycode) {
2694 writew(0, baseAddr + C218DLoad_len);
2695 writew(usum, baseAddr + C218check_sum);
2696 writew(0, baseAddr + C218_key);
2697 for (i = 0; i < 100; i++) {
2698 if (readw(baseAddr + C218_key) == keycode)
2700 moxadelay(1); /* delay 10 ms */
2703 } while ((readb(baseAddr + C218chksum_ok) != 1) && (retry < 3));
2704 if (readb(baseAddr + C218chksum_ok) != 1) {
2707 writew(0, baseAddr + C218_key);
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) {
2716 writew(1, baseAddr + Disable_IRQ);
2717 writew(0, baseAddr + Magic_no);
2718 for (i = 0; i < 100; i++) {
2719 if (readw(baseAddr + Magic_no) == Magic_code)
2721 moxadelay(1); /* delay 10 ms */
2723 if (readw(baseAddr + Magic_no) != Magic_code) {
2727 moxaIntNdx[cardno] = baseAddr + IRQindex;
2728 moxaIntPend[cardno] = baseAddr + IRQpending;
2729 moxaIntTable[cardno] = baseAddr + IRQtable;
2733 static int moxaloadc320(int cardno, void __iomem *baseAddr, int len, int *numPorts)
2736 int i, j, wlen, len2, retry;
2741 uptr = (ushort *) moxaBuff;
2742 for (i = 0; i < wlen; i++)
2743 usum += le16_to_cpu(uptr[i]);
2754 for (i = 0; i < len2; i++)
2755 writeb(moxaBuff[j + i], baseAddr + C320_LoadBuf + i);
2758 writew(len2, baseAddr + C320DLoad_len);
2759 writew(0, baseAddr + C320_key);
2760 for (i = 0; i < 10; i++) {
2761 if (readw(baseAddr + C320_key) == C320_KeyCode)
2765 if (readw(baseAddr + C320_key) != C320_KeyCode)
2768 writew(0, baseAddr + C320DLoad_len);
2769 writew(usum, baseAddr + C320check_sum);
2770 writew(0, baseAddr + C320_key);
2771 for (i = 0; i < 10; i++) {
2772 if (readw(baseAddr + C320_key) == C320_KeyCode)
2777 } while ((readb(baseAddr + C320chksum_ok) != 1) && (retry < 3));
2778 if (readb(baseAddr + C320chksum_ok) != 1)
2780 writew(0, baseAddr + C320_key);
2781 for (i = 0; i < 600; i++) {
2782 if (readw(baseAddr + Magic_no) == Magic_code)
2786 if (readw(baseAddr + Magic_no) != Magic_code)
2789 if (moxa_boards[cardno].busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
2790 writew(0x3800, baseAddr + TMS320_PORT1);
2791 writew(0x3900, baseAddr + TMS320_PORT2);
2792 writew(28499, baseAddr + TMS320_CLOCK);
2794 writew(0x3200, baseAddr + TMS320_PORT1);
2795 writew(0x3400, baseAddr + TMS320_PORT2);
2796 writew(19999, baseAddr + TMS320_CLOCK);
2798 writew(1, baseAddr + Disable_IRQ);
2799 writew(0, baseAddr + Magic_no);
2800 for (i = 0; i < 500; i++) {
2801 if (readw(baseAddr + Magic_no) == Magic_code)
2805 if (readw(baseAddr + Magic_no) != Magic_code)
2808 j = readw(baseAddr + Module_cnt);
2812 writew(j, baseAddr + Module_no);
2813 writew(0, baseAddr + Magic_no);
2814 for (i = 0; i < 600; i++) {
2815 if (readw(baseAddr + Magic_no) == Magic_code)
2819 if (readw(baseAddr + Magic_no) != Magic_code)
2822 moxaIntNdx[cardno] = baseAddr + IRQindex;
2823 moxaIntPend[cardno] = baseAddr + IRQpending;
2824 moxaIntTable[cardno] = baseAddr + IRQtable;
2828 static void MoxaSetFifo(int port, int enable)
2830 void __iomem *ofsAddr = moxaTableAddr[port];
2833 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2834 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2836 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2837 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);