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