channel.c revision c49aa4aa2bc89e88672dc419a293d7b8c1f094d2
15b435de0d786869c95d1962121af0d7df2542009Arend van Spriel/*
25b435de0d786869c95d1962121af0d7df2542009Arend van Spriel * Copyright (c) 2010 Broadcom Corporation
35b435de0d786869c95d1962121af0d7df2542009Arend van Spriel *
45b435de0d786869c95d1962121af0d7df2542009Arend van Spriel * Permission to use, copy, modify, and/or distribute this software for any
55b435de0d786869c95d1962121af0d7df2542009Arend van Spriel * purpose with or without fee is hereby granted, provided that the above
65b435de0d786869c95d1962121af0d7df2542009Arend van Spriel * copyright notice and this permission notice appear in all copies.
75b435de0d786869c95d1962121af0d7df2542009Arend van Spriel *
85b435de0d786869c95d1962121af0d7df2542009Arend van Spriel * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
95b435de0d786869c95d1962121af0d7df2542009Arend van Spriel * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
105b435de0d786869c95d1962121af0d7df2542009Arend van Spriel * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
115b435de0d786869c95d1962121af0d7df2542009Arend van Spriel * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
125b435de0d786869c95d1962121af0d7df2542009Arend van Spriel * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
135b435de0d786869c95d1962121af0d7df2542009Arend van Spriel * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
145b435de0d786869c95d1962121af0d7df2542009Arend van Spriel * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
155b435de0d786869c95d1962121af0d7df2542009Arend van Spriel */
165b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
175b435de0d786869c95d1962121af0d7df2542009Arend van Spriel#include <linux/types.h>
18cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee#include <net/cfg80211.h>
195b435de0d786869c95d1962121af0d7df2542009Arend van Spriel#include <net/mac80211.h>
20cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee#include <net/regulatory.h>
215b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
225b435de0d786869c95d1962121af0d7df2542009Arend van Spriel#include <defs.h>
235b435de0d786869c95d1962121af0d7df2542009Arend van Spriel#include "pub.h"
245b435de0d786869c95d1962121af0d7df2542009Arend van Spriel#include "phy/phy_hal.h"
255b435de0d786869c95d1962121af0d7df2542009Arend van Spriel#include "main.h"
265b435de0d786869c95d1962121af0d7df2542009Arend van Spriel#include "stf.h"
275b435de0d786869c95d1962121af0d7df2542009Arend van Spriel#include "channel.h"
28cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee#include "mac80211_if.h"
295b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
305b435de0d786869c95d1962121af0d7df2542009Arend van Spriel/* QDB() macro takes a dB value and converts to a quarter dB value */
315b435de0d786869c95d1962121af0d7df2542009Arend van Spriel#define QDB(n) ((n) * BRCMS_TXPWR_DB_FACTOR)
325b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
335b435de0d786869c95d1962121af0d7df2542009Arend van Spriel#define LOCALE_MIMO_IDX_bn		0
345b435de0d786869c95d1962121af0d7df2542009Arend van Spriel#define LOCALE_MIMO_IDX_11n		0
355b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
365b435de0d786869c95d1962121af0d7df2542009Arend van Spriel/* max of BAND_5G_PWR_LVLS and 14 for 2.4 GHz */
375b435de0d786869c95d1962121af0d7df2542009Arend van Spriel#define BRCMS_MAXPWR_MIMO_TBL_SIZE	14
385b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
395b435de0d786869c95d1962121af0d7df2542009Arend van Spriel/* maxpwr mapping to 5GHz band channels:
405b435de0d786869c95d1962121af0d7df2542009Arend van Spriel * maxpwr[0] - channels [34-48]
415b435de0d786869c95d1962121af0d7df2542009Arend van Spriel * maxpwr[1] - channels [52-60]
425b435de0d786869c95d1962121af0d7df2542009Arend van Spriel * maxpwr[2] - channels [62-64]
435b435de0d786869c95d1962121af0d7df2542009Arend van Spriel * maxpwr[3] - channels [100-140]
445b435de0d786869c95d1962121af0d7df2542009Arend van Spriel * maxpwr[4] - channels [149-165]
455b435de0d786869c95d1962121af0d7df2542009Arend van Spriel */
465b435de0d786869c95d1962121af0d7df2542009Arend van Spriel#define BAND_5G_PWR_LVLS	5	/* 5 power levels for 5G */
475b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
485b435de0d786869c95d1962121af0d7df2542009Arend van Spriel#define LC(id)	LOCALE_MIMO_IDX_ ## id
495b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
50edc7651f3a4ffa7fca37d92401562694121512adSeth Forshee#define LOCALES(mimo2, mimo5) \
51edc7651f3a4ffa7fca37d92401562694121512adSeth Forshee		{LC(mimo2), LC(mimo5)}
525b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
535b435de0d786869c95d1962121af0d7df2542009Arend van Spriel/* macro to get 5 GHz channel group index for tx power */
545b435de0d786869c95d1962121af0d7df2542009Arend van Spriel#define CHANNEL_POWER_IDX_5G(c) (((c) < 52) ? 0 : \
555b435de0d786869c95d1962121af0d7df2542009Arend van Spriel				 (((c) < 62) ? 1 : \
565b435de0d786869c95d1962121af0d7df2542009Arend van Spriel				 (((c) < 100) ? 2 : \
575b435de0d786869c95d1962121af0d7df2542009Arend van Spriel				 (((c) < 149) ? 3 : 4))))
585b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
59cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee#define BRCM_2GHZ_2412_2462	REG_RULE(2412-10, 2462+10, 40, 0, 19, 0)
60cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee#define BRCM_2GHZ_2467_2472	REG_RULE(2467-10, 2472+10, 20, 0, 19, \
61cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee					 NL80211_RRF_PASSIVE_SCAN | \
62cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee					 NL80211_RRF_NO_IBSS)
63cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee
64cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee#define BRCM_5GHZ_5180_5240	REG_RULE(5180-10, 5240+10, 40, 0, 21, \
65cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee					 NL80211_RRF_PASSIVE_SCAN | \
66cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee					 NL80211_RRF_NO_IBSS)
67cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee#define BRCM_5GHZ_5260_5320	REG_RULE(5260-10, 5320+10, 40, 0, 21, \
68cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee					 NL80211_RRF_PASSIVE_SCAN | \
69cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee					 NL80211_RRF_DFS | \
70cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee					 NL80211_RRF_NO_IBSS)
71cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee#define BRCM_5GHZ_5500_5700	REG_RULE(5500-10, 5700+10, 40, 0, 21, \
72cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee					 NL80211_RRF_PASSIVE_SCAN | \
73cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee					 NL80211_RRF_DFS | \
74cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee					 NL80211_RRF_NO_IBSS)
75cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee#define BRCM_5GHZ_5745_5825	REG_RULE(5745-10, 5825+10, 40, 0, 21, \
76cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee					 NL80211_RRF_PASSIVE_SCAN | \
77cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee					 NL80211_RRF_NO_IBSS)
78cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee
79cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forsheestatic const struct ieee80211_regdomain brcms_regdom_x2 = {
80cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	.n_reg_rules = 7,
81cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	.alpha2 = "X2",
82cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	.reg_rules = {
83cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		BRCM_2GHZ_2412_2462,
84cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		BRCM_2GHZ_2467_2472,
85cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		BRCM_5GHZ_5180_5240,
86cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		BRCM_5GHZ_5260_5320,
87cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		BRCM_5GHZ_5500_5700,
88cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		BRCM_5GHZ_5745_5825,
89cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	}
90cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee};
91cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee
925b435de0d786869c95d1962121af0d7df2542009Arend van Spriel /* locale per-channel tx power limits for MIMO frames
935b435de0d786869c95d1962121af0d7df2542009Arend van Spriel  * maxpwr arrays are index by channel for 2.4 GHz limits, and
945b435de0d786869c95d1962121af0d7df2542009Arend van Spriel  * by sub-band for 5 GHz limits using CHANNEL_POWER_IDX_5G(channel)
955b435de0d786869c95d1962121af0d7df2542009Arend van Spriel  */
965b435de0d786869c95d1962121af0d7df2542009Arend van Sprielstruct locale_mimo_info {
975b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/* tx 20 MHz power limits, qdBm units */
985b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	s8 maxpwr20[BRCMS_MAXPWR_MIMO_TBL_SIZE];
995b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/* tx 40 MHz power limits, qdBm units */
1005b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	s8 maxpwr40[BRCMS_MAXPWR_MIMO_TBL_SIZE];
1015b435de0d786869c95d1962121af0d7df2542009Arend van Spriel};
1025b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
1035b435de0d786869c95d1962121af0d7df2542009Arend van Spriel/* Country names and abbreviations with locale defined from ISO 3166 */
1045b435de0d786869c95d1962121af0d7df2542009Arend van Sprielstruct country_info {
1055b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	const u8 locale_mimo_2G;	/* 2.4G mimo info */
1065b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	const u8 locale_mimo_5G;	/* 5G mimo info */
1075b435de0d786869c95d1962121af0d7df2542009Arend van Spriel};
1085b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
109cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forsheestruct brcms_regd {
110cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	struct country_info country;
111cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	const struct ieee80211_regdomain *regdomain;
112cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee};
113cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee
1145b435de0d786869c95d1962121af0d7df2542009Arend van Sprielstruct brcms_cm_info {
1155b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	struct brcms_pub *pub;
1165b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	struct brcms_c_info *wlc;
117cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	const struct brcms_regd *world_regd;
1185b435de0d786869c95d1962121af0d7df2542009Arend van Spriel};
1195b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
1205b435de0d786869c95d1962121af0d7df2542009Arend van Spriel/*
1215b435de0d786869c95d1962121af0d7df2542009Arend van Spriel * MIMO Locale Definitions - 2.4 GHz
1225b435de0d786869c95d1962121af0d7df2542009Arend van Spriel */
1235b435de0d786869c95d1962121af0d7df2542009Arend van Sprielstatic const struct locale_mimo_info locale_bn = {
1245b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	{QDB(13), QDB(13), QDB(13), QDB(13), QDB(13),
1255b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	 QDB(13), QDB(13), QDB(13), QDB(13), QDB(13),
1265b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	 QDB(13), QDB(13), QDB(13)},
1275b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	{0, 0, QDB(13), QDB(13), QDB(13),
1285b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	 QDB(13), QDB(13), QDB(13), QDB(13), QDB(13),
1295b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	 QDB(13), 0, 0},
1305b435de0d786869c95d1962121af0d7df2542009Arend van Spriel};
1315b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
1325b435de0d786869c95d1962121af0d7df2542009Arend van Sprielstatic const struct locale_mimo_info *g_mimo_2g_table[] = {
1335b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	&locale_bn
1345b435de0d786869c95d1962121af0d7df2542009Arend van Spriel};
1355b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
1365b435de0d786869c95d1962121af0d7df2542009Arend van Spriel/*
1375b435de0d786869c95d1962121af0d7df2542009Arend van Spriel * MIMO Locale Definitions - 5 GHz
1385b435de0d786869c95d1962121af0d7df2542009Arend van Spriel */
1395b435de0d786869c95d1962121af0d7df2542009Arend van Sprielstatic const struct locale_mimo_info locale_11n = {
1405b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	{ /* 12.5 dBm */ 50, 50, 50, QDB(15), QDB(15)},
1415b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	{QDB(14), QDB(15), QDB(15), QDB(15), QDB(15)},
1425b435de0d786869c95d1962121af0d7df2542009Arend van Spriel};
1435b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
1445b435de0d786869c95d1962121af0d7df2542009Arend van Sprielstatic const struct locale_mimo_info *g_mimo_5g_table[] = {
1455b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	&locale_11n
1465b435de0d786869c95d1962121af0d7df2542009Arend van Spriel};
1475b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
148cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forsheestatic const struct brcms_regd cntry_locales[] = {
149cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	/* Worldwide RoW 2, must always be at index 0 */
1505b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	{
151edc7651f3a4ffa7fca37d92401562694121512adSeth Forshee		.country = LOCALES(bn, 11n),
152cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		.regdomain = &brcms_regdom_x2,
153cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	},
1545b435de0d786869c95d1962121af0d7df2542009Arend van Spriel};
1555b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
1565b435de0d786869c95d1962121af0d7df2542009Arend van Sprielstatic const struct locale_mimo_info *brcms_c_get_mimo_2g(u8 locale_idx)
1575b435de0d786869c95d1962121af0d7df2542009Arend van Spriel{
1585b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	if (locale_idx >= ARRAY_SIZE(g_mimo_2g_table))
1595b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		return NULL;
1605b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
1615b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	return g_mimo_2g_table[locale_idx];
1625b435de0d786869c95d1962121af0d7df2542009Arend van Spriel}
1635b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
1645b435de0d786869c95d1962121af0d7df2542009Arend van Sprielstatic const struct locale_mimo_info *brcms_c_get_mimo_5g(u8 locale_idx)
1655b435de0d786869c95d1962121af0d7df2542009Arend van Spriel{
1665b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	if (locale_idx >= ARRAY_SIZE(g_mimo_5g_table))
1675b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		return NULL;
1685b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
1695b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	return g_mimo_5g_table[locale_idx];
1705b435de0d786869c95d1962121af0d7df2542009Arend van Spriel}
1715b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
17294a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel/*
17394a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel * Indicates whether the country provided is valid to pass
17494a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel * to cfg80211 or not.
17594a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel *
17694a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel * returns true if valid; false if not.
17794a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel */
17894a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Sprielstatic bool brcms_c_country_valid(const char *ccode)
17994a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel{
18094a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel	/*
18194a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel	 * only allow ascii alpha uppercase for the first 2
18294a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel	 * chars.
18394a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel	 */
18494a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel	if (!((0x80 & ccode[0]) == 0 && ccode[0] >= 0x41 && ccode[0] <= 0x5A &&
18594a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel	      (0x80 & ccode[1]) == 0 && ccode[1] >= 0x41 && ccode[1] <= 0x5A &&
18694a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel	      ccode[2] == '\0'))
18794a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel		return false;
18894a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel
18994a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel	/*
19094a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel	 * do not match ISO 3166-1 user assigned country codes
19194a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel	 * that may be in the driver table
19294a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel	 */
19394a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel	if (!strcmp("AA", ccode) ||        /* AA */
19494a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel	    !strcmp("ZZ", ccode) ||        /* ZZ */
19594a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel	    ccode[0] == 'X' ||             /* XA - XZ */
19694a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel	    (ccode[0] == 'Q' &&            /* QM - QZ */
19794a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel	     (ccode[1] >= 'M' && ccode[1] <= 'Z')))
19894a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel		return false;
19994a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel
20094a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel	if (!strcmp("NA", ccode))
20194a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel		return false;
20294a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel
20394a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel	return true;
20494a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel}
20594a2ca311cf4154c566cf5c49b88c13cd4cdce73Arend van Spriel
206cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forsheestatic const struct brcms_regd *brcms_world_regd(const char *regdom, int len)
2075b435de0d786869c95d1962121af0d7df2542009Arend van Spriel{
208cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	const struct brcms_regd *regd = NULL;
209cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	int i;
2105b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
211cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	for (i = 0; i < ARRAY_SIZE(cntry_locales); i++) {
212cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		if (!strncmp(regdom, cntry_locales[i].regdomain->alpha2, len)) {
213cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee			regd = &cntry_locales[i];
214cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee			break;
215cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		}
2165b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	}
2175b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
218cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	return regd;
2195b435de0d786869c95d1962121af0d7df2542009Arend van Spriel}
2205b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
221cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forsheestatic const struct brcms_regd *brcms_default_world_regd(void)
2225b435de0d786869c95d1962121af0d7df2542009Arend van Spriel{
223cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	return &cntry_locales[0];
2245b435de0d786869c95d1962121af0d7df2542009Arend van Spriel}
2255b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
2265b435de0d786869c95d1962121af0d7df2542009Arend van Spriel/* JP, J1 - J10 are Japan ccodes */
2275b435de0d786869c95d1962121af0d7df2542009Arend van Sprielstatic bool brcms_c_japan_ccode(const char *ccode)
2285b435de0d786869c95d1962121af0d7df2542009Arend van Spriel{
2295b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	return (ccode[0] == 'J' &&
2305b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		(ccode[1] == 'P' || (ccode[1] >= '1' && ccode[1] <= '9')));
2315b435de0d786869c95d1962121af0d7df2542009Arend van Spriel}
2325b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
2335b435de0d786869c95d1962121af0d7df2542009Arend van Sprielstatic void
2345b435de0d786869c95d1962121af0d7df2542009Arend van Sprielbrcms_c_channel_min_txpower_limits_with_local_constraint(
2355b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		struct brcms_cm_info *wlc_cm, struct txpwr_limits *txpwr,
2365b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		u8 local_constraint_qdbm)
2375b435de0d786869c95d1962121af0d7df2542009Arend van Spriel{
2385b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	int j;
2395b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
2405b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/* CCK Rates */
2415b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	for (j = 0; j < WL_TX_POWER_CCK_NUM; j++)
2425b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		txpwr->cck[j] = min(txpwr->cck[j], local_constraint_qdbm);
2435b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
2445b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/* 20 MHz Legacy OFDM SISO */
2455b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	for (j = 0; j < WL_TX_POWER_OFDM_NUM; j++)
2465b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		txpwr->ofdm[j] = min(txpwr->ofdm[j], local_constraint_qdbm);
2475b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
2485b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/* 20 MHz Legacy OFDM CDD */
2495b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	for (j = 0; j < BRCMS_NUM_RATES_OFDM; j++)
2505b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		txpwr->ofdm_cdd[j] =
2515b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		    min(txpwr->ofdm_cdd[j], local_constraint_qdbm);
2525b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
2535b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/* 40 MHz Legacy OFDM SISO */
2545b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	for (j = 0; j < BRCMS_NUM_RATES_OFDM; j++)
2555b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		txpwr->ofdm_40_siso[j] =
2565b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		    min(txpwr->ofdm_40_siso[j], local_constraint_qdbm);
2575b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
2585b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/* 40 MHz Legacy OFDM CDD */
2595b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	for (j = 0; j < BRCMS_NUM_RATES_OFDM; j++)
2605b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		txpwr->ofdm_40_cdd[j] =
2615b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		    min(txpwr->ofdm_40_cdd[j], local_constraint_qdbm);
2625b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
2635b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/* 20MHz MCS 0-7 SISO */
2645b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
2655b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		txpwr->mcs_20_siso[j] =
2665b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		    min(txpwr->mcs_20_siso[j], local_constraint_qdbm);
2675b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
2685b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/* 20MHz MCS 0-7 CDD */
2695b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
2705b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		txpwr->mcs_20_cdd[j] =
2715b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		    min(txpwr->mcs_20_cdd[j], local_constraint_qdbm);
2725b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
2735b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/* 20MHz MCS 0-7 STBC */
2745b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
2755b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		txpwr->mcs_20_stbc[j] =
2765b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		    min(txpwr->mcs_20_stbc[j], local_constraint_qdbm);
2775b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
2785b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/* 20MHz MCS 8-15 MIMO */
2795b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	for (j = 0; j < BRCMS_NUM_RATES_MCS_2_STREAM; j++)
2805b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		txpwr->mcs_20_mimo[j] =
2815b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		    min(txpwr->mcs_20_mimo[j], local_constraint_qdbm);
2825b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
2835b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/* 40MHz MCS 0-7 SISO */
2845b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
2855b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		txpwr->mcs_40_siso[j] =
2865b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		    min(txpwr->mcs_40_siso[j], local_constraint_qdbm);
2875b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
2885b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/* 40MHz MCS 0-7 CDD */
2895b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
2905b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		txpwr->mcs_40_cdd[j] =
2915b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		    min(txpwr->mcs_40_cdd[j], local_constraint_qdbm);
2925b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
2935b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/* 40MHz MCS 0-7 STBC */
2945b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	for (j = 0; j < BRCMS_NUM_RATES_MCS_1_STREAM; j++)
2955b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		txpwr->mcs_40_stbc[j] =
2965b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		    min(txpwr->mcs_40_stbc[j], local_constraint_qdbm);
2975b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
2985b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/* 40MHz MCS 8-15 MIMO */
2995b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	for (j = 0; j < BRCMS_NUM_RATES_MCS_2_STREAM; j++)
3005b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		txpwr->mcs_40_mimo[j] =
3015b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		    min(txpwr->mcs_40_mimo[j], local_constraint_qdbm);
3025b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
3035b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/* 40MHz MCS 32 */
3045b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	txpwr->mcs32 = min(txpwr->mcs32, local_constraint_qdbm);
3055b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
3065b435de0d786869c95d1962121af0d7df2542009Arend van Spriel}
3075b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
3085b435de0d786869c95d1962121af0d7df2542009Arend van Spriel/*
3095b435de0d786869c95d1962121af0d7df2542009Arend van Spriel * set the driver's current country and regulatory information
3105b435de0d786869c95d1962121af0d7df2542009Arend van Spriel * using a country code as the source. Look up built in country
3115b435de0d786869c95d1962121af0d7df2542009Arend van Spriel * information found with the country code.
3125b435de0d786869c95d1962121af0d7df2542009Arend van Spriel */
3135b435de0d786869c95d1962121af0d7df2542009Arend van Sprielstatic void
314cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forsheebrcms_c_set_country(struct brcms_cm_info *wlc_cm,
315cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		    const struct brcms_regd *regd)
3165b435de0d786869c95d1962121af0d7df2542009Arend van Spriel{
3175b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	struct brcms_c_info *wlc = wlc_cm->wlc;
3185b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
3195b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	if ((wlc->pub->_n_enab & SUPPORT_11N) !=
3205b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	    wlc->protection->nmode_user)
3215b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		brcms_c_set_nmode(wlc);
3225b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
3235b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	brcms_c_stf_ss_update(wlc, wlc->bandstate[BAND_2G_INDEX]);
3245b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	brcms_c_stf_ss_update(wlc, wlc->bandstate[BAND_5G_INDEX]);
3255b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
326edc7651f3a4ffa7fca37d92401562694121512adSeth Forshee	brcms_c_set_gmode(wlc, wlc->protection->gmode_user, false);
3275b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
3285b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	return;
3295b435de0d786869c95d1962121af0d7df2542009Arend van Spriel}
3305b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
3315b435de0d786869c95d1962121af0d7df2542009Arend van Sprielstruct brcms_cm_info *brcms_c_channel_mgr_attach(struct brcms_c_info *wlc)
3325b435de0d786869c95d1962121af0d7df2542009Arend van Spriel{
3335b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	struct brcms_cm_info *wlc_cm;
3345b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	struct brcms_pub *pub = wlc->pub;
335898d3c3b2462cd439edb575f43b732425693aff0Hauke Mehrtens	struct ssb_sprom *sprom = &wlc->hw->d11core->bus->sprom;
336cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	const char *ccode = sprom->alpha2;
337cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	int ccode_len = sizeof(sprom->alpha2);
3385b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
3395b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	BCMMSG(wlc->wiphy, "wl%d\n", wlc->pub->unit);
3405b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
3415b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	wlc_cm = kzalloc(sizeof(struct brcms_cm_info), GFP_ATOMIC);
3425b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	if (wlc_cm == NULL)
3435b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		return NULL;
3445b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	wlc_cm->pub = pub;
3455b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	wlc_cm->wlc = wlc;
3465b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	wlc->cmi = wlc_cm;
3475b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
3485b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/* store the country code for passing up as a regulatory hint */
349cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	wlc_cm->world_regd = brcms_world_regd(ccode, ccode_len);
350cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	if (brcms_c_country_valid(ccode))
351cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		strncpy(wlc->pub->srom_ccode, ccode, ccode_len);
352cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee
353cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	/*
354cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	 * If no custom world domain is found in the SROM, use the
355cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	 * default "X2" domain.
356cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	 */
357cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	if (!wlc_cm->world_regd) {
358cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		wlc_cm->world_regd = brcms_default_world_regd();
359cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		ccode = wlc_cm->world_regd->regdomain->alpha2;
360cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		ccode_len = BRCM_CNTRY_BUF_SZ - 1;
361cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	}
3625b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
3635b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/* save default country for exiting 11d regulatory mode */
364cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	strncpy(wlc->country_default, ccode, ccode_len);
3655b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
3665b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/* initialize autocountry_default to driver default */
367cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	strncpy(wlc->autocountry_default, ccode, ccode_len);
3685b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
369cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	brcms_c_set_country(wlc_cm, wlc_cm->world_regd);
3705b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
3715b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	return wlc_cm;
3725b435de0d786869c95d1962121af0d7df2542009Arend van Spriel}
3735b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
3745b435de0d786869c95d1962121af0d7df2542009Arend van Sprielvoid brcms_c_channel_mgr_detach(struct brcms_cm_info *wlc_cm)
3755b435de0d786869c95d1962121af0d7df2542009Arend van Spriel{
3765b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	kfree(wlc_cm);
3775b435de0d786869c95d1962121af0d7df2542009Arend van Spriel}
3785b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
3795b435de0d786869c95d1962121af0d7df2542009Arend van Sprielvoid
3805b435de0d786869c95d1962121af0d7df2542009Arend van Sprielbrcms_c_channel_set_chanspec(struct brcms_cm_info *wlc_cm, u16 chanspec,
3815b435de0d786869c95d1962121af0d7df2542009Arend van Spriel			 u8 local_constraint_qdbm)
3825b435de0d786869c95d1962121af0d7df2542009Arend van Spriel{
3835b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	struct brcms_c_info *wlc = wlc_cm->wlc;
384853346d8b60738467581d1cd9ccb678aeb086121Seth Forshee	struct ieee80211_channel *ch = wlc->pub->ieee_hw->conf.channel;
385edc7651f3a4ffa7fca37d92401562694121512adSeth Forshee	const struct ieee80211_reg_rule *reg_rule;
3865b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	struct txpwr_limits txpwr;
387edc7651f3a4ffa7fca37d92401562694121512adSeth Forshee	int ret;
3885b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
3895b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	brcms_c_channel_reg_limits(wlc_cm, chanspec, &txpwr);
3905b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
3915b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	brcms_c_channel_min_txpower_limits_with_local_constraint(
3925b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		wlc_cm, &txpwr, local_constraint_qdbm
3935b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	);
3945b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
395edc7651f3a4ffa7fca37d92401562694121512adSeth Forshee	/* set or restore gmode as required by regulatory */
396edc7651f3a4ffa7fca37d92401562694121512adSeth Forshee	ret = freq_reg_info(wlc->wiphy, ch->center_freq, 0, &reg_rule);
397edc7651f3a4ffa7fca37d92401562694121512adSeth Forshee	if (!ret && (reg_rule->flags & NL80211_RRF_NO_OFDM))
398edc7651f3a4ffa7fca37d92401562694121512adSeth Forshee		brcms_c_set_gmode(wlc, GMODE_LEGACY_B, false);
399edc7651f3a4ffa7fca37d92401562694121512adSeth Forshee	else
400edc7651f3a4ffa7fca37d92401562694121512adSeth Forshee		brcms_c_set_gmode(wlc, wlc->protection->gmode_user, false);
401edc7651f3a4ffa7fca37d92401562694121512adSeth Forshee
4025b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	brcms_b_set_chanspec(wlc->hw, chanspec,
403853346d8b60738467581d1cd9ccb678aeb086121Seth Forshee			      !!(ch->flags & IEEE80211_CHAN_PASSIVE_SCAN),
4045b435de0d786869c95d1962121af0d7df2542009Arend van Spriel			      &txpwr);
4055b435de0d786869c95d1962121af0d7df2542009Arend van Spriel}
4065b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
4075b435de0d786869c95d1962121af0d7df2542009Arend van Sprielvoid
4085b435de0d786869c95d1962121af0d7df2542009Arend van Sprielbrcms_c_channel_reg_limits(struct brcms_cm_info *wlc_cm, u16 chanspec,
4095b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		       struct txpwr_limits *txpwr)
4105b435de0d786869c95d1962121af0d7df2542009Arend van Spriel{
4115b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	struct brcms_c_info *wlc = wlc_cm->wlc;
4122cf5089ed5ae4a8f73427234cb83c42eaaf34d46Seth Forshee	struct ieee80211_channel *ch = wlc->pub->ieee_hw->conf.channel;
4135b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	uint i;
4145b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	uint chan;
4155b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	int maxpwr;
4165b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	int delta;
4175b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	const struct country_info *country;
4185b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	struct brcms_band *band;
4195b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	int conducted_max = BRCMS_TXPWR_MAX;
4205b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	const struct locale_mimo_info *li_mimo;
4215b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	int maxpwr20, maxpwr40;
4225b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	int maxpwr_idx;
4235b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	uint j;
4245b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
4255b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	memset(txpwr, 0, sizeof(struct txpwr_limits));
4265b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
4272cf5089ed5ae4a8f73427234cb83c42eaaf34d46Seth Forshee	if (WARN_ON(!ch))
4282cf5089ed5ae4a8f73427234cb83c42eaaf34d46Seth Forshee		return;
4292cf5089ed5ae4a8f73427234cb83c42eaaf34d46Seth Forshee
430cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	country = &wlc_cm->world_regd->country;
4315b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
4325b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	chan = CHSPEC_CHANNEL(chanspec);
4335b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	band = wlc->bandstate[chspec_bandunit(chanspec)];
4345b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	li_mimo = (band->bandtype == BRCM_BAND_5G) ?
4355b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	    brcms_c_get_mimo_5g(country->locale_mimo_5G) :
4365b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	    brcms_c_get_mimo_2g(country->locale_mimo_2G);
4375b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
4382cf5089ed5ae4a8f73427234cb83c42eaaf34d46Seth Forshee	delta = band->antgain;
4395b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
440edc7651f3a4ffa7fca37d92401562694121512adSeth Forshee	if (band->bandtype == BRCM_BAND_2G)
4415b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		conducted_max = QDB(22);
4422cf5089ed5ae4a8f73427234cb83c42eaaf34d46Seth Forshee
4432cf5089ed5ae4a8f73427234cb83c42eaaf34d46Seth Forshee	maxpwr = QDB(ch->max_power) - delta;
4442cf5089ed5ae4a8f73427234cb83c42eaaf34d46Seth Forshee	maxpwr = max(maxpwr, 0);
4452cf5089ed5ae4a8f73427234cb83c42eaaf34d46Seth Forshee	maxpwr = min(maxpwr, conducted_max);
4465b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
4475b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/* CCK txpwr limits for 2.4G band */
4485b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	if (band->bandtype == BRCM_BAND_2G) {
4495b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		for (i = 0; i < BRCMS_NUM_RATES_CCK; i++)
4505b435de0d786869c95d1962121af0d7df2542009Arend van Spriel			txpwr->cck[i] = (u8) maxpwr;
4515b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	}
4525b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
4532cf5089ed5ae4a8f73427234cb83c42eaaf34d46Seth Forshee	for (i = 0; i < BRCMS_NUM_RATES_OFDM; i++) {
4545b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		txpwr->ofdm[i] = (u8) maxpwr;
4555b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
4565b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		/*
4575b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		 * OFDM 40 MHz SISO has the same power as the corresponding
4585b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		 * MCS0-7 rate unless overriden by the locale specific code.
4595b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		 * We set this value to 0 as a flag (presumably 0 dBm isn't
4605b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		 * a possibility) and then copy the MCS0-7 value to the 40 MHz
4615b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		 * value if it wasn't explicitly set.
4625b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		 */
4635b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		txpwr->ofdm_40_siso[i] = 0;
4645b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
4655b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		txpwr->ofdm_cdd[i] = (u8) maxpwr;
4665b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
4675b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		txpwr->ofdm_40_cdd[i] = 0;
4685b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	}
4695b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
4702cf5089ed5ae4a8f73427234cb83c42eaaf34d46Seth Forshee	delta = 0;
4712cf5089ed5ae4a8f73427234cb83c42eaaf34d46Seth Forshee	if (band->antgain > QDB(6))
4722cf5089ed5ae4a8f73427234cb83c42eaaf34d46Seth Forshee		delta = band->antgain - QDB(6);	/* Excess over 6 dB */
4735b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
4745b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	if (band->bandtype == BRCM_BAND_2G)
4755b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		maxpwr_idx = (chan - 1);
4765b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	else
4775b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		maxpwr_idx = CHANNEL_POWER_IDX_5G(chan);
4785b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
4795b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	maxpwr20 = li_mimo->maxpwr20[maxpwr_idx];
4805b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	maxpwr40 = li_mimo->maxpwr40[maxpwr_idx];
4815b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
4825b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	maxpwr20 = maxpwr20 - delta;
4835b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	maxpwr20 = max(maxpwr20, 0);
4845b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	maxpwr40 = maxpwr40 - delta;
4855b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	maxpwr40 = max(maxpwr40, 0);
4865b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
4875b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/* Fill in the MCS 0-7 (SISO) rates */
4885b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
4895b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
4905b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		/*
4915b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		 * 20 MHz has the same power as the corresponding OFDM rate
4925b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		 * unless overriden by the locale specific code.
4935b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		 */
4945b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		txpwr->mcs_20_siso[i] = txpwr->ofdm[i];
4955b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		txpwr->mcs_40_siso[i] = 0;
4965b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	}
4975b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
4985b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/* Fill in the MCS 0-7 CDD rates */
4995b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
5005b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		txpwr->mcs_20_cdd[i] = (u8) maxpwr20;
5015b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		txpwr->mcs_40_cdd[i] = (u8) maxpwr40;
5025b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	}
5035b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
5045b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/*
5055b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	 * These locales have SISO expressed in the
5065b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	 * table and override CDD later
5075b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	 */
5085b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	if (li_mimo == &locale_bn) {
5095b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		if (li_mimo == &locale_bn) {
5105b435de0d786869c95d1962121af0d7df2542009Arend van Spriel			maxpwr20 = QDB(16);
5115b435de0d786869c95d1962121af0d7df2542009Arend van Spriel			maxpwr40 = 0;
5125b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
5135b435de0d786869c95d1962121af0d7df2542009Arend van Spriel			if (chan >= 3 && chan <= 11)
5145b435de0d786869c95d1962121af0d7df2542009Arend van Spriel				maxpwr40 = QDB(16);
5155b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		}
5165b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
5175b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
5185b435de0d786869c95d1962121af0d7df2542009Arend van Spriel			txpwr->mcs_20_siso[i] = (u8) maxpwr20;
5195b435de0d786869c95d1962121af0d7df2542009Arend van Spriel			txpwr->mcs_40_siso[i] = (u8) maxpwr40;
5205b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		}
5215b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	}
5225b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
5235b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/* Fill in the MCS 0-7 STBC rates */
5245b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
5255b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		txpwr->mcs_20_stbc[i] = 0;
5265b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		txpwr->mcs_40_stbc[i] = 0;
5275b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	}
5285b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
5295b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/* Fill in the MCS 8-15 SDM rates */
5305b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	for (i = 0; i < BRCMS_NUM_RATES_MCS_2_STREAM; i++) {
5315b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		txpwr->mcs_20_mimo[i] = (u8) maxpwr20;
5325b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		txpwr->mcs_40_mimo[i] = (u8) maxpwr40;
5335b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	}
5345b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
5355b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/* Fill in MCS32 */
5365b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	txpwr->mcs32 = (u8) maxpwr40;
5375b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
5385b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	for (i = 0, j = 0; i < BRCMS_NUM_RATES_OFDM; i++, j++) {
5395b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		if (txpwr->ofdm_40_cdd[i] == 0)
5405b435de0d786869c95d1962121af0d7df2542009Arend van Spriel			txpwr->ofdm_40_cdd[i] = txpwr->mcs_40_cdd[j];
5415b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		if (i == 0) {
5425b435de0d786869c95d1962121af0d7df2542009Arend van Spriel			i = i + 1;
5435b435de0d786869c95d1962121af0d7df2542009Arend van Spriel			if (txpwr->ofdm_40_cdd[i] == 0)
5445b435de0d786869c95d1962121af0d7df2542009Arend van Spriel				txpwr->ofdm_40_cdd[i] = txpwr->mcs_40_cdd[j];
5455b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		}
5465b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	}
5475b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
5485b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/*
5495b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	 * Copy the 40 MHZ MCS 0-7 CDD value to the 40 MHZ MCS 0-7 SISO
5505b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	 * value if it wasn't provided explicitly.
5515b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	 */
5525b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
5535b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		if (txpwr->mcs_40_siso[i] == 0)
5545b435de0d786869c95d1962121af0d7df2542009Arend van Spriel			txpwr->mcs_40_siso[i] = txpwr->mcs_40_cdd[i];
5555b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	}
5565b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
5575b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	for (i = 0, j = 0; i < BRCMS_NUM_RATES_OFDM; i++, j++) {
5585b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		if (txpwr->ofdm_40_siso[i] == 0)
5595b435de0d786869c95d1962121af0d7df2542009Arend van Spriel			txpwr->ofdm_40_siso[i] = txpwr->mcs_40_siso[j];
5605b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		if (i == 0) {
5615b435de0d786869c95d1962121af0d7df2542009Arend van Spriel			i = i + 1;
5625b435de0d786869c95d1962121af0d7df2542009Arend van Spriel			if (txpwr->ofdm_40_siso[i] == 0)
5635b435de0d786869c95d1962121af0d7df2542009Arend van Spriel				txpwr->ofdm_40_siso[i] = txpwr->mcs_40_siso[j];
5645b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		}
5655b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	}
5665b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
5675b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/*
5685b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	 * Copy the 20 and 40 MHz MCS0-7 CDD values to the corresponding
5695b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	 * STBC values if they weren't provided explicitly.
5705b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	 */
5715b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	for (i = 0; i < BRCMS_NUM_RATES_MCS_1_STREAM; i++) {
5725b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		if (txpwr->mcs_20_stbc[i] == 0)
5735b435de0d786869c95d1962121af0d7df2542009Arend van Spriel			txpwr->mcs_20_stbc[i] = txpwr->mcs_20_cdd[i];
5745b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
5755b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		if (txpwr->mcs_40_stbc[i] == 0)
5765b435de0d786869c95d1962121af0d7df2542009Arend van Spriel			txpwr->mcs_40_stbc[i] = txpwr->mcs_40_cdd[i];
5775b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	}
5785b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
5795b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	return;
5805b435de0d786869c95d1962121af0d7df2542009Arend van Spriel}
5815b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
5825b435de0d786869c95d1962121af0d7df2542009Arend van Spriel/*
5833de67818e7e7ef95c8f7eac665dd29454c07b48bAlwin Beukers * Verify the chanspec is using a legal set of parameters, i.e. that the
5843de67818e7e7ef95c8f7eac665dd29454c07b48bAlwin Beukers * chanspec specified a band, bw, ctl_sb and channel and that the
5853de67818e7e7ef95c8f7eac665dd29454c07b48bAlwin Beukers * combination could be legal given any set of circumstances.
5863de67818e7e7ef95c8f7eac665dd29454c07b48bAlwin Beukers * RETURNS: true is the chanspec is malformed, false if it looks good.
5873de67818e7e7ef95c8f7eac665dd29454c07b48bAlwin Beukers */
5883de67818e7e7ef95c8f7eac665dd29454c07b48bAlwin Beukersstatic bool brcms_c_chspec_malformed(u16 chanspec)
5893de67818e7e7ef95c8f7eac665dd29454c07b48bAlwin Beukers{
5903de67818e7e7ef95c8f7eac665dd29454c07b48bAlwin Beukers	/* must be 2G or 5G band */
5913de67818e7e7ef95c8f7eac665dd29454c07b48bAlwin Beukers	if (!CHSPEC_IS5G(chanspec) && !CHSPEC_IS2G(chanspec))
5923de67818e7e7ef95c8f7eac665dd29454c07b48bAlwin Beukers		return true;
5933de67818e7e7ef95c8f7eac665dd29454c07b48bAlwin Beukers	/* must be 20 or 40 bandwidth */
5943de67818e7e7ef95c8f7eac665dd29454c07b48bAlwin Beukers	if (!CHSPEC_IS40(chanspec) && !CHSPEC_IS20(chanspec))
5953de67818e7e7ef95c8f7eac665dd29454c07b48bAlwin Beukers		return true;
5963de67818e7e7ef95c8f7eac665dd29454c07b48bAlwin Beukers
5973de67818e7e7ef95c8f7eac665dd29454c07b48bAlwin Beukers	/* 20MHZ b/w must have no ctl sb, 40 must have a ctl sb */
5983de67818e7e7ef95c8f7eac665dd29454c07b48bAlwin Beukers	if (CHSPEC_IS20(chanspec)) {
5993de67818e7e7ef95c8f7eac665dd29454c07b48bAlwin Beukers		if (!CHSPEC_SB_NONE(chanspec))
6003de67818e7e7ef95c8f7eac665dd29454c07b48bAlwin Beukers			return true;
6013de67818e7e7ef95c8f7eac665dd29454c07b48bAlwin Beukers	} else if (!CHSPEC_SB_UPPER(chanspec) && !CHSPEC_SB_LOWER(chanspec)) {
6023de67818e7e7ef95c8f7eac665dd29454c07b48bAlwin Beukers		return true;
6033de67818e7e7ef95c8f7eac665dd29454c07b48bAlwin Beukers	}
6043de67818e7e7ef95c8f7eac665dd29454c07b48bAlwin Beukers
6053de67818e7e7ef95c8f7eac665dd29454c07b48bAlwin Beukers	return false;
6063de67818e7e7ef95c8f7eac665dd29454c07b48bAlwin Beukers}
6073de67818e7e7ef95c8f7eac665dd29454c07b48bAlwin Beukers
6083de67818e7e7ef95c8f7eac665dd29454c07b48bAlwin Beukers/*
6095b435de0d786869c95d1962121af0d7df2542009Arend van Spriel * Validate the chanspec for this locale, for 40MHZ we need to also
6105b435de0d786869c95d1962121af0d7df2542009Arend van Spriel * check that the sidebands are valid 20MZH channels in this locale
6115b435de0d786869c95d1962121af0d7df2542009Arend van Spriel * and they are also a legal HT combination
6125b435de0d786869c95d1962121af0d7df2542009Arend van Spriel */
6135b435de0d786869c95d1962121af0d7df2542009Arend van Sprielstatic bool
614853346d8b60738467581d1cd9ccb678aeb086121Seth Forsheebrcms_c_valid_chanspec_ext(struct brcms_cm_info *wlc_cm, u16 chspec)
6155b435de0d786869c95d1962121af0d7df2542009Arend van Spriel{
6165b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	struct brcms_c_info *wlc = wlc_cm->wlc;
6175b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	u8 channel = CHSPEC_CHANNEL(chspec);
6185b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
6195b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	/* check the chanspec */
6203de67818e7e7ef95c8f7eac665dd29454c07b48bAlwin Beukers	if (brcms_c_chspec_malformed(chspec)) {
6215b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		wiphy_err(wlc->wiphy, "wl%d: malformed chanspec 0x%x\n",
6225b435de0d786869c95d1962121af0d7df2542009Arend van Spriel			wlc->pub->unit, chspec);
6235b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		return false;
6245b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	}
6255b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
6265b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	if (CHANNEL_BANDUNIT(wlc_cm->wlc, channel) !=
6275b435de0d786869c95d1962121af0d7df2542009Arend van Spriel	    chspec_bandunit(chspec))
6285b435de0d786869c95d1962121af0d7df2542009Arend van Spriel		return false;
6295b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
630853346d8b60738467581d1cd9ccb678aeb086121Seth Forshee	return true;
6315b435de0d786869c95d1962121af0d7df2542009Arend van Spriel}
6325b435de0d786869c95d1962121af0d7df2542009Arend van Spriel
6335b435de0d786869c95d1962121af0d7df2542009Arend van Sprielbool brcms_c_valid_chanspec_db(struct brcms_cm_info *wlc_cm, u16 chspec)
6345b435de0d786869c95d1962121af0d7df2542009Arend van Spriel{
635853346d8b60738467581d1cd9ccb678aeb086121Seth Forshee	return brcms_c_valid_chanspec_ext(wlc_cm, chspec);
6365b435de0d786869c95d1962121af0d7df2542009Arend van Spriel}
637cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee
638cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forsheestatic bool brcms_is_radar_freq(u16 center_freq)
639cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee{
640cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	return center_freq >= 5260 && center_freq <= 5700;
641cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee}
642cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee
643cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forsheestatic void brcms_reg_apply_radar_flags(struct wiphy *wiphy)
644cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee{
645cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	struct ieee80211_supported_band *sband;
646cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	struct ieee80211_channel *ch;
647cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	int i;
648cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee
649cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	sband = wiphy->bands[IEEE80211_BAND_5GHZ];
650cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	if (!sband)
651cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		return;
652cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee
653cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	for (i = 0; i < sband->n_channels; i++) {
654cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		ch = &sband->channels[i];
655cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee
656cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		if (!brcms_is_radar_freq(ch->center_freq))
657cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee			continue;
658cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee
659cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		/*
660cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		 * All channels in this range should be passive and have
661cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		 * DFS enabled.
662cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		 */
663cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		if (!(ch->flags & IEEE80211_CHAN_DISABLED))
664cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee			ch->flags |= IEEE80211_CHAN_RADAR |
665cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee				     IEEE80211_CHAN_NO_IBSS |
666cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee				     IEEE80211_CHAN_PASSIVE_SCAN;
667cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	}
668cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee}
669cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee
670cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forsheestatic void
671cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forsheebrcms_reg_apply_beaconing_flags(struct wiphy *wiphy,
672cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee				enum nl80211_reg_initiator initiator)
673cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee{
674cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	struct ieee80211_supported_band *sband;
675cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	struct ieee80211_channel *ch;
676cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	const struct ieee80211_reg_rule *rule;
677cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	int band, i, ret;
678cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee
679cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
680cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		sband = wiphy->bands[band];
681cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		if (!sband)
682cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee			continue;
683cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee
684cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		for (i = 0; i < sband->n_channels; i++) {
685cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee			ch = &sband->channels[i];
686cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee
687cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee			if (ch->flags &
688cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee			    (IEEE80211_CHAN_DISABLED | IEEE80211_CHAN_RADAR))
689cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee				continue;
690cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee
691cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee			if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
692cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee				ret = freq_reg_info(wiphy, ch->center_freq,
693cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee						    0, &rule);
694cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee				if (ret)
695cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee					continue;
696cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee
697cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee				if (!(rule->flags & NL80211_RRF_NO_IBSS))
698cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee					ch->flags &= ~IEEE80211_CHAN_NO_IBSS;
699cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee				if (!(rule->flags & NL80211_RRF_PASSIVE_SCAN))
700cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee					ch->flags &=
701cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee						~IEEE80211_CHAN_PASSIVE_SCAN;
702cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee			} else if (ch->beacon_found) {
703cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee				ch->flags &= ~(IEEE80211_CHAN_NO_IBSS |
704cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee					       IEEE80211_CHAN_PASSIVE_SCAN);
705cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee			}
706cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		}
707cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	}
708cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee}
709cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee
710cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forsheestatic int brcms_reg_notifier(struct wiphy *wiphy,
711cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee			      struct regulatory_request *request)
712cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee{
713cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
714cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	struct brcms_info *wl = hw->priv;
715cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	struct brcms_c_info *wlc = wl->wlc;
7162ab631f48cac65864a61007215187cb7511b0846Seth Forshee	struct ieee80211_supported_band *sband;
7172ab631f48cac65864a61007215187cb7511b0846Seth Forshee	struct ieee80211_channel *ch;
7182ab631f48cac65864a61007215187cb7511b0846Seth Forshee	int band, i;
7192ab631f48cac65864a61007215187cb7511b0846Seth Forshee	bool ch_found = false;
720cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee
721cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	brcms_reg_apply_radar_flags(wiphy);
722cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee
723cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	if (request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE)
724cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		brcms_reg_apply_beaconing_flags(wiphy, request->initiator);
725cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee
7262ab631f48cac65864a61007215187cb7511b0846Seth Forshee	/* Disable radio if all channels disallowed by regulatory */
7272ab631f48cac65864a61007215187cb7511b0846Seth Forshee	for (band = 0; !ch_found && band < IEEE80211_NUM_BANDS; band++) {
7282ab631f48cac65864a61007215187cb7511b0846Seth Forshee		sband = wiphy->bands[band];
7292ab631f48cac65864a61007215187cb7511b0846Seth Forshee		if (!sband)
7302ab631f48cac65864a61007215187cb7511b0846Seth Forshee			continue;
7312ab631f48cac65864a61007215187cb7511b0846Seth Forshee
7322ab631f48cac65864a61007215187cb7511b0846Seth Forshee		for (i = 0; !ch_found && i < sband->n_channels; i++) {
7332ab631f48cac65864a61007215187cb7511b0846Seth Forshee			ch = &sband->channels[i];
7342ab631f48cac65864a61007215187cb7511b0846Seth Forshee
7352ab631f48cac65864a61007215187cb7511b0846Seth Forshee			if (!(ch->flags & IEEE80211_CHAN_DISABLED))
7362ab631f48cac65864a61007215187cb7511b0846Seth Forshee				ch_found = true;
7372ab631f48cac65864a61007215187cb7511b0846Seth Forshee		}
7382ab631f48cac65864a61007215187cb7511b0846Seth Forshee	}
7392ab631f48cac65864a61007215187cb7511b0846Seth Forshee
7402ab631f48cac65864a61007215187cb7511b0846Seth Forshee	if (ch_found) {
7412ab631f48cac65864a61007215187cb7511b0846Seth Forshee		mboolclr(wlc->pub->radio_disabled, WL_RADIO_COUNTRY_DISABLE);
7422ab631f48cac65864a61007215187cb7511b0846Seth Forshee	} else {
7432ab631f48cac65864a61007215187cb7511b0846Seth Forshee		mboolset(wlc->pub->radio_disabled, WL_RADIO_COUNTRY_DISABLE);
7442ab631f48cac65864a61007215187cb7511b0846Seth Forshee		wiphy_err(wlc->wiphy, "wl%d: %s: no valid channel for \"%s\"\n",
7452ab631f48cac65864a61007215187cb7511b0846Seth Forshee			  wlc->pub->unit, __func__, request->alpha2);
7462ab631f48cac65864a61007215187cb7511b0846Seth Forshee	}
7472ab631f48cac65864a61007215187cb7511b0846Seth Forshee
748cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	if (wlc->pub->_nbands > 1 || wlc->band->bandtype == BRCM_BAND_2G)
749cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		wlc_phy_chanspec_ch14_widefilter_set(wlc->band->pi,
750cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee					brcms_c_japan_ccode(request->alpha2));
751cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee
752cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	return 0;
753cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee}
754cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee
755cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forsheevoid brcms_c_regd_init(struct brcms_c_info *wlc)
756cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee{
757cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	struct wiphy *wiphy = wlc->wiphy;
758cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	const struct brcms_regd *regd = wlc->cmi->world_regd;
759cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	struct ieee80211_supported_band *sband;
760cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	struct ieee80211_channel *ch;
761cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	struct brcms_chanvec sup_chan;
762cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	struct brcms_band *band;
763cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	int band_idx, i;
764cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee
765cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	/* Disable any channels not supported by the phy */
766cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	for (band_idx = 0; band_idx < IEEE80211_NUM_BANDS; band_idx++) {
767cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		if (band_idx == IEEE80211_BAND_2GHZ)
768cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee			band = wlc->bandstate[BAND_2G_INDEX];
769cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		else
770cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee			band = wlc->bandstate[BAND_5G_INDEX];
771c49aa4aa2bc89e88672dc419a293d7b8c1f094d2Arend van Spriel
772c49aa4aa2bc89e88672dc419a293d7b8c1f094d2Arend van Spriel		/* skip if band not initialized */
773c49aa4aa2bc89e88672dc419a293d7b8c1f094d2Arend van Spriel		if (band->pi == NULL)
774c49aa4aa2bc89e88672dc419a293d7b8c1f094d2Arend van Spriel			continue;
775c49aa4aa2bc89e88672dc419a293d7b8c1f094d2Arend van Spriel
776cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		wlc_phy_chanspec_band_validch(band->pi, band->bandtype,
777cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee					      &sup_chan);
778cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee
779cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		sband = wiphy->bands[band_idx];
780cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		for (i = 0; i < sband->n_channels; i++) {
781cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee			ch = &sband->channels[i];
782cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee			if (!isset(sup_chan.vec, ch->hw_value))
783cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee				ch->flags |= IEEE80211_CHAN_DISABLED;
784cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee		}
785cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	}
786cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee
787cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	wlc->wiphy->reg_notifier = brcms_reg_notifier;
788cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	wlc->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
789cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee			     WIPHY_FLAG_STRICT_REGULATORY;
790cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	wiphy_apply_custom_regulatory(wlc->wiphy, regd->regdomain);
791cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee	brcms_reg_apply_beaconing_flags(wiphy, NL80211_REGDOM_SET_BY_DRIVER);
792cf03c5dac83609f09d9f0e9fa3c09d86daed614dSeth Forshee}
793