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: rt2x00lib
0010     Abstract: rt2x00 generic link tuning routines.
0011  */
0012 
0013 #include <linux/kernel.h>
0014 #include <linux/module.h>
0015 
0016 #include "rt2x00.h"
0017 #include "rt2x00lib.h"
0018 
0019 /*
0020  * When we lack RSSI information return something less then -80 to
0021  * tell the driver to tune the device to maximum sensitivity.
0022  */
0023 #define DEFAULT_RSSI        -128
0024 
0025 static inline int rt2x00link_get_avg_rssi(struct ewma_rssi *ewma)
0026 {
0027     unsigned long avg;
0028 
0029     avg = ewma_rssi_read(ewma);
0030     if (avg)
0031         return -avg;
0032 
0033     return DEFAULT_RSSI;
0034 }
0035 
0036 static int rt2x00link_antenna_get_link_rssi(struct rt2x00_dev *rt2x00dev)
0037 {
0038     struct link_ant *ant = &rt2x00dev->link.ant;
0039 
0040     if (rt2x00dev->link.qual.rx_success)
0041         return rt2x00link_get_avg_rssi(&ant->rssi_ant);
0042 
0043     return DEFAULT_RSSI;
0044 }
0045 
0046 static int rt2x00link_antenna_get_rssi_history(struct rt2x00_dev *rt2x00dev)
0047 {
0048     struct link_ant *ant = &rt2x00dev->link.ant;
0049 
0050     if (ant->rssi_history)
0051         return ant->rssi_history;
0052     return DEFAULT_RSSI;
0053 }
0054 
0055 static void rt2x00link_antenna_update_rssi_history(struct rt2x00_dev *rt2x00dev,
0056                            int rssi)
0057 {
0058     struct link_ant *ant = &rt2x00dev->link.ant;
0059     ant->rssi_history = rssi;
0060 }
0061 
0062 static void rt2x00link_antenna_reset(struct rt2x00_dev *rt2x00dev)
0063 {
0064     ewma_rssi_init(&rt2x00dev->link.ant.rssi_ant);
0065 }
0066 
0067 static void rt2x00lib_antenna_diversity_sample(struct rt2x00_dev *rt2x00dev)
0068 {
0069     struct link_ant *ant = &rt2x00dev->link.ant;
0070     struct antenna_setup new_ant;
0071     int other_antenna;
0072 
0073     int sample_current = rt2x00link_antenna_get_link_rssi(rt2x00dev);
0074     int sample_other = rt2x00link_antenna_get_rssi_history(rt2x00dev);
0075 
0076     memcpy(&new_ant, &ant->active, sizeof(new_ant));
0077 
0078     /*
0079      * We are done sampling. Now we should evaluate the results.
0080      */
0081     ant->flags &= ~ANTENNA_MODE_SAMPLE;
0082 
0083     /*
0084      * During the last period we have sampled the RSSI
0085      * from both antennas. It now is time to determine
0086      * which antenna demonstrated the best performance.
0087      * When we are already on the antenna with the best
0088      * performance, just create a good starting point
0089      * for the history and we are done.
0090      */
0091     if (sample_current >= sample_other) {
0092         rt2x00link_antenna_update_rssi_history(rt2x00dev,
0093             sample_current);
0094         return;
0095     }
0096 
0097     other_antenna = (ant->active.rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
0098 
0099     if (ant->flags & ANTENNA_RX_DIVERSITY)
0100         new_ant.rx = other_antenna;
0101 
0102     if (ant->flags & ANTENNA_TX_DIVERSITY)
0103         new_ant.tx = other_antenna;
0104 
0105     rt2x00lib_config_antenna(rt2x00dev, new_ant);
0106 }
0107 
0108 static void rt2x00lib_antenna_diversity_eval(struct rt2x00_dev *rt2x00dev)
0109 {
0110     struct link_ant *ant = &rt2x00dev->link.ant;
0111     struct antenna_setup new_ant;
0112     int rssi_curr;
0113     int rssi_old;
0114 
0115     memcpy(&new_ant, &ant->active, sizeof(new_ant));
0116 
0117     /*
0118      * Get current RSSI value along with the historical value,
0119      * after that update the history with the current value.
0120      */
0121     rssi_curr = rt2x00link_antenna_get_link_rssi(rt2x00dev);
0122     rssi_old = rt2x00link_antenna_get_rssi_history(rt2x00dev);
0123     rt2x00link_antenna_update_rssi_history(rt2x00dev, rssi_curr);
0124 
0125     /*
0126      * Legacy driver indicates that we should swap antenna's
0127      * when the difference in RSSI is greater that 5. This
0128      * also should be done when the RSSI was actually better
0129      * then the previous sample.
0130      * When the difference exceeds the threshold we should
0131      * sample the rssi from the other antenna to make a valid
0132      * comparison between the 2 antennas.
0133      */
0134     if (abs(rssi_curr - rssi_old) < 5)
0135         return;
0136 
0137     ant->flags |= ANTENNA_MODE_SAMPLE;
0138 
0139     if (ant->flags & ANTENNA_RX_DIVERSITY)
0140         new_ant.rx = (new_ant.rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
0141 
0142     if (ant->flags & ANTENNA_TX_DIVERSITY)
0143         new_ant.tx = (new_ant.tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
0144 
0145     rt2x00lib_config_antenna(rt2x00dev, new_ant);
0146 }
0147 
0148 static bool rt2x00lib_antenna_diversity(struct rt2x00_dev *rt2x00dev)
0149 {
0150     struct link_ant *ant = &rt2x00dev->link.ant;
0151 
0152     /*
0153      * Determine if software diversity is enabled for
0154      * either the TX or RX antenna (or both).
0155      */
0156     if (!(ant->flags & ANTENNA_RX_DIVERSITY) &&
0157         !(ant->flags & ANTENNA_TX_DIVERSITY)) {
0158         ant->flags = 0;
0159         return true;
0160     }
0161 
0162     /*
0163      * If we have only sampled the data over the last period
0164      * we should now harvest the data. Otherwise just evaluate
0165      * the data. The latter should only be performed once
0166      * every 2 seconds.
0167      */
0168     if (ant->flags & ANTENNA_MODE_SAMPLE) {
0169         rt2x00lib_antenna_diversity_sample(rt2x00dev);
0170         return true;
0171     } else if (rt2x00dev->link.count & 1) {
0172         rt2x00lib_antenna_diversity_eval(rt2x00dev);
0173         return true;
0174     }
0175 
0176     return false;
0177 }
0178 
0179 void rt2x00link_update_stats(struct rt2x00_dev *rt2x00dev,
0180                  struct sk_buff *skb,
0181                  struct rxdone_entry_desc *rxdesc)
0182 {
0183     struct link *link = &rt2x00dev->link;
0184     struct link_qual *qual = &rt2x00dev->link.qual;
0185     struct link_ant *ant = &rt2x00dev->link.ant;
0186     struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
0187 
0188     /*
0189      * No need to update the stats for !=STA interfaces
0190      */
0191     if (!rt2x00dev->intf_sta_count)
0192         return;
0193 
0194     /*
0195      * Frame was received successfully since non-succesfull
0196      * frames would have been dropped by the hardware.
0197      */
0198     qual->rx_success++;
0199 
0200     /*
0201      * We are only interested in quality statistics from
0202      * beacons which came from the BSS which we are
0203      * associated with.
0204      */
0205     if (!ieee80211_is_beacon(hdr->frame_control) ||
0206         !(rxdesc->dev_flags & RXDONE_MY_BSS))
0207         return;
0208 
0209     /*
0210      * Update global RSSI
0211      */
0212     ewma_rssi_add(&link->avg_rssi, -rxdesc->rssi);
0213 
0214     /*
0215      * Update antenna RSSI
0216      */
0217     ewma_rssi_add(&ant->rssi_ant, -rxdesc->rssi);
0218 }
0219 
0220 void rt2x00link_start_tuner(struct rt2x00_dev *rt2x00dev)
0221 {
0222     struct link *link = &rt2x00dev->link;
0223 
0224     /*
0225      * Single monitor mode interfaces should never have
0226      * work with link tuners.
0227      */
0228     if (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count)
0229         return;
0230 
0231     /*
0232      * While scanning, link tuning is disabled. By default
0233      * the most sensitive settings will be used to make sure
0234      * that all beacons and probe responses will be received
0235      * during the scan.
0236      */
0237     if (test_bit(DEVICE_STATE_SCANNING, &rt2x00dev->flags))
0238         return;
0239 
0240     rt2x00link_reset_tuner(rt2x00dev, false);
0241 
0242     if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
0243         ieee80211_queue_delayed_work(rt2x00dev->hw,
0244                          &link->work, LINK_TUNE_INTERVAL);
0245 }
0246 
0247 void rt2x00link_stop_tuner(struct rt2x00_dev *rt2x00dev)
0248 {
0249     cancel_delayed_work_sync(&rt2x00dev->link.work);
0250 }
0251 
0252 void rt2x00link_reset_tuner(struct rt2x00_dev *rt2x00dev, bool antenna)
0253 {
0254     struct link_qual *qual = &rt2x00dev->link.qual;
0255     u8 vgc_level = qual->vgc_level_reg;
0256 
0257     if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
0258         return;
0259 
0260     /*
0261      * Reset link information.
0262      * Both the currently active vgc level as well as
0263      * the link tuner counter should be reset. Resetting
0264      * the counter is important for devices where the
0265      * device should only perform link tuning during the
0266      * first minute after being enabled.
0267      */
0268     rt2x00dev->link.count = 0;
0269     memset(qual, 0, sizeof(*qual));
0270     ewma_rssi_init(&rt2x00dev->link.avg_rssi);
0271 
0272     /*
0273      * Restore the VGC level as stored in the registers,
0274      * the driver can use this to determine if the register
0275      * must be updated during reset or not.
0276      */
0277     qual->vgc_level_reg = vgc_level;
0278 
0279     /*
0280      * Reset the link tuner.
0281      */
0282     rt2x00dev->ops->lib->reset_tuner(rt2x00dev, qual);
0283 
0284     if (antenna)
0285         rt2x00link_antenna_reset(rt2x00dev);
0286 }
0287 
0288 static void rt2x00link_reset_qual(struct rt2x00_dev *rt2x00dev)
0289 {
0290     struct link_qual *qual = &rt2x00dev->link.qual;
0291 
0292     qual->rx_success = 0;
0293     qual->rx_failed = 0;
0294     qual->tx_success = 0;
0295     qual->tx_failed = 0;
0296 }
0297 
0298 static void rt2x00link_tuner_sta(struct rt2x00_dev *rt2x00dev, struct link *link)
0299 {
0300     struct link_qual *qual = &rt2x00dev->link.qual;
0301 
0302     /*
0303      * Update statistics.
0304      */
0305     rt2x00dev->ops->lib->link_stats(rt2x00dev, qual);
0306     rt2x00dev->low_level_stats.dot11FCSErrorCount += qual->rx_failed;
0307 
0308     /*
0309      * Update quality RSSI for link tuning,
0310      * when we have received some frames and we managed to
0311      * collect the RSSI data we could use this. Otherwise we
0312      * must fallback to the default RSSI value.
0313      */
0314     if (!qual->rx_success)
0315         qual->rssi = DEFAULT_RSSI;
0316     else
0317         qual->rssi = rt2x00link_get_avg_rssi(&link->avg_rssi);
0318 
0319     /*
0320      * Check if link tuning is supported by the hardware, some hardware
0321      * do not support link tuning at all, while other devices can disable
0322      * the feature from the EEPROM.
0323      */
0324     if (rt2x00_has_cap_link_tuning(rt2x00dev))
0325         rt2x00dev->ops->lib->link_tuner(rt2x00dev, qual, link->count);
0326 
0327     /*
0328      * Send a signal to the led to update the led signal strength.
0329      */
0330     rt2x00leds_led_quality(rt2x00dev, qual->rssi);
0331 
0332     /*
0333      * Evaluate antenna setup, make this the last step when
0334      * rt2x00lib_antenna_diversity made changes the quality
0335      * statistics will be reset.
0336      */
0337     if (rt2x00lib_antenna_diversity(rt2x00dev))
0338         rt2x00link_reset_qual(rt2x00dev);
0339 }
0340 
0341 static void rt2x00link_tuner(struct work_struct *work)
0342 {
0343     struct rt2x00_dev *rt2x00dev =
0344         container_of(work, struct rt2x00_dev, link.work.work);
0345     struct link *link = &rt2x00dev->link;
0346 
0347     /*
0348      * When the radio is shutting down we should
0349      * immediately cease all link tuning.
0350      */
0351     if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags) ||
0352         test_bit(DEVICE_STATE_SCANNING, &rt2x00dev->flags))
0353         return;
0354 
0355     /* Do not race with rt2x00mac_config(). */
0356     mutex_lock(&rt2x00dev->conf_mutex);
0357 
0358     if (rt2x00dev->intf_sta_count)
0359         rt2x00link_tuner_sta(rt2x00dev, link);
0360 
0361     if (rt2x00dev->ops->lib->gain_calibration &&
0362         (link->count % (AGC_SECONDS / LINK_TUNE_SECONDS)) == 0)
0363         rt2x00dev->ops->lib->gain_calibration(rt2x00dev);
0364 
0365     if (rt2x00dev->ops->lib->vco_calibration &&
0366         rt2x00_has_cap_vco_recalibration(rt2x00dev) &&
0367         (link->count % (VCO_SECONDS / LINK_TUNE_SECONDS)) == 0)
0368         rt2x00dev->ops->lib->vco_calibration(rt2x00dev);
0369 
0370     mutex_unlock(&rt2x00dev->conf_mutex);
0371 
0372     /*
0373      * Increase tuner counter, and reschedule the next link tuner run.
0374      */
0375     link->count++;
0376 
0377     if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
0378         ieee80211_queue_delayed_work(rt2x00dev->hw,
0379                          &link->work, LINK_TUNE_INTERVAL);
0380 }
0381 
0382 void rt2x00link_start_watchdog(struct rt2x00_dev *rt2x00dev)
0383 {
0384     struct link *link = &rt2x00dev->link;
0385 
0386     if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) &&
0387         rt2x00dev->ops->lib->watchdog && !link->watchdog_disabled)
0388         ieee80211_queue_delayed_work(rt2x00dev->hw,
0389                          &link->watchdog_work,
0390                          link->watchdog_interval);
0391 }
0392 
0393 void rt2x00link_stop_watchdog(struct rt2x00_dev *rt2x00dev)
0394 {
0395     cancel_delayed_work_sync(&rt2x00dev->link.watchdog_work);
0396 }
0397 
0398 static void rt2x00link_watchdog(struct work_struct *work)
0399 {
0400     struct rt2x00_dev *rt2x00dev =
0401         container_of(work, struct rt2x00_dev, link.watchdog_work.work);
0402     struct link *link = &rt2x00dev->link;
0403 
0404     /*
0405      * When the radio is shutting down we should
0406      * immediately cease the watchdog monitoring.
0407      */
0408     if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
0409         return;
0410 
0411     rt2x00dev->ops->lib->watchdog(rt2x00dev);
0412 
0413     if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
0414         ieee80211_queue_delayed_work(rt2x00dev->hw,
0415                          &link->watchdog_work,
0416                          link->watchdog_interval);
0417 }
0418 
0419 void rt2x00link_register(struct rt2x00_dev *rt2x00dev)
0420 {
0421     struct link *link = &rt2x00dev->link;
0422 
0423     INIT_DELAYED_WORK(&link->work, rt2x00link_tuner);
0424     INIT_DELAYED_WORK(&link->watchdog_work, rt2x00link_watchdog);
0425 
0426     if (link->watchdog_interval == 0)
0427         link->watchdog_interval = WATCHDOG_INTERVAL;
0428 }