moxa.c 64.7 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
/*****************************************************************************/
/*
 *           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>
J
Jiri Slaby 已提交
28
#include <linux/firmware.h>
L
Linus Torvalds 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#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 已提交
45
#include <linux/completion.h>
L
Linus Torvalds 已提交
46 47 48 49 50

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

J
Jiri Slaby 已提交
51 52
#include "moxa.h"

J
Jiri Slaby 已提交
53
#define MOXA_VERSION		"5.1k"
L
Linus Torvalds 已提交
54

J
Jiri Slaby 已提交
55 56
#define MOXA_FW_HDRLEN		32

J
Jiri Slaby 已提交
57 58
#define MOXAMAJOR		172
#define MOXACUMAJOR		173
L
Linus Torvalds 已提交
59

J
Jiri Slaby 已提交
60
#define MAX_BOARDS		4	/* Don't change this value */
L
Linus Torvalds 已提交
61
#define MAX_PORTS_PER_BOARD	32	/* Don't change this value */
J
Jiri Slaby 已提交
62
#define MAX_PORTS		(MAX_BOARDS * MAX_PORTS_PER_BOARD)
L
Linus Torvalds 已提交
63 64 65 66

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

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 已提交
89 90 91 92 93 94
	{ 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 已提交
95 96 97 98 99
	{ 0 }
};
MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
#endif /* CONFIG_PCI */

J
Jiri Slaby 已提交
100 101
struct moxa_port;

102
static struct moxa_board_conf {
L
Linus Torvalds 已提交
103 104 105
	int boardType;
	int numPorts;
	int busType;
106 107 108

	int loadstat;

J
Jiri Slaby 已提交
109 110
	struct moxa_port *ports;

111 112 113 114 115 116 117 118 119 120 121 122
	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;
123
};
L
Linus Torvalds 已提交
124

125 126 127 128
struct moxaq_str {
	int inq;
	int outq;
};
L
Linus Torvalds 已提交
129

130
struct moxa_port {
J
Jiri Slaby 已提交
131
	struct moxa_board_conf *board;
L
Linus Torvalds 已提交
132 133 134 135 136 137 138 139 140
	int type;
	int close_delay;
	int count;
	int blocked_open;
	int asyncflags;
	unsigned long statusflags;
	struct tty_struct *tty;
	int cflag;
	wait_queue_head_t open_wait;
J
Jiri Slaby 已提交
141
	struct completion close_wait;
L
Linus Torvalds 已提交
142

143
	struct timer_list emptyTimer;
L
Linus Torvalds 已提交
144

145 146 147 148 149 150 151 152
	char chkPort;
	char lineCtrl;
	void __iomem *tableAddr;
	char DCDState;
	char lowChkFlag;

	ushort breakCnt;
};
L
Linus Torvalds 已提交
153 154 155 156 157 158 159 160 161 162 163 164

/* 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;
J
Jiri Slaby 已提交
165
static int moxaCard;
L
Linus Torvalds 已提交
166 167
/* Variables for insmod */
#ifdef MODULE
168 169 170
static unsigned long baseaddr[MAX_BOARDS];
static unsigned int type[MAX_BOARDS];
static unsigned int numports[MAX_BOARDS];
L
Linus Torvalds 已提交
171 172 173 174 175 176
#endif

MODULE_AUTHOR("William Chen");
MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
MODULE_LICENSE("GPL");
#ifdef MODULE
177 178 179 180 181 182
module_param_array(type, uint, NULL, 0);
MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
module_param_array(baseaddr, ulong, NULL, 0);
MODULE_PARM_DESC(baseaddr, "base address");
module_param_array(numports, uint, NULL, 0);
MODULE_PARM_DESC(numports, "numports (ignored for C218)");
L
Linus Torvalds 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
#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 已提交
200
static void moxa_set_termios(struct tty_struct *, struct ktermios *);
L
Linus Torvalds 已提交
201 202 203 204 205 206 207
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 已提交
208
static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
J
Jiri Slaby 已提交
209
static int moxa_block_till_ready(struct tty_struct *, struct file *,
210
			    struct moxa_port *);
J
Jiri Slaby 已提交
211 212 213 214
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 已提交
215 216 217
/*
 * moxa board interface functions:
 */
J
Jiri Slaby 已提交
218
static int MoxaDriverIoctl(struct tty_struct *, unsigned int, unsigned long);
L
Linus Torvalds 已提交
219 220 221
static int MoxaDriverPoll(void);
static int MoxaPortsOfCard(int);
static int MoxaPortIsValid(int);
J
Jiri Slaby 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
static void MoxaPortEnable(struct moxa_port *);
static void MoxaPortDisable(struct moxa_port *);
static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
static void MoxaPortLineCtrl(struct moxa_port *, int, int);
static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
static int MoxaPortLineStatus(struct moxa_port *);
static int MoxaPortDCDChange(struct moxa_port *);
static int MoxaPortDCDON(struct moxa_port *);
static void MoxaPortFlushData(struct moxa_port *, int);
static int MoxaPortWriteData(struct moxa_port *, unsigned char *, int);
static int MoxaPortReadData(struct moxa_port *, struct tty_struct *tty);
static int MoxaPortTxQueue(struct moxa_port *);
static int MoxaPortRxQueue(struct moxa_port *);
static int MoxaPortTxFree(struct moxa_port *);
static void MoxaPortTxDisable(struct moxa_port *);
static void MoxaPortTxEnable(struct moxa_port *);
static int MoxaPortResetBrkCnt(struct moxa_port *);
static void MoxaPortSendBreak(struct moxa_port *, int);
241 242
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 *);
J
Jiri Slaby 已提交
243
static void MoxaSetFifo(struct moxa_port *port, int enable);
L
Linus Torvalds 已提交
244

J
Jeff Dike 已提交
245
static const struct tty_operations moxa_ops = {
L
Linus Torvalds 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
	.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 已提交
265
static struct tty_driver *moxaDriver;
266
static struct moxa_port moxa_ports[MAX_PORTS];
J
Jiri Slaby 已提交
267
static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
I
Ingo Molnar 已提交
268
static DEFINE_SPINLOCK(moxa_lock);
A
Alan Cox 已提交
269

J
Jiri Slaby 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
{
	switch (brd->boardType) {
	case MOXA_BOARD_C218_ISA:
	case MOXA_BOARD_C218_PCI:
		if (model != 1)
			goto err;
		break;
	case MOXA_BOARD_CP204J:
		if (model != 3)
			goto err;
		break;
	default:
		if (model != 2)
			goto err;
		break;
	}
	return 0;
err:
	return -EINVAL;
}

static int moxa_check_fw(const void *ptr)
{
	const __le16 *lptr = ptr;

	if (*lptr != cpu_to_le16(0x7980))
		return -EINVAL;

	return 0;
}

static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
		size_t len)
{
	void __iomem *baseAddr = brd->basemem;
	u16 tmp;

	writeb(HW_reset, baseAddr + Control_reg);	/* reset */
	msleep(10);
	memset_io(baseAddr, 0, 4096);
	memcpy_toio(baseAddr, buf, len);	/* download BIOS */
	writeb(0, baseAddr + Control_reg);	/* restart */

	msleep(2000);

	switch (brd->boardType) {
	case MOXA_BOARD_C218_ISA:
	case MOXA_BOARD_C218_PCI:
		tmp = readw(baseAddr + C218_key);
		if (tmp != C218_KeyCode)
			goto err;
		break;
	case MOXA_BOARD_CP204J:
		tmp = readw(baseAddr + C218_key);
		if (tmp != CP204J_KeyCode)
			goto err;
		break;
	default:
		tmp = readw(baseAddr + C320_key);
		if (tmp != C320_KeyCode)
			goto err;
		tmp = readw(baseAddr + C320_status);
		if (tmp != STS_init) {
			printk(KERN_ERR "moxa: bios upload failed -- CPU/Basic "
					"module not found\n");
			return -EIO;
		}
		break;
	}

	return 0;
err:
	printk(KERN_ERR "moxa: bios upload failed -- board not found\n");
	return -EIO;
}

static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
		size_t len)
{
	void __iomem *baseAddr = brd->basemem;

	if (len < 7168) {
		printk(KERN_ERR "moxa: invalid 320 bios -- too short\n");
		return -EINVAL;
	}

	writew(len - 7168 - 2, baseAddr + C320bapi_len);
	writeb(1, baseAddr + Control_reg);	/* Select Page 1 */
	memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
	writeb(2, baseAddr + Control_reg);	/* Select Page 2 */
	memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);

	return 0;
}

