n_r3964.c 30.7 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
/* r3964 linediscipline for linux
 *
 * -----------------------------------------------------------
 * Copyright by 
 * Philips Automation Projects
 * Kassel (Germany)
 * -----------------------------------------------------------
 * This software may be used and distributed according to the terms of
 * the GNU General Public License, incorporated herein by reference.
 *
 * Author:
 * L. Haag
 *
 * $Log: n_r3964.c,v $
 * Revision 1.10  2001/03/18 13:02:24  dwmw2
 * Fix timer usage, use spinlocks properly.
 *
 * Revision 1.9  2001/03/18 12:52:14  dwmw2
 * Merge changes in 2.4.2
 *
 * Revision 1.8  2000/03/23 14:14:54  dwmw2
 * Fix race in sleeping in r3964_read()
 *
 * Revision 1.7  1999/28/08 11:41:50  dwmw2
 * Port to 2.3 kernel
 *
 * Revision 1.6  1998/09/30 00:40:40  dwmw2
 * Fixed compilation on 2.0.x kernels
 * Updated to newly registered tty-ldisc number 9
 *
 * Revision 1.5  1998/09/04 21:57:36  dwmw2
 * Signal handling bug fixes, port to 2.1.x.
 *
 * Revision 1.4  1998/04/02 20:26:59  lhaag
 * select, blocking, ...
 *
 * Revision 1.3  1998/02/12 18:58:43  root
 * fixed some memory leaks
 * calculation of checksum characters
 *
 * Revision 1.2  1998/02/07 13:03:34  root
 * ioctl read_telegram
 *
 * Revision 1.1  1998/02/06 19:21:03  root
 * Initial revision
 *
 *
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/interrupt.h>
#include <linux/ptrace.h>
#include <linux/ioport.h>
#include <linux/in.h>
#include <linux/slab.h>
Alexey Dobriyan's avatar
Alexey Dobriyan committed
60
#include <linux/smp_lock.h>
Linus Torvalds's avatar
Linus Torvalds committed
61 62
#include <linux/tty.h>
#include <linux/errno.h>
Jiri Slaby's avatar
Jiri Slaby committed
63 64
#include <linux/string.h>	/* used in new tty drivers */
#include <linux/signal.h>	/* used in new tty drivers */
Linus Torvalds's avatar
Linus Torvalds committed
65 66 67 68 69 70
#include <linux/ioctl.h>
#include <linux/n_r3964.h>
#include <linux/poll.h>
#include <linux/init.h>
#include <asm/uaccess.h>

Jiri Slaby's avatar
Jiri Slaby committed
71
/*#define DEBUG_QUEUE*/
Linus Torvalds's avatar
Linus Torvalds committed
72 73

/* Log successful handshake and protocol operations  */
Jiri Slaby's avatar
Jiri Slaby committed
74
/*#define DEBUG_PROTO_S*/
Linus Torvalds's avatar
Linus Torvalds committed
75 76

/* Log handshake and protocol errors: */
Jiri Slaby's avatar
Jiri Slaby committed
77
/*#define DEBUG_PROTO_E*/
Linus Torvalds's avatar
Linus Torvalds committed
78 79

/* Log Linediscipline operations (open, close, read, write...): */
Jiri Slaby's avatar
Jiri Slaby committed
80
/*#define DEBUG_LDISC*/
Linus Torvalds's avatar
Linus Torvalds committed
81 82

/* Log module and memory operations (init, cleanup; kmalloc, kfree): */
Jiri Slaby's avatar
Jiri Slaby committed
83
/*#define DEBUG_MODUL*/
Linus Torvalds's avatar
Linus Torvalds committed
84 85

/* Macro helpers for debug output: */
Jiri Slaby's avatar
Jiri Slaby committed
86
#define TRACE(format, args...) printk("r3964: " format "\n" , ## args)
Linus Torvalds's avatar
Linus Torvalds committed
87 88

#ifdef DEBUG_MODUL
Jiri Slaby's avatar
Jiri Slaby committed
89
#define TRACE_M(format, args...) printk("r3964: " format "\n" , ## args)
Linus Torvalds's avatar
Linus Torvalds committed
90
#else
Jiri Slaby's avatar
Jiri Slaby committed
91
#define TRACE_M(fmt, arg...) do {} while (0)
Linus Torvalds's avatar
Linus Torvalds committed
92 93
#endif
#ifdef DEBUG_PROTO_S
Jiri Slaby's avatar
Jiri Slaby committed
94
#define TRACE_PS(format, args...) printk("r3964: " format "\n" , ## args)
Linus Torvalds's avatar
Linus Torvalds committed
95
#else
Jiri Slaby's avatar
Jiri Slaby committed
96
#define TRACE_PS(fmt, arg...) do {} while (0)
Linus Torvalds's avatar
Linus Torvalds committed
97 98
#endif
#ifdef DEBUG_PROTO_E
Jiri Slaby's avatar
Jiri Slaby committed
99
#define TRACE_PE(format, args...) printk("r3964: " format "\n" , ## args)
Linus Torvalds's avatar
Linus Torvalds committed
100
#else
Jiri Slaby's avatar
Jiri Slaby committed
101
#define TRACE_PE(fmt, arg...) do {} while (0)
Linus Torvalds's avatar
Linus Torvalds committed
102 103
#endif
#ifdef DEBUG_LDISC
Jiri Slaby's avatar
Jiri Slaby committed
104
#define TRACE_L(format, args...) printk("r3964: " format "\n" , ## args)
Linus Torvalds's avatar
Linus Torvalds committed
105
#else
Jiri Slaby's avatar
Jiri Slaby committed
106
#define TRACE_L(fmt, arg...) do {} while (0)
Linus Torvalds's avatar
Linus Torvalds committed
107 108
#endif
#ifdef DEBUG_QUEUE
Jiri Slaby's avatar
Jiri Slaby committed
109
#define TRACE_Q(format, args...) printk("r3964: " format "\n" , ## args)
Linus Torvalds's avatar
Linus Torvalds committed
110
#else
Jiri Slaby's avatar
Jiri Slaby committed
111
#define TRACE_Q(fmt, arg...) do {} while (0)
Linus Torvalds's avatar
Linus Torvalds committed
112 113 114 115 116 117 118 119 120 121
#endif
static void add_tx_queue(struct r3964_info *, struct r3964_block_header *);
static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code);
static void put_char(struct r3964_info *pInfo, unsigned char ch);
static void trigger_transmit(struct r3964_info *pInfo);
static void retry_transmit(struct r3964_info *pInfo);
static void transmit_block(struct r3964_info *pInfo);
static void receive_char(struct r3964_info *pInfo, const unsigned char c);
static void receive_error(struct r3964_info *pInfo, const char flag);
static void on_timeout(unsigned long priv);
122
static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg);
Jiri Slaby's avatar
Jiri Slaby committed
123 124
static int read_telegram(struct r3964_info *pInfo, struct pid *pid,
		unsigned char __user * buf);
