From 8fad346f366a72978ea942abd06bd501ebd39c22 Mon Sep 17 00:00:00 2001 From: Phoebe Buckheister Date: Mon, 17 Feb 2014 11:34:06 +0100 Subject: [PATCH 01/10] ieee802154: add basic support for RF212 to at86rf230 driver Since the AT86RF2xy chips are mostly compatible, this is only a small change to the actual driver code. The at86rf230 driver already supports the RF212 in most places, only three small adjustments are required: * force the initial state after P_ON to FORCE_TRX_OFF to work around a documented erratum * channels_supported depends on the frequency of the transceiver, and thus is_rf212 * do early detection of chip version select an appropriate _ops struct based on the chip version. Signed-off-by: Phoebe Buckheister Signed-off-by: David S. Miller --- drivers/net/ieee802154/at86rf230.c | 298 +++++++++++++++++++---------- 1 file changed, 194 insertions(+), 104 deletions(-) diff --git a/drivers/net/ieee802154/at86rf230.c b/drivers/net/ieee802154/at86rf230.c index ab31544bc254..6dd97829ff08 100644 --- a/drivers/net/ieee802154/at86rf230.c +++ b/drivers/net/ieee802154/at86rf230.c @@ -37,7 +37,6 @@ struct at86rf230_local { struct spi_device *spi; - int rstn, slp_tr, dig2; u8 part; u8 vers; @@ -55,6 +54,11 @@ struct at86rf230_local { bool is_tx; }; +static inline int is_rf212(struct at86rf230_local *local) +{ + return local->part == 7; +} + #define RG_TRX_STATUS (0x01) #define SR_TRX_STATUS 0x01, 0x1f, 0 #define SR_RESERVED_01_3 0x01, 0x20, 5 @@ -100,7 +104,9 @@ struct at86rf230_local { #define SR_SFD_VALUE 0x0b, 0xff, 0 #define RG_TRX_CTRL_2 (0x0c) #define SR_OQPSK_DATA_RATE 0x0c, 0x03, 0 -#define SR_RESERVED_0c_2 0x0c, 0x7c, 2 +#define SR_SUB_MODE 0x0c, 0x04, 2 +#define SR_BPSK_QPSK 0x0c, 0x08, 3 +#define SR_RESERVED_0c_4 0x0c, 0x70, 4 #define SR_RX_SAFE_MODE 0x0c, 0x80, 7 #define RG_ANT_DIV (0x0d) #define SR_ANT_CTRL 0x0d, 0x03, 0 @@ -243,6 +249,57 @@ struct at86rf230_local { #define STATE_BUSY_RX_AACK_NOCLK 0x1E #define STATE_TRANSITION_IN_PROGRESS 0x1F +static int +__at86rf230_detect_device(struct spi_device *spi, u16 *man_id, u8 *part, + u8 *version) +{ + u8 data[4]; + u8 *buf = kmalloc(2, GFP_KERNEL); + int status; + struct spi_message msg; + struct spi_transfer xfer = { + .len = 2, + .tx_buf = buf, + .rx_buf = buf, + }; + u8 reg; + + if (!buf) + return -ENOMEM; + + for (reg = RG_PART_NUM; reg <= RG_MAN_ID_1; reg++) { + buf[0] = (reg & CMD_REG_MASK) | CMD_REG; + buf[1] = 0xff; + dev_vdbg(&spi->dev, "buf[0] = %02x\n", buf[0]); + spi_message_init(&msg); + spi_message_add_tail(&xfer, &msg); + + status = spi_sync(spi, &msg); + dev_vdbg(&spi->dev, "status = %d\n", status); + if (msg.status) + status = msg.status; + + dev_vdbg(&spi->dev, "status = %d\n", status); + dev_vdbg(&spi->dev, "buf[0] = %02x\n", buf[0]); + dev_vdbg(&spi->dev, "buf[1] = %02x\n", buf[1]); + + if (status == 0) + data[reg - RG_PART_NUM] = buf[1]; + else + break; + } + + if (status == 0) { + *part = data[0]; + *version = data[1]; + *man_id = (data[3] << 8) | data[2]; + } + + kfree(buf); + + return status; +} + static int __at86rf230_write(struct at86rf230_local *lp, u8 addr, u8 data) { @@ -519,6 +576,27 @@ at86rf230_stop(struct ieee802154_dev *dev) at86rf230_state(dev, STATE_FORCE_TRX_OFF); } +static int +at86rf230_set_channel(struct at86rf230_local *lp, int page, int channel) +{ + return at86rf230_write_subreg(lp, SR_CHANNEL, channel); +} + +static int +at86rf212_set_channel(struct at86rf230_local *lp, int page, int channel) +{ + int rc; + + if (channel == 0) + rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 0); + else + rc = at86rf230_write_subreg(lp, SR_SUB_MODE, 1); + if (rc < 0) + return rc; + + return at86rf230_write_subreg(lp, SR_CHANNEL, channel); +} + static int at86rf230_channel(struct ieee802154_dev *dev, int page, int channel) { @@ -527,12 +605,19 @@ at86rf230_channel(struct ieee802154_dev *dev, int page, int channel) might_sleep(); - if (page != 0 || channel < 11 || channel > 26) { + if (page < 0 || page > 31 || + !(lp->dev->phy->channels_supported[page] & BIT(channel))) { WARN_ON(1); return -EINVAL; } - rc = at86rf230_write_subreg(lp, SR_CHANNEL, channel); + if (is_rf212(lp)) + rc = at86rf212_set_channel(lp, page, channel); + else + rc = at86rf230_set_channel(lp, page, channel); + if (rc < 0) + return rc; + msleep(1); /* Wait for PLL */ dev->phy->current_channel = channel; @@ -678,6 +763,16 @@ static struct ieee802154_ops at86rf230_ops = { .set_hw_addr_filt = at86rf230_set_hw_addr_filt, }; +static struct ieee802154_ops at86rf212_ops = { + .owner = THIS_MODULE, + .xmit = at86rf230_xmit, + .ed = at86rf230_ed, + .set_channel = at86rf230_channel, + .start = at86rf230_start, + .stop = at86rf230_stop, + .set_hw_addr_filt = at86rf230_set_hw_addr_filt, +}; + static void at86rf230_irqwork(struct work_struct *work) { struct at86rf230_local *lp = @@ -759,7 +854,8 @@ static int at86rf230_hw_init(struct at86rf230_local *lp) dev_info(&lp->spi->dev, "Status: %02x\n", status); if (status == STATE_P_ON) { - rc = at86rf230_write_subreg(lp, SR_TRX_CMD, STATE_TRX_OFF); + rc = at86rf230_write_subreg(lp, SR_TRX_CMD, + STATE_FORCE_TRX_OFF); if (rc) return rc; msleep(1); @@ -824,26 +920,18 @@ static int at86rf230_hw_init(struct at86rf230_local *lp) return 0; } -static void at86rf230_fill_data(struct spi_device *spi) -{ - struct at86rf230_local *lp = spi_get_drvdata(spi); - struct at86rf230_platform_data *pdata = spi->dev.platform_data; - - lp->rstn = pdata->rstn; - lp->slp_tr = pdata->slp_tr; - lp->dig2 = pdata->dig2; -} - static int at86rf230_probe(struct spi_device *spi) { struct at86rf230_platform_data *pdata; struct ieee802154_dev *dev; struct at86rf230_local *lp; - u8 man_id_0, man_id_1, status; + u16 man_id = 0; + u8 part = 0, version = 0, status; irq_handler_t irq_handler; work_func_t irq_worker; - int rc, supported = 0; + int rc; const char *chip; + struct ieee802154_ops *ops = NULL; if (!spi->irq) { dev_err(&spi->dev, "no IRQ specified\n"); @@ -856,20 +944,85 @@ static int at86rf230_probe(struct spi_device *spi) return -EINVAL; } - dev = ieee802154_alloc_device(sizeof(*lp), &at86rf230_ops); - if (!dev) - return -ENOMEM; + rc = gpio_request(pdata->rstn, "rstn"); + if (rc) + return rc; + + if (gpio_is_valid(pdata->slp_tr)) { + rc = gpio_request(pdata->slp_tr, "slp_tr"); + if (rc) + goto err_slp_tr; + } + + rc = gpio_direction_output(pdata->rstn, 1); + if (rc) + goto err_gpio_dir; + + if (gpio_is_valid(pdata->slp_tr)) { + rc = gpio_direction_output(pdata->slp_tr, 0); + if (rc) + goto err_gpio_dir; + } + + /* Reset */ + msleep(1); + gpio_set_value(pdata->rstn, 0); + msleep(1); + gpio_set_value(pdata->rstn, 1); + msleep(1); + + rc = __at86rf230_detect_device(spi, &man_id, &part, &version); + if (rc < 0) + goto err_gpio_dir; + + if (man_id != 0x001f) { + dev_err(&spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n", + man_id >> 8, man_id & 0xFF); + rc = -EINVAL; + goto err_gpio_dir; + } + + switch (part) { + case 2: + chip = "at86rf230"; + /* FIXME: should be easy to support; */ + break; + case 3: + chip = "at86rf231"; + ops = &at86rf230_ops; + break; + case 7: + chip = "at86rf212"; + if (version == 1) + ops = &at86rf212_ops; + break; + default: + chip = "UNKNOWN"; + break; + } + + dev_info(&spi->dev, "Detected %s chip version %d\n", chip, version); + if (!ops) { + rc = -ENOTSUPP; + goto err_gpio_dir; + } + + dev = ieee802154_alloc_device(sizeof(*lp), ops); + if (!dev) { + rc = -ENOMEM; + goto err_gpio_dir; + } lp = dev->priv; lp->dev = dev; + lp->part = part; + lp->vers = version; lp->spi = spi; dev->parent = &spi->dev; dev->extra_tx_headroom = 0; - /* We do support only 2.4 Ghz */ - dev->phy->channels_supported[0] = 0x7FFF800; - dev->flags = IEEE802154_HW_OMIT_CKSUM; + dev->flags = IEEE802154_HW_OMIT_CKSUM | IEEE802154_HW_AACK; if (pdata->irq_type & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) { irq_worker = at86rf230_irqwork; @@ -886,86 +1039,20 @@ static int at86rf230_probe(struct spi_device *spi) spi_set_drvdata(spi, lp); - at86rf230_fill_data(spi); - - rc = gpio_request(lp->rstn, "rstn"); - if (rc) - goto err_rstn; - - if (gpio_is_valid(lp->slp_tr)) { - rc = gpio_request(lp->slp_tr, "slp_tr"); - if (rc) - goto err_slp_tr; - } - - rc = gpio_direction_output(lp->rstn, 1); - if (rc) - goto err_gpio_dir; - - if (gpio_is_valid(lp->slp_tr)) { - rc = gpio_direction_output(lp->slp_tr, 0); - if (rc) - goto err_gpio_dir; - } - - /* Reset */ - msleep(1); - gpio_set_value(lp->rstn, 0); - msleep(1); - gpio_set_value(lp->rstn, 1); - msleep(1); - - rc = at86rf230_read_subreg(lp, SR_MAN_ID_0, &man_id_0); - if (rc) - goto err_gpio_dir; - rc = at86rf230_read_subreg(lp, SR_MAN_ID_1, &man_id_1); - if (rc) - goto err_gpio_dir; - - if (man_id_1 != 0x00 || man_id_0 != 0x1f) { - dev_err(&spi->dev, "Non-Atmel dev found (MAN_ID %02x %02x)\n", - man_id_1, man_id_0); - rc = -EINVAL; - goto err_gpio_dir; - } - - rc = at86rf230_read_subreg(lp, SR_PART_NUM, &lp->part); - if (rc) - goto err_gpio_dir; - - rc = at86rf230_read_subreg(lp, SR_VERSION_NUM, &lp->vers); - if (rc) - goto err_gpio_dir; - - switch (lp->part) { - case 2: - chip = "at86rf230"; - /* supported = 1; FIXME: should be easy to support; */ - break; - case 3: - chip = "at86rf231"; - supported = 1; - break; - default: - chip = "UNKNOWN"; - break; - } - - dev_info(&spi->dev, "Detected %s chip version %d\n", chip, lp->vers); - if (!supported) { - rc = -ENOTSUPP; - goto err_gpio_dir; - } + if (is_rf212(lp)) + dev->phy->channels_supported[0] = 0x00007FF; + else + dev->phy->channels_supported[0] = 0x7FFF800; rc = at86rf230_hw_init(lp); if (rc) - goto err_gpio_dir; + goto err_hw_init; rc = request_irq(spi->irq, irq_handler, IRQF_SHARED | pdata->irq_type, dev_name(&spi->dev), lp); if (rc) - goto err_gpio_dir; + goto err_hw_init; /* Read irq status register to reset irq line */ rc = at86rf230_read_subreg(lp, RG_IRQ_STATUS, 0xff, 0, &status); @@ -980,30 +1067,33 @@ static int at86rf230_probe(struct spi_device *spi) err_irq: free_irq(spi->irq, lp); +err_hw_init: flush_work(&lp->irqwork); -err_gpio_dir: - if (gpio_is_valid(lp->slp_tr)) - gpio_free(lp->slp_tr); -err_slp_tr: - gpio_free(lp->rstn); -err_rstn: + spi_set_drvdata(spi, NULL); mutex_destroy(&lp->bmux); ieee802154_free_device(lp->dev); + +err_gpio_dir: + if (gpio_is_valid(pdata->slp_tr)) + gpio_free(pdata->slp_tr); +err_slp_tr: + gpio_free(pdata->rstn); return rc; } static int at86rf230_remove(struct spi_device *spi) { struct at86rf230_local *lp = spi_get_drvdata(spi); + struct at86rf230_platform_data *pdata = spi->dev.platform_data; ieee802154_unregister_device(lp->dev); free_irq(spi->irq, lp); flush_work(&lp->irqwork); - if (gpio_is_valid(lp->slp_tr)) - gpio_free(lp->slp_tr); - gpio_free(lp->rstn); + if (gpio_is_valid(pdata->slp_tr)) + gpio_free(pdata->slp_tr); + gpio_free(pdata->rstn); mutex_destroy(&lp->bmux); ieee802154_free_device(lp->dev); From 5b520bbb538da7f7122b1e5dc85f2b4c592d4781 Mon Sep 17 00:00:00 2001 From: Phoebe Buckheister Date: Mon, 17 Feb 2014 11:34:07 +0100 Subject: [PATCH 02/10] ieee802154: default to AACK in at86rf230 The current IEEE802.15.4 stack assumes that a radio will never deliver packets with a bad CRC into the stack, as required by the standard. at86rf230 driven radios violates this assumption because of another incompatibility: devices are required to send ACKs if requested by a sender, but RF2xx will only send ACKs from a special receive mode that is currently not used by the driver. Enable this receive mode to fix both bugs. Frames with bad CRCs will not be received at all, and ACKs will be sent if so requested. Signed-off-by: Phoebe Buckheister Signed-off-by: David S. Miller --- drivers/net/ieee802154/at86rf230.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/net/ieee802154/at86rf230.c b/drivers/net/ieee802154/at86rf230.c index 6dd97829ff08..bd1ef0b3bee9 100644 --- a/drivers/net/ieee802154/at86rf230.c +++ b/drivers/net/ieee802154/at86rf230.c @@ -567,7 +567,7 @@ at86rf230_start(struct ieee802154_dev *dev) if (rc) return rc; - return at86rf230_state(dev, STATE_RX_ON); + return at86rf230_state(dev, STATE_RX_AACK_ON); } static void From 9b2777d6089bcd7fb035847f907280560fe233c8 Mon Sep 17 00:00:00 2001 From: Phoebe Buckheister Date: Mon, 17 Feb 2014 11:34:08 +0100 Subject: [PATCH 03/10] ieee802154: add TX power control to wpan_phy Replace the current u8 transmit_power in wpan_phy with s8 transmit_power. The u8 field contained the actual tx power and a tolerance field, which no physical radio every used. Adjust sysfs entries to keep compatibility with userspace, give tolerances of +-1dB statically there. This patch only adds support for this in the at86rf230 driver and the RF212 chip. Configuration calculation for RF212 is also somewhat basic, but does the job - the RF212 datasheet gives a large table with suggested values for combinations of TX power and page/channel, if this does not work well, we might have to copy the whole table. Signed-off-by: Phoebe Buckheister Signed-off-by: David S. Miller --- drivers/net/ieee802154/at86rf230.c | 25 +++++++++++++++ include/linux/nl802154.h | 4 +++ include/net/mac802154.h | 5 +++ include/net/wpan-phy.h | 6 ++-- net/ieee802154/ieee802154.h | 1 + net/ieee802154/netlink.c | 1 + net/ieee802154/nl-phy.c | 49 +++++++++++++++++++++++++++++- net/ieee802154/nl_policy.c | 2 ++ net/ieee802154/wpan-class.c | 4 +-- net/mac802154/ieee802154_dev.c | 11 +++++++ 10 files changed, 102 insertions(+), 6 deletions(-) diff --git a/drivers/net/ieee802154/at86rf230.c b/drivers/net/ieee802154/at86rf230.c index bd1ef0b3bee9..9afb4b9d7c93 100644 --- a/drivers/net/ieee802154/at86rf230.c +++ b/drivers/net/ieee802154/at86rf230.c @@ -753,6 +753,30 @@ at86rf230_set_hw_addr_filt(struct ieee802154_dev *dev, return 0; } +static int +at86rf212_set_txpower(struct ieee802154_dev *dev, int db) +{ + struct at86rf230_local *lp = dev->priv; + int rc; + + /* typical maximum output is 5dBm with RG_PHY_TX_PWR 0x60, lower five + * bits decrease power in 1dB steps. 0x60 represents extra PA gain of + * 0dB. + * thus, supported values for db range from -26 to 5, for 31dB of + * reduction to 0dB of reduction. + */ + if (db > 5 || db < -26) + return -EINVAL; + + db = -(db - 5); + + rc = __at86rf230_write(lp, RG_PHY_TX_PWR, 0x60 | db); + if (rc) + return rc; + + return 0; +} + static struct ieee802154_ops at86rf230_ops = { .owner = THIS_MODULE, .xmit = at86rf230_xmit, @@ -771,6 +795,7 @@ static struct ieee802154_ops at86rf212_ops = { .start = at86rf230_start, .stop = at86rf230_stop, .set_hw_addr_filt = at86rf230_set_hw_addr_filt, + .set_txpower = at86rf212_set_txpower, }; static void at86rf230_irqwork(struct work_struct *work) diff --git a/include/linux/nl802154.h b/include/linux/nl802154.h index fd4f2d1cdf6c..625d19e0a1de 100644 --- a/include/linux/nl802154.h +++ b/include/linux/nl802154.h @@ -70,6 +70,8 @@ enum { IEEE802154_ATTR_PHY_NAME, IEEE802154_ATTR_DEV_TYPE, + IEEE802154_ATTR_TXPOWER, + __IEEE802154_ATTR_MAX, }; @@ -122,6 +124,8 @@ enum { IEEE802154_ADD_IFACE, IEEE802154_DEL_IFACE, + IEEE802154_SET_PHYPARAMS, + __IEEE802154_CMD_MAX, }; diff --git a/include/net/mac802154.h b/include/net/mac802154.h index 807d6b7a943f..8bd2785a663c 100644 --- a/include/net/mac802154.h +++ b/include/net/mac802154.h @@ -113,6 +113,10 @@ struct ieee802154_dev { * Set radio for listening on specific address. * Set the device for listening on specified address. * Returns either zero, or negative errno. + * + * set_txpower: + * Set radio transmit power in dB. Called with pib_lock held. + * Returns either zero, or negative errno. */ struct ieee802154_ops { struct module *owner; @@ -129,6 +133,7 @@ struct ieee802154_ops { unsigned long changed); int (*ieee_addr)(struct ieee802154_dev *dev, u8 addr[IEEE802154_ADDR_LEN]); + int (*set_txpower)(struct ieee802154_dev *dev, int db); }; /* Basic interface to register ieee802154 device */ diff --git a/include/net/wpan-phy.h b/include/net/wpan-phy.h index b52bda8d13b1..47fc0c1bc3c7 100644 --- a/include/net/wpan-phy.h +++ b/include/net/wpan-phy.h @@ -37,14 +37,14 @@ struct wpan_phy { struct mutex pib_lock; /* - * This is a PIB according to 802.15.4-2006. + * This is a PIB according to 802.15.4-2011. * We do not provide timing-related variables, as they * aren't used outside of driver */ u8 current_channel; u8 current_page; u32 channels_supported[32]; - u8 transmit_power; + s8 transmit_power; u8 cca_mode; struct device dev; @@ -54,6 +54,8 @@ struct wpan_phy { const char *name, int type); void (*del_iface)(struct wpan_phy *phy, struct net_device *dev); + int (*set_txpower)(struct wpan_phy *phy, int db); + char priv[0] __attribute__((__aligned__(NETDEV_ALIGN))); }; diff --git a/net/ieee802154/ieee802154.h b/net/ieee802154/ieee802154.h index cee4425b9956..6cbc8965be91 100644 --- a/net/ieee802154/ieee802154.h +++ b/net/ieee802154/ieee802154.h @@ -53,6 +53,7 @@ int ieee802154_list_phy(struct sk_buff *skb, struct genl_info *info); int ieee802154_dump_phy(struct sk_buff *skb, struct netlink_callback *cb); int ieee802154_add_iface(struct sk_buff *skb, struct genl_info *info); int ieee802154_del_iface(struct sk_buff *skb, struct genl_info *info); +int ieee802154_set_phyparams(struct sk_buff *skb, struct genl_info *info); enum ieee802154_mcgrp_ids { IEEE802154_COORD_MCGRP, diff --git a/net/ieee802154/netlink.c b/net/ieee802154/netlink.c index 43f1b2bf469f..67c151bf4b91 100644 --- a/net/ieee802154/netlink.c +++ b/net/ieee802154/netlink.c @@ -115,6 +115,7 @@ static const struct genl_ops ieee8021154_ops[] = { ieee802154_dump_phy), IEEE802154_OP(IEEE802154_ADD_IFACE, ieee802154_add_iface), IEEE802154_OP(IEEE802154_DEL_IFACE, ieee802154_del_iface), + IEEE802154_OP(IEEE802154_SET_PHYPARAMS, ieee802154_set_phyparams), /* see nl-mac.c */ IEEE802154_OP(IEEE802154_ASSOCIATE_REQ, ieee802154_associate_req), IEEE802154_OP(IEEE802154_ASSOCIATE_RESP, ieee802154_associate_resp), diff --git a/net/ieee802154/nl-phy.c b/net/ieee802154/nl-phy.c index 89b265aea151..d3ee62fbae99 100644 --- a/net/ieee802154/nl-phy.c +++ b/net/ieee802154/nl-phy.c @@ -55,7 +55,8 @@ static int ieee802154_nl_fill_phy(struct sk_buff *msg, u32 portid, mutex_lock(&phy->pib_lock); if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) || nla_put_u8(msg, IEEE802154_ATTR_PAGE, phy->current_page) || - nla_put_u8(msg, IEEE802154_ATTR_CHANNEL, phy->current_channel)) + nla_put_u8(msg, IEEE802154_ATTR_CHANNEL, phy->current_channel) || + nla_put_s8(msg, IEEE802154_ATTR_TXPOWER, phy->transmit_power)) goto nla_put_failure; for (i = 0; i < 32; i++) { if (phy->channels_supported[i]) @@ -354,3 +355,49 @@ out_dev: return rc; } + +int ieee802154_set_phyparams(struct sk_buff *skb, struct genl_info *info) +{ + struct wpan_phy *phy; + const char *name; + int txpower; + int rc = -EINVAL; + + pr_debug("%s\n", __func__); + + if (!info->attrs[IEEE802154_ATTR_PHY_NAME]) + return -EINVAL; + + name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]); + if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0') + return -EINVAL; /* phy name should be null-terminated */ + + txpower = nla_get_s8(info->attrs[IEEE802154_ATTR_TXPOWER]); + + phy = wpan_phy_find(name); + if (!phy) + return -ENODEV; + + if (!phy->set_txpower) + goto out; + + mutex_lock(&phy->pib_lock); + + rc = phy->set_txpower(phy, txpower); + if (rc < 0) { + mutex_unlock(&phy->pib_lock); + goto out; + } + + phy->transmit_power = txpower; + + mutex_unlock(&phy->pib_lock); + + wpan_phy_put(phy); + + return 0; + +out: + wpan_phy_put(phy); + return rc; +} diff --git a/net/ieee802154/nl_policy.c b/net/ieee802154/nl_policy.c index 6adda4d46f95..90b1d0d2c14e 100644 --- a/net/ieee802154/nl_policy.c +++ b/net/ieee802154/nl_policy.c @@ -52,5 +52,7 @@ const struct nla_policy ieee802154_policy[IEEE802154_ATTR_MAX + 1] = { [IEEE802154_ATTR_DURATION] = { .type = NLA_U8, }, [IEEE802154_ATTR_ED_LIST] = { .len = 27 }, [IEEE802154_ATTR_CHANNEL_PAGE_LIST] = { .len = 32 * 4, }, + + [IEEE802154_ATTR_TXPOWER] = { .type = NLA_S8, }, }; diff --git a/net/ieee802154/wpan-class.c b/net/ieee802154/wpan-class.c index 4dd37615a749..8d6f6704da84 100644 --- a/net/ieee802154/wpan-class.c +++ b/net/ieee802154/wpan-class.c @@ -44,9 +44,7 @@ static DEVICE_ATTR_RO(name); MASTER_SHOW(current_channel, "%d"); MASTER_SHOW(current_page, "%d"); -MASTER_SHOW_COMPLEX(transmit_power, "%d +- %d dB", - ((signed char) (phy->transmit_power << 2)) >> 2, - (phy->transmit_power >> 6) ? (phy->transmit_power >> 6) * 3 : 1); +MASTER_SHOW(transmit_power, "%d +- 1 dB"); MASTER_SHOW(cca_mode, "%d"); static ssize_t channels_supported_show(struct device *dev, diff --git a/net/mac802154/ieee802154_dev.c b/net/mac802154/ieee802154_dev.c index 52ae6646a411..9eb49e0886a4 100644 --- a/net/mac802154/ieee802154_dev.c +++ b/net/mac802154/ieee802154_dev.c @@ -165,6 +165,16 @@ err: return ERR_PTR(err); } +static int mac802154_set_txpower(struct wpan_phy *phy, int db) +{ + struct mac802154_priv *priv = wpan_phy_priv(phy); + + if (!priv->ops->set_txpower) + return -ENOTSUPP; + + return priv->ops->set_txpower(&priv->hw, db); +} + struct ieee802154_dev * ieee802154_alloc_device(size_t priv_data_len, struct ieee802154_ops *ops) { @@ -242,6 +252,7 @@ int ieee802154_register_device(struct ieee802154_dev *dev) priv->phy->add_iface = mac802154_add_iface; priv->phy->del_iface = mac802154_del_iface; + priv->phy->set_txpower = mac802154_set_txpower; rc = wpan_phy_register(priv->phy); if (rc < 0) From 643e53c20a6ae78149f75861f260d0ca6d9a17e3 Mon Sep 17 00:00:00 2001 From: Phoebe Buckheister Date: Mon, 17 Feb 2014 11:34:09 +0100 Subject: [PATCH 04/10] ieee802154: support 100kbps QPSK/EU in at86rf230 The standard assigns channel 0 on page 2 to be 100kbps QPSK in the 868.3MHz band. Add support to the at86rf230 driver for this channel and page, at the moment predicated only for the RF212 chip. Per the datasheet, configurations for page 0, channels 0 to 10 and page 2, channels 0 to 10 differ only in the BPSK_QPSK bit. Support for channels 1 to 10 is untested. Signed-off-by: Phoebe Buckheister Signed-off-by: David S. Miller --- drivers/net/ieee802154/at86rf230.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/drivers/net/ieee802154/at86rf230.c b/drivers/net/ieee802154/at86rf230.c index 9afb4b9d7c93..04a995dad593 100644 --- a/drivers/net/ieee802154/at86rf230.c +++ b/drivers/net/ieee802154/at86rf230.c @@ -106,7 +106,8 @@ static inline int is_rf212(struct at86rf230_local *local) #define SR_OQPSK_DATA_RATE 0x0c, 0x03, 0 #define SR_SUB_MODE 0x0c, 0x04, 2 #define SR_BPSK_QPSK 0x0c, 0x08, 3 -#define SR_RESERVED_0c_4 0x0c, 0x70, 4 +#define SR_OQPSK_SUB1_RC_EN 0x0c, 0x10, 4 +#define SR_RESERVED_0c_5 0x0c, 0x60, 5 #define SR_RX_SAFE_MODE 0x0c, 0x80, 7 #define RG_ANT_DIV (0x0d) #define SR_ANT_CTRL 0x0d, 0x03, 0 @@ -594,6 +595,13 @@ at86rf212_set_channel(struct at86rf230_local *lp, int page, int channel) if (rc < 0) return rc; + if (page == 0) + rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0); + else + rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1); + if (rc < 0) + return rc; + return at86rf230_write_subreg(lp, SR_CHANNEL, channel); } @@ -620,6 +628,7 @@ at86rf230_channel(struct ieee802154_dev *dev, int page, int channel) msleep(1); /* Wait for PLL */ dev->phy->current_channel = channel; + dev->phy->current_page = page; return 0; } @@ -1064,10 +1073,12 @@ static int at86rf230_probe(struct spi_device *spi) spi_set_drvdata(spi, lp); - if (is_rf212(lp)) + if (is_rf212(lp)) { dev->phy->channels_supported[0] = 0x00007FF; - else + dev->phy->channels_supported[2] = 0x00007FF; + } else { dev->phy->channels_supported[0] = 0x7FFF800; + } rc = at86rf230_hw_init(lp); if (rc) From 84dda3c648fd55898064d76366b14f964cdc9d16 Mon Sep 17 00:00:00 2001 From: Phoebe Buckheister Date: Mon, 17 Feb 2014 11:34:10 +0100 Subject: [PATCH 05/10] ieee802154: add support for listen-before-talk in wpan_phy Listen-before-talk is an alternative to CSMA in uncoordinated networks and prescribed by european regulations if one wants to have a device with radio duty cycles above 10% (or less in some bands). Add a phy property to enable/disable LBT in the phy, including support in the at86rf230 driver for RF212 chips. Signed-off-by: Phoebe Buckheister Signed-off-by: David S. Miller --- drivers/net/ieee802154/at86rf230.c | 11 +++++- include/linux/nl802154.h | 1 + include/net/mac802154.h | 6 ++++ include/net/wpan-phy.h | 3 ++ net/ieee802154/nl-phy.c | 58 +++++++++++++++++++++++------- net/ieee802154/nl_policy.c | 1 + net/mac802154/ieee802154_dev.c | 11 ++++++ 7 files changed, 78 insertions(+), 13 deletions(-) diff --git a/drivers/net/ieee802154/at86rf230.c b/drivers/net/ieee802154/at86rf230.c index 04a995dad593..3d40c2350261 100644 --- a/drivers/net/ieee802154/at86rf230.c +++ b/drivers/net/ieee802154/at86rf230.c @@ -152,7 +152,7 @@ static inline int is_rf212(struct at86rf230_local *local) #define SR_RESERVED_17_5 0x17, 0x08, 3 #define SR_AACK_UPLD_RES_FT 0x17, 0x10, 4 #define SR_AACK_FLTR_RES_FT 0x17, 0x20, 5 -#define SR_RESERVED_17_2 0x17, 0x40, 6 +#define SR_CSMA_LBT_MODE 0x17, 0x40, 6 #define SR_RESERVED_17_1 0x17, 0x80, 7 #define RG_FTN_CTRL (0x18) #define SR_RESERVED_18_2 0x18, 0x7f, 0 @@ -786,6 +786,14 @@ at86rf212_set_txpower(struct ieee802154_dev *dev, int db) return 0; } +static int +at86rf212_set_lbt(struct ieee802154_dev *dev, bool on) +{ + struct at86rf230_local *lp = dev->priv; + + return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on); +} + static struct ieee802154_ops at86rf230_ops = { .owner = THIS_MODULE, .xmit = at86rf230_xmit, @@ -805,6 +813,7 @@ static struct ieee802154_ops at86rf212_ops = { .stop = at86rf230_stop, .set_hw_addr_filt = at86rf230_set_hw_addr_filt, .set_txpower = at86rf212_set_txpower, + .set_lbt = at86rf212_set_lbt, }; static void at86rf230_irqwork(struct work_struct *work) diff --git a/include/linux/nl802154.h b/include/linux/nl802154.h index 625d19e0a1de..326baee227f7 100644 --- a/include/linux/nl802154.h +++ b/include/linux/nl802154.h @@ -71,6 +71,7 @@ enum { IEEE802154_ATTR_DEV_TYPE, IEEE802154_ATTR_TXPOWER, + IEEE802154_ATTR_LBT_ENABLED, __IEEE802154_ATTR_MAX, }; diff --git a/include/net/mac802154.h b/include/net/mac802154.h index 8bd2785a663c..521edcb0e586 100644 --- a/include/net/mac802154.h +++ b/include/net/mac802154.h @@ -117,6 +117,11 @@ struct ieee802154_dev { * set_txpower: * Set radio transmit power in dB. Called with pib_lock held. * Returns either zero, or negative errno. + * + * set_lbt + * Enables or disables listen before talk on the device. Called with + * pib_lock held. + * Returns either zero, or negative errno. */ struct ieee802154_ops { struct module *owner; @@ -134,6 +139,7 @@ struct ieee802154_ops { int (*ieee_addr)(struct ieee802154_dev *dev, u8 addr[IEEE802154_ADDR_LEN]); int (*set_txpower)(struct ieee802154_dev *dev, int db); + int (*set_lbt)(struct ieee802154_dev *dev, bool on); }; /* Basic interface to register ieee802154 device */ diff --git a/include/net/wpan-phy.h b/include/net/wpan-phy.h index 47fc0c1bc3c7..804e6c4f5f8a 100644 --- a/include/net/wpan-phy.h +++ b/include/net/wpan-phy.h @@ -47,6 +47,8 @@ struct wpan_phy { s8 transmit_power; u8 cca_mode; + bool lbt; + struct device dev; int idx; @@ -55,6 +57,7 @@ struct wpan_phy { void (*del_iface)(struct wpan_phy *phy, struct net_device *dev); int (*set_txpower)(struct wpan_phy *phy, int db); + int (*set_lbt)(struct wpan_phy *phy, bool on); char priv[0] __attribute__((__aligned__(NETDEV_ALIGN))); }; diff --git a/net/ieee802154/nl-phy.c b/net/ieee802154/nl-phy.c index d3ee62fbae99..f029310b0662 100644 --- a/net/ieee802154/nl-phy.c +++ b/net/ieee802154/nl-phy.c @@ -56,7 +56,8 @@ static int ieee802154_nl_fill_phy(struct sk_buff *msg, u32 portid, if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) || nla_put_u8(msg, IEEE802154_ATTR_PAGE, phy->current_page) || nla_put_u8(msg, IEEE802154_ATTR_CHANNEL, phy->current_channel) || - nla_put_s8(msg, IEEE802154_ATTR_TXPOWER, phy->transmit_power)) + nla_put_s8(msg, IEEE802154_ATTR_TXPOWER, phy->transmit_power) || + nla_put_u8(msg, IEEE802154_ATTR_LBT_ENABLED, phy->lbt)) goto nla_put_failure; for (i = 0; i < 32; i++) { if (phy->channels_supported[i]) @@ -356,40 +357,71 @@ out_dev: return rc; } +static int phy_set_txpower(struct wpan_phy *phy, struct genl_info *info) +{ + int txpower = nla_get_s8(info->attrs[IEEE802154_ATTR_TXPOWER]); + int rc; + + rc = phy->set_txpower(phy, txpower); + if (rc < 0) + return rc; + + phy->transmit_power = txpower; + + return 0; +} + +static int phy_set_lbt(struct wpan_phy *phy, struct genl_info *info) +{ + u8 on = !!nla_get_u8(info->attrs[IEEE802154_ATTR_LBT_ENABLED]); + int rc; + + rc = phy->set_lbt(phy, on); + if (rc < 0) + return rc; + + phy->lbt = on; + + return 0; +} + int ieee802154_set_phyparams(struct sk_buff *skb, struct genl_info *info) { struct wpan_phy *phy; const char *name; - int txpower; - int rc = -EINVAL; + int rc = -ENOTSUPP; pr_debug("%s\n", __func__); - if (!info->attrs[IEEE802154_ATTR_PHY_NAME]) + if (!info->attrs[IEEE802154_ATTR_PHY_NAME] && + !info->attrs[IEEE802154_ATTR_LBT_ENABLED]) return -EINVAL; name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]); if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0') return -EINVAL; /* phy name should be null-terminated */ - txpower = nla_get_s8(info->attrs[IEEE802154_ATTR_TXPOWER]); - phy = wpan_phy_find(name); if (!phy) return -ENODEV; - if (!phy->set_txpower) + if ((!phy->set_txpower && info->attrs[IEEE802154_ATTR_TXPOWER]) || + (!phy->set_lbt && info->attrs[IEEE802154_ATTR_LBT_ENABLED])) goto out; mutex_lock(&phy->pib_lock); - rc = phy->set_txpower(phy, txpower); - if (rc < 0) { - mutex_unlock(&phy->pib_lock); - goto out; + if (info->attrs[IEEE802154_ATTR_TXPOWER]) { + rc = phy_set_txpower(phy, info); + if (rc < 0) + goto error; } - phy->transmit_power = txpower; + if (info->attrs[IEEE802154_ATTR_LBT_ENABLED]) { + rc = phy_set_lbt(phy, info); + if (rc < 0) + goto error; + } mutex_unlock(&phy->pib_lock); @@ -397,6 +429,8 @@ int ieee802154_set_phyparams(struct sk_buff *skb, struct genl_info *info) return 0; +error: + mutex_unlock(&phy->pib_lock); out: wpan_phy_put(phy); return rc; diff --git a/net/ieee802154/nl_policy.c b/net/ieee802154/nl_policy.c index 90b1d0d2c14e..a09f6423a6e9 100644 --- a/net/ieee802154/nl_policy.c +++ b/net/ieee802154/nl_policy.c @@ -54,5 +54,6 @@ const struct nla_policy ieee802154_policy[IEEE802154_ATTR_MAX + 1] = { [IEEE802154_ATTR_CHANNEL_PAGE_LIST] = { .len = 32 * 4, }, [IEEE802154_ATTR_TXPOWER] = { .type = NLA_S8, }, + [IEEE802154_ATTR_LBT_ENABLED] = { .type = NLA_U8, }, }; diff --git a/net/mac802154/ieee802154_dev.c b/net/mac802154/ieee802154_dev.c index 9eb49e0886a4..56338c8cfc33 100644 --- a/net/mac802154/ieee802154_dev.c +++ b/net/mac802154/ieee802154_dev.c @@ -175,6 +175,16 @@ static int mac802154_set_txpower(struct wpan_phy *phy, int db) return priv->ops->set_txpower(&priv->hw, db); } +static int mac802154_set_lbt(struct wpan_phy *phy, bool on) +{ + struct mac802154_priv *priv = wpan_phy_priv(phy); + + if (!priv->ops->set_lbt) + return -ENOTSUPP; + + return priv->ops->set_lbt(&priv->hw, on); +} + struct ieee802154_dev * ieee802154_alloc_device(size_t priv_data_len, struct ieee802154_ops *ops) { @@ -253,6 +263,7 @@ int ieee802154_register_device(struct ieee802154_dev *dev) priv->phy->add_iface = mac802154_add_iface; priv->phy->del_iface = mac802154_del_iface; priv->phy->set_txpower = mac802154_set_txpower; + priv->phy->set_lbt = mac802154_set_lbt; rc = wpan_phy_register(priv->phy); if (rc < 0) From ba08fea53a43e02b590d89224afdad976dece841 Mon Sep 17 00:00:00 2001 From: Phoebe Buckheister Date: Mon, 17 Feb 2014 11:34:11 +0100 Subject: [PATCH 06/10] ieee802154: add support for CCA mode in wpan phys The standard describes four modes of clear channel assesment: "energy above threshold", "carrier found", and the logical and/or of these two. Support for CCA mode setting is included in the at86rf230 driver, predicated for RF212 chips. Signed-off-by: Phoebe Buckheister Signed-off-by: David S. Miller --- drivers/net/ieee802154/at86rf230.c | 9 +++++++++ include/linux/nl802154.h | 1 + include/net/mac802154.h | 5 +++++ include/net/wpan-phy.h | 1 + net/ieee802154/nl-phy.c | 32 +++++++++++++++++++++++++++--- net/ieee802154/nl_policy.c | 1 + net/mac802154/ieee802154_dev.c | 11 ++++++++++ 7 files changed, 57 insertions(+), 3 deletions(-) diff --git a/drivers/net/ieee802154/at86rf230.c b/drivers/net/ieee802154/at86rf230.c index 3d40c2350261..c60871aff333 100644 --- a/drivers/net/ieee802154/at86rf230.c +++ b/drivers/net/ieee802154/at86rf230.c @@ -794,6 +794,14 @@ at86rf212_set_lbt(struct ieee802154_dev *dev, bool on) return at86rf230_write_subreg(lp, SR_CSMA_LBT_MODE, on); } +static int +at86rf212_set_cca_mode(struct ieee802154_dev *dev, u8 mode) +{ + struct at86rf230_local *lp = dev->priv; + + return at86rf230_write_subreg(lp, SR_CCA_MODE, mode); +} + static struct ieee802154_ops at86rf230_ops = { .owner = THIS_MODULE, .xmit = at86rf230_xmit, @@ -814,6 +822,7 @@ static struct ieee802154_ops at86rf212_ops = { .set_hw_addr_filt = at86rf230_set_hw_addr_filt, .set_txpower = at86rf212_set_txpower, .set_lbt = at86rf212_set_lbt, + .set_cca_mode = at86rf212_set_cca_mode, }; static void at86rf230_irqwork(struct work_struct *work) diff --git a/include/linux/nl802154.h b/include/linux/nl802154.h index 326baee227f7..5edefc14bd83 100644 --- a/include/linux/nl802154.h +++ b/include/linux/nl802154.h @@ -72,6 +72,7 @@ enum { IEEE802154_ATTR_TXPOWER, IEEE802154_ATTR_LBT_ENABLED, + IEEE802154_ATTR_CCA_MODE, __IEEE802154_ATTR_MAX, }; diff --git a/include/net/mac802154.h b/include/net/mac802154.h index 521edcb0e586..1a98e5014e66 100644 --- a/include/net/mac802154.h +++ b/include/net/mac802154.h @@ -122,6 +122,10 @@ struct ieee802154_dev { * Enables or disables listen before talk on the device. Called with * pib_lock held. * Returns either zero, or negative errno. + * + * set_cca_mode + * Sets the CCA mode used by the device. Called with pib_lock held. + * Returns either zero, or negative errno. */ struct ieee802154_ops { struct module *owner; @@ -140,6 +144,7 @@ struct ieee802154_ops { u8 addr[IEEE802154_ADDR_LEN]); int (*set_txpower)(struct ieee802154_dev *dev, int db); int (*set_lbt)(struct ieee802154_dev *dev, bool on); + int (*set_cca_mode)(struct ieee802154_dev *dev, u8 mode); }; /* Basic interface to register ieee802154 device */ diff --git a/include/net/wpan-phy.h b/include/net/wpan-phy.h index 804e6c4f5f8a..03b59051972d 100644 --- a/include/net/wpan-phy.h +++ b/include/net/wpan-phy.h @@ -58,6 +58,7 @@ struct wpan_phy { int (*set_txpower)(struct wpan_phy *phy, int db); int (*set_lbt)(struct wpan_phy *phy, bool on); + int (*set_cca_mode)(struct wpan_phy *phy, u8 cca_mode); char priv[0] __attribute__((__aligned__(NETDEV_ALIGN))); }; diff --git a/net/ieee802154/nl-phy.c b/net/ieee802154/nl-phy.c index f029310b0662..36f58d633868 100644 --- a/net/ieee802154/nl-phy.c +++ b/net/ieee802154/nl-phy.c @@ -57,7 +57,8 @@ static int ieee802154_nl_fill_phy(struct sk_buff *msg, u32 portid, nla_put_u8(msg, IEEE802154_ATTR_PAGE, phy->current_page) || nla_put_u8(msg, IEEE802154_ATTR_CHANNEL, phy->current_channel) || nla_put_s8(msg, IEEE802154_ATTR_TXPOWER, phy->transmit_power) || - nla_put_u8(msg, IEEE802154_ATTR_LBT_ENABLED, phy->lbt)) + nla_put_u8(msg, IEEE802154_ATTR_LBT_ENABLED, phy->lbt) || + nla_put_u8(msg, IEEE802154_ATTR_CCA_MODE, phy->cca_mode)) goto nla_put_failure; for (i = 0; i < 32; i++) { if (phy->channels_supported[i]) @@ -385,6 +386,23 @@ static int phy_set_lbt(struct wpan_phy *phy, struct genl_info *info) return 0; } +static int phy_set_cca_mode(struct wpan_phy *phy, struct genl_info *info) +{ + u8 mode = nla_get_u8(info->attrs[IEEE802154_ATTR_CCA_MODE]); + int rc; + + if (mode > 3) + return -EINVAL; + + rc = phy->set_cca_mode(phy, mode); + if (rc < 0) + return rc; + + phy->cca_mode = mode; + + return 0; +} + int ieee802154_set_phyparams(struct sk_buff *skb, struct genl_info *info) { struct wpan_phy *phy; @@ -394,7 +412,8 @@ int ieee802154_set_phyparams(struct sk_buff *skb, struct genl_info *info) pr_debug("%s\n", __func__); if (!info->attrs[IEEE802154_ATTR_PHY_NAME] && - !info->attrs[IEEE802154_ATTR_LBT_ENABLED]) + !info->attrs[IEEE802154_ATTR_LBT_ENABLED] && + !info->attrs[IEEE802154_ATTR_CCA_MODE]) return -EINVAL; name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]); @@ -406,7 +425,8 @@ int ieee802154_set_phyparams(struct sk_buff *skb, struct genl_info *info) return -ENODEV; if ((!phy->set_txpower && info->attrs[IEEE802154_ATTR_TXPOWER]) || - (!phy->set_lbt && info->attrs[IEEE802154_ATTR_LBT_ENABLED])) + (!phy->set_lbt && info->attrs[IEEE802154_ATTR_LBT_ENABLED]) || + (!phy->set_cca_mode && info->attrs[IEEE802154_ATTR_CCA_MODE])) goto out; mutex_lock(&phy->pib_lock); @@ -423,6 +443,12 @@ int ieee802154_set_phyparams(struct sk_buff *skb, struct genl_info *info) goto error; } + if (info->attrs[IEEE802154_ATTR_CCA_MODE]) { + rc = phy_set_cca_mode(phy, info); + if (rc < 0) + goto error; + } + mutex_unlock(&phy->pib_lock); wpan_phy_put(phy); diff --git a/net/ieee802154/nl_policy.c b/net/ieee802154/nl_policy.c index a09f6423a6e9..d87c2c904110 100644 --- a/net/ieee802154/nl_policy.c +++ b/net/ieee802154/nl_policy.c @@ -55,5 +55,6 @@ const struct nla_policy ieee802154_policy[IEEE802154_ATTR_MAX + 1] = { [IEEE802154_ATTR_TXPOWER] = { .type = NLA_S8, }, [IEEE802154_ATTR_LBT_ENABLED] = { .type = NLA_U8, }, + [IEEE802154_ATTR_CCA_MODE] = { .type = NLA_U8, }, }; diff --git a/net/mac802154/ieee802154_dev.c b/net/mac802154/ieee802154_dev.c index 56338c8cfc33..4965e4ce6b5b 100644 --- a/net/mac802154/ieee802154_dev.c +++ b/net/mac802154/ieee802154_dev.c @@ -185,6 +185,16 @@ static int mac802154_set_lbt(struct wpan_phy *phy, bool on) return priv->ops->set_lbt(&priv->hw, on); } +static int mac802154_set_cca_mode(struct wpan_phy *phy, u8 mode) +{ + struct mac802154_priv *priv = wpan_phy_priv(phy); + + if (!priv->ops->set_cca_mode) + return -ENOTSUPP; + + return priv->ops->set_cca_mode(&priv->hw, mode); +} + struct ieee802154_dev * ieee802154_alloc_device(size_t priv_data_len, struct ieee802154_ops *ops) { @@ -264,6 +274,7 @@ int ieee802154_register_device(struct ieee802154_dev *dev) priv->phy->del_iface = mac802154_del_iface; priv->phy->set_txpower = mac802154_set_txpower; priv->phy->set_lbt = mac802154_set_lbt; + priv->phy->set_cca_mode = mac802154_set_cca_mode; rc = wpan_phy_register(priv->phy); if (rc < 0) From 6ca001978dce0d50ebac01a38d6287f241a520c6 Mon Sep 17 00:00:00 2001 From: Phoebe Buckheister Date: Mon, 17 Feb 2014 11:34:12 +0100 Subject: [PATCH 07/10] ieee802154: add support for setting CCA energy detection levels Since three of the four clear channel assesment modes make use of energy detection, provide an API to set the energy detection threshold. Driver support for this is available in at86rf230 for the RF212 chips. Since for these chips the minimal energy detection threshold depends on page and channel used, add a field to struct at86rf230_local that stores the minimal threshold. Actual ED thresholds are configured as offsets from this value. For RF212, setting the ED threshold will not work before a channel/page has been set due to the dependency of energy detection in the chip and the actual channel/page selected. Signed-off-by: Phoebe Buckheister Signed-off-by: David S. Miller --- drivers/net/ieee802154/at86rf230.c | 26 ++++++++++++++++++++++++-- include/linux/nl802154.h | 1 + include/net/mac802154.h | 7 +++++++ include/net/wpan-phy.h | 2 ++ net/ieee802154/nl-phy.c | 30 +++++++++++++++++++++++++++--- net/ieee802154/nl_policy.c | 1 + net/mac802154/ieee802154_dev.c | 11 +++++++++++ 7 files changed, 73 insertions(+), 5 deletions(-) diff --git a/drivers/net/ieee802154/at86rf230.c b/drivers/net/ieee802154/at86rf230.c index c60871aff333..20596be61028 100644 --- a/drivers/net/ieee802154/at86rf230.c +++ b/drivers/net/ieee802154/at86rf230.c @@ -52,6 +52,8 @@ struct at86rf230_local { spinlock_t lock; bool irq_busy; bool is_tx; + + int rssi_base_val; }; static inline int is_rf212(struct at86rf230_local *local) @@ -580,6 +582,8 @@ at86rf230_stop(struct ieee802154_dev *dev) static int at86rf230_set_channel(struct at86rf230_local *lp, int page, int channel) { + lp->rssi_base_val = -91; + return at86rf230_write_subreg(lp, SR_CHANNEL, channel); } @@ -595,10 +599,13 @@ at86rf212_set_channel(struct at86rf230_local *lp, int page, int channel) if (rc < 0) return rc; - if (page == 0) + if (page == 0) { rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 0); - else + lp->rssi_base_val = -100; + } else { rc = at86rf230_write_subreg(lp, SR_BPSK_QPSK, 1); + lp->rssi_base_val = -98; + } if (rc < 0) return rc; @@ -802,6 +809,20 @@ at86rf212_set_cca_mode(struct ieee802154_dev *dev, u8 mode) return at86rf230_write_subreg(lp, SR_CCA_MODE, mode); } +static int +at86rf212_set_cca_ed_level(struct ieee802154_dev *dev, s32 level) +{ + struct at86rf230_local *lp = dev->priv; + int desens_steps; + + if (level < lp->rssi_base_val || level > 30) + return -EINVAL; + + desens_steps = (level - lp->rssi_base_val) * 100 / 207; + + return at86rf230_write_subreg(lp, SR_CCA_ED_THRES, desens_steps); +} + static struct ieee802154_ops at86rf230_ops = { .owner = THIS_MODULE, .xmit = at86rf230_xmit, @@ -823,6 +844,7 @@ static struct ieee802154_ops at86rf212_ops = { .set_txpower = at86rf212_set_txpower, .set_lbt = at86rf212_set_lbt, .set_cca_mode = at86rf212_set_cca_mode, + .set_cca_ed_level = at86rf212_set_cca_ed_level, }; static void at86rf230_irqwork(struct work_struct *work) diff --git a/include/linux/nl802154.h b/include/linux/nl802154.h index 5edefc14bd83..0594a0ae71ba 100644 --- a/include/linux/nl802154.h +++ b/include/linux/nl802154.h @@ -73,6 +73,7 @@ enum { IEEE802154_ATTR_TXPOWER, IEEE802154_ATTR_LBT_ENABLED, IEEE802154_ATTR_CCA_MODE, + IEEE802154_ATTR_CCA_ED_LEVEL, __IEEE802154_ATTR_MAX, }; diff --git a/include/net/mac802154.h b/include/net/mac802154.h index 1a98e5014e66..15fe6bca80f0 100644 --- a/include/net/mac802154.h +++ b/include/net/mac802154.h @@ -126,6 +126,11 @@ struct ieee802154_dev { * set_cca_mode * Sets the CCA mode used by the device. Called with pib_lock held. * Returns either zero, or negative errno. + * + * set_cca_ed_level + * Sets the CCA energy detection threshold in dBm. Called with pib_lock + * held. + * Returns either zero, or negative errno. */ struct ieee802154_ops { struct module *owner; @@ -145,6 +150,8 @@ struct ieee802154_ops { int (*set_txpower)(struct ieee802154_dev *dev, int db); int (*set_lbt)(struct ieee802154_dev *dev, bool on); int (*set_cca_mode)(struct ieee802154_dev *dev, u8 mode); + int (*set_cca_ed_level)(struct ieee802154_dev *dev, + s32 level); }; /* Basic interface to register ieee802154 device */ diff --git a/include/net/wpan-phy.h b/include/net/wpan-phy.h index 03b59051972d..0b570ad5e5fa 100644 --- a/include/net/wpan-phy.h +++ b/include/net/wpan-phy.h @@ -48,6 +48,7 @@ struct wpan_phy { u8 cca_mode; bool lbt; + s32 cca_ed_level; struct device dev; int idx; @@ -59,6 +60,7 @@ struct wpan_phy { int (*set_txpower)(struct wpan_phy *phy, int db); int (*set_lbt)(struct wpan_phy *phy, bool on); int (*set_cca_mode)(struct wpan_phy *phy, u8 cca_mode); + int (*set_cca_ed_level)(struct wpan_phy *phy, int level); char priv[0] __attribute__((__aligned__(NETDEV_ALIGN))); }; diff --git a/net/ieee802154/nl-phy.c b/net/ieee802154/nl-phy.c index 36f58d633868..0af0d424dee0 100644 --- a/net/ieee802154/nl-phy.c +++ b/net/ieee802154/nl-phy.c @@ -58,7 +58,8 @@ static int ieee802154_nl_fill_phy(struct sk_buff *msg, u32 portid, nla_put_u8(msg, IEEE802154_ATTR_CHANNEL, phy->current_channel) || nla_put_s8(msg, IEEE802154_ATTR_TXPOWER, phy->transmit_power) || nla_put_u8(msg, IEEE802154_ATTR_LBT_ENABLED, phy->lbt) || - nla_put_u8(msg, IEEE802154_ATTR_CCA_MODE, phy->cca_mode)) + nla_put_u8(msg, IEEE802154_ATTR_CCA_MODE, phy->cca_mode) || + nla_put_s32(msg, IEEE802154_ATTR_CCA_ED_LEVEL, phy->cca_ed_level)) goto nla_put_failure; for (i = 0; i < 32; i++) { if (phy->channels_supported[i]) @@ -403,6 +404,20 @@ static int phy_set_cca_mode(struct wpan_phy *phy, struct genl_info *info) return 0; } +static int phy_set_cca_ed_level(struct wpan_phy *phy, struct genl_info *info) +{ + s32 level = nla_get_s32(info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL]); + int rc; + + rc = phy->set_cca_ed_level(phy, level); + if (rc < 0) + return rc; + + phy->cca_ed_level = level; + + return 0; +} + int ieee802154_set_phyparams(struct sk_buff *skb, struct genl_info *info) { struct wpan_phy *phy; @@ -413,7 +428,8 @@ int ieee802154_set_phyparams(struct sk_buff *skb, struct genl_info *info) if (!info->attrs[IEEE802154_ATTR_PHY_NAME] && !info->attrs[IEEE802154_ATTR_LBT_ENABLED] && - !info->attrs[IEEE802154_ATTR_CCA_MODE]) + !info->attrs[IEEE802154_ATTR_CCA_MODE] && + !info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL]) return -EINVAL; name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]); @@ -426,7 +442,9 @@ int ieee802154_set_phyparams(struct sk_buff *skb, struct genl_info *info) if ((!phy->set_txpower && info->attrs[IEEE802154_ATTR_TXPOWER]) || (!phy->set_lbt && info->attrs[IEEE802154_ATTR_LBT_ENABLED]) || - (!phy->set_cca_mode && info->attrs[IEEE802154_ATTR_CCA_MODE])) + (!phy->set_cca_mode && info->attrs[IEEE802154_ATTR_CCA_MODE]) || + (!phy->set_cca_ed_level && + info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL])) goto out; mutex_lock(&phy->pib_lock); @@ -449,6 +467,12 @@ int ieee802154_set_phyparams(struct sk_buff *skb, struct genl_info *info) goto error; } + if (info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL]) { + rc = phy_set_cca_ed_level(phy, info); + if (rc < 0) + goto error; + } + mutex_unlock(&phy->pib_lock); wpan_phy_put(phy); diff --git a/net/ieee802154/nl_policy.c b/net/ieee802154/nl_policy.c index d87c2c904110..55b5616295ff 100644 --- a/net/ieee802154/nl_policy.c +++ b/net/ieee802154/nl_policy.c @@ -56,5 +56,6 @@ const struct nla_policy ieee802154_policy[IEEE802154_ATTR_MAX + 1] = { [IEEE802154_ATTR_TXPOWER] = { .type = NLA_S8, }, [IEEE802154_ATTR_LBT_ENABLED] = { .type = NLA_U8, }, [IEEE802154_ATTR_CCA_MODE] = { .type = NLA_U8, }, + [IEEE802154_ATTR_CCA_ED_LEVEL] = { .type = NLA_S32, }, }; diff --git a/net/mac802154/ieee802154_dev.c b/net/mac802154/ieee802154_dev.c index 4965e4ce6b5b..4707f36546d3 100644 --- a/net/mac802154/ieee802154_dev.c +++ b/net/mac802154/ieee802154_dev.c @@ -195,6 +195,16 @@ static int mac802154_set_cca_mode(struct wpan_phy *phy, u8 mode) return priv->ops->set_cca_mode(&priv->hw, mode); } +static int mac802154_set_cca_ed_level(struct wpan_phy *phy, s32 level) +{ + struct mac802154_priv *priv = wpan_phy_priv(phy); + + if (!priv->ops->set_cca_ed_level) + return -ENOTSUPP; + + return priv->ops->set_cca_ed_level(&priv->hw, level); +} + struct ieee802154_dev * ieee802154_alloc_device(size_t priv_data_len, struct ieee802154_ops *ops) { @@ -275,6 +285,7 @@ int ieee802154_register_device(struct ieee802154_dev *dev) priv->phy->set_txpower = mac802154_set_txpower; priv->phy->set_lbt = mac802154_set_lbt; priv->phy->set_cca_mode = mac802154_set_cca_mode; + priv->phy->set_cca_ed_level = mac802154_set_cca_ed_level; rc = wpan_phy_register(priv->phy); if (rc < 0) From 7dcbd22a97eb0689e6c583ad630ae0e7341e34c1 Mon Sep 17 00:00:00 2001 From: Phoebe Buckheister Date: Mon, 17 Feb 2014 11:34:13 +0100 Subject: [PATCH 08/10] ieee802154: ensure that first RF212 state comes from TRX_OFF A documented erratum of the RF212 chip describes that some versions of RF212 may not be properly reset by the standard reset procedure. The described workaround seems to not fully work either; my RF212 chips will not correctly receive any frames unless they are taken from RX mode to TRX_OFF and back to RX mode, effectively forcing a up-down-up-cycle. Going to TRX_OFF and to RX without intermediate state changes at least once fixes this. The same transition is allowed for RF230, so simply do it all the time. Signed-off-by: Phoebe Buckheister Signed-off-by: David S. Miller --- drivers/net/ieee802154/at86rf230.c | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/drivers/net/ieee802154/at86rf230.c b/drivers/net/ieee802154/at86rf230.c index 20596be61028..345b2eeb5a14 100644 --- a/drivers/net/ieee802154/at86rf230.c +++ b/drivers/net/ieee802154/at86rf230.c @@ -926,18 +926,9 @@ static int at86rf230_hw_init(struct at86rf230_local *lp) if (rc) return rc; - dev_info(&lp->spi->dev, "Status: %02x\n", status); - if (status == STATE_P_ON) { - rc = at86rf230_write_subreg(lp, SR_TRX_CMD, - STATE_FORCE_TRX_OFF); - if (rc) - return rc; - msleep(1); - rc = at86rf230_read_subreg(lp, SR_TRX_STATUS, &status); - if (rc) - return rc; - dev_info(&lp->spi->dev, "Status: %02x\n", status); - } + rc = at86rf230_write_subreg(lp, SR_TRX_CMD, STATE_FORCE_TRX_OFF); + if (rc) + return rc; /* configure irq polarity, defaults to high active */ if (pdata->irq_type & (IRQF_TRIGGER_FALLING | IRQF_TRIGGER_LOW)) @@ -965,16 +956,6 @@ static int at86rf230_hw_init(struct at86rf230_local *lp) /* Wait the next SLEEP cycle */ msleep(100); - rc = at86rf230_write_subreg(lp, SR_TRX_CMD, STATE_TX_ON); - if (rc) - return rc; - msleep(1); - - rc = at86rf230_read_subreg(lp, SR_TRX_STATUS, &status); - if (rc) - return rc; - dev_info(&lp->spi->dev, "Status: %02x\n", status); - rc = at86rf230_read_subreg(lp, SR_DVDD_OK, &status); if (rc) return rc; From 4244db1b0b7bc9ab7b67d8c1c38de6cf15bc87a8 Mon Sep 17 00:00:00 2001 From: Phoebe Buckheister Date: Mon, 17 Feb 2014 11:34:14 +0100 Subject: [PATCH 09/10] ieee802154: add netlink APIs for smartMAC configuration Introduce new netlink attributes for SET_PHY_ATTRS: * CSMA minimal backoff exponent * CSMA maximal backoff exponent * CSMA retry limit * frame retransmission limit The CSMA attributes shall correspond to minBE, maxBE and maxCSMABackoffs of 802.15.4, respectively. The frame retransmission shall correspond to maxFrameRetries of 802.15.4, unless given as -1: then the old behaviour of the stack shall apply. For RF2xy, the old behaviour is to not do channel sensing at all and simply send *right now*, which is not intended behaviour for most applications and actually prohibited for some channel/page combinations. For all values except frame retransmission limit, the defaults of 802.15.4 apply. Frame retransmission limits are set to -1 to indicate backward-compatible behaviour. Signed-off-by: Phoebe Buckheister Signed-off-by: David S. Miller --- include/linux/nl802154.h | 5 +++ include/net/mac802154.h | 12 ++++++ include/net/wpan-phy.h | 7 ++++ net/ieee802154/nl-phy.c | 69 +++++++++++++++++++++++++++++++++- net/ieee802154/nl_policy.c | 5 +++ net/ieee802154/wpan-class.c | 6 +++ net/mac802154/ieee802154_dev.c | 23 ++++++++++++ 7 files changed, 125 insertions(+), 2 deletions(-) diff --git a/include/linux/nl802154.h b/include/linux/nl802154.h index 0594a0ae71ba..e110b8c266f5 100644 --- a/include/linux/nl802154.h +++ b/include/linux/nl802154.h @@ -74,6 +74,11 @@ enum { IEEE802154_ATTR_LBT_ENABLED, IEEE802154_ATTR_CCA_MODE, IEEE802154_ATTR_CCA_ED_LEVEL, + IEEE802154_ATTR_CSMA_RETRIES, + IEEE802154_ATTR_CSMA_MIN_BE, + IEEE802154_ATTR_CSMA_MAX_BE, + + IEEE802154_ATTR_FRAME_RETRIES, __IEEE802154_ATTR_MAX, }; diff --git a/include/net/mac802154.h b/include/net/mac802154.h index 15fe6bca80f0..8ca3d04e7558 100644 --- a/include/net/mac802154.h +++ b/include/net/mac802154.h @@ -131,6 +131,14 @@ struct ieee802154_dev { * Sets the CCA energy detection threshold in dBm. Called with pib_lock * held. * Returns either zero, or negative errno. + * + * set_csma_params + * Sets the CSMA parameter set for the PHY. Called with pib_lock held. + * Returns either zero, or negative errno. + * + * set_frame_retries + * Sets the retransmission attempt limit. Called with pib_lock held. + * Returns either zero, or negative errno. */ struct ieee802154_ops { struct module *owner; @@ -152,6 +160,10 @@ struct ieee802154_ops { int (*set_cca_mode)(struct ieee802154_dev *dev, u8 mode); int (*set_cca_ed_level)(struct ieee802154_dev *dev, s32 level); + int (*set_csma_params)(struct ieee802154_dev *dev, + u8 min_be, u8 max_be, u8 retries); + int (*set_frame_retries)(struct ieee802154_dev *dev, + s8 retries); }; /* Basic interface to register ieee802154 device */ diff --git a/include/net/wpan-phy.h b/include/net/wpan-phy.h index 0b570ad5e5fa..10ab0fc6d4f7 100644 --- a/include/net/wpan-phy.h +++ b/include/net/wpan-phy.h @@ -46,6 +46,10 @@ struct wpan_phy { u32 channels_supported[32]; s8 transmit_power; u8 cca_mode; + u8 min_be; + u8 max_be; + u8 csma_retries; + s8 frame_retries; bool lbt; s32 cca_ed_level; @@ -61,6 +65,9 @@ struct wpan_phy { int (*set_lbt)(struct wpan_phy *phy, bool on); int (*set_cca_mode)(struct wpan_phy *phy, u8 cca_mode); int (*set_cca_ed_level)(struct wpan_phy *phy, int level); + int (*set_csma_params)(struct wpan_phy *phy, u8 min_be, u8 max_be, + u8 retries); + int (*set_frame_retries)(struct wpan_phy *phy, s8 retries); char priv[0] __attribute__((__aligned__(NETDEV_ALIGN))); }; diff --git a/net/ieee802154/nl-phy.c b/net/ieee802154/nl-phy.c index 0af0d424dee0..c9dfd6f59e34 100644 --- a/net/ieee802154/nl-phy.c +++ b/net/ieee802154/nl-phy.c @@ -59,7 +59,11 @@ static int ieee802154_nl_fill_phy(struct sk_buff *msg, u32 portid, nla_put_s8(msg, IEEE802154_ATTR_TXPOWER, phy->transmit_power) || nla_put_u8(msg, IEEE802154_ATTR_LBT_ENABLED, phy->lbt) || nla_put_u8(msg, IEEE802154_ATTR_CCA_MODE, phy->cca_mode) || - nla_put_s32(msg, IEEE802154_ATTR_CCA_ED_LEVEL, phy->cca_ed_level)) + nla_put_s32(msg, IEEE802154_ATTR_CCA_ED_LEVEL, phy->cca_ed_level) || + nla_put_u8(msg, IEEE802154_ATTR_CSMA_RETRIES, phy->csma_retries) || + nla_put_u8(msg, IEEE802154_ATTR_CSMA_MIN_BE, phy->min_be) || + nla_put_u8(msg, IEEE802154_ATTR_CSMA_MAX_BE, phy->max_be) || + nla_put_s8(msg, IEEE802154_ATTR_FRAME_RETRIES, phy->frame_retries)) goto nla_put_failure; for (i = 0; i < 32; i++) { if (phy->channels_supported[i]) @@ -418,6 +422,49 @@ static int phy_set_cca_ed_level(struct wpan_phy *phy, struct genl_info *info) return 0; } +static int phy_set_csma_params(struct wpan_phy *phy, struct genl_info *info) +{ + int rc; + u8 min_be = phy->min_be; + u8 max_be = phy->max_be; + u8 retries = phy->csma_retries; + + if (info->attrs[IEEE802154_ATTR_CSMA_RETRIES]) + retries = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_RETRIES]); + if (info->attrs[IEEE802154_ATTR_CSMA_MIN_BE]) + min_be = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_MIN_BE]); + if (info->attrs[IEEE802154_ATTR_CSMA_MAX_BE]) + max_be = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_MAX_BE]); + + if (retries > 5 || max_be > 8 || min_be > max_be || + retries < -1 || retries > 7) + return -EINVAL; + + rc = phy->set_csma_params(phy, min_be, max_be, retries); + if (rc < 0) + return rc; + + phy->min_be = min_be; + phy->max_be = max_be; + phy->csma_retries = retries; + + return 0; +} + +static int phy_set_frame_retries(struct wpan_phy *phy, struct genl_info *info) +{ + s8 retries = nla_get_s8(info->attrs[IEEE802154_ATTR_FRAME_RETRIES]); + int rc; + + rc = phy->set_frame_retries(phy, retries); + if (rc < 0) + return rc; + + phy->frame_retries = retries; + + return 0; +} + int ieee802154_set_phyparams(struct sk_buff *skb, struct genl_info *info) { struct wpan_phy *phy; @@ -429,7 +476,11 @@ int ieee802154_set_phyparams(struct sk_buff *skb, struct genl_info *info) if (!info->attrs[IEEE802154_ATTR_PHY_NAME] && !info->attrs[IEEE802154_ATTR_LBT_ENABLED] && !info->attrs[IEEE802154_ATTR_CCA_MODE] && - !info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL]) + !info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL] && + !info->attrs[IEEE802154_ATTR_CSMA_RETRIES] && + !info->attrs[IEEE802154_ATTR_CSMA_MIN_BE] && + !info->attrs[IEEE802154_ATTR_CSMA_MAX_BE] && + !info->attrs[IEEE802154_ATTR_FRAME_RETRIES]) return -EINVAL; name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]); @@ -473,6 +524,20 @@ int ieee802154_set_phyparams(struct sk_buff *skb, struct genl_info *info) goto error; } + if (info->attrs[IEEE802154_ATTR_CSMA_RETRIES] || + info->attrs[IEEE802154_ATTR_CSMA_MIN_BE] || + info->attrs[IEEE802154_ATTR_CSMA_MAX_BE]) { + rc = phy_set_csma_params(phy, info); + if (rc < 0) + goto error; + } + + if (info->attrs[IEEE802154_ATTR_FRAME_RETRIES]) { + rc = phy_set_frame_retries(phy, info); + if (rc < 0) + goto error; + } + mutex_unlock(&phy->pib_lock); wpan_phy_put(phy); diff --git a/net/ieee802154/nl_policy.c b/net/ieee802154/nl_policy.c index 55b5616295ff..fd7be5e45cef 100644 --- a/net/ieee802154/nl_policy.c +++ b/net/ieee802154/nl_policy.c @@ -57,5 +57,10 @@ const struct nla_policy ieee802154_policy[IEEE802154_ATTR_MAX + 1] = { [IEEE802154_ATTR_LBT_ENABLED] = { .type = NLA_U8, }, [IEEE802154_ATTR_CCA_MODE] = { .type = NLA_U8, }, [IEEE802154_ATTR_CCA_ED_LEVEL] = { .type = NLA_S32, }, + [IEEE802154_ATTR_CSMA_RETRIES] = { .type = NLA_U8, }, + [IEEE802154_ATTR_CSMA_MIN_BE] = { .type = NLA_U8, }, + [IEEE802154_ATTR_CSMA_MAX_BE] = { .type = NLA_U8, }, + + [IEEE802154_ATTR_FRAME_RETRIES] = { .type = NLA_S8, }, }; diff --git a/net/ieee802154/wpan-class.c b/net/ieee802154/wpan-class.c index 8d6f6704da84..edd0962d55f9 100644 --- a/net/ieee802154/wpan-class.c +++ b/net/ieee802154/wpan-class.c @@ -169,6 +169,12 @@ struct wpan_phy *wpan_phy_alloc(size_t priv_size) phy->current_channel = -1; /* not initialised */ phy->current_page = 0; /* for compatibility */ + /* defaults per 802.15.4-2011 */ + phy->min_be = 3; + phy->max_be = 5; + phy->csma_retries = 4; + phy->frame_retries = -1; /* for compatibility, actual default is 3 */ + return phy; out: diff --git a/net/mac802154/ieee802154_dev.c b/net/mac802154/ieee802154_dev.c index 4707f36546d3..b75bb01e5c6b 100644 --- a/net/mac802154/ieee802154_dev.c +++ b/net/mac802154/ieee802154_dev.c @@ -205,6 +205,27 @@ static int mac802154_set_cca_ed_level(struct wpan_phy *phy, s32 level) return priv->ops->set_cca_ed_level(&priv->hw, level); } +static int mac802154_set_csma_params(struct wpan_phy *phy, u8 min_be, + u8 max_be, u8 retries) +{ + struct mac802154_priv *priv = wpan_phy_priv(phy); + + if (!priv->ops->set_csma_params) + return -ENOTSUPP; + + return priv->ops->set_csma_params(&priv->hw, min_be, max_be, retries); +} + +static int mac802154_set_frame_retries(struct wpan_phy *phy, s8 retries) +{ + struct mac802154_priv *priv = wpan_phy_priv(phy); + + if (!priv->ops->set_frame_retries) + return -ENOTSUPP; + + return priv->ops->set_frame_retries(&priv->hw, retries); +} + struct ieee802154_dev * ieee802154_alloc_device(size_t priv_data_len, struct ieee802154_ops *ops) { @@ -286,6 +307,8 @@ int ieee802154_register_device(struct ieee802154_dev *dev) priv->phy->set_lbt = mac802154_set_lbt; priv->phy->set_cca_mode = mac802154_set_cca_mode; priv->phy->set_cca_ed_level = mac802154_set_cca_ed_level; + priv->phy->set_csma_params = mac802154_set_csma_params; + priv->phy->set_frame_retries = mac802154_set_frame_retries; rc = wpan_phy_register(priv->phy); if (rc < 0) From f2fdd67c6bc89de0100410efb37de69b1c98ac03 Mon Sep 17 00:00:00 2001 From: Phoebe Buckheister Date: Mon, 17 Feb 2014 11:34:15 +0100 Subject: [PATCH 10/10] ieee802154: enable smart transmitter features of RF212 This patch does multiple things: * add .set_csma_params and .set_frame_retries for the RF212 radio. This should work fine with RF230, but since I have no RF230 radios to test with, RF230 does not implement these right now * enable TX_ARET for frame retransmission limits greater than -1 Since RF230 has no operations to change CSMA parameters or frame retry limits, RF230 will not be able to enter TX_ARET with this patch. Signed-off-by: Phoebe Buckheister Signed-off-by: David S. Miller --- drivers/net/ieee802154/at86rf230.c | 64 +++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/drivers/net/ieee802154/at86rf230.c b/drivers/net/ieee802154/at86rf230.c index 345b2eeb5a14..34bf011584fb 100644 --- a/drivers/net/ieee802154/at86rf230.c +++ b/drivers/net/ieee802154/at86rf230.c @@ -52,6 +52,7 @@ struct at86rf230_local { spinlock_t lock; bool irq_busy; bool is_tx; + bool tx_aret; int rssi_base_val; }; @@ -549,7 +550,9 @@ at86rf230_state(struct ieee802154_dev *dev, int state) } while (val == STATE_TRANSITION_IN_PROGRESS); - if (val == desired_status) + if (val == desired_status || + (desired_status == STATE_RX_ON && val == STATE_BUSY_RX) || + (desired_status == STATE_RX_AACK_ON && val == STATE_BUSY_RX_AACK)) return 0; pr_err("unexpected state change: %d, asked for %d\n", val, state); @@ -570,6 +573,10 @@ at86rf230_start(struct ieee802154_dev *dev) if (rc) return rc; + rc = at86rf230_state(dev, STATE_FORCE_TX_ON); + if (rc) + return rc; + return at86rf230_state(dev, STATE_RX_AACK_ON); } @@ -669,6 +676,12 @@ at86rf230_xmit(struct ieee802154_dev *dev, struct sk_buff *skb) if (rc) goto err_rx; + if (lp->tx_aret) { + rc = at86rf230_write_subreg(lp, SR_TRX_CMD, STATE_TX_ARET_ON); + if (rc) + goto err_rx; + } + rc = at86rf230_write_subreg(lp, SR_TRX_CMD, STATE_BUSY_TX); if (rc) goto err_rx; @@ -823,6 +836,44 @@ at86rf212_set_cca_ed_level(struct ieee802154_dev *dev, s32 level) return at86rf230_write_subreg(lp, SR_CCA_ED_THRES, desens_steps); } +static int +at86rf212_set_csma_params(struct ieee802154_dev *dev, u8 min_be, u8 max_be, + u8 retries) +{ + struct at86rf230_local *lp = dev->priv; + int rc; + + if (min_be > max_be || max_be > 8 || retries > 5) + return -EINVAL; + + rc = at86rf230_write_subreg(lp, SR_MIN_BE, min_be); + if (rc) + return rc; + + rc = at86rf230_write_subreg(lp, SR_MAX_BE, max_be); + if (rc) + return rc; + + return at86rf230_write_subreg(lp, SR_MAX_CSMA_RETRIES, max_be); +} + +static int +at86rf212_set_frame_retries(struct ieee802154_dev *dev, s8 retries) +{ + struct at86rf230_local *lp = dev->priv; + int rc = 0; + + if (retries < -1 || retries > 15) + return -EINVAL; + + lp->tx_aret = retries >= 0; + + if (retries >= 0) + rc = at86rf230_write_subreg(lp, SR_MAX_FRAME_RETRIES, retries); + + return rc; +} + static struct ieee802154_ops at86rf230_ops = { .owner = THIS_MODULE, .xmit = at86rf230_xmit, @@ -845,6 +896,8 @@ static struct ieee802154_ops at86rf212_ops = { .set_lbt = at86rf212_set_lbt, .set_cca_mode = at86rf212_set_cca_mode, .set_cca_ed_level = at86rf212_set_cca_ed_level, + .set_csma_params = at86rf212_set_csma_params, + .set_frame_retries = at86rf212_set_frame_retries, }; static void at86rf230_irqwork(struct work_struct *work) @@ -921,6 +974,7 @@ static int at86rf230_hw_init(struct at86rf230_local *lp) struct at86rf230_platform_data *pdata = lp->spi->dev.platform_data; int rc, irq_pol; u8 status; + u8 csma_seed[2]; rc = at86rf230_read_subreg(lp, SR_TRX_STATUS, &status); if (rc) @@ -944,6 +998,14 @@ static int at86rf230_hw_init(struct at86rf230_local *lp) if (rc) return rc; + get_random_bytes(csma_seed, ARRAY_SIZE(csma_seed)); + rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_0, csma_seed[0]); + if (rc) + return rc; + rc = at86rf230_write_subreg(lp, SR_CSMA_SEED_1, csma_seed[1]); + if (rc) + return rc; + /* CLKM changes are applied immediately */ rc = at86rf230_write_subreg(lp, SR_CLKM_SHA_SEL, 0x00); if (rc)