Merge branch 'for-next' into for-linus
[safe/jmp/linux-2.6] / drivers / net / wireless / zd1211rw / zd_mac.c
index 7845b6d..a22a192 100644 (file)
@@ -1,4 +1,9 @@
-/* zd_mac.c
+/* ZD1211 USB-WLAN driver for Linux
+ *
+ * Copyright (C) 2005-2007 Ulrich Kunitz <kune@deine-taler.de>
+ * Copyright (C) 2006-2007 Daniel Drake <dsd@gentoo.org>
+ * Copyright (C) 2006-2007 Michael Wu <flamingice@sourmilk.net>
+ * Copyright (C) 2007-2008 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -17,7 +22,6 @@
 
 #include <linux/netdevice.h>
 #include <linux/etherdevice.h>
-#include <linux/wireless.h>
 #include <linux/usb.h>
 #include <linux/jiffies.h>
 #include <net/ieee80211_radiotap.h>
 #include "zd_def.h"
 #include "zd_chip.h"
 #include "zd_mac.h"
-#include "zd_ieee80211.h"
-#include "zd_netdev.h"
 #include "zd_rf.h"
-#include "zd_util.h"
 
-static void ieee_init(struct ieee80211_device *ieee);
-static void softmac_init(struct ieee80211softmac_device *sm);
+struct zd_reg_alpha2_map {
+       u32 reg;
+       char alpha2[2];
+};
+
+static struct zd_reg_alpha2_map reg_alpha2_map[] = {
+       { ZD_REGDOMAIN_FCC, "US" },
+       { ZD_REGDOMAIN_IC, "CA" },
+       { ZD_REGDOMAIN_ETSI, "DE" }, /* Generic ETSI, use most restrictive */
+       { ZD_REGDOMAIN_JAPAN, "JP" },
+       { ZD_REGDOMAIN_JAPAN_ADD, "JP" },
+       { ZD_REGDOMAIN_SPAIN, "ES" },
+       { ZD_REGDOMAIN_FRANCE, "FR" },
+};
+
+/* This table contains the hardware specific values for the modulation rates. */
+static const struct ieee80211_rate zd_rates[] = {
+       { .bitrate = 10,
+         .hw_value = ZD_CCK_RATE_1M, },
+       { .bitrate = 20,
+         .hw_value = ZD_CCK_RATE_2M,
+         .hw_value_short = ZD_CCK_RATE_2M | ZD_CCK_PREA_SHORT,
+         .flags = IEEE80211_RATE_SHORT_PREAMBLE },
+       { .bitrate = 55,
+         .hw_value = ZD_CCK_RATE_5_5M,
+         .hw_value_short = ZD_CCK_RATE_5_5M | ZD_CCK_PREA_SHORT,
+         .flags = IEEE80211_RATE_SHORT_PREAMBLE },
+       { .bitrate = 110,
+         .hw_value = ZD_CCK_RATE_11M,
+         .hw_value_short = ZD_CCK_RATE_11M | ZD_CCK_PREA_SHORT,
+         .flags = IEEE80211_RATE_SHORT_PREAMBLE },
+       { .bitrate = 60,
+         .hw_value = ZD_OFDM_RATE_6M,
+         .flags = 0 },
+       { .bitrate = 90,
+         .hw_value = ZD_OFDM_RATE_9M,
+         .flags = 0 },
+       { .bitrate = 120,
+         .hw_value = ZD_OFDM_RATE_12M,
+         .flags = 0 },
+       { .bitrate = 180,
+         .hw_value = ZD_OFDM_RATE_18M,
+         .flags = 0 },
+       { .bitrate = 240,
+         .hw_value = ZD_OFDM_RATE_24M,
+         .flags = 0 },
+       { .bitrate = 360,
+         .hw_value = ZD_OFDM_RATE_36M,
+         .flags = 0 },
+       { .bitrate = 480,
+         .hw_value = ZD_OFDM_RATE_48M,
+         .flags = 0 },
+       { .bitrate = 540,
+         .hw_value = ZD_OFDM_RATE_54M,
+         .flags = 0 },
+};
+
+/*
+ * Zydas retry rates table. Each line is listed in the same order as
+ * in zd_rates[] and contains all the rate used when a packet is sent
+ * starting with a given rates. Let's consider an example :
+ *
+ * "11 Mbits : 4, 3, 2, 1, 0" means :
+ * - packet is sent using 4 different rates
+ * - 1st rate is index 3 (ie 11 Mbits)
+ * - 2nd rate is index 2 (ie 5.5 Mbits)
+ * - 3rd rate is index 1 (ie 2 Mbits)
+ * - 4th rate is index 0 (ie 1 Mbits)
+ */
+
+static const struct tx_retry_rate zd_retry_rates[] = {
+       { /*  1 Mbits */        1, { 0 }},
+       { /*  2 Mbits */        2, { 1,  0 }},
+       { /*  5.5 Mbits */      3, { 2,  1, 0 }},
+       { /* 11 Mbits */        4, { 3,  2, 1, 0 }},
+       { /*  6 Mbits */        5, { 4,  3, 2, 1, 0 }},
+       { /*  9 Mbits */        6, { 5,  4, 3, 2, 1, 0}},
+       { /* 12 Mbits */        5, { 6,  3, 2, 1, 0 }},
+       { /* 18 Mbits */        6, { 7,  6, 3, 2, 1, 0 }},
+       { /* 24 Mbits */        6, { 8,  6, 3, 2, 1, 0 }},
+       { /* 36 Mbits */        7, { 9,  8, 6, 3, 2, 1, 0 }},
+       { /* 48 Mbits */        8, {10,  9, 8, 6, 3, 2, 1, 0 }},
+       { /* 54 Mbits */        9, {11, 10, 9, 8, 6, 3, 2, 1, 0 }}
+};
+
+static const struct ieee80211_channel zd_channels[] = {
+       { .center_freq = 2412, .hw_value = 1 },
+       { .center_freq = 2417, .hw_value = 2 },
+       { .center_freq = 2422, .hw_value = 3 },
+       { .center_freq = 2427, .hw_value = 4 },
+       { .center_freq = 2432, .hw_value = 5 },
+       { .center_freq = 2437, .hw_value = 6 },
+       { .center_freq = 2442, .hw_value = 7 },
+       { .center_freq = 2447, .hw_value = 8 },
+       { .center_freq = 2452, .hw_value = 9 },
+       { .center_freq = 2457, .hw_value = 10 },
+       { .center_freq = 2462, .hw_value = 11 },
+       { .center_freq = 2467, .hw_value = 12 },
+       { .center_freq = 2472, .hw_value = 13 },
+       { .center_freq = 2484, .hw_value = 14 },
+};
 
 static void housekeeping_init(struct zd_mac *mac);
 static void housekeeping_enable(struct zd_mac *mac);
 static void housekeeping_disable(struct zd_mac *mac);
 
-int zd_mac_init(struct zd_mac *mac,
-               struct net_device *netdev,
-               struct usb_interface *intf)
+static int zd_reg2alpha2(u8 regdomain, char *alpha2)
 {
-       struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
-
-       memset(mac, 0, sizeof(*mac));
-       spin_lock_init(&mac->lock);
-       mac->netdev = netdev;
-
-       ieee_init(ieee);
-       softmac_init(ieee80211_priv(netdev));
-       zd_chip_init(&mac->chip, netdev, intf);
-       housekeeping_init(mac);
-       return 0;
+       unsigned int i;
+       struct zd_reg_alpha2_map *reg_map;
+       for (i = 0; i < ARRAY_SIZE(reg_alpha2_map); i++) {
+               reg_map = &reg_alpha2_map[i];
+               if (regdomain == reg_map->reg) {
+                       alpha2[0] = reg_map->alpha2[0];
+                       alpha2[1] = reg_map->alpha2[1];
+                       return 0;
+               }
+       }
+       return 1;
 }
 
-static int reset_channel(struct zd_mac *mac)
+int zd_mac_preinit_hw(struct ieee80211_hw *hw)
 {
        int r;
-       unsigned long flags;
-       const struct channel_range *range;
+       u8 addr[ETH_ALEN];
+       struct zd_mac *mac = zd_hw_mac(hw);
 
-       spin_lock_irqsave(&mac->lock, flags);
-       range = zd_channel_range(mac->regdomain);
-       if (!range->start) {
-               r = -EINVAL;
-               goto out;
-       }
-       mac->requested_channel = range->start;
-       r = 0;
-out:
-       spin_unlock_irqrestore(&mac->lock, flags);
-       return r;
+       r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
+       if (r)
+               return r;
+
+       SET_IEEE80211_PERM_ADDR(hw, addr);
+
+       return 0;
 }
 
