regd.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. /*
  2. * Copyright (c) 2008 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. #include <linux/kernel.h>
  17. #include <linux/slab.h>
  18. #include "core.h"
  19. #include "hw.h"
  20. #include "regd.h"
  21. #include "regd_common.h"
  22. static int ath9k_regd_chansort(const void *a, const void *b)
  23. {
  24. const struct ath9k_channel *ca = a;
  25. const struct ath9k_channel *cb = b;
  26. return (ca->channel == cb->channel) ?
  27. (ca->channelFlags & CHAN_FLAGS) -
  28. (cb->channelFlags & CHAN_FLAGS) : ca->channel - cb->channel;
  29. }
  30. static void
  31. ath9k_regd_sort(void *a, u32 n, u32 size, ath_hal_cmp_t *cmp)
  32. {
  33. u8 *aa = a;
  34. u8 *ai, *t;
  35. for (ai = aa + size; --n >= 1; ai += size)
  36. for (t = ai; t > aa; t -= size) {
  37. u8 *u = t - size;
  38. if (cmp(u, t) <= 0)
  39. break;
  40. swap_array(u, t, size);
  41. }
  42. }
  43. static u16 ath9k_regd_get_eepromRD(struct ath_hal *ah)
  44. {
  45. return ah->ah_currentRD & ~WORLDWIDE_ROAMING_FLAG;
  46. }
  47. static bool ath9k_regd_is_chan_bm_zero(u64 *bitmask)
  48. {
  49. int i;
  50. for (i = 0; i < BMLEN; i++) {
  51. if (bitmask[i] != 0)
  52. return false;
  53. }
  54. return true;
  55. }
  56. static bool ath9k_regd_is_eeprom_valid(struct ath_hal *ah)
  57. {
  58. u16 rd = ath9k_regd_get_eepromRD(ah);
  59. int i;
  60. if (rd & COUNTRY_ERD_FLAG) {
  61. u16 cc = rd & ~COUNTRY_ERD_FLAG;
  62. for (i = 0; i < ARRAY_SIZE(allCountries); i++)
  63. if (allCountries[i].countryCode == cc)
  64. return true;
  65. } else {
  66. for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++)
  67. if (regDomainPairs[i].regDmnEnum == rd)
  68. return true;
  69. }
  70. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  71. "invalid regulatory domain/country code 0x%x\n", rd);
  72. return false;
  73. }
  74. static bool ath9k_regd_is_fcc_midband_supported(struct ath_hal *ah)
  75. {
  76. u32 regcap;
  77. regcap = ah->ah_caps.reg_cap;
  78. if (regcap & AR_EEPROM_EEREGCAP_EN_FCC_MIDBAND)
  79. return true;
  80. else
  81. return false;
  82. }
  83. static bool ath9k_regd_is_ccode_valid(struct ath_hal *ah,
  84. u16 cc)
  85. {
  86. u16 rd;
  87. int i;
  88. if (cc == CTRY_DEFAULT)
  89. return true;
  90. if (cc == CTRY_DEBUG)
  91. return true;
  92. rd = ath9k_regd_get_eepromRD(ah);
  93. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY, "EEPROM regdomain 0x%x\n", rd);
  94. if (rd & COUNTRY_ERD_FLAG) {
  95. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  96. "EEPROM setting is country code %u\n",
  97. rd & ~COUNTRY_ERD_FLAG);
  98. return cc == (rd & ~COUNTRY_ERD_FLAG);
  99. }
  100. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  101. if (cc == allCountries[i].countryCode) {
  102. #ifdef AH_SUPPORT_11D
  103. if ((rd & WORLD_SKU_MASK) == WORLD_SKU_PREFIX)
  104. return true;
  105. #endif
  106. if (allCountries[i].regDmnEnum == rd ||
  107. rd == DEBUG_REG_DMN || rd == NO_ENUMRD)
  108. return true;
  109. }
  110. }
  111. return false;
  112. }
  113. static void
  114. ath9k_regd_get_wmodes_nreg(struct ath_hal *ah,
  115. struct country_code_to_enum_rd *country,
  116. struct regDomain *rd5GHz,
  117. unsigned long *modes_allowed)
  118. {
  119. bitmap_copy(modes_allowed, ah->ah_caps.wireless_modes, ATH9K_MODE_MAX);
  120. if (test_bit(ATH9K_MODE_11G, ah->ah_caps.wireless_modes) &&
  121. (!country->allow11g))
  122. clear_bit(ATH9K_MODE_11G, modes_allowed);
  123. if (test_bit(ATH9K_MODE_11A, ah->ah_caps.wireless_modes) &&
  124. (ath9k_regd_is_chan_bm_zero(rd5GHz->chan11a)))
  125. clear_bit(ATH9K_MODE_11A, modes_allowed);
  126. if (test_bit(ATH9K_MODE_11NG_HT20, ah->ah_caps.wireless_modes)
  127. && (!country->allow11ng20))
  128. clear_bit(ATH9K_MODE_11NG_HT20, modes_allowed);
  129. if (test_bit(ATH9K_MODE_11NA_HT20, ah->ah_caps.wireless_modes)
  130. && (!country->allow11na20))
  131. clear_bit(ATH9K_MODE_11NA_HT20, modes_allowed);
  132. if (test_bit(ATH9K_MODE_11NG_HT40PLUS, ah->ah_caps.wireless_modes) &&
  133. (!country->allow11ng40))
  134. clear_bit(ATH9K_MODE_11NG_HT40PLUS, modes_allowed);
  135. if (test_bit(ATH9K_MODE_11NG_HT40MINUS, ah->ah_caps.wireless_modes) &&
  136. (!country->allow11ng40))
  137. clear_bit(ATH9K_MODE_11NG_HT40MINUS, modes_allowed);
  138. if (test_bit(ATH9K_MODE_11NA_HT40PLUS, ah->ah_caps.wireless_modes) &&
  139. (!country->allow11na40))
  140. clear_bit(ATH9K_MODE_11NA_HT40PLUS, modes_allowed);
  141. if (test_bit(ATH9K_MODE_11NA_HT40MINUS, ah->ah_caps.wireless_modes) &&
  142. (!country->allow11na40))
  143. clear_bit(ATH9K_MODE_11NA_HT40MINUS, modes_allowed);
  144. }
  145. bool ath9k_regd_is_public_safety_sku(struct ath_hal *ah)
  146. {
  147. u16 rd;
  148. rd = ath9k_regd_get_eepromRD(ah);
  149. switch (rd) {
  150. case FCC4_FCCA:
  151. case (CTRY_UNITED_STATES_FCC49 | COUNTRY_ERD_FLAG):
  152. return true;
  153. case DEBUG_REG_DMN:
  154. case NO_ENUMRD:
  155. if (ah->ah_countryCode == CTRY_UNITED_STATES_FCC49)
  156. return true;
  157. break;
  158. }
  159. return false;
  160. }
  161. static struct country_code_to_enum_rd*
  162. ath9k_regd_find_country(u16 countryCode)
  163. {
  164. int i;
  165. for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
  166. if (allCountries[i].countryCode == countryCode)
  167. return &allCountries[i];
  168. }
  169. return NULL;
  170. }
  171. static u16 ath9k_regd_get_default_country(struct ath_hal *ah)
  172. {
  173. u16 rd;
  174. int i;
  175. rd = ath9k_regd_get_eepromRD(ah);
  176. if (rd & COUNTRY_ERD_FLAG) {
  177. struct country_code_to_enum_rd *country = NULL;
  178. u16 cc = rd & ~COUNTRY_ERD_FLAG;
  179. country = ath9k_regd_find_country(cc);
  180. if (country != NULL)
  181. return cc;
  182. }
  183. for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++)
  184. if (regDomainPairs[i].regDmnEnum == rd) {
  185. if (regDomainPairs[i].singleCC != 0)
  186. return regDomainPairs[i].singleCC;
  187. else
  188. i = ARRAY_SIZE(regDomainPairs);
  189. }
  190. return CTRY_DEFAULT;
  191. }
  192. static bool ath9k_regd_is_valid_reg_domain(int regDmn,
  193. struct regDomain *rd)
  194. {
  195. int i;
  196. for (i = 0; i < ARRAY_SIZE(regDomains); i++) {
  197. if (regDomains[i].regDmnEnum == regDmn) {
  198. if (rd != NULL) {
  199. memcpy(rd, &regDomains[i],
  200. sizeof(struct regDomain));
  201. }
  202. return true;
  203. }
  204. }
  205. return false;
  206. }
  207. static bool ath9k_regd_is_valid_reg_domainPair(int regDmnPair)
  208. {
  209. int i;
  210. if (regDmnPair == NO_ENUMRD)
  211. return false;
  212. for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++) {
  213. if (regDomainPairs[i].regDmnEnum == regDmnPair)
  214. return true;
  215. }
  216. return false;
  217. }
  218. static bool
  219. ath9k_regd_get_wmode_regdomain(struct ath_hal *ah, int regDmn,
  220. u16 channelFlag, struct regDomain *rd)
  221. {
  222. int i, found;
  223. u64 flags = NO_REQ;
  224. struct reg_dmn_pair_mapping *regPair = NULL;
  225. int regOrg;
  226. regOrg = regDmn;
  227. if (regDmn == CTRY_DEFAULT) {
  228. u16 rdnum;
  229. rdnum = ath9k_regd_get_eepromRD(ah);
  230. if (!(rdnum & COUNTRY_ERD_FLAG)) {
  231. if (ath9k_regd_is_valid_reg_domain(rdnum, NULL) ||
  232. ath9k_regd_is_valid_reg_domainPair(rdnum)) {
  233. regDmn = rdnum;
  234. }
  235. }
  236. }
  237. if ((regDmn & MULTI_DOMAIN_MASK) == 0) {
  238. for (i = 0, found = 0;
  239. (i < ARRAY_SIZE(regDomainPairs)) && (!found); i++) {
  240. if (regDomainPairs[i].regDmnEnum == regDmn) {
  241. regPair = &regDomainPairs[i];
  242. found = 1;
  243. }
  244. }
  245. if (!found) {
  246. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  247. "Failed to find reg domain pair %u\n", regDmn);
  248. return false;
  249. }
  250. if (!(channelFlag & CHANNEL_2GHZ)) {
  251. regDmn = regPair->regDmn5GHz;
  252. flags = regPair->flags5GHz;
  253. }
  254. if (channelFlag & CHANNEL_2GHZ) {
  255. regDmn = regPair->regDmn2GHz;
  256. flags = regPair->flags2GHz;
  257. }
  258. }
  259. found = ath9k_regd_is_valid_reg_domain(regDmn, rd);
  260. if (!found) {
  261. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  262. "Failed to find unitary reg domain %u\n", regDmn);
  263. return false;
  264. } else {
  265. rd->pscan &= regPair->pscanMask;
  266. if (((regOrg & MULTI_DOMAIN_MASK) == 0) &&
  267. (flags != NO_REQ)) {
  268. rd->flags = flags;
  269. }
  270. rd->flags &= (channelFlag & CHANNEL_2GHZ) ?
  271. REG_DOMAIN_2GHZ_MASK : REG_DOMAIN_5GHZ_MASK;
  272. return true;
  273. }
  274. }
  275. static bool ath9k_regd_is_bit_set(int bit, u64 *bitmask)
  276. {
  277. int byteOffset, bitnum;
  278. u64 val;
  279. byteOffset = bit / 64;
  280. bitnum = bit - byteOffset * 64;
  281. val = ((u64) 1) << bitnum;
  282. if (bitmask[byteOffset] & val)
  283. return true;
  284. else
  285. return false;
  286. }
  287. static void
  288. ath9k_regd_add_reg_classid(u8 *regclassids, u32 maxregids,
  289. u32 *nregids, u8 regclassid)
  290. {
  291. int i;
  292. if (regclassid == 0)
  293. return;
  294. for (i = 0; i < maxregids; i++) {
  295. if (regclassids[i] == regclassid)
  296. return;
  297. if (regclassids[i] == 0)
  298. break;
  299. }
  300. if (i == maxregids)
  301. return;
  302. else {
  303. regclassids[i] = regclassid;
  304. *nregids += 1;
  305. }
  306. return;
  307. }
  308. static bool
  309. ath9k_regd_get_eeprom_reg_ext_bits(struct ath_hal *ah,
  310. enum reg_ext_bitmap bit)
  311. {
  312. return (ah->ah_currentRDExt & (1 << bit)) ? true : false;
  313. }
  314. #ifdef ATH_NF_PER_CHAN
  315. static void ath9k_regd_init_rf_buffer(struct ath9k_channel *ichans,
  316. int nchans)
  317. {
  318. int i, j, next;
  319. for (next = 0; next < nchans; next++) {
  320. for (i = 0; i < NUM_NF_READINGS; i++) {
  321. ichans[next].nfCalHist[i].currIndex = 0;
  322. ichans[next].nfCalHist[i].privNF =
  323. AR_PHY_CCA_MAX_GOOD_VALUE;
  324. ichans[next].nfCalHist[i].invalidNFcount =
  325. AR_PHY_CCA_FILTERWINDOW_LENGTH;
  326. for (j = 0; j < ATH9K_NF_CAL_HIST_MAX; j++) {
  327. ichans[next].nfCalHist[i].nfCalBuffer[j] =
  328. AR_PHY_CCA_MAX_GOOD_VALUE;
  329. }
  330. }
  331. }
  332. }
  333. #endif
  334. static int ath9k_regd_is_chan_present(struct ath_hal *ah,
  335. u16 c)
  336. {
  337. int i;
  338. for (i = 0; i < 150; i++) {
  339. if (!ah->ah_channels[i].channel)
  340. return -1;
  341. else if (ah->ah_channels[i].channel == c)
  342. return i;
  343. }
  344. return -1;
  345. }
  346. static bool
  347. ath9k_regd_add_channel(struct ath_hal *ah,
  348. u16 c,
  349. u16 c_lo,
  350. u16 c_hi,
  351. u16 maxChan,
  352. u8 ctl,
  353. int pos,
  354. struct regDomain rd5GHz,
  355. struct RegDmnFreqBand *fband,
  356. struct regDomain *rd,
  357. const struct cmode *cm,
  358. struct ath9k_channel *ichans,
  359. bool enableExtendedChannels)
  360. {
  361. struct ath9k_channel *chan;
  362. int ret;
  363. u32 channelFlags = 0;
  364. u8 privFlags = 0;
  365. if (!(c_lo <= c && c <= c_hi)) {
  366. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  367. "c %u out of range [%u..%u]\n",
  368. c, c_lo, c_hi);
  369. return false;
  370. }
  371. if ((fband->channelBW == CHANNEL_HALF_BW) &&
  372. !(ah->ah_caps.hw_caps & ATH9K_HW_CAP_CHAN_HALFRATE)) {
  373. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  374. "Skipping %u half rate channel\n", c);
  375. return false;
  376. }
  377. if ((fband->channelBW == CHANNEL_QUARTER_BW) &&
  378. !(ah->ah_caps.hw_caps & ATH9K_HW_CAP_CHAN_QUARTERRATE)) {
  379. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  380. "Skipping %u quarter rate channel\n", c);
  381. return false;
  382. }
  383. if (((c + fband->channelSep) / 2) > (maxChan + HALF_MAXCHANBW)) {
  384. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  385. "c %u > maxChan %u\n", c, maxChan);
  386. return false;
  387. }
  388. if ((fband->usePassScan & IS_ECM_CHAN) && !enableExtendedChannels) {
  389. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  390. "Skipping ecm channel\n");
  391. return false;
  392. }
  393. if ((rd->flags & NO_HOSTAP) && (ah->ah_opmode == NL80211_IFTYPE_AP)) {
  394. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  395. "Skipping HOSTAP channel\n");
  396. return false;
  397. }
  398. if (IS_HT40_MODE(cm->mode) &&
  399. !(ath9k_regd_get_eeprom_reg_ext_bits(ah, REG_EXT_FCC_DFS_HT40)) &&
  400. (fband->useDfs) &&
  401. (rd->conformanceTestLimit != MKK)) {
  402. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  403. "Skipping HT40 channel (en_fcc_dfs_ht40 = 0)\n");
  404. return false;
  405. }
  406. if (IS_HT40_MODE(cm->mode) &&
  407. !(ath9k_regd_get_eeprom_reg_ext_bits(ah,
  408. REG_EXT_JAPAN_NONDFS_HT40)) &&
  409. !(fband->useDfs) && (rd->conformanceTestLimit == MKK)) {
  410. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  411. "Skipping HT40 channel (en_jap_ht40 = 0)\n");
  412. return false;
  413. }
  414. if (IS_HT40_MODE(cm->mode) &&
  415. !(ath9k_regd_get_eeprom_reg_ext_bits(ah, REG_EXT_JAPAN_DFS_HT40)) &&
  416. (fband->useDfs) &&
  417. (rd->conformanceTestLimit == MKK)) {
  418. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  419. "Skipping HT40 channel (en_jap_dfs_ht40 = 0)\n");
  420. return false;
  421. }
  422. /* Calculate channel flags */
  423. channelFlags = cm->flags;
  424. switch (fband->channelBW) {
  425. case CHANNEL_HALF_BW:
  426. channelFlags |= CHANNEL_HALF;
  427. break;
  428. case CHANNEL_QUARTER_BW:
  429. channelFlags |= CHANNEL_QUARTER;
  430. break;
  431. }
  432. if (fband->usePassScan & rd->pscan)
  433. channelFlags |= CHANNEL_PASSIVE;
  434. else
  435. channelFlags &= ~CHANNEL_PASSIVE;
  436. if (fband->useDfs & rd->dfsMask)
  437. privFlags = CHANNEL_DFS;
  438. else
  439. privFlags = 0;
  440. if (rd->flags & LIMIT_FRAME_4MS)
  441. privFlags |= CHANNEL_4MS_LIMIT;
  442. if (privFlags & CHANNEL_DFS)
  443. privFlags |= CHANNEL_DISALLOW_ADHOC;
  444. if (rd->flags & ADHOC_PER_11D)
  445. privFlags |= CHANNEL_PER_11D_ADHOC;
  446. if (channelFlags & CHANNEL_PASSIVE) {
  447. if ((c < 2412) || (c > 2462)) {
  448. if (rd5GHz.regDmnEnum == MKK1 ||
  449. rd5GHz.regDmnEnum == MKK2) {
  450. u32 regcap = ah->ah_caps.reg_cap;
  451. if (!(regcap &
  452. (AR_EEPROM_EEREGCAP_EN_KK_U1_EVEN |
  453. AR_EEPROM_EEREGCAP_EN_KK_U2 |
  454. AR_EEPROM_EEREGCAP_EN_KK_MIDBAND)) &&
  455. isUNII1OddChan(c)) {
  456. channelFlags &= ~CHANNEL_PASSIVE;
  457. } else {
  458. privFlags |= CHANNEL_DISALLOW_ADHOC;
  459. }
  460. } else {
  461. privFlags |= CHANNEL_DISALLOW_ADHOC;
  462. }
  463. }
  464. }
  465. if ((cm->mode == ATH9K_MODE_11A) ||
  466. (cm->mode == ATH9K_MODE_11NA_HT20) ||
  467. (cm->mode == ATH9K_MODE_11NA_HT40PLUS) ||
  468. (cm->mode == ATH9K_MODE_11NA_HT40MINUS)) {
  469. if (rd->flags & (ADHOC_NO_11A | DISALLOW_ADHOC_11A))
  470. privFlags |= CHANNEL_DISALLOW_ADHOC;
  471. }
  472. /* Fill in channel details */
  473. ret = ath9k_regd_is_chan_present(ah, c);
  474. if (ret == -1) {
  475. chan = &ah->ah_channels[pos];
  476. chan->channel = c;
  477. chan->maxRegTxPower = fband->powerDfs;
  478. chan->antennaMax = fband->antennaMax;
  479. chan->regDmnFlags = rd->flags;
  480. chan->maxTxPower = AR5416_MAX_RATE_POWER;
  481. chan->minTxPower = AR5416_MAX_RATE_POWER;
  482. chan->channelFlags = channelFlags;
  483. chan->privFlags = privFlags;
  484. } else {
  485. chan = &ah->ah_channels[ret];
  486. chan->channelFlags |= channelFlags;
  487. chan->privFlags |= privFlags;
  488. }
  489. /* Set CTLs */
  490. if ((cm->flags & CHANNEL_ALL) == CHANNEL_A)
  491. chan->conformanceTestLimit[0] = ctl;
  492. else if ((cm->flags & CHANNEL_ALL) == CHANNEL_B)
  493. chan->conformanceTestLimit[1] = ctl;
  494. else if ((cm->flags & CHANNEL_ALL) == CHANNEL_G)
  495. chan->conformanceTestLimit[2] = ctl;
  496. return (ret == -1) ? true : false;
  497. }
  498. static bool ath9k_regd_japan_check(struct ath_hal *ah,
  499. int b,
  500. struct regDomain *rd5GHz)
  501. {
  502. bool skipband = false;
  503. int i;
  504. u32 regcap;
  505. for (i = 0; i < ARRAY_SIZE(j_bandcheck); i++) {
  506. if (j_bandcheck[i].freqbandbit == b) {
  507. regcap = ah->ah_caps.reg_cap;
  508. if ((j_bandcheck[i].eepromflagtocheck & regcap) == 0) {
  509. skipband = true;
  510. } else if ((regcap & AR_EEPROM_EEREGCAP_EN_KK_U2) ||
  511. (regcap & AR_EEPROM_EEREGCAP_EN_KK_MIDBAND)) {
  512. rd5GHz->dfsMask |= DFS_MKK4;
  513. rd5GHz->pscan |= PSCAN_MKK3;
  514. }
  515. break;
  516. }
  517. }
  518. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  519. "Skipping %d freq band\n", j_bandcheck[i].freqbandbit);
  520. return skipband;
  521. }
  522. bool
  523. ath9k_regd_init_channels(struct ath_hal *ah,
  524. u32 maxchans,
  525. u32 *nchans, u8 *regclassids,
  526. u32 maxregids, u32 *nregids, u16 cc,
  527. bool enableOutdoor,
  528. bool enableExtendedChannels)
  529. {
  530. u16 maxChan = 7000;
  531. struct country_code_to_enum_rd *country = NULL;
  532. struct regDomain rd5GHz, rd2GHz;
  533. const struct cmode *cm;
  534. struct ath9k_channel *ichans = &ah->ah_channels[0];
  535. int next = 0, b;
  536. u8 ctl;
  537. int regdmn;
  538. u16 chanSep;
  539. unsigned long *modes_avail;
  540. DECLARE_BITMAP(modes_allowed, ATH9K_MODE_MAX);
  541. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY, "cc %u %s %s\n", cc,
  542. enableOutdoor ? "Enable outdoor" : "",
  543. enableExtendedChannels ? "Enable ecm" : "");
  544. if (!ath9k_regd_is_ccode_valid(ah, cc)) {
  545. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  546. "Invalid country code %d\n", cc);
  547. return false;
  548. }
  549. if (!ath9k_regd_is_eeprom_valid(ah)) {
  550. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  551. "Invalid EEPROM contents\n");
  552. return false;
  553. }
  554. ah->ah_countryCode = ath9k_regd_get_default_country(ah);
  555. if (ah->ah_countryCode == CTRY_DEFAULT) {
  556. ah->ah_countryCode = cc & COUNTRY_CODE_MASK;
  557. if ((ah->ah_countryCode == CTRY_DEFAULT) &&
  558. (ath9k_regd_get_eepromRD(ah) == CTRY_DEFAULT)) {
  559. ah->ah_countryCode = CTRY_UNITED_STATES;
  560. }
  561. }
  562. #ifdef AH_SUPPORT_11D
  563. if (ah->ah_countryCode == CTRY_DEFAULT) {
  564. regdmn = ath9k_regd_get_eepromRD(ah);
  565. country = NULL;
  566. } else {
  567. #endif
  568. country = ath9k_regd_find_country(ah->ah_countryCode);
  569. if (country == NULL) {
  570. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  571. "Country is NULL!!!!, cc= %d\n",
  572. ah->ah_countryCode);
  573. return false;
  574. } else {
  575. regdmn = country->regDmnEnum;
  576. #ifdef AH_SUPPORT_11D
  577. if (((ath9k_regd_get_eepromRD(ah) &
  578. WORLD_SKU_MASK) == WORLD_SKU_PREFIX) &&
  579. (cc == CTRY_UNITED_STATES)) {
  580. if (!isWwrSKU_NoMidband(ah)
  581. && ath9k_regd_is_fcc_midband_supported(ah))
  582. regdmn = FCC3_FCCA;
  583. else
  584. regdmn = FCC1_FCCA;
  585. }
  586. #endif
  587. }
  588. #ifdef AH_SUPPORT_11D
  589. }
  590. #endif
  591. if (!ath9k_regd_get_wmode_regdomain(ah,
  592. regdmn,
  593. ~CHANNEL_2GHZ,
  594. &rd5GHz)) {
  595. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  596. "Couldn't find unitary "
  597. "5GHz reg domain for country %u\n",
  598. ah->ah_countryCode);
  599. return false;
  600. }
  601. if (!ath9k_regd_get_wmode_regdomain(ah,
  602. regdmn,
  603. CHANNEL_2GHZ,
  604. &rd2GHz)) {
  605. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  606. "Couldn't find unitary 2GHz "
  607. "reg domain for country %u\n",
  608. ah->ah_countryCode);
  609. return false;
  610. }
  611. if (!isWwrSKU(ah) && ((rd5GHz.regDmnEnum == FCC1) ||
  612. (rd5GHz.regDmnEnum == FCC2))) {
  613. if (ath9k_regd_is_fcc_midband_supported(ah)) {
  614. if (!ath9k_regd_get_wmode_regdomain(ah,
  615. FCC3_FCCA,
  616. ~CHANNEL_2GHZ,
  617. &rd5GHz)) {
  618. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  619. "Couldn't find unitary 5GHz "
  620. "reg domain for country %u\n",
  621. ah->ah_countryCode);
  622. return false;
  623. }
  624. }
  625. }
  626. if (country == NULL) {
  627. modes_avail = ah->ah_caps.wireless_modes;
  628. } else {
  629. ath9k_regd_get_wmodes_nreg(ah, country, &rd5GHz, modes_allowed);
  630. modes_avail = modes_allowed;
  631. if (!enableOutdoor)
  632. maxChan = country->outdoorChanStart;
  633. }
  634. next = 0;
  635. if (maxchans > ARRAY_SIZE(ah->ah_channels))
  636. maxchans = ARRAY_SIZE(ah->ah_channels);
  637. for (cm = modes; cm < &modes[ARRAY_SIZE(modes)]; cm++) {
  638. u16 c, c_hi, c_lo;
  639. u64 *channelBM = NULL;
  640. struct regDomain *rd = NULL;
  641. struct RegDmnFreqBand *fband = NULL, *freqs;
  642. int8_t low_adj = 0, hi_adj = 0;
  643. if (!test_bit(cm->mode, modes_avail)) {
  644. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  645. "!avail mode %d flags 0x%x\n",
  646. cm->mode, cm->flags);
  647. continue;
  648. }
  649. if (!ath9k_get_channel_edges(ah, cm->flags, &c_lo, &c_hi)) {
  650. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  651. "channels 0x%x not supported "
  652. "by hardware\n", cm->flags);
  653. continue;
  654. }
  655. switch (cm->mode) {
  656. case ATH9K_MODE_11A:
  657. case ATH9K_MODE_11NA_HT20:
  658. case ATH9K_MODE_11NA_HT40PLUS:
  659. case ATH9K_MODE_11NA_HT40MINUS:
  660. rd = &rd5GHz;
  661. channelBM = rd->chan11a;
  662. freqs = &regDmn5GhzFreq[0];
  663. ctl = rd->conformanceTestLimit;
  664. break;
  665. case ATH9K_MODE_11B:
  666. rd = &rd2GHz;
  667. channelBM = rd->chan11b;
  668. freqs = &regDmn2GhzFreq[0];
  669. ctl = rd->conformanceTestLimit | CTL_11B;
  670. break;
  671. case ATH9K_MODE_11G:
  672. case ATH9K_MODE_11NG_HT20:
  673. case ATH9K_MODE_11NG_HT40PLUS:
  674. case ATH9K_MODE_11NG_HT40MINUS:
  675. rd = &rd2GHz;
  676. channelBM = rd->chan11g;
  677. freqs = &regDmn2Ghz11gFreq[0];
  678. ctl = rd->conformanceTestLimit | CTL_11G;
  679. break;
  680. default:
  681. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  682. "Unknown HAL mode 0x%x\n", cm->mode);
  683. continue;
  684. }
  685. if (ath9k_regd_is_chan_bm_zero(channelBM))
  686. continue;
  687. if ((cm->mode == ATH9K_MODE_11NA_HT40PLUS) ||
  688. (cm->mode == ATH9K_MODE_11NG_HT40PLUS)) {
  689. hi_adj = -20;
  690. }
  691. if ((cm->mode == ATH9K_MODE_11NA_HT40MINUS) ||
  692. (cm->mode == ATH9K_MODE_11NG_HT40MINUS)) {
  693. low_adj = 20;
  694. }
  695. /* XXX: Add a helper here instead */
  696. for (b = 0; b < 64 * BMLEN; b++) {
  697. if (ath9k_regd_is_bit_set(b, channelBM)) {
  698. fband = &freqs[b];
  699. if (rd5GHz.regDmnEnum == MKK1
  700. || rd5GHz.regDmnEnum == MKK2) {
  701. if (ath9k_regd_japan_check(ah,
  702. b,
  703. &rd5GHz))
  704. continue;
  705. }
  706. ath9k_regd_add_reg_classid(regclassids,
  707. maxregids,
  708. nregids,
  709. fband->
  710. regClassId);
  711. if (IS_HT40_MODE(cm->mode) && (rd == &rd5GHz)) {
  712. chanSep = 40;
  713. if (fband->lowChannel == 5280)
  714. low_adj += 20;
  715. if (fband->lowChannel == 5170)
  716. continue;
  717. } else
  718. chanSep = fband->channelSep;
  719. for (c = fband->lowChannel + low_adj;
  720. ((c <= (fband->highChannel + hi_adj)) &&
  721. (c >= (fband->lowChannel + low_adj)));
  722. c += chanSep) {
  723. if (next >= maxchans) {
  724. DPRINTF(ah->ah_sc,
  725. ATH_DBG_REGULATORY,
  726. "too many channels "
  727. "for channel table\n");
  728. goto done;
  729. }
  730. if (ath9k_regd_add_channel(ah,
  731. c, c_lo, c_hi,
  732. maxChan, ctl,
  733. next,
  734. rd5GHz,
  735. fband, rd, cm,
  736. ichans,
  737. enableExtendedChannels))
  738. next++;
  739. }
  740. if (IS_HT40_MODE(cm->mode) &&
  741. (fband->lowChannel == 5280)) {
  742. low_adj -= 20;
  743. }
  744. }
  745. }
  746. }
  747. done:
  748. if (next != 0) {
  749. int i;
  750. if (next > ARRAY_SIZE(ah->ah_channels)) {
  751. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  752. "too many channels %u; truncating to %u\n",
  753. next, (int) ARRAY_SIZE(ah->ah_channels));
  754. next = ARRAY_SIZE(ah->ah_channels);
  755. }
  756. #ifdef ATH_NF_PER_CHAN
  757. ath9k_regd_init_rf_buffer(ichans, next);
  758. #endif
  759. ath9k_regd_sort(ichans, next,
  760. sizeof(struct ath9k_channel),
  761. ath9k_regd_chansort);
  762. ah->ah_nchan = next;
  763. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY, "Channel list:\n");
  764. for (i = 0; i < next; i++) {
  765. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  766. "chan: %d flags: 0x%x\n",
  767. ah->ah_channels[i].channel,
  768. ah->ah_channels[i].channelFlags);
  769. }
  770. }
  771. *nchans = next;
  772. ah->ah_countryCode = ah->ah_countryCode;
  773. ah->ah_currentRDInUse = regdmn;
  774. ah->ah_currentRD5G = rd5GHz.regDmnEnum;
  775. ah->ah_currentRD2G = rd2GHz.regDmnEnum;
  776. if (country == NULL) {
  777. ah->ah_iso[0] = 0;
  778. ah->ah_iso[1] = 0;
  779. } else {
  780. ah->ah_iso[0] = country->isoName[0];
  781. ah->ah_iso[1] = country->isoName[1];
  782. }
  783. return next != 0;
  784. }
  785. struct ath9k_channel*
  786. ath9k_regd_check_channel(struct ath_hal *ah,
  787. const struct ath9k_channel *c)
  788. {
  789. struct ath9k_channel *base, *cc;
  790. int flags = c->channelFlags & CHAN_FLAGS;
  791. int n, lim;
  792. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  793. "channel %u/0x%x (0x%x) requested\n",
  794. c->channel, c->channelFlags, flags);
  795. cc = ah->ah_curchan;
  796. if (cc != NULL && cc->channel == c->channel &&
  797. (cc->channelFlags & CHAN_FLAGS) == flags) {
  798. if ((cc->privFlags & CHANNEL_INTERFERENCE) &&
  799. (cc->privFlags & CHANNEL_DFS))
  800. return NULL;
  801. else
  802. return cc;
  803. }
  804. base = ah->ah_channels;
  805. n = ah->ah_nchan;
  806. for (lim = n; lim != 0; lim >>= 1) {
  807. int d;
  808. cc = &base[lim >> 1];
  809. d = c->channel - cc->channel;
  810. if (d == 0) {
  811. if ((cc->channelFlags & CHAN_FLAGS) == flags) {
  812. if ((cc->privFlags & CHANNEL_INTERFERENCE) &&
  813. (cc->privFlags & CHANNEL_DFS))
  814. return NULL;
  815. else
  816. return cc;
  817. }
  818. d = flags - (cc->channelFlags & CHAN_FLAGS);
  819. }
  820. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY,
  821. "channel %u/0x%x d %d\n",
  822. cc->channel, cc->channelFlags, d);
  823. if (d > 0) {
  824. base = cc + 1;
  825. lim--;
  826. }
  827. }
  828. DPRINTF(ah->ah_sc, ATH_DBG_REGULATORY, "no match for %u/0x%x\n",
  829. c->channel, c->channelFlags);
  830. return NULL;
  831. }
  832. u32
  833. ath9k_regd_get_antenna_allowed(struct ath_hal *ah,
  834. struct ath9k_channel *chan)
  835. {
  836. struct ath9k_channel *ichan = NULL;
  837. ichan = ath9k_regd_check_channel(ah, chan);
  838. if (!ichan)
  839. return 0;
  840. return ichan->antennaMax;
  841. }
  842. u32 ath9k_regd_get_ctl(struct ath_hal *ah, struct ath9k_channel *chan)
  843. {
  844. u32 ctl = NO_CTL;
  845. struct ath9k_channel *ichan;
  846. if (ah->ah_countryCode == CTRY_DEFAULT && isWwrSKU(ah)) {
  847. if (IS_CHAN_B(chan))
  848. ctl = SD_NO_CTL | CTL_11B;
  849. else if (IS_CHAN_G(chan))
  850. ctl = SD_NO_CTL | CTL_11G;
  851. else
  852. ctl = SD_NO_CTL | CTL_11A;
  853. } else {
  854. ichan = ath9k_regd_check_channel(ah, chan);
  855. if (ichan != NULL) {
  856. /* FIXME */
  857. if (IS_CHAN_A(ichan))
  858. ctl = ichan->conformanceTestLimit[0];
  859. else if (IS_CHAN_B(ichan))
  860. ctl = ichan->conformanceTestLimit[1];
  861. else if (IS_CHAN_G(ichan))
  862. ctl = ichan->conformanceTestLimit[2];
  863. if (IS_CHAN_G(chan) && (ctl & 0xf) == CTL_11B)
  864. ctl = (ctl & ~0xf) | CTL_11G;
  865. }
  866. }
  867. return ctl;
  868. }
  869. void ath9k_regd_get_current_country(struct ath_hal *ah,
  870. struct ath9k_country_entry *ctry)
  871. {
  872. u16 rd = ath9k_regd_get_eepromRD(ah);
  873. ctry->isMultidomain = false;
  874. if (rd == CTRY_DEFAULT)
  875. ctry->isMultidomain = true;
  876. else if (!(rd & COUNTRY_ERD_FLAG))
  877. ctry->isMultidomain = isWwrSKU(ah);
  878. ctry->countryCode = ah->ah_countryCode;
  879. ctry->regDmnEnum = ah->ah_currentRD;
  880. ctry->regDmn5G = ah->ah_currentRD5G;
  881. ctry->regDmn2G = ah->ah_currentRD2G;
  882. ctry->iso[0] = ah->ah_iso[0];
  883. ctry->iso[1] = ah->ah_iso[1];
  884. ctry->iso[2] = ah->ah_iso[2];
  885. }