1 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
2 Date: Wed, 4 Jan 2017 18:58:31 +0100
3 Subject: [PATCH] cfg80211: support ieee80211-freq-limit DT property
5 Content-Type: text/plain; charset=UTF-8
6 Content-Transfer-Encoding: 8bit
8 This patch adds a helper for reading that new property and applying
9 limitations of supported channels specified this way.
10 It is used with devices that normally support a wide wireless band but
11 in a given config are limited to some part of it (usually due to board
12 design). For example a dual-band chipset may be able to support one band
13 only because of used antennas.
14 It's also common that tri-band routers have separated radios for lower
15 and higher part of 5 GHz band and it may be impossible to say which is
16 which without a DT info.
18 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
19 [add new function to documentation, fix link]
20 Signed-off-by: Johannes Berg <johannes.berg@intel.com>
22 create mode 100644 net/wireless/of.c
24 --- a/include/net/cfg80211.h
25 +++ b/include/net/cfg80211.h
26 @@ -311,6 +311,34 @@ struct ieee80211_supported_band {
27 struct ieee80211_sta_vht_cap vht_cap;
31 + * wiphy_read_of_freq_limits - read frequency limits from device tree
33 + * @wiphy: the wireless device to get extra limits for
35 + * Some devices may have extra limitations specified in DT. This may be useful
36 + * for chipsets that normally support more bands but are limited due to board
37 + * design (e.g. by antennas or external power amplifier).
39 + * This function reads info from DT and uses it to *modify* channels (disable
40 + * unavailable ones). It's usually a *bad* idea to use it in drivers with
41 + * shared channel data as DT limitations are device specific. You should make
42 + * sure to call it only if channels in wiphy are copied and can be modified
43 + * without affecting other devices.
45 + * As this function access device node it has to be called after set_wiphy_dev.
46 + * It also modifies channels so they have to be set first.
47 + * If using this helper, call it before wiphy_register().
50 +void wiphy_read_of_freq_limits(struct wiphy *wiphy);
51 +#else /* CONFIG_OF */
52 +static inline void wiphy_read_of_freq_limits(struct wiphy *wiphy)
55 +#endif /* !CONFIG_OF */
59 * Wireless hardware/device configuration structures and methods
61 --- a/net/wireless/Makefile
62 +++ b/net/wireless/Makefile
63 @@ -11,6 +11,7 @@ obj-$(CONFIG_WEXT_PRIV) += wext-priv.o
65 cfg80211-y += core.o sysfs.o radiotap.o util.o reg.o scan.o nl80211.o
66 cfg80211-y += mlme.o ibss.o sme.o chan.o ethtool.o mesh.o ap.o trace.o ocb.o
67 +cfg80211-$(CONFIG_OF) += of.o
68 cfg80211-$(CPTCFG_CFG80211_DEBUGFS) += debugfs.o
69 cfg80211-$(CPTCFG_CFG80211_WEXT) += wext-compat.o wext-sme.o
70 cfg80211-$(CPTCFG_CFG80211_INTERNAL_REGDB) += regdb.o
72 +++ b/net/wireless/of.c
75 + * Copyright (C) 2017 Rafał Miłecki <rafal@milecki.pl>
77 + * Permission to use, copy, modify, and/or distribute this software for any
78 + * purpose with or without fee is hereby granted, provided that the above
79 + * copyright notice and this permission notice appear in all copies.
81 + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
82 + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
83 + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
84 + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
85 + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
86 + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
87 + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
90 +#include <linux/of.h>
91 +#include <net/cfg80211.h>
94 +static bool wiphy_freq_limits_valid_chan(struct wiphy *wiphy,
95 + struct ieee80211_freq_range *freq_limits,
96 + unsigned int n_freq_limits,
97 + struct ieee80211_channel *chan)
99 + u32 bw = MHZ_TO_KHZ(20);
102 + for (i = 0; i < n_freq_limits; i++) {
103 + struct ieee80211_freq_range *limit = &freq_limits[i];
105 + if (cfg80211_does_bw_fit_range(limit,
106 + MHZ_TO_KHZ(chan->center_freq),
114 +static void wiphy_freq_limits_apply(struct wiphy *wiphy,
115 + struct ieee80211_freq_range *freq_limits,
116 + unsigned int n_freq_limits)
118 + enum nl80211_band band;
121 + if (WARN_ON(!n_freq_limits))
124 + for (band = 0; band < NUM_NL80211_BANDS; band++) {
125 + struct ieee80211_supported_band *sband = wiphy->bands[band];
130 + for (i = 0; i < sband->n_channels; i++) {
131 + struct ieee80211_channel *chan = &sband->channels[i];
133 + if (chan->flags & IEEE80211_CHAN_DISABLED)
136 + if (!wiphy_freq_limits_valid_chan(wiphy, freq_limits,
139 + pr_debug("Disabling freq %d MHz as it's out of OF limits\n",
140 + chan->center_freq);
141 + chan->flags |= IEEE80211_CHAN_DISABLED;
147 +void wiphy_read_of_freq_limits(struct wiphy *wiphy)
149 + struct device *dev = wiphy_dev(wiphy);
150 + struct device_node *np;
151 + struct property *prop;
152 + struct ieee80211_freq_range *freq_limits;
153 + unsigned int n_freq_limits;
160 + np = dev_of_node(dev);
164 + prop = of_find_property(np, "ieee80211-freq-limit", &len);
168 + if (!len || len % sizeof(u32) || len / sizeof(u32) % 2) {
169 + dev_err(dev, "ieee80211-freq-limit wrong format");
172 + n_freq_limits = len / sizeof(u32) / 2;
174 + freq_limits = kcalloc(n_freq_limits, sizeof(*freq_limits), GFP_KERNEL);
175 + if (!freq_limits) {
181 + for (i = 0; i < n_freq_limits; i++) {
182 + struct ieee80211_freq_range *limit = &freq_limits[i];
184 + p = of_prop_next_u32(prop, p, &limit->start_freq_khz);
190 + p = of_prop_next_u32(prop, p, &limit->end_freq_khz);
196 + if (!limit->start_freq_khz ||
197 + !limit->end_freq_khz ||
198 + limit->start_freq_khz >= limit->end_freq_khz) {
204 + wiphy_freq_limits_apply(wiphy, freq_limits, n_freq_limits);
207 + kfree(freq_limits);
209 + dev_err(dev, "Failed to get limits: %d\n", err);
211 +EXPORT_SYMBOL(wiphy_read_of_freq_limits);