Skip to content

Commit ffb9eb3

Browse files
Kalle Valolinvjw
authored andcommitted
nl80211: add power save commands
The most needed command from nl80211, which Wireless Extensions had, is support for power save mode. Add a simple command to make it possible to enable and disable power save via nl80211. I was also planning about extending the interface, for example adding the timeout value, but after thinking more about this I decided not to do it. Basically there were three reasons: Firstly, the parameters for power save are very much hardware dependent. Trying to find a unified interface which would work with all hardware, and still make sense to users, will be very difficult. Secondly, IEEE 802.11 power save implementation in Linux is still in state of flux. We have a long way to still to go and there is no way to predict what kind of implementation we will have after few years. And because we need to support nl80211 interface a long time, practically forever, adding now parameters to nl80211 might create maintenance problems later on. Third issue are the users. Power save parameters are mostly used for debugging, so debugfs is better, more flexible, interface for this. For example, wpa_supplicant currently doesn't configure anything related to power save mode. It's better to strive that kernel can automatically optimise the power save parameters, like with help of pm qos network and other traffic parameters. Later on, when we have better understanding of power save, we can extend this command with more features, if there's a need for that. Signed-off-by: Kalle Valo <kalle.valo@nokia.com> Signed-off-by: John W. Linville <linville@tuxdriver.com>
1 parent 088ea18 commit ffb9eb3

5 files changed

Lines changed: 159 additions & 15 deletions

File tree

