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