Linus Torvalds's avatar
Linus Torvalds committed
125
static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
Jiri Slaby's avatar
Jiri Slaby committed
126 127 128 129 130
		int error_code, struct r3964_block_header *pBlock);
static struct r3964_message *remove_msg(struct r3964_info *pInfo,
		struct r3964_client_info *pClient);
static void remove_client_block(struct r3964_info *pInfo,
		struct r3964_client_info *pClient);
Linus Torvalds's avatar
Linus Torvalds committed
131

Jiri Slaby's avatar
Jiri Slaby committed
132
static int r3964_open(struct tty_struct *tty);
Linus Torvalds's avatar
Linus Torvalds committed
133 134
static void r3964_close(struct tty_struct *tty);
static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
Jiri Slaby's avatar
Jiri Slaby committed
135 136 137 138 139 140 141 142
		unsigned char __user * buf, size_t nr);
static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
		const unsigned char *buf, size_t nr);
static int r3964_ioctl(struct tty_struct *tty, struct file *file,
		unsigned int cmd, unsigned long arg);
static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old);
static unsigned int r3964_poll(struct tty_struct *tty, struct file *file,
		struct poll_table_struct *wait);
Linus Torvalds's avatar
Linus Torvalds committed
143
static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
Jiri Slaby's avatar
Jiri Slaby committed
144
		char *fp, int count);
Linus Torvalds's avatar
Linus Torvalds committed
145

Alan Cox's avatar
Alan Cox committed
146
static struct tty_ldisc_ops tty_ldisc_N_R3964 = {
Jiri Slaby's avatar
Jiri Slaby committed
147 148 149 150 151 152 153 154
	.owner = THIS_MODULE,
	.magic = TTY_LDISC_MAGIC,
	.name = "R3964",
	.open = r3964_open,
	.close = r3964_close,
	.read = r3964_read,
	.write = r3964_write,
	.ioctl = r3964_ioctl,
Linus Torvalds's avatar
Linus Torvalds committed
155
	.set_termios = r3964_set_termios,
Jiri Slaby's avatar
Jiri Slaby committed
156
	.poll = r3964_poll,
Linus Torvalds's avatar
Linus Torvalds committed
157 158 159 160 161
	.receive_buf = r3964_receive_buf,
};

static void dump_block(const unsigned char *block, unsigned int length)
{
Jiri Slaby's avatar
Jiri Slaby committed
162 163 164 165 166 167 168 169 170 171
	unsigned int i, j;
	char linebuf[16 * 3 + 1];

	for (i = 0; i < length; i += 16) {
		for (j = 0; (j < 16) && (j + i < length); j++) {
			sprintf(linebuf + 3 * j, "%02x ", block[i + j]);
		}
		linebuf[3 * j] = '\0';
		TRACE_PS("%s", linebuf);
	}
Linus Torvalds's avatar
Linus Torvalds committed
172 173 174 175 176 177 178 179 180 181 182 183
}

/*************************************************************
 * Driver initialisation
 *************************************************************/

/*************************************************************
 * Module support routines
 *************************************************************/

static void __exit r3964_exit(void)
{
Jiri Slaby's avatar
Jiri Slaby committed
184 185 186 187 188 189 190 191 192 193 194 195
	int status;

	TRACE_M("cleanup_module()");

	status = tty_unregister_ldisc(N_R3964);

	if (status != 0) {
		printk(KERN_ERR "r3964: error unregistering linediscipline: "
				"%d\n", status);
	} else {
		TRACE_L("linediscipline successfully unregistered");
	}
Linus Torvalds's avatar
Linus Torvalds committed
196 197 198 199
}

static int __init r3964_init(void)
{
Jiri Slaby's avatar
Jiri Slaby committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
	int status;

	printk("r3964: Philips r3964 Driver $Revision: 1.10 $\n");

	/*
	 * Register the tty line discipline
	 */

	status = tty_register_ldisc(N_R3964, &tty_ldisc_N_R3964);
	if (status == 0) {
		TRACE_L("line discipline %d registered", N_R3964);
		TRACE_L("flags=%x num=%x", tty_ldisc_N_R3964.flags,
			tty_ldisc_N_R3964.num);
		TRACE_L("open=%p", tty_ldisc_N_R3964.open);
		TRACE_L("tty_ldisc_N_R3964 = %p", &tty_ldisc_N_R3964);
	} else {
		printk(KERN_ERR "r3964: error registering line discipline: "
				"%d\n", status);
	}
	return status;
Linus Torvalds's avatar
Linus Torvalds committed
220 221 222 223 224 225 226 227 228
}

module_init(r3964_init);
module_exit(r3964_exit);

/*************************************************************
 * Protocol implementation routines
 *************************************************************/

Jiri Slaby's avatar
Jiri Slaby committed
229 230
static void add_tx_queue(struct r3964_info *pInfo,
			 struct r3964_block_header *pHeader)
Linus Torvalds's avatar
Linus Torvalds committed
231
{
Jiri Slaby's avatar
Jiri Slaby committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
	unsigned long flags;

	spin_lock_irqsave(&pInfo->lock, flags);

	pHeader->next = NULL;

	if (pInfo->tx_last == NULL) {
		pInfo->tx_first = pInfo->tx_last = pHeader;
	} else {
		pInfo->tx_last->next = pHeader;
		pInfo->tx_last = pHeader;
	}

	spin_unlock_irqrestore(&pInfo->lock, flags);

	TRACE_Q("add_tx_queue %p, length %d, tx_first = %p",
		pHeader, pHeader->length, pInfo->tx_first);
Linus Torvalds's avatar
Linus Torvalds committed
249 250 251 252
}

