[ARM] S3C6410: Add helper for setting SDHCI device information
[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 list_head list;
48         struct wiphy *wiphy;
49         int granted;
50         enum reg_set_by initiator;
51         char alpha2[2];
52 };
53
54 static LIST_HEAD(regulatory_requests);
55 DEFINE_MUTEX(cfg80211_reg_mutex);
56
57 /* To trigger userspace events */
58 static struct platform_device *reg_pdev;
59
60 /* Keep the ordering from large to small */
61 static u32 supported_bandwidths[] = {
62         MHZ_TO_KHZ(40),
63         MHZ_TO_KHZ(20),
64 };
65
66 static struct list_head regulatory_requests;
67
68 /* Central wireless core regulatory domains, we only need two,
69  * the current one and a world regulatory domain in case we have no
70  * information to give us an alpha2 */
71 static const struct ieee80211_regdomain *cfg80211_regdomain;
72
73 /* We keep a static world regulatory domain in case of the absence of CRDA */
74 static const struct ieee80211_regdomain world_regdom = {
75         .n_reg_rules = 1,
76         .alpha2 =  "00",
77         .reg_rules = {
78                 REG_RULE(2412-10, 2462+10, 40, 6, 20,
79                         NL80211_RRF_PASSIVE_SCAN |
80                         NL80211_RRF_NO_IBSS),
81         }
82 };
83
84 static const struct ieee80211_regdomain *cfg80211_world_regdom =
85         &world_regdom;
86
87 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
88 static char *ieee80211_regdom = "US";
89 module_param(ieee80211_regdom, charp, 0444);
90 MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
91
92 /* We assume 40 MHz bandwidth for the old regulatory work.
93  * We make emphasis we are using the exact same frequencies
94  * as before */
95
96 static const struct ieee80211_regdomain us_regdom = {
97         .n_reg_rules = 6,
98         .alpha2 =  "US",
99         .reg_rules = {
100                 /* IEEE 802.11b/g, channels 1..11 */
101                 REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
102                 /* IEEE 802.11a, channel 36 */
103                 REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
104                 /* IEEE 802.11a, channel 40 */
105                 REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
106                 /* IEEE 802.11a, channel 44 */
107                 REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
108                 /* IEEE 802.11a, channels 48..64 */
109                 REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
110                 /* IEEE 802.11a, channels 149..165, outdoor */
111                 REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
112         }
113 };
114
115 static const struct ieee80211_regdomain jp_regdom = {
116         .n_reg_rules = 3,
117         .alpha2 =  "JP",
118         .reg_rules = {
119                 /* IEEE 802.11b/g, channels 1..14 */
120                 REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
121                 /* IEEE 802.11a, channels 34..48 */
122                 REG_RULE(5170-10, 5240+10, 40, 6, 20,
123                         NL80211_RRF_PASSIVE_SCAN),
124                 /* IEEE 802.11a, channels 52..64 */
125                 REG_RULE(5260-10, 5320+10, 40, 6, 20,
126                         NL80211_RRF_NO_IBSS |
127                         NL80211_RRF_DFS),
128         }
129 };
130
131 static const struct ieee80211_regdomain eu_regdom = {
132         .n_reg_rules = 6,
133         /* This alpha2 is bogus, we leave it here just for stupid
134          * backward compatibility */
135         .alpha2 =  "EU",
136         .reg_rules = {
137                 /* IEEE 802.11b/g, channels 1..13 */
138                 REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
139                 /* IEEE 802.11a, channel 36 */
140                 REG_RULE(5180-10, 5180+10, 40, 6, 23,
141                         NL80211_RRF_PASSIVE_SCAN),
142                 /* IEEE 802.11a, channel 40 */
143                 REG_RULE(5200-10, 5200+10, 40, 6, 23,
144                         NL80211_RRF_PASSIVE_SCAN),
145                 /* IEEE 802.11a, channel 44 */
146                 REG_RULE(5220-10, 5220+10, 40, 6, 23,
147                         NL80211_RRF_PASSIVE_SCAN),
148                 /* IEEE 802.11a, channels 48..64 */
149                 REG_RULE(5240-10, 5320+10, 40, 6, 20,
150                         NL80211_RRF_NO_IBSS |
151                         NL80211_RRF_DFS),
152                 /* IEEE 802.11a, channels 100..140 */
153                 REG_RULE(5500-10, 5700+10, 40, 6, 30,
154                         NL80211_RRF_NO_IBSS |
155                         NL80211_RRF_DFS),
156         }
157 };
158
159 static const struct ieee80211_regdomain *static_regdom(char *alpha2)
160 {
161         if (alpha2[0] == 'U' && alpha2[1] == 'S')
162                 return &us_regdom;
163         if (alpha2[0] == 'J' && alpha2[1] == 'P')
164                 return &jp_regdom;
165         if (alpha2[0] == 'E' && alpha2[1] == 'U')
166                 return &eu_regdom;
167         /* Default, as per the old rules */
168         return &us_regdom;
169 }
170
171 static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
172 {
173         if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
174                 return true;
175         return false;
176 }
177 #else
178 static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
179 {
180         return false;
181 }
182 #endif
183
184 static void reset_regdomains(void)
185 {
186         /* avoid freeing static information or freeing something twice */
187         if (cfg80211_regdomain == cfg80211_world_regdom)
188                 cfg80211_regdomain = NULL;
189         if (cfg80211_world_regdom == &world_regdom)
190                 cfg80211_world_regdom = NULL;
191         if (cfg80211_regdomain == &world_regdom)
192                 cfg80211_regdomain = NULL;
193         if (is_old_static_regdom(cfg80211_regdomain))
194                 cfg80211_regdomain = NULL;
195
196         kfree(cfg80211_regdomain);
197         kfree(cfg80211_world_regdom);
198
199         cfg80211_world_regdom = &world_regdom;
200         cfg80211_regdomain = NULL;
201 }
202
203 /* Dynamic world regulatory domain requested by the wireless
204  * core upon initialization */
205 static void update_world_regdomain(const struct ieee80211_regdomain *rd)
206 {
207         BUG_ON(list_empty(&regulatory_requests));
208
209         reset_regdomains();
210
211         cfg80211_world_regdom = rd;
212         cfg80211_regdomain = rd;
213 }
214
215 bool is_world_regdom(const char *alpha2)
216 {
217         if (!alpha2)
218                 return false;
219         if (alpha2[0] == '0' && alpha2[1] == '0')
220                 return true;
221         return false;
222 }
223
224 static bool is_alpha2_set(const char *alpha2)
225 {
226         if (!alpha2)
227                 return false;
228         if (alpha2[0] != 0 && alpha2[1] != 0)
229                 return true;
230         return false;
231 }
232
233 static bool is_alpha_upper(char letter)
234 {
235         /* ASCII A - Z */
236         if (letter >= 65 && letter <= 90)
237                 return true;
238         return false;
239 }
240
241 static bool is_unknown_alpha2(const char *alpha2)
242 {
243         if (!alpha2)
244                 return false;
245         /* Special case where regulatory domain was built by driver
246          * but a specific alpha2 cannot be determined */
247         if (alpha2[0] == '9' && alpha2[1] == '9')
248                 return true;
249         return false;
250 }
251
252 static bool is_an_alpha2(const char *alpha2)
253 {
254         if (!alpha2)
255                 return false;
256         if (is_alpha_upper(alpha2[0]) && is_alpha_upper(alpha2[1]))
257                 return true;
258         return false;
259 }
260
261 static bool alpha2_equal(const char *alpha2_x, const char *alpha2_y)
262 {
263         if (!alpha2_x || !alpha2_y)
264                 return false;
265         if (alpha2_x[0] == alpha2_y[0] &&
266                 alpha2_x[1] == alpha2_y[1])
267                 return true;
268         return false;
269 }
270
271 static bool regdom_changed(const char *alpha2)
272 {
273         if (!cfg80211_regdomain)
274                 return true;
275         if (alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
276                 return false;
277         return true;
278 }
279
280 /* This lets us keep regulatory code which is updated on a regulatory
281  * basis in userspace. */
282 static int call_crda(const char *alpha2)
283 {
284         char country_env[9 + 2] = "COUNTRY=";
285         char *envp[] = {
286                 country_env,
287                 NULL
288         };
289
290         if (!is_world_regdom((char *) alpha2))
291                 printk(KERN_INFO "cfg80211: Calling CRDA for country: %c%c\n",
292                         alpha2[0], alpha2[1]);
293         else
294                 printk(KERN_INFO "cfg80211: Calling CRDA to update world "
295                         "regulatory domain\n");
296
297         country_env[8] = alpha2[0];
298         country_env[9] = alpha2[1];
299
300         return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, envp);
301 }
302
303 /* This has the logic which determines when a new request
304  * should be ignored. */
305 static int ignore_request(struct wiphy *wiphy, enum reg_set_by set_by,
306         char *alpha2, struct ieee80211_regdomain *rd)
307 {
308         struct regulatory_request *last_request = NULL;
309
310         /* All initial requests are respected */
311         if (list_empty(&regulatory_requests))
312                 return 0;
313
314         last_request = list_first_entry(&regulatory_requests,
315                 struct regulatory_request, list);
316
317         switch (set_by) {
318         case REGDOM_SET_BY_INIT:
319                 return -EINVAL;
320         case REGDOM_SET_BY_CORE:
321                 /* Always respect new wireless core hints, should only
322                  * come in for updating the world regulatory domain at init
323                  * anyway */
324                 return 0;
325         case REGDOM_SET_BY_COUNTRY_IE:
326                 if (last_request->initiator == set_by) {
327                         if (last_request->wiphy != wiphy) {
328                                 /* Two cards with two APs claiming different
329                                  * different Country IE alpha2s!
330                                  * You're special!! */
331                                 if (!alpha2_equal(last_request->alpha2,
332                                                 cfg80211_regdomain->alpha2)) {
333                                         /* XXX: Deal with conflict, consider
334                                          * building a new one out of the
335                                          * intersection */
336                                         WARN_ON(1);
337                                         return -EOPNOTSUPP;
338                                 }
339                                 return -EALREADY;
340                         }
341                         /* Two consecutive Country IE hints on the same wiphy */
342                         if (!alpha2_equal(cfg80211_regdomain->alpha2, alpha2))
343                                 return 0;
344                         return -EALREADY;
345                 }
346                 if (WARN_ON(!is_alpha2_set(alpha2) || !is_an_alpha2(alpha2)),
347                                 "Invalid Country IE regulatory hint passed "
348                                 "to the wireless core\n")
349                         return -EINVAL;
350                 /* We ignore Country IE hints for now, as we haven't yet
351                  * added the dot11MultiDomainCapabilityEnabled flag
352                  * for wiphys */
353                 return 1;
354         case REGDOM_SET_BY_DRIVER:
355                 BUG_ON(!wiphy);
356                 if (last_request->initiator == set_by) {
357                         /* Two separate drivers hinting different things,
358                          * this is possible if you have two devices present
359                          * on a system with different EEPROM regulatory
360                          * readings. XXX: Do intersection, we support only
361                          * the first regulatory hint for now */
362                         if (last_request->wiphy != wiphy)
363                                 return -EALREADY;
364                         if (rd)
365                                 return -EALREADY;
366                         /* Driver should not be trying to hint different
367                          * regulatory domains! */
368                         BUG_ON(!alpha2_equal(alpha2,
369                                         cfg80211_regdomain->alpha2));
370                         return -EALREADY;
371                 }
372                 if (last_request->initiator == REGDOM_SET_BY_CORE)
373                         return 0;
374                 /* XXX: Handle intersection, and add the
375                  * dot11MultiDomainCapabilityEnabled flag to wiphy. For now
376                  * we assume the driver has this set to false, following the
377                  * 802.11d dot11MultiDomainCapabilityEnabled documentation */
378                 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
379                         return 0;
380                 return 0;
381         case REGDOM_SET_BY_USER:
382                 if (last_request->initiator == set_by ||
383                                 last_request->initiator == REGDOM_SET_BY_CORE)
384                         return 0;
385                 /* Drivers can use their wiphy's reg_notifier()
386                  * to override any information */
387                 if (last_request->initiator == REGDOM_SET_BY_DRIVER)
388                         return 0;
389                 /* XXX: Handle intersection */
390                 if (last_request->initiator == REGDOM_SET_BY_COUNTRY_IE)
391                         return -EOPNOTSUPP;
392                 return 0;
393         default:
394                 return -EINVAL;
395         }
396 }
397
398 static bool __reg_is_valid_request(const char *alpha2,
399         struct regulatory_request **request)
400 {
401         struct regulatory_request *req;
402         if (list_empty(&regulatory_requests))
403                 return false;
404         list_for_each_entry(req, &regulatory_requests, list) {
405                 if (alpha2_equal(req->alpha2, alpha2)) {
406                         *request = req;
407                         return true;
408                 }
409         }
410         return false;
411 }
412
413 /* Used by nl80211 before kmalloc'ing our regulatory domain */
414 bool reg_is_valid_request(const char *alpha2)
415 {
416         struct regulatory_request *request = NULL;
417         return  __reg_is_valid_request(alpha2, &request);
418 }
419
420 /* Sanity check on a regulatory rule */
421 static bool is_valid_reg_rule(const struct ieee80211_reg_rule *rule)
422 {
423         const struct ieee80211_freq_range *freq_range = &rule->freq_range;
424         u32 freq_diff;
425
426         if (freq_range->start_freq_khz == 0 || freq_range->end_freq_khz == 0)
427                 return false;
428
429         if (freq_range->start_freq_khz > freq_range->end_freq_khz)
430                 return false;
431
432         freq_diff = freq_range->end_freq_khz - freq_range->start_freq_khz;
433
434         if (freq_range->max_bandwidth_khz > freq_diff)
435                 return false;
436
437         return true;
438 }
439
440 static bool is_valid_rd(const struct ieee80211_regdomain *rd)
441 {
442         const struct ieee80211_reg_rule *reg_rule = NULL;
443         unsigned int i;
444
445         if (!rd->n_reg_rules)
446                 return false;
447
448         for (i = 0; i < rd->n_reg_rules; i++) {
449                 reg_rule = &rd->reg_rules[i];
450                 if (!is_valid_reg_rule(reg_rule))
451                         return false;
452         }
453
454         return true;
455 }
456
457 /* Returns value in KHz */
458 static u32 freq_max_bandwidth(const struct ieee80211_freq_range *freq_range,
459         u32 freq)
460 {
461         unsigned int i;
462         for (i = 0; i < ARRAY_SIZE(supported_bandwidths); i++) {
463                 u32 start_freq_khz = freq - supported_bandwidths[i]/2;
464                 u32 end_freq_khz = freq + supported_bandwidths[i]/2;
465                 if (start_freq_khz >= freq_range->start_freq_khz &&
466                         end_freq_khz <= freq_range->end_freq_khz)
467                         return supported_bandwidths[i];
468         }
469         return 0;
470 }
471
472 /* XXX: add support for the rest of enum nl80211_reg_rule_flags, we may
473  * want to just have the channel structure use these */
474 static u32 map_regdom_flags(u32 rd_flags)
475 {
476         u32 channel_flags = 0;
477         if (rd_flags & NL80211_RRF_PASSIVE_SCAN)
478                 channel_flags |= IEEE80211_CHAN_PASSIVE_SCAN;
479         if (rd_flags & NL80211_RRF_NO_IBSS)
480                 channel_flags |= IEEE80211_CHAN_NO_IBSS;
481         if (rd_flags & NL80211_RRF_DFS)
482                 channel_flags |= IEEE80211_CHAN_RADAR;
483         return channel_flags;
484 }
485
486 /**
487  * freq_reg_info - get regulatory information for the given frequency
488  * @center_freq: Frequency in KHz for which we want regulatory information for
489  * @bandwidth: the bandwidth requirement you have in KHz, if you do not have one
490  *      you can set this to 0. If this frequency is allowed we then set
491  *      this value to the maximum allowed bandwidth.
492  * @reg_rule: the regulatory rule which we have for this frequency
493  *
494  * Use this function to get the regulatory rule for a specific frequency.
495  */
496 static int freq_reg_info(u32 center_freq, u32 *bandwidth,
497                          const struct ieee80211_reg_rule **reg_rule)
498 {
499         int i;
500         u32 max_bandwidth = 0;
501
502         if (!cfg80211_regdomain)
503                 return -EINVAL;
504
505         for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
506                 const struct ieee80211_reg_rule *rr;
507                 const struct ieee80211_freq_range *fr = NULL;
508                 const struct ieee80211_power_rule *pr = NULL;
509
510                 rr = &cfg80211_regdomain->reg_rules[i];
511                 fr = &rr->freq_range;
512                 pr = &rr->power_rule;
513                 max_bandwidth = freq_max_bandwidth(fr, center_freq);
514                 if (max_bandwidth && *bandwidth <= max_bandwidth) {
515                         *reg_rule = rr;
516                         *bandwidth = max_bandwidth;
517                         break;
518                 }
519         }
520
521         return !max_bandwidth;
522 }
523
524 static void handle_channel(struct ieee80211_channel *chan)
525 {
526         int r;
527         u32 flags = chan->orig_flags;
528         u32 max_bandwidth = 0;
529         const struct ieee80211_reg_rule *reg_rule = NULL;
530         const struct ieee80211_power_rule *power_rule = NULL;
531
532         r = freq_reg_info(MHZ_TO_KHZ(chan->center_freq),
533                 &max_bandwidth, &reg_rule);
534
535         if (r) {
536                 flags |= IEEE80211_CHAN_DISABLED;
537                 chan->flags = flags;
538                 return;
539         }
540
541         power_rule = &reg_rule->power_rule;
542
543         chan->flags = flags | map_regdom_flags(reg_rule->flags);
544         chan->max_antenna_gain = min(chan->orig_mag,
545                 (int) MBI_TO_DBI(power_rule->max_antenna_gain));
546         chan->max_bandwidth = KHZ_TO_MHZ(max_bandwidth);
547         if (chan->orig_mpwr)
548                 chan->max_power = min(chan->orig_mpwr,
549                         (int) MBM_TO_DBM(power_rule->max_eirp));
550         else
551                 chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
552 }
553
554 static void handle_band(struct ieee80211_supported_band *sband)
555 {
556         int i;
557
558         for (i = 0; i < sband->n_channels; i++)
559                 handle_channel(&sband->channels[i]);
560 }
561
562 static void update_all_wiphy_regulatory(enum reg_set_by setby)
563 {
564         struct cfg80211_registered_device *drv;
565
566         list_for_each_entry(drv, &cfg80211_drv_list, list)
567                 wiphy_update_regulatory(&drv->wiphy, setby);
568 }
569
570 void wiphy_update_regulatory(struct wiphy *wiphy, enum reg_set_by setby)
571 {
572         enum ieee80211_band band;
573         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
574                 if (wiphy->bands[band])
575                         handle_band(wiphy->bands[band]);
576                 if (wiphy->reg_notifier)
577                         wiphy->reg_notifier(wiphy, setby);
578         }
579 }
580
581 /* Caller must hold &cfg80211_drv_mutex */
582 int __regulatory_hint(struct wiphy *wiphy, enum reg_set_by set_by,
583                       const char *alpha2, struct ieee80211_regdomain *rd)
584 {
585         struct regulatory_request *request;
586         char *rd_alpha2;
587         int r = 0;
588
589         r = ignore_request(wiphy, set_by, (char *) alpha2, rd);
590         if (r)
591                 return r;
592
593         if (rd)
594                 rd_alpha2 = rd->alpha2;
595         else
596                 rd_alpha2 = (char *) alpha2;
597
598         switch (set_by) {
599         case REGDOM_SET_BY_CORE:
600         case REGDOM_SET_BY_COUNTRY_IE:
601         case REGDOM_SET_BY_DRIVER:
602         case REGDOM_SET_BY_USER:
603                 request = kzalloc(sizeof(struct regulatory_request),
604                         GFP_KERNEL);
605                 if (!request)
606                         return -ENOMEM;
607
608                 request->alpha2[0] = rd_alpha2[0];
609                 request->alpha2[1] = rd_alpha2[1];
610                 request->initiator = set_by;
611                 request->wiphy = wiphy;
612
613                 list_add_tail(&request->list, &regulatory_requests);
614                 if (rd)
615                         break;
616                 r = call_crda(alpha2);
617 #ifndef CONFIG_WIRELESS_OLD_REGULATORY
618                 if (r)
619                         printk(KERN_ERR "cfg80211: Failed calling CRDA\n");
620 #endif
621                 break;
622         default:
623                 r = -ENOTSUPP;
624                 break;
625         }
626
627         return r;
628 }
629
630 /* If rd is not NULL and if this call fails the caller must free it */
631 int regulatory_hint(struct wiphy *wiphy, const char *alpha2,
632         struct ieee80211_regdomain *rd)
633 {
634         int r;
635         BUG_ON(!rd && !alpha2);
636
637         mutex_lock(&cfg80211_drv_mutex);
638
639         r = __regulatory_hint(wiphy, REGDOM_SET_BY_DRIVER, alpha2, rd);
640         if (r || !rd)
641                 goto unlock_and_exit;
642
643         /* If the driver passed a regulatory domain we skipped asking
644          * userspace for one so we can now go ahead and set it */
645         r = set_regdom(rd);
646
647 unlock_and_exit:
648         mutex_unlock(&cfg80211_drv_mutex);
649         return r;
650 }
651 EXPORT_SYMBOL(regulatory_hint);
652
653
654 static void print_rd_rules(const struct ieee80211_regdomain *rd)
655 {
656         unsigned int i;
657         const struct ieee80211_reg_rule *reg_rule = NULL;
658         const struct ieee80211_freq_range *freq_range = NULL;
659         const struct ieee80211_power_rule *power_rule = NULL;
660
661         printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
662                 "(max_antenna_gain, max_eirp)\n");
663
664         for (i = 0; i < rd->n_reg_rules; i++) {
665                 reg_rule = &rd->reg_rules[i];
666                 freq_range = &reg_rule->freq_range;
667                 power_rule = &reg_rule->power_rule;
668
669                 /* There may not be documentation for max antenna gain
670                  * in certain regions */
671                 if (power_rule->max_antenna_gain)
672                         printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
673                                 "(%d mBi, %d mBm)\n",
674                                 freq_range->start_freq_khz,
675                                 freq_range->end_freq_khz,
676                                 freq_range->max_bandwidth_khz,
677                                 power_rule->max_antenna_gain,
678                                 power_rule->max_eirp);
679                 else
680                         printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
681                                 "(N/A, %d mBm)\n",
682                                 freq_range->start_freq_khz,
683                                 freq_range->end_freq_khz,
684                                 freq_range->max_bandwidth_khz,
685                                 power_rule->max_eirp);
686         }
687 }
688
689 static void print_regdomain(const struct ieee80211_regdomain *rd)
690 {
691
692         if (is_world_regdom(rd->alpha2))
693                 printk(KERN_INFO "cfg80211: World regulatory "
694                         "domain updated:\n");
695         else {
696                 if (is_unknown_alpha2(rd->alpha2))
697                         printk(KERN_INFO "cfg80211: Regulatory domain "
698                                 "changed to driver built-in settings "
699                                 "(unknown country)\n");
700                 else
701                         printk(KERN_INFO "cfg80211: Regulatory domain "
702                                 "changed to country: %c%c\n",
703                                 rd->alpha2[0], rd->alpha2[1]);
704         }
705         print_rd_rules(rd);
706 }
707
708 void print_regdomain_info(const struct ieee80211_regdomain *rd)
709 {
710         printk(KERN_INFO "cfg80211: Regulatory domain: %c%c\n",
711                 rd->alpha2[0], rd->alpha2[1]);
712         print_rd_rules(rd);
713 }
714
715 static int __set_regdom(const struct ieee80211_regdomain *rd)
716 {
717         struct regulatory_request *request = NULL;
718
719         /* Some basic sanity checks first */
720
721         if (is_world_regdom(rd->alpha2)) {
722                 if (WARN_ON(!__reg_is_valid_request(rd->alpha2, &request)))
723                         return -EINVAL;
724                 update_world_regdomain(rd);
725                 return 0;
726         }
727
728         if (!is_alpha2_set(rd->alpha2) && !is_an_alpha2(rd->alpha2) &&
729                         !is_unknown_alpha2(rd->alpha2))
730                 return -EINVAL;
731
732         if (list_empty(&regulatory_requests))
733                 return -EINVAL;
734
735         /* allow overriding the static definitions if CRDA is present */
736         if (!is_old_static_regdom(cfg80211_regdomain) &&
737             !regdom_changed(rd->alpha2))
738                 return -EINVAL;
739
740         /* Now lets set the regulatory domain, update all driver channels
741          * and finally inform them of what we have done, in case they want
742          * to review or adjust their own settings based on their own
743          * internal EEPROM data */
744
745         if (WARN_ON(!__reg_is_valid_request(rd->alpha2, &request)))
746                 return -EINVAL;
747
748         reset_regdomains();
749
750         /* Country IE parsing coming soon */
751         switch (request->initiator) {
752         case REGDOM_SET_BY_CORE:
753         case REGDOM_SET_BY_DRIVER:
754         case REGDOM_SET_BY_USER:
755                 if (!is_valid_rd(rd)) {
756                         printk(KERN_ERR "cfg80211: Invalid "
757                                 "regulatory domain detected:\n");
758                         print_regdomain_info(rd);
759                         return -EINVAL;
760                 }
761                 break;
762         case REGDOM_SET_BY_COUNTRY_IE: /* Not yet */
763                 WARN_ON(1);
764         default:
765                 return -EOPNOTSUPP;
766         }
767
768         /* Tada! */
769         cfg80211_regdomain = rd;
770         request->granted = 1;
771
772         return 0;
773 }
774
775
776 /* Use this call to set the current regulatory domain. Conflicts with
777  * multiple drivers can be ironed out later. Caller must've already
778  * kmalloc'd the rd structure. If this calls fails you should kfree()
779  * the passed rd. Caller must hold cfg80211_drv_mutex */
780 int set_regdom(const struct ieee80211_regdomain *rd)
781 {
782         struct regulatory_request *this_request = NULL, *prev_request = NULL;
783         int r;
784
785         if (!list_empty(&regulatory_requests))
786                 prev_request = list_first_entry(&regulatory_requests,
787                         struct regulatory_request, list);
788
789         /* Note that this doesn't update the wiphys, this is done below */
790         r = __set_regdom(rd);
791         if (r)
792                 return r;
793
794         BUG_ON((!__reg_is_valid_request(rd->alpha2, &this_request)));
795
796         /* The initial standard core update of the world regulatory domain, no
797          * need to keep that request info around if it didn't fail. */
798         if (is_world_regdom(rd->alpha2) &&
799                         this_request->initiator == REGDOM_SET_BY_CORE &&
800                         this_request->granted) {
801                 list_del(&this_request->list);
802                 kfree(this_request);
803                 this_request = NULL;
804         }
805
806         /* Remove old requests, we only leave behind the last one */
807         if (prev_request) {
808                 list_del(&prev_request->list);
809                 kfree(prev_request);
810                 prev_request = NULL;
811         }
812
813         /* This would make this whole thing pointless */
814         BUG_ON(rd != cfg80211_regdomain);
815
816         /* update all wiphys now with the new established regulatory domain */
817         update_all_wiphy_regulatory(this_request->initiator);
818
819         print_regdomain(rd);
820
821         return r;
822 }
823
824 int regulatory_init(void)
825 {
826         int err;
827
828         reg_pdev = platform_device_register_simple("regulatory", 0, NULL, 0);
829         if (IS_ERR(reg_pdev))
830                 return PTR_ERR(reg_pdev);
831
832 #ifdef CONFIG_WIRELESS_OLD_REGULATORY
833         cfg80211_regdomain = static_regdom(ieee80211_regdom);
834
835         printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
836         print_regdomain_info(cfg80211_regdomain);
837         /* The old code still requests for a new regdomain and if
838          * you have CRDA you get it updated, otherwise you get
839          * stuck with the static values. We ignore "EU" code as
840          * that is not a valid ISO / IEC 3166 alpha2 */
841         if (ieee80211_regdom[0] != 'E' && ieee80211_regdom[1] != 'U')
842                 err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE,
843                                         ieee80211_regdom, NULL);
844 #else
845         cfg80211_regdomain = cfg80211_world_regdom;
846
847         err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, "00", NULL);
848         if (err)
849                 printk(KERN_ERR "cfg80211: calling CRDA failed - "
850                        "unable to update world regulatory domain, "
851                        "using static definition\n");
852 #endif
853
854         return 0;
855 }
856
857 void regulatory_exit(void)
858 {
859         struct regulatory_request *req, *req_tmp;
860
861         mutex_lock(&cfg80211_drv_mutex);
862
863         reset_regdomains();
864
865         list_for_each_entry_safe(req, req_tmp, &regulatory_requests, list) {
866                 list_del(&req->list);
867                 kfree(req);
868         }
869         platform_device_unregister(reg_pdev);
870
871         mutex_unlock(&cfg80211_drv_mutex);
872 }