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