moxa.c 52.5 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
#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>
46
#include <linux/slab.h>
47
#include <linux/ratelimit.h>
L
Linus Torvalds 已提交
48 49 50 51

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

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

J
Jiri Slaby 已提交
54
#define MOXA_VERSION		"6.0k"
L
Linus Torvalds 已提交
55

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

J
Jiri Slaby 已提交
58
#define MOXAMAJOR		172
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 MOXA_IS_320(brd) ((brd)->boardType == MOXA_BOARD_C320_ISA || \
		(brd)->boardType == MOXA_BOARD_C320_PCI)

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

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

J
Jiri Slaby 已提交
103 104
struct moxa_port;

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

110
	unsigned int ready;
111

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

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

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

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

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

A
Alan Cox 已提交
142
	u8 DCDState;		/* Protected by the port lock */
J
Jiri Slaby 已提交
143 144
	u8 lineCtrl;
	u8 lowChkFlag;
145
};
L
Linus Torvalds 已提交
146

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

L
Linus Torvalds 已提交
153
/* statusflags */
A
Alan Cox 已提交
154 155 156
#define TXSTOPPED	1
#define LOWWAIT 	2
#define EMPTYWAIT	3
L
Linus Torvalds 已提交
157 158 159 160 161 162

#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);
A
Alan Cox 已提交
167
static DEFINE_SPINLOCK(moxa_lock);
168

169 170 171
static unsigned long baseaddr[MAX_BOARDS];
static unsigned int type[MAX_BOARDS];
static unsigned int numports[MAX_BOARDS];
172
static struct tty_port moxa_service_port;
L
Linus Torvalds 已提交
173 174 175 176

MODULE_AUTHOR("William Chen");
MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
MODULE_LICENSE("GPL");
177 178 179
MODULE_FIRMWARE("c218tunx.cod");
MODULE_FIRMWARE("cp204unx.cod");
MODULE_FIRMWARE("c320tunx.cod");
180

181 182 183 184 185 186
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)");
187

L
Linus Torvalds 已提交
188 189 190 191 192 193 194 195 196 197 198
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 *);
A
Alan Cox 已提交
199
static void moxa_set_termios(struct tty_struct *, struct ktermios *);
L
Linus Torvalds 已提交
200 201 202
static void moxa_stop(struct tty_struct *);
static void moxa_start(struct tty_struct *);
static void moxa_hangup(struct tty_struct *);
203
static int moxa_tiocmget(struct tty_struct *tty);
204
static int moxa_tiocmset(struct tty_struct *tty,
L
Linus Torvalds 已提交
205 206
			 unsigned int set, unsigned int clear);
static void moxa_poll(unsigned long);
A
Alan Cox 已提交
207
static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
A
Alan Cox 已提交
208
static void moxa_shutdown(struct tty_port *);
209
static int moxa_carrier_raised(struct tty_port *);
A
Alan Cox 已提交
210
static void moxa_dtr_rts(struct tty_port *, int);
L
Linus Torvalds 已提交
211 212 213
/*
 * moxa board interface functions:
 */
J
Jiri Slaby 已提交
214 215 216 217 218 219 220 221
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);
A
Alan Cox 已提交
222
static int MoxaPortWriteData(struct tty_struct *, const unsigned char *, int);
J
Jiri Slaby 已提交
223
static int MoxaPortReadData(struct moxa_port *);
J
Jiri Slaby 已提交
224 225 226 227 228
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 *);
229 230
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 已提交
231
static void MoxaSetFifo(struct moxa_port *port, int enable);
L
Linus Torvalds 已提交
232

J
Jiri Slaby 已提交
233 234 235 236
/*
 * I/O functions
 */

A
Alan Cox 已提交
237 238
static DEFINE_SPINLOCK(moxafunc_lock);

J
Jiri Slaby 已提交
239 240 241 242 243 244 245
static void moxa_wait_finish(void __iomem *ofsAddr)
{
	unsigned long end = jiffies + moxaFuncTout;

	while (readw(ofsAddr + FuncCode) != 0)
		if (time_after(jiffies, end))
			return;
246 247
	if (readw(ofsAddr + FuncCode) != 0)
		printk_ratelimited(KERN_WARNING "moxa function expired\n");
J
Jiri Slaby 已提交
248 249
}

J
Jiri Slaby 已提交
250
static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
J
Jiri Slaby 已提交
251
{
A
Alan Cox 已提交
252 253
        unsigned long flags;
        spin_lock_irqsave(&moxafunc_lock, flags);
J
Jiri Slaby 已提交
254 255 256
	writew(arg, ofsAddr + FuncArg);
	writew(cmd, ofsAddr + FuncCode);
	moxa_wait_finish(ofsAddr);
A
Alan Cox 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270
	spin_unlock_irqrestore(&moxafunc_lock, flags);
}

static int moxafuncret(void __iomem *ofsAddr, u16 cmd, u16 arg)
{
        unsigned long flags;
        u16 ret;
        spin_lock_irqsave(&moxafunc_lock, flags);
	writew(arg, ofsAddr + FuncArg);
	writew(cmd, ofsAddr + FuncCode);
	moxa_wait_finish(ofsAddr);
	ret = readw(ofsAddr + FuncArg);
	spin_unlock_irqrestore(&moxafunc_lock, flags);
	return ret;
J
Jiri Slaby 已提交
271 272
}

J
Jiri Slaby 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286
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 已提交
287 288 289 290
/*
 * TTY operations
 */

291
static int moxa_ioctl(struct tty_struct *tty,
J
Jiri Slaby 已提交
292 293 294 295
		      unsigned int cmd, unsigned long arg)
{
	struct moxa_port *ch = tty->driver_data;
	void __user *argp = (void __user *)arg;
J
Jiri Slaby 已提交
296
	int status, ret = 0;
J
Jiri Slaby 已提交
297 298 299 300 301 302 303 304 305 306 307

	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 已提交
308 309 310
		if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
			ret = -EFAULT;
		break;
J
Jiri Slaby 已提交
311 312
	case MOXA_FLUSH_QUEUE:
		MoxaPortFlushData(ch, arg);
J
Jiri Slaby 已提交
313
		break;
J
Jiri Slaby 已提交
314 315 316 317 318 319 320 321 322 323
	case MOXA_GET_IOQUEUE: {
		struct moxaq_str __user *argm = argp;
		struct moxaq_str tmp;
		struct moxa_port *p;
		unsigned int i, j;

		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));
A
Alan Cox 已提交
324
				spin_lock_bh(&moxa_lock);
J
Jiri Slaby 已提交
325 326 327 328
				if (moxa_boards[i].ready) {
					tmp.inq = MoxaPortRxQueue(p);
					tmp.outq = MoxaPortTxQueue(p);
				}
A
Alan Cox 已提交
329 330
				spin_unlock_bh(&moxa_lock);
				if (copy_to_user(argm, &tmp, sizeof(tmp)))
J
Jiri Slaby 已提交
331 332 333
					return -EFAULT;
			}
		}
J
Jiri Slaby 已提交
334
		break;
J
Jiri Slaby 已提交
335 336
	} case MOXA_GET_OQUEUE:
		status = MoxaPortTxQueue(ch);
J
Jiri Slaby 已提交
337 338
		ret = put_user(status, (unsigned long __user *)argp);
		break;
