6pack.c 23.2 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
L
Linus Torvalds 已提交
2 3 4 5 6
/*
 * 6pack.c	This module implements the 6pack protocol for kernel-based
 *		devices like TTY. It interfaces between a raw TTY and the
 *		kernel's AX.25 protocol layers.
 *
7
 * Authors:	Andreas Könsgen <ajk@comnets.uni-bremen.de>
L
Linus Torvalds 已提交
8 9 10 11 12 13 14 15 16
 *              Ralf Baechle DL5RB <ralf@linux-mips.org>
 *
 * Quite a lot of stuff "stolen" by Joerg Reuter from slip.c, written by
 *
 *		Laurence Culhane, <loz@holmes.demon.co.uk>
 *		Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
 */

#include <linux/module.h>
17
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
18 19 20 21 22 23 24 25 26
#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/timer.h>
27
#include <linux/slab.h>
L
Linus Torvalds 已提交
28 29 30 31 32 33 34 35 36
#include <net/ax25.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/rtnetlink.h>
#include <linux/spinlock.h>
#include <linux/if_arp.h>
#include <linux/init.h>
#include <linux/ip.h>
#include <linux/tcp.h>
37
#include <linux/semaphore.h>
38
#include <linux/refcount.h>
L
Linus Torvalds 已提交
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 67 68 69 70

#define SIXPACK_VERSION    "Revision: 0.3.0"

/* sixpack priority commands */
#define SIXP_SEOF		0x40	/* start and end of a 6pack frame */
#define SIXP_TX_URUN		0x48	/* transmit overrun */
#define SIXP_RX_ORUN		0x50	/* receive overrun */
#define SIXP_RX_BUF_OVL		0x58	/* receive buffer overflow */

#define SIXP_CHKSUM		0xFF	/* valid checksum of a 6pack frame */

/* masks to get certain bits out of the status bytes sent by the TNC */

#define SIXP_CMD_MASK		0xC0
#define SIXP_CHN_MASK		0x07
#define SIXP_PRIO_CMD_MASK	0x80
#define SIXP_STD_CMD_MASK	0x40
#define SIXP_PRIO_DATA_MASK	0x38
#define SIXP_TX_MASK		0x20
#define SIXP_RX_MASK		0x10
#define SIXP_RX_DCD_MASK	0x18
#define SIXP_LEDS_ON		0x78
#define SIXP_LEDS_OFF		0x60
#define SIXP_CON		0x08
#define SIXP_STA		0x10

#define SIXP_FOUND_TNC		0xe9
#define SIXP_CON_ON		0x68
#define SIXP_DCD_MASK		0x08
#define SIXP_DAMA_OFF		0

/* default level 2 parameters */
71
#define SIXP_TXDELAY			25	/* 250 ms */
L
Linus Torvalds 已提交
72
#define SIXP_PERSIST			50	/* in 256ths */
73
#define SIXP_SLOTTIME			10	/* 100 ms */
L
Linus Torvalds 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
#define SIXP_INIT_RESYNC_TIMEOUT	(3*HZ/2) /* in 1 s */
#define SIXP_RESYNC_TIMEOUT		5*HZ	/* in 1 s */

/* 6pack configuration. */
#define SIXP_NRUNIT			31      /* MAX number of 6pack channels */
#define SIXP_MTU			256	/* Default MTU */

enum sixpack_flags {
	SIXPF_ERROR,	/* Parity, etc. error	*/
};

struct sixpack {
	/* Various fields. */
	struct tty_struct	*tty;		/* ptr to TTY structure	*/
	struct net_device	*dev;		/* easy for intr handling  */

	/* These are pointers to the malloc()ed frame buffers. */
	unsigned char		*rbuff;		/* receiver buffer	*/
	int			rcount;         /* received chars counter  */
	unsigned char		*xbuff;		/* transmitter buffer	*/
	unsigned char		*xhead;         /* next byte to XMIT */
	int			xleft;          /* bytes left in XMIT queue  */

	unsigned char		raw_buf[4];
	unsigned char		cooked_buf[400];

	unsigned int		rx_count;
	unsigned int		rx_count_cooked;

	int			mtu;		/* Our mtu (to spot changes!) */
	int			buffsize;       /* Max buffers sizes */

	unsigned long		flags;		/* Flag values/ mode etc */
	unsigned char		mode;		/* 6pack mode */

	/* 6pack stuff */
	unsigned char		tx_delay;
	unsigned char		persistence;
	unsigned char		slottime;
	unsigned char		duplex;
	unsigned char		led_state;
	unsigned char		status;
	unsigned char		status1;
	unsigned char		status2;
	unsigned char		tx_enable;
	unsigned char		tnc_state;

