Merge tag 'nfs-for-3.11-2' of git://git.linux-nfs.org/projects/trondmy/linux-nfs
[cascardo/linux.git] / drivers / net / wireless / rtlwifi / regd.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2009-2012  Realtek Corporation.
4  *
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
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 };
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,\
62         NL80211_RRF_PASSIVE_SCAN)
63
64 #define RTL819x_2GHZ_CH14       \
65         REG_RULE(2484-10, 2484+10, 40, 0, 20, \
66         NL80211_RRF_PASSIVE_SCAN | \
67         NL80211_RRF_NO_OFDM)
68
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 | \
73         NL80211_RRF_NO_IBSS)
74
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 | \
79         NL80211_RRF_NO_IBSS)
80
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 | \
85         NL80211_RRF_NO_IBSS)
86
87 #define RTL819x_5GHZ_ALL        \
88         (RTL819x_5GHZ_5150_5350, RTL819x_5GHZ_5470_5850)
89
90 static const struct ieee80211_regdomain rtl_regdom_11 = {
91         .n_reg_rules = 1,
92         .alpha2 = "99",
93         .reg_rules = {
94                       RTL819x_2GHZ_CH01_11,
95                       }
96 };
97
98 static const struct ieee80211_regdomain rtl_regdom_12_13 = {
99         .n_reg_rules = 2,
100         .alpha2 = "99",
101         .reg_rules = {
102                       RTL819x_2GHZ_CH01_11,
103                           RTL819x_2GHZ_CH12_13,
104                       }
105 };
106
107 static const struct ieee80211_regdomain rtl_regdom_no_midband = {
108         .n_reg_rules = 3,
109         .alpha2 = "99",
110         .reg_rules = {
111                       RTL819x_2GHZ_CH01_11,
112                           RTL819x_5GHZ_5150_5350,
113                           RTL819x_5GHZ_5725_5850,
114                       }
115 };
116
117 static const struct ieee80211_regdomain rtl_regdom_60_64 = {
118         .n_reg_rules = 3,
119         .alpha2 = "99",
120         .reg_rules = {
121                       RTL819x_2GHZ_CH01_11,
122                           RTL819x_2GHZ_CH12_13,
123                           RTL819x_5GHZ_5725_5850,
124                       }
125 };
126
127 static const struct ieee80211_regdomain rtl_regdom_14_60_64 = {
128         .n_reg_rules = 4,
129         .alpha2 = "99",
130         .reg_rules = {
131                       RTL819x_2GHZ_CH01_11,
132                           RTL819x_2GHZ_CH12_13,
133                           RTL819x_2GHZ_CH14,
134                           RTL819x_5GHZ_5725_5850,
135                       }
136 };
137
138 static const struct ieee80211_regdomain rtl_regdom_14 = {
139         .n_reg_rules = 3,
140         .alpha2 = "99",
141         .reg_rules = {
142                       RTL819x_2GHZ_CH01_11,
143                           RTL819x_2GHZ_CH12_13,
144                           RTL819x_2GHZ_CH14,
145                       }
146 };
147
148 static bool _rtl_is_radar_freq(u16 center_freq)
149 {
150         return (center_freq >= 5260 && center_freq <= 5700);
151 }
152
153 static void _rtl_reg_apply_beaconing_flags(struct wiphy *wiphy,
154                                            enum nl80211_reg_initiator initiator)
155 {
156         enum ieee80211_band band;
157         struct ieee80211_supported_band *sband;
158         const struct ieee80211_reg_rule *reg_rule;
159         struct ieee80211_channel *ch;
160         unsigned int i;
161
162         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
163
164                 if (!wiphy->bands[band])
165                         continue;
166
167                 sband = wiphy->bands[band];
168
169                 for (i = 0; i < sband->n_channels; i++) {
170                         ch = &sband->channels[i];
171                         if (_rtl_is_radar_freq(ch->center_freq) ||
172                             (ch->flags & IEEE80211_CHAN_RADAR))
173                                 continue;
174                         if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
175                                 reg_rule = freq_reg_info(wiphy, ch->center_freq);
176                                 if (IS_ERR(reg_rule))
177                                         continue;
178
179                                 /*
180                                  *If 11d had a rule for this channel ensure
181                                  *we enable adhoc/beaconing if it allows us to
182                                  *use it. Note that we would have disabled it
183                                  *by applying our static world regdomain by
184                                  *default during init, prior to calling our
185                                  *regulatory_hint().
186                                  */
187
188                                 if (!(reg_rule->flags & NL80211_RRF_NO_IBSS))
189                                         ch->flags &= ~IEEE80211_CHAN_NO_IBSS;
190                                 if (!(reg_rule->
191                                      flags & NL80211_RRF_PASSIVE_SCAN))
192                                         ch->flags &=
193                                             ~IEEE80211_CHAN_PASSIVE_SCAN;
194                         } else {
195                                 if (ch->beacon_found)
196                                         ch->flags &= ~(IEEE80211_CHAN_NO_IBSS |
197                                                   IEEE80211_CHAN_PASSIVE_SCAN);
198                         }
199                 }
200         }
201 }
202
203 /* Allows active scan scan on Ch 12 and 13 */
204 static void _rtl_reg_apply_active_scan_flags(struct wiphy *wiphy,
205                                              enum nl80211_reg_initiator
206                                              initiator)
207 {
208         struct ieee80211_supported_band *sband;
209         struct ieee80211_channel *ch;
210         const struct ieee80211_reg_rule *reg_rule;
211
212         if (!wiphy->bands[IEEE80211_BAND_2GHZ])
213                 return;
214         sband = wiphy->bands[IEEE80211_BAND_2GHZ];
215
216         /*
217          *If no country IE has been received always enable active scan
218          *on these channels. This is only done for specific regulatory SKUs
219          */
220         if (initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
221                 ch = &sband->channels[11];      /* CH 12 */
222                 if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
223                         ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
224                 ch = &sband->channels[12];      /* CH 13 */
225                 if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
226                         ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
227                 return;
228         }
229
230         /*
231          *If a country IE has been received check its rule for this
232          *channel first before enabling active scan. The passive scan
233          *would have been enforced by the initial processing of our
234          *custom regulatory domain.
235          */
236
237         ch = &sband->channels[11];      /* CH 12 */
238         reg_rule = freq_reg_info(wiphy, ch->center_freq);
239         if (!IS_ERR(reg_rule)) {
240                 if (!(reg_rule->flags & NL80211_RRF_PASSIVE_SCAN))
241                         if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
242                                 ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
243         }
244
245         ch = &sband->channels[12];      /* CH 13 */
246         reg_rule = freq_reg_info(wiphy, ch->center_freq);
247         if (!IS_ERR(reg_rule)) {
248                 if (!(reg_rule->flags & NL80211_RRF_PASSIVE_SCAN))
249                         if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
250                                 ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
251         }
252 }
253
254 /*
255  *Always apply Radar/DFS rules on
256  *freq range 5260 MHz - 5700 MHz
257  */
258 static void _rtl_reg_apply_radar_flags(struct wiphy *wiphy)
259 {
260         struct ieee80211_supported_band *sband;
261         struct ieee80211_channel *ch;
262         unsigned int i;
263
264         if (!wiphy->bands[IEEE80211_BAND_5GHZ])
265                 return;
266
267         sband = wiphy->bands[IEEE80211_BAND_5GHZ];
268
269         for (i = 0; i < sband->n_channels; i++) {
270                 ch = &sband->channels[i];
271                 if (!_rtl_is_radar_freq(ch->center_freq))
272                         continue;
273
274                 /*
275                  *We always enable radar detection/DFS on this
276                  *frequency range. Additionally we also apply on
277                  *this frequency range:
278                  *- If STA mode does not yet have DFS supports disable
279                  * active scanning
280                  *- If adhoc mode does not support DFS yet then disable
281                  * adhoc in the frequency.
282                  *- If AP mode does not yet support radar detection/DFS
283                  *do not allow AP mode
284                  */
285                 if (!(ch->flags & IEEE80211_CHAN_DISABLED))
286                         ch->flags |= IEEE80211_CHAN_RADAR |
287                             IEEE80211_CHAN_NO_IBSS |
288                             IEEE80211_CHAN_PASSIVE_SCAN;
289         }
290 }
291
292 static void _rtl_reg_apply_world_flags(struct wiphy *wiphy,
293                                        enum nl80211_reg_initiator initiator,
294                                        struct rtl_regulatory *reg)
295 {
296         _rtl_reg_apply_beaconing_flags(wiphy, initiator);
297         _rtl_reg_apply_active_scan_flags(wiphy, initiator);
298         return;
299 }
300
301 static void _rtl_reg_notifier_apply(struct wiphy *wiphy,
302                                     struct regulatory_request *request,
303                                     struct rtl_regulatory *reg)
304 {
305         /* We always apply this */
306         _rtl_reg_apply_radar_flags(wiphy);
307
308         switch (request->initiator) {
309         case NL80211_REGDOM_SET_BY_DRIVER:
310         case NL80211_REGDOM_SET_BY_CORE:
311         case NL80211_REGDOM_SET_BY_USER:
312                 break;
313         case NL80211_REGDOM_SET_BY_COUNTRY_IE:
314                 _rtl_reg_apply_world_flags(wiphy, request->initiator, reg);
315                 break;
316         }
317 }
318
319 static const struct ieee80211_regdomain *_rtl_regdomain_select(
320                                                struct rtl_regulatory *reg)
321 {
322         switch (reg->country_code) {
323         case COUNTRY_CODE_FCC:
324                 return &rtl_regdom_no_midband;
325         case COUNTRY_CODE_IC:
326                 return &rtl_regdom_11;
327         case COUNTRY_CODE_ETSI:
328         case COUNTRY_CODE_TELEC_NETGEAR:
329                 return &rtl_regdom_60_64;
330         case COUNTRY_CODE_SPAIN:
331         case COUNTRY_CODE_FRANCE:
332         case COUNTRY_CODE_ISRAEL:
333         case COUNTRY_CODE_WORLD_WIDE_13:
334                 return &rtl_regdom_12_13;
335         case COUNTRY_CODE_MKK:
336         case COUNTRY_CODE_MKK1:
337         case COUNTRY_CODE_TELEC:
338         case COUNTRY_CODE_MIC:
339                 return &rtl_regdom_14_60_64;
340         case COUNTRY_CODE_GLOBAL_DOMAIN:
341                 return &rtl_regdom_14;
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->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
358         wiphy->flags &= ~WIPHY_FLAG_STRICT_REGULATORY;
359         wiphy->flags &= ~WIPHY_FLAG_DISABLE_BEACON_HINTS;
360
361         regd = _rtl_regdomain_select(reg);
362         wiphy_apply_custom_regulatory(wiphy, regd);
363         _rtl_reg_apply_radar_flags(wiphy);
364         _rtl_reg_apply_world_flags(wiphy, NL80211_REGDOM_SET_BY_DRIVER, reg);
365         return 0;
366 }
367
368 static struct country_code_to_enum_rd *_rtl_regd_find_country(u16 countrycode)
369 {
370         int i;
371
372         for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
373                 if (allCountries[i].countrycode == countrycode)
374                         return &allCountries[i];
375         }
376         return NULL;
377 }
378
379 int rtl_regd_init(struct ieee80211_hw *hw,
380                   void (*reg_notifier) (struct wiphy *wiphy,
381                                        struct regulatory_request *request))
382 {
383         struct rtl_priv *rtlpriv = rtl_priv(hw);
384         struct wiphy *wiphy = hw->wiphy;
385         struct country_code_to_enum_rd *country = NULL;
386
387         if (wiphy == NULL || &rtlpriv->regd == NULL)
388                 return -EINVAL;
389
390         /* init country_code from efuse channel plan */
391         rtlpriv->regd.country_code = rtlpriv->efuse.channel_plan;
392
393         RT_TRACE(rtlpriv, COMP_REGD, DBG_TRACE,
394                  "rtl: EEPROM regdomain: 0x%0x\n", rtlpriv->regd.country_code);
395
396         if (rtlpriv->regd.country_code >= COUNTRY_CODE_MAX) {
397                 RT_TRACE(rtlpriv, COMP_REGD, DBG_DMESG,
398                          "rtl: EEPROM indicates invalid contry code, world wide 13 should be used\n");
399
400                 rtlpriv->regd.country_code = COUNTRY_CODE_WORLD_WIDE_13;
401         }
402
403         country = _rtl_regd_find_country(rtlpriv->regd.country_code);
404
405         if (country) {
406                 rtlpriv->regd.alpha2[0] = country->iso_name[0];
407                 rtlpriv->regd.alpha2[1] = country->iso_name[1];
408         } else {
409                 rtlpriv->regd.alpha2[0] = '0';
410                 rtlpriv->regd.alpha2[1] = '0';
411         }
412
413         RT_TRACE(rtlpriv, COMP_REGD, DBG_TRACE,
414                  "rtl: Country alpha2 being used: %c%c\n",
415                  rtlpriv->regd.alpha2[0], rtlpriv->regd.alpha2[1]);
416
417         _rtl_regd_init_wiphy(&rtlpriv->regd, wiphy, reg_notifier);
418
419         return 0;
420 }
421
422 void rtl_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
423 {
424         struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
425         struct rtl_priv *rtlpriv = rtl_priv(hw);
426
427         RT_TRACE(rtlpriv, COMP_REGD, DBG_LOUD, "\n");
428
429         _rtl_reg_notifier_apply(wiphy, request, &rtlpriv->regd);
430 }