File: /usr/src/linux/drivers/net/hamradio/mkiss.c

1     /*
2      *	MKISS Driver
3      *
4      *	This module:
5      *		This module is free software; you can redistribute it and/or
6      *		modify it under the terms of the GNU General Public License
7      *		as published by the Free Software Foundation; either version
8      *		2 of the License, or (at your option) any later version.
9      *
10      * 		This module implements the AX.25 protocol for kernel-based
11      *		devices like TTYs. It interfaces between a raw TTY, and the
12      *		kernel's AX.25 protocol layers, just like slip.c.
13      *		AX.25 needs to be separated from slip.c while slip.c is no
14      *		longer a static kernel device since it is a module.
15      *		This method clears the way to implement other kiss protocols
16      *		like mkiss smack g8bpq ..... so far only mkiss is implemented.
17      *
18      * Hans Alblas <hans@esrac.ele.tue.nl>
19      *
20      *	History
21      *	Jonathan (G4KLX)	Fixed to match Linux networking changes - 2.1.15.
22      *	Matthias (DG2FEF)       Added support for FlexNet CRC (on special request)
23      *                              Fixed bug in ax25_close(): dev_lock_wait() was
24      *                              called twice, causing a deadlock.
25      */
26     
27     #include <linux/config.h>
28     #include <linux/module.h>
29     #include <asm/system.h>
30     #include <asm/segment.h>
31     #include <asm/bitops.h>
32     #include <asm/uaccess.h>
33     #include <linux/string.h>
34     #include <linux/mm.h>
35     #include <linux/interrupt.h>
36     #include <linux/in.h>
37     #include <linux/inet.h>
38     #include <linux/tty.h>
39     #include <linux/errno.h>
40     #include <linux/netdevice.h>
41     #include <linux/major.h>
42     #include <linux/init.h>
43     #include <linux/rtnetlink.h>
44     #include <linux/etherdevice.h>
45     #include <linux/skbuff.h>
46     #include <linux/if_arp.h>
47     
48     #include <net/ax25.h>
49     
50     #include "mkiss.h"
51     
52     #ifdef CONFIG_INET
53     #include <linux/ip.h>
54     #include <linux/tcp.h>
55     #endif
56     
57     static char banner[] __initdata = KERN_INFO "mkiss: AX.25 Multikiss, Hans Albas PE1AYX\n";
58     
59     #define NR_MKISS 4
60     #define MKISS_SERIAL_TYPE_NORMAL 1
61     
62     struct mkiss_channel {
63     	int magic;		/* magic word */
64     	int init;		/* channel exists? */
65     	struct tty_struct *tty; /* link to tty control structure */
66     };
67     
68     typedef struct ax25_ctrl {
69     	struct ax_disp ctrl;	/* 				*/
70     	struct net_device  dev;	/* the device			*/
71     } ax25_ctrl_t;
72     
73     static ax25_ctrl_t **ax25_ctrls;
74     
75     int ax25_maxdev = AX25_MAXDEV;		/* Can be overridden with insmod! */
76     
77     static struct tty_ldisc	ax_ldisc;
78     
79     static int ax25_init(struct net_device *);
80     static int kiss_esc(unsigned char *, unsigned char *, int);
81     static int kiss_esc_crc(unsigned char *, unsigned char *, unsigned short, int);
82     static void kiss_unesc(struct ax_disp *, unsigned char);
83     
84     /*---------------------------------------------------------------------------*/
85     
86     static const unsigned short Crc_flex_table[] = {
87       0x0f87, 0x1e0e, 0x2c95, 0x3d1c, 0x49a3, 0x582a, 0x6ab1, 0x7b38,
88       0x83cf, 0x9246, 0xa0dd, 0xb154, 0xc5eb, 0xd462, 0xe6f9, 0xf770,
89       0x1f06, 0x0e8f, 0x3c14, 0x2d9d, 0x5922, 0x48ab, 0x7a30, 0x6bb9,
90       0x934e, 0x82c7, 0xb05c, 0xa1d5, 0xd56a, 0xc4e3, 0xf678, 0xe7f1,
91       0x2e85, 0x3f0c, 0x0d97, 0x1c1e, 0x68a1, 0x7928, 0x4bb3, 0x5a3a,
92       0xa2cd, 0xb344, 0x81df, 0x9056, 0xe4e9, 0xf560, 0xc7fb, 0xd672,
93       0x3e04, 0x2f8d, 0x1d16, 0x0c9f, 0x7820, 0x69a9, 0x5b32, 0x4abb,
94       0xb24c, 0xa3c5, 0x915e, 0x80d7, 0xf468, 0xe5e1, 0xd77a, 0xc6f3,
95       0x4d83, 0x5c0a, 0x6e91, 0x7f18, 0x0ba7, 0x1a2e, 0x28b5, 0x393c,
96       0xc1cb, 0xd042, 0xe2d9, 0xf350, 0x87ef, 0x9666, 0xa4fd, 0xb574,
97       0x5d02, 0x4c8b, 0x7e10, 0x6f99, 0x1b26, 0x0aaf, 0x3834, 0x29bd,
98       0xd14a, 0xc0c3, 0xf258, 0xe3d1, 0x976e, 0x86e7, 0xb47c, 0xa5f5,
99       0x6c81, 0x7d08, 0x4f93, 0x5e1a, 0x2aa5, 0x3b2c, 0x09b7, 0x183e,
100       0xe0c9, 0xf140, 0xc3db, 0xd252, 0xa6ed, 0xb764, 0x85ff, 0x9476,
101       0x7c00, 0x6d89, 0x5f12, 0x4e9b, 0x3a24, 0x2bad, 0x1936, 0x08bf,
102       0xf048, 0xe1c1, 0xd35a, 0xc2d3, 0xb66c, 0xa7e5, 0x957e, 0x84f7,
103       0x8b8f, 0x9a06, 0xa89d, 0xb914, 0xcdab, 0xdc22, 0xeeb9, 0xff30,
104       0x07c7, 0x164e, 0x24d5, 0x355c, 0x41e3, 0x506a, 0x62f1, 0x7378,
105       0x9b0e, 0x8a87, 0xb81c, 0xa995, 0xdd2a, 0xcca3, 0xfe38, 0xefb1,
106       0x1746, 0x06cf, 0x3454, 0x25dd, 0x5162, 0x40eb, 0x7270, 0x63f9,
107       0xaa8d, 0xbb04, 0x899f, 0x9816, 0xeca9, 0xfd20, 0xcfbb, 0xde32,
108       0x26c5, 0x374c, 0x05d7, 0x145e, 0x60e1, 0x7168, 0x43f3, 0x527a,
109       0xba0c, 0xab85, 0x991e, 0x8897, 0xfc28, 0xeda1, 0xdf3a, 0xceb3,
110       0x3644, 0x27cd, 0x1556, 0x04df, 0x7060, 0x61e9, 0x5372, 0x42fb,
111       0xc98b, 0xd802, 0xea99, 0xfb10, 0x8faf, 0x9e26, 0xacbd, 0xbd34,
112       0x45c3, 0x544a, 0x66d1, 0x7758, 0x03e7, 0x126e, 0x20f5, 0x317c,
113       0xd90a, 0xc883, 0xfa18, 0xeb91, 0x9f2e, 0x8ea7, 0xbc3c, 0xadb5,
114       0x5542, 0x44cb, 0x7650, 0x67d9, 0x1366, 0x02ef, 0x3074, 0x21fd,
115       0xe889, 0xf900, 0xcb9b, 0xda12, 0xaead, 0xbf24, 0x8dbf, 0x9c36,
116       0x64c1, 0x7548, 0x47d3, 0x565a, 0x22e5, 0x336c, 0x01f7, 0x107e,
117       0xf808, 0xe981, 0xdb1a, 0xca93, 0xbe2c, 0xafa5, 0x9d3e, 0x8cb7,
118       0x7440, 0x65c9, 0x5752, 0x46db, 0x3264, 0x23ed, 0x1176, 0x00ff
119     };
120     
121     /*---------------------------------------------------------------------------*/
122     
123     static unsigned short calc_crc_flex(unsigned char *cp, int size)
124     {
125         unsigned short crc = 0xffff;
126         
127         while (size--)
128     	crc = (crc << 8) ^ Crc_flex_table[((crc >> 8) ^ *cp++) & 0xff];
129     
130         return crc;
131     }
132     
133     /*---------------------------------------------------------------------------*/
134     
135     static int check_crc_flex(unsigned char *cp, int size)
136     {
137       unsigned short crc = 0xffff;
138     
139       if (size < 3)
140           return -1;
141     
142       while (size--)
143           crc = (crc << 8) ^ Crc_flex_table[((crc >> 8) ^ *cp++) & 0xff];
144     
145       if ((crc & 0xffff) != 0x7070) 
146           return -1;
147     
148       return 0;
149     }
150     
151     /*---------------------------------------------------------------------------*/
152     
153     /* Find a free channel, and link in this `tty' line. */
154     static inline struct ax_disp *ax_alloc(void)
155     {
156     	ax25_ctrl_t *axp=NULL;
157     	int i;
158     
159     	for (i = 0; i < ax25_maxdev; i++) {
160     		axp = ax25_ctrls[i];
161     
162     		/* Not allocated ? */
163     		if (axp == NULL)
164     			break;
165     
166     		/* Not in use ? */
167     		if (!test_and_set_bit(AXF_INUSE, &axp->ctrl.flags))
168     			break;
169     	}
170     
171     	/* Sorry, too many, all slots in use */
172     	if (i >= ax25_maxdev)
173     		return NULL;
174     
175     	/* If no channels are available, allocate one */
176     	if (axp == NULL && (ax25_ctrls[i] = kmalloc(sizeof(ax25_ctrl_t), GFP_KERNEL)) != NULL) {
177     		axp = ax25_ctrls[i];
178     		memset(axp, 0, sizeof(ax25_ctrl_t));
179     
180     		/* Initialize channel control data */
181     		set_bit(AXF_INUSE, &axp->ctrl.flags);
182     		sprintf(axp->dev.name, "ax%d", i++);
183     		axp->ctrl.tty      = NULL;
184     		axp->dev.base_addr = i;
185     		axp->dev.priv      = (void *)&axp->ctrl;
186     		axp->dev.next      = NULL;
187     		axp->dev.init      = ax25_init;
188     	}
189     
190     	if (axp != NULL) {
191     		/*
192     		 * register device so that it can be ifconfig'ed
193     		 * ax25_init() will be called as a side-effect
194     		 * SIDE-EFFECT WARNING: ax25_init() CLEARS axp->ctrl !
195     		 */
196     		if (register_netdev(&axp->dev) == 0) {
197     			/* (Re-)Set the INUSE bit.   Very Important! */
198     			set_bit(AXF_INUSE, &axp->ctrl.flags);
199     			axp->ctrl.dev = &axp->dev;
200     			axp->dev.priv = (void *) &axp->ctrl;
201     
202     			return &axp->ctrl;
203     		} else {
204     			clear_bit(AXF_INUSE,&axp->ctrl.flags);
205     			printk(KERN_ERR "mkiss: ax_alloc() - register_netdev() failure.\n");
206     		}
207     	}
208     
209     	return NULL;
210     }
211     
212     /* Free an AX25 channel. */
213     static inline void ax_free(struct ax_disp *ax)
214     {
215     	/* Free all AX25 frame buffers. */
216     	if (ax->rbuff)
217     		kfree(ax->rbuff);
218     	ax->rbuff = NULL;
219     	if (ax->xbuff)
220     		kfree(ax->xbuff);
221     	ax->xbuff = NULL;
222     	if (!test_and_clear_bit(AXF_INUSE, &ax->flags))
223     		printk(KERN_ERR "mkiss: %s: ax_free for already free unit.\n", ax->dev->name);
224     }
225     
226     static void ax_changedmtu(struct ax_disp *ax)
227     {
228     	struct net_device *dev = ax->dev;
229     	unsigned char *xbuff, *rbuff, *oxbuff, *orbuff;
230     	int len;
231     	unsigned long flags;
232     
233     	len = dev->mtu * 2;
234     
235     	/*
236     	 * allow for arrival of larger UDP packets, even if we say not to
237     	 * also fixes a bug in which SunOS sends 512-byte packets even with
238     	 * an MSS of 128
239     	 */
240     	if (len < 576 * 2)
241     		len = 576 * 2;
242     
243     	xbuff = kmalloc(len + 4, GFP_ATOMIC);
244     	rbuff = kmalloc(len + 4, GFP_ATOMIC);
245     
246     	if (xbuff == NULL || rbuff == NULL)  {
247     		printk(KERN_ERR "mkiss: %s: unable to grow ax25 buffers, MTU change cancelled.\n",
248     		       ax->dev->name);
249     		dev->mtu = ax->mtu;
250     		if (xbuff != NULL)
251     			kfree(xbuff);
252     		if (rbuff != NULL)
253     			kfree(rbuff);
254     		return;
255     	}
256     
257     	save_flags(flags);
258     	cli();
259     
260     	oxbuff    = ax->xbuff;
261     	ax->xbuff = xbuff;
262     	orbuff    = ax->rbuff;
263     	ax->rbuff = rbuff;
264     
265     	if (ax->xleft) {
266     		if (ax->xleft <= len) {
267     			memcpy(ax->xbuff, ax->xhead, ax->xleft);
268     		} else  {
269     			ax->xleft = 0;
270     			ax->tx_dropped++;
271     		}
272     	}
273     
274     	ax->xhead = ax->xbuff;
275     
276     	if (ax->rcount) {
277     		if (ax->rcount <= len) {
278     			memcpy(ax->rbuff, orbuff, ax->rcount);
279     		} else  {
280     			ax->rcount = 0;
281     			ax->rx_over_errors++;
282     			set_bit(AXF_ERROR, &ax->flags);
283     		}
284     	}
285     
286     	ax->mtu      = dev->mtu + 73;
287     	ax->buffsize = len;
288     
289     	restore_flags(flags);
290     
291     	if (oxbuff != NULL)
292     		kfree(oxbuff);
293     	if (orbuff != NULL)
294     		kfree(orbuff);
295     }
296     
297     
298     /* Set the "sending" flag.  This must be atomic. */
299     static inline void ax_lock(struct ax_disp *ax)
300     {
301     	netif_stop_queue(ax->dev);
302     }
303     
304     
305     /* Clear the "sending" flag.  This must be atomic. */
306     static inline void ax_unlock(struct ax_disp *ax)
307     {
308     	netif_start_queue(ax->dev);
309     }
310     
311     /* Send one completely decapsulated AX.25 packet to the AX.25 layer. */
312     static void ax_bump(struct ax_disp *ax)
313     {
314     	struct ax_disp *tmp_ax;
315     	struct sk_buff *skb;
316     	struct mkiss_channel *mkiss;
317     	int count;
318     
319             tmp_ax = ax;
320     
321     	if (ax->rbuff[0] > 0x0f) {
322     		if (ax->mkiss != NULL) {
323     			mkiss= ax->mkiss->tty->driver_data;
324     			if (mkiss->magic == MKISS_DRIVER_MAGIC)
325     				tmp_ax = ax->mkiss;
326     		} else if (ax->rbuff[0] & 0x20) {
327     		        ax->crcmode = CRC_MODE_FLEX;
328     			if (check_crc_flex(ax->rbuff, ax->rcount) < 0) {
329     			        ax->rx_errors++;
330     				return;
331     			}
332     			ax->rcount -= 2;
333     		}
334      	}
335     
336     	count = ax->rcount;
337     
338     	if ((skb = dev_alloc_skb(count)) == NULL) {
339     		printk(KERN_ERR "mkiss: %s: memory squeeze, dropping packet.\n", ax->dev->name);
340     		ax->rx_dropped++;
341     		return;
342     	}
343     
344     	skb->dev      = tmp_ax->dev;
345     	memcpy(skb_put(skb,count), ax->rbuff, count);
346     	skb->mac.raw  = skb->data;
347     	skb->protocol = htons(ETH_P_AX25);
348     	netif_rx(skb);
349     	tmp_ax->rx_packets++;
350     }
351     
352     /* Encapsulate one AX.25 packet and stuff into a TTY queue. */
353     static void ax_encaps(struct ax_disp *ax, unsigned char *icp, int len)
354     {
355     	unsigned char *p;
356     	int actual, count;
357     	struct mkiss_channel *mkiss = ax->tty->driver_data;
358     
359     	if (ax->mtu != ax->dev->mtu + 73)	/* Someone has been ifconfigging */
360     		ax_changedmtu(ax);
361     
362     	if (len > ax->mtu) {		/* Sigh, shouldn't occur BUT ... */
363     		len = ax->mtu;
364     		printk(KERN_ERR "mkiss: %s: truncating oversized transmit packet!\n", ax->dev->name);
365     		ax->tx_dropped++;
366     		ax_unlock(ax);
367     		return;
368     	}
369     
370     	p = icp;
371     
372     	if (mkiss->magic  != MKISS_DRIVER_MAGIC) {
373     	        switch (ax->crcmode) {
374     		         unsigned short crc;
375     
376     		case CRC_MODE_FLEX:
377     		         *p |= 0x20;
378     		         crc = calc_crc_flex(p, len);
379     			 count = kiss_esc_crc(p, (unsigned char *)ax->xbuff, crc, len+2);
380     			 break;
381     
382     		default:
383     		         count = kiss_esc(p, (unsigned char *)ax->xbuff, len);
384     			 break;
385     		}
386     		ax->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
387     		actual = ax->tty->driver.write(ax->tty, 0, ax->xbuff, count);
388     		ax->tx_packets++;
389     		ax->dev->trans_start = jiffies;
390     		ax->xleft = count - actual;
391     		ax->xhead = ax->xbuff + actual;
392     	} else {
393     		count = kiss_esc(p, (unsigned char *) ax->mkiss->xbuff, len);
394     		ax->mkiss->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
395     		actual = ax->mkiss->tty->driver.write(ax->mkiss->tty, 0, ax->mkiss->xbuff, count);
396     		ax->tx_packets++;
397     		ax->mkiss->dev->trans_start = jiffies;
398     		ax->mkiss->xleft = count - actual;
399     		ax->mkiss->xhead = ax->mkiss->xbuff + actual;
400     	}
401     }
402     
403     /*
404      * Called by the driver when there's room for more data.  If we have
405      * more packets to send, we send them here.
406      */
407     static void ax25_write_wakeup(struct tty_struct *tty)
408     {
409     	int actual;
410     	struct ax_disp *ax = (struct ax_disp *) tty->disc_data;
411     	struct mkiss_channel *mkiss;
412     
413     	/* First make sure we're connected. */
414     	if (ax == NULL || ax->magic != AX25_MAGIC || !netif_running(ax->dev))
415     		return;
416     	if (ax->xleft <= 0)  {
417     		/* Now serial buffer is almost free & we can start
418     		 * transmission of another packet
419     		 */
420     		tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
421     
422     		if (ax->mkiss != NULL) {
423     			mkiss= ax->mkiss->tty->driver_data;
424     			if (mkiss->magic  == MKISS_DRIVER_MAGIC)
425     				ax_unlock(ax->mkiss);
426     	        }
427     
428     		netif_wake_queue(ax->dev);
429     		return;
430     	}
431     
432     	actual = tty->driver.write(tty, 0, ax->xhead, ax->xleft);
433     	ax->xleft -= actual;
434     	ax->xhead += actual;
435     }
436     
437     /* Encapsulate an AX.25 packet and kick it into a TTY queue. */
438     static int ax_xmit(struct sk_buff *skb, struct net_device *dev)
439     {
440     	struct ax_disp *ax = (struct ax_disp *) dev->priv;
441     	struct mkiss_channel *mkiss = ax->tty->driver_data;
442     	struct ax_disp *tmp_ax;
443     
444     	tmp_ax = NULL;
445     
446     	if (mkiss->magic  == MKISS_DRIVER_MAGIC) {
447     		if (skb->data[0] < 0x10)
448     			skb->data[0] = skb->data[0] + 0x10;
449     		tmp_ax = ax->mkiss;
450     	}
451     
452     	if (!netif_running(dev))  {
453     		printk(KERN_ERR "mkiss: %s: xmit call when iface is down\n", dev->name);
454     		return 1;
455     	}
456     
457     	if (tmp_ax != NULL)
458     		if (netif_queue_stopped(tmp_ax->dev))
459     			return 1;
460     
461     	if (tmp_ax != NULL)
462     		if (netif_queue_stopped(dev)) {
463     			printk(KERN_ERR "mkiss: dev busy while serial dev is free\n");
464     			ax_unlock(ax);
465     	        }
466     
467     	if (netif_queue_stopped(dev)) {
468     		/*
469     		 * May be we must check transmitter timeout here ?
470     		 *      14 Oct 1994 Dmitry Gorodchanin.
471     		 */
472     		if (jiffies - dev->trans_start  < 20 * HZ) {
473     			/* 20 sec timeout not reached */
474     			return 1;
475     		}
476     
477     		printk(KERN_ERR "mkiss: %s: transmit timed out, %s?\n", dev->name,
478     		       (ax->tty->driver.chars_in_buffer(ax->tty) || ax->xleft) ?
479     		       "bad line quality" : "driver error");
480     
481     		ax->xleft = 0;
482     		ax->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
483     		ax_unlock(ax);
484     	}
485     
486     	/* We were not busy, so we are now... :-) */
487     	if (skb != NULL) {
488     		ax_lock(ax);
489     		if (tmp_ax != NULL)
490     			ax_lock(tmp_ax);
491     		ax_encaps(ax, skb->data, skb->len);
492     		kfree_skb(skb);
493     	}
494     
495     	return 0;
496     }
497     
498     #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
499     
500     /* Return the frame type ID */
501     static int ax_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
502     	  void *daddr, void *saddr, unsigned len)
503     {
504     #ifdef CONFIG_INET
505     	if (type != htons(ETH_P_AX25))
506     		return ax25_encapsulate(skb, dev, type, daddr, saddr, len);
507     #endif
508     	return 0;
509     }
510     
511     
512     static int ax_rebuild_header(struct sk_buff *skb)
513     {
514     #ifdef CONFIG_INET
515     	return ax25_rebuild_header(skb);
516     #else
517     	return 0;
518     #endif
519     }
520     
521     #endif	/* CONFIG_{AX25,AX25_MODULE} */
522     
523     /* Open the low-level part of the AX25 channel. Easy! */
524     static int ax_open(struct net_device *dev)
525     {
526     	struct ax_disp *ax = (struct ax_disp *) dev->priv;
527     	unsigned long len;
528     
529     	if (ax->tty == NULL)
530     		return -ENODEV;
531     
532     	/*
533     	 * Allocate the frame buffers:
534     	 *
535     	 * rbuff	Receive buffer.
536     	 * xbuff	Transmit buffer.
537     	 */
538     	len = dev->mtu * 2;
539     
540     	/*
541     	 * allow for arrival of larger UDP packets, even if we say not to
542     	 * also fixes a bug in which SunOS sends 512-byte packets even with
543     	 * an MSS of 128
544     	 */
545     	if (len < 576 * 2)
546     		len = 576 * 2;
547     
548     	if ((ax->rbuff = kmalloc(len + 4, GFP_KERNEL)) == NULL)
549     		goto norbuff;
550     
551     	if ((ax->xbuff = kmalloc(len + 4, GFP_KERNEL)) == NULL)
552     		goto noxbuff;
553     
554     	ax->mtu	     = dev->mtu + 73;
555     	ax->buffsize = len;
556     	ax->rcount   = 0;
557     	ax->xleft    = 0;
558     
559     	ax->flags   &= (1 << AXF_INUSE);      /* Clear ESCAPE & ERROR flags */
560     
561     	netif_start_queue(dev);
562     	return 0;
563     
564     noxbuff:
565     	kfree(ax->rbuff);
566     
567     norbuff:
568     	return -ENOMEM;
569     }
570     
571     
572     /* Close the low-level part of the AX25 channel. Easy! */
573     static int ax_close(struct net_device *dev)
574     {
575     	struct ax_disp *ax = (struct ax_disp *) dev->priv;
576     
577     	if (ax->tty == NULL)
578     		return -EBUSY;
579     
580     	ax->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
581     
582     	netif_stop_queue(dev);
583     
584     	return 0;
585     }
586     
587     static int ax25_receive_room(struct tty_struct *tty)
588     {
589     	return 65536;  /* We can handle an infinite amount of data. :-) */
590     }
591     
592     /*
593      * Handle the 'receiver data ready' interrupt.
594      * This function is called by the 'tty_io' module in the kernel when
595      * a block of data has been received, which can now be decapsulated
596      * and sent on to the AX.25 layer for further processing.
597      */
598     static void ax25_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
599     {
600     	struct ax_disp *ax = (struct ax_disp *) tty->disc_data;
601     
602     	if (ax == NULL || ax->magic != AX25_MAGIC || !netif_running(ax->dev))
603     		return;
604     
605     	/*
606     	 * Argh! mtu change time! - costs us the packet part received
607     	 * at the change
608     	 */
609     	if (ax->mtu != ax->dev->mtu + 73)
610     		ax_changedmtu(ax);
611     
612     	/* Read the characters out of the buffer */
613     	while (count--) {
614     		if (fp != NULL && *fp++) {
615     			if (!test_and_set_bit(AXF_ERROR, &ax->flags))
616     				ax->rx_errors++;
617     			cp++;
618     			continue;
619     		}
620     
621     		kiss_unesc(ax, *cp++);
622     	}
623     }
624     
625     static int ax25_open(struct tty_struct *tty)
626     {
627     	struct ax_disp *ax = (struct ax_disp *) tty->disc_data;
628     	struct ax_disp *tmp_ax;
629     	struct mkiss_channel *mkiss;
630     	int err, cnt;
631     
632     	/* First make sure we're not already connected. */
633     	if (ax && ax->magic == AX25_MAGIC)
634     		return -EEXIST;
635     
636     	/* OK.  Find a free AX25 channel to use. */
637     	if ((ax = ax_alloc()) == NULL)
638     		return -ENFILE;
639     
640     	ax->tty = tty;
641     	tty->disc_data = ax;
642     
643     	ax->mkiss = NULL;
644     	tmp_ax    = NULL;
645     
646     	if (tty->driver.flush_buffer)
647     		tty->driver.flush_buffer(tty);
648     	if (tty->ldisc.flush_buffer)
649     		tty->ldisc.flush_buffer(tty);
650     
651     	/* Restore default settings */
652     	ax->dev->type = ARPHRD_AX25;
653     
654     	/* Perform the low-level AX25 initialization. */
655     	if ((err = ax_open(ax->dev)))
656     		return err;
657     
658     	mkiss = ax->tty->driver_data;
659     
660     	if (mkiss->magic  == MKISS_DRIVER_MAGIC) {
661     		for (cnt = 1; cnt < ax25_maxdev; cnt++) {
662     			if (ax25_ctrls[cnt]) {
663     				if (netif_running(&ax25_ctrls[cnt]->dev)) {
664     					if (ax == &ax25_ctrls[cnt]->ctrl) {
665     						cnt--;
666     						tmp_ax = &ax25_ctrls[cnt]->ctrl;
667     						break;
668     					}
669     				}
670     			}
671     		}
672     	}
673     
674     	if (tmp_ax != NULL) {
675     		ax->mkiss     = tmp_ax;
676     		tmp_ax->mkiss = ax;
677     	}
678     
679     	MOD_INC_USE_COUNT;
680     
681     	/* Done.  We have linked the TTY line to a channel. */
682     	return ax->dev->base_addr;
683     }
684     
685     static void ax25_close(struct tty_struct *tty)
686     {
687     	struct ax_disp *ax = (struct ax_disp *) tty->disc_data;
688     
689     	/* First make sure we're connected. */
690     	if (ax == NULL || ax->magic != AX25_MAGIC)
691     		return;
692     
693     	unregister_netdev(ax->dev);
694     
695     	tty->disc_data = 0;
696     	ax->tty        = NULL;
697     
698     	ax_free(ax);
699     	MOD_DEC_USE_COUNT;
700     }
701     
702     
703     static struct net_device_stats *ax_get_stats(struct net_device *dev)
704     {
705     	static struct net_device_stats stats;
706     	struct ax_disp *ax = (struct ax_disp *) dev->priv;
707     
708     	memset(&stats, 0, sizeof(struct net_device_stats));
709     
710     	stats.rx_packets     = ax->rx_packets;
711     	stats.tx_packets     = ax->tx_packets;
712     	stats.rx_dropped     = ax->rx_dropped;
713     	stats.tx_dropped     = ax->tx_dropped;
714     	stats.tx_errors      = ax->tx_errors;
715     	stats.rx_errors      = ax->rx_errors;
716     	stats.rx_over_errors = ax->rx_over_errors;
717     
718     	return &stats;
719     }
720     
721     
722     /************************************************************************
723      *			   STANDARD ENCAPSULATION	        	 *
724      ************************************************************************/
725     
726     static int kiss_esc(unsigned char *s, unsigned char *d, int len)
727     {
728     	unsigned char *ptr = d;
729     	unsigned char c;
730     
731     	/*
732     	 * Send an initial END character to flush out any
733     	 * data that may have accumulated in the receiver
734     	 * due to line noise.
735     	 */
736     
737     	*ptr++ = END;
738     
739     	while (len-- > 0) {
740     		switch (c = *s++) {
741     			case END:
742     				*ptr++ = ESC;
743     				*ptr++ = ESC_END;
744     				break;
745     			case ESC:
746     				*ptr++ = ESC;
747     				*ptr++ = ESC_ESC;
748     				break;
749     			default:
750     				*ptr++ = c;
751     				break;
752     		}
753     	}
754     
755     	*ptr++ = END;
756     
757     	return ptr - d;
758     }
759     
760     /*
761      * MW:
762      * OK its ugly, but tell me a better solution without copying the
763      * packet to a temporary buffer :-)
764      */
765     static int kiss_esc_crc(unsigned char *s, unsigned char *d, unsigned short crc, int len)
766     {
767     	unsigned char *ptr = d;
768     	unsigned char c=0;
769     
770     	*ptr++ = END;
771     	while (len > 0) {
772     		if (len > 2) 
773     			c = *s++;
774     		else if (len > 1)
775     			c = crc >> 8;
776     		else if (len > 0)
777     			c = crc & 0xff;
778     
779     		len--;
780     
781     		switch (c) {
782                             case END:
783                                     *ptr++ = ESC;
784                                     *ptr++ = ESC_END;
785                                     break;
786                             case ESC:
787                                     *ptr++ = ESC;
788                                     *ptr++ = ESC_ESC;
789                                     break;
790                             default:
791                                     *ptr++ = c;
792                                     break;
793     		}
794     	}
795     	*ptr++ = END;
796     	return ptr - d;		
797     }
798     
799     static void kiss_unesc(struct ax_disp *ax, unsigned char s)
800     {
801     	switch (s) {
802     		case END:
803     			/* drop keeptest bit = VSV */
804     			if (test_bit(AXF_KEEPTEST, &ax->flags))
805     				clear_bit(AXF_KEEPTEST, &ax->flags);
806     
807     			if (!test_and_clear_bit(AXF_ERROR, &ax->flags) && (ax->rcount > 2))
808     				ax_bump(ax);
809     
810     			clear_bit(AXF_ESCAPE, &ax->flags);
811     			ax->rcount = 0;
812     			return;
813     
814     		case ESC:
815     			set_bit(AXF_ESCAPE, &ax->flags);
816     			return;
817     		case ESC_ESC:
818     			if (test_and_clear_bit(AXF_ESCAPE, &ax->flags))
819     				s = ESC;
820     			break;
821     		case ESC_END:
822     			if (test_and_clear_bit(AXF_ESCAPE, &ax->flags))
823     				s = END;
824     			break;
825     	}
826     
827     	if (!test_bit(AXF_ERROR, &ax->flags)) {
828     		if (ax->rcount < ax->buffsize) {
829     			ax->rbuff[ax->rcount++] = s;
830     			return;
831     		}
832     
833     		ax->rx_over_errors++;
834     		set_bit(AXF_ERROR, &ax->flags);
835     	}
836     }
837     
838     
839     static int ax_set_mac_address(struct net_device *dev, void *addr)
840     {
841     	if (copy_from_user(dev->dev_addr, addr, AX25_ADDR_LEN))
842     		return -EFAULT;
843     	return 0;
844     }
845     
846     static int ax_set_dev_mac_address(struct net_device *dev, void *addr)
847     {
848     	struct sockaddr *sa = addr;
849     
850     	memcpy(dev->dev_addr, sa->sa_data, AX25_ADDR_LEN);
851     
852     	return 0;
853     }
854     
855     
856     /* Perform I/O control on an active ax25 channel. */
857     static int ax25_disp_ioctl(struct tty_struct *tty, void *file, int cmd, void *arg)
858     {
859     	struct ax_disp *ax = (struct ax_disp *) tty->disc_data;
860     	unsigned int tmp;
861     
862     	/* First make sure we're connected. */
863     	if (ax == NULL || ax->magic != AX25_MAGIC)
864     		return -EINVAL;
865     
866     	switch (cmd) {
867     	 	case SIOCGIFNAME:
868     			if (copy_to_user(arg, ax->dev->name, strlen(ax->dev->name) + 1))
869     				return -EFAULT;
870     			return 0;
871     
872     		case SIOCGIFENCAP:
873     			return put_user(4, (int *)arg);
874     
875     		case SIOCSIFENCAP:
876     			if (get_user(tmp, (int *)arg))
877     				return -EFAULT;
878     			ax->mode = tmp;
879     			ax->dev->addr_len        = AX25_ADDR_LEN;	  /* sizeof an AX.25 addr */
880     			ax->dev->hard_header_len = AX25_KISS_HEADER_LEN + AX25_MAX_HEADER_LEN + 3;
881     			ax->dev->type            = ARPHRD_AX25;
882     			return 0;
883     
884     		 case SIOCSIFHWADDR:
885     			return ax_set_mac_address(ax->dev, arg);
886     
887     		default:
888     			return -ENOIOCTLCMD;
889     	}
890     }
891     
892     static int ax_open_dev(struct net_device *dev)
893     {
894     	struct ax_disp *ax = (struct ax_disp *) dev->priv;
895     
896     	if (ax->tty == NULL)
897     		return -ENODEV;
898     
899     	return 0;
900     }
901     
902     
903     /* Initialize the driver.  Called by network startup. */
904     static int ax25_init(struct net_device *dev)
905     {
906     	struct ax_disp *ax = (struct ax_disp *) dev->priv;
907     
908     	static char ax25_bcast[AX25_ADDR_LEN] =
909     		{'Q'<<1,'S'<<1,'T'<<1,' '<<1,' '<<1,' '<<1,'0'<<1};
910     	static char ax25_test[AX25_ADDR_LEN] =
911     		{'L'<<1,'I'<<1,'N'<<1,'U'<<1,'X'<<1,' '<<1,'1'<<1};
912     
913     	if (ax == NULL)		/* Allocation failed ?? */
914     		return -ENODEV;
915     
916     	/* Set up the "AX25 Control Block". (And clear statistics) */
917     	memset(ax, 0, sizeof (struct ax_disp));
918     	ax->magic  = AX25_MAGIC;
919     	ax->dev	   = dev;
920     
921     	/* Finish setting up the DEVICE info. */
922     	dev->mtu             = AX_MTU;
923     	dev->hard_start_xmit = ax_xmit;
924     	dev->open            = ax_open_dev;
925     	dev->stop            = ax_close;
926     	dev->get_stats	     = ax_get_stats;
927     	dev->set_mac_address = ax_set_dev_mac_address;
928     	dev->hard_header_len = 0;
929     	dev->addr_len        = 0;
930     	dev->type            = ARPHRD_AX25;
931     	dev->tx_queue_len    = 10;
932     	dev->hard_header     = ax_header;
933     	dev->rebuild_header  = ax_rebuild_header;
934     
935     	memcpy(dev->broadcast, ax25_bcast, AX25_ADDR_LEN);
936     	memcpy(dev->dev_addr,  ax25_test,  AX25_ADDR_LEN);
937     
938     	/* New-style flags. */
939     	dev->flags      = 0;
940     
941     	return 0;
942     }
943     
944     
945     /* ******************************************************************** */
946     /* *			Init MKISS driver			      * */
947     /* ******************************************************************** */
948     
949     static int __init mkiss_init_driver(void)
950     {
951     	int status;
952     
953     	printk(banner);
954     
955     	if (ax25_maxdev < 4)
956     	  ax25_maxdev = 4; /* Sanity */
957     
958     	if ((ax25_ctrls = kmalloc(sizeof(void *) * ax25_maxdev, GFP_KERNEL)) == NULL) {
959     		printk(KERN_ERR "mkiss: Can't allocate ax25_ctrls[] array!\n");
960     		return -ENOMEM;
961     	}
962     
963     	/* Clear the pointer array, we allocate devices when we need them */
964     	memset(ax25_ctrls, 0, sizeof(void*) * ax25_maxdev); /* Pointers */
965     
966     	/* Fill in our line protocol discipline, and register it */
967     	ax_ldisc.magic		= TTY_LDISC_MAGIC;
968     	ax_ldisc.name		= "mkiss";
969     	ax_ldisc.open		= ax25_open;
970     	ax_ldisc.close		= ax25_close;
971     	ax_ldisc.ioctl		= (int (*)(struct tty_struct *, struct file *,
972     					unsigned int, unsigned long))ax25_disp_ioctl;
973     	ax_ldisc.receive_buf	= ax25_receive_buf;
974     	ax_ldisc.receive_room	= ax25_receive_room;
975     	ax_ldisc.write_wakeup	= ax25_write_wakeup;
976     
977     	if ((status = tty_register_ldisc(N_AX25, &ax_ldisc)) != 0) {
978     		printk(KERN_ERR "mkiss: can't register line discipline (err = %d)\n", status);
979     		kfree(ax25_ctrls);
980     	}
981     	return status;
982     }
983     
984     static void __exit mkiss_exit_driver(void)
985     {
986     	int i;
987     
988     	for (i = 0; i < ax25_maxdev; i++) {
989     		if (ax25_ctrls[i]) {
990     			/*
991     			* VSV = if dev->start==0, then device
992     			* unregistered while close proc.
993     			*/
994     			if (netif_running(&ax25_ctrls[i]->dev))
995     				unregister_netdev(&ax25_ctrls[i]->dev);
996     			kfree(ax25_ctrls[i]);
997     		}
998     	}
999     
1000     	kfree(ax25_ctrls);
1001     	ax25_ctrls = NULL;
1002     
1003     	if ((i = tty_register_ldisc(N_AX25, NULL)))
1004     		printk(KERN_ERR "mkiss: can't unregister line discipline (err = %d)\n", i);
1005     }
1006     
1007     MODULE_AUTHOR("Hans Albas PE1AYX <hans@esrac.ele.tue.nl>");
1008     MODULE_DESCRIPTION("KISS driver for AX.25 over TTYs");
1009     MODULE_PARM(ax25_maxdev, "i");
1010     MODULE_PARM_DESC(ax25_maxdev, "number of MKISS devices");
1011     
1012     module_init(mkiss_init_driver);
1013     module_exit(mkiss_exit_driver);
1014     
1015