moxa.c 53.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/*****************************************************************************/
/*
 *           moxa.c  -- MOXA Intellio family multiport serial driver.
 *
J
Jiri Slaby 已提交
5 6
 *      Copyright (C) 1999-2000  Moxa Technologies (support@moxa.com).
 *      Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com>
L
Linus Torvalds 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 *      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 已提交
29
#include <linux/firmware.h>
L
Linus Torvalds 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#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>

#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		"6.0k"
L
Linus Torvalds 已提交
54

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

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

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

63 64 65
#define MOXA_IS_320(brd) ((brd)->boardType == MOXA_BOARD_C320_ISA || \
		(brd)->boardType == MOXA_BOARD_C320_PCI)

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

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

J
Jiri Slaby 已提交
102 103
struct moxa_port;

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

109
	unsigned int ready;
110

J
Jiri Slaby 已提交
111 112
	struct moxa_port *ports;

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

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

132
struct moxa_port {
A
Alan Cox 已提交
133
	struct tty_port port;
J
Jiri Slaby 已提交
134
	struct moxa_board_conf *board;
J
Jiri Slaby 已提交
135 136
	void __iomem *tableAddr;

L
Linus Torvalds 已提交
137 138
	int type;
	int cflag;
J
Jiri Slaby 已提交
139
	unsigned long statusflags;
L
Linus Torvalds 已提交
140

J
Jiri Slaby 已提交
141 142 143
	u8 DCDState;
	u8 lineCtrl;
	u8 lowChkFlag;
144
};
L
Linus Torvalds 已提交
145

J
Jiri Slaby 已提交
146 147 148 149 150 151
struct mon_str {
	int tick;
	int rxcnt[MAX_PORTS];
	int txcnt[MAX_PORTS];
};

L
Linus Torvalds 已提交
152 153 154 155 156 157 158 159 160 161 162
/* 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 已提交
163 164
static struct mon_str moxaLog;
static unsigned int moxaFuncTout = HZ / 2;
J
Jiri Slaby 已提交
165
static unsigned int moxaLowWaterChk;
J
Jiri Slaby 已提交
166
static DEFINE_MUTEX(moxa_openlock);
L
Linus Torvalds 已提交
167 168
/* Variables for insmod */
#ifdef MODULE
169 170 171
static unsigned long baseaddr[MAX_BOARDS];
static unsigned int type[MAX_BOARDS];
static unsigned int numports[MAX_BOARDS];
L
Linus Torvalds 已提交
172 173 174 175 176 177
#endif

MODULE_AUTHOR("William Chen");
MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
MODULE_LICENSE("GPL");
#ifdef MODULE
178 179 180 181 182 183
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 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197
#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_throttle(struct tty_struct *);
static void moxa_unthrottle(struct tty_struct *);
A
Alan Cox 已提交
198
static void moxa_set_termios(struct tty_struct *, struct ktermios *);
L
Linus Torvalds 已提交
199 200 201 202 203 204 205
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 已提交
206
static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
J
Jiri Slaby 已提交
207 208
static void moxa_setup_empty_event(struct tty_struct *);
static void moxa_shut_down(struct moxa_port *);
L
Linus Torvalds 已提交
209 210 211
/*
 * moxa board interface functions:
 */
J
Jiri Slaby 已提交
212 213 214 215 216 217 218 219
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 void MoxaPortFlushData(struct moxa_port *, int);
J
Jiri Slaby 已提交
220
static int MoxaPortWriteData(struct moxa_port *, const unsigned char *, int);
J
Jiri Slaby 已提交
221
static int MoxaPortReadData(struct moxa_port *);
J
Jiri Slaby 已提交
222 223 224 225 226
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 *);
227 228
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 已提交
229
static void MoxaSetFifo(struct moxa_port *port, int enable);
L
Linus Torvalds 已提交
230

J
Jiri Slaby 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
/*
 * I/O functions
 */

static void moxa_wait_finish(void __iomem *ofsAddr)
{
	unsigned long end = jiffies + moxaFuncTout;

	while (readw(ofsAddr + FuncCode) != 0)
		if (time_after(jiffies, end))
			return;
	if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit())
		printk(KERN_WARNING "moxa function expired\n");
}

J
Jiri Slaby 已提交
246
static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
J
Jiri Slaby 已提交
247 248 249 250 251 252
{
	writew(arg, ofsAddr + FuncArg);
	writew(cmd, ofsAddr + FuncCode);
	moxa_wait_finish(ofsAddr);
}

J
Jiri Slaby 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266
static void moxa_low_water_check(void __iomem *ofsAddr)
{
	u16 rptr, wptr, mask, len;

	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 已提交
267 268 269 270 271 272 273 274 275
/*
 * TTY operations
 */

static int moxa_ioctl(struct tty_struct *tty, struct file *file,
		      unsigned int cmd, unsigned long arg)
{
	struct moxa_port *ch = tty->driver_data;
	void __user *argp = (void __user *)arg;
J
Jiri Slaby 已提交
276
	int status, ret = 0;
J
Jiri Slaby 已提交
277 278 279 280 281 282 283 284 285 286 287

	if (tty->index == MAX_PORTS) {
		if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
				cmd != MOXA_GETMSTATUS)
			return -EINVAL;
	} else if (!ch)
		return -ENODEV;

	switch (cmd) {
	case MOXA_GETDATACOUNT:
		moxaLog.tick = jiffies;
J
Jiri Slaby 已提交
288 289 290
		if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
			ret = -EFAULT;
		break;
J
Jiri Slaby 已提交
291 292
	case MOXA_FLUSH_QUEUE:
		MoxaPortFlushData(ch, arg);
J
Jiri Slaby 已提交
293
		break;
J
Jiri Slaby 已提交
294 295 296 297 298 299
	case MOXA_GET_IOQUEUE: {
		struct moxaq_str __user *argm = argp;
		struct moxaq_str tmp;
		struct moxa_port *p;
		unsigned int i, j;

J
Jiri Slaby 已提交
300
		mutex_lock(&moxa_openlock);
J
Jiri Slaby 已提交
301 302 303 304 305 306 307 308
		for (i = 0; i < MAX_BOARDS; i++) {
			p = moxa_boards[i].ports;
			for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
				memset(&tmp, 0, sizeof(tmp));
				if (moxa_boards[i].ready) {
					tmp.inq = MoxaPortRxQueue(p);
					tmp.outq = MoxaPortTxQueue(p);
				}
J
Jiri Slaby 已提交
309 310
				if (copy_to_user(argm, &tmp, sizeof(tmp))) {
					mutex_unlock(&moxa_openlock);
J
Jiri Slaby 已提交
311
					return -EFAULT;
J
Jiri Slaby 已提交
312
				}
J
Jiri Slaby 已提交
313 314
			}
		}
J
Jiri Slaby 已提交
315 316
		mutex_unlock(&moxa_openlock);
		break;
J
Jiri Slaby 已提交
317 318
	} case MOXA_GET_OQUEUE:
		status = MoxaPortTxQueue(ch);
J
Jiri Slaby 已提交
319 320
		ret = put_user(status, (unsigned long __user *)argp);
		break;
J
Jiri Slaby 已提交
321 322
	case MOXA_GET_IQUEUE:
		status = MoxaPortRxQueue(ch);
J
Jiri Slaby 已提交
323 324
		ret = put_user(status, (unsigned long __user *)argp);
		break;
J
Jiri Slaby 已提交
325 326 327 328 329 330
	case MOXA_GETMSTATUS: {
		struct mxser_mstatus __user *argm = argp;
		struct mxser_mstatus tmp;
		struct moxa_port *p;
		unsigned int i, j;

J
Jiri Slaby 已提交
331
		mutex_lock(&moxa_openlock);
J
Jiri Slaby 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
		for (i = 0; i < MAX_BOARDS; i++) {
			p = moxa_boards[i].ports;
			for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
				memset(&tmp, 0, sizeof(tmp));
				if (!moxa_boards[i].ready)
					goto copy;

				status = MoxaPortLineStatus(p);
				if (status & 1)
					tmp.cts = 1;
				if (status & 2)
					tmp.dsr = 1;
				if (status & 4)
					tmp.dcd = 1;

A
Alan Cox 已提交
347
				if (!p->port.tty || !p->port.tty->termios)
J
Jiri Slaby 已提交
348 349
					tmp.cflag = p->cflag;
				else
A
Alan Cox 已提交
350
					tmp.cflag = p->port.tty->termios->c_cflag;
J
Jiri Slaby 已提交
351
copy:
J
Jiri Slaby 已提交
352 353
				if (copy_to_user(argm, &tmp, sizeof(tmp))) {
					mutex_unlock(&moxa_openlock);
J
Jiri Slaby 已提交
354
					return -EFAULT;
J
Jiri Slaby 已提交
355
				}
J
Jiri Slaby 已提交
356 357
			}
		}
J
Jiri Slaby 已提交
358 359
		mutex_unlock(&moxa_openlock);
		break;
J
Jiri Slaby 已提交
360 361
	}
	case TIOCGSERIAL:
