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);
100 ret = ret < 0 ? ret : -ENODATA;
103 dev_err(&usb_dev->dev,
104 "%s: req 0x%02x val 0x%x idx 0x%x, error %d\n",
105 __func__, request, value, index, ret);
110 static int atusb_command(struct atusb *atusb, u8 cmd, u8 arg)
112 struct usb_device *usb_dev = atusb->usb_dev;
114 dev_dbg(&usb_dev->dev, "%s: cmd = 0x%x\n", __func__, cmd);
115 return atusb_control_msg(atusb, usb_sndctrlpipe(usb_dev, 0),
116 cmd, ATUSB_REQ_TO_DEV, arg, 0, NULL, 0, 1000);
119 static int atusb_write_reg(struct atusb *atusb, u8 reg, u8 value)
121 struct usb_device *usb_dev = atusb->usb_dev;
123 dev_dbg(&usb_dev->dev, "%s: 0x%02x <- 0x%02x\n", __func__, reg, value);
124 return atusb_control_msg(atusb, usb_sndctrlpipe(usb_dev, 0),
125 ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
126 value, reg, NULL, 0, 1000);
129 static int atusb_read_reg(struct atusb *atusb, u8 reg)
131 struct usb_device *usb_dev = atusb->usb_dev;
136 buffer = kmalloc(1, GFP_KERNEL);
140 dev_dbg(&usb_dev->dev, "%s: reg = 0x%x\n", __func__, reg);
141 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
142 ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
143 0, reg, buffer, 1, 1000);
155 static int atusb_write_subreg(struct atusb *atusb, u8 reg, u8 mask,
158 struct usb_device *usb_dev = atusb->usb_dev;
162 dev_dbg(&usb_dev->dev, "%s: 0x%02x <- 0x%02x\n", __func__, reg, value);
164 orig = atusb_read_reg(atusb, reg);
166 /* Write the value only into that part of the register which is allowed
167 * by the mask. All other bits stay as before.
170 tmp |= (value << shift) & mask;
173 ret = atusb_write_reg(atusb, reg, tmp);
178 static int atusb_read_subreg(struct atusb *lp,
179 unsigned int addr, unsigned int mask,
184 rc = atusb_read_reg(lp, addr);
185 rc = (rc & mask) >> shift;
190 static int atusb_get_and_clear_error(struct atusb *atusb)
192 int err = atusb->err;
198 /* ----- skb allocation ---------------------------------------------------- */
201 #define MAX_RX_XFER (1 + MAX_PSDU + 2 + 1) /* PHR+PSDU+CRC+LQI */
203 #define SKB_ATUSB(skb) (*(struct atusb **)(skb)->cb)
205 static void atusb_in(struct urb *urb);
207 static int atusb_submit_rx_urb(struct atusb *atusb, struct urb *urb)
209 struct usb_device *usb_dev = atusb->usb_dev;
210 struct sk_buff *skb = urb->context;
214 skb = alloc_skb(MAX_RX_XFER, GFP_KERNEL);
216 dev_warn_ratelimited(&usb_dev->dev,
217 "atusb_in: can't allocate skb\n");
220 skb_put(skb, MAX_RX_XFER);
221 SKB_ATUSB(skb) = atusb;
224 usb_fill_bulk_urb(urb, usb_dev, usb_rcvbulkpipe(usb_dev, 1),
225 skb->data, MAX_RX_XFER, atusb_in, skb);
226 usb_anchor_urb(urb, &atusb->rx_urbs);
228 ret = usb_submit_urb(urb, GFP_KERNEL);
230 usb_unanchor_urb(urb);
237 static void atusb_work_urbs(struct work_struct *work)
239 struct atusb *atusb =
240 container_of(to_delayed_work(work), struct atusb, work);
241 struct usb_device *usb_dev = atusb->usb_dev;
249 urb = usb_get_from_anchor(&atusb->idle_urbs);
252 ret = atusb_submit_rx_urb(atusb, urb);
255 usb_anchor_urb(urb, &atusb->idle_urbs);
256 dev_warn_ratelimited(&usb_dev->dev,
257 "atusb_in: can't allocate/submit URB (%d)\n", ret);
258 schedule_delayed_work(&atusb->work,
259 msecs_to_jiffies(ATUSB_ALLOC_DELAY_MS) + 1);
262 /* ----- Asynchronous USB -------------------------------------------------- */
264 static void atusb_tx_done(struct atusb *atusb, u8 seq)
266 struct usb_device *usb_dev = atusb->usb_dev;
267 u8 expect = atusb->tx_ack_seq;
269 dev_dbg(&usb_dev->dev, "%s (0x%02x/0x%02x)\n", __func__, seq, expect);
271 /* TODO check for ifs handling in firmware */
272 ieee802154_xmit_complete(atusb->hw, atusb->tx_skb, false);
274 /* TODO I experience this case when atusb has a tx complete
275 * irq before probing, we should fix the firmware it's an
276 * unlikely case now that seq == expect is then true, but can
277 * happen and fail with a tx_skb = NULL;
279 ieee802154_wake_queue(atusb->hw);
281 dev_kfree_skb_irq(atusb->tx_skb);
285 static void atusb_in_good(struct urb *urb)
287 struct usb_device *usb_dev = urb->dev;
288 struct sk_buff *skb = urb->context;
289 struct atusb *atusb = SKB_ATUSB(skb);
292 if (!urb->actual_length) {
293 dev_dbg(&usb_dev->dev, "atusb_in: zero-sized URB ?\n");
299 if (urb->actual_length == 1) {
300 atusb_tx_done(atusb, len);
304 if (len + 1 > urb->actual_length - 1) {
305 dev_dbg(&usb_dev->dev, "atusb_in: frame len %d+1 > URB %u-1\n",
306 len, urb->actual_length);
310 if (!ieee802154_is_valid_psdu_len(len)) {
311 dev_dbg(&usb_dev->dev, "atusb_in: frame corrupted\n");
315 lqi = skb->data[len + 1];
316 dev_dbg(&usb_dev->dev, "atusb_in: rx len %d lqi 0x%02x\n", len, lqi);
317 skb_pull(skb, 1); /* remove PHR */
318 skb_trim(skb, len); /* get payload only */
319 ieee802154_rx_irqsafe(atusb->hw, skb, lqi);
320 urb->context = NULL; /* skb is gone */
323 static void atusb_in(struct urb *urb)
325 struct usb_device *usb_dev = urb->dev;
326 struct sk_buff *skb = urb->context;
327 struct atusb *atusb = SKB_ATUSB(skb);
329 dev_dbg(&usb_dev->dev, "%s: status %d len %d\n", __func__,
330 urb->status, urb->actual_length);
332 if (urb->status == -ENOENT) { /* being killed */
337 dev_dbg(&usb_dev->dev, "%s: URB error %d\n", __func__, urb->status);
342 usb_anchor_urb(urb, &atusb->idle_urbs);
343 if (!atusb->shutdown)
344 schedule_delayed_work(&atusb->work, 0);
347 /* ----- URB allocation/deallocation --------------------------------------- */
349 static void atusb_free_urbs(struct atusb *atusb)
354 urb = usb_get_from_anchor(&atusb->idle_urbs);
357 kfree_skb(urb->context);
362 static int atusb_alloc_urbs(struct atusb *atusb, int n)
367 urb = usb_alloc_urb(0, GFP_KERNEL);
369 atusb_free_urbs(atusb);
372 usb_anchor_urb(urb, &atusb->idle_urbs);
379 /* ----- IEEE 802.15.4 interface operations -------------------------------- */
381 static void atusb_xmit_complete(struct urb *urb)
383 dev_dbg(&urb->dev->dev, "atusb_xmit urb completed");
386 static int atusb_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
388 struct atusb *atusb = hw->priv;
389 struct usb_device *usb_dev = atusb->usb_dev;
392 dev_dbg(&usb_dev->dev, "%s (%d)\n", __func__, skb->len);
395 atusb->tx_dr.wIndex = cpu_to_le16(atusb->tx_ack_seq);
396 atusb->tx_dr.wLength = cpu_to_le16(skb->len);
398 usb_fill_control_urb(atusb->tx_urb, usb_dev,
399 usb_sndctrlpipe(usb_dev, 0),
400 (unsigned char *)&atusb->tx_dr, skb->data,
401 skb->len, atusb_xmit_complete, NULL);
402 ret = usb_submit_urb(atusb->tx_urb, GFP_ATOMIC);
403 dev_dbg(&usb_dev->dev, "%s done (%d)\n", __func__, ret);
407 static int atusb_ed(struct ieee802154_hw *hw, u8 *level)
414 static int atusb_set_hw_addr_filt(struct ieee802154_hw *hw,
415 struct ieee802154_hw_addr_filt *filt,
416 unsigned long changed)
418 struct atusb *atusb = hw->priv;
419 struct device *dev = &atusb->usb_dev->dev;
421 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
422 u16 addr = le16_to_cpu(filt->short_addr);
424 dev_vdbg(dev, "%s called for saddr\n", __func__);
425 atusb_write_reg(atusb, RG_SHORT_ADDR_0, addr);
426 atusb_write_reg(atusb, RG_SHORT_ADDR_1, addr >> 8);
429 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
430 u16 pan = le16_to_cpu(filt->pan_id);
432 dev_vdbg(dev, "%s called for pan id\n", __func__);
433 atusb_write_reg(atusb, RG_PAN_ID_0, pan);
434 atusb_write_reg(atusb, RG_PAN_ID_1, pan >> 8);
437 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
438 u8 i, addr[IEEE802154_EXTENDED_ADDR_LEN];
440 memcpy(addr, &filt->ieee_addr, IEEE802154_EXTENDED_ADDR_LEN);
441 dev_vdbg(dev, "%s called for IEEE addr\n", __func__);
442 for (i = 0; i < 8; i++)
443 atusb_write_reg(atusb, RG_IEEE_ADDR_0 + i, addr[i]);
446 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
447 dev_vdbg(dev, "%s called for panc change\n", __func__);
449 atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 1);
451 atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 0);
454 return atusb_get_and_clear_error(atusb);
457 static int atusb_start(struct ieee802154_hw *hw)
459 struct atusb *atusb = hw->priv;
460 struct usb_device *usb_dev = atusb->usb_dev;
463 dev_dbg(&usb_dev->dev, "%s\n", __func__);
464 schedule_delayed_work(&atusb->work, 0);
465 atusb_command(atusb, ATUSB_RX_MODE, 1);
466 ret = atusb_get_and_clear_error(atusb);
468 usb_kill_anchored_urbs(&atusb->idle_urbs);
472 static void atusb_stop(struct ieee802154_hw *hw)
474 struct atusb *atusb = hw->priv;
475 struct usb_device *usb_dev = atusb->usb_dev;
477 dev_dbg(&usb_dev->dev, "%s\n", __func__);
478 usb_kill_anchored_urbs(&atusb->idle_urbs);
479 atusb_command(atusb, ATUSB_RX_MODE, 0);
480 atusb_get_and_clear_error(atusb);
483 #define ATUSB_MAX_TX_POWERS 0xF
484 static const s32 atusb_powers[ATUSB_MAX_TX_POWERS + 1] = {
485 300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700,
490 atusb_txpower(struct ieee802154_hw *hw, s32 mbm)
492 struct atusb *atusb = hw->priv;
495 return atusb->data->set_txpower(hw, mbm);
501 atusb_set_txpower(struct ieee802154_hw *hw, s32 mbm)
503 struct atusb *atusb = hw->priv;
506 for (i = 0; i < hw->phy->supported.tx_powers_size; i++) {
507 if (hw->phy->supported.tx_powers[i] == mbm)
508 return atusb_write_subreg(atusb, SR_TX_PWR_23X, i);
515 hulusb_set_txpower(struct ieee802154_hw *hw, s32 mbm)
519 for (i = 0; i < hw->phy->supported.tx_powers_size; i++) {
520 if (hw->phy->supported.tx_powers[i] == mbm)
521 return atusb_write_subreg(hw->priv, SR_TX_PWR_212, i);
527 #define ATUSB_MAX_ED_LEVELS 0xF
528 static const s32 atusb_ed_levels[ATUSB_MAX_ED_LEVELS + 1] = {
529 -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300,
530 -7100, -6900, -6700, -6500, -6300, -6100,
533 #define AT86RF212_MAX_TX_POWERS 0x1F
534 static const s32 at86rf212_powers[AT86RF212_MAX_TX_POWERS + 1] = {
535 500, 400, 300, 200, 100, 0, -100, -200, -300, -400, -500, -600, -700,
536 -800, -900, -1000, -1100, -1200, -1300, -1400, -1500, -1600, -1700,
537 -1800, -1900, -2000, -2100, -2200, -2300, -2400, -2500, -2600,
540 #define AT86RF2XX_MAX_ED_LEVELS 0xF
541 static const s32 at86rf212_ed_levels_100[AT86RF2XX_MAX_ED_LEVELS + 1] = {
542 -10000, -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200,
543 -8000, -7800, -7600, -7400, -7200, -7000,
546 static const s32 at86rf212_ed_levels_98[AT86RF2XX_MAX_ED_LEVELS + 1] = {
547 -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200, -8000,
548 -7800, -7600, -7400, -7200, -7000, -6800,
552 atusb_set_cca_mode(struct ieee802154_hw *hw, const struct wpan_phy_cca *cca)
554 struct atusb *atusb = hw->priv;
557 /* mapping 802.15.4 to driver spec */
559 case NL802154_CCA_ENERGY:
562 case NL802154_CCA_CARRIER:
565 case NL802154_CCA_ENERGY_CARRIER:
567 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
570 case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
581 return atusb_write_subreg(atusb, SR_CCA_MODE, val);
584 static int hulusb_set_cca_ed_level(struct atusb *lp, int rssi_base_val)
586 unsigned int cca_ed_thres;
588 cca_ed_thres = atusb_read_subreg(lp, SR_CCA_ED_THRES);
590 switch (rssi_base_val) {
592 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_98;
593 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_98);
594 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_98[cca_ed_thres];
597 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
598 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
599 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_100[cca_ed_thres];
609 atusb_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
611 struct atusb *atusb = hw->priv;
614 for (i = 0; i < hw->phy->supported.cca_ed_levels_size; i++) {
615 if (hw->phy->supported.cca_ed_levels[i] == mbm)
616 return atusb_write_subreg(atusb, SR_CCA_ED_THRES, i);
622 static int atusb_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
624 struct atusb *atusb = hw->priv;
628 ret = atusb->data->set_channel(hw, page, channel);
629 /* @@@ ugly synchronization */
630 msleep(atusb->data->t_channel_switch);
636 static int atusb_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
638 struct atusb *atusb = hw->priv;
641 ret = atusb_write_subreg(atusb, SR_CHANNEL, channel);
647 static int hulusb_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
652 struct atusb *lp = hw->priv;
655 rc = atusb_write_subreg(lp, SR_SUB_MODE, 0);
657 rc = atusb_write_subreg(lp, SR_SUB_MODE, 1);
662 rc = atusb_write_subreg(lp, SR_BPSK_QPSK, 0);
663 rssi_base_val = -100;
665 rc = atusb_write_subreg(lp, SR_BPSK_QPSK, 1);
671 rc = hulusb_set_cca_ed_level(lp, rssi_base_val);
675 /* This sets the symbol_duration according frequency on the 212.
676 * TODO move this handling while set channel and page in cfg802154.
677 * We can do that, this timings are according 802.15.4 standard.
678 * If we do that in cfg802154, this is a more generic calculation.
680 * This should also protected from ifs_timer. Means cancel timer and
681 * init with a new value. For now, this is okay.
685 /* SUB:0 and BPSK:0 -> BPSK-20 */
686 lp->hw->phy->symbol_duration = 50;
688 /* SUB:1 and BPSK:0 -> BPSK-40 */
689 lp->hw->phy->symbol_duration = 25;
693 /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */
694 lp->hw->phy->symbol_duration = 40;
696 /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */
697 lp->hw->phy->symbol_duration = 16;
700 lp->hw->phy->lifs_period = IEEE802154_LIFS_PERIOD *
701 lp->hw->phy->symbol_duration;
702 lp->hw->phy->sifs_period = IEEE802154_SIFS_PERIOD *
703 lp->hw->phy->symbol_duration;
705 return atusb_write_subreg(lp, SR_CHANNEL, channel);
709 atusb_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be, u8 retries)
711 struct atusb *atusb = hw->priv;
714 ret = atusb_write_subreg(atusb, SR_MIN_BE, min_be);
718 ret = atusb_write_subreg(atusb, SR_MAX_BE, max_be);
722 return atusb_write_subreg(atusb, SR_MAX_CSMA_RETRIES, retries);
726 hulusb_set_lbt(struct ieee802154_hw *hw, bool on)
728 struct atusb *atusb = hw->priv;
730 return atusb_write_subreg(atusb, SR_CSMA_LBT_MODE, on);
734 atusb_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
736 struct atusb *atusb = hw->priv;
738 return atusb_write_subreg(atusb, SR_MAX_FRAME_RETRIES, retries);
742 atusb_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
744 struct atusb *atusb = hw->priv;
748 ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 1);
752 ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 1);
756 ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 0);
760 ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 0);
768 static struct atusb_chip_data atusb_chip_data = {
769 .t_channel_switch = 1,
770 .rssi_base_val = -91,
771 .set_txpower = atusb_set_txpower,
772 .set_channel = atusb_set_channel,
775 static struct atusb_chip_data hulusb_chip_data = {
776 .t_channel_switch = 11,
777 .rssi_base_val = -100,
778 .set_txpower = hulusb_set_txpower,
779 .set_channel = hulusb_set_channel,
782 static const struct ieee802154_ops atusb_ops = {
783 .owner = THIS_MODULE,
784 .xmit_async = atusb_xmit,
786 .set_channel = atusb_channel,
787 .start = atusb_start,
789 .set_hw_addr_filt = atusb_set_hw_addr_filt,
790 .set_txpower = atusb_txpower,
791 .set_lbt = hulusb_set_lbt,
792 .set_cca_mode = atusb_set_cca_mode,
793 .set_cca_ed_level = atusb_set_cca_ed_level,
794 .set_csma_params = atusb_set_csma_params,
795 .set_frame_retries = atusb_set_frame_retries,
796 .set_promiscuous_mode = atusb_set_promiscuous_mode,
799 /* ----- Firmware and chip version information ----------------------------- */
801 static int atusb_get_and_show_revision(struct atusb *atusb)
803 struct usb_device *usb_dev = atusb->usb_dev;
805 unsigned char *buffer;
808 buffer = kmalloc(3, GFP_KERNEL);
812 /* Get a couple of the ATMega Firmware values */
813 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
814 ATUSB_ID, ATUSB_REQ_FROM_DEV, 0, 0,
817 atusb->fw_ver_maj = buffer[0];
818 atusb->fw_ver_min = buffer[1];
819 atusb->fw_hw_type = buffer[2];
821 switch (atusb->fw_hw_type) {
822 case ATUSB_HW_TYPE_100813:
823 case ATUSB_HW_TYPE_101216:
824 case ATUSB_HW_TYPE_110131:
826 atusb->data = &atusb_chip_data;
828 case ATUSB_HW_TYPE_RZUSB:
830 atusb->data = &atusb_chip_data;
832 case ATUSB_HW_TYPE_HULUSB:
834 atusb->data = &hulusb_chip_data;
838 atusb->err = -ENOTSUPP;
843 dev_info(&usb_dev->dev,
844 "Firmware: major: %u, minor: %u, hardware type: %s (%d)\n",
845 atusb->fw_ver_maj, atusb->fw_ver_min, hw_name,
848 if (atusb->fw_ver_maj == 0 && atusb->fw_ver_min < 2) {
849 dev_info(&usb_dev->dev,
850 "Firmware version (%u.%u) predates our first public release.",
851 atusb->fw_ver_maj, atusb->fw_ver_min);
852 dev_info(&usb_dev->dev, "Please update to version 0.2 or newer");
859 static int atusb_get_and_show_build(struct atusb *atusb)
861 struct usb_device *usb_dev = atusb->usb_dev;
865 build = kmalloc(ATUSB_BUILD_SIZE + 1, GFP_KERNEL);
869 /* We cannot call atusb_control_msg() here, since this request may read various length data */
870 ret = usb_control_msg(atusb->usb_dev, usb_rcvctrlpipe(usb_dev, 0), ATUSB_BUILD,
871 ATUSB_REQ_FROM_DEV, 0, 0, build, ATUSB_BUILD_SIZE, 1000);
874 dev_info(&usb_dev->dev, "Firmware: build %s\n", build);
881 static int atusb_get_and_conf_chip(struct atusb *atusb)
883 struct usb_device *usb_dev = atusb->usb_dev;
884 u8 man_id_0, man_id_1, part_num, version_num;
886 struct ieee802154_hw *hw = atusb->hw;
888 man_id_0 = atusb_read_reg(atusb, RG_MAN_ID_0);
889 man_id_1 = atusb_read_reg(atusb, RG_MAN_ID_1);
890 part_num = atusb_read_reg(atusb, RG_PART_NUM);
891 version_num = atusb_read_reg(atusb, RG_VERSION_NUM);
896 hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AFILT |
897 IEEE802154_HW_PROMISCUOUS | IEEE802154_HW_CSMA_PARAMS;
899 hw->phy->flags = WPAN_PHY_FLAG_TXPOWER | WPAN_PHY_FLAG_CCA_ED_LEVEL |
900 WPAN_PHY_FLAG_CCA_MODE;
902 hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) |
903 BIT(NL802154_CCA_CARRIER) |
904 BIT(NL802154_CCA_ENERGY_CARRIER);
905 hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) |
906 BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR);
908 hw->phy->cca.mode = NL802154_CCA_ENERGY;
910 hw->phy->current_page = 0;
912 if ((man_id_1 << 8 | man_id_0) != ATUSB_JEDEC_ATMEL) {
913 dev_err(&usb_dev->dev,
914 "non-Atmel transceiver xxxx%02x%02x\n",
922 atusb->hw->phy->supported.channels[0] = 0x7FFF800;
923 atusb->hw->phy->current_channel = 11; /* reset default */
924 atusb->hw->phy->symbol_duration = 16;
925 atusb->hw->phy->supported.tx_powers = atusb_powers;
926 atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers);
927 hw->phy->supported.cca_ed_levels = atusb_ed_levels;
928 hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels);
932 atusb->hw->phy->supported.channels[0] = 0x7FFF800;
933 atusb->hw->phy->current_channel = 11; /* reset default */
934 atusb->hw->phy->symbol_duration = 16;
935 atusb->hw->phy->supported.tx_powers = atusb_powers;
936 atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers);
937 hw->phy->supported.cca_ed_levels = atusb_ed_levels;
938 hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels);
942 atusb->hw->flags |= IEEE802154_HW_LBT;
943 atusb->hw->phy->supported.channels[0] = 0x00007FF;
944 atusb->hw->phy->supported.channels[2] = 0x00007FF;
945 atusb->hw->phy->current_channel = 5;
946 atusb->hw->phy->symbol_duration = 25;
947 atusb->hw->phy->supported.lbt = NL802154_SUPPORTED_BOOL_BOTH;
948 atusb->hw->phy->supported.tx_powers = at86rf212_powers;
949 atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf212_powers);
950 atusb->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
951 atusb->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
954 dev_err(&usb_dev->dev,
955 "unexpected transceiver, part 0x%02x version 0x%02x\n",
956 part_num, version_num);
960 hw->phy->transmit_power = hw->phy->supported.tx_powers[0];
961 hw->phy->cca_ed_level = hw->phy->supported.cca_ed_levels[7];
963 dev_info(&usb_dev->dev, "ATUSB: %s version %d\n", chip, version_num);
968 atusb->err = -ENODEV;
972 static int atusb_set_extended_addr(struct atusb *atusb)
974 struct usb_device *usb_dev = atusb->usb_dev;
975 unsigned char *buffer;
976 __le64 extended_addr;
980 /* Firmware versions before 0.3 do not support the EUI64_READ command.
981 * Just use a random address and be done.
983 if (atusb->fw_ver_maj == 0 && atusb->fw_ver_min < 3) {
984 ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
988 buffer = kmalloc(IEEE802154_EXTENDED_ADDR_LEN, GFP_KERNEL);
992 /* Firmware is new enough so we fetch the address from EEPROM */
993 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
994 ATUSB_EUI64_READ, ATUSB_REQ_FROM_DEV, 0, 0,
995 buffer, IEEE802154_EXTENDED_ADDR_LEN, 1000);
997 dev_err(&usb_dev->dev, "failed to fetch extended address, random address set\n");
998 ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
1003 memcpy(&extended_addr, buffer, IEEE802154_EXTENDED_ADDR_LEN);
1004 /* Check if read address is not empty and the unicast bit is set correctly */
1005 if (!ieee802154_is_valid_extended_unicast_addr(extended_addr)) {
1006 dev_info(&usb_dev->dev, "no permanent extended address found, random address set\n");
1007 ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
1009 atusb->hw->phy->perm_extended_addr = extended_addr;
1010 addr = swab64((__force u64)atusb->hw->phy->perm_extended_addr);
1011 dev_info(&usb_dev->dev, "Read permanent extended address %8phC from device\n",
1019 /* ----- Setup ------------------------------------------------------------- */
1021 static int atusb_probe(struct usb_interface *interface,
1022 const struct usb_device_id *id)
1024 struct usb_device *usb_dev = interface_to_usbdev(interface);
1025 struct ieee802154_hw *hw;
1026 struct atusb *atusb = NULL;
1029 hw = ieee802154_alloc_hw(sizeof(struct atusb), &atusb_ops);
1035 atusb->usb_dev = usb_get_dev(usb_dev);
1036 usb_set_intfdata(interface, atusb);
1038 atusb->shutdown = 0;
1040 INIT_DELAYED_WORK(&atusb->work, atusb_work_urbs);
1041 init_usb_anchor(&atusb->idle_urbs);
1042 init_usb_anchor(&atusb->rx_urbs);
1044 if (atusb_alloc_urbs(atusb, ATUSB_NUM_RX_URBS))
1047 atusb->tx_dr.bRequestType = ATUSB_REQ_TO_DEV;
1048 atusb->tx_dr.bRequest = ATUSB_TX;
1049 atusb->tx_dr.wValue = cpu_to_le16(0);
1051 atusb->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1055 hw->parent = &usb_dev->dev;
1057 atusb_command(atusb, ATUSB_RF_RESET, 0);
1058 atusb_get_and_conf_chip(atusb);
1059 atusb_get_and_show_revision(atusb);
1060 atusb_get_and_show_build(atusb);
1061 atusb_set_extended_addr(atusb);
1063 if ((atusb->fw_ver_maj == 0 && atusb->fw_ver_min >= 3) || atusb->fw_ver_maj > 0)
1064 hw->flags |= IEEE802154_HW_FRAME_RETRIES;
1066 ret = atusb_get_and_clear_error(atusb);
1068 dev_err(&atusb->usb_dev->dev,
1069 "%s: initialization failed, error = %d\n",
1074 ret = ieee802154_register_hw(hw);
1078 /* If we just powered on, we're now in P_ON and need to enter TRX_OFF
1079 * explicitly. Any resets after that will send us straight to TRX_OFF,
1080 * making the command below redundant.
1082 atusb_write_reg(atusb, RG_TRX_STATE, STATE_FORCE_TRX_OFF);
1083 msleep(1); /* reset => TRX_OFF, tTR13 = 37 us */
1086 /* Calculating the maximum time available to empty the frame buffer
1089 * According to [1], the inter-frame gap is
1090 * R * 20 * 16 us + 128 us
1091 * where R is a random number from 0 to 7. Furthermore, we have 20 bit
1092 * times (80 us at 250 kbps) of SHR of the next frame before the
1093 * transceiver begins storing data in the frame buffer.
1095 * This yields a minimum time of 208 us between the last data of a
1096 * frame and the first data of the next frame. This time is further
1097 * reduced by interrupt latency in the atusb firmware.
1099 * atusb currently needs about 500 us to retrieve a maximum-sized
1100 * frame. We therefore have to allow reception of a new frame to begin
1101 * while we retrieve the previous frame.
1103 * [1] "JN-AN-1035 Calculating data rates in an IEEE 802.15.4-based
1104 * network", Jennic 2006.
1105 * http://www.jennic.com/download_file.php?supportFile=JN-AN-1035%20Calculating%20802-15-4%20Data%20Rates-1v0.pdf
1108 atusb_write_subreg(atusb, SR_RX_SAFE_MODE, 1);
1110 atusb_write_reg(atusb, RG_IRQ_MASK, 0xff);
1112 ret = atusb_get_and_clear_error(atusb);
1116 dev_err(&atusb->usb_dev->dev,
1117 "%s: setup failed, error = %d\n",
1120 ieee802154_unregister_hw(hw);
1122 atusb_free_urbs(atusb);
1123 usb_kill_urb(atusb->tx_urb);
1124 usb_free_urb(atusb->tx_urb);
1125 usb_put_dev(usb_dev);
1126 ieee802154_free_hw(hw);
1130 static void atusb_disconnect(struct usb_interface *interface)
1132 struct atusb *atusb = usb_get_intfdata(interface);
1134 dev_dbg(&atusb->usb_dev->dev, "%s\n", __func__);
1136 atusb->shutdown = 1;
1137 cancel_delayed_work_sync(&atusb->work);
1139 usb_kill_anchored_urbs(&atusb->rx_urbs);
1140 atusb_free_urbs(atusb);
1141 usb_kill_urb(atusb->tx_urb);
1142 usb_free_urb(atusb->tx_urb);
1144 ieee802154_unregister_hw(atusb->hw);
1146 usb_put_dev(atusb->usb_dev);
1148 ieee802154_free_hw(atusb->hw);
1150 usb_set_intfdata(interface, NULL);
1152 pr_debug("%s done\n", __func__);
1155 /* The devices we work with */
1156 static const struct usb_device_id atusb_device_table[] = {
1158 .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
1159 USB_DEVICE_ID_MATCH_INT_INFO,
1160 .idVendor = ATUSB_VENDOR_ID,
1161 .idProduct = ATUSB_PRODUCT_ID,
1162 .bInterfaceClass = USB_CLASS_VENDOR_SPEC
1164 /* end with null element */
1167 MODULE_DEVICE_TABLE(usb, atusb_device_table);
1169 static struct usb_driver atusb_driver = {
1171 .probe = atusb_probe,
1172 .disconnect = atusb_disconnect,
1173 .id_table = atusb_device_table,
1175 module_usb_driver(atusb_driver);
1177 MODULE_AUTHOR("Alexander Aring <alex.aring@gmail.com>");
1178 MODULE_AUTHOR("Richard Sharpe <realrichardsharpe@gmail.com>");
1179 MODULE_AUTHOR("Stefan Schmidt <stefan@datenfreihafen.org>");
1180 MODULE_AUTHOR("Werner Almesberger <werner@almesberger.net>");
1181 MODULE_AUTHOR("Josef Filzmaier <j.filzmaier@gmx.at>");
1182 MODULE_DESCRIPTION("ATUSB IEEE 802.15.4 Driver");
1183 MODULE_LICENSE("GPL");