Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003     Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
0004     <http://rt2x00.serialmonkey.com>
0005 
0006  */
0007 
0008 /*
0009     Module: rt2x00mac
0010     Abstract: rt2x00 generic mac80211 routines.
0011  */
0012 
0013 #include <linux/kernel.h>
0014 #include <linux/module.h>
0015 
0016 #include "rt2x00.h"
0017 #include "rt2x00lib.h"
0018 
0019 static int rt2x00mac_tx_rts_cts(struct rt2x00_dev *rt2x00dev,
0020                 struct data_queue *queue,
0021                 struct sk_buff *frag_skb)
0022 {
0023     struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(frag_skb);
0024     struct ieee80211_tx_info *rts_info;
0025     struct sk_buff *skb;
0026     unsigned int data_length;
0027     int retval = 0;
0028 
0029     if (tx_info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
0030         data_length = sizeof(struct ieee80211_cts);
0031     else
0032         data_length = sizeof(struct ieee80211_rts);
0033 
0034     skb = dev_alloc_skb(data_length + rt2x00dev->hw->extra_tx_headroom);
0035     if (unlikely(!skb)) {
0036         rt2x00_warn(rt2x00dev, "Failed to create RTS/CTS frame\n");
0037         return -ENOMEM;
0038     }
0039 
0040     skb_reserve(skb, rt2x00dev->hw->extra_tx_headroom);
0041     skb_put(skb, data_length);
0042 
0043     /*
0044      * Copy TX information over from original frame to
0045      * RTS/CTS frame. Note that we set the no encryption flag
0046      * since we don't want this frame to be encrypted.
0047      * RTS frames should be acked, while CTS-to-self frames
0048      * should not. The ready for TX flag is cleared to prevent
0049      * it being automatically send when the descriptor is
0050      * written to the hardware.
0051      */
0052     memcpy(skb->cb, frag_skb->cb, sizeof(skb->cb));
0053     rts_info = IEEE80211_SKB_CB(skb);
0054     rts_info->control.rates[0].flags &= ~IEEE80211_TX_RC_USE_RTS_CTS;
0055     rts_info->control.rates[0].flags &= ~IEEE80211_TX_RC_USE_CTS_PROTECT;
0056 
0057     if (tx_info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
0058         rts_info->flags |= IEEE80211_TX_CTL_NO_ACK;
0059     else
0060         rts_info->flags &= ~IEEE80211_TX_CTL_NO_ACK;
0061 
0062     /* Disable hardware encryption */
0063     rts_info->control.hw_key = NULL;
0064 
0065     /*
0066      * RTS/CTS frame should use the length of the frame plus any
0067      * encryption overhead that will be added by the hardware.
0068      */
0069     data_length += rt2x00crypto_tx_overhead(rt2x00dev, skb);
0070 
0071     if (tx_info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
0072         ieee80211_ctstoself_get(rt2x00dev->hw, tx_info->control.vif,
0073                     frag_skb->data, data_length, tx_info,
0074                     (struct ieee80211_cts *)(skb->data));
0075     else
0076         ieee80211_rts_get(rt2x00dev->hw, tx_info->control.vif,
0077                   frag_skb->data, data_length, tx_info,
0078                   (struct ieee80211_rts *)(skb->data));
0079 
0080     retval = rt2x00queue_write_tx_frame(queue, skb, NULL, true);
0081     if (retval) {
0082         dev_kfree_skb_any(skb);
0083         rt2x00_warn(rt2x00dev, "Failed to send RTS/CTS frame\n");
0084     }
0085 
0086     return retval;
0087 }
0088 
0089 void rt2x00mac_tx(struct ieee80211_hw *hw,
0090           struct ieee80211_tx_control *control,
0091           struct sk_buff *skb)
0092 {
0093     struct rt2x00_dev *rt2x00dev = hw->priv;
0094     struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
0095     enum data_queue_qid qid = skb_get_queue_mapping(skb);
0096     struct data_queue *queue = NULL;
0097 
0098     /*
0099      * Mac80211 might be calling this function while we are trying
0100      * to remove the device or perhaps suspending it.
0101      * Note that we can only stop the TX queues inside the TX path
0102      * due to possible race conditions in mac80211.
0103      */
0104     if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
0105         goto exit_free_skb;
0106 
0107     /*
0108      * Use the ATIM queue if appropriate and present.
0109      */
0110     if (tx_info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM &&
0111         rt2x00_has_cap_flag(rt2x00dev, REQUIRE_ATIM_QUEUE))
0112         qid = QID_ATIM;
0113 
0114     queue = rt2x00queue_get_tx_queue(rt2x00dev, qid);
0115     if (unlikely(!queue)) {
0116         rt2x00_err(rt2x00dev,
0117                "Attempt to send packet over invalid queue %d\n"
0118                "Please file bug report to %s\n", qid, DRV_PROJECT);
0119         goto exit_free_skb;
0120     }
0121 
0122     /*
0123      * If CTS/RTS is required. create and queue that frame first.
0124      * Make sure we have at least enough entries available to send
0125      * this CTS/RTS frame as well as the data frame.
0126      * Note that when the driver has set the set_rts_threshold()
0127      * callback function it doesn't need software generation of
0128      * either RTS or CTS-to-self frame and handles everything
0129      * inside the hardware.
0130      */
0131     if (!rt2x00dev->ops->hw->set_rts_threshold &&
0132         (tx_info->control.rates[0].flags & (IEEE80211_TX_RC_USE_RTS_CTS |
0133                         IEEE80211_TX_RC_USE_CTS_PROTECT))) {
0134         if (rt2x00queue_available(queue) <= 1) {
0135             /*
0136              * Recheck for full queue under lock to avoid race
0137              * conditions with rt2x00lib_txdone().
0138              */
0139             spin_lock(&queue->tx_lock);
0140             if (rt2x00queue_threshold(queue))
0141                 rt2x00queue_pause_queue(queue);
0142             spin_unlock(&queue->tx_lock);
0143 
0144             goto exit_free_skb;
0145         }
0146 
0147         if (rt2x00mac_tx_rts_cts(rt2x00dev, queue, skb))
0148             goto exit_free_skb;
0149     }
0150 
0151     if (unlikely(rt2x00queue_write_tx_frame(queue, skb, control->sta, false)))
0152         goto exit_free_skb;
0153 
0154     return;
0155 
0156  exit_free_skb:
0157     ieee80211_free_txskb(hw, skb);
0158 }
0159 EXPORT_SYMBOL_GPL(rt2x00mac_tx);
0160 
0161 int rt2x00mac_start(struct ieee80211_hw *hw)
0162 {
0163     struct rt2x00_dev *rt2x00dev = hw->priv;
0164 
0165     if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
0166         return 0;
0167 
0168     if (test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags)) {
0169         /*
0170          * This is special case for ieee80211_restart_hw(), otherwise
0171          * mac80211 never call start() two times in row without stop();
0172          */
0173         set_bit(DEVICE_STATE_RESET, &rt2x00dev->flags);
0174         rt2x00dev->ops->lib->pre_reset_hw(rt2x00dev);
0175         rt2x00lib_stop(rt2x00dev);
0176     }
0177     return rt2x00lib_start(rt2x00dev);
0178 }
0179 EXPORT_SYMBOL_GPL(rt2x00mac_start);
0180 
0181 void rt2x00mac_stop(struct ieee80211_hw *hw)
0182 {
0183     struct rt2x00_dev *rt2x00dev = hw->priv;
0184 
0185     if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
0186         return;
0187 
0188     rt2x00lib_stop(rt2x00dev);
0189 }
0190 EXPORT_SYMBOL_GPL(rt2x00mac_stop);
0191 
0192 void
0193 rt2x00mac_reconfig_complete(struct ieee80211_hw *hw,
0194                 enum ieee80211_reconfig_type reconfig_type)
0195 {
0196     struct rt2x00_dev *rt2x00dev = hw->priv;
0197 
0198     if (reconfig_type == IEEE80211_RECONFIG_TYPE_RESTART)
0199         clear_bit(DEVICE_STATE_RESET, &rt2x00dev->flags);
0200 }
0201 EXPORT_SYMBOL_GPL(rt2x00mac_reconfig_complete);
0202 
0203 int rt2x00mac_add_interface(struct ieee80211_hw *hw,
0204                 struct ieee80211_vif *vif)
0205 {
0206     struct rt2x00_dev *rt2x00dev = hw->priv;
0207     struct rt2x00_intf *intf = vif_to_intf(vif);
0208     struct data_queue *queue = rt2x00dev->bcn;
0209     struct queue_entry *entry = NULL;
0210     unsigned int i;
0211 
0212     /*
0213      * Don't allow interfaces to be added
0214      * the device has disappeared.
0215      */
0216     if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) ||
0217         !test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
0218         return -ENODEV;
0219 
0220     /*
0221      * Loop through all beacon queues to find a free
0222      * entry. Since there are as much beacon entries
0223      * as the maximum interfaces, this search shouldn't
0224      * fail.
0225      */
0226     for (i = 0; i < queue->limit; i++) {
0227         entry = &queue->entries[i];
0228         if (!test_and_set_bit(ENTRY_BCN_ASSIGNED, &entry->flags))
0229             break;
0230     }
0231 
0232     if (unlikely(i == queue->limit))
0233         return -ENOBUFS;
0234 
0235     /*
0236      * We are now absolutely sure the interface can be created,
0237      * increase interface count and start initialization.
0238      */
0239 
0240     if (vif->type == NL80211_IFTYPE_AP)
0241         rt2x00dev->intf_ap_count++;
0242     else
0243         rt2x00dev->intf_sta_count++;
0244 
0245     mutex_init(&intf->beacon_skb_mutex);
0246     intf->beacon = entry;
0247 
0248     /*
0249      * The MAC address must be configured after the device
0250      * has been initialized. Otherwise the device can reset
0251      * the MAC registers.
0252      * The BSSID address must only be configured in AP mode,
0253      * however we should not send an empty BSSID address for
0254      * STA interfaces at this time, since this can cause
0255      * invalid behavior in the device.
0256      */
0257     rt2x00lib_config_intf(rt2x00dev, intf, vif->type,
0258                   vif->addr, NULL);
0259 
0260     /*
0261      * Some filters depend on the current working mode. We can force
0262      * an update during the next configure_filter() run by mac80211 by
0263      * resetting the current packet_filter state.
0264      */
0265     rt2x00dev->packet_filter = 0;
0266 
0267     return 0;
0268 }
0269 EXPORT_SYMBOL_GPL(rt2x00mac_add_interface);
0270 
0271 void rt2x00mac_remove_interface(struct ieee80211_hw *hw,
0272                 struct ieee80211_vif *vif)
0273 {
0274     struct rt2x00_dev *rt2x00dev = hw->priv;
0275     struct rt2x00_intf *intf = vif_to_intf(vif);
0276 
0277     /*
0278      * Don't allow interfaces to be remove while
0279      * either the device has disappeared or when
0280      * no interface is present.
0281      */
0282     if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) ||
0283         (vif->type == NL80211_IFTYPE_AP && !rt2x00dev->intf_ap_count) ||
0284         (vif->type != NL80211_IFTYPE_AP && !rt2x00dev->intf_sta_count))
0285         return;
0286 
0287     if (vif->type == NL80211_IFTYPE_AP)
0288         rt2x00dev->intf_ap_count--;
0289     else
0290         rt2x00dev->intf_sta_count--;
0291 
0292     /*
0293      * Release beacon entry so it is available for
0294      * new interfaces again.
0295      */
0296     clear_bit(ENTRY_BCN_ASSIGNED, &intf->beacon->flags);
0297 
0298     /*
0299      * Make sure the bssid and mac address registers
0300      * are cleared to prevent false ACKing of frames.
0301      */
0302     rt2x00lib_config_intf(rt2x00dev, intf,
0303                   NL80211_IFTYPE_UNSPECIFIED, NULL, NULL);
0304 }
0305 EXPORT_SYMBOL_GPL(rt2x00mac_remove_interface);
0306 
0307 int rt2x00mac_config(struct ieee80211_hw *hw, u32 changed)
0308 {
0309     struct rt2x00_dev *rt2x00dev = hw->priv;
0310     struct ieee80211_conf *conf = &hw->conf;
0311 
0312     /*
0313      * mac80211 might be calling this function while we are trying
0314      * to remove the device or perhaps suspending it.
0315      */
0316     if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
0317         return 0;
0318 
0319     /*
0320      * Some configuration parameters (e.g. channel and antenna values) can
0321      * only be set when the radio is enabled, but do require the RX to
0322      * be off. During this period we should keep link tuning enabled,
0323      * if for any reason the link tuner must be reset, this will be
0324      * handled by rt2x00lib_config().
0325      */
0326     rt2x00queue_stop_queue(rt2x00dev->rx);
0327 
0328     /* Do not race with link tuner. */
0329     mutex_lock(&rt2x00dev->conf_mutex);
0330 
0331     /*
0332      * When we've just turned on the radio, we want to reprogram
0333      * everything to ensure a consistent state
0334      */
0335     rt2x00lib_config(rt2x00dev, conf, changed);
0336 
0337     /*
0338      * After the radio has been enabled we need to configure
0339      * the antenna to the default settings. rt2x00lib_config_antenna()
0340      * should determine if any action should be taken based on
0341      * checking if diversity has been enabled or no antenna changes
0342      * have been made since the last configuration change.
0343      */
0344     rt2x00lib_config_antenna(rt2x00dev, rt2x00dev->default_ant);
0345 
0346     mutex_unlock(&rt2x00dev->conf_mutex);
0347 
0348     /* Turn RX back on */
0349     rt2x00queue_start_queue(rt2x00dev->rx);
0350 
0351     return 0;
0352 }
0353 EXPORT_SYMBOL_GPL(rt2x00mac_config);
0354 
0355 void rt2x00mac_configure_filter(struct ieee80211_hw *hw,
0356                 unsigned int changed_flags,
0357                 unsigned int *total_flags,
0358                 u64 multicast)
0359 {
0360     struct rt2x00_dev *rt2x00dev = hw->priv;
0361 
0362     /*
0363      * Mask off any flags we are going to ignore
0364      * from the total_flags field.
0365      */
0366     *total_flags &=
0367         FIF_ALLMULTI |
0368         FIF_FCSFAIL |
0369         FIF_PLCPFAIL |
0370         FIF_CONTROL |
0371         FIF_PSPOLL |
0372         FIF_OTHER_BSS;
0373 
0374     /*
0375      * Apply some rules to the filters:
0376      * - Some filters imply different filters to be set.
0377      * - Some things we can't filter out at all.
0378      * - Multicast filter seems to kill broadcast traffic so never use it.
0379      */
0380     *total_flags |= FIF_ALLMULTI;
0381 
0382     /*
0383      * If the device has a single filter for all control frames,
0384      * FIF_CONTROL and FIF_PSPOLL flags imply each other.
0385      * And if the device has more than one filter for control frames
0386      * of different types, but has no a separate filter for PS Poll frames,
0387      * FIF_CONTROL flag implies FIF_PSPOLL.
0388      */
0389     if (!rt2x00_has_cap_control_filters(rt2x00dev)) {
0390         if (*total_flags & FIF_CONTROL || *total_flags & FIF_PSPOLL)
0391             *total_flags |= FIF_CONTROL | FIF_PSPOLL;
0392     }
0393     if (!rt2x00_has_cap_control_filter_pspoll(rt2x00dev)) {
0394         if (*total_flags & FIF_CONTROL)
0395             *total_flags |= FIF_PSPOLL;
0396     }
0397 
0398     rt2x00dev->packet_filter = *total_flags;
0399 
0400     rt2x00dev->ops->lib->config_filter(rt2x00dev, *total_flags);
0401 }
0402 EXPORT_SYMBOL_GPL(rt2x00mac_configure_filter);
0403 
0404 static void rt2x00mac_set_tim_iter(void *data, u8 *mac,
0405                    struct ieee80211_vif *vif)
0406 {
0407     struct rt2x00_intf *intf = vif_to_intf(vif);
0408 
0409     if (vif->type != NL80211_IFTYPE_AP &&
0410         vif->type != NL80211_IFTYPE_ADHOC &&
0411         vif->type != NL80211_IFTYPE_MESH_POINT)
0412         return;
0413 
0414     set_bit(DELAYED_UPDATE_BEACON, &intf->delayed_flags);
0415 }
0416 
0417 int rt2x00mac_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
0418               bool set)
0419 {
0420     struct rt2x00_dev *rt2x00dev = hw->priv;
0421 
0422     if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
0423         return 0;
0424 
0425     ieee80211_iterate_active_interfaces_atomic(
0426         rt2x00dev->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
0427         rt2x00mac_set_tim_iter, rt2x00dev);
0428 
0429     /* queue work to upodate the beacon template */
0430     ieee80211_queue_work(rt2x00dev->hw, &rt2x00dev->intf_work);
0431     return 0;
0432 }
0433 EXPORT_SYMBOL_GPL(rt2x00mac_set_tim);
0434 
0435 #ifdef CONFIG_RT2X00_LIB_CRYPTO
0436 static void memcpy_tkip(struct rt2x00lib_crypto *crypto, u8 *key, u8 key_len)
0437 {
0438     if (key_len > NL80211_TKIP_DATA_OFFSET_ENCR_KEY)
0439         memcpy(crypto->key,
0440                &key[NL80211_TKIP_DATA_OFFSET_ENCR_KEY],
0441                sizeof(crypto->key));
0442 
0443     if (key_len > NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY)
0444         memcpy(crypto->tx_mic,
0445                &key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
0446                sizeof(crypto->tx_mic));
0447 
0448     if (key_len > NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY)
0449         memcpy(crypto->rx_mic,
0450                &key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
0451                sizeof(crypto->rx_mic));
0452 }
0453 
0454 int rt2x00mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
0455               struct ieee80211_vif *vif, struct ieee80211_sta *sta,
0456               struct ieee80211_key_conf *key)
0457 {
0458     struct rt2x00_dev *rt2x00dev = hw->priv;
0459     int (*set_key) (struct rt2x00_dev *rt2x00dev,
0460             struct rt2x00lib_crypto *crypto,
0461             struct ieee80211_key_conf *key);
0462     struct rt2x00lib_crypto crypto;
0463     static const u8 bcast_addr[ETH_ALEN] =
0464         { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, };
0465     struct rt2x00_sta *sta_priv = NULL;
0466 
0467     if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
0468         return 0;
0469 
0470     /* The hardware can't do MFP */
0471     if (!rt2x00_has_cap_hw_crypto(rt2x00dev) || (sta && sta->mfp))
0472         return -EOPNOTSUPP;
0473 
0474     /*
0475      * To support IBSS RSN, don't program group keys in IBSS, the
0476      * hardware will then not attempt to decrypt the frames.
0477      */
0478     if (vif->type == NL80211_IFTYPE_ADHOC &&
0479         !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
0480         return -EOPNOTSUPP;
0481 
0482     if (key->keylen > 32)
0483         return -ENOSPC;
0484 
0485     memset(&crypto, 0, sizeof(crypto));
0486 
0487     crypto.bssidx = rt2x00lib_get_bssidx(rt2x00dev, vif);
0488     crypto.cipher = rt2x00crypto_key_to_cipher(key);
0489     if (crypto.cipher == CIPHER_NONE)
0490         return -EOPNOTSUPP;
0491     if (crypto.cipher == CIPHER_TKIP && rt2x00_is_usb(rt2x00dev))
0492         return -EOPNOTSUPP;
0493 
0494     crypto.cmd = cmd;
0495 
0496     if (sta) {
0497         crypto.address = sta->addr;
0498         sta_priv = sta_to_rt2x00_sta(sta);
0499         crypto.wcid = sta_priv->wcid;
0500     } else
0501         crypto.address = bcast_addr;
0502 
0503     if (crypto.cipher == CIPHER_TKIP)
0504         memcpy_tkip(&crypto, &key->key[0], key->keylen);
0505     else
0506         memcpy(crypto.key, &key->key[0], key->keylen);
0507     /*
0508      * Each BSS has a maximum of 4 shared keys.
0509      * Shared key index values:
0510      *  0) BSS0 key0
0511      *  1) BSS0 key1
0512      *  ...
0513      *  4) BSS1 key0
0514      *  ...
0515      *  8) BSS2 key0
0516      *  ...
0517      * Both pairwise as shared key indeces are determined by
0518      * driver. This is required because the hardware requires
0519      * keys to be assigned in correct order (When key 1 is
0520      * provided but key 0 is not, then the key is not found
0521      * by the hardware during RX).
0522      */
0523     if (cmd == SET_KEY)
0524         key->hw_key_idx = 0;
0525 
0526     if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
0527         set_key = rt2x00dev->ops->lib->config_pairwise_key;
0528     else
0529         set_key = rt2x00dev->ops->lib->config_shared_key;
0530 
0531     if (!set_key)
0532         return -EOPNOTSUPP;
0533 
0534     return set_key(rt2x00dev, &crypto, key);
0535 }
0536 EXPORT_SYMBOL_GPL(rt2x00mac_set_key);
0537 #endif /* CONFIG_RT2X00_LIB_CRYPTO */
0538 
0539 void rt2x00mac_sw_scan_start(struct ieee80211_hw *hw,
0540                  struct ieee80211_vif *vif,
0541                  const u8 *mac_addr)
0542 {
0543     struct rt2x00_dev *rt2x00dev = hw->priv;
0544     set_bit(DEVICE_STATE_SCANNING, &rt2x00dev->flags);
0545     rt2x00link_stop_tuner(rt2x00dev);
0546 }
0547 EXPORT_SYMBOL_GPL(rt2x00mac_sw_scan_start);
0548 
0549 void rt2x00mac_sw_scan_complete(struct ieee80211_hw *hw,
0550                 struct ieee80211_vif *vif)
0551 {
0552     struct rt2x00_dev *rt2x00dev = hw->priv;
0553     clear_bit(DEVICE_STATE_SCANNING, &rt2x00dev->flags);
0554     rt2x00link_start_tuner(rt2x00dev);
0555 }
0556 EXPORT_SYMBOL_GPL(rt2x00mac_sw_scan_complete);
0557 
0558 int rt2x00mac_get_stats(struct ieee80211_hw *hw,
0559             struct ieee80211_low_level_stats *stats)
0560 {
0561     struct rt2x00_dev *rt2x00dev = hw->priv;
0562 
0563     /*
0564      * The dot11ACKFailureCount, dot11RTSFailureCount and
0565      * dot11RTSSuccessCount are updated in interrupt time.
0566      * dot11FCSErrorCount is updated in the link tuner.
0567      */
0568     memcpy(stats, &rt2x00dev->low_level_stats, sizeof(*stats));
0569 
0570     return 0;
0571 }
0572 EXPORT_SYMBOL_GPL(rt2x00mac_get_stats);
0573 
0574 void rt2x00mac_bss_info_changed(struct ieee80211_hw *hw,
0575                 struct ieee80211_vif *vif,
0576                 struct ieee80211_bss_conf *bss_conf,
0577                 u64 changes)
0578 {
0579     struct rt2x00_dev *rt2x00dev = hw->priv;
0580     struct rt2x00_intf *intf = vif_to_intf(vif);
0581 
0582     /*
0583      * mac80211 might be calling this function while we are trying
0584      * to remove the device or perhaps suspending it.
0585      */
0586     if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
0587         return;
0588 
0589     /*
0590      * Update the BSSID.
0591      */
0592     if (changes & BSS_CHANGED_BSSID)
0593         rt2x00lib_config_intf(rt2x00dev, intf, vif->type, NULL,
0594                       bss_conf->bssid);
0595 
0596     /*
0597      * Start/stop beaconing.
0598      */
0599     if (changes & BSS_CHANGED_BEACON_ENABLED) {
0600         mutex_lock(&intf->beacon_skb_mutex);
0601         if (!bss_conf->enable_beacon && intf->enable_beacon) {
0602             rt2x00dev->intf_beaconing--;
0603             intf->enable_beacon = false;
0604 
0605             if (rt2x00dev->intf_beaconing == 0) {
0606                 /*
0607                  * Last beaconing interface disabled
0608                  * -> stop beacon queue.
0609                  */
0610                 rt2x00queue_stop_queue(rt2x00dev->bcn);
0611             }
0612             /*
0613              * Clear beacon in the H/W for this vif. This is needed
0614              * to disable beaconing on this particular interface
0615              * and keep it running on other interfaces.
0616              */
0617             rt2x00queue_clear_beacon(rt2x00dev, vif);
0618         } else if (bss_conf->enable_beacon && !intf->enable_beacon) {
0619             rt2x00dev->intf_beaconing++;
0620             intf->enable_beacon = true;
0621             /*
0622              * Upload beacon to the H/W. This is only required on
0623              * USB devices. PCI devices fetch beacons periodically.
0624              */
0625             if (rt2x00_is_usb(rt2x00dev))
0626                 rt2x00queue_update_beacon(rt2x00dev, vif);
0627 
0628             if (rt2x00dev->intf_beaconing == 1) {
0629                 /*
0630                  * First beaconing interface enabled
0631                  * -> start beacon queue.
0632                  */
0633                 rt2x00queue_start_queue(rt2x00dev->bcn);
0634             }
0635         }
0636         mutex_unlock(&intf->beacon_skb_mutex);
0637     }
0638 
0639     /*
0640      * When the association status has changed we must reset the link
0641      * tuner counter. This is because some drivers determine if they
0642      * should perform link tuning based on the number of seconds
0643      * while associated or not associated.
0644      */
0645     if (changes & BSS_CHANGED_ASSOC) {
0646         rt2x00dev->link.count = 0;
0647 
0648         if (vif->cfg.assoc)
0649             rt2x00dev->intf_associated++;
0650         else
0651             rt2x00dev->intf_associated--;
0652 
0653         rt2x00leds_led_assoc(rt2x00dev, !!rt2x00dev->intf_associated);
0654     }
0655 
0656     /*
0657      * When the erp information has changed, we should perform
0658      * additional configuration steps. For all other changes we are done.
0659      */
0660     if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_ERP_PREAMBLE |
0661                BSS_CHANGED_ERP_SLOT | BSS_CHANGED_BASIC_RATES |
0662                BSS_CHANGED_BEACON_INT | BSS_CHANGED_HT))
0663         rt2x00lib_config_erp(rt2x00dev, intf, bss_conf, changes);
0664 }
0665 EXPORT_SYMBOL_GPL(rt2x00mac_bss_info_changed);
0666 
0667 int rt2x00mac_conf_tx(struct ieee80211_hw *hw,
0668               struct ieee80211_vif *vif,
0669               unsigned int link_id, u16 queue_idx,
0670               const struct ieee80211_tx_queue_params *params)
0671 {
0672     struct rt2x00_dev *rt2x00dev = hw->priv;
0673     struct data_queue *queue;
0674 
0675     queue = rt2x00queue_get_tx_queue(rt2x00dev, queue_idx);
0676     if (unlikely(!queue))
0677         return -EINVAL;
0678 
0679     /*
0680      * The passed variables are stored as real value ((2^n)-1).
0681      * Ralink registers require to know the bit number 'n'.
0682      */
0683     if (params->cw_min > 0)
0684         queue->cw_min = fls(params->cw_min);
0685     else
0686         queue->cw_min = 5; /* cw_min: 2^5 = 32. */
0687 
0688     if (params->cw_max > 0)
0689         queue->cw_max = fls(params->cw_max);
0690     else
0691         queue->cw_max = 10; /* cw_min: 2^10 = 1024. */
0692 
0693     queue->aifs = params->aifs;
0694     queue->txop = params->txop;
0695 
0696     rt2x00_dbg(rt2x00dev,
0697            "Configured TX queue %d - CWmin: %d, CWmax: %d, Aifs: %d, TXop: %d\n",
0698            queue_idx, queue->cw_min, queue->cw_max, queue->aifs,
0699            queue->txop);
0700 
0701     return 0;
0702 }
0703 EXPORT_SYMBOL_GPL(rt2x00mac_conf_tx);
0704 
0705 void rt2x00mac_rfkill_poll(struct ieee80211_hw *hw)
0706 {
0707     struct rt2x00_dev *rt2x00dev = hw->priv;
0708     bool active = !!rt2x00dev->ops->lib->rfkill_poll(rt2x00dev);
0709 
0710     wiphy_rfkill_set_hw_state(hw->wiphy, !active);
0711 }
0712 EXPORT_SYMBOL_GPL(rt2x00mac_rfkill_poll);
0713 
0714 void rt2x00mac_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
0715              u32 queues, bool drop)
0716 {
0717     struct rt2x00_dev *rt2x00dev = hw->priv;
0718     struct data_queue *queue;
0719 
0720     if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
0721         return;
0722 
0723     set_bit(DEVICE_STATE_FLUSHING, &rt2x00dev->flags);
0724 
0725     tx_queue_for_each(rt2x00dev, queue)
0726         rt2x00queue_flush_queue(queue, drop);
0727 
0728     clear_bit(DEVICE_STATE_FLUSHING, &rt2x00dev->flags);
0729 }
0730 EXPORT_SYMBOL_GPL(rt2x00mac_flush);
0731 
0732 int rt2x00mac_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
0733 {
0734     struct rt2x00_dev *rt2x00dev = hw->priv;
0735     struct link_ant *ant = &rt2x00dev->link.ant;
0736     struct antenna_setup *def = &rt2x00dev->default_ant;
0737     struct antenna_setup setup;
0738 
0739     // The antenna value is not supposed to be 0,
0740     // or exceed the maximum number of antenna's.
0741     if (!tx_ant || (tx_ant & ~3) || !rx_ant || (rx_ant & ~3))
0742         return -EINVAL;
0743 
0744     // When the client tried to configure the antenna to or from
0745     // diversity mode, we must reset the default antenna as well
0746     // as that controls the diversity switch.
0747     if (ant->flags & ANTENNA_TX_DIVERSITY && tx_ant != 3)
0748         ant->flags &= ~ANTENNA_TX_DIVERSITY;
0749     if (ant->flags & ANTENNA_RX_DIVERSITY && rx_ant != 3)
0750         ant->flags &= ~ANTENNA_RX_DIVERSITY;
0751 
0752     // If diversity is being enabled, check if we need hardware
0753     // or software diversity. In the latter case, reset the value,
0754     // and make sure we update the antenna flags to have the
0755     // link tuner pick up the diversity tuning.
0756     if (tx_ant == 3 && def->tx == ANTENNA_SW_DIVERSITY) {
0757         tx_ant = ANTENNA_SW_DIVERSITY;
0758         ant->flags |= ANTENNA_TX_DIVERSITY;
0759     }
0760 
0761     if (rx_ant == 3 && def->rx == ANTENNA_SW_DIVERSITY) {
0762         rx_ant = ANTENNA_SW_DIVERSITY;
0763         ant->flags |= ANTENNA_RX_DIVERSITY;
0764     }
0765 
0766     setup.tx = tx_ant;
0767     setup.rx = rx_ant;
0768     setup.rx_chain_num = 0;
0769     setup.tx_chain_num = 0;
0770 
0771     rt2x00lib_config_antenna(rt2x00dev, setup);
0772 
0773     return 0;
0774 }
0775 EXPORT_SYMBOL_GPL(rt2x00mac_set_antenna);
0776 
0777 int rt2x00mac_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
0778 {
0779     struct rt2x00_dev *rt2x00dev = hw->priv;
0780     struct link_ant *ant = &rt2x00dev->link.ant;
0781     struct antenna_setup *active = &rt2x00dev->link.ant.active;
0782 
0783     // When software diversity is active, we must report this to the
0784     // client and not the current active antenna state.
0785     if (ant->flags & ANTENNA_TX_DIVERSITY)
0786         *tx_ant = ANTENNA_HW_DIVERSITY;
0787     else
0788         *tx_ant = active->tx;
0789 
0790     if (ant->flags & ANTENNA_RX_DIVERSITY)
0791         *rx_ant = ANTENNA_HW_DIVERSITY;
0792     else
0793         *rx_ant = active->rx;
0794 
0795     return 0;
0796 }
0797 EXPORT_SYMBOL_GPL(rt2x00mac_get_antenna);
0798 
0799 void rt2x00mac_get_ringparam(struct ieee80211_hw *hw,
0800                  u32 *tx, u32 *tx_max, u32 *rx, u32 *rx_max)
0801 {
0802     struct rt2x00_dev *rt2x00dev = hw->priv;
0803     struct data_queue *queue;
0804 
0805     tx_queue_for_each(rt2x00dev, queue) {
0806         *tx += queue->length;
0807         *tx_max += queue->limit;
0808     }
0809 
0810     *rx = rt2x00dev->rx->length;
0811     *rx_max = rt2x00dev->rx->limit;
0812 }
0813 EXPORT_SYMBOL_GPL(rt2x00mac_get_ringparam);
0814 
0815 bool rt2x00mac_tx_frames_pending(struct ieee80211_hw *hw)
0816 {
0817     struct rt2x00_dev *rt2x00dev = hw->priv;
0818     struct data_queue *queue;
0819 
0820     tx_queue_for_each(rt2x00dev, queue) {
0821         if (!rt2x00queue_empty(queue))
0822             return true;
0823     }
0824 
0825     return false;
0826 }
0827 EXPORT_SYMBOL_GPL(rt2x00mac_tx_frames_pending);