mkiss.c 23.7 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
Ralf Baechle's avatar
Ralf Baechle committed
2 3 4
 *  This program is free software; you can distribute it and/or modify it
 *  under the terms of the GNU General Public License (Version 2) as
 *  published by the Free Software Foundation.
Linus Torvalds's avatar
Linus Torvalds committed
5
 *
Ralf Baechle's avatar
Ralf Baechle committed
6 7 8 9
 *  This program is distributed in the hope it will be useful, but WITHOUT
 *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 *  for more details.
Linus Torvalds's avatar
Linus Torvalds committed
10
 *
Ralf Baechle's avatar
Ralf Baechle committed
11 12 13
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
Linus Torvalds's avatar
Linus Torvalds committed
14
 *
Ralf Baechle's avatar
Ralf Baechle committed
15 16
 * Copyright (C) Hans Alblas PE1AYX <hans@esrac.ele.tue.nl>
 * Copyright (C) 2004, 05 Ralf Baechle DL5RB <ralf@linux-mips.org>
17
 * Copyright (C) 2004, 05 Thomas Osterried DL9SAU <thomas@x-berg.in-berlin.de>
Linus Torvalds's avatar
Linus Torvalds committed
18 19 20 21 22
 */
#include <linux/module.h>
#include <asm/system.h>
#include <linux/bitops.h>
#include <asm/uaccess.h>
23
#include <linux/crc16.h>
Linus Torvalds's avatar
Linus Torvalds committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/interrupt.h>
#include <linux/in.h>
#include <linux/inet.h>
#include <linux/tty.h>
#include <linux/errno.h>
#include <linux/netdevice.h>
#include <linux/major.h>
#include <linux/init.h>
#include <linux/rtnetlink.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/if_arp.h>
38
#include <linux/jiffies.h>
Linus Torvalds's avatar
Linus Torvalds committed
39 40 41

#include <net/ax25.h>

Ralf Baechle's avatar
Ralf Baechle committed
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 71 72 73 74 75
#define AX_MTU		236

/* SLIP/KISS protocol characters. */
#define END             0300		/* indicates end of frame	*/
#define ESC             0333		/* indicates byte stuffing	*/
#define ESC_END         0334		/* ESC ESC_END means END 'data'	*/
#define ESC_ESC         0335		/* ESC ESC_ESC means ESC 'data'	*/

struct mkiss {
	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. */
	spinlock_t		buflock;/* lock for rbuf and xbuf */
	unsigned char		*rbuff;	/* receiver buffer		*/
	int			rcount;	/* received chars counter       */
	unsigned char		*xbuff;	/* transmitter buffer		*/
	unsigned char		*xhead;	/* pointer to next byte to XMIT */
	int			xleft;	/* bytes left in XMIT queue     */

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

	unsigned long	flags;		/* Flag values/ mode etc	*/
					/* long req'd: used by set_bit --RR */
#define AXF_INUSE	0		/* Channel in use               */
#define AXF_ESCAPE	1               /* ESC received                 */
#define AXF_ERROR	2               /* Parity, etc. error           */
#define AXF_KEEPTEST	3		/* Keepalive test flag		*/
#define AXF_OUTWAIT	4		/* is outpacket was flag	*/

	int		mode;
        int		crcmode;	/* MW: for FlexNet, SMACK etc.  */
76 77 78 79 80 81 82
	int		crcauto;	/* CRC auto mode */

#define CRC_MODE_NONE		0
#define CRC_MODE_FLEX		1
#define CRC_MODE_SMACK		2
#define CRC_MODE_FLEX_TEST	3
#define CRC_MODE_SMACK_TEST	4
Ralf Baechle's avatar
Ralf Baechle committed
83 84 85 86

	atomic_t		refcnt;
	struct semaphore	dead_sem;
};
Linus Torvalds's avatar
Linus Torvalds committed
87 88 89

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