	struct timer_list	tx_t;
	struct timer_list	resync_t;
123
	refcount_t		refcnt;
124
	struct completion	dead;
L
Linus Torvalds 已提交
125 126 127 128 129
	spinlock_t		lock;
};

#define AX25_6PACK_HEADER_LEN 0

A
Alan Cox 已提交
130
static void sixpack_decode(struct sixpack *, const unsigned char[], int);
L
Linus Torvalds 已提交
131 132 133
static int encode_sixpack(unsigned char *, unsigned char *, int, unsigned char);

/*
134
 * Perform the persistence/slottime algorithm for CSMA access. If the
L
Linus Torvalds 已提交
135 136 137 138
 * persistence check was successful, write the data to the serial driver.
 * Note that in case of DAMA operation, the data is not sent here.
 */

139
static void sp_xmit_on_air(struct timer_list *t)
L
Linus Torvalds 已提交
140
{
141
	struct sixpack *sp = from_timer(sp, t, tx_t);
142
	int actual, when = sp->slottime;
L
Linus Torvalds 已提交
143 144 145 146 147 148
	static unsigned char random;

	random = random * 17 + 41;

	if (((sp->status1 & SIXP_DCD_MASK) == 0) && (random < sp->persistence)) {
		sp->led_state = 0x70;
A
Alan Cox 已提交
149
		sp->tty->ops->write(sp->tty, &sp->led_state, 1);
L
Linus Torvalds 已提交
150
		sp->tx_enable = 1;
A
Alan Cox 已提交
151
		actual = sp->tty->ops->write(sp->tty, sp->xbuff, sp->status2);
L
Linus Torvalds 已提交
152 153 154
		sp->xleft -= actual;
		sp->xhead += actual;
		sp->led_state = 0x60;
A
Alan Cox 已提交
155
		sp->tty->ops->write(sp->tty, &sp->led_state, 1);
L
Linus Torvalds 已提交
156 157
		sp->status2 = 0;
	} else
158
		mod_timer(&sp->tx_t, jiffies + ((when + 1) * HZ) / 100);
L
Linus Torvalds 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
}

/* ----> 6pack timer interrupt handler and friends. <---- */

/* Encapsulate one AX.25 frame and stuff into a TTY queue. */
static void sp_encaps(struct sixpack *sp, unsigned char *icp, int len)
{
	unsigned char *msg, *p = icp;
	int actual, count;

	if (len > sp->mtu) {	/* sp->mtu = AX25_MTU = max. PACLEN = 256 */
		msg = "oversized transmit packet!";
		goto out_drop;
	}

	if (p[0] > 5) {
		msg = "invalid KISS command";
		goto out_drop;
	}

	if ((p[0] != 0) && (len > 2)) {
		msg = "KISS control packet too long";
		goto out_drop;
	}

	if ((p[0] == 0) && (len < 15)) {
		msg = "bad AX.25 packet to transmit";
		goto out_drop;
	}

	count = encode_sixpack(p, sp->xbuff, len, sp->tx_delay);
	set_bit(TTY_DO_WRITE_WAKEUP, &sp->tty->flags);

	switch (p[0]) {
	case 1:	sp->tx_delay = p[1];
		return;
	case 2:	sp->persistence = p[1];
		return;
	case 3:	sp->slottime = p[1];
		return;
	case 4:	/* ignored */
		return;
	case 5:	sp->duplex = p[1];
		return;
	}

	if (p[0] != 0)
		return;

	/*
	 * In case of fullduplex or DAMA operation, we don't take care about the
	 * state of the DCD or of any timers, as the determination of the
	 * correct time to send is the job of the AX.25 layer. We send
	 * immediately after data has arrived.
	 */
	if (sp->duplex == 1) {
		sp->led_state = 0x70;
A
Alan Cox 已提交
216
		sp->tty->ops->write(sp->tty, &sp->led_state, 1);
L
Linus Torvalds 已提交
217
		sp->tx_enable = 1;
A
Alan Cox 已提交
218
		actual = sp->tty->ops->write(sp->tty, sp->xbuff, count);
L
Linus Torvalds 已提交
219 220 221
		sp->xleft = count - actual;
		sp->xhead = sp->xbuff + actual;
		sp->led_state = 0x60;
A
Alan Cox 已提交
222
		sp->tty->ops->write(sp->tty, &sp->led_state, 1);
L
Linus Torvalds 已提交
223 224 225 226
	} else {
		sp->xleft = count;
		sp->xhead = sp->xbuff;
		sp->status2 = count;
227
		sp_xmit_on_air(&sp->tx_t);
L
Linus Torvalds 已提交
228 229 230 231 232
	}

	return;

out_drop:
233
	sp->dev->stats.tx_dropped++;
L
Linus Torvalds 已提交
234 235 236 237 238 239 240
	netif_start_queue(sp->dev);
	if (net_ratelimit())
		printk(KERN_DEBUG "%s: %s - dropped.\n", sp->dev->name, msg);
}

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

