regd.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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. static 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. static int __ath_reg_dyn_country(struct wiphy *wiphy,
  413. struct ath_regulatory *reg,
  414. struct regulatory_request *request)
  415. {
  416. u16 country_code;
  417. if (request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  418. !ath_is_world_regd(reg))
  419. return -EINVAL;
  420. country_code = ath_regd_find_country_by_name(request->alpha2);
  421. if (country_code == (u16) -1)
  422. return -EINVAL;
  423. reg->current_rd = COUNTRY_ERD_FLAG;
  424. reg->current_rd |= country_code;
  425. __ath_regd_init(reg);
  426. ath_reg_apply_world_flags(wiphy, request->initiator, reg);
  427. return 0;
  428. }
  429. static void ath_reg_dyn_country(struct wiphy *wiphy,
  430. struct ath_regulatory *reg,
  431. struct regulatory_request *request)
  432. {
  433. if (__ath_reg_dyn_country(wiphy, reg, request))
  434. return;
  435. printk(KERN_DEBUG "ath: regdomain 0x%0x "
  436. "dynamically updated by %s\n",
  437. reg->current_rd,
  438. reg_initiator_name(request->initiator));
  439. }
  440. void ath_reg_notifier_apply(struct wiphy *wiphy,
  441. struct regulatory_request *request,
  442. struct ath_regulatory *reg)
  443. {
  444. struct ath_common *common = container_of(reg, struct ath_common,
  445. regulatory);
  446. /* We always apply this */
  447. ath_reg_apply_radar_flags(wiphy);
  448. /*
  449. * This would happen when we have sent a custom regulatory request
  450. * a world regulatory domain and the scheduler hasn't yet processed
  451. * any pending requests in the queue.
  452. */
  453. if (!request)
  454. return;
  455. reg->region = request->dfs_region;
  456. switch (request->initiator) {
  457. case NL80211_REGDOM_SET_BY_CORE:
  458. /*
  459. * If common->reg_world_copy is world roaming it means we *were*
  460. * world roaming... so we now have to restore that data.
  461. */
  462. if (!ath_is_world_regd(&common->reg_world_copy))
  463. break;
  464. memcpy(reg, &common->reg_world_copy,
  465. sizeof(struct ath_regulatory));
  466. break;
  467. case NL80211_REGDOM_SET_BY_DRIVER:
  468. break;
  469. case NL80211_REGDOM_SET_BY_USER:
  470. if (ath_reg_dyn_country_user_allow(reg))
  471. ath_reg_dyn_country(wiphy, reg, request);
  472. break;
  473. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  474. ath_reg_dyn_country(wiphy, reg, request);
  475. break;
  476. }
  477. }
  478. EXPORT_SYMBOL(ath_reg_notifier_apply);
  479. static bool ath_regd_is_eeprom_valid(struct ath_regulatory *reg)
  480. {
  481. u16 rd = ath_regd_get_eepromRD(reg);
  482. int i;
  483. if (rd & COUNTRY_ERD_FLAG) {
  484. /* EEPROM value is a country code */
  485. u16 cc = rd & ~COUNTRY_ERD_FLAG;
  486. printk(KERN_DEBUG
  487. "ath: EEPROM indicates we should expect "
  488. "a country code\n");
  489. for (i = 0; i < ARRAY_SIZE(allCountries); i++)
  490. if (allCountries[i].countryCode == cc)
  491. return true;
  492. } else {
  493. /* EEPROM value is a regpair value */
  494. if (rd != CTRY_DEFAULT)
  495. printk(KERN_DEBUG "ath: EEPROM indicates we "
  496. "should expect a direct regpair map\n");
  497. for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++)
  498. if (regDomainPairs[i].reg_domain == rd)
  499. return true;
  500. }
  501. printk(KERN_DEBUG
  502. "ath: invalid regulatory domain/country code 0x%x\n", rd);
  503. return false;
  504. }
  505. /* EEPROM country code to regpair mapping */
  506. static struct country_code_to_enum_rd*
  507. ath_regd_find_country(u16 countryCode)
  508. {
  509. int i;
  510. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  511. if (allCountries[i].countryCode == countryCode)
  512. return &allCountries[i];
  513. }
  514. return NULL;
  515. }
  516. /* EEPROM rd code to regpair mapping */
  517. static struct country_code_to_enum_rd*
  518. ath_regd_find_country_by_rd(int regdmn)
  519. {
  520. int i;
  521. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  522. if (allCountries[i].regDmnEnum == regdmn)
  523. return &allCountries[i];
  524. }
  525. return NULL;
  526. }
  527. /* Returns the map of the EEPROM set RD to a country code */
  528. static u16 ath_regd_get_default_country(u16 rd)
  529. {
  530. if (rd & COUNTRY_ERD_FLAG) {
  531. struct country_code_to_enum_rd *country = NULL;
  532. u16 cc = rd & ~COUNTRY_ERD_FLAG;
  533. country = ath_regd_find_country(cc);
  534. if (country != NULL)
  535. return cc;
  536. }
  537. return CTRY_DEFAULT;
  538. }
  539. static struct reg_dmn_pair_mapping*
  540. ath_get_regpair(int regdmn)
  541. {
  542. int i;
  543. if (regdmn == NO_ENUMRD)
  544. return NULL;
  545. for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++) {
  546. if (regDomainPairs[i].reg_domain == regdmn)
  547. return &regDomainPairs[i];
  548. }
  549. return NULL;
  550. }
  551. static int
  552. ath_regd_init_wiphy(struct ath_regulatory *reg,
  553. struct wiphy *wiphy,
  554. void (*reg_notifier)(struct wiphy *wiphy,
  555. struct regulatory_request *request))
  556. {
  557. const struct ieee80211_regdomain *regd;
  558. wiphy->reg_notifier = reg_notifier;
  559. wiphy->regulatory_flags |= REGULATORY_STRICT_REG |
  560. REGULATORY_CUSTOM_REG;
  561. if (ath_is_world_regd(reg)) {
  562. /*
  563. * Anything applied here (prior to wiphy registration) gets
  564. * saved on the wiphy orig_* parameters
  565. */
  566. regd = ath_world_regdomain(reg);
  567. wiphy->regulatory_flags |= REGULATORY_COUNTRY_IE_FOLLOW_POWER;
  568. } else {
  569. /*
  570. * This gets applied in the case of the absence of CRDA,
  571. * it's our own custom world regulatory domain, similar to
  572. * cfg80211's but we enable passive scanning.
  573. */
  574. regd = ath_default_world_regdomain();
  575. }
  576. wiphy_apply_custom_regulatory(wiphy, regd);
  577. ath_reg_apply_radar_flags(wiphy);
  578. ath_reg_apply_world_flags(wiphy, NL80211_REGDOM_SET_BY_DRIVER, reg);
  579. return 0;
  580. }
  581. /*
  582. * Some users have reported their EEPROM programmed with
  583. * 0x8000 set, this is not a supported regulatory domain
  584. * but since we have more than one user with it we need
  585. * a solution for them. We default to 0x64, which is the
  586. * default Atheros world regulatory domain.
  587. */
  588. static void ath_regd_sanitize(struct ath_regulatory *reg)
  589. {
  590. if (reg->current_rd != COUNTRY_ERD_FLAG)
  591. return;
  592. printk(KERN_DEBUG "ath: EEPROM regdomain sanitized\n");
  593. reg->current_rd = 0x64;
  594. }
  595. static int __ath_regd_init(struct ath_regulatory *reg)
  596. {
  597. struct country_code_to_enum_rd *country = NULL;
  598. u16 regdmn;
  599. if (!reg)
  600. return -EINVAL;
  601. ath_regd_sanitize(reg);
  602. printk(KERN_DEBUG "ath: EEPROM regdomain: 0x%0x\n", reg->current_rd);
  603. if (!ath_regd_is_eeprom_valid(reg)) {
  604. pr_err("Invalid EEPROM contents\n");
  605. return -EINVAL;
  606. }
  607. regdmn = ath_regd_get_eepromRD(reg);
  608. reg->country_code = ath_regd_get_default_country(regdmn);
  609. if (reg->country_code == CTRY_DEFAULT &&
  610. regdmn == CTRY_DEFAULT) {
  611. printk(KERN_DEBUG "ath: EEPROM indicates default "
  612. "country code should be used\n");
  613. reg->country_code = CTRY_UNITED_STATES;
  614. }
  615. if (reg->country_code == CTRY_DEFAULT) {
  616. country = NULL;
  617. } else {
  618. printk(KERN_DEBUG "ath: doing EEPROM country->regdmn "
  619. "map search\n");
  620. country = ath_regd_find_country(reg->country_code);
  621. if (country == NULL) {
  622. printk(KERN_DEBUG
  623. "ath: no valid country maps found for "
  624. "country code: 0x%0x\n",
  625. reg->country_code);
  626. return -EINVAL;
  627. } else {
  628. regdmn = country->regDmnEnum;
  629. printk(KERN_DEBUG "ath: country maps to "
  630. "regdmn code: 0x%0x\n",
  631. regdmn);
  632. }
  633. }
  634. reg->regpair = ath_get_regpair(regdmn);
  635. if (!reg->regpair) {
  636. printk(KERN_DEBUG "ath: "
  637. "No regulatory domain pair found, cannot continue\n");
  638. return -EINVAL;
  639. }
  640. if (!country)
  641. country = ath_regd_find_country_by_rd(regdmn);
  642. if (country) {
  643. reg->alpha2[0] = country->isoName[0];
  644. reg->alpha2[1] = country->isoName[1];
  645. } else {
  646. reg->alpha2[0] = '0';
  647. reg->alpha2[1] = '0';
  648. }
  649. printk(KERN_DEBUG "ath: Country alpha2 being used: %c%c\n",
  650. reg->alpha2[0], reg->alpha2[1]);
  651. printk(KERN_DEBUG "ath: Regpair used: 0x%0x\n",
  652. reg->regpair->reg_domain);
  653. return 0;
  654. }
  655. int
  656. ath_regd_init(struct ath_regulatory *reg,
  657. struct wiphy *wiphy,
  658. void (*reg_notifier)(struct wiphy *wiphy,
  659. struct regulatory_request *request))
  660. {
  661. struct ath_common *common = container_of(reg, struct ath_common,
  662. regulatory);
  663. int r;
  664. r = __ath_regd_init(reg);
  665. if (r)
  666. return r;
  667. if (ath_is_world_regd(reg))
  668. memcpy(&common->reg_world_copy, reg,
  669. sizeof(struct ath_regulatory));
  670. ath_regd_init_wiphy(reg, wiphy, reg_notifier);
  671. return 0;
  672. }
  673. EXPORT_SYMBOL(ath_regd_init);
  674. u32 ath_regd_get_band_ctl(struct ath_regulatory *reg,
  675. enum nl80211_band band)
  676. {
  677. if (!reg->regpair ||
  678. (reg->country_code == CTRY_DEFAULT &&
  679. is_wwr_sku(ath_regd_get_eepromRD(reg)))) {
  680. return SD_NO_CTL;
  681. }
  682. if (ath_regd_get_eepromRD(reg) == CTRY_DEFAULT) {
  683. switch (reg->region) {
  684. case NL80211_DFS_FCC:
  685. return CTL_FCC;
  686. case NL80211_DFS_ETSI:
  687. return CTL_ETSI;
  688. case NL80211_DFS_JP:
  689. return CTL_MKK;
  690. default:
  691. break;
  692. }
  693. }
  694. switch (band) {
  695. case NL80211_BAND_2GHZ:
  696. return reg->regpair->reg_2ghz_ctl;
  697. case NL80211_BAND_5GHZ:
  698. return reg->regpair->reg_5ghz_ctl;
  699. default:
  700. return NO_CTL;
  701. }
  702. }
  703. EXPORT_SYMBOL(ath_regd_get_band_ctl);