Merge tag 'backport/v3.14.24-ltsi-rc1/irq-renesas-intc-irqpin-to-v3.18-rc1' into...
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / staging / rtl8821ae / regd.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2009-2010  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 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3,9,0))
162         u32 bandwidth = 0;
163         int r;
164 #endif
165
166         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
167
168                 if (!wiphy->bands[band])
169                         continue;
170
171                 sband = wiphy->bands[band];
172
173                 for (i = 0; i < sband->n_channels; i++) {
174                         ch = &sband->channels[i];
175                         if (_rtl_is_radar_freq(ch->center_freq) ||
176                             (ch->flags & IEEE80211_CHAN_RADAR))
177                                 continue;
178                         if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
179 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,9,0))
180                                 reg_rule = freq_reg_info(wiphy, ch->center_freq);
181                                 if (IS_ERR(reg_rule))
182                                         continue;
183 #else
184                                 r = freq_reg_info(wiphy, ch->center_freq,
185                                                   bandwidth, &reg_rule);
186                                 if (r)
187                                         continue;
188 #endif
189
190                                 /*
191                                  *If 11d had a rule for this channel ensure
192                                  *we enable adhoc/beaconing if it allows us to
193                                  *use it. Note that we would have disabled it
194                                  *by applying our static world regdomain by
195                                  *default during init, prior to calling our
196                                  *regulatory_hint().
197                                  */
198
199                                 if (!(reg_rule->flags & NL80211_RRF_NO_IBSS))
200                                         ch->flags &= ~IEEE80211_CHAN_NO_IBSS;
201                                 if (!(reg_rule->flags &
202                                       NL80211_RRF_PASSIVE_SCAN))
203                                         ch->flags &=
204                                             ~IEEE80211_CHAN_PASSIVE_SCAN;
205                         } else {
206                                 if (ch->beacon_found)
207                                         ch->flags &= ~(IEEE80211_CHAN_NO_IBSS |
208                                                    IEEE80211_CHAN_PASSIVE_SCAN);
209                         }
210                 }
211         }
212 }
213
214 /* Allows active scan scan on Ch 12 and 13 */
215 static void _rtl_reg_apply_active_scan_flags(struct wiphy *wiphy,
216                                              enum nl80211_reg_initiator
217                                              initiator)
218 {
219         struct ieee80211_supported_band *sband;
220         struct ieee80211_channel *ch;
221         const struct ieee80211_reg_rule *reg_rule;
222 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3,9,0))
223         u32 bandwidth = 0;
224         int r;
225 #endif
226
227         if (!wiphy->bands[IEEE80211_BAND_2GHZ])
228                 return;
229         sband = wiphy->bands[IEEE80211_BAND_2GHZ];
230
231         /*
232          *If no country IE has been received always enable active scan
233          *on these channels. This is only done for specific regulatory SKUs
234          */
235         if (initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
236                 ch = &sband->channels[11];      /* CH 12 */
237                 if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
238                         ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
239                 ch = &sband->channels[12];      /* CH 13 */
240                 if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
241                         ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
242                 return;
243         }
244
245         /*
246          *If a country IE has been recieved check its rule for this
247          *channel first before enabling active scan. The passive scan
248          *would have been enforced by the initial processing of our
249          *custom regulatory domain.
250          */
251
252         ch = &sband->channels[11];      /* CH 12 */
253 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,9,0))
254         reg_rule = freq_reg_info(wiphy, ch->center_freq);
255         if (!IS_ERR(reg_rule)) {
256 #else
257         r = freq_reg_info(wiphy, ch->center_freq, bandwidth, &reg_rule);
258         if (!r) {
259 #endif
260                 if (!(reg_rule->flags & NL80211_RRF_PASSIVE_SCAN))
261                         if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
262                                 ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
263         }
264
265         ch = &sband->channels[12];      /* CH 13 */
266 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,9,0))
267         reg_rule = freq_reg_info(wiphy, ch->center_freq);
268         if (!IS_ERR(reg_rule)) {
269 #else
270         r = freq_reg_info(wiphy, ch->center_freq, bandwidth, &reg_rule);
271         if (!r) {
272 #endif
273                 if (!(reg_rule->flags & NL80211_RRF_PASSIVE_SCAN))
274                         if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
275                                 ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
276         }
277 }
278
279 /*
280  *Always apply Radar/DFS rules on
281  *freq range 5260 MHz - 5700 MHz
282  */
283 static void _rtl_reg_apply_radar_flags(struct wiphy *wiphy)
284 {
285         struct ieee80211_supported_band *sband;
286         struct ieee80211_channel *ch;
287         unsigned int i;
288
289         if (!wiphy->bands[IEEE80211_BAND_5GHZ])
290                 return;
291
292         sband = wiphy->bands[IEEE80211_BAND_5GHZ];
293
294         for (i = 0; i < sband->n_channels; i++) {
295                 ch = &sband->channels[i];
296                 if (!_rtl_is_radar_freq(ch->center_freq))
297                         continue;
298
299                 /*
300                  *We always enable radar detection/DFS on this
301                  *frequency range. Additionally we also apply on
302                  *this frequency range:
303                  *- If STA mode does not yet have DFS supports disable
304                  * active scanning
305                  *- If adhoc mode does not support DFS yet then disable
306                  * adhoc in the frequency.
307                  *- If AP mode does not yet support radar detection/DFS
308                  *do not allow AP mode
309                  */
310                 if (!(ch->flags & IEEE80211_CHAN_DISABLED))
311                         ch->flags |= IEEE80211_CHAN_RADAR |
312                             IEEE80211_CHAN_NO_IBSS |
313                             IEEE80211_CHAN_PASSIVE_SCAN;
314         }
315 }
316
317 static void _rtl_reg_apply_world_flags(struct wiphy *wiphy,
318                                        enum nl80211_reg_initiator initiator,
319                                        struct rtl_regulatory *reg)
320 {
321         _rtl_reg_apply_beaconing_flags(wiphy, initiator);
322         _rtl_reg_apply_active_scan_flags(wiphy, initiator);
323         return;
324 }
325
326 static void _rtl_dump_channel_map(struct wiphy *wiphy)
327 {
328         enum ieee80211_band band;
329         struct ieee80211_supported_band *sband;
330         struct ieee80211_channel *ch;
331         unsigned int i;
332
333         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
334                 if (!wiphy->bands[band])
335                         continue;
336                 sband = wiphy->bands[band];
337                 for (i = 0; i < sband->n_channels; i++)
338                         ch = &sband->channels[i];
339         }
340 }
341
342 static int _rtl_reg_notifier_apply(struct wiphy *wiphy,
343                                    struct regulatory_request *request,
344                                    struct rtl_regulatory *reg)
345 {
346         /* We always apply this */
347         _rtl_reg_apply_radar_flags(wiphy);
348
349         switch (request->initiator) {
350         case NL80211_REGDOM_SET_BY_DRIVER:
351         case NL80211_REGDOM_SET_BY_CORE:
352         case NL80211_REGDOM_SET_BY_USER:
353                 break;
354         case NL80211_REGDOM_SET_BY_COUNTRY_IE:
355                 _rtl_reg_apply_world_flags(wiphy, request->initiator, reg);
356                 break;
357         }
358
359         _rtl_dump_channel_map(wiphy);
360
361         return 0;
362 }
363
364 static const struct ieee80211_regdomain *_rtl_regdomain_select(
365                                                 struct rtl_regulatory *reg)
366 {
367         switch (reg->country_code) {
368         case COUNTRY_CODE_FCC:
369                 return &rtl_regdom_no_midband;
370         case COUNTRY_CODE_IC:
371                 return &rtl_regdom_11;
372         case COUNTRY_CODE_ETSI:
373         case COUNTRY_CODE_TELEC_NETGEAR:
374                 return &rtl_regdom_60_64;
375         case COUNTRY_CODE_SPAIN:
376         case COUNTRY_CODE_FRANCE:
377         case COUNTRY_CODE_ISRAEL:
378         case COUNTRY_CODE_WORLD_WIDE_13:
379                 return &rtl_regdom_12_13;
380         case COUNTRY_CODE_MKK:
381         case COUNTRY_CODE_MKK1:
382         case COUNTRY_CODE_TELEC:
383         case COUNTRY_CODE_MIC:
384                 return &rtl_regdom_14_60_64;
385         case COUNTRY_CODE_GLOBAL_DOMAIN:
386                 return &rtl_regdom_14;
387         default:
388                 return &rtl_regdom_no_midband;
389         }
390 }
391
392 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,9,0))
393 static int _rtl_regd_init_wiphy(struct rtl_regulatory *reg,
394                                 struct wiphy *wiphy,
395                                 void (*reg_notifier) (struct wiphy * wiphy,
396                                                      struct regulatory_request *
397                                                      request))
398 #else
399 static int _rtl_regd_init_wiphy(struct rtl_regulatory *reg,
400                                 struct wiphy *wiphy,
401                                 int (*reg_notifier) (struct wiphy * wiphy,
402                                                      struct regulatory_request *
403                                                      request))
404 #endif
405 {
406         const struct ieee80211_regdomain *regd;
407
408         wiphy->reg_notifier = reg_notifier;
409
410         wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
411         wiphy->flags &= ~WIPHY_FLAG_STRICT_REGULATORY;
412         wiphy->flags &= ~WIPHY_FLAG_DISABLE_BEACON_HINTS;
413
414         regd = _rtl_regdomain_select(reg);
415         wiphy_apply_custom_regulatory(wiphy, regd);
416         _rtl_reg_apply_radar_flags(wiphy);
417         _rtl_reg_apply_world_flags(wiphy, NL80211_REGDOM_SET_BY_DRIVER, reg);
418         return 0;
419 }
420
421 static struct country_code_to_enum_rd *_rtl_regd_find_country(u16 countrycode)
422 {
423         int i;
424
425         for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
426                 if (allCountries[i].countrycode == countrycode)
427                         return &allCountries[i];
428         }
429         return NULL;
430 }
431
432 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,9,0))
433 int rtl_regd_init(struct ieee80211_hw *hw,
434                   void (*reg_notifier) (struct wiphy *wiphy,
435                                         struct regulatory_request *request))
436 #else
437 int rtl_regd_init(struct ieee80211_hw *hw,
438                   int (*reg_notifier) (struct wiphy *wiphy,
439                                        struct regulatory_request *request))
440 #endif
441 {
442         struct rtl_priv *rtlpriv = rtl_priv(hw);
443         struct wiphy *wiphy = hw->wiphy;
444         struct country_code_to_enum_rd *country = NULL;
445
446         if (wiphy == NULL || &rtlpriv->regd == NULL)
447                 return -EINVAL;
448
449         /* init country_code from efuse channel plan */
450         rtlpriv->regd.country_code = rtlpriv->efuse.channel_plan;
451
452         RT_TRACE(COMP_REGD, DBG_TRACE,
453                  (KERN_DEBUG "rtl: EEPROM regdomain: 0x%0x\n",
454                   rtlpriv->regd.country_code));
455
456         if (rtlpriv->regd.country_code >= COUNTRY_CODE_MAX) {
457                 RT_TRACE(COMP_REGD, DBG_DMESG,
458                          (KERN_DEBUG "rtl: EEPROM indicates invalid contry code"
459                           "world wide 13 should be used\n"));
460
461                 rtlpriv->regd.country_code = COUNTRY_CODE_WORLD_WIDE_13;
462         }
463
464         country = _rtl_regd_find_country(rtlpriv->regd.country_code);
465
466         if (country) {
467                 rtlpriv->regd.alpha2[0] = country->iso_name[0];
468                 rtlpriv->regd.alpha2[1] = country->iso_name[1];
469         } else {
470                 rtlpriv->regd.alpha2[0] = '0';
471                 rtlpriv->regd.alpha2[1] = '0';
472         }
473
474         RT_TRACE(COMP_REGD, DBG_TRACE,
475                  (KERN_DEBUG "rtl: Country alpha2 being used: %c%c\n",
476                   rtlpriv->regd.alpha2[0], rtlpriv->regd.alpha2[1]));
477
478         _rtl_regd_init_wiphy(&rtlpriv->regd, wiphy, reg_notifier);
479
480         return 0;
481 }
482
483 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,9,0))
484 void rtl_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
485 {
486         struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
487         struct rtl_priv *rtlpriv = rtl_priv(hw);
488
489         RT_TRACE(COMP_REGD, DBG_LOUD, ("\n"));
490
491         _rtl_reg_notifier_apply(wiphy, request, &rtlpriv->regd);
492 }
493 #else
494 int rtl_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
495 {
496         struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
497         struct rtl_priv *rtlpriv = rtl_priv(hw);
498
499         RT_TRACE(COMP_REGD, DBG_LOUD, ("\n"));
500
501         return _rtl_reg_notifier_apply(wiphy, request, &rtlpriv->regd);
502 }
503 #endif