241
static netdev_tx_t sp_xmit(struct sk_buff *skb, struct net_device *dev)
L
Linus Torvalds 已提交
242 243 244
{
	struct sixpack *sp = netdev_priv(dev);

245 246 247
	if (skb->protocol == htons(ETH_P_IP))
		return ax25_ip_xmit(skb);

L
Linus Torvalds 已提交
248 249 250
	spin_lock_bh(&sp->lock);
	/* We were not busy, so we are now... :-) */
	netif_stop_queue(dev);
251
	dev->stats.tx_bytes += skb->len;
L
Linus Torvalds 已提交
252 253 254 255 256
	sp_encaps(sp, skb->data, skb->len);
	spin_unlock_bh(&sp->lock);

	dev_kfree_skb(skb);

257
	return NETDEV_TX_OK;
L
Linus Torvalds 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
}

static int sp_open_dev(struct net_device *dev)
{
	struct sixpack *sp = netdev_priv(dev);

	if (sp->tty == NULL)
		return -ENODEV;
	return 0;
}

/* Close the low-level part of the 6pack channel. */
static int sp_close(struct net_device *dev)
{
	struct sixpack *sp = netdev_priv(dev);

	spin_lock_bh(&sp->lock);
	if (sp->tty) {
		/* TTY discipline is running. */
		clear_bit(TTY_DO_WRITE_WAKEUP, &sp->tty->flags);
	}
	netif_stop_queue(dev);
	spin_unlock_bh(&sp->lock);

	return 0;
}

static int sp_set_mac_address(struct net_device *dev, void *addr)
{
	struct sockaddr_ax25 *sa = addr;

H
Herbert Xu 已提交
289
	netif_tx_lock_bh(dev);
290
	netif_addr_lock(dev);
291
	__dev_addr_set(dev, &sa->sax25_call, AX25_ADDR_LEN);
292
	netif_addr_unlock(dev);
H
Herbert Xu 已提交
293
	netif_tx_unlock_bh(dev);
L
Linus Torvalds 已提交
294 295 296 297

	return 0;
}

298 299 300 301 302 303 304
static const struct net_device_ops sp_netdev_ops = {
	.ndo_open		= sp_open_dev,
	.ndo_stop		= sp_close,
	.ndo_start_xmit		= sp_xmit,
	.ndo_set_mac_address    = sp_set_mac_address,
};

L
Linus Torvalds 已提交
305 306 307
static void sp_setup(struct net_device *dev)
{
	/* Finish setting up the DEVICE info. */
308
	dev->netdev_ops		= &sp_netdev_ops;
309
	dev->needs_free_netdev	= true;
310
	dev->mtu		= SIXP_MTU;
L
Linus Torvalds 已提交
311
	dev->hard_header_len	= AX25_MAX_HEADER_LEN;
312
	dev->header_ops 	= &ax25_header_ops;
313

L
Linus Torvalds 已提交
314 315 316 317 318
	dev->addr_len		= AX25_ADDR_LEN;
	dev->type		= ARPHRD_AX25;
	dev->tx_queue_len	= 10;

	/* Only activated in AX.25 mode */
319
	memcpy(dev->broadcast, &ax25_bcast, AX25_ADDR_LEN);
320
	dev_addr_set(dev, (u8 *)&ax25_defaddr);
L
Linus Torvalds 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339

	dev->flags		= 0;
}

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

/*
 * This is the routine that sends the received data to the kernel AX.25.
 * 'cmd' is the KISS command. For AX.25 data, it is zero.
 */

static void sp_bump(struct sixpack *sp, char cmd)
{
	struct sk_buff *skb;
	int count;
	unsigned char *ptr;

	count = sp->rcount + 1;

340
	sp->dev->stats.rx_bytes += count;
L
Linus Torvalds 已提交
341

342
	if ((skb = dev_alloc_skb(count + 1)) == NULL)
L
Linus Torvalds 已提交
343 344
		goto out_mem;

345
	ptr = skb_put(skb, count + 1);
L
Linus Torvalds 已提交
346 347 348
	*ptr++ = cmd;	/* KISS command */

	memcpy(ptr, sp->cooked_buf + 1, count);
349
	skb->protocol = ax25_type_trans(skb, sp->dev);
L
Linus Torvalds 已提交
350
	netif_rx(skb);
351
	sp->dev->stats.rx_packets++;
L
Linus Torvalds 已提交
352 353 354 355

	return;

out_mem:
356
	sp->dev->stats.rx_dropped++;
L
Linus Torvalds 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
}