static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code)
{
Jiri Slaby's avatar
Jiri Slaby committed
253 254
	struct r3964_block_header *pHeader;
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
255
#ifdef DEBUG_QUEUE
Jiri Slaby's avatar
Jiri Slaby committed
256
	struct r3964_block_header *pDump;
Linus Torvalds's avatar
Linus Torvalds committed
257 258
#endif

Jiri Slaby's avatar
Jiri Slaby committed
259 260 261 262
	pHeader = pInfo->tx_first;

	if (pHeader == NULL)
		return;
Linus Torvalds's avatar
Linus Torvalds committed
263 264

#ifdef DEBUG_QUEUE
Jiri Slaby's avatar
Jiri Slaby committed
265 266 267 268 269
	printk("r3964: remove_from_tx_queue: %p, length %u - ",
		pHeader, pHeader->length);
	for (pDump = pHeader; pDump; pDump = pDump->next)
		printk("%p ", pDump);
	printk("\n");
Linus Torvalds's avatar
Linus Torvalds committed
270 271
#endif

Jiri Slaby's avatar
Jiri Slaby committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
	if (pHeader->owner) {
		if (error_code) {
			add_msg(pHeader->owner, R3964_MSG_ACK, 0,
				error_code, NULL);
		} else {
			add_msg(pHeader->owner, R3964_MSG_ACK, pHeader->length,
				error_code, NULL);
		}
		wake_up_interruptible(&pInfo->read_wait);
	}

	spin_lock_irqsave(&pInfo->lock, flags);

	pInfo->tx_first = pHeader->next;
	if (pInfo->tx_first == NULL) {
		pInfo->tx_last = NULL;
	}

	spin_unlock_irqrestore(&pInfo->lock, flags);

	kfree(pHeader);
	TRACE_M("remove_from_tx_queue - kfree %p", pHeader);
Linus Torvalds's avatar
Linus Torvalds committed
294

Jiri Slaby's avatar
Jiri Slaby committed
295 296
	TRACE_Q("remove_from_tx_queue: tx_first = %p, tx_last = %p",
		pInfo->tx_first, pInfo->tx_last);
Linus Torvalds's avatar
Linus Torvalds committed
297 298
}

Jiri Slaby's avatar
Jiri Slaby committed
299 300
static void add_rx_queue(struct r3964_info *pInfo,
			 struct r3964_block_header *pHeader)
Linus Torvalds's avatar
Linus Torvalds committed
301
{
Jiri Slaby's avatar
Jiri Slaby committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
	unsigned long flags;

	spin_lock_irqsave(&pInfo->lock, flags);

	pHeader->next = NULL;

	if (pInfo->rx_last == NULL) {
		pInfo->rx_first = pInfo->rx_last = pHeader;
	} else {
		pInfo->rx_last->next = pHeader;
		pInfo->rx_last = pHeader;
	}
	pInfo->blocks_in_rx_queue++;

	spin_unlock_irqrestore(&pInfo->lock, flags);

	TRACE_Q("add_rx_queue: %p, length = %d, rx_first = %p, count = %d",
		pHeader, pHeader->length,
		pInfo->rx_first, pInfo->blocks_in_rx_queue);
Linus Torvalds's avatar
Linus Torvalds committed
321 322 323
}

static void remove_from_rx_queue(struct r3964_info *pInfo,
Jiri Slaby's avatar
Jiri Slaby committed
324
				 struct r3964_block_header *pHeader)
Linus Torvalds's avatar
Linus Torvalds committed
325
{
Jiri Slaby's avatar
Jiri Slaby committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
	unsigned long flags;
	struct r3964_block_header *pFind;

	if (pHeader == NULL)
		return;

	TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
		pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue);
	TRACE_Q("remove_from_rx_queue: %p, length %u",
		pHeader, pHeader->length);

	spin_lock_irqsave(&pInfo->lock, flags);

	if (pInfo->rx_first == pHeader) {
		/* Remove the first block in the linked list: */
		pInfo->rx_first = pHeader->next;

		if (pInfo->rx_first == NULL) {
			pInfo->rx_last = NULL;
		}
		pInfo->blocks_in_rx_queue--;
	} else {
		/* Find block to remove: */
		for (pFind = pInfo->rx_first; pFind; pFind = pFind->next) {
			if (pFind->next == pHeader) {
				/* Got it. */
				pFind->next = pHeader->next;
				pInfo->blocks_in_rx_queue--;
				if (pFind->next == NULL) {
					/* Oh, removed the last one! */
					pInfo->rx_last = pFind;
				}
				break;
			}
		}
	}

	spin_unlock_irqrestore(&pInfo->lock, flags);

	kfree(pHeader);
	TRACE_M("remove_from_rx_queue - kfree %p", pHeader);

	TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
		pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue);
Linus Torvalds's avatar
Linus Torvalds committed
370 371 372 373
}

static void put_char(struct r3964_info *pInfo, unsigned char ch)
{
Jiri Slaby's avatar
Jiri Slaby committed
374
	struct tty_struct *tty = pInfo->tty;
Alan Cox's avatar
Alan Cox committed
375
	/* FIXME: put_char should not be called from an IRQ */
Alan Cox's avatar
Alan Cox committed
376
	tty_put_char(tty, ch);
Jiri Slaby's avatar
Jiri Slaby committed
377
	pInfo->bcc ^= ch;
Linus Torvalds's avatar
Linus Torvalds committed
378 379 380 381
}

static void flush(struct r3964_info *pInfo)
{
Jiri Slaby's avatar
Jiri Slaby committed
382
	struct tty_struct *tty = pInfo->tty;
Linus Torvalds's avatar
Linus Torvalds committed
383

Alan Cox's avatar
Alan Cox committed
384
	if (tty == NULL || tty->ops->flush_chars == NULL)
Jiri Slaby's avatar
Jiri Slaby committed
385
		return;
Alan Cox's avatar
Alan Cox committed
386
	tty->ops->flush_chars(tty);
Linus Torvalds's avatar
Linus Torvalds committed
387 388 389 390
}

