wifi: cfg80211: add helper for checking if a chandef is valid on a radio
authorFelix Fietkau <nbd@nbd.name>
Tue, 9 Jul 2024 08:38:32 +0000 (10:38 +0200)
committerJohannes Berg <johannes.berg@intel.com>
Tue, 9 Jul 2024 09:36:00 +0000 (11:36 +0200)
Check if the full channel width is in the radio's frequency range.

Signed-off-by: Felix Fietkau <nbd@nbd.name>
Link: https://patch.msgid.link/7c8ea146feb6f37cee62e5ba6be5370403695797.1720514221.git-series.nbd@nbd.name
[add missing Return: documentation]
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
include/net/cfg80211.h
net/wireless/util.c

index 4767e2c76b0180b5273eeb026bac4c21c08294cf..192d72c8b46544d332e718da728f32009cbf8a49 100644 (file)
@@ -6508,6 +6508,17 @@ static inline bool cfg80211_channel_is_psc(struct ieee80211_channel *chan)
        return ieee80211_frequency_to_channel(chan->center_freq) % 16 == 5;
 }
 
+/**
+ * cfg80211_radio_chandef_valid - Check if the radio supports the chandef
+ *
+ * @radio: wiphy radio
+ * @chandef: chandef for current channel
+ *
+ * Return: whether or not the given chandef is valid for the given radio
+ */
+bool cfg80211_radio_chandef_valid(const struct wiphy_radio *radio,
+                                 const struct cfg80211_chan_def *chandef);
+
 /**
  * ieee80211_get_response_rate - get basic rate for a given rate
  *
index 2492f259621fb387e4258454caac82de5b4c6695..9a7c3adc8a3bf4a9d8aaa8de324614d4153e610d 100644 (file)
@@ -2886,3 +2886,38 @@ cfg80211_get_iftype_ext_capa(struct wiphy *wiphy, enum nl80211_iftype type)
        return NULL;
 }
 EXPORT_SYMBOL(cfg80211_get_iftype_ext_capa);
+
+static bool
+ieee80211_radio_freq_range_valid(const struct wiphy_radio *radio,
+                                u32 freq, u32 width)
+{
+       const struct wiphy_radio_freq_range *r;
+       int i;
+
+       for (i = 0; i < radio->n_freq_range; i++) {
+               r = &radio->freq_range[i];
+               if (freq - width / 2 >= r->start_freq &&
+                   freq + width / 2 <= r->end_freq)
+                       return true;
+       }
+
+       return false;
+}
+
+bool cfg80211_radio_chandef_valid(const struct wiphy_radio *radio,
+                                 const struct cfg80211_chan_def *chandef)
+{
+       u32 freq, width;
+
+       freq = ieee80211_chandef_to_khz(chandef);
+       width = nl80211_chan_width_to_mhz(chandef->width);
+       if (!ieee80211_radio_freq_range_valid(radio, freq, width))
+               return false;
+
+       freq = MHZ_TO_KHZ(chandef->center_freq2);
+       if (freq && !ieee80211_radio_freq_range_valid(radio, freq, width))
+               return false;
+
+       return true;
+}
+EXPORT_SYMBOL(cfg80211_radio_chandef_valid);