wireless: make regdom passing semantics simpler
[safe/jmp/linux-2.6] / net / wireless / reg.c
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>
6  *
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.
10  */
11
12 /**
13  * DOC: Wireless regulatory infrastructure
14  *
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.
19  *
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.
23  *
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.
29  *
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.
33  *
34  */
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"
44
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];
50 };
51
52 static struct regulatory_request *last_request;
53
54 /* To trigger userspace events */
55 static struct platform_device *reg_pdev;
56
57 /* Keep the ordering from large to small */
58 static u32 supported_bandwidths[] = {
59         MHZ_TO_KHZ(40),
60         MHZ_TO_KHZ(20),
61 };
62
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;
67
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),
76         }
77 };
78
79 static const struct ieee80211_regdomain *cfg80211_world_regdom =
80         &world_regdom;
81
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");
86
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 */
90
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),
107         }
108 };
109
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),
123         }
124 };
125
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),
151         }
152 };
153
154 static const struct ieee80211_regdomain *static_regdom(char *alpha2)
155 {
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;
164 }
165
166 static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
167 {
168         if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
169                 return true;
170         return false;
171 }
172 #else
173 static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
174 {
175         return false;
176 }
177 #endif
178
179 static void reset_regdomains(void)
180 {
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;
190
191         kfree(cfg80211_regdomain);
192         kfree(cfg80211_world_regdom);
193
194         cfg80211_world_regdom = &world_regdom;
195         cfg80211_regdomain = NULL;
196 }
197
198 /* Dynamic world regulatory domain requested by the wireless
199  * core upon initialization */
200 static void update_world_regdomain(const struct ieee80211_regdomain *rd)
201 {
202         BUG_ON(!last_request);
203
204         reset_regdomains();
205
206         cfg80211_world_regdom = rd;
207         cfg80211_regdomain = rd;
208 }
209
210 bool is_world_regdom(const char *alpha2)
211 {
212         if (!alpha2)
213                 return false;
214         if (alpha2[0] == '0' && alpha2[1] == '0')
215                 return true;
216         return false;
217 }
218
219 static bool is_alpha2_set(const char *alpha2)
220 {
221         if (!alpha2)
222                 return false;
223         if (alpha2[0] != 0 && alpha2[1] != 0)
224                 return true;
225         return false;
226 }
227
228 static bool is_alpha_upper(char letter)
229 {
230         /* ASCII A - Z */
231         if (letter >= 65 && letter <= 90)
232                 return true;
233         return false;
234 }
235
236 static bool is_unknown_alpha2(const char *alpha2)
237 {
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;
245 }
246
247 static bool is_an_alpha2(const char *alpha2)
248 {
249         if (!alpha2)
250                 return false;
251         if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
252                 return true;
253         return false;
254 }
255
256 static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
257 {
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;
264 }
265
266 static bool regdom_changed(const char *alpha2)
267 {
268         if (!cfg80211_regdomain)
269                 return true;
270         if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
271                 return false;
272         return true;
273 }
274
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)
278 {
279         char country_env[9 + 2] = "COUNTRY=";
280         char *envp[] = {
281                 country_env,
282                 NULL
283         };
284
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");
291
292         country_env[8] = alpha2[0];
293         country_env[9] = alpha2[1];
294
295         return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
296 }
297
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)
302 {
303         /* All initial requests are respected */
304         if (!last_request)
305                 return 0;
306
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;
328                                 }
329                                 return -EALREADY;
330                         }
331                         /* Two consecutive Country IE hints on the same wiphy */
332                         if (!alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
333                                 return 0;
334                         return -EALREADY;
335                 }
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;
361                 }
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;
385         }
386 }
387
388 /* Used by nl80211 before kmalloc'ing our regulatory domain */
389 bool reg_is_valid_request(const char *alpha2)
390 {
391         if (!last_request)
392                 return false;
393
394         return alpha2_equal(last_request->alpha2, alpha2);
395 }
396
397 /* Sanity check on a regulatory rule */
398 static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
399 {
400         const struct ieee80211_freq_range *freq_range = &rule->freq_range;
401         u32 freq_diff;
402
403         if (freq_range->start_freq_khz == 0 || freq_range->end_freq_khz == 0)
404                 return false;
405
406         if (freq_range->start_freq_khz > freq_range->end_freq_khz)
407                 return false;
408
409         freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
410
411         if (freq_range->max_bandwidth_khz > freq_diff)
412                 return false;
413
414         return true;
415 }
416
417 static bool is_valid_rd(const struct ieee80211_regdomain *rd)
418 {
419         const struct ieee80211_reg_rule *reg_rule = NULL;
420         unsigned int i;
421
422         if (!rd->n_reg_rules)
423                 return false;
424
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;
429         }
430
431         return true;
432 }
433
434 /* Returns value in KHz */
435 static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
436         u32 freq)
437 {
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];
445         }
446         return 0;
447 }
448
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)
452 {
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;
461 }
462
463 /**
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
470  *
471  * Use this function to get the regulatory rule for a specific frequency.
472  */
473 static int freq_reg_info(u32 center_freq, u32 *bandwidth,
474                          const struct ieee80211_reg_rule **reg_rule)
475 {
476         int i;
477         u32 max_bandwidth = 0;
478
479         if (!cfg80211_regdomain)
480                 return -EINVAL;
481
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;
486
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;
495                 }
496         }
497
498         return !max_bandwidth;
499 }
500
501 static void handle_channel(struct ieee80211_channel *chan)
502 {
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;
508
509         r = freq_reg_info(MHZ_TO_KHZ(chan->center_freq),
510                 &max_bandwidth, &reg_rule);
511
512         if (r) {
513                 flags |= IEEE80211_CHAN_DISABLED;
514                 chan->flags = flags;
515                 return;
516         }
517
518         power_rule = &reg_rule->power_rule;
519
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);
529 }
530
531 static void handle_band(struct ieee80211_supported_band *sband)
532 {
533         int i;
534
535         for (i = 0; i < sband->n_channels; i++)
536                 handle_channel(&sband->channels[i]);
537 }
538
539 static void update_all_wiphy_regulatory(enum reg_set_by setby)
540 {
541         struct cfg80211_registered_device *drv;
542
543         list_for_each_entry(drv, &cfg80211_drv_list, list)
544                 wiphy_update_regulatory(&drv->wiphy, setby);
545 }
546
547 void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
548 {
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);
555         }
556 }
557
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)
561 {
562         struct regulatory_request *request;
563         char *rd_alpha2;
564         int r = 0;
565
566         r = ignore_request(wiphy, set_by, (char *) alpha2, rd);
567         if (r)
568                 return r;
569
570         if (rd)
571                 rd_alpha2 = rd->alpha2;
572         else
573                 rd_alpha2 = (char *) alpha2;
574
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;
584
585                 request->alpha2[0] = rd_alpha2[0];
586                 request->alpha2[1] = rd_alpha2[1];
587                 request->initiator = set_by;
588                 request->wiphy = wiphy;
589
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;
603         }
604
605         return r;
606 }
607
608 int regulatory_hint(struct wiphy *wiphy, const char *alpha2,
609         struct ieee80211_regdomain *rd)
610 {
611         int r;
612         BUG_ON(!rd && !alpha2);
613
614         mutex_lock(&cfg80211_drv_mutex);
615
616         r = __regulatory_hint(wiphy, REGDOM_SET_BY_DRIVER, alpha2, rd);
617         if (r || !rd)
618                 goto unlock_and_exit;
619
620         /* If the driver passed a regulatory domain we skipped asking
621          * userspace for one so we can now go ahead and set it */
622         r = set_regdom(rd);
623
624 unlock_and_exit:
625         mutex_unlock(&cfg80211_drv_mutex);
626         return r;
627 }
628 EXPORT_SYMBOL(regulatory_hint);
629
630
631 static void print_rd_rules(const struct ieee80211_regdomain *rd)
632 {
633         unsigned int i;
634         const struct ieee80211_reg_rule *reg_rule = NULL;
635         const struct ieee80211_freq_range *freq_range = NULL;
636         const struct ieee80211_power_rule *power_rule = NULL;
637
638         printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
639                 "(max_antenna_gain, max_eirp)\n");
640
641         for (i = 0; i < rd->n_reg_rules; i++) {
642                 reg_rule = &rd->reg_rules[i];
643                 freq_range = &reg_rule->freq_range;
644                 power_rule = &reg_rule->power_rule;
645
646                 /* There may not be documentation for max antenna gain
647                  * in certain regions */
648                 if (power_rule->max_antenna_gain)
649                         printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
650                                 "(%d mBi, %d mBm)\n",
651                                 freq_range->start_freq_khz,
652                                 freq_range->end_freq_khz,
653                                 freq_range->max_bandwidth_khz,
654                                 power_rule->max_antenna_gain,
655                                 power_rule->max_eirp);
656                 else
657                         printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
658                                 "(N/A, %d mBm)\n",
659                                 freq_range->start_freq_khz,
660                                 freq_range->end_freq_khz,
661                                 freq_range->max_bandwidth_khz,
662                                 power_rule->max_eirp);
663         }
664 }
665
666 static void print_regdomain(const struct ieee80211_regdomain *rd)
667 {
668
669         if (is_world_regdom(rd->alpha2))
670                 printk(KERN_INFO "cfg80211: World regulatory "
671                         "domain updated:\n");
672         else {
673                 if (is_unknown_alpha2(rd->alpha2))
674                         printk(KERN_INFO "cfg80211: Regulatory domain "
675                                 "changed to driver built-in settings "
676                                 "(unknown country)\n");
677                 else
678                         printk(KERN_INFO "cfg80211: Regulatory domain "
679                                 "changed to country: %c%c\n",
680                                 rd->alpha2[0], rd->alpha2[1]);
681         }
682         print_rd_rules(rd);
683 }
684
685 void print_regdomain_info(const struct ieee80211_regdomain *rd)
686 {
687         printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
688                 rd->alpha2[0], rd->alpha2[1]);
689         print_rd_rules(rd);
690 }
691
692 /* Takes ownership of rd only if it doesn't fail */
693 static int __set_regdom(const struct ieee80211_regdomain *rd)
694 {
695         /* Some basic sanity checks first */
696
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;
702         }
703
704         if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
705                         !is_unknown_alpha2(rd->alpha2))
706                 return -EINVAL;
707
708         if (!last_request)
709                 return -EINVAL;
710
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;
715
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 */
720
721         if (WARN_ON(!reg_is_valid_request(rd->alpha2)))
722                 return -EINVAL;
723
724         reset_regdomains();
725
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;
736                 }
737                 break;
738         case REGDOM_SET_BY_COUNTRY_IE: /* Not yet */
739                 WARN_ON(1);
740         default:
741                 return -EOPNOTSUPP;
742         }
743
744         /* Tada! */
745         cfg80211_regdomain = rd;
746
747         return 0;
748 }
749
750
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. Caller must hold cfg80211_drv_mutex */
754 int set_regdom(const struct ieee80211_regdomain *rd)
755 {
756         int r;
757
758         /* Note that this doesn't update the wiphys, this is done below */
759         r = __set_regdom(rd);
760         if (r) {
761                 kfree(rd);
762                 return r;
763         }
764
765         /* This would make this whole thing pointless */
766         BUG_ON(rd != cfg80211_regdomain);
767
768         /* update all wiphys now with the new established regulatory domain */
769         update_all_wiphy_regulatory(last_request->initiator);
770
771         print_regdomain(rd);
772
773         return r;
774 }
775
776 int regulatory_init(void)
777 {
778         int err;
779
780         reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
781         if (IS_ERR(reg_pdev))
782                 return PTR_ERR(reg_pdev);
783
784 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
785         cfg80211_regdomain = static_regdom(ieee80211_regdom);
786
787         printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
788         print_regdomain_info(cfg80211_regdomain);
789         /* The old code still requests for a new regdomain and if
790          * you have CRDA you get it updated, otherwise you get
791          * stuck with the static values. We ignore "EU" code as
792          * that is not a valid ISO / IEC 3166 alpha2 */
793         if (ieee80211_regdom[0] != 'E' || ieee80211_regdom[1] != 'U')
794                 err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE,
795                                         ieee80211_regdom, NULL);
796 #else
797         cfg80211_regdomain = cfg80211_world_regdom;
798
799         err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, "00", NULL);
800         if (err)
801                 printk(KERN_ERR "cfg80211: calling CRDA failed - "
802                        "unable to update world regulatory domain, "
803                        "using static definition\n");
804 #endif
805
806         return 0;
807 }
808
809 void regulatory_exit(void)
810 {
811         mutex_lock(&cfg80211_drv_mutex);
812
813         reset_regdomains();
814
815         kfree(last_request);
816
817         platform_device_unregister(reg_pdev);
818
819         mutex_unlock(&cfg80211_drv_mutex);
820 }