[PARISC] Remove GCC_VERSION usage as suggested by Adrian Bunk
[linux-2.6.22.y-op.git] / drivers / char / moxa.c
blob8b316953173dd5c168b2c00985c513207ea5c3ba
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.
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
27 * for : LINUX
28 * date : 1999/1/7
29 * version : 5.1
32 #include <linux/module.h>
33 #include <linux/types.h>
34 #include <linux/mm.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>
55 #include <asm/io.h>
56 #include <asm/uaccess.h>
58 #define MOXA_VERSION "5.1k"
60 #define MOXAMAJOR 172
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
78 #endif
79 #ifndef PCI_DEVICE_ID_CP204J
80 #define PCI_DEVICE_ID_CP204J 0x2040
81 #endif
82 #ifndef PCI_DEVICE_ID_C218
83 #define PCI_DEVICE_ID_C218 0x2180
84 #endif
85 #ifndef PCI_DEVICE_ID_C320
86 #define PCI_DEVICE_ID_C320 0x3200
87 #endif
89 enum {
90 MOXA_BOARD_C218_PCI = 1,
91 MOXA_BOARD_C218_ISA,
92 MOXA_BOARD_C320_PCI,
93 MOXA_BOARD_C320_ISA,
94 MOXA_BOARD_CP204J,
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",
103 "CP-204J series",
106 #ifdef CONFIG_PCI
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 },
114 { 0 }
116 MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
117 #endif /* CONFIG_PCI */
119 typedef struct _moxa_isa_board_conf {
120 int boardType;
121 int numPorts;
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 {
131 ushort busNum;
132 ushort devNum;
133 struct pci_dev *pdev;
134 } moxa_pci_devinfo;
136 typedef struct _moxa_board_conf {
137 int boardType;
138 int numPorts;
139 unsigned long baseAddr;
140 int busType;
141 moxa_pci_devinfo pciInfo;
142 } moxa_board_conf;
144 static moxa_board_conf moxa_boards[MAX_BOARDS];
145 static void __iomem *moxaBaseAddr[MAX_BOARDS];
146 static int loadstat[MAX_BOARDS];
148 struct moxa_str {
149 int type;
150 int port;
151 int close_delay;
152 unsigned short closing_wait;
153 int count;
154 int blocked_open;
155 long event; /* long req'd for set_bit --RR */
156 int asyncflags;
157 unsigned long statusflags;
158 struct tty_struct *tty;
159 int cflag;
160 wait_queue_head_t open_wait;
161 wait_queue_head_t close_wait;
162 struct work_struct tqueue;
165 struct mxser_mstatus {
166 tcflag_t cflag;
167 int cts;
168 int dsr;
169 int ri;
170 int dcd;
173 static struct mxser_mstatus GMStatus[MAX_PORTS];
175 /* statusflags */
176 #define TXSTOPPED 0x1
177 #define LOWWAIT 0x2
178 #define EMPTYWAIT 0x4
179 #define THROTTLE 0x8
181 /* event */
182 #define MOXA_EVENT_HANGUP 1
184 #define SERIAL_DO_RESTART
187 #define SERIAL_TYPE_NORMAL 1
189 #define WAKEUP_CHARS 256
191 #define PORTNO(x) ((x)->index)
193 static int verbose = 0;
194 static int ttymajor = MOXAMAJOR;
195 /* Variables for insmod */
196 #ifdef MODULE
197 static int baseaddr[] = {0, 0, 0, 0};
198 static int type[] = {0, 0, 0, 0};
199 static int numports[] = {0, 0, 0, 0};
200 #endif
202 MODULE_AUTHOR("William Chen");
203 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
204 MODULE_LICENSE("GPL");
205 #ifdef MODULE
206 module_param_array(type, int, NULL, 0);
207 module_param_array(baseaddr, int, NULL, 0);
208 module_param_array(numports, int, NULL, 0);
209 #endif
210 module_param(ttymajor, int, 0);
211 module_param(verbose, bool, 0644);
213 static struct tty_driver *moxaDriver;
214 static struct moxa_str moxaChannels[MAX_PORTS];
215 static unsigned char *moxaXmitBuff;
216 static int moxaTimer_on;
217 static struct timer_list moxaTimer;
218 static int moxaEmptyTimer_on[MAX_PORTS];
219 static struct timer_list moxaEmptyTimer[MAX_PORTS];
220 static struct semaphore moxaBuffSem;
223 * static functions:
225 static void do_moxa_softint(struct work_struct *);
226 static int moxa_open(struct tty_struct *, struct file *);
227 static void moxa_close(struct tty_struct *, struct file *);
228 static int moxa_write(struct tty_struct *, const unsigned char *, int);
229 static int moxa_write_room(struct tty_struct *);
230 static void moxa_flush_buffer(struct tty_struct *);
231 static int moxa_chars_in_buffer(struct tty_struct *);
232 static void moxa_flush_chars(struct tty_struct *);
233 static void moxa_put_char(struct tty_struct *, unsigned char);
234 static int moxa_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);
235 static void moxa_throttle(struct tty_struct *);
236 static void moxa_unthrottle(struct tty_struct *);
237 static void moxa_set_termios(struct tty_struct *, struct termios *);
238 static void moxa_stop(struct tty_struct *);
239 static void moxa_start(struct tty_struct *);
240 static void moxa_hangup(struct tty_struct *);
241 static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
242 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
243 unsigned int set, unsigned int clear);
244 static void moxa_poll(unsigned long);
245 static void set_tty_param(struct tty_struct *);
246 static int block_till_ready(struct tty_struct *, struct file *,
247 struct moxa_str *);
248 static void setup_empty_event(struct tty_struct *);
249 static void check_xmit_empty(unsigned long);
250 static void shut_down(struct moxa_str *);
251 static void receive_data(struct moxa_str *);
253 * moxa board interface functions:
255 static void MoxaDriverInit(void);
256 static int MoxaDriverIoctl(unsigned int, unsigned long, int);
257 static int MoxaDriverPoll(void);
258 static int MoxaPortsOfCard(int);
259 static int MoxaPortIsValid(int);
260 static void MoxaPortEnable(int);
261 static void MoxaPortDisable(int);
262 static long MoxaPortGetMaxBaud(int);
263 static long MoxaPortSetBaud(int, long);
264 static int MoxaPortSetTermio(int, struct termios *, speed_t);
265 static int MoxaPortGetLineOut(int, int *, int *);
266 static void MoxaPortLineCtrl(int, int, int);
267 static void MoxaPortFlowCtrl(int, int, int, int, int, int);
268 static int MoxaPortLineStatus(int);
269 static int MoxaPortDCDChange(int);
270 static int MoxaPortDCDON(int);
271 static void MoxaPortFlushData(int, int);
272 static int MoxaPortWriteData(int, unsigned char *, int);
273 static int MoxaPortReadData(int, struct tty_struct *tty);
274 static int MoxaPortTxQueue(int);
275 static int MoxaPortRxQueue(int);
276 static int MoxaPortTxFree(int);
277 static void MoxaPortTxDisable(int);
278 static void MoxaPortTxEnable(int);
279 static int MoxaPortResetBrkCnt(int);
280 static void MoxaPortSendBreak(int, int);
281 static int moxa_get_serial_info(struct moxa_str *, struct serial_struct __user *);
282 static int moxa_set_serial_info(struct moxa_str *, struct serial_struct __user *);
283 static void MoxaSetFifo(int port, int enable);
285 static const struct tty_operations moxa_ops = {
286 .open = moxa_open,
287 .close = moxa_close,
288 .write = moxa_write,
289 .write_room = moxa_write_room,
290 .flush_buffer = moxa_flush_buffer,
291 .chars_in_buffer = moxa_chars_in_buffer,
292 .flush_chars = moxa_flush_chars,
293 .put_char = moxa_put_char,
294 .ioctl = moxa_ioctl,
295 .throttle = moxa_throttle,
296 .unthrottle = moxa_unthrottle,
297 .set_termios = moxa_set_termios,
298 .stop = moxa_stop,
299 .start = moxa_start,
300 .hangup = moxa_hangup,
301 .tiocmget = moxa_tiocmget,
302 .tiocmset = moxa_tiocmset,
305 static DEFINE_SPINLOCK(moxa_lock);
307 #ifdef CONFIG_PCI
308 static int moxa_get_PCI_conf(struct pci_dev *p, int board_type, moxa_board_conf * board)
310 board->baseAddr = pci_resource_start (p, 2);
311 board->boardType = board_type;
312 switch (board_type) {
313 case MOXA_BOARD_C218_ISA:
314 case MOXA_BOARD_C218_PCI:
315 board->numPorts = 8;
316 break;
318 case MOXA_BOARD_CP204J:
319 board->numPorts = 4;
320 break;
321 default:
322 board->numPorts = 0;
323 break;
325 board->busType = MOXA_BUS_TYPE_PCI;
326 board->pciInfo.busNum = p->bus->number;
327 board->pciInfo.devNum = p->devfn >> 3;
328 board->pciInfo.pdev = p;
329 /* don't lose the reference in the next pci_get_device iteration */
330 pci_dev_get(p);
332 return (0);
334 #endif /* CONFIG_PCI */
336 static int __init moxa_init(void)
338 int i, numBoards;
339 struct moxa_str *ch;
341 printk(KERN_INFO "MOXA Intellio family driver version %s\n", MOXA_VERSION);
342 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
343 if (!moxaDriver)
344 return -ENOMEM;
346 init_MUTEX(&moxaBuffSem);
347 moxaDriver->owner = THIS_MODULE;
348 moxaDriver->name = "ttyMX";
349 moxaDriver->major = ttymajor;
350 moxaDriver->minor_start = 0;
351 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
352 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
353 moxaDriver->init_termios = tty_std_termios;
354 moxaDriver->init_termios.c_iflag = 0;
355 moxaDriver->init_termios.c_oflag = 0;
356 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
357 moxaDriver->init_termios.c_lflag = 0;
358 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
359 tty_set_operations(moxaDriver, &moxa_ops);
361 moxaXmitBuff = NULL;
363 for (i = 0, ch = moxaChannels; i < MAX_PORTS; i++, ch++) {
364 ch->type = PORT_16550A;
365 ch->port = i;
366 INIT_WORK(&ch->tqueue, do_moxa_softint);
367 ch->tty = NULL;
368 ch->close_delay = 5 * HZ / 10;
369 ch->closing_wait = 30 * HZ;
370 ch->count = 0;
371 ch->blocked_open = 0;
372 ch->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
373 init_waitqueue_head(&ch->open_wait);
374 init_waitqueue_head(&ch->close_wait);
377 for (i = 0; i < MAX_BOARDS; i++) {
378 moxa_boards[i].boardType = 0;
379 moxa_boards[i].numPorts = 0;
380 moxa_boards[i].baseAddr = 0;
381 moxa_boards[i].busType = 0;
382 moxa_boards[i].pciInfo.busNum = 0;
383 moxa_boards[i].pciInfo.devNum = 0;
385 MoxaDriverInit();
386 printk("Tty devices major number = %d\n", ttymajor);
388 if (tty_register_driver(moxaDriver)) {
389 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
390 put_tty_driver(moxaDriver);
391 return -1;
393 for (i = 0; i < MAX_PORTS; i++) {
394 init_timer(&moxaEmptyTimer[i]);
395 moxaEmptyTimer[i].function = check_xmit_empty;
396 moxaEmptyTimer[i].data = (unsigned long) & moxaChannels[i];
397 moxaEmptyTimer_on[i] = 0;
400 init_timer(&moxaTimer);
401 moxaTimer.function = moxa_poll;
402 moxaTimer.expires = jiffies + (HZ / 50);
403 moxaTimer_on = 1;
404 add_timer(&moxaTimer);
406 /* Find the boards defined in source code */
407 numBoards = 0;
408 for (i = 0; i < MAX_BOARDS; i++) {
409 if ((moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA) ||
410 (moxa_isa_boards[i].boardType == MOXA_BOARD_C320_ISA)) {
411 moxa_boards[numBoards].boardType = moxa_isa_boards[i].boardType;
412 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
413 moxa_boards[numBoards].numPorts = 8;
414 else
415 moxa_boards[numBoards].numPorts = moxa_isa_boards[i].numPorts;
416 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
417 moxa_boards[numBoards].baseAddr = moxa_isa_boards[i].baseAddr;
418 if (verbose)
419 printk("Board %2d: %s board(baseAddr=%lx)\n",
420 numBoards + 1,
421 moxa_brdname[moxa_boards[numBoards].boardType - 1],
422 moxa_boards[numBoards].baseAddr);
423 numBoards++;
426 /* Find the boards defined form module args. */
427 #ifdef MODULE
428 for (i = 0; i < MAX_BOARDS; i++) {
429 if ((type[i] == MOXA_BOARD_C218_ISA) ||
430 (type[i] == MOXA_BOARD_C320_ISA)) {
431 if (verbose)
432 printk("Board %2d: %s board(baseAddr=%lx)\n",
433 numBoards + 1,
434 moxa_brdname[type[i] - 1],
435 (unsigned long) baseaddr[i]);
436 if (numBoards >= MAX_BOARDS) {
437 if (verbose)
438 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
439 continue;
441 moxa_boards[numBoards].boardType = type[i];
442 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
443 moxa_boards[numBoards].numPorts = 8;
444 else
445 moxa_boards[numBoards].numPorts = numports[i];
446 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
447 moxa_boards[numBoards].baseAddr = baseaddr[i];
448 numBoards++;
451 #endif
452 /* Find PCI boards here */
453 #ifdef CONFIG_PCI
455 struct pci_dev *p = NULL;
456 int n = ARRAY_SIZE(moxa_pcibrds) - 1;
457 i = 0;
458 while (i < n) {
459 while ((p = pci_get_device(moxa_pcibrds[i].vendor, moxa_pcibrds[i].device, p))!=NULL)
461 if (pci_enable_device(p))
462 continue;
463 if (numBoards >= MAX_BOARDS) {
464 if (verbose)
465 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
466 } else {
467 moxa_get_PCI_conf(p, moxa_pcibrds[i].driver_data,
468 &moxa_boards[numBoards]);
469 numBoards++;
472 i++;
475 #endif
476 for (i = 0; i < numBoards; i++) {
477 moxaBaseAddr[i] = ioremap((unsigned long) moxa_boards[i].baseAddr, 0x4000);
480 return (0);
483 static void __exit moxa_exit(void)
485 int i;
487 if (verbose)
488 printk("Unloading module moxa ...\n");
490 if (moxaTimer_on)
491 del_timer(&moxaTimer);
493 for (i = 0; i < MAX_PORTS; i++)
494 if (moxaEmptyTimer_on[i])
495 del_timer(&moxaEmptyTimer[i]);
497 if (tty_unregister_driver(moxaDriver))
498 printk("Couldn't unregister MOXA Intellio family serial driver\n");
499 put_tty_driver(moxaDriver);
501 for (i = 0; i < MAX_BOARDS; i++) {
502 if (moxaBaseAddr[i])
503 iounmap(moxaBaseAddr[i]);
504 if (moxa_boards[i].busType == MOXA_BUS_TYPE_PCI)
505 pci_dev_put(moxa_boards[i].pciInfo.pdev);
508 if (verbose)
509 printk("Done\n");
512 module_init(moxa_init);
513 module_exit(moxa_exit);
515 static void do_moxa_softint(struct work_struct *work)
517 struct moxa_str *ch = container_of(work, struct moxa_str, tqueue);
518 struct tty_struct *tty;
520 if (ch && (tty = ch->tty)) {
521 if (test_and_clear_bit(MOXA_EVENT_HANGUP, &ch->event)) {
522 tty_hangup(tty); /* FIXME: module removal race here - AKPM */
523 wake_up_interruptible(&ch->open_wait);
524 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
529 static int moxa_open(struct tty_struct *tty, struct file *filp)
531 struct moxa_str *ch;
532 int port;
533 int retval;
534 unsigned long page;
536 port = PORTNO(tty);
537 if (port == MAX_PORTS) {
538 return (0);
540 if (!MoxaPortIsValid(port)) {
541 tty->driver_data = NULL;
542 return (-ENODEV);
544 down(&moxaBuffSem);
545 if (!moxaXmitBuff) {
546 page = get_zeroed_page(GFP_KERNEL);
547 if (!page) {
548 up(&moxaBuffSem);
549 return (-ENOMEM);
551 /* This test is guarded by the BuffSem so no longer needed
552 delete me in 2.5 */
553 if (moxaXmitBuff)
554 free_page(page);
555 else
556 moxaXmitBuff = (unsigned char *) page;
558 up(&moxaBuffSem);
560 ch = &moxaChannels[port];
561 ch->count++;
562 tty->driver_data = ch;
563 ch->tty = tty;
564 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
565 ch->statusflags = 0;
566 set_tty_param(tty);
567 MoxaPortLineCtrl(ch->port, 1, 1);
568 MoxaPortEnable(ch->port);
569 ch->asyncflags |= ASYNC_INITIALIZED;
571 retval = block_till_ready(tty, filp, ch);
573 moxa_unthrottle(tty);
575 if (ch->type == PORT_16550A) {
576 MoxaSetFifo(ch->port, 1);
577 } else {
578 MoxaSetFifo(ch->port, 0);
581 return (retval);
584 static void moxa_close(struct tty_struct *tty, struct file *filp)
586 struct moxa_str *ch;
587 int port;
589 port = PORTNO(tty);
590 if (port == MAX_PORTS) {
591 return;
593 if (!MoxaPortIsValid(port)) {
594 #ifdef SERIAL_DEBUG_CLOSE
595 printk("Invalid portno in moxa_close\n");
596 #endif
597 tty->driver_data = NULL;
598 return;
600 if (tty->driver_data == NULL) {
601 return;
603 if (tty_hung_up_p(filp)) {
604 return;
606 ch = (struct moxa_str *) tty->driver_data;
608 if ((tty->count == 1) && (ch->count != 1)) {
609 printk("moxa_close: bad serial port count; tty->count is 1, "
610 "ch->count is %d\n", ch->count);
611 ch->count = 1;
613 if (--ch->count < 0) {
614 printk("moxa_close: bad serial port count, device=%s\n",
615 tty->name);
616 ch->count = 0;
618 if (ch->count) {
619 return;
621 ch->asyncflags |= ASYNC_CLOSING;
623 ch->cflag = tty->termios->c_cflag;
624 if (ch->asyncflags & ASYNC_INITIALIZED) {
625 setup_empty_event(tty);
626 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
627 moxaEmptyTimer_on[ch->port] = 0;
628 del_timer(&moxaEmptyTimer[ch->port]);
630 shut_down(ch);
631 MoxaPortFlushData(port, 2);
633 if (tty->driver->flush_buffer)
634 tty->driver->flush_buffer(tty);
635 tty_ldisc_flush(tty);
637 tty->closing = 0;
638 ch->event = 0;
639 ch->tty = NULL;
640 if (ch->blocked_open) {
641 if (ch->close_delay) {
642 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
644 wake_up_interruptible(&ch->open_wait);
646 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
647 wake_up_interruptible(&ch->close_wait);
650 static int moxa_write(struct tty_struct *tty,
651 const unsigned char *buf, int count)
653 struct moxa_str *ch;
654 int len, port;
655 unsigned long flags;
657 ch = (struct moxa_str *) tty->driver_data;
658 if (ch == NULL)
659 return (0);
660 port = ch->port;
662 spin_lock_irqsave(&moxa_lock, flags);
663 len = MoxaPortWriteData(port, (unsigned char *) buf, count);
664 spin_unlock_irqrestore(&moxa_lock, flags);
666 /*********************************************
667 if ( !(ch->statusflags & LOWWAIT) &&
668 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
669 ************************************************/
670 ch->statusflags |= LOWWAIT;
671 return (len);
674 static int moxa_write_room(struct tty_struct *tty)
676 struct moxa_str *ch;
678 if (tty->stopped)
679 return (0);
680 ch = (struct moxa_str *) tty->driver_data;
681 if (ch == NULL)
682 return (0);
683 return (MoxaPortTxFree(ch->port));
686 static void moxa_flush_buffer(struct tty_struct *tty)
688 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
690 if (ch == NULL)
691 return;
692 MoxaPortFlushData(ch->port, 1);
693 tty_wakeup(tty);
696 static int moxa_chars_in_buffer(struct tty_struct *tty)
698 int chars;
699 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
702 * Sigh...I have to check if driver_data is NULL here, because
703 * if an open() fails, the TTY subsystem eventually calls
704 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
705 * routine. And since the open() failed, we return 0 here. TDJ
707 if (ch == NULL)
708 return (0);
709 chars = MoxaPortTxQueue(ch->port);
710 if (chars) {
712 * Make it possible to wakeup anything waiting for output
713 * in tty_ioctl.c, etc.
715 if (!(ch->statusflags & EMPTYWAIT))
716 setup_empty_event(tty);
718 return (chars);
721 static void moxa_flush_chars(struct tty_struct *tty)
724 * Don't think I need this, because this is called to empty the TX
725 * buffer for the 16450, 16550, etc.
729 static void moxa_put_char(struct tty_struct *tty, unsigned char c)
731 struct moxa_str *ch;
732 int port;
733 unsigned long flags;
735 ch = (struct moxa_str *) tty->driver_data;
736 if (ch == NULL)
737 return;
738 port = ch->port;
739 spin_lock_irqsave(&moxa_lock, flags);
740 moxaXmitBuff[0] = c;
741 MoxaPortWriteData(port, moxaXmitBuff, 1);
742 spin_unlock_irqrestore(&moxa_lock, flags);
743 /************************************************
744 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
745 *************************************************/
746 ch->statusflags |= LOWWAIT;
749 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
751 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
752 int port;
753 int flag = 0, dtr, rts;
755 port = PORTNO(tty);
756 if ((port != MAX_PORTS) && (!ch))
757 return (-EINVAL);
759 MoxaPortGetLineOut(ch->port, &dtr, &rts);
760 if (dtr)
761 flag |= TIOCM_DTR;
762 if (rts)
763 flag |= TIOCM_RTS;
764 dtr = MoxaPortLineStatus(ch->port);
765 if (dtr & 1)
766 flag |= TIOCM_CTS;
767 if (dtr & 2)
768 flag |= TIOCM_DSR;
769 if (dtr & 4)
770 flag |= TIOCM_CD;
771 return flag;
774 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
775 unsigned int set, unsigned int clear)
777 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
778 int port;
779 int dtr, rts;
781 port = PORTNO(tty);
782 if ((port != MAX_PORTS) && (!ch))
783 return (-EINVAL);
785 MoxaPortGetLineOut(ch->port, &dtr, &rts);
786 if (set & TIOCM_RTS)
787 rts = 1;
788 if (set & TIOCM_DTR)
789 dtr = 1;
790 if (clear & TIOCM_RTS)
791 rts = 0;
792 if (clear & TIOCM_DTR)
793 dtr = 0;
794 MoxaPortLineCtrl(ch->port, dtr, rts);
795 return 0;
798 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
799 unsigned int cmd, unsigned long arg)
801 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
802 register int port;
803 void __user *argp = (void __user *)arg;
804 int retval;
806 port = PORTNO(tty);
807 if ((port != MAX_PORTS) && (!ch))
808 return (-EINVAL);
810 switch (cmd) {
811 case TCSBRK: /* SVID version: non-zero arg --> no break */
812 retval = tty_check_change(tty);
813 if (retval)
814 return (retval);
815 setup_empty_event(tty);
816 tty_wait_until_sent(tty, 0);
817 if (!arg)
818 MoxaPortSendBreak(ch->port, 0);
819 return (0);
820 case TCSBRKP: /* support for POSIX tcsendbreak() */
821 retval = tty_check_change(tty);
822 if (retval)
823 return (retval);
824 setup_empty_event(tty);
825 tty_wait_until_sent(tty, 0);
826 MoxaPortSendBreak(ch->port, arg);
827 return (0);
828 case TIOCGSOFTCAR:
829 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) argp);
830 case TIOCSSOFTCAR:
831 if(get_user(retval, (unsigned long __user *) argp))
832 return -EFAULT;
833 arg = retval;
834 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
835 (arg ? CLOCAL : 0));
836 if (C_CLOCAL(tty))
837 ch->asyncflags &= ~ASYNC_CHECK_CD;
838 else
839 ch->asyncflags |= ASYNC_CHECK_CD;
840 return (0);
841 case TIOCGSERIAL:
842 return moxa_get_serial_info(ch, argp);
844 case TIOCSSERIAL:
845 return moxa_set_serial_info(ch, argp);
846 default:
847 retval = MoxaDriverIoctl(cmd, arg, port);
849 return (retval);
852 static void moxa_throttle(struct tty_struct *tty)
854 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
856 ch->statusflags |= THROTTLE;
859 static void moxa_unthrottle(struct tty_struct *tty)
861 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
863 ch->statusflags &= ~THROTTLE;
866 static void moxa_set_termios(struct tty_struct *tty,
867 struct termios *old_termios)
869 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
871 if (ch == NULL)
872 return;
873 set_tty_param(tty);
874 if (!(old_termios->c_cflag & CLOCAL) &&
875 (tty->termios->c_cflag & CLOCAL))
876 wake_up_interruptible(&ch->open_wait);
879 static void moxa_stop(struct tty_struct *tty)
881 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
883 if (ch == NULL)
884 return;
885 MoxaPortTxDisable(ch->port);
886 ch->statusflags |= TXSTOPPED;
890 static void moxa_start(struct tty_struct *tty)
892 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
894 if (ch == NULL)
895 return;
897 if (!(ch->statusflags & TXSTOPPED))
898 return;
900 MoxaPortTxEnable(ch->port);
901 ch->statusflags &= ~TXSTOPPED;
904 static void moxa_hangup(struct tty_struct *tty)
906 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
908 moxa_flush_buffer(tty);
909 shut_down(ch);
910 ch->event = 0;
911 ch->count = 0;
912 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
913 ch->tty = NULL;
914 wake_up_interruptible(&ch->open_wait);
917 static void moxa_poll(unsigned long ignored)
919 register int card;
920 struct moxa_str *ch;
921 struct tty_struct *tp;
922 int i, ports;
924 moxaTimer_on = 0;
925 del_timer(&moxaTimer);
927 if (MoxaDriverPoll() < 0) {
928 moxaTimer.function = moxa_poll;
929 moxaTimer.expires = jiffies + (HZ / 50);
930 moxaTimer_on = 1;
931 add_timer(&moxaTimer);
932 return;
934 for (card = 0; card < MAX_BOARDS; card++) {
935 if ((ports = MoxaPortsOfCard(card)) <= 0)
936 continue;
937 ch = &moxaChannels[card * MAX_PORTS_PER_BOARD];
938 for (i = 0; i < ports; i++, ch++) {
939 if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
940 continue;
941 if (!(ch->statusflags & THROTTLE) &&
942 (MoxaPortRxQueue(ch->port) > 0))
943 receive_data(ch);
944 if ((tp = ch->tty) == 0)
945 continue;
946 if (ch->statusflags & LOWWAIT) {
947 if (MoxaPortTxQueue(ch->port) <= WAKEUP_CHARS) {
948 if (!tp->stopped) {
949 ch->statusflags &= ~LOWWAIT;
950 tty_wakeup(tp);
954 if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch->port) > 0)) {
955 tty_insert_flip_char(tp, 0, TTY_BREAK);
956 tty_schedule_flip(tp);
958 if (MoxaPortDCDChange(ch->port)) {
959 if (ch->asyncflags & ASYNC_CHECK_CD) {
960 if (MoxaPortDCDON(ch->port))
961 wake_up_interruptible(&ch->open_wait);
962 else {
963 set_bit(MOXA_EVENT_HANGUP, &ch->event);
964 schedule_work(&ch->tqueue);
971 moxaTimer.function = moxa_poll;
972 moxaTimer.expires = jiffies + (HZ / 50);
973 moxaTimer_on = 1;
974 add_timer(&moxaTimer);
977 /******************************************************************************/
979 static void set_tty_param(struct tty_struct *tty)
981 register struct termios *ts;
982 struct moxa_str *ch;
983 int rts, cts, txflow, rxflow, xany;
985 ch = (struct moxa_str *) tty->driver_data;
986 ts = tty->termios;
987 if (ts->c_cflag & CLOCAL)
988 ch->asyncflags &= ~ASYNC_CHECK_CD;
989 else
990 ch->asyncflags |= ASYNC_CHECK_CD;
991 rts = cts = txflow = rxflow = xany = 0;
992 if (ts->c_cflag & CRTSCTS)
993 rts = cts = 1;
994 if (ts->c_iflag & IXON)
995 txflow = 1;
996 if (ts->c_iflag & IXOFF)
997 rxflow = 1;
998 if (ts->c_iflag & IXANY)
999 xany = 1;
1000 MoxaPortFlowCtrl(ch->port, rts, cts, txflow, rxflow, xany);
1001 MoxaPortSetTermio(ch->port, ts, tty_get_baud_rate(tty));
1004 static int block_till_ready(struct tty_struct *tty, struct file *filp,
1005 struct moxa_str *ch)
1007 DECLARE_WAITQUEUE(wait,current);
1008 unsigned long flags;
1009 int retval;
1010 int do_clocal = C_CLOCAL(tty);
1013 * If the device is in the middle of being closed, then block
1014 * until it's done, and then try again.
1016 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
1017 if (ch->asyncflags & ASYNC_CLOSING)
1018 interruptible_sleep_on(&ch->close_wait);
1019 #ifdef SERIAL_DO_RESTART
1020 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1021 return (-EAGAIN);
1022 else
1023 return (-ERESTARTSYS);
1024 #else
1025 return (-EAGAIN);
1026 #endif
1029 * If non-blocking mode is set, then make the check up front
1030 * and then exit.
1032 if (filp->f_flags & O_NONBLOCK) {
1033 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1034 return (0);
1037 * Block waiting for the carrier detect and the line to become free
1039 retval = 0;
1040 add_wait_queue(&ch->open_wait, &wait);
1041 #ifdef SERIAL_DEBUG_OPEN
1042 printk("block_til_ready before block: ttys%d, count = %d\n",
1043 ch->line, ch->count);
1044 #endif
1045 spin_lock_irqsave(&moxa_lock, flags);
1046 if (!tty_hung_up_p(filp))
1047 ch->count--;
1048 ch->blocked_open++;
1049 spin_unlock_irqrestore(&moxa_lock, flags);
1051 while (1) {
1052 set_current_state(TASK_INTERRUPTIBLE);
1053 if (tty_hung_up_p(filp) ||
1054 !(ch->asyncflags & ASYNC_INITIALIZED)) {
1055 #ifdef SERIAL_DO_RESTART
1056 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1057 retval = -EAGAIN;
1058 else
1059 retval = -ERESTARTSYS;
1060 #else
1061 retval = -EAGAIN;
1062 #endif
1063 break;
1065 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
1066 MoxaPortDCDON(ch->port)))
1067 break;
1069 if (signal_pending(current)) {
1070 retval = -ERESTARTSYS;
1071 break;
1073 schedule();
1075 set_current_state(TASK_RUNNING);
1076 remove_wait_queue(&ch->open_wait, &wait);
1078 spin_lock_irqsave(&moxa_lock, flags);
1079 if (!tty_hung_up_p(filp))
1080 ch->count++;
1081 ch->blocked_open--;
1082 spin_unlock_irqrestore(&moxa_lock, flags);
1083 #ifdef SERIAL_DEBUG_OPEN
1084 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1085 ch->line, ch->count);
1086 #endif
1087 if (retval)
1088 return (retval);
1089 /* FIXME: review to see if we need to use set_bit on these */
1090 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1091 return 0;
1094 static void setup_empty_event(struct tty_struct *tty)
1096 struct moxa_str *ch = tty->driver_data;
1097 unsigned long flags;
1099 spin_lock_irqsave(&moxa_lock, flags);
1100 ch->statusflags |= EMPTYWAIT;
1101 moxaEmptyTimer_on[ch->port] = 0;
1102 del_timer(&moxaEmptyTimer[ch->port]);
1103 moxaEmptyTimer[ch->port].expires = jiffies + HZ;
1104 moxaEmptyTimer_on[ch->port] = 1;
1105 add_timer(&moxaEmptyTimer[ch->port]);
1106 spin_unlock_irqrestore(&moxa_lock, flags);
1109 static void check_xmit_empty(unsigned long data)
1111 struct moxa_str *ch;
1113 ch = (struct moxa_str *) data;
1114 moxaEmptyTimer_on[ch->port] = 0;
1115 del_timer(&moxaEmptyTimer[ch->port]);
1116 if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
1117 if (MoxaPortTxQueue(ch->port) == 0) {
1118 ch->statusflags &= ~EMPTYWAIT;
1119 tty_wakeup(ch->tty);
1120 return;
1122 moxaEmptyTimer[ch->port].expires = jiffies + HZ;
1123 moxaEmptyTimer_on[ch->port] = 1;
1124 add_timer(&moxaEmptyTimer[ch->port]);
1125 } else
1126 ch->statusflags &= ~EMPTYWAIT;
1129 static void shut_down(struct moxa_str *ch)
1131 struct tty_struct *tp;
1133 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1134 return;
1136 tp = ch->tty;
1138 MoxaPortDisable(ch->port);
1141 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1143 if (tp->termios->c_cflag & HUPCL)
1144 MoxaPortLineCtrl(ch->port, 0, 0);
1146 ch->asyncflags &= ~ASYNC_INITIALIZED;
1149 static void receive_data(struct moxa_str *ch)
1151 struct tty_struct *tp;
1152 struct termios *ts;
1153 unsigned long flags;
1155 ts = NULL;
1156 tp = ch->tty;
1157 if (tp)
1158 ts = tp->termios;
1159 /**************************************************
1160 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1161 *****************************************************/
1162 if (!tp || !ts) {
1163 MoxaPortFlushData(ch->port, 0);
1164 return;
1166 spin_lock_irqsave(&moxa_lock, flags);
1167 MoxaPortReadData(ch->port, tp);
1168 spin_unlock_irqrestore(&moxa_lock, flags);
1169 tty_schedule_flip(tp);
1172 #define Magic_code 0x404
1175 * System Configuration
1178 * for C218 BIOS initialization
1180 #define C218_ConfBase 0x800
1181 #define C218_status (C218_ConfBase + 0) /* BIOS running status */
1182 #define C218_diag (C218_ConfBase + 2) /* diagnostic status */
1183 #define C218_key (C218_ConfBase + 4) /* WORD (0x218 for C218) */
1184 #define C218DLoad_len (C218_ConfBase + 6) /* WORD */
1185 #define C218check_sum (C218_ConfBase + 8) /* BYTE */
1186 #define C218chksum_ok (C218_ConfBase + 0x0a) /* BYTE (1:ok) */
1187 #define C218_TestRx (C218_ConfBase + 0x10) /* 8 bytes for 8 ports */
1188 #define C218_TestTx (C218_ConfBase + 0x18) /* 8 bytes for 8 ports */
1189 #define C218_RXerr (C218_ConfBase + 0x20) /* 8 bytes for 8 ports */
1190 #define C218_ErrFlag (C218_ConfBase + 0x28) /* 8 bytes for 8 ports */
1192 #define C218_LoadBuf 0x0F00
1193 #define C218_KeyCode 0x218
1194 #define CP204J_KeyCode 0x204
1197 * for C320 BIOS initialization
1199 #define C320_ConfBase 0x800
1200 #define C320_LoadBuf 0x0f00
1201 #define STS_init 0x05 /* for C320_status */
1203 #define C320_status C320_ConfBase + 0 /* BIOS running status */
1204 #define C320_diag C320_ConfBase + 2 /* diagnostic status */
1205 #define C320_key C320_ConfBase + 4 /* WORD (0320H for C320) */
1206 #define C320DLoad_len C320_ConfBase + 6 /* WORD */
1207 #define C320check_sum C320_ConfBase + 8 /* WORD */
1208 #define C320chksum_ok C320_ConfBase + 0x0a /* WORD (1:ok) */
1209 #define C320bapi_len C320_ConfBase + 0x0c /* WORD */
1210 #define C320UART_no C320_ConfBase + 0x0e /* WORD */
1212 #define C320_KeyCode 0x320
1214 #define FixPage_addr 0x0000 /* starting addr of static page */
1215 #define DynPage_addr 0x2000 /* starting addr of dynamic page */
1216 #define C218_start 0x3000 /* starting addr of C218 BIOS prg */
1217 #define Control_reg 0x1ff0 /* select page and reset control */
1218 #define HW_reset 0x80
1221 * Function Codes
1223 #define FC_CardReset 0x80
1224 #define FC_ChannelReset 1 /* C320 firmware not supported */
1225 #define FC_EnableCH 2
1226 #define FC_DisableCH 3
1227 #define FC_SetParam 4
1228 #define FC_SetMode 5
1229 #define FC_SetRate 6
1230 #define FC_LineControl 7
1231 #define FC_LineStatus 8
1232 #define FC_XmitControl 9
1233 #define FC_FlushQueue 10
1234 #define FC_SendBreak 11
1235 #define FC_StopBreak 12
1236 #define FC_LoopbackON 13
1237 #define FC_LoopbackOFF 14
1238 #define FC_ClrIrqTable 15
1239 #define FC_SendXon 16
1240 #define FC_SetTermIrq 17 /* C320 firmware not supported */
1241 #define FC_SetCntIrq 18 /* C320 firmware not supported */
1242 #define FC_SetBreakIrq 19
1243 #define FC_SetLineIrq 20
1244 #define FC_SetFlowCtl 21
1245 #define FC_GenIrq 22
1246 #define FC_InCD180 23
1247 #define FC_OutCD180 24
1248 #define FC_InUARTreg 23
1249 #define FC_OutUARTreg 24
1250 #define FC_SetXonXoff 25
1251 #define FC_OutCD180CCR 26
1252 #define FC_ExtIQueue 27
1253 #define FC_ExtOQueue 28
1254 #define FC_ClrLineIrq 29
1255 #define FC_HWFlowCtl 30
1256 #define FC_GetClockRate 35
1257 #define FC_SetBaud 36
1258 #define FC_SetDataMode 41
1259 #define FC_GetCCSR 43
1260 #define FC_GetDataError 45
1261 #define FC_RxControl 50
1262 #define FC_ImmSend 51
1263 #define FC_SetXonState 52
1264 #define FC_SetXoffState 53
1265 #define FC_SetRxFIFOTrig 54
1266 #define FC_SetTxFIFOCnt 55
1267 #define FC_UnixRate 56
1268 #define FC_UnixResetTimer 57
1270 #define RxFIFOTrig1 0
1271 #define RxFIFOTrig4 1
1272 #define RxFIFOTrig8 2
1273 #define RxFIFOTrig14 3
1276 * Dual-Ported RAM
1278 #define DRAM_global 0
1279 #define INT_data (DRAM_global + 0)
1280 #define Config_base (DRAM_global + 0x108)
1282 #define IRQindex (INT_data + 0)
1283 #define IRQpending (INT_data + 4)
1284 #define IRQtable (INT_data + 8)
1287 * Interrupt Status
1289 #define IntrRx 0x01 /* receiver data O.K. */
1290 #define IntrTx 0x02 /* transmit buffer empty */
1291 #define IntrFunc 0x04 /* function complete */
1292 #define IntrBreak 0x08 /* received break */
1293 #define IntrLine 0x10 /* line status change
1294 for transmitter */
1295 #define IntrIntr 0x20 /* received INTR code */
1296 #define IntrQuit 0x40 /* received QUIT code */
1297 #define IntrEOF 0x80 /* received EOF code */
1299 #define IntrRxTrigger 0x100 /* rx data count reach tigger value */
1300 #define IntrTxTrigger 0x200 /* tx data count below trigger value */
1302 #define Magic_no (Config_base + 0)
1303 #define Card_model_no (Config_base + 2)
1304 #define Total_ports (Config_base + 4)
1305 #define Module_cnt (Config_base + 8)
1306 #define Module_no (Config_base + 10)
1307 #define Timer_10ms (Config_base + 14)
1308 #define Disable_IRQ (Config_base + 20)
1309 #define TMS320_PORT1 (Config_base + 22)
1310 #define TMS320_PORT2 (Config_base + 24)
1311 #define TMS320_CLOCK (Config_base + 26)
1314 * DATA BUFFER in DRAM
1316 #define Extern_table 0x400 /* Base address of the external table
1317 (24 words * 64) total 3K bytes
1318 (24 words * 128) total 6K bytes */
1319 #define Extern_size 0x60 /* 96 bytes */
1320 #define RXrptr 0x00 /* read pointer for RX buffer */
1321 #define RXwptr 0x02 /* write pointer for RX buffer */
1322 #define TXrptr 0x04 /* read pointer for TX buffer */
1323 #define TXwptr 0x06 /* write pointer for TX buffer */
1324 #define HostStat 0x08 /* IRQ flag and general flag */
1325 #define FlagStat 0x0A
1326 #define FlowControl 0x0C /* B7 B6 B5 B4 B3 B2 B1 B0 */
1327 /* x x x x | | | | */
1328 /* | | | + CTS flow */
1329 /* | | +--- RTS flow */
1330 /* | +------ TX Xon/Xoff */
1331 /* +--------- RX Xon/Xoff */
1332 #define Break_cnt 0x0E /* received break count */
1333 #define CD180TXirq 0x10 /* if non-0: enable TX irq */
1334 #define RX_mask 0x12
1335 #define TX_mask 0x14
1336 #define Ofs_rxb 0x16
1337 #define Ofs_txb 0x18
1338 #define Page_rxb 0x1A
1339 #define Page_txb 0x1C
1340 #define EndPage_rxb 0x1E
1341 #define EndPage_txb 0x20
1342 #define Data_error 0x22
1343 #define RxTrigger 0x28
1344 #define TxTrigger 0x2a
1346 #define rRXwptr 0x34
1347 #define Low_water 0x36
1349 #define FuncCode 0x40
1350 #define FuncArg 0x42
1351 #define FuncArg1 0x44
1353 #define C218rx_size 0x2000 /* 8K bytes */
1354 #define C218tx_size 0x8000 /* 32K bytes */
1356 #define C218rx_mask (C218rx_size - 1)
1357 #define C218tx_mask (C218tx_size - 1)
1359 #define C320p8rx_size 0x2000
1360 #define C320p8tx_size 0x8000
1361 #define C320p8rx_mask (C320p8rx_size - 1)
1362 #define C320p8tx_mask (C320p8tx_size - 1)
1364 #define C320p16rx_size 0x2000
1365 #define C320p16tx_size 0x4000
1366 #define C320p16rx_mask (C320p16rx_size - 1)
1367 #define C320p16tx_mask (C320p16tx_size - 1)
1369 #define C320p24rx_size 0x2000
1370 #define C320p24tx_size 0x2000
1371 #define C320p24rx_mask (C320p24rx_size - 1)
1372 #define C320p24tx_mask (C320p24tx_size - 1)
1374 #define C320p32rx_size 0x1000
1375 #define C320p32tx_size 0x1000
1376 #define C320p32rx_mask (C320p32rx_size - 1)
1377 #define C320p32tx_mask (C320p32tx_size - 1)
1379 #define Page_size 0x2000
1380 #define Page_mask (Page_size - 1)
1381 #define C218rx_spage 3
1382 #define C218tx_spage 4
1383 #define C218rx_pageno 1
1384 #define C218tx_pageno 4
1385 #define C218buf_pageno 5
1387 #define C320p8rx_spage 3
1388 #define C320p8tx_spage 4
1389 #define C320p8rx_pgno 1
1390 #define C320p8tx_pgno 4
1391 #define C320p8buf_pgno 5
1393 #define C320p16rx_spage 3
1394 #define C320p16tx_spage 4
1395 #define C320p16rx_pgno 1
1396 #define C320p16tx_pgno 2
1397 #define C320p16buf_pgno 3
1399 #define C320p24rx_spage 3
1400 #define C320p24tx_spage 4
1401 #define C320p24rx_pgno 1
1402 #define C320p24tx_pgno 1
1403 #define C320p24buf_pgno 2
1405 #define C320p32rx_spage 3
1406 #define C320p32tx_ofs C320p32rx_size
1407 #define C320p32tx_spage 3
1408 #define C320p32buf_pgno 1
1411 * Host Status
1413 #define WakeupRx 0x01
1414 #define WakeupTx 0x02
1415 #define WakeupBreak 0x08
1416 #define WakeupLine 0x10
1417 #define WakeupIntr 0x20
1418 #define WakeupQuit 0x40
1419 #define WakeupEOF 0x80 /* used in VTIME control */
1420 #define WakeupRxTrigger 0x100
1421 #define WakeupTxTrigger 0x200
1423 * Flag status
1425 #define Rx_over 0x01
1426 #define Xoff_state 0x02
1427 #define Tx_flowOff 0x04
1428 #define Tx_enable 0x08
1429 #define CTS_state 0x10
1430 #define DSR_state 0x20
1431 #define DCD_state 0x80
1433 * FlowControl
1435 #define CTS_FlowCtl 1
1436 #define RTS_FlowCtl 2
1437 #define Tx_FlowCtl 4
1438 #define Rx_FlowCtl 8
1439 #define IXM_IXANY 0x10
1441 #define LowWater 128
1443 #define DTR_ON 1
1444 #define RTS_ON 2
1445 #define CTS_ON 1
1446 #define DSR_ON 2
1447 #define DCD_ON 8
1449 /* mode definition */
1450 #define MX_CS8 0x03
1451 #define MX_CS7 0x02
1452 #define MX_CS6 0x01
1453 #define MX_CS5 0x00
1455 #define MX_STOP1 0x00
1456 #define MX_STOP15 0x04
1457 #define MX_STOP2 0x08
1459 #define MX_PARNONE 0x00
1460 #define MX_PAREVEN 0x40
1461 #define MX_PARODD 0xC0
1464 * Query
1466 #define QueryPort MAX_PORTS
1470 struct mon_str {
1471 int tick;
1472 int rxcnt[MAX_PORTS];
1473 int txcnt[MAX_PORTS];
1475 typedef struct mon_str mon_st;
1477 #define DCD_changed 0x01
1478 #define DCD_oldstate 0x80
1480 static unsigned char moxaBuff[10240];
1481 static void __iomem *moxaIntNdx[MAX_BOARDS];
1482 static void __iomem *moxaIntPend[MAX_BOARDS];
1483 static void __iomem *moxaIntTable[MAX_BOARDS];
1484 static char moxaChkPort[MAX_PORTS];
1485 static char moxaLineCtrl[MAX_PORTS];
1486 static void __iomem *moxaTableAddr[MAX_PORTS];
1487 static long moxaCurBaud[MAX_PORTS];
1488 static char moxaDCDState[MAX_PORTS];
1489 static char moxaLowChkFlag[MAX_PORTS];
1490 static int moxaLowWaterChk;
1491 static int moxaCard;
1492 static mon_st moxaLog;
1493 static int moxaFuncTout;
1494 static ushort moxaBreakCnt[MAX_PORTS];
1496 static void moxadelay(int);
1497 static void moxafunc(void __iomem *, int, ushort);
1498 static void wait_finish(void __iomem *);
1499 static void low_water_check(void __iomem *);
1500 static int moxaloadbios(int, unsigned char __user *, int);
1501 static int moxafindcard(int);
1502 static int moxaload320b(int, unsigned char __user *, int);
1503 static int moxaloadcode(int, unsigned char __user *, int);
1504 static int moxaloadc218(int, void __iomem *, int);
1505 static int moxaloadc320(int, void __iomem *, int, int *);
1507 /*****************************************************************************
1508 * Driver level functions: *
1509 * 1. MoxaDriverInit(void); *
1510 * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
1511 * 3. MoxaDriverPoll(void); *
1512 *****************************************************************************/
1513 void MoxaDriverInit(void)
1515 int i;
1517 moxaFuncTout = HZ / 2; /* 500 mini-seconds */
1518 moxaCard = 0;
1519 moxaLog.tick = 0;
1520 moxaLowWaterChk = 0;
1521 for (i = 0; i < MAX_PORTS; i++) {
1522 moxaChkPort[i] = 0;
1523 moxaLowChkFlag[i] = 0;
1524 moxaLineCtrl[i] = 0;
1525 moxaLog.rxcnt[i] = 0;
1526 moxaLog.txcnt[i] = 0;
1530 #define MOXA 0x400
1531 #define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1532 #define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
1533 #define MOXA_INIT_DRIVER (MOXA + 6) /* moxaCard=0 */
1534 #define MOXA_LOAD_BIOS (MOXA + 9) /* download BIOS */
1535 #define MOXA_FIND_BOARD (MOXA + 10) /* Check if MOXA card exist? */
1536 #define MOXA_LOAD_C320B (MOXA + 11) /* download 320B firmware */
1537 #define MOXA_LOAD_CODE (MOXA + 12) /* download firmware */
1538 #define MOXA_GETDATACOUNT (MOXA + 23)
1539 #define MOXA_GET_IOQUEUE (MOXA + 27)
1540 #define MOXA_FLUSH_QUEUE (MOXA + 28)
1541 #define MOXA_GET_CONF (MOXA + 35) /* configuration */
1542 #define MOXA_GET_MAJOR (MOXA + 63)
1543 #define MOXA_GET_CUMAJOR (MOXA + 64)
1544 #define MOXA_GETMSTATUS (MOXA + 65)
1547 struct moxaq_str {
1548 int inq;
1549 int outq;
1552 struct dl_str {
1553 char __user *buf;
1554 int len;
1555 int cardno;
1558 static struct moxaq_str temp_queue[MAX_PORTS];
1559 static struct dl_str dltmp;
1561 void MoxaPortFlushData(int port, int mode)
1563 void __iomem *ofsAddr;
1564 if ((mode < 0) || (mode > 2))
1565 return;
1566 ofsAddr = moxaTableAddr[port];
1567 moxafunc(ofsAddr, FC_FlushQueue, mode);
1568 if (mode != 1) {
1569 moxaLowChkFlag[port] = 0;
1570 low_water_check(ofsAddr);
1574 int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port)
1576 int i;
1577 int status;
1578 int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
1579 void __user *argp = (void __user *)arg;
1581 if (port == QueryPort) {
1582 if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_INIT_DRIVER) &&
1583 (cmd != MOXA_LOAD_BIOS) && (cmd != MOXA_FIND_BOARD) && (cmd != MOXA_LOAD_C320B) &&
1584 (cmd != MOXA_LOAD_CODE) && (cmd != MOXA_GETDATACOUNT) &&
1585 (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
1586 (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
1587 return (-EINVAL);
1589 switch (cmd) {
1590 case MOXA_GET_CONF:
1591 if(copy_to_user(argp, &moxa_boards, MAX_BOARDS * sizeof(moxa_board_conf)))
1592 return -EFAULT;
1593 return (0);
1594 case MOXA_INIT_DRIVER:
1595 if ((int) arg == 0x404)
1596 MoxaDriverInit();
1597 return (0);
1598 case MOXA_GETDATACOUNT:
1599 moxaLog.tick = jiffies;
1600 if(copy_to_user(argp, &moxaLog, sizeof(mon_st)))
1601 return -EFAULT;
1602 return (0);
1603 case MOXA_FLUSH_QUEUE:
1604 MoxaPortFlushData(port, arg);
1605 return (0);
1606 case MOXA_GET_IOQUEUE:
1607 for (i = 0; i < MAX_PORTS; i++) {
1608 if (moxaChkPort[i]) {
1609 temp_queue[i].inq = MoxaPortRxQueue(i);
1610 temp_queue[i].outq = MoxaPortTxQueue(i);
1613 if(copy_to_user(argp, temp_queue, sizeof(struct moxaq_str) * MAX_PORTS))
1614 return -EFAULT;
1615 return (0);
1616 case MOXA_GET_OQUEUE:
1617 i = MoxaPortTxQueue(port);
1618 return put_user(i, (unsigned long __user *)argp);
1619 case MOXA_GET_IQUEUE:
1620 i = MoxaPortRxQueue(port);
1621 return put_user(i, (unsigned long __user *)argp);
1622 case MOXA_GET_MAJOR:
1623 if(copy_to_user(argp, &ttymajor, sizeof(int)))
1624 return -EFAULT;
1625 return 0;
1626 case MOXA_GET_CUMAJOR:
1627 i = 0;
1628 if(copy_to_user(argp, &i, sizeof(int)))
1629 return -EFAULT;
1630 return 0;
1631 case MOXA_GETMSTATUS:
1632 for (i = 0; i < MAX_PORTS; i++) {
1633 GMStatus[i].ri = 0;
1634 GMStatus[i].dcd = 0;
1635 GMStatus[i].dsr = 0;
1636 GMStatus[i].cts = 0;
1637 if (!moxaChkPort[i]) {
1638 continue;
1639 } else {
1640 status = MoxaPortLineStatus(moxaChannels[i].port);
1641 if (status & 1)
1642 GMStatus[i].cts = 1;
1643 if (status & 2)
1644 GMStatus[i].dsr = 1;
1645 if (status & 4)
1646 GMStatus[i].dcd = 1;
1649 if (!moxaChannels[i].tty || !moxaChannels[i].tty->termios)
1650 GMStatus[i].cflag = moxaChannels[i].cflag;
1651 else
1652 GMStatus[i].cflag = moxaChannels[i].tty->termios->c_cflag;
1654 if(copy_to_user(argp, GMStatus, sizeof(struct mxser_mstatus) * MAX_PORTS))
1655 return -EFAULT;
1656 return 0;
1657 default:
1658 return (-ENOIOCTLCMD);
1659 case MOXA_LOAD_BIOS:
1660 case MOXA_FIND_BOARD:
1661 case MOXA_LOAD_C320B:
1662 case MOXA_LOAD_CODE:
1663 if (!capable(CAP_SYS_RAWIO))
1664 return -EPERM;
1665 break;
1668 if(copy_from_user(&dltmp, argp, sizeof(struct dl_str)))
1669 return -EFAULT;
1670 if(dltmp.cardno < 0 || dltmp.cardno >= MAX_BOARDS)
1671 return -EINVAL;
1673 switch(cmd)
1675 case MOXA_LOAD_BIOS:
1676 i = moxaloadbios(dltmp.cardno, dltmp.buf, dltmp.len);
1677 return (i);
1678 case MOXA_FIND_BOARD:
1679 return moxafindcard(dltmp.cardno);
1680 case MOXA_LOAD_C320B:
1681 moxaload320b(dltmp.cardno, dltmp.buf, dltmp.len);
1682 default: /* to keep gcc happy */
1683 return (0);
1684 case MOXA_LOAD_CODE:
1685 i = moxaloadcode(dltmp.cardno, dltmp.buf, dltmp.len);
1686 if (i == -1)
1687 return (-EFAULT);
1688 return (i);
1693 int MoxaDriverPoll(void)
1695 register ushort temp;
1696 register int card;
1697 void __iomem *ofsAddr;
1698 void __iomem *ip;
1699 int port, p, ports;
1701 if (moxaCard == 0)
1702 return (-1);
1703 for (card = 0; card < MAX_BOARDS; card++) {
1704 if (loadstat[card] == 0)
1705 continue;
1706 if ((ports = moxa_boards[card].numPorts) == 0)
1707 continue;
1708 if (readb(moxaIntPend[card]) == 0xff) {
1709 ip = moxaIntTable[card] + readb(moxaIntNdx[card]);
1710 p = card * MAX_PORTS_PER_BOARD;
1711 ports <<= 1;
1712 for (port = 0; port < ports; port += 2, p++) {
1713 if ((temp = readw(ip + port)) != 0) {
1714 writew(0, ip + port);
1715 ofsAddr = moxaTableAddr[p];
1716 if (temp & IntrTx)
1717 writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
1718 if (temp & IntrBreak) {
1719 moxaBreakCnt[p]++;
1721 if (temp & IntrLine) {
1722 if (readb(ofsAddr + FlagStat) & DCD_state) {
1723 if ((moxaDCDState[p] & DCD_oldstate) == 0)
1724 moxaDCDState[p] = (DCD_oldstate |
1725 DCD_changed);
1726 } else {
1727 if (moxaDCDState[p] & DCD_oldstate)
1728 moxaDCDState[p] = DCD_changed;
1733 writeb(0, moxaIntPend[card]);
1735 if (moxaLowWaterChk) {
1736 p = card * MAX_PORTS_PER_BOARD;
1737 for (port = 0; port < ports; port++, p++) {
1738 if (moxaLowChkFlag[p]) {
1739 moxaLowChkFlag[p] = 0;
1740 ofsAddr = moxaTableAddr[p];
1741 low_water_check(ofsAddr);
1746 moxaLowWaterChk = 0;
1747 return (0);
1750 /*****************************************************************************
1751 * Card level function: *
1752 * 1. MoxaPortsOfCard(int cardno); *
1753 *****************************************************************************/
1754 int MoxaPortsOfCard(int cardno)
1757 if (moxa_boards[cardno].boardType == 0)
1758 return (0);
1759 return (moxa_boards[cardno].numPorts);
1762 /*****************************************************************************
1763 * Port level functions: *
1764 * 1. MoxaPortIsValid(int port); *
1765 * 2. MoxaPortEnable(int port); *
1766 * 3. MoxaPortDisable(int port); *
1767 * 4. MoxaPortGetMaxBaud(int port); *
1768 * 5. MoxaPortGetCurBaud(int port); *
1769 * 6. MoxaPortSetBaud(int port, long baud); *
1770 * 7. MoxaPortSetMode(int port, int databit, int stopbit, int parity); *
1771 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1772 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1773 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1774 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1775 * 12. MoxaPortLineStatus(int port); *
1776 * 13. MoxaPortDCDChange(int port); *
1777 * 14. MoxaPortDCDON(int port); *
1778 * 15. MoxaPortFlushData(int port, int mode); *
1779 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1780 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
1781 * 18. MoxaPortTxBufSize(int port); *
1782 * 19. MoxaPortRxBufSize(int port); *
1783 * 20. MoxaPortTxQueue(int port); *
1784 * 21. MoxaPortTxFree(int port); *
1785 * 22. MoxaPortRxQueue(int port); *
1786 * 23. MoxaPortRxFree(int port); *
1787 * 24. MoxaPortTxDisable(int port); *
1788 * 25. MoxaPortTxEnable(int port); *
1789 * 26. MoxaPortGetBrkCnt(int port); *
1790 * 27. MoxaPortResetBrkCnt(int port); *
1791 * 28. MoxaPortSetXonXoff(int port, int xonValue, int xoffValue); *
1792 * 29. MoxaPortIsTxHold(int port); *
1793 * 30. MoxaPortSendBreak(int port, int ticks); *
1794 *****************************************************************************/
1796 * Moxa Port Number Description:
1798 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1799 * the port number using in MOXA driver functions will be 0 to 31 for
1800 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1801 * to 127 for fourth. For example, if you setup three MOXA boards,
1802 * first board is C218, second board is C320-16 and third board is
1803 * C320-32. The port number of first board (C218 - 8 ports) is from
1804 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1805 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1806 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1807 * 127 will be invalid.
1810 * Moxa Functions Description:
1812 * Function 1: Driver initialization routine, this routine must be
1813 * called when initialized driver.
1814 * Syntax:
1815 * void MoxaDriverInit();
1818 * Function 2: Moxa driver private IOCTL command processing.
1819 * Syntax:
1820 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1822 * unsigned int cmd : IOCTL command
1823 * unsigned long arg : IOCTL argument
1824 * int port : port number (0 - 127)
1826 * return: 0 (OK)
1827 * -EINVAL
1828 * -ENOIOCTLCMD
1831 * Function 3: Moxa driver polling process routine.
1832 * Syntax:
1833 * int MoxaDriverPoll(void);
1835 * return: 0 ; polling O.K.
1836 * -1 : no any Moxa card.
1839 * Function 4: Get the ports of this card.
1840 * Syntax:
1841 * int MoxaPortsOfCard(int cardno);
1843 * int cardno : card number (0 - 3)
1845 * return: 0 : this card is invalid
1846 * 8/16/24/32
1849 * Function 5: Check this port is valid or invalid
1850 * Syntax:
1851 * int MoxaPortIsValid(int port);
1852 * int port : port number (0 - 127, ref port description)
1854 * return: 0 : this port is invalid
1855 * 1 : this port is valid
1858 * Function 6: Enable this port to start Tx/Rx data.
1859 * Syntax:
1860 * void MoxaPortEnable(int port);
1861 * int port : port number (0 - 127)
1864 * Function 7: Disable this port
1865 * Syntax:
1866 * void MoxaPortDisable(int port);
1867 * int port : port number (0 - 127)
1870 * Function 8: Get the maximun available baud rate of this port.
1871 * Syntax:
1872 * long MoxaPortGetMaxBaud(int port);
1873 * int port : port number (0 - 127)
1875 * return: 0 : this port is invalid
1876 * 38400/57600/115200 bps
1879 * Function 9: Get the current baud rate of this port.
1880 * Syntax:
1881 * long MoxaPortGetCurBaud(int port);
1882 * int port : port number (0 - 127)
1884 * return: 0 : this port is invalid
1885 * 50 - 115200 bps
1888 * Function 10: Setting baud rate of this port.
1889 * Syntax:
1890 * long MoxaPortSetBaud(int port, long baud);
1891 * int port : port number (0 - 127)
1892 * long baud : baud rate (50 - 115200)
1894 * return: 0 : this port is invalid or baud < 50
1895 * 50 - 115200 : the real baud rate set to the port, if
1896 * the argument baud is large than maximun
1897 * available baud rate, the real setting
1898 * baud rate will be the maximun baud rate.
1901 * Function 11: Setting the data-bits/stop-bits/parity of this port
1902 * Syntax:
1903 * int MoxaPortSetMode(int port, int databits, int stopbits, int parity);
1904 * int port : port number (0 - 127)
1905 * int databits : data bits (8/7/6/5)
1906 * int stopbits : stop bits (2/1/0, 0 show 1.5 stop bits)
1907 int parity : parity (0:None,1:Odd,2:Even,3:Mark,4:Space)
1909 * return: -1 : invalid parameter
1910 * 0 : setting O.K.
1913 * Function 12: Configure the port.
1914 * Syntax:
1915 * int MoxaPortSetTermio(int port, struct termios *termio, speed_t baud);
1916 * int port : port number (0 - 127)
1917 * struct termios * termio : termio structure pointer
1918 * speed_t baud : baud rate
1920 * return: -1 : this port is invalid or termio == NULL
1921 * 0 : setting O.K.
1924 * Function 13: Get the DTR/RTS state of this port.
1925 * Syntax:
1926 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1927 * int port : port number (0 - 127)
1928 * int * dtrState : pointer to INT to receive the current DTR
1929 * state. (if NULL, this function will not
1930 * write to this address)
1931 * int * rtsState : pointer to INT to receive the current RTS
1932 * state. (if NULL, this function will not
1933 * write to this address)
1935 * return: -1 : this port is invalid
1936 * 0 : O.K.
1939 * Function 14: Setting the DTR/RTS output state of this port.
1940 * Syntax:
1941 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1942 * int port : port number (0 - 127)
1943 * int dtrState : DTR output state (0: off, 1: on)
1944 * int rtsState : RTS output state (0: off, 1: on)
1947 * Function 15: Setting the flow control of this port.
1948 * Syntax:
1949 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1950 * int txFlow,int xany);
1951 * int port : port number (0 - 127)
1952 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1953 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1954 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1955 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1956 * int xany : S/W XANY flow control (0: no, 1: yes)
1959 * Function 16: Get ths line status of this port
1960 * Syntax:
1961 * int MoxaPortLineStatus(int port);
1962 * int port : port number (0 - 127)
1964 * return: Bit 0 - CTS state (0: off, 1: on)
1965 * Bit 1 - DSR state (0: off, 1: on)
1966 * Bit 2 - DCD state (0: off, 1: on)
1969 * Function 17: Check the DCD state has changed since the last read
1970 * of this function.
1971 * Syntax:
1972 * int MoxaPortDCDChange(int port);
1973 * int port : port number (0 - 127)
1975 * return: 0 : no changed
1976 * 1 : DCD has changed
1979 * Function 18: Check ths current DCD state is ON or not.
1980 * Syntax:
1981 * int MoxaPortDCDON(int port);
1982 * int port : port number (0 - 127)
1984 * return: 0 : DCD off
1985 * 1 : DCD on
1988 * Function 19: Flush the Rx/Tx buffer data of this port.
1989 * Syntax:
1990 * void MoxaPortFlushData(int port, int mode);
1991 * int port : port number (0 - 127)
1992 * int mode
1993 * 0 : flush the Rx buffer
1994 * 1 : flush the Tx buffer
1995 * 2 : flush the Rx and Tx buffer
1998 * Function 20: Write data.
1999 * Syntax:
2000 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
2001 * int port : port number (0 - 127)
2002 * unsigned char * buffer : pointer to write data buffer.
2003 * int length : write data length
2005 * return: 0 - length : real write data length
2008 * Function 21: Read data.
2009 * Syntax:
2010 * int MoxaPortReadData(int port, struct tty_struct *tty);
2011 * int port : port number (0 - 127)
2012 * struct tty_struct *tty : tty for data
2014 * return: 0 - length : real read data length
2017 * Function 22: Get the Tx buffer size of this port
2018 * Syntax:
2019 * int MoxaPortTxBufSize(int port);
2020 * int port : port number (0 - 127)
2022 * return: .. : Tx buffer size
2025 * Function 23: Get the Rx buffer size of this port
2026 * Syntax:
2027 * int MoxaPortRxBufSize(int port);
2028 * int port : port number (0 - 127)
2030 * return: .. : Rx buffer size
2033 * Function 24: Get the Tx buffer current queued data bytes
2034 * Syntax:
2035 * int MoxaPortTxQueue(int port);
2036 * int port : port number (0 - 127)
2038 * return: .. : Tx buffer current queued data bytes
2041 * Function 25: Get the Tx buffer current free space
2042 * Syntax:
2043 * int MoxaPortTxFree(int port);
2044 * int port : port number (0 - 127)
2046 * return: .. : Tx buffer current free space
2049 * Function 26: Get the Rx buffer current queued data bytes
2050 * Syntax:
2051 * int MoxaPortRxQueue(int port);
2052 * int port : port number (0 - 127)
2054 * return: .. : Rx buffer current queued data bytes
2057 * Function 27: Get the Rx buffer current free space
2058 * Syntax:
2059 * int MoxaPortRxFree(int port);
2060 * int port : port number (0 - 127)
2062 * return: .. : Rx buffer current free space
2065 * Function 28: Disable port data transmission.
2066 * Syntax:
2067 * void MoxaPortTxDisable(int port);
2068 * int port : port number (0 - 127)
2071 * Function 29: Enable port data transmission.
2072 * Syntax:
2073 * void MoxaPortTxEnable(int port);
2074 * int port : port number (0 - 127)
2077 * Function 30: Get the received BREAK signal count.
2078 * Syntax:
2079 * int MoxaPortGetBrkCnt(int port);
2080 * int port : port number (0 - 127)
2082 * return: 0 - .. : BREAK signal count
2085 * Function 31: Get the received BREAK signal count and reset it.
2086 * Syntax:
2087 * int MoxaPortResetBrkCnt(int port);
2088 * int port : port number (0 - 127)
2090 * return: 0 - .. : BREAK signal count
2093 * Function 32: Set the S/W flow control new XON/XOFF value, default
2094 * XON is 0x11 & XOFF is 0x13.
2095 * Syntax:
2096 * void MoxaPortSetXonXoff(int port, int xonValue, int xoffValue);
2097 * int port : port number (0 - 127)
2098 * int xonValue : new XON value (0 - 255)
2099 * int xoffValue : new XOFF value (0 - 255)
2102 * Function 33: Check this port's transmission is hold by remote site
2103 * because the flow control.
2104 * Syntax:
2105 * int MoxaPortIsTxHold(int port);
2106 * int port : port number (0 - 127)
2108 * return: 0 : normal
2109 * 1 : hold by remote site
2112 * Function 34: Send out a BREAK signal.
2113 * Syntax:
2114 * void MoxaPortSendBreak(int port, int ms100);
2115 * int port : port number (0 - 127)
2116 * int ms100 : break signal time interval.
2117 * unit: 100 mini-second. if ms100 == 0, it will
2118 * send out a about 250 ms BREAK signal.
2121 int MoxaPortIsValid(int port)
2124 if (moxaCard == 0)
2125 return (0);
2126 if (moxaChkPort[port] == 0)
2127 return (0);
2128 return (1);
2131 void MoxaPortEnable(int port)
2133 void __iomem *ofsAddr;
2134 int MoxaPortLineStatus(int);
2135 short lowwater = 512;
2137 ofsAddr = moxaTableAddr[port];
2138 writew(lowwater, ofsAddr + Low_water);
2139 moxaBreakCnt[port] = 0;
2140 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2141 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2142 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
2143 } else {
2144 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
2147 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
2148 moxafunc(ofsAddr, FC_FlushQueue, 2);
2150 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
2151 MoxaPortLineStatus(port);
2154 void MoxaPortDisable(int port)
2156 void __iomem *ofsAddr = moxaTableAddr[port];
2158 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
2159 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
2160 writew(0, ofsAddr + HostStat);
2161 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
2164 long MoxaPortGetMaxBaud(int port)
2166 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2167 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI))
2168 return (460800L);
2169 else
2170 return (921600L);
2174 long MoxaPortSetBaud(int port, long baud)
2176 void __iomem *ofsAddr;
2177 long max, clock;
2178 unsigned int val;
2180 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
2181 return (0);
2182 ofsAddr = moxaTableAddr[port];
2183 if (baud > max)
2184 baud = max;
2185 if (max == 38400L)
2186 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
2187 else if (max == 57600L)
2188 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
2189 else
2190 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
2191 val = clock / baud;
2192 moxafunc(ofsAddr, FC_SetBaud, val);
2193 baud = clock / val;
2194 moxaCurBaud[port] = baud;
2195 return (baud);
2198 int MoxaPortSetTermio(int port, struct termios *termio, speed_t baud)
2200 void __iomem *ofsAddr;
2201 tcflag_t cflag;
2202 tcflag_t mode = 0;
2204 if (moxaChkPort[port] == 0 || termio == 0)
2205 return (-1);
2206 ofsAddr = moxaTableAddr[port];
2207 cflag = termio->c_cflag; /* termio->c_cflag */
2209 mode = termio->c_cflag & CSIZE;
2210 if (mode == CS5)
2211 mode = MX_CS5;
2212 else if (mode == CS6)
2213 mode = MX_CS6;
2214 else if (mode == CS7)
2215 mode = MX_CS7;
2216 else if (mode == CS8)
2217 mode = MX_CS8;
2219 if (termio->c_cflag & CSTOPB) {
2220 if (mode == MX_CS5)
2221 mode |= MX_STOP15;
2222 else
2223 mode |= MX_STOP2;
2224 } else
2225 mode |= MX_STOP1;
2227 if (termio->c_cflag & PARENB) {
2228 if (termio->c_cflag & PARODD)
2229 mode |= MX_PARODD;
2230 else
2231 mode |= MX_PAREVEN;
2232 } else
2233 mode |= MX_PARNONE;
2235 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
2237 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2238 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2239 if (baud >= 921600L)
2240 return (-1);
2242 MoxaPortSetBaud(port, baud);
2244 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
2245 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
2246 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
2247 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
2248 wait_finish(ofsAddr);
2251 return (0);
2254 int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState)
2257 if (!MoxaPortIsValid(port))
2258 return (-1);
2259 if (dtrState) {
2260 if (moxaLineCtrl[port] & DTR_ON)
2261 *dtrState = 1;
2262 else
2263 *dtrState = 0;
2265 if (rtsState) {
2266 if (moxaLineCtrl[port] & RTS_ON)
2267 *rtsState = 1;
2268 else
2269 *rtsState = 0;
2271 return (0);
2274 void MoxaPortLineCtrl(int port, int dtr, int rts)
2276 void __iomem *ofsAddr;
2277 int mode;
2279 ofsAddr = moxaTableAddr[port];
2280 mode = 0;
2281 if (dtr)
2282 mode |= DTR_ON;
2283 if (rts)
2284 mode |= RTS_ON;
2285 moxaLineCtrl[port] = mode;
2286 moxafunc(ofsAddr, FC_LineControl, mode);
2289 void MoxaPortFlowCtrl(int port, int rts, int cts, int txflow, int rxflow, int txany)
2291 void __iomem *ofsAddr;
2292 int mode;
2294 ofsAddr = moxaTableAddr[port];
2295 mode = 0;
2296 if (rts)
2297 mode |= RTS_FlowCtl;
2298 if (cts)
2299 mode |= CTS_FlowCtl;
2300 if (txflow)
2301 mode |= Tx_FlowCtl;
2302 if (rxflow)
2303 mode |= Rx_FlowCtl;
2304 if (txany)
2305 mode |= IXM_IXANY;
2306 moxafunc(ofsAddr, FC_SetFlowCtl, mode);
2309 int MoxaPortLineStatus(int port)
2311 void __iomem *ofsAddr;
2312 int val;
2314 ofsAddr = moxaTableAddr[port];
2315 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2316 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2317 moxafunc(ofsAddr, FC_LineStatus, 0);
2318 val = readw(ofsAddr + FuncArg);
2319 } else {
2320 val = readw(ofsAddr + FlagStat) >> 4;
2322 val &= 0x0B;
2323 if (val & 8) {
2324 val |= 4;
2325 if ((moxaDCDState[port] & DCD_oldstate) == 0)
2326 moxaDCDState[port] = (DCD_oldstate | DCD_changed);
2327 } else {
2328 if (moxaDCDState[port] & DCD_oldstate)
2329 moxaDCDState[port] = DCD_changed;
2331 val &= 7;
2332 return (val);
2335 int MoxaPortDCDChange(int port)
2337 int n;
2339 if (moxaChkPort[port] == 0)
2340 return (0);
2341 n = moxaDCDState[port];
2342 moxaDCDState[port] &= ~DCD_changed;
2343 n &= DCD_changed;
2344 return (n);
2347 int MoxaPortDCDON(int port)
2349 int n;
2351 if (moxaChkPort[port] == 0)
2352 return (0);
2353 if (moxaDCDState[port] & DCD_oldstate)
2354 n = 1;
2355 else
2356 n = 0;
2357 return (n);
2362 int MoxaDumpMem(int port, unsigned char * buffer, int len)
2364 int i;
2365 unsigned long baseAddr,ofsAddr,ofs;
2367 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2368 ofs = baseAddr + DynPage_addr + pageofs;
2369 if (len > 0x2000L)
2370 len = 0x2000L;
2371 for (i = 0; i < len; i++)
2372 buffer[i] = readb(ofs+i);
2377 int MoxaPortWriteData(int port, unsigned char * buffer, int len)
2379 int c, total, i;
2380 ushort tail;
2381 int cnt;
2382 ushort head, tx_mask, spage, epage;
2383 ushort pageno, pageofs, bufhead;
2384 void __iomem *baseAddr, *ofsAddr, *ofs;
2386 ofsAddr = moxaTableAddr[port];
2387 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2388 tx_mask = readw(ofsAddr + TX_mask);
2389 spage = readw(ofsAddr + Page_txb);
2390 epage = readw(ofsAddr + EndPage_txb);
2391 tail = readw(ofsAddr + TXwptr);
2392 head = readw(ofsAddr + TXrptr);
2393 c = (head > tail) ? (head - tail - 1)
2394 : (head - tail + tx_mask);
2395 if (c > len)
2396 c = len;
2397 moxaLog.txcnt[port] += c;
2398 total = c;
2399 if (spage == epage) {
2400 bufhead = readw(ofsAddr + Ofs_txb);
2401 writew(spage, baseAddr + Control_reg);
2402 while (c > 0) {
2403 if (head > tail)
2404 len = head - tail - 1;
2405 else
2406 len = tx_mask + 1 - tail;
2407 len = (c > len) ? len : c;
2408 ofs = baseAddr + DynPage_addr + bufhead + tail;
2409 for (i = 0; i < len; i++)
2410 writeb(*buffer++, ofs + i);
2411 tail = (tail + len) & tx_mask;
2412 c -= len;
2414 writew(tail, ofsAddr + TXwptr);
2415 } else {
2416 len = c;
2417 pageno = spage + (tail >> 13);
2418 pageofs = tail & Page_mask;
2419 do {
2420 cnt = Page_size - pageofs;
2421 if (cnt > c)
2422 cnt = c;
2423 c -= cnt;
2424 writeb(pageno, baseAddr + Control_reg);
2425 ofs = baseAddr + DynPage_addr + pageofs;
2426 for (i = 0; i < cnt; i++)
2427 writeb(*buffer++, ofs + i);
2428 if (c == 0) {
2429 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2430 break;
2432 if (++pageno == epage)
2433 pageno = spage;
2434 pageofs = 0;
2435 } while (1);
2437 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2438 return (total);
2441 int MoxaPortReadData(int port, struct tty_struct *tty)
2443 register ushort head, pageofs;
2444 int i, count, cnt, len, total, remain;
2445 ushort tail, rx_mask, spage, epage;
2446 ushort pageno, bufhead;
2447 void __iomem *baseAddr, *ofsAddr, *ofs;
2449 ofsAddr = moxaTableAddr[port];
2450 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2451 head = readw(ofsAddr + RXrptr);
2452 tail = readw(ofsAddr + RXwptr);
2453 rx_mask = readw(ofsAddr + RX_mask);
2454 spage = readw(ofsAddr + Page_rxb);
2455 epage = readw(ofsAddr + EndPage_rxb);
2456 count = (tail >= head) ? (tail - head)
2457 : (tail - head + rx_mask + 1);
2458 if (count == 0)
2459 return 0;
2461 total = count;
2462 remain = count - total;
2463 moxaLog.rxcnt[port] += total;
2464 count = total;
2465 if (spage == epage) {
2466 bufhead = readw(ofsAddr + Ofs_rxb);
2467 writew(spage, baseAddr + Control_reg);
2468 while (count > 0) {
2469 if (tail >= head)
2470 len = tail - head;
2471 else
2472 len = rx_mask + 1 - head;
2473 len = (count > len) ? len : count;
2474 ofs = baseAddr + DynPage_addr + bufhead + head;
2475 for (i = 0; i < len; i++)
2476 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2477 head = (head + len) & rx_mask;
2478 count -= len;
2480 writew(head, ofsAddr + RXrptr);
2481 } else {
2482 len = count;
2483 pageno = spage + (head >> 13);
2484 pageofs = head & Page_mask;
2485 do {
2486 cnt = Page_size - pageofs;
2487 if (cnt > count)
2488 cnt = count;
2489 count -= cnt;
2490 writew(pageno, baseAddr + Control_reg);
2491 ofs = baseAddr + DynPage_addr + pageofs;
2492 for (i = 0; i < cnt; i++)
2493 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2494 if (count == 0) {
2495 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2496 break;
2498 if (++pageno == epage)
2499 pageno = spage;
2500 pageofs = 0;
2501 } while (1);
2503 if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2504 moxaLowWaterChk = 1;
2505 moxaLowChkFlag[port] = 1;
2507 return (total);
2511 int MoxaPortTxQueue(int port)
2513 void __iomem *ofsAddr;
2514 ushort rptr, wptr, mask;
2515 int len;
2517 ofsAddr = moxaTableAddr[port];
2518 rptr = readw(ofsAddr + TXrptr);
2519 wptr = readw(ofsAddr + TXwptr);
2520 mask = readw(ofsAddr + TX_mask);
2521 len = (wptr - rptr) & mask;
2522 return (len);
2525 int MoxaPortTxFree(int port)
2527 void __iomem *ofsAddr;
2528 ushort rptr, wptr, mask;
2529 int len;
2531 ofsAddr = moxaTableAddr[port];
2532 rptr = readw(ofsAddr + TXrptr);
2533 wptr = readw(ofsAddr + TXwptr);
2534 mask = readw(ofsAddr + TX_mask);
2535 len = mask - ((wptr - rptr) & mask);
2536 return (len);
2539 int MoxaPortRxQueue(int port)
2541 void __iomem *ofsAddr;
2542 ushort rptr, wptr, mask;
2543 int len;
2545 ofsAddr = moxaTableAddr[port];
2546 rptr = readw(ofsAddr + RXrptr);
2547 wptr = readw(ofsAddr + RXwptr);
2548 mask = readw(ofsAddr + RX_mask);
2549 len = (wptr - rptr) & mask;
2550 return (len);
2554 void MoxaPortTxDisable(int port)
2556 void __iomem *ofsAddr;
2558 ofsAddr = moxaTableAddr[port];
2559 moxafunc(ofsAddr, FC_SetXoffState, Magic_code);
2562 void MoxaPortTxEnable(int port)
2564 void __iomem *ofsAddr;
2566 ofsAddr = moxaTableAddr[port];
2567 moxafunc(ofsAddr, FC_SetXonState, Magic_code);
2571 int MoxaPortResetBrkCnt(int port)
2573 ushort cnt;
2574 cnt = moxaBreakCnt[port];
2575 moxaBreakCnt[port] = 0;
2576 return (cnt);
2580 void MoxaPortSendBreak(int port, int ms100)
2582 void __iomem *ofsAddr;
2584 ofsAddr = moxaTableAddr[port];
2585 if (ms100) {
2586 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2587 moxadelay(ms100 * (HZ / 10));
2588 } else {
2589 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2590 moxadelay(HZ / 4); /* 250 ms */
2592 moxafunc(ofsAddr, FC_StopBreak, Magic_code);
2595 static int moxa_get_serial_info(struct moxa_str *info,
2596 struct serial_struct __user *retinfo)
2598 struct serial_struct tmp;
2600 memset(&tmp, 0, sizeof(tmp));
2601 tmp.type = info->type;
2602 tmp.line = info->port;
2603 tmp.port = 0;
2604 tmp.irq = 0;
2605 tmp.flags = info->asyncflags;
2606 tmp.baud_base = 921600;
2607 tmp.close_delay = info->close_delay;
2608 tmp.closing_wait = info->closing_wait;
2609 tmp.custom_divisor = 0;
2610 tmp.hub6 = 0;
2611 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2612 return -EFAULT;
2613 return (0);
2617 static int moxa_set_serial_info(struct moxa_str *info,
2618 struct serial_struct __user *new_info)
2620 struct serial_struct new_serial;
2622 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2623 return -EFAULT;
2625 if ((new_serial.irq != 0) ||
2626 (new_serial.port != 0) ||
2627 // (new_serial.type != info->type) ||
2628 (new_serial.custom_divisor != 0) ||
2629 (new_serial.baud_base != 921600))
2630 return (-EPERM);
2632 if (!capable(CAP_SYS_ADMIN)) {
2633 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2634 (info->asyncflags & ~ASYNC_USR_MASK)))
2635 return (-EPERM);
2636 } else {
2637 info->close_delay = new_serial.close_delay * HZ / 100;
2638 info->closing_wait = new_serial.closing_wait * HZ / 100;
2641 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2642 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2644 if (new_serial.type == PORT_16550A) {
2645 MoxaSetFifo(info->port, 1);
2646 } else {
2647 MoxaSetFifo(info->port, 0);
2650 info->type = new_serial.type;
2651 return (0);
2656 /*****************************************************************************
2657 * Static local functions: *
2658 *****************************************************************************/
2660 * moxadelay - delays a specified number ticks
2662 static void moxadelay(int tick)
2664 unsigned long st, et;
2666 st = jiffies;
2667 et = st + tick;
2668 while (time_before(jiffies, et));
2671 static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
2674 writew(arg, ofsAddr + FuncArg);
2675 writew(cmd, ofsAddr + FuncCode);
2676 wait_finish(ofsAddr);
2679 static void wait_finish(void __iomem *ofsAddr)
2681 unsigned long i, j;
2683 i = jiffies;
2684 while (readw(ofsAddr + FuncCode) != 0) {
2685 j = jiffies;
2686 if ((j - i) > moxaFuncTout) {
2687 return;
2692 static void low_water_check(void __iomem *ofsAddr)
2694 int len;
2695 ushort rptr, wptr, mask;
2697 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2698 rptr = readw(ofsAddr + RXrptr);
2699 wptr = readw(ofsAddr + RXwptr);
2700 mask = readw(ofsAddr + RX_mask);
2701 len = (wptr - rptr) & mask;
2702 if (len <= Low_water)
2703 moxafunc(ofsAddr, FC_SendXon, 0);
2707 static int moxaloadbios(int cardno, unsigned char __user *tmp, int len)
2709 void __iomem *baseAddr;
2710 int i;
2712 if(copy_from_user(moxaBuff, tmp, len))
2713 return -EFAULT;
2714 baseAddr = moxaBaseAddr[cardno];
2715 writeb(HW_reset, baseAddr + Control_reg); /* reset */
2716 moxadelay(1); /* delay 10 ms */
2717 for (i = 0; i < 4096; i++)
2718 writeb(0, baseAddr + i); /* clear fix page */
2719 for (i = 0; i < len; i++)
2720 writeb(moxaBuff[i], baseAddr + i); /* download BIOS */
2721 writeb(0, baseAddr + Control_reg); /* restart */
2722 return (0);
2725 static int moxafindcard(int cardno)
2727 void __iomem *baseAddr;
2728 ushort tmp;
2730 baseAddr = moxaBaseAddr[cardno];
2731 switch (moxa_boards[cardno].boardType) {
2732 case MOXA_BOARD_C218_ISA:
2733 case MOXA_BOARD_C218_PCI:
2734 if ((tmp = readw(baseAddr + C218_key)) != C218_KeyCode) {
2735 return (-1);
2737 break;
2738 case MOXA_BOARD_CP204J:
2739 if ((tmp = readw(baseAddr + C218_key)) != CP204J_KeyCode) {
2740 return (-1);
2742 break;
2743 default:
2744 if ((tmp = readw(baseAddr + C320_key)) != C320_KeyCode) {
2745 return (-1);
2747 if ((tmp = readw(baseAddr + C320_status)) != STS_init) {
2748 return (-2);
2751 return (0);
2754 static int moxaload320b(int cardno, unsigned char __user *tmp, int len)
2756 void __iomem *baseAddr;
2757 int i;
2759 if(len > sizeof(moxaBuff))
2760 return -EINVAL;
2761 if(copy_from_user(moxaBuff, tmp, len))
2762 return -EFAULT;
2763 baseAddr = moxaBaseAddr[cardno];
2764 writew(len - 7168 - 2, baseAddr + C320bapi_len);
2765 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
2766 for (i = 0; i < 7168; i++)
2767 writeb(moxaBuff[i], baseAddr + DynPage_addr + i);
2768 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
2769 for (i = 0; i < (len - 7168); i++)
2770 writeb(moxaBuff[i + 7168], baseAddr + DynPage_addr + i);
2771 return (0);
2774 static int moxaloadcode(int cardno, unsigned char __user *tmp, int len)
2776 void __iomem *baseAddr, *ofsAddr;
2777 int retval, port, i;
2779 if(copy_from_user(moxaBuff, tmp, len))
2780 return -EFAULT;
2781 baseAddr = moxaBaseAddr[cardno];
2782 switch (moxa_boards[cardno].boardType) {
2783 case MOXA_BOARD_C218_ISA:
2784 case MOXA_BOARD_C218_PCI:
2785 case MOXA_BOARD_CP204J:
2786 retval = moxaloadc218(cardno, baseAddr, len);
2787 if (retval)
2788 return (retval);
2789 port = cardno * MAX_PORTS_PER_BOARD;
2790 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2791 moxaChkPort[port] = 1;
2792 moxaCurBaud[port] = 9600L;
2793 moxaDCDState[port] = 0;
2794 moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
2795 ofsAddr = moxaTableAddr[port];
2796 writew(C218rx_mask, ofsAddr + RX_mask);
2797 writew(C218tx_mask, ofsAddr + TX_mask);
2798 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
2799 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
2801 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
2802 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
2805 break;
2806 default:
2807 retval = moxaloadc320(cardno, baseAddr, len,
2808 &moxa_boards[cardno].numPorts);
2809 if (retval)
2810 return (retval);
2811 port = cardno * MAX_PORTS_PER_BOARD;
2812 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2813 moxaChkPort[port] = 1;
2814 moxaCurBaud[port] = 9600L;
2815 moxaDCDState[port] = 0;
2816 moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
2817 ofsAddr = moxaTableAddr[port];
2818 if (moxa_boards[cardno].numPorts == 8) {
2819 writew(C320p8rx_mask, ofsAddr + RX_mask);
2820 writew(C320p8tx_mask, ofsAddr + TX_mask);
2821 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
2822 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
2823 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
2824 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
2826 } else if (moxa_boards[cardno].numPorts == 16) {
2827 writew(C320p16rx_mask, ofsAddr + RX_mask);
2828 writew(C320p16tx_mask, ofsAddr + TX_mask);
2829 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
2830 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
2831 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
2832 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
2834 } else if (moxa_boards[cardno].numPorts == 24) {
2835 writew(C320p24rx_mask, ofsAddr + RX_mask);
2836 writew(C320p24tx_mask, ofsAddr + TX_mask);
2837 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
2838 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
2839 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
2840 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2841 } else if (moxa_boards[cardno].numPorts == 32) {
2842 writew(C320p32rx_mask, ofsAddr + RX_mask);
2843 writew(C320p32tx_mask, ofsAddr + TX_mask);
2844 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
2845 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
2846 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
2847 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
2848 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2851 break;
2853 loadstat[cardno] = 1;
2854 return (0);
2857 static int moxaloadc218(int cardno, void __iomem *baseAddr, int len)
2859 char retry;
2860 int i, j, len1, len2;
2861 ushort usum, *ptr, keycode;
2863 if (moxa_boards[cardno].boardType == MOXA_BOARD_CP204J)
2864 keycode = CP204J_KeyCode;
2865 else
2866 keycode = C218_KeyCode;
2867 usum = 0;
2868 len1 = len >> 1;
2869 ptr = (ushort *) moxaBuff;
2870 for (i = 0; i < len1; i++)
2871 usum += le16_to_cpu(*(ptr + i));
2872 retry = 0;
2873 do {
2874 len1 = len >> 1;
2875 j = 0;
2876 while (len1) {
2877 len2 = (len1 > 2048) ? 2048 : len1;
2878 len1 -= len2;
2879 for (i = 0; i < len2 << 1; i++)
2880 writeb(moxaBuff[i + j], baseAddr + C218_LoadBuf + i);
2881 j += i;
2883 writew(len2, baseAddr + C218DLoad_len);
2884 writew(0, baseAddr + C218_key);
2885 for (i = 0; i < 100; i++) {
2886 if (readw(baseAddr + C218_key) == keycode)
2887 break;
2888 moxadelay(1); /* delay 10 ms */
2890 if (readw(baseAddr + C218_key) != keycode) {
2891 return (-1);
2894 writew(0, baseAddr + C218DLoad_len);
2895 writew(usum, baseAddr + C218check_sum);
2896 writew(0, baseAddr + C218_key);
2897 for (i = 0; i < 100; i++) {
2898 if (readw(baseAddr + C218_key) == keycode)
2899 break;
2900 moxadelay(1); /* delay 10 ms */
2902 retry++;
2903 } while ((readb(baseAddr + C218chksum_ok) != 1) && (retry < 3));
2904 if (readb(baseAddr + C218chksum_ok) != 1) {
2905 return (-1);
2907 writew(0, baseAddr + C218_key);
2908 for (i = 0; i < 100; i++) {
2909 if (readw(baseAddr + Magic_no) == Magic_code)
2910 break;
2911 moxadelay(1); /* delay 10 ms */
2913 if (readw(baseAddr + Magic_no) != Magic_code) {
2914 return (-1);
2916 writew(1, baseAddr + Disable_IRQ);
2917 writew(0, baseAddr + Magic_no);
2918 for (i = 0; i < 100; i++) {
2919 if (readw(baseAddr + Magic_no) == Magic_code)
2920 break;
2921 moxadelay(1); /* delay 10 ms */
2923 if (readw(baseAddr + Magic_no) != Magic_code) {
2924 return (-1);
2926 moxaCard = 1;
2927 moxaIntNdx[cardno] = baseAddr + IRQindex;
2928 moxaIntPend[cardno] = baseAddr + IRQpending;
2929 moxaIntTable[cardno] = baseAddr + IRQtable;
2930 return (0);
2933 static int moxaloadc320(int cardno, void __iomem *baseAddr, int len, int *numPorts)
2935 ushort usum;
2936 int i, j, wlen, len2, retry;
2937 ushort *uptr;
2939 usum = 0;
2940 wlen = len >> 1;
2941 uptr = (ushort *) moxaBuff;
2942 for (i = 0; i < wlen; i++)
2943 usum += le16_to_cpu(uptr[i]);
2944 retry = 0;
2945 j = 0;
2946 do {
2947 while (wlen) {
2948 if (wlen > 2048)
2949 len2 = 2048;
2950 else
2951 len2 = wlen;
2952 wlen -= len2;
2953 len2 <<= 1;
2954 for (i = 0; i < len2; i++)
2955 writeb(moxaBuff[j + i], baseAddr + C320_LoadBuf + i);
2956 len2 >>= 1;
2957 j += i;
2958 writew(len2, baseAddr + C320DLoad_len);
2959 writew(0, baseAddr + C320_key);
2960 for (i = 0; i < 10; i++) {
2961 if (readw(baseAddr + C320_key) == C320_KeyCode)
2962 break;
2963 moxadelay(1);
2965 if (readw(baseAddr + C320_key) != C320_KeyCode)
2966 return (-1);
2968 writew(0, baseAddr + C320DLoad_len);
2969 writew(usum, baseAddr + C320check_sum);
2970 writew(0, baseAddr + C320_key);
2971 for (i = 0; i < 10; i++) {
2972 if (readw(baseAddr + C320_key) == C320_KeyCode)
2973 break;
2974 moxadelay(1);
2976 retry++;
2977 } while ((readb(baseAddr + C320chksum_ok) != 1) && (retry < 3));
2978 if (readb(baseAddr + C320chksum_ok) != 1)
2979 return (-1);
2980 writew(0, baseAddr + C320_key);
2981 for (i = 0; i < 600; i++) {
2982 if (readw(baseAddr + Magic_no) == Magic_code)
2983 break;
2984 moxadelay(1);
2986 if (readw(baseAddr + Magic_no) != Magic_code)
2987 return (-100);
2989 if (moxa_boards[cardno].busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
2990 writew(0x3800, baseAddr + TMS320_PORT1);
2991 writew(0x3900, baseAddr + TMS320_PORT2);
2992 writew(28499, baseAddr + TMS320_CLOCK);
2993 } else {
2994 writew(0x3200, baseAddr + TMS320_PORT1);
2995 writew(0x3400, baseAddr + TMS320_PORT2);
2996 writew(19999, baseAddr + TMS320_CLOCK);
2998 writew(1, baseAddr + Disable_IRQ);
2999 writew(0, baseAddr + Magic_no);
3000 for (i = 0; i < 500; i++) {
3001 if (readw(baseAddr + Magic_no) == Magic_code)
3002 break;
3003 moxadelay(1);
3005 if (readw(baseAddr + Magic_no) != Magic_code)
3006 return (-102);
3008 j = readw(baseAddr + Module_cnt);
3009 if (j <= 0)
3010 return (-101);
3011 *numPorts = j * 8;
3012 writew(j, baseAddr + Module_no);
3013 writew(0, baseAddr + Magic_no);
3014 for (i = 0; i < 600; i++) {
3015 if (readw(baseAddr + Magic_no) == Magic_code)
3016 break;
3017 moxadelay(1);
3019 if (readw(baseAddr + Magic_no) != Magic_code)
3020 return (-102);
3021 moxaCard = 1;
3022 moxaIntNdx[cardno] = baseAddr + IRQindex;
3023 moxaIntPend[cardno] = baseAddr + IRQpending;
3024 moxaIntTable[cardno] = baseAddr + IRQtable;
3025 return (0);
3028 #if 0
3029 long MoxaPortGetCurBaud(int port)
3032 if (moxaChkPort[port] == 0)
3033 return (0);
3034 return (moxaCurBaud[port]);
3036 #endif /* 0 */
3038 static void MoxaSetFifo(int port, int enable)
3040 void __iomem *ofsAddr = moxaTableAddr[port];
3042 if (!enable) {
3043 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
3044 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
3045 } else {
3046 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
3047 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
3051 #if 0
3052 int MoxaPortSetMode(int port, int databits, int stopbits, int parity)
3054 void __iomem *ofsAddr;
3055 int val;
3057 val = 0;
3058 switch (databits) {
3059 case 5:
3060 val |= 0;
3061 break;
3062 case 6:
3063 val |= 1;
3064 break;
3065 case 7:
3066 val |= 2;
3067 break;
3068 case 8:
3069 val |= 3;
3070 break;
3071 default:
3072 return (-1);
3074 switch (stopbits) {
3075 case 0:
3076 val |= 0;
3077 break; /* stop bits 1.5 */
3078 case 1:
3079 val |= 0;
3080 break;
3081 case 2:
3082 val |= 4;
3083 break;
3084 default:
3085 return (-1);
3087 switch (parity) {
3088 case 0:
3089 val |= 0x00;
3090 break; /* None */
3091 case 1:
3092 val |= 0x08;
3093 break; /* Odd */
3094 case 2:
3095 val |= 0x18;
3096 break; /* Even */
3097 case 3:
3098 val |= 0x28;
3099 break; /* Mark */
3100 case 4:
3101 val |= 0x38;
3102 break; /* Space */
3103 default:
3104 return (-1);
3106 ofsAddr = moxaTableAddr[port];
3107 moxafunc(ofsAddr, FC_SetMode, val);
3108 return (0);
3111 int MoxaPortTxBufSize(int port)
3113 void __iomem *ofsAddr;
3114 int size;
3116 ofsAddr = moxaTableAddr[port];
3117 size = readw(ofsAddr + TX_mask);
3118 return (size);
3121 int MoxaPortRxBufSize(int port)
3123 void __iomem *ofsAddr;
3124 int size;
3126 ofsAddr = moxaTableAddr[port];
3127 size = readw(ofsAddr + RX_mask);
3128 return (size);
3131 int MoxaPortRxFree(int port)
3133 void __iomem *ofsAddr;
3134 ushort rptr, wptr, mask;
3135 int len;
3137 ofsAddr = moxaTableAddr[port];
3138 rptr = readw(ofsAddr + RXrptr);
3139 wptr = readw(ofsAddr + RXwptr);
3140 mask = readw(ofsAddr + RX_mask);
3141 len = mask - ((wptr - rptr) & mask);
3142 return (len);
3144 int MoxaPortGetBrkCnt(int port)
3146 return (moxaBreakCnt[port]);
3149 void MoxaPortSetXonXoff(int port, int xonValue, int xoffValue)
3151 void __iomem *ofsAddr;
3153 ofsAddr = moxaTableAddr[port];
3154 writew(xonValue, ofsAddr + FuncArg);
3155 writew(xoffValue, ofsAddr + FuncArg1);
3156 writew(FC_SetXonXoff, ofsAddr + FuncCode);
3157 wait_finish(ofsAddr);
3160 int MoxaPortIsTxHold(int port)
3162 void __iomem *ofsAddr;
3163 int val;
3165 ofsAddr = moxaTableAddr[port];
3166 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
3167 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
3168 moxafunc(ofsAddr, FC_GetCCSR, 0);
3169 val = readw(ofsAddr + FuncArg);
3170 if (val & 0x04)
3171 return (1);
3172 } else {
3173 if (readw(ofsAddr + FlagStat) & Tx_flowOff)
3174 return (1);
3176 return (0);
3178 #endif