Linux Audio

Check our new training course

Loading...
   1// SPDX-License-Identifier: GPL-1.0+
   2/* r3964 linediscipline for linux
   3 *
   4 * -----------------------------------------------------------
   5 * Copyright by 
   6 * Philips Automation Projects
   7 * Kassel (Germany)
   8 * -----------------------------------------------------------
   9 * Author:
  10 * L. Haag
  11 *
  12 * $Log: n_r3964.c,v $
  13 * Revision 1.10  2001/03/18 13:02:24  dwmw2
  14 * Fix timer usage, use spinlocks properly.
  15 *
  16 * Revision 1.9  2001/03/18 12:52:14  dwmw2
  17 * Merge changes in 2.4.2
  18 *
  19 * Revision 1.8  2000/03/23 14:14:54  dwmw2
  20 * Fix race in sleeping in r3964_read()
  21 *
  22 * Revision 1.7  1999/28/08 11:41:50  dwmw2
  23 * Port to 2.3 kernel
  24 *
  25 * Revision 1.6  1998/09/30 00:40:40  dwmw2
  26 * Fixed compilation on 2.0.x kernels
  27 * Updated to newly registered tty-ldisc number 9
  28 *
  29 * Revision 1.5  1998/09/04 21:57:36  dwmw2
  30 * Signal handling bug fixes, port to 2.1.x.
  31 *
  32 * Revision 1.4  1998/04/02 20:26:59  lhaag
  33 * select, blocking, ...
  34 *
  35 * Revision 1.3  1998/02/12 18:58:43  root
  36 * fixed some memory leaks
  37 * calculation of checksum characters
  38 *
  39 * Revision 1.2  1998/02/07 13:03:34  root
  40 * ioctl read_telegram
  41 *
  42 * Revision 1.1  1998/02/06 19:21:03  root
  43 * Initial revision
  44 *
  45 *
  46 */
  47
  48#include <linux/module.h>
  49#include <linux/kernel.h>
  50#include <linux/sched.h>
  51#include <linux/types.h>
  52#include <linux/fcntl.h>
  53#include <linux/interrupt.h>
  54#include <linux/ptrace.h>
  55#include <linux/ioport.h>
  56#include <linux/in.h>
  57#include <linux/slab.h>
  58#include <linux/tty.h>
  59#include <linux/errno.h>
  60#include <linux/string.h>	/* used in new tty drivers */
  61#include <linux/signal.h>	/* used in new tty drivers */
  62#include <linux/ioctl.h>
  63#include <linux/n_r3964.h>
  64#include <linux/poll.h>
  65#include <linux/init.h>
  66#include <linux/uaccess.h>
  67
  68/*#define DEBUG_QUEUE*/
  69
  70/* Log successful handshake and protocol operations  */
  71/*#define DEBUG_PROTO_S*/
  72
  73/* Log handshake and protocol errors: */
  74/*#define DEBUG_PROTO_E*/
  75
  76/* Log Linediscipline operations (open, close, read, write...): */
  77/*#define DEBUG_LDISC*/
  78
  79/* Log module and memory operations (init, cleanup; kmalloc, kfree): */
  80/*#define DEBUG_MODUL*/
  81
  82/* Macro helpers for debug output: */
  83#define TRACE(format, args...) printk("r3964: " format "\n" , ## args)
  84
  85#ifdef DEBUG_MODUL
  86#define TRACE_M(format, args...) printk("r3964: " format "\n" , ## args)
  87#else
  88#define TRACE_M(fmt, arg...) do {} while (0)
  89#endif
  90#ifdef DEBUG_PROTO_S
  91#define TRACE_PS(format, args...) printk("r3964: " format "\n" , ## args)
  92#else
  93#define TRACE_PS(fmt, arg...) do {} while (0)
  94#endif
  95#ifdef DEBUG_PROTO_E
  96#define TRACE_PE(format, args...) printk("r3964: " format "\n" , ## args)
  97#else
  98#define TRACE_PE(fmt, arg...) do {} while (0)
  99#endif
 100#ifdef DEBUG_LDISC
 101#define TRACE_L(format, args...) printk("r3964: " format "\n" , ## args)
 102#else
 103#define TRACE_L(fmt, arg...) do {} while (0)
 104#endif
 105#ifdef DEBUG_QUEUE
 106#define TRACE_Q(format, args...) printk("r3964: " format "\n" , ## args)
 107#else
 108#define TRACE_Q(fmt, arg...) do {} while (0)
 109#endif
 110static void add_tx_queue(struct r3964_info *, struct r3964_block_header *);
 111static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code);
 112static void put_char(struct r3964_info *pInfo, unsigned char ch);
 113static void trigger_transmit(struct r3964_info *pInfo);
 114static void retry_transmit(struct r3964_info *pInfo);
 115static void transmit_block(struct r3964_info *pInfo);
 116static void receive_char(struct r3964_info *pInfo, const unsigned char c);
 117static void receive_error(struct r3964_info *pInfo, const char flag);
 118static void on_timeout(struct timer_list *t);
 119static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg);
 120static int read_telegram(struct r3964_info *pInfo, struct pid *pid,
 121		unsigned char __user * buf);
 122static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
 123		int error_code, struct r3964_block_header *pBlock);
 124static struct r3964_message *remove_msg(struct r3964_info *pInfo,
 125		struct r3964_client_info *pClient);
 126static void remove_client_block(struct r3964_info *pInfo,
 127		struct r3964_client_info *pClient);
 128
 129static int r3964_open(struct tty_struct *tty);
 130static void r3964_close(struct tty_struct *tty);
 131static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
 132		unsigned char __user * buf, size_t nr);
 133static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
 134		const unsigned char *buf, size_t nr);
 135static int r3964_ioctl(struct tty_struct *tty, struct file *file,
 136		unsigned int cmd, unsigned long arg);
 137#ifdef CONFIG_COMPAT
 138static int r3964_compat_ioctl(struct tty_struct *tty, struct file *file,
 139		unsigned int cmd, unsigned long arg);
 140#endif
 141static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old);
 142static __poll_t r3964_poll(struct tty_struct *tty, struct file *file,
 143		struct poll_table_struct *wait);
 144static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
 145		char *fp, int count);
 146
 147static struct tty_ldisc_ops tty_ldisc_N_R3964 = {
 148	.owner = THIS_MODULE,
 149	.magic = TTY_LDISC_MAGIC,
 150	.name = "R3964",
 151	.open = r3964_open,
 152	.close = r3964_close,
 153	.read = r3964_read,
 154	.write = r3964_write,
 155	.ioctl = r3964_ioctl,
 156#ifdef CONFIG_COMPAT
 157	.compat_ioctl = r3964_compat_ioctl,
 158#endif
 159	.set_termios = r3964_set_termios,
 160	.poll = r3964_poll,
 161	.receive_buf = r3964_receive_buf,
 162};
 163
 164static void dump_block(const unsigned char *block, unsigned int length)
 165{
 166	unsigned int i, j;
 167	char linebuf[16 * 3 + 1];
 168
 169	for (i = 0; i < length; i += 16) {
 170		for (j = 0; (j < 16) && (j + i < length); j++) {
 171			sprintf(linebuf + 3 * j, "%02x ", block[i + j]);
 172		}
 173		linebuf[3 * j] = '\0';
 174		TRACE_PS("%s", linebuf);
 175	}
 176}
 177
 178/*************************************************************
 179 * Driver initialisation
 180 *************************************************************/
 181
 182/*************************************************************
 183 * Module support routines
 184 *************************************************************/
 185
 186static void __exit r3964_exit(void)
 187{
 188	int status;
 189
 190	TRACE_M("cleanup_module()");
 191
 192	status = tty_unregister_ldisc(N_R3964);
 193
 194	if (status != 0) {
 195		printk(KERN_ERR "r3964: error unregistering linediscipline: "
 196				"%d\n", status);
 197	} else {
 198		TRACE_L("linediscipline successfully unregistered");
 199	}
 200}
 201
 202static int __init r3964_init(void)
 203{
 204	int status;
 205
 206	printk("r3964: Philips r3964 Driver $Revision: 1.10 $\n");
 207
 208	/*
 209	 * Register the tty line discipline
 210	 */
 211
 212	status = tty_register_ldisc(N_R3964, &tty_ldisc_N_R3964);
 213	if (status == 0) {
 214		TRACE_L("line discipline %d registered", N_R3964);
 215		TRACE_L("flags=%x num=%x", tty_ldisc_N_R3964.flags,
 216			tty_ldisc_N_R3964.num);
 217		TRACE_L("open=%p", tty_ldisc_N_R3964.open);
 218		TRACE_L("tty_ldisc_N_R3964 = %p", &tty_ldisc_N_R3964);
 219	} else {
 220		printk(KERN_ERR "r3964: error registering line discipline: "
 221				"%d\n", status);
 222	}
 223	return status;
 224}
 225
 226module_init(r3964_init);
 227module_exit(r3964_exit);
 228
 229/*************************************************************
 230 * Protocol implementation routines
 231 *************************************************************/
 232
 233static void add_tx_queue(struct r3964_info *pInfo,
 234			 struct r3964_block_header *pHeader)
 235{
 236	unsigned long flags;
 237
 238	spin_lock_irqsave(&pInfo->lock, flags);
 239
 240	pHeader->next = NULL;
 241
 242	if (pInfo->tx_last == NULL) {
 243		pInfo->tx_first = pInfo->tx_last = pHeader;
 244	} else {
 245		pInfo->tx_last->next = pHeader;
 246		pInfo->tx_last = pHeader;
 247	}
 248
 249	spin_unlock_irqrestore(&pInfo->lock, flags);
 250
 251	TRACE_Q("add_tx_queue %p, length %d, tx_first = %p",
 252		pHeader, pHeader->length, pInfo->tx_first);
 253}
 254
 255static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code)
 256{
 257	struct r3964_block_header *pHeader;
 258	unsigned long flags;
 259#ifdef DEBUG_QUEUE
 260	struct r3964_block_header *pDump;
 261#endif
 262
 263	pHeader = pInfo->tx_first;
 264
 265	if (pHeader == NULL)
 266		return;
 267
 268#ifdef DEBUG_QUEUE
 269	printk("r3964: remove_from_tx_queue: %p, length %u - ",
 270		pHeader, pHeader->length);
 271	for (pDump = pHeader; pDump; pDump = pDump->next)
 272		printk("%p ", pDump);
 273	printk("\n");
 274#endif
 275
 276	if (pHeader->owner) {
 277		if (error_code) {
 278			add_msg(pHeader->owner, R3964_MSG_ACK, 0,
 279				error_code, NULL);
 280		} else {
 281			add_msg(pHeader->owner, R3964_MSG_ACK, pHeader->length,
 282				error_code, NULL);
 283		}
 284		wake_up_interruptible(&pInfo->tty->read_wait);
 285	}
 286
 287	spin_lock_irqsave(&pInfo->lock, flags);
 288
 289	pInfo->tx_first = pHeader->next;
 290	if (pInfo->tx_first == NULL) {
 291		pInfo->tx_last = NULL;
 292	}
 293
 294	spin_unlock_irqrestore(&pInfo->lock, flags);
 295
 296	kfree(pHeader);
 297	TRACE_M("remove_from_tx_queue - kfree %p", pHeader);
 298
 299	TRACE_Q("remove_from_tx_queue: tx_first = %p, tx_last = %p",
 300		pInfo->tx_first, pInfo->tx_last);
 301}
 302
 303static void add_rx_queue(struct r3964_info *pInfo,
 304			 struct r3964_block_header *pHeader)
 305{
 306	unsigned long flags;
 307
 308	spin_lock_irqsave(&pInfo->lock, flags);
 309
 310	pHeader->next = NULL;
 311
 312	if (pInfo->rx_last == NULL) {
 313		pInfo->rx_first = pInfo->rx_last = pHeader;
 314	} else {
 315		pInfo->rx_last->next = pHeader;
 316		pInfo->rx_last = pHeader;
 317	}
 318	pInfo->blocks_in_rx_queue++;
 319
 320	spin_unlock_irqrestore(&pInfo->lock, flags);
 321
 322	TRACE_Q("add_rx_queue: %p, length = %d, rx_first = %p, count = %d",
 323		pHeader, pHeader->length,
 324		pInfo->rx_first, pInfo->blocks_in_rx_queue);
 325}
 326
 327static void remove_from_rx_queue(struct r3964_info *pInfo,
 328				 struct r3964_block_header *pHeader)
 329{
 330	unsigned long flags;
 331	struct r3964_block_header *pFind;
 332
 333	if (pHeader == NULL)
 334		return;
 335
 336	TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
 337		pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue);
 338	TRACE_Q("remove_from_rx_queue: %p, length %u",
 339		pHeader, pHeader->length);
 340
 341	spin_lock_irqsave(&pInfo->lock, flags);
 342
 343	if (pInfo->rx_first == pHeader) {
 344		/* Remove the first block in the linked list: */
 345		pInfo->rx_first = pHeader->next;
 346
 347		if (pInfo->rx_first == NULL) {
 348			pInfo->rx_last = NULL;
 349		}
 350		pInfo->blocks_in_rx_queue--;
 351	} else {
 352		/* Find block to remove: */
 353		for (pFind = pInfo->rx_first; pFind; pFind = pFind->next) {
 354			if (pFind->next == pHeader) {
 355				/* Got it. */
 356				pFind->next = pHeader->next;
 357				pInfo->blocks_in_rx_queue--;
 358				if (pFind->next == NULL) {
 359					/* Oh, removed the last one! */
 360					pInfo->rx_last = pFind;
 361				}
 362				break;
 363			}
 364		}
 365	}
 366
 367	spin_unlock_irqrestore(&pInfo->lock, flags);
 368
 369	kfree(pHeader);
 370	TRACE_M("remove_from_rx_queue - kfree %p", pHeader);
 371
 372	TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
 373		pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue);
 374}
 375
 376static void put_char(struct r3964_info *pInfo, unsigned char ch)
 377{
 378	struct tty_struct *tty = pInfo->tty;
 379	/* FIXME: put_char should not be called from an IRQ */
 380	tty_put_char(tty, ch);
 381	pInfo->bcc ^= ch;
 382}
 383
 384static void flush(struct r3964_info *pInfo)
 385{
 386	struct tty_struct *tty = pInfo->tty;
 387
 388	if (tty == NULL || tty->ops->flush_chars == NULL)
 389		return;
 390	tty->ops->flush_chars(tty);
 391}
 392
 393static void trigger_transmit(struct r3964_info *pInfo)
 394{
 395	unsigned long flags;
 396
 397	spin_lock_irqsave(&pInfo->lock, flags);
 398
 399	if ((pInfo->state == R3964_IDLE) && (pInfo->tx_first != NULL)) {
 400		pInfo->state = R3964_TX_REQUEST;
 401		pInfo->nRetry = 0;
 402		pInfo->flags &= ~R3964_ERROR;
 403		mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
 404
 405		spin_unlock_irqrestore(&pInfo->lock, flags);
 406
 407		TRACE_PS("trigger_transmit - sent STX");
 408
 409		put_char(pInfo, STX);
 410		flush(pInfo);
 411
 412		pInfo->bcc = 0;
 413	} else {
 414		spin_unlock_irqrestore(&pInfo->lock, flags);
 415	}
 416}
 417
 418static void retry_transmit(struct r3964_info *pInfo)
 419{
 420	if (pInfo->nRetry < R3964_MAX_RETRIES) {
 421		TRACE_PE("transmission failed. Retry #%d", pInfo->nRetry);
 422		pInfo->bcc = 0;
 423		put_char(pInfo, STX);
 424		flush(pInfo);
 425		pInfo->state = R3964_TX_REQUEST;
 426		pInfo->nRetry++;
 427		mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
 428	} else {
 429		TRACE_PE("transmission failed after %d retries",
 430			 R3964_MAX_RETRIES);
 431
 432		remove_from_tx_queue(pInfo, R3964_TX_FAIL);
 433
 434		put_char(pInfo, NAK);
 435		flush(pInfo);
 436		pInfo->state = R3964_IDLE;
 437
 438		trigger_transmit(pInfo);
 439	}
 440}
 441
 442static void transmit_block(struct r3964_info *pInfo)
 443{
 444	struct tty_struct *tty = pInfo->tty;
 445	struct r3964_block_header *pBlock = pInfo->tx_first;
 446	int room = 0;
 447
 448	if (tty == NULL || pBlock == NULL) {
 449		return;
 450	}
 451
 452	room = tty_write_room(tty);
 453
 454	TRACE_PS("transmit_block %p, room %d, length %d",
 455		 pBlock, room, pBlock->length);
 456
 457	while (pInfo->tx_position < pBlock->length) {
 458		if (room < 2)
 459			break;
 460
 461		if (pBlock->data[pInfo->tx_position] == DLE) {
 462			/* send additional DLE char: */
 463			put_char(pInfo, DLE);
 464		}
 465		put_char(pInfo, pBlock->data[pInfo->tx_position++]);
 466
 467		room--;
 468	}
 469
 470	if ((pInfo->tx_position == pBlock->length) && (room >= 3)) {
 471		put_char(pInfo, DLE);
 472		put_char(pInfo, ETX);
 473		if (pInfo->flags & R3964_BCC) {
 474			put_char(pInfo, pInfo->bcc);
 475		}
 476		pInfo->state = R3964_WAIT_FOR_TX_ACK;
 477		mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
 478	}
 479	flush(pInfo);
 480}
 481
 482static void on_receive_block(struct r3964_info *pInfo)
 483{
 484	unsigned int length;
 485	struct r3964_client_info *pClient;
 486	struct r3964_block_header *pBlock;
 487
 488	length = pInfo->rx_position;
 489
 490	/* compare byte checksum characters: */
 491	if (pInfo->flags & R3964_BCC) {
 492		if (pInfo->bcc != pInfo->last_rx) {
 493			TRACE_PE("checksum error - got %x but expected %x",
 494				 pInfo->last_rx, pInfo->bcc);
 495			pInfo->flags |= R3964_CHECKSUM;
 496		}
 497	}
 498
 499	/* check for errors (parity, overrun,...): */
 500	if (pInfo->flags & R3964_ERROR) {
 501		TRACE_PE("on_receive_block - transmission failed error %x",
 502			 pInfo->flags & R3964_ERROR);
 503
 504		put_char(pInfo, NAK);
 505		flush(pInfo);
 506		if (pInfo->nRetry < R3964_MAX_RETRIES) {
 507			pInfo->state = R3964_WAIT_FOR_RX_REPEAT;
 508			pInfo->nRetry++;
 509			mod_timer(&pInfo->tmr, jiffies + R3964_TO_RX_PANIC);
 510		} else {
 511			TRACE_PE("on_receive_block - failed after max retries");
 512			pInfo->state = R3964_IDLE;
 513		}
 514		return;
 515	}
 516
 517	/* received block; submit DLE: */
 518	put_char(pInfo, DLE);
 519	flush(pInfo);
 520	del_timer_sync(&pInfo->tmr);
 521	TRACE_PS(" rx success: got %d chars", length);
 522
 523	/* prepare struct r3964_block_header: */
 524	pBlock = kmalloc(length + sizeof(struct r3964_block_header),
 525			GFP_KERNEL);
 526	TRACE_M("on_receive_block - kmalloc %p", pBlock);
 527
 528	if (pBlock == NULL)
 529		return;
 530
 531	pBlock->length = length;
 532	pBlock->data = ((unsigned char *)pBlock) +
 533			sizeof(struct r3964_block_header);
 534	pBlock->locks = 0;
 535	pBlock->next = NULL;
 536	pBlock->owner = NULL;
 537
 538	memcpy(pBlock->data, pInfo->rx_buf, length);
 539
 540	/* queue block into rx_queue: */
 541	add_rx_queue(pInfo, pBlock);
 542
 543	/* notify attached client processes: */
 544	for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) {
 545		if (pClient->sig_flags & R3964_SIG_DATA) {
 546			add_msg(pClient, R3964_MSG_DATA, length, R3964_OK,
 547				pBlock);
 548		}
 549	}
 550	wake_up_interruptible(&pInfo->tty->read_wait);
 551
 552	pInfo->state = R3964_IDLE;
 553
 554	trigger_transmit(pInfo);
 555}
 556
 557static void receive_char(struct r3964_info *pInfo, const unsigned char c)
 558{
 559	switch (pInfo->state) {
 560	case R3964_TX_REQUEST:
 561		if (c == DLE) {
 562			TRACE_PS("TX_REQUEST - got DLE");
 563
 564			pInfo->state = R3964_TRANSMITTING;
 565			pInfo->tx_position = 0;
 566
 567			transmit_block(pInfo);
 568		} else if (c == STX) {
 569			if (pInfo->nRetry == 0) {
 570				TRACE_PE("TX_REQUEST - init conflict");
 571				if (pInfo->priority == R3964_SLAVE) {
 572					goto start_receiving;
 573				}
 574			} else {
 575				TRACE_PE("TX_REQUEST - secondary init "
 576					"conflict!? Switching to SLAVE mode "
 577					"for next rx.");
 578				goto start_receiving;
 579			}
 580		} else {
 581			TRACE_PE("TX_REQUEST - char != DLE: %x", c);
 582			retry_transmit(pInfo);
 583		}
 584		break;
 585	case R3964_TRANSMITTING:
 586		if (c == NAK) {
 587			TRACE_PE("TRANSMITTING - got NAK");
 588			retry_transmit(pInfo);
 589		} else {
 590			TRACE_PE("TRANSMITTING - got invalid char");
 591
 592			pInfo->state = R3964_WAIT_ZVZ_BEFORE_TX_RETRY;
 593			mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
 594		}
 595		break;
 596	case R3964_WAIT_FOR_TX_ACK:
 597		if (c == DLE) {
 598			TRACE_PS("WAIT_FOR_TX_ACK - got DLE");
 599			remove_from_tx_queue(pInfo, R3964_OK);
 600
 601			pInfo->state = R3964_IDLE;
 602			trigger_transmit(pInfo);
 603		} else {
 604			retry_transmit(pInfo);
 605		}
 606		break;
 607	case R3964_WAIT_FOR_RX_REPEAT:
 608		/* FALLTHROUGH */
 609	case R3964_IDLE:
 610		if (c == STX) {
 611			/* Prevent rx_queue from overflow: */
 612			if (pInfo->blocks_in_rx_queue >=
 613			    R3964_MAX_BLOCKS_IN_RX_QUEUE) {
 614				TRACE_PE("IDLE - got STX but no space in "
 615						"rx_queue!");
 616				pInfo->state = R3964_WAIT_FOR_RX_BUF;
 617				mod_timer(&pInfo->tmr,
 618					  jiffies + R3964_TO_NO_BUF);
 619				break;
 620			}
 621start_receiving:
 622			/* Ok, start receiving: */
 623			TRACE_PS("IDLE - got STX");
 624			pInfo->rx_position = 0;
 625			pInfo->last_rx = 0;
 626			pInfo->flags &= ~R3964_ERROR;
 627			pInfo->state = R3964_RECEIVING;
 628			mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
 629			pInfo->nRetry = 0;
 630			put_char(pInfo, DLE);
 631			flush(pInfo);
 632			pInfo->bcc = 0;
 633		}
 634		break;
 635	case R3964_RECEIVING:
 636		if (pInfo->rx_position < RX_BUF_SIZE) {
 637			pInfo->bcc ^= c;
 638
 639			if (c == DLE) {
 640				if (pInfo->last_rx == DLE) {
 641					pInfo->last_rx = 0;
 642					goto char_to_buf;
 643				}
 644				pInfo->last_rx = DLE;
 645				break;
 646			} else if ((c == ETX) && (pInfo->last_rx == DLE)) {
 647				if (pInfo->flags & R3964_BCC) {
 648					pInfo->state = R3964_WAIT_FOR_BCC;
 649					mod_timer(&pInfo->tmr,
 650						  jiffies + R3964_TO_ZVZ);
 651				} else {
 652					on_receive_block(pInfo);
 653				}
 654			} else {
 655				pInfo->last_rx = c;
 656char_to_buf:
 657				pInfo->rx_buf[pInfo->rx_position++] = c;
 658				mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
 659			}
 660		}
 661		/* else: overflow-msg? BUF_SIZE>MTU; should not happen? */
 662		break;
 663	case R3964_WAIT_FOR_BCC:
 664		pInfo->last_rx = c;
 665		on_receive_block(pInfo);
 666		break;
 667	}
 668}
 669
 670static void receive_error(struct r3964_info *pInfo, const char flag)
 671{
 672	switch (flag) {
 673	case TTY_NORMAL:
 674		break;
 675	case TTY_BREAK:
 676		TRACE_PE("received break");
 677		pInfo->flags |= R3964_BREAK;
 678		break;
 679	case TTY_PARITY:
 680		TRACE_PE("parity error");
 681		pInfo->flags |= R3964_PARITY;
 682		break;
 683	case TTY_FRAME:
 684		TRACE_PE("frame error");
 685		pInfo->flags |= R3964_FRAME;
 686		break;
 687	case TTY_OVERRUN:
 688		TRACE_PE("frame overrun");
 689		pInfo->flags |= R3964_OVERRUN;
 690		break;
 691	default:
 692		TRACE_PE("receive_error - unknown flag %d", flag);
 693		pInfo->flags |= R3964_UNKNOWN;
 694		break;
 695	}
 696}
 697
 698static void on_timeout(struct timer_list *t)
 699{
 700	struct r3964_info *pInfo = from_timer(pInfo, t, tmr);
 701
 702	switch (pInfo->state) {
 703	case R3964_TX_REQUEST:
 704		TRACE_PE("TX_REQUEST - timeout");
 705		retry_transmit(pInfo);
 706		break;
 707	case R3964_WAIT_ZVZ_BEFORE_TX_RETRY:
 708		put_char(pInfo, NAK);
 709		flush(pInfo);
 710		retry_transmit(pInfo);
 711		break;
 712	case R3964_WAIT_FOR_TX_ACK:
 713		TRACE_PE("WAIT_FOR_TX_ACK - timeout");
 714		retry_transmit(pInfo);
 715		break;
 716	case R3964_WAIT_FOR_RX_BUF:
 717		TRACE_PE("WAIT_FOR_RX_BUF - timeout");
 718		put_char(pInfo, NAK);
 719		flush(pInfo);
 720		pInfo->state = R3964_IDLE;
 721		break;
 722	case R3964_RECEIVING:
 723		TRACE_PE("RECEIVING - timeout after %d chars",
 724			 pInfo->rx_position);
 725		put_char(pInfo, NAK);
 726		flush(pInfo);
 727		pInfo->state = R3964_IDLE;
 728		break;
 729	case R3964_WAIT_FOR_RX_REPEAT:
 730		TRACE_PE("WAIT_FOR_RX_REPEAT - timeout");
 731		pInfo->state = R3964_IDLE;
 732		break;
 733	case R3964_WAIT_FOR_BCC:
 734		TRACE_PE("WAIT_FOR_BCC - timeout");
 735		put_char(pInfo, NAK);
 736		flush(pInfo);
 737		pInfo->state = R3964_IDLE;
 738		break;
 739	}
 740}
 741
 742static struct r3964_client_info *findClient(struct r3964_info *pInfo,
 743		struct pid *pid)
 744{
 745	struct r3964_client_info *pClient;
 746
 747	for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) {
 748		if (pClient->pid == pid) {
 749			return pClient;
 750		}
 751	}
 752	return NULL;
 753}
 754
 755static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg)
 756{
 757	struct r3964_client_info *pClient;
 758	struct r3964_client_info **ppClient;
 759	struct r3964_message *pMsg;
 760
 761	if ((arg & R3964_SIG_ALL) == 0) {
 762		/* Remove client from client list */
 763		for (ppClient = &pInfo->firstClient; *ppClient;
 764		     ppClient = &(*ppClient)->next) {
 765			pClient = *ppClient;
 766
 767			if (pClient->pid == pid) {
 768				TRACE_PS("removing client %d from client list",
 769					 pid_nr(pid));
 770				*ppClient = pClient->next;
 771				while (pClient->msg_count) {
 772					pMsg = remove_msg(pInfo, pClient);
 773					if (pMsg) {
 774						kfree(pMsg);
 775						TRACE_M("enable_signals - msg "
 776							"kfree %p", pMsg);
 777					}
 778				}
 779				put_pid(pClient->pid);
 780				kfree(pClient);
 781				TRACE_M("enable_signals - kfree %p", pClient);
 782				return 0;
 783			}
 784		}
 785		return -EINVAL;
 786	} else {
 787		pClient = findClient(pInfo, pid);
 788		if (pClient) {
 789			/* update signal options */
 790			pClient->sig_flags = arg;
 791		} else {
 792			/* add client to client list */
 793			pClient = kmalloc(sizeof(struct r3964_client_info),
 794					GFP_KERNEL);
 795			TRACE_M("enable_signals - kmalloc %p", pClient);
 796			if (pClient == NULL)
 797				return -ENOMEM;
 798
 799			TRACE_PS("add client %d to client list", pid_nr(pid));
 800			spin_lock_init(&pClient->lock);
 801			pClient->sig_flags = arg;
 802			pClient->pid = get_pid(pid);
 803			pClient->next = pInfo->firstClient;
 804			pClient->first_msg = NULL;
 805			pClient->last_msg = NULL;
 806			pClient->next_block_to_read = NULL;
 807			pClient->msg_count = 0;
 808			pInfo->firstClient = pClient;
 809		}
 810	}
 811
 812	return 0;
 813}
 814
 815static int read_telegram(struct r3964_info *pInfo, struct pid *pid,
 816			 unsigned char __user * buf)
 817{
 818	struct r3964_client_info *pClient;
 819	struct r3964_block_header *block;
 820
 821	if (!buf) {
 822		return -EINVAL;
 823	}
 824
 825	pClient = findClient(pInfo, pid);
 826	if (pClient == NULL) {
 827		return -EINVAL;
 828	}
 829
 830	block = pClient->next_block_to_read;
 831	if (!block) {
 832		return 0;
 833	} else {
 834		if (copy_to_user(buf, block->data, block->length))
 835			return -EFAULT;
 836
 837		remove_client_block(pInfo, pClient);
 838		return block->length;
 839	}
 840
 841	return -EINVAL;
 842}
 843
 844static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
 845		int error_code, struct r3964_block_header *pBlock)
 846{
 847	struct r3964_message *pMsg;
 848	unsigned long flags;
 849
 850	if (pClient->msg_count < R3964_MAX_MSG_COUNT - 1) {
 851queue_the_message:
 852
 853		pMsg = kmalloc(sizeof(struct r3964_message),
 854				error_code ? GFP_ATOMIC : GFP_KERNEL);
 855		TRACE_M("add_msg - kmalloc %p", pMsg);
 856		if (pMsg == NULL) {
 857			return;
 858		}
 859
 860		spin_lock_irqsave(&pClient->lock, flags);
 861
 862		pMsg->msg_id = msg_id;
 863		pMsg->arg = arg;
 864		pMsg->error_code = error_code;
 865		pMsg->block = pBlock;
 866		pMsg->next = NULL;
 867
 868		if (pClient->last_msg == NULL) {
 869			pClient->first_msg = pClient->last_msg = pMsg;
 870		} else {
 871			pClient->last_msg->next = pMsg;
 872			pClient->last_msg = pMsg;
 873		}
 874
 875		pClient->msg_count++;
 876
 877		if (pBlock != NULL) {
 878			pBlock->locks++;
 879		}
 880		spin_unlock_irqrestore(&pClient->lock, flags);
 881	} else {
 882		if ((pClient->last_msg->msg_id == R3964_MSG_ACK)
 883		    && (pClient->last_msg->error_code == R3964_OVERFLOW)) {
 884			pClient->last_msg->arg++;
 885			TRACE_PE("add_msg - inc prev OVERFLOW-msg");
 886		} else {
 887			msg_id = R3964_MSG_ACK;
 888			arg = 0;
 889			error_code = R3964_OVERFLOW;
 890			pBlock = NULL;
 891			TRACE_PE("add_msg - queue OVERFLOW-msg");
 892			goto queue_the_message;
 893		}
 894	}
 895	/* Send SIGIO signal to client process: */
 896	if (pClient->sig_flags & R3964_USE_SIGIO) {
 897		kill_pid(pClient->pid, SIGIO, 1);
 898	}
 899}
 900
 901static struct r3964_message *remove_msg(struct r3964_info *pInfo,
 902					struct r3964_client_info *pClient)
 903{
 904	struct r3964_message *pMsg = NULL;
 905	unsigned long flags;
 906
 907	if (pClient->first_msg) {
 908		spin_lock_irqsave(&pClient->lock, flags);
 909
 910		pMsg = pClient->first_msg;
 911		pClient->first_msg = pMsg->next;
 912		if (pClient->first_msg == NULL) {
 913			pClient->last_msg = NULL;
 914		}
 915
 916		pClient->msg_count--;
 917		if (pMsg->block) {
 918			remove_client_block(pInfo, pClient);
 919			pClient->next_block_to_read = pMsg->block;
 920		}
 921		spin_unlock_irqrestore(&pClient->lock, flags);
 922	}
 923	return pMsg;
 924}
 925
 926static void remove_client_block(struct r3964_info *pInfo,
 927				struct r3964_client_info *pClient)
 928{
 929	struct r3964_block_header *block;
 930
 931	TRACE_PS("remove_client_block PID %d", pid_nr(pClient->pid));
 932
 933	block = pClient->next_block_to_read;
 934	if (block) {
 935		block->locks--;
 936		if (block->locks == 0) {
 937			remove_from_rx_queue(pInfo, block);
 938		}
 939	}
 940	pClient->next_block_to_read = NULL;
 941}
 942
 943/*************************************************************
 944 * Line discipline routines
 945 *************************************************************/
 946
 947static int r3964_open(struct tty_struct *tty)
 948{
 949	struct r3964_info *pInfo;
 950
 951	TRACE_L("open");
 952	TRACE_L("tty=%p, PID=%d, disc_data=%p",
 953		tty, current->pid, tty->disc_data);
 954
 955	pInfo = kmalloc(sizeof(struct r3964_info), GFP_KERNEL);
 956	TRACE_M("r3964_open - info kmalloc %p", pInfo);
 957
 958	if (!pInfo) {
 959		printk(KERN_ERR "r3964: failed to alloc info structure\n");
 960		return -ENOMEM;
 961	}
 962
 963	pInfo->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL);
 964	TRACE_M("r3964_open - rx_buf kmalloc %p", pInfo->rx_buf);
 965
 966	if (!pInfo->rx_buf) {
 967		printk(KERN_ERR "r3964: failed to alloc receive buffer\n");
 968		kfree(pInfo);
 969		TRACE_M("r3964_open - info kfree %p", pInfo);
 970		return -ENOMEM;
 971	}
 972
 973	pInfo->tx_buf = kmalloc(TX_BUF_SIZE, GFP_KERNEL);
 974	TRACE_M("r3964_open - tx_buf kmalloc %p", pInfo->tx_buf);
 975
 976	if (!pInfo->tx_buf) {
 977		printk(KERN_ERR "r3964: failed to alloc transmit buffer\n");
 978		kfree(pInfo->rx_buf);
 979		TRACE_M("r3964_open - rx_buf kfree %p", pInfo->rx_buf);
 980		kfree(pInfo);
 981		TRACE_M("r3964_open - info kfree %p", pInfo);
 982		return -ENOMEM;
 983	}
 984
 985	spin_lock_init(&pInfo->lock);
 986	mutex_init(&pInfo->read_lock);
 987	pInfo->tty = tty;
 988	pInfo->priority = R3964_MASTER;
 989	pInfo->rx_first = pInfo->rx_last = NULL;
 990	pInfo->tx_first = pInfo->tx_last = NULL;
 991	pInfo->rx_position = 0;
 992	pInfo->tx_position = 0;
 993	pInfo->last_rx = 0;
 994	pInfo->blocks_in_rx_queue = 0;
 995	pInfo->firstClient = NULL;
 996	pInfo->state = R3964_IDLE;
 997	pInfo->flags = R3964_DEBUG;
 998	pInfo->nRetry = 0;
 999
