moxa.c 74.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/*****************************************************************************/
/*
 *           moxa.c  -- MOXA Intellio family multiport serial driver.
 *
 *      Copyright (C) 1999-2000  Moxa Technologies (support@moxa.com.tw).
 *
 *      This code is loosely based on the Linux serial driver, written by
 *      Linus Torvalds, Theodore T'so and others.
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 */

/*
 *    MOXA Intellio Series Driver
 *      for             : LINUX
 *      date            : 1999/1/7
 *      version         : 5.1
 */

#include <linux/module.h>
#include <linux/types.h>
#include <linux/mm.h>
#include <linux/ioport.h>
#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/interrupt.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/major.h>
#include <linux/string.h>
#include <linux/fcntl.h>
#include <linux/ptrace.h>
#include <linux/serial.h>
#include <linux/tty_driver.h>
#include <linux/delay.h>
#include <linux/pci.h>
#include <linux/init.h>
#include <linux/bitops.h>
J
Jiri Slaby 已提交
44
#include <linux/completion.h>
L
Linus Torvalds 已提交
45 46 47 48 49

#include <asm/system.h>
#include <asm/io.h>
#include <asm/uaccess.h>

J
Jiri Slaby 已提交
50
#define MOXA_VERSION		"5.1k"
L
Linus Torvalds 已提交
51

J
Jiri Slaby 已提交
52 53
#define MOXAMAJOR		172
#define MOXACUMAJOR		173
L
Linus Torvalds 已提交
54

J
Jiri Slaby 已提交
55
#define MAX_BOARDS		4	/* Don't change this value */
L
Linus Torvalds 已提交
56
#define MAX_PORTS_PER_BOARD	32	/* Don't change this value */
J
Jiri Slaby 已提交
57
#define MAX_PORTS		(MAX_BOARDS * MAX_PORTS_PER_BOARD)
L
Linus Torvalds 已提交
58 59 60 61

/*
 *    Define the Moxa PCI vendor and device IDs.
 */
J
Jiri Slaby 已提交
62 63
#define MOXA_BUS_TYPE_ISA	0
#define MOXA_BUS_TYPE_PCI	1
L
Linus Torvalds 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

enum {
	MOXA_BOARD_C218_PCI = 1,
	MOXA_BOARD_C218_ISA,
	MOXA_BOARD_C320_PCI,
	MOXA_BOARD_C320_ISA,
	MOXA_BOARD_CP204J,
};

static char *moxa_brdname[] =
{
	"C218 Turbo PCI series",
	"C218 Turbo ISA series",
	"C320 Turbo PCI series",
	"C320 Turbo ISA series",
	"CP-204J series",
};

#ifdef CONFIG_PCI
static struct pci_device_id moxa_pcibrds[] = {
J
Jiri Slaby 已提交
84 85 86 87 88 89
	{ PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
		.driver_data = MOXA_BOARD_C218_PCI },
	{ PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
		.driver_data = MOXA_BOARD_C320_PCI },
	{ PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
		.driver_data = MOXA_BOARD_CP204J },
L
Linus Torvalds 已提交
90 91 92 93 94
	{ 0 }
};
MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
#endif /* CONFIG_PCI */

95
static struct moxa_board_conf {
L
Linus Torvalds 已提交
96 97 98 99
	int boardType;
	int numPorts;
	unsigned long baseAddr;
	int busType;
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

	int loadstat;

	void __iomem *basemem;
	void __iomem *intNdx;
	void __iomem *intPend;
	void __iomem *intTable;
} moxa_boards[MAX_BOARDS];

struct mxser_mstatus {
	tcflag_t cflag;
	int cts;
	int dsr;
	int ri;
	int dcd;
115
};
L
Linus Torvalds 已提交
116

117 118 119 120
struct moxaq_str {
	int inq;
	int outq;
};
L
Linus Torvalds 已提交
121

122
struct moxa_port {
L
Linus Torvalds 已提交
123 124 125 126 127 128 129 130 131 132 133 134
	int type;
	int port;
	int close_delay;
	unsigned short closing_wait;
	int count;
	int blocked_open;
	long event; /* long req'd for set_bit --RR */
	int asyncflags;
	unsigned long statusflags;
	struct tty_struct *tty;
	int cflag;
	wait_queue_head_t open_wait;
J
Jiri Slaby 已提交
135
	struct completion close_wait;
L
Linus Torvalds 已提交
136

137
	struct timer_list emptyTimer;
L
Linus Torvalds 已提交
138

139 140 141 142 143 144 145 146 147
	char chkPort;
	char lineCtrl;
	void __iomem *tableAddr;
	long curBaud;
	char DCDState;
	char lowChkFlag;

	ushort breakCnt;
};
L
Linus Torvalds 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161

/* statusflags */
#define TXSTOPPED	0x1
#define LOWWAIT 	0x2
#define EMPTYWAIT	0x4
#define THROTTLE	0x8

#define SERIAL_DO_RESTART

#define WAKEUP_CHARS		256

static int ttymajor = MOXAMAJOR;
/* Variables for insmod */
#ifdef MODULE
162 163 164
static int baseaddr[4];
static int type[4];
static int numports[4];
L
Linus Torvalds 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
#endif

MODULE_AUTHOR("William Chen");
MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
MODULE_LICENSE("GPL");
#ifdef MODULE
module_param_array(type, int, NULL, 0);
module_param_array(baseaddr, int, NULL, 0);
module_param_array(numports, int, NULL, 0);
#endif
module_param(ttymajor, int, 0);

/*
 * static functions:
 */
static int moxa_open(struct tty_struct *, struct file *);
static void moxa_close(struct tty_struct *, struct file *);
static int moxa_write(struct tty_struct *, const unsigned char *, int);
static int moxa_write_room(struct tty_struct *);
static void moxa_flush_buffer(struct tty_struct *);
static int moxa_chars_in_buffer(struct tty_struct *);
static void moxa_flush_chars(struct tty_struct *);
static void moxa_put_char(struct tty_struct *, unsigned char);
static int moxa_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);
static void moxa_throttle(struct tty_struct *);
static void moxa_unthrottle(struct tty_struct *);
A
Alan Cox 已提交
191
static void moxa_set_termios(struct tty_struct *, struct ktermios *);
L
Linus Torvalds 已提交
192 193 194 195 196 197 198
static void moxa_stop(struct tty_struct *);
static void moxa_start(struct tty_struct *);
static void moxa_hangup(struct tty_struct *);
static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
			 unsigned int set, unsigned int clear);
static void moxa_poll(unsigned long);
A
Alan Cox 已提交
199
static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
J
Jiri Slaby 已提交
200
static int moxa_block_till_ready(struct tty_struct *, struct file *,
201
			    struct moxa_port *);
J
Jiri Slaby 已提交
202 203 204 205
static void moxa_setup_empty_event(struct tty_struct *);
static void moxa_check_xmit_empty(unsigned long);
static void moxa_shut_down(struct moxa_port *);
static void moxa_receive_data(struct moxa_port *);
L
Linus Torvalds 已提交
206 207 208 209 210 211 212 213 214 215 216 217
/*
 * moxa board interface functions:
 */
static void MoxaDriverInit(void);
static int MoxaDriverIoctl(unsigned int, unsigned long, int);
static int MoxaDriverPoll(void);
static int MoxaPortsOfCard(int);
static int MoxaPortIsValid(int);
static void MoxaPortEnable(int);
static void MoxaPortDisable(int);
static long MoxaPortGetMaxBaud(int);
static long MoxaPortSetBaud(int, long);
A
Alan Cox 已提交
218
static int MoxaPortSetTermio(int, struct ktermios *, speed_t);
L
Linus Torvalds 已提交
219 220 221 222 223 224 225 226
static int MoxaPortGetLineOut(int, int *, int *);
static void MoxaPortLineCtrl(int, int, int);
static void MoxaPortFlowCtrl(int, int, int, int, int, int);
static int MoxaPortLineStatus(int);
static int MoxaPortDCDChange(int);
static int MoxaPortDCDON(int);
static void MoxaPortFlushData(int, int);
static int MoxaPortWriteData(int, unsigned char *, int);
A
Alan Cox 已提交
227
static int MoxaPortReadData(int, struct tty_struct *tty);
L
Linus Torvalds 已提交
228 229 230 231 232 233 234
static int MoxaPortTxQueue(int);
static int MoxaPortRxQueue(int);
static int MoxaPortTxFree(int);
static void MoxaPortTxDisable(int);
static void MoxaPortTxEnable(int);
static int MoxaPortResetBrkCnt(int);
static void MoxaPortSendBreak(int, int);
235 236
static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
L
Linus Torvalds 已提交
237 238
static void MoxaSetFifo(int port, int enable);

J
Jeff Dike 已提交
239
static const struct tty_operations moxa_ops = {
L
Linus Torvalds 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
	.open = moxa_open,
	.close = moxa_close,
	.write = moxa_write,
	.write_room = moxa_write_room,
	.flush_buffer = moxa_flush_buffer,
	.chars_in_buffer = moxa_chars_in_buffer,
	.flush_chars = moxa_flush_chars,
	.put_char = moxa_put_char,
	.ioctl = moxa_ioctl,
	.throttle = moxa_throttle,
	.unthrottle = moxa_unthrottle,
	.set_termios = moxa_set_termios,
	.stop = moxa_stop,
	.start = moxa_start,
	.hangup = moxa_hangup,
	.tiocmget = moxa_tiocmget,
	.tiocmset = moxa_tiocmset,
};

J
Jiri Slaby 已提交
259
static struct tty_driver *moxaDriver;
260
static struct moxa_port moxa_ports[MAX_PORTS];
J
Jiri Slaby 已提交
261
static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
I
Ingo Molnar 已提交
262
static DEFINE_SPINLOCK(moxa_lock);
A
Alan Cox 已提交
263

L
Linus Torvalds 已提交
264
#ifdef CONFIG_PCI
265 266
static int __devinit moxa_pci_probe(struct pci_dev *pdev,
		const struct pci_device_id *ent)
L
Linus Torvalds 已提交
267
{
268 269 270 271 272 273
	struct moxa_board_conf *board;
	unsigned int i;
	int board_type = ent->driver_data;
	int retval;

	retval = pci_enable_device(pdev);
J
Jiri Slaby 已提交
274 275
	if (retval) {
		dev_err(&pdev->dev, "can't enable pci device\n");
276
		goto err;
J
Jiri Slaby 已提交
277
	}
278 279 280 281 282 283 284

	for (i = 0; i < MAX_BOARDS; i++)
		if (moxa_boards[i].basemem == NULL)
			break;

	retval = -ENODEV;
	if (i >= MAX_BOARDS) {
J
Jiri Slaby 已提交
285
		dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
286 287 288 289 290 291
				"found. Board is ignored.\n", MAX_BOARDS);
		goto err;
	}

	board = &moxa_boards[i];
	board->basemem = pci_iomap(pdev, 2, 0x4000);
J
Jiri Slaby 已提交
292 293
	if (board->basemem == NULL) {
		dev_err(&pdev->dev, "can't remap io space 2\n");
294
		goto err;
J
Jiri Slaby 已提交
295
	}
296

L
Linus Torvalds 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
	board->boardType = board_type;
	switch (board_type) {
	case MOXA_BOARD_C218_ISA:
	case MOXA_BOARD_C218_PCI:
		board->numPorts = 8;
		break;

	case MOXA_BOARD_CP204J:
		board->numPorts = 4;
		break;
	default:
		board->numPorts = 0;
		break;
	}
	board->busType = MOXA_BUS_TYPE_PCI;
J
Jiri Slaby 已提交
312

313
	pci_set_drvdata(pdev, board);
L
Linus Torvalds 已提交
314 315

	return (0);
316 317 318 319 320 321 322 323 324 325
err:
	return retval;
}

static void __devexit moxa_pci_remove(struct pci_dev *pdev)
{
	struct moxa_board_conf *brd = pci_get_drvdata(pdev);

	pci_iounmap(pdev, brd->basemem);
	brd->basemem = NULL;
L
Linus Torvalds 已提交
326
}
J
Jiri Slaby 已提交
327 328 329 330 331 332 333

static struct pci_driver moxa_pci_driver = {
	.name = "moxa",
	.id_table = moxa_pcibrds,
	.probe = moxa_pci_probe,
	.remove = __devexit_p(moxa_pci_remove)
};
L
Linus Torvalds 已提交
334 335 336 337
#endif /* CONFIG_PCI */

