regd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. u32 bandwidth = 0;
  145. int r;
  146. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  147. if (!wiphy->bands[band])
  148. continue;
  149. sband = wiphy->bands[band];
  150. for (i = 0; i < sband->n_channels; i++) {
  151. ch = &sband->channels[i];
  152. if (_rtl_is_radar_freq(ch->center_freq) ||
  153. (ch->flags & IEEE80211_CHAN_RADAR))
  154. continue;
  155. if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  156. r = freq_reg_info(wiphy, ch->center_freq,
  157. bandwidth, &reg_rule);
  158. if (r)
  159. continue;
  160. /*
  161. *If 11d had a rule for this channel ensure
  162. *we enable adhoc/beaconing if it allows us to
  163. *use it. Note that we would have disabled it
  164. *by applying our static world regdomain by
  165. *default during init, prior to calling our
  166. *regulatory_hint().
  167. */
  168. if (!(reg_rule->flags & NL80211_RRF_NO_IBSS))
  169. ch->flags &= ~IEEE80211_CHAN_NO_IBSS;
  170. if (!(reg_rule->
  171. flags & NL80211_RRF_PASSIVE_SCAN))
  172. ch->flags &=
  173. ~IEEE80211_CHAN_PASSIVE_SCAN;
  174. } else {
  175. if (ch->beacon_found)
  176. ch->flags &= ~(IEEE80211_CHAN_NO_IBSS |
  177. IEEE80211_CHAN_PASSIVE_SCAN);
  178. }
  179. }
  180. }
  181. }
  182. /* Allows active scan scan on Ch 12 and 13 */
  183. static void _rtl_reg_apply_active_scan_flags(struct wiphy *wiphy,
  184. enum nl80211_reg_initiator
  185. initiator)
  186. {
  187. struct ieee80211_supported_band *sband;
  188. struct ieee80211_channel *ch;
  189. const struct ieee80211_reg_rule *reg_rule;
  190. u32 bandwidth = 0;
  191. int r;
  192. if (!wiphy->bands[IEEE80211_BAND_2GHZ])
  193. return;
  194. sband = wiphy->bands[IEEE80211_BAND_2GHZ];
  195. /*
  196. *If no country IE has been received always enable active scan
  197. *on these channels. This is only done for specific regulatory SKUs
  198. */
  199. if (initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  200. ch = &sband->channels[11]; /* CH 12 */
  201. if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
  202. ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
  203. ch = &sband->channels[12]; /* CH 13 */
  204. if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
  205. ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
  206. return;
  207. }
  208. /*
  209. *If a country IE has been received check its rule for this
  210. *channel first before enabling active scan. The passive scan
  211. *would have been enforced by the initial processing of our
  212. *custom regulatory domain.
  213. */
  214. ch = &sband->channels[11]; /* CH 12 */
  215. r = freq_reg_info(wiphy, ch->center_freq, bandwidth, &reg_rule);
  216. if (!r) {
  217. if (!(reg_rule->flags & NL80211_RRF_PASSIVE_SCAN))
  218. if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
  219. ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
  220. }
  221. ch = &sband->channels[12]; /* CH 13 */
  222. r = freq_reg_info(wiphy, ch->center_freq, bandwidth, &reg_rule);
  223. if (!r) {
  224. if (!(reg_rule->flags & NL80211_RRF_PASSIVE_SCAN))
  225. if (ch->flags & IEEE80211_CHAN_PASSIVE_SCAN)
  226. ch->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
  227. }
  228. }
  229. /*
  230. *Always apply Radar/DFS rules on
  231. *freq range 5260 MHz - 5700 MHz
  232. */
  233. static void _rtl_reg_apply_radar_flags(struct wiphy *wiphy)
  234. {
  235. struct ieee80211_supported_band *sband;
  236. struct ieee80211_channel *ch;
  237. unsigned int i;
  238. if (!wiphy->bands[IEEE80211_BAND_5GHZ])
  239. return;
  240. sband = wiphy->bands[IEEE80211_BAND_5GHZ];
  241. for (i = 0; i < sband->n_channels; i++) {
  242. ch = &sband->channels[i];
  243. if (!_rtl_is_radar_freq(ch->center_freq))
  244. continue;
  245. /*
  246. *We always enable radar detection/DFS on this
  247. *frequency range. Additionally we also apply on
  248. *this frequency range:
  249. *- If STA mode does not yet have DFS supports disable
  250. * active scanning
  251. *- If adhoc mode does not support DFS yet then disable
  252. * adhoc in the frequency.
  253. *- If AP mode does not yet support radar detection/DFS
  254. *do not allow AP mode
  255. */
  256. if (!(ch->flags & IEEE80211_CHAN_DISABLED))
  257. ch->flags |= IEEE80211_CHAN_RADAR |
  258. IEEE80211_CHAN_NO_IBSS |
  259. IEEE80211_CHAN_PASSIVE_SCAN;
  260. }
  261. }
  262. static void _rtl_reg_apply_world_flags(struct wiphy *wiphy,
  263. enum nl80211_reg_initiator initiator,
  264. struct rtl_regulatory *reg)
  265. {
  266. _rtl_reg_apply_beaconing_flags(wiphy, initiator);
  267. _rtl_reg_apply_active_scan_flags(wiphy, initiator);
  268. return;
  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. return 0;
  286. }
  287. static const struct ieee80211_regdomain *_rtl_regdomain_select(
  288. struct rtl_regulatory *reg)
  289. {
  290. switch (reg->country_code) {
  291. case COUNTRY_CODE_FCC:
  292. return &rtl_regdom_no_midband;
  293. case COUNTRY_CODE_IC:
  294. return &rtl_regdom_11;
  295. case COUNTRY_CODE_ETSI:
  296. case COUNTRY_CODE_TELEC_NETGEAR:
  297. return &rtl_regdom_60_64;
  298. case COUNTRY_CODE_SPAIN:
  299. case COUNTRY_CODE_FRANCE:
  300. case COUNTRY_CODE_ISRAEL:
  301. case COUNTRY_CODE_WORLD_WIDE_13:
  302. return &rtl_regdom_12_13;
  303. case COUNTRY_CODE_MKK:
  304. case COUNTRY_CODE_MKK1:
  305. case COUNTRY_CODE_TELEC:
  306. case COUNTRY_CODE_MIC:
  307. return &rtl_regdom_14_60_64;
  308. case COUNTRY_CODE_GLOBAL_DOMAIN:
  309. return &rtl_regdom_14;
  310. default:
  311. return &rtl_regdom_no_midband;
  312. }
  313. }
  314. static int _rtl_regd_init_wiphy(struct rtl_regulatory *reg,
  315. struct wiphy *wiphy,
  316. int (*reg_notifier) (struct wiphy *wiphy,
  317. struct regulatory_request *
  318. request))
  319. {
  320. const struct ieee80211_regdomain *regd;
  321. wiphy->reg_notifier = reg_notifier;
  322. wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  323. wiphy->flags &= ~WIPHY_FLAG_STRICT_REGULATORY;
  324. wiphy->flags &= ~WIPHY_FLAG_DISABLE_BEACON_HINTS;
  325. regd = _rtl_regdomain_select(reg);
  326. wiphy_apply_custom_regulatory(wiphy, regd);
  327. _rtl_reg_apply_radar_flags(wiphy);
  328. _rtl_reg_apply_world_flags(wiphy, NL80211_REGDOM_SET_BY_DRIVER, reg);
  329. return 0;
  330. }
  331. static struct country_code_to_enum_rd *_rtl_regd_find_country(u16 countrycode)
  332. {
  333. int i;
  334. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  335. if (allCountries[i].countrycode == countrycode)
  336. return &allCountries[i];
  337. }
  338. return NULL;
  339. }
  340. int rtl_regd_init(struct ieee80211_hw *hw,
  341. int (*reg_notifier) (struct wiphy *wiphy,
  342. struct regulatory_request *request))
  343. {
  344. struct rtl_priv *rtlpriv = rtl_priv(hw);
  345. struct wiphy *wiphy = hw->wiphy;
  346. struct country_code_to_enum_rd *country = NULL;
  347. if (wiphy == NULL || &rtlpriv->regd == NULL)
  348. return -EINVAL;
  349. /* init country_code from efuse channel plan */
  350. rtlpriv->regd.country_code = rtlpriv->efuse.channel_plan;
  351. RT_TRACE(rtlpriv, COMP_REGD, DBG_TRACE,
  352. "rtl: EEPROM regdomain: 0x%0x\n", rtlpriv->regd.country_code);
  353. if (rtlpriv->regd.country_code >= COUNTRY_CODE_MAX) {
  354. RT_TRACE(rtlpriv, COMP_REGD, DBG_DMESG,
  355. "rtl: EEPROM indicates invalid contry code, world wide 13 should be used\n");
  356. rtlpriv->regd.country_code = COUNTRY_CODE_WORLD_WIDE_13;
  357. }
  358. country = _rtl_regd_find_country(rtlpriv->regd.country_code);
  359. if (country) {
  360. rtlpriv->regd.alpha2[0] = country->iso_name[0];
  361. rtlpriv->regd.alpha2[1] = country->iso_name[1];
  362. } else {
  363. rtlpriv->regd.alpha2[0] = '0';
  364. rtlpriv->regd.alpha2[1] = '0';
  365. }
  366. RT_TRACE(rtlpriv, COMP_REGD, DBG_TRACE,
  367. "rtl: Country alpha2 being used: %c%c\n",
  368. rtlpriv->regd.alpha2[0], rtlpriv->regd.alpha2[1]);
  369. _rtl_regd_init_wiphy(&rtlpriv->regd, wiphy, reg_notifier);
  370. return 0;
  371. }
  372. int rtl_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
  373. {
  374. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  375. struct rtl_priv *rtlpriv = rtl_priv(hw);
  376. RT_TRACE(rtlpriv, COMP_REGD, DBG_LOUD, "\n");
  377. return _rtl_reg_notifier_apply(wiphy, request, &rtlpriv->regd);
  378. }