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