x25_asy.c 18.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 *	Things to sort out:
 *
 *	o	tbusy handling
 *	o	allow users to set the parameters
 *	o	sync/async switching ?
 *
 *	Note: This does _not_ implement CCITT X.25 asynchronous framing
 *	recommendations. Its primarily for testing purposes. If you wanted
 *	to do CCITT then in theory all you need is to nick the HDLC async
 *	checksum routines from ppp.c
 *      Changes:
 *
 *	2000-10-29	Henner Eisen	lapb_data_indication() return status.
 */

#include <linux/module.h>

#include <asm/system.h>
A
Alan Cox 已提交
20
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33
#include <linux/bitops.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/interrupt.h>
#include <linux/in.h>
#include <linux/tty.h>
#include <linux/errno.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/if_arp.h>
#include <linux/lapb.h>
#include <linux/init.h>
34
#include <linux/rtnetlink.h>
35
#include <linux/compat.h>
36
#include <linux/slab.h>
37
#include <net/x25device.h>
L
Linus Torvalds 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#include "x25_asy.h"

static struct net_device **x25_asy_devs;
static int x25_asy_maxdev = SL_NRUNIT;

module_param(x25_asy_maxdev, int, 0);
MODULE_LICENSE("GPL");

static int x25_asy_esc(unsigned char *p, unsigned char *d, int len);
static void x25_asy_unesc(struct x25_asy *sl, unsigned char c);
static void x25_asy_setup(struct net_device *dev);

/* Find a free X.25 channel, and link in this `tty' line. */
static struct x25_asy *x25_asy_alloc(void)
{
	struct net_device *dev = NULL;
	struct x25_asy *sl;
	int i;

	if (x25_asy_devs == NULL)
		return NULL;	/* Master array missing ! */

	for (i = 0; i < x25_asy_maxdev; i++) {
		dev = x25_asy_devs[i];

		/* Not allocated ? */
		if (dev == NULL)
			break;

67
		sl = netdev_priv(dev);
L
Linus Torvalds 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
		/* Not in use ? */
		if (!test_and_set_bit(SLF_INUSE, &sl->flags))
			return sl;
	}


	/* Sorry, too many, all slots in use */
	if (i >= x25_asy_maxdev)
		return NULL;

	/* If no channels are available, allocate one */
	if (!dev) {
		char name[IFNAMSIZ];
		sprintf(name, "x25asy%d", i);

		dev = alloc_netdev(sizeof(struct x25_asy),
				   name, x25_asy_setup);
		if (!dev)
			return NULL;

		/* Initialize channel control data */
89
		sl = netdev_priv(dev);
L
Linus Torvalds 已提交
90 91 92 93 94 95 96 97 98
		dev->base_addr    = i;

		/* register device so that it can be ifconfig'ed       */
		if (register_netdev(dev) == 0) {
			/* (Re-)Set the INUSE bit.   Very Important! */
			set_bit(SLF_INUSE, &sl->flags);
			x25_asy_devs[i] = dev;
			return sl;
		} else {
A
Alan Cox 已提交
99
			printk(KERN_WARNING "x25_asy_alloc() - register_netdev() failure.\n");
L
Linus Torvalds 已提交
100 101 102 103 104 105 106 107 108 109 110
			free_netdev(dev);
		}
	}
	return NULL;
}


/* Free an X.25 channel. */
static void x25_asy_free(struct x25_asy *sl)
{
	/* Free all X.25 frame buffers. */
111
	kfree(sl->rbuff);
L
Linus Torvalds 已提交
112
	sl->rbuff = NULL;
113
	kfree(sl->xbuff);
L
Linus Torvalds 已提交
114 115
	sl->xbuff = NULL;

A
Alan Cox 已提交
116 117 118
	if (!test_and_clear_bit(SLF_INUSE, &sl->flags))
		printk(KERN_ERR "%s: x25_asy_free for already free unit.\n",
			sl->dev->name);
L
Linus Torvalds 已提交
119 120 121 122
}

