Revert "ipw2200: fix oops on missing firmware"
[deliverable/linux.git] / drivers / net / wireless / rt2x00 / rt2x00link.c
CommitLineData
84e3196f 1/*
4e54c711 2 Copyright (C) 2004 - 2009 rt2x00 SourceForge Project
84e3196f
ID
3 <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
5352ff65
ID
32/*
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/*
66679a65
LE
49 * Small helper macro for percentage calculation
50 * This is a very simple macro with the only catch that it will
51 * produce a default value in case no total value was provided.
52 */
53#define PERCENTAGE(__value, __total) \
54 ( (__total) ? (((__value) * 100) / (__total)) : (DEFAULT_PERCENTAGE) )
55
56/*
57 * Helper struct and macro to work with moving/walking averages.
5352ff65
ID
58 * When adding a value to the average value the following calculation
59 * is needed:
60 *
61 * avg_rssi = ((avg_rssi * 7) + rssi) / 8;
62 *
63 * The advantage of this approach is that we only need 1 variable
64 * to store the average in (No need for a count and a total).
65 * But more importantly, normal average values will over time
66 * move less and less towards newly added values this results
67 * that with link tuning, the device can have a very good RSSI
68 * for a few minutes but when the device is moved away from the AP
69 * the average will not decrease fast enough to compensate.
70 * The walking average compensates this and will move towards
66679a65
LE
71 * the new values correctly allowing a effective link tuning,
72 * the speed of the average moving towards other values depends
73 * on the value for the number of samples. The higher the number
74 * of samples, the slower the average will move.
75 * We use two variables to keep track of the average value to
76 * compensate for the rounding errors. This can be a significant
77 * error (>5dBm) if the factor is too low.
5352ff65 78 */
66679a65
LE
79#define AVG_SAMPLES 8
80#define AVG_FACTOR 1000
81#define MOVING_AVERAGE(__avg, __val) \
82({ \
83 struct avg_val __new; \
84 __new.avg_weight = \
85 (__avg).avg_weight ? \
86 ((((__avg).avg_weight * ((AVG_SAMPLES) - 1)) + \
87 ((__val) * (AVG_FACTOR))) / \
88 (AVG_SAMPLES) ) : \
89 ((__val) * (AVG_FACTOR)); \
90 __new.avg = __new.avg_weight / (AVG_FACTOR); \
91 __new; \
92})
5352ff65
ID
93
94/*
95 * For calculating the Signal quality we have determined
96 * the total number of success and failed RX and TX frames.
97 * With the addition of the average RSSI value we can determine
98 * the link quality using the following algorithm:
99 *
100 * rssi_percentage = (avg_rssi * 100) / rssi_offset
101 * rx_percentage = (rx_success * 100) / rx_total
102 * tx_percentage = (tx_success * 100) / tx_total
103 * avg_signal = ((WEIGHT_RSSI * avg_rssi) +
104 * (WEIGHT_TX * tx_percentage) +
105 * (WEIGHT_RX * rx_percentage)) / 100
106 *
107 * This value should then be checked to not be greater then 100.
108 * This means the values of WEIGHT_RSSI, WEIGHT_RX, WEIGHT_TX must
109 * sum up to 100 as well.
110 */
111#define WEIGHT_RSSI 20
112#define WEIGHT_RX 40
113#define WEIGHT_TX 40
114
84e3196f
ID
115static int rt2x00link_antenna_get_link_rssi(struct rt2x00_dev *rt2x00dev)
116{
117 struct link_ant *ant = &rt2x00dev->link.ant;
118
66679a65
LE
119 if (ant->rssi_ant.avg && rt2x00dev->link.qual.rx_success)
120 return ant->rssi_ant.avg;
84e3196f
ID
121 return DEFAULT_RSSI;
122}
123
193df183 124static int rt2x00link_antenna_get_rssi_history(struct rt2x00_dev *rt2x00dev)
84e3196f
ID
125{
126 struct link_ant *ant = &rt2x00dev->link.ant;
127
193df183
LE
128 if (ant->rssi_history)
129 return ant->rssi_history;
84e3196f
ID
130 return DEFAULT_RSSI;
131}
84e3196f
ID
132
133static void rt2x00link_antenna_update_rssi_history(struct rt2x00_dev *rt2x00dev,
84e3196f
ID
134 int rssi)
135{
136 struct link_ant *ant = &rt2x00dev->link.ant;
193df183 137 ant->rssi_history = rssi;
84e3196f 138}
84e3196f
ID
139
140static void rt2x00link_antenna_reset(struct rt2x00_dev *rt2x00dev)
141{
66679a65
LE
142 rt2x00dev->link.ant.rssi_ant.avg = 0;
143 rt2x00dev->link.ant.rssi_ant.avg_weight = 0;
84e3196f
ID
144}
145
146static void rt2x00lib_antenna_diversity_sample(struct rt2x00_dev *rt2x00dev)
147{
148 struct link_ant *ant = &rt2x00dev->link.ant;
149 struct antenna_setup new_ant;
193df183
LE
150 int other_antenna;
151
152 int sample_current = rt2x00link_antenna_get_link_rssi(rt2x00dev);
153 int sample_other = rt2x00link_antenna_get_rssi_history(rt2x00dev);
84e3196f
ID
154
155 memcpy(&new_ant, &ant->active, sizeof(new_ant));
156
157 /*
158 * We are done sampling. Now we should evaluate the results.
159 */
160 ant->flags &= ~ANTENNA_MODE_SAMPLE;
161
162 /*
163 * During the last period we have sampled the RSSI
49513481 164 * from both antennas. It now is time to determine
84e3196f
ID
165 * which antenna demonstrated the best performance.
166 * When we are already on the antenna with the best
193df183
LE
167 * performance, just create a good starting point
168 * for the history and we are done.
84e3196f 169 */
193df183
LE
170 if (sample_current >= sample_other) {
171 rt2x00link_antenna_update_rssi_history(rt2x00dev,
172 sample_current);
84e3196f 173 return;
193df183
LE
174 }
175
176 other_antenna = (ant->active.rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
84e3196f
ID
177
178 if (ant->flags & ANTENNA_RX_DIVERSITY)
193df183 179 new_ant.rx = other_antenna;
84e3196f
ID
180
181 if (ant->flags & ANTENNA_TX_DIVERSITY)
193df183 182 new_ant.tx = other_antenna;
84e3196f 183
eb87eaac 184 rt2x00lib_config_antenna(rt2x00dev, new_ant);
84e3196f
ID
185}
186
187static void rt2x00lib_antenna_diversity_eval(struct rt2x00_dev *rt2x00dev)
188{
189 struct link_ant *ant = &rt2x00dev->link.ant;
190 struct antenna_setup new_ant;
191 int rssi_curr;
192 int rssi_old;
193
194 memcpy(&new_ant, &ant->active, sizeof(new_ant));
195
196 /*
197 * Get current RSSI value along with the historical value,
198 * after that update the history with the current value.
199 */
200 rssi_curr = rt2x00link_antenna_get_link_rssi(rt2x00dev);
193df183
LE
201 rssi_old = rt2x00link_antenna_get_rssi_history(rt2x00dev);
202 rt2x00link_antenna_update_rssi_history(rt2x00dev, rssi_curr);
84e3196f
ID
203
204 /*
205 * Legacy driver indicates that we should swap antenna's
206 * when the difference in RSSI is greater that 5. This
207 * also should be done when the RSSI was actually better
208 * then the previous sample.
209 * When the difference exceeds the threshold we should
210 * sample the rssi from the other antenna to make a valid
211 * comparison between the 2 antennas.
212 */
213 if (abs(rssi_curr - rssi_old) < 5)
214 return;
215
216 ant->flags |= ANTENNA_MODE_SAMPLE;
217
218 if (ant->flags & ANTENNA_RX_DIVERSITY)
219 new_ant.rx = (new_ant.rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
220
221 if (ant->flags & ANTENNA_TX_DIVERSITY)
222 new_ant.tx = (new_ant.tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
223
eb87eaac 224 rt2x00lib_config_antenna(rt2x00dev, new_ant);
84e3196f
ID
225}
226
193df183 227static bool rt2x00lib_antenna_diversity(struct rt2x00_dev *rt2x00dev)
84e3196f
ID
228{
229 struct link_ant *ant = &rt2x00dev->link.ant;
bdfa500b 230 unsigned int flags = ant->flags;
84e3196f
ID
231
232 /*
233 * Determine if software diversity is enabled for
234 * either the TX or RX antenna (or both).
235 * Always perform this check since within the link
236 * tuner interval the configuration might have changed.
237 */
bdfa500b
ID
238 flags &= ~ANTENNA_RX_DIVERSITY;
239 flags &= ~ANTENNA_TX_DIVERSITY;
84e3196f
ID
240
241 if (rt2x00dev->default_ant.rx == ANTENNA_SW_DIVERSITY)
bdfa500b 242 flags |= ANTENNA_RX_DIVERSITY;
84e3196f 243 if (rt2x00dev->default_ant.tx == ANTENNA_SW_DIVERSITY)
bdfa500b 244 flags |= ANTENNA_TX_DIVERSITY;
84e3196f
ID
245
246 if (!(ant->flags & ANTENNA_RX_DIVERSITY) &&
247 !(ant->flags & ANTENNA_TX_DIVERSITY)) {
248 ant->flags = 0;
193df183 249 return true;
84e3196f
ID
250 }
251
bdfa500b
ID
252 /* Update flags */
253 ant->flags = flags;
254
84e3196f
ID
255 /*
256 * If we have only sampled the data over the last period
257 * we should now harvest the data. Otherwise just evaluate
258 * the data. The latter should only be performed once
259 * every 2 seconds.
260 */
193df183 261 if (ant->flags & ANTENNA_MODE_SAMPLE) {
84e3196f 262 rt2x00lib_antenna_diversity_sample(rt2x00dev);
193df183
LE
263 return true;
264 } else if (rt2x00dev->link.count & 1) {
84e3196f 265 rt2x00lib_antenna_diversity_eval(rt2x00dev);
193df183
LE
266 return true;
267 }
268
269 return false;
84e3196f
ID
270}
271
272void rt2x00link_update_stats(struct rt2x00_dev *rt2x00dev,
273 struct sk_buff *skb,
274 struct rxdone_entry_desc *rxdesc)
275{
5352ff65 276 struct link *link = &rt2x00dev->link;
84e3196f
ID
277 struct link_qual *qual = &rt2x00dev->link.qual;
278 struct link_ant *ant = &rt2x00dev->link.ant;
279 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
84e3196f
ID
280
281 /*
282 * Frame was received successfully since non-succesfull
283 * frames would have been dropped by the hardware.
284 */
285 qual->rx_success++;
286
287 /*
288 * We are only interested in quality statistics from
289 * beacons which came from the BSS which we are
290 * associated with.
291 */
292 if (!ieee80211_is_beacon(hdr->frame_control) ||
293 !(rxdesc->dev_flags & RXDONE_MY_BSS))
294 return;
295
296 /*
297 * Update global RSSI
298 */
66679a65 299 link->avg_rssi = MOVING_AVERAGE(link->avg_rssi, rxdesc->rssi);
84e3196f
ID
300
301 /*
302 * Update antenna RSSI
303 */
66679a65 304 ant->rssi_ant = MOVING_AVERAGE(ant->rssi_ant, rxdesc->rssi);
84e3196f
ID
305}
306
307static void rt2x00link_precalculate_signal(struct rt2x00_dev *rt2x00dev)
308{
5352ff65 309 struct link *link = &rt2x00dev->link;
84e3196f
ID
310 struct link_qual *qual = &rt2x00dev->link.qual;
311
5352ff65
ID
312 link->rx_percentage =
313 PERCENTAGE(qual->rx_success, qual->rx_failed + qual->rx_success);
314 link->tx_percentage =
315 PERCENTAGE(qual->tx_success, qual->tx_failed + qual->tx_success);
84e3196f
ID
316}
317
318int rt2x00link_calculate_signal(struct rt2x00_dev *rt2x00dev, int rssi)
319{
5352ff65 320 struct link *link = &rt2x00dev->link;
84e3196f
ID
321 int rssi_percentage = 0;
322 int signal;
323
324 /*
325 * We need a positive value for the RSSI.
326 */
327 if (rssi < 0)
328 rssi += rt2x00dev->rssi_offset;
329
330 /*
331 * Calculate the different percentages,
332 * which will be used for the signal.
333 */
5352ff65 334 rssi_percentage = PERCENTAGE(rssi, rt2x00dev->rssi_offset);
84e3196f
ID
335
336 /*
337 * Add the individual percentages and use the WEIGHT
338 * defines to calculate the current link signal.
339 */
340 signal = ((WEIGHT_RSSI * rssi_percentage) +
5352ff65
ID
341 (WEIGHT_TX * link->tx_percentage) +
342 (WEIGHT_RX * link->rx_percentage)) / 100;
84e3196f
ID
343
344 return max_t(int, signal, 100);
345}
346
347void rt2x00link_start_tuner(struct rt2x00_dev *rt2x00dev)
348{
5352ff65 349 struct link *link = &rt2x00dev->link;
84e3196f
ID
350
351 /*
352 * Link tuning should only be performed when
353 * an active sta or master interface exists.
354 * Single monitor mode interfaces should never have
355 * work with link tuners.
356 */
357 if (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count)
358 return;
359
5352ff65
ID
360 link->rx_percentage = DEFAULT_PERCENTAGE;
361 link->tx_percentage = DEFAULT_PERCENTAGE;
84e3196f
ID
362
363 rt2x00link_reset_tuner(rt2x00dev, false);
364
42935eca
LR
365 ieee80211_queue_delayed_work(rt2x00dev->hw,
366 &link->work, LINK_TUNE_INTERVAL);
84e3196f
ID
367}
368
369void rt2x00link_stop_tuner(struct rt2x00_dev *rt2x00dev)
370{
371 cancel_delayed_work_sync(&rt2x00dev->link.work);
372}
373
374void rt2x00link_reset_tuner(struct rt2x00_dev *rt2x00dev, bool antenna)
375{
5352ff65
ID
376 struct link_qual *qual = &rt2x00dev->link.qual;
377
84e3196f
ID
378 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
379 return;
380
381 /*
382 * Reset link information.
383 * Both the currently active vgc level as well as
384 * the link tuner counter should be reset. Resetting
385 * the counter is important for devices where the
386 * device should only perform link tuning during the
387 * first minute after being enabled.
388 */
389 rt2x00dev->link.count = 0;
5352ff65 390 memset(qual, 0, sizeof(*qual));
84e3196f
ID
391
392 /*
393 * Reset the link tuner.
394 */
5352ff65 395 rt2x00dev->ops->lib->reset_tuner(rt2x00dev, qual);
84e3196f
ID
396
397 if (antenna)
398 rt2x00link_antenna_reset(rt2x00dev);
399}
400
a57e2e84 401static void rt2x00link_reset_qual(struct rt2x00_dev *rt2x00dev)
64abd803
ID
402{
403 struct link_qual *qual = &rt2x00dev->link.qual;
404
405 qual->rx_success = 0;
406 qual->rx_failed = 0;
407 qual->tx_success = 0;
408 qual->tx_failed = 0;
409}
410
84e3196f
ID
411static void rt2x00link_tuner(struct work_struct *work)
412{
413 struct rt2x00_dev *rt2x00dev =
414 container_of(work, struct rt2x00_dev, link.work.work);
5352ff65 415 struct link *link = &rt2x00dev->link;
84e3196f
ID
416 struct link_qual *qual = &rt2x00dev->link.qual;
417
418 /*
419 * When the radio is shutting down we should
420 * immediately cease all link tuning.
421 */
422 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
423 return;
424
425 /*
426 * Update statistics.
427 */
428 rt2x00dev->ops->lib->link_stats(rt2x00dev, qual);
429 rt2x00dev->low_level_stats.dot11FCSErrorCount += qual->rx_failed;
430
5352ff65
ID
431 /*
432 * Update quality RSSI for link tuning,
433 * when we have received some frames and we managed to
434 * collect the RSSI data we could use this. Otherwise we
435 * must fallback to the default RSSI value.
436 */
66679a65 437 if (!link->avg_rssi.avg || !qual->rx_success)
5352ff65
ID
438 qual->rssi = DEFAULT_RSSI;
439 else
66679a65 440 qual->rssi = link->avg_rssi.avg;
5352ff65 441
84e3196f
ID
442 /*
443 * Only perform the link tuning when Link tuning
444 * has been enabled (This could have been disabled from the EEPROM).
445 */
446 if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
5352ff65 447 rt2x00dev->ops->lib->link_tuner(rt2x00dev, qual, link->count);
84e3196f
ID
448
449 /*
450 * Precalculate a portion of the link signal which is
451 * in based on the tx/rx success/failure counters.
452 */
453 rt2x00link_precalculate_signal(rt2x00dev);
454
455 /*
456 * Send a signal to the led to update the led signal strength.
457 */
66679a65 458 rt2x00leds_led_quality(rt2x00dev, qual->rssi);
84e3196f
ID
459
460 /*
193df183
LE
461 * Evaluate antenna setup, make this the last step when
462 * rt2x00lib_antenna_diversity made changes the quality
463 * statistics will be reset.
64abd803 464 */
193df183
LE
465 if (rt2x00lib_antenna_diversity(rt2x00dev))
466 rt2x00link_reset_qual(rt2x00dev);
64abd803 467
84e3196f
ID
468 /*
469 * Increase tuner counter, and reschedule the next link tuner run.
470 */
5352ff65 471 link->count++;
42935eca
LR
472 ieee80211_queue_delayed_work(rt2x00dev->hw,
473 &link->work, LINK_TUNE_INTERVAL);
84e3196f
ID
474}
475
476void rt2x00link_register(struct rt2x00_dev *rt2x00dev)
477{
478 INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00link_tuner);
479}
This page took 0.169921 seconds and 5 git commands to generate.