1000	tty->disc_data = pInfo;
1001	tty->receive_room = 65536;
1002
1003	timer_setup(&pInfo->tmr, on_timeout, 0);
1004
1005	return 0;
1006}
1007
1008static void r3964_close(struct tty_struct *tty)
1009{
1010	struct r3964_info *pInfo = tty->disc_data;
1011	struct r3964_client_info *pClient, *pNext;
1012	struct r3964_message *pMsg;
1013	struct r3964_block_header *pHeader, *pNextHeader;
1014	unsigned long flags;
1015
1016	TRACE_L("close");
1017
1018	/*
1019	 * Make sure that our task queue isn't activated.  If it
1020	 * is, take it out of the linked list.
1021	 */
1022	del_timer_sync(&pInfo->tmr);
1023
1024	/* Remove client-structs and message queues: */
1025	pClient = pInfo->firstClient;
1026	while (pClient) {
1027		pNext = pClient->next;
1028		while (pClient->msg_count) {
1029			pMsg = remove_msg(pInfo, pClient);
1030			if (pMsg) {
1031				kfree(pMsg);
1032				TRACE_M("r3964_close - msg kfree %p", pMsg);
1033			}
1034		}
1035		put_pid(pClient->pid);
1036		kfree(pClient);
1037		TRACE_M("r3964_close - client kfree %p", pClient);
1038		pClient = pNext;
1039	}
1040	/* Remove jobs from tx_queue: */
1041	spin_lock_irqsave(&pInfo->lock, flags);
1042	pHeader = pInfo->tx_first;
1043	pInfo->tx_first = pInfo->tx_last = NULL;
1044	spin_unlock_irqrestore(&pInfo->lock, flags);
1045
1046	while (pHeader) {
1047		pNextHeader = pHeader->next;
1048		kfree(pHeader);
1049		pHeader = pNextHeader;
1050	}
1051
1052	/* Free buffers: */
1053	kfree(pInfo->rx_buf);
1054	TRACE_M("r3964_close - rx_buf kfree %p", pInfo->rx_buf);
1055	kfree(pInfo->tx_buf);
1056	TRACE_M("r3964_close - tx_buf kfree %p", pInfo->tx_buf);
1057	kfree(pInfo);
1058	TRACE_M("r3964_close - info kfree %p", pInfo);
1059}
1060
1061static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
1062			  unsigned char __user * buf, size_t nr)
1063{
1064	struct r3964_info *pInfo = tty->disc_data;
1065	struct r3964_client_info *pClient;
1066	struct r3964_message *pMsg;
1067	struct r3964_client_message theMsg;
1068	int ret;
1069
1070	TRACE_L("read()");
1071
1072	/*
1073	 *	Internal serialization of reads.
1074	 */
1075	if (file->f_flags & O_NONBLOCK) {
1076		if (!mutex_trylock(&pInfo->read_lock))
1077			return -EAGAIN;
1078	} else {
1079		if (mutex_lock_interruptible(&pInfo->read_lock))
1080			return -ERESTARTSYS;
1081	}
1082
1083	pClient = findClient(pInfo, task_pid(current));
1084	if (pClient) {
1085		pMsg = remove_msg(pInfo, pClient);
1086		if (pMsg == NULL) {
1087			/* no messages available. */
1088			if (tty_io_nonblock(tty, file)) {
1089				ret = -EAGAIN;
1090				goto unlock;
1091			}
1092			/* block until there is a message: */
1093			wait_event_interruptible(tty->read_wait,
1094					(pMsg = remove_msg(pInfo, pClient)));
1095		}
1096
1097		/* If we still haven't got a message, we must have been signalled */
1098
1099		if (!pMsg) {
1100			ret = -EINTR;
1101			goto unlock;
1102		}
1103
1104		/* deliver msg to client process: */
1105		theMsg.msg_id = pMsg->msg_id;
1106		theMsg.arg = pMsg->arg;
1107		theMsg.error_code = pMsg->error_code;
1108		ret = sizeof(struct r3964_client_message);
1109
1110		kfree(pMsg);
1111		TRACE_M("r3964_read - msg kfree %p", pMsg);
1112
1113		if (copy_to_user(buf, &theMsg, ret)) {
1114			ret = -EFAULT;
1115			goto unlock;
1116		}
1117
1118		TRACE_PS("read - return %d", ret);
1119		goto unlock;
1120	}
1121	ret = -EPERM;
1122unlock:
1123	mutex_unlock(&pInfo->read_lock);
1124	return ret;
1125}
1126
1127static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
1128			   const unsigned char *data, size_t count)
1129{
1130	struct r3964_info *pInfo = tty->disc_data;
1131	struct r3964_block_header *pHeader;
1132	struct r3964_client_info *pClient;
1133	unsigned char *new_data;
1134
1135	TRACE_L("write request, %d characters", count);
1136/* 
1137 * Verify the pointers 
1138 */
1139
1140	if (!pInfo)
1141		return -EIO;
1142
1143/*
1144 * Ensure that the caller does not wish to send too much.
1145 */
1146	if (count > R3964_MTU) {
1147		if (pInfo->flags & R3964_DEBUG) {
1148			TRACE_L(KERN_WARNING "r3964_write: truncating user "
1149				"packet from %u to mtu %d", count, R3964_MTU);
1150		}
1151		count = R3964_MTU;
1152	}
1153/*
1154 * Allocate a buffer for the data and copy it from the buffer with header prepended
1155 */
1156	new_data = kmalloc(count + sizeof(struct r3964_block_header),
1157			GFP_KERNEL);
1158	TRACE_M("r3964_write - kmalloc %p", new_data);
1159	if (new_data == NULL) {
1160		if (pInfo->flags & R3964_DEBUG) {
1161			printk(KERN_ERR "r3964_write: no memory\n");
1162		}
1163		return -ENOSPC;
1164	}
1165
1166	pHeader = (struct r3964_block_header *)new_data;
1167	pHeader->data = new_data + sizeof(struct r3964_block_header);
1168	pHeader->length = count;
1169	pHeader->locks = 0;
1170	pHeader->owner = NULL;
1171
1172	pClient = findClient(pInfo, task_pid(current));
1173	if (pClient) {
1174		pHeader->owner = pClient;
1175	}
1176
1177	memcpy(pHeader->data, data, count);	/* We already verified this */
1178
1179	if (pInfo->flags & R3964_DEBUG) {
1180		dump_block(pHeader->data, count);
1181	}
1182
1183/*
1184 * Add buffer to transmit-queue:
1185 */
1186	add_tx_queue(pInfo, pHeader);
1187	trigger_transmit(pInfo);
1188
1189	return 0;
1190}
1191
1192static int r3964_ioctl(struct tty_struct *tty, struct file *file,
1193		unsigned int cmd, unsigned long arg)
1194{
1195	struct r3964_info *pInfo = tty->disc_data;
1196	if (pInfo == NULL)
1197		return -EINVAL;
1198	switch (cmd) {
1199	case R3964_ENABLE_SIGNALS:
1200		return enable_signals(pInfo, task_pid(current), arg);
1201	case R3964_SETPRIORITY:
1202		if (arg < R3964_MASTER || arg > R3964_SLAVE)
1203			return -EINVAL;
1204		pInfo->priority = arg & 0xff;
1205		return 0;
1206	case R3964_USE_BCC:
1207		if (arg)
1208			pInfo->flags |= R3964_BCC;
1209		else
1210			pInfo->flags &= ~R3964_BCC;
1211		return 0;
1212	case R3964_READ_TELEGRAM:
1213		return read_telegram(pInfo, task_pid(current),
1214				(unsigned char __user *)arg);
1215	default:
1216		return -ENOIOCTLCMD;
1217	}
1218}
1219
1220#ifdef CONFIG_COMPAT
1221static int r3964_compat_ioctl(struct tty_struct *tty, struct file *file,
1222		unsigned int cmd, unsigned long arg)
1223{
1224	switch (cmd) {
1225	case R3964_ENABLE_SIGNALS:
1226	case R3964_SETPRIORITY:
1227	case R3964_USE_BCC:
1228		return r3964_ioctl(tty, file, cmd, arg);
1229	default:
1230		return -ENOIOCTLCMD;
1231	}
1232}
1233#endif
1234
1235static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old)
1236{
1237	TRACE_L("set_termios");
1238}
1239
1240/* Called without the kernel lock held - fine */
1241static __poll_t r3964_poll(struct tty_struct *tty, struct file *file,
1242			struct poll_table_struct *wait)
1243{
1244	struct r3964_info *pInfo = tty->disc_data;
1245	struct r3964_client_info *pClient;
1246	struct r3964_message *pMsg = NULL;
1247	unsigned long flags;
1248	__poll_t result = EPOLLOUT;
1249
1250	TRACE_L("POLL");
1251
1252	pClient = findClient(pInfo, task_pid(current));
1253	if (pClient) {
1254		poll_wait(file, &tty->read_wait, wait);
1255		spin_lock_irqsave(&pInfo->lock, flags);
1256		pMsg = pClient->first_msg;
1257		spin_unlock_irqrestore(&pInfo->lock, flags);
1258		if (pMsg)
1259			result |= EPOLLIN | EPOLLRDNORM;
1260	} else {
1261		result = -EINVAL;
1262	}
1263	return result;
1264}
1265
1266static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1267			char *fp, int count)
1268{
1269	struct r3964_info *pInfo = tty->disc_data;
1270	const unsigned char *p;
1271	char *f, flags = TTY_NORMAL;
1272	int i;
1273
1274	for (i = count, p = cp, f = fp; i; i--, p++) {
1275		if (f)
1276			flags = *f++;
1277		if (flags == TTY_NORMAL) {
1278			receive_char(pInfo, *p);
1279		} else {
1280			receive_error(pInfo, flags);
1281		}
1282
1283	}
1284}
1285
1286MODULE_LICENSE("GPL");
1287MODULE_ALIAS_LDISC(N_R3964);
   1/* r3964 linediscipline for linux
   2 *
   3 * -----------------------------------------------------------
   4 * Copyright by 
   5 * Philips Automation Projects
   6 * Kassel (Germany)
   7 * -----------------------------------------------------------
   8 * This software may be used and distributed according to the terms of
   9 * the GNU General Public License, incorporated herein by reference.
  10 *
  11 * Author:
  12 * L. Haag
  13 *
  14 * $Log: n_r3964.c,v $
  15 * Revision 1.10  2001/03/18 13:02:24  dwmw2
  16 * Fix timer usage, use spinlocks properly.
  17 *
  18 * Revision 1.9  2001/03/18 12:52:14  dwmw2
  19 * Merge changes in 2.4.2
  20 *
  21 * Revision 1.8  2000/03/23 14:14:54  dwmw2
  22 * Fix race in sleeping in r3964_read()
  23 *
  24 * Revision 1.7  1999/28/08 11:41:50  dwmw2
  25 * Port to 2.3 kernel
  26 *
  27 * Revision 1.6  1998/09/30 00:40:40  dwmw2
  28 * Fixed compilation on 2.0.x kernels
  29 * Updated to newly registered tty-ldisc number 9
  30 *
  31 * Revision 1.5  1998/09/04 21:57:36  dwmw2
  32 * Signal handling bug fixes, port to 2.1.x.
  33 *
  34 * Revision 1.4  1998/04/02 20:26:59  lhaag
  35 * select, blocking, ...
  36 *
  37 * Revision 1.3  1998/02/12 18:58:43  root
  38 * fixed some memory leaks
  39 * calculation of checksum characters
  40 *
  41 * Revision 1.2  1998/02/07 13:03:34  root
  42 * ioctl read_telegram
  43 *
  44 * Revision 1.1  1998/02/06 19:21:03  root
  45 * Initial revision
  46 *
  47 *
  48 */
  49
  50#include <linux/module.h>
  51#include <linux/kernel.h>
  52#include <linux/sched.h>
  53#include <linux/types.h>
  54#include <linux/fcntl.h>
  55#include <linux/interrupt.h>
  56#include <linux/ptrace.h>
  57#include <linux/ioport.h>
  58#include <linux/in.h>
  59#include <linux/slab.h>
  60#include <linux/tty.h>
  61#include <linux/errno.h>
  62#include <linux/string.h>	/* used in new tty drivers */
  63#include <linux/signal.h>	/* used in new tty drivers */
  64#include <linux/ioctl.h>
  65#include <linux/n_r3964.h>
  66#include <linux/poll.h>
  67#include <linux/init.h>
  68#include <linux/uaccess.h>
  69
  70/*#define DEBUG_QUEUE*/
  71
  72/* Log successful handshake and protocol operations  */
  73/*#define DEBUG_PROTO_S*/
  74
  75/* Log handshake and protocol errors: */
  76/*#define DEBUG_PROTO_E*/
  77
  78/* Log Linediscipline operations (open, close, read, write...): */
  79/*#define DEBUG_LDISC*/
  80
  81/* Log module and memory operations (init, cleanup; kmalloc, kfree): */
  82/*#define DEBUG_MODUL*/
  83
  84/* Macro helpers for debug output: */
  85#define TRACE(format, args...) printk("r3964: " format "\n" , ## args)
  86
  87#ifdef DEBUG_MODUL
  88#define TRACE_M(format, args...) printk("r3964: " format "\n" , ## args)
  89#else
  90#define TRACE_M(fmt, arg...) do {} while (0)
  91#endif
  92#ifdef DEBUG_PROTO_S
  93#define TRACE_PS(format, args...) printk("r3964: " format "\n" , ## args)
  94#else
  95#define TRACE_PS(fmt, arg...) do {} while (0)
  96#endif
  97#ifdef DEBUG_PROTO_E
  98#define TRACE_PE(format, args...) printk("r3964: " format "\n" , ## args)
  99#else
 100#define TRACE_PE(fmt, arg...) do {} while (0)
 101#endif
 102#ifdef DEBUG_LDISC
 103#define TRACE_L(format, args...) printk("r3964: " format "\n" , ## args)
 104#else
 105#define TRACE_L(fmt, arg...) do {} while (0)
 106#endif
 107#ifdef DEBUG_QUEUE
 108#define TRACE_Q(format, args...) printk("r3964: " format "\n" , ## args)
 109#else
 110#define TRACE_Q(fmt, arg...) do {} while (0)
 111#endif
 112static void add_tx_queue(struct r3964_info *, struct r3964_block_header *);
 113static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code);
 114static void put_char(struct r3964_info *pInfo, unsigned char ch);
 115static void trigger_transmit(struct r3964_info *pInfo);
 116static void retry_transmit(struct r3964_info *pInfo);
 117static void transmit_block(struct r3964_info *pInfo);
 118static void receive_char(struct r3964_info *pInfo, const unsigned char c);
 119static void receive_error(struct r3964_info *pInfo, const char flag);
 120static void on_timeout(unsigned long priv);
 121static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg);
 122static int read_telegram(struct r3964_info *pInfo, struct pid *pid,
 123		unsigned char __user * buf);
 124static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
 125		int error_code, struct r3964_block_header *pBlock);
 126static struct r3964_message *remove_msg(struct r3964_info *pInfo,
 127		struct r3964_client_info *pClient);
 128static void remove_client_block(struct r3964_info *pInfo,
 129		struct r3964_client_info *pClient);
 130
 131static int r3964_open(struct tty_struct *tty);
 132static void r3964_close(struct tty_struct *tty);
 133static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
 134		unsigned char __user * buf, size_t nr);
 135static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
 136		const unsigned char *buf, size_t nr);
 137static int r3964_ioctl(struct tty_struct *tty, struct file *file,
 138		unsigned int cmd, unsigned long arg);
 139static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old);
 140static unsigned int r3964_poll(struct tty_struct *tty, struct file *file,
 141		struct poll_table_struct *wait);
 142static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
 143		char *fp, int count);
 144
 145static struct tty_ldisc_ops tty_ldisc_N_R3964 = {
 146	.owner = THIS_MODULE,
 147	.magic = TTY_LDISC_MAGIC,
 148	.name = "R3964",
 149	.open = r3964_open,
 150	.close = r3964_close,
 151	.read = r3964_read,
 152	.write = r3964_write,
 153	.ioctl = r3964_ioctl,
 154	.set_termios = r3964_set_termios,
 155	.poll = r3964_poll,
 156	.receive_buf = r3964_receive_buf,
 157};
 158
 159static void dump_block(const unsigned char *block, unsigned int length)
 160{
 161	unsigned int i, j;
 162	char linebuf[16 * 3 + 1];
 163
 164	for (i = 0; i < length; i += 16) {
 165		for (j = 0; (j < 16) && (j + i < length); j++) {
 166			sprintf(linebuf + 3 * j, "%02x ", block[i + j]);
 167		}
 168		linebuf[3 * j] = '\0';
 169		TRACE_PS("%s", linebuf);
 170	}
 171}
 172
 173/*************************************************************
 174 * Driver initialisation
 175 *************************************************************/
 176
 177/*************************************************************
 178 * Module support routines
 179 *************************************************************/
 180
 181static void __exit r3964_exit(void)
 182{
 183	int status;
 184
 185	TRACE_M("cleanup_module()");
 186
 187	status = tty_unregister_ldisc(N_R3964);
 188
 189	if (status != 0) {
 190		printk(KERN_ERR "r3964: error unregistering linediscipline: "
 191				"%d\n", status);
 192	} else {
 193		TRACE_L("linediscipline successfully unregistered");
 194	}
 195}
 196
 197static int __init r3964_init(void)
 198{
 199	int status;
 200
 201	printk("r3964: Philips r3964 Driver $Revision: 1.10 $\n");
 202
 203	/*
 204	 * Register the tty line discipline
 205	 */
 206
 207	status = tty_register_ldisc(N_R3964, &tty_ldisc_N_R3964);
 208	if (status == 0) {
 209		TRACE_L("line discipline %d registered", N_R3964);
 210		TRACE_L("flags=%x num=%x", tty_ldisc_N_R3964.flags,
 211			tty_ldisc_N_R3964.num);
 212		TRACE_L("open=%p", tty_ldisc_N_R3964.open);
 213		TRACE_L("tty_ldisc_N_R3964 = %p", &tty_ldisc_N_R3964);
 214	} else {
 215		printk(KERN_ERR "r3964: error registering line discipline: "
 216				"%d\n", status);
 217	}
 218	return status;
 219}
 220
 221module_init(r3964_init);
 222module_exit(r3964_exit);
 223
 224/*************************************************************
 225 * Protocol implementation routines
 226 *************************************************************/
 227
 228static void add_tx_queue(struct r3964_info *pInfo,
 229			 struct r3964_block_header *pHeader)
 230{
 231	unsigned long flags;
 232
 233	spin_lock_irqsave(&pInfo->lock, flags);
 234
 235	pHeader->next = NULL;
 236
 237	if (pInfo->tx_last == NULL) {
 238		pInfo->tx_first = pInfo->tx_last = pHeader;
 239	} else {
 240		pInfo->tx_last->next = pHeader;
 241		pInfo->tx_last = pHeader;
 242	}
 243
 244	spin_unlock_irqrestore(&pInfo->lock, flags);
 245
 246	TRACE_Q("add_tx_queue %p, length %d, tx_first = %p",
 247		pHeader, pHeader->length, pInfo->tx_first);
 248}
 249
 250static void remove_from_tx_queue(struct r3964_info *pInfo, int error_code)
 251{
 252	struct r3964_block_header *pHeader;
 253	unsigned long flags;
 254#ifdef DEBUG_QUEUE
 255	struct r3964_block_header *pDump;
 256#endif
 257
 258	pHeader = pInfo->tx_first;
 259
 260	if (pHeader == NULL)
 261		return;
 262
 263#ifdef DEBUG_QUEUE
 264	printk("r3964: remove_from_tx_queue: %p, length %u - ",
 265		pHeader, pHeader->length);
 266	for (pDump = pHeader; pDump; pDump = pDump->next)
 267		printk("%p ", pDump);
 268	printk("\n");
 269#endif
 270
 271	if (pHeader->owner) {
 272		if (error_code) {
 273			add_msg(pHeader->owner, R3964_MSG_ACK, 0,
 274				error_code, NULL);
 275		} else {
 276			add_msg(pHeader->owner, R3964_MSG_ACK, pHeader->length,
 277				error_code, NULL);
 278		}
 279		wake_up_interruptible(&pInfo->tty->read_wait);
 280	}
 281
 282	spin_lock_irqsave(&pInfo->lock, flags);
 283
 284	pInfo->tx_first = pHeader->next;
 285	if (pInfo->tx_first == NULL) {
 286		pInfo->tx_last = NULL;
 287	}
 288
 289	spin_unlock_irqrestore(&pInfo->lock, flags);
 290
 291	kfree(pHeader);
 292	TRACE_M("remove_from_tx_queue - kfree %p", pHeader);
 293
 294	TRACE_Q("remove_from_tx_queue: tx_first = %p, tx_last = %p",
 295		pInfo->tx_first, pInfo->tx_last);
 296}
 297
 298static void add_rx_queue(struct r3964_info *pInfo,
 299			 struct r3964_block_header *pHeader)
 300{
 301	unsigned long flags;
 302
 303	spin_lock_irqsave(&pInfo->lock, flags);
 304
 305	pHeader->next = NULL;
 306
 307	if (pInfo->rx_last == NULL) {
 308		pInfo->rx_first = pInfo->rx_last = pHeader;
 309	} else {
 310		pInfo->rx_last->next = pHeader;
 311		pInfo->rx_last = pHeader;
 312	}
 313	pInfo->blocks_in_rx_queue++;
 314
 315	spin_unlock_irqrestore(&pInfo->lock, flags);
 316
 317	TRACE_Q("add_rx_queue: %p, length = %d, rx_first = %p, count = %d",
 318		pHeader, pHeader->length,
 319		pInfo->rx_first, pInfo->blocks_in_rx_queue);
 320}
 321
 322static void remove_from_rx_queue(struct r3964_info *pInfo,
 323				 struct r3964_block_header *pHeader)
 324{
 325	unsigned long flags;
 326	struct r3964_block_header *pFind;
 327
 328	if (pHeader == NULL)
 329		return;
 330
 331	TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
 332		pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue);
 333	TRACE_Q("remove_from_rx_queue: %p, length %u",
 334		pHeader, pHeader->length);
 335
 336	spin_lock_irqsave(&pInfo->lock, flags);
 337
 338	if (pInfo->rx_first == pHeader) {
 339		/* Remove the first block in the linked list: */
 340		pInfo->rx_first = pHeader->next;
 341
 342		if (pInfo->rx_first == NULL) {
 343			pInfo->rx_last = NULL;
 344		}
 345		pInfo->blocks_in_rx_queue--;
 346	} else {
 347		/* Find block to remove: */
 348		for (pFind = pInfo->rx_first; pFind; pFind = pFind->next) {
 349			if (pFind->next == pHeader) {
 350				/* Got it. */
 351				pFind->next = pHeader->next;
 352				pInfo->blocks_in_rx_queue--;
 353				if (pFind->next == NULL) {
 354					/* Oh, removed the last one! */
 355					pInfo->rx_last = pFind;
 356				}
 357				break;
 358			}
 359		}
 360	}
 361
 362	spin_unlock_irqrestore(&pInfo->lock, flags);
 363
 364	kfree(pHeader);
 365	TRACE_M("remove_from_rx_queue - kfree %p", pHeader);
 366
 367	TRACE_Q("remove_from_rx_queue: rx_first = %p, rx_last = %p, count = %d",
 368		pInfo->rx_first, pInfo->rx_last, pInfo->blocks_in_rx_queue);
 369}
 370
 371static void put_char(struct r3964_info *pInfo, unsigned char ch)
 372{
 373	struct tty_struct *tty = pInfo->tty;
 374	/* FIXME: put_char should not be called from an IRQ */
 375	tty_put_char(tty, ch);
 376	pInfo->bcc ^= ch;
 377}
 378
 379static void flush(struct r3964_info *pInfo)
 380{
 381	struct tty_struct *tty = pInfo->tty;
 382
 383	if (tty == NULL || tty->ops->flush_chars == NULL)
 384		return;
 385	tty->ops->flush_chars(tty);
 386}
 387
 388static void trigger_transmit(struct r3964_info *pInfo)
 389{
 390	unsigned long flags;
 391
 392	spin_lock_irqsave(&pInfo->lock, flags);
 393
 394	if ((pInfo->state == R3964_IDLE) && (pInfo->tx_first != NULL)) {
 395		pInfo->state = R3964_TX_REQUEST;
 396		pInfo->nRetry = 0;
 397		pInfo->flags &= ~R3964_ERROR;
 398		mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
 399
 400		spin_unlock_irqrestore(&pInfo->lock, flags);
 401
 402		TRACE_PS("trigger_transmit - sent STX");
 403
 404		put_char(pInfo, STX);
 405		flush(pInfo);
 406
 407		pInfo->bcc = 0;
 408	} else {
 409		spin_unlock_irqrestore(&pInfo->lock, flags);
 410	}
 411}
 412
 413static void retry_transmit(struct r3964_info *pInfo)
 414{
 415	if (pInfo->nRetry < R3964_MAX_RETRIES) {
 416		TRACE_PE("transmission failed. Retry #%d", pInfo->nRetry);
 417		pInfo->bcc = 0;
 418		put_char(pInfo, STX);
 419		flush(pInfo);
 420		pInfo->state = R3964_TX_REQUEST;
 421		pInfo->nRetry++;
 422		mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
 423	} else {
 424		TRACE_PE("transmission failed after %d retries",
 425			 R3964_MAX_RETRIES);
 426
 427		remove_from_tx_queue(pInfo, R3964_TX_FAIL);
 428
 429		put_char(pInfo, NAK);
 430		flush(pInfo);
 431		pInfo->state = R3964_IDLE;
 432
 433		trigger_transmit(pInfo);
 434	}
 435}
 436
 437static void transmit_block(struct r3964_info *pInfo)
 438{
 439	struct tty_struct *tty = pInfo->tty;
 440	struct r3964_block_header *pBlock = pInfo->tx_first;
 441	int room = 0;
 442
 443	if (tty == NULL || pBlock == NULL) {
 444		return;
 445	}
 446
 447	room = tty_write_room(tty);
 448
 449	TRACE_PS("transmit_block %p, room %d, length %d",
 450		 pBlock, room, pBlock->length);
 451
 452	while (pInfo->tx_position < pBlock->length) {
 453		if (room < 2)
 454			break;
 455
 456		if (pBlock->data[pInfo->tx_position] == DLE) {
 457			/* send additional DLE char: */
 458			put_char(pInfo, DLE);
 459		}
 460		put_char(pInfo, pBlock->data[pInfo->tx_position++]);
 461
 462		room--;
 463	}
 464
 465	if ((pInfo->tx_position == pBlock->length) && (room >= 3)) {
 466		put_char(pInfo, DLE);
 467		put_char(pInfo, ETX);
 468		if (pInfo->flags & R3964_BCC) {
 469			put_char(pInfo, pInfo->bcc);
 470		}
 471		pInfo->state = R3964_WAIT_FOR_TX_ACK;
 472		mod_timer(&pInfo->tmr, jiffies + R3964_TO_QVZ);
 473	}
 474	flush(pInfo);
 475}
 476
 477static void on_receive_block(struct r3964_info *pInfo)
 478{
 479	unsigned int length;
 480	struct r3964_client_info *pClient;
 481	struct r3964_block_header *pBlock;
 482
 483	length = pInfo->rx_position;
 484
 485	/* compare byte checksum characters: */
 486	if (pInfo->flags & R3964_BCC) {
 487		if (pInfo->bcc != pInfo->last_rx) {
 488			TRACE_PE("checksum error - got %x but expected %x",
 489				 pInfo->last_rx, pInfo->bcc);
 490			pInfo->flags |= R3964_CHECKSUM;
 491		}
 492	}
 493
 494	/* check for errors (parity, overrun,...): */
 495	if (pInfo->flags & R3964_ERROR) {
 496		TRACE_PE("on_receive_block - transmission failed error %x",
 497			 pInfo->flags & R3964_ERROR);
 498
 499		put_char(pInfo, NAK);
 500		flush(pInfo);
 501		if (pInfo->nRetry < R3964_MAX_RETRIES) {
 502			pInfo->state = R3964_WAIT_FOR_RX_REPEAT;
 503			pInfo->nRetry++;
 504			mod_timer(&pInfo->tmr, jiffies + R3964_TO_RX_PANIC);
 505		} else {
 506			TRACE_PE("on_receive_block - failed after max retries");
 507			pInfo->state = R3964_IDLE;
 508		}
 509		return;
 510	}
 511
 512	/* received block; submit DLE: */
 513	put_char(pInfo, DLE);
 514	flush(pInfo);
 515	del_timer_sync(&pInfo->tmr);
 516	TRACE_PS(" rx success: got %d chars", length);
 517
 518	/* prepare struct r3964_block_header: */
 519	pBlock = kmalloc(length + sizeof(struct r3964_block_header),
 520			GFP_KERNEL);
 521	TRACE_M("on_receive_block - kmalloc %p", pBlock);
 522
 523	if (pBlock == NULL)
 524		return;
 525
 526	pBlock->length = length;
 527	pBlock->data = ((unsigned char *)pBlock) +
 528			sizeof(struct r3964_block_header);
 529	pBlock->locks = 0;
 530	pBlock->next = NULL;
 531	pBlock->owner = NULL;
 532
 533	memcpy(pBlock->data, pInfo->rx_buf, length);
 534
 535	/* queue block into rx_queue: */
 536	add_rx_queue(pInfo, pBlock);
 537
 538	/* notify attached client processes: */
 539	for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) {
 540		if (pClient->sig_flags & R3964_SIG_DATA) {
 541			add_msg(pClient, R3964_MSG_DATA, length, R3964_OK,
 542				pBlock);
 543		}
 544	}
 545	wake_up_interruptible(&pInfo->tty->read_wait);
 546
 547	pInfo->state = R3964_IDLE;
 548
 549	trigger_transmit(pInfo);
 550}
 551
 552static void receive_char(struct r3964_info *pInfo, const unsigned char c)
 553{
 554	switch (pInfo->state) {
 555	case R3964_TX_REQUEST:
 556		if (c == DLE) {
 557			TRACE_PS("TX_REQUEST - got DLE");
 558
 559			pInfo->state = R3964_TRANSMITTING;
 560			pInfo->tx_position = 0;
 561
 562			transmit_block(pInfo);
 563		} else if (c == STX) {
 564			if (pInfo->nRetry == 0) {
 565				TRACE_PE("TX_REQUEST - init conflict");
 566				if (pInfo->priority == R3964_SLAVE) {
 567					goto start_receiving;
 568				}
 569			} else {
 570				TRACE_PE("TX_REQUEST - secondary init "
 571					"conflict!? Switching to SLAVE mode "
 572					"for next rx.");
 573				goto start_receiving;
 574			}
 575		} else {
 576			TRACE_PE("TX_REQUEST - char != DLE: %x", c);
 577			retry_transmit(pInfo);
 578		}
 579		break;
 580	case R3964_TRANSMITTING:
 581		if (c == NAK) {
 582			TRACE_PE("TRANSMITTING - got NAK");
 583			retry_transmit(pInfo);
 584		} else {
 585			TRACE_PE("TRANSMITTING - got invalid char");
 586
 587			pInfo->state = R3964_WAIT_ZVZ_BEFORE_TX_RETRY;
 588			mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
 589		}
 590		break;
 591	case R3964_WAIT_FOR_TX_ACK:
 592		if (c == DLE) {
 593			TRACE_PS("WAIT_FOR_TX_ACK - got DLE");
 594			remove_from_tx_queue(pInfo, R3964_OK);
 595
 596			pInfo->state = R3964_IDLE;
 597			trigger_transmit(pInfo);
 598		} else {
 599			retry_transmit(pInfo);
 600		}
 601		break;
 602	case R3964_WAIT_FOR_RX_REPEAT:
 603		/* FALLTHROUGH */
 604	case R3964_IDLE:
 605		if (c == STX) {
 606			/* Prevent rx_queue from overflow: */
 607			if (pInfo->blocks_in_rx_queue >=
 608			    R3964_MAX_BLOCKS_IN_RX_QUEUE) {
 609				TRACE_PE("IDLE - got STX but no space in "
 610						"rx_queue!");
 611				pInfo->state = R3964_WAIT_FOR_RX_BUF;
 612				mod_timer(&pInfo->tmr,
 613					  jiffies + R3964_TO_NO_BUF);
 614				break;
 615			}
 616start_receiving:
 617			/* Ok, start receiving: */
 618			TRACE_PS("IDLE - got STX");
 619			pInfo->rx_position = 0;
 620			pInfo->last_rx = 0;
 621			pInfo->flags &= ~R3964_ERROR;
 622			pInfo->state = R3964_RECEIVING;
 623			mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
 624			pInfo->nRetry = 0;
 625			put_char(pInfo, DLE);
 626			flush(pInfo);
 627			pInfo->bcc = 0;
 628		}
 629		break;
 630	case R3964_RECEIVING:
 631		if (pInfo->rx_position < RX_BUF_SIZE) {
 632			pInfo->bcc ^= c;
 633
 634			if (c == DLE) {
 635				if (pInfo->last_rx == DLE) {
 636					pInfo->last_rx = 0;
 637					goto char_to_buf;
 638				}
 639				pInfo->last_rx = DLE;
 640				break;
 641			} else if ((c == ETX) && (pInfo->last_rx == DLE)) {
 642				if (pInfo->flags & R3964_BCC) {
 643					pInfo->state = R3964_WAIT_FOR_BCC;
 644					mod_timer(&pInfo->tmr,
 645						  jiffies + R3964_TO_ZVZ);
 646				} else {
 647					on_receive_block(pInfo);
 648				}
 649			} else {
 650				pInfo->last_rx = c;
 651char_to_buf:
 652				pInfo->rx_buf[pInfo->rx_position++] = c;
 653				mod_timer(&pInfo->tmr, jiffies + R3964_TO_ZVZ);
 654			}
 655		}
 656		/* else: overflow-msg? BUF_SIZE>MTU; should not happen? */
 657		break;
 658	case R3964_WAIT_FOR_BCC:
 659		pInfo->last_rx = c;
 660		on_receive_block(pInfo);
 661		break;
 662	}
 663}
 664
 665static void receive_error(struct r3964_info *pInfo, const char flag)
 666{
 667	switch (flag) {
 668	case TTY_NORMAL:
 669		break;
 670	case TTY_BREAK:
 671		TRACE_PE("received break");
 672		pInfo->flags |= R3964_BREAK;
 673		break;
 674	case TTY_PARITY:
 675		TRACE_PE("parity error");
 676		pInfo->flags |= R3964_PARITY;
 677		break;
 678	case TTY_FRAME:
 679		TRACE_PE("frame error");
 680		pInfo->flags |= R3964_FRAME;
 681		break;
 682	case TTY_OVERRUN:
 683		TRACE_PE("frame overrun");
 684		pInfo->flags |= R3964_OVERRUN;
 685		break;
 686	default:
 687		TRACE_PE("receive_error - unknown flag %d", flag);
 688		pInfo->flags |= R3964_UNKNOWN;
 689		break;
 690	}
 691}
 692
 693static void on_timeout(unsigned long priv)
 694{
 695	struct r3964_info *pInfo = (void *)priv;
 696
 697	switch (pInfo->state) {
 698	case R3964_TX_REQUEST:
 699		TRACE_PE("TX_REQUEST - timeout");
 700		retry_transmit(pInfo);
 701		break;
 702	case R3964_WAIT_ZVZ_BEFORE_TX_RETRY:
 703		put_char(pInfo, NAK);
 704		flush(pInfo);
 705		retry_transmit(pInfo);
 706		break;
 707	case R3964_WAIT_FOR_TX_ACK:
 708		TRACE_PE("WAIT_FOR_TX_ACK - timeout");
 709		retry_transmit(pInfo);
 710		break;
 711	case R3964_WAIT_FOR_RX_BUF:
 712		TRACE_PE("WAIT_FOR_RX_BUF - timeout");
 713		put_char(pInfo, NAK);
 714		flush(pInfo);
 715		pInfo->state = R3964_IDLE;
 716		break;
 717	case R3964_RECEIVING:
 718		TRACE_PE("RECEIVING - timeout after %d chars",
 719			 pInfo->rx_position);
 720		put_char(pInfo, NAK);
 721		flush(pInfo);
 722		pInfo->state = R3964_IDLE;
 723		break;
 724	case R3964_WAIT_FOR_RX_REPEAT:
 725		TRACE_PE("WAIT_FOR_RX_REPEAT - timeout");
 726		pInfo->state = R3964_IDLE;
 727		break;
 728	case R3964_WAIT_FOR_BCC:
 729		TRACE_PE("WAIT_FOR_BCC - timeout");
 730		put_char(pInfo, NAK);
 731		flush(pInfo);
 732		pInfo->state = R3964_IDLE;
 733		break;
 734	}
 735}
 736
 737static struct r3964_client_info *findClient(struct r3964_info *pInfo,
 738		struct pid *pid)
 739{
 740	struct r3964_client_info *pClient;
 741
 742	for (pClient = pInfo->firstClient; pClient; pClient = pClient->next) {
 743		if (pClient->pid == pid) {
 744			return pClient;
 745		}
 746	}
 747	return NULL;
 748}
 749
 750static int enable_signals(struct r3964_info *pInfo, struct pid *pid, int arg)
 751{
 752	struct r3964_client_info *pClient;
 753	struct r3964_client_info **ppClient;
 754	struct r3964_message *pMsg;
 755
 756	if ((arg & R3964_SIG_ALL) == 0) {
 757		/* Remove client from client list */
 758		for (ppClient = &pInfo->firstClient; *ppClient;
 759		     ppClient = &(*ppClient)->next) {
 760			pClient = *ppClient;
 761
 762			if (pClient->pid == pid) {
 763				TRACE_PS("removing client %d from client list",
 764					 pid_nr(pid));
 765				*ppClient = pClient->next;
 766				while (pClient->msg_count) {
 767					pMsg = remove_msg(pInfo, pClient);
 768					if (pMsg) {
 769						kfree(pMsg);
 770						TRACE_M("enable_signals - msg "
 771							"kfree %p", pMsg);
 772					}
 773				}
 774				put_pid(pClient->pid);
 775				kfree(pClient);
 776				TRACE_M("enable_signals - kfree %p", pClient);
 777				return 0;
 778			}
 779		}
 780		return -EINVAL;
 781	} else {
 782		pClient = findClient(pInfo, pid);
 783		if (pClient) {
 784			/* update signal options */
 785			pClient->sig_flags = arg;
 786		} else {
 787			/* add client to client list */
 788			pClient = kmalloc(sizeof(struct r3964_client_info),
 789					GFP_KERNEL);
 790			TRACE_M("enable_signals - kmalloc %p", pClient);
 791			if (pClient == NULL)
 792				return -ENOMEM;
 793
 794			TRACE_PS("add client %d to client list", pid_nr(pid));
 795			spin_lock_init(&pClient->lock);
 796			pClient->sig_flags = arg;
 797			pClient->pid = get_pid(pid);
 798			pClient->next = pInfo->firstClient;
 799			pClient->first_msg = NULL;
 800			pClient->last_msg = NULL;
 801			pClient->next_block_to_read = NULL;
 802			pClient->msg_count = 0;
 803			pInfo->firstClient = pClient;
 804		}
 805	}
 806
 807	return 0;
 808}
 809
 810static int read_telegram(struct r3964_info *pInfo, struct pid *pid,
 811			 unsigned char __user * buf)
 812{
 813	struct r3964_client_info *pClient;
 814	struct r3964_block_header *block;
 815
 816	if (!buf) {
 817		return -EINVAL;
 818	}
 819
 820	pClient = findClient(pInfo, pid);
 821	if (pClient == NULL) {
 822		return -EINVAL;
 823	}
 824
 825	block = pClient->next_block_to_read;
 826	if (!block) {
 827		return 0;
 828	} else {
 829		if (copy_to_user(buf, block->data, block->length))
 830			return -EFAULT;
 831
 832		remove_client_block(pInfo, pClient);
 833		return block->length;
 834	}
 835
 836	return -EINVAL;
 837}
 838
 839static void add_msg(struct r3964_client_info *pClient, int msg_id, int arg,
 840		int error_code, struct r3964_block_header *pBlock)
 841{
 842	struct r3964_message *pMsg;
 843	unsigned long flags;
 844
 845	if (pClient->msg_count < R3964_MAX_MSG_COUNT - 1) {
 846queue_the_message:
 847
 848		pMsg = kmalloc(sizeof(struct r3964_message),
 849				error_code ? GFP_ATOMIC : GFP_KERNEL);
 850		TRACE_M("add_msg - kmalloc %p", pMsg);
 851		if (pMsg == NULL) {
 852			return;
 853		}
 854
 855		spin_lock_irqsave(&pClient->lock, flags);
 856
 857		pMsg->msg_id = msg_id;
 858		pMsg->arg = arg;
 859		pMsg->error_code = error_code;
 860		pMsg->block = pBlock;
 861		pMsg->next = NULL;
 862
 863		if (pClient->last_msg == NULL) {
 864			pClient->first_msg = pClient->last_msg = pMsg;
 865		} else {
 866			pClient->last_msg->next = pMsg;
 867			pClient->last_msg = pMsg;
 868		}
 869
 870		pClient->msg_count++;
 871
 872		if (pBlock != NULL) {
 873			pBlock->locks++;
 874		}
 875		spin_unlock_irqrestore(&pClient->lock, flags);
 876	} else {
 877		if ((pClient->last_msg->msg_id == R3964_MSG_ACK)
 878		    && (pClient->last_msg->error_code == R3964_OVERFLOW)) {
 879			pClient->last_msg->arg++;
 880			TRACE_PE("add_msg - inc prev OVERFLOW-msg");
 881		} else {
 882			msg_id = R3964_MSG_ACK;
 883			arg = 0;
 884			error_code = R3964_OVERFLOW;
 885			pBlock = NULL;
 886			TRACE_PE("add_msg - queue OVERFLOW-msg");
 887			goto queue_the_message;
 888		}
 889	}
 890	/* Send SIGIO signal to client process: */
 891	if (pClient->sig_flags & R3964_USE_SIGIO) {
 892		kill_pid(pClient->pid, SIGIO, 1);
 893	}
 894}
 895
 896static struct r3964_message *remove_msg(struct r3964_info *pInfo,
 897					struct r3964_client_info *pClient)
 898{
 899	struct r3964_message *pMsg = NULL;
 900	unsigned long flags;
 901
 902	if (pClient->first_msg) {
 903		spin_lock_irqsave(&pClient->lock, flags);
 904
 905		pMsg = pClient->first_msg;
 906		pClient->first_msg = pMsg->next;
 907		if (pClient->first_msg == NULL) {
 908			pClient->last_msg = NULL;
 909		}
 910
 911		pClient->msg_count--;
 912		if (pMsg->block) {
 913			remove_client_block(pInfo, pClient);
 914			pClient->next_block_to_read = pMsg->block;
 915		}
 916		spin_unlock_irqrestore(&pClient->lock, flags);
 917	}
 918	return pMsg;
 919}
 920
 921static void remove_client_block(struct r3964_info *pInfo,
 922				struct r3964_client_info *pClient)
 923{
 924	struct r3964_block_header *block;
 925
 926	TRACE_PS("remove_client_block PID %d", pid_nr(pClient->pid));
 927
 928	block = pClient->next_block_to_read;
 929	if (block) {
 930		block->locks--;
 931		if (block->locks == 0) {
 932			remove_from_rx_queue(pInfo, block);
 933		}
 934	}
 935	pClient->next_block_to_read = NULL;
 936}
 937
 938/*************************************************************
 939 * Line discipline routines
 940 *************************************************************/
 941
 942static int r3964_open(struct tty_struct *tty)
 943{
 944	struct r3964_info *pInfo;
 945
 946	TRACE_L("open");
 947	TRACE_L("tty=%p, PID=%d, disc_data=%p",
 948		tty, current->pid, tty->disc_data);
 949
 950	pInfo = kmalloc(sizeof(struct r3964_info), GFP_KERNEL);
 951	TRACE_M("r3964_open - info kmalloc %p", pInfo);
 952
 953	if (!pInfo) {
 954		printk(KERN_ERR "r3964: failed to alloc info structure\n");
 955		return -ENOMEM;
 956	}
 957
 958	pInfo->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL);
 959	TRACE_M("r3964_open - rx_buf kmalloc %p", pInfo->rx_buf);
 960
 961	if (!pInfo->rx_buf) {
 962		printk(KERN_ERR "r3964: failed to alloc receive buffer\n");
 963		kfree(pInfo);
 964		TRACE_M("r3964_open - info kfree %p", pInfo);
 965		return -ENOMEM;
 966	}
 967
 968	pInfo->tx_buf = kmalloc(TX_BUF_SIZE, GFP_KERNEL);
 969	TRACE_M("r3964_open - tx_buf kmalloc %p", pInfo->tx_buf);
 970
 971	if (!pInfo->tx_buf) {
 972		printk(KERN_ERR "r3964: failed to alloc transmit buffer\n");
 973		kfree(pInfo->rx_buf);
 974		TRACE_M("r3964_open - rx_buf kfree %p", pInfo->rx_buf);
 975		kfree(pInfo);
 976		TRACE_M("r3964_open - info kfree %p", pInfo);
 977		return -ENOMEM;
 978	}
 979
 980	spin_lock_init(&pInfo->lock);
 981	mutex_init(&pInfo->read_lock);
 982	pInfo->tty = tty;
 983	pInfo->priority = R3964_MASTER;
 984	pInfo->rx_first = pInfo->rx_last = NULL;
 985	pInfo->tx_first = pInfo->tx_last = NULL;
 986	pInfo->rx_position = 0;
 987	pInfo->tx_position = 0;
 988	pInfo->last_rx = 0;
 989	pInfo->blocks_in_rx_queue = 0;
 990	pInfo->firstClient = NULL;
 991	pInfo->state = R3964_IDLE;
 992	pInfo->flags = R3964_DEBUG;
 993	pInfo->nRetry = 0;
 994
 995	tty->disc_data = pInfo;
 996	tty->receive_room = 65536;
 997
 998	setup_timer(&pInfo->tmr, on_timeout, (unsigned long)pInfo);
 999
