Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / net / wireless / of.c
blobde221f0edca5ee9adca135ea9eda1b803fd483f9
1 /*
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.
17 #include <linux/of.h>
18 #include <net/cfg80211.h>
19 #include "core.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);
27 int i;
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),
34 bw))
35 return true;
38 return false;
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;
46 int i;
48 if (WARN_ON(!n_freq_limits))
49 return;
51 for (band = 0; band < NUM_NL80211_BANDS; band++) {
52 struct ieee80211_supported_band *sband = wiphy->bands[band];
54 if (!sband)
55 continue;
57 for (i = 0; i < sband->n_channels; i++) {
58 struct ieee80211_channel *chan = &sband->channels[i];
60 if (chan->flags & IEEE80211_CHAN_DISABLED)
61 continue;
63 if (!wiphy_freq_limits_valid_chan(wiphy, freq_limits,
64 n_freq_limits,
65 chan)) {
66 pr_debug("Disabling freq %d MHz as it's out of OF limits\n",
67 chan->center_freq);
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;
81 const __be32 *p;
82 int len, i;
83 int err = 0;
85 if (!dev)
86 return;
87 np = dev_of_node(dev);
88 if (!np)
89 return;
91 prop = of_find_property(np, "ieee80211-freq-limit", &len);
92 if (!prop)
93 return;
95 if (!len || len % sizeof(u32) || len / sizeof(u32) % 2) {
96 dev_err(dev, "ieee80211-freq-limit wrong format");
97 return;
99 n_freq_limits = len / sizeof(u32) / 2;
101 freq_limits = kcalloc(n_freq_limits, sizeof(*freq_limits), GFP_KERNEL);
102 if (!freq_limits) {
103 err = -ENOMEM;
104 goto out_kfree;
107 p = NULL;
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);
112 if (!p) {
113 err = -EINVAL;
114 goto out_kfree;
117 p = of_prop_next_u32(prop, p, &limit->end_freq_khz);
118 if (!p) {
119 err = -EINVAL;
120 goto out_kfree;
123 if (!limit->start_freq_khz ||
124 !limit->end_freq_khz ||
125 limit->start_freq_khz >= limit->end_freq_khz) {
126 err = -EINVAL;
127 goto out_kfree;
131 wiphy_freq_limits_apply(wiphy, freq_limits, n_freq_limits);
133 out_kfree:
134 kfree(freq_limits);
135 if (err)
136 dev_err(dev, "Failed to get limits: %d\n", err);
138 EXPORT_SYMBOL(wiphy_read_of_freq_limits);