2 * atusb.c - Driver for the ATUSB IEEE 802.15.4 dongle
4 * Written 2013 by Werner Almesberger <werner@almesberger.net>
6 * Copyright (c) 2015 - 2016 Stefan Schmidt <stefan@datenfreihafen.org>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation, version 2
12 * Based on at86rf230.c and spi_atusb.c.
14 * Copyright (C) 2009 Siemens AG
15 * Written by: Dmitry Eremin-Solenikov <dmitry.baryshkov@siemens.com>
18 * Copyright (c) 2011 Richard Sharpe <realrichardsharpe@gmail.com>
19 * Copyright (c) 2011 Stefan Schmidt <stefan@datenfreihafen.org>
20 * Copyright (c) 2011 Werner Almesberger <werner@almesberger.net>
22 * USB initialization is
23 * Copyright (c) 2013 Alexander Aring <alex.aring@gmail.com>
25 * Busware HUL support is
26 * Copyright (c) 2017 Josef Filzmaier <j.filzmaier@gmx.at>
29 #include <linux/kernel.h>
30 #include <linux/slab.h>
31 #include <linux/module.h>
32 #include <linux/jiffies.h>
33 #include <linux/usb.h>
34 #include <linux/skbuff.h>
36 #include <net/cfg802154.h>
37 #include <net/mac802154.h>
39 #include "at86rf230.h"
42 #define ATUSB_JEDEC_ATMEL 0x1f /* JEDEC manufacturer ID */
44 #define ATUSB_NUM_RX_URBS 4 /* allow for a bit of local latency */
45 #define ATUSB_ALLOC_DELAY_MS 100 /* delay after failed allocation */
46 #define ATUSB_TX_TIMEOUT_MS 200 /* on the air timeout */
49 struct ieee802154_hw *hw;
50 struct usb_device *usb_dev;
51 struct atusb_chip_data *data;
52 int shutdown; /* non-zero if shutting down */
53 int err; /* set by first error */
56 struct delayed_work work; /* memory allocations */
57 struct usb_anchor idle_urbs; /* URBs waiting to be submitted */
58 struct usb_anchor rx_urbs; /* URBs waiting for reception */
61 struct usb_ctrlrequest tx_dr;
63 struct sk_buff *tx_skb;
64 u8 tx_ack_seq; /* current TX ACK sequence number */
66 /* Firmware variable */
67 unsigned char fw_ver_maj; /* Firmware major version number */
68 unsigned char fw_ver_min; /* Firmware minor version number */
69 unsigned char fw_hw_type; /* Firmware hardware type */
72 struct atusb_chip_data {
76 int (*set_channel)(struct ieee802154_hw*, u8, u8);
77 int (*set_txpower)(struct ieee802154_hw*, s32);
80 /* ----- USB commands without data ----------------------------------------- */
82 /* To reduce the number of error checks in the code, we record the first error
83 * in atusb->err and reject all subsequent requests until the error is cleared.
86 static int atusb_control_msg(struct atusb *atusb, unsigned int pipe,
87 __u8 request, __u8 requesttype,
88 __u16 value, __u16 index,
89 void *data, __u16 size, int timeout)
91 struct usb_device *usb_dev = atusb->usb_dev;
97 ret = usb_control_msg(usb_dev, pipe, request, requesttype,
98 value, index, data, size, timeout);
101 dev_err(&usb_dev->dev,
102 "%s: req 0x%02x val 0x%x idx 0x%x, error %d\n",
103 __func__, request, value, index, ret);
108 static int atusb_command(struct atusb *atusb, u8 cmd, u8 arg)
110 struct usb_device *usb_dev = atusb->usb_dev;
112 dev_dbg(&usb_dev->dev, "%s: cmd = 0x%x\n", __func__, cmd);
113 return atusb_control_msg(atusb, usb_sndctrlpipe(usb_dev, 0),
114 cmd, ATUSB_REQ_TO_DEV, arg, 0, NULL, 0, 1000);
117 static int atusb_write_reg(struct atusb *atusb, u8 reg, u8 value)
119 struct usb_device *usb_dev = atusb->usb_dev;
121 dev_dbg(&usb_dev->dev, "%s: 0x%02x <- 0x%02x\n", __func__, reg, value);
122 return atusb_control_msg(atusb, usb_sndctrlpipe(usb_dev, 0),
123 ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
124 value, reg, NULL, 0, 1000);
127 static int atusb_read_reg(struct atusb *atusb, u8 reg)
129 struct usb_device *usb_dev = atusb->usb_dev;
134 buffer = kmalloc(1, GFP_KERNEL);
138 dev_dbg(&usb_dev->dev, "%s: reg = 0x%x\n", __func__, reg);
139 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
140 ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
141 0, reg, buffer, 1, 1000);
153 static int atusb_write_subreg(struct atusb *atusb, u8 reg, u8 mask,
156 struct usb_device *usb_dev = atusb->usb_dev;
160 dev_dbg(&usb_dev->dev, "%s: 0x%02x <- 0x%02x\n", __func__, reg, value);
162 orig = atusb_read_reg(atusb, reg);
164 /* Write the value only into that part of the register which is allowed
165 * by the mask. All other bits stay as before.
168 tmp |= (value << shift) & mask;
171 ret = atusb_write_reg(atusb, reg, tmp);
176 static int atusb_read_subreg(struct atusb *lp,
177 unsigned int addr, unsigned int mask,
182 rc = atusb_read_reg(lp, addr);
183 rc = (rc & mask) >> shift;
188 static int atusb_get_and_clear_error(struct atusb *atusb)
190 int err = atusb->err;
196 /* ----- skb allocation ---------------------------------------------------- */
199 #define MAX_RX_XFER (1 + MAX_PSDU + 2 + 1) /* PHR+PSDU+CRC+LQI */
201 #define SKB_ATUSB(skb) (*(struct atusb **)(skb)->cb)
203 static void atusb_in(struct urb *urb);
205 static int atusb_submit_rx_urb(struct atusb *atusb, struct urb *urb)
207 struct usb_device *usb_dev = atusb->usb_dev;
208 struct sk_buff *skb = urb->context;
212 skb = alloc_skb(MAX_RX_XFER, GFP_KERNEL);
214 dev_warn_ratelimited(&usb_dev->dev,
215 "atusb_in: can't allocate skb\n");
218 skb_put(skb, MAX_RX_XFER);
219 SKB_ATUSB(skb) = atusb;
222 usb_fill_bulk_urb(urb, usb_dev, usb_rcvbulkpipe(usb_dev, 1),
223 skb->data, MAX_RX_XFER, atusb_in, skb);
224 usb_anchor_urb(urb, &atusb->rx_urbs);
226 ret = usb_submit_urb(urb, GFP_KERNEL);
228 usb_unanchor_urb(urb);
235 static void atusb_work_urbs(struct work_struct *work)
237 struct atusb *atusb =
238 container_of(to_delayed_work(work), struct atusb, work);
239 struct usb_device *usb_dev = atusb->usb_dev;
247 urb = usb_get_from_anchor(&atusb->idle_urbs);
250 ret = atusb_submit_rx_urb(atusb, urb);
253 usb_anchor_urb(urb, &atusb->idle_urbs);
254 dev_warn_ratelimited(&usb_dev->dev,
255 "atusb_in: can't allocate/submit URB (%d)\n", ret);
256 schedule_delayed_work(&atusb->work,
257 msecs_to_jiffies(ATUSB_ALLOC_DELAY_MS) + 1);
260 /* ----- Asynchronous USB -------------------------------------------------- */
262 static void atusb_tx_done(struct atusb *atusb, u8 seq)
264 struct usb_device *usb_dev = atusb->usb_dev;
265 u8 expect = atusb->tx_ack_seq;
267 dev_dbg(&usb_dev->dev, "%s (0x%02x/0x%02x)\n", __func__, seq, expect);
269 /* TODO check for ifs handling in firmware */
270 ieee802154_xmit_complete(atusb->hw, atusb->tx_skb, false);
272 /* TODO I experience this case when atusb has a tx complete
273 * irq before probing, we should fix the firmware it's an
274 * unlikely case now that seq == expect is then true, but can
275 * happen and fail with a tx_skb = NULL;
277 ieee802154_wake_queue(atusb->hw);
279 dev_kfree_skb_irq(atusb->tx_skb);
283 static void atusb_in_good(struct urb *urb)
285 struct usb_device *usb_dev = urb->dev;
286 struct sk_buff *skb = urb->context;
287 struct atusb *atusb = SKB_ATUSB(skb);
290 if (!urb->actual_length) {
291 dev_dbg(&usb_dev->dev, "atusb_in: zero-sized URB ?\n");
297 if (urb->actual_length == 1) {
298 atusb_tx_done(atusb, len);
302 if (len + 1 > urb->actual_length - 1) {
303 dev_dbg(&usb_dev->dev, "atusb_in: frame len %d+1 > URB %u-1\n",
304 len, urb->actual_length);
308 if (!ieee802154_is_valid_psdu_len(len)) {
309 dev_dbg(&usb_dev->dev, "atusb_in: frame corrupted\n");
313 lqi = skb->data[len + 1];
314 dev_dbg(&usb_dev->dev, "atusb_in: rx len %d lqi 0x%02x\n", len, lqi);
315 skb_pull(skb, 1); /* remove PHR */
316 skb_trim(skb, len); /* get payload only */
317 ieee802154_rx_irqsafe(atusb->hw, skb, lqi);
318 urb->context = NULL; /* skb is gone */
321 static void atusb_in(struct urb *urb)
323 struct usb_device *usb_dev = urb->dev;
324 struct sk_buff *skb = urb->context;
325 struct atusb *atusb = SKB_ATUSB(skb);
327 dev_dbg(&usb_dev->dev, "%s: status %d len %d\n", __func__,
328 urb->status, urb->actual_length);
330 if (urb->status == -ENOENT) { /* being killed */
335 dev_dbg(&usb_dev->dev, "%s: URB error %d\n", __func__, urb->status);
340 usb_anchor_urb(urb, &atusb->idle_urbs);
341 if (!atusb->shutdown)
342 schedule_delayed_work(&atusb->work, 0);
345 /* ----- URB allocation/deallocation --------------------------------------- */
347 static void atusb_free_urbs(struct atusb *atusb)
352 urb = usb_get_from_anchor(&atusb->idle_urbs);
355 kfree_skb(urb->context);
360 static int atusb_alloc_urbs(struct atusb *atusb, int n)
365 urb = usb_alloc_urb(0, GFP_KERNEL);
367 atusb_free_urbs(atusb);
370 usb_anchor_urb(urb, &atusb->idle_urbs);
377 /* ----- IEEE 802.15.4 interface operations -------------------------------- */
379 static void atusb_xmit_complete(struct urb *urb)
381 dev_dbg(&urb->dev->dev, "atusb_xmit urb completed");
384 static int atusb_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
386 struct atusb *atusb = hw->priv;
387 struct usb_device *usb_dev = atusb->usb_dev;
390 dev_dbg(&usb_dev->dev, "%s (%d)\n", __func__, skb->len);
393 atusb->tx_dr.wIndex = cpu_to_le16(atusb->tx_ack_seq);
394 atusb->tx_dr.wLength = cpu_to_le16(skb->len);
396 usb_fill_control_urb(atusb->tx_urb, usb_dev,
397 usb_sndctrlpipe(usb_dev, 0),
398 (unsigned char *)&atusb->tx_dr, skb->data,
399 skb->len, atusb_xmit_complete, NULL);
400 ret = usb_submit_urb(atusb->tx_urb, GFP_ATOMIC);
401 dev_dbg(&usb_dev->dev, "%s done (%d)\n", __func__, ret);
405 static int atusb_ed(struct ieee802154_hw *hw, u8 *level)
412 static int atusb_set_hw_addr_filt(struct ieee802154_hw *hw,
413 struct ieee802154_hw_addr_filt *filt,
414 unsigned long changed)
416 struct atusb *atusb = hw->priv;
417 struct device *dev = &atusb->usb_dev->dev;
419 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
420 u16 addr = le16_to_cpu(filt->short_addr);
422 dev_vdbg(dev, "%s called for saddr\n", __func__);
423 atusb_write_reg(atusb, RG_SHORT_ADDR_0, addr);
424 atusb_write_reg(atusb, RG_SHORT_ADDR_1, addr >> 8);
427 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
428 u16 pan = le16_to_cpu(filt->pan_id);
430 dev_vdbg(dev, "%s called for pan id\n", __func__);
431 atusb_write_reg(atusb, RG_PAN_ID_0, pan);
432 atusb_write_reg(atusb, RG_PAN_ID_1, pan >> 8);
435 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
436 u8 i, addr[IEEE802154_EXTENDED_ADDR_LEN];
438 memcpy(addr, &filt->ieee_addr, IEEE802154_EXTENDED_ADDR_LEN);
439 dev_vdbg(dev, "%s called for IEEE addr\n", __func__);
440 for (i = 0; i < 8; i++)
441 atusb_write_reg(atusb, RG_IEEE_ADDR_0 + i, addr[i]);
444 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
445 dev_vdbg(dev, "%s called for panc change\n", __func__);
447 atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 1);
449 atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 0);
452 return atusb_get_and_clear_error(atusb);
455 static int atusb_start(struct ieee802154_hw *hw)
457 struct atusb *atusb = hw->priv;
458 struct usb_device *usb_dev = atusb->usb_dev;
461 dev_dbg(&usb_dev->dev, "%s\n", __func__);
462 schedule_delayed_work(&atusb->work, 0);
463 atusb_command(atusb, ATUSB_RX_MODE, 1);
464 ret = atusb_get_and_clear_error(atusb);
466 usb_kill_anchored_urbs(&atusb->idle_urbs);
470 static void atusb_stop(struct ieee802154_hw *hw)
472 struct atusb *atusb = hw->priv;
473 struct usb_device *usb_dev = atusb->usb_dev;
475 dev_dbg(&usb_dev->dev, "%s\n", __func__);
476 usb_kill_anchored_urbs(&atusb->idle_urbs);
477 atusb_command(atusb, ATUSB_RX_MODE, 0);
478 atusb_get_and_clear_error(atusb);
481 #define ATUSB_MAX_TX_POWERS 0xF
482 static const s32 atusb_powers[ATUSB_MAX_TX_POWERS + 1] = {
483 300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700,
488 atusb_txpower(struct ieee802154_hw *hw, s32 mbm)
490 struct atusb *atusb = hw->priv;
493 return atusb->data->set_txpower(hw, mbm);
499 atusb_set_txpower(struct ieee802154_hw *hw, s32 mbm)
501 struct atusb *atusb = hw->priv;
504 for (i = 0; i < hw->phy->supported.tx_powers_size; i++) {
505 if (hw->phy->supported.tx_powers[i] == mbm)
506 return atusb_write_subreg(atusb, SR_TX_PWR_23X, i);
513 hulusb_set_txpower(struct ieee802154_hw *hw, s32 mbm)
517 for (i = 0; i < hw->phy->supported.tx_powers_size; i++) {
518 if (hw->phy->supported.tx_powers[i] == mbm)
519 return atusb_write_subreg(hw->priv, SR_TX_PWR_212, i);
525 #define ATUSB_MAX_ED_LEVELS 0xF
526 static const s32 atusb_ed_levels[ATUSB_MAX_ED_LEVELS + 1] = {
527 -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300,
528 -7100, -6900, -6700, -6500, -6300, -6100,
531 #define AT86RF212_MAX_TX_POWERS 0x1F
532 static const s32 at86rf212_powers[AT86RF212_MAX_TX_POWERS + 1] = {
533 500, 400, 300, 200, 100, 0, -100, -200, -300, -400, -500, -600, -700,
534 -800, -900, -1000, -1100, -1200, -1300, -1400, -1500, -1600, -1700,
535 -1800, -1900, -2000, -2100, -2200, -2300, -2400, -2500, -2600,
538 #define AT86RF2XX_MAX_ED_LEVELS 0xF
539 static const s32 at86rf212_ed_levels_100[AT86RF2XX_MAX_ED_LEVELS + 1] = {
540 -10000, -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200,
541 -8000, -7800, -7600, -7400, -7200, -7000,
544 static const s32 at86rf212_ed_levels_98[AT86RF2XX_MAX_ED_LEVELS + 1] = {
545 -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200, -8000,
546 -7800, -7600, -7400, -7200, -7000, -6800,
550 atusb_set_cca_mode(struct ieee802154_hw *hw, const struct wpan_phy_cca *cca)
552 struct atusb *atusb = hw->priv;
555 /* mapping 802.15.4 to driver spec */
557 case NL802154_CCA_ENERGY:
560 case NL802154_CCA_CARRIER:
563 case NL802154_CCA_ENERGY_CARRIER:
565 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
568 case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
579 return atusb_write_subreg(atusb, SR_CCA_MODE, val);
582 static int hulusb_set_cca_ed_level(struct atusb *lp, int rssi_base_val)
584 unsigned int cca_ed_thres;
586 cca_ed_thres = atusb_read_subreg(lp, SR_CCA_ED_THRES);
588 switch (rssi_base_val) {
590 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_98;
591 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_98);
592 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_98[cca_ed_thres];
595 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
596 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
597 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_100[cca_ed_thres];
607 atusb_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
609 struct atusb *atusb = hw->priv;
612 for (i = 0; i < hw->phy->supported.cca_ed_levels_size; i++) {
613 if (hw->phy->supported.cca_ed_levels[i] == mbm)
614 return atusb_write_subreg(atusb, SR_CCA_ED_THRES, i);
620 static int atusb_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
622 struct atusb *atusb = hw->priv;
626 ret = atusb->data->set_channel(hw, page, channel);
627 /* @@@ ugly synchronization */
628 msleep(atusb->data->t_channel_switch);
634 static int atusb_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
636 struct atusb *atusb = hw->priv;
639 ret = atusb_write_subreg(atusb, SR_CHANNEL, channel);
645 static int hulusb_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
650 struct atusb *lp = hw->priv;
653 rc = atusb_write_subreg(lp, SR_SUB_MODE, 0);
655 rc = atusb_write_subreg(lp, SR_SUB_MODE, 1);
660 rc = atusb_write_subreg(lp, SR_BPSK_QPSK, 0);
661 rssi_base_val = -100;
663 rc = atusb_write_subreg(lp, SR_BPSK_QPSK, 1);
669 rc = hulusb_set_cca_ed_level(lp, rssi_base_val);
673 /* This sets the symbol_duration according frequency on the 212.
674 * TODO move this handling while set channel and page in cfg802154.
675 * We can do that, this timings are according 802.15.4 standard.
676 * If we do that in cfg802154, this is a more generic calculation.
678 * This should also protected from ifs_timer. Means cancel timer and
679 * init with a new value. For now, this is okay.
683 /* SUB:0 and BPSK:0 -> BPSK-20 */
684 lp->hw->phy->symbol_duration = 50;
686 /* SUB:1 and BPSK:0 -> BPSK-40 */
687 lp->hw->phy->symbol_duration = 25;
691 /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */
692 lp->hw->phy->symbol_duration = 40;
694 /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */
695 lp->hw->phy->symbol_duration = 16;
698 lp->hw->phy->lifs_period = IEEE802154_LIFS_PERIOD *
699 lp->hw->phy->symbol_duration;
700 lp->hw->phy->sifs_period = IEEE802154_SIFS_PERIOD *
701 lp->hw->phy->symbol_duration;
703 return atusb_write_subreg(lp, SR_CHANNEL, channel);
707 atusb_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be, u8 retries)
709 struct atusb *atusb = hw->priv;
712 ret = atusb_write_subreg(atusb, SR_MIN_BE, min_be);
716 ret = atusb_write_subreg(atusb, SR_MAX_BE, max_be);
720 return atusb_write_subreg(atusb, SR_MAX_CSMA_RETRIES, retries);
724 hulusb_set_lbt(struct ieee802154_hw *hw, bool on)
726 struct atusb *atusb = hw->priv;
728 return atusb_write_subreg(atusb, SR_CSMA_LBT_MODE, on);
732 atusb_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
734 struct atusb *atusb = hw->priv;
736 return atusb_write_subreg(atusb, SR_MAX_FRAME_RETRIES, retries);
740 atusb_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
742 struct atusb *atusb = hw->priv;
746 ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 1);
750 ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 1);
754 ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 0);
758 ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 0);
766 static struct atusb_chip_data atusb_chip_data = {
767 .t_channel_switch = 1,
768 .rssi_base_val = -91,
769 .set_txpower = atusb_set_txpower,
770 .set_channel = atusb_set_channel,
773 static struct atusb_chip_data hulusb_chip_data = {
774 .t_channel_switch = 11,
775 .rssi_base_val = -100,
776 .set_txpower = hulusb_set_txpower,
777 .set_channel = hulusb_set_channel,
780 static const struct ieee802154_ops atusb_ops = {
781 .owner = THIS_MODULE,
782 .xmit_async = atusb_xmit,
784 .set_channel = atusb_channel,
785 .start = atusb_start,
787 .set_hw_addr_filt = atusb_set_hw_addr_filt,
788 .set_txpower = atusb_txpower,
789 .set_lbt = hulusb_set_lbt,
790 .set_cca_mode = atusb_set_cca_mode,
791 .set_cca_ed_level = atusb_set_cca_ed_level,
792 .set_csma_params = atusb_set_csma_params,
793 .set_frame_retries = atusb_set_frame_retries,
794 .set_promiscuous_mode = atusb_set_promiscuous_mode,
797 /* ----- Firmware and chip version information ----------------------------- */
799 static int atusb_get_and_show_revision(struct atusb *atusb)
801 struct usb_device *usb_dev = atusb->usb_dev;
803 unsigned char *buffer;
806 buffer = kmalloc(3, GFP_KERNEL);
810 /* Get a couple of the ATMega Firmware values */
811 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
812 ATUSB_ID, ATUSB_REQ_FROM_DEV, 0, 0,
815 atusb->fw_ver_maj = buffer[0];
816 atusb->fw_ver_min = buffer[1];
817 atusb->fw_hw_type = buffer[2];
819 switch (atusb->fw_hw_type) {
820 case ATUSB_HW_TYPE_100813:
821 case ATUSB_HW_TYPE_101216:
822 case ATUSB_HW_TYPE_110131:
824 atusb->data = &atusb_chip_data;
826 case ATUSB_HW_TYPE_RZUSB:
828 atusb->data = &atusb_chip_data;
830 case ATUSB_HW_TYPE_HULUSB:
832 atusb->data = &hulusb_chip_data;
836 atusb->err = -ENOTSUPP;
841 dev_info(&usb_dev->dev,
842 "Firmware: major: %u, minor: %u, hardware type: %s (%d)\n",
843 atusb->fw_ver_maj, atusb->fw_ver_min, hw_name,
846 if (atusb->fw_ver_maj == 0 && atusb->fw_ver_min < 2) {
847 dev_info(&usb_dev->dev,
848 "Firmware version (%u.%u) predates our first public release.",
849 atusb->fw_ver_maj, atusb->fw_ver_min);
850 dev_info(&usb_dev->dev, "Please update to version 0.2 or newer");
857 static int atusb_get_and_show_build(struct atusb *atusb)
859 struct usb_device *usb_dev = atusb->usb_dev;
863 build = kmalloc(ATUSB_BUILD_SIZE + 1, GFP_KERNEL);
867 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
868 ATUSB_BUILD, ATUSB_REQ_FROM_DEV, 0, 0,
869 build, ATUSB_BUILD_SIZE, 1000);
872 dev_info(&usb_dev->dev, "Firmware: build %s\n", build);
879 static int atusb_get_and_conf_chip(struct atusb *atusb)
881 struct usb_device *usb_dev = atusb->usb_dev;
882 u8 man_id_0, man_id_1, part_num, version_num;
884 struct ieee802154_hw *hw = atusb->hw;
886 man_id_0 = atusb_read_reg(atusb, RG_MAN_ID_0);
887 man_id_1 = atusb_read_reg(atusb, RG_MAN_ID_1);
888 part_num = atusb_read_reg(atusb, RG_PART_NUM);
889 version_num = atusb_read_reg(atusb, RG_VERSION_NUM);
894 hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AFILT |
895 IEEE802154_HW_PROMISCUOUS | IEEE802154_HW_CSMA_PARAMS;
897 hw->phy->flags = WPAN_PHY_FLAG_TXPOWER | WPAN_PHY_FLAG_CCA_ED_LEVEL |
898 WPAN_PHY_FLAG_CCA_MODE;
900 hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) |
901 BIT(NL802154_CCA_CARRIER) |
902 BIT(NL802154_CCA_ENERGY_CARRIER);
903 hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) |
904 BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR);
906 hw->phy->cca.mode = NL802154_CCA_ENERGY;
908 hw->phy->current_page = 0;
910 if ((man_id_1 << 8 | man_id_0) != ATUSB_JEDEC_ATMEL) {
911 dev_err(&usb_dev->dev,
912 "non-Atmel transceiver xxxx%02x%02x\n",
920 atusb->hw->phy->supported.channels[0] = 0x7FFF800;
921 atusb->hw->phy->current_channel = 11; /* reset default */
922 atusb->hw->phy->symbol_duration = 16;
923 atusb->hw->phy->supported.tx_powers = atusb_powers;
924 atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers);
925 hw->phy->supported.cca_ed_levels = atusb_ed_levels;
926 hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels);
930 atusb->hw->phy->supported.channels[0] = 0x7FFF800;
931 atusb->hw->phy->current_channel = 11; /* reset default */
932 atusb->hw->phy->symbol_duration = 16;
933 atusb->hw->phy->supported.tx_powers = atusb_powers;
934 atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers);
935 hw->phy->supported.cca_ed_levels = atusb_ed_levels;
936 hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels);
940 atusb->hw->flags |= IEEE802154_HW_LBT;
941 atusb->hw->phy->supported.channels[0] = 0x00007FF;
942 atusb->hw->phy->supported.channels[2] = 0x00007FF;
943 atusb->hw->phy->current_channel = 5;
944 atusb->hw->phy->symbol_duration = 25;
945 atusb->hw->phy->supported.lbt = NL802154_SUPPORTED_BOOL_BOTH;
946 atusb->hw->phy->supported.tx_powers = at86rf212_powers;
947 atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf212_powers);
948 atusb->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
949 atusb->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
952 dev_err(&usb_dev->dev,
953 "unexpected transceiver, part 0x%02x version 0x%02x\n",
954 part_num, version_num);
958 hw->phy->transmit_power = hw->phy->supported.tx_powers[0];
959 hw->phy->cca_ed_level = hw->phy->supported.cca_ed_levels[7];
961 dev_info(&usb_dev->dev, "ATUSB: %s version %d\n", chip, version_num);
966 atusb->err = -ENODEV;
970 static int atusb_set_extended_addr(struct atusb *atusb)
972 struct usb_device *usb_dev = atusb->usb_dev;
973 unsigned char *buffer;
974 __le64 extended_addr;
978 /* Firmware versions before 0.3 do not support the EUI64_READ command.
979 * Just use a random address and be done.
981 if (atusb->fw_ver_maj == 0 && atusb->fw_ver_min < 3) {
982 ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
986 buffer = kmalloc(IEEE802154_EXTENDED_ADDR_LEN, GFP_KERNEL);
990 /* Firmware is new enough so we fetch the address from EEPROM */
991 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
992 ATUSB_EUI64_READ, ATUSB_REQ_FROM_DEV, 0, 0,
993 buffer, IEEE802154_EXTENDED_ADDR_LEN, 1000);
995 dev_err(&usb_dev->dev, "failed to fetch extended address, random address set\n");
996 ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
1001 memcpy(&extended_addr, buffer, IEEE802154_EXTENDED_ADDR_LEN);
1002 /* Check if read address is not empty and the unicast bit is set correctly */
1003 if (!ieee802154_is_valid_extended_unicast_addr(extended_addr)) {
1004 dev_info(&usb_dev->dev, "no permanent extended address found, random address set\n");
1005 ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
1007 atusb->hw->phy->perm_extended_addr = extended_addr;
1008 addr = swab64((__force u64)atusb->hw->phy->perm_extended_addr);
1009 dev_info(&usb_dev->dev, "Read permanent extended address %8phC from device\n",
1017 /* ----- Setup ------------------------------------------------------------- */
1019 static int atusb_probe(struct usb_interface *interface,
1020 const struct usb_device_id *id)
1022 struct usb_device *usb_dev = interface_to_usbdev(interface);
1023 struct ieee802154_hw *hw;
1024 struct atusb *atusb = NULL;
1027 hw = ieee802154_alloc_hw(sizeof(struct atusb), &atusb_ops);
1033 atusb->usb_dev = usb_get_dev(usb_dev);
1034 usb_set_intfdata(interface, atusb);
1036 atusb->shutdown = 0;
1038 INIT_DELAYED_WORK(&atusb->work, atusb_work_urbs);
1039 init_usb_anchor(&atusb->idle_urbs);
1040 init_usb_anchor(&atusb->rx_urbs);
1042 if (atusb_alloc_urbs(atusb, ATUSB_NUM_RX_URBS))
1045 atusb->tx_dr.bRequestType = ATUSB_REQ_TO_DEV;
1046 atusb->tx_dr.bRequest = ATUSB_TX;
1047 atusb->tx_dr.wValue = cpu_to_le16(0);
1049 atusb->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1053 hw->parent = &usb_dev->dev;
1055 atusb_command(atusb, ATUSB_RF_RESET, 0);
1056 atusb_get_and_conf_chip(atusb);
1057 atusb_get_and_show_revision(atusb);
1058 atusb_get_and_show_build(atusb);
1059 atusb_set_extended_addr(atusb);
1061 if ((atusb->fw_ver_maj == 0 && atusb->fw_ver_min >= 3) || atusb->fw_ver_maj > 0)
1062 hw->flags |= IEEE802154_HW_FRAME_RETRIES;
1064 ret = atusb_get_and_clear_error(atusb);
1066 dev_err(&atusb->usb_dev->dev,
1067 "%s: initialization failed, error = %d\n",
1072 ret = ieee802154_register_hw(hw);
1076 /* If we just powered on, we're now in P_ON and need to enter TRX_OFF
1077 * explicitly. Any resets after that will send us straight to TRX_OFF,
1078 * making the command below redundant.
1080 atusb_write_reg(atusb, RG_TRX_STATE, STATE_FORCE_TRX_OFF);
1081 msleep(1); /* reset => TRX_OFF, tTR13 = 37 us */
1084 /* Calculating the maximum time available to empty the frame buffer
1087 * According to [1], the inter-frame gap is
1088 * R * 20 * 16 us + 128 us
1089 * where R is a random number from 0 to 7. Furthermore, we have 20 bit
1090 * times (80 us at 250 kbps) of SHR of the next frame before the
1091 * transceiver begins storing data in the frame buffer.
1093 * This yields a minimum time of 208 us between the last data of a
1094 * frame and the first data of the next frame. This time is further
1095 * reduced by interrupt latency in the atusb firmware.
1097 * atusb currently needs about 500 us to retrieve a maximum-sized
1098 * frame. We therefore have to allow reception of a new frame to begin
1099 * while we retrieve the previous frame.
1101 * [1] "JN-AN-1035 Calculating data rates in an IEEE 802.15.4-based
1102 * network", Jennic 2006.
1103 * http://www.jennic.com/download_file.php?supportFile=JN-AN-1035%20Calculating%20802-15-4%20Data%20Rates-1v0.pdf
1106 atusb_write_subreg(atusb, SR_RX_SAFE_MODE, 1);
1108 atusb_write_reg(atusb, RG_IRQ_MASK, 0xff);
1110 ret = atusb_get_and_clear_error(atusb);
1114 dev_err(&atusb->usb_dev->dev,
1115 "%s: setup failed, error = %d\n",
1118 ieee802154_unregister_hw(hw);
1120 atusb_free_urbs(atusb);
1121 usb_kill_urb(atusb->tx_urb);
1122 usb_free_urb(atusb->tx_urb);
1123 usb_put_dev(usb_dev);
1124 ieee802154_free_hw(hw);
1128 static void atusb_disconnect(struct usb_interface *interface)
1130 struct atusb *atusb = usb_get_intfdata(interface);
1132 dev_dbg(&atusb->usb_dev->dev, "%s\n", __func__);
1134 atusb->shutdown = 1;
1135 cancel_delayed_work_sync(&atusb->work);
1137 usb_kill_anchored_urbs(&atusb->rx_urbs);
1138 atusb_free_urbs(atusb);
1139 usb_kill_urb(atusb->tx_urb);
1140 usb_free_urb(atusb->tx_urb);
1142 ieee802154_unregister_hw(atusb->hw);
1144 usb_put_dev(atusb->usb_dev);
1146 ieee802154_free_hw(atusb->hw);
1148 usb_set_intfdata(interface, NULL);
1150 pr_debug("%s done\n", __func__);
1153 /* The devices we work with */
1154 static const struct usb_device_id atusb_device_table[] = {
1156 .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
1157 USB_DEVICE_ID_MATCH_INT_INFO,
1158 .idVendor = ATUSB_VENDOR_ID,
1159 .idProduct = ATUSB_PRODUCT_ID,
1160 .bInterfaceClass = USB_CLASS_VENDOR_SPEC
1162 /* end with null element */
1165 MODULE_DEVICE_TABLE(usb, atusb_device_table);
1167 static struct usb_driver atusb_driver = {
1169 .probe = atusb_probe,
1170 .disconnect = atusb_disconnect,
1171 .id_table = atusb_device_table,
1173 module_usb_driver(atusb_driver);
1175 MODULE_AUTHOR("Alexander Aring <alex.aring@gmail.com>");
1176 MODULE_AUTHOR("Richard Sharpe <realrichardsharpe@gmail.com>");
1177 MODULE_AUTHOR("Stefan Schmidt <stefan@datenfreihafen.org>");
1178 MODULE_AUTHOR("Werner Almesberger <werner@almesberger.net>");
1179 MODULE_AUTHOR("Josef Filzmaier <j.filzmaier@gmx.at>");
1180 MODULE_DESCRIPTION("ATUSB IEEE 802.15.4 Driver");
1181 MODULE_LICENSE("GPL");