[PATCH] Char: moxa, pci_probing prepare
[linux-2.6.22.y-op.git] / drivers / char / moxa.c
blob9b7067b6ab73c336198af63b543aa9dd70460013
1 /*****************************************************************************/
2 /*
3 * moxa.c -- MOXA Intellio family multiport serial driver.
5 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com.tw).
7 * This code is loosely based on the Linux serial driver, written by
8 * Linus Torvalds, Theodore T'so and others.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
17 * MOXA Intellio Series Driver
18 * for : LINUX
19 * date : 1999/1/7
20 * version : 5.1
23 #include <linux/module.h>
24 #include <linux/types.h>
25 #include <linux/mm.h>
26 #include <linux/ioport.h>
27 #include <linux/errno.h>
28 #include <linux/signal.h>
29 #include <linux/sched.h>
30 #include <linux/timer.h>
31 #include <linux/interrupt.h>
32 #include <linux/tty.h>
33 #include <linux/tty_flip.h>
34 #include <linux/major.h>
35 #include <linux/string.h>
36 #include <linux/fcntl.h>
37 #include <linux/ptrace.h>
38 #include <linux/serial.h>
39 #include <linux/tty_driver.h>
40 #include <linux/delay.h>
41 #include <linux/pci.h>
42 #include <linux/init.h>
43 #include <linux/bitops.h>
45 #include <asm/system.h>
46 #include <asm/io.h>
47 #include <asm/uaccess.h>
49 #define MOXA_VERSION "5.1k"
51 #define MOXAMAJOR 172
52 #define MOXACUMAJOR 173
54 #define MAX_BOARDS 4 /* Don't change this value */
55 #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
56 #define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
59 * Define the Moxa PCI vendor and device IDs.
61 #define MOXA_BUS_TYPE_ISA 0
62 #define MOXA_BUS_TYPE_PCI 1
64 enum {
65 MOXA_BOARD_C218_PCI = 1,
66 MOXA_BOARD_C218_ISA,
67 MOXA_BOARD_C320_PCI,
68 MOXA_BOARD_C320_ISA,
69 MOXA_BOARD_CP204J,
72 static char *moxa_brdname[] =
74 "C218 Turbo PCI series",
75 "C218 Turbo ISA series",
76 "C320 Turbo PCI series",
77 "C320 Turbo ISA series",
78 "CP-204J series",
81 #ifdef CONFIG_PCI
82 static struct pci_device_id moxa_pcibrds[] = {
83 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
84 .driver_data = MOXA_BOARD_C218_PCI },
85 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
86 .driver_data = MOXA_BOARD_C320_PCI },
87 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
88 .driver_data = MOXA_BOARD_CP204J },
89 { 0 }
91 MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
92 #endif /* CONFIG_PCI */
94 struct moxa_isa_board_conf {
95 int boardType;
96 int numPorts;
97 unsigned long baseAddr;
100 static struct moxa_isa_board_conf moxa_isa_boards[] =
102 /* {MOXA_BOARD_C218_ISA,8,0xDC000}, */
105 static struct moxa_board_conf {
106 int boardType;
107 int numPorts;
108 unsigned long baseAddr;
109 int busType;
110 struct pci_dev *pdev;
112 int loadstat;
114 void __iomem *basemem;
115 void __iomem *intNdx;
116 void __iomem *intPend;
117 void __iomem *intTable;
118 } moxa_boards[MAX_BOARDS];
120 struct mxser_mstatus {
121 tcflag_t cflag;
122 int cts;
123 int dsr;
124 int ri;
125 int dcd;
128 struct moxaq_str {
129 int inq;
130 int outq;
133 struct moxa_port {
134 int type;
135 int port;
136 int close_delay;
137 unsigned short closing_wait;
138 int count;
139 int blocked_open;
140 long event; /* long req'd for set_bit --RR */
141 int asyncflags;
142 unsigned long statusflags;
143 struct tty_struct *tty;
144 int cflag;
145 wait_queue_head_t open_wait;
146 wait_queue_head_t close_wait;
148 struct timer_list emptyTimer;
150 char chkPort;
151 char lineCtrl;
152 void __iomem *tableAddr;
153 long curBaud;
154 char DCDState;
155 char lowChkFlag;
157 ushort breakCnt;
160 /* statusflags */
161 #define TXSTOPPED 0x1
162 #define LOWWAIT 0x2
163 #define EMPTYWAIT 0x4
164 #define THROTTLE 0x8
166 #define SERIAL_DO_RESTART
168 #define WAKEUP_CHARS 256
170 static int verbose = 0;
171 static int ttymajor = MOXAMAJOR;
172 /* Variables for insmod */
173 #ifdef MODULE
174 static int baseaddr[4];
175 static int type[4];
176 static int numports[4];
177 #endif
179 MODULE_AUTHOR("William Chen");
180 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
181 MODULE_LICENSE("GPL");
182 #ifdef MODULE
183 module_param_array(type, int, NULL, 0);
184 module_param_array(baseaddr, int, NULL, 0);
185 module_param_array(numports, int, NULL, 0);
186 #endif
187 module_param(ttymajor, int, 0);
188 module_param(verbose, bool, 0644);
191 * static functions:
193 static int moxa_open(struct tty_struct *, struct file *);
194 static void moxa_close(struct tty_struct *, struct file *);
195 static int moxa_write(struct tty_struct *, const unsigned char *, int);
196 static int moxa_write_room(struct tty_struct *);
197 static void moxa_flush_buffer(struct tty_struct *);
198 static int moxa_chars_in_buffer(struct tty_struct *);
199 static void moxa_flush_chars(struct tty_struct *);
200 static void moxa_put_char(struct tty_struct *, unsigned char);
201 static int moxa_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);
202 static void moxa_throttle(struct tty_struct *);
203 static void moxa_unthrottle(struct tty_struct *);
204 static void moxa_set_termios(struct tty_struct *, struct ktermios *);
205 static void moxa_stop(struct tty_struct *);
206 static void moxa_start(struct tty_struct *);
207 static void moxa_hangup(struct tty_struct *);
208 static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
209 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
210 unsigned int set, unsigned int clear);
211 static void moxa_poll(unsigned long);
212 static void set_tty_param(struct tty_struct *);
213 static int block_till_ready(struct tty_struct *, struct file *,
214 struct moxa_port *);
215 static void setup_empty_event(struct tty_struct *);
216 static void check_xmit_empty(unsigned long);
217 static void shut_down(struct moxa_port *);
218 static void receive_data(struct moxa_port *);
220 * moxa board interface functions:
222 static void MoxaDriverInit(void);
223 static int MoxaDriverIoctl(unsigned int, unsigned long, int);
224 static int MoxaDriverPoll(void);
225 static int MoxaPortsOfCard(int);
226 static int MoxaPortIsValid(int);
227 static void MoxaPortEnable(int);
228 static void MoxaPortDisable(int);
229 static long MoxaPortGetMaxBaud(int);
230 static long MoxaPortSetBaud(int, long);
231 static int MoxaPortSetTermio(int, struct ktermios *, speed_t);
232 static int MoxaPortGetLineOut(int, int *, int *);
233 static void MoxaPortLineCtrl(int, int, int);
234 static void MoxaPortFlowCtrl(int, int, int, int, int, int);
235 static int MoxaPortLineStatus(int);
236 static int MoxaPortDCDChange(int);
237 static int MoxaPortDCDON(int);
238 static void MoxaPortFlushData(int, int);
239 static int MoxaPortWriteData(int, unsigned char *, int);
240 static int MoxaPortReadData(int, struct tty_struct *tty);
241 static int MoxaPortTxQueue(int);
242 static int MoxaPortRxQueue(int);
243 static int MoxaPortTxFree(int);
244 static void MoxaPortTxDisable(int);
245 static void MoxaPortTxEnable(int);
246 static int MoxaPortResetBrkCnt(int);
247 static void MoxaPortSendBreak(int, int);
248 static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
249 static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
250 static void MoxaSetFifo(int port, int enable);
252 static const struct tty_operations moxa_ops = {
253 .open = moxa_open,
254 .close = moxa_close,
255 .write = moxa_write,
256 .write_room = moxa_write_room,
257 .flush_buffer = moxa_flush_buffer,
258 .chars_in_buffer = moxa_chars_in_buffer,
259 .flush_chars = moxa_flush_chars,
260 .put_char = moxa_put_char,
261 .ioctl = moxa_ioctl,
262 .throttle = moxa_throttle,
263 .unthrottle = moxa_unthrottle,
264 .set_termios = moxa_set_termios,
265 .stop = moxa_stop,
266 .start = moxa_start,
267 .hangup = moxa_hangup,
268 .tiocmget = moxa_tiocmget,
269 .tiocmset = moxa_tiocmset,
272 static struct tty_driver *moxaDriver;
273 static struct moxa_port moxa_ports[MAX_PORTS];
274 static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
275 static DEFINE_SPINLOCK(moxa_lock);
277 #ifdef CONFIG_PCI
278 static int __devinit moxa_pci_probe(struct pci_dev *pdev,
279 const struct pci_device_id *ent)
281 struct moxa_board_conf *board;
282 unsigned int i;
283 int board_type = ent->driver_data;
284 int retval;
286 retval = pci_enable_device(pdev);
287 if (retval)
288 goto err;
290 for (i = 0; i < MAX_BOARDS; i++)
291 if (moxa_boards[i].basemem == NULL)
292 break;
294 retval = -ENODEV;
295 if (i >= MAX_BOARDS) {
296 if (verbose)
297 printk("More than %d MOXA Intellio family boards "
298 "found. Board is ignored.\n", MAX_BOARDS);
299 goto err;
302 board = &moxa_boards[i];
303 board->basemem = pci_iomap(pdev, 2, 0x4000);
304 if (board->basemem == NULL)
305 goto err;
307 board->boardType = board_type;
308 switch (board_type) {
309 case MOXA_BOARD_C218_ISA:
310 case MOXA_BOARD_C218_PCI:
311 board->numPorts = 8;
312 break;
314 case MOXA_BOARD_CP204J:
315 board->numPorts = 4;
316 break;
317 default:
318 board->numPorts = 0;
319 break;
321 board->busType = MOXA_BUS_TYPE_PCI;
322 /* don't lose the reference in the next pci_get_device iteration */
323 board->pdev = pci_dev_get(pdev);
324 pci_set_drvdata(pdev, board);
326 return (0);
327 err:
328 return retval;
331 static void __devexit moxa_pci_remove(struct pci_dev *pdev)
333 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
335 pci_iounmap(pdev, brd->basemem);
336 brd->basemem = NULL;
337 pci_dev_put(pdev);
339 #endif /* CONFIG_PCI */
341 static int __init moxa_init(void)
343 int i, numBoards;
344 struct moxa_port *ch;
346 printk(KERN_INFO "MOXA Intellio family driver version %s\n", MOXA_VERSION);
347 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
348 if (!moxaDriver)
349 return -ENOMEM;
351 moxaDriver->owner = THIS_MODULE;
352 moxaDriver->name = "ttyMX";
353 moxaDriver->major = ttymajor;
354 moxaDriver->minor_start = 0;
355 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
356 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
357 moxaDriver->init_termios = tty_std_termios;
358 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
359 moxaDriver->init_termios.c_ispeed = 9600;
360 moxaDriver->init_termios.c_ospeed = 9600;
361 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
362 tty_set_operations(moxaDriver, &moxa_ops);
364 for (i = 0, ch = moxa_ports; i < MAX_PORTS; i++, ch++) {
365 ch->type = PORT_16550A;
366 ch->port = i;
367 ch->close_delay = 5 * HZ / 10;
368 ch->closing_wait = 30 * HZ;
369 ch->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
370 init_waitqueue_head(&ch->open_wait);
371 init_waitqueue_head(&ch->close_wait);
373 setup_timer(&ch->emptyTimer, check_xmit_empty,
374 (unsigned long)ch);
377 printk("Tty devices major number = %d\n", ttymajor);
379 if (tty_register_driver(moxaDriver)) {
380 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
381 put_tty_driver(moxaDriver);
382 return -1;
385 mod_timer(&moxaTimer, jiffies + HZ / 50);
387 /* Find the boards defined in source code */
388 numBoards = 0;
389 for (i = 0; i < MAX_BOARDS; i++) {
390 if ((moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA) ||
391 (moxa_isa_boards[i].boardType == MOXA_BOARD_C320_ISA)) {
392 moxa_boards[numBoards].boardType = moxa_isa_boards[i].boardType;
393 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
394 moxa_boards[numBoards].numPorts = 8;
395 else
396 moxa_boards[numBoards].numPorts = moxa_isa_boards[i].numPorts;
397 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
398 moxa_boards[numBoards].baseAddr = moxa_isa_boards[i].baseAddr;
399 if (verbose)
400 printk("Board %2d: %s board(baseAddr=%lx)\n",
401 numBoards + 1,
402 moxa_brdname[moxa_boards[numBoards].boardType - 1],
403 moxa_boards[numBoards].baseAddr);
404 numBoards++;
407 /* Find the boards defined form module args. */
408 #ifdef MODULE
409 for (i = 0; i < MAX_BOARDS; i++) {
410 if ((type[i] == MOXA_BOARD_C218_ISA) ||
411 (type[i] == MOXA_BOARD_C320_ISA)) {
412 if (verbose)
413 printk("Board %2d: %s board(baseAddr=%lx)\n",
414 numBoards + 1,
415 moxa_brdname[type[i] - 1],
416 (unsigned long) baseaddr[i]);
417 if (numBoards >= MAX_BOARDS) {
418 if (verbose)
419 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
420 continue;
422 moxa_boards[numBoards].boardType = type[i];
423 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
424 moxa_boards[numBoards].numPorts = 8;
425 else
426 moxa_boards[numBoards].numPorts = numports[i];
427 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
428 moxa_boards[numBoards].baseAddr = baseaddr[i];
429 numBoards++;
432 #endif
433 /* Find PCI boards here */
434 #ifdef CONFIG_PCI
436 struct pci_dev *p = NULL;
437 int n = ARRAY_SIZE(moxa_pcibrds) - 1;
438 i = 0;
439 while (i < n) {
440 while ((p = pci_get_device(moxa_pcibrds[i].vendor, moxa_pcibrds[i].device, p))!=NULL)
441 moxa_pci_probe(p, &moxa_pcibrds[i]);
442 i++;
445 #endif
446 for (i = 0; i < numBoards; i++) {
447 moxa_boards[i].basemem = ioremap(moxa_boards[i].baseAddr,
448 0x4000);
451 return (0);
454 static void __exit moxa_exit(void)
456 int i;
458 if (verbose)
459 printk("Unloading module moxa ...\n");
461 del_timer_sync(&moxaTimer);
463 for (i = 0; i < MAX_PORTS; i++)
464 del_timer_sync(&moxa_ports[i].emptyTimer);
466 if (tty_unregister_driver(moxaDriver))
467 printk("Couldn't unregister MOXA Intellio family serial driver\n");
468 put_tty_driver(moxaDriver);
470 for (i = 0; i < MAX_BOARDS; i++) {
471 #ifdef CONFIG_PCI
472 if (moxa_boards[i].busType == MOXA_BUS_TYPE_PCI)
473 moxa_pci_remove(moxa_boards[i].pdev);
474 #endif
475 if (moxa_boards[i].basemem)
476 iounmap(moxa_boards[i].basemem);
479 if (verbose)
480 printk("Done\n");
483 module_init(moxa_init);
484 module_exit(moxa_exit);
486 static int moxa_open(struct tty_struct *tty, struct file *filp)
488 struct moxa_port *ch;
489 int port;
490 int retval;
492 port = tty->index;
493 if (port == MAX_PORTS) {
494 return (0);
496 if (!MoxaPortIsValid(port)) {
497 tty->driver_data = NULL;
498 return (-ENODEV);
501 ch = &moxa_ports[port];
502 ch->count++;
503 tty->driver_data = ch;
504 ch->tty = tty;
505 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
506 ch->statusflags = 0;
507 set_tty_param(tty);
508 MoxaPortLineCtrl(ch->port, 1, 1);
509 MoxaPortEnable(ch->port);
510 ch->asyncflags |= ASYNC_INITIALIZED;
512 retval = block_till_ready(tty, filp, ch);
514 moxa_unthrottle(tty);
516 if (ch->type == PORT_16550A) {
517 MoxaSetFifo(ch->port, 1);
518 } else {
519 MoxaSetFifo(ch->port, 0);
522 return (retval);
525 static void moxa_close(struct tty_struct *tty, struct file *filp)
527 struct moxa_port *ch;
528 int port;
530 port = tty->index;
531 if (port == MAX_PORTS) {
532 return;
534 if (!MoxaPortIsValid(port)) {
535 #ifdef SERIAL_DEBUG_CLOSE
536 printk("Invalid portno in moxa_close\n");
537 #endif
538 tty->driver_data = NULL;
539 return;
541 if (tty->driver_data == NULL) {
542 return;
544 if (tty_hung_up_p(filp)) {
545 return;
547 ch = (struct moxa_port *) tty->driver_data;
549 if ((tty->count == 1) && (ch->count != 1)) {
550 printk("moxa_close: bad serial port count; tty->count is 1, "
551 "ch->count is %d\n", ch->count);
552 ch->count = 1;
554 if (--ch->count < 0) {
555 printk("moxa_close: bad serial port count, device=%s\n",
556 tty->name);
557 ch->count = 0;
559 if (ch->count) {
560 return;
562 ch->asyncflags |= ASYNC_CLOSING;
564 ch->cflag = tty->termios->c_cflag;
565 if (ch->asyncflags & ASYNC_INITIALIZED) {
566 setup_empty_event(tty);
567 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
568 del_timer_sync(&moxa_ports[ch->port].emptyTimer);
570 shut_down(ch);
571 MoxaPortFlushData(port, 2);
573 if (tty->driver->flush_buffer)
574 tty->driver->flush_buffer(tty);
575 tty_ldisc_flush(tty);
577 tty->closing = 0;
578 ch->event = 0;
579 ch->tty = NULL;
580 if (ch->blocked_open) {
581 if (ch->close_delay) {
582 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
584 wake_up_interruptible(&ch->open_wait);
586 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
587 wake_up_interruptible(&ch->close_wait);
590 static int moxa_write(struct tty_struct *tty,
591 const unsigned char *buf, int count)
593 struct moxa_port *ch;
594 int len, port;
595 unsigned long flags;
597 ch = (struct moxa_port *) tty->driver_data;
598 if (ch == NULL)
599 return (0);
600 port = ch->port;
602 spin_lock_irqsave(&moxa_lock, flags);
603 len = MoxaPortWriteData(port, (unsigned char *) buf, count);
604 spin_unlock_irqrestore(&moxa_lock, flags);
606 /*********************************************
607 if ( !(ch->statusflags & LOWWAIT) &&
608 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
609 ************************************************/
610 ch->statusflags |= LOWWAIT;
611 return (len);
614 static int moxa_write_room(struct tty_struct *tty)
616 struct moxa_port *ch;
618 if (tty->stopped)
619 return (0);
620 ch = (struct moxa_port *) tty->driver_data;
621 if (ch == NULL)
622 return (0);
623 return (MoxaPortTxFree(ch->port));
626 static void moxa_flush_buffer(struct tty_struct *tty)
628 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
630 if (ch == NULL)
631 return;
632 MoxaPortFlushData(ch->port, 1);
633 tty_wakeup(tty);
636 static int moxa_chars_in_buffer(struct tty_struct *tty)
638 int chars;
639 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
642 * Sigh...I have to check if driver_data is NULL here, because
643 * if an open() fails, the TTY subsystem eventually calls
644 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
645 * routine. And since the open() failed, we return 0 here. TDJ
647 if (ch == NULL)
648 return (0);
649 chars = MoxaPortTxQueue(ch->port);
650 if (chars) {
652 * Make it possible to wakeup anything waiting for output
653 * in tty_ioctl.c, etc.
655 if (!(ch->statusflags & EMPTYWAIT))
656 setup_empty_event(tty);
658 return (chars);
661 static void moxa_flush_chars(struct tty_struct *tty)
664 * Don't think I need this, because this is called to empty the TX
665 * buffer for the 16450, 16550, etc.
669 static void moxa_put_char(struct tty_struct *tty, unsigned char c)
671 struct moxa_port *ch;
672 int port;
673 unsigned long flags;
675 ch = (struct moxa_port *) tty->driver_data;
676 if (ch == NULL)
677 return;
678 port = ch->port;
679 spin_lock_irqsave(&moxa_lock, flags);
680 MoxaPortWriteData(port, &c, 1);
681 spin_unlock_irqrestore(&moxa_lock, flags);
682 /************************************************
683 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
684 *************************************************/
685 ch->statusflags |= LOWWAIT;
688 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
690 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
691 int port;
692 int flag = 0, dtr, rts;
694 port = tty->index;
695 if ((port != MAX_PORTS) && (!ch))
696 return (-EINVAL);
698 MoxaPortGetLineOut(ch->port, &dtr, &rts);
699 if (dtr)
700 flag |= TIOCM_DTR;
701 if (rts)
702 flag |= TIOCM_RTS;
703 dtr = MoxaPortLineStatus(ch->port);
704 if (dtr & 1)
705 flag |= TIOCM_CTS;
706 if (dtr & 2)
707 flag |= TIOCM_DSR;
708 if (dtr & 4)
709 flag |= TIOCM_CD;
710 return flag;
713 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
714 unsigned int set, unsigned int clear)
716 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
717 int port;
718 int dtr, rts;
720 port = tty->index;
721 if ((port != MAX_PORTS) && (!ch))
722 return (-EINVAL);
724 MoxaPortGetLineOut(ch->port, &dtr, &rts);
725 if (set & TIOCM_RTS)
726 rts = 1;
727 if (set & TIOCM_DTR)
728 dtr = 1;
729 if (clear & TIOCM_RTS)
730 rts = 0;
731 if (clear & TIOCM_DTR)
732 dtr = 0;
733 MoxaPortLineCtrl(ch->port, dtr, rts);
734 return 0;
737 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
738 unsigned int cmd, unsigned long arg)
740 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
741 register int port;
742 void __user *argp = (void __user *)arg;
743 int retval;
745 port = tty->index;
746 if ((port != MAX_PORTS) && (!ch))
747 return (-EINVAL);
749 switch (cmd) {
750 case TCSBRK: /* SVID version: non-zero arg --> no break */
751 retval = tty_check_change(tty);
752 if (retval)
753 return (retval);
754 setup_empty_event(tty);
755 tty_wait_until_sent(tty, 0);
756 if (!arg)
757 MoxaPortSendBreak(ch->port, 0);
758 return (0);
759 case TCSBRKP: /* support for POSIX tcsendbreak() */
760 retval = tty_check_change(tty);
761 if (retval)
762 return (retval);
763 setup_empty_event(tty);
764 tty_wait_until_sent(tty, 0);
765 MoxaPortSendBreak(ch->port, arg);
766 return (0);
767 case TIOCGSOFTCAR:
768 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) argp);
769 case TIOCSSOFTCAR:
770 if(get_user(retval, (unsigned long __user *) argp))
771 return -EFAULT;
772 arg = retval;
773 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
774 (arg ? CLOCAL : 0));
775 if (C_CLOCAL(tty))
776 ch->asyncflags &= ~ASYNC_CHECK_CD;
777 else
778 ch->asyncflags |= ASYNC_CHECK_CD;
779 return (0);
780 case TIOCGSERIAL:
781 return moxa_get_serial_info(ch, argp);
783 case TIOCSSERIAL:
784 return moxa_set_serial_info(ch, argp);
785 default:
786 retval = MoxaDriverIoctl(cmd, arg, port);
788 return (retval);
791 static void moxa_throttle(struct tty_struct *tty)
793 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
795 ch->statusflags |= THROTTLE;
798 static void moxa_unthrottle(struct tty_struct *tty)
800 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
802 ch->statusflags &= ~THROTTLE;
805 static void moxa_set_termios(struct tty_struct *tty,
806 struct ktermios *old_termios)
808 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
810 if (ch == NULL)
811 return;
812 set_tty_param(tty);
813 if (!(old_termios->c_cflag & CLOCAL) &&
814 (tty->termios->c_cflag & CLOCAL))
815 wake_up_interruptible(&ch->open_wait);
818 static void moxa_stop(struct tty_struct *tty)
820 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
822 if (ch == NULL)
823 return;
824 MoxaPortTxDisable(ch->port);
825 ch->statusflags |= TXSTOPPED;
829 static void moxa_start(struct tty_struct *tty)
831 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
833 if (ch == NULL)
834 return;
836 if (!(ch->statusflags & TXSTOPPED))
837 return;
839 MoxaPortTxEnable(ch->port);
840 ch->statusflags &= ~TXSTOPPED;
843 static void moxa_hangup(struct tty_struct *tty)
845 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
847 moxa_flush_buffer(tty);
848 shut_down(ch);
849 ch->event = 0;
850 ch->count = 0;
851 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
852 ch->tty = NULL;
853 wake_up_interruptible(&ch->open_wait);
856 static void moxa_poll(unsigned long ignored)
858 register int card;
859 struct moxa_port *ch;
860 struct tty_struct *tp;
861 int i, ports;
863 del_timer(&moxaTimer);
865 if (MoxaDriverPoll() < 0) {
866 mod_timer(&moxaTimer, jiffies + HZ / 50);
867 return;
869 for (card = 0; card < MAX_BOARDS; card++) {
870 if ((ports = MoxaPortsOfCard(card)) <= 0)
871 continue;
872 ch = &moxa_ports[card * MAX_PORTS_PER_BOARD];
873 for (i = 0; i < ports; i++, ch++) {
874 if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
875 continue;
876 if (!(ch->statusflags & THROTTLE) &&
877 (MoxaPortRxQueue(ch->port) > 0))
878 receive_data(ch);
879 if ((tp = ch->tty) == 0)
880 continue;
881 if (ch->statusflags & LOWWAIT) {
882 if (MoxaPortTxQueue(ch->port) <= WAKEUP_CHARS) {
883 if (!tp->stopped) {
884 ch->statusflags &= ~LOWWAIT;
885 tty_wakeup(tp);
889 if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch->port) > 0)) {
890 tty_insert_flip_char(tp, 0, TTY_BREAK);
891 tty_schedule_flip(tp);
893 if (MoxaPortDCDChange(ch->port)) {
894 if (ch->asyncflags & ASYNC_CHECK_CD) {
895 if (MoxaPortDCDON(ch->port))
896 wake_up_interruptible(&ch->open_wait);
897 else {
898 tty_hangup(tp);
899 wake_up_interruptible(&ch->open_wait);
900 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
907 mod_timer(&moxaTimer, jiffies + HZ / 50);
910 /******************************************************************************/
912 static void set_tty_param(struct tty_struct *tty)
914 register struct ktermios *ts;
915 struct moxa_port *ch;
916 int rts, cts, txflow, rxflow, xany;
918 ch = (struct moxa_port *) tty->driver_data;
919 ts = tty->termios;
920 if (ts->c_cflag & CLOCAL)
921 ch->asyncflags &= ~ASYNC_CHECK_CD;
922 else
923 ch->asyncflags |= ASYNC_CHECK_CD;
924 rts = cts = txflow = rxflow = xany = 0;
925 if (ts->c_cflag & CRTSCTS)
926 rts = cts = 1;
927 if (ts->c_iflag & IXON)
928 txflow = 1;
929 if (ts->c_iflag & IXOFF)
930 rxflow = 1;
931 if (ts->c_iflag & IXANY)
932 xany = 1;
933 MoxaPortFlowCtrl(ch->port, rts, cts, txflow, rxflow, xany);
934 MoxaPortSetTermio(ch->port, ts, tty_get_baud_rate(tty));
937 static int block_till_ready(struct tty_struct *tty, struct file *filp,
938 struct moxa_port *ch)
940 DECLARE_WAITQUEUE(wait,current);
941 unsigned long flags;
942 int retval;
943 int do_clocal = C_CLOCAL(tty);
946 * If the device is in the middle of being closed, then block
947 * until it's done, and then try again.
949 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
950 if (ch->asyncflags & ASYNC_CLOSING)
951 interruptible_sleep_on(&ch->close_wait);
952 #ifdef SERIAL_DO_RESTART
953 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
954 return (-EAGAIN);
955 else
956 return (-ERESTARTSYS);
957 #else
958 return (-EAGAIN);
959 #endif
962 * If non-blocking mode is set, then make the check up front
963 * and then exit.
965 if (filp->f_flags & O_NONBLOCK) {
966 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
967 return (0);
970 * Block waiting for the carrier detect and the line to become free
972 retval = 0;
973 add_wait_queue(&ch->open_wait, &wait);
974 #ifdef SERIAL_DEBUG_OPEN
975 printk("block_til_ready before block: ttys%d, count = %d\n",
976 ch->line, ch->count);
977 #endif
978 spin_lock_irqsave(&moxa_lock, flags);
979 if (!tty_hung_up_p(filp))
980 ch->count--;
981 ch->blocked_open++;
982 spin_unlock_irqrestore(&moxa_lock, flags);
984 while (1) {
985 set_current_state(TASK_INTERRUPTIBLE);
986 if (tty_hung_up_p(filp) ||
987 !(ch->asyncflags & ASYNC_INITIALIZED)) {
988 #ifdef SERIAL_DO_RESTART
989 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
990 retval = -EAGAIN;
991 else
992 retval = -ERESTARTSYS;
993 #else
994 retval = -EAGAIN;
995 #endif
996 break;
998 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
999 MoxaPortDCDON(ch->port)))
1000 break;
1002 if (signal_pending(current)) {
1003 retval = -ERESTARTSYS;
1004 break;
1006 schedule();
1008 set_current_state(TASK_RUNNING);
1009 remove_wait_queue(&ch->open_wait, &wait);
1011 spin_lock_irqsave(&moxa_lock, flags);
1012 if (!tty_hung_up_p(filp))
1013 ch->count++;
1014 ch->blocked_open--;
1015 spin_unlock_irqrestore(&moxa_lock, flags);
1016 #ifdef SERIAL_DEBUG_OPEN
1017 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1018 ch->line, ch->count);
1019 #endif
1020 if (retval)
1021 return (retval);
1022 /* FIXME: review to see if we need to use set_bit on these */
1023 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1024 return 0;
1027 static void setup_empty_event(struct tty_struct *tty)
1029 struct moxa_port *ch = tty->driver_data;
1030 unsigned long flags;
1032 spin_lock_irqsave(&moxa_lock, flags);
1033 ch->statusflags |= EMPTYWAIT;
1034 mod_timer(&moxa_ports[ch->port].emptyTimer, jiffies + HZ);
1035 spin_unlock_irqrestore(&moxa_lock, flags);
1038 static void check_xmit_empty(unsigned long data)
1040 struct moxa_port *ch;
1042 ch = (struct moxa_port *) data;
1043 del_timer_sync(&moxa_ports[ch->port].emptyTimer);
1044 if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
1045 if (MoxaPortTxQueue(ch->port) == 0) {
1046 ch->statusflags &= ~EMPTYWAIT;
1047 tty_wakeup(ch->tty);
1048 return;
1050 mod_timer(&moxa_ports[ch->port].emptyTimer, jiffies + HZ);
1051 } else
1052 ch->statusflags &= ~EMPTYWAIT;
1055 static void shut_down(struct moxa_port *ch)
1057 struct tty_struct *tp;
1059 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1060 return;
1062 tp = ch->tty;
1064 MoxaPortDisable(ch->port);
1067 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1069 if (tp->termios->c_cflag & HUPCL)
1070 MoxaPortLineCtrl(ch->port, 0, 0);
1072 ch->asyncflags &= ~ASYNC_INITIALIZED;
1075 static void receive_data(struct moxa_port *ch)
1077 struct tty_struct *tp;
1078 struct ktermios *ts;
1079 unsigned long flags;
1081 ts = NULL;
1082 tp = ch->tty;
1083 if (tp)
1084 ts = tp->termios;
1085 /**************************************************
1086 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1087 *****************************************************/
1088 if (!tp || !ts) {
1089 MoxaPortFlushData(ch->port, 0);
1090 return;
1092 spin_lock_irqsave(&moxa_lock, flags);
1093 MoxaPortReadData(ch->port, tp);
1094 spin_unlock_irqrestore(&moxa_lock, flags);
1095 tty_schedule_flip(tp);
1098 #define Magic_code 0x404
1101 * System Configuration
1104 * for C218 BIOS initialization
1106 #define C218_ConfBase 0x800
1107 #define C218_status (C218_ConfBase + 0) /* BIOS running status */
1108 #define C218_diag (C218_ConfBase + 2) /* diagnostic status */
1109 #define C218_key (C218_ConfBase + 4) /* WORD (0x218 for C218) */
1110 #define C218DLoad_len (C218_ConfBase + 6) /* WORD */
1111 #define C218check_sum (C218_ConfBase + 8) /* BYTE */
1112 #define C218chksum_ok (C218_ConfBase + 0x0a) /* BYTE (1:ok) */
1113 #define C218_TestRx (C218_ConfBase + 0x10) /* 8 bytes for 8 ports */
1114 #define C218_TestTx (C218_ConfBase + 0x18) /* 8 bytes for 8 ports */
1115 #define C218_RXerr (C218_ConfBase + 0x20) /* 8 bytes for 8 ports */
1116 #define C218_ErrFlag (C218_ConfBase + 0x28) /* 8 bytes for 8 ports */
1118 #define C218_LoadBuf 0x0F00
1119 #define C218_KeyCode 0x218
1120 #define CP204J_KeyCode 0x204
1123 * for C320 BIOS initialization
1125 #define C320_ConfBase 0x800
1126 #define C320_LoadBuf 0x0f00
1127 #define STS_init 0x05 /* for C320_status */
1129 #define C320_status C320_ConfBase + 0 /* BIOS running status */
1130 #define C320_diag C320_ConfBase + 2 /* diagnostic status */
1131 #define C320_key C320_ConfBase + 4 /* WORD (0320H for C320) */
1132 #define C320DLoad_len C320_ConfBase + 6 /* WORD */
1133 #define C320check_sum C320_ConfBase + 8 /* WORD */
1134 #define C320chksum_ok C320_ConfBase + 0x0a /* WORD (1:ok) */
1135 #define C320bapi_len C320_ConfBase + 0x0c /* WORD */
1136 #define C320UART_no C320_ConfBase + 0x0e /* WORD */
1138 #define C320_KeyCode 0x320
1140 #define FixPage_addr 0x0000 /* starting addr of static page */
1141 #define DynPage_addr 0x2000 /* starting addr of dynamic page */
1142 #define C218_start 0x3000 /* starting addr of C218 BIOS prg */
1143 #define Control_reg 0x1ff0 /* select page and reset control */
1144 #define HW_reset 0x80
1147 * Function Codes
1149 #define FC_CardReset 0x80
1150 #define FC_ChannelReset 1 /* C320 firmware not supported */
1151 #define FC_EnableCH 2
1152 #define FC_DisableCH 3
1153 #define FC_SetParam 4
1154 #define FC_SetMode 5
1155 #define FC_SetRate 6
1156 #define FC_LineControl 7
1157 #define FC_LineStatus 8
1158 #define FC_XmitControl 9
1159 #define FC_FlushQueue 10
1160 #define FC_SendBreak 11
1161 #define FC_StopBreak 12
1162 #define FC_LoopbackON 13
1163 #define FC_LoopbackOFF 14
1164 #define FC_ClrIrqTable 15
1165 #define FC_SendXon 16
1166 #define FC_SetTermIrq 17 /* C320 firmware not supported */
1167 #define FC_SetCntIrq 18 /* C320 firmware not supported */
1168 #define FC_SetBreakIrq 19
1169 #define FC_SetLineIrq 20
1170 #define FC_SetFlowCtl 21
1171 #define FC_GenIrq 22
1172 #define FC_InCD180 23
1173 #define FC_OutCD180 24
1174 #define FC_InUARTreg 23
1175 #define FC_OutUARTreg 24
1176 #define FC_SetXonXoff 25
1177 #define FC_OutCD180CCR 26
1178 #define FC_ExtIQueue 27
1179 #define FC_ExtOQueue 28
1180 #define FC_ClrLineIrq 29
1181 #define FC_HWFlowCtl 30
1182 #define FC_GetClockRate 35
1183 #define FC_SetBaud 36
1184 #define FC_SetDataMode 41
1185 #define FC_GetCCSR 43
1186 #define FC_GetDataError 45
1187 #define FC_RxControl 50
1188 #define FC_ImmSend 51
1189 #define FC_SetXonState 52
1190 #define FC_SetXoffState 53
1191 #define FC_SetRxFIFOTrig 54
1192 #define FC_SetTxFIFOCnt 55
1193 #define FC_UnixRate 56
1194 #define FC_UnixResetTimer 57
1196 #define RxFIFOTrig1 0
1197 #define RxFIFOTrig4 1
1198 #define RxFIFOTrig8 2
1199 #define RxFIFOTrig14 3
1202 * Dual-Ported RAM
1204 #define DRAM_global 0
1205 #define INT_data (DRAM_global + 0)
1206 #define Config_base (DRAM_global + 0x108)
1208 #define IRQindex (INT_data + 0)
1209 #define IRQpending (INT_data + 4)
1210 #define IRQtable (INT_data + 8)
1213 * Interrupt Status
1215 #define IntrRx 0x01 /* receiver data O.K. */
1216 #define IntrTx 0x02 /* transmit buffer empty */
1217 #define IntrFunc 0x04 /* function complete */
1218 #define IntrBreak 0x08 /* received break */
1219 #define IntrLine 0x10 /* line status change
1220 for transmitter */
1221 #define IntrIntr 0x20 /* received INTR code */
1222 #define IntrQuit 0x40 /* received QUIT code */
1223 #define IntrEOF 0x80 /* received EOF code */
1225 #define IntrRxTrigger 0x100 /* rx data count reach tigger value */
1226 #define IntrTxTrigger 0x200 /* tx data count below trigger value */
1228 #define Magic_no (Config_base + 0)
1229 #define Card_model_no (Config_base + 2)
1230 #define Total_ports (Config_base + 4)
1231 #define Module_cnt (Config_base + 8)
1232 #define Module_no (Config_base + 10)
1233 #define Timer_10ms (Config_base + 14)
1234 #define Disable_IRQ (Config_base + 20)
1235 #define TMS320_PORT1 (Config_base + 22)
1236 #define TMS320_PORT2 (Config_base + 24)
1237 #define TMS320_CLOCK (Config_base + 26)
1240 * DATA BUFFER in DRAM
1242 #define Extern_table 0x400 /* Base address of the external table
1243 (24 words * 64) total 3K bytes
1244 (24 words * 128) total 6K bytes */
1245 #define Extern_size 0x60 /* 96 bytes */
1246 #define RXrptr 0x00 /* read pointer for RX buffer */
1247 #define RXwptr 0x02 /* write pointer for RX buffer */
1248 #define TXrptr 0x04 /* read pointer for TX buffer */
1249 #define TXwptr 0x06 /* write pointer for TX buffer */
1250 #define HostStat 0x08 /* IRQ flag and general flag */
1251 #define FlagStat 0x0A
1252 #define FlowControl 0x0C /* B7 B6 B5 B4 B3 B2 B1 B0 */
1253 /* x x x x | | | | */
1254 /* | | | + CTS flow */
1255 /* | | +--- RTS flow */
1256 /* | +------ TX Xon/Xoff */
1257 /* +--------- RX Xon/Xoff */
1258 #define Break_cnt 0x0E /* received break count */
1259 #define CD180TXirq 0x10 /* if non-0: enable TX irq */
1260 #define RX_mask 0x12
1261 #define TX_mask 0x14
1262 #define Ofs_rxb 0x16
1263 #define Ofs_txb 0x18
1264 #define Page_rxb 0x1A
1265 #define Page_txb 0x1C
1266 #define EndPage_rxb 0x1E
1267 #define EndPage_txb 0x20
1268 #define Data_error 0x22
1269 #define RxTrigger 0x28
1270 #define TxTrigger 0x2a
1272 #define rRXwptr 0x34
1273 #define Low_water 0x36
1275 #define FuncCode 0x40
1276 #define FuncArg 0x42
1277 #define FuncArg1 0x44
1279 #define C218rx_size 0x2000 /* 8K bytes */
1280 #define C218tx_size 0x8000 /* 32K bytes */
1282 #define C218rx_mask (C218rx_size - 1)
1283 #define C218tx_mask (C218tx_size - 1)
1285 #define C320p8rx_size 0x2000
1286 #define C320p8tx_size 0x8000
1287 #define C320p8rx_mask (C320p8rx_size - 1)
1288 #define C320p8tx_mask (C320p8tx_size - 1)
1290 #define C320p16rx_size 0x2000
1291 #define C320p16tx_size 0x4000
1292 #define C320p16rx_mask (C320p16rx_size - 1)
1293 #define C320p16tx_mask (C320p16tx_size - 1)
1295 #define C320p24rx_size 0x2000
1296 #define C320p24tx_size 0x2000
1297 #define C320p24rx_mask (C320p24rx_size - 1)
1298 #define C320p24tx_mask (C320p24tx_size - 1)
1300 #define C320p32rx_size 0x1000
1301 #define C320p32tx_size 0x1000
1302 #define C320p32rx_mask (C320p32rx_size - 1)
1303 #define C320p32tx_mask (C320p32tx_size - 1)
1305 #define Page_size 0x2000
1306 #define Page_mask (Page_size - 1)
1307 #define C218rx_spage 3
1308 #define C218tx_spage 4
1309 #define C218rx_pageno 1
1310 #define C218tx_pageno 4
1311 #define C218buf_pageno 5
1313 #define C320p8rx_spage 3
1314 #define C320p8tx_spage 4
1315 #define C320p8rx_pgno 1
1316 #define C320p8tx_pgno 4
1317 #define C320p8buf_pgno 5
1319 #define C320p16rx_spage 3
1320 #define C320p16tx_spage 4
1321 #define C320p16rx_pgno 1
1322 #define C320p16tx_pgno 2
1323 #define C320p16buf_pgno 3
1325 #define C320p24rx_spage 3
1326 #define C320p24tx_spage 4
1327 #define C320p24rx_pgno 1
1328 #define C320p24tx_pgno 1
1329 #define C320p24buf_pgno 2
1331 #define C320p32rx_spage 3
1332 #define C320p32tx_ofs C320p32rx_size
1333 #define C320p32tx_spage 3
1334 #define C320p32buf_pgno 1
1337 * Host Status
1339 #define WakeupRx 0x01
1340 #define WakeupTx 0x02
1341 #define WakeupBreak 0x08
1342 #define WakeupLine 0x10
1343 #define WakeupIntr 0x20
1344 #define WakeupQuit 0x40
1345 #define WakeupEOF 0x80 /* used in VTIME control */
1346 #define WakeupRxTrigger 0x100
1347 #define WakeupTxTrigger 0x200
1349 * Flag status
1351 #define Rx_over 0x01
1352 #define Xoff_state 0x02
1353 #define Tx_flowOff 0x04
1354 #define Tx_enable 0x08
1355 #define CTS_state 0x10
1356 #define DSR_state 0x20
1357 #define DCD_state 0x80
1359 * FlowControl
1361 #define CTS_FlowCtl 1
1362 #define RTS_FlowCtl 2
1363 #define Tx_FlowCtl 4
1364 #define Rx_FlowCtl 8
1365 #define IXM_IXANY 0x10
1367 #define LowWater 128
1369 #define DTR_ON 1
1370 #define RTS_ON 2
1371 #define CTS_ON 1
1372 #define DSR_ON 2
1373 #define DCD_ON 8
1375 /* mode definition */
1376 #define MX_CS8 0x03
1377 #define MX_CS7 0x02
1378 #define MX_CS6 0x01
1379 #define MX_CS5 0x00
1381 #define MX_STOP1 0x00
1382 #define MX_STOP15 0x04
1383 #define MX_STOP2 0x08
1385 #define MX_PARNONE 0x00
1386 #define MX_PAREVEN 0x40
1387 #define MX_PARODD 0xC0
1390 * Query
1393 struct mon_str {
1394 int tick;
1395 int rxcnt[MAX_PORTS];
1396 int txcnt[MAX_PORTS];
1399 #define DCD_changed 0x01
1400 #define DCD_oldstate 0x80
1402 static unsigned char moxaBuff[10240];
1403 static int moxaLowWaterChk;
1404 static int moxaCard;
1405 static struct mon_str moxaLog;
1406 static int moxaFuncTout = HZ / 2;
1408 static void moxadelay(int);
1409 static void moxafunc(void __iomem *, int, ushort);
1410 static void wait_finish(void __iomem *);
1411 static void low_water_check(void __iomem *);
1412 static int moxaloadbios(int, unsigned char __user *, int);
1413 static int moxafindcard(int);
1414 static int moxaload320b(int, unsigned char __user *, int);
1415 static int moxaloadcode(int, unsigned char __user *, int);
1416 static int moxaloadc218(int, void __iomem *, int);
1417 static int moxaloadc320(int, void __iomem *, int, int *);
1419 /*****************************************************************************
1420 * Driver level functions: *
1421 * 1. MoxaDriverInit(void); *
1422 * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
1423 * 3. MoxaDriverPoll(void); *
1424 *****************************************************************************/
1425 void MoxaDriverInit(void)
1427 struct moxa_port *p;
1428 unsigned int i;
1430 moxaFuncTout = HZ / 2; /* 500 mini-seconds */
1431 moxaCard = 0;
1432 moxaLog.tick = 0;
1433 moxaLowWaterChk = 0;
1434 for (i = 0; i < MAX_PORTS; i++) {
1435 p = &moxa_ports[i];
1436 p->chkPort = 0;
1437 p->lowChkFlag = 0;
1438 p->lineCtrl = 0;
1439 moxaLog.rxcnt[i] = 0;
1440 moxaLog.txcnt[i] = 0;
1444 #define MOXA 0x400
1445 #define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1446 #define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
1447 #define MOXA_INIT_DRIVER (MOXA + 6) /* moxaCard=0 */
1448 #define MOXA_LOAD_BIOS (MOXA + 9) /* download BIOS */
1449 #define MOXA_FIND_BOARD (MOXA + 10) /* Check if MOXA card exist? */
1450 #define MOXA_LOAD_C320B (MOXA + 11) /* download 320B firmware */
1451 #define MOXA_LOAD_CODE (MOXA + 12) /* download firmware */
1452 #define MOXA_GETDATACOUNT (MOXA + 23)
1453 #define MOXA_GET_IOQUEUE (MOXA + 27)
1454 #define MOXA_FLUSH_QUEUE (MOXA + 28)
1455 #define MOXA_GET_CONF (MOXA + 35) /* configuration */
1456 #define MOXA_GET_MAJOR (MOXA + 63)
1457 #define MOXA_GET_CUMAJOR (MOXA + 64)
1458 #define MOXA_GETMSTATUS (MOXA + 65)
1460 struct dl_str {
1461 char __user *buf;
1462 int len;
1463 int cardno;
1466 static struct dl_str dltmp;
1468 void MoxaPortFlushData(int port, int mode)
1470 void __iomem *ofsAddr;
1471 if ((mode < 0) || (mode > 2))
1472 return;
1473 ofsAddr = moxa_ports[port].tableAddr;
1474 moxafunc(ofsAddr, FC_FlushQueue, mode);
1475 if (mode != 1) {
1476 moxa_ports[port].lowChkFlag = 0;
1477 low_water_check(ofsAddr);
1481 int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port)
1483 int i;
1484 int status;
1485 int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
1486 void __user *argp = (void __user *)arg;
1488 if (port == MAX_PORTS) {
1489 if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_INIT_DRIVER) &&
1490 (cmd != MOXA_LOAD_BIOS) && (cmd != MOXA_FIND_BOARD) && (cmd != MOXA_LOAD_C320B) &&
1491 (cmd != MOXA_LOAD_CODE) && (cmd != MOXA_GETDATACOUNT) &&
1492 (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
1493 (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
1494 return (-EINVAL);
1496 switch (cmd) {
1497 case MOXA_GET_CONF:
1498 if(copy_to_user(argp, &moxa_boards, MAX_BOARDS *
1499 sizeof(struct moxa_board_conf)))
1500 return -EFAULT;
1501 return (0);
1502 case MOXA_INIT_DRIVER:
1503 if ((int) arg == 0x404)
1504 MoxaDriverInit();
1505 return (0);
1506 case MOXA_GETDATACOUNT:
1507 moxaLog.tick = jiffies;
1508 if(copy_to_user(argp, &moxaLog, sizeof(struct mon_str)))
1509 return -EFAULT;
1510 return (0);
1511 case MOXA_FLUSH_QUEUE:
1512 MoxaPortFlushData(port, arg);
1513 return (0);
1514 case MOXA_GET_IOQUEUE: {
1515 struct moxaq_str __user *argm = argp;
1516 struct moxaq_str tmp;
1518 for (i = 0; i < MAX_PORTS; i++, argm++) {
1519 memset(&tmp, 0, sizeof(tmp));
1520 if (moxa_ports[i].chkPort) {
1521 tmp.inq = MoxaPortRxQueue(i);
1522 tmp.outq = MoxaPortTxQueue(i);
1524 if (copy_to_user(argm, &tmp, sizeof(tmp)))
1525 return -EFAULT;
1527 return (0);
1528 } case MOXA_GET_OQUEUE:
1529 i = MoxaPortTxQueue(port);
1530 return put_user(i, (unsigned long __user *)argp);
1531 case MOXA_GET_IQUEUE:
1532 i = MoxaPortRxQueue(port);
1533 return put_user(i, (unsigned long __user *)argp);
1534 case MOXA_GET_MAJOR:
1535 if(copy_to_user(argp, &ttymajor, sizeof(int)))
1536 return -EFAULT;
1537 return 0;
1538 case MOXA_GET_CUMAJOR:
1539 i = 0;
1540 if(copy_to_user(argp, &i, sizeof(int)))
1541 return -EFAULT;
1542 return 0;
1543 case MOXA_GETMSTATUS: {
1544 struct mxser_mstatus __user *argm = argp;
1545 struct mxser_mstatus tmp;
1546 struct moxa_port *p;
1548 for (i = 0; i < MAX_PORTS; i++, argm++) {
1549 p = &moxa_ports[i];
1550 memset(&tmp, 0, sizeof(tmp));
1551 if (!p->chkPort) {
1552 goto copy;
1553 } else {
1554 status = MoxaPortLineStatus(p->port);
1555 if (status & 1)
1556 tmp.cts = 1;
1557 if (status & 2)
1558 tmp.dsr = 1;
1559 if (status & 4)
1560 tmp.dcd = 1;
1563 if (!p->tty || !p->tty->termios)
1564 tmp.cflag = p->cflag;
1565 else
1566 tmp.cflag = p->tty->termios->c_cflag;
1567 copy:
1568 if (copy_to_user(argm, &tmp, sizeof(tmp)))
1569 return -EFAULT;
1571 return 0;
1572 } default:
1573 return (-ENOIOCTLCMD);
1574 case MOXA_LOAD_BIOS:
1575 case MOXA_FIND_BOARD:
1576 case MOXA_LOAD_C320B:
1577 case MOXA_LOAD_CODE:
1578 if (!capable(CAP_SYS_RAWIO))
1579 return -EPERM;
1580 break;
1583 if(copy_from_user(&dltmp, argp, sizeof(struct dl_str)))
1584 return -EFAULT;
1585 if(dltmp.cardno < 0 || dltmp.cardno >= MAX_BOARDS)
1586 return -EINVAL;
1588 switch(cmd)
1590 case MOXA_LOAD_BIOS:
1591 i = moxaloadbios(dltmp.cardno, dltmp.buf, dltmp.len);
1592 return (i);
1593 case MOXA_FIND_BOARD:
1594 return moxafindcard(dltmp.cardno);
1595 case MOXA_LOAD_C320B:
1596 moxaload320b(dltmp.cardno, dltmp.buf, dltmp.len);
1597 default: /* to keep gcc happy */
1598 return (0);
1599 case MOXA_LOAD_CODE:
1600 i = moxaloadcode(dltmp.cardno, dltmp.buf, dltmp.len);
1601 if (i == -1)
1602 return (-EFAULT);
1603 return (i);
1608 int MoxaDriverPoll(void)
1610 struct moxa_board_conf *brd;
1611 register ushort temp;
1612 register int card;
1613 void __iomem *ofsAddr;
1614 void __iomem *ip;
1615 int port, p, ports;
1617 if (moxaCard == 0)
1618 return (-1);
1619 for (card = 0; card < MAX_BOARDS; card++) {
1620 brd = &moxa_boards[card];
1621 if (brd->loadstat == 0)
1622 continue;
1623 if ((ports = brd->numPorts) == 0)
1624 continue;
1625 if (readb(brd->intPend) == 0xff) {
1626 ip = brd->intTable + readb(brd->intNdx);
1627 p = card * MAX_PORTS_PER_BOARD;
1628 ports <<= 1;
1629 for (port = 0; port < ports; port += 2, p++) {
1630 if ((temp = readw(ip + port)) != 0) {
1631 writew(0, ip + port);
1632 ofsAddr = moxa_ports[p].tableAddr;
1633 if (temp & IntrTx)
1634 writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
1635 if (temp & IntrBreak) {
1636 moxa_ports[p].breakCnt++;
1638 if (temp & IntrLine) {
1639 if (readb(ofsAddr + FlagStat) & DCD_state) {
1640 if ((moxa_ports[p].DCDState & DCD_oldstate) == 0)
1641 moxa_ports[p].DCDState = (DCD_oldstate |
1642 DCD_changed);
1643 } else {
1644 if (moxa_ports[p].DCDState & DCD_oldstate)
1645 moxa_ports[p].DCDState = DCD_changed;
1650 writeb(0, brd->intPend);
1652 if (moxaLowWaterChk) {
1653 p = card * MAX_PORTS_PER_BOARD;
1654 for (port = 0; port < ports; port++, p++) {
1655 if (moxa_ports[p].lowChkFlag) {
1656 moxa_ports[p].lowChkFlag = 0;
1657 ofsAddr = moxa_ports[p].tableAddr;
1658 low_water_check(ofsAddr);
1663 moxaLowWaterChk = 0;
1664 return (0);
1667 /*****************************************************************************
1668 * Card level function: *
1669 * 1. MoxaPortsOfCard(int cardno); *
1670 *****************************************************************************/
1671 int MoxaPortsOfCard(int cardno)
1674 if (moxa_boards[cardno].boardType == 0)
1675 return (0);
1676 return (moxa_boards[cardno].numPorts);
1679 /*****************************************************************************
1680 * Port level functions: *
1681 * 1. MoxaPortIsValid(int port); *
1682 * 2. MoxaPortEnable(int port); *
1683 * 3. MoxaPortDisable(int port); *
1684 * 4. MoxaPortGetMaxBaud(int port); *
1685 * 6. MoxaPortSetBaud(int port, long baud); *
1686 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1687 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1688 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1689 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1690 * 12. MoxaPortLineStatus(int port); *
1691 * 13. MoxaPortDCDChange(int port); *
1692 * 14. MoxaPortDCDON(int port); *
1693 * 15. MoxaPortFlushData(int port, int mode); *
1694 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1695 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
1696 * 20. MoxaPortTxQueue(int port); *
1697 * 21. MoxaPortTxFree(int port); *
1698 * 22. MoxaPortRxQueue(int port); *
1699 * 24. MoxaPortTxDisable(int port); *
1700 * 25. MoxaPortTxEnable(int port); *
1701 * 27. MoxaPortResetBrkCnt(int port); *
1702 * 30. MoxaPortSendBreak(int port, int ticks); *
1703 *****************************************************************************/
1705 * Moxa Port Number Description:
1707 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1708 * the port number using in MOXA driver functions will be 0 to 31 for
1709 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1710 * to 127 for fourth. For example, if you setup three MOXA boards,
1711 * first board is C218, second board is C320-16 and third board is
1712 * C320-32. The port number of first board (C218 - 8 ports) is from
1713 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1714 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1715 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1716 * 127 will be invalid.
1719 * Moxa Functions Description:
1721 * Function 1: Driver initialization routine, this routine must be
1722 * called when initialized driver.
1723 * Syntax:
1724 * void MoxaDriverInit();
1727 * Function 2: Moxa driver private IOCTL command processing.
1728 * Syntax:
1729 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1731 * unsigned int cmd : IOCTL command
1732 * unsigned long arg : IOCTL argument
1733 * int port : port number (0 - 127)
1735 * return: 0 (OK)
1736 * -EINVAL
1737 * -ENOIOCTLCMD
1740 * Function 3: Moxa driver polling process routine.
1741 * Syntax:
1742 * int MoxaDriverPoll(void);
1744 * return: 0 ; polling O.K.
1745 * -1 : no any Moxa card.
1748 * Function 4: Get the ports of this card.
1749 * Syntax:
1750 * int MoxaPortsOfCard(int cardno);
1752 * int cardno : card number (0 - 3)
1754 * return: 0 : this card is invalid
1755 * 8/16/24/32
1758 * Function 5: Check this port is valid or invalid
1759 * Syntax:
1760 * int MoxaPortIsValid(int port);
1761 * int port : port number (0 - 127, ref port description)
1763 * return: 0 : this port is invalid
1764 * 1 : this port is valid
1767 * Function 6: Enable this port to start Tx/Rx data.
1768 * Syntax:
1769 * void MoxaPortEnable(int port);
1770 * int port : port number (0 - 127)
1773 * Function 7: Disable this port
1774 * Syntax:
1775 * void MoxaPortDisable(int port);
1776 * int port : port number (0 - 127)
1779 * Function 8: Get the maximun available baud rate of this port.
1780 * Syntax:
1781 * long MoxaPortGetMaxBaud(int port);
1782 * int port : port number (0 - 127)
1784 * return: 0 : this port is invalid
1785 * 38400/57600/115200 bps
1788 * Function 10: Setting baud rate of this port.
1789 * Syntax:
1790 * long MoxaPortSetBaud(int port, long baud);
1791 * int port : port number (0 - 127)
1792 * long baud : baud rate (50 - 115200)
1794 * return: 0 : this port is invalid or baud < 50
1795 * 50 - 115200 : the real baud rate set to the port, if
1796 * the argument baud is large than maximun
1797 * available baud rate, the real setting
1798 * baud rate will be the maximun baud rate.
1801 * Function 12: Configure the port.
1802 * Syntax:
1803 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1804 * int port : port number (0 - 127)
1805 * struct ktermios * termio : termio structure pointer
1806 * speed_t baud : baud rate
1808 * return: -1 : this port is invalid or termio == NULL
1809 * 0 : setting O.K.
1812 * Function 13: Get the DTR/RTS state of this port.
1813 * Syntax:
1814 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1815 * int port : port number (0 - 127)
1816 * int * dtrState : pointer to INT to receive the current DTR
1817 * state. (if NULL, this function will not
1818 * write to this address)
1819 * int * rtsState : pointer to INT to receive the current RTS
1820 * state. (if NULL, this function will not
1821 * write to this address)
1823 * return: -1 : this port is invalid
1824 * 0 : O.K.
1827 * Function 14: Setting the DTR/RTS output state of this port.
1828 * Syntax:
1829 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1830 * int port : port number (0 - 127)
1831 * int dtrState : DTR output state (0: off, 1: on)
1832 * int rtsState : RTS output state (0: off, 1: on)
1835 * Function 15: Setting the flow control of this port.
1836 * Syntax:
1837 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1838 * int txFlow,int xany);
1839 * int port : port number (0 - 127)
1840 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1841 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1842 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1843 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1844 * int xany : S/W XANY flow control (0: no, 1: yes)
1847 * Function 16: Get ths line status of this port
1848 * Syntax:
1849 * int MoxaPortLineStatus(int port);
1850 * int port : port number (0 - 127)
1852 * return: Bit 0 - CTS state (0: off, 1: on)
1853 * Bit 1 - DSR state (0: off, 1: on)
1854 * Bit 2 - DCD state (0: off, 1: on)
1857 * Function 17: Check the DCD state has changed since the last read
1858 * of this function.
1859 * Syntax:
1860 * int MoxaPortDCDChange(int port);
1861 * int port : port number (0 - 127)
1863 * return: 0 : no changed
1864 * 1 : DCD has changed
1867 * Function 18: Check ths current DCD state is ON or not.
1868 * Syntax:
1869 * int MoxaPortDCDON(int port);
1870 * int port : port number (0 - 127)
1872 * return: 0 : DCD off
1873 * 1 : DCD on
1876 * Function 19: Flush the Rx/Tx buffer data of this port.
1877 * Syntax:
1878 * void MoxaPortFlushData(int port, int mode);
1879 * int port : port number (0 - 127)
1880 * int mode
1881 * 0 : flush the Rx buffer
1882 * 1 : flush the Tx buffer
1883 * 2 : flush the Rx and Tx buffer
1886 * Function 20: Write data.
1887 * Syntax:
1888 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1889 * int port : port number (0 - 127)
1890 * unsigned char * buffer : pointer to write data buffer.
1891 * int length : write data length
1893 * return: 0 - length : real write data length
1896 * Function 21: Read data.
1897 * Syntax:
1898 * int MoxaPortReadData(int port, struct tty_struct *tty);
1899 * int port : port number (0 - 127)
1900 * struct tty_struct *tty : tty for data
1902 * return: 0 - length : real read data length
1905 * Function 24: Get the Tx buffer current queued data bytes
1906 * Syntax:
1907 * int MoxaPortTxQueue(int port);
1908 * int port : port number (0 - 127)
1910 * return: .. : Tx buffer current queued data bytes
1913 * Function 25: Get the Tx buffer current free space
1914 * Syntax:
1915 * int MoxaPortTxFree(int port);
1916 * int port : port number (0 - 127)
1918 * return: .. : Tx buffer current free space
1921 * Function 26: Get the Rx buffer current queued data bytes
1922 * Syntax:
1923 * int MoxaPortRxQueue(int port);
1924 * int port : port number (0 - 127)
1926 * return: .. : Rx buffer current queued data bytes
1929 * Function 28: Disable port data transmission.
1930 * Syntax:
1931 * void MoxaPortTxDisable(int port);
1932 * int port : port number (0 - 127)
1935 * Function 29: Enable port data transmission.
1936 * Syntax:
1937 * void MoxaPortTxEnable(int port);
1938 * int port : port number (0 - 127)
1941 * Function 31: Get the received BREAK signal count and reset it.
1942 * Syntax:
1943 * int MoxaPortResetBrkCnt(int port);
1944 * int port : port number (0 - 127)
1946 * return: 0 - .. : BREAK signal count
1949 * Function 34: Send out a BREAK signal.
1950 * Syntax:
1951 * void MoxaPortSendBreak(int port, int ms100);
1952 * int port : port number (0 - 127)
1953 * int ms100 : break signal time interval.
1954 * unit: 100 mini-second. if ms100 == 0, it will
1955 * send out a about 250 ms BREAK signal.
1958 int MoxaPortIsValid(int port)
1961 if (moxaCard == 0)
1962 return (0);
1963 if (moxa_ports[port].chkPort == 0)
1964 return (0);
1965 return (1);
1968 void MoxaPortEnable(int port)
1970 void __iomem *ofsAddr;
1971 int MoxaPortLineStatus(int);
1972 short lowwater = 512;
1974 ofsAddr = moxa_ports[port].tableAddr;
1975 writew(lowwater, ofsAddr + Low_water);
1976 moxa_ports[port].breakCnt = 0;
1977 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
1978 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
1979 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1980 } else {
1981 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
1984 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1985 moxafunc(ofsAddr, FC_FlushQueue, 2);
1987 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1988 MoxaPortLineStatus(port);
1991 void MoxaPortDisable(int port)
1993 void __iomem *ofsAddr = moxa_ports[port].tableAddr;
1995 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1996 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1997 writew(0, ofsAddr + HostStat);
1998 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
2001 long MoxaPortGetMaxBaud(int port)
2003 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2004 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI))
2005 return (460800L);
2006 else
2007 return (921600L);
2011 long MoxaPortSetBaud(int port, long baud)
2013 void __iomem *ofsAddr;
2014 long max, clock;
2015 unsigned int val;
2017 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
2018 return (0);
2019 ofsAddr = moxa_ports[port].tableAddr;
2020 if (baud > max)
2021 baud = max;
2022 if (max == 38400L)
2023 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
2024 else if (max == 57600L)
2025 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
2026 else
2027 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
2028 val = clock / baud;
2029 moxafunc(ofsAddr, FC_SetBaud, val);
2030 baud = clock / val;
2031 moxa_ports[port].curBaud = baud;
2032 return (baud);
2035 int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud)
2037 void __iomem *ofsAddr;
2038 tcflag_t cflag;
2039 tcflag_t mode = 0;
2041 if (moxa_ports[port].chkPort == 0 || termio == 0)
2042 return (-1);
2043 ofsAddr = moxa_ports[port].tableAddr;
2044 cflag = termio->c_cflag; /* termio->c_cflag */
2046 mode = termio->c_cflag & CSIZE;
2047 if (mode == CS5)
2048 mode = MX_CS5;
2049 else if (mode == CS6)
2050 mode = MX_CS6;
2051 else if (mode == CS7)
2052 mode = MX_CS7;
2053 else if (mode == CS8)
2054 mode = MX_CS8;
2056 if (termio->c_cflag & CSTOPB) {
2057 if (mode == MX_CS5)
2058 mode |= MX_STOP15;
2059 else
2060 mode |= MX_STOP2;
2061 } else
2062 mode |= MX_STOP1;
2064 if (termio->c_cflag & PARENB) {
2065 if (termio->c_cflag & PARODD)
2066 mode |= MX_PARODD;
2067 else
2068 mode |= MX_PAREVEN;
2069 } else
2070 mode |= MX_PARNONE;
2072 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
2074 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2075 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2076 if (baud >= 921600L)
2077 return (-1);
2079 MoxaPortSetBaud(port, baud);
2081 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
2082 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
2083 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
2084 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
2085 wait_finish(ofsAddr);
2088 return (0);
2091 int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState)
2094 if (!MoxaPortIsValid(port))
2095 return (-1);
2096 if (dtrState) {
2097 if (moxa_ports[port].lineCtrl & DTR_ON)
2098 *dtrState = 1;
2099 else
2100 *dtrState = 0;
2102 if (rtsState) {
2103 if (moxa_ports[port].lineCtrl & RTS_ON)
2104 *rtsState = 1;
2105 else
2106 *rtsState = 0;
2108 return (0);
2111 void MoxaPortLineCtrl(int port, int dtr, int rts)
2113 void __iomem *ofsAddr;
2114 int mode;
2116 ofsAddr = moxa_ports[port].tableAddr;
2117 mode = 0;
2118 if (dtr)
2119 mode |= DTR_ON;
2120 if (rts)
2121 mode |= RTS_ON;
2122 moxa_ports[port].lineCtrl = mode;
2123 moxafunc(ofsAddr, FC_LineControl, mode);
2126 void MoxaPortFlowCtrl(int port, int rts, int cts, int txflow, int rxflow, int txany)
2128 void __iomem *ofsAddr;
2129 int mode;
2131 ofsAddr = moxa_ports[port].tableAddr;
2132 mode = 0;
2133 if (rts)
2134 mode |= RTS_FlowCtl;
2135 if (cts)
2136 mode |= CTS_FlowCtl;
2137 if (txflow)
2138 mode |= Tx_FlowCtl;
2139 if (rxflow)
2140 mode |= Rx_FlowCtl;
2141 if (txany)
2142 mode |= IXM_IXANY;
2143 moxafunc(ofsAddr, FC_SetFlowCtl, mode);
2146 int MoxaPortLineStatus(int port)
2148 void __iomem *ofsAddr;
2149 int val;
2151 ofsAddr = moxa_ports[port].tableAddr;
2152 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2153 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2154 moxafunc(ofsAddr, FC_LineStatus, 0);
2155 val = readw(ofsAddr + FuncArg);
2156 } else {
2157 val = readw(ofsAddr + FlagStat) >> 4;
2159 val &= 0x0B;
2160 if (val & 8) {
2161 val |= 4;
2162 if ((moxa_ports[port].DCDState & DCD_oldstate) == 0)
2163 moxa_ports[port].DCDState = (DCD_oldstate | DCD_changed);
2164 } else {
2165 if (moxa_ports[port].DCDState & DCD_oldstate)
2166 moxa_ports[port].DCDState = DCD_changed;
2168 val &= 7;
2169 return (val);
2172 int MoxaPortDCDChange(int port)
2174 int n;
2176 if (moxa_ports[port].chkPort == 0)
2177 return (0);
2178 n = moxa_ports[port].DCDState;
2179 moxa_ports[port].DCDState &= ~DCD_changed;
2180 n &= DCD_changed;
2181 return (n);
2184 int MoxaPortDCDON(int port)
2186 int n;
2188 if (moxa_ports[port].chkPort == 0)
2189 return (0);
2190 if (moxa_ports[port].DCDState & DCD_oldstate)
2191 n = 1;
2192 else
2193 n = 0;
2194 return (n);
2197 int MoxaPortWriteData(int port, unsigned char * buffer, int len)
2199 int c, total, i;
2200 ushort tail;
2201 int cnt;
2202 ushort head, tx_mask, spage, epage;
2203 ushort pageno, pageofs, bufhead;
2204 void __iomem *baseAddr, *ofsAddr, *ofs;
2206 ofsAddr = moxa_ports[port].tableAddr;
2207 baseAddr = moxa_boards[port / MAX_PORTS_PER_BOARD].basemem;
2208 tx_mask = readw(ofsAddr + TX_mask);
2209 spage = readw(ofsAddr + Page_txb);
2210 epage = readw(ofsAddr + EndPage_txb);
2211 tail = readw(ofsAddr + TXwptr);
2212 head = readw(ofsAddr + TXrptr);
2213 c = (head > tail) ? (head - tail - 1)
2214 : (head - tail + tx_mask);
2215 if (c > len)
2216 c = len;
2217 moxaLog.txcnt[port] += c;
2218 total = c;
2219 if (spage == epage) {
2220 bufhead = readw(ofsAddr + Ofs_txb);
2221 writew(spage, baseAddr + Control_reg);
2222 while (c > 0) {
2223 if (head > tail)
2224 len = head - tail - 1;
2225 else
2226 len = tx_mask + 1 - tail;
2227 len = (c > len) ? len : c;
2228 ofs = baseAddr + DynPage_addr + bufhead + tail;
2229 for (i = 0; i < len; i++)
2230 writeb(*buffer++, ofs + i);
2231 tail = (tail + len) & tx_mask;
2232 c -= len;
2234 writew(tail, ofsAddr + TXwptr);
2235 } else {
2236 len = c;
2237 pageno = spage + (tail >> 13);
2238 pageofs = tail & Page_mask;
2239 do {
2240 cnt = Page_size - pageofs;
2241 if (cnt > c)
2242 cnt = c;
2243 c -= cnt;
2244 writeb(pageno, baseAddr + Control_reg);
2245 ofs = baseAddr + DynPage_addr + pageofs;
2246 for (i = 0; i < cnt; i++)
2247 writeb(*buffer++, ofs + i);
2248 if (c == 0) {
2249 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2250 break;
2252 if (++pageno == epage)
2253 pageno = spage;
2254 pageofs = 0;
2255 } while (1);
2257 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2258 return (total);
2261 int MoxaPortReadData(int port, struct tty_struct *tty)
2263 register ushort head, pageofs;
2264 int i, count, cnt, len, total, remain;
2265 ushort tail, rx_mask, spage, epage;
2266 ushort pageno, bufhead;
2267 void __iomem *baseAddr, *ofsAddr, *ofs;
2269 ofsAddr = moxa_ports[port].tableAddr;
2270 baseAddr = moxa_boards[port / MAX_PORTS_PER_BOARD].basemem;
2271 head = readw(ofsAddr + RXrptr);
2272 tail = readw(ofsAddr + RXwptr);
2273 rx_mask = readw(ofsAddr + RX_mask);
2274 spage = readw(ofsAddr + Page_rxb);
2275 epage = readw(ofsAddr + EndPage_rxb);
2276 count = (tail >= head) ? (tail - head)
2277 : (tail - head + rx_mask + 1);
2278 if (count == 0)
2279 return 0;
2281 total = count;
2282 remain = count - total;
2283 moxaLog.rxcnt[port] += total;
2284 count = total;
2285 if (spage == epage) {
2286 bufhead = readw(ofsAddr + Ofs_rxb);
2287 writew(spage, baseAddr + Control_reg);
2288 while (count > 0) {
2289 if (tail >= head)
2290 len = tail - head;
2291 else
2292 len = rx_mask + 1 - head;
2293 len = (count > len) ? len : count;
2294 ofs = baseAddr + DynPage_addr + bufhead + head;
2295 for (i = 0; i < len; i++)
2296 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2297 head = (head + len) & rx_mask;
2298 count -= len;
2300 writew(head, ofsAddr + RXrptr);
2301 } else {
2302 len = count;
2303 pageno = spage + (head >> 13);
2304 pageofs = head & Page_mask;
2305 do {
2306 cnt = Page_size - pageofs;
2307 if (cnt > count)
2308 cnt = count;
2309 count -= cnt;
2310 writew(pageno, baseAddr + Control_reg);
2311 ofs = baseAddr + DynPage_addr + pageofs;
2312 for (i = 0; i < cnt; i++)
2313 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2314 if (count == 0) {
2315 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2316 break;
2318 if (++pageno == epage)
2319 pageno = spage;
2320 pageofs = 0;
2321 } while (1);
2323 if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2324 moxaLowWaterChk = 1;
2325 moxa_ports[port].lowChkFlag = 1;
2327 return (total);
2331 int MoxaPortTxQueue(int port)
2333 void __iomem *ofsAddr;
2334 ushort rptr, wptr, mask;
2335 int len;
2337 ofsAddr = moxa_ports[port].tableAddr;
2338 rptr = readw(ofsAddr + TXrptr);
2339 wptr = readw(ofsAddr + TXwptr);
2340 mask = readw(ofsAddr + TX_mask);
2341 len = (wptr - rptr) & mask;
2342 return (len);
2345 int MoxaPortTxFree(int port)
2347 void __iomem *ofsAddr;
2348 ushort rptr, wptr, mask;
2349 int len;
2351 ofsAddr = moxa_ports[port].tableAddr;
2352 rptr = readw(ofsAddr + TXrptr);
2353 wptr = readw(ofsAddr + TXwptr);
2354 mask = readw(ofsAddr + TX_mask);
2355 len = mask - ((wptr - rptr) & mask);
2356 return (len);
2359 int MoxaPortRxQueue(int port)
2361 void __iomem *ofsAddr;
2362 ushort rptr, wptr, mask;
2363 int len;
2365 ofsAddr = moxa_ports[port].tableAddr;
2366 rptr = readw(ofsAddr + RXrptr);
2367 wptr = readw(ofsAddr + RXwptr);
2368 mask = readw(ofsAddr + RX_mask);
2369 len = (wptr - rptr) & mask;
2370 return (len);
2374 void MoxaPortTxDisable(int port)
2376 void __iomem *ofsAddr;
2378 ofsAddr = moxa_ports[port].tableAddr;
2379 moxafunc(ofsAddr, FC_SetXoffState, Magic_code);
2382 void MoxaPortTxEnable(int port)
2384 void __iomem *ofsAddr;
2386 ofsAddr = moxa_ports[port].tableAddr;
2387 moxafunc(ofsAddr, FC_SetXonState, Magic_code);
2391 int MoxaPortResetBrkCnt(int port)
2393 ushort cnt;
2394 cnt = moxa_ports[port].breakCnt;
2395 moxa_ports[port].breakCnt = 0;
2396 return (cnt);
2400 void MoxaPortSendBreak(int port, int ms100)
2402 void __iomem *ofsAddr;
2404 ofsAddr = moxa_ports[port].tableAddr;
2405 if (ms100) {
2406 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2407 moxadelay(ms100 * (HZ / 10));
2408 } else {
2409 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2410 moxadelay(HZ / 4); /* 250 ms */
2412 moxafunc(ofsAddr, FC_StopBreak, Magic_code);
2415 static int moxa_get_serial_info(struct moxa_port *info,
2416 struct serial_struct __user *retinfo)
2418 struct serial_struct tmp;
2420 memset(&tmp, 0, sizeof(tmp));
2421 tmp.type = info->type;
2422 tmp.line = info->port;
2423 tmp.port = 0;
2424 tmp.irq = 0;
2425 tmp.flags = info->asyncflags;
2426 tmp.baud_base = 921600;
2427 tmp.close_delay = info->close_delay;
2428 tmp.closing_wait = info->closing_wait;
2429 tmp.custom_divisor = 0;
2430 tmp.hub6 = 0;
2431 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2432 return -EFAULT;
2433 return (0);
2437 static int moxa_set_serial_info(struct moxa_port *info,
2438 struct serial_struct __user *new_info)
2440 struct serial_struct new_serial;
2442 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2443 return -EFAULT;
2445 if ((new_serial.irq != 0) ||
2446 (new_serial.port != 0) ||
2447 // (new_serial.type != info->type) ||
2448 (new_serial.custom_divisor != 0) ||
2449 (new_serial.baud_base != 921600))
2450 return (-EPERM);
2452 if (!capable(CAP_SYS_ADMIN)) {
2453 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2454 (info->asyncflags & ~ASYNC_USR_MASK)))
2455 return (-EPERM);
2456 } else {
2457 info->close_delay = new_serial.close_delay * HZ / 100;
2458 info->closing_wait = new_serial.closing_wait * HZ / 100;
2461 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2462 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2464 if (new_serial.type == PORT_16550A) {
2465 MoxaSetFifo(info->port, 1);
2466 } else {
2467 MoxaSetFifo(info->port, 0);
2470 info->type = new_serial.type;
2471 return (0);
2476 /*****************************************************************************
2477 * Static local functions: *
2478 *****************************************************************************/
2480 * moxadelay - delays a specified number ticks
2482 static void moxadelay(int tick)
2484 unsigned long st, et;
2486 st = jiffies;
2487 et = st + tick;
2488 while (time_before(jiffies, et));
2491 static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
2494 writew(arg, ofsAddr + FuncArg);
2495 writew(cmd, ofsAddr + FuncCode);
2496 wait_finish(ofsAddr);
2499 static void wait_finish(void __iomem *ofsAddr)
2501 unsigned long i, j;
2503 i = jiffies;
2504 while (readw(ofsAddr + FuncCode) != 0) {
2505 j = jiffies;
2506 if ((j - i) > moxaFuncTout) {
2507 return;
2512 static void low_water_check(void __iomem *ofsAddr)
2514 int len;
2515 ushort rptr, wptr, mask;
2517 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2518 rptr = readw(ofsAddr + RXrptr);
2519 wptr = readw(ofsAddr + RXwptr);
2520 mask = readw(ofsAddr + RX_mask);
2521 len = (wptr - rptr) & mask;
2522 if (len <= Low_water)
2523 moxafunc(ofsAddr, FC_SendXon, 0);
2527 static int moxaloadbios(int cardno, unsigned char __user *tmp, int len)
2529 void __iomem *baseAddr;
2530 int i;
2532 if(copy_from_user(moxaBuff, tmp, len))
2533 return -EFAULT;
2534 baseAddr = moxa_boards[cardno].basemem;
2535 writeb(HW_reset, baseAddr + Control_reg); /* reset */
2536 moxadelay(1); /* delay 10 ms */
2537 for (i = 0; i < 4096; i++)
2538 writeb(0, baseAddr + i); /* clear fix page */
2539 for (i = 0; i < len; i++)
2540 writeb(moxaBuff[i], baseAddr + i); /* download BIOS */
2541 writeb(0, baseAddr + Control_reg); /* restart */
2542 return (0);
2545 static int moxafindcard(int cardno)
2547 void __iomem *baseAddr;
2548 ushort tmp;
2550 baseAddr = moxa_boards[cardno].basemem;
2551 switch (moxa_boards[cardno].boardType) {
2552 case MOXA_BOARD_C218_ISA:
2553 case MOXA_BOARD_C218_PCI:
2554 if ((tmp = readw(baseAddr + C218_key)) != C218_KeyCode) {
2555 return (-1);
2557 break;
2558 case MOXA_BOARD_CP204J:
2559 if ((tmp = readw(baseAddr + C218_key)) != CP204J_KeyCode) {
2560 return (-1);
2562 break;
2563 default:
2564 if ((tmp = readw(baseAddr + C320_key)) != C320_KeyCode) {
2565 return (-1);
2567 if ((tmp = readw(baseAddr + C320_status)) != STS_init) {
2568 return (-2);
2571 return (0);
2574 static int moxaload320b(int cardno, unsigned char __user *tmp, int len)
2576 void __iomem *baseAddr;
2577 int i;
2579 if(len > sizeof(moxaBuff))
2580 return -EINVAL;
2581 if(copy_from_user(moxaBuff, tmp, len))
2582 return -EFAULT;
2583 baseAddr = moxa_boards[cardno].basemem;
2584 writew(len - 7168 - 2, baseAddr + C320bapi_len);
2585 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
2586 for (i = 0; i < 7168; i++)
2587 writeb(moxaBuff[i], baseAddr + DynPage_addr + i);
2588 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
2589 for (i = 0; i < (len - 7168); i++)
2590 writeb(moxaBuff[i + 7168], baseAddr + DynPage_addr + i);
2591 return (0);
2594 static int moxaloadcode(int cardno, unsigned char __user *tmp, int len)
2596 void __iomem *baseAddr, *ofsAddr;
2597 int retval, port, i;
2599 if(copy_from_user(moxaBuff, tmp, len))
2600 return -EFAULT;
2601 baseAddr = moxa_boards[cardno].basemem;
2602 switch (moxa_boards[cardno].boardType) {
2603 case MOXA_BOARD_C218_ISA:
2604 case MOXA_BOARD_C218_PCI:
2605 case MOXA_BOARD_CP204J:
2606 retval = moxaloadc218(cardno, baseAddr, len);
2607 if (retval)
2608 return (retval);
2609 port = cardno * MAX_PORTS_PER_BOARD;
2610 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2611 struct moxa_port *p = &moxa_ports[port];
2613 p->chkPort = 1;
2614 p->curBaud = 9600L;
2615 p->DCDState = 0;
2616 p->tableAddr = baseAddr + Extern_table + Extern_size * i;
2617 ofsAddr = p->tableAddr;
2618 writew(C218rx_mask, ofsAddr + RX_mask);
2619 writew(C218tx_mask, ofsAddr + TX_mask);
2620 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
2621 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
2623 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
2624 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
2627 break;
2628 default:
2629 retval = moxaloadc320(cardno, baseAddr, len,
2630 &moxa_boards[cardno].numPorts);
2631 if (retval)
2632 return (retval);
2633 port = cardno * MAX_PORTS_PER_BOARD;
2634 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2635 struct moxa_port *p = &moxa_ports[port];
2637 p->chkPort = 1;
2638 p->curBaud = 9600L;
2639 p->DCDState = 0;
2640 p->tableAddr = baseAddr + Extern_table + Extern_size * i;
2641 ofsAddr = p->tableAddr;
2642 if (moxa_boards[cardno].numPorts == 8) {
2643 writew(C320p8rx_mask, ofsAddr + RX_mask);
2644 writew(C320p8tx_mask, ofsAddr + TX_mask);
2645 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
2646 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
2647 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
2648 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
2650 } else if (moxa_boards[cardno].numPorts == 16) {
2651 writew(C320p16rx_mask, ofsAddr + RX_mask);
2652 writew(C320p16tx_mask, ofsAddr + TX_mask);
2653 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
2654 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
2655 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
2656 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
2658 } else if (moxa_boards[cardno].numPorts == 24) {
2659 writew(C320p24rx_mask, ofsAddr + RX_mask);
2660 writew(C320p24tx_mask, ofsAddr + TX_mask);
2661 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
2662 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
2663 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
2664 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2665 } else if (moxa_boards[cardno].numPorts == 32) {
2666 writew(C320p32rx_mask, ofsAddr + RX_mask);
2667 writew(C320p32tx_mask, ofsAddr + TX_mask);
2668 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
2669 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
2670 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
2671 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
2672 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2675 break;
2677 moxa_boards[cardno].loadstat = 1;
2678 return (0);
2681 static int moxaloadc218(int cardno, void __iomem *baseAddr, int len)
2683 char retry;
2684 int i, j, len1, len2;
2685 ushort usum, *ptr, keycode;
2687 if (moxa_boards[cardno].boardType == MOXA_BOARD_CP204J)
2688 keycode = CP204J_KeyCode;
2689 else
2690 keycode = C218_KeyCode;
2691 usum = 0;
2692 len1 = len >> 1;
2693 ptr = (ushort *) moxaBuff;
2694 for (i = 0; i < len1; i++)
2695 usum += le16_to_cpu(*(ptr + i));
2696 retry = 0;
2697 do {
2698 len1 = len >> 1;
2699 j = 0;
2700 while (len1) {
2701 len2 = (len1 > 2048) ? 2048 : len1;
2702 len1 -= len2;
2703 for (i = 0; i < len2 << 1; i++)
2704 writeb(moxaBuff[i + j], baseAddr + C218_LoadBuf + i);
2705 j += i;
2707 writew(len2, baseAddr + C218DLoad_len);
2708 writew(0, baseAddr + C218_key);
2709 for (i = 0; i < 100; i++) {
2710 if (readw(baseAddr + C218_key) == keycode)
2711 break;
2712 moxadelay(1); /* delay 10 ms */
2714 if (readw(baseAddr + C218_key) != keycode) {
2715 return (-1);
2718 writew(0, baseAddr + C218DLoad_len);
2719 writew(usum, baseAddr + C218check_sum);
2720 writew(0, baseAddr + C218_key);
2721 for (i = 0; i < 100; i++) {
2722 if (readw(baseAddr + C218_key) == keycode)
2723 break;
2724 moxadelay(1); /* delay 10 ms */
2726 retry++;
2727 } while ((readb(baseAddr + C218chksum_ok) != 1) && (retry < 3));
2728 if (readb(baseAddr + C218chksum_ok) != 1) {
2729 return (-1);
2731 writew(0, baseAddr + C218_key);
2732 for (i = 0; i < 100; i++) {
2733 if (readw(baseAddr + Magic_no) == Magic_code)
2734 break;
2735 moxadelay(1); /* delay 10 ms */
2737 if (readw(baseAddr + Magic_no) != Magic_code) {
2738 return (-1);
2740 writew(1, baseAddr + Disable_IRQ);
2741 writew(0, baseAddr + Magic_no);
2742 for (i = 0; i < 100; i++) {
2743 if (readw(baseAddr + Magic_no) == Magic_code)
2744 break;
2745 moxadelay(1); /* delay 10 ms */
2747 if (readw(baseAddr + Magic_no) != Magic_code) {
2748 return (-1);
2750 moxaCard = 1;
2751 moxa_boards[cardno].intNdx = baseAddr + IRQindex;
2752 moxa_boards[cardno].intPend = baseAddr + IRQpending;
2753 moxa_boards[cardno].intTable = baseAddr + IRQtable;
2754 return (0);
2757 static int moxaloadc320(int cardno, void __iomem *baseAddr, int len, int *numPorts)
2759 ushort usum;
2760 int i, j, wlen, len2, retry;
2761 ushort *uptr;
2763 usum = 0;
2764 wlen = len >> 1;
2765 uptr = (ushort *) moxaBuff;
2766 for (i = 0; i < wlen; i++)
2767 usum += le16_to_cpu(uptr[i]);
2768 retry = 0;
2769 j = 0;
2770 do {
2771 while (wlen) {
2772 if (wlen > 2048)
2773 len2 = 2048;
2774 else
2775 len2 = wlen;
2776 wlen -= len2;
2777 len2 <<= 1;
2778 for (i = 0; i < len2; i++)
2779 writeb(moxaBuff[j + i], baseAddr + C320_LoadBuf + i);
2780 len2 >>= 1;
2781 j += i;
2782 writew(len2, baseAddr + C320DLoad_len);
2783 writew(0, baseAddr + C320_key);
2784 for (i = 0; i < 10; i++) {
2785 if (readw(baseAddr + C320_key) == C320_KeyCode)
2786 break;
2787 moxadelay(1);
2789 if (readw(baseAddr + C320_key) != C320_KeyCode)
2790 return (-1);
2792 writew(0, baseAddr + C320DLoad_len);
2793 writew(usum, baseAddr + C320check_sum);
2794 writew(0, baseAddr + C320_key);
2795 for (i = 0; i < 10; i++) {
2796 if (readw(baseAddr + C320_key) == C320_KeyCode)
2797 break;
2798 moxadelay(1);
2800 retry++;
2801 } while ((readb(baseAddr + C320chksum_ok) != 1) && (retry < 3));
2802 if (readb(baseAddr + C320chksum_ok) != 1)
2803 return (-1);
2804 writew(0, baseAddr + C320_key);
2805 for (i = 0; i < 600; i++) {
2806 if (readw(baseAddr + Magic_no) == Magic_code)
2807 break;
2808 moxadelay(1);
2810 if (readw(baseAddr + Magic_no) != Magic_code)
2811 return (-100);
2813 if (moxa_boards[cardno].busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
2814 writew(0x3800, baseAddr + TMS320_PORT1);
2815 writew(0x3900, baseAddr + TMS320_PORT2);
2816 writew(28499, baseAddr + TMS320_CLOCK);
2817 } else {
2818 writew(0x3200, baseAddr + TMS320_PORT1);
2819 writew(0x3400, baseAddr + TMS320_PORT2);
2820 writew(19999, baseAddr + TMS320_CLOCK);
2822 writew(1, baseAddr + Disable_IRQ);
2823 writew(0, baseAddr + Magic_no);
2824 for (i = 0; i < 500; i++) {
2825 if (readw(baseAddr + Magic_no) == Magic_code)
2826 break;
2827 moxadelay(1);
2829 if (readw(baseAddr + Magic_no) != Magic_code)
2830 return (-102);
2832 j = readw(baseAddr + Module_cnt);
2833 if (j <= 0)
2834 return (-101);
2835 *numPorts = j * 8;
2836 writew(j, baseAddr + Module_no);
2837 writew(0, baseAddr + Magic_no);
2838 for (i = 0; i < 600; i++) {
2839 if (readw(baseAddr + Magic_no) == Magic_code)
2840 break;
2841 moxadelay(1);
2843 if (readw(baseAddr + Magic_no) != Magic_code)
2844 return (-102);
2845 moxaCard = 1;
2846 moxa_boards[cardno].intNdx = baseAddr + IRQindex;
2847 moxa_boards[cardno].intPend = baseAddr + IRQpending;
2848 moxa_boards[cardno].intTable = baseAddr + IRQtable;
2849 return (0);
2852 static void MoxaSetFifo(int port, int enable)
2854 void __iomem *ofsAddr = moxa_ports[port].tableAddr;
2856 if (!enable) {
2857 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2858 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2859 } else {
2860 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2861 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);