static void trigger_transmit(struct r3964_info *pInfo)
{
Jiri Slaby's avatar
Jiri Slaby committed
391
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
392

Jiri Slaby's avatar
Jiri Slaby committed
393
	spin_lock_irqsave(&pInfo->lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
394

Jiri Slaby's avatar
Jiri Slaby committed
395 396 397 398 399
	if ((pInfo->state == R3964_IDLE) && (pInfo->tx_first != NULL)) {
		pInfo->state = R3964_TX_REQUEST;
		pInfo->nRetry = 0;
		pInfo->flags &= ~R3964_ERROR;
		mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
Linus Torvalds's avatar
Linus Torvalds committed
400

Jiri Slaby's avatar
Jiri Slaby committed
401
		spin_unlock_irqrestore(&pInfo->lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
402

Jiri Slaby's avatar
Jiri Slaby committed
403
		TRACE_PS("trigger_transmit - sent STX");
Linus Torvalds's avatar
Linus Torvalds committed
404

Jiri Slaby's avatar
Jiri Slaby committed
405 406
		put_char(pInfo, STX);
		flush(pInfo);
Linus Torvalds's avatar
Linus Torvalds committed
407

Jiri Slaby's avatar
Jiri Slaby committed
408 409 410 411
		pInfo->bcc = 0;
	} else {
		spin_unlock_irqrestore(&pInfo->lock, flags);
	}
Linus Torvalds's avatar
Linus Torvalds committed
412 413 414 415
}

static void retry_transmit(struct r3964_info *pInfo)
{
Jiri Slaby's avatar
Jiri Slaby committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
	if (pInfo->nRetry < R3964_MAX_RETRIES) {
		TRACE_PE("transmission failed. Retry #%d", pInfo->nRetry);
		pInfo->bcc = 0;
		put_char(pInfo, STX);
		flush(pInfo);
		pInfo->state = R3964_TX_REQUEST;
		pInfo->nRetry++;
		mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
	} else {
		TRACE_PE("transmission failed after %d retries",
			 R3964_MAX_RETRIES);

		remove_from_tx_queue(pInfo, R3964_TX_FAIL);

		put_char(pInfo, NAK);
		flush(pInfo);
		pInfo->state = R3964_IDLE;

		trigger_transmit(pInfo);
	}
Linus Torvalds's avatar
Linus Torvalds committed
436 437 438 439
}

static void transmit_block(struct r3964_info *pInfo)
{
Jiri Slaby's avatar
Jiri Slaby committed
440 441 442 443
	struct tty_struct *tty = pInfo->tty;
	struct r3964_block_header *pBlock = pInfo->tx_first;
	int room = 0;

Alan Cox's avatar
Alan Cox committed
444
	if (tty == NULL || pBlock == NULL) {
Jiri Slaby's avatar
Jiri Slaby committed
445 446 447
		return;
	}

Alan Cox's avatar
Alan Cox committed
448
	room = tty_write_room(tty);
Jiri Slaby's avatar
Jiri Slaby committed
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

	TRACE_PS("transmit_block %p, room %d, length %d",
		 pBlock, room, pBlock->length);

	while (pInfo->tx_position < pBlock->length) {
		if (room < 2)
			break;

		if (pBlock->data[pInfo->tx_position] == DLE) {
			/* send additional DLE char: */
			put_char(pInfo, DLE);
		}
		put_char(pInfo, pBlock->data[pInfo->tx_position++]);

		room--;
	}

	if ((pInfo->tx_position == pBlock->length) && (room >= 3)) {
		put_char(pInfo, DLE);
		put_char(pInfo, ETX);
		if (pInfo->flags & R3964_BCC) {
			put_char(pInfo, pInfo->bcc);
		}
		pInfo->state = R3964_WAIT_FOR_TX_ACK;
		mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
	}
	flush(pInfo);
Linus Torvalds's avatar
Linus Torvalds committed
476 477 478 479
}

static void on_receive_block(struct r3964_info *pInfo)
{
Jiri Slaby's avatar
Jiri Slaby committed
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
	unsigned int length;
	struct r3964_client_info *pClient;
	struct r3964_block_header *pBlock;

	length = pInfo->rx_position;

	/* compare byte checksum characters: */
	if (pInfo->flags & R3964_BCC) {
		if (pInfo->bcc != pInfo->last_rx) {
			TRACE_PE("checksum error - got %x but expected %x",
				 pInfo->last_rx, pInfo->bcc);
			pInfo->flags |= R3964_CHECKSUM;
		}
	}

	/* check for errors (parity, overrun,...): */
	if (pInfo->flags & R3964_ERROR) {
		TRACE_PE("on_receive_block - transmission failed error %x",
			 pInfo->flags & R3964_ERROR);

		put_char(pInfo, NAK);
		flush(pInfo);
		if (pInfo->nRetry < R3964_MAX_RETRIES) {
			pInfo->state = R3964_WAIT_FOR_RX_REPEAT;
			pInfo->nRetry++;
			mod_timer(&pInfo->tmr, jiffies + R3964_TO_RX_PANIC);
		} else {
			TRACE_PE("on_receive_block - failed after max retries");
			pInfo->state = R3964_IDLE;
		}
		return;
	}

	/* received block; submit DLE: */
	put_char(pInfo, DLE);
	flush(pInfo);
	del_timer_sync(&pInfo->tmr);
	TRACE_PS(" rx success: got %d chars", length);

	/* prepare struct r3964_block_header: */
	pBlock = kmalloc(length + sizeof(struct r3964_block_header),
			GFP_KERNEL);
	TRACE_M("on_receive_block - kmalloc %p", pBlock);

	if (pBlock == NULL)
		return;

	pBlock->length = length;
	pBlock->data = ((unsigned char *)pBlock) +
			sizeof(struct r3964_block_header);
	pBlock->locks = 0;
	pBlock->next = NULL;
	pBlock->owner = NULL;

	memcpy(pBlock->data, pInfo->rx_buf, length);

	/* queue block into rx_queue: */
	add_rx_queue(pInfo, pBlock);

	/* notify attached client processes: */
	for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) {
		if (pClient->sig_flags & R3964_SIG_DATA) {
			add_msg(pClient, R3964_MSG_DATA, length, R3964_OK,
				pBlock);
		}
	}
	wake_up_interruptible(&pInfo->read_wait);
Linus Torvalds's avatar
Linus Torvalds committed
547

Jiri Slaby's avatar
Jiri Slaby committed
548 549 550 551
	pInfo->state = R3964_IDLE;

	trigger_transmit(pInfo);
}
Linus Torvalds's avatar
Linus Torvalds committed
552 553 554

static void receive_char(struct r3964_info *pInfo, const unsigned char c)
{
Jiri Slaby's avatar
Jiri Slaby committed
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
	switch (pInfo->state) {
	case R3964_TX_REQUEST:
		if (c == DLE) {
			TRACE_PS("TX_REQUEST - got DLE");

			pInfo->state = R3964_TRANSMITTING;
			pInfo->tx_position = 0;

			transmit_block(pInfo);
		} else if (c == STX) {
			if (pInfo->nRetry == 0) {
				TRACE_PE("TX_REQUEST - init conflict");
				if (pInfo->priority == R3964_SLAVE) {
					goto start_receiving;
				}
			} else {
				TRACE_PE("TX_REQUEST - secondary init "
					"conflict!? Switching to SLAVE mode "
					"for next rx.");
				goto start_receiving;
			}
		} else {
			TRACE_PE("TX_REQUEST - char != DLE: %x", c);
			retry_transmit(pInfo);
		}
		break;
	case R3964_TRANSMITTING:
		if (c == NAK) {
			TRACE_PE("TRANSMITTING - got NAK");
			retry_transmit(pInfo);
		} else {
			TRACE_PE("TRANSMITTING - got invalid char");

			pInfo->state = R3964_WAIT_ZVZ_BEFORE_TX_RETRY;
			mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
		}
		break;
	case R3964_WAIT_FOR_TX_ACK:
		if (c == DLE) {
			TRACE_PS("WAIT_FOR_TX_ACK - got DLE");
			remove_from_tx_queue(pInfo, R3964_OK);

			pInfo->state = R3964_IDLE;
			trigger_transmit(pInfo);
		} else {
			retry_transmit(pInfo);
		}
		break;
	case R3964_WAIT_FOR_RX_REPEAT:
604
		/* FALLTHROUGH */
Jiri Slaby's avatar
Jiri Slaby committed
605 606 607 608 609 610 611 612 613 614 615 616
	case R3964_IDLE:
		if (c == STX) {
			/* Prevent rx_queue from overflow: */
			if (pInfo->blocks_in_rx_queue >=
			    R3964_MAX_BLOCKS_IN_RX_QUEUE) {
				TRACE_PE("IDLE - got STX but no space in "
						"rx_queue!");
				pInfo->state = R3964_WAIT_FOR_RX_BUF;
				mod_timer(&pInfo->tmr,
					  jiffies + R3964_TO_NO_BUF);
				break;
			}
Linus Torvalds's avatar
Linus Torvalds committed
617
start_receiving:
Jiri Slaby's avatar
Jiri Slaby committed
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
			/* Ok, start receiving: */
			TRACE_PS("IDLE - got STX");
			pInfo->rx_position = 0;
			pInfo->last_rx = 0;
			pInfo->flags &= ~R3964_ERROR;
			pInfo->state = R3964_RECEIVING;
			mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
			pInfo->nRetry = 0;
			put_char(pInfo, DLE);
			flush(pInfo);
			pInfo->bcc = 0;
		}
		break;
	case R3964_RECEIVING:
		if (pInfo->rx_position < RX_BUF_SIZE) {
			pInfo->bcc ^= c;

			if (c == DLE) {
				if (pInfo->last_rx == DLE) {
					pInfo->last_rx = 0;
					goto char_to_buf;
				}
				pInfo->last_rx = DLE;
				break;
			} else if ((c == ETX) && (pInfo->last_rx == DLE)) {
				if (pInfo->flags & R3964_BCC) {
					pInfo->state = R3964_WAIT_FOR_BCC;
					mod_timer(&pInfo->tmr,
						  jiffies + R3964_TO_ZVZ);
				} else {
					on_receive_block(pInfo);
				}
			} else {
				pInfo->last_rx = c;
Linus Torvalds's avatar
Linus Torvalds committed
652
char_to_buf:
Jiri Slaby's avatar
Jiri Slaby committed
653 654 655 656 657 658 659 660 661 662 663
				pInfo->rx_buf[pInfo->rx_position++] = c;
				mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
			}
		}
		/* else: overflow-msg? BUF_SIZE>MTU; should not happen? */
		break;
	case R3964_WAIT_FOR_BCC:
		pInfo->last_rx = c;
		on_receive_block(pInfo);
		break;
	}
Linus Torvalds's avatar
Linus Torvalds committed
664 665 666 667
}

static void receive_error(struct r3964_info *pInfo, const char flag)
{
Jiri Slaby's avatar
Jiri Slaby committed
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
	switch (flag) {
	case TTY_NORMAL:
		break;
	case TTY_BREAK:
		TRACE_PE("received break");
		pInfo->flags |= R3964_BREAK;
		break;
	case TTY_PARITY:
		TRACE_PE("parity error");
		pInfo->flags |= R3964_PARITY;
		break;
	case TTY_FRAME:
		TRACE_PE("frame error");
		pInfo->flags |= R3964_FRAME;
		break;
	case TTY_OVERRUN:
		TRACE_PE("frame overrun");
		pInfo->flags |= R3964_OVERRUN;
		break;
	default:
		TRACE_PE("receive_error - unknown flag %d", flag);
		pInfo->flags |= R3964_UNKNOWN;
		break;
	}
Linus Torvalds's avatar
Linus Torvalds committed
692 693 694 695
}

static void on_timeout(unsigned long priv)
{
Jiri Slaby's avatar
Jiri Slaby committed
696 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
	struct r3964_info *pInfo = (void *)priv;

	switch (pInfo->state) {
	case R3964_TX_REQUEST:
		TRACE_PE("TX_REQUEST - timeout");
		retry_transmit(pInfo);
		break;
	case R3964_WAIT_ZVZ_BEFORE_TX_RETRY:
		put_char(pInfo, NAK);
		flush(pInfo);
		retry_transmit(pInfo);
		break;
	case R3964_WAIT_FOR_TX_ACK:
		TRACE_PE("WAIT_FOR_TX_ACK - timeout");
		retry_transmit(pInfo);
		break;
	case R3964_WAIT_FOR_RX_BUF:
		TRACE_PE("WAIT_FOR_RX_BUF - timeout");
		put_char(pInfo, NAK);
		flush(pInfo);
		pInfo->state = R3964_IDLE;
		break;
	case R3964_RECEIVING:
		TRACE_PE("RECEIVING - timeout after %d chars",
			 pInfo->rx_position);
		put_char(pInfo, NAK);
		flush(pInfo);
		pInfo->state = R3964_IDLE;
		break;
	case R3964_WAIT_FOR_RX_REPEAT:
		TRACE_PE("WAIT_FOR_RX_REPEAT - timeout");
		pInfo->state = R3964_IDLE;
		break;
	case R3964_WAIT_FOR_BCC:
		TRACE_PE("WAIT_FOR_BCC - timeout");
		put_char(pInfo, NAK);
		flush(pInfo);
		pInfo->state = R3964_IDLE;
		break;
	}
Linus Torvalds's avatar
Linus Torvalds committed
736 737
}

Jiri Slaby's avatar
Jiri Slaby committed
738 739
static struct r3964_client_info *findClient(struct r3964_info *pInfo,
		struct pid *pid)
Linus Torvalds's avatar
Linus Torvalds committed
740
{
Jiri Slaby's avatar
Jiri Slaby committed
741 742 743 744 745 746 747 748
	struct r3964_client_info *pClient;

	for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) {
		if (pClient->pid == pid) {
			return pClient;
		}
	}
	return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
749 750
}

