regd.c 12 KB

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