net: convert print_mac to %pM
[safe/jmp/linux-2.6] / drivers / net / wireless / b43 / main.c
index 6c58f8b..6e77301 100644 (file)
@@ -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>
 #include "b43.h"
 #include "main.h"
 #include "debugfs.h"
-#include "phy.h"
+#include "phy_common.h"
+#include "phy_g.h"
+#include "phy_n.h"
 #include "dma.h"
+#include "pio.h"
 #include "sysfs.h"
 #include "xmit.h"
 #include "lo.h"
@@ -82,6 +84,10 @@ int b43_modparam_qos = 1;
 module_param_named(qos, b43_modparam_qos, int, 0444);
 MODULE_PARM_DESC(qos, "Enable QOS support (default on)");
 
+static int modparam_btcoex = 1;
+module_param_named(btcoex, modparam_btcoex, int, 0444);
+MODULE_PARM_DESC(btcoex, "Enable Bluetooth coexistance (default on)");
+
 
 static const struct ssb_device_id b43_ssb_tbl[] = {
        SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, 5),
@@ -367,13 +373,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) {
@@ -391,18 +394,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) {
@@ -417,17 +428,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) {
@@ -437,35 +455,47 @@ 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);
 }
 
@@ -723,6 +753,7 @@ static void b43_synchronize_irq(struct b43_wldev *dev)
  */
 void b43_dummy_transmission(struct b43_wldev *dev)
 {
+       struct b43_wl *wl = dev->wl;
        struct b43_phy *phy = &dev->phy;
        unsigned int i, max_loop;
        u16 value;
@@ -749,6 +780,9 @@ void b43_dummy_transmission(struct b43_wldev *dev)
                return;
        }
 
+       spin_lock_irq(&wl->irq_lock);
+       write_lock(&wl->tx_lock);
+
        for (i = 0; i < 5; i++)
                b43_ram_write(dev, i * 4, buffer[i]);
 
@@ -781,7 +815,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;
@@ -789,6 +823,9 @@ void b43_dummy_transmission(struct b43_wldev *dev)
        }
        if (phy->radio_ver == 0x2050 && phy->radio_rev <= 0x5)
                b43_radio_write16(dev, 0x0051, 0x0037);
+
+       write_unlock(&wl->tx_lock);
+       spin_unlock_irq(&wl->irq_lock);
 }
 
 static void key_write(struct b43_wldev *dev,
@@ -1015,12 +1052,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)
-{
-       b43_write16(dev, B43_MMIO_PHY0, on ? 0 : 0xF4);
-}
-
 void b43_wireless_core_reset(struct b43_wldev *dev, u32 flags)
 {
        u32 tmslow;
@@ -1043,8 +1074,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;
@@ -1121,16 +1156,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;
 
@@ -1139,7 +1174,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;
@@ -1147,9 +1182,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)
@@ -1158,10 +1203,10 @@ static void handle_irq_noise(struct b43_wldev *dev)
        /* Get the noise samples. */
        B43_WARN_ON(dev->noisecalc.nr_samples >= 8);
        i = dev->noisecalc.nr_samples;
