initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / net / hamradio / hdlcdrv.c
blob86deb52100be1c383f515d67dbc9a3a30cdc5aaf
1 /*****************************************************************************/
3 /*
4 * hdlcdrv.c -- HDLC packet radio network driver.
6 * Copyright (C) 1996-2000 Thomas Sailer (sailer@ife.ee.ethz.ch)
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.
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.
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.
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.
26 * The driver was derived from Donald Beckers skeleton.c
27 * Written 1993-94 by Donald Becker.
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
43 /*****************************************************************************/
45 #include <linux/config.h>
46 #include <linux/module.h>
47 #include <linux/types.h>
48 #include <linux/net.h>
49 #include <linux/in.h>
50 #include <linux/if.h>
51 #include <linux/slab.h>
52 #include <linux/errno.h>
53 #include <linux/init.h>
54 #include <asm/bitops.h>
55 #include <asm/uaccess.h>
57 #include <linux/netdevice.h>
58 #include <linux/if_arp.h>
59 #include <linux/etherdevice.h>
60 #include <linux/skbuff.h>
61 #include <linux/hdlcdrv.h>
62 /* prototypes for ax25_encapsulate and ax25_rebuild_header */
63 #include <net/ax25.h>
65 /* make genksyms happy */
66 #include <linux/ip.h>
67 #include <linux/udp.h>
68 #include <linux/tcp.h>
69 #include <linux/crc-ccitt.h>
71 /* --------------------------------------------------------------------- */
74 * The name of the card. Is used for messages and in the requests for
75 * io regions, irqs and dma channels
78 static char ax25_bcast[AX25_ADDR_LEN] =
79 {'Q' << 1, 'S' << 1, 'T' << 1, ' ' << 1, ' ' << 1, ' ' << 1, '0' << 1};
80 static char ax25_nocall[AX25_ADDR_LEN] =
81 {'L' << 1, 'I' << 1, 'N' << 1, 'U' << 1, 'X' << 1, ' ' << 1, '1' << 1};
83 /* --------------------------------------------------------------------- */
85 #define KISS_VERBOSE
87 /* --------------------------------------------------------------------- */
89 #define PARAM_TXDELAY 1
90 #define PARAM_PERSIST 2
91 #define PARAM_SLOTTIME 3
92 #define PARAM_TXTAIL 4
93 #define PARAM_FULLDUP 5
94 #define PARAM_HARDWARE 6
95 #define PARAM_RETURN 255
97 /* --------------------------------------------------------------------- */
99 * the CRC routines are stolen from WAMPES
100 * by Dieter Deyke
104 /*---------------------------------------------------------------------------*/
106 static inline void append_crc_ccitt(unsigned char *buffer, int len)
108 unsigned int crc = crc_ccitt(0xffff, buffer, len) ^ 0xffff;
109 *buffer++ = crc;
110 *buffer++ = crc >> 8;
113 /*---------------------------------------------------------------------------*/
115 static inline int check_crc_ccitt(const unsigned char *buf, int cnt)
117 return (crc_ccitt(0xffff, buf, cnt) & 0xffff) == 0xf0b8;
120 /*---------------------------------------------------------------------------*/
122 #if 0
123 static int calc_crc_ccitt(const unsigned char *buf, int cnt)
125 unsigned int crc = 0xffff;
127 for (; cnt > 0; cnt--)
128 crc = (crc >> 8) ^ crc_ccitt_table[(crc ^ *buf++) & 0xff];
129 crc ^= 0xffff;
130 return (crc & 0xffff);
132 #endif
134 /* ---------------------------------------------------------------------- */
136 #define tenms_to_2flags(s,tenms) ((tenms * s->par.bitrate) / 100 / 16)
138 /* ---------------------------------------------------------------------- */
140 * The HDLC routines
143 static int hdlc_rx_add_bytes(struct hdlcdrv_state *s, unsigned int bits,
144 int num)
146 int added = 0;
148 while (s->hdlcrx.rx_state && num >= 8) {
149 if (s->hdlcrx.len >= sizeof(s->hdlcrx.buffer)) {
150 s->hdlcrx.rx_state = 0;
151 return 0;
153 *s->hdlcrx.bp++ = bits >> (32-num);
154 s->hdlcrx.len++;
155 num -= 8;
156 added += 8;
158 return added;
161 static void hdlc_rx_flag(struct net_device *dev, struct hdlcdrv_state *s)
163 struct sk_buff *skb;
164 int pkt_len;
165 unsigned char *cp;
167 if (s->hdlcrx.len < 4)
168 return;
169 if (!check_crc_ccitt(s->hdlcrx.buffer, s->hdlcrx.len))
170 return;
171 pkt_len = s->hdlcrx.len - 2 + 1; /* KISS kludge */
172 if (!(skb = dev_alloc_skb(pkt_len))) {
173 printk("%s: memory squeeze, dropping packet\n", dev->name);
174 s->stats.rx_dropped++;
175 return;
177 skb->dev = dev;
178 cp = skb_put(skb, pkt_len);
179 *cp++ = 0; /* KISS kludge */
180 memcpy(cp, s->hdlcrx.buffer, pkt_len - 1);
181 skb->protocol = htons(ETH_P_AX25);
182 skb->mac.raw = skb->data;
183 netif_rx(skb);
184 dev->last_rx = jiffies;
185 s->stats.rx_packets++;
188 void hdlcdrv_receiver(struct net_device *dev, struct hdlcdrv_state *s)
190 int i;
191 unsigned int mask1, mask2, mask3, mask4, mask5, mask6, word;
193 if (!s || s->magic != HDLCDRV_MAGIC)
194 return;
195 if (test_and_set_bit(0, &s->hdlcrx.in_hdlc_rx))
196 return;
198 while (!hdlcdrv_hbuf_empty(&s->hdlcrx.hbuf)) {
199 word = hdlcdrv_hbuf_get(&s->hdlcrx.hbuf);
201 #ifdef HDLCDRV_DEBUG
202 hdlcdrv_add_bitbuffer_word(&s->bitbuf_hdlc, word);
203 #endif /* HDLCDRV_DEBUG */
204 s->hdlcrx.bitstream >>= 16;
205 s->hdlcrx.bitstream |= word << 16;
206 s->hdlcrx.bitbuf >>= 16;
207 s->hdlcrx.bitbuf |= word << 16;
208 s->hdlcrx.numbits += 16;
209 for(i = 15, mask1 = 0x1fc00, mask2 = 0x1fe00, mask3 = 0x0fc00,
210 mask4 = 0x1f800, mask5 = 0xf800, mask6 = 0xffff;
211 i >= 0;
212 i--, mask1 <<= 1, mask2 <<= 1, mask3 <<= 1, mask4 <<= 1,
213 mask5 <<= 1, mask6 = (mask6 << 1) | 1) {
214 if ((s->hdlcrx.bitstream & mask1) == mask1)
215 s->hdlcrx.rx_state = 0; /* abort received */
216 else if ((s->hdlcrx.bitstream & mask2) == mask3) {
217 /* flag received */
218 if (s->hdlcrx.rx_state) {
219 hdlc_rx_add_bytes(s, s->hdlcrx.bitbuf
220 << (8+i),
221 s->hdlcrx.numbits
222 -8-i);
223 hdlc_rx_flag(dev, s);
225 s->hdlcrx.len = 0;
226 s->hdlcrx.bp = s->hdlcrx.buffer;
227 s->hdlcrx.rx_state = 1;
228 s->hdlcrx.numbits = i;
229 } else if ((s->hdlcrx.bitstream & mask4) == mask5) {
230 /* stuffed bit */
231 s->hdlcrx.numbits--;
232 s->hdlcrx.bitbuf = (s->hdlcrx.bitbuf & (~mask6)) |
233 ((s->hdlcrx.bitbuf & mask6) << 1);
236 s->hdlcrx.numbits -= hdlc_rx_add_bytes(s, s->hdlcrx.bitbuf,
237 s->hdlcrx.numbits);
239 clear_bit(0, &s->hdlcrx.in_hdlc_rx);
242 /* ---------------------------------------------------------------------- */
244 static inline void do_kiss_params(struct hdlcdrv_state *s,
245 unsigned char *data, unsigned long len)
248 #ifdef KISS_VERBOSE
249 #define PKP(a,b) printk(KERN_INFO "hdlcdrv.c: channel params: " a "\n", b)
250 #else /* KISS_VERBOSE */
251 #define PKP(a,b)
252 #endif /* KISS_VERBOSE */
254 if (len < 2)
255 return;
256 switch(data[0]) {
257 case PARAM_TXDELAY:
258 s->ch_params.tx_delay = data[1];
259 PKP("TX delay = %ums", 10 * s->ch_params.tx_delay);
260 break;
261 case PARAM_PERSIST:
262 s->ch_params.ppersist = data[1];
263 PKP("p persistence = %u", s->ch_params.ppersist);
264 break;
265 case PARAM_SLOTTIME:
266 s->ch_params.slottime = data[1];
267 PKP("slot time = %ums", s->ch_params.slottime);
268 break;
269 case PARAM_TXTAIL:
270 s->ch_params.tx_tail = data[1];
271 PKP("TX tail = %ums", s->ch_params.tx_tail);
272 break;
273 case PARAM_FULLDUP:
274 s->ch_params.fulldup = !!data[1];
275 PKP("%s duplex", s->ch_params.fulldup ? "full" : "half");
276 break;
277 default:
278 break;
280 #undef PKP
283 /* ---------------------------------------------------------------------- */
285 void hdlcdrv_transmitter(struct net_device *dev, struct hdlcdrv_state *s)
287 unsigned int mask1, mask2, mask3;
288 int i;
289 struct sk_buff *skb;
290 int pkt_len;
292 if (!s || s->magic != HDLCDRV_MAGIC)
293 return;
294 if (test_and_set_bit(0, &s->hdlctx.in_hdlc_tx))
295 return;
296 for (;;) {
297 if (s->hdlctx.numbits >= 16) {
298 if (hdlcdrv_hbuf_full(&s->hdlctx.hbuf)) {
299 clear_bit(0, &s->hdlctx.in_hdlc_tx);
300 return;
302 hdlcdrv_hbuf_put(&s->hdlctx.hbuf, s->hdlctx.bitbuf);
303 s->hdlctx.bitbuf >>= 16;
304 s->hdlctx.numbits -= 16;
306 switch (s->hdlctx.tx_state) {
307 default:
308 clear_bit(0, &s->hdlctx.in_hdlc_tx);
309 return;
310 case 0:
311 case 1:
312 if (s->hdlctx.numflags) {
313 s->hdlctx.numflags--;
314 s->hdlctx.bitbuf |=
315 0x7e7e << s->hdlctx.numbits;
316 s->hdlctx.numbits += 16;
317 break;
319 if (s->hdlctx.tx_state == 1) {
320 clear_bit(0, &s->hdlctx.in_hdlc_tx);
321 return;
323 if (!(skb = s->skb)) {
324 int flgs = tenms_to_2flags(s, s->ch_params.tx_tail);
325 if (flgs < 2)
326 flgs = 2;
327 s->hdlctx.tx_state = 1;
328 s->hdlctx.numflags = flgs;
329 break;
331 s->skb = NULL;
332 netif_wake_queue(dev);
333 pkt_len = skb->len-1; /* strip KISS byte */
334 if (pkt_len >= HDLCDRV_MAXFLEN || pkt_len < 2) {
335 s->hdlctx.tx_state = 0;
336 s->hdlctx.numflags = 1;
337 dev_kfree_skb_irq(skb);
338 break;
340 memcpy(s->hdlctx.buffer, skb->data+1, pkt_len);
341 dev_kfree_skb_irq(skb);
342 s->hdlctx.bp = s->hdlctx.buffer;
343 append_crc_ccitt(s->hdlctx.buffer, pkt_len);
344 s->hdlctx.len = pkt_len+2; /* the appended CRC */
345 s->hdlctx.tx_state = 2;
346 s->hdlctx.bitstream = 0;
347 s->stats.tx_packets++;
348 break;
349 case 2:
350 if (!s->hdlctx.len) {
351 s->hdlctx.tx_state = 0;
352 s->hdlctx.numflags = 1;
353 break;
355 s->hdlctx.len--;
356 s->hdlctx.bitbuf |= *s->hdlctx.bp <<
357 s->hdlctx.numbits;
358 s->hdlctx.bitstream >>= 8;
359 s->hdlctx.bitstream |= (*s->hdlctx.bp++) << 16;
360 mask1 = 0x1f000;
361 mask2 = 0x10000;
362 mask3 = 0xffffffff >> (31-s->hdlctx.numbits);
363 s->hdlctx.numbits += 8;
364 for(i = 0; i < 8; i++, mask1 <<= 1, mask2 <<= 1,
365 mask3 = (mask3 << 1) | 1) {
366 if ((s->hdlctx.bitstream & mask1) != mask1)
367 continue;
368 s->hdlctx.bitstream &= ~mask2;
369 s->hdlctx.bitbuf =
370 (s->hdlctx.bitbuf & mask3) |
371 ((s->hdlctx.bitbuf &
372 (~mask3)) << 1);
373 s->hdlctx.numbits++;
374 mask3 = (mask3 << 1) | 1;
376 break;
381 /* ---------------------------------------------------------------------- */
383 static void start_tx(struct net_device *dev, struct hdlcdrv_state *s)
385 s->hdlctx.tx_state = 0;
386 s->hdlctx.numflags = tenms_to_2flags(s, s->ch_params.tx_delay);
387 s->hdlctx.bitbuf = s->hdlctx.bitstream = s->hdlctx.numbits = 0;
388 hdlcdrv_transmitter(dev, s);
389 s->hdlctx.ptt = 1;
390 s->ptt_keyed++;
393 /* ---------------------------------------------------------------------- */
395 static unsigned short random_seed;
397 static inline unsigned short random_num(void)
399 random_seed = 28629 * random_seed + 157;
400 return random_seed;
403 /* ---------------------------------------------------------------------- */
405 void hdlcdrv_arbitrate(struct net_device *dev, struct hdlcdrv_state *s)
407 if (!s || s->magic != HDLCDRV_MAGIC || s->hdlctx.ptt || !s->skb)
408 return;
409 if (s->ch_params.fulldup) {
410 start_tx(dev, s);
411 return;
413 if (s->hdlcrx.dcd) {
414 s->hdlctx.slotcnt = s->ch_params.slottime;
415 return;
417 if ((--s->hdlctx.slotcnt) > 0)
418 return;
419 s->hdlctx.slotcnt = s->ch_params.slottime;
420 if ((random_num() % 256) > s->ch_params.ppersist)
421 return;
422 start_tx(dev, s);
425 /* --------------------------------------------------------------------- */
427 * ===================== network driver interface =========================
430 static inline int hdlcdrv_paranoia_check(struct net_device *dev,
431 const char *routine)
433 if (!dev || !dev->priv ||
434 ((struct hdlcdrv_state *)dev->priv)->magic != HDLCDRV_MAGIC) {
435 printk(KERN_ERR "hdlcdrv: bad magic number for hdlcdrv_state "
436 "struct in routine %s\n", routine);
437 return 1;
439 return 0;
442 /* --------------------------------------------------------------------- */
444 static int hdlcdrv_send_packet(struct sk_buff *skb, struct net_device *dev)
446 struct hdlcdrv_state *sm;
448 if (hdlcdrv_paranoia_check(dev, "hdlcdrv_send_packet"))
449 return 0;
450 sm = (struct hdlcdrv_state *)dev->priv;
451 if (skb->data[0] != 0) {
452 do_kiss_params(sm, skb->data, skb->len);
453 dev_kfree_skb(skb);
454 return 0;
456 if (sm->skb)
457 return -1;
458 netif_stop_queue(dev);
459 sm->skb = skb;
460 return 0;
463 /* --------------------------------------------------------------------- */
465 static int hdlcdrv_set_mac_address(struct net_device *dev, void *addr)
467 struct sockaddr *sa = (struct sockaddr *)addr;
469 /* addr is an AX.25 shifted ASCII mac address */
470 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
471 return 0;
474 /* --------------------------------------------------------------------- */
476 static struct net_device_stats *hdlcdrv_get_stats(struct net_device *dev)
478 struct hdlcdrv_state *sm;
480 if (hdlcdrv_paranoia_check(dev, "hdlcdrv_get_stats"))
481 return NULL;
482 sm = (struct hdlcdrv_state *)dev->priv;
484 * Get the current statistics. This may be called with the
485 * card open or closed.
487 return &sm->stats;
490 /* --------------------------------------------------------------------- */
492 * Open/initialize the board. This is called (in the current kernel)
493 * sometime after booting when the 'ifconfig' program is run.
495 * This routine should set everything up anew at each open, even
496 * registers that "should" only need to be set once at boot, so that
497 * there is non-reboot way to recover if something goes wrong.
500 static int hdlcdrv_open(struct net_device *dev)
502 struct hdlcdrv_state *s;
503 int i;
505 if (hdlcdrv_paranoia_check(dev, "hdlcdrv_open"))
506 return -EINVAL;
507 s = (struct hdlcdrv_state *)dev->priv;
509 if (!s->ops || !s->ops->open)
510 return -ENODEV;
513 * initialise some variables
515 s->opened = 1;
516 s->hdlcrx.hbuf.rd = s->hdlcrx.hbuf.wr = 0;
517 s->hdlcrx.in_hdlc_rx = 0;
518 s->hdlcrx.rx_state = 0;
520 s->hdlctx.hbuf.rd = s->hdlctx.hbuf.wr = 0;
521 s->hdlctx.in_hdlc_tx = 0;
522 s->hdlctx.tx_state = 1;
523 s->hdlctx.numflags = 0;
524 s->hdlctx.bitstream = s->hdlctx.bitbuf = s->hdlctx.numbits = 0;
525 s->hdlctx.ptt = 0;
526 s->hdlctx.slotcnt = s->ch_params.slottime;
527 s->hdlctx.calibrate = 0;
529 i = s->ops->open(dev);
530 if (i)
531 return i;
532 netif_start_queue(dev);
533 return 0;
536 /* --------------------------------------------------------------------- */
538 * The inverse routine to hdlcdrv_open().
541 static int hdlcdrv_close(struct net_device *dev)
543 struct hdlcdrv_state *s;
544 int i = 0;
546 if (hdlcdrv_paranoia_check(dev, "hdlcdrv_close"))
547 return -EINVAL;
548 s = (struct hdlcdrv_state *)dev->priv;
550 netif_stop_queue(dev);
552 if (s->ops && s->ops->close)
553 i = s->ops->close(dev);
554 if (s->skb)
555 dev_kfree_skb(s->skb);
556 s->skb = NULL;
557 s->opened = 0;
558 return i;
561 /* --------------------------------------------------------------------- */
563 static int hdlcdrv_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
565 struct hdlcdrv_state *s;
566 struct hdlcdrv_ioctl bi;
568 if (hdlcdrv_paranoia_check(dev, "hdlcdrv_ioctl"))
569 return -EINVAL;
570 s = (struct hdlcdrv_state *)dev->priv;
572 if (cmd != SIOCDEVPRIVATE) {
573 if (s->ops && s->ops->ioctl)
574 return s->ops->ioctl(dev, ifr, &bi, cmd);
575 return -ENOIOCTLCMD;
577 if (copy_from_user(&bi, ifr->ifr_data, sizeof(bi)))
578 return -EFAULT;
580 switch (bi.cmd) {
581 default:
582 if (s->ops && s->ops->ioctl)
583 return s->ops->ioctl(dev, ifr, &bi, cmd);
584 return -ENOIOCTLCMD;
586 case HDLCDRVCTL_GETCHANNELPAR:
587 bi.data.cp.tx_delay = s->ch_params.tx_delay;
588 bi.data.cp.tx_tail = s->ch_params.tx_tail;
589 bi.data.cp.slottime = s->ch_params.slottime;
590 bi.data.cp.ppersist = s->ch_params.ppersist;
591 bi.data.cp.fulldup = s->ch_params.fulldup;
592 break;
594 case HDLCDRVCTL_SETCHANNELPAR:
595 if (!capable(CAP_NET_ADMIN))
596 return -EACCES;
597 s->ch_params.tx_delay = bi.data.cp.tx_delay;
598 s->ch_params.tx_tail = bi.data.cp.tx_tail;
599 s->ch_params.slottime = bi.data.cp.slottime;
600 s->ch_params.ppersist = bi.data.cp.ppersist;
601 s->ch_params.fulldup = bi.data.cp.fulldup;
602 s->hdlctx.slotcnt = 1;
603 return 0;
605 case HDLCDRVCTL_GETMODEMPAR:
606 bi.data.mp.iobase = dev->base_addr;
607 bi.data.mp.irq = dev->irq;
608 bi.data.mp.dma = dev->dma;
609 bi.data.mp.dma2 = s->ptt_out.dma2;
610 bi.data.mp.seriobase = s->ptt_out.seriobase;
611 bi.data.mp.pariobase = s->ptt_out.pariobase;
612 bi.data.mp.midiiobase = s->ptt_out.midiiobase;
613 break;
615 case HDLCDRVCTL_SETMODEMPAR:
616 if ((!capable(CAP_SYS_RAWIO)) || netif_running(dev))
617 return -EACCES;
618 dev->base_addr = bi.data.mp.iobase;
619 dev->irq = bi.data.mp.irq;
620 dev->dma = bi.data.mp.dma;
621 s->ptt_out.dma2 = bi.data.mp.dma2;
622 s->ptt_out.seriobase = bi.data.mp.seriobase;
623 s->ptt_out.pariobase = bi.data.mp.pariobase;
624 s->ptt_out.midiiobase = bi.data.mp.midiiobase;
625 return 0;
627 case HDLCDRVCTL_GETSTAT:
628 bi.data.cs.ptt = hdlcdrv_ptt(s);
629 bi.data.cs.dcd = s->hdlcrx.dcd;
630 bi.data.cs.ptt_keyed = s->ptt_keyed;
631 bi.data.cs.tx_packets = s->stats.tx_packets;
632 bi.data.cs.tx_errors = s->stats.tx_errors;
633 bi.data.cs.rx_packets = s->stats.rx_packets;
634 bi.data.cs.rx_errors = s->stats.rx_errors;
635 break;
637 case HDLCDRVCTL_OLDGETSTAT:
638 bi.data.ocs.ptt = hdlcdrv_ptt(s);
639 bi.data.ocs.dcd = s->hdlcrx.dcd;
640 bi.data.ocs.ptt_keyed = s->ptt_keyed;
641 break;
643 case HDLCDRVCTL_CALIBRATE:
644 if(!capable(CAP_SYS_RAWIO))
645 return -EPERM;
646 s->hdlctx.calibrate = bi.data.calibrate * s->par.bitrate / 16;
647 return 0;
649 case HDLCDRVCTL_GETSAMPLES:
650 #ifndef HDLCDRV_DEBUG
651 return -EPERM;
652 #else /* HDLCDRV_DEBUG */
653 if (s->bitbuf_channel.rd == s->bitbuf_channel.wr)
654 return -EAGAIN;
655 bi.data.bits =
656 s->bitbuf_channel.buffer[s->bitbuf_channel.rd];
657 s->bitbuf_channel.rd = (s->bitbuf_channel.rd+1) %
658 sizeof(s->bitbuf_channel.buffer);
659 break;
660 #endif /* HDLCDRV_DEBUG */
662 case HDLCDRVCTL_GETBITS:
663 #ifndef HDLCDRV_DEBUG
664 return -EPERM;
665 #else /* HDLCDRV_DEBUG */
666 if (s->bitbuf_hdlc.rd == s->bitbuf_hdlc.wr)
667 return -EAGAIN;
668 bi.data.bits =
669 s->bitbuf_hdlc.buffer[s->bitbuf_hdlc.rd];
670 s->bitbuf_hdlc.rd = (s->bitbuf_hdlc.rd+1) %
671 sizeof(s->bitbuf_hdlc.buffer);
672 break;
673 #endif /* HDLCDRV_DEBUG */
675 case HDLCDRVCTL_DRIVERNAME:
676 if (s->ops && s->ops->drvname) {
677 strncpy(bi.data.drivername, s->ops->drvname,
678 sizeof(bi.data.drivername));
679 break;
681 bi.data.drivername[0] = '\0';
682 break;
685 if (copy_to_user(ifr->ifr_data, &bi, sizeof(bi)))
686 return -EFAULT;
687 return 0;
691 /* --------------------------------------------------------------------- */
694 * Initialize fields in hdlcdrv
696 static void hdlcdrv_setup(struct net_device *dev)
698 static const struct hdlcdrv_channel_params dflt_ch_params = {
699 20, 2, 10, 40, 0
701 struct hdlcdrv_state *s = dev->priv;
704 * initialize the hdlcdrv_state struct
706 s->ch_params = dflt_ch_params;
707 s->ptt_keyed = 0;
709 spin_lock_init(&s->hdlcrx.hbuf.lock);
710 s->hdlcrx.hbuf.rd = s->hdlcrx.hbuf.wr = 0;
711 s->hdlcrx.in_hdlc_rx = 0;
712 s->hdlcrx.rx_state = 0;
714 spin_lock_init(&s->hdlctx.hbuf.lock);
715 s->hdlctx.hbuf.rd = s->hdlctx.hbuf.wr = 0;
716 s->hdlctx.in_hdlc_tx = 0;
717 s->hdlctx.tx_state = 1;
718 s->hdlctx.numflags = 0;
719 s->hdlctx.bitstream = s->hdlctx.bitbuf = s->hdlctx.numbits = 0;
720 s->hdlctx.ptt = 0;
721 s->hdlctx.slotcnt = s->ch_params.slottime;
722 s->hdlctx.calibrate = 0;
724 #ifdef HDLCDRV_DEBUG
725 s->bitbuf_channel.rd = s->bitbuf_channel.wr = 0;
726 s->bitbuf_channel.shreg = 0x80;
728 s->bitbuf_hdlc.rd = s->bitbuf_hdlc.wr = 0;
729 s->bitbuf_hdlc.shreg = 0x80;
730 #endif /* HDLCDRV_DEBUG */
733 * initialize the device struct
735 dev->open = hdlcdrv_open;
736 dev->stop = hdlcdrv_close;
737 dev->do_ioctl = hdlcdrv_ioctl;
738 dev->hard_start_xmit = hdlcdrv_send_packet;
739 dev->get_stats = hdlcdrv_get_stats;
741 /* Fill in the fields of the device structure */
743 s->skb = NULL;
745 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
746 dev->hard_header = ax25_encapsulate;
747 dev->rebuild_header = ax25_rebuild_header;
748 #else /* CONFIG_AX25 || CONFIG_AX25_MODULE */
749 dev->hard_header = NULL;
750 dev->rebuild_header = NULL;
751 #endif /* CONFIG_AX25 || CONFIG_AX25_MODULE */
752 dev->set_mac_address = hdlcdrv_set_mac_address;
754 dev->type = ARPHRD_AX25; /* AF_AX25 device */
755 dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN;
756 dev->mtu = AX25_DEF_PACLEN; /* eth_mtu is the default */
757 dev->addr_len = AX25_ADDR_LEN; /* sizeof an ax.25 address */
758 memcpy(dev->broadcast, ax25_bcast, AX25_ADDR_LEN);
759 memcpy(dev->dev_addr, ax25_nocall, AX25_ADDR_LEN);
760 dev->tx_queue_len = 16;
763 /* --------------------------------------------------------------------- */
764 struct net_device *hdlcdrv_register(const struct hdlcdrv_ops *ops,
765 unsigned int privsize, const char *ifname,
766 unsigned int baseaddr, unsigned int irq,
767 unsigned int dma)
769 struct net_device *dev;
770 struct hdlcdrv_state *s;
771 int err;
773 BUG_ON(ops == NULL);
775 if (privsize < sizeof(struct hdlcdrv_state))
776 privsize = sizeof(struct hdlcdrv_state);
778 dev = alloc_netdev(privsize, ifname, hdlcdrv_setup);
779 if (!dev)
780 return ERR_PTR(-ENOMEM);
783 * initialize part of the hdlcdrv_state struct
785 s = dev->priv;
786 s->magic = HDLCDRV_MAGIC;
787 s->ops = ops;
788 dev->base_addr = baseaddr;
789 dev->irq = irq;
790 dev->dma = dma;
792 err = register_netdev(dev);
793 if (err < 0) {
794 printk(KERN_WARNING "hdlcdrv: cannot register net "
795 "device %s\n", dev->name);
796 free_netdev(dev);
797 dev = ERR_PTR(err);
799 return dev;
802 /* --------------------------------------------------------------------- */
804 void hdlcdrv_unregister(struct net_device *dev)
806 struct hdlcdrv_state *s = dev->priv;
808 BUG_ON(s->magic != HDLCDRV_MAGIC);
810 if (s->opened && s->ops->close)
811 s->ops->close(dev);
812 unregister_netdev(dev);
814 free_netdev(dev);
817 /* --------------------------------------------------------------------- */
819 EXPORT_SYMBOL(hdlcdrv_receiver);
820 EXPORT_SYMBOL(hdlcdrv_transmitter);
821 EXPORT_SYMBOL(hdlcdrv_arbitrate);
822 EXPORT_SYMBOL(hdlcdrv_register);
823 EXPORT_SYMBOL(hdlcdrv_unregister);
825 /* --------------------------------------------------------------------- */
827 static int __init hdlcdrv_init_driver(void)
829 printk(KERN_INFO "hdlcdrv: (C) 1996-2000 Thomas Sailer HB9JNX/AE4WA\n");
830 printk(KERN_INFO "hdlcdrv: version 0.8 compiled " __TIME__ " " __DATE__ "\n");
831 return 0;
834 /* --------------------------------------------------------------------- */
836 static void __exit hdlcdrv_cleanup_driver(void)
838 printk(KERN_INFO "hdlcdrv: cleanup\n");
841 /* --------------------------------------------------------------------- */
843 MODULE_AUTHOR("Thomas M. Sailer, sailer@ife.ee.ethz.ch, hb9jnx@hb9w.che.eu");
844 MODULE_DESCRIPTION("Packet Radio network interface HDLC encoder/decoder");
845 MODULE_LICENSE("GPL");
846 module_init(hdlcdrv_init_driver);
847 module_exit(hdlcdrv_cleanup_driver);
849 /* --------------------------------------------------------------------- */