1 /******************************************************************************
3 * Copyright(c) 2009-2010 Realtek Corporation.
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.
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
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
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
21 * Contact Information:
22 * wlanfae <wlanfae@realtek.com>
23 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
24 * Hsinchu 300, Taiwan.
26 * Larry Finger <Larry.Finger@lwfinger.net>
28 *****************************************************************************/
33 static 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 {COUNTRY_CODE_WORLD_WIDE_13_5G_ALL, "US"},
51 *Only these channels all allow active
52 *scan on all world regulatory domains
54 #define RTL819x_2GHZ_CH01_11 \
55 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0)
58 *We enable active scan on these a case
59 *by case basis by regulatory domain
61 #define RTL819x_2GHZ_CH12_13 \
62 REG_RULE(2467-10, 2472+10, 40, 0, 20,\
63 NL80211_RRF_PASSIVE_SCAN)
65 #define RTL819x_2GHZ_CH14 \
66 REG_RULE(2484-10, 2484+10, 40, 0, 20, \
67 NL80211_RRF_PASSIVE_SCAN | \
70 /* 5G chan 36 - chan 64*/
71 #define RTL819x_5GHZ_5150_5350 \
72 REG_RULE(5150-10, 5350+10, 40, 0, 30, \
73 NL80211_RRF_PASSIVE_SCAN | \
76 /* 5G chan 100 - chan 165*/
77 #define RTL819x_5GHZ_5470_5850 \
78 REG_RULE(5470-10, 5850+10, 40, 0, 30, \
79 NL80211_RRF_PASSIVE_SCAN | \
82 /* 5G chan 149 - chan 165*/
83 #define RTL819x_5GHZ_5725_5850 \
84 REG_RULE(5725-10, 5850+10, 40, 0, 30, \
85 NL80211_RRF_PASSIVE_SCAN | \
88 #define RTL819x_5GHZ_ALL \
89 (RTL819x_5GHZ_5150_5350, RTL819x_5GHZ_5470_5850)
91 static const struct ieee80211_regdomain rtl_regdom_11 = {
99 static const struct ieee80211_regdomain rtl_regdom_12_13 = {
103 RTL819x_2GHZ_CH01_11,
104 RTL819x_2GHZ_CH12_13,
108 static const struct ieee80211_regdomain rtl_regdom_no_midband = {
112 RTL819x_2GHZ_CH01_11,
113 RTL819x_5GHZ_5150_5350,
114 RTL819x_5GHZ_5725_5850,
118 static const struct ieee80211_regdomain rtl_regdom_60_64 = {
122 RTL819x_2GHZ_CH01_11,
123 RTL819x_2GHZ_CH12_13,
124 RTL819x_5GHZ_5725_5850,
128 static const struct ieee80211_regdomain rtl_regdom_14_60_64 = {
132 RTL819x_2GHZ_CH01_11,
133 RTL819x_2GHZ_CH12_13,
135 RTL819x_5GHZ_5725_5850,
139 static const struct ieee80211_regdomain rtl_regdom_12_13_5g_all = {
143 RTL819x_2GHZ_CH01_11,
144 RTL819x_2GHZ_CH12_13,
145 RTL819x_5GHZ_5150_5350,
146 RTL819x_5GHZ_5470_5850,
150 static const struct ieee80211_regdomain rtl_regdom_14 = {
154 RTL819x_2GHZ_CH01_11,
155 RTL819x_2GHZ_CH12_13,
160 static bool _rtl_is_radar_freq(u16 center_freq)
162 return (center_freq >= 5260 && center_freq <= 5700);
165 static void _rtl_reg_apply_beaconing_flags(struct wiphy *wiphy,
166 enum nl80211_reg_initiator initiator)
168 enum ieee80211_band band;
169 struct ieee80211_supported_band *sband;
170 const struct ieee80211_reg_rule *reg_rule;
171 struct ieee80211_channel *ch;
176 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
178 if (!wiphy->bands[band])
181 sband = wiphy->bands[band];
183 for (i = 0; i < sband->n_channels; i++) {
184 ch = &sband->channels[i];
185 if (_rtl_is_radar_freq(ch->center_freq) ||
186 (ch->flags & IEEE80211_CHAN_RADAR))
188 if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
189 r = freq_reg_info(wiphy, ch->center_freq,
190 bandwidth, ®_rule);
195 *If 11d had a rule for this channel ensure
196 *we enable adhoc/beaconing if it allows us to
197 *use it. Note that we would have disabled it
198 *by applying our static world regdomain by
199 *default during init, prior to calling our
203 if (!(reg_rule->flags & NL80211_RRF_NO_IBSS))
204 ch->flags &= ~IEEE80211_CHAN_NO_IBSS;
206 flags & NL80211_RRF_PASSIVE_SCAN))
208 ~IEEE80211_CHAN_PASSIVE_SCAN;
210 if (ch->beacon_found)
211 ch->flags &= ~(IEEE80211_CHAN_NO_IBSS |
212 IEEE80211_CHAN_PASSIVE_SCAN);
218 /* Allows active scan scan on Ch 12 and 13 */
219 static void _rtl_reg_apply_active_scan_flags(struct wiphy *wiphy,
220 enum nl80211_reg_initiator
223 struct ieee80211_supported_band *sband;
224 struct ieee80211_channel *ch;
225 const struct ieee80211_reg_rule *reg_rule;
229 if (!wiphy->bands[IEEE80211_BAND_2GHZ])
231 sband = wiphy->bands[IEEE80211_BAND_2GHZ];
234 *If no country IE has been received always enable active scan
235 *on these channels. This is only done for specific regulatory SKUs
237 if (initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
238 ch = &sband->channels[11]; /* CH 12 */
239 if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
240 ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
241 ch = &sband->channels[12]; /* CH 13 */
242 if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
243 ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
248 *If a country IE has been received check its rule for this
249 *channel first before enabling active scan. The passive scan
250 *would have been enforced by the initial processing of our
251 *custom regulatory domain.
254 ch = &sband->channels[11]; /* CH 12 */
255 r = freq_reg_info(wiphy, ch->center_freq, bandwidth, ®_rule);
257 if (!(reg_rule->flags & NL80211_RRF_PASSIVE_SCAN))
258 if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
259 ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
262 ch = &sband->channels[12]; /* CH 13 */
263 r = freq_reg_info(wiphy, ch->center_freq, bandwidth, ®_rule);
265 if (!(reg_rule->flags & NL80211_RRF_PASSIVE_SCAN))
266 if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
267 ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
272 *Always apply Radar/DFS rules on
273 *freq range 5260 MHz - 5700 MHz
275 static void _rtl_reg_apply_radar_flags(struct wiphy *wiphy)
277 struct ieee80211_supported_band *sband;
278 struct ieee80211_channel *ch;
281 if (!wiphy->bands[IEEE80211_BAND_5GHZ])
284 sband = wiphy->bands[IEEE80211_BAND_5GHZ];
286 for (i = 0; i < sband->n_channels; i++) {
287 ch = &sband->channels[i];
288 if (!_rtl_is_radar_freq(ch->center_freq))
292 *We always enable radar detection/DFS on this
293 *frequency range. Additionally we also apply on
294 *this frequency range:
295 *- If STA mode does not yet have DFS supports disable
297 *- If adhoc mode does not support DFS yet then disable
298 * adhoc in the frequency.
299 *- If AP mode does not yet support radar detection/DFS
300 *do not allow AP mode
302 if (!(ch->flags & IEEE80211_CHAN_DISABLED))
303 ch->flags |= IEEE80211_CHAN_RADAR |
304 IEEE80211_CHAN_NO_IBSS |
305 IEEE80211_CHAN_PASSIVE_SCAN;
309 static void _rtl_reg_apply_world_flags(struct wiphy *wiphy,
310 enum nl80211_reg_initiator initiator,
311 struct rtl_regulatory *reg)
313 _rtl_reg_apply_beaconing_flags(wiphy, initiator);
314 _rtl_reg_apply_active_scan_flags(wiphy, initiator);
318 static int _rtl_reg_notifier_apply(struct wiphy *wiphy,
319 struct regulatory_request *request,
320 struct rtl_regulatory *reg)
322 /* We always apply this */
323 _rtl_reg_apply_radar_flags(wiphy);
325 switch (request->initiator) {
326 case NL80211_REGDOM_SET_BY_DRIVER:
327 case NL80211_REGDOM_SET_BY_CORE:
328 case NL80211_REGDOM_SET_BY_USER:
330 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
331 _rtl_reg_apply_world_flags(wiphy, request->initiator, reg);
338 static const struct ieee80211_regdomain *_rtl_regdomain_select(
339 struct rtl_regulatory *reg)
341 switch (reg->country_code) {
342 case COUNTRY_CODE_FCC:
343 return &rtl_regdom_no_midband;
344 case COUNTRY_CODE_IC:
345 return &rtl_regdom_11;
346 case COUNTRY_CODE_TELEC_NETGEAR:
347 return &rtl_regdom_60_64;
348 case COUNTRY_CODE_ETSI:
349 case COUNTRY_CODE_SPAIN:
350 case COUNTRY_CODE_FRANCE:
351 case COUNTRY_CODE_ISRAEL:
352 case COUNTRY_CODE_WORLD_WIDE_13:
353 return &rtl_regdom_12_13;
354 case COUNTRY_CODE_MKK:
355 case COUNTRY_CODE_MKK1:
356 case COUNTRY_CODE_TELEC:
357 case COUNTRY_CODE_MIC:
358 return &rtl_regdom_14_60_64;
359 case COUNTRY_CODE_GLOBAL_DOMAIN:
360 return &rtl_regdom_14;
361 case COUNTRY_CODE_WORLD_WIDE_13_5G_ALL:
362 return &rtl_regdom_12_13_5g_all;
364 return &rtl_regdom_no_midband;
368 static int _rtl_regd_init_wiphy(struct rtl_regulatory *reg,
370 int (*reg_notifier) (struct wiphy *wiphy,
371 struct regulatory_request *
374 const struct ieee80211_regdomain *regd;
376 wiphy->reg_notifier = reg_notifier;
378 wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
379 wiphy->flags &= ~WIPHY_FLAG_STRICT_REGULATORY;
380 wiphy->flags &= ~WIPHY_FLAG_DISABLE_BEACON_HINTS;
382 regd = _rtl_regdomain_select(reg);
383 wiphy_apply_custom_regulatory(wiphy, regd);
384 _rtl_reg_apply_radar_flags(wiphy);
385 _rtl_reg_apply_world_flags(wiphy, NL80211_REGDOM_SET_BY_DRIVER, reg);
389 static struct country_code_to_enum_rd *_rtl_regd_find_country(u16 countrycode)
393 for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
394 if (allCountries[i].countrycode == countrycode)
395 return &allCountries[i];
400 static u8 channel_plan_to_country_code(u8 channelplan)
402 switch (channelplan) {
405 return COUNTRY_CODE_WORLD_WIDE_13;
407 return COUNTRY_CODE_IC;
409 return COUNTRY_CODE_ETSI;
411 return COUNTRY_CODE_TELEC_NETGEAR;
413 return COUNTRY_CODE_GLOBAL_DOMAIN;
415 return COUNTRY_CODE_WORLD_WIDE_13_5G_ALL;
417 return COUNTRY_CODE_MAX; /*Error*/
421 int rtl_regd_init(struct ieee80211_hw *hw,
422 int (*reg_notifier) (struct wiphy *wiphy,
423 struct regulatory_request *request))
425 struct rtl_priv *rtlpriv = rtl_priv(hw);
426 struct wiphy *wiphy = hw->wiphy;
427 struct country_code_to_enum_rd *country = NULL;
429 if (wiphy == NULL || &rtlpriv->regd == NULL)
432 /* init country_code from efuse channel plan */
433 rtlpriv->regd.country_code =
434 channel_plan_to_country_code(rtlpriv->efuse.channel_plan);
436 RT_TRACE(rtlpriv, COMP_REGD, DBG_DMESG,
437 (KERN_DEBUG "rtl: EEPROM regdomain: 0x%0x conuntry code: %d\n",
438 rtlpriv->efuse.channel_plan, rtlpriv->regd.country_code));
440 if (rtlpriv->regd.country_code >= COUNTRY_CODE_MAX) {
441 RT_TRACE(rtlpriv, COMP_REGD, DBG_DMESG,
442 (KERN_DEBUG "rtl: EEPROM indicates invalid contry code"
443 "world wide 13 should be used\n"));
445 rtlpriv->regd.country_code = COUNTRY_CODE_WORLD_WIDE_13;
448 country = _rtl_regd_find_country(rtlpriv->regd.country_code);
451 rtlpriv->regd.alpha2[0] = country->iso_name[0];
452 rtlpriv->regd.alpha2[1] = country->iso_name[1];
454 rtlpriv->regd.alpha2[0] = '0';
455 rtlpriv->regd.alpha2[1] = '0';
458 RT_TRACE(rtlpriv, COMP_REGD, DBG_TRACE,
459 (KERN_DEBUG "rtl: Country alpha2 being used: %c%c\n",
460 rtlpriv->regd.alpha2[0], rtlpriv->regd.alpha2[1]));
462 _rtl_regd_init_wiphy(&rtlpriv->regd, wiphy, reg_notifier);
467 int rtl_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
469 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
470 struct rtl_priv *rtlpriv = rtl_priv(hw);
472 RT_TRACE(rtlpriv, COMP_REGD, DBG_LOUD, ("\n"));
474 return _rtl_reg_notifier_apply(wiphy, request, &rtlpriv->regd);