regd.c 19.5 KB
Newer Older
1
/*
2
 * Copyright (c) 2008-2009 Atheros Communications Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

17 18
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

19
#include <linux/kernel.h>
20
#include <linux/export.h>
21 22 23
#include <net/cfg80211.h>
#include <net/mac80211.h>
#include "regd.h"
24 25
#include "regd_common.h"

26 27
static int __ath_regd_init(struct ath_regulatory *reg);

28 29 30 31 32 33
/*
 * This is a set of common rules used by our world regulatory domains.
 * We have 12 world regulatory domains. To save space we consolidate
 * the regulatory domains in 5 structures by frequency and change
 * the flags on our reg_notifier() on a case by case basis.
 */
34

35 36 37 38 39
/* Only these channels all allow active scan on all world regulatory domains */
#define ATH9K_2GHZ_CH01_11	REG_RULE(2412-10, 2462+10, 40, 0, 20, 0)

/* We enable active scan on these a case by case basis by regulatory domain */
#define ATH9K_2GHZ_CH12_13	REG_RULE(2467-10, 2472+10, 40, 0, 20,\
40
					 NL80211_RRF_NO_IR)
41
#define ATH9K_2GHZ_CH14		REG_RULE(2484-10, 2484+10, 40, 0, 20,\
42 43
					 NL80211_RRF_NO_IR | \
					 NL80211_RRF_NO_OFDM)
44 45

/* We allow IBSS on these on a case by case basis by regulatory domain */
46
#define ATH9K_5GHZ_5150_5350	REG_RULE(5150-10, 5350+10, 80, 0, 30,\
47
					 NL80211_RRF_NO_IR)
48
#define ATH9K_5GHZ_5470_5850	REG_RULE(5470-10, 5850+10, 80, 0, 30,\
49
					 NL80211_RRF_NO_IR)
50
#define ATH9K_5GHZ_5725_5850	REG_RULE(5725-10, 5850+10, 80, 0, 30,\
51
					 NL80211_RRF_NO_IR)
52 53 54 55 56 57 58

#define ATH9K_2GHZ_ALL		ATH9K_2GHZ_CH01_11, \
				ATH9K_2GHZ_CH12_13, \
				ATH9K_2GHZ_CH14

#define ATH9K_5GHZ_ALL		ATH9K_5GHZ_5150_5350, \
				ATH9K_5GHZ_5470_5850
59

60 61 62 63 64 65
/* This one skips what we call "mid band" */
#define ATH9K_5GHZ_NO_MIDBAND	ATH9K_5GHZ_5150_5350, \
				ATH9K_5GHZ_5725_5850

/* Can be used for:
 * 0x60, 0x61, 0x62 */
66
static const struct ieee80211_regdomain ath_world_regdom_60_61_62 = {
67 68 69 70 71 72 73 74 75
	.n_reg_rules = 5,
	.alpha2 =  "99",
	.reg_rules = {
		ATH9K_2GHZ_ALL,
		ATH9K_5GHZ_ALL,
	}
};

/* Can be used by 0x63 and 0x65 */
76
static const struct ieee80211_regdomain ath_world_regdom_63_65 = {
77 78 79 80 81 82 83 84 85 86
	.n_reg_rules = 4,
	.alpha2 =  "99",
	.reg_rules = {
		ATH9K_2GHZ_CH01_11,
		ATH9K_2GHZ_CH12_13,
		ATH9K_5GHZ_NO_MIDBAND,
	}
};

/* Can be used by 0x64 only */
87
static const struct ieee80211_regdomain ath_world_regdom_64 = {
88 89 90 91 92 93 94 95 96
	.n_reg_rules = 3,
	.alpha2 =  "99",
	.reg_rules = {
		ATH9K_2GHZ_CH01_11,
		ATH9K_5GHZ_NO_MIDBAND,
	}
};

/* Can be used by 0x66 and 0x69 */
97
static const struct ieee80211_regdomain ath_world_regdom_66_69 = {
98 99 100 101 102 103 104 105
	.n_reg_rules = 3,
	.alpha2 =  "99",
	.reg_rules = {
		ATH9K_2GHZ_CH01_11,
		ATH9K_5GHZ_ALL,
	}
};