Ralf Baechle's avatar
Ralf Baechle committed
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
static const unsigned short crc_flex_table[] = {
	0x0f87, 0x1e0e, 0x2c95, 0x3d1c, 0x49a3, 0x582a, 0x6ab1, 0x7b38,
	0x83cf, 0x9246, 0xa0dd, 0xb154, 0xc5eb, 0xd462, 0xe6f9, 0xf770,
	0x1f06, 0x0e8f, 0x3c14, 0x2d9d, 0x5922, 0x48ab, 0x7a30, 0x6bb9,
	0x934e, 0x82c7, 0xb05c, 0xa1d5, 0xd56a, 0xc4e3, 0xf678, 0xe7f1,
	0x2e85, 0x3f0c, 0x0d97, 0x1c1e, 0x68a1, 0x7928, 0x4bb3, 0x5a3a,
	0xa2cd, 0xb344, 0x81df, 0x9056, 0xe4e9, 0xf560, 0xc7fb, 0xd672,
	0x3e04, 0x2f8d, 0x1d16, 0x0c9f, 0x7820, 0x69a9, 0x5b32, 0x4abb,
	0xb24c, 0xa3c5, 0x915e, 0x80d7, 0xf468, 0xe5e1, 0xd77a, 0xc6f3,
	0x4d83, 0x5c0a, 0x6e91, 0x7f18, 0x0ba7, 0x1a2e, 0x28b5, 0x393c,
	0xc1cb, 0xd042, 0xe2d9, 0xf350, 0x87ef, 0x9666, 0xa4fd, 0xb574,
	0x5d02, 0x4c8b, 0x7e10, 0x6f99, 0x1b26, 0x0aaf, 0x3834, 0x29bd,
	0xd14a, 0xc0c3, 0xf258, 0xe3d1, 0x976e, 0x86e7, 0xb47c, 0xa5f5,
	0x6c81, 0x7d08, 0x4f93, 0x5e1a, 0x2aa5, 0x3b2c, 0x09b7, 0x183e,
	0xe0c9, 0xf140, 0xc3db, 0xd252, 0xa6ed, 0xb764, 0x85ff, 0x9476,
	0x7c00, 0x6d89, 0x5f12, 0x4e9b, 0x3a24, 0x2bad, 0x1936, 0x08bf,
	0xf048, 0xe1c1, 0xd35a, 0xc2d3, 0xb66c, 0xa7e5, 0x957e, 0x84f7,
	0x8b8f, 0x9a06, 0xa89d, 0xb914, 0xcdab, 0xdc22, 0xeeb9, 0xff30,
	0x07c7, 0x164e, 0x24d5, 0x355c, 0x41e3, 0x506a, 0x62f1, 0x7378,
	0x9b0e, 0x8a87, 0xb81c, 0xa995, 0xdd2a, 0xcca3, 0xfe38, 0xefb1,
	0x1746, 0x06cf, 0x3454, 0x25dd, 0x5162, 0x40eb, 0x7270, 0x63f9,
	0xaa8d, 0xbb04, 0x899f, 0x9816, 0xeca9, 0xfd20, 0xcfbb, 0xde32,
	0x26c5, 0x374c, 0x05d7, 0x145e, 0x60e1, 0x7168, 0x43f3, 0x527a,
	0xba0c, 0xab85, 0x991e, 0x8897, 0xfc28, 0xeda1, 0xdf3a, 0xceb3,
	0x3644, 0x27cd, 0x1556, 0x04df, 0x7060, 0x61e9, 0x5372, 0x42fb,
	0xc98b, 0xd802, 0xea99, 0xfb10, 0x8faf, 0x9e26, 0xacbd, 0xbd34,
	0x45c3, 0x544a, 0x66d1, 0x7758, 0x03e7, 0x126e, 0x20f5, 0x317c,
	0xd90a, 0xc883, 0xfa18, 0xeb91, 0x9f2e, 0x8ea7, 0xbc3c, 0xadb5,
	0x5542, 0x44cb, 0x7650, 0x67d9, 0x1366, 0x02ef, 0x3074, 0x21fd,
	0xe889, 0xf900, 0xcb9b, 0xda12, 0xaead, 0xbf24, 0x8dbf, 0x9c36,
	0x64c1, 0x7548, 0x47d3, 0x565a, 0x22e5, 0x336c, 0x01f7, 0x107e,
	0xf808, 0xe981, 0xdb1a, 0xca93, 0xbe2c, 0xafa5, 0x9d3e, 0x8cb7,
	0x7440, 0x65c9, 0x5752, 0x46db, 0x3264, 0x23ed, 0x1176, 0x00ff
Linus Torvalds's avatar
Linus Torvalds committed
123 124 125 126
};

static unsigned short calc_crc_flex(unsigned char *cp, int size)
{
Ralf Baechle's avatar
Ralf Baechle committed
127
	unsigned short crc = 0xffff;
Linus Torvalds's avatar
Linus Torvalds committed
128

Ralf Baechle's avatar
Ralf Baechle committed
129 130
	while (size--)
		crc = (crc << 8) ^ crc_flex_table[((crc >> 8) ^ *cp++) & 0xff];
Linus Torvalds's avatar
Linus Torvalds committed
131

Ralf Baechle's avatar
Ralf Baechle committed
132 133
	return crc;
}
Linus Torvalds's avatar
Linus Torvalds committed
134 135 136

static int check_crc_flex(unsigned char *cp, int size)
{
Ralf Baechle's avatar
Ralf Baechle committed
137
	unsigned short crc = 0xffff;
Linus Torvalds's avatar
Linus Torvalds committed
138

Ralf Baechle's avatar
Ralf Baechle committed
139 140
	if (size < 3)
		return -1;
Linus Torvalds's avatar
Linus Torvalds committed
141

Ralf Baechle's avatar
Ralf Baechle committed
142 143
	while (size--)
		crc = (crc << 8) ^ crc_flex_table[((crc >> 8) ^ *cp++) & 0xff];
Linus Torvalds's avatar
Linus Torvalds committed
144

Ralf Baechle's avatar
Ralf Baechle committed
145 146
	if ((crc & 0xffff) != 0x7070)
		return -1;
Linus Torvalds's avatar
Linus Torvalds committed
147

Ralf Baechle's avatar
Ralf Baechle committed
148
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
149 150
}

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
static int check_crc_16(unsigned char *cp, int size)
{
	unsigned short crc = 0x0000;

	if (size < 3)
		return -1;

	crc = crc16(0, cp, size);

	if (crc != 0x0000)
		return -1;

	return 0;
}

Ralf Baechle's avatar
Ralf Baechle committed
166 167 168
/*
 * Standard encapsulation
 */
Linus Torvalds's avatar
Linus Torvalds committed
169