‎include/linux/nl80211.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,9 @@ enum nl80211_commands {
416416
NL80211_CMD_ACTION,
417417
NL80211_CMD_ACTION_TX_STATUS,
418418

419+
NL80211_CMD_SET_POWER_SAVE,
420+
NL80211_CMD_GET_POWER_SAVE,
421+
419422
/* add new commands above here */
420423

421424
/* used to define NL80211_CMD_MAX below */
@@ -837,6 +840,8 @@ enum nl80211_attrs {
837840

838841
NL80211_ATTR_ACK,
839842

843+
NL80211_ATTR_PS_STATE,
844+
840845
/* add attributes here, update the policy in nl80211.c */
841846

842847
__NL80211_ATTR_AFTER_LAST,
@@ -1573,4 +1578,9 @@ enum nl80211_band {
15731578
NL80211_BAND_5GHZ,
15741579
};
15751580

1581+
enum nl80211_ps_state {
1582+
NL80211_PS_DISABLED,
1583+
NL80211_PS_ENABLED,
1584+
};
1585+
15761586
#endif /* __LINUX_NL80211_H */

‎include/net/cfg80211.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,6 @@ struct cfg80211_ops {
11501150
enum nl80211_channel_type channel_type,
11511151
const u8 *buf, size_t len, u64 *cookie);
11521152

1153-
/* some temporary stuff to finish wext */
11541153
int (*set_power_mgmt)(struct wiphy *wiphy, struct net_device *dev,
11551154
bool enabled, int timeout);
11561155
};
@@ -1489,6 +1488,9 @@ struct wireless_dev {
14891488
struct cfg80211_internal_bss *auth_bsses[MAX_AUTH_BSSES];
14901489
struct cfg80211_internal_bss *current_bss; /* associated / joined */
14911490

1491+
bool ps;
1492+
int ps_timeout;
1493+
14921494
#ifdef CONFIG_CFG80211_WEXT
14931495
/* wext data */
14941496
struct {
@@ -1500,8 +1502,7 @@ struct wireless_dev {
15001502
u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
15011503
u8 ssid[IEEE80211_MAX_SSID_LEN];
15021504
s8 default_key, default_mgmt_key;
1503-
bool ps, prev_bssid_valid;
1504-
int ps_timeout;
1505+
bool prev_bssid_valid;
15051506
} wext;
15061507
#endif
15071508
};

‎net/wireless/core.c‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -698,19 +698,21 @@ static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
698698
wdev->wext.default_key = -1;
699699
wdev->wext.default_mgmt_key = -1;
700700
wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
701+
#endif
702+
701703
if (wdev->wiphy->flags & WIPHY_FLAG_PS_ON_BY_DEFAULT)
702-
wdev->wext.ps = true;
704+
wdev->ps = true;
703705
else
704-
wdev->wext.ps = false;
705-
wdev->wext.ps_timeout = 100;
706+
wdev->ps = false;
707+
wdev->ps_timeout = 100;
706708
if (rdev->ops->set_power_mgmt)
707709
if (rdev->ops->set_power_mgmt(wdev->wiphy, dev,
708-
wdev->wext.ps,
709-
wdev->wext.ps_timeout)) {
710+
wdev->ps,
711+
wdev->ps_timeout)) {
710712
/* assume this means it's off */
711-
wdev->wext.ps = false;
713+
wdev->ps = false;
712714
}
713-
#endif
715+
714716
if (!dev->ethtool_ops)
715717
dev->ethtool_ops = &cfg80211_ethtool_ops;
716718

‎net/wireless/nl80211.c‎

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ static struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] __read_mostly = {
148148
[NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
149149
.len = IEEE80211_MAX_DATA_LEN },
150150
[NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
151+
[NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
151152
};
152153

153154
/* policy for the attributes */
@@ -4663,6 +4664,124 @@ static int nl80211_action(struct sk_buff *skb, struct genl_info *info)
46634664
return err;
46644665
}
46654666

4667+
static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
4668+
{
4669+
struct cfg80211_registered_device *rdev;
4670+
struct wireless_dev *wdev;
4671+
struct net_device *dev;
4672+
u8 ps_state;
4673+
bool state;
4674+
int err;
4675+
4676+
if (!info->attrs[NL80211_ATTR_PS_STATE]) {
4677+
err = -EINVAL;
4678+
goto out;
4679+
}
4680+
4681+
ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
4682+
4683+
if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED) {
4684+
err = -EINVAL;
4685+
goto out;
4686+
}
4687+
4688+
rtnl_lock();
4689+
4690+
err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4691+
if (err)
4692+
goto unlock_rdev;
4693+
4694+
wdev = dev->ieee80211_ptr;
4695+
4696+
if (!rdev->ops->set_power_mgmt) {
4697+
err = -EOPNOTSUPP;
4698+
goto unlock_rdev;
4699+
}
4700+
4701+
state = (ps_state == NL80211_PS_ENABLED) ? true : false;
4702+
4703+
if (state == wdev->ps)
4704+
goto unlock_rdev;
4705+
4706+
wdev->ps = state;
4707+
4708+
if (rdev->ops->set_power_mgmt(wdev->wiphy, dev, wdev->ps,
4709+
wdev->ps_timeout))
4710+
/* assume this means it's off */
4711+
wdev->ps = false;
4712+
4713+
unlock_rdev:
4714+
cfg80211_unlock_rdev(rdev);
4715+
dev_put(dev);
4716+
rtnl_unlock();
4717+
4718+
out:
4719+
return err;
4720+
}
4721+
4722+
static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
4723+
{
4724+
struct cfg80211_registered_device *rdev;
4725+
enum nl80211_ps_state ps_state;
4726+
struct wireless_dev *wdev;
4727+
struct net_device *dev;
4728+
struct sk_buff *msg;
4729+
void *hdr;
4730+
int err;
4731+
4732+
rtnl_lock();
4733+
4734+
err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4735+
if (err)
4736+
goto unlock_rtnl;
4737+
4738+
wdev = dev->ieee80211_ptr;
4739+
4740+
if (!rdev->ops->set_power_mgmt) {
4741+
err = -EOPNOTSUPP;
4742+
goto out;
4743+
}
4744+
4745+
msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4746+
if (!msg) {
4747+
err = -ENOMEM;
4748+
goto out;
4749+
}
4750+
4751+
hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4752+
NL80211_CMD_GET_POWER_SAVE);
4753+
if (!hdr) {
4754+
err = -ENOMEM;
4755+
goto free_msg;
4756+
}
4757+
4758+
if (wdev->ps)
4759+
ps_state = NL80211_PS_ENABLED;
4760+
else
4761+
ps_state = NL80211_PS_DISABLED;
4762+
4763+
NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
4764+
4765+
genlmsg_end(msg, hdr);
4766+
err = genlmsg_reply(msg, info);
4767+
goto out;
4768+
4769+
nla_put_failure:
4770+
err = -ENOBUFS;
4771+
4772+
free_msg:
4773+
nlmsg_free(msg);
4774+
4775+
out:
4776+
cfg80211_unlock_rdev(rdev);
4777+
dev_put(dev);
4778+
4779+
unlock_rtnl:
4780+
rtnl_unlock();
4781+
4782+
return err;
4783+
}
4784+
46664785
static struct genl_ops nl80211_ops[] = {
46674786
{
46684787
.cmd = NL80211_CMD_GET_WIPHY,
@@ -4955,6 +5074,18 @@ static struct genl_ops nl80211_ops[] = {
49555074
.policy = nl80211_policy,
49565075
.flags = GENL_ADMIN_PERM,
49575076
},
5077+
{
5078+
.cmd = NL80211_CMD_SET_POWER_SAVE,
5079+
.doit = nl80211_set_power_save,
5080+
.policy = nl80211_policy,
5081+
.flags = GENL_ADMIN_PERM,
5082+
},
5083+
{
5084+
.cmd = NL80211_CMD_GET_POWER_SAVE,
5085+
.doit = nl80211_get_power_save,
5086+
.policy = nl80211_policy,
5087+
/* can be retrieved by unprivileged users */
5088+
},
49585089
};
49595090

49605091
static struct genl_multicast_group nl80211_mlme_mcgrp = {

‎net/wireless/wext-compat.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,8 +1099,8 @@ int cfg80211_wext_siwpower(struct net_device *dev,
10991099
{
11001100
struct wireless_dev *wdev = dev->ieee80211_ptr;
11011101
struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
1102-
bool ps = wdev->wext.ps;
1103-
int timeout = wdev->wext.ps_timeout;
1102+
bool ps = wdev->ps;
1103+
int timeout = wdev->ps_timeout;
11041104
int err;
11051105

11061106
if (wdev->iftype != NL80211_IFTYPE_STATION)
@@ -1133,8 +1133,8 @@ int cfg80211_wext_siwpower(struct net_device *dev,
11331133
if (err)
11341134
return err;
11351135

1136-
wdev->wext.ps = ps;
1137-
wdev->wext.ps_timeout = timeout;
1136+
wdev->ps = ps;
1137+
wdev->ps_timeout = timeout;
11381138

11391139
return 0;
11401140

@@ -1147,7 +1147,7 @@ int cfg80211_wext_giwpower(struct net_device *dev,
11471147
{
11481148
struct wireless_dev *wdev = dev->ieee80211_ptr;
11491149

1150-
wrq->disabled = !wdev->wext.ps;
1150+
wrq->disabled = !wdev->ps;
11511151

11521152
return 0;
11531153
}

0 commit comments

Comments
 (0)