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