blob: fe951dc9bacf579c1ff223b41c7e581acbe3cbf2 [file] [log] [blame]
Ivo van Doorn84e31962008-12-20 10:53:29 +01001/*
Ivo van Doorn4e54c712009-01-17 20:42:32 +01002 Copyright (C) 2004 - 2009 rt2x00 SourceForge Project
Ivo van Doorn84e31962008-12-20 10:53:29 +01003 <http://rt2x00.serialmonkey.com>
4
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.
9
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.
14
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.
19 */
20
21/*
22 Module: rt2x00lib
23 Abstract: rt2x00 generic link tuning routines.
24 */
25
26#include <linux/kernel.h>
27#include <linux/module.h>
28
29#include "rt2x00.h"
30#include "rt2x00lib.h"
31
Ivo van Doorn5352ff62008-12-20 10:54:54 +010032/*
33 * When we lack RSSI information return something less then -80 to
34 * tell the driver to tune the device to maximum sensitivity.
35 */
36#define DEFAULT_RSSI -128
37
38/*
39 * When no TX/RX percentage could be calculated due to lack of
40 * frames on the air, we fallback to a percentage of 50%.
41 * This will assure we will get at least get some decent value
42 * when the link tuner starts.
43 * The value will be dropped and overwritten with the correct (measured)
44 * value anyway during the first run of the link tuner.
45 */
46#define DEFAULT_PERCENTAGE 50
47
48/*
49 * Small helper macro to work with moving/walking averages.
50 * When adding a value to the average value the following calculation
51 * is needed:
52 *
53 * avg_rssi = ((avg_rssi * 7) + rssi) / 8;
54 *
55 * The advantage of this approach is that we only need 1 variable
56 * to store the average in (No need for a count and a total).
57 * But more importantly, normal average values will over time
58 * move less and less towards newly added values this results
59 * that with link tuning, the device can have a very good RSSI
60 * for a few minutes but when the device is moved away from the AP
61 * the average will not decrease fast enough to compensate.
62 * The walking average compensates this and will move towards
63 * the new values correctly allowing a effective link tuning.
64 */
65#define MOVING_AVERAGE(__avg, __val, __samples) \
66 ( (((__avg) * ((__samples) - 1)) + (__val)) / (__samples) )
67
68/*
69 * Small helper macro for percentage calculation
70 * This is a very simple macro with the only catch that it will
71 * produce a default value in case no total value was provided.
72 */
73#define PERCENTAGE(__value, __total) \
74 ( (__total) ? (((__value) * 100) / (__total)) : (DEFAULT_PERCENTAGE) )
75
76/*
77 * For calculating the Signal quality we have determined
78 * the total number of success and failed RX and TX frames.
79 * With the addition of the average RSSI value we can determine
80 * the link quality using the following algorithm:
81 *
82 * rssi_percentage = (avg_rssi * 100) / rssi_offset
83 * rx_percentage = (rx_success * 100) / rx_total
84 * tx_percentage = (tx_success * 100) / tx_total
85 * avg_signal = ((WEIGHT_RSSI * avg_rssi) +
86 * (WEIGHT_TX * tx_percentage) +
87 * (WEIGHT_RX * rx_percentage)) / 100
88 *
89 * This value should then be checked to not be greater then 100.
90 * This means the values of WEIGHT_RSSI, WEIGHT_RX, WEIGHT_TX must
91 * sum up to 100 as well.
92 */
93#define WEIGHT_RSSI 20
94#define WEIGHT_RX 40
95#define WEIGHT_TX 40
96
Ivo van Doorn84e31962008-12-20 10:53:29 +010097static int rt2x00link_antenna_get_link_rssi(struct rt2x00_dev *rt2x00dev)
98{
99 struct link_ant *ant = &rt2x00dev->link.ant;
100
101 if (ant->rssi_ant && rt2x00dev->link.qual.rx_success)
102 return ant->rssi_ant;
103 return DEFAULT_RSSI;
104}
105
106static int rt2x00link_antenna_get_rssi_history(struct rt2x00_dev *rt2x00dev,
107 enum antenna antenna)
108{
109 struct link_ant *ant = &rt2x00dev->link.ant;
110
111 if (ant->rssi_history[antenna - ANTENNA_A])
112 return ant->rssi_history[antenna - ANTENNA_A];
113 return DEFAULT_RSSI;
114}
115/* Small wrapper for rt2x00link_antenna_get_rssi_history() */
116#define rt2x00link_antenna_get_rssi_rx_history(__dev) \
117 rt2x00link_antenna_get_rssi_history((__dev), \
118 (__dev)->link.ant.active.rx)
119#define rt2x00link_antenna_get_rssi_tx_history(__dev) \
120 rt2x00link_antenna_get_rssi_history((__dev), \
121 (__dev)->link.ant.active.tx)
122
123static void rt2x00link_antenna_update_rssi_history(struct rt2x00_dev *rt2x00dev,
124 enum antenna antenna,
125 int rssi)
126{
127 struct link_ant *ant = &rt2x00dev->link.ant;
128 ant->rssi_history[ant->active.rx - ANTENNA_A] = rssi;
129}
130/* Small wrapper for rt2x00link_antenna_get_rssi_history() */
131#define rt2x00link_antenna_update_rssi_rx_history(__dev, __rssi) \
132 rt2x00link_antenna_update_rssi_history((__dev), \
133 (__dev)->link.ant.active.rx, \
134 (__rssi))
135#define rt2x00link_antenna_update_rssi_tx_history(__dev, __rssi) \
136 rt2x00link_antenna_update_rssi_history((__dev), \
137 (__dev)->link.ant.active.tx, \
138 (__rssi))
139
140static void rt2x00link_antenna_reset(struct rt2x00_dev *rt2x00dev)
141{
142 rt2x00dev->link.ant.rssi_ant = 0;
143}
144
145static void rt2x00lib_antenna_diversity_sample(struct rt2x00_dev *rt2x00dev)
146{
147 struct link_ant *ant = &rt2x00dev->link.ant;
148 struct antenna_setup new_ant;
149 int sample_a = rt2x00link_antenna_get_rssi_history(rt2x00dev, ANTENNA_A);
150 int sample_b = rt2x00link_antenna_get_rssi_history(rt2x00dev, ANTENNA_B);
151
152 memcpy(&new_ant, &ant->active, sizeof(new_ant));
153
154 /*
155 * We are done sampling. Now we should evaluate the results.
156 */
157 ant->flags &= ~ANTENNA_MODE_SAMPLE;
158
159 /*
160 * During the last period we have sampled the RSSI
Luis Correia49513482009-07-17 21:39:19 +0200161 * from both antennas. It now is time to determine
Ivo van Doorn84e31962008-12-20 10:53:29 +0100162 * which antenna demonstrated the best performance.
163 * When we are already on the antenna with the best
164 * performance, then there really is nothing for us
165 * left to do.
166 */
167 if (sample_a == sample_b)
168 return;
169
170 if (ant->flags & ANTENNA_RX_DIVERSITY)
171 new_ant.rx = (sample_a > sample_b) ? ANTENNA_A : ANTENNA_B;
172
173 if (ant->flags & ANTENNA_TX_DIVERSITY)
174 new_ant.tx = (sample_a > sample_b) ? ANTENNA_A : ANTENNA_B;
175
Lars Ericssoneb87eaa2009-07-18 20:21:52 +0200176 rt2x00lib_config_antenna(rt2x00dev, new_ant);
Ivo van Doorn84e31962008-12-20 10:53:29 +0100177}
178
179static void rt2x00lib_antenna_diversity_eval(struct rt2x00_dev *rt2x00dev)
180{
181 struct link_ant *ant = &rt2x00dev->link.ant;
182 struct antenna_setup new_ant;
183 int rssi_curr;
184 int rssi_old;
185
186 memcpy(&new_ant, &ant->active, sizeof(new_ant));
187
188 /*
189 * Get current RSSI value along with the historical value,
190 * after that update the history with the current value.
191 */
192 rssi_curr = rt2x00link_antenna_get_link_rssi(rt2x00dev);
193 rssi_old = rt2x00link_antenna_get_rssi_rx_history(rt2x00dev);
194 rt2x00link_antenna_update_rssi_rx_history(rt2x00dev, rssi_curr);
195
196 /*
197 * Legacy driver indicates that we should swap antenna's
198 * when the difference in RSSI is greater that 5. This
199 * also should be done when the RSSI was actually better
200 * then the previous sample.
201 * When the difference exceeds the threshold we should
202 * sample the rssi from the other antenna to make a valid
203 * comparison between the 2 antennas.
204 */
205 if (abs(rssi_curr - rssi_old) < 5)
206 return;
207
208 ant->flags |= ANTENNA_MODE_SAMPLE;
209
210 if (ant->flags & ANTENNA_RX_DIVERSITY)
211 new_ant.rx = (new_ant.rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
212
213 if (ant->flags & ANTENNA_TX_DIVERSITY)
214 new_ant.tx = (new_ant.tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
215
Lars Ericssoneb87eaa2009-07-18 20:21:52 +0200216 rt2x00lib_config_antenna(rt2x00dev, new_ant);
Ivo van Doorn84e31962008-12-20 10:53:29 +0100217}
218
219static void rt2x00lib_antenna_diversity(struct rt2x00_dev *rt2x00dev)
220{
221 struct link_ant *ant = &rt2x00dev->link.ant;
Ivo van Doornbdfa5002009-08-08 23:53:04 +0200222 unsigned int flags = ant->flags;
Ivo van Doorn84e31962008-12-20 10:53:29 +0100223
224 /*
225 * Determine if software diversity is enabled for
226 * either the TX or RX antenna (or both).
227 * Always perform this check since within the link
228 * tuner interval the configuration might have changed.
229 */
Ivo van Doornbdfa5002009-08-08 23:53:04 +0200230 flags &= ~ANTENNA_RX_DIVERSITY;
231 flags &= ~ANTENNA_TX_DIVERSITY;
Ivo van Doorn84e31962008-12-20 10:53:29 +0100232
233 if (rt2x00dev->default_ant.rx == ANTENNA_SW_DIVERSITY)
Ivo van Doornbdfa5002009-08-08 23:53:04 +0200234 flags |= ANTENNA_RX_DIVERSITY;
Ivo van Doorn84e31962008-12-20 10:53:29 +0100235 if (rt2x00dev->default_ant.tx == ANTENNA_SW_DIVERSITY)
Ivo van Doornbdfa5002009-08-08 23:53:04 +0200236 flags |= ANTENNA_TX_DIVERSITY;
Ivo van Doorn84e31962008-12-20 10:53:29 +0100237
238 if (!(ant->flags & ANTENNA_RX_DIVERSITY) &&
239 !(ant->flags & ANTENNA_TX_DIVERSITY)) {
240 ant->flags = 0;
241 return;
242 }
243
Ivo van Doornbdfa5002009-08-08 23:53:04 +0200244 /* Update flags */
245 ant->flags = flags;
246
Ivo van Doorn84e31962008-12-20 10:53:29 +0100247 /*
248 * If we have only sampled the data over the last period
249 * we should now harvest the data. Otherwise just evaluate
250 * the data. The latter should only be performed once
251 * every 2 seconds.
252 */
253 if (ant->flags & ANTENNA_MODE_SAMPLE)
254 rt2x00lib_antenna_diversity_sample(rt2x00dev);
255 else if (rt2x00dev->link.count & 1)
256 rt2x00lib_antenna_diversity_eval(rt2x00dev);
257}
258
259void rt2x00link_update_stats(struct rt2x00_dev *rt2x00dev,
260 struct sk_buff *skb,
261 struct rxdone_entry_desc *rxdesc)
262{
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100263 struct link *link = &rt2x00dev->link;
Ivo van Doorn84e31962008-12-20 10:53:29 +0100264 struct link_qual *qual = &rt2x00dev->link.qual;
265 struct link_ant *ant = &rt2x00dev->link.ant;
266 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
267 int avg_rssi = rxdesc->rssi;
268 int ant_rssi = rxdesc->rssi;
269
270 /*
271 * Frame was received successfully since non-succesfull
272 * frames would have been dropped by the hardware.
273 */
274 qual->rx_success++;
275
276 /*
277 * We are only interested in quality statistics from
278 * beacons which came from the BSS which we are
279 * associated with.
280 */
281 if (!ieee80211_is_beacon(hdr->frame_control) ||
282 !(rxdesc->dev_flags & RXDONE_MY_BSS))
283 return;
284
285 /*
286 * Update global RSSI
287 */
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100288 if (link->avg_rssi)
289 avg_rssi = MOVING_AVERAGE(link->avg_rssi, rxdesc->rssi, 8);
290 link->avg_rssi = avg_rssi;
Ivo van Doorn84e31962008-12-20 10:53:29 +0100291
292 /*
293 * Update antenna RSSI
294 */
295 if (ant->rssi_ant)
296 ant_rssi = MOVING_AVERAGE(ant->rssi_ant, rxdesc->rssi, 8);
297 ant->rssi_ant = ant_rssi;
298}
299
300static void rt2x00link_precalculate_signal(struct rt2x00_dev *rt2x00dev)
301{
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100302 struct link *link = &rt2x00dev->link;
Ivo van Doorn84e31962008-12-20 10:53:29 +0100303 struct link_qual *qual = &rt2x00dev->link.qual;
304
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100305 link->rx_percentage =
306 PERCENTAGE(qual->rx_success, qual->rx_failed + qual->rx_success);
307 link->tx_percentage =
308 PERCENTAGE(qual->tx_success, qual->tx_failed + qual->tx_success);
Ivo van Doorn84e31962008-12-20 10:53:29 +0100309}
310
311int rt2x00link_calculate_signal(struct rt2x00_dev *rt2x00dev, int rssi)
312{
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100313 struct link *link = &rt2x00dev->link;
Ivo van Doorn84e31962008-12-20 10:53:29 +0100314 int rssi_percentage = 0;
315 int signal;
316
317 /*
318 * We need a positive value for the RSSI.
319 */
320 if (rssi < 0)
321 rssi += rt2x00dev->rssi_offset;
322
323 /*
324 * Calculate the different percentages,
325 * which will be used for the signal.
326 */
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100327 rssi_percentage = PERCENTAGE(rssi, rt2x00dev->rssi_offset);
Ivo van Doorn84e31962008-12-20 10:53:29 +0100328
329 /*
330 * Add the individual percentages and use the WEIGHT
331 * defines to calculate the current link signal.
332 */
333 signal = ((WEIGHT_RSSI * rssi_percentage) +
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100334 (WEIGHT_TX * link->tx_percentage) +
335 (WEIGHT_RX * link->rx_percentage)) / 100;
Ivo van Doorn84e31962008-12-20 10:53:29 +0100336
337 return max_t(int, signal, 100);
338}
339
340void rt2x00link_start_tuner(struct rt2x00_dev *rt2x00dev)
341{
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100342 struct link *link = &rt2x00dev->link;
Ivo van Doorn84e31962008-12-20 10:53:29 +0100343
344 /*
345 * Link tuning should only be performed when
346 * an active sta or master interface exists.
347 * Single monitor mode interfaces should never have
348 * work with link tuners.
349 */
350 if (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count)
351 return;
352
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100353 link->rx_percentage = DEFAULT_PERCENTAGE;
354 link->tx_percentage = DEFAULT_PERCENTAGE;
Ivo van Doorn84e31962008-12-20 10:53:29 +0100355
356 rt2x00link_reset_tuner(rt2x00dev, false);
357
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400358 ieee80211_queue_delayed_work(rt2x00dev->hw,
359 &link->work, LINK_TUNE_INTERVAL);
Ivo van Doorn84e31962008-12-20 10:53:29 +0100360}
361
362void rt2x00link_stop_tuner(struct rt2x00_dev *rt2x00dev)
363{
364 cancel_delayed_work_sync(&rt2x00dev->link.work);
365}
366
367void rt2x00link_reset_tuner(struct rt2x00_dev *rt2x00dev, bool antenna)
368{
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100369 struct link_qual *qual = &rt2x00dev->link.qual;
370
Ivo van Doorn84e31962008-12-20 10:53:29 +0100371 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
372 return;
373
374 /*
375 * Reset link information.
376 * Both the currently active vgc level as well as
377 * the link tuner counter should be reset. Resetting
378 * the counter is important for devices where the
379 * device should only perform link tuning during the
380 * first minute after being enabled.
381 */
382 rt2x00dev->link.count = 0;
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100383 memset(qual, 0, sizeof(*qual));
Ivo van Doorn84e31962008-12-20 10:53:29 +0100384
385 /*
386 * Reset the link tuner.
387 */
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100388 rt2x00dev->ops->lib->reset_tuner(rt2x00dev, qual);
Ivo van Doorn84e31962008-12-20 10:53:29 +0100389
390 if (antenna)
391 rt2x00link_antenna_reset(rt2x00dev);
392}
393
Ivo van Doorna57e2e82009-03-28 20:51:41 +0100394static void rt2x00link_reset_qual(struct rt2x00_dev *rt2x00dev)
Ivo van Doorn64abd802009-03-01 17:42:00 +0100395{
396 struct link_qual *qual = &rt2x00dev->link.qual;
397
398 qual->rx_success = 0;
399 qual->rx_failed = 0;
400 qual->tx_success = 0;
401 qual->tx_failed = 0;
402}
403
Ivo van Doorn84e31962008-12-20 10:53:29 +0100404static void rt2x00link_tuner(struct work_struct *work)
405{
406 struct rt2x00_dev *rt2x00dev =
407 container_of(work, struct rt2x00_dev, link.work.work);
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100408 struct link *link = &rt2x00dev->link;
Ivo van Doorn84e31962008-12-20 10:53:29 +0100409 struct link_qual *qual = &rt2x00dev->link.qual;
410
411 /*
412 * When the radio is shutting down we should
413 * immediately cease all link tuning.
414 */
415 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
416 return;
417
418 /*
419 * Update statistics.
420 */
421 rt2x00dev->ops->lib->link_stats(rt2x00dev, qual);
422 rt2x00dev->low_level_stats.dot11FCSErrorCount += qual->rx_failed;
423
424 /*
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100425 * Update quality RSSI for link tuning,
426 * when we have received some frames and we managed to
427 * collect the RSSI data we could use this. Otherwise we
428 * must fallback to the default RSSI value.
429 */
430 if (!link->avg_rssi || !qual->rx_success)
431 qual->rssi = DEFAULT_RSSI;
432 else
433 qual->rssi = link->avg_rssi;
434
435 /*
Ivo van Doorn84e31962008-12-20 10:53:29 +0100436 * Only perform the link tuning when Link tuning
437 * has been enabled (This could have been disabled from the EEPROM).
438 */
439 if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100440 rt2x00dev->ops->lib->link_tuner(rt2x00dev, qual, link->count);
Ivo van Doorn84e31962008-12-20 10:53:29 +0100441
442 /*
443 * Precalculate a portion of the link signal which is
444 * in based on the tx/rx success/failure counters.
445 */
446 rt2x00link_precalculate_signal(rt2x00dev);
447
448 /*
449 * Send a signal to the led to update the led signal strength.
450 */
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100451 rt2x00leds_led_quality(rt2x00dev, link->avg_rssi);
Ivo van Doorn84e31962008-12-20 10:53:29 +0100452
453 /*
454 * Evaluate antenna setup, make this the last step since this could
455 * possibly reset some statistics.
456 */
457 rt2x00lib_antenna_diversity(rt2x00dev);
458
459 /*
Ivo van Doorn64abd802009-03-01 17:42:00 +0100460 * Reset the quality counters which recounted during each period.
461 */
462 rt2x00link_reset_qual(rt2x00dev);
463
464 /*
Ivo van Doorn84e31962008-12-20 10:53:29 +0100465 * Increase tuner counter, and reschedule the next link tuner run.
466 */
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100467 link->count++;
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400468 ieee80211_queue_delayed_work(rt2x00dev->hw,
469 &link->work, LINK_TUNE_INTERVAL);
Ivo van Doorn84e31962008-12-20 10:53:29 +0100470}
471
472void rt2x00link_register(struct rt2x00_dev *rt2x00dev)
473{
474 INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00link_tuner);
475}