J
Jiri Slaby 已提交
362 363 364 365
		mutex_lock(&moxa_openlock);
		ret = moxa_get_serial_info(ch, argp);
		mutex_unlock(&moxa_openlock);
		break;
J
Jiri Slaby 已提交
366
	case TIOCSSERIAL:
J
Jiri Slaby 已提交
367 368 369 370 371 372
		mutex_lock(&moxa_openlock);
		ret = moxa_set_serial_info(ch, argp);
		mutex_unlock(&moxa_openlock);
		break;
	default:
		ret = -ENOIOCTLCMD;
J
Jiri Slaby 已提交
373
	}
J
Jiri Slaby 已提交
374
	return ret;
J
Jiri Slaby 已提交
375 376 377 378 379 380 381 382 383 384
}

static void moxa_break_ctl(struct tty_struct *tty, int state)
{
	struct moxa_port *port = tty->driver_data;

	moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
			Magic_code);
}

J
Jeff Dike 已提交
385
static const struct tty_operations moxa_ops = {
L
Linus Torvalds 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398
	.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,
	.ioctl = moxa_ioctl,
	.throttle = moxa_throttle,
	.unthrottle = moxa_unthrottle,
	.set_termios = moxa_set_termios,
	.stop = moxa_stop,
	.start = moxa_start,
	.hangup = moxa_hangup,
J
Jiri Slaby 已提交
399
	.break_ctl = moxa_break_ctl,
L
Linus Torvalds 已提交
400 401 402 403
	.tiocmget = moxa_tiocmget,
	.tiocmset = moxa_tiocmset,
};

J
Jiri Slaby 已提交
404 405
static struct tty_driver *moxaDriver;
static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
I
Ingo Molnar 已提交
406
static DEFINE_SPINLOCK(moxa_lock);
A
Alan Cox 已提交
407

J
Jiri Slaby 已提交
408 409 410 411
/*
 * HW init
 */

J
Jiri Slaby 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
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) {
J
Jiri Slaby 已提交
476
			printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
J
Jiri Slaby 已提交
477 478 479 480 481 482 483 484
					"module not found\n");
			return -EIO;
		}
		break;
	}

	return 0;
err:
J
Jiri Slaby 已提交
485
	printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
J
Jiri Slaby 已提交
486 487 488 489 490 491 492 493 494
	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) {
J
Jiri Slaby 已提交
495
		printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
J
Jiri Slaby 已提交
496 497 498 499 500 501 502 503 504 505 506 507
		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;
}

508
static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
J
Jiri Slaby 已提交
509 510 511 512 513
		size_t len)
{
	void __iomem *baseAddr = brd->basemem;
	const u16 *uptr = ptr;
	size_t wlen, len2, j;
514
	unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
515
	unsigned int i, retry;
J
Jiri Slaby 已提交
516 517
	u16 usum, keycode;

518 519
	keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
				C218_KeyCode;
J
Jiri Slaby 已提交
520

521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
	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 已提交
539 540 541 542 543 544 545 546 547 548 549 550 551
	}

	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;
552
			memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
J
Jiri Slaby 已提交
553
			j += len2 << 1;
554 555 556 557 558

			writew(len2, baseAddr + loadlen);
			writew(0, baseAddr + key);
			for (i = 0; i < 100; i++) {
				if (readw(baseAddr + key) == keycode)
J
Jiri Slaby 已提交
559 560 561
					break;
				msleep(10);
			}
562
			if (readw(baseAddr + key) != keycode)
J
Jiri Slaby 已提交
563 564
				return -EIO;
		}
565 566 567 568 569
		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 已提交
570 571 572 573
				break;
			msleep(10);
		}
		retry++;
574 575
	} while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
	if (readb(baseAddr + checksum_ok) != 1)
J
Jiri Slaby 已提交
576 577
		return -EIO;

578
	writew(0, baseAddr + key);
J
Jiri Slaby 已提交
579 580 581 582 583 584 585 586
	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;

587
	if (MOXA_IS_320(brd)) {
588 589 590 591 592 593 594 595 596
		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 已提交
597 598 599 600 601 602 603 604 605 606 607
	}
	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;

608
	if (MOXA_IS_320(brd)) {
609 610 611 612 613 614 615 616 617 618 619 620 621
		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 已提交
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
	}
	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) {
J
Jiri Slaby 已提交
638
		printk(KERN_ERR "MOXA: bios length is not even\n");
J
Jiri Slaby 已提交
639 640 641
		return -EINVAL;
	}

642 643 644 645
	retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
	if (retval)
		return retval;

J
Jiri Slaby 已提交
646 647 648 649 650 651
	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 已提交
652
			port->board = brd;
J
Jiri Slaby 已提交
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
			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 已提交
670
			port->board = brd;
J
Jiri Slaby 已提交
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 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
			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;
	}
	return 0;
}

static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
{
720
	const void *ptr = fw->data;
J
Jiri Slaby 已提交
721 722 723 724 725 726 727 728 729 730 731 732
	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];
733
	} const *hdr = ptr;
J
Jiri Slaby 已提交
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759

	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]))
J
Jiri Slaby 已提交
760
			printk(KERN_WARNING "MOXA firmware: unexpected input "
J
Jiri Slaby 已提交
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
				"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;
811 812
	struct moxa_port *p;
	unsigned int i;
J
Jiri Slaby 已提交
813 814
	int ret;

815 816 817 818 819 820 821 822 823
	brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
			GFP_KERNEL);
	if (brd->ports == NULL) {
		printk(KERN_ERR "cannot allocate memory for ports\n");
		ret = -ENOMEM;
		goto err;
	}

	for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
A
Alan Cox 已提交
824
		tty_port_init(&p->port);
825 826 827 828
		p->type = PORT_16550A;
		p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
	}

J
Jiri Slaby 已提交
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
	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) {
844 845 846 847
		printk(KERN_ERR "MOXA: request_firmware failed. Make sure "
				"you've placed '%s' file into your firmware "
				"loader directory (e.g. /lib/firmware)\n",
				file);
848
		goto err_free;
J
Jiri Slaby 已提交
849 850 851 852 853
	}

	ret = moxa_load_fw(brd, fw);

	release_firmware(fw);
854 855 856 857

	if (ret)
		goto err_free;

J
Jiri Slaby 已提交
858
	spin_lock_bh(&moxa_lock);
859
	brd->ready = 1;
J
Jiri Slaby 已提交
860 861
	if (!timer_pending(&moxaTimer))
		mod_timer(&moxaTimer, jiffies + HZ / 50);
J
Jiri Slaby 已提交
862
	spin_unlock_bh(&moxa_lock);
J
Jiri Slaby 已提交
863

864 865 866 867
	return 0;
err_free:
	kfree(brd->ports);
err:
J
Jiri Slaby 已提交
868 869 870
	return ret;
}

871 872
static void moxa_board_deinit(struct moxa_board_conf *brd)
{
J
Jiri Slaby 已提交
873 874 875
	unsigned int a, opened;

	mutex_lock(&moxa_openlock);
J
Jiri Slaby 已提交
876
	spin_lock_bh(&moxa_lock);
877
	brd->ready = 0;
J
Jiri Slaby 已提交
878
	spin_unlock_bh(&moxa_lock);
J
Jiri Slaby 已提交
879 880 881

	/* pci hot-un-plug support */
	for (a = 0; a < brd->numPorts; a++)
A
Alan Cox 已提交
882 883
		if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
			tty_hangup(brd->ports[a].port.tty);
J
Jiri Slaby 已提交
884 885 886
	while (1) {
		opened = 0;
		for (a = 0; a < brd->numPorts; a++)
A
Alan Cox 已提交
887
			if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
J
Jiri Slaby 已提交
888 889 890 891 892 893 894 895
				opened++;
		mutex_unlock(&moxa_openlock);
		if (!opened)
			break;
		msleep(50);
		mutex_lock(&moxa_openlock);
	}

896 897 898 899 900
	iounmap(brd->basemem);
	brd->basemem = NULL;
	kfree(brd->ports);
}

