mac80211: unify config_interface and bss_info_changed
[safe/jmp/linux-2.6] / drivers / net / wireless / b43 / main.c
index 7c23aa8..2615aaf 100644 (file)
@@ -4,7 +4,7 @@
 
   Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>
   Copyright (c) 2005 Stefano Brivio <stefano.brivio@polimi.it>
-  Copyright (c) 2005, 2006 Michael Buesch <mb@bu3sch.de>
+  Copyright (c) 2005-2009 Michael Buesch <mb@bu3sch.de>
   Copyright (c) 2005 Danny van Dyk <kugelfang@gentoo.org>
   Copyright (c) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
 
@@ -33,7 +33,6 @@
 #include <linux/moduleparam.h>
 #include <linux/if_arp.h>
 #include <linux/etherdevice.h>
-#include <linux/version.h>
 #include <linux/firmware.h>
 #include <linux/wireless.h>
 #include <linux/workqueue.h>
@@ -45,8 +44,9 @@
 #include "b43.h"
 #include "main.h"
 #include "debugfs.h"
-#include "phy.h"
-#include "nphy.h"
+#include "phy_common.h"
+#include "phy_g.h"
+#include "phy_n.h"
 #include "dma.h"
 #include "pio.h"
 #include "sysfs.h"
@@ -88,6 +88,10 @@ static int modparam_btcoex = 1;
 module_param_named(btcoex, modparam_btcoex, int, 0444);
 MODULE_PARM_DESC(btcoex, "Enable Bluetooth coexistance (default on)");
 
+int b43_modparam_verbose = B43_VERBOSITY_DEFAULT;
+module_param_named(verbose, b43_modparam_verbose, int, 0644);
+MODULE_PARM_DESC(verbose, "Log message verbosity: 0=error, 1=warn, 2=info(default), 3=debug");
+
 
 static const struct ssb_device_id b43_ssb_tbl[] = {
        SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 5),
@@ -97,6 +101,8 @@ static const struct ssb_device_id b43_ssb_tbl[] = {
        SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 10),
        SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 11),
        SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 13),
+       SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 15),
+       SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 16),
        SSB_DEVTABLE_END
 };
 
@@ -298,6 +304,8 @@ void b43info(struct b43_wl *wl, const char *fmt, ...)
 {
        va_list args;
 
+       if (b43_modparam_verbose < B43_VERBOSITY_INFO)
+               return;
        if (!b43_ratelimit(wl))
                return;
        va_start(args, fmt);
@@ -311,6 +319,8 @@ void b43err(struct b43_wl *wl, const char *fmt, ...)
 {
        va_list args;
 
+       if (b43_modparam_verbose < B43_VERBOSITY_ERROR)
+               return;
        if (!b43_ratelimit(wl))
                return;
        va_start(args, fmt);
@@ -324,6 +334,8 @@ void b43warn(struct b43_wl *wl, const char *fmt, ...)
 {
        va_list args;
 
+       if (b43_modparam_verbose < B43_VERBOSITY_WARN)
+               return;
        if (!b43_ratelimit(wl))
                return;
        va_start(args, fmt);
@@ -333,18 +345,18 @@ void b43warn(struct b43_wl *wl, const char *fmt, ...)
        va_end(args);
 }
 
-#if B43_DEBUG
 void b43dbg(struct b43_wl *wl, const char *fmt, ...)
 {
        va_list args;
 
+       if (b43_modparam_verbose < B43_VERBOSITY_DEBUG)
+               return;
        va_start(args, fmt);
        printk(KERN_DEBUG "b43-%s debug: ",
               (wl && wl->hw) ? wiphy_name(wl->hw->wiphy) : "wlan");
        vprintk(fmt, args);
        va_end(args);
 }
-#endif /* DEBUG */
 
 static void b43_ram_write(struct b43_wldev *dev, u16 offset, u32 val)
 {
@@ -373,13 +385,10 @@ static inline void b43_shm_control_word(struct b43_wldev *dev,
        b43_write32(dev, B43_MMIO_SHM_CONTROL, control);
 }
 
-u32 b43_shm_read32(struct b43_wldev *dev, u16 routing, u16 offset)
+u32 __b43_shm_read32(struct b43_wldev *dev, u16 routing, u16 offset)
 {
-       struct b43_wl *wl = dev->wl;
-       unsigned long flags;
        u32 ret;
 
-       spin_lock_irqsave(&wl->shm_lock, flags);
        if (routing == B43_SHM_SHARED) {
                B43_WARN_ON(offset & 0x0001);
                if (offset & 0x0003) {
@@ -397,18 +406,26 @@ u32 b43_shm_read32(struct b43_wldev *dev, u16 routing, u16 offset)
        b43_shm_control_word(dev, routing, offset);
        ret = b43_read32(dev, B43_MMIO_SHM_DATA);
 out:
-       spin_unlock_irqrestore(&wl->shm_lock, flags);
-
        return ret;
 }
 
-u16 b43_shm_read16(struct b43_wldev * dev, u16 routing, u16 offset)
+u32 b43_shm_read32(struct b43_wldev *dev, u16 routing, u16 offset)
 {
        struct b43_wl *wl = dev->wl;
        unsigned long flags;
-       u16 ret;
+       u32 ret;
 
        spin_lock_irqsave(&wl->shm_lock, flags);
+       ret = __b43_shm_read32(dev, routing, offset);
+       spin_unlock_irqrestore(&wl->shm_lock, flags);
+
+       return ret;
+}
+
+u16 __b43_shm_read16(struct b43_wldev *dev, u16 routing, u16 offset)
+{
+       u16 ret;
+
        if (routing == B43_SHM_SHARED) {
                B43_WARN_ON(offset & 0x0001);
                if (offset & 0x0003) {
@@ -423,17 +440,24 @@ u16 b43_shm_read16(struct b43_wldev * dev, u16 routing, u16 offset)
        b43_shm_control_word(dev, routing, offset);
        ret = b43_read16(dev, B43_MMIO_SHM_DATA);
 out:
-       spin_unlock_irqrestore(&wl->shm_lock, flags);
-
        return ret;
 }
 
-void b43_shm_write32(struct b43_wldev *dev, u16 routing, u16 offset, u32 value)
+u16 b43_shm_read16(struct b43_wldev *dev, u16 routing, u16 offset)
 {
        struct b43_wl *wl = dev->wl;
        unsigned long flags;
+       u16 ret;
 
        spin_lock_irqsave(&wl->shm_lock, flags);
+       ret = __b43_shm_read16(dev, routing, offset);
+       spin_unlock_irqrestore(&wl->shm_lock, flags);
+
+       return ret;
+}
+
+void __b43_shm_write32(struct b43_wldev *dev, u16 routing, u16 offset, u32 value)
+{
        if (routing == B43_SHM_SHARED) {
                B43_WARN_ON(offset & 0x0001);
                if (offset & 0x0003) {
@@ -443,40 +467,52 @@ void b43_shm_write32(struct b43_wldev *dev, u16 routing, u16 offset, u32 value)
                                    (value >> 16) & 0xffff);
                        b43_shm_control_word(dev, routing, (offset >> 2) + 1);
                        b43_write16(dev, B43_MMIO_SHM_DATA, value & 0xffff);
-                       goto out;
+                       return;
                }
                offset >>= 2;
        }
        b43_shm_control_word(dev, routing, offset);
        b43_write32(dev, B43_MMIO_SHM_DATA, value);
-out:
-       spin_unlock_irqrestore(&wl->shm_lock, flags);
 }
 
-void b43_shm_write16(struct b43_wldev *dev, u16 routing, u16 offset, u16 value)
+void b43_shm_write32(struct b43_wldev *dev, u16 routing, u16 offset, u32 value)
 {
        struct b43_wl *wl = dev->wl;
        unsigned long flags;
 
        spin_lock_irqsave(&wl->shm_lock, flags);
+       __b43_shm_write32(dev, routing, offset, value);
+       spin_unlock_irqrestore(&wl->shm_lock, flags);
+}
+
+void __b43_shm_write16(struct b43_wldev *dev, u16 routing, u16 offset, u16 value)
+{
        if (routing == B43_SHM_SHARED) {
                B43_WARN_ON(offset & 0x0001);
                if (offset & 0x0003) {
                        /* Unaligned access */
                        b43_shm_control_word(dev, routing, offset >> 2);
                        b43_write16(dev, B43_MMIO_SHM_DATA_UNALIGNED, value);
-                       goto out;
+                       return;
                }
                offset >>= 2;
        }
        b43_shm_control_word(dev, routing, offset);
        b43_write16(dev, B43_MMIO_SHM_DATA, value);
-out:
+}
+
+void b43_shm_write16(struct b43_wldev *dev, u16 routing, u16 offset, u16 value)
+{
+       struct b43_wl *wl = dev->wl;
+       unsigned long flags;
+
+       spin_lock_irqsave(&wl->shm_lock, flags);
+       __b43_shm_write16(dev, routing, offset, value);
        spin_unlock_irqrestore(&wl->shm_lock, flags);
 }
 
 /* Read HostFlags */
-u64 b43_hf_read(struct b43_wldev * dev)
+u64 b43_hf_read(struct b43_wldev *dev)
 {
        u64 ret;
 
@@ -502,52 +538,20 @@ void b43_hf_write(struct b43_wldev *dev, u64 value)
        b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTFHI, hi);
 }
 
-void b43_tsf_read(struct b43_wldev *dev, u64 * tsf)
+void b43_tsf_read(struct b43_wldev *dev, u64 *tsf)
 {
-       /* We need to be careful. As we read the TSF from multiple
-        * registers, we should take care of register overflows.
-        * In theory, the whole tsf read process should be atomic.
-        * We try to be atomic here, by restaring the read process,
-        * if any of the high registers changed (overflew).
-        */
-       if (dev->dev->id.revision >= 3) {
-               u32 low, high, high2;
+       u32 low, high;
 
-               do {
-                       high = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_HIGH);
-                       low = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_LOW);
-                       high2 = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_HIGH);
-               } while (unlikely(high != high2));
+       B43_WARN_ON(dev->dev->id.revision < 3);
 
-               *tsf = high;
-               *tsf <<= 32;
-               *tsf |= low;
-       } else {
-               u64 tmp;
-               u16 v0, v1, v2, v3;
-               u16 test1, test2, test3;
-
-               do {
-                       v3 = b43_read16(dev, B43_MMIO_TSF_3);
-                       v2 = b43_read16(dev, B43_MMIO_TSF_2);
-                       v1 = b43_read16(dev, B43_MMIO_TSF_1);
-                       v0 = b43_read16(dev, B43_MMIO_TSF_0);
+       /* The hardware guarantees us an atomic read, if we
+        * read the low register first. */
+       low = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_LOW);
+       high = b43_read32(dev, B43_MMIO_REV3PLUS_TSF_HIGH);
 
-                       test3 = b43_read16(dev, B43_MMIO_TSF_3);
-                       test2 = b43_read16(dev, B43_MMIO_TSF_2);
-                       test1 = b43_read16(dev, B43_MMIO_TSF_1);
-               } while (v3 != test3 || v2 != test2 || v1 != test1);
-
-               *tsf = v3;
-               *tsf <<= 48;
-               tmp = v2;
-               tmp <<= 32;
-               *tsf |= tmp;
-               tmp = v1;
-               tmp <<= 16;
-               *tsf |= tmp;
-               *tsf |= v0;
-       }
+       *tsf = high;
+       *tsf <<= 32;
+       *tsf |= low;
 }
 
 static void b43_time_lock(struct b43_wldev *dev)
@@ -574,35 +578,18 @@ static void b43_time_unlock(struct b43_wldev *dev)
 
 static void b43_tsf_write_locked(struct b43_wldev *dev, u64 tsf)
 {
-       /* Be careful with the in-progress timer.
-        * First zero out the low register, so we have a full
-        * register-overflow duration to complete the operation.
-        */
-       if (dev->dev->id.revision >= 3) {
-               u32 lo = (tsf & 0x00000000FFFFFFFFULL);
-               u32 hi = (tsf & 0xFFFFFFFF00000000ULL) >> 32;
+       u32 low, high;
 
-               b43_write32(dev, B43_MMIO_REV3PLUS_TSF_LOW, 0);
-               mmiowb();
-               b43_write32(dev, B43_MMIO_REV3PLUS_TSF_HIGH, hi);
-               mmiowb();
-               b43_write32(dev, B43_MMIO_REV3PLUS_TSF_LOW, lo);
-       } else {
-               u16 v0 = (tsf & 0x000000000000FFFFULL);
-               u16 v1 = (tsf & 0x00000000FFFF0000ULL) >> 16;
-               u16 v2 = (tsf & 0x0000FFFF00000000ULL) >> 32;
-               u16 v3 = (tsf & 0xFFFF000000000000ULL) >> 48;
+       B43_WARN_ON(dev->dev->id.revision < 3);
 
-               b43_write16(dev, B43_MMIO_TSF_0, 0);
-               mmiowb();
-               b43_write16(dev, B43_MMIO_TSF_3, v3);
-               mmiowb();
-               b43_write16(dev, B43_MMIO_TSF_2, v2);
-               mmiowb();
-               b43_write16(dev, B43_MMIO_TSF_1, v1);
-               mmiowb();
-               b43_write16(dev, B43_MMIO_TSF_0, v0);
-       }
+       low = tsf;
+       high = (tsf >> 32);
+       /* The hardware guarantees us an atomic write, if we
+        * write the low register first. */
+       b43_write32(dev, B43_MMIO_REV3PLUS_TSF_LOW, low);
+       mmiowb();
+       b43_write32(dev, B43_MMIO_REV3PLUS_TSF_HIGH, high);
+       mmiowb();
 }
 
 void b43_tsf_write(struct b43_wldev *dev, u64 tsf)
@@ -613,7 +600,7 @@ void b43_tsf_write(struct b43_wldev *dev, u64 tsf)
 }
 
 static
