Merge git://git.kernel.org/pub/scm/linux/kernel/git/linville/wireless-2.6
[safe/jmp/linux-2.6] / net / wireless / reg.c
index 8d176a8..87ea60d 100644 (file)
@@ -40,6 +40,7 @@
 #include <net/cfg80211.h>
 #include "core.h"
 #include "reg.h"
+#include "regdb.h"
 #include "nl80211.h"
 
 /* Receipt of information from last regulatory request */
@@ -62,6 +63,16 @@ const struct ieee80211_regdomain *cfg80211_regdomain;
  */
 static const struct ieee80211_regdomain *country_ie_regdomain;
 
+/*
+ * Protects static reg.c components:
+ *     - cfg80211_world_regdom
+ *     - cfg80211_regdom
+ *     - country_ie_regdomain
+ *     - last_request
+ */
+DEFINE_MUTEX(reg_mutex);
+#define assert_reg_lock() WARN_ON(!mutex_is_locked(&reg_mutex))
+
 /* Used to queue up regulatory hints */
 static LIST_HEAD(reg_requests_list);
 static spinlock_t reg_requests_lock;
@@ -113,112 +124,11 @@ static const struct ieee80211_regdomain world_regdom = {
 static const struct ieee80211_regdomain *cfg80211_world_regdom =
        &world_regdom;
 
-#ifdef CONFIG_WIRELESS_OLD_REGULATORY
-static char *ieee80211_regdom = "US";
-#else
 static char *ieee80211_regdom = "00";
-#endif
 
 module_param(ieee80211_regdom, charp, 0444);
 MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
 
-#ifdef CONFIG_WIRELESS_OLD_REGULATORY
-/*
- * We assume 40 MHz bandwidth for the old regulatory work.
- * We make emphasis we are using the exact same frequencies
- * as before
- */
-
-static const struct ieee80211_regdomain us_regdom = {
-       .n_reg_rules = 6,
-       .alpha2 =  "US",
-       .reg_rules = {
-               /* IEEE 802.11b/g, channels 1..11 */
-               REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
-               /* IEEE 802.11a, channel 36 */
-               REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
-               /* IEEE 802.11a, channel 40 */
-               REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
-               /* IEEE 802.11a, channel 44 */
-               REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
-               /* IEEE 802.11a, channels 48..64 */
-               REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
-               /* IEEE 802.11a, channels 149..165, outdoor */
-               REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
-       }
-};
-
-static const struct ieee80211_regdomain jp_regdom = {
-       .n_reg_rules = 3,
-       .alpha2 =  "JP",
-       .reg_rules = {
-               /* IEEE 802.11b/g, channels 1..14 */
-               REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
-               /* IEEE 802.11a, channels 34..48 */
-               REG_RULE(5170-10, 5240+10, 40, 6, 20,
-                       NL80211_RRF_PASSIVE_SCAN),
-               /* IEEE 802.11a, channels 52..64 */
-               REG_RULE(5260-10, 5320+10, 40, 6, 20,
-                       NL80211_RRF_NO_IBSS |
-                       NL80211_RRF_DFS),
-       }
-};
-
-static const struct ieee80211_regdomain eu_regdom = {
-       .n_reg_rules = 6,
-       /*
-        * This alpha2 is bogus, we leave it here just for stupid
-        * backward compatibility
-        */
-       .alpha2 =  "EU",
-       .reg_rules = {
-               /* IEEE 802.11b/g, channels 1..13 */
-               REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
-               /* IEEE 802.11a, channel 36 */
-               REG_RULE(5180-10, 5180+10, 40, 6, 23,
-                       NL80211_RRF_PASSIVE_SCAN),
-               /* IEEE 802.11a, channel 40 */
-               REG_RULE(5200-10, 5200+10, 40, 6, 23,
-                       NL80211_RRF_PASSIVE_SCAN),
-               /* IEEE 802.11a, channel 44 */
-               REG_RULE(5220-10, 5220+10, 40, 6, 23,
-                       NL80211_RRF_PASSIVE_SCAN),
-               /* IEEE 802.11a, channels 48..64 */
-               REG_RULE(5240-10, 5320+10, 40, 6, 20,
-                       NL80211_RRF_NO_IBSS |
-                       NL80211_RRF_DFS),
-               /* IEEE 802.11a, channels 100..140 */
-               REG_RULE(5500-10, 5700+10, 40, 6, 30,
-                       NL80211_RRF_NO_IBSS |
-                       NL80211_RRF_DFS),
-       }
-};
-
-static const struct ieee80211_regdomain *static_regdom(char *alpha2)
-{
-       if (alpha2[0] == 'U' && alpha2[1] == 'S')
-               return &us_regdom;
-       if (alpha2[0] == 'J' && alpha2[1] == 'P')
-               return &jp_regdom;
-       if (alpha2[0] == 'E' && alpha2[1] == 'U')
-               return &eu_regdom;
-       /* Default, as per the old rules */
-       return &us_regdom;
-}
-
-static bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
-{
-       if (rd == &us_regdom || rd == &jp_regdom || rd == &eu_regdom)
-               return true;
-       return false;
-}
-#else
-static inline bool is_old_static_regdom(const struct ieee80211_regdomain *rd)
-{
-       return false;
-}
-#endif
-
 static void reset_regdomains(void)
 {
        /* avoid freeing static information or freeing something twice */
@@ -228,8 +138,6 @@ static void reset_regdomains(void)
                cfg80211_world_regdom = NULL;
        if (cfg80211_regdomain == &world_regdom)
                cfg80211_regdomain = NULL;
-       if (is_old_static_regdom(cfg80211_regdomain))
-               cfg80211_regdomain = NULL;
 
        kfree(cfg80211_regdomain);
        kfree(cfg80211_world_regdom);
@@ -354,6 +262,98 @@ static bool country_ie_integrity_changes(u32 checksum)
        return false;
 }
 
+static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
+                        const struct ieee80211_regdomain *src_regd)
+{
+       struct ieee80211_regdomain *regd;
+       int size_of_regd = 0;
+       unsigned int i;
+
+       size_of_regd = sizeof(struct ieee80211_regdomain) +
+         ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
+
+       regd = kzalloc(size_of_regd, GFP_KERNEL);
+       if (!regd)
+               return -ENOMEM;
+
+       memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
+
+       for (i = 0; i < src_regd->n_reg_rules; i++)
+               memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
+                       sizeof(struct ieee80211_reg_rule));
+
+       *dst_regd = regd;
+       return 0;
+}
+
+#ifdef CONFIG_CFG80211_INTERNAL_REGDB
+struct reg_regdb_search_request {
+       char alpha2[2];
+       struct list_head list;
+};
+
+static LIST_HEAD(reg_regdb_search_list);
+static DEFINE_SPINLOCK(reg_regdb_search_lock);
+
+static void reg_regdb_search(struct work_struct *work)
+{
+       struct reg_regdb_search_request *request;
+       const struct ieee80211_regdomain *curdom, *regdom;
+       int i, r;
+
+       spin_lock(&reg_regdb_search_lock);
+       while (!list_empty(&reg_regdb_search_list)) {
+               request = list_first_entry(&reg_regdb_search_list,
+                                          struct reg_regdb_search_request,
+                                          list);
+               list_del(&request->list);
+
+               for (i=0; i<reg_regdb_size; i++) {
+                       curdom = reg_regdb[i];
+
+                       if (!memcmp(request->alpha2, curdom->alpha2, 2)) {
+                               r = reg_copy_regd(&regdom, curdom);
+                               if (r)
+                                       break;
+                               spin_unlock(&reg_regdb_search_lock);
+                               mutex_lock(&cfg80211_mutex);
+                               set_regdom(regdom);
+                               mutex_unlock(&cfg80211_mutex);
+                               spin_lock(&reg_regdb_search_lock);
+                               break;
+                       }
+               }
+
+               kfree(request);
+       }
+       spin_unlock(&reg_regdb_search_lock);
+}
+
+static DECLARE_WORK(reg_regdb_work, reg_regdb_search);
+
+static void reg_regdb_query(const char *alpha2)
+{
+       struct reg_regdb_search_request *request;
+
+       if (!alpha2)
+               return;
+
+       request = kzalloc(sizeof(struct reg_regdb_search_request), GFP_KERNEL);
+       if (!request)
+               return;
+
+       memcpy(request->alpha2, alpha2, 2);
+
+       spin_lock(&reg_regdb_search_lock);
+       list_add_tail(&request->list, &reg_regdb_search_list);
+       spin_unlock(&reg_regdb_search_lock);
+
+       schedule_work(&reg_regdb_work);
+}
+#else
+static inline void reg_regdb_query(const char *alpha2) {}
+#endif /* CONFIG_CFG80211_INTERNAL_REGDB */
+
 /*
  * This lets us keep regulatory code which is updated on a regulatory
  * basis in userspace.
@@ -373,6 +373,9 @@ static int call_crda(const char *alpha2)
                printk(KERN_INFO "cfg80211: Calling CRDA to update world "
                        "regulatory domain\n");
 
+       /* query internal regulatory database (if it exists) */
+       reg_regdb_query(alpha2);
+
        country_env[8] = alpha2[0];
        country_env[9] = alpha2[1];
 