J
Jiri Slaby 已提交
339 340
	case MOXA_GET_IQUEUE:
		status = MoxaPortRxQueue(ch);
J
Jiri Slaby 已提交
341 342
		ret = put_user(status, (unsigned long __user *)argp);
		break;
J
Jiri Slaby 已提交
343 344 345 346 347 348 349 350 351
	case MOXA_GETMSTATUS: {
		struct mxser_mstatus __user *argm = argp;
		struct mxser_mstatus tmp;
		struct moxa_port *p;
		unsigned int i, j;

		for (i = 0; i < MAX_BOARDS; i++) {
			p = moxa_boards[i].ports;
			for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
A
Alan Cox 已提交
352
				struct tty_struct *ttyp;
J
Jiri Slaby 已提交
353
				memset(&tmp, 0, sizeof(tmp));
A
Alan Cox 已提交
354 355 356
				spin_lock_bh(&moxa_lock);
				if (!moxa_boards[i].ready) {
				        spin_unlock_bh(&moxa_lock);
J
Jiri Slaby 已提交
357
					goto copy;
A
Alan Cox 已提交
358
                                }
J
Jiri Slaby 已提交
359 360

				status = MoxaPortLineStatus(p);
A
Alan Cox 已提交
361 362
				spin_unlock_bh(&moxa_lock);

J
Jiri Slaby 已提交
363 364 365 366 367 368 369
				if (status & 1)
					tmp.cts = 1;
				if (status & 2)
					tmp.dsr = 1;
				if (status & 4)
					tmp.dcd = 1;

A
Alan Cox 已提交
370
				ttyp = tty_port_tty_get(&p->port);
371
				if (!ttyp)
J
Jiri Slaby 已提交
372 373
					tmp.cflag = p->cflag;
				else
374
					tmp.cflag = ttyp->termios.c_cflag;
375
				tty_kref_put(ttyp);
J
Jiri Slaby 已提交
376
copy:
A
Alan Cox 已提交
377
				if (copy_to_user(argm, &tmp, sizeof(tmp)))
J
Jiri Slaby 已提交
378 379 380
					return -EFAULT;
			}
		}
J
Jiri Slaby 已提交
381
		break;
J
Jiri Slaby 已提交
382 383
	}
	case TIOCGSERIAL:
A
Alan Cox 已提交
384
	        mutex_lock(&ch->port.mutex);
J
Jiri Slaby 已提交
385
		ret = moxa_get_serial_info(ch, argp);
A
Alan Cox 已提交
386
		mutex_unlock(&ch->port.mutex);
J
Jiri Slaby 已提交
387
		break;
J
Jiri Slaby 已提交
388
	case TIOCSSERIAL:
A
Alan Cox 已提交
389
	        mutex_lock(&ch->port.mutex);
J
Jiri Slaby 已提交
390
		ret = moxa_set_serial_info(ch, argp);
A
Alan Cox 已提交
391
		mutex_unlock(&ch->port.mutex);
J
Jiri Slaby 已提交
392 393 394
		break;
	default:
		ret = -ENOIOCTLCMD;
J
Jiri Slaby 已提交
395
	}
J
Jiri Slaby 已提交
396
	return ret;
J
Jiri Slaby 已提交
397 398
}

A
Alan Cox 已提交
399
static int moxa_break_ctl(struct tty_struct *tty, int state)
J
Jiri Slaby 已提交
400 401 402 403 404
{
	struct moxa_port *port = tty->driver_data;

	moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
			Magic_code);
A
Alan Cox 已提交
405
	return 0;
J
Jiri Slaby 已提交
406 407
}

J
Jeff Dike 已提交
408
static const struct tty_operations moxa_ops = {
L
Linus Torvalds 已提交
409 410 411 412 413 414 415 416 417 418 419
	.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,
	.set_termios = moxa_set_termios,
	.stop = moxa_stop,
	.start = moxa_start,
	.hangup = moxa_hangup,
J
Jiri Slaby 已提交
420
	.break_ctl = moxa_break_ctl,
L
Linus Torvalds 已提交
421 422 423 424
	.tiocmget = moxa_tiocmget,
	.tiocmset = moxa_tiocmset,
};

425 426
static const struct tty_port_operations moxa_port_ops = {
	.carrier_raised = moxa_carrier_raised,
A
Alan Cox 已提交
427 428
	.dtr_rts = moxa_dtr_rts,
	.shutdown = moxa_shutdown,
429 430
};

J
Jiri Slaby 已提交
431 432
static struct tty_driver *moxaDriver;
static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
A
Alan Cox 已提交
433

J
Jiri Slaby 已提交
434 435 436 437
/*
 * HW init
 */

J
Jiri Slaby 已提交
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 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
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 已提交
502
			printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
J
Jiri Slaby 已提交
503 504 505 506 507 508 509 510
					"module not found\n");
			return -EIO;
		}
		break;
	}

	return 0;
err:
J
Jiri Slaby 已提交
511
	printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
J
Jiri Slaby 已提交
512 513 514 515 516 517 518 519 520
	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 已提交
521
		printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
J
Jiri Slaby 已提交
522 523 524 525 526 527 528 529 530 531 532 533
		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;
}

534
static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
J
Jiri Slaby 已提交
535 536 537
		size_t len)
{
	void __iomem *baseAddr = brd->basemem;
H
Harvey Harrison 已提交
538
	const __le16 *uptr = ptr;
J
Jiri Slaby 已提交
539
	size_t wlen, len2, j;
540
	unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
541
	unsigned int i, retry;
J
Jiri Slaby 已提交
542 543
	u16 usum, keycode;

544 545
	keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
				C218_KeyCode;
J
Jiri Slaby 已提交
546

547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
	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 已提交
565 566 567 568 569 570 571 572 573 574 575 576 577
	}

	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;
578
			memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
J
Jiri Slaby 已提交
579
			j += len2 << 1;
580 581 582 583 584

			writew(len2, baseAddr + loadlen);
			writew(0, baseAddr + key);
			for (i = 0; i < 100; i++) {
				if (readw(baseAddr + key) == keycode)
J
Jiri Slaby 已提交
585 586 587
					break;
				msleep(10);
			}
588
			if (readw(baseAddr + key) != keycode)
J
Jiri Slaby 已提交
589 590
				return -EIO;
		}
591 592 593 594 595
		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 已提交
596 597 598 599
				break;
			msleep(10);
		}
		retry++;
600 601
	} while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
	if (readb(baseAddr + checksum_ok) != 1)
J
Jiri Slaby 已提交
602 603
		return -EIO;

604
	writew(0, baseAddr + key);
J
Jiri Slaby 已提交
605 606 607 608 609 610 611 612
	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;

613
	if (MOXA_IS_320(brd)) {
614 615 616 617 618 619 620 621 622
		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 已提交
623 624 625 626 627 628 629 630 631 632 633
	}
	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;

634
	if (MOXA_IS_320(brd)) {
635 636 637 638 639 640 641 642 643 644 645 646 647
		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 已提交
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
	}
	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 已提交
664
		printk(KERN_ERR "MOXA: bios length is not even\n");
J
Jiri Slaby 已提交
665 666 667
		return -EINVAL;
	}

668 669 670 671
	retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
	if (retval)
		return retval;

J
Jiri Slaby 已提交
672 673 674 675 676 677
	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 已提交