-void b43_macfilter_set(struct b43_wldev *dev, u16 offset, const u8 * mac)
+void b43_macfilter_set(struct b43_wldev *dev, u16 offset, const u8 *mac)
 {
        static const u8 zero_addr[ETH_ALEN] = { 0 };
        u16 data;
@@ -679,39 +666,11 @@ static void b43_set_slot_time(struct b43_wldev *dev, u16 slot_time)
 static void b43_short_slot_timing_enable(struct b43_wldev *dev)
 {
        b43_set_slot_time(dev, 9);
-       dev->short_slot = 1;
 }
 
 static void b43_short_slot_timing_disable(struct b43_wldev *dev)
 {
        b43_set_slot_time(dev, 20);
-       dev->short_slot = 0;
-}
-
-/* Enable a Generic IRQ. "mask" is the mask of which IRQs to enable.
- * Returns the _previously_ enabled IRQ mask.
- */
-static inline u32 b43_interrupt_enable(struct b43_wldev *dev, u32 mask)
-{
-       u32 old_mask;
-
-       old_mask = b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
-       b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, old_mask | mask);
-
-       return old_mask;
-}
-
-/* Disable a Generic IRQ. "mask" is the mask of which IRQs to disable.
- * Returns the _previously_ enabled IRQ mask.
- */
-static inline u32 b43_interrupt_disable(struct b43_wldev *dev, u32 mask)
-{
-       u32 old_mask;
-
-       old_mask = b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
-       b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, old_mask & ~mask);
-
-       return old_mask;
 }
 
 /* Synchronize IRQ top- and bottom-half.
@@ -791,7 +750,7 @@ void b43_dummy_transmission(struct b43_wldev *dev)
                        break;
                udelay(10);
        }
-       for (i = 0x00; i < 0x0A; i++) {
+       for (i = 0x00; i < 0x19; i++) {
                value = b43_read16(dev, 0x0690);
                if (!(value & 0x0100))
                        break;
@@ -805,7 +764,7 @@ void b43_dummy_transmission(struct b43_wldev *dev)
 }
 
 static void key_write(struct b43_wldev *dev,
-                     u8 index, u8 algorithm, const u8 * key)
+                     u8 index, u8 algorithm, const u8 *key)
 {
        unsigned int i;
        u32 offset;
@@ -827,7 +786,7 @@ static void key_write(struct b43_wldev *dev,
        }
 }
 
-static void keymac_write(struct b43_wldev *dev, u8 index, const u8 * addr)
+static void keymac_write(struct b43_wldev *dev, u8 index, const u8 *addr)
 {
        u32 addrtmp[2] = { 0, 0, };
        u8 per_sta_keys_start = 8;
@@ -877,7 +836,7 @@ static void keymac_write(struct b43_wldev *dev, u8 index, const u8 * addr)
 
 static void do_key_write(struct b43_wldev *dev,
                         u8 index, u8 algorithm,
-                        const u8 * key, size_t key_len, const u8 * mac_addr)
+                        const u8 *key, size_t key_len, const u8 *mac_addr)
 {
        u8 buf[B43_SEC_KEYSIZE] = { 0, };
        u8 per_sta_keys_start = 8;
@@ -901,8 +860,8 @@ static void do_key_write(struct b43_wldev *dev,
 
 static int b43_key_write(struct b43_wldev *dev,
                         int index, u8 algorithm,
-                        const u8 * key, size_t key_len,
-                        const u8 * mac_addr,
+                        const u8 *key, size_t key_len,
+                        const u8 *mac_addr,
                         struct ieee80211_key_conf *keyconf)
 {
        int i;
@@ -915,8 +874,7 @@ static int b43_key_write(struct b43_wldev *dev,
                B43_WARN_ON(dev->key[i].keyconf == keyconf);
        }
        if (index < 0) {
-               /* Either pairwise key or address is 00:00:00:00:00:00
-                * for transmit-only keys. Search the index. */
+               /* Pairwise key. Get an empty slot for the key. */
                if (b43_new_kidx_api(dev))
                        sta_keys_start = 4;
                else
@@ -929,7 +887,7 @@ static int b43_key_write(struct b43_wldev *dev,
                        }
                }
                if (index < 0) {
-                       b43err(dev->wl, "Out of hardware key memory\n");
+                       b43warn(dev->wl, "Out of hardware key memory\n");
                        return -ENOSPC;
                }
        } else
@@ -970,6 +928,52 @@ static void b43_clear_keys(struct b43_wldev *dev)
                b43_key_clear(dev, i);
 }
 
+static void b43_dump_keymemory(struct b43_wldev *dev)
+{
+       unsigned int i, index, offset;
+       DECLARE_MAC_BUF(macbuf);
+       u8 mac[ETH_ALEN];
+       u16 algo;
+       u32 rcmta0;
+       u16 rcmta1;
+       u64 hf;
+       struct b43_key *key;
+
+       if (!b43_debug(dev, B43_DBG_KEYS))
+               return;
+
+       hf = b43_hf_read(dev);
+       b43dbg(dev->wl, "Hardware key memory dump:  USEDEFKEYS=%u\n",
+              !!(hf & B43_HF_USEDEFKEYS));
+       for (index = 0; index < dev->max_nr_keys; index++) {
+               key = &(dev->key[index]);
+               printk(KERN_DEBUG "Key slot %02u: %s",
+                      index, (key->keyconf == NULL) ? " " : "*");
+               offset = dev->ktp + (index * B43_SEC_KEYSIZE);
+               for (i = 0; i < B43_SEC_KEYSIZE; i += 2) {
+                       u16 tmp = b43_shm_read16(dev, B43_SHM_SHARED, offset + i);
+                       printk("%02X%02X", (tmp & 0xFF), ((tmp >> 8) & 0xFF));
+               }
+
+               algo = b43_shm_read16(dev, B43_SHM_SHARED,
+                                     B43_SHM_SH_KEYIDXBLOCK + (index * 2));
+               printk("   Algo: %04X/%02X", algo, key->algorithm);
+
+               if (index >= 4) {
+                       rcmta0 = b43_shm_read32(dev, B43_SHM_RCMTA,
+                                               ((index - 4) * 2) + 0);
+                       rcmta1 = b43_shm_read16(dev, B43_SHM_RCMTA,
+                                               ((index - 4) * 2) + 1);
+                       *((__le32 *)(&mac[0])) = cpu_to_le32(rcmta0);
+                       *((__le16 *)(&mac[4])) = cpu_to_le16(rcmta1);
+                       printk("   MAC: %s",
+                              print_mac(macbuf, mac));
+               } else
+                       printk("   DEFAULT KEY");
+               printk("\n");
+       }
+}
+
 void b43_power_saving_ctl_bits(struct b43_wldev *dev, unsigned int ps_flags)
 {
        u32 macctl;
@@ -1028,23 +1032,6 @@ void b43_power_saving_ctl_bits(struct b43_wldev *dev, unsigned int ps_flags)
        }
 }
 
-/* Turn the Analog ON/OFF */
-static void b43_switch_analog(struct b43_wldev *dev, int on)
-{
-       switch (dev->phy.type) {
-       case B43_PHYTYPE_A:
-       case B43_PHYTYPE_G:
-               b43_write16(dev, B43_MMIO_PHY0, on ? 0 : 0xF4);
-               break;
-       case B43_PHYTYPE_N:
-               b43_phy_write(dev, B43_NPHY_AFECTL_OVER,
-                             on ? 0 : 0x7FFF);
-               break;
-       default:
-               B43_WARN_ON(1);
-       }
-}
-
 void b43_wireless_core_reset(struct b43_wldev *dev, u32 flags)
 {
        u32 tmslow;
@@ -1067,8 +1054,12 @@ void b43_wireless_core_reset(struct b43_wldev *dev, u32 flags)
        ssb_read32(dev->dev, SSB_TMSLOW);       /* flush */
        msleep(1);
 
-       /* Turn Analog ON */
-       b43_switch_analog(dev, 1);
+       /* Turn Analog ON, but only if we already know the PHY-type.
+        * This protects against very early setup where we don't know the
+        * PHY-type, yet. wireless_core_reset will be called once again later,
+        * when we know the PHY-type. */
+       if (dev->phy.ops)
+               dev->phy.ops->switch_analog(dev, 1);
 
        macctl = b43_read32(dev, B43_MMIO_MACCTL);
        macctl &= ~B43_MACCTL_GMODE;
@@ -1145,16 +1136,16 @@ static void b43_generate_noise_sample(struct b43_wldev *dev)
        b43_jssi_write(dev, 0x7F7F7F7F);
        b43_write32(dev, B43_MMIO_MACCMD,
                    b43_read32(dev, B43_MMIO_MACCMD) | B43_MACCMD_BGNOISE);
-       B43_WARN_ON(dev->noisecalc.channel_at_start != dev->phy.channel);
 }
 
 static void b43_calculate_link_quality(struct b43_wldev *dev)
 {
        /* Top half of Link Quality calculation. */
 
+       if (dev->phy.type != B43_PHYTYPE_G)
+               return;
        if (dev->noisecalc.calculation_running)
                return;
-       dev->noisecalc.channel_at_start = dev->phy.channel;
        dev->noisecalc.calculation_running = 1;
        dev->noisecalc.nr_samples = 0;
 
@@ -1163,7 +1154,7 @@ static void b43_calculate_link_quality(struct b43_wldev *dev)
 
 static void handle_irq_noise(struct b43_wldev *dev)
 {
-       struct b43_phy *phy = &dev->phy;
+       struct b43_phy_g *phy = dev->phy.g;
        u16 tmp;
        u8 noise[4];
        u8 i, j;
@@ -1171,9 +1162,19 @@ static void handle_irq_noise(struct b43_wldev *dev)
 
        /* Bottom half of Link Quality calculation. */
 
+       if (dev->phy.type != B43_PHYTYPE_G)
+               return;
+
+       /* Possible race condition: It might be possible that the user
+        * changed to a different channel in the meantime since we
+        * started the calculation. We ignore that fact, since it's
+        * not really that much of a problem. The background noise is
+        * an estimation only anyway. Slightly wrong results will get damped
+        * by the averaging of the 8 sample rounds. Additionally the
+        * value is shortlived. So it will be replaced by the next noise
+        * calculation round soon. */
+
        B43_WARN_ON(!dev->noisecalc.calculation_running);
-       if (dev->noisecalc.channel_at_start != phy->channel)
-               goto drop_calculation;
        *((__le32 *)noise) = cpu_to_le32(b43_jssi_read(dev));
        if (noise[0] == 0x7F || noise[1] == 0x7F ||
            noise[2] == 0x7F || noise[3] == 0x7F)
@@ -1214,23 +1215,22 @@ static void handle_irq_noise(struct b43_wldev *dev)
                        average -= 48;
 
                dev->stats.link_noise = average;
-             drop_calculation:
                dev->noisecalc.calculation_running = 0;
                return;
        }
-      generate_new:
+generate_new:
        b43_generate_noise_sample(dev);
 }
 
 static void handle_irq_tbtt_indication(struct b43_wldev *dev)
 {
-       if (b43_is_mode(dev->wl, IEEE80211_IF_TYPE_AP)) {
+       if (b43_is_mode(dev->wl, NL80211_IFTYPE_AP)) {
                ///TODO: PS TBTT
        } else {
                if (1 /*FIXME: the last PSpoll frame was sent successfully */ )
                        b43_power_saving_ctl_bits(dev, 0);
        }
-       if (b43_is_mode(dev->wl, IEEE80211_IF_TYPE_IBSS))
+       if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC))
                dev->dfq_valid = 1;
 }
 
@@ -1260,7 +1260,7 @@ static void handle_irq_pmq(struct b43_wldev *dev)
 }
 
 static void b43_write_template_common(struct b43_wldev *dev,
-                                     const u8 * data, u16 size,
+                                     const u8 *data, u16 size,
                                      u16 ram_offset,
                                      u16 shm_size_offset, u8 rate)
 {
@@ -1319,25 +1319,6 @@ u8 b43_ieee80211_antenna_sanitize(struct b43_wldev *dev,
        return antenna_nr;
 }
 
-static int b43_antenna_from_ieee80211(struct b43_wldev *dev, u8 antenna)
-{
-       antenna = b43_ieee80211_antenna_sanitize(dev, antenna);
-       switch (antenna) {
-       case 0:         /* default/diversity */
-               return B43_ANTENNA_DEFAULT;
-       case 1:         /* Antenna 0 */
-               return B43_ANTENNA0;
-       case 2:         /* Antenna 1 */
-               return B43_ANTENNA1;
-       case 3:         /* Antenna 2 */
-               return B43_ANTENNA2;
-       case 4:         /* Antenna 3 */
-               return B43_ANTENNA3;
-       default:
-               return B43_ANTENNA_DEFAULT;
-       }
-}
-
 /* Convert a b43 antenna number value to the PHY TX control value. */
 static u16 b43_antenna_to_phyctl(int antenna)
 {
@@ -1368,18 +1349,18 @@ static void b43_write_beacon_template(struct b43_wldev *dev,
        unsigned int rate;
        u16 ctl;
        int antenna;
+       struct ieee80211_tx_info *info = IEEE80211_SKB_CB(dev->wl->current_beacon);
 
        bcn = (const struct ieee80211_mgmt *)(dev->wl->current_beacon->data);
        len = min((size_t) dev->wl->current_beacon->len,
                  0x200 - sizeof(struct b43_plcp_hdr6));
-       rate = dev->wl->beacon_txctl.tx_rate->hw_value;
+       rate = ieee80211_get_tx_rate(dev->wl->hw, info)->hw_value;
 
        b43_write_template_common(dev, (const u8 *)bcn,
                                  len, ram_offset, shm_size_offset, rate);
 
        /* Write the PHY TX control parameters. */
-       antenna = b43_antenna_from_ieee80211(dev,
-                       dev->wl->beacon_txctl.antenna_sel_tx);
+       antenna = B43_ANTENNA_DEFAULT;
        antenna = b43_antenna_to_phyctl(antenna);
        ctl = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL);
        /* We can't send beacons with short preamble. Would get PHY errors. */
@@ -1430,11 +1411,17 @@ static void b43_write_beacon_template(struct b43_wldev *dev,
                i += ie_len + 2;
        }
        if (!tim_found) {
-               b43warn(dev->wl, "Did not find a valid TIM IE in "
-                       "the beacon template packet. AP or IBSS operation "
-                       "may be broken.\n");
-       } else
-               b43dbg(dev->wl, "Updated beacon template\n");
+               /*
+                * If ucode wants to modify TIM do it behind the beacon, this
+                * will happen, for example, when doing mesh networking.
+                */
+               b43_shm_write16(dev, B43_SHM_SHARED,
+                               B43_SHM_SH_TIMBPOS,
+                               len + sizeof(struct b43_plcp_hdr6));
+               b43_shm_write16(dev, B43_SHM_SHARED,
+                               B43_SHM_SH_DTIMPER, 0);
+       }
+       b43dbg(dev->wl, "Updated beacon template at 0x%x\n", ram_offset);
 }
 
 static void b43_write_probe_resp_plcp(struct b43_wldev *dev,
@@ -1463,9 +1450,9 @@ static void b43_write_probe_resp_plcp(struct b43_wldev *dev,
  * 2) Patching duration field
  * 3) Stripping TIM
  */