L
Linus Torvalds 已提交
901
#ifdef CONFIG_PCI
902 903
static int __devinit moxa_pci_probe(struct pci_dev *pdev,
		const struct pci_device_id *ent)
L
Linus Torvalds 已提交
904
{
905 906 907 908 909 910
	struct moxa_board_conf *board;
	unsigned int i;
	int board_type = ent->driver_data;
	int retval;

	retval = pci_enable_device(pdev);
J
Jiri Slaby 已提交
911 912
	if (retval) {
		dev_err(&pdev->dev, "can't enable pci device\n");
913
		goto err;
J
Jiri Slaby 已提交
914
	}
915 916 917 918 919 920 921

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

	retval = -ENODEV;
	if (i >= MAX_BOARDS) {
J
Jiri Slaby 已提交
922
		dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
923 924 925 926 927
				"found. Board is ignored.\n", MAX_BOARDS);
		goto err;
	}

	board = &moxa_boards[i];
J
Jiri Slaby 已提交
928 929 930 931 932 933 934

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

935
	board->basemem = ioremap_nocache(pci_resource_start(pdev, 2), 0x4000);
J
Jiri Slaby 已提交
936 937
	if (board->basemem == NULL) {
		dev_err(&pdev->dev, "can't remap io space 2\n");
J
Jiri Slaby 已提交
938
		goto err_reg;
J
Jiri Slaby 已提交
939
	}
940

L
Linus Torvalds 已提交
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
	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 已提交
956

J
Jiri Slaby 已提交
957 958 959 960
	retval = moxa_init_board(board, &pdev->dev);
	if (retval)
		goto err_base;

961
	pci_set_drvdata(pdev, board);
L
Linus Torvalds 已提交
962

963 964 965
	dev_info(&pdev->dev, "board '%s' ready (%u ports, firmware loaded)\n",
			moxa_brdname[board_type - 1], board->numPorts);

J
Jiri Slaby 已提交
966
	return 0;
J
Jiri Slaby 已提交
967 968 969
err_base:
	iounmap(board->basemem);
	board->basemem = NULL;
J
Jiri Slaby 已提交
970 971
err_reg:
	pci_release_region(pdev, 2);
972 973 974 975 976 977 978 979
err:
	return retval;
}

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

980 981
	moxa_board_deinit(brd);

J
Jiri Slaby 已提交
982
	pci_release_region(pdev, 2);
L
Linus Torvalds 已提交
983
}
J
Jiri Slaby 已提交
984 985 986 987 988 989 990

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 已提交
991 992 993 994
#endif /* CONFIG_PCI */

static int __init moxa_init(void)
{
995
	unsigned int isabrds = 0;
996
	int retval = 0;
L
Linus Torvalds 已提交
997

J
Jiri Slaby 已提交
998 999
	printk(KERN_INFO "MOXA Intellio family driver version %s\n",
			MOXA_VERSION);
L
Linus Torvalds 已提交
1000 1001 1002 1003 1004
	moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
	if (!moxaDriver)
		return -ENOMEM;

	moxaDriver->owner = THIS_MODULE;
1005
	moxaDriver->name = "ttyMX";
L
Linus Torvalds 已提交
1006 1007 1008 1009 1010 1011
	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 已提交
1012 1013
	moxaDriver->init_termios.c_ispeed = 9600;
	moxaDriver->init_termios.c_ospeed = 9600;
L
Linus Torvalds 已提交
1014 1015 1016 1017
	moxaDriver->flags = TTY_DRIVER_REAL_RAW;
	tty_set_operations(moxaDriver, &moxa_ops);

	if (tty_register_driver(moxaDriver)) {
J
Jiri Slaby 已提交
1018
		printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
L
Linus Torvalds 已提交
1019 1020 1021 1022
		put_tty_driver(moxaDriver);
		return -1;
	}

1023
	/* Find the boards defined from module args. */
L
Linus Torvalds 已提交
1024
#ifdef MODULE
1025 1026
	{
	struct moxa_board_conf *brd = moxa_boards;
1027
	unsigned int i;
L
Linus Torvalds 已提交
1028
	for (i = 0; i < MAX_BOARDS; i++) {
1029 1030 1031 1032
		if (!baseaddr[i])
			break;
		if (type[i] == MOXA_BOARD_C218_ISA ||
				type[i] == MOXA_BOARD_C320_ISA) {
J
Jiri Slaby 已提交
1033
			pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
1034 1035 1036 1037 1038 1039
					isabrds + 1, moxa_brdname[type[i] - 1],
					baseaddr[i]);
			brd->boardType = type[i];
			brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
					numports[i];
			brd->busType = MOXA_BUS_TYPE_ISA;
1040
			brd->basemem = ioremap_nocache(baseaddr[i], 0x4000);
1041
			if (!brd->basemem) {
J
Jiri Slaby 已提交
1042
				printk(KERN_ERR "MOXA: can't remap %lx\n",
1043
						baseaddr[i]);
L
Linus Torvalds 已提交
1044 1045
				continue;
			}
J
Jiri Slaby 已提交
1046 1047 1048 1049 1050
			if (moxa_init_board(brd, NULL)) {
				iounmap(brd->basemem);
				brd->basemem = NULL;
				continue;
			}
1051

1052 1053 1054 1055
			printk(KERN_INFO "MOXA isa board found at 0x%.8lu and "
					"ready (%u ports, firmware loaded)\n",
					baseaddr[i], brd->numPorts);

1056 1057
			brd++;
			isabrds++;
L
Linus Torvalds 已提交
1058 1059
		}
	}
1060
	}
L
Linus Torvalds 已提交
1061
#endif
J
Jiri Slaby 已提交
1062

L
Linus Torvalds 已提交
1063
#ifdef CONFIG_PCI
J
Jiri Slaby 已提交
1064 1065
	retval = pci_register_driver(&moxa_pci_driver);
	if (retval) {
J
Jiri Slaby 已提交
1066
		printk(KERN_ERR "Can't register MOXA pci driver!\n");
1067
		if (isabrds)
J
Jiri Slaby 已提交
1068
			retval = 0;
L
Linus Torvalds 已提交
1069 1070
	}
#endif
J
Jiri Slaby 已提交
1071 1072

	return retval;
L
Linus Torvalds 已提交
1073 1074 1075 1076
}

static void __exit moxa_exit(void)
{
J
Jiri Slaby 已提交
1077
	unsigned int i;
L
Linus Torvalds 已提交
1078

1079
#ifdef CONFIG_PCI
J
Jiri Slaby 已提交
1080
	pci_unregister_driver(&moxa_pci_driver);
1081
#endif
J
Jiri Slaby 已提交
1082

1083 1084 1085
	for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
		if (moxa_boards[i].ready)
			moxa_board_deinit(&moxa_boards[i]);
J
Jiri Slaby 已提交
1086 1087 1088 1089 1090 1091 1092

	del_timer_sync(&moxaTimer);

	if (tty_unregister_driver(moxaDriver))
		printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
				"serial driver\n");
	put_tty_driver(moxaDriver);
L
Linus Torvalds 已提交
1093 1094 1095 1096 1097
}

module_init(moxa_init);
module_exit(moxa_exit);

J
Jiri Slaby 已提交
1098 1099 1100 1101
static void moxa_close_port(struct moxa_port *ch)
{
	moxa_shut_down(ch);
	MoxaPortFlushData(ch, 2);
A
Alan Cox 已提交
1102 1103 1104
	ch->port.flags &= ~ASYNC_NORMAL_ACTIVE;
	ch->port.tty->driver_data = NULL;
	ch->port.tty = NULL;
J
Jiri Slaby 已提交
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
}