366
static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
J
Jiri Slaby 已提交
367 368 369 370 371
		size_t len)
{
	void __iomem *baseAddr = brd->basemem;
	const u16 *uptr = ptr;
	size_t wlen, len2, j;
372 373
	unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
	unsigned int i, retry, c320;
J
Jiri Slaby 已提交
374 375
	u16 usum, keycode;

376 377 378 379
	c320 = brd->boardType == MOXA_BOARD_C320_PCI ||
			brd->boardType == MOXA_BOARD_C320_ISA;
	keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
				C218_KeyCode;
J
Jiri Slaby 已提交
380

381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
	switch (brd->boardType) {
	case MOXA_BOARD_CP204J:
	case MOXA_BOARD_C218_ISA:
	case MOXA_BOARD_C218_PCI:
		key = C218_key;
		loadbuf = C218_LoadBuf;
		loadlen = C218DLoad_len;
		checksum = C218check_sum;
		checksum_ok = C218chksum_ok;
		break;
	default:
		key = C320_key;
		keycode = C320_KeyCode;
		loadbuf = C320_LoadBuf;
		loadlen = C320DLoad_len;
		checksum = C320check_sum;
		checksum_ok = C320chksum_ok;
		break;
J
Jiri Slaby 已提交
399 400 401 402 403 404 405 406 407 408 409 410 411
	}

	usum = 0;
	wlen = len >> 1;
	for (i = 0; i < wlen; i++)
		usum += le16_to_cpu(uptr[i]);
	retry = 0;
	do {
		wlen = len >> 1;
		j = 0;
		while (wlen) {
			len2 = (wlen > 2048) ? 2048 : wlen;
			wlen -= len2;
412
			memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
J
Jiri Slaby 已提交
413
			j += len2 << 1;
414 415 416 417 418

			writew(len2, baseAddr + loadlen);
			writew(0, baseAddr + key);
			for (i = 0; i < 100; i++) {
				if (readw(baseAddr + key) == keycode)
J
Jiri Slaby 已提交
419 420 421
					break;
				msleep(10);
			}
422
			if (readw(baseAddr + key) != keycode)
J
Jiri Slaby 已提交
423 424
				return -EIO;
		}
425 426 427 428 429
		writew(0, baseAddr + loadlen);
		writew(usum, baseAddr + checksum);
		writew(0, baseAddr + key);
		for (i = 0; i < 100; i++) {
			if (readw(baseAddr + key) == keycode)
J
Jiri Slaby 已提交
430 431 432 433
				break;
			msleep(10);
		}
		retry++;
434 435
	} while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
	if (readb(baseAddr + checksum_ok) != 1)
J
Jiri Slaby 已提交
436 437
		return -EIO;

438
	writew(0, baseAddr + key);
J
Jiri Slaby 已提交
439 440 441 442 443 444 445 446
	for (i = 0; i < 600; i++) {
		if (readw(baseAddr + Magic_no) == Magic_code)
			break;
		msleep(10);
	}
	if (readw(baseAddr + Magic_no) != Magic_code)
		return -EIO;

447 448 449 450 451 452 453 454 455 456
	if (c320) {
		if (brd->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);
		}
J
Jiri Slaby 已提交
457 458 459 460 461 462 463 464 465 466 467
	}
	writew(1, baseAddr + Disable_IRQ);
	writew(0, baseAddr + Magic_no);
	for (i = 0; i < 500; i++) {
		if (readw(baseAddr + Magic_no) == Magic_code)
			break;
		msleep(10);
	}
	if (readw(baseAddr + Magic_no) != Magic_code)
		return -EIO;

468 469 470 471 472 473 474 475 476 477 478 479 480 481
	if (c320) {
		j = readw(baseAddr + Module_cnt);
		if (j <= 0)
			return -EIO;
		brd->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;
			msleep(10);
		}
		if (readw(baseAddr + Magic_no) != Magic_code)
			return -EIO;
J
Jiri Slaby 已提交
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
	}
	moxaCard = 1;
	brd->intNdx = baseAddr + IRQindex;
	brd->intPend = baseAddr + IRQpending;
	brd->intTable = baseAddr + IRQtable;

	return 0;
}

static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
		size_t len)
{
	void __iomem *ofsAddr, *baseAddr = brd->basemem;
	struct moxa_port *port;
	int retval, i;

	if (len % 2) {
499
		printk(KERN_ERR "moxa: bios length is not even\n");
J
Jiri Slaby 已提交
500 501 502
		return -EINVAL;
	}

503 504 505 506
	retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
	if (retval)
		return retval;

J
Jiri Slaby 已提交
507 508 509 510 511 512
	switch (brd->boardType) {
	case MOXA_BOARD_C218_ISA:
	case MOXA_BOARD_C218_PCI:
	case MOXA_BOARD_CP204J:
		port = brd->ports;
		for (i = 0; i < brd->numPorts; i++, port++) {
J
Jiri Slaby 已提交
513
			port->board = brd;
J
Jiri Slaby 已提交
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
			port->chkPort = 1;
			port->DCDState = 0;
			port->tableAddr = baseAddr + Extern_table +
					Extern_size * i;
			ofsAddr = port->tableAddr;
			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:
		port = brd->ports;
		for (i = 0; i < brd->numPorts; i++, port++) {
J
Jiri Slaby 已提交
532
			port->board = brd;
J
Jiri Slaby 已提交
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
			port->chkPort = 1;
			port->DCDState = 0;
			port->tableAddr = baseAddr + Extern_table +
					Extern_size * i;
			ofsAddr = port->tableAddr;
			switch (brd->numPorts) {
			case 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);

				break;
			case 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);
				break;

			case 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);
				break;
			case 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;
			}
		}
		break;
	}
	brd->loadstat = 1;
	return 0;
}

static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
{
	void *ptr = fw->data;
	char rsn[64];
	u16 lens[5];
	size_t len;
	unsigned int a, lenp, lencnt;
	int ret = -EINVAL;
	struct {
		__le32 magic;	/* 0x34303430 */
		u8 reserved1[2];
		u8 type;	/* UNIX = 3 */
		u8 model;	/* C218T=1, C320T=2, CP204=3 */
		u8 reserved2[8];
		__le16 len[5];
	} *hdr = ptr;

	BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));

	if (fw->size < MOXA_FW_HDRLEN) {
		strcpy(rsn, "too short (even header won't fit)");
		goto err;
	}
	if (hdr->magic != cpu_to_le32(0x30343034)) {
		sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
		goto err;
	}
	if (hdr->type != 3) {
		sprintf(rsn, "not for linux, type is %u", hdr->type);
		goto err;
	}
	if (moxa_check_fw_model(brd, hdr->model)) {
		sprintf(rsn, "not for this card, model is %u", hdr->model);
		goto err;
	}

	len = MOXA_FW_HDRLEN;
	lencnt = hdr->model == 2 ? 5 : 3;
	for (a = 0; a < ARRAY_SIZE(lens); a++) {
		lens[a] = le16_to_cpu(hdr->len[a]);
		if (lens[a] && len + lens[a] <= fw->size &&
				moxa_check_fw(&fw->data[len]))
			printk(KERN_WARNING "moxa firmware: unexpected input "
				"at offset %u, but going on\n", (u32)len);
		if (!lens[a] && a < lencnt) {
			sprintf(rsn, "too few entries in fw file");
			goto err;
		}
		len += lens[a];
	}

	if (len != fw->size) {
		sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
				(u32)len);
		goto err;
	}

	ptr += MOXA_FW_HDRLEN;
	lenp = 0; /* bios */

	strcpy(rsn, "read above");

	ret = moxa_load_bios(brd, ptr, lens[lenp]);
	if (ret)
		goto err;

	/* we skip the tty section (lens[1]), since we don't need it */
	ptr += lens[lenp] + lens[lenp + 1];
	lenp += 2; /* comm */

	if (hdr->model == 2) {
		ret = moxa_load_320b(brd, ptr, lens[lenp]);
		if (ret)
			goto err;
		/* skip another tty */
		ptr += lens[lenp] + lens[lenp + 1];
		lenp += 2;
	}

	ret = moxa_load_code(brd, ptr, lens[lenp]);
	if (ret)
		goto err;

	return 0;
err:
	printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
	return ret;
}

static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
{
	const struct firmware *fw;
	const char *file;
	int ret;

	switch (brd->boardType) {
	case MOXA_BOARD_C218_ISA:
	case MOXA_BOARD_C218_PCI:
		file = "c218tunx.cod";
		break;
	case MOXA_BOARD_CP204J:
		file = "cp204unx.cod";
		break;
	default:
		file = "c320tunx.cod";
		break;
	}

	ret = request_firmware(&fw, file, dev);
	if (ret) {
		printk(KERN_ERR "request_firmware failed\n");
		goto end;
	}

	ret = moxa_load_fw(brd, fw);

	release_firmware(fw);
end:
	return ret;
}

L
Linus Torvalds 已提交
703
#ifdef CONFIG_PCI
704 705
static int __devinit moxa_pci_probe(struct pci_dev *pdev,
		const struct pci_device_id *ent)
