Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * atusb.c - Driver for the ATUSB IEEE 802.15.4 dongle
0004  *
0005  * Written 2013 by Werner Almesberger <werner@almesberger.net>
0006  *
0007  * Copyright (c) 2015 - 2016 Stefan Schmidt <stefan@datenfreihafen.org>
0008  *
0009  * Based on at86rf230.c and spi_atusb.c.
0010  * at86rf230.c is
0011  * Copyright (C) 2009 Siemens AG
0012  * Written by: Dmitry Eremin-Solenikov <dmitry.baryshkov@siemens.com>
0013  *
0014  * spi_atusb.c is
0015  * Copyright (c) 2011 Richard Sharpe <realrichardsharpe@gmail.com>
0016  * Copyright (c) 2011 Stefan Schmidt <stefan@datenfreihafen.org>
0017  * Copyright (c) 2011 Werner Almesberger <werner@almesberger.net>
0018  *
0019  * USB initialization is
0020  * Copyright (c) 2013 Alexander Aring <alex.aring@gmail.com>
0021  *
0022  * Busware HUL support is
0023  * Copyright (c) 2017 Josef Filzmaier <j.filzmaier@gmx.at>
0024  */
0025 
0026 #include <linux/kernel.h>
0027 #include <linux/slab.h>
0028 #include <linux/module.h>
0029 #include <linux/jiffies.h>
0030 #include <linux/usb.h>
0031 #include <linux/skbuff.h>
0032 
0033 #include <net/cfg802154.h>
0034 #include <net/mac802154.h>
0035 
0036 #include "at86rf230.h"
0037 #include "atusb.h"
0038 
0039 #define ATUSB_JEDEC_ATMEL   0x1f    /* JEDEC manufacturer ID */
0040 
0041 #define ATUSB_NUM_RX_URBS   4   /* allow for a bit of local latency */
0042 #define ATUSB_ALLOC_DELAY_MS    100 /* delay after failed allocation */
0043 #define ATUSB_TX_TIMEOUT_MS 200 /* on the air timeout */
0044 
0045 struct atusb {
0046     struct ieee802154_hw *hw;
0047     struct usb_device *usb_dev;
0048     struct atusb_chip_data *data;
0049     int shutdown;           /* non-zero if shutting down */
0050     int err;            /* set by first error */
0051 
0052     /* RX variables */
0053     struct delayed_work work;   /* memory allocations */
0054     struct usb_anchor idle_urbs;    /* URBs waiting to be submitted */
0055     struct usb_anchor rx_urbs;  /* URBs waiting for reception */
0056 
0057     /* TX variables */
0058     struct usb_ctrlrequest tx_dr;
0059     struct urb *tx_urb;
0060     struct sk_buff *tx_skb;
0061     u8 tx_ack_seq;      /* current TX ACK sequence number */
0062 
0063     /* Firmware variable */
0064     unsigned char fw_ver_maj;   /* Firmware major version number */
0065     unsigned char fw_ver_min;   /* Firmware minor version number */
0066     unsigned char fw_hw_type;   /* Firmware hardware type */
0067 };
0068 
0069 struct atusb_chip_data {
0070     u16 t_channel_switch;
0071     int rssi_base_val;
0072 
0073     int (*set_channel)(struct ieee802154_hw*, u8, u8);
0074     int (*set_txpower)(struct ieee802154_hw*, s32);
0075 };
0076 
0077 static int atusb_write_subreg(struct atusb *atusb, u8 reg, u8 mask,
0078                   u8 shift, u8 value)
0079 {
0080     struct usb_device *usb_dev = atusb->usb_dev;
0081     u8 orig, tmp;
0082     int ret = 0;
0083 
0084     dev_dbg(&usb_dev->dev, "%s: 0x%02x <- 0x%02x\n", __func__, reg, value);
0085 
0086     ret = usb_control_msg_recv(usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
0087                    0, reg, &orig, 1, 1000, GFP_KERNEL);
0088     if (ret < 0)
0089         return ret;
0090 
0091     /* Write the value only into that part of the register which is allowed
0092      * by the mask. All other bits stay as before.
0093      */
0094     tmp = orig & ~mask;
0095     tmp |= (value << shift) & mask;
0096 
0097     if (tmp != orig)
0098         ret = usb_control_msg_send(usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
0099                        tmp, reg, NULL, 0, 1000, GFP_KERNEL);
0100 
0101     return ret;
0102 }
0103 
0104 static int atusb_read_subreg(struct atusb *lp,
0105                  unsigned int addr, unsigned int mask,
0106                  unsigned int shift)
0107 {
0108     int reg, ret;
0109 
0110     ret = usb_control_msg_recv(lp->usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
0111                    0, addr, &reg, 1, 1000, GFP_KERNEL);
0112     if (ret < 0)
0113         return ret;
0114 
0115     reg = (reg & mask) >> shift;
0116 
0117     return reg;
0118 }
0119 
0120 static int atusb_get_and_clear_error(struct atusb *atusb)
0121 {
0122     int err = atusb->err;
0123 
0124     atusb->err = 0;
0125     return err;
0126 }
0127 
0128 /* ----- skb allocation ---------------------------------------------------- */
0129 
0130 #define MAX_PSDU    127
0131 #define MAX_RX_XFER (1 + MAX_PSDU + 2 + 1)  /* PHR+PSDU+CRC+LQI */
0132 
0133 #define SKB_ATUSB(skb)  (*(struct atusb **)(skb)->cb)
0134 
0135 static void atusb_in(struct urb *urb);
0136 
0137 static int atusb_submit_rx_urb(struct atusb *atusb, struct urb *urb)
0138 {
0139     struct usb_device *usb_dev = atusb->usb_dev;
0140     struct sk_buff *skb = urb->context;
0141     int ret;
0142 
0143     if (!skb) {
0144         skb = alloc_skb(MAX_RX_XFER, GFP_KERNEL);
0145         if (!skb) {
0146             dev_warn_ratelimited(&usb_dev->dev,
0147                          "atusb_in: can't allocate skb\n");
0148             return -ENOMEM;
0149         }
0150         skb_put(skb, MAX_RX_XFER);
0151         SKB_ATUSB(skb) = atusb;
0152     }
0153 
0154     usb_fill_bulk_urb(urb, usb_dev, usb_rcvbulkpipe(usb_dev, 1),
0155               skb->data, MAX_RX_XFER, atusb_in, skb);
0156     usb_anchor_urb(urb, &atusb->rx_urbs);
0157 
0158     ret = usb_submit_urb(urb, GFP_KERNEL);
0159     if (ret) {
0160         usb_unanchor_urb(urb);
0161         kfree_skb(skb);
0162         urb->context = NULL;
0163     }
0164     return ret;
0165 }
0166 
0167 static void atusb_work_urbs(struct work_struct *work)
0168 {
0169     struct atusb *atusb =
0170         container_of(to_delayed_work(work), struct atusb, work);
0171     struct usb_device *usb_dev = atusb->usb_dev;
0172     struct urb *urb;
0173     int ret;
0174 
0175     if (atusb->shutdown)
0176         return;
0177 
0178     do {
0179         urb = usb_get_from_anchor(&atusb->idle_urbs);
0180         if (!urb)
0181             return;
0182         ret = atusb_submit_rx_urb(atusb, urb);
0183     } while (!ret);
0184 
0185     usb_anchor_urb(urb, &atusb->idle_urbs);
0186     dev_warn_ratelimited(&usb_dev->dev,
0187                  "atusb_in: can't allocate/submit URB (%d)\n", ret);
0188     schedule_delayed_work(&atusb->work,
0189                   msecs_to_jiffies(ATUSB_ALLOC_DELAY_MS) + 1);
0190 }
0191 
0192 /* ----- Asynchronous USB -------------------------------------------------- */
0193 
0194 static void atusb_tx_done(struct atusb *atusb, u8 seq)
0195 {
0196     struct usb_device *usb_dev = atusb->usb_dev;
0197     u8 expect = atusb->tx_ack_seq;
0198 
0199     dev_dbg(&usb_dev->dev, "%s (0x%02x/0x%02x)\n", __func__, seq, expect);
0200     if (seq == expect) {
0201         /* TODO check for ifs handling in firmware */
0202         ieee802154_xmit_complete(atusb->hw, atusb->tx_skb, false);
0203     } else {
0204         /* TODO I experience this case when atusb has a tx complete
0205          * irq before probing, we should fix the firmware it's an
0206          * unlikely case now that seq == expect is then true, but can
0207          * happen and fail with a tx_skb = NULL;
0208          */
0209         ieee802154_xmit_hw_error(atusb->hw, atusb->tx_skb);
0210     }
0211 }
0212 
0213 static void atusb_in_good(struct urb *urb)
0214 {
0215     struct usb_device *usb_dev = urb->dev;
0216     struct sk_buff *skb = urb->context;
0217     struct atusb *atusb = SKB_ATUSB(skb);
0218     u8 len, lqi;
0219 
0220     if (!urb->actual_length) {
0221         dev_dbg(&usb_dev->dev, "atusb_in: zero-sized URB ?\n");
0222         return;
0223     }
0224 
0225     len = *skb->data;
0226 
0227     if (urb->actual_length == 1) {
0228         atusb_tx_done(atusb, len);
0229         return;
0230     }
0231 
0232     if (len + 1 > urb->actual_length - 1) {
0233         dev_dbg(&usb_dev->dev, "atusb_in: frame len %d+1 > URB %u-1\n",
0234             len, urb->actual_length);
0235         return;
0236     }
0237 
0238     if (!ieee802154_is_valid_psdu_len(len)) {
0239         dev_dbg(&usb_dev->dev, "atusb_in: frame corrupted\n");
0240         return;
0241     }
0242 
0243     lqi = skb->data[len + 1];
0244     dev_dbg(&usb_dev->dev, "atusb_in: rx len %d lqi 0x%02x\n", len, lqi);
0245     skb_pull(skb, 1);   /* remove PHR */
0246     skb_trim(skb, len); /* get payload only */
0247     ieee802154_rx_irqsafe(atusb->hw, skb, lqi);
0248     urb->context = NULL;    /* skb is gone */
0249 }
0250 
0251 static void atusb_in(struct urb *urb)
0252 {
0253     struct usb_device *usb_dev = urb->dev;
0254     struct sk_buff *skb = urb->context;
0255     struct atusb *atusb = SKB_ATUSB(skb);
0256 
0257     dev_dbg(&usb_dev->dev, "%s: status %d len %d\n", __func__,
0258         urb->status, urb->actual_length);
0259     if (urb->status) {
0260         if (urb->status == -ENOENT) { /* being killed */
0261             kfree_skb(skb);
0262             urb->context = NULL;
0263             return;
0264         }
0265         dev_dbg(&usb_dev->dev, "%s: URB error %d\n", __func__, urb->status);
0266     } else {
0267         atusb_in_good(urb);
0268     }
0269 
0270     usb_anchor_urb(urb, &atusb->idle_urbs);
0271     if (!atusb->shutdown)
0272         schedule_delayed_work(&atusb->work, 0);
0273 }
0274 
0275 /* ----- URB allocation/deallocation --------------------------------------- */
0276 
0277 static void atusb_free_urbs(struct atusb *atusb)
0278 {
0279     struct urb *urb;
0280 
0281     while (1) {
0282         urb = usb_get_from_anchor(&atusb->idle_urbs);
0283         if (!urb)
0284             break;
0285         kfree_skb(urb->context);
0286         usb_free_urb(urb);
0287     }
0288 }
0289 
0290 static int atusb_alloc_urbs(struct atusb *atusb, int n)
0291 {
0292     struct urb *urb;
0293 
0294     while (n) {
0295         urb = usb_alloc_urb(0, GFP_KERNEL);
0296         if (!urb) {
0297             atusb_free_urbs(atusb);
0298             return -ENOMEM;
0299         }
0300         usb_anchor_urb(urb, &atusb->idle_urbs);
0301         usb_free_urb(urb);
0302         n--;
0303     }
0304     return 0;
0305 }
0306 
0307 /* ----- IEEE 802.15.4 interface operations -------------------------------- */
0308 
0309 static void atusb_xmit_complete(struct urb *urb)
0310 {
0311     dev_dbg(&urb->dev->dev, "atusb_xmit urb completed");
0312 }
0313 
0314 static int atusb_xmit(struct ieee802154_hw *hw, struct sk_buff *skb)
0315 {
0316     struct atusb *atusb = hw->priv;
0317     struct usb_device *usb_dev = atusb->usb_dev;
0318     int ret;
0319 
0320     dev_dbg(&usb_dev->dev, "%s (%d)\n", __func__, skb->len);
0321     atusb->tx_skb = skb;
0322     atusb->tx_ack_seq++;
0323     atusb->tx_dr.wIndex = cpu_to_le16(atusb->tx_ack_seq);
0324     atusb->tx_dr.wLength = cpu_to_le16(skb->len);
0325 
0326     usb_fill_control_urb(atusb->tx_urb, usb_dev,
0327                  usb_sndctrlpipe(usb_dev, 0),
0328                  (unsigned char *)&atusb->tx_dr, skb->data,
0329                  skb->len, atusb_xmit_complete, NULL);
0330     ret = usb_submit_urb(atusb->tx_urb, GFP_ATOMIC);
0331     dev_dbg(&usb_dev->dev, "%s done (%d)\n", __func__, ret);
0332     return ret;
0333 }
0334 
0335 static int atusb_ed(struct ieee802154_hw *hw, u8 *level)
0336 {
0337     WARN_ON(!level);
0338     *level = 0xbe;
0339     return 0;
0340 }
0341 
0342 static int atusb_set_hw_addr_filt(struct ieee802154_hw *hw,
0343                   struct ieee802154_hw_addr_filt *filt,
0344                   unsigned long changed)
0345 {
0346     struct atusb *atusb = hw->priv;
0347     struct device *dev = &atusb->usb_dev->dev;
0348 
0349     if (changed & IEEE802154_AFILT_SADDR_CHANGED) {
0350         u16 addr = le16_to_cpu(filt->short_addr);
0351 
0352         dev_vdbg(dev, "%s called for saddr\n", __func__);
0353         usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
0354                      addr, RG_SHORT_ADDR_0, NULL, 0, 1000, GFP_KERNEL);
0355 
0356         usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
0357                      addr >> 8, RG_SHORT_ADDR_1, NULL, 0, 1000, GFP_KERNEL);
0358     }
0359 
0360     if (changed & IEEE802154_AFILT_PANID_CHANGED) {
0361         u16 pan = le16_to_cpu(filt->pan_id);
0362 
0363         dev_vdbg(dev, "%s called for pan id\n", __func__);
0364         usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
0365                      pan, RG_PAN_ID_0, NULL, 0, 1000, GFP_KERNEL);
0366 
0367         usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
0368                      pan >> 8, RG_PAN_ID_1, NULL, 0, 1000, GFP_KERNEL);
0369     }
0370 
0371     if (changed & IEEE802154_AFILT_IEEEADDR_CHANGED) {
0372         u8 i, addr[IEEE802154_EXTENDED_ADDR_LEN];
0373 
0374         memcpy(addr, &filt->ieee_addr, IEEE802154_EXTENDED_ADDR_LEN);
0375         dev_vdbg(dev, "%s called for IEEE addr\n", __func__);
0376         for (i = 0; i < 8; i++)
0377             usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
0378                          addr[i], RG_IEEE_ADDR_0 + i, NULL, 0,
0379                          1000, GFP_KERNEL);
0380     }
0381 
0382     if (changed & IEEE802154_AFILT_PANC_CHANGED) {
0383         dev_vdbg(dev, "%s called for panc change\n", __func__);
0384         if (filt->pan_coord)
0385             atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 1);
0386         else
0387             atusb_write_subreg(atusb, SR_AACK_I_AM_COORD, 0);
0388     }
0389 
0390     return atusb_get_and_clear_error(atusb);
0391 }
0392 
0393 static int atusb_start(struct ieee802154_hw *hw)
0394 {
0395     struct atusb *atusb = hw->priv;
0396     struct usb_device *usb_dev = atusb->usb_dev;
0397     int ret;
0398 
0399     dev_dbg(&usb_dev->dev, "%s\n", __func__);
0400     schedule_delayed_work(&atusb->work, 0);
0401     usb_control_msg_send(atusb->usb_dev, 0, ATUSB_RX_MODE, ATUSB_REQ_TO_DEV, 1, 0,
0402                  NULL, 0, 1000, GFP_KERNEL);
0403     ret = atusb_get_and_clear_error(atusb);
0404     if (ret < 0)
0405         usb_kill_anchored_urbs(&atusb->idle_urbs);
0406     return ret;
0407 }
0408 
0409 static void atusb_stop(struct ieee802154_hw *hw)
0410 {
0411     struct atusb *atusb = hw->priv;
0412     struct usb_device *usb_dev = atusb->usb_dev;
0413 
0414     dev_dbg(&usb_dev->dev, "%s\n", __func__);
0415     usb_kill_anchored_urbs(&atusb->idle_urbs);
0416     usb_control_msg_send(atusb->usb_dev, 0, ATUSB_RX_MODE, ATUSB_REQ_TO_DEV, 0, 0,
0417                  NULL, 0, 1000, GFP_KERNEL);
0418     atusb_get_and_clear_error(atusb);
0419 }
0420 
0421 #define ATUSB_MAX_TX_POWERS 0xF
0422 static const s32 atusb_powers[ATUSB_MAX_TX_POWERS + 1] = {
0423     300, 280, 230, 180, 130, 70, 0, -100, -200, -300, -400, -500, -700,
0424     -900, -1200, -1700,
0425 };
0426 
0427 static int
0428 atusb_txpower(struct ieee802154_hw *hw, s32 mbm)
0429 {
0430     struct atusb *atusb = hw->priv;
0431 
0432     if (atusb->data)
0433         return atusb->data->set_txpower(hw, mbm);
0434     else
0435         return -ENOTSUPP;
0436 }
0437 
0438 static int
0439 atusb_set_txpower(struct ieee802154_hw *hw, s32 mbm)
0440 {
0441     struct atusb *atusb = hw->priv;
0442     u32 i;
0443 
0444     for (i = 0; i < hw->phy->supported.tx_powers_size; i++) {
0445         if (hw->phy->supported.tx_powers[i] == mbm)
0446             return atusb_write_subreg(atusb, SR_TX_PWR_23X, i);
0447     }
0448 
0449     return -EINVAL;
0450 }
0451 
0452 static int
0453 hulusb_set_txpower(struct ieee802154_hw *hw, s32 mbm)
0454 {
0455     u32 i;
0456 
0457     for (i = 0; i < hw->phy->supported.tx_powers_size; i++) {
0458         if (hw->phy->supported.tx_powers[i] == mbm)
0459             return atusb_write_subreg(hw->priv, SR_TX_PWR_212, i);
0460     }
0461 
0462     return -EINVAL;
0463 }
0464 
0465 #define ATUSB_MAX_ED_LEVELS 0xF
0466 static const s32 atusb_ed_levels[ATUSB_MAX_ED_LEVELS + 1] = {
0467     -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300,
0468     -7100, -6900, -6700, -6500, -6300, -6100,
0469 };
0470 
0471 #define AT86RF212_MAX_TX_POWERS 0x1F
0472 static const s32 at86rf212_powers[AT86RF212_MAX_TX_POWERS + 1] = {
0473     500, 400, 300, 200, 100, 0, -100, -200, -300, -400, -500, -600, -700,
0474     -800, -900, -1000, -1100, -1200, -1300, -1400, -1500, -1600, -1700,
0475     -1800, -1900, -2000, -2100, -2200, -2300, -2400, -2500, -2600,
0476 };
0477 
0478 #define AT86RF2XX_MAX_ED_LEVELS 0xF
0479 static const s32 at86rf212_ed_levels_100[AT86RF2XX_MAX_ED_LEVELS + 1] = {
0480     -10000, -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200,
0481     -8000, -7800, -7600, -7400, -7200, -7000,
0482 };
0483 
0484 static const s32 at86rf212_ed_levels_98[AT86RF2XX_MAX_ED_LEVELS + 1] = {
0485     -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200, -8000,
0486     -7800, -7600, -7400, -7200, -7000, -6800,
0487 };
0488 
0489 static int
0490 atusb_set_cca_mode(struct ieee802154_hw *hw, const struct wpan_phy_cca *cca)
0491 {
0492     struct atusb *atusb = hw->priv;
0493     u8 val;
0494 
0495     /* mapping 802.15.4 to driver spec */
0496     switch (cca->mode) {
0497     case NL802154_CCA_ENERGY:
0498         val = 1;
0499         break;
0500     case NL802154_CCA_CARRIER:
0501         val = 2;
0502         break;
0503     case NL802154_CCA_ENERGY_CARRIER:
0504         switch (cca->opt) {
0505         case NL802154_CCA_OPT_ENERGY_CARRIER_AND:
0506             val = 3;
0507             break;
0508         case NL802154_CCA_OPT_ENERGY_CARRIER_OR:
0509             val = 0;
0510             break;
0511         default:
0512             return -EINVAL;
0513         }
0514         break;
0515     default:
0516         return -EINVAL;
0517     }
0518 
0519     return atusb_write_subreg(atusb, SR_CCA_MODE, val);
0520 }
0521 
0522 static int hulusb_set_cca_ed_level(struct atusb *lp, int rssi_base_val)
0523 {
0524     int cca_ed_thres;
0525 
0526     cca_ed_thres = atusb_read_subreg(lp, SR_CCA_ED_THRES);
0527     if (cca_ed_thres < 0)
0528         return cca_ed_thres;
0529 
0530     switch (rssi_base_val) {
0531     case -98:
0532         lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_98;
0533         lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_98);
0534         lp->hw->phy->cca_ed_level = at86rf212_ed_levels_98[cca_ed_thres];
0535         break;
0536     case -100:
0537         lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
0538         lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
0539         lp->hw->phy->cca_ed_level = at86rf212_ed_levels_100[cca_ed_thres];
0540         break;
0541     default:
0542         WARN_ON(1);
0543     }
0544 
0545     return 0;
0546 }
0547 
0548 static int
0549 atusb_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm)
0550 {
0551     struct atusb *atusb = hw->priv;
0552     u32 i;
0553 
0554     for (i = 0; i < hw->phy->supported.cca_ed_levels_size; i++) {
0555         if (hw->phy->supported.cca_ed_levels[i] == mbm)
0556             return atusb_write_subreg(atusb, SR_CCA_ED_THRES, i);
0557     }
0558 
0559     return -EINVAL;
0560 }
0561 
0562 static int atusb_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
0563 {
0564     struct atusb *atusb = hw->priv;
0565     int ret = -ENOTSUPP;
0566 
0567     if (atusb->data) {
0568         ret = atusb->data->set_channel(hw, page, channel);
0569         /* @@@ ugly synchronization */
0570         msleep(atusb->data->t_channel_switch);
0571     }
0572 
0573     return ret;
0574 }
0575 
0576 static int atusb_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
0577 {
0578     struct atusb *atusb = hw->priv;
0579     int ret;
0580 
0581     ret = atusb_write_subreg(atusb, SR_CHANNEL, channel);
0582     if (ret < 0)
0583         return ret;
0584     return 0;
0585 }
0586 
0587 static int hulusb_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel)
0588 {
0589     int rc;
0590     int rssi_base_val;
0591 
0592     struct atusb *lp = hw->priv;
0593 
0594     if (channel == 0)
0595         rc = atusb_write_subreg(lp, SR_SUB_MODE, 0);
0596     else
0597         rc = atusb_write_subreg(lp, SR_SUB_MODE, 1);
0598     if (rc < 0)
0599         return rc;
0600 
0601     if (page == 0) {
0602         rc = atusb_write_subreg(lp, SR_BPSK_QPSK, 0);
0603         rssi_base_val = -100;
0604     } else {
0605         rc = atusb_write_subreg(lp, SR_BPSK_QPSK, 1);
0606         rssi_base_val = -98;
0607     }
0608     if (rc < 0)
0609         return rc;
0610 
0611     rc = hulusb_set_cca_ed_level(lp, rssi_base_val);
0612     if (rc < 0)
0613         return rc;
0614 
0615     return atusb_write_subreg(lp, SR_CHANNEL, channel);
0616 }
0617 
0618 static int
0619 atusb_set_csma_params(struct ieee802154_hw *hw, u8 min_be, u8 max_be, u8 retries)
0620 {
0621     struct atusb *atusb = hw->priv;
0622     int ret;
0623 
0624     ret = atusb_write_subreg(atusb, SR_MIN_BE, min_be);
0625     if (ret)
0626         return ret;
0627 
0628     ret = atusb_write_subreg(atusb, SR_MAX_BE, max_be);
0629     if (ret)
0630         return ret;
0631 
0632     return atusb_write_subreg(atusb, SR_MAX_CSMA_RETRIES, retries);
0633 }
0634 
0635 static int
0636 hulusb_set_lbt(struct ieee802154_hw *hw, bool on)
0637 {
0638     struct atusb *atusb = hw->priv;
0639 
0640     return atusb_write_subreg(atusb, SR_CSMA_LBT_MODE, on);
0641 }
0642 
0643 static int
0644 atusb_set_frame_retries(struct ieee802154_hw *hw, s8 retries)
0645 {
0646     struct atusb *atusb = hw->priv;
0647 
0648     return atusb_write_subreg(atusb, SR_MAX_FRAME_RETRIES, retries);
0649 }
0650 
0651 static int
0652 atusb_set_promiscuous_mode(struct ieee802154_hw *hw, const bool on)
0653 {
0654     struct atusb *atusb = hw->priv;
0655     int ret;
0656 
0657     if (on) {
0658         ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 1);
0659         if (ret < 0)
0660             return ret;
0661 
0662         ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 1);
0663         if (ret < 0)
0664             return ret;
0665     } else {
0666         ret = atusb_write_subreg(atusb, SR_AACK_PROM_MODE, 0);
0667         if (ret < 0)
0668             return ret;
0669 
0670         ret = atusb_write_subreg(atusb, SR_AACK_DIS_ACK, 0);
0671         if (ret < 0)
0672             return ret;
0673     }
0674 
0675     return 0;
0676 }
0677 
0678 static struct atusb_chip_data atusb_chip_data = {
0679     .t_channel_switch = 1,
0680     .rssi_base_val = -91,
0681     .set_txpower = atusb_set_txpower,
0682     .set_channel = atusb_set_channel,
0683 };
0684 
0685 static struct atusb_chip_data hulusb_chip_data = {
0686     .t_channel_switch = 11,
0687     .rssi_base_val = -100,
0688     .set_txpower = hulusb_set_txpower,
0689     .set_channel = hulusb_set_channel,
0690 };
0691 
0692 static const struct ieee802154_ops atusb_ops = {
0693     .owner          = THIS_MODULE,
0694     .xmit_async     = atusb_xmit,
0695     .ed         = atusb_ed,
0696     .set_channel        = atusb_channel,
0697     .start          = atusb_start,
0698     .stop           = atusb_stop,
0699     .set_hw_addr_filt   = atusb_set_hw_addr_filt,
0700     .set_txpower        = atusb_txpower,
0701     .set_lbt        = hulusb_set_lbt,
0702     .set_cca_mode       = atusb_set_cca_mode,
0703     .set_cca_ed_level   = atusb_set_cca_ed_level,
0704     .set_csma_params    = atusb_set_csma_params,
0705     .set_frame_retries  = atusb_set_frame_retries,
0706     .set_promiscuous_mode   = atusb_set_promiscuous_mode,
0707 };
0708 
0709 /* ----- Firmware and chip version information ----------------------------- */
0710 
0711 static int atusb_get_and_show_revision(struct atusb *atusb)
0712 {
0713     struct usb_device *usb_dev = atusb->usb_dev;
0714     char *hw_name;
0715     unsigned char buffer[3];
0716     int ret;
0717 
0718     /* Get a couple of the ATMega Firmware values */
0719     ret = usb_control_msg_recv(atusb->usb_dev, 0, ATUSB_ID, ATUSB_REQ_FROM_DEV, 0, 0,
0720                    buffer, 3, 1000, GFP_KERNEL);
0721     if (!ret) {
0722         atusb->fw_ver_maj = buffer[0];
0723         atusb->fw_ver_min = buffer[1];
0724         atusb->fw_hw_type = buffer[2];
0725 
0726         switch (atusb->fw_hw_type) {
0727         case ATUSB_HW_TYPE_100813:
0728         case ATUSB_HW_TYPE_101216:
0729         case ATUSB_HW_TYPE_110131:
0730             hw_name = "ATUSB";
0731             atusb->data = &atusb_chip_data;
0732             break;
0733         case ATUSB_HW_TYPE_RZUSB:
0734             hw_name = "RZUSB";
0735             atusb->data = &atusb_chip_data;
0736             break;
0737         case ATUSB_HW_TYPE_HULUSB:
0738             hw_name = "HULUSB";
0739             atusb->data = &hulusb_chip_data;
0740             break;
0741         default:
0742             hw_name = "UNKNOWN";
0743             atusb->err = -ENOTSUPP;
0744             ret = -ENOTSUPP;
0745             break;
0746         }
0747 
0748         dev_info(&usb_dev->dev,
0749              "Firmware: major: %u, minor: %u, hardware type: %s (%d)\n",
0750              atusb->fw_ver_maj, atusb->fw_ver_min, hw_name,
0751              atusb->fw_hw_type);
0752     }
0753     if (atusb->fw_ver_maj == 0 && atusb->fw_ver_min < 2) {
0754         dev_info(&usb_dev->dev,
0755              "Firmware version (%u.%u) predates our first public release.",
0756              atusb->fw_ver_maj, atusb->fw_ver_min);
0757         dev_info(&usb_dev->dev, "Please update to version 0.2 or newer");
0758     }
0759 
0760     return ret;
0761 }
0762 
0763 static int atusb_get_and_show_build(struct atusb *atusb)
0764 {
0765     struct usb_device *usb_dev = atusb->usb_dev;
0766     char *build;
0767     int ret;
0768 
0769     build = kmalloc(ATUSB_BUILD_SIZE + 1, GFP_KERNEL);
0770     if (!build)
0771         return -ENOMEM;
0772 
0773     ret = usb_control_msg(atusb->usb_dev, usb_rcvctrlpipe(usb_dev, 0), ATUSB_BUILD,
0774                   ATUSB_REQ_FROM_DEV, 0, 0, build, ATUSB_BUILD_SIZE, 1000);
0775     if (ret >= 0) {
0776         build[ret] = 0;
0777         dev_info(&usb_dev->dev, "Firmware: build %s\n", build);
0778     }
0779 
0780     kfree(build);
0781     return ret;
0782 }
0783 
0784 static int atusb_get_and_conf_chip(struct atusb *atusb)
0785 {
0786     struct usb_device *usb_dev = atusb->usb_dev;
0787     u8 man_id_0, man_id_1, part_num, version_num;
0788     const char *chip;
0789     struct ieee802154_hw *hw = atusb->hw;
0790     int ret;
0791 
0792     ret = usb_control_msg_recv(usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
0793                    0, RG_MAN_ID_0, &man_id_0, 1, 1000, GFP_KERNEL);
0794     if (ret < 0)
0795         return ret;
0796 
0797     ret = usb_control_msg_recv(usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
0798                    0, RG_MAN_ID_1, &man_id_1, 1, 1000, GFP_KERNEL);
0799     if (ret < 0)
0800         return ret;
0801 
0802     ret = usb_control_msg_recv(usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
0803                    0, RG_PART_NUM, &part_num, 1, 1000, GFP_KERNEL);
0804     if (ret < 0)
0805         return ret;
0806 
0807     ret = usb_control_msg_recv(usb_dev, 0, ATUSB_REG_READ, ATUSB_REQ_FROM_DEV,
0808                    0, RG_VERSION_NUM, &version_num, 1, 1000, GFP_KERNEL);
0809     if (ret < 0)
0810         return ret;
0811 
0812     hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AFILT |
0813             IEEE802154_HW_PROMISCUOUS | IEEE802154_HW_CSMA_PARAMS;
0814 
0815     hw->phy->flags = WPAN_PHY_FLAG_TXPOWER | WPAN_PHY_FLAG_CCA_ED_LEVEL |
0816              WPAN_PHY_FLAG_CCA_MODE;
0817 
0818     hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) |
0819                        BIT(NL802154_CCA_CARRIER) |
0820                        BIT(NL802154_CCA_ENERGY_CARRIER);
0821     hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) |
0822                       BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR);
0823 
0824     hw->phy->cca.mode = NL802154_CCA_ENERGY;
0825 
0826     hw->phy->current_page = 0;
0827 
0828     if ((man_id_1 << 8 | man_id_0) != ATUSB_JEDEC_ATMEL) {
0829         dev_err(&usb_dev->dev,
0830             "non-Atmel transceiver xxxx%02x%02x\n",
0831             man_id_1, man_id_0);
0832         goto fail;
0833     }
0834 
0835     switch (part_num) {
0836     case 2:
0837         chip = "AT86RF230";
0838         atusb->hw->phy->supported.channels[0] = 0x7FFF800;
0839         atusb->hw->phy->current_channel = 11;   /* reset default */
0840         atusb->hw->phy->supported.tx_powers = atusb_powers;
0841         atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers);
0842         hw->phy->supported.cca_ed_levels = atusb_ed_levels;
0843         hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels);
0844         break;
0845     case 3:
0846         chip = "AT86RF231";
0847         atusb->hw->phy->supported.channels[0] = 0x7FFF800;
0848         atusb->hw->phy->current_channel = 11;   /* reset default */
0849         atusb->hw->phy->supported.tx_powers = atusb_powers;
0850         atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers);
0851         hw->phy->supported.cca_ed_levels = atusb_ed_levels;
0852         hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels);
0853         break;
0854     case 7:
0855         chip = "AT86RF212";
0856         atusb->hw->flags |= IEEE802154_HW_LBT;
0857         atusb->hw->phy->supported.channels[0] = 0x00007FF;
0858         atusb->hw->phy->supported.channels[2] = 0x00007FF;
0859         atusb->hw->phy->current_channel = 5;
0860         atusb->hw->phy->supported.lbt = NL802154_SUPPORTED_BOOL_BOTH;
0861         atusb->hw->phy->supported.tx_powers = at86rf212_powers;
0862         atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf212_powers);
0863         atusb->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100;
0864         atusb->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100);
0865         break;
0866     default:
0867         dev_err(&usb_dev->dev,
0868             "unexpected transceiver, part 0x%02x version 0x%02x\n",
0869             part_num, version_num);
0870         goto fail;
0871     }
0872 
0873     hw->phy->transmit_power = hw->phy->supported.tx_powers[0];
0874     hw->phy->cca_ed_level = hw->phy->supported.cca_ed_levels[7];
0875 
0876     dev_info(&usb_dev->dev, "ATUSB: %s version %d\n", chip, version_num);
0877 
0878     return 0;
0879 
0880 fail:
0881     atusb->err = -ENODEV;
0882     return -ENODEV;
0883 }
0884 
0885 static int atusb_set_extended_addr(struct atusb *atusb)
0886 {
0887     struct usb_device *usb_dev = atusb->usb_dev;
0888     unsigned char buffer[IEEE802154_EXTENDED_ADDR_LEN];
0889     __le64 extended_addr;
0890     u64 addr;
0891     int ret;
0892 
0893     /* Firmware versions before 0.3 do not support the EUI64_READ command.
0894      * Just use a random address and be done.
0895      */
0896     if (atusb->fw_ver_maj == 0 && atusb->fw_ver_min < 3) {
0897         ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
0898         return 0;
0899     }
0900 
0901     /* Firmware is new enough so we fetch the address from EEPROM */
0902     ret = usb_control_msg_recv(atusb->usb_dev, 0, ATUSB_EUI64_READ, ATUSB_REQ_FROM_DEV, 0, 0,
0903                    buffer, IEEE802154_EXTENDED_ADDR_LEN, 1000, GFP_KERNEL);
0904     if (ret < 0) {
0905         dev_err(&usb_dev->dev, "failed to fetch extended address, random address set\n");
0906         ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
0907         return ret;
0908     }
0909 
0910     memcpy(&extended_addr, buffer, IEEE802154_EXTENDED_ADDR_LEN);
0911     /* Check if read address is not empty and the unicast bit is set correctly */
0912     if (!ieee802154_is_valid_extended_unicast_addr(extended_addr)) {
0913         dev_info(&usb_dev->dev, "no permanent extended address found, random address set\n");
0914         ieee802154_random_extended_addr(&atusb->hw->phy->perm_extended_addr);
0915     } else {
0916         atusb->hw->phy->perm_extended_addr = extended_addr;
0917         addr = swab64((__force u64)atusb->hw->phy->perm_extended_addr);
0918         dev_info(&usb_dev->dev, "Read permanent extended address %8phC from device\n",
0919              &addr);
0920     }
0921 
0922     return ret;
0923 }
0924 
0925 /* ----- Setup ------------------------------------------------------------- */
0926 
0927 static int atusb_probe(struct usb_interface *interface,
0928                const struct usb_device_id *id)
0929 {
0930     struct usb_device *usb_dev = interface_to_usbdev(interface);
0931     struct ieee802154_hw *hw;
0932     struct atusb *atusb = NULL;
0933     int ret = -ENOMEM;
0934 
0935     hw = ieee802154_alloc_hw(sizeof(struct atusb), &atusb_ops);
0936     if (!hw)
0937         return -ENOMEM;
0938 
0939     atusb = hw->priv;
0940     atusb->hw = hw;
0941     atusb->usb_dev = usb_get_dev(usb_dev);
0942     usb_set_intfdata(interface, atusb);
0943 
0944     atusb->shutdown = 0;
0945     atusb->err = 0;
0946     INIT_DELAYED_WORK(&atusb->work, atusb_work_urbs);
0947     init_usb_anchor(&atusb->idle_urbs);
0948     init_usb_anchor(&atusb->rx_urbs);
0949 
0950     if (atusb_alloc_urbs(atusb, ATUSB_NUM_RX_URBS))
0951         goto fail;
0952 
0953     atusb->tx_dr.bRequestType = ATUSB_REQ_TO_DEV;
0954     atusb->tx_dr.bRequest = ATUSB_TX;
0955     atusb->tx_dr.wValue = cpu_to_le16(0);
0956 
0957     atusb->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
0958     if (!atusb->tx_urb)
0959         goto fail;
0960 
0961     hw->parent = &usb_dev->dev;
0962 
0963     usb_control_msg_send(atusb->usb_dev, 0, ATUSB_RF_RESET, ATUSB_REQ_TO_DEV, 0, 0,
0964                  NULL, 0, 1000, GFP_KERNEL);
0965     atusb_get_and_conf_chip(atusb);
0966     atusb_get_and_show_revision(atusb);
0967     atusb_get_and_show_build(atusb);
0968     atusb_set_extended_addr(atusb);
0969 
0970     if ((atusb->fw_ver_maj == 0 && atusb->fw_ver_min >= 3) || atusb->fw_ver_maj > 0)
0971         hw->flags |= IEEE802154_HW_FRAME_RETRIES;
0972 
0973     ret = atusb_get_and_clear_error(atusb);
0974     if (ret) {
0975         dev_err(&atusb->usb_dev->dev,
0976             "%s: initialization failed, error = %d\n",
0977             __func__, ret);
0978         goto fail;
0979     }
0980 
0981     ret = ieee802154_register_hw(hw);
0982     if (ret)
0983         goto fail;
0984 
0985     /* If we just powered on, we're now in P_ON and need to enter TRX_OFF
0986      * explicitly. Any resets after that will send us straight to TRX_OFF,
0987      * making the command below redundant.
0988      */
0989     usb_control_msg_send(atusb->usb_dev, 0, ATUSB_REG_WRITE, ATUSB_REQ_TO_DEV,
0990                  STATE_FORCE_TRX_OFF, RG_TRX_STATE, NULL, 0, 1000, GFP_KERNEL);
0991 
0992     msleep(1);  /* reset => TRX_OFF, tTR13 = 37 us */
0993 
0994 #if 0
0995     /* Calculating the maximum time available to empty the frame buffer
0996      * on reception:
0997      *
0998      * According to [1], the inter-frame gap is
0999      * 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.
1003      *
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.
1007      *
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.
1011      *
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
1015      */
1016 
1017     atusb_write_subreg(atusb, SR_RX_SAFE_MODE, 1);
1018 #endif
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);
1021 
1022     ret = atusb_get_and_clear_error(atusb);
1023     if (!ret)
1024         return 0;
1025 
1026     dev_err(&atusb->usb_dev->dev,
1027         "%s: setup failed, error = %d\n",
1028         __func__, ret);
1029 
1030     ieee802154_unregister_hw(hw);
1031 fail:
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);
1037     return ret;
1038 }
1039 
1040 static void atusb_disconnect(struct usb_interface *interface)
1041 {
1042     struct atusb *atusb = usb_get_intfdata(interface);
1043 
1044     dev_dbg(&atusb->usb_dev->dev, "%s\n", __func__);
1045 
1046     atusb->shutdown = 1;
1047     cancel_delayed_work_sync(&atusb->work);
1048 
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);
1053 
1054     ieee802154_unregister_hw(atusb->hw);
1055 
1056     usb_put_dev(atusb->usb_dev);
1057 
1058     ieee802154_free_hw(atusb->hw);
1059 
1060     usb_set_intfdata(interface, NULL);
1061 
1062     pr_debug("%s done\n", __func__);
1063 }
1064 
1065 /* The devices we work with */
1066 static const struct usb_device_id atusb_device_table[] = {
1067     {
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
1073     },
1074     /* end with null element */
1075     {}
1076 };
1077 MODULE_DEVICE_TABLE(usb, atusb_device_table);
1078 
1079 static struct usb_driver atusb_driver = {
1080     .name       = "atusb",
1081     .probe      = atusb_probe,
1082     .disconnect = atusb_disconnect,
1083     .id_table   = atusb_device_table,
1084 };
1085 module_usb_driver(atusb_driver);
1086 
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");