static int __init moxa_init(void)
{
338
	int i, numBoards = 0, retval = 0;
339
	struct moxa_port *ch;
L
Linus Torvalds 已提交
340

J
Jiri Slaby 已提交
341 342
	printk(KERN_INFO "MOXA Intellio family driver version %s\n",
			MOXA_VERSION);
L
Linus Torvalds 已提交
343 344 345 346 347
	moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
	if (!moxaDriver)
		return -ENOMEM;

	moxaDriver->owner = THIS_MODULE;
348
	moxaDriver->name = "ttyMX";
L
Linus Torvalds 已提交
349 350 351 352 353 354
	moxaDriver->major = ttymajor;
	moxaDriver->minor_start = 0;
	moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
	moxaDriver->subtype = SERIAL_TYPE_NORMAL;
	moxaDriver->init_termios = tty_std_termios;
	moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
A
Alan Cox 已提交
355 356
	moxaDriver->init_termios.c_ispeed = 9600;
	moxaDriver->init_termios.c_ospeed = 9600;
L
Linus Torvalds 已提交
357 358 359
	moxaDriver->flags = TTY_DRIVER_REAL_RAW;
	tty_set_operations(moxaDriver, &moxa_ops);

360
	for (i = 0, ch = moxa_ports; i < MAX_PORTS; i++, ch++) {
L
Linus Torvalds 已提交
361 362 363 364 365 366
		ch->type = PORT_16550A;
		ch->port = i;
		ch->close_delay = 5 * HZ / 10;
		ch->closing_wait = 30 * HZ;
		ch->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
		init_waitqueue_head(&ch->open_wait);
J
Jiri Slaby 已提交
367
		init_completion(&ch->close_wait);
368

J
Jiri Slaby 已提交
369
		setup_timer(&ch->emptyTimer, moxa_check_xmit_empty,
370
				(unsigned long)ch);
L
Linus Torvalds 已提交
371 372
	}

J
Jiri Slaby 已提交
373
	pr_debug("Moxa tty devices major number = %d\n", ttymajor);
L
Linus Torvalds 已提交
374 375 376 377 378 379 380

	if (tty_register_driver(moxaDriver)) {
		printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
		put_tty_driver(moxaDriver);
		return -1;
	}

J
Jiri Slaby 已提交
381
	mod_timer(&moxaTimer, jiffies + HZ / 50);
L
Linus Torvalds 已提交
382 383 384 385 386 387

	/* Find the boards defined form module args. */
#ifdef MODULE
	for (i = 0; i < MAX_BOARDS; i++) {
		if ((type[i] == MOXA_BOARD_C218_ISA) ||
		    (type[i] == MOXA_BOARD_C320_ISA)) {
J
Jiri Slaby 已提交
388 389 390
			pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
			       numBoards + 1, moxa_brdname[type[i] - 1],
			       (unsigned long)baseaddr[i]);
L
Linus Torvalds 已提交
391
			if (numBoards >= MAX_BOARDS) {
J
Jiri Slaby 已提交
392 393 394
				printk(KERN_WARNING "More than %d MOXA "
					"Intellio family boards found. Board "
					"is ignored.\n", MAX_BOARDS);
L
Linus Torvalds 已提交
395 396 397
				continue;
			}
			moxa_boards[numBoards].boardType = type[i];
398
			if (type[i] == MOXA_BOARD_C218_ISA)
L
Linus Torvalds 已提交
399 400 401 402 403 404 405 406 407
				moxa_boards[numBoards].numPorts = 8;
			else
				moxa_boards[numBoards].numPorts = numports[i];
			moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
			moxa_boards[numBoards].baseAddr = baseaddr[i];
			numBoards++;
		}
	}
#endif
J
Jiri Slaby 已提交
408

L
Linus Torvalds 已提交
409
#ifdef CONFIG_PCI
J
Jiri Slaby 已提交
410 411 412 413 414
	retval = pci_register_driver(&moxa_pci_driver);
	if (retval) {
		printk(KERN_ERR "Can't register moxa pci driver!\n");
		if (numBoards)
			retval = 0;
L
Linus Torvalds 已提交
415 416
	}
#endif
J
Jiri Slaby 已提交
417

L
Linus Torvalds 已提交
418
	for (i = 0; i < numBoards; i++) {
419 420
		moxa_boards[i].basemem = ioremap(moxa_boards[i].baseAddr,
				0x4000);
L
Linus Torvalds 已提交
421 422
	}

J
Jiri Slaby 已提交
423
	return retval;
L
Linus Torvalds 已提交
424 425 426 427 428 429
}

static void __exit moxa_exit(void)
{
	int i;

430
	del_timer_sync(&moxaTimer);
L
Linus Torvalds 已提交
431 432

	for (i = 0; i < MAX_PORTS; i++)
433
		del_timer_sync(&moxa_ports[i].emptyTimer);
L
Linus Torvalds 已提交
434 435

	if (tty_unregister_driver(moxaDriver))
J
Jiri Slaby 已提交
436 437
		printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
				"serial driver\n");
L
Linus Torvalds 已提交
438
	put_tty_driver(moxaDriver);
439

440
#ifdef CONFIG_PCI
J
Jiri Slaby 已提交
441
	pci_unregister_driver(&moxa_pci_driver);
442
#endif
J
Jiri Slaby 已提交
443 444

	for (i = 0; i < MAX_BOARDS; i++)
445 446
		if (moxa_boards[i].basemem)
			iounmap(moxa_boards[i].basemem);
L
Linus Torvalds 已提交
447 448 449 450 451 452 453
}

module_init(moxa_init);
module_exit(moxa_exit);

static int moxa_open(struct tty_struct *tty, struct file *filp)
{
454
	struct moxa_port *ch;
L
Linus Torvalds 已提交
455 456 457
	int port;
	int retval;

J
Jiri Slaby 已提交
458
	port = tty->index;
L
Linus Torvalds 已提交
459 460 461 462 463 464 465 466
	if (port == MAX_PORTS) {
		return (0);
	}
	if (!MoxaPortIsValid(port)) {
		tty->driver_data = NULL;
		return (-ENODEV);
	}

467
	ch = &moxa_ports[port];
L
Linus Torvalds 已提交
468 469 470 471 472
	ch->count++;
	tty->driver_data = ch;
	ch->tty = tty;
	if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
		ch->statusflags = 0;
A
Alan Cox 已提交
473
		moxa_set_tty_param(tty, tty->termios);
L
Linus Torvalds 已提交
474 475 476 477
		MoxaPortLineCtrl(ch->port, 1, 1);
		MoxaPortEnable(ch->port);
		ch->asyncflags |= ASYNC_INITIALIZED;
	}
J
Jiri Slaby 已提交
478
	retval = moxa_block_till_ready(tty, filp, ch);
L
Linus Torvalds 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492

	moxa_unthrottle(tty);

	if (ch->type == PORT_16550A) {
		MoxaSetFifo(ch->port, 1);
	} else {
		MoxaSetFifo(ch->port, 0);
	}

	return (retval);
}

static void moxa_close(struct tty_struct *tty, struct file *filp)
{
493
	struct moxa_port *ch;
L
Linus Torvalds 已提交
494 495
	int port;

J
Jiri Slaby 已提交
496
	port = tty->index;
L
Linus Torvalds 已提交
497 498 499 500
	if (port == MAX_PORTS) {
		return;
	}
	if (!MoxaPortIsValid(port)) {
J
Jiri Slaby 已提交
501
		pr_debug("Invalid portno in moxa_close\n");
L
Linus Torvalds 已提交
502 503 504 505 506 507 508 509 510
		tty->driver_data = NULL;
		return;
	}
	if (tty->driver_data == NULL) {
		return;
	}
	if (tty_hung_up_p(filp)) {
		return;
	}
511
	ch = (struct moxa_port *) tty->driver_data;
L
Linus Torvalds 已提交
512 513

	if ((tty->count == 1) && (ch->count != 1)) {
J
Jiri Slaby 已提交
514 515
		printk(KERN_WARNING "moxa_close: bad serial port count; "
			"tty->count is 1, ch->count is %d\n", ch->count);
L
Linus Torvalds 已提交
516 517 518
		ch->count = 1;
	}
	if (--ch->count < 0) {
J
Jiri Slaby 已提交
519 520
		printk(KERN_WARNING "moxa_close: bad serial port count, "
			"device=%s\n", tty->name);
L
Linus Torvalds 已提交
521 522 523 524 525 526 527 528 529
		ch->count = 0;
	}
	if (ch->count) {
		return;
	}
	ch->asyncflags |= ASYNC_CLOSING;

	ch->cflag = tty->termios->c_cflag;
	if (ch->asyncflags & ASYNC_INITIALIZED) {
J
Jiri Slaby 已提交
530
		moxa_setup_empty_event(tty);
L
Linus Torvalds 已提交
531
		tty_wait_until_sent(tty, 30 * HZ);	/* 30 seconds timeout */
532
		del_timer_sync(&moxa_ports[ch->port].emptyTimer);
L
Linus Torvalds 已提交
533
	}
J
Jiri Slaby 已提交
534
	moxa_shut_down(ch);
L
Linus Torvalds 已提交
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
	MoxaPortFlushData(port, 2);

	if (tty->driver->flush_buffer)
		tty->driver->flush_buffer(tty);
	tty_ldisc_flush(tty);
			
	tty->closing = 0;
	ch->event = 0;
	ch->tty = NULL;
	if (ch->blocked_open) {
		if (ch->close_delay) {
			msleep_interruptible(jiffies_to_msecs(ch->close_delay));
		}
		wake_up_interruptible(&ch->open_wait);
	}
	ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
J
Jiri Slaby 已提交
551
	complete_all(&ch->close_wait);
L
Linus Torvalds 已提交
552 553 554 555 556
}

static int moxa_write(struct tty_struct *tty,
		      const unsigned char *buf, int count)
{
557
	struct moxa_port *ch;
L
Linus Torvalds 已提交
558 559 560
	int len, port;
	unsigned long flags;

561
	ch = (struct moxa_port *) tty->driver_data;
L
Linus Torvalds 已提交
562 563 564
	if (ch == NULL)
		return (0);
	port = ch->port;
A
Alan Cox 已提交
565 566

	spin_lock_irqsave(&moxa_lock, flags);
L
Linus Torvalds 已提交
567
	len = MoxaPortWriteData(port, (unsigned char *) buf, count);
A
Alan Cox 已提交
568
	spin_unlock_irqrestore(&moxa_lock, flags);
L
Linus Torvalds 已提交
569 570 571 572 573 574 575 576 577 578 579

	/*********************************************
	if ( !(ch->statusflags & LOWWAIT) &&
	     ((len != count) || (MoxaPortTxFree(port) <= 100)) )
	************************************************/
	ch->statusflags |= LOWWAIT;
	return (len);
}

static int moxa_write_room(struct tty_struct *tty)
{
580
	struct moxa_port *ch;
L
Linus Torvalds 已提交
581 582 583

	if (tty->stopped)
		return (0);
584
	ch = (struct moxa_port *) tty->driver_data;
L
Linus Torvalds 已提交
585 586 587 588 589 590 591
	if (ch == NULL)
		return (0);
	return (MoxaPortTxFree(ch->port));
}

static void moxa_flush_buffer(struct tty_struct *tty)
{
592
	struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
L
Linus Torvalds 已提交
593 594 595 596 597 598 599 600 601 602

	if (ch == NULL)
		return;
	MoxaPortFlushData(ch->port, 1);
	tty_wakeup(tty);
}

static int moxa_chars_in_buffer(struct tty_struct *tty)
{
	int chars;
603
	struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
L
Linus Torvalds 已提交
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619

	/*
	 * Sigh...I have to check if driver_data is NULL here, because
	 * if an open() fails, the TTY subsystem eventually calls
	 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
	 * routine.  And since the open() failed, we return 0 here.  TDJ
	 */
	if (ch == NULL)
		return (0);
	chars = MoxaPortTxQueue(ch->port);
	if (chars) {
		/*
		 * Make it possible to wakeup anything waiting for output
		 * in tty_ioctl.c, etc.
		 */
		if (!(ch->statusflags & EMPTYWAIT))
J
Jiri Slaby 已提交
620
			moxa_setup_empty_event(tty);
L
Linus Torvalds 已提交
621 622 623 624 625 626 627 628 629 630 631 632 633 634
	}
	return (chars);
}

static void moxa_flush_chars(struct tty_struct *tty)
{
	/*
	 * Don't think I need this, because this is called to empty the TX
	 * buffer for the 16450, 16550, etc.
	 */
}

static void moxa_put_char(struct tty_struct *tty, unsigned char c)
{
635
	struct moxa_port *ch;
L
Linus Torvalds 已提交
636 637 638
	int port;
	unsigned long flags;

639
	ch = (struct moxa_port *) tty->driver_data;
L
Linus Torvalds 已提交
640 641 642
	if (ch == NULL)
		return;
	port = ch->port;
A
Alan Cox 已提交
643
	spin_lock_irqsave(&moxa_lock, flags);
644
	MoxaPortWriteData(port, &c, 1);
A
Alan Cox 已提交
645
	spin_unlock_irqrestore(&moxa_lock, flags);
L
Linus Torvalds 已提交
646 647 648 649 650 651 652 653
	/************************************************
	if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
	*************************************************/
	ch->statusflags |= LOWWAIT;
}

static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
{
654
	struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
L
Linus Torvalds 已提交
655 656 657
	int port;
	int flag = 0, dtr, rts;

J
Jiri Slaby 已提交
658
	port = tty->index;
L
Linus Torvalds 已提交
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
	if ((port != MAX_PORTS) && (!ch))
		return (-EINVAL);

	MoxaPortGetLineOut(ch->port, &dtr, &rts);
	if (dtr)
		flag |= TIOCM_DTR;
	if (rts)
		flag |= TIOCM_RTS;
	dtr = MoxaPortLineStatus(ch->port);
	if (dtr & 1)
		flag |= TIOCM_CTS;
	if (dtr & 2)
		flag |= TIOCM_DSR;
	if (dtr & 4)
		flag |= TIOCM_CD;
	return flag;
}

static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
			 unsigned int set, unsigned int clear)
{
680
	struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
L
Linus Torvalds 已提交
681 682 683
	int port;
	int dtr, rts;

J
Jiri Slaby 已提交
684
	port = tty->index;
L
Linus Torvalds 已提交
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
	if ((port != MAX_PORTS) && (!ch))
		return (-EINVAL);

	MoxaPortGetLineOut(ch->port, &dtr, &rts);
	if (set & TIOCM_RTS)
		rts = 1;
	if (set & TIOCM_DTR)
		dtr = 1;
	if (clear & TIOCM_RTS)
		rts = 0;
	if (clear & TIOCM_DTR)
		dtr = 0;
	MoxaPortLineCtrl(ch->port, dtr, rts);
	return 0;
}