L
Linus Torvalds 已提交
706
{
707 708 709 710 711 712
	struct moxa_board_conf *board;
	unsigned int i;
	int board_type = ent->driver_data;
	int retval;

	retval = pci_enable_device(pdev);
J
Jiri Slaby 已提交
713 714
	if (retval) {
		dev_err(&pdev->dev, "can't enable pci device\n");
715
		goto err;
J
Jiri Slaby 已提交
716
	}
717 718 719 720 721 722 723

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

	retval = -ENODEV;
	if (i >= MAX_BOARDS) {
J
Jiri Slaby 已提交
724
		dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
725 726 727 728 729
				"found. Board is ignored.\n", MAX_BOARDS);
		goto err;
	}

	board = &moxa_boards[i];
J
Jiri Slaby 已提交
730
	board->ports = &moxa_ports[i * MAX_PORTS_PER_BOARD];
J
Jiri Slaby 已提交
731 732 733 734 735 736 737 738

	retval = pci_request_region(pdev, 2, "moxa-base");
	if (retval) {
		dev_err(&pdev->dev, "can't request pci region 2\n");
		goto err;
	}

	board->basemem = ioremap(pci_resource_start(pdev, 2), 0x4000);
J
Jiri Slaby 已提交
739 740
	if (board->basemem == NULL) {
		dev_err(&pdev->dev, "can't remap io space 2\n");
J
Jiri Slaby 已提交
741
		goto err_reg;
J
Jiri Slaby 已提交
742
	}
743

L
Linus Torvalds 已提交
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
	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 已提交
759

J
Jiri Slaby 已提交
760 761 762 763
	retval = moxa_init_board(board, &pdev->dev);
	if (retval)
		goto err_base;

764
	pci_set_drvdata(pdev, board);
L
Linus Torvalds 已提交
765 766

	return (0);
J
Jiri Slaby 已提交
767 768 769
err_base:
	iounmap(board->basemem);
	board->basemem = NULL;
J
Jiri Slaby 已提交
770 771
err_reg:
	pci_release_region(pdev, 2);
772 773 774 775 776 777 778 779
err:
	return retval;
}

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

J
Jiri Slaby 已提交
780
	iounmap(brd->basemem);
781
	brd->basemem = NULL;
J
Jiri Slaby 已提交
782
	pci_release_region(pdev, 2);
L
Linus Torvalds 已提交
783
}
J
Jiri Slaby 已提交
784 785 786 787 788 789 790

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 已提交
791 792 793 794
#endif /* CONFIG_PCI */

static int __init moxa_init(void)
{
795
	struct moxa_port *ch;
796 797
	unsigned int i, isabrds = 0;
	int retval = 0;
L
Linus Torvalds 已提交
798

J
Jiri Slaby 已提交
799 800
	printk(KERN_INFO "MOXA Intellio family driver version %s\n",
			MOXA_VERSION);
L
Linus Torvalds 已提交
801 802 803 804 805
	moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
	if (!moxaDriver)
		return -ENOMEM;

	moxaDriver->owner = THIS_MODULE;
806
	moxaDriver->name = "ttyMX";
L
Linus Torvalds 已提交
807 808 809 810 811 812
	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 已提交
813 814
	moxaDriver->init_termios.c_ispeed = 9600;
	moxaDriver->init_termios.c_ospeed = 9600;
L
Linus Torvalds 已提交
815 816 817
	moxaDriver->flags = TTY_DRIVER_REAL_RAW;
	tty_set_operations(moxaDriver, &moxa_ops);

818
	for (i = 0, ch = moxa_ports; i < MAX_PORTS; i++, ch++) {
L
Linus Torvalds 已提交
819 820 821 822
		ch->type = PORT_16550A;
		ch->close_delay = 5 * HZ / 10;
		ch->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
		init_waitqueue_head(&ch->open_wait);
J
Jiri Slaby 已提交
823
		init_completion(&ch->close_wait);
824

J
Jiri Slaby 已提交
825
		setup_timer(&ch->emptyTimer, moxa_check_xmit_empty,
826
				(unsigned long)ch);
L
Linus Torvalds 已提交
827 828
	}

J
Jiri Slaby 已提交
829
	pr_debug("Moxa tty devices major number = %d\n", ttymajor);
L
Linus Torvalds 已提交
830 831 832 833 834 835 836

	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 已提交
837
	mod_timer(&moxaTimer, jiffies + HZ / 50);
L
Linus Torvalds 已提交
838

839
	/* Find the boards defined from module args. */
L
Linus Torvalds 已提交
840
#ifdef MODULE
841 842
	{
	struct moxa_board_conf *brd = moxa_boards;
L
Linus Torvalds 已提交
843
	for (i = 0; i < MAX_BOARDS; i++) {
844 845 846 847
		if (!baseaddr[i])
			break;
		if (type[i] == MOXA_BOARD_C218_ISA ||
				type[i] == MOXA_BOARD_C320_ISA) {
J
Jiri Slaby 已提交
848
			pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
849 850 851
					isabrds + 1, moxa_brdname[type[i] - 1],
					baseaddr[i]);
			brd->boardType = type[i];
J
Jiri Slaby 已提交
852
			brd->ports = &moxa_ports[isabrds * MAX_PORTS_PER_BOARD];
853 854 855 856 857 858 859
			brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
					numports[i];
			brd->busType = MOXA_BUS_TYPE_ISA;
			brd->basemem = ioremap(baseaddr[i], 0x4000);
			if (!brd->basemem) {
				printk(KERN_ERR "moxa: can't remap %lx\n",
						baseaddr[i]);
L
Linus Torvalds 已提交
860 861
				continue;
			}
J
Jiri Slaby 已提交
862 863 864 865 866
			if (moxa_init_board(brd, NULL)) {
				iounmap(brd->basemem);
				brd->basemem = NULL;
				continue;
			}
867 868 869

			brd++;
			isabrds++;
L
Linus Torvalds 已提交
870 871
		}
	}
872
	}
L
Linus Torvalds 已提交
873
#endif
J
Jiri Slaby 已提交
874

L
Linus Torvalds 已提交
875
#ifdef CONFIG_PCI
J
Jiri Slaby 已提交
876 877 878
	retval = pci_register_driver(&moxa_pci_driver);
	if (retval) {
		printk(KERN_ERR "Can't register moxa pci driver!\n");
879
		if (isabrds)
J
Jiri Slaby 已提交
880
			retval = 0;
L
Linus Torvalds 已提交
881 882
	}
#endif
J
Jiri Slaby 已提交
883 884

	return retval;
L
Linus Torvalds 已提交
885 886 887 888 889 890
}

static void __exit moxa_exit(void)
{
	int i;

891
	del_timer_sync(&moxaTimer);
L
Linus Torvalds 已提交
892 893

	for (i = 0; i < MAX_PORTS; i++)
894
		del_timer_sync(&moxa_ports[i].emptyTimer);
L
Linus Torvalds 已提交
895 896

	if (tty_unregister_driver(moxaDriver))
J
Jiri Slaby 已提交
897 898
		printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
				"serial driver\n");
L
Linus Torvalds 已提交
899
	put_tty_driver(moxaDriver);
900

901
#ifdef CONFIG_PCI
J
Jiri Slaby 已提交
902
	pci_unregister_driver(&moxa_pci_driver);
903
#endif
J
Jiri Slaby 已提交
904 905

	for (i = 0; i < MAX_BOARDS; i++)
906 907
		if (moxa_boards[i].basemem)
			iounmap(moxa_boards[i].basemem);
L
Linus Torvalds 已提交
908 909 910 911 912 913 914
}

module_init(moxa_init);
module_exit(moxa_exit);

static int moxa_open(struct tty_struct *tty, struct file *filp)
{
915
	struct moxa_port *ch;
L
Linus Torvalds 已提交
916 917 918
	int port;
	int retval;

J
Jiri Slaby 已提交
919
	port = tty->index;
L
Linus Torvalds 已提交
920 921 922 923 924 925 926 927
	if (port == MAX_PORTS) {
		return (0);
	}
	if (!MoxaPortIsValid(port)) {
		tty->driver_data = NULL;
		return (-ENODEV);
	}

928
	ch = &moxa_ports[port];
L
Linus Torvalds 已提交
929 930 931 932 933
	ch->count++;
	tty->driver_data = ch;
	ch->tty = tty;
	if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
		ch->statusflags = 0;
A
Alan Cox 已提交
934
		moxa_set_tty_param(tty, tty->termios);
J
Jiri Slaby 已提交
935 936
		MoxaPortLineCtrl(ch, 1, 1);
		MoxaPortEnable(ch);
L
Linus Torvalds 已提交
937 938
		ch->asyncflags |= ASYNC_INITIALIZED;
	}
J
Jiri Slaby 已提交
939
	retval = moxa_block_till_ready(tty, filp, ch);