678
			port->board = brd;
J
Jiri Slaby 已提交
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
			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 已提交
696
			port->board = brd;
J
Jiri Slaby 已提交
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
			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)
{
746
	const void *ptr = fw->data;
J
Jiri Slaby 已提交
747 748 749 750 751 752 753 754 755 756 757 758
	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];
759
	} const *hdr = ptr;
J
Jiri Slaby 已提交
760 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

	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 已提交
786
			printk(KERN_WARNING "MOXA firmware: unexpected input "
J
Jiri Slaby 已提交
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
				"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;
837
	struct moxa_port *p;
838
	unsigned int i, first_idx;
J
Jiri Slaby 已提交
839 840
	int ret;

841 842 843 844 845 846 847 848 849
	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 已提交
850
		tty_port_init(&p->port);
851
		p->port.ops = &moxa_port_ops;
852 853 854 855
		p->type = PORT_16550A;
		p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
	}

J
Jiri Slaby 已提交
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
	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) {
871 872 873 874
		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);
875
		goto err_free;
J
Jiri Slaby 已提交
876 877 878 879 880
	}

	ret = moxa_load_fw(brd, fw);

	release_firmware(fw);
881 882 883 884

	if (ret)
		goto err_free;

J
Jiri Slaby 已提交
885
	spin_lock_bh(&moxa_lock);
886
	brd->ready = 1;
J
Jiri Slaby 已提交
887 888
	if (!timer_pending(&moxaTimer))
		mod_timer(&moxaTimer, jiffies + HZ / 50);
J
Jiri Slaby 已提交
889
	spin_unlock_bh(&moxa_lock);
J
Jiri Slaby 已提交
890

891 892 893 894 895
	first_idx = (brd - moxa_boards) * MAX_PORTS_PER_BOARD;
	for (i = 0; i < brd->numPorts; i++)
		tty_port_register_device(&brd->ports[i].port, moxaDriver,
				first_idx + i, dev);

896 897 898 899
	return 0;
err_free:
	kfree(brd->ports);
err:
J
Jiri Slaby 已提交
900 901 902
	return ret;
}

903 904
static void moxa_board_deinit(struct moxa_board_conf *brd)
{
905
	unsigned int a, opened, first_idx;
J
Jiri Slaby 已提交
906 907

	mutex_lock(&moxa_openlock);
J
Jiri Slaby 已提交
908
	spin_lock_bh(&moxa_lock);
909
	brd->ready = 0;
J
Jiri Slaby 已提交
910
	spin_unlock_bh(&moxa_lock);
J
Jiri Slaby 已提交
911 912 913

	/* pci hot-un-plug support */
	for (a = 0; a < brd->numPorts; a++)
A
Alan Cox 已提交
914 915 916 917 918 919 920 921
		if (brd->ports[a].port.flags & ASYNC_INITIALIZED) {
			struct tty_struct *tty = tty_port_tty_get(
						&brd->ports[a].port);
			if (tty) {
				tty_hangup(tty);
				tty_kref_put(tty);
			}
		}
J
Jiri Slaby 已提交
922 923 924
	while (1) {
		opened = 0;
		for (a = 0; a < brd->numPorts; a++)
A
Alan Cox 已提交
925
			if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
J
Jiri Slaby 已提交
926 927 928 929 930 931 932 933
				opened++;
		mutex_unlock(&moxa_openlock);
		if (!opened)
			break;
		msleep(50);
		mutex_lock(&moxa_openlock);
	}

934 935 936 937
	first_idx = (brd - moxa_boards) * MAX_PORTS_PER_BOARD;
	for (a = 0; a < brd->numPorts; a++)
		tty_unregister_device(moxaDriver, first_idx + a);

938 939 940 941 942
	iounmap(brd->basemem);
	brd->basemem = NULL;
	kfree(brd->ports);
}

L
Linus Torvalds 已提交
943
#ifdef CONFIG_PCI
944 945
static int __devinit moxa_pci_probe(struct pci_dev *pdev,
		const struct pci_device_id *ent)
L
Linus Torvalds 已提交
946
{
947 948 949 950 951 952
	struct moxa_board_conf *board;
	unsigned int i;
	int board_type = ent->driver_data;
	int retval;

	retval = pci_enable_device(pdev);
J
Jiri Slaby 已提交
953 954
	if (retval) {
		dev_err(&pdev->dev, "can't enable pci device\n");
955
		goto err;
J
Jiri Slaby 已提交
956
	}
957 958 959 960 961 962 963

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

	retval = -ENODEV;
	if (i >= MAX_BOARDS) {
J
Jiri Slaby 已提交
964
		dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
965 966 967 968 969
				"found. Board is ignored.\n", MAX_BOARDS);
		goto err;
	}

	board = &moxa_boards[i];
J
Jiri Slaby 已提交
970 971 972 973 974 975 976

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

977
	board->basemem = ioremap_nocache(pci_resource_start(pdev, 2), 0x4000);
J
Jiri Slaby 已提交
978 979
	if (board->basemem == NULL) {
		dev_err(&pdev->dev, "can't remap io space 2\n");
980
		retval = -ENOMEM;
J
Jiri Slaby 已提交
981
		goto err_reg;
J
Jiri Slaby 已提交
982
	}
983

L
Linus Torvalds 已提交
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
	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 已提交
999

J
Jiri Slaby 已提交
1000 1001 1002 1003
	retval = moxa_init_board(board, &pdev->dev);
	if (retval)
		goto err_base;

1004
	pci_set_drvdata(pdev, board);
L
Linus Torvalds 已提交
1005

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

J
Jiri Slaby 已提交
1009
	return 0;
J
Jiri Slaby 已提交
1010 1011 1012
err_base:
	iounmap(board->basemem);
	board->basemem = NULL;
J
Jiri Slaby 已提交
1013 1014
err_reg:
	pci_release_region(pdev, 2);
1015 1016 1017 1018 1019 1020 1021 1022
err:
	return retval;
}

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

1023 1024
	moxa_board_deinit(brd);

J
Jiri Slaby 已提交
1025
	pci_release_region(pdev, 2);
L
Linus Torvalds 已提交
1026
}
J
Jiri Slaby 已提交
1027 1028 1029 1030 1031 1032 1033

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 已提交
1034 1035 1036 1037
#endif /* CONFIG_PCI */

