regd.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /*
  2. * Copyright (c) 2008-2009 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/kernel.h>
  18. #include <linux/export.h>
  19. #include <net/cfg80211.h>
  20. #include <net/mac80211.h>
  21. #include "regd.h"
  22. #include "regd_common.h"
  23. static int __ath_regd_init(struct ath_regulatory *reg);
  24. /*
  25. * This is a set of common rules used by our world regulatory domains.
  26. * We have 12 world regulatory domains. To save space we consolidate
  27. * the regulatory domains in 5 structures by frequency and change
  28. * the flags on our reg_notifier() on a case by case basis.
  29. */
  30. /* Only these channels all allow active scan on all world regulatory domains */
  31. #define ATH9K_2GHZ_CH01_11 REG_RULE(2412-10, 2462+10, 40, 0, 20, 0)
  32. /* We enable active scan on these a case by case basis by regulatory domain */
  33. #define ATH9K_2GHZ_CH12_13 REG_RULE(2467-10, 2472+10, 40, 0, 20,\
  34. NL80211_RRF_NO_IR)
  35. #define ATH9K_2GHZ_CH14 REG_RULE(2484-10, 2484+10, 40, 0, 20,\
  36. NL80211_RRF_NO_IR | \
  37. NL80211_RRF_NO_OFDM)
  38. /* We allow IBSS on these on a case by case basis by regulatory domain */
  39. #define ATH9K_5GHZ_5150_5350 REG_RULE(5150-10, 5350+10, 80, 0, 30,\
  40. NL80211_RRF_NO_IR)
  41. #define ATH9K_5GHZ_5470_5850 REG_RULE(5470-10, 5850+10, 80, 0, 30,\
  42. NL80211_RRF_NO_IR)
  43. #define ATH9K_5GHZ_5725_5850 REG_RULE(5725-10, 5850+10, 80, 0, 30,\
  44. NL80211_RRF_NO_IR)
  45. #define ATH9K_2GHZ_ALL ATH9K_2GHZ_CH01_11, \
  46. ATH9K_2GHZ_CH12_13, \
  47. ATH9K_2GHZ_CH14
  48. #define ATH9K_5GHZ_ALL ATH9K_5GHZ_5150_5350, \
  49. ATH9K_5GHZ_5470_5850
  50. /* This one skips what we call "mid band" */
  51. #define ATH9K_5GHZ_NO_MIDBAND ATH9K_5GHZ_5150_5350, \
  52. ATH9K_5GHZ_5725_5850
  53. /* Can be used for:
  54. * 0x60, 0x61, 0x62 */
  55. static const struct ieee80211_regdomain ath_world_regdom_60_61_62 = {
  56. .n_reg_rules = 5,
  57. .alpha2 = "99",
  58. .reg_rules = {
  59. ATH9K_2GHZ_ALL,
  60. ATH9K_5GHZ_ALL,
  61. }
  62. };
  63. /* Can be used by 0x63 and 0x65 */
  64. static const struct ieee80211_regdomain ath_world_regdom_63_65 = {
  65. .n_reg_rules = 4,
  66. .alpha2 = "99",
  67. .reg_rules = {
  68. ATH9K_2GHZ_CH01_11,
  69. ATH9K_2GHZ_CH12_13,
  70. ATH9K_5GHZ_NO_MIDBAND,
  71. }
  72. };
  73. /* Can be used by 0x64 only */
  74. static const struct ieee80211_regdomain ath_world_regdom_64 = {
  75. .n_reg_rules = 3,
  76. .alpha2 = "99",
  77. .reg_rules = {
  78. ATH9K_2GHZ_CH01_11,
  79. ATH9K_5GHZ_NO_MIDBAND,
  80. }
  81. };
  82. /* Can be used by 0x66 and 0x69 */
  83. static const struct ieee80211_regdomain ath_world_regdom_66_69 = {
  84. .n_reg_rules = 3,
  85. .alpha2 = "99",
  86. .reg_rules = {
  87. ATH9K_2GHZ_CH01_11,
  88. ATH9K_5GHZ_ALL,
  89. }
  90. };
  91. /* Can be used by 0x67, 0x68, 0x6A and 0x6C */
  92. static const struct ieee80211_regdomain ath_world_regdom_67_68_6A_6C = {
  93. .n_reg_rules = 4,
  94. .alpha2 = "99",
  95. .reg_rules = {
  96. ATH9K_2GHZ_CH01_11,
  97. ATH9K_2GHZ_CH12_13,
  98. ATH9K_5GHZ_ALL,
  99. }
  100. };
  101. static bool dynamic_country_user_possible(struct ath_regulatory *reg)
  102. {
  103. if (IS_ENABLED(CONFIG_ATH_REG_DYNAMIC_USER_CERT_TESTING))
  104. return true;
  105. switch (reg->country_code) {
  106. case CTRY_UNITED_STATES:
  107. case CTRY_JAPAN1:
  108. case CTRY_JAPAN2:
  109. case CTRY_JAPAN3:
  110. case CTRY_JAPAN4:
  111. case CTRY_JAPAN5:
  112. case CTRY_JAPAN6:
  113. case CTRY_JAPAN7:
  114. case CTRY_JAPAN8:
  115. case CTRY_JAPAN9:
  116. case CTRY_JAPAN10:
  117. case CTRY_JAPAN11:
  118. case CTRY_JAPAN12:
  119. case CTRY_JAPAN13:
  120. case CTRY_JAPAN14:
  121. case CTRY_JAPAN15:
  122. case CTRY_JAPAN16:
  123. case CTRY_JAPAN17:
  124. case CTRY_JAPAN18:
  125. case CTRY_JAPAN19:
  126. case CTRY_JAPAN20:
  127. case CTRY_JAPAN21:
  128. case CTRY_JAPAN22:
  129. case CTRY_JAPAN23:
  130. case CTRY_JAPAN24:
  131. case CTRY_JAPAN25:
  132. case CTRY_JAPAN26:
  133. case CTRY_JAPAN27:
  134. case CTRY_JAPAN28:
  135. case CTRY_JAPAN29:
  136. case CTRY_JAPAN30:
  137. case CTRY_JAPAN31:
  138. case CTRY_JAPAN32:
  139. case CTRY_JAPAN33:
  140. case CTRY_JAPAN34:
  141. case CTRY_JAPAN35:
  142. case CTRY_JAPAN36:
  143. case CTRY_JAPAN37:
  144. case CTRY_JAPAN38:
  145. case CTRY_JAPAN39:
  146. case CTRY_JAPAN40:
  147. case CTRY_JAPAN41:
  148. case CTRY_JAPAN42:
  149. case CTRY_JAPAN43:
  150. case CTRY_JAPAN44:
  151. case CTRY_JAPAN45:
  152. case CTRY_JAPAN46:
  153. case CTRY_JAPAN47:
  154. case CTRY_JAPAN48:
  155. case CTRY_JAPAN49:
  156. case CTRY_JAPAN50:
  157. case CTRY_JAPAN51:
  158. case CTRY_JAPAN52:
  159. case CTRY_JAPAN53:
  160. case CTRY_JAPAN54:
  161. case CTRY_JAPAN55:
  162. case CTRY_JAPAN56:
  163. case CTRY_JAPAN57:
  164. case CTRY_JAPAN58:
  165. case CTRY_JAPAN59:
  166. return false;
  167. }
  168. return true;
  169. }
  170. static bool ath_reg_dyn_country_user_allow(struct ath_regulatory *reg)
  171. {
  172. if (!IS_ENABLED(CONFIG_ATH_REG_DYNAMIC_USER_REG_HINTS))
  173. return false;
  174. if (!dynamic_country_user_possible(reg))
  175. return false;
  176. return true;
  177. }
  178. static inline bool is_wwr_sku(u16 regd)
  179. {
  180. return ((regd & COUNTRY_ERD_FLAG) != COUNTRY_ERD_FLAG) &&
  181. (((regd & WORLD_SKU_MASK) == WORLD_SKU_PREFIX) ||
  182. (regd == WORLD));
  183. }
  184. static u16 ath_regd_get_eepromRD(struct ath_regulatory *reg)
  185. {
  186. return reg->current_rd & ~WORLDWIDE_ROAMING_FLAG;
  187. }
  188. bool ath_is_world_regd(struct ath_regulatory *reg)
  189. {
  190. return is_wwr_sku(ath_regd_get_eepromRD(reg));
  191. }
  192. EXPORT_SYMBOL(ath_is_world_regd);
  193. static const struct ieee80211_regdomain *ath_default_world_regdomain(void)
  194. {
  195. /* this is the most restrictive */
  196. return &ath_world_regdom_64;
  197. }
  198. static const struct
  199. ieee80211_regdomain *ath_world_regdomain(struct ath_regulatory *reg)
  200. {
  201. switch (reg->regpair->reg_domain) {
  202. case 0x60:
  203. case 0x61:
  204. case 0x62:
  205. return &ath_world_regdom_60_61_62;
  206. case 0x63:
  207. case 0x65:
  208. return &ath_world_regdom_63_65;
  209. case 0x64:
  210. return &ath_world_regdom_64;
  211. case 0x66:
  212. case 0x69:
  213. return &ath_world_regdom_66_69;
  214. case 0x67:
  215. case 0x68:
  216. case 0x6A:
  217. case 0x6C:
  218. return &ath_world_regdom_67_68_6A_6C;
  219. default:
  220. WARN_ON(1);
  221. return ath_default_world_regdomain();
  222. }
  223. }
  224. bool ath_is_49ghz_allowed(u16 regdomain)
  225. {
  226. /* possibly more */
  227. return regdomain == MKK9_MKKC;
  228. }
  229. EXPORT_SYMBOL(ath_is_49ghz_allowed);
  230. /* Frequency is one where radar detection is required */
  231. static bool ath_is_radar_freq(u16 center_freq)
  232. {
  233. return (center_freq >= 5260 && center_freq <= 5700);
  234. }
  235. static void ath_force_clear_no_ir_chan(struct wiphy *wiphy,
  236. struct ieee80211_channel *ch)
  237. {
  238. const struct ieee80211_reg_rule *reg_rule;
  239. reg_rule = freq_reg_info(wiphy, MHZ_TO_KHZ(ch->center_freq));
  240. if (IS_ERR(reg_rule))
  241. return;
  242. if (!(reg_rule->flags & NL80211_RRF_NO_IR))
  243. if (ch->flags & IEEE80211_CHAN_NO_IR)
  244. ch->flags &= ~IEEE80211_CHAN_NO_IR;
  245. }
  246. static void ath_force_clear_no_ir_freq(struct wiphy *wiphy, u16 center_freq)
  247. {
  248. struct ieee80211_channel *ch;
  249. ch = ieee80211_get_channel(wiphy, center_freq);
  250. if (!ch)
  251. return;
  252. ath_force_clear_no_ir_chan(wiphy, ch);
  253. }
  254. static void ath_force_no_ir_chan(struct ieee80211_channel *ch)
  255. {
  256. ch->flags |= IEEE80211_CHAN_NO_IR;
  257. }
  258. static void ath_force_no_ir_freq(struct wiphy *wiphy, u16 center_freq)
  259. {
  260. struct ieee80211_channel *ch;
  261. ch = ieee80211_get_channel(wiphy, center_freq);
  262. if (!ch)
  263. return;
  264. ath_force_no_ir_chan(ch);
  265. }
  266. static void
  267. __ath_reg_apply_beaconing_flags(struct wiphy *wiphy,
  268. struct ath_regulatory *reg,
  269. enum nl80211_reg_initiator initiator,
  270. struct ieee80211_channel *ch)
  271. {
  272. if (ath_is_radar_freq(ch->center_freq) ||
  273. (ch->flags & IEEE80211_CHAN_RADAR))
  274. return;
  275. switch (initiator) {
  276. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  277. ath_force_clear_no_ir_chan(wiphy, ch);
  278. break;
  279. case NL80211_REGDOM_SET_BY_USER:
  280. if (ath_reg_dyn_country_user_allow(reg))
  281. ath_force_clear_no_ir_chan(wiphy, ch);
  282. break;
  283. default:
  284. if (ch->beacon_found)
  285. ch->flags &= ~IEEE80211_CHAN_NO_IR;
  286. }
  287. }
  288. /*
  289. * These exception rules do not apply radar frequencies.
  290. *
  291. * - We enable initiating radiation if the country IE says its fine:
  292. * - If no country IE has been processed and a we determine we have
  293. * received a beacon on a channel we can enable initiating radiation.
  294. */
  295. static void
  296. ath_reg_apply_beaconing_flags(struct wiphy *wiphy,
  297. struct ath_regulatory *reg,
  298. enum nl80211_reg_initiator initiator)
  299. {
  300. enum nl80211_band band;
  301. struct ieee80211_supported_band *sband;
  302. struct ieee80211_channel *ch;
  303. unsigned int i;
  304. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  305. if (!wiphy->bands[band])
  306. continue;
  307. sband = wiphy->bands[band];
  308. for (i = 0; i < sband->n_channels; i++) {
  309. ch = &sband->channels[i];
  310. __ath_reg_apply_beaconing_flags(wiphy, reg,
  311. initiator, ch);
  312. }
  313. }
  314. }
  315. /**
  316. * ath_reg_apply_ir_flags()
  317. * @wiphy: the wiphy to use
  318. * @initiator: the regulatory hint initiator
  319. *
  320. * If no country IE has been received always enable passive scan
  321. * and no-ibss on these channels. This is only done for specific
  322. * regulatory SKUs.
  323. *
  324. * If a country IE has been received check its rule for this
  325. * channel first before enabling active scan. The passive scan
  326. * would have been enforced by the initial processing of our
  327. * custom regulatory domain.
  328. */
  329. static void
  330. ath_reg_apply_ir_flags(struct wiphy *wiphy,
  331. struct ath_regulatory *reg,
  332. enum nl80211_reg_initiator initiator)
  333. {
  334. struct ieee80211_supported_band *sband;
  335. sband = wiphy->bands[NL80211_BAND_2GHZ];
  336. if (!sband)
  337. return;
  338. switch(initiator) {
  339. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  340. ath_force_clear_no_ir_freq(wiphy, 2467);
  341. ath_force_clear_no_ir_freq(wiphy, 2472);
  342. break;
  343. case NL80211_REGDOM_SET_BY_USER:
  344. if (!ath_reg_dyn_country_user_allow(reg))
  345. break;
  346. ath_force_clear_no_ir_freq(wiphy, 2467);
  347. ath_force_clear_no_ir_freq(wiphy, 2472);
  348. break;
  349. default:
  350. ath_force_no_ir_freq(wiphy, 2467);
  351. ath_force_no_ir_freq(wiphy, 2472);
  352. }
  353. }
  354. /* Always apply Radar/DFS rules on freq range 5260 MHz - 5700 MHz */
  355. static void ath_reg_apply_radar_flags(struct wiphy *wiphy)
  356. {
  357. struct ieee80211_supported_band *sband;
  358. struct ieee80211_channel *ch;
  359. unsigned int i;
  360. if (!wiphy->bands[NL80211_BAND_5GHZ])
  361. return;
  362. sband = wiphy->bands[NL80211_BAND_5GHZ];
  363. for (i = 0; i < sband->n_channels; i++) {
  364. ch = &sband->channels[i];
  365. if (!ath_is_radar_freq(ch->center_freq))
  366. continue;
  367. /* We always enable radar detection/DFS on this
  368. * frequency range. Additionally we also apply on
  369. * this frequency range:
  370. * - If STA mode does not yet have DFS supports disable
  371. * active scanning
  372. * - If adhoc mode does not support DFS yet then
  373. * disable adhoc in the frequency.
  374. * - If AP mode does not yet support radar detection/DFS
  375. * do not allow AP mode
  376. */
  377. if (!(ch->flags & IEEE80211_CHAN_DISABLED))
  378. ch->flags |= IEEE80211_CHAN_RADAR |
  379. IEEE80211_CHAN_NO_IR;
  380. }
  381. }
  382. static void ath_reg_apply_world_flags(struct wiphy *wiphy,
  383. enum nl80211_reg_initiator initiator,
  384. struct ath_regulatory *reg)
  385. {
  386. switch (reg->regpair->reg_domain) {
  387. case 0x60:
  388. case 0x63:
  389. case 0x66:
  390. case 0x67:
  391. case 0x6C:
  392. ath_reg_apply_beaconing_flags(wiphy, reg, initiator);
  393. break;
  394. case 0x68:
  395. ath_reg_apply_beaconing_flags(wiphy, reg, initiator);
  396. ath_reg_apply_ir_flags(wiphy, reg, initiator);
  397. break;
  398. default:
  399. if (ath_reg_dyn_country_user_allow(reg))
  400. ath_reg_apply_beaconing_flags(wiphy, reg, initiator);
  401. }
  402. }
  403. u16 ath_regd_find_country_by_name(char *alpha2)
  404. {
  405. unsigned int i;
  406. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  407. if (!memcmp(allCountries[i].isoName, alpha2, 2))
  408. return allCountries[i].countryCode;
  409. }
  410. return -1;
  411. }
  412. EXPORT_SYMBOL(ath_regd_find_country_by_name);
  413. static int __ath_reg_dyn_country(struct wiphy *wiphy,
  414. struct ath_regulatory *reg,
  415. struct regulatory_request *request)
  416. {
  417. u16 country_code;
  418. if (request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  419. !ath_is_world_regd(reg))
  420. return -EINVAL;
  421. country_code = ath_regd_find_country_by_name(request->alpha2);
  422. if (country_code == (u16) -1)
  423. return -EINVAL;
  424. reg->current_rd = COUNTRY_ERD_FLAG;
  425. reg->current_rd |= country_code;
  426. __ath_regd_init(reg);
  427. ath_reg_apply_world_flags(wiphy, request->initiator, reg);
  428. return 0;
  429. }
  430. static void ath_reg_dyn_country(struct wiphy *wiphy,
  431. struct ath_regulatory *reg,
  432. struct regulatory_request *request)
  433. {
  434. if (__ath_reg_dyn_country(wiphy, reg, request))
  435. return;
  436. printk(KERN_DEBUG "ath: regdomain 0x%0x "
  437. "dynamically updated by %s\n",
  438. reg->current_rd,
  439. reg_initiator_name(request->initiator));
  440. }
  441. void ath_reg_notifier_apply(struct wiphy *wiphy,
  442. struct regulatory_request *request,
  443. struct ath_regulatory *reg)
  444. {
  445. struct ath_common *common = container_of(reg, struct ath_common,
  446. regulatory);
  447. /* We always apply this */
  448. ath_reg_apply_radar_flags(wiphy);
  449. /*
  450. * This would happen when we have sent a custom regulatory request
  451. * a world regulatory domain and the scheduler hasn't yet processed
  452. * any pending requests in the queue.
  453. */
  454. if (!request)
  455. return;
  456. reg->region = request->dfs_region;
  457. switch (request->initiator) {
  458. case NL80211_REGDOM_SET_BY_CORE:
  459. /*
  460. * If common->reg_world_copy is world roaming it means we *were*
  461. * world roaming... so we now have to restore that data.
  462. */
  463. if (!ath_is_world_regd(&common->reg_world_copy))
  464. break;
  465. memcpy(reg, &common->reg_world_copy,
  466. sizeof(struct ath_regulatory));
  467. break;
  468. case NL80211_REGDOM_SET_BY_DRIVER:
  469. break;
  470. case NL80211_REGDOM_SET_BY_USER:
  471. if (ath_reg_dyn_country_user_allow(reg))
  472. ath_reg_dyn_country(wiphy, reg, request);
  473. break;
  474. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  475. ath_reg_dyn_country(wiphy, reg, request);
  476. break;
  477. }
  478. }
  479. EXPORT_SYMBOL(ath_reg_notifier_apply);
  480. static bool ath_regd_is_eeprom_valid(struct ath_regulatory *reg)
  481. {
  482. u16 rd = ath_regd_get_eepromRD(reg);
  483. int i;
  484. if (rd & COUNTRY_ERD_FLAG) {
  485. /* EEPROM value is a country code */
  486. u16 cc = rd & ~COUNTRY_ERD_FLAG;
  487. printk(KERN_DEBUG
  488. "ath: EEPROM indicates we should expect "
  489. "a country code\n");
  490. for (i = 0; i < ARRAY_SIZE(allCountries); i++)
  491. if (allCountries[i].countryCode == cc)
  492. return true;
  493. } else {
  494. /* EEPROM value is a regpair value */
  495. if (rd != CTRY_DEFAULT)
  496. printk(KERN_DEBUG "ath: EEPROM indicates we "
  497. "should expect a direct regpair map\n");
  498. for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++)
  499. if (regDomainPairs[i].reg_domain == rd)
  500. return true;
  501. }
  502. printk(KERN_DEBUG
  503. "ath: invalid regulatory domain/country code 0x%x\n", rd);
  504. return false;
  505. }
  506. /* EEPROM country code to regpair mapping */
  507. static struct country_code_to_enum_rd*
  508. ath_regd_find_country(u16 countryCode)
  509. {
  510. int i;
  511. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  512. if (allCountries[i].countryCode == countryCode)
  513. return &allCountries[i];
  514. }
  515. return NULL;
  516. }
  517. /* EEPROM rd code to regpair mapping */
  518. static struct country_code_to_enum_rd*
  519. ath_regd_find_country_by_rd(int regdmn)
  520. {
  521. int i;
  522. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  523. if (allCountries[i].regDmnEnum == regdmn)
  524. return &allCountries[i];
  525. }
  526. return NULL;
  527. }
  528. /* Returns the map of the EEPROM set RD to a country code */
  529. static u16 ath_regd_get_default_country(u16 rd)
  530. {
  531. if (rd & COUNTRY_ERD_FLAG) {
  532. struct country_code_to_enum_rd *country = NULL;
  533. u16 cc = rd & ~COUNTRY_ERD_FLAG;
  534. country = ath_regd_find_country(cc);
  535. if (country != NULL)
  536. return cc;
  537. }
  538. return CTRY_DEFAULT;
  539. }
  540. static struct reg_dmn_pair_mapping*
  541. ath_get_regpair(int regdmn)
  542. {
  543. int i;
  544. if (regdmn == NO_ENUMRD)
  545. return NULL;
  546. for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++) {
  547. if (regDomainPairs[i].reg_domain == regdmn)
  548. return &regDomainPairs[i];
  549. }
  550. return NULL;
  551. }
  552. static int
  553. ath_regd_init_wiphy(struct ath_regulatory *reg,
  554. struct wiphy *wiphy,
  555. void (*reg_notifier)(struct wiphy *wiphy,
  556. struct regulatory_request *request))
  557. {
  558. const struct ieee80211_regdomain *regd;
  559. wiphy->reg_notifier = reg_notifier;
  560. wiphy->regulatory_flags |= REGULATORY_STRICT_REG |
  561. REGULATORY_CUSTOM_REG;
  562. if (ath_is_world_regd(reg)) {
  563. /*
  564. * Anything applied here (prior to wiphy registration) gets
  565. * saved on the wiphy orig_* parameters
  566. */
  567. regd = ath_world_regdomain(reg);
  568. wiphy->regulatory_flags |= REGULATORY_COUNTRY_IE_FOLLOW_POWER;
  569. } else {
  570. /*
  571. * This gets applied in the case of the absence of CRDA,
  572. * it's our own custom world regulatory domain, similar to
  573. * cfg80211's but we enable passive scanning.
  574. */
  575. regd = ath_default_world_regdomain();
  576. }
  577. wiphy_apply_custom_regulatory(wiphy, regd);
  578. ath_reg_apply_radar_flags(wiphy);
  579. ath_reg_apply_world_flags(wiphy, NL80211_REGDOM_SET_BY_DRIVER, reg);
  580. return 0;
  581. }
  582. /*
  583. * Some users have reported their EEPROM programmed with
  584. * 0x8000 set, this is not a supported regulatory domain
  585. * but since we have more than one user with it we need
  586. * a solution for them. We default to 0x64, which is the
  587. * default Atheros world regulatory domain.
  588. */
  589. static void ath_regd_sanitize(struct ath_regulatory *reg)
  590. {
  591. if (reg->current_rd != COUNTRY_ERD_FLAG)
  592. return;
  593. printk(KERN_DEBUG "ath: EEPROM regdomain sanitized\n");
  594. reg->current_rd = 0x64;
  595. }
  596. static int __ath_regd_init(struct ath_regulatory *reg)
  597. {
  598. struct country_code_to_enum_rd *country = NULL;
  599. u16 regdmn;
  600. if (!reg)
  601. return -EINVAL;
  602. ath_regd_sanitize(reg);
  603. printk(KERN_DEBUG "ath: EEPROM regdomain: 0x%0x\n", reg->current_rd);
  604. if (!ath_regd_is_eeprom_valid(reg)) {
  605. pr_err("Invalid EEPROM contents\n");
  606. return -EINVAL;
  607. }
  608. regdmn = ath_regd_get_eepromRD(reg);
  609. reg->country_code = ath_regd_get_default_country(regdmn);
  610. if (reg->country_code == CTRY_DEFAULT &&
  611. regdmn == CTRY_DEFAULT) {
  612. printk(KERN_DEBUG "ath: EEPROM indicates default "
  613. "country code should be used\n");
  614. reg->country_code = CTRY_UNITED_STATES;
  615. }
  616. if (reg->country_code == CTRY_DEFAULT) {
  617. country = NULL;
  618. } else {
  619. printk(KERN_DEBUG "ath: doing EEPROM country->regdmn "
  620. "map search\n");
  621. country = ath_regd_find_country(reg->country_code);
  622. if (country == NULL) {
  623. printk(KERN_DEBUG
  624. "ath: no valid country maps found for "
  625. "country code: 0x%0x\n",
  626. reg->country_code);
  627. return -EINVAL;
  628. } else {
  629. regdmn = country->regDmnEnum;
  630. printk(KERN_DEBUG "ath: country maps to "
  631. "regdmn code: 0x%0x\n",
  632. regdmn);
  633. }
  634. }
  635. reg->regpair = ath_get_regpair(regdmn);
  636. if (!reg->regpair) {
  637. printk(KERN_DEBUG "ath: "
  638. "No regulatory domain pair found, cannot continue\n");
  639. return -EINVAL;
  640. }
  641. if (!country)
  642. country = ath_regd_find_country_by_rd(regdmn);
  643. if (country) {
  644. reg->alpha2[0] = country->isoName[0];
  645. reg->alpha2[1] = country->isoName[1];
  646. } else {
  647. reg->alpha2[0] = '0';
  648. reg->alpha2[1] = '0';
  649. }
  650. printk(KERN_DEBUG "ath: Country alpha2 being used: %c%c\n",
  651. reg->alpha2[0], reg->alpha2[1]);
  652. printk(KERN_DEBUG "ath: Regpair used: 0x%0x\n",
  653. reg->regpair->reg_domain);
  654. return 0;
  655. }
  656. int
  657. ath_regd_init(struct ath_regulatory *reg,
  658. struct wiphy *wiphy,
  659. void (*reg_notifier)(struct wiphy *wiphy,
  660. struct regulatory_request *request))
  661. {
  662. struct ath_common *common = container_of(reg, struct ath_common,
  663. regulatory);
  664. int r;
  665. r = __ath_regd_init(reg);
  666. if (r)
  667. return r;
  668. if (ath_is_world_regd(reg))
  669. memcpy(&common->reg_world_copy, reg,
  670. sizeof(struct ath_regulatory));
  671. ath_regd_init_wiphy(reg, wiphy, reg_notifier);
  672. return 0;
  673. }
  674. EXPORT_SYMBOL(ath_regd_init);
  675. u32 ath_regd_get_band_ctl(struct ath_regulatory *reg,
  676. enum nl80211_band band)
  677. {
  678. if (!reg->regpair ||
  679. (reg->country_code == CTRY_DEFAULT &&
  680. is_wwr_sku(ath_regd_get_eepromRD(reg)))) {
  681. return SD_NO_CTL;
  682. }
  683. if (ath_regd_get_eepromRD(reg) == CTRY_DEFAULT) {
  684. switch (reg->region) {
  685. case NL80211_DFS_FCC:
  686. return CTL_FCC;
  687. case NL80211_DFS_ETSI:
  688. return CTL_ETSI;
  689. case NL80211_DFS_JP:
  690. return CTL_MKK;
  691. default:
  692. break;
  693. }
  694. }
  695. switch (band) {
  696. case NL80211_BAND_2GHZ:
  697. return reg->regpair->reg_2ghz_ctl;
  698. case NL80211_BAND_5GHZ:
  699. return reg->regpair->reg_5ghz_ctl;
  700. default:
  701. return NO_CTL;
  702. }
  703. }
  704. EXPORT_SYMBOL(ath_regd_get_band_ctl);