static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
			    struct moxa_port *ch)
{
	DEFINE_WAIT(wait);
	int retval = 0;
	u8 dcd;

	while (1) {
A
Alan Cox 已提交
1115
		prepare_to_wait(&ch->port.open_wait, &wait, TASK_INTERRUPTIBLE);
J
Jiri Slaby 已提交
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
		if (tty_hung_up_p(filp)) {
#ifdef SERIAL_DO_RESTART
			retval = -ERESTARTSYS;
#else
			retval = -EAGAIN;
#endif
			break;
		}
		spin_lock_bh(&moxa_lock);
		dcd = ch->DCDState;
		spin_unlock_bh(&moxa_lock);
		if (dcd)
			break;

		if (signal_pending(current)) {
			retval = -ERESTARTSYS;
			break;
		}
		schedule();
	}
A
Alan Cox 已提交
1136
	finish_wait(&ch->port.open_wait, &wait);
J
Jiri Slaby 已提交
1137 1138 1139 1140

	return retval;
}

L
Linus Torvalds 已提交
1141 1142
static int moxa_open(struct tty_struct *tty, struct file *filp)
{
1143
	struct moxa_board_conf *brd;
1144
	struct moxa_port *ch;
L
Linus Torvalds 已提交
1145 1146 1147
	int port;
	int retval;

J
Jiri Slaby 已提交
1148
	port = tty->index;
L
Linus Torvalds 已提交
1149
	if (port == MAX_PORTS) {
J
Jiri Slaby 已提交
1150
		return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
L
Linus Torvalds 已提交
1151
	}
J
Jiri Slaby 已提交
1152 1153
	if (mutex_lock_interruptible(&moxa_openlock))
		return -ERESTARTSYS;
1154
	brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
J
Jiri Slaby 已提交
1155 1156
	if (!brd->ready) {
		mutex_unlock(&moxa_openlock);
1157
		return -ENODEV;
J
Jiri Slaby 已提交
1158
	}
L
Linus Torvalds 已提交
1159

1160
	ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
A
Alan Cox 已提交
1161
	ch->port.count++;
L
Linus Torvalds 已提交
1162
	tty->driver_data = ch;
A
Alan Cox 已提交
1163 1164
	ch->port.tty = tty;
	if (!(ch->port.flags & ASYNC_INITIALIZED)) {
L
Linus Torvalds 已提交
1165
		ch->statusflags = 0;
A
Alan Cox 已提交
1166
		moxa_set_tty_param(tty, tty->termios);
J
Jiri Slaby 已提交
1167 1168
		MoxaPortLineCtrl(ch, 1, 1);
		MoxaPortEnable(ch);
J
Jiri Slaby 已提交
1169
		MoxaSetFifo(ch, ch->type == PORT_16550A);
A
Alan Cox 已提交
1170
		ch->port.flags |= ASYNC_INITIALIZED;
L
Linus Torvalds 已提交
1171
	}
J
Jiri Slaby 已提交
1172
	mutex_unlock(&moxa_openlock);
L
Linus Torvalds 已提交
1173

J
Jiri Slaby 已提交
1174 1175 1176 1177 1178
	retval = 0;
	if (!(filp->f_flags & O_NONBLOCK) && !C_CLOCAL(tty))
		retval = moxa_block_till_ready(tty, filp, ch);
	mutex_lock(&moxa_openlock);
	if (retval) {
A
Alan Cox 已提交
1179 1180
		if (ch->port.count) /* 0 means already hung up... */
			if (--ch->port.count == 0)
J
Jiri Slaby 已提交
1181 1182
				moxa_close_port(ch);
	} else
A
Alan Cox 已提交
1183
		ch->port.flags |= ASYNC_NORMAL_ACTIVE;
J
Jiri Slaby 已提交
1184
	mutex_unlock(&moxa_openlock);
L
Linus Torvalds 已提交
1185

J
Jiri Slaby 已提交
1186
	return retval;
L
Linus Torvalds 已提交
1187 1188 1189 1190
}

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

J
Jiri Slaby 已提交
1194
	port = tty->index;
J
Jiri Slaby 已提交
1195
	if (port == MAX_PORTS || tty_hung_up_p(filp))
L
Linus Torvalds 已提交
1196 1197
		return;

J
Jiri Slaby 已提交
1198 1199 1200 1201
	mutex_lock(&moxa_openlock);
	ch = tty->driver_data;
	if (ch == NULL)
		goto unlock;
A
Alan Cox 已提交
1202
	if (tty->count == 1 && ch->port.count != 1) {
J
Jiri Slaby 已提交
1203
		printk(KERN_WARNING "moxa_close: bad serial port count; "
A
Alan Cox 已提交
1204 1205
			"tty->count is 1, ch->port.count is %d\n", ch->port.count);
		ch->port.count = 1;
L
Linus Torvalds 已提交
1206
	}
A
Alan Cox 已提交
1207
	if (--ch->port.count < 0) {
J
Jiri Slaby 已提交
1208 1209
		printk(KERN_WARNING "moxa_close: bad serial port count, "
			"device=%s\n", tty->name);
A
Alan Cox 已提交
1210
		ch->port.count = 0;
L
Linus Torvalds 已提交
1211
	}
A
Alan Cox 已提交
1212
	if (ch->port.count)
J
Jiri Slaby 已提交
1213
		goto unlock;
L
Linus Torvalds 已提交
1214 1215

	ch->cflag = tty->termios->c_cflag;
A
Alan Cox 已提交
1216
	if (ch->port.flags & ASYNC_INITIALIZED) {
J
Jiri Slaby 已提交
1217
		moxa_setup_empty_event(tty);
L
Linus Torvalds 已提交
1218 1219 1220
		tty_wait_until_sent(tty, 30 * HZ);	/* 30 seconds timeout */
	}

J
Jiri Slaby 已提交
1221 1222 1223
	moxa_close_port(ch);
unlock:
	mutex_unlock(&moxa_openlock);
L
Linus Torvalds 已提交
1224 1225 1226 1227 1228
}

static int moxa_write(struct tty_struct *tty,
		      const unsigned char *buf, int count)
{
J
Jiri Slaby 已提交
1229 1230
	struct moxa_port *ch = tty->driver_data;
	int len;
L
Linus Torvalds 已提交
1231 1232

	if (ch == NULL)
J
Jiri Slaby 已提交
1233
		return 0;
A
Alan Cox 已提交
1234

J
Jiri Slaby 已提交
1235
	spin_lock_bh(&moxa_lock);
J
Jiri Slaby 已提交
1236
	len = MoxaPortWriteData(ch, buf, count);
J
Jiri Slaby 已提交
1237
	spin_unlock_bh(&moxa_lock);
L
Linus Torvalds 已提交
1238 1239

	ch->statusflags |= LOWWAIT;
J
Jiri Slaby 已提交
1240
	return len;
L
Linus Torvalds 已提交
1241 1242 1243 1244
}

static int moxa_write_room(struct tty_struct *tty)
{
1245
	struct moxa_port *ch;
L
Linus Torvalds 已提交
1246 1247

	if (tty->stopped)
J
Jiri Slaby 已提交
1248
		return 0;
J
Jiri Slaby 已提交
1249
	ch = tty->driver_data;
L
Linus Torvalds 已提交
1250
	if (ch == NULL)
J
Jiri Slaby 已提交
1251
		return 0;
J
Jiri Slaby 已提交
1252
	return MoxaPortTxFree(ch);
L
Linus Torvalds 已提交
1253 1254 1255 1256
}

static void moxa_flush_buffer(struct tty_struct *tty)
{
J
Jiri Slaby 已提交
1257
	struct moxa_port *ch = tty->driver_data;
L
Linus Torvalds 已提交
1258 1259 1260

	if (ch == NULL)
		return;
J
Jiri Slaby 已提交
1261
	MoxaPortFlushData(ch, 1);
L
Linus Torvalds 已提交
1262 1263 1264 1265 1266
	tty_wakeup(tty);
}

static int moxa_chars_in_buffer(struct tty_struct *tty)
{
J
Jiri Slaby 已提交
1267
	struct moxa_port *ch = tty->driver_data;
L
Linus Torvalds 已提交
1268 1269 1270 1271 1272 1273 1274 1275 1276
	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)