static int __init moxa_init(void)
{
1038
	unsigned int isabrds = 0;
1039
	int retval = 0;
1040 1041
	struct moxa_board_conf *brd = moxa_boards;
	unsigned int i;
L
Linus Torvalds 已提交
1042

J
Jiri Slaby 已提交
1043 1044
	printk(KERN_INFO "MOXA Intellio family driver version %s\n",
			MOXA_VERSION);
1045 1046 1047 1048 1049 1050

	tty_port_init(&moxa_service_port);

	moxaDriver = tty_alloc_driver(MAX_PORTS + 1,
			TTY_DRIVER_REAL_RAW |
			TTY_DRIVER_DYNAMIC_DEV);
1051 1052
	if (IS_ERR(moxaDriver))
		return PTR_ERR(moxaDriver);
L
Linus Torvalds 已提交
1053

1054
	moxaDriver->name = "ttyMX";
L
Linus Torvalds 已提交
1055 1056 1057 1058 1059 1060
	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 已提交
1061 1062
	moxaDriver->init_termios.c_ispeed = 9600;
	moxaDriver->init_termios.c_ospeed = 9600;
L
Linus Torvalds 已提交
1063
	tty_set_operations(moxaDriver, &moxa_ops);
1064 1065
	/* Having one more port only for ioctls is ugly */
	tty_port_link_device(&moxa_service_port, moxaDriver, MAX_PORTS);
L
Linus Torvalds 已提交
1066 1067

	if (tty_register_driver(moxaDriver)) {
J
Jiri Slaby 已提交
1068
		printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
L
Linus Torvalds 已提交
1069 1070 1071 1072
		put_tty_driver(moxaDriver);
		return -1;
	}

1073
	/* Find the boards defined from module args. */
1074

L
Linus Torvalds 已提交
1075
	for (i = 0; i < MAX_BOARDS; i++) {
1076 1077 1078 1079
		if (!baseaddr[i])
			break;
		if (type[i] == MOXA_BOARD_C218_ISA ||
				type[i] == MOXA_BOARD_C320_ISA) {
J
Jiri Slaby 已提交
1080
			pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
1081 1082 1083 1084 1085 1086
					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;
1087
			brd->basemem = ioremap_nocache(baseaddr[i], 0x4000);
1088
			if (!brd->basemem) {
J
Jiri Slaby 已提交
1089
				printk(KERN_ERR "MOXA: can't remap %lx\n",
1090
						baseaddr[i]);
L
Linus Torvalds 已提交
1091 1092
				continue;
			}
J
Jiri Slaby 已提交
1093 1094 1095 1096 1097
			if (moxa_init_board(brd, NULL)) {
				iounmap(brd->basemem);
				brd->basemem = NULL;
				continue;
			}
1098

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

1103 1104
			brd++;
			isabrds++;
L
Linus Torvalds 已提交
1105 1106
		}
	}
J
Jiri Slaby 已提交
1107

L
Linus Torvalds 已提交
1108
#ifdef CONFIG_PCI
J
Jiri Slaby 已提交
1109 1110
	retval = pci_register_driver(&moxa_pci_driver);
	if (retval) {
J
Jiri Slaby 已提交
1111
		printk(KERN_ERR "Can't register MOXA pci driver!\n");
1112
		if (isabrds)
J
Jiri Slaby 已提交
1113
			retval = 0;
L
Linus Torvalds 已提交
1114 1115
	}
#endif
J
Jiri Slaby 已提交
1116 1117

	return retval;
L
Linus Torvalds 已提交
1118 1119 1120 1121
}

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

1124
#ifdef CONFIG_PCI
J
Jiri Slaby 已提交
1125
	pci_unregister_driver(&moxa_pci_driver);
1126
#endif
J
Jiri Slaby 已提交
1127

1128 1129 1130
	for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
		if (moxa_boards[i].ready)
			moxa_board_deinit(&moxa_boards[i]);
J
Jiri Slaby 已提交
1131 1132 1133 1134 1135 1136 1137

	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 已提交
1138 1139 1140 1141 1142
}

module_init(moxa_init);
module_exit(moxa_exit);

A
Alan Cox 已提交
1143
static void moxa_shutdown(struct tty_port *port)
J
Jiri Slaby 已提交
1144
{
A
Alan Cox 已提交
1145 1146
	struct moxa_port *ch = container_of(port, struct moxa_port, port);
        MoxaPortDisable(ch);
J
Jiri Slaby 已提交
1147 1148 1149
	MoxaPortFlushData(ch, 2);
}

1150 1151 1152 1153 1154
static int moxa_carrier_raised(struct tty_port *port)
{
	struct moxa_port *ch = container_of(port, struct moxa_port, port);
	int dcd;

A
Alan Cox 已提交
1155
	spin_lock_irq(&port->lock);
1156
	dcd = ch->DCDState;
A
Alan Cox 已提交
1157
	spin_unlock_irq(&port->lock);
1158 1159 1160
	return dcd;
}

A
Alan Cox 已提交
1161
static void moxa_dtr_rts(struct tty_port *port, int onoff)
J
Jiri Slaby 已提交
1162
{
A
Alan Cox 已提交
1163 1164
	struct moxa_port *ch = container_of(port, struct moxa_port, port);
	MoxaPortLineCtrl(ch, onoff, onoff);
J
Jiri Slaby 已提交
1165 1166
}

A
Alan Cox 已提交
1167

L
Linus Torvalds 已提交
1168 1169
static int moxa_open(struct tty_struct *tty, struct file *filp)
{
1170
	struct moxa_board_conf *brd;
1171
	struct moxa_port *ch;
L
Linus Torvalds 已提交
1172 1173
	int port;

J
Jiri Slaby 已提交
1174
	port = tty->index;
L
Linus Torvalds 已提交
1175
	if (port == MAX_PORTS) {
J
Jiri Slaby 已提交
1176
		return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
L
Linus Torvalds 已提交
1177
	}
J
Jiri Slaby 已提交
1178 1179
	if (mutex_lock_interruptible(&moxa_openlock))
		return -ERESTARTSYS;
1180
	brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
J
Jiri Slaby 已提交
1181 1182
	if (!brd->ready) {
		mutex_unlock(&moxa_openlock);
1183
		return -ENODEV;
J
Jiri Slaby 已提交
1184
	}
L
Linus Torvalds 已提交
1185

1186 1187 1188 1189 1190
	if (port % MAX_PORTS_PER_BOARD >= brd->numPorts) {
		mutex_unlock(&moxa_openlock);
		return -ENODEV;
	}

1191
	ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
A
Alan Cox 已提交
1192
	ch->port.count++;
L
Linus Torvalds 已提交
1193
	tty->driver_data = ch;
A
Alan Cox 已提交
1194
	tty_port_tty_set(&ch->port, tty);
A
Alan Cox 已提交
1195
	mutex_lock(&ch->port.mutex);
A
Alan Cox 已提交
1196
	if (!(ch->port.flags & ASYNC_INITIALIZED)) {
L
Linus Torvalds 已提交
1197
		ch->statusflags = 0;
1198
		moxa_set_tty_param(tty, &tty->termios);
J
Jiri Slaby 已提交
1199 1200
		MoxaPortLineCtrl(ch, 1, 1);
		MoxaPortEnable(ch);
J
Jiri Slaby 已提交
1201
		MoxaSetFifo(ch, ch->type == PORT_16550A);
A
Alan Cox 已提交
1202
		ch->port.flags |= ASYNC_INITIALIZED;
L
Linus Torvalds 已提交
1203
	}
A
Alan Cox 已提交
1204
	mutex_unlock(&ch->port.mutex);
J
Jiri Slaby 已提交
1205
	mutex_unlock(&moxa_openlock);
L
Linus Torvalds 已提交
1206

1207
	return tty_port_block_til_ready(&ch->port, tty, filp);
L
Linus Torvalds 已提交
1208 1209 1210 1211
}

static void moxa_close(struct tty_struct *tty, struct file *filp)
{
A
Alan Cox 已提交
1212
	struct moxa_port *ch = tty->driver_data;
1213
	ch->cflag = tty->termios.c_cflag;
A
Alan Cox 已提交
1214
	tty_port_close(&ch->port, tty, filp);
L
Linus Torvalds 已提交
1215 1216 1217 1218 1219
}