L
Linus Torvalds 已提交
940 941 942 943

	moxa_unthrottle(tty);

	if (ch->type == PORT_16550A) {
J
Jiri Slaby 已提交
944
		MoxaSetFifo(ch, 1);
L
Linus Torvalds 已提交
945
	} else {
J
Jiri Slaby 已提交
946
		MoxaSetFifo(ch, 0);
L
Linus Torvalds 已提交
947 948 949 950 951 952 953
	}

	return (retval);
}

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

J
Jiri Slaby 已提交
957
	port = tty->index;
L
Linus Torvalds 已提交
958 959 960 961
	if (port == MAX_PORTS) {
		return;
	}
	if (!MoxaPortIsValid(port)) {
J
Jiri Slaby 已提交
962
		pr_debug("Invalid portno in moxa_close\n");
L
Linus Torvalds 已提交
963 964 965 966 967 968 969 970 971
		tty->driver_data = NULL;
		return;
	}
	if (tty->driver_data == NULL) {
		return;
	}
	if (tty_hung_up_p(filp)) {
		return;
	}
972
	ch = (struct moxa_port *) tty->driver_data;
L
Linus Torvalds 已提交
973 974

	if ((tty->count == 1) && (ch->count != 1)) {
J
Jiri Slaby 已提交
975 976
		printk(KERN_WARNING "moxa_close: bad serial port count; "
			"tty->count is 1, ch->count is %d\n", ch->count);
L
Linus Torvalds 已提交
977 978 979
		ch->count = 1;
	}
	if (--ch->count < 0) {
J
Jiri Slaby 已提交
980 981
		printk(KERN_WARNING "moxa_close: bad serial port count, "
			"device=%s\n", tty->name);
L
Linus Torvalds 已提交
982 983 984 985 986 987 988 989 990
		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 已提交
991
		moxa_setup_empty_event(tty);
L
Linus Torvalds 已提交
992
		tty_wait_until_sent(tty, 30 * HZ);	/* 30 seconds timeout */
J
Jiri Slaby 已提交
993
		del_timer_sync(&ch->emptyTimer);
L
Linus Torvalds 已提交
994
	}
J
Jiri Slaby 已提交
995
	moxa_shut_down(ch);
J
Jiri Slaby 已提交
996
	MoxaPortFlushData(ch, 2);
L
Linus Torvalds 已提交
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010

	if (tty->driver->flush_buffer)
		tty->driver->flush_buffer(tty);
	tty_ldisc_flush(tty);
			
	tty->closing = 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 已提交
1011
	complete_all(&ch->close_wait);
L
Linus Torvalds 已提交
1012 1013 1014 1015 1016
}

static int moxa_write(struct tty_struct *tty,
		      const unsigned char *buf, int count)
{
J
Jiri Slaby 已提交
1017
	struct moxa_port *ch = tty->driver_data;
L
Linus Torvalds 已提交
1018
	unsigned long flags;
J
Jiri Slaby 已提交
1019
	int len;
L
Linus Torvalds 已提交
1020 1021

	if (ch == NULL)
J
Jiri Slaby 已提交
1022
		return 0;
A
Alan Cox 已提交
1023 1024

	spin_lock_irqsave(&moxa_lock, flags);
J
Jiri Slaby 已提交
1025
	len = MoxaPortWriteData(ch, (unsigned char *) buf, count);
A
Alan Cox 已提交
1026
	spin_unlock_irqrestore(&moxa_lock, flags);
L
Linus Torvalds 已提交
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037

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

static int moxa_write_room(struct tty_struct *tty)
{
1038
	struct moxa_port *ch;
L
Linus Torvalds 已提交
1039 1040 1041

	if (tty->stopped)
		return (0);
J
Jiri Slaby 已提交
1042
	ch = tty->driver_data;
L
Linus Torvalds 已提交
1043 1044
	if (ch == NULL)
		return (0);
J
Jiri Slaby 已提交
1045
	return MoxaPortTxFree(ch);
L
Linus Torvalds 已提交
1046 1047 1048 1049
}

static void moxa_flush_buffer(struct tty_struct *tty)
{
J
Jiri Slaby 已提交
1050
	struct moxa_port *ch = tty->driver_data;
L
Linus Torvalds 已提交
1051 1052 1053

	if (ch == NULL)
		return;
J
Jiri Slaby 已提交
1054
	MoxaPortFlushData(ch, 1);
L
Linus Torvalds 已提交
1055 1056 1057 1058 1059
	tty_wakeup(tty);
}

static int moxa_chars_in_buffer(struct tty_struct *tty)
{
J
Jiri Slaby 已提交
1060
	struct moxa_port *ch = tty->driver_data;
L
Linus Torvalds 已提交
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
	int chars;

	/*
	 * 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);
J
Jiri Slaby 已提交
1071
	chars = MoxaPortTxQueue(ch);
L
Linus Torvalds 已提交
1072 1073 1074 1075 1076 1077
	if (chars) {
		/*
		 * Make it possible to wakeup anything waiting for output
		 * in tty_ioctl.c, etc.
		 */
		if (!(ch->statusflags & EMPTYWAIT))
J
Jiri Slaby 已提交
1078
			moxa_setup_empty_event(tty);
L
Linus Torvalds 已提交
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
	}
	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)
{
J
Jiri Slaby 已提交
1093
	struct moxa_port *ch = tty->driver_data;
L
Linus Torvalds 已提交
1094 1095 1096 1097
	unsigned long flags;

	if (ch == NULL)
		return;
A
Alan Cox 已提交
1098
	spin_lock_irqsave(&moxa_lock, flags);
J
Jiri Slaby 已提交
1099
	MoxaPortWriteData(ch, &c, 1);
A
Alan Cox 已提交
1100
	spin_unlock_irqrestore(&moxa_lock, flags);
L
Linus Torvalds 已提交
1101 1102 1103 1104 1105 1106 1107 1108
	/************************************************
	if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
	*************************************************/
	ch->statusflags |= LOWWAIT;
}

static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
{
J
Jiri Slaby 已提交
1109
	struct moxa_port *ch = tty->driver_data;
L
Linus Torvalds 已提交
1110 1111
	int flag = 0, dtr, rts;

J
Jiri Slaby 已提交
1112
	if ((tty->index != MAX_PORTS) && (!ch))
L
Linus Torvalds 已提交
1113 1114
		return (-EINVAL);

J
Jiri Slaby 已提交
1115
	MoxaPortGetLineOut(ch, &dtr, &rts);
L
Linus Torvalds 已提交
1116 1117 1118 1119
	if (dtr)
		flag |= TIOCM_DTR;
	if (rts)
		flag |= TIOCM_RTS;
J
Jiri Slaby 已提交
1120
	dtr = MoxaPortLineStatus(ch);
L
Linus Torvalds 已提交
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
	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)
{
J
Jiri Slaby 已提交
1133
	struct moxa_port *ch = tty->driver_data;
L
Linus Torvalds 已提交
1134 1135 1136
	int port;
	int dtr, rts;

J
Jiri Slaby 已提交
1137
	port = tty->index;
L
Linus Torvalds 已提交
1138 1139 1140
	if ((port != MAX_PORTS) && (!ch))
		return (-EINVAL);

J
Jiri Slaby 已提交
1141
	MoxaPortGetLineOut(ch, &dtr, &rts);
L
Linus Torvalds 已提交
1142 1143 1144 1145 1146 1147 1148 1149
	if (set & TIOCM_RTS)
		rts = 1;
	if (set & TIOCM_DTR)
		dtr = 1;
	if (clear & TIOCM_RTS)
		rts = 0;
	if (clear & TIOCM_DTR)
		dtr = 0;
J
Jiri Slaby 已提交
1150
	MoxaPortLineCtrl(ch, dtr, rts);
L
Linus Torvalds 已提交
1151 1152 1153 1154 1155 1156
	return 0;
}

static int moxa_ioctl(struct tty_struct *tty, struct file *file,
		      unsigned int cmd, unsigned long arg)
{
J
Jiri Slaby 已提交
1157
	struct moxa_port *ch = tty->driver_data;
L
Linus Torvalds 已提交
1158 1159 1160 1161
	register int port;
	void __user *argp = (void __user *)arg;
	int retval;

J
Jiri Slaby 已提交
1162
	port = tty->index;
L
Linus Torvalds 已提交
1163 1164 1165 1166 1167 1168 1169 1170
	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 已提交
1171
		moxa_setup_empty_event(tty);
L
Linus Torvalds 已提交
1172 1173
		tty_wait_until_sent(tty, 0);
		if (!arg)
J
Jiri Slaby 已提交
1174
			MoxaPortSendBreak(ch, 0);
L
Linus Torvalds 已提交
1175 1176 1177 1178 1179
		return (0);
	case TCSBRKP:		/* support for POSIX tcsendbreak() */
		retval = tty_check_change(tty);
		if (retval)
			return (retval);
J
Jiri Slaby 已提交
1180
		moxa_setup_empty_event(tty);
L
Linus Torvalds 已提交
1181
		tty_wait_until_sent(tty, 0);
J
Jiri Slaby 已提交
1182
		MoxaPortSendBreak(ch, arg);
L
Linus Torvalds 已提交
1183 1184
		return (0);
	case TIOCGSOFTCAR:
1185
		return put_user(C_CLOCAL(tty) ? 1 : 0, (int __user *)argp);
L
Linus Torvalds 已提交
1186
	case TIOCSSOFTCAR:
1187
		if (get_user(retval, (int __user *)argp))
L
Linus Torvalds 已提交
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
			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:
J
Jiri Slaby 已提交
1203
		retval = MoxaDriverIoctl(tty, cmd, arg);
L
Linus Torvalds 已提交
1204 1205 1206 1207 1208 1209
	}
	return (retval);
}