751
static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg)
Linus Torvalds's avatar
Linus Torvalds committed
752
{
Jiri Slaby's avatar
Jiri Slaby committed
753 754 755 756 757 758 759 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 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
	struct r3964_client_info *pClient;
	struct r3964_client_info **ppClient;
	struct r3964_message *pMsg;

	if ((arg & R3964_SIG_ALL) == 0) {
		/* Remove client from client list */
		for (ppClient = &pInfo->firstClient; *ppClient;
		     ppClient = &(*ppClient)->next) {
			pClient = *ppClient;

			if (pClient->pid == pid) {
				TRACE_PS("removing client %d from client list",
					 pid_nr(pid));
				*ppClient = pClient->next;
				while (pClient->msg_count) {
					pMsg = remove_msg(pInfo, pClient);
					if (pMsg) {
						kfree(pMsg);
						TRACE_M("enable_signals - msg "
							"kfree %p", pMsg);
					}
				}
				put_pid(pClient->pid);
				kfree(pClient);
				TRACE_M("enable_signals - kfree %p", pClient);
				return 0;
			}
		}
		return -EINVAL;
	} else {
		pClient = findClient(pInfo, pid);
		if (pClient) {
			/* update signal options */
			pClient->sig_flags = arg;
		} else {
			/* add client to client list */
			pClient = kmalloc(sizeof(struct r3964_client_info),
					GFP_KERNEL);
			TRACE_M("enable_signals - kmalloc %p", pClient);
			if (pClient == NULL)
				return -ENOMEM;

			TRACE_PS("add client %d to client list", pid_nr(pid));
			spin_lock_init(&pClient->lock);
			pClient->sig_flags = arg;
			pClient->pid = get_pid(pid);
			pClient->next = pInfo->firstClient;
			pClient->first_msg = NULL;
			pClient->last_msg = NULL;
			pClient->next_block_to_read = NULL;
			pClient->msg_count = 0;
			pInfo->firstClient = pClient;
		}
	}

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
809 810
}