-int zd_mac_init_hw(struct zd_mac *mac, u8 device_type)
+int zd_mac_init_hw(struct ieee80211_hw *hw)
 {
        int r;
+       struct zd_mac *mac = zd_hw_mac(hw);
        struct zd_chip *chip = &mac->chip;
-       u8 addr[ETH_ALEN];
+       char alpha2[2];
        u8 default_regdomain;
 
        r = zd_chip_enable_int(chip);
        if (r)
                goto out;
-       r = zd_chip_init_hw(chip, device_type);
+       r = zd_chip_init_hw(chip);
        if (r)
                goto disable_int;
 
-       zd_get_e2p_mac_addr(chip, addr);
-       r = zd_write_mac_addr(chip, addr);
-       if (r)
-               goto disable_int;
        ZD_ASSERT(!irqs_disabled());
-       spin_lock_irq(&mac->lock);
-       memcpy(mac->netdev->dev_addr, addr, ETH_ALEN);
-       spin_unlock_irq(&mac->lock);
 
        r = zd_read_regdomain(chip, &default_regdomain);
        if (r)
                goto disable_int;
-       if (!zd_regdomain_supported(default_regdomain)) {
-               dev_dbg_f(zd_mac_dev(mac),
-                         "Regulatory Domain %#04x is not supported.\n",
-                         default_regdomain);
-               r = -EINVAL;
-               goto disable_int;
-       }
        spin_lock_irq(&mac->lock);
        mac->regdomain = mac->default_regdomain = default_regdomain;
        spin_unlock_irq(&mac->lock);
-       r = reset_channel(mac);
-       if (r)
-               goto disable_int;
 
        /* We must inform the device that we are doing encryption/decryption in
         * software at the moment. */
@@ -119,11 +197,11 @@ int zd_mac_init_hw(struct zd_mac *mac, u8 device_type)
        if (r)
                goto disable_int;
 
-       r = zd_geo_init(zd_mac_to_ieee80211(mac), mac->regdomain);
+       r = zd_reg2alpha2(mac->regdomain, alpha2);
        if (r)
                goto disable_int;
 
-       r = 0;
+       r = regulatory_hint(hw->wiphy, alpha2);
 disable_int:
        zd_chip_disable_int(chip);
 out:
@@ -132,34 +210,45 @@ out:
 
 void zd_mac_clear(struct zd_mac *mac)
 {
+       flush_workqueue(zd_workqueue);
        zd_chip_clear(&mac->chip);
        ZD_ASSERT(!spin_is_locked(&mac->lock));
        ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
 }
 
-static int reset_mode(struct zd_mac *mac)
+static int set_rx_filter(struct zd_mac *mac)
 {
-       struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
-       struct zd_ioreq32 ioreqs[3] = {
-               { CR_RX_FILTER, STA_RX_FILTER },
-               { CR_SNIFFER_ON, 0U },
-       };
+       unsigned long flags;
+       u32 filter = STA_RX_FILTER;
 
-       if (ieee->iw_mode == IW_MODE_MONITOR) {
-               ioreqs[0].value = 0xffffffff;
-               ioreqs[1].value = 0x1;
-               ioreqs[2].value = ENC_SNIFFER;
-       }
+       spin_lock_irqsave(&mac->lock, flags);
+       if (mac->pass_ctrl)
+               filter |= RX_FILTER_CTRL;
+       spin_unlock_irqrestore(&mac->lock, flags);
 
-       return zd_iowrite32a(&mac->chip, ioreqs, 3);
+       return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
 }
 
-int zd_mac_open(struct net_device *netdev)
+static int set_mc_hash(struct zd_mac *mac)
 {
-       struct zd_mac *mac = zd_netdev_mac(netdev);
+       struct zd_mc_hash hash;
+       zd_mc_clear(&hash);
+       return zd_chip_set_multicast_hash(&mac->chip, &hash);
+}
+
+static int zd_op_start(struct ieee80211_hw *hw)
+{
+       struct zd_mac *mac = zd_hw_mac(hw);
        struct zd_chip *chip = &mac->chip;
+       struct zd_usb *usb = &chip->usb;
        int r;
 
+       if (!usb->initialized) {
+               r = zd_usb_init_hw(usb);
+               if (r)
+                       goto out;
+       }
+
        r = zd_chip_enable_int(chip);
        if (r < 0)
                goto out;
@@ -167,27 +256,26 @@ int zd_mac_open(struct net_device *netdev)
        r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
        if (r < 0)
                goto disable_int;
-       r = reset_mode(mac);
+       r = set_rx_filter(mac);
+       if (r)
+               goto disable_int;
+       r = set_mc_hash(mac);
        if (r)
                goto disable_int;
        r = zd_chip_switch_radio_on(chip);
        if (r < 0)
                goto disable_int;
-       r = zd_chip_set_channel(chip, mac->requested_channel);
-       if (r < 0)
-               goto disable_radio;
-       r = zd_chip_enable_rx(chip);
+       r = zd_chip_enable_rxtx(chip);
        if (r < 0)
                goto disable_radio;
        r = zd_chip_enable_hwint(chip);
        if (r < 0)
-               goto disable_rx;
+               goto disable_rxtx;
 
        housekeeping_enable(mac);
-       ieee80211softmac_start(netdev);
        return 0;
-disable_rx:
-       zd_chip_disable_rx(chip);
+disable_rxtx:
+       zd_chip_disable_rxtx(chip);
 disable_radio:
        zd_chip_switch_radio_off(chip);
 disable_int:
@@ -196,314 +284,246 @@ out:
        return r;
 }
 
-int zd_mac_stop(struct net_device *netdev)
+static void zd_op_stop(struct ieee80211_hw *hw)
 {
-       struct zd_mac *mac = zd_netdev_mac(netdev);
+       struct zd_mac *mac = zd_hw_mac(hw);
        struct zd_chip *chip = &mac->chip;
+       struct sk_buff *skb;
+       struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
 
-       netif_stop_queue(netdev);
-
-       /*
-        * The order here deliberately is a little different from the open()
+       /* The order here deliberately is a little different from the open()
         * method, since we need to make sure there is no opportunity for RX
-        * frames to be processed by softmac after we have stopped it.
+        * frames to be processed by mac80211 after we have stopped it.
         */
 
-       zd_chip_disable_rx(chip);
+       zd_chip_disable_rxtx(chip);
        housekeeping_disable(mac);
-       ieee80211softmac_stop(netdev);
+       flush_workqueue(zd_workqueue);
 
        zd_chip_disable_hwint(chip);
        zd_chip_switch_radio_off(chip);
        zd_chip_disable_int(chip);
 
-       return 0;
+
+       while ((skb = skb_dequeue(ack_wait_queue)))
+               dev_kfree_skb_any(skb);
 }
 
-int zd_mac_set_mac_address(struct net_device *netdev, void *p)
+/**
+ * zd_mac_tx_status - reports tx status of a packet if required
+ * @hw - a &struct ieee80211_hw pointer
+ * @skb - a sk-buffer
+ * @flags: extra flags to set in the TX status info
+ * @ackssi: ACK signal strength
+ * @success - True for successful transmission of the frame
+ *
+ * This information calls ieee80211_tx_status_irqsafe() if required by the
+ * control information. It copies the control information into the status
+ * information.
+ *
+ * If no status information has been requested, the skb is freed.
+ */
+static void zd_mac_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
+                     int ackssi, struct tx_status *tx_status)
 {
-       int r;
-       unsigned long flags;
-       struct sockaddr *addr = p;
-       struct zd_mac *mac = zd_netdev_mac(netdev);
-       struct zd_chip *chip = &mac->chip;
-
-       if (!is_valid_ether_addr(addr->sa_data))
-               return -EADDRNOTAVAIL;
+       struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
+       int i;
+       int success = 1, retry = 1;
+       int first_idx;
+       const struct tx_retry_rate *retries;
 
-       dev_dbg_f(zd_mac_dev(mac),
-                 "Setting MAC to " MAC_FMT "\n", MAC_ARG(addr->sa_data));
+       ieee80211_tx_info_clear_status(info);
 
-       r = zd_write_mac_addr(chip, addr->sa_data);
-       if (r)
-               return r;
+       if (tx_status) {
+               success = !tx_status->failure;
+               retry = tx_status->retry + success;
+       }
 
-       spin_lock_irqsave(&mac->lock, flags);
-       memcpy(netdev->dev_addr, addr->sa_data, ETH_ALEN);
-       spin_unlock_irqrestore(&mac->lock, flags);
+       if (success) {
+               /* success */
+               info->flags |= IEEE80211_TX_STAT_ACK;
+       } else {
+               /* failure */
+               info->flags &= ~IEEE80211_TX_STAT_ACK;
+       }
 
-       return 0;
-}
+       first_idx = info->status.rates[0].idx;
+       ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
+       retries = &zd_retry_rates[first_idx];
+       ZD_ASSERT(0<=retry && retry<=retries->count);
 
