1 // SPDX-License-Identifier: GPL-2.0-only
3 * atusb.c - Driver for the ATUSB IEEE 802.15.4 dongle
9 * Based on at86rf230.c and spi_atusb.c.
11 * Copyright (C) 2009 Siemens AG
19 * USB initialization is
22 * Busware HUL support is
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, int reason)
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 if (reason == IEEE802154_SUCCESS)
203 ieee802154_xmit_complete(atusb->hw, atusb->tx_skb, false);
205 ieee802154_xmit_error(atusb->hw, atusb->tx_skb, reason);
207 /* TODO I experience this case when atusb has a tx complete
208 * irq before probing, we should fix the firmware it's an
209 * unlikely case now that seq == expect is then true, but can
210 * happen and fail with a tx_skb = NULL;
212 ieee802154_xmit_hw_error(atusb->hw, atusb->tx_skb);
216 static void atusb_in_good(struct urb *urb)
218 struct usb_device *usb_dev = urb->dev;
219 struct sk_buff *skb = urb->context;
220 struct atusb *atusb = SKB_ATUSB(skb);
221 int result = IEEE802154_SUCCESS;
224 if (!urb->actual_length) {
225 dev_dbg(&usb_dev->dev, "atusb_in: zero-sized URB ?\n");
231 switch (urb->actual_length) {
233 trac = TRAC_MASK(*(skb->data + 1));
236 case TRAC_SUCCESS_DATA_PENDING:
237 /* already IEEE802154_SUCCESS */
239 case TRAC_CHANNEL_ACCESS_FAILURE:
240 result = IEEE802154_CHANNEL_ACCESS_FAILURE;
243 result = IEEE802154_NO_ACK;
246 result = IEEE802154_SYSTEM_ERROR;
251 atusb_tx_done(atusb, len, result);
255 if (len + 1 > urb->actual_length - 1) {
256 dev_dbg(&usb_dev->dev, "atusb_in: frame len %d+1 > URB %u-1\n",
257 len, urb->actual_length);
261 if (!ieee802154_is_valid_psdu_len(len)) {
262 dev_dbg(&usb_dev->dev, "atusb_in: frame corrupted\n");
266 lqi = skb->data[len + 1];
267 dev_dbg(&usb_dev->dev, "atusb_in: rx len %d lqi 0x%02x\n", len, lqi);
268 skb_pull(skb, 1); /* remove PHR */
269 skb_trim(skb, len); /* get payload only */
270 ieee802154_rx_irqsafe(atusb->hw, skb, lqi);
271 urb->context = NULL; /* skb is gone */
274 static void atusb_in(struct urb *urb)
276 struct usb_device *usb_dev = urb->dev;
277 struct sk_buff *skb = urb->context;
278 struct atusb *atusb = SKB_ATUSB(skb);
280 dev_dbg(&usb_dev->dev, "%s: status %d len %d\n", __func__,
281 urb->status, urb->actual_length);
283 if (urb->status == -ENOENT) { /* being killed */
288 dev_dbg(&usb_dev->dev, "%s: URB error %d\n", __func__, urb->status);
293 usb_anchor_urb(urb, &atusb->idle_urbs);
294 if (!atusb->shutdown)
295 schedule_delayed_work(&atusb->work, 0);
298 /* ----- URB allocation/deallocation --------------------------------------- */
300 static void atusb_free_urbs(struct atusb *atusb)
305 urb = usb_get_from_anchor(&atusb->idle_urbs);
308 kfree_skb(urb->context);
313 static int atusb_alloc_urbs(struct atusb *atusb, int n)
318 urb = usb_alloc_urb(0, GFP_KERNEL);
320 atusb_free_urbs(atusb);
323 usb_anchor_urb(urb, &atusb->idle_urbs);
330 /* ----- IEEE 802.15.4 interface operations -------------------------------- */
332 static void atusb_xmit_complete(struct urb *urb)
334 dev_dbg(&urb->dev->dev, "atusb_xmit urb completed");
337 static int atusb_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
339 struct atusb *atusb = hw->priv;
340 struct usb_device *usb_dev = atusb->usb_dev;
343 dev_dbg(&usb_dev->dev, "%s (%d)\n", __func__, skb->len);
346 atusb->tx_dr.wIndex = cpu_to_le16(atusb->tx_ack_seq);
347 atusb->tx_dr.wLength = cpu_to_le16(skb->len);
349 usb_fill_control_urb(atusb->tx_urb, usb_dev,
350 usb_sndctrlpipe(usb_dev, 0),
351 (unsigned char *)&atusb->tx_dr, skb->data,
352 skb->len, atusb_xmit_complete, NULL);
353 ret = usb_submit_urb(atusb->tx_urb, GFP_ATOMIC);
354 dev_dbg(&usb_dev->dev, "%s done (%d)\n", __func__, ret);
358 static int atusb_ed(struct ieee802154_hw *hw, u8 *level)
365 static int atusb_set_hw_addr_filt(struct ieee802154_hw *hw,
366 struct ieee802154_hw_addr_filt *filt,
367 unsigned long changed)
369 struct atusb *atusb = hw->priv;
370 struct device *dev = &atusb->usb_dev->dev;
372 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
373 u16 addr = le16_to_cpu(filt->short_addr);
375 dev_vdbg(dev, "%s called for saddr\n", __func__);
376 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
377 addr, RG_SHORT_ADDR_0, NULL, 0, 1000, GFP_KERNEL);
379 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
380 addr >> 8, RG_SHORT_ADDR_1, NULL, 0, 1000, GFP_KERNEL);
383 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
384 u16 pan = le16_to_cpu(filt->pan_id);
386 dev_vdbg(dev, "%s called for pan id\n", __func__);
387 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
388 pan, RG_PAN_ID_0, NULL, 0, 1000, GFP_KERNEL);
390 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
391 pan >> 8, RG_PAN_ID_1, NULL, 0, 1000, GFP_KERNEL);
394 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
395 u8 i, addr[IEEE802154_EXTENDED_ADDR_LEN];
397 memcpy(addr, &filt->ieee_addr, IEEE802154_EXTENDED_ADDR_LEN);
398 dev_vdbg(dev, "%s called for IEEE addr\n", __func__);
399 for (i = 0; i < 8; i++)
400 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
401 addr[i], RG_IEEE_ADDR_0 + i, NULL, 0,
405 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
406 dev_vdbg(dev, "%s called for panc change\n", __func__);
408 atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 1);
410 atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 0);
413 return atusb_get_and_clear_error(atusb);
416 static int atusb_start(struct ieee802154_hw *hw)
418 struct atusb *atusb = hw->priv;
419 struct usb_device *usb_dev = atusb->usb_dev;
422 dev_dbg(&usb_dev->dev, "%s\n", __func__);
423 schedule_delayed_work(&atusb->work, 0);
424 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_RX_MODE, ATUSB_REQ_TO_DEV, 1, 0,
425 NULL, 0, 1000, GFP_KERNEL);
426 ret = atusb_get_and_clear_error(atusb);
428 usb_kill_anchored_urbs(&atusb->idle_urbs);
432 static void atusb_stop(struct ieee802154_hw *hw)
434 struct atusb *atusb = hw->priv;
435 struct usb_device *usb_dev = atusb->usb_dev;
437 dev_dbg(&usb_dev->dev, "%s\n", __func__);
438 usb_kill_anchored_urbs(&atusb->idle_urbs);
439 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_RX_MODE, ATUSB_REQ_TO_DEV, 0, 0,
440 NULL, 0, 1000, GFP_KERNEL);
441 atusb_get_and_clear_error(atusb);
444 #define ATUSB_MAX_TX_POWERS 0xF
445 static const s32 atusb_powers[ATUSB_MAX_TX_POWERS + 1] = {
446 300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700,
451 atusb_txpower(struct ieee802154_hw *hw, s32 mbm)
453 struct atusb *atusb = hw->priv;
456 return atusb->data->set_txpower(hw, mbm);
462 atusb_set_txpower(struct ieee802154_hw *hw, s32 mbm)
464 struct atusb *atusb = hw->priv;
467 for (i = 0; i < hw->phy->supported.tx_powers_size; i++) {
468 if (hw->phy->supported.tx_powers[i] == mbm)
469 return atusb_write_subreg(atusb, SR_TX_PWR_23X, i);
476 hulusb_set_txpower(struct ieee802154_hw *hw, s32 mbm)
480 for (i = 0; i < hw->phy->supported.tx_powers_size; i++) {
481 if (hw->phy->supported.tx_powers[i] == mbm)
482 return atusb_write_subreg(hw->priv, SR_TX_PWR_212, i);
488 #define ATUSB_MAX_ED_LEVELS 0xF
489 static const s32 atusb_ed_levels[ATUSB_MAX_ED_LEVELS + 1] = {
490 -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300,
491 -7100, -6900, -6700, -6500, -6300, -6100,
494 #define AT86RF212_MAX_TX_POWERS 0x1F
495 static const s32 at86rf212_powers[AT86RF212_MAX_TX_POWERS + 1] = {
496 500, 400, 300, 200, 100, 0, -100, -200, -300, -400, -500, -600, -700,
497 -800, -900, -1000, -1100, -1200, -1300, -1400, -1500, -1600, -1700,
498 -1800, -1900, -2000, -2100, -2200, -2300, -2400, -2500, -2600,
501 #define AT86RF2XX_MAX_ED_LEVELS 0xF
502 static const s32 at86rf212_ed_levels_100[AT86RF2XX_MAX_ED_LEVELS + 1] = {
503 -10000, -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200,
504 -8000, -7800, -7600, -7400, -7200, -7000,
507 static const s32 at86rf212_ed_levels_98[AT86RF2XX_MAX_ED_LEVELS + 1] = {
508 -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200, -8000,
509 -7800, -7600, -7400, -7200, -7000, -6800,
513 atusb_set_cca_mode(struct ieee802154_hw *hw, const struct wpan_phy_cca *cca)
515 struct atusb *atusb = hw->priv;
518 /* mapping 802.15.4 to driver spec */
520 case NL802154_CCA_ENERGY:
523 case NL802154_CCA_CARRIER:
526 case NL802154_CCA_ENERGY_CARRIER:
528 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
531 case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
542 return atusb_write_subreg(atusb, SR_CCA_MODE, val);
545 static int hulusb_set_cca_ed_level(struct atusb *lp, int rssi_base_val)
549 cca_ed_thres = atusb_read_subreg(lp, SR_CCA_ED_THRES);
550 if (cca_ed_thres < 0)
553 switch (rssi_base_val) {
555 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_98;
556 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_98);
557 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_98[cca_ed_thres];
560 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
561 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
562 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_100[cca_ed_thres];
572 atusb_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
574 struct atusb *atusb = hw->priv;
577 for (i = 0; i < hw->phy->supported.cca_ed_levels_size; i++) {
578 if (hw->phy->supported.cca_ed_levels[i] == mbm)
579 return atusb_write_subreg(atusb, SR_CCA_ED_THRES, i);
585 static int atusb_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
587 struct atusb *atusb = hw->priv;
591 ret = atusb->data->set_channel(hw, page, channel);
592 /* @@@ ugly synchronization */
593 msleep(atusb->data->t_channel_switch);
599 static int atusb_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
601 struct atusb *atusb = hw->priv;
604 ret = atusb_write_subreg(atusb, SR_CHANNEL, channel);
610 static int hulusb_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
615 struct atusb *lp = hw->priv;
618 rc = atusb_write_subreg(lp, SR_SUB_MODE, 0);
620 rc = atusb_write_subreg(lp, SR_SUB_MODE, 1);
625 rc = atusb_write_subreg(lp, SR_BPSK_QPSK, 0);
626 rssi_base_val = -100;
628 rc = atusb_write_subreg(lp, SR_BPSK_QPSK, 1);
634 rc = hulusb_set_cca_ed_level(lp, rssi_base_val);
638 return atusb_write_subreg(lp, SR_CHANNEL, channel);
642 atusb_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be, u8 retries)
644 struct atusb *atusb = hw->priv;
647 ret = atusb_write_subreg(atusb, SR_MIN_BE, min_be);
651 ret = atusb_write_subreg(atusb, SR_MAX_BE, max_be);
655 return atusb_write_subreg(atusb, SR_MAX_CSMA_RETRIES, retries);
659 hulusb_set_lbt(struct ieee802154_hw *hw, bool on)
661 struct atusb *atusb = hw->priv;
663 return atusb_write_subreg(atusb, SR_CSMA_LBT_MODE, on);
667 atusb_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
669 struct atusb *atusb = hw->priv;
671 return atusb_write_subreg(atusb, SR_MAX_FRAME_RETRIES, retries);
675 atusb_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
677 struct atusb *atusb = hw->priv;
681 ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 1);
685 ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 1);
689 ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 0);
693 ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 0);
701 static struct atusb_chip_data atusb_chip_data = {
702 .t_channel_switch = 1,
703 .rssi_base_val = -91,
704 .set_txpower = atusb_set_txpower,
705 .set_channel = atusb_set_channel,
708 static struct atusb_chip_data hulusb_chip_data = {
709 .t_channel_switch = 11,
710 .rssi_base_val = -100,
711 .set_txpower = hulusb_set_txpower,
712 .set_channel = hulusb_set_channel,
715 static const struct ieee802154_ops atusb_ops = {
716 .owner = THIS_MODULE,
717 .xmit_async = atusb_xmit,
719 .set_channel = atusb_channel,
720 .start = atusb_start,
722 .set_hw_addr_filt = atusb_set_hw_addr_filt,
723 .set_txpower = atusb_txpower,
724 .set_lbt = hulusb_set_lbt,
725 .set_cca_mode = atusb_set_cca_mode,
726 .set_cca_ed_level = atusb_set_cca_ed_level,
727 .set_csma_params = atusb_set_csma_params,
728 .set_frame_retries = atusb_set_frame_retries,
729 .set_promiscuous_mode = atusb_set_promiscuous_mode,
732 /* ----- Firmware and chip version information ----------------------------- */
734 static int atusb_get_and_show_revision(struct atusb *atusb)
736 struct usb_device *usb_dev = atusb->usb_dev;
738 unsigned char buffer[3];
741 /* Get a couple of the ATMega Firmware values */
742 ret = usb_control_msg_recv(atusb->usb_dev, 0, ATUSB_ID, ATUSB_REQ_FROM_DEV, 0, 0,
743 buffer, 3, 1000, GFP_KERNEL);
745 atusb->fw_ver_maj = buffer[0];
746 atusb->fw_ver_min = buffer[1];
747 atusb->fw_hw_type = buffer[2];
749 switch (atusb->fw_hw_type) {
750 case ATUSB_HW_TYPE_100813:
751 case ATUSB_HW_TYPE_101216:
752 case ATUSB_HW_TYPE_110131:
754 atusb->data = &atusb_chip_data;
756 case ATUSB_HW_TYPE_RZUSB:
758 atusb->data = &atusb_chip_data;
760 case ATUSB_HW_TYPE_HULUSB:
762 atusb->data = &hulusb_chip_data;
766 atusb->err = -ENOTSUPP;
771 dev_info(&usb_dev->dev,
772 "Firmware: major: %u, minor: %u, hardware type: %s (%d)\n",
773 atusb->fw_ver_maj, atusb->fw_ver_min, hw_name,
776 if (atusb->fw_ver_maj == 0 && atusb->fw_ver_min < 2) {
777 dev_info(&usb_dev->dev,
778 "Firmware version (%u.%u) predates our first public release.",
779 atusb->fw_ver_maj, atusb->fw_ver_min);
780 dev_info(&usb_dev->dev, "Please update to version 0.2 or newer");
786 static int atusb_get_and_show_build(struct atusb *atusb)
788 struct usb_device *usb_dev = atusb->usb_dev;
792 build = kmalloc(ATUSB_BUILD_SIZE + 1, GFP_KERNEL);
796 ret = usb_control_msg(atusb->usb_dev, usb_rcvctrlpipe(usb_dev, 0), ATUSB_BUILD,
797 ATUSB_REQ_FROM_DEV, 0, 0, build, ATUSB_BUILD_SIZE, 1000);
800 dev_info(&usb_dev->dev, "Firmware: build %s\n", build);
807 static int atusb_get_and_conf_chip(struct atusb *atusb)
809 struct usb_device *usb_dev = atusb->usb_dev;
810 u8 man_id_0, man_id_1, part_num, version_num;
812 struct ieee802154_hw *hw = atusb->hw;
815 ret = usb_control_msg_recv(usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
816 0, RG_MAN_ID_0, &man_id_0, 1, 1000, GFP_KERNEL);
820 ret = usb_control_msg_recv(usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
821 0, RG_MAN_ID_1, &man_id_1, 1, 1000, GFP_KERNEL);
825 ret = usb_control_msg_recv(usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
826 0, RG_PART_NUM, &part_num, 1, 1000, GFP_KERNEL);
830 ret = usb_control_msg_recv(usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
831 0, RG_VERSION_NUM, &version_num, 1, 1000, GFP_KERNEL);
835 hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AFILT |
836 IEEE802154_HW_PROMISCUOUS | IEEE802154_HW_CSMA_PARAMS;
838 hw->phy->flags = WPAN_PHY_FLAG_TXPOWER | WPAN_PHY_FLAG_CCA_ED_LEVEL |
839 WPAN_PHY_FLAG_CCA_MODE;
841 hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) |
842 BIT(NL802154_CCA_CARRIER) |
843 BIT(NL802154_CCA_ENERGY_CARRIER);
844 hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) |
845 BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR);
847 hw->phy->cca.mode = NL802154_CCA_ENERGY;
849 hw->phy->current_page = 0;
851 if ((man_id_1 << 8 | man_id_0) != ATUSB_JEDEC_ATMEL) {
852 dev_err(&usb_dev->dev,
853 "non-Atmel transceiver xxxx%02x%02x\n",
861 atusb->hw->phy->supported.channels[0] = 0x7FFF800;
862 atusb->hw->phy->current_channel = 11; /* reset default */
863 atusb->hw->phy->supported.tx_powers = atusb_powers;
864 atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers);
865 hw->phy->supported.cca_ed_levels = atusb_ed_levels;
866 hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels);
870 atusb->hw->phy->supported.channels[0] = 0x7FFF800;
871 atusb->hw->phy->current_channel = 11; /* reset default */
872 atusb->hw->phy->supported.tx_powers = atusb_powers;
873 atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers);
874 hw->phy->supported.cca_ed_levels = atusb_ed_levels;
875 hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels);
879 atusb->hw->flags |= IEEE802154_HW_LBT;
880 atusb->hw->phy->supported.channels[0] = 0x00007FF;
881 atusb->hw->phy->supported.channels[2] = 0x00007FF;
882 atusb->hw->phy->current_channel = 5;
883 atusb->hw->phy->supported.lbt = NL802154_SUPPORTED_BOOL_BOTH;
884 atusb->hw->phy->supported.tx_powers = at86rf212_powers;
885 atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf212_powers);
886 atusb->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
887 atusb->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
890 dev_err(&usb_dev->dev,
891 "unexpected transceiver, part 0x%02x version 0x%02x\n",
892 part_num, version_num);
896 hw->phy->transmit_power = hw->phy->supported.tx_powers[0];
897 hw->phy->cca_ed_level = hw->phy->supported.cca_ed_levels[7];
899 dev_info(&usb_dev->dev, "ATUSB: %s version %d\n", chip, version_num);
904 atusb->err = -ENODEV;
908 static int atusb_set_extended_addr(struct atusb *atusb)
910 struct usb_device *usb_dev = atusb->usb_dev;
911 unsigned char buffer[IEEE802154_EXTENDED_ADDR_LEN];
912 __le64 extended_addr;
916 /* Firmware versions before 0.3 do not support the EUI64_READ command.
917 * Just use a random address and be done.
919 if (atusb->fw_ver_maj == 0 && atusb->fw_ver_min < 3) {
920 ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
924 /* Firmware is new enough so we fetch the address from EEPROM */
925 ret = usb_control_msg_recv(atusb->usb_dev, 0, ATUSB_EUI64_READ, ATUSB_REQ_FROM_DEV, 0, 0,
926 buffer, IEEE802154_EXTENDED_ADDR_LEN, 1000, GFP_KERNEL);
928 dev_err(&usb_dev->dev, "failed to fetch extended address, random address set\n");
929 ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
933 memcpy(&extended_addr, buffer, IEEE802154_EXTENDED_ADDR_LEN);
934 /* Check if read address is not empty and the unicast bit is set correctly */
935 if (!ieee802154_is_valid_extended_unicast_addr(extended_addr)) {
936 dev_info(&usb_dev->dev, "no permanent extended address found, random address set\n");
937 ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
939 atusb->hw->phy->perm_extended_addr = extended_addr;
940 addr = swab64((__force u64)atusb->hw->phy->perm_extended_addr);
941 dev_info(&usb_dev->dev, "Read permanent extended address %8phC from device\n",
948 /* ----- Setup ------------------------------------------------------------- */
950 static int atusb_probe(struct usb_interface *interface,
951 const struct usb_device_id *id)
953 struct usb_device *usb_dev = interface_to_usbdev(interface);
954 struct ieee802154_hw *hw;
955 struct atusb *atusb = NULL;
958 hw = ieee802154_alloc_hw(sizeof(struct atusb), &atusb_ops);
964 atusb->usb_dev = usb_get_dev(usb_dev);
965 usb_set_intfdata(interface, atusb);
969 INIT_DELAYED_WORK(&atusb->work, atusb_work_urbs);
970 init_usb_anchor(&atusb->idle_urbs);
971 init_usb_anchor(&atusb->rx_urbs);
973 if (atusb_alloc_urbs(atusb, ATUSB_NUM_RX_URBS))
976 atusb->tx_dr.bRequestType = ATUSB_REQ_TO_DEV;
977 atusb->tx_dr.bRequest = ATUSB_TX;
978 atusb->tx_dr.wValue = cpu_to_le16(0);
980 atusb->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
984 hw->parent = &usb_dev->dev;
986 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_RF_RESET, ATUSB_REQ_TO_DEV, 0, 0,
987 NULL, 0, 1000, GFP_KERNEL);
988 atusb_get_and_conf_chip(atusb);
989 atusb_get_and_show_revision(atusb);
990 atusb_get_and_show_build(atusb);
991 atusb_set_extended_addr(atusb);
993 if ((atusb->fw_ver_maj == 0 && atusb->fw_ver_min >= 3) || atusb->fw_ver_maj > 0)
994 hw->flags |= IEEE802154_HW_FRAME_RETRIES;
996 ret = atusb_get_and_clear_error(atusb);
998 dev_err(&atusb->usb_dev->dev,
999 "%s: initialization failed, error = %d\n",
1004 ret = ieee802154_register_hw(hw);
1008 /* If we just powered on, we're now in P_ON and need to enter TRX_OFF
1009 * explicitly. Any resets after that will send us straight to TRX_OFF,
1010 * making the command below redundant.
1012 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
1013 STATE_FORCE_TRX_OFF, RG_TRX_STATE, NULL, 0, 1000, GFP_KERNEL);
1015 msleep(1); /* reset => TRX_OFF, tTR13 = 37 us */
1018 /* Calculating the maximum time available to empty the frame buffer
1021 * According to [1], the inter-frame gap is
1022 * R * 20 * 16 us + 128 us
1023 * where R is a random number from 0 to 7. Furthermore, we have 20 bit
1024 * times (80 us at 250 kbps) of SHR of the next frame before the
1025 * transceiver begins storing data in the frame buffer.
1027 * This yields a minimum time of 208 us between the last data of a
1028 * frame and the first data of the next frame. This time is further
1029 * reduced by interrupt latency in the atusb firmware.
1031 * atusb currently needs about 500 us to retrieve a maximum-sized
1032 * frame. We therefore have to allow reception of a new frame to begin
1033 * while we retrieve the previous frame.
1035 * [1] "JN-AN-1035 Calculating data rates in an IEEE 802.15.4-based
1036 * network", Jennic 2006.
1037 * http://www.jennic.com/download_file.php?supportFile=JN-AN-1035%20Calculating%20802-15-4%20Data%20Rates-1v0.pdf
1040 atusb_write_subreg(atusb, SR_RX_SAFE_MODE, 1);
1042 usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
1043 0xff, RG_IRQ_MASK, NULL, 0, 1000, GFP_KERNEL);
1045 ret = atusb_get_and_clear_error(atusb);
1049 dev_err(&atusb->usb_dev->dev,
1050 "%s: setup failed, error = %d\n",
1053 ieee802154_unregister_hw(hw);
1055 atusb_free_urbs(atusb);
1056 usb_kill_urb(atusb->tx_urb);
1057 usb_free_urb(atusb->tx_urb);
1058 usb_put_dev(usb_dev);
1059 ieee802154_free_hw(hw);
1063 static void atusb_disconnect(struct usb_interface *interface)
1065 struct atusb *atusb = usb_get_intfdata(interface);
1067 dev_dbg(&atusb->usb_dev->dev, "%s\n", __func__);
1069 atusb->shutdown = 1;
1070 cancel_delayed_work_sync(&atusb->work);
1072 usb_kill_anchored_urbs(&atusb->rx_urbs);
1073 atusb_free_urbs(atusb);
1074 usb_kill_urb(atusb->tx_urb);
1075 usb_free_urb(atusb->tx_urb);
1077 ieee802154_unregister_hw(atusb->hw);
1079 usb_put_dev(atusb->usb_dev);
1081 ieee802154_free_hw(atusb->hw);
1083 usb_set_intfdata(interface, NULL);
1085 pr_debug("%s done\n", __func__);
1088 /* The devices we work with */
1089 static const struct usb_device_id atusb_device_table[] = {
1091 .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
1092 USB_DEVICE_ID_MATCH_INT_INFO,
1093 .idVendor = ATUSB_VENDOR_ID,
1094 .idProduct = ATUSB_PRODUCT_ID,
1095 .bInterfaceClass = USB_CLASS_VENDOR_SPEC
1097 /* end with null element */
1100 MODULE_DEVICE_TABLE(usb, atusb_device_table);
1102 static struct usb_driver atusb_driver = {
1104 .probe = atusb_probe,
1105 .disconnect = atusb_disconnect,
1106 .id_table = atusb_device_table,
1108 module_usb_driver(atusb_driver);
1115 MODULE_DESCRIPTION("ATUSB IEEE 802.15.4 Driver");
1116 MODULE_LICENSE("GPL");