Jiri Slaby's avatar
Jiri Slaby committed
811 812
static int read_telegram(struct r3964_info *pInfo, struct pid *pid,
			 unsigned char __user * buf)
Linus Torvalds's avatar
Linus Torvalds committed
813
{
Jiri Slaby's avatar
Jiri Slaby committed
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
	struct r3964_client_info *pClient;
	struct r3964_block_header *block;

	if (!buf) {
		return -EINVAL;
	}

	pClient = findClient(pInfo, pid);
	if (pClient == NULL) {
		return -EINVAL;
	}

	block = pClient->next_block_to_read;
	if (!block) {
		return 0;
	} else {
		if (copy_to_user(buf, block->data, block->length))
			return -EFAULT;

		remove_client_block(pInfo, pClient);
		return block->length;
	}

	return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
838 839 840
}

static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
Jiri Slaby's avatar
Jiri Slaby committed
841
		int error_code, struct r3964_block_header *pBlock)
Linus Torvalds's avatar
Linus Torvalds committed
842
{
Jiri Slaby's avatar
Jiri Slaby committed
843 844 845 846
	struct r3964_message *pMsg;
	unsigned long flags;

	if (pClient->msg_count < R3964_MAX_MSG_COUNT - 1) {
Linus Torvalds's avatar
Linus Torvalds committed
847 848
queue_the_message:

Jiri Slaby's avatar
Jiri Slaby committed
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
		pMsg = kmalloc(sizeof(struct r3964_message),
				error_code ? GFP_ATOMIC : GFP_KERNEL);
		TRACE_M("add_msg - kmalloc %p", pMsg);
		if (pMsg == NULL) {
			return;
		}

		spin_lock_irqsave(&pClient->lock, flags);

		pMsg->msg_id = msg_id;
		pMsg->arg = arg;
		pMsg->error_code = error_code;
		pMsg->block = pBlock;
		pMsg->next = NULL;

		if (pClient->last_msg == NULL) {
			pClient->first_msg = pClient->last_msg = pMsg;
		} else {
			pClient->last_msg->next = pMsg;
			pClient->last_msg = pMsg;
		}

		pClient->msg_count++;

		if (pBlock != NULL) {
			pBlock->locks++;
		}
		spin_unlock_irqrestore(&pClient->lock, flags);
	} else {
		if ((pClient->last_msg->msg_id == R3964_MSG_ACK)
		    && (pClient->last_msg->error_code == R3964_OVERFLOW)) {
			pClient->last_msg->arg++;
			TRACE_PE("add_msg - inc prev OVERFLOW-msg");
		} else {
			msg_id = R3964_MSG_ACK;
			arg = 0;
			error_code = R3964_OVERFLOW;
			pBlock = NULL;
			TRACE_PE("add_msg - queue OVERFLOW-msg");
			goto queue_the_message;
		}
	}
	/* Send SIGIO signal to client process: */
	if (pClient->sig_flags & R3964_USE_SIGIO) {
		kill_pid(pClient->pid, SIGIO, 1);
	}
Linus Torvalds's avatar
Linus Torvalds committed
895 896 897
}

static struct r3964_message *remove_msg(struct r3964_info *pInfo,
Jiri Slaby's avatar
Jiri Slaby committed
898
					struct r3964_client_info *pClient)
Linus Torvalds's avatar
Linus Torvalds committed
899
{
Jiri Slaby's avatar
Jiri Slaby committed
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
	struct r3964_message *pMsg = NULL;
	unsigned long flags;

	if (pClient->first_msg) {
		spin_lock_irqsave(&pClient->lock, flags);

		pMsg = pClient->first_msg;
		pClient->first_msg = pMsg->next;
		if (pClient->first_msg == NULL) {
			pClient->last_msg = NULL;
		}

		pClient->msg_count--;
		if (pMsg->block) {
			remove_client_block(pInfo, pClient);
			pClient->next_block_to_read = pMsg->block;
		}
		spin_unlock_irqrestore(&pClient->lock, flags);
	}
	return pMsg;
Linus Torvalds's avatar
Linus Torvalds committed
920 921
}

Jiri Slaby's avatar
Jiri Slaby committed
922 923
static void remove_client_block(struct r3964_info *pInfo,
				struct r3964_client_info *pClient)
Linus Torvalds's avatar
Linus Torvalds committed
924
{
Jiri Slaby's avatar
Jiri Slaby committed
925 926 927
	struct r3964_block_header *block;

	TRACE_PS("remove_client_block PID %d", pid_nr(pClient->pid));
Linus Torvalds's avatar
Linus Torvalds committed
928

Jiri Slaby's avatar
Jiri Slaby committed
929 930 931 932 933 934 935 936 937
	block = pClient->next_block_to_read;
	if (block) {
		block->locks--;
		if (block->locks == 0) {
			remove_from_rx_queue(pInfo, block);
		}
	}
	pClient->next_block_to_read = NULL;
}
Linus Torvalds's avatar
Linus Torvalds committed
938 939 940 941 942 943 944

/*************************************************************
 * Line discipline routines
 *************************************************************/

static int r3964_open(struct tty_struct *tty)
{
Jiri Slaby's avatar
Jiri Slaby committed
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
	struct r3964_info *pInfo;

	TRACE_L("open");
	TRACE_L("tty=%p, PID=%d, disc_data=%p",
		tty, current->pid, tty->disc_data);

	pInfo = kmalloc(sizeof(struct r3964_info), GFP_KERNEL);
	TRACE_M("r3964_open - info kmalloc %p", pInfo);

	if (!pInfo) {
		printk(KERN_ERR "r3964: failed to alloc info structure\n");
		return -ENOMEM;
	}

	pInfo->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL);
	TRACE_M("r3964_open - rx_buf kmalloc %p", pInfo->rx_buf);

	if (!pInfo->rx_buf) {
		printk(KERN_ERR "r3964: failed to alloc receive buffer\n");
		kfree(pInfo);
		TRACE_M("r3964_open - info kfree %p", pInfo);
		return -ENOMEM;
	}

	pInfo->tx_buf = kmalloc(TX_BUF_SIZE, GFP_KERNEL);
	TRACE_M("r3964_open - tx_buf kmalloc %p", pInfo->tx_buf);

	if (!pInfo->tx_buf) {
		printk(KERN_ERR "r3964: failed to alloc transmit buffer\n");
		kfree(pInfo->rx_buf);
		TRACE_M("r3964_open - rx_buf kfree %p", pInfo->rx_buf);
		kfree(pInfo);
		TRACE_M("r3964_open - info kfree %p", pInfo);
		return -ENOMEM;
	}

	spin_lock_init(&pInfo->lock);
	pInfo->tty = tty;
	init_waitqueue_head(&pInfo->read_wait);
	pInfo->priority = R3964_MASTER;
	pInfo->rx_first = pInfo->rx_last = NULL;
	pInfo->tx_first = pInfo->tx_last = NULL;
	pInfo->rx_position = 0;
	pInfo->tx_position = 0;
	pInfo->last_rx = 0;
	pInfo->blocks_in_rx_queue = 0;
	pInfo->firstClient = NULL;
	pInfo->state = R3964_IDLE;
	pInfo->flags = R3964_DEBUG;
	pInfo->nRetry = 0;

	tty->disc_data = pInfo;
	tty->receive_room = 65536;

