regd.c 12 KB

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