static int x25_asy_change_mtu(struct net_device *dev, int newmtu)
{
123
	struct x25_asy *sl = netdev_priv(dev);
L
Linus Torvalds 已提交
124
	unsigned char *xbuff, *rbuff;
A
Alan Cox 已提交
125
	int len = 2 * newmtu;
L
Linus Torvalds 已提交
126

127 128
	xbuff = kmalloc(len + 4, GFP_ATOMIC);
	rbuff = kmalloc(len + 4, GFP_ATOMIC);
L
Linus Torvalds 已提交
129

A
Alan Cox 已提交
130 131
	if (xbuff == NULL || rbuff == NULL) {
		printk(KERN_WARNING "%s: unable to grow X.25 buffers, MTU change cancelled.\n",
L
Linus Torvalds 已提交
132
		       dev->name);
133 134
		kfree(xbuff);
		kfree(rbuff);
L
Linus Torvalds 已提交
135 136 137 138 139 140 141 142 143 144
		return -ENOMEM;
	}

	spin_lock_bh(&sl->lock);
	xbuff    = xchg(&sl->xbuff, xbuff);
	if (sl->xleft)  {
		if (sl->xleft <= len)  {
			memcpy(sl->xbuff, sl->xhead, sl->xleft);
		} else  {
			sl->xleft = 0;
145
			dev->stats.tx_dropped++;
L
Linus Torvalds 已提交
146 147 148 149 150 151 152 153 154 155
		}
	}
	sl->xhead = sl->xbuff;

	rbuff	 = xchg(&sl->rbuff, rbuff);
	if (sl->rcount)  {
		if (sl->rcount <= len) {
			memcpy(sl->rbuff, rbuff, sl->rcount);
		} else  {
			sl->rcount = 0;
156
			dev->stats.rx_over_errors++;
L
Linus Torvalds 已提交
157 158 159 160 161 162 163 164 165
			set_bit(SLF_ERROR, &sl->flags);
		}
	}

	dev->mtu    = newmtu;
	sl->buffsize = len;

	spin_unlock_bh(&sl->lock);

166 167
	kfree(xbuff);
	kfree(rbuff);
L
Linus Torvalds 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
	return 0;
}


/* Set the "sending" flag.  This must be atomic, hence the ASM. */

static inline void x25_asy_lock(struct x25_asy *sl)
{
	netif_stop_queue(sl->dev);
}


/* Clear the "sending" flag.  This must be atomic, hence the ASM. */

static inline void x25_asy_unlock(struct x25_asy *sl)
{
	netif_wake_queue(sl->dev);
}

/* Send one completely decapsulated IP datagram to the IP layer. */

static void x25_asy_bump(struct x25_asy *sl)
{
191
	struct net_device *dev = sl->dev;
L
Linus Torvalds 已提交
192 193 194 195 196
	struct sk_buff *skb;
	int count;
	int err;

	count = sl->rcount;
197
	dev->stats.rx_bytes += count;
A
Alan Cox 已提交
198

L
Linus Torvalds 已提交
199
	skb = dev_alloc_skb(count+1);
A
Alan Cox 已提交
200 201 202
	if (skb == NULL) {
		printk(KERN_WARNING "%s: memory squeeze, dropping packet.\n",
			sl->dev->name);
203
		dev->stats.rx_dropped++;
L
Linus Torvalds 已提交
204 205
		return;
	}
A
Alan Cox 已提交
206 207
	skb_push(skb, 1);	/* LAPB internal control */
	memcpy(skb_put(skb, count), sl->rbuff, count);
L
Linus Torvalds 已提交
208
	skb->protocol = x25_type_trans(skb, sl->dev);
A
Alan Cox 已提交
209 210
	err = lapb_data_received(skb->dev, skb);
	if (err != LAPB_OK) {
L
Linus Torvalds 已提交
211
		kfree_skb(skb);
A
Alan Cox 已提交
212 213
		printk(KERN_DEBUG "x25_asy: data received err - %d\n", err);
	} else {
L
Linus Torvalds 已提交
214
		netif_rx(skb);
215
		dev->stats.rx_packets++;
L
Linus Torvalds 已提交
216 217 218 219 220 221 222 223 224
	}
}

