2 * Copyright (C) 2017 Rafał Miłecki <rafal@milecki.pl>
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <net/cfg80211.h>
21 static bool wiphy_freq_limits_valid_chan(struct wiphy *wiphy,
22 struct ieee80211_freq_range *freq_limits,
23 unsigned int n_freq_limits,
24 struct ieee80211_channel *chan)
26 u32 bw = MHZ_TO_KHZ(20);
29 for (i = 0; i < n_freq_limits; i++) {
30 struct ieee80211_freq_range *limit = &freq_limits[i];
32 if (cfg80211_does_bw_fit_range(limit,
33 MHZ_TO_KHZ(chan->center_freq),
41 static void wiphy_freq_limits_apply(struct wiphy *wiphy,
42 struct ieee80211_freq_range *freq_limits,
43 unsigned int n_freq_limits)
45 enum nl80211_band band;
48 if (WARN_ON(!n_freq_limits))
51 for (band = 0; band < NUM_NL80211_BANDS; band++) {
52 struct ieee80211_supported_band *sband = wiphy->bands[band];
57 for (i = 0; i < sband->n_channels; i++) {
58 struct ieee80211_channel *chan = &sband->channels[i];
60 if (chan->flags & IEEE80211_CHAN_DISABLED)
63 if (!wiphy_freq_limits_valid_chan(wiphy, freq_limits,
66 pr_debug("Disabling freq %d MHz as it's out of OF limits\n",
68 chan->flags |= IEEE80211_CHAN_DISABLED;
74 void wiphy_read_of_freq_limits(struct wiphy *wiphy)
76 struct device *dev = wiphy_dev(wiphy);
77 struct device_node *np;
78 struct property *prop;
79 struct ieee80211_freq_range *freq_limits;
80 unsigned int n_freq_limits;
87 np = dev_of_node(dev);
91 prop = of_find_property(np, "ieee80211-freq-limit", &len);
95 if (!len || len % sizeof(u32) || len / sizeof(u32) % 2) {
96 dev_err(dev, "ieee80211-freq-limit wrong format");
99 n_freq_limits = len / sizeof(u32) / 2;
101 freq_limits = kcalloc(n_freq_limits, sizeof(*freq_limits), GFP_KERNEL);
108 for (i = 0; i < n_freq_limits; i++) {
109 struct ieee80211_freq_range *limit = &freq_limits[i];
111 p = of_prop_next_u32(prop, p, &limit->start_freq_khz);
117 p = of_prop_next_u32(prop, p, &limit->end_freq_khz);
123 if (!limit->start_freq_khz ||
124 !limit->end_freq_khz ||
125 limit->start_freq_khz >= limit->end_freq_khz) {
131 wiphy_freq_limits_apply(wiphy, freq_limits, n_freq_limits);
136 dev_err(dev, "Failed to get limits: %d\n", err);
138 EXPORT_SYMBOL(wiphy_read_of_freq_limits);