static int moxa_ioctl(struct tty_struct *tty, struct file *file,
		      unsigned int cmd, unsigned long arg)
{
704
	struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
L
Linus Torvalds 已提交
705 706 707 708
	register int port;
	void __user *argp = (void __user *)arg;
	int retval;

J
Jiri Slaby 已提交
709
	port = tty->index;
L
Linus Torvalds 已提交
710 711 712 713 714 715 716 717
	if ((port != MAX_PORTS) && (!ch))
		return (-EINVAL);

	switch (cmd) {
	case TCSBRK:		/* SVID version: non-zero arg --> no break */
		retval = tty_check_change(tty);
		if (retval)
			return (retval);
J
Jiri Slaby 已提交
718
		moxa_setup_empty_event(tty);
L
Linus Torvalds 已提交
719 720 721 722 723 724 725 726
		tty_wait_until_sent(tty, 0);
		if (!arg)
			MoxaPortSendBreak(ch->port, 0);
		return (0);
	case TCSBRKP:		/* support for POSIX tcsendbreak() */
		retval = tty_check_change(tty);
		if (retval)
			return (retval);
J
Jiri Slaby 已提交
727
		moxa_setup_empty_event(tty);
L
Linus Torvalds 已提交
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
		tty_wait_until_sent(tty, 0);
		MoxaPortSendBreak(ch->port, arg);
		return (0);
	case TIOCGSOFTCAR:
		return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) argp);
	case TIOCSSOFTCAR:
		if(get_user(retval, (unsigned long __user *) argp))
			return -EFAULT;
		arg = retval;
		tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
					 (arg ? CLOCAL : 0));
		if (C_CLOCAL(tty))
			ch->asyncflags &= ~ASYNC_CHECK_CD;
		else
			ch->asyncflags |= ASYNC_CHECK_CD;
		return (0);
	case TIOCGSERIAL:
		return moxa_get_serial_info(ch, argp);

	case TIOCSSERIAL:
		return moxa_set_serial_info(ch, argp);
	default:
		retval = MoxaDriverIoctl(cmd, arg, port);
	}
	return (retval);
}

static void moxa_throttle(struct tty_struct *tty)
{
757
	struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
L
Linus Torvalds 已提交
758 759 760 761 762 763

	ch->statusflags |= THROTTLE;
}

static void moxa_unthrottle(struct tty_struct *tty)
{
764
	struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
L
Linus Torvalds 已提交
765 766 767 768 769

	ch->statusflags &= ~THROTTLE;
}

static void moxa_set_termios(struct tty_struct *tty,
A
Alan Cox 已提交
770
			     struct ktermios *old_termios)
L
Linus Torvalds 已提交
771
{
772
	struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
L
Linus Torvalds 已提交
773 774 775

	if (ch == NULL)
		return;
A
Alan Cox 已提交
776
	moxa_set_tty_param(tty, old_termios);
L
Linus Torvalds 已提交
777 778 779 780 781 782 783
	if (!(old_termios->c_cflag & CLOCAL) &&
	    (tty->termios->c_cflag & CLOCAL))
		wake_up_interruptible(&ch->open_wait);
}

static void moxa_stop(struct tty_struct *tty)
{
784
	struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
L
Linus Torvalds 已提交
785 786 787 788 789 790 791 792 793 794

	if (ch == NULL)
		return;
	MoxaPortTxDisable(ch->port);
	ch->statusflags |= TXSTOPPED;
}


static void moxa_start(struct tty_struct *tty)
{
795
	struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
L
Linus Torvalds 已提交
796 797 798 799 800 801 802 803 804 805 806 807 808

	if (ch == NULL)
		return;

	if (!(ch->statusflags & TXSTOPPED))
		return;

	MoxaPortTxEnable(ch->port);
	ch->statusflags &= ~TXSTOPPED;
}

static void moxa_hangup(struct tty_struct *tty)
{
809
	struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
L
Linus Torvalds 已提交
810 811

	moxa_flush_buffer(tty);
J
Jiri Slaby 已提交
812
	moxa_shut_down(ch);
L
Linus Torvalds 已提交
813 814 815 816 817 818 819 820 821 822
	ch->event = 0;
	ch->count = 0;
	ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
	ch->tty = NULL;
	wake_up_interruptible(&ch->open_wait);
}

static void moxa_poll(unsigned long ignored)
{
	register int card;
823
	struct moxa_port *ch;
L
Linus Torvalds 已提交
824 825 826 827 828 829
	struct tty_struct *tp;
	int i, ports;

	del_timer(&moxaTimer);

	if (MoxaDriverPoll() < 0) {
J
Jiri Slaby 已提交
830
		mod_timer(&moxaTimer, jiffies + HZ / 50);
L
Linus Torvalds 已提交
831 832 833 834 835
		return;
	}
	for (card = 0; card < MAX_BOARDS; card++) {
		if ((ports = MoxaPortsOfCard(card)) <= 0)
			continue;
836
		ch = &moxa_ports[card * MAX_PORTS_PER_BOARD];
L
Linus Torvalds 已提交
837 838 839 840 841
		for (i = 0; i < ports; i++, ch++) {
			if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
				continue;
			if (!(ch->statusflags & THROTTLE) &&
			    (MoxaPortRxQueue(ch->port) > 0))
J
Jiri Slaby 已提交
842
				moxa_receive_data(ch);
L
Linus Torvalds 已提交
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
			if ((tp = ch->tty) == 0)
				continue;
			if (ch->statusflags & LOWWAIT) {
				if (MoxaPortTxQueue(ch->port) <= WAKEUP_CHARS) {
					if (!tp->stopped) {
						ch->statusflags &= ~LOWWAIT;
						tty_wakeup(tp);
					}
				}
			}
			if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch->port) > 0)) {
				tty_insert_flip_char(tp, 0, TTY_BREAK);
				tty_schedule_flip(tp);
			}
			if (MoxaPortDCDChange(ch->port)) {
				if (ch->asyncflags & ASYNC_CHECK_CD) {
					if (MoxaPortDCDON(ch->port))
						wake_up_interruptible(&ch->open_wait);
					else {
862 863 864
						tty_hangup(tp);
						wake_up_interruptible(&ch->open_wait);
						ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
L
Linus Torvalds 已提交
865 866 867 868 869 870
					}
				}
			}
		}
	}

J
Jiri Slaby 已提交
871
	mod_timer(&moxaTimer, jiffies + HZ / 50);
L
Linus Torvalds 已提交
872 873 874 875
}

/******************************************************************************/

A
Alan Cox 已提交
876
static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
L
Linus Torvalds 已提交
877
{
A
Alan Cox 已提交
878
	register struct ktermios *ts;
879
	struct moxa_port *ch;
A
Alan Cox 已提交
880
	int rts, cts, txflow, rxflow, xany, baud;
L
Linus Torvalds 已提交
881

882
	ch = (struct moxa_port *) tty->driver_data;
L
Linus Torvalds 已提交
883 884 885 886 887 888 889 890 891 892 893 894 895 896
	ts = tty->termios;
	if (ts->c_cflag & CLOCAL)
		ch->asyncflags &= ~ASYNC_CHECK_CD;
	else
		ch->asyncflags |= ASYNC_CHECK_CD;
	rts = cts = txflow = rxflow = xany = 0;
	if (ts->c_cflag & CRTSCTS)
		rts = cts = 1;
	if (ts->c_iflag & IXON)
		txflow = 1;
	if (ts->c_iflag & IXOFF)
		rxflow = 1;
	if (ts->c_iflag & IXANY)
		xany = 1;
A
Alan Cox 已提交
897 898 899

	/* Clear the features we don't support */
	ts->c_cflag &= ~CMSPAR;
L
Linus Torvalds 已提交
900
	MoxaPortFlowCtrl(ch->port, rts, cts, txflow, rxflow, xany);
A
Alan Cox 已提交
901 902 903 904 905
	baud = MoxaPortSetTermio(ch->port, ts, tty_get_baud_rate(tty));
	if (baud == -1)
		baud = tty_termios_baud_rate(old_termios);
	/* Not put the baud rate into the termios data */
	tty_encode_baud_rate(tty, baud, baud);
L
Linus Torvalds 已提交
906 907
}

J
Jiri Slaby 已提交
908
static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
909
			    struct moxa_port *ch)
L
Linus Torvalds 已提交
910 911 912 913 914 915 916 917 918 919 920 921
{
	DECLARE_WAITQUEUE(wait,current);
	unsigned long flags;
	int retval;
	int do_clocal = C_CLOCAL(tty);

	/*
	 * If the device is in the middle of being closed, then block
	 * until it's done, and then try again.
	 */
	if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
		if (ch->asyncflags & ASYNC_CLOSING)
J
Jiri Slaby 已提交
922
			wait_for_completion_interruptible(&ch->close_wait);
L
Linus Torvalds 已提交
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
#ifdef SERIAL_DO_RESTART
		if (ch->asyncflags & ASYNC_HUP_NOTIFY)
			return (-EAGAIN);
		else
			return (-ERESTARTSYS);
#else
		return (-EAGAIN);
#endif
	}
	/*
	 * If non-blocking mode is set, then make the check up front
	 * and then exit.
	 */
	if (filp->f_flags & O_NONBLOCK) {
		ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
		return (0);
	}
	/*
	 * Block waiting for the carrier detect and the line to become free
	 */
	retval = 0;
	add_wait_queue(&ch->open_wait, &wait);
J
Jiri Slaby 已提交
945 946
	pr_debug("block_til_ready before block: ttys%d, count = %d\n",
		ch->port, ch->count);
A
Alan Cox 已提交
947
	spin_lock_irqsave(&moxa_lock, flags);
L
Linus Torvalds 已提交
948 949 950
	if (!tty_hung_up_p(filp))
		ch->count--;
	ch->blocked_open++;
A
Alan Cox 已提交
951 952
	spin_unlock_irqrestore(&moxa_lock, flags);

L
Linus Torvalds 已提交
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
	while (1) {
		set_current_state(TASK_INTERRUPTIBLE);
		if (tty_hung_up_p(filp) ||
		    !(ch->asyncflags & ASYNC_INITIALIZED)) {
#ifdef SERIAL_DO_RESTART
			if (ch->asyncflags & ASYNC_HUP_NOTIFY)
				retval = -EAGAIN;
			else
				retval = -ERESTARTSYS;
#else
			retval = -EAGAIN;
#endif
			break;
		}
		if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
						MoxaPortDCDON(ch->port)))
			break;

		if (signal_pending(current)) {
			retval = -ERESTARTSYS;
			break;
		}
		schedule();
	}
	set_current_state(TASK_RUNNING);
	remove_wait_queue(&ch->open_wait, &wait);
A
Alan Cox 已提交
979 980

	spin_lock_irqsave(&moxa_lock, flags);
L
Linus Torvalds 已提交
981 982 983
	if (!tty_hung_up_p(filp))
		ch->count++;
	ch->blocked_open--;
A
Alan Cox 已提交
984
	spin_unlock_irqrestore(&moxa_lock, flags);
J
Jiri Slaby 已提交
985 986
	pr_debug("block_til_ready after blocking: ttys%d, count = %d\n",
		ch->port, ch->count);
L
Linus Torvalds 已提交
987 988
	if (retval)
		return (retval);
A
Alan Cox 已提交
989
	/* FIXME: review to see if we need to use set_bit on these */
L
Linus Torvalds 已提交
990
	ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
A
Alan Cox 已提交
991
	return 0;
L
Linus Torvalds 已提交
992 993
}

J
Jiri Slaby 已提交
994
static void moxa_setup_empty_event(struct tty_struct *tty)
L
Linus Torvalds 已提交
995
{
996
	struct moxa_port *ch = tty->driver_data;
L
Linus Torvalds 已提交
997 998
	unsigned long flags;

A
Alan Cox 已提交
999
	spin_lock_irqsave(&moxa_lock, flags);
L
Linus Torvalds 已提交
1000
	ch->statusflags |= EMPTYWAIT;
1001
	mod_timer(&moxa_ports[ch->port].emptyTimer, jiffies + HZ);
A
Alan Cox 已提交
1002
	spin_unlock_irqrestore(&moxa_lock, flags);
L
Linus Torvalds 已提交
1003 1004
}

J
Jiri Slaby 已提交
1005
static void moxa_check_xmit_empty(unsigned long data)
L
Linus Torvalds 已提交
1006
{
1007
	struct moxa_port *ch;
L
Linus Torvalds 已提交
1008

1009
	ch = (struct moxa_port *) data;
L
Linus Torvalds 已提交
1010 1011 1012 1013 1014 1015
	if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
		if (MoxaPortTxQueue(ch->port) == 0) {
			ch->statusflags &= ~EMPTYWAIT;
			tty_wakeup(ch->tty);
			return;
		}
1016 1017
		mod_timer(&moxa_ports[ch->port].emptyTimer,
				round_jiffies(jiffies + HZ));
L
Linus Torvalds 已提交
1018 1019 1020 1021
	} else
		ch->statusflags &= ~EMPTYWAIT;
}

J
Jiri Slaby 已提交
1022
static void moxa_shut_down(struct moxa_port *ch)
L
Linus Torvalds 已提交
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
{
	struct tty_struct *tp;

	if (!(ch->asyncflags & ASYNC_INITIALIZED))
		return;

	tp = ch->tty;

	MoxaPortDisable(ch->port);

	/*
	 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
	 */
	if (tp->termios->c_cflag & HUPCL)
		MoxaPortLineCtrl(ch->port, 0, 0);

	ch->asyncflags &= ~ASYNC_INITIALIZED;
}