106 107
/* Can be used by 0x67, 0x68, 0x6A and 0x6C */
static const struct ieee80211_regdomain ath_world_regdom_67_68_6A_6C = {
108 109 110 111 112 113 114 115
	.n_reg_rules = 4,
	.alpha2 =  "99",
	.reg_rules = {
		ATH9K_2GHZ_CH01_11,
		ATH9K_2GHZ_CH12_13,
		ATH9K_5GHZ_ALL,
	}
};
116

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
static bool dynamic_country_user_possible(struct ath_regulatory *reg)
{
	if (config_enabled(CONFIG_ATH_REG_DYNAMIC_USER_CERT_TESTING))
		return true;

	switch (reg->country_code) {
	case CTRY_UNITED_STATES:
	case CTRY_JAPAN1:
	case CTRY_JAPAN2:
	case CTRY_JAPAN3:
	case CTRY_JAPAN4:
	case CTRY_JAPAN5:
	case CTRY_JAPAN6:
	case CTRY_JAPAN7:
	case CTRY_JAPAN8:
	case CTRY_JAPAN9:
	case CTRY_JAPAN10:
	case CTRY_JAPAN11:
	case CTRY_JAPAN12:
	case CTRY_JAPAN13:
	case CTRY_JAPAN14:
	case CTRY_JAPAN15:
	case CTRY_JAPAN16:
	case CTRY_JAPAN17:
	case CTRY_JAPAN18:
	case CTRY_JAPAN19:
	case CTRY_JAPAN20:
	case CTRY_JAPAN21:
	case CTRY_JAPAN22:
	case CTRY_JAPAN23:
	case CTRY_JAPAN24:
	case CTRY_JAPAN25:
	case CTRY_JAPAN26:
	case CTRY_JAPAN27:
	case CTRY_JAPAN28:
	case CTRY_JAPAN29:
	case CTRY_JAPAN30:
	case CTRY_JAPAN31:
	case CTRY_JAPAN32:
	case CTRY_JAPAN33:
	case CTRY_JAPAN34:
	case CTRY_JAPAN35:
	case CTRY_JAPAN36:
	case CTRY_JAPAN37:
	case CTRY_JAPAN38:
	case CTRY_JAPAN39:
	case CTRY_JAPAN40:
	case CTRY_JAPAN41:
	case CTRY_JAPAN42:
	case CTRY_JAPAN43:
	case CTRY_JAPAN44:
	case CTRY_JAPAN45:
	case CTRY_JAPAN46:
	case CTRY_JAPAN47:
	case CTRY_JAPAN48:
	case CTRY_JAPAN49:
	case CTRY_JAPAN50:
	case CTRY_JAPAN51:
	case CTRY_JAPAN52:
	case CTRY_JAPAN53:
	case CTRY_JAPAN54:
	case CTRY_JAPAN55:
	case CTRY_JAPAN56:
	case CTRY_JAPAN57:
	case CTRY_JAPAN58:
	case CTRY_JAPAN59:
		return false;
	}

	return true;
}

189 190 191 192 193 194 195 196 197
static bool ath_reg_dyn_country_user_allow(struct ath_regulatory *reg)
{
	if (!config_enabled(CONFIG_ATH_REG_DYNAMIC_USER_REG_HINTS))
		return false;
	if (!dynamic_country_user_possible(reg))
		return false;
	return true;
}

198 199
static inline bool is_wwr_sku(u16 regd)
{
200 201 202
	return ((regd & COUNTRY_ERD_FLAG) != COUNTRY_ERD_FLAG) &&
		(((regd & WORLD_SKU_MASK) == WORLD_SKU_PREFIX) ||
		(regd == WORLD));
203 204
}

205
static u16 ath_regd_get_eepromRD(struct ath_regulatory *reg)
206
{
207
	return reg->current_rd & ~WORLDWIDE_ROAMING_FLAG;
208 209
}

210
bool ath_is_world_regd(struct ath_regulatory *reg)
211
{
212
	return is_wwr_sku(ath_regd_get_eepromRD(reg));
213
}
214
EXPORT_SYMBOL(ath_is_world_regd);
215

216
static const struct ieee80211_regdomain *ath_default_world_regdomain(void)
217
{
218
	/* this is the most restrictive */
219
	return &ath_world_regdom_64;
220
}
221

