regd.c 11 KB

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