@@ -1002,7 +1005,7 @@ static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
 
        if (last_request->initiator == NL80211_REGDOM_SET_BY_DRIVER &&
            request_wiphy && request_wiphy == wiphy &&
-           request_wiphy->strict_regulatory) {
+           request_wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY) {
                /*
                 * This gaurantees the driver's requested regulatory domain
                 * will always be used as a base for further regulatory
@@ -1012,7 +1015,6 @@ static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
                        map_regdom_flags(reg_rule->flags) | bw_flags;
                chan->max_antenna_gain = chan->orig_mag =
                        (int) MBI_TO_DBI(power_rule->max_antenna_gain);
-               chan->max_bandwidth = KHZ_TO_MHZ(desired_bw_khz);
                chan->max_power = chan->orig_mpwr =
                        (int) MBM_TO_DBM(power_rule->max_eirp);
                return;
@@ -1021,7 +1023,6 @@ static void handle_channel(struct wiphy *wiphy, enum ieee80211_band band,
        chan->flags = flags | bw_flags | map_regdom_flags(reg_rule->flags);
        chan->max_antenna_gain = min(chan->orig_mag,
                (int) MBI_TO_DBI(power_rule->max_antenna_gain));
-       chan->max_bandwidth = KHZ_TO_MHZ(desired_bw_khz);
        if (chan->orig_mpwr)
                chan->max_power = min(chan->orig_mpwr,
                        (int) MBM_TO_DBM(power_rule->max_eirp));
@@ -1047,13 +1048,13 @@ static bool ignore_reg_update(struct wiphy *wiphy,
        if (!last_request)
                return true;
        if (initiator == NL80211_REGDOM_SET_BY_CORE &&
-                 wiphy->custom_regulatory)
+           wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
                return true;
        /*
         * wiphy->regd will be set once the device has its own
         * desired regulatory domain set
         */
-       if (wiphy->strict_regulatory && !wiphy->regd &&
+       if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY && !wiphy->regd &&
            !is_world_regdom(last_request->alpha2))
                return true;
        return false;
@@ -1061,10 +1062,10 @@ static bool ignore_reg_update(struct wiphy *wiphy,
 
 static void update_all_wiphy_regulatory(enum nl80211_reg_initiator initiator)
 {
-       struct cfg80211_registered_device *drv;
+       struct cfg80211_registered_device *rdev;
 
-       list_for_each_entry(drv, &cfg80211_drv_list, list)
-               wiphy_update_regulatory(&drv->wiphy, initiator);
+       list_for_each_entry(rdev, &cfg80211_rdev_list, list)
+               wiphy_update_regulatory(&rdev->wiphy, initiator);
 }
 
 static void handle_reg_beacon(struct wiphy *wiphy,
@@ -1089,17 +1090,18 @@ static void handle_reg_beacon(struct wiphy *wiphy,
 
        chan->beacon_found = true;
 
+       if (wiphy->flags & WIPHY_FLAG_DISABLE_BEACON_HINTS)
+               return;
+
        chan_before.center_freq = chan->center_freq;
        chan_before.flags = chan->flags;
 
-       if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
-           !(chan->orig_flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
+       if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) {
                chan->flags &= ~IEEE80211_CHAN_PASSIVE_SCAN;
                channel_changed = true;
        }
 
-       if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
-           !(chan->orig_flags & IEEE80211_CHAN_NO_IBSS)) {
+       if (chan->flags & IEEE80211_CHAN_NO_IBSS) {
                chan->flags &= ~IEEE80211_CHAN_NO_IBSS;
                channel_changed = true;
        }
@@ -1159,7 +1161,7 @@ static bool reg_is_world_roaming(struct wiphy *wiphy)
                return true;
        if (last_request &&
            last_request->initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
-           wiphy->custom_regulatory)
+           wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY)
                return true;
        return false;
 }
@@ -1297,7 +1299,7 @@ static void handle_channel_custom(struct wiphy *wiphy,
        struct ieee80211_supported_band *sband;
        struct ieee80211_channel *chan;
 
-       assert_cfg80211_lock();
+       assert_reg_lock();
 
        sband = wiphy->bands[band];
        BUG_ON(chan_idx >= sband->n_channels);
@@ -1322,7 +1324,6 @@ static void handle_channel_custom(struct wiphy *wiphy,
 
        chan->flags |= map_regdom_flags(reg_rule->flags) | bw_flags;
        chan->max_antenna_gain = (int) MBI_TO_DBI(power_rule->max_antenna_gain);
-       chan->max_bandwidth = KHZ_TO_MHZ(desired_bw_khz);
        chan->max_power = (int) MBM_TO_DBM(power_rule->max_eirp);
 }
 
@@ -1344,39 +1345,24 @@ void wiphy_apply_custom_regulatory(struct wiphy *wiphy,
                                   const struct ieee80211_regdomain *regd)
 {
        enum ieee80211_band band;
+       unsigned int bands_set = 0;
 
-       mutex_lock(&cfg80211_mutex);
+       mutex_lock(&reg_mutex);
        for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
-               if (wiphy->bands[band])
-                       handle_band_custom(wiphy, band, regd);
+               if (!wiphy->bands[band])
+                       continue;
+               handle_band_custom(wiphy, band, regd);
+               bands_set++;
        }
-       mutex_unlock(&cfg80211_mutex);
-}
-EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
-
-static int reg_copy_regd(const struct ieee80211_regdomain **dst_regd,
-                        const struct ieee80211_regdomain *src_regd)
-{
-       struct ieee80211_regdomain *regd;
-       int size_of_regd = 0;
-       unsigned int i;
-
-       size_of_regd = sizeof(struct ieee80211_regdomain) +
-         ((src_regd->n_reg_rules + 1) * sizeof(struct ieee80211_reg_rule));
-
-       regd = kzalloc(size_of_regd, GFP_KERNEL);
-       if (!regd)
-               return -ENOMEM;
+       mutex_unlock(&reg_mutex);
 
-       memcpy(regd, src_regd, sizeof(struct ieee80211_regdomain));
-
-       for (i = 0; i < src_regd->n_reg_rules; i++)
-               memcpy(&regd->reg_rules[i], &src_regd->reg_rules[i],
-                       sizeof(struct ieee80211_reg_rule));
-
-       *dst_regd = regd;
-       return 0;
+       /*
+        * no point in calling this if it won't have any effect
+        * on your device's supportd bands.
+        */
+       WARN_ON(!bands_set);
 }
+EXPORT_SYMBOL(wiphy_apply_custom_regulatory);
 
 /*
  * Return value which can be used by ignore_request() to indicate
@@ -1411,7 +1397,7 @@ static int ignore_request(struct wiphy *wiphy,
                        if (last_wiphy != wiphy) {
                                /*
                                 * Two cards with two APs claiming different
-                                * different Country IE alpha2s. We could
+                                * Country IE alpha2s. We could
                                 * intersect them, but that seems unlikely
                                 * to be correct. Reject second one for now.
                                 */
@@ -1430,8 +1416,6 @@ static int ignore_request(struct wiphy *wiphy,
                return REG_INTERSECT;
        case NL80211_REGDOM_SET_BY_DRIVER:
                if (last_request->initiator == NL80211_REGDOM_SET_BY_CORE) {
-                       if (is_old_static_regdom(cfg80211_regdomain))
-                               return 0;
                        if (regdom_changes(pending_request->alpha2))
                                return 0;
                        return -EALREADY;
@@ -1468,8 +1452,7 @@ static int ignore_request(struct wiphy *wiphy,
                                return -EAGAIN;
                }
 
-               if (!is_old_static_regdom(cfg80211_regdomain) &&
-                   !regdom_changes(pending_request->alpha2))
+               if (!regdom_changes(pending_request->alpha2))
                        return -EALREADY;
 
                return 0;
@@ -1490,7 +1473,7 @@ static int ignore_request(struct wiphy *wiphy,
  * Returns zero if all went fine, %-EALREADY if a regulatory domain had
  * already been set or other standard error codes.
  *
- * Caller must hold &cfg80211_mutex
+ * Caller must hold &cfg80211_mutex and &reg_mutex
  */
 static int __regulatory_hint(struct wiphy *wiphy,
                             struct regulatory_request *pending_request)
@@ -1565,6 +1548,7 @@ static void reg_process_hint(struct regulatory_request *reg_request)
        BUG_ON(!reg_request->alpha2);
 
        mutex_lock(&cfg80211_mutex);
+       mutex_lock(&reg_mutex);
 
        if (wiphy_idx_valid(reg_request->wiphy_idx))
                wiphy = wiphy_idx_to_wiphy(reg_request->wiphy_idx);
@@ -1577,9 +1561,11 @@ static void reg_process_hint(struct regulatory_request *reg_request)
 
        r = __regulatory_hint(wiphy, reg_request);
        /* This is required so that the orig_* parameters are saved */
-       if (r == -EALREADY && wiphy && wiphy->strict_regulatory)
+       if (r == -EALREADY && wiphy &&
+           wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY)
                wiphy_update_regulatory(wiphy, reg_request->initiator);
 out:
+       mutex_unlock(&reg_mutex);
        mutex_unlock(&cfg80211_mutex);
 }
 
@@ -1605,9 +1591,13 @@ static void reg_process_pending_hints(void)
 /* Processes beacon hints -- this has nothing to do with country IEs */
 static void reg_process_pending_beacon_hints(void)
 {
-       struct cfg80211_registered_device *drv;
+       struct cfg80211_registered_device *rdev;
        struct reg_beacon *pending_beacon, *tmp;
 
+       /*
+        * No need to hold the reg_mutex here as we just touch wiphys
+        * and do not read or access regulatory variables.
+        */
        mutex_lock(&cfg80211_mutex);
 
        /* This goes through the _pending_ beacon list */
@@ -1624,8 +1614,8 @@ static void reg_process_pending_beacon_hints(void)
                list_del_init(&pending_beacon->list);
 
                /* Applies the beacon hint to current wiphys */
-               list_for_each_entry(drv, &cfg80211_drv_list, list)
-                       wiphy_update_new_beacon(&drv->wiphy, pending_beacon);
+               list_for_each_entry(rdev, &cfg80211_rdev_list, list)
+                       wiphy_update_new_beacon(&rdev->wiphy, pending_beacon);
 
                /* Remembers the beacon hint for new wiphys or reg changes */
                list_add_tail(&pending_beacon->list, &reg_beacon_list);
@@ -1671,6 +1661,13 @@ static int regulatory_hint_core(const char *alpha2)
 
        queue_regulatory_request(request);
 
+       /*
+        * This ensures last_request is populated once modules
+        * come swinging in and calling regulatory hints and
+        * wiphy_apply_custom_regulatory().
+        */
+       flush_scheduled_work();
+
        return 0;
 }
 
@@ -1722,12 +1719,13 @@ int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
 }
 EXPORT_SYMBOL(regulatory_hint);
 
+/* Caller must hold reg_mutex */
 static bool reg_same_country_ie_hint(struct wiphy *wiphy,
                        u32 country_ie_checksum)
 {
        struct wiphy *request_wiphy;
 
-       assert_cfg80211_lock();
+       assert_reg_lock();
 
        if (unlikely(last_request->initiator !=
            NL80211_REGDOM_SET_BY_COUNTRY_IE))
@@ -1750,6 +1748,10 @@ static bool reg_same_country_ie_hint(struct wiphy *wiphy,
        return false;
 }
 
+/*
+ * We hold wdev_lock() here so we cannot hold cfg80211_mutex() and
+ * therefore cannot iterate over the rdev list here.
+ */
 void regulatory_hint_11d(struct wiphy *wiphy,
                        u8 *country_ie,
                        u8 country_ie_len)
@@ -1760,12 +1762,10 @@ void regulatory_hint_11d(struct wiphy *wiphy,
        enum environment_cap env = ENVIRON_ANY;
        struct regulatory_request *request;
 
-       mutex_lock(&cfg80211_mutex);
+       mutex_lock(&reg_mutex);
 
-       if (unlikely(!last_request)) {
-               mutex_unlock(&cfg80211_mutex);
-               return;
-       }
+       if (unlikely(!last_request))
+               goto out;
 
        /* IE len must be evenly divisible by 2 */
        if (country_ie_len & 0x01)
@@ -1791,54 +1791,14 @@ void regulatory_hint_11d(struct wiphy *wiphy,
                env = ENVIRON_OUTDOOR;
 
        /*
-        * We will run this for *every* beacon processed for the BSSID, so
-        * we optimize an early check to exit out early if we don't have to
-        * do anything
+        * We will run this only upon a successful connection on cfg80211.
+        * We leave conflict resolution to the workqueue, where can hold
+        * cfg80211_mutex.
         */
        if (likely(last_request->initiator ==
            NL80211_REGDOM_SET_BY_COUNTRY_IE &&
-           wiphy_idx_valid(last_request->wiphy_idx))) {
-               struct cfg80211_registered_device *drv_last_ie;
-
-               drv_last_ie =
-                       cfg80211_drv_by_wiphy_idx(last_request->wiphy_idx);
-
-               /*
-                * Lets keep this simple -- we trust the first AP
-                * after we intersect with CRDA
-                */
-               if (likely(&drv_last_ie->wiphy == wiphy)) {
-                       /*
-                        * Ignore IEs coming in on this wiphy with
-                        * the same alpha2 and environment cap
-                        */
-                       if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
-                                 alpha2) &&
-                                 env == drv_last_ie->env)) {
-                               goto out;
-                       }
-                       /*
-                        * the wiphy moved on to another BSSID or the AP
-                        * was reconfigured. XXX: We need to deal with the
-                        * case where the user suspends and goes to goes
-                        * to another country, and then gets IEs from an
-                        * AP with different settings
-                        */
-                       goto out;
-               } else {
-                       /*
-                        * Ignore IEs coming in on two separate wiphys with
-                        * the same alpha2 and environment cap
-                        */
-                       if (likely(alpha2_equal(drv_last_ie->country_ie_alpha2,
-                                 alpha2) &&
-                                 env == drv_last_ie->env)) {
-                               goto out;
-                       }
-                       /* We could potentially intersect though */
-                       goto out;
-               }
-       }
+           wiphy_idx_valid(last_request->wiphy_idx)))
+               goto out;
 
        rd = country_ie_2_rd(country_ie, country_ie_len, &checksum);
        if (!rd)
@@ -1873,7 +1833,7 @@ void regulatory_hint_11d(struct wiphy *wiphy,
        request->country_ie_checksum = checksum;
        request->country_ie_env = env;
 
-       mutex_unlock(&cfg80211_mutex);
+       mutex_unlock(&reg_mutex);
 
        queue_regulatory_request(request);
 
@@ -1882,9 +1842,8 @@ void regulatory_hint_11d(struct wiphy *wiphy,
 free_rd_out:
        kfree(rd);
 out:
-       mutex_unlock(&cfg80211_mutex);
+       mutex_unlock(&reg_mutex);
 }
-EXPORT_SYMBOL(regulatory_hint_11d);
 
 static bool freq_is_chan_12_13_14(u16 freq)
 {
@@ -1942,7 +1901,7 @@ static void print_rd_rules(const struct ieee80211_regdomain *rd)
        const struct ieee80211_freq_range *freq_range = NULL;
        const struct ieee80211_power_rule *power_rule = NULL;
 
-       printk(KERN_INFO "\t(start_freq - end_freq @ bandwidth), "
+       printk(KERN_INFO "    (start_freq - end_freq @ bandwidth), "
                "(max_antenna_gain, max_eirp)\n");
 
        for (i = 0; i < rd->n_reg_rules; i++) {
@@ -1955,7 +1914,7 @@ static void print_rd_rules(const struct ieee80211_regdomain *rd)
                 * in certain regions
                 */
                if (power_rule->max_antenna_gain)
-                       printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
+                       printk(KERN_INFO "    (%d KHz - %d KHz @ %d KHz), "
                                "(%d mBi, %d mBm)\n",
                                freq_range->start_freq_khz,
                                freq_range->end_freq_khz,
@@ -1963,7 +1922,7 @@ static void print_rd_rules(const struct ieee80211_regdomain *rd)
                                power_rule->max_antenna_gain,
                                power_rule->max_eirp);
                else
-                       printk(KERN_INFO "\t(%d KHz - %d KHz @ %d KHz), "
+                       printk(KERN_INFO "    (%d KHz - %d KHz @ %d KHz), "
                                "(N/A, %d mBm)\n",
                                freq_range->start_freq_khz,
                                freq_range->end_freq_khz,
@@ -1979,14 +1938,14 @@ static void print_regdomain(const struct ieee80211_regdomain *rd)
 
                if (last_request->initiator ==
                    NL80211_REGDOM_SET_BY_COUNTRY_IE) {
-                       struct cfg80211_registered_device *drv;
-                       drv = cfg80211_drv_by_wiphy_idx(
+                       struct cfg80211_registered_device *rdev;
+                       rdev = cfg80211_rdev_by_wiphy_idx(
                                last_request->wiphy_idx);
-                       if (drv) {
+                       if (rdev) {
                                printk(KERN_INFO "cfg80211: Current regulatory "
                                        "domain updated by AP to: %c%c\n",
-                                       drv->country_ie_alpha2[0],
-                                       drv->country_ie_alpha2[1]);
+                                       rdev->country_ie_alpha2[0],
+                                       rdev->country_ie_alpha2[1]);
                        } else
                                printk(KERN_INFO "cfg80211: Current regulatory "
                                        "domain intersected: \n");
@@ -2047,7 +2006,7 @@ static inline void reg_country_ie_process_debug(
 static int __set_regdom(const struct ieee80211_regdomain *rd)
 {
        const struct ieee80211_regdomain *intersected_rd = NULL;
-       struct cfg80211_registered_device *drv = NULL;
+       struct cfg80211_registered_device *rdev = NULL;
        struct wiphy *request_wiphy;
        /* Some basic sanity checks first */
 
@@ -2075,8 +2034,7 @@ static int __set_regdom(const struct ieee80211_regdomain *rd)
                 * If someone else asked us to change the rd lets only bother
                 * checking if the alpha2 changes if CRDA was already called
                 */
-               if (!is_old_static_regdom(cfg80211_regdomain) &&
-                   !regdom_changes(rd->alpha2))
+               if (!regdom_changes(rd->alpha2))
                        return -EINVAL;
        }
 
@@ -2113,7 +2071,12 @@ static int __set_regdom(const struct ieee80211_regdomain *rd)
                 * driver wanted to the wiphy to deal with conflicts
                 */
 
-               BUG_ON(request_wiphy->regd);
+               /*
+                * Userspace could have sent two replies with only
+                * one kernel request.
+                */
+               if (request_wiphy->regd)
+                       return -EALREADY;
 
                r = reg_copy_regd(&request_wiphy->regd, rd);
                if (r)
@@ -2155,7 +2118,13 @@ static int __set_regdom(const struct ieee80211_regdomain *rd)
         * the country IE rd with what CRDA believes that country should have
         */
 
-       BUG_ON(!country_ie_regdomain);
+       /*
+        * Userspace could have sent two replies with only
+        * one kernel request. By the second reply we would have
+        * already processed and consumed the country_ie_regdomain.
+        */
+       if (!country_ie_regdomain)
+               return -EALREADY;
        BUG_ON(rd == country_ie_regdomain);
 
        /*
@@ -2175,11 +2144,11 @@ static int __set_regdom(const struct ieee80211_regdomain *rd)
        if (!intersected_rd)
                return -EINVAL;
 
-       drv = wiphy_to_dev(request_wiphy);
+       rdev = wiphy_to_dev(request_wiphy);
 
-       drv->country_ie_alpha2[0] = rd->alpha2[0];
-       drv->country_ie_alpha2[1] = rd->alpha2[1];
-       drv->env = last_request->country_ie_env;
+       rdev->country_ie_alpha2[0] = rd->alpha2[0];
+       rdev->country_ie_alpha2[1] = rd->alpha2[1];
+       rdev->env = last_request->country_ie_env;
 
        BUG_ON(intersected_rd == rd);
 
@@ -2204,10 +2173,13 @@ int set_regdom(const struct ieee80211_regdomain *rd)
 
        assert_cfg80211_lock();
 
+       mutex_lock(&reg_mutex);
+
        /* Note that this doesn't update the wiphys, this is done below */
        r = __set_regdom(rd);
        if (r) {
                kfree(rd);
+               mutex_unlock(&reg_mutex);
                return r;
        }
 
@@ -2222,6 +2194,8 @@ int set_regdom(const struct ieee80211_regdomain *rd)
 
        nl80211_send_reg_change_event(last_request);
 
+       mutex_unlock(&reg_mutex);
+
        return r;
 }
 
@@ -2232,16 +2206,20 @@ void reg_device_remove(struct wiphy *wiphy)
 
        assert_cfg80211_lock();
 
+       mutex_lock(&reg_mutex);
+
        kfree(wiphy->regd);
 
        if (last_request)
                request_wiphy = wiphy_idx_to_wiphy(last_request->wiphy_idx);
 
        if (!request_wiphy || request_wiphy != wiphy)
-               return;
+               goto out;
 
        last_request->wiphy_idx = WIPHY_IDX_STALE;
        last_request->country_ie_env = ENVIRON_ANY;
+out:
+       mutex_unlock(&reg_mutex);
 }
 
 int regulatory_init(void)
@@ -2255,27 +2233,10 @@ int regulatory_init(void)
        spin_lock_init(&reg_requests_lock);
        spin_lock_init(&reg_pending_beacons_lock);
 
-#ifdef CONFIG_WIRELESS_OLD_REGULATORY
-       cfg80211_regdomain = static_regdom(ieee80211_regdom);
-
-       printk(KERN_INFO "cfg80211: Using static regulatory domain info\n");
-       print_regdomain_info(cfg80211_regdomain);
-       /*
-        * The old code still requests for a new regdomain and if
-        * you have CRDA you get it updated, otherwise you get
-        * stuck with the static values. Since "EU" is not a valid
-        * ISO / IEC 3166 alpha2 code we can't expect userpace to
-        * give us a regulatory domain for it. We need last_request
-        * iniitalized though so lets just send a request which we
-        * know will be ignored... this crap will be removed once
-        * OLD_REG dies.
-        */
-       err = regulatory_hint_core(ieee80211_regdom);
-#else
        cfg80211_regdomain = cfg80211_world_regdom;
 
-       err = regulatory_hint_core(ieee80211_regdom);
-#endif
+       /* We always try to get an update for the static regdomain */
+       err = regulatory_hint_core(cfg80211_regdomain->alpha2);
        if (err) {
                if (err == -ENOMEM)
                        return err;
@@ -2294,6 +2255,13 @@ int regulatory_init(void)
 #endif
        }
 
+       /*
+        * Finally, if the user set the module parameter treat it
+        * as a user hint.
+        */
+       if (!is_world_regdom(ieee80211_regdom))
+               regulatory_hint_user(ieee80211_regdom);
+
        return 0;
 }
 
@@ -2305,6 +2273,7 @@ void regulatory_exit(void)
        cancel_work_sync(&reg_work);
 
        mutex_lock(&cfg80211_mutex);
+       mutex_lock(&reg_mutex);
 
        reset_regdomains();
 
@@ -2343,5 +2312,6 @@ void regulatory_exit(void)
        }
        spin_unlock(&reg_requests_lock);
 
+       mutex_unlock(&reg_mutex);
        mutex_unlock(&cfg80211_mutex);
 }