J
Jiri Slaby 已提交
1042
static void moxa_receive_data(struct moxa_port *ch)
L
Linus Torvalds 已提交
1043 1044
{
	struct tty_struct *tp;
A
Alan Cox 已提交
1045
	struct ktermios *ts;
L
Linus Torvalds 已提交
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
	unsigned long flags;

	ts = NULL;
	tp = ch->tty;
	if (tp)
		ts = tp->termios;
	/**************************************************
	if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
	*****************************************************/
	if (!tp || !ts) {
		MoxaPortFlushData(ch->port, 0);
		return;
	}
A
Alan Cox 已提交
1059 1060 1061 1062
	spin_lock_irqsave(&moxa_lock, flags);
	MoxaPortReadData(ch->port, tp);
	spin_unlock_irqrestore(&moxa_lock, flags);
	tty_schedule_flip(tp);
L
Linus Torvalds 已提交
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
}

#define Magic_code	0x404

/*
 *    System Configuration
 */
/*
 *    for C218 BIOS initialization
 */
#define C218_ConfBase	0x800
#define C218_status	(C218_ConfBase + 0)	/* BIOS running status    */
#define C218_diag	(C218_ConfBase + 2)	/* diagnostic status      */
#define C218_key	(C218_ConfBase + 4)	/* WORD (0x218 for C218) */
#define C218DLoad_len	(C218_ConfBase + 6)	/* WORD           */
#define C218check_sum	(C218_ConfBase + 8)	/* BYTE           */
#define C218chksum_ok	(C218_ConfBase + 0x0a)	/* BYTE (1:ok)            */
#define C218_TestRx	(C218_ConfBase + 0x10)	/* 8 bytes for 8 ports    */
#define C218_TestTx	(C218_ConfBase + 0x18)	/* 8 bytes for 8 ports    */
#define C218_RXerr	(C218_ConfBase + 0x20)	/* 8 bytes for 8 ports    */
#define C218_ErrFlag	(C218_ConfBase + 0x28)	/* 8 bytes for 8 ports    */

#define C218_LoadBuf	0x0F00
#define C218_KeyCode	0x218
#define CP204J_KeyCode	0x204

/*
 *    for C320 BIOS initialization
 */
#define C320_ConfBase	0x800
#define C320_LoadBuf	0x0f00
#define STS_init	0x05	/* for C320_status        */

#define C320_status	C320_ConfBase + 0	/* BIOS running status    */
#define C320_diag	C320_ConfBase + 2	/* diagnostic status      */
#define C320_key	C320_ConfBase + 4	/* WORD (0320H for C320) */
#define C320DLoad_len	C320_ConfBase + 6	/* WORD           */
#define C320check_sum	C320_ConfBase + 8	/* WORD           */
#define C320chksum_ok	C320_ConfBase + 0x0a	/* WORD (1:ok)            */
#define C320bapi_len	C320_ConfBase + 0x0c	/* WORD           */
#define C320UART_no	C320_ConfBase + 0x0e	/* WORD           */

#define C320_KeyCode	0x320

#define FixPage_addr	0x0000	/* starting addr of static page  */
#define DynPage_addr	0x2000	/* starting addr of dynamic page */
#define C218_start	0x3000	/* starting addr of C218 BIOS prg */
#define Control_reg	0x1ff0	/* select page and reset control */
#define HW_reset	0x80

/*
 *    Function Codes
 */
#define FC_CardReset	0x80
#define FC_ChannelReset 1	/* C320 firmware not supported */
#define FC_EnableCH	2
#define FC_DisableCH	3
#define FC_SetParam	4
#define FC_SetMode	5
#define FC_SetRate	6
#define FC_LineControl	7
#define FC_LineStatus	8
#define FC_XmitControl	9
#define FC_FlushQueue	10
#define FC_SendBreak	11
#define FC_StopBreak	12
#define FC_LoopbackON	13
#define FC_LoopbackOFF	14
#define FC_ClrIrqTable	15
#define FC_SendXon	16
#define FC_SetTermIrq	17	/* C320 firmware not supported */
#define FC_SetCntIrq	18	/* C320 firmware not supported */
#define FC_SetBreakIrq	19
#define FC_SetLineIrq	20
#define FC_SetFlowCtl	21
#define FC_GenIrq	22
#define FC_InCD180	23
#define FC_OutCD180	24
#define FC_InUARTreg	23
#define FC_OutUARTreg	24
#define FC_SetXonXoff	25
#define FC_OutCD180CCR	26
#define FC_ExtIQueue	27
#define FC_ExtOQueue	28
#define FC_ClrLineIrq	29
#define FC_HWFlowCtl	30
#define FC_GetClockRate 35
#define FC_SetBaud	36
#define FC_SetDataMode  41
#define FC_GetCCSR      43
#define FC_GetDataError 45
#define FC_RxControl	50
#define FC_ImmSend	51
#define FC_SetXonState	52
#define FC_SetXoffState	53
#define FC_SetRxFIFOTrig 54
#define FC_SetTxFIFOCnt 55
#define FC_UnixRate	56
#define FC_UnixResetTimer 57

#define	RxFIFOTrig1	0
#define	RxFIFOTrig4	1
#define	RxFIFOTrig8	2
#define	RxFIFOTrig14	3

/*
 *    Dual-Ported RAM
 */
#define DRAM_global	0
#define INT_data	(DRAM_global + 0)
#define Config_base	(DRAM_global + 0x108)

#define IRQindex	(INT_data + 0)
#define IRQpending	(INT_data + 4)
#define IRQtable	(INT_data + 8)

/*
 *    Interrupt Status
 */
#define IntrRx		0x01	/* receiver data O.K.             */
#define IntrTx		0x02	/* transmit buffer empty  */
#define IntrFunc	0x04	/* function complete              */
#define IntrBreak	0x08	/* received break         */
#define IntrLine	0x10	/* line status change
				   for transmitter                */
#define IntrIntr	0x20	/* received INTR code             */
#define IntrQuit	0x40	/* received QUIT code             */
#define IntrEOF 	0x80	/* received EOF code              */

#define IntrRxTrigger 	0x100	/* rx data count reach tigger value */
#define IntrTxTrigger 	0x200	/* tx data count below trigger value */

#define Magic_no	(Config_base + 0)
#define Card_model_no	(Config_base + 2)
#define Total_ports	(Config_base + 4)
#define Module_cnt	(Config_base + 8)
#define Module_no	(Config_base + 10)
#define Timer_10ms	(Config_base + 14)
#define Disable_IRQ	(Config_base + 20)
#define TMS320_PORT1	(Config_base + 22)
#define TMS320_PORT2	(Config_base + 24)
#define TMS320_CLOCK	(Config_base + 26)

/*
 *    DATA BUFFER in DRAM
 */
#define Extern_table	0x400	/* Base address of the external table
				   (24 words *    64) total 3K bytes
				   (24 words * 128) total 6K bytes */
#define Extern_size	0x60	/* 96 bytes                       */
#define RXrptr		0x00	/* read pointer for RX buffer     */
#define RXwptr		0x02	/* write pointer for RX buffer    */
#define TXrptr		0x04	/* read pointer for TX buffer     */
#define TXwptr		0x06	/* write pointer for TX buffer    */
#define HostStat	0x08	/* IRQ flag and general flag      */
#define FlagStat	0x0A
#define FlowControl	0x0C	/* B7 B6 B5 B4 B3 B2 B1 B0              */
					/*  x  x  x  x  |  |  |  |            */
					/*              |  |  |  + CTS flow   */
					/*              |  |  +--- RTS flow   */
					/*              |  +------ TX Xon/Xoff */
					/*              +--------- RX Xon/Xoff */
#define Break_cnt	0x0E	/* received break count   */
#define CD180TXirq	0x10	/* if non-0: enable TX irq        */
#define RX_mask 	0x12
#define TX_mask 	0x14
#define Ofs_rxb 	0x16
#define Ofs_txb 	0x18
#define Page_rxb	0x1A
#define Page_txb	0x1C
#define EndPage_rxb	0x1E
#define EndPage_txb	0x20
#define Data_error	0x22
#define RxTrigger	0x28
#define TxTrigger	0x2a

#define rRXwptr 	0x34
#define Low_water	0x36

#define FuncCode	0x40
#define FuncArg 	0x42
#define FuncArg1	0x44

#define C218rx_size	0x2000	/* 8K bytes */
#define C218tx_size	0x8000	/* 32K bytes */

#define C218rx_mask	(C218rx_size - 1)
#define C218tx_mask	(C218tx_size - 1)

#define C320p8rx_size	0x2000
#define C320p8tx_size	0x8000
#define C320p8rx_mask	(C320p8rx_size - 1)
#define C320p8tx_mask	(C320p8tx_size - 1)

#define C320p16rx_size	0x2000
#define C320p16tx_size	0x4000
#define C320p16rx_mask	(C320p16rx_size - 1)
#define C320p16tx_mask	(C320p16tx_size - 1)

#define C320p24rx_size	0x2000
#define C320p24tx_size	0x2000
#define C320p24rx_mask	(C320p24rx_size - 1)
#define C320p24tx_mask	(C320p24tx_size - 1)

#define C320p32rx_size	0x1000
#define C320p32tx_size	0x1000
#define C320p32rx_mask	(C320p32rx_size - 1)
#define C320p32tx_mask	(C320p32tx_size - 1)

#define Page_size	0x2000
#define Page_mask	(Page_size - 1)
#define C218rx_spage	3
#define C218tx_spage	4
#define C218rx_pageno	1
#define C218tx_pageno	4
#define C218buf_pageno	5

#define C320p8rx_spage	3
#define C320p8tx_spage	4
#define C320p8rx_pgno	1
#define C320p8tx_pgno	4
#define C320p8buf_pgno	5

#define C320p16rx_spage 3
#define C320p16tx_spage 4
#define C320p16rx_pgno	1
#define C320p16tx_pgno	2
#define C320p16buf_pgno 3

#define C320p24rx_spage 3
#define C320p24tx_spage 4
#define C320p24rx_pgno	1
#define C320p24tx_pgno	1
#define C320p24buf_pgno 2

#define C320p32rx_spage 3
#define C320p32tx_ofs	C320p32rx_size
#define C320p32tx_spage 3
#define C320p32buf_pgno 1

/*
 *    Host Status
 */
#define WakeupRx	0x01
#define WakeupTx	0x02
#define WakeupBreak	0x08
#define WakeupLine	0x10
#define WakeupIntr	0x20
#define WakeupQuit	0x40
#define WakeupEOF	0x80	/* used in VTIME control */
#define WakeupRxTrigger	0x100
#define WakeupTxTrigger	0x200
/*
 *    Flag status
 */
#define Rx_over		0x01
#define Xoff_state	0x02
#define Tx_flowOff	0x04
#define Tx_enable	0x08
#define CTS_state	0x10
#define DSR_state	0x20
#define DCD_state	0x80
/*
 *    FlowControl
 */
#define CTS_FlowCtl	1
#define RTS_FlowCtl	2
#define Tx_FlowCtl	4
#define Rx_FlowCtl	8
#define IXM_IXANY	0x10

#define LowWater	128

#define DTR_ON		1
#define RTS_ON		2
#define CTS_ON		1
#define DSR_ON		2
#define DCD_ON		8

/* mode definition */
#define	MX_CS8		0x03
#define	MX_CS7		0x02
#define	MX_CS6		0x01
#define	MX_CS5		0x00

#define	MX_STOP1	0x00
#define	MX_STOP15	0x04
#define	MX_STOP2	0x08

#define	MX_PARNONE	0x00
#define	MX_PAREVEN	0x40
#define	MX_PARODD	0xC0

/*
 *    Query
 */

struct mon_str {
	int tick;
	int rxcnt[MAX_PORTS];
	int txcnt[MAX_PORTS];
};

#define 	DCD_changed	0x01
#define 	DCD_oldstate	0x80

static unsigned char moxaBuff[10240];
static int moxaLowWaterChk;
static int moxaCard;
1372
static struct mon_str moxaLog;
1373
static int moxaFuncTout = HZ / 2;
L
Linus Torvalds 已提交
1374 1375

static void moxafunc(void __iomem *, int, ushort);
J
Jiri Slaby 已提交
1376 1377
static void moxa_wait_finish(void __iomem *);
static void moxa_low_water_check(void __iomem *);
L
Linus Torvalds 已提交
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
static int moxaloadbios(int, unsigned char __user *, int);
static int moxafindcard(int);
static int moxaload320b(int, unsigned char __user *, int);
static int moxaloadcode(int, unsigned char __user *, int);
static int moxaloadc218(int, void __iomem *, int);
static int moxaloadc320(int, void __iomem *, int, int *);

/*****************************************************************************
 *	Driver level functions: 					     *
 *	1. MoxaDriverInit(void);					     *
 *	2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);   *
 *	3. MoxaDriverPoll(void);					     *
 *****************************************************************************/
void MoxaDriverInit(void)
{
1393 1394
	struct moxa_port *p;
	unsigned int i;
L
Linus Torvalds 已提交
1395 1396 1397 1398 1399 1400

	moxaFuncTout = HZ / 2;	/* 500 mini-seconds */
	moxaCard = 0;
	moxaLog.tick = 0;
	moxaLowWaterChk = 0;
	for (i = 0; i < MAX_PORTS; i++) {
1401 1402 1403 1404
		p = &moxa_ports[i];
		p->chkPort = 0;
		p->lowChkFlag = 0;
		p->lineCtrl = 0;
L
Linus Torvalds 已提交
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
		moxaLog.rxcnt[i] = 0;
		moxaLog.txcnt[i] = 0;
	}
}