static int moxa_write(struct tty_struct *tty,
		      const unsigned char *buf, int count)
{
J
Jiri Slaby 已提交
1220
	struct moxa_port *ch = tty->driver_data;
1221
	unsigned long flags;
J
Jiri Slaby 已提交
1222
	int len;
L
Linus Torvalds 已提交
1223 1224

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

1227
	spin_lock_irqsave(&moxa_lock, flags);
A
Alan Cox 已提交
1228
	len = MoxaPortWriteData(tty, buf, count);
1229
	spin_unlock_irqrestore(&moxa_lock, flags);
L
Linus Torvalds 已提交
1230

A
Alan Cox 已提交
1231
	set_bit(LOWWAIT, &ch->statusflags);
J
Jiri Slaby 已提交
1232
	return len;
L
Linus Torvalds 已提交
1233 1234 1235 1236
}

static int moxa_write_room(struct tty_struct *tty)
{
1237
	struct moxa_port *ch;
L
Linus Torvalds 已提交
1238 1239

	if (tty->stopped)
J
Jiri Slaby 已提交
1240
		return 0;
J
Jiri Slaby 已提交
1241
	ch = tty->driver_data;
L
Linus Torvalds 已提交
1242
	if (ch == NULL)
J
Jiri Slaby 已提交
1243
		return 0;
J
Jiri Slaby 已提交
1244
	return MoxaPortTxFree(ch);
L
Linus Torvalds 已提交
1245 1246 1247 1248
}

static void moxa_flush_buffer(struct tty_struct *tty)
{
J
Jiri Slaby 已提交
1249
	struct moxa_port *ch = tty->driver_data;
L
Linus Torvalds 已提交
1250 1251 1252

	if (ch == NULL)
		return;
J
Jiri Slaby 已提交
1253
	MoxaPortFlushData(ch, 1);
L
Linus Torvalds 已提交
1254 1255 1256 1257 1258
	tty_wakeup(tty);
}

static int moxa_chars_in_buffer(struct tty_struct *tty)
{
J
Jiri Slaby 已提交
1259
	struct moxa_port *ch = tty->driver_data;
L
Linus Torvalds 已提交
1260 1261
	int chars;

J
Jiri Slaby 已提交
1262
	chars = MoxaPortTxQueue(ch);
A
Alan Cox 已提交
1263
	if (chars)
L
Linus Torvalds 已提交
1264 1265 1266 1267
		/*
		 * Make it possible to wakeup anything waiting for output
		 * in tty_ioctl.c, etc.
		 */
A
Alan Cox 已提交
1268
        	set_bit(EMPTYWAIT, &ch->statusflags);
J
Jiri Slaby 已提交
1269
	return chars;
L
Linus Torvalds 已提交
1270 1271
}

1272
static int moxa_tiocmget(struct tty_struct *tty)
L
Linus Torvalds 已提交
1273
{
A
Alan Cox 已提交
1274
	struct moxa_port *ch = tty->driver_data;
L
Linus Torvalds 已提交
1275 1276
	int flag = 0, dtr, rts;

J
Jiri Slaby 已提交
1277
	MoxaPortGetLineOut(ch, &dtr, &rts);
L
Linus Torvalds 已提交
1278 1279 1280 1281
	if (dtr)
		flag |= TIOCM_DTR;
	if (rts)
		flag |= TIOCM_RTS;
J
Jiri Slaby 已提交
1282
	dtr = MoxaPortLineStatus(ch);
L
Linus Torvalds 已提交
1283 1284 1285 1286 1287 1288 1289 1290 1291
	if (dtr & 1)
		flag |= TIOCM_CTS;
	if (dtr & 2)
		flag |= TIOCM_DSR;
	if (dtr & 4)
		flag |= TIOCM_CD;
	return flag;
}

1292
static int moxa_tiocmset(struct tty_struct *tty,
L
Linus Torvalds 已提交
1293 1294
			 unsigned int set, unsigned int clear)
{
J
Jiri Slaby 已提交
1295
	struct moxa_port *ch;
L
Linus Torvalds 已提交
1296 1297
	int dtr, rts;

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

J
Jiri Slaby 已提交
1305
	MoxaPortGetLineOut(ch, &dtr, &rts);
L
Linus Torvalds 已提交
1306 1307 1308 1309 1310 1311 1312 1313
	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 已提交
1314
	MoxaPortLineCtrl(ch, dtr, rts);
J
Jiri Slaby 已提交
1315
	mutex_unlock(&moxa_openlock);
L
Linus Torvalds 已提交
1316 1317 1318 1319
	return 0;
}

static void moxa_set_termios(struct tty_struct *tty,
J
Jiri Slaby 已提交
1320
		struct ktermios *old_termios)
L
Linus Torvalds 已提交
1321
{
J
Jiri Slaby 已提交
1322
	struct moxa_port *ch = tty->driver_data;
L
Linus Torvalds 已提交
1323 1324 1325

	if (ch == NULL)
		return;
A
Alan Cox 已提交
1326
	moxa_set_tty_param(tty, old_termios);
J
Jiri Slaby 已提交
1327
	if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
A
Alan Cox 已提交
1328
		wake_up_interruptible(&ch->port.open_wait);
L
Linus Torvalds 已提交
1329 1330 1331 1332
}

static void moxa_stop(struct tty_struct *tty)
{
J
Jiri Slaby 已提交
1333
	struct moxa_port *ch = tty->driver_data;
L
Linus Torvalds 已提交
1334 1335 1336

	if (ch == NULL)
		return;
J
Jiri Slaby 已提交
1337
	MoxaPortTxDisable(ch);
A
Alan Cox 已提交
1338
	set_bit(TXSTOPPED, &ch->statusflags);
L
Linus Torvalds 已提交
1339 1340 1341 1342 1343
}


static void moxa_start(struct tty_struct *tty)
{
J
Jiri Slaby 已提交
1344
	struct moxa_port *ch = tty->driver_data;
L
Linus Torvalds 已提交
1345 1346 1347 1348

	if (ch == NULL)
		return;

1349
	if (!test_bit(TXSTOPPED, &ch->statusflags))
L
Linus Torvalds 已提交
1350 1351
		return;

J
Jiri Slaby 已提交
1352
	MoxaPortTxEnable(ch);
A
Alan Cox 已提交
1353
	clear_bit(TXSTOPPED, &ch->statusflags);
L
Linus Torvalds 已提交
1354 1355 1356 1357
}

static void moxa_hangup(struct tty_struct *tty)
{
A
Alan Cox 已提交
1358
	struct moxa_port *ch = tty->driver_data;
A
Alan Cox 已提交
1359
	tty_port_hangup(&ch->port);
L
Linus Torvalds 已提交
1360 1361
}

J
Jiri Slaby 已提交
1362
static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
L
Linus Torvalds 已提交
1363
{
A
Alan Cox 已提交
1364
	struct tty_struct *tty;
A
Alan Cox 已提交
1365
	unsigned long flags;
J
Jiri Slaby 已提交
1366
	dcd = !!dcd;
L
Linus Torvalds 已提交
1367

A
Alan Cox 已提交
1368
	spin_lock_irqsave(&p->port.lock, flags);
A
Alan Cox 已提交
1369
	if (dcd != p->DCDState) {
A
Alan Cox 已提交
1370 1371
        	p->DCDState = dcd;
        	spin_unlock_irqrestore(&p->port.lock, flags);
A
Alan Cox 已提交
1372 1373 1374 1375
		tty = tty_port_tty_get(&p->port);
		if (tty && C_CLOCAL(tty) && !dcd)
			tty_hangup(tty);
		tty_kref_put(tty);
J
Jiri Slaby 已提交
1376
	}
A
Alan Cox 已提交
1377 1378
	else
		spin_unlock_irqrestore(&p->port.lock, flags);
J
Jiri Slaby 已提交
1379
}
L
Linus Torvalds 已提交
1380

