diff mbox series

[4/7] wiphy: add wiphy_check_{frequency,band}

Message ID 20221213203624.1423277-4-prestwoj@gmail.com (mailing list archive)
State New
Headers show
Series [1/7] band: introduce new method of tracking frequencies | expand

Checks

Context Check Description
tedd_an/pre-ci_am success Success
prestwoj/iwd-ci-gitlint success GitLint

Commit Message

James Prestwood Dec. 13, 2022, 8:36 p.m. UTC
Add two new APIs as a replacement for wiphy_get_disabled_freqs().

wiphy_check_frequency() serves as a way to check a single frequency
for certain attributes, currently: supported, disabled, and no-IR.

wiphy_check_band() similarly checks the entire band. This is mainly
targeted at station which needs to check if a given band is disabled
and is only meant to be used with the supported and disabled flags.
---
 src/wiphy.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/wiphy.h |  4 +++
 2 files changed, 93 insertions(+)
diff mbox series

Patch

diff --git a/src/wiphy.c b/src/wiphy.c
index c8ef5937..f5b7c05e 100644
--- a/src/wiphy.c
+++ b/src/wiphy.c
@@ -496,6 +496,95 @@  const struct scan_freq_set *wiphy_get_disabled_freqs(const struct wiphy *wiphy)
 	return wiphy->disabled_freqs;
 }
 
+static struct band *wiphy_get_band(const struct wiphy *wiphy, enum band_freq band)
+{
+	switch (band) {
+	case BAND_FREQ_2_4_GHZ:
+		return wiphy->band_2g;
+	case BAND_FREQ_5_GHZ:
+		return wiphy->band_5g;
+	case BAND_FREQ_6_GHZ:
+		return wiphy->band_6g;
+	default:
+		return NULL;
+	}
+}
+
+bool wiphy_check_frequency(const struct wiphy *wiphy, uint32_t freq,
+				uint16_t flags)
+{
+	enum band_freq band;
+	struct band *bandp;
+	uint8_t channel;
+	uint16_t mask;
+
+	channel = band_freq_to_channel(freq, &band);
+	if (!channel)
+		return false;
+
+	bandp = wiphy_get_band(wiphy, band);
+	if (!bandp)
+		return false;
+
+	if (L_WARN_ON(channel > bandp->freqs_len))
+		return false;
+
+
+	mask = bandp->frequencies[channel];
+
+	if ((flags & mask) != flags)
+		return false;
+
+	return true;
+}
+
+bool wiphy_check_band(const struct wiphy *wiphy, enum band_freq band,
+				uint16_t flags)
+{
+	unsigned int i;
+	bool supported = false;
+	bool disabled = true;
+	bool ret = false;
+	struct band *bandp = wiphy_get_band(wiphy, band);
+
+	/*
+	 * Caller should either include the SUPPORTED flag, or verify support
+	 * before calling otherwise this return could be misleading i.e.
+	 * checking a band for only DISABLED that isn't supported would
+	 * return false which could be interpreted that the band is enabled.
+	 */
+	if ((flags & BAND_FREQ_ATTR_SUPPORTED) && !bandp)
+		return false;
+
+	if (L_WARN_ON(!bandp))
+		return false;
+
+	/*
+	 * This should only be used with SUPPORTED/DISABLED flags. For supported
+	 * only ONE frequency needs to be supported. And for DISABLED ALL
+	 * frequencies must be disabled.
+	 */
+	for (i = 0; i < bandp->freqs_len; i++) {
+		uint16_t mask = bandp->frequencies[i];
+
+		if (!(mask & BAND_FREQ_ATTR_SUPPORTED))
+			continue;
+
+		supported = true;
+
+		if (!(mask & BAND_FREQ_ATTR_DISABLED))
+			disabled = false;
+	}
+
+	if (supported && (flags & BAND_FREQ_ATTR_SUPPORTED))
+		ret = true;
+
+	if (disabled && (flags & BAND_FREQ_ATTR_DISABLED))
+		ret &= true;
+
+	return ret;
+}
+
 bool wiphy_supports_probe_resp_offload(struct wiphy *wiphy)
 {
 	return wiphy->ap_probe_resp_offload;
diff --git a/src/wiphy.h b/src/wiphy.h
index 410105dd..469ccdc8 100644
--- a/src/wiphy.h
+++ b/src/wiphy.h
@@ -100,6 +100,10 @@  uint32_t wiphy_get_supported_bands(struct wiphy *wiphy);
 const struct scan_freq_set *wiphy_get_supported_freqs(
 						const struct wiphy *wiphy);
 const struct scan_freq_set *wiphy_get_disabled_freqs(const struct wiphy *wiphy);
+bool wiphy_check_frequency(const struct wiphy *wiphy, uint32_t freq,
+				uint16_t flags);
+bool wiphy_check_band(const struct wiphy *wiphy, enum band_freq band,
+				uint16_t flags);
 bool wiphy_supports_probe_resp_offload(struct wiphy *wiphy);
 bool wiphy_can_transition_disable(struct wiphy *wiphy);
 bool wiphy_can_offload(struct wiphy *wiphy);