Skip to content

Commit 9b2777d

Browse files
Phoebe Buckheisterdavem330
authored andcommitted
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 <phoebe.buckheister@itwm.fraunhofer.de> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 5b520bb commit 9b2777d

10 files changed

Lines changed: 102 additions & 6 deletions

File tree

‎drivers/net/ieee802154/at86rf230.c‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,30 @@ at86rf230_set_hw_addr_filt(struct ieee802154_dev *dev,
753753
return 0;
754754
}
755755

756+
static int
757+
at86rf212_set_txpower(struct ieee802154_dev *dev, int db)
758+
{
759+
struct at86rf230_local *lp = dev->priv;
760+
int rc;
761+
762+
/* typical maximum output is 5dBm with RG_PHY_TX_PWR 0x60, lower five
763+
* bits decrease power in 1dB steps. 0x60 represents extra PA gain of
764+
* 0dB.
765+
* thus, supported values for db range from -26 to 5, for 31dB of
766+
* reduction to 0dB of reduction.
767+
*/
768+
if (db > 5 || db < -26)
769+
return -EINVAL;
770+
771+
db = -(db - 5);
772+
773+
rc = __at86rf230_write(lp, RG_PHY_TX_PWR, 0x60 | db);
774+
if (rc)
775+
return rc;
776+
777+
return 0;
778+
}
779+
756780
static struct ieee802154_ops at86rf230_ops = {
757781
.owner = THIS_MODULE,
758782
.xmit = at86rf230_xmit,
@@ -771,6 +795,7 @@ static struct ieee802154_ops at86rf212_ops = {
771795
.start = at86rf230_start,
772796
.stop = at86rf230_stop,
773797
.set_hw_addr_filt = at86rf230_set_hw_addr_filt,
798+
.set_txpower = at86rf212_set_txpower,
774799
};
775800

776801
static void at86rf230_irqwork(struct work_struct *work)

‎include/linux/nl802154.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ enum {
7070
IEEE802154_ATTR_PHY_NAME,
7171
IEEE802154_ATTR_DEV_TYPE,
7272

73+
IEEE802154_ATTR_TXPOWER,
74+
7375
__IEEE802154_ATTR_MAX,
7476
};
7577

@@ -122,6 +124,8 @@ enum {
122124
IEEE802154_ADD_IFACE,
123125
IEEE802154_DEL_IFACE,
124126

127+
IEEE802154_SET_PHYPARAMS,
128+
125129
__IEEE802154_CMD_MAX,
126130
};
127131

‎include/net/mac802154.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ struct ieee802154_dev {
113113
* Set radio for listening on specific address.
114114
* Set the device for listening on specified address.
115115
* Returns either zero, or negative errno.
116+
*
117+
* set_txpower:
118+
* Set radio transmit power in dB. Called with pib_lock held.
119+
* Returns either zero, or negative errno.
116120
*/
117121
struct ieee802154_ops {
118122
struct module *owner;
@@ -129,6 +133,7 @@ struct ieee802154_ops {
129133
unsigned long changed);
130134
int (*ieee_addr)(struct ieee802154_dev *dev,
131135
u8 addr[IEEE802154_ADDR_LEN]);
136+
int (*set_txpower)(struct ieee802154_dev *dev, int db);
132137
};
133138

134139
/* Basic interface to register ieee802154 device */

‎include/net/wpan-phy.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ struct wpan_phy {
3737
struct mutex pib_lock;
3838

3939
/*
40-
* This is a PIB according to 802.15.4-2006.
40+
* This is a PIB according to 802.15.4-2011.
4141
* We do not provide timing-related variables, as they
4242
* aren't used outside of driver
4343
*/
4444
u8 current_channel;
4545
u8 current_page;
4646
u32 channels_supported[32];
47-
u8 transmit_power;
47+
s8 transmit_power;
4848
u8 cca_mode;
4949

5050
struct device dev;
@@ -54,6 +54,8 @@ struct wpan_phy {
5454
const char *name, int type);
5555
void (*del_iface)(struct wpan_phy *phy, struct net_device *dev);
5656

57+
int (*set_txpower)(struct wpan_phy *phy, int db);
58+
5759
char priv[0] __attribute__((__aligned__(NETDEV_ALIGN)));
5860
};
5961

‎net/ieee802154/ieee802154.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ int ieee802154_list_phy(struct sk_buff *skb, struct genl_info *info);
5353
int ieee802154_dump_phy(struct sk_buff *skb, struct netlink_callback *cb);
5454
int ieee802154_add_iface(struct sk_buff *skb, struct genl_info *info);
5555
int ieee802154_del_iface(struct sk_buff *skb, struct genl_info *info);
56+
int ieee802154_set_phyparams(struct sk_buff *skb, struct genl_info *info);
5657

5758
enum ieee802154_mcgrp_ids {
5859
IEEE802154_COORD_MCGRP,

‎net/ieee802154/netlink.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ static const struct genl_ops ieee8021154_ops[] = {
115115
ieee802154_dump_phy),
116116
IEEE802154_OP(IEEE802154_ADD_IFACE, ieee802154_add_iface),
117117
IEEE802154_OP(IEEE802154_DEL_IFACE, ieee802154_del_iface),
118+
IEEE802154_OP(IEEE802154_SET_PHYPARAMS, ieee802154_set_phyparams),
118119
/* see nl-mac.c */
119120
IEEE802154_OP(IEEE802154_ASSOCIATE_REQ, ieee802154_associate_req),
120121
IEEE802154_OP(IEEE802154_ASSOCIATE_RESP, ieee802154_associate_resp),

‎net/ieee802154/nl-phy.c‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ static int ieee802154_nl_fill_phy(struct sk_buff *msg, u32 portid,
5555
mutex_lock(&phy->pib_lock);
5656
if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
5757
nla_put_u8(msg, IEEE802154_ATTR_PAGE, phy->current_page) ||
58-
nla_put_u8(msg, IEEE802154_ATTR_CHANNEL, phy->current_channel))
58+
nla_put_u8(msg, IEEE802154_ATTR_CHANNEL, phy->current_channel) ||
59+
nla_put_s8(msg, IEEE802154_ATTR_TXPOWER, phy->transmit_power))
5960
goto nla_put_failure;
6061
for (i = 0; i < 32; i++) {
6162
if (phy->channels_supported[i])
@@ -354,3 +355,49 @@ int ieee802154_del_iface(struct sk_buff *skb, struct genl_info *info)
354355

355356
return rc;
356357
}
358+
359+
int ieee802154_set_phyparams(struct sk_buff *skb, struct genl_info *info)
360+
{
361+
struct wpan_phy *phy;
362+
const char *name;
363+
int txpower;
364+
int rc = -EINVAL;
365+
366+
pr_debug("%s\n", __func__);
367+
368+
if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
369+
return -EINVAL;
370+
371+
name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
372+
if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
373+
return -EINVAL; /* phy name should be null-terminated */
374+
375+
txpower = nla_get_s8(info->attrs[IEEE802154_ATTR_TXPOWER]);
376+
377+
phy = wpan_phy_find(name);
378+
if (!phy)
379+
return -ENODEV;
380+
381+
if (!phy->set_txpower)
382+
goto out;
383+
384+
mutex_lock(&phy->pib_lock);
385+
386+
rc = phy->set_txpower(phy, txpower);
387+
if (rc < 0) {
388+
mutex_unlock(&phy->pib_lock);
389+
goto out;
390+
}
391+
392+
phy->transmit_power = txpower;
393+
394+
mutex_unlock(&phy->pib_lock);
395+
396+
wpan_phy_put(phy);
397+
398+
return 0;
399+
400+
out:
401+
wpan_phy_put(phy);
402+
return rc;
403+
}

‎net/ieee802154/nl_policy.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,7 @@ const struct nla_policy ieee802154_policy[IEEE802154_ATTR_MAX + 1] = {
5252
[IEEE802154_ATTR_DURATION] = { .type = NLA_U8, },
5353
[IEEE802154_ATTR_ED_LIST] = { .len = 27 },
5454
[IEEE802154_ATTR_CHANNEL_PAGE_LIST] = { .len = 32 * 4, },
55+
56+
[IEEE802154_ATTR_TXPOWER] = { .type = NLA_S8, },
5557
};
5658

‎net/ieee802154/wpan-class.c‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ static DEVICE_ATTR_RO(name);
4444

4545
MASTER_SHOW(current_channel, "%d");
4646
MASTER_SHOW(current_page, "%d");
47-
MASTER_SHOW_COMPLEX(transmit_power, "%d +- %d dB",
48-
((signed char) (phy->transmit_power << 2)) >> 2,
49-
(phy->transmit_power >> 6) ? (phy->transmit_power >> 6) * 3 : 1);
47+
MASTER_SHOW(transmit_power, "%d +- 1 dB");
5048
MASTER_SHOW(cca_mode, "%d");
5149

5250
static ssize_t channels_supported_show(struct device *dev,

‎net/mac802154/ieee802154_dev.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,16 @@ mac802154_add_iface(struct wpan_phy *phy, const char *name, int type)
165165
return ERR_PTR(err);
166166
}
167167

168+
static int mac802154_set_txpower(struct wpan_phy *phy, int db)
169+
{
170+
struct mac802154_priv *priv = wpan_phy_priv(phy);
171+
172+
if (!priv->ops->set_txpower)
173+
return -ENOTSUPP;
174+
175+
return priv->ops->set_txpower(&priv->hw, db);
176+
}
177+
168178
struct ieee802154_dev *
169179
ieee802154_alloc_device(size_t priv_data_len, struct ieee802154_ops *ops)
170180
{
@@ -242,6 +252,7 @@ int ieee802154_register_device(struct ieee802154_dev *dev)
242252

243253
priv->phy->add_iface = mac802154_add_iface;
244254
priv->phy->del_iface = mac802154_del_iface;
255+
priv->phy->set_txpower = mac802154_set_txpower;
245256

246257
rc = wpan_phy_register(priv->phy);
247258
if (rc < 0)

0 commit comments

Comments
 (0)