J
Jiri Slaby 已提交
1381 1382 1383
static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
		u16 __iomem *ip)
{
A
Alan Cox 已提交
1384
	struct tty_struct *tty = tty_port_tty_get(&p->port);
J
Jiri Slaby 已提交
1385
	void __iomem *ofsAddr;
A
Alan Cox 已提交
1386
	unsigned int inited = p->port.flags & ASYNC_INITIALIZED;
J
Jiri Slaby 已提交
1387 1388 1389
	u16 intr;

	if (tty) {
A
Alan Cox 已提交
1390
		if (test_bit(EMPTYWAIT, &p->statusflags) &&
J
Jiri Slaby 已提交
1391
				MoxaPortTxQueue(p) == 0) {
A
Alan Cox 已提交
1392
			clear_bit(EMPTYWAIT, &p->statusflags);
J
Jiri Slaby 已提交
1393 1394
			tty_wakeup(tty);
		}
A
Alan Cox 已提交
1395
		if (test_bit(LOWWAIT, &p->statusflags) && !tty->stopped &&
J
Jiri Slaby 已提交
1396
				MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
A
Alan Cox 已提交
1397
			clear_bit(LOWWAIT, &p->statusflags);
J
Jiri Slaby 已提交
1398 1399 1400
			tty_wakeup(tty);
		}

A
Alan Cox 已提交
1401
		if (inited && !test_bit(TTY_THROTTLED, &tty->flags) &&
J
Jiri Slaby 已提交
1402 1403 1404 1405 1406
				MoxaPortRxQueue(p) > 0) { /* RX */
			MoxaPortReadData(p);
			tty_schedule_flip(tty);
		}
	} else {
A
Alan Cox 已提交
1407
		clear_bit(EMPTYWAIT, &p->statusflags);
J
Jiri Slaby 已提交
1408
		MoxaPortFlushData(p, 0); /* flush RX */
L
Linus Torvalds 已提交
1409
	}
J
Jiri Slaby 已提交
1410

J
Jiri Slaby 已提交
1411
	if (!handle) /* nothing else to do */
1412
		goto put;
J
Jiri Slaby 已提交
1413 1414 1415

	intr = readw(ip); /* port irq status */
	if (intr == 0)
1416
		goto put;
J
Jiri Slaby 已提交
1417 1418 1419 1420 1421 1422 1423 1424

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

	if (!inited)
1425
		goto put;
J
Jiri Slaby 已提交
1426 1427 1428 1429 1430 1431 1432 1433

	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);
1434 1435
put:
	tty_kref_put(tty);
J
Jiri Slaby 已提交
1436 1437 1438 1439 1440 1441 1442 1443

	return 0;
}

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

	spin_lock(&moxa_lock);
L
Linus Torvalds 已提交
1447
	for (card = 0; card < MAX_BOARDS; card++) {
J
Jiri Slaby 已提交
1448 1449
		brd = &moxa_boards[card];
		if (!brd->ready)
L
Linus Torvalds 已提交
1450
			continue;
J
Jiri Slaby 已提交
1451

J
Jiri Slaby 已提交
1452 1453
		served++;

J
Jiri Slaby 已提交
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469
		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 已提交
1470 1471 1472
				}
		}
	}
J
Jiri Slaby 已提交
1473
	moxaLowWaterChk = 0;
L
Linus Torvalds 已提交
1474

J
Jiri Slaby 已提交
1475 1476 1477
	if (served)
		mod_timer(&moxaTimer, jiffies + HZ / 50);
	spin_unlock(&moxa_lock);
L
Linus Torvalds 已提交
1478 1479 1480 1481
}

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

A
Alan Cox 已提交
1482
static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
L
Linus Torvalds 已提交
1483
{
1484
	register struct ktermios *ts = &tty->termios;
J
Jiri Slaby 已提交
1485
	struct moxa_port *ch = tty->driver_data;
A
Alan Cox 已提交
1486
	int rts, cts, txflow, rxflow, xany, baud;
L
Linus Torvalds 已提交
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496

	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 已提交
1497 1498 1499

	/* Clear the features we don't support */
	ts->c_cflag &= ~CMSPAR;
J
Jiri Slaby 已提交
1500 1501
	MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
	baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
A
Alan Cox 已提交
1502 1503 1504 1505
	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 已提交
1506 1507 1508 1509 1510 1511
}

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

J
Jiri Slaby 已提交
1512
static void MoxaPortFlushData(struct moxa_port *port, int mode)
L
Linus Torvalds 已提交
1513 1514
{
	void __iomem *ofsAddr;
J
Jiri Slaby 已提交
1515
	if (mode < 0 || mode > 2)
L
Linus Torvalds 已提交
1516
		return;
J
Jiri Slaby 已提交
1517
	ofsAddr = port->tableAddr;
L
Linus Torvalds 已提交
1518 1519
	moxafunc(ofsAddr, FC_FlushQueue, mode);
	if (mode != 1) {
J
Jiri Slaby 已提交
1520
		port->lowChkFlag = 0;
J
Jiri Slaby 已提交
1521
		moxa_low_water_check(ofsAddr);
L
Linus Torvalds 已提交
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
	}
}