static void moxa_throttle(struct tty_struct *tty)
{
1210
	struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
L
Linus Torvalds 已提交
1211 1212 1213 1214 1215 1216

	ch->statusflags |= THROTTLE;
}

static void moxa_unthrottle(struct tty_struct *tty)
{
1217
	struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
L
Linus Torvalds 已提交
1218 1219 1220 1221 1222

	ch->statusflags &= ~THROTTLE;
}

static void moxa_set_termios(struct tty_struct *tty,
A
Alan Cox 已提交
1223
			     struct ktermios *old_termios)
L
Linus Torvalds 已提交
1224
{
1225
	struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
L
Linus Torvalds 已提交
1226 1227 1228

	if (ch == NULL)
		return;
A
Alan Cox 已提交
1229
	moxa_set_tty_param(tty, old_termios);
L
Linus Torvalds 已提交
1230 1231 1232 1233 1234 1235 1236
	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)
{
1237
	struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
L
Linus Torvalds 已提交
1238 1239 1240

	if (ch == NULL)
		return;
J
Jiri Slaby 已提交
1241
	MoxaPortTxDisable(ch);
L
Linus Torvalds 已提交
1242 1243 1244 1245 1246 1247
	ch->statusflags |= TXSTOPPED;
}


static void moxa_start(struct tty_struct *tty)
{
1248
	struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
L
Linus Torvalds 已提交
1249 1250 1251 1252 1253 1254 1255

	if (ch == NULL)
		return;

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

J
Jiri Slaby 已提交
1256
	MoxaPortTxEnable(ch);
L
Linus Torvalds 已提交
1257 1258 1259 1260 1261
	ch->statusflags &= ~TXSTOPPED;
}

static void moxa_hangup(struct tty_struct *tty)
{
1262
	struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
L
Linus Torvalds 已提交
1263 1264

	moxa_flush_buffer(tty);
J
Jiri Slaby 已提交
1265
	moxa_shut_down(ch);
L
Linus Torvalds 已提交
1266 1267 1268 1269 1270 1271 1272 1273 1274
	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;
1275
	struct moxa_port *ch;
L
Linus Torvalds 已提交
1276 1277 1278 1279 1280 1281
	struct tty_struct *tp;
	int i, ports;

	del_timer(&moxaTimer);

	if (MoxaDriverPoll() < 0) {
J
Jiri Slaby 已提交
1282
		mod_timer(&moxaTimer, jiffies + HZ / 50);
L
Linus Torvalds 已提交
1283 1284 1285 1286 1287
		return;
	}
	for (card = 0; card < MAX_BOARDS; card++) {
		if ((ports = MoxaPortsOfCard(card)) <= 0)
			continue;
1288
		ch = &moxa_ports[card * MAX_PORTS_PER_BOARD];
L
Linus Torvalds 已提交
1289 1290 1291 1292
		for (i = 0; i < ports; i++, ch++) {
			if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
				continue;
			if (!(ch->statusflags & THROTTLE) &&
J
Jiri Slaby 已提交
1293
			    (MoxaPortRxQueue(ch) > 0))
J
Jiri Slaby 已提交
1294
				moxa_receive_data(ch);
L
Linus Torvalds 已提交
1295 1296 1297
			if ((tp = ch->tty) == 0)
				continue;
			if (ch->statusflags & LOWWAIT) {
J
Jiri Slaby 已提交
1298
				if (MoxaPortTxQueue(ch) <= WAKEUP_CHARS) {
L
Linus Torvalds 已提交
1299 1300 1301 1302 1303 1304
					if (!tp->stopped) {
						ch->statusflags &= ~LOWWAIT;
						tty_wakeup(tp);
					}
				}
			}
J
Jiri Slaby 已提交
1305
			if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch) > 0)) {
L
Linus Torvalds 已提交
1306 1307 1308
				tty_insert_flip_char(tp, 0, TTY_BREAK);
				tty_schedule_flip(tp);
			}
J
Jiri Slaby 已提交
1309
			if (MoxaPortDCDChange(ch)) {
L
Linus Torvalds 已提交
1310
				if (ch->asyncflags & ASYNC_CHECK_CD) {
J
Jiri Slaby 已提交
1311
					if (MoxaPortDCDON(ch))
L
Linus Torvalds 已提交
1312 1313
						wake_up_interruptible(&ch->open_wait);
					else {
1314 1315 1316
						tty_hangup(tp);
						wake_up_interruptible(&ch->open_wait);
						ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
L
Linus Torvalds 已提交
1317 1318 1319 1320 1321 1322
					}
				}
			}
		}
	}

J
Jiri Slaby 已提交
1323
	mod_timer(&moxaTimer, jiffies + HZ / 50);
L
Linus Torvalds 已提交
1324 1325 1326 1327
}

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

A
Alan Cox 已提交
1328
static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
L
Linus Torvalds 已提交
1329
{
A
Alan Cox 已提交
1330
	register struct ktermios *ts;
1331
	struct moxa_port *ch;
A
Alan Cox 已提交
1332
	int rts, cts, txflow, rxflow, xany, baud;
L
Linus Torvalds 已提交
1333

1334
	ch = (struct moxa_port *) tty->driver_data;
L
Linus Torvalds 已提交
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
	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 已提交
1349 1350 1351

	/* Clear the features we don't support */
	ts->c_cflag &= ~CMSPAR;
J
Jiri Slaby 已提交
1352 1353
	MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
	baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
A
Alan Cox 已提交
1354 1355 1356 1357
	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 已提交
1358 1359
}

J
Jiri Slaby 已提交
1360
static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
1361
			    struct moxa_port *ch)
L
Linus Torvalds 已提交
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
{
	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 已提交
1374
			wait_for_completion_interruptible(&ch->close_wait);
L
Linus Torvalds 已提交
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
#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 已提交
1397
	pr_debug("block_til_ready before block: ttys%d, count = %d\n",
J
Jiri Slaby 已提交
1398
		tty->index, ch->count);
A
Alan Cox 已提交
1399
	spin_lock_irqsave(&moxa_lock, flags);
L
Linus Torvalds 已提交
1400 1401 1402
	if (!tty_hung_up_p(filp))
		ch->count--;
	ch->blocked_open++;
A
Alan Cox 已提交
1403 1404
	spin_unlock_irqrestore(&moxa_lock, flags);

L
Linus Torvalds 已提交
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
	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 ||
J
Jiri Slaby 已提交
1420
						MoxaPortDCDON(ch)))
L
Linus Torvalds 已提交
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
			break;

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

	spin_lock_irqsave(&moxa_lock, flags);
L
Linus Torvalds 已提交
1433 1434 1435
	if (!tty_hung_up_p(filp))
		ch->count++;
	ch->blocked_open--;
A
Alan Cox 已提交
1436
	spin_unlock_irqrestore(&moxa_lock, flags);
J
Jiri Slaby 已提交
1437
	pr_debug("block_til_ready after blocking: ttys%d, count = %d\n",
J
Jiri Slaby 已提交
1438
		tty->index, ch->count);
L
Linus Torvalds 已提交
1439 1440
	if (retval)
		return (retval);
A
Alan Cox 已提交
1441
	/* FIXME: review to see if we need to use set_bit on these */
L
Linus Torvalds 已提交
1442
	ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
A
Alan Cox 已提交
1443
	return 0;
L
Linus Torvalds 已提交
1444 1445
}

J
Jiri Slaby 已提交
1446
static void moxa_setup_empty_event(struct tty_struct *tty)
L
Linus Torvalds 已提交
1447
{
1448
	struct moxa_port *ch = tty->driver_data;
L
Linus Torvalds 已提交
1449 1450
	unsigned long flags;

A
Alan Cox 已提交
1451
	spin_lock_irqsave(&moxa_lock, flags);
L
Linus Torvalds 已提交
1452
	ch->statusflags |= EMPTYWAIT;
J
Jiri Slaby 已提交
1453
	mod_timer(&ch->emptyTimer, jiffies + HZ);
A
Alan Cox 已提交
1454
	spin_unlock_irqrestore(&moxa_lock, flags);
L
Linus Torvalds 已提交
1455 1456
}