-       noise[0] = limit_value(noise[0], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
-       noise[1] = limit_value(noise[1], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
-       noise[2] = limit_value(noise[2], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
-       noise[3] = limit_value(noise[3], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
+       noise[0] = clamp_val(noise[0], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
+       noise[1] = clamp_val(noise[1], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
+       noise[2] = clamp_val(noise[2], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
+       noise[3] = clamp_val(noise[3], 0, ARRAY_SIZE(phy->nrssi_lt) - 1);
        dev->noisecalc.samples[i][0] = phy->nrssi_lt[noise[0]];
        dev->noisecalc.samples[i][1] = phy->nrssi_lt[noise[1]];
        dev->noisecalc.samples[i][2] = phy->nrssi_lt[noise[2]];
@@ -1190,23 +1235,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;
 }
 
@@ -1268,22 +1312,107 @@ static void b43_write_template_common(struct b43_wldev *dev,
                        size + sizeof(struct b43_plcp_hdr6));
 }
 
+/* Check if the use of the antenna that ieee80211 told us to
+ * use is possible. This will fall back to DEFAULT.
+ * "antenna_nr" is the antenna identifier we got from ieee80211. */
+u8 b43_ieee80211_antenna_sanitize(struct b43_wldev *dev,
+                                 u8 antenna_nr)
+{
+       u8 antenna_mask;
+
+       if (antenna_nr == 0) {
+               /* Zero means "use default antenna". That's always OK. */
+               return 0;
+       }
+
+       /* Get the mask of available antennas. */
+       if (dev->phy.gmode)
+               antenna_mask = dev->dev->bus->sprom.ant_available_bg;
+       else
+               antenna_mask = dev->dev->bus->sprom.ant_available_a;
+
+       if (!(antenna_mask & (1 << (antenna_nr - 1)))) {
+               /* This antenna is not available. Fall back to default. */
+               return 0;
+       }
+
+       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)
+{
+       switch (antenna) {
+       case B43_ANTENNA0:
+               return B43_TXH_PHY_ANT0;
+       case B43_ANTENNA1:
+               return B43_TXH_PHY_ANT1;
+       case B43_ANTENNA2:
+               return B43_TXH_PHY_ANT2;
+       case B43_ANTENNA3:
+               return B43_TXH_PHY_ANT3;
+       case B43_ANTENNA_AUTO:
+               return B43_TXH_PHY_ANT01AUTO;
+       }
+       B43_WARN_ON(1);
+       return 0;
+}
+
 static void b43_write_beacon_template(struct b43_wldev *dev,
                                      u16 ram_offset,
-                                     u16 shm_size_offset, u8 rate)
+                                     u16 shm_size_offset)
 {
        unsigned int i, len, variable_len;
        const struct ieee80211_mgmt *bcn;
        const u8 *ie;
        bool tim_found = 0;
+       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 = 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, info->antenna_sel_tx);
+       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. */
+       ctl &= ~B43_TXH_PHY_SHORTPRMBL;
+       ctl &= ~B43_TXH_PHY_ANT;
+       ctl &= ~B43_TXH_PHY_ENC;
+       ctl |= antenna;
+       if (b43_is_cck_rate(rate))
+               ctl |= B43_TXH_PHY_ENC_CCK;
+       else
+               ctl |= B43_TXH_PHY_ENC_OFDM;
+       b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, ctl);
+
        /* Find the position of the TIM and the DTIM_period value
         * and write them to SHM. */
        ie = bcn->u.beacon.variable;
@@ -1321,10 +1450,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");
+               /*
+                * 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,
@@ -1434,21 +1570,127 @@ 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, 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;
+
+       cmd = b43_read32(dev, B43_MMIO_MACCMD);
+       beacon0_valid = (cmd & B43_MACCMD_BEACON0_VALID);
+       beacon1_valid = (cmd & B43_MACCMD_BEACON1_VALID);
+
+       /* 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;
+               return;
+       }
+
+       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 (!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);
+               }
+       }
+}
+
+static void b43_beacon_update_trigger_work(struct work_struct *work)
+{
+       struct b43_wl *wl = container_of(work, struct b43_wl,
+                                        beacon_update_trigger);
+       struct b43_wldev *dev;
+
+       mutex_lock(&wl->mutex);
+       dev = wl->current_dev;
+       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);
+               mmiowb();
+               spin_unlock_irq(&wl->irq_lock);
+       }
+       mutex_unlock(&wl->mutex);
+}
+
 /* 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)
+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;
        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)
@@ -1474,49 +1716,110 @@ static void b43_set_beacon_int(struct b43_wldev *dev, u16 beacon_int)
 {
        b43_time_lock(dev);
        if (dev->dev->id.revision >= 3) {
-               b43_write32(dev, 0x188, (beacon_int << 16));
+               b43_write32(dev, B43_MMIO_TSF_CFP_REP, (beacon_int << 16));
+               b43_write32(dev, B43_MMIO_TSF_CFP_START, (beacon_int << 10));
        } else {
                b43_write16(dev, 0x606, (beacon_int >> 6));
                b43_write16(dev, 0x610, beacon_int);
        }
        b43_time_unlock(dev);
+       b43dbg(dev->wl, "Set beacon interval to %u\n", beacon_int);
 }
 
-static void handle_irq_beacon(struct b43_wldev *dev)
+static void b43_handle_firmware_panic(struct b43_wldev *dev)
 {
-       struct b43_wl *wl = dev->wl;
-       u32 cmd;
+       u16 reason;
 
-       if (!b43_is_mode(wl, IEEE80211_IF_TYPE_AP))
-               return;
-
-       /* This is the bottom half of the asynchronous beacon update. */
+       /* 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);
 
-       cmd = b43_read32(dev, B43_MMIO_MACCMD);
-       if (!(cmd & B43_MACCMD_BEACON0_VALID)) {
-               if (!wl->beacon0_uploaded) {
-                       b43_write_beacon_template(dev, 0x68, 0x18,
-                                                 B43_CCK_RATE_1MB);
-                       b43_write_probe_resp_template(dev, 0x268, 0x4A,
-                                                     &__b43_ratetable[3]);
-                       wl->beacon0_uploaded = 1;
-               }
-               cmd |= B43_MACCMD_BEACON0_VALID;
-       }
-       if (!(cmd & B43_MACCMD_BEACON1_VALID)) {
-               if (!wl->beacon1_uploaded) {
-                       b43_write_beacon_template(dev, 0x468, 0x1A,
-                                                 B43_CCK_RATE_1MB);
-                       wl->beacon1_uploaded = 1;
-               }
-               cmd |= B43_MACCMD_BEACON1_VALID;
+       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;
        }
-       b43_write32(dev, B43_MMIO_MACCMD, cmd);
 }
 
 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 */
@@ -1593,8 +1896,12 @@ static void b43_interrupt_tasklet(struct b43_wldev *dev)
                handle_irq_noise(dev);
 
        /* Check the DMA reason registers for received data. */
-       if (dma_reason[0] & B43_DMAIRQ_RX_DONE)
-               b43_dma_rx(dev->dma.rx_ring);
+       if (dma_reason[0] & B43_DMAIRQ_RX_DONE) {
+               if (b43_using_pio_transfers(dev))
+                       b43_pio_rx(dev->pio.rx_queue);
+               else
+                       b43_dma_rx(dev->dma.rx_ring);
+       }
        B43_WARN_ON(dma_reason[1] & B43_DMAIRQ_RX_DONE);
        B43_WARN_ON(dma_reason[2] & B43_DMAIRQ_RX_DONE);
        B43_WARN_ON(dma_reason[3] & B43_DMAIRQ_RX_DONE);
@@ -1699,7 +2006,8 @@ static void b43_print_fw_helptext(struct b43_wl *wl, bool error)
 
 static int do_request_fw(struct b43_wldev *dev,
                         const char *name,
-                        struct b43_firmware_file *fw)
+                        struct b43_firmware_file *fw,
+                        bool silent)
 {
        char path[sizeof(modparam_fwpostfix) + 32];
        const struct firmware *blob;
@@ -1723,9 +2031,15 @@ static int do_request_fw(struct b43_wldev *dev,
                 "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);
+       if (err == -ENOENT) {
+               if (!silent) {
+                       b43err(dev->wl, "Firmware file \"%s\" not found\n",
+                              path);
+               }
+               return err;
+       } else if (err) {
+               b43err(dev->wl, "Firmware file \"%s\" request failed (err=%d)\n",
+                      path, err);
                return err;
        }
        if (blob->size < sizeof(struct b43_fw_header))
@@ -1776,7 +2090,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 = do_request_fw(dev, filename, &fw->ucode, 0);
        if (err)
                goto err_load;
 
@@ -1787,8 +2101,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 = do_request_fw(dev, filename, &fw->pcm, 1);
+       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 */
@@ -1806,7 +2125,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;
@@ -1819,7 +2138,7 @@ static int b43_request_firmware(struct b43_wldev *dev)
        default:
                goto err_no_initvals;
        }
-       err = do_request_fw(dev, filename, &fw->initvals);
+       err = do_request_fw(dev, filename, &fw->initvals, 0);
        if (err)
                goto err_load;
 
@@ -1853,7 +2172,7 @@ static int b43_request_firmware(struct b43_wldev *dev)
        default:
                goto err_no_initvals;
        }
-       err = do_request_fw(dev, filename, &fw->initvals_band);
+       err = do_request_fw(dev, filename, &fw->initvals_band, 0);
        if (err)
                goto err_load;
 
@@ -1970,14 +2289,28 @@ 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)) {
                b43warn(dev->wl, "You are using an old firmware image. "
@@ -2024,7 +2357,7 @@ static int b43_write_initvals(struct b43_wldev *dev,
                                goto err_format;
                        array_size -= sizeof(iv->data.d32);
 
-                       value = be32_to_cpu(get_unaligned(&iv->data.d32));
+                       value = get_unaligned_be32(&iv->data.d32);
                        b43_write32(dev, offset, value);
 
                        iv = (const struct b43_iv *)((const uint8_t *)iv +
@@ -2154,11 +2487,23 @@ static void b43_gpio_cleanup(struct b43_wldev *dev)
 }
 
 /* http://bcm-specs.sipsolutions.net/EnableMac */
-static void b43_mac_enable(struct b43_wldev *dev)
-{
+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);
-       B43_WARN_ON(irqs_disabled());
        if (dev->mac_suspended == 0) {
                b43_write32(dev, B43_MMIO_MACCTL,
                            b43_read32(dev, B43_MMIO_MACCTL)
@@ -2169,39 +2514,32 @@ static void b43_mac_enable(struct b43_wldev *dev)
                b43_read32(dev, B43_MMIO_MACCTL);
                b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
                b43_power_saving_ctl_bits(dev, 0);
-
-               /* Re-enable IRQs. */
-               spin_lock_irq(&dev->wl->irq_lock);
-               b43_interrupt_enable(dev, dev->irq_savedstate);
-               spin_unlock_irq(&dev->wl->irq_lock);
        }
 }
 
 /* http://bcm-specs.sipsolutions.net/SuspendMAC */
-static void b43_mac_suspend(struct b43_wldev *dev)
+void b43_mac_suspend(struct b43_wldev *dev)
 {
        int i;
        u32 tmp;
 
        might_sleep();
-       B43_WARN_ON(irqs_disabled());
        B43_WARN_ON(dev->mac_suspended < 0);
 
        if (dev->mac_suspended == 0) {
-               /* Mask IRQs before suspending MAC. Otherwise
-                * the MAC stays busy and won't suspend. */
-               spin_lock_irq(&dev->wl->irq_lock);
-               tmp = b43_interrupt_disable(dev, B43_IRQ_ALL);
-               spin_unlock_irq(&dev->wl->irq_lock);
-               b43_synchronize_irq(dev);
-               dev->irq_savedstate = tmp;
-
                b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
                b43_write32(dev, B43_MMIO_MACCTL,
                            b43_read32(dev, B43_MMIO_MACCTL)
                            & ~B43_MACCTL_ENABLED);
                /* force pci to flush the write */
                b43_read32(dev, B43_MMIO_MACCTL);
+               for (i = 35; i; i--) {
+                       tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
+                       if (tmp & B43_IRQ_MAC_SUSPENDED)
+                               goto out;
+                       udelay(10);
+               }
+               /* Hm, it seems this will take some time. Use msleep(). */
                for (i = 40; i; i--) {
                        tmp = b43_read32(dev, B43_MMIO_GEN_IRQ_REASON);
                        if (tmp & B43_IRQ_MAC_SUSPENDED)
@@ -2230,9 +2568,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)
@@ -2307,38 +2646,28 @@ static void b43_rate_memory_init(struct b43_wldev *dev)
        }
 }
 
+/* Set the default values for the PHY TX Control Words. */
+static void b43_set_phytxctl_defaults(struct b43_wldev *dev)
+{
+       u16 ctl = 0;
+
+       ctl |= B43_TXH_PHY_ENC_CCK;
+       ctl |= B43_TXH_PHY_ANT01AUTO;
+       ctl |= B43_TXH_PHY_TXPWR;
+
+       b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, ctl);
+       b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL, ctl);
+       b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRPHYCTL, ctl);
+}
+
 /* Set the TX-Antenna for management frames sent by firmware. */
 static void b43_mgmtframe_txantenna(struct b43_wldev *dev, int antenna)
 {
-       u16 ant = 0;
+       u16 ant;
        u16 tmp;
 
-       switch (antenna) {
-       case B43_ANTENNA0:
-               ant |= B43_TXH_PHY_ANT0;
-               break;
-       case B43_ANTENNA1:
-               ant |= B43_TXH_PHY_ANT1;
-               break;
-       case B43_ANTENNA2:
-               ant |= B43_TXH_PHY_ANT2;
-               break;
-       case B43_ANTENNA3:
-               ant |= B43_TXH_PHY_ANT3;
-               break;
-       case B43_ANTENNA_AUTO:
-               ant |= B43_TXH_PHY_ANT01AUTO;
-               break;
-       default:
-               B43_WARN_ON(1);
-       }
+       ant = b43_antenna_to_phyctl(antenna);
 
-       /* FIXME We also need to set the other flags of the PHY control field somewhere. */
-
-       /* For Beacons */
-       tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL);
-       tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
-       b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_BEACPHYCTL, tmp);
        /* For ACK/CTS */
        tmp = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_ACKCTSPHYCTL);
        tmp = (tmp & ~B43_TXH_PHY_ANT) | ant;