J
Jiri Slaby 已提交
1277
		return 0;
1278
	lock_kernel();
J
Jiri Slaby 已提交
1279
	chars = MoxaPortTxQueue(ch);
L
Linus Torvalds 已提交
1280 1281 1282 1283 1284 1285
	if (chars) {
		/*
		 * Make it possible to wakeup anything waiting for output
		 * in tty_ioctl.c, etc.
		 */
		if (!(ch->statusflags & EMPTYWAIT))
J
Jiri Slaby 已提交
1286
			moxa_setup_empty_event(tty);
L
Linus Torvalds 已提交
1287
	}
1288
	unlock_kernel();
J
Jiri Slaby 已提交
1289
	return chars;
L
Linus Torvalds 已提交
1290 1291 1292 1293
}

static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
{
J
Jiri Slaby 已提交
1294
	struct moxa_port *ch;
L
Linus Torvalds 已提交
1295 1296
	int flag = 0, dtr, rts;

J
Jiri Slaby 已提交
1297 1298 1299 1300
	mutex_lock(&moxa_openlock);
	ch = tty->driver_data;
	if (!ch) {
		mutex_unlock(&moxa_openlock);
J
Jiri Slaby 已提交
1301
		return -EINVAL;
J
Jiri Slaby 已提交
1302
	}
L
Linus Torvalds 已提交
1303

J
Jiri Slaby 已提交
1304
	MoxaPortGetLineOut(ch, &dtr, &rts);
L
Linus Torvalds 已提交
1305 1306 1307 1308
	if (dtr)
		flag |= TIOCM_DTR;
	if (rts)
		flag |= TIOCM_RTS;
J
Jiri Slaby 已提交
1309
	dtr = MoxaPortLineStatus(ch);
L
Linus Torvalds 已提交
1310 1311 1312 1313 1314 1315
	if (dtr & 1)
		flag |= TIOCM_CTS;
	if (dtr & 2)
		flag |= TIOCM_DSR;
	if (dtr & 4)
		flag |= TIOCM_CD;
J
Jiri Slaby 已提交
1316
	mutex_unlock(&moxa_openlock);
L
Linus Torvalds 已提交
1317 1318 1319 1320 1321 1322
	return flag;
}

static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
			 unsigned int set, unsigned int clear)
{
J
Jiri Slaby 已提交
1323
	struct moxa_port *ch;
L
Linus Torvalds 已提交
1324 1325 1326
	int port;
	int dtr, rts;

J
Jiri Slaby 已提交
1327
	port = tty->index;
J
Jiri Slaby 已提交
1328 1329 1330 1331
	mutex_lock(&moxa_openlock);
	ch = tty->driver_data;
	if (!ch) {
		mutex_unlock(&moxa_openlock);
J
Jiri Slaby 已提交
1332
		return -EINVAL;
J
Jiri Slaby 已提交
1333
	}
L
Linus Torvalds 已提交
1334

J
Jiri Slaby 已提交
1335
	MoxaPortGetLineOut(ch, &dtr, &rts);
L
Linus Torvalds 已提交
1336 1337 1338 1339 1340 1341 1342 1343
	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 已提交
1344
	MoxaPortLineCtrl(ch, dtr, rts);
J
Jiri Slaby 已提交
1345
	mutex_unlock(&moxa_openlock);
L
Linus Torvalds 已提交
1346 1347 1348 1349 1350
	return 0;
}

static void moxa_throttle(struct tty_struct *tty)
{
J
Jiri Slaby 已提交
1351
	struct moxa_port *ch = tty->driver_data;
L
Linus Torvalds 已提交
1352 1353 1354 1355 1356 1357

	ch->statusflags |= THROTTLE;
}

static void moxa_unthrottle(struct tty_struct *tty)
{
J
Jiri Slaby 已提交
1358
	struct moxa_port *ch = tty->driver_data;
L
Linus Torvalds 已提交
1359 1360 1361 1362 1363

	ch->statusflags &= ~THROTTLE;
}

static void moxa_set_termios(struct tty_struct *tty,
J
Jiri Slaby 已提交
1364
		struct ktermios *old_termios)
L
Linus Torvalds 已提交
1365
{
J
Jiri Slaby 已提交
1366
	struct moxa_port *ch = tty->driver_data;
L
Linus Torvalds 已提交
1367 1368 1369

	if (ch == NULL)
		return;
A
Alan Cox 已提交
1370
	moxa_set_tty_param(tty, old_termios);
J
Jiri Slaby 已提交
1371
	if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
A
Alan Cox 已提交
1372
		wake_up_interruptible(&ch->port.open_wait);
L
Linus Torvalds 已提交
1373 1374 1375 1376
}

static void moxa_stop(struct tty_struct *tty)
{
J
Jiri Slaby 已提交
1377
	struct moxa_port *ch = tty->driver_data;
L
Linus Torvalds 已提交
1378 1379 1380

	if (ch == NULL)
		return;
J
Jiri Slaby 已提交
1381
	MoxaPortTxDisable(ch);
L
Linus Torvalds 已提交
1382 1383 1384 1385 1386 1387
	ch->statusflags |= TXSTOPPED;
}


static void moxa_start(struct tty_struct *tty)
{
J
Jiri Slaby 已提交
1388
	struct moxa_port *ch = tty->driver_data;
L
Linus Torvalds 已提交
1389 1390 1391 1392 1393 1394 1395

	if (ch == NULL)
		return;

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

J
Jiri Slaby 已提交
1396
	MoxaPortTxEnable(ch);
L
Linus Torvalds 已提交
1397 1398 1399 1400 1401
	ch->statusflags &= ~TXSTOPPED;
}

static void moxa_hangup(struct tty_struct *tty)
{
J
Jiri Slaby 已提交
1402
	struct moxa_port *ch;
L
Linus Torvalds 已提交
1403

J
Jiri Slaby 已提交
1404 1405 1406 1407 1408 1409
	mutex_lock(&moxa_openlock);
	ch = tty->driver_data;
	if (ch == NULL) {
		mutex_unlock(&moxa_openlock);
		return;
	}
A
Alan Cox 已提交
1410
	ch->port.count = 0;
J
Jiri Slaby 已提交
1411 1412 1413
	moxa_close_port(ch);
	mutex_unlock(&moxa_openlock);

A
Alan Cox 已提交
1414
	wake_up_interruptible(&ch->port.open_wait);
L
Linus Torvalds 已提交
1415 1416
}

J
Jiri Slaby 已提交
1417
static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
L
Linus Torvalds 已提交
1418
{
J
Jiri Slaby 已提交
1419
	dcd = !!dcd;
L
Linus Torvalds 已提交
1420

A
Alan Cox 已提交
1421
	if (dcd != p->DCDState && p->port.tty && C_CLOCAL(p->port.tty)) {
J
Jiri Slaby 已提交
1422
		if (!dcd)
A
Alan Cox 已提交
1423
			tty_hangup(p->port.tty);
J
Jiri Slaby 已提交
1424 1425 1426
	}
	p->DCDState = dcd;
}
L
Linus Torvalds 已提交
1427

J
Jiri Slaby 已提交
1428 1429 1430
static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
		u16 __iomem *ip)
{
A
Alan Cox 已提交
1431
	struct tty_struct *tty = p->port.tty;
J
Jiri Slaby 已提交
1432
	void __iomem *ofsAddr;
A
Alan Cox 已提交
1433
	unsigned int inited = p->port.flags & ASYNC_INITIALIZED;
J
Jiri Slaby 已提交
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
	u16 intr;

	if (tty) {
		if ((p->statusflags & EMPTYWAIT) &&
				MoxaPortTxQueue(p) == 0) {
			p->statusflags &= ~EMPTYWAIT;
			tty_wakeup(tty);
		}
		if ((p->statusflags & LOWWAIT) && !tty->stopped &&
				MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
			p->statusflags &= ~LOWWAIT;
			tty_wakeup(tty);
		}

		if (inited && !(p->statusflags & THROTTLE) &&
				MoxaPortRxQueue(p) > 0) { /* RX */
			MoxaPortReadData(p);
			tty_schedule_flip(tty);
		}
	} else {
		p->statusflags &= ~EMPTYWAIT;
		MoxaPortFlushData(p, 0); /* flush RX */
L
Linus Torvalds 已提交
1456
	}
J
Jiri Slaby 已提交
1457

J
Jiri Slaby 已提交
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
	if (!handle) /* nothing else to do */
		return 0;

	intr = readw(ip); /* port irq status */
	if (intr == 0)
		return 0;

	writew(0, ip); /* ACK port */
	ofsAddr = p->tableAddr;
	if (intr & IntrTx) /* disable tx intr */
		writew(readw(ofsAddr + HostStat) & ~WakeupTx,
				ofsAddr + HostStat);

	if (!inited)
		return 0;

	if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
		tty_insert_flip_char(tty, 0, TTY_BREAK);
		tty_schedule_flip(tty);
	}

	if (intr & IntrLine)
		moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);

	return 0;
}

