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 {
135 typedef struct _moxa_board_conf {
138 unsigned long baseAddr;
140 moxa_pci_devinfo pciInfo;
143 static moxa_board_conf moxa_boards[MAX_BOARDS];
144 static void __iomem *moxaBaseAddr[MAX_BOARDS];
145 static int loadstat[MAX_BOARDS];
151 unsigned short closing_wait;
154 long event; /* long req'd for set_bit --RR */
156 unsigned long statusflags;
157 struct tty_struct *tty;
159 wait_queue_head_t open_wait;
160 wait_queue_head_t close_wait;
161 struct work_struct tqueue;
164 struct mxser_mstatus {
172 static struct mxser_mstatus GMStatus[MAX_PORTS];
175 #define TXSTOPPED 0x1
177 #define EMPTYWAIT 0x4
181 #define MOXA_EVENT_HANGUP 1
183 #define SERIAL_DO_RESTART
186 #define SERIAL_TYPE_NORMAL 1
188 #define WAKEUP_CHARS 256
190 #define PORTNO(x) ((x)->index)
192 static int verbose = 0;
193 static int ttymajor = MOXAMAJOR;
194 /* Variables for insmod */
196 static int baseaddr[] = {0, 0, 0, 0};
197 static int type[] = {0, 0, 0, 0};
198 static int numports[] = {0, 0, 0, 0};
201 MODULE_AUTHOR("William Chen");
202 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
203 MODULE_LICENSE("GPL");
205 module_param_array(type, int, NULL, 0);
206 module_param_array(baseaddr, int, NULL, 0);
207 module_param_array(numports, int, NULL, 0);
209 module_param(ttymajor, int, 0);
210 module_param(verbose, bool, 0644);
212 static struct tty_driver *moxaDriver;
213 static struct moxa_str moxaChannels[MAX_PORTS];
214 static unsigned char *moxaXmitBuff;
215 static int moxaTimer_on;
216 static struct timer_list moxaTimer;
217 static int moxaEmptyTimer_on[MAX_PORTS];
218 static struct timer_list moxaEmptyTimer[MAX_PORTS];
219 static struct semaphore moxaBuffSem;
224 static void do_moxa_softint(void *);
225 static int moxa_open(struct tty_struct *, struct file *);
226 static void moxa_close(struct tty_struct *, struct file *);
227 static int moxa_write(struct tty_struct *, const unsigned char *, int);
228 static int moxa_write_room(struct tty_struct *);
229 static void moxa_flush_buffer(struct tty_struct *);
230 static int moxa_chars_in_buffer(struct tty_struct *);
231 static void moxa_flush_chars(struct tty_struct *);
232 static void moxa_put_char(struct tty_struct *, unsigned char);
233 static int moxa_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);
234 static void moxa_throttle(struct tty_struct *);
235 static void moxa_unthrottle(struct tty_struct *);
236 static void moxa_set_termios(struct tty_struct *, struct termios *);
237 static void moxa_stop(struct tty_struct *);
238 static void moxa_start(struct tty_struct *);
239 static void moxa_hangup(struct tty_struct *);
240 static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
241 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
242 unsigned int set, unsigned int clear);
243 static void moxa_poll(unsigned long);
244 static void set_tty_param(struct tty_struct *);
245 static int block_till_ready(struct tty_struct *, struct file *,
247 static void setup_empty_event(struct tty_struct *);
248 static void check_xmit_empty(unsigned long);
249 static void shut_down(struct moxa_str *);
250 static void receive_data(struct moxa_str *);
252 * moxa board interface functions:
254 static void MoxaDriverInit(void);
255 static int MoxaDriverIoctl(unsigned int, unsigned long, int);
256 static int MoxaDriverPoll(void);
257 static int MoxaPortsOfCard(int);
258 static int MoxaPortIsValid(int);
259 static void MoxaPortEnable(int);
260 static void MoxaPortDisable(int);
261 static long MoxaPortGetMaxBaud(int);
262 static long MoxaPortSetBaud(int, long);
263 static int MoxaPortSetTermio(int, struct termios *, speed_t);
264 static int MoxaPortGetLineOut(int, int *, int *);
265 static void MoxaPortLineCtrl(int, int, int);
266 static void MoxaPortFlowCtrl(int, int, int, int, int, int);
267 static int MoxaPortLineStatus(int);
268 static int MoxaPortDCDChange(int);
269 static int MoxaPortDCDON(int);
270 static void MoxaPortFlushData(int, int);
271 static int MoxaPortWriteData(int, unsigned char *, int);
272 static int MoxaPortReadData(int, struct tty_struct *tty);
273 static int MoxaPortTxQueue(int);
274 static int MoxaPortRxQueue(int);
275 static int MoxaPortTxFree(int);
276 static void MoxaPortTxDisable(int);
277 static void MoxaPortTxEnable(int);
278 static int MoxaPortResetBrkCnt(int);
279 static void MoxaPortSendBreak(int, int);
280 static int moxa_get_serial_info(struct moxa_str *, struct serial_struct __user *);
281 static int moxa_set_serial_info(struct moxa_str *, struct serial_struct __user *);
282 static void MoxaSetFifo(int port, int enable);
284 static const struct tty_operations moxa_ops = {
288 .write_room = moxa_write_room,
289 .flush_buffer = moxa_flush_buffer,
290 .chars_in_buffer = moxa_chars_in_buffer,
291 .flush_chars = moxa_flush_chars,
292 .put_char = moxa_put_char,
294 .throttle = moxa_throttle,
295 .unthrottle = moxa_unthrottle,
296 .set_termios = moxa_set_termios,
299 .hangup = moxa_hangup,
300 .tiocmget = moxa_tiocmget,
301 .tiocmset = moxa_tiocmset,
304 static DEFINE_SPINLOCK(moxa_lock);
307 static int moxa_get_PCI_conf(struct pci_dev *p, int board_type, moxa_board_conf * board)
309 board->baseAddr = pci_resource_start (p, 2);
310 board->boardType = board_type;
311 switch (board_type) {
312 case MOXA_BOARD_C218_ISA:
313 case MOXA_BOARD_C218_PCI:
317 case MOXA_BOARD_CP204J:
324 board->busType = MOXA_BUS_TYPE_PCI;
325 board->pciInfo.busNum = p->bus->number;
326 board->pciInfo.devNum = p->devfn >> 3;
330 #endif /* CONFIG_PCI */
332 static int __init moxa_init(void)
337 printk(KERN_INFO "MOXA Intellio family driver version %s\n", MOXA_VERSION);
338 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
342 init_MUTEX(&moxaBuffSem);
343 moxaDriver->owner = THIS_MODULE;
344 moxaDriver->name = "ttyMX";
345 moxaDriver->major = ttymajor;
346 moxaDriver->minor_start = 0;
347 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
348 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
349 moxaDriver->init_termios = tty_std_termios;
350 moxaDriver->init_termios.c_iflag = 0;
351 moxaDriver->init_termios.c_oflag = 0;
352 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
353 moxaDriver->init_termios.c_lflag = 0;
354 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
355 tty_set_operations(moxaDriver, &moxa_ops);
359 for (i = 0, ch = moxaChannels; i < MAX_PORTS; i++, ch++) {
360 ch->type = PORT_16550A;
362 INIT_WORK(&ch->tqueue, do_moxa_softint, ch);
364 ch->close_delay = 5 * HZ / 10;
365 ch->closing_wait = 30 * HZ;
367 ch->blocked_open = 0;
368 ch->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
369 init_waitqueue_head(&ch->open_wait);
370 init_waitqueue_head(&ch->close_wait);
373 for (i = 0; i < MAX_BOARDS; i++) {
374 moxa_boards[i].boardType = 0;
375 moxa_boards[i].numPorts = 0;
376 moxa_boards[i].baseAddr = 0;
377 moxa_boards[i].busType = 0;
378 moxa_boards[i].pciInfo.busNum = 0;
379 moxa_boards[i].pciInfo.devNum = 0;
382 printk("Tty devices major number = %d\n", ttymajor);
384 if (tty_register_driver(moxaDriver)) {
385 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
386 put_tty_driver(moxaDriver);
389 for (i = 0; i < MAX_PORTS; i++) {
390 init_timer(&moxaEmptyTimer[i]);
391 moxaEmptyTimer[i].function = check_xmit_empty;
392 moxaEmptyTimer[i].data = (unsigned long) & moxaChannels[i];
393 moxaEmptyTimer_on[i] = 0;
396 init_timer(&moxaTimer);
397 moxaTimer.function = moxa_poll;
398 moxaTimer.expires = jiffies + (HZ / 50);
400 add_timer(&moxaTimer);
402 /* Find the boards defined in source code */
404 for (i = 0; i < MAX_BOARDS; i++) {
405 if ((moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA) ||
406 (moxa_isa_boards[i].boardType == MOXA_BOARD_C320_ISA)) {
407 moxa_boards[numBoards].boardType = moxa_isa_boards[i].boardType;
408 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
409 moxa_boards[numBoards].numPorts = 8;
411 moxa_boards[numBoards].numPorts = moxa_isa_boards[i].numPorts;
412 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
413 moxa_boards[numBoards].baseAddr = moxa_isa_boards[i].baseAddr;
415 printk("Board %2d: %s board(baseAddr=%lx)\n",
417 moxa_brdname[moxa_boards[numBoards].boardType - 1],
418 moxa_boards[numBoards].baseAddr);
422 /* Find the boards defined form module args. */
424 for (i = 0; i < MAX_BOARDS; i++) {
425 if ((type[i] == MOXA_BOARD_C218_ISA) ||
426 (type[i] == MOXA_BOARD_C320_ISA)) {
428 printk("Board %2d: %s board(baseAddr=%lx)\n",
430 moxa_brdname[type[i] - 1],
431 (unsigned long) baseaddr[i]);
432 if (numBoards >= MAX_BOARDS) {
434 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
437 moxa_boards[numBoards].boardType = type[i];
438 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
439 moxa_boards[numBoards].numPorts = 8;
441 moxa_boards[numBoards].numPorts = numports[i];
442 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
443 moxa_boards[numBoards].baseAddr = baseaddr[i];
448 /* Find PCI boards here */
451 struct pci_dev *p = NULL;
452 int n = ARRAY_SIZE(moxa_pcibrds) - 1;
455 while ((p = pci_get_device(moxa_pcibrds[i].vendor, moxa_pcibrds[i].device, p))!=NULL)
457 if (pci_enable_device(p))
459 if (numBoards >= MAX_BOARDS) {
461 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
463 moxa_get_PCI_conf(p, moxa_pcibrds[i].driver_data,
464 &moxa_boards[numBoards]);
472 for (i = 0; i < numBoards; i++) {
473 moxaBaseAddr[i] = ioremap((unsigned long) moxa_boards[i].baseAddr, 0x4000);
479 static void __exit moxa_exit(void)
484 printk("Unloading module moxa ...\n");
487 del_timer(&moxaTimer);
489 for (i = 0; i < MAX_PORTS; i++)
490 if (moxaEmptyTimer_on[i])
491 del_timer(&moxaEmptyTimer[i]);
493 if (tty_unregister_driver(moxaDriver))
494 printk("Couldn't unregister MOXA Intellio family serial driver\n");
495 put_tty_driver(moxaDriver);
500 module_init(moxa_init);
501 module_exit(moxa_exit);
503 static void do_moxa_softint(void *private_)
505 struct moxa_str *ch = (struct moxa_str *) private_;
506 struct tty_struct *tty;
508 if (ch && (tty = ch->tty)) {
509 if (test_and_clear_bit(MOXA_EVENT_HANGUP, &ch->event)) {
510 tty_hangup(tty); /* FIXME: module removal race here - AKPM */
511 wake_up_interruptible(&ch->open_wait);
512 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
517 static int moxa_open(struct tty_struct *tty, struct file *filp)
525 if (port == MAX_PORTS) {
528 if (!MoxaPortIsValid(port)) {
529 tty->driver_data = NULL;
534 page = get_zeroed_page(GFP_KERNEL);
539 /* This test is guarded by the BuffSem so no longer needed
544 moxaXmitBuff = (unsigned char *) page;
548 ch = &moxaChannels[port];
550 tty->driver_data = ch;
552 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
555 MoxaPortLineCtrl(ch->port, 1, 1);
556 MoxaPortEnable(ch->port);
557 ch->asyncflags |= ASYNC_INITIALIZED;
559 retval = block_till_ready(tty, filp, ch);
561 moxa_unthrottle(tty);
563 if (ch->type == PORT_16550A) {
564 MoxaSetFifo(ch->port, 1);
566 MoxaSetFifo(ch->port, 0);
572 static void moxa_close(struct tty_struct *tty, struct file *filp)
578 if (port == MAX_PORTS) {
581 if (!MoxaPortIsValid(port)) {
582 #ifdef SERIAL_DEBUG_CLOSE
583 printk("Invalid portno in moxa_close\n");
585 tty->driver_data = NULL;
588 if (tty->driver_data == NULL) {
591 if (tty_hung_up_p(filp)) {
594 ch = (struct moxa_str *) tty->driver_data;
596 if ((tty->count == 1) && (ch->count != 1)) {
597 printk("moxa_close: bad serial port count; tty->count is 1, "
598 "ch->count is %d\n", ch->count);
601 if (--ch->count < 0) {
602 printk("moxa_close: bad serial port count, device=%s\n",
609 ch->asyncflags |= ASYNC_CLOSING;
611 ch->cflag = tty->termios->c_cflag;
612 if (ch->asyncflags & ASYNC_INITIALIZED) {
613 setup_empty_event(tty);
614 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
615 moxaEmptyTimer_on[ch->port] = 0;
616 del_timer(&moxaEmptyTimer[ch->port]);
619 MoxaPortFlushData(port, 2);
621 if (tty->driver->flush_buffer)
622 tty->driver->flush_buffer(tty);
623 tty_ldisc_flush(tty);
628 if (ch->blocked_open) {
629 if (ch->close_delay) {
630 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
632 wake_up_interruptible(&ch->open_wait);
634 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
635 wake_up_interruptible(&ch->close_wait);
638 static int moxa_write(struct tty_struct *tty,
639 const unsigned char *buf, int count)
645 ch = (struct moxa_str *) tty->driver_data;
650 spin_lock_irqsave(&moxa_lock, flags);
651 len = MoxaPortWriteData(port, (unsigned char *) buf, count);
652 spin_unlock_irqrestore(&moxa_lock, flags);
654 /*********************************************
655 if ( !(ch->statusflags & LOWWAIT) &&
656 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
657 ************************************************/
658 ch->statusflags |= LOWWAIT;
662 static int moxa_write_room(struct tty_struct *tty)
668 ch = (struct moxa_str *) tty->driver_data;
671 return (MoxaPortTxFree(ch->port));
674 static void moxa_flush_buffer(struct tty_struct *tty)
676 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
680 MoxaPortFlushData(ch->port, 1);
684 static int moxa_chars_in_buffer(struct tty_struct *tty)
687 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
690 * Sigh...I have to check if driver_data is NULL here, because
691 * if an open() fails, the TTY subsystem eventually calls
692 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
693 * routine. And since the open() failed, we return 0 here. TDJ
697 chars = MoxaPortTxQueue(ch->port);
700 * Make it possible to wakeup anything waiting for output
701 * in tty_ioctl.c, etc.
703 if (!(ch->statusflags & EMPTYWAIT))
704 setup_empty_event(tty);
709 static void moxa_flush_chars(struct tty_struct *tty)
712 * Don't think I need this, because this is called to empty the TX
713 * buffer for the 16450, 16550, etc.
717 static void moxa_put_char(struct tty_struct *tty, unsigned char c)
723 ch = (struct moxa_str *) tty->driver_data;
727 spin_lock_irqsave(&moxa_lock, flags);
729 MoxaPortWriteData(port, moxaXmitBuff, 1);
730 spin_unlock_irqrestore(&moxa_lock, flags);
731 /************************************************
732 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
733 *************************************************/
734 ch->statusflags |= LOWWAIT;
737 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
739 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
741 int flag = 0, dtr, rts;
744 if ((port != MAX_PORTS) && (!ch))
747 MoxaPortGetLineOut(ch->port, &dtr, &rts);
752 dtr = MoxaPortLineStatus(ch->port);
762 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
763 unsigned int set, unsigned int clear)
765 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
770 if ((port != MAX_PORTS) && (!ch))
773 MoxaPortGetLineOut(ch->port, &dtr, &rts);
778 if (clear & TIOCM_RTS)
780 if (clear & TIOCM_DTR)
782 MoxaPortLineCtrl(ch->port, dtr, rts);
786 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
787 unsigned int cmd, unsigned long arg)
789 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
791 void __user *argp = (void __user *)arg;
795 if ((port != MAX_PORTS) && (!ch))
799 case TCSBRK: /* SVID version: non-zero arg --> no break */
800 retval = tty_check_change(tty);
803 setup_empty_event(tty);
804 tty_wait_until_sent(tty, 0);
806 MoxaPortSendBreak(ch->port, 0);
808 case TCSBRKP: /* support for POSIX tcsendbreak() */
809 retval = tty_check_change(tty);
812 setup_empty_event(tty);
813 tty_wait_until_sent(tty, 0);
814 MoxaPortSendBreak(ch->port, arg);
817 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) argp);
819 if(get_user(retval, (unsigned long __user *) argp))
822 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
825 ch->asyncflags &= ~ASYNC_CHECK_CD;
827 ch->asyncflags |= ASYNC_CHECK_CD;
830 return moxa_get_serial_info(ch, argp);
833 return moxa_set_serial_info(ch, argp);
835 retval = MoxaDriverIoctl(cmd, arg, port);
840 static void moxa_throttle(struct tty_struct *tty)
842 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
844 ch->statusflags |= THROTTLE;
847 static void moxa_unthrottle(struct tty_struct *tty)
849 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
851 ch->statusflags &= ~THROTTLE;
854 static void moxa_set_termios(struct tty_struct *tty,
855 struct termios *old_termios)
857 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
862 if (!(old_termios->c_cflag & CLOCAL) &&
863 (tty->termios->c_cflag & CLOCAL))
864 wake_up_interruptible(&ch->open_wait);
867 static void moxa_stop(struct tty_struct *tty)
869 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
873 MoxaPortTxDisable(ch->port);
874 ch->statusflags |= TXSTOPPED;
878 static void moxa_start(struct tty_struct *tty)
880 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
885 if (!(ch->statusflags & TXSTOPPED))
888 MoxaPortTxEnable(ch->port);
889 ch->statusflags &= ~TXSTOPPED;
892 static void moxa_hangup(struct tty_struct *tty)
894 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
896 moxa_flush_buffer(tty);
900 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
902 wake_up_interruptible(&ch->open_wait);
905 static void moxa_poll(unsigned long ignored)
909 struct tty_struct *tp;
913 del_timer(&moxaTimer);
915 if (MoxaDriverPoll() < 0) {
916 moxaTimer.function = moxa_poll;
917 moxaTimer.expires = jiffies + (HZ / 50);
919 add_timer(&moxaTimer);
922 for (card = 0; card < MAX_BOARDS; card++) {
923 if ((ports = MoxaPortsOfCard(card)) <= 0)
925 ch = &moxaChannels[card * MAX_PORTS_PER_BOARD];
926 for (i = 0; i < ports; i++, ch++) {
927 if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
929 if (!(ch->statusflags & THROTTLE) &&
930 (MoxaPortRxQueue(ch->port) > 0))
932 if ((tp = ch->tty) == 0)
934 if (ch->statusflags & LOWWAIT) {
935 if (MoxaPortTxQueue(ch->port) <= WAKEUP_CHARS) {
937 ch->statusflags &= ~LOWWAIT;
942 if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch->port) > 0)) {
943 tty_insert_flip_char(tp, 0, TTY_BREAK);
944 tty_schedule_flip(tp);
946 if (MoxaPortDCDChange(ch->port)) {
947 if (ch->asyncflags & ASYNC_CHECK_CD) {
948 if (MoxaPortDCDON(ch->port))
949 wake_up_interruptible(&ch->open_wait);
951 set_bit(MOXA_EVENT_HANGUP, &ch->event);
952 schedule_work(&ch->tqueue);
959 moxaTimer.function = moxa_poll;
960 moxaTimer.expires = jiffies + (HZ / 50);
962 add_timer(&moxaTimer);
965 /******************************************************************************/
967 static void set_tty_param(struct tty_struct *tty)
969 register struct termios *ts;
971 int rts, cts, txflow, rxflow, xany;
973 ch = (struct moxa_str *) tty->driver_data;
975 if (ts->c_cflag & CLOCAL)
976 ch->asyncflags &= ~ASYNC_CHECK_CD;
978 ch->asyncflags |= ASYNC_CHECK_CD;
979 rts = cts = txflow = rxflow = xany = 0;
980 if (ts->c_cflag & CRTSCTS)
982 if (ts->c_iflag & IXON)
984 if (ts->c_iflag & IXOFF)
986 if (ts->c_iflag & IXANY)
988 MoxaPortFlowCtrl(ch->port, rts, cts, txflow, rxflow, xany);
989 MoxaPortSetTermio(ch->port, ts, tty_get_baud_rate(tty));
992 static int block_till_ready(struct tty_struct *tty, struct file *filp,
995 DECLARE_WAITQUEUE(wait,current);
998 int do_clocal = C_CLOCAL(tty);
1001 * If the device is in the middle of being closed, then block
1002 * until it's done, and then try again.
1004 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
1005 if (ch->asyncflags & ASYNC_CLOSING)
1006 interruptible_sleep_on(&ch->close_wait);
1007 #ifdef SERIAL_DO_RESTART
1008 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1011 return (-ERESTARTSYS);
1017 * If non-blocking mode is set, then make the check up front
1020 if (filp->f_flags & O_NONBLOCK) {
1021 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1025 * Block waiting for the carrier detect and the line to become free
1028 add_wait_queue(&ch->open_wait, &wait);
1029 #ifdef SERIAL_DEBUG_OPEN
1030 printk("block_til_ready before block: ttys%d, count = %d\n",
1031 ch->line, ch->count);
1033 spin_lock_irqsave(&moxa_lock, flags);
1034 if (!tty_hung_up_p(filp))
1037 spin_unlock_irqrestore(&moxa_lock, flags);
1040 set_current_state(TASK_INTERRUPTIBLE);
1041 if (tty_hung_up_p(filp) ||
1042 !(ch->asyncflags & ASYNC_INITIALIZED)) {
1043 #ifdef SERIAL_DO_RESTART
1044 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1047 retval = -ERESTARTSYS;
1053 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
1054 MoxaPortDCDON(ch->port)))
1057 if (signal_pending(current)) {
1058 retval = -ERESTARTSYS;
1063 set_current_state(TASK_RUNNING);
1064 remove_wait_queue(&ch->open_wait, &wait);
1066 spin_lock_irqsave(&moxa_lock, flags);
1067 if (!tty_hung_up_p(filp))
1070 spin_unlock_irqrestore(&moxa_lock, flags);
1071 #ifdef SERIAL_DEBUG_OPEN
1072 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1073 ch->line, ch->count);
1077 /* FIXME: review to see if we need to use set_bit on these */
1078 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1082 static void setup_empty_event(struct tty_struct *tty)
1084 struct moxa_str *ch = tty->driver_data;
1085 unsigned long flags;
1087 spin_lock_irqsave(&moxa_lock, flags);
1088 ch->statusflags |= EMPTYWAIT;
1089 moxaEmptyTimer_on[ch->port] = 0;
1090 del_timer(&moxaEmptyTimer[ch->port]);
1091 moxaEmptyTimer[ch->port].expires = jiffies + HZ;
1092 moxaEmptyTimer_on[ch->port] = 1;
1093 add_timer(&moxaEmptyTimer[ch->port]);
1094 spin_unlock_irqrestore(&moxa_lock, flags);
1097 static void check_xmit_empty(unsigned long data)
1099 struct moxa_str *ch;
1101 ch = (struct moxa_str *) data;
1102 moxaEmptyTimer_on[ch->port] = 0;
1103 del_timer(&moxaEmptyTimer[ch->port]);
1104 if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
1105 if (MoxaPortTxQueue(ch->port) == 0) {
1106 ch->statusflags &= ~EMPTYWAIT;
1107 tty_wakeup(ch->tty);
1110 moxaEmptyTimer[ch->port].expires = jiffies + HZ;
1111 moxaEmptyTimer_on[ch->port] = 1;
1112 add_timer(&moxaEmptyTimer[ch->port]);
1114 ch->statusflags &= ~EMPTYWAIT;
1117 static void shut_down(struct moxa_str *ch)
1119 struct tty_struct *tp;
1121 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1126 MoxaPortDisable(ch->port);
1129 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1131 if (tp->termios->c_cflag & HUPCL)
1132 MoxaPortLineCtrl(ch->port, 0, 0);
1134 ch->asyncflags &= ~ASYNC_INITIALIZED;
1137 static void receive_data(struct moxa_str *ch)
1139 struct tty_struct *tp;
1141 unsigned long flags;
1147 /**************************************************
1148 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1149 *****************************************************/
1151 MoxaPortFlushData(ch->port, 0);
1154 spin_lock_irqsave(&moxa_lock, flags);
1155 MoxaPortReadData(ch->port, tp);
1156 spin_unlock_irqrestore(&moxa_lock, flags);
1157 tty_schedule_flip(tp);
1160 #define Magic_code 0x404
1163 * System Configuration
1166 * for C218 BIOS initialization
1168 #define C218_ConfBase 0x800
1169 #define C218_status (C218_ConfBase + 0) /* BIOS running status */
1170 #define C218_diag (C218_ConfBase + 2) /* diagnostic status */
1171 #define C218_key (C218_ConfBase + 4) /* WORD (0x218 for C218) */
1172 #define C218DLoad_len (C218_ConfBase + 6) /* WORD */
1173 #define C218check_sum (C218_ConfBase + 8) /* BYTE */
1174 #define C218chksum_ok (C218_ConfBase + 0x0a) /* BYTE (1:ok) */
1175 #define C218_TestRx (C218_ConfBase + 0x10) /* 8 bytes for 8 ports */
1176 #define C218_TestTx (C218_ConfBase + 0x18) /* 8 bytes for 8 ports */
1177 #define C218_RXerr (C218_ConfBase + 0x20) /* 8 bytes for 8 ports */
1178 #define C218_ErrFlag (C218_ConfBase + 0x28) /* 8 bytes for 8 ports */
1180 #define C218_LoadBuf 0x0F00
1181 #define C218_KeyCode 0x218
1182 #define CP204J_KeyCode 0x204
1185 * for C320 BIOS initialization
1187 #define C320_ConfBase 0x800
1188 #define C320_LoadBuf 0x0f00
1189 #define STS_init 0x05 /* for C320_status */
1191 #define C320_status C320_ConfBase + 0 /* BIOS running status */
1192 #define C320_diag C320_ConfBase + 2 /* diagnostic status */
1193 #define C320_key C320_ConfBase + 4 /* WORD (0320H for C320) */
1194 #define C320DLoad_len C320_ConfBase + 6 /* WORD */
1195 #define C320check_sum C320_ConfBase + 8 /* WORD */
1196 #define C320chksum_ok C320_ConfBase + 0x0a /* WORD (1:ok) */
1197 #define C320bapi_len C320_ConfBase + 0x0c /* WORD */
1198 #define C320UART_no C320_ConfBase + 0x0e /* WORD */
1200 #define C320_KeyCode 0x320
1202 #define FixPage_addr 0x0000 /* starting addr of static page */
1203 #define DynPage_addr 0x2000 /* starting addr of dynamic page */
1204 #define C218_start 0x3000 /* starting addr of C218 BIOS prg */
1205 #define Control_reg 0x1ff0 /* select page and reset control */
1206 #define HW_reset 0x80
1211 #define FC_CardReset 0x80
1212 #define FC_ChannelReset 1 /* C320 firmware not supported */
1213 #define FC_EnableCH 2
1214 #define FC_DisableCH 3
1215 #define FC_SetParam 4
1216 #define FC_SetMode 5
1217 #define FC_SetRate 6
1218 #define FC_LineControl 7
1219 #define FC_LineStatus 8
1220 #define FC_XmitControl 9
1221 #define FC_FlushQueue 10
1222 #define FC_SendBreak 11
1223 #define FC_StopBreak 12
1224 #define FC_LoopbackON 13
1225 #define FC_LoopbackOFF 14
1226 #define FC_ClrIrqTable 15
1227 #define FC_SendXon 16
1228 #define FC_SetTermIrq 17 /* C320 firmware not supported */
1229 #define FC_SetCntIrq 18 /* C320 firmware not supported */
1230 #define FC_SetBreakIrq 19
1231 #define FC_SetLineIrq 20
1232 #define FC_SetFlowCtl 21
1233 #define FC_GenIrq 22
1234 #define FC_InCD180 23
1235 #define FC_OutCD180 24
1236 #define FC_InUARTreg 23
1237 #define FC_OutUARTreg 24
1238 #define FC_SetXonXoff 25
1239 #define FC_OutCD180CCR 26
1240 #define FC_ExtIQueue 27
1241 #define FC_ExtOQueue 28
1242 #define FC_ClrLineIrq 29
1243 #define FC_HWFlowCtl 30
1244 #define FC_GetClockRate 35
1245 #define FC_SetBaud 36
1246 #define FC_SetDataMode 41
1247 #define FC_GetCCSR 43
1248 #define FC_GetDataError 45
1249 #define FC_RxControl 50
1250 #define FC_ImmSend 51
1251 #define FC_SetXonState 52
1252 #define FC_SetXoffState 53
1253 #define FC_SetRxFIFOTrig 54
1254 #define FC_SetTxFIFOCnt 55
1255 #define FC_UnixRate 56
1256 #define FC_UnixResetTimer 57
1258 #define RxFIFOTrig1 0
1259 #define RxFIFOTrig4 1
1260 #define RxFIFOTrig8 2
1261 #define RxFIFOTrig14 3
1266 #define DRAM_global 0
1267 #define INT_data (DRAM_global + 0)
1268 #define Config_base (DRAM_global + 0x108)
1270 #define IRQindex (INT_data + 0)
1271 #define IRQpending (INT_data + 4)
1272 #define IRQtable (INT_data + 8)
1277 #define IntrRx 0x01 /* receiver data O.K. */
1278 #define IntrTx 0x02 /* transmit buffer empty */
1279 #define IntrFunc 0x04 /* function complete */
1280 #define IntrBreak 0x08 /* received break */
1281 #define IntrLine 0x10 /* line status change
1283 #define IntrIntr 0x20 /* received INTR code */
1284 #define IntrQuit 0x40 /* received QUIT code */
1285 #define IntrEOF 0x80 /* received EOF code */
1287 #define IntrRxTrigger 0x100 /* rx data count reach tigger value */
1288 #define IntrTxTrigger 0x200 /* tx data count below trigger value */
1290 #define Magic_no (Config_base + 0)
1291 #define Card_model_no (Config_base + 2)
1292 #define Total_ports (Config_base + 4)
1293 #define Module_cnt (Config_base + 8)
1294 #define Module_no (Config_base + 10)
1295 #define Timer_10ms (Config_base + 14)
1296 #define Disable_IRQ (Config_base + 20)
1297 #define TMS320_PORT1 (Config_base + 22)
1298 #define TMS320_PORT2 (Config_base + 24)
1299 #define TMS320_CLOCK (Config_base + 26)
1302 * DATA BUFFER in DRAM
1304 #define Extern_table 0x400 /* Base address of the external table
1305 (24 words * 64) total 3K bytes
1306 (24 words * 128) total 6K bytes */
1307 #define Extern_size 0x60 /* 96 bytes */
1308 #define RXrptr 0x00 /* read pointer for RX buffer */
1309 #define RXwptr 0x02 /* write pointer for RX buffer */
1310 #define TXrptr 0x04 /* read pointer for TX buffer */
1311 #define TXwptr 0x06 /* write pointer for TX buffer */
1312 #define HostStat 0x08 /* IRQ flag and general flag */
1313 #define FlagStat 0x0A
1314 #define FlowControl 0x0C /* B7 B6 B5 B4 B3 B2 B1 B0 */
1315 /* x x x x | | | | */
1316 /* | | | + CTS flow */
1317 /* | | +--- RTS flow */
1318 /* | +------ TX Xon/Xoff */
1319 /* +--------- RX Xon/Xoff */
1320 #define Break_cnt 0x0E /* received break count */
1321 #define CD180TXirq 0x10 /* if non-0: enable TX irq */
1322 #define RX_mask 0x12
1323 #define TX_mask 0x14
1324 #define Ofs_rxb 0x16
1325 #define Ofs_txb 0x18
1326 #define Page_rxb 0x1A
1327 #define Page_txb 0x1C
1328 #define EndPage_rxb 0x1E
1329 #define EndPage_txb 0x20
1330 #define Data_error 0x22
1331 #define RxTrigger 0x28
1332 #define TxTrigger 0x2a
1334 #define rRXwptr 0x34
1335 #define Low_water 0x36
1337 #define FuncCode 0x40
1338 #define FuncArg 0x42
1339 #define FuncArg1 0x44
1341 #define C218rx_size 0x2000 /* 8K bytes */
1342 #define C218tx_size 0x8000 /* 32K bytes */
1344 #define C218rx_mask (C218rx_size - 1)
1345 #define C218tx_mask (C218tx_size - 1)
1347 #define C320p8rx_size 0x2000
1348 #define C320p8tx_size 0x8000
1349 #define C320p8rx_mask (C320p8rx_size - 1)
1350 #define C320p8tx_mask (C320p8tx_size - 1)
1352 #define C320p16rx_size 0x2000
1353 #define C320p16tx_size 0x4000
1354 #define C320p16rx_mask (C320p16rx_size - 1)
1355 #define C320p16tx_mask (C320p16tx_size - 1)
1357 #define C320p24rx_size 0x2000
1358 #define C320p24tx_size 0x2000
1359 #define C320p24rx_mask (C320p24rx_size - 1)
1360 #define C320p24tx_mask (C320p24tx_size - 1)
1362 #define C320p32rx_size 0x1000
1363 #define C320p32tx_size 0x1000
1364 #define C320p32rx_mask (C320p32rx_size - 1)
1365 #define C320p32tx_mask (C320p32tx_size - 1)
1367 #define Page_size 0x2000
1368 #define Page_mask (Page_size - 1)
1369 #define C218rx_spage 3
1370 #define C218tx_spage 4
1371 #define C218rx_pageno 1
1372 #define C218tx_pageno 4
1373 #define C218buf_pageno 5
1375 #define C320p8rx_spage 3
1376 #define C320p8tx_spage 4
1377 #define C320p8rx_pgno 1
1378 #define C320p8tx_pgno 4
1379 #define C320p8buf_pgno 5
1381 #define C320p16rx_spage 3
1382 #define C320p16tx_spage 4
1383 #define C320p16rx_pgno 1
1384 #define C320p16tx_pgno 2
1385 #define C320p16buf_pgno 3
1387 #define C320p24rx_spage 3
1388 #define C320p24tx_spage 4
1389 #define C320p24rx_pgno 1
1390 #define C320p24tx_pgno 1
1391 #define C320p24buf_pgno 2
1393 #define C320p32rx_spage 3
1394 #define C320p32tx_ofs C320p32rx_size
1395 #define C320p32tx_spage 3
1396 #define C320p32buf_pgno 1
1401 #define WakeupRx 0x01
1402 #define WakeupTx 0x02
1403 #define WakeupBreak 0x08
1404 #define WakeupLine 0x10
1405 #define WakeupIntr 0x20
1406 #define WakeupQuit 0x40
1407 #define WakeupEOF 0x80 /* used in VTIME control */
1408 #define WakeupRxTrigger 0x100
1409 #define WakeupTxTrigger 0x200
1413 #define Rx_over 0x01
1414 #define Xoff_state 0x02
1415 #define Tx_flowOff 0x04
1416 #define Tx_enable 0x08
1417 #define CTS_state 0x10
1418 #define DSR_state 0x20
1419 #define DCD_state 0x80
1423 #define CTS_FlowCtl 1
1424 #define RTS_FlowCtl 2
1425 #define Tx_FlowCtl 4
1426 #define Rx_FlowCtl 8
1427 #define IXM_IXANY 0x10
1429 #define LowWater 128
1437 /* mode definition */
1443 #define MX_STOP1 0x00
1444 #define MX_STOP15 0x04
1445 #define MX_STOP2 0x08
1447 #define MX_PARNONE 0x00
1448 #define MX_PAREVEN 0x40
1449 #define MX_PARODD 0xC0
1454 #define QueryPort MAX_PORTS
1460 int rxcnt[MAX_PORTS];
1461 int txcnt[MAX_PORTS];
1463 typedef struct mon_str mon_st;
1465 #define DCD_changed 0x01
1466 #define DCD_oldstate 0x80
1468 static unsigned char moxaBuff[10240];
1469 static void __iomem *moxaIntNdx[MAX_BOARDS];
1470 static void __iomem *moxaIntPend[MAX_BOARDS];
1471 static void __iomem *moxaIntTable[MAX_BOARDS];
1472 static char moxaChkPort[MAX_PORTS];
1473 static char moxaLineCtrl[MAX_PORTS];
1474 static void __iomem *moxaTableAddr[MAX_PORTS];
1475 static long moxaCurBaud[MAX_PORTS];
1476 static char moxaDCDState[MAX_PORTS];
1477 static char moxaLowChkFlag[MAX_PORTS];
1478 static int moxaLowWaterChk;
1479 static int moxaCard;
1480 static mon_st moxaLog;
1481 static int moxaFuncTout;
1482 static ushort moxaBreakCnt[MAX_PORTS];
1484 static void moxadelay(int);
1485 static void moxafunc(void __iomem *, int, ushort);
1486 static void wait_finish(void __iomem *);
1487 static void low_water_check(void __iomem *);
1488 static int moxaloadbios(int, unsigned char __user *, int);
1489 static int moxafindcard(int);
1490 static int moxaload320b(int, unsigned char __user *, int);
1491 static int moxaloadcode(int, unsigned char __user *, int);
1492 static int moxaloadc218(int, void __iomem *, int);
1493 static int moxaloadc320(int, void __iomem *, int, int *);
1495 /*****************************************************************************
1496 * Driver level functions: *
1497 * 1. MoxaDriverInit(void); *
1498 * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
1499 * 3. MoxaDriverPoll(void); *
1500 *****************************************************************************/
1501 void MoxaDriverInit(void)
1505 moxaFuncTout = HZ / 2; /* 500 mini-seconds */
1508 moxaLowWaterChk = 0;
1509 for (i = 0; i < MAX_PORTS; i++) {
1511 moxaLowChkFlag[i] = 0;
1512 moxaLineCtrl[i] = 0;
1513 moxaLog.rxcnt[i] = 0;
1514 moxaLog.txcnt[i] = 0;
1519 #define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1520 #define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
1521 #define MOXA_INIT_DRIVER (MOXA + 6) /* moxaCard=0 */
1522 #define MOXA_LOAD_BIOS (MOXA + 9) /* download BIOS */
1523 #define MOXA_FIND_BOARD (MOXA + 10) /* Check if MOXA card exist? */
1524 #define MOXA_LOAD_C320B (MOXA + 11) /* download 320B firmware */
1525 #define MOXA_LOAD_CODE (MOXA + 12) /* download firmware */
1526 #define MOXA_GETDATACOUNT (MOXA + 23)
1527 #define MOXA_GET_IOQUEUE (MOXA + 27)
1528 #define MOXA_FLUSH_QUEUE (MOXA + 28)
1529 #define MOXA_GET_CONF (MOXA + 35) /* configuration */
1530 #define MOXA_GET_MAJOR (MOXA + 63)
1531 #define MOXA_GET_CUMAJOR (MOXA + 64)
1532 #define MOXA_GETMSTATUS (MOXA + 65)
1546 static struct moxaq_str temp_queue[MAX_PORTS];
1547 static struct dl_str dltmp;
1549 void MoxaPortFlushData(int port, int mode)
1551 void __iomem *ofsAddr;
1552 if ((mode < 0) || (mode > 2))
1554 ofsAddr = moxaTableAddr[port];
1555 moxafunc(ofsAddr, FC_FlushQueue, mode);
1557 moxaLowChkFlag[port] = 0;
1558 low_water_check(ofsAddr);
1562 int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port)
1566 int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
1567 void __user *argp = (void __user *)arg;
1569 if (port == QueryPort) {
1570 if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_INIT_DRIVER) &&
1571 (cmd != MOXA_LOAD_BIOS) && (cmd != MOXA_FIND_BOARD) && (cmd != MOXA_LOAD_C320B) &&
1572 (cmd != MOXA_LOAD_CODE) && (cmd != MOXA_GETDATACOUNT) &&
1573 (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
1574 (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
1579 if(copy_to_user(argp, &moxa_boards, MAX_BOARDS * sizeof(moxa_board_conf)))
1582 case MOXA_INIT_DRIVER:
1583 if ((int) arg == 0x404)
1586 case MOXA_GETDATACOUNT:
1587 moxaLog.tick = jiffies;
1588 if(copy_to_user(argp, &moxaLog, sizeof(mon_st)))
1591 case MOXA_FLUSH_QUEUE:
1592 MoxaPortFlushData(port, arg);
1594 case MOXA_GET_IOQUEUE:
1595 for (i = 0; i < MAX_PORTS; i++) {
1596 if (moxaChkPort[i]) {
1597 temp_queue[i].inq = MoxaPortRxQueue(i);
1598 temp_queue[i].outq = MoxaPortTxQueue(i);
1601 if(copy_to_user(argp, temp_queue, sizeof(struct moxaq_str) * MAX_PORTS))
1604 case MOXA_GET_OQUEUE:
1605 i = MoxaPortTxQueue(port);
1606 return put_user(i, (unsigned long __user *)argp);
1607 case MOXA_GET_IQUEUE:
1608 i = MoxaPortRxQueue(port);
1609 return put_user(i, (unsigned long __user *)argp);
1610 case MOXA_GET_MAJOR:
1611 if(copy_to_user(argp, &ttymajor, sizeof(int)))
1614 case MOXA_GET_CUMAJOR:
1616 if(copy_to_user(argp, &i, sizeof(int)))
1619 case MOXA_GETMSTATUS:
1620 for (i = 0; i < MAX_PORTS; i++) {
1622 GMStatus[i].dcd = 0;
1623 GMStatus[i].dsr = 0;
1624 GMStatus[i].cts = 0;
1625 if (!moxaChkPort[i]) {
1628 status = MoxaPortLineStatus(moxaChannels[i].port);
1630 GMStatus[i].cts = 1;
1632 GMStatus[i].dsr = 1;
1634 GMStatus[i].dcd = 1;
1637 if (!moxaChannels[i].tty || !moxaChannels[i].tty->termios)
1638 GMStatus[i].cflag = moxaChannels[i].cflag;
1640 GMStatus[i].cflag = moxaChannels[i].tty->termios->c_cflag;
1642 if(copy_to_user(argp, GMStatus, sizeof(struct mxser_mstatus) * MAX_PORTS))
1646 return (-ENOIOCTLCMD);
1647 case MOXA_LOAD_BIOS:
1648 case MOXA_FIND_BOARD:
1649 case MOXA_LOAD_C320B:
1650 case MOXA_LOAD_CODE:
1651 if (!capable(CAP_SYS_RAWIO))
1656 if(copy_from_user(&dltmp, argp, sizeof(struct dl_str)))
1658 if(dltmp.cardno < 0 || dltmp.cardno >= MAX_BOARDS)
1663 case MOXA_LOAD_BIOS:
1664 i = moxaloadbios(dltmp.cardno, dltmp.buf, dltmp.len);
1666 case MOXA_FIND_BOARD:
1667 return moxafindcard(dltmp.cardno);
1668 case MOXA_LOAD_C320B:
1669 moxaload320b(dltmp.cardno, dltmp.buf, dltmp.len);
1670 default: /* to keep gcc happy */
1672 case MOXA_LOAD_CODE:
1673 i = moxaloadcode(dltmp.cardno, dltmp.buf, dltmp.len);
1681 int MoxaDriverPoll(void)
1683 register ushort temp;
1685 void __iomem *ofsAddr;
1691 for (card = 0; card < MAX_BOARDS; card++) {
1692 if (loadstat[card] == 0)
1694 if ((ports = moxa_boards[card].numPorts) == 0)
1696 if (readb(moxaIntPend[card]) == 0xff) {
1697 ip = moxaIntTable[card] + readb(moxaIntNdx[card]);
1698 p = card * MAX_PORTS_PER_BOARD;
1700 for (port = 0; port < ports; port += 2, p++) {
1701 if ((temp = readw(ip + port)) != 0) {
1702 writew(0, ip + port);
1703 ofsAddr = moxaTableAddr[p];
1705 writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
1706 if (temp & IntrBreak) {
1709 if (temp & IntrLine) {
1710 if (readb(ofsAddr + FlagStat) & DCD_state) {
1711 if ((moxaDCDState[p] & DCD_oldstate) == 0)
1712 moxaDCDState[p] = (DCD_oldstate |
1715 if (moxaDCDState[p] & DCD_oldstate)
1716 moxaDCDState[p] = DCD_changed;
1721 writeb(0, moxaIntPend[card]);
1723 if (moxaLowWaterChk) {
1724 p = card * MAX_PORTS_PER_BOARD;
1725 for (port = 0; port < ports; port++, p++) {
1726 if (moxaLowChkFlag[p]) {
1727 moxaLowChkFlag[p] = 0;
1728 ofsAddr = moxaTableAddr[p];
1729 low_water_check(ofsAddr);
1734 moxaLowWaterChk = 0;
1738 /*****************************************************************************
1739 * Card level function: *
1740 * 1. MoxaPortsOfCard(int cardno); *
1741 *****************************************************************************/
1742 int MoxaPortsOfCard(int cardno)
1745 if (moxa_boards[cardno].boardType == 0)
1747 return (moxa_boards[cardno].numPorts);
1750 /*****************************************************************************
1751 * Port level functions: *
1752 * 1. MoxaPortIsValid(int port); *
1753 * 2. MoxaPortEnable(int port); *
1754 * 3. MoxaPortDisable(int port); *
1755 * 4. MoxaPortGetMaxBaud(int port); *
1756 * 5. MoxaPortGetCurBaud(int port); *
1757 * 6. MoxaPortSetBaud(int port, long baud); *
1758 * 7. MoxaPortSetMode(int port, int databit, int stopbit, int parity); *
1759 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1760 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1761 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1762 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1763 * 12. MoxaPortLineStatus(int port); *
1764 * 13. MoxaPortDCDChange(int port); *
1765 * 14. MoxaPortDCDON(int port); *
1766 * 15. MoxaPortFlushData(int port, int mode); *
1767 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1768 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
1769 * 18. MoxaPortTxBufSize(int port); *
1770 * 19. MoxaPortRxBufSize(int port); *
1771 * 20. MoxaPortTxQueue(int port); *
1772 * 21. MoxaPortTxFree(int port); *
1773 * 22. MoxaPortRxQueue(int port); *
1774 * 23. MoxaPortRxFree(int port); *
1775 * 24. MoxaPortTxDisable(int port); *
1776 * 25. MoxaPortTxEnable(int port); *
1777 * 26. MoxaPortGetBrkCnt(int port); *
1778 * 27. MoxaPortResetBrkCnt(int port); *
1779 * 28. MoxaPortSetXonXoff(int port, int xonValue, int xoffValue); *
1780 * 29. MoxaPortIsTxHold(int port); *
1781 * 30. MoxaPortSendBreak(int port, int ticks); *
1782 *****************************************************************************/
1784 * Moxa Port Number Description:
1786 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1787 * the port number using in MOXA driver functions will be 0 to 31 for
1788 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1789 * to 127 for fourth. For example, if you setup three MOXA boards,
1790 * first board is C218, second board is C320-16 and third board is
1791 * C320-32. The port number of first board (C218 - 8 ports) is from
1792 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1793 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1794 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1795 * 127 will be invalid.
1798 * Moxa Functions Description:
1800 * Function 1: Driver initialization routine, this routine must be
1801 * called when initialized driver.
1803 * void MoxaDriverInit();
1806 * Function 2: Moxa driver private IOCTL command processing.
1808 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1810 * unsigned int cmd : IOCTL command
1811 * unsigned long arg : IOCTL argument
1812 * int port : port number (0 - 127)
1819 * Function 3: Moxa driver polling process routine.
1821 * int MoxaDriverPoll(void);
1823 * return: 0 ; polling O.K.
1824 * -1 : no any Moxa card.
1827 * Function 4: Get the ports of this card.
1829 * int MoxaPortsOfCard(int cardno);
1831 * int cardno : card number (0 - 3)
1833 * return: 0 : this card is invalid
1837 * Function 5: Check this port is valid or invalid
1839 * int MoxaPortIsValid(int port);
1840 * int port : port number (0 - 127, ref port description)
1842 * return: 0 : this port is invalid
1843 * 1 : this port is valid
1846 * Function 6: Enable this port to start Tx/Rx data.
1848 * void MoxaPortEnable(int port);
1849 * int port : port number (0 - 127)
1852 * Function 7: Disable this port
1854 * void MoxaPortDisable(int port);
1855 * int port : port number (0 - 127)
1858 * Function 8: Get the maximun available baud rate of this port.
1860 * long MoxaPortGetMaxBaud(int port);
1861 * int port : port number (0 - 127)
1863 * return: 0 : this port is invalid
1864 * 38400/57600/115200 bps
1867 * Function 9: Get the current baud rate of this port.
1869 * long MoxaPortGetCurBaud(int port);
1870 * int port : port number (0 - 127)
1872 * return: 0 : this port is invalid
1876 * Function 10: Setting baud rate of this port.
1878 * long MoxaPortSetBaud(int port, long baud);
1879 * int port : port number (0 - 127)
1880 * long baud : baud rate (50 - 115200)
1882 * return: 0 : this port is invalid or baud < 50
1883 * 50 - 115200 : the real baud rate set to the port, if
1884 * the argument baud is large than maximun
1885 * available baud rate, the real setting
1886 * baud rate will be the maximun baud rate.
1889 * Function 11: Setting the data-bits/stop-bits/parity of this port
1891 * int MoxaPortSetMode(int port, int databits, int stopbits, int parity);
1892 * int port : port number (0 - 127)
1893 * int databits : data bits (8/7/6/5)
1894 * int stopbits : stop bits (2/1/0, 0 show 1.5 stop bits)
1895 int parity : parity (0:None,1:Odd,2:Even,3:Mark,4:Space)
1897 * return: -1 : invalid parameter
1901 * Function 12: Configure the port.
1903 * int MoxaPortSetTermio(int port, struct termios *termio, speed_t baud);
1904 * int port : port number (0 - 127)
1905 * struct termios * termio : termio structure pointer
1906 * speed_t baud : baud rate
1908 * return: -1 : this port is invalid or termio == NULL
1912 * Function 13: Get the DTR/RTS state of this port.
1914 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1915 * int port : port number (0 - 127)
1916 * int * dtrState : pointer to INT to receive the current DTR
1917 * state. (if NULL, this function will not
1918 * write to this address)
1919 * int * rtsState : pointer to INT to receive the current RTS
1920 * state. (if NULL, this function will not
1921 * write to this address)
1923 * return: -1 : this port is invalid
1927 * Function 14: Setting the DTR/RTS output state of this port.
1929 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1930 * int port : port number (0 - 127)
1931 * int dtrState : DTR output state (0: off, 1: on)
1932 * int rtsState : RTS output state (0: off, 1: on)
1935 * Function 15: Setting the flow control of this port.
1937 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1938 * int txFlow,int xany);
1939 * int port : port number (0 - 127)
1940 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1941 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1942 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1943 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1944 * int xany : S/W XANY flow control (0: no, 1: yes)
1947 * Function 16: Get ths line status of this port
1949 * int MoxaPortLineStatus(int port);
1950 * int port : port number (0 - 127)
1952 * return: Bit 0 - CTS state (0: off, 1: on)
1953 * Bit 1 - DSR state (0: off, 1: on)
1954 * Bit 2 - DCD state (0: off, 1: on)
1957 * Function 17: Check the DCD state has changed since the last read
1960 * int MoxaPortDCDChange(int port);
1961 * int port : port number (0 - 127)
1963 * return: 0 : no changed
1964 * 1 : DCD has changed
1967 * Function 18: Check ths current DCD state is ON or not.
1969 * int MoxaPortDCDON(int port);
1970 * int port : port number (0 - 127)
1972 * return: 0 : DCD off
1976 * Function 19: Flush the Rx/Tx buffer data of this port.
1978 * void MoxaPortFlushData(int port, int mode);
1979 * int port : port number (0 - 127)
1981 * 0 : flush the Rx buffer
1982 * 1 : flush the Tx buffer
1983 * 2 : flush the Rx and Tx buffer
1986 * Function 20: Write data.
1988 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1989 * int port : port number (0 - 127)
1990 * unsigned char * buffer : pointer to write data buffer.
1991 * int length : write data length
1993 * return: 0 - length : real write data length
1996 * Function 21: Read data.
1998 * int MoxaPortReadData(int port, struct tty_struct *tty);
1999 * int port : port number (0 - 127)
2000 * struct tty_struct *tty : tty for data
2002 * return: 0 - length : real read data length
2005 * Function 22: Get the Tx buffer size of this port
2007 * int MoxaPortTxBufSize(int port);
2008 * int port : port number (0 - 127)
2010 * return: .. : Tx buffer size
2013 * Function 23: Get the Rx buffer size of this port
2015 * int MoxaPortRxBufSize(int port);
2016 * int port : port number (0 - 127)
2018 * return: .. : Rx buffer size
2021 * Function 24: Get the Tx buffer current queued data bytes
2023 * int MoxaPortTxQueue(int port);
2024 * int port : port number (0 - 127)
2026 * return: .. : Tx buffer current queued data bytes
2029 * Function 25: Get the Tx buffer current free space
2031 * int MoxaPortTxFree(int port);
2032 * int port : port number (0 - 127)
2034 * return: .. : Tx buffer current free space
2037 * Function 26: Get the Rx buffer current queued data bytes
2039 * int MoxaPortRxQueue(int port);
2040 * int port : port number (0 - 127)
2042 * return: .. : Rx buffer current queued data bytes
2045 * Function 27: Get the Rx buffer current free space
2047 * int MoxaPortRxFree(int port);
2048 * int port : port number (0 - 127)
2050 * return: .. : Rx buffer current free space
2053 * Function 28: Disable port data transmission.
2055 * void MoxaPortTxDisable(int port);
2056 * int port : port number (0 - 127)
2059 * Function 29: Enable port data transmission.
2061 * void MoxaPortTxEnable(int port);
2062 * int port : port number (0 - 127)
2065 * Function 30: Get the received BREAK signal count.
2067 * int MoxaPortGetBrkCnt(int port);
2068 * int port : port number (0 - 127)
2070 * return: 0 - .. : BREAK signal count
2073 * Function 31: Get the received BREAK signal count and reset it.
2075 * int MoxaPortResetBrkCnt(int port);
2076 * int port : port number (0 - 127)
2078 * return: 0 - .. : BREAK signal count
2081 * Function 32: Set the S/W flow control new XON/XOFF value, default
2082 * XON is 0x11 & XOFF is 0x13.
2084 * void MoxaPortSetXonXoff(int port, int xonValue, int xoffValue);
2085 * int port : port number (0 - 127)
2086 * int xonValue : new XON value (0 - 255)
2087 * int xoffValue : new XOFF value (0 - 255)
2090 * Function 33: Check this port's transmission is hold by remote site
2091 * because the flow control.
2093 * int MoxaPortIsTxHold(int port);
2094 * int port : port number (0 - 127)
2096 * return: 0 : normal
2097 * 1 : hold by remote site
2100 * Function 34: Send out a BREAK signal.
2102 * void MoxaPortSendBreak(int port, int ms100);
2103 * int port : port number (0 - 127)
2104 * int ms100 : break signal time interval.
2105 * unit: 100 mini-second. if ms100 == 0, it will
2106 * send out a about 250 ms BREAK signal.
2109 int MoxaPortIsValid(int port)
2114 if (moxaChkPort[port] == 0)
2119 void MoxaPortEnable(int port)
2121 void __iomem *ofsAddr;
2122 int MoxaPortLineStatus(int);
2123 short lowwater = 512;
2125 ofsAddr = moxaTableAddr[port];
2126 writew(lowwater, ofsAddr + Low_water);
2127 moxaBreakCnt[port] = 0;
2128 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2129 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2130 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
2132 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
2135 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
2136 moxafunc(ofsAddr, FC_FlushQueue, 2);
2138 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
2139 MoxaPortLineStatus(port);
2142 void MoxaPortDisable(int port)
2144 void __iomem *ofsAddr = moxaTableAddr[port];
2146 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
2147 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
2148 writew(0, ofsAddr + HostStat);
2149 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
2152 long MoxaPortGetMaxBaud(int port)
2154 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2155 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI))
2162 long MoxaPortSetBaud(int port, long baud)
2164 void __iomem *ofsAddr;
2168 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
2170 ofsAddr = moxaTableAddr[port];
2174 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
2175 else if (max == 57600L)
2176 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
2178 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
2180 moxafunc(ofsAddr, FC_SetBaud, val);
2182 moxaCurBaud[port] = baud;
2186 int MoxaPortSetTermio(int port, struct termios *termio, speed_t baud)
2188 void __iomem *ofsAddr;
2192 if (moxaChkPort[port] == 0 || termio == 0)
2194 ofsAddr = moxaTableAddr[port];
2195 cflag = termio->c_cflag; /* termio->c_cflag */
2197 mode = termio->c_cflag & CSIZE;
2200 else if (mode == CS6)
2202 else if (mode == CS7)
2204 else if (mode == CS8)
2207 if (termio->c_cflag & CSTOPB) {
2215 if (termio->c_cflag & PARENB) {
2216 if (termio->c_cflag & PARODD)
2223 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
2225 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2226 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2227 if (baud >= 921600L)
2230 MoxaPortSetBaud(port, baud);
2232 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
2233 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
2234 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
2235 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
2236 wait_finish(ofsAddr);
2242 int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState)
2245 if (!MoxaPortIsValid(port))
2248 if (moxaLineCtrl[port] & DTR_ON)
2254 if (moxaLineCtrl[port] & RTS_ON)
2262 void MoxaPortLineCtrl(int port, int dtr, int rts)
2264 void __iomem *ofsAddr;
2267 ofsAddr = moxaTableAddr[port];
2273 moxaLineCtrl[port] = mode;
2274 moxafunc(ofsAddr, FC_LineControl, mode);
2277 void MoxaPortFlowCtrl(int port, int rts, int cts, int txflow, int rxflow, int txany)
2279 void __iomem *ofsAddr;
2282 ofsAddr = moxaTableAddr[port];
2285 mode |= RTS_FlowCtl;
2287 mode |= CTS_FlowCtl;
2294 moxafunc(ofsAddr, FC_SetFlowCtl, mode);
2297 int MoxaPortLineStatus(int port)
2299 void __iomem *ofsAddr;
2302 ofsAddr = moxaTableAddr[port];
2303 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2304 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2305 moxafunc(ofsAddr, FC_LineStatus, 0);
2306 val = readw(ofsAddr + FuncArg);
2308 val = readw(ofsAddr + FlagStat) >> 4;
2313 if ((moxaDCDState[port] & DCD_oldstate) == 0)
2314 moxaDCDState[port] = (DCD_oldstate | DCD_changed);
2316 if (moxaDCDState[port] & DCD_oldstate)
2317 moxaDCDState[port] = DCD_changed;
2323 int MoxaPortDCDChange(int port)
2327 if (moxaChkPort[port] == 0)
2329 n = moxaDCDState[port];
2330 moxaDCDState[port] &= ~DCD_changed;
2335 int MoxaPortDCDON(int port)
2339 if (moxaChkPort[port] == 0)
2341 if (moxaDCDState[port] & DCD_oldstate)
2350 int MoxaDumpMem(int port, unsigned char * buffer, int len)
2353 unsigned long baseAddr,ofsAddr,ofs;
2355 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2356 ofs = baseAddr + DynPage_addr + pageofs;
2359 for (i = 0; i < len; i++)
2360 buffer[i] = readb(ofs+i);
2365 int MoxaPortWriteData(int port, unsigned char * buffer, int len)
2370 ushort head, tx_mask, spage, epage;
2371 ushort pageno, pageofs, bufhead;
2372 void __iomem *baseAddr, *ofsAddr, *ofs;
2374 ofsAddr = moxaTableAddr[port];
2375 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2376 tx_mask = readw(ofsAddr + TX_mask);
2377 spage = readw(ofsAddr + Page_txb);
2378 epage = readw(ofsAddr + EndPage_txb);
2379 tail = readw(ofsAddr + TXwptr);
2380 head = readw(ofsAddr + TXrptr);
2381 c = (head > tail) ? (head - tail - 1)
2382 : (head - tail + tx_mask);
2385 moxaLog.txcnt[port] += c;
2387 if (spage == epage) {
2388 bufhead = readw(ofsAddr + Ofs_txb);
2389 writew(spage, baseAddr + Control_reg);
2392 len = head - tail - 1;
2394 len = tx_mask + 1 - tail;
2395 len = (c > len) ? len : c;
2396 ofs = baseAddr + DynPage_addr + bufhead + tail;
2397 for (i = 0; i < len; i++)
2398 writeb(*buffer++, ofs + i);
2399 tail = (tail + len) & tx_mask;
2402 writew(tail, ofsAddr + TXwptr);
2405 pageno = spage + (tail >> 13);
2406 pageofs = tail & Page_mask;
2408 cnt = Page_size - pageofs;
2412 writeb(pageno, baseAddr + Control_reg);
2413 ofs = baseAddr + DynPage_addr + pageofs;
2414 for (i = 0; i < cnt; i++)
2415 writeb(*buffer++, ofs + i);
2417 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2420 if (++pageno == epage)
2425 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2429 int MoxaPortReadData(int port, struct tty_struct *tty)
2431 register ushort head, pageofs;
2432 int i, count, cnt, len, total, remain;
2433 ushort tail, rx_mask, spage, epage;
2434 ushort pageno, bufhead;
2435 void __iomem *baseAddr, *ofsAddr, *ofs;
2437 ofsAddr = moxaTableAddr[port];
2438 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2439 head = readw(ofsAddr + RXrptr);
2440 tail = readw(ofsAddr + RXwptr);
2441 rx_mask = readw(ofsAddr + RX_mask);
2442 spage = readw(ofsAddr + Page_rxb);
2443 epage = readw(ofsAddr + EndPage_rxb);
2444 count = (tail >= head) ? (tail - head)
2445 : (tail - head + rx_mask + 1);
2450 remain = count - total;
2451 moxaLog.rxcnt[port] += total;
2453 if (spage == epage) {
2454 bufhead = readw(ofsAddr + Ofs_rxb);
2455 writew(spage, baseAddr + Control_reg);
2460 len = rx_mask + 1 - head;
2461 len = (count > len) ? len : count;
2462 ofs = baseAddr + DynPage_addr + bufhead + head;
2463 for (i = 0; i < len; i++)
2464 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2465 head = (head + len) & rx_mask;
2468 writew(head, ofsAddr + RXrptr);
2471 pageno = spage + (head >> 13);
2472 pageofs = head & Page_mask;
2474 cnt = Page_size - pageofs;
2478 writew(pageno, baseAddr + Control_reg);
2479 ofs = baseAddr + DynPage_addr + pageofs;
2480 for (i = 0; i < cnt; i++)
2481 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2483 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2486 if (++pageno == epage)
2491 if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2492 moxaLowWaterChk = 1;
2493 moxaLowChkFlag[port] = 1;
2499 int MoxaPortTxQueue(int port)
2501 void __iomem *ofsAddr;
2502 ushort rptr, wptr, mask;
2505 ofsAddr = moxaTableAddr[port];
2506 rptr = readw(ofsAddr + TXrptr);
2507 wptr = readw(ofsAddr + TXwptr);
2508 mask = readw(ofsAddr + TX_mask);
2509 len = (wptr - rptr) & mask;
2513 int MoxaPortTxFree(int port)
2515 void __iomem *ofsAddr;
2516 ushort rptr, wptr, mask;
2519 ofsAddr = moxaTableAddr[port];
2520 rptr = readw(ofsAddr + TXrptr);
2521 wptr = readw(ofsAddr + TXwptr);
2522 mask = readw(ofsAddr + TX_mask);
2523 len = mask - ((wptr - rptr) & mask);
2527 int MoxaPortRxQueue(int port)
2529 void __iomem *ofsAddr;
2530 ushort rptr, wptr, mask;
2533 ofsAddr = moxaTableAddr[port];
2534 rptr = readw(ofsAddr + RXrptr);
2535 wptr = readw(ofsAddr + RXwptr);
2536 mask = readw(ofsAddr + RX_mask);
2537 len = (wptr - rptr) & mask;
2542 void MoxaPortTxDisable(int port)
2544 void __iomem *ofsAddr;
2546 ofsAddr = moxaTableAddr[port];
2547 moxafunc(ofsAddr, FC_SetXoffState, Magic_code);
2550 void MoxaPortTxEnable(int port)
2552 void __iomem *ofsAddr;
2554 ofsAddr = moxaTableAddr[port];
2555 moxafunc(ofsAddr, FC_SetXonState, Magic_code);
2559 int MoxaPortResetBrkCnt(int port)
2562 cnt = moxaBreakCnt[port];
2563 moxaBreakCnt[port] = 0;
2568 void MoxaPortSendBreak(int port, int ms100)
2570 void __iomem *ofsAddr;
2572 ofsAddr = moxaTableAddr[port];
2574 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2575 moxadelay(ms100 * (HZ / 10));
2577 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2578 moxadelay(HZ / 4); /* 250 ms */
2580 moxafunc(ofsAddr, FC_StopBreak, Magic_code);
2583 static int moxa_get_serial_info(struct moxa_str *info,
2584 struct serial_struct __user *retinfo)
2586 struct serial_struct tmp;
2588 memset(&tmp, 0, sizeof(tmp));
2589 tmp.type = info->type;
2590 tmp.line = info->port;
2593 tmp.flags = info->asyncflags;
2594 tmp.baud_base = 921600;
2595 tmp.close_delay = info->close_delay;
2596 tmp.closing_wait = info->closing_wait;
2597 tmp.custom_divisor = 0;
2599 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2605 static int moxa_set_serial_info(struct moxa_str *info,
2606 struct serial_struct __user *new_info)
2608 struct serial_struct new_serial;
2610 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2613 if ((new_serial.irq != 0) ||
2614 (new_serial.port != 0) ||
2615 // (new_serial.type != info->type) ||
2616 (new_serial.custom_divisor != 0) ||
2617 (new_serial.baud_base != 921600))
2620 if (!capable(CAP_SYS_ADMIN)) {
2621 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2622 (info->asyncflags & ~ASYNC_USR_MASK)))
2625 info->close_delay = new_serial.close_delay * HZ / 100;
2626 info->closing_wait = new_serial.closing_wait * HZ / 100;
2629 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2630 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2632 if (new_serial.type == PORT_16550A) {
2633 MoxaSetFifo(info->port, 1);
2635 MoxaSetFifo(info->port, 0);
2638 info->type = new_serial.type;
2644 /*****************************************************************************
2645 * Static local functions: *
2646 *****************************************************************************/
2648 * moxadelay - delays a specified number ticks
2650 static void moxadelay(int tick)
2652 unsigned long st, et;
2656 while (time_before(jiffies, et));
2659 static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
2662 writew(arg, ofsAddr + FuncArg);
2663 writew(cmd, ofsAddr + FuncCode);
2664 wait_finish(ofsAddr);
2667 static void wait_finish(void __iomem *ofsAddr)
2672 while (readw(ofsAddr + FuncCode) != 0) {
2674 if ((j - i) > moxaFuncTout) {
2680 static void low_water_check(void __iomem *ofsAddr)
2683 ushort rptr, wptr, mask;
2685 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2686 rptr = readw(ofsAddr + RXrptr);
2687 wptr = readw(ofsAddr + RXwptr);
2688 mask = readw(ofsAddr + RX_mask);
2689 len = (wptr - rptr) & mask;
2690 if (len <= Low_water)
2691 moxafunc(ofsAddr, FC_SendXon, 0);
2695 static int moxaloadbios(int cardno, unsigned char __user *tmp, int len)
2697 void __iomem *baseAddr;
2700 if(copy_from_user(moxaBuff, tmp, len))
2702 baseAddr = moxaBaseAddr[cardno];
2703 writeb(HW_reset, baseAddr + Control_reg); /* reset */
2704 moxadelay(1); /* delay 10 ms */
2705 for (i = 0; i < 4096; i++)
2706 writeb(0, baseAddr + i); /* clear fix page */
2707 for (i = 0; i < len; i++)
2708 writeb(moxaBuff[i], baseAddr + i); /* download BIOS */
2709 writeb(0, baseAddr + Control_reg); /* restart */
2713 static int moxafindcard(int cardno)
2715 void __iomem *baseAddr;
2718 baseAddr = moxaBaseAddr[cardno];
2719 switch (moxa_boards[cardno].boardType) {
2720 case MOXA_BOARD_C218_ISA:
2721 case MOXA_BOARD_C218_PCI:
2722 if ((tmp = readw(baseAddr + C218_key)) != C218_KeyCode) {
2726 case MOXA_BOARD_CP204J:
2727 if ((tmp = readw(baseAddr + C218_key)) != CP204J_KeyCode) {
2732 if ((tmp = readw(baseAddr + C320_key)) != C320_KeyCode) {
2735 if ((tmp = readw(baseAddr + C320_status)) != STS_init) {
2742 static int moxaload320b(int cardno, unsigned char __user *tmp, int len)
2744 void __iomem *baseAddr;
2747 if(len > sizeof(moxaBuff))
2749 if(copy_from_user(moxaBuff, tmp, len))
2751 baseAddr = moxaBaseAddr[cardno];
2752 writew(len - 7168 - 2, baseAddr + C320bapi_len);
2753 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
2754 for (i = 0; i < 7168; i++)
2755 writeb(moxaBuff[i], baseAddr + DynPage_addr + i);
2756 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
2757 for (i = 0; i < (len - 7168); i++)
2758 writeb(moxaBuff[i + 7168], baseAddr + DynPage_addr + i);
2762 static int moxaloadcode(int cardno, unsigned char __user *tmp, int len)
2764 void __iomem *baseAddr, *ofsAddr;
2765 int retval, port, i;
2767 if(copy_from_user(moxaBuff, tmp, len))
2769 baseAddr = moxaBaseAddr[cardno];
2770 switch (moxa_boards[cardno].boardType) {
2771 case MOXA_BOARD_C218_ISA:
2772 case MOXA_BOARD_C218_PCI:
2773 case MOXA_BOARD_CP204J:
2774 retval = moxaloadc218(cardno, baseAddr, len);
2777 port = cardno * MAX_PORTS_PER_BOARD;
2778 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2779 moxaChkPort[port] = 1;
2780 moxaCurBaud[port] = 9600L;
2781 moxaDCDState[port] = 0;
2782 moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
2783 ofsAddr = moxaTableAddr[port];
2784 writew(C218rx_mask, ofsAddr + RX_mask);
2785 writew(C218tx_mask, ofsAddr + TX_mask);
2786 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
2787 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
2789 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
2790 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
2795 retval = moxaloadc320(cardno, baseAddr, len,
2796 &moxa_boards[cardno].numPorts);
2799 port = cardno * MAX_PORTS_PER_BOARD;
2800 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2801 moxaChkPort[port] = 1;
2802 moxaCurBaud[port] = 9600L;
2803 moxaDCDState[port] = 0;
2804 moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
2805 ofsAddr = moxaTableAddr[port];
2806 if (moxa_boards[cardno].numPorts == 8) {
2807 writew(C320p8rx_mask, ofsAddr + RX_mask);
2808 writew(C320p8tx_mask, ofsAddr + TX_mask);
2809 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
2810 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
2811 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
2812 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
2814 } else if (moxa_boards[cardno].numPorts == 16) {
2815 writew(C320p16rx_mask, ofsAddr + RX_mask);
2816 writew(C320p16tx_mask, ofsAddr + TX_mask);
2817 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
2818 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
2819 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
2820 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
2822 } else if (moxa_boards[cardno].numPorts == 24) {
2823 writew(C320p24rx_mask, ofsAddr + RX_mask);
2824 writew(C320p24tx_mask, ofsAddr + TX_mask);
2825 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
2826 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
2827 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
2828 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2829 } else if (moxa_boards[cardno].numPorts == 32) {
2830 writew(C320p32rx_mask, ofsAddr + RX_mask);
2831 writew(C320p32tx_mask, ofsAddr + TX_mask);
2832 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
2833 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
2834 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
2835 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
2836 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2841 loadstat[cardno] = 1;
2845 static int moxaloadc218(int cardno, void __iomem *baseAddr, int len)
2848 int i, j, len1, len2;
2849 ushort usum, *ptr, keycode;
2851 if (moxa_boards[cardno].boardType == MOXA_BOARD_CP204J)
2852 keycode = CP204J_KeyCode;
2854 keycode = C218_KeyCode;
2857 ptr = (ushort *) moxaBuff;
2858 for (i = 0; i < len1; i++)
2859 usum += le16_to_cpu(*(ptr + i));
2865 len2 = (len1 > 2048) ? 2048 : len1;
2867 for (i = 0; i < len2 << 1; i++)
2868 writeb(moxaBuff[i + j], baseAddr + C218_LoadBuf + i);
2871 writew(len2, baseAddr + C218DLoad_len);
2872 writew(0, baseAddr + C218_key);
2873 for (i = 0; i < 100; i++) {
2874 if (readw(baseAddr + C218_key) == keycode)
2876 moxadelay(1); /* delay 10 ms */
2878 if (readw(baseAddr + C218_key) != keycode) {
2882 writew(0, baseAddr + C218DLoad_len);
2883 writew(usum, baseAddr + C218check_sum);
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 */
2891 } while ((readb(baseAddr + C218chksum_ok) != 1) && (retry < 3));
2892 if (readb(baseAddr + C218chksum_ok) != 1) {
2895 writew(0, baseAddr + C218_key);
2896 for (i = 0; i < 100; i++) {
2897 if (readw(baseAddr + Magic_no) == Magic_code)
2899 moxadelay(1); /* delay 10 ms */
2901 if (readw(baseAddr + Magic_no) != Magic_code) {
2904 writew(1, baseAddr + Disable_IRQ);
2905 writew(0, baseAddr + Magic_no);
2906 for (i = 0; i < 100; i++) {
2907 if (readw(baseAddr + Magic_no) == Magic_code)
2909 moxadelay(1); /* delay 10 ms */
2911 if (readw(baseAddr + Magic_no) != Magic_code) {
2915 moxaIntNdx[cardno] = baseAddr + IRQindex;
2916 moxaIntPend[cardno] = baseAddr + IRQpending;
2917 moxaIntTable[cardno] = baseAddr + IRQtable;
2921 static int moxaloadc320(int cardno, void __iomem *baseAddr, int len, int *numPorts)
2924 int i, j, wlen, len2, retry;
2929 uptr = (ushort *) moxaBuff;
2930 for (i = 0; i < wlen; i++)
2931 usum += le16_to_cpu(uptr[i]);
2942 for (i = 0; i < len2; i++)
2943 writeb(moxaBuff[j + i], baseAddr + C320_LoadBuf + i);
2946 writew(len2, baseAddr + C320DLoad_len);
2947 writew(0, baseAddr + C320_key);
2948 for (i = 0; i < 10; i++) {
2949 if (readw(baseAddr + C320_key) == C320_KeyCode)
2953 if (readw(baseAddr + C320_key) != C320_KeyCode)
2956 writew(0, baseAddr + C320DLoad_len);
2957 writew(usum, baseAddr + C320check_sum);
2958 writew(0, baseAddr + C320_key);
2959 for (i = 0; i < 10; i++) {
2960 if (readw(baseAddr + C320_key) == C320_KeyCode)
2965 } while ((readb(baseAddr + C320chksum_ok) != 1) && (retry < 3));
2966 if (readb(baseAddr + C320chksum_ok) != 1)
2968 writew(0, baseAddr + C320_key);
2969 for (i = 0; i < 600; i++) {
2970 if (readw(baseAddr + Magic_no) == Magic_code)
2974 if (readw(baseAddr + Magic_no) != Magic_code)
2977 if (moxa_boards[cardno].busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
2978 writew(0x3800, baseAddr + TMS320_PORT1);
2979 writew(0x3900, baseAddr + TMS320_PORT2);
2980 writew(28499, baseAddr + TMS320_CLOCK);
2982 writew(0x3200, baseAddr + TMS320_PORT1);
2983 writew(0x3400, baseAddr + TMS320_PORT2);
2984 writew(19999, baseAddr + TMS320_CLOCK);
2986 writew(1, baseAddr + Disable_IRQ);
2987 writew(0, baseAddr + Magic_no);
2988 for (i = 0; i < 500; i++) {
2989 if (readw(baseAddr + Magic_no) == Magic_code)
2993 if (readw(baseAddr + Magic_no) != Magic_code)
2996 j = readw(baseAddr + Module_cnt);
3000 writew(j, baseAddr + Module_no);
3001 writew(0, baseAddr + Magic_no);
3002 for (i = 0; i < 600; i++) {
3003 if (readw(baseAddr + Magic_no) == Magic_code)
3007 if (readw(baseAddr + Magic_no) != Magic_code)
3010 moxaIntNdx[cardno] = baseAddr + IRQindex;
3011 moxaIntPend[cardno] = baseAddr + IRQpending;
3012 moxaIntTable[cardno] = baseAddr + IRQtable;
3017 long MoxaPortGetCurBaud(int port)
3020 if (moxaChkPort[port] == 0)
3022 return (moxaCurBaud[port]);
3026 static void MoxaSetFifo(int port, int enable)
3028 void __iomem *ofsAddr = moxaTableAddr[port];
3031 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
3032 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
3034 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
3035 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
3040 int MoxaPortSetMode(int port, int databits, int stopbits, int parity)
3042 void __iomem *ofsAddr;
3065 break; /* stop bits 1.5 */
3094 ofsAddr = moxaTableAddr[port];
3095 moxafunc(ofsAddr, FC_SetMode, val);
3099 int MoxaPortTxBufSize(int port)
3101 void __iomem *ofsAddr;
3104 ofsAddr = moxaTableAddr[port];
3105 size = readw(ofsAddr + TX_mask);
3109 int MoxaPortRxBufSize(int port)
3111 void __iomem *ofsAddr;
3114 ofsAddr = moxaTableAddr[port];
3115 size = readw(ofsAddr + RX_mask);
3119 int MoxaPortRxFree(int port)
3121 void __iomem *ofsAddr;
3122 ushort rptr, wptr, mask;
3125 ofsAddr = moxaTableAddr[port];
3126 rptr = readw(ofsAddr + RXrptr);
3127 wptr = readw(ofsAddr + RXwptr);
3128 mask = readw(ofsAddr + RX_mask);
3129 len = mask - ((wptr - rptr) & mask);
3132 int MoxaPortGetBrkCnt(int port)
3134 return (moxaBreakCnt[port]);
3137 void MoxaPortSetXonXoff(int port, int xonValue, int xoffValue)
3139 void __iomem *ofsAddr;
3141 ofsAddr = moxaTableAddr[port];
3142 writew(xonValue, ofsAddr + FuncArg);
3143 writew(xoffValue, ofsAddr + FuncArg1);
3144 writew(FC_SetXonXoff, ofsAddr + FuncCode);
3145 wait_finish(ofsAddr);
3148 int MoxaPortIsTxHold(int port)
3150 void __iomem *ofsAddr;
3153 ofsAddr = moxaTableAddr[port];
3154 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
3155 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
3156 moxafunc(ofsAddr, FC_GetCCSR, 0);
3157 val = readw(ofsAddr + FuncArg);
3161 if (readw(ofsAddr + FlagStat) & Tx_flowOff)