@@ -2352,7 +2681,7 @@ 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);
        /* firmware is released later */
 }
@@ -2363,7 +2692,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;
 
@@ -2388,19 +2717,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) {
@@ -2453,50 +2783,20 @@ 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;
 }
 
-static void b43_periodic_every120sec(struct b43_wldev *dev)
-{
-       struct b43_phy *phy = &dev->phy;
-
-       if (phy->type != B43_PHYTYPE_G || phy->rev < 2)
-               return;
-
-       b43_mac_suspend(dev);
-       b43_lo_g_measure(dev);
-       b43_mac_enable(dev);
-       if (b43_has_hardware_pctl(phy))
-               b43_lo_g_ctl_mark_all_unused(dev);
-}
-
 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 (!b43_has_hardware_pctl(phy))
-               b43_lo_g_ctl_mark_all_unused(dev);
-       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)
@@ -2508,32 +2808,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?
-       //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();
@@ -2544,8 +2836,6 @@ static void do_periodic_work(struct b43_wldev *dev)
        unsigned int state;
 
        state = dev->periodic_state;
-       if (state % 8 == 0)
-               b43_periodic_every120sec(dev);
        if (state % 4 == 0)
                b43_periodic_every60sec(dev);
        if (state % 2 == 0)
@@ -2666,10 +2956,10 @@ static int b43_rng_read(struct hwrng *rng, u32 * data)
        return (sizeof(u16));
 }
 