222
static const struct
223
ieee80211_regdomain *ath_world_regdomain(struct ath_regulatory *reg)
224
{
225
	switch (reg->regpair->regDmnEnum) {
226 227 228
	case 0x60:
	case 0x61:
	case 0x62:
229
		return &ath_world_regdom_60_61_62;
230 231
	case 0x63:
	case 0x65:
232
		return &ath_world_regdom_63_65;
233
	case 0x64:
234
		return &ath_world_regdom_64;
235 236
	case 0x66:
	case 0x69:
237
		return &ath_world_regdom_66_69;
238 239 240
	case 0x67:
	case 0x68:
	case 0x6A:
241 242
	case 0x6C:
		return &ath_world_regdom_67_68_6A_6C;
243 244
	default:
		WARN_ON(1);
245
		return ath_default_world_regdomain();
246 247 248
	}
}

249 250 251 252 253 254 255
bool ath_is_49ghz_allowed(u16 regdomain)
{
	/* possibly more */
	return regdomain == MKK9_MKKC;
}
EXPORT_SYMBOL(ath_is_49ghz_allowed);

256
/* Frequency is one where radar detection is required */
257
static bool ath_is_radar_freq(u16 center_freq)
258 259 260 261
{
	return (center_freq >= 5260 && center_freq <= 5700);
}

262 263 264 265 266
static void ath_force_clear_no_ir_chan(struct wiphy *wiphy,
				       struct ieee80211_channel *ch)
{
	const struct ieee80211_reg_rule *reg_rule;

267
	reg_rule = freq_reg_info(wiphy, MHZ_TO_KHZ(ch->center_freq));
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
	if (IS_ERR(reg_rule))
		return;

	if (!(reg_rule->flags & NL80211_RRF_NO_IR))
		if (ch->flags & IEEE80211_CHAN_NO_IR)
			ch->flags &= ~IEEE80211_CHAN_NO_IR;
}

static void ath_force_clear_no_ir_freq(struct wiphy *wiphy, u16 center_freq)
{
	struct ieee80211_channel *ch;

	ch = ieee80211_get_channel(wiphy, center_freq);
	if (!ch)
		return;

	ath_force_clear_no_ir_chan(wiphy, ch);
}

static void ath_force_no_ir_chan(struct ieee80211_channel *ch)
{
	ch->flags |= IEEE80211_CHAN_NO_IR;
}

static void ath_force_no_ir_freq(struct wiphy *wiphy, u16 center_freq)
{
	struct ieee80211_channel *ch;

	ch = ieee80211_get_channel(wiphy, center_freq);
	if (!ch)
		return;

	ath_force_no_ir_chan(ch);
}

303 304
static void
__ath_reg_apply_beaconing_flags(struct wiphy *wiphy,
305
				struct ath_regulatory *reg,
306 307 308 309 310 311 312 313 314 315 316
				enum nl80211_reg_initiator initiator,
				struct ieee80211_channel *ch)
{
	if (ath_is_radar_freq(ch->center_freq) ||
	    (ch->flags & IEEE80211_CHAN_RADAR))
		return;

	switch (initiator) {
	case NL80211_REGDOM_SET_BY_COUNTRY_IE:
		ath_force_clear_no_ir_chan(wiphy, ch);
		break;
317 318 319 320
	case NL80211_REGDOM_SET_BY_USER:
		if (ath_reg_dyn_country_user_allow(reg))
			ath_force_clear_no_ir_chan(wiphy, ch);
		break;
321 322 323 324 325 326
	default:
		if (ch->beacon_found)
			ch->flags &= ~IEEE80211_CHAN_NO_IR;
	}
}

327
/*
328
 * These exception rules do not apply radar frequencies.
329
 *
330
 * - We enable initiating radiation if the country IE says its fine:
331
 * - If no country IE has been processed and a we determine we have
332
 *   received a beacon on a channel we can enable initiating radiation.
333
 */
L
Luis R. Rodriguez 已提交
334 335
static void
ath_reg_apply_beaconing_flags(struct wiphy *wiphy,
336
			      struct ath_regulatory *reg,
L
Luis R. Rodriguez 已提交
337
			      enum nl80211_reg_initiator initiator)
