File: /usr/src/linux/drivers/net/hamradio/6pack.c

1     /*
2      * 6pack.c	This module implements the 6pack protocol for kernel-based
3      *		devices like TTY. It interfaces between a raw TTY and the
4      *		kernel's AX.25 protocol layers.
5      *
6      * Version:	@(#)6pack.c	0.3.0	04/07/98
7      *
8      * Authors:	Andreas Könsgen <ajk@iehk.rwth-aachen.de>
9      *
10      * Quite a lot of stuff "stolen" by Jörg Reuter from slip.c, written by
11      *
12      *		Laurence Culhane, <loz@holmes.demon.co.uk>
13      *		Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
14      *
15      */
16     
17     #include <linux/config.h>
18     #include <linux/module.h>
19     #include <asm/system.h>
20     #include <asm/uaccess.h>
21     #include <asm/bitops.h>
22     #include <linux/string.h>
23     #include <linux/mm.h>
24     #include <linux/interrupt.h>
25     #include <linux/in.h>
26     #include <linux/tty.h>
27     #include <linux/errno.h>
28     #include <linux/netdevice.h>
29     #include <linux/timer.h>
30     #include <net/ax25.h>
31     #include <linux/etherdevice.h>
32     #include <linux/skbuff.h>
33     #include <linux/rtnetlink.h>
34     #include <linux/if_arp.h>
35     #include <linux/init.h>
36     #include <linux/ip.h>
37     #include <linux/tcp.h>
38     
39     #define SIXPACK_VERSION    "Revision: 0.3.0"
40     
41     /* sixpack priority commands */
42     #define SIXP_SEOF	0x40	/* start and end of a 6pack frame */
43     #define SIXP_TX_URUN	0x48	/* transmit overrun */
44     #define SIXP_RX_ORUN	0x50	/* receive overrun */
45     #define SIXP_RX_BUF_OVL	0x58	/* receive buffer overflow */
46     
47     #define SIXP_CHKSUM	0xFF	/* valid checksum of a 6pack frame */
48     
49     /* masks to get certain bits out of the status bytes sent by the TNC */
50     
51     #define SIXP_CMD_MASK		0xC0
52     #define SIXP_CHN_MASK		0x07
53     #define SIXP_PRIO_CMD_MASK	0x80
54     #define SIXP_STD_CMD_MASK	0x40
55     #define SIXP_PRIO_DATA_MASK	0x38
56     #define SIXP_TX_MASK		0x20
57     #define SIXP_RX_MASK		0x10
58     #define SIXP_RX_DCD_MASK	0x18
59     #define SIXP_LEDS_ON		0x78
60     #define SIXP_LEDS_OFF		0x60
61     #define SIXP_CON		0x08
62     #define SIXP_STA		0x10
63     
64     #define SIXP_FOUND_TNC		0xe9
65     #define SIXP_CON_ON		0x68
66     #define SIXP_DCD_MASK		0x08
67     #define SIXP_DAMA_OFF		0
68     
69     /* default level 2 parameters */
70     #define SIXP_TXDELAY			25	/* in 10 ms */
71     #define SIXP_PERSIST			50	/* in 256ths */
72     #define SIXP_SLOTTIME			10	/* in 10 ms */
73     #define SIXP_INIT_RESYNC_TIMEOUT	150	/* in 10 ms */
74     #define SIXP_RESYNC_TIMEOUT		500	/* in 10 ms */
75     
76     /* 6pack configuration. */
77     #define SIXP_NRUNIT			31      /* MAX number of 6pack channels */
78     #define SIXP_MTU			256	/* Default MTU */
79     
80     enum sixpack_flags {
81     	SIXPF_INUSE,	/* Channel in use	*/
82     	SIXPF_ERROR,	/* Parity, etc. error	*/
83     };
84     
85     struct sixpack {
86     	int			magic;
87     
88     	/* Various fields. */
89     	struct tty_struct	*tty;		/* ptr to TTY structure		*/
90     	struct net_device	*dev;		/* easy for intr handling	*/
91     
92     	/* These are pointers to the malloc()ed frame buffers. */
93     	unsigned char		*rbuff;		/* receiver buffer		*/
94     	int			rcount;         /* received chars counter       */
95     	unsigned char		*xbuff;		/* transmitter buffer		*/
96     	unsigned char		*xhead;         /* pointer to next byte to XMIT */
97     	int			xleft;          /* bytes left in XMIT queue     */
98     
99     	unsigned char		raw_buf[4];
100     	unsigned char		cooked_buf[400];
101     
102     	unsigned int		rx_count;
103     	unsigned int		rx_count_cooked;
104     
105     	/* 6pack interface statistics. */
106     	struct net_device_stats stats;
107     
108     	int			mtu;		/* Our mtu (to spot changes!)   */
109     	int			buffsize;       /* Max buffers sizes            */
110     
111     	unsigned long		flags;		/* Flag values/ mode etc	*/
112     	unsigned char		mode;		/* 6pack mode			*/
113     
114     	/* 6pack stuff */
115     	unsigned char		tx_delay;
116     	unsigned char		persistance;
117     	unsigned char		slottime;
118     	unsigned char		duplex;
119     	unsigned char		led_state;
120     	unsigned char		status;
121     	unsigned char		status1;
122     	unsigned char		status2;
123     	unsigned char		tx_enable;
124     	unsigned char		tnc_ok;
125     
126     	struct timer_list	tx_t;
127     	struct timer_list	resync_t;
128     };
129     
130     #define AX25_6PACK_HEADER_LEN 0
131     #define SIXPACK_MAGIC 0x5304
132     
133     typedef struct sixpack_ctrl {
134     	struct sixpack	ctrl;			/* 6pack things			*/
135     	struct net_device	dev;		/* the device			*/
136     } sixpack_ctrl_t;
137     static sixpack_ctrl_t **sixpack_ctrls;
138     
139     int sixpack_maxdev = SIXP_NRUNIT;	/* Can be overridden with insmod! */
140     MODULE_PARM(sixpack_maxdev, "i");
141     MODULE_PARM_DESC(sixpack_maxdev, "number of 6PACK devices");
142     
143     static void sp_start_tx_timer(struct sixpack *);
144     static void sp_xmit_on_air(unsigned long);
145     static void resync_tnc(unsigned long);
146     static void sixpack_decode(struct sixpack *, unsigned char[], int);
147     static int encode_sixpack(unsigned char *, unsigned char *, int, unsigned char);
148     static int sixpack_init(struct net_device *dev);
149     
150     static void decode_prio_command(unsigned char, struct sixpack *);
151     static void decode_std_command(unsigned char, struct sixpack *);
152     static void decode_data(unsigned char, struct sixpack *);
153     
154     static int tnc_init(struct sixpack *);
155     
156     /* Find a free 6pack channel, and link in this `tty' line. */
157     static inline struct sixpack *sp_alloc(void)
158     {
159     	sixpack_ctrl_t *spp = NULL;
160     	int i;
161     
162     	for (i = 0; i < sixpack_maxdev; i++) {
163     		spp = sixpack_ctrls[i];
164     
165     		if (spp == NULL)
166     			break;
167     
168     		if (!test_and_set_bit(SIXPF_INUSE, &spp->ctrl.flags))
169     			break;
170     	}
171     
172     	/* Too many devices... */
173     	if (i >= sixpack_maxdev)
174     		return NULL;
175     
176     	/* If no channels are available, allocate one */
177     	if (!spp &&
178     	    (sixpack_ctrls[i] = (sixpack_ctrl_t *)kmalloc(sizeof(sixpack_ctrl_t),
179     						    GFP_KERNEL)) != NULL) {
180     		spp = sixpack_ctrls[i];
181     		memset(spp, 0, sizeof(sixpack_ctrl_t));
182     
183     		/* Initialize channel control data */
184     		set_bit(SIXPF_INUSE, &spp->ctrl.flags);
185     		spp->ctrl.tty         = NULL;
186     		sprintf(spp->dev.name, "sp%d", i);
187     		spp->dev.base_addr    = i;
188     		spp->dev.priv         = (void *) &spp->ctrl;
189     		spp->dev.next         = NULL;
190     		spp->dev.init         = sixpack_init;
191     	}
192     
193     	if (spp != NULL) {
194     		/* register device so that it can be ifconfig'ed       */
195     		/* sixpack_init() will be called as a side-effect         */
196     		/* SIDE-EFFECT WARNING: sixpack_init() CLEARS spp->ctrl ! */
197     
198     		if (register_netdev(&spp->dev) == 0) {
199     			set_bit(SIXPF_INUSE, &spp->ctrl.flags);
200     			spp->ctrl.dev = &spp->dev;
201     			spp->dev.priv = (void *) &spp->ctrl;
202     			SET_MODULE_OWNER(&spp->dev);
203     			return &spp->ctrl;
204     		} else {
205     			clear_bit(SIXPF_INUSE, &spp->ctrl.flags);
206     			printk(KERN_WARNING "sp_alloc() - register_netdev() failure.\n");
207     		}
208     	}
209     
210     	return NULL;
211     }
212     
213     
214     /* Free a 6pack channel. */
215     static inline void sp_free(struct sixpack *sp)
216     {
217     	/* Free all 6pack frame buffers. */
218     	if (sp->rbuff)
219     		kfree(sp->rbuff);
220     	sp->rbuff = NULL;
221     	if (sp->xbuff)
222     		kfree(sp->xbuff);
223     	sp->xbuff = NULL;
224     
225     	if (!test_and_clear_bit(SIXPF_INUSE, &sp->flags))
226     		printk(KERN_WARNING "%s: sp_free for already free unit.\n", sp->dev->name);
227     }
228     
229     
230     /* Send one completely decapsulated IP datagram to the IP layer. */
231     
232     /* This is the routine that sends the received data to the kernel AX.25.
233        'cmd' is the KISS command. For AX.25 data, it is zero. */
234     
235     static void sp_bump(struct sixpack *sp, char cmd)
236     {
237     	struct sk_buff *skb;
238     	int count;
239     	unsigned char *ptr;
240     
241     	count = sp->rcount+1;
242     
243     	sp->stats.rx_bytes += count;
244     
245     	if ((skb = dev_alloc_skb(count)) == NULL) {
246     		printk(KERN_DEBUG "%s: memory squeeze, dropping packet.\n", sp->dev->name);
247     		sp->stats.rx_dropped++;
248     		return;
249     	}
250     
251     	skb->dev = sp->dev;
252     	ptr = skb_put(skb, count);
253     	*ptr++ = cmd;	/* KISS command */
254     
255     	memcpy(ptr, (sp->cooked_buf)+1, count);
256     	skb->mac.raw = skb->data;
257     	skb->protocol = htons(ETH_P_AX25);
258     	netif_rx(skb);
259     	sp->stats.rx_packets++;
260     }
261     
262     
263     /* ----------------------------------------------------------------------- */
264     
265     /* Encapsulate one AX.25 frame and stuff into a TTY queue. */
266     static void sp_encaps(struct sixpack *sp, unsigned char *icp, int len)
267     {
268     	unsigned char *p;
269     	int actual, count;
270     
271     	if (len > sp->mtu) {	/* sp->mtu = AX25_MTU = max. PACLEN = 256 */
272     		printk(KERN_DEBUG "%s: truncating oversized transmit packet!\n", sp->dev->name);
273     		sp->stats.tx_dropped++;
274     		netif_start_queue(sp->dev);
275     		return;
276     	}
277     
278     	p = icp;
279     
280     	if (p[0] > 5) {
281     		printk(KERN_DEBUG "%s: invalid KISS command -- dropped\n", sp->dev->name);
282     		netif_start_queue(sp->dev);
283     		return;
284     	}
285     
286     	if ((p[0] != 0) && (len > 2)) {
287     		printk(KERN_DEBUG "%s: KISS control packet too long -- dropped\n", sp->dev->name);
288     		netif_start_queue(sp->dev);
289     		return;
290     	}
291     
292     	if ((p[0] == 0) && (len < 15)) {
293     		printk(KERN_DEBUG "%s: bad AX.25 packet to transmit -- dropped\n", sp->dev->name);
294     		netif_start_queue(sp->dev);
295     		sp->stats.tx_dropped++;
296     		return;
297     	}
298     
299     	count = encode_sixpack(p, (unsigned char *) sp->xbuff, len, sp->tx_delay);
300     	sp->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
301     
302     	switch (p[0]) {
303     		case 1:	sp->tx_delay = p[1];		return;
304     		case 2:	sp->persistance = p[1];		return;
305     		case 3: sp->slottime = p[1];		return;
306     		case 4: /* ignored */			return;
307     		case 5: sp->duplex = p[1];		return;
308     	}
309     
310     	if (p[0] == 0) {
311     		/* in case of fullduplex or DAMA operation, we don't take care
312     		   about the state of the DCD or of any timers, as the determination
313     		   of the correct time to send is the job of the AX.25 layer. We send
314     		   immediately after data has arrived. */
315     		if (sp->duplex == 1) {
316     			sp->led_state = 0x70;
317     			sp->tty->driver.write(sp->tty, 0, &sp->led_state, 1);
318     			sp->tx_enable = 1;
319     			actual = sp->tty->driver.write(sp->tty, 0, sp->xbuff, count);
320     			sp->xleft = count - actual;
321     			sp->xhead = sp->xbuff + actual;
322     			sp->led_state = 0x60;
323     			sp->tty->driver.write(sp->tty, 0, &sp->led_state, 1);
324     		} else {
325     			sp->xleft = count;
326     			sp->xhead = sp->xbuff;
327     			sp->status2 = count;
328     			if (sp->duplex == 0)
329     				sp_start_tx_timer(sp);
330     		}
331     	}
332     }
333     
334     /*
335      * Called by the TTY driver when there's room for more data.  If we have
336      * more packets to send, we send them here.
337      */
338     static void sixpack_write_wakeup(struct tty_struct *tty)
339     {
340     	int actual;
341     	struct sixpack *sp = (struct sixpack *) tty->disc_data;
342     
343     	/* First make sure we're connected. */
344     	if (!sp || sp->magic != SIXPACK_MAGIC ||
345     	    !netif_running(sp->dev))
346     		return;
347     
348     	if (sp->xleft <= 0)  {
349     		/* Now serial buffer is almost free & we can start
350     		 * transmission of another packet */
351     		sp->stats.tx_packets++;
352     		tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
353     		sp->tx_enable = 0;
354     		netif_wake_queue(sp->dev);
355     		return;
356     	}
357     
358     	if (sp->tx_enable == 1) {
359     		actual = tty->driver.write(tty, 0, sp->xhead, sp->xleft);
360     		sp->xleft -= actual;
361     		sp->xhead += actual;
362     	}
363     }
364     
365     /* ----------------------------------------------------------------------- */
366     
367     /* Encapsulate an IP datagram and kick it into a TTY queue. */
368     
369     static int sp_xmit(struct sk_buff *skb, struct net_device *dev)
370     {
371     	struct sixpack *sp = (struct sixpack *) dev->priv;
372     
373     	/* We were not busy, so we are now... :-) */
374     	netif_stop_queue(dev);
375     	sp->stats.tx_bytes += skb->len;
376     	sp_encaps(sp, skb->data, skb->len);
377     	dev_kfree_skb(skb);
378     	return 0;
379     }
380     
381     
382     /* perform the persistence/slottime algorithm for CSMA access. If the persistence
383        check was successful, write the data to the serial driver. Note that in case
384        of DAMA operation, the data is not sent here. */
385     
386     static void sp_xmit_on_air(unsigned long channel)
387     {
388     	struct sixpack *sp = (struct sixpack *) channel;
389     	int actual;
390     	static unsigned char random;
391     
392     	random = random * 17 + 41;
393     
394     	if (((sp->status1 & SIXP_DCD_MASK) == 0) && (random < sp->persistance)) {
395     		sp->led_state = 0x70;
396     		sp->tty->driver.write(sp->tty, 0, &sp->led_state, 1);
397     		sp->tx_enable = 1;
398     		actual = sp->tty->driver.write(sp->tty, 0, sp->xbuff, sp->status2);
399     		sp->xleft -= actual;
400     		sp->xhead += actual;
401     		sp->led_state = 0x60;
402     		sp->tty->driver.write(sp->tty, 0, &sp->led_state, 1);
403     		sp->status2 = 0;
404     	} else
405     		sp_start_tx_timer(sp);
406     }
407     
408     
409     /* Return the frame type ID */
410     static int sp_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
411     	  void *daddr, void *saddr, unsigned len)
412     {
413     #ifdef CONFIG_INET
414     	if (type != htons(ETH_P_AX25))
415     		return ax25_encapsulate(skb, dev, type, daddr, saddr, len);
416     #endif
417     	return 0;
418     }
419     
420     
421     static int sp_rebuild_header(struct sk_buff *skb)
422     {
423     #ifdef CONFIG_INET
424     	return ax25_rebuild_header(skb);
425     #else
426     	return 0;
427     #endif
428     }
429     
430     
431     /* Open the low-level part of the 6pack channel. */
432     static int sp_open(struct net_device *dev)
433     {
434     	struct sixpack *sp = (struct sixpack *) dev->priv;
435     	unsigned long len;
436     
437     	if (sp->tty == NULL)
438     		return -ENODEV;
439     
440     	/*
441     	 * Allocate the 6pack frame buffers:
442     	 *
443     	 * rbuff	Receive buffer.
444     	 * xbuff	Transmit buffer.
445     	 */
446     
447     	/* !!! length of the buffers. MTU is IP MTU, not PACLEN!
448     	 */
449     
450     	len = dev->mtu * 2;
451     
452     	if ((sp->rbuff = kmalloc(len + 4, GFP_KERNEL)) == NULL)
453     		return -ENOMEM;
454     
455     	if ((sp->xbuff = kmalloc(len + 4, GFP_KERNEL)) == NULL) {
456     		kfree(sp->rbuff);
457     		return -ENOMEM;
458     	}
459     
460     	sp->mtu	     = AX25_MTU + 73;
461     	sp->buffsize = len;
462     	sp->rcount   = 0;
463     	sp->rx_count = 0;
464     	sp->rx_count_cooked = 0;
465     	sp->xleft    = 0;
466     
467     	sp->flags   &= (1 << SIXPF_INUSE);      /* Clear ESCAPE & ERROR flags */
468     
469     	sp->duplex = 0;
470     	sp->tx_delay    = SIXP_TXDELAY;
471     	sp->persistance = SIXP_PERSIST;
472     	sp->slottime    = SIXP_SLOTTIME;
473     	sp->led_state   = 0x60;
474     	sp->status      = 1;
475     	sp->status1     = 1;
476     	sp->status2     = 0;
477     	sp->tnc_ok      = 0;
478     	sp->tx_enable   = 0;
479     
480     	netif_start_queue(dev);
481     
482     	init_timer(&sp->tx_t);
483     	init_timer(&sp->resync_t);
484     	return 0;
485     }
486     
487     
488     /* Close the low-level part of the 6pack channel. */
489     static int sp_close(struct net_device *dev)
490     {
491     	struct sixpack *sp = (struct sixpack *) dev->priv;
492     
493     	if (sp->tty == NULL)
494     		return -EBUSY;
495     
496     	sp->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
497     
498     	netif_stop_queue(dev);
499     	return 0;
500     }
501     
502     static int sixpack_receive_room(struct tty_struct *tty)
503     {
504     	return 65536;  /* We can handle an infinite amount of data. :-) */
505     }
506     
507     /* !!! receive state machine */
508     
509     /*
510      * Handle the 'receiver data ready' interrupt.
511      * This function is called by the 'tty_io' module in the kernel when
512      * a block of 6pack data has been received, which can now be decapsulated
513      * and sent on to some IP layer for further processing.
514      */
515     static void sixpack_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
516     {
517     	unsigned char buf[512];
518     	unsigned long flags;
519     	int count1;
520     
521     	struct sixpack *sp = (struct sixpack *) tty->disc_data;
522     
523     	if (!sp || sp->magic != SIXPACK_MAGIC ||
524     	    !netif_running(sp->dev) || !count)
525     		return;
526     
527     	save_flags(flags);
528     	cli();
529     	memcpy(buf, cp, count<sizeof(buf)? count:sizeof(buf));
530     	restore_flags(flags);
531     
532     	/* Read the characters out of the buffer */
533     
534     	count1 = count;
535     	while (count) {
536     		count--;
537     		if (fp && *fp++) {
538     			if (!test_and_set_bit(SIXPF_ERROR, &sp->flags))
539     				sp->stats.rx_errors++;
540     			continue;
541     		}
542     	}
543     	sixpack_decode(sp, buf, count1);
544     }
545     
546     /*
547      * Open the high-level part of the 6pack channel.
548      * This function is called by the TTY module when the
549      * 6pack line discipline is called for.  Because we are
550      * sure the tty line exists, we only have to link it to
551      * a free 6pcack channel...
552      */
553     static int sixpack_open(struct tty_struct *tty)
554     {
555     	struct sixpack *sp = (struct sixpack *) tty->disc_data;
556     	int err;
557     
558     	/* First make sure we're not already connected. */
559     
560     	if (sp && sp->magic == SIXPACK_MAGIC)
561     		return -EEXIST;
562     
563     	/* OK.  Find a free 6pack channel to use. */
564     	if ((sp = sp_alloc()) == NULL)
565     		return -ENFILE;
566     	sp->tty = tty;
567     	tty->disc_data = sp;
568     	if (tty->driver.flush_buffer)
569     		tty->driver.flush_buffer(tty);
570     
571     	if (tty->ldisc.flush_buffer)
572     		tty->ldisc.flush_buffer(tty);
573     
574     	/* Restore default settings */
575     	sp->dev->type = ARPHRD_AX25;
576     
577     	/* Perform the low-level 6pack initialization. */
578     	if ((err = sp_open(sp->dev)))
579     		return err;
580     
581     	/* Done.  We have linked the TTY line to a channel. */
582     
583     	tnc_init(sp);
584     
585     	return sp->dev->base_addr;
586     }
587     
588     
589     /*
590      * Close down a 6pack channel.
591      * This means flushing out any pending queues, and then restoring the
592      * TTY line discipline to what it was before it got hooked to 6pack
593      * (which usually is TTY again).
594      */
595     static void sixpack_close(struct tty_struct *tty)
596     {
597     	struct sixpack *sp = (struct sixpack *) tty->disc_data;
598     
599     	/* First make sure we're connected. */
600     	if (!sp || sp->magic != SIXPACK_MAGIC)
601     		return;
602     
603     	rtnl_lock();
604     	dev_close(sp->dev);
605     
606     	del_timer(&sp->tx_t);
607     	del_timer(&sp->resync_t);
608     
609     	tty->disc_data = 0;
610     	sp->tty = NULL;
611     
612     	sp_free(sp);
613     	unregister_netdevice(sp->dev);
614     	rtnl_unlock();
615     }
616     
617     
618     static struct net_device_stats *sp_get_stats(struct net_device *dev)
619     {
620     	struct sixpack *sp = (struct sixpack *) dev->priv;
621     	return &sp->stats;
622     }
623     
624     
625     static int sp_set_mac_address(struct net_device *dev, void *addr)
626     {
627     	return copy_from_user(dev->dev_addr, addr, AX25_ADDR_LEN) ? -EFAULT : 0;
628     }
629     
630     static int sp_set_dev_mac_address(struct net_device *dev, void *addr)
631     {
632     	struct sockaddr *sa = addr;
633     	memcpy(dev->dev_addr, sa->sa_data, AX25_ADDR_LEN);
634     	return 0;
635     }
636     
637     
638     /* Perform I/O control on an active 6pack channel. */
639     static int sixpack_ioctl(struct tty_struct *tty, void *file, int cmd, void *arg)
640     {
641     	struct sixpack *sp = (struct sixpack *) tty->disc_data;
642     	unsigned int tmp;
643     
644     	/* First make sure we're connected. */
645     	if (!sp || sp->magic != SIXPACK_MAGIC)
646     		return -EINVAL;
647     
648     	switch(cmd) {
649     	case SIOCGIFNAME:
650     		return copy_to_user(arg, sp->dev->name, strlen(sp->dev->name) + 1) ? -EFAULT : 0;
651     
652     	case SIOCGIFENCAP:
653     		return put_user(0, (int *)arg);
654     
655     	case SIOCSIFENCAP:
656     		if (get_user(tmp, (int *) arg))
657     			return -EFAULT;
658     
659     		sp->mode = tmp;
660     		sp->dev->addr_len        = AX25_ADDR_LEN;	  /* sizeof an AX.25 addr */
661     		sp->dev->hard_header_len = AX25_KISS_HEADER_LEN + AX25_MAX_HEADER_LEN + 3;
662     		sp->dev->type            = ARPHRD_AX25;
663     
664     		return 0;
665     
666     	 case SIOCSIFHWADDR:
667     		return sp_set_mac_address(sp->dev, arg);
668     
669     	/* Allow stty to read, but not set, the serial port */
670     	case TCGETS:
671     	case TCGETA:
672     		return n_tty_ioctl(tty, (struct file *) file, cmd, (unsigned long) arg);
673     
674     	default:
675     		return -ENOIOCTLCMD;
676     	}
677     }
678     
679     static int sp_open_dev(struct net_device *dev)
680     {
681     	struct sixpack *sp = (struct sixpack *) dev->priv;
682     	if (sp->tty == NULL)
683     		return -ENODEV;
684     	return 0;
685     }
686     
687     /* Fill in our line protocol discipline */
688     static struct tty_ldisc sp_ldisc = {
689     	magic:		TTY_LDISC_MAGIC,
690     	name:		"6pack",
691     	open:		sixpack_open,
692     	close:		sixpack_close,
693     	ioctl:		(int (*)(struct tty_struct *, struct file *,
694     			unsigned int, unsigned long)) sixpack_ioctl,
695     	receive_buf:	sixpack_receive_buf,
696     	receive_room:	sixpack_receive_room,
697     	write_wakeup:	sixpack_write_wakeup,
698     };
699     
700     /* Initialize 6pack control device -- register 6pack line discipline */
701     
702     static char msg_banner[]  __initdata = KERN_INFO "AX.25: 6pack driver, " SIXPACK_VERSION " (dynamic channels, max=%d)\n";
703     static char msg_invparm[] __initdata = KERN_ERR  "6pack: sixpack_maxdev parameter too large.\n";
704     static char msg_nomem[]   __initdata = KERN_ERR  "6pack: can't allocate sixpack_ctrls[] array! No 6pack available.\n";
705     static char msg_regfail[] __initdata = KERN_ERR  "6pack: can't register line discipline (err = %d)\n";
706     
707     static int __init sixpack_init_driver(void)
708     {
709     	int status;
710     
711     	/* Do sanity checks on maximum device parameter. */
712     	if (sixpack_maxdev < 4)
713     		sixpack_maxdev = 4;
714     
715     	printk(msg_banner, sixpack_maxdev);
716     
717     	sixpack_ctrls = (sixpack_ctrl_t **) kmalloc(sizeof(void*)*sixpack_maxdev, GFP_KERNEL);
718     	if (sixpack_ctrls == NULL) {
719     		printk(msg_nomem);
720     		return -ENOMEM;
721     	}
722     
723     	/* Clear the pointer array, we allocate devices when we need them */
724     	memset(sixpack_ctrls, 0, sizeof(void*)*sixpack_maxdev); /* Pointers */
725     
726     	/* Register the provided line protocol discipline */
727     	if ((status = tty_register_ldisc(N_6PACK, &sp_ldisc)) != 0) {
728     		printk(msg_regfail, status);
729     		kfree(sixpack_ctrls);
730     	}
731     
732     	return status;
733     }
734     
735     static const char msg_unregfail[] __exitdata = KERN_ERR "6pack: can't unregister line discipline (err = %d)\n";
736     
737     static void __exit sixpack_exit_driver(void)
738     {
739     	int i;
740     
741     	if ((i = tty_register_ldisc(N_6PACK, NULL)))
742     		printk(msg_unregfail, i);
743     
744     	for (i = 0; i < sixpack_maxdev; i++) {
745     		if (sixpack_ctrls[i]) {
746     			/*
747     			* VSV = if dev->start==0, then device
748     			* unregistered while close proc.
749     			*/
750     			if (netif_running(&sixpack_ctrls[i]->dev))
751     				 unregister_netdev(&sixpack_ctrls[i]->dev);
752     
753     			kfree(sixpack_ctrls[i]);
754     		}
755     	}
756     	kfree(sixpack_ctrls);
757     }
758     
759     
760     /* Initialize the 6pack driver.  Called by DDI. */
761     static int sixpack_init(struct net_device *dev)
762     {
763     	struct sixpack *sp = (struct sixpack *) dev->priv;
764     
765     	static char ax25_bcast[AX25_ADDR_LEN] =
766     		{'Q'<<1,'S'<<1,'T'<<1,' '<<1,' '<<1,' '<<1,'0'<<1};
767     	static char ax25_test[AX25_ADDR_LEN] =
768     		{'L'<<1,'I'<<1,'N'<<1,'U'<<1,'X'<<1,' '<<1,'1'<<1};
769     
770     	if (sp == NULL)		/* Allocation failed ?? */
771     		return -ENODEV;
772     
773     	/* Set up the "6pack Control Block". (And clear statistics) */
774     
775     	memset(sp, 0, sizeof (struct sixpack));
776     	sp->magic  = SIXPACK_MAGIC;
777     	sp->dev	   = dev;
778     
779     	/* Finish setting up the DEVICE info. */
780     	dev->mtu		= SIXP_MTU;
781     	dev->hard_start_xmit	= sp_xmit;
782     	dev->open		= sp_open_dev;
783     	dev->stop		= sp_close;
784     	dev->hard_header	= sp_header;
785     	dev->get_stats	        = sp_get_stats;
786     	dev->set_mac_address    = sp_set_dev_mac_address;
787     	dev->hard_header_len	= AX25_MAX_HEADER_LEN;
788     	dev->addr_len		= AX25_ADDR_LEN;
789     	dev->type		= ARPHRD_AX25;
790     	dev->tx_queue_len	= 10;
791     	dev->rebuild_header	= sp_rebuild_header;
792     	dev->tx_timeout		= NULL;
793     
794     	memcpy(dev->broadcast, ax25_bcast, AX25_ADDR_LEN);	/* Only activated in AX.25 mode */
795     	memcpy(dev->dev_addr, ax25_test, AX25_ADDR_LEN);	/*    ""      ""       ""    "" */
796     
797     	/* New-style flags. */
798     	dev->flags		= 0;
799     
800     	return 0;
801     }
802     
803     
804     
805     
806     /* ----> 6pack timer interrupt handler and friends. <---- */
807     static void sp_start_tx_timer(struct sixpack *sp)
808     {
809     	int when = sp->slottime;
810     
811     	del_timer(&sp->tx_t);
812     	sp->tx_t.data = (unsigned long) sp;
813     	sp->tx_t.function = sp_xmit_on_air;
814     	sp->tx_t.expires = jiffies + ((when+1)*HZ)/100;
815     	add_timer(&sp->tx_t);
816     }
817     
818     
819     /* encode an AX.25 packet into 6pack */
820     
821     static int encode_sixpack(unsigned char *tx_buf, unsigned char *tx_buf_raw, int length, unsigned char tx_delay)
822     {
823     	int count = 0;
824     	unsigned char checksum = 0, buf[400];
825     	int raw_count = 0;
826     
827     	tx_buf_raw[raw_count++] = SIXP_PRIO_CMD_MASK | SIXP_TX_MASK;
828     	tx_buf_raw[raw_count++] = SIXP_SEOF;
829     
830     	buf[0] = tx_delay;
831     	for (count = 1; count < length; count++)
832     		buf[count] = tx_buf[count];
833     
834     	for (count = 0; count < length; count++)
835     		checksum += buf[count];
836     	buf[length] = (unsigned char) 0xff - checksum;
837     
838     	for (count = 0; count <= length; count++) {
839     		if ((count % 3) == 0) {
840     			tx_buf_raw[raw_count++] = (buf[count] & 0x3f);
841     			tx_buf_raw[raw_count] = ((buf[count] >> 2) & 0x30);
842     		} else if ((count % 3) == 1) {
843     			tx_buf_raw[raw_count++] |= (buf[count] & 0x0f);
844     			tx_buf_raw[raw_count] =	((buf[count] >> 2) & 0x3c);
845     		} else {
846     			tx_buf_raw[raw_count++] |= (buf[count] & 0x03);
847     			tx_buf_raw[raw_count++] = (buf[count] >> 2);
848     		}
849     	}
850     	if ((length % 3) != 2)
851     		raw_count++;
852     	tx_buf_raw[raw_count++] = SIXP_SEOF;
853     	return raw_count;
854     }
855     
856     
857     /* decode a 6pack packet */
858     
859     static void
860     sixpack_decode(struct sixpack *sp, unsigned char pre_rbuff[], int count)
861     {
862     	unsigned char inbyte;
863     	int count1;
864     
865     	for (count1 = 0; count1 < count; count1++) {
866     		inbyte = pre_rbuff[count1];
867     		if (inbyte == SIXP_FOUND_TNC) {
868     			printk(KERN_INFO "6pack: TNC found.\n");
869     			sp->tnc_ok = 1;
870     			del_timer(&sp->resync_t);
871     		}
872     		if ((inbyte & SIXP_PRIO_CMD_MASK) != 0)
873     			decode_prio_command(inbyte, sp);
874     		else if ((inbyte & SIXP_STD_CMD_MASK) != 0)
875     			decode_std_command(inbyte, sp);
876     		else if ((sp->status & SIXP_RX_DCD_MASK) == SIXP_RX_DCD_MASK)
877     			decode_data(inbyte, sp);
878     	}
879     }
880     
881     static int tnc_init(struct sixpack *sp)
882     {
883     	unsigned char inbyte = 0xe8;
884     
885     	sp->tty->driver.write(sp->tty, 0, &inbyte, 1);
886     
887     	del_timer(&sp->resync_t);
888     	sp->resync_t.data = (unsigned long) sp;
889     	sp->resync_t.function = resync_tnc;
890     	sp->resync_t.expires = jiffies + SIXP_RESYNC_TIMEOUT;
891     	add_timer(&sp->resync_t);
892     
893     	return 0;
894     }
895     
896     
897     /* identify and execute a 6pack priority command byte */
898     
899     static void decode_prio_command(unsigned char cmd, struct sixpack *sp)
900     {
901     	unsigned char channel;
902     	int actual;
903     
904     	channel = cmd & SIXP_CHN_MASK;
905     	if ((cmd & SIXP_PRIO_DATA_MASK) != 0) {     /* idle ? */
906     
907     	/* RX and DCD flags can only be set in the same prio command,
908     	   if the DCD flag has been set without the RX flag in the previous
909     	   prio command. If DCD has not been set before, something in the
910     	   transmission has gone wrong. In this case, RX and DCD are
911     	   cleared in order to prevent the decode_data routine from
912     	   reading further data that might be corrupt. */
913     
914     		if (((sp->status & SIXP_DCD_MASK) == 0) &&
915     			((cmd & SIXP_RX_DCD_MASK) == SIXP_RX_DCD_MASK)) {
916     				if (sp->status != 1)
917     					printk(KERN_DEBUG "6pack: protocol violation\n");
918     				else
919     					sp->status = 0;
920     				cmd &= !SIXP_RX_DCD_MASK;
921     		}
922     		sp->status = cmd & SIXP_PRIO_DATA_MASK;
923     	}
924     	else { /* output watchdog char if idle */
925     		if ((sp->status2 != 0) && (sp->duplex == 1)) {
926     			sp->led_state = 0x70;
927     			sp->tty->driver.write(sp->tty, 0, &sp->led_state, 1);
928     			sp->tx_enable = 1;
929     			actual = sp->tty->driver.write(sp->tty, 0, sp->xbuff, sp->status2);
930     			sp->xleft -= actual;
931     			sp->xhead += actual;
932     			sp->led_state = 0x60;
933     			sp->status2 = 0;
934     
935     		}
936     	}
937     
938     	/* needed to trigger the TNC watchdog */
939     	sp->tty->driver.write(sp->tty, 0, &sp->led_state, 1);
940     
941             /* if the state byte has been received, the TNC is present,
942                so the resync timer can be reset. */
943     
944     	if (sp->tnc_ok == 1) {
945     		del_timer(&sp->resync_t);
946     		sp->resync_t.data = (unsigned long) sp;
947     		sp->resync_t.function = resync_tnc;
948     		sp->resync_t.expires = jiffies + SIXP_INIT_RESYNC_TIMEOUT;
949     		add_timer(&sp->resync_t);
950     	}
951     
952     	sp->status1 = cmd & SIXP_PRIO_DATA_MASK;
953     }
954     
955     /* try to resync the TNC. Called by the resync timer defined in
956       decode_prio_command */
957     
958     static void resync_tnc(unsigned long channel)
959     {
960     	static char resync_cmd = 0xe8;
961     	struct sixpack *sp = (struct sixpack *) channel;
962     
963     	printk(KERN_INFO "6pack: resyncing TNC\n");
964     
965     	/* clear any data that might have been received */
966     
967     	sp->rx_count = 0;
968     	sp->rx_count_cooked = 0;
969     
970     	/* reset state machine */
971     
972     	sp->status = 1;
973     	sp->status1 = 1;
974     	sp->status2 = 0;
975     	sp->tnc_ok = 0;
976     
977     	/* resync the TNC */
978     
979     	sp->led_state = 0x60;
980     	sp->tty->driver.write(sp->tty, 0, &sp->led_state, 1);
981     	sp->tty->driver.write(sp->tty, 0, &resync_cmd, 1);
982     
983     
984     	/* Start resync timer again -- the TNC might be still absent */
985     
986     	del_timer(&sp->resync_t);
987     	sp->resync_t.data = (unsigned long) sp;
988     	sp->resync_t.function = resync_tnc;
989     	sp->resync_t.expires = jiffies + SIXP_RESYNC_TIMEOUT;
990     	add_timer(&sp->resync_t);
991     }
992     
993     
994     
995     /* identify and execute a standard 6pack command byte */
996     
997     static void decode_std_command(unsigned char cmd, struct sixpack *sp)
998     {
999     	unsigned char checksum = 0, rest = 0, channel;
1000     	short i;
1001     
1002     	channel = cmd & SIXP_CHN_MASK;
1003     	switch (cmd & SIXP_CMD_MASK) {     /* normal command */
1004     		case SIXP_SEOF:
1005     			if ((sp->rx_count == 0) && (sp->rx_count_cooked == 0)) {
1006     				if ((sp->status & SIXP_RX_DCD_MASK) ==
1007     					SIXP_RX_DCD_MASK) {
1008     					sp->led_state = 0x68;
1009     					sp->tty->driver.write(sp->tty, 0, &sp->led_state, 1);
1010     				}
1011     			} else {
1012     				sp->led_state = 0x60;
1013     				/* fill trailing bytes with zeroes */
1014     				sp->tty->driver.write(sp->tty, 0, &sp->led_state, 1);
1015     				rest = sp->rx_count;
1016     				if (rest != 0)
1017     					 for (i = rest; i <= 3; i++)
1018     						decode_data(0, sp);
1019     				if (rest == 2)
1020     					sp->rx_count_cooked -= 2;
1021     				else if (rest == 3)
1022     					sp->rx_count_cooked -= 1;
1023     				for (i = 0; i < sp->rx_count_cooked; i++)
1024     					checksum += sp->cooked_buf[i];
1025     				if (checksum != SIXP_CHKSUM) {
1026     					printk(KERN_DEBUG "6pack: bad checksum %2.2x\n", checksum);
1027     				} else {
1028     					sp->rcount = sp->rx_count_cooked-2;
1029     					sp_bump(sp, 0);
1030     				}
1031     				sp->rx_count_cooked = 0;
1032     			}
1033     			break;
1034     		case SIXP_TX_URUN: printk(KERN_DEBUG "6pack: TX underrun\n");
1035     			break;
1036     		case SIXP_RX_ORUN: printk(KERN_DEBUG "6pack: RX overrun\n");
1037     			break;
1038     		case SIXP_RX_BUF_OVL:
1039     			printk(KERN_DEBUG "6pack: RX buffer overflow\n");
1040     	}
1041     }
1042     
1043     /* decode 4 sixpack-encoded bytes into 3 data bytes */
1044     
1045     static void decode_data(unsigned char inbyte, struct sixpack *sp)
1046     {
1047     	unsigned char *buf;
1048     
1049     	if (sp->rx_count != 3)
1050     		sp->raw_buf[sp->rx_count++] = inbyte;
1051     	else {
1052     		buf = sp->raw_buf;
1053     		sp->cooked_buf[sp->rx_count_cooked++] =
1054     			buf[0] | ((buf[1] << 2) & 0xc0);
1055     		sp->cooked_buf[sp->rx_count_cooked++] =
1056     			(buf[1] & 0x0f) | ((buf[2] << 2) & 0xf0);
1057     		sp->cooked_buf[sp->rx_count_cooked++] =
1058     			(buf[2] & 0x03) | (inbyte << 2);
1059     		sp->rx_count = 0;
1060     	}
1061     }
1062     
1063     
1064     MODULE_AUTHOR("Andreas Könsgen <ajk@ccac.rwth-aachen.de>");
1065     MODULE_DESCRIPTION("6pack driver for AX.25");
1066     
1067     module_init(sixpack_init_driver);
1068     module_exit(sixpack_exit_driver);
1069