regd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. #include "wifi.h"
  30. #include "regd.h"
  31. static struct country_code_to_enum_rd allCountries[] = {
  32. {COUNTRY_CODE_FCC, "US"},
  33. {COUNTRY_CODE_IC, "US"},
  34. {COUNTRY_CODE_ETSI, "EC"},
  35. {COUNTRY_CODE_SPAIN, "EC"},
  36. {COUNTRY_CODE_FRANCE, "EC"},
  37. {COUNTRY_CODE_MKK, "JP"},
  38. {COUNTRY_CODE_MKK1, "JP"},
  39. {COUNTRY_CODE_ISRAEL, "EC"},
  40. {COUNTRY_CODE_TELEC, "JP"},
  41. {COUNTRY_CODE_MIC, "JP"},
  42. {COUNTRY_CODE_GLOBAL_DOMAIN, "JP"},
  43. {COUNTRY_CODE_WORLD_WIDE_13, "EC"},
  44. {COUNTRY_CODE_TELEC_NETGEAR, "EC"},
  45. };
  46. /*
  47. *Only these channels all allow active
  48. *scan on all world regulatory domains
  49. */
  50. #define RTL819x_2GHZ_CH01_11 \
  51. REG_RULE(2412-10, 2462+10, 40, 0, 20, 0)
  52. /*
  53. *We enable active scan on these a case
  54. *by case basis by regulatory domain
  55. */
  56. #define RTL819x_2GHZ_CH12_13 \
  57. REG_RULE(2467-10, 2472+10, 40, 0, 20,\
  58. NL80211_RRF_NO_IR)
  59. #define RTL819x_2GHZ_CH14 \
  60. REG_RULE(2484-10, 2484+10, 40, 0, 20, \
  61. NL80211_RRF_NO_IR | NL80211_RRF_NO_OFDM)
  62. /* 5G chan 36 - chan 64*/
  63. #define RTL819x_5GHZ_5150_5350 \
  64. REG_RULE(5150-10, 5350+10, 40, 0, 30, \
  65. NL80211_RRF_NO_IR)
  66. /* 5G chan 100 - chan 165*/
  67. #define RTL819x_5GHZ_5470_5850 \
  68. REG_RULE(5470-10, 5850+10, 40, 0, 30, \
  69. NL80211_RRF_NO_IR)
  70. /* 5G chan 149 - chan 165*/
  71. #define RTL819x_5GHZ_5725_5850 \
  72. REG_RULE(5725-10, 5850+10, 40, 0, 30, \
  73. NL80211_RRF_NO_IR)
  74. #define RTL819x_5GHZ_ALL \
  75. (RTL819x_5GHZ_5150_5350, RTL819x_5GHZ_5470_5850)
  76. static const struct ieee80211_regdomain rtl_regdom_11 = {
  77. .n_reg_rules = 1,
  78. .alpha2 = "99",
  79. .reg_rules = {
  80. RTL819x_2GHZ_CH01_11,
  81. }
  82. };
  83. static const struct ieee80211_regdomain rtl_regdom_12_13 = {
  84. .n_reg_rules = 2,
  85. .alpha2 = "99",
  86. .reg_rules = {
  87. RTL819x_2GHZ_CH01_11,
  88. RTL819x_2GHZ_CH12_13,
  89. }
  90. };
  91. static const struct ieee80211_regdomain rtl_regdom_no_midband = {
  92. .n_reg_rules = 3,
  93. .alpha2 = "99",
  94. .reg_rules = {
  95. RTL819x_2GHZ_CH01_11,
  96. RTL819x_5GHZ_5150_5350,
  97. RTL819x_5GHZ_5725_5850,
  98. }
  99. };
  100. static const struct ieee80211_regdomain rtl_regdom_60_64 = {
  101. .n_reg_rules = 3,
  102. .alpha2 = "99",
  103. .reg_rules = {
  104. RTL819x_2GHZ_CH01_11,
  105. RTL819x_2GHZ_CH12_13,
  106. RTL819x_5GHZ_5725_5850,
  107. }
  108. };
  109. static const struct ieee80211_regdomain rtl_regdom_14_60_64 = {
  110. .n_reg_rules = 4,
  111. .alpha2 = "99",
  112. .reg_rules = {
  113. RTL819x_2GHZ_CH01_11,
  114. RTL819x_2GHZ_CH12_13,
  115. RTL819x_2GHZ_CH14,
  116. RTL819x_5GHZ_5725_5850,
  117. }
  118. };
  119. static const struct ieee80211_regdomain rtl_regdom_14 = {
  120. .n_reg_rules = 3,
  121. .alpha2 = "99",
  122. .reg_rules = {
  123. RTL819x_2GHZ_CH01_11,
  124. RTL819x_2GHZ_CH12_13,
  125. RTL819x_2GHZ_CH14,
  126. }
  127. };
  128. static bool _rtl_is_radar_freq(u16 center_freq)
  129. {
  130. return (center_freq >= 5260 && center_freq <= 5700);
  131. }
  132. static void _rtl_reg_apply_beaconing_flags(struct wiphy *wiphy,
  133. enum nl80211_reg_initiator initiator)
  134. {
  135. enum ieee80211_band band;
  136. struct ieee80211_supported_band *sband;
  137. const struct ieee80211_reg_rule *reg_rule;
  138. struct ieee80211_channel *ch;
  139. unsigned int i;
  140. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  141. if (!wiphy->bands[band])
  142. continue;
  143. sband = wiphy->bands[band];
  144. for (i = 0; i < sband->n_channels; i++) {
  145. ch = &sband->channels[i];
  146. if (_rtl_is_radar_freq(ch->center_freq) ||
  147. (ch->flags & IEEE80211_CHAN_RADAR))
  148. continue;
  149. if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  150. reg_rule = freq_reg_info(wiphy,
  151. MHZ_TO_KHZ(ch->center_freq));
  152. if (IS_ERR(reg_rule))
  153. continue;
  154. /*
  155. *If 11d had a rule for this channel ensure
  156. *we enable adhoc/beaconing if it allows us to
  157. *use it. Note that we would have disabled it
  158. *by applying our static world regdomain by
  159. *default during init, prior to calling our
  160. *regulatory_hint().
  161. */
  162. if (!(reg_rule->flags & NL80211_RRF_NO_IR))
  163. ch->flags &= ~IEEE80211_CHAN_NO_IR;
  164. } else {
  165. if (ch->beacon_found)
  166. ch->flags &= ~IEEE80211_CHAN_NO_IR;
  167. }
  168. }
  169. }
  170. }
  171. /* Allows active scan scan on Ch 12 and 13 */
  172. static void _rtl_reg_apply_active_scan_flags(struct wiphy *wiphy,
  173. enum nl80211_reg_initiator
  174. initiator)
  175. {
  176. struct ieee80211_supported_band *sband;
  177. struct ieee80211_channel *ch;
  178. const struct ieee80211_reg_rule *reg_rule;
  179. if (!wiphy->bands[IEEE80211_BAND_2GHZ])
  180. return;
  181. sband = wiphy->bands[IEEE80211_BAND_2GHZ];
  182. /*
  183. *If no country IE has been received always enable active scan
  184. *on these channels. This is only done for specific regulatory SKUs
  185. */
  186. if (initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  187. ch = &sband->channels[11]; /* CH 12 */
  188. if (ch->flags & IEEE80211_CHAN_NO_IR)
  189. ch->flags &= ~IEEE80211_CHAN_NO_IR;
  190. ch = &sband->channels[12]; /* CH 13 */
  191. if (ch->flags & IEEE80211_CHAN_NO_IR)
  192. ch->flags &= ~IEEE80211_CHAN_NO_IR;
  193. return;
  194. }
  195. /*
  196. *If a country IE has been received check its rule for this
  197. *channel first before enabling active scan. The passive scan
  198. *would have been enforced by the initial processing of our
  199. *custom regulatory domain.
  200. */
  201. ch = &sband->channels[11]; /* CH 12 */
  202. reg_rule = freq_reg_info(wiphy, MHZ_TO_KHZ(ch->center_freq));
  203. if (!IS_ERR(reg_rule)) {
  204. if (!(reg_rule->flags & NL80211_RRF_NO_IR))
  205. if (ch->flags & IEEE80211_CHAN_NO_IR)
  206. ch->flags &= ~IEEE80211_CHAN_NO_IR;
  207. }
  208. ch = &sband->channels[12]; /* CH 13 */
  209. reg_rule = freq_reg_info(wiphy, MHZ_TO_KHZ(ch->center_freq));
  210. if (!IS_ERR(reg_rule)) {
  211. if (!(reg_rule->flags & NL80211_RRF_NO_IR))
  212. if (ch->flags & IEEE80211_CHAN_NO_IR)
  213. ch->flags &= ~IEEE80211_CHAN_NO_IR;
  214. }
  215. }
  216. /*
  217. *Always apply Radar/DFS rules on
  218. *freq range 5260 MHz - 5700 MHz
  219. */
  220. static void _rtl_reg_apply_radar_flags(struct wiphy *wiphy)
  221. {
  222. struct ieee80211_supported_band *sband;
  223. struct ieee80211_channel *ch;
  224. unsigned int i;
  225. if (!wiphy->bands[IEEE80211_BAND_5GHZ])
  226. return;
  227. sband = wiphy->bands[IEEE80211_BAND_5GHZ];
  228. for (i = 0; i < sband->n_channels; i++) {
  229. ch = &sband->channels[i];
  230. if (!_rtl_is_radar_freq(ch->center_freq))
  231. continue;
  232. /*
  233. *We always enable radar detection/DFS on this
  234. *frequency range. Additionally we also apply on
  235. *this frequency range:
  236. *- If STA mode does not yet have DFS supports disable
  237. * active scanning
  238. *- If adhoc mode does not support DFS yet then disable
  239. * adhoc in the frequency.
  240. *- If AP mode does not yet support radar detection/DFS
  241. *do not allow AP mode
  242. */
  243. if (!(ch->flags & IEEE80211_CHAN_DISABLED))
  244. ch->flags |= IEEE80211_CHAN_RADAR |
  245. IEEE80211_CHAN_NO_IR;
  246. }
  247. }
  248. static void _rtl_reg_apply_world_flags(struct wiphy *wiphy,
  249. enum nl80211_reg_initiator initiator,
  250. struct rtl_regulatory *reg)
  251. {
  252. _rtl_reg_apply_beaconing_flags(wiphy, initiator);
  253. _rtl_reg_apply_active_scan_flags(wiphy, initiator);
  254. return;
  255. }
  256. static void _rtl_reg_notifier_apply(struct wiphy *wiphy,
  257. struct regulatory_request *request,
  258. struct rtl_regulatory *reg)
  259. {
  260. /* We always apply this */
  261. _rtl_reg_apply_radar_flags(wiphy);
  262. switch (request->initiator) {
  263. case NL80211_REGDOM_SET_BY_DRIVER:
  264. case NL80211_REGDOM_SET_BY_CORE:
  265. case NL80211_REGDOM_SET_BY_USER:
  266. break;
  267. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  268. _rtl_reg_apply_world_flags(wiphy, request->initiator, reg);
  269. break;
  270. }
  271. }
  272. static const struct ieee80211_regdomain *_rtl_regdomain_select(
  273. struct rtl_regulatory *reg)
  274. {
  275. switch (reg->country_code) {
  276. case COUNTRY_CODE_FCC:
  277. return &rtl_regdom_no_midband;
  278. case COUNTRY_CODE_IC:
  279. return &rtl_regdom_11;
  280. case COUNTRY_CODE_ETSI:
  281. case COUNTRY_CODE_TELEC_NETGEAR:
  282. return &rtl_regdom_60_64;
  283. case COUNTRY_CODE_SPAIN:
  284. case COUNTRY_CODE_FRANCE:
  285. case COUNTRY_CODE_ISRAEL:
  286. case COUNTRY_CODE_WORLD_WIDE_13:
  287. return &rtl_regdom_12_13;
  288. case COUNTRY_CODE_MKK:
  289. case COUNTRY_CODE_MKK1:
  290. case COUNTRY_CODE_TELEC:
  291. case COUNTRY_CODE_MIC:
  292. return &rtl_regdom_14_60_64;
  293. case COUNTRY_CODE_GLOBAL_DOMAIN:
  294. return &rtl_regdom_14;
  295. default:
  296. return &rtl_regdom_no_midband;
  297. }
  298. }
  299. static int _rtl_regd_init_wiphy(struct rtl_regulatory *reg,
  300. struct wiphy *wiphy,
  301. void (*reg_notifier) (struct wiphy *wiphy,
  302. struct regulatory_request *
  303. request))
  304. {
  305. const struct ieee80211_regdomain *regd;
  306. wiphy->reg_notifier = reg_notifier;
  307. wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
  308. wiphy->regulatory_flags &= ~REGULATORY_STRICT_REG;
  309. wiphy->regulatory_flags &= ~REGULATORY_DISABLE_BEACON_HINTS;
  310. regd = _rtl_regdomain_select(reg);
  311. wiphy_apply_custom_regulatory(wiphy, regd);
  312. _rtl_reg_apply_radar_flags(wiphy);
  313. _rtl_reg_apply_world_flags(wiphy, NL80211_REGDOM_SET_BY_DRIVER, reg);
  314. return 0;
  315. }
  316. static struct country_code_to_enum_rd *_rtl_regd_find_country(u16 countrycode)
  317. {
  318. int i;
  319. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  320. if (allCountries[i].countrycode == countrycode)
  321. return &allCountries[i];
  322. }
  323. return NULL;
  324. }
  325. int rtl_regd_init(struct ieee80211_hw *hw,
  326. void (*reg_notifier) (struct wiphy *wiphy,
  327. struct regulatory_request *request))
  328. {
  329. struct rtl_priv *rtlpriv = rtl_priv(hw);
  330. struct wiphy *wiphy = hw->wiphy;
  331. struct country_code_to_enum_rd *country = NULL;
  332. if (wiphy == NULL || &rtlpriv->regd == NULL)
  333. return -EINVAL;
  334. /* init country_code from efuse channel plan */
  335. rtlpriv->regd.country_code = rtlpriv->efuse.channel_plan;
  336. RT_TRACE(rtlpriv, COMP_REGD, DBG_TRACE,
  337. "rtl: EEPROM regdomain: 0x%0x\n", rtlpriv->regd.country_code);
  338. if (rtlpriv->regd.country_code >= COUNTRY_CODE_MAX) {
  339. RT_TRACE(rtlpriv, COMP_REGD, DBG_DMESG,
  340. "rtl: EEPROM indicates invalid contry code, world wide 13 should be used\n");
  341. rtlpriv->regd.country_code = COUNTRY_CODE_WORLD_WIDE_13;
  342. }
  343. country = _rtl_regd_find_country(rtlpriv->regd.country_code);
  344. if (country) {
  345. rtlpriv->regd.alpha2[0] = country->iso_name[0];
  346. rtlpriv->regd.alpha2[1] = country->iso_name[1];
  347. } else {
  348. rtlpriv->regd.alpha2[0] = '0';
  349. rtlpriv->regd.alpha2[1] = '0';
  350. }
  351. RT_TRACE(rtlpriv, COMP_REGD, DBG_TRACE,
  352. "rtl: Country alpha2 being used: %c%c\n",
  353. rtlpriv->regd.alpha2[0], rtlpriv->regd.alpha2[1]);
  354. _rtl_regd_init_wiphy(&rtlpriv->regd, wiphy, reg_notifier);
  355. return 0;
  356. }
  357. void rtl_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
  358. {
  359. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  360. struct rtl_priv *rtlpriv = rtl_priv(hw);
  361. RT_TRACE(rtlpriv, COMP_REGD, DBG_LOUD, "\n");
  362. _rtl_reg_notifier_apply(wiphy, request, &rtlpriv->regd);
  363. }