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