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"},
50 *Only these channels all allow active
51 *scan on all world regulatory domains
53 #define RTL819x_2GHZ_CH01_11 \
54 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0)
57 *We enable active scan on these a case
58 *by case basis by regulatory domain
60 #define RTL819x_2GHZ_CH12_13 \
61 REG_RULE(2467-10, 2472+10, 40, 0, 20,\
62 NL80211_RRF_PASSIVE_SCAN)
64 #define RTL819x_2GHZ_CH14 \
65 REG_RULE(2484-10, 2484+10, 40, 0, 20, \
66 NL80211_RRF_PASSIVE_SCAN | \
69 /* 5G chan 36 - chan 64*/
70 #define RTL819x_5GHZ_5150_5350 \
71 REG_RULE(5150-10, 5350+10, 40, 0, 30, \
72 NL80211_RRF_PASSIVE_SCAN | \
75 /* 5G chan 100 - chan 165*/
76 #define RTL819x_5GHZ_5470_5850 \
77 REG_RULE(5470-10, 5850+10, 40, 0, 30, \
78 NL80211_RRF_PASSIVE_SCAN | \
81 /* 5G chan 149 - chan 165*/
82 #define RTL819x_5GHZ_5725_5850 \
83 REG_RULE(5725-10, 5850+10, 40, 0, 30, \
84 NL80211_RRF_PASSIVE_SCAN | \
87 #define RTL819x_5GHZ_ALL \
88 RTL819x_5GHZ_5150_5350, RTL819x_5GHZ_5470_5850
90 static const struct ieee80211_regdomain rtl_regdom_11
= {
98 static const struct ieee80211_regdomain rtl_regdom_12_13
= {
102 RTL819x_2GHZ_CH01_11
,
103 RTL819x_2GHZ_CH12_13
,
107 static const struct ieee80211_regdomain rtl_regdom_no_midband
= {
111 RTL819x_2GHZ_CH01_11
,
112 RTL819x_5GHZ_5150_5350
,
113 RTL819x_5GHZ_5725_5850
,
117 static const struct ieee80211_regdomain rtl_regdom_60_64
= {
121 RTL819x_2GHZ_CH01_11
,
122 RTL819x_2GHZ_CH12_13
,
123 RTL819x_5GHZ_5725_5850
,
127 static const struct ieee80211_regdomain rtl_regdom_14_60_64
= {
131 RTL819x_2GHZ_CH01_11
,
132 RTL819x_2GHZ_CH12_13
,
134 RTL819x_5GHZ_5725_5850
,
138 static const struct ieee80211_regdomain rtl_regdom_14
= {
142 RTL819x_2GHZ_CH01_11
,
143 RTL819x_2GHZ_CH12_13
,
148 static bool _rtl_is_radar_freq(u16 center_freq
)
150 return (center_freq
>= 5260 && center_freq
<= 5700);
153 static void _rtl_reg_apply_beaconing_flags(struct wiphy
*wiphy
,
154 enum nl80211_reg_initiator initiator
)
156 enum ieee80211_band band
;
157 struct ieee80211_supported_band
*sband
;
158 const struct ieee80211_reg_rule
*reg_rule
;
159 struct ieee80211_channel
*ch
;
161 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
163 if (!wiphy
->bands
[band
])
166 sband
= wiphy
->bands
[band
];
168 for (i
= 0; i
< sband
->n_channels
; i
++) {
169 ch
= &sband
->channels
[i
];
170 if (_rtl_is_radar_freq(ch
->center_freq
) ||
171 (ch
->flags
& IEEE80211_CHAN_RADAR
))
173 if (initiator
== NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
174 reg_rule
= freq_reg_info(wiphy
, ch
->center_freq
);
175 if (IS_ERR(reg_rule
))
179 *If 11d had a rule for this channel ensure
180 *we enable adhoc/beaconing if it allows us to
181 *use it. Note that we would have disabled it
182 *by applying our static world regdomain by
183 *default during init, prior to calling our
187 if (!(reg_rule
->flags
& NL80211_RRF_NO_IBSS
))
188 ch
->flags
&= ~IEEE80211_CHAN_NO_IBSS
;
189 if (!(reg_rule
->flags
&
190 NL80211_RRF_PASSIVE_SCAN
))
192 ~IEEE80211_CHAN_PASSIVE_SCAN
;
194 if (ch
->beacon_found
)
195 ch
->flags
&= ~(IEEE80211_CHAN_NO_IBSS
|
196 IEEE80211_CHAN_PASSIVE_SCAN
);
202 /* Allows active scan scan on Ch 12 and 13 */
203 static void _rtl_reg_apply_active_scan_flags(struct wiphy
*wiphy
,
204 enum nl80211_reg_initiator
207 struct ieee80211_supported_band
*sband
;
208 struct ieee80211_channel
*ch
;
209 const struct ieee80211_reg_rule
*reg_rule
;
211 if (!wiphy
->bands
[IEEE80211_BAND_2GHZ
])
213 sband
= wiphy
->bands
[IEEE80211_BAND_2GHZ
];
216 *If no country IE has been received always enable active scan
217 *on these channels. This is only done for specific regulatory SKUs
219 if (initiator
!= NL80211_REGDOM_SET_BY_COUNTRY_IE
) {
220 ch
= &sband
->channels
[11]; /* CH 12 */
221 if (ch
->flags
& IEEE80211_CHAN_PASSIVE_SCAN
)
222 ch
->flags
&= ~IEEE80211_CHAN_PASSIVE_SCAN
;
223 ch
= &sband
->channels
[12]; /* CH 13 */
224 if (ch
->flags
& IEEE80211_CHAN_PASSIVE_SCAN
)
225 ch
->flags
&= ~IEEE80211_CHAN_PASSIVE_SCAN
;
230 *If a country IE has been received check its rule for this
231 *channel first before enabling active scan. The passive scan
232 *would have been enforced by the initial processing of our
233 *custom regulatory domain.
236 ch
= &sband
->channels
[11]; /* CH 12 */
237 reg_rule
= freq_reg_info(wiphy
, ch
->center_freq
);
238 if (!IS_ERR(reg_rule
)) {
239 if (!(reg_rule
->flags
& NL80211_RRF_PASSIVE_SCAN
))
240 if (ch
->flags
& IEEE80211_CHAN_PASSIVE_SCAN
)
241 ch
->flags
&= ~IEEE80211_CHAN_PASSIVE_SCAN
;
244 ch
= &sband
->channels
[12]; /* CH 13 */
245 reg_rule
= freq_reg_info(wiphy
, ch
->center_freq
);
246 if (!IS_ERR(reg_rule
)) {
247 if (!(reg_rule
->flags
& NL80211_RRF_PASSIVE_SCAN
))
248 if (ch
->flags
& IEEE80211_CHAN_PASSIVE_SCAN
)
249 ch
->flags
&= ~IEEE80211_CHAN_PASSIVE_SCAN
;
254 *Always apply Radar/DFS rules on
255 *freq range 5260 MHz - 5700 MHz
257 static void _rtl_reg_apply_radar_flags(struct wiphy
*wiphy
)
259 struct ieee80211_supported_band
*sband
;
260 struct ieee80211_channel
*ch
;
263 if (!wiphy
->bands
[IEEE80211_BAND_5GHZ
])
266 sband
= wiphy
->bands
[IEEE80211_BAND_5GHZ
];
268 for (i
= 0; i
< sband
->n_channels
; i
++) {
269 ch
= &sband
->channels
[i
];
270 if (!_rtl_is_radar_freq(ch
->center_freq
))
274 *We always enable radar detection/DFS on this
275 *frequency range. Additionally we also apply on
276 *this frequency range:
277 *- If STA mode does not yet have DFS supports disable
279 *- If adhoc mode does not support DFS yet then disable
280 * adhoc in the frequency.
281 *- If AP mode does not yet support radar detection/DFS
282 *do not allow AP mode
284 if (!(ch
->flags
& IEEE80211_CHAN_DISABLED
))
285 ch
->flags
|= IEEE80211_CHAN_RADAR
|
286 IEEE80211_CHAN_NO_IBSS
|
287 IEEE80211_CHAN_PASSIVE_SCAN
;
291 static void _rtl_reg_apply_world_flags(struct wiphy
*wiphy
,
292 enum nl80211_reg_initiator initiator
,
293 struct rtl_regulatory
*reg
)
295 _rtl_reg_apply_beaconing_flags(wiphy
, initiator
);
296 _rtl_reg_apply_active_scan_flags(wiphy
, initiator
);
300 static void _rtl_dump_channel_map(struct wiphy
*wiphy
)
302 enum ieee80211_band band
;
303 struct ieee80211_supported_band
*sband
;
304 struct ieee80211_channel
*ch
;
307 for (band
= 0; band
< IEEE80211_NUM_BANDS
; band
++) {
308 if (!wiphy
->bands
[band
])
310 sband
= wiphy
->bands
[band
];
311 for (i
= 0; i
< sband
->n_channels
; i
++)
312 ch
= &sband
->channels
[i
];
316 static int _rtl_reg_notifier_apply(struct wiphy
*wiphy
,
317 struct regulatory_request
*request
,
318 struct rtl_regulatory
*reg
)
320 /* We always apply this */
321 _rtl_reg_apply_radar_flags(wiphy
);
323 switch (request
->initiator
) {
324 case NL80211_REGDOM_SET_BY_DRIVER
:
325 case NL80211_REGDOM_SET_BY_CORE
:
326 case NL80211_REGDOM_SET_BY_USER
:
328 case NL80211_REGDOM_SET_BY_COUNTRY_IE
:
329 _rtl_reg_apply_world_flags(wiphy
, request
->initiator
, reg
);
333 _rtl_dump_channel_map(wiphy
);
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_ETSI
:
347 case COUNTRY_CODE_TELEC_NETGEAR
:
348 return &rtl_regdom_60_64
;
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
;
362 return &rtl_regdom_no_midband
;
366 static int _rtl_regd_init_wiphy(struct rtl_regulatory
*reg
,
368 void (*reg_notifier
) (struct wiphy
* wiphy
,
369 struct regulatory_request
*
372 const struct ieee80211_regdomain
*regd
;
374 wiphy
->reg_notifier
= reg_notifier
;
376 wiphy
->flags
|= WIPHY_FLAG_CUSTOM_REGULATORY
;
377 wiphy
->flags
&= ~WIPHY_FLAG_STRICT_REGULATORY
;
378 wiphy
->flags
&= ~WIPHY_FLAG_DISABLE_BEACON_HINTS
;
380 regd
= _rtl_regdomain_select(reg
);
381 wiphy_apply_custom_regulatory(wiphy
, regd
);
382 _rtl_reg_apply_radar_flags(wiphy
);
383 _rtl_reg_apply_world_flags(wiphy
, NL80211_REGDOM_SET_BY_DRIVER
, reg
);
387 static struct country_code_to_enum_rd
*_rtl_regd_find_country(u16 countrycode
)
391 for (i
= 0; i
< ARRAY_SIZE(allCountries
); i
++) {
392 if (allCountries
[i
].countrycode
== countrycode
)
393 return &allCountries
[i
];
398 int rtl_regd_init(struct ieee80211_hw
*hw
,
399 void (*reg_notifier
) (struct wiphy
*wiphy
,
400 struct regulatory_request
*request
))
402 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
403 struct wiphy
*wiphy
= hw
->wiphy
;
404 struct country_code_to_enum_rd
*country
= NULL
;
406 if (wiphy
== NULL
|| &rtlpriv
->regd
== NULL
)
409 /* init country_code from efuse channel plan */
410 rtlpriv
->regd
.country_code
= rtlpriv
->efuse
.channel_plan
;
412 RT_TRACE(COMP_REGD
, DBG_TRACE
,
413 (KERN_DEBUG
"rtl: EEPROM regdomain: 0x%0x\n",
414 rtlpriv
->regd
.country_code
));
416 if (rtlpriv
->regd
.country_code
>= COUNTRY_CODE_MAX
) {
417 RT_TRACE(COMP_REGD
, DBG_DMESG
,
418 (KERN_DEBUG
"rtl: EEPROM indicates invalid country code"
419 "world wide 13 should be used\n"));
421 rtlpriv
->regd
.country_code
= COUNTRY_CODE_WORLD_WIDE_13
;
424 country
= _rtl_regd_find_country(rtlpriv
->regd
.country_code
);
427 rtlpriv
->regd
.alpha2
[0] = country
->iso_name
[0];
428 rtlpriv
->regd
.alpha2
[1] = country
->iso_name
[1];
430 rtlpriv
->regd
.alpha2
[0] = '0';
431 rtlpriv
->regd
.alpha2
[1] = '0';
434 RT_TRACE(COMP_REGD
, DBG_TRACE
,
435 (KERN_DEBUG
"rtl: Country alpha2 being used: %c%c\n",
436 rtlpriv
->regd
.alpha2
[0], rtlpriv
->regd
.alpha2
[1]));
438 _rtl_regd_init_wiphy(&rtlpriv
->regd
, wiphy
, reg_notifier
);
443 void rtl_reg_notifier(struct wiphy
*wiphy
, struct regulatory_request
*request
)
445 struct ieee80211_hw
*hw
= wiphy_to_ieee80211_hw(wiphy
);
446 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
448 RT_TRACE(COMP_REGD
, DBG_LOUD
, ("\n"));
450 _rtl_reg_notifier_apply(wiphy
, request
, &rtlpriv
->regd
);