wireless: remove write-only 'granted' variable
[linux-2.6/kvm.git] / net / wireless / reg.c
blob00c326b66c0331535885903ea8c6b495df1ac157
1 /*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
5 * Copyright 2008 Luis R. Rodriguez <lrodriguz@atheros.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 /**
13 * DOC: Wireless regulatory infrastructure
15 * The usual implementation is for a driver to read a device EEPROM to
16 * determine which regulatory domain it should be operating under, then
17 * looking up the allowable channels in a driver-local table and finally
18 * registering those channels in the wiphy structure.
20 * Another set of compliance enforcement is for drivers to use their
21 * own compliance limits which can be stored on the EEPROM. The host
22 * driver or firmware may ensure these are used.
24 * In addition to all this we provide an extra layer of regulatory
25 * conformance. For drivers which do not have any regulatory
26 * information CRDA provides the complete regulatory solution.
27 * For others it provides a community effort on further restrictions
28 * to enhance compliance.
30 * Note: When number of rules --> infinity we will not be able to
31 * index on alpha2 any more, instead we'll probably have to
32 * rely on some SHA1 checksum of the regdomain for example.
35 #include <linux/kernel.h>
36 #include <linux/list.h>
37 #include <linux/random.h>
38 #include <linux/nl80211.h>
39 #include <linux/platform_device.h>
40 #include <net/wireless.h>
41 #include <net/cfg80211.h>
42 #include "core.h"
43 #include "reg.h"
45 /* wiphy is set if this request's initiator is REGDOM_SET_BY_DRIVER */
46 struct regulatory_request {
47 struct wiphy *wiphy;
48 enum reg_set_by initiator;
49 char alpha2[2];
52 static struct regulatory_request *last_request;
54 /* To trigger userspace events */
55 static struct platform_device *reg_pdev;
57 /* Keep the ordering from large to small */
58 static u32 supported_bandwidths[] = {
59 MHZ_TO_KHZ(40),
60 MHZ_TO_KHZ(20),
63 /* Central wireless core regulatory domains, we only need two,
64 * the current one and a world regulatory domain in case we have no
65 * information to give us an alpha2 */
66 static const struct ieee80211_regdomain *cfg80211_regdomain;
68 /* We keep a static world regulatory domain in case of the absence of CRDA */
69 static const struct ieee80211_regdomain world_regdom = {
70 .n_reg_rules = 1,
71 .alpha2 = "00",
72 .reg_rules = {
73 REG_RULE(2412-10, 2462+10, 40, 6, 20,
74 NL80211_RRF_PASSIVE_SCAN |
75 NL80211_RRF_NO_IBSS),
79 static const struct ieee80211_regdomain *cfg80211_world_regdom =
80 &world_regdom;
82 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
83 static char *ieee80211_regdom = "US";
84 module_param(ieee80211_regdom, charp, 0444);
85 MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
87 /* We assume 40 MHz bandwidth for the old regulatory work.
88 * We make emphasis we are using the exact same frequencies
89 * as before */
91 static const struct ieee80211_regdomain us_regdom = {
92 .n_reg_rules = 6,
93 .alpha2 = "US",
94 .reg_rules = {
95 /* IEEE 802.11b/g, channels 1..11 */
96 REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
97 /* IEEE 802.11a, channel 36 */
98 REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
99 /* IEEE 802.11a, channel 40 */
100 REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
101 /* IEEE 802.11a, channel 44 */
102 REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
103 /* IEEE 802.11a, channels 48..64 */
104 REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
105 /* IEEE 802.11a, channels 149..165, outdoor */
106 REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
110 static const struct ieee80211_regdomain jp_regdom = {
111 .n_reg_rules = 3,
112 .alpha2 = "JP",
113 .reg_rules = {
114 /* IEEE 802.11b/g, channels 1..14 */
115 REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
116 /* IEEE 802.11a, channels 34..48 */
117 REG_RULE(5170-10, 5240+10, 40, 6, 20,
118 NL80211_RRF_PASSIVE_SCAN),
119 /* IEEE 802.11a, channels 52..64 */
120 REG_RULE(5260-10, 5320+10, 40, 6, 20,
121 NL80211_RRF_NO_IBSS |
122 NL80211_RRF_DFS),
126 static const struct ieee80211_regdomain eu_regdom = {
127 .n_reg_rules = 6,
128 /* This alpha2 is bogus, we leave it here just for stupid
129 * backward compatibility */
130 .alpha2 = "EU",
131 .reg_rules = {
132 /* IEEE 802.11b/g, channels 1..13 */
133 REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
134 /* IEEE 802.11a, channel 36 */
135 REG_RULE(5180-10, 5180+10, 40, 6, 23,
136 NL80211_RRF_PASSIVE_SCAN),
137 /* IEEE 802.11a, channel 40 */
138 REG_RULE(5200-10, 5200+10, 40, 6, 23,
139 NL80211_RRF_PASSIVE_SCAN),
140 /* IEEE 802.11a, channel 44 */
141 REG_RULE(5220-10, 5220+10, 40, 6, 23,
142 NL80211_RRF_PASSIVE_SCAN),
143 /* IEEE 802.11a, channels 48..64 */
144 REG_RULE(5240-10, 5320+10, 40, 6, 20,
145 NL80211_RRF_NO_IBSS |
146 NL80211_RRF_DFS),
147 /* IEEE 802.11a, channels 100..140 */
148 REG_RULE(5500-10, 5700+10, 40, 6, 30,
149 NL80211_RRF_NO_IBSS |
150 NL80211_RRF_DFS),
154 static const struct ieee80211_regdomain *static_regdom(char *alpha2)
156 if (alpha2[0] == 'U' && alpha2[1] == 'S')
157 return &us_regdom;
158 if (alpha2[0] == 'J' && alpha2[1] == 'P')
159 return &jp_regdom;
160 if (alpha2[0] == 'E' && alpha2[1] == 'U')
161 return &eu_regdom;
162 /* Default, as per the old rules */
163 return &us_regdom;
166 static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
168 if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
169 return true;
170 return false;
172 #else
173 static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
175 return false;
177 #endif
179 static void reset_regdomains(void)
181 /* avoid freeing static information or freeing something twice */
182 if (cfg80211_regdomain == cfg80211_world_regdom)
183 cfg80211_regdomain = NULL;
184 if (cfg80211_world_regdom == &world_regdom)
185 cfg80211_world_regdom = NULL;
186 if (cfg80211_regdomain == &world_regdom)
187 cfg80211_regdomain = NULL;
188 if (is_old_static_regdom(cfg80211_regdomain))
189 cfg80211_regdomain = NULL;
191 kfree(cfg80211_regdomain);
192 kfree(cfg80211_world_regdom);
194 cfg80211_world_regdom = &world_regdom;
195 cfg80211_regdomain = NULL;
198 /* Dynamic world regulatory domain requested by the wireless
199 * core upon initialization */
200 static void update_world_regdomain(const struct ieee80211_regdomain *rd)
202 BUG_ON(!last_request);
204 reset_regdomains();
206 cfg80211_world_regdom = rd;
207 cfg80211_regdomain = rd;
210 bool is_world_regdom(const char *alpha2)
212 if (!alpha2)
213 return false;
214 if (alpha2[0] == '0' && alpha2[1] == '0')
215 return true;
216 return false;
219 static bool is_alpha2_set(const char *alpha2)
221 if (!alpha2)
222 return false;
223 if (alpha2[0] != 0 && alpha2[1] != 0)
224 return true;
225 return false;
228 static bool is_alpha_upper(char letter)
230 /* ASCII A - Z */
231 if (letter >= 65 && letter <= 90)
232 return true;
233 return false;
236 static bool is_unknown_alpha2(const char *alpha2)
238 if (!alpha2)
239 return false;
240 /* Special case where regulatory domain was built by driver
241 * but a specific alpha2 cannot be determined */
242 if (alpha2[0] == '9' && alpha2[1] == '9')
243 return true;
244 return false;
247 static bool is_an_alpha2(const char *alpha2)
249 if (!alpha2)
250 return false;
251 if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
252 return true;
253 return false;
256 static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
258 if (!alpha2_x || !alpha2_y)
259 return false;
260 if (alpha2_x[0] == alpha2_y[0] &&
261 alpha2_x[1] == alpha2_y[1])
262 return true;
263 return false;
266 static bool regdom_changed(const char *alpha2)
268 if (!cfg80211_regdomain)
269 return true;
270 if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
271 return false;
272 return true;
275 /* This lets us keep regulatory code which is updated on a regulatory
276 * basis in userspace. */
277 static int call_crda(const char *alpha2)
279 char country_env[9 + 2] = "COUNTRY=";
280 char *envp[] = {
281 country_env,
282 NULL
285 if (!is_world_regdom((char *) alpha2))
286 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
287 alpha2[0], alpha2[1]);
288 else
289 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
290 "regulatory domain\n");
292 country_env[8] = alpha2[0];
293 country_env[9] = alpha2[1];
295 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
298 /* This has the logic which determines when a new request
299 * should be ignored. */
300 static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by,
301 char *alpha2, struct ieee80211_regdomain *rd)
303 /* All initial requests are respected */
304 if (!last_request)
305 return 0;
307 switch (set_by) {
308 case REGDOM_SET_BY_INIT:
309 return -EINVAL;
310 case REGDOM_SET_BY_CORE:
311 /* Always respect new wireless core hints, should only
312 * come in for updating the world regulatory domain at init
313 * anyway */
314 return 0;
315 case REGDOM_SET_BY_COUNTRY_IE:
316 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE) {
317 if (last_request->wiphy != wiphy) {
318 /* Two cards with two APs claiming different
319 * different Country IE alpha2s!
320 * You're special!! */
321 if (!alpha2_equal(last_request->alpha2,
322 cfg80211_regdomain->alpha2)) {
323 /* XXX: Deal with conflict, consider
324 * building a new one out of the
325 * intersection */
326 WARN_ON(1);
327 return -EOPNOTSUPP;
329 return -EALREADY;
331 /* Two consecutive Country IE hints on the same wiphy */
332 if (!alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
333 return 0;
334 return -EALREADY;
336 if (WARN_ON(!is_alpha2_set(alpha2) || !is_an_alpha2(alpha2)),
337 "Invalid Country IE regulatory hint passed "
338 "to the wireless core\n")
339 return -EINVAL;
340 /* We ignore Country IE hints for now, as we haven't yet
341 * added the dot11MultiDomainCapabilityEnabled flag
342 * for wiphys */
343 return 1;
344 case REGDOM_SET_BY_DRIVER:
345 BUG_ON(!wiphy);
346 if (last_request->initiator == REGDOM_SET_BY_DRIVER) {
347 /* Two separate drivers hinting different things,
348 * this is possible if you have two devices present
349 * on a system with different EEPROM regulatory
350 * readings. XXX: Do intersection, we support only
351 * the first regulatory hint for now */
352 if (last_request->wiphy != wiphy)
353 return -EALREADY;
354 if (rd)
355 return -EALREADY;
356 /* Driver should not be trying to hint different
357 * regulatory domains! */
358 BUG_ON(!alpha2_equal(alpha2,
359 cfg80211_regdomain->alpha2));
360 return -EALREADY;
362 if (last_request->initiator == REGDOM_SET_BY_CORE)
363 return 0;
364 /* XXX: Handle intersection, and add the
365 * dot11MultiDomainCapabilityEnabled flag to wiphy. For now
366 * we assume the driver has this set to false, following the
367 * 802.11d dot11MultiDomainCapabilityEnabled documentation */
368 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
369 return 0;
370 return 0;
371 case REGDOM_SET_BY_USER:
372 if (last_request->initiator == REGDOM_SET_BY_USER ||
373 last_request->initiator == REGDOM_SET_BY_CORE)
374 return 0;
375 /* Drivers can use their wiphy's reg_notifier()
376 * to override any information */
377 if (last_request->initiator == REGDOM_SET_BY_DRIVER)
378 return 0;
379 /* XXX: Handle intersection */
380 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
381 return -EOPNOTSUPP;
382 return 0;
383 default:
384 return -EINVAL;
388 /* Used by nl80211 before kmalloc'ing our regulatory domain */
389 bool reg_is_valid_request(const char *alpha2)
391 if (!last_request)
392 return false;
394 return alpha2_equal(last_request->alpha2, alpha2);
397 /* Sanity check on a regulatory rule */
398 static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
400 const struct ieee80211_freq_range *freq_range = &rule->freq_range;
401 u32 freq_diff;
403 if (freq_range->start_freq_khz == 0 || freq_range->end_freq_khz == 0)
404 return false;
406 if (freq_range->start_freq_khz > freq_range->end_freq_khz)
407 return false;
409 freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
411 if (freq_range->max_bandwidth_khz > freq_diff)
412 return false;
414 return true;
417 static bool is_valid_rd(const struct ieee80211_regdomain *rd)
419 const struct ieee80211_reg_rule *reg_rule = NULL;
420 unsigned int i;
422 if (!rd->n_reg_rules)
423 return false;
425 for (i = 0; i < rd->n_reg_rules; i++) {
426 reg_rule = &rd->reg_rules[i];
427 if (!is_valid_reg_rule(reg_rule))
428 return false;
431 return true;
434 /* Returns value in KHz */
435 static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
436 u32 freq)
438 unsigned int i;
439 for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
440 u32 start_freq_khz = freq - supported_bandwidths[i]/2;
441 u32 end_freq_khz = freq + supported_bandwidths[i]/2;
442 if (start_freq_khz >= freq_range->start_freq_khz &&
443 end_freq_khz <= freq_range->end_freq_khz)
444 return supported_bandwidths[i];
446 return 0;
449 /* XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
450 * want to just have the channel structure use these */
451 static u32 map_regdom_flags(u32 rd_flags)
453 u32 channel_flags = 0;
454 if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
455 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
456 if (rd_flags & NL80211_RRF_NO_IBSS)
457 channel_flags |= IEEE80211_CHAN_NO_IBSS;
458 if (rd_flags & NL80211_RRF_DFS)
459 channel_flags |= IEEE80211_CHAN_RADAR;
460 return channel_flags;
464 * freq_reg_info - get regulatory information for the given frequency
465 * @center_freq: Frequency in KHz for which we want regulatory information for
466 * @bandwidth: the bandwidth requirement you have in KHz, if you do not have one
467 * you can set this to 0. If this frequency is allowed we then set
468 * this value to the maximum allowed bandwidth.
469 * @reg_rule: the regulatory rule which we have for this frequency
471 * Use this function to get the regulatory rule for a specific frequency.
473 static int freq_reg_info(u32 center_freq, u32 *bandwidth,
474 const struct ieee80211_reg_rule **reg_rule)
476 int i;
477 u32 max_bandwidth = 0;
479 if (!cfg80211_regdomain)
480 return -EINVAL;
482 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
483 const struct ieee80211_reg_rule *rr;
484 const struct ieee80211_freq_range *fr = NULL;
485 const struct ieee80211_power_rule *pr = NULL;
487 rr = &cfg80211_regdomain->reg_rules[i];
488 fr = &rr->freq_range;
489 pr = &rr->power_rule;
490 max_bandwidth = freq_max_bandwidth(fr, center_freq);
491 if (max_bandwidth && *bandwidth <= max_bandwidth) {
492 *reg_rule = rr;
493 *bandwidth = max_bandwidth;
494 break;
498 return !max_bandwidth;
501 static void handle_channel(struct ieee80211_channel *chan)
503 int r;
504 u32 flags = chan->orig_flags;
505 u32 max_bandwidth = 0;
506 const struct ieee80211_reg_rule *reg_rule = NULL;
507 const struct ieee80211_power_rule *power_rule = NULL;
509 r = freq_reg_info(MHZ_TO_KHZ(chan->center_freq),
510 &max_bandwidth, &reg_rule);
512 if (r) {
513 flags |= IEEE80211_CHAN_DISABLED;
514 chan->flags = flags;
515 return;
518 power_rule = &reg_rule->power_rule;
520 chan->flags = flags | map_regdom_flags(reg_rule->flags);
521 chan->max_antenna_gain = min(chan->orig_mag,
522 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
523 chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
524 if (chan->orig_mpwr)
525 chan->max_power = min(chan->orig_mpwr,
526 (int) MBM_TO_DBM(power_rule->max_eirp));
527 else
528 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
531 static void handle_band(struct ieee80211_supported_band *sband)
533 int i;
535 for (i = 0; i < sband->n_channels; i++)
536 handle_channel(&sband->channels[i]);
539 static void update_all_wiphy_regulatory(enum reg_set_by setby)
541 struct cfg80211_registered_device *drv;
543 list_for_each_entry(drv, &cfg80211_drv_list, list)
544 wiphy_update_regulatory(&drv->wiphy, setby);
547 void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
549 enum ieee80211_band band;
550 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
551 if (wiphy->bands[band])
552 handle_band(wiphy->bands[band]);
553 if (wiphy->reg_notifier)
554 wiphy->reg_notifier(wiphy, setby);
558 /* Caller must hold &cfg80211_drv_mutex */
559 int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by,
560 const char *alpha2, struct ieee80211_regdomain *rd)
562 struct regulatory_request *request;
563 char *rd_alpha2;
564 int r = 0;
566 r = ignore_request(wiphy, set_by, (char *) alpha2, rd);
567 if (r)
568 return r;
570 if (rd)
571 rd_alpha2 = rd->alpha2;
572 else
573 rd_alpha2 = (char *) alpha2;
575 switch (set_by) {
576 case REGDOM_SET_BY_CORE:
577 case REGDOM_SET_BY_COUNTRY_IE:
578 case REGDOM_SET_BY_DRIVER:
579 case REGDOM_SET_BY_USER:
580 request = kzalloc(sizeof(struct regulatory_request),
581 GFP_KERNEL);
582 if (!request)
583 return -ENOMEM;
585 request->alpha2[0] = rd_alpha2[0];
586 request->alpha2[1] = rd_alpha2[1];
587 request->initiator = set_by;
588 request->wiphy = wiphy;
590 kfree(last_request);
591 last_request = request;
592 if (rd)
593 break;
594 r = call_crda(alpha2);
595 #ifndef CONFIG_WIRELESS_OLD_REGULATORY
596 if (r)
597 printk(KERN_ERR "cfg80211: Failed calling CRDA\n");
598 #endif
599 break;
600 default:
601 r = -ENOTSUPP;
602 break;
605 return r;
608 /* If rd is not NULL and if this call fails the caller must free it */
609 int regulatory_hint(struct wiphy *wiphy, const char *alpha2,
610 struct ieee80211_regdomain *rd)
612 int r;
613 BUG_ON(!rd && !alpha2);
615 mutex_lock(&cfg80211_drv_mutex);
617 r = __regulatory_hint(wiphy, REGDOM_SET_BY_DRIVER, alpha2, rd);
618 if (r || !rd)
619 goto unlock_and_exit;
621 /* If the driver passed a regulatory domain we skipped asking
622 * userspace for one so we can now go ahead and set it */
623 r = set_regdom(rd);
625 unlock_and_exit:
626 mutex_unlock(&cfg80211_drv_mutex);
627 return r;
629 EXPORT_SYMBOL(regulatory_hint);
632 static void print_rd_rules(const struct ieee80211_regdomain *rd)
634 unsigned int i;
635 const struct ieee80211_reg_rule *reg_rule = NULL;
636 const struct ieee80211_freq_range *freq_range = NULL;
637 const struct ieee80211_power_rule *power_rule = NULL;
639 printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
640 "(max_antenna_gain, max_eirp)\n");
642 for (i = 0; i < rd->n_reg_rules; i++) {
643 reg_rule = &rd->reg_rules[i];
644 freq_range = &reg_rule->freq_range;
645 power_rule = &reg_rule->power_rule;
647 /* There may not be documentation for max antenna gain
648 * in certain regions */
649 if (power_rule->max_antenna_gain)
650 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
651 "(%d mBi, %d mBm)\n",
652 freq_range->start_freq_khz,
653 freq_range->end_freq_khz,
654 freq_range->max_bandwidth_khz,
655 power_rule->max_antenna_gain,
656 power_rule->max_eirp);
657 else
658 printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
659 "(N/A, %d mBm)\n",
660 freq_range->start_freq_khz,
661 freq_range->end_freq_khz,
662 freq_range->max_bandwidth_khz,
663 power_rule->max_eirp);
667 static void print_regdomain(const struct ieee80211_regdomain *rd)
670 if (is_world_regdom(rd->alpha2))
671 printk(KERN_INFO "cfg80211: World regulatory "
672 "domain updated:\n");
673 else {
674 if (is_unknown_alpha2(rd->alpha2))
675 printk(KERN_INFO "cfg80211: Regulatory domain "
676 "changed to driver built-in settings "
677 "(unknown country)\n");
678 else
679 printk(KERN_INFO "cfg80211: Regulatory domain "
680 "changed to country: %c%c\n",
681 rd->alpha2[0], rd->alpha2[1]);
683 print_rd_rules(rd);
686 void print_regdomain_info(const struct ieee80211_regdomain *rd)
688 printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
689 rd->alpha2[0], rd->alpha2[1]);
690 print_rd_rules(rd);
693 static int __set_regdom(const struct ieee80211_regdomain *rd)
695 /* Some basic sanity checks first */
697 if (is_world_regdom(rd->alpha2)) {
698 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
699 return -EINVAL;
700 update_world_regdomain(rd);
701 return 0;
704 if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
705 !is_unknown_alpha2(rd->alpha2))
706 return -EINVAL;
708 if (!last_request)
709 return -EINVAL;
711 /* allow overriding the static definitions if CRDA is present */
712 if (!is_old_static_regdom(cfg80211_regdomain) &&
713 !regdom_changed(rd->alpha2))
714 return -EINVAL;
716 /* Now lets set the regulatory domain, update all driver channels
717 * and finally inform them of what we have done, in case they want
718 * to review or adjust their own settings based on their own
719 * internal EEPROM data */
721 if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
722 return -EINVAL;
724 reset_regdomains();
726 /* Country IE parsing coming soon */
727 switch (last_request->initiator) {
728 case REGDOM_SET_BY_CORE:
729 case REGDOM_SET_BY_DRIVER:
730 case REGDOM_SET_BY_USER:
731 if (!is_valid_rd(rd)) {
732 printk(KERN_ERR "cfg80211: Invalid "
733 "regulatory domain detected:\n");
734 print_regdomain_info(rd);
735 return -EINVAL;
737 break;
738 case REGDOM_SET_BY_COUNTRY_IE: /* Not yet */
739 WARN_ON(1);
740 default:
741 return -EOPNOTSUPP;
744 /* Tada! */
745 cfg80211_regdomain = rd;
747 return 0;
751 /* Use this call to set the current regulatory domain. Conflicts with
752 * multiple drivers can be ironed out later. Caller must've already
753 * kmalloc'd the rd structure. If this calls fails you should kfree()
754 * the passed rd. Caller must hold cfg80211_drv_mutex */
755 int set_regdom(const struct ieee80211_regdomain *rd)
757 int r;
759 /* Note that this doesn't update the wiphys, this is done below */
760 r = __set_regdom(rd);
761 if (r)
762 return r;
764 /* This would make this whole thing pointless */
765 BUG_ON(rd != cfg80211_regdomain);
767 /* update all wiphys now with the new established regulatory domain */
768 update_all_wiphy_regulatory(last_request->initiator);
770 print_regdomain(rd);
772 return r;
775 int regulatory_init(void)
777 int err;
779 reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
780 if (IS_ERR(reg_pdev))
781 return PTR_ERR(reg_pdev);
783 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
784 cfg80211_regdomain = static_regdom(ieee80211_regdom);
786 printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
787 print_regdomain_info(cfg80211_regdomain);
788 /* The old code still requests for a new regdomain and if
789 * you have CRDA you get it updated, otherwise you get
790 * stuck with the static values. We ignore "EU" code as
791 * that is not a valid ISO / IEC 3166 alpha2 */
792 if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U')
793 err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE,
794 ieee80211_regdom, NULL);
795 #else
796 cfg80211_regdomain = cfg80211_world_regdom;
798 err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, "00", NULL);
799 if (err)
800 printk(KERN_ERR "cfg80211: calling CRDA failed - "
801 "unable to update world regulatory domain, "
802 "using static definition\n");
803 #endif
805 return 0;
808 void regulatory_exit(void)
810 mutex_lock(&cfg80211_drv_mutex);
812 reset_regdomains();
814 kfree(last_request);
816 platform_device_unregister(reg_pdev);
818 mutex_unlock(&cfg80211_drv_mutex);