/* ----------------------------------------------------------------------- */

/*
 * We have a potential race on dereferencing tty->disc_data, because the tty
 * layer provides no locking at all - thus one cpu could be running
 * sixpack_receive_buf while another calls sixpack_close, which zeroes
 * tty->disc_data and frees the memory that sixpack_receive_buf is using.  The
 * best way to fix this is to use a rwlock in the tty struct, but for now we
 * use a single global rwlock for all ttys in ppp line discipline.
 */
static DEFINE_RWLOCK(disc_data_lock);
                                                                                
static struct sixpack *sp_get(struct tty_struct *tty)
{
	struct sixpack *sp;

	read_lock(&disc_data_lock);
	sp = tty->disc_data;
	if (sp)
379
		refcount_inc(&sp->refcnt);
L
Linus Torvalds 已提交
380 381 382 383 384 385 386
	read_unlock(&disc_data_lock);

	return sp;
}

static void sp_put(struct sixpack *sp)
{
387
	if (refcount_dec_and_test(&sp->refcnt))
388
		complete(&sp->dead);
L
Linus Torvalds 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
}

/*
 * Called by the TTY driver when there's room for more data.  If we have
 * more packets to send, we send them here.
 */
static void sixpack_write_wakeup(struct tty_struct *tty)
{
	struct sixpack *sp = sp_get(tty);
	int actual;

	if (!sp)
		return;
	if (sp->xleft <= 0)  {
		/* Now serial buffer is almost free & we can start
		 * transmission of another packet */
405
		sp->dev->stats.tx_packets++;
L
Linus Torvalds 已提交
406 407 408 409 410 411 412
		clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
		sp->tx_enable = 0;
		netif_wake_queue(sp->dev);
		goto out;
	}

	if (sp->tx_enable) {
A
Alan Cox 已提交
413
		actual = tty->ops->write(tty, sp->xhead, sp->xleft);
L
Linus Torvalds 已提交
414 415 416 417 418 419 420 421 422 423 424 425
		sp->xleft -= actual;
		sp->xhead += actual;
	}

out:
	sp_put(sp);
}

/* ----------------------------------------------------------------------- */

/*
 * Handle the 'receiver data ready' interrupt.
A
Alan Cox 已提交
426
 * This function is called by the tty module in the kernel when
L
Linus Torvalds 已提交
427 428 429
 * a block of 6pack data has been received, which can now be decapsulated
 * and sent on to some IP layer for further processing.
 */
430
static void sixpack_receive_buf(struct tty_struct *tty,
431
	const unsigned char *cp, const char *fp, int count)
L
Linus Torvalds 已提交
432 433 434 435 436
{
	struct sixpack *sp;
	int count1;

	if (!count)
437
		return;
L
Linus Torvalds 已提交
438 439 440

	sp = sp_get(tty);
	if (!sp)
441
		return;
L
Linus Torvalds 已提交
442 443 444 445 446 447 448

	/* Read the characters out of the buffer */
	count1 = count;
	while (count) {
		count--;
		if (fp && *fp++) {
			if (!test_and_set_bit(SIXPF_ERROR, &sp->flags))
449
				sp->dev->stats.rx_errors++;
L
Linus Torvalds 已提交
450 451 452
			continue;
		}
	}
A
Alan Cox 已提交
453
	sixpack_decode(sp, cp, count1);
L
Linus Torvalds 已提交
454 455

	sp_put(sp);
A
Alan Cox 已提交
456
	tty_unthrottle(tty);
L
Linus Torvalds 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
}

/*
 * Try to resync the TNC. Called by the resync timer defined in
 * decode_prio_command
 */

#define TNC_UNINITIALIZED	0
#define TNC_UNSYNC_STARTUP	1
#define TNC_UNSYNCED		2
#define TNC_IN_SYNC		3

static void __tnc_set_sync_state(struct sixpack *sp, int new_tnc_state)
{
	char *msg;

	switch (new_tnc_state) {
	default:			/* gcc oh piece-o-crap ... */
	case TNC_UNSYNC_STARTUP:
		msg = "Synchronizing with TNC";
		break;
	case TNC_UNSYNCED:
		msg = "Lost synchronization with TNC\n";
		break;
	case TNC_IN_SYNC:
		msg = "Found TNC";
		break;
	}

	sp->tnc_state = new_tnc_state;
	printk(KERN_INFO "%s: %s\n", sp->dev->name, msg);
}

static inline void tnc_set_sync_state(struct sixpack *sp, int new_tnc_state)
{
	int old_tnc_state = sp->tnc_state;

	if (old_tnc_state != new_tnc_state)
		__tnc_set_sync_state(sp, new_tnc_state);
}