338
{
339
	enum ieee80211_band band;
340 341 342 343
	struct ieee80211_supported_band *sband;
	struct ieee80211_channel *ch;
	unsigned int i;

344 345
	for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
		if (!wiphy->bands[band])
346
			continue;
347 348 349
		sband = wiphy->bands[band];
		for (i = 0; i < sband->n_channels; i++) {
			ch = &sband->channels[i];
350 351
			__ath_reg_apply_beaconing_flags(wiphy, reg,
							initiator, ch);
352
		}
353
	}
354 355
}

356
/**
357
 * ath_reg_apply_ir_flags()
358 359 360 361 362 363 364 365 366 367 368 369
 * @wiphy: the wiphy to use
 * @initiator: the regulatory hint initiator
 *
 * If no country IE has been received always enable passive scan
 * and no-ibss on these channels. This is only done for specific
 * regulatory SKUs.
 *
 * If a country IE has been received check its rule for this
 * channel first before enabling active scan. The passive scan
 * would have been enforced by the initial processing of our
 * custom regulatory domain.
 */
L
Luis R. Rodriguez 已提交
370
static void
371
ath_reg_apply_ir_flags(struct wiphy *wiphy,
372 373
		       struct ath_regulatory *reg,
		       enum nl80211_reg_initiator initiator)
374
{
375 376 377
	struct ieee80211_supported_band *sband;

	sband = wiphy->bands[IEEE80211_BAND_2GHZ];
378 379
	if (!sband)
		return;
380

381 382 383 384 385
	switch(initiator) {
	case NL80211_REGDOM_SET_BY_COUNTRY_IE:
		ath_force_clear_no_ir_freq(wiphy, 2467);
		ath_force_clear_no_ir_freq(wiphy, 2472);
		break;
386 387 388 389 390 391
	case NL80211_REGDOM_SET_BY_USER:
		if (!ath_reg_dyn_country_user_allow(reg))
			break;
		ath_force_clear_no_ir_freq(wiphy, 2467);
		ath_force_clear_no_ir_freq(wiphy, 2472);
		break;
392 393 394
	default:
		ath_force_no_ir_freq(wiphy, 2467);
		ath_force_no_ir_freq(wiphy, 2472);
395 396 397
	}
}

398
/* Always apply Radar/DFS rules on freq range 5260 MHz - 5700 MHz */
399
static void ath_reg_apply_radar_flags(struct wiphy *wiphy)
400
{
401 402 403
	struct ieee80211_supported_band *sband;
	struct ieee80211_channel *ch;
	unsigned int i;
404

405 406
	if (!wiphy->bands[IEEE80211_BAND_5GHZ])
		return;
407

408
	sband = wiphy->bands[IEEE80211_BAND_5GHZ];
409

410 411
	for (i = 0; i < sband->n_channels; i++) {
		ch = &sband->channels[i];
412
		if (!ath_is_radar_freq(ch->center_freq))
413 414 415 416 417 418 419 420 421 422 423 424 425
			continue;
		/* We always enable radar detection/DFS on this
		 * frequency range. Additionally we also apply on
		 * this frequency range:
		 * - If STA mode does not yet have DFS supports disable
		 *   active scanning
		 * - If adhoc mode does not support DFS yet then
		 *   disable adhoc in the frequency.
		 * - If AP mode does not yet support radar detection/DFS
		 *   do not allow AP mode
		 */
		if (!(ch->flags & IEEE80211_CHAN_DISABLED))
			ch->flags |= IEEE80211_CHAN_RADAR |
426
				     IEEE80211_CHAN_NO_IR;
427 428
	}
}
429

430
static void ath_reg_apply_world_flags(struct wiphy *wiphy,
L
Luis R. Rodriguez 已提交
431 432
				      enum nl80211_reg_initiator initiator,
				      struct ath_regulatory *reg)
433
{
434
	switch (reg->regpair->regDmnEnum) {
435 436 437 438
	case 0x60:
	case 0x63:
	case 0x66:
	case 0x67:
439
	case 0x6C:
440
		ath_reg_apply_beaconing_flags(wiphy, reg, initiator);
441 442
		break;
	case 0x68:
443 444
		ath_reg_apply_beaconing_flags(wiphy, reg, initiator);
		ath_reg_apply_ir_flags(wiphy, reg, initiator);
445
		break;
446 447 448
	default:
		if (ath_reg_dyn_country_user_allow(reg))
			ath_reg_apply_beaconing_flags(wiphy, reg, initiator);
449 450
	}
}
451