-static void b43_rng_exit(struct b43_wl *wl, bool suspended)
+static void b43_rng_exit(struct b43_wl *wl)
 {
        if (wl->rng_initialized)
-               __hwrng_unregister(&wl->rng, suspended);
+               hwrng_unregister(&wl->rng);
 }
 
 static int b43_rng_init(struct b43_wl *wl)
@@ -2693,22 +2983,41 @@ static int b43_rng_init(struct b43_wl *wl)
 }
 
 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;
-       int err = -ENODEV;
+       unsigned long flags;
+       int err;
 
+       if (unlikely(skb->len < 2 + 2 + 6)) {
+               /* Too short, this can't be a valid frame. */
+               goto drop_packet;
+       }
+       B43_WARN_ON(skb_shinfo(skb)->nr_frags);
        if (unlikely(!dev))
-               goto out;
-       if (unlikely(b43_status(dev) < B43_STAT_STARTED))
-               goto out;
-       /* DMA-TX is done without a global lock. */
-       err = b43_dma_tx(dev, skb, ctl);
-out:
+               goto drop_packet;
+
+       /* Transmissions on seperate queues can run concurrently. */
+       read_lock_irqsave(&wl->tx_lock, flags);
+
+       err = -ENODEV;
+       if (likely(b43_status(dev) >= B43_STAT_STARTED)) {
+               if (b43_using_pio_transfers(dev))
+                       err = b43_pio_tx(dev, skb);
+               else
+                       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;
 }
 