Jiri Slaby's avatar
Jiri Slaby committed
999
	setup_timer(&pInfo->tmr, on_timeout, (unsigned long)pInfo);
Jiri Slaby's avatar
Jiri Slaby committed
1000 1001

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1002 1003 1004 1005
}

static void r3964_close(struct tty_struct *tty)
{
Alan Cox's avatar
Alan Cox committed
1006
	struct r3964_info *pInfo = tty->disc_data;
Jiri Slaby's avatar
Jiri Slaby committed
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
	struct r3964_client_info *pClient, *pNext;
	struct r3964_message *pMsg;
	struct r3964_block_header *pHeader, *pNextHeader;
	unsigned long flags;

	TRACE_L("close");

	/*
	 * Make sure that our task queue isn't activated.  If it
	 * is, take it out of the linked list.
	 */
	del_timer_sync(&pInfo->tmr);

	/* Remove client-structs and message queues: */
	pClient = pInfo->firstClient;
	while (pClient) {
		pNext = pClient->next;
		while (pClient->msg_count) {
			pMsg = remove_msg(pInfo, pClient);
			if (pMsg) {
				kfree(pMsg);
				TRACE_M("r3964_close - msg kfree %p", pMsg);
			}
		}
		put_pid(pClient->pid);
		kfree(pClient);
		TRACE_M("r3964_close - client kfree %p", pClient);
		pClient = pNext;
	}
	/* Remove jobs from tx_queue: */
	spin_lock_irqsave(&pInfo->lock, flags);
	pHeader = pInfo->tx_first;
	pInfo->tx_first = pInfo->tx_last = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1040
	spin_unlock_irqrestore(&pInfo->lock, flags);
Jiri Slaby's avatar
Jiri Slaby committed
1041 1042 1043 1044 1045

	while (pHeader) {
		pNextHeader = pHeader->next;
		kfree(pHeader);
		pHeader = pNextHeader;
Linus Torvalds's avatar
Linus Torvalds committed
1046 1047
	}

Jiri Slaby's avatar
Jiri Slaby committed
1048 1049 1050 1051 1052 1053 1054 1055
	/* Free buffers: */
	wake_up_interruptible(&pInfo->read_wait);
	kfree(pInfo->rx_buf);
	TRACE_M("r3964_close - rx_buf kfree %p", pInfo->rx_buf);
	kfree(pInfo->tx_buf);
	TRACE_M("r3964_close - tx_buf kfree %p", pInfo->tx_buf);
	kfree(pInfo);
	TRACE_M("r3964_close - info kfree %p", pInfo);
Linus Torvalds's avatar
Linus Torvalds committed
1056 1057 1058
}

static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
Jiri Slaby's avatar
Jiri Slaby committed
1059
			  unsigned char __user * buf, size_t nr)
Linus Torvalds's avatar
Linus Torvalds committed
1060
{
Alan Cox's avatar
Alan Cox committed
1061
	struct r3964_info *pInfo = tty->disc_data;
Jiri Slaby's avatar
Jiri Slaby committed
1062 1063 1064
	struct r3964_client_info *pClient;
	struct r3964_message *pMsg;
	struct r3964_client_message theMsg;
Jiri Slaby's avatar
Jiri Slaby committed
1065
	int ret;
Jiri Slaby's avatar
Jiri Slaby committed
1066 1067 1068

	TRACE_L("read()");

1069
	tty_lock();
Alan Cox's avatar
Alan Cox committed
1070

Jiri Slaby's avatar
Jiri Slaby committed
1071 1072 1073 1074 1075 1076
	pClient = findClient(pInfo, task_pid(current));
	if (pClient) {
		pMsg = remove_msg(pInfo, pClient);
		if (pMsg == NULL) {
			/* no messages available. */
			if (file->f_flags & O_NONBLOCK) {
Jiri Slaby's avatar
Jiri Slaby committed
1077 1078
				ret = -EAGAIN;
				goto unlock;
Jiri Slaby's avatar
Jiri Slaby committed
1079 1080
			}
			/* block until there is a message: */
1081
			wait_event_interruptible_tty(pInfo->read_wait,
1082
					(pMsg = remove_msg(pInfo, pClient)));
Jiri Slaby's avatar
Jiri Slaby committed
1083 1084 1085 1086
		}

		/* If we still haven't got a message, we must have been signalled */

Alan Cox's avatar
Alan Cox committed
1087
		if (!pMsg) {
Jiri Slaby's avatar
Jiri Slaby committed
1088 1089
			ret = -EINTR;
			goto unlock;
Alan Cox's avatar
Alan Cox committed
1090
		}
Jiri Slaby's avatar
Jiri Slaby committed
1091 1092 1093 1094 1095

		/* deliver msg to client process: */
		theMsg.msg_id = pMsg->msg_id;
		theMsg.arg = pMsg->arg;
		theMsg.error_code = pMsg->error_code;
Jiri Slaby's avatar
Jiri Slaby committed
1096
		ret = sizeof(struct r3964_client_message);
Jiri Slaby's avatar
Jiri Slaby committed
1097 1098 1099 1100

		kfree(pMsg);
		TRACE_M("r3964_read - msg kfree %p", pMsg);

Jiri Slaby's avatar
Jiri Slaby committed
1101 1102 1103
		if (copy_to_user(buf, &theMsg, ret)) {
			ret = -EFAULT;
			goto unlock;
Alan Cox's avatar
Alan Cox committed
1104
		}
Jiri Slaby's avatar
Jiri Slaby committed
1105

Jiri Slaby's avatar
Jiri Slaby committed
1106 1107
		TRACE_PS("read - return %d", ret);
		goto unlock;
Jiri Slaby's avatar
Jiri Slaby committed
1108
	}
Jiri Slaby's avatar
Jiri Slaby committed
1109 1110
	ret = -EPERM;
unlock:
1111
	tty_unlock();
Jiri Slaby's avatar
Jiri Slaby committed
1112
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
1113 1114
}