static void moxa_poll(unsigned long ignored)
{
	struct moxa_board_conf *brd;
	u16 __iomem *ip;
J
Jiri Slaby 已提交
1489
	unsigned int card, port, served = 0;
J
Jiri Slaby 已提交
1490 1491

	spin_lock(&moxa_lock);
L
Linus Torvalds 已提交
1492
	for (card = 0; card < MAX_BOARDS; card++) {
J
Jiri Slaby 已提交
1493 1494
		brd = &moxa_boards[card];
		if (!brd->ready)
L
Linus Torvalds 已提交
1495
			continue;
J
Jiri Slaby 已提交
1496

J
Jiri Slaby 已提交
1497 1498
		served++;

J
Jiri Slaby 已提交
1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
		ip = NULL;
		if (readb(brd->intPend) == 0xff)
			ip = brd->intTable + readb(brd->intNdx);

		for (port = 0; port < brd->numPorts; port++)
			moxa_poll_port(&brd->ports[port], !!ip, ip + port);

		if (ip)
			writeb(0, brd->intPend); /* ACK */

		if (moxaLowWaterChk) {
			struct moxa_port *p = brd->ports;
			for (port = 0; port < brd->numPorts; port++, p++)
				if (p->lowChkFlag) {
					p->lowChkFlag = 0;
					moxa_low_water_check(p->tableAddr);
L
Linus Torvalds 已提交
1515 1516 1517
				}
		}
	}
J
Jiri Slaby 已提交
1518
	moxaLowWaterChk = 0;
L
Linus Torvalds 已提交
1519

J
Jiri Slaby 已提交
1520 1521 1522
	if (served)
		mod_timer(&moxaTimer, jiffies + HZ / 50);
	spin_unlock(&moxa_lock);
L
Linus Torvalds 已提交
1523 1524 1525 1526
}

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

A
Alan Cox 已提交
1527
static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
L
Linus Torvalds 已提交
1528
{
J
Jiri Slaby 已提交
1529 1530
	register struct ktermios *ts = tty->termios;
	struct moxa_port *ch = tty->driver_data;
A
Alan Cox 已提交
1531
	int rts, cts, txflow, rxflow, xany, baud;
L
Linus Torvalds 已提交
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541

	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 已提交
1542 1543 1544

	/* Clear the features we don't support */
	ts->c_cflag &= ~CMSPAR;
J
Jiri Slaby 已提交
1545 1546
	MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
	baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
A
Alan Cox 已提交
1547 1548 1549 1550
	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 已提交
1551 1552
}

J
Jiri Slaby 已提交
1553
static void moxa_setup_empty_event(struct tty_struct *tty)
L
Linus Torvalds 已提交
1554
{
1555
	struct moxa_port *ch = tty->driver_data;
L
Linus Torvalds 已提交
1556

J
Jiri Slaby 已提交
1557
	spin_lock_bh(&moxa_lock);
L
Linus Torvalds 已提交
1558
	ch->statusflags |= EMPTYWAIT;
J
Jiri Slaby 已提交
1559
	spin_unlock_bh(&moxa_lock);
L
Linus Torvalds 已提交
1560 1561
}

J
Jiri Slaby 已提交
1562
static void moxa_shut_down(struct moxa_port *ch)
L
Linus Torvalds 已提交
1563
{
A
Alan Cox 已提交
1564
	struct tty_struct *tp = ch->port.tty;
L
Linus Torvalds 已提交
1565

A
Alan Cox 已提交
1566
	if (!(ch->port.flags & ASYNC_INITIALIZED))
L
Linus Torvalds 已提交
1567 1568
		return;

J
Jiri Slaby 已提交
1569
	MoxaPortDisable(ch);
L
Linus Torvalds 已提交
1570 1571 1572 1573

	/*
	 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
	 */
J
Jiri Slaby 已提交
1574
	if (C_HUPCL(tp))
J
Jiri Slaby 已提交
1575
		MoxaPortLineCtrl(ch, 0, 0);
L
Linus Torvalds 已提交
1576

J
Jiri Slaby 已提交
1577
	spin_lock_bh(&moxa_lock);
A
Alan Cox 已提交
1578
	ch->port.flags &= ~ASYNC_INITIALIZED;
J
Jiri Slaby 已提交
1579
	spin_unlock_bh(&moxa_lock);
L
Linus Torvalds 已提交
1580 1581 1582 1583 1584 1585
}

/*****************************************************************************
 *	Driver level functions: 					     *
 *****************************************************************************/

J
Jiri Slaby 已提交
1586
static void MoxaPortFlushData(struct moxa_port *port, int mode)
L
Linus Torvalds 已提交
1587 1588
{
	void __iomem *ofsAddr;
J
Jiri Slaby 已提交
1589
	if (mode < 0 || mode > 2)
L
Linus Torvalds 已提交
1590
		return;
J
Jiri Slaby 已提交
1591
	ofsAddr = port->tableAddr;
L
Linus Torvalds 已提交
1592 1593
	moxafunc(ofsAddr, FC_FlushQueue, mode);
	if (mode != 1) {
J
Jiri Slaby 已提交
1594
		port->lowChkFlag = 0;
J
Jiri Slaby 已提交
1595
		moxa_low_water_check(ofsAddr);
L
Linus Torvalds 已提交
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648
	}
}

/*
 *    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 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 10:    Setting baud rate of this port.
 *      Syntax:
1649
 *      speed_t MoxaPortSetBaud(int port, speed_t baud);
L
Linus Torvalds 已提交
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
 *           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 已提交
1662
 *      int  MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
L
Linus Torvalds 已提交
1663
 *           int port           : port number (0 - 127)
A
Alan Cox 已提交
1664
 *           struct ktermios * termio : termio structure pointer
1665
 *	     speed_t baud	: baud rate
L
Linus Torvalds 已提交
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
 *
 *           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 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 已提交
1738
 *      int  MoxaPortReadData(int port, struct tty_struct *tty);
L
Linus Torvalds 已提交
1739
 *           int port           : port number (0 - 127)
A
Alan Cox 已提交
1740
 *	     struct tty_struct *tty : tty for data
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
 *
 *           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
 *
 *
 */