498
static void resync_tnc(struct timer_list *t)
L
Linus Torvalds 已提交
499
{
500
	struct sixpack *sp = from_timer(sp, t, resync_t);
L
Linus Torvalds 已提交
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
	static char resync_cmd = 0xe8;

	/* clear any data that might have been received */

	sp->rx_count = 0;
	sp->rx_count_cooked = 0;

	/* reset state machine */

	sp->status = 1;
	sp->status1 = 1;
	sp->status2 = 0;

	/* resync the TNC */

	sp->led_state = 0x60;
A
Alan Cox 已提交
517 518
	sp->tty->ops->write(sp->tty, &sp->led_state, 1);
	sp->tty->ops->write(sp->tty, &resync_cmd, 1);
L
Linus Torvalds 已提交
519 520 521


	/* Start resync timer again -- the TNC might be still absent */
522
	mod_timer(&sp->resync_t, jiffies + SIXP_RESYNC_TIMEOUT);
L
Linus Torvalds 已提交
523 524 525 526 527 528 529 530
}

static inline int tnc_init(struct sixpack *sp)
{
	unsigned char inbyte = 0xe8;

	tnc_set_sync_state(sp, TNC_UNSYNC_STARTUP);

A
Alan Cox 已提交
531
	sp->tty->ops->write(sp->tty, &inbyte, 1);
L
Linus Torvalds 已提交
532

533
	mod_timer(&sp->resync_t, jiffies + SIXP_RESYNC_TIMEOUT);
L
Linus Torvalds 已提交
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554

	return 0;
}

/*
 * Open the high-level part of the 6pack channel.
 * This function is called by the TTY module when the
 * 6pack line discipline is called for.  Because we are
 * sure the tty line exists, we only have to link it to
 * a free 6pcack channel...
 */
static int sixpack_open(struct tty_struct *tty)
{
	char *rbuff = NULL, *xbuff = NULL;
	struct net_device *dev;
	struct sixpack *sp;
	unsigned long len;
	int err = 0;

	if (!capable(CAP_NET_ADMIN))
		return -EPERM;
A
Alan Cox 已提交
555 556
	if (tty->ops->write == NULL)
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
557

558 559
	dev = alloc_netdev(sizeof(struct sixpack), "sp%d", NET_NAME_UNKNOWN,
			   sp_setup);
L
Linus Torvalds 已提交
560 561 562 563 564 565 566 567 568
	if (!dev) {
		err = -ENOMEM;
		goto out;
	}

	sp = netdev_priv(dev);
	sp->dev = dev;

	spin_lock_init(&sp->lock);
569
	refcount_set(&sp->refcnt, 1);
570
	init_completion(&sp->dead);
L
Linus Torvalds 已提交
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611

	/* !!! length of the buffers. MTU is IP MTU, not PACLEN!  */

	len = dev->mtu * 2;

	rbuff = kmalloc(len + 4, GFP_KERNEL);
	xbuff = kmalloc(len + 4, GFP_KERNEL);

	if (rbuff == NULL || xbuff == NULL) {
		err = -ENOBUFS;
		goto out_free;
	}

	spin_lock_bh(&sp->lock);

	sp->tty = tty;

	sp->rbuff	= rbuff;
	sp->xbuff	= xbuff;

	sp->mtu		= AX25_MTU + 73;
	sp->buffsize	= len;
	sp->rcount	= 0;
	sp->rx_count	= 0;
	sp->rx_count_cooked = 0;
	sp->xleft	= 0;

	sp->flags	= 0;		/* Clear ESCAPE & ERROR flags */

	sp->duplex	= 0;
	sp->tx_delay    = SIXP_TXDELAY;
	sp->persistence = SIXP_PERSIST;
	sp->slottime    = SIXP_SLOTTIME;
	sp->led_state   = 0x60;
	sp->status      = 1;
	sp->status1     = 1;
	sp->status2     = 0;
	sp->tx_enable   = 0;

	netif_start_queue(dev);

612
	timer_setup(&sp->tx_t, sp_xmit_on_air, 0);
613

614
	timer_setup(&sp->resync_t, resync_tnc, 0);
L
Linus Torvalds 已提交
615 616 617 618 619

	spin_unlock_bh(&sp->lock);

	/* Done.  We have linked the TTY line to a channel. */
	tty->disc_data = sp;
A
Alan Cox 已提交
620
	tty->receive_room = 65536;
L
Linus Torvalds 已提交
621 622

	/* Now we're ready to register. */
623 624
	err = register_netdev(dev);
	if (err)
L
Linus Torvalds 已提交
625 626 627 628 629 630 631 632 633 634
		goto out_free;

	tnc_init(sp);

	return 0;

out_free:
	kfree(xbuff);
	kfree(rbuff);

635
	free_netdev(dev);
L
Linus Torvalds 已提交
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651

out:
	return err;
}


