regd.c 12 KB

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