-static const u8 * b43_generate_probe_resp(struct b43_wldev *dev,
-                                         u16 *dest_size,
-                                         struct ieee80211_rate *rate)
+static const u8 *b43_generate_probe_resp(struct b43_wldev *dev,
+                                        u16 *dest_size,
+                                        struct ieee80211_rate *rate)
 {
        const u8 *src_data;
        u8 *dest_data;
@@ -1544,18 +1531,43 @@ static void b43_write_probe_resp_template(struct b43_wldev *dev,
        kfree(probe_resp_data);
 }
 
+static void b43_upload_beacon0(struct b43_wldev *dev)
+{
+       struct b43_wl *wl = dev->wl;
+
+       if (wl->beacon0_uploaded)
+               return;
+       b43_write_beacon_template(dev, 0x68, 0x18);
+       /* FIXME: Probe resp upload doesn't really belong here,
+        *        but we don't use that feature anyway. */
+       b43_write_probe_resp_template(dev, 0x268, 0x4A,
+                                     &__b43_ratetable[3]);
+       wl->beacon0_uploaded = 1;
+}
+
+static void b43_upload_beacon1(struct b43_wldev *dev)
+{
+       struct b43_wl *wl = dev->wl;
+
+       if (wl->beacon1_uploaded)
+               return;
+       b43_write_beacon_template(dev, 0x468, 0x1A);
+       wl->beacon1_uploaded = 1;
+}
+
 static void handle_irq_beacon(struct b43_wldev *dev)
 {
        struct b43_wl *wl = dev->wl;
        u32 cmd, beacon0_valid, beacon1_valid;
 
-       if (!b43_is_mode(wl, IEEE80211_IF_TYPE_AP))
+       if (!b43_is_mode(wl, NL80211_IFTYPE_AP) &&
+           !b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT))
                return;
 
        /* This is the bottom half of the asynchronous beacon update. */
 
        /* Ignore interrupt in the future. */
-       dev->irq_savedstate &= ~B43_IRQ_BEACON;
+       dev->irq_mask &= ~B43_IRQ_BEACON;
 
        cmd = b43_read32(dev, B43_MMIO_MACCMD);
        beacon0_valid = (cmd & B43_MACCMD_BEACON0_VALID);
@@ -1564,28 +1576,31 @@ static void handle_irq_beacon(struct b43_wldev *dev)
        /* Schedule interrupt manually, if busy. */
        if (beacon0_valid && beacon1_valid) {
                b43_write32(dev, B43_MMIO_GEN_IRQ_REASON, B43_IRQ_BEACON);
-               dev->irq_savedstate |= B43_IRQ_BEACON;
+               dev->irq_mask |= B43_IRQ_BEACON;
                return;
        }
 
-       if (!beacon0_valid) {
-               if (!wl->beacon0_uploaded) {
-                       b43_write_beacon_template(dev, 0x68, 0x18);
-                       b43_write_probe_resp_template(dev, 0x268, 0x4A,
-                                                     &__b43_ratetable[3]);
-                       wl->beacon0_uploaded = 1;
-               }
+       if (unlikely(wl->beacon_templates_virgin)) {
+               /* We never uploaded a beacon before.
+                * Upload both templates now, but only mark one valid. */
+               wl->beacon_templates_virgin = 0;
+               b43_upload_beacon0(dev);
+               b43_upload_beacon1(dev);
                cmd = b43_read32(dev, B43_MMIO_MACCMD);
                cmd |= B43_MACCMD_BEACON0_VALID;
                b43_write32(dev, B43_MMIO_MACCMD, cmd);
-       } else if (!beacon1_valid) {
-               if (!wl->beacon1_uploaded) {
-                       b43_write_beacon_template(dev, 0x468, 0x1A);
-                       wl->beacon1_uploaded = 1;
+       } else {
+               if (!beacon0_valid) {
+                       b43_upload_beacon0(dev);
+                       cmd = b43_read32(dev, B43_MMIO_MACCMD);
+                       cmd |= B43_MACCMD_BEACON0_VALID;
+                       b43_write32(dev, B43_MMIO_MACCMD, cmd);
+               } else if (!beacon1_valid) {
+                       b43_upload_beacon1(dev);
+                       cmd = b43_read32(dev, B43_MMIO_MACCMD);
+                       cmd |= B43_MACCMD_BEACON1_VALID;
+                       b43_write32(dev, B43_MMIO_MACCMD, cmd);
                }
-               cmd = b43_read32(dev, B43_MMIO_MACCMD);
-               cmd |= B43_MACCMD_BEACON1_VALID;
-               b43_write32(dev, B43_MMIO_MACCMD, cmd);
        }
 }
 
@@ -1600,11 +1615,9 @@ static void b43_beacon_update_trigger_work(struct work_struct *work)
        if (likely(dev && (b43_status(dev) >= B43_STAT_INITIALIZED))) {
                spin_lock_irq(&wl->irq_lock);
                /* update beacon right away or defer to irq */
-               dev->irq_savedstate = b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
                handle_irq_beacon(dev);
                /* The handler might have updated the IRQ mask. */
-               b43_write32(dev, B43_MMIO_GEN_IRQ_MASK,
-                           dev->irq_savedstate);
+               b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
                mmiowb();
                spin_unlock_irq(&wl->irq_lock);
        }
@@ -1613,43 +1626,32 @@ static void b43_beacon_update_trigger_work(struct work_struct *work)
 
 /* Asynchronously update the packet templates in template RAM.
  * Locking: Requires wl->irq_lock to be locked. */
-static void b43_update_templates(struct b43_wl *wl, struct sk_buff *beacon,
-                                const struct ieee80211_tx_control *txctl)
+static void b43_update_templates(struct b43_wl *wl)
 {
+       struct sk_buff *beacon;
+
        /* This is the top half of the ansynchronous beacon update.
         * The bottom half is the beacon IRQ.
         * Beacon update must be asynchronous to avoid sending an
         * invalid beacon. This can happen for example, if the firmware
         * transmits a beacon while we are updating it. */
 
+       /* We could modify the existing beacon and set the aid bit in
+        * the TIM field, but that would probably require resizing and
+        * moving of data within the beacon template.
+        * Simply request a new beacon and let mac80211 do the hard work. */
+       beacon = ieee80211_beacon_get(wl->hw, wl->vif);
+       if (unlikely(!beacon))
+               return;
+
        if (wl->current_beacon)
                dev_kfree_skb_any(wl->current_beacon);
        wl->current_beacon = beacon;
-       memcpy(&wl->beacon_txctl, txctl, sizeof(wl->beacon_txctl));
        wl->beacon0_uploaded = 0;
        wl->beacon1_uploaded = 0;
        queue_work(wl->hw->workqueue, &wl->beacon_update_trigger);
 }
 
-static void b43_set_ssid(struct b43_wldev *dev, const u8 * ssid, u8 ssid_len)
-{
-       u32 tmp;
-       u16 i, len;
-
-       len = min((u16) ssid_len, (u16) 0x100);
-       for (i = 0; i < len; i += sizeof(u32)) {
-               tmp = (u32) (ssid[i + 0]);
-               if (i + 1 < len)
-                       tmp |= (u32) (ssid[i + 1]) << 8;
-               if (i + 2 < len)
-                       tmp |= (u32) (ssid[i + 2]) << 16;
-               if (i + 3 < len)
-                       tmp |= (u32) (ssid[i + 3]) << 24;
-               b43_shm_write32(dev, B43_SHM_SHARED, 0x380 + i, tmp);
-       }
-       b43_shm_write16(dev, B43_SHM_SHARED, 0x48, len);
-}
-
 static void b43_set_beacon_int(struct b43_wldev *dev, u16 beacon_int)
 {
        b43_time_lock(dev);
@@ -1664,9 +1666,100 @@ static void b43_set_beacon_int(struct b43_wldev *dev, u16 beacon_int)
        b43dbg(dev->wl, "Set beacon interval to %u\n", beacon_int);
 }
 
+static void b43_handle_firmware_panic(struct b43_wldev *dev)
+{
+       u16 reason;
+
+       /* Read the register that contains the reason code for the panic. */
+       reason = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_FWPANIC_REASON_REG);
+       b43err(dev->wl, "Whoopsy, firmware panic! Reason: %u\n", reason);
+
+       switch (reason) {
+       default:
+               b43dbg(dev->wl, "The panic reason is unknown.\n");
+               /* fallthrough */
+       case B43_FWPANIC_DIE:
+               /* Do not restart the controller or firmware.
+                * The device is nonfunctional from now on.
+                * Restarting would result in this panic to trigger again,
+                * so we avoid that recursion. */
+               break;
+       case B43_FWPANIC_RESTART:
+               b43_controller_restart(dev, "Microcode panic");
+               break;
+       }
+}
+
 static void handle_irq_ucode_debug(struct b43_wldev *dev)
 {
-       //TODO
+       unsigned int i, cnt;
+       u16 reason, marker_id, marker_line;
+       __le16 *buf;
+
+       /* The proprietary firmware doesn't have this IRQ. */
+       if (!dev->fw.opensource)
+               return;
+
+       /* Read the register that contains the reason code for this IRQ. */
+       reason = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_DEBUGIRQ_REASON_REG);
+
+       switch (reason) {
+       case B43_DEBUGIRQ_PANIC:
+               b43_handle_firmware_panic(dev);
+               break;
+       case B43_DEBUGIRQ_DUMP_SHM:
+               if (!B43_DEBUG)
+                       break; /* Only with driver debugging enabled. */
+               buf = kmalloc(4096, GFP_ATOMIC);
+               if (!buf) {
+                       b43dbg(dev->wl, "SHM-dump: Failed to allocate memory\n");
+                       goto out;
+               }
+               for (i = 0; i < 4096; i += 2) {
+                       u16 tmp = b43_shm_read16(dev, B43_SHM_SHARED, i);
+                       buf[i / 2] = cpu_to_le16(tmp);
+               }
+               b43info(dev->wl, "Shared memory dump:\n");
+               print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET,
+                              16, 2, buf, 4096, 1);
+               kfree(buf);
+               break;
+       case B43_DEBUGIRQ_DUMP_REGS:
+               if (!B43_DEBUG)
+                       break; /* Only with driver debugging enabled. */
+               b43info(dev->wl, "Microcode register dump:\n");
+               for (i = 0, cnt = 0; i < 64; i++) {
+                       u16 tmp = b43_shm_read16(dev, B43_SHM_SCRATCH, i);
+                       if (cnt == 0)
+                               printk(KERN_INFO);
+                       printk("r%02u: 0x%04X  ", i, tmp);
+                       cnt++;
+                       if (cnt == 6) {
+                               printk("\n");
+                               cnt = 0;
+                       }
+               }
+               printk("\n");
+               break;
+       case B43_DEBUGIRQ_MARKER:
+               if (!B43_DEBUG)
+                       break; /* Only with driver debugging enabled. */
+               marker_id = b43_shm_read16(dev, B43_SHM_SCRATCH,
+                                          B43_MARKER_ID_REG);
+               marker_line = b43_shm_read16(dev, B43_SHM_SCRATCH,
+                                            B43_MARKER_LINE_REG);
+               b43info(dev->wl, "The firmware just executed the MARKER(%u) "
+                       "at line number %u\n",
+                       marker_id, marker_line);
+               break;
+       default:
+               b43dbg(dev->wl, "Debug-IRQ triggered for unknown reason: %u\n",
+                      reason);
+       }
+out:
+       /* Acknowledge the debug-IRQ, so the firmware can continue. */
+       b43_shm_write16(dev, B43_SHM_SCRATCH,
+                       B43_DEBUGIRQ_REASON_REG, B43_DEBUGIRQ_ACK);
 }
 
 /* Interrupt handler bottom-half */
@@ -1758,7 +1851,7 @@ static void b43_interrupt_tasklet(struct b43_wldev *dev)
        if (reason & B43_IRQ_TX_OK)
                handle_irq_transmit_status(dev);
 
-       b43_interrupt_enable(dev, dev->irq_savedstate);
+       b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
        mmiowb();
        spin_unlock_irqrestore(&dev->wl->irq_lock, flags);
 }
@@ -1772,7 +1865,9 @@ static void b43_interrupt_ack(struct b43_wldev *dev, u32 reason)
        b43_write32(dev, B43_MMIO_DMA2_REASON, dev->dma_reason[2]);
        b43_write32(dev, B43_MMIO_DMA3_REASON, dev->dma_reason[3]);
        b43_write32(dev, B43_MMIO_DMA4_REASON, dev->dma_reason[4]);
+/* Unused ring
        b43_write32(dev, B43_MMIO_DMA5_REASON, dev->dma_reason[5]);
+*/
 }
 
 /* Interrupt handler top-half */
@@ -1782,18 +1877,19 @@ static irqreturn_t b43_interrupt_handler(int irq, void *dev_id)
        struct b43_wldev *dev = dev_id;
        u32 reason;
 
-       if (!dev)
-               return IRQ_NONE;
+       B43_WARN_ON(!dev);
 
        spin_lock(&dev->wl->irq_lock);
 
-       if (b43_status(dev) < B43_STAT_STARTED)
+       if (unlikely(b43_status(dev) < B43_STAT_STARTED)) {
+               /* This can only happen on shared IRQ lines. */
                goto out;
+       }
        reason = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
        if (reason == 0xffffffff)       /* shared IRQ */
                goto out;
        ret = IRQ_HANDLED;
-       reason &= b43_read32(dev, B43_MMIO_GEN_IRQ_MASK);
+       reason &= dev->irq_mask;
        if (!reason)
                goto out;
 
@@ -1807,23 +1903,25 @@ static irqreturn_t b43_interrupt_handler(int irq, void *dev_id)
            & 0x0001DC00;
        dev->dma_reason[4] = b43_read32(dev, B43_MMIO_DMA4_REASON)
            & 0x0000DC00;
+/* Unused ring
        dev->dma_reason[5] = b43_read32(dev, B43_MMIO_DMA5_REASON)
            & 0x0000DC00;
+*/
 
        b43_interrupt_ack(dev, reason);
        /* disable all IRQs. They are enabled again in the bottom half. */
-       dev->irq_savedstate = b43_interrupt_disable(dev, B43_IRQ_ALL);
+       b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, 0);
        /* save the reason code and call our bottom half. */
        dev->irq_reason = reason;
        tasklet_schedule(&dev->isr_tasklet);
-      out:
+out:
        mmiowb();
        spin_unlock(&dev->wl->irq_lock);
 
        return ret;
 }
 
