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 /* ----- USB commands without data ----------------------------------------- */
79 /* To reduce the number of error checks in the code, we record the first error
80 * in atusb->err and reject all subsequent requests until the error is cleared.
83 static int atusb_control_msg(struct atusb *atusb, unsigned int pipe,
84 __u8 request, __u8 requesttype,
85 __u16 value, __u16 index,
86 void *data, __u16 size, int timeout)
88 struct usb_device *usb_dev = atusb->usb_dev;
94 ret = usb_control_msg(usb_dev, pipe, request, requesttype,
95 value, index, data, size, timeout);
98 dev_err(&usb_dev->dev,
99 "%s: req 0x%02x val 0x%x idx 0x%x, error %d\n",
100 __func__, request, value, index, ret);
105 static int atusb_command(struct atusb *atusb, u8 cmd, u8 arg)
107 struct usb_device *usb_dev = atusb->usb_dev;
109 dev_dbg(&usb_dev->dev, "%s: cmd = 0x%x\n", __func__, cmd);
110 return atusb_control_msg(atusb, usb_sndctrlpipe(usb_dev, 0),
111 cmd, ATUSB_REQ_TO_DEV, arg, 0, NULL, 0, 1000);
114 static int atusb_write_reg(struct atusb *atusb, u8 reg, u8 value)
116 struct usb_device *usb_dev = atusb->usb_dev;
118 dev_dbg(&usb_dev->dev, "%s: 0x%02x <- 0x%02x\n", __func__, reg, value);
119 return atusb_control_msg(atusb, usb_sndctrlpipe(usb_dev, 0),
120 ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
121 value, reg, NULL, 0, 1000);
124 static int atusb_read_reg(struct atusb *atusb, u8 reg)
126 struct usb_device *usb_dev = atusb->usb_dev;
131 buffer = kmalloc(1, GFP_KERNEL);
135 dev_dbg(&usb_dev->dev, "%s: reg = 0x%x\n", __func__, reg);
136 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
137 ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
138 0, reg, buffer, 1, 1000);
150 static int atusb_write_subreg(struct atusb *atusb, u8 reg, u8 mask,
153 struct usb_device *usb_dev = atusb->usb_dev;
157 dev_dbg(&usb_dev->dev, "%s: 0x%02x <- 0x%02x\n", __func__, reg, value);
159 orig = atusb_read_reg(atusb, reg);
161 /* Write the value only into that part of the register which is allowed
162 * by the mask. All other bits stay as before.
165 tmp |= (value << shift) & mask;
168 ret = atusb_write_reg(atusb, reg, tmp);
173 static int atusb_read_subreg(struct atusb *lp,
174 unsigned int addr, unsigned int mask,
179 rc = atusb_read_reg(lp, addr);
180 rc = (rc & mask) >> shift;
185 static int atusb_get_and_clear_error(struct atusb *atusb)
187 int err = atusb->err;
193 /* ----- skb allocation ---------------------------------------------------- */
196 #define MAX_RX_XFER (1 + MAX_PSDU + 2 + 1) /* PHR+PSDU+CRC+LQI */
198 #define SKB_ATUSB(skb) (*(struct atusb **)(skb)->cb)
200 static void atusb_in(struct urb *urb);
202 static int atusb_submit_rx_urb(struct atusb *atusb, struct urb *urb)
204 struct usb_device *usb_dev = atusb->usb_dev;
205 struct sk_buff *skb = urb->context;
209 skb = alloc_skb(MAX_RX_XFER, GFP_KERNEL);
211 dev_warn_ratelimited(&usb_dev->dev,
212 "atusb_in: can't allocate skb\n");
215 skb_put(skb, MAX_RX_XFER);
216 SKB_ATUSB(skb) = atusb;
219 usb_fill_bulk_urb(urb, usb_dev, usb_rcvbulkpipe(usb_dev, 1),
220 skb->data, MAX_RX_XFER, atusb_in, skb);
221 usb_anchor_urb(urb, &atusb->rx_urbs);
223 ret = usb_submit_urb(urb, GFP_KERNEL);
225 usb_unanchor_urb(urb);
232 static void atusb_work_urbs(struct work_struct *work)
234 struct atusb *atusb =
235 container_of(to_delayed_work(work), struct atusb, work);
236 struct usb_device *usb_dev = atusb->usb_dev;
244 urb = usb_get_from_anchor(&atusb->idle_urbs);
247 ret = atusb_submit_rx_urb(atusb, urb);
250 usb_anchor_urb(urb, &atusb->idle_urbs);
251 dev_warn_ratelimited(&usb_dev->dev,
252 "atusb_in: can't allocate/submit URB (%d)\n", ret);
253 schedule_delayed_work(&atusb->work,
254 msecs_to_jiffies(ATUSB_ALLOC_DELAY_MS) + 1);
257 /* ----- Asynchronous USB -------------------------------------------------- */
259 static void atusb_tx_done(struct atusb *atusb, u8 seq)
261 struct usb_device *usb_dev = atusb->usb_dev;
262 u8 expect = atusb->tx_ack_seq;
264 dev_dbg(&usb_dev->dev, "%s (0x%02x/0x%02x)\n", __func__, seq, expect);
266 /* TODO check for ifs handling in firmware */
267 ieee802154_xmit_complete(atusb->hw, atusb->tx_skb, false);
269 /* TODO I experience this case when atusb has a tx complete
270 * irq before probing, we should fix the firmware it's an
271 * unlikely case now that seq == expect is then true, but can
272 * happen and fail with a tx_skb = NULL;
274 ieee802154_wake_queue(atusb->hw);
276 dev_kfree_skb_irq(atusb->tx_skb);
280 static void atusb_in_good(struct urb *urb)
282 struct usb_device *usb_dev = urb->dev;
283 struct sk_buff *skb = urb->context;
284 struct atusb *atusb = SKB_ATUSB(skb);
287 if (!urb->actual_length) {
288 dev_dbg(&usb_dev->dev, "atusb_in: zero-sized URB ?\n");
294 if (urb->actual_length == 1) {
295 atusb_tx_done(atusb, len);
299 if (len + 1 > urb->actual_length - 1) {
300 dev_dbg(&usb_dev->dev, "atusb_in: frame len %d+1 > URB %u-1\n",
301 len, urb->actual_length);
305 if (!ieee802154_is_valid_psdu_len(len)) {
306 dev_dbg(&usb_dev->dev, "atusb_in: frame corrupted\n");
310 lqi = skb->data[len + 1];
311 dev_dbg(&usb_dev->dev, "atusb_in: rx len %d lqi 0x%02x\n", len, lqi);
312 skb_pull(skb, 1); /* remove PHR */
313 skb_trim(skb, len); /* get payload only */
314 ieee802154_rx_irqsafe(atusb->hw, skb, lqi);
315 urb->context = NULL; /* skb is gone */
318 static void atusb_in(struct urb *urb)
320 struct usb_device *usb_dev = urb->dev;
321 struct sk_buff *skb = urb->context;
322 struct atusb *atusb = SKB_ATUSB(skb);
324 dev_dbg(&usb_dev->dev, "%s: status %d len %d\n", __func__,
325 urb->status, urb->actual_length);
327 if (urb->status == -ENOENT) { /* being killed */
332 dev_dbg(&usb_dev->dev, "%s: URB error %d\n", __func__, urb->status);
337 usb_anchor_urb(urb, &atusb->idle_urbs);
338 if (!atusb->shutdown)
339 schedule_delayed_work(&atusb->work, 0);
342 /* ----- URB allocation/deallocation --------------------------------------- */
344 static void atusb_free_urbs(struct atusb *atusb)
349 urb = usb_get_from_anchor(&atusb->idle_urbs);
352 kfree_skb(urb->context);
357 static int atusb_alloc_urbs(struct atusb *atusb, int n)
362 urb = usb_alloc_urb(0, GFP_KERNEL);
364 atusb_free_urbs(atusb);
367 usb_anchor_urb(urb, &atusb->idle_urbs);
374 /* ----- IEEE 802.15.4 interface operations -------------------------------- */
376 static void atusb_xmit_complete(struct urb *urb)
378 dev_dbg(&urb->dev->dev, "atusb_xmit urb completed");
381 static int atusb_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
383 struct atusb *atusb = hw->priv;
384 struct usb_device *usb_dev = atusb->usb_dev;
387 dev_dbg(&usb_dev->dev, "%s (%d)\n", __func__, skb->len);
390 atusb->tx_dr.wIndex = cpu_to_le16(atusb->tx_ack_seq);
391 atusb->tx_dr.wLength = cpu_to_le16(skb->len);
393 usb_fill_control_urb(atusb->tx_urb, usb_dev,
394 usb_sndctrlpipe(usb_dev, 0),
395 (unsigned char *)&atusb->tx_dr, skb->data,
396 skb->len, atusb_xmit_complete, NULL);
397 ret = usb_submit_urb(atusb->tx_urb, GFP_ATOMIC);
398 dev_dbg(&usb_dev->dev, "%s done (%d)\n", __func__, ret);
402 static int atusb_ed(struct ieee802154_hw *hw, u8 *level)
409 static int atusb_set_hw_addr_filt(struct ieee802154_hw *hw,
410 struct ieee802154_hw_addr_filt *filt,
411 unsigned long changed)
413 struct atusb *atusb = hw->priv;
414 struct device *dev = &atusb->usb_dev->dev;
416 if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
417 u16 addr = le16_to_cpu(filt->short_addr);
419 dev_vdbg(dev, "%s called for saddr\n", __func__);
420 atusb_write_reg(atusb, RG_SHORT_ADDR_0, addr);
421 atusb_write_reg(atusb, RG_SHORT_ADDR_1, addr >> 8);
424 if (changed & IEEE802154_AFILT_PANID_CHANGED) {
425 u16 pan = le16_to_cpu(filt->pan_id);
427 dev_vdbg(dev, "%s called for pan id\n", __func__);
428 atusb_write_reg(atusb, RG_PAN_ID_0, pan);
429 atusb_write_reg(atusb, RG_PAN_ID_1, pan >> 8);
432 if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
433 u8 i, addr[IEEE802154_EXTENDED_ADDR_LEN];
435 memcpy(addr, &filt->ieee_addr, IEEE802154_EXTENDED_ADDR_LEN);
436 dev_vdbg(dev, "%s called for IEEE addr\n", __func__);
437 for (i = 0; i < 8; i++)
438 atusb_write_reg(atusb, RG_IEEE_ADDR_0 + i, addr[i]);
441 if (changed & IEEE802154_AFILT_PANC_CHANGED) {
442 dev_vdbg(dev, "%s called for panc change\n", __func__);
444 atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 1);
446 atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 0);
449 return atusb_get_and_clear_error(atusb);
452 static int atusb_start(struct ieee802154_hw *hw)
454 struct atusb *atusb = hw->priv;
455 struct usb_device *usb_dev = atusb->usb_dev;
458 dev_dbg(&usb_dev->dev, "%s\n", __func__);
459 schedule_delayed_work(&atusb->work, 0);
460 atusb_command(atusb, ATUSB_RX_MODE, 1);
461 ret = atusb_get_and_clear_error(atusb);
463 usb_kill_anchored_urbs(&atusb->idle_urbs);
467 static void atusb_stop(struct ieee802154_hw *hw)
469 struct atusb *atusb = hw->priv;
470 struct usb_device *usb_dev = atusb->usb_dev;
472 dev_dbg(&usb_dev->dev, "%s\n", __func__);
473 usb_kill_anchored_urbs(&atusb->idle_urbs);
474 atusb_command(atusb, ATUSB_RX_MODE, 0);
475 atusb_get_and_clear_error(atusb);
478 #define ATUSB_MAX_TX_POWERS 0xF
479 static const s32 atusb_powers[ATUSB_MAX_TX_POWERS + 1] = {
480 300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700,
485 atusb_txpower(struct ieee802154_hw *hw, s32 mbm)
487 struct atusb *atusb = hw->priv;
490 return atusb->data->set_txpower(hw, mbm);
496 atusb_set_txpower(struct ieee802154_hw *hw, s32 mbm)
498 struct atusb *atusb = hw->priv;
501 for (i = 0; i < hw->phy->supported.tx_powers_size; i++) {
502 if (hw->phy->supported.tx_powers[i] == mbm)
503 return atusb_write_subreg(atusb, SR_TX_PWR_23X, i);
510 hulusb_set_txpower(struct ieee802154_hw *hw, s32 mbm)
514 for (i = 0; i < hw->phy->supported.tx_powers_size; i++) {
515 if (hw->phy->supported.tx_powers[i] == mbm)
516 return atusb_write_subreg(hw->priv, SR_TX_PWR_212, i);
522 #define ATUSB_MAX_ED_LEVELS 0xF
523 static const s32 atusb_ed_levels[ATUSB_MAX_ED_LEVELS + 1] = {
524 -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300,
525 -7100, -6900, -6700, -6500, -6300, -6100,
528 #define AT86RF212_MAX_TX_POWERS 0x1F
529 static const s32 at86rf212_powers[AT86RF212_MAX_TX_POWERS + 1] = {
530 500, 400, 300, 200, 100, 0, -100, -200, -300, -400, -500, -600, -700,
531 -800, -900, -1000, -1100, -1200, -1300, -1400, -1500, -1600, -1700,
532 -1800, -1900, -2000, -2100, -2200, -2300, -2400, -2500, -2600,
535 #define AT86RF2XX_MAX_ED_LEVELS 0xF
536 static const s32 at86rf212_ed_levels_100[AT86RF2XX_MAX_ED_LEVELS + 1] = {
537 -10000, -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200,
538 -8000, -7800, -7600, -7400, -7200, -7000,
541 static const s32 at86rf212_ed_levels_98[AT86RF2XX_MAX_ED_LEVELS + 1] = {
542 -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200, -8000,
543 -7800, -7600, -7400, -7200, -7000, -6800,
547 atusb_set_cca_mode(struct ieee802154_hw *hw, const struct wpan_phy_cca *cca)
549 struct atusb *atusb = hw->priv;
552 /* mapping 802.15.4 to driver spec */
554 case NL802154_CCA_ENERGY:
557 case NL802154_CCA_CARRIER:
560 case NL802154_CCA_ENERGY_CARRIER:
562 case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
565 case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
576 return atusb_write_subreg(atusb, SR_CCA_MODE, val);
579 static int hulusb_set_cca_ed_level(struct atusb *lp, int rssi_base_val)
581 unsigned int cca_ed_thres;
583 cca_ed_thres = atusb_read_subreg(lp, SR_CCA_ED_THRES);
585 switch (rssi_base_val) {
587 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_98;
588 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_98);
589 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_98[cca_ed_thres];
592 lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
593 lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
594 lp->hw->phy->cca_ed_level = at86rf212_ed_levels_100[cca_ed_thres];
604 atusb_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
606 struct atusb *atusb = hw->priv;
609 for (i = 0; i < hw->phy->supported.cca_ed_levels_size; i++) {
610 if (hw->phy->supported.cca_ed_levels[i] == mbm)
611 return atusb_write_subreg(atusb, SR_CCA_ED_THRES, i);
617 static int atusb_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
619 struct atusb *atusb = hw->priv;
623 ret = atusb->data->set_channel(hw, page, channel);
624 /* @@@ ugly synchronization */
625 msleep(atusb->data->t_channel_switch);
631 static int atusb_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
633 struct atusb *atusb = hw->priv;
636 ret = atusb_write_subreg(atusb, SR_CHANNEL, channel);
642 static int hulusb_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
647 struct atusb *lp = hw->priv;
650 rc = atusb_write_subreg(lp, SR_SUB_MODE, 0);
652 rc = atusb_write_subreg(lp, SR_SUB_MODE, 1);
657 rc = atusb_write_subreg(lp, SR_BPSK_QPSK, 0);
658 rssi_base_val = -100;
660 rc = atusb_write_subreg(lp, SR_BPSK_QPSK, 1);
666 rc = hulusb_set_cca_ed_level(lp, rssi_base_val);
670 /* This sets the symbol_duration according frequency on the 212.
671 * TODO move this handling while set channel and page in cfg802154.
672 * We can do that, this timings are according 802.15.4 standard.
673 * If we do that in cfg802154, this is a more generic calculation.
675 * This should also protected from ifs_timer. Means cancel timer and
676 * init with a new value. For now, this is okay.
680 /* SUB:0 and BPSK:0 -> BPSK-20 */
681 lp->hw->phy->symbol_duration = 50;
683 /* SUB:1 and BPSK:0 -> BPSK-40 */
684 lp->hw->phy->symbol_duration = 25;
688 /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */
689 lp->hw->phy->symbol_duration = 40;
691 /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */
692 lp->hw->phy->symbol_duration = 16;
695 lp->hw->phy->lifs_period = IEEE802154_LIFS_PERIOD *
696 lp->hw->phy->symbol_duration;
697 lp->hw->phy->sifs_period = IEEE802154_SIFS_PERIOD *
698 lp->hw->phy->symbol_duration;
700 return atusb_write_subreg(lp, SR_CHANNEL, channel);
704 atusb_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be, u8 retries)
706 struct atusb *atusb = hw->priv;
709 ret = atusb_write_subreg(atusb, SR_MIN_BE, min_be);
713 ret = atusb_write_subreg(atusb, SR_MAX_BE, max_be);
717 return atusb_write_subreg(atusb, SR_MAX_CSMA_RETRIES, retries);
721 hulusb_set_lbt(struct ieee802154_hw *hw, bool on)
723 struct atusb *atusb = hw->priv;
725 return atusb_write_subreg(atusb, SR_CSMA_LBT_MODE, on);
729 atusb_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
731 struct atusb *atusb = hw->priv;
733 return atusb_write_subreg(atusb, SR_MAX_FRAME_RETRIES, retries);
737 atusb_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
739 struct atusb *atusb = hw->priv;
743 ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 1);
747 ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 1);
751 ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 0);
755 ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 0);
763 static struct atusb_chip_data atusb_chip_data = {
764 .t_channel_switch = 1,
765 .rssi_base_val = -91,
766 .set_txpower = atusb_set_txpower,
767 .set_channel = atusb_set_channel,
770 static struct atusb_chip_data hulusb_chip_data = {
771 .t_channel_switch = 11,
772 .rssi_base_val = -100,
773 .set_txpower = hulusb_set_txpower,
774 .set_channel = hulusb_set_channel,
777 static const struct ieee802154_ops atusb_ops = {
778 .owner = THIS_MODULE,
779 .xmit_async = atusb_xmit,
781 .set_channel = atusb_channel,
782 .start = atusb_start,
784 .set_hw_addr_filt = atusb_set_hw_addr_filt,
785 .set_txpower = atusb_txpower,
786 .set_lbt = hulusb_set_lbt,
787 .set_cca_mode = atusb_set_cca_mode,
788 .set_cca_ed_level = atusb_set_cca_ed_level,
789 .set_csma_params = atusb_set_csma_params,
790 .set_frame_retries = atusb_set_frame_retries,
791 .set_promiscuous_mode = atusb_set_promiscuous_mode,
794 /* ----- Firmware and chip version information ----------------------------- */
796 static int atusb_get_and_show_revision(struct atusb *atusb)
798 struct usb_device *usb_dev = atusb->usb_dev;
800 unsigned char *buffer;
803 buffer = kmalloc(3, GFP_KERNEL);
807 /* Get a couple of the ATMega Firmware values */
808 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
809 ATUSB_ID, ATUSB_REQ_FROM_DEV, 0, 0,
812 atusb->fw_ver_maj = buffer[0];
813 atusb->fw_ver_min = buffer[1];
814 atusb->fw_hw_type = buffer[2];
816 switch (atusb->fw_hw_type) {
817 case ATUSB_HW_TYPE_100813:
818 case ATUSB_HW_TYPE_101216:
819 case ATUSB_HW_TYPE_110131:
821 atusb->data = &atusb_chip_data;
823 case ATUSB_HW_TYPE_RZUSB:
825 atusb->data = &atusb_chip_data;
827 case ATUSB_HW_TYPE_HULUSB:
829 atusb->data = &hulusb_chip_data;
833 atusb->err = -ENOTSUPP;
838 dev_info(&usb_dev->dev,
839 "Firmware: major: %u, minor: %u, hardware type: %s (%d)\n",
840 atusb->fw_ver_maj, atusb->fw_ver_min, hw_name,
843 if (atusb->fw_ver_maj == 0 && atusb->fw_ver_min < 2) {
844 dev_info(&usb_dev->dev,
845 "Firmware version (%u.%u) predates our first public release.",
846 atusb->fw_ver_maj, atusb->fw_ver_min);
847 dev_info(&usb_dev->dev, "Please update to version 0.2 or newer");
854 static int atusb_get_and_show_build(struct atusb *atusb)
856 struct usb_device *usb_dev = atusb->usb_dev;
860 build = kmalloc(ATUSB_BUILD_SIZE + 1, GFP_KERNEL);
864 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
865 ATUSB_BUILD, ATUSB_REQ_FROM_DEV, 0, 0,
866 build, ATUSB_BUILD_SIZE, 1000);
869 dev_info(&usb_dev->dev, "Firmware: build %s\n", build);
876 static int atusb_get_and_conf_chip(struct atusb *atusb)
878 struct usb_device *usb_dev = atusb->usb_dev;
879 u8 man_id_0, man_id_1, part_num, version_num;
881 struct ieee802154_hw *hw = atusb->hw;
883 man_id_0 = atusb_read_reg(atusb, RG_MAN_ID_0);
884 man_id_1 = atusb_read_reg(atusb, RG_MAN_ID_1);
885 part_num = atusb_read_reg(atusb, RG_PART_NUM);
886 version_num = atusb_read_reg(atusb, RG_VERSION_NUM);
891 hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AFILT |
892 IEEE802154_HW_PROMISCUOUS | IEEE802154_HW_CSMA_PARAMS;
894 hw->phy->flags = WPAN_PHY_FLAG_TXPOWER | WPAN_PHY_FLAG_CCA_ED_LEVEL |
895 WPAN_PHY_FLAG_CCA_MODE;
897 hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) |
898 BIT(NL802154_CCA_CARRIER) |
899 BIT(NL802154_CCA_ENERGY_CARRIER);
900 hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) |
901 BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR);
903 hw->phy->cca.mode = NL802154_CCA_ENERGY;
905 hw->phy->current_page = 0;
907 if ((man_id_1 << 8 | man_id_0) != ATUSB_JEDEC_ATMEL) {
908 dev_err(&usb_dev->dev,
909 "non-Atmel transceiver xxxx%02x%02x\n",
917 atusb->hw->phy->supported.channels[0] = 0x7FFF800;
918 atusb->hw->phy->current_channel = 11; /* reset default */
919 atusb->hw->phy->symbol_duration = 16;
920 atusb->hw->phy->supported.tx_powers = atusb_powers;
921 atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers);
922 hw->phy->supported.cca_ed_levels = atusb_ed_levels;
923 hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels);
927 atusb->hw->phy->supported.channels[0] = 0x7FFF800;
928 atusb->hw->phy->current_channel = 11; /* reset default */
929 atusb->hw->phy->symbol_duration = 16;
930 atusb->hw->phy->supported.tx_powers = atusb_powers;
931 atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers);
932 hw->phy->supported.cca_ed_levels = atusb_ed_levels;
933 hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels);
937 atusb->hw->flags |= IEEE802154_HW_LBT;
938 atusb->hw->phy->supported.channels[0] = 0x00007FF;
939 atusb->hw->phy->supported.channels[2] = 0x00007FF;
940 atusb->hw->phy->current_channel = 5;
941 atusb->hw->phy->symbol_duration = 25;
942 atusb->hw->phy->supported.lbt = NL802154_SUPPORTED_BOOL_BOTH;
943 atusb->hw->phy->supported.tx_powers = at86rf212_powers;
944 atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf212_powers);
945 atusb->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
946 atusb->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
949 dev_err(&usb_dev->dev,
950 "unexpected transceiver, part 0x%02x version 0x%02x\n",
951 part_num, version_num);
955 hw->phy->transmit_power = hw->phy->supported.tx_powers[0];
956 hw->phy->cca_ed_level = hw->phy->supported.cca_ed_levels[7];
958 dev_info(&usb_dev->dev, "ATUSB: %s version %d\n", chip, version_num);
963 atusb->err = -ENODEV;
967 static int atusb_set_extended_addr(struct atusb *atusb)
969 struct usb_device *usb_dev = atusb->usb_dev;
970 unsigned char *buffer;
971 __le64 extended_addr;
975 /* Firmware versions before 0.3 do not support the EUI64_READ command.
976 * Just use a random address and be done.
978 if (atusb->fw_ver_maj == 0 && atusb->fw_ver_min < 3) {
979 ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
983 buffer = kmalloc(IEEE802154_EXTENDED_ADDR_LEN, GFP_KERNEL);
987 /* Firmware is new enough so we fetch the address from EEPROM */
988 ret = atusb_control_msg(atusb, usb_rcvctrlpipe(usb_dev, 0),
989 ATUSB_EUI64_READ, ATUSB_REQ_FROM_DEV, 0, 0,
990 buffer, IEEE802154_EXTENDED_ADDR_LEN, 1000);
992 dev_err(&usb_dev->dev, "failed to fetch extended address, random address set\n");
993 ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
998 memcpy(&extended_addr, buffer, IEEE802154_EXTENDED_ADDR_LEN);
999 /* Check if read address is not empty and the unicast bit is set correctly */
1000 if (!ieee802154_is_valid_extended_unicast_addr(extended_addr)) {
1001 dev_info(&usb_dev->dev, "no permanent extended address found, random address set\n");
1002 ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
1004 atusb->hw->phy->perm_extended_addr = extended_addr;
1005 addr = swab64((__force u64)atusb->hw->phy->perm_extended_addr);
1006 dev_info(&usb_dev->dev, "Read permanent extended address %8phC from device\n",
1014 /* ----- Setup ------------------------------------------------------------- */
1016 static int atusb_probe(struct usb_interface *interface,
1017 const struct usb_device_id *id)
1019 struct usb_device *usb_dev = interface_to_usbdev(interface);
1020 struct ieee802154_hw *hw;
1021 struct atusb *atusb = NULL;
1024 hw = ieee802154_alloc_hw(sizeof(struct atusb), &atusb_ops);
1030 atusb->usb_dev = usb_get_dev(usb_dev);
1031 usb_set_intfdata(interface, atusb);
1033 atusb->shutdown = 0;
1035 INIT_DELAYED_WORK(&atusb->work, atusb_work_urbs);
1036 init_usb_anchor(&atusb->idle_urbs);
1037 init_usb_anchor(&atusb->rx_urbs);
1039 if (atusb_alloc_urbs(atusb, ATUSB_NUM_RX_URBS))
1042 atusb->tx_dr.bRequestType = ATUSB_REQ_TO_DEV;
1043 atusb->tx_dr.bRequest = ATUSB_TX;
1044 atusb->tx_dr.wValue = cpu_to_le16(0);
1046 atusb->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
1050 hw->parent = &usb_dev->dev;
1052 atusb_command(atusb, ATUSB_RF_RESET, 0);
1053 atusb_get_and_conf_chip(atusb);
1054 atusb_get_and_show_revision(atusb);
1055 atusb_get_and_show_build(atusb);
1056 atusb_set_extended_addr(atusb);
1058 if ((atusb->fw_ver_maj == 0 && atusb->fw_ver_min >= 3) || atusb->fw_ver_maj > 0)
1059 hw->flags |= IEEE802154_HW_FRAME_RETRIES;
1061 ret = atusb_get_and_clear_error(atusb);
1063 dev_err(&atusb->usb_dev->dev,
1064 "%s: initialization failed, error = %d\n",
1069 ret = ieee802154_register_hw(hw);
1073 /* If we just powered on, we're now in P_ON and need to enter TRX_OFF
1074 * explicitly. Any resets after that will send us straight to TRX_OFF,
1075 * making the command below redundant.
1077 atusb_write_reg(atusb, RG_TRX_STATE, STATE_FORCE_TRX_OFF);
1078 msleep(1); /* reset => TRX_OFF, tTR13 = 37 us */
1081 /* Calculating the maximum time available to empty the frame buffer
1084 * According to [1], the inter-frame gap is
1085 * R * 20 * 16 us + 128 us
1086 * where R is a random number from 0 to 7. Furthermore, we have 20 bit
1087 * times (80 us at 250 kbps) of SHR of the next frame before the
1088 * transceiver begins storing data in the frame buffer.
1090 * This yields a minimum time of 208 us between the last data of a
1091 * frame and the first data of the next frame. This time is further
1092 * reduced by interrupt latency in the atusb firmware.
1094 * atusb currently needs about 500 us to retrieve a maximum-sized
1095 * frame. We therefore have to allow reception of a new frame to begin
1096 * while we retrieve the previous frame.
1098 * [1] "JN-AN-1035 Calculating data rates in an IEEE 802.15.4-based
1099 * network", Jennic 2006.
1100 * http://www.jennic.com/download_file.php?supportFile=JN-AN-1035%20Calculating%20802-15-4%20Data%20Rates-1v0.pdf
1103 atusb_write_subreg(atusb, SR_RX_SAFE_MODE, 1);
1105 atusb_write_reg(atusb, RG_IRQ_MASK, 0xff);
1107 ret = atusb_get_and_clear_error(atusb);
1111 dev_err(&atusb->usb_dev->dev,
1112 "%s: setup failed, error = %d\n",
1115 ieee802154_unregister_hw(hw);
1117 atusb_free_urbs(atusb);
1118 usb_kill_urb(atusb->tx_urb);
1119 usb_free_urb(atusb->tx_urb);
1120 usb_put_dev(usb_dev);
1121 ieee802154_free_hw(hw);
1125 static void atusb_disconnect(struct usb_interface *interface)
1127 struct atusb *atusb = usb_get_intfdata(interface);
1129 dev_dbg(&atusb->usb_dev->dev, "%s\n", __func__);
1131 atusb->shutdown = 1;
1132 cancel_delayed_work_sync(&atusb->work);
1134 usb_kill_anchored_urbs(&atusb->rx_urbs);
1135 atusb_free_urbs(atusb);
1136 usb_kill_urb(atusb->tx_urb);
1137 usb_free_urb(atusb->tx_urb);
1139 ieee802154_unregister_hw(atusb->hw);
1141 usb_put_dev(atusb->usb_dev);
1143 ieee802154_free_hw(atusb->hw);
1145 usb_set_intfdata(interface, NULL);
1147 pr_debug("%s done\n", __func__);
1150 /* The devices we work with */
1151 static const struct usb_device_id atusb_device_table[] = {
1153 .match_flags = USB_DEVICE_ID_MATCH_DEVICE |
1154 USB_DEVICE_ID_MATCH_INT_INFO,
1155 .idVendor = ATUSB_VENDOR_ID,
1156 .idProduct = ATUSB_PRODUCT_ID,
1157 .bInterfaceClass = USB_CLASS_VENDOR_SPEC
1159 /* end with null element */
1162 MODULE_DEVICE_TABLE(usb, atusb_device_table);
1164 static struct usb_driver atusb_driver = {
1166 .probe = atusb_probe,
1167 .disconnect = atusb_disconnect,
1168 .id_table = atusb_device_table,
1170 module_usb_driver(atusb_driver);
1177 MODULE_DESCRIPTION("ATUSB IEEE 802.15.4 Driver");
1178 MODULE_LICENSE("GPL");