blob: 96cb1f07332b68e5fb5b77dfb68a74d6356c6b64 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*****************************************************************************/
2/*
3 * moxa.c -- MOXA Intellio family multiport serial driver.
4 *
5 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com.tw).
6 *
7 * This code is loosely based on the Linux serial driver, written by
8 * Linus Torvalds, Theodore T'so and others.
9 *
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.
14 *
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.
19 *
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.
23 */
24
25/*
26 * MOXA Intellio Series Driver
27 * for : LINUX
28 * date : 1999/1/7
29 * version : 5.1
30 */
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/module.h>
33#include <linux/types.h>
34#include <linux/mm.h>
35#include <linux/ioport.h>
36#include <linux/errno.h>
37#include <linux/signal.h>
38#include <linux/sched.h>
39#include <linux/timer.h>
40#include <linux/interrupt.h>
41#include <linux/tty.h>
42#include <linux/tty_flip.h>
43#include <linux/major.h>
44#include <linux/string.h>
45#include <linux/fcntl.h>
46#include <linux/ptrace.h>
47#include <linux/serial.h>
48#include <linux/tty_driver.h>
49#include <linux/delay.h>
50#include <linux/pci.h>
51#include <linux/init.h>
52#include <linux/bitops.h>
53
54#include <asm/system.h>
55#include <asm/io.h>
56#include <asm/uaccess.h>
57
58#define MOXA_VERSION "5.1k"
59
60#define MOXAMAJOR 172
61#define MOXACUMAJOR 173
62
63#define put_to_user(arg1, arg2) put_user(arg1, (unsigned long *)arg2)
64#define get_from_user(arg1, arg2) get_user(arg1, (unsigned int *)arg2)
65
66#define MAX_BOARDS 4 /* Don't change this value */
67#define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
68#define MAX_PORTS 128 /* Don't change this value */
69
70/*
71 * Define the Moxa PCI vendor and device IDs.
72 */
73#define MOXA_BUS_TYPE_ISA 0
74#define MOXA_BUS_TYPE_PCI 1
75
76#ifndef PCI_VENDOR_ID_MOXA
77#define PCI_VENDOR_ID_MOXA 0x1393
78#endif
79#ifndef PCI_DEVICE_ID_CP204J
80#define PCI_DEVICE_ID_CP204J 0x2040
81#endif
82#ifndef PCI_DEVICE_ID_C218
83#define PCI_DEVICE_ID_C218 0x2180
84#endif
85#ifndef PCI_DEVICE_ID_C320
86#define PCI_DEVICE_ID_C320 0x3200
87#endif
88
89enum {
90 MOXA_BOARD_C218_PCI = 1,
91 MOXA_BOARD_C218_ISA,
92 MOXA_BOARD_C320_PCI,
93 MOXA_BOARD_C320_ISA,
94 MOXA_BOARD_CP204J,
95};
96
97static char *moxa_brdname[] =
98{
99 "C218 Turbo PCI series",
100 "C218 Turbo ISA series",
101 "C320 Turbo PCI series",
102 "C320 Turbo ISA series",
103 "CP-204J series",
104};
105
106#ifdef CONFIG_PCI
107static struct pci_device_id moxa_pcibrds[] = {
108 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C218, PCI_ANY_ID, PCI_ANY_ID,
109 0, 0, MOXA_BOARD_C218_PCI },
110 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C320, PCI_ANY_ID, PCI_ANY_ID,
111 0, 0, MOXA_BOARD_C320_PCI },
112 { PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_CP204J, PCI_ANY_ID, PCI_ANY_ID,
113 0, 0, MOXA_BOARD_CP204J },
114 { 0 }
115};
116MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
117#endif /* CONFIG_PCI */
118
119typedef struct _moxa_isa_board_conf {
120 int boardType;
121 int numPorts;
122 unsigned long baseAddr;
123} moxa_isa_board_conf;
124
125static moxa_isa_board_conf moxa_isa_boards[] =
126{
127/* {MOXA_BOARD_C218_ISA,8,0xDC000}, */
128};
129
130typedef struct _moxa_pci_devinfo {
131 ushort busNum;
132 ushort devNum;
Jiri Slaby86fbf142006-10-21 10:24:01 -0700133 struct pci_dev *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134} moxa_pci_devinfo;
135
136typedef 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;
143
144static moxa_board_conf moxa_boards[MAX_BOARDS];
145static void __iomem *moxaBaseAddr[MAX_BOARDS];
Dirk Eibach01cfaf02006-08-27 01:23:36 -0700146static int loadstat[MAX_BOARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148struct moxa_str {
149 int type;
150 int port;
151 int close_delay;
152 unsigned short closing_wait;
153 int count;
154 int blocked_open;
155 long event; /* long req'd for set_bit --RR */
156 int asyncflags;
157 unsigned long statusflags;
158 struct tty_struct *tty;
159 int cflag;
160 wait_queue_head_t open_wait;
161 wait_queue_head_t close_wait;
162 struct work_struct tqueue;
163};
164
165struct mxser_mstatus {
166 tcflag_t cflag;
167 int cts;
168 int dsr;
169 int ri;
170 int dcd;
171};
172
173static struct mxser_mstatus GMStatus[MAX_PORTS];
174
175/* statusflags */
176#define TXSTOPPED 0x1
177#define LOWWAIT 0x2
178#define EMPTYWAIT 0x4
179#define THROTTLE 0x8
180
181/* event */
182#define MOXA_EVENT_HANGUP 1
183
184#define SERIAL_DO_RESTART
185
186
187#define SERIAL_TYPE_NORMAL 1
188
189#define WAKEUP_CHARS 256
190
191#define PORTNO(x) ((x)->index)
192
193static int verbose = 0;
194static int ttymajor = MOXAMAJOR;
195/* Variables for insmod */
196#ifdef MODULE
197static int baseaddr[] = {0, 0, 0, 0};
198static int type[] = {0, 0, 0, 0};
199static int numports[] = {0, 0, 0, 0};
200#endif
201
202MODULE_AUTHOR("William Chen");
203MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
204MODULE_LICENSE("GPL");
205#ifdef MODULE
206module_param_array(type, int, NULL, 0);
207module_param_array(baseaddr, int, NULL, 0);
208module_param_array(numports, int, NULL, 0);
209#endif
210module_param(ttymajor, int, 0);
211module_param(verbose, bool, 0644);
212
213static struct tty_driver *moxaDriver;
214static struct moxa_str moxaChannels[MAX_PORTS];
215static unsigned char *moxaXmitBuff;
216static int moxaTimer_on;
217static struct timer_list moxaTimer;
218static int moxaEmptyTimer_on[MAX_PORTS];
219static struct timer_list moxaEmptyTimer[MAX_PORTS];
220static struct semaphore moxaBuffSem;
221
222/*
223 * static functions:
224 */
225static void do_moxa_softint(void *);
226static int moxa_open(struct tty_struct *, struct file *);
227static void moxa_close(struct tty_struct *, struct file *);
228static int moxa_write(struct tty_struct *, const unsigned char *, int);
229static int moxa_write_room(struct tty_struct *);
230static void moxa_flush_buffer(struct tty_struct *);
231static int moxa_chars_in_buffer(struct tty_struct *);
232static void moxa_flush_chars(struct tty_struct *);
233static void moxa_put_char(struct tty_struct *, unsigned char);
234static int moxa_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);
235static void moxa_throttle(struct tty_struct *);
236static void moxa_unthrottle(struct tty_struct *);
237static void moxa_set_termios(struct tty_struct *, struct termios *);
238static void moxa_stop(struct tty_struct *);
239static void moxa_start(struct tty_struct *);
240static void moxa_hangup(struct tty_struct *);
241static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
242static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
243 unsigned int set, unsigned int clear);
244static void moxa_poll(unsigned long);
245static void set_tty_param(struct tty_struct *);
246static int block_till_ready(struct tty_struct *, struct file *,
247 struct moxa_str *);
248static void setup_empty_event(struct tty_struct *);
249static void check_xmit_empty(unsigned long);
250static void shut_down(struct moxa_str *);
251static void receive_data(struct moxa_str *);
252/*
253 * moxa board interface functions:
254 */
255static void MoxaDriverInit(void);
256static int MoxaDriverIoctl(unsigned int, unsigned long, int);
257static int MoxaDriverPoll(void);
258static int MoxaPortsOfCard(int);
259static int MoxaPortIsValid(int);
260static void MoxaPortEnable(int);
261static void MoxaPortDisable(int);
262static long MoxaPortGetMaxBaud(int);
263static long MoxaPortSetBaud(int, long);
Alan Coxc7bce302006-09-30 23:27:24 -0700264static int MoxaPortSetTermio(int, struct termios *, speed_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265static int MoxaPortGetLineOut(int, int *, int *);
266static void MoxaPortLineCtrl(int, int, int);
267static void MoxaPortFlowCtrl(int, int, int, int, int, int);
268static int MoxaPortLineStatus(int);
269static int MoxaPortDCDChange(int);
270static int MoxaPortDCDON(int);
271static void MoxaPortFlushData(int, int);
272static int MoxaPortWriteData(int, unsigned char *, int);
Alan Cox33f0f882006-01-09 20:54:13 -0800273static int MoxaPortReadData(int, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274static int MoxaPortTxQueue(int);
275static int MoxaPortRxQueue(int);
276static int MoxaPortTxFree(int);
277static void MoxaPortTxDisable(int);
278static void MoxaPortTxEnable(int);
279static int MoxaPortResetBrkCnt(int);
280static void MoxaPortSendBreak(int, int);
281static int moxa_get_serial_info(struct moxa_str *, struct serial_struct __user *);
282static int moxa_set_serial_info(struct moxa_str *, struct serial_struct __user *);
283static void MoxaSetFifo(int port, int enable);
284
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700285static const struct tty_operations moxa_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 .open = moxa_open,
287 .close = moxa_close,
288 .write = moxa_write,
289 .write_room = moxa_write_room,
290 .flush_buffer = moxa_flush_buffer,
291 .chars_in_buffer = moxa_chars_in_buffer,
292 .flush_chars = moxa_flush_chars,
293 .put_char = moxa_put_char,
294 .ioctl = moxa_ioctl,
295 .throttle = moxa_throttle,
296 .unthrottle = moxa_unthrottle,
297 .set_termios = moxa_set_termios,
298 .stop = moxa_stop,
299 .start = moxa_start,
300 .hangup = moxa_hangup,
301 .tiocmget = moxa_tiocmget,
302 .tiocmset = moxa_tiocmset,
303};
304
Ingo Molnar34af9462006-06-27 02:53:55 -0700305static DEFINE_SPINLOCK(moxa_lock);
Alan Cox33f0f882006-01-09 20:54:13 -0800306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307#ifdef CONFIG_PCI
308static int moxa_get_PCI_conf(struct pci_dev *p, int board_type, moxa_board_conf * board)
309{
310 board->baseAddr = pci_resource_start (p, 2);
311 board->boardType = board_type;
312 switch (board_type) {
313 case MOXA_BOARD_C218_ISA:
314 case MOXA_BOARD_C218_PCI:
315 board->numPorts = 8;
316 break;
317
318 case MOXA_BOARD_CP204J:
319 board->numPorts = 4;
320 break;
321 default:
322 board->numPorts = 0;
323 break;
324 }
325 board->busType = MOXA_BUS_TYPE_PCI;
326 board->pciInfo.busNum = p->bus->number;
327 board->pciInfo.devNum = p->devfn >> 3;
Jiri Slaby86fbf142006-10-21 10:24:01 -0700328 board->pciInfo.pdev = p;
329 /* don't lose the reference in the next pci_get_device iteration */
330 pci_dev_get(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 return (0);
333}
334#endif /* CONFIG_PCI */
335
336static int __init moxa_init(void)
337{
338 int i, numBoards;
339 struct moxa_str *ch;
340
341 printk(KERN_INFO "MOXA Intellio family driver version %s\n", MOXA_VERSION);
342 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
343 if (!moxaDriver)
344 return -ENOMEM;
345
346 init_MUTEX(&moxaBuffSem);
347 moxaDriver->owner = THIS_MODULE;
Sergey Vlasov9b4e3b12005-09-03 16:26:49 +0100348 moxaDriver->name = "ttyMX";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 moxaDriver->major = ttymajor;
350 moxaDriver->minor_start = 0;
351 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
352 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
353 moxaDriver->init_termios = tty_std_termios;
354 moxaDriver->init_termios.c_iflag = 0;
355 moxaDriver->init_termios.c_oflag = 0;
356 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
357 moxaDriver->init_termios.c_lflag = 0;
358 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
359 tty_set_operations(moxaDriver, &moxa_ops);
360
361 moxaXmitBuff = NULL;
362
363 for (i = 0, ch = moxaChannels; i < MAX_PORTS; i++, ch++) {
364 ch->type = PORT_16550A;
365 ch->port = i;
366 INIT_WORK(&ch->tqueue, do_moxa_softint, ch);
367 ch->tty = NULL;
368 ch->close_delay = 5 * HZ / 10;
369 ch->closing_wait = 30 * HZ;
370 ch->count = 0;
371 ch->blocked_open = 0;
372 ch->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
373 init_waitqueue_head(&ch->open_wait);
374 init_waitqueue_head(&ch->close_wait);
375 }
376
377 for (i = 0; i < MAX_BOARDS; i++) {
378 moxa_boards[i].boardType = 0;
379 moxa_boards[i].numPorts = 0;
380 moxa_boards[i].baseAddr = 0;
381 moxa_boards[i].busType = 0;
382 moxa_boards[i].pciInfo.busNum = 0;
383 moxa_boards[i].pciInfo.devNum = 0;
384 }
385 MoxaDriverInit();
386 printk("Tty devices major number = %d\n", ttymajor);
387
388 if (tty_register_driver(moxaDriver)) {
389 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
390 put_tty_driver(moxaDriver);
391 return -1;
392 }
393 for (i = 0; i < MAX_PORTS; i++) {
394 init_timer(&moxaEmptyTimer[i]);
395 moxaEmptyTimer[i].function = check_xmit_empty;
396 moxaEmptyTimer[i].data = (unsigned long) & moxaChannels[i];
397 moxaEmptyTimer_on[i] = 0;
398 }
399
400 init_timer(&moxaTimer);
401 moxaTimer.function = moxa_poll;
402 moxaTimer.expires = jiffies + (HZ / 50);
403 moxaTimer_on = 1;
404 add_timer(&moxaTimer);
405
406 /* Find the boards defined in source code */
407 numBoards = 0;
408 for (i = 0; i < MAX_BOARDS; i++) {
409 if ((moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA) ||
410 (moxa_isa_boards[i].boardType == MOXA_BOARD_C320_ISA)) {
411 moxa_boards[numBoards].boardType = moxa_isa_boards[i].boardType;
412 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
413 moxa_boards[numBoards].numPorts = 8;
414 else
415 moxa_boards[numBoards].numPorts = moxa_isa_boards[i].numPorts;
416 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
417 moxa_boards[numBoards].baseAddr = moxa_isa_boards[i].baseAddr;
418 if (verbose)
419 printk("Board %2d: %s board(baseAddr=%lx)\n",
420 numBoards + 1,
421 moxa_brdname[moxa_boards[numBoards].boardType - 1],
422 moxa_boards[numBoards].baseAddr);
423 numBoards++;
424 }
425 }
426 /* Find the boards defined form module args. */
427#ifdef MODULE
428 for (i = 0; i < MAX_BOARDS; i++) {
429 if ((type[i] == MOXA_BOARD_C218_ISA) ||
430 (type[i] == MOXA_BOARD_C320_ISA)) {
431 if (verbose)
432 printk("Board %2d: %s board(baseAddr=%lx)\n",
433 numBoards + 1,
434 moxa_brdname[type[i] - 1],
435 (unsigned long) baseaddr[i]);
436 if (numBoards >= MAX_BOARDS) {
437 if (verbose)
438 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
439 continue;
440 }
441 moxa_boards[numBoards].boardType = type[i];
442 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
443 moxa_boards[numBoards].numPorts = 8;
444 else
445 moxa_boards[numBoards].numPorts = numports[i];
446 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
447 moxa_boards[numBoards].baseAddr = baseaddr[i];
448 numBoards++;
449 }
450 }
451#endif
452 /* Find PCI boards here */
453#ifdef CONFIG_PCI
454 {
455 struct pci_dev *p = NULL;
Tobias Klauserfe971072006-01-09 20:54:02 -0800456 int n = ARRAY_SIZE(moxa_pcibrds) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 i = 0;
458 while (i < n) {
Amit Gud881a8c12005-04-12 19:03:33 +0530459 while ((p = pci_get_device(moxa_pcibrds[i].vendor, moxa_pcibrds[i].device, p))!=NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 {
461 if (pci_enable_device(p))
462 continue;
463 if (numBoards >= MAX_BOARDS) {
464 if (verbose)
465 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
466 } else {
467 moxa_get_PCI_conf(p, moxa_pcibrds[i].driver_data,
468 &moxa_boards[numBoards]);
469 numBoards++;
470 }
471 }
472 i++;
473 }
474 }
475#endif
476 for (i = 0; i < numBoards; i++) {
477 moxaBaseAddr[i] = ioremap((unsigned long) moxa_boards[i].baseAddr, 0x4000);
478 }
479
480 return (0);
481}
482
483static void __exit moxa_exit(void)
484{
485 int i;
486
487 if (verbose)
488 printk("Unloading module moxa ...\n");
489
490 if (moxaTimer_on)
491 del_timer(&moxaTimer);
492
493 for (i = 0; i < MAX_PORTS; i++)
494 if (moxaEmptyTimer_on[i])
495 del_timer(&moxaEmptyTimer[i]);
496
497 if (tty_unregister_driver(moxaDriver))
498 printk("Couldn't unregister MOXA Intellio family serial driver\n");
499 put_tty_driver(moxaDriver);
Jiri Slaby86fbf142006-10-21 10:24:01 -0700500
501 for (i = 0; i < MAX_BOARDS; i++)
502 if (moxa_boards[i].busType == MOXA_BUS_TYPE_PCI)
503 pci_dev_put(moxa_boards[i].pciInfo.pdev);
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (verbose)
506 printk("Done\n");
507}
508
509module_init(moxa_init);
510module_exit(moxa_exit);
511
512static void do_moxa_softint(void *private_)
513{
514 struct moxa_str *ch = (struct moxa_str *) private_;
515 struct tty_struct *tty;
516
517 if (ch && (tty = ch->tty)) {
518 if (test_and_clear_bit(MOXA_EVENT_HANGUP, &ch->event)) {
519 tty_hangup(tty); /* FIXME: module removal race here - AKPM */
520 wake_up_interruptible(&ch->open_wait);
521 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
522 }
523 }
524}
525
526static int moxa_open(struct tty_struct *tty, struct file *filp)
527{
528 struct moxa_str *ch;
529 int port;
530 int retval;
531 unsigned long page;
532
533 port = PORTNO(tty);
534 if (port == MAX_PORTS) {
535 return (0);
536 }
537 if (!MoxaPortIsValid(port)) {
538 tty->driver_data = NULL;
539 return (-ENODEV);
540 }
541 down(&moxaBuffSem);
542 if (!moxaXmitBuff) {
543 page = get_zeroed_page(GFP_KERNEL);
544 if (!page) {
545 up(&moxaBuffSem);
546 return (-ENOMEM);
547 }
548 /* This test is guarded by the BuffSem so no longer needed
549 delete me in 2.5 */
550 if (moxaXmitBuff)
551 free_page(page);
552 else
553 moxaXmitBuff = (unsigned char *) page;
554 }
555 up(&moxaBuffSem);
556
557 ch = &moxaChannels[port];
558 ch->count++;
559 tty->driver_data = ch;
560 ch->tty = tty;
561 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
562 ch->statusflags = 0;
563 set_tty_param(tty);
564 MoxaPortLineCtrl(ch->port, 1, 1);
565 MoxaPortEnable(ch->port);
566 ch->asyncflags |= ASYNC_INITIALIZED;
567 }
568 retval = block_till_ready(tty, filp, ch);
569
570 moxa_unthrottle(tty);
571
572 if (ch->type == PORT_16550A) {
573 MoxaSetFifo(ch->port, 1);
574 } else {
575 MoxaSetFifo(ch->port, 0);
576 }
577
578 return (retval);
579}
580
581static void moxa_close(struct tty_struct *tty, struct file *filp)
582{
583 struct moxa_str *ch;
584 int port;
585
586 port = PORTNO(tty);
587 if (port == MAX_PORTS) {
588 return;
589 }
590 if (!MoxaPortIsValid(port)) {
591#ifdef SERIAL_DEBUG_CLOSE
592 printk("Invalid portno in moxa_close\n");
593#endif
594 tty->driver_data = NULL;
595 return;
596 }
597 if (tty->driver_data == NULL) {
598 return;
599 }
600 if (tty_hung_up_p(filp)) {
601 return;
602 }
603 ch = (struct moxa_str *) tty->driver_data;
604
605 if ((tty->count == 1) && (ch->count != 1)) {
606 printk("moxa_close: bad serial port count; tty->count is 1, "
607 "ch->count is %d\n", ch->count);
608 ch->count = 1;
609 }
610 if (--ch->count < 0) {
611 printk("moxa_close: bad serial port count, device=%s\n",
612 tty->name);
613 ch->count = 0;
614 }
615 if (ch->count) {
616 return;
617 }
618 ch->asyncflags |= ASYNC_CLOSING;
619
620 ch->cflag = tty->termios->c_cflag;
621 if (ch->asyncflags & ASYNC_INITIALIZED) {
622 setup_empty_event(tty);
623 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
624 moxaEmptyTimer_on[ch->port] = 0;
625 del_timer(&moxaEmptyTimer[ch->port]);
626 }
627 shut_down(ch);
628 MoxaPortFlushData(port, 2);
629
630 if (tty->driver->flush_buffer)
631 tty->driver->flush_buffer(tty);
632 tty_ldisc_flush(tty);
633
634 tty->closing = 0;
635 ch->event = 0;
636 ch->tty = NULL;
637 if (ch->blocked_open) {
638 if (ch->close_delay) {
639 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
640 }
641 wake_up_interruptible(&ch->open_wait);
642 }
643 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
644 wake_up_interruptible(&ch->close_wait);
645}
646
647static int moxa_write(struct tty_struct *tty,
648 const unsigned char *buf, int count)
649{
650 struct moxa_str *ch;
651 int len, port;
652 unsigned long flags;
653
654 ch = (struct moxa_str *) tty->driver_data;
655 if (ch == NULL)
656 return (0);
657 port = ch->port;
Alan Cox33f0f882006-01-09 20:54:13 -0800658
659 spin_lock_irqsave(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 len = MoxaPortWriteData(port, (unsigned char *) buf, count);
Alan Cox33f0f882006-01-09 20:54:13 -0800661 spin_unlock_irqrestore(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 /*********************************************
664 if ( !(ch->statusflags & LOWWAIT) &&
665 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
666 ************************************************/
667 ch->statusflags |= LOWWAIT;
668 return (len);
669}
670
671static int moxa_write_room(struct tty_struct *tty)
672{
673 struct moxa_str *ch;
674
675 if (tty->stopped)
676 return (0);
677 ch = (struct moxa_str *) tty->driver_data;
678 if (ch == NULL)
679 return (0);
680 return (MoxaPortTxFree(ch->port));
681}
682
683static void moxa_flush_buffer(struct tty_struct *tty)
684{
685 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
686
687 if (ch == NULL)
688 return;
689 MoxaPortFlushData(ch->port, 1);
690 tty_wakeup(tty);
691}
692
693static int moxa_chars_in_buffer(struct tty_struct *tty)
694{
695 int chars;
696 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
697
698 /*
699 * Sigh...I have to check if driver_data is NULL here, because
700 * if an open() fails, the TTY subsystem eventually calls
701 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
702 * routine. And since the open() failed, we return 0 here. TDJ
703 */
704 if (ch == NULL)
705 return (0);
706 chars = MoxaPortTxQueue(ch->port);
707 if (chars) {
708 /*
709 * Make it possible to wakeup anything waiting for output
710 * in tty_ioctl.c, etc.
711 */
712 if (!(ch->statusflags & EMPTYWAIT))
713 setup_empty_event(tty);
714 }
715 return (chars);
716}
717
718static void moxa_flush_chars(struct tty_struct *tty)
719{
720 /*
721 * Don't think I need this, because this is called to empty the TX
722 * buffer for the 16450, 16550, etc.
723 */
724}
725
726static void moxa_put_char(struct tty_struct *tty, unsigned char c)
727{
728 struct moxa_str *ch;
729 int port;
730 unsigned long flags;
731
732 ch = (struct moxa_str *) tty->driver_data;
733 if (ch == NULL)
734 return;
735 port = ch->port;
Alan Cox33f0f882006-01-09 20:54:13 -0800736 spin_lock_irqsave(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 moxaXmitBuff[0] = c;
738 MoxaPortWriteData(port, moxaXmitBuff, 1);
Alan Cox33f0f882006-01-09 20:54:13 -0800739 spin_unlock_irqrestore(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 /************************************************
741 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
742 *************************************************/
743 ch->statusflags |= LOWWAIT;
744}
745
746static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
747{
748 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
749 int port;
750 int flag = 0, dtr, rts;
751
752 port = PORTNO(tty);
753 if ((port != MAX_PORTS) && (!ch))
754 return (-EINVAL);
755
756 MoxaPortGetLineOut(ch->port, &dtr, &rts);
757 if (dtr)
758 flag |= TIOCM_DTR;
759 if (rts)
760 flag |= TIOCM_RTS;
761 dtr = MoxaPortLineStatus(ch->port);
762 if (dtr & 1)
763 flag |= TIOCM_CTS;
764 if (dtr & 2)
765 flag |= TIOCM_DSR;
766 if (dtr & 4)
767 flag |= TIOCM_CD;
768 return flag;
769}
770
771static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
772 unsigned int set, unsigned int clear)
773{
774 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
775 int port;
776 int dtr, rts;
777
778 port = PORTNO(tty);
779 if ((port != MAX_PORTS) && (!ch))
780 return (-EINVAL);
781
782 MoxaPortGetLineOut(ch->port, &dtr, &rts);
783 if (set & TIOCM_RTS)
784 rts = 1;
785 if (set & TIOCM_DTR)
786 dtr = 1;
787 if (clear & TIOCM_RTS)
788 rts = 0;
789 if (clear & TIOCM_DTR)
790 dtr = 0;
791 MoxaPortLineCtrl(ch->port, dtr, rts);
792 return 0;
793}
794
795static int moxa_ioctl(struct tty_struct *tty, struct file *file,
796 unsigned int cmd, unsigned long arg)
797{
798 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
799 register int port;
800 void __user *argp = (void __user *)arg;
801 int retval;
802
803 port = PORTNO(tty);
804 if ((port != MAX_PORTS) && (!ch))
805 return (-EINVAL);
806
807 switch (cmd) {
808 case TCSBRK: /* SVID version: non-zero arg --> no break */
809 retval = tty_check_change(tty);
810 if (retval)
811 return (retval);
812 setup_empty_event(tty);
813 tty_wait_until_sent(tty, 0);
814 if (!arg)
815 MoxaPortSendBreak(ch->port, 0);
816 return (0);
817 case TCSBRKP: /* support for POSIX tcsendbreak() */
818 retval = tty_check_change(tty);
819 if (retval)
820 return (retval);
821 setup_empty_event(tty);
822 tty_wait_until_sent(tty, 0);
823 MoxaPortSendBreak(ch->port, arg);
824 return (0);
825 case TIOCGSOFTCAR:
826 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) argp);
827 case TIOCSSOFTCAR:
828 if(get_user(retval, (unsigned long __user *) argp))
829 return -EFAULT;
830 arg = retval;
831 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
832 (arg ? CLOCAL : 0));
833 if (C_CLOCAL(tty))
834 ch->asyncflags &= ~ASYNC_CHECK_CD;
835 else
836 ch->asyncflags |= ASYNC_CHECK_CD;
837 return (0);
838 case TIOCGSERIAL:
839 return moxa_get_serial_info(ch, argp);
840
841 case TIOCSSERIAL:
842 return moxa_set_serial_info(ch, argp);
843 default:
844 retval = MoxaDriverIoctl(cmd, arg, port);
845 }
846 return (retval);
847}
848
849static void moxa_throttle(struct tty_struct *tty)
850{
851 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
852
853 ch->statusflags |= THROTTLE;
854}
855
856static void moxa_unthrottle(struct tty_struct *tty)
857{
858 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
859
860 ch->statusflags &= ~THROTTLE;
861}
862
863static void moxa_set_termios(struct tty_struct *tty,
864 struct termios *old_termios)
865{
866 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
867
868 if (ch == NULL)
869 return;
870 set_tty_param(tty);
871 if (!(old_termios->c_cflag & CLOCAL) &&
872 (tty->termios->c_cflag & CLOCAL))
873 wake_up_interruptible(&ch->open_wait);
874}
875
876static void moxa_stop(struct tty_struct *tty)
877{
878 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
879
880 if (ch == NULL)
881 return;
882 MoxaPortTxDisable(ch->port);
883 ch->statusflags |= TXSTOPPED;
884}
885
886
887static void moxa_start(struct tty_struct *tty)
888{
889 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
890
891 if (ch == NULL)
892 return;
893
894 if (!(ch->statusflags & TXSTOPPED))
895 return;
896
897 MoxaPortTxEnable(ch->port);
898 ch->statusflags &= ~TXSTOPPED;
899}
900
901static void moxa_hangup(struct tty_struct *tty)
902{
903 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
904
905 moxa_flush_buffer(tty);
906 shut_down(ch);
907 ch->event = 0;
908 ch->count = 0;
909 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
910 ch->tty = NULL;
911 wake_up_interruptible(&ch->open_wait);
912}
913
914static void moxa_poll(unsigned long ignored)
915{
916 register int card;
917 struct moxa_str *ch;
918 struct tty_struct *tp;
919 int i, ports;
920
921 moxaTimer_on = 0;
922 del_timer(&moxaTimer);
923
924 if (MoxaDriverPoll() < 0) {
925 moxaTimer.function = moxa_poll;
926 moxaTimer.expires = jiffies + (HZ / 50);
927 moxaTimer_on = 1;
928 add_timer(&moxaTimer);
929 return;
930 }
931 for (card = 0; card < MAX_BOARDS; card++) {
932 if ((ports = MoxaPortsOfCard(card)) <= 0)
933 continue;
934 ch = &moxaChannels[card * MAX_PORTS_PER_BOARD];
935 for (i = 0; i < ports; i++, ch++) {
936 if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
937 continue;
938 if (!(ch->statusflags & THROTTLE) &&
939 (MoxaPortRxQueue(ch->port) > 0))
940 receive_data(ch);
941 if ((tp = ch->tty) == 0)
942 continue;
943 if (ch->statusflags & LOWWAIT) {
944 if (MoxaPortTxQueue(ch->port) <= WAKEUP_CHARS) {
945 if (!tp->stopped) {
946 ch->statusflags &= ~LOWWAIT;
947 tty_wakeup(tp);
948 }
949 }
950 }
951 if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch->port) > 0)) {
952 tty_insert_flip_char(tp, 0, TTY_BREAK);
953 tty_schedule_flip(tp);
954 }
955 if (MoxaPortDCDChange(ch->port)) {
956 if (ch->asyncflags & ASYNC_CHECK_CD) {
957 if (MoxaPortDCDON(ch->port))
958 wake_up_interruptible(&ch->open_wait);
959 else {
960 set_bit(MOXA_EVENT_HANGUP, &ch->event);
961 schedule_work(&ch->tqueue);
962 }
963 }
964 }
965 }
966 }
967
968 moxaTimer.function = moxa_poll;
969 moxaTimer.expires = jiffies + (HZ / 50);
970 moxaTimer_on = 1;
971 add_timer(&moxaTimer);
972}
973
974/******************************************************************************/
975
976static void set_tty_param(struct tty_struct *tty)
977{
978 register struct termios *ts;
979 struct moxa_str *ch;
980 int rts, cts, txflow, rxflow, xany;
981
982 ch = (struct moxa_str *) tty->driver_data;
983 ts = tty->termios;
984 if (ts->c_cflag & CLOCAL)
985 ch->asyncflags &= ~ASYNC_CHECK_CD;
986 else
987 ch->asyncflags |= ASYNC_CHECK_CD;
988 rts = cts = txflow = rxflow = xany = 0;
989 if (ts->c_cflag & CRTSCTS)
990 rts = cts = 1;
991 if (ts->c_iflag & IXON)
992 txflow = 1;
993 if (ts->c_iflag & IXOFF)
994 rxflow = 1;
995 if (ts->c_iflag & IXANY)
996 xany = 1;
997 MoxaPortFlowCtrl(ch->port, rts, cts, txflow, rxflow, xany);
Alan Coxc7bce302006-09-30 23:27:24 -0700998 MoxaPortSetTermio(ch->port, ts, tty_get_baud_rate(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
1001static int block_till_ready(struct tty_struct *tty, struct file *filp,
1002 struct moxa_str *ch)
1003{
1004 DECLARE_WAITQUEUE(wait,current);
1005 unsigned long flags;
1006 int retval;
1007 int do_clocal = C_CLOCAL(tty);
1008
1009 /*
1010 * If the device is in the middle of being closed, then block
1011 * until it's done, and then try again.
1012 */
1013 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
1014 if (ch->asyncflags & ASYNC_CLOSING)
1015 interruptible_sleep_on(&ch->close_wait);
1016#ifdef SERIAL_DO_RESTART
1017 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1018 return (-EAGAIN);
1019 else
1020 return (-ERESTARTSYS);
1021#else
1022 return (-EAGAIN);
1023#endif
1024 }
1025 /*
1026 * If non-blocking mode is set, then make the check up front
1027 * and then exit.
1028 */
1029 if (filp->f_flags & O_NONBLOCK) {
1030 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1031 return (0);
1032 }
1033 /*
1034 * Block waiting for the carrier detect and the line to become free
1035 */
1036 retval = 0;
1037 add_wait_queue(&ch->open_wait, &wait);
1038#ifdef SERIAL_DEBUG_OPEN
1039 printk("block_til_ready before block: ttys%d, count = %d\n",
1040 ch->line, ch->count);
1041#endif
Alan Cox33f0f882006-01-09 20:54:13 -08001042 spin_lock_irqsave(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (!tty_hung_up_p(filp))
1044 ch->count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 ch->blocked_open++;
Alan Cox33f0f882006-01-09 20:54:13 -08001046 spin_unlock_irqrestore(&moxa_lock, flags);
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 while (1) {
1049 set_current_state(TASK_INTERRUPTIBLE);
1050 if (tty_hung_up_p(filp) ||
1051 !(ch->asyncflags & ASYNC_INITIALIZED)) {
1052#ifdef SERIAL_DO_RESTART
1053 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1054 retval = -EAGAIN;
1055 else
1056 retval = -ERESTARTSYS;
1057#else
1058 retval = -EAGAIN;
1059#endif
1060 break;
1061 }
1062 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
1063 MoxaPortDCDON(ch->port)))
1064 break;
1065
1066 if (signal_pending(current)) {
1067 retval = -ERESTARTSYS;
1068 break;
1069 }
1070 schedule();
1071 }
1072 set_current_state(TASK_RUNNING);
1073 remove_wait_queue(&ch->open_wait, &wait);
Alan Cox33f0f882006-01-09 20:54:13 -08001074
1075 spin_lock_irqsave(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if (!tty_hung_up_p(filp))
1077 ch->count++;
1078 ch->blocked_open--;
Alan Cox33f0f882006-01-09 20:54:13 -08001079 spin_unlock_irqrestore(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080#ifdef SERIAL_DEBUG_OPEN
1081 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1082 ch->line, ch->count);
1083#endif
1084 if (retval)
1085 return (retval);
Alan Cox33f0f882006-01-09 20:54:13 -08001086 /* FIXME: review to see if we need to use set_bit on these */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
Alan Cox33f0f882006-01-09 20:54:13 -08001088 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
1091static void setup_empty_event(struct tty_struct *tty)
1092{
1093 struct moxa_str *ch = tty->driver_data;
1094 unsigned long flags;
1095
Alan Cox33f0f882006-01-09 20:54:13 -08001096 spin_lock_irqsave(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 ch->statusflags |= EMPTYWAIT;
1098 moxaEmptyTimer_on[ch->port] = 0;
1099 del_timer(&moxaEmptyTimer[ch->port]);
1100 moxaEmptyTimer[ch->port].expires = jiffies + HZ;
1101 moxaEmptyTimer_on[ch->port] = 1;
1102 add_timer(&moxaEmptyTimer[ch->port]);
Alan Cox33f0f882006-01-09 20:54:13 -08001103 spin_unlock_irqrestore(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105
1106static void check_xmit_empty(unsigned long data)
1107{
1108 struct moxa_str *ch;
1109
1110 ch = (struct moxa_str *) data;
1111 moxaEmptyTimer_on[ch->port] = 0;
1112 del_timer(&moxaEmptyTimer[ch->port]);
1113 if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
1114 if (MoxaPortTxQueue(ch->port) == 0) {
1115 ch->statusflags &= ~EMPTYWAIT;
1116 tty_wakeup(ch->tty);
1117 return;
1118 }
1119 moxaEmptyTimer[ch->port].expires = jiffies + HZ;
1120 moxaEmptyTimer_on[ch->port] = 1;
1121 add_timer(&moxaEmptyTimer[ch->port]);
1122 } else
1123 ch->statusflags &= ~EMPTYWAIT;
1124}
1125
1126static void shut_down(struct moxa_str *ch)
1127{
1128 struct tty_struct *tp;
1129
1130 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1131 return;
1132
1133 tp = ch->tty;
1134
1135 MoxaPortDisable(ch->port);
1136
1137 /*
1138 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1139 */
1140 if (tp->termios->c_cflag & HUPCL)
1141 MoxaPortLineCtrl(ch->port, 0, 0);
1142
1143 ch->asyncflags &= ~ASYNC_INITIALIZED;
1144}
1145
1146static void receive_data(struct moxa_str *ch)
1147{
1148 struct tty_struct *tp;
1149 struct termios *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 unsigned long flags;
1151
1152 ts = NULL;
1153 tp = ch->tty;
1154 if (tp)
1155 ts = tp->termios;
1156 /**************************************************
1157 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1158 *****************************************************/
1159 if (!tp || !ts) {
1160 MoxaPortFlushData(ch->port, 0);
1161 return;
1162 }
Alan Cox33f0f882006-01-09 20:54:13 -08001163 spin_lock_irqsave(&moxa_lock, flags);
1164 MoxaPortReadData(ch->port, tp);
1165 spin_unlock_irqrestore(&moxa_lock, flags);
1166 tty_schedule_flip(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167}
1168
1169#define Magic_code 0x404
1170
1171/*
1172 * System Configuration
1173 */
1174/*
1175 * for C218 BIOS initialization
1176 */
1177#define C218_ConfBase 0x800
1178#define C218_status (C218_ConfBase + 0) /* BIOS running status */
1179#define C218_diag (C218_ConfBase + 2) /* diagnostic status */
1180#define C218_key (C218_ConfBase + 4) /* WORD (0x218 for C218) */
1181#define C218DLoad_len (C218_ConfBase + 6) /* WORD */
1182#define C218check_sum (C218_ConfBase + 8) /* BYTE */
1183#define C218chksum_ok (C218_ConfBase + 0x0a) /* BYTE (1:ok) */
1184#define C218_TestRx (C218_ConfBase + 0x10) /* 8 bytes for 8 ports */
1185#define C218_TestTx (C218_ConfBase + 0x18) /* 8 bytes for 8 ports */
1186#define C218_RXerr (C218_ConfBase + 0x20) /* 8 bytes for 8 ports */
1187#define C218_ErrFlag (C218_ConfBase + 0x28) /* 8 bytes for 8 ports */
1188
1189#define C218_LoadBuf 0x0F00
1190#define C218_KeyCode 0x218
1191#define CP204J_KeyCode 0x204
1192
1193/*
1194 * for C320 BIOS initialization
1195 */
1196#define C320_ConfBase 0x800
1197#define C320_LoadBuf 0x0f00
1198#define STS_init 0x05 /* for C320_status */
1199
1200#define C320_status C320_ConfBase + 0 /* BIOS running status */
1201#define C320_diag C320_ConfBase + 2 /* diagnostic status */
1202#define C320_key C320_ConfBase + 4 /* WORD (0320H for C320) */
1203#define C320DLoad_len C320_ConfBase + 6 /* WORD */
1204#define C320check_sum C320_ConfBase + 8 /* WORD */
1205#define C320chksum_ok C320_ConfBase + 0x0a /* WORD (1:ok) */
1206#define C320bapi_len C320_ConfBase + 0x0c /* WORD */
1207#define C320UART_no C320_ConfBase + 0x0e /* WORD */
1208
1209#define C320_KeyCode 0x320
1210
1211#define FixPage_addr 0x0000 /* starting addr of static page */
1212#define DynPage_addr 0x2000 /* starting addr of dynamic page */
1213#define C218_start 0x3000 /* starting addr of C218 BIOS prg */
1214#define Control_reg 0x1ff0 /* select page and reset control */
1215#define HW_reset 0x80
1216
1217/*
1218 * Function Codes
1219 */
1220#define FC_CardReset 0x80
1221#define FC_ChannelReset 1 /* C320 firmware not supported */
1222#define FC_EnableCH 2
1223#define FC_DisableCH 3
1224#define FC_SetParam 4
1225#define FC_SetMode 5
1226#define FC_SetRate 6
1227#define FC_LineControl 7
1228#define FC_LineStatus 8
1229#define FC_XmitControl 9
1230#define FC_FlushQueue 10
1231#define FC_SendBreak 11
1232#define FC_StopBreak 12
1233#define FC_LoopbackON 13
1234#define FC_LoopbackOFF 14
1235#define FC_ClrIrqTable 15
1236#define FC_SendXon 16
1237#define FC_SetTermIrq 17 /* C320 firmware not supported */
1238#define FC_SetCntIrq 18 /* C320 firmware not supported */
1239#define FC_SetBreakIrq 19
1240#define FC_SetLineIrq 20
1241#define FC_SetFlowCtl 21
1242#define FC_GenIrq 22
1243#define FC_InCD180 23
1244#define FC_OutCD180 24
1245#define FC_InUARTreg 23
1246#define FC_OutUARTreg 24
1247#define FC_SetXonXoff 25
1248#define FC_OutCD180CCR 26
1249#define FC_ExtIQueue 27
1250#define FC_ExtOQueue 28
1251#define FC_ClrLineIrq 29
1252#define FC_HWFlowCtl 30
1253#define FC_GetClockRate 35
1254#define FC_SetBaud 36
1255#define FC_SetDataMode 41
1256#define FC_GetCCSR 43
1257#define FC_GetDataError 45
1258#define FC_RxControl 50
1259#define FC_ImmSend 51
1260#define FC_SetXonState 52
1261#define FC_SetXoffState 53
1262#define FC_SetRxFIFOTrig 54
1263#define FC_SetTxFIFOCnt 55
1264#define FC_UnixRate 56
1265#define FC_UnixResetTimer 57
1266
1267#define RxFIFOTrig1 0
1268#define RxFIFOTrig4 1
1269#define RxFIFOTrig8 2
1270#define RxFIFOTrig14 3
1271
1272/*
1273 * Dual-Ported RAM
1274 */
1275#define DRAM_global 0
1276#define INT_data (DRAM_global + 0)
1277#define Config_base (DRAM_global + 0x108)
1278
1279#define IRQindex (INT_data + 0)
1280#define IRQpending (INT_data + 4)
1281#define IRQtable (INT_data + 8)
1282
1283/*
1284 * Interrupt Status
1285 */
1286#define IntrRx 0x01 /* receiver data O.K. */
1287#define IntrTx 0x02 /* transmit buffer empty */
1288#define IntrFunc 0x04 /* function complete */
1289#define IntrBreak 0x08 /* received break */
1290#define IntrLine 0x10 /* line status change
1291 for transmitter */
1292#define IntrIntr 0x20 /* received INTR code */
1293#define IntrQuit 0x40 /* received QUIT code */
1294#define IntrEOF 0x80 /* received EOF code */
1295
1296#define IntrRxTrigger 0x100 /* rx data count reach tigger value */
1297#define IntrTxTrigger 0x200 /* tx data count below trigger value */
1298
1299#define Magic_no (Config_base + 0)
1300#define Card_model_no (Config_base + 2)
1301#define Total_ports (Config_base + 4)
1302#define Module_cnt (Config_base + 8)
1303#define Module_no (Config_base + 10)
1304#define Timer_10ms (Config_base + 14)
1305#define Disable_IRQ (Config_base + 20)
1306#define TMS320_PORT1 (Config_base + 22)
1307#define TMS320_PORT2 (Config_base + 24)
1308#define TMS320_CLOCK (Config_base + 26)
1309
1310/*
1311 * DATA BUFFER in DRAM
1312 */
1313#define Extern_table 0x400 /* Base address of the external table
1314 (24 words * 64) total 3K bytes
1315 (24 words * 128) total 6K bytes */
1316#define Extern_size 0x60 /* 96 bytes */
1317#define RXrptr 0x00 /* read pointer for RX buffer */
1318#define RXwptr 0x02 /* write pointer for RX buffer */
1319#define TXrptr 0x04 /* read pointer for TX buffer */
1320#define TXwptr 0x06 /* write pointer for TX buffer */
1321#define HostStat 0x08 /* IRQ flag and general flag */
1322#define FlagStat 0x0A
1323#define FlowControl 0x0C /* B7 B6 B5 B4 B3 B2 B1 B0 */
1324 /* x x x x | | | | */
1325 /* | | | + CTS flow */
1326 /* | | +--- RTS flow */
1327 /* | +------ TX Xon/Xoff */
1328 /* +--------- RX Xon/Xoff */
1329#define Break_cnt 0x0E /* received break count */
1330#define CD180TXirq 0x10 /* if non-0: enable TX irq */
1331#define RX_mask 0x12
1332#define TX_mask 0x14
1333#define Ofs_rxb 0x16
1334#define Ofs_txb 0x18
1335#define Page_rxb 0x1A
1336#define Page_txb 0x1C
1337#define EndPage_rxb 0x1E
1338#define EndPage_txb 0x20
1339#define Data_error 0x22
1340#define RxTrigger 0x28
1341#define TxTrigger 0x2a
1342
1343#define rRXwptr 0x34
1344#define Low_water 0x36
1345
1346#define FuncCode 0x40
1347#define FuncArg 0x42
1348#define FuncArg1 0x44
1349
1350#define C218rx_size 0x2000 /* 8K bytes */
1351#define C218tx_size 0x8000 /* 32K bytes */
1352
1353#define C218rx_mask (C218rx_size - 1)
1354#define C218tx_mask (C218tx_size - 1)
1355
1356#define C320p8rx_size 0x2000
1357#define C320p8tx_size 0x8000
1358#define C320p8rx_mask (C320p8rx_size - 1)
1359#define C320p8tx_mask (C320p8tx_size - 1)
1360
1361#define C320p16rx_size 0x2000
1362#define C320p16tx_size 0x4000
1363#define C320p16rx_mask (C320p16rx_size - 1)
1364#define C320p16tx_mask (C320p16tx_size - 1)
1365
1366#define C320p24rx_size 0x2000
1367#define C320p24tx_size 0x2000
1368#define C320p24rx_mask (C320p24rx_size - 1)
1369#define C320p24tx_mask (C320p24tx_size - 1)
1370
1371#define C320p32rx_size 0x1000
1372#define C320p32tx_size 0x1000
1373#define C320p32rx_mask (C320p32rx_size - 1)
1374#define C320p32tx_mask (C320p32tx_size - 1)
1375
1376#define Page_size 0x2000
1377#define Page_mask (Page_size - 1)
1378#define C218rx_spage 3
1379#define C218tx_spage 4
1380#define C218rx_pageno 1
1381#define C218tx_pageno 4
1382#define C218buf_pageno 5
1383
1384#define C320p8rx_spage 3
1385#define C320p8tx_spage 4
1386#define C320p8rx_pgno 1
1387#define C320p8tx_pgno 4
1388#define C320p8buf_pgno 5
1389
1390#define C320p16rx_spage 3
1391#define C320p16tx_spage 4
1392#define C320p16rx_pgno 1
1393#define C320p16tx_pgno 2
1394#define C320p16buf_pgno 3
1395
1396#define C320p24rx_spage 3
1397#define C320p24tx_spage 4
1398#define C320p24rx_pgno 1
1399#define C320p24tx_pgno 1
1400#define C320p24buf_pgno 2
1401
1402#define C320p32rx_spage 3
1403#define C320p32tx_ofs C320p32rx_size
1404#define C320p32tx_spage 3
1405#define C320p32buf_pgno 1
1406
1407/*
1408 * Host Status
1409 */
1410#define WakeupRx 0x01
1411#define WakeupTx 0x02
1412#define WakeupBreak 0x08
1413#define WakeupLine 0x10
1414#define WakeupIntr 0x20
1415#define WakeupQuit 0x40
1416#define WakeupEOF 0x80 /* used in VTIME control */
1417#define WakeupRxTrigger 0x100
1418#define WakeupTxTrigger 0x200
1419/*
1420 * Flag status
1421 */
1422#define Rx_over 0x01
1423#define Xoff_state 0x02
1424#define Tx_flowOff 0x04
1425#define Tx_enable 0x08
1426#define CTS_state 0x10
1427#define DSR_state 0x20
1428#define DCD_state 0x80
1429/*
1430 * FlowControl
1431 */
1432#define CTS_FlowCtl 1
1433#define RTS_FlowCtl 2
1434#define Tx_FlowCtl 4
1435#define Rx_FlowCtl 8
1436#define IXM_IXANY 0x10
1437
1438#define LowWater 128
1439
1440#define DTR_ON 1
1441#define RTS_ON 2
1442#define CTS_ON 1
1443#define DSR_ON 2
1444#define DCD_ON 8
1445
1446/* mode definition */
1447#define MX_CS8 0x03
1448#define MX_CS7 0x02
1449#define MX_CS6 0x01
1450#define MX_CS5 0x00
1451
1452#define MX_STOP1 0x00
1453#define MX_STOP15 0x04
1454#define MX_STOP2 0x08
1455
1456#define MX_PARNONE 0x00
1457#define MX_PAREVEN 0x40
1458#define MX_PARODD 0xC0
1459
1460/*
1461 * Query
1462 */
1463#define QueryPort MAX_PORTS
1464
1465
1466
1467struct mon_str {
1468 int tick;
1469 int rxcnt[MAX_PORTS];
1470 int txcnt[MAX_PORTS];
1471};
1472typedef struct mon_str mon_st;
1473
1474#define DCD_changed 0x01
1475#define DCD_oldstate 0x80
1476
1477static unsigned char moxaBuff[10240];
1478static void __iomem *moxaIntNdx[MAX_BOARDS];
1479static void __iomem *moxaIntPend[MAX_BOARDS];
1480static void __iomem *moxaIntTable[MAX_BOARDS];
1481static char moxaChkPort[MAX_PORTS];
1482static char moxaLineCtrl[MAX_PORTS];
1483static void __iomem *moxaTableAddr[MAX_PORTS];
1484static long moxaCurBaud[MAX_PORTS];
1485static char moxaDCDState[MAX_PORTS];
1486static char moxaLowChkFlag[MAX_PORTS];
1487static int moxaLowWaterChk;
1488static int moxaCard;
1489static mon_st moxaLog;
1490static int moxaFuncTout;
1491static ushort moxaBreakCnt[MAX_PORTS];
1492
1493static void moxadelay(int);
1494static void moxafunc(void __iomem *, int, ushort);
1495static void wait_finish(void __iomem *);
1496static void low_water_check(void __iomem *);
1497static int moxaloadbios(int, unsigned char __user *, int);
1498static int moxafindcard(int);
1499static int moxaload320b(int, unsigned char __user *, int);
1500static int moxaloadcode(int, unsigned char __user *, int);
1501static int moxaloadc218(int, void __iomem *, int);
1502static int moxaloadc320(int, void __iomem *, int, int *);
1503
1504/*****************************************************************************
1505 * Driver level functions: *
1506 * 1. MoxaDriverInit(void); *
1507 * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
1508 * 3. MoxaDriverPoll(void); *
1509 *****************************************************************************/
1510void MoxaDriverInit(void)
1511{
1512 int i;
1513
1514 moxaFuncTout = HZ / 2; /* 500 mini-seconds */
1515 moxaCard = 0;
1516 moxaLog.tick = 0;
1517 moxaLowWaterChk = 0;
1518 for (i = 0; i < MAX_PORTS; i++) {
1519 moxaChkPort[i] = 0;
1520 moxaLowChkFlag[i] = 0;
1521 moxaLineCtrl[i] = 0;
1522 moxaLog.rxcnt[i] = 0;
1523 moxaLog.txcnt[i] = 0;
1524 }
1525}
1526
1527#define MOXA 0x400
1528#define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1529#define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
1530#define MOXA_INIT_DRIVER (MOXA + 6) /* moxaCard=0 */
1531#define MOXA_LOAD_BIOS (MOXA + 9) /* download BIOS */
1532#define MOXA_FIND_BOARD (MOXA + 10) /* Check if MOXA card exist? */
1533#define MOXA_LOAD_C320B (MOXA + 11) /* download 320B firmware */
1534#define MOXA_LOAD_CODE (MOXA + 12) /* download firmware */
1535#define MOXA_GETDATACOUNT (MOXA + 23)
1536#define MOXA_GET_IOQUEUE (MOXA + 27)
1537#define MOXA_FLUSH_QUEUE (MOXA + 28)
1538#define MOXA_GET_CONF (MOXA + 35) /* configuration */
1539#define MOXA_GET_MAJOR (MOXA + 63)
1540#define MOXA_GET_CUMAJOR (MOXA + 64)
1541#define MOXA_GETMSTATUS (MOXA + 65)
1542
1543
1544struct moxaq_str {
1545 int inq;
1546 int outq;
1547};
1548
1549struct dl_str {
1550 char __user *buf;
1551 int len;
1552 int cardno;
1553};
1554
1555static struct moxaq_str temp_queue[MAX_PORTS];
1556static struct dl_str dltmp;
1557
1558void MoxaPortFlushData(int port, int mode)
1559{
1560 void __iomem *ofsAddr;
1561 if ((mode < 0) || (mode > 2))
1562 return;
1563 ofsAddr = moxaTableAddr[port];
1564 moxafunc(ofsAddr, FC_FlushQueue, mode);
1565 if (mode != 1) {
1566 moxaLowChkFlag[port] = 0;
1567 low_water_check(ofsAddr);
1568 }
1569}
1570
1571int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port)
1572{
1573 int i;
1574 int status;
1575 int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
1576 void __user *argp = (void __user *)arg;
1577
1578 if (port == QueryPort) {
1579 if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_INIT_DRIVER) &&
1580 (cmd != MOXA_LOAD_BIOS) && (cmd != MOXA_FIND_BOARD) && (cmd != MOXA_LOAD_C320B) &&
1581 (cmd != MOXA_LOAD_CODE) && (cmd != MOXA_GETDATACOUNT) &&
1582 (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
1583 (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
1584 return (-EINVAL);
1585 }
1586 switch (cmd) {
1587 case MOXA_GET_CONF:
1588 if(copy_to_user(argp, &moxa_boards, MAX_BOARDS * sizeof(moxa_board_conf)))
1589 return -EFAULT;
1590 return (0);
1591 case MOXA_INIT_DRIVER:
1592 if ((int) arg == 0x404)
1593 MoxaDriverInit();
1594 return (0);
1595 case MOXA_GETDATACOUNT:
1596 moxaLog.tick = jiffies;
1597 if(copy_to_user(argp, &moxaLog, sizeof(mon_st)))
1598 return -EFAULT;
1599 return (0);
1600 case MOXA_FLUSH_QUEUE:
1601 MoxaPortFlushData(port, arg);
1602 return (0);
1603 case MOXA_GET_IOQUEUE:
1604 for (i = 0; i < MAX_PORTS; i++) {
1605 if (moxaChkPort[i]) {
1606 temp_queue[i].inq = MoxaPortRxQueue(i);
1607 temp_queue[i].outq = MoxaPortTxQueue(i);
1608 }
1609 }
1610 if(copy_to_user(argp, temp_queue, sizeof(struct moxaq_str) * MAX_PORTS))
1611 return -EFAULT;
1612 return (0);
1613 case MOXA_GET_OQUEUE:
1614 i = MoxaPortTxQueue(port);
1615 return put_user(i, (unsigned long __user *)argp);
1616 case MOXA_GET_IQUEUE:
1617 i = MoxaPortRxQueue(port);
1618 return put_user(i, (unsigned long __user *)argp);
1619 case MOXA_GET_MAJOR:
1620 if(copy_to_user(argp, &ttymajor, sizeof(int)))
1621 return -EFAULT;
1622 return 0;
1623 case MOXA_GET_CUMAJOR:
1624 i = 0;
1625 if(copy_to_user(argp, &i, sizeof(int)))
1626 return -EFAULT;
1627 return 0;
1628 case MOXA_GETMSTATUS:
1629 for (i = 0; i < MAX_PORTS; i++) {
1630 GMStatus[i].ri = 0;
1631 GMStatus[i].dcd = 0;
1632 GMStatus[i].dsr = 0;
1633 GMStatus[i].cts = 0;
1634 if (!moxaChkPort[i]) {
1635 continue;
1636 } else {
1637 status = MoxaPortLineStatus(moxaChannels[i].port);
1638 if (status & 1)
1639 GMStatus[i].cts = 1;
1640 if (status & 2)
1641 GMStatus[i].dsr = 1;
1642 if (status & 4)
1643 GMStatus[i].dcd = 1;
1644 }
1645
1646 if (!moxaChannels[i].tty || !moxaChannels[i].tty->termios)
1647 GMStatus[i].cflag = moxaChannels[i].cflag;
1648 else
1649 GMStatus[i].cflag = moxaChannels[i].tty->termios->c_cflag;
1650 }
1651 if(copy_to_user(argp, GMStatus, sizeof(struct mxser_mstatus) * MAX_PORTS))
1652 return -EFAULT;
1653 return 0;
1654 default:
1655 return (-ENOIOCTLCMD);
1656 case MOXA_LOAD_BIOS:
1657 case MOXA_FIND_BOARD:
1658 case MOXA_LOAD_C320B:
1659 case MOXA_LOAD_CODE:
Alan Cox49cd6192006-01-09 09:35:28 -05001660 if (!capable(CAP_SYS_RAWIO))
1661 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 break;
1663 }
1664
1665 if(copy_from_user(&dltmp, argp, sizeof(struct dl_str)))
1666 return -EFAULT;
1667 if(dltmp.cardno < 0 || dltmp.cardno >= MAX_BOARDS)
1668 return -EINVAL;
1669
1670 switch(cmd)
1671 {
1672 case MOXA_LOAD_BIOS:
1673 i = moxaloadbios(dltmp.cardno, dltmp.buf, dltmp.len);
1674 return (i);
1675 case MOXA_FIND_BOARD:
1676 return moxafindcard(dltmp.cardno);
1677 case MOXA_LOAD_C320B:
1678 moxaload320b(dltmp.cardno, dltmp.buf, dltmp.len);
1679 default: /* to keep gcc happy */
1680 return (0);
1681 case MOXA_LOAD_CODE:
1682 i = moxaloadcode(dltmp.cardno, dltmp.buf, dltmp.len);
1683 if (i == -1)
1684 return (-EFAULT);
1685 return (i);
1686
1687 }
1688}
1689
1690int MoxaDriverPoll(void)
1691{
1692 register ushort temp;
1693 register int card;
1694 void __iomem *ofsAddr;
1695 void __iomem *ip;
1696 int port, p, ports;
1697
1698 if (moxaCard == 0)
1699 return (-1);
1700 for (card = 0; card < MAX_BOARDS; card++) {
Dirk Eibach01cfaf02006-08-27 01:23:36 -07001701 if (loadstat[card] == 0)
1702 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 if ((ports = moxa_boards[card].numPorts) == 0)
1704 continue;
1705 if (readb(moxaIntPend[card]) == 0xff) {
1706 ip = moxaIntTable[card] + readb(moxaIntNdx[card]);
1707 p = card * MAX_PORTS_PER_BOARD;
1708 ports <<= 1;
1709 for (port = 0; port < ports; port += 2, p++) {
1710 if ((temp = readw(ip + port)) != 0) {
1711 writew(0, ip + port);
1712 ofsAddr = moxaTableAddr[p];
1713 if (temp & IntrTx)
1714 writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
1715 if (temp & IntrBreak) {
1716 moxaBreakCnt[p]++;
1717 }
1718 if (temp & IntrLine) {
1719 if (readb(ofsAddr + FlagStat) & DCD_state) {
1720 if ((moxaDCDState[p] & DCD_oldstate) == 0)
1721 moxaDCDState[p] = (DCD_oldstate |
1722 DCD_changed);
1723 } else {
1724 if (moxaDCDState[p] & DCD_oldstate)
1725 moxaDCDState[p] = DCD_changed;
1726 }
1727 }
1728 }
1729 }
1730 writeb(0, moxaIntPend[card]);
1731 }
1732 if (moxaLowWaterChk) {
1733 p = card * MAX_PORTS_PER_BOARD;
1734 for (port = 0; port < ports; port++, p++) {
1735 if (moxaLowChkFlag[p]) {
1736 moxaLowChkFlag[p] = 0;
1737 ofsAddr = moxaTableAddr[p];
1738 low_water_check(ofsAddr);
1739 }
1740 }
1741 }
1742 }
1743 moxaLowWaterChk = 0;
1744 return (0);
1745}
1746
1747/*****************************************************************************
1748 * Card level function: *
1749 * 1. MoxaPortsOfCard(int cardno); *
1750 *****************************************************************************/
1751int MoxaPortsOfCard(int cardno)
1752{
1753
1754 if (moxa_boards[cardno].boardType == 0)
1755 return (0);
1756 return (moxa_boards[cardno].numPorts);
1757}
1758
1759/*****************************************************************************
1760 * Port level functions: *
1761 * 1. MoxaPortIsValid(int port); *
1762 * 2. MoxaPortEnable(int port); *
1763 * 3. MoxaPortDisable(int port); *
1764 * 4. MoxaPortGetMaxBaud(int port); *
1765 * 5. MoxaPortGetCurBaud(int port); *
1766 * 6. MoxaPortSetBaud(int port, long baud); *
1767 * 7. MoxaPortSetMode(int port, int databit, int stopbit, int parity); *
1768 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1769 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1770 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1771 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1772 * 12. MoxaPortLineStatus(int port); *
1773 * 13. MoxaPortDCDChange(int port); *
1774 * 14. MoxaPortDCDON(int port); *
1775 * 15. MoxaPortFlushData(int port, int mode); *
1776 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
Alan Cox33f0f882006-01-09 20:54:13 -08001777 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 * 18. MoxaPortTxBufSize(int port); *
1779 * 19. MoxaPortRxBufSize(int port); *
1780 * 20. MoxaPortTxQueue(int port); *
1781 * 21. MoxaPortTxFree(int port); *
1782 * 22. MoxaPortRxQueue(int port); *
1783 * 23. MoxaPortRxFree(int port); *
1784 * 24. MoxaPortTxDisable(int port); *
1785 * 25. MoxaPortTxEnable(int port); *
1786 * 26. MoxaPortGetBrkCnt(int port); *
1787 * 27. MoxaPortResetBrkCnt(int port); *
1788 * 28. MoxaPortSetXonXoff(int port, int xonValue, int xoffValue); *
1789 * 29. MoxaPortIsTxHold(int port); *
1790 * 30. MoxaPortSendBreak(int port, int ticks); *
1791 *****************************************************************************/
1792/*
1793 * Moxa Port Number Description:
1794 *
1795 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1796 * the port number using in MOXA driver functions will be 0 to 31 for
1797 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1798 * to 127 for fourth. For example, if you setup three MOXA boards,
1799 * first board is C218, second board is C320-16 and third board is
1800 * C320-32. The port number of first board (C218 - 8 ports) is from
1801 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1802 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1803 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1804 * 127 will be invalid.
1805 *
1806 *
1807 * Moxa Functions Description:
1808 *
1809 * Function 1: Driver initialization routine, this routine must be
1810 * called when initialized driver.
1811 * Syntax:
1812 * void MoxaDriverInit();
1813 *
1814 *
1815 * Function 2: Moxa driver private IOCTL command processing.
1816 * Syntax:
1817 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1818 *
1819 * unsigned int cmd : IOCTL command
1820 * unsigned long arg : IOCTL argument
1821 * int port : port number (0 - 127)
1822 *
1823 * return: 0 (OK)
1824 * -EINVAL
1825 * -ENOIOCTLCMD
1826 *
1827 *
1828 * Function 3: Moxa driver polling process routine.
1829 * Syntax:
1830 * int MoxaDriverPoll(void);
1831 *
1832 * return: 0 ; polling O.K.
1833 * -1 : no any Moxa card.
1834 *
1835 *
1836 * Function 4: Get the ports of this card.
1837 * Syntax:
1838 * int MoxaPortsOfCard(int cardno);
1839 *
1840 * int cardno : card number (0 - 3)
1841 *
1842 * return: 0 : this card is invalid
1843 * 8/16/24/32
1844 *
1845 *
1846 * Function 5: Check this port is valid or invalid
1847 * Syntax:
1848 * int MoxaPortIsValid(int port);
1849 * int port : port number (0 - 127, ref port description)
1850 *
1851 * return: 0 : this port is invalid
1852 * 1 : this port is valid
1853 *
1854 *
1855 * Function 6: Enable this port to start Tx/Rx data.
1856 * Syntax:
1857 * void MoxaPortEnable(int port);
1858 * int port : port number (0 - 127)
1859 *
1860 *
1861 * Function 7: Disable this port
1862 * Syntax:
1863 * void MoxaPortDisable(int port);
1864 * int port : port number (0 - 127)
1865 *
1866 *
1867 * Function 8: Get the maximun available baud rate of this port.
1868 * Syntax:
1869 * long MoxaPortGetMaxBaud(int port);
1870 * int port : port number (0 - 127)
1871 *
1872 * return: 0 : this port is invalid
1873 * 38400/57600/115200 bps
1874 *
1875 *
1876 * Function 9: Get the current baud rate of this port.
1877 * Syntax:
1878 * long MoxaPortGetCurBaud(int port);
1879 * int port : port number (0 - 127)
1880 *
1881 * return: 0 : this port is invalid
1882 * 50 - 115200 bps
1883 *
1884 *
1885 * Function 10: Setting baud rate of this port.
1886 * Syntax:
1887 * long MoxaPortSetBaud(int port, long baud);
1888 * int port : port number (0 - 127)
1889 * long baud : baud rate (50 - 115200)
1890 *
1891 * return: 0 : this port is invalid or baud < 50
1892 * 50 - 115200 : the real baud rate set to the port, if
1893 * the argument baud is large than maximun
1894 * available baud rate, the real setting
1895 * baud rate will be the maximun baud rate.
1896 *
1897 *
1898 * Function 11: Setting the data-bits/stop-bits/parity of this port
1899 * Syntax:
1900 * int MoxaPortSetMode(int port, int databits, int stopbits, int parity);
1901 * int port : port number (0 - 127)
1902 * int databits : data bits (8/7/6/5)
1903 * int stopbits : stop bits (2/1/0, 0 show 1.5 stop bits)
1904 int parity : parity (0:None,1:Odd,2:Even,3:Mark,4:Space)
1905 *
1906 * return: -1 : invalid parameter
1907 * 0 : setting O.K.
1908 *
1909 *
1910 * Function 12: Configure the port.
1911 * Syntax:
Alan Coxc7bce302006-09-30 23:27:24 -07001912 * int MoxaPortSetTermio(int port, struct termios *termio, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 * int port : port number (0 - 127)
1914 * struct termios * termio : termio structure pointer
Alan Coxc7bce302006-09-30 23:27:24 -07001915 * speed_t baud : baud rate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 *
1917 * return: -1 : this port is invalid or termio == NULL
1918 * 0 : setting O.K.
1919 *
1920 *
1921 * Function 13: Get the DTR/RTS state of this port.
1922 * Syntax:
1923 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1924 * int port : port number (0 - 127)
1925 * int * dtrState : pointer to INT to receive the current DTR
1926 * state. (if NULL, this function will not
1927 * write to this address)
1928 * int * rtsState : pointer to INT to receive the current RTS
1929 * state. (if NULL, this function will not
1930 * write to this address)
1931 *
1932 * return: -1 : this port is invalid
1933 * 0 : O.K.
1934 *
1935 *
1936 * Function 14: Setting the DTR/RTS output state of this port.
1937 * Syntax:
1938 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1939 * int port : port number (0 - 127)
1940 * int dtrState : DTR output state (0: off, 1: on)
1941 * int rtsState : RTS output state (0: off, 1: on)
1942 *
1943 *
1944 * Function 15: Setting the flow control of this port.
1945 * Syntax:
1946 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1947 * int txFlow,int xany);
1948 * int port : port number (0 - 127)
1949 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1950 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1951 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1952 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1953 * int xany : S/W XANY flow control (0: no, 1: yes)
1954 *
1955 *
1956 * Function 16: Get ths line status of this port
1957 * Syntax:
1958 * int MoxaPortLineStatus(int port);
1959 * int port : port number (0 - 127)
1960 *
1961 * return: Bit 0 - CTS state (0: off, 1: on)
1962 * Bit 1 - DSR state (0: off, 1: on)
1963 * Bit 2 - DCD state (0: off, 1: on)
1964 *
1965 *
1966 * Function 17: Check the DCD state has changed since the last read
1967 * of this function.
1968 * Syntax:
1969 * int MoxaPortDCDChange(int port);
1970 * int port : port number (0 - 127)
1971 *
1972 * return: 0 : no changed
1973 * 1 : DCD has changed
1974 *
1975 *
1976 * Function 18: Check ths current DCD state is ON or not.
1977 * Syntax:
1978 * int MoxaPortDCDON(int port);
1979 * int port : port number (0 - 127)
1980 *
1981 * return: 0 : DCD off
1982 * 1 : DCD on
1983 *
1984 *
1985 * Function 19: Flush the Rx/Tx buffer data of this port.
1986 * Syntax:
1987 * void MoxaPortFlushData(int port, int mode);
1988 * int port : port number (0 - 127)
1989 * int mode
1990 * 0 : flush the Rx buffer
1991 * 1 : flush the Tx buffer
1992 * 2 : flush the Rx and Tx buffer
1993 *
1994 *
1995 * Function 20: Write data.
1996 * Syntax:
1997 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1998 * int port : port number (0 - 127)
1999 * unsigned char * buffer : pointer to write data buffer.
2000 * int length : write data length
2001 *
2002 * return: 0 - length : real write data length
2003 *
2004 *
2005 * Function 21: Read data.
2006 * Syntax:
Alan Cox33f0f882006-01-09 20:54:13 -08002007 * int MoxaPortReadData(int port, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 * int port : port number (0 - 127)
Alan Cox33f0f882006-01-09 20:54:13 -08002009 * struct tty_struct *tty : tty for data
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 *
2011 * return: 0 - length : real read data length
2012 *
2013 *
2014 * Function 22: Get the Tx buffer size of this port
2015 * Syntax:
2016 * int MoxaPortTxBufSize(int port);
2017 * int port : port number (0 - 127)
2018 *
2019 * return: .. : Tx buffer size
2020 *
2021 *
2022 * Function 23: Get the Rx buffer size of this port
2023 * Syntax:
2024 * int MoxaPortRxBufSize(int port);
2025 * int port : port number (0 - 127)
2026 *
2027 * return: .. : Rx buffer size
2028 *
2029 *
2030 * Function 24: Get the Tx buffer current queued data bytes
2031 * Syntax:
2032 * int MoxaPortTxQueue(int port);
2033 * int port : port number (0 - 127)
2034 *
2035 * return: .. : Tx buffer current queued data bytes
2036 *
2037 *
2038 * Function 25: Get the Tx buffer current free space
2039 * Syntax:
2040 * int MoxaPortTxFree(int port);
2041 * int port : port number (0 - 127)
2042 *
2043 * return: .. : Tx buffer current free space
2044 *
2045 *
2046 * Function 26: Get the Rx buffer current queued data bytes
2047 * Syntax:
2048 * int MoxaPortRxQueue(int port);
2049 * int port : port number (0 - 127)
2050 *
2051 * return: .. : Rx buffer current queued data bytes
2052 *
2053 *
2054 * Function 27: Get the Rx buffer current free space
2055 * Syntax:
2056 * int MoxaPortRxFree(int port);
2057 * int port : port number (0 - 127)
2058 *
2059 * return: .. : Rx buffer current free space
2060 *
2061 *
2062 * Function 28: Disable port data transmission.
2063 * Syntax:
2064 * void MoxaPortTxDisable(int port);
2065 * int port : port number (0 - 127)
2066 *
2067 *
2068 * Function 29: Enable port data transmission.
2069 * Syntax:
2070 * void MoxaPortTxEnable(int port);
2071 * int port : port number (0 - 127)
2072 *
2073 *
2074 * Function 30: Get the received BREAK signal count.
2075 * Syntax:
2076 * int MoxaPortGetBrkCnt(int port);
2077 * int port : port number (0 - 127)
2078 *
2079 * return: 0 - .. : BREAK signal count
2080 *
2081 *
2082 * Function 31: Get the received BREAK signal count and reset it.
2083 * Syntax:
2084 * int MoxaPortResetBrkCnt(int port);
2085 * int port : port number (0 - 127)
2086 *
2087 * return: 0 - .. : BREAK signal count
2088 *
2089 *
2090 * Function 32: Set the S/W flow control new XON/XOFF value, default
2091 * XON is 0x11 & XOFF is 0x13.
2092 * Syntax:
2093 * void MoxaPortSetXonXoff(int port, int xonValue, int xoffValue);
2094 * int port : port number (0 - 127)
2095 * int xonValue : new XON value (0 - 255)
2096 * int xoffValue : new XOFF value (0 - 255)
2097 *
2098 *
2099 * Function 33: Check this port's transmission is hold by remote site
2100 * because the flow control.
2101 * Syntax:
2102 * int MoxaPortIsTxHold(int port);
2103 * int port : port number (0 - 127)
2104 *
2105 * return: 0 : normal
2106 * 1 : hold by remote site
2107 *
2108 *
2109 * Function 34: Send out a BREAK signal.
2110 * Syntax:
2111 * void MoxaPortSendBreak(int port, int ms100);
2112 * int port : port number (0 - 127)
2113 * int ms100 : break signal time interval.
2114 * unit: 100 mini-second. if ms100 == 0, it will
2115 * send out a about 250 ms BREAK signal.
2116 *
2117 */
2118int MoxaPortIsValid(int port)
2119{
2120
2121 if (moxaCard == 0)
2122 return (0);
2123 if (moxaChkPort[port] == 0)
2124 return (0);
2125 return (1);
2126}
2127
2128void MoxaPortEnable(int port)
2129{
2130 void __iomem *ofsAddr;
2131 int MoxaPortLineStatus(int);
2132 short lowwater = 512;
2133
2134 ofsAddr = moxaTableAddr[port];
2135 writew(lowwater, ofsAddr + Low_water);
2136 moxaBreakCnt[port] = 0;
2137 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2138 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2139 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
2140 } else {
2141 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
2142 }
2143
2144 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
2145 moxafunc(ofsAddr, FC_FlushQueue, 2);
2146
2147 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
2148 MoxaPortLineStatus(port);
2149}
2150
2151void MoxaPortDisable(int port)
2152{
2153 void __iomem *ofsAddr = moxaTableAddr[port];
2154
2155 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
2156 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
2157 writew(0, ofsAddr + HostStat);
2158 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
2159}
2160
2161long MoxaPortGetMaxBaud(int port)
2162{
2163 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2164 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI))
2165 return (460800L);
2166 else
2167 return (921600L);
2168}
2169
2170
2171long MoxaPortSetBaud(int port, long baud)
2172{
2173 void __iomem *ofsAddr;
2174 long max, clock;
2175 unsigned int val;
2176
2177 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
2178 return (0);
2179 ofsAddr = moxaTableAddr[port];
2180 if (baud > max)
2181 baud = max;
2182 if (max == 38400L)
2183 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
2184 else if (max == 57600L)
2185 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
2186 else
2187 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
2188 val = clock / baud;
2189 moxafunc(ofsAddr, FC_SetBaud, val);
2190 baud = clock / val;
2191 moxaCurBaud[port] = baud;
2192 return (baud);
2193}
2194
Alan Coxc7bce302006-09-30 23:27:24 -07002195int MoxaPortSetTermio(int port, struct termios *termio, speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
2197 void __iomem *ofsAddr;
2198 tcflag_t cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 tcflag_t mode = 0;
2200
2201 if (moxaChkPort[port] == 0 || termio == 0)
2202 return (-1);
2203 ofsAddr = moxaTableAddr[port];
2204 cflag = termio->c_cflag; /* termio->c_cflag */
2205
2206 mode = termio->c_cflag & CSIZE;
2207 if (mode == CS5)
2208 mode = MX_CS5;
2209 else if (mode == CS6)
2210 mode = MX_CS6;
2211 else if (mode == CS7)
2212 mode = MX_CS7;
2213 else if (mode == CS8)
2214 mode = MX_CS8;
2215
2216 if (termio->c_cflag & CSTOPB) {
2217 if (mode == MX_CS5)
2218 mode |= MX_STOP15;
2219 else
2220 mode |= MX_STOP2;
2221 } else
2222 mode |= MX_STOP1;
2223
2224 if (termio->c_cflag & PARENB) {
2225 if (termio->c_cflag & PARODD)
2226 mode |= MX_PARODD;
2227 else
2228 mode |= MX_PAREVEN;
2229 } else
2230 mode |= MX_PARNONE;
2231
2232 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
2233
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2235 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
Alan Coxc7bce302006-09-30 23:27:24 -07002236 if (baud >= 921600L)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return (-1);
2238 }
2239 MoxaPortSetBaud(port, baud);
2240
2241 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
2242 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
2243 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
2244 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
2245 wait_finish(ofsAddr);
2246
2247 }
2248 return (0);
2249}
2250
2251int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState)
2252{
2253
2254 if (!MoxaPortIsValid(port))
2255 return (-1);
2256 if (dtrState) {
2257 if (moxaLineCtrl[port] & DTR_ON)
2258 *dtrState = 1;
2259 else
2260 *dtrState = 0;
2261 }
2262 if (rtsState) {
2263 if (moxaLineCtrl[port] & RTS_ON)
2264 *rtsState = 1;
2265 else
2266 *rtsState = 0;
2267 }
2268 return (0);
2269}
2270
2271void MoxaPortLineCtrl(int port, int dtr, int rts)
2272{
2273 void __iomem *ofsAddr;
2274 int mode;
2275
2276 ofsAddr = moxaTableAddr[port];
2277 mode = 0;
2278 if (dtr)
2279 mode |= DTR_ON;
2280 if (rts)
2281 mode |= RTS_ON;
2282 moxaLineCtrl[port] = mode;
2283 moxafunc(ofsAddr, FC_LineControl, mode);
2284}
2285
2286void MoxaPortFlowCtrl(int port, int rts, int cts, int txflow, int rxflow, int txany)
2287{
2288 void __iomem *ofsAddr;
2289 int mode;
2290
2291 ofsAddr = moxaTableAddr[port];
2292 mode = 0;
2293 if (rts)
2294 mode |= RTS_FlowCtl;
2295 if (cts)
2296 mode |= CTS_FlowCtl;
2297 if (txflow)
2298 mode |= Tx_FlowCtl;
2299 if (rxflow)
2300 mode |= Rx_FlowCtl;
2301 if (txany)
2302 mode |= IXM_IXANY;
2303 moxafunc(ofsAddr, FC_SetFlowCtl, mode);
2304}
2305
2306int MoxaPortLineStatus(int port)
2307{
2308 void __iomem *ofsAddr;
2309 int val;
2310
2311 ofsAddr = moxaTableAddr[port];
2312 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2313 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2314 moxafunc(ofsAddr, FC_LineStatus, 0);
2315 val = readw(ofsAddr + FuncArg);
2316 } else {
2317 val = readw(ofsAddr + FlagStat) >> 4;
2318 }
2319 val &= 0x0B;
2320 if (val & 8) {
2321 val |= 4;
2322 if ((moxaDCDState[port] & DCD_oldstate) == 0)
2323 moxaDCDState[port] = (DCD_oldstate | DCD_changed);
2324 } else {
2325 if (moxaDCDState[port] & DCD_oldstate)
2326 moxaDCDState[port] = DCD_changed;
2327 }
2328 val &= 7;
2329 return (val);
2330}
2331
2332int MoxaPortDCDChange(int port)
2333{
2334 int n;
2335
2336 if (moxaChkPort[port] == 0)
2337 return (0);
2338 n = moxaDCDState[port];
2339 moxaDCDState[port] &= ~DCD_changed;
2340 n &= DCD_changed;
2341 return (n);
2342}
2343
2344int MoxaPortDCDON(int port)
2345{
2346 int n;
2347
2348 if (moxaChkPort[port] == 0)
2349 return (0);
2350 if (moxaDCDState[port] & DCD_oldstate)
2351 n = 1;
2352 else
2353 n = 0;
2354 return (n);
2355}
2356
2357
2358/*
2359 int MoxaDumpMem(int port, unsigned char * buffer, int len)
2360 {
2361 int i;
2362 unsigned long baseAddr,ofsAddr,ofs;
2363
2364 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2365 ofs = baseAddr + DynPage_addr + pageofs;
2366 if (len > 0x2000L)
2367 len = 0x2000L;
2368 for (i = 0; i < len; i++)
2369 buffer[i] = readb(ofs+i);
2370 }
2371 */
2372
2373
2374int MoxaPortWriteData(int port, unsigned char * buffer, int len)
2375{
2376 int c, total, i;
2377 ushort tail;
2378 int cnt;
2379 ushort head, tx_mask, spage, epage;
2380 ushort pageno, pageofs, bufhead;
2381 void __iomem *baseAddr, *ofsAddr, *ofs;
2382
2383 ofsAddr = moxaTableAddr[port];
2384 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2385 tx_mask = readw(ofsAddr + TX_mask);
2386 spage = readw(ofsAddr + Page_txb);
2387 epage = readw(ofsAddr + EndPage_txb);
2388 tail = readw(ofsAddr + TXwptr);
2389 head = readw(ofsAddr + TXrptr);
2390 c = (head > tail) ? (head - tail - 1)
2391 : (head - tail + tx_mask);
2392 if (c > len)
2393 c = len;
2394 moxaLog.txcnt[port] += c;
2395 total = c;
2396 if (spage == epage) {
2397 bufhead = readw(ofsAddr + Ofs_txb);
2398 writew(spage, baseAddr + Control_reg);
2399 while (c > 0) {
2400 if (head > tail)
2401 len = head - tail - 1;
2402 else
2403 len = tx_mask + 1 - tail;
2404 len = (c > len) ? len : c;
2405 ofs = baseAddr + DynPage_addr + bufhead + tail;
2406 for (i = 0; i < len; i++)
2407 writeb(*buffer++, ofs + i);
2408 tail = (tail + len) & tx_mask;
2409 c -= len;
2410 }
2411 writew(tail, ofsAddr + TXwptr);
2412 } else {
2413 len = c;
2414 pageno = spage + (tail >> 13);
2415 pageofs = tail & Page_mask;
2416 do {
2417 cnt = Page_size - pageofs;
2418 if (cnt > c)
2419 cnt = c;
2420 c -= cnt;
2421 writeb(pageno, baseAddr + Control_reg);
2422 ofs = baseAddr + DynPage_addr + pageofs;
2423 for (i = 0; i < cnt; i++)
2424 writeb(*buffer++, ofs + i);
2425 if (c == 0) {
2426 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2427 break;
2428 }
2429 if (++pageno == epage)
2430 pageno = spage;
2431 pageofs = 0;
2432 } while (1);
2433 }
2434 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2435 return (total);
2436}
2437
Alan Cox33f0f882006-01-09 20:54:13 -08002438int MoxaPortReadData(int port, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439{
2440 register ushort head, pageofs;
2441 int i, count, cnt, len, total, remain;
2442 ushort tail, rx_mask, spage, epage;
2443 ushort pageno, bufhead;
2444 void __iomem *baseAddr, *ofsAddr, *ofs;
2445
2446 ofsAddr = moxaTableAddr[port];
2447 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2448 head = readw(ofsAddr + RXrptr);
2449 tail = readw(ofsAddr + RXwptr);
2450 rx_mask = readw(ofsAddr + RX_mask);
2451 spage = readw(ofsAddr + Page_rxb);
2452 epage = readw(ofsAddr + EndPage_rxb);
2453 count = (tail >= head) ? (tail - head)
2454 : (tail - head + rx_mask + 1);
2455 if (count == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08002456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
Alan Cox33f0f882006-01-09 20:54:13 -08002458 total = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 remain = count - total;
2460 moxaLog.rxcnt[port] += total;
2461 count = total;
2462 if (spage == epage) {
2463 bufhead = readw(ofsAddr + Ofs_rxb);
2464 writew(spage, baseAddr + Control_reg);
2465 while (count > 0) {
2466 if (tail >= head)
2467 len = tail - head;
2468 else
2469 len = rx_mask + 1 - head;
2470 len = (count > len) ? len : count;
2471 ofs = baseAddr + DynPage_addr + bufhead + head;
2472 for (i = 0; i < len; i++)
Alan Cox33f0f882006-01-09 20:54:13 -08002473 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 head = (head + len) & rx_mask;
2475 count -= len;
2476 }
2477 writew(head, ofsAddr + RXrptr);
2478 } else {
2479 len = count;
2480 pageno = spage + (head >> 13);
2481 pageofs = head & Page_mask;
2482 do {
2483 cnt = Page_size - pageofs;
2484 if (cnt > count)
2485 cnt = count;
2486 count -= cnt;
2487 writew(pageno, baseAddr + Control_reg);
2488 ofs = baseAddr + DynPage_addr + pageofs;
2489 for (i = 0; i < cnt; i++)
Alan Cox33f0f882006-01-09 20:54:13 -08002490 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 if (count == 0) {
2492 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2493 break;
2494 }
2495 if (++pageno == epage)
2496 pageno = spage;
2497 pageofs = 0;
2498 } while (1);
2499 }
2500 if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2501 moxaLowWaterChk = 1;
2502 moxaLowChkFlag[port] = 1;
2503 }
2504 return (total);
2505}
2506
2507
2508int MoxaPortTxQueue(int port)
2509{
2510 void __iomem *ofsAddr;
2511 ushort rptr, wptr, mask;
2512 int len;
2513
2514 ofsAddr = moxaTableAddr[port];
2515 rptr = readw(ofsAddr + TXrptr);
2516 wptr = readw(ofsAddr + TXwptr);
2517 mask = readw(ofsAddr + TX_mask);
2518 len = (wptr - rptr) & mask;
2519 return (len);
2520}
2521
2522int MoxaPortTxFree(int port)
2523{
2524 void __iomem *ofsAddr;
2525 ushort rptr, wptr, mask;
2526 int len;
2527
2528 ofsAddr = moxaTableAddr[port];
2529 rptr = readw(ofsAddr + TXrptr);
2530 wptr = readw(ofsAddr + TXwptr);
2531 mask = readw(ofsAddr + TX_mask);
2532 len = mask - ((wptr - rptr) & mask);
2533 return (len);
2534}
2535
2536int MoxaPortRxQueue(int port)
2537{
2538 void __iomem *ofsAddr;
2539 ushort rptr, wptr, mask;
2540 int len;
2541
2542 ofsAddr = moxaTableAddr[port];
2543 rptr = readw(ofsAddr + RXrptr);
2544 wptr = readw(ofsAddr + RXwptr);
2545 mask = readw(ofsAddr + RX_mask);
2546 len = (wptr - rptr) & mask;
2547 return (len);
2548}
2549
2550
2551void MoxaPortTxDisable(int port)
2552{
2553 void __iomem *ofsAddr;
2554
2555 ofsAddr = moxaTableAddr[port];
2556 moxafunc(ofsAddr, FC_SetXoffState, Magic_code);
2557}
2558
2559void MoxaPortTxEnable(int port)
2560{
2561 void __iomem *ofsAddr;
2562
2563 ofsAddr = moxaTableAddr[port];
2564 moxafunc(ofsAddr, FC_SetXonState, Magic_code);
2565}
2566
2567
2568int MoxaPortResetBrkCnt(int port)
2569{
2570 ushort cnt;
2571 cnt = moxaBreakCnt[port];
2572 moxaBreakCnt[port] = 0;
2573 return (cnt);
2574}
2575
2576
2577void MoxaPortSendBreak(int port, int ms100)
2578{
2579 void __iomem *ofsAddr;
2580
2581 ofsAddr = moxaTableAddr[port];
2582 if (ms100) {
2583 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2584 moxadelay(ms100 * (HZ / 10));
2585 } else {
2586 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2587 moxadelay(HZ / 4); /* 250 ms */
2588 }
2589 moxafunc(ofsAddr, FC_StopBreak, Magic_code);
2590}
2591
2592static int moxa_get_serial_info(struct moxa_str *info,
2593 struct serial_struct __user *retinfo)
2594{
2595 struct serial_struct tmp;
2596
2597 memset(&tmp, 0, sizeof(tmp));
2598 tmp.type = info->type;
2599 tmp.line = info->port;
2600 tmp.port = 0;
2601 tmp.irq = 0;
2602 tmp.flags = info->asyncflags;
2603 tmp.baud_base = 921600;
2604 tmp.close_delay = info->close_delay;
2605 tmp.closing_wait = info->closing_wait;
2606 tmp.custom_divisor = 0;
2607 tmp.hub6 = 0;
2608 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2609 return -EFAULT;
2610 return (0);
2611}
2612
2613
2614static int moxa_set_serial_info(struct moxa_str *info,
2615 struct serial_struct __user *new_info)
2616{
2617 struct serial_struct new_serial;
2618
2619 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2620 return -EFAULT;
2621
2622 if ((new_serial.irq != 0) ||
2623 (new_serial.port != 0) ||
2624// (new_serial.type != info->type) ||
2625 (new_serial.custom_divisor != 0) ||
2626 (new_serial.baud_base != 921600))
2627 return (-EPERM);
2628
2629 if (!capable(CAP_SYS_ADMIN)) {
2630 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2631 (info->asyncflags & ~ASYNC_USR_MASK)))
2632 return (-EPERM);
2633 } else {
2634 info->close_delay = new_serial.close_delay * HZ / 100;
2635 info->closing_wait = new_serial.closing_wait * HZ / 100;
2636 }
2637
2638 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2639 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2640
2641 if (new_serial.type == PORT_16550A) {
2642 MoxaSetFifo(info->port, 1);
2643 } else {
2644 MoxaSetFifo(info->port, 0);
2645 }
2646
2647 info->type = new_serial.type;
2648 return (0);
2649}
2650
2651
2652
2653/*****************************************************************************
2654 * Static local functions: *
2655 *****************************************************************************/
2656/*
2657 * moxadelay - delays a specified number ticks
2658 */
2659static void moxadelay(int tick)
2660{
2661 unsigned long st, et;
2662
2663 st = jiffies;
2664 et = st + tick;
2665 while (time_before(jiffies, et));
2666}
2667
2668static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
2669{
2670
2671 writew(arg, ofsAddr + FuncArg);
2672 writew(cmd, ofsAddr + FuncCode);
2673 wait_finish(ofsAddr);
2674}
2675
2676static void wait_finish(void __iomem *ofsAddr)
2677{
2678 unsigned long i, j;
2679
2680 i = jiffies;
2681 while (readw(ofsAddr + FuncCode) != 0) {
2682 j = jiffies;
2683 if ((j - i) > moxaFuncTout) {
2684 return;
2685 }
2686 }
2687}
2688
2689static void low_water_check(void __iomem *ofsAddr)
2690{
2691 int len;
2692 ushort rptr, wptr, mask;
2693
2694 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2695 rptr = readw(ofsAddr + RXrptr);
2696 wptr = readw(ofsAddr + RXwptr);
2697 mask = readw(ofsAddr + RX_mask);
2698 len = (wptr - rptr) & mask;
2699 if (len <= Low_water)
2700 moxafunc(ofsAddr, FC_SendXon, 0);
2701 }
2702}
2703
2704static int moxaloadbios(int cardno, unsigned char __user *tmp, int len)
2705{
2706 void __iomem *baseAddr;
2707 int i;
2708
2709 if(copy_from_user(moxaBuff, tmp, len))
2710 return -EFAULT;
2711 baseAddr = moxaBaseAddr[cardno];
2712 writeb(HW_reset, baseAddr + Control_reg); /* reset */
2713 moxadelay(1); /* delay 10 ms */
2714 for (i = 0; i < 4096; i++)
2715 writeb(0, baseAddr + i); /* clear fix page */
2716 for (i = 0; i < len; i++)
2717 writeb(moxaBuff[i], baseAddr + i); /* download BIOS */
2718 writeb(0, baseAddr + Control_reg); /* restart */
2719 return (0);
2720}
2721
2722static int moxafindcard(int cardno)
2723{
2724 void __iomem *baseAddr;
2725 ushort tmp;
2726
2727 baseAddr = moxaBaseAddr[cardno];
2728 switch (moxa_boards[cardno].boardType) {
2729 case MOXA_BOARD_C218_ISA:
2730 case MOXA_BOARD_C218_PCI:
2731 if ((tmp = readw(baseAddr + C218_key)) != C218_KeyCode) {
2732 return (-1);
2733 }
2734 break;
2735 case MOXA_BOARD_CP204J:
2736 if ((tmp = readw(baseAddr + C218_key)) != CP204J_KeyCode) {
2737 return (-1);
2738 }
2739 break;
2740 default:
2741 if ((tmp = readw(baseAddr + C320_key)) != C320_KeyCode) {
2742 return (-1);
2743 }
2744 if ((tmp = readw(baseAddr + C320_status)) != STS_init) {
2745 return (-2);
2746 }
2747 }
2748 return (0);
2749}
2750
2751static int moxaload320b(int cardno, unsigned char __user *tmp, int len)
2752{
2753 void __iomem *baseAddr;
2754 int i;
2755
2756 if(len > sizeof(moxaBuff))
2757 return -EINVAL;
2758 if(copy_from_user(moxaBuff, tmp, len))
2759 return -EFAULT;
2760 baseAddr = moxaBaseAddr[cardno];
2761 writew(len - 7168 - 2, baseAddr + C320bapi_len);
2762 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
2763 for (i = 0; i < 7168; i++)
2764 writeb(moxaBuff[i], baseAddr + DynPage_addr + i);
2765 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
2766 for (i = 0; i < (len - 7168); i++)
2767 writeb(moxaBuff[i + 7168], baseAddr + DynPage_addr + i);
2768 return (0);
2769}
2770
2771static int moxaloadcode(int cardno, unsigned char __user *tmp, int len)
2772{
2773 void __iomem *baseAddr, *ofsAddr;
2774 int retval, port, i;
2775
2776 if(copy_from_user(moxaBuff, tmp, len))
2777 return -EFAULT;
2778 baseAddr = moxaBaseAddr[cardno];
2779 switch (moxa_boards[cardno].boardType) {
2780 case MOXA_BOARD_C218_ISA:
2781 case MOXA_BOARD_C218_PCI:
2782 case MOXA_BOARD_CP204J:
2783 retval = moxaloadc218(cardno, baseAddr, len);
2784 if (retval)
2785 return (retval);
2786 port = cardno * MAX_PORTS_PER_BOARD;
2787 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2788 moxaChkPort[port] = 1;
2789 moxaCurBaud[port] = 9600L;
2790 moxaDCDState[port] = 0;
2791 moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
2792 ofsAddr = moxaTableAddr[port];
2793 writew(C218rx_mask, ofsAddr + RX_mask);
2794 writew(C218tx_mask, ofsAddr + TX_mask);
2795 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
2796 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
2797
2798 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
2799 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
2800
2801 }
2802 break;
2803 default:
2804 retval = moxaloadc320(cardno, baseAddr, len,
2805 &moxa_boards[cardno].numPorts);
2806 if (retval)
2807 return (retval);
2808 port = cardno * MAX_PORTS_PER_BOARD;
2809 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2810 moxaChkPort[port] = 1;
2811 moxaCurBaud[port] = 9600L;
2812 moxaDCDState[port] = 0;
2813 moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
2814 ofsAddr = moxaTableAddr[port];
2815 if (moxa_boards[cardno].numPorts == 8) {
2816 writew(C320p8rx_mask, ofsAddr + RX_mask);
2817 writew(C320p8tx_mask, ofsAddr + TX_mask);
2818 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
2819 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
2820 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
2821 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
2822
2823 } else if (moxa_boards[cardno].numPorts == 16) {
2824 writew(C320p16rx_mask, ofsAddr + RX_mask);
2825 writew(C320p16tx_mask, ofsAddr + TX_mask);
2826 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
2827 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
2828 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
2829 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
2830
2831 } else if (moxa_boards[cardno].numPorts == 24) {
2832 writew(C320p24rx_mask, ofsAddr + RX_mask);
2833 writew(C320p24tx_mask, ofsAddr + TX_mask);
2834 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
2835 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
2836 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
2837 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2838 } else if (moxa_boards[cardno].numPorts == 32) {
2839 writew(C320p32rx_mask, ofsAddr + RX_mask);
2840 writew(C320p32tx_mask, ofsAddr + TX_mask);
2841 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
2842 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
2843 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
2844 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
2845 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2846 }
2847 }
2848 break;
2849 }
Dirk Eibach01cfaf02006-08-27 01:23:36 -07002850 loadstat[cardno] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 return (0);
2852}
2853
2854static int moxaloadc218(int cardno, void __iomem *baseAddr, int len)
2855{
2856 char retry;
2857 int i, j, len1, len2;
2858 ushort usum, *ptr, keycode;
2859
2860 if (moxa_boards[cardno].boardType == MOXA_BOARD_CP204J)
2861 keycode = CP204J_KeyCode;
2862 else
2863 keycode = C218_KeyCode;
2864 usum = 0;
2865 len1 = len >> 1;
2866 ptr = (ushort *) moxaBuff;
2867 for (i = 0; i < len1; i++)
Dirk Eibach01cfaf02006-08-27 01:23:36 -07002868 usum += le16_to_cpu(*(ptr + i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 retry = 0;
2870 do {
2871 len1 = len >> 1;
2872 j = 0;
2873 while (len1) {
2874 len2 = (len1 > 2048) ? 2048 : len1;
2875 len1 -= len2;
2876 for (i = 0; i < len2 << 1; i++)
2877 writeb(moxaBuff[i + j], baseAddr + C218_LoadBuf + i);
2878 j += i;
2879
2880 writew(len2, baseAddr + C218DLoad_len);
2881 writew(0, baseAddr + C218_key);
2882 for (i = 0; i < 100; i++) {
2883 if (readw(baseAddr + C218_key) == keycode)
2884 break;
2885 moxadelay(1); /* delay 10 ms */
2886 }
2887 if (readw(baseAddr + C218_key) != keycode) {
2888 return (-1);
2889 }
2890 }
2891 writew(0, baseAddr + C218DLoad_len);
2892 writew(usum, baseAddr + C218check_sum);
2893 writew(0, baseAddr + C218_key);
2894 for (i = 0; i < 100; i++) {
2895 if (readw(baseAddr + C218_key) == keycode)
2896 break;
2897 moxadelay(1); /* delay 10 ms */
2898 }
2899 retry++;
2900 } while ((readb(baseAddr + C218chksum_ok) != 1) && (retry < 3));
2901 if (readb(baseAddr + C218chksum_ok) != 1) {
2902 return (-1);
2903 }
2904 writew(0, baseAddr + C218_key);
2905 for (i = 0; i < 100; i++) {
2906 if (readw(baseAddr + Magic_no) == Magic_code)
2907 break;
2908 moxadelay(1); /* delay 10 ms */
2909 }
2910 if (readw(baseAddr + Magic_no) != Magic_code) {
2911 return (-1);
2912 }
2913 writew(1, baseAddr + Disable_IRQ);
2914 writew(0, baseAddr + Magic_no);
2915 for (i = 0; i < 100; i++) {
2916 if (readw(baseAddr + Magic_no) == Magic_code)
2917 break;
2918 moxadelay(1); /* delay 10 ms */
2919 }
2920 if (readw(baseAddr + Magic_no) != Magic_code) {
2921 return (-1);
2922 }
2923 moxaCard = 1;
2924 moxaIntNdx[cardno] = baseAddr + IRQindex;
2925 moxaIntPend[cardno] = baseAddr + IRQpending;
2926 moxaIntTable[cardno] = baseAddr + IRQtable;
2927 return (0);
2928}
2929
2930static int moxaloadc320(int cardno, void __iomem *baseAddr, int len, int *numPorts)
2931{
2932 ushort usum;
2933 int i, j, wlen, len2, retry;
2934 ushort *uptr;
2935
2936 usum = 0;
2937 wlen = len >> 1;
2938 uptr = (ushort *) moxaBuff;
2939 for (i = 0; i < wlen; i++)
Dirk Eibach01cfaf02006-08-27 01:23:36 -07002940 usum += le16_to_cpu(uptr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 retry = 0;
2942 j = 0;
2943 do {
2944 while (wlen) {
2945 if (wlen > 2048)
2946 len2 = 2048;
2947 else
2948 len2 = wlen;
2949 wlen -= len2;
2950 len2 <<= 1;
2951 for (i = 0; i < len2; i++)
2952 writeb(moxaBuff[j + i], baseAddr + C320_LoadBuf + i);
2953 len2 >>= 1;
2954 j += i;
2955 writew(len2, baseAddr + C320DLoad_len);
2956 writew(0, baseAddr + C320_key);
2957 for (i = 0; i < 10; i++) {
2958 if (readw(baseAddr + C320_key) == C320_KeyCode)
2959 break;
2960 moxadelay(1);
2961 }
2962 if (readw(baseAddr + C320_key) != C320_KeyCode)
2963 return (-1);
2964 }
2965 writew(0, baseAddr + C320DLoad_len);
2966 writew(usum, baseAddr + C320check_sum);
2967 writew(0, baseAddr + C320_key);
2968 for (i = 0; i < 10; i++) {
2969 if (readw(baseAddr + C320_key) == C320_KeyCode)
2970 break;
2971 moxadelay(1);
2972 }
2973 retry++;
2974 } while ((readb(baseAddr + C320chksum_ok) != 1) && (retry < 3));
2975 if (readb(baseAddr + C320chksum_ok) != 1)
2976 return (-1);
2977 writew(0, baseAddr + C320_key);
2978 for (i = 0; i < 600; i++) {
2979 if (readw(baseAddr + Magic_no) == Magic_code)
2980 break;
2981 moxadelay(1);
2982 }
2983 if (readw(baseAddr + Magic_no) != Magic_code)
2984 return (-100);
2985
2986 if (moxa_boards[cardno].busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
2987 writew(0x3800, baseAddr + TMS320_PORT1);
2988 writew(0x3900, baseAddr + TMS320_PORT2);
2989 writew(28499, baseAddr + TMS320_CLOCK);
2990 } else {
2991 writew(0x3200, baseAddr + TMS320_PORT1);
2992 writew(0x3400, baseAddr + TMS320_PORT2);
2993 writew(19999, baseAddr + TMS320_CLOCK);
2994 }
2995 writew(1, baseAddr + Disable_IRQ);
2996 writew(0, baseAddr + Magic_no);
2997 for (i = 0; i < 500; i++) {
2998 if (readw(baseAddr + Magic_no) == Magic_code)
2999 break;
3000 moxadelay(1);
3001 }
3002 if (readw(baseAddr + Magic_no) != Magic_code)
3003 return (-102);
3004
3005 j = readw(baseAddr + Module_cnt);
3006 if (j <= 0)
3007 return (-101);
3008 *numPorts = j * 8;
3009 writew(j, baseAddr + Module_no);
3010 writew(0, baseAddr + Magic_no);
3011 for (i = 0; i < 600; i++) {
3012 if (readw(baseAddr + Magic_no) == Magic_code)
3013 break;
3014 moxadelay(1);
3015 }
3016 if (readw(baseAddr + Magic_no) != Magic_code)
3017 return (-102);
3018 moxaCard = 1;
3019 moxaIntNdx[cardno] = baseAddr + IRQindex;
3020 moxaIntPend[cardno] = baseAddr + IRQpending;
3021 moxaIntTable[cardno] = baseAddr + IRQtable;
3022 return (0);
3023}
3024
3025#if 0
3026long MoxaPortGetCurBaud(int port)
3027{
3028
3029 if (moxaChkPort[port] == 0)
3030 return (0);
3031 return (moxaCurBaud[port]);
3032}
3033#endif /* 0 */
3034
3035static void MoxaSetFifo(int port, int enable)
3036{
3037 void __iomem *ofsAddr = moxaTableAddr[port];
3038
3039 if (!enable) {
3040 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
3041 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
3042 } else {
3043 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
3044 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
3045 }
3046}
3047
3048#if 0
3049int MoxaPortSetMode(int port, int databits, int stopbits, int parity)
3050{
3051 void __iomem *ofsAddr;
3052 int val;
3053
3054 val = 0;
3055 switch (databits) {
3056 case 5:
3057 val |= 0;
3058 break;
3059 case 6:
3060 val |= 1;
3061 break;
3062 case 7:
3063 val |= 2;
3064 break;
3065 case 8:
3066 val |= 3;
3067 break;
3068 default:
3069 return (-1);
3070 }
3071 switch (stopbits) {
3072 case 0:
3073 val |= 0;
3074 break; /* stop bits 1.5 */
3075 case 1:
3076 val |= 0;
3077 break;
3078 case 2:
3079 val |= 4;
3080 break;
3081 default:
3082 return (-1);
3083 }
3084 switch (parity) {
3085 case 0:
3086 val |= 0x00;
3087 break; /* None */
3088 case 1:
3089 val |= 0x08;
3090 break; /* Odd */
3091 case 2:
3092 val |= 0x18;
3093 break; /* Even */
3094 case 3:
3095 val |= 0x28;
3096 break; /* Mark */
3097 case 4:
3098 val |= 0x38;
3099 break; /* Space */
3100 default:
3101 return (-1);
3102 }
3103 ofsAddr = moxaTableAddr[port];
3104 moxafunc(ofsAddr, FC_SetMode, val);
3105 return (0);
3106}
3107
3108int MoxaPortTxBufSize(int port)
3109{
3110 void __iomem *ofsAddr;
3111 int size;
3112
3113 ofsAddr = moxaTableAddr[port];
3114 size = readw(ofsAddr + TX_mask);
3115 return (size);
3116}
3117
3118int MoxaPortRxBufSize(int port)
3119{
3120 void __iomem *ofsAddr;
3121 int size;
3122
3123 ofsAddr = moxaTableAddr[port];
3124 size = readw(ofsAddr + RX_mask);
3125 return (size);
3126}
3127
3128int MoxaPortRxFree(int port)
3129{
3130 void __iomem *ofsAddr;
3131 ushort rptr, wptr, mask;
3132 int len;
3133
3134 ofsAddr = moxaTableAddr[port];
3135 rptr = readw(ofsAddr + RXrptr);
3136 wptr = readw(ofsAddr + RXwptr);
3137 mask = readw(ofsAddr + RX_mask);
3138 len = mask - ((wptr - rptr) & mask);
3139 return (len);
3140}
3141int MoxaPortGetBrkCnt(int port)
3142{
3143 return (moxaBreakCnt[port]);
3144}
3145
3146void MoxaPortSetXonXoff(int port, int xonValue, int xoffValue)
3147{
3148 void __iomem *ofsAddr;
3149
3150 ofsAddr = moxaTableAddr[port];
3151 writew(xonValue, ofsAddr + FuncArg);
3152 writew(xoffValue, ofsAddr + FuncArg1);
3153 writew(FC_SetXonXoff, ofsAddr + FuncCode);
3154 wait_finish(ofsAddr);
3155}
3156
3157int MoxaPortIsTxHold(int port)
3158{
3159 void __iomem *ofsAddr;
3160 int val;
3161
3162 ofsAddr = moxaTableAddr[port];
3163 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
3164 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
3165 moxafunc(ofsAddr, FC_GetCCSR, 0);
3166 val = readw(ofsAddr + FuncArg);
3167 if (val & 0x04)
3168 return (1);
3169 } else {
3170 if (readw(ofsAddr + FlagStat) & Tx_flowOff)
3171 return (1);
3172 }
3173 return (0);
3174}
3175#endif