18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci	Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
48c2ecf20Sopenharmony_ci	<http://rt2x00.serialmonkey.com>
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci/*
98c2ecf20Sopenharmony_ci	Module: rt2x00lib
108c2ecf20Sopenharmony_ci	Abstract: rt2x00 generic link tuning routines.
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/kernel.h>
148c2ecf20Sopenharmony_ci#include <linux/module.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include "rt2x00.h"
178c2ecf20Sopenharmony_ci#include "rt2x00lib.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/*
208c2ecf20Sopenharmony_ci * When we lack RSSI information return something less then -80 to
218c2ecf20Sopenharmony_ci * tell the driver to tune the device to maximum sensitivity.
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci#define DEFAULT_RSSI		-128
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic inline int rt2x00link_get_avg_rssi(struct ewma_rssi *ewma)
268c2ecf20Sopenharmony_ci{
278c2ecf20Sopenharmony_ci	unsigned long avg;
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	avg = ewma_rssi_read(ewma);
308c2ecf20Sopenharmony_ci	if (avg)
318c2ecf20Sopenharmony_ci		return -avg;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	return DEFAULT_RSSI;
348c2ecf20Sopenharmony_ci}
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_cistatic int rt2x00link_antenna_get_link_rssi(struct rt2x00_dev *rt2x00dev)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	struct link_ant *ant = &rt2x00dev->link.ant;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	if (rt2x00dev->link.qual.rx_success)
418c2ecf20Sopenharmony_ci		return rt2x00link_get_avg_rssi(&ant->rssi_ant);
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	return DEFAULT_RSSI;
448c2ecf20Sopenharmony_ci}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic int rt2x00link_antenna_get_rssi_history(struct rt2x00_dev *rt2x00dev)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	struct link_ant *ant = &rt2x00dev->link.ant;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	if (ant->rssi_history)
518c2ecf20Sopenharmony_ci		return ant->rssi_history;
528c2ecf20Sopenharmony_ci	return DEFAULT_RSSI;
538c2ecf20Sopenharmony_ci}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistatic void rt2x00link_antenna_update_rssi_history(struct rt2x00_dev *rt2x00dev,
568c2ecf20Sopenharmony_ci						   int rssi)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	struct link_ant *ant = &rt2x00dev->link.ant;
598c2ecf20Sopenharmony_ci	ant->rssi_history = rssi;
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic void rt2x00link_antenna_reset(struct rt2x00_dev *rt2x00dev)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	ewma_rssi_init(&rt2x00dev->link.ant.rssi_ant);
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic void rt2x00lib_antenna_diversity_sample(struct rt2x00_dev *rt2x00dev)
688c2ecf20Sopenharmony_ci{
698c2ecf20Sopenharmony_ci	struct link_ant *ant = &rt2x00dev->link.ant;
708c2ecf20Sopenharmony_ci	struct antenna_setup new_ant;
718c2ecf20Sopenharmony_ci	int other_antenna;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	int sample_current = rt2x00link_antenna_get_link_rssi(rt2x00dev);
748c2ecf20Sopenharmony_ci	int sample_other = rt2x00link_antenna_get_rssi_history(rt2x00dev);
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	memcpy(&new_ant, &ant->active, sizeof(new_ant));
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	/*
798c2ecf20Sopenharmony_ci	 * We are done sampling. Now we should evaluate the results.
808c2ecf20Sopenharmony_ci	 */
818c2ecf20Sopenharmony_ci	ant->flags &= ~ANTENNA_MODE_SAMPLE;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	/*
848c2ecf20Sopenharmony_ci	 * During the last period we have sampled the RSSI
858c2ecf20Sopenharmony_ci	 * from both antennas. It now is time to determine
868c2ecf20Sopenharmony_ci	 * which antenna demonstrated the best performance.
878c2ecf20Sopenharmony_ci	 * When we are already on the antenna with the best
888c2ecf20Sopenharmony_ci	 * performance, just create a good starting point
898c2ecf20Sopenharmony_ci	 * for the history and we are done.
908c2ecf20Sopenharmony_ci	 */
918c2ecf20Sopenharmony_ci	if (sample_current >= sample_other) {
928c2ecf20Sopenharmony_ci		rt2x00link_antenna_update_rssi_history(rt2x00dev,
938c2ecf20Sopenharmony_ci			sample_current);
948c2ecf20Sopenharmony_ci		return;
958c2ecf20Sopenharmony_ci	}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	other_antenna = (ant->active.rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	if (ant->flags & ANTENNA_RX_DIVERSITY)
1008c2ecf20Sopenharmony_ci		new_ant.rx = other_antenna;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	if (ant->flags & ANTENNA_TX_DIVERSITY)
1038c2ecf20Sopenharmony_ci		new_ant.tx = other_antenna;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	rt2x00lib_config_antenna(rt2x00dev, new_ant);
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic void rt2x00lib_antenna_diversity_eval(struct rt2x00_dev *rt2x00dev)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	struct link_ant *ant = &rt2x00dev->link.ant;
1118c2ecf20Sopenharmony_ci	struct antenna_setup new_ant;
1128c2ecf20Sopenharmony_ci	int rssi_curr;
1138c2ecf20Sopenharmony_ci	int rssi_old;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	memcpy(&new_ant, &ant->active, sizeof(new_ant));
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	/*
1188c2ecf20Sopenharmony_ci	 * Get current RSSI value along with the historical value,
1198c2ecf20Sopenharmony_ci	 * after that update the history with the current value.
1208c2ecf20Sopenharmony_ci	 */
1218c2ecf20Sopenharmony_ci	rssi_curr = rt2x00link_antenna_get_link_rssi(rt2x00dev);
1228c2ecf20Sopenharmony_ci	rssi_old = rt2x00link_antenna_get_rssi_history(rt2x00dev);
1238c2ecf20Sopenharmony_ci	rt2x00link_antenna_update_rssi_history(rt2x00dev, rssi_curr);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	/*
1268c2ecf20Sopenharmony_ci	 * Legacy driver indicates that we should swap antenna's
1278c2ecf20Sopenharmony_ci	 * when the difference in RSSI is greater that 5. This
1288c2ecf20Sopenharmony_ci	 * also should be done when the RSSI was actually better
1298c2ecf20Sopenharmony_ci	 * then the previous sample.
1308c2ecf20Sopenharmony_ci	 * When the difference exceeds the threshold we should
1318c2ecf20Sopenharmony_ci	 * sample the rssi from the other antenna to make a valid
1328c2ecf20Sopenharmony_ci	 * comparison between the 2 antennas.
1338c2ecf20Sopenharmony_ci	 */
1348c2ecf20Sopenharmony_ci	if (abs(rssi_curr - rssi_old) < 5)
1358c2ecf20Sopenharmony_ci		return;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	ant->flags |= ANTENNA_MODE_SAMPLE;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	if (ant->flags & ANTENNA_RX_DIVERSITY)
1408c2ecf20Sopenharmony_ci		new_ant.rx = (new_ant.rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	if (ant->flags & ANTENNA_TX_DIVERSITY)
1438c2ecf20Sopenharmony_ci		new_ant.tx = (new_ant.tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	rt2x00lib_config_antenna(rt2x00dev, new_ant);
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_cistatic bool rt2x00lib_antenna_diversity(struct rt2x00_dev *rt2x00dev)
1498c2ecf20Sopenharmony_ci{
1508c2ecf20Sopenharmony_ci	struct link_ant *ant = &rt2x00dev->link.ant;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	/*
1538c2ecf20Sopenharmony_ci	 * Determine if software diversity is enabled for
1548c2ecf20Sopenharmony_ci	 * either the TX or RX antenna (or both).
1558c2ecf20Sopenharmony_ci	 */
1568c2ecf20Sopenharmony_ci	if (!(ant->flags & ANTENNA_RX_DIVERSITY) &&
1578c2ecf20Sopenharmony_ci	    !(ant->flags & ANTENNA_TX_DIVERSITY)) {
1588c2ecf20Sopenharmony_ci		ant->flags = 0;
1598c2ecf20Sopenharmony_ci		return true;
1608c2ecf20Sopenharmony_ci	}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	/*
1638c2ecf20Sopenharmony_ci	 * If we have only sampled the data over the last period
1648c2ecf20Sopenharmony_ci	 * we should now harvest the data. Otherwise just evaluate
1658c2ecf20Sopenharmony_ci	 * the data. The latter should only be performed once
1668c2ecf20Sopenharmony_ci	 * every 2 seconds.
1678c2ecf20Sopenharmony_ci	 */
1688c2ecf20Sopenharmony_ci	if (ant->flags & ANTENNA_MODE_SAMPLE) {
1698c2ecf20Sopenharmony_ci		rt2x00lib_antenna_diversity_sample(rt2x00dev);
1708c2ecf20Sopenharmony_ci		return true;
1718c2ecf20Sopenharmony_ci	} else if (rt2x00dev->link.count & 1) {
1728c2ecf20Sopenharmony_ci		rt2x00lib_antenna_diversity_eval(rt2x00dev);
1738c2ecf20Sopenharmony_ci		return true;
1748c2ecf20Sopenharmony_ci	}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	return false;
1778c2ecf20Sopenharmony_ci}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_civoid rt2x00link_update_stats(struct rt2x00_dev *rt2x00dev,
1808c2ecf20Sopenharmony_ci			     struct sk_buff *skb,
1818c2ecf20Sopenharmony_ci			     struct rxdone_entry_desc *rxdesc)
1828c2ecf20Sopenharmony_ci{
1838c2ecf20Sopenharmony_ci	struct link *link = &rt2x00dev->link;
1848c2ecf20Sopenharmony_ci	struct link_qual *qual = &rt2x00dev->link.qual;
1858c2ecf20Sopenharmony_ci	struct link_ant *ant = &rt2x00dev->link.ant;
1868c2ecf20Sopenharmony_ci	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	/*
1898c2ecf20Sopenharmony_ci	 * No need to update the stats for !=STA interfaces
1908c2ecf20Sopenharmony_ci	 */
1918c2ecf20Sopenharmony_ci	if (!rt2x00dev->intf_sta_count)
1928c2ecf20Sopenharmony_ci		return;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	/*
1958c2ecf20Sopenharmony_ci	 * Frame was received successfully since non-succesfull
1968c2ecf20Sopenharmony_ci	 * frames would have been dropped by the hardware.
1978c2ecf20Sopenharmony_ci	 */
1988c2ecf20Sopenharmony_ci	qual->rx_success++;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	/*
2018c2ecf20Sopenharmony_ci	 * We are only interested in quality statistics from
2028c2ecf20Sopenharmony_ci	 * beacons which came from the BSS which we are
2038c2ecf20Sopenharmony_ci	 * associated with.
2048c2ecf20Sopenharmony_ci	 */
2058c2ecf20Sopenharmony_ci	if (!ieee80211_is_beacon(hdr->frame_control) ||
2068c2ecf20Sopenharmony_ci	    !(rxdesc->dev_flags & RXDONE_MY_BSS))
2078c2ecf20Sopenharmony_ci		return;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	/*
2108c2ecf20Sopenharmony_ci	 * Update global RSSI
2118c2ecf20Sopenharmony_ci	 */
2128c2ecf20Sopenharmony_ci	ewma_rssi_add(&link->avg_rssi, -rxdesc->rssi);
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	/*
2158c2ecf20Sopenharmony_ci	 * Update antenna RSSI
2168c2ecf20Sopenharmony_ci	 */
2178c2ecf20Sopenharmony_ci	ewma_rssi_add(&ant->rssi_ant, -rxdesc->rssi);
2188c2ecf20Sopenharmony_ci}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_civoid rt2x00link_start_tuner(struct rt2x00_dev *rt2x00dev)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	struct link *link = &rt2x00dev->link;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	/*
2258c2ecf20Sopenharmony_ci	 * Single monitor mode interfaces should never have
2268c2ecf20Sopenharmony_ci	 * work with link tuners.
2278c2ecf20Sopenharmony_ci	 */
2288c2ecf20Sopenharmony_ci	if (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count)
2298c2ecf20Sopenharmony_ci		return;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	/*
2328c2ecf20Sopenharmony_ci	 * While scanning, link tuning is disabled. By default
2338c2ecf20Sopenharmony_ci	 * the most sensitive settings will be used to make sure
2348c2ecf20Sopenharmony_ci	 * that all beacons and probe responses will be received
2358c2ecf20Sopenharmony_ci	 * during the scan.
2368c2ecf20Sopenharmony_ci	 */
2378c2ecf20Sopenharmony_ci	if (test_bit(DEVICE_STATE_SCANNING, &rt2x00dev->flags))
2388c2ecf20Sopenharmony_ci		return;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	rt2x00link_reset_tuner(rt2x00dev, false);
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
2438c2ecf20Sopenharmony_ci		ieee80211_queue_delayed_work(rt2x00dev->hw,
2448c2ecf20Sopenharmony_ci					     &link->work, LINK_TUNE_INTERVAL);
2458c2ecf20Sopenharmony_ci}
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_civoid rt2x00link_stop_tuner(struct rt2x00_dev *rt2x00dev)
2488c2ecf20Sopenharmony_ci{
2498c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&rt2x00dev->link.work);
2508c2ecf20Sopenharmony_ci}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_civoid rt2x00link_reset_tuner(struct rt2x00_dev *rt2x00dev, bool antenna)
2538c2ecf20Sopenharmony_ci{
2548c2ecf20Sopenharmony_ci	struct link_qual *qual = &rt2x00dev->link.qual;
2558c2ecf20Sopenharmony_ci	u8 vgc_level = qual->vgc_level_reg;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
2588c2ecf20Sopenharmony_ci		return;
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	/*
2618c2ecf20Sopenharmony_ci	 * Reset link information.
2628c2ecf20Sopenharmony_ci	 * Both the currently active vgc level as well as
2638c2ecf20Sopenharmony_ci	 * the link tuner counter should be reset. Resetting
2648c2ecf20Sopenharmony_ci	 * the counter is important for devices where the
2658c2ecf20Sopenharmony_ci	 * device should only perform link tuning during the
2668c2ecf20Sopenharmony_ci	 * first minute after being enabled.
2678c2ecf20Sopenharmony_ci	 */
2688c2ecf20Sopenharmony_ci	rt2x00dev->link.count = 0;
2698c2ecf20Sopenharmony_ci	memset(qual, 0, sizeof(*qual));
2708c2ecf20Sopenharmony_ci	ewma_rssi_init(&rt2x00dev->link.avg_rssi);
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	/*
2738c2ecf20Sopenharmony_ci	 * Restore the VGC level as stored in the registers,
2748c2ecf20Sopenharmony_ci	 * the driver can use this to determine if the register
2758c2ecf20Sopenharmony_ci	 * must be updated during reset or not.
2768c2ecf20Sopenharmony_ci	 */
2778c2ecf20Sopenharmony_ci	qual->vgc_level_reg = vgc_level;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	/*
2808c2ecf20Sopenharmony_ci	 * Reset the link tuner.
2818c2ecf20Sopenharmony_ci	 */
2828c2ecf20Sopenharmony_ci	rt2x00dev->ops->lib->reset_tuner(rt2x00dev, qual);
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	if (antenna)
2858c2ecf20Sopenharmony_ci		rt2x00link_antenna_reset(rt2x00dev);
2868c2ecf20Sopenharmony_ci}
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_cistatic void rt2x00link_reset_qual(struct rt2x00_dev *rt2x00dev)
2898c2ecf20Sopenharmony_ci{
2908c2ecf20Sopenharmony_ci	struct link_qual *qual = &rt2x00dev->link.qual;
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	qual->rx_success = 0;
2938c2ecf20Sopenharmony_ci	qual->rx_failed = 0;
2948c2ecf20Sopenharmony_ci	qual->tx_success = 0;
2958c2ecf20Sopenharmony_ci	qual->tx_failed = 0;
2968c2ecf20Sopenharmony_ci}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_cistatic void rt2x00link_tuner_sta(struct rt2x00_dev *rt2x00dev, struct link *link)
2998c2ecf20Sopenharmony_ci{
3008c2ecf20Sopenharmony_ci	struct link_qual *qual = &rt2x00dev->link.qual;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	/*
3038c2ecf20Sopenharmony_ci	 * Update statistics.
3048c2ecf20Sopenharmony_ci	 */
3058c2ecf20Sopenharmony_ci	rt2x00dev->ops->lib->link_stats(rt2x00dev, qual);
3068c2ecf20Sopenharmony_ci	rt2x00dev->low_level_stats.dot11FCSErrorCount += qual->rx_failed;
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	/*
3098c2ecf20Sopenharmony_ci	 * Update quality RSSI for link tuning,
3108c2ecf20Sopenharmony_ci	 * when we have received some frames and we managed to
3118c2ecf20Sopenharmony_ci	 * collect the RSSI data we could use this. Otherwise we
3128c2ecf20Sopenharmony_ci	 * must fallback to the default RSSI value.
3138c2ecf20Sopenharmony_ci	 */
3148c2ecf20Sopenharmony_ci	if (!qual->rx_success)
3158c2ecf20Sopenharmony_ci		qual->rssi = DEFAULT_RSSI;
3168c2ecf20Sopenharmony_ci	else
3178c2ecf20Sopenharmony_ci		qual->rssi = rt2x00link_get_avg_rssi(&link->avg_rssi);
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	/*
3208c2ecf20Sopenharmony_ci	 * Check if link tuning is supported by the hardware, some hardware
3218c2ecf20Sopenharmony_ci	 * do not support link tuning at all, while other devices can disable
3228c2ecf20Sopenharmony_ci	 * the feature from the EEPROM.
3238c2ecf20Sopenharmony_ci	 */
3248c2ecf20Sopenharmony_ci	if (rt2x00_has_cap_link_tuning(rt2x00dev))
3258c2ecf20Sopenharmony_ci		rt2x00dev->ops->lib->link_tuner(rt2x00dev, qual, link->count);
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	/*
3288c2ecf20Sopenharmony_ci	 * Send a signal to the led to update the led signal strength.
3298c2ecf20Sopenharmony_ci	 */
3308c2ecf20Sopenharmony_ci	rt2x00leds_led_quality(rt2x00dev, qual->rssi);
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	/*
3338c2ecf20Sopenharmony_ci	 * Evaluate antenna setup, make this the last step when
3348c2ecf20Sopenharmony_ci	 * rt2x00lib_antenna_diversity made changes the quality
3358c2ecf20Sopenharmony_ci	 * statistics will be reset.
3368c2ecf20Sopenharmony_ci	 */
3378c2ecf20Sopenharmony_ci	if (rt2x00lib_antenna_diversity(rt2x00dev))
3388c2ecf20Sopenharmony_ci		rt2x00link_reset_qual(rt2x00dev);
3398c2ecf20Sopenharmony_ci}
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_cistatic void rt2x00link_tuner(struct work_struct *work)
3428c2ecf20Sopenharmony_ci{
3438c2ecf20Sopenharmony_ci	struct rt2x00_dev *rt2x00dev =
3448c2ecf20Sopenharmony_ci	    container_of(work, struct rt2x00_dev, link.work.work);
3458c2ecf20Sopenharmony_ci	struct link *link = &rt2x00dev->link;
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	/*
3488c2ecf20Sopenharmony_ci	 * When the radio is shutting down we should
3498c2ecf20Sopenharmony_ci	 * immediately cease all link tuning.
3508c2ecf20Sopenharmony_ci	 */
3518c2ecf20Sopenharmony_ci	if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags) ||
3528c2ecf20Sopenharmony_ci	    test_bit(DEVICE_STATE_SCANNING, &rt2x00dev->flags))
3538c2ecf20Sopenharmony_ci		return;
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	/* Do not race with rt2x00mac_config(). */
3568c2ecf20Sopenharmony_ci	mutex_lock(&rt2x00dev->conf_mutex);
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	if (rt2x00dev->intf_sta_count)
3598c2ecf20Sopenharmony_ci		rt2x00link_tuner_sta(rt2x00dev, link);
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	if (rt2x00dev->ops->lib->gain_calibration &&
3628c2ecf20Sopenharmony_ci	    (link->count % (AGC_SECONDS / LINK_TUNE_SECONDS)) == 0)
3638c2ecf20Sopenharmony_ci		rt2x00dev->ops->lib->gain_calibration(rt2x00dev);
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	if (rt2x00dev->ops->lib->vco_calibration &&
3668c2ecf20Sopenharmony_ci	    rt2x00_has_cap_vco_recalibration(rt2x00dev) &&
3678c2ecf20Sopenharmony_ci	    (link->count % (VCO_SECONDS / LINK_TUNE_SECONDS)) == 0)
3688c2ecf20Sopenharmony_ci		rt2x00dev->ops->lib->vco_calibration(rt2x00dev);
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	mutex_unlock(&rt2x00dev->conf_mutex);
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	/*
3738c2ecf20Sopenharmony_ci	 * Increase tuner counter, and reschedule the next link tuner run.
3748c2ecf20Sopenharmony_ci	 */
3758c2ecf20Sopenharmony_ci	link->count++;
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
3788c2ecf20Sopenharmony_ci		ieee80211_queue_delayed_work(rt2x00dev->hw,
3798c2ecf20Sopenharmony_ci					     &link->work, LINK_TUNE_INTERVAL);
3808c2ecf20Sopenharmony_ci}
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_civoid rt2x00link_start_watchdog(struct rt2x00_dev *rt2x00dev)
3838c2ecf20Sopenharmony_ci{
3848c2ecf20Sopenharmony_ci	struct link *link = &rt2x00dev->link;
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) &&
3878c2ecf20Sopenharmony_ci	    rt2x00dev->ops->lib->watchdog && !link->watchdog_disabled)
3888c2ecf20Sopenharmony_ci		ieee80211_queue_delayed_work(rt2x00dev->hw,
3898c2ecf20Sopenharmony_ci					     &link->watchdog_work,
3908c2ecf20Sopenharmony_ci					     link->watchdog_interval);
3918c2ecf20Sopenharmony_ci}
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_civoid rt2x00link_stop_watchdog(struct rt2x00_dev *rt2x00dev)
3948c2ecf20Sopenharmony_ci{
3958c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&rt2x00dev->link.watchdog_work);
3968c2ecf20Sopenharmony_ci}
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_cistatic void rt2x00link_watchdog(struct work_struct *work)
3998c2ecf20Sopenharmony_ci{
4008c2ecf20Sopenharmony_ci	struct rt2x00_dev *rt2x00dev =
4018c2ecf20Sopenharmony_ci	    container_of(work, struct rt2x00_dev, link.watchdog_work.work);
4028c2ecf20Sopenharmony_ci	struct link *link = &rt2x00dev->link;
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	/*
4058c2ecf20Sopenharmony_ci	 * When the radio is shutting down we should
4068c2ecf20Sopenharmony_ci	 * immediately cease the watchdog monitoring.
4078c2ecf20Sopenharmony_ci	 */
4088c2ecf20Sopenharmony_ci	if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
4098c2ecf20Sopenharmony_ci		return;
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	rt2x00dev->ops->lib->watchdog(rt2x00dev);
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
4148c2ecf20Sopenharmony_ci		ieee80211_queue_delayed_work(rt2x00dev->hw,
4158c2ecf20Sopenharmony_ci					     &link->watchdog_work,
4168c2ecf20Sopenharmony_ci					     link->watchdog_interval);
4178c2ecf20Sopenharmony_ci}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_civoid rt2x00link_register(struct rt2x00_dev *rt2x00dev)
4208c2ecf20Sopenharmony_ci{
4218c2ecf20Sopenharmony_ci	struct link *link = &rt2x00dev->link;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&link->work, rt2x00link_tuner);
4248c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&link->watchdog_work, rt2x00link_watchdog);
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	if (link->watchdog_interval == 0)
4278c2ecf20Sopenharmony_ci		link->watchdog_interval = WATCHDOG_INTERVAL;
4288c2ecf20Sopenharmony_ci}
429