-static void do_release_fw(struct b43_firmware_file *fw)
+void b43_do_release_fw(struct b43_firmware_file *fw)
 {
        release_firmware(fw->data);
        fw->data = NULL;
@@ -1832,30 +1930,30 @@ static void do_release_fw(struct b43_firmware_file *fw)
 
 static void b43_release_firmware(struct b43_wldev *dev)
 {
-       do_release_fw(&dev->fw.ucode);
-       do_release_fw(&dev->fw.pcm);
-       do_release_fw(&dev->fw.initvals);
-       do_release_fw(&dev->fw.initvals_band);
+       b43_do_release_fw(&dev->fw.ucode);
+       b43_do_release_fw(&dev->fw.pcm);
+       b43_do_release_fw(&dev->fw.initvals);
+       b43_do_release_fw(&dev->fw.initvals_band);
 }
 
 static void b43_print_fw_helptext(struct b43_wl *wl, bool error)
 {
-       const char *text;
+       const char text[] =
+               "You must go to " \
+               "http://wireless.kernel.org/en/users/Drivers/b43#devicefirmware " \
+               "and download the correct firmware for this driver version. " \
+               "Please carefully read all instructions on this website.\n";
 
-       text = "You must go to "
-              "http://linuxwireless.org/en/users/Drivers/b43#devicefirmware "
-              "and download the latest firmware (version 4).\n";
        if (error)
                b43err(wl, text);
        else
                b43warn(wl, text);
 }
 
-static int do_request_fw(struct b43_wldev *dev,
-                        const char *name,
-                        struct b43_firmware_file *fw)
+int b43_do_request_fw(struct b43_request_fw_context *ctx,
+                     const char *name,
+                     struct b43_firmware_file *fw)
 {
-       char path[sizeof(modparam_fwpostfix) + 32];
        const struct firmware *blob;
        struct b43_fw_header *hdr;
        u32 size;
@@ -1863,23 +1961,49 @@ static int do_request_fw(struct b43_wldev *dev,
 
        if (!name) {
                /* Don't fetch anything. Free possibly cached firmware. */
-               do_release_fw(fw);
+               /* FIXME: We should probably keep it anyway, to save some headache
+                * on suspend/resume with multiband devices. */
+               b43_do_release_fw(fw);
                return 0;
        }
        if (fw->filename) {
-               if (strcmp(fw->filename, name) == 0)
+               if ((fw->type == ctx->req_type) &&
+                   (strcmp(fw->filename, name) == 0))
                        return 0; /* Already have this fw. */
                /* Free the cached firmware first. */
-               do_release_fw(fw);
+               /* FIXME: We should probably do this later after we successfully
+                * got the new fw. This could reduce headache with multiband devices.
+                * We could also redesign this to cache the firmware for all possible
+                * bands all the time. */
+               b43_do_release_fw(fw);
+       }
+
+       switch (ctx->req_type) {
+       case B43_FWTYPE_PROPRIETARY:
+               snprintf(ctx->fwname, sizeof(ctx->fwname),
+                        "b43%s/%s.fw",
+                        modparam_fwpostfix, name);
+               break;
+       case B43_FWTYPE_OPENSOURCE:
+               snprintf(ctx->fwname, sizeof(ctx->fwname),
+                        "b43-open%s/%s.fw",
+                        modparam_fwpostfix, name);
+               break;
+       default:
+               B43_WARN_ON(1);
+               return -ENOSYS;
        }
-
-       snprintf(path, ARRAY_SIZE(path),
-                "b43%s/%s.fw",
-                modparam_fwpostfix, name);
-       err = request_firmware(&blob, path, dev->dev->dev);
-       if (err) {
-               b43err(dev->wl, "Firmware file \"%s\" not found "
-                      "or load failed.\n", path);
+       err = request_firmware(&blob, ctx->fwname, ctx->dev->dev->dev);
+       if (err == -ENOENT) {
+               snprintf(ctx->errors[ctx->req_type],
+                        sizeof(ctx->errors[ctx->req_type]),
+                        "Firmware file \"%s\" not found\n", ctx->fwname);
+               return err;
+       } else if (err) {
+               snprintf(ctx->errors[ctx->req_type],
+                        sizeof(ctx->errors[ctx->req_type]),
+                        "Firmware file \"%s\" request failed (err=%d)\n",
+                        ctx->fwname, err);
                return err;
        }
        if (blob->size < sizeof(struct b43_fw_header))
@@ -1902,20 +2026,24 @@ static int do_request_fw(struct b43_wldev *dev,
 
        fw->data = blob;
        fw->filename = name;
+       fw->type = ctx->req_type;
 
        return 0;
 
 err_format:
-       b43err(dev->wl, "Firmware file \"%s\" format error.\n", path);
+       snprintf(ctx->errors[ctx->req_type],
+                sizeof(ctx->errors[ctx->req_type]),
+                "Firmware file \"%s\" format error.\n", ctx->fwname);
        release_firmware(blob);
 
        return -EPROTO;
 }
 
-static int b43_request_firmware(struct b43_wldev *dev)
+static int b43_try_request_fw(struct b43_request_fw_context *ctx)
 {
-       struct b43_firmware *fw = &dev->fw;
-       const u8 rev = dev->dev->id.revision;
+       struct b43_wldev *dev = ctx->dev;
+       struct b43_firmware *fw = &ctx->dev->fw;
+       const u8 rev = ctx->dev->dev->id.revision;
        const char *filename;
        u32 tmshigh;
        int err;
@@ -1930,7 +2058,7 @@ static int b43_request_firmware(struct b43_wldev *dev)
                filename = "ucode13";
        else
                goto err_no_ucode;
-       err = do_request_fw(dev, filename, &fw->ucode);
+       err = b43_do_request_fw(ctx, filename, &fw->ucode);
        if (err)
                goto err_load;
 
@@ -1941,8 +2069,13 @@ static int b43_request_firmware(struct b43_wldev *dev)
                filename = NULL;
        else
                goto err_no_pcm;
-       err = do_request_fw(dev, filename, &fw->pcm);
-       if (err)
+       fw->pcm_request_failed = 0;
+       err = b43_do_request_fw(ctx, filename, &fw->pcm);
+       if (err == -ENOENT) {
+               /* We did not find a PCM file? Not fatal, but
+                * core rev <= 10 must do without hwcrypto then. */
+               fw->pcm_request_failed = 1;
+       } else if (err)
                goto err_load;
 
        /* Get initvals */
@@ -1960,7 +2093,7 @@ static int b43_request_firmware(struct b43_wldev *dev)
                if ((rev >= 5) && (rev <= 10))
                        filename = "b0g0initvals5";
                else if (rev >= 13)
-                       filename = "lp0initvals13";
+                       filename = "b0g0initvals13";
                else
                        goto err_no_initvals;
                break;
@@ -1973,7 +2106,7 @@ static int b43_request_firmware(struct b43_wldev *dev)
        default:
                goto err_no_initvals;
        }
-       err = do_request_fw(dev, filename, &fw->initvals);
+       err = b43_do_request_fw(ctx, filename, &fw->initvals);
        if (err)
                goto err_load;
 
@@ -2007,30 +2140,34 @@ static int b43_request_firmware(struct b43_wldev *dev)
        default:
                goto err_no_initvals;
        }
-       err = do_request_fw(dev, filename, &fw->initvals_band);
+       err = b43_do_request_fw(ctx, filename, &fw->initvals_band);
        if (err)
                goto err_load;
 
        return 0;
 
-err_load:
-       b43_print_fw_helptext(dev->wl, 1);
-       goto error;
-
 err_no_ucode:
-       err = -ENODEV;
-       b43err(dev->wl, "No microcode available for core rev %u\n", rev);
+       err = ctx->fatal_failure = -EOPNOTSUPP;
+       b43err(dev->wl, "The driver does not know which firmware (ucode) "
+              "is required for your device (wl-core rev %u)\n", rev);
        goto error;
 
 err_no_pcm:
-       err = -ENODEV;
-       b43err(dev->wl, "No PCM available for core rev %u\n", rev);
+       err = ctx->fatal_failure = -EOPNOTSUPP;
+       b43err(dev->wl, "The driver does not know which firmware (PCM) "
+              "is required for your device (wl-core rev %u)\n", rev);
        goto error;
 
 err_no_initvals:
-       err = -ENODEV;
-       b43err(dev->wl, "No Initial Values firmware file for PHY %u, "
-              "core rev %u\n", dev->phy.type, rev);
+       err = ctx->fatal_failure = -EOPNOTSUPP;
+       b43err(dev->wl, "The driver does not know which firmware (initvals) "
+              "is required for your device (wl-core rev %u)\n", rev);
+       goto error;
+
+err_load:
+       /* We failed to load this firmware image. The error message
+        * already is in ctx->errors. Return and let our caller decide
+        * what to do. */
        goto error;
 
 error:
@@ -2038,6 +2175,48 @@ error:
        return err;
 }
 
+static int b43_request_firmware(struct b43_wldev *dev)
+{
+       struct b43_request_fw_context *ctx;
+       unsigned int i;
+       int err;
+       const char *errmsg;
+
+       ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
+       if (!ctx)
+               return -ENOMEM;
+       ctx->dev = dev;
+
+       ctx->req_type = B43_FWTYPE_PROPRIETARY;
+       err = b43_try_request_fw(ctx);
+       if (!err)
+               goto out; /* Successfully loaded it. */
+       err = ctx->fatal_failure;
+       if (err)
+               goto out;
+
+       ctx->req_type = B43_FWTYPE_OPENSOURCE;
+       err = b43_try_request_fw(ctx);
+       if (!err)
+               goto out; /* Successfully loaded it. */
+       err = ctx->fatal_failure;
+       if (err)
+               goto out;
+
+       /* Could not find a usable firmware. Print the errors. */
+       for (i = 0; i < B43_NR_FWTYPES; i++) {
+               errmsg = ctx->errors[i];
+               if (strlen(errmsg))
+                       b43err(dev->wl, errmsg);
+       }
+       b43_print_fw_helptext(dev->wl, 1);
+       err = -ENOENT;
+
+out:
+       kfree(ctx);
+       return err;
+}
+
 static int b43_upload_microcode(struct b43_wldev *dev)
 {
        const size_t hdr_len = sizeof(struct b43_fw_header);
@@ -2124,18 +2303,35 @@ static int b43_upload_microcode(struct b43_wldev *dev)
                err = -EOPNOTSUPP;
                goto error;
        }
-       b43info(dev->wl, "Loading firmware version %u.%u "
-               "(20%.2i-%.2i-%.2i %.2i:%.2i:%.2i)\n",
-               fwrev, fwpatch,
-               (fwdate >> 12) & 0xF, (fwdate >> 8) & 0xF, fwdate & 0xFF,
-               (fwtime >> 11) & 0x1F, (fwtime >> 5) & 0x3F, fwtime & 0x1F);
-
        dev->fw.rev = fwrev;
        dev->fw.patch = fwpatch;
+       dev->fw.opensource = (fwdate == 0xFFFF);
+
+       if (dev->fw.opensource) {
+               /* Patchlevel info is encoded in the "time" field. */
+               dev->fw.patch = fwtime;
+               b43info(dev->wl, "Loading OpenSource firmware version %u.%u%s\n",
+                       dev->fw.rev, dev->fw.patch,
+                       dev->fw.pcm_request_failed ? " (Hardware crypto not supported)" : "");
+       } else {
+               b43info(dev->wl, "Loading firmware version %u.%u "
+                       "(20%.2i-%.2i-%.2i %.2i:%.2i:%.2i)\n",
+                       fwrev, fwpatch,
+                       (fwdate >> 12) & 0xF, (fwdate >> 8) & 0xF, fwdate & 0xFF,
+                       (fwtime >> 11) & 0x1F, (fwtime >> 5) & 0x3F, fwtime & 0x1F);
+               if (dev->fw.pcm_request_failed) {
+                       b43warn(dev->wl, "No \"pcm5.fw\" firmware file found. "
+                               "Hardware accelerated cryptography is disabled.\n");
+                       b43_print_fw_helptext(dev->wl, 0);
+               }
+       }
 
        if (b43_is_old_txhdr_format(dev)) {
+               /* We're over the deadline, but we keep support for old fw
+                * until it turns out to be in major conflict with something new. */
                b43warn(dev->wl, "You are using an old firmware image. "
-                       "Support for old firmware will be removed in July 2008.\n");
+                       "Support for old firmware will be removed soon "
+                       "(official deadline was July 2008).\n");
                b43_print_fw_helptext(dev->wl, 0);
        }
 
@@ -2310,6 +2506,19 @@ static void b43_gpio_cleanup(struct b43_wldev *dev)
 /* http://bcm-specs.sipsolutions.net/EnableMac */
 void b43_mac_enable(struct b43_wldev *dev)
 {
+       if (b43_debug(dev, B43_DBG_FIRMWARE)) {
+               u16 fwstate;
+
+               fwstate = b43_shm_read16(dev, B43_SHM_SHARED,
+                                        B43_SHM_SH_UCODESTAT);
+               if ((fwstate != B43_SHM_SH_UCODESTAT_SUSP) &&
+                   (fwstate != B43_SHM_SH_UCODESTAT_SLEEP)) {
+                       b43err(dev->wl, "b43_mac_enable(): The firmware "
+                              "should be suspended, but current state is %u\n",
+                              fwstate);
+               }
+       }
+
        dev->mac_suspended--;
        B43_WARN_ON(dev->mac_suspended < 0);
        if (dev->mac_suspended == 0) {
@@ -2376,9 +2585,10 @@ static void b43_adjust_opmode(struct b43_wldev *dev)
        ctl &= ~B43_MACCTL_BEACPROMISC;
        ctl |= B43_MACCTL_INFRA;
 
-       if (b43_is_mode(wl, IEEE80211_IF_TYPE_AP))
+       if (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
+           b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT))
                ctl |= B43_MACCTL_AP;
-       else if (b43_is_mode(wl, IEEE80211_IF_TYPE_IBSS))
+       else if (b43_is_mode(wl, NL80211_IFTYPE_ADHOC))
                ctl &= ~B43_MACCTL_INFRA;
 
        if (wl->filter_flags & FIF_CONTROL)
@@ -2488,9 +2698,8 @@ static void b43_mgmtframe_txantenna(struct b43_wldev *dev, int antenna)
 /* This is the opposite of b43_chip_init() */
 static void b43_chip_exit(struct b43_wldev *dev)
 {
-       b43_radio_turn_off(dev, 1);
+       b43_phy_exit(dev);
        b43_gpio_cleanup(dev);
-       b43_lo_g_cleanup(dev);
        /* firmware is released later */
 }
 
@@ -2500,7 +2709,7 @@ static void b43_chip_exit(struct b43_wldev *dev)
 static int b43_chip_init(struct b43_wldev *dev)
 {
        struct b43_phy *phy = &dev->phy;
-       int err, tmp;
+       int err;
        u32 value32, macctl;
        u16 value16;
 
@@ -2525,19 +2734,20 @@ static int b43_chip_init(struct b43_wldev *dev)
        err = b43_upload_initvals(dev);
        if (err)
                goto err_gpio_clean;
-       b43_radio_turn_on(dev);
 
-       b43_write16(dev, 0x03E6, 0x0000);
+       /* Turn the Analog on and initialize the PHY. */
+       phy->ops->switch_analog(dev, 1);
        err = b43_phy_init(dev);
        if (err)
-               goto err_radio_off;
+               goto err_gpio_clean;
 
-       /* Select initial Interference Mitigation. */
-       tmp = phy->interfmode;
-       phy->interfmode = B43_INTERFMODE_NONE;
-       b43_radio_set_interference_mitigation(dev, tmp);
+       /* Disable Interference Mitigation. */
+       if (phy->ops->interf_mitigation)
+               phy->ops->interf_mitigation(dev, B43_INTERFMODE_NONE);
 
-       b43_set_rx_antenna(dev, B43_ANTENNA_DEFAULT);
+       /* Select the antennae */
+       if (phy->ops->set_rx_antenna)
+               phy->ops->set_rx_antenna(dev, B43_ANTENNA_DEFAULT);
        b43_mgmtframe_txantenna(dev, B43_ANTENNA_DEFAULT);
 
        if (phy->type == B43_PHYTYPE_B) {
@@ -2590,8 +2800,6 @@ static int b43_chip_init(struct b43_wldev *dev)
 out:
        return err;
 
-err_radio_off:
-       b43_radio_turn_off(dev, 1);
 err_gpio_clean:
        b43_gpio_cleanup(dev);
        return err;
@@ -2599,25 +2807,13 @@ err_gpio_clean:
 
 static void b43_periodic_every60sec(struct b43_wldev *dev)
 {
-       struct b43_phy *phy = &dev->phy;
+       const struct b43_phy_operations *ops = dev->phy.ops;
 
-       if (phy->type != B43_PHYTYPE_G)
-               return;
-       if (dev->dev->bus->sprom.boardflags_lo & B43_BFL_RSSI) {
-               b43_mac_suspend(dev);
-               b43_calc_nrssi_slope(dev);
-               if ((phy->radio_ver == 0x2050) && (phy->radio_rev == 8)) {
-                       u8 old_chan = phy->channel;
-
-                       /* VCO Calibration */
-                       if (old_chan >= 8)
-                               b43_radio_selectchannel(dev, 1, 0);
-                       else
-                               b43_radio_selectchannel(dev, 13, 0);
-                       b43_radio_selectchannel(dev, old_chan, 0);
-               }
-               b43_mac_enable(dev);
-       }
+       if (ops->pwork_60sec)
+               ops->pwork_60sec(dev);
+
+       /* Force check the TX power emission now. */
+       b43_phy_txpower_check(dev, B43_TXPWR_IGNORE_TIME);
 }
 
 static void b43_periodic_every30sec(struct b43_wldev *dev)
@@ -2629,33 +2825,24 @@ static void b43_periodic_every30sec(struct b43_wldev *dev)
 static void b43_periodic_every15sec(struct b43_wldev *dev)
 {
        struct b43_phy *phy = &dev->phy;
-
-       if (phy->type == B43_PHYTYPE_G) {
-               //TODO: update_aci_moving_average
-               if (phy->aci_enable && phy->aci_wlan_automatic) {
-                       b43_mac_suspend(dev);
-                       if (!phy->aci_enable && 1 /*TODO: not scanning? */ ) {
-                               if (0 /*TODO: bunch of conditions */ ) {
-                                       b43_radio_set_interference_mitigation
-                                           (dev, B43_INTERFMODE_MANUALWLAN);
-                               }
-                       } else if (1 /*TODO*/) {
-                               /*
-                                  if ((aci_average > 1000) && !(b43_radio_aci_scan(dev))) {
-                                  b43_radio_set_interference_mitigation(dev,
-                                  B43_INTERFMODE_NONE);
-                                  }
-                                */
-                       }
-                       b43_mac_enable(dev);
-               } else if (phy->interfmode == B43_INTERFMODE_NONWLAN &&
-                          phy->rev == 1) {
-                       //TODO: implement rev1 workaround
+       u16 wdr;
+
+       if (dev->fw.opensource) {
+               /* Check if the firmware is still alive.
+                * It will reset the watchdog counter to 0 in its idle loop. */
+               wdr = b43_shm_read16(dev, B43_SHM_SCRATCH, B43_WATCHDOG_REG);
+               if (unlikely(wdr)) {
+                       b43err(dev->wl, "Firmware watchdog: The firmware died!\n");
+                       b43_controller_restart(dev, "Firmware watchdog");
+                       return;
+               } else {
+                       b43_shm_write16(dev, B43_SHM_SCRATCH,
+                                       B43_WATCHDOG_REG, 1);
                }
        }
-       b43_phy_xmitpower(dev); //FIXME: unless scanning?
-       b43_lo_g_maintanance_work(dev);
-       //TODO for APHY (temperature?)
+
+       if (phy->ops->pwork_15sec)
+               phy->ops->pwork_15sec(dev);
 
        atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
        wmb();
@@ -2770,7 +2957,8 @@ static void b43_security_init(struct b43_wldev *dev)
        b43_clear_keys(dev);
 }
 
-static int b43_rng_read(struct hwrng *rng, u32 * data)
+#ifdef CONFIG_B43_HWRNG
+static int b43_rng_read(struct hwrng *rng, u32 *data)
 {
        struct b43_wl *wl = (struct b43_wl *)rng->priv;
        unsigned long flags;
@@ -2785,17 +2973,21 @@ static int b43_rng_read(struct hwrng *rng, u32 * data)
 
        return (sizeof(u16));
 }
+#endif /* CONFIG_B43_HWRNG */
 
 static void b43_rng_exit(struct b43_wl *wl)
 {
+#ifdef CONFIG_B43_HWRNG
        if (wl->rng_initialized)
                hwrng_unregister(&wl->rng);
+#endif /* CONFIG_B43_HWRNG */
 }
 
 static int b43_rng_init(struct b43_wl *wl)
 {
-       int err;
+       int err = 0;
 
+#ifdef CONFIG_B43_HWRNG
        snprintf(wl->rng_name, ARRAY_SIZE(wl->rng_name),
                 "%s_%s", KBUILD_MODNAME, wiphy_name(wl->hw->wiphy));
        wl->rng.name = wl->rng_name;
@@ -2808,13 +3000,13 @@ static int b43_rng_init(struct b43_wl *wl)
                b43err(wl, "Failed to register the random "
                       "number generator (%d)\n", err);
        }
+#endif /* CONFIG_B43_HWRNG */
 
        return err;
 }
 
 static int b43_op_tx(struct ieee80211_hw *hw,
-                    struct sk_buff *skb,
-                    struct ieee80211_tx_control *ctl)
+                    struct sk_buff *skb)
 {
        struct b43_wl *wl = hw_to_b43_wl(hw);
        struct b43_wldev *dev = wl->current_dev;
@@ -2823,12 +3015,11 @@ static int b43_op_tx(struct ieee80211_hw *hw,
 
        if (unlikely(skb->len < 2 + 2 + 6)) {
                /* Too short, this can't be a valid frame. */
-               dev_kfree_skb_any(skb);
-               return NETDEV_TX_OK;
+               goto drop_packet;
        }
        B43_WARN_ON(skb_shinfo(skb)->nr_frags);
        if (unlikely(!dev))
-               return NETDEV_TX_BUSY;
+               goto drop_packet;
 
        /* Transmissions on seperate queues can run concurrently. */
        read_lock_irqsave(&wl->tx_lock, flags);
@@ -2836,17 +3027,22 @@ static int b43_op_tx(struct ieee80211_hw *hw,
        err = -ENODEV;
        if (likely(b43_status(dev) >= B43_STAT_STARTED)) {
                if (b43_using_pio_transfers(dev))
-                       err = b43_pio_tx(dev, skb, ctl);
+                       err = b43_pio_tx(dev, skb);
                else
-                       err = b43_dma_tx(dev, skb, ctl);
+                       err = b43_dma_tx(dev, skb);
        }
 
        read_unlock_irqrestore(&wl->tx_lock, flags);
 
        if (unlikely(err))
-               return NETDEV_TX_BUSY;
+               goto drop_packet;
        return NETDEV_TX_OK;
-}
+
+drop_packet:
+       /* We can not transmit this packet. Drop it. */
+       dev_kfree_skb_any(skb);
+       return NETDEV_TX_OK;
+}
 
 /* Locking: wl->irq_lock */
 static void b43_qos_params_upload(struct b43_wldev *dev,
@@ -2854,53 +3050,20 @@ static void b43_qos_params_upload(struct b43_wldev *dev,
                                  u16 shm_offset)
 {
        u16 params[B43_NR_QOSPARAMS];
-       int cw_min, cw_max, aifs, bslots, tmp;
+       int bslots, tmp;
        unsigned int i;
 
-       const u16 aCWmin = 0x0001;
-       const u16 aCWmax = 0x03FF;
-
-       /* Calculate the default values for the parameters, if needed. */
-       switch (shm_offset) {
-       case B43_QOS_VOICE:
-               aifs = (p->aifs == -1) ? 2 : p->aifs;
-               cw_min = (p->cw_min == 0) ? ((aCWmin + 1) / 4 - 1) : p->cw_min;
-               cw_max = (p->cw_max == 0) ? ((aCWmin + 1) / 2 - 1) : p->cw_max;
-               break;
-       case B43_QOS_VIDEO:
-               aifs = (p->aifs == -1) ? 2 : p->aifs;
-               cw_min = (p->cw_min == 0) ? ((aCWmin + 1) / 2 - 1) : p->cw_min;
-               cw_max = (p->cw_max == 0) ? aCWmin : p->cw_max;
-               break;
-       case B43_QOS_BESTEFFORT:
-               aifs = (p->aifs == -1) ? 3 : p->aifs;
-               cw_min = (p->cw_min == 0) ? aCWmin : p->cw_min;
-               cw_max = (p->cw_max == 0) ? aCWmax : p->cw_max;
-               break;
-       case B43_QOS_BACKGROUND:
-               aifs = (p->aifs == -1) ? 7 : p->aifs;
-               cw_min = (p->cw_min == 0) ? aCWmin : p->cw_min;
-               cw_max = (p->cw_max == 0) ? aCWmax : p->cw_max;
-               break;
-       default:
-               B43_WARN_ON(1);
-               return;
-       }
-       if (cw_min <= 0)
-               cw_min = aCWmin;
-       if (cw_max <= 0)
-               cw_max = aCWmin;
-       bslots = b43_read16(dev, B43_MMIO_RNG) % cw_min;
+       bslots = b43_read16(dev, B43_MMIO_RNG) & p->cw_min;
 
        memset(&params, 0, sizeof(params));
 
        params[B43_QOSPARAM_TXOP] = p->txop * 32;
-       params[B43_QOSPARAM_CWMIN] = cw_min;
-       params[B43_QOSPARAM_CWMAX] = cw_max;
-       params[B43_QOSPARAM_CWCUR] = cw_min;
-       params[B43_QOSPARAM_AIFS] = aifs;
+       params[B43_QOSPARAM_CWMIN] = p->cw_min;
+       params[B43_QOSPARAM_CWMAX] = p->cw_max;
+       params[B43_QOSPARAM_CWCUR] = p->cw_min;
+       params[B43_QOSPARAM_AIFS] = p->aifs;
        params[B43_QOSPARAM_BSLOTS] = bslots;
-       params[B43_QOSPARAM_REGGAP] = bslots + aifs;
+       params[B43_QOSPARAM_REGGAP] = bslots + p->aifs;
 
        for (i = 0; i < ARRAY_SIZE(params); i++) {
                if (i == B43_QOSPARAM_STATUS) {
@@ -2919,36 +3082,31 @@ static void b43_qos_params_upload(struct b43_wldev *dev,
        }
 }
 
-/* Update the QOS parameters in hardware. */
-static void b43_qos_update(struct b43_wldev *dev)
+/* Mapping of mac80211 queue numbers to b43 QoS SHM offsets. */
+static const u16 b43_qos_shm_offsets[] = {
+       /* [mac80211-queue-nr] = SHM_OFFSET, */
+       [0] = B43_QOS_VOICE,
+       [1] = B43_QOS_VIDEO,
+       [2] = B43_QOS_BESTEFFORT,
+       [3] = B43_QOS_BACKGROUND,
+};
+
+/* Update all QOS parameters in hardware. */
+static void b43_qos_upload_all(struct b43_wldev *dev)
 {
        struct b43_wl *wl = dev->wl;
        struct b43_qos_params *params;
-       unsigned long flags;
        unsigned int i;
 
-       /* Mapping of mac80211 queues to b43 SHM offsets. */
-       static const u16 qos_shm_offsets[] = {
-               [0] = B43_QOS_VOICE,
-               [1] = B43_QOS_VIDEO,
-               [2] = B43_QOS_BESTEFFORT,
-               [3] = B43_QOS_BACKGROUND,
-       };
-       BUILD_BUG_ON(ARRAY_SIZE(qos_shm_offsets) != ARRAY_SIZE(wl->qos_params));
+       BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
+                    ARRAY_SIZE(wl->qos_params));
 
        b43_mac_suspend(dev);
-       spin_lock_irqsave(&wl->irq_lock, flags);
-
        for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++) {
                params = &(wl->qos_params[i]);
-               if (params->need_hw_update) {
-                       b43_qos_params_upload(dev, &(params->p),
-                                             qos_shm_offsets[i]);
-                       params->need_hw_update = 0;
-               }
+               b43_qos_params_upload(dev, &(params->p),
+                                     b43_qos_shm_offsets[i]);
        }
-
-       spin_unlock_irqrestore(&wl->irq_lock, flags);
        b43_mac_enable(dev);
 }
 
@@ -2957,25 +3115,50 @@ static void b43_qos_clear(struct b43_wl *wl)
        struct b43_qos_params *params;
        unsigned int i;
 
+       /* Initialize QoS parameters to sane defaults. */
+
+       BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
+                    ARRAY_SIZE(wl->qos_params));
+
        for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++) {
                params = &(wl->qos_params[i]);
 
-               memset(&(params->p), 0, sizeof(params->p));
-               params->p.aifs = -1;
-               params->need_hw_update = 1;
+               switch (b43_qos_shm_offsets[i]) {
+               case B43_QOS_VOICE:
+                       params->p.txop = 0;
+                       params->p.aifs = 2;
+                       params->p.cw_min = 0x0001;
+                       params->p.cw_max = 0x0001;
+                       break;
+               case B43_QOS_VIDEO:
+                       params->p.txop = 0;
+                       params->p.aifs = 2;
+                       params->p.cw_min = 0x0001;
+                       params->p.cw_max = 0x0001;
+                       break;
+               case B43_QOS_BESTEFFORT:
+                       params->p.txop = 0;
+                       params->p.aifs = 3;
+                       params->p.cw_min = 0x0001;
+                       params->p.cw_max = 0x03FF;
+                       break;
+               case B43_QOS_BACKGROUND:
+                       params->p.txop = 0;
+                       params->p.aifs = 7;
+                       params->p.cw_min = 0x0001;
+                       params->p.cw_max = 0x03FF;
+                       break;
+               default:
+                       B43_WARN_ON(1);
+               }
        }
 }
 
 /* Initialize the core's QOS capabilities */
 static void b43_qos_init(struct b43_wldev *dev)
 {
-       struct b43_wl *wl = dev->wl;
-       unsigned int i;
-
        /* Upload the current QOS parameters. */
-       for (i = 0; i < ARRAY_SIZE(wl->qos_params); i++)
-               wl->qos_params[i].need_hw_update = 1;
-       b43_qos_update(dev);
+       b43_qos_upload_all(dev);
 
        /* Enable QOS support. */
        b43_hf_write(dev, b43_hf_read(dev) | B43_HF_EDCF);
@@ -2984,25 +3167,13 @@ static void b43_qos_init(struct b43_wldev *dev)
                    | B43_MMIO_IFSCTL_USE_EDCF);
 }
 
-static void b43_qos_update_work(struct work_struct *work)
-{
-       struct b43_wl *wl = container_of(work, struct b43_wl, qos_update_work);
-       struct b43_wldev *dev;
-
-       mutex_lock(&wl->mutex);
-       dev = wl->current_dev;
-       if (likely(dev && (b43_status(dev) >= B43_STAT_INITIALIZED)))
-               b43_qos_update(dev);
-       mutex_unlock(&wl->mutex);
-}
-
 static int b43_op_conf_tx(struct ieee80211_hw *hw, u16 _queue,
                          const struct ieee80211_tx_queue_params *params)
 {
        struct b43_wl *wl = hw_to_b43_wl(hw);
-       unsigned long flags;
+       struct b43_wldev *dev;
        unsigned int queue = (unsigned int)_queue;
-       struct b43_qos_params *p;
+       int err = -ENODEV;
 
        if (queue >= ARRAY_SIZE(wl->qos_params)) {
                /* Queue not available or don't support setting
@@ -3010,16 +3181,25 @@ static int b43_op_conf_tx(struct ieee80211_hw *hw, u16 _queue,
                 * confuse mac80211. */
                return 0;
        }
+       BUILD_BUG_ON(ARRAY_SIZE(b43_qos_shm_offsets) !=
+                    ARRAY_SIZE(wl->qos_params));
 
-       spin_lock_irqsave(&wl->irq_lock, flags);
-       p = &(wl->qos_params[queue]);
-       memcpy(&(p->p), params, sizeof(p->p));
-       p->need_hw_update = 1;
-       spin_unlock_irqrestore(&wl->irq_lock, flags);
+       mutex_lock(&wl->mutex);
+       dev = wl->current_dev;
+       if (unlikely(!dev || (b43_status(dev) < B43_STAT_INITIALIZED)))
+               goto out_unlock;
+
+       memcpy(&(wl->qos_params[queue].p), params, sizeof(*params));
+       b43_mac_suspend(dev);
+       b43_qos_params_upload(dev, &(wl->qos_params[queue].p),
+                             b43_qos_shm_offsets[queue]);
+       b43_mac_enable(dev);
+       err = 0;
 
-       queue_work(hw->workqueue, &wl->qos_update_work);
+out_unlock:
+       mutex_unlock(&wl->mutex);
 
-       return 0;
+       return err;
 }
 
 static int b43_op_get_tx_stats(struct ieee80211_hw *hw,
@@ -3058,6 +3238,43 @@ static int b43_op_get_stats(struct ieee80211_hw *hw,
        return 0;
 }
 
+static u64 b43_op_get_tsf(struct ieee80211_hw *hw)
+{
+       struct b43_wl *wl = hw_to_b43_wl(hw);
+       struct b43_wldev *dev;
+       u64 tsf;
+
+       mutex_lock(&wl->mutex);
+       spin_lock_irq(&wl->irq_lock);
+       dev = wl->current_dev;
+
+       if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED))
+               b43_tsf_read(dev, &tsf);
+       else
+               tsf = 0;
+
+       spin_unlock_irq(&wl->irq_lock);
+       mutex_unlock(&wl->mutex);
+
+       return tsf;
+}
+
+static void b43_op_set_tsf(struct ieee80211_hw *hw, u64 tsf)
+{
+       struct b43_wl *wl = hw_to_b43_wl(hw);
+       struct b43_wldev *dev;
+
+       mutex_lock(&wl->mutex);
+       spin_lock_irq(&wl->irq_lock);
+       dev = wl->current_dev;
+
+       if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED))
+               b43_tsf_write(dev, tsf);
+
+       spin_unlock_irq(&wl->irq_lock);
+       mutex_unlock(&wl->mutex);
+}
+
 static void b43_put_phy_into_reset(struct b43_wldev *dev)
 {
        struct ssb_device *sdev = dev->dev;
@@ -3077,7 +3294,7 @@ static void b43_put_phy_into_reset(struct b43_wldev *dev)
        msleep(1);
 }
 