#define	MOXA		0x400
#define MOXA_GET_IQUEUE 	(MOXA + 1)	/* get input buffered count */
#define MOXA_GET_OQUEUE 	(MOXA + 2)	/* get output buffered count */
#define MOXA_INIT_DRIVER	(MOXA + 6)	/* moxaCard=0 */
#define MOXA_LOAD_BIOS		(MOXA + 9)	/* download BIOS */
#define MOXA_FIND_BOARD		(MOXA + 10)	/* Check if MOXA card exist? */
#define MOXA_LOAD_C320B		(MOXA + 11)	/* download 320B firmware */
#define MOXA_LOAD_CODE		(MOXA + 12)	/* download firmware */
#define MOXA_GETDATACOUNT       (MOXA + 23)
#define MOXA_GET_IOQUEUE	(MOXA + 27)
#define MOXA_FLUSH_QUEUE	(MOXA + 28)
#define MOXA_GET_CONF		(MOXA + 35)	/* configuration */
#define MOXA_GET_MAJOR          (MOXA + 63)
#define MOXA_GET_CUMAJOR        (MOXA + 64)
#define MOXA_GETMSTATUS         (MOXA + 65)

struct dl_str {
	char __user *buf;
	int len;
	int cardno;
};

static struct dl_str dltmp;

void MoxaPortFlushData(int port, int mode)
{
	void __iomem *ofsAddr;
	if ((mode < 0) || (mode > 2))
		return;
1439
	ofsAddr = moxa_ports[port].tableAddr;
L
Linus Torvalds 已提交
1440 1441
	moxafunc(ofsAddr, FC_FlushQueue, mode);
	if (mode != 1) {
1442
		moxa_ports[port].lowChkFlag = 0;
J
Jiri Slaby 已提交
1443
		moxa_low_water_check(ofsAddr);
L
Linus Torvalds 已提交
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
	}
}

int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port)
{
	int i;
	int status;
	int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
	void __user *argp = (void __user *)arg;

J
Jiri Slaby 已提交
1454
	if (port == MAX_PORTS) {
L
Linus Torvalds 已提交
1455 1456 1457 1458 1459 1460 1461 1462 1463
		if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_INIT_DRIVER) &&
		    (cmd != MOXA_LOAD_BIOS) && (cmd != MOXA_FIND_BOARD) && (cmd != MOXA_LOAD_C320B) &&
		 (cmd != MOXA_LOAD_CODE) && (cmd != MOXA_GETDATACOUNT) &&
		  (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
		    (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
			return (-EINVAL);
	}
	switch (cmd) {
	case MOXA_GET_CONF:
1464 1465
		if(copy_to_user(argp, &moxa_boards, MAX_BOARDS *
				sizeof(struct moxa_board_conf)))
L
Linus Torvalds 已提交
1466 1467 1468 1469 1470 1471 1472 1473
			return -EFAULT;
		return (0);
	case MOXA_INIT_DRIVER:
		if ((int) arg == 0x404)
			MoxaDriverInit();
		return (0);
	case MOXA_GETDATACOUNT:
		moxaLog.tick = jiffies;
1474
		if(copy_to_user(argp, &moxaLog, sizeof(struct mon_str)))
L
Linus Torvalds 已提交
1475 1476 1477 1478 1479
			return -EFAULT;
		return (0);
	case MOXA_FLUSH_QUEUE:
		MoxaPortFlushData(port, arg);
		return (0);
1480 1481
	case MOXA_GET_IOQUEUE: {
		struct moxaq_str __user *argm = argp;
1482
		struct moxaq_str tmp;
1483 1484

		for (i = 0; i < MAX_PORTS; i++, argm++) {
1485 1486 1487 1488
			memset(&tmp, 0, sizeof(tmp));
			if (moxa_ports[i].chkPort) {
				tmp.inq = MoxaPortRxQueue(i);
				tmp.outq = MoxaPortTxQueue(i);
L
Linus Torvalds 已提交
1489
			}
1490
			if (copy_to_user(argm, &tmp, sizeof(tmp)))
1491
				return -EFAULT;
L
Linus Torvalds 已提交
1492 1493
		}
		return (0);
1494
	} case MOXA_GET_OQUEUE:
L
Linus Torvalds 已提交
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
		i = MoxaPortTxQueue(port);
		return put_user(i, (unsigned long __user *)argp);
	case MOXA_GET_IQUEUE:
		i = MoxaPortRxQueue(port);
		return put_user(i, (unsigned long __user *)argp);
	case MOXA_GET_MAJOR:
		if(copy_to_user(argp, &ttymajor, sizeof(int)))
			return -EFAULT;
		return 0;
	case MOXA_GET_CUMAJOR:
		i = 0;
		if(copy_to_user(argp, &i, sizeof(int)))
			return -EFAULT;
		return 0;
1509 1510
	case MOXA_GETMSTATUS: {
		struct mxser_mstatus __user *argm = argp;
1511
		struct mxser_mstatus tmp;
1512 1513 1514 1515
		struct moxa_port *p;

		for (i = 0; i < MAX_PORTS; i++, argm++) {
			p = &moxa_ports[i];
1516
			memset(&tmp, 0, sizeof(tmp));
1517 1518
			if (!p->chkPort) {
				goto copy;
L
Linus Torvalds 已提交
1519
			} else {
1520
				status = MoxaPortLineStatus(p->port);
L
Linus Torvalds 已提交
1521
				if (status & 1)
1522
					tmp.cts = 1;
L
Linus Torvalds 已提交
1523
				if (status & 2)
1524
					tmp.dsr = 1;
L
Linus Torvalds 已提交
1525
				if (status & 4)
1526
					tmp.dcd = 1;
L
Linus Torvalds 已提交
1527 1528
			}

1529
			if (!p->tty || !p->tty->termios)
1530
				tmp.cflag = p->cflag;
L
Linus Torvalds 已提交
1531
			else
1532
				tmp.cflag = p->tty->termios->c_cflag;
1533
copy:
1534
			if (copy_to_user(argm, &tmp, sizeof(tmp)))
1535
				return -EFAULT;
L
Linus Torvalds 已提交
1536 1537
		}
		return 0;
1538
	} default:
L
Linus Torvalds 已提交
1539 1540 1541 1542 1543
		return (-ENOIOCTLCMD);
	case MOXA_LOAD_BIOS:
	case MOXA_FIND_BOARD:
	case MOXA_LOAD_C320B:
	case MOXA_LOAD_CODE:
1544 1545
		if (!capable(CAP_SYS_RAWIO))
			return -EPERM;
L
Linus Torvalds 已提交
1546 1547 1548 1549 1550
		break;
	}

	if(copy_from_user(&dltmp, argp, sizeof(struct dl_str)))
		return -EFAULT;
1551
	if(dltmp.cardno < 0 || dltmp.cardno >= MAX_BOARDS || dltmp.len < 0)
L
Linus Torvalds 已提交
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
		return -EINVAL;

	switch(cmd)
	{
	case MOXA_LOAD_BIOS:
		i = moxaloadbios(dltmp.cardno, dltmp.buf, dltmp.len);
		return (i);
	case MOXA_FIND_BOARD:
		return moxafindcard(dltmp.cardno);
	case MOXA_LOAD_C320B:
		moxaload320b(dltmp.cardno, dltmp.buf, dltmp.len);
	default: /* to keep gcc happy */
		return (0);
	case MOXA_LOAD_CODE:
		i = moxaloadcode(dltmp.cardno, dltmp.buf, dltmp.len);
		if (i == -1)
			return (-EFAULT);
		return (i);

	}
}

int MoxaDriverPoll(void)
{
1576
	struct moxa_board_conf *brd;
L
Linus Torvalds 已提交
1577 1578 1579 1580 1581 1582 1583 1584 1585
	register ushort temp;
	register int card;
	void __iomem *ofsAddr;
	void __iomem *ip;
	int port, p, ports;

	if (moxaCard == 0)
		return (-1);
	for (card = 0; card < MAX_BOARDS; card++) {
1586 1587
		brd = &moxa_boards[card];
	        if (brd->loadstat == 0)
1588
			continue;
1589
		if ((ports = brd->numPorts) == 0)
L
Linus Torvalds 已提交
1590
			continue;
1591 1592
		if (readb(brd->intPend) == 0xff) {
			ip = brd->intTable + readb(brd->intNdx);
L
Linus Torvalds 已提交
1593 1594 1595 1596 1597
			p = card * MAX_PORTS_PER_BOARD;
			ports <<= 1;
			for (port = 0; port < ports; port += 2, p++) {
				if ((temp = readw(ip + port)) != 0) {
					writew(0, ip + port);
1598
					ofsAddr = moxa_ports[p].tableAddr;
L
Linus Torvalds 已提交
1599 1600 1601
					if (temp & IntrTx)
						writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
					if (temp & IntrBreak) {
1602
						moxa_ports[p].breakCnt++;
L
Linus Torvalds 已提交
1603 1604 1605
					}
					if (temp & IntrLine) {
						if (readb(ofsAddr + FlagStat) & DCD_state) {
1606 1607
							if ((moxa_ports[p].DCDState & DCD_oldstate) == 0)
								moxa_ports[p].DCDState = (DCD_oldstate |
L
Linus Torvalds 已提交
1608 1609
										   DCD_changed);
						} else {
1610 1611
							if (moxa_ports[p].DCDState & DCD_oldstate)
								moxa_ports[p].DCDState = DCD_changed;
L
Linus Torvalds 已提交
1612 1613 1614 1615
						}
					}
				}
			}
1616
			writeb(0, brd->intPend);
L
Linus Torvalds 已提交
1617 1618 1619 1620
		}
		if (moxaLowWaterChk) {
			p = card * MAX_PORTS_PER_BOARD;
			for (port = 0; port < ports; port++, p++) {
1621 1622 1623
				if (moxa_ports[p].lowChkFlag) {
					moxa_ports[p].lowChkFlag = 0;
					ofsAddr = moxa_ports[p].tableAddr;
J
Jiri Slaby 已提交
1624
					moxa_low_water_check(ofsAddr);
L
Linus Torvalds 已提交
1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660
				}
			}
		}
	}
	moxaLowWaterChk = 0;
	return (0);
}

/*****************************************************************************
 *	Card level function:						     *
 *	1. MoxaPortsOfCard(int cardno); 				     *
 *****************************************************************************/
int MoxaPortsOfCard(int cardno)
{

	if (moxa_boards[cardno].boardType == 0)
		return (0);
	return (moxa_boards[cardno].numPorts);
}

/*****************************************************************************
 *	Port level functions:						     *
 *	1.  MoxaPortIsValid(int port);					     *
 *	2.  MoxaPortEnable(int port);					     *
 *	3.  MoxaPortDisable(int port);					     *
 *	4.  MoxaPortGetMaxBaud(int port);				     *
 *	6.  MoxaPortSetBaud(int port, long baud);			     *
 *	8.  MoxaPortSetTermio(int port, unsigned char *termio); 	     *
 *	9.  MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);      *
 *	10. MoxaPortLineCtrl(int port, int dtrState, int rtsState);	     *
 *	11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany);    *
 *	12. MoxaPortLineStatus(int port);				     *
 *	13. MoxaPortDCDChange(int port);				     *
 *	14. MoxaPortDCDON(int port);					     *
 *	15. MoxaPortFlushData(int port, int mode);	                     *
 *	16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
A
Alan Cox 已提交
1661
 *	17. MoxaPortReadData(int port, struct tty_struct *tty); 	     *
L
Linus Torvalds 已提交
1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768
 *	20. MoxaPortTxQueue(int port);					     *
 *	21. MoxaPortTxFree(int port);					     *
 *	22. MoxaPortRxQueue(int port);					     *
 *	24. MoxaPortTxDisable(int port);				     *
 *	25. MoxaPortTxEnable(int port); 				     *
 *	27. MoxaPortResetBrkCnt(int port);				     *
 *	30. MoxaPortSendBreak(int port, int ticks);			     *
 *****************************************************************************/
/*
 *    Moxa Port Number Description:
 *
 *      MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
 *      the port number using in MOXA driver functions will be 0 to 31 for
 *      first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
 *      to 127 for fourth. For example, if you setup three MOXA boards,
 *      first board is C218, second board is C320-16 and third board is
 *      C320-32. The port number of first board (C218 - 8 ports) is from
 *      0 to 7. The port number of second board (C320 - 16 ports) is form
 *      32 to 47. The port number of third board (C320 - 32 ports) is from
 *      64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
 *      127 will be invalid.
 *
 *
 *      Moxa Functions Description:
 *
 *      Function 1:     Driver initialization routine, this routine must be
 *                      called when initialized driver.
 *      Syntax:
 *      void MoxaDriverInit();
 *
 *
 *      Function 2:     Moxa driver private IOCTL command processing.
 *      Syntax:
 *      int  MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
 *
 *           unsigned int cmd   : IOCTL command
 *           unsigned long arg  : IOCTL argument
 *           int port           : port number (0 - 127)
 *
 *           return:    0  (OK)
 *                      -EINVAL
 *                      -ENOIOCTLCMD
 *
 *
 *      Function 3:     Moxa driver polling process routine.
 *      Syntax:
 *      int  MoxaDriverPoll(void);
 *
 *           return:    0       ; polling O.K.
 *                      -1      : no any Moxa card.             
 *
 *
 *      Function 4:     Get the ports of this card.
 *      Syntax:
 *      int  MoxaPortsOfCard(int cardno);
 *
 *           int cardno         : card number (0 - 3)
 *
 *           return:    0       : this card is invalid
 *                      8/16/24/32
 *
 *
 *      Function 5:     Check this port is valid or invalid
 *      Syntax:
 *      int  MoxaPortIsValid(int port);
 *           int port           : port number (0 - 127, ref port description)
 *
 *           return:    0       : this port is invalid
 *                      1       : this port is valid
 *
 *
 *      Function 6:     Enable this port to start Tx/Rx data.
 *      Syntax:
 *      void MoxaPortEnable(int port);
 *           int port           : port number (0 - 127)
 *
 *
 *      Function 7:     Disable this port
 *      Syntax:
 *      void MoxaPortDisable(int port);
 *           int port           : port number (0 - 127)
 *
 *
 *      Function 8:     Get the maximun available baud rate of this port.
 *      Syntax:
 *      long MoxaPortGetMaxBaud(int port);
 *           int port           : port number (0 - 127)
 *
 *           return:    0       : this port is invalid
 *                      38400/57600/115200 bps
 *
 *
 *      Function 10:    Setting baud rate of this port.
 *      Syntax:
 *      long MoxaPortSetBaud(int port, long baud);
 *           int port           : port number (0 - 127)
 *           long baud          : baud rate (50 - 115200)
 *
 *           return:    0       : this port is invalid or baud < 50
 *                      50 - 115200 : the real baud rate set to the port, if
 *                                    the argument baud is large than maximun
 *                                    available baud rate, the real setting
 *                                    baud rate will be the maximun baud rate.
 *
 *
 *      Function 12:    Configure the port.
 *      Syntax:
A
Alan Cox 已提交
1769
 *      int  MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
L
Linus Torvalds 已提交
1770
 *           int port           : port number (0 - 127)
A
Alan Cox 已提交
1771
 *           struct ktermios * termio : termio structure pointer
1772
 *	     speed_t baud	: baud rate
L
Linus Torvalds 已提交
1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863
 *
 *           return:    -1      : this port is invalid or termio == NULL
 *                      0       : setting O.K.
 *
 *
 *      Function 13:    Get the DTR/RTS state of this port.
 *      Syntax:
 *      int  MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
 *           int port           : port number (0 - 127)
 *           int * dtrState     : pointer to INT to receive the current DTR
 *                                state. (if NULL, this function will not
 *                                write to this address)
 *           int * rtsState     : pointer to INT to receive the current RTS
 *                                state. (if NULL, this function will not
 *                                write to this address)
 *
 *           return:    -1      : this port is invalid
 *                      0       : O.K.
 *
 *
 *      Function 14:    Setting the DTR/RTS output state of this port.
 *      Syntax:
 *      void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
 *           int port           : port number (0 - 127)
 *           int dtrState       : DTR output state (0: off, 1: on)
 *           int rtsState       : RTS output state (0: off, 1: on)
 *
 *
 *      Function 15:    Setting the flow control of this port.
 *      Syntax:
 *      void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
 *                            int txFlow,int xany);
 *           int port           : port number (0 - 127)
 *           int rtsFlow        : H/W RTS flow control (0: no, 1: yes)
 *           int ctsFlow        : H/W CTS flow control (0: no, 1: yes)
 *           int rxFlow         : S/W Rx XON/XOFF flow control (0: no, 1: yes)
 *           int txFlow         : S/W Tx XON/XOFF flow control (0: no, 1: yes)
 *           int xany           : S/W XANY flow control (0: no, 1: yes)
 *
 *
 *      Function 16:    Get ths line status of this port
 *      Syntax:
 *      int  MoxaPortLineStatus(int port);
 *           int port           : port number (0 - 127)
 *
 *           return:    Bit 0 - CTS state (0: off, 1: on)
 *                      Bit 1 - DSR state (0: off, 1: on)
 *                      Bit 2 - DCD state (0: off, 1: on)
 *
 *
 *      Function 17:    Check the DCD state has changed since the last read
 *                      of this function.
 *      Syntax:
 *      int  MoxaPortDCDChange(int port);
 *           int port           : port number (0 - 127)
 *
 *           return:    0       : no changed
 *                      1       : DCD has changed
 *
 *
 *      Function 18:    Check ths current DCD state is ON or not.
 *      Syntax:
 *      int  MoxaPortDCDON(int port);
 *           int port           : port number (0 - 127)
 *
 *           return:    0       : DCD off
 *                      1       : DCD on
 *
 *
 *      Function 19:    Flush the Rx/Tx buffer data of this port.
 *      Syntax:
 *      void MoxaPortFlushData(int port, int mode);
 *           int port           : port number (0 - 127)
 *           int mode    
 *                      0       : flush the Rx buffer 
 *                      1       : flush the Tx buffer 
 *                      2       : flush the Rx and Tx buffer 
 *
 *
 *      Function 20:    Write data.
 *      Syntax:
 *      int  MoxaPortWriteData(int port, unsigned char * buffer, int length);
 *           int port           : port number (0 - 127)
 *           unsigned char * buffer     : pointer to write data buffer.
 *           int length         : write data length
 *
 *           return:    0 - length      : real write data length
 *
 *
 *      Function 21:    Read data.
 *      Syntax:
A
Alan Cox 已提交
1864
 *      int  MoxaPortReadData(int port, struct tty_struct *tty);
L
Linus Torvalds 已提交
1865
 *           int port           : port number (0 - 127)
A
Alan Cox 已提交
1866
 *	     struct tty_struct *tty : tty for data
L
Linus Torvalds 已提交
1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928
 *
 *           return:    0 - length      : real read data length
 *
 *
 *      Function 24:    Get the Tx buffer current queued data bytes
 *      Syntax:
 *      int  MoxaPortTxQueue(int port);
 *           int port           : port number (0 - 127)
 *
 *           return:    ..      : Tx buffer current queued data bytes
 *
 *
 *      Function 25:    Get the Tx buffer current free space
 *      Syntax:
 *      int  MoxaPortTxFree(int port);
 *           int port           : port number (0 - 127)
 *
 *           return:    ..      : Tx buffer current free space
 *
 *
 *      Function 26:    Get the Rx buffer current queued data bytes
 *      Syntax:
 *      int  MoxaPortRxQueue(int port);
 *           int port           : port number (0 - 127)
 *
 *           return:    ..      : Rx buffer current queued data bytes
 *
 *
 *      Function 28:    Disable port data transmission.
 *      Syntax:
 *      void MoxaPortTxDisable(int port);
 *           int port           : port number (0 - 127)
 *
 *
 *      Function 29:    Enable port data transmission.
 *      Syntax:
 *      void MoxaPortTxEnable(int port);
 *           int port           : port number (0 - 127)
 *
 *
 *      Function 31:    Get the received BREAK signal count and reset it.
 *      Syntax:
 *      int  MoxaPortResetBrkCnt(int port);
 *           int port           : port number (0 - 127)
 *
 *           return:    0 - ..  : BREAK signal count
 *
 *
 *      Function 34:    Send out a BREAK signal.
 *      Syntax:
 *      void MoxaPortSendBreak(int port, int ms100);
 *           int port           : port number (0 - 127)
 *           int ms100          : break signal time interval.
 *                                unit: 100 mini-second. if ms100 == 0, it will
 *                                send out a about 250 ms BREAK signal.
 *
 */
int MoxaPortIsValid(int port)
{

	if (moxaCard == 0)
		return (0);
1929
	if (moxa_ports[port].chkPort == 0)
L
Linus Torvalds 已提交
1930 1931 1932 1933 1934 1935 1936 1937 1938 1939
		return (0);
	return (1);
}

void MoxaPortEnable(int port)
{
	void __iomem *ofsAddr;
	int MoxaPortLineStatus(int);
	short lowwater = 512;

1940
	ofsAddr = moxa_ports[port].tableAddr;
L
Linus Torvalds 已提交
1941
	writew(lowwater, ofsAddr + Low_water);
1942
	moxa_ports[port].breakCnt = 0;
L
Linus Torvalds 已提交
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
	if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
	    (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
		moxafunc(ofsAddr, FC_SetBreakIrq, 0);
	} else {
		writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
	}

	moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
	moxafunc(ofsAddr, FC_FlushQueue, 2);

	moxafunc(ofsAddr, FC_EnableCH, Magic_code);
	MoxaPortLineStatus(port);
}

void MoxaPortDisable(int port)
{
1959
	void __iomem *ofsAddr = moxa_ports[port].tableAddr;
L
Linus Torvalds 已提交
1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984

	moxafunc(ofsAddr, FC_SetFlowCtl, 0);	/* disable flow control */
	moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
	writew(0, ofsAddr + HostStat);
	moxafunc(ofsAddr, FC_DisableCH, Magic_code);
}

long MoxaPortGetMaxBaud(int port)
{
	if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
	    (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI))
		return (460800L);
	else
		return (921600L);
}