/*
 * Close down a 6pack channel.
 * This means flushing out any pending queues, and then restoring the
 * TTY line discipline to what it was before it got hooked to 6pack
 * (which usually is TTY again).
 */
static void sixpack_close(struct tty_struct *tty)
{
	struct sixpack *sp;

E
Eric Dumazet 已提交
652
	write_lock_irq(&disc_data_lock);
L
Linus Torvalds 已提交
653 654
	sp = tty->disc_data;
	tty->disc_data = NULL;
E
Eric Dumazet 已提交
655
	write_unlock_irq(&disc_data_lock);
A
Al Viro 已提交
656
	if (!sp)
L
Linus Torvalds 已提交
657 658 659 660 661 662
		return;

	/*
	 * We have now ensured that nobody can start using ap from now on, but
	 * we have to wait for all existing users to finish.
	 */
663
	if (!refcount_dec_and_test(&sp->refcnt))
664
		wait_for_completion(&sp->dead);
L
Linus Torvalds 已提交
665

666
	/* We must stop the queue to avoid potentially scribbling
667
	 * on the free buffers. The sp->dead completion is not sufficient
668 669 670 671
	 * to protect us from sp->xbuff access.
	 */
	netif_stop_queue(sp->dev);

672 673
	del_timer_sync(&sp->tx_t);
	del_timer_sync(&sp->resync_t);
L
Linus Torvalds 已提交
674

675 676 677
	unregister_netdev(sp->dev);

	/* Free all 6pack frame buffers after unreg. */
L
Linus Torvalds 已提交
678 679
	kfree(sp->rbuff);
	kfree(sp->xbuff);
680

681
	free_netdev(sp->dev);
L
Linus Torvalds 已提交
682 683 684 685 686 687 688
}

/* Perform I/O control on an active 6pack channel. */
static int sixpack_ioctl(struct tty_struct *tty, struct file *file,
	unsigned int cmd, unsigned long arg)
{
	struct sixpack *sp = sp_get(tty);
689
	struct net_device *dev;
L
Linus Torvalds 已提交
690 691 692 693
	unsigned int tmp, err;

	if (!sp)
		return -ENXIO;
694
	dev = sp->dev;
L
Linus Torvalds 已提交
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720

	switch(cmd) {
	case SIOCGIFNAME:
		err = copy_to_user((void __user *) arg, dev->name,
		                   strlen(dev->name) + 1) ? -EFAULT : 0;
		break;

	case SIOCGIFENCAP:
		err = put_user(0, (int __user *) arg);
		break;

	case SIOCSIFENCAP:
		if (get_user(tmp, (int __user *) arg)) {
			err = -EFAULT;
			break;
		}

		sp->mode = tmp;
		dev->addr_len        = AX25_ADDR_LEN;
		dev->hard_header_len = AX25_KISS_HEADER_LEN +
		                       AX25_MAX_HEADER_LEN + 3;
		dev->type            = ARPHRD_AX25;

		err = 0;
		break;

721 722
	case SIOCSIFHWADDR: {
			char addr[AX25_ADDR_LEN];
L
Linus Torvalds 已提交
723

724 725
			if (copy_from_user(&addr,
					   (void __user *)arg, AX25_ADDR_LEN)) {
726 727 728
				err = -EFAULT;
				break;
			}
L
Linus Torvalds 已提交
729

730
			netif_tx_lock_bh(dev);
731
			__dev_addr_set(dev, &addr, AX25_ADDR_LEN);
732 733 734 735
			netif_tx_unlock_bh(dev);
			err = 0;
			break;
		}
L
Linus Torvalds 已提交
736
	default:
737
		err = tty_mode_ioctl(tty, file, cmd, arg);
L
Linus Torvalds 已提交
738 739 740 741 742 743 744
	}

	sp_put(sp);

	return err;
}

A
Alan Cox 已提交
745
static struct tty_ldisc_ops sp_ldisc = {
L
Linus Torvalds 已提交
746
	.owner		= THIS_MODULE,
747
	.num		= N_6PACK,
L
Linus Torvalds 已提交
748 749 750 751 752 753 754 755 756 757
	.name		= "6pack",
	.open		= sixpack_open,
	.close		= sixpack_close,
	.ioctl		= sixpack_ioctl,
	.receive_buf	= sixpack_receive_buf,
	.write_wakeup	= sixpack_write_wakeup,
};