-static const char * band_to_string(enum ieee80211_band band)
+static const char *band_to_string(enum ieee80211_band band)
 {
        switch (band) {
        case IEEE80211_BAND_5GHZ:
@@ -3098,7 +3315,7 @@ static int b43_switch_band(struct b43_wl *wl, struct ieee80211_channel *chan)
        struct b43_wldev *down_dev;
        struct b43_wldev *d;
        int err;
-       bool gmode;
+       bool uninitialized_var(gmode);
        int prev_status;
 
        /* Find a device and PHY which supports the band. */
@@ -3182,15 +3399,31 @@ init_failure:
        return err;
 }
 
-static int b43_op_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
+/* Write the short and long frame retry limit values. */
+static void b43_set_retry_limits(struct b43_wldev *dev,
+                                unsigned int short_retry,
+                                unsigned int long_retry)
+{
+       /* The retry limit is a 4-bit counter. Enforce this to avoid overflowing
+        * the chip-internal counter. */
+       short_retry = min(short_retry, (unsigned int)0xF);
+       long_retry = min(long_retry, (unsigned int)0xF);
+
+       b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_SRLIMIT,
+                       short_retry);
+       b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_LRLIMIT,
+                       long_retry);
+}
+
+static int b43_op_config(struct ieee80211_hw *hw, u32 changed)
 {
        struct b43_wl *wl = hw_to_b43_wl(hw);
        struct b43_wldev *dev;
        struct b43_phy *phy;
+       struct ieee80211_conf *conf = &hw->conf;
        unsigned long flags;
        int antenna;
        int err = 0;
-       u32 savedirqs;
 
        mutex_lock(&wl->mutex);
 
@@ -3201,56 +3434,43 @@ static int b43_op_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
        dev = wl->current_dev;
        phy = &dev->phy;
 
-       /* Disable IRQs while reconfiguring the device.
-        * This makes it possible to drop the spinlock throughout
-        * the reconfiguration process. */
-       spin_lock_irqsave(&wl->irq_lock, flags);
-       if (b43_status(dev) < B43_STAT_STARTED) {
-               spin_unlock_irqrestore(&wl->irq_lock, flags);
-               goto out_unlock_mutex;
-       }
-       savedirqs = b43_interrupt_disable(dev, B43_IRQ_ALL);
-       spin_unlock_irqrestore(&wl->irq_lock, flags);
-       b43_synchronize_irq(dev);
+       b43_mac_suspend(dev);
+
+       if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS)
+               b43_set_retry_limits(dev, conf->short_frame_max_tx_count,
+                                         conf->long_frame_max_tx_count);
+       changed &= ~IEEE80211_CONF_CHANGE_RETRY_LIMITS;
+       if (!changed)
+               goto out_mac_enable;
 
        /* Switch to the requested channel.
         * The firmware takes care of races with the TX handler. */
        if (conf->channel->hw_value != phy->channel)