J
Jiri Slaby 已提交
1457
static void moxa_check_xmit_empty(unsigned long data)
L
Linus Torvalds 已提交
1458
{
1459
	struct moxa_port *ch;
L
Linus Torvalds 已提交
1460

1461
	ch = (struct moxa_port *) data;
L
Linus Torvalds 已提交
1462
	if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
J
Jiri Slaby 已提交
1463
		if (MoxaPortTxQueue(ch) == 0) {
L
Linus Torvalds 已提交
1464 1465 1466 1467
			ch->statusflags &= ~EMPTYWAIT;
			tty_wakeup(ch->tty);
			return;
		}
J
Jiri Slaby 已提交
1468
		mod_timer(&ch->emptyTimer, round_jiffies(jiffies + HZ));
L
Linus Torvalds 已提交
1469 1470 1471 1472
	} else
		ch->statusflags &= ~EMPTYWAIT;
}

J
Jiri Slaby 已提交
1473
static void moxa_shut_down(struct moxa_port *ch)
L
Linus Torvalds 已提交
1474 1475 1476 1477 1478 1479 1480 1481
{
	struct tty_struct *tp;

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

	tp = ch->tty;

J
Jiri Slaby 已提交
1482
	MoxaPortDisable(ch);
L
Linus Torvalds 已提交
1483 1484 1485 1486 1487

	/*
	 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
	 */
	if (tp->termios->c_cflag & HUPCL)
J
Jiri Slaby 已提交
1488
		MoxaPortLineCtrl(ch, 0, 0);
L
Linus Torvalds 已提交
1489 1490 1491 1492

	ch->asyncflags &= ~ASYNC_INITIALIZED;
}

J
Jiri Slaby 已提交
1493
static void moxa_receive_data(struct moxa_port *ch)
L
Linus Torvalds 已提交
1494 1495
{
	struct tty_struct *tp;
A
Alan Cox 已提交
1496
	struct ktermios *ts;
L
Linus Torvalds 已提交
1497 1498 1499 1500 1501 1502 1503
	unsigned long flags;

	ts = NULL;
	tp = ch->tty;
	if (tp)
		ts = tp->termios;
	/**************************************************
J
Jiri Slaby 已提交
1504 1505 1506
	if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
	*****************************************************/
	if (!tp || !ts) {
J
Jiri Slaby 已提交
1507
		MoxaPortFlushData(ch, 0);
J
Jiri Slaby 已提交
1508 1509 1510
		return;
	}
	spin_lock_irqsave(&moxa_lock, flags);
J
Jiri Slaby 已提交
1511
	MoxaPortReadData(ch, tp);
J
Jiri Slaby 已提交
1512 1513 1514
	spin_unlock_irqrestore(&moxa_lock, flags);
	tty_schedule_flip(tp);
}
L
Linus Torvalds 已提交
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529

/*
 *    Query
 */

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

#define 	DCD_changed	0x01
#define 	DCD_oldstate	0x80

static int moxaLowWaterChk;
1530
static struct mon_str moxaLog;
1531
static int moxaFuncTout = HZ / 2;
L
Linus Torvalds 已提交
1532 1533

static void moxafunc(void __iomem *, int, ushort);
J
Jiri Slaby 已提交
1534 1535
static void moxa_wait_finish(void __iomem *);
static void moxa_low_water_check(void __iomem *);
L
Linus Torvalds 已提交
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552

/*****************************************************************************
 *	Driver level functions: 					     *
 *	2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);   *
 *	3. MoxaDriverPoll(void);					     *
 *****************************************************************************/
#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_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)

J
Jiri Slaby 已提交
1553
static void MoxaPortFlushData(struct moxa_port *port, int mode)
L
Linus Torvalds 已提交
1554 1555 1556 1557
{
	void __iomem *ofsAddr;
	if ((mode < 0) || (mode > 2))
		return;
J
Jiri Slaby 已提交
1558
	ofsAddr = port->tableAddr;
L
Linus Torvalds 已提交
1559 1560
	moxafunc(ofsAddr, FC_FlushQueue, mode);
	if (mode != 1) {
J
Jiri Slaby 已提交
1561
		port->lowChkFlag = 0;
J
Jiri Slaby 已提交
1562
		moxa_low_water_check(ofsAddr);
L
Linus Torvalds 已提交
1563 1564 1565
	}
}

J
Jiri Slaby 已提交
1566 1567
static int MoxaDriverIoctl(struct tty_struct *tty, unsigned int cmd,
		unsigned long arg)
L
Linus Torvalds 已提交
1568
{
J
Jiri Slaby 已提交
1569
	struct moxa_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1570 1571 1572 1573
	int i;
	int status;
	void __user *argp = (void __user *)arg;

J
Jiri Slaby 已提交
1574
	if (tty->index == MAX_PORTS) {
J
Jiri Slaby 已提交
1575 1576
		if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_GETDATACOUNT) &&
		    (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
L
Linus Torvalds 已提交
1577 1578 1579 1580 1581 1582
		    (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
			return (-EINVAL);
	}
	switch (cmd) {
	case MOXA_GETDATACOUNT:
		moxaLog.tick = jiffies;
1583
		if(copy_to_user(argp, &moxaLog, sizeof(struct mon_str)))
L
Linus Torvalds 已提交
1584 1585 1586 1587 1588
			return -EFAULT;
		return (0);
	case MOXA_FLUSH_QUEUE:
		MoxaPortFlushData(port, arg);
		return (0);
1589 1590
	case MOXA_GET_IOQUEUE: {
		struct moxaq_str __user *argm = argp;
1591
		struct moxaq_str tmp;
1592 1593

		for (i = 0; i < MAX_PORTS; i++, argm++) {
1594 1595
			memset(&tmp, 0, sizeof(tmp));
			if (moxa_ports[i].chkPort) {
J
Jiri Slaby 已提交
1596 1597
				tmp.inq = MoxaPortRxQueue(&moxa_ports[i]);
				tmp.outq = MoxaPortTxQueue(&moxa_ports[i]);
L
Linus Torvalds 已提交
1598
			}
1599
			if (copy_to_user(argm, &tmp, sizeof(tmp)))
1600
				return -EFAULT;
L
Linus Torvalds 已提交
1601 1602
		}
		return (0);
1603
	} case MOXA_GET_OQUEUE:
L
Linus Torvalds 已提交
1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
		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;
1618 1619
	case MOXA_GETMSTATUS: {
		struct mxser_mstatus __user *argm = argp;
1620
		struct mxser_mstatus tmp;
1621 1622 1623 1624
		struct moxa_port *p;

		for (i = 0; i < MAX_PORTS; i++, argm++) {
			p = &moxa_ports[i];
1625
			memset(&tmp, 0, sizeof(tmp));
1626 1627
			if (!p->chkPort) {
				goto copy;
L
Linus Torvalds 已提交
1628
			} else {
J
Jiri Slaby 已提交
1629
				status = MoxaPortLineStatus(p);
L
Linus Torvalds 已提交
1630
				if (status & 1)
1631
					tmp.cts = 1;
L
Linus Torvalds 已提交
1632
				if (status & 2)
1633
					tmp.dsr = 1;
L
Linus Torvalds 已提交
1634
				if (status & 4)
1635
					tmp.dcd = 1;
L
Linus Torvalds 已提交
1636 1637
			}

1638
			if (!p->tty || !p->tty->termios)
1639
				tmp.cflag = p->cflag;
L
Linus Torvalds 已提交
1640
			else
1641
				tmp.cflag = p->tty->termios->c_cflag;
1642
copy:
1643
			if (copy_to_user(argm, &tmp, sizeof(tmp)))
1644
				return -EFAULT;
L
Linus Torvalds 已提交
1645 1646 1647 1648
		}
		return 0;
	}
	}
J
Jiri Slaby 已提交
1649 1650

	return -ENOIOCTLCMD;
L
Linus Torvalds 已提交
1651 1652 1653 1654
}

int MoxaDriverPoll(void)
{
1655
	struct moxa_board_conf *brd;
L
Linus Torvalds 已提交
1656 1657 1658 1659 1660 1661 1662 1663 1664
	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++) {
1665 1666
		brd = &moxa_boards[card];
	        if (brd->loadstat == 0)
1667
			continue;
1668
		if ((ports = brd->numPorts) == 0)
L
Linus Torvalds 已提交
1669
			continue;
1670 1671
		if (readb(brd->intPend) == 0xff) {
			ip = brd->intTable + readb(brd->intNdx);
L
Linus Torvalds 已提交
1672 1673 1674 1675 1676
			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);
1677
					ofsAddr = moxa_ports[p].tableAddr;
L
Linus Torvalds 已提交
1678 1679 1680
					if (temp & IntrTx)
						writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
					if (temp & IntrBreak) {
1681
						moxa_ports[p].breakCnt++;
L
Linus Torvalds 已提交
1682 1683 1684
					}
					if (temp & IntrLine) {
						if (readb(ofsAddr + FlagStat) & DCD_state) {
1685 1686
							if ((moxa_ports[p].DCDState & DCD_oldstate) == 0)
								moxa_ports[p].DCDState = (DCD_oldstate |
L
Linus Torvalds 已提交
1687 1688
										   DCD_changed);
						} else {
1689 1690
							if (moxa_ports[p].DCDState & DCD_oldstate)
								moxa_ports[p].DCDState = DCD_changed;
L
Linus Torvalds 已提交
1691 1692 1693 1694
						}
					}
				}
			}