452 453 454 455 456 457 458 459 460 461 462 463
static u16 ath_regd_find_country_by_name(char *alpha2)
{
	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
		if (!memcmp(allCountries[i].isoName, alpha2, 2))
			return allCountries[i].countryCode;
	}

	return -1;
}

464 465 466 467 468 469
static int __ath_reg_dyn_country(struct wiphy *wiphy,
				 struct ath_regulatory *reg,
				 struct regulatory_request *request)
{
	u16 country_code;

470 471
	if (request->initiator == NL80211_REGDOM_SET_BY_COUNTRY_IE &&
	    !ath_is_world_regd(reg))
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
		return -EINVAL;

	country_code = ath_regd_find_country_by_name(request->alpha2);
	if (country_code == (u16) -1)
		return -EINVAL;

	reg->current_rd = COUNTRY_ERD_FLAG;
	reg->current_rd |= country_code;

	__ath_regd_init(reg);

	ath_reg_apply_world_flags(wiphy, request->initiator, reg);

	return 0;
}

static void ath_reg_dyn_country(struct wiphy *wiphy,
				struct ath_regulatory *reg,
				struct regulatory_request *request)
{
	if (__ath_reg_dyn_country(wiphy, reg, request))
		return;

	printk(KERN_DEBUG "ath: regdomain 0x%0x "
			  "dynamically updated by %s\n",
	       reg->current_rd,
	       reg_initiator_name(request->initiator));
}

501 502 503
void ath_reg_notifier_apply(struct wiphy *wiphy,
			    struct regulatory_request *request,
			    struct ath_regulatory *reg)
504
{
505 506
	struct ath_common *common = container_of(reg, struct ath_common,
						 regulatory);
507
	/* We always apply this */
508
	ath_reg_apply_radar_flags(wiphy);
509

510 511 512 513 514 515
	/*
	 * This would happen when we have sent a custom regulatory request
	 * a world regulatory domain and the scheduler hasn't yet processed
	 * any pending requests in the queue.
	 */
	if (!request)
516
		return;
517

518
	switch (request->initiator) {
519
	case NL80211_REGDOM_SET_BY_CORE:
520 521 522 523 524 525 526 527 528 529 530
		/*
		 * If common->reg_world_copy is world roaming it means we *were*
		 * world roaming... so we now have to restore that data.
		 */
		if (!ath_is_world_regd(&common->reg_world_copy))
			break;

		memcpy(reg, &common->reg_world_copy,
		       sizeof(struct ath_regulatory));
		break;
	case NL80211_REGDOM_SET_BY_DRIVER:
531
		break;
532
	case NL80211_REGDOM_SET_BY_USER:
533 534
		if (ath_reg_dyn_country_user_allow(reg))
			ath_reg_dyn_country(wiphy, reg, request);
535
		break;
536
	case NL80211_REGDOM_SET_BY_COUNTRY_IE:
537
		ath_reg_dyn_country(wiphy, reg, request);
538 539
		break;
	}
540
}
541
EXPORT_SYMBOL(ath_reg_notifier_apply);
542

543
static bool ath_regd_is_eeprom_valid(struct ath_regulatory *reg)
544
{
545
	u16 rd = ath_regd_get_eepromRD(reg);
546
	int i;
547

548 549 550
	if (rd & COUNTRY_ERD_FLAG) {
		/* EEPROM value is a country code */
		u16 cc = rd & ~COUNTRY_ERD_FLAG;
551 552 553
		printk(KERN_DEBUG
		       "ath: EEPROM indicates we should expect "
			"a country code\n");
554 555 556 557 558
		for (i = 0; i < ARRAY_SIZE(allCountries); i++)
			if (allCountries[i].countryCode == cc)
				return true;
	} else {
		/* EEPROM value is a regpair value */
559 560 561
		if (rd != CTRY_DEFAULT)
			printk(KERN_DEBUG "ath: EEPROM indicates we "
			       "should expect a direct regpair map\n");
562 563 564
		for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++)
			if (regDomainPairs[i].regDmnEnum == rd)
				return true;
565
	}
