| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /*****************************************************************************/ | 
 | 2 |  | 
 | 3 | /* | 
 | 4 |  *	hdlcdrv.c  -- HDLC packet radio network driver. | 
 | 5 |  * | 
 | 6 |  *	Copyright (C) 1996-2000  Thomas Sailer (sailer@ife.ee.ethz.ch) | 
 | 7 |  * | 
 | 8 |  *	This program is free software; you can redistribute it and/or modify | 
 | 9 |  *	it under the terms of the GNU General Public License as published by | 
 | 10 |  *	the Free Software Foundation; either version 2 of the License, or | 
 | 11 |  *	(at your option) any later version. | 
 | 12 |  * | 
 | 13 |  *	This program is distributed in the hope that it will be useful, | 
 | 14 |  *	but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 15 |  *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
 | 16 |  *	GNU General Public License for more details. | 
 | 17 |  * | 
 | 18 |  *	You should have received a copy of the GNU General Public License | 
 | 19 |  *	along with this program; if not, write to the Free Software | 
 | 20 |  *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
 | 21 |  * | 
 | 22 |  *  Please note that the GPL allows you to use the driver, NOT the radio. | 
 | 23 |  *  In order to use the radio, you need a license from the communications | 
 | 24 |  *  authority of your country. | 
 | 25 |  * | 
 | 26 |  *  The driver was derived from Donald Beckers skeleton.c | 
 | 27 |  *	Written 1993-94 by Donald Becker. | 
 | 28 |  * | 
 | 29 |  *  History: | 
 | 30 |  *   0.1  21.09.1996  Started | 
 | 31 |  *        18.10.1996  Changed to new user space access routines  | 
 | 32 |  *                    (copy_{to,from}_user) | 
 | 33 |  *   0.2  21.11.1996  various small changes | 
 | 34 |  *   0.3  03.03.1997  fixed (hopefully) IP not working with ax.25 as a module | 
 | 35 |  *   0.4  16.04.1997  init code/data tagged | 
 | 36 |  *   0.5  30.07.1997  made HDLC buffers bigger (solves a problem with the | 
 | 37 |  *                    soundmodem driver) | 
 | 38 |  *   0.6  05.04.1998  add spinlocks | 
 | 39 |  *   0.7  03.08.1999  removed some old compatibility cruft | 
 | 40 |  *   0.8  12.02.2000  adapted to softnet driver interface | 
 | 41 |  */ | 
 | 42 |  | 
 | 43 | /*****************************************************************************/ | 
 | 44 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/module.h> | 
 | 46 | #include <linux/types.h> | 
 | 47 | #include <linux/net.h> | 
 | 48 | #include <linux/in.h> | 
 | 49 | #include <linux/if.h> | 
 | 50 | #include <linux/slab.h> | 
 | 51 | #include <linux/errno.h> | 
 | 52 | #include <linux/init.h> | 
 | 53 | #include <linux/bitops.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 |  | 
 | 55 | #include <linux/netdevice.h> | 
 | 56 | #include <linux/if_arp.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #include <linux/skbuff.h> | 
 | 58 | #include <linux/hdlcdrv.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #include <net/ax25.h>  | 