1695
			writeb(0, brd->intPend);
L
Linus Torvalds 已提交
1696 1697 1698 1699
		}
		if (moxaLowWaterChk) {
			p = card * MAX_PORTS_PER_BOARD;
			for (port = 0; port < ports; port++, p++) {
1700 1701 1702
				if (moxa_ports[p].lowChkFlag) {
					moxa_ports[p].lowChkFlag = 0;
					ofsAddr = moxa_ports[p].tableAddr;
J
Jiri Slaby 已提交
1703
					moxa_low_water_check(ofsAddr);
L
Linus Torvalds 已提交
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
				}
			}
		}
	}
	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 已提交
1740
 *	17. MoxaPortReadData(int port, struct tty_struct *tty); 	     *
L
Linus Torvalds 已提交
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 1769 1770 1771 1772 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
 *	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 已提交
1848
 *      int  MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
L
Linus Torvalds 已提交
1849
 *           int port           : port number (0 - 127)
A
Alan Cox 已提交
1850
 *           struct ktermios * termio : termio structure pointer
1851
 *	     speed_t baud	: baud rate
L
Linus Torvalds 已提交
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 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 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942
 *
 *           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 已提交
1943
 *      int  MoxaPortReadData(int port, struct tty_struct *tty);
L
Linus Torvalds 已提交
1944
 *           int port           : port number (0 - 127)
A
Alan Cox 已提交
1945
 *	     struct tty_struct *tty : tty for data
L
Linus Torvalds 已提交
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 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 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002
 *
 *           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.
 *
 */
J
Jiri Slaby 已提交
2003
static int MoxaPortIsValid(int port)
L
Linus Torvalds 已提交
2004 2005 2006
{
	if (moxaCard == 0)
		return (0);
2007
	if (moxa_ports[port].chkPort == 0)
L
Linus Torvalds 已提交
2008 2009 2010 2011
		return (0);
	return (1);
}

J
Jiri Slaby 已提交
2012
static void MoxaPortEnable(struct moxa_port *port)
L
Linus Torvalds 已提交
2013 2014 2015 2016
{
	void __iomem *ofsAddr;
	short lowwater = 512;

J
Jiri Slaby 已提交
2017
	ofsAddr = port->tableAddr;
L
Linus Torvalds 已提交
2018
	writew(lowwater, ofsAddr + Low_water);
J
Jiri Slaby 已提交
2019 2020 2021
	port->breakCnt = 0;
	if (port->board->boardType == MOXA_BOARD_C320_ISA ||
	    port->board->boardType == MOXA_BOARD_C320_PCI) {
L
Linus Torvalds 已提交
2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033
		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);
}

J
Jiri Slaby 已提交
2034
static void MoxaPortDisable(struct moxa_port *port)
L
Linus Torvalds 已提交
2035
{
J
Jiri Slaby 已提交
2036
	void __iomem *ofsAddr = port->tableAddr;
L
Linus Torvalds 已提交
2037 2038 2039 2040 2041 2042 2043

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

J
Jiri Slaby 已提交
2044
static long MoxaPortGetMaxBaud(struct moxa_port *port)
L
Linus Torvalds 已提交
2045
{
J
Jiri Slaby 已提交
2046 2047
	if (port->board->boardType == MOXA_BOARD_C320_ISA ||
			port->board->boardType == MOXA_BOARD_C320_PCI)
L
Linus Torvalds 已提交
2048 2049 2050 2051 2052 2053
		return (460800L);
	else
		return (921600L);
}


J
Jiri Slaby 已提交
2054
static long MoxaPortSetBaud(struct moxa_port *port, long baud)
L
Linus Torvalds 已提交
2055 2056 2057 2058 2059 2060 2061
{
	void __iomem *ofsAddr;
	long max, clock;
	unsigned int val;

	if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
		return (0);
J
Jiri Slaby 已提交
2062
	ofsAddr = port->tableAddr;
L
Linus Torvalds 已提交
2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076
	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;
	return (baud);
}

J
Jiri Slaby 已提交
2077 2078
static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
		speed_t baud)
L
Linus Torvalds 已提交
2079 2080 2081 2082 2083
{
	void __iomem *ofsAddr;
	tcflag_t cflag;
	tcflag_t mode = 0;

J
Jiri Slaby 已提交
2084
	if (port->chkPort == 0 || termio == 0)
L
Linus Torvalds 已提交
2085
		return (-1);
J
Jiri Slaby 已提交
2086
	ofsAddr = port->tableAddr;
L
Linus Torvalds 已提交
2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116
	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);

J
Jiri Slaby 已提交
2117 2118
	if (port->board->boardType == MOXA_BOARD_C320_ISA ||
			port->board->boardType == MOXA_BOARD_C320_PCI) {
2119
		if (baud >= 921600L)
L
Linus Torvalds 已提交
2120 2121
			return (-1);
	}
A
Alan Cox 已提交
2122
	baud = MoxaPortSetBaud(port, baud);
L
Linus Torvalds 已提交
2123 2124 2125 2126 2127

	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 已提交
2128
		moxa_wait_finish(ofsAddr);
L
Linus Torvalds 已提交
2129 2130

	}
A
Alan Cox 已提交
2131
	return (baud);
L
Linus Torvalds 已提交
2132 2133
}

J
Jiri Slaby 已提交
2134 2135
static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
		int *rtsState)
L
Linus Torvalds 已提交
2136 2137
{

J
Jiri Slaby 已提交
2138
	if (!MoxaPortIsValid(port->tty->index))
L
Linus Torvalds 已提交
2139
		return (-1);
J
Jiri Slaby 已提交
2140 2141 2142 2143 2144
	if (dtrState)
		*dtrState = !!(port->lineCtrl & DTR_ON);
	if (rtsState)
		*rtsState = !!(port->lineCtrl & RTS_ON);

L
Linus Torvalds 已提交
2145 2146 2147
	return (0);
}

J
Jiri Slaby 已提交
2148
static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
L
Linus Torvalds 已提交
2149
{
J
Jiri Slaby 已提交
2150
	int mode = 0;
L
Linus Torvalds 已提交
2151 2152 2153 2154 2155

	if (dtr)
		mode |= DTR_ON;
	if (rts)
		mode |= RTS_ON;
J
Jiri Slaby 已提交
2156 2157
	port->lineCtrl = mode;
	moxafunc(port->tableAddr, FC_LineControl, mode);
L
Linus Torvalds 已提交
2158 2159
}

J
Jiri Slaby 已提交
2160 2161
static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
		int txflow, int rxflow, int txany)
L
Linus Torvalds 已提交
2162
{
J
Jiri Slaby 已提交
2163
	int mode = 0;
L
Linus Torvalds 已提交
2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174

	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;
J
Jiri Slaby 已提交
2175
	moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
L
Linus Torvalds 已提交
2176 2177
}

J
Jiri Slaby 已提交
2178
static int MoxaPortLineStatus(struct moxa_port *port)
L
Linus Torvalds 已提交
2179 2180 2181 2182
{
	void __iomem *ofsAddr;
	int val;

J
Jiri Slaby 已提交
2183 2184 2185
	ofsAddr = port->tableAddr;
	if (port->board->boardType == MOXA_BOARD_C320_ISA ||
			port->board->boardType == MOXA_BOARD_C320_PCI) {
L
Linus Torvalds 已提交
2186 2187 2188 2189 2190 2191 2192 2193
		moxafunc(ofsAddr, FC_LineStatus, 0);
		val = readw(ofsAddr + FuncArg);
	} else {
		val = readw(ofsAddr + FlagStat) >> 4;
	}
	val &= 0x0B;
	if (val & 8) {
		val |= 4;
J
Jiri Slaby 已提交
2194 2195
		if ((port->DCDState & DCD_oldstate) == 0)
			port->DCDState = (DCD_oldstate | DCD_changed);
L
Linus Torvalds 已提交
2196
	} else {
J
Jiri Slaby 已提交
2197 2198
		if (port->DCDState & DCD_oldstate)
			port->DCDState = DCD_changed;
L
Linus Torvalds 已提交
2199 2200 2201 2202 2203
	}
	val &= 7;
	return (val);
}