-               b43_radio_selectchannel(dev, conf->channel->hw_value, 0);
-
-       /* Enable/Disable ShortSlot timing. */
-       if ((!!(conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME)) !=
-           dev->short_slot) {
-               B43_WARN_ON(phy->type != B43_PHYTYPE_G);
-               if (conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME)
-                       b43_short_slot_timing_enable(dev);
-               else
-                       b43_short_slot_timing_disable(dev);
-       }
+               b43_switch_channel(dev, conf->channel->hw_value);
 
        dev->wl->radiotap_enabled = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
 
        /* Adjust the desired TX power level. */
        if (conf->power_level != 0) {
-               if (conf->power_level != phy->power_level) {
-                       phy->power_level = conf->power_level;
-                       b43_phy_xmitpower(dev);
+               spin_lock_irqsave(&wl->irq_lock, flags);
+               if (conf->power_level != phy->desired_txpower) {
+                       phy->desired_txpower = conf->power_level;
+                       b43_phy_txpower_check(dev, B43_TXPWR_IGNORE_TIME |
+                                                  B43_TXPWR_IGNORE_TSSI);
                }
+               spin_unlock_irqrestore(&wl->irq_lock, flags);
        }
 
        /* Antennas for RX and management frame TX. */
-       antenna = b43_antenna_from_ieee80211(dev, conf->antenna_sel_tx);
+       antenna = B43_ANTENNA_DEFAULT;
        b43_mgmtframe_txantenna(dev, antenna);
-       antenna = b43_antenna_from_ieee80211(dev, conf->antenna_sel_rx);
-       b43_set_rx_antenna(dev, antenna);
-
-       /* Update templates for AP mode. */
-       if (b43_is_mode(wl, IEEE80211_IF_TYPE_AP))
-               b43_set_beacon_int(dev, conf->beacon_int);
+       antenna = B43_ANTENNA_DEFAULT;
+       if (phy->ops->set_rx_antenna)
+               phy->ops->set_rx_antenna(dev, antenna);
 
        if (!!conf->radio_enabled != phy->radio_on) {
                if (conf->radio_enabled) {
-                       b43_radio_turn_on(dev);
+                       b43_software_rfkill(dev, RFKILL_STATE_UNBLOCKED);
                        b43info(dev->wl, "Radio turned on by software\n");
                        if (!dev->radio_hw_enable) {
                                b43info(dev->wl, "The hardware RF-kill button "
@@ -3258,48 +3478,168 @@ static int b43_op_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
                                        "Press the button to turn it on.\n");
                        }
                } else {
-                       b43_radio_turn_off(dev, 0);
+                       b43_software_rfkill(dev, RFKILL_STATE_SOFT_BLOCKED);
                        b43info(dev->wl, "Radio turned off by software\n");
                }
        }
 
-       spin_lock_irqsave(&wl->irq_lock, flags);
-       b43_interrupt_enable(dev, savedirqs);
-       mmiowb();
-       spin_unlock_irqrestore(&wl->irq_lock, flags);
-      out_unlock_mutex:
+out_mac_enable:
+       b43_mac_enable(dev);
+out_unlock_mutex:
        mutex_unlock(&wl->mutex);
 
        return err;
 }
 
-static int b43_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
-                          const u8 *local_addr, const u8 *addr,
-                          struct ieee80211_key_conf *key)
+static void b43_update_basic_rates(struct b43_wldev *dev, u32 brates)
+{
+       struct ieee80211_supported_band *sband =
+               dev->wl->hw->wiphy->bands[b43_current_band(dev->wl)];
+       struct ieee80211_rate *rate;
+       int i;
+       u16 basic, direct, offset, basic_offset, rateptr;
+
+       for (i = 0; i < sband->n_bitrates; i++) {
+               rate = &sband->bitrates[i];
+
+               if (b43_is_cck_rate(rate->hw_value)) {
+                       direct = B43_SHM_SH_CCKDIRECT;
+                       basic = B43_SHM_SH_CCKBASIC;
+                       offset = b43_plcp_get_ratecode_cck(rate->hw_value);
+                       offset &= 0xF;
+               } else {
+                       direct = B43_SHM_SH_OFDMDIRECT;
+                       basic = B43_SHM_SH_OFDMBASIC;
+                       offset = b43_plcp_get_ratecode_ofdm(rate->hw_value);
+                       offset &= 0xF;
+               }
+
+               rate = ieee80211_get_response_rate(sband, brates, rate->bitrate);
+
+               if (b43_is_cck_rate(rate->hw_value)) {
+                       basic_offset = b43_plcp_get_ratecode_cck(rate->hw_value);
+                       basic_offset &= 0xF;
+               } else {
+                       basic_offset = b43_plcp_get_ratecode_ofdm(rate->hw_value);
+                       basic_offset &= 0xF;
+               }
+
+               /*
+                * Get the pointer that we need to point to
+                * from the direct map
+                */
+               rateptr = b43_shm_read16(dev, B43_SHM_SHARED,
+                                        direct + 2 * basic_offset);
+               /* and write it to the basic map */
+               b43_shm_write16(dev, B43_SHM_SHARED, basic + 2 * offset,
+                               rateptr);
+       }
+}
+
+static void b43_op_bss_info_changed(struct ieee80211_hw *hw,
+                                   struct ieee80211_vif *vif,
+                                   struct ieee80211_bss_conf *conf,
+                                   u32 changed)
 {
        struct b43_wl *wl = hw_to_b43_wl(hw);
        struct b43_wldev *dev;
        unsigned long flags;
+
+       mutex_lock(&wl->mutex);
+
+       dev = wl->current_dev;
+       if (!dev || b43_status(dev) < B43_STAT_STARTED)
+               goto out_unlock_mutex;
+
+       B43_WARN_ON(wl->vif != vif);
+
+       if (changed & BSS_CHANGED_BSSID) {
+               spin_lock_irqsave(&wl->irq_lock, flags);
+               if (conf->bssid)
+                       memcpy(wl->bssid, conf->bssid, ETH_ALEN);
+               else
+                       memset(wl->bssid, 0, ETH_ALEN);
+
+               if (b43_status(dev) >= B43_STAT_INITIALIZED) {
+                       if (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
+                           b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT)) {
+                               B43_WARN_ON(vif->type != wl->if_type);
+                               if (changed & BSS_CHANGED_BEACON)
+                                       b43_update_templates(wl);
+                       } else if (b43_is_mode(wl, NL80211_IFTYPE_ADHOC)) {
+                               if (changed & BSS_CHANGED_BEACON)
+                                       b43_update_templates(wl);
+                       }
+                       b43_write_mac_bssid_templates(dev);
+               }
+               spin_unlock_irqrestore(&wl->irq_lock, flags);
+       }
+
+       b43_mac_suspend(dev);
+
+       /* Update templates for AP/mesh mode. */
+       if (changed & BSS_CHANGED_BEACON_INT &&
+           (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
+            b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT) ||
+            b43_is_mode(wl, NL80211_IFTYPE_ADHOC)))
+               b43_set_beacon_int(dev, conf->beacon_int);
+
+       if (changed & BSS_CHANGED_BASIC_RATES)
+               b43_update_basic_rates(dev, conf->basic_rates);
+
+       if (changed & BSS_CHANGED_ERP_SLOT) {
+               if (conf->use_short_slot)
+                       b43_short_slot_timing_enable(dev);
+               else
+                       b43_short_slot_timing_disable(dev);
+       }
+
+       b43_mac_enable(dev);
+out_unlock_mutex:
+       mutex_unlock(&wl->mutex);
+}
+
+static int b43_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
+                         struct ieee80211_vif *vif, struct ieee80211_sta *sta,
+                         struct ieee80211_key_conf *key)
+{
+       struct b43_wl *wl = hw_to_b43_wl(hw);
+       struct b43_wldev *dev;
        u8 algorithm;
        u8 index;
        int err;
-       DECLARE_MAC_BUF(mac);
+       static const u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
 
        if (modparam_nohwcrypt)
                return -ENOSPC; /* User disabled HW-crypto */
 
        mutex_lock(&wl->mutex);
-       spin_lock_irqsave(&wl->irq_lock, flags);
+       spin_lock_irq(&wl->irq_lock);
+       write_lock(&wl->tx_lock);
+       /* Why do we need all this locking here?
+        * mutex     -> Every config operation must take it.
+        * irq_lock  -> We modify the dev->key array, which is accessed
+        *              in the IRQ handlers.
+        * tx_lock   -> We modify the dev->key array, which is accessed
+        *              in the TX handler.
+        */
 
        dev = wl->current_dev;
        err = -ENODEV;
        if (!dev || b43_status(dev) < B43_STAT_INITIALIZED)
                goto out_unlock;
 
+       if (dev->fw.pcm_request_failed) {
+               /* We don't have firmware for the crypto engine.
+                * Must use software-crypto. */
+               err = -EOPNOTSUPP;
+               goto out_unlock;
+       }
+
        err = -EINVAL;
        switch (key->alg) {
        case ALG_WEP:
-               if (key->keylen == 5)
+               if (key->keylen == LEN_WEP40)
                        algorithm = B43_SEC_ALGO_WEP40;
                else
                        algorithm = B43_SEC_ALGO_WEP104;
@@ -3326,17 +3666,19 @@ static int b43_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
                        goto out_unlock;
                }
 
-               if (is_broadcast_ether_addr(addr)) {
-                       /* addr is FF:FF:FF:FF:FF:FF for default keys */
+               if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
+                       if (WARN_ON(!sta)) {
+                               err = -EOPNOTSUPP;
+                               goto out_unlock;
+                       }
+                       /* Pairwise key with an assigned MAC address. */
+                       err = b43_key_write(dev, -1, algorithm,
+                                           key->key, key->keylen,
+                                           sta->addr, key);
+               } else {
+                       /* Group key */
                        err = b43_key_write(dev, index, algorithm,
                                            key->key, key->keylen, NULL, key);
-               } else {
-                       /*
-                        * either pairwise key or address is 00:00:00:00:00:00
-                        * for transmit-only keys
-                        */
-                       err = b43_key_write(dev, -1, algorithm,
-                                           key->key, key->keylen, addr, key);
                }
                if (err)
                        goto out_unlock;
@@ -3359,15 +3701,19 @@ static int b43_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
        default:
                B43_WARN_ON(1);
        }