566
	printk(KERN_DEBUG
567
		 "ath: invalid regulatory domain/country code 0x%x\n", rd);
568 569 570
	return false;
}

571
/* EEPROM country code to regpair mapping */
572
static struct country_code_to_enum_rd*
573
ath_regd_find_country(u16 countryCode)
574 575 576 577 578 579 580 581 582 583
{
	int i;

	for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
		if (allCountries[i].countryCode == countryCode)
			return &allCountries[i];
	}
	return NULL;
}

584 585
/* EEPROM rd code to regpair mapping */
static struct country_code_to_enum_rd*
586
ath_regd_find_country_by_rd(int regdmn)
587 588 589 590 591 592 593 594 595 596 597
{
	int i;

	for (i = 0; i < ARRAY_SIZE(allCountries); i++) {
		if (allCountries[i].regDmnEnum == regdmn)
			return &allCountries[i];
	}
	return NULL;
}

/* Returns the map of the EEPROM set RD to a country code */
598
static u16 ath_regd_get_default_country(u16 rd)
599 600 601 602 603
{
	if (rd & COUNTRY_ERD_FLAG) {
		struct country_code_to_enum_rd *country = NULL;
		u16 cc = rd & ~COUNTRY_ERD_FLAG;

604
		country = ath_regd_find_country(cc);
605 606 607 608 609 610 611
		if (country != NULL)
			return cc;
	}

	return CTRY_DEFAULT;
}

612
static struct reg_dmn_pair_mapping*
613
ath_get_regpair(int regdmn)
614 615 616
{
	int i;

617 618
	if (regdmn == NO_ENUMRD)
		return NULL;
619
	for (i = 0; i < ARRAY_SIZE(regDomainPairs); i++) {
620 621
		if (regDomainPairs[i].regDmnEnum == regdmn)
			return &regDomainPairs[i];
622
	}
623
	return NULL;
624 625
}

L
Luis R. Rodriguez 已提交
626 627 628
static int
ath_regd_init_wiphy(struct ath_regulatory *reg,
		    struct wiphy *wiphy,
629 630
		    void (*reg_notifier)(struct wiphy *wiphy,
					 struct regulatory_request *request))
631 632 633 634
{
	const struct ieee80211_regdomain *regd;

	wiphy->reg_notifier = reg_notifier;
635 636
	wiphy->regulatory_flags |= REGULATORY_STRICT_REG |
				   REGULATORY_CUSTOM_REG;
637 638 639 640 641 642 643

	if (ath_is_world_regd(reg)) {
		/*
		 * Anything applied here (prior to wiphy registration) gets
		 * saved on the wiphy orig_* parameters
		 */
		regd = ath_world_regdomain(reg);
644
		wiphy->regulatory_flags |= REGULATORY_COUNTRY_IE_FOLLOW_POWER;
645 646
	} else {
		/*
L
Lucas De Marchi 已提交
647
		 * This gets applied in the case of the absence of CRDA,
648 649 650 651 652
		 * it's our own custom world regulatory domain, similar to
		 * cfg80211's but we enable passive scanning.
		 */
		regd = ath_default_world_regdomain();
	}
653

654
	wiphy_apply_custom_regulatory(wiphy, regd);
655 656
	if (!ath_is_world_regd(reg))
		wiphy->regulatory_flags &= ~REGULATORY_CUSTOM_REG;
657 658 659 660 661
	ath_reg_apply_radar_flags(wiphy);
	ath_reg_apply_world_flags(wiphy, NL80211_REGDOM_SET_BY_DRIVER, reg);
	return 0;
}

662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
/*
 * Some users have reported their EEPROM programmed with
 * 0x8000 set, this is not a supported regulatory domain
 * but since we have more than one user with it we need
 * a solution for them. We default to 0x64, which is the
 * default Atheros world regulatory domain.
 */
static void ath_regd_sanitize(struct ath_regulatory *reg)
{
	if (reg->current_rd != COUNTRY_ERD_FLAG)
		return;
	printk(KERN_DEBUG "ath: EEPROM regdomain sanitized\n");
	reg->current_rd = 0x64;
}