| Ralf Baechle | c4bc7ee | 2005-09-12 14:19:26 -0700 | [diff] [blame] | 60 | #include <asm/uaccess.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | #include <linux/crc-ccitt.h> | 
 | 63 |  | 
 | 64 | /* --------------------------------------------------------------------- */ | 
 | 65 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | #define KISS_VERBOSE | 
 | 67 |  | 
 | 68 | /* --------------------------------------------------------------------- */ | 
 | 69 |  | 
 | 70 | #define PARAM_TXDELAY   1 | 
 | 71 | #define PARAM_PERSIST   2 | 
 | 72 | #define PARAM_SLOTTIME  3 | 
 | 73 | #define PARAM_TXTAIL    4 | 
 | 74 | #define PARAM_FULLDUP   5 | 
 | 75 | #define PARAM_HARDWARE  6 | 
 | 76 | #define PARAM_RETURN    255 | 
 | 77 |  | 
 | 78 | /* --------------------------------------------------------------------- */ | 
 | 79 | /* | 
 | 80 |  * the CRC routines are stolen from WAMPES | 
 | 81 |  * by Dieter Deyke | 
 | 82 |  */ | 
 | 83 |  | 
 | 84 |  | 
 | 85 | /*---------------------------------------------------------------------------*/ | 
 | 86 |  | 
 | 87 | static inline void append_crc_ccitt(unsigned char *buffer, int len) | 
 | 88 | { | 
 | 89 |  	unsigned int crc = crc_ccitt(0xffff, buffer, len) ^ 0xffff; | 
 | 90 | 	*buffer++ = crc; | 
 | 91 | 	*buffer++ = crc >> 8; | 
 | 92 | } | 
 | 93 |  | 
 | 94 | /*---------------------------------------------------------------------------*/ | 
 | 95 |  | 
 | 96 | static inline int check_crc_ccitt(const unsigned char *buf, int cnt) | 
 | 97 | { | 
 | 98 | 	return (crc_ccitt(0xffff, buf, cnt) & 0xffff) == 0xf0b8; | 
 | 99 | } | 
 | 100 |  | 
 | 101 | /*---------------------------------------------------------------------------*/ | 
 | 102 |  | 
 | 103 | #if 0 | 
 | 104 | static int calc_crc_ccitt(const unsigned char *buf, int cnt) | 
 | 105 | { | 
 | 106 | 	unsigned int crc = 0xffff; | 
 | 107 |  | 
 | 108 | 	for (; cnt > 0; cnt--) | 
 | 109 | 		crc = (crc >> 8) ^ crc_ccitt_table[(crc ^ *buf++) & 0xff]; | 
 | 110 | 	crc ^= 0xffff; | 
 | 111 | 	return (crc & 0xffff); | 
 | 112 | } | 
 | 113 | #endif | 
 | 114 |  | 
 | 115 | /* ---------------------------------------------------------------------- */ | 
 | 116 |  | 
 | 117 | #define tenms_to_2flags(s,tenms) ((tenms * s->par.bitrate) / 100 / 16) | 
 | 118 |  | 
 | 119 | /* ---------------------------------------------------------------------- */ | 
 | 120 | /* | 
 | 121 |  * The HDLC routines | 
 | 122 |  */ | 
 | 123 |  | 
 | 124 | static int hdlc_rx_add_bytes(struct hdlcdrv_state *s, unsigned int bits,  | 
 | 125 | 			     int num) | 
 | 126 | { | 
 | 127 | 	int added = 0; | 
 | 128 | 	 | 
 | 129 | 	while (s->hdlcrx.rx_state && num >= 8) { | 
 | 130 | 		if (s->hdlcrx.len >= sizeof(s->hdlcrx.buffer)) { | 
 | 131 | 			s->hdlcrx.rx_state = 0; | 
 | 132 | 			return 0; | 
 | 133 | 		} | 
 | 134 | 		*s->hdlcrx.bp++ = bits >> (32-num); | 
 | 135 | 		s->hdlcrx.len++; | 
 | 136 | 		num -= 8; | 
 | 137 | 		added += 8; | 
 | 138 | 	} | 
 | 139 | 	return added; | 
 | 140 | } | 
 | 141 |  | 
 | 142 | static void hdlc_rx_flag(struct net_device *dev, struct hdlcdrv_state *s) | 
 | 143 | { | 
 | 144 | 	struct sk_buff *skb; | 
 | 145 | 	int pkt_len; | 
 | 146 | 	unsigned char *cp; | 
 | 147 |  | 
 | 148 | 	if (s->hdlcrx.len < 4)  | 
 | 149 | 		return; | 
 | 150 | 	if (!check_crc_ccitt(s->hdlcrx.buffer, s->hdlcrx.len))  | 
 | 151 | 		return; | 
 | 152 | 	pkt_len = s->hdlcrx.len - 2 + 1; /* KISS kludge */ | 
 | 153 | 	if (!(skb = dev_alloc_skb(pkt_len))) { | 
 | 154 | 		printk("%s: memory squeeze, dropping packet\n", dev->name); | 
 | 155 | 		s->stats.rx_dropped++; | 
 | 156 | 		return; | 
 | 157 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | 	cp = skb_put(skb, pkt_len); | 
 | 159 | 	*cp++ = 0; /* KISS kludge */ | 
 | 160 | 	memcpy(cp, s->hdlcrx.buffer, pkt_len - 1); | 
| Arnaldo Carvalho de Melo | 56cb515 | 2005-04-24 18:53:06 -0700 | [diff] [blame] | 161 | 	skb->protocol = ax25_type_trans(skb, dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | 	netif_rx(skb); | 
 | 163 | 	dev->last_rx = jiffies; | 
 | 164 | 	s->stats.rx_packets++; | 
 | 165 | } | 
 | 166 |  | 
 | 167 | void hdlcdrv_receiver(struct net_device *dev, struct hdlcdrv_state *s) | 
 | 168 | { | 
 | 169 | 	int i; | 
 | 170 | 	unsigned int mask1, mask2, mask3, mask4, mask5, mask6, word; | 
 | 171 | 	 | 
 | 172 | 	if (!s || s->magic != HDLCDRV_MAGIC)  | 
 | 173 | 		return; | 
 | 174 | 	if (test_and_set_bit(0, &s->hdlcrx.in_hdlc_rx)) | 
 | 175 | 		return; | 
 | 176 |  | 
 | 177 | 	while (!hdlcdrv_hbuf_empty(&s->hdlcrx.hbuf)) { | 
 | 178 | 		word = hdlcdrv_hbuf_get(&s->hdlcrx.hbuf);	 | 
 | 179 |  | 
 | 180 | #ifdef HDLCDRV_DEBUG | 
 | 181 | 		hdlcdrv_add_bitbuffer_word(&s->bitbuf_hdlc, word); | 
 | 182 | #endif /* HDLCDRV_DEBUG */ | 
 | 183 | 	       	s->hdlcrx.bitstream >>= 16; | 
 | 184 | 		s->hdlcrx.bitstream |= word << 16; | 
 | 185 | 		s->hdlcrx.bitbuf >>= 16; | 
 | 186 | 		s->hdlcrx.bitbuf |= word << 16; | 
 | 187 | 		s->hdlcrx.numbits += 16; | 
 | 188 | 		for(i = 15, mask1 = 0x1fc00, mask2 = 0x1fe00, mask3 = 0x0fc00, | 
 | 189 | 		    mask4 = 0x1f800, mask5 = 0xf800, mask6 = 0xffff;  | 
 | 190 | 		    i >= 0;  | 
 | 191 | 		    i--, mask1 <<= 1, mask2 <<= 1, mask3 <<= 1, mask4 <<= 1,  | 
 | 192 | 		    mask5 <<= 1, mask6 = (mask6 << 1) | 1) { | 
 | 193 | 			if ((s->hdlcrx.bitstream & mask1) == mask1) | 
 | 194 | 				s->hdlcrx.rx_state = 0; /* abort received */ | 
 | 195 | 			else if ((s->hdlcrx.bitstream & mask2) == mask3) { | 
 | 196 | 				/* flag received */ | 
 | 197 | 				if (s->hdlcrx.rx_state) { | 
 | 198 | 					hdlc_rx_add_bytes(s, s->hdlcrx.bitbuf  | 
 | 199 | 							  << (8+i), | 
 | 200 | 							  s->hdlcrx.numbits | 
 | 201 | 							  -8-i); | 
 | 202 | 					hdlc_rx_flag(dev, s); | 
 | 203 | 				} | 
 | 204 | 				s->hdlcrx.len = 0; | 
 | 205 | 				s->hdlcrx.bp = s->hdlcrx.buffer; | 
 | 206 | 				s->hdlcrx.rx_state = 1; | 
 | 207 | 				s->hdlcrx.numbits = i; | 
 | 208 | 			} else if ((s->hdlcrx.bitstream & mask4) == mask5) { | 
 | 209 | 				/* stuffed bit */ | 
 | 210 | 				s->hdlcrx.numbits--; | 
 | 211 | 				s->hdlcrx.bitbuf = (s->hdlcrx.bitbuf & (~mask6)) | | 
 | 212 | 					((s->hdlcrx.bitbuf & mask6) << 1); | 
 | 213 | 			} | 
 | 214 | 		} | 
 | 215 | 		s->hdlcrx.numbits -= hdlc_rx_add_bytes(s, s->hdlcrx.bitbuf, | 
 | 216 | 						       s->hdlcrx.numbits); | 
 | 217 | 	} | 
 | 218 | 	clear_bit(0, &s->hdlcrx.in_hdlc_rx); | 
 | 219 | } | 
 | 220 |  | 
 | 221 | /* ---------------------------------------------------------------------- */ | 
 | 222 |  | 
 | 223 | static inline void do_kiss_params(struct hdlcdrv_state *s, | 
 | 224 | 				  unsigned char *data, unsigned long len) | 
 | 225 | { | 
 | 226 |  | 
 | 227 | #ifdef KISS_VERBOSE | 
 | 228 | #define PKP(a,b) printk(KERN_INFO "hdlcdrv.c: channel params: " a "\n", b) | 
 | 229 | #else /* KISS_VERBOSE */	       | 
 | 230 | #define PKP(a,b)  | 
 | 231 | #endif /* KISS_VERBOSE */	       | 
 | 232 |  | 
 | 233 | 	if (len < 2) | 
 | 234 | 		return; | 
 | 235 | 	switch(data[0]) { | 
 | 236 | 	case PARAM_TXDELAY: | 
 | 237 | 		s->ch_params.tx_delay = data[1]; | 
 | 238 | 		PKP("TX delay = %ums", 10 * s->ch_params.tx_delay); | 
 | 239 | 		break; | 
 | 240 | 	case PARAM_PERSIST:    | 
 | 241 | 		s->ch_params.ppersist = data[1]; | 
 | 242 | 		PKP("p persistence = %u", s->ch_params.ppersist); | 
 | 243 | 		break; | 
 | 244 | 	case PARAM_SLOTTIME:   | 
 | 245 | 		s->ch_params.slottime = data[1]; | 
 | 246 | 		PKP("slot time = %ums", s->ch_params.slottime); | 
 | 247 | 		break; | 
 | 248 | 	case PARAM_TXTAIL:     | 
 | 249 | 		s->ch_params.tx_tail = data[1]; | 
 | 250 | 		PKP("TX tail = %ums", s->ch_params.tx_tail); | 
 | 251 | 		break; | 
 | 252 | 	case PARAM_FULLDUP:    | 
 | 253 | 		s->ch_params.fulldup = !!data[1]; | 
 | 254 | 		PKP("%s duplex", s->ch_params.fulldup ? "full" : "half"); | 
 | 255 | 		break; | 
 | 256 | 	default: | 
 | 257 | 		break; | 
 | 258 | 	} | 
 | 259 | #undef PKP | 
 | 260 | } | 
 | 261 |  | 
 | 262 | /* ---------------------------------------------------------------------- */ | 
 | 263 |  | 
 | 264 | void hdlcdrv_transmitter(struct net_device *dev, struct hdlcdrv_state *s) | 
 | 265 | { | 
 | 266 | 	unsigned int mask1, mask2, mask3; | 
 | 267 | 	int i; | 
 | 268 | 	struct sk_buff *skb; | 
 | 269 | 	int pkt_len; | 
 | 270 |  | 
 | 271 | 	if (!s || s->magic != HDLCDRV_MAGIC)  | 
 | 272 | 		return; | 
 | 273 | 	if (test_and_set_bit(0, &s->hdlctx.in_hdlc_tx)) | 
 | 274 | 		return; | 
 | 275 | 	for (;;) { | 
 | 276 | 		if (s->hdlctx.numbits >= 16) { | 
 | 277 | 			if (hdlcdrv_hbuf_full(&s->hdlctx.hbuf)) { | 
 | 278 | 				clear_bit(0, &s->hdlctx.in_hdlc_tx); | 
 | 279 | 				return; | 
 | 280 | 			} | 
 | 281 | 			hdlcdrv_hbuf_put(&s->hdlctx.hbuf, s->hdlctx.bitbuf); | 
 | 282 | 			s->hdlctx.bitbuf >>= 16; | 
 | 283 | 			s->hdlctx.numbits -= 16; | 
 | 284 | 		} | 
 | 285 | 		switch (s->hdlctx.tx_state) { | 
 | 286 | 		default: | 
 | 287 | 			clear_bit(0, &s->hdlctx.in_hdlc_tx); | 
 | 288 | 			return; | 
 | 289 | 		case 0: | 
 | 290 | 		case 1: | 
 | 291 | 			if (s->hdlctx.numflags) { | 
 | 292 | 				s->hdlctx.numflags--; | 
 | 293 | 				s->hdlctx.bitbuf |=  | 
 | 294 | 					0x7e7e << s->hdlctx.numbits; | 
 | 295 | 				s->hdlctx.numbits += 16; | 
 | 296 | 				break; | 
 | 297 | 			} | 
 | 298 | 			if (s->hdlctx.tx_state == 1) { | 
 | 299 | 				clear_bit(0, &s->hdlctx.in_hdlc_tx); | 
 | 300 | 				return; | 
 | 301 | 			} | 
 | 302 | 			if (!(skb = s->skb)) { | 
 | 303 | 				int flgs = tenms_to_2flags(s, s->ch_params.tx_tail); | 
 | 304 | 				if (flgs < 2) | 
 | 305 | 					flgs = 2; | 
 | 306 | 				s->hdlctx.tx_state = 1; | 
 | 307 | 				s->hdlctx.numflags = flgs; | 
 | 308 | 				break; | 
 | 309 | 			} | 
 | 310 | 			s->skb = NULL; | 
 | 311 | 			netif_wake_queue(dev); | 
 | 312 | 			pkt_len = skb->len-1; /* strip KISS byte */ | 
 | 313 | 			if (pkt_len >= HDLCDRV_MAXFLEN || pkt_len < 2) { | 
 | 314 | 				s->hdlctx.tx_state = 0; | 
 | 315 | 				s->hdlctx.numflags = 1; | 
 | 316 | 				dev_kfree_skb_irq(skb); | 
 | 317 | 				break; | 
 | 318 | 			} | 
 | 319 | 			memcpy(s->hdlctx.buffer, skb->data+1, pkt_len); | 
 | 320 | 			dev_kfree_skb_irq(skb); | 
 | 321 | 			s->hdlctx.bp = s->hdlctx.buffer; | 
 | 322 | 			append_crc_ccitt(s->hdlctx.buffer, pkt_len); | 
 | 323 | 			s->hdlctx.len = pkt_len+2; /* the appended CRC */ | 
 | 324 | 			s->hdlctx.tx_state = 2; | 
 | 325 | 			s->hdlctx.bitstream = 0; | 
 | 326 | 			s->stats.tx_packets++; | 
 | 327 | 			break; | 
 | 328 | 		case 2: | 
 | 329 | 			if (!s->hdlctx.len) { | 
 | 330 | 				s->hdlctx.tx_state = 0; | 
 | 331 | 				s->hdlctx.numflags = 1; | 
 | 332 | 				break; | 
 | 333 | 			} | 
 | 334 | 			s->hdlctx.len--; | 
 | 335 | 			s->hdlctx.bitbuf |= *s->hdlctx.bp << | 
 | 336 | 				s->hdlctx.numbits; | 
 | 337 | 			s->hdlctx.bitstream >>= 8; | 
 | 338 | 			s->hdlctx.bitstream |= (*s->hdlctx.bp++) << 16; | 
 | 339 | 			mask1 = 0x1f000; | 
 | 340 | 			mask2 = 0x10000; | 
 | 341 | 			mask3 = 0xffffffff >> (31-s->hdlctx.numbits); | 
 | 342 | 			s->hdlctx.numbits += 8; | 
 | 343 | 			for(i = 0; i < 8; i++, mask1 <<= 1, mask2 <<= 1,  | 
 | 344 | 			    mask3 = (mask3 << 1) | 1) { | 
 | 345 | 				if ((s->hdlctx.bitstream & mask1) != mask1)  | 
 | 346 | 					continue; | 
 | 347 | 				s->hdlctx.bitstream &= ~mask2; | 
 | 348 | 				s->hdlctx.bitbuf =  | 
 | 349 | 					(s->hdlctx.bitbuf & mask3) | | 
 | 350 | 						((s->hdlctx.bitbuf &  | 
 | 351 | 						 (~mask3)) << 1); | 
 | 352 | 				s->hdlctx.numbits++; | 
 | 353 | 				mask3 = (mask3 << 1) | 1; | 
 | 354 | 			} | 
 | 355 | 			break; | 
 | 356 | 		} | 
 | 357 | 	} | 
 | 358 | } | 
 | 359 |  | 
 | 360 | /* ---------------------------------------------------------------------- */ | 
 | 361 |  | 
 | 362 | static void start_tx(struct net_device *dev, struct hdlcdrv_state *s) | 
 | 363 | { | 
 | 364 | 	s->hdlctx.tx_state = 0; | 
 | 365 | 	s->hdlctx.numflags = tenms_to_2flags(s, s->ch_params.tx_delay); | 
 | 366 | 	s->hdlctx.bitbuf = s->hdlctx.bitstream = s->hdlctx.numbits = 0; | 
 | 367 | 	hdlcdrv_transmitter(dev, s); | 
 | 368 | 	s->hdlctx.ptt = 1; | 
 | 369 | 	s->ptt_keyed++; | 
 | 370 | } | 
 | 371 |  | 
 | 372 | /* ---------------------------------------------------------------------- */ | 
 | 373 |  | 
 | 374 | static unsigned short random_seed; | 
 | 375 |  | 
 | 376 | static inline unsigned short random_num(void) | 
 | 377 | { | 
 | 378 | 	random_seed = 28629 * random_seed + 157; | 
 | 379 | 	return random_seed; | 
 | 380 | } | 
 | 381 |  | 
 | 382 | /* ---------------------------------------------------------------------- */ | 
 | 383 |  | 
 | 384 | void hdlcdrv_arbitrate(struct net_device *dev, struct hdlcdrv_state *s) | 
 | 385 | { | 
 | 386 | 	if (!s || s->magic != HDLCDRV_MAGIC || s->hdlctx.ptt || !s->skb)  | 
 | 387 | 		return; | 
 | 388 | 	if (s->ch_params.fulldup) { | 
 | 389 | 		start_tx(dev, s); | 
 | 390 | 		return; | 
 | 391 | 	} | 
 | 392 | 	if (s->hdlcrx.dcd) { | 
 | 393 | 		s->hdlctx.slotcnt = s->ch_params.slottime; | 
 | 394 | 		return; | 
 | 395 | 	} | 
 | 396 | 	if ((--s->hdlctx.slotcnt) > 0) | 
 | 397 | 		return; | 
 | 398 | 	s->hdlctx.slotcnt = s->ch_params.slottime; | 
 | 399 | 	if ((random_num() % 256) > s->ch_params.ppersist) | 
 | 400 | 		return; | 
 | 401 | 	start_tx(dev, s); | 
 | 402 | } | 
 | 403 |  | 
 | 404 | /* --------------------------------------------------------------------- */ | 
 | 405 | /* | 
 | 406 |  * ===================== network driver interface ========================= | 
 | 407 |  */ | 
 | 408 |  | 
 | 409 | static int hdlcdrv_send_packet(struct sk_buff *skb, struct net_device *dev) | 
 | 410 | { | 
 | 411 | 	struct hdlcdrv_state *sm = netdev_priv(dev); | 
 | 412 |  | 
 | 413 | 	if (skb->data[0] != 0) { | 
 | 414 | 		do_kiss_params(sm, skb->data, skb->len); | 
 | 415 | 		dev_kfree_skb(skb); | 
 | 416 | 		return 0; | 
 | 417 | 	} | 
 | 418 | 	if (sm->skb) | 
 | 419 | 		return -1; | 
 | 420 | 	netif_stop_queue(dev); | 
 | 421 | 	sm->skb = skb; | 
 | 422 | 	return 0; | 
 | 423 | } | 
 | 424 |  | 
 | 425 | /* --------------------------------------------------------------------- */ | 
 | 426 |  | 
 | 427 | static int hdlcdrv_set_mac_address(struct net_device *dev, void *addr) | 
 | 428 | { | 
 | 429 | 	struct sockaddr *sa = (struct sockaddr *)addr; | 
 | 430 |  | 
 | 431 | 	/* addr is an AX.25 shifted ASCII mac address */ | 
 | 432 | 	memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);  | 
 | 433 | 	return 0;                                          | 
 | 434 | } | 
 | 435 |  | 
 | 436 | /* --------------------------------------------------------------------- */ | 
 | 437 |  | 
 | 438 | static struct net_device_stats *hdlcdrv_get_stats(struct net_device *dev) | 
 | 439 | { | 
 | 440 | 	struct hdlcdrv_state *sm = netdev_priv(dev); | 
 | 441 |  | 
 | 442 | 	/*  | 
 | 443 | 	 * Get the current statistics.  This may be called with the | 
 | 444 | 	 * card open or closed.  | 
 | 445 | 	 */ | 
 | 446 | 	return &sm->stats; | 
 | 447 | } | 
 | 448 |  | 
 | 449 | /* --------------------------------------------------------------------- */ | 
 | 450 | /* | 
 | 451 |  * Open/initialize the board. This is called (in the current kernel) | 
 | 452 |  * sometime after booting when the 'ifconfig' program is run. | 
 | 453 |  * | 
 | 454 |  * This routine should set everything up anew at each open, even | 
 | 455 |  * registers that "should" only need to be set once at boot, so that | 
 | 456 |  * there is non-reboot way to recover if something goes wrong. | 
 | 457 |  */ | 
 | 458 |  | 
 | 459 | static int hdlcdrv_open(struct net_device *dev) | 
 | 460 | { | 
 | 461 | 	struct hdlcdrv_state *s = netdev_priv(dev); | 
 | 462 | 	int i; | 
 | 463 |  | 
 | 464 | 	if (!s->ops || !s->ops->open) | 
 | 465 | 		return -ENODEV; | 
 | 466 |  | 
 | 467 | 	/* | 
 | 468 | 	 * initialise some variables | 
 | 469 | 	 */ | 
 | 470 | 	s->opened = 1; | 
 | 471 | 	s->hdlcrx.hbuf.rd = s->hdlcrx.hbuf.wr = 0; | 
 | 472 | 	s->hdlcrx.in_hdlc_rx = 0; | 
 | 473 | 	s->hdlcrx.rx_state = 0; | 
 | 474 | 	 | 
 | 475 | 	s->hdlctx.hbuf.rd = s->hdlctx.hbuf.wr = 0; | 
 | 476 | 	s->hdlctx.in_hdlc_tx = 0; | 
 | 477 | 	s->hdlctx.tx_state = 1; | 
 | 478 | 	s->hdlctx.numflags = 0; | 
 | 479 | 	s->hdlctx.bitstream = s->hdlctx.bitbuf = s->hdlctx.numbits = 0; | 
 | 480 | 	s->hdlctx.ptt = 0; | 
 | 481 | 	s->hdlctx.slotcnt = s->ch_params.slottime; | 
 | 482 | 	s->hdlctx.calibrate = 0; | 
 | 483 |  | 
 | 484 | 	i = s->ops->open(dev); | 
 | 485 | 	if (i) | 
 | 486 | 		return i; | 
 | 487 | 	netif_start_queue(dev); | 
 | 488 | 	return 0; | 
 | 489 | } | 
 | 490 |  | 
 | 491 | /* --------------------------------------------------------------------- */ | 
 | 492 | /*  | 
 | 493 |  * The inverse routine to hdlcdrv_open().  | 
 | 494 |  */ | 
 | 495 |  | 
 | 496 | static int hdlcdrv_close(struct net_device *dev) | 
 | 497 | { | 
 | 498 | 	struct hdlcdrv_state *s = netdev_priv(dev); | 
 | 499 | 	int i = 0; | 
 | 500 |  | 
 | 501 | 	netif_stop_queue(dev); | 
 | 502 |  | 
 | 503 | 	if (s->ops && s->ops->close) | 
 | 504 | 		i = s->ops->close(dev); | 
 | 505 | 	if (s->skb) | 
 | 506 | 		dev_kfree_skb(s->skb); | 
 | 507 | 	s->skb = NULL; | 
 | 508 | 	s->opened = 0; | 
 | 509 | 	return i; | 
 | 510 | } | 
 | 511 |  | 
 | 512 | /* --------------------------------------------------------------------- */ | 
 | 513 |  | 
 | 514 | static int hdlcdrv_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) | 
 | 515 | { | 
 | 516 | 	struct hdlcdrv_state *s = netdev_priv(dev); | 
 | 517 | 	struct hdlcdrv_ioctl bi; | 
 | 518 |  | 
 | 519 | 	if (cmd != SIOCDEVPRIVATE) { | 
 | 520 | 		if (s->ops && s->ops->ioctl) | 
 | 521 | 			return s->ops->ioctl(dev, ifr, &bi, cmd); | 
 | 522 | 		return -ENOIOCTLCMD; | 
 | 523 | 	} | 
 | 524 | 	if (copy_from_user(&bi, ifr->ifr_data, sizeof(bi))) | 
 | 525 | 		return -EFAULT; | 
 | 526 |  | 
 | 527 | 	switch (bi.cmd) { | 
 | 528 | 	default: | 
 | 529 | 		if (s->ops && s->ops->ioctl) | 
 | 530 | 			return s->ops->ioctl(dev, ifr, &bi, cmd); | 
 | 531 | 		return -ENOIOCTLCMD; | 
 | 532 |  | 
 | 533 | 	case HDLCDRVCTL_GETCHANNELPAR: | 
 | 534 | 		bi.data.cp.tx_delay = s->ch_params.tx_delay; | 
 | 535 | 		bi.data.cp.tx_tail = s->ch_params.tx_tail; | 
 | 536 | 		bi.data.cp.slottime = s->ch_params.slottime; | 
 | 537 | 		bi.data.cp.ppersist = s->ch_params.ppersist; | 
 | 538 | 		bi.data.cp.fulldup = s->ch_params.fulldup; | 
 | 539 | 		break; | 
 | 540 |  | 
 | 541 | 	case HDLCDRVCTL_SETCHANNELPAR: | 
 | 542 | 		if (!capable(CAP_NET_ADMIN)) | 
 | 543 | 			return -EACCES; | 
 | 544 | 		s->ch_params.tx_delay = bi.data.cp.tx_delay; | 
 | 545 | 		s->ch_params.tx_tail = bi.data.cp.tx_tail; | 
 | 546 | 		s->ch_params.slottime = bi.data.cp.slottime; | 
 | 547 | 		s->ch_params.ppersist = bi.data.cp.ppersist; | 
 | 548 | 		s->ch_params.fulldup = bi.data.cp.fulldup; | 
 | 549 | 		s->hdlctx.slotcnt = 1; | 
 | 550 | 		return 0; | 
 | 551 | 		 | 
 | 552 | 	case HDLCDRVCTL_GETMODEMPAR: | 
 | 553 | 		bi.data.mp.iobase = dev->base_addr; | 
 | 554 | 		bi.data.mp.irq = dev->irq; | 
 | 555 | 		bi.data.mp.dma = dev->dma; | 
 | 556 | 		bi.data.mp.dma2 = s->ptt_out.dma2; | 
 | 557 | 		bi.data.mp.seriobase = s->ptt_out.seriobase; | 
 | 558 | 		bi.data.mp.pariobase = s->ptt_out.pariobase; | 
 | 559 | 		bi.data.mp.midiiobase = s->ptt_out.midiiobase; | 
 | 560 | 		break; | 
 | 561 |  | 
 | 562 | 	case HDLCDRVCTL_SETMODEMPAR: | 
 | 563 | 		if ((!capable(CAP_SYS_RAWIO)) || netif_running(dev)) | 
 | 564 | 			return -EACCES; | 
 | 565 | 		dev->base_addr = bi.data.mp.iobase; | 
 | 566 | 		dev->irq = bi.data.mp.irq; | 
 | 567 | 		dev->dma = bi.data.mp.dma; | 
 | 568 | 		s->ptt_out.dma2 = bi.data.mp.dma2; | 
 | 569 | 		s->ptt_out.seriobase = bi.data.mp.seriobase; | 
 | 570 | 		s->ptt_out.pariobase = bi.data.mp.pariobase; | 
 | 571 | 		s->ptt_out.midiiobase = bi.data.mp.midiiobase; | 
 | 572 | 		return 0;	 | 
 | 573 | 	 | 
 | 574 | 	case HDLCDRVCTL_GETSTAT: | 
 | 575 | 		bi.data.cs.ptt = hdlcdrv_ptt(s); | 
 | 576 | 		bi.data.cs.dcd = s->hdlcrx.dcd; | 
 | 577 | 		bi.data.cs.ptt_keyed = s->ptt_keyed; | 
 | 578 | 		bi.data.cs.tx_packets = s->stats.tx_packets; | 
 | 579 | 		bi.data.cs.tx_errors = s->stats.tx_errors; | 
 | 580 | 		bi.data.cs.rx_packets = s->stats.rx_packets; | 
 | 581 | 		bi.data.cs.rx_errors = s->stats.rx_errors; | 
 | 582 | 		break;		 | 
 | 583 |  | 
 | 584 | 	case HDLCDRVCTL_OLDGETSTAT: | 
 | 585 | 		bi.data.ocs.ptt = hdlcdrv_ptt(s); | 
 | 586 | 		bi.data.ocs.dcd = s->hdlcrx.dcd; | 
 | 587 | 		bi.data.ocs.ptt_keyed = s->ptt_keyed; | 
 | 588 | 		break;		 | 
 | 589 |  | 
 | 590 | 	case HDLCDRVCTL_CALIBRATE: | 
 | 591 | 		if(!capable(CAP_SYS_RAWIO)) | 
 | 592 | 			return -EPERM; | 
 | 593 | 		s->hdlctx.calibrate = bi.data.calibrate * s->par.bitrate / 16; | 
 | 594 | 		return 0; | 
 | 595 |  | 
 | 596 | 	case HDLCDRVCTL_GETSAMPLES: | 
 | 597 | #ifndef HDLCDRV_DEBUG | 
 | 598 | 		return -EPERM; | 
 | 599 | #else /* HDLCDRV_DEBUG */ | 
 | 600 | 		if (s->bitbuf_channel.rd == s->bitbuf_channel.wr)  | 
 | 601 | 			return -EAGAIN; | 
 | 602 | 		bi.data.bits =  | 
 | 603 | 			s->bitbuf_channel.buffer[s->bitbuf_channel.rd]; | 
 | 604 | 		s->bitbuf_channel.rd = (s->bitbuf_channel.rd+1) % | 
 | 605 | 			sizeof(s->bitbuf_channel.buffer); | 
 | 606 | 		break; | 
 | 607 | #endif /* HDLCDRV_DEBUG */ | 
 | 608 | 				 | 
 | 609 | 	case HDLCDRVCTL_GETBITS: | 
 | 610 | #ifndef HDLCDRV_DEBUG | 
 | 611 | 		return -EPERM; | 
 | 612 | #else /* HDLCDRV_DEBUG */ | 
 | 613 | 		if (s->bitbuf_hdlc.rd == s->bitbuf_hdlc.wr)  | 
 | 614 | 			return -EAGAIN; | 
 | 615 | 		bi.data.bits =  | 
 | 616 | 			s->bitbuf_hdlc.buffer[s->bitbuf_hdlc.rd]; | 
 | 617 | 		s->bitbuf_hdlc.rd = (s->bitbuf_hdlc.rd+1) % | 
 | 618 | 			sizeof(s->bitbuf_hdlc.buffer); | 
 | 619 | 		break;		 | 
 | 620 | #endif /* HDLCDRV_DEBUG */ | 
 | 621 |  | 
 | 622 | 	case HDLCDRVCTL_DRIVERNAME: | 
 | 623 | 		if (s->ops && s->ops->drvname) { | 
 | 624 | 			strncpy(bi.data.drivername, s->ops->drvname,  | 
 | 625 | 				sizeof(bi.data.drivername)); | 
 | 626 | 			break; | 
 | 627 | 		} | 
 | 628 | 		bi.data.drivername[0] = '\0'; | 
 | 629 | 		break; | 
 | 630 | 		 | 
 | 631 | 	} | 
 | 632 | 	if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi))) | 
 | 633 | 		return -EFAULT; | 
 | 634 | 	return 0; | 
 | 635 |  | 
 | 636 | } | 
 | 637 |  | 
 | 638 | /* --------------------------------------------------------------------- */ | 
 | 639 |  | 
 | 640 | /* | 
 | 641 |  * Initialize fields in hdlcdrv | 
 | 642 |  */ | 
 | 643 | static void hdlcdrv_setup(struct net_device *dev) | 
 | 644 | { | 
 | 645 | 	static const struct hdlcdrv_channel_params dflt_ch_params = {  | 
 | 646 | 		20, 2, 10, 40, 0  | 
 | 647 | 	}; | 
 | 648 | 	struct hdlcdrv_state *s = netdev_priv(dev); | 
 | 649 |  | 
 | 650 | 	/* | 
 | 651 | 	 * initialize the hdlcdrv_state struct | 
 | 652 | 	 */ | 
 | 653 | 	s->ch_params = dflt_ch_params; | 
 | 654 | 	s->ptt_keyed = 0; | 
 | 655 |  | 
 | 656 | 	spin_lock_init(&s->hdlcrx.hbuf.lock); | 
 | 657 | 	s->hdlcrx.hbuf.rd = s->hdlcrx.hbuf.wr = 0; | 
 | 658 | 	s->hdlcrx.in_hdlc_rx = 0; | 
 | 659 | 	s->hdlcrx.rx_state = 0; | 
 | 660 | 	 | 
 | 661 | 	spin_lock_init(&s->hdlctx.hbuf.lock); | 
 | 662 | 	s->hdlctx.hbuf.rd = s->hdlctx.hbuf.wr = 0; | 
 | 663 | 	s->hdlctx.in_hdlc_tx = 0; | 
 | 664 | 	s->hdlctx.tx_state = 1; | 
 | 665 | 	s->hdlctx.numflags = 0; | 
 | 666 | 	s->hdlctx.bitstream = s->hdlctx.bitbuf = s->hdlctx.numbits = 0; | 
 | 667 | 	s->hdlctx.ptt = 0; | 
 | 668 | 	s->hdlctx.slotcnt = s->ch_params.slottime; | 
 | 669 | 	s->hdlctx.calibrate = 0; | 
 | 670 |  | 
 | 671 | #ifdef HDLCDRV_DEBUG | 
 | 672 | 	s->bitbuf_channel.rd = s->bitbuf_channel.wr = 0; | 
 | 673 | 	s->bitbuf_channel.shreg = 0x80; | 
 | 674 |  | 
 | 675 | 	s->bitbuf_hdlc.rd = s->bitbuf_hdlc.wr = 0; | 
 | 676 | 	s->bitbuf_hdlc.shreg = 0x80; | 
 | 677 | #endif /* HDLCDRV_DEBUG */ | 
 | 678 |  | 
 | 679 | 	/* | 
 | 680 | 	 * initialize the device struct | 
 | 681 | 	 */ | 
 | 682 | 	dev->open = hdlcdrv_open; | 
 | 683 | 	dev->stop = hdlcdrv_close; | 
 | 684 | 	dev->do_ioctl = hdlcdrv_ioctl; | 
 | 685 | 	dev->hard_start_xmit = hdlcdrv_send_packet; | 
 | 686 | 	dev->get_stats = hdlcdrv_get_stats; | 
 | 687 |  | 
 | 688 | 	/* Fill in the fields of the device structure */ | 
 | 689 |  | 
 | 690 | 	s->skb = NULL; | 
 | 691 | 	 | 
| Ralf Baechle | 6f74998 | 2005-09-12 14:21:01 -0700 | [diff] [blame] | 692 | 	dev->hard_header = ax25_hard_header; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | 	dev->rebuild_header = ax25_rebuild_header; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | 	dev->set_mac_address = hdlcdrv_set_mac_address; | 
 | 695 | 	 | 
 | 696 | 	dev->type = ARPHRD_AX25;           /* AF_AX25 device */ | 
 | 697 | 	dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN; | 
 | 698 | 	dev->mtu = AX25_DEF_PACLEN;        /* eth_mtu is the default */ | 
 | 699 | 	dev->addr_len = AX25_ADDR_LEN;     /* sizeof an ax.25 address */ | 
| Ralf Baechle | 15b1c0e | 2006-12-07 15:47:08 -0800 | [diff] [blame] | 700 | 	memcpy(dev->broadcast, &ax25_bcast, AX25_ADDR_LEN); | 
 | 701 | 	memcpy(dev->dev_addr, &ax25_defaddr, AX25_ADDR_LEN); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | 	dev->tx_queue_len = 16; | 
 | 703 | } | 
 | 704 |  | 
 | 705 | /* --------------------------------------------------------------------- */ | 
 | 706 | struct net_device *hdlcdrv_register(const struct hdlcdrv_ops *ops, | 
 | 707 | 				    unsigned int privsize, const char *ifname, | 
 | 708 | 				    unsigned int baseaddr, unsigned int irq,  | 
 | 709 | 				    unsigned int dma)  | 
 | 710 | { | 
 | 711 | 	struct net_device *dev; | 
 | 712 | 	struct hdlcdrv_state *s; | 
 | 713 | 	int err; | 
 | 714 |  | 
 | 715 | 	BUG_ON(ops == NULL); | 
 | 716 |  | 
 | 717 | 	if (privsize < sizeof(struct hdlcdrv_state)) | 
 | 718 | 		privsize = sizeof(struct hdlcdrv_state); | 
 | 719 |  | 
 | 720 | 	dev = alloc_netdev(privsize, ifname, hdlcdrv_setup); | 
 | 721 | 	if (!dev) | 
 | 722 | 		return ERR_PTR(-ENOMEM); | 
 | 723 |  | 
 | 724 | 	/* | 
 | 725 | 	 * initialize part of the hdlcdrv_state struct | 
 | 726 | 	 */ | 
 | 727 | 	s = netdev_priv(dev); | 
 | 728 | 	s->magic = HDLCDRV_MAGIC; | 
 | 729 | 	s->ops = ops; | 
 | 730 | 	dev->base_addr = baseaddr; | 
 | 731 | 	dev->irq = irq; | 
 | 732 | 	dev->dma = dma; | 
 | 733 |  | 
 | 734 | 	err = register_netdev(dev); | 
 | 735 | 	if (err < 0) { | 
 | 736 | 		printk(KERN_WARNING "hdlcdrv: cannot register net " | 
 | 737 | 		       "device %s\n", dev->name); | 
 | 738 | 		free_netdev(dev); | 
 | 739 | 		dev = ERR_PTR(err); | 
 | 740 | 	} | 
 | 741 | 	return dev; | 
 | 742 | } | 
 | 743 |  | 
 | 744 | /* --------------------------------------------------------------------- */ | 
 | 745 |  | 
 | 746 | void hdlcdrv_unregister(struct net_device *dev)  | 
 | 747 | { | 
 | 748 | 	struct hdlcdrv_state *s = netdev_priv(dev); | 
 | 749 |  | 
 | 750 | 	BUG_ON(s->magic != HDLCDRV_MAGIC); | 
 | 751 |  | 
 | 752 | 	if (s->opened && s->ops->close) | 
 | 753 | 		s->ops->close(dev); | 
 | 754 | 	unregister_netdev(dev); | 
 | 755 | 	 | 
 | 756 | 	free_netdev(dev); | 
 | 757 | } | 
 | 758 |  | 
 | 759 | /* --------------------------------------------------------------------- */ | 
 | 760 |  | 
 | 761 | EXPORT_SYMBOL(hdlcdrv_receiver); | 
 | 762 | EXPORT_SYMBOL(hdlcdrv_transmitter); | 
 | 763 | EXPORT_SYMBOL(hdlcdrv_arbitrate); | 
 | 764 | EXPORT_SYMBOL(hdlcdrv_register); | 
 | 765 | EXPORT_SYMBOL(hdlcdrv_unregister); | 
 | 766 |  | 
 | 767 | /* --------------------------------------------------------------------- */ | 
 | 768 |  | 
 | 769 | static int __init hdlcdrv_init_driver(void) | 
 | 770 | { | 
 | 771 | 	printk(KERN_INFO "hdlcdrv: (C) 1996-2000 Thomas Sailer HB9JNX/AE4WA\n"); | 
 | 772 | 	printk(KERN_INFO "hdlcdrv: version 0.8 compiled " __TIME__ " " __DATE__ "\n"); | 
 | 773 | 	return 0; | 
 | 774 | } | 
 | 775 |  | 
 | 776 | /* --------------------------------------------------------------------- */ | 
 | 777 |  | 
 | 778 | static void __exit hdlcdrv_cleanup_driver(void) | 
 | 779 | { | 
 | 780 | 	printk(KERN_INFO "hdlcdrv: cleanup\n"); | 
 | 781 | } | 
 | 782 |  | 
 | 783 | /* --------------------------------------------------------------------- */ | 
 | 784 |  | 
 | 785 | MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu"); | 
 | 786 | MODULE_DESCRIPTION("Packet Radio network interface HDLC encoder/decoder"); | 
 | 787 | MODULE_LICENSE("GPL"); | 
 | 788 | module_init(hdlcdrv_init_driver); | 
 | 789 | module_exit(hdlcdrv_cleanup_driver); | 
 | 790 |  | 
 | 791 | /* --------------------------------------------------------------------- */ |