regd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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 inline bool is_wwr_sku(u16 regd)
  102. {
  103. return ((regd & COUNTRY_ERD_FLAG) != COUNTRY_ERD_FLAG) &&
  104. (((regd & WORLD_SKU_MASK) == WORLD_SKU_PREFIX) ||
  105. (regd == WORLD));
  106. }
  107. static u16 ath_regd_get_eepromRD(struct ath_regulatory *reg)
  108. {
  109. return reg->current_rd & ~WORLDWIDE_ROAMING_FLAG;
  110. }
  111. bool ath_is_world_regd(struct ath_regulatory *reg)
  112. {
  113. return is_wwr_sku(ath_regd_get_eepromRD(reg));
  114. }
  115. EXPORT_SYMBOL(ath_is_world_regd);
  116. static const struct ieee80211_regdomain *ath_default_world_regdomain(void)
  117. {
  118. /* this is the most restrictive */
  119. return &ath_world_regdom_64;
  120. }
  121. static const struct
  122. ieee80211_regdomain *ath_world_regdomain(struct ath_regulatory *reg)
  123. {
  124. switch (reg->regpair->regDmnEnum) {
  125. case 0x60:
  126. case 0x61:
  127. case 0x62:
  128. return &ath_world_regdom_60_61_62;
  129. case 0x63:
  130. case 0x65:
  131. return &ath_world_regdom_63_65;
  132. case 0x64:
  133. return &ath_world_regdom_64;
  134. case 0x66:
  135. case 0x69:
  136. return &ath_world_regdom_66_69;
  137. case 0x67:
  138. case 0x68:
  139. case 0x6A:
  140. case 0x6C:
  141. return &ath_world_regdom_67_68_6A_6C;
  142. default:
  143. WARN_ON(1);
  144. return ath_default_world_regdomain();
  145. }
  146. }
  147. bool ath_is_49ghz_allowed(u16 regdomain)
  148. {
  149. /* possibly more */
  150. return regdomain == MKK9_MKKC;
  151. }
  152. EXPORT_SYMBOL(ath_is_49ghz_allowed);
  153. /* Frequency is one where radar detection is required */
  154. static bool ath_is_radar_freq(u16 center_freq)
  155. {
  156. return (center_freq >= 5260 && center_freq <= 5700);
  157. }
  158. /*
  159. * N.B: These exception rules do not apply radar freqs.
  160. *
  161. * - We enable adhoc (or beaconing) if allowed by 11d
  162. * - We enable active scan if the channel is allowed by 11d
  163. * - If no country IE has been processed and a we determine we have
  164. * received a beacon on a channel we can enable active scan and
  165. * adhoc (or beaconing).
  166. */
  167. static void
  168. ath_reg_apply_beaconing_flags(struct wiphy *wiphy,
  169. enum nl80211_reg_initiator initiator)
  170. {
  171. enum ieee80211_band band;
  172. struct ieee80211_supported_band *sband;
  173. const struct ieee80211_reg_rule *reg_rule;
  174. struct ieee80211_channel *ch;
  175. unsigned int i;
  176. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  177. if (!wiphy->bands[band])
  178. continue;
  179. sband = wiphy->bands[band];
  180. for (i = 0; i < sband->n_channels; i++) {
  181. ch = &sband->channels[i];
  182. if (ath_is_radar_freq(ch->center_freq) ||
  183. (ch->flags & IEEE80211_CHAN_RADAR))
  184. continue;
  185. if (initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  186. reg_rule = freq_reg_info(wiphy, ch->center_freq);
  187. if (IS_ERR(reg_rule))
  188. continue;
  189. /*
  190. * If 11d had a rule for this channel ensure
  191. * we enable adhoc/beaconing if it allows us to
  192. * use it. Note that we would have disabled it
  193. * by applying our static world regdomain by
  194. * default during init, prior to calling our
  195. * regulatory_hint().
  196. */
  197. if (!(reg_rule->flags & NL80211_RRF_NO_IR))
  198. ch->flags &= ~IEEE80211_CHAN_NO_IR;
  199. } else {
  200. if (ch->beacon_found)
  201. ch->flags &= ~IEEE80211_CHAN_NO_IR;
  202. }
  203. }
  204. }
  205. }
  206. /* Allows active scan scan on Ch 12 and 13 */
  207. static void
  208. ath_reg_apply_active_scan_flags(struct wiphy *wiphy,
  209. enum nl80211_reg_initiator initiator)
  210. {
  211. struct ieee80211_supported_band *sband;
  212. struct ieee80211_channel *ch;
  213. const struct ieee80211_reg_rule *reg_rule;
  214. sband = wiphy->bands[IEEE80211_BAND_2GHZ];
  215. if (!sband)
  216. return;
  217. /*
  218. * If no country IE has been received always enable passive scan
  219. * and no-ibss on these channels. This is only done for specific
  220. * regulatory SKUs.
  221. */
  222. if (initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE) {
  223. ch = &sband->channels[11]; /* CH 12 */
  224. ch->flags |= IEEE80211_CHAN_NO_IR;
  225. ch = &sband->channels[12]; /* CH 13 */
  226. ch->flags |= IEEE80211_CHAN_NO_IR;
  227. return;
  228. }
  229. /*
  230. * If a country IE has been received check its rule for this
  231. * channel first before enabling active scan. The passive scan
  232. * would have been enforced by the initial processing of our
  233. * custom regulatory domain.
  234. */
  235. ch = &sband->channels[11]; /* CH 12 */
  236. reg_rule = freq_reg_info(wiphy, ch->center_freq);
  237. if (!IS_ERR(reg_rule)) {
  238. if (!(reg_rule->flags & NL80211_RRF_NO_IR))
  239. if (ch->flags & IEEE80211_CHAN_NO_IR)
  240. ch->flags &= ~IEEE80211_CHAN_NO_IR;
  241. }
  242. ch = &sband->channels[12]; /* CH 13 */
  243. reg_rule = freq_reg_info(wiphy, ch->center_freq);
  244. if (!IS_ERR(reg_rule)) {
  245. if (!(reg_rule->flags & NL80211_RRF_NO_IR))
  246. if (ch->flags & IEEE80211_CHAN_NO_IR)
  247. ch->flags &= ~IEEE80211_CHAN_NO_IR;
  248. }
  249. }
  250. /* Always apply Radar/DFS rules on freq range 5260 MHz - 5700 MHz */
  251. static void ath_reg_apply_radar_flags(struct wiphy *wiphy)
  252. {
  253. struct ieee80211_supported_band *sband;
  254. struct ieee80211_channel *ch;
  255. unsigned int i;
  256. if (!wiphy->bands[IEEE80211_BAND_5GHZ])
  257. return;
  258. sband = wiphy->bands[IEEE80211_BAND_5GHZ];
  259. for (i = 0; i < sband->n_channels; i++) {
  260. ch = &sband->channels[i];
  261. if (!ath_is_radar_freq(ch->center_freq))
  262. continue;
  263. /* We always enable radar detection/DFS on this
  264. * frequency range. Additionally we also apply on
  265. * this frequency range:
  266. * - If STA mode does not yet have DFS supports disable
  267. * active scanning
  268. * - If adhoc mode does not support DFS yet then
  269. * disable adhoc in the frequency.
  270. * - If AP mode does not yet support radar detection/DFS
  271. * do not allow AP mode
  272. */
  273. if (!(ch->flags & IEEE80211_CHAN_DISABLED))
  274. ch->flags |= IEEE80211_CHAN_RADAR |
  275. IEEE80211_CHAN_NO_IR;
  276. }
  277. }
  278. static void ath_reg_apply_world_flags(struct wiphy *wiphy,
  279. enum nl80211_reg_initiator initiator,
  280. struct ath_regulatory *reg)
  281. {
  282. switch (reg->regpair->regDmnEnum) {
  283. case 0x60:
  284. case 0x63:
  285. case 0x66:
  286. case 0x67:
  287. case 0x6C:
  288. ath_reg_apply_beaconing_flags(wiphy, initiator);
  289. break;
  290. case 0x68:
  291. ath_reg_apply_beaconing_flags(wiphy, initiator);
  292. ath_reg_apply_active_scan_flags(wiphy, initiator);
  293. break;
  294. }
  295. }
  296. static u16 ath_regd_find_country_by_name(char *alpha2)
  297. {
  298. unsigned int i;
  299. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  300. if (!memcmp(allCountries[i].isoName, alpha2, 2))
  301. return allCountries[i].countryCode;
  302. }
  303. return -1;
  304. }
  305. static int __ath_reg_dyn_country(struct wiphy *wiphy,
  306. struct ath_regulatory *reg,
  307. struct regulatory_request *request)
  308. {
  309. u16 country_code;
  310. if (request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
  311. !ath_is_world_regd(reg))
  312. return -EINVAL;
  313. country_code = ath_regd_find_country_by_name(request->alpha2);
  314. if (country_code == (u16) -1)
  315. return -EINVAL;
  316. reg->current_rd = COUNTRY_ERD_FLAG;
  317. reg->current_rd |= country_code;
  318. __ath_regd_init(reg);
  319. ath_reg_apply_world_flags(wiphy, request->initiator, reg);
  320. return 0;
  321. }
  322. static void ath_reg_dyn_country(struct wiphy *wiphy,
  323. struct ath_regulatory *reg,
  324. struct regulatory_request *request)
  325. {
  326. if (__ath_reg_dyn_country(wiphy, reg, request))
  327. return;
  328. printk(KERN_DEBUG "ath: regdomain 0x%0x "
  329. "dynamically updated by %s\n",
  330. reg->current_rd,
  331. reg_initiator_name(request->initiator));
  332. }
  333. static bool dynamic_country_user_possible(struct ath_regulatory *reg)
  334. {
  335. if (config_enabled(CONFIG_ATH_REG_DYNAMIC_USER_CERT_TESTING))
  336. return true;
  337. switch (reg->country_code) {
  338. case CTRY_UNITED_STATES:
  339. case CTRY_JAPAN1:
  340. case CTRY_JAPAN2:
  341. case CTRY_JAPAN3:
  342. case CTRY_JAPAN4:
  343. case CTRY_JAPAN5:
  344. case CTRY_JAPAN6:
  345. case CTRY_JAPAN7:
  346. case CTRY_JAPAN8:
  347. case CTRY_JAPAN9:
  348. case CTRY_JAPAN10:
  349. case CTRY_JAPAN11:
  350. case CTRY_JAPAN12:
  351. case CTRY_JAPAN13:
  352. case CTRY_JAPAN14:
  353. case CTRY_JAPAN15:
  354. case CTRY_JAPAN16:
  355. case CTRY_JAPAN17:
  356. case CTRY_JAPAN18:
  357. case CTRY_JAPAN19:
  358. case CTRY_JAPAN20:
  359. case CTRY_JAPAN21:
  360. case CTRY_JAPAN22:
  361. case CTRY_JAPAN23:
  362. case CTRY_JAPAN24:
  363. case CTRY_JAPAN25:
  364. case CTRY_JAPAN26:
  365. case CTRY_JAPAN27:
  366. case CTRY_JAPAN28:
  367. case CTRY_JAPAN29:
  368. case CTRY_JAPAN30:
  369. case CTRY_JAPAN31:
  370. case CTRY_JAPAN32:
  371. case CTRY_JAPAN33:
  372. case CTRY_JAPAN34:
  373. case CTRY_JAPAN35:
  374. case CTRY_JAPAN36:
  375. case CTRY_JAPAN37:
  376. case CTRY_JAPAN38:
  377. case CTRY_JAPAN39:
  378. case CTRY_JAPAN40:
  379. case CTRY_JAPAN41:
  380. case CTRY_JAPAN42:
  381. case CTRY_JAPAN43:
  382. case CTRY_JAPAN44:
  383. case CTRY_JAPAN45:
  384. case CTRY_JAPAN46:
  385. case CTRY_JAPAN47:
  386. case CTRY_JAPAN48:
  387. case CTRY_JAPAN49:
  388. case CTRY_JAPAN50:
  389. case CTRY_JAPAN51:
  390. case CTRY_JAPAN52:
  391. case CTRY_JAPAN53:
  392. case CTRY_JAPAN54:
  393. case CTRY_JAPAN55:
  394. case CTRY_JAPAN56:
  395. case CTRY_JAPAN57:
  396. case CTRY_JAPAN58:
  397. case CTRY_JAPAN59:
  398. return false;
  399. }
  400. return true;
  401. }
  402. static void ath_reg_dyn_country_user(struct wiphy *wiphy,
  403. struct ath_regulatory *reg,
  404. struct regulatory_request *request)
  405. {
  406. if (!config_enabled(CONFIG_ATH_REG_DYNAMIC_USER_REG_HINTS))
  407. return;
  408. if (!dynamic_country_user_possible(reg))
  409. return;
  410. ath_reg_dyn_country(wiphy, reg, request);
  411. }
  412. void ath_reg_notifier_apply(struct wiphy *wiphy,
  413. struct regulatory_request *request,
  414. struct ath_regulatory *reg)
  415. {
  416. struct ath_common *common = container_of(reg, struct ath_common,
  417. regulatory);
  418. /* We always apply this */
  419. ath_reg_apply_radar_flags(wiphy);
  420. /*
  421. * This would happen when we have sent a custom regulatory request
  422. * a world regulatory domain and the scheduler hasn't yet processed
  423. * any pending requests in the queue.
  424. */
  425. if (!request)
  426. return;
  427. switch (request->initiator) {
  428. case NL80211_REGDOM_SET_BY_CORE:
  429. /*
  430. * If common->reg_world_copy is world roaming it means we *were*
  431. * world roaming... so we now have to restore that data.
  432. */
  433. if (!ath_is_world_regd(&common->reg_world_copy))
  434. break;
  435. memcpy(reg, &common->reg_world_copy,
  436. sizeof(struct ath_regulatory));
  437. break;
  438. case NL80211_REGDOM_SET_BY_DRIVER:
  439. break;
  440. case NL80211_REGDOM_SET_BY_USER:
  441. ath_reg_dyn_country_user(wiphy, reg, request);
  442. break;
  443. case NL80211_REGDOM_SET_BY_COUNTRY_IE:
  444. ath_reg_dyn_country(wiphy, reg, request);
  445. break;
  446. }
  447. }
  448. EXPORT_SYMBOL(ath_reg_notifier_apply);
  449. static bool ath_regd_is_eeprom_valid(struct ath_regulatory *reg)
  450. {
  451. u16 rd = ath_regd_get_eepromRD(reg);
  452. int i;
  453. if (rd & COUNTRY_ERD_FLAG) {
  454. /* EEPROM value is a country code */
  455. u16 cc = rd & ~COUNTRY_ERD_FLAG;
  456. printk(KERN_DEBUG
  457. "ath: EEPROM indicates we should expect "
  458. "a country code\n");
  459. for (i = 0; i < ARRAY_SIZE(allCountries); i++)
  460. if (allCountries[i].countryCode == cc)
  461. return true;
  462. } else {
  463. /* EEPROM value is a regpair value */
  464. if (rd != CTRY_DEFAULT)
  465. printk(KERN_DEBUG "ath: EEPROM indicates we "
  466. "should expect a direct regpair map\n");
  467. for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++)
  468. if (regDomainPairs[i].regDmnEnum == rd)
  469. return true;
  470. }
  471. printk(KERN_DEBUG
  472. "ath: invalid regulatory domain/country code 0x%x\n", rd);
  473. return false;
  474. }
  475. /* EEPROM country code to regpair mapping */
  476. static struct country_code_to_enum_rd*
  477. ath_regd_find_country(u16 countryCode)
  478. {
  479. int i;
  480. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  481. if (allCountries[i].countryCode == countryCode)
  482. return &allCountries[i];
  483. }
  484. return NULL;
  485. }
  486. /* EEPROM rd code to regpair mapping */
  487. static struct country_code_to_enum_rd*
  488. ath_regd_find_country_by_rd(int regdmn)
  489. {
  490. int i;
  491. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  492. if (allCountries[i].regDmnEnum == regdmn)
  493. return &allCountries[i];
  494. }
  495. return NULL;
  496. }
  497. /* Returns the map of the EEPROM set RD to a country code */
  498. static u16 ath_regd_get_default_country(u16 rd)
  499. {
  500. if (rd & COUNTRY_ERD_FLAG) {
  501. struct country_code_to_enum_rd *country = NULL;
  502. u16 cc = rd & ~COUNTRY_ERD_FLAG;
  503. country = ath_regd_find_country(cc);
  504. if (country != NULL)
  505. return cc;
  506. }
  507. return CTRY_DEFAULT;
  508. }
  509. static struct reg_dmn_pair_mapping*
  510. ath_get_regpair(int regdmn)
  511. {
  512. int i;
  513. if (regdmn == NO_ENUMRD)
  514. return NULL;
  515. for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++) {
  516. if (regDomainPairs[i].regDmnEnum == regdmn)
  517. return &regDomainPairs[i];
  518. }
  519. return NULL;
  520. }
  521. static int
  522. ath_regd_init_wiphy(struct ath_regulatory *reg,
  523. struct wiphy *wiphy,
  524. void (*reg_notifier)(struct wiphy *wiphy,
  525. struct regulatory_request *request))
  526. {
  527. const struct ieee80211_regdomain *regd;
  528. wiphy->reg_notifier = reg_notifier;
  529. wiphy->regulatory_flags |= REGULATORY_STRICT_REG;
  530. if (ath_is_world_regd(reg)) {
  531. /*
  532. * Anything applied here (prior to wiphy registration) gets
  533. * saved on the wiphy orig_* parameters
  534. */
  535. regd = ath_world_regdomain(reg);
  536. wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
  537. REGULATORY_COUNTRY_IE_FOLLOW_POWER;
  538. } else {
  539. /*
  540. * This gets applied in the case of the absence of CRDA,
  541. * it's our own custom world regulatory domain, similar to
  542. * cfg80211's but we enable passive scanning.
  543. */
  544. regd = ath_default_world_regdomain();
  545. }
  546. wiphy_apply_custom_regulatory(wiphy, regd);
  547. ath_reg_apply_radar_flags(wiphy);
  548. ath_reg_apply_world_flags(wiphy, NL80211_REGDOM_SET_BY_DRIVER, reg);
  549. return 0;
  550. }
  551. /*
  552. * Some users have reported their EEPROM programmed with
  553. * 0x8000 set, this is not a supported regulatory domain
  554. * but since we have more than one user with it we need
  555. * a solution for them. We default to 0x64, which is the
  556. * default Atheros world regulatory domain.
  557. */
  558. static void ath_regd_sanitize(struct ath_regulatory *reg)
  559. {
  560. if (reg->current_rd != COUNTRY_ERD_FLAG)
  561. return;
  562. printk(KERN_DEBUG "ath: EEPROM regdomain sanitized\n");
  563. reg->current_rd = 0x64;
  564. }
  565. static int __ath_regd_init(struct ath_regulatory *reg)
  566. {
  567. struct country_code_to_enum_rd *country = NULL;
  568. u16 regdmn;
  569. if (!reg)
  570. return -EINVAL;
  571. ath_regd_sanitize(reg);
  572. printk(KERN_DEBUG "ath: EEPROM regdomain: 0x%0x\n", reg->current_rd);
  573. if (!ath_regd_is_eeprom_valid(reg)) {
  574. pr_err("Invalid EEPROM contents\n");
  575. return -EINVAL;
  576. }
  577. regdmn = ath_regd_get_eepromRD(reg);
  578. reg->country_code = ath_regd_get_default_country(regdmn);
  579. if (reg->country_code == CTRY_DEFAULT &&
  580. regdmn == CTRY_DEFAULT) {
  581. printk(KERN_DEBUG "ath: EEPROM indicates default "
  582. "country code should be used\n");
  583. reg->country_code = CTRY_UNITED_STATES;
  584. }
  585. if (reg->country_code == CTRY_DEFAULT) {
  586. country = NULL;
  587. } else {
  588. printk(KERN_DEBUG "ath: doing EEPROM country->regdmn "
  589. "map search\n");
  590. country = ath_regd_find_country(reg->country_code);
  591. if (country == NULL) {
  592. printk(KERN_DEBUG
  593. "ath: no valid country maps found for "
  594. "country code: 0x%0x\n",
  595. reg->country_code);
  596. return -EINVAL;
  597. } else {
  598. regdmn = country->regDmnEnum;
  599. printk(KERN_DEBUG "ath: country maps to "
  600. "regdmn code: 0x%0x\n",
  601. regdmn);
  602. }
  603. }
  604. reg->regpair = ath_get_regpair(regdmn);
  605. if (!reg->regpair) {
  606. printk(KERN_DEBUG "ath: "
  607. "No regulatory domain pair found, cannot continue\n");
  608. return -EINVAL;
  609. }
  610. if (!country)
  611. country = ath_regd_find_country_by_rd(regdmn);
  612. if (country) {
  613. reg->alpha2[0] = country->isoName[0];
  614. reg->alpha2[1] = country->isoName[1];
  615. } else {
  616. reg->alpha2[0] = '0';
  617. reg->alpha2[1] = '0';
  618. }
  619. printk(KERN_DEBUG "ath: Country alpha2 being used: %c%c\n",
  620. reg->alpha2[0], reg->alpha2[1]);
  621. printk(KERN_DEBUG "ath: Regpair used: 0x%0x\n",
  622. reg->regpair->regDmnEnum);
  623. return 0;
  624. }
  625. int
  626. ath_regd_init(struct ath_regulatory *reg,
  627. struct wiphy *wiphy,
  628. void (*reg_notifier)(struct wiphy *wiphy,
  629. struct regulatory_request *request))
  630. {
  631. struct ath_common *common = container_of(reg, struct ath_common,
  632. regulatory);
  633. int r;
  634. r = __ath_regd_init(reg);
  635. if (r)
  636. return r;
  637. if (ath_is_world_regd(reg))
  638. memcpy(&common->reg_world_copy, reg,
  639. sizeof(struct ath_regulatory));
  640. ath_regd_init_wiphy(reg, wiphy, reg_notifier);
  641. return 0;
  642. }
  643. EXPORT_SYMBOL(ath_regd_init);
  644. u32 ath_regd_get_band_ctl(struct ath_regulatory *reg,
  645. enum ieee80211_band band)
  646. {
  647. if (!reg->regpair ||
  648. (reg->country_code == CTRY_DEFAULT &&
  649. is_wwr_sku(ath_regd_get_eepromRD(reg)))) {
  650. return SD_NO_CTL;
  651. }
  652. switch (band) {
  653. case IEEE80211_BAND_2GHZ:
  654. return reg->regpair->reg_2ghz_ctl;
  655. case IEEE80211_BAND_5GHZ:
  656. return reg->regpair->reg_5ghz_ctl;
  657. default:
  658. return NO_CTL;
  659. }
  660. }
  661. EXPORT_SYMBOL(ath_regd_get_band_ctl);