1000	return 0;
1001}
1002
1003static void r3964_close(struct tty_struct *tty)
1004{
1005	struct r3964_info *pInfo = tty->disc_data;
1006	struct r3964_client_info *pClient, *pNext;
1007	struct r3964_message *pMsg;
1008	struct r3964_block_header *pHeader, *pNextHeader;
1009	unsigned long flags;
1010
1011	TRACE_L("close");
1012
1013	/*
1014	 * Make sure that our task queue isn't activated.  If it
1015	 * is, take it out of the linked list.
1016	 */
1017	del_timer_sync(&pInfo->tmr);
1018
1019	/* Remove client-structs and message queues: */
1020	pClient = pInfo->firstClient;
1021	while (pClient) {
1022		pNext = pClient->next;
1023		while (pClient->msg_count) {
1024			pMsg = remove_msg(pInfo, pClient);
1025			if (pMsg) {
1026				kfree(pMsg);
1027				TRACE_M("r3964_close - msg kfree %p", pMsg);
1028			}
1029		}
1030		put_pid(pClient->pid);
1031		kfree(pClient);
1032		TRACE_M("r3964_close - client kfree %p", pClient);
1033		pClient = pNext;
1034	}
1035	/* Remove jobs from tx_queue: */
1036	spin_lock_irqsave(&pInfo->lock, flags);
1037	pHeader = pInfo->tx_first;
1038	pInfo->tx_first = pInfo->tx_last = NULL;
1039	spin_unlock_irqrestore(&pInfo->lock, flags);
1040
1041	while (pHeader) {
1042		pNextHeader = pHeader->next;
1043		kfree(pHeader);
1044		pHeader = pNextHeader;
1045	}
1046
1047	/* Free buffers: */
1048	kfree(pInfo->rx_buf);
1049	TRACE_M("r3964_close - rx_buf kfree %p", pInfo->rx_buf);
1050	kfree(pInfo->tx_buf);
1051	TRACE_M("r3964_close - tx_buf kfree %p", pInfo->tx_buf);
1052	kfree(pInfo);
1053	TRACE_M("r3964_close - info kfree %p", pInfo);
1054}
1055
1056static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
1057			  unsigned char __user * buf, size_t nr)
1058{
1059	struct r3964_info *pInfo = tty->disc_data;
1060	struct r3964_client_info *pClient;
1061	struct r3964_message *pMsg;
1062	struct r3964_client_message theMsg;
1063	int ret;
1064
1065	TRACE_L("read()");
1066
1067	/*
1068	 *	Internal serialization of reads.
1069	 */
1070	if (file->f_flags & O_NONBLOCK) {
1071		if (!mutex_trylock(&pInfo->read_lock))
1072			return -EAGAIN;
1073	} else {
1074		if (mutex_lock_interruptible(&pInfo->read_lock))
1075			return -ERESTARTSYS;
1076	}
1077
1078	pClient = findClient(pInfo, task_pid(current));
1079	if (pClient) {
1080		pMsg = remove_msg(pInfo, pClient);
1081		if (pMsg == NULL) {
1082			/* no messages available. */
1083			if (file->f_flags & O_NONBLOCK) {
1084				ret = -EAGAIN;
1085				goto unlock;
1086			}
1087			/* block until there is a message: */
1088			wait_event_interruptible(tty->read_wait,
1089					(pMsg = remove_msg(pInfo, pClient)));
1090		}
1091
1092		/* If we still haven't got a message, we must have been signalled */
1093
1094		if (!pMsg) {
1095			ret = -EINTR;
1096			goto unlock;
1097		}
1098
1099		/* deliver msg to client process: */
1100		theMsg.msg_id = pMsg->msg_id;
1101		theMsg.arg = pMsg->arg;
1102		theMsg.error_code = pMsg->error_code;
1103		ret = sizeof(struct r3964_client_message);
1104
1105		kfree(pMsg);
1106		TRACE_M("r3964_read - msg kfree %p", pMsg);
1107
1108		if (copy_to_user(buf, &theMsg, ret)) {
1109			ret = -EFAULT;
1110			goto unlock;
1111		}
1112
1113		TRACE_PS("read - return %d", ret);
1114		goto unlock;
1115	}
1116	ret = -EPERM;
1117unlock:
1118	mutex_unlock(&pInfo->read_lock);
1119	return ret;
1120}
1121
1122static ssize_t r3964_write(struct tty_struct *tty, struct file *file,
1123			   const unsigned char *data, size_t count)
1124{
1125	struct r3964_info *pInfo = tty->disc_data;
1126	struct r3964_block_header *pHeader;
1127	struct r3964_client_info *pClient;
1128	unsigned char *new_data;
1129
1130	TRACE_L("write request, %d characters", count);
1131/* 
1132 * Verify the pointers 
1133 */
1134
1135	if (!pInfo)
1136		return -EIO;
1137
1138/*
1139 * Ensure that the caller does not wish to send too much.
1140 */
1141	if (count > R3964_MTU) {
1142		if (pInfo->flags & R3964_DEBUG) {
1143			TRACE_L(KERN_WARNING "r3964_write: truncating user "
1144				"packet from %u to mtu %d", count, R3964_MTU);
1145		}
1146		count = R3964_MTU;
1147	}
1148/*
1149 * Allocate a buffer for the data and copy it from the buffer with header prepended
1150 */
1151	new_data = kmalloc(count + sizeof(struct r3964_block_header),
1152			GFP_KERNEL);
1153	TRACE_M("r3964_write - kmalloc %p", new_data);
1154	if (new_data == NULL) {
1155		if (pInfo->flags & R3964_DEBUG) {
1156			printk(KERN_ERR "r3964_write: no memory\n");
1157		}
1158		return -ENOSPC;
1159	}
1160
1161	pHeader = (struct r3964_block_header *)new_data;
1162	pHeader->data = new_data + sizeof(struct r3964_block_header);
1163	pHeader->length = count;
1164	pHeader->locks = 0;
1165	pHeader->owner = NULL;
1166
1167	pClient = findClient(pInfo, task_pid(current));
1168	if (pClient) {
1169		pHeader->owner = pClient;
1170	}
1171
1172	memcpy(pHeader->data, data, count);	/* We already verified this */
1173
1174	if (pInfo->flags & R3964_DEBUG) {
1175		dump_block(pHeader->data, count);
1176	}
1177
1178/*
1179 * Add buffer to transmit-queue:
1180 */
1181	add_tx_queue(pInfo, pHeader);
1182	trigger_transmit(pInfo);
1183
1184	return 0;
1185}
1186
1187static int r3964_ioctl(struct tty_struct *tty, struct file *file,
1188		unsigned int cmd, unsigned long arg)
1189{
1190	struct r3964_info *pInfo = tty->disc_data;
1191	if (pInfo == NULL)
1192		return -EINVAL;
1193	switch (cmd) {
1194	case R3964_ENABLE_SIGNALS:
1195		return enable_signals(pInfo, task_pid(current), arg);
1196	case R3964_SETPRIORITY:
1197		if (arg < R3964_MASTER || arg > R3964_SLAVE)
1198			return -EINVAL;
1199		pInfo->priority = arg & 0xff;
1200		return 0;
1201	case R3964_USE_BCC:
1202		if (arg)
1203			pInfo->flags |= R3964_BCC;
1204		else
1205			pInfo->flags &= ~R3964_BCC;
1206		return 0;
1207	case R3964_READ_TELEGRAM:
1208		return read_telegram(pInfo, task_pid(current),
1209				(unsigned char __user *)arg);
1210	default:
1211		return -ENOIOCTLCMD;
1212	}
1213}
1214
1215static void r3964_set_termios(struct tty_struct *tty, struct ktermios *old)
1216{
1217	TRACE_L("set_termios");
1218}
1219
1220/* Called without the kernel lock held - fine */
1221static unsigned int r3964_poll(struct tty_struct *tty, struct file *file,
1222			struct poll_table_struct *wait)
1223{
1224	struct r3964_info *pInfo = tty->disc_data;
1225	struct r3964_client_info *pClient;
1226	struct r3964_message *pMsg = NULL;
1227	unsigned long flags;
1228	int result = POLLOUT;
1229
1230	TRACE_L("POLL");
1231
1232	pClient = findClient(pInfo, task_pid(current));
1233	if (pClient) {
1234		poll_wait(file, &tty->read_wait, wait);
1235		spin_lock_irqsave(&pInfo->lock, flags);
1236		pMsg = pClient->first_msg;
1237		spin_unlock_irqrestore(&pInfo->lock, flags);
1238		if (pMsg)
1239			result |= POLLIN | POLLRDNORM;
1240	} else {
1241		result = -EINVAL;
1242	}
1243	return result;
1244}
1245
1246static void r3964_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1247			char *fp, int count)
1248{
1249	struct r3964_info *pInfo = tty->disc_data;
1250	const unsigned char *p;
1251	char *f, flags = TTY_NORMAL;
1252	int i;
1253
1254	for (i = count, p = cp, f = fp; i; i--, p++) {
1255		if (f)
1256			flags = *f++;
1257		if (flags == TTY_NORMAL) {
1258			receive_char(pInfo, *p);
1259		} else {
1260			receive_error(pInfo, flags);
1261		}
1262
1263	}
1264}
1265
1266MODULE_LICENSE("GPL");
1267MODULE_ALIAS_LDISC(N_R3964);