blob: 89e36568e70f71861c0b5ab1a988a6ac4ea998bd [file] [log] [blame]
Larry Finger0c817332010-12-08 11:12:31 -06001/******************************************************************************
2 *
Larry Fingera8d76062012-01-07 20:46:42 -06003 * Copyright(c) 2009-2012 Realtek Corporation.
Larry Finger0c817332010-12-08 11:12:31 -06004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
21 * Contact Information:
22 * wlanfae <wlanfae@realtek.com>
23 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
24 * Hsinchu 300, Taiwan.
25 *
26 * Larry Finger <Larry.Finger@lwfinger.net>
27 *
28 *****************************************************************************/
29
30#include "wifi.h"
31#include "regd.h"
32
33static struct country_code_to_enum_rd allCountries[] = {
34 {COUNTRY_CODE_FCC, "US"},
35 {COUNTRY_CODE_IC, "US"},
36 {COUNTRY_CODE_ETSI, "EC"},
37 {COUNTRY_CODE_SPAIN, "EC"},
38 {COUNTRY_CODE_FRANCE, "EC"},
39 {COUNTRY_CODE_MKK, "JP"},
40 {COUNTRY_CODE_MKK1, "JP"},
41 {COUNTRY_CODE_ISRAEL, "EC"},
42 {COUNTRY_CODE_TELEC, "JP"},
43 {COUNTRY_CODE_MIC, "JP"},
44 {COUNTRY_CODE_GLOBAL_DOMAIN, "JP"},
45 {COUNTRY_CODE_WORLD_WIDE_13, "EC"},
46 {COUNTRY_CODE_TELEC_NETGEAR, "EC"},
47};
48
49/*
50 *Only these channels all allow active
51 *scan on all world regulatory domains
52 */
53#define RTL819x_2GHZ_CH01_11 \
54 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0)
55
56/*
57 *We enable active scan on these a case
58 *by case basis by regulatory domain
59 */
60#define RTL819x_2GHZ_CH12_13 \
61 REG_RULE(2467-10, 2472+10, 40, 0, 20,\
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +020062 NL80211_RRF_NO_IR)
Larry Finger0c817332010-12-08 11:12:31 -060063
64#define RTL819x_2GHZ_CH14 \
65 REG_RULE(2484-10, 2484+10, 40, 0, 20, \
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +020066 NL80211_RRF_NO_IR | NL80211_RRF_NO_OFDM)
Larry Finger0c817332010-12-08 11:12:31 -060067
Chaoming_Li81b29042011-04-25 12:53:24 -050068/* 5G chan 36 - chan 64*/
69#define RTL819x_5GHZ_5150_5350 \
70 REG_RULE(5150-10, 5350+10, 40, 0, 30, \
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +020071 NL80211_RRF_NO_IR)
Chaoming_Li81b29042011-04-25 12:53:24 -050072
73/* 5G chan 100 - chan 165*/
74#define RTL819x_5GHZ_5470_5850 \
75 REG_RULE(5470-10, 5850+10, 40, 0, 30, \
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +020076 NL80211_RRF_NO_IR)
Chaoming_Li81b29042011-04-25 12:53:24 -050077
78/* 5G chan 149 - chan 165*/
79#define RTL819x_5GHZ_5725_5850 \
80 REG_RULE(5725-10, 5850+10, 40, 0, 30, \
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +020081 NL80211_RRF_NO_IR)
Chaoming_Li81b29042011-04-25 12:53:24 -050082
83#define RTL819x_5GHZ_ALL \
84 (RTL819x_5GHZ_5150_5350, RTL819x_5GHZ_5470_5850)
85
Larry Finger0c817332010-12-08 11:12:31 -060086static const struct ieee80211_regdomain rtl_regdom_11 = {
87 .n_reg_rules = 1,
88 .alpha2 = "99",
89 .reg_rules = {
90 RTL819x_2GHZ_CH01_11,
Chaoming_Li81b29042011-04-25 12:53:24 -050091 }
Larry Finger0c817332010-12-08 11:12:31 -060092};
93
Chaoming_Li81b29042011-04-25 12:53:24 -050094static const struct ieee80211_regdomain rtl_regdom_12_13 = {
Larry Finger0c817332010-12-08 11:12:31 -060095 .n_reg_rules = 2,
96 .alpha2 = "99",
97 .reg_rules = {
98 RTL819x_2GHZ_CH01_11,
Chaoming_Li81b29042011-04-25 12:53:24 -050099 RTL819x_2GHZ_CH12_13,
100 }
101};
102
103static const struct ieee80211_regdomain rtl_regdom_no_midband = {
104 .n_reg_rules = 3,
105 .alpha2 = "99",
106 .reg_rules = {
107 RTL819x_2GHZ_CH01_11,
108 RTL819x_5GHZ_5150_5350,
109 RTL819x_5GHZ_5725_5850,
110 }
111};
112
113static const struct ieee80211_regdomain rtl_regdom_60_64 = {
114 .n_reg_rules = 3,
115 .alpha2 = "99",
116 .reg_rules = {
117 RTL819x_2GHZ_CH01_11,
118 RTL819x_2GHZ_CH12_13,
119 RTL819x_5GHZ_5725_5850,
120 }
121};
122
123static const struct ieee80211_regdomain rtl_regdom_14_60_64 = {
124 .n_reg_rules = 4,
125 .alpha2 = "99",
126 .reg_rules = {
127 RTL819x_2GHZ_CH01_11,
128 RTL819x_2GHZ_CH12_13,
129 RTL819x_2GHZ_CH14,
130 RTL819x_5GHZ_5725_5850,
131 }
132};
133
134static const struct ieee80211_regdomain rtl_regdom_14 = {
135 .n_reg_rules = 3,
136 .alpha2 = "99",
137 .reg_rules = {
138 RTL819x_2GHZ_CH01_11,
139 RTL819x_2GHZ_CH12_13,
140 RTL819x_2GHZ_CH14,
141 }
Larry Finger0c817332010-12-08 11:12:31 -0600142};
143
144static bool _rtl_is_radar_freq(u16 center_freq)
145{
146 return (center_freq >= 5260 && center_freq <= 5700);
147}
148
149static void _rtl_reg_apply_beaconing_flags(struct wiphy *wiphy,
150 enum nl80211_reg_initiator initiator)
151{
152 enum ieee80211_band band;
153 struct ieee80211_supported_band *sband;
154 const struct ieee80211_reg_rule *reg_rule;
155 struct ieee80211_channel *ch;
156 unsigned int i;
Larry Finger0c817332010-12-08 11:12:31 -0600157
158 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
159
160 if (!wiphy->bands[band])
161 continue;
162
163 sband = wiphy->bands[band];
164
165 for (i = 0; i < sband->n_channels; i++) {
166 ch = &sband->channels[i];
167 if (_rtl_is_radar_freq(ch->center_freq) ||
168 (ch->flags & IEEE80211_CHAN_RADAR))
169 continue;
170 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
Johannes Berg361c9c82012-12-06 15:57:14 +0100171 reg_rule = freq_reg_info(wiphy, ch->center_freq);
172 if (IS_ERR(reg_rule))
Larry Finger0c817332010-12-08 11:12:31 -0600173 continue;
174
175 /*
176 *If 11d had a rule for this channel ensure
177 *we enable adhoc/beaconing if it allows us to
178 *use it. Note that we would have disabled it
179 *by applying our static world regdomain by
180 *default during init, prior to calling our
181 *regulatory_hint().
182 */
183
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200184 if (!(reg_rule->flags & NL80211_RRF_NO_IR))
185 ch->flags &= ~IEEE80211_CHAN_NO_IR;
Larry Finger0c817332010-12-08 11:12:31 -0600186 } else {
187 if (ch->beacon_found)
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200188 ch->flags &= ~IEEE80211_CHAN_NO_IR;
Larry Finger0c817332010-12-08 11:12:31 -0600189 }
190 }
191 }
192}
193
194/* Allows active scan scan on Ch 12 and 13 */
195static void _rtl_reg_apply_active_scan_flags(struct wiphy *wiphy,
196 enum nl80211_reg_initiator
197 initiator)
198{
199 struct ieee80211_supported_band *sband;
200 struct ieee80211_channel *ch;
201 const struct ieee80211_reg_rule *reg_rule;
Larry Finger0c817332010-12-08 11:12:31 -0600202
Chaoming_Li81b29042011-04-25 12:53:24 -0500203 if (!wiphy->bands[IEEE80211_BAND_2GHZ])
204 return;
Larry Finger0c817332010-12-08 11:12:31 -0600205 sband = wiphy->bands[IEEE80211_BAND_2GHZ];
206
207 /*
208 *If no country IE has been received always enable active scan
209 *on these channels. This is only done for specific regulatory SKUs
210 */
211 if (initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
212 ch = &sband->channels[11]; /* CH 12 */
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200213 if (ch->flags & IEEE80211_CHAN_NO_IR)
214 ch->flags &= ~IEEE80211_CHAN_NO_IR;
Larry Finger0c817332010-12-08 11:12:31 -0600215 ch = &sband->channels[12]; /* CH 13 */
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200216 if (ch->flags & IEEE80211_CHAN_NO_IR)
217 ch->flags &= ~IEEE80211_CHAN_NO_IR;
Larry Finger0c817332010-12-08 11:12:31 -0600218 return;
219 }
220
221 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300222 *If a country IE has been received check its rule for this
Larry Finger0c817332010-12-08 11:12:31 -0600223 *channel first before enabling active scan. The passive scan
224 *would have been enforced by the initial processing of our
225 *custom regulatory domain.
226 */
227
228 ch = &sband->channels[11]; /* CH 12 */
Johannes Berg361c9c82012-12-06 15:57:14 +0100229 reg_rule = freq_reg_info(wiphy, ch->center_freq);
230 if (!IS_ERR(reg_rule)) {
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200231 if (!(reg_rule->flags & NL80211_RRF_NO_IR))
232 if (ch->flags & IEEE80211_CHAN_NO_IR)
233 ch->flags &= ~IEEE80211_CHAN_NO_IR;
Larry Finger0c817332010-12-08 11:12:31 -0600234 }
235
236 ch = &sband->channels[12]; /* CH 13 */
Johannes Berg361c9c82012-12-06 15:57:14 +0100237 reg_rule = freq_reg_info(wiphy, ch->center_freq);
238 if (!IS_ERR(reg_rule)) {
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200239 if (!(reg_rule->flags & NL80211_RRF_NO_IR))
240 if (ch->flags & IEEE80211_CHAN_NO_IR)
241 ch->flags &= ~IEEE80211_CHAN_NO_IR;
Larry Finger0c817332010-12-08 11:12:31 -0600242 }
243}
244
245/*
246 *Always apply Radar/DFS rules on
247 *freq range 5260 MHz - 5700 MHz
248 */
249static void _rtl_reg_apply_radar_flags(struct wiphy *wiphy)
250{
251 struct ieee80211_supported_band *sband;
252 struct ieee80211_channel *ch;
253 unsigned int i;
254
255 if (!wiphy->bands[IEEE80211_BAND_5GHZ])
256 return;
257
258 sband = wiphy->bands[IEEE80211_BAND_5GHZ];
259
260 for (i = 0; i < sband->n_channels; i++) {
261 ch = &sband->channels[i];
262 if (!_rtl_is_radar_freq(ch->center_freq))
263 continue;
264
265 /*
266 *We always enable radar detection/DFS on this
267 *frequency range. Additionally we also apply on
268 *this frequency range:
269 *- If STA mode does not yet have DFS supports disable
270 * active scanning
271 *- If adhoc mode does not support DFS yet then disable
272 * adhoc in the frequency.
273 *- If AP mode does not yet support radar detection/DFS
274 *do not allow AP mode
275 */
276 if (!(ch->flags & IEEE80211_CHAN_DISABLED))
277 ch->flags |= IEEE80211_CHAN_RADAR |
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200278 IEEE80211_CHAN_NO_IR;
Larry Finger0c817332010-12-08 11:12:31 -0600279 }
280}
281
282static void _rtl_reg_apply_world_flags(struct wiphy *wiphy,
283 enum nl80211_reg_initiator initiator,
284 struct rtl_regulatory *reg)
285{
286 _rtl_reg_apply_beaconing_flags(wiphy, initiator);
287 _rtl_reg_apply_active_scan_flags(wiphy, initiator);
288 return;
289}
290
Luis R. Rodriguez0c0280b2013-01-11 18:39:36 +0000291static void _rtl_reg_notifier_apply(struct wiphy *wiphy,
292 struct regulatory_request *request,
293 struct rtl_regulatory *reg)
Larry Finger0c817332010-12-08 11:12:31 -0600294{
295 /* We always apply this */
296 _rtl_reg_apply_radar_flags(wiphy);
297
298 switch (request->initiator) {
299 case NL80211_REGDOM_SET_BY_DRIVER:
300 case NL80211_REGDOM_SET_BY_CORE:
301 case NL80211_REGDOM_SET_BY_USER:
302 break;
303 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
304 _rtl_reg_apply_world_flags(wiphy, request->initiator, reg);
305 break;
306 }
Larry Finger0c817332010-12-08 11:12:31 -0600307}
308
309static const struct ieee80211_regdomain *_rtl_regdomain_select(
310 struct rtl_regulatory *reg)
311{
312 switch (reg->country_code) {
313 case COUNTRY_CODE_FCC:
Chaoming_Li81b29042011-04-25 12:53:24 -0500314 return &rtl_regdom_no_midband;
Larry Finger0c817332010-12-08 11:12:31 -0600315 case COUNTRY_CODE_IC:
316 return &rtl_regdom_11;
317 case COUNTRY_CODE_ETSI:
Chaoming_Li81b29042011-04-25 12:53:24 -0500318 case COUNTRY_CODE_TELEC_NETGEAR:
319 return &rtl_regdom_60_64;
Larry Finger0c817332010-12-08 11:12:31 -0600320 case COUNTRY_CODE_SPAIN:
321 case COUNTRY_CODE_FRANCE:
322 case COUNTRY_CODE_ISRAEL:
Chaoming_Li81b29042011-04-25 12:53:24 -0500323 case COUNTRY_CODE_WORLD_WIDE_13:
324 return &rtl_regdom_12_13;
Larry Finger0c817332010-12-08 11:12:31 -0600325 case COUNTRY_CODE_MKK:
326 case COUNTRY_CODE_MKK1:
327 case COUNTRY_CODE_TELEC:
328 case COUNTRY_CODE_MIC:
Chaoming_Li81b29042011-04-25 12:53:24 -0500329 return &rtl_regdom_14_60_64;
Larry Finger0c817332010-12-08 11:12:31 -0600330 case COUNTRY_CODE_GLOBAL_DOMAIN:
Chaoming_Li81b29042011-04-25 12:53:24 -0500331 return &rtl_regdom_14;
Larry Finger0c817332010-12-08 11:12:31 -0600332 default:
Chaoming_Li81b29042011-04-25 12:53:24 -0500333 return &rtl_regdom_no_midband;
Larry Finger0c817332010-12-08 11:12:31 -0600334 }
335}
336
337static int _rtl_regd_init_wiphy(struct rtl_regulatory *reg,
338 struct wiphy *wiphy,
Luis R. Rodriguez0c0280b2013-01-11 18:39:36 +0000339 void (*reg_notifier) (struct wiphy *wiphy,
340 struct regulatory_request *
341 request))
Larry Finger0c817332010-12-08 11:12:31 -0600342{
343 const struct ieee80211_regdomain *regd;
344
345 wiphy->reg_notifier = reg_notifier;
Chaoming_Li81b29042011-04-25 12:53:24 -0500346
Luis R. Rodrigueza2f73b62013-11-11 22:15:29 +0100347 wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
348 wiphy->regulatory_flags &= ~REGULATORY_STRICT_REG;
349 wiphy->regulatory_flags &= ~REGULATORY_DISABLE_BEACON_HINTS;
Chaoming_Li81b29042011-04-25 12:53:24 -0500350
Larry Finger0c817332010-12-08 11:12:31 -0600351 regd = _rtl_regdomain_select(reg);
352 wiphy_apply_custom_regulatory(wiphy, regd);
353 _rtl_reg_apply_radar_flags(wiphy);
354 _rtl_reg_apply_world_flags(wiphy, NL80211_REGDOM_SET_BY_DRIVER, reg);
355 return 0;
356}
357
358static struct country_code_to_enum_rd *_rtl_regd_find_country(u16 countrycode)
359{
360 int i;
361
362 for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
363 if (allCountries[i].countrycode == countrycode)
364 return &allCountries[i];
365 }
366 return NULL;
367}
368
369int rtl_regd_init(struct ieee80211_hw *hw,
Luis R. Rodriguez0c0280b2013-01-11 18:39:36 +0000370 void (*reg_notifier) (struct wiphy *wiphy,
Larry Finger0c817332010-12-08 11:12:31 -0600371 struct regulatory_request *request))
372{
373 struct rtl_priv *rtlpriv = rtl_priv(hw);
374 struct wiphy *wiphy = hw->wiphy;
375 struct country_code_to_enum_rd *country = NULL;
376
377 if (wiphy == NULL || &rtlpriv->regd == NULL)
378 return -EINVAL;
379
Chaoming_Li81b29042011-04-25 12:53:24 -0500380 /* init country_code from efuse channel plan */
381 rtlpriv->regd.country_code = rtlpriv->efuse.channel_plan;
Larry Finger0c817332010-12-08 11:12:31 -0600382
383 RT_TRACE(rtlpriv, COMP_REGD, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -0800384 "rtl: EEPROM regdomain: 0x%0x\n", rtlpriv->regd.country_code);
Larry Finger0c817332010-12-08 11:12:31 -0600385
386 if (rtlpriv->regd.country_code >= COUNTRY_CODE_MAX) {
387 RT_TRACE(rtlpriv, COMP_REGD, DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -0800388 "rtl: EEPROM indicates invalid contry code, world wide 13 should be used\n");
Larry Finger0c817332010-12-08 11:12:31 -0600389
390 rtlpriv->regd.country_code = COUNTRY_CODE_WORLD_WIDE_13;
391 }
392
393 country = _rtl_regd_find_country(rtlpriv->regd.country_code);
394
395 if (country) {
Chaoming_Li81b29042011-04-25 12:53:24 -0500396 rtlpriv->regd.alpha2[0] = country->iso_name[0];
397 rtlpriv->regd.alpha2[1] = country->iso_name[1];
Larry Finger0c817332010-12-08 11:12:31 -0600398 } else {
399 rtlpriv->regd.alpha2[0] = '0';
400 rtlpriv->regd.alpha2[1] = '0';
401 }
402
403 RT_TRACE(rtlpriv, COMP_REGD, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -0800404 "rtl: Country alpha2 being used: %c%c\n",
405 rtlpriv->regd.alpha2[0], rtlpriv->regd.alpha2[1]);
Larry Finger0c817332010-12-08 11:12:31 -0600406
407 _rtl_regd_init_wiphy(&rtlpriv->regd, wiphy, reg_notifier);
408
409 return 0;
410}
411
Luis R. Rodriguez0c0280b2013-01-11 18:39:36 +0000412void rtl_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
Larry Finger0c817332010-12-08 11:12:31 -0600413{
414 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
415 struct rtl_priv *rtlpriv = rtl_priv(hw);
416
Joe Perchesf30d7502012-01-04 19:40:41 -0800417 RT_TRACE(rtlpriv, COMP_REGD, DBG_LOUD, "\n");
Larry Finger0c817332010-12-08 11:12:31 -0600418
Luis R. Rodriguez0c0280b2013-01-11 18:39:36 +0000419 _rtl_reg_notifier_apply(wiphy, request, &rtlpriv->regd);
Larry Finger0c817332010-12-08 11:12:31 -0600420}