long MoxaPortSetBaud(int port, long baud)
{
	void __iomem *ofsAddr;
	long max, clock;
	unsigned int val;

	if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
		return (0);
1985
	ofsAddr = moxa_ports[port].tableAddr;
L
Linus Torvalds 已提交
1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996
	if (baud > max)
		baud = max;
	if (max == 38400L)
		clock = 614400L;	/* for 9.8304 Mhz : max. 38400 bps */
	else if (max == 57600L)
		clock = 691200L;	/* for 11.0592 Mhz : max. 57600 bps */
	else
		clock = 921600L;	/* for 14.7456 Mhz : max. 115200 bps */
	val = clock / baud;
	moxafunc(ofsAddr, FC_SetBaud, val);
	baud = clock / val;
1997
	moxa_ports[port].curBaud = baud;
L
Linus Torvalds 已提交
1998 1999 2000
	return (baud);
}

A
Alan Cox 已提交
2001
int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud)
L
Linus Torvalds 已提交
2002 2003 2004 2005 2006
{
	void __iomem *ofsAddr;
	tcflag_t cflag;
	tcflag_t mode = 0;

2007
	if (moxa_ports[port].chkPort == 0 || termio == 0)
L
Linus Torvalds 已提交
2008
		return (-1);
2009
	ofsAddr = moxa_ports[port].tableAddr;
L
Linus Torvalds 已提交
2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041
	cflag = termio->c_cflag;	/* termio->c_cflag */

	mode = termio->c_cflag & CSIZE;
	if (mode == CS5)
		mode = MX_CS5;
	else if (mode == CS6)
		mode = MX_CS6;
	else if (mode == CS7)
		mode = MX_CS7;
	else if (mode == CS8)
		mode = MX_CS8;

	if (termio->c_cflag & CSTOPB) {
		if (mode == MX_CS5)
			mode |= MX_STOP15;
		else
			mode |= MX_STOP2;
	} else
		mode |= MX_STOP1;

	if (termio->c_cflag & PARENB) {
		if (termio->c_cflag & PARODD)
			mode |= MX_PARODD;
		else
			mode |= MX_PAREVEN;
	} else
		mode |= MX_PARNONE;

	moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);

	if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
	    (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2042
		if (baud >= 921600L)
L
Linus Torvalds 已提交
2043 2044
			return (-1);
	}
A
Alan Cox 已提交
2045
	baud = MoxaPortSetBaud(port, baud);
L
Linus Torvalds 已提交
2046 2047 2048 2049 2050

	if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
		writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
		writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
		writeb(FC_SetXonXoff, ofsAddr + FuncCode);
J
Jiri Slaby 已提交
2051
		moxa_wait_finish(ofsAddr);
L
Linus Torvalds 已提交
2052 2053

	}
A
Alan Cox 已提交
2054
	return (baud);
L
Linus Torvalds 已提交
2055 2056 2057 2058 2059 2060 2061 2062
}

int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState)
{

	if (!MoxaPortIsValid(port))
		return (-1);
	if (dtrState) {
2063
		if (moxa_ports[port].lineCtrl & DTR_ON)
L
Linus Torvalds 已提交
2064 2065 2066 2067 2068
			*dtrState = 1;
		else
			*dtrState = 0;
	}
	if (rtsState) {
2069
		if (moxa_ports[port].lineCtrl & RTS_ON)
L
Linus Torvalds 已提交
2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081
			*rtsState = 1;
		else
			*rtsState = 0;
	}
	return (0);
}

void MoxaPortLineCtrl(int port, int dtr, int rts)
{
	void __iomem *ofsAddr;
	int mode;

2082
	ofsAddr = moxa_ports[port].tableAddr;
L
Linus Torvalds 已提交
2083 2084 2085 2086 2087
	mode = 0;
	if (dtr)
		mode |= DTR_ON;
	if (rts)
		mode |= RTS_ON;
2088
	moxa_ports[port].lineCtrl = mode;
L
Linus Torvalds 已提交
2089 2090 2091 2092 2093 2094 2095 2096
	moxafunc(ofsAddr, FC_LineControl, mode);
}

void MoxaPortFlowCtrl(int port, int rts, int cts, int txflow, int rxflow, int txany)
{
	void __iomem *ofsAddr;
	int mode;

2097
	ofsAddr = moxa_ports[port].tableAddr;
L
Linus Torvalds 已提交
2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116
	mode = 0;
	if (rts)
		mode |= RTS_FlowCtl;
	if (cts)
		mode |= CTS_FlowCtl;
	if (txflow)
		mode |= Tx_FlowCtl;
	if (rxflow)
		mode |= Rx_FlowCtl;
	if (txany)
		mode |= IXM_IXANY;
	moxafunc(ofsAddr, FC_SetFlowCtl, mode);
}