-int zd_mac_set_regdomain(struct zd_mac *mac, u8 regdomain)
-{
-       int r;
-       u8 channel;
+       info->status.rates[0].idx = retries->rate[0];
+       info->status.rates[0].count = 1; // (retry > 1 ? 2 : 1);
 
-       ZD_ASSERT(!irqs_disabled());
-       spin_lock_irq(&mac->lock);
-       if (regdomain == 0) {
-               regdomain = mac->default_regdomain;
+       for (i=1; i<IEEE80211_TX_MAX_RATES-1 && i<retry; i++) {
+               info->status.rates[i].idx = retries->rate[i];
+               info->status.rates[i].count = 1; // ((i==retry-1) && success ? 1:2);
        }
-       if (!zd_regdomain_supported(regdomain)) {
-               spin_unlock_irq(&mac->lock);
-               return -EINVAL;
-       }
-       mac->regdomain = regdomain;
-       channel = mac->requested_channel;
-       spin_unlock_irq(&mac->lock);
-
-       r = zd_geo_init(zd_mac_to_ieee80211(mac), regdomain);
-       if (r)
-               return r;
-       if (!zd_regdomain_supports_channel(regdomain, channel)) {
-               r = reset_channel(mac);
-               if (r)
-                       return r;
+       for (; i<IEEE80211_TX_MAX_RATES && i<retry; i++) {
+               info->status.rates[i].idx = retries->rate[retry-1];
+               info->status.rates[i].count = 1; // (success ? 1:2);
        }
+       if (i<IEEE80211_TX_MAX_RATES)
+               info->status.rates[i].idx = -1; /* terminate */
 
-       return 0;
+       info->status.ack_signal = ackssi;
+       ieee80211_tx_status_irqsafe(hw, skb);
 }
 
-u8 zd_mac_get_regdomain(struct zd_mac *mac)
+/**
+ * zd_mac_tx_failed - callback for failed frames
+ * @dev: the mac80211 wireless device
+ *
+ * This function is called if a frame couldn't be successfully
+ * transferred. The first frame from the tx queue, will be selected and
+ * reported as error to the upper layers.
+ */
+void zd_mac_tx_failed(struct urb *urb)
 {
+       struct ieee80211_hw * hw = zd_usb_to_hw(urb->context);
+       struct zd_mac *mac = zd_hw_mac(hw);
+       struct sk_buff_head *q = &mac->ack_wait_queue;
+       struct sk_buff *skb;
+       struct tx_status *tx_status = (struct tx_status *)urb->transfer_buffer;
        unsigned long flags;
-       u8 regdomain;
-
-       spin_lock_irqsave(&mac->lock, flags);
-       regdomain = mac->regdomain;
-       spin_unlock_irqrestore(&mac->lock, flags);
-       return regdomain;
-}
-
-static void set_channel(struct net_device *netdev, u8 channel)
-{
-       struct zd_mac *mac = zd_netdev_mac(netdev);
-
-       dev_dbg_f(zd_mac_dev(mac), "channel %d\n", channel);
-
-       zd_chip_set_channel(&mac->chip, channel);
-}
-
-/* TODO: Should not work in Managed mode. */
-int zd_mac_request_channel(struct zd_mac *mac, u8 channel)
-{
-       unsigned long lock_flags;
-       struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
-
-       if (ieee->iw_mode == IW_MODE_INFRA)
-               return -EPERM;
-
-       spin_lock_irqsave(&mac->lock, lock_flags);
-       if (!zd_regdomain_supports_channel(mac->regdomain, channel)) {
-               spin_unlock_irqrestore(&mac->lock, lock_flags);
-               return -EINVAL;
-       }
-       mac->requested_channel = channel;
-       spin_unlock_irqrestore(&mac->lock, lock_flags);
-       if (netif_running(mac->netdev))
-               return zd_chip_set_channel(&mac->chip, channel);
-       else
-               return 0;
-}
+       int success = !tx_status->failure;
+       int retry = tx_status->retry + success;
+       int found = 0;
+       int i, position = 0;
+
+       q = &mac->ack_wait_queue;
+       spin_lock_irqsave(&q->lock, flags);
+
+       skb_queue_walk(q, skb) {
+               struct ieee80211_hdr *tx_hdr;
+               struct ieee80211_tx_info *info;
+               int first_idx, final_idx;
+               const struct tx_retry_rate *retries;
+               u8 final_rate;
+
+               position ++;
+
+               /* if the hardware reports a failure and we had a 802.11 ACK
+                * pending, then we skip the first skb when searching for a
+                * matching frame */
+               if (tx_status->failure && mac->ack_pending &&
+                   skb_queue_is_first(q, skb)) {
+                       continue;
+               }
 
-u8 zd_mac_get_channel(struct zd_mac *mac)
-{
-       u8 channel = zd_chip_get_channel(&mac->chip);
+               tx_hdr = (struct ieee80211_hdr *)skb->data;
 
-       dev_dbg_f(zd_mac_dev(mac), "channel %u\n", channel);
-       return channel;
-}
+               /* we skip all frames not matching the reported destination */
+               if (unlikely(memcmp(tx_hdr->addr1, tx_status->mac, ETH_ALEN))) {
+                       continue;
+               }
 
-/* If wrong rate is given, we are falling back to the slowest rate: 1MBit/s */
-static u8 cs_typed_rate(u8 cs_rate)
-{
-       static const u8 typed_rates[16] = {
-               [ZD_CS_CCK_RATE_1M]     = ZD_CS_CCK|ZD_CS_CCK_RATE_1M,
-               [ZD_CS_CCK_RATE_2M]     = ZD_CS_CCK|ZD_CS_CCK_RATE_2M,
-               [ZD_CS_CCK_RATE_5_5M]   = ZD_CS_CCK|ZD_CS_CCK_RATE_5_5M,
-               [ZD_CS_CCK_RATE_11M]    = ZD_CS_CCK|ZD_CS_CCK_RATE_11M,
-               [ZD_OFDM_RATE_6M]       = ZD_CS_OFDM|ZD_OFDM_RATE_6M,
-               [ZD_OFDM_RATE_9M]       = ZD_CS_OFDM|ZD_OFDM_RATE_9M,
-               [ZD_OFDM_RATE_12M]      = ZD_CS_OFDM|ZD_OFDM_RATE_12M,
-               [ZD_OFDM_RATE_18M]      = ZD_CS_OFDM|ZD_OFDM_RATE_18M,
-               [ZD_OFDM_RATE_24M]      = ZD_CS_OFDM|ZD_OFDM_RATE_24M,
-               [ZD_OFDM_RATE_36M]      = ZD_CS_OFDM|ZD_OFDM_RATE_36M,
-               [ZD_OFDM_RATE_48M]      = ZD_CS_OFDM|ZD_OFDM_RATE_48M,
-               [ZD_OFDM_RATE_54M]      = ZD_CS_OFDM|ZD_OFDM_RATE_54M,
-       };
+               /* we skip all frames not matching the reported final rate */
 
-       ZD_ASSERT(ZD_CS_RATE_MASK == 0x0f);
-       return typed_rates[cs_rate & ZD_CS_RATE_MASK];
-}
+               info = IEEE80211_SKB_CB(skb);
+               first_idx = info->status.rates[0].idx;
+               ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
+               retries = &zd_retry_rates[first_idx];
+               if (retry < 0 || retry > retries->count) {
+                       continue;
+               }
 
-/* Fallback to lowest rate, if rate is unknown. */
-static u8 rate_to_cs_rate(u8 rate)
-{
-       switch (rate) {
-       case IEEE80211_CCK_RATE_2MB:
-               return ZD_CS_CCK_RATE_2M;
-       case IEEE80211_CCK_RATE_5MB:
-               return ZD_CS_CCK_RATE_5_5M;
-       case IEEE80211_CCK_RATE_11MB:
-               return ZD_CS_CCK_RATE_11M;
-       case IEEE80211_OFDM_RATE_6MB:
-               return ZD_OFDM_RATE_6M;
-       case IEEE80211_OFDM_RATE_9MB:
-               return ZD_OFDM_RATE_9M;
-       case IEEE80211_OFDM_RATE_12MB:
-               return ZD_OFDM_RATE_12M;
-       case IEEE80211_OFDM_RATE_18MB:
-               return ZD_OFDM_RATE_18M;
-       case IEEE80211_OFDM_RATE_24MB:
-               return ZD_OFDM_RATE_24M;
-       case IEEE80211_OFDM_RATE_36MB:
-               return ZD_OFDM_RATE_36M;
-       case IEEE80211_OFDM_RATE_48MB:
-               return ZD_OFDM_RATE_48M;
-       case IEEE80211_OFDM_RATE_54MB:
-               return ZD_OFDM_RATE_54M;
-       }
-       return ZD_CS_CCK_RATE_1M;
-}
+               ZD_ASSERT(0<=retry && retry<=retries->count);
+               final_idx = retries->rate[retry-1];
+               final_rate = zd_rates[final_idx].hw_value;
 
-int zd_mac_set_mode(struct zd_mac *mac, u32 mode)
-{
-       struct ieee80211_device *ieee;
+               if (final_rate != tx_status->rate) {
+                       continue;
+               }
 
-       switch (mode) {
-       case IW_MODE_AUTO:
-       case IW_MODE_ADHOC:
-       case IW_MODE_INFRA:
-               mac->netdev->type = ARPHRD_ETHER;
-               break;
-       case IW_MODE_MONITOR:
-               mac->netdev->type = ARPHRD_IEEE80211_RADIOTAP;
+               found = 1;
                break;
-       default:
-               dev_dbg_f(zd_mac_dev(mac), "wrong mode %u\n", mode);
-               return -EINVAL;
        }
 
-       ieee = zd_mac_to_ieee80211(mac);
-       ZD_ASSERT(!irqs_disabled());
-       spin_lock_irq(&ieee->lock);
-       ieee->iw_mode = mode;
-       spin_unlock_irq(&ieee->lock);
-
-       if (netif_running(mac->netdev))
-               return reset_mode(mac);
-
-       return 0;
-}
-
-int zd_mac_get_mode(struct zd_mac *mac, u32 *mode)
-{
-       unsigned long flags;
-       struct ieee80211_device *ieee;
+       if (found) {
+               for (i=1; i<=position; i++) {
+                       skb = __skb_dequeue(q);
+                       zd_mac_tx_status(hw, skb,
+                                        mac->ack_pending ? mac->ack_signal : 0,
+                                        i == position ? tx_status : NULL);
+                       mac->ack_pending = 0;
+               }
+       }
 
-       ieee = zd_mac_to_ieee80211(mac);
-       spin_lock_irqsave(&ieee->lock, flags);
-       *mode = ieee->iw_mode;
-       spin_unlock_irqrestore(&ieee->lock, flags);
-       return 0;
+       spin_unlock_irqrestore(&q->lock, flags);
 }
 
-int zd_mac_get_range(struct zd_mac *mac, struct iw_range *range)
+/**
+ * zd_mac_tx_to_dev - callback for USB layer
+ * @skb: a &sk_buff pointer
+ * @error: error value, 0 if transmission successful
+ *
+ * Informs the MAC layer that the frame has successfully transferred to the
+ * device. If an ACK is required and the transfer to the device has been
+ * successful, the packets are put on the @ack_wait_queue with
+ * the control set removed.
+ */
+void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
 {
-       int i;
-       const struct channel_range *channel_range;
-       u8 regdomain;
-
-       memset(range, 0, sizeof(*range));
-
-       /* FIXME: Not so important and depends on the mode. For 802.11g
-        * usually this value is used. It seems to be that Bit/s number is
-        * given here.
-        */
-       range->throughput = 27 * 1000 * 1000;
-
-       range->max_qual.qual = 100;
-       range->max_qual.level = 100;
-
-       /* FIXME: Needs still to be tuned. */
-       range->avg_qual.qual = 71;
-       range->avg_qual.level = 80;
-
-       /* FIXME: depends on standard? */
-       range->min_rts = 256;
-       range->max_rts = 2346;
-
-       range->min_frag = MIN_FRAG_THRESHOLD;
-       range->max_frag = MAX_FRAG_THRESHOLD;
-
-       range->max_encoding_tokens = WEP_KEYS;
-       range->num_encoding_sizes = 2;
-       range->encoding_size[0] = 5;
-       range->encoding_size[1] = WEP_KEY_LEN;
-
-       range->we_version_compiled = WIRELESS_EXT;
-       range->we_version_source = 20;
-
-       ZD_ASSERT(!irqs_disabled());
-       spin_lock_irq(&mac->lock);
-       regdomain = mac->regdomain;
-       spin_unlock_irq(&mac->lock);
-       channel_range = zd_channel_range(regdomain);
-
-       range->num_channels = channel_range->end - channel_range->start;
-       range->old_num_channels = range->num_channels;
-       range->num_frequency = range->num_channels;
-       range->old_num_frequency = range->num_frequency;
-
-       for (i = 0; i < range->num_frequency; i++) {
-               struct iw_freq *freq = &range->freq[i];
-               freq->i = channel_range->start + i;
-               zd_channel_to_freq(freq, freq->i);
+       struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
+       struct ieee80211_hw *hw = info->rate_driver_data[0];
+       struct zd_mac *mac = zd_hw_mac(hw);
+
+       ieee80211_tx_info_clear_status(info);
+
+       skb_pull(skb, sizeof(struct zd_ctrlset));
+       if (unlikely(error ||
+           (info->flags & IEEE80211_TX_CTL_NO_ACK))) {
+               /*
+                * FIXME : do we need to fill in anything ?
+                */
+               ieee80211_tx_status_irqsafe(hw, skb);
+       } else {
+               struct sk_buff_head *q = &mac->ack_wait_queue;
+
+               skb_queue_tail(q, skb);
+               while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS) {
+                       zd_mac_tx_status(hw, skb_dequeue(q),
+                                        mac->ack_pending ? mac->ack_signal : 0,
+                                        NULL);
+                       mac->ack_pending = 0;
+               }
        }
-
-       return 0;
 }
 
-static int zd_calc_tx_length_us(u8 *service, u8 cs_rate, u16 tx_length)
+static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
 {
+       /* ZD_PURE_RATE() must be used to remove the modulation type flag of
+        * the zd-rate values.
+        */
        static const u8 rate_divisor[] = {
-               [ZD_CS_CCK_RATE_1M]     =  1,
-               [ZD_CS_CCK_RATE_2M]     =  2,
-               [ZD_CS_CCK_RATE_5_5M]   = 11, /* bits must be doubled */
-               [ZD_CS_CCK_RATE_11M]    = 11,
-               [ZD_OFDM_RATE_6M]       =  6,
-               [ZD_OFDM_RATE_9M]       =  9,
-               [ZD_OFDM_RATE_12M]      = 12,
-               [ZD_OFDM_RATE_18M]      = 18,
-               [ZD_OFDM_RATE_24M]      = 24,
-               [ZD_OFDM_RATE_36M]      = 36,
-               [ZD_OFDM_RATE_48M]      = 48,
-               [ZD_OFDM_RATE_54M]      = 54,
+               [ZD_PURE_RATE(ZD_CCK_RATE_1M)]   =  1,
+               [ZD_PURE_RATE(ZD_CCK_RATE_2M)]   =  2,
+               /* Bits must be doubled. */
+               [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
+               [ZD_PURE_RATE(ZD_CCK_RATE_11M)]  = 11,
+               [ZD_PURE_RATE(ZD_OFDM_RATE_6M)]  =  6,
+               [ZD_PURE_RATE(ZD_OFDM_RATE_9M)]  =  9,
+               [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
+               [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
+               [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
+               [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
+               [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
+               [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
        };
 
        u32 bits = (u32)tx_length * 8;
        u32 divisor;
 
-       divisor = rate_divisor[cs_rate];
+       divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
        if (divisor == 0)
                return -EINVAL;
 
-       switch (cs_rate) {
-       case ZD_CS_CCK_RATE_5_5M:
+       switch (zd_rate) {
+       case ZD_CCK_RATE_5_5M:
                bits = (2*bits) + 10; /* round up to the next integer */
                break;
-       case ZD_CS_CCK_RATE_11M:
+       case ZD_CCK_RATE_11M:
                if (service) {
                        u32 t = bits % 11;
                        *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
@@ -518,137 +538,132 @@ static int zd_calc_tx_length_us(u8 *service, u8 cs_rate, u16 tx_length)
        return bits/divisor;
 }
 
-enum {
-       R2M_SHORT_PREAMBLE = 0x01,
-       R2M_11A            = 0x02,
-};
-
-static u8 cs_rate_to_modulation(u8 cs_rate, int flags)
-{
-       u8 modulation;
-
-       modulation = cs_typed_rate(cs_rate);
-       if (flags & R2M_SHORT_PREAMBLE) {
-               switch (ZD_CS_RATE(modulation)) {
-               case ZD_CS_CCK_RATE_2M:
-               case ZD_CS_CCK_RATE_5_5M:
-               case ZD_CS_CCK_RATE_11M:
-                       modulation |= ZD_CS_CCK_PREA_SHORT;
-                       return modulation;
-               }
-       }
-       if (flags & R2M_11A) {
-               if (ZD_CS_TYPE(modulation) == ZD_CS_OFDM)
-                       modulation |= ZD_CS_OFDM_MODE_11A;
-       }
-       return modulation;
-}
-
-static void cs_set_modulation(struct zd_mac *mac, struct zd_ctrlset *cs,
-                             struct ieee80211_hdr_4addr *hdr)
-{
-       struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
-       u16 ftype = WLAN_FC_GET_TYPE(le16_to_cpu(hdr->frame_ctl));
-       u8 rate, cs_rate;
-       int is_mgt = (ftype == IEEE80211_FTYPE_MGMT) != 0;
-
-       /* FIXME: 802.11a? short preamble? */
-       rate = ieee80211softmac_suggest_txrate(softmac,
-               is_multicast_ether_addr(hdr->addr1), is_mgt);
-
-       cs_rate = rate_to_cs_rate(rate);
-       cs->modulation = cs_rate_to_modulation(cs_rate, 0);
-}
-
 static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
-                          struct ieee80211_hdr_4addr *header)
+                          struct ieee80211_hdr *header,
+                          struct ieee80211_tx_info *info)
 {
-       unsigned int tx_length = le16_to_cpu(cs->tx_length);
-       u16 fctl = le16_to_cpu(header->frame_ctl);
-       u16 ftype = WLAN_FC_GET_TYPE(fctl);
-       u16 stype = WLAN_FC_GET_STYPE(fctl);
-
        /*
-        * CONTROL:
-        * - start at 0x00
-        * - if fragment 0, enable bit 0
+        * CONTROL TODO:
         * - if backoff needed, enable bit 0
         * - if burst (backoff not needed) disable bit 0
-        * - if multicast, enable bit 1
-        * - if PS-POLL frame, enable bit 2
-        * - if in INDEPENDENT_BSS mode and zd1205_DestPowerSave, then enable
-        *   bit 4 (FIXME: wtf)
-        * - if frag_len > RTS threshold, set bit 5 as long if it isnt
-        *   multicast or mgt
-        * - if bit 5 is set, and we are in OFDM mode, unset bit 5 and set bit
-        *   7
         */
 
        cs->control = 0;
 
        /* First fragment */
-       if (WLAN_GET_SEQ_FRAG(le16_to_cpu(header->seq_ctl)) == 0)
+       if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
                cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
 
-       /* Multicast */
-       if (is_multicast_ether_addr(header->addr1))
-               cs->control |= ZD_CS_MULTICAST;
+       /* No ACK expected (multicast, etc.) */
+       if (info->flags & IEEE80211_TX_CTL_NO_ACK)
+               cs->control |= ZD_CS_NO_ACK;
 
        /* PS-POLL */
-       if (stype == IEEE80211_STYPE_PSPOLL)
+       if (ieee80211_is_pspoll(header->frame_control))
                cs->control |= ZD_CS_PS_POLL_FRAME;
 
-       if (!is_multicast_ether_addr(header->addr1) &&
-           ftype != IEEE80211_FTYPE_MGMT &&
-           tx_length > zd_netdev_ieee80211(mac->netdev)->rts)
-       {
-               /* FIXME: check the logic */
-               if (ZD_CS_TYPE(cs->modulation) == ZD_CS_OFDM) {
-                       /* 802.11g */
-                       cs->control |= ZD_CS_SELF_CTS;
-               } else { /* 802.11b */
-                       cs->control |= ZD_CS_RTS;
+       if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
+               cs->control |= ZD_CS_RTS;
+
+       if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
+               cs->control |= ZD_CS_SELF_CTS;
+
+       /* FIXME: Management frame? */
+}
+
+static int zd_mac_config_beacon(struct ieee80211_hw *hw, struct sk_buff *beacon)
+{
+       struct zd_mac *mac = zd_hw_mac(hw);
+       int r;
+       u32 tmp, j = 0;
+       /* 4 more bytes for tail CRC */
+       u32 full_len = beacon->len + 4;
+
+       r = zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 0);
+       if (r < 0)
+               return r;
+       r = zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp);
+       if (r < 0)
+               return r;
+
+       while (tmp & 0x2) {
+               r = zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp);
+               if (r < 0)
+                       return r;
+               if ((++j % 100) == 0) {
+                       printk(KERN_ERR "CR_BCN_FIFO_SEMAPHORE not ready\n");
+                       if (j >= 500)  {
+                               printk(KERN_ERR "Giving up beacon config.\n");
+                               return -ETIMEDOUT;
+                       }
                }
+               msleep(1);
        }
 
-       /* FIXME: Management frame? */
+       r = zd_iowrite32(&mac->chip, CR_BCN_FIFO, full_len - 1);
+       if (r < 0)
+               return r;
+       if (zd_chip_is_zd1211b(&mac->chip)) {
+               r = zd_iowrite32(&mac->chip, CR_BCN_LENGTH, full_len - 1);
+               if (r < 0)
+                       return r;
+       }
+
+       for (j = 0 ; j < beacon->len; j++) {
+               r = zd_iowrite32(&mac->chip, CR_BCN_FIFO,
+                               *((u8 *)(beacon->data + j)));
+               if (r < 0)
+                       return r;
+       }
+
+       for (j = 0; j < 4; j++) {
+               r = zd_iowrite32(&mac->chip, CR_BCN_FIFO, 0x0);
+               if (r < 0)
+                       return r;
+       }
+
+       r = zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 1);
+       if (r < 0)
+               return r;
+
+       /* 802.11b/g 2.4G CCK 1Mb
+        * 802.11a, not yet implemented, uses different values (see GPL vendor
+        * driver)
+        */
+       return zd_iowrite32(&mac->chip, CR_BCN_PLCP_CFG, 0x00000400 |
+                       (full_len << 19));
 }
 
 static int fill_ctrlset(struct zd_mac *mac,
-                       struct ieee80211_txb *txb,
-                       int frag_num)
+                       struct sk_buff *skb)
 {
        int r;
-       struct sk_buff *skb = txb->fragments[frag_num];
-       struct ieee80211_hdr_4addr *hdr =
-               (struct ieee80211_hdr_4addr *) skb->data;
-       unsigned int frag_len = skb->len + IEEE80211_FCS_LEN;
-       unsigned int next_frag_len;
+       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
+       unsigned int frag_len = skb->len + FCS_LEN;
        unsigned int packet_length;
+       struct ieee80211_rate *txrate;
        struct zd_ctrlset *cs = (struct zd_ctrlset *)
                skb_push(skb, sizeof(struct zd_ctrlset));
+       struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
 
-       if (frag_num+1  < txb->nr_frags) {
-               next_frag_len = txb->fragments[frag_num+1]->len +
-                               IEEE80211_FCS_LEN;
-       } else {
-               next_frag_len = 0;
-       }
        ZD_ASSERT(frag_len <= 0xffff);
-       ZD_ASSERT(next_frag_len <= 0xffff);
 
-       cs_set_modulation(mac, cs, hdr);
+       txrate = ieee80211_get_tx_rate(mac->hw, info);
+
+       cs->modulation = txrate->hw_value;
+       if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
+               cs->modulation = txrate->hw_value_short;
 
        cs->tx_length = cpu_to_le16(frag_len);
 
-       cs_set_control(mac, cs, hdr);
+       cs_set_control(mac, cs, hdr, info);
 
        packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
        ZD_ASSERT(packet_length <= 0xffff);
        /* ZD1211B: Computing the length difference this way, gives us
         * flexibility to compute the packet length.
         */
-       cs->packet_length = cpu_to_le16(mac->chip.is_zd1211b ?
+       cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
                        packet_length - frag_len : packet_length);
 
        /*
@@ -665,438 +680,516 @@ static int fill_ctrlset(struct zd_mac *mac,
         * - see line 53 of zdinlinef.h
         */
        cs->service = 0;
-       r = zd_calc_tx_length_us(&cs->service, ZD_CS_RATE(cs->modulation),
+       r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
                                 le16_to_cpu(cs->tx_length));
        if (r < 0)
                return r;
        cs->current_length = cpu_to_le16(r);
-
-       if (next_frag_len == 0) {
-               cs->next_frame_length = 0;
-       } else {
-               r = zd_calc_tx_length_us(NULL, ZD_CS_RATE(cs->modulation),
-                                        next_frag_len);
-               if (r < 0)
-                       return r;
-               cs->next_frame_length = cpu_to_le16(r);
-       }
+       cs->next_frame_length = 0;
 
        return 0;
 }
 
-static int zd_mac_tx(struct zd_mac *mac, struct ieee80211_txb *txb, int pri)
+/**
+ * zd_op_tx - transmits a network frame to the device
+ *
+ * @dev: mac80211 hardware device
+ * @skb: socket buffer
+ * @control: the control structure
+ *
+ * This function transmit an IEEE 802.11 network frame to the device. The
+ * control block of the skbuff will be initialized. If necessary the incoming
+ * mac80211 queues will be stopped.
+ */
+static int zd_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
 {
-       int i, r;
-
-       for (i = 0; i < txb->nr_frags; i++) {
-               struct sk_buff *skb = txb->fragments[i];
+       struct zd_mac *mac = zd_hw_mac(hw);
+       struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
+       int r;
 
-               r = fill_ctrlset(mac, txb, i);
-               if (r)
-                       return r;
-               r = zd_usb_tx(&mac->chip.usb, skb->data, skb->len);
-               if (r)
-                       return r;
-       }
+       r = fill_ctrlset(mac, skb);
+       if (r)
+               goto fail;
 
-       /* FIXME: shouldn't this be handled by the upper layers? */
-       mac->netdev->trans_start = jiffies;
+       info->rate_driver_data[0] = hw;
 
-       ieee80211_txb_free(txb);
+       r = zd_usb_tx(&mac->chip.usb, skb);
+       if (r)
+               goto fail;
        return 0;
-}
 
-struct zd_rt_hdr {
-       struct ieee80211_radiotap_header rt_hdr;
-       u8  rt_flags;
-       u8  rt_rate;
-       u16 rt_channel;
-       u16 rt_chbitmask;
-} __attribute__((packed));
-
-static void fill_rt_header(void *buffer, struct zd_mac *mac,
-                          const struct ieee80211_rx_stats *stats,
-                          const struct rx_status *status)
-{
-       struct zd_rt_hdr *hdr = buffer;
-
-       hdr->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
-       hdr->rt_hdr.it_pad = 0;
-       hdr->rt_hdr.it_len = cpu_to_le16(sizeof(struct zd_rt_hdr));
-       hdr->rt_hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
-                                (1 << IEEE80211_RADIOTAP_CHANNEL) |
-                                (1 << IEEE80211_RADIOTAP_RATE));
-
-       hdr->rt_flags = 0;
-       if (status->decryption_type & (ZD_RX_WEP64|ZD_RX_WEP128|ZD_RX_WEP256))
-               hdr->rt_flags |= IEEE80211_RADIOTAP_F_WEP;
-
-       hdr->rt_rate = stats->rate / 5;
-
-       /* FIXME: 802.11a */
-       hdr->rt_channel = cpu_to_le16(ieee80211chan2mhz(
-                                            _zd_chip_get_channel(&mac->chip)));
-       hdr->rt_chbitmask = cpu_to_le16(IEEE80211_CHAN_2GHZ |
-               ((status->frame_status & ZD_RX_FRAME_MODULATION_MASK) ==
-               ZD_RX_OFDM ? IEEE80211_CHAN_OFDM : IEEE80211_CHAN_CCK));
+fail:
+       dev_kfree_skb(skb);
+       return 0;
 }
 
-/* Returns 1 if the data packet is for us and 0 otherwise. */
-static int is_data_packet_for_us(struct ieee80211_device *ieee,
-                                struct ieee80211_hdr_4addr *hdr)
+/**
+ * filter_ack - filters incoming packets for acknowledgements
+ * @dev: the mac80211 device
+ * @rx_hdr: received header
+ * @stats: the status for the received packet
+ *
+ * This functions looks for ACK packets and tries to match them with the
+ * frames in the tx queue. If a match is found the frame will be dequeued and
+ * the upper layers is informed about the successful transmission. If
+ * mac80211 queues have been stopped and the number of frames still to be
+ * transmitted is low the queues will be opened again.
+ *
+ * Returns 1 if the frame was an ACK, 0 if it was ignored.
+ */
+static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
+                     struct ieee80211_rx_status *stats)
 {
-       struct net_device *netdev = ieee->dev;
-       u16 fc = le16_to_cpu(hdr->frame_ctl);
-
-       ZD_ASSERT(WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA);
+       struct zd_mac *mac = zd_hw_mac(hw);
+       struct sk_buff *skb;
+       struct sk_buff_head *q;
+       unsigned long flags;
+       int found = 0;
+       int i, position = 0;
 
-       switch (ieee->iw_mode) {
-       case IW_MODE_ADHOC:
-               if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) != 0 ||
-                   memcmp(hdr->addr3, ieee->bssid, ETH_ALEN) != 0)
-                       return 0;
-               break;
-       case IW_MODE_AUTO:
-       case IW_MODE_INFRA:
-               if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) !=
-                   IEEE80211_FCTL_FROMDS ||
-                   memcmp(hdr->addr2, ieee->bssid, ETH_ALEN) != 0)
-                       return 0;
-               break;
-       default:
-               ZD_ASSERT(ieee->iw_mode != IW_MODE_MONITOR);
+       if (!ieee80211_is_ack(rx_hdr->frame_control))
                return 0;
-       }
 
-       return memcmp(hdr->addr1, netdev->dev_addr, ETH_ALEN) == 0 ||
-              is_multicast_ether_addr(hdr->addr1) ||
-              (netdev->flags & IFF_PROMISC);
-}
+       q = &mac->ack_wait_queue;
+       spin_lock_irqsave(&q->lock, flags);
+       skb_queue_walk(q, skb) {
+               struct ieee80211_hdr *tx_hdr;
 
-/* Filters receiving packets. If it returns 1 send it to ieee80211_rx, if 0
- * return. If an error is detected -EINVAL is returned. ieee80211_rx_mgt() is
- * called here.
- *
- * It has been based on ieee80211_rx_any.
- */
-static int filter_rx(struct ieee80211_device *ieee,
-                    const u8 *buffer, unsigned int length,
-                    struct ieee80211_rx_stats *stats)
-{
-       struct ieee80211_hdr_4addr *hdr;
-       u16 fc;
+               position ++;
 
-       if (ieee->iw_mode == IW_MODE_MONITOR)
-               return 1;
+               if (mac->ack_pending && skb_queue_is_first(q, skb))
+                   continue;
 
-       hdr = (struct ieee80211_hdr_4addr *)buffer;
-       fc = le16_to_cpu(hdr->frame_ctl);
-       if ((fc & IEEE80211_FCTL_VERS) != 0)
-               return -EINVAL;
+               tx_hdr = (struct ieee80211_hdr *)skb->data;
+               if (likely(!memcmp(tx_hdr->addr2, rx_hdr->addr1, ETH_ALEN)))
+               {
+                       found = 1;
+                       break;
+               }
+       }
 
-       switch (WLAN_FC_GET_TYPE(fc)) {
-       case IEEE80211_FTYPE_MGMT:
-               if (length < sizeof(struct ieee80211_hdr_3addr))
-                       return -EINVAL;
-               ieee80211_rx_mgt(ieee, hdr, stats);
-               return 0;
-       case IEEE80211_FTYPE_CTL:
-               /* Ignore invalid short buffers */
-               return 0;
-       case IEEE80211_FTYPE_DATA:
-               if (length < sizeof(struct ieee80211_hdr_3addr))
-                       return -EINVAL;
-               return is_data_packet_for_us(ieee, hdr);
+       if (found) {
+               for (i=1; i<position; i++) {
+                       skb = __skb_dequeue(q);
+                       zd_mac_tx_status(hw, skb,
+                                        mac->ack_pending ? mac->ack_signal : 0,
+                                        NULL);
+                       mac->ack_pending = 0;
+               }
+
+               mac->ack_pending = 1;
+               mac->ack_signal = stats->signal;
        }
 
-       return -EINVAL;
+       spin_unlock_irqrestore(&q->lock, flags);
+       return 1;
 }
 
-static void update_qual_rssi(struct zd_mac *mac,
-                            const u8 *buffer, unsigned int length,
-                            u8 qual_percent, u8 rssi_percent)
+int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
 {
-       unsigned long flags;
-       struct ieee80211_hdr_3addr *hdr;
+       struct zd_mac *mac = zd_hw_mac(hw);
+       struct ieee80211_rx_status stats;
+       const struct rx_status *status;
+       struct sk_buff *skb;
+       int bad_frame = 0;
+       __le16 fc;
+       int need_padding;
        int i;
+       u8 rate;
 
-       hdr = (struct ieee80211_hdr_3addr *)buffer;
-       if (length < offsetof(struct ieee80211_hdr_3addr, addr3))
-               return;
-       if (memcmp(hdr->addr2, zd_mac_to_ieee80211(mac)->bssid, ETH_ALEN) != 0)
-               return;
+       if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
+                    FCS_LEN + sizeof(struct rx_status))
+               return -EINVAL;
 
-       spin_lock_irqsave(&mac->lock, flags);
-       i = mac->stats_count % ZD_MAC_STATS_BUFFER_SIZE;
-       mac->qual_buffer[i] = qual_percent;
-       mac->rssi_buffer[i] = rssi_percent;
-       mac->stats_count++;
-       spin_unlock_irqrestore(&mac->lock, flags);
-}
+       memset(&stats, 0, sizeof(stats));
 
-static int fill_rx_stats(struct ieee80211_rx_stats *stats,
-                        const struct rx_status **pstatus,
-                        struct zd_mac *mac,
-                        const u8 *buffer, unsigned int length)
-{
-       const struct rx_status *status;
+       /* Note about pass_failed_fcs and pass_ctrl access below:
+        * mac locking intentionally omitted here, as this is the only unlocked
+        * reader and the only writer is configure_filter. Plus, if there were
+        * any races accessing these variables, it wouldn't really matter.
+        * If mac80211 ever provides a way for us to access filter flags
+        * from outside configure_filter, we could improve on this. Also, this
+        * situation may change once we implement some kind of DMA-into-skb
+        * RX path. */
 
-       *pstatus = status = zd_tail(buffer, length, sizeof(struct rx_status));
+       /* Caller has to ensure that length >= sizeof(struct rx_status). */
+       status = (struct rx_status *)
+               (buffer + (length - sizeof(struct rx_status)));
        if (status->frame_status & ZD_RX_ERROR) {
-               /* FIXME: update? */
-               return -EINVAL;
+               if (mac->pass_failed_fcs &&
+                               (status->frame_status & ZD_RX_CRC32_ERROR)) {
+                       stats.flag |= RX_FLAG_FAILED_FCS_CRC;
+                       bad_frame = 1;
+               } else {
+                       return -EINVAL;
+               }
        }
-       memset(stats, 0, sizeof(struct ieee80211_rx_stats));
-       stats->len = length - (ZD_PLCP_HEADER_SIZE + IEEE80211_FCS_LEN +
-                              + sizeof(struct rx_status));
-       /* FIXME: 802.11a */
-       stats->freq = IEEE80211_24GHZ_BAND;
-       stats->received_channel = _zd_chip_get_channel(&mac->chip);
-       stats->rssi = zd_rx_strength_percent(status->signal_strength);
-       stats->signal = zd_rx_qual_percent(buffer,
-                                         length - sizeof(struct rx_status),
-                                         status);
-       stats->mask = IEEE80211_STATMASK_RSSI | IEEE80211_STATMASK_SIGNAL;
-       stats->rate = zd_rx_rate(buffer, status);
-       if (stats->rate)
-               stats->mask |= IEEE80211_STATMASK_RATE;
-
-       return 0;
-}
 
-int zd_mac_rx(struct zd_mac *mac, const u8 *buffer, unsigned int length)
-{
-       int r;
-       struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
-       struct ieee80211_rx_stats stats;
-       const struct rx_status *status;
-       struct sk_buff *skb;
+       stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
+       stats.band = IEEE80211_BAND_2GHZ;
+       stats.signal = status->signal_strength;
 
-       if (length < ZD_PLCP_HEADER_SIZE + IEEE80211_1ADDR_LEN +
-                    IEEE80211_FCS_LEN + sizeof(struct rx_status))
-               return -EINVAL;
+       rate = zd_rx_rate(buffer, status);
 
-       r = fill_rx_stats(&stats, &status, mac, buffer, length);
-       if (r)
-               return r;
+       /* todo: return index in the big switches in zd_rx_rate instead */
+       for (i = 0; i < mac->band.n_bitrates; i++)
+               if (rate == mac->band.bitrates[i].hw_value)
+                       stats.rate_idx = i;
 
-       length -= ZD_PLCP_HEADER_SIZE+IEEE80211_FCS_LEN+
-                 sizeof(struct rx_status);
+       length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
        buffer += ZD_PLCP_HEADER_SIZE;
 
-       update_qual_rssi(mac, buffer, length, stats.signal, stats.rssi);
+       /* Except for bad frames, filter each frame to see if it is an ACK, in
+        * which case our internal TX tracking is updated. Normally we then
+        * bail here as there's no need to pass ACKs on up to the stack, but
+        * there is also the case where the stack has requested us to pass
+        * control frames on up (pass_ctrl) which we must consider. */
+       if (!bad_frame &&
+                       filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
+                       && !mac->pass_ctrl)
+               return 0;
 
-       r = filter_rx(ieee, buffer, length, &stats);
-       if (r <= 0)
-               return r;
+       fc = get_unaligned((__le16*)buffer);
+       need_padding = ieee80211_is_data_qos(fc) ^ ieee80211_has_a4(fc);
 
-       skb = dev_alloc_skb(sizeof(struct zd_rt_hdr) + length);
-       if (!skb)
+       skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
+       if (skb == NULL)
                return -ENOMEM;
-       if (ieee->iw_mode == IW_MODE_MONITOR)
-               fill_rt_header(skb_put(skb, sizeof(struct zd_rt_hdr)), mac,
-                              &stats, status);
+       if (need_padding) {
+               /* Make sure the the payload data is 4 byte aligned. */
+               skb_reserve(skb, 2);
+       }
+
+       /* FIXME : could we avoid this big memcpy ? */
        memcpy(skb_put(skb, length), buffer, length);
 
-       r = ieee80211_rx(ieee, skb, &stats);
-       if (!r) {
-               ZD_ASSERT(in_irq());
-               dev_kfree_skb_irq(skb);
-       }
+       memcpy(IEEE80211_SKB_RXCB(skb), &stats, sizeof(stats));
+       ieee80211_rx_irqsafe(hw, skb);
        return 0;
 }
 
-static int netdev_tx(struct ieee80211_txb *txb, struct net_device *netdev,
-                    int pri)
+static int zd_op_add_interface(struct ieee80211_hw *hw,
+                               struct ieee80211_vif *vif)
 {
-       return zd_mac_tx(zd_netdev_mac(netdev), txb, pri);
+       struct zd_mac *mac = zd_hw_mac(hw);
+
+       /* using NL80211_IFTYPE_UNSPECIFIED to indicate no mode selected */
+       if (mac->type != NL80211_IFTYPE_UNSPECIFIED)
+               return -EOPNOTSUPP;
+
+       switch (vif->type) {
+       case NL80211_IFTYPE_MONITOR:
+       case NL80211_IFTYPE_MESH_POINT:
+       case NL80211_IFTYPE_STATION:
+       case NL80211_IFTYPE_ADHOC:
+               mac->type = vif->type;
+               break;
+       default:
+               return -EOPNOTSUPP;
+       }
+
+       return zd_write_mac_addr(&mac->chip, vif->addr);
 }
 
-static void set_security(struct net_device *netdev,
-                        struct ieee80211_security *sec)
+static void zd_op_remove_interface(struct ieee80211_hw *hw,
+                                   struct ieee80211_vif *vif)
 {
-       struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
-       struct ieee80211_security *secinfo = &ieee->sec;
-       int keyidx;
-
-       dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)), "\n");
-
-       for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
-               if (sec->flags & (1<<keyidx)) {
-                       secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
-                       secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
-                       memcpy(secinfo->keys[keyidx], sec->keys[keyidx],
-                              SCM_KEY_LEN);
-               }
-
-       if (sec->flags & SEC_ACTIVE_KEY) {
-               secinfo->active_key = sec->active_key;
-               dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
-                       "   .active_key = %d\n", sec->active_key);
-       }
-       if (sec->flags & SEC_UNICAST_GROUP) {
-               secinfo->unicast_uses_group = sec->unicast_uses_group;
-               dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
-                       "   .unicast_uses_group = %d\n",
-                       sec->unicast_uses_group);
-       }
-       if (sec->flags & SEC_LEVEL) {
-               secinfo->level = sec->level;
-               dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
-                       "   .level = %d\n", sec->level);
-       }
-       if (sec->flags & SEC_ENABLED) {
-               secinfo->enabled = sec->enabled;
-               dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
-                       "   .enabled = %d\n", sec->enabled);
-       }
-       if (sec->flags & SEC_ENCRYPT) {
-               secinfo->encrypt = sec->encrypt;
-               dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
-                       "   .encrypt = %d\n", sec->encrypt);
-       }
-       if (sec->flags & SEC_AUTH_MODE) {
-               secinfo->auth_mode = sec->auth_mode;
-               dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
-                       "   .auth_mode = %d\n", sec->auth_mode);
-       }
+       struct zd_mac *mac = zd_hw_mac(hw);
+       mac->type = NL80211_IFTYPE_UNSPECIFIED;
+       zd_set_beacon_interval(&mac->chip, 0);
+       zd_write_mac_addr(&mac->chip, NULL);
 }
 
