[PATCH] orinoco: orinoco_xmit() should only return valid symbolic constants
[linux-2.6/sactl.git] / drivers / char / moxa.c
blobf43c2e04eadd3878d57126b154afa2b4cbdc74aa
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/mm.h>
36 #include <linux/ioport.h>
37 #include <linux/errno.h>
38 #include <linux/signal.h>
39 #include <linux/sched.h>
40 #include <linux/timer.h>
41 #include <linux/interrupt.h>
42 #include <linux/tty.h>
43 #include <linux/tty_flip.h>
44 #include <linux/major.h>
45 #include <linux/string.h>
46 #include <linux/fcntl.h>
47 #include <linux/ptrace.h>
48 #include <linux/serial.h>
49 #include <linux/tty_driver.h>
50 #include <linux/delay.h>
51 #include <linux/pci.h>
52 #include <linux/init.h>
53 #include <linux/bitops.h>
55 #include <asm/system.h>
56 #include <asm/io.h>
57 #include <asm/uaccess.h>
59 #define MOXA_VERSION "5.1k"
61 #define MOXAMAJOR 172
62 #define MOXACUMAJOR 173
64 #define put_to_user(arg1, arg2) put_user(arg1, (unsigned long *)arg2)
65 #define get_from_user(arg1, arg2) get_user(arg1, (unsigned int *)arg2)
67 #define MAX_BOARDS 4 /* Don't change this value */
68 #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
69 #define MAX_PORTS 128 /* Don't change this value */
72 * Define the Moxa PCI vendor and device IDs.
74 #define MOXA_BUS_TYPE_ISA 0
75 #define MOXA_BUS_TYPE_PCI 1
77 #ifndef PCI_VENDOR_ID_MOXA
78 #define PCI_VENDOR_ID_MOXA 0x1393
79 #endif
80 #ifndef PCI_DEVICE_ID_CP204J
81 #define PCI_DEVICE_ID_CP204J 0x2040
82 #endif
83 #ifndef PCI_DEVICE_ID_C218
84 #define PCI_DEVICE_ID_C218 0x2180
85 #endif
86 #ifndef PCI_DEVICE_ID_C320
87 #define PCI_DEVICE_ID_C320 0x3200
88 #endif
90 enum {
91 MOXA_BOARD_C218_PCI = 1,
92 MOXA_BOARD_C218_ISA,
93 MOXA_BOARD_C320_PCI,
94 MOXA_BOARD_C320_ISA,
95 MOXA_BOARD_CP204J,
98 static char *moxa_brdname[] =
100 "C218 Turbo PCI series",
101 "C218 Turbo ISA series",
102 "C320 Turbo PCI series",
103 "C320 Turbo ISA series",
104 "CP-204J series",
107 #ifdef CONFIG_PCI
108 static struct pci_device_id moxa_pcibrds[] = {
109 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C218, PCI_ANY_ID, PCI_ANY_ID,
110 0, 0, MOXA_BOARD_C218_PCI },
111 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C320, PCI_ANY_ID, PCI_ANY_ID,
112 0, 0, MOXA_BOARD_C320_PCI },
113 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP204J, PCI_ANY_ID, PCI_ANY_ID,
114 0, 0, MOXA_BOARD_CP204J },
115 { 0 }
117 MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
118 #endif /* CONFIG_PCI */
120 typedef struct _moxa_isa_board_conf {
121 int boardType;
122 int numPorts;
123 unsigned long baseAddr;
124 } moxa_isa_board_conf;
126 static moxa_isa_board_conf moxa_isa_boards[] =
128 /* {MOXA_BOARD_C218_ISA,8,0xDC000}, */
131 typedef struct _moxa_pci_devinfo {
132 ushort busNum;
133 ushort devNum;
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];
147 struct moxa_str {
148 int type;
149 int port;
150 int close_delay;
151 unsigned short closing_wait;
152 int count;
153 int blocked_open;
154 long event; /* long req'd for set_bit --RR */
155 int asyncflags;
156 unsigned long statusflags;
157 struct tty_struct *tty;
158 int cflag;
159 wait_queue_head_t open_wait;
160 wait_queue_head_t close_wait;
161 struct work_struct tqueue;
164 struct mxser_mstatus {
165 tcflag_t cflag;
166 int cts;
167 int dsr;
168 int ri;
169 int dcd;
172 static struct mxser_mstatus GMStatus[MAX_PORTS];
174 /* statusflags */
175 #define TXSTOPPED 0x1
176 #define LOWWAIT 0x2
177 #define EMPTYWAIT 0x4
178 #define THROTTLE 0x8
180 /* event */
181 #define MOXA_EVENT_HANGUP 1
183 #define SERIAL_DO_RESTART
186 #define SERIAL_TYPE_NORMAL 1
188 #define WAKEUP_CHARS 256
190 #define PORTNO(x) ((x)->index)
192 static int verbose = 0;
193 static int ttymajor = MOXAMAJOR;
194 /* Variables for insmod */
195 #ifdef MODULE
196 static int baseaddr[] = {0, 0, 0, 0};
197 static int type[] = {0, 0, 0, 0};
198 static int numports[] = {0, 0, 0, 0};
199 #endif
201 MODULE_AUTHOR("William Chen");
202 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
203 MODULE_LICENSE("GPL");
204 #ifdef MODULE
205 module_param_array(type, int, NULL, 0);
206 module_param_array(baseaddr, int, NULL, 0);
207 module_param_array(numports, int, NULL, 0);
208 #endif
209 module_param(ttymajor, int, 0);
210 module_param(verbose, bool, 0644);
212 static struct tty_driver *moxaDriver;
213 static struct moxa_str moxaChannels[MAX_PORTS];
214 static unsigned char *moxaXmitBuff;
215 static int moxaTimer_on;
216 static struct timer_list moxaTimer;
217 static int moxaEmptyTimer_on[MAX_PORTS];
218 static struct timer_list moxaEmptyTimer[MAX_PORTS];
219 static struct semaphore moxaBuffSem;
222 * static functions:
224 static void do_moxa_softint(void *);
225 static int moxa_open(struct tty_struct *, struct file *);
226 static void moxa_close(struct tty_struct *, struct file *);
227 static int moxa_write(struct tty_struct *, const unsigned char *, int);
228 static int moxa_write_room(struct tty_struct *);
229 static void moxa_flush_buffer(struct tty_struct *);
230 static int moxa_chars_in_buffer(struct tty_struct *);
231 static void moxa_flush_chars(struct tty_struct *);
232 static void moxa_put_char(struct tty_struct *, unsigned char);
233 static int moxa_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);
234 static void moxa_throttle(struct tty_struct *);
235 static void moxa_unthrottle(struct tty_struct *);
236 static void moxa_set_termios(struct tty_struct *, struct termios *);
237 static void moxa_stop(struct tty_struct *);
238 static void moxa_start(struct tty_struct *);
239 static void moxa_hangup(struct tty_struct *);
240 static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
241 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
242 unsigned int set, unsigned int clear);
243 static void moxa_poll(unsigned long);
244 static void set_tty_param(struct tty_struct *);
245 static int block_till_ready(struct tty_struct *, struct file *,
246 struct moxa_str *);
247 static void setup_empty_event(struct tty_struct *);
248 static void check_xmit_empty(unsigned long);
249 static void shut_down(struct moxa_str *);
250 static void receive_data(struct moxa_str *);
252 * moxa board interface functions:
254 static void MoxaDriverInit(void);
255 static int MoxaDriverIoctl(unsigned int, unsigned long, int);
256 static int MoxaDriverPoll(void);
257 static int MoxaPortsOfCard(int);
258 static int MoxaPortIsValid(int);
259 static void MoxaPortEnable(int);
260 static void MoxaPortDisable(int);
261 static long MoxaPortGetMaxBaud(int);
262 static long MoxaPortSetBaud(int, long);
263 static int MoxaPortSetTermio(int, struct termios *);
264 static int MoxaPortGetLineOut(int, int *, int *);
265 static void MoxaPortLineCtrl(int, int, int);
266 static void MoxaPortFlowCtrl(int, int, int, int, int, int);
267 static int MoxaPortLineStatus(int);
268 static int MoxaPortDCDChange(int);
269 static int MoxaPortDCDON(int);
270 static void MoxaPortFlushData(int, int);
271 static int MoxaPortWriteData(int, unsigned char *, int);
272 static int MoxaPortReadData(int, struct tty_struct *tty);
273 static int MoxaPortTxQueue(int);
274 static int MoxaPortRxQueue(int);
275 static int MoxaPortTxFree(int);
276 static void MoxaPortTxDisable(int);
277 static void MoxaPortTxEnable(int);
278 static int MoxaPortResetBrkCnt(int);
279 static void MoxaPortSendBreak(int, int);
280 static int moxa_get_serial_info(struct moxa_str *, struct serial_struct __user *);
281 static int moxa_set_serial_info(struct moxa_str *, struct serial_struct __user *);
282 static void MoxaSetFifo(int port, int enable);
284 static struct tty_operations moxa_ops = {
285 .open = moxa_open,
286 .close = moxa_close,
287 .write = moxa_write,
288 .write_room = moxa_write_room,
289 .flush_buffer = moxa_flush_buffer,
290 .chars_in_buffer = moxa_chars_in_buffer,
291 .flush_chars = moxa_flush_chars,
292 .put_char = moxa_put_char,
293 .ioctl = moxa_ioctl,
294 .throttle = moxa_throttle,
295 .unthrottle = moxa_unthrottle,
296 .set_termios = moxa_set_termios,
297 .stop = moxa_stop,
298 .start = moxa_start,
299 .hangup = moxa_hangup,
300 .tiocmget = moxa_tiocmget,
301 .tiocmset = moxa_tiocmset,
304 static spinlock_t moxa_lock = SPIN_LOCK_UNLOCKED;
306 #ifdef CONFIG_PCI
307 static int moxa_get_PCI_conf(struct pci_dev *p, int board_type, moxa_board_conf * board)
309 board->baseAddr = pci_resource_start (p, 2);
310 board->boardType = board_type;
311 switch (board_type) {
312 case MOXA_BOARD_C218_ISA:
313 case MOXA_BOARD_C218_PCI:
314 board->numPorts = 8;
315 break;
317 case MOXA_BOARD_CP204J:
318 board->numPorts = 4;
319 break;
320 default:
321 board->numPorts = 0;
322 break;
324 board->busType = MOXA_BUS_TYPE_PCI;
325 board->pciInfo.busNum = p->bus->number;
326 board->pciInfo.devNum = p->devfn >> 3;
328 return (0);
330 #endif /* CONFIG_PCI */
332 static int __init moxa_init(void)
334 int i, numBoards;
335 struct moxa_str *ch;
337 printk(KERN_INFO "MOXA Intellio family driver version %s\n", MOXA_VERSION);
338 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
339 if (!moxaDriver)
340 return -ENOMEM;
342 init_MUTEX(&moxaBuffSem);
343 moxaDriver->owner = THIS_MODULE;
344 moxaDriver->name = "ttyMX";
345 moxaDriver->devfs_name = "tts/a";
346 moxaDriver->major = ttymajor;
347 moxaDriver->minor_start = 0;
348 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
349 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
350 moxaDriver->init_termios = tty_std_termios;
351 moxaDriver->init_termios.c_iflag = 0;
352 moxaDriver->init_termios.c_oflag = 0;
353 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
354 moxaDriver->init_termios.c_lflag = 0;
355 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
356 tty_set_operations(moxaDriver, &moxa_ops);
358 moxaXmitBuff = NULL;
360 for (i = 0, ch = moxaChannels; i < MAX_PORTS; i++, ch++) {
361 ch->type = PORT_16550A;
362 ch->port = i;
363 INIT_WORK(&ch->tqueue, do_moxa_softint, ch);
364 ch->tty = NULL;
365 ch->close_delay = 5 * HZ / 10;
366 ch->closing_wait = 30 * HZ;
367 ch->count = 0;
368 ch->blocked_open = 0;
369 ch->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
370 init_waitqueue_head(&ch->open_wait);
371 init_waitqueue_head(&ch->close_wait);
374 for (i = 0; i < MAX_BOARDS; i++) {
375 moxa_boards[i].boardType = 0;
376 moxa_boards[i].numPorts = 0;
377 moxa_boards[i].baseAddr = 0;
378 moxa_boards[i].busType = 0;
379 moxa_boards[i].pciInfo.busNum = 0;
380 moxa_boards[i].pciInfo.devNum = 0;
382 MoxaDriverInit();
383 printk("Tty devices major number = %d\n", ttymajor);
385 if (tty_register_driver(moxaDriver)) {
386 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
387 put_tty_driver(moxaDriver);
388 return -1;
390 for (i = 0; i < MAX_PORTS; i++) {
391 init_timer(&moxaEmptyTimer[i]);
392 moxaEmptyTimer[i].function = check_xmit_empty;
393 moxaEmptyTimer[i].data = (unsigned long) & moxaChannels[i];
394 moxaEmptyTimer_on[i] = 0;
397 init_timer(&moxaTimer);
398 moxaTimer.function = moxa_poll;
399 moxaTimer.expires = jiffies + (HZ / 50);
400 moxaTimer_on = 1;
401 add_timer(&moxaTimer);
403 /* Find the boards defined in source code */
404 numBoards = 0;
405 for (i = 0; i < MAX_BOARDS; i++) {
406 if ((moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA) ||
407 (moxa_isa_boards[i].boardType == MOXA_BOARD_C320_ISA)) {
408 moxa_boards[numBoards].boardType = moxa_isa_boards[i].boardType;
409 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
410 moxa_boards[numBoards].numPorts = 8;
411 else
412 moxa_boards[numBoards].numPorts = moxa_isa_boards[i].numPorts;
413 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
414 moxa_boards[numBoards].baseAddr = moxa_isa_boards[i].baseAddr;
415 if (verbose)
416 printk("Board %2d: %s board(baseAddr=%lx)\n",
417 numBoards + 1,
418 moxa_brdname[moxa_boards[numBoards].boardType - 1],
419 moxa_boards[numBoards].baseAddr);
420 numBoards++;
423 /* Find the boards defined form module args. */
424 #ifdef MODULE
425 for (i = 0; i < MAX_BOARDS; i++) {
426 if ((type[i] == MOXA_BOARD_C218_ISA) ||
427 (type[i] == MOXA_BOARD_C320_ISA)) {
428 if (verbose)
429 printk("Board %2d: %s board(baseAddr=%lx)\n",
430 numBoards + 1,
431 moxa_brdname[type[i] - 1],
432 (unsigned long) baseaddr[i]);
433 if (numBoards >= MAX_BOARDS) {
434 if (verbose)
435 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
436 continue;
438 moxa_boards[numBoards].boardType = type[i];
439 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
440 moxa_boards[numBoards].numPorts = 8;
441 else
442 moxa_boards[numBoards].numPorts = numports[i];
443 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
444 moxa_boards[numBoards].baseAddr = baseaddr[i];
445 numBoards++;
448 #endif
449 /* Find PCI boards here */
450 #ifdef CONFIG_PCI
452 struct pci_dev *p = NULL;
453 int n = ARRAY_SIZE(moxa_pcibrds) - 1;
454 i = 0;
455 while (i < n) {
456 while ((p = pci_get_device(moxa_pcibrds[i].vendor, moxa_pcibrds[i].device, p))!=NULL)
458 if (pci_enable_device(p))
459 continue;
460 if (numBoards >= MAX_BOARDS) {
461 if (verbose)
462 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
463 } else {
464 moxa_get_PCI_conf(p, moxa_pcibrds[i].driver_data,
465 &moxa_boards[numBoards]);
466 numBoards++;
469 i++;
472 #endif
473 for (i = 0; i < numBoards; i++) {
474 moxaBaseAddr[i] = ioremap((unsigned long) moxa_boards[i].baseAddr, 0x4000);
477 return (0);
480 static void __exit moxa_exit(void)
482 int i;
484 if (verbose)
485 printk("Unloading module moxa ...\n");
487 if (moxaTimer_on)
488 del_timer(&moxaTimer);
490 for (i = 0; i < MAX_PORTS; i++)
491 if (moxaEmptyTimer_on[i])
492 del_timer(&moxaEmptyTimer[i]);
494 if (tty_unregister_driver(moxaDriver))
495 printk("Couldn't unregister MOXA Intellio family serial driver\n");
496 put_tty_driver(moxaDriver);
497 if (verbose)
498 printk("Done\n");
501 module_init(moxa_init);
502 module_exit(moxa_exit);
504 static void do_moxa_softint(void *private_)
506 struct moxa_str *ch = (struct moxa_str *) private_;
507 struct tty_struct *tty;
509 if (ch && (tty = ch->tty)) {
510 if (test_and_clear_bit(MOXA_EVENT_HANGUP, &ch->event)) {
511 tty_hangup(tty); /* FIXME: module removal race here - AKPM */
512 wake_up_interruptible(&ch->open_wait);
513 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
518 static int moxa_open(struct tty_struct *tty, struct file *filp)
520 struct moxa_str *ch;
521 int port;
522 int retval;
523 unsigned long page;
525 port = PORTNO(tty);
526 if (port == MAX_PORTS) {
527 return (0);
529 if (!MoxaPortIsValid(port)) {
530 tty->driver_data = NULL;
531 return (-ENODEV);
533 down(&moxaBuffSem);
534 if (!moxaXmitBuff) {
535 page = get_zeroed_page(GFP_KERNEL);
536 if (!page) {
537 up(&moxaBuffSem);
538 return (-ENOMEM);
540 /* This test is guarded by the BuffSem so no longer needed
541 delete me in 2.5 */
542 if (moxaXmitBuff)
543 free_page(page);
544 else
545 moxaXmitBuff = (unsigned char *) page;
547 up(&moxaBuffSem);
549 ch = &moxaChannels[port];
550 ch->count++;
551 tty->driver_data = ch;
552 ch->tty = tty;
553 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
554 ch->statusflags = 0;
555 set_tty_param(tty);
556 MoxaPortLineCtrl(ch->port, 1, 1);
557 MoxaPortEnable(ch->port);
558 ch->asyncflags |= ASYNC_INITIALIZED;
560 retval = block_till_ready(tty, filp, ch);
562 moxa_unthrottle(tty);
564 if (ch->type == PORT_16550A) {
565 MoxaSetFifo(ch->port, 1);
566 } else {
567 MoxaSetFifo(ch->port, 0);
570 return (retval);
573 static void moxa_close(struct tty_struct *tty, struct file *filp)
575 struct moxa_str *ch;
576 int port;
578 port = PORTNO(tty);
579 if (port == MAX_PORTS) {
580 return;
582 if (!MoxaPortIsValid(port)) {
583 #ifdef SERIAL_DEBUG_CLOSE
584 printk("Invalid portno in moxa_close\n");
585 #endif
586 tty->driver_data = NULL;
587 return;
589 if (tty->driver_data == NULL) {
590 return;
592 if (tty_hung_up_p(filp)) {
593 return;
595 ch = (struct moxa_str *) tty->driver_data;
597 if ((tty->count == 1) && (ch->count != 1)) {
598 printk("moxa_close: bad serial port count; tty->count is 1, "
599 "ch->count is %d\n", ch->count);
600 ch->count = 1;
602 if (--ch->count < 0) {
603 printk("moxa_close: bad serial port count, device=%s\n",
604 tty->name);
605 ch->count = 0;
607 if (ch->count) {
608 return;
610 ch->asyncflags |= ASYNC_CLOSING;
612 ch->cflag = tty->termios->c_cflag;
613 if (ch->asyncflags & ASYNC_INITIALIZED) {
614 setup_empty_event(tty);
615 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
616 moxaEmptyTimer_on[ch->port] = 0;
617 del_timer(&moxaEmptyTimer[ch->port]);
619 shut_down(ch);
620 MoxaPortFlushData(port, 2);
622 if (tty->driver->flush_buffer)
623 tty->driver->flush_buffer(tty);
624 tty_ldisc_flush(tty);
626 tty->closing = 0;
627 ch->event = 0;
628 ch->tty = NULL;
629 if (ch->blocked_open) {
630 if (ch->close_delay) {
631 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
633 wake_up_interruptible(&ch->open_wait);
635 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
636 wake_up_interruptible(&ch->close_wait);
639 static int moxa_write(struct tty_struct *tty,
640 const unsigned char *buf, int count)
642 struct moxa_str *ch;
643 int len, port;
644 unsigned long flags;
646 ch = (struct moxa_str *) tty->driver_data;
647 if (ch == NULL)
648 return (0);
649 port = ch->port;
651 spin_lock_irqsave(&moxa_lock, flags);
652 len = MoxaPortWriteData(port, (unsigned char *) buf, count);
653 spin_unlock_irqrestore(&moxa_lock, flags);
655 /*********************************************
656 if ( !(ch->statusflags & LOWWAIT) &&
657 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
658 ************************************************/
659 ch->statusflags |= LOWWAIT;
660 return (len);
663 static int moxa_write_room(struct tty_struct *tty)
665 struct moxa_str *ch;
667 if (tty->stopped)
668 return (0);
669 ch = (struct moxa_str *) tty->driver_data;
670 if (ch == NULL)
671 return (0);
672 return (MoxaPortTxFree(ch->port));
675 static void moxa_flush_buffer(struct tty_struct *tty)
677 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
679 if (ch == NULL)
680 return;
681 MoxaPortFlushData(ch->port, 1);
682 tty_wakeup(tty);
685 static int moxa_chars_in_buffer(struct tty_struct *tty)
687 int chars;
688 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
691 * Sigh...I have to check if driver_data is NULL here, because
692 * if an open() fails, the TTY subsystem eventually calls
693 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
694 * routine. And since the open() failed, we return 0 here. TDJ
696 if (ch == NULL)
697 return (0);
698 chars = MoxaPortTxQueue(ch->port);
699 if (chars) {
701 * Make it possible to wakeup anything waiting for output
702 * in tty_ioctl.c, etc.
704 if (!(ch->statusflags & EMPTYWAIT))
705 setup_empty_event(tty);
707 return (chars);
710 static void moxa_flush_chars(struct tty_struct *tty)
713 * Don't think I need this, because this is called to empty the TX
714 * buffer for the 16450, 16550, etc.
718 static void moxa_put_char(struct tty_struct *tty, unsigned char c)
720 struct moxa_str *ch;
721 int port;
722 unsigned long flags;
724 ch = (struct moxa_str *) tty->driver_data;
725 if (ch == NULL)
726 return;
727 port = ch->port;
728 spin_lock_irqsave(&moxa_lock, flags);
729 moxaXmitBuff[0] = c;
730 MoxaPortWriteData(port, moxaXmitBuff, 1);
731 spin_unlock_irqrestore(&moxa_lock, flags);
732 /************************************************
733 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
734 *************************************************/
735 ch->statusflags |= LOWWAIT;
738 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
740 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
741 int port;
742 int flag = 0, dtr, rts;
744 port = PORTNO(tty);
745 if ((port != MAX_PORTS) && (!ch))
746 return (-EINVAL);
748 MoxaPortGetLineOut(ch->port, &dtr, &rts);
749 if (dtr)
750 flag |= TIOCM_DTR;
751 if (rts)
752 flag |= TIOCM_RTS;
753 dtr = MoxaPortLineStatus(ch->port);
754 if (dtr & 1)
755 flag |= TIOCM_CTS;
756 if (dtr & 2)
757 flag |= TIOCM_DSR;
758 if (dtr & 4)
759 flag |= TIOCM_CD;
760 return flag;
763 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
764 unsigned int set, unsigned int clear)
766 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
767 int port;
768 int dtr, rts;
770 port = PORTNO(tty);
771 if ((port != MAX_PORTS) && (!ch))
772 return (-EINVAL);
774 MoxaPortGetLineOut(ch->port, &dtr, &rts);
775 if (set & TIOCM_RTS)
776 rts = 1;
777 if (set & TIOCM_DTR)
778 dtr = 1;
779 if (clear & TIOCM_RTS)
780 rts = 0;
781 if (clear & TIOCM_DTR)
782 dtr = 0;
783 MoxaPortLineCtrl(ch->port, dtr, rts);
784 return 0;
787 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
788 unsigned int cmd, unsigned long arg)
790 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
791 register int port;
792 void __user *argp = (void __user *)arg;
793 int retval;
795 port = PORTNO(tty);
796 if ((port != MAX_PORTS) && (!ch))
797 return (-EINVAL);
799 switch (cmd) {
800 case TCSBRK: /* SVID version: non-zero arg --> no break */
801 retval = tty_check_change(tty);
802 if (retval)
803 return (retval);
804 setup_empty_event(tty);
805 tty_wait_until_sent(tty, 0);
806 if (!arg)
807 MoxaPortSendBreak(ch->port, 0);
808 return (0);
809 case TCSBRKP: /* support for POSIX tcsendbreak() */
810 retval = tty_check_change(tty);
811 if (retval)
812 return (retval);
813 setup_empty_event(tty);
814 tty_wait_until_sent(tty, 0);
815 MoxaPortSendBreak(ch->port, arg);
816 return (0);
817 case TIOCGSOFTCAR:
818 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) argp);
819 case TIOCSSOFTCAR:
820 if(get_user(retval, (unsigned long __user *) argp))
821 return -EFAULT;
822 arg = retval;
823 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
824 (arg ? CLOCAL : 0));
825 if (C_CLOCAL(tty))
826 ch->asyncflags &= ~ASYNC_CHECK_CD;
827 else
828 ch->asyncflags |= ASYNC_CHECK_CD;
829 return (0);
830 case TIOCGSERIAL:
831 return moxa_get_serial_info(ch, argp);
833 case TIOCSSERIAL:
834 return moxa_set_serial_info(ch, argp);
835 default:
836 retval = MoxaDriverIoctl(cmd, arg, port);
838 return (retval);
841 static void moxa_throttle(struct tty_struct *tty)
843 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
845 ch->statusflags |= THROTTLE;
848 static void moxa_unthrottle(struct tty_struct *tty)
850 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
852 ch->statusflags &= ~THROTTLE;
855 static void moxa_set_termios(struct tty_struct *tty,
856 struct termios *old_termios)
858 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
860 if (ch == NULL)
861 return;
862 set_tty_param(tty);
863 if (!(old_termios->c_cflag & CLOCAL) &&
864 (tty->termios->c_cflag & CLOCAL))
865 wake_up_interruptible(&ch->open_wait);
868 static void moxa_stop(struct tty_struct *tty)
870 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
872 if (ch == NULL)
873 return;
874 MoxaPortTxDisable(ch->port);
875 ch->statusflags |= TXSTOPPED;
879 static void moxa_start(struct tty_struct *tty)
881 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
883 if (ch == NULL)
884 return;
886 if (!(ch->statusflags & TXSTOPPED))
887 return;
889 MoxaPortTxEnable(ch->port);
890 ch->statusflags &= ~TXSTOPPED;
893 static void moxa_hangup(struct tty_struct *tty)
895 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
897 moxa_flush_buffer(tty);
898 shut_down(ch);
899 ch->event = 0;
900 ch->count = 0;
901 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
902 ch->tty = NULL;
903 wake_up_interruptible(&ch->open_wait);
906 static void moxa_poll(unsigned long ignored)
908 register int card;
909 struct moxa_str *ch;
910 struct tty_struct *tp;
911 int i, ports;
913 moxaTimer_on = 0;
914 del_timer(&moxaTimer);
916 if (MoxaDriverPoll() < 0) {
917 moxaTimer.function = moxa_poll;
918 moxaTimer.expires = jiffies + (HZ / 50);
919 moxaTimer_on = 1;
920 add_timer(&moxaTimer);
921 return;
923 for (card = 0; card < MAX_BOARDS; card++) {
924 if ((ports = MoxaPortsOfCard(card)) <= 0)
925 continue;
926 ch = &moxaChannels[card * MAX_PORTS_PER_BOARD];
927 for (i = 0; i < ports; i++, ch++) {
928 if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
929 continue;
930 if (!(ch->statusflags & THROTTLE) &&
931 (MoxaPortRxQueue(ch->port) > 0))
932 receive_data(ch);
933 if ((tp = ch->tty) == 0)
934 continue;
935 if (ch->statusflags & LOWWAIT) {
936 if (MoxaPortTxQueue(ch->port) <= WAKEUP_CHARS) {
937 if (!tp->stopped) {
938 ch->statusflags &= ~LOWWAIT;
939 tty_wakeup(tp);
943 if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch->port) > 0)) {
944 tty_insert_flip_char(tp, 0, TTY_BREAK);
945 tty_schedule_flip(tp);
947 if (MoxaPortDCDChange(ch->port)) {
948 if (ch->asyncflags & ASYNC_CHECK_CD) {
949 if (MoxaPortDCDON(ch->port))
950 wake_up_interruptible(&ch->open_wait);
951 else {
952 set_bit(MOXA_EVENT_HANGUP, &ch->event);
953 schedule_work(&ch->tqueue);
960 moxaTimer.function = moxa_poll;
961 moxaTimer.expires = jiffies + (HZ / 50);
962 moxaTimer_on = 1;
963 add_timer(&moxaTimer);
966 /******************************************************************************/
968 static void set_tty_param(struct tty_struct *tty)
970 register struct termios *ts;
971 struct moxa_str *ch;
972 int rts, cts, txflow, rxflow, xany;
974 ch = (struct moxa_str *) tty->driver_data;
975 ts = tty->termios;
976 if (ts->c_cflag & CLOCAL)
977 ch->asyncflags &= ~ASYNC_CHECK_CD;
978 else
979 ch->asyncflags |= ASYNC_CHECK_CD;
980 rts = cts = txflow = rxflow = xany = 0;
981 if (ts->c_cflag & CRTSCTS)
982 rts = cts = 1;
983 if (ts->c_iflag & IXON)
984 txflow = 1;
985 if (ts->c_iflag & IXOFF)
986 rxflow = 1;
987 if (ts->c_iflag & IXANY)
988 xany = 1;
989 MoxaPortFlowCtrl(ch->port, rts, cts, txflow, rxflow, xany);
990 MoxaPortSetTermio(ch->port, ts);
993 static int block_till_ready(struct tty_struct *tty, struct file *filp,
994 struct moxa_str *ch)
996 DECLARE_WAITQUEUE(wait,current);
997 unsigned long flags;
998 int retval;
999 int do_clocal = C_CLOCAL(tty);
1002 * If the device is in the middle of being closed, then block
1003 * until it's done, and then try again.
1005 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
1006 if (ch->asyncflags & ASYNC_CLOSING)
1007 interruptible_sleep_on(&ch->close_wait);
1008 #ifdef SERIAL_DO_RESTART
1009 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1010 return (-EAGAIN);
1011 else
1012 return (-ERESTARTSYS);
1013 #else
1014 return (-EAGAIN);
1015 #endif
1018 * If non-blocking mode is set, then make the check up front
1019 * and then exit.
1021 if (filp->f_flags & O_NONBLOCK) {
1022 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1023 return (0);
1026 * Block waiting for the carrier detect and the line to become free
1028 retval = 0;
1029 add_wait_queue(&ch->open_wait, &wait);
1030 #ifdef SERIAL_DEBUG_OPEN
1031 printk("block_til_ready before block: ttys%d, count = %d\n",
1032 ch->line, ch->count);
1033 #endif
1034 spin_lock_irqsave(&moxa_lock, flags);
1035 if (!tty_hung_up_p(filp))
1036 ch->count--;
1037 ch->blocked_open++;
1038 spin_unlock_irqrestore(&moxa_lock, flags);
1040 while (1) {
1041 set_current_state(TASK_INTERRUPTIBLE);
1042 if (tty_hung_up_p(filp) ||
1043 !(ch->asyncflags & ASYNC_INITIALIZED)) {
1044 #ifdef SERIAL_DO_RESTART
1045 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1046 retval = -EAGAIN;
1047 else
1048 retval = -ERESTARTSYS;
1049 #else
1050 retval = -EAGAIN;
1051 #endif
1052 break;
1054 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
1055 MoxaPortDCDON(ch->port)))
1056 break;
1058 if (signal_pending(current)) {
1059 retval = -ERESTARTSYS;
1060 break;
1062 schedule();
1064 set_current_state(TASK_RUNNING);
1065 remove_wait_queue(&ch->open_wait, &wait);
1067 spin_lock_irqsave(&moxa_lock, flags);
1068 if (!tty_hung_up_p(filp))
1069 ch->count++;
1070 ch->blocked_open--;
1071 spin_unlock_irqrestore(&moxa_lock, flags);
1072 #ifdef SERIAL_DEBUG_OPEN
1073 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1074 ch->line, ch->count);
1075 #endif
1076 if (retval)
1077 return (retval);
1078 /* FIXME: review to see if we need to use set_bit on these */
1079 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1080 return 0;
1083 static void setup_empty_event(struct tty_struct *tty)
1085 struct moxa_str *ch = tty->driver_data;
1086 unsigned long flags;
1088 spin_lock_irqsave(&moxa_lock, flags);
1089 ch->statusflags |= EMPTYWAIT;
1090 moxaEmptyTimer_on[ch->port] = 0;
1091 del_timer(&moxaEmptyTimer[ch->port]);
1092 moxaEmptyTimer[ch->port].expires = jiffies + HZ;
1093 moxaEmptyTimer_on[ch->port] = 1;
1094 add_timer(&moxaEmptyTimer[ch->port]);
1095 spin_unlock_irqrestore(&moxa_lock, flags);
1098 static void check_xmit_empty(unsigned long data)
1100 struct moxa_str *ch;
1102 ch = (struct moxa_str *) data;
1103 moxaEmptyTimer_on[ch->port] = 0;
1104 del_timer(&moxaEmptyTimer[ch->port]);
1105 if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
1106 if (MoxaPortTxQueue(ch->port) == 0) {
1107 ch->statusflags &= ~EMPTYWAIT;
1108 tty_wakeup(ch->tty);
1109 return;
1111 moxaEmptyTimer[ch->port].expires = jiffies + HZ;
1112 moxaEmptyTimer_on[ch->port] = 1;
1113 add_timer(&moxaEmptyTimer[ch->port]);
1114 } else
1115 ch->statusflags &= ~EMPTYWAIT;
1118 static void shut_down(struct moxa_str *ch)
1120 struct tty_struct *tp;
1122 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1123 return;
1125 tp = ch->tty;
1127 MoxaPortDisable(ch->port);
1130 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1132 if (tp->termios->c_cflag & HUPCL)
1133 MoxaPortLineCtrl(ch->port, 0, 0);
1135 ch->asyncflags &= ~ASYNC_INITIALIZED;
1138 static void receive_data(struct moxa_str *ch)
1140 struct tty_struct *tp;
1141 struct termios *ts;
1142 unsigned long flags;
1144 ts = NULL;
1145 tp = ch->tty;
1146 if (tp)
1147 ts = tp->termios;
1148 /**************************************************
1149 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1150 *****************************************************/
1151 if (!tp || !ts) {
1152 MoxaPortFlushData(ch->port, 0);
1153 return;
1155 spin_lock_irqsave(&moxa_lock, flags);
1156 MoxaPortReadData(ch->port, tp);
1157 spin_unlock_irqrestore(&moxa_lock, flags);
1158 tty_schedule_flip(tp);
1161 #define Magic_code 0x404
1164 * System Configuration
1167 * for C218 BIOS initialization
1169 #define C218_ConfBase 0x800
1170 #define C218_status (C218_ConfBase + 0) /* BIOS running status */
1171 #define C218_diag (C218_ConfBase + 2) /* diagnostic status */
1172 #define C218_key (C218_ConfBase + 4) /* WORD (0x218 for C218) */
1173 #define C218DLoad_len (C218_ConfBase + 6) /* WORD */
1174 #define C218check_sum (C218_ConfBase + 8) /* BYTE */
1175 #define C218chksum_ok (C218_ConfBase + 0x0a) /* BYTE (1:ok) */
1176 #define C218_TestRx (C218_ConfBase + 0x10) /* 8 bytes for 8 ports */
1177 #define C218_TestTx (C218_ConfBase + 0x18) /* 8 bytes for 8 ports */
1178 #define C218_RXerr (C218_ConfBase + 0x20) /* 8 bytes for 8 ports */
1179 #define C218_ErrFlag (C218_ConfBase + 0x28) /* 8 bytes for 8 ports */
1181 #define C218_LoadBuf 0x0F00
1182 #define C218_KeyCode 0x218
1183 #define CP204J_KeyCode 0x204
1186 * for C320 BIOS initialization
1188 #define C320_ConfBase 0x800
1189 #define C320_LoadBuf 0x0f00
1190 #define STS_init 0x05 /* for C320_status */
1192 #define C320_status C320_ConfBase + 0 /* BIOS running status */
1193 #define C320_diag C320_ConfBase + 2 /* diagnostic status */
1194 #define C320_key C320_ConfBase + 4 /* WORD (0320H for C320) */
1195 #define C320DLoad_len C320_ConfBase + 6 /* WORD */
1196 #define C320check_sum C320_ConfBase + 8 /* WORD */
1197 #define C320chksum_ok C320_ConfBase + 0x0a /* WORD (1:ok) */
1198 #define C320bapi_len C320_ConfBase + 0x0c /* WORD */
1199 #define C320UART_no C320_ConfBase + 0x0e /* WORD */
1201 #define C320_KeyCode 0x320
1203 #define FixPage_addr 0x0000 /* starting addr of static page */
1204 #define DynPage_addr 0x2000 /* starting addr of dynamic page */
1205 #define C218_start 0x3000 /* starting addr of C218 BIOS prg */
1206 #define Control_reg 0x1ff0 /* select page and reset control */
1207 #define HW_reset 0x80
1210 * Function Codes
1212 #define FC_CardReset 0x80
1213 #define FC_ChannelReset 1 /* C320 firmware not supported */
1214 #define FC_EnableCH 2
1215 #define FC_DisableCH 3
1216 #define FC_SetParam 4
1217 #define FC_SetMode 5
1218 #define FC_SetRate 6
1219 #define FC_LineControl 7
1220 #define FC_LineStatus 8
1221 #define FC_XmitControl 9
1222 #define FC_FlushQueue 10
1223 #define FC_SendBreak 11
1224 #define FC_StopBreak 12
1225 #define FC_LoopbackON 13
1226 #define FC_LoopbackOFF 14
1227 #define FC_ClrIrqTable 15
1228 #define FC_SendXon 16
1229 #define FC_SetTermIrq 17 /* C320 firmware not supported */
1230 #define FC_SetCntIrq 18 /* C320 firmware not supported */
1231 #define FC_SetBreakIrq 19
1232 #define FC_SetLineIrq 20
1233 #define FC_SetFlowCtl 21
1234 #define FC_GenIrq 22
1235 #define FC_InCD180 23
1236 #define FC_OutCD180 24
1237 #define FC_InUARTreg 23
1238 #define FC_OutUARTreg 24
1239 #define FC_SetXonXoff 25
1240 #define FC_OutCD180CCR 26
1241 #define FC_ExtIQueue 27
1242 #define FC_ExtOQueue 28
1243 #define FC_ClrLineIrq 29
1244 #define FC_HWFlowCtl 30
1245 #define FC_GetClockRate 35
1246 #define FC_SetBaud 36
1247 #define FC_SetDataMode 41
1248 #define FC_GetCCSR 43
1249 #define FC_GetDataError 45
1250 #define FC_RxControl 50
1251 #define FC_ImmSend 51
1252 #define FC_SetXonState 52
1253 #define FC_SetXoffState 53
1254 #define FC_SetRxFIFOTrig 54
1255 #define FC_SetTxFIFOCnt 55
1256 #define FC_UnixRate 56
1257 #define FC_UnixResetTimer 57
1259 #define RxFIFOTrig1 0
1260 #define RxFIFOTrig4 1
1261 #define RxFIFOTrig8 2
1262 #define RxFIFOTrig14 3
1265 * Dual-Ported RAM
1267 #define DRAM_global 0
1268 #define INT_data (DRAM_global + 0)
1269 #define Config_base (DRAM_global + 0x108)
1271 #define IRQindex (INT_data + 0)
1272 #define IRQpending (INT_data + 4)
1273 #define IRQtable (INT_data + 8)
1276 * Interrupt Status
1278 #define IntrRx 0x01 /* receiver data O.K. */
1279 #define IntrTx 0x02 /* transmit buffer empty */
1280 #define IntrFunc 0x04 /* function complete */
1281 #define IntrBreak 0x08 /* received break */
1282 #define IntrLine 0x10 /* line status change
1283 for transmitter */
1284 #define IntrIntr 0x20 /* received INTR code */
1285 #define IntrQuit 0x40 /* received QUIT code */
1286 #define IntrEOF 0x80 /* received EOF code */
1288 #define IntrRxTrigger 0x100 /* rx data count reach tigger value */
1289 #define IntrTxTrigger 0x200 /* tx data count below trigger value */
1291 #define Magic_no (Config_base + 0)
1292 #define Card_model_no (Config_base + 2)
1293 #define Total_ports (Config_base + 4)
1294 #define Module_cnt (Config_base + 8)
1295 #define Module_no (Config_base + 10)
1296 #define Timer_10ms (Config_base + 14)
1297 #define Disable_IRQ (Config_base + 20)
1298 #define TMS320_PORT1 (Config_base + 22)
1299 #define TMS320_PORT2 (Config_base + 24)
1300 #define TMS320_CLOCK (Config_base + 26)
1303 * DATA BUFFER in DRAM
1305 #define Extern_table 0x400 /* Base address of the external table
1306 (24 words * 64) total 3K bytes
1307 (24 words * 128) total 6K bytes */
1308 #define Extern_size 0x60 /* 96 bytes */
1309 #define RXrptr 0x00 /* read pointer for RX buffer */
1310 #define RXwptr 0x02 /* write pointer for RX buffer */
1311 #define TXrptr 0x04 /* read pointer for TX buffer */
1312 #define TXwptr 0x06 /* write pointer for TX buffer */
1313 #define HostStat 0x08 /* IRQ flag and general flag */
1314 #define FlagStat 0x0A
1315 #define FlowControl 0x0C /* B7 B6 B5 B4 B3 B2 B1 B0 */
1316 /* x x x x | | | | */
1317 /* | | | + CTS flow */
1318 /* | | +--- RTS flow */
1319 /* | +------ TX Xon/Xoff */
1320 /* +--------- RX Xon/Xoff */
1321 #define Break_cnt 0x0E /* received break count */
1322 #define CD180TXirq 0x10 /* if non-0: enable TX irq */
1323 #define RX_mask 0x12
1324 #define TX_mask 0x14
1325 #define Ofs_rxb 0x16
1326 #define Ofs_txb 0x18
1327 #define Page_rxb 0x1A
1328 #define Page_txb 0x1C
1329 #define EndPage_rxb 0x1E
1330 #define EndPage_txb 0x20
1331 #define Data_error 0x22
1332 #define RxTrigger 0x28
1333 #define TxTrigger 0x2a
1335 #define rRXwptr 0x34
1336 #define Low_water 0x36
1338 #define FuncCode 0x40
1339 #define FuncArg 0x42
1340 #define FuncArg1 0x44
1342 #define C218rx_size 0x2000 /* 8K bytes */
1343 #define C218tx_size 0x8000 /* 32K bytes */
1345 #define C218rx_mask (C218rx_size - 1)
1346 #define C218tx_mask (C218tx_size - 1)
1348 #define C320p8rx_size 0x2000
1349 #define C320p8tx_size 0x8000
1350 #define C320p8rx_mask (C320p8rx_size - 1)
1351 #define C320p8tx_mask (C320p8tx_size - 1)
1353 #define C320p16rx_size 0x2000
1354 #define C320p16tx_size 0x4000
1355 #define C320p16rx_mask (C320p16rx_size - 1)
1356 #define C320p16tx_mask (C320p16tx_size - 1)
1358 #define C320p24rx_size 0x2000
1359 #define C320p24tx_size 0x2000
1360 #define C320p24rx_mask (C320p24rx_size - 1)
1361 #define C320p24tx_mask (C320p24tx_size - 1)
1363 #define C320p32rx_size 0x1000
1364 #define C320p32tx_size 0x1000
1365 #define C320p32rx_mask (C320p32rx_size - 1)
1366 #define C320p32tx_mask (C320p32tx_size - 1)
1368 #define Page_size 0x2000
1369 #define Page_mask (Page_size - 1)
1370 #define C218rx_spage 3
1371 #define C218tx_spage 4
1372 #define C218rx_pageno 1
1373 #define C218tx_pageno 4
1374 #define C218buf_pageno 5
1376 #define C320p8rx_spage 3
1377 #define C320p8tx_spage 4
1378 #define C320p8rx_pgno 1
1379 #define C320p8tx_pgno 4
1380 #define C320p8buf_pgno 5
1382 #define C320p16rx_spage 3
1383 #define C320p16tx_spage 4
1384 #define C320p16rx_pgno 1
1385 #define C320p16tx_pgno 2
1386 #define C320p16buf_pgno 3
1388 #define C320p24rx_spage 3
1389 #define C320p24tx_spage 4
1390 #define C320p24rx_pgno 1
1391 #define C320p24tx_pgno 1
1392 #define C320p24buf_pgno 2
1394 #define C320p32rx_spage 3
1395 #define C320p32tx_ofs C320p32rx_size
1396 #define C320p32tx_spage 3
1397 #define C320p32buf_pgno 1
1400 * Host Status
1402 #define WakeupRx 0x01
1403 #define WakeupTx 0x02
1404 #define WakeupBreak 0x08
1405 #define WakeupLine 0x10
1406 #define WakeupIntr 0x20
1407 #define WakeupQuit 0x40
1408 #define WakeupEOF 0x80 /* used in VTIME control */
1409 #define WakeupRxTrigger 0x100
1410 #define WakeupTxTrigger 0x200
1412 * Flag status
1414 #define Rx_over 0x01
1415 #define Xoff_state 0x02
1416 #define Tx_flowOff 0x04
1417 #define Tx_enable 0x08
1418 #define CTS_state 0x10
1419 #define DSR_state 0x20
1420 #define DCD_state 0x80
1422 * FlowControl
1424 #define CTS_FlowCtl 1
1425 #define RTS_FlowCtl 2
1426 #define Tx_FlowCtl 4
1427 #define Rx_FlowCtl 8
1428 #define IXM_IXANY 0x10
1430 #define LowWater 128
1432 #define DTR_ON 1
1433 #define RTS_ON 2
1434 #define CTS_ON 1
1435 #define DSR_ON 2
1436 #define DCD_ON 8
1438 /* mode definition */
1439 #define MX_CS8 0x03
1440 #define MX_CS7 0x02
1441 #define MX_CS6 0x01
1442 #define MX_CS5 0x00
1444 #define MX_STOP1 0x00
1445 #define MX_STOP15 0x04
1446 #define MX_STOP2 0x08
1448 #define MX_PARNONE 0x00
1449 #define MX_PAREVEN 0x40
1450 #define MX_PARODD 0xC0
1453 * Query
1455 #define QueryPort MAX_PORTS
1459 struct mon_str {
1460 int tick;
1461 int rxcnt[MAX_PORTS];
1462 int txcnt[MAX_PORTS];
1464 typedef struct mon_str mon_st;
1466 #define DCD_changed 0x01
1467 #define DCD_oldstate 0x80
1469 static unsigned char moxaBuff[10240];
1470 static void __iomem *moxaIntNdx[MAX_BOARDS];
1471 static void __iomem *moxaIntPend[MAX_BOARDS];
1472 static void __iomem *moxaIntTable[MAX_BOARDS];
1473 static char moxaChkPort[MAX_PORTS];
1474 static char moxaLineCtrl[MAX_PORTS];
1475 static void __iomem *moxaTableAddr[MAX_PORTS];
1476 static long moxaCurBaud[MAX_PORTS];
1477 static char moxaDCDState[MAX_PORTS];
1478 static char moxaLowChkFlag[MAX_PORTS];
1479 static int moxaLowWaterChk;
1480 static int moxaCard;
1481 static mon_st moxaLog;
1482 static int moxaFuncTout;
1483 static ushort moxaBreakCnt[MAX_PORTS];
1485 static void moxadelay(int);
1486 static void moxafunc(void __iomem *, int, ushort);
1487 static void wait_finish(void __iomem *);
1488 static void low_water_check(void __iomem *);
1489 static int moxaloadbios(int, unsigned char __user *, int);
1490 static int moxafindcard(int);
1491 static int moxaload320b(int, unsigned char __user *, int);
1492 static int moxaloadcode(int, unsigned char __user *, int);
1493 static int moxaloadc218(int, void __iomem *, int);
1494 static int moxaloadc320(int, void __iomem *, int, int *);
1496 /*****************************************************************************
1497 * Driver level functions: *
1498 * 1. MoxaDriverInit(void); *
1499 * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
1500 * 3. MoxaDriverPoll(void); *
1501 *****************************************************************************/
1502 void MoxaDriverInit(void)
1504 int i;
1506 moxaFuncTout = HZ / 2; /* 500 mini-seconds */
1507 moxaCard = 0;
1508 moxaLog.tick = 0;
1509 moxaLowWaterChk = 0;
1510 for (i = 0; i < MAX_PORTS; i++) {
1511 moxaChkPort[i] = 0;
1512 moxaLowChkFlag[i] = 0;
1513 moxaLineCtrl[i] = 0;
1514 moxaLog.rxcnt[i] = 0;
1515 moxaLog.txcnt[i] = 0;
1519 #define MOXA 0x400
1520 #define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1521 #define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
1522 #define MOXA_INIT_DRIVER (MOXA + 6) /* moxaCard=0 */
1523 #define MOXA_LOAD_BIOS (MOXA + 9) /* download BIOS */
1524 #define MOXA_FIND_BOARD (MOXA + 10) /* Check if MOXA card exist? */
1525 #define MOXA_LOAD_C320B (MOXA + 11) /* download 320B firmware */
1526 #define MOXA_LOAD_CODE (MOXA + 12) /* download firmware */
1527 #define MOXA_GETDATACOUNT (MOXA + 23)
1528 #define MOXA_GET_IOQUEUE (MOXA + 27)
1529 #define MOXA_FLUSH_QUEUE (MOXA + 28)
1530 #define MOXA_GET_CONF (MOXA + 35) /* configuration */
1531 #define MOXA_GET_MAJOR (MOXA + 63)
1532 #define MOXA_GET_CUMAJOR (MOXA + 64)
1533 #define MOXA_GETMSTATUS (MOXA + 65)
1536 struct moxaq_str {
1537 int inq;
1538 int outq;
1541 struct dl_str {
1542 char __user *buf;
1543 int len;
1544 int cardno;
1547 static struct moxaq_str temp_queue[MAX_PORTS];
1548 static struct dl_str dltmp;
1550 void MoxaPortFlushData(int port, int mode)
1552 void __iomem *ofsAddr;
1553 if ((mode < 0) || (mode > 2))
1554 return;
1555 ofsAddr = moxaTableAddr[port];
1556 moxafunc(ofsAddr, FC_FlushQueue, mode);
1557 if (mode != 1) {
1558 moxaLowChkFlag[port] = 0;
1559 low_water_check(ofsAddr);
1563 int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port)
1565 int i;
1566 int status;
1567 int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
1568 void __user *argp = (void __user *)arg;
1570 if (port == QueryPort) {
1571 if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_INIT_DRIVER) &&
1572 (cmd != MOXA_LOAD_BIOS) && (cmd != MOXA_FIND_BOARD) && (cmd != MOXA_LOAD_C320B) &&
1573 (cmd != MOXA_LOAD_CODE) && (cmd != MOXA_GETDATACOUNT) &&
1574 (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
1575 (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
1576 return (-EINVAL);
1578 switch (cmd) {
1579 case MOXA_GET_CONF:
1580 if(copy_to_user(argp, &moxa_boards, MAX_BOARDS * sizeof(moxa_board_conf)))
1581 return -EFAULT;
1582 return (0);
1583 case MOXA_INIT_DRIVER:
1584 if ((int) arg == 0x404)
1585 MoxaDriverInit();
1586 return (0);
1587 case MOXA_GETDATACOUNT:
1588 moxaLog.tick = jiffies;
1589 if(copy_to_user(argp, &moxaLog, sizeof(mon_st)))
1590 return -EFAULT;
1591 return (0);
1592 case MOXA_FLUSH_QUEUE:
1593 MoxaPortFlushData(port, arg);
1594 return (0);
1595 case MOXA_GET_IOQUEUE:
1596 for (i = 0; i < MAX_PORTS; i++) {
1597 if (moxaChkPort[i]) {
1598 temp_queue[i].inq = MoxaPortRxQueue(i);
1599 temp_queue[i].outq = MoxaPortTxQueue(i);
1602 if(copy_to_user(argp, temp_queue, sizeof(struct moxaq_str) * MAX_PORTS))
1603 return -EFAULT;
1604 return (0);
1605 case MOXA_GET_OQUEUE:
1606 i = MoxaPortTxQueue(port);
1607 return put_user(i, (unsigned long __user *)argp);
1608 case MOXA_GET_IQUEUE:
1609 i = MoxaPortRxQueue(port);
1610 return put_user(i, (unsigned long __user *)argp);
1611 case MOXA_GET_MAJOR:
1612 if(copy_to_user(argp, &ttymajor, sizeof(int)))
1613 return -EFAULT;
1614 return 0;
1615 case MOXA_GET_CUMAJOR:
1616 i = 0;
1617 if(copy_to_user(argp, &i, sizeof(int)))
1618 return -EFAULT;
1619 return 0;
1620 case MOXA_GETMSTATUS:
1621 for (i = 0; i < MAX_PORTS; i++) {
1622 GMStatus[i].ri = 0;
1623 GMStatus[i].dcd = 0;
1624 GMStatus[i].dsr = 0;
1625 GMStatus[i].cts = 0;
1626 if (!moxaChkPort[i]) {
1627 continue;
1628 } else {
1629 status = MoxaPortLineStatus(moxaChannels[i].port);
1630 if (status & 1)
1631 GMStatus[i].cts = 1;
1632 if (status & 2)
1633 GMStatus[i].dsr = 1;
1634 if (status & 4)
1635 GMStatus[i].dcd = 1;
1638 if (!moxaChannels[i].tty || !moxaChannels[i].tty->termios)
1639 GMStatus[i].cflag = moxaChannels[i].cflag;
1640 else
1641 GMStatus[i].cflag = moxaChannels[i].tty->termios->c_cflag;
1643 if(copy_to_user(argp, GMStatus, sizeof(struct mxser_mstatus) * MAX_PORTS))
1644 return -EFAULT;
1645 return 0;
1646 default:
1647 return (-ENOIOCTLCMD);
1648 case MOXA_LOAD_BIOS:
1649 case MOXA_FIND_BOARD:
1650 case MOXA_LOAD_C320B:
1651 case MOXA_LOAD_CODE:
1652 if (!capable(CAP_SYS_RAWIO))
1653 return -EPERM;
1654 break;
1657 if(copy_from_user(&dltmp, argp, sizeof(struct dl_str)))
1658 return -EFAULT;
1659 if(dltmp.cardno < 0 || dltmp.cardno >= MAX_BOARDS)
1660 return -EINVAL;
1662 switch(cmd)
1664 case MOXA_LOAD_BIOS:
1665 i = moxaloadbios(dltmp.cardno, dltmp.buf, dltmp.len);
1666 return (i);
1667 case MOXA_FIND_BOARD:
1668 return moxafindcard(dltmp.cardno);
1669 case MOXA_LOAD_C320B:
1670 moxaload320b(dltmp.cardno, dltmp.buf, dltmp.len);
1671 default: /* to keep gcc happy */
1672 return (0);
1673 case MOXA_LOAD_CODE:
1674 i = moxaloadcode(dltmp.cardno, dltmp.buf, dltmp.len);
1675 if (i == -1)
1676 return (-EFAULT);
1677 return (i);
1682 int MoxaDriverPoll(void)
1684 register ushort temp;
1685 register int card;
1686 void __iomem *ofsAddr;
1687 void __iomem *ip;
1688 int port, p, ports;
1690 if (moxaCard == 0)
1691 return (-1);
1692 for (card = 0; card < MAX_BOARDS; card++) {
1693 if ((ports = moxa_boards[card].numPorts) == 0)
1694 continue;
1695 if (readb(moxaIntPend[card]) == 0xff) {
1696 ip = moxaIntTable[card] + readb(moxaIntNdx[card]);
1697 p = card * MAX_PORTS_PER_BOARD;
1698 ports <<= 1;
1699 for (port = 0; port < ports; port += 2, p++) {
1700 if ((temp = readw(ip + port)) != 0) {
1701 writew(0, ip + port);
1702 ofsAddr = moxaTableAddr[p];
1703 if (temp & IntrTx)
1704 writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
1705 if (temp & IntrBreak) {
1706 moxaBreakCnt[p]++;
1708 if (temp & IntrLine) {
1709 if (readb(ofsAddr + FlagStat) & DCD_state) {
1710 if ((moxaDCDState[p] & DCD_oldstate) == 0)
1711 moxaDCDState[p] = (DCD_oldstate |
1712 DCD_changed);
1713 } else {
1714 if (moxaDCDState[p] & DCD_oldstate)
1715 moxaDCDState[p] = DCD_changed;
1720 writeb(0, moxaIntPend[card]);
1722 if (moxaLowWaterChk) {
1723 p = card * MAX_PORTS_PER_BOARD;
1724 for (port = 0; port < ports; port++, p++) {
1725 if (moxaLowChkFlag[p]) {
1726 moxaLowChkFlag[p] = 0;
1727 ofsAddr = moxaTableAddr[p];
1728 low_water_check(ofsAddr);
1733 moxaLowWaterChk = 0;
1734 return (0);
1737 /*****************************************************************************
1738 * Card level function: *
1739 * 1. MoxaPortsOfCard(int cardno); *
1740 *****************************************************************************/
1741 int MoxaPortsOfCard(int cardno)
1744 if (moxa_boards[cardno].boardType == 0)
1745 return (0);
1746 return (moxa_boards[cardno].numPorts);
1749 /*****************************************************************************
1750 * Port level functions: *
1751 * 1. MoxaPortIsValid(int port); *
1752 * 2. MoxaPortEnable(int port); *
1753 * 3. MoxaPortDisable(int port); *
1754 * 4. MoxaPortGetMaxBaud(int port); *
1755 * 5. MoxaPortGetCurBaud(int port); *
1756 * 6. MoxaPortSetBaud(int port, long baud); *
1757 * 7. MoxaPortSetMode(int port, int databit, int stopbit, int parity); *
1758 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1759 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1760 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1761 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1762 * 12. MoxaPortLineStatus(int port); *
1763 * 13. MoxaPortDCDChange(int port); *
1764 * 14. MoxaPortDCDON(int port); *
1765 * 15. MoxaPortFlushData(int port, int mode); *
1766 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1767 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
1768 * 18. MoxaPortTxBufSize(int port); *
1769 * 19. MoxaPortRxBufSize(int port); *
1770 * 20. MoxaPortTxQueue(int port); *
1771 * 21. MoxaPortTxFree(int port); *
1772 * 22. MoxaPortRxQueue(int port); *
1773 * 23. MoxaPortRxFree(int port); *
1774 * 24. MoxaPortTxDisable(int port); *
1775 * 25. MoxaPortTxEnable(int port); *
1776 * 26. MoxaPortGetBrkCnt(int port); *
1777 * 27. MoxaPortResetBrkCnt(int port); *
1778 * 28. MoxaPortSetXonXoff(int port, int xonValue, int xoffValue); *
1779 * 29. MoxaPortIsTxHold(int port); *
1780 * 30. MoxaPortSendBreak(int port, int ticks); *
1781 *****************************************************************************/
1783 * Moxa Port Number Description:
1785 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1786 * the port number using in MOXA driver functions will be 0 to 31 for
1787 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1788 * to 127 for fourth. For example, if you setup three MOXA boards,
1789 * first board is C218, second board is C320-16 and third board is
1790 * C320-32. The port number of first board (C218 - 8 ports) is from
1791 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1792 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1793 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1794 * 127 will be invalid.
1797 * Moxa Functions Description:
1799 * Function 1: Driver initialization routine, this routine must be
1800 * called when initialized driver.
1801 * Syntax:
1802 * void MoxaDriverInit();
1805 * Function 2: Moxa driver private IOCTL command processing.
1806 * Syntax:
1807 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1809 * unsigned int cmd : IOCTL command
1810 * unsigned long arg : IOCTL argument
1811 * int port : port number (0 - 127)
1813 * return: 0 (OK)
1814 * -EINVAL
1815 * -ENOIOCTLCMD
1818 * Function 3: Moxa driver polling process routine.
1819 * Syntax:
1820 * int MoxaDriverPoll(void);
1822 * return: 0 ; polling O.K.
1823 * -1 : no any Moxa card.
1826 * Function 4: Get the ports of this card.
1827 * Syntax:
1828 * int MoxaPortsOfCard(int cardno);
1830 * int cardno : card number (0 - 3)
1832 * return: 0 : this card is invalid
1833 * 8/16/24/32
1836 * Function 5: Check this port is valid or invalid
1837 * Syntax:
1838 * int MoxaPortIsValid(int port);
1839 * int port : port number (0 - 127, ref port description)
1841 * return: 0 : this port is invalid
1842 * 1 : this port is valid
1845 * Function 6: Enable this port to start Tx/Rx data.
1846 * Syntax:
1847 * void MoxaPortEnable(int port);
1848 * int port : port number (0 - 127)
1851 * Function 7: Disable this port
1852 * Syntax:
1853 * void MoxaPortDisable(int port);
1854 * int port : port number (0 - 127)
1857 * Function 8: Get the maximun available baud rate of this port.
1858 * Syntax:
1859 * long MoxaPortGetMaxBaud(int port);
1860 * int port : port number (0 - 127)
1862 * return: 0 : this port is invalid
1863 * 38400/57600/115200 bps
1866 * Function 9: Get the current baud rate of this port.
1867 * Syntax:
1868 * long MoxaPortGetCurBaud(int port);
1869 * int port : port number (0 - 127)
1871 * return: 0 : this port is invalid
1872 * 50 - 115200 bps
1875 * Function 10: Setting baud rate of this port.
1876 * Syntax:
1877 * long MoxaPortSetBaud(int port, long baud);
1878 * int port : port number (0 - 127)
1879 * long baud : baud rate (50 - 115200)
1881 * return: 0 : this port is invalid or baud < 50
1882 * 50 - 115200 : the real baud rate set to the port, if
1883 * the argument baud is large than maximun
1884 * available baud rate, the real setting
1885 * baud rate will be the maximun baud rate.
1888 * Function 11: Setting the data-bits/stop-bits/parity of this port
1889 * Syntax:
1890 * int MoxaPortSetMode(int port, int databits, int stopbits, int parity);
1891 * int port : port number (0 - 127)
1892 * int databits : data bits (8/7/6/5)
1893 * int stopbits : stop bits (2/1/0, 0 show 1.5 stop bits)
1894 int parity : parity (0:None,1:Odd,2:Even,3:Mark,4:Space)
1896 * return: -1 : invalid parameter
1897 * 0 : setting O.K.
1900 * Function 12: Configure the port.
1901 * Syntax:
1902 * int MoxaPortSetTermio(int port, struct termios *termio);
1903 * int port : port number (0 - 127)
1904 * struct termios * termio : termio structure pointer
1906 * return: -1 : this port is invalid or termio == NULL
1907 * 0 : setting O.K.
1910 * Function 13: Get the DTR/RTS state of this port.
1911 * Syntax:
1912 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1913 * int port : port number (0 - 127)
1914 * int * dtrState : pointer to INT to receive the current DTR
1915 * state. (if NULL, this function will not
1916 * write to this address)
1917 * int * rtsState : pointer to INT to receive the current RTS
1918 * state. (if NULL, this function will not
1919 * write to this address)
1921 * return: -1 : this port is invalid
1922 * 0 : O.K.
1925 * Function 14: Setting the DTR/RTS output state of this port.
1926 * Syntax:
1927 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1928 * int port : port number (0 - 127)
1929 * int dtrState : DTR output state (0: off, 1: on)
1930 * int rtsState : RTS output state (0: off, 1: on)
1933 * Function 15: Setting the flow control of this port.
1934 * Syntax:
1935 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1936 * int txFlow,int xany);
1937 * int port : port number (0 - 127)
1938 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1939 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1940 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1941 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1942 * int xany : S/W XANY flow control (0: no, 1: yes)
1945 * Function 16: Get ths line status of this port
1946 * Syntax:
1947 * int MoxaPortLineStatus(int port);
1948 * int port : port number (0 - 127)
1950 * return: Bit 0 - CTS state (0: off, 1: on)
1951 * Bit 1 - DSR state (0: off, 1: on)
1952 * Bit 2 - DCD state (0: off, 1: on)
1955 * Function 17: Check the DCD state has changed since the last read
1956 * of this function.
1957 * Syntax:
1958 * int MoxaPortDCDChange(int port);
1959 * int port : port number (0 - 127)
1961 * return: 0 : no changed
1962 * 1 : DCD has changed
1965 * Function 18: Check ths current DCD state is ON or not.
1966 * Syntax:
1967 * int MoxaPortDCDON(int port);
1968 * int port : port number (0 - 127)
1970 * return: 0 : DCD off
1971 * 1 : DCD on
1974 * Function 19: Flush the Rx/Tx buffer data of this port.
1975 * Syntax:
1976 * void MoxaPortFlushData(int port, int mode);
1977 * int port : port number (0 - 127)
1978 * int mode
1979 * 0 : flush the Rx buffer
1980 * 1 : flush the Tx buffer
1981 * 2 : flush the Rx and Tx buffer
1984 * Function 20: Write data.
1985 * Syntax:
1986 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1987 * int port : port number (0 - 127)
1988 * unsigned char * buffer : pointer to write data buffer.
1989 * int length : write data length
1991 * return: 0 - length : real write data length
1994 * Function 21: Read data.
1995 * Syntax:
1996 * int MoxaPortReadData(int port, struct tty_struct *tty);
1997 * int port : port number (0 - 127)
1998 * struct tty_struct *tty : tty for data
2000 * return: 0 - length : real read data length
2003 * Function 22: Get the Tx buffer size of this port
2004 * Syntax:
2005 * int MoxaPortTxBufSize(int port);
2006 * int port : port number (0 - 127)
2008 * return: .. : Tx buffer size
2011 * Function 23: Get the Rx buffer size of this port
2012 * Syntax:
2013 * int MoxaPortRxBufSize(int port);
2014 * int port : port number (0 - 127)
2016 * return: .. : Rx buffer size
2019 * Function 24: Get the Tx buffer current queued data bytes
2020 * Syntax:
2021 * int MoxaPortTxQueue(int port);
2022 * int port : port number (0 - 127)
2024 * return: .. : Tx buffer current queued data bytes
2027 * Function 25: Get the Tx buffer current free space
2028 * Syntax:
2029 * int MoxaPortTxFree(int port);
2030 * int port : port number (0 - 127)
2032 * return: .. : Tx buffer current free space
2035 * Function 26: Get the Rx buffer current queued data bytes
2036 * Syntax:
2037 * int MoxaPortRxQueue(int port);
2038 * int port : port number (0 - 127)
2040 * return: .. : Rx buffer current queued data bytes
2043 * Function 27: Get the Rx buffer current free space
2044 * Syntax:
2045 * int MoxaPortRxFree(int port);
2046 * int port : port number (0 - 127)
2048 * return: .. : Rx buffer current free space
2051 * Function 28: Disable port data transmission.
2052 * Syntax:
2053 * void MoxaPortTxDisable(int port);
2054 * int port : port number (0 - 127)
2057 * Function 29: Enable port data transmission.
2058 * Syntax:
2059 * void MoxaPortTxEnable(int port);
2060 * int port : port number (0 - 127)
2063 * Function 30: Get the received BREAK signal count.
2064 * Syntax:
2065 * int MoxaPortGetBrkCnt(int port);
2066 * int port : port number (0 - 127)
2068 * return: 0 - .. : BREAK signal count
2071 * Function 31: Get the received BREAK signal count and reset it.
2072 * Syntax:
2073 * int MoxaPortResetBrkCnt(int port);
2074 * int port : port number (0 - 127)
2076 * return: 0 - .. : BREAK signal count
2079 * Function 32: Set the S/W flow control new XON/XOFF value, default
2080 * XON is 0x11 & XOFF is 0x13.
2081 * Syntax:
2082 * void MoxaPortSetXonXoff(int port, int xonValue, int xoffValue);
2083 * int port : port number (0 - 127)
2084 * int xonValue : new XON value (0 - 255)
2085 * int xoffValue : new XOFF value (0 - 255)
2088 * Function 33: Check this port's transmission is hold by remote site
2089 * because the flow control.
2090 * Syntax:
2091 * int MoxaPortIsTxHold(int port);
2092 * int port : port number (0 - 127)
2094 * return: 0 : normal
2095 * 1 : hold by remote site
2098 * Function 34: Send out a BREAK signal.
2099 * Syntax:
2100 * void MoxaPortSendBreak(int port, int ms100);
2101 * int port : port number (0 - 127)
2102 * int ms100 : break signal time interval.
2103 * unit: 100 mini-second. if ms100 == 0, it will
2104 * send out a about 250 ms BREAK signal.
2107 int MoxaPortIsValid(int port)
2110 if (moxaCard == 0)
2111 return (0);
2112 if (moxaChkPort[port] == 0)
2113 return (0);
2114 return (1);
2117 void MoxaPortEnable(int port)
2119 void __iomem *ofsAddr;
2120 int MoxaPortLineStatus(int);
2121 short lowwater = 512;
2123 ofsAddr = moxaTableAddr[port];
2124 writew(lowwater, ofsAddr + Low_water);
2125 moxaBreakCnt[port] = 0;
2126 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2127 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2128 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
2129 } else {
2130 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
2133 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
2134 moxafunc(ofsAddr, FC_FlushQueue, 2);
2136 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
2137 MoxaPortLineStatus(port);
2140 void MoxaPortDisable(int port)
2142 void __iomem *ofsAddr = moxaTableAddr[port];
2144 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
2145 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
2146 writew(0, ofsAddr + HostStat);
2147 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
2150 long MoxaPortGetMaxBaud(int port)
2152 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2153 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI))
2154 return (460800L);
2155 else
2156 return (921600L);
2160 long MoxaPortSetBaud(int port, long baud)
2162 void __iomem *ofsAddr;
2163 long max, clock;
2164 unsigned int val;
2166 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
2167 return (0);
2168 ofsAddr = moxaTableAddr[port];
2169 if (baud > max)
2170 baud = max;
2171 if (max == 38400L)
2172 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
2173 else if (max == 57600L)
2174 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
2175 else
2176 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
2177 val = clock / baud;
2178 moxafunc(ofsAddr, FC_SetBaud, val);
2179 baud = clock / val;
2180 moxaCurBaud[port] = baud;
2181 return (baud);
2184 int MoxaPortSetTermio(int port, struct termios *termio)
2186 void __iomem *ofsAddr;
2187 tcflag_t cflag;
2188 long baud;
2189 tcflag_t mode = 0;
2191 if (moxaChkPort[port] == 0 || termio == 0)
2192 return (-1);
2193 ofsAddr = moxaTableAddr[port];
2194 cflag = termio->c_cflag; /* termio->c_cflag */
2196 mode = termio->c_cflag & CSIZE;
2197 if (mode == CS5)
2198 mode = MX_CS5;
2199 else if (mode == CS6)
2200 mode = MX_CS6;
2201 else if (mode == CS7)
2202 mode = MX_CS7;
2203 else if (mode == CS8)
2204 mode = MX_CS8;
2206 if (termio->c_cflag & CSTOPB) {
2207 if (mode == MX_CS5)
2208 mode |= MX_STOP15;
2209 else
2210 mode |= MX_STOP2;
2211 } else
2212 mode |= MX_STOP1;
2214 if (termio->c_cflag & PARENB) {
2215 if (termio->c_cflag & PARODD)
2216 mode |= MX_PARODD;
2217 else
2218 mode |= MX_PAREVEN;
2219 } else
2220 mode |= MX_PARNONE;
2222 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
2224 cflag &= (CBAUD | CBAUDEX);
2225 #ifndef B921600
2226 #define B921600 (B460800+1)
2227 #endif
2228 switch (cflag) {
2229 case B921600:
2230 baud = 921600L;
2231 break;
2232 case B460800:
2233 baud = 460800L;
2234 break;
2235 case B230400:
2236 baud = 230400L;
2237 break;
2238 case B115200:
2239 baud = 115200L;
2240 break;
2241 case B57600:
2242 baud = 57600L;
2243 break;
2244 case B38400:
2245 baud = 38400L;
2246 break;
2247 case B19200:
2248 baud = 19200L;
2249 break;
2250 case B9600:
2251 baud = 9600L;
2252 break;
2253 case B4800:
2254 baud = 4800L;
2255 break;
2256 case B2400:
2257 baud = 2400L;
2258 break;
2259 case B1800:
2260 baud = 1800L;
2261 break;
2262 case B1200:
2263 baud = 1200L;
2264 break;
2265 case B600:
2266 baud = 600L;
2267 break;
2268 case B300:
2269 baud = 300L;
2270 break;
2271 case B200:
2272 baud = 200L;
2273 break;
2274 case B150:
2275 baud = 150L;
2276 break;
2277 case B134:
2278 baud = 134L;
2279 break;
2280 case B110:
2281 baud = 110L;
2282 break;
2283 case B75:
2284 baud = 75L;
2285 break;
2286 case B50:
2287 baud = 50L;
2288 break;
2289 default:
2290 baud = 0;
2292 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2293 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2294 if (baud == 921600L)
2295 return (-1);
2297 MoxaPortSetBaud(port, baud);
2299 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
2300 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
2301 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
2302 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
2303 wait_finish(ofsAddr);
2306 return (0);
2309 int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState)
2312 if (!MoxaPortIsValid(port))
2313 return (-1);
2314 if (dtrState) {
2315 if (moxaLineCtrl[port] & DTR_ON)
2316 *dtrState = 1;
2317 else
2318 *dtrState = 0;
2320 if (rtsState) {
2321 if (moxaLineCtrl[port] & RTS_ON)
2322 *rtsState = 1;
2323 else
2324 *rtsState = 0;
2326 return (0);
2329 void MoxaPortLineCtrl(int port, int dtr, int rts)
2331 void __iomem *ofsAddr;
2332 int mode;
2334 ofsAddr = moxaTableAddr[port];
2335 mode = 0;
2336 if (dtr)
2337 mode |= DTR_ON;
2338 if (rts)
2339 mode |= RTS_ON;
2340 moxaLineCtrl[port] = mode;
2341 moxafunc(ofsAddr, FC_LineControl, mode);
2344 void MoxaPortFlowCtrl(int port, int rts, int cts, int txflow, int rxflow, int txany)
2346 void __iomem *ofsAddr;
2347 int mode;
2349 ofsAddr = moxaTableAddr[port];
2350 mode = 0;
2351 if (rts)
2352 mode |= RTS_FlowCtl;
2353 if (cts)
2354 mode |= CTS_FlowCtl;
2355 if (txflow)
2356 mode |= Tx_FlowCtl;
2357 if (rxflow)
2358 mode |= Rx_FlowCtl;
2359 if (txany)
2360 mode |= IXM_IXANY;
2361 moxafunc(ofsAddr, FC_SetFlowCtl, mode);
2364 int MoxaPortLineStatus(int port)
2366 void __iomem *ofsAddr;
2367 int val;
2369 ofsAddr = moxaTableAddr[port];
2370 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2371 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2372 moxafunc(ofsAddr, FC_LineStatus, 0);
2373 val = readw(ofsAddr + FuncArg);
2374 } else {
2375 val = readw(ofsAddr + FlagStat) >> 4;
2377 val &= 0x0B;
2378 if (val & 8) {
2379 val |= 4;
2380 if ((moxaDCDState[port] & DCD_oldstate) == 0)
2381 moxaDCDState[port] = (DCD_oldstate | DCD_changed);
2382 } else {
2383 if (moxaDCDState[port] & DCD_oldstate)
2384 moxaDCDState[port] = DCD_changed;
2386 val &= 7;
2387 return (val);
2390 int MoxaPortDCDChange(int port)
2392 int n;
2394 if (moxaChkPort[port] == 0)
2395 return (0);
2396 n = moxaDCDState[port];
2397 moxaDCDState[port] &= ~DCD_changed;
2398 n &= DCD_changed;
2399 return (n);
2402 int MoxaPortDCDON(int port)
2404 int n;
2406 if (moxaChkPort[port] == 0)
2407 return (0);
2408 if (moxaDCDState[port] & DCD_oldstate)
2409 n = 1;
2410 else
2411 n = 0;
2412 return (n);
2417 int MoxaDumpMem(int port, unsigned char * buffer, int len)
2419 int i;
2420 unsigned long baseAddr,ofsAddr,ofs;
2422 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2423 ofs = baseAddr + DynPage_addr + pageofs;
2424 if (len > 0x2000L)
2425 len = 0x2000L;
2426 for (i = 0; i < len; i++)
2427 buffer[i] = readb(ofs+i);
2432 int MoxaPortWriteData(int port, unsigned char * buffer, int len)
2434 int c, total, i;
2435 ushort tail;
2436 int cnt;
2437 ushort head, tx_mask, spage, epage;
2438 ushort pageno, pageofs, bufhead;
2439 void __iomem *baseAddr, *ofsAddr, *ofs;
2441 ofsAddr = moxaTableAddr[port];
2442 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2443 tx_mask = readw(ofsAddr + TX_mask);
2444 spage = readw(ofsAddr + Page_txb);
2445 epage = readw(ofsAddr + EndPage_txb);
2446 tail = readw(ofsAddr + TXwptr);
2447 head = readw(ofsAddr + TXrptr);
2448 c = (head > tail) ? (head - tail - 1)
2449 : (head - tail + tx_mask);
2450 if (c > len)
2451 c = len;
2452 moxaLog.txcnt[port] += c;
2453 total = c;
2454 if (spage == epage) {
2455 bufhead = readw(ofsAddr + Ofs_txb);
2456 writew(spage, baseAddr + Control_reg);
2457 while (c > 0) {
2458 if (head > tail)
2459 len = head - tail - 1;
2460 else
2461 len = tx_mask + 1 - tail;
2462 len = (c > len) ? len : c;
2463 ofs = baseAddr + DynPage_addr + bufhead + tail;
2464 for (i = 0; i < len; i++)
2465 writeb(*buffer++, ofs + i);
2466 tail = (tail + len) & tx_mask;
2467 c -= len;
2469 writew(tail, ofsAddr + TXwptr);
2470 } else {
2471 len = c;
2472 pageno = spage + (tail >> 13);
2473 pageofs = tail & Page_mask;
2474 do {
2475 cnt = Page_size - pageofs;
2476 if (cnt > c)
2477 cnt = c;
2478 c -= cnt;
2479 writeb(pageno, baseAddr + Control_reg);
2480 ofs = baseAddr + DynPage_addr + pageofs;
2481 for (i = 0; i < cnt; i++)
2482 writeb(*buffer++, ofs + i);
2483 if (c == 0) {
2484 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2485 break;
2487 if (++pageno == epage)
2488 pageno = spage;
2489 pageofs = 0;
2490 } while (1);
2492 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2493 return (total);
2496 int MoxaPortReadData(int port, struct tty_struct *tty)
2498 register ushort head, pageofs;
2499 int i, count, cnt, len, total, remain;
2500 ushort tail, rx_mask, spage, epage;
2501 ushort pageno, bufhead;
2502 void __iomem *baseAddr, *ofsAddr, *ofs;
2504 ofsAddr = moxaTableAddr[port];
2505 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2506 head = readw(ofsAddr + RXrptr);
2507 tail = readw(ofsAddr + RXwptr);
2508 rx_mask = readw(ofsAddr + RX_mask);
2509 spage = readw(ofsAddr + Page_rxb);
2510 epage = readw(ofsAddr + EndPage_rxb);
2511 count = (tail >= head) ? (tail - head)
2512 : (tail - head + rx_mask + 1);
2513 if (count == 0)
2514 return 0;
2516 total = count;
2517 remain = count - total;
2518 moxaLog.rxcnt[port] += total;
2519 count = total;
2520 if (spage == epage) {
2521 bufhead = readw(ofsAddr + Ofs_rxb);
2522 writew(spage, baseAddr + Control_reg);
2523 while (count > 0) {
2524 if (tail >= head)
2525 len = tail - head;
2526 else
2527 len = rx_mask + 1 - head;
2528 len = (count > len) ? len : count;
2529 ofs = baseAddr + DynPage_addr + bufhead + head;
2530 for (i = 0; i < len; i++)
2531 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2532 head = (head + len) & rx_mask;
2533 count -= len;
2535 writew(head, ofsAddr + RXrptr);
2536 } else {
2537 len = count;
2538 pageno = spage + (head >> 13);
2539 pageofs = head & Page_mask;
2540 do {
2541 cnt = Page_size - pageofs;
2542 if (cnt > count)
2543 cnt = count;
2544 count -= cnt;
2545 writew(pageno, baseAddr + Control_reg);
2546 ofs = baseAddr + DynPage_addr + pageofs;
2547 for (i = 0; i < cnt; i++)
2548 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2549 if (count == 0) {
2550 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2551 break;
2553 if (++pageno == epage)
2554 pageno = spage;
2555 pageofs = 0;
2556 } while (1);
2558 if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2559 moxaLowWaterChk = 1;
2560 moxaLowChkFlag[port] = 1;
2562 return (total);
2566 int MoxaPortTxQueue(int port)
2568 void __iomem *ofsAddr;
2569 ushort rptr, wptr, mask;
2570 int len;
2572 ofsAddr = moxaTableAddr[port];
2573 rptr = readw(ofsAddr + TXrptr);
2574 wptr = readw(ofsAddr + TXwptr);
2575 mask = readw(ofsAddr + TX_mask);
2576 len = (wptr - rptr) & mask;
2577 return (len);
2580 int MoxaPortTxFree(int port)
2582 void __iomem *ofsAddr;
2583 ushort rptr, wptr, mask;
2584 int len;
2586 ofsAddr = moxaTableAddr[port];
2587 rptr = readw(ofsAddr + TXrptr);
2588 wptr = readw(ofsAddr + TXwptr);
2589 mask = readw(ofsAddr + TX_mask);
2590 len = mask - ((wptr - rptr) & mask);
2591 return (len);
2594 int MoxaPortRxQueue(int port)
2596 void __iomem *ofsAddr;
2597 ushort rptr, wptr, mask;
2598 int len;
2600 ofsAddr = moxaTableAddr[port];
2601 rptr = readw(ofsAddr + RXrptr);
2602 wptr = readw(ofsAddr + RXwptr);
2603 mask = readw(ofsAddr + RX_mask);
2604 len = (wptr - rptr) & mask;
2605 return (len);
2609 void MoxaPortTxDisable(int port)
2611 void __iomem *ofsAddr;
2613 ofsAddr = moxaTableAddr[port];
2614 moxafunc(ofsAddr, FC_SetXoffState, Magic_code);
2617 void MoxaPortTxEnable(int port)
2619 void __iomem *ofsAddr;
2621 ofsAddr = moxaTableAddr[port];
2622 moxafunc(ofsAddr, FC_SetXonState, Magic_code);
2626 int MoxaPortResetBrkCnt(int port)
2628 ushort cnt;
2629 cnt = moxaBreakCnt[port];
2630 moxaBreakCnt[port] = 0;
2631 return (cnt);
2635 void MoxaPortSendBreak(int port, int ms100)
2637 void __iomem *ofsAddr;
2639 ofsAddr = moxaTableAddr[port];
2640 if (ms100) {
2641 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2642 moxadelay(ms100 * (HZ / 10));
2643 } else {
2644 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2645 moxadelay(HZ / 4); /* 250 ms */
2647 moxafunc(ofsAddr, FC_StopBreak, Magic_code);
2650 static int moxa_get_serial_info(struct moxa_str *info,
2651 struct serial_struct __user *retinfo)
2653 struct serial_struct tmp;
2655 memset(&tmp, 0, sizeof(tmp));
2656 tmp.type = info->type;
2657 tmp.line = info->port;
2658 tmp.port = 0;
2659 tmp.irq = 0;
2660 tmp.flags = info->asyncflags;
2661 tmp.baud_base = 921600;
2662 tmp.close_delay = info->close_delay;
2663 tmp.closing_wait = info->closing_wait;
2664 tmp.custom_divisor = 0;
2665 tmp.hub6 = 0;
2666 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2667 return -EFAULT;
2668 return (0);
2672 static int moxa_set_serial_info(struct moxa_str *info,
2673 struct serial_struct __user *new_info)
2675 struct serial_struct new_serial;
2677 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2678 return -EFAULT;
2680 if ((new_serial.irq != 0) ||
2681 (new_serial.port != 0) ||
2682 // (new_serial.type != info->type) ||
2683 (new_serial.custom_divisor != 0) ||
2684 (new_serial.baud_base != 921600))
2685 return (-EPERM);
2687 if (!capable(CAP_SYS_ADMIN)) {
2688 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2689 (info->asyncflags & ~ASYNC_USR_MASK)))
2690 return (-EPERM);
2691 } else {
2692 info->close_delay = new_serial.close_delay * HZ / 100;
2693 info->closing_wait = new_serial.closing_wait * HZ / 100;
2696 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2697 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2699 if (new_serial.type == PORT_16550A) {
2700 MoxaSetFifo(info->port, 1);
2701 } else {
2702 MoxaSetFifo(info->port, 0);
2705 info->type = new_serial.type;
2706 return (0);
2711 /*****************************************************************************
2712 * Static local functions: *
2713 *****************************************************************************/
2715 * moxadelay - delays a specified number ticks
2717 static void moxadelay(int tick)
2719 unsigned long st, et;
2721 st = jiffies;
2722 et = st + tick;
2723 while (time_before(jiffies, et));
2726 static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
2729 writew(arg, ofsAddr + FuncArg);
2730 writew(cmd, ofsAddr + FuncCode);
2731 wait_finish(ofsAddr);
2734 static void wait_finish(void __iomem *ofsAddr)
2736 unsigned long i, j;
2738 i = jiffies;
2739 while (readw(ofsAddr + FuncCode) != 0) {
2740 j = jiffies;
2741 if ((j - i) > moxaFuncTout) {
2742 return;
2747 static void low_water_check(void __iomem *ofsAddr)
2749 int len;
2750 ushort rptr, wptr, mask;
2752 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2753 rptr = readw(ofsAddr + RXrptr);
2754 wptr = readw(ofsAddr + RXwptr);
2755 mask = readw(ofsAddr + RX_mask);
2756 len = (wptr - rptr) & mask;
2757 if (len <= Low_water)
2758 moxafunc(ofsAddr, FC_SendXon, 0);
2762 static int moxaloadbios(int cardno, unsigned char __user *tmp, int len)
2764 void __iomem *baseAddr;
2765 int i;
2767 if(copy_from_user(moxaBuff, tmp, len))
2768 return -EFAULT;
2769 baseAddr = moxaBaseAddr[cardno];
2770 writeb(HW_reset, baseAddr + Control_reg); /* reset */
2771 moxadelay(1); /* delay 10 ms */
2772 for (i = 0; i < 4096; i++)
2773 writeb(0, baseAddr + i); /* clear fix page */
2774 for (i = 0; i < len; i++)
2775 writeb(moxaBuff[i], baseAddr + i); /* download BIOS */
2776 writeb(0, baseAddr + Control_reg); /* restart */
2777 return (0);
2780 static int moxafindcard(int cardno)
2782 void __iomem *baseAddr;
2783 ushort tmp;
2785 baseAddr = moxaBaseAddr[cardno];
2786 switch (moxa_boards[cardno].boardType) {
2787 case MOXA_BOARD_C218_ISA:
2788 case MOXA_BOARD_C218_PCI:
2789 if ((tmp = readw(baseAddr + C218_key)) != C218_KeyCode) {
2790 return (-1);
2792 break;
2793 case MOXA_BOARD_CP204J:
2794 if ((tmp = readw(baseAddr + C218_key)) != CP204J_KeyCode) {
2795 return (-1);
2797 break;
2798 default:
2799 if ((tmp = readw(baseAddr + C320_key)) != C320_KeyCode) {
2800 return (-1);
2802 if ((tmp = readw(baseAddr + C320_status)) != STS_init) {
2803 return (-2);
2806 return (0);
2809 static int moxaload320b(int cardno, unsigned char __user *tmp, int len)
2811 void __iomem *baseAddr;
2812 int i;
2814 if(len > sizeof(moxaBuff))
2815 return -EINVAL;
2816 if(copy_from_user(moxaBuff, tmp, len))
2817 return -EFAULT;
2818 baseAddr = moxaBaseAddr[cardno];
2819 writew(len - 7168 - 2, baseAddr + C320bapi_len);
2820 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
2821 for (i = 0; i < 7168; i++)
2822 writeb(moxaBuff[i], baseAddr + DynPage_addr + i);
2823 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
2824 for (i = 0; i < (len - 7168); i++)
2825 writeb(moxaBuff[i + 7168], baseAddr + DynPage_addr + i);
2826 return (0);
2829 static int moxaloadcode(int cardno, unsigned char __user *tmp, int len)
2831 void __iomem *baseAddr, *ofsAddr;
2832 int retval, port, i;
2834 if(copy_from_user(moxaBuff, tmp, len))
2835 return -EFAULT;
2836 baseAddr = moxaBaseAddr[cardno];
2837 switch (moxa_boards[cardno].boardType) {
2838 case MOXA_BOARD_C218_ISA:
2839 case MOXA_BOARD_C218_PCI:
2840 case MOXA_BOARD_CP204J:
2841 retval = moxaloadc218(cardno, baseAddr, len);
2842 if (retval)
2843 return (retval);
2844 port = cardno * MAX_PORTS_PER_BOARD;
2845 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2846 moxaChkPort[port] = 1;
2847 moxaCurBaud[port] = 9600L;
2848 moxaDCDState[port] = 0;
2849 moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
2850 ofsAddr = moxaTableAddr[port];
2851 writew(C218rx_mask, ofsAddr + RX_mask);
2852 writew(C218tx_mask, ofsAddr + TX_mask);
2853 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
2854 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
2856 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
2857 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
2860 break;
2861 default:
2862 retval = moxaloadc320(cardno, baseAddr, len,
2863 &moxa_boards[cardno].numPorts);
2864 if (retval)
2865 return (retval);
2866 port = cardno * MAX_PORTS_PER_BOARD;
2867 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2868 moxaChkPort[port] = 1;
2869 moxaCurBaud[port] = 9600L;
2870 moxaDCDState[port] = 0;
2871 moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
2872 ofsAddr = moxaTableAddr[port];
2873 if (moxa_boards[cardno].numPorts == 8) {
2874 writew(C320p8rx_mask, ofsAddr + RX_mask);
2875 writew(C320p8tx_mask, ofsAddr + TX_mask);
2876 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
2877 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
2878 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
2879 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
2881 } else if (moxa_boards[cardno].numPorts == 16) {
2882 writew(C320p16rx_mask, ofsAddr + RX_mask);
2883 writew(C320p16tx_mask, ofsAddr + TX_mask);
2884 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
2885 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
2886 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
2887 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
2889 } else if (moxa_boards[cardno].numPorts == 24) {
2890 writew(C320p24rx_mask, ofsAddr + RX_mask);
2891 writew(C320p24tx_mask, ofsAddr + TX_mask);
2892 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
2893 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
2894 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
2895 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2896 } else if (moxa_boards[cardno].numPorts == 32) {
2897 writew(C320p32rx_mask, ofsAddr + RX_mask);
2898 writew(C320p32tx_mask, ofsAddr + TX_mask);
2899 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
2900 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
2901 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
2902 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
2903 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2906 break;
2908 return (0);
2911 static int moxaloadc218(int cardno, void __iomem *baseAddr, int len)
2913 char retry;
2914 int i, j, len1, len2;
2915 ushort usum, *ptr, keycode;
2917 if (moxa_boards[cardno].boardType == MOXA_BOARD_CP204J)
2918 keycode = CP204J_KeyCode;
2919 else
2920 keycode = C218_KeyCode;
2921 usum = 0;
2922 len1 = len >> 1;
2923 ptr = (ushort *) moxaBuff;
2924 for (i = 0; i < len1; i++)
2925 usum += *(ptr + i);
2926 retry = 0;
2927 do {
2928 len1 = len >> 1;
2929 j = 0;
2930 while (len1) {
2931 len2 = (len1 > 2048) ? 2048 : len1;
2932 len1 -= len2;
2933 for (i = 0; i < len2 << 1; i++)
2934 writeb(moxaBuff[i + j], baseAddr + C218_LoadBuf + i);
2935 j += i;
2937 writew(len2, baseAddr + C218DLoad_len);
2938 writew(0, baseAddr + C218_key);
2939 for (i = 0; i < 100; i++) {
2940 if (readw(baseAddr + C218_key) == keycode)
2941 break;
2942 moxadelay(1); /* delay 10 ms */
2944 if (readw(baseAddr + C218_key) != keycode) {
2945 return (-1);
2948 writew(0, baseAddr + C218DLoad_len);
2949 writew(usum, baseAddr + C218check_sum);
2950 writew(0, baseAddr + C218_key);
2951 for (i = 0; i < 100; i++) {
2952 if (readw(baseAddr + C218_key) == keycode)
2953 break;
2954 moxadelay(1); /* delay 10 ms */
2956 retry++;
2957 } while ((readb(baseAddr + C218chksum_ok) != 1) && (retry < 3));
2958 if (readb(baseAddr + C218chksum_ok) != 1) {
2959 return (-1);
2961 writew(0, baseAddr + C218_key);
2962 for (i = 0; i < 100; i++) {
2963 if (readw(baseAddr + Magic_no) == Magic_code)
2964 break;
2965 moxadelay(1); /* delay 10 ms */
2967 if (readw(baseAddr + Magic_no) != Magic_code) {
2968 return (-1);
2970 writew(1, baseAddr + Disable_IRQ);
2971 writew(0, baseAddr + Magic_no);
2972 for (i = 0; i < 100; i++) {
2973 if (readw(baseAddr + Magic_no) == Magic_code)
2974 break;
2975 moxadelay(1); /* delay 10 ms */
2977 if (readw(baseAddr + Magic_no) != Magic_code) {
2978 return (-1);
2980 moxaCard = 1;
2981 moxaIntNdx[cardno] = baseAddr + IRQindex;
2982 moxaIntPend[cardno] = baseAddr + IRQpending;
2983 moxaIntTable[cardno] = baseAddr + IRQtable;
2984 return (0);
2987 static int moxaloadc320(int cardno, void __iomem *baseAddr, int len, int *numPorts)
2989 ushort usum;
2990 int i, j, wlen, len2, retry;
2991 ushort *uptr;
2993 usum = 0;
2994 wlen = len >> 1;
2995 uptr = (ushort *) moxaBuff;
2996 for (i = 0; i < wlen; i++)
2997 usum += uptr[i];
2998 retry = 0;
2999 j = 0;
3000 do {
3001 while (wlen) {
3002 if (wlen > 2048)
3003 len2 = 2048;
3004 else
3005 len2 = wlen;
3006 wlen -= len2;
3007 len2 <<= 1;
3008 for (i = 0; i < len2; i++)
3009 writeb(moxaBuff[j + i], baseAddr + C320_LoadBuf + i);
3010 len2 >>= 1;
3011 j += i;
3012 writew(len2, baseAddr + C320DLoad_len);
3013 writew(0, baseAddr + C320_key);
3014 for (i = 0; i < 10; i++) {
3015 if (readw(baseAddr + C320_key) == C320_KeyCode)
3016 break;
3017 moxadelay(1);
3019 if (readw(baseAddr + C320_key) != C320_KeyCode)
3020 return (-1);
3022 writew(0, baseAddr + C320DLoad_len);
3023 writew(usum, baseAddr + C320check_sum);
3024 writew(0, baseAddr + C320_key);
3025 for (i = 0; i < 10; i++) {
3026 if (readw(baseAddr + C320_key) == C320_KeyCode)
3027 break;
3028 moxadelay(1);
3030 retry++;
3031 } while ((readb(baseAddr + C320chksum_ok) != 1) && (retry < 3));
3032 if (readb(baseAddr + C320chksum_ok) != 1)
3033 return (-1);
3034 writew(0, baseAddr + C320_key);
3035 for (i = 0; i < 600; i++) {
3036 if (readw(baseAddr + Magic_no) == Magic_code)
3037 break;
3038 moxadelay(1);
3040 if (readw(baseAddr + Magic_no) != Magic_code)
3041 return (-100);
3043 if (moxa_boards[cardno].busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
3044 writew(0x3800, baseAddr + TMS320_PORT1);
3045 writew(0x3900, baseAddr + TMS320_PORT2);
3046 writew(28499, baseAddr + TMS320_CLOCK);
3047 } else {
3048 writew(0x3200, baseAddr + TMS320_PORT1);
3049 writew(0x3400, baseAddr + TMS320_PORT2);
3050 writew(19999, baseAddr + TMS320_CLOCK);
3052 writew(1, baseAddr + Disable_IRQ);
3053 writew(0, baseAddr + Magic_no);
3054 for (i = 0; i < 500; i++) {
3055 if (readw(baseAddr + Magic_no) == Magic_code)
3056 break;
3057 moxadelay(1);
3059 if (readw(baseAddr + Magic_no) != Magic_code)
3060 return (-102);
3062 j = readw(baseAddr + Module_cnt);
3063 if (j <= 0)
3064 return (-101);
3065 *numPorts = j * 8;
3066 writew(j, baseAddr + Module_no);
3067 writew(0, baseAddr + Magic_no);
3068 for (i = 0; i < 600; i++) {
3069 if (readw(baseAddr + Magic_no) == Magic_code)
3070 break;
3071 moxadelay(1);
3073 if (readw(baseAddr + Magic_no) != Magic_code)
3074 return (-102);
3075 moxaCard = 1;
3076 moxaIntNdx[cardno] = baseAddr + IRQindex;
3077 moxaIntPend[cardno] = baseAddr + IRQpending;
3078 moxaIntTable[cardno] = baseAddr + IRQtable;
3079 return (0);
3082 #if 0
3083 long MoxaPortGetCurBaud(int port)
3086 if (moxaChkPort[port] == 0)
3087 return (0);
3088 return (moxaCurBaud[port]);
3090 #endif /* 0 */
3092 static void MoxaSetFifo(int port, int enable)
3094 void __iomem *ofsAddr = moxaTableAddr[port];
3096 if (!enable) {
3097 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
3098 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
3099 } else {
3100 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
3101 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
3105 #if 0
3106 int MoxaPortSetMode(int port, int databits, int stopbits, int parity)
3108 void __iomem *ofsAddr;
3109 int val;
3111 val = 0;
3112 switch (databits) {
3113 case 5:
3114 val |= 0;
3115 break;
3116 case 6:
3117 val |= 1;
3118 break;
3119 case 7:
3120 val |= 2;
3121 break;
3122 case 8:
3123 val |= 3;
3124 break;
3125 default:
3126 return (-1);
3128 switch (stopbits) {
3129 case 0:
3130 val |= 0;
3131 break; /* stop bits 1.5 */
3132 case 1:
3133 val |= 0;
3134 break;
3135 case 2:
3136 val |= 4;
3137 break;
3138 default:
3139 return (-1);
3141 switch (parity) {
3142 case 0:
3143 val |= 0x00;
3144 break; /* None */
3145 case 1:
3146 val |= 0x08;
3147 break; /* Odd */
3148 case 2:
3149 val |= 0x18;
3150 break; /* Even */
3151 case 3:
3152 val |= 0x28;
3153 break; /* Mark */
3154 case 4:
3155 val |= 0x38;
3156 break; /* Space */
3157 default:
3158 return (-1);
3160 ofsAddr = moxaTableAddr[port];
3161 moxafunc(ofsAddr, FC_SetMode, val);
3162 return (0);
3165 int MoxaPortTxBufSize(int port)
3167 void __iomem *ofsAddr;
3168 int size;
3170 ofsAddr = moxaTableAddr[port];
3171 size = readw(ofsAddr + TX_mask);
3172 return (size);
3175 int MoxaPortRxBufSize(int port)
3177 void __iomem *ofsAddr;
3178 int size;
3180 ofsAddr = moxaTableAddr[port];
3181 size = readw(ofsAddr + RX_mask);
3182 return (size);
3185 int MoxaPortRxFree(int port)
3187 void __iomem *ofsAddr;
3188 ushort rptr, wptr, mask;
3189 int len;
3191 ofsAddr = moxaTableAddr[port];
3192 rptr = readw(ofsAddr + RXrptr);
3193 wptr = readw(ofsAddr + RXwptr);
3194 mask = readw(ofsAddr + RX_mask);
3195 len = mask - ((wptr - rptr) & mask);
3196 return (len);
3198 int MoxaPortGetBrkCnt(int port)
3200 return (moxaBreakCnt[port]);
3203 void MoxaPortSetXonXoff(int port, int xonValue, int xoffValue)
3205 void __iomem *ofsAddr;
3207 ofsAddr = moxaTableAddr[port];
3208 writew(xonValue, ofsAddr + FuncArg);
3209 writew(xoffValue, ofsAddr + FuncArg1);
3210 writew(FC_SetXonXoff, ofsAddr + FuncCode);
3211 wait_finish(ofsAddr);
3214 int MoxaPortIsTxHold(int port)
3216 void __iomem *ofsAddr;
3217 int val;
3219 ofsAddr = moxaTableAddr[port];
3220 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
3221 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
3222 moxafunc(ofsAddr, FC_GetCCSR, 0);
3223 val = readw(ofsAddr + FuncArg);
3224 if (val & 0x04)
3225 return (1);
3226 } else {
3227 if (readw(ofsAddr + FlagStat) & Tx_flowOff)
3228 return (1);
3230 return (0);
3232 #endif