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