J
Jiri Slaby 已提交
1791
static void MoxaPortEnable(struct moxa_port *port)
L
Linus Torvalds 已提交
1792 1793
{
	void __iomem *ofsAddr;
J
Jiri Slaby 已提交
1794
	u16 lowwater = 512;
L
Linus Torvalds 已提交
1795

J
Jiri Slaby 已提交
1796
	ofsAddr = port->tableAddr;
L
Linus Torvalds 已提交
1797
	writew(lowwater, ofsAddr + Low_water);
1798
	if (MOXA_IS_320(port->board))
L
Linus Torvalds 已提交
1799
		moxafunc(ofsAddr, FC_SetBreakIrq, 0);
J
Jiri Slaby 已提交
1800 1801 1802
	else
		writew(readw(ofsAddr + HostStat) | WakeupBreak,
				ofsAddr + HostStat);
L
Linus Torvalds 已提交
1803 1804 1805 1806 1807 1808 1809 1810

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

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

J
Jiri Slaby 已提交
1811
static void MoxaPortDisable(struct moxa_port *port)
L
Linus Torvalds 已提交
1812
{
J
Jiri Slaby 已提交
1813
	void __iomem *ofsAddr = port->tableAddr;
L
Linus Torvalds 已提交
1814 1815 1816 1817 1818 1819 1820

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

1821
static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
L
Linus Torvalds 已提交
1822
{
1823 1824 1825
	void __iomem *ofsAddr = port->tableAddr;
	unsigned int clock, val;
	speed_t max;
L
Linus Torvalds 已提交
1826

1827 1828
	max = MOXA_IS_320(port->board) ? 460800 : 921600;
	if (baud < 50)
J
Jiri Slaby 已提交
1829
		return 0;
L
Linus Torvalds 已提交
1830 1831
	if (baud > max)
		baud = max;
1832
	clock = 921600;
L
Linus Torvalds 已提交
1833 1834 1835
	val = clock / baud;
	moxafunc(ofsAddr, FC_SetBaud, val);
	baud = clock / val;
J
Jiri Slaby 已提交
1836
	return baud;
L
Linus Torvalds 已提交
1837 1838
}

J
Jiri Slaby 已提交
1839 1840
static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
		speed_t baud)
L
Linus Torvalds 已提交
1841 1842 1843 1844 1845
{
	void __iomem *ofsAddr;
	tcflag_t cflag;
	tcflag_t mode = 0;

J
Jiri Slaby 已提交
1846
	ofsAddr = port->tableAddr;
L
Linus Torvalds 已提交
1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
	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;

J
Jiri Slaby 已提交
1875
	moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
L
Linus Torvalds 已提交
1876

1877 1878 1879
	if (MOXA_IS_320(port->board) && baud >= 921600)
		return -1;

A
Alan Cox 已提交
1880
	baud = MoxaPortSetBaud(port, baud);
L
Linus Torvalds 已提交
1881 1882 1883 1884 1885

	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 已提交
1886
		moxa_wait_finish(ofsAddr);
L
Linus Torvalds 已提交
1887 1888

	}
J
Jiri Slaby 已提交
1889
	return baud;
L
Linus Torvalds 已提交
1890 1891
}

J
Jiri Slaby 已提交
1892 1893
static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
		int *rtsState)
L
Linus Torvalds 已提交
1894
{
J
Jiri Slaby 已提交
1895 1896 1897 1898 1899
	if (dtrState)
		*dtrState = !!(port->lineCtrl & DTR_ON);
	if (rtsState)
		*rtsState = !!(port->lineCtrl & RTS_ON);

J
Jiri Slaby 已提交
1900
	return 0;
L
Linus Torvalds 已提交
1901 1902
}

J
Jiri Slaby 已提交
1903
static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
L
Linus Torvalds 已提交
1904
{
J
Jiri Slaby 已提交
1905
	u8 mode = 0;
L
Linus Torvalds 已提交
1906 1907 1908 1909 1910

	if (dtr)
		mode |= DTR_ON;
	if (rts)
		mode |= RTS_ON;
J
Jiri Slaby 已提交
1911 1912
	port->lineCtrl = mode;
	moxafunc(port->tableAddr, FC_LineControl, mode);
L
Linus Torvalds 已提交
1913 1914
}

J
Jiri Slaby 已提交
1915 1916
static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
		int txflow, int rxflow, int txany)
L
Linus Torvalds 已提交
1917
{
J
Jiri Slaby 已提交
1918
	int mode = 0;
L
Linus Torvalds 已提交
1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929

	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 已提交
1930
	moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
L
Linus Torvalds 已提交
1931 1932
}

J
Jiri Slaby 已提交
1933
static int MoxaPortLineStatus(struct moxa_port *port)
L
Linus Torvalds 已提交
1934 1935 1936 1937
{
	void __iomem *ofsAddr;
	int val;

J
Jiri Slaby 已提交
1938
	ofsAddr = port->tableAddr;
1939
	if (MOXA_IS_320(port->board)) {
L
Linus Torvalds 已提交
1940 1941 1942 1943 1944 1945
		moxafunc(ofsAddr, FC_LineStatus, 0);
		val = readw(ofsAddr + FuncArg);
	} else {
		val = readw(ofsAddr + FlagStat) >> 4;
	}
	val &= 0x0B;
J
Jiri Slaby 已提交
1946
	if (val & 8)
L
Linus Torvalds 已提交
1947
		val |= 4;
J
Jiri Slaby 已提交
1948
	spin_lock_bh(&moxa_lock);
J
Jiri Slaby 已提交
1949
	moxa_new_dcdstate(port, val & 8);
J
Jiri Slaby 已提交
1950
	spin_unlock_bh(&moxa_lock);
L
Linus Torvalds 已提交
1951
	val &= 7;
J
Jiri Slaby 已提交
1952
	return val;
L
Linus Torvalds 已提交
1953 1954
}

J
Jiri Slaby 已提交
1955 1956
static int MoxaPortWriteData(struct moxa_port *port,
		const unsigned char *buffer, int len)
L
Linus Torvalds 已提交
1957 1958
{
	void __iomem *baseAddr, *ofsAddr, *ofs;
J
Jiri Slaby 已提交
1959 1960 1961
	unsigned int c, total;
	u16 head, tail, tx_mask, spage, epage;
	u16 pageno, pageofs, bufhead;
L
Linus Torvalds 已提交
1962

J
Jiri Slaby 已提交
1963 1964
	ofsAddr = port->tableAddr;
	baseAddr = port->board->basemem;
L
Linus Torvalds 已提交
1965 1966 1967 1968 1969
	tx_mask = readw(ofsAddr + TX_mask);
	spage = readw(ofsAddr + Page_txb);
	epage = readw(ofsAddr + EndPage_txb);
	tail = readw(ofsAddr + TXwptr);
	head = readw(ofsAddr + TXrptr);
J
Jiri Slaby 已提交
1970
	c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
L
Linus Torvalds 已提交
1971 1972
	if (c > len)
		c = len;
A
Alan Cox 已提交
1973
	moxaLog.txcnt[port->port.tty->index] += c;
L
Linus Torvalds 已提交
1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
	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;
J
Jiri Slaby 已提交
1985 1986
			memcpy_toio(ofs, buffer, len);
			buffer += len;
L
Linus Torvalds 已提交
1987 1988 1989 1990 1991 1992
			tail = (tail + len) & tx_mask;
			c -= len;
		}
	} else {
		pageno = spage + (tail >> 13);
		pageofs = tail & Page_mask;
J
Jiri Slaby 已提交
1993 1994 1995 1996
		while (c > 0) {
			len = Page_size - pageofs;
			if (len > c)
				len = c;
L
Linus Torvalds 已提交
1997 1998
			writeb(pageno, baseAddr + Control_reg);
			ofs = baseAddr + DynPage_addr + pageofs;
J
Jiri Slaby 已提交
1999 2000
			memcpy_toio(ofs, buffer, len);
			buffer += len;
L
Linus Torvalds 已提交
2001 2002 2003
			if (++pageno == epage)
				pageno = spage;
			pageofs = 0;
J
Jiri Slaby 已提交
2004 2005 2006
			c -= len;
		}
		tail = (tail + total) & tx_mask;
L
Linus Torvalds 已提交
2007
	}
J
Jiri Slaby 已提交
2008
	writew(tail, ofsAddr + TXwptr);
L
Linus Torvalds 已提交
2009
	writeb(1, ofsAddr + CD180TXirq);	/* start to send */
J
Jiri Slaby 已提交
2010
	return total;
L
Linus Torvalds 已提交
2011 2012
}

