xref: /freebsd/contrib/wpa/src/common/hw_features_common.c (revision c1d255d3ffdbe447de3ab875bf4e7d7accc5bfc5)
15b9c547cSRui Paulo /*
25b9c547cSRui Paulo  * Common hostapd/wpa_supplicant HW features
35b9c547cSRui Paulo  * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
45b9c547cSRui Paulo  * Copyright (c) 2015, Qualcomm Atheros, Inc.
55b9c547cSRui Paulo  *
65b9c547cSRui Paulo  * This software may be distributed under the terms of the BSD license.
75b9c547cSRui Paulo  * See README for more details.
85b9c547cSRui Paulo  */
95b9c547cSRui Paulo 
105b9c547cSRui Paulo #include "includes.h"
115b9c547cSRui Paulo 
125b9c547cSRui Paulo #include "common.h"
135b9c547cSRui Paulo #include "defs.h"
145b9c547cSRui Paulo #include "ieee802_11_defs.h"
155b9c547cSRui Paulo #include "ieee802_11_common.h"
165b9c547cSRui Paulo #include "hw_features_common.h"
175b9c547cSRui Paulo 
185b9c547cSRui Paulo 
195b9c547cSRui Paulo struct hostapd_channel_data * hw_get_channel_chan(struct hostapd_hw_modes *mode,
205b9c547cSRui Paulo 						  int chan, int *freq)
215b9c547cSRui Paulo {
225b9c547cSRui Paulo 	int i;
235b9c547cSRui Paulo 
245b9c547cSRui Paulo 	if (freq)
255b9c547cSRui Paulo 		*freq = 0;
265b9c547cSRui Paulo 
275b9c547cSRui Paulo 	if (!mode)
285b9c547cSRui Paulo 		return NULL;
295b9c547cSRui Paulo 
305b9c547cSRui Paulo 	for (i = 0; i < mode->num_channels; i++) {
315b9c547cSRui Paulo 		struct hostapd_channel_data *ch = &mode->channels[i];
325b9c547cSRui Paulo 		if (ch->chan == chan) {
335b9c547cSRui Paulo 			if (freq)
345b9c547cSRui Paulo 				*freq = ch->freq;
355b9c547cSRui Paulo 			return ch;
365b9c547cSRui Paulo 		}
375b9c547cSRui Paulo 	}
385b9c547cSRui Paulo 
395b9c547cSRui Paulo 	return NULL;
405b9c547cSRui Paulo }
415b9c547cSRui Paulo 
425b9c547cSRui Paulo 
43*c1d255d3SCy Schubert struct hostapd_channel_data *
44*c1d255d3SCy Schubert hw_mode_get_channel(struct hostapd_hw_modes *mode, int freq, int *chan)
455b9c547cSRui Paulo {
465b9c547cSRui Paulo 	int i;
475b9c547cSRui Paulo 
485b9c547cSRui Paulo 	for (i = 0; i < mode->num_channels; i++) {
495b9c547cSRui Paulo 		struct hostapd_channel_data *ch = &mode->channels[i];
50*c1d255d3SCy Schubert 
515b9c547cSRui Paulo 		if (ch->freq == freq) {
525b9c547cSRui Paulo 			if (chan)
535b9c547cSRui Paulo 				*chan = ch->chan;
545b9c547cSRui Paulo 			return ch;
555b9c547cSRui Paulo 		}
565b9c547cSRui Paulo 	}
575b9c547cSRui Paulo 
585b9c547cSRui Paulo 	return NULL;
595b9c547cSRui Paulo }
605b9c547cSRui Paulo 
615b9c547cSRui Paulo 
62*c1d255d3SCy Schubert struct hostapd_channel_data *
63*c1d255d3SCy Schubert hw_get_channel_freq(enum hostapd_hw_mode mode, int freq, int *chan,
64*c1d255d3SCy Schubert 		    struct hostapd_hw_modes *hw_features, int num_hw_features)
65*c1d255d3SCy Schubert {
66*c1d255d3SCy Schubert 	struct hostapd_channel_data *chan_data;
67*c1d255d3SCy Schubert 	int i;
68*c1d255d3SCy Schubert 
69*c1d255d3SCy Schubert 	if (chan)
70*c1d255d3SCy Schubert 		*chan = 0;
71*c1d255d3SCy Schubert 
72*c1d255d3SCy Schubert 	if (!hw_features)
73*c1d255d3SCy Schubert 		return NULL;
74*c1d255d3SCy Schubert 
75*c1d255d3SCy Schubert 	for (i = 0; i < num_hw_features; i++) {
76*c1d255d3SCy Schubert 		struct hostapd_hw_modes *curr_mode = &hw_features[i];
77*c1d255d3SCy Schubert 
78*c1d255d3SCy Schubert 		if (curr_mode->mode != mode)
79*c1d255d3SCy Schubert 			continue;
80*c1d255d3SCy Schubert 
81*c1d255d3SCy Schubert 		chan_data = hw_mode_get_channel(curr_mode, freq, chan);
82*c1d255d3SCy Schubert 		if (chan_data)
83*c1d255d3SCy Schubert 			return chan_data;
84*c1d255d3SCy Schubert 	}
85*c1d255d3SCy Schubert 
86*c1d255d3SCy Schubert 	return NULL;
87*c1d255d3SCy Schubert }
88*c1d255d3SCy Schubert 
89*c1d255d3SCy Schubert 
905b9c547cSRui Paulo int hw_get_freq(struct hostapd_hw_modes *mode, int chan)
915b9c547cSRui Paulo {
925b9c547cSRui Paulo 	int freq;
935b9c547cSRui Paulo 
945b9c547cSRui Paulo 	hw_get_channel_chan(mode, chan, &freq);
955b9c547cSRui Paulo 
965b9c547cSRui Paulo 	return freq;
975b9c547cSRui Paulo }
985b9c547cSRui Paulo 
995b9c547cSRui Paulo 
100*c1d255d3SCy Schubert int hw_get_chan(enum hostapd_hw_mode mode, int freq,
101*c1d255d3SCy Schubert 		struct hostapd_hw_modes *hw_features, int num_hw_features)
1025b9c547cSRui Paulo {
1035b9c547cSRui Paulo 	int chan;
1045b9c547cSRui Paulo 
105*c1d255d3SCy Schubert 	hw_get_channel_freq(mode, freq, &chan, hw_features, num_hw_features);
1065b9c547cSRui Paulo 
1075b9c547cSRui Paulo 	return chan;
1085b9c547cSRui Paulo }
1095b9c547cSRui Paulo 
1105b9c547cSRui Paulo 
111*c1d255d3SCy Schubert int allowed_ht40_channel_pair(enum hostapd_hw_mode mode,
112*c1d255d3SCy Schubert 			      struct hostapd_channel_data *p_chan,
113*c1d255d3SCy Schubert 			      struct hostapd_channel_data *s_chan)
1145b9c547cSRui Paulo {
1154bc52338SCy Schubert 	int ok, first;
116325151a3SRui Paulo 	int allowed[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 140,
11785732ac8SCy Schubert 			  149, 157, 165, 184, 192 };
1185b9c547cSRui Paulo 	size_t k;
119*c1d255d3SCy Schubert 	int ht40_plus, pri_chan, sec_chan;
1205b9c547cSRui Paulo 
121*c1d255d3SCy Schubert 	if (!p_chan || !s_chan)
1224bc52338SCy Schubert 		return 0;
123*c1d255d3SCy Schubert 	pri_chan = p_chan->chan;
124*c1d255d3SCy Schubert 	sec_chan = s_chan->chan;
125*c1d255d3SCy Schubert 
126*c1d255d3SCy Schubert 	ht40_plus = pri_chan < sec_chan;
1274bc52338SCy Schubert 
1284bc52338SCy Schubert 	if (pri_chan == sec_chan || !sec_chan) {
1294bc52338SCy Schubert 		if (chan_pri_allowed(p_chan))
1305b9c547cSRui Paulo 			return 1; /* HT40 not used */
1315b9c547cSRui Paulo 
1324bc52338SCy Schubert 		wpa_printf(MSG_ERROR, "Channel %d is not allowed as primary",
1334bc52338SCy Schubert 			   pri_chan);
1344bc52338SCy Schubert 		return 0;
1354bc52338SCy Schubert 	}
1364bc52338SCy Schubert 
1375b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG,
138*c1d255d3SCy Schubert 		   "HT40: control channel: %d (%d MHz), secondary channel: %d (%d MHz)",
139*c1d255d3SCy Schubert 		   pri_chan, p_chan->freq, sec_chan, s_chan->freq);
1405b9c547cSRui Paulo 
1415b9c547cSRui Paulo 	/* Verify that HT40 secondary channel is an allowed 20 MHz
1425b9c547cSRui Paulo 	 * channel */
1434bc52338SCy Schubert 	if ((s_chan->flag & HOSTAPD_CHAN_DISABLED) ||
1444bc52338SCy Schubert 	    (ht40_plus && !(p_chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40P)) ||
1454bc52338SCy Schubert 	    (!ht40_plus && !(p_chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40M))) {
1465b9c547cSRui Paulo 		wpa_printf(MSG_ERROR, "HT40 secondary channel %d not allowed",
1475b9c547cSRui Paulo 			   sec_chan);
1485b9c547cSRui Paulo 		return 0;
1495b9c547cSRui Paulo 	}
1505b9c547cSRui Paulo 
1515b9c547cSRui Paulo 	/*
1525b9c547cSRui Paulo 	 * Verify that HT40 primary,secondary channel pair is allowed per
1535b9c547cSRui Paulo 	 * IEEE 802.11n Annex J. This is only needed for 5 GHz band since
1545b9c547cSRui Paulo 	 * 2.4 GHz rules allow all cases where the secondary channel fits into
1555b9c547cSRui Paulo 	 * the list of allowed channels (already checked above).
1565b9c547cSRui Paulo 	 */
157*c1d255d3SCy Schubert 	if (mode != HOSTAPD_MODE_IEEE80211A)
1585b9c547cSRui Paulo 		return 1;
1595b9c547cSRui Paulo 
1605b9c547cSRui Paulo 	first = pri_chan < sec_chan ? pri_chan : sec_chan;
1615b9c547cSRui Paulo 
1625b9c547cSRui Paulo 	ok = 0;
1635b9c547cSRui Paulo 	for (k = 0; k < ARRAY_SIZE(allowed); k++) {
1645b9c547cSRui Paulo 		if (first == allowed[k]) {
1655b9c547cSRui Paulo 			ok = 1;
1665b9c547cSRui Paulo 			break;
1675b9c547cSRui Paulo 		}
1685b9c547cSRui Paulo 	}
1695b9c547cSRui Paulo 	if (!ok) {
1705b9c547cSRui Paulo 		wpa_printf(MSG_ERROR, "HT40 channel pair (%d, %d) not allowed",
1715b9c547cSRui Paulo 			   pri_chan, sec_chan);
1725b9c547cSRui Paulo 		return 0;
1735b9c547cSRui Paulo 	}
1745b9c547cSRui Paulo 
1755b9c547cSRui Paulo 	return 1;
1765b9c547cSRui Paulo }
1775b9c547cSRui Paulo 
1785b9c547cSRui Paulo 
1795b9c547cSRui Paulo void get_pri_sec_chan(struct wpa_scan_res *bss, int *pri_chan, int *sec_chan)
1805b9c547cSRui Paulo {
1815b9c547cSRui Paulo 	struct ieee80211_ht_operation *oper;
1825b9c547cSRui Paulo 	struct ieee802_11_elems elems;
1835b9c547cSRui Paulo 
1845b9c547cSRui Paulo 	*pri_chan = *sec_chan = 0;
1855b9c547cSRui Paulo 
1865b9c547cSRui Paulo 	ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len, &elems, 0);
187325151a3SRui Paulo 	if (elems.ht_operation) {
1885b9c547cSRui Paulo 		oper = (struct ieee80211_ht_operation *) elems.ht_operation;
1895b9c547cSRui Paulo 		*pri_chan = oper->primary_chan;
1905b9c547cSRui Paulo 		if (oper->ht_param & HT_INFO_HT_PARAM_STA_CHNL_WIDTH) {
1915b9c547cSRui Paulo 			int sec = oper->ht_param &
1925b9c547cSRui Paulo 				HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
1935b9c547cSRui Paulo 			if (sec == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
1945b9c547cSRui Paulo 				*sec_chan = *pri_chan + 4;
1955b9c547cSRui Paulo 			else if (sec == HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
1965b9c547cSRui Paulo 				*sec_chan = *pri_chan - 4;
1975b9c547cSRui Paulo 		}
1985b9c547cSRui Paulo 	}
1995b9c547cSRui Paulo }
2005b9c547cSRui Paulo 
2015b9c547cSRui Paulo 
202*c1d255d3SCy Schubert int check_40mhz_5g(struct wpa_scan_results *scan_res,
203*c1d255d3SCy Schubert 		   struct hostapd_channel_data *pri_chan,
204*c1d255d3SCy Schubert 		   struct hostapd_channel_data *sec_chan)
2055b9c547cSRui Paulo {
206*c1d255d3SCy Schubert 	int pri_bss, sec_bss;
2075b9c547cSRui Paulo 	int bss_pri_chan, bss_sec_chan;
2085b9c547cSRui Paulo 	size_t i;
2095b9c547cSRui Paulo 	int match;
2105b9c547cSRui Paulo 
211*c1d255d3SCy Schubert 	if (!scan_res || !pri_chan || !sec_chan ||
212*c1d255d3SCy Schubert 	    pri_chan->freq == sec_chan->freq)
2135b9c547cSRui Paulo 		return 0;
2145b9c547cSRui Paulo 
2155b9c547cSRui Paulo 	/*
2165b9c547cSRui Paulo 	 * Switch PRI/SEC channels if Beacons were detected on selected SEC
2175b9c547cSRui Paulo 	 * channel, but not on selected PRI channel.
2185b9c547cSRui Paulo 	 */
2195b9c547cSRui Paulo 	pri_bss = sec_bss = 0;
2205b9c547cSRui Paulo 	for (i = 0; i < scan_res->num; i++) {
2215b9c547cSRui Paulo 		struct wpa_scan_res *bss = scan_res->res[i];
222*c1d255d3SCy Schubert 		if (bss->freq == pri_chan->freq)
2235b9c547cSRui Paulo 			pri_bss++;
224*c1d255d3SCy Schubert 		else if (bss->freq == sec_chan->freq)
2255b9c547cSRui Paulo 			sec_bss++;
2265b9c547cSRui Paulo 	}
2275b9c547cSRui Paulo 	if (sec_bss && !pri_bss) {
2285b9c547cSRui Paulo 		wpa_printf(MSG_INFO,
2295b9c547cSRui Paulo 			   "Switch own primary and secondary channel to get secondary channel with no Beacons from other BSSes");
2305b9c547cSRui Paulo 		return 2;
2315b9c547cSRui Paulo 	}
2325b9c547cSRui Paulo 
2335b9c547cSRui Paulo 	/*
2345b9c547cSRui Paulo 	 * Match PRI/SEC channel with any existing HT40 BSS on the same
2355b9c547cSRui Paulo 	 * channels that we are about to use (if already mixed order in
2365b9c547cSRui Paulo 	 * existing BSSes, use own preference).
2375b9c547cSRui Paulo 	 */
2385b9c547cSRui Paulo 	match = 0;
2395b9c547cSRui Paulo 	for (i = 0; i < scan_res->num; i++) {
2405b9c547cSRui Paulo 		struct wpa_scan_res *bss = scan_res->res[i];
2415b9c547cSRui Paulo 		get_pri_sec_chan(bss, &bss_pri_chan, &bss_sec_chan);
242*c1d255d3SCy Schubert 		if (pri_chan->chan == bss_pri_chan &&
243*c1d255d3SCy Schubert 		    sec_chan->chan == bss_sec_chan) {
2445b9c547cSRui Paulo 			match = 1;
2455b9c547cSRui Paulo 			break;
2465b9c547cSRui Paulo 		}
2475b9c547cSRui Paulo 	}
2485b9c547cSRui Paulo 	if (!match) {
2495b9c547cSRui Paulo 		for (i = 0; i < scan_res->num; i++) {
2505b9c547cSRui Paulo 			struct wpa_scan_res *bss = scan_res->res[i];
2515b9c547cSRui Paulo 			get_pri_sec_chan(bss, &bss_pri_chan, &bss_sec_chan);
252*c1d255d3SCy Schubert 			if (pri_chan->chan == bss_sec_chan &&
253*c1d255d3SCy Schubert 			    sec_chan->chan == bss_pri_chan) {
2545b9c547cSRui Paulo 				wpa_printf(MSG_INFO, "Switch own primary and "
2555b9c547cSRui Paulo 					   "secondary channel due to BSS "
2565b9c547cSRui Paulo 					   "overlap with " MACSTR,
2575b9c547cSRui Paulo 					   MAC2STR(bss->bssid));
2585b9c547cSRui Paulo 				return 2;
2595b9c547cSRui Paulo 			}
2605b9c547cSRui Paulo 		}
2615b9c547cSRui Paulo 	}
2625b9c547cSRui Paulo 
2635b9c547cSRui Paulo 	return 1;
2645b9c547cSRui Paulo }
2655b9c547cSRui Paulo 
2665b9c547cSRui Paulo 
267325151a3SRui Paulo static int check_20mhz_bss(struct wpa_scan_res *bss, int pri_freq, int start,
268325151a3SRui Paulo 			   int end)
2695b9c547cSRui Paulo {
2705b9c547cSRui Paulo 	struct ieee802_11_elems elems;
2715b9c547cSRui Paulo 	struct ieee80211_ht_operation *oper;
2725b9c547cSRui Paulo 
2735b9c547cSRui Paulo 	if (bss->freq < start || bss->freq > end || bss->freq == pri_freq)
2745b9c547cSRui Paulo 		return 0;
2755b9c547cSRui Paulo 
2765b9c547cSRui Paulo 	ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len, &elems, 0);
2775b9c547cSRui Paulo 	if (!elems.ht_capabilities) {
2785b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Found overlapping legacy BSS: "
2795b9c547cSRui Paulo 			   MACSTR " freq=%d", MAC2STR(bss->bssid), bss->freq);
2805b9c547cSRui Paulo 		return 1;
2815b9c547cSRui Paulo 	}
2825b9c547cSRui Paulo 
283325151a3SRui Paulo 	if (elems.ht_operation) {
2845b9c547cSRui Paulo 		oper = (struct ieee80211_ht_operation *) elems.ht_operation;
2855b9c547cSRui Paulo 		if (oper->ht_param & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)
2865b9c547cSRui Paulo 			return 0;
2875b9c547cSRui Paulo 
2885b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Found overlapping 20 MHz HT BSS: "
2895b9c547cSRui Paulo 			   MACSTR " freq=%d", MAC2STR(bss->bssid), bss->freq);
2905b9c547cSRui Paulo 		return 1;
2915b9c547cSRui Paulo 	}
2925b9c547cSRui Paulo 	return 0;
2935b9c547cSRui Paulo }
2945b9c547cSRui Paulo 
2955b9c547cSRui Paulo 
296*c1d255d3SCy Schubert /*
297*c1d255d3SCy Schubert  * Returns:
298*c1d255d3SCy Schubert  * 0: no impact
299*c1d255d3SCy Schubert  * 1: overlapping BSS
300*c1d255d3SCy Schubert  * 2: overlapping BSS with 40 MHz intolerant advertisement
301*c1d255d3SCy Schubert  */
302*c1d255d3SCy Schubert int check_bss_coex_40mhz(struct wpa_scan_res *bss, int pri_freq, int sec_freq)
3035b9c547cSRui Paulo {
3045b9c547cSRui Paulo 	int affected_start, affected_end;
305*c1d255d3SCy Schubert 	struct ieee802_11_elems elems;
306*c1d255d3SCy Schubert 	int pri_chan, sec_chan;
307*c1d255d3SCy Schubert 	int pri = bss->freq;
308*c1d255d3SCy Schubert 	int sec = pri;
3095b9c547cSRui Paulo 
310*c1d255d3SCy Schubert 	if (pri_freq == sec_freq)
311*c1d255d3SCy Schubert 		return 1;
3125b9c547cSRui Paulo 
3135b9c547cSRui Paulo 	affected_start = (pri_freq + sec_freq) / 2 - 25;
3145b9c547cSRui Paulo 	affected_end = (pri_freq + sec_freq) / 2 + 25;
3155b9c547cSRui Paulo 
3165b9c547cSRui Paulo 	/* Check for overlapping 20 MHz BSS */
317*c1d255d3SCy Schubert 	if (check_20mhz_bss(bss, pri_freq, affected_start, affected_end)) {
318*c1d255d3SCy Schubert 		wpa_printf(MSG_DEBUG, "Overlapping 20 MHz BSS is found");
319*c1d255d3SCy Schubert 		return 1;
3205b9c547cSRui Paulo 	}
3215b9c547cSRui Paulo 
3225b9c547cSRui Paulo 	get_pri_sec_chan(bss, &pri_chan, &sec_chan);
3235b9c547cSRui Paulo 
3245b9c547cSRui Paulo 	if (sec_chan) {
3255b9c547cSRui Paulo 		if (sec_chan < pri_chan)
3265b9c547cSRui Paulo 			sec = pri - 20;
3275b9c547cSRui Paulo 		else
3285b9c547cSRui Paulo 			sec = pri + 20;
3295b9c547cSRui Paulo 	}
3305b9c547cSRui Paulo 
3315b9c547cSRui Paulo 	if ((pri < affected_start || pri > affected_end) &&
3325b9c547cSRui Paulo 	    (sec < affected_start || sec > affected_end))
333*c1d255d3SCy Schubert 		return 0; /* not within affected channel range */
3345b9c547cSRui Paulo 
3355b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "Neighboring BSS: " MACSTR
3365b9c547cSRui Paulo 		   " freq=%d pri=%d sec=%d",
3375b9c547cSRui Paulo 		   MAC2STR(bss->bssid), bss->freq, pri_chan, sec_chan);
3385b9c547cSRui Paulo 
3395b9c547cSRui Paulo 	if (sec_chan) {
3405b9c547cSRui Paulo 		if (pri_freq != pri || sec_freq != sec) {
3415b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
3425b9c547cSRui Paulo 				   "40 MHz pri/sec mismatch with BSS "
3435b9c547cSRui Paulo 				   MACSTR
3445b9c547cSRui Paulo 				   " <%d,%d> (chan=%d%c) vs. <%d,%d>",
3455b9c547cSRui Paulo 				   MAC2STR(bss->bssid),
3465b9c547cSRui Paulo 				   pri, sec, pri_chan,
3475b9c547cSRui Paulo 				   sec > pri ? '+' : '-',
3485b9c547cSRui Paulo 				   pri_freq, sec_freq);
349*c1d255d3SCy Schubert 			return 1;
3505b9c547cSRui Paulo 		}
3515b9c547cSRui Paulo 	}
3525b9c547cSRui Paulo 
353*c1d255d3SCy Schubert 	ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len, &elems, 0);
354325151a3SRui Paulo 	if (elems.ht_capabilities) {
3555b9c547cSRui Paulo 		struct ieee80211_ht_capabilities *ht_cap =
3565b9c547cSRui Paulo 			(struct ieee80211_ht_capabilities *)
3575b9c547cSRui Paulo 			elems.ht_capabilities;
3585b9c547cSRui Paulo 
3595b9c547cSRui Paulo 		if (le_to_host16(ht_cap->ht_capabilities_info) &
3605b9c547cSRui Paulo 		    HT_CAP_INFO_40MHZ_INTOLERANT) {
3615b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
3625b9c547cSRui Paulo 				   "40 MHz Intolerant is set on channel %d in BSS "
3635b9c547cSRui Paulo 				   MACSTR, pri, MAC2STR(bss->bssid));
364*c1d255d3SCy Schubert 			return 2;
365*c1d255d3SCy Schubert 		}
366*c1d255d3SCy Schubert 	}
367*c1d255d3SCy Schubert 
3685b9c547cSRui Paulo 	return 0;
3695b9c547cSRui Paulo }
370*c1d255d3SCy Schubert 
371*c1d255d3SCy Schubert 
372*c1d255d3SCy Schubert int check_40mhz_2g4(struct hostapd_hw_modes *mode,
373*c1d255d3SCy Schubert 		    struct wpa_scan_results *scan_res, int pri_chan,
374*c1d255d3SCy Schubert 		    int sec_chan)
375*c1d255d3SCy Schubert {
376*c1d255d3SCy Schubert 	int pri_freq, sec_freq;
377*c1d255d3SCy Schubert 	size_t i;
378*c1d255d3SCy Schubert 
379*c1d255d3SCy Schubert 	if (!mode || !scan_res || !pri_chan || !sec_chan ||
380*c1d255d3SCy Schubert 	    pri_chan == sec_chan)
381*c1d255d3SCy Schubert 		return 0;
382*c1d255d3SCy Schubert 
383*c1d255d3SCy Schubert 	pri_freq = hw_get_freq(mode, pri_chan);
384*c1d255d3SCy Schubert 	sec_freq = hw_get_freq(mode, sec_chan);
385*c1d255d3SCy Schubert 
386*c1d255d3SCy Schubert 	wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
387*c1d255d3SCy Schubert 		   (pri_freq + sec_freq) / 2 - 25,
388*c1d255d3SCy Schubert 		   (pri_freq + sec_freq) / 2 + 25);
389*c1d255d3SCy Schubert 	for (i = 0; i < scan_res->num; i++) {
390*c1d255d3SCy Schubert 		if (check_bss_coex_40mhz(scan_res->res[i], pri_freq, sec_freq))
391*c1d255d3SCy Schubert 			return 0;
3925b9c547cSRui Paulo 	}
3935b9c547cSRui Paulo 
3945b9c547cSRui Paulo 	return 1;
3955b9c547cSRui Paulo }
3965b9c547cSRui Paulo 
3975b9c547cSRui Paulo 
3985b9c547cSRui Paulo int hostapd_set_freq_params(struct hostapd_freq_params *data,
3995b9c547cSRui Paulo 			    enum hostapd_hw_mode mode,
400*c1d255d3SCy Schubert 			    int freq, int channel, int enable_edmg,
401*c1d255d3SCy Schubert 			    u8 edmg_channel, int ht_enabled,
402206b73d0SCy Schubert 			    int vht_enabled, int he_enabled,
403206b73d0SCy Schubert 			    int sec_channel_offset,
404206b73d0SCy Schubert 			    int oper_chwidth, int center_segment0,
405206b73d0SCy Schubert 			    int center_segment1, u32 vht_caps,
406206b73d0SCy Schubert 			    struct he_capabilities *he_cap)
4075b9c547cSRui Paulo {
408206b73d0SCy Schubert 	if (!he_cap)
409206b73d0SCy Schubert 		he_enabled = 0;
4105b9c547cSRui Paulo 	os_memset(data, 0, sizeof(*data));
4115b9c547cSRui Paulo 	data->mode = mode;
4125b9c547cSRui Paulo 	data->freq = freq;
4135b9c547cSRui Paulo 	data->channel = channel;
4145b9c547cSRui Paulo 	data->ht_enabled = ht_enabled;
4155b9c547cSRui Paulo 	data->vht_enabled = vht_enabled;
416206b73d0SCy Schubert 	data->he_enabled = he_enabled;
4175b9c547cSRui Paulo 	data->sec_channel_offset = sec_channel_offset;
4185b9c547cSRui Paulo 	data->center_freq1 = freq + sec_channel_offset * 10;
4195b9c547cSRui Paulo 	data->center_freq2 = 0;
4205b9c547cSRui Paulo 	data->bandwidth = sec_channel_offset ? 40 : 20;
4215b9c547cSRui Paulo 
422*c1d255d3SCy Schubert 	hostapd_encode_edmg_chan(enable_edmg, edmg_channel, channel,
423*c1d255d3SCy Schubert 				 &data->edmg);
424*c1d255d3SCy Schubert 
425*c1d255d3SCy Schubert 	if (is_6ghz_freq(freq)) {
426*c1d255d3SCy Schubert 		if (!data->he_enabled) {
427*c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
428*c1d255d3SCy Schubert 				   "Can't set 6 GHz mode - HE isn't enabled");
4295b9c547cSRui Paulo 			return -1;
430*c1d255d3SCy Schubert 		}
431*c1d255d3SCy Schubert 
432*c1d255d3SCy Schubert 		if (center_idx_to_bw_6ghz(channel) < 0) {
433*c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
434*c1d255d3SCy Schubert 				   "Invalid control channel for 6 GHz band");
435*c1d255d3SCy Schubert 			return -1;
436*c1d255d3SCy Schubert 		}
437*c1d255d3SCy Schubert 
438*c1d255d3SCy Schubert 		if (!center_segment0) {
439*c1d255d3SCy Schubert 			if (center_segment1) {
440*c1d255d3SCy Schubert 				wpa_printf(MSG_ERROR,
441*c1d255d3SCy Schubert 					   "Segment 0 center frequency isn't set");
442*c1d255d3SCy Schubert 				return -1;
443*c1d255d3SCy Schubert 			}
444*c1d255d3SCy Schubert 
445*c1d255d3SCy Schubert 			data->center_freq1 = data->freq;
446*c1d255d3SCy Schubert 			data->bandwidth = 20;
447*c1d255d3SCy Schubert 		} else {
448*c1d255d3SCy Schubert 			int freq1, freq2 = 0;
449*c1d255d3SCy Schubert 			int bw = center_idx_to_bw_6ghz(center_segment0);
450*c1d255d3SCy Schubert 
451*c1d255d3SCy Schubert 			if (bw < 0) {
452*c1d255d3SCy Schubert 				wpa_printf(MSG_ERROR,
453*c1d255d3SCy Schubert 					   "Invalid center frequency index for 6 GHz");
454*c1d255d3SCy Schubert 				return -1;
455*c1d255d3SCy Schubert 			}
456*c1d255d3SCy Schubert 
457*c1d255d3SCy Schubert 			freq1 = ieee80211_chan_to_freq(NULL, 131,
458*c1d255d3SCy Schubert 						       center_segment0);
459*c1d255d3SCy Schubert 			if (freq1 < 0) {
460*c1d255d3SCy Schubert 				wpa_printf(MSG_ERROR,
461*c1d255d3SCy Schubert 					   "Invalid segment 0 center frequency for 6 GHz");
462*c1d255d3SCy Schubert 				return -1;
463*c1d255d3SCy Schubert 			}
464*c1d255d3SCy Schubert 
465*c1d255d3SCy Schubert 			if (center_segment1) {
466*c1d255d3SCy Schubert 				if (center_idx_to_bw_6ghz(center_segment1) != 2 ||
467*c1d255d3SCy Schubert 				    bw != 2) {
468*c1d255d3SCy Schubert 					wpa_printf(MSG_ERROR,
469*c1d255d3SCy Schubert 						   "6 GHz 80+80 MHz configuration doesn't use valid 80 MHz channels");
470*c1d255d3SCy Schubert 					return -1;
471*c1d255d3SCy Schubert 				}
472*c1d255d3SCy Schubert 
473*c1d255d3SCy Schubert 				freq2 = ieee80211_chan_to_freq(NULL, 131,
474*c1d255d3SCy Schubert 							       center_segment1);
475*c1d255d3SCy Schubert 				if (freq2 < 0) {
476*c1d255d3SCy Schubert 					wpa_printf(MSG_ERROR,
477*c1d255d3SCy Schubert 						   "Invalid segment 1 center frequency for UHB");
478*c1d255d3SCy Schubert 					return -1;
479*c1d255d3SCy Schubert 				}
480*c1d255d3SCy Schubert 			}
481*c1d255d3SCy Schubert 
482*c1d255d3SCy Schubert 			data->bandwidth = (1 << (u8) bw) * 20;
483*c1d255d3SCy Schubert 			data->center_freq1 = freq1;
484*c1d255d3SCy Schubert 			data->center_freq2 = freq2;
485*c1d255d3SCy Schubert 		}
486*c1d255d3SCy Schubert 		data->ht_enabled = 0;
487*c1d255d3SCy Schubert 		data->vht_enabled = 0;
488*c1d255d3SCy Schubert 
489*c1d255d3SCy Schubert 		return 0;
490*c1d255d3SCy Schubert 	}
491*c1d255d3SCy Schubert 
492*c1d255d3SCy Schubert 	if (data->he_enabled) switch (oper_chwidth) {
493*c1d255d3SCy Schubert 	case CHANWIDTH_USE_HT:
494*c1d255d3SCy Schubert 		if (mode == HOSTAPD_MODE_IEEE80211G && sec_channel_offset) {
495*c1d255d3SCy Schubert 			if (!(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
496*c1d255d3SCy Schubert 			      HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_IN_2G)) {
497*c1d255d3SCy Schubert 				wpa_printf(MSG_ERROR,
498*c1d255d3SCy Schubert 					   "40 MHz channel width is not supported in 2.4 GHz");
499*c1d255d3SCy Schubert 				return -1;
500*c1d255d3SCy Schubert 			}
501*c1d255d3SCy Schubert 			break;
502*c1d255d3SCy Schubert 		}
503*c1d255d3SCy Schubert 		/* fall through */
504*c1d255d3SCy Schubert 	case CHANWIDTH_80MHZ:
505*c1d255d3SCy Schubert 		if (mode == HOSTAPD_MODE_IEEE80211A) {
506*c1d255d3SCy Schubert 			if (!(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
507*c1d255d3SCy Schubert 			      HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) {
508*c1d255d3SCy Schubert 				wpa_printf(MSG_ERROR,
509*c1d255d3SCy Schubert 					   "40/80 MHz channel width is not supported in 5/6 GHz");
510*c1d255d3SCy Schubert 				return -1;
511*c1d255d3SCy Schubert 			}
512*c1d255d3SCy Schubert 		}
513*c1d255d3SCy Schubert 		break;
514*c1d255d3SCy Schubert 	case CHANWIDTH_80P80MHZ:
515*c1d255d3SCy Schubert 		if (!(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
516*c1d255d3SCy Schubert 		      HE_PHYCAP_CHANNEL_WIDTH_SET_80PLUS80MHZ_IN_5G)) {
517*c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
518*c1d255d3SCy Schubert 				   "80+80 MHz channel width is not supported in 5/6 GHz");
519*c1d255d3SCy Schubert 			return -1;
520*c1d255d3SCy Schubert 		}
521*c1d255d3SCy Schubert 		break;
522*c1d255d3SCy Schubert 	case CHANWIDTH_160MHZ:
523*c1d255d3SCy Schubert 		if (!(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
524*c1d255d3SCy Schubert 		      HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G)) {
525*c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
526*c1d255d3SCy Schubert 				   "160 MHz channel width is not supported in 5 / 6GHz");
527*c1d255d3SCy Schubert 			return -1;
528*c1d255d3SCy Schubert 		}
529*c1d255d3SCy Schubert 		break;
530*c1d255d3SCy Schubert 	} else if (data->vht_enabled) switch (oper_chwidth) {
531*c1d255d3SCy Schubert 	case CHANWIDTH_USE_HT:
5325b9c547cSRui Paulo 		break;
533206b73d0SCy Schubert 	case CHANWIDTH_80P80MHZ:
5345b9c547cSRui Paulo 		if (!(vht_caps & VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)) {
5355b9c547cSRui Paulo 			wpa_printf(MSG_ERROR,
5365b9c547cSRui Paulo 				   "80+80 channel width is not supported!");
5375b9c547cSRui Paulo 			return -1;
5385b9c547cSRui Paulo 		}
539*c1d255d3SCy Schubert 		/* fall through */
540*c1d255d3SCy Schubert 	case CHANWIDTH_80MHZ:
541*c1d255d3SCy Schubert 		break;
542*c1d255d3SCy Schubert 	case CHANWIDTH_160MHZ:
543*c1d255d3SCy Schubert 		if (!(vht_caps & (VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
544*c1d255d3SCy Schubert 				  VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))) {
545*c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
546*c1d255d3SCy Schubert 				   "160 MHz channel width is not supported!");
5475b9c547cSRui Paulo 			return -1;
548*c1d255d3SCy Schubert 		}
549*c1d255d3SCy Schubert 		break;
550*c1d255d3SCy Schubert 	}
551*c1d255d3SCy Schubert 
552*c1d255d3SCy Schubert 	if (data->he_enabled || data->vht_enabled) switch (oper_chwidth) {
553*c1d255d3SCy Schubert 	case CHANWIDTH_USE_HT:
554*c1d255d3SCy Schubert 		if (center_segment1 ||
555*c1d255d3SCy Schubert 		    (center_segment0 != 0 &&
556*c1d255d3SCy Schubert 		     5000 + center_segment0 * 5 != data->center_freq1 &&
557*c1d255d3SCy Schubert 		     2407 + center_segment0 * 5 != data->center_freq1)) {
558*c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
559*c1d255d3SCy Schubert 				   "20/40 MHz: center segment 0 (=%d) and center freq 1 (=%d) not in sync",
560*c1d255d3SCy Schubert 				   center_segment0, data->center_freq1);
561*c1d255d3SCy Schubert 			return -1;
562*c1d255d3SCy Schubert 		}
563*c1d255d3SCy Schubert 		break;
564*c1d255d3SCy Schubert 	case CHANWIDTH_80P80MHZ:
565*c1d255d3SCy Schubert 		if (center_segment1 == center_segment0 + 4 ||
566*c1d255d3SCy Schubert 		    center_segment1 == center_segment0 - 4) {
567*c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
568*c1d255d3SCy Schubert 				   "80+80 MHz: center segment 1 only 20 MHz apart");
569*c1d255d3SCy Schubert 			return -1;
570*c1d255d3SCy Schubert 		}
5715b9c547cSRui Paulo 		data->center_freq2 = 5000 + center_segment1 * 5;
5725b9c547cSRui Paulo 		/* fall through */
573206b73d0SCy Schubert 	case CHANWIDTH_80MHZ:
5745b9c547cSRui Paulo 		data->bandwidth = 80;
575*c1d255d3SCy Schubert 		if (!sec_channel_offset) {
576*c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
577*c1d255d3SCy Schubert 				   "80/80+80 MHz: no second channel offset");
5785b9c547cSRui Paulo 			return -1;
579*c1d255d3SCy Schubert 		}
580*c1d255d3SCy Schubert 		if (oper_chwidth == CHANWIDTH_80MHZ && center_segment1) {
581*c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
582*c1d255d3SCy Schubert 				   "80 MHz: center segment 1 configured");
583*c1d255d3SCy Schubert 			return -1;
584*c1d255d3SCy Schubert 		}
585*c1d255d3SCy Schubert 		if (oper_chwidth == CHANWIDTH_80P80MHZ && !center_segment1) {
586*c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
587*c1d255d3SCy Schubert 				   "80+80 MHz: center segment 1 not configured");
588*c1d255d3SCy Schubert 			return -1;
589*c1d255d3SCy Schubert 		}
590325151a3SRui Paulo 		if (!center_segment0) {
591325151a3SRui Paulo 			if (channel <= 48)
592325151a3SRui Paulo 				center_segment0 = 42;
593325151a3SRui Paulo 			else if (channel <= 64)
594325151a3SRui Paulo 				center_segment0 = 58;
595325151a3SRui Paulo 			else if (channel <= 112)
596325151a3SRui Paulo 				center_segment0 = 106;
597325151a3SRui Paulo 			else if (channel <= 128)
598325151a3SRui Paulo 				center_segment0 = 122;
599325151a3SRui Paulo 			else if (channel <= 144)
600325151a3SRui Paulo 				center_segment0 = 138;
601325151a3SRui Paulo 			else if (channel <= 161)
602325151a3SRui Paulo 				center_segment0 = 155;
603*c1d255d3SCy Schubert 			else if (channel <= 177)
604*c1d255d3SCy Schubert 				center_segment0 = 171;
6055b9c547cSRui Paulo 			data->center_freq1 = 5000 + center_segment0 * 5;
606325151a3SRui Paulo 		} else {
607325151a3SRui Paulo 			/*
608325151a3SRui Paulo 			 * Note: HT/VHT config and params are coupled. Check if
609325151a3SRui Paulo 			 * HT40 channel band is in VHT80 Pri channel band
610325151a3SRui Paulo 			 * configuration.
611325151a3SRui Paulo 			 */
612325151a3SRui Paulo 			if (center_segment0 == channel + 6 ||
613325151a3SRui Paulo 			    center_segment0 == channel + 2 ||
614325151a3SRui Paulo 			    center_segment0 == channel - 2 ||
615325151a3SRui Paulo 			    center_segment0 == channel - 6)
616325151a3SRui Paulo 				data->center_freq1 = 5000 + center_segment0 * 5;
617*c1d255d3SCy Schubert 			else {
618*c1d255d3SCy Schubert 				wpa_printf(MSG_ERROR,
619*c1d255d3SCy Schubert 					   "Wrong coupling between HT and VHT/HE channel setting");
620325151a3SRui Paulo 				return -1;
621325151a3SRui Paulo 			}
622*c1d255d3SCy Schubert 		}
6235b9c547cSRui Paulo 		break;
624206b73d0SCy Schubert 	case CHANWIDTH_160MHZ:
6255b9c547cSRui Paulo 		data->bandwidth = 160;
626*c1d255d3SCy Schubert 		if (center_segment1) {
6275b9c547cSRui Paulo 			wpa_printf(MSG_ERROR,
628*c1d255d3SCy Schubert 				   "160 MHz: center segment 1 should not be set");
6295b9c547cSRui Paulo 			return -1;
6305b9c547cSRui Paulo 		}
631*c1d255d3SCy Schubert 		if (!sec_channel_offset) {
632*c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
633*c1d255d3SCy Schubert 				   "160 MHz: second channel offset not set");
6345b9c547cSRui Paulo 			return -1;
635*c1d255d3SCy Schubert 		}
636325151a3SRui Paulo 		/*
637325151a3SRui Paulo 		 * Note: HT/VHT config and params are coupled. Check if
638325151a3SRui Paulo 		 * HT40 channel band is in VHT160 channel band configuration.
639325151a3SRui Paulo 		 */
640325151a3SRui Paulo 		if (center_segment0 == channel + 14 ||
641325151a3SRui Paulo 		    center_segment0 == channel + 10 ||
642325151a3SRui Paulo 		    center_segment0 == channel + 6 ||
643325151a3SRui Paulo 		    center_segment0 == channel + 2 ||
644325151a3SRui Paulo 		    center_segment0 == channel - 2 ||
645325151a3SRui Paulo 		    center_segment0 == channel - 6 ||
646325151a3SRui Paulo 		    center_segment0 == channel - 10 ||
647325151a3SRui Paulo 		    center_segment0 == channel - 14)
6485b9c547cSRui Paulo 			data->center_freq1 = 5000 + center_segment0 * 5;
649*c1d255d3SCy Schubert 		else {
650*c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
651*c1d255d3SCy Schubert 				   "160 MHz: HT40 channel band is not in 160 MHz band");
652325151a3SRui Paulo 			return -1;
653*c1d255d3SCy Schubert 		}
6545b9c547cSRui Paulo 		break;
6555b9c547cSRui Paulo 	}
6565b9c547cSRui Paulo 
6575b9c547cSRui Paulo 	return 0;
6585b9c547cSRui Paulo }
65985732ac8SCy Schubert 
66085732ac8SCy Schubert 
66185732ac8SCy Schubert void set_disable_ht40(struct ieee80211_ht_capabilities *htcaps,
66285732ac8SCy Schubert 		      int disabled)
66385732ac8SCy Schubert {
66485732ac8SCy Schubert 	/* Masking these out disables HT40 */
66585732ac8SCy Schubert 	le16 msk = host_to_le16(HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET |
66685732ac8SCy Schubert 				HT_CAP_INFO_SHORT_GI40MHZ);
66785732ac8SCy Schubert 
66885732ac8SCy Schubert 	if (disabled)
66985732ac8SCy Schubert 		htcaps->ht_capabilities_info &= ~msk;
67085732ac8SCy Schubert 	else
67185732ac8SCy Schubert 		htcaps->ht_capabilities_info |= msk;
67285732ac8SCy Schubert }
67385732ac8SCy Schubert 
67485732ac8SCy Schubert 
67585732ac8SCy Schubert #ifdef CONFIG_IEEE80211AC
67685732ac8SCy Schubert 
67785732ac8SCy Schubert static int _ieee80211ac_cap_check(u32 hw, u32 conf, u32 cap,
67885732ac8SCy Schubert 				  const char *name)
67985732ac8SCy Schubert {
68085732ac8SCy Schubert 	u32 req_cap = conf & cap;
68185732ac8SCy Schubert 
68285732ac8SCy Schubert 	/*
68385732ac8SCy Schubert 	 * Make sure we support all requested capabilities.
68485732ac8SCy Schubert 	 * NOTE: We assume that 'cap' represents a capability mask,
68585732ac8SCy Schubert 	 * not a discrete value.
68685732ac8SCy Schubert 	 */
68785732ac8SCy Schubert 	if ((hw & req_cap) != req_cap) {
68885732ac8SCy Schubert 		wpa_printf(MSG_ERROR,
68985732ac8SCy Schubert 			   "Driver does not support configured VHT capability [%s]",
69085732ac8SCy Schubert 			   name);
69185732ac8SCy Schubert 		return 0;
69285732ac8SCy Schubert 	}
69385732ac8SCy Schubert 	return 1;
69485732ac8SCy Schubert }
69585732ac8SCy Schubert 
69685732ac8SCy Schubert 
69785732ac8SCy Schubert static int ieee80211ac_cap_check_max(u32 hw, u32 conf, u32 mask,
69885732ac8SCy Schubert 				     unsigned int shift,
69985732ac8SCy Schubert 				     const char *name)
70085732ac8SCy Schubert {
70185732ac8SCy Schubert 	u32 hw_max = hw & mask;
70285732ac8SCy Schubert 	u32 conf_val = conf & mask;
70385732ac8SCy Schubert 
70485732ac8SCy Schubert 	if (conf_val > hw_max) {
70585732ac8SCy Schubert 		wpa_printf(MSG_ERROR,
70685732ac8SCy Schubert 			   "Configured VHT capability [%s] exceeds max value supported by the driver (%d > %d)",
70785732ac8SCy Schubert 			   name, conf_val >> shift, hw_max >> shift);
70885732ac8SCy Schubert 		return 0;
70985732ac8SCy Schubert 	}
71085732ac8SCy Schubert 	return 1;
71185732ac8SCy Schubert }
71285732ac8SCy Schubert 
71385732ac8SCy Schubert 
71485732ac8SCy Schubert int ieee80211ac_cap_check(u32 hw, u32 conf)
71585732ac8SCy Schubert {
71685732ac8SCy Schubert #define VHT_CAP_CHECK(cap) \
71785732ac8SCy Schubert 	do { \
71885732ac8SCy Schubert 		if (!_ieee80211ac_cap_check(hw, conf, cap, #cap)) \
71985732ac8SCy Schubert 			return 0; \
72085732ac8SCy Schubert 	} while (0)
72185732ac8SCy Schubert 
72285732ac8SCy Schubert #define VHT_CAP_CHECK_MAX(cap) \
72385732ac8SCy Schubert 	do { \
72485732ac8SCy Schubert 		if (!ieee80211ac_cap_check_max(hw, conf, cap, cap ## _SHIFT, \
72585732ac8SCy Schubert 					       #cap)) \
72685732ac8SCy Schubert 			return 0; \
72785732ac8SCy Schubert 	} while (0)
72885732ac8SCy Schubert 
72985732ac8SCy Schubert 	VHT_CAP_CHECK_MAX(VHT_CAP_MAX_MPDU_LENGTH_MASK);
73085732ac8SCy Schubert 	VHT_CAP_CHECK_MAX(VHT_CAP_SUPP_CHAN_WIDTH_MASK);
73185732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_RXLDPC);
73285732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_SHORT_GI_80);
73385732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_SHORT_GI_160);
73485732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_TXSTBC);
73585732ac8SCy Schubert 	VHT_CAP_CHECK_MAX(VHT_CAP_RXSTBC_MASK);
73685732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMER_CAPABLE);
73785732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMEE_CAPABLE);
73885732ac8SCy Schubert 	VHT_CAP_CHECK_MAX(VHT_CAP_BEAMFORMEE_STS_MAX);
73985732ac8SCy Schubert 	VHT_CAP_CHECK_MAX(VHT_CAP_SOUNDING_DIMENSION_MAX);
74085732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMER_CAPABLE);
74185732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMEE_CAPABLE);
74285732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_VHT_TXOP_PS);
74385732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_HTC_VHT);
74485732ac8SCy Schubert 	VHT_CAP_CHECK_MAX(VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX);
74585732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_UNSOL_MFB);
74685732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_MRQ_MFB);
74785732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_RX_ANTENNA_PATTERN);
74885732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_TX_ANTENNA_PATTERN);
74985732ac8SCy Schubert 
75085732ac8SCy Schubert #undef VHT_CAP_CHECK
75185732ac8SCy Schubert #undef VHT_CAP_CHECK_MAX
75285732ac8SCy Schubert 
75385732ac8SCy Schubert 	return 1;
75485732ac8SCy Schubert }
75585732ac8SCy Schubert 
75685732ac8SCy Schubert #endif /* CONFIG_IEEE80211AC */
7574bc52338SCy Schubert 
7584bc52338SCy Schubert 
7594bc52338SCy Schubert u32 num_chan_to_bw(int num_chans)
7604bc52338SCy Schubert {
7614bc52338SCy Schubert 	switch (num_chans) {
7624bc52338SCy Schubert 	case 2:
7634bc52338SCy Schubert 	case 4:
7644bc52338SCy Schubert 	case 8:
7654bc52338SCy Schubert 		return num_chans * 20;
7664bc52338SCy Schubert 	default:
7674bc52338SCy Schubert 		return 20;
7684bc52338SCy Schubert 	}
7694bc52338SCy Schubert }
7704bc52338SCy Schubert 
7714bc52338SCy Schubert 
7724bc52338SCy Schubert /* check if BW is applicable for channel */
7734bc52338SCy Schubert int chan_bw_allowed(const struct hostapd_channel_data *chan, u32 bw,
7744bc52338SCy Schubert 		    int ht40_plus, int pri)
7754bc52338SCy Schubert {
7764bc52338SCy Schubert 	u32 bw_mask;
7774bc52338SCy Schubert 
7784bc52338SCy Schubert 	switch (bw) {
7794bc52338SCy Schubert 	case 20:
7804bc52338SCy Schubert 		bw_mask = HOSTAPD_CHAN_WIDTH_20;
7814bc52338SCy Schubert 		break;
7824bc52338SCy Schubert 	case 40:
7834bc52338SCy Schubert 		/* HT 40 MHz support declared only for primary channel,
7844bc52338SCy Schubert 		 * just skip 40 MHz secondary checking */
7854bc52338SCy Schubert 		if (pri && ht40_plus)
7864bc52338SCy Schubert 			bw_mask = HOSTAPD_CHAN_WIDTH_40P;
7874bc52338SCy Schubert 		else if (pri && !ht40_plus)
7884bc52338SCy Schubert 			bw_mask = HOSTAPD_CHAN_WIDTH_40M;
7894bc52338SCy Schubert 		else
7904bc52338SCy Schubert 			bw_mask = 0;
7914bc52338SCy Schubert 		break;
7924bc52338SCy Schubert 	case 80:
7934bc52338SCy Schubert 		bw_mask = HOSTAPD_CHAN_WIDTH_80;
7944bc52338SCy Schubert 		break;
7954bc52338SCy Schubert 	case 160:
7964bc52338SCy Schubert 		bw_mask = HOSTAPD_CHAN_WIDTH_160;
7974bc52338SCy Schubert 		break;
7984bc52338SCy Schubert 	default:
7994bc52338SCy Schubert 		bw_mask = 0;
8004bc52338SCy Schubert 		break;
8014bc52338SCy Schubert 	}
8024bc52338SCy Schubert 
8034bc52338SCy Schubert 	return (chan->allowed_bw & bw_mask) == bw_mask;
8044bc52338SCy Schubert }
8054bc52338SCy Schubert 
8064bc52338SCy Schubert 
8074bc52338SCy Schubert /* check if channel is allowed to be used as primary */
8084bc52338SCy Schubert int chan_pri_allowed(const struct hostapd_channel_data *chan)
8094bc52338SCy Schubert {
8104bc52338SCy Schubert 	return !(chan->flag & HOSTAPD_CHAN_DISABLED) &&
8114bc52338SCy Schubert 		(chan->allowed_bw & HOSTAPD_CHAN_WIDTH_20);
8124bc52338SCy Schubert }
813