rtl8187: Add USB ID for Netgear WG111V3
[safe/jmp/linux-2.6] / drivers / net / wireless / ipw2200.c
index 0bf1931..36e8d2f 100644 (file)
@@ -1,6 +1,6 @@
 /******************************************************************************
 
-  Copyright(c) 2003 - 2004 Intel Corporation. All rights reserved.
+  Copyright(c) 2003 - 2006 Intel Corporation. All rights reserved.
 
   802.11 status code portion of this file from ethereal-0.10.6:
     Copyright 2000, Axis Communications AB
 ******************************************************************************/
 
 #include "ipw2200.h"
+#include <linux/version.h>
 
-#define IPW2200_VERSION "1.0.3"
+
+#ifndef KBUILD_EXTMOD
+#define VK "k"
+#else
+#define VK
+#endif
+
+#ifdef CONFIG_IPW2200_DEBUG
+#define VD "d"
+#else
+#define VD
+#endif
+
+#ifdef CONFIG_IPW2200_MONITOR
+#define VM "m"
+#else
+#define VM
+#endif
+
+#ifdef CONFIG_IPW2200_PROMISCUOUS
+#define VP "p"
+#else
+#define VP
+#endif
+
+#ifdef CONFIG_IPW2200_RADIOTAP
+#define VR "r"
+#else
+#define VR
+#endif
+
+#ifdef CONFIG_IPW2200_QOS
+#define VQ "q"
+#else
+#define VQ
+#endif
+
+#define IPW2200_VERSION "1.2.2" VK VD VM VP VR VQ
 #define DRV_DESCRIPTION        "Intel(R) PRO/Wireless 2200/2915 Network Driver"
-#define DRV_COPYRIGHT  "Copyright(c) 2003-2004 Intel Corporation"
+#define DRV_COPYRIGHT  "Copyright(c) 2003-2006 Intel Corporation"
 #define DRV_VERSION     IPW2200_VERSION
 
+#define ETH_P_80211_STATS (ETH_P_80211_RAW + 1)
+
 MODULE_DESCRIPTION(DRV_DESCRIPTION);
 MODULE_VERSION(DRV_VERSION);
 MODULE_AUTHOR(DRV_COPYRIGHT);
 MODULE_LICENSE("GPL");
 
+static int cmdlog = 0;
 static int debug = 0;
 static int channel = 0;
 static int mode = 0;
@@ -51,10 +92,87 @@ static int associate = 1;
 static int auto_create = 1;
 static int led = 0;
 static int disable = 0;
+static int bt_coexist = 0;
+static int hwcrypto = 0;
+static int roaming = 1;
 static const char ipw_modes[] = {
        'a', 'b', 'g', '?'
 };
+static int antenna = CFG_SYS_ANTENNA_BOTH;
+
+#ifdef CONFIG_IPW2200_PROMISCUOUS
+static int rtap_iface = 0;     /* def: 0 -- do not create rtap interface */
+#endif
+
+
+#ifdef CONFIG_IPW2200_QOS
+static int qos_enable = 0;
+static int qos_burst_enable = 0;
+static int qos_no_ack_mask = 0;
+static int burst_duration_CCK = 0;
+static int burst_duration_OFDM = 0;
+
+static struct ieee80211_qos_parameters def_qos_parameters_OFDM = {
+       {QOS_TX0_CW_MIN_OFDM, QOS_TX1_CW_MIN_OFDM, QOS_TX2_CW_MIN_OFDM,
+        QOS_TX3_CW_MIN_OFDM},
+       {QOS_TX0_CW_MAX_OFDM, QOS_TX1_CW_MAX_OFDM, QOS_TX2_CW_MAX_OFDM,
+        QOS_TX3_CW_MAX_OFDM},
+       {QOS_TX0_AIFS, QOS_TX1_AIFS, QOS_TX2_AIFS, QOS_TX3_AIFS},
+       {QOS_TX0_ACM, QOS_TX1_ACM, QOS_TX2_ACM, QOS_TX3_ACM},
+       {QOS_TX0_TXOP_LIMIT_OFDM, QOS_TX1_TXOP_LIMIT_OFDM,
+        QOS_TX2_TXOP_LIMIT_OFDM, QOS_TX3_TXOP_LIMIT_OFDM}
+};
+
+static struct ieee80211_qos_parameters def_qos_parameters_CCK = {
+       {QOS_TX0_CW_MIN_CCK, QOS_TX1_CW_MIN_CCK, QOS_TX2_CW_MIN_CCK,
+        QOS_TX3_CW_MIN_CCK},
+       {QOS_TX0_CW_MAX_CCK, QOS_TX1_CW_MAX_CCK, QOS_TX2_CW_MAX_CCK,
+        QOS_TX3_CW_MAX_CCK},
+       {QOS_TX0_AIFS, QOS_TX1_AIFS, QOS_TX2_AIFS, QOS_TX3_AIFS},
+       {QOS_TX0_ACM, QOS_TX1_ACM, QOS_TX2_ACM, QOS_TX3_ACM},
+       {QOS_TX0_TXOP_LIMIT_CCK, QOS_TX1_TXOP_LIMIT_CCK, QOS_TX2_TXOP_LIMIT_CCK,
+        QOS_TX3_TXOP_LIMIT_CCK}
+};
+
+static struct ieee80211_qos_parameters def_parameters_OFDM = {
+       {DEF_TX0_CW_MIN_OFDM, DEF_TX1_CW_MIN_OFDM, DEF_TX2_CW_MIN_OFDM,
+        DEF_TX3_CW_MIN_OFDM},
+       {DEF_TX0_CW_MAX_OFDM, DEF_TX1_CW_MAX_OFDM, DEF_TX2_CW_MAX_OFDM,
+        DEF_TX3_CW_MAX_OFDM},
+       {DEF_TX0_AIFS, DEF_TX1_AIFS, DEF_TX2_AIFS, DEF_TX3_AIFS},
+       {DEF_TX0_ACM, DEF_TX1_ACM, DEF_TX2_ACM, DEF_TX3_ACM},
+       {DEF_TX0_TXOP_LIMIT_OFDM, DEF_TX1_TXOP_LIMIT_OFDM,
+        DEF_TX2_TXOP_LIMIT_OFDM, DEF_TX3_TXOP_LIMIT_OFDM}
+};
+
+static struct ieee80211_qos_parameters def_parameters_CCK = {
+       {DEF_TX0_CW_MIN_CCK, DEF_TX1_CW_MIN_CCK, DEF_TX2_CW_MIN_CCK,
+        DEF_TX3_CW_MIN_CCK},
+       {DEF_TX0_CW_MAX_CCK, DEF_TX1_CW_MAX_CCK, DEF_TX2_CW_MAX_CCK,
+        DEF_TX3_CW_MAX_CCK},
+       {DEF_TX0_AIFS, DEF_TX1_AIFS, DEF_TX2_AIFS, DEF_TX3_AIFS},
+       {DEF_TX0_ACM, DEF_TX1_ACM, DEF_TX2_ACM, DEF_TX3_ACM},
+       {DEF_TX0_TXOP_LIMIT_CCK, DEF_TX1_TXOP_LIMIT_CCK, DEF_TX2_TXOP_LIMIT_CCK,
+        DEF_TX3_TXOP_LIMIT_CCK}
+};
+
+static u8 qos_oui[QOS_OUI_LEN] = { 0x00, 0x50, 0xF2 };
+
+static int from_priority_to_tx_queue[] = {
+       IPW_TX_QUEUE_1, IPW_TX_QUEUE_2, IPW_TX_QUEUE_2, IPW_TX_QUEUE_1,
+       IPW_TX_QUEUE_3, IPW_TX_QUEUE_3, IPW_TX_QUEUE_4, IPW_TX_QUEUE_4
+};
+
+static u32 ipw_qos_get_burst_duration(struct ipw_priv *priv);
+
+static int ipw_send_qos_params_command(struct ipw_priv *priv, struct ieee80211_qos_parameters
+                                      *qos_param);
+static int ipw_send_qos_info_command(struct ipw_priv *priv, struct ieee80211_qos_information_element
+                                    *qos_param);
+#endif                         /* CONFIG_IPW2200_QOS */
 
+static struct iw_statistics *ipw_get_wireless_stats(struct net_device *dev);
+static void ipw_remove_current_network(struct ipw_priv *priv);
 static void ipw_rx(struct ipw_priv *priv);
 static int ipw_queue_tx_reclaim(struct ipw_priv *priv,
                                struct clx2_tx_queue *txq, int qindex);
@@ -69,42 +187,17 @@ static struct ipw_rx_queue *ipw_rx_queue_alloc(struct ipw_priv *);
 static void ipw_rx_queue_free(struct ipw_priv *, struct ipw_rx_queue *);
 static void ipw_rx_queue_replenish(void *);
 static int ipw_up(struct ipw_priv *);
-static void ipw_bg_up(void *);
+static void ipw_bg_up(struct work_struct *work);
 static void ipw_down(struct ipw_priv *);
-static void ipw_bg_down(void *);
+static void ipw_bg_down(struct work_struct *work);
 static int ipw_config(struct ipw_priv *);
 static int init_supported_rates(struct ipw_priv *priv,
                                struct ipw_supported_rates *prates);
+static void ipw_set_hwcrypto_keys(struct ipw_priv *);
+static void ipw_send_wep_keys(struct ipw_priv *, int);
 
-static u8 band_b_active_channel[MAX_B_CHANNELS] = {
-       1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0
-};
-static u8 band_a_active_channel[MAX_A_CHANNELS] = {
-       36, 40, 44, 48, 149, 153, 157, 161, 165, 52, 56, 60, 64, 0
-};
-
-static int is_valid_channel(int mode_mask, int channel)
-{
-       int i;
-
-       if (!channel)
-               return 0;
-
-       if (mode_mask & IEEE_A)
-               for (i = 0; i < MAX_A_CHANNELS; i++)
-                       if (band_a_active_channel[i] == channel)
-                               return IEEE_A;
-
-       if (mode_mask & (IEEE_B | IEEE_G))
-               for (i = 0; i < MAX_B_CHANNELS; i++)
-                       if (band_b_active_channel[i] == channel)
-                               return mode_mask & (IEEE_B | IEEE_G);
-
-       return 0;
-}
-
-static char *snprint_line(char *buf, size_t count,
-                         const u8 * data, u32 len, u32 ofs)
+static int snprint_line(char *buf, size_t count,
+                       const u8 * data, u32 len, u32 ofs)
 {
        int out, i, j, l;
        char c;
@@ -135,7 +228,7 @@ static char *snprint_line(char *buf, size_t count,
                        out += snprintf(buf + out, count - out, " ");
        }
 
-       return buf;
+       return out;
 }
 
 static void printk_buf(int level, const u8 * data, u32 len)
@@ -146,20 +239,42 @@ static void printk_buf(int level, const u8 * data, u32 len)
                return;
 
        while (len) {
-               printk(KERN_DEBUG "%s\n",
-                      snprint_line(line, sizeof(line), &data[ofs],
-                                   min(len, 16U), ofs));
+               snprint_line(line, sizeof(line), &data[ofs],
+                            min(len, 16U), ofs);
+               printk(KERN_DEBUG "%s\n", line);
                ofs += 16;
                len -= min(len, 16U);
        }
 }
 
+static int snprintk_buf(u8 * output, size_t size, const u8 * data, size_t len)
+{
+       size_t out = size;
+       u32 ofs = 0;
+       int total = 0;
+
+       while (size && len) {
+               out = snprint_line(output, size, &data[ofs],
+                                  min_t(size_t, len, 16U), ofs);
+
+               ofs += 16;
+               output += out;
+               size -= out;
+               len -= min_t(size_t, len, 16U);
+               total += out;
+       }
+       return total;
+}
+
+/* alias for 32-bit indirect read (for SRAM/reg above 4K), with debug wrapper */
 static u32 _ipw_read_reg32(struct ipw_priv *priv, u32 reg);
 #define ipw_read_reg32(a, b) _ipw_read_reg32(a, b)
 
+/* alias for 8-bit indirect read (for SRAM/reg above 4K), with debug wrapper */
 static u8 _ipw_read_reg8(struct ipw_priv *ipw, u32 reg);
 #define ipw_read_reg8(a, b) _ipw_read_reg8(a, b)
 
+/* 8-bit indirect write (for SRAM/reg above 4K), with debug wrapper */
 static void _ipw_write_reg8(struct ipw_priv *priv, u32 reg, u8 value);
 static inline void ipw_write_reg8(struct ipw_priv *a, u32 b, u8 c)
 {
@@ -168,6 +283,7 @@ static inline void ipw_write_reg8(struct ipw_priv *a, u32 b, u8 c)
        _ipw_write_reg8(a, b, c);
 }
 
+/* 16-bit indirect write (for SRAM/reg above 4K), with debug wrapper */
 static void _ipw_write_reg16(struct ipw_priv *priv, u32 reg, u16 value);
 static inline void ipw_write_reg16(struct ipw_priv *a, u32 b, u16 c)
 {
@@ -176,6 +292,7 @@ static inline void ipw_write_reg16(struct ipw_priv *a, u32 b, u16 c)
        _ipw_write_reg16(a, b, c);
 }
 
+/* 32-bit indirect write (for SRAM/reg above 4K), with debug wrapper */
 static void _ipw_write_reg32(struct ipw_priv *priv, u32 reg, u32 value);
 static inline void ipw_write_reg32(struct ipw_priv *a, u32 b, u32 c)
 {
@@ -184,111 +301,149 @@ static inline void ipw_write_reg32(struct ipw_priv *a, u32 b, u32 c)
        _ipw_write_reg32(a, b, c);
 }
 
+/* 8-bit direct write (low 4K) */
 #define _ipw_write8(ipw, ofs, val) writeb((val), (ipw)->hw_base + (ofs))
-#define ipw_write8(ipw, ofs, val) \
+
+/* 8-bit direct write (for low 4K of SRAM/regs), with debug wrapper */
+#define ipw_write8(ipw, ofs, val) do { \
  IPW_DEBUG_IO("%s %d: write_direct8(0x%08X, 0x%08X)\n", __FILE__, __LINE__, (u32)(ofs), (u32)(val)); \
- _ipw_write8(ipw, ofs, val)
+ _ipw_write8(ipw, ofs, val); \
+ } while (0)
 
+/* 16-bit direct write (low 4K) */
 #define _ipw_write16(ipw, ofs, val) writew((val), (ipw)->hw_base + (ofs))
+
+/* 16-bit direct write (for low 4K of SRAM/regs), with debug wrapper */
 #define ipw_write16(ipw, ofs, val) \
  IPW_DEBUG_IO("%s %d: write_direct16(0x%08X, 0x%08X)\n", __FILE__, __LINE__, (u32)(ofs), (u32)(val)); \
  _ipw_write16(ipw, ofs, val)
 
+/* 32-bit direct write (low 4K) */
 #define _ipw_write32(ipw, ofs, val) writel((val), (ipw)->hw_base + (ofs))
+
+/* 32-bit direct write (for low 4K of SRAM/regs), with debug wrapper */
 #define ipw_write32(ipw, ofs, val) \
  IPW_DEBUG_IO("%s %d: write_direct32(0x%08X, 0x%08X)\n", __FILE__, __LINE__, (u32)(ofs), (u32)(val)); \
  _ipw_write32(ipw, ofs, val)
 
+/* 8-bit direct read (low 4K) */
 #define _ipw_read8(ipw, ofs) readb((ipw)->hw_base + (ofs))
+
+/* 8-bit direct read (low 4K), with debug wrapper */
 static inline u8 __ipw_read8(char *f, u32 l, struct ipw_priv *ipw, u32 ofs)
 {
        IPW_DEBUG_IO("%s %d: read_direct8(0x%08X)\n", f, l, (u32) (ofs));
        return _ipw_read8(ipw, ofs);
 }
 
+/* alias to 8-bit direct read (low 4K of SRAM/regs), with debug wrapper */
 #define ipw_read8(ipw, ofs) __ipw_read8(__FILE__, __LINE__, ipw, ofs)
 
+/* 16-bit direct read (low 4K) */
 #define _ipw_read16(ipw, ofs) readw((ipw)->hw_base + (ofs))
+
+/* 16-bit direct read (low 4K), with debug wrapper */
 static inline u16 __ipw_read16(char *f, u32 l, struct ipw_priv *ipw, u32 ofs)
 {
        IPW_DEBUG_IO("%s %d: read_direct16(0x%08X)\n", f, l, (u32) (ofs));
        return _ipw_read16(ipw, ofs);
 }
 
+/* alias to 16-bit direct read (low 4K of SRAM/regs), with debug wrapper */
 #define ipw_read16(ipw, ofs) __ipw_read16(__FILE__, __LINE__, ipw, ofs)
 
+/* 32-bit direct read (low 4K) */
 #define _ipw_read32(ipw, ofs) readl((ipw)->hw_base + (ofs))
+
+/* 32-bit direct read (low 4K), with debug wrapper */
 static inline u32 __ipw_read32(char *f, u32 l, struct ipw_priv *ipw, u32 ofs)
 {
        IPW_DEBUG_IO("%s %d: read_direct32(0x%08X)\n", f, l, (u32) (ofs));
        return _ipw_read32(ipw, ofs);
 }
 
+/* alias to 32-bit direct read (low 4K of SRAM/regs), with debug wrapper */
 #define ipw_read32(ipw, ofs) __ipw_read32(__FILE__, __LINE__, ipw, ofs)
 
+/* multi-byte read (above 4K), with debug wrapper */
 static void _ipw_read_indirect(struct ipw_priv *, u32, u8 *, int);
-#define ipw_read_indirect(a, b, c, d) \
-       IPW_DEBUG_IO("%s %d: read_inddirect(0x%08X) %d bytes\n", __FILE__, __LINE__, (u32)(b), d); \
-       _ipw_read_indirect(a, b, c, d)
+static inline void __ipw_read_indirect(const char *f, int l,
+                                      struct ipw_priv *a, u32 b, u8 * c, int d)
+{
+       IPW_DEBUG_IO("%s %d: read_indirect(0x%08X) %d bytes\n", f, l, (u32) (b),
+                    d);
+       _ipw_read_indirect(a, b, c, d);
+}
+
+/* alias to multi-byte read (SRAM/regs above 4K), with debug wrapper */
+#define ipw_read_indirect(a, b, c, d) __ipw_read_indirect(__FILE__, __LINE__, a, b, c, d)
 
+/* alias to multi-byte read (SRAM/regs above 4K), with debug wrapper */
 static void _ipw_write_indirect(struct ipw_priv *priv, u32 addr, u8 * data,
                                int num);
 #define ipw_write_indirect(a, b, c, d) \
        IPW_DEBUG_IO("%s %d: write_indirect(0x%08X) %d bytes\n", __FILE__, __LINE__, (u32)(b), d); \
-        _ipw_write_indirect(a, b, c, d)
+       _ipw_write_indirect(a, b, c, d)
 
-/* indirect write s */
+/* 32-bit indirect write (above 4K) */
 static void _ipw_write_reg32(struct ipw_priv *priv, u32 reg, u32 value)
 {
        IPW_DEBUG_IO(" %p : reg = 0x%8X : value = 0x%8X\n", priv, reg, value);
-       _ipw_write32(priv, CX2_INDIRECT_ADDR, reg);
-       _ipw_write32(priv, CX2_INDIRECT_DATA, value);
+       _ipw_write32(priv, IPW_INDIRECT_ADDR, reg);
+       _ipw_write32(priv, IPW_INDIRECT_DATA, value);
 }
 
+/* 8-bit indirect write (above 4K) */
 static void _ipw_write_reg8(struct ipw_priv *priv, u32 reg, u8 value)
 {
+       u32 aligned_addr = reg & IPW_INDIRECT_ADDR_MASK;        /* dword align */
+       u32 dif_len = reg - aligned_addr;
+
        IPW_DEBUG_IO(" reg = 0x%8X : value = 0x%8X\n", reg, value);
-       _ipw_write32(priv, CX2_INDIRECT_ADDR, reg & CX2_INDIRECT_ADDR_MASK);
-       _ipw_write8(priv, CX2_INDIRECT_DATA, value);
-       IPW_DEBUG_IO(" reg = 0x%8lX : value = 0x%8X\n",
-                    (unsigned long)(priv->hw_base + CX2_INDIRECT_DATA), value);
+       _ipw_write32(priv, IPW_INDIRECT_ADDR, aligned_addr);
+       _ipw_write8(priv, IPW_INDIRECT_DATA + dif_len, value);
 }
 
+/* 16-bit indirect write (above 4K) */
 static void _ipw_write_reg16(struct ipw_priv *priv, u32 reg, u16 value)
 {
+       u32 aligned_addr = reg & IPW_INDIRECT_ADDR_MASK;        /* dword align */
+       u32 dif_len = (reg - aligned_addr) & (~0x1ul);
+
        IPW_DEBUG_IO(" reg = 0x%8X : value = 0x%8X\n", reg, value);
-       _ipw_write32(priv, CX2_INDIRECT_ADDR, reg & CX2_INDIRECT_ADDR_MASK);
-       _ipw_write16(priv, CX2_INDIRECT_DATA, value);
+       _ipw_write32(priv, IPW_INDIRECT_ADDR, aligned_addr);
+       _ipw_write16(priv, IPW_INDIRECT_DATA + dif_len, value);
 }
 
-/* indirect read s */
-
+/* 8-bit indirect read (above 4K) */
 static u8 _ipw_read_reg8(struct ipw_priv *priv, u32 reg)
 {
        u32 word;
-       _ipw_write32(priv, CX2_INDIRECT_ADDR, reg & CX2_INDIRECT_ADDR_MASK);
+       _ipw_write32(priv, IPW_INDIRECT_ADDR, reg & IPW_INDIRECT_ADDR_MASK);
        IPW_DEBUG_IO(" reg = 0x%8X : \n", reg);
-       word = _ipw_read32(priv, CX2_INDIRECT_DATA);
+       word = _ipw_read32(priv, IPW_INDIRECT_DATA);
        return (word >> ((reg & 0x3) * 8)) & 0xff;
 }
 
+/* 32-bit indirect read (above 4K) */
 static u32 _ipw_read_reg32(struct ipw_priv *priv, u32 reg)
 {
        u32 value;
 
        IPW_DEBUG_IO("%p : reg = 0x%08x\n", priv, reg);
 
-       _ipw_write32(priv, CX2_INDIRECT_ADDR, reg);
-       value = _ipw_read32(priv, CX2_INDIRECT_DATA);
+       _ipw_write32(priv, IPW_INDIRECT_ADDR, reg);
+       value = _ipw_read32(priv, IPW_INDIRECT_DATA);
        IPW_DEBUG_IO(" reg = 0x%4X : value = 0x%4x \n", reg, value);
        return value;
 }
 
-/* iterative/auto-increment 32 bit reads and writes */
+/* General purpose, no alignment requirement, iterative (multi-byte) read, */
+/*    for area above 1st 4K of SRAM/reg space */
 static void _ipw_read_indirect(struct ipw_priv *priv, u32 addr, u8 * buf,
                               int num)
 {
-       u32 aligned_addr = addr & CX2_INDIRECT_ADDR_MASK;
+       u32 aligned_addr = addr & IPW_INDIRECT_ADDR_MASK;       /* dword align */
        u32 dif_len = addr - aligned_addr;
        u32 i;
 
@@ -298,31 +453,34 @@ static void _ipw_read_indirect(struct ipw_priv *priv, u32 addr, u8 * buf,
                return;
        }
 
-       /* Read the first nibble byte by byte */
+       /* Read the first dword (or portion) byte by byte */
        if (unlikely(dif_len)) {
-               _ipw_write32(priv, CX2_INDIRECT_ADDR, aligned_addr);
+               _ipw_write32(priv, IPW_INDIRECT_ADDR, aligned_addr);
                /* Start reading at aligned_addr + dif_len */
                for (i = dif_len; ((i < 4) && (num > 0)); i++, num--)
-                       *buf++ = _ipw_read8(priv, CX2_INDIRECT_DATA + i);
+                       *buf++ = _ipw_read8(priv, IPW_INDIRECT_DATA + i);
                aligned_addr += 4;
        }
 
-       _ipw_write32(priv, CX2_AUTOINC_ADDR, aligned_addr);
+       /* Read all of the middle dwords as dwords, with auto-increment */
+       _ipw_write32(priv, IPW_AUTOINC_ADDR, aligned_addr);
        for (; num >= 4; buf += 4, aligned_addr += 4, num -= 4)
-               *(u32 *) buf = _ipw_read32(priv, CX2_AUTOINC_DATA);
+               *(u32 *) buf = _ipw_read32(priv, IPW_AUTOINC_DATA);
 
-       /* Copy the last nibble */
+       /* Read the last dword (or portion) byte by byte */
        if (unlikely(num)) {
-               _ipw_write32(priv, CX2_INDIRECT_ADDR, aligned_addr);
+               _ipw_write32(priv, IPW_INDIRECT_ADDR, aligned_addr);
                for (i = 0; num > 0; i++, num--)
-                       *buf++ = ipw_read8(priv, CX2_INDIRECT_DATA + i);
+                       *buf++ = ipw_read8(priv, IPW_INDIRECT_DATA + i);
        }
 }
 
+/* General purpose, no alignment requirement, iterative (multi-byte) write, */
+/*    for area above 1st 4K of SRAM/reg space */
 static void _ipw_write_indirect(struct ipw_priv *priv, u32 addr, u8 * buf,
                                int num)
 {
-       u32 aligned_addr = addr & CX2_INDIRECT_ADDR_MASK;
+       u32 aligned_addr = addr & IPW_INDIRECT_ADDR_MASK;       /* dword align */
        u32 dif_len = addr - aligned_addr;
        u32 i;
 
@@ -332,57 +490,80 @@ static void _ipw_write_indirect(struct ipw_priv *priv, u32 addr, u8 * buf,
                return;
        }
 
-       /* Write the first nibble byte by byte */
+       /* Write the first dword (or portion) byte by byte */
        if (unlikely(dif_len)) {
-               _ipw_write32(priv, CX2_INDIRECT_ADDR, aligned_addr);
-               /* Start reading at aligned_addr + dif_len */
+               _ipw_write32(priv, IPW_INDIRECT_ADDR, aligned_addr);
+               /* Start writing at aligned_addr + dif_len */
                for (i = dif_len; ((i < 4) && (num > 0)); i++, num--, buf++)
-                       _ipw_write8(priv, CX2_INDIRECT_DATA + i, *buf);
+                       _ipw_write8(priv, IPW_INDIRECT_DATA + i, *buf);
                aligned_addr += 4;
        }
 
-       _ipw_write32(priv, CX2_AUTOINC_ADDR, aligned_addr);
+       /* Write all of the middle dwords as dwords, with auto-increment */
+       _ipw_write32(priv, IPW_AUTOINC_ADDR, aligned_addr);
        for (; num >= 4; buf += 4, aligned_addr += 4, num -= 4)
-               _ipw_write32(priv, CX2_AUTOINC_DATA, *(u32 *) buf);
+               _ipw_write32(priv, IPW_AUTOINC_DATA, *(u32 *) buf);
 
-       /* Copy the last nibble */
+       /* Write the last dword (or portion) byte by byte */
        if (unlikely(num)) {
-               _ipw_write32(priv, CX2_INDIRECT_ADDR, aligned_addr);
+               _ipw_write32(priv, IPW_INDIRECT_ADDR, aligned_addr);
                for (i = 0; num > 0; i++, num--, buf++)
-                       _ipw_write8(priv, CX2_INDIRECT_DATA + i, *buf);
+                       _ipw_write8(priv, IPW_INDIRECT_DATA + i, *buf);
        }
 }
 
+/* General purpose, no alignment requirement, iterative (multi-byte) write, */
+/*    for 1st 4K of SRAM/regs space */
 static void ipw_write_direct(struct ipw_priv *priv, u32 addr, void *buf,
                             int num)
 {
        memcpy_toio((priv->hw_base + addr), buf, num);
 }
 
+/* Set bit(s) in low 4K of SRAM/regs */
 static inline void ipw_set_bit(struct ipw_priv *priv, u32 reg, u32 mask)
 {
        ipw_write32(priv, reg, ipw_read32(priv, reg) | mask);
 }
 
+/* Clear bit(s) in low 4K of SRAM/regs */
 static inline void ipw_clear_bit(struct ipw_priv *priv, u32 reg, u32 mask)
 {
        ipw_write32(priv, reg, ipw_read32(priv, reg) & ~mask);
 }
 
-static inline void ipw_enable_interrupts(struct ipw_priv *priv)
+static inline void __ipw_enable_interrupts(struct ipw_priv *priv)
 {
        if (priv->status & STATUS_INT_ENABLED)
                return;
        priv->status |= STATUS_INT_ENABLED;
-       ipw_write32(priv, CX2_INTA_MASK_R, CX2_INTA_MASK_ALL);
+       ipw_write32(priv, IPW_INTA_MASK_R, IPW_INTA_MASK_ALL);
 }
 
-static inline void ipw_disable_interrupts(struct ipw_priv *priv)
+static inline void __ipw_disable_interrupts(struct ipw_priv *priv)
 {
        if (!(priv->status & STATUS_INT_ENABLED))
                return;
        priv->status &= ~STATUS_INT_ENABLED;
-       ipw_write32(priv, CX2_INTA_MASK_R, ~CX2_INTA_MASK_ALL);
+       ipw_write32(priv, IPW_INTA_MASK_R, ~IPW_INTA_MASK_ALL);
+}
+
+static inline void ipw_enable_interrupts(struct ipw_priv *priv)
+{
+       unsigned long flags;
+
+       spin_lock_irqsave(&priv->irq_lock, flags);
+       __ipw_enable_interrupts(priv);
+       spin_unlock_irqrestore(&priv->irq_lock, flags);
+}
+
+static inline void ipw_disable_interrupts(struct ipw_priv *priv)
+{
+       unsigned long flags;
+
+       spin_lock_irqsave(&priv->irq_lock, flags);
+       __ipw_disable_interrupts(priv);
+       spin_unlock_irqrestore(&priv->irq_lock, flags);
 }
 
 static char *ipw_error_desc(u32 val)
@@ -397,81 +578,59 @@ static char *ipw_error_desc(u32 val)
        case IPW_FW_ERROR_MEMORY_OVERFLOW:
                return "MEMORY_OVERFLOW";
        case IPW_FW_ERROR_BAD_PARAM:
-               return "ERROR_BAD_PARAM";
+               return "BAD_PARAM";
        case IPW_FW_ERROR_BAD_CHECKSUM:
-               return "ERROR_BAD_CHECKSUM";
+               return "BAD_CHECKSUM";
        case IPW_FW_ERROR_NMI_INTERRUPT:
-               return "ERROR_NMI_INTERRUPT";
+               return "NMI_INTERRUPT";
        case IPW_FW_ERROR_BAD_DATABASE:
-               return "ERROR_BAD_DATABASE";
+               return "BAD_DATABASE";
        case IPW_FW_ERROR_ALLOC_FAIL:
-               return "ERROR_ALLOC_FAIL";
+               return "ALLOC_FAIL";
        case IPW_FW_ERROR_DMA_UNDERRUN:
-               return "ERROR_DMA_UNDERRUN";
+               return "DMA_UNDERRUN";
        case IPW_FW_ERROR_DMA_STATUS:
-               return "ERROR_DMA_STATUS";
-       case IPW_FW_ERROR_DINOSTATUS_ERROR:
-               return "ERROR_DINOSTATUS_ERROR";
-       case IPW_FW_ERROR_EEPROMSTATUS_ERROR:
-               return "ERROR_EEPROMSTATUS_ERROR";
+               return "DMA_STATUS";
+       case IPW_FW_ERROR_DINO_ERROR:
+               return "DINO_ERROR";
+       case IPW_FW_ERROR_EEPROM_ERROR:
+               return "EEPROM_ERROR";
        case IPW_FW_ERROR_SYSASSERT:
-               return "ERROR_SYSASSERT";
+               return "SYSASSERT";
        case IPW_FW_ERROR_FATAL_ERROR:
-               return "ERROR_FATALSTATUS_ERROR";
+               return "FATAL_ERROR";
        default:
-               return "UNKNOWNSTATUS_ERROR";
+               return "UNKNOWN_ERROR";
        }
 }
 
-static void ipw_dump_nic_error_log(struct ipw_priv *priv)
+static void ipw_dump_error_log(struct ipw_priv *priv,
+                              struct ipw_fw_error *error)
 {
-       u32 desc, time, blink1, blink2, ilink1, ilink2, idata, i, count, base;
-
-       base = ipw_read32(priv, IPWSTATUS_ERROR_LOG);
-       count = ipw_read_reg32(priv, base);
+       u32 i;
 
-       if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
-               IPW_ERROR("Start IPW Error Log Dump:\n");
-               IPW_ERROR("Status: 0x%08X, Config: %08X\n",
-                         priv->status, priv->config);
+       if (!error) {
+               IPW_ERROR("Error allocating and capturing error log.  "
+                         "Nothing to dump.\n");
+               return;
        }
 
-       for (i = ERROR_START_OFFSET;
-            i <= count * ERROR_ELEM_SIZE; i += ERROR_ELEM_SIZE) {
-               desc = ipw_read_reg32(priv, base + i);
-               time = ipw_read_reg32(priv, base + i + 1 * sizeof(u32));
-               blink1 = ipw_read_reg32(priv, base + i + 2 * sizeof(u32));
-               blink2 = ipw_read_reg32(priv, base + i + 3 * sizeof(u32));
-               ilink1 = ipw_read_reg32(priv, base + i + 4 * sizeof(u32));
-               ilink2 = ipw_read_reg32(priv, base + i + 5 * sizeof(u32));
-               idata = ipw_read_reg32(priv, base + i + 6 * sizeof(u32));
+       IPW_ERROR("Start IPW Error Log Dump:\n");
+       IPW_ERROR("Status: 0x%08X, Config: %08X\n",
+                 error->status, error->config);
 
+       for (i = 0; i < error->elem_len; i++)
                IPW_ERROR("%s %i 0x%08x  0x%08x  0x%08x  0x%08x  0x%08x\n",
-                         ipw_error_desc(desc), time, blink1, blink2,
-                         ilink1, ilink2, idata);
-       }
-}
-
-static void ipw_dump_nic_event_log(struct ipw_priv *priv)
-{
-       u32 ev, time, data, i, count, base;
-
-       base = ipw_read32(priv, IPW_EVENT_LOG);
-       count = ipw_read_reg32(priv, base);
-
-       if (EVENT_START_OFFSET <= count * EVENT_ELEM_SIZE)
-               IPW_ERROR("Start IPW Event Log Dump:\n");
-
-       for (i = EVENT_START_OFFSET;
-            i <= count * EVENT_ELEM_SIZE; i += EVENT_ELEM_SIZE) {
-               ev = ipw_read_reg32(priv, base + i);
-               time = ipw_read_reg32(priv, base + i + 1 * sizeof(u32));
-               data = ipw_read_reg32(priv, base + i + 2 * sizeof(u32));
-
-#ifdef CONFIG_IPW_DEBUG
-               IPW_ERROR("%i\t0x%08x\t%i\n", time, data, ev);
-#endif
-       }
+                         ipw_error_desc(error->elem[i].desc),
+                         error->elem[i].time,
+                         error->elem[i].blink1,
+                         error->elem[i].blink2,
+                         error->elem[i].link1,
+                         error->elem[i].link2, error->elem[i].data);
+       for (i = 0; i < error->log_len; i++)
+               IPW_ERROR("%i\t0x%08x\t%i\n",
+                         error->log[i].time,
+                         error->log[i].data, error->log[i].event);
 }
 
 static inline int ipw_is_init(struct ipw_priv *priv)
@@ -644,15 +803,15 @@ static void ipw_init_ordinals(struct ipw_priv *priv)
 
 }
 
-u32 ipw_register_toggle(u32 reg)
+static u32 ipw_register_toggle(u32 reg)
 {
-       reg &= ~CX2_START_STANDBY;
-       if (reg & CX2_GATE_ODMA)
-               reg &= ~CX2_GATE_ODMA;
-       if (reg & CX2_GATE_IDMA)
-               reg &= ~CX2_GATE_IDMA;
-       if (reg & CX2_GATE_ADMA)
-               reg &= ~CX2_GATE_ADMA;
+       reg &= ~IPW_START_STANDBY;
+       if (reg & IPW_GATE_ODMA)
+               reg &= ~IPW_GATE_ODMA;
+       if (reg & IPW_GATE_IDMA)
+               reg &= ~IPW_GATE_IDMA;
+       if (reg & IPW_GATE_ADMA)
+               reg &= ~IPW_GATE_ADMA;
        return reg;
 }
 
@@ -665,11 +824,11 @@ u32 ipw_register_toggle(u32 reg)
  * - On radio OFF, turn off any LEDs started during radio on
  *
  */
-#define LD_TIME_LINK_ON 300
-#define LD_TIME_LINK_OFF 2700
-#define LD_TIME_ACT_ON 250
+#define LD_TIME_LINK_ON msecs_to_jiffies(300)
+#define LD_TIME_LINK_OFF msecs_to_jiffies(2700)
+#define LD_TIME_ACT_ON msecs_to_jiffies(250)
 
-void ipw_led_link_on(struct ipw_priv *priv)
+static void ipw_led_link_on(struct ipw_priv *priv)
 {
        unsigned long flags;
        u32 led;
@@ -684,13 +843,13 @@ void ipw_led_link_on(struct ipw_priv *priv)
        if (!(priv->status & STATUS_RF_KILL_MASK) &&
            !(priv->status & STATUS_LED_LINK_ON)) {
                IPW_DEBUG_LED("Link LED On\n");
-               led = ipw_read_reg32(priv, CX2_EVENT_REG);
+               led = ipw_read_reg32(priv, IPW_EVENT_REG);
                led |= priv->led_association_on;
 
                led = ipw_register_toggle(led);
 
                IPW_DEBUG_LED("Reg: 0x%08X\n", led);
-               ipw_write_reg32(priv, CX2_EVENT_REG, led);
+               ipw_write_reg32(priv, IPW_EVENT_REG, led);
 
                priv->status |= STATUS_LED_LINK_ON;
 
@@ -704,15 +863,16 @@ void ipw_led_link_on(struct ipw_priv *priv)
        spin_unlock_irqrestore(&priv->lock, flags);
 }
 
-static void ipw_bg_led_link_on(void *data)
+static void ipw_bg_led_link_on(struct work_struct *work)
 {
-       struct ipw_priv *priv = data;
-       down(&priv->sem);
-       ipw_led_link_on(data);
-       up(&priv->sem);
+       struct ipw_priv *priv =
+               container_of(work, struct ipw_priv, led_link_on.work);
+       mutex_lock(&priv->mutex);
+       ipw_led_link_on(priv);
+       mutex_unlock(&priv->mutex);
 }
 
-void ipw_led_link_off(struct ipw_priv *priv)
+static void ipw_led_link_off(struct ipw_priv *priv)
 {
        unsigned long flags;
        u32 led;
@@ -725,12 +885,12 @@ void ipw_led_link_off(struct ipw_priv *priv)
        spin_lock_irqsave(&priv->lock, flags);
 
        if (priv->status & STATUS_LED_LINK_ON) {
-               led = ipw_read_reg32(priv, CX2_EVENT_REG);
+               led = ipw_read_reg32(priv, IPW_EVENT_REG);
                led &= priv->led_association_off;
                led = ipw_register_toggle(led);
 
                IPW_DEBUG_LED("Reg: 0x%08X\n", led);
-               ipw_write_reg32(priv, CX2_EVENT_REG, led);
+               ipw_write_reg32(priv, IPW_EVENT_REG, led);
 
                IPW_DEBUG_LED("Link LED Off\n");
 
@@ -748,37 +908,33 @@ void ipw_led_link_off(struct ipw_priv *priv)
        spin_unlock_irqrestore(&priv->lock, flags);
 }
 
-static void ipw_bg_led_link_off(void *data)
+static void ipw_bg_led_link_off(struct work_struct *work)
 {
-       struct ipw_priv *priv = data;
-       down(&priv->sem);
-       ipw_led_link_off(data);
-       up(&priv->sem);
+       struct ipw_priv *priv =
+               container_of(work, struct ipw_priv, led_link_off.work);
+       mutex_lock(&priv->mutex);
+       ipw_led_link_off(priv);
+       mutex_unlock(&priv->mutex);
 }
 
-void ipw_led_activity_on(struct ipw_priv *priv)
+static void __ipw_led_activity_on(struct ipw_priv *priv)
 {
-       unsigned long flags;
        u32 led;
 
        if (priv->config & CFG_NO_LED)
                return;
 
-       spin_lock_irqsave(&priv->lock, flags);
-
-       if (priv->status & STATUS_RF_KILL_MASK) {
-               spin_unlock_irqrestore(&priv->lock, flags);
+       if (priv->status & STATUS_RF_KILL_MASK)
                return;
-       }
 
        if (!(priv->status & STATUS_LED_ACT_ON)) {
-               led = ipw_read_reg32(priv, CX2_EVENT_REG);
+               led = ipw_read_reg32(priv, IPW_EVENT_REG);
                led |= priv->led_activity_on;
 
                led = ipw_register_toggle(led);
 
                IPW_DEBUG_LED("Reg: 0x%08X\n", led);
-               ipw_write_reg32(priv, CX2_EVENT_REG, led);
+               ipw_write_reg32(priv, IPW_EVENT_REG, led);
 
                IPW_DEBUG_LED("Activity LED On\n");
 
@@ -793,11 +949,19 @@ void ipw_led_activity_on(struct ipw_priv *priv)
                queue_delayed_work(priv->workqueue, &priv->led_act_off,
                                   LD_TIME_ACT_ON);
        }
+}
 
+#if 0
+void ipw_led_activity_on(struct ipw_priv *priv)
+{
+       unsigned long flags;
+       spin_lock_irqsave(&priv->lock, flags);
+       __ipw_led_activity_on(priv);
        spin_unlock_irqrestore(&priv->lock, flags);
 }
+#endif  /*  0  */
 
-void ipw_led_activity_off(struct ipw_priv *priv)
+static void ipw_led_activity_off(struct ipw_priv *priv)
 {
        unsigned long flags;
        u32 led;
@@ -808,13 +972,13 @@ void ipw_led_activity_off(struct ipw_priv *priv)
        spin_lock_irqsave(&priv->lock, flags);
 
        if (priv->status & STATUS_LED_ACT_ON) {
-               led = ipw_read_reg32(priv, CX2_EVENT_REG);
+               led = ipw_read_reg32(priv, IPW_EVENT_REG);
                led &= priv->led_activity_off;
 
                led = ipw_register_toggle(led);
 
                IPW_DEBUG_LED("Reg: 0x%08X\n", led);
-               ipw_write_reg32(priv, CX2_EVENT_REG, led);
+               ipw_write_reg32(priv, IPW_EVENT_REG, led);
 
                IPW_DEBUG_LED("Activity LED Off\n");
 
@@ -824,15 +988,16 @@ void ipw_led_activity_off(struct ipw_priv *priv)
        spin_unlock_irqrestore(&priv->lock, flags);
 }
 
-static void ipw_bg_led_activity_off(void *data)
+static void ipw_bg_led_activity_off(struct work_struct *work)
 {
-       struct ipw_priv *priv = data;
-       down(&priv->sem);
-       ipw_led_activity_off(data);
-       up(&priv->sem);
+       struct ipw_priv *priv =
+               container_of(work, struct ipw_priv, led_act_off.work);
+       mutex_lock(&priv->mutex);
+       ipw_led_activity_off(priv);
+       mutex_unlock(&priv->mutex);
 }
 
-void ipw_led_band_on(struct ipw_priv *priv)
+static void ipw_led_band_on(struct ipw_priv *priv)
 {
        unsigned long flags;
        u32 led;
@@ -844,7 +1009,7 @@ void ipw_led_band_on(struct ipw_priv *priv)
 
        spin_lock_irqsave(&priv->lock, flags);
 
-       led = ipw_read_reg32(priv, CX2_EVENT_REG);
+       led = ipw_read_reg32(priv, IPW_EVENT_REG);
        if (priv->assoc_network->mode == IEEE_A) {
                led |= priv->led_ofdm_on;
                led &= priv->led_association_off;
@@ -862,12 +1027,12 @@ void ipw_led_band_on(struct ipw_priv *priv)
        led = ipw_register_toggle(led);
 
        IPW_DEBUG_LED("Reg: 0x%08X\n", led);
-       ipw_write_reg32(priv, CX2_EVENT_REG, led);
+       ipw_write_reg32(priv, IPW_EVENT_REG, led);
 
        spin_unlock_irqrestore(&priv->lock, flags);
 }
 
-void ipw_led_band_off(struct ipw_priv *priv)
+static void ipw_led_band_off(struct ipw_priv *priv)
 {
        unsigned long flags;
        u32 led;
@@ -878,36 +1043,36 @@ void ipw_led_band_off(struct ipw_priv *priv)
 
        spin_lock_irqsave(&priv->lock, flags);
 
-       led = ipw_read_reg32(priv, CX2_EVENT_REG);
+       led = ipw_read_reg32(priv, IPW_EVENT_REG);
        led &= priv->led_ofdm_off;
        led &= priv->led_association_off;
 
        led = ipw_register_toggle(led);
 
        IPW_DEBUG_LED("Reg: 0x%08X\n", led);
-       ipw_write_reg32(priv, CX2_EVENT_REG, led);
+       ipw_write_reg32(priv, IPW_EVENT_REG, led);
 
        spin_unlock_irqrestore(&priv->lock, flags);
 }
 
-void ipw_led_radio_on(struct ipw_priv *priv)
+static void ipw_led_radio_on(struct ipw_priv *priv)
 {
        ipw_led_link_on(priv);
 }
 
-void ipw_led_radio_off(struct ipw_priv *priv)
+static void ipw_led_radio_off(struct ipw_priv *priv)
 {
        ipw_led_activity_off(priv);
        ipw_led_link_off(priv);
 }
 
-void ipw_led_link_up(struct ipw_priv *priv)
+static void ipw_led_link_up(struct ipw_priv *priv)
 {
        /* Set the Link Led on for all nic types */
        ipw_led_link_on(priv);
 }
 
-void ipw_led_link_down(struct ipw_priv *priv)
+static void ipw_led_link_down(struct ipw_priv *priv)
 {
        ipw_led_activity_off(priv);
        ipw_led_link_off(priv);
@@ -916,28 +1081,28 @@ void ipw_led_link_down(struct ipw_priv *priv)
                ipw_led_radio_off(priv);
 }
 
-void ipw_led_init(struct ipw_priv *priv)
+static void ipw_led_init(struct ipw_priv *priv)
 {
        priv->nic_type = priv->eeprom[EEPROM_NIC_TYPE];
 
        /* Set the default PINs for the link and activity leds */
-       priv->led_activity_on = CX2_ACTIVITY_LED;
-       priv->led_activity_off = ~(CX2_ACTIVITY_LED);
+       priv->led_activity_on = IPW_ACTIVITY_LED;
+       priv->led_activity_off = ~(IPW_ACTIVITY_LED);
 
-       priv->led_association_on = CX2_ASSOCIATED_LED;
-       priv->led_association_off = ~(CX2_ASSOCIATED_LED);
+       priv->led_association_on = IPW_ASSOCIATED_LED;
+       priv->led_association_off = ~(IPW_ASSOCIATED_LED);
 
        /* Set the default PINs for the OFDM leds */
-       priv->led_ofdm_on = CX2_OFDM_LED;
-       priv->led_ofdm_off = ~(CX2_OFDM_LED);
+       priv->led_ofdm_on = IPW_OFDM_LED;
+       priv->led_ofdm_off = ~(IPW_OFDM_LED);
 
        switch (priv->nic_type) {
        case EEPROM_NIC_TYPE_1:
                /* In this NIC type, the LEDs are reversed.... */
-               priv->led_activity_on = CX2_ASSOCIATED_LED;
-               priv->led_activity_off = ~(CX2_ASSOCIATED_LED);
-               priv->led_association_on = CX2_ACTIVITY_LED;
-               priv->led_association_off = ~(CX2_ACTIVITY_LED);
+               priv->led_activity_on = IPW_ASSOCIATED_LED;
+               priv->led_activity_off = ~(IPW_ASSOCIATED_LED);
+               priv->led_association_on = IPW_ACTIVITY_LED;
+               priv->led_association_off = ~(IPW_ACTIVITY_LED);
 
                if (!(priv->config & CFG_NO_LED))
                        ipw_led_band_on(priv);
@@ -967,14 +1132,14 @@ void ipw_led_init(struct ipw_priv *priv)
        }
 }
 
-void ipw_led_shutdown(struct ipw_priv *priv)
+static void ipw_led_shutdown(struct ipw_priv *priv)
 {
-       cancel_delayed_work(&priv->led_link_on);
-       cancel_delayed_work(&priv->led_link_off);
-       cancel_delayed_work(&priv->led_act_off);
        ipw_led_activity_off(priv);
        ipw_led_link_off(priv);
        ipw_led_band_off(priv);
+       cancel_delayed_work(&priv->led_link_on);
+       cancel_delayed_work(&priv->led_link_off);
+       cancel_delayed_work(&priv->led_act_off);
 }
 
 /*
@@ -1014,6 +1179,262 @@ static ssize_t store_debug_level(struct device_driver *d, const char *buf,
 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
                   show_debug_level, store_debug_level);
 
+static inline u32 ipw_get_event_log_len(struct ipw_priv *priv)
+{
+       /* length = 1st dword in log */
+       return ipw_read_reg32(priv, ipw_read32(priv, IPW_EVENT_LOG));
+}
+
+static void ipw_capture_event_log(struct ipw_priv *priv,
+                                 u32 log_len, struct ipw_event *log)
+{
+       u32 base;
+
+       if (log_len) {
+               base = ipw_read32(priv, IPW_EVENT_LOG);
+               ipw_read_indirect(priv, base + sizeof(base) + sizeof(u32),
+                                 (u8 *) log, sizeof(*log) * log_len);
+       }
+}
+
+static struct ipw_fw_error *ipw_alloc_error_log(struct ipw_priv *priv)
+{
+       struct ipw_fw_error *error;
+       u32 log_len = ipw_get_event_log_len(priv);
+       u32 base = ipw_read32(priv, IPW_ERROR_LOG);
+       u32 elem_len = ipw_read_reg32(priv, base);
+
+       error = kmalloc(sizeof(*error) +
+                       sizeof(*error->elem) * elem_len +
+                       sizeof(*error->log) * log_len, GFP_ATOMIC);
+       if (!error) {
+               IPW_ERROR("Memory allocation for firmware error log "
+                         "failed.\n");
+               return NULL;
+       }
+       error->jiffies = jiffies;
+       error->status = priv->status;
+       error->config = priv->config;
+       error->elem_len = elem_len;
+       error->log_len = log_len;
+       error->elem = (struct ipw_error_elem *)error->payload;
+       error->log = (struct ipw_event *)(error->elem + elem_len);
+
+       ipw_capture_event_log(priv, log_len, error->log);
+
+       if (elem_len)
+               ipw_read_indirect(priv, base + sizeof(base), (u8 *) error->elem,
+                                 sizeof(*error->elem) * elem_len);
+
+       return error;
+}
+
+static ssize_t show_event_log(struct device *d,
+                             struct device_attribute *attr, char *buf)
+{
+       struct ipw_priv *priv = dev_get_drvdata(d);
+       u32 log_len = ipw_get_event_log_len(priv);
+       u32 log_size;
+       struct ipw_event *log;
+       u32 len = 0, i;
+
+       /* not using min() because of its strict type checking */
+       log_size = PAGE_SIZE / sizeof(*log) > log_len ?
+                       sizeof(*log) * log_len : PAGE_SIZE;
+       log = kzalloc(log_size, GFP_KERNEL);
+       if (!log) {
+               IPW_ERROR("Unable to allocate memory for log\n");
+               return 0;
+       }
+       log_len = log_size / sizeof(*log);
+       ipw_capture_event_log(priv, log_len, log);
+
+       len += snprintf(buf + len, PAGE_SIZE - len, "%08X", log_len);
+       for (i = 0; i < log_len; i++)
+               len += snprintf(buf + len, PAGE_SIZE - len,
+                               "\n%08X%08X%08X",
+                               log[i].time, log[i].event, log[i].data);
+       len += snprintf(buf + len, PAGE_SIZE - len, "\n");
+       kfree(log);
+       return len;
+}
+
+static DEVICE_ATTR(event_log, S_IRUGO, show_event_log, NULL);
+
+static ssize_t show_error(struct device *d,
+                         struct device_attribute *attr, char *buf)
+{
+       struct ipw_priv *priv = dev_get_drvdata(d);
+       u32 len = 0, i;
+       if (!priv->error)
+               return 0;
+       len += snprintf(buf + len, PAGE_SIZE - len,
+                       "%08lX%08X%08X%08X",
+                       priv->error->jiffies,
+                       priv->error->status,
+                       priv->error->config, priv->error->elem_len);
+       for (i = 0; i < priv->error->elem_len; i++)
+               len += snprintf(buf + len, PAGE_SIZE - len,
+                               "\n%08X%08X%08X%08X%08X%08X%08X",
+                               priv->error->elem[i].time,
+                               priv->error->elem[i].desc,
+                               priv->error->elem[i].blink1,
+                               priv->error->elem[i].blink2,
+                               priv->error->elem[i].link1,
+                               priv->error->elem[i].link2,
+                               priv->error->elem[i].data);
+
+       len += snprintf(buf + len, PAGE_SIZE - len,
+                       "\n%08X", priv->error->log_len);
+       for (i = 0; i < priv->error->log_len; i++)
+               len += snprintf(buf + len, PAGE_SIZE - len,
+                               "\n%08X%08X%08X",
+                               priv->error->log[i].time,
+                               priv->error->log[i].event,
+                               priv->error->log[i].data);
+       len += snprintf(buf + len, PAGE_SIZE - len, "\n");
+       return len;
+}
+
+static ssize_t clear_error(struct device *d,
+                          struct device_attribute *attr,
+                          const char *buf, size_t count)
+{
+       struct ipw_priv *priv = dev_get_drvdata(d);
+
+       kfree(priv->error);
+       priv->error = NULL;
+       return count;
+}
+
+static DEVICE_ATTR(error, S_IRUGO | S_IWUSR, show_error, clear_error);
+
+static ssize_t show_cmd_log(struct device *d,
+                           struct device_attribute *attr, char *buf)
+{
+       struct ipw_priv *priv = dev_get_drvdata(d);
+       u32 len = 0, i;
+       if (!priv->cmdlog)
+               return 0;
+       for (i = (priv->cmdlog_pos + 1) % priv->cmdlog_len;
+            (i != priv->cmdlog_pos) && (PAGE_SIZE - len);
+            i = (i + 1) % priv->cmdlog_len) {
+               len +=
+                   snprintf(buf + len, PAGE_SIZE - len,
+                            "\n%08lX%08X%08X%08X\n", priv->cmdlog[i].jiffies,
+                            priv->cmdlog[i].retcode, priv->cmdlog[i].cmd.cmd,
+                            priv->cmdlog[i].cmd.len);
+               len +=
+                   snprintk_buf(buf + len, PAGE_SIZE - len,
+                                (u8 *) priv->cmdlog[i].cmd.param,
+                                priv->cmdlog[i].cmd.len);
+               len += snprintf(buf + len, PAGE_SIZE - len, "\n");
+       }
+       len += snprintf(buf + len, PAGE_SIZE - len, "\n");
+       return len;
+}
+
+static DEVICE_ATTR(cmd_log, S_IRUGO, show_cmd_log, NULL);
+
+#ifdef CONFIG_IPW2200_PROMISCUOUS
+static void ipw_prom_free(struct ipw_priv *priv);
+static int ipw_prom_alloc(struct ipw_priv *priv);
+static ssize_t store_rtap_iface(struct device *d,
+                        struct device_attribute *attr,
+                        const char *buf, size_t count)
+{
+       struct ipw_priv *priv = dev_get_drvdata(d);
+       int rc = 0;
+
+       if (count < 1)
+               return -EINVAL;
+
+       switch (buf[0]) {
+       case '0':
+               if (!rtap_iface)
+                       return count;
+
+               if (netif_running(priv->prom_net_dev)) {
+                       IPW_WARNING("Interface is up.  Cannot unregister.\n");
+                       return count;
+               }
+
+               ipw_prom_free(priv);
+               rtap_iface = 0;
+               break;
+
+       case '1':
+               if (rtap_iface)
+                       return count;
+
+               rc = ipw_prom_alloc(priv);
+               if (!rc)
+                       rtap_iface = 1;
+               break;
+
+       default:
+               return -EINVAL;
+       }
+
+       if (rc) {
+               IPW_ERROR("Failed to register promiscuous network "
+                         "device (error %d).\n", rc);
+       }
+
+       return count;
+}
+
+static ssize_t show_rtap_iface(struct device *d,
+                       struct device_attribute *attr,
+                       char *buf)
+{
+       struct ipw_priv *priv = dev_get_drvdata(d);
+       if (rtap_iface)
+               return sprintf(buf, "%s", priv->prom_net_dev->name);
+       else {
+               buf[0] = '-';
+               buf[1] = '1';
+               buf[2] = '\0';
+               return 3;
+       }
+}
+
+static DEVICE_ATTR(rtap_iface, S_IWUSR | S_IRUSR, show_rtap_iface,
+                  store_rtap_iface);
+
+static ssize_t store_rtap_filter(struct device *d,
+                        struct device_attribute *attr,
+                        const char *buf, size_t count)
+{
+       struct ipw_priv *priv = dev_get_drvdata(d);
+
+       if (!priv->prom_priv) {
+               IPW_ERROR("Attempting to set filter without "
+                         "rtap_iface enabled.\n");
+               return -EPERM;
+       }
+
+       priv->prom_priv->filter = simple_strtol(buf, NULL, 0);
+
+       IPW_DEBUG_INFO("Setting rtap filter to " BIT_FMT16 "\n",
+                      BIT_ARG16(priv->prom_priv->filter));
+
+       return count;
+}
+
+static ssize_t show_rtap_filter(struct device *d,
+                       struct device_attribute *attr,
+                       char *buf)
+{
+       struct ipw_priv *priv = dev_get_drvdata(d);
+       return sprintf(buf, "0x%04X",
+                      priv->prom_priv ? priv->prom_priv->filter : 0);
+}
+
+static DEVICE_ATTR(rtap_filter, S_IWUSR | S_IRUSR, show_rtap_filter,
+                  store_rtap_filter);
+#endif
+
 static ssize_t show_scan_age(struct device *d, struct device_attribute *attr,
                             char *buf)
 {
@@ -1025,9 +1446,7 @@ static ssize_t store_scan_age(struct device *d, struct device_attribute *attr,
                              const char *buf, size_t count)
 {
        struct ipw_priv *priv = dev_get_drvdata(d);
-#ifdef CONFIG_IPW_DEBUG
        struct net_device *dev = priv->net_dev;
-#endif
        char buffer[] = "00000000";
        unsigned long len =
            (sizeof(buffer) - 1) > count ? count : sizeof(buffer) - 1;
@@ -1119,34 +1538,6 @@ static ssize_t show_nic_type(struct device *d,
 
 static DEVICE_ATTR(nic_type, S_IRUGO, show_nic_type, NULL);
 
-static ssize_t dump_error_log(struct device *d,
-                             struct device_attribute *attr, const char *buf,
-                             size_t count)
-{
-       char *p = (char *)buf;
-
-       if (p[0] == '1')
-               ipw_dump_nic_error_log((struct ipw_priv *)d->driver_data);
-
-       return strnlen(buf, count);
-}
-
-static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
-
-static ssize_t dump_event_log(struct device *d,
-                             struct device_attribute *attr, const char *buf,
-                             size_t count)
-{
-       char *p = (char *)buf;
-
-       if (p[0] == '1')
-               ipw_dump_nic_event_log((struct ipw_priv *)d->driver_data);
-
-       return strnlen(buf, count);
-}
-
-static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
-
 static ssize_t show_ucode_version(struct device *d,
                                  struct device_attribute *attr, char *buf)
 {
@@ -1203,7 +1594,7 @@ static ssize_t show_command_event_reg(struct device *d,
        u32 reg = 0;
        struct ipw_priv *p = d->driver_data;
 
-       reg = ipw_read_reg32(p, CX2_INTERNAL_CMD_EVENT);
+       reg = ipw_read_reg32(p, IPW_INTERNAL_CMD_EVENT);
        return sprintf(buf, "0x%08x\n", reg);
 }
 static ssize_t store_command_event_reg(struct device *d,
@@ -1214,7 +1605,7 @@ static ssize_t store_command_event_reg(struct device *d,
        struct ipw_priv *p = d->driver_data;
 
        sscanf(buf, "%x", &reg);
-       ipw_write_reg32(p, CX2_INTERNAL_CMD_EVENT, reg);
+       ipw_write_reg32(p, IPW_INTERNAL_CMD_EVENT, reg);
        return strnlen(buf, count);
 }
 
@@ -1250,6 +1641,7 @@ static ssize_t show_indirect_dword(struct device *d,
 {
        u32 reg = 0;
        struct ipw_priv *priv = d->driver_data;
+
        if (priv->status & STATUS_INDIRECT_DWORD)
                reg = ipw_read_reg32(priv, priv->indirect_dword);
        else
@@ -1276,6 +1668,7 @@ static ssize_t show_indirect_byte(struct device *d,
 {
        u8 reg = 0;
        struct ipw_priv *priv = d->driver_data;
+
        if (priv->status & STATUS_INDIRECT_BYTE)
                reg = ipw_read_reg8(priv, priv->indirect_byte);
        else
@@ -1324,7 +1717,7 @@ static ssize_t store_direct_dword(struct device *d,
 static DEVICE_ATTR(direct_dword, S_IWUSR | S_IRUGO,
                   show_direct_dword, store_direct_dword);
 
-static inline int rf_kill_active(struct ipw_priv *priv)
+static int rf_kill_active(struct ipw_priv *priv)
 {
        if (0 == (ipw_read32(priv, 0x30) & 0x10000))
                priv->status |= STATUS_RF_KILL_HW;
@@ -1359,9 +1752,12 @@ static int ipw_radio_kill_sw(struct ipw_priv *priv, int disable_radio)
        if (disable_radio) {
                priv->status |= STATUS_RF_KILL_SW;
 
-               if (priv->workqueue)
+               if (priv->workqueue) {
                        cancel_delayed_work(&priv->request_scan);
-               wake_up_interruptible(&priv->wait_command_queue);
+                       cancel_delayed_work(&priv->request_direct_scan);
+                       cancel_delayed_work(&priv->request_passive_scan);
+                       cancel_delayed_work(&priv->scan_event);
+               }
                queue_work(priv->workqueue, &priv->down);
        } else {
                priv->status &= ~STATUS_RF_KILL_SW;
@@ -1371,7 +1767,7 @@ static int ipw_radio_kill_sw(struct ipw_priv *priv, int disable_radio)
                        /* Make sure the RF_KILL check timer is running */
                        cancel_delayed_work(&priv->rf_kill);
                        queue_delayed_work(priv->workqueue, &priv->rf_kill,
-                                          2 * HZ);
+                                          round_jiffies_relative(2 * HZ));
                } else
                        queue_work(priv->workqueue, &priv->up);
        }
@@ -1391,134 +1787,290 @@ static ssize_t store_rf_kill(struct device *d, struct device_attribute *attr,
 
 static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
 
-static void notify_wx_assoc_event(struct ipw_priv *priv)
+static ssize_t show_speed_scan(struct device *d, struct device_attribute *attr,
+                              char *buf)
 {
-       union iwreq_data wrqu;
-       wrqu.ap_addr.sa_family = ARPHRD_ETHER;
-       if (priv->status & STATUS_ASSOCIATED)
-               memcpy(wrqu.ap_addr.sa_data, priv->bssid, ETH_ALEN);
-       else
-               memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
-       wireless_send_event(priv->net_dev, SIOCGIWAP, &wrqu, NULL);
+       struct ipw_priv *priv = (struct ipw_priv *)d->driver_data;
+       int pos = 0, len = 0;
+       if (priv->config & CFG_SPEED_SCAN) {
+               while (priv->speed_scan[pos] != 0)
+                       len += sprintf(&buf[len], "%d ",
+                                      priv->speed_scan[pos++]);
+               return len + sprintf(&buf[len], "\n");
+       }
+
+       return sprintf(buf, "0\n");
 }
 
-static void ipw_irq_tasklet(struct ipw_priv *priv)
+static ssize_t store_speed_scan(struct device *d, struct device_attribute *attr,
+                               const char *buf, size_t count)
 {
-       u32 inta, inta_mask, handled = 0;
-       unsigned long flags;
+       struct ipw_priv *priv = (struct ipw_priv *)d->driver_data;
+       int channel, pos = 0;
+       const char *p = buf;
+
+       /* list of space separated channels to scan, optionally ending with 0 */
+       while ((channel = simple_strtol(p, NULL, 0))) {
+               if (pos == MAX_SPEED_SCAN - 1) {
+                       priv->speed_scan[pos] = 0;
+                       break;
+               }
+
+               if (ieee80211_is_valid_channel(priv->ieee, channel))
+                       priv->speed_scan[pos++] = channel;
+               else
+                       IPW_WARNING("Skipping invalid channel request: %d\n",
+                                   channel);
+               p = strchr(p, ' ');
+               if (!p)
+                       break;
+               while (*p == ' ' || *p == '\t')
+                       p++;
+       }
+
+       if (pos == 0)
+               priv->config &= ~CFG_SPEED_SCAN;
+       else {
+               priv->speed_scan_pos = 0;
+               priv->config |= CFG_SPEED_SCAN;
+       }
+
+       return count;
+}
+
+static DEVICE_ATTR(speed_scan, S_IWUSR | S_IRUGO, show_speed_scan,
+                  store_speed_scan);
+
+static ssize_t show_net_stats(struct device *d, struct device_attribute *attr,
+                             char *buf)
+{
+       struct ipw_priv *priv = (struct ipw_priv *)d->driver_data;
+       return sprintf(buf, "%c\n", (priv->config & CFG_NET_STATS) ? '1' : '0');
+}
+
+static ssize_t store_net_stats(struct device *d, struct device_attribute *attr,
+                              const char *buf, size_t count)
+{
+       struct ipw_priv *priv = (struct ipw_priv *)d->driver_data;
+       if (buf[0] == '1')
+               priv->config |= CFG_NET_STATS;
+       else
+               priv->config &= ~CFG_NET_STATS;
+
+       return count;
+}
+
+static DEVICE_ATTR(net_stats, S_IWUSR | S_IRUGO,
+                  show_net_stats, store_net_stats);
+
+static ssize_t show_channels(struct device *d,
+                            struct device_attribute *attr,
+                            char *buf)
+{
+       struct ipw_priv *priv = dev_get_drvdata(d);
+       const struct ieee80211_geo *geo = ieee80211_get_geo(priv->ieee);
+       int len = 0, i;
+
+       len = sprintf(&buf[len],
+                     "Displaying %d channels in 2.4Ghz band "
+                     "(802.11bg):\n", geo->bg_channels);
+
+       for (i = 0; i < geo->bg_channels; i++) {
+               len += sprintf(&buf[len], "%d: BSS%s%s, %s, Band %s.\n",
+                              geo->bg[i].channel,
+                              geo->bg[i].flags & IEEE80211_CH_RADAR_DETECT ?
+                              " (radar spectrum)" : "",
+                              ((geo->bg[i].flags & IEEE80211_CH_NO_IBSS) ||
+                               (geo->bg[i].flags & IEEE80211_CH_RADAR_DETECT))
+                              ? "" : ", IBSS",
+                              geo->bg[i].flags & IEEE80211_CH_PASSIVE_ONLY ?
+                              "passive only" : "active/passive",
+                              geo->bg[i].flags & IEEE80211_CH_B_ONLY ?
+                              "B" : "B/G");
+       }
+
+       len += sprintf(&buf[len],
+                      "Displaying %d channels in 5.2Ghz band "
+                      "(802.11a):\n", geo->a_channels);
+       for (i = 0; i < geo->a_channels; i++) {
+               len += sprintf(&buf[len], "%d: BSS%s%s, %s.\n",
+                              geo->a[i].channel,
+                              geo->a[i].flags & IEEE80211_CH_RADAR_DETECT ?
+                              " (radar spectrum)" : "",
+                              ((geo->a[i].flags & IEEE80211_CH_NO_IBSS) ||
+                               (geo->a[i].flags & IEEE80211_CH_RADAR_DETECT))
+                              ? "" : ", IBSS",
+                              geo->a[i].flags & IEEE80211_CH_PASSIVE_ONLY ?
+                              "passive only" : "active/passive");
+       }
+
+       return len;
+}
+
+static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
+
+static void notify_wx_assoc_event(struct ipw_priv *priv)
+{
+       union iwreq_data wrqu;
+       wrqu.ap_addr.sa_family = ARPHRD_ETHER;
+       if (priv->status & STATUS_ASSOCIATED)
+               memcpy(wrqu.ap_addr.sa_data, priv->bssid, ETH_ALEN);
+       else
+               memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
+       wireless_send_event(priv->net_dev, SIOCGIWAP, &wrqu, NULL);
+}
+
+static void ipw_irq_tasklet(struct ipw_priv *priv)
+{
+       u32 inta, inta_mask, handled = 0;
+       unsigned long flags;
        int rc = 0;
 
-       spin_lock_irqsave(&priv->lock, flags);
+       spin_lock_irqsave(&priv->irq_lock, flags);
 
-       inta = ipw_read32(priv, CX2_INTA_RW);
-       inta_mask = ipw_read32(priv, CX2_INTA_MASK_R);
-       inta &= (CX2_INTA_MASK_ALL & inta_mask);
+       inta = ipw_read32(priv, IPW_INTA_RW);
+       inta_mask = ipw_read32(priv, IPW_INTA_MASK_R);
+       inta &= (IPW_INTA_MASK_ALL & inta_mask);
 
        /* Add any cached INTA values that need to be handled */
        inta |= priv->isr_inta;
 
+       spin_unlock_irqrestore(&priv->irq_lock, flags);
+
+       spin_lock_irqsave(&priv->lock, flags);
+
        /* handle all the justifications for the interrupt */
-       if (inta & CX2_INTA_BIT_RX_TRANSFER) {
+       if (inta & IPW_INTA_BIT_RX_TRANSFER) {
                ipw_rx(priv);
-               handled |= CX2_INTA_BIT_RX_TRANSFER;
+               handled |= IPW_INTA_BIT_RX_TRANSFER;
        }
 
-       if (inta & CX2_INTA_BIT_TX_CMD_QUEUE) {
+       if (inta & IPW_INTA_BIT_TX_CMD_QUEUE) {
                IPW_DEBUG_HC("Command completed.\n");
                rc = ipw_queue_tx_reclaim(priv, &priv->txq_cmd, -1);
                priv->status &= ~STATUS_HCMD_ACTIVE;
                wake_up_interruptible(&priv->wait_command_queue);
-               handled |= CX2_INTA_BIT_TX_CMD_QUEUE;
+               handled |= IPW_INTA_BIT_TX_CMD_QUEUE;
        }
 
-       if (inta & CX2_INTA_BIT_TX_QUEUE_1) {
+       if (inta & IPW_INTA_BIT_TX_QUEUE_1) {
                IPW_DEBUG_TX("TX_QUEUE_1\n");
                rc = ipw_queue_tx_reclaim(priv, &priv->txq[0], 0);
-               handled |= CX2_INTA_BIT_TX_QUEUE_1;
+               handled |= IPW_INTA_BIT_TX_QUEUE_1;
        }
 
-       if (inta & CX2_INTA_BIT_TX_QUEUE_2) {
+       if (inta & IPW_INTA_BIT_TX_QUEUE_2) {
                IPW_DEBUG_TX("TX_QUEUE_2\n");
                rc = ipw_queue_tx_reclaim(priv, &priv->txq[1], 1);
-               handled |= CX2_INTA_BIT_TX_QUEUE_2;
+               handled |= IPW_INTA_BIT_TX_QUEUE_2;
        }
 
-       if (inta & CX2_INTA_BIT_TX_QUEUE_3) {
+       if (inta & IPW_INTA_BIT_TX_QUEUE_3) {
                IPW_DEBUG_TX("TX_QUEUE_3\n");
                rc = ipw_queue_tx_reclaim(priv, &priv->txq[2], 2);
-               handled |= CX2_INTA_BIT_TX_QUEUE_3;
+               handled |= IPW_INTA_BIT_TX_QUEUE_3;
        }
 
-       if (inta & CX2_INTA_BIT_TX_QUEUE_4) {
+       if (inta & IPW_INTA_BIT_TX_QUEUE_4) {
                IPW_DEBUG_TX("TX_QUEUE_4\n");
                rc = ipw_queue_tx_reclaim(priv, &priv->txq[3], 3);
-               handled |= CX2_INTA_BIT_TX_QUEUE_4;
+               handled |= IPW_INTA_BIT_TX_QUEUE_4;
        }
 
-       if (inta & CX2_INTA_BIT_STATUS_CHANGE) {
+       if (inta & IPW_INTA_BIT_STATUS_CHANGE) {
                IPW_WARNING("STATUS_CHANGE\n");
-               handled |= CX2_INTA_BIT_STATUS_CHANGE;
+               handled |= IPW_INTA_BIT_STATUS_CHANGE;
        }
 
-       if (inta & CX2_INTA_BIT_BEACON_PERIOD_EXPIRED) {
+       if (inta & IPW_INTA_BIT_BEACON_PERIOD_EXPIRED) {
                IPW_WARNING("TX_PERIOD_EXPIRED\n");
-               handled |= CX2_INTA_BIT_BEACON_PERIOD_EXPIRED;
+               handled |= IPW_INTA_BIT_BEACON_PERIOD_EXPIRED;
        }
 
-       if (inta & CX2_INTA_BIT_SLAVE_MODE_HOST_CMD_DONE) {
+       if (inta & IPW_INTA_BIT_SLAVE_MODE_HOST_CMD_DONE) {
                IPW_WARNING("HOST_CMD_DONE\n");
-               handled |= CX2_INTA_BIT_SLAVE_MODE_HOST_CMD_DONE;
+               handled |= IPW_INTA_BIT_SLAVE_MODE_HOST_CMD_DONE;
        }
 
-       if (inta & CX2_INTA_BIT_FW_INITIALIZATION_DONE) {
+       if (inta & IPW_INTA_BIT_FW_INITIALIZATION_DONE) {
                IPW_WARNING("FW_INITIALIZATION_DONE\n");
-               handled |= CX2_INTA_BIT_FW_INITIALIZATION_DONE;
+               handled |= IPW_INTA_BIT_FW_INITIALIZATION_DONE;
        }
 
-       if (inta & CX2_INTA_BIT_FW_CARD_DISABLE_PHY_OFF_DONE) {
+       if (inta & IPW_INTA_BIT_FW_CARD_DISABLE_PHY_OFF_DONE) {
                IPW_WARNING("PHY_OFF_DONE\n");
-               handled |= CX2_INTA_BIT_FW_CARD_DISABLE_PHY_OFF_DONE;
+               handled |= IPW_INTA_BIT_FW_CARD_DISABLE_PHY_OFF_DONE;
        }
 
-       if (inta & CX2_INTA_BIT_RF_KILL_DONE) {
+       if (inta & IPW_INTA_BIT_RF_KILL_DONE) {
                IPW_DEBUG_RF_KILL("RF_KILL_DONE\n");
                priv->status |= STATUS_RF_KILL_HW;
                wake_up_interruptible(&priv->wait_command_queue);
                priv->status &= ~(STATUS_ASSOCIATED | STATUS_ASSOCIATING);
                cancel_delayed_work(&priv->request_scan);
+               cancel_delayed_work(&priv->request_direct_scan);
+               cancel_delayed_work(&priv->request_passive_scan);
+               cancel_delayed_work(&priv->scan_event);
                schedule_work(&priv->link_down);
                queue_delayed_work(priv->workqueue, &priv->rf_kill, 2 * HZ);
-               handled |= CX2_INTA_BIT_RF_KILL_DONE;
-       }
+               handled |= IPW_INTA_BIT_RF_KILL_DONE;
+       }
+
+       if (inta & IPW_INTA_BIT_FATAL_ERROR) {
+               IPW_WARNING("Firmware error detected.  Restarting.\n");
+               if (priv->error) {
+                       IPW_DEBUG_FW("Sysfs 'error' log already exists.\n");
+                       if (ipw_debug_level & IPW_DL_FW_ERRORS) {
+                               struct ipw_fw_error *error =
+                                   ipw_alloc_error_log(priv);
+                               ipw_dump_error_log(priv, error);
+                               kfree(error);
+                       }
+               } else {
+                       priv->error = ipw_alloc_error_log(priv);
+                       if (priv->error)
+                               IPW_DEBUG_FW("Sysfs 'error' log captured.\n");
+                       else
+                               IPW_DEBUG_FW("Error allocating sysfs 'error' "
+                                            "log.\n");
+                       if (ipw_debug_level & IPW_DL_FW_ERRORS)
+                               ipw_dump_error_log(priv, priv->error);
+               }
 
-       if (inta & CX2_INTA_BIT_FATAL_ERROR) {
-               IPW_ERROR("Firmware error detected.  Restarting.\n");
-#ifdef CONFIG_IPW_DEBUG
-               if (ipw_debug_level & IPW_DL_FW_ERRORS) {
-                       ipw_dump_nic_error_log(priv);
-                       ipw_dump_nic_event_log(priv);
+               /* XXX: If hardware encryption is for WPA/WPA2,
+                * we have to notify the supplicant. */
+               if (priv->ieee->sec.encrypt) {
+                       priv->status &= ~STATUS_ASSOCIATED;
+                       notify_wx_assoc_event(priv);
                }
-#endif
+
+               /* Keep the restart process from trying to send host
+                * commands by clearing the INIT status bit */
+               priv->status &= ~STATUS_INIT;
+
+               /* Cancel currently queued command. */
+               priv->status &= ~STATUS_HCMD_ACTIVE;
+               wake_up_interruptible(&priv->wait_command_queue);
+
                queue_work(priv->workqueue, &priv->adapter_restart);
-               handled |= CX2_INTA_BIT_FATAL_ERROR;
+               handled |= IPW_INTA_BIT_FATAL_ERROR;
        }
 
-       if (inta & CX2_INTA_BIT_PARITY_ERROR) {
+       if (inta & IPW_INTA_BIT_PARITY_ERROR) {
                IPW_ERROR("Parity error\n");
-               handled |= CX2_INTA_BIT_PARITY_ERROR;
+               handled |= IPW_INTA_BIT_PARITY_ERROR;
        }
 
        if (handled != inta) {
                IPW_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
        }
 
+       spin_unlock_irqrestore(&priv->lock, flags);
+
        /* enable all interrupts */
        ipw_enable_interrupts(priv);
-
-       spin_unlock_irqrestore(&priv->lock, flags);
 }
 
-#ifdef CONFIG_IPW_DEBUG
 #define IPW_CMD(x) case IPW_CMD_ ## x : return #x
 static char *get_cmd_string(u8 cmd)
 {
@@ -1577,32 +2129,51 @@ static char *get_cmd_string(u8 cmd)
                return "UNKNOWN";
        }
 }
-#endif
 
 #define HOST_COMPLETE_TIMEOUT HZ
-static int ipw_send_cmd(struct ipw_priv *priv, struct host_cmd *cmd)
+
+static int __ipw_send_cmd(struct ipw_priv *priv, struct host_cmd *cmd)
 {
        int rc = 0;
        unsigned long flags;
 
        spin_lock_irqsave(&priv->lock, flags);
        if (priv->status & STATUS_HCMD_ACTIVE) {
-               IPW_ERROR("Already sending a command\n");
+               IPW_ERROR("Failed to send %s: Already sending a command.\n",
+                         get_cmd_string(cmd->cmd));
                spin_unlock_irqrestore(&priv->lock, flags);
-               return -1;
+               return -EAGAIN;
        }
 
        priv->status |= STATUS_HCMD_ACTIVE;
 
-       IPW_DEBUG_HC("Sending %s command (#%d), %d bytes\n",
-                    get_cmd_string(cmd->cmd), cmd->cmd, cmd->len);
-       printk_buf(IPW_DL_HOST_COMMAND, (u8 *) cmd->param, cmd->len);
+       if (priv->cmdlog) {
+               priv->cmdlog[priv->cmdlog_pos].jiffies = jiffies;
+               priv->cmdlog[priv->cmdlog_pos].cmd.cmd = cmd->cmd;
+               priv->cmdlog[priv->cmdlog_pos].cmd.len = cmd->len;
+               memcpy(priv->cmdlog[priv->cmdlog_pos].cmd.param, cmd->param,
+                      cmd->len);
+               priv->cmdlog[priv->cmdlog_pos].retcode = -1;
+       }
+
+       IPW_DEBUG_HC("%s command (#%d) %d bytes: 0x%08X\n",
+                    get_cmd_string(cmd->cmd), cmd->cmd, cmd->len,
+                    priv->status);
+
+#ifndef DEBUG_CMD_WEP_KEY
+       if (cmd->cmd == IPW_CMD_WEP_KEY)
+               IPW_DEBUG_HC("WEP_KEY command masked out for secure.\n");
+       else
+#endif
+               printk_buf(IPW_DL_HOST_COMMAND, (u8 *) cmd->param, cmd->len);
 
-       rc = ipw_queue_tx_hcmd(priv, cmd->cmd, &cmd->param, cmd->len, 0);
+       rc = ipw_queue_tx_hcmd(priv, cmd->cmd, cmd->param, cmd->len, 0);
        if (rc) {
                priv->status &= ~STATUS_HCMD_ACTIVE;
+               IPW_ERROR("Failed to send %s: Reason %d\n",
+                         get_cmd_string(cmd->cmd), rc);
                spin_unlock_irqrestore(&priv->lock, flags);
-               return rc;
+               goto exit;
        }
        spin_unlock_irqrestore(&priv->lock, flags);
 
@@ -1613,110 +2184,92 @@ static int ipw_send_cmd(struct ipw_priv *priv, struct host_cmd *cmd)
        if (rc == 0) {
                spin_lock_irqsave(&priv->lock, flags);
                if (priv->status & STATUS_HCMD_ACTIVE) {
-                       IPW_DEBUG_INFO("Command completion failed out after "
-                                      "%dms.\n",
-                                      1000 * (HOST_COMPLETE_TIMEOUT / HZ));
+                       IPW_ERROR("Failed to send %s: Command timed out.\n",
+                                 get_cmd_string(cmd->cmd));
                        priv->status &= ~STATUS_HCMD_ACTIVE;
                        spin_unlock_irqrestore(&priv->lock, flags);
-                       return -EIO;
+                       rc = -EIO;
+                       goto exit;
                }
                spin_unlock_irqrestore(&priv->lock, flags);
-       }
+       } else
+               rc = 0;
 
-       if (priv->status & STATUS_RF_KILL_MASK) {
-               IPW_DEBUG_INFO("Command aborted due to RF Kill Switch\n");
-               return -EIO;
+       if (priv->status & STATUS_RF_KILL_HW) {
+               IPW_ERROR("Failed to send %s: Aborted due to RF kill switch.\n",
+                         get_cmd_string(cmd->cmd));
+               rc = -EIO;
+               goto exit;
        }
 
-       return 0;
+      exit:
+       if (priv->cmdlog) {
+               priv->cmdlog[priv->cmdlog_pos++].retcode = rc;
+               priv->cmdlog_pos %= priv->cmdlog_len;
+       }
+       return rc;
 }
 
-static int ipw_send_host_complete(struct ipw_priv *priv)
+static int ipw_send_cmd_simple(struct ipw_priv *priv, u8 command)
 {
        struct host_cmd cmd = {
-               .cmd = IPW_CMD_HOST_COMPLETE,
-               .len = 0
+               .cmd = command,
        };
 
-       if (!priv) {
-               IPW_ERROR("Invalid args\n");
-               return -1;
-       }
-
-       if (ipw_send_cmd(priv, &cmd)) {
-               IPW_ERROR("failed to send HOST_COMPLETE command\n");
-               return -1;
-       }
-
-       return 0;
+       return __ipw_send_cmd(priv, &cmd);
 }
 
-static int ipw_send_system_config(struct ipw_priv *priv,
-                                 struct ipw_sys_config *config)
+static int ipw_send_cmd_pdu(struct ipw_priv *priv, u8 command, u8 len,
+                           void *data)
 {
        struct host_cmd cmd = {
-               .cmd = IPW_CMD_SYSTEM_CONFIG,
-               .len = sizeof(*config)
+               .cmd = command,
+               .len = len,
+               .param = data,
        };
 
-       if (!priv || !config) {
+       return __ipw_send_cmd(priv, &cmd);
+}
+
+static int ipw_send_host_complete(struct ipw_priv *priv)
+{
+       if (!priv) {
                IPW_ERROR("Invalid args\n");
                return -1;
        }
 
-       memcpy(&cmd.param, config, sizeof(*config));
-       if (ipw_send_cmd(priv, &cmd)) {
-               IPW_ERROR("failed to send SYSTEM_CONFIG command\n");
-               return -1;
-       }
+       return ipw_send_cmd_simple(priv, IPW_CMD_HOST_COMPLETE);
+}
 
-       return 0;
+static int ipw_send_system_config(struct ipw_priv *priv)
+{
+       return ipw_send_cmd_pdu(priv, IPW_CMD_SYSTEM_CONFIG,
+                               sizeof(priv->sys_config),
+                               &priv->sys_config);
 }
 
 static int ipw_send_ssid(struct ipw_priv *priv, u8 * ssid, int len)
 {
-       struct host_cmd cmd = {
-               .cmd = IPW_CMD_SSID,
-               .len = min(len, IW_ESSID_MAX_SIZE)
-       };
-
        if (!priv || !ssid) {
                IPW_ERROR("Invalid args\n");
                return -1;
        }
 
-       memcpy(&cmd.param, ssid, cmd.len);
-       if (ipw_send_cmd(priv, &cmd)) {
-               IPW_ERROR("failed to send SSID command\n");
-               return -1;
-       }
-
-       return 0;
+       return ipw_send_cmd_pdu(priv, IPW_CMD_SSID, min(len, IW_ESSID_MAX_SIZE),
+                               ssid);
 }
 
 static int ipw_send_adapter_address(struct ipw_priv *priv, u8 * mac)
 {
-       struct host_cmd cmd = {
-               .cmd = IPW_CMD_ADAPTER_ADDRESS,
-               .len = ETH_ALEN
-       };
-
        if (!priv || !mac) {
                IPW_ERROR("Invalid args\n");
                return -1;
        }
 
-       IPW_DEBUG_INFO("%s: Setting MAC to " MAC_FMT "\n",
-                      priv->net_dev->name, MAC_ARG(mac));
-
-       memcpy(&cmd.param, mac, ETH_ALEN);
-
-       if (ipw_send_cmd(priv, &cmd)) {
-               IPW_ERROR("failed to send ADAPTER_ADDRESS command\n");
-               return -1;
-       }
+       IPW_DEBUG_INFO("%s: Setting MAC to %s\n",
+                      priv->net_dev->name, print_mac(mac, mac));
 
-       return 0;
+       return ipw_send_cmd_pdu(priv, IPW_CMD_ADAPTER_ADDRESS, ETH_ALEN, mac);
 }
 
 /*
@@ -1732,18 +2285,24 @@ static void ipw_adapter_restart(void *adapter)
                return;
 
        ipw_down(priv);
+
+       if (priv->assoc_network &&
+           (priv->assoc_network->capability & WLAN_CAPABILITY_IBSS))
+               ipw_remove_current_network(priv);
+
        if (ipw_up(priv)) {
                IPW_ERROR("Failed to up device\n");
                return;
        }
 }
 
-static void ipw_bg_adapter_restart(void *data)
+static void ipw_bg_adapter_restart(struct work_struct *work)
 {
-       struct ipw_priv *priv = data;
-       down(&priv->sem);
-       ipw_adapter_restart(data);
-       up(&priv->sem);
+       struct ipw_priv *priv =
+               container_of(work, struct ipw_priv, adapter_restart);
+       mutex_lock(&priv->mutex);
+       ipw_adapter_restart(priv);
+       mutex_unlock(&priv->mutex);
 }
 
 #define IPW_SCAN_CHECK_WATCHDOG (5 * HZ)
@@ -1753,213 +2312,155 @@ static void ipw_scan_check(void *data)
        struct ipw_priv *priv = data;
        if (priv->status & (STATUS_SCANNING | STATUS_SCAN_ABORTING)) {
                IPW_DEBUG_SCAN("Scan completion watchdog resetting "
-                              "adapter (%dms).\n",
-                              IPW_SCAN_CHECK_WATCHDOG / 100);
+                              "adapter after (%dms).\n",
+                              jiffies_to_msecs(IPW_SCAN_CHECK_WATCHDOG));
                queue_work(priv->workqueue, &priv->adapter_restart);
        }
 }
 
-static void ipw_bg_scan_check(void *data)
+static void ipw_bg_scan_check(struct work_struct *work)
 {
-       struct ipw_priv *priv = data;
-       down(&priv->sem);
-       ipw_scan_check(data);
-       up(&priv->sem);
+       struct ipw_priv *priv =
+               container_of(work, struct ipw_priv, scan_check.work);
+       mutex_lock(&priv->mutex);
+       ipw_scan_check(priv);
+       mutex_unlock(&priv->mutex);
 }
 
 static int ipw_send_scan_request_ext(struct ipw_priv *priv,
                                     struct ipw_scan_request_ext *request)
 {
-       struct host_cmd cmd = {
-               .cmd = IPW_CMD_SCAN_REQUEST_EXT,
-               .len = sizeof(*request)
-       };
-
-       if (!priv || !request) {
-               IPW_ERROR("Invalid args\n");
-               return -1;
-       }
-
-       memcpy(&cmd.param, request, sizeof(*request));
-       if (ipw_send_cmd(priv, &cmd)) {
-               IPW_ERROR("failed to send SCAN_REQUEST_EXT command\n");
-               return -1;
-       }
-
-       queue_delayed_work(priv->workqueue, &priv->scan_check,
-                          IPW_SCAN_CHECK_WATCHDOG);
-       return 0;
+       return ipw_send_cmd_pdu(priv, IPW_CMD_SCAN_REQUEST_EXT,
+                               sizeof(*request), request);
 }
 
 static int ipw_send_scan_abort(struct ipw_priv *priv)
 {
-       struct host_cmd cmd = {
-               .cmd = IPW_CMD_SCAN_ABORT,
-               .len = 0
-       };
-
        if (!priv) {
                IPW_ERROR("Invalid args\n");
                return -1;
        }
 
-       if (ipw_send_cmd(priv, &cmd)) {
-               IPW_ERROR("failed to send SCAN_ABORT command\n");
-               return -1;
-       }
-
-       return 0;
+       return ipw_send_cmd_simple(priv, IPW_CMD_SCAN_ABORT);
 }
 
 static int ipw_set_sensitivity(struct ipw_priv *priv, u16 sens)
 {
-       struct host_cmd cmd = {
-               .cmd = IPW_CMD_SENSITIVITY_CALIB,
-               .len = sizeof(struct ipw_sensitivity_calib)
+       struct ipw_sensitivity_calib calib = {
+               .beacon_rssi_raw = cpu_to_le16(sens),
        };
-       struct ipw_sensitivity_calib *calib = (struct ipw_sensitivity_calib *)
-           &cmd.param;
-       calib->beacon_rssi_raw = sens;
-       if (ipw_send_cmd(priv, &cmd)) {
-               IPW_ERROR("failed to send SENSITIVITY CALIB command\n");
-               return -1;
-       }
 
-       return 0;
+       return ipw_send_cmd_pdu(priv, IPW_CMD_SENSITIVITY_CALIB, sizeof(calib),
+                               &calib);
 }
 
 static int ipw_send_associate(struct ipw_priv *priv,
                              struct ipw_associate *associate)
 {
-       struct host_cmd cmd = {
-               .cmd = IPW_CMD_ASSOCIATE,
-               .len = sizeof(*associate)
-       };
-
-       struct ipw_associate tmp_associate;
-       memcpy(&tmp_associate, associate, sizeof(*associate));
-       tmp_associate.policy_support =
-           cpu_to_le16(tmp_associate.policy_support);
-       tmp_associate.assoc_tsf_msw = cpu_to_le32(tmp_associate.assoc_tsf_msw);
-       tmp_associate.assoc_tsf_lsw = cpu_to_le32(tmp_associate.assoc_tsf_lsw);
-       tmp_associate.capability = cpu_to_le16(tmp_associate.capability);
-       tmp_associate.listen_interval =
-           cpu_to_le16(tmp_associate.listen_interval);
-       tmp_associate.beacon_interval =
-           cpu_to_le16(tmp_associate.beacon_interval);
-       tmp_associate.atim_window = cpu_to_le16(tmp_associate.atim_window);
-
        if (!priv || !associate) {
                IPW_ERROR("Invalid args\n");
                return -1;
        }
 
-       memcpy(&cmd.param, &tmp_associate, sizeof(*associate));
-       if (ipw_send_cmd(priv, &cmd)) {
-               IPW_ERROR("failed to send ASSOCIATE command\n");
-               return -1;
-       }
-
-       return 0;
+       return ipw_send_cmd_pdu(priv, IPW_CMD_ASSOCIATE, sizeof(*associate),
+                               associate);
 }
 
 static int ipw_send_supported_rates(struct ipw_priv *priv,
                                    struct ipw_supported_rates *rates)
 {
-       struct host_cmd cmd = {
-               .cmd = IPW_CMD_SUPPORTED_RATES,
-               .len = sizeof(*rates)
-       };
-
        if (!priv || !rates) {
                IPW_ERROR("Invalid args\n");
                return -1;
        }
 
-       memcpy(&cmd.param, rates, sizeof(*rates));
-       if (ipw_send_cmd(priv, &cmd)) {
-               IPW_ERROR("failed to send SUPPORTED_RATES command\n");
-               return -1;
-       }
-
-       return 0;
+       return ipw_send_cmd_pdu(priv, IPW_CMD_SUPPORTED_RATES, sizeof(*rates),
+                               rates);
 }
 
 static int ipw_set_random_seed(struct ipw_priv *priv)
 {
-       struct host_cmd cmd = {
-               .cmd = IPW_CMD_SEED_NUMBER,
-               .len = sizeof(u32)
-       };
+       u32 val;
 
        if (!priv) {
                IPW_ERROR("Invalid args\n");
                return -1;
        }
 
-       get_random_bytes(&cmd.param, sizeof(u32));
-
-       if (ipw_send_cmd(priv, &cmd)) {
-               IPW_ERROR("failed to send SEED_NUMBER command\n");
-               return -1;
-       }
+       get_random_bytes(&val, sizeof(val));
 
-       return 0;
+       return ipw_send_cmd_pdu(priv, IPW_CMD_SEED_NUMBER, sizeof(val), &val);
 }
 
-#if 0
 static int ipw_send_card_disable(struct ipw_priv *priv, u32 phy_off)
 {
-       struct host_cmd cmd = {
-               .cmd = IPW_CMD_CARD_DISABLE,
-               .len = sizeof(u32)
-       };
-
+       __le32 v = cpu_to_le32(phy_off);
        if (!priv) {
                IPW_ERROR("Invalid args\n");
                return -1;
        }
 
-       *((u32 *) & cmd.param) = phy_off;
-
-       if (ipw_send_cmd(priv, &cmd)) {
-               IPW_ERROR("failed to send CARD_DISABLE command\n");
-               return -1;
-       }
-
-       return 0;
+       return ipw_send_cmd_pdu(priv, IPW_CMD_CARD_DISABLE, sizeof(v), &v);
 }
-#endif
 
 static int ipw_send_tx_power(struct ipw_priv *priv, struct ipw_tx_power *power)
 {
-       struct host_cmd cmd = {
-               .cmd = IPW_CMD_TX_POWER,
-               .len = sizeof(*power)
-       };
-
        if (!priv || !power) {
                IPW_ERROR("Invalid args\n");
                return -1;
        }
 
-       memcpy(&cmd.param, power, sizeof(*power));
-       if (ipw_send_cmd(priv, &cmd)) {
-               IPW_ERROR("failed to send TX_POWER command\n");
-               return -1;
+       return ipw_send_cmd_pdu(priv, IPW_CMD_TX_POWER, sizeof(*power), power);
+}
+
+static int ipw_set_tx_power(struct ipw_priv *priv)
+{
+       const struct ieee80211_geo *geo = ieee80211_get_geo(priv->ieee);
+       struct ipw_tx_power tx_power;
+       s8 max_power;
+       int i;
+
+       memset(&tx_power, 0, sizeof(tx_power));
+
+       /* configure device for 'G' band */
+       tx_power.ieee_mode = IPW_G_MODE;
+       tx_power.num_channels = geo->bg_channels;
+       for (i = 0; i < geo->bg_channels; i++) {
+               max_power = geo->bg[i].max_power;
+               tx_power.channels_tx_power[i].channel_number =
+                   geo->bg[i].channel;
+               tx_power.channels_tx_power[i].tx_power = max_power ?
+                   min(max_power, priv->tx_power) : priv->tx_power;
        }
+       if (ipw_send_tx_power(priv, &tx_power))
+               return -EIO;
+
+       /* configure device to also handle 'B' band */
+       tx_power.ieee_mode = IPW_B_MODE;
+       if (ipw_send_tx_power(priv, &tx_power))
+               return -EIO;
 
+       /* configure device to also handle 'A' band */
+       if (priv->ieee->abg_true) {
+               tx_power.ieee_mode = IPW_A_MODE;
+               tx_power.num_channels = geo->a_channels;
+               for (i = 0; i < tx_power.num_channels; i++) {
+                       max_power = geo->a[i].max_power;
+                       tx_power.channels_tx_power[i].channel_number =
+                           geo->a[i].channel;
+                       tx_power.channels_tx_power[i].tx_power = max_power ?
+                           min(max_power, priv->tx_power) : priv->tx_power;
+               }
+               if (ipw_send_tx_power(priv, &tx_power))
+                       return -EIO;
+       }
        return 0;
 }
 
 static int ipw_send_rts_threshold(struct ipw_priv *priv, u16 rts)
 {
        struct ipw_rts_threshold rts_threshold = {
-               .rts_threshold = rts,
-       };
-       struct host_cmd cmd = {
-               .cmd = IPW_CMD_RTS_THRESHOLD,
-               .len = sizeof(rts_threshold)
+               .rts_threshold = cpu_to_le16(rts),
        };
 
        if (!priv) {
@@ -1967,23 +2468,14 @@ static int ipw_send_rts_threshold(struct ipw_priv *priv, u16 rts)
                return -1;
        }
 
-       memcpy(&cmd.param, &rts_threshold, sizeof(rts_threshold));
-       if (ipw_send_cmd(priv, &cmd)) {
-               IPW_ERROR("failed to send RTS_THRESHOLD command\n");
-               return -1;
-       }
-
-       return 0;
+       return ipw_send_cmd_pdu(priv, IPW_CMD_RTS_THRESHOLD,
+                               sizeof(rts_threshold), &rts_threshold);
 }
 
 static int ipw_send_frag_threshold(struct ipw_priv *priv, u16 frag)
 {
        struct ipw_frag_threshold frag_threshold = {
-               .frag_threshold = frag,
-       };
-       struct host_cmd cmd = {
-               .cmd = IPW_CMD_FRAG_THRESHOLD,
-               .len = sizeof(frag_threshold)
+               .frag_threshold = cpu_to_le16(frag),
        };
 
        if (!priv) {
@@ -1991,22 +2483,13 @@ static int ipw_send_frag_threshold(struct ipw_priv *priv, u16 frag)
                return -1;
        }
 
-       memcpy(&cmd.param, &frag_threshold, sizeof(frag_threshold));
-       if (ipw_send_cmd(priv, &cmd)) {
-               IPW_ERROR("failed to send FRAG_THRESHOLD command\n");
-               return -1;
-       }
-
-       return 0;
+       return ipw_send_cmd_pdu(priv, IPW_CMD_FRAG_THRESHOLD,
+                               sizeof(frag_threshold), &frag_threshold);
 }
 
 static int ipw_send_power_mode(struct ipw_priv *priv, u32 mode)
 {
-       struct host_cmd cmd = {
-               .cmd = IPW_CMD_POWER_MODE,
-               .len = sizeof(u32)
-       };
-       u32 *param = (u32 *) (&cmd.param);
+       __le32 param;
 
        if (!priv) {
                IPW_ERROR("Invalid args\n");
@@ -2017,22 +2500,34 @@ static int ipw_send_power_mode(struct ipw_priv *priv, u32 mode)
         * level */
        switch (mode) {
        case IPW_POWER_BATTERY:
-               *param = IPW_POWER_INDEX_3;
+               param = cpu_to_le32(IPW_POWER_INDEX_3);
                break;
        case IPW_POWER_AC:
-               *param = IPW_POWER_MODE_CAM;
+               param = cpu_to_le32(IPW_POWER_MODE_CAM);
                break;
        default:
-               *param = mode;
+               param = cpu_to_le32(mode);
                break;
        }
 
-       if (ipw_send_cmd(priv, &cmd)) {
-               IPW_ERROR("failed to send POWER_MODE command\n");
+       return ipw_send_cmd_pdu(priv, IPW_CMD_POWER_MODE, sizeof(param),
+                               &param);
+}
+
+static int ipw_send_retry_limit(struct ipw_priv *priv, u8 slimit, u8 llimit)
+{
+       struct ipw_retry_limit retry_limit = {
+               .short_retry_limit = slimit,
+               .long_retry_limit = llimit
+       };
+
+       if (!priv) {
+               IPW_ERROR("Invalid args\n");
                return -1;
        }
 
-       return 0;
+       return ipw_send_cmd_pdu(priv, IPW_CMD_RETRY_LIMIT, sizeof(retry_limit),
+                               &retry_limit);
 }
 
 /*
@@ -2064,7 +2559,7 @@ static inline void eeprom_write_reg(struct ipw_priv *p, u32 data)
 }
 
 /* perform a chip select operation */
-static inline void eeprom_cs(struct ipw_priv *priv)
+static void eeprom_cs(struct ipw_priv *priv)
 {
        eeprom_write_reg(priv, 0);
        eeprom_write_reg(priv, EEPROM_BIT_CS);
@@ -2073,7 +2568,7 @@ static inline void eeprom_cs(struct ipw_priv *priv)
 }
 
 /* perform a chip select operation */
-static inline void eeprom_disable_cs(struct ipw_priv *priv)
+static void eeprom_disable_cs(struct ipw_priv *priv)
 {
        eeprom_write_reg(priv, EEPROM_BIT_CS);
        eeprom_write_reg(priv, 0);
@@ -2134,8 +2629,7 @@ static u16 eeprom_read_u16(struct ipw_priv *priv, u8 addr)
 /* data's copy of the eeprom data                                 */
 static void eeprom_parse_mac(struct ipw_priv *priv, u8 * mac)
 {
-       u8 *ee = (u8 *) priv->eeprom;
-       memcpy(mac, &ee[EEPROM_MAC_ADDRESS], 6);
+       memcpy(mac, &priv->eeprom[EEPROM_MAC_ADDRESS], 6);
 }
 
 /*
@@ -2149,24 +2643,24 @@ static void eeprom_parse_mac(struct ipw_priv *priv, u8 * mac)
 static void ipw_eeprom_init_sram(struct ipw_priv *priv)
 {
        int i;
-       u16 *eeprom = (u16 *) priv->eeprom;
+       __le16 *eeprom = (__le16 *) priv->eeprom;
 
        IPW_DEBUG_TRACE(">>\n");
 
        /* read entire contents of eeprom into private buffer */
        for (i = 0; i < 128; i++)
-               eeprom[i] = le16_to_cpu(eeprom_read_u16(priv, (u8) i));
+               eeprom[i] = cpu_to_le16(eeprom_read_u16(priv, (u8) i));
 
        /*
           If the data looks correct, then copy it to our private
           copy.  Otherwise let the firmware know to perform the operation
-          on it's own
+          on its own.
         */
-       if ((priv->eeprom + EEPROM_VERSION) != 0) {
+       if (priv->eeprom[EEPROM_VERSION] != 0) {
                IPW_DEBUG_INFO("Writing EEPROM data into SRAM\n");
 
                /* write the eeprom data to sram */
-               for (i = 0; i < CX2_EEPROM_IMAGE_SIZE; i++)
+               for (i = 0; i < IPW_EEPROM_IMAGE_SIZE; i++)
                        ipw_write8(priv, IPW_EEPROM_DATA + i, priv->eeprom[i]);
 
                /* Do not load eeprom data on fatal error or suspend */
@@ -2181,19 +2675,19 @@ static void ipw_eeprom_init_sram(struct ipw_priv *priv)
        IPW_DEBUG_TRACE("<<\n");
 }
 
-static inline void ipw_zero_memory(struct ipw_priv *priv, u32 start, u32 count)
+static void ipw_zero_memory(struct ipw_priv *priv, u32 start, u32 count)
 {
        count >>= 2;
        if (!count)
                return;
-       _ipw_write32(priv, CX2_AUTOINC_ADDR, start);
+       _ipw_write32(priv, IPW_AUTOINC_ADDR, start);
        while (count--)
-               _ipw_write32(priv, CX2_AUTOINC_DATA, 0);
+               _ipw_write32(priv, IPW_AUTOINC_DATA, 0);
 }
 
 static inline void ipw_fw_dma_reset_command_blocks(struct ipw_priv *priv)
 {
-       ipw_zero_memory(priv, CX2_SHARED_SRAM_DMA_CONTROL,
+       ipw_zero_memory(priv, IPW_SHARED_SRAM_DMA_CONTROL,
                        CB_NUMBER_OF_ELEMENTS_SMALL *
                        sizeof(struct command_block));
 }
@@ -2207,7 +2701,7 @@ static int ipw_fw_dma_enable(struct ipw_priv *priv)
        ipw_fw_dma_reset_command_blocks(priv);
 
        /* Write CB base address */
-       ipw_write_reg32(priv, CX2_DMA_I_CB_BASE, CX2_SHARED_SRAM_DMA_CONTROL);
+       ipw_write_reg32(priv, IPW_DMA_I_CB_BASE, IPW_SHARED_SRAM_DMA_CONTROL);
 
        IPW_DEBUG_FW("<< : \n");
        return 0;
@@ -2219,9 +2713,9 @@ static void ipw_fw_dma_abort(struct ipw_priv *priv)
 
        IPW_DEBUG_FW(">> :\n");
 
-       //set the Stop and Abort bit
+       /* set the Stop and Abort bit */
        control = DMA_CONTROL_SMALL_CB_CONST_VALUE | DMA_CB_STOP_AND_ABORT;
-       ipw_write_reg32(priv, CX2_DMA_I_DMA_CONTROL, control);
+       ipw_write_reg32(priv, IPW_DMA_I_DMA_CONTROL, control);
        priv->sram_desc.last_cb_index = 0;
 
        IPW_DEBUG_FW("<< \n");
@@ -2231,7 +2725,7 @@ static int ipw_fw_dma_write_command_block(struct ipw_priv *priv, int index,
                                          struct command_block *cb)
 {
        u32 address =
-           CX2_SHARED_SRAM_DMA_CONTROL +
+           IPW_SHARED_SRAM_DMA_CONTROL +
            (sizeof(struct command_block) * index);
        IPW_DEBUG_FW(">> :\n");
 
@@ -2255,13 +2749,13 @@ static int ipw_fw_dma_kick(struct ipw_priv *priv)
                                               &priv->sram_desc.cb_list[index]);
 
        /* Enable the DMA in the CSR register */
-       ipw_clear_bit(priv, CX2_RESET_REG,
-                     CX2_RESET_REG_MASTER_DISABLED |
-                     CX2_RESET_REG_STOP_MASTER);
+       ipw_clear_bit(priv, IPW_RESET_REG,
+                     IPW_RESET_REG_MASTER_DISABLED |
+                     IPW_RESET_REG_STOP_MASTER);
 
        /* Set the Start bit. */
        control = DMA_CONTROL_SMALL_CB_CONST_VALUE | DMA_CB_START;
-       ipw_write_reg32(priv, CX2_DMA_I_DMA_CONTROL, control);
+       ipw_write_reg32(priv, IPW_DMA_I_DMA_CONTROL, control);
 
        IPW_DEBUG_FW("<< :\n");
        return 0;
@@ -2274,12 +2768,12 @@ static void ipw_fw_dma_dump_command_block(struct ipw_priv *priv)
        u32 cb_fields_address = 0;
 
        IPW_DEBUG_FW(">> :\n");
-       address = ipw_read_reg32(priv, CX2_DMA_I_CURRENT_CB);
+       address = ipw_read_reg32(priv, IPW_DMA_I_CURRENT_CB);
        IPW_DEBUG_FW_INFO("Current CB is 0x%x \n", address);
 
        /* Read the DMA Controlor register */
-       register_value = ipw_read_reg32(priv, CX2_DMA_I_DMA_CONTROL);
-       IPW_DEBUG_FW_INFO("CX2_DMA_I_DMA_CONTROL is 0x%x \n", register_value);
+       register_value = ipw_read_reg32(priv, IPW_DMA_I_DMA_CONTROL);
+       IPW_DEBUG_FW_INFO("IPW_DMA_I_DMA_CONTROL is 0x%x \n", register_value);
 
        /* Print the CB values */
        cb_fields_address = address;
@@ -2308,9 +2802,9 @@ static int ipw_fw_dma_command_block_index(struct ipw_priv *priv)
        u32 current_cb_index = 0;
 
        IPW_DEBUG_FW("<< :\n");
-       current_cb_address = ipw_read_reg32(priv, CX2_DMA_I_CURRENT_CB);
+       current_cb_address = ipw_read_reg32(priv, IPW_DMA_I_CURRENT_CB);
 
-       current_cb_index = (current_cb_address - CX2_SHARED_SRAM_DMA_CONTROL) /
+       current_cb_index = (current_cb_address - IPW_SHARED_SRAM_DMA_CONTROL) /
            sizeof(struct command_block);
 
        IPW_DEBUG_FW_INFO("Current CB index 0x%x address = 0x%X \n",
@@ -2413,22 +2907,25 @@ static int ipw_fw_dma_add_buffer(struct ipw_priv *priv,
 
 static int ipw_fw_dma_wait(struct ipw_priv *priv)
 {
-       u32 current_index = 0;
+       u32 current_index = 0, previous_index;
        u32 watchdog = 0;
 
        IPW_DEBUG_FW(">> : \n");
 
        current_index = ipw_fw_dma_command_block_index(priv);
-       IPW_DEBUG_FW_INFO("sram_desc.last_cb_index:0x%8X\n",
+       IPW_DEBUG_FW_INFO("sram_desc.last_cb_index:0x%08X\n",
                          (int)priv->sram_desc.last_cb_index);
 
        while (current_index < priv->sram_desc.last_cb_index) {
                udelay(50);
+               previous_index = current_index;
                current_index = ipw_fw_dma_command_block_index(priv);
 
-               watchdog++;
-
-               if (watchdog > 400) {
+               if (previous_index < current_index) {
+                       watchdog = 0;
+                       continue;
+               }
+               if (++watchdog > 400) {
                        IPW_DEBUG_FW_INFO("Timeout\n");
                        ipw_fw_dma_dump_command_block(priv);
                        ipw_fw_dma_abort(priv);
@@ -2439,8 +2936,8 @@ static int ipw_fw_dma_wait(struct ipw_priv *priv)
        ipw_fw_dma_abort(priv);
 
        /*Disable the DMA in the CSR register */
-       ipw_set_bit(priv, CX2_RESET_REG,
-                   CX2_RESET_REG_MASTER_DISABLED | CX2_RESET_REG_STOP_MASTER);
+       ipw_set_bit(priv, IPW_RESET_REG,
+                   IPW_RESET_REG_MASTER_DISABLED | IPW_RESET_REG_STOP_MASTER);
 
        IPW_DEBUG_FW("<< dmaWaitSync \n");
        return 0;
@@ -2478,7 +2975,8 @@ static inline int ipw_alive(struct ipw_priv *priv)
        return ipw_read32(priv, 0x90) == 0xd55555d5;
 }
 
-static inline int ipw_poll_bit(struct ipw_priv *priv, u32 addr, u32 mask,
+/* timeout in msec, attempted in 10-msec quanta */
+static int ipw_poll_bit(struct ipw_priv *priv, u32 addr, u32 mask,
                               int timeout)
 {
        int i = 0;
@@ -2504,12 +3002,13 @@ static int ipw_stop_master(struct ipw_priv *priv)
 
        IPW_DEBUG_TRACE(">> \n");
        /* stop master. typical delay - 0 */
-       ipw_set_bit(priv, CX2_RESET_REG, CX2_RESET_REG_STOP_MASTER);
+       ipw_set_bit(priv, IPW_RESET_REG, IPW_RESET_REG_STOP_MASTER);
 
-       rc = ipw_poll_bit(priv, CX2_RESET_REG,
-                         CX2_RESET_REG_MASTER_DISABLED, 100);
+       /* timeout is in msec, polled in 10-msec quanta */
+       rc = ipw_poll_bit(priv, IPW_RESET_REG,
+                         IPW_RESET_REG_MASTER_DISABLED, 100);
        if (rc < 0) {
-               IPW_ERROR("stop master failed in 10ms\n");
+               IPW_ERROR("wait for stop master failed after 100ms\n");
                return -1;
        }
 
@@ -2523,47 +3022,24 @@ static void ipw_arc_release(struct ipw_priv *priv)
        IPW_DEBUG_TRACE(">> \n");
        mdelay(5);
 
-       ipw_clear_bit(priv, CX2_RESET_REG, CBD_RESET_REG_PRINCETON_RESET);
+       ipw_clear_bit(priv, IPW_RESET_REG, CBD_RESET_REG_PRINCETON_RESET);
 
        /* no one knows timing, for safety add some delay */
        mdelay(5);
 }
 
-struct fw_header {
-       u32 version;
-       u32 mode;
-};
-
 struct fw_chunk {
-       u32 address;
-       u32 length;
+       __le32 address;
+       __le32 length;
 };
 
-#define IPW_FW_MAJOR_VERSION 2
-#define IPW_FW_MINOR_VERSION 2
-
-#define IPW_FW_MINOR(x) ((x & 0xff) >> 8)
-#define IPW_FW_MAJOR(x) (x & 0xff)
-
-#define IPW_FW_VERSION ((IPW_FW_MINOR_VERSION << 8) | \
-                         IPW_FW_MAJOR_VERSION)
-
-#define IPW_FW_PREFIX "ipw-" __stringify(IPW_FW_MAJOR_VERSION) \
-"." __stringify(IPW_FW_MINOR_VERSION) "-"
-
-#if IPW_FW_MAJOR_VERSION >= 2 && IPW_FW_MINOR_VERSION > 0
-#define IPW_FW_NAME(x) IPW_FW_PREFIX "" x ".fw"
-#else
-#define IPW_FW_NAME(x) "ipw2200_" x ".fw"
-#endif
-
 static int ipw_load_ucode(struct ipw_priv *priv, u8 * data, size_t len)
 {
        int rc = 0, i, addr;
        u8 cr = 0;
-       u16 *image;
+       __le16 *image;
 
-       image = (u16 *) data;
+       image = (__le16 *) data;
 
        IPW_DEBUG_TRACE(">> \n");
 
@@ -2572,10 +3048,8 @@ static int ipw_load_ucode(struct ipw_priv *priv, u8 * data, size_t len)
        if (rc < 0)
                return rc;
 
-//      spin_lock_irqsave(&priv->lock, flags);
-
-       for (addr = CX2_SHARED_LOWER_BOUND;
-            addr < CX2_REGISTER_DOMAIN1_END; addr += 4) {
+       for (addr = IPW_SHARED_LOWER_BOUND;
+            addr < IPW_REGISTER_DOMAIN1_END; addr += 4) {
                ipw_write32(priv, addr, 0);
        }
 
@@ -2584,21 +3058,21 @@ static int ipw_load_ucode(struct ipw_priv *priv, u8 * data, size_t len)
        /* destroy DMA queues */
        /* reset sequence */
 
-       ipw_write_reg32(priv, CX2_MEM_HALT_AND_RESET, CX2_BIT_HALT_RESET_ON);
+       ipw_write_reg32(priv, IPW_MEM_HALT_AND_RESET, IPW_BIT_HALT_RESET_ON);
        ipw_arc_release(priv);
-       ipw_write_reg32(priv, CX2_MEM_HALT_AND_RESET, CX2_BIT_HALT_RESET_OFF);
+       ipw_write_reg32(priv, IPW_MEM_HALT_AND_RESET, IPW_BIT_HALT_RESET_OFF);
        mdelay(1);
 
        /* reset PHY */
-       ipw_write_reg32(priv, CX2_INTERNAL_CMD_EVENT, CX2_BASEBAND_POWER_DOWN);
+       ipw_write_reg32(priv, IPW_INTERNAL_CMD_EVENT, IPW_BASEBAND_POWER_DOWN);
        mdelay(1);
 
-       ipw_write_reg32(priv, CX2_INTERNAL_CMD_EVENT, 0);
+       ipw_write_reg32(priv, IPW_INTERNAL_CMD_EVENT, 0);
        mdelay(1);
 
        /* enable ucode store */
-       ipw_write_reg8(priv, DINO_CONTROL_REG, 0x0);
-       ipw_write_reg8(priv, DINO_CONTROL_REG, DINO_ENABLE_CS);
+       ipw_write_reg8(priv, IPW_BASEBAND_CONTROL_STATUS, 0x0);
+       ipw_write_reg8(priv, IPW_BASEBAND_CONTROL_STATUS, DINO_ENABLE_CS);
        mdelay(1);
 
        /* write ucode */
@@ -2611,19 +3085,19 @@ static int ipw_load_ucode(struct ipw_priv *priv, u8 * data, size_t len)
         */
        /* load new ipw uCode */
        for (i = 0; i < len / 2; i++)
-               ipw_write_reg16(priv, CX2_BASEBAND_CONTROL_STORE,
-                               cpu_to_le16(image[i]));
+               ipw_write_reg16(priv, IPW_BASEBAND_CONTROL_STORE,
+                               le16_to_cpu(image[i]));
 
        /* enable DINO */
-       ipw_write_reg8(priv, CX2_BASEBAND_CONTROL_STATUS, 0);
-       ipw_write_reg8(priv, CX2_BASEBAND_CONTROL_STATUS, DINO_ENABLE_SYSTEM);
+       ipw_write_reg8(priv, IPW_BASEBAND_CONTROL_STATUS, 0);
+       ipw_write_reg8(priv, IPW_BASEBAND_CONTROL_STATUS, DINO_ENABLE_SYSTEM);
 
        /* this is where the igx / win driver deveates from the VAP driver. */
 
        /* wait for alive response */
        for (i = 0; i < 100; i++) {
                /* poll for incoming data */
-               cr = ipw_read_reg8(priv, CX2_BASEBAND_CONTROL_STATUS);
+               cr = ipw_read_reg8(priv, IPW_BASEBAND_CONTROL_STATUS);
                if (cr & DINO_RXFIFO_DATA)
                        break;
                mdelay(1);
@@ -2631,12 +3105,12 @@ static int ipw_load_ucode(struct ipw_priv *priv, u8 * data, size_t len)
 
        if (cr & DINO_RXFIFO_DATA) {
                /* alive_command_responce size is NOT multiple of 4 */
-               u32 response_buffer[(sizeof(priv->dino_alive) + 3) / 4];
+               __le32 response_buffer[(sizeof(priv->dino_alive) + 3) / 4];
 
                for (i = 0; i < ARRAY_SIZE(response_buffer); i++)
                        response_buffer[i] =
-                           le32_to_cpu(ipw_read_reg32(priv,
-                                                      CX2_BASEBAND_RX_FIFO_READ));
+                           cpu_to_le32(ipw_read_reg32(priv,
+                                                      IPW_BASEBAND_RX_FIFO_READ));
                memcpy(&priv->dino_alive, response_buffer,
                       sizeof(priv->dino_alive));
                if (priv->dino_alive.alive_command == 1
@@ -2665,9 +3139,7 @@ static int ipw_load_ucode(struct ipw_priv *priv, u8 * data, size_t len)
 
        /* disable DINO, otherwise for some reason
           firmware have problem getting alive resp. */
-       ipw_write_reg8(priv, CX2_BASEBAND_CONTROL_STATUS, 0);
-
-//      spin_unlock_irqrestore(&priv->lock, flags);
+       ipw_write_reg8(priv, IPW_BASEBAND_CONTROL_STATUS, 0);
 
        return rc;
 }
@@ -2738,16 +3210,16 @@ static int ipw_stop_nic(struct ipw_priv *priv)
        int rc = 0;
 
        /* stop */
-       ipw_write32(priv, CX2_RESET_REG, CX2_RESET_REG_STOP_MASTER);
+       ipw_write32(priv, IPW_RESET_REG, IPW_RESET_REG_STOP_MASTER);
 
-       rc = ipw_poll_bit(priv, CX2_RESET_REG,
-                         CX2_RESET_REG_MASTER_DISABLED, 500);
+       rc = ipw_poll_bit(priv, IPW_RESET_REG,
+                         IPW_RESET_REG_MASTER_DISABLED, 500);
        if (rc < 0) {
-               IPW_ERROR("wait for reg master disabled failed\n");
+               IPW_ERROR("wait for reg master disabled failed after 500ms\n");
                return rc;
        }
 
-       ipw_set_bit(priv, CX2_RESET_REG, CBD_RESET_REG_PRINCETON_RESET);
+       ipw_set_bit(priv, IPW_RESET_REG, CBD_RESET_REG_PRINCETON_RESET);
 
        return rc;
 }
@@ -2757,14 +3229,14 @@ static void ipw_start_nic(struct ipw_priv *priv)
        IPW_DEBUG_TRACE(">>\n");
 
        /* prvHwStartNic  release ARC */
-       ipw_clear_bit(priv, CX2_RESET_REG,
-                     CX2_RESET_REG_MASTER_DISABLED |
-                     CX2_RESET_REG_STOP_MASTER |
+       ipw_clear_bit(priv, IPW_RESET_REG,
+                     IPW_RESET_REG_MASTER_DISABLED |
+                     IPW_RESET_REG_STOP_MASTER |
                      CBD_RESET_REG_PRINCETON_RESET);
 
        /* enable power management */
-       ipw_set_bit(priv, CX2_GP_CNTRL_RW,
-                   CX2_GP_CNTRL_BIT_HOST_ALLOWS_STANDBY);
+       ipw_set_bit(priv, IPW_GP_CNTRL_RW,
+                   IPW_GP_CNTRL_BIT_HOST_ALLOWS_STANDBY);
 
        IPW_DEBUG_TRACE("<<\n");
 }
@@ -2777,25 +3249,25 @@ static int ipw_init_nic(struct ipw_priv *priv)
        /* reset */
        /*prvHwInitNic */
        /* set "initialization complete" bit to move adapter to D0 state */
-       ipw_set_bit(priv, CX2_GP_CNTRL_RW, CX2_GP_CNTRL_BIT_INIT_DONE);
+       ipw_set_bit(priv, IPW_GP_CNTRL_RW, IPW_GP_CNTRL_BIT_INIT_DONE);
 
        /* low-level PLL activation */
-       ipw_write32(priv, CX2_READ_INT_REGISTER,
-                   CX2_BIT_INT_HOST_SRAM_READ_INT_REGISTER);
+       ipw_write32(priv, IPW_READ_INT_REGISTER,
+                   IPW_BIT_INT_HOST_SRAM_READ_INT_REGISTER);
 
        /* wait for clock stabilization */
-       rc = ipw_poll_bit(priv, CX2_GP_CNTRL_RW,
-                         CX2_GP_CNTRL_BIT_CLOCK_READY, 250);
+       rc = ipw_poll_bit(priv, IPW_GP_CNTRL_RW,
+                         IPW_GP_CNTRL_BIT_CLOCK_READY, 250);
        if (rc < 0)
                IPW_DEBUG_INFO("FAILED wait for clock stablization\n");
 
        /* assert SW reset */
-       ipw_set_bit(priv, CX2_RESET_REG, CX2_RESET_REG_SW_RESET);
+       ipw_set_bit(priv, IPW_RESET_REG, IPW_RESET_REG_SW_RESET);
 
        udelay(10);
 
        /* set "initialization complete" bit to move adapter to D0 state */
-       ipw_set_bit(priv, CX2_GP_CNTRL_RW, CX2_GP_CNTRL_BIT_INIT_DONE);
+       ipw_set_bit(priv, IPW_GP_CNTRL_RW, IPW_GP_CNTRL_BIT_INIT_DONE);
 
        IPW_DEBUG_TRACE(">>\n");
        return 0;
@@ -2817,45 +3289,61 @@ static int ipw_reset_nic(struct ipw_priv *priv)
        /* Clear the 'host command active' bit... */
        priv->status &= ~STATUS_HCMD_ACTIVE;
        wake_up_interruptible(&priv->wait_command_queue);
+       priv->status &= ~(STATUS_SCANNING | STATUS_SCAN_ABORTING);
+       wake_up_interruptible(&priv->wait_state);
        spin_unlock_irqrestore(&priv->lock, flags);
 
        IPW_DEBUG_TRACE("<<\n");
        return rc;
 }
 
+
+struct ipw_fw {
+       __le32 ver;
+       __le32 boot_size;
+       __le32 ucode_size;
+       __le32 fw_size;
+       u8 data[0];
+};
+
 static int ipw_get_fw(struct ipw_priv *priv,
-                     const struct firmware **fw, const char *name)
+                     const struct firmware **raw, const char *name)
 {
-       struct fw_header *header;
+       struct ipw_fw *fw;
        int rc;
 
        /* ask firmware_class module to get the boot firmware off disk */
-       rc = request_firmware(fw, name, &priv->pci_dev->dev);
+       rc = request_firmware(raw, name, &priv->pci_dev->dev);
        if (rc < 0) {
-               IPW_ERROR("%s load failed: Reason %d\n", name, rc);
+               IPW_ERROR("%s request_firmware failed: Reason %d\n", name, rc);
                return rc;
        }
 
-       header = (struct fw_header *)(*fw)->data;
-       if (IPW_FW_MAJOR(le32_to_cpu(header->version)) != IPW_FW_MAJOR_VERSION) {
-               IPW_ERROR("'%s' firmware version not compatible (%d != %d)\n",
-                         name,
-                         IPW_FW_MAJOR(le32_to_cpu(header->version)),
-                         IPW_FW_MAJOR_VERSION);
+       if ((*raw)->size < sizeof(*fw)) {
+               IPW_ERROR("%s is too small (%zd)\n", name, (*raw)->size);
+               return -EINVAL;
+       }
+
+       fw = (void *)(*raw)->data;
+
+       if ((*raw)->size < sizeof(*fw) + le32_to_cpu(fw->boot_size) +
+           le32_to_cpu(fw->ucode_size) + le32_to_cpu(fw->fw_size)) {
+               IPW_ERROR("%s is too small or corrupt (%zd)\n",
+                         name, (*raw)->size);
                return -EINVAL;
        }
 
-       IPW_DEBUG_INFO("Loading firmware '%s' file v%d.%d (%zd bytes)\n",
+       IPW_DEBUG_INFO("Read firmware '%s' image v%d.%d (%zd bytes)\n",
                       name,
-                      IPW_FW_MAJOR(le32_to_cpu(header->version)),
-                      IPW_FW_MINOR(le32_to_cpu(header->version)),
-                      (*fw)->size - sizeof(struct fw_header));
+                      le32_to_cpu(fw->ver) >> 16,
+                      le32_to_cpu(fw->ver) & 0xff,
+                      (*raw)->size - sizeof(*fw));
        return 0;
 }
 
-#define CX2_RX_BUF_SIZE (3000)
+#define IPW_RX_BUF_SIZE (3000)
 
-static inline void ipw_rx_queue_reset(struct ipw_priv *priv,
+static void ipw_rx_queue_reset(struct ipw_priv *priv,
                                      struct ipw_rx_queue *rxq)
 {
        unsigned long flags;
@@ -2872,7 +3360,7 @@ static inline void ipw_rx_queue_reset(struct ipw_priv *priv,
                 * to an SKB, so we need to unmap and free potential storage */
                if (rxq->pool[i].skb != NULL) {
                        pci_unmap_single(priv->pci_dev, rxq->pool[i].dma_addr,
-                                        CX2_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
+                                        IPW_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
                        dev_kfree_skb(rxq->pool[i].skb);
                        rxq->pool[i].skb = NULL;
                }
@@ -2882,75 +3370,74 @@ static inline void ipw_rx_queue_reset(struct ipw_priv *priv,
        /* Set us so that we have processed and used all buffers, but have
         * not restocked the Rx queue with fresh buffers */
        rxq->read = rxq->write = 0;
-       rxq->processed = RX_QUEUE_SIZE - 1;
        rxq->free_count = 0;
        spin_unlock_irqrestore(&rxq->lock, flags);
 }
 
 #ifdef CONFIG_PM
 static int fw_loaded = 0;
-static const struct firmware *bootfw = NULL;
-static const struct firmware *firmware = NULL;
-static const struct firmware *ucode = NULL;
-#endif
+static const struct firmware *raw = NULL;
 
-static int ipw_load(struct ipw_priv *priv)
+static void free_firmware(void)
 {
-#ifndef CONFIG_PM
-       const struct firmware *bootfw = NULL;
-       const struct firmware *firmware = NULL;
-       const struct firmware *ucode = NULL;
-#endif
-       int rc = 0, retries = 3;
-
-#ifdef CONFIG_PM
-       if (!fw_loaded) {
+       if (fw_loaded) {
+               release_firmware(raw);
+               raw = NULL;
+               fw_loaded = 0;
+       }
+}
+#else
+#define free_firmware() do {} while (0)
 #endif
-               rc = ipw_get_fw(priv, &bootfw, IPW_FW_NAME("boot"));
-               if (rc)
-                       goto error;
-
-               switch (priv->ieee->iw_mode) {
-               case IW_MODE_ADHOC:
-                       rc = ipw_get_fw(priv, &ucode,
-                                       IPW_FW_NAME("ibss_ucode"));
-                       if (rc)
-                               goto error;
-
-                       rc = ipw_get_fw(priv, &firmware, IPW_FW_NAME("ibss"));
-                       break;
 
-#ifdef CONFIG_IPW_MONITOR
-               case IW_MODE_MONITOR:
-                       rc = ipw_get_fw(priv, &ucode,
-                                       IPW_FW_NAME("sniffer_ucode"));
-                       if (rc)
-                               goto error;
-
-                       rc = ipw_get_fw(priv, &firmware,
-                                       IPW_FW_NAME("sniffer"));
-                       break;
+static int ipw_load(struct ipw_priv *priv)
+{
+#ifndef CONFIG_PM
+       const struct firmware *raw = NULL;
 #endif
-               case IW_MODE_INFRA:
-                       rc = ipw_get_fw(priv, &ucode, IPW_FW_NAME("bss_ucode"));
-                       if (rc)
-                               goto error;
+       struct ipw_fw *fw;
+       u8 *boot_img, *ucode_img, *fw_img;
+       u8 *name = NULL;
+       int rc = 0, retries = 3;
 
-                       rc = ipw_get_fw(priv, &firmware, IPW_FW_NAME("bss"));
-                       break;
+       switch (priv->ieee->iw_mode) {
+       case IW_MODE_ADHOC:
+               name = "ipw2200-ibss.fw";
+               break;
+#ifdef CONFIG_IPW2200_MONITOR
+       case IW_MODE_MONITOR:
+               name = "ipw2200-sniffer.fw";
+               break;
+#endif
+       case IW_MODE_INFRA:
+               name = "ipw2200-bss.fw";
+               break;
+       }
 
-               default:
-                       rc = -EINVAL;
-               }
+       if (!name) {
+               rc = -EINVAL;
+               goto error;
+       }
 
-               if (rc)
+#ifdef CONFIG_PM
+       if (!fw_loaded) {
+#endif
+               rc = ipw_get_fw(priv, &raw, name);
+               if (rc < 0)
                        goto error;
-
 #ifdef CONFIG_PM
-               fw_loaded = 1;
        }
 #endif
 
+       fw = (void *)raw->data;
+       boot_img = &fw->data[0];
+       ucode_img = &fw->data[le32_to_cpu(fw->boot_size)];
+       fw_img = &fw->data[le32_to_cpu(fw->boot_size) +
+                          le32_to_cpu(fw->ucode_size)];
+
+       if (rc < 0)
+               goto error;
+
        if (!priv->rxq)
                priv->rxq = ipw_rx_queue_alloc(priv);
        else
@@ -2962,37 +3449,36 @@ static int ipw_load(struct ipw_priv *priv)
 
       retry:
        /* Ensure interrupts are disabled */
-       ipw_write32(priv, CX2_INTA_MASK_R, ~CX2_INTA_MASK_ALL);
+       ipw_write32(priv, IPW_INTA_MASK_R, ~IPW_INTA_MASK_ALL);
        priv->status &= ~STATUS_INT_ENABLED;
 
        /* ack pending interrupts */
-       ipw_write32(priv, CX2_INTA_RW, CX2_INTA_MASK_ALL);
+       ipw_write32(priv, IPW_INTA_RW, IPW_INTA_MASK_ALL);
 
        ipw_stop_nic(priv);
 
        rc = ipw_reset_nic(priv);
-       if (rc) {
+       if (rc < 0) {
                IPW_ERROR("Unable to reset NIC\n");
                goto error;
        }
 
-       ipw_zero_memory(priv, CX2_NIC_SRAM_LOWER_BOUND,
-                       CX2_NIC_SRAM_UPPER_BOUND - CX2_NIC_SRAM_LOWER_BOUND);
+       ipw_zero_memory(priv, IPW_NIC_SRAM_LOWER_BOUND,
+                       IPW_NIC_SRAM_UPPER_BOUND - IPW_NIC_SRAM_LOWER_BOUND);
 
        /* DMA the initial boot firmware into the device */
-       rc = ipw_load_firmware(priv, bootfw->data + sizeof(struct fw_header),
-                              bootfw->size - sizeof(struct fw_header));
+       rc = ipw_load_firmware(priv, boot_img, le32_to_cpu(fw->boot_size));
        if (rc < 0) {
-               IPW_ERROR("Unable to load boot firmware\n");
+               IPW_ERROR("Unable to load boot firmware: %d\n", rc);
                goto error;
        }
 
        /* kick start the device */
        ipw_start_nic(priv);
 
-       /* wait for the device to finish it's initial startup sequence */
-       rc = ipw_poll_bit(priv, CX2_INTA_RW,
-                         CX2_INTA_BIT_FW_INITIALIZATION_DONE, 500);
+       /* wait for the device to finish its initial startup sequence */
+       rc = ipw_poll_bit(priv, IPW_INTA_RW,
+                         IPW_INTA_BIT_FW_INITIALIZATION_DONE, 500);
        if (rc < 0) {
                IPW_ERROR("device failed to boot initial fw image\n");
                goto error;
@@ -3000,13 +3486,12 @@ static int ipw_load(struct ipw_priv *priv)
        IPW_DEBUG_INFO("initial device response after %dms\n", rc);
 
        /* ack fw init done interrupt */
-       ipw_write32(priv, CX2_INTA_RW, CX2_INTA_BIT_FW_INITIALIZATION_DONE);
+       ipw_write32(priv, IPW_INTA_RW, IPW_INTA_BIT_FW_INITIALIZATION_DONE);
 
        /* DMA the ucode into the device */
-       rc = ipw_load_ucode(priv, ucode->data + sizeof(struct fw_header),
-                           ucode->size - sizeof(struct fw_header));
+       rc = ipw_load_ucode(priv, ucode_img, le32_to_cpu(fw->ucode_size));
        if (rc < 0) {
-               IPW_ERROR("Unable to load ucode\n");
+               IPW_ERROR("Unable to load ucode: %d\n", rc);
                goto error;
        }
 
@@ -3014,31 +3499,32 @@ static int ipw_load(struct ipw_priv *priv)
        ipw_stop_nic(priv);
 
        /* DMA bss firmware into the device */
-       rc = ipw_load_firmware(priv, firmware->data +
-                              sizeof(struct fw_header),
-                              firmware->size - sizeof(struct fw_header));
+       rc = ipw_load_firmware(priv, fw_img, le32_to_cpu(fw->fw_size));
        if (rc < 0) {
-               IPW_ERROR("Unable to load firmware\n");
+               IPW_ERROR("Unable to load firmware: %d\n", rc);
                goto error;
        }
+#ifdef CONFIG_PM
+       fw_loaded = 1;
+#endif
 
        ipw_write32(priv, IPW_EEPROM_LOAD_DISABLE, 0);
 
        rc = ipw_queue_reset(priv);
-       if (rc) {
+       if (rc < 0) {
                IPW_ERROR("Unable to initialize queues\n");
                goto error;
        }
 
        /* Ensure interrupts are disabled */
-       ipw_write32(priv, CX2_INTA_MASK_R, ~CX2_INTA_MASK_ALL);
+       ipw_write32(priv, IPW_INTA_MASK_R, ~IPW_INTA_MASK_ALL);
        /* ack pending interrupts */
-       ipw_write32(priv, CX2_INTA_RW, CX2_INTA_MASK_ALL);
+       ipw_write32(priv, IPW_INTA_RW, IPW_INTA_MASK_ALL);
 
        /* kick start the device */
        ipw_start_nic(priv);
 
-       if (ipw_read32(priv, CX2_INTA_RW) & CX2_INTA_BIT_PARITY_ERROR) {
+       if (ipw_read32(priv, IPW_INTA_RW) & IPW_INTA_BIT_PARITY_ERROR) {
                if (retries > 0) {
                        IPW_WARNING("Parity error.  Retrying init.\n");
                        retries--;
@@ -3051,16 +3537,16 @@ static int ipw_load(struct ipw_priv *priv)
        }
 
        /* wait for the device */
-       rc = ipw_poll_bit(priv, CX2_INTA_RW,
-                         CX2_INTA_BIT_FW_INITIALIZATION_DONE, 500);
+       rc = ipw_poll_bit(priv, IPW_INTA_RW,
+                         IPW_INTA_BIT_FW_INITIALIZATION_DONE, 500);
        if (rc < 0) {
-               IPW_ERROR("device failed to start after 500ms\n");
+               IPW_ERROR("device failed to start within 500ms\n");
                goto error;
        }
        IPW_DEBUG_INFO("device response after %dms\n", rc);
 
        /* ack fw init done interrupt */
-       ipw_write32(priv, CX2_INTA_RW, CX2_INTA_BIT_FW_INITIALIZATION_DONE);
+       ipw_write32(priv, IPW_INTA_RW, IPW_INTA_BIT_FW_INITIALIZATION_DONE);
 
        /* read eeprom data and initialize the eeprom region of sram */
        priv->eeprom_delay = 1;
@@ -3072,15 +3558,13 @@ static int ipw_load(struct ipw_priv *priv)
        /* Ensure our queue has valid packets */
        ipw_rx_queue_replenish(priv);
 
-       ipw_write32(priv, CX2_RX_READ_INDEX, priv->rxq->read);
+       ipw_write32(priv, IPW_RX_READ_INDEX, priv->rxq->read);
 
        /* ack pending interrupts */
-       ipw_write32(priv, CX2_INTA_RW, CX2_INTA_MASK_ALL);
+       ipw_write32(priv, IPW_INTA_RW, IPW_INTA_MASK_ALL);
 
 #ifndef CONFIG_PM
-       release_firmware(bootfw);
-       release_firmware(ucode);
-       release_firmware(firmware);
+       release_firmware(raw);
 #endif
        return 0;
 
@@ -3090,15 +3574,11 @@ static int ipw_load(struct ipw_priv *priv)
                priv->rxq = NULL;
        }
        ipw_tx_queue_free(priv);
-       if (bootfw)
-               release_firmware(bootfw);
-       if (ucode)
-               release_firmware(ucode);
-       if (firmware)
-               release_firmware(firmware);
+       if (raw)
+               release_firmware(raw);
 #ifdef CONFIG_PM
        fw_loaded = 0;
-       bootfw = ucode = firmware = NULL;
+       raw = NULL;
 #endif
 
        return rc;
@@ -3131,7 +3611,22 @@ static int ipw_load(struct ipw_priv *priv)
  * Driver allocates buffers of this size for Rx
  */
 
-static inline int ipw_queue_space(const struct clx2_queue *q)
+/**
+ * ipw_rx_queue_space - Return number of free slots available in queue.
+ */
+static int ipw_rx_queue_space(const struct ipw_rx_queue *q)
+{
+       int s = q->read - q->write;
+       if (s <= 0)
+               s += RX_QUEUE_SIZE;
+       /* keep some buffer to not confuse full and empty queue */
+       s -= 2;
+       if (s < 0)
+               s = 0;
+       return s;
+}
+
+static inline int ipw_tx_queue_space(const struct clx2_queue *q)
 {
        int s = q->last_used - q->first_empty;
        if (s <= 0)
@@ -3300,21 +3795,7 @@ static void ipw_tx_queue_free(struct ipw_priv *priv)
        ipw_queue_tx_free(priv, &priv->txq[3]);
 }
 
-static void inline __maybe_wake_tx(struct ipw_priv *priv)
-{
-       if (netif_running(priv->net_dev)) {
-               switch (priv->port_type) {
-               case DCR_TYPE_MU_BSS:
-               case DCR_TYPE_MU_IBSS:
-                       if (!(priv->status & STATUS_ASSOCIATED))
-                               return;
-               }
-               netif_wake_queue(priv->net_dev);
-       }
-
-}
-
-static inline void ipw_create_bssid(struct ipw_priv *priv, u8 * bssid)
+static void ipw_create_bssid(struct ipw_priv *priv, u8 * bssid)
 {
        /* First 3 bytes are manufacturer */
        bssid[0] = priv->mac_addr[0];
@@ -3328,10 +3809,11 @@ static inline void ipw_create_bssid(struct ipw_priv *priv, u8 * bssid)
        bssid[0] |= 0x02;       /* set local assignment bit (IEEE802) */
 }
 
-static inline u8 ipw_add_station(struct ipw_priv *priv, u8 * bssid)
+static u8 ipw_add_station(struct ipw_priv *priv, u8 * bssid)
 {
        struct ipw_station_entry entry;
        int i;
+       DECLARE_MAC_BUF(mac);
 
        for (i = 0; i < priv->num_stations; i++) {
                if (!memcmp(priv->stations[i], bssid, ETH_ALEN)) {
@@ -3348,7 +3830,7 @@ static inline u8 ipw_add_station(struct ipw_priv *priv, u8 * bssid)
        if (i == MAX_STATIONS)
                return IPW_INVALID_STATION;
 
-       IPW_DEBUG_SCAN("Adding AdHoc station: " MAC_FMT "\n", MAC_ARG(bssid));
+       IPW_DEBUG_SCAN("Adding AdHoc station: %s\n", print_mac(mac, bssid));
 
        entry.reserved = 0;
        entry.support_mode = 0;
@@ -3361,7 +3843,7 @@ static inline u8 ipw_add_station(struct ipw_priv *priv, u8 * bssid)
        return i;
 }
 
-static inline u8 ipw_find_station(struct ipw_priv *priv, u8 * bssid)
+static u8 ipw_find_station(struct ipw_priv *priv, u8 * bssid)
 {
        int i;
 
@@ -3375,15 +3857,22 @@ static inline u8 ipw_find_station(struct ipw_priv *priv, u8 * bssid)
 static void ipw_send_disassociate(struct ipw_priv *priv, int quiet)
 {
        int err;
+       DECLARE_MAC_BUF(mac);
+
+       if (priv->status & STATUS_ASSOCIATING) {
+               IPW_DEBUG_ASSOC("Disassociating while associating.\n");
+               queue_work(priv->workqueue, &priv->disassociate);
+               return;
+       }
 
-       if (!(priv->status & (STATUS_ASSOCIATING | STATUS_ASSOCIATED))) {
+       if (!(priv->status & STATUS_ASSOCIATED)) {
                IPW_DEBUG_ASSOC("Disassociating while not associated.\n");
                return;
        }
 
-       IPW_DEBUG_ASSOC("Disassocation attempt from " MAC_FMT " "
+       IPW_DEBUG_ASSOC("Disassocation attempt from %s "
                        "on channel %d.\n",
-                       MAC_ARG(priv->assoc_request.bssid),
+                       print_mac(mac, priv->assoc_request.bssid),
                        priv->assoc_request.channel);
 
        priv->status &= ~(STATUS_ASSOCIATING | STATUS_ASSOCIATED);
@@ -3393,6 +3882,7 @@ static void ipw_send_disassociate(struct ipw_priv *priv, int quiet)
                priv->assoc_request.assoc_type = HC_DISASSOC_QUIET;
        else
                priv->assoc_request.assoc_type = HC_DISASSOCIATE;
+
        err = ipw_send_associate(priv, &priv->assoc_request);
        if (err) {
                IPW_DEBUG_HC("Attempt to send [dis]associate command "
@@ -3411,12 +3901,30 @@ static int ipw_disassociate(void *data)
        return 1;
 }
 
-static void ipw_bg_disassociate(void *data)
+static void ipw_bg_disassociate(struct work_struct *work)
 {
-       struct ipw_priv *priv = data;
-       down(&priv->sem);
-       ipw_disassociate(data);
-       up(&priv->sem);
+       struct ipw_priv *priv =
+               container_of(work, struct ipw_priv, disassociate);
+       mutex_lock(&priv->mutex);
+       ipw_disassociate(priv);
+       mutex_unlock(&priv->mutex);
+}
+
+static void ipw_system_config(struct work_struct *work)
+{
+       struct ipw_priv *priv =
+               container_of(work, struct ipw_priv, system_config);
+
+#ifdef CONFIG_IPW2200_PROMISCUOUS
+       if (priv->prom_net_dev && netif_running(priv->prom_net_dev)) {
+               priv->sys_config.accept_all_data_frames = 1;
+               priv->sys_config.accept_non_directed_frames = 1;
+               priv->sys_config.accept_all_mgmt_bcpr = 1;
+               priv->sys_config.accept_all_mgmt_frames = 1;
+       }
+#endif
+
+       ipw_send_system_config(priv);
 }
 
 struct ipw_status_code {
@@ -3471,7 +3979,6 @@ static const struct ipw_status_code ipw_status_codes[] = {
        {0x2E, "Cipher suite is rejected per security policy"},
 };
 
-#ifdef CONFIG_IPW_DEBUG
 static const char *ipw_get_status_code(u16 status)
 {
        int i;
@@ -3480,14 +3987,20 @@ static const char *ipw_get_status_code(u16 status)
                        return ipw_status_codes[i].reason;
        return "Unknown status value.";
 }
-#endif
 
 static void inline average_init(struct average *avg)
 {
        memset(avg, 0, sizeof(*avg));
 }
 
-static void inline average_add(struct average *avg, s16 val)
+#define DEPTH_RSSI 8
+#define DEPTH_NOISE 16
+static s16 exponential_average(s16 prev_avg, s16 val, u8 depth)
+{
+       return ((depth-1)*prev_avg +  val)/depth;
+}
+
+static void average_add(struct average *avg, s16 val)
 {
        avg->sum -= avg->entries[avg->pos];
        avg->sum += val;
@@ -3498,7 +4011,7 @@ static void inline average_add(struct average *avg, s16 val)
        }
 }
 
-static s16 inline average_value(struct average *avg)
+static s16 average_value(struct average *avg)
 {
        if (!unlikely(avg->init)) {
                if (avg->pos)
@@ -3516,8 +4029,8 @@ static void ipw_reset_stats(struct ipw_priv *priv)
        priv->quality = 0;
 
        average_init(&priv->average_missed_beacons);
-       average_init(&priv->average_rssi);
-       average_init(&priv->average_noise);
+       priv->exp_avg_rssi = -60;
+       priv->exp_avg_noise = -85 + 0x100;
 
        priv->last_rate = 0;
        priv->last_missed_beacons = 0;
@@ -3540,7 +4053,7 @@ static void ipw_reset_stats(struct ipw_priv *priv)
 
 }
 
-static inline u32 ipw_get_max_rate(struct ipw_priv *priv)
+static u32 ipw_get_max_rate(struct ipw_priv *priv)
 {
        u32 i = 0x80000000;
        u32 mask = priv->rates_mask;
@@ -3660,7 +4173,7 @@ static void ipw_gather_stats(struct ipw_priv *priv)
        priv->last_missed_beacons = priv->missed_beacons;
        if (priv->assoc_request.beacon_interval) {
                missed_beacons_percent = missed_beacons_delta *
-                   (HZ * priv->assoc_request.beacon_interval) /
+                   (HZ * le16_to_cpu(priv->assoc_request.beacon_interval)) /
                    (IPW_STATS_INTERVAL * 10);
        } else {
                missed_beacons_percent = 0;
@@ -3724,7 +4237,7 @@ static void ipw_gather_stats(struct ipw_priv *priv)
        IPW_DEBUG_STATS("Tx quality   : %3d%% (%u errors, %u packets)\n",
                        tx_quality, tx_failures_delta, tx_packets_delta);
 
-       rssi = average_value(&priv->average_rssi);
+       rssi = priv->exp_avg_rssi;
        signal_quality =
            (100 *
             (priv->ieee->perfect_rssi - priv->ieee->worst_rssi) *
@@ -3767,26 +4280,32 @@ static void ipw_gather_stats(struct ipw_priv *priv)
                           IPW_STATS_INTERVAL);
 }
 
-static void ipw_bg_gather_stats(void *data)
+static void ipw_bg_gather_stats(struct work_struct *work)
 {
-       struct ipw_priv *priv = data;
-       down(&priv->sem);
-       ipw_gather_stats(data);
-       up(&priv->sem);
+       struct ipw_priv *priv =
+               container_of(work, struct ipw_priv, gather_stats.work);
+       mutex_lock(&priv->mutex);
+       ipw_gather_stats(priv);
+       mutex_unlock(&priv->mutex);
 }
 
-static inline void ipw_handle_missed_beacon(struct ipw_priv *priv,
+/* Missed beacon behavior:
+ * 1st missed -> roaming_threshold, just wait, don't do any scan/roam.
+ * roaming_threshold -> disassociate_threshold, scan and roam for better signal.
+ * Above disassociate threshold, give up and stop scanning.
+ * Roaming is disabled if disassociate_threshold <= roaming_threshold  */
+static void ipw_handle_missed_beacon(struct ipw_priv *priv,
                                            int missed_count)
 {
        priv->notif_missed_beacons = missed_count;
 
-       if (missed_count > priv->missed_beacon_threshold &&
+       if (missed_count > priv->disassociate_threshold &&
            priv->status & STATUS_ASSOCIATED) {
                /* If associated and we've hit the missed
                 * beacon threshold, disassociate, turn
                 * off roaming, and abort any active scans */
                IPW_DEBUG(IPW_DL_INFO | IPW_DL_NOTIF |
-                         IPW_DL_STATE,
+                         IPW_DL_STATE | IPW_DL_ASSOC,
                          "Missed beacon: %d - disassociate\n", missed_count);
                priv->status &= ~STATUS_ROAMING;
                if (priv->status & STATUS_SCANNING) {
@@ -3809,17 +4328,21 @@ static inline void ipw_handle_missed_beacon(struct ipw_priv *priv,
                return;
        }
 
-       if (missed_count > priv->roaming_threshold) {
+       if (roaming &&
+           (missed_count > priv->roaming_threshold &&
+            missed_count <= priv->disassociate_threshold)) {
                /* If we are not already roaming, set the ROAM
-                * bit in the status and kick off a scan */
+                * bit in the status and kick off a scan.
+                * This can happen several times before we reach
+                * disassociate_threshold. */
                IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE,
                          "Missed beacon: %d - initiate "
                          "roaming\n", missed_count);
                if (!(priv->status & STATUS_ROAMING)) {
                        priv->status |= STATUS_ROAMING;
                        if (!(priv->status & STATUS_SCANNING))
-                               queue_work(priv->workqueue,
-                                          &priv->request_scan);
+                               queue_delayed_work(priv->workqueue,
+                                                  &priv->request_scan, 0);
                }
                return;
        }
@@ -3829,25 +4352,57 @@ static inline void ipw_handle_missed_beacon(struct ipw_priv *priv,
                 * stuck (only if we aren't roaming --
                 * otherwise we'll never scan more than 2 or 3
                 * channels..) */
-               IPW_DEBUG(IPW_DL_INFO | IPW_DL_NOTIF |
-                         IPW_DL_STATE, "Aborting scan with missed beacon.\n");
+               IPW_DEBUG(IPW_DL_INFO | IPW_DL_NOTIF | IPW_DL_STATE,
+                         "Aborting scan with missed beacon.\n");
                queue_work(priv->workqueue, &priv->abort_scan);
        }
 
        IPW_DEBUG_NOTIF("Missed beacon: %d\n", missed_count);
+}
+
+static void ipw_scan_event(struct work_struct *work)
+{
+       union iwreq_data wrqu;
+
+       struct ipw_priv *priv =
+               container_of(work, struct ipw_priv, scan_event.work);
+
+       wrqu.data.length = 0;
+       wrqu.data.flags = 0;
+       wireless_send_event(priv->net_dev, SIOCGIWSCAN, &wrqu, NULL);
+}
+
+static void handle_scan_event(struct ipw_priv *priv)
+{
+       /* Only userspace-requested scan completion events go out immediately */
+       if (!priv->user_requested_scan) {
+               if (!delayed_work_pending(&priv->scan_event))
+                       queue_delayed_work(priv->workqueue, &priv->scan_event,
+                                        round_jiffies_relative(msecs_to_jiffies(4000)));
+       } else {
+               union iwreq_data wrqu;
 
+               priv->user_requested_scan = 0;
+               cancel_delayed_work(&priv->scan_event);
+
+               wrqu.data.length = 0;
+               wrqu.data.flags = 0;
+               wireless_send_event(priv->net_dev, SIOCGIWSCAN, &wrqu, NULL);
+       }
 }
 
 /**
  * Handle host notification packet.
  * Called from interrupt routine
  */
-static inline void ipw_rx_notification(struct ipw_priv *priv,
+static void ipw_rx_notification(struct ipw_priv *priv,
                                       struct ipw_rx_notification *notif)
 {
+       DECLARE_MAC_BUF(mac);
+       u16 size = le16_to_cpu(notif->size);
        notif->size = le16_to_cpu(notif->size);
 
-       IPW_DEBUG_NOTIF("type = %i (%d bytes)\n", notif->subtype, notif->size);
+       IPW_DEBUG_NOTIF("type = %i (%d bytes)\n", notif->subtype, size);
 
        switch (notif->subtype) {
        case HOST_NOTIFICATION_STATUS_ASSOCIATED:{
@@ -3857,11 +4412,11 @@ static inline void ipw_rx_notification(struct ipw_priv *priv,
                        case CMAS_ASSOCIATED:{
                                        IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
                                                  IPW_DL_ASSOC,
-                                                 "associated: '%s' " MAC_FMT
+                                                 "associated: '%s' %s"
                                                  " \n",
                                                  escape_essid(priv->essid,
                                                               priv->essid_len),
-                                                 MAC_ARG(priv->bssid));
+                                                 print_mac(mac, priv->bssid));
 
                                        switch (priv->ieee->iw_mode) {
                                        case IW_MODE_INFRA:
@@ -3882,14 +4437,46 @@ static inline void ipw_rx_notification(struct ipw_priv *priv,
                                                                   workqueue,
                                                                   &priv->
                                                                   adhoc_check,
-                                                                  priv->
+                                                                  le16_to_cpu(priv->
                                                                   assoc_request.
-                                                                  beacon_interval);
+                                                                  beacon_interval));
                                                break;
                                        }
 
                                        priv->status &= ~STATUS_ASSOCIATING;
                                        priv->status |= STATUS_ASSOCIATED;
+                                       queue_work(priv->workqueue,
+                                                  &priv->system_config);
+
+#ifdef CONFIG_IPW2200_QOS
+#define IPW_GET_PACKET_STYPE(x) WLAN_FC_GET_STYPE( \
+                        le16_to_cpu(((struct ieee80211_hdr *)(x))->frame_ctl))
+                                       if ((priv->status & STATUS_AUTH) &&
+                                           (IPW_GET_PACKET_STYPE(&notif->u.raw)
+                                            == IEEE80211_STYPE_ASSOC_RESP)) {
+                                               if ((sizeof
+                                                    (struct
+                                                     ieee80211_assoc_response)
+                                                    <= size)
+                                                   && (size <= 2314)) {
+                                                       struct
+                                                       ieee80211_rx_stats
+                                                           stats = {
+                                                               .len = size - 1,
+                                                       };
+
+                                                       IPW_DEBUG_QOS
+                                                           ("QoS Associate "
+                                                            "size %d\n", size);
+                                                       ieee80211_rx_mgt(priv->
+                                                                        ieee,
+                                                                        (struct
+                                                                         ieee80211_hdr_4addr
+                                                                         *)
+                                                                        &notif->u.raw, &stats);
+                                               }
+                                       }
+#endif
 
                                        schedule_work(&priv->link_up);
 
@@ -3900,25 +4487,23 @@ static inline void ipw_rx_notification(struct ipw_priv *priv,
                                        if (priv->
                                            status & (STATUS_ASSOCIATED |
                                                      STATUS_AUTH)) {
-#ifdef CONFIG_IPW_DEBUG
                                                struct notif_authenticate *auth
                                                    = &notif->u.auth;
                                                IPW_DEBUG(IPW_DL_NOTIF |
                                                          IPW_DL_STATE |
                                                          IPW_DL_ASSOC,
                                                          "deauthenticated: '%s' "
-                                                         MAC_FMT
+                                                         "%s"
                                                          ": (0x%04X) - %s \n",
                                                          escape_essid(priv->
                                                                       essid,
                                                                       priv->
                                                                       essid_len),
-                                                         MAC_ARG(priv->bssid),
-                                                         ntohs(auth->status),
+                                                         print_mac(mac, priv->bssid),
+                                                         le16_to_cpu(auth->status),
                                                          ipw_get_status_code
-                                                         (ntohs
+                                                         (le16_to_cpu
                                                           (auth->status)));
-#endif
 
                                                priv->status &=
                                                    ~(STATUS_ASSOCIATING |
@@ -3931,11 +4516,11 @@ static inline void ipw_rx_notification(struct ipw_priv *priv,
 
                                        IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
                                                  IPW_DL_ASSOC,
-                                                 "authenticated: '%s' " MAC_FMT
+                                                 "authenticated: '%s' %s"
                                                  "\n",
                                                  escape_essid(priv->essid,
                                                               priv->essid_len),
-                                                 MAC_ARG(priv->bssid));
+                                                 print_mac(mac, priv->bssid));
                                        break;
                                }
 
@@ -3952,30 +4537,39 @@ static inline void ipw_rx_notification(struct ipw_priv *priv,
                                                          IPW_DL_STATE |
                                                          IPW_DL_ASSOC,
                                                          "association failed (0x%04X): %s\n",
-                                                         ntohs(resp->status),
+                                                         le16_to_cpu(resp->status),
                                                          ipw_get_status_code
-                                                         (ntohs
+                                                         (le16_to_cpu
                                                           (resp->status)));
                                        }
 
                                        IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
                                                  IPW_DL_ASSOC,
-                                                 "disassociated: '%s' " MAC_FMT
+                                                 "disassociated: '%s' %s"
                                                  " \n",
                                                  escape_essid(priv->essid,
                                                               priv->essid_len),
-                                                 MAC_ARG(priv->bssid));
+                                                 print_mac(mac, priv->bssid));
 
                                        priv->status &=
                                            ~(STATUS_DISASSOCIATING |
                                              STATUS_ASSOCIATING |
                                              STATUS_ASSOCIATED | STATUS_AUTH);
+                                       if (priv->assoc_network
+                                           && (priv->assoc_network->
+                                               capability &
+                                               WLAN_CAPABILITY_IBSS))
+                                               ipw_remove_current_network
+                                                   (priv);
 
                                        schedule_work(&priv->link_down);
 
                                        break;
                                }
 
+                       case CMAS_RX_ASSOC_RESP:
+                               break;
+
                        default:
                                IPW_ERROR("assoc: unknown (%d)\n",
                                          assoc->state);
@@ -3990,10 +4584,10 @@ static inline void ipw_rx_notification(struct ipw_priv *priv,
                        switch (auth->state) {
                        case CMAS_AUTHENTICATED:
                                IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE,
-                                         "authenticated: '%s' " MAC_FMT " \n",
+                                         "authenticated: '%s' %s \n",
                                          escape_essid(priv->essid,
                                                       priv->essid_len),
-                                         MAC_ARG(priv->bssid));
+                                         print_mac(mac, priv->bssid));
                                priv->status |= STATUS_AUTH;
                                break;
 
@@ -4002,17 +4596,17 @@ static inline void ipw_rx_notification(struct ipw_priv *priv,
                                        IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
                                                  IPW_DL_ASSOC,
                                                  "authentication failed (0x%04X): %s\n",
-                                                 ntohs(auth->status),
-                                                 ipw_get_status_code(ntohs
+                                                 le16_to_cpu(auth->status),
+                                                 ipw_get_status_code(le16_to_cpu
                                                                      (auth->
                                                                       status)));
                                }
                                IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
                                          IPW_DL_ASSOC,
-                                         "deauthenticated: '%s' " MAC_FMT "\n",
+                                         "deauthenticated: '%s' %s\n",
                                          escape_essid(priv->essid,
                                                       priv->essid_len),
-                                         MAC_ARG(priv->bssid));
+                                         print_mac(mac, priv->bssid));
 
                                priv->status &= ~(STATUS_ASSOCIATING |
                                                  STATUS_AUTH |
@@ -4060,6 +4654,7 @@ static inline void ipw_rx_notification(struct ipw_priv *priv,
                        case CMAS_RX_ASSOC_RESP:
                                IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
                                          IPW_DL_ASSOC, "RX_ASSOC_RESP\n");
+
                                break;
                        case CMAS_ASSOCIATED:
                                IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
@@ -4077,20 +4672,20 @@ static inline void ipw_rx_notification(struct ipw_priv *priv,
                        struct notif_channel_result *x =
                            &notif->u.channel_result;
 
-                       if (notif->size == sizeof(*x)) {
+                       if (size == sizeof(*x)) {
                                IPW_DEBUG_SCAN("Scan result for channel %d\n",
                                               x->channel_num);
                        } else {
                                IPW_DEBUG_SCAN("Scan result of wrong size %d "
                                               "(should be %zd)\n",
-                                              notif->size, sizeof(*x));
+                                              size, sizeof(*x));
                        }
                        break;
                }
 
        case HOST_NOTIFICATION_STATUS_SCAN_COMPLETED:{
                        struct notif_scan_complete *x = &notif->u.scan_complete;
-                       if (notif->size == sizeof(*x)) {
+                       if (size == sizeof(*x)) {
                                IPW_DEBUG_SCAN
                                    ("Scan completed: type %d, %d channels, "
                                     "%d status\n", x->scan_type,
@@ -4098,65 +4693,103 @@ static inline void ipw_rx_notification(struct ipw_priv *priv,
                        } else {
                                IPW_ERROR("Scan completed of wrong size %d "
                                          "(should be %zd)\n",
-                                         notif->size, sizeof(*x));
+                                         size, sizeof(*x));
                        }
 
                        priv->status &=
                            ~(STATUS_SCANNING | STATUS_SCAN_ABORTING);
 
+                       wake_up_interruptible(&priv->wait_state);
                        cancel_delayed_work(&priv->scan_check);
 
+                       if (priv->status & STATUS_EXIT_PENDING)
+                               break;
+
+                       priv->ieee->scans++;
+
+#ifdef CONFIG_IPW2200_MONITOR
+                       if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
+                               priv->status |= STATUS_SCAN_FORCED;
+                               queue_delayed_work(priv->workqueue,
+                                                  &priv->request_scan, 0);
+                               break;
+                       }
+                       priv->status &= ~STATUS_SCAN_FORCED;
+#endif                         /* CONFIG_IPW2200_MONITOR */
+
+                       /* Do queued direct scans first */
+                       if (priv->status & STATUS_DIRECT_SCAN_PENDING) {
+                               queue_delayed_work(priv->workqueue,
+                                                  &priv->request_direct_scan, 0);
+                       }
+
                        if (!(priv->status & (STATUS_ASSOCIATED |
                                              STATUS_ASSOCIATING |
                                              STATUS_ROAMING |
                                              STATUS_DISASSOCIATING)))
                                queue_work(priv->workqueue, &priv->associate);
                        else if (priv->status & STATUS_ROAMING) {
-                               /* If a scan completed and we are in roam mode, then
-                                * the scan that completed was the one requested as a
-                                * result of entering roam... so, schedule the
-                                * roam work */
-                               queue_work(priv->workqueue, &priv->roam);
+                               if (x->status == SCAN_COMPLETED_STATUS_COMPLETE)
+                                       /* If a scan completed and we are in roam mode, then
+                                        * the scan that completed was the one requested as a
+                                        * result of entering roam... so, schedule the
+                                        * roam work */
+                                       queue_work(priv->workqueue,
+                                                  &priv->roam);
+                               else
+                                       /* Don't schedule if we aborted the scan */
+                                       priv->status &= ~STATUS_ROAMING;
                        } else if (priv->status & STATUS_SCAN_PENDING)
-                               queue_work(priv->workqueue,
-                                          &priv->request_scan);
+                               queue_delayed_work(priv->workqueue,
+                                                  &priv->request_scan, 0);
                        else if (priv->config & CFG_BACKGROUND_SCAN
                                 && priv->status & STATUS_ASSOCIATED)
                                queue_delayed_work(priv->workqueue,
-                                                  &priv->request_scan, HZ);
-
-                       priv->ieee->scans++;
+                                                  &priv->request_scan,
+                                                  round_jiffies_relative(HZ));
+
+                       /* Send an empty event to user space.
+                        * We don't send the received data on the event because
+                        * it would require us to do complex transcoding, and
+                        * we want to minimise the work done in the irq handler
+                        * Use a request to extract the data.
+                        * Also, we generate this even for any scan, regardless
+                        * on how the scan was initiated. User space can just
+                        * sync on periodic scan to get fresh data...
+                        * Jean II */
+                       if (x->status == SCAN_COMPLETED_STATUS_COMPLETE)
+                               handle_scan_event(priv);
                        break;
                }
 
        case HOST_NOTIFICATION_STATUS_FRAG_LENGTH:{
                        struct notif_frag_length *x = &notif->u.frag_len;
 
-                       if (notif->size == sizeof(*x))
+                       if (size == sizeof(*x))
                                IPW_ERROR("Frag length: %d\n",
                                          le16_to_cpu(x->frag_length));
                        else
                                IPW_ERROR("Frag length of wrong size %d "
                                          "(should be %zd)\n",
-                                         notif->size, sizeof(*x));
+                                         size, sizeof(*x));
                        break;
                }
 
        case HOST_NOTIFICATION_STATUS_LINK_DETERIORATION:{
                        struct notif_link_deterioration *x =
                            &notif->u.link_deterioration;
-                       if (notif->size == sizeof(*x)) {
+
+                       if (size == sizeof(*x)) {
                                IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE,
-                                         "link deterioration: '%s' " MAC_FMT
-                                         " \n", escape_essid(priv->essid,
-                                                             priv->essid_len),
-                                         MAC_ARG(priv->bssid));
+                                       "link deterioration: type %d, cnt %d\n",
+                                       x->silence_notification_type,
+                                       x->silence_count);
                                memcpy(&priv->last_link_deterioration, x,
                                       sizeof(*x));
                        } else {
                                IPW_ERROR("Link Deterioration of wrong size %d "
                                          "(should be %zd)\n",
-                                         notif->size, sizeof(*x));
+                                         size, sizeof(*x));
                        }
                        break;
                }
@@ -4172,10 +4805,10 @@ static inline void ipw_rx_notification(struct ipw_priv *priv,
 
        case HOST_NOTIFICATION_STATUS_BEACON_STATE:{
                        struct notif_beacon_state *x = &notif->u.beacon_state;
-                       if (notif->size != sizeof(*x)) {
+                       if (size != sizeof(*x)) {
                                IPW_ERROR
                                    ("Beacon state of wrong size %d (should "
-                                    "be %zd)\n", notif->size, sizeof(*x));
+                                    "be %zd)\n", size, sizeof(*x));
                                break;
                        }
 
@@ -4190,7 +4823,7 @@ static inline void ipw_rx_notification(struct ipw_priv *priv,
 
        case HOST_NOTIFICATION_STATUS_TGI_TX_KEY:{
                        struct notif_tgi_tx_key *x = &notif->u.tgi_tx_key;
-                       if (notif->size == sizeof(*x)) {
+                       if (size == sizeof(*x)) {
                                IPW_ERROR("TGi Tx Key: state 0x%02x sec type "
                                          "0x%02x station %d\n",
                                          x->key_state, x->security_type,
@@ -4200,14 +4833,14 @@ static inline void ipw_rx_notification(struct ipw_priv *priv,
 
                        IPW_ERROR
                            ("TGi Tx Key of wrong size %d (should be %zd)\n",
-                            notif->size, sizeof(*x));
+                            size, sizeof(*x));
                        break;
                }
 
        case HOST_NOTIFICATION_CALIB_KEEP_RESULTS:{
                        struct notif_calibration *x = &notif->u.calibration;
 
-                       if (notif->size == sizeof(*x)) {
+                       if (size == sizeof(*x)) {
                                memcpy(&priv->calib, x, sizeof(*x));
                                IPW_DEBUG_INFO("TODO: Calibration\n");
                                break;
@@ -4215,30 +4848,29 @@ static inline void ipw_rx_notification(struct ipw_priv *priv,
 
                        IPW_ERROR
                            ("Calibration of wrong size %d (should be %zd)\n",
-                            notif->size, sizeof(*x));
+                            size, sizeof(*x));
                        break;
                }
 
        case HOST_NOTIFICATION_NOISE_STATS:{
-                       if (notif->size == sizeof(u32)) {
-                               priv->last_noise =
-                                   (u8) (le32_to_cpu(notif->u.noise.value) &
-                                         0xff);
-                               average_add(&priv->average_noise,
-                                           priv->last_noise);
+                       if (size == sizeof(u32)) {
+                               priv->exp_avg_noise =
+                                   exponential_average(priv->exp_avg_noise,
+                                   (u8) (le32_to_cpu(notif->u.noise.value) & 0xff),
+                                   DEPTH_NOISE);
                                break;
                        }
 
                        IPW_ERROR
                            ("Noise stat is wrong size %d (should be %zd)\n",
-                            notif->size, sizeof(u32));
+                            size, sizeof(u32));
                        break;
                }
 
        default:
-               IPW_ERROR("Unknown notification: "
-                         "subtype=%d,flags=0x%2x,size=%d\n",
-                         notif->subtype, notif->flags, notif->size);
+               IPW_DEBUG_NOTIF("Unknown notification: "
+                               "subtype=%d,flags=0x%2x,size=%d\n",
+                               notif->subtype, notif->flags, size);
        }
 }
 
@@ -4256,43 +4888,43 @@ static int ipw_queue_reset(struct ipw_priv *priv)
        ipw_tx_queue_free(priv);
        /* Tx CMD queue */
        rc = ipw_queue_tx_init(priv, &priv->txq_cmd, nTxCmd,
-                              CX2_TX_CMD_QUEUE_READ_INDEX,
-                              CX2_TX_CMD_QUEUE_WRITE_INDEX,
-                              CX2_TX_CMD_QUEUE_BD_BASE,
-                              CX2_TX_CMD_QUEUE_BD_SIZE);
+                              IPW_TX_CMD_QUEUE_READ_INDEX,
+                              IPW_TX_CMD_QUEUE_WRITE_INDEX,
+                              IPW_TX_CMD_QUEUE_BD_BASE,
+                              IPW_TX_CMD_QUEUE_BD_SIZE);
        if (rc) {
                IPW_ERROR("Tx Cmd queue init failed\n");
                goto error;
        }
        /* Tx queue(s) */
        rc = ipw_queue_tx_init(priv, &priv->txq[0], nTx,
-                              CX2_TX_QUEUE_0_READ_INDEX,
-                              CX2_TX_QUEUE_0_WRITE_INDEX,
-                              CX2_TX_QUEUE_0_BD_BASE, CX2_TX_QUEUE_0_BD_SIZE);
+                              IPW_TX_QUEUE_0_READ_INDEX,
+                              IPW_TX_QUEUE_0_WRITE_INDEX,
+                              IPW_TX_QUEUE_0_BD_BASE, IPW_TX_QUEUE_0_BD_SIZE);
        if (rc) {
                IPW_ERROR("Tx 0 queue init failed\n");
                goto error;
        }
        rc = ipw_queue_tx_init(priv, &priv->txq[1], nTx,
-                              CX2_TX_QUEUE_1_READ_INDEX,
-                              CX2_TX_QUEUE_1_WRITE_INDEX,
-                              CX2_TX_QUEUE_1_BD_BASE, CX2_TX_QUEUE_1_BD_SIZE);
+                              IPW_TX_QUEUE_1_READ_INDEX,
+                              IPW_TX_QUEUE_1_WRITE_INDEX,
+                              IPW_TX_QUEUE_1_BD_BASE, IPW_TX_QUEUE_1_BD_SIZE);
        if (rc) {
                IPW_ERROR("Tx 1 queue init failed\n");
                goto error;
        }
        rc = ipw_queue_tx_init(priv, &priv->txq[2], nTx,
-                              CX2_TX_QUEUE_2_READ_INDEX,
-                              CX2_TX_QUEUE_2_WRITE_INDEX,
-                              CX2_TX_QUEUE_2_BD_BASE, CX2_TX_QUEUE_2_BD_SIZE);
+                              IPW_TX_QUEUE_2_READ_INDEX,
+                              IPW_TX_QUEUE_2_WRITE_INDEX,
+                              IPW_TX_QUEUE_2_BD_BASE, IPW_TX_QUEUE_2_BD_SIZE);
        if (rc) {
                IPW_ERROR("Tx 2 queue init failed\n");
                goto error;
        }
        rc = ipw_queue_tx_init(priv, &priv->txq[3], nTx,
-                              CX2_TX_QUEUE_3_READ_INDEX,
-                              CX2_TX_QUEUE_3_WRITE_INDEX,
-                              CX2_TX_QUEUE_3_BD_BASE, CX2_TX_QUEUE_3_BD_SIZE);
+                              IPW_TX_QUEUE_3_READ_INDEX,
+                              IPW_TX_QUEUE_3_WRITE_INDEX,
+                              IPW_TX_QUEUE_3_BD_BASE, IPW_TX_QUEUE_3_BD_SIZE);
        if (rc) {
                IPW_ERROR("Tx 3 queue init failed\n");
                goto error;
@@ -4310,7 +4942,7 @@ static int ipw_queue_reset(struct ipw_priv *priv)
 /**
  * Reclaim Tx queue entries no more used by NIC.
  *
- * When FW adwances 'R' index, all entries between old and
+ * When FW advances 'R' index, all entries between old and
  * new 'R' index need to be reclaimed. As result, some free space
  * forms. If there is enough free space (> low mark), wake Tx queue.
  *
@@ -4340,8 +4972,9 @@ static int ipw_queue_tx_reclaim(struct ipw_priv *priv,
                priv->tx_packets++;
        }
       done:
-       if (ipw_queue_space(q) > q->low_mark && qindex >= 0)
-               __maybe_wake_tx(priv);
+       if ((ipw_tx_queue_space(q) > q->low_mark) &&
+           (qindex >= 0))
+               netif_wake_queue(priv->net_dev);
        used = q->first_empty - q->last_used;
        if (used < 0)
                used += q->n_bd;
@@ -4356,7 +4989,7 @@ static int ipw_queue_tx_hcmd(struct ipw_priv *priv, int hcmd, void *buf,
        struct clx2_queue *q = &txq->q;
        struct tfd_frame *tfd;
 
-       if (ipw_queue_space(q) < (sync ? 1 : 2)) {
+       if (ipw_tx_queue_space(q) < (sync ? 1 : 2)) {
                IPW_ERROR("No space for Tx\n");
                return -EBUSY;
        }
@@ -4382,7 +5015,7 @@ static int ipw_queue_tx_hcmd(struct ipw_priv *priv, int hcmd, void *buf,
  * Rx theory of operation
  *
  * The host allocates 32 DMA target addresses and passes the host address
- * to the firmware at register CX2_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
+ * to the firmware at register IPW_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
  * 0 to 31
  *
  * Rx Queue Indexes
@@ -4461,12 +5094,12 @@ static void ipw_rx_queue_restock(struct ipw_priv *priv)
 
        spin_lock_irqsave(&rxq->lock, flags);
        write = rxq->write;
-       while ((rxq->write != rxq->processed) && (rxq->free_count)) {
+       while ((ipw_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
                element = rxq->rx_free.next;
                rxb = list_entry(element, struct ipw_rx_mem_buffer, list);
                list_del(element);
 
-               ipw_write32(priv, CX2_RFDS_TABLE_LOWER + rxq->write * RFD_SIZE,
+               ipw_write32(priv, IPW_RFDS_TABLE_LOWER + rxq->write * RFD_SIZE,
                            rxb->dma_addr);
                rxq->queue[rxq->write] = rxb;
                rxq->write = (rxq->write + 1) % RX_QUEUE_SIZE;
@@ -4481,7 +5114,7 @@ static void ipw_rx_queue_restock(struct ipw_priv *priv)
 
        /* If we've added more space for the firmware to place data, tell it */
        if (write != rxq->write)
-               ipw_write32(priv, CX2_RX_WRITE_INDEX, rxq->write);
+               ipw_write32(priv, IPW_RX_WRITE_INDEX, rxq->write);
 }
 
 /*
@@ -4502,7 +5135,7 @@ static void ipw_rx_queue_replenish(void *data)
        while (!list_empty(&rxq->rx_used)) {
                element = rxq->rx_used.next;
                rxb = list_entry(element, struct ipw_rx_mem_buffer, list);
-               rxb->skb = alloc_skb(CX2_RX_BUF_SIZE, GFP_ATOMIC);
+               rxb->skb = alloc_skb(IPW_RX_BUF_SIZE, GFP_ATOMIC);
                if (!rxb->skb) {
                        printk(KERN_CRIT "%s: Can not allocate SKB buffers.\n",
                               priv->net_dev->name);
@@ -4513,10 +5146,9 @@ static void ipw_rx_queue_replenish(void *data)
                }
                list_del(element);
 
-               rxb->rxb = (struct ipw_rx_buffer *)rxb->skb->data;
                rxb->dma_addr =
                    pci_map_single(priv->pci_dev, rxb->skb->data,
-                                  CX2_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
+                                  IPW_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
 
                list_add_tail(&rxb->list, &rxq->rx_free);
                rxq->free_count++;
@@ -4526,16 +5158,17 @@ static void ipw_rx_queue_replenish(void *data)
        ipw_rx_queue_restock(priv);
 }
 
-static void ipw_bg_rx_queue_replenish(void *data)
+static void ipw_bg_rx_queue_replenish(struct work_struct *work)
 {
-       struct ipw_priv *priv = data;
-       down(&priv->sem);
-       ipw_rx_queue_replenish(data);
-       up(&priv->sem);
+       struct ipw_priv *priv =
+               container_of(work, struct ipw_priv, rx_replenish);
+       mutex_lock(&priv->mutex);
+       ipw_rx_queue_replenish(priv);
+       mutex_unlock(&priv->mutex);
 }
 
 /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
- * If an SKB has been detached, the POOL needs to have it's SKB set to NULL
+ * If an SKB has been detached, the POOL needs to have its SKB set to NULL
  * This free routine walks the list of POOL entries and if SKB is set to
  * non NULL it is unmapped and freed
  */
@@ -4549,7 +5182,7 @@ static void ipw_rx_queue_free(struct ipw_priv *priv, struct ipw_rx_queue *rxq)
        for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
                if (rxq->pool[i].skb != NULL) {
                        pci_unmap_single(priv->pci_dev, rxq->pool[i].dma_addr,
-                                        CX2_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
+                                        IPW_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
                        dev_kfree_skb(rxq->pool[i].skb);
                }
        }
@@ -4562,12 +5195,11 @@ static struct ipw_rx_queue *ipw_rx_queue_alloc(struct ipw_priv *priv)
        struct ipw_rx_queue *rxq;
        int i;
 
-       rxq = (struct ipw_rx_queue *)kmalloc(sizeof(*rxq), GFP_KERNEL);
+       rxq = kzalloc(sizeof(*rxq), GFP_KERNEL);
        if (unlikely(!rxq)) {
                IPW_ERROR("memory allocation failed\n");
                return NULL;
        }
-       memset(rxq, 0, sizeof(*rxq));
        spin_lock_init(&rxq->lock);
        INIT_LIST_HEAD(&rxq->rx_free);
        INIT_LIST_HEAD(&rxq->rx_used);
@@ -4579,7 +5211,6 @@ static struct ipw_rx_queue *ipw_rx_queue_alloc(struct ipw_priv *priv)
        /* Set us so that we have processed and used all buffers, but have
         * not restocked the Rx queue with fresh buffers */
        rxq->read = rxq->write = 0;
-       rxq->processed = RX_QUEUE_SIZE - 1;
        rxq->free_count = 0;
 
        return rxq;
@@ -4714,7 +5345,7 @@ static int ipw_compatible_rates(struct ipw_priv *priv,
        return 1;
 }
 
-static inline void ipw_copy_rates(struct ipw_supported_rates *dest,
+static void ipw_copy_rates(struct ipw_supported_rates *dest,
                                  const struct ipw_supported_rates *src)
 {
        u8 i;
@@ -4799,25 +5430,27 @@ static int ipw_find_adhoc_network(struct ipw_priv *priv,
                                  int roaming)
 {
        struct ipw_supported_rates rates;
+       DECLARE_MAC_BUF(mac);
+       DECLARE_MAC_BUF(mac2);
 
        /* Verify that this network's capability is compatible with the
         * current mode (AdHoc or Infrastructure) */
        if ((priv->ieee->iw_mode == IW_MODE_ADHOC &&
             !(network->capability & WLAN_CAPABILITY_IBSS))) {
-               IPW_DEBUG_MERGE("Network '%s (" MAC_FMT ")' excluded due to "
+               IPW_DEBUG_MERGE("Network '%s (%s)' excluded due to "
                                "capability mismatch.\n",
                                escape_essid(network->ssid, network->ssid_len),
-                               MAC_ARG(network->bssid));
+                               print_mac(mac, network->bssid));
                return 0;
        }
 
        /* If we do not have an ESSID for this AP, we can not associate with
         * it */
        if (network->flags & NETWORK_EMPTY_ESSID) {
-               IPW_DEBUG_MERGE("Network '%s (" MAC_FMT ")' excluded "
+               IPW_DEBUG_MERGE("Network '%s (%s)' excluded "
                                "because of hidden ESSID.\n",
                                escape_essid(network->ssid, network->ssid_len),
-                               MAC_ARG(network->bssid));
+                               print_mac(mac, network->bssid));
                return 0;
        }
 
@@ -4827,11 +5460,11 @@ static int ipw_find_adhoc_network(struct ipw_priv *priv,
                if ((network->ssid_len != match->network->ssid_len) ||
                    memcmp(network->ssid, match->network->ssid,
                           network->ssid_len)) {
-                       IPW_DEBUG_MERGE("Netowrk '%s (" MAC_FMT ")' excluded "
+                       IPW_DEBUG_MERGE("Network '%s (%s)' excluded "
                                        "because of non-network ESSID.\n",
                                        escape_essid(network->ssid,
                                                     network->ssid_len),
-                                       MAC_ARG(network->bssid));
+                                       print_mac(mac, network->bssid));
                        return 0;
                }
        } else {
@@ -4842,12 +5475,13 @@ static int ipw_find_adhoc_network(struct ipw_priv *priv,
                     memcmp(network->ssid, priv->essid,
                            min(network->ssid_len, priv->essid_len)))) {
                        char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
+
                        strncpy(escaped,
                                escape_essid(network->ssid, network->ssid_len),
                                sizeof(escaped));
-                       IPW_DEBUG_MERGE("Network '%s (" MAC_FMT ")' excluded "
+                       IPW_DEBUG_MERGE("Network '%s (%s)' excluded "
                                        "because of ESSID mismatch: '%s'.\n",
-                                       escaped, MAC_ARG(network->bssid),
+                                       escaped, print_mac(mac, network->bssid),
                                        escape_essid(priv->essid,
                                                     priv->essid_len));
                        return 0;
@@ -4858,36 +5492,37 @@ static int ipw_find_adhoc_network(struct ipw_priv *priv,
         * testing everything else. */
 
        if (network->time_stamp[0] < match->network->time_stamp[0]) {
-               IPW_DEBUG_MERGE
-                   ("Network '%s excluded because newer than current network.\n",
-                    escape_essid(match->network->ssid,
-                                 match->network->ssid_len));
+               IPW_DEBUG_MERGE("Network '%s excluded because newer than "
+                               "current network.\n",
+                               escape_essid(match->network->ssid,
+                                            match->network->ssid_len));
                return 0;
        } else if (network->time_stamp[1] < match->network->time_stamp[1]) {
-               IPW_DEBUG_MERGE
-                   ("Network '%s excluded because newer than current network.\n",
-                    escape_essid(match->network->ssid,
-                                 match->network->ssid_len));
+               IPW_DEBUG_MERGE("Network '%s excluded because newer than "
+                               "current network.\n",
+                               escape_essid(match->network->ssid,
+                                            match->network->ssid_len));
                return 0;
        }
 
        /* Now go through and see if the requested network is valid... */
        if (priv->ieee->scan_age != 0 &&
            time_after(jiffies, network->last_scanned + priv->ieee->scan_age)) {
-               IPW_DEBUG_MERGE("Network '%s (" MAC_FMT ")' excluded "
-                               "because of age: %lums.\n",
+               IPW_DEBUG_MERGE("Network '%s (%s)' excluded "
+                               "because of age: %ums.\n",
                                escape_essid(network->ssid, network->ssid_len),
-                               MAC_ARG(network->bssid),
-                               (jiffies - network->last_scanned) / (HZ / 100));
+                               print_mac(mac, network->bssid),
+                               jiffies_to_msecs(jiffies -
+                                                network->last_scanned));
                return 0;
        }
 
        if ((priv->config & CFG_STATIC_CHANNEL) &&
            (network->channel != priv->channel)) {
-               IPW_DEBUG_MERGE("Network '%s (" MAC_FMT ")' excluded "
+               IPW_DEBUG_MERGE("Network '%s (%s)' excluded "
                                "because of channel mismatch: %d != %d.\n",
                                escape_essid(network->ssid, network->ssid_len),
-                               MAC_ARG(network->bssid),
+                               print_mac(mac, network->bssid),
                                network->channel, priv->channel);
                return 0;
        }
@@ -4895,52 +5530,54 @@ static int ipw_find_adhoc_network(struct ipw_priv *priv,
        /* Verify privacy compatability */
        if (((priv->capability & CAP_PRIVACY_ON) ? 1 : 0) !=
            ((network->capability & WLAN_CAPABILITY_PRIVACY) ? 1 : 0)) {
-               IPW_DEBUG_MERGE("Network '%s (" MAC_FMT ")' excluded "
+               IPW_DEBUG_MERGE("Network '%s (%s)' excluded "
                                "because of privacy mismatch: %s != %s.\n",
                                escape_essid(network->ssid, network->ssid_len),
-                               MAC_ARG(network->bssid),
-                               priv->capability & CAP_PRIVACY_ON ? "on" :
-                               "off",
-                               network->capability &
-                               WLAN_CAPABILITY_PRIVACY ? "on" : "off");
+                               print_mac(mac, network->bssid),
+                               priv->
+                               capability & CAP_PRIVACY_ON ? "on" : "off",
+                               network->
+                               capability & WLAN_CAPABILITY_PRIVACY ? "on" :
+                               "off");
                return 0;
        }
 
        if (!memcmp(network->bssid, priv->bssid, ETH_ALEN)) {
-               IPW_DEBUG_MERGE("Network '%s (" MAC_FMT ")' excluded "
-                               "because of the same BSSID match: " MAC_FMT
+               IPW_DEBUG_MERGE("Network '%s (%s)' excluded "
+                               "because of the same BSSID match: %s"
                                ".\n", escape_essid(network->ssid,
                                                    network->ssid_len),
-                               MAC_ARG(network->bssid), MAC_ARG(priv->bssid));
+                               print_mac(mac, network->bssid),
+                               print_mac(mac2, priv->bssid));
                return 0;
        }
 
        /* Filter out any incompatible freq / mode combinations */
        if (!ieee80211_is_valid_mode(priv->ieee, network->mode)) {
-               IPW_DEBUG_MERGE("Network '%s (" MAC_FMT ")' excluded "
+               IPW_DEBUG_MERGE("Network '%s (%s)' excluded "
                                "because of invalid frequency/mode "
                                "combination.\n",
                                escape_essid(network->ssid, network->ssid_len),
-                               MAC_ARG(network->bssid));
+                               print_mac(mac, network->bssid));
                return 0;
        }
 
        /* Ensure that the rates supported by the driver are compatible with
         * this AP, including verification of basic rates (mandatory) */
        if (!ipw_compatible_rates(priv, network, &rates)) {
-               IPW_DEBUG_MERGE("Network '%s (" MAC_FMT ")' excluded "
+               IPW_DEBUG_MERGE("Network '%s (%s)' excluded "
                                "because configured rate mask excludes "
                                "AP mandatory rate.\n",
                                escape_essid(network->ssid, network->ssid_len),
-                               MAC_ARG(network->bssid));
+                               print_mac(mac, network->bssid));
                return 0;
        }
 
        if (rates.num_rates == 0) {
-               IPW_DEBUG_MERGE("Network '%s (" MAC_FMT ")' excluded "
+               IPW_DEBUG_MERGE("Network '%s (%s)' excluded "
                                "because of no compatible rates.\n",
                                escape_essid(network->ssid, network->ssid_len),
-                               MAC_ARG(network->bssid));
+                               print_mac(mac, network->bssid));
                return 0;
        }
 
@@ -4951,23 +5588,24 @@ static int ipw_find_adhoc_network(struct ipw_priv *priv,
        /* Set up 'new' AP to this network */
        ipw_copy_rates(&match->rates, &rates);
        match->network = network;
-       IPW_DEBUG_MERGE("Network '%s (" MAC_FMT ")' is a viable match.\n",
+       IPW_DEBUG_MERGE("Network '%s (%s)' is a viable match.\n",
                        escape_essid(network->ssid, network->ssid_len),
-                       MAC_ARG(network->bssid));
+                       print_mac(mac, network->bssid));
 
        return 1;
 }
 
-static void ipw_merge_adhoc_network(void *data)
+static void ipw_merge_adhoc_network(struct work_struct *work)
 {
-       struct ipw_priv *priv = data;
+       struct ipw_priv *priv =
+               container_of(work, struct ipw_priv, merge_networks);
        struct ieee80211_network *network = NULL;
        struct ipw_network_match match = {
                .network = priv->assoc_network
        };
 
-       if ((priv->status & STATUS_ASSOCIATED)
-           && (priv->ieee->iw_mode == IW_MODE_ADHOC)) {
+       if ((priv->status & STATUS_ASSOCIATED) &&
+           (priv->ieee->iw_mode == IW_MODE_ADHOC)) {
                /* First pass through ROAM process -- look for a better
                 * network */
                unsigned long flags;
@@ -4986,7 +5624,7 @@ static void ipw_merge_adhoc_network(void *data)
                        return;
                }
 
-               down(&priv->sem);
+               mutex_lock(&priv->mutex);
                if ((priv->ieee->iw_mode == IW_MODE_ADHOC)) {
                        IPW_DEBUG_MERGE("remove network %s\n",
                                        escape_essid(priv->essid,
@@ -4996,10 +5634,9 @@ static void ipw_merge_adhoc_network(void *data)
 
                ipw_disassociate(priv);
                priv->assoc_network = match.network;
-               up(&priv->sem);
+               mutex_unlock(&priv->mutex);
                return;
        }
-
 }
 
 static int ipw_best_network(struct ipw_priv *priv,
@@ -5007,6 +5644,7 @@ static int ipw_best_network(struct ipw_priv *priv,
                            struct ieee80211_network *network, int roaming)
 {
        struct ipw_supported_rates rates;
+       DECLARE_MAC_BUF(mac);
 
        /* Verify that this network's capability is compatible with the
         * current mode (AdHoc or Infrastructure) */
@@ -5014,20 +5652,20 @@ static int ipw_best_network(struct ipw_priv *priv,
             !(network->capability & WLAN_CAPABILITY_ESS)) ||
            (priv->ieee->iw_mode == IW_MODE_ADHOC &&
             !(network->capability & WLAN_CAPABILITY_IBSS))) {
-               IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded due to "
+               IPW_DEBUG_ASSOC("Network '%s (%s)' excluded due to "
                                "capability mismatch.\n",
                                escape_essid(network->ssid, network->ssid_len),
-                               MAC_ARG(network->bssid));
+                               print_mac(mac, network->bssid));
                return 0;
        }
 
        /* If we do not have an ESSID for this AP, we can not associate with
         * it */
        if (network->flags & NETWORK_EMPTY_ESSID) {
-               IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
+               IPW_DEBUG_ASSOC("Network '%s (%s)' excluded "
                                "because of hidden ESSID.\n",
                                escape_essid(network->ssid, network->ssid_len),
-                               MAC_ARG(network->bssid));
+                               print_mac(mac, network->bssid));
                return 0;
        }
 
@@ -5037,11 +5675,11 @@ static int ipw_best_network(struct ipw_priv *priv,
                if ((network->ssid_len != match->network->ssid_len) ||
                    memcmp(network->ssid, match->network->ssid,
                           network->ssid_len)) {
-                       IPW_DEBUG_ASSOC("Netowrk '%s (" MAC_FMT ")' excluded "
+                       IPW_DEBUG_ASSOC("Network '%s (%s)' excluded "
                                        "because of non-network ESSID.\n",
                                        escape_essid(network->ssid,
                                                     network->ssid_len),
-                                       MAC_ARG(network->bssid));
+                                       print_mac(mac, network->bssid));
                        return 0;
                }
        } else {
@@ -5055,9 +5693,9 @@ static int ipw_best_network(struct ipw_priv *priv,
                        strncpy(escaped,
                                escape_essid(network->ssid, network->ssid_len),
                                sizeof(escaped));
-                       IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
+                       IPW_DEBUG_ASSOC("Network '%s (%s)' excluded "
                                        "because of ESSID mismatch: '%s'.\n",
-                                       escaped, MAC_ARG(network->bssid),
+                                       escaped, print_mac(mac, network->bssid),
                                        escape_essid(priv->essid,
                                                     priv->essid_len));
                        return 0;
@@ -5071,12 +5709,12 @@ static int ipw_best_network(struct ipw_priv *priv,
                strncpy(escaped,
                        escape_essid(network->ssid, network->ssid_len),
                        sizeof(escaped));
-               IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded because "
-                               "'%s (" MAC_FMT ")' has a stronger signal.\n",
-                               escaped, MAC_ARG(network->bssid),
+               IPW_DEBUG_ASSOC("Network '%s (%s)' excluded because "
+                               "'%s (%s)' has a stronger signal.\n",
+                               escaped, print_mac(mac, network->bssid),
                                escape_essid(match->network->ssid,
                                             match->network->ssid_len),
-                               MAC_ARG(match->network->bssid));
+                               print_mac(mac, match->network->bssid));
                return 0;
        }
 
@@ -5084,32 +5722,34 @@ static int ipw_best_network(struct ipw_priv *priv,
         * last 3 seconds, do not try and associate again... */
        if (network->last_associate &&
            time_after(network->last_associate + (HZ * 3UL), jiffies)) {
-               IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
-                               "because of storming (%lu since last "
+               IPW_DEBUG_ASSOC("Network '%s (%s)' excluded "
+                               "because of storming (%ums since last "
                                "assoc attempt).\n",
                                escape_essid(network->ssid, network->ssid_len),
-                               MAC_ARG(network->bssid),
-                               (jiffies - network->last_associate) / HZ);
+                               print_mac(mac, network->bssid),
+                               jiffies_to_msecs(jiffies -
+                                                network->last_associate));
                return 0;
        }
 
        /* Now go through and see if the requested network is valid... */
        if (priv->ieee->scan_age != 0 &&
            time_after(jiffies, network->last_scanned + priv->ieee->scan_age)) {
-               IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
-                               "because of age: %lums.\n",
+               IPW_DEBUG_ASSOC("Network '%s (%s)' excluded "
+                               "because of age: %ums.\n",
                                escape_essid(network->ssid, network->ssid_len),
-                               MAC_ARG(network->bssid),
-                               (jiffies - network->last_scanned) / (HZ / 100));
+                               print_mac(mac, network->bssid),
+                               jiffies_to_msecs(jiffies -
+                                                network->last_scanned));
                return 0;
        }
 
        if ((priv->config & CFG_STATIC_CHANNEL) &&
            (network->channel != priv->channel)) {
-               IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
+               IPW_DEBUG_ASSOC("Network '%s (%s)' excluded "
                                "because of channel mismatch: %d != %d.\n",
                                escape_essid(network->ssid, network->ssid_len),
-                               MAC_ARG(network->bssid),
+                               print_mac(mac, network->bssid),
                                network->channel, priv->channel);
                return 0;
        }
@@ -5117,10 +5757,10 @@ static int ipw_best_network(struct ipw_priv *priv,
        /* Verify privacy compatability */
        if (((priv->capability & CAP_PRIVACY_ON) ? 1 : 0) !=
            ((network->capability & WLAN_CAPABILITY_PRIVACY) ? 1 : 0)) {
-               IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
+               IPW_DEBUG_ASSOC("Network '%s (%s)' excluded "
                                "because of privacy mismatch: %s != %s.\n",
                                escape_essid(network->ssid, network->ssid_len),
-                               MAC_ARG(network->bssid),
+                               print_mac(mac, network->bssid),
                                priv->capability & CAP_PRIVACY_ON ? "on" :
                                "off",
                                network->capability &
@@ -5130,39 +5770,48 @@ static int ipw_best_network(struct ipw_priv *priv,
 
        if ((priv->config & CFG_STATIC_BSSID) &&
            memcmp(network->bssid, priv->bssid, ETH_ALEN)) {
-               IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
-                               "because of BSSID mismatch: " MAC_FMT ".\n",
+               IPW_DEBUG_ASSOC("Network '%s (%s)' excluded "
+                               "because of BSSID mismatch: %s.\n",
                                escape_essid(network->ssid, network->ssid_len),
-                               MAC_ARG(network->bssid), MAC_ARG(priv->bssid));
+                               print_mac(mac, network->bssid), print_mac(mac, priv->bssid));
                return 0;
        }
 
        /* Filter out any incompatible freq / mode combinations */
        if (!ieee80211_is_valid_mode(priv->ieee, network->mode)) {
-               IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
+               IPW_DEBUG_ASSOC("Network '%s (%s)' excluded "
                                "because of invalid frequency/mode "
                                "combination.\n",
                                escape_essid(network->ssid, network->ssid_len),
-                               MAC_ARG(network->bssid));
+                               print_mac(mac, network->bssid));
+               return 0;
+       }
+
+       /* Filter out invalid channel in current GEO */
+       if (!ieee80211_is_valid_channel(priv->ieee, network->channel)) {
+               IPW_DEBUG_ASSOC("Network '%s (%s)' excluded "
+                               "because of invalid channel in current GEO\n",
+                               escape_essid(network->ssid, network->ssid_len),
+                               print_mac(mac, network->bssid));
                return 0;
        }
 
        /* Ensure that the rates supported by the driver are compatible with
         * this AP, including verification of basic rates (mandatory) */
        if (!ipw_compatible_rates(priv, network, &rates)) {
-               IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
+               IPW_DEBUG_ASSOC("Network '%s (%s)' excluded "
                                "because configured rate mask excludes "
                                "AP mandatory rate.\n",
                                escape_essid(network->ssid, network->ssid_len),
-                               MAC_ARG(network->bssid));
+                               print_mac(mac, network->bssid));
                return 0;
        }
 
        if (rates.num_rates == 0) {
-               IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
+               IPW_DEBUG_ASSOC("Network '%s (%s)' excluded "
                                "because of no compatible rates.\n",
                                escape_essid(network->ssid, network->ssid_len),
-                               MAC_ARG(network->bssid));
+                               print_mac(mac, network->bssid));
                return 0;
        }
 
@@ -5174,9 +5823,9 @@ static int ipw_best_network(struct ipw_priv *priv,
        ipw_copy_rates(&match->rates, &rates);
        match->network = network;
 
-       IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' is a viable match.\n",
+       IPW_DEBUG_ASSOC("Network '%s (%s)' is a viable match.\n",
                        escape_essid(network->ssid, network->ssid_len),
-                       MAC_ARG(network->bssid));
+                       print_mac(mac, network->bssid));
 
        return 1;
 }
@@ -5184,6 +5833,9 @@ static int ipw_best_network(struct ipw_priv *priv,
 static void ipw_adhoc_create(struct ipw_priv *priv,
                             struct ieee80211_network *network)
 {
+       const struct ieee80211_geo *geo = ieee80211_get_geo(priv->ieee);
+       int i;
+
        /*
         * For the purposes of scanning, we can set our wireless mode
         * to trigger scans across combinations of bands, but when it
@@ -5194,20 +5846,45 @@ static void ipw_adhoc_create(struct ipw_priv *priv,
         * chossen band.  Attempting to create a new ad-hoc network
         * with an invalid channel for wireless mode will trigger a
         * FW fatal error.
+        *
         */
-       network->mode = is_valid_channel(priv->ieee->mode, priv->channel);
-       if (!network->mode) {
+       switch (ieee80211_is_valid_channel(priv->ieee, priv->channel)) {
+       case IEEE80211_52GHZ_BAND:
+               network->mode = IEEE_A;
+               i = ieee80211_channel_to_index(priv->ieee, priv->channel);
+               BUG_ON(i == -1);
+               if (geo->a[i].flags & IEEE80211_CH_PASSIVE_ONLY) {
+                       IPW_WARNING("Overriding invalid channel\n");
+                       priv->channel = geo->a[0].channel;
+               }
+               break;
+
+       case IEEE80211_24GHZ_BAND:
+               if (priv->ieee->mode & IEEE_G)
+                       network->mode = IEEE_G;
+               else
+                       network->mode = IEEE_B;
+               i = ieee80211_channel_to_index(priv->ieee, priv->channel);
+               BUG_ON(i == -1);
+               if (geo->bg[i].flags & IEEE80211_CH_PASSIVE_ONLY) {
+                       IPW_WARNING("Overriding invalid channel\n");
+                       priv->channel = geo->bg[0].channel;
+               }
+               break;
+
+       default:
                IPW_WARNING("Overriding invalid channel\n");
                if (priv->ieee->mode & IEEE_A) {
                        network->mode = IEEE_A;
-                       priv->channel = band_a_active_channel[0];
+                       priv->channel = geo->a[0].channel;
                } else if (priv->ieee->mode & IEEE_G) {
                        network->mode = IEEE_G;
-                       priv->channel = band_b_active_channel[0];
+                       priv->channel = geo->bg[0].channel;
                } else {
                        network->mode = IEEE_B;
-                       priv->channel = band_b_active_channel[0];
+                       priv->channel = geo->bg[0].channel;
                }
+               break;
        }
 
        network->channel = priv->channel;
@@ -5239,32 +5916,124 @@ static void ipw_adhoc_create(struct ipw_priv *priv,
        network->rsn_ie_len = 0;
 }
 
-static void ipw_send_wep_keys(struct ipw_priv *priv)
+static void ipw_send_tgi_tx_key(struct ipw_priv *priv, int type, int index)
+{
+       struct ipw_tgi_tx_key key;
+
+       if (!(priv->ieee->sec.flags & (1 << index)))
+               return;
+
+       key.key_id = index;
+       memcpy(key.key, priv->ieee->sec.keys[index], SCM_TEMPORAL_KEY_LENGTH);
+       key.security_type = type;
+       key.station_index = 0;  /* always 0 for BSS */
+       key.flags = 0;
+       /* 0 for new key; previous value of counter (after fatal error) */
+       key.tx_counter[0] = cpu_to_le32(0);
+       key.tx_counter[1] = cpu_to_le32(0);
+
+       ipw_send_cmd_pdu(priv, IPW_CMD_TGI_TX_KEY, sizeof(key), &key);
+}
+
+static void ipw_send_wep_keys(struct ipw_priv *priv, int type)
 {
-       struct ipw_wep_key *key;
+       struct ipw_wep_key key;
        int i;
-       struct host_cmd cmd = {
-               .cmd = IPW_CMD_WEP_KEY,
-               .len = sizeof(*key)
-       };
 
-       key = (struct ipw_wep_key *)&cmd.param;
-       key->cmd_id = DINO_CMD_WEP_KEY;
-       key->seq_num = 0;
+       key.cmd_id = DINO_CMD_WEP_KEY;
+       key.seq_num = 0;
 
+       /* Note: AES keys cannot be set for multiple times.
+        * Only set it at the first time. */
        for (i = 0; i < 4; i++) {
-               key->key_index = i;
-               if (!(priv->sec.flags & (1 << i)))
-                       key->key_size = 0;
-               else {
-                       key->key_size = priv->sec.key_sizes[i];
-                       memcpy(key->key, priv->sec.keys[i], key->key_size);
+               key.key_index = i | type;
+               if (!(priv->ieee->sec.flags & (1 << i))) {
+                       key.key_size = 0;
+                       continue;
                }
 
-               if (ipw_send_cmd(priv, &cmd)) {
-                       IPW_ERROR("failed to send WEP_KEY command\n");
-                       return;
-               }
+               key.key_size = priv->ieee->sec.key_sizes[i];
+               memcpy(key.key, priv->ieee->sec.keys[i], key.key_size);
+
+               ipw_send_cmd_pdu(priv, IPW_CMD_WEP_KEY, sizeof(key), &key);
+       }
+}
+
+static void ipw_set_hw_decrypt_unicast(struct ipw_priv *priv, int level)
+{
+       if (priv->ieee->host_encrypt)
+               return;
+
+       switch (level) {
+       case SEC_LEVEL_3:
+               priv->sys_config.disable_unicast_decryption = 0;
+               priv->ieee->host_decrypt = 0;
+               break;
+       case SEC_LEVEL_2:
+               priv->sys_config.disable_unicast_decryption = 1;
+               priv->ieee->host_decrypt = 1;
+               break;
+       case SEC_LEVEL_1:
+               priv->sys_config.disable_unicast_decryption = 0;
+               priv->ieee->host_decrypt = 0;
+               break;
+       case SEC_LEVEL_0:
+               priv->sys_config.disable_unicast_decryption = 1;
+               break;
+       default:
+               break;
+       }
+}
+
+static void ipw_set_hw_decrypt_multicast(struct ipw_priv *priv, int level)
+{
+       if (priv->ieee->host_encrypt)
+               return;
+
+       switch (level) {
+       case SEC_LEVEL_3:
+               priv->sys_config.disable_multicast_decryption = 0;
+               break;
+       case SEC_LEVEL_2:
+               priv->sys_config.disable_multicast_decryption = 1;
+               break;
+       case SEC_LEVEL_1:
+               priv->sys_config.disable_multicast_decryption = 0;
+               break;
+       case SEC_LEVEL_0:
+               priv->sys_config.disable_multicast_decryption = 1;
+               break;
+       default:
+               break;
+       }
+}
+
+static void ipw_set_hwcrypto_keys(struct ipw_priv *priv)
+{
+       switch (priv->ieee->sec.level) {
+       case SEC_LEVEL_3:
+               if (priv->ieee->sec.flags & SEC_ACTIVE_KEY)
+                       ipw_send_tgi_tx_key(priv,
+                                           DCT_FLAG_EXT_SECURITY_CCM,
+                                           priv->ieee->sec.active_key);
+
+               if (!priv->ieee->host_mc_decrypt)
+                       ipw_send_wep_keys(priv, DCW_WEP_KEY_SEC_TYPE_CCM);
+               break;
+       case SEC_LEVEL_2:
+               if (priv->ieee->sec.flags & SEC_ACTIVE_KEY)
+                       ipw_send_tgi_tx_key(priv,
+                                           DCT_FLAG_EXT_SECURITY_TKIP,
+                                           priv->ieee->sec.active_key);
+               break;
+       case SEC_LEVEL_1:
+               ipw_send_wep_keys(priv, DCW_WEP_KEY_SEC_TYPE_WEP);
+               ipw_set_hw_decrypt_unicast(priv, priv->ieee->sec.level);
+               ipw_set_hw_decrypt_multicast(priv, priv->ieee->sec.level);
+               break;
+       case SEC_LEVEL_0:
+       default:
+               break;
        }
 }
 
@@ -5272,29 +6041,33 @@ static void ipw_adhoc_check(void *data)
 {
        struct ipw_priv *priv = data;
 
-       if (priv->missed_adhoc_beacons++ > priv->missed_beacon_threshold &&
+       if (priv->missed_adhoc_beacons++ > priv->disassociate_threshold &&
            !(priv->config & CFG_ADHOC_PERSIST)) {
-               IPW_DEBUG_SCAN("Disassociating due to missed beacons\n");
+               IPW_DEBUG(IPW_DL_INFO | IPW_DL_NOTIF |
+                         IPW_DL_STATE | IPW_DL_ASSOC,
+                         "Missed beacon: %d - disassociate\n",
+                         priv->missed_adhoc_beacons);
                ipw_remove_current_network(priv);
                ipw_disassociate(priv);
                return;
        }
 
        queue_delayed_work(priv->workqueue, &priv->adhoc_check,
-                          priv->assoc_request.beacon_interval);
+                          le16_to_cpu(priv->assoc_request.beacon_interval));
 }
 
-static void ipw_bg_adhoc_check(void *data)
+static void ipw_bg_adhoc_check(struct work_struct *work)
 {
-       struct ipw_priv *priv = data;
-       down(&priv->sem);
-       ipw_adhoc_check(data);
-       up(&priv->sem);
+       struct ipw_priv *priv =
+               container_of(work, struct ipw_priv, adhoc_check.work);
+       mutex_lock(&priv->mutex);
+       ipw_adhoc_check(priv);
+       mutex_unlock(&priv->mutex);
 }
 
-#ifdef CONFIG_IPW_DEBUG
 static void ipw_debug_config(struct ipw_priv *priv)
 {
+       DECLARE_MAC_BUF(mac);
        IPW_DEBUG_INFO("Scan completed, no valid APs matched "
                       "[CFG 0x%08X]\n", priv->config);
        if (priv->config & CFG_STATIC_CHANNEL)
@@ -5307,8 +6080,8 @@ static void ipw_debug_config(struct ipw_priv *priv)
        else
                IPW_DEBUG_INFO("ESSID unlocked.\n");
        if (priv->config & CFG_STATIC_BSSID)
-               IPW_DEBUG_INFO("BSSID locked to " MAC_FMT "\n",
-                              MAC_ARG(priv->bssid));
+               IPW_DEBUG_INFO("BSSID locked to %s\n",
+                              print_mac(mac, priv->bssid));
        else
                IPW_DEBUG_INFO("BSSID unlocked.\n");
        if (priv->capability & CAP_PRIVACY_ON)
@@ -5317,12 +6090,8 @@ static void ipw_debug_config(struct ipw_priv *priv)
                IPW_DEBUG_INFO("PRIVACY off\n");
        IPW_DEBUG_INFO("RATE MASK: 0x%08X\n", priv->rates_mask);
 }
-#else
-#define ipw_debug_config(x) do {} while (0)
-#endif
 
-static inline void ipw_set_fixed_rate(struct ipw_priv *priv,
-                                     struct ieee80211_network *network)
+static void ipw_set_fixed_rate(struct ipw_priv *priv, int mode)
 {
        /* TODO: Verify that this works... */
        struct ipw_fixed_rate fr = {
@@ -5350,7 +6119,7 @@ static inline void ipw_set_fixed_rate(struct ipw_priv *priv,
 
        default:                /* 2.4Ghz or Mixed */
                /* IEEE_B */
-               if (network->mode == IEEE_B) {
+               if (mode == IEEE_B) {
                        if (fr.tx_rates & ~IEEE80211_CCK_RATES_MASK) {
                                /* Invalid fixed rate mask */
                                IPW_DEBUG_WX
@@ -5408,245 +6177,320 @@ static void ipw_abort_scan(struct ipw_priv *priv)
                IPW_DEBUG_HC("Request to abort scan failed.\n");
 }
 
-static int ipw_request_scan(struct ipw_priv *priv)
+static void ipw_add_scan_channels(struct ipw_priv *priv,
+                                 struct ipw_scan_request_ext *scan,
+                                 int scan_type)
 {
-       struct ipw_scan_request_ext scan;
        int channel_index = 0;
-       int i, err, scan_type;
+       const struct ieee80211_geo *geo;
+       int i;
+
+       geo = ieee80211_get_geo(priv->ieee);
+
+       if (priv->ieee->freq_band & IEEE80211_52GHZ_BAND) {
+               int start = channel_index;
+               for (i = 0; i < geo->a_channels; i++) {
+                       if ((priv->status & STATUS_ASSOCIATED) &&
+                           geo->a[i].channel == priv->channel)
+                               continue;
+                       channel_index++;
+                       scan->channels_list[channel_index] = geo->a[i].channel;
+                       ipw_set_scan_type(scan, channel_index,
+                                         geo->a[i].
+                                         flags & IEEE80211_CH_PASSIVE_ONLY ?
+                                         IPW_SCAN_PASSIVE_FULL_DWELL_SCAN :
+                                         scan_type);
+               }
+
+               if (start != channel_index) {
+                       scan->channels_list[start] = (u8) (IPW_A_MODE << 6) |
+                           (channel_index - start);
+                       channel_index++;
+               }
+       }
+
+       if (priv->ieee->freq_band & IEEE80211_24GHZ_BAND) {
+               int start = channel_index;
+               if (priv->config & CFG_SPEED_SCAN) {
+                       int index;
+                       u8 channels[IEEE80211_24GHZ_CHANNELS] = {
+                               /* nop out the list */
+                               [0] = 0
+                       };
+
+                       u8 channel;
+                       while (channel_index < IPW_SCAN_CHANNELS) {
+                               channel =
+                                   priv->speed_scan[priv->speed_scan_pos];
+                               if (channel == 0) {
+                                       priv->speed_scan_pos = 0;
+                                       channel = priv->speed_scan[0];
+                               }
+                               if ((priv->status & STATUS_ASSOCIATED) &&
+                                   channel == priv->channel) {
+                                       priv->speed_scan_pos++;
+                                       continue;
+                               }
+
+                               /* If this channel has already been
+                                * added in scan, break from loop
+                                * and this will be the first channel
+                                * in the next scan.
+                                */
+                               if (channels[channel - 1] != 0)
+                                       break;
+
+                               channels[channel - 1] = 1;
+                               priv->speed_scan_pos++;
+                               channel_index++;
+                               scan->channels_list[channel_index] = channel;
+                               index =
+                                   ieee80211_channel_to_index(priv->ieee, channel);
+                               ipw_set_scan_type(scan, channel_index,
+                                                 geo->bg[index].
+                                                 flags &
+                                                 IEEE80211_CH_PASSIVE_ONLY ?
+                                                 IPW_SCAN_PASSIVE_FULL_DWELL_SCAN
+                                                 : scan_type);
+                       }
+               } else {
+                       for (i = 0; i < geo->bg_channels; i++) {
+                               if ((priv->status & STATUS_ASSOCIATED) &&
+                                   geo->bg[i].channel == priv->channel)
+                                       continue;
+                               channel_index++;
+                               scan->channels_list[channel_index] =
+                                   geo->bg[i].channel;
+                               ipw_set_scan_type(scan, channel_index,
+                                                 geo->bg[i].
+                                                 flags &
+                                                 IEEE80211_CH_PASSIVE_ONLY ?
+                                                 IPW_SCAN_PASSIVE_FULL_DWELL_SCAN
+                                                 : scan_type);
+                       }
+               }
+
+               if (start != channel_index) {
+                       scan->channels_list[start] = (u8) (IPW_B_MODE << 6) |
+                           (channel_index - start);
+               }
+       }
+}
+
+static int ipw_request_scan_helper(struct ipw_priv *priv, int type, int direct)
+{
+       struct ipw_scan_request_ext scan;
+       int err = 0, scan_type;
 
-       if (priv->status & STATUS_EXIT_PENDING) {
-               IPW_DEBUG_SCAN("Aborting scan due to device shutdown\n");
-               priv->status |= STATUS_SCAN_PENDING;
+       if (!(priv->status & STATUS_INIT) ||
+           (priv->status & STATUS_EXIT_PENDING))
                return 0;
+
+       mutex_lock(&priv->mutex);
+
+       if (direct && (priv->direct_scan_ssid_len == 0)) {
+               IPW_DEBUG_HC("Direct scan requested but no SSID to scan for\n");
+               priv->status &= ~STATUS_DIRECT_SCAN_PENDING;
+               goto done;
        }
 
        if (priv->status & STATUS_SCANNING) {
-               IPW_DEBUG_HC("Concurrent scan requested.  Ignoring.\n");
-//              IPW_DEBUG_HC("Concurrent scan requested.  Aborting first.\n");
-               priv->status |= STATUS_SCAN_PENDING;
-//              ipw_abort_scan(priv);
-               return 0;
+               IPW_DEBUG_HC("Concurrent scan requested.  Queuing.\n");
+               priv->status |= direct ? STATUS_DIRECT_SCAN_PENDING :
+                                       STATUS_SCAN_PENDING;
+               goto done;
        }
 
-       if (priv->status & STATUS_SCAN_ABORTING) {
+       if (!(priv->status & STATUS_SCAN_FORCED) &&
+           priv->status & STATUS_SCAN_ABORTING) {
                IPW_DEBUG_HC("Scan request while abort pending.  Queuing.\n");
-               priv->status |= STATUS_SCAN_PENDING;
-               return 0;
+               priv->status |= direct ? STATUS_DIRECT_SCAN_PENDING :
+                                       STATUS_SCAN_PENDING;
+               goto done;
        }
 
        if (priv->status & STATUS_RF_KILL_MASK) {
-               IPW_DEBUG_HC("Aborting scan due to RF Kill activation\n");
-               priv->status |= STATUS_SCAN_PENDING;
-               return 0;
+               IPW_DEBUG_HC("Queuing scan due to RF Kill activation\n");
+               priv->status |= direct ? STATUS_DIRECT_SCAN_PENDING :
+                                       STATUS_SCAN_PENDING;
+               goto done;
        }
 
        memset(&scan, 0, sizeof(scan));
+       scan.full_scan_index = cpu_to_le32(ieee80211_get_scans(priv->ieee));
+
+       if (type == IW_SCAN_TYPE_PASSIVE) {
+               IPW_DEBUG_WX("use passive scanning\n");
+               scan_type = IPW_SCAN_PASSIVE_FULL_DWELL_SCAN;
+               scan.dwell_time[IPW_SCAN_PASSIVE_FULL_DWELL_SCAN] =
+                       cpu_to_le16(120);
+               ipw_add_scan_channels(priv, &scan, scan_type);
+               goto send_request;
+       }
+
+       /* Use active scan by default. */
+       if (priv->config & CFG_SPEED_SCAN)
+               scan.dwell_time[IPW_SCAN_ACTIVE_BROADCAST_SCAN] =
+                       cpu_to_le16(30);
+       else
+               scan.dwell_time[IPW_SCAN_ACTIVE_BROADCAST_SCAN] =
+                       cpu_to_le16(20);
 
-       scan.dwell_time[IPW_SCAN_ACTIVE_BROADCAST_SCAN] = cpu_to_le16(20);
        scan.dwell_time[IPW_SCAN_ACTIVE_BROADCAST_AND_DIRECT_SCAN] =
-           cpu_to_le16(20);
-       scan.dwell_time[IPW_SCAN_PASSIVE_FULL_DWELL_SCAN] = cpu_to_le16(20);
+               cpu_to_le16(20);
 
-       scan.full_scan_index = cpu_to_le32(ieee80211_get_scans(priv->ieee));
+       scan.dwell_time[IPW_SCAN_PASSIVE_FULL_DWELL_SCAN] = cpu_to_le16(120);
+       scan.dwell_time[IPW_SCAN_ACTIVE_DIRECT_SCAN] = cpu_to_le16(20);
 
-#ifdef CONFIG_IPW_MONITOR
+#ifdef CONFIG_IPW2200_MONITOR
        if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
-               u8 band = 0, channel = priv->channel;
+               u8 channel;
+               u8 band = 0;
 
-               if (is_valid_channel(IEEE_A, channel))
+               switch (ieee80211_is_valid_channel(priv->ieee, priv->channel)) {
+               case IEEE80211_52GHZ_BAND:
                        band = (u8) (IPW_A_MODE << 6) | 1;
+                       channel = priv->channel;
+                       break;
 
-               if (is_valid_channel(IEEE_B | IEEE_G, channel))
+               case IEEE80211_24GHZ_BAND:
                        band = (u8) (IPW_B_MODE << 6) | 1;
+                       channel = priv->channel;
+                       break;
 
-               if (band == 0) {
+               default:
                        band = (u8) (IPW_B_MODE << 6) | 1;
                        channel = 9;
+                       break;
                }
 
-               scan.channels_list[channel_index++] = band;
-               scan.channels_list[channel_index] = channel;
-               ipw_set_scan_type(&scan, channel_index,
-                                 IPW_SCAN_PASSIVE_FULL_DWELL_SCAN);
+               scan.channels_list[0] = band;
+               scan.channels_list[1] = channel;
+               ipw_set_scan_type(&scan, 1, IPW_SCAN_PASSIVE_FULL_DWELL_SCAN);
 
+               /* NOTE:  The card will sit on this channel for this time
+                * period.  Scan aborts are timing sensitive and frequently
+                * result in firmware restarts.  As such, it is best to
+                * set a small dwell_time here and just keep re-issuing
+                * scans.  Otherwise fast channel hopping will not actually
+                * hop channels.
+                *
+                * TODO: Move SPEED SCAN support to all modes and bands */
                scan.dwell_time[IPW_SCAN_PASSIVE_FULL_DWELL_SCAN] =
-                   cpu_to_le16(2000);
+                       cpu_to_le16(2000);
        } else {
-#endif                         /* CONFIG_IPW_MONITOR */
-               /* If we are roaming, then make this a directed scan for the current
-                * network.  Otherwise, ensure that every other scan is a fast
-                * channel hop scan */
-               if ((priv->status & STATUS_ROAMING) || (!(priv->status & STATUS_ASSOCIATED) && (priv->config & CFG_STATIC_ESSID) && (le32_to_cpu(scan.full_scan_index) % 2))) { /* || (
-                                                                                                                                                                                  (priv->status & STATUS_ASSOCIATED) &&
-                                                                                                                                                                                  (priv->ieee->iw_mode == IW_MODE_ADHOC))) { */
-                       err = ipw_send_ssid(priv, priv->essid, priv->essid_len);
+#endif                         /* CONFIG_IPW2200_MONITOR */
+               /* Honor direct scans first, otherwise if we are roaming make
+                * this a direct scan for the current network.  Finally,
+                * ensure that every other scan is a fast channel hop scan */
+               if (direct) {
+                       err = ipw_send_ssid(priv, priv->direct_scan_ssid,
+                                           priv->direct_scan_ssid_len);
                        if (err) {
-                               IPW_DEBUG_HC
-                                   ("Attempt to send SSID command failed.\n");
-                               return err;
+                               IPW_DEBUG_HC("Attempt to send SSID command  "
+                                            "failed\n");
+                               goto done;
                        }
 
                        scan_type = IPW_SCAN_ACTIVE_BROADCAST_AND_DIRECT_SCAN;
-               } else {
-                       scan_type = IPW_SCAN_ACTIVE_BROADCAST_SCAN;
-               }
-
-               if (priv->ieee->freq_band & IEEE80211_52GHZ_BAND) {
-                       int start = channel_index;
-                       for (i = 0; i < MAX_A_CHANNELS; i++) {
-                               if (band_a_active_channel[i] == 0)
-                                       break;
-                               if ((priv->status & STATUS_ASSOCIATED) &&
-                                   band_a_active_channel[i] == priv->channel)
-                                       continue;
-                               channel_index++;
-                               scan.channels_list[channel_index] =
-                                   band_a_active_channel[i];
-                               ipw_set_scan_type(&scan, channel_index,
-                                                 scan_type);
-                       }
-
-                       if (start != channel_index) {
-                               scan.channels_list[start] =
-                                   (u8) (IPW_A_MODE << 6) | (channel_index -
-                                                             start);
-                               channel_index++;
+               } else if ((priv->status & STATUS_ROAMING)
+                          || (!(priv->status & STATUS_ASSOCIATED)
+                              && (priv->config & CFG_STATIC_ESSID)
+                              && (le32_to_cpu(scan.full_scan_index) % 2))) {
+                       err = ipw_send_ssid(priv, priv->essid, priv->essid_len);
+                       if (err) {
+                               IPW_DEBUG_HC("Attempt to send SSID command "
+                                            "failed.\n");
+                               goto done;
                        }
-               }
 
-               if (priv->ieee->freq_band & IEEE80211_24GHZ_BAND) {
-                       int start = channel_index;
-                       for (i = 0; i < MAX_B_CHANNELS; i++) {
-                               if (band_b_active_channel[i] == 0)
-                                       break;
-                               if ((priv->status & STATUS_ASSOCIATED) &&
-                                   band_b_active_channel[i] == priv->channel)
-                                       continue;
-                               channel_index++;
-                               scan.channels_list[channel_index] =
-                                   band_b_active_channel[i];
-                               ipw_set_scan_type(&scan, channel_index,
-                                                 scan_type);
-                       }
+                       scan_type = IPW_SCAN_ACTIVE_BROADCAST_AND_DIRECT_SCAN;
+               } else
+                       scan_type = IPW_SCAN_ACTIVE_BROADCAST_SCAN;
 
-                       if (start != channel_index) {
-                               scan.channels_list[start] =
-                                   (u8) (IPW_B_MODE << 6) | (channel_index -
-                                                             start);
-                       }
-               }
-#ifdef CONFIG_IPW_MONITOR
+               ipw_add_scan_channels(priv, &scan, scan_type);
+#ifdef CONFIG_IPW2200_MONITOR
        }
 #endif
 
+send_request:
        err = ipw_send_scan_request_ext(priv, &scan);
        if (err) {
                IPW_DEBUG_HC("Sending scan command failed: %08X\n", err);
-               return -EIO;
+               goto done;
        }
 
        priv->status |= STATUS_SCANNING;
-       priv->status &= ~STATUS_SCAN_PENDING;
+       if (direct) {
+               priv->status &= ~STATUS_DIRECT_SCAN_PENDING;
+               priv->direct_scan_ssid_len = 0;
+       } else
+               priv->status &= ~STATUS_SCAN_PENDING;
 
-       return 0;
+       queue_delayed_work(priv->workqueue, &priv->scan_check,
+                          IPW_SCAN_CHECK_WATCHDOG);
+done:
+       mutex_unlock(&priv->mutex);
+       return err;
 }
 
-static void ipw_bg_request_scan(void *data)
+static void ipw_request_passive_scan(struct work_struct *work)
 {
-       struct ipw_priv *priv = data;
-       down(&priv->sem);
-       ipw_request_scan(data);
-       up(&priv->sem);
+       struct ipw_priv *priv =
+               container_of(work, struct ipw_priv, request_passive_scan.work);
+       ipw_request_scan_helper(priv, IW_SCAN_TYPE_PASSIVE, 0);
 }
 
-static void ipw_bg_abort_scan(void *data)
+static void ipw_request_scan(struct work_struct *work)
 {
-       struct ipw_priv *priv = data;
-       down(&priv->sem);
-       ipw_abort_scan(data);
-       up(&priv->sem);
-}
-
-/* Support for wpa_supplicant. Will be replaced with WEXT once
- * they get WPA support. */
-
-/* following definitions must match definitions in driver_ipw.c */
-
-#define IPW_IOCTL_WPA_SUPPLICANT               SIOCIWFIRSTPRIV+30
-
-#define IPW_CMD_SET_WPA_PARAM                  1
-#define        IPW_CMD_SET_WPA_IE                      2
-#define IPW_CMD_SET_ENCRYPTION                 3
-#define IPW_CMD_MLME                           4
-
-#define IPW_PARAM_WPA_ENABLED                  1
-#define IPW_PARAM_TKIP_COUNTERMEASURES         2
-#define IPW_PARAM_DROP_UNENCRYPTED             3
-#define IPW_PARAM_PRIVACY_INVOKED              4
-#define IPW_PARAM_AUTH_ALGS                    5
-#define IPW_PARAM_IEEE_802_1X                  6
-
-#define IPW_MLME_STA_DEAUTH                    1
-#define IPW_MLME_STA_DISASSOC                  2
-
-#define IPW_CRYPT_ERR_UNKNOWN_ALG              2
-#define IPW_CRYPT_ERR_UNKNOWN_ADDR             3
-#define IPW_CRYPT_ERR_CRYPT_INIT_FAILED                4
-#define IPW_CRYPT_ERR_KEY_SET_FAILED           5
-#define IPW_CRYPT_ERR_TX_KEY_SET_FAILED                6
-#define IPW_CRYPT_ERR_CARD_CONF_FAILED         7
-
-#define        IPW_CRYPT_ALG_NAME_LEN                  16
-
-struct ipw_param {
-       u32 cmd;
-       u8 sta_addr[ETH_ALEN];
-       union {
-               struct {
-                       u8 name;
-                       u32 value;
-               } wpa_param;
-               struct {
-                       u32 len;
-                       u8 *data;
-               } wpa_ie;
-               struct {
-                       int command;
-                       int reason_code;
-               } mlme;
-               struct {
-                       u8 alg[IPW_CRYPT_ALG_NAME_LEN];
-                       u8 set_tx;
-                       u32 err;
-                       u8 idx;
-                       u8 seq[8];      /* sequence counter (set: RX, get: TX) */
-                       u16 key_len;
-                       u8 key[0];
-               } crypt;
-
-       } u;
-};
+       struct ipw_priv *priv =
+               container_of(work, struct ipw_priv, request_scan.work);
+       ipw_request_scan_helper(priv, IW_SCAN_TYPE_ACTIVE, 0);
+}
 
-/* end of driver_ipw.c code */
+static void ipw_request_direct_scan(struct work_struct *work)
+{
+       struct ipw_priv *priv =
+               container_of(work, struct ipw_priv, request_direct_scan.work);
+       ipw_request_scan_helper(priv, IW_SCAN_TYPE_ACTIVE, 1);
+}
+
+static void ipw_bg_abort_scan(struct work_struct *work)
+{
+       struct ipw_priv *priv =
+               container_of(work, struct ipw_priv, abort_scan);
+       mutex_lock(&priv->mutex);
+       ipw_abort_scan(priv);
+       mutex_unlock(&priv->mutex);
+}
 
 static int ipw_wpa_enable(struct ipw_priv *priv, int value)
 {
+       /* This is called when wpa_supplicant loads and closes the driver
+        * interface. */
+       priv->ieee->wpa_enabled = value;
+       return 0;
+}
+
+static int ipw_wpa_set_auth_algs(struct ipw_priv *priv, int value)
+{
        struct ieee80211_device *ieee = priv->ieee;
        struct ieee80211_security sec = {
-               .flags = SEC_LEVEL | SEC_ENABLED,
+               .flags = SEC_AUTH_MODE,
        };
        int ret = 0;
 
-       ieee->wpa_enabled = value;
-
-       if (value) {
-               sec.level = SEC_LEVEL_3;
-               sec.enabled = 1;
-       } else {
-               sec.level = SEC_LEVEL_0;
-               sec.enabled = 0;
-               ieee->wpa_ie_len = 0;
-       }
+       if (value & IW_AUTH_ALG_SHARED_KEY) {
+               sec.auth_mode = WLAN_AUTH_SHARED_KEY;
+               ieee->open_wep = 0;
+       } else if (value & IW_AUTH_ALG_OPEN_SYSTEM) {
+               sec.auth_mode = WLAN_AUTH_OPEN;
+               ieee->open_wep = 1;
+       } else if (value & IW_AUTH_ALG_LEAP) {
+               sec.auth_mode = WLAN_AUTH_LEAP;
+               ieee->open_wep = 1;
+       } else
+               return -EINVAL;
 
        if (ieee->set_security)
                ieee->set_security(ieee->dev, &sec);
@@ -5656,56 +6500,142 @@ static int ipw_wpa_enable(struct ipw_priv *priv, int value)
        return ret;
 }
 
-#define AUTH_ALG_OPEN_SYSTEM                   0x1
-#define AUTH_ALG_SHARED_KEY                    0x2
+static void ipw_wpa_assoc_frame(struct ipw_priv *priv, char *wpa_ie,
+                               int wpa_ie_len)
+{
+       /* make sure WPA is enabled */
+       ipw_wpa_enable(priv, 1);
+}
 
-static int ipw_wpa_set_auth_algs(struct ipw_priv *priv, int value)
+static int ipw_set_rsn_capa(struct ipw_priv *priv,
+                           char *capabilities, int length)
 {
+       IPW_DEBUG_HC("HOST_CMD_RSN_CAPABILITIES\n");
+
+       return ipw_send_cmd_pdu(priv, IPW_CMD_RSN_CAPABILITIES, length,
+                               capabilities);
+}
+
+/*
+ * WE-18 support
+ */
+
+/* SIOCSIWGENIE */
+static int ipw_wx_set_genie(struct net_device *dev,
+                           struct iw_request_info *info,
+                           union iwreq_data *wrqu, char *extra)
+{
+       struct ipw_priv *priv = ieee80211_priv(dev);
        struct ieee80211_device *ieee = priv->ieee;
-       struct ieee80211_security sec = {
-               .flags = SEC_AUTH_MODE,
-       };
-       int ret = 0;
+       u8 *buf;
+       int err = 0;
 
-       if (value & AUTH_ALG_SHARED_KEY) {
-               sec.auth_mode = WLAN_AUTH_SHARED_KEY;
-               ieee->open_wep = 0;
+       if (wrqu->data.length > MAX_WPA_IE_LEN ||
+           (wrqu->data.length && extra == NULL))
+               return -EINVAL;
+
+       if (wrqu->data.length) {
+               buf = kmalloc(wrqu->data.length, GFP_KERNEL);
+               if (buf == NULL) {
+                       err = -ENOMEM;
+                       goto out;
+               }
+
+               memcpy(buf, extra, wrqu->data.length);
+               kfree(ieee->wpa_ie);
+               ieee->wpa_ie = buf;
+               ieee->wpa_ie_len = wrqu->data.length;
        } else {
-               sec.auth_mode = WLAN_AUTH_OPEN;
-               ieee->open_wep = 1;
+               kfree(ieee->wpa_ie);
+               ieee->wpa_ie = NULL;
+               ieee->wpa_ie_len = 0;
        }
 
-       if (ieee->set_security)
-               ieee->set_security(ieee->dev, &sec);
-       else
-               ret = -EOPNOTSUPP;
+       ipw_wpa_assoc_frame(priv, ieee->wpa_ie, ieee->wpa_ie_len);
+      out:
+       return err;
+}
 
-       return ret;
+/* SIOCGIWGENIE */
+static int ipw_wx_get_genie(struct net_device *dev,
+                           struct iw_request_info *info,
+                           union iwreq_data *wrqu, char *extra)
+{
+       struct ipw_priv *priv = ieee80211_priv(dev);
+       struct ieee80211_device *ieee = priv->ieee;
+       int err = 0;
+
+       if (ieee->wpa_ie_len == 0 || ieee->wpa_ie == NULL) {
+               wrqu->data.length = 0;
+               goto out;
+       }
+
+       if (wrqu->data.length < ieee->wpa_ie_len) {
+               err = -E2BIG;
+               goto out;
+       }
+
+       wrqu->data.length = ieee->wpa_ie_len;
+       memcpy(extra, ieee->wpa_ie, ieee->wpa_ie_len);
+
+      out:
+       return err;
 }
 
-static int ipw_wpa_set_param(struct net_device *dev, u8 name, u32 value)
+static int wext_cipher2level(int cipher)
+{
+       switch (cipher) {
+       case IW_AUTH_CIPHER_NONE:
+               return SEC_LEVEL_0;
+       case IW_AUTH_CIPHER_WEP40:
+       case IW_AUTH_CIPHER_WEP104:
+               return SEC_LEVEL_1;
+       case IW_AUTH_CIPHER_TKIP:
+               return SEC_LEVEL_2;
+       case IW_AUTH_CIPHER_CCMP:
+               return SEC_LEVEL_3;
+       default:
+               return -1;
+       }
+}
+
+/* SIOCSIWAUTH */
+static int ipw_wx_set_auth(struct net_device *dev,
+                          struct iw_request_info *info,
+                          union iwreq_data *wrqu, char *extra)
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
+       struct ieee80211_device *ieee = priv->ieee;
+       struct iw_param *param = &wrqu->param;
        struct ieee80211_crypt_data *crypt;
        unsigned long flags;
        int ret = 0;
 
-       switch (name) {
-       case IPW_PARAM_WPA_ENABLED:
-               ret = ipw_wpa_enable(priv, value);
+       switch (param->flags & IW_AUTH_INDEX) {
+       case IW_AUTH_WPA_VERSION:
+               break;
+       case IW_AUTH_CIPHER_PAIRWISE:
+               ipw_set_hw_decrypt_unicast(priv,
+                                          wext_cipher2level(param->value));
+               break;
+       case IW_AUTH_CIPHER_GROUP:
+               ipw_set_hw_decrypt_multicast(priv,
+                                            wext_cipher2level(param->value));
+               break;
+       case IW_AUTH_KEY_MGMT:
+               /*
+                * ipw2200 does not use these parameters
+                */
                break;
 
-       case IPW_PARAM_TKIP_COUNTERMEASURES:
+       case IW_AUTH_TKIP_COUNTERMEASURES:
                crypt = priv->ieee->crypt[priv->ieee->tx_keyidx];
-               if (!crypt || !crypt->ops->set_flags || !crypt->ops->get_flags) {
-                       IPW_WARNING("Can't set TKIP countermeasures: "
-                                   "crypt not set!\n");
+               if (!crypt || !crypt->ops->set_flags || !crypt->ops->get_flags)
                        break;
-               }
 
                flags = crypt->ops->get_flags(crypt->priv);
 
-               if (value)
+               if (param->value)
                        flags |= IEEE80211_CRYPTO_TKIP_COUNTERMEASURES;
                else
                        flags &= ~IEEE80211_CRYPTO_TKIP_COUNTERMEASURES;
@@ -5714,317 +6644,662 @@ static int ipw_wpa_set_param(struct net_device *dev, u8 name, u32 value)
 
                break;
 
-       case IPW_PARAM_DROP_UNENCRYPTED:
-               priv->ieee->drop_unencrypted = value;
+       case IW_AUTH_DROP_UNENCRYPTED:{
+                       /* HACK:
+                        *
+                        * wpa_supplicant calls set_wpa_enabled when the driver
+                        * is loaded and unloaded, regardless of if WPA is being
+                        * used.  No other calls are made which can be used to
+                        * determine if encryption will be used or not prior to
+                        * association being expected.  If encryption is not being
+                        * used, drop_unencrypted is set to false, else true -- we
+                        * can use this to determine if the CAP_PRIVACY_ON bit should
+                        * be set.
+                        */
+                       struct ieee80211_security sec = {
+                               .flags = SEC_ENABLED,
+                               .enabled = param->value,
+                       };
+                       priv->ieee->drop_unencrypted = param->value;
+                       /* We only change SEC_LEVEL for open mode. Others
+                        * are set by ipw_wpa_set_encryption.
+                        */
+                       if (!param->value) {
+                               sec.flags |= SEC_LEVEL;
+                               sec.level = SEC_LEVEL_0;
+                       } else {
+                               sec.flags |= SEC_LEVEL;
+                               sec.level = SEC_LEVEL_1;
+                       }
+                       if (priv->ieee->set_security)
+                               priv->ieee->set_security(priv->ieee->dev, &sec);
+                       break;
+               }
+
+       case IW_AUTH_80211_AUTH_ALG:
+               ret = ipw_wpa_set_auth_algs(priv, param->value);
                break;
 
-       case IPW_PARAM_PRIVACY_INVOKED:
-               priv->ieee->privacy_invoked = value;
+       case IW_AUTH_WPA_ENABLED:
+               ret = ipw_wpa_enable(priv, param->value);
+               ipw_disassociate(priv);
                break;
 
-       case IPW_PARAM_AUTH_ALGS:
-               ret = ipw_wpa_set_auth_algs(priv, value);
+       case IW_AUTH_RX_UNENCRYPTED_EAPOL:
+               ieee->ieee802_1x = param->value;
                break;
 
-       case IPW_PARAM_IEEE_802_1X:
-               priv->ieee->ieee802_1x = value;
+       case IW_AUTH_PRIVACY_INVOKED:
+               ieee->privacy_invoked = param->value;
                break;
 
        default:
-               IPW_ERROR("%s: Unknown WPA param: %d\n", dev->name, name);
-               ret = -EOPNOTSUPP;
+               return -EOPNOTSUPP;
        }
-
        return ret;
 }
 
-static int ipw_wpa_mlme(struct net_device *dev, int command, int reason)
+/* SIOCGIWAUTH */
+static int ipw_wx_get_auth(struct net_device *dev,
+                          struct iw_request_info *info,
+                          union iwreq_data *wrqu, char *extra)
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
+       struct ieee80211_device *ieee = priv->ieee;
+       struct ieee80211_crypt_data *crypt;
+       struct iw_param *param = &wrqu->param;
        int ret = 0;
 
-       switch (command) {
-       case IPW_MLME_STA_DEAUTH:
-               // silently ignore
+       switch (param->flags & IW_AUTH_INDEX) {
+       case IW_AUTH_WPA_VERSION:
+       case IW_AUTH_CIPHER_PAIRWISE:
+       case IW_AUTH_CIPHER_GROUP:
+       case IW_AUTH_KEY_MGMT:
+               /*
+                * wpa_supplicant will control these internally
+                */
+               ret = -EOPNOTSUPP;
                break;
 
-       case IPW_MLME_STA_DISASSOC:
-               ipw_disassociate(priv);
+       case IW_AUTH_TKIP_COUNTERMEASURES:
+               crypt = priv->ieee->crypt[priv->ieee->tx_keyidx];
+               if (!crypt || !crypt->ops->get_flags)
+                       break;
+
+               param->value = (crypt->ops->get_flags(crypt->priv) &
+                               IEEE80211_CRYPTO_TKIP_COUNTERMEASURES) ? 1 : 0;
+
                break;
 
-       default:
-               IPW_ERROR("%s: Unknown MLME request: %d\n", dev->name, command);
-               ret = -EOPNOTSUPP;
-       }
+       case IW_AUTH_DROP_UNENCRYPTED:
+               param->value = ieee->drop_unencrypted;
+               break;
 
-       return ret;
-}
+       case IW_AUTH_80211_AUTH_ALG:
+               param->value = ieee->sec.auth_mode;
+               break;
 
-static int ipw_set_rsn_capa(struct ipw_priv *priv,
-                           char *capabilities, int length)
-{
-       struct host_cmd cmd = {
-               .cmd = IPW_CMD_RSN_CAPABILITIES,
-               .len = length,
-       };
+       case IW_AUTH_WPA_ENABLED:
+               param->value = ieee->wpa_enabled;
+               break;
 
-       IPW_DEBUG_HC("HOST_CMD_RSN_CAPABILITIES\n");
+       case IW_AUTH_RX_UNENCRYPTED_EAPOL:
+               param->value = ieee->ieee802_1x;
+               break;
 
-       memcpy(&cmd.param, capabilities, length);
-       if (ipw_send_cmd(priv, &cmd)) {
-               IPW_ERROR("failed to send HOST_CMD_RSN_CAPABILITIES command\n");
-               return -1;
+       case IW_AUTH_ROAMING_CONTROL:
+       case IW_AUTH_PRIVACY_INVOKED:
+               param->value = ieee->privacy_invoked;
+               break;
+
+       default:
+               return -EOPNOTSUPP;
        }
        return 0;
 }
 
-void ipw_wpa_assoc_frame(struct ipw_priv *priv, char *wpa_ie, int wpa_ie_len)
+/* SIOCSIWENCODEEXT */
+static int ipw_wx_set_encodeext(struct net_device *dev,
+                               struct iw_request_info *info,
+                               union iwreq_data *wrqu, char *extra)
 {
-       /* make sure WPA is enabled */
-       ipw_wpa_enable(priv, 1);
+       struct ipw_priv *priv = ieee80211_priv(dev);
+       struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
+
+       if (hwcrypto) {
+               if (ext->alg == IW_ENCODE_ALG_TKIP) {
+                       /* IPW HW can't build TKIP MIC,
+                          host decryption still needed */
+                       if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY)
+                               priv->ieee->host_mc_decrypt = 1;
+                       else {
+                               priv->ieee->host_encrypt = 0;
+                               priv->ieee->host_encrypt_msdu = 1;
+                               priv->ieee->host_decrypt = 1;
+                       }
+               } else {
+                       priv->ieee->host_encrypt = 0;
+                       priv->ieee->host_encrypt_msdu = 0;
+                       priv->ieee->host_decrypt = 0;
+                       priv->ieee->host_mc_decrypt = 0;
+               }
+       }
 
-       ipw_disassociate(priv);
+       return ieee80211_wx_set_encodeext(priv->ieee, info, wrqu, extra);
 }
 
-static int ipw_wpa_set_wpa_ie(struct net_device *dev,
-                             struct ipw_param *param, int plen)
+/* SIOCGIWENCODEEXT */
+static int ipw_wx_get_encodeext(struct net_device *dev,
+                               struct iw_request_info *info,
+                               union iwreq_data *wrqu, char *extra)
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
-       struct ieee80211_device *ieee = priv->ieee;
-       u8 *buf;
+       return ieee80211_wx_get_encodeext(priv->ieee, info, wrqu, extra);
+}
 
-       if (!ieee->wpa_enabled)
+/* SIOCSIWMLME */
+static int ipw_wx_set_mlme(struct net_device *dev,
+                          struct iw_request_info *info,
+                          union iwreq_data *wrqu, char *extra)
+{
+       struct ipw_priv *priv = ieee80211_priv(dev);
+       struct iw_mlme *mlme = (struct iw_mlme *)extra;
+       __le16 reason;
+
+       reason = cpu_to_le16(mlme->reason_code);
+
+       switch (mlme->cmd) {
+       case IW_MLME_DEAUTH:
+               /* silently ignore */
+               break;
+
+       case IW_MLME_DISASSOC:
+               ipw_disassociate(priv);
+               break;
+
+       default:
                return -EOPNOTSUPP;
+       }
+       return 0;
+}
 
-       if (param->u.wpa_ie.len > MAX_WPA_IE_LEN ||
-           (param->u.wpa_ie.len && param->u.wpa_ie.data == NULL))
-               return -EINVAL;
+#ifdef CONFIG_IPW2200_QOS
 
-       if (param->u.wpa_ie.len) {
-               buf = kmalloc(param->u.wpa_ie.len, GFP_KERNEL);
-               if (buf == NULL)
-                       return -ENOMEM;
+/* QoS */
+/*
+* get the modulation type of the current network or
+* the card current mode
+*/
+static u8 ipw_qos_current_mode(struct ipw_priv * priv)
+{
+       u8 mode = 0;
 
-               memcpy(buf, param->u.wpa_ie.data, param->u.wpa_ie.len);
-               kfree(ieee->wpa_ie);
-               ieee->wpa_ie = buf;
-               ieee->wpa_ie_len = param->u.wpa_ie.len;
+       if (priv->status & STATUS_ASSOCIATED) {
+               unsigned long flags;
+
+               spin_lock_irqsave(&priv->ieee->lock, flags);
+               mode = priv->assoc_network->mode;
+               spin_unlock_irqrestore(&priv->ieee->lock, flags);
        } else {
-               kfree(ieee->wpa_ie);
-               ieee->wpa_ie = NULL;
-               ieee->wpa_ie_len = 0;
+               mode = priv->ieee->mode;
+       }
+       IPW_DEBUG_QOS("QoS network/card mode %d \n", mode);
+       return mode;
+}
+
+/*
+* Handle management frame beacon and probe response
+*/
+static int ipw_qos_handle_probe_response(struct ipw_priv *priv,
+                                        int active_network,
+                                        struct ieee80211_network *network)
+{
+       u32 size = sizeof(struct ieee80211_qos_parameters);
+
+       if (network->capability & WLAN_CAPABILITY_IBSS)
+               network->qos_data.active = network->qos_data.supported;
+
+       if (network->flags & NETWORK_HAS_QOS_MASK) {
+               if (active_network &&
+                   (network->flags & NETWORK_HAS_QOS_PARAMETERS))
+                       network->qos_data.active = network->qos_data.supported;
+
+               if ((network->qos_data.active == 1) && (active_network == 1) &&
+                   (network->flags & NETWORK_HAS_QOS_PARAMETERS) &&
+                   (network->qos_data.old_param_count !=
+                    network->qos_data.param_count)) {
+                       network->qos_data.old_param_count =
+                           network->qos_data.param_count;
+                       schedule_work(&priv->qos_activate);
+                       IPW_DEBUG_QOS("QoS parameters change call "
+                                     "qos_activate\n");
+               }
+       } else {
+               if ((priv->ieee->mode == IEEE_B) || (network->mode == IEEE_B))
+                       memcpy(&network->qos_data.parameters,
+                              &def_parameters_CCK, size);
+               else
+                       memcpy(&network->qos_data.parameters,
+                              &def_parameters_OFDM, size);
+
+               if ((network->qos_data.active == 1) && (active_network == 1)) {
+                       IPW_DEBUG_QOS("QoS was disabled call qos_activate \n");
+                       schedule_work(&priv->qos_activate);
+               }
+
+               network->qos_data.active = 0;
+               network->qos_data.supported = 0;
+       }
+       if ((priv->status & STATUS_ASSOCIATED) &&
+           (priv->ieee->iw_mode == IW_MODE_ADHOC) && (active_network == 0)) {
+               if (memcmp(network->bssid, priv->bssid, ETH_ALEN))
+                       if ((network->capability & WLAN_CAPABILITY_IBSS) &&
+                           !(network->flags & NETWORK_EMPTY_ESSID))
+                               if ((network->ssid_len ==
+                                    priv->assoc_network->ssid_len) &&
+                                   !memcmp(network->ssid,
+                                           priv->assoc_network->ssid,
+                                           network->ssid_len)) {
+                                       queue_work(priv->workqueue,
+                                                  &priv->merge_networks);
+                               }
        }
 
-       ipw_wpa_assoc_frame(priv, ieee->wpa_ie, ieee->wpa_ie_len);
        return 0;
 }
 
-/* implementation borrowed from hostap driver */
+/*
+* This function set up the firmware to support QoS. It sends
+* IPW_CMD_QOS_PARAMETERS and IPW_CMD_WME_INFO
+*/
+static int ipw_qos_activate(struct ipw_priv *priv,
+                           struct ieee80211_qos_data *qos_network_data)
+{
+       int err;
+       struct ieee80211_qos_parameters qos_parameters[QOS_QOS_SETS];
+       struct ieee80211_qos_parameters *active_one = NULL;
+       u32 size = sizeof(struct ieee80211_qos_parameters);
+       u32 burst_duration;
+       int i;
+       u8 type;
+
+       type = ipw_qos_current_mode(priv);
+
+       active_one = &(qos_parameters[QOS_PARAM_SET_DEF_CCK]);
+       memcpy(active_one, priv->qos_data.def_qos_parm_CCK, size);
+       active_one = &(qos_parameters[QOS_PARAM_SET_DEF_OFDM]);
+       memcpy(active_one, priv->qos_data.def_qos_parm_OFDM, size);
+
+       if (qos_network_data == NULL) {
+               if (type == IEEE_B) {
+                       IPW_DEBUG_QOS("QoS activate network mode %d\n", type);
+                       active_one = &def_parameters_CCK;
+               } else
+                       active_one = &def_parameters_OFDM;
+
+               memcpy(&qos_parameters[QOS_PARAM_SET_ACTIVE], active_one, size);
+               burst_duration = ipw_qos_get_burst_duration(priv);
+               for (i = 0; i < QOS_QUEUE_NUM; i++)
+                       qos_parameters[QOS_PARAM_SET_ACTIVE].tx_op_limit[i] =
+                           cpu_to_le16(burst_duration);
+       } else if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
+               if (type == IEEE_B) {
+                       IPW_DEBUG_QOS("QoS activate IBSS nework mode %d\n",
+                                     type);
+                       if (priv->qos_data.qos_enable == 0)
+                               active_one = &def_parameters_CCK;
+                       else
+                               active_one = priv->qos_data.def_qos_parm_CCK;
+               } else {
+                       if (priv->qos_data.qos_enable == 0)
+                               active_one = &def_parameters_OFDM;
+                       else
+                               active_one = priv->qos_data.def_qos_parm_OFDM;
+               }
+               memcpy(&qos_parameters[QOS_PARAM_SET_ACTIVE], active_one, size);
+       } else {
+               unsigned long flags;
+               int active;
+
+               spin_lock_irqsave(&priv->ieee->lock, flags);
+               active_one = &(qos_network_data->parameters);
+               qos_network_data->old_param_count =
+                   qos_network_data->param_count;
+               memcpy(&qos_parameters[QOS_PARAM_SET_ACTIVE], active_one, size);
+               active = qos_network_data->supported;
+               spin_unlock_irqrestore(&priv->ieee->lock, flags);
+
+               if (active == 0) {
+                       burst_duration = ipw_qos_get_burst_duration(priv);
+                       for (i = 0; i < QOS_QUEUE_NUM; i++)
+                               qos_parameters[QOS_PARAM_SET_ACTIVE].
+                                   tx_op_limit[i] = cpu_to_le16(burst_duration);
+               }
+       }
+
+       IPW_DEBUG_QOS("QoS sending IPW_CMD_QOS_PARAMETERS\n");
+       err = ipw_send_qos_params_command(priv,
+                                         (struct ieee80211_qos_parameters *)
+                                         &(qos_parameters[0]));
+       if (err)
+               IPW_DEBUG_QOS("QoS IPW_CMD_QOS_PARAMETERS failed\n");
+
+       return err;
+}
 
-static int ipw_wpa_set_encryption(struct net_device *dev,
-                                 struct ipw_param *param, int param_len)
+/*
+* send IPW_CMD_WME_INFO to the firmware
+*/
+static int ipw_qos_set_info_element(struct ipw_priv *priv)
 {
        int ret = 0;
-       struct ipw_priv *priv = ieee80211_priv(dev);
-       struct ieee80211_device *ieee = priv->ieee;
-       struct ieee80211_crypto_ops *ops;
-       struct ieee80211_crypt_data **crypt;
+       struct ieee80211_qos_information_element qos_info;
 
-       struct ieee80211_security sec = {
-               .flags = 0,
+       if (priv == NULL)
+               return -1;
+
+       qos_info.elementID = QOS_ELEMENT_ID;
+       qos_info.length = sizeof(struct ieee80211_qos_information_element) - 2;
+
+       qos_info.version = QOS_VERSION_1;
+       qos_info.ac_info = 0;
+
+       memcpy(qos_info.qui, qos_oui, QOS_OUI_LEN);
+       qos_info.qui_type = QOS_OUI_TYPE;
+       qos_info.qui_subtype = QOS_OUI_INFO_SUB_TYPE;
+
+       ret = ipw_send_qos_info_command(priv, &qos_info);
+       if (ret != 0) {
+               IPW_DEBUG_QOS("QoS error calling ipw_send_qos_info_command\n");
+       }
+       return ret;
+}
+
+/*
+* Set the QoS parameter with the association request structure
+*/
+static int ipw_qos_association(struct ipw_priv *priv,
+                              struct ieee80211_network *network)
+{
+       int err = 0;
+       struct ieee80211_qos_data *qos_data = NULL;
+       struct ieee80211_qos_data ibss_data = {
+               .supported = 1,
+               .active = 1,
        };
 
-       param->u.crypt.err = 0;
-       param->u.crypt.alg[IPW_CRYPT_ALG_NAME_LEN - 1] = '\0';
+       switch (priv->ieee->iw_mode) {
+       case IW_MODE_ADHOC:
+               BUG_ON(!(network->capability & WLAN_CAPABILITY_IBSS));
 
-       if (param_len !=
-           (int)((char *)param->u.crypt.key - (char *)param) +
-           param->u.crypt.key_len) {
-               IPW_DEBUG_INFO("Len mismatch %d, %d\n", param_len,
-                              param->u.crypt.key_len);
-               return -EINVAL;
+               qos_data = &ibss_data;
+               break;
+
+       case IW_MODE_INFRA:
+               qos_data = &network->qos_data;
+               break;
+
+       default:
+               BUG();
+               break;
        }
-       if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff &&
-           param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff &&
-           param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) {
-               if (param->u.crypt.idx >= WEP_KEYS)
-                       return -EINVAL;
-               crypt = &ieee->crypt[param->u.crypt.idx];
+
+       err = ipw_qos_activate(priv, qos_data);
+       if (err) {
+               priv->assoc_request.policy_support &= ~HC_QOS_SUPPORT_ASSOC;
+               return err;
+       }
+
+       if (priv->qos_data.qos_enable && qos_data->supported) {
+               IPW_DEBUG_QOS("QoS will be enabled for this association\n");
+               priv->assoc_request.policy_support |= HC_QOS_SUPPORT_ASSOC;
+               return ipw_qos_set_info_element(priv);
+       }
+
+       return 0;
+}
+
+/*
+* handling the beaconing responses. if we get different QoS setting
+* off the network from the associated setting, adjust the QoS
+* setting
+*/
+static int ipw_qos_association_resp(struct ipw_priv *priv,
+                                   struct ieee80211_network *network)
+{
+       int ret = 0;
+       unsigned long flags;
+       u32 size = sizeof(struct ieee80211_qos_parameters);
+       int set_qos_param = 0;
+
+       if ((priv == NULL) || (network == NULL) ||
+           (priv->assoc_network == NULL))
+               return ret;
+
+       if (!(priv->status & STATUS_ASSOCIATED))
+               return ret;
+
+       if ((priv->ieee->iw_mode != IW_MODE_INFRA))
+               return ret;
+
+       spin_lock_irqsave(&priv->ieee->lock, flags);
+       if (network->flags & NETWORK_HAS_QOS_PARAMETERS) {
+               memcpy(&priv->assoc_network->qos_data, &network->qos_data,
+                      sizeof(struct ieee80211_qos_data));
+               priv->assoc_network->qos_data.active = 1;
+               if ((network->qos_data.old_param_count !=
+                    network->qos_data.param_count)) {
+                       set_qos_param = 1;
+                       network->qos_data.old_param_count =
+                           network->qos_data.param_count;
+               }
+
+       } else {
+               if ((network->mode == IEEE_B) || (priv->ieee->mode == IEEE_B))
+                       memcpy(&priv->assoc_network->qos_data.parameters,
+                              &def_parameters_CCK, size);
+               else
+                       memcpy(&priv->assoc_network->qos_data.parameters,
+                              &def_parameters_OFDM, size);
+               priv->assoc_network->qos_data.active = 0;
+               priv->assoc_network->qos_data.supported = 0;
+               set_qos_param = 1;
+       }
+
+       spin_unlock_irqrestore(&priv->ieee->lock, flags);
+
+       if (set_qos_param == 1)
+               schedule_work(&priv->qos_activate);
+
+       return ret;
+}
+
+static u32 ipw_qos_get_burst_duration(struct ipw_priv *priv)
+{
+       u32 ret = 0;
+
+       if ((priv == NULL))
+               return 0;
+
+       if (!(priv->ieee->modulation & IEEE80211_OFDM_MODULATION))
+               ret = priv->qos_data.burst_duration_CCK;
+       else
+               ret = priv->qos_data.burst_duration_OFDM;
+
+       return ret;
+}
+
+/*
+* Initialize the setting of QoS global
+*/
+static void ipw_qos_init(struct ipw_priv *priv, int enable,
+                        int burst_enable, u32 burst_duration_CCK,
+                        u32 burst_duration_OFDM)
+{
+       priv->qos_data.qos_enable = enable;
+
+       if (priv->qos_data.qos_enable) {
+               priv->qos_data.def_qos_parm_CCK = &def_qos_parameters_CCK;
+               priv->qos_data.def_qos_parm_OFDM = &def_qos_parameters_OFDM;
+               IPW_DEBUG_QOS("QoS is enabled\n");
+       } else {
+               priv->qos_data.def_qos_parm_CCK = &def_parameters_CCK;
+               priv->qos_data.def_qos_parm_OFDM = &def_parameters_OFDM;
+               IPW_DEBUG_QOS("QoS is not enabled\n");
+       }
+
+       priv->qos_data.burst_enable = burst_enable;
+
+       if (burst_enable) {
+               priv->qos_data.burst_duration_CCK = burst_duration_CCK;
+               priv->qos_data.burst_duration_OFDM = burst_duration_OFDM;
        } else {
-               return -EINVAL;
+               priv->qos_data.burst_duration_CCK = 0;
+               priv->qos_data.burst_duration_OFDM = 0;
        }
+}
 
-       if (strcmp(param->u.crypt.alg, "none") == 0) {
-               if (crypt) {
-                       sec.enabled = 0;
-                       sec.level = SEC_LEVEL_0;
-                       sec.flags |= SEC_ENABLED | SEC_LEVEL;
-                       ieee80211_crypt_delayed_deinit(ieee, crypt);
-               }
-               goto done;
-       }
-       sec.enabled = 1;
-       sec.flags |= SEC_ENABLED;
-
-       ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
-       if (ops == NULL && strcmp(param->u.crypt.alg, "WEP") == 0) {
-               request_module("ieee80211_crypt_wep");
-               ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
-       } else if (ops == NULL && strcmp(param->u.crypt.alg, "TKIP") == 0) {
-               request_module("ieee80211_crypt_tkip");
-               ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
-       } else if (ops == NULL && strcmp(param->u.crypt.alg, "CCMP") == 0) {
-               request_module("ieee80211_crypt_ccmp");
-               ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
-       }
-       if (ops == NULL) {
-               IPW_DEBUG_INFO("%s: unknown crypto alg '%s'\n",
-                              dev->name, param->u.crypt.alg);
-               param->u.crypt.err = IPW_CRYPT_ERR_UNKNOWN_ALG;
-               ret = -EINVAL;
-               goto done;
-       }
+/*
+* map the packet priority to the right TX Queue
+*/
+static int ipw_get_tx_queue_number(struct ipw_priv *priv, u16 priority)
+{
+       if (priority > 7 || !priv->qos_data.qos_enable)
+               priority = 0;
 
-       if (*crypt == NULL || (*crypt)->ops != ops) {
-               struct ieee80211_crypt_data *new_crypt;
+       return from_priority_to_tx_queue[priority] - 1;
+}
 
-               ieee80211_crypt_delayed_deinit(ieee, crypt);
+static int ipw_is_qos_active(struct net_device *dev,
+                            struct sk_buff *skb)
+{
+       struct ipw_priv *priv = ieee80211_priv(dev);
+       struct ieee80211_qos_data *qos_data = NULL;
+       int active, supported;
+       u8 *daddr = skb->data + ETH_ALEN;
+       int unicast = !is_multicast_ether_addr(daddr);
 
-               new_crypt = (struct ieee80211_crypt_data *)
-                   kmalloc(sizeof(*new_crypt), GFP_KERNEL);
-               if (new_crypt == NULL) {
-                       ret = -ENOMEM;
-                       goto done;
-               }
-               memset(new_crypt, 0, sizeof(struct ieee80211_crypt_data));
-               new_crypt->ops = ops;
-               if (new_crypt->ops && try_module_get(new_crypt->ops->owner))
-                       new_crypt->priv =
-                           new_crypt->ops->init(param->u.crypt.idx);
-
-               if (new_crypt->priv == NULL) {
-                       kfree(new_crypt);
-                       param->u.crypt.err = IPW_CRYPT_ERR_CRYPT_INIT_FAILED;
-                       ret = -EINVAL;
-                       goto done;
-               }
+       if (!(priv->status & STATUS_ASSOCIATED))
+               return 0;
 
-               *crypt = new_crypt;
-       }
+       qos_data = &priv->assoc_network->qos_data;
 
-       if (param->u.crypt.key_len > 0 && (*crypt)->ops->set_key &&
-           (*crypt)->ops->set_key(param->u.crypt.key,
-                                  param->u.crypt.key_len, param->u.crypt.seq,
-                                  (*crypt)->priv) < 0) {
-               IPW_DEBUG_INFO("%s: key setting failed\n", dev->name);
-               param->u.crypt.err = IPW_CRYPT_ERR_KEY_SET_FAILED;
-               ret = -EINVAL;
-               goto done;
+       if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
+               if (unicast == 0)
+                       qos_data->active = 0;
+               else
+                       qos_data->active = qos_data->supported;
        }
+       active = qos_data->active;
+       supported = qos_data->supported;
+       IPW_DEBUG_QOS("QoS  %d network is QoS active %d  supported %d  "
+                     "unicast %d\n",
+                     priv->qos_data.qos_enable, active, supported, unicast);
+       if (active && priv->qos_data.qos_enable)
+               return 1;
 
-       if (param->u.crypt.set_tx) {
-               ieee->tx_keyidx = param->u.crypt.idx;
-               sec.active_key = param->u.crypt.idx;
-               sec.flags |= SEC_ACTIVE_KEY;
-       }
-
-       if (ops->name != NULL) {
-               if (strcmp(ops->name, "WEP") == 0) {
-                       memcpy(sec.keys[param->u.crypt.idx],
-                              param->u.crypt.key, param->u.crypt.key_len);
-                       sec.key_sizes[param->u.crypt.idx] =
-                           param->u.crypt.key_len;
-                       sec.flags |= (1 << param->u.crypt.idx);
-                       sec.flags |= SEC_LEVEL;
-                       sec.level = SEC_LEVEL_1;
-               } else if (strcmp(ops->name, "TKIP") == 0) {
-                       sec.flags |= SEC_LEVEL;
-                       sec.level = SEC_LEVEL_2;
-               } else if (strcmp(ops->name, "CCMP") == 0) {
-                       sec.flags |= SEC_LEVEL;
-                       sec.level = SEC_LEVEL_3;
-               }
-       }
-      done:
-       if (ieee->set_security)
-               ieee->set_security(ieee->dev, &sec);
+       return 0;
 
-       /* Do not reset port if card is in Managed mode since resetting will
-        * generate new IEEE 802.11 authentication which may end up in looping
-        * with IEEE 802.1X.  If your hardware requires a reset after WEP
-        * configuration (for example... Prism2), implement the reset_port in
-        * the callbacks structures used to initialize the 802.11 stack. */
-       if (ieee->reset_on_keychange &&
-           ieee->iw_mode != IW_MODE_INFRA &&
-           ieee->reset_port && ieee->reset_port(dev)) {
-               IPW_DEBUG_INFO("%s: reset_port failed\n", dev->name);
-               param->u.crypt.err = IPW_CRYPT_ERR_CARD_CONF_FAILED;
-               return -EINVAL;
-       }
+}
+/*
+* add QoS parameter to the TX command
+*/
+static int ipw_qos_set_tx_queue_command(struct ipw_priv *priv,
+                                       u16 priority,
+                                       struct tfd_data *tfd)
+{
+       int tx_queue_id = 0;
 
-       return ret;
+
+       tx_queue_id = from_priority_to_tx_queue[priority] - 1;
+       tfd->tx_flags_ext |= DCT_FLAG_EXT_QOS_ENABLED;
+
+       if (priv->qos_data.qos_no_ack_mask & (1UL << tx_queue_id)) {
+               tfd->tx_flags &= ~DCT_FLAG_ACK_REQD;
+               tfd->tfd.tfd_26.mchdr.qos_ctrl |= cpu_to_le16(CTRL_QOS_NO_ACK);
+       }
+       return 0;
 }
 
-static int ipw_wpa_supplicant(struct net_device *dev, struct iw_point *p)
+/*
+* background support to run QoS activate functionality
+*/
+static void ipw_bg_qos_activate(struct work_struct *work)
 {
-       struct ipw_param *param;
-       int ret = 0;
+       struct ipw_priv *priv =
+               container_of(work, struct ipw_priv, qos_activate);
 
-       IPW_DEBUG_INFO("wpa_supplicant: len=%d\n", p->length);
+       if (priv == NULL)
+               return;
 
-       if (p->length < sizeof(struct ipw_param) || !p->pointer)
-               return -EINVAL;
+       mutex_lock(&priv->mutex);
 
-       param = (struct ipw_param *)kmalloc(p->length, GFP_KERNEL);
-       if (param == NULL)
-               return -ENOMEM;
+       if (priv->status & STATUS_ASSOCIATED)
+               ipw_qos_activate(priv, &(priv->assoc_network->qos_data));
 
-       if (copy_from_user(param, p->pointer, p->length)) {
-               kfree(param);
-               return -EFAULT;
-       }
+       mutex_unlock(&priv->mutex);
+}
+
+static int ipw_handle_probe_response(struct net_device *dev,
+                                    struct ieee80211_probe_response *resp,
+                                    struct ieee80211_network *network)
+{
+       struct ipw_priv *priv = ieee80211_priv(dev);
+       int active_network = ((priv->status & STATUS_ASSOCIATED) &&
+                             (network == priv->assoc_network));
 
-       switch (param->cmd) {
+       ipw_qos_handle_probe_response(priv, active_network, network);
 
-       case IPW_CMD_SET_WPA_PARAM:
-               ret = ipw_wpa_set_param(dev, param->u.wpa_param.name,
-                                       param->u.wpa_param.value);
-               break;
+       return 0;
+}
 
-       case IPW_CMD_SET_WPA_IE:
-               ret = ipw_wpa_set_wpa_ie(dev, param, p->length);
-               break;
+static int ipw_handle_beacon(struct net_device *dev,
+                            struct ieee80211_beacon *resp,
+                            struct ieee80211_network *network)
+{
+       struct ipw_priv *priv = ieee80211_priv(dev);
+       int active_network = ((priv->status & STATUS_ASSOCIATED) &&
+                             (network == priv->assoc_network));
 
-       case IPW_CMD_SET_ENCRYPTION:
-               ret = ipw_wpa_set_encryption(dev, param, p->length);
-               break;
+       ipw_qos_handle_probe_response(priv, active_network, network);
 
-       case IPW_CMD_MLME:
-               ret = ipw_wpa_mlme(dev, param->u.mlme.command,
-                                  param->u.mlme.reason_code);
-               break;
+       return 0;
+}
 
-       default:
-               IPW_ERROR("%s: Unknown WPA supplicant request: %d\n",
-                         dev->name, param->cmd);
-               ret = -EOPNOTSUPP;
-       }
+static int ipw_handle_assoc_response(struct net_device *dev,
+                                    struct ieee80211_assoc_response *resp,
+                                    struct ieee80211_network *network)
+{
+       struct ipw_priv *priv = ieee80211_priv(dev);
+       ipw_qos_association_resp(priv, network);
+       return 0;
+}
 
-       if (ret == 0 && copy_to_user(p->pointer, param, p->length))
-               ret = -EFAULT;
+static int ipw_send_qos_params_command(struct ipw_priv *priv, struct ieee80211_qos_parameters
+                                      *qos_param)
+{
+       return ipw_send_cmd_pdu(priv, IPW_CMD_QOS_PARAMETERS,
+                               sizeof(*qos_param) * 3, qos_param);
+}
 
-       kfree(param);
-       return ret;
+static int ipw_send_qos_info_command(struct ipw_priv *priv, struct ieee80211_qos_information_element
+                                    *qos_param)
+{
+       return ipw_send_cmd_pdu(priv, IPW_CMD_WME_INFO, sizeof(*qos_param),
+                               qos_param);
 }
 
+#endif                         /* CONFIG_IPW2200_QOS */
+
 static int ipw_associate_network(struct ipw_priv *priv,
                                 struct ieee80211_network *network,
                                 struct ipw_supported_rates *rates, int roaming)
 {
        int err;
+       DECLARE_MAC_BUF(mac);
 
        if (priv->config & CFG_FIXED_RATE)
-               ipw_set_fixed_rate(priv, network);
+               ipw_set_fixed_rate(priv, network->mode);
 
        if (!(priv->config & CFG_STATIC_ESSID)) {
                priv->essid_len = min(network->ssid_len,
@@ -6036,20 +7311,24 @@ static int ipw_associate_network(struct ipw_priv *priv,
 
        memset(&priv->assoc_request, 0, sizeof(priv->assoc_request));
        priv->assoc_request.channel = network->channel;
+       priv->assoc_request.auth_key = 0;
+
        if ((priv->capability & CAP_PRIVACY_ON) &&
-           (priv->capability & CAP_SHARED_KEY)) {
+           (priv->ieee->sec.auth_mode == WLAN_AUTH_SHARED_KEY)) {
                priv->assoc_request.auth_type = AUTH_SHARED_KEY;
-               priv->assoc_request.auth_key = priv->sec.active_key;
-       } else {
-               priv->assoc_request.auth_type = AUTH_OPEN;
-               priv->assoc_request.auth_key = 0;
-       }
+               priv->assoc_request.auth_key = priv->ieee->sec.active_key;
 
-       if (priv->capability & CAP_PRIVACY_ON)
-               ipw_send_wep_keys(priv);
+               if (priv->ieee->sec.level == SEC_LEVEL_1)
+                       ipw_send_wep_keys(priv, DCW_WEP_KEY_SEC_TYPE_WEP);
+
+       } else if ((priv->capability & CAP_PRIVACY_ON) &&
+                  (priv->ieee->sec.auth_mode == WLAN_AUTH_LEAP))
+               priv->assoc_request.auth_type = AUTH_LEAP;
+       else
+               priv->assoc_request.auth_type = AUTH_OPEN;
 
        if (priv->ieee->wpa_ie_len) {
-               priv->assoc_request.policy_support = 0x02;      /* RSN active */
+               priv->assoc_request.policy_support = cpu_to_le16(0x02); /* RSN active */
                ipw_set_rsn_capa(priv, priv->ieee->wpa_ie,
                                 priv->ieee->wpa_ie_len);
        }
@@ -6066,7 +7345,7 @@ static int ipw_associate_network(struct ipw_priv *priv,
        else if (network->mode & priv->ieee->mode & IEEE_B)
                priv->assoc_request.ieee_mode = IPW_B_MODE;
 
-       priv->assoc_request.capability = network->capability;
+       priv->assoc_request.capability = cpu_to_le16(network->capability);
        if ((network->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
            && !(priv->config & CFG_PREAMBLE_LONG)) {
                priv->assoc_request.preamble_length = DCT_FLAG_SHORT_PREAMBLE;
@@ -6075,9 +7354,14 @@ static int ipw_associate_network(struct ipw_priv *priv,
 
                /* Clear the short preamble if we won't be supporting it */
                priv->assoc_request.capability &=
-                   ~WLAN_CAPABILITY_SHORT_PREAMBLE;
+                   ~cpu_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE);
        }
 
+       /* Clear capability bits that aren't used in Ad Hoc */
+       if (priv->ieee->iw_mode == IW_MODE_ADHOC)
+               priv->assoc_request.capability &=
+                   ~cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT_TIME);
+
        IPW_DEBUG_ASSOC("%sssocation attempt: '%s', channel %d, "
                        "802.11%c [%d], %s[:%s], enc=%s%s%s%c%c\n",
                        roaming ? "Rea" : "A",
@@ -6095,10 +7379,10 @@ static int ipw_associate_network(struct ipw_priv *priv,
                         "(open)") : "",
                        priv->capability & CAP_PRIVACY_ON ? " key=" : "",
                        priv->capability & CAP_PRIVACY_ON ?
-                       '1' + priv->sec.active_key : '.',
+                       '1' + priv->ieee->sec.active_key : '.',
                        priv->capability & CAP_PRIVACY_ON ? '.' : ' ');
 
-       priv->assoc_request.beacon_interval = network->beacon_interval;
+       priv->assoc_request.beacon_interval = cpu_to_le16(network->beacon_interval);
        if ((priv->ieee->iw_mode == IW_MODE_ADHOC) &&
            (network->time_stamp[0] == 0) && (network->time_stamp[1] == 0)) {
                priv->assoc_request.assoc_type = HC_IBSS_START;
@@ -6109,21 +7393,21 @@ static int ipw_associate_network(struct ipw_priv *priv,
                        priv->assoc_request.assoc_type = HC_REASSOCIATE;
                else
                        priv->assoc_request.assoc_type = HC_ASSOCIATE;
-               priv->assoc_request.assoc_tsf_msw = network->time_stamp[1];
-               priv->assoc_request.assoc_tsf_lsw = network->time_stamp[0];
+               priv->assoc_request.assoc_tsf_msw = cpu_to_le32(network->time_stamp[1]);
+               priv->assoc_request.assoc_tsf_lsw = cpu_to_le32(network->time_stamp[0]);
        }
 
-       memcpy(&priv->assoc_request.bssid, network->bssid, ETH_ALEN);
+       memcpy(priv->assoc_request.bssid, network->bssid, ETH_ALEN);
 
        if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
                memset(&priv->assoc_request.dest, 0xFF, ETH_ALEN);
-               priv->assoc_request.atim_window = network->atim_window;
+               priv->assoc_request.atim_window = cpu_to_le16(network->atim_window);
        } else {
-               memcpy(&priv->assoc_request.dest, network->bssid, ETH_ALEN);
+               memcpy(priv->assoc_request.dest, network->bssid, ETH_ALEN);
                priv->assoc_request.atim_window = 0;
        }
 
-       priv->assoc_request.listen_interval = network->listen_interval;
+       priv->assoc_request.listen_interval = cpu_to_le16(network->listen_interval);
 
        err = ipw_send_ssid(priv, priv->essid, priv->essid_len);
        if (err) {
@@ -6145,7 +7429,7 @@ static int ipw_associate_network(struct ipw_priv *priv,
        else
                priv->sys_config.answer_broadcast_ssid_probe = 0;
 
-       err = ipw_send_system_config(priv, &priv->sys_config);
+       err = ipw_send_system_config(priv);
        if (err) {
                IPW_DEBUG_HC("Attempt to send sys config command failed.\n");
                return err;
@@ -6170,15 +7454,19 @@ static int ipw_associate_network(struct ipw_priv *priv,
 
        priv->assoc_network = network;
 
+#ifdef CONFIG_IPW2200_QOS
+       ipw_qos_association(priv, network);
+#endif
+
        err = ipw_send_associate(priv, &priv->assoc_request);
        if (err) {
                IPW_DEBUG_HC("Attempt to send associate command failed.\n");
                return err;
        }
 
-       IPW_DEBUG(IPW_DL_STATE, "associating: '%s' " MAC_FMT " \n",
+       IPW_DEBUG(IPW_DL_STATE, "associating: '%s' %s \n",
                  escape_essid(priv->essid, priv->essid_len),
-                 MAC_ARG(priv->bssid));
+                 print_mac(mac, priv->bssid));
 
        return 0;
 }
@@ -6248,12 +7536,13 @@ static void ipw_roam(void *data)
        priv->status &= ~STATUS_ROAMING;
 }
 
-static void ipw_bg_roam(void *data)
+static void ipw_bg_roam(struct work_struct *work)
 {
-       struct ipw_priv *priv = data;
-       down(&priv->sem);
-       ipw_roam(data);
-       up(&priv->sem);
+       struct ipw_priv *priv =
+               container_of(work, struct ipw_priv, roam);
+       mutex_lock(&priv->mutex);
+       ipw_roam(priv);
+       mutex_unlock(&priv->mutex);
 }
 
 static int ipw_associate(void *data)
@@ -6268,15 +7557,27 @@ static int ipw_associate(void *data)
        struct list_head *element;
        unsigned long flags;
 
+       if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
+               IPW_DEBUG_ASSOC("Not attempting association (monitor mode)\n");
+               return 0;
+       }
+
        if (priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) {
-               IPW_DEBUG_ASSOC
-                   ("Not attempting association (already in progress)\n");
+               IPW_DEBUG_ASSOC("Not attempting association (already in "
+                               "progress)\n");
+               return 0;
+       }
+
+       if (priv->status & STATUS_DISASSOCIATING) {
+               IPW_DEBUG_ASSOC("Not attempting association (in "
+                               "disassociating)\n ");
+               queue_work(priv->workqueue, &priv->associate);
                return 0;
        }
 
        if (!ipw_is_init(priv) || (priv->status & STATUS_SCANNING)) {
-               IPW_DEBUG_ASSOC
-                   ("Not attempting association (scanning or not initialized)\n");
+               IPW_DEBUG_ASSOC("Not attempting association (scanning or not "
+                               "initialized)\n");
                return 0;
        }
 
@@ -6299,8 +7600,31 @@ static int ipw_associate(void *data)
            priv->ieee->iw_mode == IW_MODE_ADHOC &&
            priv->config & CFG_ADHOC_CREATE &&
            priv->config & CFG_STATIC_ESSID &&
-           priv->config & CFG_STATIC_CHANNEL &&
-           !list_empty(&priv->ieee->network_free_list)) {
+           priv->config & CFG_STATIC_CHANNEL) {
+               /* Use oldest network if the free list is empty */
+               if (list_empty(&priv->ieee->network_free_list)) {
+                       struct ieee80211_network *oldest = NULL;
+                       struct ieee80211_network *target;
+                       DECLARE_MAC_BUF(mac);
+
+                       list_for_each_entry(target, &priv->ieee->network_list, list) {
+                               if ((oldest == NULL) ||
+                                   (target->last_scanned < oldest->last_scanned))
+                                       oldest = target;
+                       }
+
+                       /* If there are no more slots, expire the oldest */
+                       list_del(&oldest->list);
+                       target = oldest;
+                       IPW_DEBUG_ASSOC("Expired '%s' (%s) from "
+                                       "network list.\n",
+                                       escape_essid(target->ssid,
+                                                    target->ssid_len),
+                                       print_mac(mac, target->bssid));
+                       list_add_tail(&target->list,
+                                     &priv->ieee->network_free_list);
+               }
+
                element = priv->ieee->network_free_list.next;
                network = list_entry(element, struct ieee80211_network, list);
                ipw_adhoc_create(priv, network);
@@ -6315,9 +7639,15 @@ static int ipw_associate(void *data)
        if (!network) {
                ipw_debug_config(priv);
 
-               if (!(priv->status & STATUS_SCANNING))
-                       queue_delayed_work(priv->workqueue, &priv->request_scan,
-                                          SCAN_INTERVAL);
+               if (!(priv->status & STATUS_SCANNING)) {
+                       if (!(priv->config & CFG_SPEED_SCAN))
+                               queue_delayed_work(priv->workqueue,
+                                                  &priv->request_scan,
+                                                  SCAN_INTERVAL);
+                       else
+                               queue_delayed_work(priv->workqueue,
+                                                  &priv->request_scan, 0);
+               }
 
                return 0;
        }
@@ -6327,19 +7657,122 @@ static int ipw_associate(void *data)
        return 1;
 }
 
-static void ipw_bg_associate(void *data)
+static void ipw_bg_associate(struct work_struct *work)
 {
-       struct ipw_priv *priv = data;
-       down(&priv->sem);
-       ipw_associate(data);
-       up(&priv->sem);
+       struct ipw_priv *priv =
+               container_of(work, struct ipw_priv, associate);
+       mutex_lock(&priv->mutex);
+       ipw_associate(priv);
+       mutex_unlock(&priv->mutex);
+}
+
+static void ipw_rebuild_decrypted_skb(struct ipw_priv *priv,
+                                     struct sk_buff *skb)
+{
+       struct ieee80211_hdr *hdr;
+       u16 fc;
+
+       hdr = (struct ieee80211_hdr *)skb->data;
+       fc = le16_to_cpu(hdr->frame_ctl);
+       if (!(fc & IEEE80211_FCTL_PROTECTED))
+               return;
+
+       fc &= ~IEEE80211_FCTL_PROTECTED;
+       hdr->frame_ctl = cpu_to_le16(fc);
+       switch (priv->ieee->sec.level) {
+       case SEC_LEVEL_3:
+               /* Remove CCMP HDR */
+               memmove(skb->data + IEEE80211_3ADDR_LEN,
+                       skb->data + IEEE80211_3ADDR_LEN + 8,
+                       skb->len - IEEE80211_3ADDR_LEN - 8);
+               skb_trim(skb, skb->len - 16);   /* CCMP_HDR_LEN + CCMP_MIC_LEN */
+               break;
+       case SEC_LEVEL_2:
+               break;
+       case SEC_LEVEL_1:
+               /* Remove IV */
+               memmove(skb->data + IEEE80211_3ADDR_LEN,
+                       skb->data + IEEE80211_3ADDR_LEN + 4,
+                       skb->len - IEEE80211_3ADDR_LEN - 4);
+               skb_trim(skb, skb->len - 8);    /* IV + ICV */
+               break;
+       case SEC_LEVEL_0:
+               break;
+       default:
+               printk(KERN_ERR "Unknow security level %d\n",
+                      priv->ieee->sec.level);
+               break;
+       }
+}
+
+static void ipw_handle_data_packet(struct ipw_priv *priv,
+                                  struct ipw_rx_mem_buffer *rxb,
+                                  struct ieee80211_rx_stats *stats)
+{
+       struct ieee80211_hdr_4addr *hdr;
+       struct ipw_rx_packet *pkt = (struct ipw_rx_packet *)rxb->skb->data;
+
+       /* We received data from the HW, so stop the watchdog */
+       priv->net_dev->trans_start = jiffies;
+
+       /* We only process data packets if the
+        * interface is open */
+       if (unlikely((le16_to_cpu(pkt->u.frame.length) + IPW_RX_FRAME_SIZE) >
+                    skb_tailroom(rxb->skb))) {
+               priv->ieee->stats.rx_errors++;
+               priv->wstats.discard.misc++;
+               IPW_DEBUG_DROP("Corruption detected! Oh no!\n");
+               return;
+       } else if (unlikely(!netif_running(priv->net_dev))) {
+               priv->ieee->stats.rx_dropped++;
+               priv->wstats.discard.misc++;
+               IPW_DEBUG_DROP("Dropping packet while interface is not up.\n");
+               return;
+       }
+
+       /* Advance skb->data to the start of the actual payload */
+       skb_reserve(rxb->skb, offsetof(struct ipw_rx_packet, u.frame.data));
+
+       /* Set the size of the skb to the size of the frame */
+       skb_put(rxb->skb, le16_to_cpu(pkt->u.frame.length));
+
+       IPW_DEBUG_RX("Rx packet of %d bytes.\n", rxb->skb->len);
+
+       /* HW decrypt will not clear the WEP bit, MIC, PN, etc. */
+       hdr = (struct ieee80211_hdr_4addr *)rxb->skb->data;
+       if (priv->ieee->iw_mode != IW_MODE_MONITOR &&
+           (is_multicast_ether_addr(hdr->addr1) ?
+            !priv->ieee->host_mc_decrypt : !priv->ieee->host_decrypt))
+               ipw_rebuild_decrypted_skb(priv, rxb->skb);
+
+       if (!ieee80211_rx(priv->ieee, rxb->skb, stats))
+               priv->ieee->stats.rx_errors++;
+       else {                  /* ieee80211_rx succeeded, so it now owns the SKB */
+               rxb->skb = NULL;
+               __ipw_led_activity_on(priv);
+       }
 }
 
-static inline void ipw_handle_data_packet(struct ipw_priv *priv,
-                                         struct ipw_rx_mem_buffer *rxb,
-                                         struct ieee80211_rx_stats *stats)
+#ifdef CONFIG_IPW2200_RADIOTAP
+static void ipw_handle_data_packet_monitor(struct ipw_priv *priv,
+                                          struct ipw_rx_mem_buffer *rxb,
+                                          struct ieee80211_rx_stats *stats)
 {
        struct ipw_rx_packet *pkt = (struct ipw_rx_packet *)rxb->skb->data;
+       struct ipw_rx_frame *frame = &pkt->u.frame;
+
+       /* initial pull of some data */
+       u16 received_channel = frame->received_channel;
+       u8 antennaAndPhy = frame->antennaAndPhy;
+       s8 antsignal = frame->rssi_dbm - IPW_RSSI_TO_DBM;       /* call it signed anyhow */
+       u16 pktrate = frame->rate;
+
+       /* Magic struct that slots into the radiotap header -- no reason
+        * to build this manually element by element, we can write it much
+        * more efficiently than we can parse it. ORDER MATTERS HERE */
+       struct ipw_rt_hdr *ipw_rt;
+
+       short len = le16_to_cpu(pkt->u.frame.length);
 
        /* We received data from the HW, so stop the watchdog */
        priv->net_dev->trans_start = jiffies;
@@ -6359,23 +7792,346 @@ static inline void ipw_handle_data_packet(struct ipw_priv *priv,
                return;
        }
 
-       /* Advance skb->data to the start of the actual payload */
-       skb_reserve(rxb->skb, offsetof(struct ipw_rx_packet, u.frame.data));
+       /* Libpcap 0.9.3+ can handle variable length radiotap, so we'll use
+        * that now */
+       if (len > IPW_RX_BUF_SIZE - sizeof(struct ipw_rt_hdr)) {
+               /* FIXME: Should alloc bigger skb instead */
+               priv->ieee->stats.rx_dropped++;
+               priv->wstats.discard.misc++;
+               IPW_DEBUG_DROP("Dropping too large packet in monitor\n");
+               return;
+       }
+
+       /* copy the frame itself */
+       memmove(rxb->skb->data + sizeof(struct ipw_rt_hdr),
+               rxb->skb->data + IPW_RX_FRAME_SIZE, len);
+
+       /* Zero the radiotap static buffer  ...  We only need to zero the bytes NOT
+        * part of our real header, saves a little time.
+        *
+        * No longer necessary since we fill in all our data.  Purge before merging
+        * patch officially.
+        * memset(rxb->skb->data + sizeof(struct ipw_rt_hdr), 0,
+        *        IEEE80211_RADIOTAP_HDRLEN - sizeof(struct ipw_rt_hdr));
+        */
+
+       ipw_rt = (struct ipw_rt_hdr *)rxb->skb->data;
+
+       ipw_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
+       ipw_rt->rt_hdr.it_pad = 0;      /* always good to zero */
+       ipw_rt->rt_hdr.it_len = cpu_to_le16(sizeof(struct ipw_rt_hdr)); /* total header+data */
+
+       /* Big bitfield of all the fields we provide in radiotap */
+       ipw_rt->rt_hdr.it_present = cpu_to_le32(
+            (1 << IEEE80211_RADIOTAP_TSFT) |
+            (1 << IEEE80211_RADIOTAP_FLAGS) |
+            (1 << IEEE80211_RADIOTAP_RATE) |
+            (1 << IEEE80211_RADIOTAP_CHANNEL) |
+            (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
+            (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
+            (1 << IEEE80211_RADIOTAP_ANTENNA));
+
+       /* Zero the flags, we'll add to them as we go */
+       ipw_rt->rt_flags = 0;
+       ipw_rt->rt_tsf = (u64)(frame->parent_tsf[3] << 24 |
+                              frame->parent_tsf[2] << 16 |
+                              frame->parent_tsf[1] << 8  |
+                              frame->parent_tsf[0]);
+
+       /* Convert signal to DBM */
+       ipw_rt->rt_dbmsignal = antsignal;
+       ipw_rt->rt_dbmnoise = frame->noise;
+
+       /* Convert the channel data and set the flags */
+       ipw_rt->rt_channel = cpu_to_le16(ieee80211chan2mhz(received_channel));
+       if (received_channel > 14) {    /* 802.11a */
+               ipw_rt->rt_chbitmask =
+                   cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ));
+       } else if (antennaAndPhy & 32) {        /* 802.11b */
+               ipw_rt->rt_chbitmask =
+                   cpu_to_le16((IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ));
+       } else {                /* 802.11g */
+               ipw_rt->rt_chbitmask =
+                   cpu_to_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ);
+       }
+
+       /* set the rate in multiples of 500k/s */
+       switch (pktrate) {
+       case IPW_TX_RATE_1MB:
+               ipw_rt->rt_rate = 2;
+               break;
+       case IPW_TX_RATE_2MB:
+               ipw_rt->rt_rate = 4;
+               break;
+       case IPW_TX_RATE_5MB:
+               ipw_rt->rt_rate = 10;
+               break;
+       case IPW_TX_RATE_6MB:
+               ipw_rt->rt_rate = 12;
+               break;
+       case IPW_TX_RATE_9MB:
+               ipw_rt->rt_rate = 18;
+               break;
+       case IPW_TX_RATE_11MB:
+               ipw_rt->rt_rate = 22;
+               break;
+       case IPW_TX_RATE_12MB:
+               ipw_rt->rt_rate = 24;
+               break;
+       case IPW_TX_RATE_18MB:
+               ipw_rt->rt_rate = 36;
+               break;
+       case IPW_TX_RATE_24MB:
+               ipw_rt->rt_rate = 48;
+               break;
+       case IPW_TX_RATE_36MB:
+               ipw_rt->rt_rate = 72;
+               break;
+       case IPW_TX_RATE_48MB:
+               ipw_rt->rt_rate = 96;
+               break;
+       case IPW_TX_RATE_54MB:
+               ipw_rt->rt_rate = 108;
+               break;
+       default:
+               ipw_rt->rt_rate = 0;
+               break;
+       }
+
+       /* antenna number */
+       ipw_rt->rt_antenna = (antennaAndPhy & 3);       /* Is this right? */
+
+       /* set the preamble flag if we have it */
+       if ((antennaAndPhy & 64))
+               ipw_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
+
+       /* Set the size of the skb to the size of the frame */
+       skb_put(rxb->skb, len + sizeof(struct ipw_rt_hdr));
+
+       IPW_DEBUG_RX("Rx packet of %d bytes.\n", rxb->skb->len);
+
+       if (!ieee80211_rx(priv->ieee, rxb->skb, stats))
+               priv->ieee->stats.rx_errors++;
+       else {                  /* ieee80211_rx succeeded, so it now owns the SKB */
+               rxb->skb = NULL;
+               /* no LED during capture */
+       }
+}
+#endif
+
+#ifdef CONFIG_IPW2200_PROMISCUOUS
+#define ieee80211_is_probe_response(fc) \
+   ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT && \
+    (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP )
+
+#define ieee80211_is_management(fc) \
+   ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
+
+#define ieee80211_is_control(fc) \
+   ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL)
+
+#define ieee80211_is_data(fc) \
+   ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
+
+#define ieee80211_is_assoc_request(fc) \
+   ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ)
+
+#define ieee80211_is_reassoc_request(fc) \
+   ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
+
+static void ipw_handle_promiscuous_rx(struct ipw_priv *priv,
+                                     struct ipw_rx_mem_buffer *rxb,
+                                     struct ieee80211_rx_stats *stats)
+{
+       struct ipw_rx_packet *pkt = (struct ipw_rx_packet *)rxb->skb->data;
+       struct ipw_rx_frame *frame = &pkt->u.frame;
+       struct ipw_rt_hdr *ipw_rt;
+
+       /* First cache any information we need before we overwrite
+        * the information provided in the skb from the hardware */
+       struct ieee80211_hdr *hdr;
+       u16 channel = frame->received_channel;
+       u8 phy_flags = frame->antennaAndPhy;
+       s8 signal = frame->rssi_dbm - IPW_RSSI_TO_DBM;
+       s8 noise = frame->noise;
+       u8 rate = frame->rate;
+       short len = le16_to_cpu(pkt->u.frame.length);
+       struct sk_buff *skb;
+       int hdr_only = 0;
+       u16 filter = priv->prom_priv->filter;
+
+       /* If the filter is set to not include Rx frames then return */
+       if (filter & IPW_PROM_NO_RX)
+               return;
+
+       /* We received data from the HW, so stop the watchdog */
+       priv->prom_net_dev->trans_start = jiffies;
+
+       if (unlikely((len + IPW_RX_FRAME_SIZE) > skb_tailroom(rxb->skb))) {
+               priv->prom_priv->ieee->stats.rx_errors++;
+               IPW_DEBUG_DROP("Corruption detected! Oh no!\n");
+               return;
+       }
+
+       /* We only process data packets if the interface is open */
+       if (unlikely(!netif_running(priv->prom_net_dev))) {
+               priv->prom_priv->ieee->stats.rx_dropped++;
+               IPW_DEBUG_DROP("Dropping packet while interface is not up.\n");
+               return;
+       }
+
+       /* Libpcap 0.9.3+ can handle variable length radiotap, so we'll use
+        * that now */
+       if (len > IPW_RX_BUF_SIZE - sizeof(struct ipw_rt_hdr)) {
+               /* FIXME: Should alloc bigger skb instead */
+               priv->prom_priv->ieee->stats.rx_dropped++;
+               IPW_DEBUG_DROP("Dropping too large packet in monitor\n");
+               return;
+       }
+
+       hdr = (void *)rxb->skb->data + IPW_RX_FRAME_SIZE;
+       if (ieee80211_is_management(le16_to_cpu(hdr->frame_ctl))) {
+               if (filter & IPW_PROM_NO_MGMT)
+                       return;
+               if (filter & IPW_PROM_MGMT_HEADER_ONLY)
+                       hdr_only = 1;
+       } else if (ieee80211_is_control(le16_to_cpu(hdr->frame_ctl))) {
+               if (filter & IPW_PROM_NO_CTL)
+                       return;
+               if (filter & IPW_PROM_CTL_HEADER_ONLY)
+                       hdr_only = 1;
+       } else if (ieee80211_is_data(le16_to_cpu(hdr->frame_ctl))) {
+               if (filter & IPW_PROM_NO_DATA)
+                       return;
+               if (filter & IPW_PROM_DATA_HEADER_ONLY)
+                       hdr_only = 1;
+       }
+
+       /* Copy the SKB since this is for the promiscuous side */
+       skb = skb_copy(rxb->skb, GFP_ATOMIC);
+       if (skb == NULL) {
+               IPW_ERROR("skb_clone failed for promiscuous copy.\n");
+               return;
+       }
+
+       /* copy the frame data to write after where the radiotap header goes */
+       ipw_rt = (void *)skb->data;
+
+       if (hdr_only)
+               len = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
+
+       memcpy(ipw_rt->payload, hdr, len);
+
+       /* Zero the radiotap static buffer  ...  We only need to zero the bytes
+        * NOT part of our real header, saves a little time.
+        *
+        * No longer necessary since we fill in all our data.  Purge before
+        * merging patch officially.
+        * memset(rxb->skb->data + sizeof(struct ipw_rt_hdr), 0,
+        *        IEEE80211_RADIOTAP_HDRLEN - sizeof(struct ipw_rt_hdr));
+        */
+
+       ipw_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
+       ipw_rt->rt_hdr.it_pad = 0;      /* always good to zero */
+       ipw_rt->rt_hdr.it_len = cpu_to_le16(sizeof(*ipw_rt));   /* total header+data */
+
+       /* Set the size of the skb to the size of the frame */
+       skb_put(skb, sizeof(*ipw_rt) + len);
+
+       /* Big bitfield of all the fields we provide in radiotap */
+       ipw_rt->rt_hdr.it_present = cpu_to_le32(
+            (1 << IEEE80211_RADIOTAP_TSFT) |
+            (1 << IEEE80211_RADIOTAP_FLAGS) |
+            (1 << IEEE80211_RADIOTAP_RATE) |
+            (1 << IEEE80211_RADIOTAP_CHANNEL) |
+            (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
+            (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
+            (1 << IEEE80211_RADIOTAP_ANTENNA));
+
+       /* Zero the flags, we'll add to them as we go */
+       ipw_rt->rt_flags = 0;
+       ipw_rt->rt_tsf = (u64)(frame->parent_tsf[3] << 24 |
+                              frame->parent_tsf[2] << 16 |
+                              frame->parent_tsf[1] << 8  |
+                              frame->parent_tsf[0]);
+
+       /* Convert to DBM */
+       ipw_rt->rt_dbmsignal = signal;
+       ipw_rt->rt_dbmnoise = noise;
+
+       /* Convert the channel data and set the flags */
+       ipw_rt->rt_channel = cpu_to_le16(ieee80211chan2mhz(channel));
+       if (channel > 14) {     /* 802.11a */
+               ipw_rt->rt_chbitmask =
+                   cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ));
+       } else if (phy_flags & (1 << 5)) {      /* 802.11b */
+               ipw_rt->rt_chbitmask =
+                   cpu_to_le16((IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ));
+       } else {                /* 802.11g */
+               ipw_rt->rt_chbitmask =
+                   cpu_to_le16(IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ);
+       }
+
+       /* set the rate in multiples of 500k/s */
+       switch (rate) {
+       case IPW_TX_RATE_1MB:
+               ipw_rt->rt_rate = 2;
+               break;
+       case IPW_TX_RATE_2MB:
+               ipw_rt->rt_rate = 4;
+               break;
+       case IPW_TX_RATE_5MB:
+               ipw_rt->rt_rate = 10;
+               break;
+       case IPW_TX_RATE_6MB:
+               ipw_rt->rt_rate = 12;
+               break;
+       case IPW_TX_RATE_9MB:
+               ipw_rt->rt_rate = 18;
+               break;
+       case IPW_TX_RATE_11MB:
+               ipw_rt->rt_rate = 22;
+               break;
+       case IPW_TX_RATE_12MB:
+               ipw_rt->rt_rate = 24;
+               break;
+       case IPW_TX_RATE_18MB:
+               ipw_rt->rt_rate = 36;
+               break;
+       case IPW_TX_RATE_24MB:
+               ipw_rt->rt_rate = 48;
+               break;
+       case IPW_TX_RATE_36MB:
+               ipw_rt->rt_rate = 72;
+               break;
+       case IPW_TX_RATE_48MB:
+               ipw_rt->rt_rate = 96;
+               break;
+       case IPW_TX_RATE_54MB:
+               ipw_rt->rt_rate = 108;
+               break;
+       default:
+               ipw_rt->rt_rate = 0;
+               break;
+       }
+
+       /* antenna number */
+       ipw_rt->rt_antenna = (phy_flags & 3);
 
-       /* Set the size of the skb to the size of the frame */
-       skb_put(rxb->skb, le16_to_cpu(pkt->u.frame.length));
+       /* set the preamble flag if we have it */
+       if (phy_flags & (1 << 6))
+               ipw_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
 
-       IPW_DEBUG_RX("Rx packet of %d bytes.\n", rxb->skb->len);
+       IPW_DEBUG_RX("Rx packet of %d bytes.\n", skb->len);
 
-       if (!ieee80211_rx(priv->ieee, rxb->skb, stats))
-               priv->ieee->stats.rx_errors++;
-       else {                  /* ieee80211_rx succeeded, so it now owns the SKB */
-               rxb->skb = NULL;
-               ipw_led_activity_on(priv);
+       if (!ieee80211_rx(priv->prom_priv->ieee, skb, stats)) {
+               priv->prom_priv->ieee->stats.rx_errors++;
+               dev_kfree_skb_any(skb);
        }
 }
+#endif
 
-static inline int is_network_packet(struct ipw_priv *priv,
+static int is_network_packet(struct ipw_priv *priv,
                                    struct ieee80211_hdr_4addr *header)
 {
        /* Filter incoming packets to determine if they are targetted toward
@@ -6386,23 +8142,21 @@ static inline int is_network_packet(struct ipw_priv *priv,
                if (!memcmp(header->addr2, priv->net_dev->dev_addr, ETH_ALEN))
                        return 0;
 
-               /* {broad,multi}cast packets to our IBSS go through */
-               if (is_broadcast_ether_addr(header->addr1) ||
-                   is_multicast_ether_addr(header->addr1))
+               /* {broad,multi}cast packets to our BSSID go through */
+               if (is_multicast_ether_addr(header->addr1))
                        return !memcmp(header->addr3, priv->bssid, ETH_ALEN);
 
                /* packets to our adapter go through */
                return !memcmp(header->addr1, priv->net_dev->dev_addr,
                               ETH_ALEN);
 
-       case IW_MODE_INFRA:     /* Header: Dest. | AP{BSSID} | Source */
+       case IW_MODE_INFRA:     /* Header: Dest. | BSSID | Source */
                /* packets from our adapter are dropped (echo) */
                if (!memcmp(header->addr3, priv->net_dev->dev_addr, ETH_ALEN))
                        return 0;
 
-               /* {broad,multi}cast packets to our IBSS go through */
-               if (is_broadcast_ether_addr(header->addr1) ||
-                   is_multicast_ether_addr(header->addr1))
+               /* {broad,multi}cast packets to our BSS go through */
+               if (is_multicast_ether_addr(header->addr1))
                        return !memcmp(header->addr2, priv->bssid, ETH_ALEN);
 
                /* packets to our adapter go through */
@@ -6413,6 +8167,128 @@ static inline int is_network_packet(struct ipw_priv *priv,
        return 1;
 }
 
+#define IPW_PACKET_RETRY_TIME HZ
+
+static  int is_duplicate_packet(struct ipw_priv *priv,
+                                     struct ieee80211_hdr_4addr *header)
+{
+       u16 sc = le16_to_cpu(header->seq_ctl);
+       u16 seq = WLAN_GET_SEQ_SEQ(sc);
+       u16 frag = WLAN_GET_SEQ_FRAG(sc);
+       u16 *last_seq, *last_frag;
+       unsigned long *last_time;
+
+       switch (priv->ieee->iw_mode) {
+       case IW_MODE_ADHOC:
+               {
+                       struct list_head *p;
+                       struct ipw_ibss_seq *entry = NULL;
+                       u8 *mac = header->addr2;
+                       int index = mac[5] % IPW_IBSS_MAC_HASH_SIZE;
+
+                       __list_for_each(p, &priv->ibss_mac_hash[index]) {
+                               entry =
+                                   list_entry(p, struct ipw_ibss_seq, list);
+                               if (!memcmp(entry->mac, mac, ETH_ALEN))
+                                       break;
+                       }
+                       if (p == &priv->ibss_mac_hash[index]) {
+                               entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
+                               if (!entry) {
+                                       IPW_ERROR
+                                           ("Cannot malloc new mac entry\n");
+                                       return 0;
+                               }
+                               memcpy(entry->mac, mac, ETH_ALEN);
+                               entry->seq_num = seq;
+                               entry->frag_num = frag;
+                               entry->packet_time = jiffies;
+                               list_add(&entry->list,
+                                        &priv->ibss_mac_hash[index]);
+                               return 0;
+                       }
+                       last_seq = &entry->seq_num;
+                       last_frag = &entry->frag_num;
+                       last_time = &entry->packet_time;
+                       break;
+               }
+       case IW_MODE_INFRA:
+               last_seq = &priv->last_seq_num;
+               last_frag = &priv->last_frag_num;
+               last_time = &priv->last_packet_time;
+               break;
+       default:
+               return 0;
+       }
+       if ((*last_seq == seq) &&
+           time_after(*last_time + IPW_PACKET_RETRY_TIME, jiffies)) {
+               if (*last_frag == frag)
+                       goto drop;
+               if (*last_frag + 1 != frag)
+                       /* out-of-order fragment */
+                       goto drop;
+       } else
+               *last_seq = seq;
+
+       *last_frag = frag;
+       *last_time = jiffies;
+       return 0;
+
+      drop:
+       /* Comment this line now since we observed the card receives
+        * duplicate packets but the FCTL_RETRY bit is not set in the
+        * IBSS mode with fragmentation enabled.
+        BUG_ON(!(le16_to_cpu(header->frame_ctl) & IEEE80211_FCTL_RETRY)); */
+       return 1;
+}
+
+static void ipw_handle_mgmt_packet(struct ipw_priv *priv,
+                                  struct ipw_rx_mem_buffer *rxb,
+                                  struct ieee80211_rx_stats *stats)
+{
+       struct sk_buff *skb = rxb->skb;
+       struct ipw_rx_packet *pkt = (struct ipw_rx_packet *)skb->data;
+       struct ieee80211_hdr_4addr *header = (struct ieee80211_hdr_4addr *)
+           (skb->data + IPW_RX_FRAME_SIZE);
+
+       ieee80211_rx_mgt(priv->ieee, header, stats);
+
+       if (priv->ieee->iw_mode == IW_MODE_ADHOC &&
+           ((WLAN_FC_GET_STYPE(le16_to_cpu(header->frame_ctl)) ==
+             IEEE80211_STYPE_PROBE_RESP) ||
+            (WLAN_FC_GET_STYPE(le16_to_cpu(header->frame_ctl)) ==
+             IEEE80211_STYPE_BEACON))) {
+               if (!memcmp(header->addr3, priv->bssid, ETH_ALEN))
+                       ipw_add_station(priv, header->addr2);
+       }
+
+       if (priv->config & CFG_NET_STATS) {
+               IPW_DEBUG_HC("sending stat packet\n");
+
+               /* Set the size of the skb to the size of the full
+                * ipw header and 802.11 frame */
+               skb_put(skb, le16_to_cpu(pkt->u.frame.length) +
+                       IPW_RX_FRAME_SIZE);
+
+               /* Advance past the ipw packet header to the 802.11 frame */
+               skb_pull(skb, IPW_RX_FRAME_SIZE);
+
+               /* Push the ieee80211_rx_stats before the 802.11 frame */
+               memcpy(skb_push(skb, sizeof(*stats)), stats, sizeof(*stats));
+
+               skb->dev = priv->ieee->dev;
+
+               /* Point raw at the ieee80211_stats */
+               skb_reset_mac_header(skb);
+
+               skb->pkt_type = PACKET_OTHERHOST;
+               skb->protocol = __constant_htons(ETH_P_80211_STATS);
+               memset(skb->cb, 0, sizeof(rxb->skb->cb));
+               netif_rx(skb);
+               rxb->skb = NULL;
+       }
+}
+
 /*
  * Main entry function for recieving a packet with 80211 headers.  This
  * should be called when ever the FW has notified us that there is a new
@@ -6425,23 +8301,28 @@ static void ipw_rx(struct ipw_priv *priv)
        struct ieee80211_hdr_4addr *header;
        u32 r, w, i;
        u8 network_packet;
+       u8 fill_rx = 0;
+       DECLARE_MAC_BUF(mac);
+       DECLARE_MAC_BUF(mac2);
+       DECLARE_MAC_BUF(mac3);
 
-       r = ipw_read32(priv, CX2_RX_READ_INDEX);
-       w = ipw_read32(priv, CX2_RX_WRITE_INDEX);
-       i = (priv->rxq->processed + 1) % RX_QUEUE_SIZE;
+       r = ipw_read32(priv, IPW_RX_READ_INDEX);
+       w = ipw_read32(priv, IPW_RX_WRITE_INDEX);
+       i = priv->rxq->read;
+
+       if (ipw_rx_queue_space (priv->rxq) > (RX_QUEUE_SIZE / 2))
+               fill_rx = 1;
 
        while (i != r) {
                rxb = priv->rxq->queue[i];
-#ifdef CONFIG_IPW_DEBUG
                if (unlikely(rxb == NULL)) {
                        printk(KERN_CRIT "Queue not allocated!\n");
                        break;
                }
-#endif
                priv->rxq->queue[i] = NULL;
 
                pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
-                                           CX2_RX_BUF_SIZE,
+                                           IPW_RX_BUF_SIZE,
                                            PCI_DMA_FROMDEVICE);
 
                pkt = (struct ipw_rx_packet *)rxb->skb->data;
@@ -6452,11 +8333,11 @@ static void ipw_rx(struct ipw_priv *priv)
                switch (pkt->header.message_type) {
                case RX_FRAME_TYPE:     /* 802.11 frame */  {
                                struct ieee80211_rx_stats stats = {
-                                       .rssi =
-                                           le16_to_cpu(pkt->u.frame.rssi_dbm) -
+                                       .rssi = pkt->u.frame.rssi_dbm -
                                            IPW_RSSI_TO_DBM,
                                        .signal =
-                                           le16_to_cpu(pkt->u.frame.signal),
+                                           le16_to_cpu(pkt->u.frame.rssi_dbm) -
+                                           IPW_RSSI_TO_DBM + 0x100,
                                        .noise =
                                            le16_to_cpu(pkt->u.frame.noise),
                                        .rate = pkt->u.frame.rate,
@@ -6482,10 +8363,22 @@ static void ipw_rx(struct ipw_priv *priv)
 
                                priv->rx_packets++;
 
-#ifdef CONFIG_IPW_MONITOR
+#ifdef CONFIG_IPW2200_PROMISCUOUS
+       if (priv->prom_net_dev && netif_running(priv->prom_net_dev))
+               ipw_handle_promiscuous_rx(priv, rxb, &stats);
+#endif
+
+#ifdef CONFIG_IPW2200_MONITOR
                                if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
-                                       ipw_handle_data_packet(priv, rxb,
-                                                              &stats);
+#ifdef CONFIG_IPW2200_RADIOTAP
+
+                ipw_handle_data_packet_monitor(priv,
+                                              rxb,
+                                              &stats);
+#else
+               ipw_handle_data_packet(priv, rxb,
+                                      &stats);
+#endif
                                        break;
                                }
 #endif
@@ -6505,16 +8398,17 @@ static void ipw_rx(struct ipw_priv *priv)
                                if (network_packet && priv->assoc_network) {
                                        priv->assoc_network->stats.rssi =
                                            stats.rssi;
-                                       average_add(&priv->average_rssi,
-                                                   stats.rssi);
-                                       priv->last_rx_rssi = stats.rssi;
+                                       priv->exp_avg_rssi =
+                                           exponential_average(priv->exp_avg_rssi,
+                                           stats.rssi, DEPTH_RSSI);
                                }
 
                                IPW_DEBUG_RX("Frame: len=%u\n",
                                             le16_to_cpu(pkt->u.frame.length));
 
                                if (le16_to_cpu(pkt->u.frame.length) <
-                                   frame_hdr_len(header)) {
+                                   ieee80211_get_hdrlen(le16_to_cpu(
+                                                   header->frame_ctl))) {
                                        IPW_DEBUG_DROP
                                            ("Received packet is too small. "
                                             "Dropping.\n");
@@ -6525,66 +8419,39 @@ static void ipw_rx(struct ipw_priv *priv)
 
                                switch (WLAN_FC_GET_TYPE
                                        (le16_to_cpu(header->frame_ctl))) {
+
                                case IEEE80211_FTYPE_MGMT:
-                                       ieee80211_rx_mgt(priv->ieee, header,
-                                                        &stats);
-                                       if (priv->ieee->iw_mode == IW_MODE_ADHOC
-                                           &&
-                                           ((WLAN_FC_GET_STYPE
-                                             (le16_to_cpu(header->frame_ctl))
-                                             == IEEE80211_STYPE_PROBE_RESP)
-                                            ||
-                                            (WLAN_FC_GET_STYPE
-                                             (le16_to_cpu(header->frame_ctl))
-                                             == IEEE80211_STYPE_BEACON))) {
-                                               if (!memcmp
-                                                   (header->addr3, priv->bssid,
-                                                    ETH_ALEN))
-                                                       ipw_add_station(priv,
-                                                                       header->
-                                                                       addr2);
-                                               else {
-                                                       struct
-                                                           ieee80211_probe_response
-                                                       *beacon;
-                                                       beacon =
-                                                           (struct
-                                                            ieee80211_probe_response
-                                                            *)header;
-                                                       if (le16_to_cpu
-                                                           (beacon->
-                                                            capability) &
-                                                           WLAN_CAPABILITY_IBSS)
-                                                       {
-                                                               queue_work
-                                                                   (priv->
-                                                                    workqueue,
-                                                                    &priv->
-                                                                    merge_networks);
-                                                       }
-                                               }
-                                       }
+                                       ipw_handle_mgmt_packet(priv, rxb,
+                                                              &stats);
                                        break;
 
                                case IEEE80211_FTYPE_CTL:
                                        break;
 
                                case IEEE80211_FTYPE_DATA:
-                                       if (network_packet)
-                                               ipw_handle_data_packet(priv,
-                                                                      rxb,
-                                                                      &stats);
-                                       else
+                                       if (unlikely(!network_packet ||
+                                                    is_duplicate_packet(priv,
+                                                                        header)))
+                                       {
                                                IPW_DEBUG_DROP("Dropping: "
-                                                              MAC_FMT ", "
-                                                              MAC_FMT ", "
-                                                              MAC_FMT "\n",
-                                                              MAC_ARG(header->
+                                                              "%s, "
+                                                              "%s, "
+                                                              "%s\n",
+                                                              print_mac(mac,
+                                                                        header->
                                                                       addr1),
-                                                              MAC_ARG(header->
+                                                              print_mac(mac2,
+                                                                        header->
                                                                       addr2),
-                                                              MAC_ARG(header->
+                                                              print_mac(mac3,
+                                                                        header->
                                                                       addr3));
+                                               break;
+                                       }
+
+                                       ipw_handle_data_packet(priv, rxb,
+                                                              &stats);
+
                                        break;
                                }
                                break;
@@ -6595,7 +8462,7 @@ static void ipw_rx(struct ipw_priv *priv)
                                    ("Notification: subtype=%02X flags=%02X size=%d\n",
                                     pkt->u.notification.subtype,
                                     pkt->u.notification.flags,
-                                    pkt->u.notification.size);
+                                    le16_to_cpu(pkt->u.notification.size));
                                ipw_rx_notification(priv, &pkt->u.notification);
                                break;
                        }
@@ -6615,18 +8482,160 @@ static void ipw_rx(struct ipw_priv *priv)
                }
 
                pci_unmap_single(priv->pci_dev, rxb->dma_addr,
-                                CX2_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
+                                IPW_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
                list_add_tail(&rxb->list, &priv->rxq->rx_used);
 
                i = (i + 1) % RX_QUEUE_SIZE;
+
+               /* If there are a lot of unsued frames, restock the Rx queue
+                * so the ucode won't assert */
+               if (fill_rx) {
+                       priv->rxq->read = i;
+                       ipw_rx_queue_replenish(priv);
+               }
        }
 
        /* Backtrack one entry */
-       priv->rxq->processed = (i ? i : RX_QUEUE_SIZE) - 1;
-
+       priv->rxq->read = i;
        ipw_rx_queue_restock(priv);
 }
 
+#define DEFAULT_RTS_THRESHOLD     2304U
+#define MIN_RTS_THRESHOLD         1U
+#define MAX_RTS_THRESHOLD         2304U
+#define DEFAULT_BEACON_INTERVAL   100U
+#define        DEFAULT_SHORT_RETRY_LIMIT 7U
+#define        DEFAULT_LONG_RETRY_LIMIT  4U
+
+/**
+ * ipw_sw_reset
+ * @option: options to control different reset behaviour
+ *         0 = reset everything except the 'disable' module_param
+ *         1 = reset everything and print out driver info (for probe only)
+ *         2 = reset everything
+ */
+static int ipw_sw_reset(struct ipw_priv *priv, int option)
+{
+       int band, modulation;
+       int old_mode = priv->ieee->iw_mode;
+
+       /* Initialize module parameter values here */
+       priv->config = 0;
+
+       /* We default to disabling the LED code as right now it causes
+        * too many systems to lock up... */
+       if (!led)
+               priv->config |= CFG_NO_LED;
+
+       if (associate)
+               priv->config |= CFG_ASSOCIATE;
+       else
+               IPW_DEBUG_INFO("Auto associate disabled.\n");
+
+       if (auto_create)
+               priv->config |= CFG_ADHOC_CREATE;
+       else
+               IPW_DEBUG_INFO("Auto adhoc creation disabled.\n");
+
+       priv->config &= ~CFG_STATIC_ESSID;
+       priv->essid_len = 0;
+       memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
+
+       if (disable && option) {
+               priv->status |= STATUS_RF_KILL_SW;
+               IPW_DEBUG_INFO("Radio disabled.\n");
+       }
+
+       if (channel != 0) {
+               priv->config |= CFG_STATIC_CHANNEL;
+               priv->channel = channel;
+               IPW_DEBUG_INFO("Bind to static channel %d\n", channel);
+               /* TODO: Validate that provided channel is in range */
+       }
+#ifdef CONFIG_IPW2200_QOS
+       ipw_qos_init(priv, qos_enable, qos_burst_enable,
+                    burst_duration_CCK, burst_duration_OFDM);
+#endif                         /* CONFIG_IPW2200_QOS */
+
+       switch (mode) {
+       case 1:
+               priv->ieee->iw_mode = IW_MODE_ADHOC;
+               priv->net_dev->type = ARPHRD_ETHER;
+
+               break;
+#ifdef CONFIG_IPW2200_MONITOR
+       case 2:
+               priv->ieee->iw_mode = IW_MODE_MONITOR;
+#ifdef CONFIG_IPW2200_RADIOTAP
+               priv->net_dev->type = ARPHRD_IEEE80211_RADIOTAP;
+#else
+               priv->net_dev->type = ARPHRD_IEEE80211;
+#endif
+               break;
+#endif
+       default:
+       case 0:
+               priv->net_dev->type = ARPHRD_ETHER;
+               priv->ieee->iw_mode = IW_MODE_INFRA;
+               break;
+       }
+
+       if (hwcrypto) {
+               priv->ieee->host_encrypt = 0;
+               priv->ieee->host_encrypt_msdu = 0;
+               priv->ieee->host_decrypt = 0;
+               priv->ieee->host_mc_decrypt = 0;
+       }
+       IPW_DEBUG_INFO("Hardware crypto [%s]\n", hwcrypto ? "on" : "off");
+
+       /* IPW2200/2915 is abled to do hardware fragmentation. */
+       priv->ieee->host_open_frag = 0;
+
+       if ((priv->pci_dev->device == 0x4223) ||
+           (priv->pci_dev->device == 0x4224)) {
+               if (option == 1)
+                       printk(KERN_INFO DRV_NAME
+                              ": Detected Intel PRO/Wireless 2915ABG Network "
+                              "Connection\n");
+               priv->ieee->abg_true = 1;
+               band = IEEE80211_52GHZ_BAND | IEEE80211_24GHZ_BAND;
+               modulation = IEEE80211_OFDM_MODULATION |
+                   IEEE80211_CCK_MODULATION;
+               priv->adapter = IPW_2915ABG;
+               priv->ieee->mode = IEEE_A | IEEE_G | IEEE_B;
+       } else {
+               if (option == 1)
+                       printk(KERN_INFO DRV_NAME
+                              ": Detected Intel PRO/Wireless 2200BG Network "
+                              "Connection\n");
+
+               priv->ieee->abg_true = 0;
+               band = IEEE80211_24GHZ_BAND;
+               modulation = IEEE80211_OFDM_MODULATION |
+                   IEEE80211_CCK_MODULATION;
+               priv->adapter = IPW_2200BG;
+               priv->ieee->mode = IEEE_G | IEEE_B;
+       }
+
+       priv->ieee->freq_band = band;
+       priv->ieee->modulation = modulation;
+
+       priv->rates_mask = IEEE80211_DEFAULT_RATES_MASK;
+
+       priv->disassociate_threshold = IPW_MB_DISASSOCIATE_THRESHOLD_DEFAULT;
+       priv->roaming_threshold = IPW_MB_ROAMING_THRESHOLD_DEFAULT;
+
+       priv->rts_threshold = DEFAULT_RTS_THRESHOLD;
+       priv->short_retry_limit = DEFAULT_SHORT_RETRY_LIMIT;
+       priv->long_retry_limit = DEFAULT_LONG_RETRY_LIMIT;
+
+       /* If power management is turned on, default to AC mode */
+       priv->power_mode = IPW_POWER_AC;
+       priv->tx_power = IPW_TX_POWER_DEFAULT;
+
+       return old_mode == priv->ieee->iw_mode;
+}
+
 /*
  * This file defines the Wireless Extension handlers.  It does not
  * define any methods of hardware manipulation and relies on the
@@ -6642,7 +8651,7 @@ static int ipw_wx_get_name(struct net_device *dev,
                           union iwreq_data *wrqu, char *extra)
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
-       down(&priv->sem);
+       mutex_lock(&priv->mutex);
        if (priv->status & STATUS_RF_KILL_MASK)
                strcpy(wrqu->name, "radio off");
        else if (!(priv->status & STATUS_ASSOCIATED))
@@ -6651,7 +8660,7 @@ static int ipw_wx_get_name(struct net_device *dev,
                snprintf(wrqu->name, IFNAMSIZ, "IEEE 802.11%c",
                         ipw_modes[priv->assoc_request.ieee_mode]);
        IPW_DEBUG_WX("Name: %s\n", wrqu->name);
-       up(&priv->sem);
+       mutex_unlock(&priv->mutex);
        return 0;
 }
 
@@ -6677,6 +8686,28 @@ static int ipw_set_channel(struct ipw_priv *priv, u8 channel)
        IPW_DEBUG_INFO("Setting channel to %i\n", (int)channel);
        priv->channel = channel;
 
+#ifdef CONFIG_IPW2200_MONITOR
+       if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
+               int i;
+               if (priv->status & STATUS_SCANNING) {
+                       IPW_DEBUG_SCAN("Scan abort triggered due to "
+                                      "channel change.\n");
+                       ipw_abort_scan(priv);
+               }
+
+               for (i = 1000; i && (priv->status & STATUS_SCANNING); i--)
+                       udelay(10);
+
+               if (priv->status & STATUS_SCANNING)
+                       IPW_DEBUG_SCAN("Still scanning...\n");
+               else
+                       IPW_DEBUG_SCAN("Took %dms to abort current scan\n",
+                                      1000 - i);
+
+               return 0;
+       }
+#endif                         /* CONFIG_IPW2200_MONITOR */
+
        /* Network configuration changed -- force [re]association */
        IPW_DEBUG_ASSOC("[re]association triggered due to channel change.\n");
        if (!ipw_disassociate(priv))
@@ -6690,32 +8721,47 @@ static int ipw_wx_set_freq(struct net_device *dev,
                           union iwreq_data *wrqu, char *extra)
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
+       const struct ieee80211_geo *geo = ieee80211_get_geo(priv->ieee);
        struct iw_freq *fwrq = &wrqu->freq;
-       int ret = 0;
-
+       int ret = 0, i;
+       u8 channel, flags;
+       int band;
+
+       if (fwrq->m == 0) {
+               IPW_DEBUG_WX("SET Freq/Channel -> any\n");
+               mutex_lock(&priv->mutex);
+               ret = ipw_set_channel(priv, 0);
+               mutex_unlock(&priv->mutex);
+               return ret;
+       }
        /* if setting by freq convert to channel */
        if (fwrq->e == 1) {
-               if ((fwrq->m >= (int)2.412e8 && fwrq->m <= (int)2.487e8)) {
-                       int f = fwrq->m / 100000;
-                       int c = 0;
+               channel = ieee80211_freq_to_channel(priv->ieee, fwrq->m);
+               if (channel == 0)
+                       return -EINVAL;
+       } else
+               channel = fwrq->m;
+
+       if (!(band = ieee80211_is_valid_channel(priv->ieee, channel)))
+               return -EINVAL;
 
-                       while ((c < REG_MAX_CHANNEL) &&
-                              (f != ipw_frequencies[c]))
-                               c++;
+       if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
+               i = ieee80211_channel_to_index(priv->ieee, channel);
+               if (i == -1)
+                       return -EINVAL;
 
-                       /* hack to fall through */
-                       fwrq->e = 0;
-                       fwrq->m = c + 1;
+               flags = (band == IEEE80211_24GHZ_BAND) ?
+                   geo->bg[i].flags : geo->a[i].flags;
+               if (flags & IEEE80211_CH_PASSIVE_ONLY) {
+                       IPW_DEBUG_WX("Invalid Ad-Hoc channel for 802.11a\n");
+                       return -EINVAL;
                }
        }
 
-       if (fwrq->e > 0 || fwrq->m > 1000)
-               return -EOPNOTSUPP;
-
        IPW_DEBUG_WX("SET Freq/Channel -> %d \n", fwrq->m);
-       down(&priv->sem);
-       ret = ipw_set_channel(priv, (u8) fwrq->m);
-       up(&priv->sem);
+       mutex_lock(&priv->mutex);
+       ret = ipw_set_channel(priv, channel);
+       mutex_unlock(&priv->mutex);
        return ret;
 }
 
@@ -6729,14 +8775,31 @@ static int ipw_wx_get_freq(struct net_device *dev,
 
        /* If we are associated, trying to associate, or have a statically
         * configured CHANNEL then return that; otherwise return ANY */
-       down(&priv->sem);
+       mutex_lock(&priv->mutex);
        if (priv->config & CFG_STATIC_CHANNEL ||
-           priv->status & (STATUS_ASSOCIATING | STATUS_ASSOCIATED))
-               wrqu->freq.m = priv->channel;
-       else
+           priv->status & (STATUS_ASSOCIATING | STATUS_ASSOCIATED)) {
+               int i;
+
+               i = ieee80211_channel_to_index(priv->ieee, priv->channel);
+               BUG_ON(i == -1);
+               wrqu->freq.e = 1;
+
+               switch (ieee80211_is_valid_channel(priv->ieee, priv->channel)) {
+               case IEEE80211_52GHZ_BAND:
+                       wrqu->freq.m = priv->ieee->geo.a[i].freq * 100000;
+                       break;
+
+               case IEEE80211_24GHZ_BAND:
+                       wrqu->freq.m = priv->ieee->geo.bg[i].freq * 100000;
+                       break;
+
+               default:
+                       BUG();
+               }
+       } else
                wrqu->freq.m = 0;
 
-       up(&priv->sem);
+       mutex_unlock(&priv->mutex);
        IPW_DEBUG_WX("GET Freq/Channel -> %d \n", priv->channel);
        return 0;
 }
@@ -6749,14 +8812,9 @@ static int ipw_wx_set_mode(struct net_device *dev,
        int err = 0;
 
        IPW_DEBUG_WX("Set MODE: %d\n", wrqu->mode);
-       down(&priv->sem);
-       if (wrqu->mode == priv->ieee->iw_mode) {
-               up(&priv->sem);
-               return 0;
-       }
 
        switch (wrqu->mode) {
-#ifdef CONFIG_IPW_MONITOR
+#ifdef CONFIG_IPW2200_MONITOR
        case IW_MODE_MONITOR:
 #endif
        case IW_MODE_ADHOC:
@@ -6766,34 +8824,35 @@ static int ipw_wx_set_mode(struct net_device *dev,
                wrqu->mode = IW_MODE_INFRA;
                break;
        default:
-               up(&priv->sem);
                return -EINVAL;
        }
+       if (wrqu->mode == priv->ieee->iw_mode)
+               return 0;
+
+       mutex_lock(&priv->mutex);
 
-#ifdef CONFIG_IPW_MONITOR
+       ipw_sw_reset(priv, 0);
+
+#ifdef CONFIG_IPW2200_MONITOR
        if (priv->ieee->iw_mode == IW_MODE_MONITOR)
                priv->net_dev->type = ARPHRD_ETHER;
 
        if (wrqu->mode == IW_MODE_MONITOR)
+#ifdef CONFIG_IPW2200_RADIOTAP
+               priv->net_dev->type = ARPHRD_IEEE80211_RADIOTAP;
+#else
                priv->net_dev->type = ARPHRD_IEEE80211;
-#endif                         /* CONFIG_IPW_MONITOR */
+#endif
+#endif                         /* CONFIG_IPW2200_MONITOR */
 
-#ifdef CONFIG_PM
        /* Free the existing firmware and reset the fw_loaded
         * flag so ipw_load() will bring in the new firmawre */
-       if (fw_loaded)
-               fw_loaded = 0;
-
-       release_firmware(bootfw);
-       release_firmware(ucode);
-       release_firmware(firmware);
-       bootfw = ucode = firmware = NULL;
-#endif
+       free_firmware();
 
        priv->ieee->iw_mode = wrqu->mode;
 
        queue_work(priv->workqueue, &priv->adapter_restart);
-       up(&priv->sem);
+       mutex_unlock(&priv->mutex);
        return err;
 }
 
@@ -6802,20 +8861,13 @@ static int ipw_wx_get_mode(struct net_device *dev,
                           union iwreq_data *wrqu, char *extra)
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
-       down(&priv->sem);
+       mutex_lock(&priv->mutex);
        wrqu->mode = priv->ieee->iw_mode;
        IPW_DEBUG_WX("Get MODE -> %d\n", wrqu->mode);
-       up(&priv->sem);
+       mutex_unlock(&priv->mutex);
        return 0;
 }
 
-#define DEFAULT_RTS_THRESHOLD     2304U
-#define MIN_RTS_THRESHOLD         1U
-#define MAX_RTS_THRESHOLD         2304U
-#define DEFAULT_BEACON_INTERVAL   100U
-#define        DEFAULT_SHORT_RETRY_LIMIT 7U
-#define        DEFAULT_LONG_RETRY_LIMIT  4U
-
 /* Values are in microsecond */
 static const s32 timeout_duration[] = {
        350000,
@@ -6839,8 +8891,8 @@ static int ipw_wx_get_range(struct net_device *dev,
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
        struct iw_range *range = (struct iw_range *)extra;
-       u16 val;
-       int i;
+       const struct ieee80211_geo *geo = ieee80211_get_geo(priv->ieee);
+       int i = 0, j;
 
        wrqu->data.length = sizeof(*range);
        memset(range, 0, sizeof(*range));
@@ -6851,7 +8903,7 @@ static int ipw_wx_get_range(struct net_device *dev,
        range->max_qual.qual = 100;
        /* TODO: Find real max RSSI and stick here */
        range->max_qual.level = 0;
-       range->max_qual.noise = priv->ieee->worst_rssi + 0x100;
+       range->max_qual.noise = 0;
        range->max_qual.updated = 7;    /* Updated all three */
 
        range->avg_qual.qual = 70;
@@ -6859,7 +8911,7 @@ static int ipw_wx_get_range(struct net_device *dev,
        range->avg_qual.level = 0;      /* FIXME to real average level */
        range->avg_qual.noise = 0;
        range->avg_qual.updated = 7;    /* Updated all three */
-       down(&priv->sem);
+       mutex_lock(&priv->mutex);
        range->num_bitrates = min(priv->rates.num_rates, (u8) IW_MAX_BITRATES);
 
        for (i = 0; i < range->num_bitrates; i++)
@@ -6875,24 +8927,54 @@ static int ipw_wx_get_range(struct net_device *dev,
        range->num_encoding_sizes = 2;
        range->max_encoding_tokens = WEP_KEYS;
 
-       /* Set the Wireless Extension versions */
-       range->we_version_compiled = WIRELESS_EXT;
-       range->we_version_source = 16;
+       /* Set the Wireless Extension versions */
+       range->we_version_compiled = WIRELESS_EXT;
+       range->we_version_source = 18;
+
+       i = 0;
+       if (priv->ieee->mode & (IEEE_B | IEEE_G)) {
+               for (j = 0; j < geo->bg_channels && i < IW_MAX_FREQUENCIES; j++) {
+                       if ((priv->ieee->iw_mode == IW_MODE_ADHOC) &&
+                           (geo->bg[j].flags & IEEE80211_CH_PASSIVE_ONLY))
+                               continue;
+
+                       range->freq[i].i = geo->bg[j].channel;
+                       range->freq[i].m = geo->bg[j].freq * 100000;
+                       range->freq[i].e = 1;
+                       i++;
+               }
+       }
+
+       if (priv->ieee->mode & IEEE_A) {
+               for (j = 0; j < geo->a_channels && i < IW_MAX_FREQUENCIES; j++) {
+                       if ((priv->ieee->iw_mode == IW_MODE_ADHOC) &&
+                           (geo->a[j].flags & IEEE80211_CH_PASSIVE_ONLY))
+                               continue;
+
+                       range->freq[i].i = geo->a[j].channel;
+                       range->freq[i].m = geo->a[j].freq * 100000;
+                       range->freq[i].e = 1;
+                       i++;
+               }
+       }
+
+       range->num_channels = i;
+       range->num_frequency = i;
 
-       range->num_channels = FREQ_COUNT;
+       mutex_unlock(&priv->mutex);
 
-       val = 0;
-       for (i = 0; i < FREQ_COUNT; i++) {
-               range->freq[val].i = i + 1;
-               range->freq[val].m = ipw_frequencies[i] * 100000;
-               range->freq[val].e = 1;
-               val++;
+       /* Event capability (kernel + driver) */
+       range->event_capa[0] = (IW_EVENT_CAPA_K_0 |
+                               IW_EVENT_CAPA_MASK(SIOCGIWTHRSPY) |
+                               IW_EVENT_CAPA_MASK(SIOCGIWAP) |
+                               IW_EVENT_CAPA_MASK(SIOCGIWSCAN));
+       range->event_capa[1] = IW_EVENT_CAPA_K_1;
+
+       range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
+               IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
+
+       range->scan_capa = IW_SCAN_CAPA_ESSID | IW_SCAN_CAPA_TYPE;
 
-               if (val == IW_MAX_FREQUENCIES)
-                       break;
-       }
-       range->num_frequency = val;
-       up(&priv->sem);
        IPW_DEBUG_WX("GET Range\n");
        return 0;
 }
@@ -6902,6 +8984,7 @@ static int ipw_wx_set_wap(struct net_device *dev,
                          union iwreq_data *wrqu, char *extra)
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
+       DECLARE_MAC_BUF(mac);
 
        static const unsigned char any[] = {
                0xff, 0xff, 0xff, 0xff, 0xff, 0xff
@@ -6912,7 +8995,7 @@ static int ipw_wx_set_wap(struct net_device *dev,
 
        if (wrqu->ap_addr.sa_family != ARPHRD_ETHER)
                return -EINVAL;
-       down(&priv->sem);
+       mutex_lock(&priv->mutex);
        if (!memcmp(any, wrqu->ap_addr.sa_data, ETH_ALEN) ||
            !memcmp(off, wrqu->ap_addr.sa_data, ETH_ALEN)) {
                /* we disable mandatory BSSID association */
@@ -6921,19 +9004,19 @@ static int ipw_wx_set_wap(struct net_device *dev,
                IPW_DEBUG_ASSOC("Attempting to associate with new "
                                "parameters.\n");
                ipw_associate(priv);
-               up(&priv->sem);
+               mutex_unlock(&priv->mutex);
                return 0;
        }
 
        priv->config |= CFG_STATIC_BSSID;
        if (!memcmp(priv->bssid, wrqu->ap_addr.sa_data, ETH_ALEN)) {
                IPW_DEBUG_WX("BSSID set to current BSSID.\n");
-               up(&priv->sem);
+               mutex_unlock(&priv->mutex);
                return 0;
        }
 
-       IPW_DEBUG_WX("Setting mandatory BSSID to " MAC_FMT "\n",
-                    MAC_ARG(wrqu->ap_addr.sa_data));
+       IPW_DEBUG_WX("Setting mandatory BSSID to %s\n",
+                    print_mac(mac, wrqu->ap_addr.sa_data));
 
        memcpy(priv->bssid, wrqu->ap_addr.sa_data, ETH_ALEN);
 
@@ -6942,7 +9025,7 @@ static int ipw_wx_set_wap(struct net_device *dev,
        if (!ipw_disassociate(priv))
                ipw_associate(priv);
 
-       up(&priv->sem);
+       mutex_unlock(&priv->mutex);
        return 0;
 }
 
@@ -6951,19 +9034,21 @@ static int ipw_wx_get_wap(struct net_device *dev,
                          union iwreq_data *wrqu, char *extra)
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
+       DECLARE_MAC_BUF(mac);
+
        /* If we are associated, trying to associate, or have a statically
         * configured BSSID then return that; otherwise return ANY */
-       down(&priv->sem);
+       mutex_lock(&priv->mutex);
        if (priv->config & CFG_STATIC_BSSID ||
            priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) {
                wrqu->ap_addr.sa_family = ARPHRD_ETHER;
-               memcpy(wrqu->ap_addr.sa_data, &priv->bssid, ETH_ALEN);
+               memcpy(wrqu->ap_addr.sa_data, priv->bssid, ETH_ALEN);
        } else
                memset(wrqu->ap_addr.sa_data, 0, ETH_ALEN);
 
-       IPW_DEBUG_WX("Getting WAP BSSID: " MAC_FMT "\n",
-                    MAC_ARG(wrqu->ap_addr.sa_data));
-       up(&priv->sem);
+       IPW_DEBUG_WX("Getting WAP BSSID: %s\n",
+                    print_mac(mac, wrqu->ap_addr.sa_data));
+       mutex_unlock(&priv->mutex);
        return 0;
 }
 
@@ -6972,47 +9057,43 @@ static int ipw_wx_set_essid(struct net_device *dev,
                            union iwreq_data *wrqu, char *extra)
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
-       char *essid = "";       /* ANY */
-       int length = 0;
-       down(&priv->sem);
-       if (wrqu->essid.flags && wrqu->essid.length) {
-               length = wrqu->essid.length - 1;
-               essid = extra;
-       }
-       if (length == 0) {
-               IPW_DEBUG_WX("Setting ESSID to ANY\n");
-               priv->config &= ~CFG_STATIC_ESSID;
-               if (!(priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING))) {
-                       IPW_DEBUG_ASSOC("Attempting to associate with new "
-                                       "parameters.\n");
-                       ipw_associate(priv);
-               }
-               up(&priv->sem);
-               return 0;
-       }
+        int length;
+
+        mutex_lock(&priv->mutex);
 
-       length = min(length, IW_ESSID_MAX_SIZE);
+        if (!wrqu->essid.flags)
+        {
+                IPW_DEBUG_WX("Setting ESSID to ANY\n");
+                ipw_disassociate(priv);
+                priv->config &= ~CFG_STATIC_ESSID;
+                ipw_associate(priv);
+                mutex_unlock(&priv->mutex);
+                return 0;
+        }
+
+       length = min((int)wrqu->essid.length, IW_ESSID_MAX_SIZE);
 
        priv->config |= CFG_STATIC_ESSID;
 
-       if (priv->essid_len == length && !memcmp(priv->essid, extra, length)) {
+       if (priv->essid_len == length && !memcmp(priv->essid, extra, length)
+           && (priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING))) {
                IPW_DEBUG_WX("ESSID set to current ESSID.\n");
-               up(&priv->sem);
+               mutex_unlock(&priv->mutex);
                return 0;
        }
 
-       IPW_DEBUG_WX("Setting ESSID: '%s' (%d)\n", escape_essid(essid, length),
+       IPW_DEBUG_WX("Setting ESSID: '%s' (%d)\n", escape_essid(extra, length),
                     length);
 
        priv->essid_len = length;
-       memcpy(priv->essid, essid, priv->essid_len);
+       memcpy(priv->essid, extra, priv->essid_len);
 
        /* Network configuration changed -- force [re]association */
        IPW_DEBUG_ASSOC("[re]association triggered due to ESSID change.\n");
        if (!ipw_disassociate(priv))
                ipw_associate(priv);
 
-       up(&priv->sem);
+       mutex_unlock(&priv->mutex);
        return 0;
 }
 
@@ -7024,7 +9105,7 @@ static int ipw_wx_get_essid(struct net_device *dev,
 
        /* If we are associated, trying to associate, or have a statically
         * configured ESSID then return that; otherwise return ANY */
-       down(&priv->sem);
+       mutex_lock(&priv->mutex);
        if (priv->config & CFG_STATIC_ESSID ||
            priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) {
                IPW_DEBUG_WX("Getting essid: '%s'\n",
@@ -7037,7 +9118,7 @@ static int ipw_wx_get_essid(struct net_device *dev,
                wrqu->essid.length = 0;
                wrqu->essid.flags = 0;  /* active */
        }
-       up(&priv->sem);
+       mutex_unlock(&priv->mutex);
        return 0;
 }
 
@@ -7050,12 +9131,12 @@ static int ipw_wx_set_nick(struct net_device *dev,
        IPW_DEBUG_WX("Setting nick to '%s'\n", extra);
        if (wrqu->data.length > IW_ESSID_MAX_SIZE)
                return -E2BIG;
-       down(&priv->sem);
+       mutex_lock(&priv->mutex);
        wrqu->data.length = min((size_t) wrqu->data.length, sizeof(priv->nick));
        memset(priv->nick, 0, sizeof(priv->nick));
        memcpy(priv->nick, extra, wrqu->data.length);
        IPW_DEBUG_TRACE("<<\n");
-       up(&priv->sem);
+       mutex_unlock(&priv->mutex);
        return 0;
 
 }
@@ -7066,11 +9147,57 @@ static int ipw_wx_get_nick(struct net_device *dev,
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
        IPW_DEBUG_WX("Getting nick\n");
-       down(&priv->sem);
-       wrqu->data.length = strlen(priv->nick) + 1;
+       mutex_lock(&priv->mutex);
+       wrqu->data.length = strlen(priv->nick);
        memcpy(extra, priv->nick, wrqu->data.length);
        wrqu->data.flags = 1;   /* active */
-       up(&priv->sem);
+       mutex_unlock(&priv->mutex);
+       return 0;
+}
+
+static int ipw_wx_set_sens(struct net_device *dev,
+                           struct iw_request_info *info,
+                           union iwreq_data *wrqu, char *extra)
+{
+       struct ipw_priv *priv = ieee80211_priv(dev);
+       int err = 0;
+
+       IPW_DEBUG_WX("Setting roaming threshold to %d\n", wrqu->sens.value);
+       IPW_DEBUG_WX("Setting disassociate threshold to %d\n", 3*wrqu->sens.value);
+       mutex_lock(&priv->mutex);
+
+       if (wrqu->sens.fixed == 0)
+       {
+               priv->roaming_threshold = IPW_MB_ROAMING_THRESHOLD_DEFAULT;
+               priv->disassociate_threshold = IPW_MB_DISASSOCIATE_THRESHOLD_DEFAULT;
+               goto out;
+       }
+       if ((wrqu->sens.value > IPW_MB_ROAMING_THRESHOLD_MAX) ||
+           (wrqu->sens.value < IPW_MB_ROAMING_THRESHOLD_MIN)) {
+               err = -EINVAL;
+               goto out;
+       }
+
+       priv->roaming_threshold = wrqu->sens.value;
+       priv->disassociate_threshold = 3*wrqu->sens.value;
+      out:
+       mutex_unlock(&priv->mutex);
+       return err;
+}
+
+static int ipw_wx_get_sens(struct net_device *dev,
+                           struct iw_request_info *info,
+                           union iwreq_data *wrqu, char *extra)
+{
+       struct ipw_priv *priv = ieee80211_priv(dev);
+       mutex_lock(&priv->mutex);
+       wrqu->sens.fixed = 1;
+       wrqu->sens.value = priv->roaming_threshold;
+       mutex_unlock(&priv->mutex);
+
+       IPW_DEBUG_WX("GET roaming threshold -> %s %d \n",
+                    wrqu->power.disabled ? "OFF" : "ON", wrqu->power.value);
+
        return 0;
 }
 
@@ -7163,15 +9290,16 @@ static int ipw_wx_set_rate(struct net_device *dev,
       apply:
        IPW_DEBUG_WX("Setting rate mask to 0x%08X [%s]\n",
                     mask, fixed ? "fixed" : "sub-rates");
-       down(&priv->sem);
-       if (mask == IEEE80211_DEFAULT_RATES_MASK)
+       mutex_lock(&priv->mutex);
+       if (mask == IEEE80211_DEFAULT_RATES_MASK) {
                priv->config &= ~CFG_FIXED_RATE;
-       else
+               ipw_set_fixed_rate(priv, priv->ieee->mode);
+       } else
                priv->config |= CFG_FIXED_RATE;
 
        if (priv->rates_mask == mask) {
                IPW_DEBUG_WX("Mask set to current mask.\n");
-               up(&priv->sem);
+               mutex_unlock(&priv->mutex);
                return 0;
        }
 
@@ -7182,7 +9310,7 @@ static int ipw_wx_set_rate(struct net_device *dev,
        if (!ipw_disassociate(priv))
                ipw_associate(priv);
 
-       up(&priv->sem);
+       mutex_unlock(&priv->mutex);
        return 0;
 }
 
@@ -7191,9 +9319,10 @@ static int ipw_wx_get_rate(struct net_device *dev,
                           union iwreq_data *wrqu, char *extra)
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
-       down(&priv->sem);
+       mutex_lock(&priv->mutex);
        wrqu->bitrate.value = priv->last_rate;
-       up(&priv->sem);
+       wrqu->bitrate.fixed = (priv->config & CFG_FIXED_RATE) ? 1 : 0;
+       mutex_unlock(&priv->mutex);
        IPW_DEBUG_WX("GET Rate -> %d \n", wrqu->bitrate.value);
        return 0;
 }
@@ -7203,20 +9332,20 @@ static int ipw_wx_set_rts(struct net_device *dev,
                          union iwreq_data *wrqu, char *extra)
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
-       down(&priv->sem);
-       if (wrqu->rts.disabled)
+       mutex_lock(&priv->mutex);
+       if (wrqu->rts.disabled || !wrqu->rts.fixed)
                priv->rts_threshold = DEFAULT_RTS_THRESHOLD;
        else {
                if (wrqu->rts.value < MIN_RTS_THRESHOLD ||
                    wrqu->rts.value > MAX_RTS_THRESHOLD) {
-                       up(&priv->sem);
+                       mutex_unlock(&priv->mutex);
                        return -EINVAL;
                }
                priv->rts_threshold = wrqu->rts.value;
        }
 
        ipw_send_rts_threshold(priv, priv->rts_threshold);
-       up(&priv->sem);
+       mutex_unlock(&priv->mutex);
        IPW_DEBUG_WX("SET RTS Threshold -> %d \n", priv->rts_threshold);
        return 0;
 }
@@ -7226,11 +9355,11 @@ static int ipw_wx_get_rts(struct net_device *dev,
                          union iwreq_data *wrqu, char *extra)
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
-       down(&priv->sem);
+       mutex_lock(&priv->mutex);
        wrqu->rts.value = priv->rts_threshold;
        wrqu->rts.fixed = 0;    /* no auto select */
        wrqu->rts.disabled = (wrqu->rts.value == DEFAULT_RTS_THRESHOLD);
-       up(&priv->sem);
+       mutex_unlock(&priv->mutex);
        IPW_DEBUG_WX("GET RTS Threshold -> %d \n", wrqu->rts.value);
        return 0;
 }
@@ -7240,49 +9369,33 @@ static int ipw_wx_set_txpow(struct net_device *dev,
                            union iwreq_data *wrqu, char *extra)
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
-       struct ipw_tx_power tx_power;
-       int i;
-       down(&priv->sem);
+       int err = 0;
+
+       mutex_lock(&priv->mutex);
        if (ipw_radio_kill_sw(priv, wrqu->power.disabled)) {
-               up(&priv->sem);
-               return -EINPROGRESS;
+               err = -EINPROGRESS;
+               goto out;
        }
 
+       if (!wrqu->power.fixed)
+               wrqu->power.value = IPW_TX_POWER_DEFAULT;
+
        if (wrqu->power.flags != IW_TXPOW_DBM) {
-               up(&priv->sem);
-               return -EINVAL;
+               err = -EINVAL;
+               goto out;
        }
 
-       if ((wrqu->power.value > 20) || (wrqu->power.value < -12)) {
-               up(&priv->sem);
-               return -EINVAL;
+       if ((wrqu->power.value > IPW_TX_POWER_MAX) ||
+           (wrqu->power.value < IPW_TX_POWER_MIN)) {
+               err = -EINVAL;
+               goto out;
        }
 
        priv->tx_power = wrqu->power.value;
-
-       memset(&tx_power, 0, sizeof(tx_power));
-
-       /* configure device for 'G' band */
-       tx_power.ieee_mode = IPW_G_MODE;
-       tx_power.num_channels = 11;
-       for (i = 0; i < 11; i++) {
-               tx_power.channels_tx_power[i].channel_number = i + 1;
-               tx_power.channels_tx_power[i].tx_power = priv->tx_power;
-       }
-       if (ipw_send_tx_power(priv, &tx_power))
-               goto error;
-
-       /* configure device to also handle 'B' band */
-       tx_power.ieee_mode = IPW_B_MODE;
-       if (ipw_send_tx_power(priv, &tx_power))
-               goto error;
-
-       up(&priv->sem);
-       return 0;
-
-      error:
-       up(&priv->sem);
-       return -EIO;
+       err = ipw_set_tx_power(priv);
+      out:
+       mutex_unlock(&priv->mutex);
+       return err;
 }
 
 static int ipw_wx_get_txpow(struct net_device *dev,
@@ -7290,15 +9403,15 @@ static int ipw_wx_get_txpow(struct net_device *dev,
                            union iwreq_data *wrqu, char *extra)
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
-       down(&priv->sem);
+       mutex_lock(&priv->mutex);
        wrqu->power.value = priv->tx_power;
        wrqu->power.fixed = 1;
        wrqu->power.flags = IW_TXPOW_DBM;
        wrqu->power.disabled = (priv->status & STATUS_RF_KILL_MASK) ? 1 : 0;
-       up(&priv->sem);
+       mutex_unlock(&priv->mutex);
 
        IPW_DEBUG_WX("GET TX Power -> %s %d \n",
-                    wrqu->power.disabled ? "ON" : "OFF", wrqu->power.value);
+                    wrqu->power.disabled ? "OFF" : "ON", wrqu->power.value);
 
        return 0;
 }
@@ -7308,19 +9421,21 @@ static int ipw_wx_set_frag(struct net_device *dev,
                           union iwreq_data *wrqu, char *extra)
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
-       down(&priv->sem);
-       if (wrqu->frag.disabled)
+       mutex_lock(&priv->mutex);
+       if (wrqu->frag.disabled || !wrqu->frag.fixed)
                priv->ieee->fts = DEFAULT_FTS;
        else {
                if (wrqu->frag.value < MIN_FRAG_THRESHOLD ||
-                   wrqu->frag.value > MAX_FRAG_THRESHOLD)
+                   wrqu->frag.value > MAX_FRAG_THRESHOLD) {
+                       mutex_unlock(&priv->mutex);
                        return -EINVAL;
+               }
 
                priv->ieee->fts = wrqu->frag.value & ~0x1;
        }
 
        ipw_send_frag_threshold(priv, wrqu->frag.value);
-       up(&priv->sem);
+       mutex_unlock(&priv->mutex);
        IPW_DEBUG_WX("SET Frag Threshold -> %d \n", wrqu->frag.value);
        return 0;
 }
@@ -7330,11 +9445,11 @@ static int ipw_wx_get_frag(struct net_device *dev,
                           union iwreq_data *wrqu, char *extra)
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
-       down(&priv->sem);
+       mutex_lock(&priv->mutex);
        wrqu->frag.value = priv->ieee->fts;
        wrqu->frag.fixed = 0;   /* no auto select */
        wrqu->frag.disabled = (wrqu->frag.value == DEFAULT_FTS);
-       up(&priv->sem);
+       mutex_unlock(&priv->mutex);
        IPW_DEBUG_WX("GET Frag Threshold -> %d \n", wrqu->frag.value);
 
        return 0;
@@ -7344,16 +9459,64 @@ static int ipw_wx_set_retry(struct net_device *dev,
                            struct iw_request_info *info,
                            union iwreq_data *wrqu, char *extra)
 {
-       IPW_DEBUG_WX("0x%p, 0x%p, 0x%p\n", dev, info, wrqu);
-       return -EOPNOTSUPP;
+       struct ipw_priv *priv = ieee80211_priv(dev);
+
+       if (wrqu->retry.flags & IW_RETRY_LIFETIME || wrqu->retry.disabled)
+               return -EINVAL;
+
+       if (!(wrqu->retry.flags & IW_RETRY_LIMIT))
+               return 0;
+
+       if (wrqu->retry.value < 0 || wrqu->retry.value >= 255)
+               return -EINVAL;
+
+       mutex_lock(&priv->mutex);
+       if (wrqu->retry.flags & IW_RETRY_SHORT)
+               priv->short_retry_limit = (u8) wrqu->retry.value;
+       else if (wrqu->retry.flags & IW_RETRY_LONG)
+               priv->long_retry_limit = (u8) wrqu->retry.value;
+       else {
+               priv->short_retry_limit = (u8) wrqu->retry.value;
+               priv->long_retry_limit = (u8) wrqu->retry.value;
+       }
+
+       ipw_send_retry_limit(priv, priv->short_retry_limit,
+                            priv->long_retry_limit);
+       mutex_unlock(&priv->mutex);
+       IPW_DEBUG_WX("SET retry limit -> short:%d long:%d\n",
+                    priv->short_retry_limit, priv->long_retry_limit);
+       return 0;
 }
 
 static int ipw_wx_get_retry(struct net_device *dev,
                            struct iw_request_info *info,
                            union iwreq_data *wrqu, char *extra)
 {
-       IPW_DEBUG_WX("0x%p, 0x%p, 0x%p\n", dev, info, wrqu);
-       return -EOPNOTSUPP;
+       struct ipw_priv *priv = ieee80211_priv(dev);
+
+       mutex_lock(&priv->mutex);
+       wrqu->retry.disabled = 0;
+
+       if ((wrqu->retry.flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME) {
+               mutex_unlock(&priv->mutex);
+               return -EINVAL;
+       }
+
+       if (wrqu->retry.flags & IW_RETRY_LONG) {
+               wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
+               wrqu->retry.value = priv->long_retry_limit;
+       } else if (wrqu->retry.flags & IW_RETRY_SHORT) {
+               wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_SHORT;
+               wrqu->retry.value = priv->short_retry_limit;
+       } else {
+               wrqu->retry.flags = IW_RETRY_LIMIT;
+               wrqu->retry.value = priv->short_retry_limit;
+       }
+       mutex_unlock(&priv->mutex);
+
+       IPW_DEBUG_WX("GET retry -> %d \n", wrqu->retry.value);
+
+       return 0;
 }
 
 static int ipw_wx_set_scan(struct net_device *dev,
@@ -7361,13 +9524,34 @@ static int ipw_wx_set_scan(struct net_device *dev,
                           union iwreq_data *wrqu, char *extra)
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
-       IPW_DEBUG_WX("Start scan\n");
-       down(&priv->sem);
-       if (ipw_request_scan(priv)) {
-               up(&priv->sem);
-               return -EIO;
+       struct iw_scan_req *req = (struct iw_scan_req *)extra;
+       struct delayed_work *work = NULL;
+
+       mutex_lock(&priv->mutex);
+
+       priv->user_requested_scan = 1;
+
+       if (wrqu->data.length == sizeof(struct iw_scan_req)) {
+               if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
+                       int len = min((int)req->essid_len,
+                                     (int)sizeof(priv->direct_scan_ssid));
+                       memcpy(priv->direct_scan_ssid, req->essid, len);
+                       priv->direct_scan_ssid_len = len;
+                       work = &priv->request_direct_scan;
+               } else if (req->scan_type == IW_SCAN_TYPE_PASSIVE) {
+                       work = &priv->request_passive_scan;
+               }
+       } else {
+               /* Normal active broadcast scan */
+               work = &priv->request_scan;
        }
-       up(&priv->sem);
+
+       mutex_unlock(&priv->mutex);
+
+       IPW_DEBUG_WX("Start scan\n");
+
+       queue_delayed_work(priv->workqueue, work, 0);
+
        return 0;
 }
 
@@ -7384,7 +9568,21 @@ static int ipw_wx_set_encode(struct net_device *dev,
                             union iwreq_data *wrqu, char *key)
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
-       return ieee80211_wx_set_encode(priv->ieee, info, wrqu, key);
+       int ret;
+       u32 cap = priv->capability;
+
+       mutex_lock(&priv->mutex);
+       ret = ieee80211_wx_set_encode(priv->ieee, info, wrqu, key);
+
+       /* In IBSS mode, we need to notify the firmware to update
+        * the beacon info after we changed the capability. */
+       if (cap != priv->capability &&
+           priv->ieee->iw_mode == IW_MODE_ADHOC &&
+           priv->status & STATUS_ASSOCIATED)
+               ipw_disassociate(priv);
+
+       mutex_unlock(&priv->mutex);
+       return ret;
 }
 
 static int ipw_wx_get_encode(struct net_device *dev,
@@ -7401,29 +9599,29 @@ static int ipw_wx_set_power(struct net_device *dev,
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
        int err;
-       down(&priv->sem);
+       mutex_lock(&priv->mutex);
        if (wrqu->power.disabled) {
                priv->power_mode = IPW_POWER_LEVEL(priv->power_mode);
                err = ipw_send_power_mode(priv, IPW_POWER_MODE_CAM);
                if (err) {
                        IPW_DEBUG_WX("failed setting power mode.\n");
-                       up(&priv->sem);
+                       mutex_unlock(&priv->mutex);
                        return err;
                }
                IPW_DEBUG_WX("SET Power Management Mode -> off\n");
-               up(&priv->sem);
+               mutex_unlock(&priv->mutex);
                return 0;
        }
 
        switch (wrqu->power.flags & IW_POWER_MODE) {
        case IW_POWER_ON:       /* If not specified */
        case IW_POWER_MODE:     /* If set all mask */
-       case IW_POWER_ALL_R:    /* If explicitely state all */
+       case IW_POWER_ALL_R:    /* If explicitly state all */
                break;
        default:                /* Otherwise we don't support it */
                IPW_DEBUG_WX("SET PM Mode: %X not supported.\n",
                             wrqu->power.flags);
-               up(&priv->sem);
+               mutex_unlock(&priv->mutex);
                return -EOPNOTSUPP;
        }
 
@@ -7433,15 +9631,16 @@ static int ipw_wx_set_power(struct net_device *dev,
                priv->power_mode = IPW_POWER_ENABLED | IPW_POWER_BATTERY;
        else
                priv->power_mode = IPW_POWER_ENABLED | priv->power_mode;
+
        err = ipw_send_power_mode(priv, IPW_POWER_LEVEL(priv->power_mode));
        if (err) {
                IPW_DEBUG_WX("failed setting power mode.\n");
-               up(&priv->sem);
+               mutex_unlock(&priv->mutex);
                return err;
        }
 
        IPW_DEBUG_WX("SET Power Management Mode -> 0x%02X\n", priv->power_mode);
-       up(&priv->sem);
+       mutex_unlock(&priv->mutex);
        return 0;
 }
 
@@ -7450,13 +9649,13 @@ static int ipw_wx_get_power(struct net_device *dev,
                            union iwreq_data *wrqu, char *extra)
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
-       down(&priv->sem);
+       mutex_lock(&priv->mutex);
        if (!(priv->power_mode & IPW_POWER_ENABLED))
                wrqu->power.disabled = 1;
        else
                wrqu->power.disabled = 0;
 
-       up(&priv->sem);
+       mutex_unlock(&priv->mutex);
        IPW_DEBUG_WX("GET Power Management Mode -> %02X\n", priv->power_mode);
 
        return 0;
@@ -7469,24 +9668,21 @@ static int ipw_wx_set_powermode(struct net_device *dev,
        struct ipw_priv *priv = ieee80211_priv(dev);
        int mode = *(int *)extra;
        int err;
-       down(&priv->sem);
-       if ((mode < 1) || (mode > IPW_POWER_LIMIT)) {
+
+       mutex_lock(&priv->mutex);
+       if ((mode < 1) || (mode > IPW_POWER_LIMIT))
                mode = IPW_POWER_AC;
-               priv->power_mode = mode;
-       } else {
-               priv->power_mode = IPW_POWER_ENABLED | mode;
-       }
 
-       if (priv->power_mode != mode) {
+       if (IPW_POWER_LEVEL(priv->power_mode) != mode) {
                err = ipw_send_power_mode(priv, mode);
-
                if (err) {
                        IPW_DEBUG_WX("failed setting power mode.\n");
-                       up(&priv->sem);
+                       mutex_unlock(&priv->mutex);
                        return err;
                }
+               priv->power_mode = IPW_POWER_ENABLED | mode;
        }
-       up(&priv->sem);
+       mutex_unlock(&priv->mutex);
        return 0;
 }
 
@@ -7535,7 +9731,7 @@ static int ipw_wx_set_wireless_mode(struct net_device *dev,
                IPW_WARNING("Attempt to set invalid wireless mode: %d\n", mode);
                return -EINVAL;
        }
-       down(&priv->sem);
+       mutex_lock(&priv->mutex);
        if (priv->adapter == IPW_2915ABG) {
                priv->ieee->abg_true = 1;
                if (mode & IEEE_A) {
@@ -7547,7 +9743,7 @@ static int ipw_wx_set_wireless_mode(struct net_device *dev,
                if (mode & IEEE_A) {
                        IPW_WARNING("Attempt to set 2200BG into "
                                    "802.11a mode\n");
-                       up(&priv->sem);
+                       mutex_unlock(&priv->mutex);
                        return -EINVAL;
                }
 
@@ -7584,7 +9780,7 @@ static int ipw_wx_set_wireless_mode(struct net_device *dev,
        IPW_DEBUG_WX("PRIV SET MODE: %c%c%c\n",
                     mode & IEEE_A ? 'a' : '.',
                     mode & IEEE_B ? 'b' : '.', mode & IEEE_G ? 'g' : '.');
-       up(&priv->sem);
+       mutex_unlock(&priv->mutex);
        return 0;
 }
 
@@ -7593,7 +9789,7 @@ static int ipw_wx_get_wireless_mode(struct net_device *dev,
                                    union iwreq_data *wrqu, char *extra)
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
-       down(&priv->sem);
+       mutex_lock(&priv->mutex);
        switch (priv->ieee->mode) {
        case IEEE_A:
                strncpy(extra, "802.11a (1)", MAX_WX_STRING);
@@ -7624,7 +9820,7 @@ static int ipw_wx_get_wireless_mode(struct net_device *dev,
        IPW_DEBUG_WX("PRIV GET MODE: %s\n", extra);
 
        wrqu->data.length = strlen(extra) + 1;
-       up(&priv->sem);
+       mutex_unlock(&priv->mutex);
 
        return 0;
 }
@@ -7635,7 +9831,7 @@ static int ipw_wx_set_preamble(struct net_device *dev,
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
        int mode = *(int *)extra;
-       down(&priv->sem);
+       mutex_lock(&priv->mutex);
        /* Switching from SHORT -> LONG requires a disassociation */
        if (mode == 1) {
                if (!(priv->config & CFG_PREAMBLE_LONG)) {
@@ -7654,11 +9850,11 @@ static int ipw_wx_set_preamble(struct net_device *dev,
                priv->config &= ~CFG_PREAMBLE_LONG;
                goto done;
        }
-       up(&priv->sem);
+       mutex_unlock(&priv->mutex);
        return -EINVAL;
 
       done:
-       up(&priv->sem);
+       mutex_unlock(&priv->mutex);
        return 0;
 }
 
@@ -7667,16 +9863,16 @@ static int ipw_wx_get_preamble(struct net_device *dev,
                               union iwreq_data *wrqu, char *extra)
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
-       down(&priv->sem);
+       mutex_lock(&priv->mutex);
        if (priv->config & CFG_PREAMBLE_LONG)
                snprintf(wrqu->name, IFNAMSIZ, "long (1)");
        else
                snprintf(wrqu->name, IFNAMSIZ, "auto (0)");
-       up(&priv->sem);
+       mutex_unlock(&priv->mutex);
        return 0;
 }
 
-#ifdef CONFIG_IPW_MONITOR
+#ifdef CONFIG_IPW2200_MONITOR
 static int ipw_wx_set_monitor(struct net_device *dev,
                              struct iw_request_info *info,
                              union iwreq_data *wrqu, char *extra)
@@ -7684,27 +9880,33 @@ static int ipw_wx_set_monitor(struct net_device *dev,
        struct ipw_priv *priv = ieee80211_priv(dev);
        int *parms = (int *)extra;
        int enable = (parms[0] > 0);
-       down(&priv->sem);
+       mutex_lock(&priv->mutex);
        IPW_DEBUG_WX("SET MONITOR: %d %d\n", enable, parms[1]);
        if (enable) {
                if (priv->ieee->iw_mode != IW_MODE_MONITOR) {
+#ifdef CONFIG_IPW2200_RADIOTAP
+                       priv->net_dev->type = ARPHRD_IEEE80211_RADIOTAP;
+#else
                        priv->net_dev->type = ARPHRD_IEEE80211;
+#endif
                        queue_work(priv->workqueue, &priv->adapter_restart);
                }
 
                ipw_set_channel(priv, parms[1]);
        } else {
                if (priv->ieee->iw_mode != IW_MODE_MONITOR) {
-                       up(&priv->sem);
+                       mutex_unlock(&priv->mutex);
                        return 0;
                }
                priv->net_dev->type = ARPHRD_ETHER;
                queue_work(priv->workqueue, &priv->adapter_restart);
        }
-       up(&priv->sem);
+       mutex_unlock(&priv->mutex);
        return 0;
 }
 
+#endif                         /* CONFIG_IPW2200_MONITOR */
+
 static int ipw_wx_reset(struct net_device *dev,
                        struct iw_request_info *info,
                        union iwreq_data *wrqu, char *extra)
@@ -7714,7 +9916,49 @@ static int ipw_wx_reset(struct net_device *dev,
        queue_work(priv->workqueue, &priv->adapter_restart);
        return 0;
 }
-#endif                         // CONFIG_IPW_MONITOR
+
+static int ipw_wx_sw_reset(struct net_device *dev,
+                          struct iw_request_info *info,
+                          union iwreq_data *wrqu, char *extra)
+{
+       struct ipw_priv *priv = ieee80211_priv(dev);
+       union iwreq_data wrqu_sec = {
+               .encoding = {
+                            .flags = IW_ENCODE_DISABLED,
+                            },
+       };
+       int ret;
+
+       IPW_DEBUG_WX("SW_RESET\n");
+
+       mutex_lock(&priv->mutex);
+
+       ret = ipw_sw_reset(priv, 2);
+       if (!ret) {
+               free_firmware();
+               ipw_adapter_restart(priv);
+       }
+
+       /* The SW reset bit might have been toggled on by the 'disable'
+        * module parameter, so take appropriate action */
+       ipw_radio_kill_sw(priv, priv->status & STATUS_RF_KILL_SW);
+
+       mutex_unlock(&priv->mutex);
+       ieee80211_wx_set_encode(priv->ieee, info, &wrqu_sec, NULL);
+       mutex_lock(&priv->mutex);
+
+       if (!(priv->status & STATUS_RF_KILL_MASK)) {
+               /* Configuration likely changed -- force [re]association */
+               IPW_DEBUG_ASSOC("[re]association triggered due to sw "
+                               "reset.\n");
+               if (!ipw_disassociate(priv))
+                       ipw_associate(priv);
+       }
+
+       mutex_unlock(&priv->mutex);
+
+       return 0;
+}
 
 /* Rebase the WE IOCTLs to zero for the handler array */
 #define IW_IOCTL(x) [(x)-SIOCSIWCOMMIT]
@@ -7724,6 +9968,8 @@ static iw_handler ipw_wx_handlers[] = {
        IW_IOCTL(SIOCGIWFREQ) = ipw_wx_get_freq,
        IW_IOCTL(SIOCSIWMODE) = ipw_wx_set_mode,
        IW_IOCTL(SIOCGIWMODE) = ipw_wx_get_mode,
+       IW_IOCTL(SIOCSIWSENS) = ipw_wx_set_sens,
+       IW_IOCTL(SIOCGIWSENS) = ipw_wx_get_sens,
        IW_IOCTL(SIOCGIWRANGE) = ipw_wx_get_range,
        IW_IOCTL(SIOCSIWAP) = ipw_wx_set_wap,
        IW_IOCTL(SIOCGIWAP) = ipw_wx_get_wap,
@@ -7751,16 +9997,28 @@ static iw_handler ipw_wx_handlers[] = {
        IW_IOCTL(SIOCGIWSPY) = iw_handler_get_spy,
        IW_IOCTL(SIOCSIWTHRSPY) = iw_handler_set_thrspy,
        IW_IOCTL(SIOCGIWTHRSPY) = iw_handler_get_thrspy,
+       IW_IOCTL(SIOCSIWGENIE) = ipw_wx_set_genie,
+       IW_IOCTL(SIOCGIWGENIE) = ipw_wx_get_genie,
+       IW_IOCTL(SIOCSIWMLME) = ipw_wx_set_mlme,
+       IW_IOCTL(SIOCSIWAUTH) = ipw_wx_set_auth,
+       IW_IOCTL(SIOCGIWAUTH) = ipw_wx_get_auth,
+       IW_IOCTL(SIOCSIWENCODEEXT) = ipw_wx_set_encodeext,
+       IW_IOCTL(SIOCGIWENCODEEXT) = ipw_wx_get_encodeext,
 };
 
-#define IPW_PRIV_SET_POWER     SIOCIWFIRSTPRIV
-#define IPW_PRIV_GET_POWER     SIOCIWFIRSTPRIV+1
-#define IPW_PRIV_SET_MODE      SIOCIWFIRSTPRIV+2
-#define IPW_PRIV_GET_MODE      SIOCIWFIRSTPRIV+3
-#define IPW_PRIV_SET_PREAMBLE  SIOCIWFIRSTPRIV+4
-#define IPW_PRIV_GET_PREAMBLE  SIOCIWFIRSTPRIV+5
-#define IPW_PRIV_SET_MONITOR   SIOCIWFIRSTPRIV+6
-#define IPW_PRIV_RESET         SIOCIWFIRSTPRIV+7
+enum {
+       IPW_PRIV_SET_POWER = SIOCIWFIRSTPRIV,
+       IPW_PRIV_GET_POWER,
+       IPW_PRIV_SET_MODE,
+       IPW_PRIV_GET_MODE,
+       IPW_PRIV_SET_PREAMBLE,
+       IPW_PRIV_GET_PREAMBLE,
+       IPW_PRIV_RESET,
+       IPW_PRIV_SW_RESET,
+#ifdef CONFIG_IPW2200_MONITOR
+       IPW_PRIV_SET_MONITOR,
+#endif
+};
 
 static struct iw_priv_args ipw_priv_args[] = {
        {
@@ -7787,14 +10045,17 @@ static struct iw_priv_args ipw_priv_args[] = {
         .cmd = IPW_PRIV_GET_PREAMBLE,
         .get_args = IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | IFNAMSIZ,
         .name = "get_preamble"},
-#ifdef CONFIG_IPW_MONITOR
-       {
-        IPW_PRIV_SET_MONITOR,
-        IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 2, 0, "monitor"},
        {
         IPW_PRIV_RESET,
         IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 0, 0, "reset"},
-#endif                         /* CONFIG_IPW_MONITOR */
+       {
+        IPW_PRIV_SW_RESET,
+        IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 0, 0, "sw_reset"},
+#ifdef CONFIG_IPW2200_MONITOR
+       {
+        IPW_PRIV_SET_MONITOR,
+        IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 2, 0, "monitor"},
+#endif                         /* CONFIG_IPW2200_MONITOR */
 };
 
 static iw_handler ipw_priv_handler[] = {
@@ -7804,9 +10065,10 @@ static iw_handler ipw_priv_handler[] = {
        ipw_wx_get_wireless_mode,
        ipw_wx_set_preamble,
        ipw_wx_get_preamble,
-#ifdef CONFIG_IPW_MONITOR
-       ipw_wx_set_monitor,
        ipw_wx_reset,
+       ipw_wx_sw_reset,
+#ifdef CONFIG_IPW2200_MONITOR
+       ipw_wx_set_monitor,
 #endif
 };
 
@@ -7817,10 +10079,9 @@ static struct iw_handler_def ipw_wx_handler_def = {
        .num_private_args = ARRAY_SIZE(ipw_priv_args),
        .private = ipw_priv_handler,
        .private_args = ipw_priv_args,
+       .get_wireless_stats = ipw_get_wireless_stats,
 };
 
-static struct iw_public_data ipw_wx_data;
-
 /*
  * Get wireless statistics.
  * Called by /proc/net/wireless
@@ -7834,7 +10095,7 @@ static struct iw_statistics *ipw_get_wireless_stats(struct net_device *dev)
        wstats = &priv->wstats;
 
        /* if hw is disabled, then ipw_get_ordinal() can't be called.
-        * ipw2100_wx_wireless_stats seems to be called before fw is
+        * netdev->get_wireless_stats seems to be called before fw is
         * initialized.  STATUS_ASSOCIATED will only be set if the hw is up
         * and associated; if not associcated, the values are all meaningless
         * anyway, so set them all to NULL and INVALID */
@@ -7851,10 +10112,10 @@ static struct iw_statistics *ipw_get_wireless_stats(struct net_device *dev)
        }
 
        wstats->qual.qual = priv->quality;
-       wstats->qual.level = average_value(&priv->average_rssi);
-       wstats->qual.noise = average_value(&priv->average_noise);
+       wstats->qual.level = priv->exp_avg_rssi;
+       wstats->qual.noise = priv->exp_avg_noise;
        wstats->qual.updated = IW_QUAL_QUAL_UPDATED | IW_QUAL_LEVEL_UPDATED |
-           IW_QUAL_NOISE_UPDATED;
+           IW_QUAL_NOISE_UPDATED | IW_QUAL_DBM;
 
        wstats->miss.beacon = average_value(&priv->average_missed_beacons);
        wstats->discard.retries = priv->last_tx_failures;
@@ -7869,10 +10130,10 @@ static struct iw_statistics *ipw_get_wireless_stats(struct net_device *dev)
 
 /* net device stuff */
 
-static inline void init_sys_config(struct ipw_sys_config *sys_config)
+static  void init_sys_config(struct ipw_sys_config *sys_config)
 {
        memset(sys_config, 0, sizeof(struct ipw_sys_config));
-       sys_config->bt_coexistence = 1; /* We may need to look into prvStaBtConfig */
+       sys_config->bt_coexistence = 0;
        sys_config->answer_broadcast_ssid_probe = 0;
        sys_config->accept_all_data_frames = 0;
        sys_config->accept_non_directed_frames = 1;
@@ -7880,24 +10141,21 @@ static inline void init_sys_config(struct ipw_sys_config *sys_config)
        sys_config->disable_unicast_decryption = 1;
        sys_config->exclude_multicast_unencrypted = 0;
        sys_config->disable_multicast_decryption = 1;
-       sys_config->antenna_diversity = CFG_SYS_ANTENNA_BOTH;
+       if (antenna < CFG_SYS_ANTENNA_BOTH || antenna > CFG_SYS_ANTENNA_B)
+               antenna = CFG_SYS_ANTENNA_BOTH;
+       sys_config->antenna_diversity = antenna;
        sys_config->pass_crc_to_host = 0;       /* TODO: See if 1 gives us FCS */
        sys_config->dot11g_auto_detection = 0;
        sys_config->enable_cts_to_self = 0;
        sys_config->bt_coexist_collision_thr = 0;
-       sys_config->pass_noise_stats_to_host = 1;       //1 -- fix for 256
+       sys_config->pass_noise_stats_to_host = 1;       /* 1 -- fix for 256 */
+       sys_config->silence_threshold = 0x1e;
 }
 
 static int ipw_net_open(struct net_device *dev)
 {
-       struct ipw_priv *priv = ieee80211_priv(dev);
        IPW_DEBUG_INFO("dev->open\n");
-       /* we should be verifying the device is ready to be opened */
-       down(&priv->sem);
-       if (!(priv->status & STATUS_RF_KILL_MASK) &&
-           (priv->status & STATUS_ASSOCIATED))
-               netif_start_queue(dev);
-       up(&priv->sem);
+       netif_start_queue(dev);
        return 0;
 }
 
@@ -7915,30 +10173,37 @@ modify to send one tfd per fragment instead of using chunking.  otherwise
 we need to heavily modify the ieee80211_skb_to_txb.
 */
 
-static inline void ipw_tx_skb(struct ipw_priv *priv, struct ieee80211_txb *txb)
+static int ipw_tx_skb(struct ipw_priv *priv, struct ieee80211_txb *txb,
+                            int pri)
 {
-       struct ieee80211_hdr_3addr *hdr = (struct ieee80211_hdr_3addr *)
+       struct ieee80211_hdr_3addrqos *hdr = (struct ieee80211_hdr_3addrqos *)
            txb->fragments[0]->data;
        int i = 0;
        struct tfd_frame *tfd;
+#ifdef CONFIG_IPW2200_QOS
+       int tx_id = ipw_get_tx_queue_number(priv, pri);
+       struct clx2_tx_queue *txq = &priv->txq[tx_id];
+#else
        struct clx2_tx_queue *txq = &priv->txq[0];
+#endif
        struct clx2_queue *q = &txq->q;
        u8 id, hdr_len, unicast;
        u16 remaining_bytes;
        int fc;
 
+       hdr_len = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
        switch (priv->ieee->iw_mode) {
        case IW_MODE_ADHOC:
-               hdr_len = IEEE80211_3ADDR_LEN;
-               unicast = !is_broadcast_ether_addr(hdr->addr1) &&
-                   !is_multicast_ether_addr(hdr->addr1);
+               unicast = !is_multicast_ether_addr(hdr->addr1);
                id = ipw_find_station(priv, hdr->addr1);
                if (id == IPW_INVALID_STATION) {
                        id = ipw_add_station(priv, hdr->addr1);
                        if (id == IPW_INVALID_STATION) {
                                IPW_WARNING("Attempt to send data to "
                                            "invalid cell: " MAC_FMT "\n",
-                                           MAC_ARG(hdr->addr1));
+                                           hdr->addr1[0], hdr->addr1[1],
+                                           hdr->addr1[2], hdr->addr1[3],
+                                           hdr->addr1[4], hdr->addr1[5]);
                                goto drop;
                        }
                }
@@ -7946,9 +10211,7 @@ static inline void ipw_tx_skb(struct ipw_priv *priv, struct ieee80211_txb *txb)
 
        case IW_MODE_INFRA:
        default:
-               unicast = !is_broadcast_ether_addr(hdr->addr3) &&
-                   !is_multicast_ether_addr(hdr->addr3);
-               hdr_len = IEEE80211_3ADDR_LEN;
+               unicast = !is_multicast_ether_addr(hdr->addr3);
                id = 0;
                break;
        }
@@ -7964,15 +10227,11 @@ static inline void ipw_tx_skb(struct ipw_priv *priv, struct ieee80211_txb *txb)
        tfd->u.data.cmd_id = DINO_CMD_TX;
        tfd->u.data.len = cpu_to_le16(txb->payload_size);
        remaining_bytes = txb->payload_size;
-       if (unlikely(!unicast))
-               tfd->u.data.tx_flags = DCT_FLAG_NO_WEP;
-       else
-               tfd->u.data.tx_flags = DCT_FLAG_NO_WEP | DCT_FLAG_ACK_REQD;
 
        if (priv->assoc_request.ieee_mode == IPW_B_MODE)
-               tfd->u.data.tx_flags_ext = DCT_FLAG_EXT_MODE_CCK;
+               tfd->u.data.tx_flags_ext |= DCT_FLAG_EXT_MODE_CCK;
        else
-               tfd->u.data.tx_flags_ext = DCT_FLAG_EXT_MODE_OFDM;
+               tfd->u.data.tx_flags_ext |= DCT_FLAG_EXT_MODE_OFDM;
 
        if (priv->assoc_request.preamble_length == DCT_FLAG_SHORT_PREAMBLE)
                tfd->u.data.tx_flags |= DCT_FLAG_SHORT_PREAMBLE;
@@ -7982,6 +10241,59 @@ static inline void ipw_tx_skb(struct ipw_priv *priv, struct ieee80211_txb *txb)
 
        memcpy(&tfd->u.data.tfd.tfd_24.mchdr, hdr, hdr_len);
 
+       if (likely(unicast))
+               tfd->u.data.tx_flags |= DCT_FLAG_ACK_REQD;
+
+       if (txb->encrypted && !priv->ieee->host_encrypt) {
+               switch (priv->ieee->sec.level) {
+               case SEC_LEVEL_3:
+                       tfd->u.data.tfd.tfd_24.mchdr.frame_ctl |=
+                           cpu_to_le16(IEEE80211_FCTL_PROTECTED);
+                       /* XXX: ACK flag must be set for CCMP even if it
+                        * is a multicast/broadcast packet, because CCMP
+                        * group communication encrypted by GTK is
+                        * actually done by the AP. */
+                       if (!unicast)
+                               tfd->u.data.tx_flags |= DCT_FLAG_ACK_REQD;
+
+                       tfd->u.data.tx_flags &= ~DCT_FLAG_NO_WEP;
+                       tfd->u.data.tx_flags_ext |= DCT_FLAG_EXT_SECURITY_CCM;
+                       tfd->u.data.key_index = 0;
+                       tfd->u.data.key_index |= DCT_WEP_INDEX_USE_IMMEDIATE;
+                       break;
+               case SEC_LEVEL_2:
+                       tfd->u.data.tfd.tfd_24.mchdr.frame_ctl |=
+                           cpu_to_le16(IEEE80211_FCTL_PROTECTED);
+                       tfd->u.data.tx_flags &= ~DCT_FLAG_NO_WEP;
+                       tfd->u.data.tx_flags_ext |= DCT_FLAG_EXT_SECURITY_TKIP;
+                       tfd->u.data.key_index = DCT_WEP_INDEX_USE_IMMEDIATE;
+                       break;
+               case SEC_LEVEL_1:
+                       tfd->u.data.tfd.tfd_24.mchdr.frame_ctl |=
+                           cpu_to_le16(IEEE80211_FCTL_PROTECTED);
+                       tfd->u.data.key_index = priv->ieee->tx_keyidx;
+                       if (priv->ieee->sec.key_sizes[priv->ieee->tx_keyidx] <=
+                           40)
+                               tfd->u.data.key_index |= DCT_WEP_KEY_64Bit;
+                       else
+                               tfd->u.data.key_index |= DCT_WEP_KEY_128Bit;
+                       break;
+               case SEC_LEVEL_0:
+                       break;
+               default:
+                       printk(KERN_ERR "Unknow security level %d\n",
+                              priv->ieee->sec.level);
+                       break;
+               }
+       } else
+               /* No hardware encryption */
+               tfd->u.data.tx_flags |= DCT_FLAG_NO_WEP;
+
+#ifdef CONFIG_IPW2200_QOS
+       if (fc & IEEE80211_STYPE_QOS_DATA)
+               ipw_qos_set_tx_queue_command(priv, pri, &(tfd->u.data));
+#endif                         /* CONFIG_IPW2200_QOS */
+
        /* payload */
        tfd->u.data.num_chunks = cpu_to_le32(min((u8) (NUM_TFD_CHUNKS - 2),
                                                 txb->nr_frags));
@@ -8022,6 +10334,7 @@ static inline void ipw_tx_skb(struct ipw_priv *priv, struct ieee80211_txb *txb)
                        tfd->u.data.chunk_len[i] = cpu_to_le16(remaining_bytes);
                        for (j = i; j < txb->nr_frags; j++) {
                                int size = txb->fragments[j]->len - hdr_len;
+
                                printk(KERN_INFO "Adding frag %d %d...\n",
                                       j, size);
                                memcpy(skb_put(skb, size),
@@ -8032,12 +10345,10 @@ static inline void ipw_tx_skb(struct ipw_priv *priv, struct ieee80211_txb *txb)
                        tfd->u.data.chunk_ptr[i] =
                            cpu_to_le32(pci_map_single
                                        (priv->pci_dev, skb->data,
-                                        tfd->u.data.chunk_len[i],
+                                        remaining_bytes,
                                         PCI_DMA_TODEVICE));
 
-                       tfd->u.data.num_chunks =
-                           cpu_to_le32(le32_to_cpu(tfd->u.data.num_chunks) +
-                                       1);
+                       le32_add_cpu(&tfd->u.data.num_chunks, 1);
                }
        }
 
@@ -8045,43 +10356,136 @@ static inline void ipw_tx_skb(struct ipw_priv *priv, struct ieee80211_txb *txb)
        q->first_empty = ipw_queue_inc_wrap(q->first_empty, q->n_bd);
        ipw_write32(priv, q->reg_w, q->first_empty);
 
-       if (ipw_queue_space(q) < q->high_mark)
+       if (ipw_tx_queue_space(q) < q->high_mark)
                netif_stop_queue(priv->net_dev);
 
-       return;
+       return NETDEV_TX_OK;
 
       drop:
        IPW_DEBUG_DROP("Silently dropping Tx packet.\n");
        ieee80211_txb_free(txb);
+       return NETDEV_TX_OK;
+}
+
+static int ipw_net_is_queue_full(struct net_device *dev, int pri)
+{
+       struct ipw_priv *priv = ieee80211_priv(dev);
+#ifdef CONFIG_IPW2200_QOS
+       int tx_id = ipw_get_tx_queue_number(priv, pri);
+       struct clx2_tx_queue *txq = &priv->txq[tx_id];
+#else
+       struct clx2_tx_queue *txq = &priv->txq[0];
+#endif                         /* CONFIG_IPW2200_QOS */
+
+       if (ipw_tx_queue_space(&txq->q) < txq->q.high_mark)
+               return 1;
+
+       return 0;
+}
+
+#ifdef CONFIG_IPW2200_PROMISCUOUS
+static void ipw_handle_promiscuous_tx(struct ipw_priv *priv,
+                                     struct ieee80211_txb *txb)
+{
+       struct ieee80211_rx_stats dummystats;
+       struct ieee80211_hdr *hdr;
+       u8 n;
+       u16 filter = priv->prom_priv->filter;
+       int hdr_only = 0;
+
+       if (filter & IPW_PROM_NO_TX)
+               return;
+
+       memset(&dummystats, 0, sizeof(dummystats));
+
+       /* Filtering of fragment chains is done agains the first fragment */
+       hdr = (void *)txb->fragments[0]->data;
+       if (ieee80211_is_management(le16_to_cpu(hdr->frame_ctl))) {
+               if (filter & IPW_PROM_NO_MGMT)
+                       return;
+               if (filter & IPW_PROM_MGMT_HEADER_ONLY)
+                       hdr_only = 1;
+       } else if (ieee80211_is_control(le16_to_cpu(hdr->frame_ctl))) {
+               if (filter & IPW_PROM_NO_CTL)
+                       return;
+               if (filter & IPW_PROM_CTL_HEADER_ONLY)
+                       hdr_only = 1;
+       } else if (ieee80211_is_data(le16_to_cpu(hdr->frame_ctl))) {
+               if (filter & IPW_PROM_NO_DATA)
+                       return;
+               if (filter & IPW_PROM_DATA_HEADER_ONLY)
+                       hdr_only = 1;
+       }
+
+       for(n=0; n<txb->nr_frags; ++n) {
+               struct sk_buff *src = txb->fragments[n];
+               struct sk_buff *dst;
+               struct ieee80211_radiotap_header *rt_hdr;
+               int len;
+
+               if (hdr_only) {
+                       hdr = (void *)src->data;
+                       len = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
+               } else
+                       len = src->len;
+
+               dst = alloc_skb(
+                       len + IEEE80211_RADIOTAP_HDRLEN, GFP_ATOMIC);
+               if (!dst) continue;
+
+               rt_hdr = (void *)skb_put(dst, sizeof(*rt_hdr));
+
+               rt_hdr->it_version = PKTHDR_RADIOTAP_VERSION;
+               rt_hdr->it_pad = 0;
+               rt_hdr->it_present = 0; /* after all, it's just an idea */
+               rt_hdr->it_present |=  cpu_to_le32(1 << IEEE80211_RADIOTAP_CHANNEL);
+
+               *(__le16*)skb_put(dst, sizeof(u16)) = cpu_to_le16(
+                       ieee80211chan2mhz(priv->channel));
+               if (priv->channel > 14)         /* 802.11a */
+                       *(__le16*)skb_put(dst, sizeof(u16)) =
+                               cpu_to_le16(IEEE80211_CHAN_OFDM |
+                                            IEEE80211_CHAN_5GHZ);
+               else if (priv->ieee->mode == IEEE_B) /* 802.11b */
+                       *(__le16*)skb_put(dst, sizeof(u16)) =
+                               cpu_to_le16(IEEE80211_CHAN_CCK |
+                                            IEEE80211_CHAN_2GHZ);
+               else            /* 802.11g */
+                       *(__le16*)skb_put(dst, sizeof(u16)) =
+                               cpu_to_le16(IEEE80211_CHAN_OFDM |
+                                IEEE80211_CHAN_2GHZ);
+
+               rt_hdr->it_len = cpu_to_le16(dst->len);
+
+               skb_copy_from_linear_data(src, skb_put(dst, len), len);
+
+               if (!ieee80211_rx(priv->prom_priv->ieee, dst, &dummystats))
+                       dev_kfree_skb_any(dst);
+       }
 }
+#endif
 
 static int ipw_net_hard_start_xmit(struct ieee80211_txb *txb,
                                   struct net_device *dev, int pri)
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
        unsigned long flags;
+       int ret;
 
        IPW_DEBUG_TX("dev->xmit(%d bytes)\n", txb->payload_size);
        spin_lock_irqsave(&priv->lock, flags);
 
-       if (!(priv->status & STATUS_ASSOCIATED)) {
-               IPW_DEBUG_INFO("Tx attempt while not associated.\n");
-               priv->ieee->stats.tx_carrier_errors++;
-               netif_stop_queue(dev);
-               goto fail_unlock;
-       }
+#ifdef CONFIG_IPW2200_PROMISCUOUS
+       if (rtap_iface && netif_running(priv->prom_net_dev))
+               ipw_handle_promiscuous_tx(priv, txb);
+#endif
 
-       ipw_tx_skb(priv, txb);
+       ret = ipw_tx_skb(priv, txb, pri);
+       if (ret == NETDEV_TX_OK)
+               __ipw_led_activity_on(priv);
        spin_unlock_irqrestore(&priv->lock, flags);
-       ipw_led_activity_on(priv);
-
-//      up(&priv->sem);
-       return 0;
 
-      fail_unlock:
-       spin_unlock_irqrestore(&priv->lock, flags);
-//      up(&priv->sem);
-       return 1;
+       return ret;
 }
 
 static struct net_device_stats *ipw_net_get_stats(struct net_device *dev)
@@ -8102,15 +10506,17 @@ static int ipw_net_set_mac_address(struct net_device *dev, void *p)
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
        struct sockaddr *addr = p;
+       DECLARE_MAC_BUF(mac);
+
        if (!is_valid_ether_addr(addr->sa_data))
                return -EADDRNOTAVAIL;
-       down(&priv->sem);
+       mutex_lock(&priv->mutex);
        priv->config |= CFG_CUSTOM_MAC;
        memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
-       printk(KERN_INFO "%s: Setting MAC to " MAC_FMT "\n",
-              priv->net_dev->name, MAC_ARG(priv->mac_addr));
+       printk(KERN_INFO "%s: Setting MAC to %s\n",
+              priv->net_dev->name, print_mac(mac, priv->mac_addr));
        queue_work(priv->workqueue, &priv->adapter_restart);
-       up(&priv->sem);
+       mutex_unlock(&priv->mutex);
        return 0;
 }
 
@@ -8133,7 +10539,7 @@ static void ipw_ethtool_get_drvinfo(struct net_device *dev,
        snprintf(info->fw_version, sizeof(info->fw_version), "%s (%s)",
                 vers, date);
        strcpy(info->bus_info, pci_name(p->pci_dev));
-       info->eedump_len = CX2_EEPROM_IMAGE_SIZE;
+       info->eedump_len = IPW_EEPROM_IMAGE_SIZE;
 }
 
 static u32 ipw_ethtool_get_link(struct net_device *dev)
@@ -8144,7 +10550,7 @@ static u32 ipw_ethtool_get_link(struct net_device *dev)
 
 static int ipw_ethtool_get_eeprom_len(struct net_device *dev)
 {
-       return CX2_EEPROM_IMAGE_SIZE;
+       return IPW_EEPROM_IMAGE_SIZE;
 }
 
 static int ipw_ethtool_get_eeprom(struct net_device *dev,
@@ -8152,11 +10558,11 @@ static int ipw_ethtool_get_eeprom(struct net_device *dev,
 {
        struct ipw_priv *p = ieee80211_priv(dev);
 
-       if (eeprom->offset + eeprom->len > CX2_EEPROM_IMAGE_SIZE)
+       if (eeprom->offset + eeprom->len > IPW_EEPROM_IMAGE_SIZE)
                return -EINVAL;
-       down(&p->sem);
-       memcpy(bytes, &((u8 *) p->eeprom)[eeprom->offset], eeprom->len);
-       up(&p->sem);
+       mutex_lock(&p->mutex);
+       memcpy(bytes, &p->eeprom[eeprom->offset], eeprom->len);
+       mutex_unlock(&p->mutex);
        return 0;
 }
 
@@ -8166,18 +10572,17 @@ static int ipw_ethtool_set_eeprom(struct net_device *dev,
        struct ipw_priv *p = ieee80211_priv(dev);
        int i;
 
-       if (eeprom->offset + eeprom->len > CX2_EEPROM_IMAGE_SIZE)
+       if (eeprom->offset + eeprom->len > IPW_EEPROM_IMAGE_SIZE)
                return -EINVAL;
-       down(&p->sem);
-       memcpy(&((u8 *) p->eeprom)[eeprom->offset], bytes, eeprom->len);
-       for (i = IPW_EEPROM_DATA;
-            i < IPW_EEPROM_DATA + CX2_EEPROM_IMAGE_SIZE; i++)
-               ipw_write8(p, i, p->eeprom[i]);
-       up(&p->sem);
+       mutex_lock(&p->mutex);
+       memcpy(&p->eeprom[eeprom->offset], bytes, eeprom->len);
+       for (i = 0; i < IPW_EEPROM_IMAGE_SIZE; i++)
+               ipw_write8(p, i + IPW_EEPROM_DATA, p->eeprom[i]);
+       mutex_unlock(&p->mutex);
        return 0;
 }
 
-static struct ethtool_ops ipw_ethtool_ops = {
+static const struct ethtool_ops ipw_ethtool_ops = {
        .get_link = ipw_ethtool_get_link,
        .get_drvinfo = ipw_ethtool_get_drvinfo,
        .get_eeprom_len = ipw_ethtool_get_eeprom_len,
@@ -8185,7 +10590,7 @@ static struct ethtool_ops ipw_ethtool_ops = {
        .set_eeprom = ipw_ethtool_set_eeprom,
 };
 
-static irqreturn_t ipw_isr(int irq, void *data, struct pt_regs *regs)
+static irqreturn_t ipw_isr(int irq, void *data)
 {
        struct ipw_priv *priv = data;
        u32 inta, inta_mask;
@@ -8193,17 +10598,15 @@ static irqreturn_t ipw_isr(int irq, void *data, struct pt_regs *regs)
        if (!priv)
                return IRQ_NONE;
 
-       spin_lock(&priv->lock);
+       spin_lock(&priv->irq_lock);
 
        if (!(priv->status & STATUS_INT_ENABLED)) {
-               /* Shared IRQ */
-//              ipw_write32(priv, CX2_INTA_RW, CX2_INTA_MASK_ALL);
-//              return IRQ_HANDLED;
+               /* IRQ is disabled */
                goto none;
        }
 
-       inta = ipw_read32(priv, CX2_INTA_RW);
-       inta_mask = ipw_read32(priv, CX2_INTA_MASK_R);
+       inta = ipw_read32(priv, IPW_INTA_RW);
+       inta_mask = ipw_read32(priv, IPW_INTA_MASK_R);
 
        if (inta == 0xFFFFFFFF) {
                /* Hardware disappeared */
@@ -8211,28 +10614,28 @@ static irqreturn_t ipw_isr(int irq, void *data, struct pt_regs *regs)
                goto none;
        }
 
-       if (!(inta & (CX2_INTA_MASK_ALL & inta_mask))) {
+       if (!(inta & (IPW_INTA_MASK_ALL & inta_mask))) {
                /* Shared interrupt */
                goto none;
        }
 
        /* tell the device to stop sending interrupts */
-       ipw_disable_interrupts(priv);
+       __ipw_disable_interrupts(priv);
 
        /* ack current interrupts */
-       inta &= (CX2_INTA_MASK_ALL & inta_mask);
-       ipw_write32(priv, CX2_INTA_RW, inta);
+       inta &= (IPW_INTA_MASK_ALL & inta_mask);
+       ipw_write32(priv, IPW_INTA_RW, inta);
 
        /* Cache INTA value for our tasklet */
        priv->isr_inta = inta;
 
        tasklet_schedule(&priv->irq_tasklet);
 
-       spin_unlock(&priv->lock);
+       spin_unlock(&priv->irq_lock);
 
        return IRQ_HANDLED;
       none:
-       spin_unlock(&priv->lock);
+       spin_unlock(&priv->irq_lock);
        return IRQ_NONE;
 }
 
@@ -8267,26 +10670,27 @@ static void ipw_rf_kill(void *adapter)
        spin_unlock_irqrestore(&priv->lock, flags);
 }
 
-static void ipw_bg_rf_kill(void *data)
+static void ipw_bg_rf_kill(struct work_struct *work)
 {
-       struct ipw_priv *priv = data;
-       down(&priv->sem);
-       ipw_rf_kill(data);
-       up(&priv->sem);
+       struct ipw_priv *priv =
+               container_of(work, struct ipw_priv, rf_kill.work);
+       mutex_lock(&priv->mutex);
+       ipw_rf_kill(priv);
+       mutex_unlock(&priv->mutex);
 }
 
-void ipw_link_up(struct ipw_priv *priv)
+static void ipw_link_up(struct ipw_priv *priv)
 {
+       priv->last_seq_num = -1;
+       priv->last_frag_num = -1;
+       priv->last_packet_time = 0;
+
        netif_carrier_on(priv->net_dev);
-       if (netif_queue_stopped(priv->net_dev)) {
-               IPW_DEBUG_NOTIF("waking queue\n");
-               netif_wake_queue(priv->net_dev);
-       } else {
-               IPW_DEBUG_NOTIF("starting queue\n");
-               netif_start_queue(priv->net_dev);
-       }
 
        cancel_delayed_work(&priv->request_scan);
+       cancel_delayed_work(&priv->request_direct_scan);
+       cancel_delayed_work(&priv->request_passive_scan);
+       cancel_delayed_work(&priv->scan_event);
        ipw_reset_stats(priv);
        /* Ensure the rate is updated immediately */
        priv->last_rate = ipw_get_current_rate(priv);
@@ -8298,72 +10702,81 @@ void ipw_link_up(struct ipw_priv *priv)
                queue_delayed_work(priv->workqueue, &priv->request_scan, HZ);
 }
 
-static void ipw_bg_link_up(void *data)
+static void ipw_bg_link_up(struct work_struct *work)
 {
-       struct ipw_priv *priv = data;
-       down(&priv->sem);
-       ipw_link_up(data);
-       up(&priv->sem);
+       struct ipw_priv *priv =
+               container_of(work, struct ipw_priv, link_up);
+       mutex_lock(&priv->mutex);
+       ipw_link_up(priv);
+       mutex_unlock(&priv->mutex);
 }
 
-void ipw_link_down(struct ipw_priv *priv)
+static void ipw_link_down(struct ipw_priv *priv)
 {
        ipw_led_link_down(priv);
        netif_carrier_off(priv->net_dev);
-       netif_stop_queue(priv->net_dev);
        notify_wx_assoc_event(priv);
 
        /* Cancel any queued work ... */
        cancel_delayed_work(&priv->request_scan);
+       cancel_delayed_work(&priv->request_direct_scan);
+       cancel_delayed_work(&priv->request_passive_scan);
        cancel_delayed_work(&priv->adhoc_check);
        cancel_delayed_work(&priv->gather_stats);
 
        ipw_reset_stats(priv);
 
-       /* Queue up another scan... */
-       queue_work(priv->workqueue, &priv->request_scan);
+       if (!(priv->status & STATUS_EXIT_PENDING)) {
+               /* Queue up another scan... */
+               queue_delayed_work(priv->workqueue, &priv->request_scan, 0);
+       } else
+               cancel_delayed_work(&priv->scan_event);
 }
 
-static void ipw_bg_link_down(void *data)
+static void ipw_bg_link_down(struct work_struct *work)
 {
-       struct ipw_priv *priv = data;
-       down(&priv->sem);
-       ipw_link_down(data);
-       up(&priv->sem);
+       struct ipw_priv *priv =
+               container_of(work, struct ipw_priv, link_down);
+       mutex_lock(&priv->mutex);
+       ipw_link_down(priv);
+       mutex_unlock(&priv->mutex);
 }
 
-static int ipw_setup_deferred_work(struct ipw_priv *priv)
+static int __devinit ipw_setup_deferred_work(struct ipw_priv *priv)
 {
        int ret = 0;
 
        priv->workqueue = create_workqueue(DRV_NAME);
        init_waitqueue_head(&priv->wait_command_queue);
-
-       INIT_WORK(&priv->adhoc_check, ipw_bg_adhoc_check, priv);
-       INIT_WORK(&priv->associate, ipw_bg_associate, priv);
-       INIT_WORK(&priv->disassociate, ipw_bg_disassociate, priv);
-       INIT_WORK(&priv->rx_replenish, ipw_bg_rx_queue_replenish, priv);
-       INIT_WORK(&priv->adapter_restart, ipw_bg_adapter_restart, priv);
-       INIT_WORK(&priv->rf_kill, ipw_bg_rf_kill, priv);
-       INIT_WORK(&priv->up, (void (*)(void *))ipw_bg_up, priv);
-       INIT_WORK(&priv->down, (void (*)(void *))ipw_bg_down, priv);
-       INIT_WORK(&priv->request_scan,
-                 (void (*)(void *))ipw_bg_request_scan, priv);
-       INIT_WORK(&priv->gather_stats,
-                 (void (*)(void *))ipw_bg_gather_stats, priv);
-       INIT_WORK(&priv->abort_scan, (void (*)(void *))ipw_bg_abort_scan, priv);
-       INIT_WORK(&priv->roam, ipw_bg_roam, priv);
-       INIT_WORK(&priv->scan_check, ipw_bg_scan_check, priv);
-       INIT_WORK(&priv->link_up, (void (*)(void *))ipw_bg_link_up, priv);
-       INIT_WORK(&priv->link_down, (void (*)(void *))ipw_bg_link_down, priv);
-       INIT_WORK(&priv->led_link_on, (void (*)(void *))ipw_bg_led_link_on,
-                 priv);
-       INIT_WORK(&priv->led_link_off, (void (*)(void *))ipw_bg_led_link_off,
-                 priv);
-       INIT_WORK(&priv->led_act_off, (void (*)(void *))ipw_bg_led_activity_off,
-                 priv);
-       INIT_WORK(&priv->merge_networks,
-                 (void (*)(void *))ipw_merge_adhoc_network, priv);
+       init_waitqueue_head(&priv->wait_state);
+
+       INIT_DELAYED_WORK(&priv->adhoc_check, ipw_bg_adhoc_check);
+       INIT_WORK(&priv->associate, ipw_bg_associate);
+       INIT_WORK(&priv->disassociate, ipw_bg_disassociate);
+       INIT_WORK(&priv->system_config, ipw_system_config);
+       INIT_WORK(&priv->rx_replenish, ipw_bg_rx_queue_replenish);
+       INIT_WORK(&priv->adapter_restart, ipw_bg_adapter_restart);
+       INIT_DELAYED_WORK(&priv->rf_kill, ipw_bg_rf_kill);
+       INIT_WORK(&priv->up, ipw_bg_up);
+       INIT_WORK(&priv->down, ipw_bg_down);
+       INIT_DELAYED_WORK(&priv->request_scan, ipw_request_scan);
+       INIT_DELAYED_WORK(&priv->request_direct_scan, ipw_request_direct_scan);
+       INIT_DELAYED_WORK(&priv->request_passive_scan, ipw_request_passive_scan);
+       INIT_DELAYED_WORK(&priv->scan_event, ipw_scan_event);
+       INIT_DELAYED_WORK(&priv->gather_stats, ipw_bg_gather_stats);
+       INIT_WORK(&priv->abort_scan, ipw_bg_abort_scan);
+       INIT_WORK(&priv->roam, ipw_bg_roam);
+       INIT_DELAYED_WORK(&priv->scan_check, ipw_bg_scan_check);
+       INIT_WORK(&priv->link_up, ipw_bg_link_up);
+       INIT_WORK(&priv->link_down, ipw_bg_link_down);
+       INIT_DELAYED_WORK(&priv->led_link_on, ipw_bg_led_link_on);
+       INIT_DELAYED_WORK(&priv->led_link_off, ipw_bg_led_link_off);
+       INIT_DELAYED_WORK(&priv->led_act_off, ipw_bg_led_activity_off);
+       INIT_WORK(&priv->merge_networks, ipw_merge_adhoc_network);
+
+#ifdef CONFIG_IPW2200_QOS
+       INIT_WORK(&priv->qos_activate, ipw_bg_qos_activate);
+#endif                         /* CONFIG_IPW2200_QOS */
 
        tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
                     ipw_irq_tasklet, (unsigned long)priv);
@@ -8376,34 +10789,36 @@ static void shim__set_security(struct net_device *dev,
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
        int i;
-       down(&priv->sem);
        for (i = 0; i < 4; i++) {
                if (sec->flags & (1 << i)) {
-                       priv->sec.key_sizes[i] = sec->key_sizes[i];
+                       priv->ieee->sec.encode_alg[i] = sec->encode_alg[i];
+                       priv->ieee->sec.key_sizes[i] = sec->key_sizes[i];
                        if (sec->key_sizes[i] == 0)
-                               priv->sec.flags &= ~(1 << i);
-                       else
-                               memcpy(priv->sec.keys[i], sec->keys[i],
+                               priv->ieee->sec.flags &= ~(1 << i);
+                       else {
+                               memcpy(priv->ieee->sec.keys[i], sec->keys[i],
                                       sec->key_sizes[i]);
-                       priv->sec.flags |= (1 << i);
+                               priv->ieee->sec.flags |= (1 << i);
+                       }
                        priv->status |= STATUS_SECURITY_UPDATED;
-               }
+               } else if (sec->level != SEC_LEVEL_1)
+                       priv->ieee->sec.flags &= ~(1 << i);
        }
 
-       if ((sec->flags & SEC_ACTIVE_KEY) &&
-           priv->sec.active_key != sec->active_key) {
+       if (sec->flags & SEC_ACTIVE_KEY) {
                if (sec->active_key <= 3) {
-                       priv->sec.active_key = sec->active_key;
-                       priv->sec.flags |= SEC_ACTIVE_KEY;
+                       priv->ieee->sec.active_key = sec->active_key;
+                       priv->ieee->sec.flags |= SEC_ACTIVE_KEY;
                } else
-                       priv->sec.flags &= ~SEC_ACTIVE_KEY;
+                       priv->ieee->sec.flags &= ~SEC_ACTIVE_KEY;
                priv->status |= STATUS_SECURITY_UPDATED;
-       }
+       } else
+               priv->ieee->sec.flags &= ~SEC_ACTIVE_KEY;
 
        if ((sec->flags & SEC_AUTH_MODE) &&
-           (priv->sec.auth_mode != sec->auth_mode)) {
-               priv->sec.auth_mode = sec->auth_mode;
-               priv->sec.flags |= SEC_AUTH_MODE;
+           (priv->ieee->sec.auth_mode != sec->auth_mode)) {
+               priv->ieee->sec.auth_mode = sec->auth_mode;
+               priv->ieee->sec.flags |= SEC_AUTH_MODE;
                if (sec->auth_mode == WLAN_AUTH_SHARED_KEY)
                        priv->capability |= CAP_SHARED_KEY;
                else
@@ -8411,9 +10826,9 @@ static void shim__set_security(struct net_device *dev,
                priv->status |= STATUS_SECURITY_UPDATED;
        }
 
-       if (sec->flags & SEC_ENABLED && priv->sec.enabled != sec->enabled) {
-               priv->sec.flags |= SEC_ENABLED;
-               priv->sec.enabled = sec->enabled;
+       if (sec->flags & SEC_ENABLED && priv->ieee->sec.enabled != sec->enabled) {
+               priv->ieee->sec.flags |= SEC_ENABLED;
+               priv->ieee->sec.enabled = sec->enabled;
                priv->status |= STATUS_SECURITY_UPDATED;
                if (sec->enabled)
                        priv->capability |= CAP_PRIVACY_ON;
@@ -8421,27 +10836,32 @@ static void shim__set_security(struct net_device *dev,
                        priv->capability &= ~CAP_PRIVACY_ON;
        }
 
-       if (sec->flags & SEC_LEVEL && priv->sec.level != sec->level) {
-               priv->sec.level = sec->level;
-               priv->sec.flags |= SEC_LEVEL;
+       if (sec->flags & SEC_ENCRYPT)
+               priv->ieee->sec.encrypt = sec->encrypt;
+
+       if (sec->flags & SEC_LEVEL && priv->ieee->sec.level != sec->level) {
+               priv->ieee->sec.level = sec->level;
+               priv->ieee->sec.flags |= SEC_LEVEL;
                priv->status |= STATUS_SECURITY_UPDATED;
        }
 
+       if (!priv->ieee->host_encrypt && (sec->flags & SEC_ENCRYPT))
+               ipw_set_hwcrypto_keys(priv);
+
        /* To match current functionality of ipw2100 (which works well w/
         * various supplicants, we don't force a disassociate if the
         * privacy capability changes ... */
 #if 0
        if ((priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) &&
            (((priv->assoc_request.capability &
-              WLAN_CAPABILITY_PRIVACY) && !sec->enabled) ||
+              cpu_to_le16(WLAN_CAPABILITY_PRIVACY)) && !sec->enabled) ||
             (!(priv->assoc_request.capability &
-               WLAN_CAPABILITY_PRIVACY) && sec->enabled))) {
+               cpu_to_le16(WLAN_CAPABILITY_PRIVACY)) && sec->enabled))) {
                IPW_DEBUG_ASSOC("Disassociating due to capability "
                                "change.\n");
                ipw_disassociate(priv);
        }
 #endif
-       up(&priv->sem);
 }
 
 static int init_supported_rates(struct ipw_priv *priv,
@@ -8476,29 +10896,10 @@ static int init_supported_rates(struct ipw_priv *priv,
 
 static int ipw_config(struct ipw_priv *priv)
 {
-       int i;
-       struct ipw_tx_power tx_power;
-
-       memset(&priv->sys_config, 0, sizeof(priv->sys_config));
-       memset(&tx_power, 0, sizeof(tx_power));
-
        /* This is only called from ipw_up, which resets/reloads the firmware
           so, we don't need to first disable the card before we configure
           it */
-
-       /* configure device for 'G' band */
-       tx_power.ieee_mode = IPW_G_MODE;
-       tx_power.num_channels = 11;
-       for (i = 0; i < 11; i++) {
-               tx_power.channels_tx_power[i].channel_number = i + 1;
-               tx_power.channels_tx_power[i].tx_power = priv->tx_power;
-       }
-       if (ipw_send_tx_power(priv, &tx_power))
-               goto error;
-
-       /* configure device to also handle 'B' band */
-       tx_power.ieee_mode = IPW_B_MODE;
-       if (ipw_send_tx_power(priv, &tx_power))
+       if (ipw_set_tx_power(priv))
                goto error;
 
        /* initialize adapter address */
@@ -8507,12 +10908,35 @@ static int ipw_config(struct ipw_priv *priv)
 
        /* set basic system config settings */
        init_sys_config(&priv->sys_config);
+
+       /* Support Bluetooth if we have BT h/w on board, and user wants to.
+        * Does not support BT priority yet (don't abort or defer our Tx) */
+       if (bt_coexist) {
+               unsigned char bt_caps = priv->eeprom[EEPROM_SKU_CAPABILITY];
+
+               if (bt_caps & EEPROM_SKU_CAP_BT_CHANNEL_SIG)
+                       priv->sys_config.bt_coexistence
+                           |= CFG_BT_COEXISTENCE_SIGNAL_CHNL;
+               if (bt_caps & EEPROM_SKU_CAP_BT_OOB)
+                       priv->sys_config.bt_coexistence
+                           |= CFG_BT_COEXISTENCE_OOB;
+       }
+
+#ifdef CONFIG_IPW2200_PROMISCUOUS
+       if (priv->prom_net_dev && netif_running(priv->prom_net_dev)) {
+               priv->sys_config.accept_all_data_frames = 1;
+               priv->sys_config.accept_non_directed_frames = 1;
+               priv->sys_config.accept_all_mgmt_bcpr = 1;
+               priv->sys_config.accept_all_mgmt_frames = 1;
+       }
+#endif
+
        if (priv->ieee->iw_mode == IW_MODE_ADHOC)
                priv->sys_config.answer_broadcast_ssid_probe = 1;
        else
                priv->sys_config.answer_broadcast_ssid_probe = 0;
 
-       if (ipw_send_system_config(priv, &priv->sys_config))
+       if (ipw_send_system_config(priv))
                goto error;
 
        init_supported_rates(priv, &priv->rates);
@@ -8524,6 +10948,10 @@ static int ipw_config(struct ipw_priv *priv)
                if (ipw_send_rts_threshold(priv, priv->rts_threshold))
                        goto error;
        }
+#ifdef CONFIG_IPW2200_QOS
+       IPW_DEBUG_QOS("QoS: call ipw_qos_activate\n");
+       ipw_qos_activate(priv, NULL);
+#endif                         /* CONFIG_IPW2200_QOS */
 
        if (ipw_set_random_seed(priv))
                goto error;
@@ -8532,11 +10960,17 @@ static int ipw_config(struct ipw_priv *priv)
        if (ipw_send_host_complete(priv))
                goto error;
 
-       /* If configured to try and auto-associate, kick off a scan */
-       if ((priv->config & CFG_ASSOCIATE) && ipw_request_scan(priv)) {
-               IPW_WARNING("error sending scan request\n");
-               goto error;
-       }
+       priv->status |= STATUS_INIT;
+
+       ipw_led_init(priv);
+       ipw_led_radio_on(priv);
+       priv->notif_missed_beacons = 0;
+
+       /* Set hardware WEP key if it is configured. */
+       if ((priv->capability & CAP_PRIVACY_ON) &&
+           (priv->ieee->sec.level == SEC_LEVEL_1) &&
+           !(priv->ieee->host_encrypt || priv->ieee->host_decrypt))
+               ipw_set_hwcrypto_keys(priv);
 
        return 0;
 
@@ -8544,20 +10978,292 @@ static int ipw_config(struct ipw_priv *priv)
        return -EIO;
 }
 
+/*
+ * NOTE:
+ *
+ * These tables have been tested in conjunction with the
+ * Intel PRO/Wireless 2200BG and 2915ABG Network Connection Adapters.
+ *
+ * Altering this values, using it on other hardware, or in geographies
+ * not intended for resale of the above mentioned Intel adapters has
+ * not been tested.
+ *
+ * Remember to update the table in README.ipw2200 when changing this
+ * table.
+ *
+ */
+static const struct ieee80211_geo ipw_geos[] = {
+       {                       /* Restricted */
+        "---",
+        .bg_channels = 11,
+        .bg = {{2412, 1}, {2417, 2}, {2422, 3},
+               {2427, 4}, {2432, 5}, {2437, 6},
+               {2442, 7}, {2447, 8}, {2452, 9},
+               {2457, 10}, {2462, 11}},
+        },
+
+       {                       /* Custom US/Canada */
+        "ZZF",
+        .bg_channels = 11,
+        .bg = {{2412, 1}, {2417, 2}, {2422, 3},
+               {2427, 4}, {2432, 5}, {2437, 6},
+               {2442, 7}, {2447, 8}, {2452, 9},
+               {2457, 10}, {2462, 11}},
+        .a_channels = 8,
+        .a = {{5180, 36},
+              {5200, 40},
+              {5220, 44},
+              {5240, 48},
+              {5260, 52, IEEE80211_CH_PASSIVE_ONLY},
+              {5280, 56, IEEE80211_CH_PASSIVE_ONLY},
+              {5300, 60, IEEE80211_CH_PASSIVE_ONLY},
+              {5320, 64, IEEE80211_CH_PASSIVE_ONLY}},
+        },
+
+       {                       /* Rest of World */
+        "ZZD",
+        .bg_channels = 13,
+        .bg = {{2412, 1}, {2417, 2}, {2422, 3},
+               {2427, 4}, {2432, 5}, {2437, 6},
+               {2442, 7}, {2447, 8}, {2452, 9},
+               {2457, 10}, {2462, 11}, {2467, 12},
+               {2472, 13}},
+        },
+
+       {                       /* Custom USA & Europe & High */
+        "ZZA",
+        .bg_channels = 11,
+        .bg = {{2412, 1}, {2417, 2}, {2422, 3},
+               {2427, 4}, {2432, 5}, {2437, 6},
+               {2442, 7}, {2447, 8}, {2452, 9},
+               {2457, 10}, {2462, 11}},
+        .a_channels = 13,
+        .a = {{5180, 36},
+              {5200, 40},
+              {5220, 44},
+              {5240, 48},
+              {5260, 52, IEEE80211_CH_PASSIVE_ONLY},
+              {5280, 56, IEEE80211_CH_PASSIVE_ONLY},
+              {5300, 60, IEEE80211_CH_PASSIVE_ONLY},
+              {5320, 64, IEEE80211_CH_PASSIVE_ONLY},
+              {5745, 149},
+              {5765, 153},
+              {5785, 157},
+              {5805, 161},
+              {5825, 165}},
+        },
+
+       {                       /* Custom NA & Europe */
+        "ZZB",
+        .bg_channels = 11,
+        .bg = {{2412, 1}, {2417, 2}, {2422, 3},
+               {2427, 4}, {2432, 5}, {2437, 6},
+               {2442, 7}, {2447, 8}, {2452, 9},
+               {2457, 10}, {2462, 11}},
+        .a_channels = 13,
+        .a = {{5180, 36},
+              {5200, 40},
+              {5220, 44},
+              {5240, 48},
+              {5260, 52, IEEE80211_CH_PASSIVE_ONLY},
+              {5280, 56, IEEE80211_CH_PASSIVE_ONLY},
+              {5300, 60, IEEE80211_CH_PASSIVE_ONLY},
+              {5320, 64, IEEE80211_CH_PASSIVE_ONLY},
+              {5745, 149, IEEE80211_CH_PASSIVE_ONLY},
+              {5765, 153, IEEE80211_CH_PASSIVE_ONLY},
+              {5785, 157, IEEE80211_CH_PASSIVE_ONLY},
+              {5805, 161, IEEE80211_CH_PASSIVE_ONLY},
+              {5825, 165, IEEE80211_CH_PASSIVE_ONLY}},
+        },
+
+       {                       /* Custom Japan */
+        "ZZC",
+        .bg_channels = 11,
+        .bg = {{2412, 1}, {2417, 2}, {2422, 3},
+               {2427, 4}, {2432, 5}, {2437, 6},
+               {2442, 7}, {2447, 8}, {2452, 9},
+               {2457, 10}, {2462, 11}},
+        .a_channels = 4,
+        .a = {{5170, 34}, {5190, 38},
+              {5210, 42}, {5230, 46}},
+        },
+
+       {                       /* Custom */
+        "ZZM",
+        .bg_channels = 11,
+        .bg = {{2412, 1}, {2417, 2}, {2422, 3},
+               {2427, 4}, {2432, 5}, {2437, 6},
+               {2442, 7}, {2447, 8}, {2452, 9},
+               {2457, 10}, {2462, 11}},
+        },
+
+       {                       /* Europe */
+        "ZZE",
+        .bg_channels = 13,
+        .bg = {{2412, 1}, {2417, 2}, {2422, 3},
+               {2427, 4}, {2432, 5}, {2437, 6},
+               {2442, 7}, {2447, 8}, {2452, 9},
+               {2457, 10}, {2462, 11}, {2467, 12},
+               {2472, 13}},
+        .a_channels = 19,
+        .a = {{5180, 36},
+              {5200, 40},
+              {5220, 44},
+              {5240, 48},
+              {5260, 52, IEEE80211_CH_PASSIVE_ONLY},
+              {5280, 56, IEEE80211_CH_PASSIVE_ONLY},
+              {5300, 60, IEEE80211_CH_PASSIVE_ONLY},
+              {5320, 64, IEEE80211_CH_PASSIVE_ONLY},
+              {5500, 100, IEEE80211_CH_PASSIVE_ONLY},
+              {5520, 104, IEEE80211_CH_PASSIVE_ONLY},
+              {5540, 108, IEEE80211_CH_PASSIVE_ONLY},
+              {5560, 112, IEEE80211_CH_PASSIVE_ONLY},
+              {5580, 116, IEEE80211_CH_PASSIVE_ONLY},
+              {5600, 120, IEEE80211_CH_PASSIVE_ONLY},
+              {5620, 124, IEEE80211_CH_PASSIVE_ONLY},
+              {5640, 128, IEEE80211_CH_PASSIVE_ONLY},
+              {5660, 132, IEEE80211_CH_PASSIVE_ONLY},
+              {5680, 136, IEEE80211_CH_PASSIVE_ONLY},
+              {5700, 140, IEEE80211_CH_PASSIVE_ONLY}},
+        },
+
+       {                       /* Custom Japan */
+        "ZZJ",
+        .bg_channels = 14,
+        .bg = {{2412, 1}, {2417, 2}, {2422, 3},
+               {2427, 4}, {2432, 5}, {2437, 6},
+               {2442, 7}, {2447, 8}, {2452, 9},
+               {2457, 10}, {2462, 11}, {2467, 12},
+               {2472, 13}, {2484, 14, IEEE80211_CH_B_ONLY}},
+        .a_channels = 4,
+        .a = {{5170, 34}, {5190, 38},
+              {5210, 42}, {5230, 46}},
+        },
+
+       {                       /* Rest of World */
+        "ZZR",
+        .bg_channels = 14,
+        .bg = {{2412, 1}, {2417, 2}, {2422, 3},
+               {2427, 4}, {2432, 5}, {2437, 6},
+               {2442, 7}, {2447, 8}, {2452, 9},
+               {2457, 10}, {2462, 11}, {2467, 12},
+               {2472, 13}, {2484, 14, IEEE80211_CH_B_ONLY |
+                            IEEE80211_CH_PASSIVE_ONLY}},
+        },
+
+       {                       /* High Band */
+        "ZZH",
+        .bg_channels = 13,
+        .bg = {{2412, 1}, {2417, 2}, {2422, 3},
+               {2427, 4}, {2432, 5}, {2437, 6},
+               {2442, 7}, {2447, 8}, {2452, 9},
+               {2457, 10}, {2462, 11},
+               {2467, 12, IEEE80211_CH_PASSIVE_ONLY},
+               {2472, 13, IEEE80211_CH_PASSIVE_ONLY}},
+        .a_channels = 4,
+        .a = {{5745, 149}, {5765, 153},
+              {5785, 157}, {5805, 161}},
+        },
+
+       {                       /* Custom Europe */
+        "ZZG",
+        .bg_channels = 13,
+        .bg = {{2412, 1}, {2417, 2}, {2422, 3},
+               {2427, 4}, {2432, 5}, {2437, 6},
+               {2442, 7}, {2447, 8}, {2452, 9},
+               {2457, 10}, {2462, 11},
+               {2467, 12}, {2472, 13}},
+        .a_channels = 4,
+        .a = {{5180, 36}, {5200, 40},
+              {5220, 44}, {5240, 48}},
+        },
+
+       {                       /* Europe */
+        "ZZK",
+        .bg_channels = 13,
+        .bg = {{2412, 1}, {2417, 2}, {2422, 3},
+               {2427, 4}, {2432, 5}, {2437, 6},
+               {2442, 7}, {2447, 8}, {2452, 9},
+               {2457, 10}, {2462, 11},
+               {2467, 12, IEEE80211_CH_PASSIVE_ONLY},
+               {2472, 13, IEEE80211_CH_PASSIVE_ONLY}},
+        .a_channels = 24,
+        .a = {{5180, 36, IEEE80211_CH_PASSIVE_ONLY},
+              {5200, 40, IEEE80211_CH_PASSIVE_ONLY},
+              {5220, 44, IEEE80211_CH_PASSIVE_ONLY},
+              {5240, 48, IEEE80211_CH_PASSIVE_ONLY},
+              {5260, 52, IEEE80211_CH_PASSIVE_ONLY},
+              {5280, 56, IEEE80211_CH_PASSIVE_ONLY},
+              {5300, 60, IEEE80211_CH_PASSIVE_ONLY},
+              {5320, 64, IEEE80211_CH_PASSIVE_ONLY},
+              {5500, 100, IEEE80211_CH_PASSIVE_ONLY},
+              {5520, 104, IEEE80211_CH_PASSIVE_ONLY},
+              {5540, 108, IEEE80211_CH_PASSIVE_ONLY},
+              {5560, 112, IEEE80211_CH_PASSIVE_ONLY},
+              {5580, 116, IEEE80211_CH_PASSIVE_ONLY},
+              {5600, 120, IEEE80211_CH_PASSIVE_ONLY},
+              {5620, 124, IEEE80211_CH_PASSIVE_ONLY},
+              {5640, 128, IEEE80211_CH_PASSIVE_ONLY},
+              {5660, 132, IEEE80211_CH_PASSIVE_ONLY},
+              {5680, 136, IEEE80211_CH_PASSIVE_ONLY},
+              {5700, 140, IEEE80211_CH_PASSIVE_ONLY},
+              {5745, 149, IEEE80211_CH_PASSIVE_ONLY},
+              {5765, 153, IEEE80211_CH_PASSIVE_ONLY},
+              {5785, 157, IEEE80211_CH_PASSIVE_ONLY},
+              {5805, 161, IEEE80211_CH_PASSIVE_ONLY},
+              {5825, 165, IEEE80211_CH_PASSIVE_ONLY}},
+        },
+
+       {                       /* Europe */
+        "ZZL",
+        .bg_channels = 11,
+        .bg = {{2412, 1}, {2417, 2}, {2422, 3},
+               {2427, 4}, {2432, 5}, {2437, 6},
+               {2442, 7}, {2447, 8}, {2452, 9},
+               {2457, 10}, {2462, 11}},
+        .a_channels = 13,
+        .a = {{5180, 36, IEEE80211_CH_PASSIVE_ONLY},
+              {5200, 40, IEEE80211_CH_PASSIVE_ONLY},
+              {5220, 44, IEEE80211_CH_PASSIVE_ONLY},
+              {5240, 48, IEEE80211_CH_PASSIVE_ONLY},
+              {5260, 52, IEEE80211_CH_PASSIVE_ONLY},
+              {5280, 56, IEEE80211_CH_PASSIVE_ONLY},
+              {5300, 60, IEEE80211_CH_PASSIVE_ONLY},
+              {5320, 64, IEEE80211_CH_PASSIVE_ONLY},
+              {5745, 149, IEEE80211_CH_PASSIVE_ONLY},
+              {5765, 153, IEEE80211_CH_PASSIVE_ONLY},
+              {5785, 157, IEEE80211_CH_PASSIVE_ONLY},
+              {5805, 161, IEEE80211_CH_PASSIVE_ONLY},
+              {5825, 165, IEEE80211_CH_PASSIVE_ONLY}},
+        }
+};
+
 #define MAX_HW_RESTARTS 5
 static int ipw_up(struct ipw_priv *priv)
 {
-       int rc, i;
+       int rc, i, j;
 
        if (priv->status & STATUS_EXIT_PENDING)
                return -EIO;
 
+       if (cmdlog && !priv->cmdlog) {
+               priv->cmdlog = kcalloc(cmdlog, sizeof(*priv->cmdlog),
+                                      GFP_KERNEL);
+               if (priv->cmdlog == NULL) {
+                       IPW_ERROR("Error allocating %d command log entries.\n",
+                                 cmdlog);
+                       return -ENOMEM;
+               } else {
+                       priv->cmdlog_len = cmdlog;
+               }
+       }
+
        for (i = 0; i < MAX_HW_RESTARTS; i++) {
                /* Load the microcode, firmware, and eeprom.
                 * Also start the clocks. */
                rc = ipw_load(priv);
                if (rc) {
-                       IPW_ERROR("Unable to load firmware: 0x%08X\n", rc);
+                       IPW_ERROR("Unable to load firmware: %d\n", rc);
                        return rc;
                }
 
@@ -8566,17 +11272,44 @@ static int ipw_up(struct ipw_priv *priv)
                        eeprom_parse_mac(priv, priv->mac_addr);
                memcpy(priv->net_dev->dev_addr, priv->mac_addr, ETH_ALEN);
 
-               if (priv->status & STATUS_RF_KILL_MASK) {
+               for (j = 0; j < ARRAY_SIZE(ipw_geos); j++) {
+                       if (!memcmp(&priv->eeprom[EEPROM_COUNTRY_CODE],
+                                   ipw_geos[j].name, 3))
+                               break;
+               }
+               if (j == ARRAY_SIZE(ipw_geos)) {
+                       IPW_WARNING("SKU [%c%c%c] not recognized.\n",
+                                   priv->eeprom[EEPROM_COUNTRY_CODE + 0],
+                                   priv->eeprom[EEPROM_COUNTRY_CODE + 1],
+                                   priv->eeprom[EEPROM_COUNTRY_CODE + 2]);
+                       j = 0;
+               }
+               if (ieee80211_set_geo(priv->ieee, &ipw_geos[j])) {
+                       IPW_WARNING("Could not set geography.");
+                       return 0;
+               }
+
+               if (priv->status & STATUS_RF_KILL_SW) {
+                       IPW_WARNING("Radio disabled by module parameter.\n");
+                       return 0;
+               } else if (rf_kill_active(priv)) {
+                       IPW_WARNING("Radio Frequency Kill Switch is On:\n"
+                                   "Kill switch must be turned off for "
+                                   "wireless networking to work.\n");
+                       queue_delayed_work(priv->workqueue, &priv->rf_kill,
+                                          2 * HZ);
                        return 0;
                }
 
                rc = ipw_config(priv);
                if (!rc) {
                        IPW_DEBUG_INFO("Configured device on count %i\n", i);
-                       ipw_led_init(priv);
-                       ipw_led_radio_on(priv);
-                       priv->notif_missed_beacons = 0;
-                       priv->status |= STATUS_INIT;
+
+                       /* If configure to try and auto-associate, kick
+                        * off a scan. */
+                       queue_delayed_work(priv->workqueue,
+                                          &priv->request_scan, 0);
+
                        return 0;
                }
 
@@ -8596,82 +11329,98 @@ static int ipw_up(struct ipw_priv *priv)
        return -EIO;
 }
 
-static void ipw_bg_up(void *data)
+static void ipw_bg_up(struct work_struct *work)
 {
-       struct ipw_priv *priv = data;
-       down(&priv->sem);
-       ipw_up(data);
-       up(&priv->sem);
+       struct ipw_priv *priv =
+               container_of(work, struct ipw_priv, up);
+       mutex_lock(&priv->mutex);
+       ipw_up(priv);
+       mutex_unlock(&priv->mutex);
 }
 
-static void ipw_down(struct ipw_priv *priv)
+static void ipw_deinit(struct ipw_priv *priv)
 {
-#if 0
+       int i;
+
+       if (priv->status & STATUS_SCANNING) {
+               IPW_DEBUG_INFO("Aborting scan during shutdown.\n");
+               ipw_abort_scan(priv);
+       }
+
+       if (priv->status & STATUS_ASSOCIATED) {
+               IPW_DEBUG_INFO("Disassociating during shutdown.\n");
+               ipw_disassociate(priv);
+       }
+
+       ipw_led_shutdown(priv);
+
+       /* Wait up to 1s for status to change to not scanning and not
+        * associated (disassociation can take a while for a ful 802.11
+        * exchange */
+       for (i = 1000; i && (priv->status &
+                            (STATUS_DISASSOCIATING |
+                             STATUS_ASSOCIATED | STATUS_SCANNING)); i--)
+               udelay(10);
+
+       if (priv->status & (STATUS_DISASSOCIATING |
+                           STATUS_ASSOCIATED | STATUS_SCANNING))
+               IPW_DEBUG_INFO("Still associated or scanning...\n");
+       else
+               IPW_DEBUG_INFO("Took %dms to de-init\n", 1000 - i);
+
        /* Attempt to disable the card */
        ipw_send_card_disable(priv, 0);
-#endif
+
+       priv->status &= ~STATUS_INIT;
+}
+
+static void ipw_down(struct ipw_priv *priv)
+{
+       int exit_pending = priv->status & STATUS_EXIT_PENDING;
+
+       priv->status |= STATUS_EXIT_PENDING;
+
+       if (ipw_is_init(priv))
+               ipw_deinit(priv);
+
+       /* Wipe out the EXIT_PENDING status bit if we are not actually
+        * exiting the module */
+       if (!exit_pending)
+               priv->status &= ~STATUS_EXIT_PENDING;
 
        /* tell the device to stop sending interrupts */
        ipw_disable_interrupts(priv);
 
        /* Clear all bits but the RF Kill */
-       priv->status &= STATUS_RF_KILL_MASK;
+       priv->status &= STATUS_RF_KILL_MASK | STATUS_EXIT_PENDING;
        netif_carrier_off(priv->net_dev);
-       netif_stop_queue(priv->net_dev);
 
        ipw_stop_nic(priv);
 
        ipw_led_radio_off(priv);
 }
 
-static void ipw_bg_down(void *data)
-{
-       struct ipw_priv *priv = data;
-       down(&priv->sem);
-       ipw_down(data);
-       up(&priv->sem);
-}
-
-static int ipw_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
+static void ipw_bg_down(struct work_struct *work)
 {
-       struct iwreq *wrq = (struct iwreq *)rq;
-       int ret = -1;
-       switch (cmd) {
-       case IPW_IOCTL_WPA_SUPPLICANT:
-               ret = ipw_wpa_supplicant(dev, &wrq->u.data);
-               return ret;
-
-       default:
-               return -EOPNOTSUPP;
-       }
-
-       return -EOPNOTSUPP;
+       struct ipw_priv *priv =
+               container_of(work, struct ipw_priv, down);
+       mutex_lock(&priv->mutex);
+       ipw_down(priv);
+       mutex_unlock(&priv->mutex);
 }
 
 /* Called by register_netdev() */
 static int ipw_net_init(struct net_device *dev)
 {
        struct ipw_priv *priv = ieee80211_priv(dev);
-       down(&priv->sem);
-       if (priv->status & STATUS_RF_KILL_SW) {
-               IPW_WARNING("Radio disabled by module parameter.\n");
-               up(&priv->sem);
-               return 0;
-       } else if (rf_kill_active(priv)) {
-               IPW_WARNING("Radio Frequency Kill Switch is On:\n"
-                           "Kill switch must be turned off for "
-                           "wireless networking to work.\n");
-               queue_delayed_work(priv->workqueue, &priv->rf_kill, 2 * HZ);
-               up(&priv->sem);
-               return 0;
-       }
+       mutex_lock(&priv->mutex);
 
        if (ipw_up(priv)) {
-               up(&priv->sem);
+               mutex_unlock(&priv->mutex);
                return -EIO;
        }
 
-       up(&priv->sem);
+       mutex_unlock(&priv->mutex);
        return 0;
 }
 
@@ -8716,13 +11465,21 @@ static struct attribute *ipw_sysfs_entries[] = {
        &dev_attr_nic_type.attr,
        &dev_attr_status.attr,
        &dev_attr_cfg.attr,
-       &dev_attr_dump_errors.attr,
-       &dev_attr_dump_events.attr,
+       &dev_attr_error.attr,
+       &dev_attr_event_log.attr,
+       &dev_attr_cmd_log.attr,
        &dev_attr_eeprom_delay.attr,
        &dev_attr_ucode_version.attr,
        &dev_attr_rtc.attr,
        &dev_attr_scan_age.attr,
        &dev_attr_led.attr,
+       &dev_attr_speed_scan.attr,
+       &dev_attr_net_stats.attr,
+       &dev_attr_channels.attr,
+#ifdef CONFIG_IPW2200_PROMISCUOUS
+       &dev_attr_rtap_iface.attr,
+       &dev_attr_rtap_filter.attr,
+#endif
        NULL
 };
 
@@ -8731,14 +11488,118 @@ static struct attribute_group ipw_attribute_group = {
        .attrs = ipw_sysfs_entries,
 };
 
-static int ipw_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
+#ifdef CONFIG_IPW2200_PROMISCUOUS
+static int ipw_prom_open(struct net_device *dev)
+{
+       struct ipw_prom_priv *prom_priv = ieee80211_priv(dev);
+       struct ipw_priv *priv = prom_priv->priv;
+
+       IPW_DEBUG_INFO("prom dev->open\n");
+       netif_carrier_off(dev);
+
+       if (priv->ieee->iw_mode != IW_MODE_MONITOR) {
+               priv->sys_config.accept_all_data_frames = 1;
+               priv->sys_config.accept_non_directed_frames = 1;
+               priv->sys_config.accept_all_mgmt_bcpr = 1;
+               priv->sys_config.accept_all_mgmt_frames = 1;
+
+               ipw_send_system_config(priv);
+       }
+
+       return 0;
+}
+
+static int ipw_prom_stop(struct net_device *dev)
+{
+       struct ipw_prom_priv *prom_priv = ieee80211_priv(dev);
+       struct ipw_priv *priv = prom_priv->priv;
+
+       IPW_DEBUG_INFO("prom dev->stop\n");
+
+       if (priv->ieee->iw_mode != IW_MODE_MONITOR) {
+               priv->sys_config.accept_all_data_frames = 0;
+               priv->sys_config.accept_non_directed_frames = 0;
+               priv->sys_config.accept_all_mgmt_bcpr = 0;
+               priv->sys_config.accept_all_mgmt_frames = 0;
+
+               ipw_send_system_config(priv);
+       }
+
+       return 0;
+}
+
+static int ipw_prom_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
+{
+       IPW_DEBUG_INFO("prom dev->xmit\n");
+       return -EOPNOTSUPP;
+}
+
+static struct net_device_stats *ipw_prom_get_stats(struct net_device *dev)
+{
+       struct ipw_prom_priv *prom_priv = ieee80211_priv(dev);
+       return &prom_priv->ieee->stats;
+}
+
+static int ipw_prom_alloc(struct ipw_priv *priv)
+{
+       int rc = 0;
+
+       if (priv->prom_net_dev)
+               return -EPERM;
+
+       priv->prom_net_dev = alloc_ieee80211(sizeof(struct ipw_prom_priv));
+       if (priv->prom_net_dev == NULL)
+               return -ENOMEM;
+
+       priv->prom_priv = ieee80211_priv(priv->prom_net_dev);
+       priv->prom_priv->ieee = netdev_priv(priv->prom_net_dev);
+       priv->prom_priv->priv = priv;
+
+       strcpy(priv->prom_net_dev->name, "rtap%d");
+       memcpy(priv->prom_net_dev->dev_addr, priv->mac_addr, ETH_ALEN);
+
+       priv->prom_net_dev->type = ARPHRD_IEEE80211_RADIOTAP;
+       priv->prom_net_dev->open = ipw_prom_open;
+       priv->prom_net_dev->stop = ipw_prom_stop;
+       priv->prom_net_dev->get_stats = ipw_prom_get_stats;
+       priv->prom_net_dev->hard_start_xmit = ipw_prom_hard_start_xmit;
+
+       priv->prom_priv->ieee->iw_mode = IW_MODE_MONITOR;
+       SET_NETDEV_DEV(priv->prom_net_dev, &priv->pci_dev->dev);
+
+       rc = register_netdev(priv->prom_net_dev);
+       if (rc) {
+               free_ieee80211(priv->prom_net_dev);
+               priv->prom_net_dev = NULL;
+               return rc;
+       }
+
+       return 0;
+}
+
+static void ipw_prom_free(struct ipw_priv *priv)
+{
+       if (!priv->prom_net_dev)
+               return;
+
+       unregister_netdev(priv->prom_net_dev);
+       free_ieee80211(priv->prom_net_dev);
+
+       priv->prom_net_dev = NULL;
+}
+
+#endif
+
+
+static int __devinit ipw_pci_probe(struct pci_dev *pdev,
+                                  const struct pci_device_id *ent)
 {
        int err = 0;
        struct net_device *net_dev;
        void __iomem *base;
        u32 length, val;
        struct ipw_priv *priv;
-       int band, modulation;
+       int i;
 
        net_dev = alloc_ieee80211(sizeof(struct ipw_priv));
        if (net_dev == NULL) {
@@ -8751,12 +11612,13 @@ static int ipw_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 
        priv->net_dev = net_dev;
        priv->pci_dev = pdev;
-#ifdef CONFIG_IPW_DEBUG
        ipw_debug_level = debug;
-#endif
+       spin_lock_init(&priv->irq_lock);
        spin_lock_init(&priv->lock);
+       for (i = 0; i < IPW_IBSS_MAC_HASH_SIZE; i++)
+               INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
 
-       init_MUTEX(&priv->sem);
+       mutex_init(&priv->mutex);
        if (pci_enable_device(pdev)) {
                err = -ENODEV;
                goto out_free_ieee80211;
@@ -8803,105 +11665,28 @@ static int ipw_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
                goto out_iounmap;
        }
 
-       /* Initialize module parameter values here */
-
-       /* We default to disabling the LED code as right now it causes
-        * too many systems to lock up... */
-       if (!led)
-               priv->config |= CFG_NO_LED;
-
-       if (associate)
-               priv->config |= CFG_ASSOCIATE;
-       else
-               IPW_DEBUG_INFO("Auto associate disabled.\n");
-
-       if (auto_create)
-               priv->config |= CFG_ADHOC_CREATE;
-       else
-               IPW_DEBUG_INFO("Auto adhoc creation disabled.\n");
-
-       if (disable) {
-               priv->status |= STATUS_RF_KILL_SW;
-               IPW_DEBUG_INFO("Radio disabled.\n");
-       }
-
-       if (channel != 0) {
-               priv->config |= CFG_STATIC_CHANNEL;
-               priv->channel = channel;
-               IPW_DEBUG_INFO("Bind to static channel %d\n", channel);
-               IPW_DEBUG_INFO("Bind to static channel %d\n", channel);
-               /* TODO: Validate that provided channel is in range */
-       }
-
-       switch (mode) {
-       case 1:
-               priv->ieee->iw_mode = IW_MODE_ADHOC;
-               break;
-#ifdef CONFIG_IPW_MONITOR
-       case 2:
-               priv->ieee->iw_mode = IW_MODE_MONITOR;
-               break;
-#endif
-       default:
-       case 0:
-               priv->ieee->iw_mode = IW_MODE_INFRA;
-               break;
-       }
-
-       if ((priv->pci_dev->device == 0x4223) ||
-           (priv->pci_dev->device == 0x4224)) {
-               printk(KERN_INFO DRV_NAME
-                      ": Detected Intel PRO/Wireless 2915ABG Network "
-                      "Connection\n");
-               priv->ieee->abg_true = 1;
-               band = IEEE80211_52GHZ_BAND | IEEE80211_24GHZ_BAND;
-               modulation = IEEE80211_OFDM_MODULATION |
-                   IEEE80211_CCK_MODULATION;
-               priv->adapter = IPW_2915ABG;
-               priv->ieee->mode = IEEE_A | IEEE_G | IEEE_B;
-       } else {
-               printk(KERN_INFO DRV_NAME
-                      ": Detected Intel PRO/Wireless 2200BG Network "
-                      "Connection\n");
-
-               priv->ieee->abg_true = 0;
-               band = IEEE80211_24GHZ_BAND;
-               modulation = IEEE80211_OFDM_MODULATION |
-                   IEEE80211_CCK_MODULATION;
-               priv->adapter = IPW_2200BG;
-               priv->ieee->mode = IEEE_G | IEEE_B;
-       }
-
-       priv->ieee->freq_band = band;
-       priv->ieee->modulation = modulation;
-
-       priv->rates_mask = IEEE80211_DEFAULT_RATES_MASK;
-
-       priv->missed_beacon_threshold = IPW_MB_DISASSOCIATE_THRESHOLD_DEFAULT;
-       priv->roaming_threshold = IPW_MB_ROAMING_THRESHOLD_DEFAULT;
-
-       priv->rts_threshold = DEFAULT_RTS_THRESHOLD;
-
-       /* If power management is turned on, default to AC mode */
-       priv->power_mode = IPW_POWER_AC;
-       priv->tx_power = IPW_DEFAULT_TX_POWER;
+       ipw_sw_reset(priv, 1);
 
-       err = request_irq(pdev->irq, ipw_isr, SA_SHIRQ, DRV_NAME, priv);
+       err = request_irq(pdev->irq, ipw_isr, IRQF_SHARED, DRV_NAME, priv);
        if (err) {
                IPW_ERROR("Error allocating IRQ %d\n", pdev->irq);
                goto out_destroy_workqueue;
        }
 
-       SET_MODULE_OWNER(net_dev);
        SET_NETDEV_DEV(net_dev, &pdev->dev);
 
-       ipw_wx_data.spy_data = &priv->ieee->spy_data;
-       ipw_wx_data.ieee80211 = priv->ieee;
-
-       down(&priv->sem);
+       mutex_lock(&priv->mutex);
 
        priv->ieee->hard_start_xmit = ipw_net_hard_start_xmit;
        priv->ieee->set_security = shim__set_security;
+       priv->ieee->is_queue_full = ipw_net_is_queue_full;
+
+#ifdef CONFIG_IPW2200_QOS
+       priv->ieee->is_qos_active = ipw_is_qos_active;
+       priv->ieee->handle_probe_response = ipw_handle_beacon;
+       priv->ieee->handle_beacon = ipw_handle_probe_response;
+       priv->ieee->handle_assoc_response = ipw_handle_assoc_response;
+#endif                         /* CONFIG_IPW2200_QOS */
 
        priv->ieee->perfect_rssi = -20;
        priv->ieee->worst_rssi = -85;
@@ -8909,12 +11694,11 @@ static int ipw_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
        net_dev->open = ipw_net_open;
        net_dev->stop = ipw_net_stop;
        net_dev->init = ipw_net_init;
-       net_dev->do_ioctl = ipw_ioctl;
        net_dev->get_stats = ipw_net_get_stats;
        net_dev->set_multicast_list = ipw_net_set_multicast_list;
        net_dev->set_mac_address = ipw_net_set_mac_address;
-       net_dev->get_wireless_stats = ipw_get_wireless_stats;
-       net_dev->wireless_data = &ipw_wx_data;
+       priv->wireless_data.spy_data = &priv->ieee->spy_data;
+       net_dev->wireless_data = &priv->wireless_data;
        net_dev->wireless_handlers = &ipw_wx_handler_def;
        net_dev->ethtool_ops = &ipw_ethtool_ops;
        net_dev->irq = pdev->irq;
@@ -8925,16 +11709,34 @@ static int ipw_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
        err = sysfs_create_group(&pdev->dev.kobj, &ipw_attribute_group);
        if (err) {
                IPW_ERROR("failed to create sysfs device attributes\n");
-               up(&priv->sem);
+               mutex_unlock(&priv->mutex);
                goto out_release_irq;
        }
 
-       up(&priv->sem);
+       mutex_unlock(&priv->mutex);
        err = register_netdev(net_dev);
        if (err) {
                IPW_ERROR("failed to register network device\n");
                goto out_remove_sysfs;
        }
+
+#ifdef CONFIG_IPW2200_PROMISCUOUS
+       if (rtap_iface) {
+               err = ipw_prom_alloc(priv);
+               if (err) {
+                       IPW_ERROR("Failed to register promiscuous network "
+                                 "device (error %d).\n", err);
+                       unregister_netdev(priv->net_dev);
+                       goto out_remove_sysfs;
+               }
+       }
+#endif
+
+       printk(KERN_INFO DRV_NAME ": Detected geography %s (%d 802.11bg "
+              "channels, %d 802.11a channels)\n",
+              priv->ieee->geo.name, priv->ieee->geo.bg_channels,
+              priv->ieee->geo.a_channels);
+
        return 0;
 
       out_remove_sysfs:
@@ -8957,17 +11759,22 @@ static int ipw_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
        return err;
 }
 
-static void ipw_pci_remove(struct pci_dev *pdev)
+static void __devexit ipw_pci_remove(struct pci_dev *pdev)
 {
        struct ipw_priv *priv = pci_get_drvdata(pdev);
+       struct list_head *p, *q;
+       int i;
+
        if (!priv)
                return;
 
-       priv->status |= STATUS_EXIT_PENDING;
+       mutex_lock(&priv->mutex);
 
+       priv->status |= STATUS_EXIT_PENDING;
+       ipw_down(priv);
        sysfs_remove_group(&pdev->dev.kobj, &ipw_attribute_group);
 
-       ipw_down(priv);
+       mutex_unlock(&priv->mutex);
 
        unregister_netdev(priv->net_dev);
 
@@ -8977,33 +11784,45 @@ static void ipw_pci_remove(struct pci_dev *pdev)
        }
        ipw_tx_queue_free(priv);
 
-       ipw_led_shutdown(priv);
-
+       if (priv->cmdlog) {
+               kfree(priv->cmdlog);
+               priv->cmdlog = NULL;
+       }
        /* ipw_down will ensure that there is no more pending work
         * in the workqueue's, so we can safely remove them now. */
        cancel_delayed_work(&priv->adhoc_check);
        cancel_delayed_work(&priv->gather_stats);
        cancel_delayed_work(&priv->request_scan);
+       cancel_delayed_work(&priv->request_direct_scan);
+       cancel_delayed_work(&priv->request_passive_scan);
+       cancel_delayed_work(&priv->scan_event);
        cancel_delayed_work(&priv->rf_kill);
        cancel_delayed_work(&priv->scan_check);
        destroy_workqueue(priv->workqueue);
        priv->workqueue = NULL;
 
+       /* Free MAC hash list for ADHOC */
+       for (i = 0; i < IPW_IBSS_MAC_HASH_SIZE; i++) {
+               list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
+                       list_del(p);
+                       kfree(list_entry(p, struct ipw_ibss_seq, list));
+               }
+       }
+
+       kfree(priv->error);
+       priv->error = NULL;
+
+#ifdef CONFIG_IPW2200_PROMISCUOUS
+       ipw_prom_free(priv);
+#endif
+
        free_irq(pdev->irq, priv);
        iounmap(priv->hw_base);
        pci_release_regions(pdev);
        pci_disable_device(pdev);
        pci_set_drvdata(pdev, NULL);
        free_ieee80211(priv->net_dev);
-
-#ifdef CONFIG_PM
-       if (fw_loaded) {
-               release_firmware(bootfw);
-               release_firmware(ucode);
-               release_firmware(firmware);
-               fw_loaded = 0;
-       }
-#endif
+       free_firmware();
 }
 
 #ifdef CONFIG_PM
@@ -9031,12 +11850,18 @@ static int ipw_pci_resume(struct pci_dev *pdev)
 {
        struct ipw_priv *priv = pci_get_drvdata(pdev);
        struct net_device *dev = priv->net_dev;
+       int err;
        u32 val;
 
        printk(KERN_INFO "%s: Coming out of suspend...\n", dev->name);
 
        pci_set_power_state(pdev, PCI_D0);
-       pci_enable_device(pdev);
+       err = pci_enable_device(pdev);
+       if (err) {
+               printk(KERN_ERR "%s: pci_enable_device failed on resume\n",
+                      dev->name);
+               return err;
+       }
        pci_restore_state(pdev);
 
        /*
@@ -9060,6 +11885,16 @@ static int ipw_pci_resume(struct pci_dev *pdev)
 }
 #endif
 
+static void ipw_pci_shutdown(struct pci_dev *pdev)
+{
+       struct ipw_priv *priv = pci_get_drvdata(pdev);
+
+       /* Take down the device; powers it off, etc. */
+       ipw_down(priv);
+
+       pci_disable_device(pdev);
+}
+
 /* driver initialization stuff */
 static struct pci_driver ipw_driver = {
        .name = DRV_NAME,
@@ -9070,6 +11905,7 @@ static struct pci_driver ipw_driver = {
        .suspend = ipw_pci_suspend,
        .resume = ipw_pci_resume,
 #endif
+       .shutdown = ipw_pci_shutdown,
 };
 
 static int __init ipw_init(void)
@@ -9079,7 +11915,7 @@ static int __init ipw_init(void)
        printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
        printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
 
-       ret = pci_module_init(&ipw_driver);
+       ret = pci_register_driver(&ipw_driver);
        if (ret) {
                IPW_ERROR("Unable to initialize PCI module\n");
                return ret;
@@ -9111,7 +11947,7 @@ module_param(auto_create, int, 0444);
 MODULE_PARM_DESC(auto_create, "auto create adhoc network (default on)");
 
 module_param(led, int, 0444);
-MODULE_PARM_DESC(led, "enable led control on some systems (default 0 off)\n");
+MODULE_PARM_DESC(led, "enable led control on some systems (default 0 off)");
 
 module_param(debug, int, 0444);
 MODULE_PARM_DESC(debug, "debug output mask");
@@ -9119,7 +11955,29 @@ MODULE_PARM_DESC(debug, "debug output mask");
 module_param(channel, int, 0444);
 MODULE_PARM_DESC(channel, "channel to limit associate to (default 0 [ANY])");
 
-#ifdef CONFIG_IPW_MONITOR
+#ifdef CONFIG_IPW2200_PROMISCUOUS
+module_param(rtap_iface, int, 0444);
+MODULE_PARM_DESC(rtap_iface, "create the rtap interface (1 - create, default 0)");
+#endif
+
+#ifdef CONFIG_IPW2200_QOS
+module_param(qos_enable, int, 0444);
+MODULE_PARM_DESC(qos_enable, "enable all QoS functionalitis");
+
+module_param(qos_burst_enable, int, 0444);
+MODULE_PARM_DESC(qos_burst_enable, "enable QoS burst mode");
+
+module_param(qos_no_ack_mask, int, 0444);
+MODULE_PARM_DESC(qos_no_ack_mask, "mask Tx_Queue to no ack");
+
+module_param(burst_duration_CCK, int, 0444);
+MODULE_PARM_DESC(burst_duration_CCK, "set CCK burst value");
+
+module_param(burst_duration_OFDM, int, 0444);
+MODULE_PARM_DESC(burst_duration_OFDM, "set OFDM burst value");
+#endif                         /* CONFIG_IPW2200_QOS */
+
+#ifdef CONFIG_IPW2200_MONITOR
 module_param(mode, int, 0444);
 MODULE_PARM_DESC(mode, "network mode (0=BSS,1=IBSS,2=Monitor)");
 #else
@@ -9127,5 +11985,21 @@ module_param(mode, int, 0444);
 MODULE_PARM_DESC(mode, "network mode (0=BSS,1=IBSS)");
 #endif
 
+module_param(bt_coexist, int, 0444);
+MODULE_PARM_DESC(bt_coexist, "enable bluetooth coexistence (default off)");
+
+module_param(hwcrypto, int, 0444);
+MODULE_PARM_DESC(hwcrypto, "enable hardware crypto (default off)");
+
+module_param(cmdlog, int, 0444);
+MODULE_PARM_DESC(cmdlog,
+                "allocate a ring buffer for logging firmware commands");
+
+module_param(roaming, int, 0444);
+MODULE_PARM_DESC(roaming, "enable roaming support (default on)");
+
+module_param(antenna, int, 0444);
+MODULE_PARM_DESC(antenna, "select antenna 1=Main, 3=Aux, default 0 [both], 2=slow_diversity (choose the one with lower background noise)");
+
 module_exit(ipw_exit);
 module_init(ipw_init);