/*
 *    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:
1575
 *      speed_t MoxaPortSetBaud(int port, speed_t baud);
L
Linus Torvalds 已提交
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
 *           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 已提交
1588
 *      int  MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
L
Linus Torvalds 已提交
1589
 *           int port           : port number (0 - 127)
A
Alan Cox 已提交
1590
 *           struct ktermios * termio : termio structure pointer
1591
 *	     speed_t baud	: baud rate
L
Linus Torvalds 已提交
1592 1593 1594 1595 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 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
 *
 *           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 已提交
1664
 *      int  MoxaPortReadData(int port, struct tty_struct *tty);
L
Linus Torvalds 已提交
1665
 *           int port           : port number (0 - 127)
A
Alan Cox 已提交
1666
 *	     struct tty_struct *tty : tty for data
L
Linus Torvalds 已提交
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
 *
 *           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 已提交
1717
static void MoxaPortEnable(struct moxa_port *port)
L
Linus Torvalds 已提交
1718 1719
{
	void __iomem *ofsAddr;
J
Jiri Slaby 已提交
1720
	u16 lowwater = 512;
L
Linus Torvalds 已提交
1721

J
Jiri Slaby 已提交
1722
	ofsAddr = port->tableAddr;
L
Linus Torvalds 已提交
1723
	writew(lowwater, ofsAddr + Low_water);
1724
	if (MOXA_IS_320(port->board))
L
Linus Torvalds 已提交
1725
		moxafunc(ofsAddr, FC_SetBreakIrq, 0);
J
Jiri Slaby 已提交
1726 1727 1728
	else
		writew(readw(ofsAddr + HostStat) | WakeupBreak,
				ofsAddr + HostStat);
L
Linus Torvalds 已提交
1729 1730 1731 1732 1733 1734 1735 1736

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

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

J
Jiri Slaby 已提交
1737
static void MoxaPortDisable(struct moxa_port *port)
L
Linus Torvalds 已提交
1738
{
J
Jiri Slaby 已提交
1739
	void __iomem *ofsAddr = port->tableAddr;
L
Linus Torvalds 已提交
1740 1741 1742 1743 1744 1745 1746

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

1747
static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
L
Linus Torvalds 已提交
1748
{
1749 1750 1751
	void __iomem *ofsAddr = port->tableAddr;
	unsigned int clock, val;
	speed_t max;
L
Linus Torvalds 已提交
1752

1753 1754
	max = MOXA_IS_320(port->board) ? 460800 : 921600;
	if (baud < 50)
J
Jiri Slaby 已提交
1755
		return 0;
L
Linus Torvalds 已提交
1756 1757
	if (baud > max)
		baud = max;
1758
	clock = 921600;
L
Linus Torvalds 已提交
1759 1760 1761
	val = clock / baud;
	moxafunc(ofsAddr, FC_SetBaud, val);
	baud = clock / val;
J
Jiri Slaby 已提交
1762
	return baud;
L
Linus Torvalds 已提交
1763 1764
}

J
Jiri Slaby 已提交
1765 1766
static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
		speed_t baud)
L
Linus Torvalds 已提交
1767 1768 1769 1770
{
	void __iomem *ofsAddr;
	tcflag_t mode = 0;

J
Jiri Slaby 已提交
1771
	ofsAddr = port->tableAddr;
L
Linus Torvalds 已提交
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

	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 已提交
1799
	moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
L
Linus Torvalds 已提交
1800

1801 1802 1803
	if (MOXA_IS_320(port->board) && baud >= 921600)
		return -1;

A
Alan Cox 已提交
1804
	baud = MoxaPortSetBaud(port, baud);
L
Linus Torvalds 已提交
1805 1806

	if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
A
Alan Cox 已提交
1807
	        spin_lock_irq(&moxafunc_lock);
L
Linus Torvalds 已提交
1808 1809 1810
		writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
		writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
		writeb(FC_SetXonXoff, ofsAddr + FuncCode);
J
Jiri Slaby 已提交
1811
		moxa_wait_finish(ofsAddr);
A
Alan Cox 已提交
1812
		spin_unlock_irq(&moxafunc_lock);
L
Linus Torvalds 已提交
1813 1814

	}
J
Jiri Slaby 已提交
1815
	return baud;
L
Linus Torvalds 已提交
1816 1817
}

J
Jiri Slaby 已提交
1818 1819
static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
		int *rtsState)
L
Linus Torvalds 已提交
1820
{
J
Jiri Slaby 已提交
1821 1822 1823 1824 1825
	if (dtrState)
		*dtrState = !!(port->lineCtrl & DTR_ON);
	if (rtsState)
		*rtsState = !!(port->lineCtrl & RTS_ON);

J
Jiri Slaby 已提交
1826
	return 0;
L
Linus Torvalds 已提交
1827 1828
}

J
Jiri Slaby 已提交
1829
static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
L
Linus Torvalds 已提交
1830
{
J
Jiri Slaby 已提交
1831
	u8 mode = 0;
L
Linus Torvalds 已提交
1832 1833 1834 1835 1836

	if (dtr)
		mode |= DTR_ON;
	if (rts)
		mode |= RTS_ON;
J
Jiri Slaby 已提交
1837 1838
	port->lineCtrl = mode;
	moxafunc(port->tableAddr, FC_LineControl, mode);
L
Linus Torvalds 已提交
1839 1840
}

J
Jiri Slaby 已提交
1841 1842
static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
		int txflow, int rxflow, int txany)
L
Linus Torvalds 已提交
1843
{
J
Jiri Slaby 已提交
1844
	int mode = 0;
L
Linus Torvalds 已提交
1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855

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

J
Jiri Slaby 已提交
1859
static int MoxaPortLineStatus(struct moxa_port *port)
L
Linus Torvalds 已提交
1860 1861 1862 1863
{
	void __iomem *ofsAddr;
	int val;

J
Jiri Slaby 已提交
1864
	ofsAddr = port->tableAddr;
A
Alan Cox 已提交
1865 1866 1867
	if (MOXA_IS_320(port->board))
		val = moxafuncret(ofsAddr, FC_LineStatus, 0);
	else
L
Linus Torvalds 已提交
1868 1869
		val = readw(ofsAddr + FlagStat) >> 4;
	val &= 0x0B;
J
Jiri Slaby 已提交
1870
	if (val & 8)
L
Linus Torvalds 已提交
1871
		val |= 4;
J
Jiri Slaby 已提交
1872
	moxa_new_dcdstate(port, val & 8);
L
Linus Torvalds 已提交
1873
	val &= 7;
J
Jiri Slaby 已提交
1874
	return val;
L
Linus Torvalds 已提交
1875 1876
}

A
Alan Cox 已提交
1877
static int MoxaPortWriteData(struct tty_struct *tty,
J
Jiri Slaby 已提交
1878
		const unsigned char *buffer, int len)
L
Linus Torvalds 已提交
1879
{
A
Alan Cox 已提交
1880
	struct moxa_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1881
	void __iomem *baseAddr, *ofsAddr, *ofs;
J
Jiri Slaby 已提交
1882 1883 1884
	unsigned int c, total;
	u16 head, tail, tx_mask, spage, epage;
	u16 pageno, pageofs, bufhead;
L
Linus Torvalds 已提交
1885

J
Jiri Slaby 已提交
1886 1887
	ofsAddr = port->tableAddr;
	baseAddr = port->board->basemem;
L
Linus Torvalds 已提交
1888 1889 1890 1891 1892
	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 已提交
1893
	c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
L
Linus Torvalds 已提交
1894 1895
	if (c > len)
		c = len;
A
Alan Cox 已提交
1896
	moxaLog.txcnt[port->port.tty->index] += c;
L
Linus Torvalds 已提交
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907
	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 已提交
1908 1909
			memcpy_toio(ofs, buffer, len);
			buffer += len;
L
Linus Torvalds 已提交
1910 1911 1912 1913 1914 1915
			tail = (tail + len) & tx_mask;
			c -= len;
		}
	} else {
		pageno = spage + (tail >> 13);
		pageofs = tail & Page_mask;
J
Jiri Slaby 已提交
1916 1917 1918 1919
		while (c > 0) {
			len = Page_size - pageofs;
			if (len > c)
				len = c;
L
Linus Torvalds 已提交
1920 1921
			writeb(pageno, baseAddr + Control_reg);
			ofs = baseAddr + DynPage_addr + pageofs;
J
Jiri Slaby 已提交
1922 1923
			memcpy_toio(ofs, buffer, len);
			buffer += len;
L
Linus Torvalds 已提交
1924 1925 1926
			if (++pageno == epage)
				pageno = spage;
			pageofs = 0;
J
Jiri Slaby 已提交
1927 1928 1929
			c -= len;
		}
		tail = (tail + total) & tx_mask;
L
Linus Torvalds 已提交
1930
	}
J
Jiri Slaby 已提交
1931
	writew(tail, ofsAddr + TXwptr);
L
Linus Torvalds 已提交
1932
	writeb(1, ofsAddr + CD180TXirq);	/* start to send */
