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