677
static int __ath_regd_init(struct ath_regulatory *reg)
678 679
{
	struct country_code_to_enum_rd *country = NULL;
680
	u16 regdmn;
681

682 683 684
	if (!reg)
		return -EINVAL;

685 686
	ath_regd_sanitize(reg);

687 688
	printk(KERN_DEBUG "ath: EEPROM regdomain: 0x%0x\n", reg->current_rd);

689
	if (!ath_regd_is_eeprom_valid(reg)) {
690
		pr_err("Invalid EEPROM contents\n");
691
		return -EINVAL;
692 693
	}

694 695
	regdmn = ath_regd_get_eepromRD(reg);
	reg->country_code = ath_regd_get_default_country(regdmn);
696

697
	if (reg->country_code == CTRY_DEFAULT &&
698 699 700
	    regdmn == CTRY_DEFAULT) {
		printk(KERN_DEBUG "ath: EEPROM indicates default "
		       "country code should be used\n");
701
		reg->country_code = CTRY_UNITED_STATES;
702
	}
703

704
	if (reg->country_code == CTRY_DEFAULT) {
705 706
		country = NULL;
	} else {
707 708
		printk(KERN_DEBUG "ath: doing EEPROM country->regdmn "
		       "map search\n");
709
		country = ath_regd_find_country(reg->country_code);
710
		if (country == NULL) {
711
			printk(KERN_DEBUG
712 713
				"ath: no valid country maps found for "
				"country code: 0x%0x\n",
714
				reg->country_code);
715
			return -EINVAL;
716
		} else {
717
			regdmn = country->regDmnEnum;
718 719 720 721
			printk(KERN_DEBUG "ath: country maps to "
			       "regdmn code: 0x%0x\n",
			       regdmn);
		}
722 723
	}

724
	reg->regpair = ath_get_regpair(regdmn);
725

726
	if (!reg->regpair) {
727
		printk(KERN_DEBUG "ath: "
728 729
			"No regulatory domain pair found, cannot continue\n");
		return -EINVAL;
730 731
	}

732
	if (!country)
733
		country = ath_regd_find_country_by_rd(regdmn);
734

735
	if (country) {
736 737
		reg->alpha2[0] = country->isoName[0];
		reg->alpha2[1] = country->isoName[1];
738
	} else {
739 740
		reg->alpha2[0] = '0';
		reg->alpha2[1] = '0';
741 742
	}

743
	printk(KERN_DEBUG "ath: Country alpha2 being used: %c%c\n",
744
		reg->alpha2[0], reg->alpha2[1]);
745
	printk(KERN_DEBUG "ath: Regpair used: 0x%0x\n",
746
		reg->regpair->regDmnEnum);
747

748 749 750 751 752 753
	return 0;
}

int
ath_regd_init(struct ath_regulatory *reg,
	      struct wiphy *wiphy,
754 755
	      void (*reg_notifier)(struct wiphy *wiphy,
				   struct regulatory_request *request))
756
{
757 758
	struct ath_common *common = container_of(reg, struct ath_common,
						 regulatory);
759 760 761 762 763 764
	int r;

	r = __ath_regd_init(reg);
	if (r)
		return r;

765 766 767 768
	if (ath_is_world_regd(reg))
		memcpy(&common->reg_world_copy, reg,
		       sizeof(struct ath_regulatory));

769
	ath_regd_init_wiphy(reg, wiphy, reg_notifier);
770

771
	return 0;
772
}
773
EXPORT_SYMBOL(ath_regd_init);
774

775 776
u32 ath_regd_get_band_ctl(struct ath_regulatory *reg,
			  enum ieee80211_band band)
777
{
778 779
	if (!reg->regpair ||
	    (reg->country_code == CTRY_DEFAULT &&
780
	     is_wwr_sku(ath_regd_get_eepromRD(reg)))) {
781 782 783 784 785
		return SD_NO_CTL;
	}

	switch (band) {
	case IEEE80211_BAND_2GHZ:
786
		return reg->regpair->reg_2ghz_ctl;
787
	case IEEE80211_BAND_5GHZ:
788
		return reg->regpair->reg_5ghz_ctl;
789 790
	default:
		return NO_CTL;
791
	}
792
}
793
EXPORT_SYMBOL(ath_regd_get_band_ctl);