1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2009-2012  Realtek Corporation.*/
3 
4 #include "wifi.h"
5 #include "regd.h"
6 
7 static struct country_code_to_enum_rd all_countries[] = {
8 	{COUNTRY_CODE_FCC, "US"},
9 	{COUNTRY_CODE_IC, "US"},
10 	{COUNTRY_CODE_ETSI, "EC"},
11 	{COUNTRY_CODE_SPAIN, "EC"},
12 	{COUNTRY_CODE_FRANCE, "EC"},
13 	{COUNTRY_CODE_MKK, "JP"},
14 	{COUNTRY_CODE_MKK1, "JP"},
15 	{COUNTRY_CODE_ISRAEL, "EC"},
16 	{COUNTRY_CODE_TELEC, "JP"},
17 	{COUNTRY_CODE_MIC, "JP"},
18 	{COUNTRY_CODE_GLOBAL_DOMAIN, "JP"},
19 	{COUNTRY_CODE_WORLD_WIDE_13, "EC"},
20 	{COUNTRY_CODE_TELEC_NETGEAR, "EC"},
21 	{COUNTRY_CODE_WORLD_WIDE_13_5G_ALL, "US"},
22 };
23 
24 /*
25  *Only these channels all allow active
26  *scan on all world regulatory domains
27  */
28 #define RTL819x_2GHZ_CH01_11	\
29 	REG_RULE(2412-10, 2462+10, 40, 0, 20, 0)
30 
31 /*
32  *We enable active scan on these a case
33  *by case basis by regulatory domain
34  */
35 #define RTL819x_2GHZ_CH12_13	\
36 	REG_RULE(2467-10, 2472+10, 40, 0, 20,\
37 	NL80211_RRF_PASSIVE_SCAN)
38 
39 #define RTL819x_2GHZ_CH14	\
40 	REG_RULE(2484-10, 2484+10, 40, 0, 20, \
41 	NL80211_RRF_PASSIVE_SCAN | \
42 	NL80211_RRF_NO_OFDM)
43 
44 /* 5G chan 36 - chan 64*/
45 #define RTL819x_5GHZ_5150_5350	\
46 	REG_RULE(5150-10, 5350+10, 80, 0, 30, 0)
47 /* 5G chan 100 - chan 165*/
48 #define RTL819x_5GHZ_5470_5850	\
49 	REG_RULE(5470-10, 5850+10, 80, 0, 30, 0)
50 /* 5G chan 149 - chan 165*/
51 #define RTL819x_5GHZ_5725_5850	\
52 	REG_RULE(5725-10, 5850+10, 80, 0, 30, 0)
53 
54 #define RTL819x_5GHZ_ALL	\
55 	(RTL819x_5GHZ_5150_5350, RTL819x_5GHZ_5470_5850)
56 
57 static const struct ieee80211_regdomain rtl_regdom_11 = {
58 	.n_reg_rules = 1,
59 	.alpha2 = "99",
60 	.reg_rules = {
61 		      RTL819x_2GHZ_CH01_11,
62 		      }
63 };
64 
65 static const struct ieee80211_regdomain rtl_regdom_12_13 = {
66 	.n_reg_rules = 2,
67 	.alpha2 = "99",
68 	.reg_rules = {
69 		      RTL819x_2GHZ_CH01_11,
70 			  RTL819x_2GHZ_CH12_13,
71 		      }
72 };
73 
74 static const struct ieee80211_regdomain rtl_regdom_no_midband = {
75 	.n_reg_rules = 3,
76 	.alpha2 = "99",
77 	.reg_rules = {
78 		      RTL819x_2GHZ_CH01_11,
79 			  RTL819x_5GHZ_5150_5350,
80 			  RTL819x_5GHZ_5725_5850,
81 		      }
82 };
83 
84 static const struct ieee80211_regdomain rtl_regdom_60_64 = {
85 	.n_reg_rules = 3,
86 	.alpha2 = "99",
87 	.reg_rules = {
88 		      RTL819x_2GHZ_CH01_11,
89 			  RTL819x_2GHZ_CH12_13,
90 			  RTL819x_5GHZ_5725_5850,
91 		      }
92 };
93 
94 static const struct ieee80211_regdomain rtl_regdom_14_60_64 = {
95 	.n_reg_rules = 4,
96 	.alpha2 = "99",
97 	.reg_rules = {
98 		      RTL819x_2GHZ_CH01_11,
99 			  RTL819x_2GHZ_CH12_13,
100 			  RTL819x_2GHZ_CH14,
101 			  RTL819x_5GHZ_5725_5850,
102 		      }
103 };
104 
105 static const struct ieee80211_regdomain rtl_regdom_12_13_5g_all = {
106 	.n_reg_rules = 4,
107 	.alpha2 = "99",
108 	.reg_rules = {
109 			RTL819x_2GHZ_CH01_11,
110 			RTL819x_2GHZ_CH12_13,
111 			RTL819x_5GHZ_5150_5350,
112 			RTL819x_5GHZ_5470_5850,
113 		}
114 };
115 
116 static const struct ieee80211_regdomain rtl_regdom_14 = {
117 	.n_reg_rules = 3,
118 	.alpha2 = "99",
119 	.reg_rules = {
120 		      RTL819x_2GHZ_CH01_11,
121 			  RTL819x_2GHZ_CH12_13,
122 			  RTL819x_2GHZ_CH14,
123 		      }
124 };
125 
126 static bool _rtl_is_radar_freq(u16 center_freq)
127 {
128 	return center_freq >= 5260 && center_freq <= 5700;
129 }
130 
131 static void _rtl_reg_apply_beaconing_flags(struct wiphy *wiphy,
132 					   enum nl80211_reg_initiator initiator)
133 {
134 	enum nl80211_band band;
135 	struct ieee80211_supported_band *sband;
136 	const struct ieee80211_reg_rule *reg_rule;
137 	struct ieee80211_channel *ch;
138 	unsigned int i;
139 
140 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
141 
142 		if (!wiphy->bands[band])
143 			continue;
144 
145 		sband = wiphy->bands[band];
146 
147 		for (i = 0; i < sband->n_channels; i++) {
148 			ch = &sband->channels[i];
149 			if (_rtl_is_radar_freq(ch->center_freq) ||
150 			    (ch->flags & IEEE80211_CHAN_RADAR))
151 				continue;
152 			if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
153 				reg_rule = freq_reg_info(wiphy,
154 							 ch->center_freq);
155 				if (IS_ERR(reg_rule))
156 					continue;
157 				/*
158 				 *If 11d had a rule for this channel ensure
159 				 *we enable adhoc/beaconing if it allows us to
160 				 *use it. Note that we would have disabled it
161 				 *by applying our static world regdomain by
162 				 *default during init, prior to calling our
163 				 *regulatory_hint().
164 				 */
165 
166 				if (!(reg_rule->flags & NL80211_RRF_NO_IBSS))
167 					ch->flags &= ~IEEE80211_CHAN_NO_IBSS;
168 				if (!(reg_rule->flags &
169 				      NL80211_RRF_PASSIVE_SCAN))
170 					ch->flags &=
171 					    ~IEEE80211_CHAN_PASSIVE_SCAN;
172 			} else {
173 				if (ch->beacon_found)
174 					ch->flags &= ~(IEEE80211_CHAN_NO_IBSS |
175 						   IEEE80211_CHAN_PASSIVE_SCAN);
176 			}
177 		}
178 	}
179 }
180 
181 /* Allows active scan scan on Ch 12 and 13 */
182 static void _rtl_reg_apply_active_scan_flags(struct wiphy *wiphy,
183 					     enum nl80211_reg_initiator
184 					     initiator)
185 {
186 	struct ieee80211_supported_band *sband;
187 	struct ieee80211_channel *ch;
188 	const struct ieee80211_reg_rule *reg_rule;
189 
190 	if (!wiphy->bands[NL80211_BAND_2GHZ])
191 		return;
192 	sband = wiphy->bands[NL80211_BAND_2GHZ];
193 
194 	/*
195 	 *If no country IE has been received always enable active scan
196 	 *on these channels. This is only done for specific regulatory SKUs
197 	 */
198 	if (initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
199 		ch = &sband->channels[11];	/* CH 12 */
200 		if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
201 			ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
202 		ch = &sband->channels[12];	/* CH 13 */
203 		if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
204 			ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
205 		return;
206 	}
207 
208 	/*
209 	 *If a country IE has been recieved check its rule for this
210 	 *channel first before enabling active scan. The passive scan
211 	 *would have been enforced by the initial processing of our
212 	 *custom regulatory domain.
213 	 */
214 
215 	ch = &sband->channels[11];	/* CH 12 */
216 	reg_rule = freq_reg_info(wiphy, ch->center_freq);
217 	if (!IS_ERR(reg_rule)) {
218 		if (!(reg_rule->flags & NL80211_RRF_PASSIVE_SCAN))
219 			if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
220 				ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
221 	}
222 
223 	ch = &sband->channels[12];	/* CH 13 */
224 	reg_rule = freq_reg_info(wiphy, ch->center_freq);
225 	if (!IS_ERR(reg_rule)) {
226 		if (!(reg_rule->flags & NL80211_RRF_PASSIVE_SCAN))
227 			if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
228 				ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
229 	}
230 }
231 
232 /*
233  *Always apply Radar/DFS rules on
234  *freq range 5260 MHz - 5700 MHz
235  */
236 static void _rtl_reg_apply_radar_flags(struct wiphy *wiphy)
237 {
238 	struct ieee80211_supported_band *sband;
239 	struct ieee80211_channel *ch;
240 	unsigned int i;
241 
242 	if (!wiphy->bands[NL80211_BAND_5GHZ])
243 		return;
244 
245 	sband = wiphy->bands[NL80211_BAND_5GHZ];
246 
247 	for (i = 0; i < sband->n_channels; i++) {
248 		ch = &sband->channels[i];
249 		if (!_rtl_is_radar_freq(ch->center_freq))
250 			continue;
251 
252 		/*
253 		 *We always enable radar detection/DFS on this
254 		 *frequency range. Additionally we also apply on
255 		 *this frequency range:
256 		 *- If STA mode does not yet have DFS supports disable
257 		 * active scanning
258 		 *- If adhoc mode does not support DFS yet then disable
259 		 * adhoc in the frequency.
260 		 *- If AP mode does not yet support radar detection/DFS
261 		 *do not allow AP mode
262 		 */
263 		if (!(ch->flags & IEEE80211_CHAN_DISABLED))
264 			ch->flags |= IEEE80211_CHAN_RADAR |
265 			    IEEE80211_CHAN_NO_IBSS |
266 			    IEEE80211_CHAN_PASSIVE_SCAN;
267 	}
268 }
269 
270 static void _rtl_reg_apply_world_flags(struct wiphy *wiphy,
271 				       enum nl80211_reg_initiator initiator,
272 				       struct rtl_regulatory *reg)
273 {
274 	_rtl_reg_apply_beaconing_flags(wiphy, initiator);
275 	_rtl_reg_apply_active_scan_flags(wiphy, initiator);
276 	return;
277 }
278 
279 static void _rtl_dump_channel_map(struct wiphy *wiphy)
280 {
281 	enum nl80211_band band;
282 	struct ieee80211_supported_band *sband;
283 	struct ieee80211_channel *ch;
284 	unsigned int i;
285 
286 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
287 		if (!wiphy->bands[band])
288 			continue;
289 		sband = wiphy->bands[band];
290 		for (i = 0; i < sband->n_channels; i++)
291 			ch = &sband->channels[i];
292 	}
293 }
294 
295 static int _rtl_reg_notifier_apply(struct wiphy *wiphy,
296 				   struct regulatory_request *request,
297 				   struct rtl_regulatory *reg)
298 {
299 	/* We always apply this */
300 	_rtl_reg_apply_radar_flags(wiphy);
301 
302 	switch (request->initiator) {
303 	case NL80211_REGDOM_SET_BY_DRIVER:
304 	case NL80211_REGDOM_SET_BY_CORE:
305 	case NL80211_REGDOM_SET_BY_USER:
306 		break;
307 	case NL80211_REGDOM_SET_BY_COUNTRY_IE:
308 		_rtl_reg_apply_world_flags(wiphy, request->initiator, reg);
309 		break;
310 	}
311 
312 	_rtl_dump_channel_map(wiphy);
313 
314 	return 0;
315 }
316 
317 static const struct ieee80211_regdomain *_rtl_regdomain_select(
318 						struct rtl_regulatory *reg)
319 {
320 	switch (reg->country_code) {
321 	case COUNTRY_CODE_FCC:
322 		return &rtl_regdom_no_midband;
323 	case COUNTRY_CODE_IC:
324 		return &rtl_regdom_11;
325 	case COUNTRY_CODE_TELEC_NETGEAR:
326 		return &rtl_regdom_60_64;
327 	case COUNTRY_CODE_ETSI:
328 	case COUNTRY_CODE_SPAIN:
329 	case COUNTRY_CODE_FRANCE:
330 	case COUNTRY_CODE_ISRAEL:
331 		return &rtl_regdom_12_13;
332 	case COUNTRY_CODE_MKK:
333 	case COUNTRY_CODE_MKK1:
334 	case COUNTRY_CODE_TELEC:
335 	case COUNTRY_CODE_MIC:
336 		return &rtl_regdom_14_60_64;
337 	case COUNTRY_CODE_GLOBAL_DOMAIN:
338 		return &rtl_regdom_14;
339 	case COUNTRY_CODE_WORLD_WIDE_13:
340 	case COUNTRY_CODE_WORLD_WIDE_13_5G_ALL:
341 		return &rtl_regdom_12_13_5g_all;
342 	default:
343 		return &rtl_regdom_no_midband;
344 	}
345 }
346 
347 static int _rtl_regd_init_wiphy(struct rtl_regulatory *reg,
348 				struct wiphy *wiphy,
349 				void (*reg_notifier)(struct wiphy *wiphy,
350 						     struct regulatory_request *
351 						     request))
352 {
353 	const struct ieee80211_regdomain *regd;
354 
355 	wiphy->reg_notifier = reg_notifier;
356 
357 	wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
358 	wiphy->regulatory_flags &= ~REGULATORY_STRICT_REG;
359 	wiphy->regulatory_flags &= ~REGULATORY_DISABLE_BEACON_HINTS;
360 	regd = _rtl_regdomain_select(reg);
361 	wiphy_apply_custom_regulatory(wiphy, regd);
362 	_rtl_reg_apply_radar_flags(wiphy);
363 	_rtl_reg_apply_world_flags(wiphy, NL80211_REGDOM_SET_BY_DRIVER, reg);
364 	return 0;
365 }
366 
367 static struct country_code_to_enum_rd *_rtl_regd_find_country(u16 countrycode)
368 {
369 	int i;
370 
371 	for (i = 0; i < ARRAY_SIZE(all_countries); i++) {
372 		if (all_countries[i].countrycode == countrycode)
373 			return &all_countries[i];
374 	}
375 	return NULL;
376 }
377 
378 static u8 channel_plan_to_country_code(u8 channelplan)
379 {
380 	switch (channelplan) {
381 	case 0x20:
382 	case 0x21:
383 		return COUNTRY_CODE_WORLD_WIDE_13;
384 	case 0x22:
385 		return COUNTRY_CODE_IC;
386 	case 0x25:
387 		return COUNTRY_CODE_ETSI;
388 	case 0x32:
389 		return COUNTRY_CODE_TELEC_NETGEAR;
390 	case 0x41:
391 		return COUNTRY_CODE_GLOBAL_DOMAIN;
392 	case 0x7f:
393 		return COUNTRY_CODE_WORLD_WIDE_13_5G_ALL;
394 	default:
395 		return COUNTRY_CODE_MAX; /*Error*/
396 	}
397 }
398 
399 int rtl_regd_init(struct ieee80211_hw *hw,
400 		  void (*reg_notifier)(struct wiphy *wiphy,
401 				       struct regulatory_request *request))
402 {
403 	struct rtl_priv *rtlpriv = rtl_priv(hw);
404 	struct wiphy *wiphy = hw->wiphy;
405 	struct country_code_to_enum_rd *country = NULL;
406 
407 	if (wiphy == NULL || &rtlpriv->regd == NULL)
408 		return -EINVAL;
409 
410 	/* init country_code from efuse channel plan */
411 	rtlpriv->regd.country_code =
412 		channel_plan_to_country_code(rtlpriv->efuse.channel_plan);
413 
414 	RT_TRACE(rtlpriv, COMP_REGD, DBG_DMESG,
415 		 "rtl: EEPROM regdomain: 0x%0x country code: %d\n",
416 		 rtlpriv->efuse.channel_plan, rtlpriv->regd.country_code);
417 
418 	if (rtlpriv->regd.country_code >= COUNTRY_CODE_MAX) {
419 		RT_TRACE(rtlpriv, COMP_REGD, DBG_DMESG,
420 			 "rtl: EEPROM indicates invalid country code, world wide 13 should be used\n");
421 
422 		rtlpriv->regd.country_code = COUNTRY_CODE_WORLD_WIDE_13;
423 	}
424 
425 	country = _rtl_regd_find_country(rtlpriv->regd.country_code);
426 
427 	if (country) {
428 		rtlpriv->regd.alpha2[0] = country->iso_name[0];
429 		rtlpriv->regd.alpha2[1] = country->iso_name[1];
430 	} else {
431 		rtlpriv->regd.alpha2[0] = '0';
432 		rtlpriv->regd.alpha2[1] = '0';
433 	}
434 
435 	RT_TRACE(rtlpriv, COMP_REGD, DBG_TRACE,
436 		 "rtl: Country alpha2 being used: %c%c\n",
437 		  rtlpriv->regd.alpha2[0], rtlpriv->regd.alpha2[1]);
438 
439 	_rtl_regd_init_wiphy(&rtlpriv->regd, wiphy, reg_notifier);
440 
441 	return 0;
442 }
443 
444 void rtl_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
445 {
446 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
447 	struct rtl_priv *rtlpriv = rtl_priv(hw);
448 
449 	RT_TRACE(rtlpriv, COMP_REGD, DBG_LOUD, "\n");
450 
451 	_rtl_reg_notifier_apply(wiphy, request, &rtlpriv->regd);
452 }
453