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