/* Encapsulate one IP datagram and stuff into a TTY queue. */
static void x25_asy_encaps(struct x25_asy *sl, unsigned char *icp, int len)
{
	unsigned char *p;
	int actual, count, mtu = sl->dev->mtu;

A
Alan Cox 已提交
225 226
	if (len > mtu) {
		/* Sigh, shouldn't occur BUT ... */
L
Linus Torvalds 已提交
227
		len = mtu;
A
Alan Cox 已提交
228 229
		printk(KERN_DEBUG "%s: truncating oversized transmit packet!\n",
					sl->dev->name);
230
		sl->dev->stats.tx_dropped++;
L
Linus Torvalds 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
		x25_asy_unlock(sl);
		return;
	}

	p = icp;
	count = x25_asy_esc(p, (unsigned char *) sl->xbuff, len);

	/* Order of next two lines is *very* important.
	 * When we are sending a little amount of data,
	 * the transfer may be completed inside driver.write()
	 * routine, because it's running with interrupts enabled.
	 * In this case we *never* got WRITE_WAKEUP event,
	 * if we did not request it before write operation.
	 *       14 Oct 1994  Dmitry Gorodchanin.
	 */
246
	set_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
A
Alan Cox 已提交
247
	actual = sl->tty->ops->write(sl->tty, sl->xbuff, count);
L
Linus Torvalds 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260
	sl->xleft = count - actual;
	sl->xhead = sl->xbuff + actual;
	/* VSV */
	clear_bit(SLF_OUTWAIT, &sl->flags);	/* reset outfill flag */
}

/*
 * Called by the driver when there's room for more data.  If we have
 * more packets to send, we send them here.
 */
static void x25_asy_write_wakeup(struct tty_struct *tty)
{
	int actual;
A
Alan Cox 已提交
261
	struct x25_asy *sl = tty->disc_data;
L
Linus Torvalds 已提交
262 263 264 265 266

	/* First make sure we're connected. */
	if (!sl || sl->magic != X25_ASY_MAGIC || !netif_running(sl->dev))
		return;

A
Alan Cox 已提交
267
	if (sl->xleft <= 0) {
L
Linus Torvalds 已提交
268 269
		/* Now serial buffer is almost free & we can start
		 * transmission of another packet */
270
		sl->dev->stats.tx_packets++;
271
		clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
L
Linus Torvalds 已提交
272 273 274 275
		x25_asy_unlock(sl);
		return;
	}

A
Alan Cox 已提交
276
	actual = tty->ops->write(tty, sl->xhead, sl->xleft);
L
Linus Torvalds 已提交
277 278 279 280 281 282
	sl->xleft -= actual;
	sl->xhead += actual;
}

static void x25_asy_timeout(struct net_device *dev)
{
283
	struct x25_asy *sl = netdev_priv(dev);
L
Linus Torvalds 已提交
284 285 286 287 288 289 290

	spin_lock(&sl->lock);
	if (netif_queue_stopped(dev)) {
		/* May be we must check transmitter timeout here ?
		 *      14 Oct 1994 Dmitry Gorodchanin.
		 */
		printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
A
Alan Cox 已提交
291
		       (tty_chars_in_buffer(sl->tty) || sl->xleft) ?
L
Linus Torvalds 已提交
292 293
		       "bad line quality" : "driver error");
		sl->xleft = 0;
294
		clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
L
Linus Torvalds 已提交
295 296 297 298 299 300 301
		x25_asy_unlock(sl);
	}
	spin_unlock(&sl->lock);
}

/* Encapsulate an IP datagram and kick it into a TTY queue. */

302 303
static netdev_tx_t x25_asy_xmit(struct sk_buff *skb,
				      struct net_device *dev)