/* Initialize 6pack control device -- register 6pack line discipline */

758
static const char msg_banner[]  __initconst = KERN_INFO \
L
Linus Torvalds 已提交
759
	"AX.25: 6pack driver, " SIXPACK_VERSION "\n";
760
static const char msg_regfail[] __initconst = KERN_ERR  \
L
Linus Torvalds 已提交
761 762 763 764 765 766 767 768 769
	"6pack: can't register line discipline (err = %d)\n";

static int __init sixpack_init_driver(void)
{
	int status;

	printk(msg_banner);

	/* Register the provided line protocol discipline */
770 771
	status = tty_register_ldisc(&sp_ldisc);
	if (status)
L
Linus Torvalds 已提交
772 773 774 775 776 777 778
		printk(msg_regfail, status);

	return status;
}

static void __exit sixpack_exit_driver(void)
{
779
	tty_unregister_ldisc(&sp_ldisc);
L
Linus Torvalds 已提交
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
}

/* encode an AX.25 packet into 6pack */

static int encode_sixpack(unsigned char *tx_buf, unsigned char *tx_buf_raw,
	int length, unsigned char tx_delay)
{
	int count = 0;
	unsigned char checksum = 0, buf[400];
	int raw_count = 0;

	tx_buf_raw[raw_count++] = SIXP_PRIO_CMD_MASK | SIXP_TX_MASK;
	tx_buf_raw[raw_count++] = SIXP_SEOF;

	buf[0] = tx_delay;
	for (count = 1; count < length; count++)
		buf[count] = tx_buf[count];

	for (count = 0; count < length; count++)
		checksum += buf[count];
	buf[length] = (unsigned char) 0xff - checksum;

	for (count = 0; count <= length; count++) {
		if ((count % 3) == 0) {
			tx_buf_raw[raw_count++] = (buf[count] & 0x3f);
			tx_buf_raw[raw_count] = ((buf[count] >> 2) & 0x30);
		} else if ((count % 3) == 1) {
			tx_buf_raw[raw_count++] |= (buf[count] & 0x0f);
			tx_buf_raw[raw_count] =	((buf[count] >> 2) & 0x3c);
		} else {
			tx_buf_raw[raw_count++] |= (buf[count] & 0x03);
			tx_buf_raw[raw_count++] = (buf[count] >> 2);
		}
	}
	if ((length % 3) != 2)
		raw_count++;
	tx_buf_raw[raw_count++] = SIXP_SEOF;
	return raw_count;
}

/* decode 4 sixpack-encoded bytes into 3 data bytes */

static void decode_data(struct sixpack *sp, unsigned char inbyte)
{
	unsigned char *buf;

	if (sp->rx_count != 3) {
		sp->raw_buf[sp->rx_count++] = inbyte;

		return;
	}

832 833 834 835 836 837
	if (sp->rx_count_cooked + 2 >= sizeof(sp->cooked_buf)) {
		pr_err("6pack: cooked buffer overrun, data loss\n");
		sp->rx_count = 0;
		return;
	}

L
Linus Torvalds 已提交
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
	buf = sp->raw_buf;
	sp->cooked_buf[sp->rx_count_cooked++] =
		buf[0] | ((buf[1] << 2) & 0xc0);
	sp->cooked_buf[sp->rx_count_cooked++] =
		(buf[1] & 0x0f) | ((buf[2] << 2) & 0xf0);
	sp->cooked_buf[sp->rx_count_cooked++] =
		(buf[2] & 0x03) | (inbyte << 2);
	sp->rx_count = 0;
}

/* identify and execute a 6pack priority command byte */

