[PATCH wireless-next 1/3] wifi: cfg80211: Add utility API to get radio index from channel

Raj Kumar Bhagat posted 3 patches 9 months ago
There is a newer version of this series
[PATCH wireless-next 1/3] wifi: cfg80211: Add utility API to get radio index from channel
Posted by Raj Kumar Bhagat 9 months ago
From: Vasanthakumar Thiagarajan <vasanthakumar.thiagarajan@oss.qualcomm.com>

Add utility API cfg80211_get_radio_idx_by_chan() to retrieve the radio
index corresponding to a given channel in a multi-radio wiphy.

This utility function can be used when we want to check the radio-specific
data for a channel in a multi-radio wiphy. For example, it can help
determine the radio index required to handle a scan request. This index
can then be used to decide whether the scan can proceed without
interfering with ongoing DFS operations on another radio.

Signed-off-by: Vasanthakumar Thiagarajan <vasanthakumar.thiagarajan@oss.qualcomm.com>
Co-developed-by: Raj Kumar Bhagat <quic_rajkbhag@quicinc.com>
Signed-off-by: Raj Kumar Bhagat <quic_rajkbhag@quicinc.com>
---
 include/net/cfg80211.h | 11 +++++++++++
 net/wireless/util.c    | 24 ++++++++++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/include/net/cfg80211.h b/include/net/cfg80211.h
index d1848dc8ec99df36592517b46d58223be9bee7e5..7719a90ab4d75045983ac8a63360fd90472cbb7d 100644
--- a/include/net/cfg80211.h
+++ b/include/net/cfg80211.h
@@ -9372,6 +9372,17 @@ int cfg80211_iter_combinations(struct wiphy *wiphy,
 			       void (*iter)(const struct ieee80211_iface_combination *c,
 					    void *data),
 			       void *data);