L
Linus Torvalds 已提交
304
{
305
	struct x25_asy *sl = netdev_priv(dev);
L
Linus Torvalds 已提交
306 307 308
	int err;

	if (!netif_running(sl->dev)) {
A
Alan Cox 已提交
309 310
		printk(KERN_ERR "%s: xmit call when iface is down\n",
			dev->name);
L
Linus Torvalds 已提交
311
		kfree_skb(skb);
312
		return NETDEV_TX_OK;
L
Linus Torvalds 已提交
313
	}
A
Alan Cox 已提交
314 315

	switch (skb->data[0]) {
316
	case X25_IFACE_DATA:
A
Alan Cox 已提交
317
		break;
318
	case X25_IFACE_CONNECT: /* Connection request .. do nothing */
A
Alan Cox 已提交
319 320 321 322
		err = lapb_connect_request(dev);
		if (err != LAPB_OK)
			printk(KERN_ERR "x25_asy: lapb_connect_request error - %d\n", err);
		kfree_skb(skb);
323
		return NETDEV_TX_OK;
324
	case X25_IFACE_DISCONNECT: /* do nothing - hang up ?? */
A
Alan Cox 已提交
325 326 327 328 329
		err = lapb_disconnect_request(dev);
		if (err != LAPB_OK)
			printk(KERN_ERR "x25_asy: lapb_disconnect_request error - %d\n", err);
	default:
		kfree_skb(skb);
330
		return NETDEV_TX_OK;
L
Linus Torvalds 已提交
331
	}
A
Alan Cox 已提交
332
	skb_pull(skb, 1);	/* Remove control byte */
L
Linus Torvalds 已提交
333 334 335 336 337 338 339 340 341 342
	/*
	 * If we are busy already- too bad.  We ought to be able
	 * to queue things at this point, to allow for a little
	 * frame buffer.  Oh well...
	 * -----------------------------------------------------
	 * I hate queues in X.25 driver. May be it's efficient,
	 * but for me latency is more important. ;)
	 * So, no queues !
	 *        14 Oct 1994  Dmitry Gorodchanin.
	 */
A
Alan Cox 已提交
343 344 345 346

	err = lapb_data_request(dev, skb);
	if (err != LAPB_OK) {
		printk(KERN_ERR "x25_asy: lapb_data_request error - %d\n", err);
L
Linus Torvalds 已提交
347
		kfree_skb(skb);
348
		return NETDEV_TX_OK;
L
Linus Torvalds 已提交
349
	}
350
	return NETDEV_TX_OK;
L
Linus Torvalds 已提交
351 352 353 354 355 356 357 358 359 360 361
}


/*
 *	LAPB interface boilerplate
 */

/*
 *	Called when I frame data arrives. We did the work above - throw it
 *	at the net layer.
 */
A
Alan Cox 已提交
362

L
Linus Torvalds 已提交
363 364 365 366 367 368 369 370 371 372
static int x25_asy_data_indication(struct net_device *dev, struct sk_buff *skb)
{
	return netif_rx(skb);
}

/*
 *	Data has emerged from the LAPB protocol machine. We don't handle
 *	busy cases too well. Its tricky to see how to do this nicely -
 *	perhaps lapb should allow us to bounce this ?
 */
A
Alan Cox 已提交
373

L
Linus Torvalds 已提交
374 375
static void x25_asy_data_transmit(struct net_device *dev, struct sk_buff *skb)
{
376
	struct x25_asy *sl = netdev_priv(dev);
A
Alan Cox 已提交
377

L
Linus Torvalds 已提交
378
	spin_lock(&sl->lock);
A
Alan Cox 已提交
379
	if (netif_queue_stopped(sl->dev) || sl->tty == NULL) {
L
Linus Torvalds 已提交
380 381 382 383 384 385
		spin_unlock(&sl->lock);
		printk(KERN_ERR "x25_asy: tbusy drop\n");
		kfree_skb(skb);
		return;
	}
	/* We were not busy, so we are now... :-) */
A
Alan Cox 已提交
386
	if (skb != NULL) {
L
Linus Torvalds 已提交
387
		x25_asy_lock(sl);
388
		dev->stats.tx_bytes += skb->len;
L
Linus Torvalds 已提交
389 390 391 392 393 394 395 396 397
		x25_asy_encaps(sl, skb->data, skb->len);
		dev_kfree_skb(skb);
	}
	spin_unlock(&sl->lock);
}

/*
 *	LAPB connection establish/down information.
 */
A
Alan Cox 已提交
398

L
Linus Torvalds 已提交
399 400
static void x25_asy_connected(struct net_device *dev, int reason)
{
401
	struct x25_asy *sl = netdev_priv(dev);
L
Linus Torvalds 已提交
402 403 404
	struct sk_buff *skb;
	unsigned char *ptr;

A
Alan Cox 已提交
405 406 407
	skb = dev_alloc_skb(1);
	if (skb == NULL) {
		printk(KERN_ERR "x25_asy: out of memory\n");
L
Linus Torvalds 已提交
408 409 410 411
		return;
	}

	ptr  = skb_put(skb, 1);
412
	*ptr = X25_IFACE_CONNECT;
L
Linus Torvalds 已提交
413 414 415 416 417 418 419

	skb->protocol = x25_type_trans(skb, sl->dev);
	netif_rx(skb);
}

