1 // SPDX-License-Identifier: GPL-2.0-only
3 * atusb.c - Driver for the ATUSB IEEE 802.15.4 dongle
5 * Written 2013 by Werner Almesberger <werner@almesberger.net>
7 * Copyright (c) 2015 - 2016 Stefan Schmidt <stefan@datenfreihafen.org>
9 * Based on at86rf230.c and spi_atusb.c.
11 * Copyright (C) 2009 Siemens AG
12 * Written by: Dmitry Eremin-Solenikov <dmitry.baryshkov@siemens.com>
15 * Copyright (c) 2011 Richard Sharpe <realrichardsharpe@gmail.com>
16 * Copyright (c) 2011 Stefan Schmidt <stefan@datenfreihafen.org>
17 * Copyright (c) 2011 Werner Almesberger <werner@almesberger.net>
19 * USB initialization is
20 * Copyright (c) 2013 Alexander Aring <alex.aring@gmail.com>
22 * Busware HUL support is
23 * Copyright (c) 2017 Josef Filzmaier <j.filzmaier@gmx.at>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/module.h>
29 #include <linux/jiffies.h>
30 #include <linux/usb.h>
31 #include <linux/skbuff.h>
33 #include <net/cfg802154.h>
34 #include <net/mac802154.h>
36 #include "at86rf230.h"
39 #define ATUSB_JEDEC_ATMEL 0x1f /* JEDEC manufacturer ID */
41 #define ATUSB_NUM_RX_URBS 4 /* allow for a bit of local latency */
42 #define ATUSB_ALLOC_DELAY_MS 100 /* delay after failed allocation */
43 #define ATUSB_TX_TIMEOUT_MS 200 /* on the air timeout */
46 struct ieee802154_hw *hw;
47 struct usb_device *usb_dev;
48 struct atusb_chip_data *data;
49 int shutdown; /* non-zero if shutting down */
50 int err; /* set by first error */
53 struct delayed_work work; /* memory allocations */
54 struct usb_anchor idle_urbs; /* URBs waiting to be submitted */
55 struct usb_anchor rx_urbs; /* URBs waiting for reception */
58 struct usb_ctrlrequest tx_dr;
60 struct sk_buff *tx_skb;
61 u8 tx_ack_seq; /* current TX ACK sequence number */
63 /* Firmware variable */
64 unsigned char fw_ver_maj; /* Firmware major version number */
65 unsigned char fw_ver_min; /* Firmware minor version number */
66 unsigned char fw_hw_type; /* Firmware hardware type */
69 struct atusb_chip_data {
73 int (*set_channel)(struct ieee802154_hw*, u8, u8);
74 int (*set_txpower)(struct ieee802154_hw*, s32);
77 static int atusb_write_subreg(struct atusb *atusb, u8 reg, u8 mask,
80 struct usb_device *usb_dev = atusb->usb_dev;
84 dev_dbg(&usb_dev->dev, "%s: 0x%02x <- 0x%02x\n", __func__, reg, value);
86 ret = usb_control_msg_recv(usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
87 0, reg, &orig, 1, 1000, GFP_KERNEL);
91 /* Write the value only into that part of the register which is allowed
92 * by the mask. All other bits stay as before.
95 tmp |= (value << shift) & mask;
98 ret = usb_control_msg_send(usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
99 tmp, reg, NULL, 0, 1000, GFP_KERNEL);
104 static int atusb_read_subreg(struct atusb *lp,
105 unsigned int addr, unsigned int mask,
110 ret = usb_control_msg_recv(lp->usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
111 0, addr, ®, 1, 1000, GFP_KERNEL);
115 reg = (reg & mask) >> shift;
120 static int atusb_get_and_clear_error(struct atusb *atusb)
122 int err = atusb->err;
128 /* ----- skb allocation ---------------------------------------------------- */
131 #define MAX_RX_XFER (1 + MAX_PSDU + 2 + 1) /* PHR+PSDU+CRC+LQI */
133 #define SKB_ATUSB(skb) (*(struct atusb **)(skb)->cb)
135 static void atusb_in(struct urb *urb);
137 static int atusb_submit_rx_urb(struct atusb *atusb, struct urb *urb)
139 struct usb_device *usb_dev = atusb->usb_dev;
140 struct sk_buff *skb = urb->context;
144 skb = alloc_skb(MAX_RX_XFER, GFP_KERNEL);
146 dev_warn_ratelimited(&usb_dev->dev,
147 "atusb_in: can't allocate skb\n");
150 skb_put(skb, MAX_RX_XFER);
151 SKB_ATUSB(skb) = atusb;
154 usb_fill_bulk_urb(urb, usb_dev, usb_rcvbulkpipe(usb_dev, 1),
155 skb->data, MAX_RX_XFER, atusb_in, skb);
156 usb_anchor_urb(urb, &atusb->rx_urbs);
158 ret = usb_submit_urb(urb, GFP_KERNEL);
160 usb_unanchor_urb(urb);
167 static void atusb_work_urbs(struct work_struct *work)
169 struct atusb *atusb =
170 container_of(to_delayed_work(work), struct atusb, work);
171 struct usb_device *usb_dev = atusb->usb_dev;
179 urb = usb_get_from_anchor(&atusb->idle_urbs);
182 ret = atusb_submit_rx_urb(atusb, urb);
185 usb_anchor_urb(urb, &atusb->idle_urbs);
186 dev_warn_ratelimited(&usb_dev->dev,
187 "atusb_in: can't allocate/submit URB (%d)\n", ret);
188 schedule_delayed_work(&atusb->work,
189 msecs_to_jiffies(ATUSB_ALLOC_DELAY_MS) + 1);
192 /* ----- Asynchronous USB -------------------------------------------------- */
194 static void atusb_tx_done(struct atusb *atusb, u8 seq)
196 struct usb_device *usb_dev = atusb->usb_dev;
197 u8 expect = atusb->tx_ack_seq;
199 dev_dbg(&usb_dev->dev, "%s (0x%02x/0x%02x)\n", __func__, seq, expect);
201 /* TODO check for ifs handling in firmware */
202 ieee802154_xmit_complete(atusb->hw, atusb->tx_skb, false);
204 /* TODO I experience this case when atusb has a tx complete
205 * irq before probing, we should fix the firmware it's an
206 * unlikely case now that seq == expect is then true, but can
207 * happen and fail with a tx_skb = NULL;
209 ieee802154_xmit_hw_error(atusb->hw, atusb->tx_skb);
213 static void atusb_in_good(struct urb *urb)
215 struct usb_device *usb_dev = urb->dev;
216 struct sk_buff *skb = urb->context;
217 struct atusb *atusb = SKB_ATUSB(skb);
220 if (!urb->actual_length) {
221 dev_dbg(&usb_dev->dev, "atusb_in: zero-sized URB ?\n");
227 if (urb->actual_length == 1) {
228 atusb_tx_done(atusb, len);
232 if (len + 1 > urb->actual_length - 1) {
233 dev_dbg(&usb_dev->dev, "atusb_in: frame len %d+1 > URB %u-1\n",
234 len, urb->actual_length);
238 if (!ieee802154_is_valid_psdu_len(len)) {
239 dev_dbg(&usb_dev->dev, "atusb_in: frame corrupted\n");
243 lqi = skb->data[len + 1];
244 dev_dbg(&usb_dev->dev, "atusb_in: rx len %d lqi 0x%02x\n", len, lqi);
245 skb_pull(skb, 1); /* remove PHR */
246 skb_trim(skb, len); /* get payload only */
247 ieee802154_rx_irqsafe(atusb->hw, skb, lqi);
248 urb->context = NULL; /* skb is gone */
251 static void atusb_in(struct urb *urb)
253 struct usb_device *usb_dev = urb->dev;
254 struct sk_buff *skb = urb->context;
255 struct atusb *atusb = SKB_ATUSB(skb);
257 dev_dbg(&usb_dev->dev, "%s: status %d len %d\n", __func__,
258 urb->status, urb->actual_length);
260 if (urb->status == -ENOENT) { /* being killed */
265 dev_dbg(&usb_dev->dev, "%s: URB error %d\n", __func__, urb->status);
270 usb_anchor_urb(urb, &atusb->idle_urbs);
271 if (!atusb->shutdown)
272 schedule_delayed_work(&atusb->work, 0);
275 /* ----- URB allocation/deallocation --------------------------------------- */
277 static void atusb_free_urbs(struct atusb *atusb)
282 urb = usb_get_from_anchor(&atusb->idle_urbs);
285 kfree_skb(urb->context);
290 static int atusb_alloc_urbs(struct atusb *atusb, int n)
295 urb = usb_alloc_urb(0, GFP_KERNEL);
297 atusb_free_urbs(atusb);
300 usb_anchor_urb(urb, &atusb->idle_urbs);
307 /* ----- IEEE 802.15.4 interface operations -------------------------------- */
309 static void atusb_xmit_complete(struct urb *urb)
311 dev_dbg(&urb->dev->dev, "atusb_xmit urb completed");
314 static int atusb_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
316 struct atusb *atusb = hw->priv;
317 struct usb_device *usb_dev = atusb->usb_dev;
320 dev_dbg(&usb_dev->dev, "%s (%d)\n", __func__, skb->len);
323 atusb->tx_dr.wIndex = cpu_to_le16(atusb->tx_ack_seq);
324 atusb->tx_dr.wLength = cpu_to_le16(skb->len);
326 usb_fill_control_urb(atusb->tx_urb, usb_dev,
327 usb_sndctrlpipe(usb_dev, 0),
328 (unsigned char *)&atusb->tx_dr, skb->data,
329 skb->len, atusb_xmit_complete, NULL);
330 ret = usb_submit_urb(atusb->tx_urb, GFP_ATOMIC);
331 dev_dbg(&usb_dev->dev, "%s done (%d)\n", __func__, ret);
335 static int atusb_ed(struct ieee802154_hw *hw, u8 *level)
342 static int atusb_set_hw_addr_filt(struct ieee802154_hw *hw,
343 struct ieee802154_hw_addr_filt *filt,
344 unsigned long changed)
346 struct atusb *atusb = hw->priv;
347 struct device *dev = &atusb->usb_dev->dev;
349 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
350 u16 addr = le16_to_cpu(filt->short_addr);
352 dev_vdbg(dev, "%s called for saddr\n", __func__);
353 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
354 addr, RG_SHORT_ADDR_0, NULL, 0, 1000, GFP_KERNEL);
356 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
357 addr >> 8, RG_SHORT_ADDR_1, NULL, 0, 1000, GFP_KERNEL);
360 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
361 u16 pan = le16_to_cpu(filt->pan_id);
363 dev_vdbg(dev, "%s called for pan id\n", __func__);
364 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
365 pan, RG_PAN_ID_0, NULL, 0, 1000, GFP_KERNEL);
367 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
368 pan >> 8, RG_PAN_ID_1, NULL, 0, 1000, GFP_KERNEL);
371 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
372 u8 i, addr[IEEE802154_EXTENDED_ADDR_LEN];
374 memcpy(addr, &filt->ieee_addr, IEEE802154_EXTENDED_ADDR_LEN);
375 dev_vdbg(dev, "%s called for IEEE addr\n", __func__);
376 for (i = 0; i < 8; i++)
377 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
378 addr[i], RG_IEEE_ADDR_0 + i, NULL, 0,
382 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
383 dev_vdbg(dev, "%s called for panc change\n", __func__);
385 atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 1);
387 atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 0);
390 return atusb_get_and_clear_error(atusb);
393 static int atusb_start(struct ieee802154_hw *hw)
395 struct atusb *atusb = hw->priv;
396 struct usb_device *usb_dev = atusb->usb_dev;
399 dev_dbg(&usb_dev->dev, "%s\n", __func__);
400 schedule_delayed_work(&atusb->work, 0);
401 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_RX_MODE, ATUSB_REQ_TO_DEV, 1, 0,
402 NULL, 0, 1000, GFP_KERNEL);
403 ret = atusb_get_and_clear_error(atusb);
405 usb_kill_anchored_urbs(&atusb->idle_urbs);
409 static void atusb_stop(struct ieee802154_hw *hw)
411 struct atusb *atusb = hw->priv;
412 struct usb_device *usb_dev = atusb->usb_dev;
414 dev_dbg(&usb_dev->dev, "%s\n", __func__);
415 usb_kill_anchored_urbs(&atusb->idle_urbs);
416 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_RX_MODE, ATUSB_REQ_TO_DEV, 0, 0,
417 NULL, 0, 1000, GFP_KERNEL);
418 atusb_get_and_clear_error(atusb);
421 #define ATUSB_MAX_TX_POWERS 0xF
422 static const s32 atusb_powers[ATUSB_MAX_TX_POWERS + 1] = {
423 300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700,
428 atusb_txpower(struct ieee802154_hw *hw, s32 mbm)
430 struct atusb *atusb = hw->priv;
433 return atusb->data->set_txpower(hw, mbm);
439 atusb_set_txpower(struct ieee802154_hw *hw, s32 mbm)
441 struct atusb *atusb = hw->priv;
444 for (i = 0; i < hw->phy->supported.tx_powers_size; i++) {
445 if (hw->phy->supported.tx_powers[i] == mbm)
446 return atusb_write_subreg(atusb, SR_TX_PWR_23X, i);
453 hulusb_set_txpower(struct ieee802154_hw *hw, s32 mbm)
457 for (i = 0; i < hw->phy->supported.tx_powers_size; i++) {
458 if (hw->phy->supported.tx_powers[i] == mbm)
459 return atusb_write_subreg(hw->priv, SR_TX_PWR_212, i);
465 #define ATUSB_MAX_ED_LEVELS 0xF
466 static const s32 atusb_ed_levels[ATUSB_MAX_ED_LEVELS + 1] = {
467 -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300,
468 -7100, -6900, -6700, -6500, -6300, -6100,
471 #define AT86RF212_MAX_TX_POWERS 0x1F
472 static const s32 at86rf212_powers[AT86RF212_MAX_TX_POWERS + 1] = {
473 500, 400, 300, 200, 100, 0, -100, -200, -300, -400, -500, -600, -700,
474 -800, -900, -1000, -1100, -1200, -1300, -1400, -1500, -1600, -1700,
475 -1800, -1900, -2000, -2100, -2200, -2300, -2400, -2500, -2600,
478 #define AT86RF2XX_MAX_ED_LEVELS 0xF
479 static const s32 at86rf212_ed_levels_100[AT86RF2XX_MAX_ED_LEVELS + 1] = {
480 -10000, -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200,
481 -8000, -7800, -7600, -7400, -7200, -7000,
484 static const s32 at86rf212_ed_levels_98[AT86RF2XX_MAX_ED_LEVELS + 1] = {
485 -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200, -8000,
486 -7800, -7600, -7400, -7200, -7000, -6800,
490 atusb_set_cca_mode(struct ieee802154_hw *hw, const struct wpan_phy_cca *cca)
492 struct atusb *atusb = hw->priv;
495 /* mapping 802.15.4 to driver spec */
497 case NL802154_CCA_ENERGY:
500 case NL802154_CCA_CARRIER:
503 case NL802154_CCA_ENERGY_CARRIER:
505 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
508 case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
519 return atusb_write_subreg(atusb, SR_CCA_MODE, val);
522 static int hulusb_set_cca_ed_level(struct atusb *lp, int rssi_base_val)
526 cca_ed_thres = atusb_read_subreg(lp, SR_CCA_ED_THRES);
527 if (cca_ed_thres < 0)
530 switch (rssi_base_val) {
532 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_98;
533 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_98);
534 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_98[cca_ed_thres];
537 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
538 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
539 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_100[cca_ed_thres];
549 atusb_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
551 struct atusb *atusb = hw->priv;
554 for (i = 0; i < hw->phy->supported.cca_ed_levels_size; i++) {
555 if (hw->phy->supported.cca_ed_levels[i] == mbm)
556 return atusb_write_subreg(atusb, SR_CCA_ED_THRES, i);
562 static int atusb_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
564 struct atusb *atusb = hw->priv;
568 ret = atusb->data->set_channel(hw, page, channel);
569 /* @@@ ugly synchronization */
570 msleep(atusb->data->t_channel_switch);
576 static int atusb_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
578 struct atusb *atusb = hw->priv;
581 ret = atusb_write_subreg(atusb, SR_CHANNEL, channel);
587 static int hulusb_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
592 struct atusb *lp = hw->priv;
595 rc = atusb_write_subreg(lp, SR_SUB_MODE, 0);
597 rc = atusb_write_subreg(lp, SR_SUB_MODE, 1);
602 rc = atusb_write_subreg(lp, SR_BPSK_QPSK, 0);
603 rssi_base_val = -100;
605 rc = atusb_write_subreg(lp, SR_BPSK_QPSK, 1);
611 rc = hulusb_set_cca_ed_level(lp, rssi_base_val);
615 return atusb_write_subreg(lp, SR_CHANNEL, channel);
619 atusb_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be, u8 retries)
621 struct atusb *atusb = hw->priv;
624 ret = atusb_write_subreg(atusb, SR_MIN_BE, min_be);
628 ret = atusb_write_subreg(atusb, SR_MAX_BE, max_be);
632 return atusb_write_subreg(atusb, SR_MAX_CSMA_RETRIES, retries);
636 hulusb_set_lbt(struct ieee802154_hw *hw, bool on)
638 struct atusb *atusb = hw->priv;
640 return atusb_write_subreg(atusb, SR_CSMA_LBT_MODE, on);
644 atusb_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
646 struct atusb *atusb = hw->priv;
648 return atusb_write_subreg(atusb, SR_MAX_FRAME_RETRIES, retries);
652 atusb_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
654 struct atusb *atusb = hw->priv;
658 ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 1);
662 ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 1);
666 ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 0);
670 ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 0);
678 static struct atusb_chip_data atusb_chip_data = {
679 .t_channel_switch = 1,
680 .rssi_base_val = -91,
681 .set_txpower = atusb_set_txpower,
682 .set_channel = atusb_set_channel,
685 static struct atusb_chip_data hulusb_chip_data = {
686 .t_channel_switch = 11,
687 .rssi_base_val = -100,
688 .set_txpower = hulusb_set_txpower,
689 .set_channel = hulusb_set_channel,
692 static const struct ieee802154_ops atusb_ops = {
693 .owner = THIS_MODULE,
694 .xmit_async = atusb_xmit,
696 .set_channel = atusb_channel,
697 .start = atusb_start,
699 .set_hw_addr_filt = atusb_set_hw_addr_filt,
700 .set_txpower = atusb_txpower,
701 .set_lbt = hulusb_set_lbt,
702 .set_cca_mode = atusb_set_cca_mode,
703 .set_cca_ed_level = atusb_set_cca_ed_level,
704 .set_csma_params = atusb_set_csma_params,
705 .set_frame_retries = atusb_set_frame_retries,
706 .set_promiscuous_mode = atusb_set_promiscuous_mode,
709 /* ----- Firmware and chip version information ----------------------------- */
711 static int atusb_get_and_show_revision(struct atusb *atusb)
713 struct usb_device *usb_dev = atusb->usb_dev;
715 unsigned char buffer[3];
718 /* Get a couple of the ATMega Firmware values */
719 ret = usb_control_msg_recv(atusb->usb_dev, 0, ATUSB_ID, ATUSB_REQ_FROM_DEV, 0, 0,
720 buffer, 3, 1000, GFP_KERNEL);
722 atusb->fw_ver_maj = buffer[0];
723 atusb->fw_ver_min = buffer[1];
724 atusb->fw_hw_type = buffer[2];
726 switch (atusb->fw_hw_type) {
727 case ATUSB_HW_TYPE_100813:
728 case ATUSB_HW_TYPE_101216:
729 case ATUSB_HW_TYPE_110131:
731 atusb->data = &atusb_chip_data;
733 case ATUSB_HW_TYPE_RZUSB:
735 atusb->data = &atusb_chip_data;
737 case ATUSB_HW_TYPE_HULUSB:
739 atusb->data = &hulusb_chip_data;
743 atusb->err = -ENOTSUPP;
748 dev_info(&usb_dev->dev,
749 "Firmware: major: %u, minor: %u, hardware type: %s (%d)\n",
750 atusb->fw_ver_maj, atusb->fw_ver_min, hw_name,
753 if (atusb->fw_ver_maj == 0 && atusb->fw_ver_min < 2) {
754 dev_info(&usb_dev->dev,
755 "Firmware version (%u.%u) predates our first public release.",
756 atusb->fw_ver_maj, atusb->fw_ver_min);
757 dev_info(&usb_dev->dev, "Please update to version 0.2 or newer");
763 static int atusb_get_and_show_build(struct atusb *atusb)
765 struct usb_device *usb_dev = atusb->usb_dev;
769 build = kmalloc(ATUSB_BUILD_SIZE + 1, GFP_KERNEL);
773 ret = usb_control_msg(atusb->usb_dev, usb_rcvctrlpipe(usb_dev, 0), ATUSB_BUILD,
774 ATUSB_REQ_FROM_DEV, 0, 0, build, ATUSB_BUILD_SIZE, 1000);
777 dev_info(&usb_dev->dev, "Firmware: build %s\n", build);
784 static int atusb_get_and_conf_chip(struct atusb *atusb)
786 struct usb_device *usb_dev = atusb->usb_dev;
787 u8 man_id_0, man_id_1, part_num, version_num;
789 struct ieee802154_hw *hw = atusb->hw;
792 ret = usb_control_msg_recv(usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
793 0, RG_MAN_ID_0, &man_id_0, 1, 1000, GFP_KERNEL);
797 ret = usb_control_msg_recv(usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
798 0, RG_MAN_ID_1, &man_id_1, 1, 1000, GFP_KERNEL);
802 ret = usb_control_msg_recv(usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
803 0, RG_PART_NUM, &part_num, 1, 1000, GFP_KERNEL);
807 ret = usb_control_msg_recv(usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
808 0, RG_VERSION_NUM, &version_num, 1, 1000, GFP_KERNEL);
812 hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AFILT |
813 IEEE802154_HW_PROMISCUOUS | IEEE802154_HW_CSMA_PARAMS;
815 hw->phy->flags = WPAN_PHY_FLAG_TXPOWER | WPAN_PHY_FLAG_CCA_ED_LEVEL |
816 WPAN_PHY_FLAG_CCA_MODE;
818 hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) |
819 BIT(NL802154_CCA_CARRIER) |
820 BIT(NL802154_CCA_ENERGY_CARRIER);
821 hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) |
822 BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR);
824 hw->phy->cca.mode = NL802154_CCA_ENERGY;
826 hw->phy->current_page = 0;
828 if ((man_id_1 << 8 | man_id_0) != ATUSB_JEDEC_ATMEL) {
829 dev_err(&usb_dev->dev,
830 "non-Atmel transceiver xxxx%02x%02x\n",
838 atusb->hw->phy->supported.channels[0] = 0x7FFF800;
839 atusb->hw->phy->current_channel = 11; /* reset default */
840 atusb->hw->phy->supported.tx_powers = atusb_powers;
841 atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers);
842 hw->phy->supported.cca_ed_levels = atusb_ed_levels;
843 hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels);
847 atusb->hw->phy->supported.channels[0] = 0x7FFF800;
848 atusb->hw->phy->current_channel = 11; /* reset default */
849 atusb->hw->phy->supported.tx_powers = atusb_powers;
850 atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers);
851 hw->phy->supported.cca_ed_levels = atusb_ed_levels;
852 hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels);
856 atusb->hw->flags |= IEEE802154_HW_LBT;
857 atusb->hw->phy->supported.channels[0] = 0x00007FF;
858 atusb->hw->phy->supported.channels[2] = 0x00007FF;
859 atusb->hw->phy->current_channel = 5;
860 atusb->hw->phy->supported.lbt = NL802154_SUPPORTED_BOOL_BOTH;
861 atusb->hw->phy->supported.tx_powers = at86rf212_powers;
862 atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf212_powers);
863 atusb->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
864 atusb->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
867 dev_err(&usb_dev->dev,
868 "unexpected transceiver, part 0x%02x version 0x%02x\n",
869 part_num, version_num);
873 hw->phy->transmit_power = hw->phy->supported.tx_powers[0];
874 hw->phy->cca_ed_level = hw->phy->supported.cca_ed_levels[7];
876 dev_info(&usb_dev->dev, "ATUSB: %s version %d\n", chip, version_num);
881 atusb->err = -ENODEV;
885 static int atusb_set_extended_addr(struct atusb *atusb)
887 struct usb_device *usb_dev = atusb->usb_dev;
888 unsigned char buffer[IEEE802154_EXTENDED_ADDR_LEN];
889 __le64 extended_addr;
893 /* Firmware versions before 0.3 do not support the EUI64_READ command.
894 * Just use a random address and be done.
896 if (atusb->fw_ver_maj == 0 && atusb->fw_ver_min < 3) {
897 ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
901 /* Firmware is new enough so we fetch the address from EEPROM */
902 ret = usb_control_msg_recv(atusb->usb_dev, 0, ATUSB_EUI64_READ, ATUSB_REQ_FROM_DEV, 0, 0,
903 buffer, IEEE802154_EXTENDED_ADDR_LEN, 1000, GFP_KERNEL);
905 dev_err(&usb_dev->dev, "failed to fetch extended address, random address set\n");
906 ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
910 memcpy(&extended_addr, buffer, IEEE802154_EXTENDED_ADDR_LEN);
911 /* Check if read address is not empty and the unicast bit is set correctly */
912 if (!ieee802154_is_valid_extended_unicast_addr(extended_addr)) {
913 dev_info(&usb_dev->dev, "no permanent extended address found, random address set\n");
914 ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
916 atusb->hw->phy->perm_extended_addr = extended_addr;
917 addr = swab64((__force u64)atusb->hw->phy->perm_extended_addr);
918 dev_info(&usb_dev->dev, "Read permanent extended address %8phC from device\n",
925 /* ----- Setup ------------------------------------------------------------- */
927 static int atusb_probe(struct usb_interface *interface,
928 const struct usb_device_id *id)
930 struct usb_device *usb_dev = interface_to_usbdev(interface);
931 struct ieee802154_hw *hw;
932 struct atusb *atusb = NULL;
935 hw = ieee802154_alloc_hw(sizeof(struct atusb), &atusb_ops);
941 atusb->usb_dev = usb_get_dev(usb_dev);
942 usb_set_intfdata(interface, atusb);
946 INIT_DELAYED_WORK(&atusb->work, atusb_work_urbs);
947 init_usb_anchor(&atusb->idle_urbs);
948 init_usb_anchor(&atusb->rx_urbs);
950 if (atusb_alloc_urbs(atusb, ATUSB_NUM_RX_URBS))
953 atusb->tx_dr.bRequestType = ATUSB_REQ_TO_DEV;
954 atusb->tx_dr.bRequest = ATUSB_TX;
955 atusb->tx_dr.wValue = cpu_to_le16(0);
957 atusb->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
961 hw->parent = &usb_dev->dev;
963 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_RF_RESET, ATUSB_REQ_TO_DEV, 0, 0,
964 NULL, 0, 1000, GFP_KERNEL);
965 atusb_get_and_conf_chip(atusb);
966 atusb_get_and_show_revision(atusb);
967 atusb_get_and_show_build(atusb);
968 atusb_set_extended_addr(atusb);
970 if ((atusb->fw_ver_maj == 0 && atusb->fw_ver_min >= 3) || atusb->fw_ver_maj > 0)
971 hw->flags |= IEEE802154_HW_FRAME_RETRIES;
973 ret = atusb_get_and_clear_error(atusb);
975 dev_err(&atusb->usb_dev->dev,
976 "%s: initialization failed, error = %d\n",
981 ret = ieee802154_register_hw(hw);
985 /* If we just powered on, we're now in P_ON and need to enter TRX_OFF
986 * explicitly. Any resets after that will send us straight to TRX_OFF,
987 * making the command below redundant.
989 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
990 STATE_FORCE_TRX_OFF, RG_TRX_STATE, NULL, 0, 1000, GFP_KERNEL);
992 msleep(1); /* reset => TRX_OFF, tTR13 = 37 us */
995 /* Calculating the maximum time available to empty the frame buffer
998 * According to [1], the inter-frame gap is
999 * R * 20 * 16 us + 128 us
1000 * where R is a random number from 0 to 7. Furthermore, we have 20 bit
1001 * times (80 us at 250 kbps) of SHR of the next frame before the
1002 * transceiver begins storing data in the frame buffer.
1004 * This yields a minimum time of 208 us between the last data of a
1005 * frame and the first data of the next frame. This time is further
1006 * reduced by interrupt latency in the atusb firmware.
1008 * atusb currently needs about 500 us to retrieve a maximum-sized
1009 * frame. We therefore have to allow reception of a new frame to begin
1010 * while we retrieve the previous frame.
1012 * [1] "JN-AN-1035 Calculating data rates in an IEEE 802.15.4-based
1013 * network", Jennic 2006.
1014 * http://www.jennic.com/download_file.php?supportFile=JN-AN-1035%20Calculating%20802-15-4%20Data%20Rates-1v0.pdf
1017 atusb_write_subreg(atusb, SR_RX_SAFE_MODE, 1);
1019 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
1020 0xff, RG_IRQ_MASK, NULL, 0, 1000, GFP_KERNEL);
1022 ret = atusb_get_and_clear_error(atusb);
1026 dev_err(&atusb->usb_dev->dev,
1027 "%s: setup failed, error = %d\n",
1030 ieee802154_unregister_hw(hw);
1032 atusb_free_urbs(atusb);
1033 usb_kill_urb(atusb->tx_urb);
1034 usb_free_urb(atusb->tx_urb);
1035 usb_put_dev(usb_dev);
1036 ieee802154_free_hw(hw);
1040 static void atusb_disconnect(struct usb_interface *interface)
1042 struct atusb *atusb = usb_get_intfdata(interface);
1044 dev_dbg(&atusb->usb_dev->dev, "%s\n", __func__);
1046 atusb->shutdown = 1;
1047 cancel_delayed_work_sync(&atusb->work);
1049 usb_kill_anchored_urbs(&atusb->rx_urbs);
1050 atusb_free_urbs(atusb);
1051 usb_kill_urb(atusb->tx_urb);
1052 usb_free_urb(atusb->tx_urb);
1054 ieee802154_unregister_hw(atusb->hw);
1056 usb_put_dev(atusb->usb_dev);
1058 ieee802154_free_hw(atusb->hw);
1060 usb_set_intfdata(interface, NULL);
1062 pr_debug("%s done\n", __func__);
1065 /* The devices we work with */
1066 static const struct usb_device_id atusb_device_table[] = {
1068 .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
1069 USB_DEVICE_ID_MATCH_INT_INFO,
1070 .idVendor = ATUSB_VENDOR_ID,
1071 .idProduct = ATUSB_PRODUCT_ID,
1072 .bInterfaceClass = USB_CLASS_VENDOR_SPEC
1074 /* end with null element */
1077 MODULE_DEVICE_TABLE(usb, atusb_device_table);
1079 static struct usb_driver atusb_driver = {
1081 .probe = atusb_probe,
1082 .disconnect = atusb_disconnect,
1083 .id_table = atusb_device_table,
1085 module_usb_driver(atusb_driver);
1087 MODULE_AUTHOR("Alexander Aring <alex.aring@gmail.com>");
1088 MODULE_AUTHOR("Richard Sharpe <realrichardsharpe@gmail.com>");
1089 MODULE_AUTHOR("Stefan Schmidt <stefan@datenfreihafen.org>");
1090 MODULE_AUTHOR("Werner Almesberger <werner@almesberger.net>");
1091 MODULE_AUTHOR("Josef Filzmaier <j.filzmaier@gmx.at>");
1092 MODULE_DESCRIPTION("ATUSB IEEE 802.15.4 Driver");
1093 MODULE_LICENSE("GPL");