2 Copyright (C) 2004 - 2008 rt2x00 SourceForge Project
3 <http://rt2x00.serialmonkey.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the
17 Free Software Foundation, Inc.,
18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 Abstract: rt2x00 generic link tuning routines.
26 #include <linux/kernel.h>
27 #include <linux/module.h>
30 #include "rt2x00lib.h"
32 static int rt2x00link_antenna_get_link_rssi(struct rt2x00_dev *rt2x00dev)
34 struct link_ant *ant = &rt2x00dev->link.ant;
36 if (ant->rssi_ant && rt2x00dev->link.qual.rx_success)
41 static int rt2x00link_antenna_get_rssi_history(struct rt2x00_dev *rt2x00dev,
44 struct link_ant *ant = &rt2x00dev->link.ant;
46 if (ant->rssi_history[antenna - ANTENNA_A])
47 return ant->rssi_history[antenna - ANTENNA_A];
50 /* Small wrapper for rt2x00link_antenna_get_rssi_history() */
51 #define rt2x00link_antenna_get_rssi_rx_history(__dev) \
52 rt2x00link_antenna_get_rssi_history((__dev), \
53 (__dev)->link.ant.active.rx)
54 #define rt2x00link_antenna_get_rssi_tx_history(__dev) \
55 rt2x00link_antenna_get_rssi_history((__dev), \
56 (__dev)->link.ant.active.tx)
58 static void rt2x00link_antenna_update_rssi_history(struct rt2x00_dev *rt2x00dev,
62 struct link_ant *ant = &rt2x00dev->link.ant;
63 ant->rssi_history[ant->active.rx - ANTENNA_A] = rssi;
65 /* Small wrapper for rt2x00link_antenna_get_rssi_history() */
66 #define rt2x00link_antenna_update_rssi_rx_history(__dev, __rssi) \
67 rt2x00link_antenna_update_rssi_history((__dev), \
68 (__dev)->link.ant.active.rx, \
70 #define rt2x00link_antenna_update_rssi_tx_history(__dev, __rssi) \
71 rt2x00link_antenna_update_rssi_history((__dev), \
72 (__dev)->link.ant.active.tx, \
75 static void rt2x00link_antenna_reset(struct rt2x00_dev *rt2x00dev)
77 rt2x00dev->link.ant.rssi_ant = 0;
80 static void rt2x00lib_antenna_diversity_sample(struct rt2x00_dev *rt2x00dev)
82 struct link_ant *ant = &rt2x00dev->link.ant;
83 struct antenna_setup new_ant;
84 int sample_a = rt2x00link_antenna_get_rssi_history(rt2x00dev, ANTENNA_A);
85 int sample_b = rt2x00link_antenna_get_rssi_history(rt2x00dev, ANTENNA_B);
87 memcpy(&new_ant, &ant->active, sizeof(new_ant));
90 * We are done sampling. Now we should evaluate the results.
92 ant->flags &= ~ANTENNA_MODE_SAMPLE;
95 * During the last period we have sampled the RSSI
96 * from both antenna's. It now is time to determine
97 * which antenna demonstrated the best performance.
98 * When we are already on the antenna with the best
99 * performance, then there really is nothing for us
102 if (sample_a == sample_b)
105 if (ant->flags & ANTENNA_RX_DIVERSITY)
106 new_ant.rx = (sample_a > sample_b) ? ANTENNA_A : ANTENNA_B;
108 if (ant->flags & ANTENNA_TX_DIVERSITY)
109 new_ant.tx = (sample_a > sample_b) ? ANTENNA_A : ANTENNA_B;
111 rt2x00lib_config_antenna(rt2x00dev, &new_ant);
114 static void rt2x00lib_antenna_diversity_eval(struct rt2x00_dev *rt2x00dev)
116 struct link_ant *ant = &rt2x00dev->link.ant;
117 struct antenna_setup new_ant;
121 memcpy(&new_ant, &ant->active, sizeof(new_ant));
124 * Get current RSSI value along with the historical value,
125 * after that update the history with the current value.
127 rssi_curr = rt2x00link_antenna_get_link_rssi(rt2x00dev);
128 rssi_old = rt2x00link_antenna_get_rssi_rx_history(rt2x00dev);
129 rt2x00link_antenna_update_rssi_rx_history(rt2x00dev, rssi_curr);
132 * Legacy driver indicates that we should swap antenna's
133 * when the difference in RSSI is greater that 5. This
134 * also should be done when the RSSI was actually better
135 * then the previous sample.
136 * When the difference exceeds the threshold we should
137 * sample the rssi from the other antenna to make a valid
138 * comparison between the 2 antennas.
140 if (abs(rssi_curr - rssi_old) < 5)
143 ant->flags |= ANTENNA_MODE_SAMPLE;
145 if (ant->flags & ANTENNA_RX_DIVERSITY)
146 new_ant.rx = (new_ant.rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
148 if (ant->flags & ANTENNA_TX_DIVERSITY)
149 new_ant.tx = (new_ant.tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
151 rt2x00lib_config_antenna(rt2x00dev, &new_ant);
154 static void rt2x00lib_antenna_diversity(struct rt2x00_dev *rt2x00dev)
156 struct link_ant *ant = &rt2x00dev->link.ant;
159 * Determine if software diversity is enabled for
160 * either the TX or RX antenna (or both).
161 * Always perform this check since within the link
162 * tuner interval the configuration might have changed.
164 ant->flags &= ~ANTENNA_RX_DIVERSITY;
165 ant->flags &= ~ANTENNA_TX_DIVERSITY;
167 if (rt2x00dev->default_ant.rx == ANTENNA_SW_DIVERSITY)
168 ant->flags |= ANTENNA_RX_DIVERSITY;
169 if (rt2x00dev->default_ant.tx == ANTENNA_SW_DIVERSITY)
170 ant->flags |= ANTENNA_TX_DIVERSITY;
172 if (!(ant->flags & ANTENNA_RX_DIVERSITY) &&
173 !(ant->flags & ANTENNA_TX_DIVERSITY)) {
179 * If we have only sampled the data over the last period
180 * we should now harvest the data. Otherwise just evaluate
181 * the data. The latter should only be performed once
184 if (ant->flags & ANTENNA_MODE_SAMPLE)
185 rt2x00lib_antenna_diversity_sample(rt2x00dev);
186 else if (rt2x00dev->link.count & 1)
187 rt2x00lib_antenna_diversity_eval(rt2x00dev);
190 void rt2x00link_update_stats(struct rt2x00_dev *rt2x00dev,
192 struct rxdone_entry_desc *rxdesc)
194 struct link_qual *qual = &rt2x00dev->link.qual;
195 struct link_ant *ant = &rt2x00dev->link.ant;
196 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
197 int avg_rssi = rxdesc->rssi;
198 int ant_rssi = rxdesc->rssi;
201 * Frame was received successfully since non-succesfull
202 * frames would have been dropped by the hardware.
207 * We are only interested in quality statistics from
208 * beacons which came from the BSS which we are
211 if (!ieee80211_is_beacon(hdr->frame_control) ||
212 !(rxdesc->dev_flags & RXDONE_MY_BSS))
219 avg_rssi = MOVING_AVERAGE(qual->avg_rssi, rxdesc->rssi, 8);
220 qual->avg_rssi = avg_rssi;
223 * Update antenna RSSI
226 ant_rssi = MOVING_AVERAGE(ant->rssi_ant, rxdesc->rssi, 8);
227 ant->rssi_ant = ant_rssi;
230 static void rt2x00link_precalculate_signal(struct rt2x00_dev *rt2x00dev)
232 struct link_qual *qual = &rt2x00dev->link.qual;
234 if (qual->rx_failed || qual->rx_success)
235 qual->rx_percentage =
236 (qual->rx_success * 100) /
237 (qual->rx_failed + qual->rx_success);
239 qual->rx_percentage = 50;
241 if (qual->tx_failed || qual->tx_success)
242 qual->tx_percentage =
243 (qual->tx_success * 100) /
244 (qual->tx_failed + qual->tx_success);
246 qual->tx_percentage = 50;
248 qual->rx_success = 0;
250 qual->tx_success = 0;
254 int rt2x00link_calculate_signal(struct rt2x00_dev *rt2x00dev, int rssi)
256 struct link_qual *qual = &rt2x00dev->link.qual;
257 int rssi_percentage = 0;
261 * We need a positive value for the RSSI.
264 rssi += rt2x00dev->rssi_offset;
267 * Calculate the different percentages,
268 * which will be used for the signal.
270 rssi_percentage = (rssi * 100) / rt2x00dev->rssi_offset;
273 * Add the individual percentages and use the WEIGHT
274 * defines to calculate the current link signal.
276 signal = ((WEIGHT_RSSI * rssi_percentage) +
277 (WEIGHT_TX * qual->tx_percentage) +
278 (WEIGHT_RX * qual->rx_percentage)) / 100;
280 return max_t(int, signal, 100);
283 void rt2x00link_start_tuner(struct rt2x00_dev *rt2x00dev)
285 struct link_qual *qual = &rt2x00dev->link.qual;
288 * Link tuning should only be performed when
289 * an active sta or master interface exists.
290 * Single monitor mode interfaces should never have
291 * work with link tuners.
293 if (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count)
297 * Clear all (possibly) pre-existing quality statistics.
299 memset(qual, 0, sizeof(*qual));
302 * The RX and TX percentage should start at 50%
303 * this will assure we will get at least get some
304 * decent value when the link tuner starts.
305 * The value will be dropped and overwritten with
306 * the correct (measured) value anyway during the
307 * first run of the link tuner.
309 qual->rx_percentage = 50;
310 qual->tx_percentage = 50;
312 rt2x00link_reset_tuner(rt2x00dev, false);
314 queue_delayed_work(rt2x00dev->hw->workqueue,
315 &rt2x00dev->link.work, LINK_TUNE_INTERVAL);
318 void rt2x00link_stop_tuner(struct rt2x00_dev *rt2x00dev)
320 cancel_delayed_work_sync(&rt2x00dev->link.work);
323 void rt2x00link_reset_tuner(struct rt2x00_dev *rt2x00dev, bool antenna)
325 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
329 * Reset link information.
330 * Both the currently active vgc level as well as
331 * the link tuner counter should be reset. Resetting
332 * the counter is important for devices where the
333 * device should only perform link tuning during the
334 * first minute after being enabled.
336 rt2x00dev->link.count = 0;
337 rt2x00dev->link.vgc_level = 0;
340 * Reset the link tuner.
342 rt2x00dev->ops->lib->reset_tuner(rt2x00dev);
345 rt2x00link_antenna_reset(rt2x00dev);
348 static void rt2x00link_tuner(struct work_struct *work)
350 struct rt2x00_dev *rt2x00dev =
351 container_of(work, struct rt2x00_dev, link.work.work);
352 struct link_qual *qual = &rt2x00dev->link.qual;
355 * When the radio is shutting down we should
356 * immediately cease all link tuning.
358 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
364 rt2x00dev->ops->lib->link_stats(rt2x00dev, qual);
365 rt2x00dev->low_level_stats.dot11FCSErrorCount += qual->rx_failed;
368 * Only perform the link tuning when Link tuning
369 * has been enabled (This could have been disabled from the EEPROM).
371 if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
372 rt2x00dev->ops->lib->link_tuner(rt2x00dev);
375 * Precalculate a portion of the link signal which is
376 * in based on the tx/rx success/failure counters.
378 rt2x00link_precalculate_signal(rt2x00dev);
381 * Send a signal to the led to update the led signal strength.
383 rt2x00leds_led_quality(rt2x00dev, qual->avg_rssi);
386 * Evaluate antenna setup, make this the last step since this could
387 * possibly reset some statistics.
389 rt2x00lib_antenna_diversity(rt2x00dev);
392 * Increase tuner counter, and reschedule the next link tuner run.
394 rt2x00dev->link.count++;
395 queue_delayed_work(rt2x00dev->hw->workqueue,
396 &rt2x00dev->link.work, LINK_TUNE_INTERVAL);
399 void rt2x00link_register(struct rt2x00_dev *rt2x00dev)
401 INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00link_tuner);