Merge with Linux 2.3.40.
[linux-2.6/linux-mips.git] / drivers / char / moxa.c
blob44f01bb773dd09de2c3abd124e44b865babb684d
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/config.h>
33 #include <linux/module.h>
34 #include <linux/types.h>
35 #include <linux/version.h>
36 #include <linux/mm.h>
37 #include <linux/ioport.h>
38 #include <linux/errno.h>
39 #include <linux/signal.h>
40 #include <linux/sched.h>
41 #include <linux/timer.h>
42 #include <linux/interrupt.h>
43 #include <linux/tty.h>
44 #include <linux/tty_flip.h>
45 #include <linux/config.h>
46 #include <linux/major.h>
47 #include <linux/string.h>
48 #include <linux/fcntl.h>
49 #include <linux/ptrace.h>
50 #include <linux/serial.h>
51 #include <linux/tty_driver.h>
52 #include <linux/delay.h>
53 #include <linux/pci.h>
55 #include <asm/system.h>
56 #include <asm/io.h>
57 #include <asm/segment.h>
58 #include <asm/bitops.h>
59 #include <asm/uaccess.h>
61 #define MOXA_VERSION "5.1k"
63 #define MOXAMAJOR 172
64 #define MOXACUMAJOR 173
66 #define put_to_user(arg1, arg2) put_user(arg1, (unsigned long *)arg2)
67 #define get_from_user(arg1, arg2) get_user(arg1, (unsigned int *)arg2)
69 #define MAX_BOARDS 4 /* Don't change this value */
70 #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
71 #define MAX_PORTS 128 /* Don't change this value */
74 * Define the Moxa PCI vendor and device IDs.
76 #define MOXA_BUS_TYPE_ISA 0
77 #define MOXA_BUS_TYPE_PCI 1
79 #ifndef PCI_VENDOR_ID_MOXA
80 #define PCI_VENDOR_ID_MOXA 0x1393
81 #endif
82 #ifndef PCI_DEVICE_ID_CP204J
83 #define PCI_DEVICE_ID_CP204J 0x2040
84 #endif
85 #ifndef PCI_DEVICE_ID_C218
86 #define PCI_DEVICE_ID_C218 0x2180
87 #endif
88 #ifndef PCI_DEVICE_ID_C320
89 #define PCI_DEVICE_ID_C320 0x3200
90 #endif
92 enum {
93 MOXA_BOARD_C218_PCI = 1,
94 MOXA_BOARD_C218_ISA,
95 MOXA_BOARD_C320_PCI,
96 MOXA_BOARD_C320_ISA,
97 MOXA_BOARD_CP204J,
100 static char *moxa_brdname[] =
102 "C218 Turbo PCI series",
103 "C218 Turbo ISA series",
104 "C320 Turbo PCI series",
105 "C320 Turbo ISA series",
106 "CP-204J series",
109 typedef struct {
110 unsigned short vendor_id;
111 unsigned short device_id;
112 unsigned short board_type;
113 } moxa_pciinfo;
115 static moxa_pciinfo moxa_pcibrds[] =
117 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C218, MOXA_BOARD_C218_PCI},
118 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C320, MOXA_BOARD_C320_PCI},
119 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP204J, MOXA_BOARD_CP204J},
122 typedef struct _moxa_isa_board_conf {
123 int boardType;
124 int numPorts;
125 unsigned long baseAddr;
126 } moxa_isa_board_conf;
128 static moxa_isa_board_conf moxa_isa_boards[] =
130 /* {MOXA_BOARD_C218_ISA,8,0xDC000}, */
133 typedef struct _moxa_pci_devinfo {
134 ushort busNum;
135 ushort devNum;
136 } moxa_pci_devinfo;
138 typedef struct _moxa_board_conf {
139 int boardType;
140 int numPorts;
141 unsigned long baseAddr;
142 int busType;
143 moxa_pci_devinfo pciInfo;
144 } moxa_board_conf;
146 static moxa_board_conf moxa_boards[MAX_BOARDS];
147 static unsigned long moxaBaseAddr[MAX_BOARDS];
149 struct moxa_str {
150 int type;
151 int port;
152 int close_delay;
153 unsigned short closing_wait;
154 int count;
155 int blocked_open;
156 int event;
157 int asyncflags;
158 long session;
159 long pgrp;
160 unsigned long statusflags;
161 struct tty_struct *tty;
162 struct termios normal_termios;
163 struct termios callout_termios;
164 wait_queue_head_t open_wait;
165 wait_queue_head_t close_wait;
166 struct tq_struct tqueue;
169 struct mxser_mstatus {
170 tcflag_t cflag;
171 int cts;
172 int dsr;
173 int ri;
174 int dcd;
177 static struct mxser_mstatus GMStatus[MAX_PORTS];
179 /* statusflags */
180 #define TXSTOPPED 0x1
181 #define LOWWAIT 0x2
182 #define EMPTYWAIT 0x4
183 #define THROTTLE 0x8
185 /* event */
186 #define MOXA_EVENT_HANGUP 1
188 #define SERIAL_DO_RESTART
191 #define SERIAL_TYPE_NORMAL 1
192 #define SERIAL_TYPE_CALLOUT 2
194 #define WAKEUP_CHARS 256
196 #define PORTNO(x) (MINOR((x)->device) - (x)->driver.minor_start)
198 static int verbose = 0;
199 static int ttymajor = MOXAMAJOR;
200 static int calloutmajor = MOXACUMAJOR;
201 #ifdef MODULE
202 /* Variables for insmod */
203 static int baseaddr[] = {0, 0, 0, 0};
204 static int type[] = {0, 0, 0, 0};
205 static int numports[] = {0, 0, 0, 0};
207 MODULE_AUTHOR("William Chen");
208 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
209 MODULE_PARM(type, "1-4i");
210 MODULE_PARM(baseaddr, "1-4i");
211 MODULE_PARM(numports, "1-4i");
212 MODULE_PARM(ttymajor, "i");
213 MODULE_PARM(calloutmajor, "i");
214 MODULE_PARM(verbose, "i");
216 #endif //MODULE
218 static struct tty_driver moxaDriver;
219 static struct tty_driver moxaCallout;
220 static struct tty_struct *moxaTable[MAX_PORTS + 1];
221 static struct termios *moxaTermios[MAX_PORTS + 1];
222 static struct termios *moxaTermiosLocked[MAX_PORTS + 1];
223 static struct moxa_str moxaChannels[MAX_PORTS];
224 static int moxaRefcount;
225 static unsigned char *moxaXmitBuff;
226 static int moxaTimer_on;
227 static struct timer_list moxaTimer;
228 static int moxaEmptyTimer_on[MAX_PORTS];
229 static struct timer_list moxaEmptyTimer[MAX_PORTS];
230 static struct semaphore moxaBuffSem;
232 int moxa_init(void);
233 #ifdef MODULE
234 int init_module(void);
235 void cleanup_module(void);
236 #endif
238 * static functions:
240 static int moxa_get_PCI_conf(struct pci_dev *, int, moxa_board_conf *);
241 static void do_moxa_softint(void *);
242 static int moxa_open(struct tty_struct *, struct file *);
243 static void moxa_close(struct tty_struct *, struct file *);
244 static int moxa_write(struct tty_struct *, int, const unsigned char *, int);
245 static int moxa_write_room(struct tty_struct *);
246 static void moxa_flush_buffer(struct tty_struct *);
247 static int moxa_chars_in_buffer(struct tty_struct *);
248 static void moxa_flush_chars(struct tty_struct *);
249 static void moxa_put_char(struct tty_struct *, unsigned char);
250 static int moxa_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);
251 static void moxa_throttle(struct tty_struct *);
252 static void moxa_unthrottle(struct tty_struct *);
253 static void moxa_set_termios(struct tty_struct *, struct termios *);
254 static void moxa_stop(struct tty_struct *);
255 static void moxa_start(struct tty_struct *);
256 static void moxa_hangup(struct tty_struct *);
257 static void moxa_poll(unsigned long);
258 static void set_tty_param(struct tty_struct *);
259 static int block_till_ready(struct tty_struct *, struct file *,
260 struct moxa_str *);
261 static void setup_empty_event(struct tty_struct *);
262 static void check_xmit_empty(unsigned long);
263 static void shut_down(struct moxa_str *);
264 static void receive_data(struct moxa_str *);
266 * moxa board interface functions:
268 static void MoxaDriverInit(void);
269 static int MoxaDriverIoctl(unsigned int, unsigned long, int);
270 static int MoxaDriverPoll(void);
271 static int MoxaPortsOfCard(int);
272 static int MoxaPortIsValid(int);
273 static void MoxaPortEnable(int);
274 static void MoxaPortDisable(int);
275 static long MoxaPortGetMaxBaud(int);
276 static long MoxaPortSetBaud(int, long);
277 static int MoxaPortSetTermio(int, struct termios *);
278 static int MoxaPortGetLineOut(int, int *, int *);
279 static void MoxaPortLineCtrl(int, int, int);
280 static void MoxaPortFlowCtrl(int, int, int, int, int, int);
281 static int MoxaPortLineStatus(int);
282 static int MoxaPortDCDChange(int);
283 static int MoxaPortDCDON(int);
284 static void MoxaPortFlushData(int, int);
285 static int MoxaPortWriteData(int, unsigned char *, int);
286 static int MoxaPortReadData(int, unsigned char *, int);
287 static int MoxaPortTxQueue(int);
288 static int MoxaPortRxQueue(int);
289 static int MoxaPortTxFree(int);
290 static void MoxaPortTxDisable(int);
291 static void MoxaPortTxEnable(int);
292 static int MoxaPortResetBrkCnt(int);
293 static void MoxaPortSendBreak(int, int);
294 static int moxa_get_serial_info(struct moxa_str *, struct serial_struct *);
295 static int moxa_set_serial_info(struct moxa_str *, struct serial_struct *);
296 static void MoxaSetFifo(int port, int enable);
298 #ifdef MODULE
299 int init_module(void)
301 int ret;
303 if (verbose)
304 printk("Loading module moxa ...\n");
305 ret = moxa_init();
306 if (verbose)
307 printk("Done\n");
308 return (ret);
311 void cleanup_module(void)
313 int i;
315 if (verbose)
316 printk("Unloading module moxa ...\n");
318 if (moxaTimer_on)
319 del_timer(&moxaTimer);
321 for (i = 0; i < MAX_PORTS; i++)
322 if (moxaEmptyTimer_on[i])
323 del_timer(&moxaEmptyTimer[i]);
325 if (tty_unregister_driver(&moxaCallout))
326 printk("Couldn't unregister MOXA Intellio family callout driver\n");
327 if (tty_unregister_driver(&moxaDriver))
328 printk("Couldn't unregister MOXA Intellio family serial driver\n");
329 if (verbose)
330 printk("Done\n");
333 #endif
335 int moxa_init(void)
337 int i, n, numBoards;
338 struct moxa_str *ch;
339 int ret1, ret2;
341 printk(KERN_INFO "MOXA Intellio family driver version %s\n", MOXA_VERSION);
343 init_MUTEX(&moxaBuffSem);
344 memset(&moxaDriver, 0, sizeof(struct tty_driver));
345 memset(&moxaCallout, 0, sizeof(struct tty_driver));
346 moxaDriver.magic = TTY_DRIVER_MAGIC;
347 moxaDriver.name = "ttya";
348 moxaDriver.major = ttymajor;
349 moxaDriver.minor_start = 0;
350 moxaDriver.num = MAX_PORTS + 1;
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 moxaDriver.refcount = &moxaRefcount;
360 moxaDriver.table = moxaTable;
361 moxaDriver.termios = moxaTermios;
362 moxaDriver.termios_locked = moxaTermiosLocked;
364 moxaDriver.open = moxa_open;
365 moxaDriver.close = moxa_close;
366 moxaDriver.write = moxa_write;
367 moxaDriver.write_room = moxa_write_room;
368 moxaDriver.flush_buffer = moxa_flush_buffer;
369 moxaDriver.chars_in_buffer = moxa_chars_in_buffer;
370 moxaDriver.flush_chars = moxa_flush_chars;
371 moxaDriver.put_char = moxa_put_char;
372 moxaDriver.ioctl = moxa_ioctl;
373 moxaDriver.throttle = moxa_throttle;
374 moxaDriver.unthrottle = moxa_unthrottle;
375 moxaDriver.set_termios = moxa_set_termios;
376 moxaDriver.stop = moxa_stop;
377 moxaDriver.start = moxa_start;
378 moxaDriver.hangup = moxa_hangup;
380 moxaCallout = moxaDriver;
381 moxaCallout.name = "ttyA";
382 moxaCallout.major = calloutmajor;
383 moxaCallout.subtype = SERIAL_TYPE_CALLOUT;
385 moxaXmitBuff = 0;
387 for (i = 0, ch = moxaChannels; i < MAX_PORTS; i++, ch++) {
388 ch->type = PORT_16550A;
389 ch->port = i;
390 ch->tqueue.routine = do_moxa_softint;
391 ch->tqueue.data = ch;
392 ch->tty = 0;
393 ch->close_delay = 5 * HZ / 10;
394 ch->closing_wait = 30 * HZ;
395 ch->count = 0;
396 ch->blocked_open = 0;
397 ch->callout_termios = moxaCallout.init_termios;
398 ch->normal_termios = moxaDriver.init_termios;
399 init_waitqueue_head(&ch->open_wait);
400 init_waitqueue_head(&ch->close_wait);
403 for (i = 0; i < MAX_BOARDS; i++) {
404 moxa_boards[i].boardType = 0;
405 moxa_boards[i].numPorts = 0;
406 moxa_boards[i].baseAddr = 0;
407 moxa_boards[i].busType = 0;
408 moxa_boards[i].pciInfo.busNum = 0;
409 moxa_boards[i].pciInfo.devNum = 0;
411 MoxaDriverInit();
412 printk("Tty devices major number = %d, callout devices major number = %d\n", ttymajor, calloutmajor);
414 ret1 = 0;
415 ret2 = 0;
416 if ((ret1 = tty_register_driver(&moxaDriver))) {
417 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
418 } else if ((ret2 = tty_register_driver(&moxaCallout))) {
419 tty_unregister_driver(&moxaDriver);
420 printk(KERN_ERR "Couldn't install MOXA Smartio family callout driver !\n");
422 if (ret1 || ret2) {
423 return -1;
425 for (i = 0; i < MAX_PORTS; i++) {
426 init_timer(&moxaEmptyTimer[i]);
427 moxaEmptyTimer[i].function = check_xmit_empty;
428 moxaEmptyTimer[i].data = (unsigned long) & moxaChannels[i];
429 moxaEmptyTimer_on[i] = 0;
432 init_timer(&moxaTimer);
433 moxaTimer.function = moxa_poll;
434 moxaTimer.expires = jiffies + (HZ / 50);
435 moxaTimer_on = 1;
436 add_timer(&moxaTimer);
438 /* Find the boards defined in source code */
439 numBoards = 0;
440 for (i = 0; i < MAX_BOARDS; i++) {
441 if ((moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA) ||
442 (moxa_isa_boards[i].boardType == MOXA_BOARD_C320_ISA)) {
443 moxa_boards[numBoards].boardType = moxa_isa_boards[i].boardType;
444 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
445 moxa_boards[numBoards].numPorts = 8;
446 else
447 moxa_boards[numBoards].numPorts = moxa_isa_boards[i].numPorts;
448 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
449 moxa_boards[numBoards].baseAddr = moxa_isa_boards[i].baseAddr;
450 if (verbose)
451 printk("Board %2d: %s board(baseAddr=%lx)\n",
452 numBoards + 1,
453 moxa_brdname[moxa_boards[numBoards].boardType - 1],
454 moxa_boards[numBoards].baseAddr);
455 numBoards++;
458 /* Find the boards defined form module args. */
459 #ifdef MODULE
460 for (i = 0; i < MAX_BOARDS; i++) {
461 if ((type[i] == MOXA_BOARD_C218_ISA) ||
462 (type[i] == MOXA_BOARD_C320_ISA)) {
463 if (verbose)
464 printk("Board %2d: %s board(baseAddr=%lx)\n",
465 numBoards + 1,
466 moxa_brdname[type[i] - 1],
467 (unsigned long) baseaddr[i]);
468 if (numBoards >= MAX_BOARDS) {
469 if (verbose)
470 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
471 continue;
473 moxa_boards[numBoards].boardType = type[i];
474 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
475 moxa_boards[numBoards].numPorts = 8;
476 else
477 moxa_boards[numBoards].numPorts = numports[i];
478 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
479 moxa_boards[numBoards].baseAddr = baseaddr[i];
480 numBoards++;
483 #endif
484 /* Find PCI boards here */
485 #ifdef CONFIG_PCI
486 if (pci_present()) {
487 struct pci_dev *p = NULL;
488 n = sizeof(moxa_pcibrds) / sizeof(moxa_pciinfo);
489 i = 0;
490 while (i < n) {
491 while((p = pci_find_device(moxa_pcibrds[i].vendor_id, moxa_pcibrds[i].device_id, p))!=NULL)
493 if (numBoards >= MAX_BOARDS) {
494 if (verbose)
495 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
496 } else {
497 moxa_get_PCI_conf(p, moxa_pcibrds[i].board_type,
498 &moxa_boards[numBoards]);
499 numBoards++;
502 i++;
505 #endif
506 for (i = 0; i < numBoards; i++) {
507 moxaBaseAddr[i] = (unsigned long) ioremap((unsigned long) moxa_boards[i].baseAddr, 0x4000);
510 return (0);
513 static int moxa_get_PCI_conf(struct pci_dev *p, int board_type, moxa_board_conf * board)
515 unsigned int val;
517 board->baseAddr = p->resource[2].start;
518 board->boardType = board_type;
519 switch (board_type) {
520 case MOXA_BOARD_C218_ISA:
521 case MOXA_BOARD_C218_PCI:
522 board->numPorts = 8;
523 break;
525 case MOXA_BOARD_CP204J:
526 board->numPorts = 4;
527 break;
528 default:
529 board->numPorts = 0;
530 break;
532 board->busType = MOXA_BUS_TYPE_PCI;
533 board->pciInfo.busNum = p->bus->number;
534 board->pciInfo.devNum = p->devfn >> 3;
536 return (0);
539 static void do_moxa_softint(void *private_)
541 struct moxa_str *ch = (struct moxa_str *) private_;
542 struct tty_struct *tty;
544 if (!ch || !(tty = ch->tty))
545 return;
546 if (test_and_clear_bit(MOXA_EVENT_HANGUP, &ch->event)) {
547 tty_hangup(tty);
548 wake_up_interruptible(&ch->open_wait);
549 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CALLOUT_ACTIVE);
553 static int moxa_open(struct tty_struct *tty, struct file *filp)
555 struct moxa_str *ch;
556 int port;
557 int retval;
558 unsigned long page;
560 port = PORTNO(tty);
561 if (port == MAX_PORTS) {
562 MOD_INC_USE_COUNT;
563 return (0);
565 if (!MoxaPortIsValid(port)) {
566 tty->driver_data = NULL;
567 return (-ENODEV);
569 down(&moxaBuffSem);
570 if (!moxaXmitBuff) {
571 page = get_free_page(GFP_KERNEL);
572 if (!page) {
573 up(&moxaBuffSem);
574 return (-ENOMEM);
576 if (moxaXmitBuff)
577 free_page(page);
578 else
579 moxaXmitBuff = (unsigned char *) page;
581 up(&moxaBuffSem);
583 MOD_INC_USE_COUNT;
584 ch = &moxaChannels[port];
585 ch->count++;
586 tty->driver_data = ch;
587 ch->tty = tty;
588 if (ch->count == 1 && (ch->asyncflags & ASYNC_SPLIT_TERMIOS)) {
589 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
590 *tty->termios = ch->normal_termios;
591 else
592 *tty->termios = ch->callout_termios;
594 ch->session = current->session;
595 ch->pgrp = current->pgrp;
596 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
597 ch->statusflags = 0;
598 set_tty_param(tty);
599 MoxaPortLineCtrl(ch->port, 1, 1);
600 MoxaPortEnable(ch->port);
601 ch->asyncflags |= ASYNC_INITIALIZED;
603 retval = block_till_ready(tty, filp, ch);
605 moxa_unthrottle(tty);
607 if (ch->type == PORT_16550A) {
608 MoxaSetFifo(ch->port, 1);
609 } else {
610 MoxaSetFifo(ch->port, 0);
613 return (retval);
616 static void moxa_close(struct tty_struct *tty, struct file *filp)
618 struct moxa_str *ch;
619 int port;
621 port = PORTNO(tty);
622 if (port == MAX_PORTS) {
623 MOD_DEC_USE_COUNT;
624 return;
626 if (!MoxaPortIsValid(port)) {
627 #ifdef SERIAL_DEBUG_CLOSE
628 printk("Invalid portno in moxa_close\n");
629 #endif
630 tty->driver_data = NULL;
631 return;
633 if (tty->driver_data == NULL) {
634 return;
636 if (tty_hung_up_p(filp)) {
637 MOD_DEC_USE_COUNT;
638 return;
640 ch = (struct moxa_str *) tty->driver_data;
642 if ((tty->count == 1) && (ch->count != 1)) {
643 printk("moxa_close: bad serial port count; tty->count is 1, "
644 "ch->count is %d\n", ch->count);
645 ch->count = 1;
647 if (--ch->count < 0) {
648 printk("moxa_close: bad serial port count, minor=%d\n",
649 MINOR(tty->device));
650 ch->count = 0;
652 if (ch->count) {
653 MOD_DEC_USE_COUNT;
654 return;
656 ch->asyncflags |= ASYNC_CLOSING;
659 * Save the termios structure, since this port may have
660 * separate termios for callout and dialin.
662 if (ch->asyncflags & ASYNC_NORMAL_ACTIVE)
663 ch->normal_termios = *tty->termios;
664 if (ch->asyncflags & ASYNC_CALLOUT_ACTIVE)
665 ch->callout_termios = *tty->termios;
666 if (ch->asyncflags & ASYNC_INITIALIZED) {
667 setup_empty_event(tty);
668 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
669 moxaEmptyTimer_on[ch->port] = 0;
670 del_timer(&moxaEmptyTimer[ch->port]);
672 shut_down(ch);
673 MoxaPortFlushData(port, 2);
675 if (tty->driver.flush_buffer)
676 tty->driver.flush_buffer(tty);
677 if (tty->ldisc.flush_buffer)
678 tty->ldisc.flush_buffer(tty);
679 tty->closing = 0;
680 ch->event = 0;
681 ch->tty = 0;
682 if (ch->blocked_open) {
683 if (ch->close_delay) {
684 current->state = TASK_INTERRUPTIBLE;
685 schedule_timeout(ch->close_delay);
687 wake_up_interruptible(&ch->open_wait);
689 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CALLOUT_ACTIVE |
690 ASYNC_CLOSING);
691 wake_up_interruptible(&ch->close_wait);
692 MOD_DEC_USE_COUNT;
695 static int moxa_write(struct tty_struct *tty, int from_user,
696 const unsigned char *buf, int count)
698 struct moxa_str *ch;
699 int len, port;
700 unsigned long flags;
701 unsigned char *temp;
703 ch = (struct moxa_str *) tty->driver_data;
704 if (ch == NULL)
705 return (0);
706 port = ch->port;
707 save_flags(flags);
708 cli();
709 if (from_user) {
710 copy_from_user(moxaXmitBuff, buf, count);
711 temp = moxaXmitBuff;
712 } else
713 temp = (unsigned char *) buf;
714 len = MoxaPortWriteData(port, temp, count);
715 restore_flags(flags);
716 /*********************************************
717 if ( !(ch->statusflags & LOWWAIT) &&
718 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
719 ************************************************/
720 ch->statusflags |= LOWWAIT;
721 return (len);
724 static int moxa_write_room(struct tty_struct *tty)
726 struct moxa_str *ch;
728 if (tty->stopped)
729 return (0);
730 ch = (struct moxa_str *) tty->driver_data;
731 if (ch == NULL)
732 return (0);
733 return (MoxaPortTxFree(ch->port));
736 static void moxa_flush_buffer(struct tty_struct *tty)
738 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
740 if (ch == NULL)
741 return;
742 MoxaPortFlushData(ch->port, 1);
743 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
744 tty->ldisc.write_wakeup)
745 (tty->ldisc.write_wakeup) (tty);
746 wake_up_interruptible(&tty->write_wait);
749 static int moxa_chars_in_buffer(struct tty_struct *tty)
751 int chars;
752 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
755 * Sigh...I have to check if driver_data is NULL here, because
756 * if an open() fails, the TTY subsystem eventually calls
757 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
758 * routine. And since the open() failed, we return 0 here. TDJ
760 if (ch == NULL)
761 return (0);
762 chars = MoxaPortTxQueue(ch->port);
763 if (chars) {
765 * Make it possible to wakeup anything waiting for output
766 * in tty_ioctl.c, etc.
768 if (!(ch->statusflags & EMPTYWAIT))
769 setup_empty_event(tty);
771 return (chars);
774 static void moxa_flush_chars(struct tty_struct *tty)
777 * Don't think I need this, because this is called to empty the TX
778 * buffer for the 16450, 16550, etc.
782 static void moxa_put_char(struct tty_struct *tty, unsigned char c)
784 struct moxa_str *ch;
785 int port;
786 unsigned long flags;
788 ch = (struct moxa_str *) tty->driver_data;
789 if (ch == NULL)
790 return;
791 port = ch->port;
792 save_flags(flags);
793 cli();
794 moxaXmitBuff[0] = c;
795 MoxaPortWriteData(port, moxaXmitBuff, 1);
796 restore_flags(flags);
797 /************************************************
798 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
799 *************************************************/
800 ch->statusflags |= LOWWAIT;
803 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
804 unsigned int cmd, unsigned long arg)
806 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
807 register int port;
808 int retval, dtr, rts;
809 unsigned long flag;
811 port = PORTNO(tty);
812 if ((port != MAX_PORTS) && (!ch))
813 return (-EINVAL);
815 switch (cmd) {
816 case TCSBRK: /* SVID version: non-zero arg --> no break */
817 retval = tty_check_change(tty);
818 if (retval)
819 return (retval);
820 setup_empty_event(tty);
821 tty_wait_until_sent(tty, 0);
822 if (!arg)
823 MoxaPortSendBreak(ch->port, 0);
824 return (0);
825 case TCSBRKP: /* support for POSIX tcsendbreak() */
826 retval = tty_check_change(tty);
827 if (retval)
828 return (retval);
829 setup_empty_event(tty);
830 tty_wait_until_sent(tty, 0);
831 MoxaPortSendBreak(ch->port, arg);
832 return (0);
833 case TIOCGSOFTCAR:
834 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long *) arg);
835 case TIOCSSOFTCAR:
836 if(get_user(retval, (unsigned long *) arg))
837 return -EFAULT;
838 arg = retval;
839 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
840 (arg ? CLOCAL : 0));
841 if (C_CLOCAL(tty))
842 ch->asyncflags &= ~ASYNC_CHECK_CD;
843 else
844 ch->asyncflags |= ASYNC_CHECK_CD;
845 return (0);
846 case TIOCMGET:
847 flag = 0;
848 MoxaPortGetLineOut(ch->port, &dtr, &rts);
849 if (dtr)
850 flag |= TIOCM_DTR;
851 if (rts)
852 flag |= TIOCM_RTS;
853 dtr = MoxaPortLineStatus(ch->port);
854 if (dtr & 1)
855 flag |= TIOCM_CTS;
856 if (dtr & 2)
857 flag |= TIOCM_DSR;
858 if (dtr & 4)
859 flag |= TIOCM_CD;
860 return put_user(flag, (unsigned int *) arg);
861 case TIOCMBIS:
862 if(get_user(retval, (unsigned int *) arg))
863 return -EFAULT;
864 MoxaPortGetLineOut(ch->port, &dtr, &rts);
865 if (retval & TIOCM_RTS)
866 rts = 1;
867 if (retval & TIOCM_DTR)
868 dtr = 1;
869 MoxaPortLineCtrl(ch->port, dtr, rts);
870 return (0);
871 case TIOCMBIC:
872 if(get_user(retval, (unsigned int *) arg))
873 return -EFAULT;
874 MoxaPortGetLineOut(ch->port, &dtr, &rts);
875 if (retval & TIOCM_RTS)
876 rts = 0;
877 if (retval & TIOCM_DTR)
878 dtr = 0;
879 MoxaPortLineCtrl(ch->port, dtr, rts);
880 return (0);
881 case TIOCMSET:
882 if(get_user(retval, (unsigned long *) arg))
883 return -EFAULT;
884 dtr = rts = 0;
885 if (retval & TIOCM_RTS)
886 rts = 1;
887 if (retval & TIOCM_DTR)
888 dtr = 1;
889 MoxaPortLineCtrl(ch->port, dtr, rts);
890 return (0);
891 case TIOCGSERIAL:
892 return (moxa_get_serial_info(ch, (struct serial_struct *) arg));
894 case TIOCSSERIAL:
895 return (moxa_set_serial_info(ch, (struct serial_struct *) arg));
896 default:
897 retval = MoxaDriverIoctl(cmd, arg, port);
899 return (retval);
902 static void moxa_throttle(struct tty_struct *tty)
904 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
906 ch->statusflags |= THROTTLE;
909 static void moxa_unthrottle(struct tty_struct *tty)
911 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
913 ch->statusflags &= ~THROTTLE;
916 static void moxa_set_termios(struct tty_struct *tty,
917 struct termios *old_termios)
919 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
921 if (ch == NULL)
922 return;
923 set_tty_param(tty);
924 if (!(old_termios->c_cflag & CLOCAL) &&
925 (tty->termios->c_cflag & CLOCAL))
926 wake_up_interruptible(&ch->open_wait);
929 static void moxa_stop(struct tty_struct *tty)
931 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
933 if (ch == NULL)
934 return;
935 MoxaPortTxDisable(ch->port);
936 ch->statusflags |= TXSTOPPED;
940 static void moxa_start(struct tty_struct *tty)
942 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
944 if (ch == NULL)
945 return;
947 if (!(ch->statusflags & TXSTOPPED))
948 return;
950 MoxaPortTxEnable(ch->port);
951 ch->statusflags &= ~TXSTOPPED;
954 static void moxa_hangup(struct tty_struct *tty)
956 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
958 moxa_flush_buffer(tty);
959 shut_down(ch);
960 ch->event = 0;
961 ch->count = 0;
962 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CALLOUT_ACTIVE);
963 ch->tty = 0;
964 wake_up_interruptible(&ch->open_wait);
967 static void moxa_poll(unsigned long ignored)
969 register int card;
970 struct moxa_str *ch;
971 struct tty_struct *tp;
972 int i, ports;
974 moxaTimer_on = 0;
975 del_timer(&moxaTimer);
977 if (MoxaDriverPoll() < 0) {
978 moxaTimer.function = moxa_poll;
979 moxaTimer.expires = jiffies + (HZ / 50);
980 moxaTimer_on = 1;
981 add_timer(&moxaTimer);
982 return;
984 for (card = 0; card < MAX_BOARDS; card++) {
985 if ((ports = MoxaPortsOfCard(card)) <= 0)
986 continue;
987 ch = &moxaChannels[card * MAX_PORTS_PER_BOARD];
988 for (i = 0; i < ports; i++, ch++) {
989 if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
990 continue;
991 if (!(ch->statusflags & THROTTLE) &&
992 (MoxaPortRxQueue(ch->port) > 0))
993 receive_data(ch);
994 if ((tp = ch->tty) == 0)
995 continue;
996 if (ch->statusflags & LOWWAIT) {
997 if (MoxaPortTxQueue(ch->port) <= WAKEUP_CHARS) {
998 if (!tp->stopped) {
999 ch->statusflags &= ~LOWWAIT;
1000 if ((tp->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1001 tp->ldisc.write_wakeup)
1002 (tp->ldisc.write_wakeup) (tp);
1003 wake_up_interruptible(&tp->write_wait);
1007 if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch->port) > 0)) {
1008 tty_insert_flip_char(tp, 0, TTY_BREAK);
1009 tty_schedule_flip(tp);
1011 if (MoxaPortDCDChange(ch->port)) {
1012 if (ch->asyncflags & ASYNC_CHECK_CD) {
1013 if (MoxaPortDCDON(ch->port))
1014 wake_up_interruptible(&ch->open_wait);
1015 else {
1016 set_bit(MOXA_EVENT_HANGUP, &ch->event);
1017 queue_task(&ch->tqueue, &tq_scheduler);
1024 moxaTimer.function = moxa_poll;
1025 moxaTimer.expires = jiffies + (HZ / 50);
1026 moxaTimer_on = 1;
1027 add_timer(&moxaTimer);
1030 /******************************************************************************/
1032 static void set_tty_param(struct tty_struct *tty)
1034 register struct termios *ts;
1035 struct moxa_str *ch;
1036 int rts, cts, txflow, rxflow, xany;
1038 ch = (struct moxa_str *) tty->driver_data;
1039 ts = tty->termios;
1040 if (ts->c_cflag & CLOCAL)
1041 ch->asyncflags &= ~ASYNC_CHECK_CD;
1042 else
1043 ch->asyncflags |= ASYNC_CHECK_CD;
1044 rts = cts = txflow = rxflow = xany = 0;
1045 if (ts->c_cflag & CRTSCTS)
1046 rts = cts = 1;
1047 if (ts->c_iflag & IXON)
1048 txflow = 1;
1049 if (ts->c_iflag & IXOFF)
1050 rxflow = 1;
1051 if (ts->c_iflag & IXANY)
1052 xany = 1;
1053 MoxaPortFlowCtrl(ch->port, rts, cts, txflow, rxflow, xany);
1054 MoxaPortSetTermio(ch->port, ts);
1057 static int block_till_ready(struct tty_struct *tty, struct file *filp,
1058 struct moxa_str *ch)
1060 DECLARE_WAITQUEUE(wait,current);
1061 unsigned long flags;
1062 int retval;
1063 int do_clocal = C_CLOCAL(tty);
1066 * If the device is in the middle of being closed, then block
1067 * until it's done, and then try again.
1069 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
1070 if (ch->asyncflags & ASYNC_CLOSING)
1071 interruptible_sleep_on(&ch->close_wait);
1072 #ifdef SERIAL_DO_RESTART
1073 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1074 return (-EAGAIN);
1075 else
1076 return (-ERESTARTSYS);
1077 #else
1078 return (-EAGAIN);
1079 #endif
1082 * If this is a callout device, then just make sure the normal
1083 * device isn't being used.
1085 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1086 if (ch->asyncflags & ASYNC_NORMAL_ACTIVE)
1087 return (-EBUSY);
1088 if ((ch->asyncflags & ASYNC_CALLOUT_ACTIVE) &&
1089 (ch->asyncflags & ASYNC_SESSION_LOCKOUT) &&
1090 (ch->session != current->session))
1091 return (-EBUSY);
1092 if ((ch->asyncflags & ASYNC_CALLOUT_ACTIVE) &&
1093 (ch->asyncflags & ASYNC_PGRP_LOCKOUT) &&
1094 (ch->pgrp != current->pgrp))
1095 return (-EBUSY);
1096 ch->asyncflags |= ASYNC_CALLOUT_ACTIVE;
1097 return (0);
1100 * If non-blocking mode is set, then make the check up front
1101 * and then exit.
1103 if (filp->f_flags & O_NONBLOCK) {
1104 if (ch->asyncflags & ASYNC_CALLOUT_ACTIVE)
1105 return (-EBUSY);
1106 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1107 return (0);
1110 * Block waiting for the carrier detect and the line to become free
1112 retval = 0;
1113 add_wait_queue(&ch->open_wait, &wait);
1114 #ifdef SERIAL_DEBUG_OPEN
1115 printk("block_til_ready before block: ttys%d, count = %d\n",
1116 ch->line, ch->count);
1117 #endif
1118 save_flags(flags);
1119 cli();
1120 if (!tty_hung_up_p(filp))
1121 ch->count--;
1122 restore_flags(flags);
1123 ch->blocked_open++;
1124 while (1) {
1125 current->state = TASK_INTERRUPTIBLE;
1126 if (tty_hung_up_p(filp) ||
1127 !(ch->asyncflags & ASYNC_INITIALIZED)) {
1128 #ifdef SERIAL_DO_RESTART
1129 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1130 retval = -EAGAIN;
1131 else
1132 retval = -ERESTARTSYS;
1133 #else
1134 retval = -EAGAIN;
1135 #endif
1136 break;
1138 if (!(ch->asyncflags & ASYNC_CALLOUT_ACTIVE) &&
1139 !(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
1140 MoxaPortDCDON(ch->port)))
1141 break;
1143 if (signal_pending(current)) {
1144 retval = -ERESTARTSYS;
1145 break;
1147 schedule();
1149 current->state = TASK_RUNNING;
1150 remove_wait_queue(&ch->open_wait, &wait);
1151 if (!tty_hung_up_p(filp))
1152 ch->count++;
1153 ch->blocked_open--;
1154 #ifdef SERIAL_DEBUG_OPEN
1155 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1156 ch->line, ch->count);
1157 #endif
1158 if (retval)
1159 return (retval);
1160 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1161 return (0);
1164 static void setup_empty_event(struct tty_struct *tty)
1166 struct moxa_str *ch = tty->driver_data;
1167 unsigned long flags;
1169 save_flags(flags);
1170 cli();
1171 ch->statusflags |= EMPTYWAIT;
1172 moxaEmptyTimer_on[ch->port] = 0;
1173 del_timer(&moxaEmptyTimer[ch->port]);
1174 moxaEmptyTimer[ch->port].expires = jiffies + HZ;
1175 moxaEmptyTimer_on[ch->port] = 1;
1176 add_timer(&moxaEmptyTimer[ch->port]);
1177 restore_flags(flags);
1180 static void check_xmit_empty(unsigned long data)
1182 struct moxa_str *ch;
1184 ch = (struct moxa_str *) data;
1185 moxaEmptyTimer_on[ch->port] = 0;
1186 del_timer(&moxaEmptyTimer[ch->port]);
1187 if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
1188 if (MoxaPortTxQueue(ch->port) == 0) {
1189 ch->statusflags &= ~EMPTYWAIT;
1190 if ((ch->tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1191 ch->tty->ldisc.write_wakeup)
1192 (ch->tty->ldisc.write_wakeup) (ch->tty);
1193 wake_up_interruptible(&ch->tty->write_wait);
1194 return;
1196 moxaEmptyTimer[ch->port].expires = jiffies + HZ;
1197 moxaEmptyTimer_on[ch->port] = 1;
1198 add_timer(&moxaEmptyTimer[ch->port]);
1199 } else
1200 ch->statusflags &= ~EMPTYWAIT;
1203 static void shut_down(struct moxa_str *ch)
1205 struct tty_struct *tp;
1207 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1208 return;
1210 tp = ch->tty;
1212 MoxaPortDisable(ch->port);
1215 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1217 if (tp->termios->c_cflag & HUPCL)
1218 MoxaPortLineCtrl(ch->port, 0, 0);
1220 ch->asyncflags &= ~ASYNC_INITIALIZED;
1223 static void receive_data(struct moxa_str *ch)
1225 struct tty_struct *tp;
1226 struct termios *ts;
1227 int i, count, rc, space;
1228 unsigned char *charptr, *flagptr;
1229 unsigned long flags;
1231 ts = 0;
1232 tp = ch->tty;
1233 if (tp)
1234 ts = tp->termios;
1235 /**************************************************
1236 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1237 *****************************************************/
1238 if (!tp || !ts) {
1239 MoxaPortFlushData(ch->port, 0);
1240 return;
1242 space = TTY_FLIPBUF_SIZE - tp->flip.count;
1243 if (space <= 0)
1244 return;
1245 charptr = tp->flip.char_buf_ptr;
1246 flagptr = tp->flip.flag_buf_ptr;
1247 rc = tp->flip.count;
1248 save_flags(flags);
1249 cli();
1250 count = MoxaPortReadData(ch->port, charptr, space);
1251 restore_flags(flags);
1252 for (i = 0; i < count; i++)
1253 *flagptr++ = 0;
1254 charptr += count;
1255 rc += count;
1256 tp->flip.count = rc;
1257 tp->flip.char_buf_ptr = charptr;
1258 tp->flip.flag_buf_ptr = flagptr;
1259 tty_schedule_flip(ch->tty);
1262 #define Magic_code 0x404
1265 * System Configuration
1268 * for C218 BIOS initialization
1270 #define C218_ConfBase 0x800
1271 #define C218_status (C218_ConfBase + 0) /* BIOS running status */
1272 #define C218_diag (C218_ConfBase + 2) /* diagnostic status */
1273 #define C218_key (C218_ConfBase + 4) /* WORD (0x218 for C218) */
1274 #define C218DLoad_len (C218_ConfBase + 6) /* WORD */
1275 #define C218check_sum (C218_ConfBase + 8) /* BYTE */
1276 #define C218chksum_ok (C218_ConfBase + 0x0a) /* BYTE (1:ok) */
1277 #define C218_TestRx (C218_ConfBase + 0x10) /* 8 bytes for 8 ports */
1278 #define C218_TestTx (C218_ConfBase + 0x18) /* 8 bytes for 8 ports */
1279 #define C218_RXerr (C218_ConfBase + 0x20) /* 8 bytes for 8 ports */
1280 #define C218_ErrFlag (C218_ConfBase + 0x28) /* 8 bytes for 8 ports */
1282 #define C218_LoadBuf 0x0F00
1283 #define C218_KeyCode 0x218
1284 #define CP204J_KeyCode 0x204
1287 * for C320 BIOS initialization
1289 #define C320_ConfBase 0x800
1290 #define C320_LoadBuf 0x0f00
1291 #define STS_init 0x05 /* for C320_status */
1293 #define C320_status C320_ConfBase + 0 /* BIOS running status */
1294 #define C320_diag C320_ConfBase + 2 /* diagnostic status */
1295 #define C320_key C320_ConfBase + 4 /* WORD (0320H for C320) */
1296 #define C320DLoad_len C320_ConfBase + 6 /* WORD */
1297 #define C320check_sum C320_ConfBase + 8 /* WORD */
1298 #define C320chksum_ok C320_ConfBase + 0x0a /* WORD (1:ok) */
1299 #define C320bapi_len C320_ConfBase + 0x0c /* WORD */
1300 #define C320UART_no C320_ConfBase + 0x0e /* WORD */
1302 #define C320_KeyCode 0x320
1304 #define FixPage_addr 0x0000 /* starting addr of static page */
1305 #define DynPage_addr 0x2000 /* starting addr of dynamic page */
1306 #define C218_start 0x3000 /* starting addr of C218 BIOS prg */
1307 #define Control_reg 0x1ff0 /* select page and reset control */
1308 #define HW_reset 0x80
1311 * Function Codes
1313 #define FC_CardReset 0x80
1314 #define FC_ChannelReset 1 /* C320 firmware not supported */
1315 #define FC_EnableCH 2
1316 #define FC_DisableCH 3
1317 #define FC_SetParam 4
1318 #define FC_SetMode 5
1319 #define FC_SetRate 6
1320 #define FC_LineControl 7
1321 #define FC_LineStatus 8
1322 #define FC_XmitControl 9
1323 #define FC_FlushQueue 10
1324 #define FC_SendBreak 11
1325 #define FC_StopBreak 12
1326 #define FC_LoopbackON 13
1327 #define FC_LoopbackOFF 14
1328 #define FC_ClrIrqTable 15
1329 #define FC_SendXon 16
1330 #define FC_SetTermIrq 17 /* C320 firmware not supported */
1331 #define FC_SetCntIrq 18 /* C320 firmware not supported */
1332 #define FC_SetBreakIrq 19
1333 #define FC_SetLineIrq 20
1334 #define FC_SetFlowCtl 21
1335 #define FC_GenIrq 22
1336 #define FC_InCD180 23
1337 #define FC_OutCD180 24
1338 #define FC_InUARTreg 23
1339 #define FC_OutUARTreg 24
1340 #define FC_SetXonXoff 25
1341 #define FC_OutCD180CCR 26
1342 #define FC_ExtIQueue 27
1343 #define FC_ExtOQueue 28
1344 #define FC_ClrLineIrq 29
1345 #define FC_HWFlowCtl 30
1346 #define FC_GetClockRate 35
1347 #define FC_SetBaud 36
1348 #define FC_SetDataMode 41
1349 #define FC_GetCCSR 43
1350 #define FC_GetDataError 45
1351 #define FC_RxControl 50
1352 #define FC_ImmSend 51
1353 #define FC_SetXonState 52
1354 #define FC_SetXoffState 53
1355 #define FC_SetRxFIFOTrig 54
1356 #define FC_SetTxFIFOCnt 55
1357 #define FC_UnixRate 56
1358 #define FC_UnixResetTimer 57
1360 #define RxFIFOTrig1 0
1361 #define RxFIFOTrig4 1
1362 #define RxFIFOTrig8 2
1363 #define RxFIFOTrig14 3
1366 * Dual-Ported RAM
1368 #define DRAM_global 0
1369 #define INT_data (DRAM_global + 0)
1370 #define Config_base (DRAM_global + 0x108)
1372 #define IRQindex (INT_data + 0)
1373 #define IRQpending (INT_data + 4)
1374 #define IRQtable (INT_data + 8)
1377 * Interrupt Status
1379 #define IntrRx 0x01 /* receiver data O.K. */
1380 #define IntrTx 0x02 /* transmit buffer empty */
1381 #define IntrFunc 0x04 /* function complete */
1382 #define IntrBreak 0x08 /* received break */
1383 #define IntrLine 0x10 /* line status change
1384 for transmitter */
1385 #define IntrIntr 0x20 /* received INTR code */
1386 #define IntrQuit 0x40 /* received QUIT code */
1387 #define IntrEOF 0x80 /* received EOF code */
1389 #define IntrRxTrigger 0x100 /* rx data count reach tigger value */
1390 #define IntrTxTrigger 0x200 /* tx data count below trigger value */
1392 #define Magic_no (Config_base + 0)
1393 #define Card_model_no (Config_base + 2)
1394 #define Total_ports (Config_base + 4)
1395 #define Module_cnt (Config_base + 8)
1396 #define Module_no (Config_base + 10)
1397 #define Timer_10ms (Config_base + 14)
1398 #define Disable_IRQ (Config_base + 20)
1399 #define TMS320_PORT1 (Config_base + 22)
1400 #define TMS320_PORT2 (Config_base + 24)
1401 #define TMS320_CLOCK (Config_base + 26)
1404 * DATA BUFFER in DRAM
1406 #define Extern_table 0x400 /* Base address of the external table
1407 (24 words * 64) total 3K bytes
1408 (24 words * 128) total 6K bytes */
1409 #define Extern_size 0x60 /* 96 bytes */
1410 #define RXrptr 0x00 /* read pointer for RX buffer */
1411 #define RXwptr 0x02 /* write pointer for RX buffer */
1412 #define TXrptr 0x04 /* read pointer for TX buffer */
1413 #define TXwptr 0x06 /* write pointer for TX buffer */
1414 #define HostStat 0x08 /* IRQ flag and general flag */
1415 #define FlagStat 0x0A
1416 #define FlowControl 0x0C /* B7 B6 B5 B4 B3 B2 B1 B0 */
1417 /* x x x x | | | | */
1418 /* | | | + CTS flow */
1419 /* | | +--- RTS flow */
1420 /* | +------ TX Xon/Xoff */
1421 /* +--------- RX Xon/Xoff */
1422 #define Break_cnt 0x0E /* received break count */
1423 #define CD180TXirq 0x10 /* if non-0: enable TX irq */
1424 #define RX_mask 0x12
1425 #define TX_mask 0x14
1426 #define Ofs_rxb 0x16
1427 #define Ofs_txb 0x18
1428 #define Page_rxb 0x1A
1429 #define Page_txb 0x1C
1430 #define EndPage_rxb 0x1E
1431 #define EndPage_txb 0x20
1432 #define Data_error 0x22
1433 #define RxTrigger 0x28
1434 #define TxTrigger 0x2a
1436 #define rRXwptr 0x34
1437 #define Low_water 0x36
1439 #define FuncCode 0x40
1440 #define FuncArg 0x42
1441 #define FuncArg1 0x44
1443 #define C218rx_size 0x2000 /* 8K bytes */
1444 #define C218tx_size 0x8000 /* 32K bytes */
1446 #define C218rx_mask (C218rx_size - 1)
1447 #define C218tx_mask (C218tx_size - 1)
1449 #define C320p8rx_size 0x2000
1450 #define C320p8tx_size 0x8000
1451 #define C320p8rx_mask (C320p8rx_size - 1)
1452 #define C320p8tx_mask (C320p8tx_size - 1)
1454 #define C320p16rx_size 0x2000
1455 #define C320p16tx_size 0x4000
1456 #define C320p16rx_mask (C320p16rx_size - 1)
1457 #define C320p16tx_mask (C320p16tx_size - 1)
1459 #define C320p24rx_size 0x2000
1460 #define C320p24tx_size 0x2000
1461 #define C320p24rx_mask (C320p24rx_size - 1)
1462 #define C320p24tx_mask (C320p24tx_size - 1)
1464 #define C320p32rx_size 0x1000
1465 #define C320p32tx_size 0x1000
1466 #define C320p32rx_mask (C320p32rx_size - 1)
1467 #define C320p32tx_mask (C320p32tx_size - 1)
1469 #define Page_size 0x2000
1470 #define Page_mask (Page_size - 1)
1471 #define C218rx_spage 3
1472 #define C218tx_spage 4
1473 #define C218rx_pageno 1
1474 #define C218tx_pageno 4
1475 #define C218buf_pageno 5
1477 #define C320p8rx_spage 3
1478 #define C320p8tx_spage 4
1479 #define C320p8rx_pgno 1
1480 #define C320p8tx_pgno 4
1481 #define C320p8buf_pgno 5
1483 #define C320p16rx_spage 3
1484 #define C320p16tx_spage 4
1485 #define C320p16rx_pgno 1
1486 #define C320p16tx_pgno 2
1487 #define C320p16buf_pgno 3
1489 #define C320p24rx_spage 3
1490 #define C320p24tx_spage 4
1491 #define C320p24rx_pgno 1
1492 #define C320p24tx_pgno 1
1493 #define C320p24buf_pgno 2
1495 #define C320p32rx_spage 3
1496 #define C320p32tx_ofs C320p32rx_size
1497 #define C320p32tx_spage 3
1498 #define C320p32buf_pgno 1
1501 * Host Status
1503 #define WakeupRx 0x01
1504 #define WakeupTx 0x02
1505 #define WakeupBreak 0x08
1506 #define WakeupLine 0x10
1507 #define WakeupIntr 0x20
1508 #define WakeupQuit 0x40
1509 #define WakeupEOF 0x80 /* used in VTIME control */
1510 #define WakeupRxTrigger 0x100
1511 #define WakeupTxTrigger 0x200
1513 * Flag status
1515 #define Rx_over 0x01
1516 #define Xoff_state 0x02
1517 #define Tx_flowOff 0x04
1518 #define Tx_enable 0x08
1519 #define CTS_state 0x10
1520 #define DSR_state 0x20
1521 #define DCD_state 0x80
1523 * FlowControl
1525 #define CTS_FlowCtl 1
1526 #define RTS_FlowCtl 2
1527 #define Tx_FlowCtl 4
1528 #define Rx_FlowCtl 8
1529 #define IXM_IXANY 0x10
1531 #define LowWater 128
1533 #define DTR_ON 1
1534 #define RTS_ON 2
1535 #define CTS_ON 1
1536 #define DSR_ON 2
1537 #define DCD_ON 8
1539 /* mode definition */
1540 #define MX_CS8 0x03
1541 #define MX_CS7 0x02
1542 #define MX_CS6 0x01
1543 #define MX_CS5 0x00
1545 #define MX_STOP1 0x00
1546 #define MX_STOP15 0x04
1547 #define MX_STOP2 0x08
1549 #define MX_PARNONE 0x00
1550 #define MX_PAREVEN 0x40
1551 #define MX_PARODD 0xC0
1554 * Query
1556 #define QueryPort MAX_PORTS
1560 struct mon_str {
1561 int tick;
1562 int rxcnt[MAX_PORTS];
1563 int txcnt[MAX_PORTS];
1565 typedef struct mon_str mon_st;
1567 #define DCD_changed 0x01
1568 #define DCD_oldstate 0x80
1570 static unsigned char moxaBuff[10240];
1571 static unsigned long moxaIntNdx[MAX_BOARDS];
1572 static unsigned long moxaIntPend[MAX_BOARDS];
1573 static unsigned long moxaIntTable[MAX_BOARDS];
1574 static char moxaChkPort[MAX_PORTS];
1575 static char moxaLineCtrl[MAX_PORTS];
1576 static unsigned long moxaTableAddr[MAX_PORTS];
1577 static long moxaCurBaud[MAX_PORTS];
1578 static char moxaDCDState[MAX_PORTS];
1579 static char moxaLowChkFlag[MAX_PORTS];
1580 static int moxaLowWaterChk;
1581 static int moxaCard;
1582 static mon_st moxaLog;
1583 static int moxaFuncTout;
1584 static ushort moxaBreakCnt[MAX_PORTS];
1586 static void moxadelay(int);
1587 static void moxafunc(unsigned long, int, ushort);
1588 static void wait_finish(unsigned long);
1589 static void low_water_check(unsigned long);
1590 static int moxaloadbios(int, unsigned char *, int);
1591 static int moxafindcard(int);
1592 static int moxaload320b(int, unsigned char *, int);
1593 static int moxaloadcode(int, unsigned char *, int);
1594 static int moxaloadc218(int, unsigned long, int);
1595 static int moxaloadc320(int, unsigned long, int, int *);
1597 /*****************************************************************************
1598 * Driver level functions: *
1599 * 1. MoxaDriverInit(void); *
1600 * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
1601 * 3. MoxaDriverPoll(void); *
1602 *****************************************************************************/
1603 void MoxaDriverInit(void)
1605 int i;
1607 moxaFuncTout = HZ / 2; /* 500 mini-seconds */
1608 moxaCard = 0;
1609 moxaLog.tick = 0;
1610 moxaLowWaterChk = 0;
1611 for (i = 0; i < MAX_PORTS; i++) {
1612 moxaChkPort[i] = 0;
1613 moxaLowChkFlag[i] = 0;
1614 moxaLineCtrl[i] = 0;
1615 moxaLog.rxcnt[i] = 0;
1616 moxaLog.txcnt[i] = 0;
1620 #define MOXA 0x400
1621 #define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1622 #define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
1623 #define MOXA_INIT_DRIVER (MOXA + 6) /* moxaCard=0 */
1624 #define MOXA_LOAD_BIOS (MOXA + 9) /* download BIOS */
1625 #define MOXA_FIND_BOARD (MOXA + 10) /* Check if MOXA card exist? */
1626 #define MOXA_LOAD_C320B (MOXA + 11) /* download 320B firmware */
1627 #define MOXA_LOAD_CODE (MOXA + 12) /* download firmware */
1628 #define MOXA_GETDATACOUNT (MOXA + 23)
1629 #define MOXA_GET_IOQUEUE (MOXA + 27)
1630 #define MOXA_FLUSH_QUEUE (MOXA + 28)
1631 #define MOXA_GET_CONF (MOXA + 35) /* configuration */
1632 #define MOXA_GET_MAJOR (MOXA + 63)
1633 #define MOXA_GET_CUMAJOR (MOXA + 64)
1634 #define MOXA_GETMSTATUS (MOXA + 65)
1637 struct moxaq_str {
1638 int inq;
1639 int outq;
1642 struct dl_str {
1643 char *buf;
1644 int len;
1645 int cardno;
1648 static struct moxaq_str temp_queue[MAX_PORTS];
1649 static struct dl_str dltmp;
1651 void MoxaPortFlushData(int port, int mode)
1653 unsigned long ofsAddr;
1654 if ((mode < 0) || (mode > 2))
1655 return;
1656 ofsAddr = moxaTableAddr[port];
1657 moxafunc(ofsAddr, FC_FlushQueue, mode);
1658 if (mode != 1) {
1659 moxaLowChkFlag[port] = 0;
1660 low_water_check(ofsAddr);
1664 int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port)
1666 int i;
1667 int status;
1668 int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
1670 if (port == QueryPort) {
1671 if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_INIT_DRIVER) &&
1672 (cmd != MOXA_LOAD_BIOS) && (cmd != MOXA_FIND_BOARD) && (cmd != MOXA_LOAD_C320B) &&
1673 (cmd != MOXA_LOAD_CODE) && (cmd != MOXA_GETDATACOUNT) &&
1674 (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
1675 (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
1676 return (-EINVAL);
1678 switch (cmd) {
1679 case MOXA_GET_CONF:
1680 if(copy_to_user((void *)arg, &moxa_boards, MAX_BOARDS * sizeof(moxa_board_conf)))
1681 return -EFAULT;
1682 return (0);
1683 case MOXA_INIT_DRIVER:
1684 if ((int) arg == 0x404)
1685 MoxaDriverInit();
1686 return (0);
1687 case MOXA_GETDATACOUNT:
1688 moxaLog.tick = jiffies;
1689 if(copy_to_user((void *)arg, &moxaLog, sizeof(mon_st)))
1690 return -EFAULT;
1691 return (0);
1692 case MOXA_FLUSH_QUEUE:
1693 MoxaPortFlushData(port, arg);
1694 return (0);
1695 case MOXA_GET_IOQUEUE:
1696 for (i = 0; i < MAX_PORTS; i++) {
1697 if (moxaChkPort[i]) {
1698 temp_queue[i].inq = MoxaPortRxQueue(i);
1699 temp_queue[i].outq = MoxaPortTxQueue(i);
1702 if(copy_to_user((void *)arg, temp_queue, sizeof(struct moxaq_str) * MAX_PORTS))
1703 return -EFAULT;
1704 return (0);
1705 case MOXA_LOAD_BIOS:
1706 if(copy_from_user(&dltmp, (void *)arg, sizeof(struct dl_str)))
1707 return -EFAULT;
1708 i = moxaloadbios(dltmp.cardno, dltmp.buf, dltmp.len);
1709 return (i);
1710 case MOXA_FIND_BOARD:
1711 if(copy_from_user(&dltmp, (void *)arg, sizeof(struct dl_str)))
1712 return -EFAULT;
1713 return moxafindcard(dltmp.cardno);
1714 case MOXA_LOAD_C320B:
1715 if(copy_from_user(&dltmp, (void *)arg, sizeof(struct dl_str)))
1716 return -EFAULT;
1717 moxaload320b(dltmp.cardno, dltmp.buf, dltmp.len);
1718 return (0);
1719 case MOXA_LOAD_CODE:
1720 if(copy_from_user(&dltmp, (void *)arg, sizeof(struct dl_str)))
1721 return -EFAULT;
1722 i = moxaloadcode(dltmp.cardno, dltmp.buf, dltmp.len);
1723 if (i == -1)
1724 return (-EFAULT);
1725 return (i);
1726 case MOXA_GET_OQUEUE:
1727 i = MoxaPortTxQueue(port);
1728 return put_user(i, (unsigned long *) arg);
1729 case MOXA_GET_IQUEUE:
1730 i = MoxaPortRxQueue(port);
1731 return put_user(i, (unsigned long *) arg);
1732 case MOXA_GET_MAJOR:
1733 if(copy_to_user((void *)arg, &ttymajor, sizeof(int)))
1734 return -EFAULT;
1735 return 0;
1736 case MOXA_GET_CUMAJOR:
1737 if(copy_to_user((void *)arg, &calloutmajor, sizeof(int)))
1738 return -EFAULT;
1739 return 0;
1740 case MOXA_GETMSTATUS:
1741 for (i = 0; i < MAX_PORTS; i++) {
1742 GMStatus[i].ri = 0;
1743 GMStatus[i].dcd = 0;
1744 GMStatus[i].dsr = 0;
1745 GMStatus[i].cts = 0;
1746 if (!moxaChkPort[i]) {
1747 continue;
1748 } else {
1749 status = MoxaPortLineStatus(moxaChannels[i].port);
1750 if (status & 1)
1751 GMStatus[i].cts = 1;
1752 if (status & 2)
1753 GMStatus[i].dsr = 1;
1754 if (status & 4)
1755 GMStatus[i].dcd = 1;
1758 if (!moxaChannels[i].tty || !moxaChannels[i].tty->termios)
1759 GMStatus[i].cflag = moxaChannels[i].normal_termios.c_cflag;
1760 else
1761 GMStatus[i].cflag = moxaChannels[i].tty->termios->c_cflag;
1763 if(copy_to_user((void *)arg, GMStatus, sizeof(struct mxser_mstatus) * MAX_PORTS))
1764 return -EFAULT;
1765 return 0;
1768 return (-ENOIOCTLCMD);
1771 int MoxaDriverPoll(void)
1773 register ushort temp;
1774 register int card;
1775 unsigned long ip, ofsAddr;
1776 int port, p, ports;
1778 if (moxaCard == 0)
1779 return (-1);
1780 for (card = 0; card < MAX_BOARDS; card++) {
1781 if ((ports = moxa_boards[card].numPorts) == 0)
1782 continue;
1783 if (readb(moxaIntPend[card]) == 0xff) {
1784 ip = moxaIntTable[card] + readb(moxaIntNdx[card]);
1785 p = card * MAX_PORTS_PER_BOARD;
1786 ports <<= 1;
1787 for (port = 0; port < ports; port += 2, p++) {
1788 if ((temp = readw(ip + port)) != 0) {
1789 writew(0, ip + port);
1790 ofsAddr = moxaTableAddr[p];
1791 if (temp & IntrTx)
1792 writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
1793 if (temp & IntrBreak) {
1794 moxaBreakCnt[p]++;
1796 if (temp & IntrLine) {
1797 if (readb(ofsAddr + FlagStat) & DCD_state) {
1798 if ((moxaDCDState[p] & DCD_oldstate) == 0)
1799 moxaDCDState[p] = (DCD_oldstate |
1800 DCD_changed);
1801 } else {
1802 if (moxaDCDState[p] & DCD_oldstate)
1803 moxaDCDState[p] = DCD_changed;
1808 writeb(0, moxaIntPend[card]);
1810 if (moxaLowWaterChk) {
1811 p = card * MAX_PORTS_PER_BOARD;
1812 for (port = 0; port < ports; port++, p++) {
1813 if (moxaLowChkFlag[p]) {
1814 moxaLowChkFlag[p] = 0;
1815 ofsAddr = moxaTableAddr[p];
1816 low_water_check(ofsAddr);
1821 moxaLowWaterChk = 0;
1822 return (0);
1825 /*****************************************************************************
1826 * Card level function: *
1827 * 1. MoxaPortsOfCard(int cardno); *
1828 *****************************************************************************/
1829 int MoxaPortsOfCard(int cardno)
1832 if (moxa_boards[cardno].boardType == 0)
1833 return (0);
1834 return (moxa_boards[cardno].numPorts);
1837 /*****************************************************************************
1838 * Port level functions: *
1839 * 1. MoxaPortIsValid(int port); *
1840 * 2. MoxaPortEnable(int port); *
1841 * 3. MoxaPortDisable(int port); *
1842 * 4. MoxaPortGetMaxBaud(int port); *
1843 * 5. MoxaPortGetCurBaud(int port); *
1844 * 6. MoxaPortSetBaud(int port, long baud); *
1845 * 7. MoxaPortSetMode(int port, int databit, int stopbit, int parity); *
1846 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1847 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1848 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1849 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1850 * 12. MoxaPortLineStatus(int port); *
1851 * 13. MoxaPortDCDChange(int port); *
1852 * 14. MoxaPortDCDON(int port); *
1853 * 15. MoxaPortFlushData(int port, int mode); *
1854 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1855 * 17. MoxaPortReadData(int port, unsigned char * buffer, int length); *
1856 * 18. MoxaPortTxBufSize(int port); *
1857 * 19. MoxaPortRxBufSize(int port); *
1858 * 20. MoxaPortTxQueue(int port); *
1859 * 21. MoxaPortTxFree(int port); *
1860 * 22. MoxaPortRxQueue(int port); *
1861 * 23. MoxaPortRxFree(int port); *
1862 * 24. MoxaPortTxDisable(int port); *
1863 * 25. MoxaPortTxEnable(int port); *
1864 * 26. MoxaPortGetBrkCnt(int port); *
1865 * 27. MoxaPortResetBrkCnt(int port); *
1866 * 28. MoxaPortSetXonXoff(int port, int xonValue, int xoffValue); *
1867 * 29. MoxaPortIsTxHold(int port); *
1868 * 30. MoxaPortSendBreak(int port, int ticks); *
1869 *****************************************************************************/
1871 * Moxa Port Number Description:
1873 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1874 * the port number using in MOXA driver functions will be 0 to 31 for
1875 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1876 * to 127 for fourth. For example, if you setup three MOXA boards,
1877 * first board is C218, second board is C320-16 and third board is
1878 * C320-32. The port number of first board (C218 - 8 ports) is from
1879 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1880 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1881 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1882 * 127 will be invalid.
1885 * Moxa Functions Description:
1887 * Function 1: Driver initialization routine, this routine must be
1888 * called when initialized driver.
1889 * Syntax:
1890 * void MoxaDriverInit();
1893 * Function 2: Moxa driver private IOCTL command processing.
1894 * Syntax:
1895 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1897 * unsigned int cmd : IOCTL command
1898 * unsigned long arg : IOCTL argument
1899 * int port : port number (0 - 127)
1901 * return: 0 (OK)
1902 * -EINVAL
1903 * -ENOIOCTLCMD
1906 * Function 3: Moxa driver polling process routine.
1907 * Syntax:
1908 * int MoxaDriverPoll(void);
1910 * return: 0 ; polling O.K.
1911 * -1 : no any Moxa card.
1914 * Function 4: Get the ports of this card.
1915 * Syntax:
1916 * int MoxaPortsOfCard(int cardno);
1918 * int cardno : card number (0 - 3)
1920 * return: 0 : this card is invalid
1921 * 8/16/24/32
1924 * Function 5: Check this port is valid or invalid
1925 * Syntax:
1926 * int MoxaPortIsValid(int port);
1927 * int port : port number (0 - 127, ref port description)
1929 * return: 0 : this port is invalid
1930 * 1 : this port is valid
1933 * Function 6: Enable this port to start Tx/Rx data.
1934 * Syntax:
1935 * void MoxaPortEnable(int port);
1936 * int port : port number (0 - 127)
1939 * Function 7: Disable this port
1940 * Syntax:
1941 * void MoxaPortDisable(int port);
1942 * int port : port number (0 - 127)
1945 * Function 8: Get the maximun available baud rate of this port.
1946 * Syntax:
1947 * long MoxaPortGetMaxBaud(int port);
1948 * int port : port number (0 - 127)
1950 * return: 0 : this port is invalid
1951 * 38400/57600/115200 bps
1954 * Function 9: Get the current baud rate of this port.
1955 * Syntax:
1956 * long MoxaPortGetCurBaud(int port);
1957 * int port : port number (0 - 127)
1959 * return: 0 : this port is invalid
1960 * 50 - 115200 bps
1963 * Function 10: Setting baud rate of this port.
1964 * Syntax:
1965 * long MoxaPortSetBaud(int port, long baud);
1966 * int port : port number (0 - 127)
1967 * long baud : baud rate (50 - 115200)
1969 * return: 0 : this port is invalid or baud < 50
1970 * 50 - 115200 : the real baud rate set to the port, if
1971 * the argument baud is large than maximun
1972 * available baud rate, the real setting
1973 * baud rate will be the maximun baud rate.
1976 * Function 11: Setting the data-bits/stop-bits/parity of this port
1977 * Syntax:
1978 * int MoxaPortSetMode(int port, int databits, int stopbits, int parity);
1979 * int port : port number (0 - 127)
1980 * int databits : data bits (8/7/6/5)
1981 * int stopbits : stop bits (2/1/0, 0 show 1.5 stop bits)
1982 int parity : parity (0:None,1:Odd,2:Even,3:Mark,4:Space)
1984 * return: -1 : invalid parameter
1985 * 0 : setting O.K.
1988 * Function 12: Configure the port.
1989 * Syntax:
1990 * int MoxaPortSetTermio(int port, struct termios *termio);
1991 * int port : port number (0 - 127)
1992 * struct termios * termio : termio structure pointer
1994 * return: -1 : this port is invalid or termio == NULL
1995 * 0 : setting O.K.
1998 * Function 13: Get the DTR/RTS state of this port.
1999 * Syntax:
2000 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
2001 * int port : port number (0 - 127)
2002 * int * dtrState : pointer to INT to receive the current DTR
2003 * state. (if NULL, this function will not
2004 * write to this address)
2005 * int * rtsState : pointer to INT to receive the current RTS
2006 * state. (if NULL, this function will not
2007 * write to this address)
2009 * return: -1 : this port is invalid
2010 * 0 : O.K.
2013 * Function 14: Setting the DTR/RTS output state of this port.
2014 * Syntax:
2015 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
2016 * int port : port number (0 - 127)
2017 * int dtrState : DTR output state (0: off, 1: on)
2018 * int rtsState : RTS output state (0: off, 1: on)
2021 * Function 15: Setting the flow control of this port.
2022 * Syntax:
2023 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
2024 * int txFlow,int xany);
2025 * int port : port number (0 - 127)
2026 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
2027 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
2028 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
2029 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
2030 * int xany : S/W XANY flow control (0: no, 1: yes)
2033 * Function 16: Get ths line status of this port
2034 * Syntax:
2035 * int MoxaPortLineStatus(int port);
2036 * int port : port number (0 - 127)
2038 * return: Bit 0 - CTS state (0: off, 1: on)
2039 * Bit 1 - DSR state (0: off, 1: on)
2040 * Bit 2 - DCD state (0: off, 1: on)
2043 * Function 17: Check the DCD state has changed since the last read
2044 * of this function.
2045 * Syntax:
2046 * int MoxaPortDCDChange(int port);
2047 * int port : port number (0 - 127)
2049 * return: 0 : no changed
2050 * 1 : DCD has changed
2053 * Function 18: Check ths current DCD state is ON or not.
2054 * Syntax:
2055 * int MoxaPortDCDON(int port);
2056 * int port : port number (0 - 127)
2058 * return: 0 : DCD off
2059 * 1 : DCD on
2062 * Function 19: Flush the Rx/Tx buffer data of this port.
2063 * Syntax:
2064 * void MoxaPortFlushData(int port, int mode);
2065 * int port : port number (0 - 127)
2066 * int mode
2067 * 0 : flush the Rx buffer
2068 * 1 : flush the Tx buffer
2069 * 2 : flush the Rx and Tx buffer
2072 * Function 20: Write data.
2073 * Syntax:
2074 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
2075 * int port : port number (0 - 127)
2076 * unsigned char * buffer : pointer to write data buffer.
2077 * int length : write data length
2079 * return: 0 - length : real write data length
2082 * Function 21: Read data.
2083 * Syntax:
2084 * int MoxaPortReadData(int port, unsigned char * buffer, int length);
2085 * int port : port number (0 - 127)
2086 * unsigned char * buffer : pointer to read data buffer.
2087 * int length : read data buffer length
2089 * return: 0 - length : real read data length
2092 * Function 22: Get the Tx buffer size of this port
2093 * Syntax:
2094 * int MoxaPortTxBufSize(int port);
2095 * int port : port number (0 - 127)
2097 * return: .. : Tx buffer size
2100 * Function 23: Get the Rx buffer size of this port
2101 * Syntax:
2102 * int MoxaPortRxBufSize(int port);
2103 * int port : port number (0 - 127)
2105 * return: .. : Rx buffer size
2108 * Function 24: Get the Tx buffer current queued data bytes
2109 * Syntax:
2110 * int MoxaPortTxQueue(int port);
2111 * int port : port number (0 - 127)
2113 * return: .. : Tx buffer current queued data bytes
2116 * Function 25: Get the Tx buffer current free space
2117 * Syntax:
2118 * int MoxaPortTxFree(int port);
2119 * int port : port number (0 - 127)
2121 * return: .. : Tx buffer current free space
2124 * Function 26: Get the Rx buffer current queued data bytes
2125 * Syntax:
2126 * int MoxaPortRxQueue(int port);
2127 * int port : port number (0 - 127)
2129 * return: .. : Rx buffer current queued data bytes
2132 * Function 27: Get the Rx buffer current free space
2133 * Syntax:
2134 * int MoxaPortRxFree(int port);
2135 * int port : port number (0 - 127)
2137 * return: .. : Rx buffer current free space
2140 * Function 28: Disable port data transmission.
2141 * Syntax:
2142 * void MoxaPortTxDisable(int port);
2143 * int port : port number (0 - 127)
2146 * Function 29: Enable port data transmission.
2147 * Syntax:
2148 * void MoxaPortTxEnable(int port);
2149 * int port : port number (0 - 127)
2152 * Function 30: Get the received BREAK signal count.
2153 * Syntax:
2154 * int MoxaPortGetBrkCnt(int port);
2155 * int port : port number (0 - 127)
2157 * return: 0 - .. : BREAK signal count
2160 * Function 31: Get the received BREAK signal count and reset it.
2161 * Syntax:
2162 * int MoxaPortResetBrkCnt(int port);
2163 * int port : port number (0 - 127)
2165 * return: 0 - .. : BREAK signal count
2168 * Function 32: Set the S/W flow control new XON/XOFF value, default
2169 * XON is 0x11 & XOFF is 0x13.
2170 * Syntax:
2171 * void MoxaPortSetXonXoff(int port, int xonValue, int xoffValue);
2172 * int port : port number (0 - 127)
2173 * int xonValue : new XON value (0 - 255)
2174 * int xoffValue : new XOFF value (0 - 255)
2177 * Function 33: Check this port's transmission is hold by remote site
2178 * because the flow control.
2179 * Syntax:
2180 * int MoxaPortIsTxHold(int port);
2181 * int port : port number (0 - 127)
2183 * return: 0 : normal
2184 * 1 : hold by remote site
2187 * Function 34: Send out a BREAK signal.
2188 * Syntax:
2189 * void MoxaPortSendBreak(int port, int ms100);
2190 * int port : port number (0 - 127)
2191 * int ms100 : break signal time interval.
2192 * unit: 100 mini-second. if ms100 == 0, it will
2193 * send out a about 250 ms BREAK signal.
2196 int MoxaPortIsValid(int port)
2199 if (moxaCard == 0)
2200 return (0);
2201 if (moxaChkPort[port] == 0)
2202 return (0);
2203 return (1);
2206 void MoxaPortEnable(int port)
2208 unsigned long ofsAddr;
2209 int MoxaPortLineStatus(int);
2210 short lowwater = 512;
2212 ofsAddr = moxaTableAddr[port];
2213 writew(lowwater, ofsAddr + Low_water);
2214 moxaBreakCnt[port] = 0;
2215 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2216 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2217 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
2218 } else {
2219 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
2222 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
2223 moxafunc(ofsAddr, FC_FlushQueue, 2);
2225 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
2226 MoxaPortLineStatus(port);
2229 void MoxaPortDisable(int port)
2231 unsigned long ofsAddr;
2233 ofsAddr = moxaTableAddr[port];
2234 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
2235 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
2236 writew(0, ofsAddr + HostStat);
2237 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
2240 long MoxaPortGetMaxBaud(int port)
2242 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2243 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI))
2244 return (460800L);
2245 else
2246 return (921600L);
2250 long MoxaPortSetBaud(int port, long baud)
2252 unsigned long ofsAddr;
2253 long max, clock;
2254 unsigned int val;
2256 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
2257 return (0);
2258 ofsAddr = moxaTableAddr[port];
2259 if (baud > max)
2260 baud = max;
2261 if (max == 38400L)
2262 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
2263 else if (max == 57600L)
2264 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
2265 else
2266 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
2267 val = clock / baud;
2268 moxafunc(ofsAddr, FC_SetBaud, val);
2269 baud = clock / val;
2270 moxaCurBaud[port] = baud;
2271 return (baud);
2274 int MoxaPortSetTermio(int port, struct termios *termio)
2276 unsigned long ofsAddr;
2277 tcflag_t cflag;
2278 long baud;
2279 tcflag_t mode = 0;
2281 if (moxaChkPort[port] == 0 || termio == 0)
2282 return (-1);
2283 ofsAddr = moxaTableAddr[port];
2284 cflag = termio->c_cflag; /* termio->c_cflag */
2286 mode = termio->c_cflag & CSIZE;
2287 if (mode == CS5)
2288 mode = MX_CS5;
2289 else if (mode == CS6)
2290 mode = MX_CS6;
2291 else if (mode == CS7)
2292 mode = MX_CS7;
2293 else if (mode == CS8)
2294 mode = MX_CS8;
2296 if (termio->c_cflag & CSTOPB) {
2297 if (mode == MX_CS5)
2298 mode |= MX_STOP15;
2299 else
2300 mode |= MX_STOP2;
2301 } else
2302 mode |= MX_STOP1;
2304 if (termio->c_cflag & PARENB) {
2305 if (termio->c_cflag & PARODD)
2306 mode |= MX_PARODD;
2307 else
2308 mode |= MX_PAREVEN;
2309 } else
2310 mode |= MX_PARNONE;
2312 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
2314 cflag &= (CBAUD | CBAUDEX);
2315 #ifndef B921600
2316 #define B921600 (B460800+1)
2317 #endif
2318 switch (cflag) {
2319 case B921600:
2320 baud = 921600L;
2321 break;
2322 case B460800:
2323 baud = 460800L;
2324 break;
2325 case B230400:
2326 baud = 230400L;
2327 break;
2328 case B115200:
2329 baud = 115200L;
2330 break;
2331 case B57600:
2332 baud = 57600L;
2333 break;
2334 case B38400:
2335 baud = 38400L;
2336 break;
2337 case B19200:
2338 baud = 19200L;
2339 break;
2340 case B9600:
2341 baud = 9600L;
2342 break;
2343 case B4800:
2344 baud = 4800L;
2345 break;
2346 case B2400:
2347 baud = 2400L;
2348 break;
2349 case B1800:
2350 baud = 1800L;
2351 break;
2352 case B1200:
2353 baud = 1200L;
2354 break;
2355 case B600:
2356 baud = 600L;
2357 break;
2358 case B300:
2359 baud = 300L;
2360 break;
2361 case B200:
2362 baud = 200L;
2363 break;
2364 case B150:
2365 baud = 150L;
2366 break;
2367 case B134:
2368 baud = 134L;
2369 break;
2370 case B110:
2371 baud = 110L;
2372 break;
2373 case B75:
2374 baud = 75L;
2375 break;
2376 case B50:
2377 baud = 50L;
2378 break;
2379 default:
2380 baud = 0;
2382 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2383 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2384 if (baud == 921600L)
2385 return (-1);
2387 MoxaPortSetBaud(port, baud);
2389 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
2390 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
2391 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
2392 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
2393 wait_finish(ofsAddr);
2396 return (0);
2399 int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState)
2402 if (!MoxaPortIsValid(port))
2403 return (-1);
2404 if (dtrState) {
2405 if (moxaLineCtrl[port] & DTR_ON)
2406 *dtrState = 1;
2407 else
2408 *dtrState = 0;
2410 if (rtsState) {
2411 if (moxaLineCtrl[port] & RTS_ON)
2412 *rtsState = 1;
2413 else
2414 *rtsState = 0;
2416 return (0);
2419 void MoxaPortLineCtrl(int port, int dtr, int rts)
2421 unsigned long ofsAddr;
2422 int mode;
2424 ofsAddr = moxaTableAddr[port];
2425 mode = 0;
2426 if (dtr)
2427 mode |= DTR_ON;
2428 if (rts)
2429 mode |= RTS_ON;
2430 moxaLineCtrl[port] = mode;
2431 moxafunc(ofsAddr, FC_LineControl, mode);
2434 void MoxaPortFlowCtrl(int port, int rts, int cts, int txflow, int rxflow, int txany)
2436 unsigned long ofsAddr;
2437 int mode;
2439 ofsAddr = moxaTableAddr[port];
2440 mode = 0;
2441 if (rts)
2442 mode |= RTS_FlowCtl;
2443 if (cts)
2444 mode |= CTS_FlowCtl;
2445 if (txflow)
2446 mode |= Tx_FlowCtl;
2447 if (rxflow)
2448 mode |= Rx_FlowCtl;
2449 if (txany)
2450 mode |= IXM_IXANY;
2451 moxafunc(ofsAddr, FC_SetFlowCtl, mode);
2454 int MoxaPortLineStatus(int port)
2456 unsigned long ofsAddr;
2457 int val;
2459 ofsAddr = moxaTableAddr[port];
2460 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2461 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2462 moxafunc(ofsAddr, FC_LineStatus, 0);
2463 val = readw(ofsAddr + FuncArg);
2464 } else {
2465 val = readw(ofsAddr + FlagStat) >> 4;
2467 val &= 0x0B;
2468 if (val & 8) {
2469 val |= 4;
2470 if ((moxaDCDState[port] & DCD_oldstate) == 0)
2471 moxaDCDState[port] = (DCD_oldstate | DCD_changed);
2472 } else {
2473 if (moxaDCDState[port] & DCD_oldstate)
2474 moxaDCDState[port] = DCD_changed;
2476 val &= 7;
2477 return (val);
2480 int MoxaPortDCDChange(int port)
2482 int n;
2484 if (moxaChkPort[port] == 0)
2485 return (0);
2486 n = moxaDCDState[port];
2487 moxaDCDState[port] &= ~DCD_changed;
2488 n &= DCD_changed;
2489 return (n);
2492 int MoxaPortDCDON(int port)
2494 int n;
2496 if (moxaChkPort[port] == 0)
2497 return (0);
2498 if (moxaDCDState[port] & DCD_oldstate)
2499 n = 1;
2500 else
2501 n = 0;
2502 return (n);
2507 int MoxaDumpMem(int port, unsigned char * buffer, int len)
2509 int i;
2510 unsigned long baseAddr,ofsAddr,ofs;
2512 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2513 ofs = baseAddr + DynPage_addr + pageofs;
2514 if (len > 0x2000L)
2515 len = 0x2000L;
2516 for (i = 0; i < len; i++)
2517 buffer[i] = readb(ofs+i);
2522 int MoxaPortWriteData(int port, unsigned char * buffer, int len)
2524 int c, total, i;
2525 ushort tail;
2526 int cnt;
2527 ushort head, tx_mask, spage, epage;
2528 ushort pageno, pageofs, bufhead;
2529 unsigned long baseAddr, ofsAddr, ofs;
2531 ofsAddr = moxaTableAddr[port];
2532 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2533 tx_mask = readw(ofsAddr + TX_mask);
2534 spage = readw(ofsAddr + Page_txb);
2535 epage = readw(ofsAddr + EndPage_txb);
2536 tail = readw(ofsAddr + TXwptr);
2537 head = readw(ofsAddr + TXrptr);
2538 c = (head > tail) ? (head - tail - 1)
2539 : (head - tail + tx_mask);
2540 if (c > len)
2541 c = len;
2542 moxaLog.txcnt[port] += c;
2543 total = c;
2544 if (spage == epage) {
2545 bufhead = readw(ofsAddr + Ofs_txb);
2546 writew(spage, baseAddr + Control_reg);
2547 while (c > 0) {
2548 if (head > tail)
2549 len = head - tail - 1;
2550 else
2551 len = tx_mask + 1 - tail;
2552 len = (c > len) ? len : c;
2553 ofs = baseAddr + DynPage_addr + bufhead + tail;
2554 for (i = 0; i < len; i++)
2555 writeb(*buffer++, ofs + i);
2556 tail = (tail + len) & tx_mask;
2557 c -= len;
2559 writew(tail, ofsAddr + TXwptr);
2560 } else {
2561 len = c;
2562 pageno = spage + (tail >> 13);
2563 pageofs = tail & Page_mask;
2564 do {
2565 cnt = Page_size - pageofs;
2566 if (cnt > c)
2567 cnt = c;
2568 c -= cnt;
2569 writeb(pageno, baseAddr + Control_reg);
2570 ofs = baseAddr + DynPage_addr + pageofs;
2571 for (i = 0; i < cnt; i++)
2572 writeb(*buffer++, ofs + i);
2573 if (c == 0) {
2574 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2575 break;
2577 if (++pageno == epage)
2578 pageno = spage;
2579 pageofs = 0;
2580 } while (1);
2582 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2583 return (total);
2586 int MoxaPortReadData(int port, unsigned char * buffer, int space)
2588 register ushort head, pageofs;
2589 int i, count, cnt, len, total, remain;
2590 ushort tail, rx_mask, spage, epage;
2591 ushort pageno, bufhead;
2592 unsigned long baseAddr, ofsAddr, ofs;
2594 ofsAddr = moxaTableAddr[port];
2595 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2596 head = readw(ofsAddr + RXrptr);
2597 tail = readw(ofsAddr + RXwptr);
2598 rx_mask = readw(ofsAddr + RX_mask);
2599 spage = readw(ofsAddr + Page_rxb);
2600 epage = readw(ofsAddr + EndPage_rxb);
2601 count = (tail >= head) ? (tail - head)
2602 : (tail - head + rx_mask + 1);
2603 if (count == 0)
2604 return (0);
2606 total = (space > count) ? count : space;
2607 remain = count - total;
2608 moxaLog.rxcnt[port] += total;
2609 count = total;
2610 if (spage == epage) {
2611 bufhead = readw(ofsAddr + Ofs_rxb);
2612 writew(spage, baseAddr + Control_reg);
2613 while (count > 0) {
2614 if (tail >= head)
2615 len = tail - head;
2616 else
2617 len = rx_mask + 1 - head;
2618 len = (count > len) ? len : count;
2619 ofs = baseAddr + DynPage_addr + bufhead + head;
2620 for (i = 0; i < len; i++)
2621 *buffer++ = readb(ofs + i);
2622 head = (head + len) & rx_mask;
2623 count -= len;
2625 writew(head, ofsAddr + RXrptr);
2626 } else {
2627 len = count;
2628 pageno = spage + (head >> 13);
2629 pageofs = head & Page_mask;
2630 do {
2631 cnt = Page_size - pageofs;
2632 if (cnt > count)
2633 cnt = count;
2634 count -= cnt;
2635 writew(pageno, baseAddr + Control_reg);
2636 ofs = baseAddr + DynPage_addr + pageofs;
2637 for (i = 0; i < cnt; i++)
2638 *buffer++ = readb(ofs + i);
2639 if (count == 0) {
2640 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2641 break;
2643 if (++pageno == epage)
2644 pageno = spage;
2645 pageofs = 0;
2646 } while (1);
2648 if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2649 moxaLowWaterChk = 1;
2650 moxaLowChkFlag[port] = 1;
2652 return (total);
2656 int MoxaPortTxQueue(int port)
2658 unsigned long ofsAddr;
2659 ushort rptr, wptr, mask;
2660 int len;
2662 ofsAddr = moxaTableAddr[port];
2663 rptr = readw(ofsAddr + TXrptr);
2664 wptr = readw(ofsAddr + TXwptr);
2665 mask = readw(ofsAddr + TX_mask);
2666 len = (wptr - rptr) & mask;
2667 return (len);
2670 int MoxaPortTxFree(int port)
2672 unsigned long ofsAddr;
2673 ushort rptr, wptr, mask;
2674 int len;
2676 ofsAddr = moxaTableAddr[port];
2677 rptr = readw(ofsAddr + TXrptr);
2678 wptr = readw(ofsAddr + TXwptr);
2679 mask = readw(ofsAddr + TX_mask);
2680 len = mask - ((wptr - rptr) & mask);
2681 return (len);
2684 int MoxaPortRxQueue(int port)
2686 unsigned long ofsAddr;
2687 ushort rptr, wptr, mask;
2688 int len;
2690 ofsAddr = moxaTableAddr[port];
2691 rptr = readw(ofsAddr + RXrptr);
2692 wptr = readw(ofsAddr + RXwptr);
2693 mask = readw(ofsAddr + RX_mask);
2694 len = (wptr - rptr) & mask;
2695 return (len);
2699 void MoxaPortTxDisable(int port)
2701 unsigned long ofsAddr;
2703 ofsAddr = moxaTableAddr[port];
2704 moxafunc(ofsAddr, FC_SetXoffState, Magic_code);
2707 void MoxaPortTxEnable(int port)
2709 unsigned long ofsAddr;
2711 ofsAddr = moxaTableAddr[port];
2712 moxafunc(ofsAddr, FC_SetXonState, Magic_code);
2716 int MoxaPortResetBrkCnt(int port)
2718 ushort cnt;
2719 cnt = moxaBreakCnt[port];
2720 moxaBreakCnt[port] = 0;
2721 return (cnt);
2725 void MoxaPortSendBreak(int port, int ms100)
2727 unsigned long ofsAddr;
2729 ofsAddr = moxaTableAddr[port];
2730 if (ms100) {
2731 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2732 moxadelay(ms100 * (HZ / 10));
2733 } else {
2734 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2735 moxadelay(HZ / 4); /* 250 ms */
2737 moxafunc(ofsAddr, FC_StopBreak, Magic_code);
2740 static int moxa_get_serial_info(struct moxa_str *info,
2741 struct serial_struct *retinfo)
2743 struct serial_struct tmp;
2745 if (!retinfo)
2746 return (-EFAULT);
2747 memset(&tmp, 0, sizeof(tmp));
2748 tmp.type = info->type;
2749 tmp.line = info->port;
2750 tmp.port = 0;
2751 tmp.irq = 0;
2752 tmp.flags = info->asyncflags;
2753 tmp.baud_base = 921600;
2754 tmp.close_delay = info->close_delay;
2755 tmp.closing_wait = info->closing_wait;
2756 tmp.custom_divisor = 0;
2757 tmp.hub6 = 0;
2758 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2759 return -EFAULT;
2760 return (0);
2764 static int moxa_set_serial_info(struct moxa_str *info,
2765 struct serial_struct *new_info)
2767 struct serial_struct new_serial;
2769 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2770 return -EFAULT;
2772 if ((new_serial.irq != 0) ||
2773 (new_serial.port != 0) ||
2774 // (new_serial.type != info->type) ||
2775 (new_serial.custom_divisor != 0) ||
2776 (new_serial.baud_base != 921600))
2777 return (-EPERM);
2779 if (!suser()) {
2780 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2781 (info->asyncflags & ~ASYNC_USR_MASK)))
2782 return (-EPERM);
2783 } else {
2784 info->close_delay = new_serial.close_delay * HZ / 100;
2785 info->closing_wait = new_serial.closing_wait * HZ / 100;
2788 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2789 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2791 if (new_serial.type == PORT_16550A) {
2792 MoxaSetFifo(info->port, 1);
2793 } else {
2794 MoxaSetFifo(info->port, 0);
2797 info->type = new_serial.type;
2798 return (0);
2803 /*****************************************************************************
2804 * Static local functions: *
2805 *****************************************************************************/
2807 * moxadelay - delays a specified number ticks
2809 static void moxadelay(int tick)
2811 unsigned long st, et;
2813 st = jiffies;
2814 et = st + tick;
2815 while (jiffies < et);
2818 static void moxafunc(unsigned long ofsAddr, int cmd, ushort arg)
2821 writew(arg, ofsAddr + FuncArg);
2822 writew(cmd, ofsAddr + FuncCode);
2823 wait_finish(ofsAddr);
2826 static void wait_finish(unsigned long ofsAddr)
2828 unsigned long i, j;
2830 i = jiffies;
2831 while (readw(ofsAddr + FuncCode) != 0) {
2832 j = jiffies;
2833 if ((j - i) > moxaFuncTout) {
2834 return;
2839 static void low_water_check(unsigned long ofsAddr)
2841 int len;
2842 ushort rptr, wptr, mask;
2844 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2845 rptr = readw(ofsAddr + RXrptr);
2846 wptr = readw(ofsAddr + RXwptr);
2847 mask = readw(ofsAddr + RX_mask);
2848 len = (wptr - rptr) & mask;
2849 if (len <= Low_water)
2850 moxafunc(ofsAddr, FC_SendXon, 0);
2854 static int moxaloadbios(int cardno, unsigned char *tmp, int len)
2856 unsigned long baseAddr;
2857 int i;
2859 if(copy_from_user(moxaBuff, tmp, len))
2860 return -EFAULT;
2861 baseAddr = moxaBaseAddr[cardno];
2862 writeb(HW_reset, baseAddr + Control_reg); /* reset */
2863 moxadelay(1); /* delay 10 ms */
2864 for (i = 0; i < 4096; i++)
2865 writeb(0, baseAddr + i); /* clear fix page */
2866 for (i = 0; i < len; i++)
2867 writeb(moxaBuff[i], baseAddr + i); /* download BIOS */
2868 writeb(0, baseAddr + Control_reg); /* restart */
2869 return (0);
2872 static int moxafindcard(int cardno)
2874 unsigned long baseAddr;
2875 ushort tmp;
2877 baseAddr = moxaBaseAddr[cardno];
2878 switch (moxa_boards[cardno].boardType) {
2879 case MOXA_BOARD_C218_ISA:
2880 case MOXA_BOARD_C218_PCI:
2881 if ((tmp = readw(baseAddr + C218_key)) != C218_KeyCode) {
2882 return (-1);
2884 break;
2885 case MOXA_BOARD_CP204J:
2886 if ((tmp = readw(baseAddr + C218_key)) != CP204J_KeyCode) {
2887 return (-1);
2889 break;
2890 default:
2891 if ((tmp = readw(baseAddr + C320_key)) != C320_KeyCode) {
2892 return (-1);
2894 if ((tmp = readw(baseAddr + C320_status)) != STS_init) {
2895 return (-2);
2898 return (0);
2901 static int moxaload320b(int cardno, unsigned char * tmp, int len)
2903 unsigned long baseAddr;
2904 int i;
2906 if(copy_from_user(moxaBuff, tmp, len))
2907 return -EFAULT;
2908 baseAddr = moxaBaseAddr[cardno];
2909 writew(len - 7168 - 2, baseAddr + C320bapi_len);
2910 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
2911 for (i = 0; i < 7168; i++)
2912 writeb(moxaBuff[i], baseAddr + DynPage_addr + i);
2913 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
2914 for (i = 0; i < (len - 7168); i++)
2915 writeb(moxaBuff[i + 7168], baseAddr + DynPage_addr + i);
2916 return (0);
2919 static int moxaloadcode(int cardno, unsigned char * tmp, int len)
2921 unsigned long baseAddr, ofsAddr;
2922 int retval, port, i;
2924 if(copy_from_user(moxaBuff, tmp, len))
2925 return -EFAULT;
2926 baseAddr = moxaBaseAddr[cardno];
2927 switch (moxa_boards[cardno].boardType) {
2928 case MOXA_BOARD_C218_ISA:
2929 case MOXA_BOARD_C218_PCI:
2930 case MOXA_BOARD_CP204J:
2931 retval = moxaloadc218(cardno, baseAddr, len);
2932 if (retval)
2933 return (retval);
2934 port = cardno * MAX_PORTS_PER_BOARD;
2935 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2936 moxaChkPort[port] = 1;
2937 moxaCurBaud[port] = 9600L;
2938 moxaDCDState[port] = 0;
2939 moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
2940 ofsAddr = moxaTableAddr[port];
2941 writew(C218rx_mask, ofsAddr + RX_mask);
2942 writew(C218tx_mask, ofsAddr + TX_mask);
2943 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
2944 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
2946 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
2947 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
2950 break;
2951 default:
2952 retval = moxaloadc320(cardno, baseAddr, len,
2953 &moxa_boards[cardno].numPorts);
2954 if (retval)
2955 return (retval);
2956 port = cardno * MAX_PORTS_PER_BOARD;
2957 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2958 moxaChkPort[port] = 1;
2959 moxaCurBaud[port] = 9600L;
2960 moxaDCDState[port] = 0;
2961 moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
2962 ofsAddr = moxaTableAddr[port];
2963 if (moxa_boards[cardno].numPorts == 8) {
2964 writew(C320p8rx_mask, ofsAddr + RX_mask);
2965 writew(C320p8tx_mask, ofsAddr + TX_mask);
2966 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
2967 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
2968 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
2969 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
2971 } else if (moxa_boards[cardno].numPorts == 16) {
2972 writew(C320p16rx_mask, ofsAddr + RX_mask);
2973 writew(C320p16tx_mask, ofsAddr + TX_mask);
2974 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
2975 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
2976 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
2977 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
2979 } else if (moxa_boards[cardno].numPorts == 24) {
2980 writew(C320p24rx_mask, ofsAddr + RX_mask);
2981 writew(C320p24tx_mask, ofsAddr + TX_mask);
2982 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
2983 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
2984 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
2985 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2986 } else if (moxa_boards[cardno].numPorts == 32) {
2987 writew(C320p32rx_mask, ofsAddr + RX_mask);
2988 writew(C320p32tx_mask, ofsAddr + TX_mask);
2989 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
2990 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
2991 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
2992 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
2993 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2996 break;
2998 return (0);
3001 static int moxaloadc218(int cardno, unsigned long baseAddr, int len)
3003 char retry;
3004 int i, j, len1, len2;
3005 ushort usum, *ptr, keycode;
3007 if (moxa_boards[cardno].boardType == MOXA_BOARD_CP204J)
3008 keycode = CP204J_KeyCode;
3009 else
3010 keycode = C218_KeyCode;
3011 usum = 0;
3012 len1 = len >> 1;
3013 ptr = (ushort *) moxaBuff;
3014 for (i = 0; i < len1; i++)
3015 usum += *(ptr + i);
3016 retry = 0;
3017 do {
3018 len1 = len >> 1;
3019 j = 0;
3020 while (len1) {
3021 len2 = (len1 > 2048) ? 2048 : len1;
3022 len1 -= len2;
3023 for (i = 0; i < len2 << 1; i++)
3024 writeb(moxaBuff[i + j], baseAddr + C218_LoadBuf + i);
3025 j += i;
3027 writew(len2, baseAddr + C218DLoad_len);
3028 writew(0, baseAddr + C218_key);
3029 for (i = 0; i < 100; i++) {
3030 if (readw(baseAddr + C218_key) == keycode)
3031 break;
3032 moxadelay(1); /* delay 10 ms */
3034 if (readw(baseAddr + C218_key) != keycode) {
3035 return (-1);
3038 writew(0, baseAddr + C218DLoad_len);
3039 writew(usum, baseAddr + C218check_sum);
3040 writew(0, baseAddr + C218_key);
3041 for (i = 0; i < 100; i++) {
3042 if (readw(baseAddr + C218_key) == keycode)
3043 break;
3044 moxadelay(1); /* delay 10 ms */
3046 retry++;
3047 } while ((readb(baseAddr + C218chksum_ok) != 1) && (retry < 3));
3048 if (readb(baseAddr + C218chksum_ok) != 1) {
3049 return (-1);
3051 writew(0, baseAddr + C218_key);
3052 for (i = 0; i < 100; i++) {
3053 if (readw(baseAddr + Magic_no) == Magic_code)
3054 break;
3055 moxadelay(1); /* delay 10 ms */
3057 if (readw(baseAddr + Magic_no) != Magic_code) {
3058 return (-1);
3060 writew(1, baseAddr + Disable_IRQ);
3061 writew(0, baseAddr + Magic_no);
3062 for (i = 0; i < 100; i++) {
3063 if (readw(baseAddr + Magic_no) == Magic_code)
3064 break;
3065 moxadelay(1); /* delay 10 ms */
3067 if (readw(baseAddr + Magic_no) != Magic_code) {
3068 return (-1);
3070 moxaCard = 1;
3071 moxaIntNdx[cardno] = baseAddr + IRQindex;
3072 moxaIntPend[cardno] = baseAddr + IRQpending;
3073 moxaIntTable[cardno] = baseAddr + IRQtable;
3074 return (0);
3077 static int moxaloadc320(int cardno, unsigned long baseAddr, int len, int *numPorts)
3079 ushort usum;
3080 int i, j, wlen, len2, retry;
3081 ushort *uptr;
3083 usum = 0;
3084 wlen = len >> 1;
3085 uptr = (ushort *) moxaBuff;
3086 for (i = 0; i < wlen; i++)
3087 usum += uptr[i];
3088 retry = 0;
3089 j = 0;
3090 do {
3091 while (wlen) {
3092 if (wlen > 2048)
3093 len2 = 2048;
3094 else
3095 len2 = wlen;
3096 wlen -= len2;
3097 len2 <<= 1;
3098 for (i = 0; i < len2; i++)
3099 writeb(moxaBuff[j + i], baseAddr + C320_LoadBuf + i);
3100 len2 >>= 1;
3101 j += i;
3102 writew(len2, baseAddr + C320DLoad_len);
3103 writew(0, baseAddr + C320_key);
3104 for (i = 0; i < 10; i++) {
3105 if (readw(baseAddr + C320_key) == C320_KeyCode)
3106 break;
3107 moxadelay(1);
3109 if (readw(baseAddr + C320_key) != C320_KeyCode)
3110 return (-1);
3112 writew(0, baseAddr + C320DLoad_len);
3113 writew(usum, baseAddr + C320check_sum);
3114 writew(0, baseAddr + C320_key);
3115 for (i = 0; i < 10; i++) {
3116 if (readw(baseAddr + C320_key) == C320_KeyCode)
3117 break;
3118 moxadelay(1);
3120 retry++;
3121 } while ((readb(baseAddr + C320chksum_ok) != 1) && (retry < 3));
3122 if (readb(baseAddr + C320chksum_ok) != 1)
3123 return (-1);
3124 writew(0, baseAddr + C320_key);
3125 for (i = 0; i < 600; i++) {
3126 if (readw(baseAddr + Magic_no) == Magic_code)
3127 break;
3128 moxadelay(1);
3130 if (readw(baseAddr + Magic_no) != Magic_code)
3131 return (-100);
3133 if (moxa_boards[cardno].busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
3134 writew(0x3800, baseAddr + TMS320_PORT1);
3135 writew(0x3900, baseAddr + TMS320_PORT2);
3136 writew(28499, baseAddr + TMS320_CLOCK);
3137 } else {
3138 writew(0x3200, baseAddr + TMS320_PORT1);
3139 writew(0x3400, baseAddr + TMS320_PORT2);
3140 writew(19999, baseAddr + TMS320_CLOCK);
3142 writew(1, baseAddr + Disable_IRQ);
3143 writew(0, baseAddr + Magic_no);
3144 for (i = 0; i < 500; i++) {
3145 if (readw(baseAddr + Magic_no) == Magic_code)
3146 break;
3147 moxadelay(1);
3149 if (readw(baseAddr + Magic_no) != Magic_code)
3150 return (-102);
3152 j = readw(baseAddr + Module_cnt);
3153 if (j <= 0)
3154 return (-101);
3155 *numPorts = j * 8;
3156 writew(j, baseAddr + Module_no);
3157 writew(0, baseAddr + Magic_no);
3158 for (i = 0; i < 600; i++) {
3159 if (readw(baseAddr + Magic_no) == Magic_code)
3160 break;
3161 moxadelay(1);
3163 if (readw(baseAddr + Magic_no) != Magic_code)
3164 return (-102);
3165 moxaCard = 1;
3166 moxaIntNdx[cardno] = baseAddr + IRQindex;
3167 moxaIntPend[cardno] = baseAddr + IRQpending;
3168 moxaIntTable[cardno] = baseAddr + IRQtable;
3169 return (0);
3172 long MoxaPortGetCurBaud(int port)
3175 if (moxaChkPort[port] == 0)
3176 return (0);
3177 return (moxaCurBaud[port]);
3180 static void MoxaSetFifo(int port, int enable)
3182 unsigned long ofsAddr = moxaTableAddr[port];
3184 if (!enable) {
3185 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
3186 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
3187 } else {
3188 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
3189 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
3193 #if 0
3194 int MoxaPortSetMode(int port, int databits, int stopbits, int parity)
3196 unsigned long ofsAddr;
3197 int val;
3199 val = 0;
3200 switch (databits) {
3201 case 5:
3202 val |= 0;
3203 break;
3204 case 6:
3205 val |= 1;
3206 break;
3207 case 7:
3208 val |= 2;
3209 break;
3210 case 8:
3211 val |= 3;
3212 break;
3213 default:
3214 return (-1);
3216 switch (stopbits) {
3217 case 0:
3218 val |= 0;
3219 break; /* stop bits 1.5 */
3220 case 1:
3221 val |= 0;
3222 break;
3223 case 2:
3224 val |= 4;
3225 break;
3226 default:
3227 return (-1);
3229 switch (parity) {
3230 case 0:
3231 val |= 0x00;
3232 break; /* None */
3233 case 1:
3234 val |= 0x08;
3235 break; /* Odd */
3236 case 2:
3237 val |= 0x18;
3238 break; /* Even */
3239 case 3:
3240 val |= 0x28;
3241 break; /* Mark */
3242 case 4:
3243 val |= 0x38;
3244 break; /* Space */
3245 default:
3246 return (-1);
3248 ofsAddr = moxaTableAddr[port];
3249 moxafunc(ofsAddr, FC_SetMode, val);
3250 return (0);
3253 int MoxaPortTxBufSize(int port)
3255 unsigned long ofsAddr;
3256 int size;
3258 ofsAddr = moxaTableAddr[port];
3259 size = readw(ofsAddr + TX_mask);
3260 return (size);
3263 int MoxaPortRxBufSize(int port)
3265 unsigned long ofsAddr;
3266 int size;
3268 ofsAddr = moxaTableAddr[port];
3269 size = readw(ofsAddr + RX_mask);
3270 return (size);
3273 int MoxaPortRxFree(int port)
3275 unsigned long ofsAddr;
3276 ushort rptr, wptr, mask;
3277 int len;
3279 ofsAddr = moxaTableAddr[port];
3280 rptr = readw(ofsAddr + RXrptr);
3281 wptr = readw(ofsAddr + RXwptr);
3282 mask = readw(ofsAddr + RX_mask);
3283 len = mask - ((wptr - rptr) & mask);
3284 return (len);
3286 int MoxaPortGetBrkCnt(int port)
3288 return (moxaBreakCnt[port]);
3291 void MoxaPortSetXonXoff(int port, int xonValue, int xoffValue)
3293 unsigned long ofsAddr;
3295 ofsAddr = moxaTableAddr[port];
3296 writew(xonValue, ofsAddr + FuncArg);
3297 writew(xoffValue, ofsAddr + FuncArg1);
3298 writew(FC_SetXonXoff, ofsAddr + FuncCode);
3299 wait_finish(ofsAddr);
3302 int MoxaPortIsTxHold(int port)
3304 unsigned long ofsAddr;
3305 int val;
3307 ofsAddr = moxaTableAddr[port];
3308 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
3309 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
3310 moxafunc(ofsAddr, FC_GetCCSR, 0);
3311 val = readw(ofsAddr + FuncArg);
3312 if (val & 0x04)
3313 return (1);
3314 } else {
3315 if (readw(ofsAddr + FlagStat) & Tx_flowOff)
3316 return (1);
3318 return (0);
3320 #endif