+/**
+ * cfg80211_get_radio_idx_by_chan - get the radio index by the channel
+ *
+ * @wiphy: the wiphy
+ * @chan: channel for which the supported radio index is required
+ *
+ * Return: radio index on success or a negative error code
+ */
+int cfg80211_get_radio_idx_by_chan(struct wiphy *wiphy,
+				   const struct ieee80211_channel *chan);
+
 
 /**
  * cfg80211_stop_iface - trigger interface disconnection
diff --git a/net/wireless/util.c b/net/wireless/util.c
index ed868c0f7ca8ed7a9e85e70c10a738f592eac1d6..8ba1d0b268fc350064b2b8adcfeae121c3dcab70 100644
--- a/net/wireless/util.c
+++ b/net/wireless/util.c
@@ -2516,6 +2516,30 @@ int cfg80211_check_combinations(struct wiphy *wiphy,
 }
 EXPORT_SYMBOL(cfg80211_check_combinations);
 
+int cfg80211_get_radio_idx_by_chan(struct wiphy *wiphy,
+				   const struct ieee80211_channel *chan)
+{
+	const struct wiphy_radio *radio;
+	int i, j;
+	u32 freq;
+
+	if (!chan)
+		return -EINVAL;
+
+	freq = ieee80211_channel_to_khz(chan);
+	for (i = 0; i < wiphy->n_radio; i++) {
+		radio = &wiphy->radio[i];
+		for (j = 0; j < radio->n_freq_range; j++) {
+			if (freq >= radio->freq_range[j].start_freq &&
+			    freq <= radio->freq_range[j].end_freq)
+				return i;
+		}
+	}
+
+	return -ENOENT;
+}
+EXPORT_SYMBOL(cfg80211_get_radio_idx_by_chan);
+
 int ieee80211_get_ratemask(struct ieee80211_supported_band *sband,
 			   const u8 *rates, unsigned int n_rates,
 			   u32 *mask)

-- 
2.34.1
Re: [PATCH wireless-next 1/3] wifi: cfg80211: Add utility API to get radio index from channel
Posted by Johannes Berg 8 months, 4 weeks ago
On Wed, 2025-05-14 at 16:58 +0530, Raj Kumar Bhagat wrote:
> 
> +int cfg80211_get_radio_idx_by_chan(struct wiphy *wiphy,
> +				   const struct ieee80211_channel *chan)
> +{
> +	const struct wiphy_radio *radio;
> +	int i, j;
> +	u32 freq;
> +
> +	if (!chan)
> +		return -EINVAL;
> +
> +	freq = ieee80211_channel_to_khz(chan);
> +	for (i = 0; i < wiphy->n_radio; i++) {
> +		radio = &wiphy->radio[i];
> +		for (j = 0; j < radio->n_freq_range; j++) {
> +			if (freq >= radio->freq_range[j].start_freq &&
> +			    freq <= radio->freq_range[j].end_freq)
> +				return i;
> 

I believe we also discussed this in the past elsewhere, but I don't
think the the >= and <= can simultaneously be wrong. If the frequency
ranges for radios are adjacent, then the intervals here need to be half
open. I _think_ it should be < instead of <=, and therefore a half-open
interval of "[start, end[" (or "[start, end)" depending on your
preferred notation.)

johannes
Re: [PATCH wireless-next 1/3] wifi: cfg80211: Add utility API to get radio index from channel
Posted by Raj Kumar Bhagat 8 months, 3 weeks ago
On 5/16/2025 1:21 PM, Johannes Berg wrote:
> On Wed, 2025-05-14 at 16:58 +0530, Raj Kumar Bhagat wrote:
>>
>> +int cfg80211_get_radio_idx_by_chan(struct wiphy *wiphy,
>> +				   const struct ieee80211_channel *chan)
>> +{
>> +	const struct wiphy_radio *radio;
>> +	int i, j;
>> +	u32 freq;
>> +
>> +	if (!chan)
>> +		return -EINVAL;
>> +
>> +	freq = ieee80211_channel_to_khz(chan);
>> +	for (i = 0; i < wiphy->n_radio; i++) {
>> +		radio = &wiphy->radio[i];
>> +		for (j = 0; j < radio->n_freq_range; j++) {
>> +			if (freq >= radio->freq_range[j].start_freq &&
>> +			    freq <= radio->freq_range[j].end_freq)
>> +				return i;
>>
> 
> I believe we also discussed this in the past elsewhere, but I don't
> think the the >= and <= can simultaneously be wrong. If the frequency
> ranges for radios are adjacent, then the intervals here need to be half
> open. I _think_ it should be < instead of <=, and therefore a half-open
> interval of "[start, end[" (or "[start, end)" depending on your
> preferred notation.)
> 

Sure, will use half-open interval ([start, end[) in next version.
Re: [PATCH wireless-next 1/3] wifi: cfg80211: Add utility API to get radio index from channel
Posted by Johannes Berg 8 months, 4 weeks ago
On Fri, 2025-05-16 at 09:51 +0200, Johannes Berg wrote:
> On Wed, 2025-05-14 at 16:58 +0530, Raj Kumar Bhagat wrote:
> > 
> > +int cfg80211_get_radio_idx_by_chan(struct wiphy *wiphy,
> > +				   const struct ieee80211_channel *chan)
> > +{
> > +	const struct wiphy_radio *radio;
> > +	int i, j;
> > +	u32 freq;
> > +
> > +	if (!chan)
> > +		return -EINVAL;
> > +
> > +	freq = ieee80211_channel_to_khz(chan);
> > +	for (i = 0; i < wiphy->n_radio; i++) {
> > +		radio = &wiphy->radio[i];
> > +		for (j = 0; j < radio->n_freq_range; j++) {
> > +			if (freq >= radio->freq_range[j].start_freq &&
> > +			    freq <= radio->freq_range[j].end_freq)
> > +				return i;
> > 
> 
> I believe we also discussed this in the past elsewhere, but I don't
> think the the >= and <= can simultaneously be wrong. If the frequency

"simultaneously be correct". I meant "correct", not "wrong"...

johannes