J
Jiri Slaby 已提交
1933
	return total;
L
Linus Torvalds 已提交
1934 1935
}

J
Jiri Slaby 已提交
1936
static int MoxaPortReadData(struct moxa_port *port)
L
Linus Torvalds 已提交
1937
{
A
Alan Cox 已提交
1938
	struct tty_struct *tty = port->port.tty;
J
Jiri Slaby 已提交
1939
	unsigned char *dst;
L
Linus Torvalds 已提交
1940
	void __iomem *baseAddr, *ofsAddr, *ofs;
J
Jiri Slaby 已提交
1941 1942 1943
	unsigned int count, len, total;
	u16 tail, rx_mask, spage, epage;
	u16 pageno, pageofs, bufhead, head;
L
Linus Torvalds 已提交
1944

J
Jiri Slaby 已提交
1945 1946
	ofsAddr = port->tableAddr;
	baseAddr = port->board->basemem;
L
Linus Torvalds 已提交
1947 1948 1949 1950 1951
	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 已提交
1952
	count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
L
Linus Torvalds 已提交
1953
	if (count == 0)
A
Alan Cox 已提交
1954
		return 0;
L
Linus Torvalds 已提交
1955

A
Alan Cox 已提交
1956
	total = count;
J
Jiri Slaby 已提交
1957
	moxaLog.rxcnt[tty->index] += total;
L
Linus Torvalds 已提交
1958 1959 1960 1961 1962
	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 已提交
1963 1964 1965 1966 1967
			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 已提交
1968 1969 1970 1971 1972 1973
			head = (head + len) & rx_mask;
			count -= len;
		}
	} else {
		pageno = spage + (head >> 13);
		pageofs = head & Page_mask;
J
Jiri Slaby 已提交
1974
		while (count > 0) {
L
Linus Torvalds 已提交
1975 1976
			writew(pageno, baseAddr + Control_reg);
			ofs = baseAddr + DynPage_addr + pageofs;
J
Jiri Slaby 已提交
1977 1978 1979 1980 1981 1982 1983
			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 已提交
1984
				pageno = spage;
J
Jiri Slaby 已提交
1985 1986
		}
		head = (head + total) & rx_mask;
L
Linus Torvalds 已提交
1987
	}
J
Jiri Slaby 已提交
1988 1989
	writew(head, ofsAddr + RXrptr);
	if (readb(ofsAddr + FlagStat) & Xoff_state) {
L
Linus Torvalds 已提交
1990
		moxaLowWaterChk = 1;
J
Jiri Slaby 已提交
1991
		port->lowChkFlag = 1;
L
Linus Torvalds 已提交
1992
	}
J
Jiri Slaby 已提交
1993
	return total;
L
Linus Torvalds 已提交
1994 1995 1996
}


J
Jiri Slaby 已提交
1997
static int MoxaPortTxQueue(struct moxa_port *port)
L
Linus Torvalds 已提交
1998
{
J
Jiri Slaby 已提交
1999
	void __iomem *ofsAddr = port->tableAddr;
J
Jiri Slaby 已提交
2000
	u16 rptr, wptr, mask;
L
Linus Torvalds 已提交
2001 2002 2003 2004

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

J
Jiri Slaby 已提交
2008
static int MoxaPortTxFree(struct moxa_port *port)
L
Linus Torvalds 已提交
2009
{
J
Jiri Slaby 已提交
2010
	void __iomem *ofsAddr = port->tableAddr;
J
Jiri Slaby 已提交
2011
	u16 rptr, wptr, mask;
L
Linus Torvalds 已提交
2012 2013 2014 2015

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

J
Jiri Slaby 已提交
2019
static int MoxaPortRxQueue(struct moxa_port *port)
L
Linus Torvalds 已提交
2020
{
J
Jiri Slaby 已提交
2021
	void __iomem *ofsAddr = port->tableAddr;
J
Jiri Slaby 已提交
2022
	u16 rptr, wptr, mask;
L
Linus Torvalds 已提交
2023 2024 2025 2026

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

J
Jiri Slaby 已提交
2030
static void MoxaPortTxDisable(struct moxa_port *port)
L
Linus Torvalds 已提交
2031
{
J
Jiri Slaby 已提交
2032
	moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
L
Linus Torvalds 已提交
2033 2034
}

J
Jiri Slaby 已提交
2035
static void MoxaPortTxEnable(struct moxa_port *port)
L
Linus Torvalds 已提交
2036
{
J
Jiri Slaby 已提交
2037
	moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
L
Linus Torvalds 已提交
2038 2039
}

2040
static int moxa_get_serial_info(struct moxa_port *info,
J
Jiri Slaby 已提交
2041
		struct serial_struct __user *retinfo)
L
Linus Torvalds 已提交
2042
{
J
Jiri Slaby 已提交
2043 2044
	struct serial_struct tmp = {
		.type = info->type,
A
Alan Cox 已提交
2045 2046
		.line = info->port.tty->index,
		.flags = info->port.flags,
J
Jiri Slaby 已提交
2047
		.baud_base = 921600,
A
Alan Cox 已提交
2048
		.close_delay = info->port.close_delay
J
Jiri Slaby 已提交
2049 2050
	};
	return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
L
Linus Torvalds 已提交
2051 2052 2053
}


2054
static int moxa_set_serial_info(struct moxa_port *info,
J
Jiri Slaby 已提交
2055
		struct serial_struct __user *new_info)
L
Linus Torvalds 已提交
2056 2057 2058
{
	struct serial_struct new_serial;

J
Jiri Slaby 已提交
2059
	if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
L
Linus Torvalds 已提交
2060 2061
		return -EFAULT;

J
Jiri Slaby 已提交
2062 2063 2064 2065
	if (new_serial.irq != 0 || new_serial.port != 0 ||
			new_serial.custom_divisor != 0 ||
			new_serial.baud_base != 921600)
		return -EPERM;
L
Linus Torvalds 已提交
2066 2067 2068

	if (!capable(CAP_SYS_ADMIN)) {
		if (((new_serial.flags & ~ASYNC_USR_MASK) !=
A
Alan Cox 已提交
2069
		     (info->port.flags & ~ASYNC_USR_MASK)))
J
Jiri Slaby 已提交
2070 2071
			return -EPERM;
	} else
A
Alan Cox 已提交
2072
		info->port.close_delay = new_serial.close_delay * HZ / 100;
L
Linus Torvalds 已提交
2073 2074

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

J
Jiri Slaby 已提交
2077
	MoxaSetFifo(info, new_serial.type == PORT_16550A);
L
Linus Torvalds 已提交
2078 2079

	info->type = new_serial.type;
J
Jiri Slaby 已提交
2080
	return 0;
L
Linus Torvalds 已提交
2081 2082 2083 2084 2085 2086 2087 2088
}



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

J
Jiri Slaby 已提交
2089
static void MoxaSetFifo(struct moxa_port *port, int enable)
L
Linus Torvalds 已提交
2090
{
J
Jiri Slaby 已提交
2091
	void __iomem *ofsAddr = port->tableAddr;
L
Linus Torvalds 已提交
2092 2093 2094 2095 2096 2097 2098 2099 2100

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