Jiri Slaby's avatar
Jiri Slaby committed
1115
static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
Linus Torvalds's avatar
Linus Torvalds committed
1116 1117
			   const unsigned char *data, size_t count)
{
Alan Cox's avatar
Alan Cox committed
1118
	struct r3964_info *pInfo = tty->disc_data;
Jiri Slaby's avatar
Jiri Slaby committed
1119 1120 1121 1122 1123
	struct r3964_block_header *pHeader;
	struct r3964_client_info *pClient;
	unsigned char *new_data;

	TRACE_L("write request, %d characters", count);
Linus Torvalds's avatar
Linus Torvalds committed
1124 1125 1126 1127
/* 
 * Verify the pointers 
 */

Jiri Slaby's avatar
Jiri Slaby committed
1128 1129
	if (!pInfo)
		return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
1130 1131 1132 1133

/*
 * Ensure that the caller does not wish to send too much.
 */
Jiri Slaby's avatar
Jiri Slaby committed
1134 1135 1136 1137 1138 1139 1140
	if (count > R3964_MTU) {
		if (pInfo->flags & R3964_DEBUG) {
			TRACE_L(KERN_WARNING "r3964_write: truncating user "
				"packet from %u to mtu %d", count, R3964_MTU);
		}
		count = R3964_MTU;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1141 1142 1143
/*
 * Allocate a buffer for the data and copy it from the buffer with header prepended
 */
Jiri Slaby's avatar
Jiri Slaby committed
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
	new_data = kmalloc(count + sizeof(struct r3964_block_header),
			GFP_KERNEL);
	TRACE_M("r3964_write - kmalloc %p", new_data);
	if (new_data == NULL) {
		if (pInfo->flags & R3964_DEBUG) {
			printk(KERN_ERR "r3964_write: no memory\n");
		}
		return -ENOSPC;
	}

	pHeader = (struct r3964_block_header *)new_data;
	pHeader->data = new_data + sizeof(struct r3964_block_header);
	pHeader->length = count;
	pHeader->locks = 0;
	pHeader->owner = NULL;

1160
	tty_lock();
Alan Cox's avatar
Alan Cox committed
1161

Jiri Slaby's avatar
Jiri Slaby committed
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
	pClient = findClient(pInfo, task_pid(current));
	if (pClient) {
		pHeader->owner = pClient;
	}

	memcpy(pHeader->data, data, count);	/* We already verified this */

	if (pInfo->flags & R3964_DEBUG) {
		dump_block(pHeader->data, count);
	}
Linus Torvalds's avatar
Linus Torvalds committed
1172 1173 1174 1175

/*
 * Add buffer to transmit-queue:
 */
Jiri Slaby's avatar
Jiri Slaby committed
1176 1177 1178
	add_tx_queue(pInfo, pHeader);
	trigger_transmit(pInfo);

1179
	tty_unlock();
Alan Cox's avatar
Alan Cox committed
1180

Jiri Slaby's avatar
Jiri Slaby committed
1181
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1182 1183
}

Jiri Slaby's avatar
Jiri Slaby committed
1184 1185
static int r3964_ioctl(struct tty_struct *tty, struct file *file,
		unsigned int cmd, unsigned long arg)
Linus Torvalds's avatar
Linus Torvalds committed
1186
{
Alan Cox's avatar
Alan Cox committed
1187
	struct r3964_info *pInfo = tty->disc_data;
Jiri Slaby's avatar
Jiri Slaby committed
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
	if (pInfo == NULL)
		return -EINVAL;
	switch (cmd) {
	case R3964_ENABLE_SIGNALS:
		return enable_signals(pInfo, task_pid(current), arg);
	case R3964_SETPRIORITY:
		if (arg < R3964_MASTER || arg > R3964_SLAVE)
			return -EINVAL;
		pInfo->priority = arg & 0xff;
		return 0;
	case R3964_USE_BCC:
		if (arg)
			pInfo->flags |= R3964_BCC;
		else
			pInfo->flags &= ~R3964_BCC;
		return 0;
	case R3964_READ_TELEGRAM:
		return read_telegram(pInfo, task_pid(current),
				(unsigned char __user *)arg);
	default:
		return -ENOIOCTLCMD;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1210 1211
}

Jiri Slaby's avatar
Jiri Slaby committed
1212
static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old)
Linus Torvalds's avatar
Linus Torvalds committed
1213
{
Jiri Slaby's avatar
Jiri Slaby committed
1214
	TRACE_L("set_termios");
Linus Torvalds's avatar
Linus Torvalds committed
1215 1216 1217
}

/* Called without the kernel lock held - fine */
Jiri Slaby's avatar
Jiri Slaby committed
1218 1219
static unsigned int r3964_poll(struct tty_struct *tty, struct file *file,
			struct poll_table_struct *wait)
Linus Torvalds's avatar
Linus Torvalds committed
1220
{
Alan Cox's avatar
Alan Cox committed
1221
	struct r3964_info *pInfo = tty->disc_data;
Jiri Slaby's avatar
Jiri Slaby committed
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
	struct r3964_client_info *pClient;
	struct r3964_message *pMsg = NULL;
	unsigned long flags;
	int result = POLLOUT;

	TRACE_L("POLL");

	pClient = findClient(pInfo, task_pid(current));
	if (pClient) {
		poll_wait(file, &pInfo->read_wait, wait);
		spin_lock_irqsave(&pInfo->lock, flags);
		pMsg = pClient->first_msg;
		spin_unlock_irqrestore(&pInfo->lock, flags);
		if (pMsg)
			result |= POLLIN | POLLRDNORM;
	} else {
		result = -EINVAL;
	}
	return result;
Linus Torvalds's avatar
Linus Torvalds committed
1241 1242 1243
}

static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
Jiri Slaby's avatar
Jiri Slaby committed
1244
			char *fp, int count)
Linus Torvalds's avatar
Linus Torvalds committed
1245
{
Alan Cox's avatar
Alan Cox committed
1246
	struct r3964_info *pInfo = tty->disc_data;
Jiri Slaby's avatar
Jiri Slaby committed
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
	const unsigned char *p;
	char *f, flags = 0;
	int i;

	for (i = count, p = cp, f = fp; i; i--, p++) {
		if (f)
			flags = *f++;
		if (flags == TTY_NORMAL) {
			receive_char(pInfo, *p);
		} else {
			receive_error(pInfo, flags);
		}

	}
Linus Torvalds's avatar
Linus Torvalds committed
1261 1262 1263 1264
}

MODULE_LICENSE("GPL");
MODULE_ALIAS_LDISC(N_R3964);