@@ -2718,53 +3027,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) {
@@ -2783,36 +3059,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);
 }
 
@@ -2821,25 +3092,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);
@@ -2848,26 +3144,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,
-                         int _queue,
+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
@@ -2875,16 +3158,25 @@ static int b43_op_conf_tx(struct ieee80211_hw *hw,
                 * 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;
 
-       queue_work(hw->workqueue, &wl->qos_update_work);
+       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;
 
-       return 0;
+out_unlock:
+       mutex_unlock(&wl->mutex);
+
+       return err;
 }
 
 static int b43_op_get_tx_stats(struct ieee80211_hw *hw,
@@ -2899,7 +3191,10 @@ static int b43_op_get_tx_stats(struct ieee80211_hw *hw,
                goto out;
        spin_lock_irqsave(&wl->irq_lock, flags);
        if (likely(b43_status(dev) >= B43_STAT_STARTED)) {
-               b43_dma_get_tx_stats(dev, stats);
+               if (b43_using_pio_transfers(dev))
+                       b43_pio_get_tx_stats(dev, stats);
+               else
+                       b43_dma_get_tx_stats(dev, stats);
                err = 0;
        }
        spin_unlock_irqrestore(&wl->irq_lock, flags);
@@ -3044,52 +3339,6 @@ init_failure:
        return err;
 }
 
-/* Check if the use of the antenna that ieee80211 told us to
- * use is possible. This will fall back to DEFAULT.
- * "antenna_nr" is the antenna identifier we got from ieee80211. */
-u8 b43_ieee80211_antenna_sanitize(struct b43_wldev *dev,
-                                 u8 antenna_nr)
-{
-       u8 antenna_mask;
-
-       if (antenna_nr == 0) {
-               /* Zero means "use default antenna". That's always OK. */
-               return 0;
-       }
-
-       /* Get the mask of available antennas. */
-       if (dev->phy.gmode)
-               antenna_mask = dev->dev->bus->sprom.ant_available_bg;
-       else
-               antenna_mask = dev->dev->bus->sprom.ant_available_a;
-
-       if (!(antenna_mask & (1 << (antenna_nr - 1)))) {
-               /* This antenna is not available. Fall back to default. */
-               return 0;
-       }
-
-       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;
-       }
-}
-
 static int b43_op_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
 {
        struct b43_wl *wl = hw_to_b43_wl(hw);
@@ -3124,7 +3373,7 @@ static int b43_op_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
        /* 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);
+               b43_switch_channel(dev, conf->channel->hw_value);
 
        /* Enable/Disable ShortSlot timing. */
        if ((!!(conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME)) !=
@@ -3140,25 +3389,30 @@ static int b43_op_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
 
        /* 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);
        b43_mgmtframe_txantenna(dev, antenna);
        antenna = b43_antenna_from_ieee80211(dev, conf->antenna_sel_rx);
-       b43_set_rx_antenna(dev, antenna);
+       if (phy->ops->set_rx_antenna)
+               phy->ops->set_rx_antenna(dev, antenna);
 
-       /* Update templates for AP mode. */
-       if (b43_is_mode(wl, IEEE80211_IF_TYPE_AP))
+       /* Update templates for AP/mesh mode. */
+       if (b43_is_mode(wl, NL80211_IFTYPE_AP) ||
+           b43_is_mode(wl, NL80211_IFTYPE_MESH_POINT))
                b43_set_beacon_int(dev, conf->beacon_int);
 
        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 "
@@ -3166,7 +3420,7 @@ 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");
                }
        }
