xref: /linux/drivers/net/wireless/broadcom/brcm80211/brcmsmac/channel.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
105491d2cSKalle Valo /*
205491d2cSKalle Valo  * Copyright (c) 2010 Broadcom Corporation
305491d2cSKalle Valo  *
405491d2cSKalle Valo  * Permission to use, copy, modify, and/or distribute this software for any
505491d2cSKalle Valo  * purpose with or without fee is hereby granted, provided that the above
605491d2cSKalle Valo  * copyright notice and this permission notice appear in all copies.
705491d2cSKalle Valo  *
805491d2cSKalle Valo  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
905491d2cSKalle Valo  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1005491d2cSKalle Valo  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
1105491d2cSKalle Valo  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1205491d2cSKalle Valo  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
1305491d2cSKalle Valo  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
1405491d2cSKalle Valo  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1505491d2cSKalle Valo  */
1605491d2cSKalle Valo 
1705491d2cSKalle Valo #include <linux/types.h>
1805491d2cSKalle Valo #include <net/cfg80211.h>
1905491d2cSKalle Valo #include <net/mac80211.h>
2005491d2cSKalle Valo #include <net/regulatory.h>
2105491d2cSKalle Valo 
2205491d2cSKalle Valo #include <defs.h>
2305491d2cSKalle Valo #include "pub.h"
2405491d2cSKalle Valo #include "phy/phy_hal.h"
2505491d2cSKalle Valo #include "main.h"
2605491d2cSKalle Valo #include "stf.h"
2705491d2cSKalle Valo #include "channel.h"
2805491d2cSKalle Valo #include "mac80211_if.h"
2905491d2cSKalle Valo #include "debug.h"
3005491d2cSKalle Valo 
3105491d2cSKalle Valo /* QDB() macro takes a dB value and converts to a quarter dB value */
3205491d2cSKalle Valo #define QDB(n) ((n) * BRCMS_TXPWR_DB_FACTOR)
3305491d2cSKalle Valo 
3405491d2cSKalle Valo #define LOCALE_MIMO_IDX_bn		0
3505491d2cSKalle Valo #define LOCALE_MIMO_IDX_11n		0
3605491d2cSKalle Valo 
3705491d2cSKalle Valo /* max of BAND_5G_PWR_LVLS and 14 for 2.4 GHz */
3805491d2cSKalle Valo #define BRCMS_MAXPWR_MIMO_TBL_SIZE	14
3905491d2cSKalle Valo 
4005491d2cSKalle Valo /* maxpwr mapping to 5GHz band channels:
4105491d2cSKalle Valo  * maxpwr[0] - channels [34-48]
4205491d2cSKalle Valo  * maxpwr[1] - channels [52-60]
4305491d2cSKalle Valo  * maxpwr[2] - channels [62-64]
4405491d2cSKalle Valo  * maxpwr[3] - channels [100-140]
4505491d2cSKalle Valo  * maxpwr[4] - channels [149-165]
4605491d2cSKalle Valo  */
4705491d2cSKalle Valo #define BAND_5G_PWR_LVLS	5	/* 5 power levels for 5G */
4805491d2cSKalle Valo 
4905491d2cSKalle Valo #define LC(id)	LOCALE_MIMO_IDX_ ## id
5005491d2cSKalle Valo 
5105491d2cSKalle Valo #define LOCALES(mimo2, mimo5) \
5205491d2cSKalle Valo 		{LC(mimo2), LC(mimo5)}
5305491d2cSKalle Valo 
5405491d2cSKalle Valo /* macro to get 5 GHz channel group index for tx power */
5505491d2cSKalle Valo #define CHANNEL_POWER_IDX_5G(c) (((c) < 52) ? 0 : \
5605491d2cSKalle Valo 				 (((c) < 62) ? 1 : \
5705491d2cSKalle Valo 				 (((c) < 100) ? 2 : \
5805491d2cSKalle Valo 				 (((c) < 149) ? 3 : 4))))
5905491d2cSKalle Valo 
6005491d2cSKalle Valo #define BRCM_2GHZ_2412_2462	REG_RULE(2412-10, 2462+10, 40, 0, 19, 0)
6105491d2cSKalle Valo #define BRCM_2GHZ_2467_2472	REG_RULE(2467-10, 2472+10, 20, 0, 19, \
6205491d2cSKalle Valo 					 NL80211_RRF_NO_IR)
6305491d2cSKalle Valo 
6405491d2cSKalle Valo #define BRCM_5GHZ_5180_5240	REG_RULE(5180-10, 5240+10, 40, 0, 21, \
6505491d2cSKalle Valo 					 NL80211_RRF_NO_IR)
6605491d2cSKalle Valo #define BRCM_5GHZ_5260_5320	REG_RULE(5260-10, 5320+10, 40, 0, 21, \
6705491d2cSKalle Valo 					 NL80211_RRF_DFS | \
6805491d2cSKalle Valo 					 NL80211_RRF_NO_IR)
6905491d2cSKalle Valo #define BRCM_5GHZ_5500_5700	REG_RULE(5500-10, 5700+10, 40, 0, 21, \
7005491d2cSKalle Valo 					 NL80211_RRF_DFS | \
7105491d2cSKalle Valo 					 NL80211_RRF_NO_IR)
7205491d2cSKalle Valo #define BRCM_5GHZ_5745_5825	REG_RULE(5745-10, 5825+10, 40, 0, 21, \
7305491d2cSKalle Valo 					 NL80211_RRF_NO_IR)
7405491d2cSKalle Valo 
7505491d2cSKalle Valo static const struct ieee80211_regdomain brcms_regdom_x2 = {
7605491d2cSKalle Valo 	.n_reg_rules = 6,
7705491d2cSKalle Valo 	.alpha2 = "X2",
7805491d2cSKalle Valo 	.reg_rules = {
7905491d2cSKalle Valo 		BRCM_2GHZ_2412_2462,
8005491d2cSKalle Valo 		BRCM_2GHZ_2467_2472,
8105491d2cSKalle Valo 		BRCM_5GHZ_5180_5240,
8205491d2cSKalle Valo 		BRCM_5GHZ_5260_5320,
8305491d2cSKalle Valo 		BRCM_5GHZ_5500_5700,
8405491d2cSKalle Valo 		BRCM_5GHZ_5745_5825,
8505491d2cSKalle Valo 	}
8605491d2cSKalle Valo };
8705491d2cSKalle Valo 
8805491d2cSKalle Valo  /* locale per-channel tx power limits for MIMO frames
8905491d2cSKalle Valo   * maxpwr arrays are index by channel for 2.4 GHz limits, and
9005491d2cSKalle Valo   * by sub-band for 5 GHz limits using CHANNEL_POWER_IDX_5G(channel)
9105491d2cSKalle Valo   */
9205491d2cSKalle Valo struct locale_mimo_info {
9305491d2cSKalle Valo 	/* tx 20 MHz power limits, qdBm units */
9405491d2cSKalle Valo 	s8 maxpwr20[BRCMS_MAXPWR_MIMO_TBL_SIZE];
9505491d2cSKalle Valo 	/* tx 40 MHz power limits, qdBm units */
9605491d2cSKalle Valo 	s8 maxpwr40[BRCMS_MAXPWR_MIMO_TBL_SIZE];
9705491d2cSKalle Valo };
9805491d2cSKalle Valo 
9905491d2cSKalle Valo /* Country names and abbreviations with locale defined from ISO 3166 */
10005491d2cSKalle Valo struct country_info {
10105491d2cSKalle Valo 	const u8 locale_mimo_2G;	/* 2.4G mimo info */
10205491d2cSKalle Valo 	const u8 locale_mimo_5G;	/* 5G mimo info */
10305491d2cSKalle Valo };
10405491d2cSKalle Valo 
10505491d2cSKalle Valo struct brcms_regd {
10605491d2cSKalle Valo 	struct country_info country;
10705491d2cSKalle Valo 	const struct ieee80211_regdomain *regdomain;
10805491d2cSKalle Valo };
10905491d2cSKalle Valo 
11005491d2cSKalle Valo struct brcms_cm_info {
11105491d2cSKalle Valo 	struct brcms_pub *pub;
11205491d2cSKalle Valo 	struct brcms_c_info *wlc;
11305491d2cSKalle Valo 	const struct brcms_regd *world_regd;
11405491d2cSKalle Valo };
11505491d2cSKalle Valo 
11605491d2cSKalle Valo /*
11705491d2cSKalle Valo  * MIMO Locale Definitions - 2.4 GHz
11805491d2cSKalle Valo  */
11905491d2cSKalle Valo static const struct locale_mimo_info locale_bn = {
12005491d2cSKalle Valo 	{QDB(13), QDB(13), QDB(13), QDB(13), QDB(13),
12105491d2cSKalle Valo 	 QDB(13), QDB(13), QDB(13), QDB(13), QDB(13),
12205491d2cSKalle Valo 	 QDB(13), QDB(13), QDB(13)},
12305491d2cSKalle Valo 	{0, 0, QDB(13), QDB(13), QDB(13),
12405491d2cSKalle Valo 	 QDB(13), QDB(13), QDB(13), QDB(13), QDB(13),
12505491d2cSKalle Valo 	 QDB(13), 0, 0},
12605491d2cSKalle Valo };
12705491d2cSKalle Valo 
12805491d2cSKalle Valo static const struct locale_mimo_info *g_mimo_2g_table[] = {
12905491d2cSKalle Valo 	&locale_bn
13005491d2cSKalle Valo };
13105491d2cSKalle Valo 
13205491d2cSKalle Valo /*
13305491d2cSKalle Valo  * MIMO Locale Definitions - 5 GHz
13405491d2cSKalle Valo  */
13505491d2cSKalle Valo static const struct locale_mimo_info locale_11n = {
13605491d2cSKalle Valo 	{ /* 12.5 dBm */ 50, 50, 50, QDB(15), QDB(15)},
13705491d2cSKalle Valo 	{QDB(14), QDB(15), QDB(15), QDB(15), QDB(15)},
13805491d2cSKalle Valo };
13905491d2cSKalle Valo 
14005491d2cSKalle Valo static const struct locale_mimo_info *g_mimo_5g_table[] = {
14105491d2cSKalle Valo 	&locale_11n
14205491d2cSKalle Valo };
14305491d2cSKalle Valo 
14405491d2cSKalle Valo static const struct brcms_regd cntry_locales[] = {
14505491d2cSKalle Valo 	/* Worldwide RoW 2, must always be at index 0 */
14605491d2cSKalle Valo 	{
14705491d2cSKalle Valo 		.country = LOCALES(bn, 11n),
14805491d2cSKalle Valo 		.regdomain = &brcms_regdom_x2,
14905491d2cSKalle Valo 	},
15005491d2cSKalle Valo };
15105491d2cSKalle Valo 
brcms_c_get_mimo_2g(u8 locale_idx)15205491d2cSKalle Valo static const struct locale_mimo_info *brcms_c_get_mimo_2g(u8 locale_idx)
15305491d2cSKalle Valo {
15405491d2cSKalle Valo 	if (locale_idx >= ARRAY_SIZE(g_mimo_2g_table))
15505491d2cSKalle Valo 		return NULL;
15605491d2cSKalle Valo 
15705491d2cSKalle Valo 	return g_mimo_2g_table[locale_idx];
15805491d2cSKalle Valo }
15905491d2cSKalle Valo 
brcms_c_get_mimo_5g(u8 locale_idx)16005491d2cSKalle Valo static const struct locale_mimo_info *brcms_c_get_mimo_5g(u8 locale_idx)
16105491d2cSKalle Valo {
16205491d2cSKalle Valo 	if (locale_idx >= ARRAY_SIZE(g_mimo_5g_table))
16305491d2cSKalle Valo 		return NULL;
16405491d2cSKalle Valo 
16505491d2cSKalle Valo 	return g_mimo_5g_table[locale_idx];
16605491d2cSKalle Valo }
16705491d2cSKalle Valo 
16805491d2cSKalle Valo /*
16905491d2cSKalle Valo  * Indicates whether the country provided is valid to pass
17005491d2cSKalle Valo  * to cfg80211 or not.
17105491d2cSKalle Valo  *
17205491d2cSKalle Valo  * returns true if valid; false if not.
17305491d2cSKalle Valo  */
brcms_c_country_valid(const char * ccode)17405491d2cSKalle Valo static bool brcms_c_country_valid(const char *ccode)
17505491d2cSKalle Valo {
17605491d2cSKalle Valo 	/*
17705491d2cSKalle Valo 	 * only allow ascii alpha uppercase for the first 2
17805491d2cSKalle Valo 	 * chars.
17905491d2cSKalle Valo 	 */
180631d464dSJulia Lawall 	if (!((ccode[0] & 0x80) == 0 && ccode[0] >= 0x41 && ccode[0] <= 0x5A &&
181631d464dSJulia Lawall 	      (ccode[1] & 0x80) == 0 && ccode[1] >= 0x41 && ccode[1] <= 0x5A))
18205491d2cSKalle Valo 		return false;
18305491d2cSKalle Valo 
18405491d2cSKalle Valo 	/*
18505491d2cSKalle Valo 	 * do not match ISO 3166-1 user assigned country codes
18605491d2cSKalle Valo 	 * that may be in the driver table
18705491d2cSKalle Valo 	 */
18805491d2cSKalle Valo 	if (!strcmp("AA", ccode) ||        /* AA */
18905491d2cSKalle Valo 	    !strcmp("ZZ", ccode) ||        /* ZZ */
19005491d2cSKalle Valo 	    ccode[0] == 'X' ||             /* XA - XZ */
19105491d2cSKalle Valo 	    (ccode[0] == 'Q' &&            /* QM - QZ */
19205491d2cSKalle Valo 	     (ccode[1] >= 'M' && ccode[1] <= 'Z')))
19305491d2cSKalle Valo 		return false;
19405491d2cSKalle Valo 
19505491d2cSKalle Valo 	if (!strcmp("NA", ccode))
19605491d2cSKalle Valo 		return false;
19705491d2cSKalle Valo 
19805491d2cSKalle Valo 	return true;
19905491d2cSKalle Valo }
20005491d2cSKalle Valo 
brcms_world_regd(const char * regdom,int len)20105491d2cSKalle Valo static const struct brcms_regd *brcms_world_regd(const char *regdom, int len)
20205491d2cSKalle Valo {
20305491d2cSKalle Valo 	const struct brcms_regd *regd = NULL;
20405491d2cSKalle Valo 	int i;
20505491d2cSKalle Valo 
20605491d2cSKalle Valo 	for (i = 0; i < ARRAY_SIZE(cntry_locales); i++) {
20705491d2cSKalle Valo 		if (!strncmp(regdom, cntry_locales[i].regdomain->alpha2, len)) {
20805491d2cSKalle Valo 			regd = &cntry_locales[i];
20905491d2cSKalle Valo 			break;
21005491d2cSKalle Valo 		}
21105491d2cSKalle Valo 	}
21205491d2cSKalle Valo 
21305491d2cSKalle Valo 	return regd;
21405491d2cSKalle Valo }
21505491d2cSKalle Valo 
brcms_default_world_regd(void)21605491d2cSKalle Valo static const struct brcms_regd *brcms_default_world_regd(void)
21705491d2cSKalle Valo {
21805491d2cSKalle Valo 	return &cntry_locales[0];
21905491d2cSKalle Valo }
22005491d2cSKalle Valo 
22105491d2cSKalle Valo /* JP, J1 - J10 are Japan ccodes */
brcms_c_japan_ccode(const char * ccode)22205491d2cSKalle Valo static bool brcms_c_japan_ccode(const char *ccode)
22305491d2cSKalle Valo {
22405491d2cSKalle Valo 	return (ccode[0] == 'J' &&
22505491d2cSKalle Valo 		(ccode[1] == 'P' || (ccode[1] >= '1' && ccode[1] <= '9')));
22605491d2cSKalle Valo }
22705491d2cSKalle Valo 
22805491d2cSKalle Valo static void
brcms_c_channel_min_txpower_limits_with_local_constraint(struct brcms_cm_info * wlc_cm,struct txpwr_limits * txpwr,u8 local_constraint_qdbm)22905491d2cSKalle Valo brcms_c_channel_min_txpower_limits_with_local_constraint(
23005491d2cSKalle Valo 		struct brcms_cm_info *wlc_cm, struct txpwr_limits *txpwr,
23105491d2cSKalle Valo 		u8 local_constraint_qdbm)
23205491d2cSKalle Valo {
23305491d2cSKalle Valo 	int j;
23405491d2cSKalle Valo 
23505491d2cSKalle Valo 	/* CCK Rates */
23605491d2cSKalle Valo 	for (j = 0; j < WL_TX_POWER_CCK_NUM; j++)
23705491d2cSKalle Valo 		txpwr->cck[j] = min(txpwr->cck[j], local_constraint_qdbm);
23805491d2cSKalle Valo 
23905491d2cSKalle Valo 	/* 20 MHz Legacy OFDM SISO */
24005491d2cSKalle Valo 	for (j = 0; j < WL_TX_POWER_OFDM_NUM; j++)
24105491d2cSKalle Valo 		txpwr->ofdm[j] = min(txpwr->ofdm[j], local_constraint_qdbm);
24205491d2cSKalle Valo 
24305491d2cSKalle Valo 	/* 20 MHz Legacy OFDM CDD */
24405491d2cSKalle Valo 	for (j = 0; j < BRCMS_NUM_RATES_OFDM; j++)
24505491d2cSKalle Valo 		txpwr->ofdm_cdd[j] =
24605491d2cSKalle Valo 		    min(txpwr->ofdm_cdd[j], local_constraint_qdbm);
24705491d2cSKalle Valo 
24805491d2cSKalle Valo 	/* 40 MHz Legacy OFDM SISO */
24905491d2cSKalle Valo 	for (j = 0; j < BRCMS_NUM_RATES_OFDM; j++)
25005491d2cSKalle Valo 		txpwr->ofdm_40_siso[j] =
25105491d2cSKalle Valo 		    min(txpwr->ofdm_40_siso[j], local_constraint_qdbm);
25205491d2cSKalle Valo 
25305491d2cSKalle Valo 	/* 40 MHz Legacy OFDM CDD */
25405491d2cSKalle Valo 	for (j = 0; j < BRCMS_NUM_RATES_OFDM; j++)
25505491d2cSKalle Valo 		txpwr->ofdm_40_cdd[j] =
25605491d2cSKalle Valo 		    min(txpwr->ofdm_40_cdd[j], local_constraint_qdbm);
25705491d2cSKalle Valo 
25805491d2cSKalle Valo 	/* 20MHz MCS 0-7 SISO */
25905491d2cSKalle Valo 	for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
26005491d2cSKalle Valo 		txpwr->mcs_20_siso[j] =
26105491d2cSKalle Valo 		    min(txpwr->mcs_20_siso[j], local_constraint_qdbm);
26205491d2cSKalle Valo 
26305491d2cSKalle Valo 	/* 20MHz MCS 0-7 CDD */
26405491d2cSKalle Valo 	for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
26505491d2cSKalle Valo 		txpwr->mcs_20_cdd[j] =
26605491d2cSKalle Valo 		    min(txpwr->mcs_20_cdd[j], local_constraint_qdbm);
26705491d2cSKalle Valo 
26805491d2cSKalle Valo 	/* 20MHz MCS 0-7 STBC */
26905491d2cSKalle Valo 	for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
27005491d2cSKalle Valo 		txpwr->mcs_20_stbc[j] =
27105491d2cSKalle Valo 		    min(txpwr->mcs_20_stbc[j], local_constraint_qdbm);
27205491d2cSKalle Valo 
27305491d2cSKalle Valo 	/* 20MHz MCS 8-15 MIMO */
27405491d2cSKalle Valo 	for (j = 0; j < BRCMS_NUM_RATES_MCS_2_STREAM; j++)
27505491d2cSKalle Valo 		txpwr->mcs_20_mimo[j] =
27605491d2cSKalle Valo 		    min(txpwr->mcs_20_mimo[j], local_constraint_qdbm);
27705491d2cSKalle Valo 
27805491d2cSKalle Valo 	/* 40MHz MCS 0-7 SISO */
27905491d2cSKalle Valo 	for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
28005491d2cSKalle Valo 		txpwr->mcs_40_siso[j] =
28105491d2cSKalle Valo 		    min(txpwr->mcs_40_siso[j], local_constraint_qdbm);
28205491d2cSKalle Valo 
28305491d2cSKalle Valo 	/* 40MHz MCS 0-7 CDD */
28405491d2cSKalle Valo 	for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
28505491d2cSKalle Valo 		txpwr->mcs_40_cdd[j] =
28605491d2cSKalle Valo 		    min(txpwr->mcs_40_cdd[j], local_constraint_qdbm);
28705491d2cSKalle Valo 
28805491d2cSKalle Valo 	/* 40MHz MCS 0-7 STBC */
28905491d2cSKalle Valo 	for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
29005491d2cSKalle Valo 		txpwr->mcs_40_stbc[j] =
29105491d2cSKalle Valo 		    min(txpwr->mcs_40_stbc[j], local_constraint_qdbm);
29205491d2cSKalle Valo 
29305491d2cSKalle Valo 	/* 40MHz MCS 8-15 MIMO */
29405491d2cSKalle Valo 	for (j = 0; j < BRCMS_NUM_RATES_MCS_2_STREAM; j++)
29505491d2cSKalle Valo 		txpwr->mcs_40_mimo[j] =
29605491d2cSKalle Valo 		    min(txpwr->mcs_40_mimo[j], local_constraint_qdbm);
29705491d2cSKalle Valo 
29805491d2cSKalle Valo 	/* 40MHz MCS 32 */
29905491d2cSKalle Valo 	txpwr->mcs32 = min(txpwr->mcs32, local_constraint_qdbm);
30005491d2cSKalle Valo 
30105491d2cSKalle Valo }
30205491d2cSKalle Valo 
30305491d2cSKalle Valo /*
30405491d2cSKalle Valo  * set the driver's current country and regulatory information
30505491d2cSKalle Valo  * using a country code as the source. Look up built in country
30605491d2cSKalle Valo  * information found with the country code.
30705491d2cSKalle Valo  */
30805491d2cSKalle Valo static void
brcms_c_set_country(struct brcms_cm_info * wlc_cm,const struct brcms_regd * regd)30905491d2cSKalle Valo brcms_c_set_country(struct brcms_cm_info *wlc_cm,
31005491d2cSKalle Valo 		    const struct brcms_regd *regd)
31105491d2cSKalle Valo {
31205491d2cSKalle Valo 	struct brcms_c_info *wlc = wlc_cm->wlc;
31305491d2cSKalle Valo 
31405491d2cSKalle Valo 	if ((wlc->pub->_n_enab & SUPPORT_11N) !=
31505491d2cSKalle Valo 	    wlc->protection->nmode_user)
31605491d2cSKalle Valo 		brcms_c_set_nmode(wlc);
31705491d2cSKalle Valo 
31805491d2cSKalle Valo 	brcms_c_stf_ss_update(wlc, wlc->bandstate[BAND_2G_INDEX]);
31905491d2cSKalle Valo 	brcms_c_stf_ss_update(wlc, wlc->bandstate[BAND_5G_INDEX]);
32005491d2cSKalle Valo 
32105491d2cSKalle Valo 	brcms_c_set_gmode(wlc, wlc->protection->gmode_user, false);
32205491d2cSKalle Valo 
32305491d2cSKalle Valo 	return;
32405491d2cSKalle Valo }
32505491d2cSKalle Valo 
brcms_c_channel_mgr_attach(struct brcms_c_info * wlc)32605491d2cSKalle Valo struct brcms_cm_info *brcms_c_channel_mgr_attach(struct brcms_c_info *wlc)
32705491d2cSKalle Valo {
32805491d2cSKalle Valo 	struct brcms_cm_info *wlc_cm;
32905491d2cSKalle Valo 	struct brcms_pub *pub = wlc->pub;
33005491d2cSKalle Valo 	struct ssb_sprom *sprom = &wlc->hw->d11core->bus->sprom;
33105491d2cSKalle Valo 	const char *ccode = sprom->alpha2;
33205491d2cSKalle Valo 	int ccode_len = sizeof(sprom->alpha2);
33305491d2cSKalle Valo 
334*dcb77f85SErick Archer 	wlc_cm = kzalloc(sizeof(*wlc_cm), GFP_ATOMIC);
33505491d2cSKalle Valo 	if (wlc_cm == NULL)
33605491d2cSKalle Valo 		return NULL;
33705491d2cSKalle Valo 	wlc_cm->pub = pub;
33805491d2cSKalle Valo 	wlc_cm->wlc = wlc;
33905491d2cSKalle Valo 	wlc->cmi = wlc_cm;
34005491d2cSKalle Valo 
34105491d2cSKalle Valo 	/* store the country code for passing up as a regulatory hint */
34205491d2cSKalle Valo 	wlc_cm->world_regd = brcms_world_regd(ccode, ccode_len);
34305491d2cSKalle Valo 	if (brcms_c_country_valid(ccode))
344a614f957SJustin Stitt 		memcpy(wlc->pub->srom_ccode, ccode, ccode_len);
34505491d2cSKalle Valo 
34605491d2cSKalle Valo 	/*
34705491d2cSKalle Valo 	 * If no custom world domain is found in the SROM, use the
34805491d2cSKalle Valo 	 * default "X2" domain.
34905491d2cSKalle Valo 	 */
35005491d2cSKalle Valo 	if (!wlc_cm->world_regd) {
35105491d2cSKalle Valo 		wlc_cm->world_regd = brcms_default_world_regd();
35205491d2cSKalle Valo 		ccode = wlc_cm->world_regd->regdomain->alpha2;
35305491d2cSKalle Valo 		ccode_len = BRCM_CNTRY_BUF_SZ - 1;
35405491d2cSKalle Valo 	}
35505491d2cSKalle Valo 
35605491d2cSKalle Valo 	/* save default country for exiting 11d regulatory mode */
357a614f957SJustin Stitt 	memcpy(wlc->country_default, ccode, ccode_len);
35805491d2cSKalle Valo 
35905491d2cSKalle Valo 	/* initialize autocountry_default to driver default */
360a614f957SJustin Stitt 	memcpy(wlc->autocountry_default, ccode, ccode_len);
36105491d2cSKalle Valo 
36205491d2cSKalle Valo 	brcms_c_set_country(wlc_cm, wlc_cm->world_regd);
36305491d2cSKalle Valo 
36405491d2cSKalle Valo 	return wlc_cm;
36505491d2cSKalle Valo }
36605491d2cSKalle Valo 
brcms_c_channel_mgr_detach(struct brcms_cm_info * wlc_cm)36705491d2cSKalle Valo void brcms_c_channel_mgr_detach(struct brcms_cm_info *wlc_cm)
36805491d2cSKalle Valo {
36905491d2cSKalle Valo 	kfree(wlc_cm);
37005491d2cSKalle Valo }
37105491d2cSKalle Valo 
37205491d2cSKalle Valo void
brcms_c_channel_set_chanspec(struct brcms_cm_info * wlc_cm,u16 chanspec,u8 local_constraint_qdbm)37305491d2cSKalle Valo brcms_c_channel_set_chanspec(struct brcms_cm_info *wlc_cm, u16 chanspec,
37405491d2cSKalle Valo 			 u8 local_constraint_qdbm)
37505491d2cSKalle Valo {
37605491d2cSKalle Valo 	struct brcms_c_info *wlc = wlc_cm->wlc;
37705491d2cSKalle Valo 	struct ieee80211_channel *ch = wlc->pub->ieee_hw->conf.chandef.chan;
37805491d2cSKalle Valo 	struct txpwr_limits txpwr;
37905491d2cSKalle Valo 
38005491d2cSKalle Valo 	brcms_c_channel_reg_limits(wlc_cm, chanspec, &txpwr);
38105491d2cSKalle Valo 
38205491d2cSKalle Valo 	brcms_c_channel_min_txpower_limits_with_local_constraint(
38305491d2cSKalle Valo 		wlc_cm, &txpwr, local_constraint_qdbm
38405491d2cSKalle Valo 	);
38505491d2cSKalle Valo 
38605491d2cSKalle Valo 	/* set or restore gmode as required by regulatory */
38705491d2cSKalle Valo 	if (ch->flags & IEEE80211_CHAN_NO_OFDM)
38805491d2cSKalle Valo 		brcms_c_set_gmode(wlc, GMODE_LEGACY_B, false);
38905491d2cSKalle Valo 	else
39005491d2cSKalle Valo 		brcms_c_set_gmode(wlc, wlc->protection->gmode_user, false);
39105491d2cSKalle Valo 
39205491d2cSKalle Valo 	brcms_b_set_chanspec(wlc->hw, chanspec,
39305491d2cSKalle Valo 			      !!(ch->flags & IEEE80211_CHAN_NO_IR),
39405491d2cSKalle Valo 			      &txpwr);
39505491d2cSKalle Valo }
39605491d2cSKalle Valo 
39705491d2cSKalle Valo void
brcms_c_channel_reg_limits(struct brcms_cm_info * wlc_cm,u16 chanspec,struct txpwr_limits * txpwr)39805491d2cSKalle Valo brcms_c_channel_reg_limits(struct brcms_cm_info *wlc_cm, u16 chanspec,
39905491d2cSKalle Valo 		       struct txpwr_limits *txpwr)
40005491d2cSKalle Valo {
40105491d2cSKalle Valo 	struct brcms_c_info *wlc = wlc_cm->wlc;
40205491d2cSKalle Valo 	struct ieee80211_channel *ch = wlc->pub->ieee_hw->conf.chandef.chan;
40305491d2cSKalle Valo 	uint i;
40405491d2cSKalle Valo 	uint chan;
40505491d2cSKalle Valo 	int maxpwr;
40605491d2cSKalle Valo 	int delta;
40705491d2cSKalle Valo 	const struct country_info *country;
40805491d2cSKalle Valo 	struct brcms_band *band;
40905491d2cSKalle Valo 	int conducted_max = BRCMS_TXPWR_MAX;
41005491d2cSKalle Valo 	const struct locale_mimo_info *li_mimo;
41105491d2cSKalle Valo 	int maxpwr20, maxpwr40;
41205491d2cSKalle Valo 	int maxpwr_idx;
41305491d2cSKalle Valo 	uint j;
41405491d2cSKalle Valo 
41505491d2cSKalle Valo 	memset(txpwr, 0, sizeof(struct txpwr_limits));
41605491d2cSKalle Valo 
41705491d2cSKalle Valo 	if (WARN_ON(!ch))
41805491d2cSKalle Valo 		return;
41905491d2cSKalle Valo 
42005491d2cSKalle Valo 	country = &wlc_cm->world_regd->country;
42105491d2cSKalle Valo 
42205491d2cSKalle Valo 	chan = CHSPEC_CHANNEL(chanspec);
42305491d2cSKalle Valo 	band = wlc->bandstate[chspec_bandunit(chanspec)];
42405491d2cSKalle Valo 	li_mimo = (band->bandtype == BRCM_BAND_5G) ?
42505491d2cSKalle Valo 	    brcms_c_get_mimo_5g(country->locale_mimo_5G) :
42605491d2cSKalle Valo 	    brcms_c_get_mimo_2g(country->locale_mimo_2G);
42705491d2cSKalle Valo 
42805491d2cSKalle Valo 	delta = band->antgain;
42905491d2cSKalle Valo 
43005491d2cSKalle Valo 	if (band->bandtype == BRCM_BAND_2G)
43105491d2cSKalle Valo 		conducted_max = QDB(22);
43205491d2cSKalle Valo 
43305491d2cSKalle Valo 	maxpwr = QDB(ch->max_power) - delta;
43405491d2cSKalle Valo 	maxpwr = max(maxpwr, 0);
43505491d2cSKalle Valo 	maxpwr = min(maxpwr, conducted_max);
43605491d2cSKalle Valo 
43705491d2cSKalle Valo 	/* CCK txpwr limits for 2.4G band */
43805491d2cSKalle Valo 	if (band->bandtype == BRCM_BAND_2G) {
43905491d2cSKalle Valo 		for (i = 0; i < BRCMS_NUM_RATES_CCK; i++)
44005491d2cSKalle Valo 			txpwr->cck[i] = (u8) maxpwr;
44105491d2cSKalle Valo 	}
44205491d2cSKalle Valo 
44305491d2cSKalle Valo 	for (i = 0; i < BRCMS_NUM_RATES_OFDM; i++) {
44405491d2cSKalle Valo 		txpwr->ofdm[i] = (u8) maxpwr;
44505491d2cSKalle Valo 
44605491d2cSKalle Valo 		/*
44705491d2cSKalle Valo 		 * OFDM 40 MHz SISO has the same power as the corresponding
44805491d2cSKalle Valo 		 * MCS0-7 rate unless overriden by the locale specific code.
44905491d2cSKalle Valo 		 * We set this value to 0 as a flag (presumably 0 dBm isn't
45005491d2cSKalle Valo 		 * a possibility) and then copy the MCS0-7 value to the 40 MHz
45105491d2cSKalle Valo 		 * value if it wasn't explicitly set.
45205491d2cSKalle Valo 		 */
45305491d2cSKalle Valo 		txpwr->ofdm_40_siso[i] = 0;
45405491d2cSKalle Valo 
45505491d2cSKalle Valo 		txpwr->ofdm_cdd[i] = (u8) maxpwr;
45605491d2cSKalle Valo 
45705491d2cSKalle Valo 		txpwr->ofdm_40_cdd[i] = 0;
45805491d2cSKalle Valo 	}
45905491d2cSKalle Valo 
46005491d2cSKalle Valo 	delta = 0;
46105491d2cSKalle Valo 	if (band->antgain > QDB(6))
46205491d2cSKalle Valo 		delta = band->antgain - QDB(6);	/* Excess over 6 dB */
46305491d2cSKalle Valo 
46405491d2cSKalle Valo 	if (band->bandtype == BRCM_BAND_2G)
46505491d2cSKalle Valo 		maxpwr_idx = (chan - 1);
46605491d2cSKalle Valo 	else
46705491d2cSKalle Valo 		maxpwr_idx = CHANNEL_POWER_IDX_5G(chan);
46805491d2cSKalle Valo 
46905491d2cSKalle Valo 	maxpwr20 = li_mimo->maxpwr20[maxpwr_idx];
47005491d2cSKalle Valo 	maxpwr40 = li_mimo->maxpwr40[maxpwr_idx];
47105491d2cSKalle Valo 
47205491d2cSKalle Valo 	maxpwr20 = maxpwr20 - delta;
47305491d2cSKalle Valo 	maxpwr20 = max(maxpwr20, 0);
47405491d2cSKalle Valo 	maxpwr40 = maxpwr40 - delta;
47505491d2cSKalle Valo 	maxpwr40 = max(maxpwr40, 0);
47605491d2cSKalle Valo 
47705491d2cSKalle Valo 	/* Fill in the MCS 0-7 (SISO) rates */
47805491d2cSKalle Valo 	for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
47905491d2cSKalle Valo 
48005491d2cSKalle Valo 		/*
48105491d2cSKalle Valo 		 * 20 MHz has the same power as the corresponding OFDM rate
48205491d2cSKalle Valo 		 * unless overriden by the locale specific code.
48305491d2cSKalle Valo 		 */
48405491d2cSKalle Valo 		txpwr->mcs_20_siso[i] = txpwr->ofdm[i];
48505491d2cSKalle Valo 		txpwr->mcs_40_siso[i] = 0;
48605491d2cSKalle Valo 	}
48705491d2cSKalle Valo 
48805491d2cSKalle Valo 	/* Fill in the MCS 0-7 CDD rates */
48905491d2cSKalle Valo 	for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
49005491d2cSKalle Valo 		txpwr->mcs_20_cdd[i] = (u8) maxpwr20;
49105491d2cSKalle Valo 		txpwr->mcs_40_cdd[i] = (u8) maxpwr40;
49205491d2cSKalle Valo 	}
49305491d2cSKalle Valo 
49405491d2cSKalle Valo 	/*
49505491d2cSKalle Valo 	 * These locales have SISO expressed in the
49605491d2cSKalle Valo 	 * table and override CDD later
49705491d2cSKalle Valo 	 */
49805491d2cSKalle Valo 	if (li_mimo == &locale_bn) {
49905491d2cSKalle Valo 		maxpwr20 = QDB(16);
50005491d2cSKalle Valo 		maxpwr40 = 0;
50105491d2cSKalle Valo 
50205491d2cSKalle Valo 		if (chan >= 3 && chan <= 11)
50305491d2cSKalle Valo 			maxpwr40 = QDB(16);
50405491d2cSKalle Valo 
50505491d2cSKalle Valo 		for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
50605491d2cSKalle Valo 			txpwr->mcs_20_siso[i] = (u8) maxpwr20;
50705491d2cSKalle Valo 			txpwr->mcs_40_siso[i] = (u8) maxpwr40;
50805491d2cSKalle Valo 		}
50905491d2cSKalle Valo 	}
51005491d2cSKalle Valo 
51105491d2cSKalle Valo 	/* Fill in the MCS 0-7 STBC rates */
51205491d2cSKalle Valo 	for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
51305491d2cSKalle Valo 		txpwr->mcs_20_stbc[i] = 0;
51405491d2cSKalle Valo 		txpwr->mcs_40_stbc[i] = 0;
51505491d2cSKalle Valo 	}
51605491d2cSKalle Valo 
51705491d2cSKalle Valo 	/* Fill in the MCS 8-15 SDM rates */
51805491d2cSKalle Valo 	for (i = 0; i < BRCMS_NUM_RATES_MCS_2_STREAM; i++) {
51905491d2cSKalle Valo 		txpwr->mcs_20_mimo[i] = (u8) maxpwr20;
52005491d2cSKalle Valo 		txpwr->mcs_40_mimo[i] = (u8) maxpwr40;
52105491d2cSKalle Valo 	}
52205491d2cSKalle Valo 
52305491d2cSKalle Valo 	/* Fill in MCS32 */
52405491d2cSKalle Valo 	txpwr->mcs32 = (u8) maxpwr40;
52505491d2cSKalle Valo 
52605491d2cSKalle Valo 	for (i = 0, j = 0; i < BRCMS_NUM_RATES_OFDM; i++, j++) {
52705491d2cSKalle Valo 		if (txpwr->ofdm_40_cdd[i] == 0)
52805491d2cSKalle Valo 			txpwr->ofdm_40_cdd[i] = txpwr->mcs_40_cdd[j];
52905491d2cSKalle Valo 		if (i == 0) {
53005491d2cSKalle Valo 			i = i + 1;
53105491d2cSKalle Valo 			if (txpwr->ofdm_40_cdd[i] == 0)
53205491d2cSKalle Valo 				txpwr->ofdm_40_cdd[i] = txpwr->mcs_40_cdd[j];
53305491d2cSKalle Valo 		}
53405491d2cSKalle Valo 	}
53505491d2cSKalle Valo 
53605491d2cSKalle Valo 	/*
53705491d2cSKalle Valo 	 * Copy the 40 MHZ MCS 0-7 CDD value to the 40 MHZ MCS 0-7 SISO
53805491d2cSKalle Valo 	 * value if it wasn't provided explicitly.
53905491d2cSKalle Valo 	 */
54005491d2cSKalle Valo 	for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
54105491d2cSKalle Valo 		if (txpwr->mcs_40_siso[i] == 0)
54205491d2cSKalle Valo 			txpwr->mcs_40_siso[i] = txpwr->mcs_40_cdd[i];
54305491d2cSKalle Valo 	}
54405491d2cSKalle Valo 
54505491d2cSKalle Valo 	for (i = 0, j = 0; i < BRCMS_NUM_RATES_OFDM; i++, j++) {
54605491d2cSKalle Valo 		if (txpwr->ofdm_40_siso[i] == 0)
54705491d2cSKalle Valo 			txpwr->ofdm_40_siso[i] = txpwr->mcs_40_siso[j];
54805491d2cSKalle Valo 		if (i == 0) {
54905491d2cSKalle Valo 			i = i + 1;
55005491d2cSKalle Valo 			if (txpwr->ofdm_40_siso[i] == 0)
55105491d2cSKalle Valo 				txpwr->ofdm_40_siso[i] = txpwr->mcs_40_siso[j];
55205491d2cSKalle Valo 		}
55305491d2cSKalle Valo 	}
55405491d2cSKalle Valo 
55505491d2cSKalle Valo 	/*
55605491d2cSKalle Valo 	 * Copy the 20 and 40 MHz MCS0-7 CDD values to the corresponding
55705491d2cSKalle Valo 	 * STBC values if they weren't provided explicitly.
55805491d2cSKalle Valo 	 */
55905491d2cSKalle Valo 	for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
56005491d2cSKalle Valo 		if (txpwr->mcs_20_stbc[i] == 0)
56105491d2cSKalle Valo 			txpwr->mcs_20_stbc[i] = txpwr->mcs_20_cdd[i];
56205491d2cSKalle Valo 
56305491d2cSKalle Valo 		if (txpwr->mcs_40_stbc[i] == 0)
56405491d2cSKalle Valo 			txpwr->mcs_40_stbc[i] = txpwr->mcs_40_cdd[i];
56505491d2cSKalle Valo 	}
56605491d2cSKalle Valo 
56705491d2cSKalle Valo 	return;
56805491d2cSKalle Valo }
56905491d2cSKalle Valo 
57005491d2cSKalle Valo /*
57105491d2cSKalle Valo  * Verify the chanspec is using a legal set of parameters, i.e. that the
57205491d2cSKalle Valo  * chanspec specified a band, bw, ctl_sb and channel and that the
57305491d2cSKalle Valo  * combination could be legal given any set of circumstances.
57405491d2cSKalle Valo  * RETURNS: true is the chanspec is malformed, false if it looks good.
57505491d2cSKalle Valo  */
brcms_c_chspec_malformed(u16 chanspec)57605491d2cSKalle Valo static bool brcms_c_chspec_malformed(u16 chanspec)
57705491d2cSKalle Valo {
57805491d2cSKalle Valo 	/* must be 2G or 5G band */
57905491d2cSKalle Valo 	if (!CHSPEC_IS5G(chanspec) && !CHSPEC_IS2G(chanspec))
58005491d2cSKalle Valo 		return true;
58105491d2cSKalle Valo 	/* must be 20 or 40 bandwidth */
58205491d2cSKalle Valo 	if (!CHSPEC_IS40(chanspec) && !CHSPEC_IS20(chanspec))
58305491d2cSKalle Valo 		return true;
58405491d2cSKalle Valo 
58505491d2cSKalle Valo 	/* 20MHZ b/w must have no ctl sb, 40 must have a ctl sb */
58605491d2cSKalle Valo 	if (CHSPEC_IS20(chanspec)) {
58705491d2cSKalle Valo 		if (!CHSPEC_SB_NONE(chanspec))
58805491d2cSKalle Valo 			return true;
58905491d2cSKalle Valo 	} else if (!CHSPEC_SB_UPPER(chanspec) && !CHSPEC_SB_LOWER(chanspec)) {
59005491d2cSKalle Valo 		return true;
59105491d2cSKalle Valo 	}
59205491d2cSKalle Valo 
59305491d2cSKalle Valo 	return false;
59405491d2cSKalle Valo }
59505491d2cSKalle Valo 
59605491d2cSKalle Valo /*
59705491d2cSKalle Valo  * Validate the chanspec for this locale, for 40MHZ we need to also
59805491d2cSKalle Valo  * check that the sidebands are valid 20MZH channels in this locale
59905491d2cSKalle Valo  * and they are also a legal HT combination
60005491d2cSKalle Valo  */
60105491d2cSKalle Valo static bool
brcms_c_valid_chanspec_ext(struct brcms_cm_info * wlc_cm,u16 chspec)60205491d2cSKalle Valo brcms_c_valid_chanspec_ext(struct brcms_cm_info *wlc_cm, u16 chspec)
60305491d2cSKalle Valo {
60405491d2cSKalle Valo 	struct brcms_c_info *wlc = wlc_cm->wlc;
60505491d2cSKalle Valo 	u8 channel = CHSPEC_CHANNEL(chspec);
60605491d2cSKalle Valo 
60705491d2cSKalle Valo 	/* check the chanspec */
60805491d2cSKalle Valo 	if (brcms_c_chspec_malformed(chspec)) {
60905491d2cSKalle Valo 		brcms_err(wlc->hw->d11core, "wl%d: malformed chanspec 0x%x\n",
61005491d2cSKalle Valo 			  wlc->pub->unit, chspec);
61105491d2cSKalle Valo 		return false;
61205491d2cSKalle Valo 	}
61305491d2cSKalle Valo 
61405491d2cSKalle Valo 	if (CHANNEL_BANDUNIT(wlc_cm->wlc, channel) !=
61505491d2cSKalle Valo 	    chspec_bandunit(chspec))
61605491d2cSKalle Valo 		return false;
61705491d2cSKalle Valo 
61805491d2cSKalle Valo 	return true;
61905491d2cSKalle Valo }
62005491d2cSKalle Valo 
brcms_c_valid_chanspec_db(struct brcms_cm_info * wlc_cm,u16 chspec)62105491d2cSKalle Valo bool brcms_c_valid_chanspec_db(struct brcms_cm_info *wlc_cm, u16 chspec)
62205491d2cSKalle Valo {
62305491d2cSKalle Valo 	return brcms_c_valid_chanspec_ext(wlc_cm, chspec);
62405491d2cSKalle Valo }
62505491d2cSKalle Valo 
brcms_is_radar_freq(u16 center_freq)62605491d2cSKalle Valo static bool brcms_is_radar_freq(u16 center_freq)
62705491d2cSKalle Valo {
62805491d2cSKalle Valo 	return center_freq >= 5260 && center_freq <= 5700;
62905491d2cSKalle Valo }
63005491d2cSKalle Valo 
brcms_reg_apply_radar_flags(struct wiphy * wiphy)63105491d2cSKalle Valo static void brcms_reg_apply_radar_flags(struct wiphy *wiphy)
63205491d2cSKalle Valo {
63305491d2cSKalle Valo 	struct ieee80211_supported_band *sband;
63405491d2cSKalle Valo 	struct ieee80211_channel *ch;
63505491d2cSKalle Valo 	int i;
63605491d2cSKalle Valo 
63757fbcce3SJohannes Berg 	sband = wiphy->bands[NL80211_BAND_5GHZ];
63805491d2cSKalle Valo 	if (!sband)
63905491d2cSKalle Valo 		return;
64005491d2cSKalle Valo 
64105491d2cSKalle Valo 	for (i = 0; i < sband->n_channels; i++) {
64205491d2cSKalle Valo 		ch = &sband->channels[i];
64305491d2cSKalle Valo 
64405491d2cSKalle Valo 		if (!brcms_is_radar_freq(ch->center_freq))
64505491d2cSKalle Valo 			continue;
64605491d2cSKalle Valo 
64705491d2cSKalle Valo 		/*
64805491d2cSKalle Valo 		 * All channels in this range should be passive and have
64905491d2cSKalle Valo 		 * DFS enabled.
65005491d2cSKalle Valo 		 */
65105491d2cSKalle Valo 		if (!(ch->flags & IEEE80211_CHAN_DISABLED))
65205491d2cSKalle Valo 			ch->flags |= IEEE80211_CHAN_RADAR |
65305491d2cSKalle Valo 				     IEEE80211_CHAN_NO_IR;
65405491d2cSKalle Valo 	}
65505491d2cSKalle Valo }
65605491d2cSKalle Valo 
65705491d2cSKalle Valo static void
brcms_reg_apply_beaconing_flags(struct wiphy * wiphy,enum nl80211_reg_initiator initiator)65805491d2cSKalle Valo brcms_reg_apply_beaconing_flags(struct wiphy *wiphy,
65905491d2cSKalle Valo 				enum nl80211_reg_initiator initiator)
66005491d2cSKalle Valo {
66105491d2cSKalle Valo 	struct ieee80211_supported_band *sband;
66205491d2cSKalle Valo 	struct ieee80211_channel *ch;
66305491d2cSKalle Valo 	const struct ieee80211_reg_rule *rule;
66405491d2cSKalle Valo 	int band, i;
66505491d2cSKalle Valo 
66657fbcce3SJohannes Berg 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
66705491d2cSKalle Valo 		sband = wiphy->bands[band];
66805491d2cSKalle Valo 		if (!sband)
66905491d2cSKalle Valo 			continue;
67005491d2cSKalle Valo 
67105491d2cSKalle Valo 		for (i = 0; i < sband->n_channels; i++) {
67205491d2cSKalle Valo 			ch = &sband->channels[i];
67305491d2cSKalle Valo 
67405491d2cSKalle Valo 			if (ch->flags &
67505491d2cSKalle Valo 			    (IEEE80211_CHAN_DISABLED | IEEE80211_CHAN_RADAR))
67605491d2cSKalle Valo 				continue;
67705491d2cSKalle Valo 
67805491d2cSKalle Valo 			if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
67905491d2cSKalle Valo 				rule = freq_reg_info(wiphy,
68005491d2cSKalle Valo 						     MHZ_TO_KHZ(ch->center_freq));
68105491d2cSKalle Valo 				if (IS_ERR(rule))
68205491d2cSKalle Valo 					continue;
68305491d2cSKalle Valo 
68405491d2cSKalle Valo 				if (!(rule->flags & NL80211_RRF_NO_IR))
68505491d2cSKalle Valo 					ch->flags &= ~IEEE80211_CHAN_NO_IR;
68605491d2cSKalle Valo 			} else if (ch->beacon_found) {
68705491d2cSKalle Valo 				ch->flags &= ~IEEE80211_CHAN_NO_IR;
68805491d2cSKalle Valo 			}
68905491d2cSKalle Valo 		}
69005491d2cSKalle Valo 	}
69105491d2cSKalle Valo }
69205491d2cSKalle Valo 
brcms_reg_notifier(struct wiphy * wiphy,struct regulatory_request * request)69305491d2cSKalle Valo static void brcms_reg_notifier(struct wiphy *wiphy,
69405491d2cSKalle Valo 			       struct regulatory_request *request)
69505491d2cSKalle Valo {
69605491d2cSKalle Valo 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
69705491d2cSKalle Valo 	struct brcms_info *wl = hw->priv;
69805491d2cSKalle Valo 	struct brcms_c_info *wlc = wl->wlc;
69905491d2cSKalle Valo 	struct ieee80211_supported_band *sband;
70005491d2cSKalle Valo 	struct ieee80211_channel *ch;
70105491d2cSKalle Valo 	int band, i;
70205491d2cSKalle Valo 	bool ch_found = false;
70305491d2cSKalle Valo 
70405491d2cSKalle Valo 	brcms_reg_apply_radar_flags(wiphy);
70505491d2cSKalle Valo 
70605491d2cSKalle Valo 	if (request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
70705491d2cSKalle Valo 		brcms_reg_apply_beaconing_flags(wiphy, request->initiator);
70805491d2cSKalle Valo 
70905491d2cSKalle Valo 	/* Disable radio if all channels disallowed by regulatory */
71057fbcce3SJohannes Berg 	for (band = 0; !ch_found && band < NUM_NL80211_BANDS; band++) {
71105491d2cSKalle Valo 		sband = wiphy->bands[band];
71205491d2cSKalle Valo 		if (!sband)
71305491d2cSKalle Valo 			continue;
71405491d2cSKalle Valo 
71505491d2cSKalle Valo 		for (i = 0; !ch_found && i < sband->n_channels; i++) {
71605491d2cSKalle Valo 			ch = &sband->channels[i];
71705491d2cSKalle Valo 
71805491d2cSKalle Valo 			if (!(ch->flags & IEEE80211_CHAN_DISABLED))
71905491d2cSKalle Valo 				ch_found = true;
72005491d2cSKalle Valo 		}
72105491d2cSKalle Valo 	}
72205491d2cSKalle Valo 
72305491d2cSKalle Valo 	if (ch_found) {
72405491d2cSKalle Valo 		mboolclr(wlc->pub->radio_disabled, WL_RADIO_COUNTRY_DISABLE);
72505491d2cSKalle Valo 	} else {
72605491d2cSKalle Valo 		mboolset(wlc->pub->radio_disabled, WL_RADIO_COUNTRY_DISABLE);
72705491d2cSKalle Valo 		brcms_err(wlc->hw->d11core,
72805491d2cSKalle Valo 			  "wl%d: %s: no valid channel for \"%s\"\n",
72905491d2cSKalle Valo 			  wlc->pub->unit, __func__, request->alpha2);
73005491d2cSKalle Valo 	}
73105491d2cSKalle Valo 
73205491d2cSKalle Valo 	if (wlc->pub->_nbands > 1 || wlc->band->bandtype == BRCM_BAND_2G)
73305491d2cSKalle Valo 		wlc_phy_chanspec_ch14_widefilter_set(wlc->band->pi,
73405491d2cSKalle Valo 					brcms_c_japan_ccode(request->alpha2));
73505491d2cSKalle Valo }
73605491d2cSKalle Valo 
brcms_c_regd_init(struct brcms_c_info * wlc)73705491d2cSKalle Valo void brcms_c_regd_init(struct brcms_c_info *wlc)
73805491d2cSKalle Valo {
73905491d2cSKalle Valo 	struct wiphy *wiphy = wlc->wiphy;
74005491d2cSKalle Valo 	const struct brcms_regd *regd = wlc->cmi->world_regd;
74105491d2cSKalle Valo 	struct ieee80211_supported_band *sband;
74205491d2cSKalle Valo 	struct ieee80211_channel *ch;
74305491d2cSKalle Valo 	struct brcms_chanvec sup_chan;
74405491d2cSKalle Valo 	struct brcms_band *band;
74505491d2cSKalle Valo 	int band_idx, i;
74605491d2cSKalle Valo 
74705491d2cSKalle Valo 	/* Disable any channels not supported by the phy */
74805491d2cSKalle Valo 	for (band_idx = 0; band_idx < wlc->pub->_nbands; band_idx++) {
74905491d2cSKalle Valo 		band = wlc->bandstate[band_idx];
75005491d2cSKalle Valo 
75105491d2cSKalle Valo 		wlc_phy_chanspec_band_validch(band->pi, band->bandtype,
75205491d2cSKalle Valo 					      &sup_chan);
75305491d2cSKalle Valo 
75405491d2cSKalle Valo 		if (band_idx == BAND_2G_INDEX)
75557fbcce3SJohannes Berg 			sband = wiphy->bands[NL80211_BAND_2GHZ];
75605491d2cSKalle Valo 		else
75757fbcce3SJohannes Berg 			sband = wiphy->bands[NL80211_BAND_5GHZ];
75805491d2cSKalle Valo 
75905491d2cSKalle Valo 		for (i = 0; i < sband->n_channels; i++) {
76005491d2cSKalle Valo 			ch = &sband->channels[i];
76105491d2cSKalle Valo 			if (!isset(sup_chan.vec, ch->hw_value))
76205491d2cSKalle Valo 				ch->flags |= IEEE80211_CHAN_DISABLED;
76305491d2cSKalle Valo 		}
76405491d2cSKalle Valo 	}
76505491d2cSKalle Valo 
76605491d2cSKalle Valo 	wlc->wiphy->reg_notifier = brcms_reg_notifier;
76705491d2cSKalle Valo 	wlc->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
76805491d2cSKalle Valo 					REGULATORY_STRICT_REG;
76905491d2cSKalle Valo 	wiphy_apply_custom_regulatory(wlc->wiphy, regd->regdomain);
77005491d2cSKalle Valo 	brcms_reg_apply_beaconing_flags(wiphy, NL80211_REGDOM_SET_BY_DRIVER);
77105491d2cSKalle Valo }
772