static void x25_asy_disconnected(struct net_device *dev, int reason)
{
420
	struct x25_asy *sl = netdev_priv(dev);
L
Linus Torvalds 已提交
421 422 423
	struct sk_buff *skb;
	unsigned char *ptr;

A
Alan Cox 已提交
424 425
	skb = dev_alloc_skb(1);
	if (skb == NULL) {
L
Linus Torvalds 已提交
426 427 428 429 430
		printk(KERN_ERR "x25_asy: out of memory\n");
		return;
	}

	ptr  = skb_put(skb, 1);
431
	*ptr = X25_IFACE_DISCONNECT;
L
Linus Torvalds 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450

	skb->protocol = x25_type_trans(skb, sl->dev);
	netif_rx(skb);
}

static struct lapb_register_struct x25_asy_callbacks = {
	.connect_confirmation = x25_asy_connected,
	.connect_indication = x25_asy_connected,
	.disconnect_confirmation = x25_asy_disconnected,
	.disconnect_indication = x25_asy_disconnected,
	.data_indication = x25_asy_data_indication,
	.data_transmit = x25_asy_data_transmit,

};


/* Open the low-level part of the X.25 channel. Easy! */
static int x25_asy_open(struct net_device *dev)
{
451
	struct x25_asy *sl = netdev_priv(dev);
L
Linus Torvalds 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
	unsigned long len;
	int err;

	if (sl->tty == NULL)
		return -ENODEV;

	/*
	 * Allocate the X.25 frame buffers:
	 *
	 * rbuff	Receive buffer.
	 * xbuff	Transmit buffer.
	 */

	len = dev->mtu * 2;

467
	sl->rbuff = kmalloc(len + 4, GFP_KERNEL);
A
Alan Cox 已提交
468
	if (sl->rbuff == NULL)
L
Linus Torvalds 已提交
469
		goto norbuff;
470
	sl->xbuff = kmalloc(len + 4, GFP_KERNEL);
A
Alan Cox 已提交
471
	if (sl->xbuff == NULL)
L
Linus Torvalds 已提交
472 473 474 475 476 477 478 479
		goto noxbuff;

	sl->buffsize = len;
	sl->rcount   = 0;
	sl->xleft    = 0;
	sl->flags   &= (1 << SLF_INUSE);      /* Clear ESCAPE & ERROR flags */

	netif_start_queue(dev);
A
Alan Cox 已提交
480

L
Linus Torvalds 已提交
481 482 483
	/*
	 *	Now attach LAPB
	 */
A
Alan Cox 已提交
484 485
	err = lapb_register(dev, &x25_asy_callbacks);
	if (err == LAPB_OK)
L
Linus Torvalds 已提交
486 487 488 489 490 491 492 493 494 495 496 497 498 499
		return 0;

	/* Cleanup */
	kfree(sl->xbuff);
noxbuff:
	kfree(sl->rbuff);
norbuff:
	return -ENOMEM;
}


/* Close the low-level part of the X.25 channel. Easy! */
static int x25_asy_close(struct net_device *dev)
{
500
	struct x25_asy *sl = netdev_priv(dev);
L
Linus Torvalds 已提交
501 502

	spin_lock(&sl->lock);
A
Alan Cox 已提交
503
	if (sl->tty)
504
		clear_bit(TTY_DO_WRITE_WAKEUP, &sl->tty->flags);
L
Linus Torvalds 已提交
505 506 507 508 509 510 511 512 513 514 515 516 517 518

	netif_stop_queue(dev);
	sl->rcount = 0;
	sl->xleft  = 0;
	spin_unlock(&sl->lock);
	return 0;
}

/*
 * Handle the 'receiver data ready' interrupt.
 * This function is called by the 'tty_io' module in the kernel when
 * a block of X.25 data has been received, which can now be decapsulated
 * and sent on to some IP layer for further processing.
 */
A
Alan Cox 已提交
519

520
static void x25_asy_receive_buf(struct tty_struct *tty,
A
Alan Cox 已提交
521
				const unsigned char *cp, char *fp, int count)