@@ -3191,7 +3445,6 @@ static int b43_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
        u8 algorithm;
        u8 index;
        int err;
-       DECLARE_MAC_BUF(mac);
 
        if (modparam_nohwcrypt)
                return -ENOSPC; /* User disabled HW-crypto */
@@ -3204,6 +3457,13 @@ static int b43_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
        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:
@@ -3272,9 +3532,9 @@ out_unlock:
        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));
+                      addr);
        }
        return err;
 }
@@ -3334,11 +3594,16 @@ static int b43_op_config_interface(struct ieee80211_hw *hw,
        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);
+               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 (conf->changed & IEEE80211_IFCC_SSID)
+                               b43_set_ssid(dev, conf->ssid, conf->ssid_len);
+                       if (conf->changed & IEEE80211_IFCC_BEACON)
+                               b43_update_templates(wl);
+               } else if (b43_is_mode(wl, NL80211_IFTYPE_ADHOC)) {
+                       if (conf->changed & IEEE80211_IFCC_BEACON)
+                               b43_update_templates(wl);
                }
                b43_write_mac_bssid_templates(dev);
        }
@@ -3366,16 +3631,17 @@ static void b43_wireless_core_stop(struct b43_wldev *dev)
        spin_unlock_irqrestore(&wl->irq_lock, flags);
        b43_synchronize_irq(dev);
 
+       write_lock_irqsave(&wl->tx_lock, flags);
        b43_set_status(dev, B43_STAT_INITIALIZED);
+       write_unlock_irqrestore(&wl->tx_lock, flags);
 
+       b43_pio_stop(dev);
        mutex_unlock(&wl->mutex);
        /* Must unlock as it would otherwise deadlock. No races here.
         * Cancel the possibly running self-rearming periodic work. */
        cancel_delayed_work_sync(&dev->periodic_work);
        mutex_lock(&wl->mutex);
 
-       ieee80211_stop_queues(wl->hw);  //FIXME this could cause a deadlock, as mac80211 seems buggy.
-
        b43_mac_suspend(dev);
        free_irq(dev->dev->irq, dev);
        b43dbg(wl, "Wireless interface stopped\n");
@@ -3402,7 +3668,6 @@ 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);
 
        /* Start maintainance work */
        b43_periodic_tasks_setup(dev);
@@ -3528,48 +3793,10 @@ 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->rebuild = 1;
-               lo->tx_bias = 0xFF;
-       }
-       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;
 }
 
 static void setup_struct_wldev_for_init(struct b43_wldev *dev)
@@ -3599,8 +3826,10 @@ static void setup_struct_wldev_for_init(struct b43_wldev *dev)
 static void b43_bluetooth_coext_enable(struct b43_wldev *dev)
 {
        struct ssb_sprom *sprom = &dev->dev->bus->sprom;
-       u32 hf;
+       u64 hf;
 
+       if (!modparam_btcoex)
+               return;
        if (!(sprom->boardflags_lo & B43_BFL_BTCOEXIST))
                return;
        if (dev->phy.type != B43_PHYTYPE_B && !dev->phy.gmode)
@@ -3612,11 +3841,13 @@ static void b43_bluetooth_coext_enable(struct b43_wldev *dev)
        else
                hf |= B43_HF_BTCOEX;
        b43_hf_write(dev, hf);
-       //TODO
 }
 
 static void b43_bluetooth_coext_disable(struct b43_wldev *dev)
-{                              //TODO
+{
+       if (!modparam_btcoex)
+               return;
+       //TODO
 }
 
 static void b43_imcfglo_timeouts_workaround(struct b43_wldev *dev)
@@ -3662,11 +3893,45 @@ static void b43_set_retry_limits(struct b43_wldev *dev,
                        long_retry);
 }
 