J
Jiri Slaby 已提交
2204
static int MoxaPortDCDChange(struct moxa_port *port)
L
Linus Torvalds 已提交
2205 2206 2207
{
	int n;

J
Jiri Slaby 已提交
2208
	if (port->chkPort == 0)
L
Linus Torvalds 已提交
2209
		return (0);
J
Jiri Slaby 已提交
2210 2211
	n = port->DCDState;
	port->DCDState &= ~DCD_changed;
L
Linus Torvalds 已提交
2212 2213 2214 2215
	n &= DCD_changed;
	return (n);
}

J
Jiri Slaby 已提交
2216
static int MoxaPortDCDON(struct moxa_port *port)
L
Linus Torvalds 已提交
2217 2218 2219
{
	int n;

J
Jiri Slaby 已提交
2220
	if (port->chkPort == 0)
L
Linus Torvalds 已提交
2221
		return (0);
J
Jiri Slaby 已提交
2222
	if (port->DCDState & DCD_oldstate)
L
Linus Torvalds 已提交
2223 2224 2225 2226 2227 2228
		n = 1;
	else
		n = 0;
	return (n);
}

J
Jiri Slaby 已提交
2229 2230
static int MoxaPortWriteData(struct moxa_port *port, unsigned char *buffer,
		int len)
L
Linus Torvalds 已提交
2231 2232 2233 2234 2235 2236 2237 2238
{
	int c, total, i;
	ushort tail;
	int cnt;
	ushort head, tx_mask, spage, epage;
	ushort pageno, pageofs, bufhead;
	void __iomem *baseAddr, *ofsAddr, *ofs;

J
Jiri Slaby 已提交
2239 2240
	ofsAddr = port->tableAddr;
	baseAddr = port->board->basemem;
L
Linus Torvalds 已提交
2241 2242 2243 2244 2245 2246 2247 2248 2249
	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;
J
Jiri Slaby 已提交
2250
	moxaLog.txcnt[port->tty->index] += c;
L
Linus Torvalds 已提交
2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293
	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);
}

J
Jiri Slaby 已提交
2294
static int MoxaPortReadData(struct moxa_port *port, struct tty_struct *tty)
L
Linus Torvalds 已提交
2295 2296 2297 2298 2299 2300 2301
{
	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;

J
Jiri Slaby 已提交
2302 2303
	ofsAddr = port->tableAddr;
	baseAddr = port->board->basemem;
L
Linus Torvalds 已提交
2304 2305 2306 2307 2308 2309 2310 2311
	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 已提交
2312
		return 0;
L
Linus Torvalds 已提交
2313

A
Alan Cox 已提交
2314
	total = count;
L
Linus Torvalds 已提交
2315
	remain = count - total;
J
Jiri Slaby 已提交
2316
	moxaLog.rxcnt[port->tty->index] += total;
L
Linus Torvalds 已提交
2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328
	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 已提交
2329
				tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
L
Linus Torvalds 已提交
2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345
			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 已提交
2346
				tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
L
Linus Torvalds 已提交
2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357
			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;
J
Jiri Slaby 已提交
2358
		port->lowChkFlag = 1;
L
Linus Torvalds 已提交
2359 2360 2361 2362 2363
	}
	return (total);
}


J
Jiri Slaby 已提交
2364
static int MoxaPortTxQueue(struct moxa_port *port)
L
Linus Torvalds 已提交
2365
{
J
Jiri Slaby 已提交
2366
	void __iomem *ofsAddr = port->tableAddr;
L
Linus Torvalds 已提交
2367 2368 2369 2370 2371 2372 2373 2374 2375 2376
	ushort rptr, wptr, mask;
	int len;

	rptr = readw(ofsAddr + TXrptr);
	wptr = readw(ofsAddr + TXwptr);
	mask = readw(ofsAddr + TX_mask);
	len = (wptr - rptr) & mask;
	return (len);
}

J
Jiri Slaby 已提交
2377
static int MoxaPortTxFree(struct moxa_port *port)
L
Linus Torvalds 已提交
2378
{
J
Jiri Slaby 已提交
2379
	void __iomem *ofsAddr = port->tableAddr;
L
Linus Torvalds 已提交
2380 2381 2382 2383 2384 2385 2386 2387 2388 2389
	ushort rptr, wptr, mask;
	int len;

	rptr = readw(ofsAddr + TXrptr);
	wptr = readw(ofsAddr + TXwptr);
	mask = readw(ofsAddr + TX_mask);
	len = mask - ((wptr - rptr) & mask);
	return (len);
}

J
Jiri Slaby 已提交
2390
static int MoxaPortRxQueue(struct moxa_port *port)
L
Linus Torvalds 已提交
2391
{
J
Jiri Slaby 已提交
2392
	void __iomem *ofsAddr = port->tableAddr;
L
Linus Torvalds 已提交
2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403
	ushort rptr, wptr, mask;
	int len;

	rptr = readw(ofsAddr + RXrptr);
	wptr = readw(ofsAddr + RXwptr);
	mask = readw(ofsAddr + RX_mask);
	len = (wptr - rptr) & mask;
	return (len);
}


J
Jiri Slaby 已提交
2404
static void MoxaPortTxDisable(struct moxa_port *port)
L
Linus Torvalds 已提交
2405
{
J
Jiri Slaby 已提交
2406
	moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
L
Linus Torvalds 已提交
2407 2408
}

J
Jiri Slaby 已提交
2409
static void MoxaPortTxEnable(struct moxa_port *port)
L
Linus Torvalds 已提交
2410
{
J
Jiri Slaby 已提交
2411
	moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
L
Linus Torvalds 已提交
2412 2413 2414
}


J
Jiri Slaby 已提交
2415
static int MoxaPortResetBrkCnt(struct moxa_port *port)
L
Linus Torvalds 已提交
2416 2417
{
	ushort cnt;
J
Jiri Slaby 已提交
2418 2419
	cnt = port->breakCnt;
	port->breakCnt = 0;
L
Linus Torvalds 已提交
2420 2421 2422 2423
	return (cnt);
}


J
Jiri Slaby 已提交
2424
static void MoxaPortSendBreak(struct moxa_port *port, int ms100)
L
Linus Torvalds 已提交
2425
{
J
Jiri Slaby 已提交
2426
	void __iomem *ofsAddr = port->tableAddr;
L
Linus Torvalds 已提交
2427 2428 2429

	if (ms100) {
		moxafunc(ofsAddr, FC_SendBreak, Magic_code);
J
Jiri Slaby 已提交
2430
		msleep(ms100 * 10);
L
Linus Torvalds 已提交
2431 2432
	} else {
		moxafunc(ofsAddr, FC_SendBreak, Magic_code);
J
Jiri Slaby 已提交
2433
		msleep(250);
L
Linus Torvalds 已提交
2434 2435 2436 2437
	}
	moxafunc(ofsAddr, FC_StopBreak, Magic_code);
}

2438
static int moxa_get_serial_info(struct moxa_port *info,
L
Linus Torvalds 已提交
2439 2440 2441 2442 2443 2444
				struct serial_struct __user *retinfo)
{
	struct serial_struct tmp;

	memset(&tmp, 0, sizeof(tmp));
	tmp.type = info->type;
J
Jiri Slaby 已提交
2445
	tmp.line = info->tty->index;
L
Linus Torvalds 已提交
2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458
	tmp.port = 0;
	tmp.irq = 0;
	tmp.flags = info->asyncflags;
	tmp.baud_base = 921600;
	tmp.close_delay = info->close_delay;
	tmp.custom_divisor = 0;
	tmp.hub6 = 0;
	if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
		return -EFAULT;
	return (0);
}


2459
static int moxa_set_serial_info(struct moxa_port *info,
L
Linus Torvalds 已提交
2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485
				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;
	}

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

	if (new_serial.type == PORT_16550A) {
J
Jiri Slaby 已提交
2486
		MoxaSetFifo(info, 1);
L
Linus Torvalds 已提交
2487
	} else {
J
Jiri Slaby 已提交
2488
		MoxaSetFifo(info, 0);
L
Linus Torvalds 已提交
2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504
	}

	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 已提交
2505
	moxa_wait_finish(ofsAddr);
L
Linus Torvalds 已提交
2506 2507
}

J
Jiri Slaby 已提交
2508
static void moxa_wait_finish(void __iomem *ofsAddr)
L
Linus Torvalds 已提交
2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520
{
	unsigned long i, j;

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

J
Jiri Slaby 已提交
2521
static void moxa_low_water_check(void __iomem *ofsAddr)
L
Linus Torvalds 已提交
2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535
{
	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);
	}
}

J
Jiri Slaby 已提交
2536
static void MoxaSetFifo(struct moxa_port *port, int enable)
L
Linus Torvalds 已提交
2537
{
J
Jiri Slaby 已提交
2538
	void __iomem *ofsAddr = port->tableAddr;
L
Linus Torvalds 已提交
2539 2540 2541 2542 2543 2544 2545 2546 2547

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