Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 1 | /* |
| 2 | * atusb.c - Driver for the ATUSB IEEE 802.15.4 dongle |
| 3 | * |
| 4 | * Written 2013 by Werner Almesberger <werner@almesberger.net> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation, version 2 |
| 9 | * |
| 10 | * Based on at86rf230.c and spi_atusb.c. |
| 11 | * at86rf230.c is |
| 12 | * Copyright (C) 2009 Siemens AG |
| 13 | * Written by: Dmitry Eremin-Solenikov <dmitry.baryshkov@siemens.com> |
| 14 | * |
| 15 | * spi_atusb.c is |
| 16 | * Copyright (c) 2011 Richard Sharpe <realrichardsharpe@gmail.com> |
| 17 | * Copyright (c) 2011 Stefan Schmidt <stefan@datenfreihafen.org> |
| 18 | * Copyright (c) 2011 Werner Almesberger <werner@almesberger.net> |
| 19 | * |
| 20 | * USB initialization is |
| 21 | * Copyright (c) 2013 Alexander Aring <alex.aring@gmail.com> |
| 22 | */ |
| 23 | |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/jiffies.h> |
| 28 | #include <linux/usb.h> |
| 29 | #include <linux/skbuff.h> |
| 30 | |
| 31 | #include <net/cfg802154.h> |
| 32 | #include <net/mac802154.h> |
| 33 | |
| 34 | #include "at86rf230.h" |
| 35 | #include "atusb.h" |
| 36 | |
| 37 | #define ATUSB_JEDEC_ATMEL 0x1f /* JEDEC manufacturer ID */ |
| 38 | |
| 39 | #define ATUSB_NUM_RX_URBS 4 /* allow for a bit of local latency */ |
| 40 | #define ATUSB_ALLOC_DELAY_MS 100 /* delay after failed allocation */ |
| 41 | #define ATUSB_TX_TIMEOUT_MS 200 /* on the air timeout */ |
| 42 | |
| 43 | struct atusb { |
| 44 | struct ieee802154_hw *hw; |
| 45 | struct usb_device *usb_dev; |
| 46 | int shutdown; /* non-zero if shutting down */ |
| 47 | int err; /* set by first error */ |
| 48 | |
| 49 | /* RX variables */ |
| 50 | struct delayed_work work; /* memory allocations */ |
| 51 | struct usb_anchor idle_urbs; /* URBs waiting to be submitted */ |
| 52 | struct usb_anchor rx_urbs; /* URBs waiting for reception */ |
| 53 | |
| 54 | /* TX variables */ |
| 55 | struct usb_ctrlrequest tx_dr; |
| 56 | struct urb *tx_urb; |
| 57 | struct sk_buff *tx_skb; |
| 58 | uint8_t tx_ack_seq; /* current TX ACK sequence number */ |
| 59 | }; |
| 60 | |
| 61 | /* at86rf230.h defines values as <reg, mask, shift> tuples. We use the more |
| 62 | * traditional style of having registers and or-able values. SR_REG extracts |
| 63 | * the register number. SR_VALUE uses the shift to prepare a value accordingly. |
| 64 | */ |
| 65 | |
| 66 | #define __SR_REG(reg, mask, shift) (reg) |
| 67 | #define SR_REG(sr) __SR_REG(sr) |
| 68 | |
| 69 | #define __SR_VALUE(reg, mask, shift, val) ((val) << (shift)) |
| 70 | #define SR_VALUE(sr, val) __SR_VALUE(sr, (val)) |
| 71 | |
| 72 | /* ----- USB commands without data ----------------------------------------- */ |
| 73 | |
| 74 | /* To reduce the number of error checks in the code, we record the first error |
| 75 | * in atusb->err and reject all subsequent requests until the error is cleared. |
| 76 | */ |
| 77 | |
| 78 | static int atusb_control_msg(struct atusb *atusb, unsigned int pipe, |
| 79 | __u8 request, __u8 requesttype, |
| 80 | __u16 value, __u16 index, |
| 81 | void *data, __u16 size, int timeout) |
| 82 | { |
| 83 | struct usb_device *usb_dev = atusb->usb_dev; |
| 84 | int ret; |
| 85 | |
| 86 | if (atusb->err) |
| 87 | return atusb->err; |
| 88 | |
| 89 | ret = usb_control_msg(usb_dev, pipe, request, requesttype, |
| 90 | value, index, data, size, timeout); |
| 91 | if (ret < 0) { |
| 92 | atusb->err = ret; |
| 93 | dev_err(&usb_dev->dev, |
| 94 | "atusb_control_msg: req 0x%02x val 0x%x idx 0x%x, error %d\n", |
| 95 | request, value, index, ret); |
| 96 | } |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | static int atusb_command(struct atusb *atusb, uint8_t cmd, uint8_t arg) |
| 101 | { |
| 102 | struct usb_device *usb_dev = atusb->usb_dev; |
| 103 | |
| 104 | dev_dbg(&usb_dev->dev, "atusb_command: cmd = 0x%x\n", cmd); |
| 105 | return atusb_control_msg(atusb, usb_sndctrlpipe(usb_dev, 0), |
| 106 | cmd, ATUSB_REQ_TO_DEV, arg, 0, NULL, 0, 1000); |
| 107 | } |
| 108 | |
| 109 | static int atusb_write_reg(struct atusb *atusb, uint8_t reg, uint8_t value) |
| 110 | { |
| 111 | struct usb_device *usb_dev = atusb->usb_dev; |
| 112 | |
| 113 | dev_dbg(&usb_dev->dev, "atusb_write_reg: 0x%02x <- 0x%02x\n", |
| 114 | reg, value); |
| 115 | return atusb_control_msg(atusb, usb_sndctrlpipe(usb_dev, 0), |
| 116 | ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV, |
| 117 | value, reg, NULL, 0, 1000); |
| 118 | } |
| 119 | |
| 120 | static int atusb_read_reg(struct atusb *atusb, uint8_t reg) |
| 121 | { |
| 122 | struct usb_device *usb_dev = atusb->usb_dev; |
| 123 | int ret; |
| 124 | uint8_t value; |
| 125 | |
| 126 | dev_dbg(&usb_dev->dev, "atusb: reg = 0x%x\n", reg); |
| 127 | ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0), |
| 128 | ATUSB_REG_READ, ATUSB_REQ_FROM_DEV, |
| 129 | 0, reg, &value, 1, 1000); |
| 130 | return ret >= 0 ? value : ret; |
| 131 | } |
| 132 | |
| 133 | static int atusb_get_and_clear_error(struct atusb *atusb) |
| 134 | { |
| 135 | int err = atusb->err; |
| 136 | |
| 137 | atusb->err = 0; |
| 138 | return err; |
| 139 | } |
| 140 | |
| 141 | /* ----- skb allocation ---------------------------------------------------- */ |
| 142 | |
| 143 | #define MAX_PSDU 127 |
| 144 | #define MAX_RX_XFER (1 + MAX_PSDU + 2 + 1) /* PHR+PSDU+CRC+LQI */ |
| 145 | |
| 146 | #define SKB_ATUSB(skb) (*(struct atusb **)(skb)->cb) |
| 147 | |
| 148 | static void atusb_in(struct urb *urb); |
| 149 | |
| 150 | static int atusb_submit_rx_urb(struct atusb *atusb, struct urb *urb) |
| 151 | { |
| 152 | struct usb_device *usb_dev = atusb->usb_dev; |
| 153 | struct sk_buff *skb = urb->context; |
| 154 | int ret; |
| 155 | |
| 156 | if (!skb) { |
| 157 | skb = alloc_skb(MAX_RX_XFER, GFP_KERNEL); |
| 158 | if (!skb) { |
| 159 | dev_warn_ratelimited(&usb_dev->dev, |
| 160 | "atusb_in: can't allocate skb\n"); |
| 161 | return -ENOMEM; |
| 162 | } |
| 163 | skb_put(skb, MAX_RX_XFER); |
| 164 | SKB_ATUSB(skb) = atusb; |
| 165 | } |
| 166 | |
| 167 | usb_fill_bulk_urb(urb, usb_dev, usb_rcvbulkpipe(usb_dev, 1), |
| 168 | skb->data, MAX_RX_XFER, atusb_in, skb); |
| 169 | usb_anchor_urb(urb, &atusb->rx_urbs); |
| 170 | |
| 171 | ret = usb_submit_urb(urb, GFP_KERNEL); |
| 172 | if (ret) { |
| 173 | usb_unanchor_urb(urb); |
| 174 | kfree_skb(skb); |
| 175 | urb->context = NULL; |
| 176 | } |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | static void atusb_work_urbs(struct work_struct *work) |
| 181 | { |
| 182 | struct atusb *atusb = |
| 183 | container_of(to_delayed_work(work), struct atusb, work); |
| 184 | struct usb_device *usb_dev = atusb->usb_dev; |
| 185 | struct urb *urb; |
| 186 | int ret; |
| 187 | |
| 188 | if (atusb->shutdown) |
| 189 | return; |
| 190 | |
| 191 | do { |
| 192 | urb = usb_get_from_anchor(&atusb->idle_urbs); |
| 193 | if (!urb) |
| 194 | return; |
| 195 | ret = atusb_submit_rx_urb(atusb, urb); |
| 196 | } while (!ret); |
| 197 | |
| 198 | usb_anchor_urb(urb, &atusb->idle_urbs); |
| 199 | dev_warn_ratelimited(&usb_dev->dev, |
| 200 | "atusb_in: can't allocate/submit URB (%d)\n", ret); |
| 201 | schedule_delayed_work(&atusb->work, |
| 202 | msecs_to_jiffies(ATUSB_ALLOC_DELAY_MS) + 1); |
| 203 | } |
| 204 | |
| 205 | /* ----- Asynchronous USB -------------------------------------------------- */ |
| 206 | |
| 207 | static void atusb_tx_done(struct atusb *atusb, uint8_t seq) |
| 208 | { |
| 209 | struct usb_device *usb_dev = atusb->usb_dev; |
| 210 | uint8_t expect = atusb->tx_ack_seq; |
| 211 | |
| 212 | dev_dbg(&usb_dev->dev, "atusb_tx_done (0x%02x/0x%02x)\n", seq, expect); |
| 213 | if (seq == expect) { |
| 214 | /* TODO check for ifs handling in firmware */ |
| 215 | ieee802154_xmit_complete(atusb->hw, atusb->tx_skb, false); |
| 216 | } else { |
| 217 | /* TODO I experience this case when atusb has a tx complete |
| 218 | * irq before probing, we should fix the firmware it's an |
| 219 | * unlikely case now that seq == expect is then true, but can |
| 220 | * happen and fail with a tx_skb = NULL; |
| 221 | */ |
| 222 | ieee802154_wake_queue(atusb->hw); |
| 223 | if (atusb->tx_skb) |
| 224 | dev_kfree_skb_irq(atusb->tx_skb); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | static void atusb_in_good(struct urb *urb) |
| 229 | { |
| 230 | struct usb_device *usb_dev = urb->dev; |
| 231 | struct sk_buff *skb = urb->context; |
| 232 | struct atusb *atusb = SKB_ATUSB(skb); |
| 233 | uint8_t len, lqi; |
| 234 | |
| 235 | if (!urb->actual_length) { |
| 236 | dev_dbg(&usb_dev->dev, "atusb_in: zero-sized URB ?\n"); |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | len = *skb->data; |
| 241 | |
| 242 | if (urb->actual_length == 1) { |
| 243 | atusb_tx_done(atusb, len); |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | if (len + 1 > urb->actual_length - 1) { |
| 248 | dev_dbg(&usb_dev->dev, "atusb_in: frame len %d+1 > URB %u-1\n", |
| 249 | len, urb->actual_length); |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | if (!ieee802154_is_valid_psdu_len(len)) { |
| 254 | dev_dbg(&usb_dev->dev, "atusb_in: frame corrupted\n"); |
| 255 | return; |
| 256 | } |
| 257 | |
| 258 | lqi = skb->data[len + 1]; |
| 259 | dev_dbg(&usb_dev->dev, "atusb_in: rx len %d lqi 0x%02x\n", len, lqi); |
| 260 | skb_pull(skb, 1); /* remove PHR */ |
| 261 | skb_trim(skb, len); /* get payload only */ |
| 262 | ieee802154_rx_irqsafe(atusb->hw, skb, lqi); |
| 263 | urb->context = NULL; /* skb is gone */ |
| 264 | } |
| 265 | |
| 266 | static void atusb_in(struct urb *urb) |
| 267 | { |
| 268 | struct usb_device *usb_dev = urb->dev; |
| 269 | struct sk_buff *skb = urb->context; |
| 270 | struct atusb *atusb = SKB_ATUSB(skb); |
| 271 | |
| 272 | dev_dbg(&usb_dev->dev, "atusb_in: status %d len %d\n", |
| 273 | urb->status, urb->actual_length); |
| 274 | if (urb->status) { |
| 275 | if (urb->status == -ENOENT) { /* being killed */ |
| 276 | kfree_skb(skb); |
| 277 | urb->context = NULL; |
| 278 | return; |
| 279 | } |
| 280 | dev_dbg(&usb_dev->dev, "atusb_in: URB error %d\n", urb->status); |
| 281 | } else { |
| 282 | atusb_in_good(urb); |
| 283 | } |
| 284 | |
| 285 | usb_anchor_urb(urb, &atusb->idle_urbs); |
| 286 | if (!atusb->shutdown) |
| 287 | schedule_delayed_work(&atusb->work, 0); |
| 288 | } |
| 289 | |
| 290 | /* ----- URB allocation/deallocation --------------------------------------- */ |
| 291 | |
| 292 | static void atusb_free_urbs(struct atusb *atusb) |
| 293 | { |
| 294 | struct urb *urb; |
| 295 | |
| 296 | while (1) { |
| 297 | urb = usb_get_from_anchor(&atusb->idle_urbs); |
| 298 | if (!urb) |
| 299 | break; |
| 300 | if (urb->context) |
| 301 | kfree_skb(urb->context); |
| 302 | usb_free_urb(urb); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | static int atusb_alloc_urbs(struct atusb *atusb, int n) |
| 307 | { |
| 308 | struct urb *urb; |
| 309 | |
| 310 | while (n) { |
| 311 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 312 | if (!urb) { |
| 313 | atusb_free_urbs(atusb); |
| 314 | return -ENOMEM; |
| 315 | } |
| 316 | usb_anchor_urb(urb, &atusb->idle_urbs); |
| 317 | n--; |
| 318 | } |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | /* ----- IEEE 802.15.4 interface operations -------------------------------- */ |
| 323 | |
| 324 | static void atusb_xmit_complete(struct urb *urb) |
| 325 | { |
| 326 | dev_dbg(&urb->dev->dev, "atusb_xmit urb completed"); |
| 327 | } |
| 328 | |
| 329 | static int atusb_xmit(struct ieee802154_hw *hw, struct sk_buff *skb) |
| 330 | { |
| 331 | struct atusb *atusb = hw->priv; |
| 332 | struct usb_device *usb_dev = atusb->usb_dev; |
| 333 | int ret; |
| 334 | |
| 335 | dev_dbg(&usb_dev->dev, "atusb_xmit (%d)\n", skb->len); |
| 336 | atusb->tx_skb = skb; |
| 337 | atusb->tx_ack_seq++; |
| 338 | atusb->tx_dr.wIndex = cpu_to_le16(atusb->tx_ack_seq); |
| 339 | atusb->tx_dr.wLength = cpu_to_le16(skb->len); |
| 340 | |
| 341 | usb_fill_control_urb(atusb->tx_urb, usb_dev, |
| 342 | usb_sndctrlpipe(usb_dev, 0), |
| 343 | (unsigned char *)&atusb->tx_dr, skb->data, |
| 344 | skb->len, atusb_xmit_complete, NULL); |
| 345 | ret = usb_submit_urb(atusb->tx_urb, GFP_ATOMIC); |
| 346 | dev_dbg(&usb_dev->dev, "atusb_xmit done (%d)\n", ret); |
| 347 | return ret; |
| 348 | } |
| 349 | |
| 350 | static int atusb_channel(struct ieee802154_hw *hw, u8 page, u8 channel) |
| 351 | { |
| 352 | struct atusb *atusb = hw->priv; |
| 353 | int ret; |
| 354 | |
| 355 | /* This implicitly sets the CCA (Clear Channel Assessment) mode to 0, |
| 356 | * "Mode 3a, Carrier sense OR energy above threshold". |
| 357 | * We should probably make this configurable. @@@ |
| 358 | */ |
| 359 | ret = atusb_write_reg(atusb, RG_PHY_CC_CCA, channel); |
| 360 | if (ret < 0) |
| 361 | return ret; |
| 362 | msleep(1); /* @@@ ugly synchronization */ |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | static int atusb_ed(struct ieee802154_hw *hw, u8 *level) |
| 367 | { |
| 368 | /* @@@ not used by the stack yet */ |
| 369 | *level = 0; |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | static int atusb_set_hw_addr_filt(struct ieee802154_hw *hw, |
| 374 | struct ieee802154_hw_addr_filt *filt, |
| 375 | unsigned long changed) |
| 376 | { |
| 377 | struct atusb *atusb = hw->priv; |
| 378 | struct device *dev = &atusb->usb_dev->dev; |
| 379 | uint8_t reg; |
| 380 | |
| 381 | if (changed & IEEE802154_AFILT_SADDR_CHANGED) { |
| 382 | u16 addr = le16_to_cpu(filt->short_addr); |
| 383 | |
| 384 | dev_vdbg(dev, "atusb_set_hw_addr_filt called for saddr\n"); |
| 385 | atusb_write_reg(atusb, RG_SHORT_ADDR_0, addr); |
| 386 | atusb_write_reg(atusb, RG_SHORT_ADDR_1, addr >> 8); |
| 387 | } |
| 388 | |
| 389 | if (changed & IEEE802154_AFILT_PANID_CHANGED) { |
| 390 | u16 pan = le16_to_cpu(filt->pan_id); |
| 391 | |
| 392 | dev_vdbg(dev, "atusb_set_hw_addr_filt called for pan id\n"); |
| 393 | atusb_write_reg(atusb, RG_PAN_ID_0, pan); |
| 394 | atusb_write_reg(atusb, RG_PAN_ID_1, pan >> 8); |
| 395 | } |
| 396 | |
| 397 | if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) { |
| 398 | u8 i, addr[IEEE802154_EXTENDED_ADDR_LEN]; |
| 399 | |
| 400 | memcpy(addr, &filt->ieee_addr, IEEE802154_EXTENDED_ADDR_LEN); |
| 401 | dev_vdbg(dev, "atusb_set_hw_addr_filt called for IEEE addr\n"); |
| 402 | for (i = 0; i < 8; i++) |
| 403 | atusb_write_reg(atusb, RG_IEEE_ADDR_0 + i, addr[i]); |
| 404 | } |
| 405 | |
| 406 | if (changed & IEEE802154_AFILT_PANC_CHANGED) { |
| 407 | dev_vdbg(dev, |
| 408 | "atusb_set_hw_addr_filt called for panc change\n"); |
| 409 | reg = atusb_read_reg(atusb, SR_REG(SR_AACK_I_AM_COORD)); |
| 410 | if (filt->pan_coord) |
| 411 | reg |= SR_VALUE(SR_AACK_I_AM_COORD, 1); |
| 412 | else |
| 413 | reg &= ~SR_VALUE(SR_AACK_I_AM_COORD, 1); |
| 414 | atusb_write_reg(atusb, SR_REG(SR_AACK_I_AM_COORD), reg); |
| 415 | } |
| 416 | |
| 417 | return atusb_get_and_clear_error(atusb); |
| 418 | } |
| 419 | |
| 420 | static int atusb_start(struct ieee802154_hw *hw) |
| 421 | { |
| 422 | struct atusb *atusb = hw->priv; |
| 423 | struct usb_device *usb_dev = atusb->usb_dev; |
| 424 | int ret; |
| 425 | |
| 426 | dev_dbg(&usb_dev->dev, "atusb_start\n"); |
| 427 | schedule_delayed_work(&atusb->work, 0); |
| 428 | atusb_command(atusb, ATUSB_RX_MODE, 1); |
| 429 | ret = atusb_get_and_clear_error(atusb); |
| 430 | if (ret < 0) |
| 431 | usb_kill_anchored_urbs(&atusb->idle_urbs); |
| 432 | return ret; |
| 433 | } |
| 434 | |
| 435 | static void atusb_stop(struct ieee802154_hw *hw) |
| 436 | { |
| 437 | struct atusb *atusb = hw->priv; |
| 438 | struct usb_device *usb_dev = atusb->usb_dev; |
| 439 | |
| 440 | dev_dbg(&usb_dev->dev, "atusb_stop\n"); |
| 441 | usb_kill_anchored_urbs(&atusb->idle_urbs); |
| 442 | atusb_command(atusb, ATUSB_RX_MODE, 0); |
| 443 | atusb_get_and_clear_error(atusb); |
| 444 | } |
| 445 | |
| 446 | static struct ieee802154_ops atusb_ops = { |
| 447 | .owner = THIS_MODULE, |
| 448 | .xmit_async = atusb_xmit, |
| 449 | .ed = atusb_ed, |
| 450 | .set_channel = atusb_channel, |
| 451 | .start = atusb_start, |
| 452 | .stop = atusb_stop, |
| 453 | .set_hw_addr_filt = atusb_set_hw_addr_filt, |
| 454 | }; |
| 455 | |
| 456 | /* ----- Firmware and chip version information ----------------------------- */ |
| 457 | |
| 458 | static int atusb_get_and_show_revision(struct atusb *atusb) |
| 459 | { |
| 460 | struct usb_device *usb_dev = atusb->usb_dev; |
| 461 | unsigned char buffer[3]; |
| 462 | int ret; |
| 463 | |
| 464 | /* Get a couple of the ATMega Firmware values */ |
| 465 | ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0), |
| 466 | ATUSB_ID, ATUSB_REQ_FROM_DEV, 0, 0, |
| 467 | buffer, 3, 1000); |
| 468 | if (ret >= 0) |
| 469 | dev_info(&usb_dev->dev, |
| 470 | "Firmware: major: %u, minor: %u, hardware type: %u\n", |
| 471 | buffer[0], buffer[1], buffer[2]); |
Stefan Schmidt | 33a238a | 2015-05-21 16:51:35 +0200 | [diff] [blame^] | 472 | if (buffer[0] == 0 && buffer[1] < 2) { |
| 473 | dev_info(&usb_dev->dev, |
| 474 | "Firmware version (%u.%u) is predates our first public release.", |
| 475 | buffer[0], buffer[1]); |
| 476 | dev_info(&usb_dev->dev, "Please update to version 0.2 or newer"); |
| 477 | } |
Alexander Aring | 7490b00 | 2015-05-17 21:44:57 +0200 | [diff] [blame] | 478 | |
| 479 | return ret; |
| 480 | } |
| 481 | |
| 482 | static int atusb_get_and_show_build(struct atusb *atusb) |
| 483 | { |
| 484 | struct usb_device *usb_dev = atusb->usb_dev; |
| 485 | char build[ATUSB_BUILD_SIZE + 1]; |
| 486 | int ret; |
| 487 | |
| 488 | ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0), |
| 489 | ATUSB_BUILD, ATUSB_REQ_FROM_DEV, 0, 0, |
| 490 | build, ATUSB_BUILD_SIZE, 1000); |
| 491 | if (ret >= 0) { |
| 492 | build[ret] = 0; |
| 493 | dev_info(&usb_dev->dev, "Firmware: build %s\n", build); |
| 494 | } |
| 495 | |
| 496 | return ret; |
| 497 | } |
| 498 | |
| 499 | static int atusb_get_and_show_chip(struct atusb *atusb) |
| 500 | { |
| 501 | struct usb_device *usb_dev = atusb->usb_dev; |
| 502 | uint8_t man_id_0, man_id_1, part_num, version_num; |
| 503 | |
| 504 | man_id_0 = atusb_read_reg(atusb, RG_MAN_ID_0); |
| 505 | man_id_1 = atusb_read_reg(atusb, RG_MAN_ID_1); |
| 506 | part_num = atusb_read_reg(atusb, RG_PART_NUM); |
| 507 | version_num = atusb_read_reg(atusb, RG_VERSION_NUM); |
| 508 | |
| 509 | if (atusb->err) |
| 510 | return atusb->err; |
| 511 | |
| 512 | if ((man_id_1 << 8 | man_id_0) != ATUSB_JEDEC_ATMEL) { |
| 513 | dev_err(&usb_dev->dev, |
| 514 | "non-Atmel transceiver xxxx%02x%02x\n", |
| 515 | man_id_1, man_id_0); |
| 516 | goto fail; |
| 517 | } |
| 518 | if (part_num != 3) { |
| 519 | dev_err(&usb_dev->dev, |
| 520 | "unexpected transceiver, part 0x%02x version 0x%02x\n", |
| 521 | part_num, version_num); |
| 522 | goto fail; |
| 523 | } |
| 524 | |
| 525 | dev_info(&usb_dev->dev, "ATUSB: AT86RF231 version %d\n", version_num); |
| 526 | |
| 527 | return 0; |
| 528 | |
| 529 | fail: |
| 530 | atusb->err = -ENODEV; |
| 531 | return -ENODEV; |
| 532 | } |
| 533 | |
| 534 | /* ----- Setup ------------------------------------------------------------- */ |
| 535 | |
| 536 | static int atusb_probe(struct usb_interface *interface, |
| 537 | const struct usb_device_id *id) |
| 538 | { |
| 539 | struct usb_device *usb_dev = interface_to_usbdev(interface); |
| 540 | struct ieee802154_hw *hw; |
| 541 | struct atusb *atusb = NULL; |
| 542 | int ret = -ENOMEM; |
| 543 | |
| 544 | hw = ieee802154_alloc_hw(sizeof(struct atusb), &atusb_ops); |
| 545 | if (!hw) |
| 546 | return -ENOMEM; |
| 547 | |
| 548 | atusb = hw->priv; |
| 549 | atusb->hw = hw; |
| 550 | atusb->usb_dev = usb_get_dev(usb_dev); |
| 551 | usb_set_intfdata(interface, atusb); |
| 552 | |
| 553 | atusb->shutdown = 0; |
| 554 | atusb->err = 0; |
| 555 | INIT_DELAYED_WORK(&atusb->work, atusb_work_urbs); |
| 556 | init_usb_anchor(&atusb->idle_urbs); |
| 557 | init_usb_anchor(&atusb->rx_urbs); |
| 558 | |
| 559 | if (atusb_alloc_urbs(atusb, ATUSB_NUM_RX_URBS)) |
| 560 | goto fail; |
| 561 | |
| 562 | atusb->tx_dr.bRequestType = ATUSB_REQ_TO_DEV; |
| 563 | atusb->tx_dr.bRequest = ATUSB_TX; |
| 564 | atusb->tx_dr.wValue = cpu_to_le16(0); |
| 565 | |
| 566 | atusb->tx_urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 567 | if (!atusb->tx_urb) |
| 568 | goto fail; |
| 569 | |
| 570 | hw->parent = &usb_dev->dev; |
| 571 | hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AFILT; |
| 572 | |
| 573 | hw->phy->current_page = 0; |
| 574 | hw->phy->current_channel = 11; /* reset default */ |
| 575 | hw->phy->supported.channels[0] = 0x7FFF800; |
| 576 | ieee802154_random_extended_addr(&hw->phy->perm_extended_addr); |
| 577 | |
| 578 | atusb_command(atusb, ATUSB_RF_RESET, 0); |
| 579 | atusb_get_and_show_chip(atusb); |
| 580 | atusb_get_and_show_revision(atusb); |
| 581 | atusb_get_and_show_build(atusb); |
| 582 | ret = atusb_get_and_clear_error(atusb); |
| 583 | if (ret) { |
| 584 | dev_err(&atusb->usb_dev->dev, |
| 585 | "%s: initialization failed, error = %d\n", |
| 586 | __func__, ret); |
| 587 | goto fail; |
| 588 | } |
| 589 | |
| 590 | ret = ieee802154_register_hw(hw); |
| 591 | if (ret) |
| 592 | goto fail; |
| 593 | |
| 594 | /* If we just powered on, we're now in P_ON and need to enter TRX_OFF |
| 595 | * explicitly. Any resets after that will send us straight to TRX_OFF, |
| 596 | * making the command below redundant. |
| 597 | */ |
| 598 | atusb_write_reg(atusb, RG_TRX_STATE, STATE_FORCE_TRX_OFF); |
| 599 | msleep(1); /* reset => TRX_OFF, tTR13 = 37 us */ |
| 600 | |
| 601 | #if 0 |
| 602 | /* Calculating the maximum time available to empty the frame buffer |
| 603 | * on reception: |
| 604 | * |
| 605 | * According to [1], the inter-frame gap is |
| 606 | * R * 20 * 16 us + 128 us |
| 607 | * where R is a random number from 0 to 7. Furthermore, we have 20 bit |
| 608 | * times (80 us at 250 kbps) of SHR of the next frame before the |
| 609 | * transceiver begins storing data in the frame buffer. |
| 610 | * |
| 611 | * This yields a minimum time of 208 us between the last data of a |
| 612 | * frame and the first data of the next frame. This time is further |
| 613 | * reduced by interrupt latency in the atusb firmware. |
| 614 | * |
| 615 | * atusb currently needs about 500 us to retrieve a maximum-sized |
| 616 | * frame. We therefore have to allow reception of a new frame to begin |
| 617 | * while we retrieve the previous frame. |
| 618 | * |
| 619 | * [1] "JN-AN-1035 Calculating data rates in an IEEE 802.15.4-based |
| 620 | * network", Jennic 2006. |
| 621 | * http://www.jennic.com/download_file.php?supportFile=JN-AN-1035%20Calculating%20802-15-4%20Data%20Rates-1v0.pdf |
| 622 | */ |
| 623 | |
| 624 | atusb_write_reg(atusb, |
| 625 | SR_REG(SR_RX_SAFE_MODE), SR_VALUE(SR_RX_SAFE_MODE, 1)); |
| 626 | #endif |
| 627 | atusb_write_reg(atusb, RG_IRQ_MASK, 0xff); |
| 628 | |
| 629 | ret = atusb_get_and_clear_error(atusb); |
| 630 | if (!ret) |
| 631 | return 0; |
| 632 | |
| 633 | dev_err(&atusb->usb_dev->dev, |
| 634 | "%s: setup failed, error = %d\n", |
| 635 | __func__, ret); |
| 636 | |
| 637 | ieee802154_unregister_hw(hw); |
| 638 | fail: |
| 639 | atusb_free_urbs(atusb); |
| 640 | usb_kill_urb(atusb->tx_urb); |
| 641 | usb_free_urb(atusb->tx_urb); |
| 642 | usb_put_dev(usb_dev); |
| 643 | ieee802154_free_hw(hw); |
| 644 | return ret; |
| 645 | } |
| 646 | |
| 647 | static void atusb_disconnect(struct usb_interface *interface) |
| 648 | { |
| 649 | struct atusb *atusb = usb_get_intfdata(interface); |
| 650 | |
| 651 | dev_dbg(&atusb->usb_dev->dev, "atusb_disconnect\n"); |
| 652 | |
| 653 | atusb->shutdown = 1; |
| 654 | cancel_delayed_work_sync(&atusb->work); |
| 655 | |
| 656 | usb_kill_anchored_urbs(&atusb->rx_urbs); |
| 657 | atusb_free_urbs(atusb); |
| 658 | usb_kill_urb(atusb->tx_urb); |
| 659 | usb_free_urb(atusb->tx_urb); |
| 660 | |
| 661 | ieee802154_unregister_hw(atusb->hw); |
| 662 | |
| 663 | ieee802154_free_hw(atusb->hw); |
| 664 | |
| 665 | usb_set_intfdata(interface, NULL); |
| 666 | usb_put_dev(atusb->usb_dev); |
| 667 | |
| 668 | pr_debug("atusb_disconnect done\n"); |
| 669 | } |
| 670 | |
| 671 | /* The devices we work with */ |
| 672 | static const struct usb_device_id atusb_device_table[] = { |
| 673 | { |
| 674 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE | |
| 675 | USB_DEVICE_ID_MATCH_INT_INFO, |
| 676 | .idVendor = ATUSB_VENDOR_ID, |
| 677 | .idProduct = ATUSB_PRODUCT_ID, |
| 678 | .bInterfaceClass = USB_CLASS_VENDOR_SPEC |
| 679 | }, |
| 680 | /* end with null element */ |
| 681 | {} |
| 682 | }; |
| 683 | MODULE_DEVICE_TABLE(usb, atusb_device_table); |
| 684 | |
| 685 | static struct usb_driver atusb_driver = { |
| 686 | .name = "atusb", |
| 687 | .probe = atusb_probe, |
| 688 | .disconnect = atusb_disconnect, |
| 689 | .id_table = atusb_device_table, |
| 690 | }; |
| 691 | module_usb_driver(atusb_driver); |
| 692 | |
| 693 | MODULE_AUTHOR("Alexander Aring <alex.aring@gmail.com>"); |
| 694 | MODULE_AUTHOR("Richard Sharpe <realrichardsharpe@gmail.com>"); |
| 695 | MODULE_AUTHOR("Stefan Schmidt <stefan@datenfreihafen.org>"); |
| 696 | MODULE_AUTHOR("Werner Almesberger <werner@almesberger.net>"); |
| 697 | MODULE_DESCRIPTION("ATUSB IEEE 802.15.4 Driver"); |
| 698 | MODULE_LICENSE("GPL"); |