L
Linus Torvalds 已提交
522
{
A
Alan Cox 已提交
523
	struct x25_asy *sl = tty->disc_data;
L
Linus Torvalds 已提交
524 525 526 527 528 529

	if (!sl || sl->magic != X25_ASY_MAGIC || !netif_running(sl->dev))
		return;


	/* Read the characters out of the buffer */
530
	while (count--) {
L
Linus Torvalds 已提交
531
		if (fp && *fp++) {
A
Alan Cox 已提交
532
			if (!test_and_set_bit(SLF_ERROR, &sl->flags))
533
				sl->dev->stats.rx_errors++;
L
Linus Torvalds 已提交
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
			cp++;
			continue;
		}
		x25_asy_unesc(sl, *cp++);
	}
}

/*
 * Open the high-level part of the X.25 channel.
 * This function is called by the TTY module when the
 * X.25 line discipline is called for.  Because we are
 * sure the tty line exists, we only have to link it to
 * a free X.25 channel...
 */

static int x25_asy_open_tty(struct tty_struct *tty)
{
A
Alan Cox 已提交
551
	struct x25_asy *sl = tty->disc_data;
L
Linus Torvalds 已提交
552 553
	int err;

A
Alan Cox 已提交
554 555 556
	if (tty->ops->write == NULL)
		return -EOPNOTSUPP;

L
Linus Torvalds 已提交
557
	/* First make sure we're not already connected. */
A
Alan Cox 已提交
558
	if (sl && sl->magic == X25_ASY_MAGIC)
L
Linus Torvalds 已提交
559 560 561
		return -EEXIST;

	/* OK.  Find a free X.25 channel to use. */
A
Alan Cox 已提交
562 563
	sl = x25_asy_alloc();
	if (sl == NULL)
L
Linus Torvalds 已提交
564 565 566 567
		return -ENFILE;

	sl->tty = tty;
	tty->disc_data = sl;
A
Alan Cox 已提交
568
	tty->receive_room = 65536;
A
Alan Cox 已提交
569
	tty_driver_flush_buffer(tty);
A
Alan Cox 已提交
570
	tty_ldisc_flush(tty);
L
Linus Torvalds 已提交
571 572 573

	/* Restore default settings */
	sl->dev->type = ARPHRD_X25;
A
Alan Cox 已提交
574

L
Linus Torvalds 已提交
575
	/* Perform the low-level X.25 async init */
A
Alan Cox 已提交
576 577
	err = x25_asy_open(sl->dev);
	if (err)
L
Linus Torvalds 已提交
578 579
		return err;
	/* Done.  We have linked the TTY line to a channel. */
J
Jiri Slaby 已提交
580
	return 0;
L
Linus Torvalds 已提交
581 582 583 584 585 586 587 588 589 590 591
}


/*
 * Close down an X.25 channel.
 * This means flushing out any pending queues, and then restoring the
 * TTY line discipline to what it was before it got hooked to X.25
 * (which usually is TTY again).
 */
static void x25_asy_close_tty(struct tty_struct *tty)
{
A
Alan Cox 已提交
592
	struct x25_asy *sl = tty->disc_data;
593
	int err;
L
Linus Torvalds 已提交
594 595 596 597 598

	/* First make sure we're connected. */
	if (!sl || sl->magic != X25_ASY_MAGIC)
		return;

599
	rtnl_lock();
L
Linus Torvalds 已提交
600
	if (sl->dev->flags & IFF_UP)
A
Alan Cox 已提交
601
		dev_close(sl->dev);
602
	rtnl_unlock();
L
Linus Torvalds 已提交
603

604 605 606 607 608
	err = lapb_unregister(sl->dev);
	if (err != LAPB_OK)
		printk(KERN_ERR "x25_asy_close: lapb_unregister error -%d\n",
			err);

L
Linus Torvalds 已提交
609 610 611 612 613 614 615 616 617
	tty->disc_data = NULL;
	sl->tty = NULL;
	x25_asy_free(sl);
}

 /************************************************************************
  *			STANDARD X.25 ENCAPSULATION		  	 *
  ************************************************************************/

