mac80211: backport cfg80211 support for ieee80211-freq-limit DT property
[openwrt/staging/lynxis/omap.git] / package / kernel / mac80211 / patches / 347-0002-cfg80211-support-ieee80211-freq-limit-DT-property.patch
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
4 MIME-Version: 1.0
5 Content-Type: text/plain; charset=UTF-8
6 Content-Transfer-Encoding: 8bit
7
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.
17
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>
21 ---
22 create mode 100644 net/wireless/of.c
23
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;
28 };
29
30 +/**
31 + * wiphy_read_of_freq_limits - read frequency limits from device tree
32 + *
33 + * @wiphy: the wireless device to get extra limits for
34 + *
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).
38 + *
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.
44 + *
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().
48 + */
49 +#ifdef CONFIG_OF
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)
53 +{
54 +}
55 +#endif /* !CONFIG_OF */
56 +
57 +
58 /*
59 * Wireless hardware/device configuration structures and methods
60 */
61 --- a/net/wireless/Makefile
62 +++ b/net/wireless/Makefile
63 @@ -11,6 +11,7 @@ obj-$(CONFIG_WEXT_PRIV) += wext-priv.o
64
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
71 --- /dev/null
72 +++ b/net/wireless/of.c
73 @@ -0,0 +1,138 @@
74 +/*
75 + * Copyright (C) 2017 Rafał Miłecki <rafal@milecki.pl>
76 + *
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.
80 + *
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.
88 + */
89 +
90 +#include <linux/of.h>
91 +#include <net/cfg80211.h>
92 +#include "core.h"
93 +
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)
98 +{
99 + u32 bw = MHZ_TO_KHZ(20);
100 + int i;
101 +
102 + for (i = 0; i < n_freq_limits; i++) {
103 + struct ieee80211_freq_range *limit = &freq_limits[i];
104 +
105 + if (cfg80211_does_bw_fit_range(limit,
106 + MHZ_TO_KHZ(chan->center_freq),
107 + bw))
108 + return true;
109 + }
110 +
111 + return false;
112 +}
113 +
114 +static void wiphy_freq_limits_apply(struct wiphy *wiphy,
115 + struct ieee80211_freq_range *freq_limits,
116 + unsigned int n_freq_limits)
117 +{
118 + enum nl80211_band band;
119 + int i;
120 +
121 + if (WARN_ON(!n_freq_limits))
122 + return;
123 +
124 + for (band = 0; band < NUM_NL80211_BANDS; band++) {
125 + struct ieee80211_supported_band *sband = wiphy->bands[band];
126 +
127 + if (!sband)
128 + continue;
129 +
130 + for (i = 0; i < sband->n_channels; i++) {
131 + struct ieee80211_channel *chan = &sband->channels[i];
132 +
133 + if (chan->flags & IEEE80211_CHAN_DISABLED)
134 + continue;
135 +
136 + if (!wiphy_freq_limits_valid_chan(wiphy, freq_limits,
137 + n_freq_limits,
138 + chan)) {
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;
142 + }
143 + }
144 + }
145 +}
146 +
147 +void wiphy_read_of_freq_limits(struct wiphy *wiphy)
148 +{
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;
154 + const __be32 *p;
155 + int len, i;
156 + int err = 0;
157 +
158 + if (!dev)
159 + return;
160 + np = dev_of_node(dev);
161 + if (!np)
162 + return;
163 +
164 + prop = of_find_property(np, "ieee80211-freq-limit", &len);
165 + if (!prop)
166 + return;
167 +
168 + if (!len || len % sizeof(u32) || len / sizeof(u32) % 2) {
169 + dev_err(dev, "ieee80211-freq-limit wrong format");
170 + return;
171 + }
172 + n_freq_limits = len / sizeof(u32) / 2;
173 +
174 + freq_limits = kcalloc(n_freq_limits, sizeof(*freq_limits), GFP_KERNEL);
175 + if (!freq_limits) {
176 + err = -ENOMEM;
177 + goto out_kfree;
178 + }
179 +
180 + p = NULL;
181 + for (i = 0; i < n_freq_limits; i++) {
182 + struct ieee80211_freq_range *limit = &freq_limits[i];
183 +
184 + p = of_prop_next_u32(prop, p, &limit->start_freq_khz);
185 + if (!p) {
186 + err = -EINVAL;
187 + goto out_kfree;
188 + }
189 +
190 + p = of_prop_next_u32(prop, p, &limit->end_freq_khz);
191 + if (!p) {
192 + err = -EINVAL;
193 + goto out_kfree;
194 + }
195 +
196 + if (!limit->start_freq_khz ||
197 + !limit->end_freq_khz ||
198 + limit->start_freq_khz >= limit->end_freq_khz) {
199 + err = -EINVAL;
200 + goto out_kfree;
201 + }
202 + }
203 +
204 + wiphy_freq_limits_apply(wiphy, freq_limits, n_freq_limits);
205 +
206 +out_kfree:
207 + kfree(freq_limits);
208 + if (err)
209 + dev_err(dev, "Failed to get limits: %d\n", err);
210 +}
211 +EXPORT_SYMBOL(wiphy_read_of_freq_limits);