+
 out_unlock:
-       spin_unlock_irqrestore(&wl->irq_lock, flags);
-       mutex_unlock(&wl->mutex);
        if (!err) {
                b43dbg(wl, "%s hardware based encryption for keyidx: %d, "
-                      "mac: %s\n",
+                      "mac: %pM\n",
                       cmd == SET_KEY ? "Using" : "Disabling", key->keyidx,
-                      print_mac(mac, addr));
+                      sta ? sta->addr : bcast_addr);
+               b43_dump_keymemory(dev);
        }
+       write_unlock(&wl->tx_lock);
+       spin_unlock_irq(&wl->irq_lock);
+       mutex_unlock(&wl->mutex);
+
        return err;
 }
 
@@ -3408,40 +3754,6 @@ static void b43_op_configure_filter(struct ieee80211_hw *hw,
        spin_unlock_irqrestore(&wl->irq_lock, flags);
 }
 
-static int b43_op_config_interface(struct ieee80211_hw *hw,
-                                  struct ieee80211_vif *vif,
-                                  struct ieee80211_if_conf *conf)
-{
-       struct b43_wl *wl = hw_to_b43_wl(hw);
-       struct b43_wldev *dev = wl->current_dev;
-       unsigned long flags;
-
-       if (!dev)
-               return -ENODEV;
-       mutex_lock(&wl->mutex);
-       spin_lock_irqsave(&wl->irq_lock, flags);
-       B43_WARN_ON(wl->vif != vif);
-       if (conf->bssid)
-               memcpy(wl->bssid, conf->bssid, ETH_ALEN);
-       else
-               memset(wl->bssid, 0, ETH_ALEN);
-       if (b43_status(dev) >= B43_STAT_INITIALIZED) {
-               if (b43_is_mode(wl, IEEE80211_IF_TYPE_AP)) {
-                       B43_WARN_ON(conf->type != IEEE80211_IF_TYPE_AP);
-                       b43_set_ssid(dev, conf->ssid, conf->ssid_len);
-                       if (conf->beacon) {
-                               b43_update_templates(wl, conf->beacon,
-                                                    conf->beacon_control);
-                       }
-               }
-               b43_write_mac_bssid_templates(dev);
-       }
-       spin_unlock_irqrestore(&wl->irq_lock, flags);
-       mutex_unlock(&wl->mutex);
-
-       return 0;
-}
-
 /* Locking: wl->mutex */
 static void b43_wireless_core_stop(struct b43_wldev *dev)
 {
@@ -3455,7 +3767,7 @@ static void b43_wireless_core_stop(struct b43_wldev *dev)
         * setting the status to INITIALIZED, as the interrupt handler
         * won't care about IRQs then. */
        spin_lock_irqsave(&wl->irq_lock, flags);
-       dev->irq_savedstate = b43_interrupt_disable(dev, B43_IRQ_ALL);
+       b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, 0);
        b43_read32(dev, B43_MMIO_GEN_IRQ_MASK); /* flush */
        spin_unlock_irqrestore(&wl->irq_lock, flags);
        b43_synchronize_irq(dev);
@@ -3496,8 +3808,7 @@ static int b43_wireless_core_start(struct b43_wldev *dev)
 
        /* Start data flow (TX/RX). */
        b43_mac_enable(dev);
-       b43_interrupt_enable(dev, dev->irq_savedstate);
-       ieee80211_start_queues(dev->wl->hw);
+       b43_write32(dev, B43_MMIO_GEN_IRQ_MASK, dev->irq_mask);
 
        /* Start maintainance work */
        b43_periodic_tasks_setup(dev);
@@ -3541,6 +3852,12 @@ static int b43_phy_versioning(struct b43_wldev *dev)
                break;
 #ifdef CONFIG_B43_NPHY
        case B43_PHYTYPE_N:
+               if (phy_rev > 4)
+                       unsupported = 1;
+               break;
+#endif
+#ifdef CONFIG_B43_PHY_LP
+       case B43_PHYTYPE_LP:
                if (phy_rev > 1)
                        unsupported = 1;
                break;
@@ -3594,7 +3911,11 @@ static int b43_phy_versioning(struct b43_wldev *dev)
                        unsupported = 1;
                break;
        case B43_PHYTYPE_N:
-               if (radio_ver != 0x2055)
+               if (radio_ver != 0x2055 && radio_ver != 0x2056)
+                       unsupported = 1;
+               break;
+       case B43_PHYTYPE_LP:
+               if (radio_ver != 0x2062)
                        unsupported = 1;
                break;
        default:
@@ -3623,48 +3944,15 @@ static int b43_phy_versioning(struct b43_wldev *dev)
 static void setup_struct_phy_for_init(struct b43_wldev *dev,
                                      struct b43_phy *phy)
 {
-       struct b43_txpower_lo_control *lo;
-       int i;
-
-       memset(phy->minlowsig, 0xFF, sizeof(phy->minlowsig));
-       memset(phy->minlowsigpos, 0, sizeof(phy->minlowsigpos));
-
-       phy->aci_enable = 0;
-       phy->aci_wlan_automatic = 0;
-       phy->aci_hw_rssi = 0;
-
-       phy->radio_off_context.valid = 0;
-
-       lo = phy->lo_control;
-       if (lo) {
-               memset(lo, 0, sizeof(*(phy->lo_control)));
-               lo->tx_bias = 0xFF;
-               INIT_LIST_HEAD(&lo->calib_list);
-       }
-       phy->max_lb_gain = 0;
-       phy->trsw_rx_gain = 0;
-       phy->txpwr_offset = 0;
-
-       /* NRSSI */
-       phy->nrssislope = 0;
-       for (i = 0; i < ARRAY_SIZE(phy->nrssi); i++)
-               phy->nrssi[i] = -1000;
-       for (i = 0; i < ARRAY_SIZE(phy->nrssi_lt); i++)
-               phy->nrssi_lt[i] = i;
-
-       phy->lofcal = 0xFFFF;
-       phy->initval = 0xFFFF;
-
-       phy->interfmode = B43_INTERFMODE_NONE;
-       phy->channel = 0xFF;
-
        phy->hardware_power_control = !!modparam_hwpctl;
-
+       phy->next_txpwr_check_time = jiffies;
        /* PHY TX errors counter. */
        atomic_set(&phy->txerr_cnt, B43_PHY_TX_BADNESS_LIMIT);
 
-       /* OFDM-table address caching. */
-       phy->ofdmtab_addr_direction = B43_OFDMTAB_DIRECTION_UNKNOWN;
+#if B43_DEBUG
+       phy->phy_locked = 0;
+       phy->radio_locked = 0;
+#endif
 }
 
 static void setup_struct_wldev_for_init(struct b43_wldev *dev)
@@ -3683,7 +3971,9 @@ static void setup_struct_wldev_for_init(struct b43_wldev *dev)
        /* IRQ related flags */
        dev->irq_reason = 0;
        memset(dev->dma_reason, 0, sizeof(dev->dma_reason));
-       dev->irq_savedstate = B43_IRQ_MASKTEMPLATE;
+       dev->irq_mask = B43_IRQ_MASKTEMPLATE;
+       if (b43_modparam_verbose < B43_VERBOSITY_DEBUG)
+               dev->irq_mask &= ~B43_IRQ_PHY_TXERR;
 
        dev->mac_suspended = 1;
 
@@ -3745,22 +4035,6 @@ static void b43_imcfglo_timeouts_workaround(struct b43_wldev *dev)
 #endif /* CONFIG_SSB_DRIVER_PCICORE */
 }
 
-/* Write the short and long frame retry limit values. */
-static void b43_set_retry_limits(struct b43_wldev *dev,
-                                unsigned int short_retry,
-                                unsigned int long_retry)
-{
-       /* The retry limit is a 4-bit counter. Enforce this to avoid overflowing
-        * the chip-internal counter. */
-       short_retry = min(short_retry, (unsigned int)0xF);
-       long_retry = min(long_retry, (unsigned int)0xF);
-
-       b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_SRLIMIT,
-                       short_retry);
-       b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_LRLIMIT,
-                       long_retry);
-}
-
 static void b43_set_synth_pu_delay(struct b43_wldev *dev, bool idle)
 {
        u16 pu_delay;
@@ -3770,7 +4044,7 @@ static void b43_set_synth_pu_delay(struct b43_wldev *dev, bool idle)
                pu_delay = 3700;
        else
                pu_delay = 1050;
-       if (b43_is_mode(dev->wl, IEEE80211_IF_TYPE_IBSS) || idle)
+       if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC) || idle)
                pu_delay = 500;
        if ((dev->phy.radio_ver == 0x2050) && (dev->phy.radio_rev == 8))
                pu_delay = max(pu_delay, (u16)2400);
@@ -3784,7 +4058,7 @@ static void b43_set_pretbtt(struct b43_wldev *dev)
        u16 pretbtt;
 
        /* The time value is in microseconds. */