-static void ieee_init(struct ieee80211_device *ieee)
+static int zd_op_config(struct ieee80211_hw *hw, u32 changed)
 {
-       ieee->mode = IEEE_B | IEEE_G;
-       ieee->freq_band = IEEE80211_24GHZ_BAND;
-       ieee->modulation = IEEE80211_OFDM_MODULATION | IEEE80211_CCK_MODULATION;
-       ieee->tx_headroom = sizeof(struct zd_ctrlset);
-       ieee->set_security = set_security;
-       ieee->hard_start_xmit = netdev_tx;
-
-       /* Software encryption/decryption for now */
-       ieee->host_build_iv = 0;
-       ieee->host_encrypt = 1;
-       ieee->host_decrypt = 1;
-
-       /* FIXME: default to managed mode, until ieee80211 and zd1211rw can
-        * correctly support AUTO */
-       ieee->iw_mode = IW_MODE_INFRA;
+       struct zd_mac *mac = zd_hw_mac(hw);
+       struct ieee80211_conf *conf = &hw->conf;
+
+       return zd_chip_set_channel(&mac->chip, conf->channel->hw_value);
 }
 
-static void softmac_init(struct ieee80211softmac_device *sm)
+static void zd_process_intr(struct work_struct *work)
 {
-       sm->set_channel = set_channel;
+       u16 int_status;
+       struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
+
+       int_status = le16_to_cpu(*(__le16 *)(mac->intr_buffer+4));
+       if (int_status & INT_CFG_NEXT_BCN)
+               dev_dbg_f_limit(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");
+       else
+               dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
+
+       zd_chip_enable_hwint(&mac->chip);
 }
 
-struct iw_statistics *zd_mac_get_wireless_stats(struct net_device *ndev)
+
+static void set_multicast_hash_handler(struct work_struct *work)
 {
-       struct zd_mac *mac = zd_netdev_mac(ndev);
-       struct iw_statistics *iw_stats = &mac->iw_stats;
-       unsigned int i, count, qual_total, rssi_total;
+       struct zd_mac *mac =
+               container_of(work, struct zd_mac, set_multicast_hash_work);
+       struct zd_mc_hash hash;
 
-       memset(iw_stats, 0, sizeof(struct iw_statistics));
-       /* We are not setting the status, because ieee->state is not updated
-        * at all and this driver doesn't track authentication state.
-        */
        spin_lock_irq(&mac->lock);
-       count = mac->stats_count < ZD_MAC_STATS_BUFFER_SIZE ?
-               mac->stats_count : ZD_MAC_STATS_BUFFER_SIZE;
-       qual_total = rssi_total = 0;
-       for (i = 0; i < count; i++) {
-               qual_total += mac->qual_buffer[i];
-               rssi_total += mac->rssi_buffer[i];
-       }
+       hash = mac->multicast_hash;
        spin_unlock_irq(&mac->lock);
-       iw_stats->qual.updated = IW_QUAL_NOISE_INVALID;
-       if (count > 0) {
-               iw_stats->qual.qual = qual_total / count;
-               iw_stats->qual.level = rssi_total / count;
-               iw_stats->qual.updated |=
-                       IW_QUAL_QUAL_UPDATED|IW_QUAL_LEVEL_UPDATED;
-       } else {
-               iw_stats->qual.updated |=
-                       IW_QUAL_QUAL_INVALID|IW_QUAL_LEVEL_INVALID;
-       }
-       /* TODO: update counter */
-       return iw_stats;
+
+       zd_chip_set_multicast_hash(&mac->chip, &hash);
 }
 
-#ifdef DEBUG
-static const char* decryption_types[] = {
-       [ZD_RX_NO_WEP] = "none",
-       [ZD_RX_WEP64] = "WEP64",
-       [ZD_RX_TKIP] = "TKIP",
-       [ZD_RX_AES] = "AES",
-       [ZD_RX_WEP128] = "WEP128",
-       [ZD_RX_WEP256] = "WEP256",
-};
+static void set_rx_filter_handler(struct work_struct *work)
+{
+       struct zd_mac *mac =
+               container_of(work, struct zd_mac, set_rx_filter_work);
+       int r;
 
-static const char *decryption_type_string(u8 type)
+       dev_dbg_f(zd_mac_dev(mac), "\n");
+       r = set_rx_filter(mac);
+       if (r)
+               dev_err(zd_mac_dev(mac), "set_rx_filter_handler error %d\n", r);
+}
+
+static u64 zd_op_prepare_multicast(struct ieee80211_hw *hw,
+                                  int mc_count, struct dev_addr_list *mclist)
 {
-       const char *s;
+       struct zd_mac *mac = zd_hw_mac(hw);
+       struct zd_mc_hash hash;
+       int i;
 
-       if (type < ARRAY_SIZE(decryption_types)) {
-               s = decryption_types[type];
-       } else {
-               s = NULL;
+       zd_mc_clear(&hash);
+
+       for (i = 0; i < mc_count; i++) {
+               if (!mclist)
+                       break;
+               dev_dbg_f(zd_mac_dev(mac), "mc addr %pM\n", mclist->dmi_addr);
+               zd_mc_add_addr(&hash, mclist->dmi_addr);
+               mclist = mclist->next;
        }
-       return s ? s : "unknown";
+
+       return hash.low | ((u64)hash.high << 32);
 }
 
-static int is_ofdm(u8 frame_status)
+#define SUPPORTED_FIF_FLAGS \
+       (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
+       FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
+static void zd_op_configure_filter(struct ieee80211_hw *hw,
+                       unsigned int changed_flags,
+                       unsigned int *new_flags,
+                       u64 multicast)
 {
-       return (frame_status & ZD_RX_OFDM);
+       struct zd_mc_hash hash = {
+               .low = multicast,
+               .high = multicast >> 32,
+       };
+       struct zd_mac *mac = zd_hw_mac(hw);
+       unsigned long flags;
+
+       /* Only deal with supported flags */
+       changed_flags &= SUPPORTED_FIF_FLAGS;
+       *new_flags &= SUPPORTED_FIF_FLAGS;
+
+       /*
+        * If multicast parameter (as returned by zd_op_prepare_multicast)
+        * has changed, no bit in changed_flags is set. To handle this
+        * situation, we do not return if changed_flags is 0. If we do so,
+        * we will have some issue with IPv6 which uses multicast for link
+        * layer address resolution.
+        */
+       if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI))
+               zd_mc_add_all(&hash);
+
+       spin_lock_irqsave(&mac->lock, flags);
+       mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
+       mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
+       mac->multicast_hash = hash;
+       spin_unlock_irqrestore(&mac->lock, flags);
+
+       /* XXX: these can be called here now, can sleep now! */
+       queue_work(zd_workqueue, &mac->set_multicast_hash_work);
+
+       if (changed_flags & FIF_CONTROL)
+               queue_work(zd_workqueue, &mac->set_rx_filter_work);
+
+       /* no handling required for FIF_OTHER_BSS as we don't currently
+        * do BSSID filtering */
+       /* FIXME: in future it would be nice to enable the probe response
+        * filter (so that the driver doesn't see them) until
+        * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
+        * have to schedule work to enable prbresp reception, which might
+        * happen too late. For now we'll just listen and forward them all the
+        * time. */
 }
 
-void zd_dump_rx_status(const struct rx_status *status)
+static void set_rts_cts_work(struct work_struct *work)
 {
-       const char* modulation;
-       u8 quality;
+       struct zd_mac *mac =
+               container_of(work, struct zd_mac, set_rts_cts_work);
+       unsigned long flags;
+       unsigned int short_preamble;
 
-       if (is_ofdm(status->frame_status)) {
-               modulation = "ofdm";
-               quality = status->signal_quality_ofdm;
-       } else {
-               modulation = "cck";
-               quality = status->signal_quality_cck;
+       mutex_lock(&mac->chip.mutex);
+
+       spin_lock_irqsave(&mac->lock, flags);
+       mac->updating_rts_rate = 0;
+       short_preamble = mac->short_preamble;
+       spin_unlock_irqrestore(&mac->lock, flags);
+
+       zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
+       mutex_unlock(&mac->chip.mutex);
+}
+
+static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
+                                  struct ieee80211_vif *vif,
+                                  struct ieee80211_bss_conf *bss_conf,
+                                  u32 changes)
+{
+       struct zd_mac *mac = zd_hw_mac(hw);
+       unsigned long flags;
+       int associated;
+
+       dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
+
+       if (mac->type == NL80211_IFTYPE_MESH_POINT ||
+           mac->type == NL80211_IFTYPE_ADHOC) {
+               associated = true;
+               if (changes & BSS_CHANGED_BEACON) {
+                       struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
+
+                       if (beacon) {
+                               zd_mac_config_beacon(hw, beacon);
+                               kfree_skb(beacon);
+                       }
+               }
+
+               if (changes & BSS_CHANGED_BEACON_ENABLED) {
+                       u32 interval;
+
+                       if (bss_conf->enable_beacon)
+                               interval = BCN_MODE_IBSS |
+                                               bss_conf->beacon_int;
+                       else
+                               interval = 0;
+
+                       zd_set_beacon_interval(&mac->chip, interval);
+               }
+       } else
+               associated = is_valid_ether_addr(bss_conf->bssid);
+
+       spin_lock_irq(&mac->lock);
+       mac->associated = associated;
+       spin_unlock_irq(&mac->lock);
+
+       /* TODO: do hardware bssid filtering */
+
+       if (changes & BSS_CHANGED_ERP_PREAMBLE) {
+               spin_lock_irqsave(&mac->lock, flags);
+               mac->short_preamble = bss_conf->use_short_preamble;
+               if (!mac->updating_rts_rate) {
+                       mac->updating_rts_rate = 1;
+                       /* FIXME: should disable TX here, until work has
+                        * completed and RTS_CTS reg is updated */
+                       queue_work(zd_workqueue, &mac->set_rts_cts_work);
+               }
+               spin_unlock_irqrestore(&mac->lock, flags);
        }
-       pr_debug("rx status %s strength %#04x qual %#04x decryption %s\n",
-               modulation, status->signal_strength, quality,
-               decryption_type_string(status->decryption_type));
-       if (status->frame_status & ZD_RX_ERROR) {
-               pr_debug("rx error %s%s%s%s%s%s\n",
-                       (status->frame_status & ZD_RX_TIMEOUT_ERROR) ?
-                               "timeout " : "",
-                       (status->frame_status & ZD_RX_FIFO_OVERRUN_ERROR) ?
-                               "fifo " : "",
-                       (status->frame_status & ZD_RX_DECRYPTION_ERROR) ?
-                               "decryption " : "",
-                       (status->frame_status & ZD_RX_CRC32_ERROR) ?
-                               "crc32 " : "",
-                       (status->frame_status & ZD_RX_NO_ADDR1_MATCH_ERROR) ?
-                               "addr1 " : "",
-                       (status->frame_status & ZD_RX_CRC16_ERROR) ?
-                               "crc16" : "");
+}
+
+static u64 zd_op_get_tsf(struct ieee80211_hw *hw)
+{
+       struct zd_mac *mac = zd_hw_mac(hw);
+       return zd_chip_get_tsf(&mac->chip);
+}
+
+static const struct ieee80211_ops zd_ops = {
+       .tx                     = zd_op_tx,
+       .start                  = zd_op_start,
+       .stop                   = zd_op_stop,
+       .add_interface          = zd_op_add_interface,
+       .remove_interface       = zd_op_remove_interface,
+       .config                 = zd_op_config,
+       .prepare_multicast      = zd_op_prepare_multicast,
+       .configure_filter       = zd_op_configure_filter,
+       .bss_info_changed       = zd_op_bss_info_changed,
+       .get_tsf                = zd_op_get_tsf,
+};
+
+struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
+{
+       struct zd_mac *mac;
+       struct ieee80211_hw *hw;
+
+       hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
+       if (!hw) {
+               dev_dbg_f(&intf->dev, "out of memory\n");
+               return NULL;
        }
+
+       mac = zd_hw_mac(hw);
+
+       memset(mac, 0, sizeof(*mac));
+       spin_lock_init(&mac->lock);
+       mac->hw = hw;
+
+       mac->type = NL80211_IFTYPE_UNSPECIFIED;
+
+       memcpy(mac->channels, zd_channels, sizeof(zd_channels));
+       memcpy(mac->rates, zd_rates, sizeof(zd_rates));
+       mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
+       mac->band.bitrates = mac->rates;
+       mac->band.n_channels = ARRAY_SIZE(zd_channels);
+       mac->band.channels = mac->channels;
+
+       hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &mac->band;
+
+       hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
+                   IEEE80211_HW_SIGNAL_UNSPEC;
+
+       hw->wiphy->interface_modes =
+               BIT(NL80211_IFTYPE_MESH_POINT) |
+               BIT(NL80211_IFTYPE_STATION) |
+               BIT(NL80211_IFTYPE_ADHOC);
+
+       hw->max_signal = 100;
+       hw->queues = 1;
+       hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
+
+       /*
+        * Tell mac80211 that we support multi rate retries
+        */
+       hw->max_rates = IEEE80211_TX_MAX_RATES;
+       hw->max_rate_tries = 18;        /* 9 rates * 2 retries/rate */
+
+       skb_queue_head_init(&mac->ack_wait_queue);
+       mac->ack_pending = 0;
+
+       zd_chip_init(&mac->chip, hw, intf);
+       housekeeping_init(mac);
+       INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
+       INIT_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
+       INIT_WORK(&mac->set_rx_filter_work, set_rx_filter_handler);
+       INIT_WORK(&mac->process_intr, zd_process_intr);
+
+       SET_IEEE80211_DEV(hw, &intf->dev);
+       return hw;
 }
-#endif /* DEBUG */
 
 #define LINK_LED_WORK_DELAY HZ
 
-static void link_led_handler(void *p)
+static void link_led_handler(struct work_struct *work)
 {
-       struct zd_mac *mac = p;
+       struct zd_mac *mac =
+               container_of(work, struct zd_mac, housekeeping.link_led_work.work);
        struct zd_chip *chip = &mac->chip;
-       struct ieee80211softmac_device *sm = ieee80211_priv(mac->netdev);
        int is_associated;
        int r;
 
        spin_lock_irq(&mac->lock);
-       is_associated = sm->associnfo.associated != 0;
+       is_associated = mac->associated;
        spin_unlock_irq(&mac->lock);
 
        r = zd_chip_control_leds(chip,
-                                is_associated ? LED_ASSOCIATED : LED_SCANNING);
+                                is_associated ? ZD_LED_ASSOCIATED : ZD_LED_SCANNING);
        if (r)
-               dev_err(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
+               dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
 
        queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
                           LINK_LED_WORK_DELAY);
@@ -1104,7 +1197,7 @@ static void link_led_handler(void *p)
 
 static void housekeeping_init(struct zd_mac *mac)
 {
-       INIT_WORK(&mac->housekeeping.link_led_work, link_led_handler, mac);
+       INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
 }
 
 static void housekeeping_enable(struct zd_mac *mac)
@@ -1119,5 +1212,5 @@ static void housekeeping_disable(struct zd_mac *mac)
        dev_dbg_f(zd_mac_dev(mac), "\n");
        cancel_rearming_delayed_workqueue(zd_workqueue,
                &mac->housekeeping.link_led_work);
-       zd_chip_control_leds(&mac->chip, LED_OFF);
+       zd_chip_control_leds(&mac->chip, ZD_LED_OFF);
 }