^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Copyright (C) 2017 Rafał Miłecki <rafal@milecki.pl>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Permission to use, copy, modify, and/or distribute this software for any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * purpose with or without fee is hereby granted, provided that the above
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * copyright notice and this permission notice appear in all copies.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <net/cfg80211.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "core.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static bool wiphy_freq_limits_valid_chan(struct wiphy *wiphy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct ieee80211_freq_range *freq_limits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) unsigned int n_freq_limits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct ieee80211_channel *chan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) u32 bw = MHZ_TO_KHZ(20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) for (i = 0; i < n_freq_limits; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct ieee80211_freq_range *limit = &freq_limits[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (cfg80211_does_bw_fit_range(limit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) MHZ_TO_KHZ(chan->center_freq),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) bw))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static void wiphy_freq_limits_apply(struct wiphy *wiphy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct ieee80211_freq_range *freq_limits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) unsigned int n_freq_limits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) enum nl80211_band band;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (WARN_ON(!n_freq_limits))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) for (band = 0; band < NUM_NL80211_BANDS; band++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct ieee80211_supported_band *sband = wiphy->bands[band];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (!sband)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) for (i = 0; i < sband->n_channels; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct ieee80211_channel *chan = &sband->channels[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (chan->flags & IEEE80211_CHAN_DISABLED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (!wiphy_freq_limits_valid_chan(wiphy, freq_limits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) n_freq_limits,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) chan)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) pr_debug("Disabling freq %d MHz as it's out of OF limits\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) chan->center_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) chan->flags |= IEEE80211_CHAN_DISABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) void wiphy_read_of_freq_limits(struct wiphy *wiphy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct device *dev = wiphy_dev(wiphy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct device_node *np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct property *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct ieee80211_freq_range *freq_limits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) unsigned int n_freq_limits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) const __be32 *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) int len, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) np = dev_of_node(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (!np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) prop = of_find_property(np, "ieee80211-freq-limit", &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (!prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (!len || len % sizeof(u32) || len / sizeof(u32) % 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) dev_err(dev, "ieee80211-freq-limit wrong format");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) n_freq_limits = len / sizeof(u32) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) freq_limits = kcalloc(n_freq_limits, sizeof(*freq_limits), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (!freq_limits) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) goto out_kfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) p = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) for (i = 0; i < n_freq_limits; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct ieee80211_freq_range *limit = &freq_limits[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) p = of_prop_next_u32(prop, p, &limit->start_freq_khz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (!p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) goto out_kfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) p = of_prop_next_u32(prop, p, &limit->end_freq_khz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (!p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) goto out_kfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (!limit->start_freq_khz ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) !limit->end_freq_khz ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) limit->start_freq_khz >= limit->end_freq_khz) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) goto out_kfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) wiphy_freq_limits_apply(wiphy, freq_limits, n_freq_limits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) out_kfree:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) kfree(freq_limits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) dev_err(dev, "Failed to get limits: %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) EXPORT_SYMBOL(wiphy_read_of_freq_limits);