-       if (b43_is_mode(dev->wl, IEEE80211_IF_TYPE_IBSS)) {
+       if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC)) {
                pretbtt = 2;
        } else {
                if (dev->phy.type == B43_PHYTYPE_A)
@@ -3800,7 +4074,6 @@ static void b43_set_pretbtt(struct b43_wldev *dev)
 /* Locking: wl->mutex */
 static void b43_wireless_core_exit(struct b43_wldev *dev)
 {
-       struct b43_phy *phy = &dev->phy;
        u32 macctl;
 
        B43_WARN_ON(b43_status(dev) > B43_STAT_INITIALIZED);
@@ -3821,12 +4094,7 @@ static void b43_wireless_core_exit(struct b43_wldev *dev)
        b43_dma_free(dev);
        b43_pio_free(dev);
        b43_chip_exit(dev);
-       b43_radio_turn_off(dev, 1);
-       b43_switch_analog(dev, 0);
-       if (phy->dyn_tssi_tbl)
-               kfree(phy->tssi2dbm);
-       kfree(phy->lo_control);
-       phy->lo_control = NULL;
+       dev->phy.ops->switch_analog(dev, 0);
        if (dev->wl->current_beacon) {
                dev_kfree_skb_any(dev->wl->current_beacon);
                dev->wl->current_beacon = NULL;
@@ -3857,29 +4125,23 @@ static int b43_wireless_core_init(struct b43_wldev *dev)
                b43_wireless_core_reset(dev, tmp);
        }
 
-       if ((phy->type == B43_PHYTYPE_B) || (phy->type == B43_PHYTYPE_G)) {
-               phy->lo_control =
-                   kzalloc(sizeof(*(phy->lo_control)), GFP_KERNEL);
-               if (!phy->lo_control) {
-                       err = -ENOMEM;
-                       goto err_busdown;
-               }
-       }
+       /* Reset all data structures. */
        setup_struct_wldev_for_init(dev);
-
-       err = b43_phy_init_tssi2dbm_table(dev);
-       if (err)
-               goto err_kfree_lo_control;
+       phy->ops->prepare_structs(dev);
 
        /* Enable IRQ routing to this device. */
        ssb_pcicore_dev_irqvecs_enable(&bus->pcicore, dev->dev);
 
        b43_imcfglo_timeouts_workaround(dev);
        b43_bluetooth_coext_disable(dev);
-       b43_phy_early_init(dev);
+       if (phy->ops->prepare_hardware) {
+               err = phy->ops->prepare_hardware(dev);
+               if (err)
+                       goto err_busdown;
+       }
        err = b43_chip_init(dev);
        if (err)
-               goto err_kfree_tssitbl;
+               goto err_busdown;
        b43_shm_write16(dev, B43_SHM_SHARED,
                        B43_SHM_SH_WLCOREREV, dev->dev->id.revision);
        hf = b43_hf_read(dev);
@@ -3889,11 +4151,21 @@ static int b43_wireless_core_init(struct b43_wldev *dev)
                        hf |= B43_HF_GDCW;
                if (sprom->boardflags_lo & B43_BFL_PACTRL)
                        hf |= B43_HF_OFDMPABOOST;
-       } else if (phy->type == B43_PHYTYPE_B) {
-               hf |= B43_HF_SYMW;
-               if (phy->rev >= 2 && phy->radio_ver == 0x2050)
-                       hf &= ~B43_HF_GDCW;
        }
+       if (phy->radio_ver == 0x2050) {
+               if (phy->radio_rev == 6)
+                       hf |= B43_HF_4318TSSI;
+               if (phy->radio_rev < 6)
+                       hf |= B43_HF_VCORECALC;
+       }
+       if (sprom->boardflags_lo & B43_BFL_XTAL_NOSLOW)
+               hf |= B43_HF_DSCRQ; /* Disable slowclock requests from ucode. */
+#ifdef CONFIG_SSB_DRIVER_PCICORE
+       if ((bus->bustype == SSB_BUSTYPE_PCI) &&
+           (bus->pcicore.dev->id.revision <= 10))
+               hf |= B43_HF_PCISCW; /* PCI slow clock workaround. */
+#endif
+       hf &= ~B43_HF_SKCFPUP;
        b43_hf_write(dev, hf);
 
        b43_set_retry_limits(dev, B43_DEFAULT_SHORT_RETRY_LIMIT,
@@ -3932,7 +4204,7 @@ static int b43_wireless_core_init(struct b43_wldev *dev)
        b43_set_synth_pu_delay(dev, 1);
        b43_bluetooth_coext_enable(dev);
 
-       ssb_bus_powerup(bus, 1);        /* Enable dynamic PCTL */
+       ssb_bus_powerup(bus, !(sprom->boardflags_lo & B43_BFL_XTAL_NOSLOW));
        b43_upload_card_macaddress(dev);
        b43_security_init(dev);
        if (!dev->suspend_in_progress)
@@ -3945,15 +4217,9 @@ static int b43_wireless_core_init(struct b43_wldev *dev)
 out:
        return err;
 
-      err_chip_exit:
+err_chip_exit:
        b43_chip_exit(dev);
-      err_kfree_tssitbl:
-       if (phy->dyn_tssi_tbl)
-               kfree(phy->tssi2dbm);
-      err_kfree_lo_control:
-       kfree(phy->lo_control);
-       phy->lo_control = NULL;
-      err_busdown:
+err_busdown:
        ssb_bus_may_powerdown(bus);
        B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
        return err;
@@ -3969,10 +4235,11 @@ static int b43_op_add_interface(struct ieee80211_hw *hw,
 
        /* TODO: allow WDS/AP devices to coexist */
 
-       if (conf->type != IEEE80211_IF_TYPE_AP &&
-           conf->type != IEEE80211_IF_TYPE_STA &&
-           conf->type != IEEE80211_IF_TYPE_WDS &&
-           conf->type != IEEE80211_IF_TYPE_IBSS)
+       if (conf->type != NL80211_IFTYPE_AP &&
+           conf->type != NL80211_IFTYPE_MESH_POINT &&
+           conf->type != NL80211_IFTYPE_STATION &&
+           conf->type != NL80211_IFTYPE_WDS &&
+           conf->type != NL80211_IFTYPE_ADHOC)
                return -EOPNOTSUPP;
 
        mutex_lock(&wl->mutex);
@@ -4043,6 +4310,9 @@ static int b43_op_start(struct ieee80211_hw *hw)
        wl->filter_flags = 0;
        wl->radiotap_enabled = 0;
        b43_qos_clear(wl);
+       wl->beacon0_uploaded = 0;
+       wl->beacon1_uploaded = 0;
+       wl->beacon_templates_virgin = 1;
 
        /* First register RFkill.
         * LEDs that are registered later depend on it. */
@@ -4084,7 +4354,6 @@ static void b43_op_stop(struct ieee80211_hw *hw)
        struct b43_wldev *dev = wl->current_dev;
 
        b43_rfkill_exit(dev);
-       cancel_work_sync(&(wl->qos_update_work));
        cancel_work_sync(&(wl->beacon_update_trigger));
 
        mutex_lock(&wl->mutex);
@@ -4092,71 +4361,59 @@ static void b43_op_stop(struct ieee80211_hw *hw)
                b43_wireless_core_stop(dev);
        b43_wireless_core_exit(dev);
        mutex_unlock(&wl->mutex);
-}
-
-static int b43_op_set_retry_limit(struct ieee80211_hw *hw,
-                                 u32 short_retry_limit, u32 long_retry_limit)
-{
-       struct b43_wl *wl = hw_to_b43_wl(hw);
-       struct b43_wldev *dev;
-       int err = 0;
-
-       mutex_lock(&wl->mutex);
-       dev = wl->current_dev;
-       if (unlikely(!dev || (b43_status(dev) < B43_STAT_INITIALIZED))) {
-               err = -ENODEV;
-               goto out_unlock;
-       }
-       b43_set_retry_limits(dev, short_retry_limit, long_retry_limit);
-out_unlock:
-       mutex_unlock(&wl->mutex);
 
-       return err;
+       cancel_work_sync(&(wl->txpower_adjust_work));
 }
 
-static int b43_op_beacon_set_tim(struct ieee80211_hw *hw, int aid, int set)
+static int b43_op_beacon_set_tim(struct ieee80211_hw *hw,
+                                struct ieee80211_sta *sta, bool set)
 {
        struct b43_wl *wl = hw_to_b43_wl(hw);
-       struct sk_buff *beacon;
        unsigned long flags;
-       struct ieee80211_tx_control txctl;
 
-       /* We could modify the existing beacon and set the aid bit in
-        * the TIM field, but that would probably require resizing and
-        * moving of data within the beacon template.
-        * Simply request a new beacon and let mac80211 do the hard work. */
-       beacon = ieee80211_beacon_get(hw, wl->vif, &txctl);
-       if (unlikely(!beacon))
-               return -ENOMEM;
        spin_lock_irqsave(&wl->irq_lock, flags);
-       b43_update_templates(wl, beacon, &txctl);
+       b43_update_templates(wl);
        spin_unlock_irqrestore(&wl->irq_lock, flags);
 
        return 0;
 }
 
-static int b43_op_ibss_beacon_update(struct ieee80211_hw *hw,
-                                    struct sk_buff *beacon,
-                                    struct ieee80211_tx_control *ctl)
+static void b43_op_sta_notify(struct ieee80211_hw *hw,
+                             struct ieee80211_vif *vif,
+                             enum sta_notify_cmd notify_cmd,
+                             struct ieee80211_sta *sta)
 {
        struct b43_wl *wl = hw_to_b43_wl(hw);
-       unsigned long flags;
 
-       spin_lock_irqsave(&wl->irq_lock, flags);
-       b43_update_templates(wl, beacon, ctl);
-       spin_unlock_irqrestore(&wl->irq_lock, flags);
+       B43_WARN_ON(!vif || wl->vif != vif);
+}
 
-       return 0;
+static void b43_op_sw_scan_start_notifier(struct ieee80211_hw *hw)
+{
+       struct b43_wl *wl = hw_to_b43_wl(hw);
+       struct b43_wldev *dev;
+
+       mutex_lock(&wl->mutex);
+       dev = wl->current_dev;
+       if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED)) {
+               /* Disable CFP update during scan on other channels. */
+               b43_hf_write(dev, b43_hf_read(dev) | B43_HF_SKCFPUP);
+       }
+       mutex_unlock(&wl->mutex);
 }
 
-static void b43_op_sta_notify(struct ieee80211_hw *hw,
-                             struct ieee80211_vif *vif,
-                             enum sta_notify_cmd notify_cmd,
-                             const u8 *addr)
+static void b43_op_sw_scan_complete_notifier(struct ieee80211_hw *hw)
 {
        struct b43_wl *wl = hw_to_b43_wl(hw);
+       struct b43_wldev *dev;
 
-       B43_WARN_ON(!vif || wl->vif != vif);
+       mutex_lock(&wl->mutex);
+       dev = wl->current_dev;
+       if (dev && (b43_status(dev) >= B43_STAT_INITIALIZED)) {
+               /* Re-enable CFP update. */
+               b43_hf_write(dev, b43_hf_read(dev) & ~B43_HF_SKCFPUP);
+       }
+       mutex_unlock(&wl->mutex);
 }
 
 static const struct ieee80211_ops b43_hw_ops = {
@@ -4165,17 +4422,19 @@ static const struct ieee80211_ops b43_hw_ops = {
        .add_interface          = b43_op_add_interface,
        .remove_interface       = b43_op_remove_interface,
        .config                 = b43_op_config,
-       .config_interface       = b43_op_config_interface,
+       .bss_info_changed       = b43_op_bss_info_changed,
        .configure_filter       = b43_op_configure_filter,
        .set_key                = b43_op_set_key,
        .get_stats              = b43_op_get_stats,
        .get_tx_stats           = b43_op_get_tx_stats,
+       .get_tsf                = b43_op_get_tsf,
+       .set_tsf                = b43_op_set_tsf,
        .start                  = b43_op_start,
        .stop                   = b43_op_stop,
-       .set_retry_limit        = b43_op_set_retry_limit,
        .set_tim                = b43_op_beacon_set_tim,
-       .beacon_update          = b43_op_ibss_beacon_update,
        .sta_notify             = b43_op_sta_notify,
+       .sw_scan_start          = b43_op_sw_scan_start_notifier,
+       .sw_scan_complete       = b43_op_sw_scan_complete_notifier,
 };
 
 /* Hard-reset the chip. Do not call this directly.
@@ -4211,7 +4470,9 @@ static void b43_chip_reset(struct work_struct *work)
                        goto out;
                }
        }
-      out:
+out:
+       if (err)
+               wl->current_dev = NULL; /* Failed to init the dev. */
        mutex_unlock(&wl->mutex);
        if (err)
                b43err(wl, "Controller restart FAILED\n");
@@ -4245,6 +4506,7 @@ static void b43_wireless_core_detach(struct b43_wldev *dev)
        /* We release firmware that late to not be required to re-request
         * is all the time when we reinit the core. */
        b43_release_firmware(dev);
+       b43_phy_free(dev);
 }
 
 static int b43_wireless_core_attach(struct b43_wldev *dev)
@@ -4298,6 +4560,7 @@ static int b43_wireless_core_attach(struct b43_wldev *dev)
                        break;
                case B43_PHYTYPE_G:
                case B43_PHYTYPE_N:
+               case B43_PHYTYPE_LP:
                        have_2ghz_phy = 1;
                        break;
                default:
@@ -4318,30 +4581,35 @@ static int b43_wireless_core_attach(struct b43_wldev *dev)
                }
        }
 
+       err = b43_phy_allocate(dev);
+       if (err)
+               goto err_powerdown;
+
        dev->phy.gmode = have_2ghz_phy;
        tmp = dev->phy.gmode ? B43_TMSLOW_GMODE : 0;
        b43_wireless_core_reset(dev, tmp);
 
        err = b43_validate_chipaccess(dev);
        if (err)
-               goto err_powerdown;
+               goto err_phy_free;
        err = b43_setup_bands(dev, have_2ghz_phy, have_5ghz_phy);
        if (err)
-               goto err_powerdown;
+               goto err_phy_free;
 
        /* Now set some default "current_dev" */
        if (!wl->current_dev)
                wl->current_dev = dev;
        INIT_WORK(&dev->restart_work, b43_chip_reset);
 
-       b43_radio_turn_off(dev, 1);
-       b43_switch_analog(dev, 0);
+       dev->phy.ops->switch_analog(dev, 0);
        ssb_device_disable(dev->dev, 0);
        ssb_bus_may_powerdown(bus);
 
 out:
        return err;
 
+err_phy_free:
+       b43_phy_free(dev);
 err_powerdown:
        ssb_bus_may_powerdown(bus);
        return err;
@@ -4352,9 +4620,11 @@ static void b43_one_core_detach(struct ssb_device *dev)
        struct b43_wldev *wldev;
        struct b43_wl *wl;
 
+       /* Do not cancel ieee80211-workqueue based work here.
+        * See comment in b43_remove(). */
+
        wldev = ssb_get_drvdata(dev);
        wl = wldev->wl;
-       cancel_work_sync(&wldev->restart_work);
        b43_debugfs_remove_device(wldev);
        b43_wireless_core_detach(wldev);
        list_del(&wldev->list);
@@ -4435,8 +4705,12 @@ static void b43_sprom_fixup(struct ssb_bus *bus)
        if (bus->bustype == SSB_BUSTYPE_PCI) {
                pdev = bus->host_pci;
                if (IS_PDEV(pdev, BROADCOM, 0x4318, ASUSTEK, 0x100F) ||
+                   IS_PDEV(pdev, BROADCOM, 0x4320,    DELL, 0x0003) ||
+                   IS_PDEV(pdev, BROADCOM, 0x4320,      HP, 0x12f8) ||
                    IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0015) ||
-                   IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0013))
+                   IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0014) ||
+                   IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0013) ||
+                   IS_PDEV(pdev, BROADCOM, 0x4320, MOTOROLA, 0x7010))
                        bus->sprom.boardflags_lo &= ~B43_BFL_BTCOEXIST;
        }
 }
@@ -4465,12 +4739,19 @@ static int b43_wireless_init(struct ssb_device *dev)
        }
 
        /* fill hw info */
-       hw->flags = IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE |
-                   IEEE80211_HW_RX_INCLUDES_FCS;
-       hw->max_signal = 100;
-       hw->max_rssi = -110;
-       hw->max_noise = -110;
+       hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
+                   IEEE80211_HW_SIGNAL_DBM |
+                   IEEE80211_HW_NOISE_DBM;
+
+       hw->wiphy->interface_modes =
+               BIT(NL80211_IFTYPE_AP) |
+               BIT(NL80211_IFTYPE_MESH_POINT) |
+               BIT(NL80211_IFTYPE_STATION) |
+               BIT(NL80211_IFTYPE_WDS) |
+               BIT(NL80211_IFTYPE_ADHOC);
+
        hw->queues = b43_modparam_qos ? 4 : 1;
+       hw->max_rates = 2;
        SET_IEEE80211_DEV(hw, dev->dev);
        if (is_valid_ether_addr(sprom->et1mac))
                SET_IEEE80211_PERM_ADDR(hw, sprom->et1mac);
@@ -4487,13 +4768,14 @@ static int b43_wireless_init(struct ssb_device *dev)
        spin_lock_init(&wl->shm_lock);
        mutex_init(&wl->mutex);
        INIT_LIST_HEAD(&wl->devlist);
-       INIT_WORK(&wl->qos_update_work, b43_qos_update_work);
        INIT_WORK(&wl->beacon_update_trigger, b43_beacon_update_trigger_work);
+       INIT_WORK(&wl->txpower_adjust_work, b43_phy_txpower_adjust_work);
 
        ssb_set_devtypedata(dev, wl);
-       b43info(wl, "Broadcom %04X WLAN found\n", dev->bus->chip_id);
+       b43info(wl, "Broadcom %04X WLAN found (core revision %u)\n",
+               dev->bus->chip_id, dev->id.revision);
        err = 0;
-      out:
+out:
        return err;
 }
 
@@ -4539,6 +4821,10 @@ static void b43_remove(struct ssb_device *dev)
        struct b43_wl *wl = ssb_get_devtypedata(dev);
        struct b43_wldev *wldev = ssb_get_drvdata(dev);
 
+       /* We must cancel any work here before unregistering from ieee80211,
+        * as the ieee80211 unreg will destroy the workqueue. */
+       cancel_work_sync(&wldev->restart_work);
+
        B43_WARN_ON(!wl);
        if (wl->current_dev == wldev)
                ieee80211_unregister_hw(wl->hw);