regd.c 12 KB

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