618
static int x25_asy_esc(unsigned char *s, unsigned char *d, int len)
L
Linus Torvalds 已提交
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
{
	unsigned char *ptr = d;
	unsigned char c;

	/*
	 * Send an initial END character to flush out any
	 * data that may have accumulated in the receiver
	 * due to line noise.
	 */

	*ptr++ = X25_END;	/* Send 10111110 bit seq */

	/*
	 * For each byte in the packet, send the appropriate
	 * character sequence, according to the X.25 protocol.
	 */

A
Alan Cox 已提交
636 637 638 639 640 641 642 643 644 645 646 647 648
	while (len-- > 0) {
		switch (c = *s++) {
		case X25_END:
			*ptr++ = X25_ESC;
			*ptr++ = X25_ESCAPE(X25_END);
			break;
		case X25_ESC:
			*ptr++ = X25_ESC;
			*ptr++ = X25_ESCAPE(X25_ESC);
			break;
		default:
			*ptr++ = c;
			break;
L
Linus Torvalds 已提交
649 650 651
		}
	}
	*ptr++ = X25_END;
652
	return ptr - d;
L
Linus Torvalds 已提交
653 654 655 656 657
}

static void x25_asy_unesc(struct x25_asy *sl, unsigned char s)
{

A
Alan Cox 已提交
658 659
	switch (s) {
	case X25_END:
660 661
		if (!test_and_clear_bit(SLF_ERROR, &sl->flags) &&
		    sl->rcount > 2)
A
Alan Cox 已提交
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
			x25_asy_bump(sl);
		clear_bit(SLF_ESCAPE, &sl->flags);
		sl->rcount = 0;
		return;
	case X25_ESC:
		set_bit(SLF_ESCAPE, &sl->flags);
		return;
	case X25_ESCAPE(X25_ESC):
	case X25_ESCAPE(X25_END):
		if (test_and_clear_bit(SLF_ESCAPE, &sl->flags))
			s = X25_UNESCAPE(s);
		break;
	}
	if (!test_bit(SLF_ERROR, &sl->flags)) {
		if (sl->rcount < sl->buffsize) {
L
Linus Torvalds 已提交
677 678 679
			sl->rbuff[sl->rcount++] = s;
			return;
		}
680
		sl->dev->stats.rx_over_errors++;
L
Linus Torvalds 已提交
681 682 683 684 685 686 687 688 689
		set_bit(SLF_ERROR, &sl->flags);
	}
}


/* Perform I/O control on an active X.25 channel. */
static int x25_asy_ioctl(struct tty_struct *tty, struct file *file,
			 unsigned int cmd,  unsigned long arg)
{
A
Alan Cox 已提交
690
	struct x25_asy *sl = tty->disc_data;
L
Linus Torvalds 已提交
691 692 693 694 695

	/* First make sure we're connected. */
	if (!sl || sl->magic != X25_ASY_MAGIC)
		return -EINVAL;

A
Alan Cox 已提交
696
	switch (cmd) {
L
Linus Torvalds 已提交
697 698 699 700 701 702 703 704
	case SIOCGIFNAME:
		if (copy_to_user((void __user *)arg, sl->dev->name,
					strlen(sl->dev->name) + 1))
			return -EFAULT;
		return 0;
	case SIOCSIFHWADDR:
		return -EINVAL;
	default:
705
		return tty_mode_ioctl(tty, file, cmd, arg);
L
Linus Torvalds 已提交
706 707 708
	}
}

709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
#ifdef CONFIG_COMPAT
static long x25_asy_compat_ioctl(struct tty_struct *tty, struct file *file,
			 unsigned int cmd,  unsigned long arg)
{
	switch (cmd) {
	case SIOCGIFNAME:
	case SIOCSIFHWADDR:
		return x25_asy_ioctl(tty, file, cmd,
				     (unsigned long)compat_ptr(arg));
	}

	return -ENOIOCTLCMD;
}
#endif

L
Linus Torvalds 已提交
724 725
static int x25_asy_open_dev(struct net_device *dev)
{
726
	struct x25_asy *sl = netdev_priv(dev);
A
Alan Cox 已提交
727
	if (sl->tty == NULL)
L
Linus Torvalds 已提交
728 729 730 731
		return -ENODEV;
	return 0;
}

