xref: /freebsd/contrib/wpa/src/common/hw_features_common.c (revision 4b72b91a7132df1f77bbae194e1071ac621f1edb)
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 
43c1d255d3SCy Schubert struct hostapd_channel_data *
44c1d255d3SCy 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];
50c1d255d3SCy 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 
62c1d255d3SCy Schubert struct hostapd_channel_data *
63c1d255d3SCy Schubert hw_get_channel_freq(enum hostapd_hw_mode mode, int freq, int *chan,
64c1d255d3SCy Schubert 		    struct hostapd_hw_modes *hw_features, int num_hw_features)
65c1d255d3SCy Schubert {
66c1d255d3SCy Schubert 	struct hostapd_channel_data *chan_data;
67c1d255d3SCy Schubert 	int i;
68c1d255d3SCy Schubert 
69c1d255d3SCy Schubert 	if (chan)
70c1d255d3SCy Schubert 		*chan = 0;
71c1d255d3SCy Schubert 
72c1d255d3SCy Schubert 	if (!hw_features)
73c1d255d3SCy Schubert 		return NULL;
74c1d255d3SCy Schubert 
75c1d255d3SCy Schubert 	for (i = 0; i < num_hw_features; i++) {
76c1d255d3SCy Schubert 		struct hostapd_hw_modes *curr_mode = &hw_features[i];
77c1d255d3SCy Schubert 
78c1d255d3SCy Schubert 		if (curr_mode->mode != mode)
79c1d255d3SCy Schubert 			continue;
80c1d255d3SCy Schubert 
81c1d255d3SCy Schubert 		chan_data = hw_mode_get_channel(curr_mode, freq, chan);
82c1d255d3SCy Schubert 		if (chan_data)
83c1d255d3SCy Schubert 			return chan_data;
84c1d255d3SCy Schubert 	}
85c1d255d3SCy Schubert 
86c1d255d3SCy Schubert 	return NULL;
87c1d255d3SCy Schubert }
88c1d255d3SCy Schubert 
89c1d255d3SCy 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 
100c1d255d3SCy Schubert int hw_get_chan(enum hostapd_hw_mode mode, int freq,
101c1d255d3SCy Schubert 		struct hostapd_hw_modes *hw_features, int num_hw_features)
1025b9c547cSRui Paulo {
1035b9c547cSRui Paulo 	int chan;
1045b9c547cSRui Paulo 
105c1d255d3SCy 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 
111c1d255d3SCy Schubert int allowed_ht40_channel_pair(enum hostapd_hw_mode mode,
112c1d255d3SCy Schubert 			      struct hostapd_channel_data *p_chan,
113c1d255d3SCy 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,
117*4b72b91aSCy Schubert 			  149, 157, 165, 173, 184, 192 };
1185b9c547cSRui Paulo 	size_t k;
119c1d255d3SCy Schubert 	int ht40_plus, pri_chan, sec_chan;
1205b9c547cSRui Paulo 
121c1d255d3SCy Schubert 	if (!p_chan || !s_chan)
1224bc52338SCy Schubert 		return 0;
123c1d255d3SCy Schubert 	pri_chan = p_chan->chan;
124c1d255d3SCy Schubert 	sec_chan = s_chan->chan;
125c1d255d3SCy Schubert 
126c1d255d3SCy 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,
138c1d255d3SCy Schubert 		   "HT40: control channel: %d (%d MHz), secondary channel: %d (%d MHz)",
139c1d255d3SCy 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 	 */
157c1d255d3SCy 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 
202c1d255d3SCy Schubert int check_40mhz_5g(struct wpa_scan_results *scan_res,
203c1d255d3SCy Schubert 		   struct hostapd_channel_data *pri_chan,
204c1d255d3SCy Schubert 		   struct hostapd_channel_data *sec_chan)
2055b9c547cSRui Paulo {
206c1d255d3SCy 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 
211c1d255d3SCy Schubert 	if (!scan_res || !pri_chan || !sec_chan ||
212c1d255d3SCy 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];
222c1d255d3SCy Schubert 		if (bss->freq == pri_chan->freq)
2235b9c547cSRui Paulo 			pri_bss++;
224c1d255d3SCy 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);
242c1d255d3SCy Schubert 		if (pri_chan->chan == bss_pri_chan &&
243c1d255d3SCy 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);
252c1d255d3SCy Schubert 			if (pri_chan->chan == bss_sec_chan &&
253c1d255d3SCy 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 
296c1d255d3SCy Schubert /*
297c1d255d3SCy Schubert  * Returns:
298c1d255d3SCy Schubert  * 0: no impact
299c1d255d3SCy Schubert  * 1: overlapping BSS
300c1d255d3SCy Schubert  * 2: overlapping BSS with 40 MHz intolerant advertisement
301c1d255d3SCy Schubert  */
302c1d255d3SCy 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;
305c1d255d3SCy Schubert 	struct ieee802_11_elems elems;
306c1d255d3SCy Schubert 	int pri_chan, sec_chan;
307c1d255d3SCy Schubert 	int pri = bss->freq;
308c1d255d3SCy Schubert 	int sec = pri;
3095b9c547cSRui Paulo 
310c1d255d3SCy Schubert 	if (pri_freq == sec_freq)
311c1d255d3SCy 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 */
317c1d255d3SCy Schubert 	if (check_20mhz_bss(bss, pri_freq, affected_start, affected_end)) {
318c1d255d3SCy Schubert 		wpa_printf(MSG_DEBUG, "Overlapping 20 MHz BSS is found");
319c1d255d3SCy 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))
333c1d255d3SCy 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);
349c1d255d3SCy Schubert 			return 1;
3505b9c547cSRui Paulo 		}
3515b9c547cSRui Paulo 	}
3525b9c547cSRui Paulo 
353c1d255d3SCy 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));
364c1d255d3SCy Schubert 			return 2;
365c1d255d3SCy Schubert 		}
366c1d255d3SCy Schubert 	}
367c1d255d3SCy Schubert 
3685b9c547cSRui Paulo 	return 0;
3695b9c547cSRui Paulo }
370c1d255d3SCy Schubert 
371c1d255d3SCy Schubert 
372c1d255d3SCy Schubert int check_40mhz_2g4(struct hostapd_hw_modes *mode,
373c1d255d3SCy Schubert 		    struct wpa_scan_results *scan_res, int pri_chan,
374c1d255d3SCy Schubert 		    int sec_chan)
375c1d255d3SCy Schubert {
376c1d255d3SCy Schubert 	int pri_freq, sec_freq;
377c1d255d3SCy Schubert 	size_t i;
378c1d255d3SCy Schubert 
379c1d255d3SCy Schubert 	if (!mode || !scan_res || !pri_chan || !sec_chan ||
380c1d255d3SCy Schubert 	    pri_chan == sec_chan)
381c1d255d3SCy Schubert 		return 0;
382c1d255d3SCy Schubert 
383c1d255d3SCy Schubert 	pri_freq = hw_get_freq(mode, pri_chan);
384c1d255d3SCy Schubert 	sec_freq = hw_get_freq(mode, sec_chan);
385c1d255d3SCy Schubert 
386c1d255d3SCy Schubert 	wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
387c1d255d3SCy Schubert 		   (pri_freq + sec_freq) / 2 - 25,
388c1d255d3SCy Schubert 		   (pri_freq + sec_freq) / 2 + 25);
389c1d255d3SCy Schubert 	for (i = 0; i < scan_res->num; i++) {
390c1d255d3SCy Schubert 		if (check_bss_coex_40mhz(scan_res->res[i], pri_freq, sec_freq))
391c1d255d3SCy 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,
400c1d255d3SCy Schubert 			    int freq, int channel, int enable_edmg,
401c1d255d3SCy 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 {
408*4b72b91aSCy Schubert 	if (!he_cap || !he_cap->he_supported)
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;
420*4b72b91aSCy Schubert 	if (oper_chwidth == CHANWIDTH_80MHZ)
421*4b72b91aSCy Schubert 		data->bandwidth = 80;
422*4b72b91aSCy Schubert 	else if (oper_chwidth == CHANWIDTH_160MHZ ||
423*4b72b91aSCy Schubert 		 oper_chwidth == CHANWIDTH_80P80MHZ)
424*4b72b91aSCy Schubert 		data->bandwidth = 160;
425*4b72b91aSCy Schubert 	else if (sec_channel_offset)
426*4b72b91aSCy Schubert 		data->bandwidth = 40;
427*4b72b91aSCy Schubert 	else
428*4b72b91aSCy Schubert 		data->bandwidth = 20;
429*4b72b91aSCy Schubert 
4305b9c547cSRui Paulo 
431c1d255d3SCy Schubert 	hostapd_encode_edmg_chan(enable_edmg, edmg_channel, channel,
432c1d255d3SCy Schubert 				 &data->edmg);
433c1d255d3SCy Schubert 
434c1d255d3SCy Schubert 	if (is_6ghz_freq(freq)) {
435c1d255d3SCy Schubert 		if (!data->he_enabled) {
436c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
437c1d255d3SCy Schubert 				   "Can't set 6 GHz mode - HE isn't enabled");
4385b9c547cSRui Paulo 			return -1;
439c1d255d3SCy Schubert 		}
440c1d255d3SCy Schubert 
441c1d255d3SCy Schubert 		if (center_idx_to_bw_6ghz(channel) < 0) {
442c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
443c1d255d3SCy Schubert 				   "Invalid control channel for 6 GHz band");
444c1d255d3SCy Schubert 			return -1;
445c1d255d3SCy Schubert 		}
446c1d255d3SCy Schubert 
447c1d255d3SCy Schubert 		if (!center_segment0) {
448c1d255d3SCy Schubert 			if (center_segment1) {
449c1d255d3SCy Schubert 				wpa_printf(MSG_ERROR,
450c1d255d3SCy Schubert 					   "Segment 0 center frequency isn't set");
451c1d255d3SCy Schubert 				return -1;
452c1d255d3SCy Schubert 			}
453*4b72b91aSCy Schubert 			if (!sec_channel_offset)
454c1d255d3SCy Schubert 				data->center_freq1 = data->freq;
455c1d255d3SCy Schubert 		} else {
456c1d255d3SCy Schubert 			int freq1, freq2 = 0;
457c1d255d3SCy Schubert 			int bw = center_idx_to_bw_6ghz(center_segment0);
458c1d255d3SCy Schubert 
459c1d255d3SCy Schubert 			if (bw < 0) {
460c1d255d3SCy Schubert 				wpa_printf(MSG_ERROR,
461c1d255d3SCy Schubert 					   "Invalid center frequency index for 6 GHz");
462c1d255d3SCy Schubert 				return -1;
463c1d255d3SCy Schubert 			}
464c1d255d3SCy Schubert 
465c1d255d3SCy Schubert 			freq1 = ieee80211_chan_to_freq(NULL, 131,
466c1d255d3SCy Schubert 						       center_segment0);
467c1d255d3SCy Schubert 			if (freq1 < 0) {
468c1d255d3SCy Schubert 				wpa_printf(MSG_ERROR,
469c1d255d3SCy Schubert 					   "Invalid segment 0 center frequency for 6 GHz");
470c1d255d3SCy Schubert 				return -1;
471c1d255d3SCy Schubert 			}
472c1d255d3SCy Schubert 
473c1d255d3SCy Schubert 			if (center_segment1) {
474c1d255d3SCy Schubert 				if (center_idx_to_bw_6ghz(center_segment1) != 2 ||
475c1d255d3SCy Schubert 				    bw != 2) {
476c1d255d3SCy Schubert 					wpa_printf(MSG_ERROR,
477c1d255d3SCy Schubert 						   "6 GHz 80+80 MHz configuration doesn't use valid 80 MHz channels");
478c1d255d3SCy Schubert 					return -1;
479c1d255d3SCy Schubert 				}
480c1d255d3SCy Schubert 
481c1d255d3SCy Schubert 				freq2 = ieee80211_chan_to_freq(NULL, 131,
482c1d255d3SCy Schubert 							       center_segment1);
483c1d255d3SCy Schubert 				if (freq2 < 0) {
484c1d255d3SCy Schubert 					wpa_printf(MSG_ERROR,
485c1d255d3SCy Schubert 						   "Invalid segment 1 center frequency for UHB");
486c1d255d3SCy Schubert 					return -1;
487c1d255d3SCy Schubert 				}
488c1d255d3SCy Schubert 			}
489c1d255d3SCy Schubert 
490c1d255d3SCy Schubert 			data->bandwidth = (1 << (u8) bw) * 20;
491c1d255d3SCy Schubert 			data->center_freq1 = freq1;
492c1d255d3SCy Schubert 			data->center_freq2 = freq2;
493c1d255d3SCy Schubert 		}
494c1d255d3SCy Schubert 		data->ht_enabled = 0;
495c1d255d3SCy Schubert 		data->vht_enabled = 0;
496c1d255d3SCy Schubert 
497c1d255d3SCy Schubert 		return 0;
498c1d255d3SCy Schubert 	}
499c1d255d3SCy Schubert 
500c1d255d3SCy Schubert 	if (data->he_enabled) switch (oper_chwidth) {
501c1d255d3SCy Schubert 	case CHANWIDTH_USE_HT:
502*4b72b91aSCy Schubert 		if (sec_channel_offset == 0)
503*4b72b91aSCy Schubert 			break;
504*4b72b91aSCy Schubert 
505*4b72b91aSCy Schubert 		if (mode == HOSTAPD_MODE_IEEE80211G) {
506c1d255d3SCy Schubert 			if (!(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
507c1d255d3SCy Schubert 			      HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_IN_2G)) {
508c1d255d3SCy Schubert 				wpa_printf(MSG_ERROR,
509c1d255d3SCy Schubert 					   "40 MHz channel width is not supported in 2.4 GHz");
510c1d255d3SCy Schubert 				return -1;
511c1d255d3SCy Schubert 			}
512c1d255d3SCy Schubert 			break;
513c1d255d3SCy Schubert 		}
514c1d255d3SCy Schubert 		/* fall through */
515c1d255d3SCy Schubert 	case CHANWIDTH_80MHZ:
516c1d255d3SCy Schubert 		if (mode == HOSTAPD_MODE_IEEE80211A) {
517c1d255d3SCy Schubert 			if (!(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
518c1d255d3SCy Schubert 			      HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) {
519c1d255d3SCy Schubert 				wpa_printf(MSG_ERROR,
520c1d255d3SCy Schubert 					   "40/80 MHz channel width is not supported in 5/6 GHz");
521c1d255d3SCy Schubert 				return -1;
522c1d255d3SCy Schubert 			}
523c1d255d3SCy Schubert 		}
524c1d255d3SCy Schubert 		break;
525c1d255d3SCy Schubert 	case CHANWIDTH_80P80MHZ:
526c1d255d3SCy Schubert 		if (!(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
527c1d255d3SCy Schubert 		      HE_PHYCAP_CHANNEL_WIDTH_SET_80PLUS80MHZ_IN_5G)) {
528c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
529c1d255d3SCy Schubert 				   "80+80 MHz channel width is not supported in 5/6 GHz");
530c1d255d3SCy Schubert 			return -1;
531c1d255d3SCy Schubert 		}
532c1d255d3SCy Schubert 		break;
533c1d255d3SCy Schubert 	case CHANWIDTH_160MHZ:
534c1d255d3SCy Schubert 		if (!(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
535c1d255d3SCy Schubert 		      HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G)) {
536c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
537c1d255d3SCy Schubert 				   "160 MHz channel width is not supported in 5 / 6GHz");
538c1d255d3SCy Schubert 			return -1;
539c1d255d3SCy Schubert 		}
540c1d255d3SCy Schubert 		break;
541c1d255d3SCy Schubert 	} else if (data->vht_enabled) switch (oper_chwidth) {
542c1d255d3SCy Schubert 	case CHANWIDTH_USE_HT:
5435b9c547cSRui Paulo 		break;
544206b73d0SCy Schubert 	case CHANWIDTH_80P80MHZ:
5455b9c547cSRui Paulo 		if (!(vht_caps & VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)) {
5465b9c547cSRui Paulo 			wpa_printf(MSG_ERROR,
5475b9c547cSRui Paulo 				   "80+80 channel width is not supported!");
5485b9c547cSRui Paulo 			return -1;
5495b9c547cSRui Paulo 		}
550c1d255d3SCy Schubert 		/* fall through */
551c1d255d3SCy Schubert 	case CHANWIDTH_80MHZ:
552c1d255d3SCy Schubert 		break;
553c1d255d3SCy Schubert 	case CHANWIDTH_160MHZ:
554c1d255d3SCy Schubert 		if (!(vht_caps & (VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
555c1d255d3SCy Schubert 				  VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))) {
556c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
557c1d255d3SCy Schubert 				   "160 MHz channel width is not supported!");
5585b9c547cSRui Paulo 			return -1;
559c1d255d3SCy Schubert 		}
560c1d255d3SCy Schubert 		break;
561c1d255d3SCy Schubert 	}
562c1d255d3SCy Schubert 
563c1d255d3SCy Schubert 	if (data->he_enabled || data->vht_enabled) switch (oper_chwidth) {
564c1d255d3SCy Schubert 	case CHANWIDTH_USE_HT:
565c1d255d3SCy Schubert 		if (center_segment1 ||
566c1d255d3SCy Schubert 		    (center_segment0 != 0 &&
567c1d255d3SCy Schubert 		     5000 + center_segment0 * 5 != data->center_freq1 &&
568c1d255d3SCy Schubert 		     2407 + center_segment0 * 5 != data->center_freq1)) {
569c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
570c1d255d3SCy Schubert 				   "20/40 MHz: center segment 0 (=%d) and center freq 1 (=%d) not in sync",
571c1d255d3SCy Schubert 				   center_segment0, data->center_freq1);
572c1d255d3SCy Schubert 			return -1;
573c1d255d3SCy Schubert 		}
574c1d255d3SCy Schubert 		break;
575c1d255d3SCy Schubert 	case CHANWIDTH_80P80MHZ:
576c1d255d3SCy Schubert 		if (center_segment1 == center_segment0 + 4 ||
577c1d255d3SCy Schubert 		    center_segment1 == center_segment0 - 4) {
578c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
579c1d255d3SCy Schubert 				   "80+80 MHz: center segment 1 only 20 MHz apart");
580c1d255d3SCy Schubert 			return -1;
581c1d255d3SCy Schubert 		}
5825b9c547cSRui Paulo 		data->center_freq2 = 5000 + center_segment1 * 5;
5835b9c547cSRui Paulo 		/* fall through */
584206b73d0SCy Schubert 	case CHANWIDTH_80MHZ:
5855b9c547cSRui Paulo 		data->bandwidth = 80;
586c1d255d3SCy Schubert 		if (!sec_channel_offset) {
587c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
588c1d255d3SCy Schubert 				   "80/80+80 MHz: no second channel offset");
5895b9c547cSRui Paulo 			return -1;
590c1d255d3SCy Schubert 		}
591c1d255d3SCy Schubert 		if (oper_chwidth == CHANWIDTH_80MHZ && center_segment1) {
592c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
593c1d255d3SCy Schubert 				   "80 MHz: center segment 1 configured");
594c1d255d3SCy Schubert 			return -1;
595c1d255d3SCy Schubert 		}
596c1d255d3SCy Schubert 		if (oper_chwidth == CHANWIDTH_80P80MHZ && !center_segment1) {
597c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
598c1d255d3SCy Schubert 				   "80+80 MHz: center segment 1 not configured");
599c1d255d3SCy Schubert 			return -1;
600c1d255d3SCy Schubert 		}
601325151a3SRui Paulo 		if (!center_segment0) {
602325151a3SRui Paulo 			if (channel <= 48)
603325151a3SRui Paulo 				center_segment0 = 42;
604325151a3SRui Paulo 			else if (channel <= 64)
605325151a3SRui Paulo 				center_segment0 = 58;
606325151a3SRui Paulo 			else if (channel <= 112)
607325151a3SRui Paulo 				center_segment0 = 106;
608325151a3SRui Paulo 			else if (channel <= 128)
609325151a3SRui Paulo 				center_segment0 = 122;
610325151a3SRui Paulo 			else if (channel <= 144)
611325151a3SRui Paulo 				center_segment0 = 138;
612325151a3SRui Paulo 			else if (channel <= 161)
613325151a3SRui Paulo 				center_segment0 = 155;
614c1d255d3SCy Schubert 			else if (channel <= 177)
615c1d255d3SCy Schubert 				center_segment0 = 171;
6165b9c547cSRui Paulo 			data->center_freq1 = 5000 + center_segment0 * 5;
617325151a3SRui Paulo 		} else {
618325151a3SRui Paulo 			/*
619325151a3SRui Paulo 			 * Note: HT/VHT config and params are coupled. Check if
620325151a3SRui Paulo 			 * HT40 channel band is in VHT80 Pri channel band
621325151a3SRui Paulo 			 * configuration.
622325151a3SRui Paulo 			 */
623325151a3SRui Paulo 			if (center_segment0 == channel + 6 ||
624325151a3SRui Paulo 			    center_segment0 == channel + 2 ||
625325151a3SRui Paulo 			    center_segment0 == channel - 2 ||
626325151a3SRui Paulo 			    center_segment0 == channel - 6)
627325151a3SRui Paulo 				data->center_freq1 = 5000 + center_segment0 * 5;
628c1d255d3SCy Schubert 			else {
629c1d255d3SCy Schubert 				wpa_printf(MSG_ERROR,
630c1d255d3SCy Schubert 					   "Wrong coupling between HT and VHT/HE channel setting");
631325151a3SRui Paulo 				return -1;
632325151a3SRui Paulo 			}
633c1d255d3SCy Schubert 		}
6345b9c547cSRui Paulo 		break;
635206b73d0SCy Schubert 	case CHANWIDTH_160MHZ:
6365b9c547cSRui Paulo 		data->bandwidth = 160;
637c1d255d3SCy Schubert 		if (center_segment1) {
6385b9c547cSRui Paulo 			wpa_printf(MSG_ERROR,
639c1d255d3SCy Schubert 				   "160 MHz: center segment 1 should not be set");
6405b9c547cSRui Paulo 			return -1;
6415b9c547cSRui Paulo 		}
642c1d255d3SCy Schubert 		if (!sec_channel_offset) {
643c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
644c1d255d3SCy Schubert 				   "160 MHz: second channel offset not set");
6455b9c547cSRui Paulo 			return -1;
646c1d255d3SCy Schubert 		}
647325151a3SRui Paulo 		/*
648325151a3SRui Paulo 		 * Note: HT/VHT config and params are coupled. Check if
649325151a3SRui Paulo 		 * HT40 channel band is in VHT160 channel band configuration.
650325151a3SRui Paulo 		 */
651325151a3SRui Paulo 		if (center_segment0 == channel + 14 ||
652325151a3SRui Paulo 		    center_segment0 == channel + 10 ||
653325151a3SRui Paulo 		    center_segment0 == channel + 6 ||
654325151a3SRui Paulo 		    center_segment0 == channel + 2 ||
655325151a3SRui Paulo 		    center_segment0 == channel - 2 ||
656325151a3SRui Paulo 		    center_segment0 == channel - 6 ||
657325151a3SRui Paulo 		    center_segment0 == channel - 10 ||
658325151a3SRui Paulo 		    center_segment0 == channel - 14)
6595b9c547cSRui Paulo 			data->center_freq1 = 5000 + center_segment0 * 5;
660c1d255d3SCy Schubert 		else {
661c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
662c1d255d3SCy Schubert 				   "160 MHz: HT40 channel band is not in 160 MHz band");
663325151a3SRui Paulo 			return -1;
664c1d255d3SCy Schubert 		}
6655b9c547cSRui Paulo 		break;
6665b9c547cSRui Paulo 	}
6675b9c547cSRui Paulo 
6685b9c547cSRui Paulo 	return 0;
6695b9c547cSRui Paulo }
67085732ac8SCy Schubert 
67185732ac8SCy Schubert 
67285732ac8SCy Schubert void set_disable_ht40(struct ieee80211_ht_capabilities *htcaps,
67385732ac8SCy Schubert 		      int disabled)
67485732ac8SCy Schubert {
67585732ac8SCy Schubert 	/* Masking these out disables HT40 */
67685732ac8SCy Schubert 	le16 msk = host_to_le16(HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET |
67785732ac8SCy Schubert 				HT_CAP_INFO_SHORT_GI40MHZ);
67885732ac8SCy Schubert 
67985732ac8SCy Schubert 	if (disabled)
68085732ac8SCy Schubert 		htcaps->ht_capabilities_info &= ~msk;
68185732ac8SCy Schubert 	else
68285732ac8SCy Schubert 		htcaps->ht_capabilities_info |= msk;
68385732ac8SCy Schubert }
68485732ac8SCy Schubert 
68585732ac8SCy Schubert 
68685732ac8SCy Schubert #ifdef CONFIG_IEEE80211AC
68785732ac8SCy Schubert 
68885732ac8SCy Schubert static int _ieee80211ac_cap_check(u32 hw, u32 conf, u32 cap,
68985732ac8SCy Schubert 				  const char *name)
69085732ac8SCy Schubert {
69185732ac8SCy Schubert 	u32 req_cap = conf & cap;
69285732ac8SCy Schubert 
69385732ac8SCy Schubert 	/*
69485732ac8SCy Schubert 	 * Make sure we support all requested capabilities.
69585732ac8SCy Schubert 	 * NOTE: We assume that 'cap' represents a capability mask,
69685732ac8SCy Schubert 	 * not a discrete value.
69785732ac8SCy Schubert 	 */
69885732ac8SCy Schubert 	if ((hw & req_cap) != req_cap) {
69985732ac8SCy Schubert 		wpa_printf(MSG_ERROR,
70085732ac8SCy Schubert 			   "Driver does not support configured VHT capability [%s]",
70185732ac8SCy Schubert 			   name);
70285732ac8SCy Schubert 		return 0;
70385732ac8SCy Schubert 	}
70485732ac8SCy Schubert 	return 1;
70585732ac8SCy Schubert }
70685732ac8SCy Schubert 
70785732ac8SCy Schubert 
70885732ac8SCy Schubert static int ieee80211ac_cap_check_max(u32 hw, u32 conf, u32 mask,
70985732ac8SCy Schubert 				     unsigned int shift,
71085732ac8SCy Schubert 				     const char *name)
71185732ac8SCy Schubert {
71285732ac8SCy Schubert 	u32 hw_max = hw & mask;
71385732ac8SCy Schubert 	u32 conf_val = conf & mask;
71485732ac8SCy Schubert 
71585732ac8SCy Schubert 	if (conf_val > hw_max) {
71685732ac8SCy Schubert 		wpa_printf(MSG_ERROR,
71785732ac8SCy Schubert 			   "Configured VHT capability [%s] exceeds max value supported by the driver (%d > %d)",
71885732ac8SCy Schubert 			   name, conf_val >> shift, hw_max >> shift);
71985732ac8SCy Schubert 		return 0;
72085732ac8SCy Schubert 	}
72185732ac8SCy Schubert 	return 1;
72285732ac8SCy Schubert }
72385732ac8SCy Schubert 
72485732ac8SCy Schubert 
72585732ac8SCy Schubert int ieee80211ac_cap_check(u32 hw, u32 conf)
72685732ac8SCy Schubert {
72785732ac8SCy Schubert #define VHT_CAP_CHECK(cap) \
72885732ac8SCy Schubert 	do { \
72985732ac8SCy Schubert 		if (!_ieee80211ac_cap_check(hw, conf, cap, #cap)) \
73085732ac8SCy Schubert 			return 0; \
73185732ac8SCy Schubert 	} while (0)
73285732ac8SCy Schubert 
73385732ac8SCy Schubert #define VHT_CAP_CHECK_MAX(cap) \
73485732ac8SCy Schubert 	do { \
73585732ac8SCy Schubert 		if (!ieee80211ac_cap_check_max(hw, conf, cap, cap ## _SHIFT, \
73685732ac8SCy Schubert 					       #cap)) \
73785732ac8SCy Schubert 			return 0; \
73885732ac8SCy Schubert 	} while (0)
73985732ac8SCy Schubert 
74085732ac8SCy Schubert 	VHT_CAP_CHECK_MAX(VHT_CAP_MAX_MPDU_LENGTH_MASK);
74185732ac8SCy Schubert 	VHT_CAP_CHECK_MAX(VHT_CAP_SUPP_CHAN_WIDTH_MASK);
74285732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_RXLDPC);
74385732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_SHORT_GI_80);
74485732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_SHORT_GI_160);
74585732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_TXSTBC);
74685732ac8SCy Schubert 	VHT_CAP_CHECK_MAX(VHT_CAP_RXSTBC_MASK);
74785732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMER_CAPABLE);
74885732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMEE_CAPABLE);
74985732ac8SCy Schubert 	VHT_CAP_CHECK_MAX(VHT_CAP_BEAMFORMEE_STS_MAX);
75085732ac8SCy Schubert 	VHT_CAP_CHECK_MAX(VHT_CAP_SOUNDING_DIMENSION_MAX);
75185732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMER_CAPABLE);
75285732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMEE_CAPABLE);
75385732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_VHT_TXOP_PS);
75485732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_HTC_VHT);
75585732ac8SCy Schubert 	VHT_CAP_CHECK_MAX(VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX);
75685732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_UNSOL_MFB);
75785732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_MRQ_MFB);
75885732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_RX_ANTENNA_PATTERN);
75985732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_TX_ANTENNA_PATTERN);
76085732ac8SCy Schubert 
76185732ac8SCy Schubert #undef VHT_CAP_CHECK
76285732ac8SCy Schubert #undef VHT_CAP_CHECK_MAX
76385732ac8SCy Schubert 
76485732ac8SCy Schubert 	return 1;
76585732ac8SCy Schubert }
76685732ac8SCy Schubert 
76785732ac8SCy Schubert #endif /* CONFIG_IEEE80211AC */
7684bc52338SCy Schubert 
7694bc52338SCy Schubert 
7704bc52338SCy Schubert u32 num_chan_to_bw(int num_chans)
7714bc52338SCy Schubert {
7724bc52338SCy Schubert 	switch (num_chans) {
7734bc52338SCy Schubert 	case 2:
7744bc52338SCy Schubert 	case 4:
7754bc52338SCy Schubert 	case 8:
7764bc52338SCy Schubert 		return num_chans * 20;
7774bc52338SCy Schubert 	default:
7784bc52338SCy Schubert 		return 20;
7794bc52338SCy Schubert 	}
7804bc52338SCy Schubert }
7814bc52338SCy Schubert 
7824bc52338SCy Schubert 
7834bc52338SCy Schubert /* check if BW is applicable for channel */
7844bc52338SCy Schubert int chan_bw_allowed(const struct hostapd_channel_data *chan, u32 bw,
7854bc52338SCy Schubert 		    int ht40_plus, int pri)
7864bc52338SCy Schubert {
7874bc52338SCy Schubert 	u32 bw_mask;
7884bc52338SCy Schubert 
7894bc52338SCy Schubert 	switch (bw) {
7904bc52338SCy Schubert 	case 20:
7914bc52338SCy Schubert 		bw_mask = HOSTAPD_CHAN_WIDTH_20;
7924bc52338SCy Schubert 		break;
7934bc52338SCy Schubert 	case 40:
7944bc52338SCy Schubert 		/* HT 40 MHz support declared only for primary channel,
7954bc52338SCy Schubert 		 * just skip 40 MHz secondary checking */
7964bc52338SCy Schubert 		if (pri && ht40_plus)
7974bc52338SCy Schubert 			bw_mask = HOSTAPD_CHAN_WIDTH_40P;
7984bc52338SCy Schubert 		else if (pri && !ht40_plus)
7994bc52338SCy Schubert 			bw_mask = HOSTAPD_CHAN_WIDTH_40M;
8004bc52338SCy Schubert 		else
8014bc52338SCy Schubert 			bw_mask = 0;
8024bc52338SCy Schubert 		break;
8034bc52338SCy Schubert 	case 80:
8044bc52338SCy Schubert 		bw_mask = HOSTAPD_CHAN_WIDTH_80;
8054bc52338SCy Schubert 		break;
8064bc52338SCy Schubert 	case 160:
8074bc52338SCy Schubert 		bw_mask = HOSTAPD_CHAN_WIDTH_160;
8084bc52338SCy Schubert 		break;
8094bc52338SCy Schubert 	default:
8104bc52338SCy Schubert 		bw_mask = 0;
8114bc52338SCy Schubert 		break;
8124bc52338SCy Schubert 	}
8134bc52338SCy Schubert 
8144bc52338SCy Schubert 	return (chan->allowed_bw & bw_mask) == bw_mask;
8154bc52338SCy Schubert }
8164bc52338SCy Schubert 
8174bc52338SCy Schubert 
8184bc52338SCy Schubert /* check if channel is allowed to be used as primary */
8194bc52338SCy Schubert int chan_pri_allowed(const struct hostapd_channel_data *chan)
8204bc52338SCy Schubert {
8214bc52338SCy Schubert 	return !(chan->flag & HOSTAPD_CHAN_DISABLED) &&
8224bc52338SCy Schubert 		(chan->allowed_bw & HOSTAPD_CHAN_WIDTH_20);
8234bc52338SCy Schubert }
824