1 /*****************************************************************************/
3 * moxa.c -- MOXA Intellio family multiport serial driver.
5 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com.tw).
7 * This code is loosely based on the Linux serial driver, written by
8 * Linus Torvalds, Theodore T'so and others.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * MOXA Intellio Series Driver
32 #include <linux/module.h>
33 #include <linux/types.h>
35 #include <linux/ioport.h>
36 #include <linux/errno.h>
37 #include <linux/signal.h>
38 #include <linux/sched.h>
39 #include <linux/timer.h>
40 #include <linux/interrupt.h>
41 #include <linux/tty.h>
42 #include <linux/tty_flip.h>
43 #include <linux/major.h>
44 #include <linux/string.h>
45 #include <linux/fcntl.h>
46 #include <linux/ptrace.h>
47 #include <linux/serial.h>
48 #include <linux/tty_driver.h>
49 #include <linux/delay.h>
50 #include <linux/pci.h>
51 #include <linux/init.h>
52 #include <linux/bitops.h>
54 #include <asm/system.h>
56 #include <asm/uaccess.h>
58 #define MOXA_VERSION "5.1k"
61 #define MOXACUMAJOR 173
63 #define put_to_user(arg1, arg2) put_user(arg1, (unsigned long *)arg2)
64 #define get_from_user(arg1, arg2) get_user(arg1, (unsigned int *)arg2)
66 #define MAX_BOARDS 4 /* Don't change this value */
67 #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
68 #define MAX_PORTS 128 /* Don't change this value */
71 * Define the Moxa PCI vendor and device IDs.
73 #define MOXA_BUS_TYPE_ISA 0
74 #define MOXA_BUS_TYPE_PCI 1
76 #ifndef PCI_VENDOR_ID_MOXA
77 #define PCI_VENDOR_ID_MOXA 0x1393
79 #ifndef PCI_DEVICE_ID_CP204J
80 #define PCI_DEVICE_ID_CP204J 0x2040
82 #ifndef PCI_DEVICE_ID_C218
83 #define PCI_DEVICE_ID_C218 0x2180
85 #ifndef PCI_DEVICE_ID_C320
86 #define PCI_DEVICE_ID_C320 0x3200
90 MOXA_BOARD_C218_PCI = 1,
97 static char *moxa_brdname[] =
99 "C218 Turbo PCI series",
100 "C218 Turbo ISA series",
101 "C320 Turbo PCI series",
102 "C320 Turbo ISA series",
107 static struct pci_device_id moxa_pcibrds[] = {
108 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C218, PCI_ANY_ID, PCI_ANY_ID,
109 0, 0, MOXA_BOARD_C218_PCI },
110 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C320, PCI_ANY_ID, PCI_ANY_ID,
111 0, 0, MOXA_BOARD_C320_PCI },
112 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP204J, PCI_ANY_ID, PCI_ANY_ID,
113 0, 0, MOXA_BOARD_CP204J },
116 MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
117 #endif /* CONFIG_PCI */
119 typedef struct _moxa_isa_board_conf {
122 unsigned long baseAddr;
123 } moxa_isa_board_conf;
125 static moxa_isa_board_conf moxa_isa_boards[] =
127 /* {MOXA_BOARD_C218_ISA,8,0xDC000}, */
130 typedef struct _moxa_pci_devinfo {
133 struct pci_dev *pdev;
136 typedef struct _moxa_board_conf {
139 unsigned long baseAddr;
141 moxa_pci_devinfo pciInfo;
144 static moxa_board_conf moxa_boards[MAX_BOARDS];
145 static void __iomem *moxaBaseAddr[MAX_BOARDS];
146 static int loadstat[MAX_BOARDS];
152 unsigned short closing_wait;
155 long event; /* long req'd for set_bit --RR */
157 unsigned long statusflags;
158 struct tty_struct *tty;
160 wait_queue_head_t open_wait;
161 wait_queue_head_t close_wait;
162 struct work_struct tqueue;
165 struct mxser_mstatus {
173 static struct mxser_mstatus GMStatus[MAX_PORTS];
176 #define TXSTOPPED 0x1
178 #define EMPTYWAIT 0x4
182 #define MOXA_EVENT_HANGUP 1
184 #define SERIAL_DO_RESTART
187 #define SERIAL_TYPE_NORMAL 1
189 #define WAKEUP_CHARS 256
191 #define PORTNO(x) ((x)->index)
193 static int verbose = 0;
194 static int ttymajor = MOXAMAJOR;
195 /* Variables for insmod */
197 static int baseaddr[] = {0, 0, 0, 0};
198 static int type[] = {0, 0, 0, 0};
199 static int numports[] = {0, 0, 0, 0};
202 MODULE_AUTHOR("William Chen");
203 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
204 MODULE_LICENSE("GPL");
206 module_param_array(type, int, NULL, 0);
207 module_param_array(baseaddr, int, NULL, 0);
208 module_param_array(numports, int, NULL, 0);
210 module_param(ttymajor, int, 0);
211 module_param(verbose, bool, 0644);
213 static struct tty_driver *moxaDriver;
214 static struct moxa_str moxaChannels[MAX_PORTS];
215 static unsigned char *moxaXmitBuff;
216 static int moxaTimer_on;
217 static struct timer_list moxaTimer;
218 static int moxaEmptyTimer_on[MAX_PORTS];
219 static struct timer_list moxaEmptyTimer[MAX_PORTS];
220 static struct semaphore moxaBuffSem;
225 static void do_moxa_softint(struct work_struct *);
226 static int moxa_open(struct tty_struct *, struct file *);
227 static void moxa_close(struct tty_struct *, struct file *);
228 static int moxa_write(struct tty_struct *, const unsigned char *, int);
229 static int moxa_write_room(struct tty_struct *);
230 static void moxa_flush_buffer(struct tty_struct *);
231 static int moxa_chars_in_buffer(struct tty_struct *);
232 static void moxa_flush_chars(struct tty_struct *);
233 static void moxa_put_char(struct tty_struct *, unsigned char);
234 static int moxa_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);
235 static void moxa_throttle(struct tty_struct *);
236 static void moxa_unthrottle(struct tty_struct *);
237 static void moxa_set_termios(struct tty_struct *, struct termios *);
238 static void moxa_stop(struct tty_struct *);
239 static void moxa_start(struct tty_struct *);
240 static void moxa_hangup(struct tty_struct *);
241 static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
242 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
243 unsigned int set, unsigned int clear);
244 static void moxa_poll(unsigned long);
245 static void set_tty_param(struct tty_struct *);
246 static int block_till_ready(struct tty_struct *, struct file *,
248 static void setup_empty_event(struct tty_struct *);
249 static void check_xmit_empty(unsigned long);
250 static void shut_down(struct moxa_str *);
251 static void receive_data(struct moxa_str *);
253 * moxa board interface functions:
255 static void MoxaDriverInit(void);
256 static int MoxaDriverIoctl(unsigned int, unsigned long, int);
257 static int MoxaDriverPoll(void);
258 static int MoxaPortsOfCard(int);
259 static int MoxaPortIsValid(int);
260 static void MoxaPortEnable(int);
261 static void MoxaPortDisable(int);
262 static long MoxaPortGetMaxBaud(int);
263 static long MoxaPortSetBaud(int, long);
264 static int MoxaPortSetTermio(int, struct termios *, speed_t);
265 static int MoxaPortGetLineOut(int, int *, int *);
266 static void MoxaPortLineCtrl(int, int, int);
267 static void MoxaPortFlowCtrl(int, int, int, int, int, int);
268 static int MoxaPortLineStatus(int);
269 static int MoxaPortDCDChange(int);
270 static int MoxaPortDCDON(int);
271 static void MoxaPortFlushData(int, int);
272 static int MoxaPortWriteData(int, unsigned char *, int);
273 static int MoxaPortReadData(int, struct tty_struct *tty);
274 static int MoxaPortTxQueue(int);
275 static int MoxaPortRxQueue(int);
276 static int MoxaPortTxFree(int);
277 static void MoxaPortTxDisable(int);
278 static void MoxaPortTxEnable(int);
279 static int MoxaPortResetBrkCnt(int);
280 static void MoxaPortSendBreak(int, int);
281 static int moxa_get_serial_info(struct moxa_str *, struct serial_struct __user *);
282 static int moxa_set_serial_info(struct moxa_str *, struct serial_struct __user *);
283 static void MoxaSetFifo(int port, int enable);
285 static const struct tty_operations moxa_ops = {
289 .write_room = moxa_write_room,
290 .flush_buffer = moxa_flush_buffer,
291 .chars_in_buffer = moxa_chars_in_buffer,
292 .flush_chars = moxa_flush_chars,
293 .put_char = moxa_put_char,
295 .throttle = moxa_throttle,
296 .unthrottle = moxa_unthrottle,
297 .set_termios = moxa_set_termios,
300 .hangup = moxa_hangup,
301 .tiocmget = moxa_tiocmget,
302 .tiocmset = moxa_tiocmset,
305 static DEFINE_SPINLOCK(moxa_lock);
308 static int moxa_get_PCI_conf(struct pci_dev *p, int board_type, moxa_board_conf * board)
310 board->baseAddr = pci_resource_start (p, 2);
311 board->boardType = board_type;
312 switch (board_type) {
313 case MOXA_BOARD_C218_ISA:
314 case MOXA_BOARD_C218_PCI:
318 case MOXA_BOARD_CP204J:
325 board->busType = MOXA_BUS_TYPE_PCI;
326 board->pciInfo.busNum = p->bus->number;
327 board->pciInfo.devNum = p->devfn >> 3;
328 board->pciInfo.pdev = p;
329 /* don't lose the reference in the next pci_get_device iteration */
334 #endif /* CONFIG_PCI */
336 static int __init moxa_init(void)
341 printk(KERN_INFO "MOXA Intellio family driver version %s\n", MOXA_VERSION);
342 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
346 init_MUTEX(&moxaBuffSem);
347 moxaDriver->owner = THIS_MODULE;
348 moxaDriver->name = "ttyMX";
349 moxaDriver->major = ttymajor;
350 moxaDriver->minor_start = 0;
351 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
352 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
353 moxaDriver->init_termios = tty_std_termios;
354 moxaDriver->init_termios.c_iflag = 0;
355 moxaDriver->init_termios.c_oflag = 0;
356 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
357 moxaDriver->init_termios.c_lflag = 0;
358 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
359 tty_set_operations(moxaDriver, &moxa_ops);
363 for (i = 0, ch = moxaChannels; i < MAX_PORTS; i++, ch++) {
364 ch->type = PORT_16550A;
366 INIT_WORK(&ch->tqueue, do_moxa_softint);
368 ch->close_delay = 5 * HZ / 10;
369 ch->closing_wait = 30 * HZ;
371 ch->blocked_open = 0;
372 ch->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
373 init_waitqueue_head(&ch->open_wait);
374 init_waitqueue_head(&ch->close_wait);
377 for (i = 0; i < MAX_BOARDS; i++) {
378 moxa_boards[i].boardType = 0;
379 moxa_boards[i].numPorts = 0;
380 moxa_boards[i].baseAddr = 0;
381 moxa_boards[i].busType = 0;
382 moxa_boards[i].pciInfo.busNum = 0;
383 moxa_boards[i].pciInfo.devNum = 0;
386 printk("Tty devices major number = %d\n", ttymajor);
388 if (tty_register_driver(moxaDriver)) {
389 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
390 put_tty_driver(moxaDriver);
393 for (i = 0; i < MAX_PORTS; i++) {
394 init_timer(&moxaEmptyTimer[i]);
395 moxaEmptyTimer[i].function = check_xmit_empty;
396 moxaEmptyTimer[i].data = (unsigned long) & moxaChannels[i];
397 moxaEmptyTimer_on[i] = 0;
400 init_timer(&moxaTimer);
401 moxaTimer.function = moxa_poll;
402 moxaTimer.expires = jiffies + (HZ / 50);
404 add_timer(&moxaTimer);
406 /* Find the boards defined in source code */
408 for (i = 0; i < MAX_BOARDS; i++) {
409 if ((moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA) ||
410 (moxa_isa_boards[i].boardType == MOXA_BOARD_C320_ISA)) {
411 moxa_boards[numBoards].boardType = moxa_isa_boards[i].boardType;
412 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
413 moxa_boards[numBoards].numPorts = 8;
415 moxa_boards[numBoards].numPorts = moxa_isa_boards[i].numPorts;
416 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
417 moxa_boards[numBoards].baseAddr = moxa_isa_boards[i].baseAddr;
419 printk("Board %2d: %s board(baseAddr=%lx)\n",
421 moxa_brdname[moxa_boards[numBoards].boardType - 1],
422 moxa_boards[numBoards].baseAddr);
426 /* Find the boards defined form module args. */
428 for (i = 0; i < MAX_BOARDS; i++) {
429 if ((type[i] == MOXA_BOARD_C218_ISA) ||
430 (type[i] == MOXA_BOARD_C320_ISA)) {
432 printk("Board %2d: %s board(baseAddr=%lx)\n",
434 moxa_brdname[type[i] - 1],
435 (unsigned long) baseaddr[i]);
436 if (numBoards >= MAX_BOARDS) {
438 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
441 moxa_boards[numBoards].boardType = type[i];
442 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
443 moxa_boards[numBoards].numPorts = 8;
445 moxa_boards[numBoards].numPorts = numports[i];
446 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
447 moxa_boards[numBoards].baseAddr = baseaddr[i];
452 /* Find PCI boards here */
455 struct pci_dev *p = NULL;
456 int n = ARRAY_SIZE(moxa_pcibrds) - 1;
459 while ((p = pci_get_device(moxa_pcibrds[i].vendor, moxa_pcibrds[i].device, p))!=NULL)
461 if (pci_enable_device(p))
463 if (numBoards >= MAX_BOARDS) {
465 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
467 moxa_get_PCI_conf(p, moxa_pcibrds[i].driver_data,
468 &moxa_boards[numBoards]);
476 for (i = 0; i < numBoards; i++) {
477 moxaBaseAddr[i] = ioremap((unsigned long) moxa_boards[i].baseAddr, 0x4000);
483 static void __exit moxa_exit(void)
488 printk("Unloading module moxa ...\n");
491 del_timer(&moxaTimer);
493 for (i = 0; i < MAX_PORTS; i++)
494 if (moxaEmptyTimer_on[i])
495 del_timer(&moxaEmptyTimer[i]);
497 if (tty_unregister_driver(moxaDriver))
498 printk("Couldn't unregister MOXA Intellio family serial driver\n");
499 put_tty_driver(moxaDriver);
501 for (i = 0; i < MAX_BOARDS; i++) {
503 iounmap(moxaBaseAddr[i]);
504 if (moxa_boards[i].busType == MOXA_BUS_TYPE_PCI)
505 pci_dev_put(moxa_boards[i].pciInfo.pdev);
512 module_init(moxa_init);
513 module_exit(moxa_exit);
515 static void do_moxa_softint(struct work_struct *work)
517 struct moxa_str *ch = container_of(work, struct moxa_str, tqueue);
518 struct tty_struct *tty;
520 if (ch && (tty = ch->tty)) {
521 if (test_and_clear_bit(MOXA_EVENT_HANGUP, &ch->event)) {
522 tty_hangup(tty); /* FIXME: module removal race here - AKPM */
523 wake_up_interruptible(&ch->open_wait);
524 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
529 static int moxa_open(struct tty_struct *tty, struct file *filp)
537 if (port == MAX_PORTS) {
540 if (!MoxaPortIsValid(port)) {
541 tty->driver_data = NULL;
546 page = get_zeroed_page(GFP_KERNEL);
551 /* This test is guarded by the BuffSem so no longer needed
556 moxaXmitBuff = (unsigned char *) page;
560 ch = &moxaChannels[port];
562 tty->driver_data = ch;
564 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
567 MoxaPortLineCtrl(ch->port, 1, 1);
568 MoxaPortEnable(ch->port);
569 ch->asyncflags |= ASYNC_INITIALIZED;
571 retval = block_till_ready(tty, filp, ch);
573 moxa_unthrottle(tty);
575 if (ch->type == PORT_16550A) {
576 MoxaSetFifo(ch->port, 1);
578 MoxaSetFifo(ch->port, 0);
584 static void moxa_close(struct tty_struct *tty, struct file *filp)
590 if (port == MAX_PORTS) {
593 if (!MoxaPortIsValid(port)) {
594 #ifdef SERIAL_DEBUG_CLOSE
595 printk("Invalid portno in moxa_close\n");
597 tty->driver_data = NULL;
600 if (tty->driver_data == NULL) {
603 if (tty_hung_up_p(filp)) {
606 ch = (struct moxa_str *) tty->driver_data;
608 if ((tty->count == 1) && (ch->count != 1)) {
609 printk("moxa_close: bad serial port count; tty->count is 1, "
610 "ch->count is %d\n", ch->count);
613 if (--ch->count < 0) {
614 printk("moxa_close: bad serial port count, device=%s\n",
621 ch->asyncflags |= ASYNC_CLOSING;
623 ch->cflag = tty->termios->c_cflag;
624 if (ch->asyncflags & ASYNC_INITIALIZED) {
625 setup_empty_event(tty);
626 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
627 moxaEmptyTimer_on[ch->port] = 0;
628 del_timer(&moxaEmptyTimer[ch->port]);
631 MoxaPortFlushData(port, 2);
633 if (tty->driver->flush_buffer)
634 tty->driver->flush_buffer(tty);
635 tty_ldisc_flush(tty);
640 if (ch->blocked_open) {
641 if (ch->close_delay) {
642 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
644 wake_up_interruptible(&ch->open_wait);
646 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
647 wake_up_interruptible(&ch->close_wait);
650 static int moxa_write(struct tty_struct *tty,
651 const unsigned char *buf, int count)
657 ch = (struct moxa_str *) tty->driver_data;
662 spin_lock_irqsave(&moxa_lock, flags);
663 len = MoxaPortWriteData(port, (unsigned char *) buf, count);
664 spin_unlock_irqrestore(&moxa_lock, flags);
666 /*********************************************
667 if ( !(ch->statusflags & LOWWAIT) &&
668 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
669 ************************************************/
670 ch->statusflags |= LOWWAIT;
674 static int moxa_write_room(struct tty_struct *tty)
680 ch = (struct moxa_str *) tty->driver_data;
683 return (MoxaPortTxFree(ch->port));
686 static void moxa_flush_buffer(struct tty_struct *tty)
688 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
692 MoxaPortFlushData(ch->port, 1);
696 static int moxa_chars_in_buffer(struct tty_struct *tty)
699 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
702 * Sigh...I have to check if driver_data is NULL here, because
703 * if an open() fails, the TTY subsystem eventually calls
704 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
705 * routine. And since the open() failed, we return 0 here. TDJ
709 chars = MoxaPortTxQueue(ch->port);
712 * Make it possible to wakeup anything waiting for output
713 * in tty_ioctl.c, etc.
715 if (!(ch->statusflags & EMPTYWAIT))
716 setup_empty_event(tty);
721 static void moxa_flush_chars(struct tty_struct *tty)
724 * Don't think I need this, because this is called to empty the TX
725 * buffer for the 16450, 16550, etc.
729 static void moxa_put_char(struct tty_struct *tty, unsigned char c)
735 ch = (struct moxa_str *) tty->driver_data;
739 spin_lock_irqsave(&moxa_lock, flags);
741 MoxaPortWriteData(port, moxaXmitBuff, 1);
742 spin_unlock_irqrestore(&moxa_lock, flags);
743 /************************************************
744 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
745 *************************************************/
746 ch->statusflags |= LOWWAIT;
749 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
751 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
753 int flag = 0, dtr, rts;
756 if ((port != MAX_PORTS) && (!ch))
759 MoxaPortGetLineOut(ch->port, &dtr, &rts);
764 dtr = MoxaPortLineStatus(ch->port);
774 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
775 unsigned int set, unsigned int clear)
777 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
782 if ((port != MAX_PORTS) && (!ch))
785 MoxaPortGetLineOut(ch->port, &dtr, &rts);
790 if (clear & TIOCM_RTS)
792 if (clear & TIOCM_DTR)
794 MoxaPortLineCtrl(ch->port, dtr, rts);
798 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
799 unsigned int cmd, unsigned long arg)
801 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
803 void __user *argp = (void __user *)arg;
807 if ((port != MAX_PORTS) && (!ch))
811 case TCSBRK: /* SVID version: non-zero arg --> no break */
812 retval = tty_check_change(tty);
815 setup_empty_event(tty);
816 tty_wait_until_sent(tty, 0);
818 MoxaPortSendBreak(ch->port, 0);
820 case TCSBRKP: /* support for POSIX tcsendbreak() */
821 retval = tty_check_change(tty);
824 setup_empty_event(tty);
825 tty_wait_until_sent(tty, 0);
826 MoxaPortSendBreak(ch->port, arg);
829 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) argp);
831 if(get_user(retval, (unsigned long __user *) argp))
834 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
837 ch->asyncflags &= ~ASYNC_CHECK_CD;
839 ch->asyncflags |= ASYNC_CHECK_CD;
842 return moxa_get_serial_info(ch, argp);
845 return moxa_set_serial_info(ch, argp);
847 retval = MoxaDriverIoctl(cmd, arg, port);
852 static void moxa_throttle(struct tty_struct *tty)
854 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
856 ch->statusflags |= THROTTLE;
859 static void moxa_unthrottle(struct tty_struct *tty)
861 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
863 ch->statusflags &= ~THROTTLE;
866 static void moxa_set_termios(struct tty_struct *tty,
867 struct termios *old_termios)
869 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
874 if (!(old_termios->c_cflag & CLOCAL) &&
875 (tty->termios->c_cflag & CLOCAL))
876 wake_up_interruptible(&ch->open_wait);
879 static void moxa_stop(struct tty_struct *tty)
881 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
885 MoxaPortTxDisable(ch->port);
886 ch->statusflags |= TXSTOPPED;
890 static void moxa_start(struct tty_struct *tty)
892 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
897 if (!(ch->statusflags & TXSTOPPED))
900 MoxaPortTxEnable(ch->port);
901 ch->statusflags &= ~TXSTOPPED;
904 static void moxa_hangup(struct tty_struct *tty)
906 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
908 moxa_flush_buffer(tty);
912 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
914 wake_up_interruptible(&ch->open_wait);
917 static void moxa_poll(unsigned long ignored)
921 struct tty_struct *tp;
925 del_timer(&moxaTimer);
927 if (MoxaDriverPoll() < 0) {
928 moxaTimer.function = moxa_poll;
929 moxaTimer.expires = jiffies + (HZ / 50);
931 add_timer(&moxaTimer);
934 for (card = 0; card < MAX_BOARDS; card++) {
935 if ((ports = MoxaPortsOfCard(card)) <= 0)
937 ch = &moxaChannels[card * MAX_PORTS_PER_BOARD];
938 for (i = 0; i < ports; i++, ch++) {
939 if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
941 if (!(ch->statusflags & THROTTLE) &&
942 (MoxaPortRxQueue(ch->port) > 0))
944 if ((tp = ch->tty) == 0)
946 if (ch->statusflags & LOWWAIT) {
947 if (MoxaPortTxQueue(ch->port) <= WAKEUP_CHARS) {
949 ch->statusflags &= ~LOWWAIT;
954 if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch->port) > 0)) {
955 tty_insert_flip_char(tp, 0, TTY_BREAK);
956 tty_schedule_flip(tp);
958 if (MoxaPortDCDChange(ch->port)) {
959 if (ch->asyncflags & ASYNC_CHECK_CD) {
960 if (MoxaPortDCDON(ch->port))
961 wake_up_interruptible(&ch->open_wait);
963 set_bit(MOXA_EVENT_HANGUP, &ch->event);
964 schedule_work(&ch->tqueue);
971 moxaTimer.function = moxa_poll;
972 moxaTimer.expires = jiffies + (HZ / 50);
974 add_timer(&moxaTimer);
977 /******************************************************************************/
979 static void set_tty_param(struct tty_struct *tty)
981 register struct termios *ts;
983 int rts, cts, txflow, rxflow, xany;
985 ch = (struct moxa_str *) tty->driver_data;
987 if (ts->c_cflag & CLOCAL)
988 ch->asyncflags &= ~ASYNC_CHECK_CD;
990 ch->asyncflags |= ASYNC_CHECK_CD;
991 rts = cts = txflow = rxflow = xany = 0;
992 if (ts->c_cflag & CRTSCTS)
994 if (ts->c_iflag & IXON)
996 if (ts->c_iflag & IXOFF)
998 if (ts->c_iflag & IXANY)
1000 MoxaPortFlowCtrl(ch->port, rts, cts, txflow, rxflow, xany);
1001 MoxaPortSetTermio(ch->port, ts, tty_get_baud_rate(tty));
1004 static int block_till_ready(struct tty_struct *tty, struct file *filp,
1005 struct moxa_str *ch)
1007 DECLARE_WAITQUEUE(wait,current);
1008 unsigned long flags;
1010 int do_clocal = C_CLOCAL(tty);
1013 * If the device is in the middle of being closed, then block
1014 * until it's done, and then try again.
1016 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
1017 if (ch->asyncflags & ASYNC_CLOSING)
1018 interruptible_sleep_on(&ch->close_wait);
1019 #ifdef SERIAL_DO_RESTART
1020 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1023 return (-ERESTARTSYS);
1029 * If non-blocking mode is set, then make the check up front
1032 if (filp->f_flags & O_NONBLOCK) {
1033 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1037 * Block waiting for the carrier detect and the line to become free
1040 add_wait_queue(&ch->open_wait, &wait);
1041 #ifdef SERIAL_DEBUG_OPEN
1042 printk("block_til_ready before block: ttys%d, count = %d\n",
1043 ch->line, ch->count);
1045 spin_lock_irqsave(&moxa_lock, flags);
1046 if (!tty_hung_up_p(filp))
1049 spin_unlock_irqrestore(&moxa_lock, flags);
1052 set_current_state(TASK_INTERRUPTIBLE);
1053 if (tty_hung_up_p(filp) ||
1054 !(ch->asyncflags & ASYNC_INITIALIZED)) {
1055 #ifdef SERIAL_DO_RESTART
1056 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1059 retval = -ERESTARTSYS;
1065 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
1066 MoxaPortDCDON(ch->port)))
1069 if (signal_pending(current)) {
1070 retval = -ERESTARTSYS;
1075 set_current_state(TASK_RUNNING);
1076 remove_wait_queue(&ch->open_wait, &wait);
1078 spin_lock_irqsave(&moxa_lock, flags);
1079 if (!tty_hung_up_p(filp))
1082 spin_unlock_irqrestore(&moxa_lock, flags);
1083 #ifdef SERIAL_DEBUG_OPEN
1084 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1085 ch->line, ch->count);
1089 /* FIXME: review to see if we need to use set_bit on these */
1090 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1094 static void setup_empty_event(struct tty_struct *tty)
1096 struct moxa_str *ch = tty->driver_data;
1097 unsigned long flags;
1099 spin_lock_irqsave(&moxa_lock, flags);
1100 ch->statusflags |= EMPTYWAIT;
1101 moxaEmptyTimer_on[ch->port] = 0;
1102 del_timer(&moxaEmptyTimer[ch->port]);
1103 moxaEmptyTimer[ch->port].expires = jiffies + HZ;
1104 moxaEmptyTimer_on[ch->port] = 1;
1105 add_timer(&moxaEmptyTimer[ch->port]);
1106 spin_unlock_irqrestore(&moxa_lock, flags);
1109 static void check_xmit_empty(unsigned long data)
1111 struct moxa_str *ch;
1113 ch = (struct moxa_str *) data;
1114 moxaEmptyTimer_on[ch->port] = 0;
1115 del_timer(&moxaEmptyTimer[ch->port]);
1116 if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
1117 if (MoxaPortTxQueue(ch->port) == 0) {
1118 ch->statusflags &= ~EMPTYWAIT;
1119 tty_wakeup(ch->tty);
1122 moxaEmptyTimer[ch->port].expires = jiffies + HZ;
1123 moxaEmptyTimer_on[ch->port] = 1;
1124 add_timer(&moxaEmptyTimer[ch->port]);
1126 ch->statusflags &= ~EMPTYWAIT;
1129 static void shut_down(struct moxa_str *ch)
1131 struct tty_struct *tp;
1133 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1138 MoxaPortDisable(ch->port);
1141 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1143 if (tp->termios->c_cflag & HUPCL)
1144 MoxaPortLineCtrl(ch->port, 0, 0);
1146 ch->asyncflags &= ~ASYNC_INITIALIZED;
1149 static void receive_data(struct moxa_str *ch)
1151 struct tty_struct *tp;
1153 unsigned long flags;
1159 /**************************************************
1160 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1161 *****************************************************/
1163 MoxaPortFlushData(ch->port, 0);
1166 spin_lock_irqsave(&moxa_lock, flags);
1167 MoxaPortReadData(ch->port, tp);
1168 spin_unlock_irqrestore(&moxa_lock, flags);
1169 tty_schedule_flip(tp);
1172 #define Magic_code 0x404
1175 * System Configuration
1178 * for C218 BIOS initialization
1180 #define C218_ConfBase 0x800
1181 #define C218_status (C218_ConfBase + 0) /* BIOS running status */
1182 #define C218_diag (C218_ConfBase + 2) /* diagnostic status */
1183 #define C218_key (C218_ConfBase + 4) /* WORD (0x218 for C218) */
1184 #define C218DLoad_len (C218_ConfBase + 6) /* WORD */
1185 #define C218check_sum (C218_ConfBase + 8) /* BYTE */
1186 #define C218chksum_ok (C218_ConfBase + 0x0a) /* BYTE (1:ok) */
1187 #define C218_TestRx (C218_ConfBase + 0x10) /* 8 bytes for 8 ports */
1188 #define C218_TestTx (C218_ConfBase + 0x18) /* 8 bytes for 8 ports */
1189 #define C218_RXerr (C218_ConfBase + 0x20) /* 8 bytes for 8 ports */
1190 #define C218_ErrFlag (C218_ConfBase + 0x28) /* 8 bytes for 8 ports */
1192 #define C218_LoadBuf 0x0F00
1193 #define C218_KeyCode 0x218
1194 #define CP204J_KeyCode 0x204
1197 * for C320 BIOS initialization
1199 #define C320_ConfBase 0x800
1200 #define C320_LoadBuf 0x0f00
1201 #define STS_init 0x05 /* for C320_status */
1203 #define C320_status C320_ConfBase + 0 /* BIOS running status */
1204 #define C320_diag C320_ConfBase + 2 /* diagnostic status */
1205 #define C320_key C320_ConfBase + 4 /* WORD (0320H for C320) */
1206 #define C320DLoad_len C320_ConfBase + 6 /* WORD */
1207 #define C320check_sum C320_ConfBase + 8 /* WORD */
1208 #define C320chksum_ok C320_ConfBase + 0x0a /* WORD (1:ok) */
1209 #define C320bapi_len C320_ConfBase + 0x0c /* WORD */
1210 #define C320UART_no C320_ConfBase + 0x0e /* WORD */
1212 #define C320_KeyCode 0x320
1214 #define FixPage_addr 0x0000 /* starting addr of static page */
1215 #define DynPage_addr 0x2000 /* starting addr of dynamic page */
1216 #define C218_start 0x3000 /* starting addr of C218 BIOS prg */
1217 #define Control_reg 0x1ff0 /* select page and reset control */
1218 #define HW_reset 0x80
1223 #define FC_CardReset 0x80
1224 #define FC_ChannelReset 1 /* C320 firmware not supported */
1225 #define FC_EnableCH 2
1226 #define FC_DisableCH 3
1227 #define FC_SetParam 4
1228 #define FC_SetMode 5
1229 #define FC_SetRate 6
1230 #define FC_LineControl 7
1231 #define FC_LineStatus 8
1232 #define FC_XmitControl 9
1233 #define FC_FlushQueue 10
1234 #define FC_SendBreak 11
1235 #define FC_StopBreak 12
1236 #define FC_LoopbackON 13
1237 #define FC_LoopbackOFF 14
1238 #define FC_ClrIrqTable 15
1239 #define FC_SendXon 16
1240 #define FC_SetTermIrq 17 /* C320 firmware not supported */
1241 #define FC_SetCntIrq 18 /* C320 firmware not supported */
1242 #define FC_SetBreakIrq 19
1243 #define FC_SetLineIrq 20
1244 #define FC_SetFlowCtl 21
1245 #define FC_GenIrq 22
1246 #define FC_InCD180 23
1247 #define FC_OutCD180 24
1248 #define FC_InUARTreg 23
1249 #define FC_OutUARTreg 24
1250 #define FC_SetXonXoff 25
1251 #define FC_OutCD180CCR 26
1252 #define FC_ExtIQueue 27
1253 #define FC_ExtOQueue 28
1254 #define FC_ClrLineIrq 29
1255 #define FC_HWFlowCtl 30
1256 #define FC_GetClockRate 35
1257 #define FC_SetBaud 36
1258 #define FC_SetDataMode 41
1259 #define FC_GetCCSR 43
1260 #define FC_GetDataError 45
1261 #define FC_RxControl 50
1262 #define FC_ImmSend 51
1263 #define FC_SetXonState 52
1264 #define FC_SetXoffState 53
1265 #define FC_SetRxFIFOTrig 54
1266 #define FC_SetTxFIFOCnt 55
1267 #define FC_UnixRate 56
1268 #define FC_UnixResetTimer 57
1270 #define RxFIFOTrig1 0
1271 #define RxFIFOTrig4 1
1272 #define RxFIFOTrig8 2
1273 #define RxFIFOTrig14 3
1278 #define DRAM_global 0
1279 #define INT_data (DRAM_global + 0)
1280 #define Config_base (DRAM_global + 0x108)
1282 #define IRQindex (INT_data + 0)
1283 #define IRQpending (INT_data + 4)
1284 #define IRQtable (INT_data + 8)
1289 #define IntrRx 0x01 /* receiver data O.K. */
1290 #define IntrTx 0x02 /* transmit buffer empty */
1291 #define IntrFunc 0x04 /* function complete */
1292 #define IntrBreak 0x08 /* received break */
1293 #define IntrLine 0x10 /* line status change
1295 #define IntrIntr 0x20 /* received INTR code */
1296 #define IntrQuit 0x40 /* received QUIT code */
1297 #define IntrEOF 0x80 /* received EOF code */
1299 #define IntrRxTrigger 0x100 /* rx data count reach tigger value */
1300 #define IntrTxTrigger 0x200 /* tx data count below trigger value */
1302 #define Magic_no (Config_base + 0)
1303 #define Card_model_no (Config_base + 2)
1304 #define Total_ports (Config_base + 4)
1305 #define Module_cnt (Config_base + 8)
1306 #define Module_no (Config_base + 10)
1307 #define Timer_10ms (Config_base + 14)
1308 #define Disable_IRQ (Config_base + 20)
1309 #define TMS320_PORT1 (Config_base + 22)
1310 #define TMS320_PORT2 (Config_base + 24)
1311 #define TMS320_CLOCK (Config_base + 26)
1314 * DATA BUFFER in DRAM
1316 #define Extern_table 0x400 /* Base address of the external table
1317 (24 words * 64) total 3K bytes
1318 (24 words * 128) total 6K bytes */
1319 #define Extern_size 0x60 /* 96 bytes */
1320 #define RXrptr 0x00 /* read pointer for RX buffer */
1321 #define RXwptr 0x02 /* write pointer for RX buffer */
1322 #define TXrptr 0x04 /* read pointer for TX buffer */
1323 #define TXwptr 0x06 /* write pointer for TX buffer */
1324 #define HostStat 0x08 /* IRQ flag and general flag */
1325 #define FlagStat 0x0A
1326 #define FlowControl 0x0C /* B7 B6 B5 B4 B3 B2 B1 B0 */
1327 /* x x x x | | | | */
1328 /* | | | + CTS flow */
1329 /* | | +--- RTS flow */
1330 /* | +------ TX Xon/Xoff */
1331 /* +--------- RX Xon/Xoff */
1332 #define Break_cnt 0x0E /* received break count */
1333 #define CD180TXirq 0x10 /* if non-0: enable TX irq */
1334 #define RX_mask 0x12
1335 #define TX_mask 0x14
1336 #define Ofs_rxb 0x16
1337 #define Ofs_txb 0x18
1338 #define Page_rxb 0x1A
1339 #define Page_txb 0x1C
1340 #define EndPage_rxb 0x1E
1341 #define EndPage_txb 0x20
1342 #define Data_error 0x22
1343 #define RxTrigger 0x28
1344 #define TxTrigger 0x2a
1346 #define rRXwptr 0x34
1347 #define Low_water 0x36
1349 #define FuncCode 0x40
1350 #define FuncArg 0x42
1351 #define FuncArg1 0x44
1353 #define C218rx_size 0x2000 /* 8K bytes */
1354 #define C218tx_size 0x8000 /* 32K bytes */
1356 #define C218rx_mask (C218rx_size - 1)
1357 #define C218tx_mask (C218tx_size - 1)
1359 #define C320p8rx_size 0x2000
1360 #define C320p8tx_size 0x8000
1361 #define C320p8rx_mask (C320p8rx_size - 1)
1362 #define C320p8tx_mask (C320p8tx_size - 1)
1364 #define C320p16rx_size 0x2000
1365 #define C320p16tx_size 0x4000
1366 #define C320p16rx_mask (C320p16rx_size - 1)
1367 #define C320p16tx_mask (C320p16tx_size - 1)
1369 #define C320p24rx_size 0x2000
1370 #define C320p24tx_size 0x2000
1371 #define C320p24rx_mask (C320p24rx_size - 1)
1372 #define C320p24tx_mask (C320p24tx_size - 1)
1374 #define C320p32rx_size 0x1000
1375 #define C320p32tx_size 0x1000
1376 #define C320p32rx_mask (C320p32rx_size - 1)
1377 #define C320p32tx_mask (C320p32tx_size - 1)
1379 #define Page_size 0x2000
1380 #define Page_mask (Page_size - 1)
1381 #define C218rx_spage 3
1382 #define C218tx_spage 4
1383 #define C218rx_pageno 1
1384 #define C218tx_pageno 4
1385 #define C218buf_pageno 5
1387 #define C320p8rx_spage 3
1388 #define C320p8tx_spage 4
1389 #define C320p8rx_pgno 1
1390 #define C320p8tx_pgno 4
1391 #define C320p8buf_pgno 5
1393 #define C320p16rx_spage 3
1394 #define C320p16tx_spage 4
1395 #define C320p16rx_pgno 1
1396 #define C320p16tx_pgno 2
1397 #define C320p16buf_pgno 3
1399 #define C320p24rx_spage 3
1400 #define C320p24tx_spage 4
1401 #define C320p24rx_pgno 1
1402 #define C320p24tx_pgno 1
1403 #define C320p24buf_pgno 2
1405 #define C320p32rx_spage 3
1406 #define C320p32tx_ofs C320p32rx_size
1407 #define C320p32tx_spage 3
1408 #define C320p32buf_pgno 1
1413 #define WakeupRx 0x01
1414 #define WakeupTx 0x02
1415 #define WakeupBreak 0x08
1416 #define WakeupLine 0x10
1417 #define WakeupIntr 0x20
1418 #define WakeupQuit 0x40
1419 #define WakeupEOF 0x80 /* used in VTIME control */
1420 #define WakeupRxTrigger 0x100
1421 #define WakeupTxTrigger 0x200
1425 #define Rx_over 0x01
1426 #define Xoff_state 0x02
1427 #define Tx_flowOff 0x04
1428 #define Tx_enable 0x08
1429 #define CTS_state 0x10
1430 #define DSR_state 0x20
1431 #define DCD_state 0x80
1435 #define CTS_FlowCtl 1
1436 #define RTS_FlowCtl 2
1437 #define Tx_FlowCtl 4
1438 #define Rx_FlowCtl 8
1439 #define IXM_IXANY 0x10
1441 #define LowWater 128
1449 /* mode definition */
1455 #define MX_STOP1 0x00
1456 #define MX_STOP15 0x04
1457 #define MX_STOP2 0x08
1459 #define MX_PARNONE 0x00
1460 #define MX_PAREVEN 0x40
1461 #define MX_PARODD 0xC0
1466 #define QueryPort MAX_PORTS
1472 int rxcnt[MAX_PORTS];
1473 int txcnt[MAX_PORTS];
1475 typedef struct mon_str mon_st;
1477 #define DCD_changed 0x01
1478 #define DCD_oldstate 0x80
1480 static unsigned char moxaBuff[10240];
1481 static void __iomem *moxaIntNdx[MAX_BOARDS];
1482 static void __iomem *moxaIntPend[MAX_BOARDS];
1483 static void __iomem *moxaIntTable[MAX_BOARDS];
1484 static char moxaChkPort[MAX_PORTS];
1485 static char moxaLineCtrl[MAX_PORTS];
1486 static void __iomem *moxaTableAddr[MAX_PORTS];
1487 static long moxaCurBaud[MAX_PORTS];
1488 static char moxaDCDState[MAX_PORTS];
1489 static char moxaLowChkFlag[MAX_PORTS];
1490 static int moxaLowWaterChk;
1491 static int moxaCard;
1492 static mon_st moxaLog;
1493 static int moxaFuncTout;
1494 static ushort moxaBreakCnt[MAX_PORTS];
1496 static void moxadelay(int);
1497 static void moxafunc(void __iomem *, int, ushort);
1498 static void wait_finish(void __iomem *);
1499 static void low_water_check(void __iomem *);
1500 static int moxaloadbios(int, unsigned char __user *, int);
1501 static int moxafindcard(int);
1502 static int moxaload320b(int, unsigned char __user *, int);
1503 static int moxaloadcode(int, unsigned char __user *, int);
1504 static int moxaloadc218(int, void __iomem *, int);
1505 static int moxaloadc320(int, void __iomem *, int, int *);
1507 /*****************************************************************************
1508 * Driver level functions: *
1509 * 1. MoxaDriverInit(void); *
1510 * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
1511 * 3. MoxaDriverPoll(void); *
1512 *****************************************************************************/
1513 void MoxaDriverInit(void)
1517 moxaFuncTout = HZ / 2; /* 500 mini-seconds */
1520 moxaLowWaterChk = 0;
1521 for (i = 0; i < MAX_PORTS; i++) {
1523 moxaLowChkFlag[i] = 0;
1524 moxaLineCtrl[i] = 0;
1525 moxaLog.rxcnt[i] = 0;
1526 moxaLog.txcnt[i] = 0;
1531 #define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1532 #define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
1533 #define MOXA_INIT_DRIVER (MOXA + 6) /* moxaCard=0 */
1534 #define MOXA_LOAD_BIOS (MOXA + 9) /* download BIOS */
1535 #define MOXA_FIND_BOARD (MOXA + 10) /* Check if MOXA card exist? */
1536 #define MOXA_LOAD_C320B (MOXA + 11) /* download 320B firmware */
1537 #define MOXA_LOAD_CODE (MOXA + 12) /* download firmware */
1538 #define MOXA_GETDATACOUNT (MOXA + 23)
1539 #define MOXA_GET_IOQUEUE (MOXA + 27)
1540 #define MOXA_FLUSH_QUEUE (MOXA + 28)
1541 #define MOXA_GET_CONF (MOXA + 35) /* configuration */
1542 #define MOXA_GET_MAJOR (MOXA + 63)
1543 #define MOXA_GET_CUMAJOR (MOXA + 64)
1544 #define MOXA_GETMSTATUS (MOXA + 65)
1558 static struct moxaq_str temp_queue[MAX_PORTS];
1559 static struct dl_str dltmp;
1561 void MoxaPortFlushData(int port, int mode)
1563 void __iomem *ofsAddr;
1564 if ((mode < 0) || (mode > 2))
1566 ofsAddr = moxaTableAddr[port];
1567 moxafunc(ofsAddr, FC_FlushQueue, mode);
1569 moxaLowChkFlag[port] = 0;
1570 low_water_check(ofsAddr);
1574 int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port)
1578 int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
1579 void __user *argp = (void __user *)arg;
1581 if (port == QueryPort) {
1582 if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_INIT_DRIVER) &&
1583 (cmd != MOXA_LOAD_BIOS) && (cmd != MOXA_FIND_BOARD) && (cmd != MOXA_LOAD_C320B) &&
1584 (cmd != MOXA_LOAD_CODE) && (cmd != MOXA_GETDATACOUNT) &&
1585 (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
1586 (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
1591 if(copy_to_user(argp, &moxa_boards, MAX_BOARDS * sizeof(moxa_board_conf)))
1594 case MOXA_INIT_DRIVER:
1595 if ((int) arg == 0x404)
1598 case MOXA_GETDATACOUNT:
1599 moxaLog.tick = jiffies;
1600 if(copy_to_user(argp, &moxaLog, sizeof(mon_st)))
1603 case MOXA_FLUSH_QUEUE:
1604 MoxaPortFlushData(port, arg);
1606 case MOXA_GET_IOQUEUE:
1607 for (i = 0; i < MAX_PORTS; i++) {
1608 if (moxaChkPort[i]) {
1609 temp_queue[i].inq = MoxaPortRxQueue(i);
1610 temp_queue[i].outq = MoxaPortTxQueue(i);
1613 if(copy_to_user(argp, temp_queue, sizeof(struct moxaq_str) * MAX_PORTS))
1616 case MOXA_GET_OQUEUE:
1617 i = MoxaPortTxQueue(port);
1618 return put_user(i, (unsigned long __user *)argp);
1619 case MOXA_GET_IQUEUE:
1620 i = MoxaPortRxQueue(port);
1621 return put_user(i, (unsigned long __user *)argp);
1622 case MOXA_GET_MAJOR:
1623 if(copy_to_user(argp, &ttymajor, sizeof(int)))
1626 case MOXA_GET_CUMAJOR:
1628 if(copy_to_user(argp, &i, sizeof(int)))
1631 case MOXA_GETMSTATUS:
1632 for (i = 0; i < MAX_PORTS; i++) {
1634 GMStatus[i].dcd = 0;
1635 GMStatus[i].dsr = 0;
1636 GMStatus[i].cts = 0;
1637 if (!moxaChkPort[i]) {
1640 status = MoxaPortLineStatus(moxaChannels[i].port);
1642 GMStatus[i].cts = 1;
1644 GMStatus[i].dsr = 1;
1646 GMStatus[i].dcd = 1;
1649 if (!moxaChannels[i].tty || !moxaChannels[i].tty->termios)
1650 GMStatus[i].cflag = moxaChannels[i].cflag;
1652 GMStatus[i].cflag = moxaChannels[i].tty->termios->c_cflag;
1654 if(copy_to_user(argp, GMStatus, sizeof(struct mxser_mstatus) * MAX_PORTS))
1658 return (-ENOIOCTLCMD);
1659 case MOXA_LOAD_BIOS:
1660 case MOXA_FIND_BOARD:
1661 case MOXA_LOAD_C320B:
1662 case MOXA_LOAD_CODE:
1663 if (!capable(CAP_SYS_RAWIO))
1668 if(copy_from_user(&dltmp, argp, sizeof(struct dl_str)))
1670 if(dltmp.cardno < 0 || dltmp.cardno >= MAX_BOARDS)
1675 case MOXA_LOAD_BIOS:
1676 i = moxaloadbios(dltmp.cardno, dltmp.buf, dltmp.len);
1678 case MOXA_FIND_BOARD:
1679 return moxafindcard(dltmp.cardno);
1680 case MOXA_LOAD_C320B:
1681 moxaload320b(dltmp.cardno, dltmp.buf, dltmp.len);
1682 default: /* to keep gcc happy */
1684 case MOXA_LOAD_CODE:
1685 i = moxaloadcode(dltmp.cardno, dltmp.buf, dltmp.len);
1693 int MoxaDriverPoll(void)
1695 register ushort temp;
1697 void __iomem *ofsAddr;
1703 for (card = 0; card < MAX_BOARDS; card++) {
1704 if (loadstat[card] == 0)
1706 if ((ports = moxa_boards[card].numPorts) == 0)
1708 if (readb(moxaIntPend[card]) == 0xff) {
1709 ip = moxaIntTable[card] + readb(moxaIntNdx[card]);
1710 p = card * MAX_PORTS_PER_BOARD;
1712 for (port = 0; port < ports; port += 2, p++) {
1713 if ((temp = readw(ip + port)) != 0) {
1714 writew(0, ip + port);
1715 ofsAddr = moxaTableAddr[p];
1717 writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
1718 if (temp & IntrBreak) {
1721 if (temp & IntrLine) {
1722 if (readb(ofsAddr + FlagStat) & DCD_state) {
1723 if ((moxaDCDState[p] & DCD_oldstate) == 0)
1724 moxaDCDState[p] = (DCD_oldstate |
1727 if (moxaDCDState[p] & DCD_oldstate)
1728 moxaDCDState[p] = DCD_changed;
1733 writeb(0, moxaIntPend[card]);
1735 if (moxaLowWaterChk) {
1736 p = card * MAX_PORTS_PER_BOARD;
1737 for (port = 0; port < ports; port++, p++) {
1738 if (moxaLowChkFlag[p]) {
1739 moxaLowChkFlag[p] = 0;
1740 ofsAddr = moxaTableAddr[p];
1741 low_water_check(ofsAddr);
1746 moxaLowWaterChk = 0;
1750 /*****************************************************************************
1751 * Card level function: *
1752 * 1. MoxaPortsOfCard(int cardno); *
1753 *****************************************************************************/
1754 int MoxaPortsOfCard(int cardno)
1757 if (moxa_boards[cardno].boardType == 0)
1759 return (moxa_boards[cardno].numPorts);
1762 /*****************************************************************************
1763 * Port level functions: *
1764 * 1. MoxaPortIsValid(int port); *
1765 * 2. MoxaPortEnable(int port); *
1766 * 3. MoxaPortDisable(int port); *
1767 * 4. MoxaPortGetMaxBaud(int port); *
1768 * 5. MoxaPortGetCurBaud(int port); *
1769 * 6. MoxaPortSetBaud(int port, long baud); *
1770 * 7. MoxaPortSetMode(int port, int databit, int stopbit, int parity); *
1771 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1772 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1773 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1774 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1775 * 12. MoxaPortLineStatus(int port); *
1776 * 13. MoxaPortDCDChange(int port); *
1777 * 14. MoxaPortDCDON(int port); *
1778 * 15. MoxaPortFlushData(int port, int mode); *
1779 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1780 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
1781 * 18. MoxaPortTxBufSize(int port); *
1782 * 19. MoxaPortRxBufSize(int port); *
1783 * 20. MoxaPortTxQueue(int port); *
1784 * 21. MoxaPortTxFree(int port); *
1785 * 22. MoxaPortRxQueue(int port); *
1786 * 23. MoxaPortRxFree(int port); *
1787 * 24. MoxaPortTxDisable(int port); *
1788 * 25. MoxaPortTxEnable(int port); *
1789 * 26. MoxaPortGetBrkCnt(int port); *
1790 * 27. MoxaPortResetBrkCnt(int port); *
1791 * 28. MoxaPortSetXonXoff(int port, int xonValue, int xoffValue); *
1792 * 29. MoxaPortIsTxHold(int port); *
1793 * 30. MoxaPortSendBreak(int port, int ticks); *
1794 *****************************************************************************/
1796 * Moxa Port Number Description:
1798 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1799 * the port number using in MOXA driver functions will be 0 to 31 for
1800 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1801 * to 127 for fourth. For example, if you setup three MOXA boards,
1802 * first board is C218, second board is C320-16 and third board is
1803 * C320-32. The port number of first board (C218 - 8 ports) is from
1804 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1805 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1806 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1807 * 127 will be invalid.
1810 * Moxa Functions Description:
1812 * Function 1: Driver initialization routine, this routine must be
1813 * called when initialized driver.
1815 * void MoxaDriverInit();
1818 * Function 2: Moxa driver private IOCTL command processing.
1820 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1822 * unsigned int cmd : IOCTL command
1823 * unsigned long arg : IOCTL argument
1824 * int port : port number (0 - 127)
1831 * Function 3: Moxa driver polling process routine.
1833 * int MoxaDriverPoll(void);
1835 * return: 0 ; polling O.K.
1836 * -1 : no any Moxa card.
1839 * Function 4: Get the ports of this card.
1841 * int MoxaPortsOfCard(int cardno);
1843 * int cardno : card number (0 - 3)
1845 * return: 0 : this card is invalid
1849 * Function 5: Check this port is valid or invalid
1851 * int MoxaPortIsValid(int port);
1852 * int port : port number (0 - 127, ref port description)
1854 * return: 0 : this port is invalid
1855 * 1 : this port is valid
1858 * Function 6: Enable this port to start Tx/Rx data.
1860 * void MoxaPortEnable(int port);
1861 * int port : port number (0 - 127)
1864 * Function 7: Disable this port
1866 * void MoxaPortDisable(int port);
1867 * int port : port number (0 - 127)
1870 * Function 8: Get the maximun available baud rate of this port.
1872 * long MoxaPortGetMaxBaud(int port);
1873 * int port : port number (0 - 127)
1875 * return: 0 : this port is invalid
1876 * 38400/57600/115200 bps
1879 * Function 9: Get the current baud rate of this port.
1881 * long MoxaPortGetCurBaud(int port);
1882 * int port : port number (0 - 127)
1884 * return: 0 : this port is invalid
1888 * Function 10: Setting baud rate of this port.
1890 * long MoxaPortSetBaud(int port, long baud);
1891 * int port : port number (0 - 127)
1892 * long baud : baud rate (50 - 115200)
1894 * return: 0 : this port is invalid or baud < 50
1895 * 50 - 115200 : the real baud rate set to the port, if
1896 * the argument baud is large than maximun
1897 * available baud rate, the real setting
1898 * baud rate will be the maximun baud rate.
1901 * Function 11: Setting the data-bits/stop-bits/parity of this port
1903 * int MoxaPortSetMode(int port, int databits, int stopbits, int parity);
1904 * int port : port number (0 - 127)
1905 * int databits : data bits (8/7/6/5)
1906 * int stopbits : stop bits (2/1/0, 0 show 1.5 stop bits)
1907 int parity : parity (0:None,1:Odd,2:Even,3:Mark,4:Space)
1909 * return: -1 : invalid parameter
1913 * Function 12: Configure the port.
1915 * int MoxaPortSetTermio(int port, struct termios *termio, speed_t baud);
1916 * int port : port number (0 - 127)
1917 * struct termios * termio : termio structure pointer
1918 * speed_t baud : baud rate
1920 * return: -1 : this port is invalid or termio == NULL
1924 * Function 13: Get the DTR/RTS state of this port.
1926 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1927 * int port : port number (0 - 127)
1928 * int * dtrState : pointer to INT to receive the current DTR
1929 * state. (if NULL, this function will not
1930 * write to this address)
1931 * int * rtsState : pointer to INT to receive the current RTS
1932 * state. (if NULL, this function will not
1933 * write to this address)
1935 * return: -1 : this port is invalid
1939 * Function 14: Setting the DTR/RTS output state of this port.
1941 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1942 * int port : port number (0 - 127)
1943 * int dtrState : DTR output state (0: off, 1: on)
1944 * int rtsState : RTS output state (0: off, 1: on)
1947 * Function 15: Setting the flow control of this port.
1949 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1950 * int txFlow,int xany);
1951 * int port : port number (0 - 127)
1952 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1953 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1954 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1955 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1956 * int xany : S/W XANY flow control (0: no, 1: yes)
1959 * Function 16: Get ths line status of this port
1961 * int MoxaPortLineStatus(int port);
1962 * int port : port number (0 - 127)
1964 * return: Bit 0 - CTS state (0: off, 1: on)
1965 * Bit 1 - DSR state (0: off, 1: on)
1966 * Bit 2 - DCD state (0: off, 1: on)
1969 * Function 17: Check the DCD state has changed since the last read
1972 * int MoxaPortDCDChange(int port);
1973 * int port : port number (0 - 127)
1975 * return: 0 : no changed
1976 * 1 : DCD has changed
1979 * Function 18: Check ths current DCD state is ON or not.
1981 * int MoxaPortDCDON(int port);
1982 * int port : port number (0 - 127)
1984 * return: 0 : DCD off
1988 * Function 19: Flush the Rx/Tx buffer data of this port.
1990 * void MoxaPortFlushData(int port, int mode);
1991 * int port : port number (0 - 127)
1993 * 0 : flush the Rx buffer
1994 * 1 : flush the Tx buffer
1995 * 2 : flush the Rx and Tx buffer
1998 * Function 20: Write data.
2000 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
2001 * int port : port number (0 - 127)
2002 * unsigned char * buffer : pointer to write data buffer.
2003 * int length : write data length
2005 * return: 0 - length : real write data length
2008 * Function 21: Read data.
2010 * int MoxaPortReadData(int port, struct tty_struct *tty);
2011 * int port : port number (0 - 127)
2012 * struct tty_struct *tty : tty for data
2014 * return: 0 - length : real read data length
2017 * Function 22: Get the Tx buffer size of this port
2019 * int MoxaPortTxBufSize(int port);
2020 * int port : port number (0 - 127)
2022 * return: .. : Tx buffer size
2025 * Function 23: Get the Rx buffer size of this port
2027 * int MoxaPortRxBufSize(int port);
2028 * int port : port number (0 - 127)
2030 * return: .. : Rx buffer size
2033 * Function 24: Get the Tx buffer current queued data bytes
2035 * int MoxaPortTxQueue(int port);
2036 * int port : port number (0 - 127)
2038 * return: .. : Tx buffer current queued data bytes
2041 * Function 25: Get the Tx buffer current free space
2043 * int MoxaPortTxFree(int port);
2044 * int port : port number (0 - 127)
2046 * return: .. : Tx buffer current free space
2049 * Function 26: Get the Rx buffer current queued data bytes
2051 * int MoxaPortRxQueue(int port);
2052 * int port : port number (0 - 127)
2054 * return: .. : Rx buffer current queued data bytes
2057 * Function 27: Get the Rx buffer current free space
2059 * int MoxaPortRxFree(int port);
2060 * int port : port number (0 - 127)
2062 * return: .. : Rx buffer current free space
2065 * Function 28: Disable port data transmission.
2067 * void MoxaPortTxDisable(int port);
2068 * int port : port number (0 - 127)
2071 * Function 29: Enable port data transmission.
2073 * void MoxaPortTxEnable(int port);
2074 * int port : port number (0 - 127)
2077 * Function 30: Get the received BREAK signal count.
2079 * int MoxaPortGetBrkCnt(int port);
2080 * int port : port number (0 - 127)
2082 * return: 0 - .. : BREAK signal count
2085 * Function 31: Get the received BREAK signal count and reset it.
2087 * int MoxaPortResetBrkCnt(int port);
2088 * int port : port number (0 - 127)
2090 * return: 0 - .. : BREAK signal count
2093 * Function 32: Set the S/W flow control new XON/XOFF value, default
2094 * XON is 0x11 & XOFF is 0x13.
2096 * void MoxaPortSetXonXoff(int port, int xonValue, int xoffValue);
2097 * int port : port number (0 - 127)
2098 * int xonValue : new XON value (0 - 255)
2099 * int xoffValue : new XOFF value (0 - 255)
2102 * Function 33: Check this port's transmission is hold by remote site
2103 * because the flow control.
2105 * int MoxaPortIsTxHold(int port);
2106 * int port : port number (0 - 127)
2108 * return: 0 : normal
2109 * 1 : hold by remote site
2112 * Function 34: Send out a BREAK signal.
2114 * void MoxaPortSendBreak(int port, int ms100);
2115 * int port : port number (0 - 127)
2116 * int ms100 : break signal time interval.
2117 * unit: 100 mini-second. if ms100 == 0, it will
2118 * send out a about 250 ms BREAK signal.
2121 int MoxaPortIsValid(int port)
2126 if (moxaChkPort[port] == 0)
2131 void MoxaPortEnable(int port)
2133 void __iomem *ofsAddr;
2134 int MoxaPortLineStatus(int);
2135 short lowwater = 512;
2137 ofsAddr = moxaTableAddr[port];
2138 writew(lowwater, ofsAddr + Low_water);
2139 moxaBreakCnt[port] = 0;
2140 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2141 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2142 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
2144 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
2147 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
2148 moxafunc(ofsAddr, FC_FlushQueue, 2);
2150 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
2151 MoxaPortLineStatus(port);
2154 void MoxaPortDisable(int port)
2156 void __iomem *ofsAddr = moxaTableAddr[port];
2158 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
2159 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
2160 writew(0, ofsAddr + HostStat);
2161 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
2164 long MoxaPortGetMaxBaud(int port)
2166 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2167 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI))
2174 long MoxaPortSetBaud(int port, long baud)
2176 void __iomem *ofsAddr;
2180 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
2182 ofsAddr = moxaTableAddr[port];
2186 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
2187 else if (max == 57600L)
2188 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
2190 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
2192 moxafunc(ofsAddr, FC_SetBaud, val);
2194 moxaCurBaud[port] = baud;
2198 int MoxaPortSetTermio(int port, struct termios *termio, speed_t baud)
2200 void __iomem *ofsAddr;
2204 if (moxaChkPort[port] == 0 || termio == 0)
2206 ofsAddr = moxaTableAddr[port];
2207 cflag = termio->c_cflag; /* termio->c_cflag */
2209 mode = termio->c_cflag & CSIZE;
2212 else if (mode == CS6)
2214 else if (mode == CS7)
2216 else if (mode == CS8)
2219 if (termio->c_cflag & CSTOPB) {
2227 if (termio->c_cflag & PARENB) {
2228 if (termio->c_cflag & PARODD)
2235 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
2237 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2238 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2239 if (baud >= 921600L)
2242 MoxaPortSetBaud(port, baud);
2244 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
2245 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
2246 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
2247 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
2248 wait_finish(ofsAddr);
2254 int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState)
2257 if (!MoxaPortIsValid(port))
2260 if (moxaLineCtrl[port] & DTR_ON)
2266 if (moxaLineCtrl[port] & RTS_ON)
2274 void MoxaPortLineCtrl(int port, int dtr, int rts)
2276 void __iomem *ofsAddr;
2279 ofsAddr = moxaTableAddr[port];
2285 moxaLineCtrl[port] = mode;
2286 moxafunc(ofsAddr, FC_LineControl, mode);
2289 void MoxaPortFlowCtrl(int port, int rts, int cts, int txflow, int rxflow, int txany)
2291 void __iomem *ofsAddr;
2294 ofsAddr = moxaTableAddr[port];
2297 mode |= RTS_FlowCtl;
2299 mode |= CTS_FlowCtl;
2306 moxafunc(ofsAddr, FC_SetFlowCtl, mode);
2309 int MoxaPortLineStatus(int port)
2311 void __iomem *ofsAddr;
2314 ofsAddr = moxaTableAddr[port];
2315 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2316 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2317 moxafunc(ofsAddr, FC_LineStatus, 0);
2318 val = readw(ofsAddr + FuncArg);
2320 val = readw(ofsAddr + FlagStat) >> 4;
2325 if ((moxaDCDState[port] & DCD_oldstate) == 0)
2326 moxaDCDState[port] = (DCD_oldstate | DCD_changed);
2328 if (moxaDCDState[port] & DCD_oldstate)
2329 moxaDCDState[port] = DCD_changed;
2335 int MoxaPortDCDChange(int port)
2339 if (moxaChkPort[port] == 0)
2341 n = moxaDCDState[port];
2342 moxaDCDState[port] &= ~DCD_changed;
2347 int MoxaPortDCDON(int port)
2351 if (moxaChkPort[port] == 0)
2353 if (moxaDCDState[port] & DCD_oldstate)
2362 int MoxaDumpMem(int port, unsigned char * buffer, int len)
2365 unsigned long baseAddr,ofsAddr,ofs;
2367 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2368 ofs = baseAddr + DynPage_addr + pageofs;
2371 for (i = 0; i < len; i++)
2372 buffer[i] = readb(ofs+i);
2377 int MoxaPortWriteData(int port, unsigned char * buffer, int len)
2382 ushort head, tx_mask, spage, epage;
2383 ushort pageno, pageofs, bufhead;
2384 void __iomem *baseAddr, *ofsAddr, *ofs;
2386 ofsAddr = moxaTableAddr[port];
2387 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2388 tx_mask = readw(ofsAddr + TX_mask);
2389 spage = readw(ofsAddr + Page_txb);
2390 epage = readw(ofsAddr + EndPage_txb);
2391 tail = readw(ofsAddr + TXwptr);
2392 head = readw(ofsAddr + TXrptr);
2393 c = (head > tail) ? (head - tail - 1)
2394 : (head - tail + tx_mask);
2397 moxaLog.txcnt[port] += c;
2399 if (spage == epage) {
2400 bufhead = readw(ofsAddr + Ofs_txb);
2401 writew(spage, baseAddr + Control_reg);
2404 len = head - tail - 1;
2406 len = tx_mask + 1 - tail;
2407 len = (c > len) ? len : c;
2408 ofs = baseAddr + DynPage_addr + bufhead + tail;
2409 for (i = 0; i < len; i++)
2410 writeb(*buffer++, ofs + i);
2411 tail = (tail + len) & tx_mask;
2414 writew(tail, ofsAddr + TXwptr);
2417 pageno = spage + (tail >> 13);
2418 pageofs = tail & Page_mask;
2420 cnt = Page_size - pageofs;
2424 writeb(pageno, baseAddr + Control_reg);
2425 ofs = baseAddr + DynPage_addr + pageofs;
2426 for (i = 0; i < cnt; i++)
2427 writeb(*buffer++, ofs + i);
2429 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2432 if (++pageno == epage)
2437 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2441 int MoxaPortReadData(int port, struct tty_struct *tty)
2443 register ushort head, pageofs;
2444 int i, count, cnt, len, total, remain;
2445 ushort tail, rx_mask, spage, epage;
2446 ushort pageno, bufhead;
2447 void __iomem *baseAddr, *ofsAddr, *ofs;
2449 ofsAddr = moxaTableAddr[port];
2450 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2451 head = readw(ofsAddr + RXrptr);
2452 tail = readw(ofsAddr + RXwptr);
2453 rx_mask = readw(ofsAddr + RX_mask);
2454 spage = readw(ofsAddr + Page_rxb);
2455 epage = readw(ofsAddr + EndPage_rxb);
2456 count = (tail >= head) ? (tail - head)
2457 : (tail - head + rx_mask + 1);
2462 remain = count - total;
2463 moxaLog.rxcnt[port] += total;
2465 if (spage == epage) {
2466 bufhead = readw(ofsAddr + Ofs_rxb);
2467 writew(spage, baseAddr + Control_reg);
2472 len = rx_mask + 1 - head;
2473 len = (count > len) ? len : count;
2474 ofs = baseAddr + DynPage_addr + bufhead + head;
2475 for (i = 0; i < len; i++)
2476 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2477 head = (head + len) & rx_mask;
2480 writew(head, ofsAddr + RXrptr);
2483 pageno = spage + (head >> 13);
2484 pageofs = head & Page_mask;
2486 cnt = Page_size - pageofs;
2490 writew(pageno, baseAddr + Control_reg);
2491 ofs = baseAddr + DynPage_addr + pageofs;
2492 for (i = 0; i < cnt; i++)
2493 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2495 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2498 if (++pageno == epage)
2503 if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2504 moxaLowWaterChk = 1;
2505 moxaLowChkFlag[port] = 1;
2511 int MoxaPortTxQueue(int port)
2513 void __iomem *ofsAddr;
2514 ushort rptr, wptr, mask;
2517 ofsAddr = moxaTableAddr[port];
2518 rptr = readw(ofsAddr + TXrptr);
2519 wptr = readw(ofsAddr + TXwptr);
2520 mask = readw(ofsAddr + TX_mask);
2521 len = (wptr - rptr) & mask;
2525 int MoxaPortTxFree(int port)
2527 void __iomem *ofsAddr;
2528 ushort rptr, wptr, mask;
2531 ofsAddr = moxaTableAddr[port];
2532 rptr = readw(ofsAddr + TXrptr);
2533 wptr = readw(ofsAddr + TXwptr);
2534 mask = readw(ofsAddr + TX_mask);
2535 len = mask - ((wptr - rptr) & mask);
2539 int MoxaPortRxQueue(int port)
2541 void __iomem *ofsAddr;
2542 ushort rptr, wptr, mask;
2545 ofsAddr = moxaTableAddr[port];
2546 rptr = readw(ofsAddr + RXrptr);
2547 wptr = readw(ofsAddr + RXwptr);
2548 mask = readw(ofsAddr + RX_mask);
2549 len = (wptr - rptr) & mask;
2554 void MoxaPortTxDisable(int port)
2556 void __iomem *ofsAddr;
2558 ofsAddr = moxaTableAddr[port];
2559 moxafunc(ofsAddr, FC_SetXoffState, Magic_code);
2562 void MoxaPortTxEnable(int port)
2564 void __iomem *ofsAddr;
2566 ofsAddr = moxaTableAddr[port];
2567 moxafunc(ofsAddr, FC_SetXonState, Magic_code);
2571 int MoxaPortResetBrkCnt(int port)
2574 cnt = moxaBreakCnt[port];
2575 moxaBreakCnt[port] = 0;
2580 void MoxaPortSendBreak(int port, int ms100)
2582 void __iomem *ofsAddr;
2584 ofsAddr = moxaTableAddr[port];
2586 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2587 moxadelay(ms100 * (HZ / 10));
2589 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2590 moxadelay(HZ / 4); /* 250 ms */
2592 moxafunc(ofsAddr, FC_StopBreak, Magic_code);
2595 static int moxa_get_serial_info(struct moxa_str *info,
2596 struct serial_struct __user *retinfo)
2598 struct serial_struct tmp;
2600 memset(&tmp, 0, sizeof(tmp));
2601 tmp.type = info->type;
2602 tmp.line = info->port;
2605 tmp.flags = info->asyncflags;
2606 tmp.baud_base = 921600;
2607 tmp.close_delay = info->close_delay;
2608 tmp.closing_wait = info->closing_wait;
2609 tmp.custom_divisor = 0;
2611 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2617 static int moxa_set_serial_info(struct moxa_str *info,
2618 struct serial_struct __user *new_info)
2620 struct serial_struct new_serial;
2622 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2625 if ((new_serial.irq != 0) ||
2626 (new_serial.port != 0) ||
2627 // (new_serial.type != info->type) ||
2628 (new_serial.custom_divisor != 0) ||
2629 (new_serial.baud_base != 921600))
2632 if (!capable(CAP_SYS_ADMIN)) {
2633 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2634 (info->asyncflags & ~ASYNC_USR_MASK)))
2637 info->close_delay = new_serial.close_delay * HZ / 100;
2638 info->closing_wait = new_serial.closing_wait * HZ / 100;
2641 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2642 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2644 if (new_serial.type == PORT_16550A) {
2645 MoxaSetFifo(info->port, 1);
2647 MoxaSetFifo(info->port, 0);
2650 info->type = new_serial.type;
2656 /*****************************************************************************
2657 * Static local functions: *
2658 *****************************************************************************/
2660 * moxadelay - delays a specified number ticks
2662 static void moxadelay(int tick)
2664 unsigned long st, et;
2668 while (time_before(jiffies, et));
2671 static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
2674 writew(arg, ofsAddr + FuncArg);
2675 writew(cmd, ofsAddr + FuncCode);
2676 wait_finish(ofsAddr);
2679 static void wait_finish(void __iomem *ofsAddr)
2684 while (readw(ofsAddr + FuncCode) != 0) {
2686 if ((j - i) > moxaFuncTout) {
2692 static void low_water_check(void __iomem *ofsAddr)
2695 ushort rptr, wptr, mask;
2697 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2698 rptr = readw(ofsAddr + RXrptr);
2699 wptr = readw(ofsAddr + RXwptr);
2700 mask = readw(ofsAddr + RX_mask);
2701 len = (wptr - rptr) & mask;
2702 if (len <= Low_water)
2703 moxafunc(ofsAddr, FC_SendXon, 0);
2707 static int moxaloadbios(int cardno, unsigned char __user *tmp, int len)
2709 void __iomem *baseAddr;
2712 if(copy_from_user(moxaBuff, tmp, len))
2714 baseAddr = moxaBaseAddr[cardno];
2715 writeb(HW_reset, baseAddr + Control_reg); /* reset */
2716 moxadelay(1); /* delay 10 ms */
2717 for (i = 0; i < 4096; i++)
2718 writeb(0, baseAddr + i); /* clear fix page */
2719 for (i = 0; i < len; i++)
2720 writeb(moxaBuff[i], baseAddr + i); /* download BIOS */
2721 writeb(0, baseAddr + Control_reg); /* restart */
2725 static int moxafindcard(int cardno)
2727 void __iomem *baseAddr;
2730 baseAddr = moxaBaseAddr[cardno];
2731 switch (moxa_boards[cardno].boardType) {
2732 case MOXA_BOARD_C218_ISA:
2733 case MOXA_BOARD_C218_PCI:
2734 if ((tmp = readw(baseAddr + C218_key)) != C218_KeyCode) {
2738 case MOXA_BOARD_CP204J:
2739 if ((tmp = readw(baseAddr + C218_key)) != CP204J_KeyCode) {
2744 if ((tmp = readw(baseAddr + C320_key)) != C320_KeyCode) {
2747 if ((tmp = readw(baseAddr + C320_status)) != STS_init) {
2754 static int moxaload320b(int cardno, unsigned char __user *tmp, int len)
2756 void __iomem *baseAddr;
2759 if(len > sizeof(moxaBuff))
2761 if(copy_from_user(moxaBuff, tmp, len))
2763 baseAddr = moxaBaseAddr[cardno];
2764 writew(len - 7168 - 2, baseAddr + C320bapi_len);
2765 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
2766 for (i = 0; i < 7168; i++)
2767 writeb(moxaBuff[i], baseAddr + DynPage_addr + i);
2768 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
2769 for (i = 0; i < (len - 7168); i++)
2770 writeb(moxaBuff[i + 7168], baseAddr + DynPage_addr + i);
2774 static int moxaloadcode(int cardno, unsigned char __user *tmp, int len)
2776 void __iomem *baseAddr, *ofsAddr;
2777 int retval, port, i;
2779 if(copy_from_user(moxaBuff, tmp, len))
2781 baseAddr = moxaBaseAddr[cardno];
2782 switch (moxa_boards[cardno].boardType) {
2783 case MOXA_BOARD_C218_ISA:
2784 case MOXA_BOARD_C218_PCI:
2785 case MOXA_BOARD_CP204J:
2786 retval = moxaloadc218(cardno, baseAddr, len);
2789 port = cardno * MAX_PORTS_PER_BOARD;
2790 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2791 moxaChkPort[port] = 1;
2792 moxaCurBaud[port] = 9600L;
2793 moxaDCDState[port] = 0;
2794 moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
2795 ofsAddr = moxaTableAddr[port];
2796 writew(C218rx_mask, ofsAddr + RX_mask);
2797 writew(C218tx_mask, ofsAddr + TX_mask);
2798 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
2799 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
2801 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
2802 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
2807 retval = moxaloadc320(cardno, baseAddr, len,
2808 &moxa_boards[cardno].numPorts);
2811 port = cardno * MAX_PORTS_PER_BOARD;
2812 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2813 moxaChkPort[port] = 1;
2814 moxaCurBaud[port] = 9600L;
2815 moxaDCDState[port] = 0;
2816 moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
2817 ofsAddr = moxaTableAddr[port];
2818 if (moxa_boards[cardno].numPorts == 8) {
2819 writew(C320p8rx_mask, ofsAddr + RX_mask);
2820 writew(C320p8tx_mask, ofsAddr + TX_mask);
2821 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
2822 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
2823 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
2824 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
2826 } else if (moxa_boards[cardno].numPorts == 16) {
2827 writew(C320p16rx_mask, ofsAddr + RX_mask);
2828 writew(C320p16tx_mask, ofsAddr + TX_mask);
2829 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
2830 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
2831 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
2832 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
2834 } else if (moxa_boards[cardno].numPorts == 24) {
2835 writew(C320p24rx_mask, ofsAddr + RX_mask);
2836 writew(C320p24tx_mask, ofsAddr + TX_mask);
2837 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
2838 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
2839 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
2840 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2841 } else if (moxa_boards[cardno].numPorts == 32) {
2842 writew(C320p32rx_mask, ofsAddr + RX_mask);
2843 writew(C320p32tx_mask, ofsAddr + TX_mask);
2844 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
2845 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
2846 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
2847 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
2848 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2853 loadstat[cardno] = 1;
2857 static int moxaloadc218(int cardno, void __iomem *baseAddr, int len)
2860 int i, j, len1, len2;
2861 ushort usum, *ptr, keycode;
2863 if (moxa_boards[cardno].boardType == MOXA_BOARD_CP204J)
2864 keycode = CP204J_KeyCode;
2866 keycode = C218_KeyCode;
2869 ptr = (ushort *) moxaBuff;
2870 for (i = 0; i < len1; i++)
2871 usum += le16_to_cpu(*(ptr + i));
2877 len2 = (len1 > 2048) ? 2048 : len1;
2879 for (i = 0; i < len2 << 1; i++)
2880 writeb(moxaBuff[i + j], baseAddr + C218_LoadBuf + i);
2883 writew(len2, baseAddr + C218DLoad_len);
2884 writew(0, baseAddr + C218_key);
2885 for (i = 0; i < 100; i++) {
2886 if (readw(baseAddr + C218_key) == keycode)
2888 moxadelay(1); /* delay 10 ms */
2890 if (readw(baseAddr + C218_key) != keycode) {
2894 writew(0, baseAddr + C218DLoad_len);
2895 writew(usum, baseAddr + C218check_sum);
2896 writew(0, baseAddr + C218_key);
2897 for (i = 0; i < 100; i++) {
2898 if (readw(baseAddr + C218_key) == keycode)
2900 moxadelay(1); /* delay 10 ms */
2903 } while ((readb(baseAddr + C218chksum_ok) != 1) && (retry < 3));
2904 if (readb(baseAddr + C218chksum_ok) != 1) {
2907 writew(0, baseAddr + C218_key);
2908 for (i = 0; i < 100; i++) {
2909 if (readw(baseAddr + Magic_no) == Magic_code)
2911 moxadelay(1); /* delay 10 ms */
2913 if (readw(baseAddr + Magic_no) != Magic_code) {
2916 writew(1, baseAddr + Disable_IRQ);
2917 writew(0, baseAddr + Magic_no);
2918 for (i = 0; i < 100; i++) {
2919 if (readw(baseAddr + Magic_no) == Magic_code)
2921 moxadelay(1); /* delay 10 ms */
2923 if (readw(baseAddr + Magic_no) != Magic_code) {
2927 moxaIntNdx[cardno] = baseAddr + IRQindex;
2928 moxaIntPend[cardno] = baseAddr + IRQpending;
2929 moxaIntTable[cardno] = baseAddr + IRQtable;
2933 static int moxaloadc320(int cardno, void __iomem *baseAddr, int len, int *numPorts)
2936 int i, j, wlen, len2, retry;
2941 uptr = (ushort *) moxaBuff;
2942 for (i = 0; i < wlen; i++)
2943 usum += le16_to_cpu(uptr[i]);
2954 for (i = 0; i < len2; i++)
2955 writeb(moxaBuff[j + i], baseAddr + C320_LoadBuf + i);
2958 writew(len2, baseAddr + C320DLoad_len);
2959 writew(0, baseAddr + C320_key);
2960 for (i = 0; i < 10; i++) {
2961 if (readw(baseAddr + C320_key) == C320_KeyCode)
2965 if (readw(baseAddr + C320_key) != C320_KeyCode)
2968 writew(0, baseAddr + C320DLoad_len);
2969 writew(usum, baseAddr + C320check_sum);
2970 writew(0, baseAddr + C320_key);
2971 for (i = 0; i < 10; i++) {
2972 if (readw(baseAddr + C320_key) == C320_KeyCode)
2977 } while ((readb(baseAddr + C320chksum_ok) != 1) && (retry < 3));
2978 if (readb(baseAddr + C320chksum_ok) != 1)
2980 writew(0, baseAddr + C320_key);
2981 for (i = 0; i < 600; i++) {
2982 if (readw(baseAddr + Magic_no) == Magic_code)
2986 if (readw(baseAddr + Magic_no) != Magic_code)
2989 if (moxa_boards[cardno].busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
2990 writew(0x3800, baseAddr + TMS320_PORT1);
2991 writew(0x3900, baseAddr + TMS320_PORT2);
2992 writew(28499, baseAddr + TMS320_CLOCK);
2994 writew(0x3200, baseAddr + TMS320_PORT1);
2995 writew(0x3400, baseAddr + TMS320_PORT2);
2996 writew(19999, baseAddr + TMS320_CLOCK);
2998 writew(1, baseAddr + Disable_IRQ);
2999 writew(0, baseAddr + Magic_no);
3000 for (i = 0; i < 500; i++) {
3001 if (readw(baseAddr + Magic_no) == Magic_code)
3005 if (readw(baseAddr + Magic_no) != Magic_code)
3008 j = readw(baseAddr + Module_cnt);
3012 writew(j, baseAddr + Module_no);
3013 writew(0, baseAddr + Magic_no);
3014 for (i = 0; i < 600; i++) {
3015 if (readw(baseAddr + Magic_no) == Magic_code)
3019 if (readw(baseAddr + Magic_no) != Magic_code)
3022 moxaIntNdx[cardno] = baseAddr + IRQindex;
3023 moxaIntPend[cardno] = baseAddr + IRQpending;
3024 moxaIntTable[cardno] = baseAddr + IRQtable;
3029 long MoxaPortGetCurBaud(int port)
3032 if (moxaChkPort[port] == 0)
3034 return (moxaCurBaud[port]);
3038 static void MoxaSetFifo(int port, int enable)
3040 void __iomem *ofsAddr = moxaTableAddr[port];
3043 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
3044 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
3046 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
3047 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
3052 int MoxaPortSetMode(int port, int databits, int stopbits, int parity)
3054 void __iomem *ofsAddr;
3077 break; /* stop bits 1.5 */
3106 ofsAddr = moxaTableAddr[port];
3107 moxafunc(ofsAddr, FC_SetMode, val);
3111 int MoxaPortTxBufSize(int port)
3113 void __iomem *ofsAddr;
3116 ofsAddr = moxaTableAddr[port];
3117 size = readw(ofsAddr + TX_mask);
3121 int MoxaPortRxBufSize(int port)
3123 void __iomem *ofsAddr;
3126 ofsAddr = moxaTableAddr[port];
3127 size = readw(ofsAddr + RX_mask);
3131 int MoxaPortRxFree(int port)
3133 void __iomem *ofsAddr;
3134 ushort rptr, wptr, mask;
3137 ofsAddr = moxaTableAddr[port];
3138 rptr = readw(ofsAddr + RXrptr);
3139 wptr = readw(ofsAddr + RXwptr);
3140 mask = readw(ofsAddr + RX_mask);
3141 len = mask - ((wptr - rptr) & mask);
3144 int MoxaPortGetBrkCnt(int port)
3146 return (moxaBreakCnt[port]);
3149 void MoxaPortSetXonXoff(int port, int xonValue, int xoffValue)
3151 void __iomem *ofsAddr;
3153 ofsAddr = moxaTableAddr[port];
3154 writew(xonValue, ofsAddr + FuncArg);
3155 writew(xoffValue, ofsAddr + FuncArg1);
3156 writew(FC_SetXonXoff, ofsAddr + FuncCode);
3157 wait_finish(ofsAddr);
3160 int MoxaPortIsTxHold(int port)
3162 void __iomem *ofsAddr;
3165 ofsAddr = moxaTableAddr[port];
3166 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
3167 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
3168 moxafunc(ofsAddr, FC_GetCCSR, 0);
3169 val = readw(ofsAddr + FuncArg);
3173 if (readw(ofsAddr + FlagStat) & Tx_flowOff)