732 733 734 735 736 737 738 739
static const struct net_device_ops x25_asy_netdev_ops = {
	.ndo_open	= x25_asy_open_dev,
	.ndo_stop	= x25_asy_close,
	.ndo_start_xmit	= x25_asy_xmit,
	.ndo_tx_timeout	= x25_asy_timeout,
	.ndo_change_mtu	= x25_asy_change_mtu,
};

L
Linus Torvalds 已提交
740 741 742
/* Initialise the X.25 driver.  Called by the device init code */
static void x25_asy_setup(struct net_device *dev)
{
743
	struct x25_asy *sl = netdev_priv(dev);
L
Linus Torvalds 已提交
744 745 746 747 748 749 750

	sl->magic  = X25_ASY_MAGIC;
	sl->dev	   = dev;
	spin_lock_init(&sl->lock);
	set_bit(SLF_INUSE, &sl->flags);

	/*
A
Alan Cox 已提交
751
	 *	Finish setting up the DEVICE info.
L
Linus Torvalds 已提交
752
	 */
A
Alan Cox 已提交
753

L
Linus Torvalds 已提交
754
	dev->mtu		= SL_MTU;
755
	dev->netdev_ops		= &x25_asy_netdev_ops;
L
Linus Torvalds 已提交
756 757 758 759 760 761 762 763 764 765
	dev->watchdog_timeo	= HZ*20;
	dev->hard_header_len	= 0;
	dev->addr_len		= 0;
	dev->type		= ARPHRD_X25;
	dev->tx_queue_len	= 10;

	/* New-style flags. */
	dev->flags		= IFF_NOARP;
}

A
Alan Cox 已提交
766
static struct tty_ldisc_ops x25_ldisc = {
L
Linus Torvalds 已提交
767 768 769 770 771 772
	.owner		= THIS_MODULE,
	.magic		= TTY_LDISC_MAGIC,
	.name		= "X.25",
	.open		= x25_asy_open_tty,
	.close		= x25_asy_close_tty,
	.ioctl		= x25_asy_ioctl,
773 774 775
#ifdef CONFIG_COMPAT
	.compat_ioctl	= x25_asy_compat_ioctl,
#endif
L
Linus Torvalds 已提交
776 777 778 779 780 781 782 783 784 785
	.receive_buf	= x25_asy_receive_buf,
	.write_wakeup	= x25_asy_write_wakeup,
};

static int __init init_x25_asy(void)
{
	if (x25_asy_maxdev < 4)
		x25_asy_maxdev = 4; /* Sanity */

	printk(KERN_INFO "X.25 async: version 0.00 ALPHA "
A
Alan Cox 已提交
786
			"(dynamic channels, max=%d).\n", x25_asy_maxdev);
L
Linus Torvalds 已提交
787

A
Alan Cox 已提交
788 789
	x25_asy_devs = kcalloc(x25_asy_maxdev, sizeof(struct net_device *),
				GFP_KERNEL);
L
Linus Torvalds 已提交
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
	if (!x25_asy_devs) {
		printk(KERN_WARNING "X25 async: Can't allocate x25_asy_ctrls[] "
				"array! Uaargh! (-> No X.25 available)\n");
		return -ENOMEM;
	}

	return tty_register_ldisc(N_X25, &x25_ldisc);
}


static void __exit exit_x25_asy(void)
{
	struct net_device *dev;
	int i;

	for (i = 0; i < x25_asy_maxdev; i++) {
		dev = x25_asy_devs[i];
		if (dev) {
808
			struct x25_asy *sl = netdev_priv(dev);
L
Linus Torvalds 已提交
809 810

			spin_lock_bh(&sl->lock);
A
Alan Cox 已提交
811
			if (sl->tty)
L
Linus Torvalds 已提交
812 813 814 815 816 817 818 819 820 821 822 823 824
				tty_hangup(sl->tty);

			spin_unlock_bh(&sl->lock);
			/*
			 * VSV = if dev->start==0, then device
			 * unregistered while close proc.
			 */
			unregister_netdev(dev);
			free_netdev(dev);
		}
	}

	kfree(x25_asy_devs);
825
	tty_unregister_ldisc(N_X25);
L
Linus Torvalds 已提交
826 827 828 829
}

module_init(init_x25_asy);
module_exit(exit_x25_asy);