+static void b43_set_synth_pu_delay(struct b43_wldev *dev, bool idle)
+{
+       u16 pu_delay;
+
+       /* The time value is in microseconds. */
+       if (dev->phy.type == B43_PHYTYPE_A)
+               pu_delay = 3700;
+       else
+               pu_delay = 1050;
+       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);
+
+       b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_SPUWKUP, pu_delay);
+}
+
+/* Set the TSF CFP pre-TargetBeaconTransmissionTime. */
+static void b43_set_pretbtt(struct b43_wldev *dev)
+{
+       u16 pretbtt;
+
+       /* The time value is in microseconds. */
+       if (b43_is_mode(dev->wl, NL80211_IFTYPE_ADHOC)) {
+               pretbtt = 2;
+       } else {
+               if (dev->phy.type == B43_PHYTYPE_A)
+                       pretbtt = 120;
+               else
+                       pretbtt = 250;
+       }
+       b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRETBTT, pretbtt);
+       b43_write16(dev, B43_MMIO_TSF_CFP_PRETBTT, pretbtt);
+}
+
 /* Shutdown a wireless core */
 /* 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);
@@ -3682,16 +3947,12 @@ static void b43_wireless_core_exit(struct b43_wldev *dev)
 
        if (!dev->suspend_in_progress) {
                b43_leds_exit(dev);
-               b43_rng_exit(dev->wl, false);
+               b43_rng_exit(dev->wl);
        }
        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;
@@ -3709,7 +3970,8 @@ static int b43_wireless_core_init(struct b43_wldev *dev)
        struct ssb_sprom *sprom = &bus->sprom;
        struct b43_phy *phy = &dev->phy;
        int err;
-       u32 hf, tmp;
+       u64 hf;
+       u32 tmp;
 
        B43_WARN_ON(b43_status(dev) != B43_STAT_UNINIT);
 
@@ -3721,29 +3983,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);
@@ -3772,6 +4028,7 @@ static int b43_wireless_core_init(struct b43_wldev *dev)
        b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_PRMAXTIME, 1);
 
        b43_rate_memory_init(dev);
+       b43_set_phytxctl_defaults(dev);
 
        /* Minimum Contention Window */
        if (phy->type == B43_PHYTYPE_B) {
@@ -3782,18 +4039,17 @@ static int b43_wireless_core_init(struct b43_wldev *dev)
        /* Maximum Contention Window */
        b43_shm_write16(dev, B43_SHM_SCRATCH, B43_SHM_SC_MAXCONT, 0x3FF);
 
-       err = b43_dma_init(dev);
+       if ((dev->dev->bus->bustype == SSB_BUSTYPE_PCMCIA) || B43_FORCE_PIO) {
+               dev->__using_pio_transfers = 1;
+               err = b43_pio_init(dev);
+       } else {
+               dev->__using_pio_transfers = 0;
+               err = b43_dma_init(dev);
+       }
        if (err)
                goto err_chip_exit;
        b43_qos_init(dev);
-
-//FIXME
-#if 1
-       b43_write16(dev, 0x0612, 0x0050);
-       b43_shm_write16(dev, B43_SHM_SHARED, 0x0416, 0x0050);
-       b43_shm_write16(dev, B43_SHM_SHARED, 0x0414, 0x01F4);
-#endif
-
+       b43_set_synth_pu_delay(dev, 1);
        b43_bluetooth_coext_enable(dev);
 
        ssb_bus_powerup(bus, 1);        /* Enable dynamic PCTL */
@@ -3809,15 +4065,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;
@@ -3833,10 +4083,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);
@@ -3853,6 +4104,8 @@ static int b43_op_add_interface(struct ieee80211_hw *hw,
 
        spin_lock_irqsave(&wl->irq_lock, flags);
        b43_adjust_opmode(dev);
+       b43_set_pretbtt(dev);
+       b43_set_synth_pu_delay(dev, 0);
        b43_upload_card_macaddress(dev);
        spin_unlock_irqrestore(&wl->irq_lock, flags);
 
@@ -3905,6 +4158,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. */
@@ -3946,13 +4202,15 @@ 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);
        if (b43_status(dev) >= B43_STAT_STARTED)
                b43_wireless_core_stop(dev);
        b43_wireless_core_exit(dev);
        mutex_unlock(&wl->mutex);
+
+       cancel_work_sync(&(wl->txpower_adjust_work));
 }
 
 static int b43_op_set_retry_limit(struct ieee80211_hw *hw,
@@ -3975,35 +4233,14 @@ out_unlock:
        return err;
 }
 
