Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* ZD1211 USB-WLAN driver for Linux
0003  *
0004  * Copyright (C) 2005-2007 Ulrich Kunitz <kune@deine-taler.de>
0005  * Copyright (C) 2006-2007 Daniel Drake <dsd@gentoo.org>
0006  * Copyright (C) 2006-2007 Michael Wu <flamingice@sourmilk.net>
0007  * Copyright (C) 2007-2008 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu>
0008  */
0009 
0010 #include <linux/netdevice.h>
0011 #include <linux/etherdevice.h>
0012 #include <linux/slab.h>
0013 #include <linux/usb.h>
0014 #include <linux/jiffies.h>
0015 #include <net/ieee80211_radiotap.h>
0016 
0017 #include "zd_def.h"
0018 #include "zd_chip.h"
0019 #include "zd_mac.h"
0020 #include "zd_rf.h"
0021 
0022 struct zd_reg_alpha2_map {
0023     u32 reg;
0024     char alpha2[2];
0025 };
0026 
0027 static struct zd_reg_alpha2_map reg_alpha2_map[] = {
0028     { ZD_REGDOMAIN_FCC, "US" },
0029     { ZD_REGDOMAIN_IC, "CA" },
0030     { ZD_REGDOMAIN_ETSI, "DE" }, /* Generic ETSI, use most restrictive */
0031     { ZD_REGDOMAIN_JAPAN, "JP" },
0032     { ZD_REGDOMAIN_JAPAN_2, "JP" },
0033     { ZD_REGDOMAIN_JAPAN_3, "JP" },
0034     { ZD_REGDOMAIN_SPAIN, "ES" },
0035     { ZD_REGDOMAIN_FRANCE, "FR" },
0036 };
0037 
0038 /* This table contains the hardware specific values for the modulation rates. */
0039 static const struct ieee80211_rate zd_rates[] = {
0040     { .bitrate = 10,
0041       .hw_value = ZD_CCK_RATE_1M, },
0042     { .bitrate = 20,
0043       .hw_value = ZD_CCK_RATE_2M,
0044       .hw_value_short = ZD_CCK_RATE_2M | ZD_CCK_PREA_SHORT,
0045       .flags = IEEE80211_RATE_SHORT_PREAMBLE },
0046     { .bitrate = 55,
0047       .hw_value = ZD_CCK_RATE_5_5M,
0048       .hw_value_short = ZD_CCK_RATE_5_5M | ZD_CCK_PREA_SHORT,
0049       .flags = IEEE80211_RATE_SHORT_PREAMBLE },
0050     { .bitrate = 110,
0051       .hw_value = ZD_CCK_RATE_11M,
0052       .hw_value_short = ZD_CCK_RATE_11M | ZD_CCK_PREA_SHORT,
0053       .flags = IEEE80211_RATE_SHORT_PREAMBLE },
0054     { .bitrate = 60,
0055       .hw_value = ZD_OFDM_RATE_6M,
0056       .flags = 0 },
0057     { .bitrate = 90,
0058       .hw_value = ZD_OFDM_RATE_9M,
0059       .flags = 0 },
0060     { .bitrate = 120,
0061       .hw_value = ZD_OFDM_RATE_12M,
0062       .flags = 0 },
0063     { .bitrate = 180,
0064       .hw_value = ZD_OFDM_RATE_18M,
0065       .flags = 0 },
0066     { .bitrate = 240,
0067       .hw_value = ZD_OFDM_RATE_24M,
0068       .flags = 0 },
0069     { .bitrate = 360,
0070       .hw_value = ZD_OFDM_RATE_36M,
0071       .flags = 0 },
0072     { .bitrate = 480,
0073       .hw_value = ZD_OFDM_RATE_48M,
0074       .flags = 0 },
0075     { .bitrate = 540,
0076       .hw_value = ZD_OFDM_RATE_54M,
0077       .flags = 0 },
0078 };
0079 
0080 /*
0081  * Zydas retry rates table. Each line is listed in the same order as
0082  * in zd_rates[] and contains all the rate used when a packet is sent
0083  * starting with a given rates. Let's consider an example :
0084  *
0085  * "11 Mbits : 4, 3, 2, 1, 0" means :
0086  * - packet is sent using 4 different rates
0087  * - 1st rate is index 3 (ie 11 Mbits)
0088  * - 2nd rate is index 2 (ie 5.5 Mbits)
0089  * - 3rd rate is index 1 (ie 2 Mbits)
0090  * - 4th rate is index 0 (ie 1 Mbits)
0091  */
0092 
0093 static const struct tx_retry_rate zd_retry_rates[] = {
0094     { /*  1 Mbits */    1, { 0 }},
0095     { /*  2 Mbits */    2, { 1,  0 }},
0096     { /*  5.5 Mbits */  3, { 2,  1, 0 }},
0097     { /* 11 Mbits */    4, { 3,  2, 1, 0 }},
0098     { /*  6 Mbits */    5, { 4,  3, 2, 1, 0 }},
0099     { /*  9 Mbits */    6, { 5,  4, 3, 2, 1, 0}},
0100     { /* 12 Mbits */    5, { 6,  3, 2, 1, 0 }},
0101     { /* 18 Mbits */    6, { 7,  6, 3, 2, 1, 0 }},
0102     { /* 24 Mbits */    6, { 8,  6, 3, 2, 1, 0 }},
0103     { /* 36 Mbits */    7, { 9,  8, 6, 3, 2, 1, 0 }},
0104     { /* 48 Mbits */    8, {10,  9, 8, 6, 3, 2, 1, 0 }},
0105     { /* 54 Mbits */    9, {11, 10, 9, 8, 6, 3, 2, 1, 0 }}
0106 };
0107 
0108 static const struct ieee80211_channel zd_channels[] = {
0109     { .center_freq = 2412, .hw_value = 1 },
0110     { .center_freq = 2417, .hw_value = 2 },
0111     { .center_freq = 2422, .hw_value = 3 },
0112     { .center_freq = 2427, .hw_value = 4 },
0113     { .center_freq = 2432, .hw_value = 5 },
0114     { .center_freq = 2437, .hw_value = 6 },
0115     { .center_freq = 2442, .hw_value = 7 },
0116     { .center_freq = 2447, .hw_value = 8 },
0117     { .center_freq = 2452, .hw_value = 9 },
0118     { .center_freq = 2457, .hw_value = 10 },
0119     { .center_freq = 2462, .hw_value = 11 },
0120     { .center_freq = 2467, .hw_value = 12 },
0121     { .center_freq = 2472, .hw_value = 13 },
0122     { .center_freq = 2484, .hw_value = 14 },
0123 };
0124 
0125 static void housekeeping_init(struct zd_mac *mac);
0126 static void housekeeping_enable(struct zd_mac *mac);
0127 static void housekeeping_disable(struct zd_mac *mac);
0128 static void beacon_init(struct zd_mac *mac);
0129 static void beacon_enable(struct zd_mac *mac);
0130 static void beacon_disable(struct zd_mac *mac);
0131 static void set_rts_cts(struct zd_mac *mac, unsigned int short_preamble);
0132 static int zd_mac_config_beacon(struct ieee80211_hw *hw,
0133                 struct sk_buff *beacon, bool in_intr);
0134 
0135 static int zd_reg2alpha2(u8 regdomain, char *alpha2)
0136 {
0137     unsigned int i;
0138     struct zd_reg_alpha2_map *reg_map;
0139     for (i = 0; i < ARRAY_SIZE(reg_alpha2_map); i++) {
0140         reg_map = &reg_alpha2_map[i];
0141         if (regdomain == reg_map->reg) {
0142             alpha2[0] = reg_map->alpha2[0];
0143             alpha2[1] = reg_map->alpha2[1];
0144             return 0;
0145         }
0146     }
0147     return 1;
0148 }
0149 
0150 static int zd_check_signal(struct ieee80211_hw *hw, int signal)
0151 {
0152     struct zd_mac *mac = zd_hw_mac(hw);
0153 
0154     dev_dbg_f_cond(zd_mac_dev(mac), signal < 0 || signal > 100,
0155             "%s: signal value from device not in range 0..100, "
0156             "but %d.\n", __func__, signal);
0157 
0158     if (signal < 0)
0159         signal = 0;
0160     else if (signal > 100)
0161         signal = 100;
0162 
0163     return signal;
0164 }
0165 
0166 int zd_mac_preinit_hw(struct ieee80211_hw *hw)
0167 {
0168     int r;
0169     u8 addr[ETH_ALEN];
0170     struct zd_mac *mac = zd_hw_mac(hw);
0171 
0172     r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
0173     if (r)
0174         return r;
0175 
0176     SET_IEEE80211_PERM_ADDR(hw, addr);
0177 
0178     return 0;
0179 }
0180 
0181 int zd_mac_init_hw(struct ieee80211_hw *hw)
0182 {
0183     int r;
0184     struct zd_mac *mac = zd_hw_mac(hw);
0185     struct zd_chip *chip = &mac->chip;
0186     char alpha2[2];
0187     u8 default_regdomain;
0188 
0189     r = zd_chip_enable_int(chip);
0190     if (r)
0191         goto out;
0192     r = zd_chip_init_hw(chip);
0193     if (r)
0194         goto disable_int;
0195 
0196     ZD_ASSERT(!irqs_disabled());
0197 
0198     r = zd_read_regdomain(chip, &default_regdomain);
0199     if (r)
0200         goto disable_int;
0201     spin_lock_irq(&mac->lock);
0202     mac->regdomain = mac->default_regdomain = default_regdomain;
0203     spin_unlock_irq(&mac->lock);
0204 
0205     /* We must inform the device that we are doing encryption/decryption in
0206      * software at the moment. */
0207     r = zd_set_encryption_type(chip, ENC_SNIFFER);
0208     if (r)
0209         goto disable_int;
0210 
0211     r = zd_reg2alpha2(mac->regdomain, alpha2);
0212     if (r)
0213         goto disable_int;
0214 
0215     r = regulatory_hint(hw->wiphy, alpha2);
0216 disable_int:
0217     zd_chip_disable_int(chip);
0218 out:
0219     return r;
0220 }
0221 
0222 void zd_mac_clear(struct zd_mac *mac)
0223 {
0224     flush_workqueue(zd_workqueue);
0225     zd_chip_clear(&mac->chip);
0226     ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
0227 }
0228 
0229 static int set_rx_filter(struct zd_mac *mac)
0230 {
0231     unsigned long flags;
0232     u32 filter = STA_RX_FILTER;
0233 
0234     spin_lock_irqsave(&mac->lock, flags);
0235     if (mac->pass_ctrl)
0236         filter |= RX_FILTER_CTRL;
0237     spin_unlock_irqrestore(&mac->lock, flags);
0238 
0239     return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
0240 }
0241 
0242 static int set_mac_and_bssid(struct zd_mac *mac)
0243 {
0244     int r;
0245 
0246     if (!mac->vif)
0247         return -1;
0248 
0249     r = zd_write_mac_addr(&mac->chip, mac->vif->addr);
0250     if (r)
0251         return r;
0252 
0253     /* Vendor driver after setting MAC either sets BSSID for AP or
0254      * filter for other modes.
0255      */
0256     if (mac->type != NL80211_IFTYPE_AP)
0257         return set_rx_filter(mac);
0258     else
0259         return zd_write_bssid(&mac->chip, mac->vif->addr);
0260 }
0261 
0262 static int set_mc_hash(struct zd_mac *mac)
0263 {
0264     struct zd_mc_hash hash;
0265     zd_mc_clear(&hash);
0266     return zd_chip_set_multicast_hash(&mac->chip, &hash);
0267 }
0268 
0269 int zd_op_start(struct ieee80211_hw *hw)
0270 {
0271     struct zd_mac *mac = zd_hw_mac(hw);
0272     struct zd_chip *chip = &mac->chip;
0273     struct zd_usb *usb = &chip->usb;
0274     int r;
0275 
0276     if (!usb->initialized) {
0277         r = zd_usb_init_hw(usb);
0278         if (r)
0279             goto out;
0280     }
0281 
0282     r = zd_chip_enable_int(chip);
0283     if (r < 0)
0284         goto out;
0285 
0286     r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
0287     if (r < 0)
0288         goto disable_int;
0289     r = set_rx_filter(mac);
0290     if (r)
0291         goto disable_int;
0292     r = set_mc_hash(mac);
0293     if (r)
0294         goto disable_int;
0295 
0296     /* Wait after setting the multicast hash table and powering on
0297      * the radio otherwise interface bring up will fail. This matches
0298      * what the vendor driver did.
0299      */
0300     msleep(10);
0301 
0302     r = zd_chip_switch_radio_on(chip);
0303     if (r < 0) {
0304         dev_err(zd_chip_dev(chip),
0305             "%s: failed to set radio on\n", __func__);
0306         goto disable_int;
0307     }
0308     r = zd_chip_enable_rxtx(chip);
0309     if (r < 0)
0310         goto disable_radio;
0311     r = zd_chip_enable_hwint(chip);
0312     if (r < 0)
0313         goto disable_rxtx;
0314 
0315     housekeeping_enable(mac);
0316     beacon_enable(mac);
0317     set_bit(ZD_DEVICE_RUNNING, &mac->flags);
0318     return 0;
0319 disable_rxtx:
0320     zd_chip_disable_rxtx(chip);
0321 disable_radio:
0322     zd_chip_switch_radio_off(chip);
0323 disable_int:
0324     zd_chip_disable_int(chip);
0325 out:
0326     return r;
0327 }
0328 
0329 void zd_op_stop(struct ieee80211_hw *hw)
0330 {
0331     struct zd_mac *mac = zd_hw_mac(hw);
0332     struct zd_chip *chip = &mac->chip;
0333     struct sk_buff *skb;
0334     struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
0335 
0336     clear_bit(ZD_DEVICE_RUNNING, &mac->flags);
0337 
0338     /* The order here deliberately is a little different from the open()
0339      * method, since we need to make sure there is no opportunity for RX
0340      * frames to be processed by mac80211 after we have stopped it.
0341      */
0342 
0343     zd_chip_disable_rxtx(chip);
0344     beacon_disable(mac);
0345     housekeeping_disable(mac);
0346     flush_workqueue(zd_workqueue);
0347 
0348     zd_chip_disable_hwint(chip);
0349     zd_chip_switch_radio_off(chip);
0350     zd_chip_disable_int(chip);
0351 
0352 
0353     while ((skb = skb_dequeue(ack_wait_queue)))
0354         dev_kfree_skb_any(skb);
0355 }
0356 
0357 int zd_restore_settings(struct zd_mac *mac)
0358 {
0359     struct sk_buff *beacon;
0360     struct zd_mc_hash multicast_hash;
0361     unsigned int short_preamble;
0362     int r, beacon_interval, beacon_period;
0363     u8 channel;
0364 
0365     dev_dbg_f(zd_mac_dev(mac), "\n");
0366 
0367     spin_lock_irq(&mac->lock);
0368     multicast_hash = mac->multicast_hash;
0369     short_preamble = mac->short_preamble;
0370     beacon_interval = mac->beacon.interval;
0371     beacon_period = mac->beacon.period;
0372     channel = mac->channel;
0373     spin_unlock_irq(&mac->lock);
0374 
0375     r = set_mac_and_bssid(mac);
0376     if (r < 0) {
0377         dev_dbg_f(zd_mac_dev(mac), "set_mac_and_bssid failed, %d\n", r);
0378         return r;
0379     }
0380 
0381     r = zd_chip_set_channel(&mac->chip, channel);
0382     if (r < 0) {
0383         dev_dbg_f(zd_mac_dev(mac), "zd_chip_set_channel failed, %d\n",
0384               r);
0385         return r;
0386     }
0387 
0388     set_rts_cts(mac, short_preamble);
0389 
0390     r = zd_chip_set_multicast_hash(&mac->chip, &multicast_hash);
0391     if (r < 0) {
0392         dev_dbg_f(zd_mac_dev(mac),
0393               "zd_chip_set_multicast_hash failed, %d\n", r);
0394         return r;
0395     }
0396 
0397     if (mac->type == NL80211_IFTYPE_MESH_POINT ||
0398         mac->type == NL80211_IFTYPE_ADHOC ||
0399         mac->type == NL80211_IFTYPE_AP) {
0400         if (mac->vif != NULL) {
0401             beacon = ieee80211_beacon_get(mac->hw, mac->vif, 0);
0402             if (beacon)
0403                 zd_mac_config_beacon(mac->hw, beacon, false);
0404         }
0405 
0406         zd_set_beacon_interval(&mac->chip, beacon_interval,
0407                     beacon_period, mac->type);
0408 
0409         spin_lock_irq(&mac->lock);
0410         mac->beacon.last_update = jiffies;
0411         spin_unlock_irq(&mac->lock);
0412     }
0413 
0414     return 0;
0415 }
0416 
0417 /**
0418  * zd_mac_tx_status - reports tx status of a packet if required
0419  * @hw: a &struct ieee80211_hw pointer
0420  * @skb: a sk-buffer
0421  * @ackssi: ACK signal strength
0422  * @tx_status: success and/or retry
0423  *
0424  * This information calls ieee80211_tx_status_irqsafe() if required by the
0425  * control information. It copies the control information into the status
0426  * information.
0427  *
0428  * If no status information has been requested, the skb is freed.
0429  */
0430 static void zd_mac_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
0431               int ackssi, struct tx_status *tx_status)
0432 {
0433     struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
0434     int i;
0435     int success = 1, retry = 1;
0436     int first_idx;
0437     const struct tx_retry_rate *retries;
0438 
0439     ieee80211_tx_info_clear_status(info);
0440 
0441     if (tx_status) {
0442         success = !tx_status->failure;
0443         retry = tx_status->retry + success;
0444     }
0445 
0446     if (success) {
0447         /* success */
0448         info->flags |= IEEE80211_TX_STAT_ACK;
0449     } else {
0450         /* failure */
0451         info->flags &= ~IEEE80211_TX_STAT_ACK;
0452     }
0453 
0454     first_idx = info->status.rates[0].idx;
0455     ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
0456     retries = &zd_retry_rates[first_idx];
0457     ZD_ASSERT(1 <= retry && retry <= retries->count);
0458 
0459     info->status.rates[0].idx = retries->rate[0];
0460     info->status.rates[0].count = 1; // (retry > 1 ? 2 : 1);
0461 
0462     for (i=1; i<IEEE80211_TX_MAX_RATES-1 && i<retry; i++) {
0463         info->status.rates[i].idx = retries->rate[i];
0464         info->status.rates[i].count = 1; // ((i==retry-1) && success ? 1:2);
0465     }
0466     for (; i<IEEE80211_TX_MAX_RATES && i<retry; i++) {
0467         info->status.rates[i].idx = retries->rate[retry - 1];
0468         info->status.rates[i].count = 1; // (success ? 1:2);
0469     }
0470     if (i<IEEE80211_TX_MAX_RATES)
0471         info->status.rates[i].idx = -1; /* terminate */
0472 
0473     info->status.ack_signal = zd_check_signal(hw, ackssi);
0474     ieee80211_tx_status_irqsafe(hw, skb);
0475 }
0476 
0477 /**
0478  * zd_mac_tx_failed - callback for failed frames
0479  * @urb: pointer to the urb structure
0480  *
0481  * This function is called if a frame couldn't be successfully
0482  * transferred. The first frame from the tx queue, will be selected and
0483  * reported as error to the upper layers.
0484  */
0485 void zd_mac_tx_failed(struct urb *urb)
0486 {
0487     struct ieee80211_hw * hw = zd_usb_to_hw(urb->context);
0488     struct zd_mac *mac = zd_hw_mac(hw);
0489     struct sk_buff_head *q = &mac->ack_wait_queue;
0490     struct sk_buff *skb;
0491     struct tx_status *tx_status = (struct tx_status *)urb->transfer_buffer;
0492     unsigned long flags;
0493     int success = !tx_status->failure;
0494     int retry = tx_status->retry + success;
0495     int found = 0;
0496     int i, position = 0;
0497 
0498     spin_lock_irqsave(&q->lock, flags);
0499 
0500     skb_queue_walk(q, skb) {
0501         struct ieee80211_hdr *tx_hdr;
0502         struct ieee80211_tx_info *info;
0503         int first_idx, final_idx;
0504         const struct tx_retry_rate *retries;
0505         u8 final_rate;
0506 
0507         position ++;
0508 
0509         /* if the hardware reports a failure and we had a 802.11 ACK
0510          * pending, then we skip the first skb when searching for a
0511          * matching frame */
0512         if (tx_status->failure && mac->ack_pending &&
0513             skb_queue_is_first(q, skb)) {
0514             continue;
0515         }
0516 
0517         tx_hdr = (struct ieee80211_hdr *)skb->data;
0518 
0519         /* we skip all frames not matching the reported destination */
0520         if (unlikely(!ether_addr_equal(tx_hdr->addr1, tx_status->mac)))
0521             continue;
0522 
0523         /* we skip all frames not matching the reported final rate */
0524 
0525         info = IEEE80211_SKB_CB(skb);
0526         first_idx = info->status.rates[0].idx;
0527         ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
0528         retries = &zd_retry_rates[first_idx];
0529         if (retry <= 0 || retry > retries->count)
0530             continue;
0531 
0532         final_idx = retries->rate[retry - 1];
0533         final_rate = zd_rates[final_idx].hw_value;
0534 
0535         if (final_rate != tx_status->rate) {
0536             continue;
0537         }
0538 
0539         found = 1;
0540         break;
0541     }
0542 
0543     if (found) {
0544         for (i=1; i<=position; i++) {
0545             skb = __skb_dequeue(q);
0546             zd_mac_tx_status(hw, skb,
0547                      mac->ack_pending ? mac->ack_signal : 0,
0548                      i == position ? tx_status : NULL);
0549             mac->ack_pending = 0;
0550         }
0551     }
0552 
0553     spin_unlock_irqrestore(&q->lock, flags);
0554 }
0555 
0556 /**
0557  * zd_mac_tx_to_dev - callback for USB layer
0558  * @skb: a &sk_buff pointer
0559  * @error: error value, 0 if transmission successful
0560  *
0561  * Informs the MAC layer that the frame has successfully transferred to the
0562  * device. If an ACK is required and the transfer to the device has been
0563  * successful, the packets are put on the @ack_wait_queue with
0564  * the control set removed.
0565  */
0566 void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
0567 {
0568     struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
0569     struct ieee80211_hw *hw = info->rate_driver_data[0];
0570     struct zd_mac *mac = zd_hw_mac(hw);
0571 
0572     ieee80211_tx_info_clear_status(info);
0573 
0574     skb_pull(skb, sizeof(struct zd_ctrlset));
0575     if (unlikely(error ||
0576         (info->flags & IEEE80211_TX_CTL_NO_ACK))) {
0577         /*
0578          * FIXME : do we need to fill in anything ?
0579          */
0580         ieee80211_tx_status_irqsafe(hw, skb);
0581     } else {
0582         struct sk_buff_head *q = &mac->ack_wait_queue;
0583 
0584         skb_queue_tail(q, skb);
0585         while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS) {
0586             zd_mac_tx_status(hw, skb_dequeue(q),
0587                      mac->ack_pending ? mac->ack_signal : 0,
0588                      NULL);
0589             mac->ack_pending = 0;
0590         }
0591     }
0592 }
0593 
0594 static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
0595 {
0596     /* ZD_PURE_RATE() must be used to remove the modulation type flag of
0597      * the zd-rate values.
0598      */
0599     static const u8 rate_divisor[] = {
0600         [ZD_PURE_RATE(ZD_CCK_RATE_1M)]   =  1,
0601         [ZD_PURE_RATE(ZD_CCK_RATE_2M)]   =  2,
0602         /* Bits must be doubled. */
0603         [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
0604         [ZD_PURE_RATE(ZD_CCK_RATE_11M)]  = 11,
0605         [ZD_PURE_RATE(ZD_OFDM_RATE_6M)]  =  6,
0606         [ZD_PURE_RATE(ZD_OFDM_RATE_9M)]  =  9,
0607         [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
0608         [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
0609         [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
0610         [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
0611         [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
0612         [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
0613     };
0614 
0615     u32 bits = (u32)tx_length * 8;
0616     u32 divisor;
0617 
0618     divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
0619     if (divisor == 0)
0620         return -EINVAL;
0621 
0622     switch (zd_rate) {
0623     case ZD_CCK_RATE_5_5M:
0624         bits = (2*bits) + 10; /* round up to the next integer */
0625         break;
0626     case ZD_CCK_RATE_11M:
0627         if (service) {
0628             u32 t = bits % 11;
0629             *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
0630             if (0 < t && t <= 3) {
0631                 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
0632             }
0633         }
0634         bits += 10; /* round up to the next integer */
0635         break;
0636     }
0637 
0638     return bits/divisor;
0639 }
0640 
0641 static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
0642                        struct ieee80211_hdr *header,
0643                        struct ieee80211_tx_info *info)
0644 {
0645     /*
0646      * CONTROL TODO:
0647      * - if backoff needed, enable bit 0
0648      * - if burst (backoff not needed) disable bit 0
0649      */
0650 
0651     cs->control = 0;
0652 
0653     /* First fragment */
0654     if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
0655         cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
0656 
0657     /* No ACK expected (multicast, etc.) */
0658     if (info->flags & IEEE80211_TX_CTL_NO_ACK)
0659         cs->control |= ZD_CS_NO_ACK;
0660 
0661     /* PS-POLL */
0662     if (ieee80211_is_pspoll(header->frame_control))
0663         cs->control |= ZD_CS_PS_POLL_FRAME;
0664 
0665     if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
0666         cs->control |= ZD_CS_RTS;
0667 
0668     if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
0669         cs->control |= ZD_CS_SELF_CTS;
0670 
0671     /* FIXME: Management frame? */
0672 }
0673 
0674 static bool zd_mac_match_cur_beacon(struct zd_mac *mac, struct sk_buff *beacon)
0675 {
0676     if (!mac->beacon.cur_beacon)
0677         return false;
0678 
0679     if (mac->beacon.cur_beacon->len != beacon->len)
0680         return false;
0681 
0682     return !memcmp(beacon->data, mac->beacon.cur_beacon->data, beacon->len);
0683 }
0684 
0685 static void zd_mac_free_cur_beacon_locked(struct zd_mac *mac)
0686 {
0687     ZD_ASSERT(mutex_is_locked(&mac->chip.mutex));
0688 
0689     kfree_skb(mac->beacon.cur_beacon);
0690     mac->beacon.cur_beacon = NULL;
0691 }
0692 
0693 static void zd_mac_free_cur_beacon(struct zd_mac *mac)
0694 {
0695     mutex_lock(&mac->chip.mutex);
0696     zd_mac_free_cur_beacon_locked(mac);
0697     mutex_unlock(&mac->chip.mutex);
0698 }
0699 
0700 static int zd_mac_config_beacon(struct ieee80211_hw *hw, struct sk_buff *beacon,
0701                 bool in_intr)
0702 {
0703     struct zd_mac *mac = zd_hw_mac(hw);
0704     int r, ret, num_cmds, req_pos = 0;
0705     u32 tmp, j = 0;
0706     /* 4 more bytes for tail CRC */
0707     u32 full_len = beacon->len + 4;
0708     unsigned long end_jiffies, message_jiffies;
0709     struct zd_ioreq32 *ioreqs;
0710 
0711     mutex_lock(&mac->chip.mutex);
0712 
0713     /* Check if hw already has this beacon. */
0714     if (zd_mac_match_cur_beacon(mac, beacon)) {
0715         r = 0;
0716         goto out_nofree;
0717     }
0718 
0719     /* Alloc memory for full beacon write at once. */
0720     num_cmds = 1 + zd_chip_is_zd1211b(&mac->chip) + full_len;
0721     ioreqs = kmalloc_array(num_cmds, sizeof(struct zd_ioreq32),
0722                    GFP_KERNEL);
0723     if (!ioreqs) {
0724         r = -ENOMEM;
0725         goto out_nofree;
0726     }
0727 
0728     r = zd_iowrite32_locked(&mac->chip, 0, CR_BCN_FIFO_SEMAPHORE);
0729     if (r < 0)
0730         goto out;
0731     r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE);
0732     if (r < 0)
0733         goto release_sema;
0734     if (in_intr && tmp & 0x2) {
0735         r = -EBUSY;
0736         goto release_sema;
0737     }
0738 
0739     end_jiffies = jiffies + HZ / 2; /*~500ms*/
0740     message_jiffies = jiffies + HZ / 10; /*~100ms*/
0741     while (tmp & 0x2) {
0742         r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE);
0743         if (r < 0)
0744             goto release_sema;
0745         if (time_is_before_eq_jiffies(message_jiffies)) {
0746             message_jiffies = jiffies + HZ / 10;
0747             dev_err(zd_mac_dev(mac),
0748                     "CR_BCN_FIFO_SEMAPHORE not ready\n");
0749             if (time_is_before_eq_jiffies(end_jiffies))  {
0750                 dev_err(zd_mac_dev(mac),
0751                         "Giving up beacon config.\n");
0752                 r = -ETIMEDOUT;
0753                 goto reset_device;
0754             }
0755         }
0756         msleep(20);
0757     }
0758 
0759     ioreqs[req_pos].addr = CR_BCN_FIFO;
0760     ioreqs[req_pos].value = full_len - 1;
0761     req_pos++;
0762     if (zd_chip_is_zd1211b(&mac->chip)) {
0763         ioreqs[req_pos].addr = CR_BCN_LENGTH;
0764         ioreqs[req_pos].value = full_len - 1;
0765         req_pos++;
0766     }
0767 
0768     for (j = 0 ; j < beacon->len; j++) {
0769         ioreqs[req_pos].addr = CR_BCN_FIFO;
0770         ioreqs[req_pos].value = *((u8 *)(beacon->data + j));
0771         req_pos++;
0772     }
0773 
0774     for (j = 0; j < 4; j++) {
0775         ioreqs[req_pos].addr = CR_BCN_FIFO;
0776         ioreqs[req_pos].value = 0x0;
0777         req_pos++;
0778     }
0779 
0780     BUG_ON(req_pos != num_cmds);
0781 
0782     r = zd_iowrite32a_locked(&mac->chip, ioreqs, num_cmds);
0783 
0784 release_sema:
0785     /*
0786      * Try very hard to release device beacon semaphore, as otherwise
0787      * device/driver can be left in unusable state.
0788      */
0789     end_jiffies = jiffies + HZ / 2; /*~500ms*/
0790     ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE);
0791     while (ret < 0) {
0792         if (in_intr || time_is_before_eq_jiffies(end_jiffies)) {
0793             ret = -ETIMEDOUT;
0794             break;
0795         }
0796 
0797         msleep(20);
0798         ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE);
0799     }
0800 
0801     if (ret < 0)
0802         dev_err(zd_mac_dev(mac), "Could not release "
0803                      "CR_BCN_FIFO_SEMAPHORE!\n");
0804     if (r < 0 || ret < 0) {
0805         if (r >= 0)
0806             r = ret;
0807 
0808         /* We don't know if beacon was written successfully or not,
0809          * so clear current. */
0810         zd_mac_free_cur_beacon_locked(mac);
0811 
0812         goto out;
0813     }
0814 
0815     /* Beacon has now been written successfully, update current. */
0816     zd_mac_free_cur_beacon_locked(mac);
0817     mac->beacon.cur_beacon = beacon;
0818     beacon = NULL;
0819 
0820     /* 802.11b/g 2.4G CCK 1Mb
0821      * 802.11a, not yet implemented, uses different values (see GPL vendor
0822      * driver)
0823      */
0824     r = zd_iowrite32_locked(&mac->chip, 0x00000400 | (full_len << 19),
0825                 CR_BCN_PLCP_CFG);
0826 out:
0827     kfree(ioreqs);
0828 out_nofree:
0829     kfree_skb(beacon);
0830     mutex_unlock(&mac->chip.mutex);
0831 
0832     return r;
0833 
0834 reset_device:
0835     zd_mac_free_cur_beacon_locked(mac);
0836     kfree_skb(beacon);
0837 
0838     mutex_unlock(&mac->chip.mutex);
0839     kfree(ioreqs);
0840 
0841     /* semaphore stuck, reset device to avoid fw freeze later */
0842     dev_warn(zd_mac_dev(mac), "CR_BCN_FIFO_SEMAPHORE stuck, "
0843                   "resetting device...");
0844     usb_queue_reset_device(mac->chip.usb.intf);
0845 
0846     return r;
0847 }
0848 
0849 static int fill_ctrlset(struct zd_mac *mac,
0850             struct sk_buff *skb)
0851 {
0852     int r;
0853     struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
0854     unsigned int frag_len = skb->len + FCS_LEN;
0855     unsigned int packet_length;
0856     struct ieee80211_rate *txrate;
0857     struct zd_ctrlset *cs = skb_push(skb, sizeof(struct zd_ctrlset));
0858     struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
0859 
0860     ZD_ASSERT(frag_len <= 0xffff);
0861 
0862     /*
0863      * Firmware computes the duration itself (for all frames except PSPoll)
0864      * and needs the field set to 0 at input, otherwise firmware messes up
0865      * duration_id and sets bits 14 and 15 on.
0866      */
0867     if (!ieee80211_is_pspoll(hdr->frame_control))
0868         hdr->duration_id = 0;
0869 
0870     txrate = ieee80211_get_tx_rate(mac->hw, info);
0871 
0872     cs->modulation = txrate->hw_value;
0873     if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
0874         cs->modulation = txrate->hw_value_short;
0875 
0876     cs->tx_length = cpu_to_le16(frag_len);
0877 
0878     cs_set_control(mac, cs, hdr, info);
0879 
0880     packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
0881     ZD_ASSERT(packet_length <= 0xffff);
0882     /* ZD1211B: Computing the length difference this way, gives us
0883      * flexibility to compute the packet length.
0884      */
0885     cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
0886             packet_length - frag_len : packet_length);
0887 
0888     /*
0889      * CURRENT LENGTH:
0890      * - transmit frame length in microseconds
0891      * - seems to be derived from frame length
0892      * - see Cal_Us_Service() in zdinlinef.h
0893      * - if macp->bTxBurstEnable is enabled, then multiply by 4
0894      *  - bTxBurstEnable is never set in the vendor driver
0895      *
0896      * SERVICE:
0897      * - "for PLCP configuration"
0898      * - always 0 except in some situations at 802.11b 11M
0899      * - see line 53 of zdinlinef.h
0900      */
0901     cs->service = 0;
0902     r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
0903                          le16_to_cpu(cs->tx_length));
0904     if (r < 0)
0905         return r;
0906     cs->current_length = cpu_to_le16(r);
0907     cs->next_frame_length = 0;
0908 
0909     return 0;
0910 }
0911 
0912 /**
0913  * zd_op_tx - transmits a network frame to the device
0914  *
0915  * @hw: a &struct ieee80211_hw pointer
0916  * @control: the control structure
0917  * @skb: socket buffer
0918  *
0919  * This function transmit an IEEE 802.11 network frame to the device. The
0920  * control block of the skbuff will be initialized. If necessary the incoming
0921  * mac80211 queues will be stopped.
0922  */
0923 static void zd_op_tx(struct ieee80211_hw *hw,
0924              struct ieee80211_tx_control *control,
0925              struct sk_buff *skb)
0926 {
0927     struct zd_mac *mac = zd_hw_mac(hw);
0928     struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
0929     int r;
0930 
0931     r = fill_ctrlset(mac, skb);
0932     if (r)
0933         goto fail;
0934 
0935     info->rate_driver_data[0] = hw;
0936 
0937     r = zd_usb_tx(&mac->chip.usb, skb);
0938     if (r)
0939         goto fail;
0940     return;
0941 
0942 fail:
0943     dev_kfree_skb(skb);
0944 }
0945 
0946 /**
0947  * filter_ack - filters incoming packets for acknowledgements
0948  * @hw: a &struct ieee80211_hw pointer
0949  * @rx_hdr: received header
0950  * @stats: the status for the received packet
0951  *
0952  * This functions looks for ACK packets and tries to match them with the
0953  * frames in the tx queue. If a match is found the frame will be dequeued and
0954  * the upper layers is informed about the successful transmission. If
0955  * mac80211 queues have been stopped and the number of frames still to be
0956  * transmitted is low the queues will be opened again.
0957  *
0958  * Returns 1 if the frame was an ACK, 0 if it was ignored.
0959  */
0960 static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
0961               struct ieee80211_rx_status *stats)
0962 {
0963     struct zd_mac *mac = zd_hw_mac(hw);
0964     struct sk_buff *skb;
0965     struct sk_buff_head *q;
0966     unsigned long flags;
0967     int found = 0;
0968     int i, position = 0;
0969 
0970     if (!ieee80211_is_ack(rx_hdr->frame_control))
0971         return 0;
0972 
0973     q = &mac->ack_wait_queue;
0974     spin_lock_irqsave(&q->lock, flags);
0975     skb_queue_walk(q, skb) {
0976         struct ieee80211_hdr *tx_hdr;
0977 
0978         position ++;
0979 
0980         if (mac->ack_pending && skb_queue_is_first(q, skb))
0981             continue;
0982 
0983         tx_hdr = (struct ieee80211_hdr *)skb->data;
0984         if (likely(ether_addr_equal(tx_hdr->addr2, rx_hdr->addr1)))
0985         {
0986             found = 1;
0987             break;
0988         }
0989     }
0990 
0991     if (found) {
0992         for (i=1; i<position; i++) {
0993             skb = __skb_dequeue(q);
0994             zd_mac_tx_status(hw, skb,
0995                      mac->ack_pending ? mac->ack_signal : 0,
0996                      NULL);
0997             mac->ack_pending = 0;
0998         }
0999 
1000         mac->ack_pending = 1;
1001         mac->ack_signal = stats->signal;
1002 
1003         /* Prevent pending tx-packet on AP-mode */
1004         if (mac->type == NL80211_IFTYPE_AP) {
1005             skb = __skb_dequeue(q);
1006             zd_mac_tx_status(hw, skb, mac->ack_signal, NULL);
1007             mac->ack_pending = 0;
1008         }
1009     }
1010 
1011     spin_unlock_irqrestore(&q->lock, flags);
1012     return 1;
1013 }
1014 
1015 int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
1016 {
1017     struct zd_mac *mac = zd_hw_mac(hw);
1018     struct ieee80211_rx_status stats;
1019     const struct rx_status *status;
1020     struct sk_buff *skb;
1021     int bad_frame = 0;
1022     __le16 fc;
1023     int need_padding;
1024     int i;
1025     u8 rate;
1026 
1027     if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
1028                  FCS_LEN + sizeof(struct rx_status))
1029         return -EINVAL;
1030 
1031     memset(&stats, 0, sizeof(stats));
1032 
1033     /* Note about pass_failed_fcs and pass_ctrl access below:
1034      * mac locking intentionally omitted here, as this is the only unlocked
1035      * reader and the only writer is configure_filter. Plus, if there were
1036      * any races accessing these variables, it wouldn't really matter.
1037      * If mac80211 ever provides a way for us to access filter flags
1038      * from outside configure_filter, we could improve on this. Also, this
1039      * situation may change once we implement some kind of DMA-into-skb
1040      * RX path. */
1041 
1042     /* Caller has to ensure that length >= sizeof(struct rx_status). */
1043     status = (struct rx_status *)
1044         (buffer + (length - sizeof(struct rx_status)));
1045     if (status->frame_status & ZD_RX_ERROR) {
1046         if (mac->pass_failed_fcs &&
1047                 (status->frame_status & ZD_RX_CRC32_ERROR)) {
1048             stats.flag |= RX_FLAG_FAILED_FCS_CRC;
1049             bad_frame = 1;
1050         } else {
1051             return -EINVAL;
1052         }
1053     }
1054 
1055     stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
1056     stats.band = NL80211_BAND_2GHZ;
1057     stats.signal = zd_check_signal(hw, status->signal_strength);
1058 
1059     rate = zd_rx_rate(buffer, status);
1060 
1061     /* todo: return index in the big switches in zd_rx_rate instead */
1062     for (i = 0; i < mac->band.n_bitrates; i++)
1063         if (rate == mac->band.bitrates[i].hw_value)
1064             stats.rate_idx = i;
1065 
1066     length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
1067     buffer += ZD_PLCP_HEADER_SIZE;
1068 
1069     /* Except for bad frames, filter each frame to see if it is an ACK, in
1070      * which case our internal TX tracking is updated. Normally we then
1071      * bail here as there's no need to pass ACKs on up to the stack, but
1072      * there is also the case where the stack has requested us to pass
1073      * control frames on up (pass_ctrl) which we must consider. */
1074     if (!bad_frame &&
1075             filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
1076             && !mac->pass_ctrl)
1077         return 0;
1078 
1079     fc = get_unaligned((__le16*)buffer);
1080     need_padding = ieee80211_is_data_qos(fc) ^ ieee80211_has_a4(fc);
1081 
1082     skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
1083     if (skb == NULL)
1084         return -ENOMEM;
1085     if (need_padding) {
1086         /* Make sure the payload data is 4 byte aligned. */
1087         skb_reserve(skb, 2);
1088     }
1089 
1090     /* FIXME : could we avoid this big memcpy ? */
1091     skb_put_data(skb, buffer, length);
1092 
1093     memcpy(IEEE80211_SKB_RXCB(skb), &stats, sizeof(stats));
1094     ieee80211_rx_irqsafe(hw, skb);
1095     return 0;
1096 }
1097 
1098 static int zd_op_add_interface(struct ieee80211_hw *hw,
1099                 struct ieee80211_vif *vif)
1100 {
1101     struct zd_mac *mac = zd_hw_mac(hw);
1102 
1103     /* using NL80211_IFTYPE_UNSPECIFIED to indicate no mode selected */
1104     if (mac->type != NL80211_IFTYPE_UNSPECIFIED)
1105         return -EOPNOTSUPP;
1106 
1107     switch (vif->type) {
1108     case NL80211_IFTYPE_MONITOR:
1109     case NL80211_IFTYPE_MESH_POINT:
1110     case NL80211_IFTYPE_STATION:
1111     case NL80211_IFTYPE_ADHOC:
1112     case NL80211_IFTYPE_AP:
1113         mac->type = vif->type;
1114         break;
1115     default:
1116         return -EOPNOTSUPP;
1117     }
1118 
1119     mac->vif = vif;
1120 
1121     return set_mac_and_bssid(mac);
1122 }
1123 
1124 static void zd_op_remove_interface(struct ieee80211_hw *hw,
1125                     struct ieee80211_vif *vif)
1126 {
1127     struct zd_mac *mac = zd_hw_mac(hw);
1128     mac->type = NL80211_IFTYPE_UNSPECIFIED;
1129     mac->vif = NULL;
1130     zd_set_beacon_interval(&mac->chip, 0, 0, NL80211_IFTYPE_UNSPECIFIED);
1131     zd_write_mac_addr(&mac->chip, NULL);
1132 
1133     zd_mac_free_cur_beacon(mac);
1134 }
1135 
1136 static int zd_op_config(struct ieee80211_hw *hw, u32 changed)
1137 {
1138     struct zd_mac *mac = zd_hw_mac(hw);
1139     struct ieee80211_conf *conf = &hw->conf;
1140 
1141     spin_lock_irq(&mac->lock);
1142     mac->channel = conf->chandef.chan->hw_value;
1143     spin_unlock_irq(&mac->lock);
1144 
1145     return zd_chip_set_channel(&mac->chip, conf->chandef.chan->hw_value);
1146 }
1147 
1148 static void zd_beacon_done(struct zd_mac *mac)
1149 {
1150     struct sk_buff *skb, *beacon;
1151 
1152     if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
1153         return;
1154     if (!mac->vif || mac->vif->type != NL80211_IFTYPE_AP)
1155         return;
1156 
1157     /*
1158      * Send out buffered broad- and multicast frames.
1159      */
1160     while (!ieee80211_queue_stopped(mac->hw, 0)) {
1161         skb = ieee80211_get_buffered_bc(mac->hw, mac->vif);
1162         if (!skb)
1163             break;
1164         zd_op_tx(mac->hw, NULL, skb);
1165     }
1166 
1167     /*
1168      * Fetch next beacon so that tim_count is updated.
1169      */
1170     beacon = ieee80211_beacon_get(mac->hw, mac->vif, 0);
1171     if (beacon)
1172         zd_mac_config_beacon(mac->hw, beacon, true);
1173 
1174     spin_lock_irq(&mac->lock);
1175     mac->beacon.last_update = jiffies;
1176     spin_unlock_irq(&mac->lock);
1177 }
1178 
1179 static void zd_process_intr(struct work_struct *work)
1180 {
1181     u16 int_status;
1182     unsigned long flags;
1183     struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
1184 
1185     spin_lock_irqsave(&mac->lock, flags);
1186     int_status = le16_to_cpu(*(__le16 *)(mac->intr_buffer + 4));
1187     spin_unlock_irqrestore(&mac->lock, flags);
1188 
1189     if (int_status & INT_CFG_NEXT_BCN) {
1190         /*dev_dbg_f_limit(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");*/
1191         zd_beacon_done(mac);
1192     } else {
1193         dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
1194     }
1195 
1196     zd_chip_enable_hwint(&mac->chip);
1197 }
1198 
1199 
1200 static u64 zd_op_prepare_multicast(struct ieee80211_hw *hw,
1201                    struct netdev_hw_addr_list *mc_list)
1202 {
1203     struct zd_mac *mac = zd_hw_mac(hw);
1204     struct zd_mc_hash hash;
1205     struct netdev_hw_addr *ha;
1206 
1207     zd_mc_clear(&hash);
1208 
1209     netdev_hw_addr_list_for_each(ha, mc_list) {
1210         dev_dbg_f(zd_mac_dev(mac), "mc addr %pM\n", ha->addr);
1211         zd_mc_add_addr(&hash, ha->addr);
1212     }
1213 
1214     return hash.low | ((u64)hash.high << 32);
1215 }
1216 
1217 #define SUPPORTED_FIF_FLAGS \
1218     (FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
1219     FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
1220 static void zd_op_configure_filter(struct ieee80211_hw *hw,
1221             unsigned int changed_flags,
1222             unsigned int *new_flags,
1223             u64 multicast)
1224 {
1225     struct zd_mc_hash hash = {
1226         .low = multicast,
1227         .high = multicast >> 32,
1228     };
1229     struct zd_mac *mac = zd_hw_mac(hw);
1230     unsigned long flags;
1231     int r;
1232 
1233     /* Only deal with supported flags */
1234     changed_flags &= SUPPORTED_FIF_FLAGS;
1235     *new_flags &= SUPPORTED_FIF_FLAGS;
1236 
1237     /*
1238      * If multicast parameter (as returned by zd_op_prepare_multicast)
1239      * has changed, no bit in changed_flags is set. To handle this
1240      * situation, we do not return if changed_flags is 0. If we do so,
1241      * we will have some issue with IPv6 which uses multicast for link
1242      * layer address resolution.
1243      */
1244     if (*new_flags & FIF_ALLMULTI)
1245         zd_mc_add_all(&hash);
1246 
1247     spin_lock_irqsave(&mac->lock, flags);
1248     mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
1249     mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
1250     mac->multicast_hash = hash;
1251     spin_unlock_irqrestore(&mac->lock, flags);
1252 
1253     zd_chip_set_multicast_hash(&mac->chip, &hash);
1254 
1255     if (changed_flags & FIF_CONTROL) {
1256         r = set_rx_filter(mac);
1257         if (r)
1258             dev_err(zd_mac_dev(mac), "set_rx_filter error %d\n", r);
1259     }
1260 
1261     /* no handling required for FIF_OTHER_BSS as we don't currently
1262      * do BSSID filtering */
1263     /* FIXME: in future it would be nice to enable the probe response
1264      * filter (so that the driver doesn't see them) until
1265      * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
1266      * have to schedule work to enable prbresp reception, which might
1267      * happen too late. For now we'll just listen and forward them all the
1268      * time. */
1269 }
1270 
1271 static void set_rts_cts(struct zd_mac *mac, unsigned int short_preamble)
1272 {
1273     mutex_lock(&mac->chip.mutex);
1274     zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
1275     mutex_unlock(&mac->chip.mutex);
1276 }
1277 
1278 static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
1279                    struct ieee80211_vif *vif,
1280                    struct ieee80211_bss_conf *bss_conf,
1281                    u64 changes)
1282 {
1283     struct zd_mac *mac = zd_hw_mac(hw);
1284     int associated;
1285 
1286     dev_dbg_f(zd_mac_dev(mac), "changes: %llx\n", changes);
1287 
1288     if (mac->type == NL80211_IFTYPE_MESH_POINT ||
1289         mac->type == NL80211_IFTYPE_ADHOC ||
1290         mac->type == NL80211_IFTYPE_AP) {
1291         associated = true;
1292         if (changes & BSS_CHANGED_BEACON) {
1293             struct sk_buff *beacon = ieee80211_beacon_get(hw, vif,
1294                                       0);
1295 
1296             if (beacon) {
1297                 zd_chip_disable_hwint(&mac->chip);
1298                 zd_mac_config_beacon(hw, beacon, false);
1299                 zd_chip_enable_hwint(&mac->chip);
1300             }
1301         }
1302 
1303         if (changes & BSS_CHANGED_BEACON_ENABLED) {
1304             u16 interval = 0;
1305             u8 period = 0;
1306 
1307             if (bss_conf->enable_beacon) {
1308                 period = bss_conf->dtim_period;
1309                 interval = bss_conf->beacon_int;
1310             }
1311 
1312             spin_lock_irq(&mac->lock);
1313             mac->beacon.period = period;
1314             mac->beacon.interval = interval;
1315             mac->beacon.last_update = jiffies;
1316             spin_unlock_irq(&mac->lock);
1317 
1318             zd_set_beacon_interval(&mac->chip, interval, period,
1319                            mac->type);
1320         }
1321     } else
1322         associated = is_valid_ether_addr(bss_conf->bssid);
1323 
1324     spin_lock_irq(&mac->lock);
1325     mac->associated = associated;
1326     spin_unlock_irq(&mac->lock);
1327 
1328     /* TODO: do hardware bssid filtering */
1329 
1330     if (changes & BSS_CHANGED_ERP_PREAMBLE) {
1331         spin_lock_irq(&mac->lock);
1332         mac->short_preamble = bss_conf->use_short_preamble;
1333         spin_unlock_irq(&mac->lock);
1334 
1335         set_rts_cts(mac, bss_conf->use_short_preamble);
1336     }
1337 }
1338 
1339 static u64 zd_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1340 {
1341     struct zd_mac *mac = zd_hw_mac(hw);
1342     return zd_chip_get_tsf(&mac->chip);
1343 }
1344 
1345 static const struct ieee80211_ops zd_ops = {
1346     .tx         = zd_op_tx,
1347     .start          = zd_op_start,
1348     .stop           = zd_op_stop,
1349     .add_interface      = zd_op_add_interface,
1350     .remove_interface   = zd_op_remove_interface,
1351     .config         = zd_op_config,
1352     .prepare_multicast  = zd_op_prepare_multicast,
1353     .configure_filter   = zd_op_configure_filter,
1354     .bss_info_changed   = zd_op_bss_info_changed,
1355     .get_tsf        = zd_op_get_tsf,
1356 };
1357 
1358 struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
1359 {
1360     struct zd_mac *mac;
1361     struct ieee80211_hw *hw;
1362 
1363     hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
1364     if (!hw) {
1365         dev_dbg_f(&intf->dev, "out of memory\n");
1366         return NULL;
1367     }
1368 
1369     mac = zd_hw_mac(hw);
1370 
1371     memset(mac, 0, sizeof(*mac));
1372     spin_lock_init(&mac->lock);
1373     mac->hw = hw;
1374 
1375     mac->type = NL80211_IFTYPE_UNSPECIFIED;
1376 
1377     memcpy(mac->channels, zd_channels, sizeof(zd_channels));
1378     memcpy(mac->rates, zd_rates, sizeof(zd_rates));
1379     mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
1380     mac->band.bitrates = mac->rates;
1381     mac->band.n_channels = ARRAY_SIZE(zd_channels);
1382     mac->band.channels = mac->channels;
1383 
1384     hw->wiphy->bands[NL80211_BAND_2GHZ] = &mac->band;
1385 
1386     ieee80211_hw_set(hw, MFP_CAPABLE);
1387     ieee80211_hw_set(hw, HOST_BROADCAST_PS_BUFFERING);
1388     ieee80211_hw_set(hw, RX_INCLUDES_FCS);
1389     ieee80211_hw_set(hw, SIGNAL_UNSPEC);
1390 
1391     hw->wiphy->interface_modes =
1392         BIT(NL80211_IFTYPE_MESH_POINT) |
1393         BIT(NL80211_IFTYPE_STATION) |
1394         BIT(NL80211_IFTYPE_ADHOC) |
1395         BIT(NL80211_IFTYPE_AP);
1396 
1397     wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
1398 
1399     hw->max_signal = 100;
1400     hw->queues = 1;
1401     hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
1402 
1403     /*
1404      * Tell mac80211 that we support multi rate retries
1405      */
1406     hw->max_rates = IEEE80211_TX_MAX_RATES;
1407     hw->max_rate_tries = 18;    /* 9 rates * 2 retries/rate */
1408 
1409     skb_queue_head_init(&mac->ack_wait_queue);
1410     mac->ack_pending = 0;
1411 
1412     zd_chip_init(&mac->chip, hw, intf);
1413     housekeeping_init(mac);
1414     beacon_init(mac);
1415     INIT_WORK(&mac->process_intr, zd_process_intr);
1416 
1417     SET_IEEE80211_DEV(hw, &intf->dev);
1418     return hw;
1419 }
1420 
1421 #define BEACON_WATCHDOG_DELAY round_jiffies_relative(HZ)
1422 
1423 static void beacon_watchdog_handler(struct work_struct *work)
1424 {
1425     struct zd_mac *mac =
1426         container_of(work, struct zd_mac, beacon.watchdog_work.work);
1427     struct sk_buff *beacon;
1428     unsigned long timeout;
1429     int interval, period;
1430 
1431     if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
1432         goto rearm;
1433     if (mac->type != NL80211_IFTYPE_AP || !mac->vif)
1434         goto rearm;
1435 
1436     spin_lock_irq(&mac->lock);
1437     interval = mac->beacon.interval;
1438     period = mac->beacon.period;
1439     timeout = mac->beacon.last_update +
1440             msecs_to_jiffies(interval * 1024 / 1000) * 3;
1441     spin_unlock_irq(&mac->lock);
1442 
1443     if (interval > 0 && time_is_before_jiffies(timeout)) {
1444         dev_dbg_f(zd_mac_dev(mac), "beacon interrupt stalled, "
1445                        "restarting. "
1446                        "(interval: %d, dtim: %d)\n",
1447                        interval, period);
1448 
1449         zd_chip_disable_hwint(&mac->chip);
1450 
1451         beacon = ieee80211_beacon_get(mac->hw, mac->vif, 0);
1452         if (beacon) {
1453             zd_mac_free_cur_beacon(mac);
1454 
1455             zd_mac_config_beacon(mac->hw, beacon, false);
1456         }
1457 
1458         zd_set_beacon_interval(&mac->chip, interval, period, mac->type);
1459 
1460         zd_chip_enable_hwint(&mac->chip);
1461 
1462         spin_lock_irq(&mac->lock);
1463         mac->beacon.last_update = jiffies;
1464         spin_unlock_irq(&mac->lock);
1465     }
1466 
1467 rearm:
1468     queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work,
1469                BEACON_WATCHDOG_DELAY);
1470 }
1471 
1472 static void beacon_init(struct zd_mac *mac)
1473 {
1474     INIT_DELAYED_WORK(&mac->beacon.watchdog_work, beacon_watchdog_handler);
1475 }
1476 
1477 static void beacon_enable(struct zd_mac *mac)
1478 {
1479     dev_dbg_f(zd_mac_dev(mac), "\n");
1480 
1481     mac->beacon.last_update = jiffies;
1482     queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work,
1483                BEACON_WATCHDOG_DELAY);
1484 }
1485 
1486 static void beacon_disable(struct zd_mac *mac)
1487 {
1488     dev_dbg_f(zd_mac_dev(mac), "\n");
1489     cancel_delayed_work_sync(&mac->beacon.watchdog_work);
1490 
1491     zd_mac_free_cur_beacon(mac);
1492 }
1493 
1494 #define LINK_LED_WORK_DELAY HZ
1495 
1496 static void link_led_handler(struct work_struct *work)
1497 {
1498     struct zd_mac *mac =
1499         container_of(work, struct zd_mac, housekeeping.link_led_work.work);
1500     struct zd_chip *chip = &mac->chip;
1501     int is_associated;
1502     int r;
1503 
1504     if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
1505         goto requeue;
1506 
1507     spin_lock_irq(&mac->lock);
1508     is_associated = mac->associated;
1509     spin_unlock_irq(&mac->lock);
1510 
1511     r = zd_chip_control_leds(chip,
1512                          is_associated ? ZD_LED_ASSOCIATED : ZD_LED_SCANNING);
1513     if (r)
1514         dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1515 
1516 requeue:
1517     queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1518                    LINK_LED_WORK_DELAY);
1519 }
1520 
1521 static void housekeeping_init(struct zd_mac *mac)
1522 {
1523     INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
1524 }
1525 
1526 static void housekeeping_enable(struct zd_mac *mac)
1527 {
1528     dev_dbg_f(zd_mac_dev(mac), "\n");
1529     queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1530                0);
1531 }
1532 
1533 static void housekeeping_disable(struct zd_mac *mac)
1534 {
1535     dev_dbg_f(zd_mac_dev(mac), "\n");
1536     cancel_delayed_work_sync(&mac->housekeeping.link_led_work);
1537     zd_chip_control_leds(&mac->chip, ZD_LED_OFF);
1538 }