Ralf Baechle's avatar
Ralf Baechle committed
170
static int kiss_esc(unsigned char *s, unsigned char *d, int len)
Linus Torvalds's avatar
Linus Torvalds committed
171
{
Ralf Baechle's avatar
Ralf Baechle committed
172 173
	unsigned char *ptr = d;
	unsigned char c;
Linus Torvalds's avatar
Linus Torvalds committed
174

Ralf Baechle's avatar
Ralf Baechle committed
175 176 177 178
	/*
	 * Send an initial END character to flush out any data that may have
	 * accumulated in the receiver due to line noise.
	 */
Linus Torvalds's avatar
Linus Torvalds committed
179

Ralf Baechle's avatar
Ralf Baechle committed
180
	*ptr++ = END;
Linus Torvalds's avatar
Linus Torvalds committed
181

Ralf Baechle's avatar
Ralf Baechle committed
182 183 184 185 186
	while (len-- > 0) {
		switch (c = *s++) {
		case END:
			*ptr++ = ESC;
			*ptr++ = ESC_END;
Linus Torvalds's avatar
Linus Torvalds committed
187
			break;
Ralf Baechle's avatar
Ralf Baechle committed
188 189 190 191 192 193 194 195
		case ESC:
			*ptr++ = ESC;
			*ptr++ = ESC_ESC;
			break;
		default:
			*ptr++ = c;
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
196 197
	}

Ralf Baechle's avatar
Ralf Baechle committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
	*ptr++ = END;

	return ptr - d;
}

/*
 * MW:
 * OK its ugly, but tell me a better solution without copying the
 * packet to a temporary buffer :-)
 */
static int kiss_esc_crc(unsigned char *s, unsigned char *d, unsigned short crc,
	int len)
{
	unsigned char *ptr = d;
	unsigned char c=0;

	*ptr++ = END;
	while (len > 0) {
		if (len > 2)
			c = *s++;
		else if (len > 1)
			c = crc >> 8;
		else if (len > 0)
			c = crc & 0xff;

		len--;
Linus Torvalds's avatar
Linus Torvalds committed
224

Ralf Baechle's avatar
Ralf Baechle committed
225 226 227 228 229 230 231 232 233 234 235 236 237
		switch (c) {
		case END:
			*ptr++ = ESC;
			*ptr++ = ESC_END;
			break;
		case ESC:
			*ptr++ = ESC;
			*ptr++ = ESC_ESC;
			break;
		default:
			*ptr++ = c;
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
238
	}
Ralf Baechle's avatar
Ralf Baechle committed
239 240 241 242 243 244 245 246 247 248 249 250 251
	*ptr++ = END;

	return ptr - d;
}

/* Send one completely decapsulated AX.25 packet to the AX.25 layer. */
static void ax_bump(struct mkiss *ax)
{
	struct sk_buff *skb;
	int count;

	spin_lock_bh(&ax->buflock);
	if (ax->rbuff[0] > 0x0f) {
252 253
		if (ax->rbuff[0] & 0x80) {
			if (check_crc_16(ax->rbuff, ax->rcount) < 0) {
254
				ax->dev->stats.rx_errors++;
255 256 257 258 259 260 261 262 263 264 265 266 267
				spin_unlock_bh(&ax->buflock);

				return;
			}
			if (ax->crcmode != CRC_MODE_SMACK && ax->crcauto) {
				printk(KERN_INFO
				       "mkiss: %s: Switchting to crc-smack\n",
				       ax->dev->name);
				ax->crcmode = CRC_MODE_SMACK;
			}
			ax->rcount -= 2;
			*ax->rbuff &= ~0x80;
		} else if (ax->rbuff[0] & 0x20)  {
Ralf Baechle's avatar
Ralf Baechle committed
268
			if (check_crc_flex(ax->rbuff, ax->rcount) < 0) {
269
				ax->dev->stats.rx_errors++;
270
				spin_unlock_bh(&ax->buflock);
Ralf Baechle's avatar
Ralf Baechle committed
271 272
				return;
			}
273 274 275 276 277 278
			if (ax->crcmode != CRC_MODE_FLEX && ax->crcauto) {
				printk(KERN_INFO
				       "mkiss: %s: Switchting to crc-flexnet\n",
				       ax->dev->name);
				ax->crcmode = CRC_MODE_FLEX;
			}
Ralf Baechle's avatar
Ralf Baechle committed
279
			ax->rcount -= 2;
280 281 282 283 284 285

			/*
			 * dl9sau bugfix: the trailling two bytes flexnet crc
			 * will not be passed to the kernel. thus we have to
			 * correct the kissparm signature, because it indicates
			 * a crc but there's none
Ralf Baechle's avatar
Ralf Baechle committed
286
			 */
287
			*ax->rbuff &= ~0x20;
Linus Torvalds's avatar
Linus Torvalds committed
288
		}
Ralf Baechle's avatar
Ralf Baechle committed
289 290 291 292 293 294 295
 	}

	count = ax->rcount;

	if ((skb = dev_alloc_skb(count)) == NULL) {
		printk(KERN_ERR "mkiss: %s: memory squeeze, dropping packet.\n",
		       ax->dev->name);
296
		ax->dev->stats.rx_dropped++;
297
		spin_unlock_bh(&ax->buflock);
Ralf Baechle's avatar
Ralf Baechle committed
298
		return;
Linus Torvalds's avatar
Linus Torvalds committed
299 300
	}

Ralf Baechle's avatar
Ralf Baechle committed
301 302 303
	memcpy(skb_put(skb,count), ax->rbuff, count);
	skb->protocol = ax25_type_trans(skb, ax->dev);
	netif_rx(skb);
304 305
	ax->dev->stats.rx_packets++;
	ax->dev->stats.rx_bytes += count;
306
	spin_unlock_bh(&ax->buflock);
Linus Torvalds's avatar
Linus Torvalds committed
307 308
}

Ralf Baechle's avatar
Ralf Baechle committed
309
static void kiss_unesc(struct mkiss *ax, unsigned char s)
Linus Torvalds's avatar
Linus Torvalds committed
310
{
Ralf Baechle's avatar
Ralf Baechle committed
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
	switch (s) {
	case END:
		/* drop keeptest bit = VSV */
		if (test_bit(AXF_KEEPTEST, &ax->flags))
			clear_bit(AXF_KEEPTEST, &ax->flags);

		if (!test_and_clear_bit(AXF_ERROR, &ax->flags) && (ax->rcount > 2))
			ax_bump(ax);

		clear_bit(AXF_ESCAPE, &ax->flags);
		ax->rcount = 0;
		return;

	case ESC:
		set_bit(AXF_ESCAPE, &ax->flags);
		return;
	case ESC_ESC:
		if (test_and_clear_bit(AXF_ESCAPE, &ax->flags))
			s = ESC;
		break;
	case ESC_END:
		if (test_and_clear_bit(AXF_ESCAPE, &ax->flags))
			s = END;
		break;
	}

	spin_lock_bh(&ax->buflock);
	if (!test_bit(AXF_ERROR, &ax->flags)) {
		if (ax->rcount < ax->buffsize) {
			ax->rbuff[ax->rcount++] = s;
			spin_unlock_bh(&ax->buflock);
			return;
		}

345
		ax->dev->stats.rx_over_errors++;
Ralf Baechle's avatar
Ralf Baechle committed
346 347 348 349 350 351 352 353 354
		set_bit(AXF_ERROR, &ax->flags);
	}
	spin_unlock_bh(&ax->buflock);
}

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

Herbert Xu's avatar
Herbert Xu committed
355
	netif_tx_lock_bh(dev);
356
	netif_addr_lock(dev);
Ralf Baechle's avatar
Ralf Baechle committed
357
	memcpy(dev->dev_addr, &sa->sax25_call, AX25_ADDR_LEN);
358
	netif_addr_unlock(dev);
Herbert Xu's avatar
Herbert Xu committed
359
	netif_tx_unlock_bh(dev);
Ralf Baechle's avatar
Ralf Baechle committed
360 361

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
362 363
}

Ralf Baechle's avatar
Ralf Baechle committed
364 365 366
/*---------------------------------------------------------------------------*/

static void ax_changedmtu(struct mkiss *ax)
Linus Torvalds's avatar
Linus Torvalds committed
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
{
	struct net_device *dev = ax->dev;
	unsigned char *xbuff, *rbuff, *oxbuff, *orbuff;
	int len;

	len = dev->mtu * 2;

	/*
	 * allow for arrival of larger UDP packets, even if we say not to
	 * also fixes a bug in which SunOS sends 512-byte packets even with
	 * an MSS of 128
	 */
	if (len < 576 * 2)
		len = 576 * 2;

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

	if (xbuff == NULL || rbuff == NULL)  {
Ralf Baechle's avatar
Ralf Baechle committed
386 387
		printk(KERN_ERR "mkiss: %s: unable to grow ax25 buffers, "
		       "MTU change cancelled.\n",
Linus Torvalds's avatar
Linus Torvalds committed
388 389
		       ax->dev->name);
		dev->mtu = ax->mtu;
390 391
		kfree(xbuff);
		kfree(rbuff);
Linus Torvalds's avatar
Linus Torvalds committed
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
		return;
	}

	spin_lock_bh(&ax->buflock);

	oxbuff    = ax->xbuff;
	ax->xbuff = xbuff;
	orbuff    = ax->rbuff;
	ax->rbuff = rbuff;

	if (ax->xleft) {
		if (ax->xleft <= len) {
			memcpy(ax->xbuff, ax->xhead, ax->xleft);
		} else  {
			ax->xleft = 0;
407
			dev->stats.tx_dropped++;
Linus Torvalds's avatar
Linus Torvalds committed
408 409 410 411 412 413 414 415 416 417
		}
	}

	ax->xhead = ax->xbuff;

	if (ax->rcount) {
		if (ax->rcount <= len) {
			memcpy(ax->rbuff, orbuff, ax->rcount);
		} else  {
			ax->rcount = 0;
418
			dev->stats.rx_over_errors++;
Linus Torvalds's avatar
Linus Torvalds committed
419 420 421 422 423 424 425 426 427
			set_bit(AXF_ERROR, &ax->flags);
		}
	}

	ax->mtu      = dev->mtu + 73;
	ax->buffsize = len;

	spin_unlock_bh(&ax->buflock);

Ralf Baechle's avatar
Ralf Baechle committed
428 429
	kfree(oxbuff);
	kfree(orbuff);
Linus Torvalds's avatar
Linus Torvalds committed
430 431 432
}

/* Encapsulate one AX.25 packet and stuff into a TTY queue. */
Ralf Baechle's avatar
Ralf Baechle committed
433
static void ax_encaps(struct net_device *dev, unsigned char *icp, int len)
Linus Torvalds's avatar
Linus Torvalds committed
434
{
Ralf Baechle's avatar
Ralf Baechle committed
435
	struct mkiss *ax = netdev_priv(dev);
Linus Torvalds's avatar
Linus Torvalds committed
436 437 438 439 440 441 442 443 444
	unsigned char *p;
	int actual, count;

	if (ax->mtu != ax->dev->mtu + 73)	/* Someone has been ifconfigging */
		ax_changedmtu(ax);

	if (len > ax->mtu) {		/* Sigh, shouldn't occur BUT ... */
		len = ax->mtu;
		printk(KERN_ERR "mkiss: %s: truncating oversized transmit packet!\n", ax->dev->name);
445
		dev->stats.tx_dropped++;
Ralf Baechle's avatar
Ralf Baechle committed
446
		netif_start_queue(dev);
Linus Torvalds's avatar
Linus Torvalds committed
447 448 449 450 451 452
		return;
	}

	p = icp;

	spin_lock_bh(&ax->buflock);
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
	if ((*p & 0x0f) != 0) {
		/* Configuration Command (kissparms(1).
		 * Protocol spec says: never append CRC.
		 * This fixes a very old bug in the linux
		 * kiss driver. -- dl9sau */
		switch (*p & 0xff) {
		case 0x85:
			/* command from userspace especially for us,
			 * not for delivery to the tnc */
			if (len > 1) {
				int cmd = (p[1] & 0xff);
				switch(cmd) {
				case 3:
				  ax->crcmode = CRC_MODE_SMACK;
				  break;
				case 2:
				  ax->crcmode = CRC_MODE_FLEX;
				  break;
				case 1:
				  ax->crcmode = CRC_MODE_NONE;
				  break;
				case 0:
				default:
				  ax->crcmode = CRC_MODE_SMACK_TEST;
				  cmd = 0;
				}
				ax->crcauto = (cmd ? 0 : 1);
				printk(KERN_INFO "mkiss: %s: crc mode %s %d\n", ax->dev->name, (len) ? "set to" : "is", cmd);
			}
			spin_unlock_bh(&ax->buflock);
			netif_start_queue(dev);
Linus Torvalds's avatar
Linus Torvalds committed
484

485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
			return;
		default:
			count = kiss_esc(p, (unsigned char *)ax->xbuff, len);
		}
	} else {
		unsigned short crc;
		switch (ax->crcmode) {
		case CRC_MODE_SMACK_TEST:
			ax->crcmode  = CRC_MODE_FLEX_TEST;
			printk(KERN_INFO "mkiss: %s: Trying crc-smack\n", ax->dev->name);
			// fall through
		case CRC_MODE_SMACK:
			*p |= 0x80;
			crc = swab16(crc16(0, p, len));
			count = kiss_esc_crc(p, (unsigned char *)ax->xbuff, crc, len+2);
			break;
		case CRC_MODE_FLEX_TEST:
			ax->crcmode = CRC_MODE_NONE;
			printk(KERN_INFO "mkiss: %s: Trying crc-flexnet\n", ax->dev->name);
			// fall through
		case CRC_MODE_FLEX:
			*p |= 0x20;
			crc = calc_crc_flex(p, len);
			count = kiss_esc_crc(p, (unsigned char *)ax->xbuff, crc, len+2);
			break;

		default:
			count = kiss_esc(p, (unsigned char *)ax->xbuff, len);
		}
  	}
515
	spin_unlock_bh(&ax->buflock);
Linus Torvalds's avatar
Linus Torvalds committed
516

Ralf Baechle's avatar
Ralf Baechle committed
517
	set_bit(TTY_DO_WRITE_WAKEUP, &ax->tty->flags);
Alan Cox's avatar
Alan Cox committed
518
	actual = ax->tty->ops->write(ax->tty, ax->xbuff, count);
519 520
	dev->stats.tx_packets++;
	dev->stats.tx_bytes += actual;
Ralf Baechle's avatar
Ralf Baechle committed
521

Linus Torvalds's avatar
Linus Torvalds committed
522 523 524 525 526 527 528 529
	ax->dev->trans_start = jiffies;
	ax->xleft = count - actual;
	ax->xhead = ax->xbuff + actual;
}

/* Encapsulate an AX.25 packet and kick it into a TTY queue. */
static int ax_xmit(struct sk_buff *skb, struct net_device *dev)
{
Ralf Baechle's avatar
Ralf Baechle committed
530
	struct mkiss *ax = netdev_priv(dev);
Linus Torvalds's avatar
Linus Torvalds committed
531 532 533 534 535 536 537 538 539 540 541

	if (!netif_running(dev))  {
		printk(KERN_ERR "mkiss: %s: xmit call when iface is down\n", dev->name);
		return 1;
	}

	if (netif_queue_stopped(dev)) {
		/*
		 * May be we must check transmitter timeout here ?
		 *      14 Oct 1994 Dmitry Gorodchanin.
		 */
542
		if (time_before(jiffies, dev->trans_start + 20 * HZ)) {
Linus Torvalds's avatar
Linus Torvalds committed
543 544 545 546 547
			/* 20 sec timeout not reached */
			return 1;
		}

		printk(KERN_ERR "mkiss: %s: transmit timed out, %s?\n", dev->name,
548
		       (tty_chars_in_buffer(ax->tty) || ax->xleft) ?
Linus Torvalds's avatar
Linus Torvalds committed
549 550 551
		       "bad line quality" : "driver error");

		ax->xleft = 0;
Ralf Baechle's avatar
Ralf Baechle committed
552 553
		clear_bit(TTY_DO_WRITE_WAKEUP, &ax->tty->flags);
		netif_start_queue(dev);
Linus Torvalds's avatar
Linus Torvalds committed
554 555 556 557
	}

	/* We were not busy, so we are now... :-) */
	if (skb != NULL) {
Ralf Baechle's avatar
Ralf Baechle committed
558 559
		netif_stop_queue(dev);
		ax_encaps(dev, skb->data, skb->len);
Linus Torvalds's avatar
Linus Torvalds committed
560 561 562 563 564 565
		kfree_skb(skb);
	}

	return 0;
}

Ralf Baechle's avatar
Ralf Baechle committed
566 567 568 569 570 571 572 573 574 575
static int ax_open_dev(struct net_device *dev)
{
	struct mkiss *ax = netdev_priv(dev);

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

	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
576 577 578
#if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)

/* Return the frame type ID */
579 580 581
static int ax_header(struct sk_buff *skb, struct net_device *dev,
		     unsigned short type, const void *daddr,
		     const void *saddr, unsigned len)
Linus Torvalds's avatar
Linus Torvalds committed
582 583
{
#ifdef CONFIG_INET
584
	if (type != ETH_P_AX25)
585
		return ax25_hard_header(skb, dev, type, daddr, saddr, len);
Linus Torvalds's avatar
Linus Torvalds committed
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
#endif
	return 0;
}


static int ax_rebuild_header(struct sk_buff *skb)
{
#ifdef CONFIG_INET
	return ax25_rebuild_header(skb);
#else
	return 0;
#endif
}

#endif	/* CONFIG_{AX25,AX25_MODULE} */

/* Open the low-level part of the AX25 channel. Easy! */
static int ax_open(struct net_device *dev)
{
Ralf Baechle's avatar
Ralf Baechle committed
605
	struct mkiss *ax = netdev_priv(dev);
Linus Torvalds's avatar
Linus Torvalds committed
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
	unsigned long len;

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

	/*
	 * Allocate the frame buffers:
	 *
	 * rbuff	Receive buffer.
	 * xbuff	Transmit buffer.
	 */
	len = dev->mtu * 2;

	/*
	 * allow for arrival of larger UDP packets, even if we say not to
	 * also fixes a bug in which SunOS sends 512-byte packets even with
	 * an MSS of 128
	 */
	if (len < 576 * 2)
		len = 576 * 2;

	if ((ax->rbuff = kmalloc(len + 4, GFP_KERNEL)) == NULL)
		goto norbuff;

	if ((ax->xbuff = kmalloc(len + 4, GFP_KERNEL)) == NULL)
		goto noxbuff;

	ax->mtu	     = dev->mtu + 73;
	ax->buffsize = len;
	ax->rcount   = 0;
	ax->xleft    = 0;

	ax->flags   &= (1 << AXF_INUSE);      /* Clear ESCAPE & ERROR flags */

	spin_lock_init(&ax->buflock);

	return 0;

noxbuff:
	kfree(ax->rbuff);

norbuff:
	return -ENOMEM;
}


/* Close the low-level part of the AX25 channel. Easy! */
static int ax_close(struct net_device *dev)
{
Ralf Baechle's avatar
Ralf Baechle committed
655
	struct mkiss *ax = netdev_priv(dev);
Linus Torvalds's avatar
Linus Torvalds committed
656

Ralf Baechle's avatar
Ralf Baechle committed
657 658
	if (ax->tty)
		clear_bit(TTY_DO_WRITE_WAKEUP, &ax->tty->flags);
Linus Torvalds's avatar
Linus Torvalds committed
659 660 661 662 663 664

	netif_stop_queue(dev);

	return 0;
}

665 666 667 668 669
static const struct header_ops ax_header_ops = {
	.create    = ax_header,
	.rebuild   = ax_rebuild_header,
};

Ralf Baechle's avatar
Ralf Baechle committed
670 671 672 673 674 675 676 677 678 679 680 681
static void ax_setup(struct net_device *dev)
{
	/* Finish setting up the DEVICE info. */
	dev->mtu             = AX_MTU;
	dev->hard_start_xmit = ax_xmit;
	dev->open            = ax_open_dev;
	dev->stop            = ax_close;
	dev->set_mac_address = ax_set_mac_address;
	dev->hard_header_len = 0;
	dev->addr_len        = 0;
	dev->type            = ARPHRD_AX25;
	dev->tx_queue_len    = 10;
682 683
	dev->header_ops      = &ax_header_ops;

Ralf Baechle's avatar
Ralf Baechle committed
684

685 686
	memcpy(dev->broadcast, &ax25_bcast, AX25_ADDR_LEN);
	memcpy(dev->dev_addr,  &ax25_defaddr,  AX25_ADDR_LEN);
Ralf Baechle's avatar
Ralf Baechle committed
687 688

	dev->flags      = IFF_BROADCAST | IFF_MULTICAST;
Linus Torvalds's avatar
Linus Torvalds committed
689 690 691
}

/*
Ralf Baechle's avatar
Ralf Baechle committed
692 693 694 695 696 697
 * 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.
Linus Torvalds's avatar
Linus Torvalds committed
698
 */
699
static DEFINE_RWLOCK(disc_data_lock);
Ralf Baechle's avatar
Ralf Baechle committed
700 701

static struct mkiss *mkiss_get(struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
702
{
Ralf Baechle's avatar
Ralf Baechle committed
703
	struct mkiss *ax;
Linus Torvalds's avatar
Linus Torvalds committed
704

Ralf Baechle's avatar
Ralf Baechle committed
705 706 707 708 709
	read_lock(&disc_data_lock);
	ax = tty->disc_data;
	if (ax)
		atomic_inc(&ax->refcnt);
	read_unlock(&disc_data_lock);
Linus Torvalds's avatar
Linus Torvalds committed
710

Ralf Baechle's avatar
Ralf Baechle committed
711 712
	return ax;
}
Linus Torvalds's avatar
Linus Torvalds committed
713

Ralf Baechle's avatar
Ralf Baechle committed
714 715 716 717
static void mkiss_put(struct mkiss *ax)
{
	if (atomic_dec_and_test(&ax->refcnt))
		up(&ax->dead_sem);
Linus Torvalds's avatar
Linus Torvalds committed
718 719
}

720 721
static int crc_force = 0;	/* Can be overridden with insmod */

Ralf Baechle's avatar
Ralf Baechle committed
722
static int mkiss_open(struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
723
{
Ralf Baechle's avatar
Ralf Baechle committed
724 725
	struct net_device *dev;
	struct mkiss *ax;
Linus Torvalds's avatar
Linus Torvalds committed
726 727
	int err;

Ralf Baechle's avatar
Ralf Baechle committed
728 729
	if (!capable(CAP_NET_ADMIN))
		return -EPERM;
Alan Cox's avatar
Alan Cox committed
730 731
	if (tty->ops->write == NULL)
		return -EOPNOTSUPP;
Linus Torvalds's avatar
Linus Torvalds committed
732

Ralf Baechle's avatar
Ralf Baechle committed
733 734 735 736 737 738 739 740 741 742 743 744
	dev = alloc_netdev(sizeof(struct mkiss), "ax%d", ax_setup);
	if (!dev) {
		err = -ENOMEM;
		goto out;
	}

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

	spin_lock_init(&ax->buflock);
	atomic_set(&ax->refcnt, 1);
	init_MUTEX_LOCKED(&ax->dead_sem);
Linus Torvalds's avatar
Linus Torvalds committed
745 746 747

	ax->tty = tty;
	tty->disc_data = ax;
748
	tty->receive_room = 65535;
Linus Torvalds's avatar
Linus Torvalds committed
749

Alan Cox's avatar
Alan Cox committed
750
	tty_driver_flush_buffer(tty);
Linus Torvalds's avatar
Linus Torvalds committed
751 752

	/* Restore default settings */
Ralf Baechle's avatar
Ralf Baechle committed
753
	dev->type = ARPHRD_AX25;
Linus Torvalds's avatar
Linus Torvalds committed
754 755

	/* Perform the low-level AX25 initialization. */
Ralf Baechle's avatar
Ralf Baechle committed
756 757 758
	if ((err = ax_open(ax->dev))) {
		goto out_free_netdev;
	}
Linus Torvalds's avatar
Linus Torvalds committed
759

Ralf Baechle's avatar
Ralf Baechle committed
760 761
	if (register_netdev(dev))
		goto out_free_buffers;
Linus Torvalds's avatar
Linus Torvalds committed
762

763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
	/* after register_netdev() - because else printk smashes the kernel */
	switch (crc_force) {
	case 3:
		ax->crcmode  = CRC_MODE_SMACK;
		printk(KERN_INFO "mkiss: %s: crc mode smack forced.\n",
		       ax->dev->name);
		break;
	case 2:
		ax->crcmode  = CRC_MODE_FLEX;
		printk(KERN_INFO "mkiss: %s: crc mode flexnet forced.\n",
		       ax->dev->name);
		break;
	case 1:
		ax->crcmode  = CRC_MODE_NONE;
		printk(KERN_INFO "mkiss: %s: crc mode disabled.\n",
		       ax->dev->name);
		break;
	case 0:
		/* fall through */
	default:
		crc_force = 0;
		printk(KERN_INFO "mkiss: %s: crc mode is auto.\n",
		       ax->dev->name);
		ax->crcmode  = CRC_MODE_SMACK_TEST;
	}
	ax->crcauto = (crc_force ? 0 : 1);

Ralf Baechle's avatar
Ralf Baechle committed
790
	netif_start_queue(dev);
Linus Torvalds's avatar
Linus Torvalds committed
791

Ralf Baechle's avatar
Ralf Baechle committed
792 793
	/* Done.  We have linked the TTY line to a channel. */
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
794

Ralf Baechle's avatar
Ralf Baechle committed
795 796 797
out_free_buffers:
	kfree(ax->rbuff);
	kfree(ax->xbuff);
Linus Torvalds's avatar
Linus Torvalds committed
798

Ralf Baechle's avatar
Ralf Baechle committed
799 800
out_free_netdev:
	free_netdev(dev);
Linus Torvalds's avatar
Linus Torvalds committed
801

Ralf Baechle's avatar
Ralf Baechle committed
802 803
out:
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
804 805
}

Ralf Baechle's avatar
Ralf Baechle committed
806
static void mkiss_close(struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
807
{
Ralf Baechle's avatar
Ralf Baechle committed
808
	struct mkiss *ax;
Linus Torvalds's avatar
Linus Torvalds committed
809

Ralf Baechle's avatar
Ralf Baechle committed
810 811 812 813
	write_lock(&disc_data_lock);
	ax = tty->disc_data;
	tty->disc_data = NULL;
	write_unlock(&disc_data_lock);
Linus Torvalds's avatar
Linus Torvalds committed
814

Al Viro's avatar
Al Viro committed
815
	if (!ax)
Ralf Baechle's avatar
Ralf Baechle committed
816
		return;
Linus Torvalds's avatar
Linus Torvalds committed
817 818

	/*
Ralf Baechle's avatar
Ralf Baechle committed
819 820
	 * We have now ensured that nobody can start using ap from now on, but
	 * we have to wait for all existing users to finish.
Linus Torvalds's avatar
Linus Torvalds committed
821
	 */
Ralf Baechle's avatar
Ralf Baechle committed
822 823
	if (!atomic_dec_and_test(&ax->refcnt))
		down(&ax->dead_sem);
Linus Torvalds's avatar
Linus Torvalds committed
824

Ralf Baechle's avatar
Ralf Baechle committed
825
	unregister_netdev(ax->dev);
Linus Torvalds's avatar
Linus Torvalds committed
826

Ralf Baechle's avatar
Ralf Baechle committed
827 828 829
	/* Free all AX25 frame buffers. */
	kfree(ax->rbuff);
	kfree(ax->xbuff);
Linus Torvalds's avatar
Linus Torvalds committed
830

Ralf Baechle's avatar
Ralf Baechle committed
831
	ax->tty = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
832 833
}

Ralf Baechle's avatar
Ralf Baechle committed
834 835 836
/* Perform I/O control on an active ax25 channel. */
static int mkiss_ioctl(struct tty_struct *tty, struct file *file,
	unsigned int cmd, unsigned long arg)
Linus Torvalds's avatar
Linus Torvalds committed
837
{
Ralf Baechle's avatar
Ralf Baechle committed
838
	struct mkiss *ax = mkiss_get(tty);
839
	struct net_device *dev;
Ralf Baechle's avatar
Ralf Baechle committed
840
	unsigned int tmp, err;
Linus Torvalds's avatar
Linus Torvalds committed
841

Ralf Baechle's avatar
Ralf Baechle committed
842 843 844
	/* First make sure we're connected. */
	if (ax == NULL)
		return -ENXIO;
845
	dev = ax->dev;
Linus Torvalds's avatar
Linus Torvalds committed
846

Ralf Baechle's avatar
Ralf Baechle committed
847 848 849 850 851 852 853 854 855 856 857 858 859 860
	switch (cmd) {
 	case SIOCGIFNAME:
		err = copy_to_user((void __user *) arg, ax->dev->name,
		                   strlen(ax->dev->name) + 1) ? -EFAULT : 0;
		break;

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

	case SIOCSIFENCAP:
		if (get_user(tmp, (int __user *) arg)) {
			err = -EFAULT;
			break;
Linus Torvalds's avatar
Linus Torvalds committed
861 862
		}

Ralf Baechle's avatar
Ralf Baechle committed
863 864 865 866 867
		ax->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;
Linus Torvalds's avatar
Linus Torvalds committed
868

Ralf Baechle's avatar
Ralf Baechle committed
869 870
		err = 0;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
871

Ralf Baechle's avatar
Ralf Baechle committed
872 873
	case SIOCSIFHWADDR: {
		char addr[AX25_ADDR_LEN];
Linus Torvalds's avatar
Linus Torvalds committed
874

Ralf Baechle's avatar
Ralf Baechle committed
875 876 877
		if (copy_from_user(&addr,
		                   (void __user *) arg, AX25_ADDR_LEN)) {
			err = -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
878 879 880
			break;
		}

Herbert Xu's avatar
Herbert Xu committed
881
		netif_tx_lock_bh(dev);
Ralf Baechle's avatar
Ralf Baechle committed
882
		memcpy(dev->dev_addr, addr, AX25_ADDR_LEN);
Herbert Xu's avatar
Herbert Xu committed
883
		netif_tx_unlock_bh(dev);
Ralf Baechle's avatar
Ralf Baechle committed
884 885 886 887 888 889

		err = 0;
		break;
	}
	default:
		err = -ENOIOCTLCMD;
Linus Torvalds's avatar
Linus Torvalds committed
890 891
	}

Ralf Baechle's avatar
Ralf Baechle committed
892
	mkiss_put(ax);
Linus Torvalds's avatar
Linus Torvalds committed
893

Ralf Baechle's avatar
Ralf Baechle committed
894
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
895 896
}

Ralf Baechle's avatar
Ralf Baechle committed
897 898 899 900 901 902 903 904
/*
 * Handle the 'receiver data ready' interrupt.
 * This function is called by the 'tty_io' module in the kernel when
 * a block of data has been received, which can now be decapsulated
 * and sent on to the AX.25 layer for further processing.
 */
static void mkiss_receive_buf(struct tty_struct *tty, const unsigned char *cp,
	char *fp, int count)
Linus Torvalds's avatar
Linus Torvalds committed
905
{
Ralf Baechle's avatar
Ralf Baechle committed
906
	struct mkiss *ax = mkiss_get(tty);
Linus Torvalds's avatar
Linus Torvalds committed
907

Ralf Baechle's avatar
Ralf Baechle committed
908 909
	if (!ax)
		return;
Linus Torvalds's avatar
Linus Torvalds committed
910

Ralf Baechle's avatar
Ralf Baechle committed
911 912 913 914 915 916
	/*
	 * Argh! mtu change time! - costs us the packet part received
	 * at the change
	 */
	if (ax->mtu != ax->dev->mtu + 73)
		ax_changedmtu(ax);
Linus Torvalds's avatar
Linus Torvalds committed
917

Ralf Baechle's avatar
Ralf Baechle committed
918 919 920 921
	/* Read the characters out of the buffer */
	while (count--) {
		if (fp != NULL && *fp++) {
			if (!test_and_set_bit(AXF_ERROR, &ax->flags))
922
				ax->dev->stats.rx_errors++;
Ralf Baechle's avatar
Ralf Baechle committed
923 924 925
			cp++;
			continue;
		}
Linus Torvalds's avatar
Linus Torvalds committed
926

Ralf Baechle's avatar
Ralf Baechle committed
927
		kiss_unesc(ax, *cp++);
Linus Torvalds's avatar
Linus Torvalds committed
928
	}
Ralf Baechle's avatar
Ralf Baechle committed
929 930

	mkiss_put(ax);
931
	tty_unthrottle(tty);
Linus Torvalds's avatar
Linus Torvalds committed
932 933
}

Ralf Baechle's avatar
Ralf Baechle committed
934 935 936 937 938
/*
 * Called by the driver when there's room for more data.  If we have
 * more packets to send, we send them here.
 */
static void mkiss_write_wakeup(struct tty_struct *tty)
Linus Torvalds's avatar
Linus Torvalds committed
939
{
Ralf Baechle's avatar
Ralf Baechle committed
940 941
	struct mkiss *ax = mkiss_get(tty);
	int actual;
Linus Torvalds's avatar
Linus Torvalds committed
942

Ralf Baechle's avatar
Ralf Baechle committed
943 944
	if (!ax)
		return;
Linus Torvalds's avatar
Linus Torvalds committed
945

Ralf Baechle's avatar
Ralf Baechle committed
946 947 948 949 950
	if (ax->xleft <= 0)  {
		/* Now serial buffer is almost free & we can start
		 * transmission of another packet
		 */
		clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Linus Torvalds's avatar
Linus Torvalds committed
951

Ralf Baechle's avatar
Ralf Baechle committed
952 953 954
		netif_wake_queue(ax->dev);
		goto out;
	}
Linus Torvalds's avatar
Linus Torvalds committed
955

Alan Cox's avatar
Alan Cox committed
956
	actual = tty->ops->write(tty, ax->xhead, ax->xleft);
Ralf Baechle's avatar
Ralf Baechle committed
957 958
	ax->xleft -= actual;
	ax->xhead += actual;
Linus Torvalds's avatar
Linus Torvalds committed
959

Ralf Baechle's avatar
Ralf Baechle committed
960 961
out:
	mkiss_put(ax);
Linus Torvalds's avatar
Linus Torvalds committed
962 963
}

Alan Cox's avatar
Alan Cox committed
964
static struct tty_ldisc_ops ax_ldisc = {
965
	.owner		= THIS_MODULE,
Ralf Baechle's avatar
Ralf Baechle committed
966 967 968 969 970 971 972 973
	.magic		= TTY_LDISC_MAGIC,
	.name		= "mkiss",
	.open		= mkiss_open,
	.close		= mkiss_close,
	.ioctl		= mkiss_ioctl,
	.receive_buf	= mkiss_receive_buf,
	.write_wakeup	= mkiss_write_wakeup
};
Linus Torvalds's avatar
Linus Torvalds committed
974

Ralf Baechle's avatar
Ralf Baechle committed
975 976 977 978
static char banner[] __initdata = KERN_INFO \
	"mkiss: AX.25 Multikiss, Hans Albas PE1AYX\n";
static char msg_regfail[] __initdata = KERN_ERR \
	"mkiss: can't register line discipline (err = %d)\n";
Linus Torvalds's avatar
Linus Torvalds committed
979 980 981 982 983 984 985

static int __init mkiss_init_driver(void)
{
	int status;

	printk(banner);

Ralf Baechle's avatar
Ralf Baechle committed
986 987
	if ((status = tty_register_ldisc(N_AX25, &ax_ldisc)) != 0)
		printk(msg_regfail);
Linus Torvalds's avatar
Linus Torvalds committed
988 989 990 991

	return status;
}

Ralf Baechle's avatar
Ralf Baechle committed
992 993 994
static const char msg_unregfail[] __exitdata = KERN_ERR \
	"mkiss: can't unregister line discipline (err = %d)\n";

Linus Torvalds's avatar
Linus Torvalds committed
995 996
static void __exit mkiss_exit_driver(void)
{
Ralf Baechle's avatar
Ralf Baechle committed
997
	int ret;
Linus Torvalds's avatar
Linus Torvalds committed
998

Ralf Baechle's avatar
Ralf Baechle committed
999 1000
	if ((ret = tty_unregister_ldisc(N_AX25)))
		printk(msg_unregfail, ret);
Linus Torvalds's avatar
Linus Torvalds committed
1001 1002
}

Ralf Baechle's avatar
Ralf Baechle committed
1003
MODULE_AUTHOR("Ralf Baechle DL5RB <ralf@linux-mips.org>");
Linus Torvalds's avatar
Linus Torvalds committed
1004
MODULE_DESCRIPTION("KISS driver for AX.25 over TTYs");
Rusty Russell's avatar
Rusty Russell committed
1005
module_param(crc_force, int, 0);
1006
MODULE_PARM_DESC(crc_force, "crc [0 = auto | 1 = none | 2 = flexnet | 3 = smack]");
Linus Torvalds's avatar
Linus Torvalds committed
1007 1008
MODULE_LICENSE("GPL");
MODULE_ALIAS_LDISC(N_AX25);
Ralf Baechle's avatar
Ralf Baechle committed
1009

Linus Torvalds's avatar
Linus Torvalds committed
1010 1011
module_init(mkiss_init_driver);
module_exit(mkiss_exit_driver);