static void decode_prio_command(struct sixpack *sp, unsigned char cmd)
{
	int actual;

	if ((cmd & SIXP_PRIO_DATA_MASK) != 0) {     /* idle ? */

	/* RX and DCD flags can only be set in the same prio command,
	   if the DCD flag has been set without the RX flag in the previous
	   prio command. If DCD has not been set before, something in the
	   transmission has gone wrong. In this case, RX and DCD are
	   cleared in order to prevent the decode_data routine from
	   reading further data that might be corrupt. */

		if (((sp->status & SIXP_DCD_MASK) == 0) &&
			((cmd & SIXP_RX_DCD_MASK) == SIXP_RX_DCD_MASK)) {
				if (sp->status != 1)
					printk(KERN_DEBUG "6pack: protocol violation\n");
				else
					sp->status = 0;
869
				cmd &= ~SIXP_RX_DCD_MASK;
L
Linus Torvalds 已提交
870 871 872 873 874
		}
		sp->status = cmd & SIXP_PRIO_DATA_MASK;
	} else { /* output watchdog char if idle */
		if ((sp->status2 != 0) && (sp->duplex == 1)) {
			sp->led_state = 0x70;
A
Alan Cox 已提交
875
			sp->tty->ops->write(sp->tty, &sp->led_state, 1);
L
Linus Torvalds 已提交
876
			sp->tx_enable = 1;
A
Alan Cox 已提交
877
			actual = sp->tty->ops->write(sp->tty, sp->xbuff, sp->status2);
L
Linus Torvalds 已提交
878 879 880 881 882 883 884 885 886
			sp->xleft -= actual;
			sp->xhead += actual;
			sp->led_state = 0x60;
			sp->status2 = 0;

		}
	}

	/* needed to trigger the TNC watchdog */
A
Alan Cox 已提交
887
	sp->tty->ops->write(sp->tty, &sp->led_state, 1);
L
Linus Torvalds 已提交
888 889 890 891

        /* if the state byte has been received, the TNC is present,
           so the resync timer can be reset. */

892 893
	if (sp->tnc_state == TNC_IN_SYNC)
		mod_timer(&sp->resync_t, jiffies + SIXP_INIT_RESYNC_TIMEOUT);
L
Linus Torvalds 已提交
894 895 896 897 898 899 900 901

	sp->status1 = cmd & SIXP_PRIO_DATA_MASK;
}

/* identify and execute a standard 6pack command byte */

static void decode_std_command(struct sixpack *sp, unsigned char cmd)
{
902
	unsigned char checksum = 0, rest = 0;
L
Linus Torvalds 已提交
903 904 905 906 907 908 909 910
	short i;

	switch (cmd & SIXP_CMD_MASK) {     /* normal command */
	case SIXP_SEOF:
		if ((sp->rx_count == 0) && (sp->rx_count_cooked == 0)) {
			if ((sp->status & SIXP_RX_DCD_MASK) ==
				SIXP_RX_DCD_MASK) {
				sp->led_state = 0x68;
A
Alan Cox 已提交
911
				sp->tty->ops->write(sp->tty, &sp->led_state, 1);
L
Linus Torvalds 已提交
912 913 914 915
			}
		} else {
			sp->led_state = 0x60;
			/* fill trailing bytes with zeroes */
A
Alan Cox 已提交
916
			sp->tty->ops->write(sp->tty, &sp->led_state, 1);
L
Linus Torvalds 已提交
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
			rest = sp->rx_count;
			if (rest != 0)
				 for (i = rest; i <= 3; i++)
					decode_data(sp, 0);
			if (rest == 2)
				sp->rx_count_cooked -= 2;
			else if (rest == 3)
				sp->rx_count_cooked -= 1;
			for (i = 0; i < sp->rx_count_cooked; i++)
				checksum += sp->cooked_buf[i];
			if (checksum != SIXP_CHKSUM) {
				printk(KERN_DEBUG "6pack: bad checksum %2.2x\n", checksum);
			} else {
				sp->rcount = sp->rx_count_cooked-2;
				sp_bump(sp, 0);
			}
			sp->rx_count_cooked = 0;
		}
		break;
	case SIXP_TX_URUN: printk(KERN_DEBUG "6pack: TX underrun\n");
		break;
	case SIXP_RX_ORUN: printk(KERN_DEBUG "6pack: RX overrun\n");
		break;
	case SIXP_RX_BUF_OVL:
		printk(KERN_DEBUG "6pack: RX buffer overflow\n");
	}
}

/* decode a 6pack packet */

static void
A
Alan Cox 已提交
948
sixpack_decode(struct sixpack *sp, const unsigned char *pre_rbuff, int count)
L
Linus Torvalds 已提交
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
{
	unsigned char inbyte;
	int count1;

	for (count1 = 0; count1 < count; count1++) {
		inbyte = pre_rbuff[count1];
		if (inbyte == SIXP_FOUND_TNC) {
			tnc_set_sync_state(sp, TNC_IN_SYNC);
			del_timer(&sp->resync_t);
		}
		if ((inbyte & SIXP_PRIO_CMD_MASK) != 0)
			decode_prio_command(sp, inbyte);
		else if ((inbyte & SIXP_STD_CMD_MASK) != 0)
			decode_std_command(sp, inbyte);
		else if ((sp->status & SIXP_RX_DCD_MASK) == SIXP_RX_DCD_MASK)
			decode_data(sp, inbyte);
	}
}

MODULE_AUTHOR("Ralf Baechle DO1GRB <ralf@linux-mips.org>");
MODULE_DESCRIPTION("6pack driver for AX.25");
MODULE_LICENSE("GPL");
MODULE_ALIAS_LDISC(N_6PACK);

module_init(sixpack_init_driver);
module_exit(sixpack_exit_driver);