int MoxaPortLineStatus(int port)
{
	void __iomem *ofsAddr;
	int val;

2117
	ofsAddr = moxa_ports[port].tableAddr;
L
Linus Torvalds 已提交
2118 2119 2120 2121 2122 2123 2124 2125 2126 2127
	if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
	    (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
		moxafunc(ofsAddr, FC_LineStatus, 0);
		val = readw(ofsAddr + FuncArg);
	} else {
		val = readw(ofsAddr + FlagStat) >> 4;
	}
	val &= 0x0B;
	if (val & 8) {
		val |= 4;
2128 2129
		if ((moxa_ports[port].DCDState & DCD_oldstate) == 0)
			moxa_ports[port].DCDState = (DCD_oldstate | DCD_changed);
L
Linus Torvalds 已提交
2130
	} else {
2131 2132
		if (moxa_ports[port].DCDState & DCD_oldstate)
			moxa_ports[port].DCDState = DCD_changed;
L
Linus Torvalds 已提交
2133 2134 2135 2136 2137 2138 2139 2140 2141
	}
	val &= 7;
	return (val);
}

int MoxaPortDCDChange(int port)
{
	int n;

2142
	if (moxa_ports[port].chkPort == 0)
L
Linus Torvalds 已提交
2143
		return (0);
2144 2145
	n = moxa_ports[port].DCDState;
	moxa_ports[port].DCDState &= ~DCD_changed;
L
Linus Torvalds 已提交
2146 2147 2148 2149 2150 2151 2152 2153
	n &= DCD_changed;
	return (n);
}

int MoxaPortDCDON(int port)
{
	int n;

2154
	if (moxa_ports[port].chkPort == 0)
L
Linus Torvalds 已提交
2155
		return (0);
2156
	if (moxa_ports[port].DCDState & DCD_oldstate)
L
Linus Torvalds 已提交
2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171
		n = 1;
	else
		n = 0;
	return (n);
}

int MoxaPortWriteData(int port, unsigned char * buffer, int len)
{
	int c, total, i;
	ushort tail;
	int cnt;
	ushort head, tx_mask, spage, epage;
	ushort pageno, pageofs, bufhead;
	void __iomem *baseAddr, *ofsAddr, *ofs;

2172 2173
	ofsAddr = moxa_ports[port].tableAddr;
	baseAddr = moxa_boards[port / MAX_PORTS_PER_BOARD].basemem;
L
Linus Torvalds 已提交
2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226
	tx_mask = readw(ofsAddr + TX_mask);
	spage = readw(ofsAddr + Page_txb);
	epage = readw(ofsAddr + EndPage_txb);
	tail = readw(ofsAddr + TXwptr);
	head = readw(ofsAddr + TXrptr);
	c = (head > tail) ? (head - tail - 1)
	    : (head - tail + tx_mask);
	if (c > len)
		c = len;
	moxaLog.txcnt[port] += c;
	total = c;
	if (spage == epage) {
		bufhead = readw(ofsAddr + Ofs_txb);
		writew(spage, baseAddr + Control_reg);
		while (c > 0) {
			if (head > tail)
				len = head - tail - 1;
			else
				len = tx_mask + 1 - tail;
			len = (c > len) ? len : c;
			ofs = baseAddr + DynPage_addr + bufhead + tail;
			for (i = 0; i < len; i++)
				writeb(*buffer++, ofs + i);
			tail = (tail + len) & tx_mask;
			c -= len;
		}
		writew(tail, ofsAddr + TXwptr);
	} else {
		len = c;
		pageno = spage + (tail >> 13);
		pageofs = tail & Page_mask;
		do {
			cnt = Page_size - pageofs;
			if (cnt > c)
				cnt = c;
			c -= cnt;
			writeb(pageno, baseAddr + Control_reg);
			ofs = baseAddr + DynPage_addr + pageofs;
			for (i = 0; i < cnt; i++)
				writeb(*buffer++, ofs + i);
			if (c == 0) {
				writew((tail + len) & tx_mask, ofsAddr + TXwptr);
				break;
			}
			if (++pageno == epage)
				pageno = spage;
			pageofs = 0;
		} while (1);
	}
	writeb(1, ofsAddr + CD180TXirq);	/* start to send */
	return (total);
}

A
Alan Cox 已提交
2227
int MoxaPortReadData(int port, struct tty_struct *tty)
L
Linus Torvalds 已提交
2228 2229 2230 2231 2232 2233 2234
{
	register ushort head, pageofs;
	int i, count, cnt, len, total, remain;
	ushort tail, rx_mask, spage, epage;
	ushort pageno, bufhead;
	void __iomem *baseAddr, *ofsAddr, *ofs;

2235 2236
	ofsAddr = moxa_ports[port].tableAddr;
	baseAddr = moxa_boards[port / MAX_PORTS_PER_BOARD].basemem;
L
Linus Torvalds 已提交
2237 2238 2239 2240 2241 2242 2243 2244
	head = readw(ofsAddr + RXrptr);
	tail = readw(ofsAddr + RXwptr);
	rx_mask = readw(ofsAddr + RX_mask);
	spage = readw(ofsAddr + Page_rxb);
	epage = readw(ofsAddr + EndPage_rxb);
	count = (tail >= head) ? (tail - head)
	    : (tail - head + rx_mask + 1);
	if (count == 0)
A
Alan Cox 已提交
2245
		return 0;
L
Linus Torvalds 已提交
2246

A
Alan Cox 已提交
2247
	total = count;
L
Linus Torvalds 已提交
2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261
	remain = count - total;
	moxaLog.rxcnt[port] += total;
	count = total;
	if (spage == epage) {
		bufhead = readw(ofsAddr + Ofs_rxb);
		writew(spage, baseAddr + Control_reg);
		while (count > 0) {
			if (tail >= head)
				len = tail - head;
			else
				len = rx_mask + 1 - head;
			len = (count > len) ? len : count;
			ofs = baseAddr + DynPage_addr + bufhead + head;
			for (i = 0; i < len; i++)
A
Alan Cox 已提交
2262
				tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
L
Linus Torvalds 已提交
2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278
			head = (head + len) & rx_mask;
			count -= len;
		}
		writew(head, ofsAddr + RXrptr);
	} else {
		len = count;
		pageno = spage + (head >> 13);
		pageofs = head & Page_mask;
		do {
			cnt = Page_size - pageofs;
			if (cnt > count)
				cnt = count;
			count -= cnt;
			writew(pageno, baseAddr + Control_reg);
			ofs = baseAddr + DynPage_addr + pageofs;
			for (i = 0; i < cnt; i++)
A
Alan Cox 已提交
2279
				tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
L
Linus Torvalds 已提交
2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290
			if (count == 0) {
				writew((head + len) & rx_mask, ofsAddr + RXrptr);
				break;
			}
			if (++pageno == epage)
				pageno = spage;
			pageofs = 0;
		} while (1);
	}
	if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
		moxaLowWaterChk = 1;
2291
		moxa_ports[port].lowChkFlag = 1;
L
Linus Torvalds 已提交
2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302
	}
	return (total);
}


int MoxaPortTxQueue(int port)
{
	void __iomem *ofsAddr;
	ushort rptr, wptr, mask;
	int len;

2303
	ofsAddr = moxa_ports[port].tableAddr;
L
Linus Torvalds 已提交
2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316
	rptr = readw(ofsAddr + TXrptr);
	wptr = readw(ofsAddr + TXwptr);
	mask = readw(ofsAddr + TX_mask);
	len = (wptr - rptr) & mask;
	return (len);
}

int MoxaPortTxFree(int port)
{
	void __iomem *ofsAddr;
	ushort rptr, wptr, mask;
	int len;

2317
	ofsAddr = moxa_ports[port].tableAddr;
L
Linus Torvalds 已提交
2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330
	rptr = readw(ofsAddr + TXrptr);
	wptr = readw(ofsAddr + TXwptr);
	mask = readw(ofsAddr + TX_mask);
	len = mask - ((wptr - rptr) & mask);
	return (len);
}

int MoxaPortRxQueue(int port)
{
	void __iomem *ofsAddr;
	ushort rptr, wptr, mask;
	int len;

2331
	ofsAddr = moxa_ports[port].tableAddr;
L
Linus Torvalds 已提交
2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343
	rptr = readw(ofsAddr + RXrptr);
	wptr = readw(ofsAddr + RXwptr);
	mask = readw(ofsAddr + RX_mask);
	len = (wptr - rptr) & mask;
	return (len);
}


void MoxaPortTxDisable(int port)
{
	void __iomem *ofsAddr;

2344
	ofsAddr = moxa_ports[port].tableAddr;
L
Linus Torvalds 已提交
2345 2346 2347 2348 2349 2350 2351
	moxafunc(ofsAddr, FC_SetXoffState, Magic_code);
}

void MoxaPortTxEnable(int port)
{
	void __iomem *ofsAddr;

2352
	ofsAddr = moxa_ports[port].tableAddr;
L
Linus Torvalds 已提交
2353 2354 2355 2356 2357 2358 2359
	moxafunc(ofsAddr, FC_SetXonState, Magic_code);
}


int MoxaPortResetBrkCnt(int port)
{
	ushort cnt;
2360 2361
	cnt = moxa_ports[port].breakCnt;
	moxa_ports[port].breakCnt = 0;
L
Linus Torvalds 已提交
2362 2363 2364 2365 2366 2367 2368 2369
	return (cnt);
}


void MoxaPortSendBreak(int port, int ms100)
{
	void __iomem *ofsAddr;

2370
	ofsAddr = moxa_ports[port].tableAddr;
L
Linus Torvalds 已提交
2371 2372
	if (ms100) {
		moxafunc(ofsAddr, FC_SendBreak, Magic_code);
J
Jiri Slaby 已提交
2373
		msleep(ms100 * 10);
L
Linus Torvalds 已提交
2374 2375
	} else {
		moxafunc(ofsAddr, FC_SendBreak, Magic_code);
J
Jiri Slaby 已提交
2376
		msleep(250);
L
Linus Torvalds 已提交
2377 2378 2379 2380
	}
	moxafunc(ofsAddr, FC_StopBreak, Magic_code);
}

2381
static int moxa_get_serial_info(struct moxa_port *info,
L
Linus Torvalds 已提交
2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402
				struct serial_struct __user *retinfo)
{
	struct serial_struct tmp;

	memset(&tmp, 0, sizeof(tmp));
	tmp.type = info->type;
	tmp.line = info->port;
	tmp.port = 0;
	tmp.irq = 0;
	tmp.flags = info->asyncflags;
	tmp.baud_base = 921600;
	tmp.close_delay = info->close_delay;
	tmp.closing_wait = info->closing_wait;
	tmp.custom_divisor = 0;
	tmp.hub6 = 0;
	if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
		return -EFAULT;
	return (0);
}


2403
static int moxa_set_serial_info(struct moxa_port *info,
L
Linus Torvalds 已提交
2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449
				struct serial_struct __user *new_info)
{
	struct serial_struct new_serial;

	if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
		return -EFAULT;

	if ((new_serial.irq != 0) ||
	    (new_serial.port != 0) ||
//           (new_serial.type != info->type) ||
	    (new_serial.custom_divisor != 0) ||
	    (new_serial.baud_base != 921600))
		return (-EPERM);

	if (!capable(CAP_SYS_ADMIN)) {
		if (((new_serial.flags & ~ASYNC_USR_MASK) !=
		     (info->asyncflags & ~ASYNC_USR_MASK)))
			return (-EPERM);
	} else {
		info->close_delay = new_serial.close_delay * HZ / 100;
		info->closing_wait = new_serial.closing_wait * HZ / 100;
	}

	new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
	new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);

	if (new_serial.type == PORT_16550A) {
		MoxaSetFifo(info->port, 1);
	} else {
		MoxaSetFifo(info->port, 0);
	}

	info->type = new_serial.type;
	return (0);
}



/*****************************************************************************
 *	Static local functions: 					     *
 *****************************************************************************/
static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
{

	writew(arg, ofsAddr + FuncArg);
	writew(cmd, ofsAddr + FuncCode);
J
Jiri Slaby 已提交
2450
	moxa_wait_finish(ofsAddr);
L
Linus Torvalds 已提交
2451 2452
}

J
Jiri Slaby 已提交
2453
static void moxa_wait_finish(void __iomem *ofsAddr)
L
Linus Torvalds 已提交
2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465
{
	unsigned long i, j;

	i = jiffies;
	while (readw(ofsAddr + FuncCode) != 0) {
		j = jiffies;
		if ((j - i) > moxaFuncTout) {
			return;
		}
	}
}

J
Jiri Slaby 已提交
2466
static void moxa_low_water_check(void __iomem *ofsAddr)
L
Linus Torvalds 已提交
2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485
{
	int len;
	ushort rptr, wptr, mask;

	if (readb(ofsAddr + FlagStat) & Xoff_state) {
		rptr = readw(ofsAddr + RXrptr);
		wptr = readw(ofsAddr + RXwptr);
		mask = readw(ofsAddr + RX_mask);
		len = (wptr - rptr) & mask;
		if (len <= Low_water)
			moxafunc(ofsAddr, FC_SendXon, 0);
	}
}