J
Jiri Slaby 已提交
2013
static int MoxaPortReadData(struct moxa_port *port)
L
Linus Torvalds 已提交
2014
{
A
Alan Cox 已提交
2015
	struct tty_struct *tty = port->port.tty;
J
Jiri Slaby 已提交
2016
	unsigned char *dst;
L
Linus Torvalds 已提交
2017
	void __iomem *baseAddr, *ofsAddr, *ofs;
J
Jiri Slaby 已提交
2018 2019 2020
	unsigned int count, len, total;
	u16 tail, rx_mask, spage, epage;
	u16 pageno, pageofs, bufhead, head;
L
Linus Torvalds 已提交
2021

J
Jiri Slaby 已提交
2022 2023
	ofsAddr = port->tableAddr;
	baseAddr = port->board->basemem;
L
Linus Torvalds 已提交
2024 2025 2026 2027 2028
	head = readw(ofsAddr + RXrptr);
	tail = readw(ofsAddr + RXwptr);
	rx_mask = readw(ofsAddr + RX_mask);
	spage = readw(ofsAddr + Page_rxb);
	epage = readw(ofsAddr + EndPage_rxb);
J
Jiri Slaby 已提交
2029
	count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
L
Linus Torvalds 已提交
2030
	if (count == 0)
A
Alan Cox 已提交
2031
		return 0;
L
Linus Torvalds 已提交
2032

A
Alan Cox 已提交
2033
	total = count;
J
Jiri Slaby 已提交
2034
	moxaLog.rxcnt[tty->index] += total;
L
Linus Torvalds 已提交
2035 2036 2037 2038 2039
	if (spage == epage) {
		bufhead = readw(ofsAddr + Ofs_rxb);
		writew(spage, baseAddr + Control_reg);
		while (count > 0) {
			ofs = baseAddr + DynPage_addr + bufhead + head;
J
Jiri Slaby 已提交
2040 2041 2042 2043 2044
			len = (tail >= head) ? (tail - head) :
					(rx_mask + 1 - head);
			len = tty_prepare_flip_string(tty, &dst,
					min(len, count));
			memcpy_fromio(dst, ofs, len);
L
Linus Torvalds 已提交
2045 2046 2047 2048 2049 2050
			head = (head + len) & rx_mask;
			count -= len;
		}
	} else {
		pageno = spage + (head >> 13);
		pageofs = head & Page_mask;
J
Jiri Slaby 已提交
2051
		while (count > 0) {
L
Linus Torvalds 已提交
2052 2053
			writew(pageno, baseAddr + Control_reg);
			ofs = baseAddr + DynPage_addr + pageofs;
J
Jiri Slaby 已提交
2054 2055 2056 2057 2058 2059 2060
			len = tty_prepare_flip_string(tty, &dst,
					min(Page_size - pageofs, count));
			memcpy_fromio(dst, ofs, len);

			count -= len;
			pageofs = (pageofs + len) & Page_mask;
			if (pageofs == 0 && ++pageno == epage)
L
Linus Torvalds 已提交
2061
				pageno = spage;
J
Jiri Slaby 已提交
2062 2063
		}
		head = (head + total) & rx_mask;
L
Linus Torvalds 已提交
2064
	}
J
Jiri Slaby 已提交
2065 2066
	writew(head, ofsAddr + RXrptr);
	if (readb(ofsAddr + FlagStat) & Xoff_state) {
L
Linus Torvalds 已提交
2067
		moxaLowWaterChk = 1;
J
Jiri Slaby 已提交
2068
		port->lowChkFlag = 1;
L
Linus Torvalds 已提交
2069
	}
J
Jiri Slaby 已提交
2070
	return total;
L
Linus Torvalds 已提交
2071 2072 2073
}


J
Jiri Slaby 已提交
2074
static int MoxaPortTxQueue(struct moxa_port *port)
L
Linus Torvalds 已提交
2075
{
J
Jiri Slaby 已提交
2076
	void __iomem *ofsAddr = port->tableAddr;
J
Jiri Slaby 已提交
2077
	u16 rptr, wptr, mask;
L
Linus Torvalds 已提交
2078 2079 2080 2081

	rptr = readw(ofsAddr + TXrptr);
	wptr = readw(ofsAddr + TXwptr);
	mask = readw(ofsAddr + TX_mask);
J
Jiri Slaby 已提交
2082
	return (wptr - rptr) & mask;
L
Linus Torvalds 已提交
2083 2084
}

J
Jiri Slaby 已提交
2085
static int MoxaPortTxFree(struct moxa_port *port)
L
Linus Torvalds 已提交
2086
{
J
Jiri Slaby 已提交
2087
	void __iomem *ofsAddr = port->tableAddr;
J
Jiri Slaby 已提交
2088
	u16 rptr, wptr, mask;
L
Linus Torvalds 已提交
2089 2090 2091 2092

	rptr = readw(ofsAddr + TXrptr);
	wptr = readw(ofsAddr + TXwptr);
	mask = readw(ofsAddr + TX_mask);
J
Jiri Slaby 已提交
2093
	return mask - ((wptr - rptr) & mask);
L
Linus Torvalds 已提交
2094 2095
}

J
Jiri Slaby 已提交
2096
static int MoxaPortRxQueue(struct moxa_port *port)
L
Linus Torvalds 已提交
2097
{
J
Jiri Slaby 已提交
2098
	void __iomem *ofsAddr = port->tableAddr;
J
Jiri Slaby 已提交
2099
	u16 rptr, wptr, mask;
L
Linus Torvalds 已提交
2100 2101 2102 2103

	rptr = readw(ofsAddr + RXrptr);
	wptr = readw(ofsAddr + RXwptr);
	mask = readw(ofsAddr + RX_mask);
J
Jiri Slaby 已提交
2104
	return (wptr - rptr) & mask;
L
Linus Torvalds 已提交
2105 2106
}

J
Jiri Slaby 已提交
2107
static void MoxaPortTxDisable(struct moxa_port *port)
L
Linus Torvalds 已提交
2108
{
J
Jiri Slaby 已提交
2109
	moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
L
Linus Torvalds 已提交
2110 2111
}

J
Jiri Slaby 已提交
2112
static void MoxaPortTxEnable(struct moxa_port *port)
L
Linus Torvalds 已提交
2113
{
J
Jiri Slaby 已提交
2114
	moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
L
Linus Torvalds 已提交
2115 2116
}

2117
static int moxa_get_serial_info(struct moxa_port *info,
J
Jiri Slaby 已提交
2118
		struct serial_struct __user *retinfo)
L
Linus Torvalds 已提交
2119
{
J
Jiri Slaby 已提交
2120 2121
	struct serial_struct tmp = {
		.type = info->type,
A
Alan Cox 已提交
2122 2123
		.line = info->port.tty->index,
		.flags = info->port.flags,
J
Jiri Slaby 已提交
2124
		.baud_base = 921600,
A
Alan Cox 已提交
2125
		.close_delay = info->port.close_delay
J
Jiri Slaby 已提交
2126 2127
	};
	return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
L
Linus Torvalds 已提交
2128 2129 2130
}


2131
static int moxa_set_serial_info(struct moxa_port *info,
J
Jiri Slaby 已提交
2132
		struct serial_struct __user *new_info)
L
Linus Torvalds 已提交
2133 2134 2135
{
	struct serial_struct new_serial;

J
Jiri Slaby 已提交
2136
	if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
L
Linus Torvalds 已提交
2137 2138
		return -EFAULT;

J
Jiri Slaby 已提交
2139 2140 2141 2142
	if (new_serial.irq != 0 || new_serial.port != 0 ||
			new_serial.custom_divisor != 0 ||
			new_serial.baud_base != 921600)
		return -EPERM;
L
Linus Torvalds 已提交
2143 2144 2145

	if (!capable(CAP_SYS_ADMIN)) {
		if (((new_serial.flags & ~ASYNC_USR_MASK) !=
A
Alan Cox 已提交
2146
		     (info->port.flags & ~ASYNC_USR_MASK)))
J
Jiri Slaby 已提交
2147 2148
			return -EPERM;
	} else
A
Alan Cox 已提交
2149
		info->port.close_delay = new_serial.close_delay * HZ / 100;
L
Linus Torvalds 已提交
2150 2151

	new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
A
Alan Cox 已提交
2152
	new_serial.flags |= (info->port.flags & ASYNC_FLAGS);
L
Linus Torvalds 已提交
2153

J
Jiri Slaby 已提交
2154
	MoxaSetFifo(info, new_serial.type == PORT_16550A);
L
Linus Torvalds 已提交
2155 2156

	info->type = new_serial.type;
J
Jiri Slaby 已提交
2157
	return 0;
L
Linus Torvalds 已提交
2158 2159 2160 2161 2162 2163 2164 2165
}



/*****************************************************************************
 *	Static local functions: 					     *
 *****************************************************************************/

J
Jiri Slaby 已提交
2166
static void MoxaSetFifo(struct moxa_port *port, int enable)
L
Linus Torvalds 已提交
2167
{
J
Jiri Slaby 已提交
2168
	void __iomem *ofsAddr = port->tableAddr;
L
Linus Torvalds 已提交
2169 2170 2171 2172 2173 2174 2175 2176 2177

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