-static int b43_op_beacon_set_tim(struct ieee80211_hw *hw, int aid, int set)
-{
-       struct b43_wl *wl = hw_to_b43_wl(hw);
-       struct sk_buff *beacon;
-       unsigned long flags;
-
-       /* 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, NULL);
-       if (unlikely(!beacon))
-               return -ENOMEM;
-       spin_lock_irqsave(&wl->irq_lock, flags);
-       b43_update_templates(wl, beacon);
-       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 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);
        unsigned long flags;
 
        spin_lock_irqsave(&wl->irq_lock, flags);
-       b43_update_templates(wl, beacon);
+       b43_update_templates(wl);
        spin_unlock_irqrestore(&wl->irq_lock, flags);
 
        return 0;
@@ -4012,7 +4249,7 @@ static int b43_op_ibss_beacon_update(struct ieee80211_hw *hw,
 static void b43_op_sta_notify(struct ieee80211_hw *hw,
                              struct ieee80211_vif *vif,
                              enum sta_notify_cmd notify_cmd,
-                             const u8 *addr)
+                             struct ieee80211_sta *sta)
 {
        struct b43_wl *wl = hw_to_b43_wl(hw);
 
@@ -4034,7 +4271,6 @@ static const struct ieee80211_ops b43_hw_ops = {
        .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,
 };
 
@@ -4071,7 +4307,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");
@@ -4105,6 +4343,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)
@@ -4170,30 +4409,43 @@ static int b43_wireless_core_attach(struct b43_wldev *dev)
                err = -EOPNOTSUPP;
                goto err_powerdown;
        }
+       if (1 /* disable A-PHY */) {
+               /* FIXME: For now we disable the A-PHY on multi-PHY devices. */
+               if (dev->phy.type != B43_PHYTYPE_N) {
+                       have_2ghz_phy = 1;
+                       have_5ghz_phy = 0;
+               }
+       }
+
+       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;
@@ -4204,9 +4456,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);
@@ -4267,8 +4521,16 @@ static int b43_one_core_attach(struct ssb_device *dev, struct b43_wl *wl)
        return err;
 }
 
+#define IS_PDEV(pdev, _vendor, _device, _subvendor, _subdevice)                ( \
+       (pdev->vendor == PCI_VENDOR_ID_##_vendor) &&                    \
+       (pdev->device == _device) &&                                    \
+       (pdev->subsystem_vendor == PCI_VENDOR_ID_##_subvendor) &&       \
+       (pdev->subsystem_device == _subdevice)                          )
+
 static void b43_sprom_fixup(struct ssb_bus *bus)
 {
+       struct pci_dev *pdev;
+
        /* boardflags workarounds */
        if (bus->boardinfo.vendor == SSB_BOARDVENDOR_DELL &&
            bus->chip_id == 0x4301 && bus->boardinfo.rev == 0x74)
@@ -4276,6 +4538,17 @@ static void b43_sprom_fixup(struct ssb_bus *bus)
        if (bus->boardinfo.vendor == PCI_VENDOR_ID_APPLE &&
            bus->boardinfo.type == 0x4E && bus->boardinfo.rev > 0x40)
                bus->sprom.boardflags_lo |= B43_BFL_PACTRL;
+       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, 0x0014) ||
+                   IS_PDEV(pdev, BROADCOM, 0x4320, LINKSYS, 0x0013) ||
+                   IS_PDEV(pdev, BROADCOM, 0x4320, MOTOROLA, 0x7010))
+                       bus->sprom.boardflags_lo &= ~B43_BFL_BTCOEXIST;
+       }
 }
 
 static void b43_wireless_exit(struct ssb_device *dev, struct b43_wl *wl)
@@ -4302,12 +4575,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_altrates = 1;
        SET_IEEE80211_DEV(hw, dev->dev);
        if (is_valid_ether_addr(sprom->et1mac))
                SET_IEEE80211_PERM_ADDR(hw, sprom->et1mac);
@@ -4319,11 +4599,13 @@ static int b43_wireless_init(struct ssb_device *dev)
        memset(wl, 0, sizeof(*wl));
        wl->hw = hw;
        spin_lock_init(&wl->irq_lock);
+       rwlock_init(&wl->tx_lock);
        spin_lock_init(&wl->leds_lock);
        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);
@@ -4374,6 +4656,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);
@@ -4441,7 +4727,7 @@ static int b43_resume(struct ssb_device *dev)
                err = b43_wireless_core_start(wldev);
                if (err) {
                        b43_leds_exit(wldev);
-                       b43_rng_exit(wldev->wl, true);
+                       b43_rng_exit(wldev->wl);
                        b43_wireless_core_exit(wldev);
                        b43err(wl, "Resume failed at core start\n");
                        goto out;