static int moxaloadbios(int cardno, unsigned char __user *tmp, int len)
{
	void __iomem *baseAddr;
	int i;

2486 2487
	if(len < 0 || len > sizeof(moxaBuff))
		return -EINVAL;
L
Linus Torvalds 已提交
2488 2489
	if(copy_from_user(moxaBuff, tmp, len))
		return -EFAULT;
2490
	baseAddr = moxa_boards[cardno].basemem;
L
Linus Torvalds 已提交
2491
	writeb(HW_reset, baseAddr + Control_reg);	/* reset */
J
Jiri Slaby 已提交
2492
	msleep(10);
L
Linus Torvalds 已提交
2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505
	for (i = 0; i < 4096; i++)
		writeb(0, baseAddr + i);	/* clear fix page */
	for (i = 0; i < len; i++)
		writeb(moxaBuff[i], baseAddr + i);	/* download BIOS */
	writeb(0, baseAddr + Control_reg);	/* restart */
	return (0);
}

static int moxafindcard(int cardno)
{
	void __iomem *baseAddr;
	ushort tmp;

2506
	baseAddr = moxa_boards[cardno].basemem;
L
Linus Torvalds 已提交
2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534
	switch (moxa_boards[cardno].boardType) {
	case MOXA_BOARD_C218_ISA:
	case MOXA_BOARD_C218_PCI:
		if ((tmp = readw(baseAddr + C218_key)) != C218_KeyCode) {
			return (-1);
		}
		break;
	case MOXA_BOARD_CP204J:
		if ((tmp = readw(baseAddr + C218_key)) != CP204J_KeyCode) {
			return (-1);
		}
		break;
	default:
		if ((tmp = readw(baseAddr + C320_key)) != C320_KeyCode) {
			return (-1);
		}
		if ((tmp = readw(baseAddr + C320_status)) != STS_init) {
			return (-2);
		}
	}
	return (0);
}

static int moxaload320b(int cardno, unsigned char __user *tmp, int len)
{
	void __iomem *baseAddr;
	int i;

2535
	if(len < 0 || len > sizeof(moxaBuff))
L
Linus Torvalds 已提交
2536 2537 2538
		return -EINVAL;
	if(copy_from_user(moxaBuff, tmp, len))
		return -EFAULT;
2539
	baseAddr = moxa_boards[cardno].basemem;
L
Linus Torvalds 已提交
2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554
	writew(len - 7168 - 2, baseAddr + C320bapi_len);
	writeb(1, baseAddr + Control_reg);	/* Select Page 1 */
	for (i = 0; i < 7168; i++)
		writeb(moxaBuff[i], baseAddr + DynPage_addr + i);
	writeb(2, baseAddr + Control_reg);	/* Select Page 2 */
	for (i = 0; i < (len - 7168); i++)
		writeb(moxaBuff[i + 7168], baseAddr + DynPage_addr + i);
	return (0);
}

static int moxaloadcode(int cardno, unsigned char __user *tmp, int len)
{
	void __iomem *baseAddr, *ofsAddr;
	int retval, port, i;

2555 2556
	if(len < 0 || len > sizeof(moxaBuff))
		return -EINVAL;
L
Linus Torvalds 已提交
2557 2558
	if(copy_from_user(moxaBuff, tmp, len))
		return -EFAULT;
2559
	baseAddr = moxa_boards[cardno].basemem;
L
Linus Torvalds 已提交
2560 2561 2562 2563 2564 2565 2566 2567 2568
	switch (moxa_boards[cardno].boardType) {
	case MOXA_BOARD_C218_ISA:
	case MOXA_BOARD_C218_PCI:
	case MOXA_BOARD_CP204J:
		retval = moxaloadc218(cardno, baseAddr, len);
		if (retval)
			return (retval);
		port = cardno * MAX_PORTS_PER_BOARD;
		for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2569 2570 2571 2572 2573 2574 2575
			struct moxa_port *p = &moxa_ports[port];

			p->chkPort = 1;
			p->curBaud = 9600L;
			p->DCDState = 0;
			p->tableAddr = baseAddr + Extern_table + Extern_size * i;
			ofsAddr = p->tableAddr;
L
Linus Torvalds 已提交
2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592
			writew(C218rx_mask, ofsAddr + RX_mask);
			writew(C218tx_mask, ofsAddr + TX_mask);
			writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
			writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);

			writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
			writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);

		}
		break;
	default:
		retval = moxaloadc320(cardno, baseAddr, len,
				      &moxa_boards[cardno].numPorts);
		if (retval)
			return (retval);
		port = cardno * MAX_PORTS_PER_BOARD;
		for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2593 2594 2595 2596 2597 2598 2599
			struct moxa_port *p = &moxa_ports[port];

			p->chkPort = 1;
			p->curBaud = 9600L;
			p->DCDState = 0;
			p->tableAddr = baseAddr + Extern_table + Extern_size * i;
			ofsAddr = p->tableAddr;
L
Linus Torvalds 已提交
2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634
			if (moxa_boards[cardno].numPorts == 8) {
				writew(C320p8rx_mask, ofsAddr + RX_mask);
				writew(C320p8tx_mask, ofsAddr + TX_mask);
				writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
				writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
				writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
				writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);

			} else if (moxa_boards[cardno].numPorts == 16) {
				writew(C320p16rx_mask, ofsAddr + RX_mask);
				writew(C320p16tx_mask, ofsAddr + TX_mask);
				writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
				writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
				writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
				writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);

			} else if (moxa_boards[cardno].numPorts == 24) {
				writew(C320p24rx_mask, ofsAddr + RX_mask);
				writew(C320p24tx_mask, ofsAddr + TX_mask);
				writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
				writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
				writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
				writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
			} else if (moxa_boards[cardno].numPorts == 32) {
				writew(C320p32rx_mask, ofsAddr + RX_mask);
				writew(C320p32tx_mask, ofsAddr + TX_mask);
				writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
				writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
				writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
				writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
				writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
			}
		}
		break;
	}
2635
	moxa_boards[cardno].loadstat = 1;
L
Linus Torvalds 已提交
2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652
	return (0);
}

static int moxaloadc218(int cardno, void __iomem *baseAddr, int len)
{
	char retry;
	int i, j, len1, len2;
	ushort usum, *ptr, keycode;

	if (moxa_boards[cardno].boardType == MOXA_BOARD_CP204J)
		keycode = CP204J_KeyCode;
	else
		keycode = C218_KeyCode;
	usum = 0;
	len1 = len >> 1;
	ptr = (ushort *) moxaBuff;
	for (i = 0; i < len1; i++)
2653
		usum += le16_to_cpu(*(ptr + i));
L
Linus Torvalds 已提交
2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669
	retry = 0;
	do {
		len1 = len >> 1;
		j = 0;
		while (len1) {
			len2 = (len1 > 2048) ? 2048 : len1;
			len1 -= len2;
			for (i = 0; i < len2 << 1; i++)
				writeb(moxaBuff[i + j], baseAddr + C218_LoadBuf + i);
			j += i;

			writew(len2, baseAddr + C218DLoad_len);
			writew(0, baseAddr + C218_key);
			for (i = 0; i < 100; i++) {
				if (readw(baseAddr + C218_key) == keycode)
					break;
J
Jiri Slaby 已提交
2670
				msleep(10);
L
Linus Torvalds 已提交
2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681
			}
			if (readw(baseAddr + C218_key) != keycode) {
				return (-1);
			}
		}
		writew(0, baseAddr + C218DLoad_len);
		writew(usum, baseAddr + C218check_sum);
		writew(0, baseAddr + C218_key);
		for (i = 0; i < 100; i++) {
			if (readw(baseAddr + C218_key) == keycode)
				break;
J
Jiri Slaby 已提交
2682
			msleep(10);
L
Linus Torvalds 已提交
2683 2684 2685 2686 2687 2688 2689 2690 2691 2692
		}
		retry++;
	} while ((readb(baseAddr + C218chksum_ok) != 1) && (retry < 3));
	if (readb(baseAddr + C218chksum_ok) != 1) {
		return (-1);
	}
	writew(0, baseAddr + C218_key);
	for (i = 0; i < 100; i++) {
		if (readw(baseAddr + Magic_no) == Magic_code)
			break;
J
Jiri Slaby 已提交
2693
		msleep(10);
L
Linus Torvalds 已提交
2694 2695 2696 2697 2698 2699 2700 2701 2702
	}
	if (readw(baseAddr + Magic_no) != Magic_code) {
		return (-1);
	}
	writew(1, baseAddr + Disable_IRQ);
	writew(0, baseAddr + Magic_no);
	for (i = 0; i < 100; i++) {
		if (readw(baseAddr + Magic_no) == Magic_code)
			break;
J
Jiri Slaby 已提交
2703
		msleep(10);
L
Linus Torvalds 已提交
2704 2705 2706 2707 2708
	}
	if (readw(baseAddr + Magic_no) != Magic_code) {
		return (-1);
	}
	moxaCard = 1;
2709 2710 2711
	moxa_boards[cardno].intNdx = baseAddr + IRQindex;
	moxa_boards[cardno].intPend = baseAddr + IRQpending;
	moxa_boards[cardno].intTable = baseAddr + IRQtable;
L
Linus Torvalds 已提交
2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724
	return (0);
}

static int moxaloadc320(int cardno, void __iomem *baseAddr, int len, int *numPorts)
{
	ushort usum;
	int i, j, wlen, len2, retry;
	ushort *uptr;

	usum = 0;
	wlen = len >> 1;
	uptr = (ushort *) moxaBuff;
	for (i = 0; i < wlen; i++)
2725
		usum += le16_to_cpu(uptr[i]);
L
Linus Torvalds 已提交
2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744
	retry = 0;
	j = 0;
	do {
		while (wlen) {
			if (wlen > 2048)
				len2 = 2048;
			else
				len2 = wlen;
			wlen -= len2;
			len2 <<= 1;
			for (i = 0; i < len2; i++)
				writeb(moxaBuff[j + i], baseAddr + C320_LoadBuf + i);
			len2 >>= 1;
			j += i;
			writew(len2, baseAddr + C320DLoad_len);
			writew(0, baseAddr + C320_key);
			for (i = 0; i < 10; i++) {
				if (readw(baseAddr + C320_key) == C320_KeyCode)
					break;
J
Jiri Slaby 已提交
2745
				msleep(10);
L
Linus Torvalds 已提交
2746 2747 2748 2749 2750 2751 2752 2753 2754 2755
			}
			if (readw(baseAddr + C320_key) != C320_KeyCode)
				return (-1);
		}
		writew(0, baseAddr + C320DLoad_len);
		writew(usum, baseAddr + C320check_sum);
		writew(0, baseAddr + C320_key);
		for (i = 0; i < 10; i++) {
			if (readw(baseAddr + C320_key) == C320_KeyCode)
				break;
J
Jiri Slaby 已提交
2756
			msleep(10);
L
Linus Torvalds 已提交
2757 2758 2759 2760 2761 2762 2763 2764 2765
		}
		retry++;
	} while ((readb(baseAddr + C320chksum_ok) != 1) && (retry < 3));
	if (readb(baseAddr + C320chksum_ok) != 1)
		return (-1);
	writew(0, baseAddr + C320_key);
	for (i = 0; i < 600; i++) {
		if (readw(baseAddr + Magic_no) == Magic_code)
			break;
J
Jiri Slaby 已提交
2766
		msleep(10);
L
Linus Torvalds 已提交
2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784
	}
	if (readw(baseAddr + Magic_no) != Magic_code)
		return (-100);

	if (moxa_boards[cardno].busType == MOXA_BUS_TYPE_PCI) {		/* ASIC board */
		writew(0x3800, baseAddr + TMS320_PORT1);
		writew(0x3900, baseAddr + TMS320_PORT2);
		writew(28499, baseAddr + TMS320_CLOCK);
	} else {
		writew(0x3200, baseAddr + TMS320_PORT1);
		writew(0x3400, baseAddr + TMS320_PORT2);
		writew(19999, baseAddr + TMS320_CLOCK);
	}
	writew(1, baseAddr + Disable_IRQ);
	writew(0, baseAddr + Magic_no);
	for (i = 0; i < 500; i++) {
		if (readw(baseAddr + Magic_no) == Magic_code)
			break;
J
Jiri Slaby 已提交
2785
		msleep(10);
L
Linus Torvalds 已提交
2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798
	}
	if (readw(baseAddr + Magic_no) != Magic_code)
		return (-102);

	j = readw(baseAddr + Module_cnt);
	if (j <= 0)
		return (-101);
	*numPorts = j * 8;
	writew(j, baseAddr + Module_no);
	writew(0, baseAddr + Magic_no);
	for (i = 0; i < 600; i++) {
		if (readw(baseAddr + Magic_no) == Magic_code)
			break;
J
Jiri Slaby 已提交
2799
		msleep(10);
L
Linus Torvalds 已提交
2800 2801 2802 2803
	}
	if (readw(baseAddr + Magic_no) != Magic_code)
		return (-102);
	moxaCard = 1;
2804 2805 2806
	moxa_boards[cardno].intNdx = baseAddr + IRQindex;
	moxa_boards[cardno].intPend = baseAddr + IRQpending;
	moxa_boards[cardno].intTable = baseAddr + IRQtable;
L
Linus Torvalds 已提交
2807 2808 2809 2810 2811
	return (0);
}

static void MoxaSetFifo(int port, int enable)
{
2812
	void __iomem *ofsAddr = moxa_ports[port].tableAddr;
L
Linus Torvalds 已提交
2813 2814 2815 2816 2817 2818 2819 2820 2821

	if (!enable) {
		moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
		moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
	} else {
		moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
		moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
	}
}