Merge tag 'nfs-for-3.13-2' of git://git.linux-nfs.org/projects/trondmy/linux-nfs
[deliverable/linux.git] / drivers / net / wireless / rt2x00 / rt2x00link.c
CommitLineData
84e3196f 1/*
9c9a0d14 2 Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
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
1dc254ac
GJ
38/* Constants for EWMA calculations. */
39#define RT2X00_EWMA_FACTOR 1024
40#define RT2X00_EWMA_WEIGHT 8
41
42static inline int rt2x00link_get_avg_rssi(struct ewma *ewma)
43{
44 unsigned long avg;
45
46 avg = ewma_read(ewma);
47 if (avg)
48 return -avg;
49
50 return DEFAULT_RSSI;
51}
5352ff65 52
84e3196f
ID
53static int rt2x00link_antenna_get_link_rssi(struct rt2x00_dev *rt2x00dev)
54{
55 struct link_ant *ant = &rt2x00dev->link.ant;
56
1dc254ac
GJ
57 if (rt2x00dev->link.qual.rx_success)
58 return rt2x00link_get_avg_rssi(&ant->rssi_ant);
59
84e3196f
ID
60 return DEFAULT_RSSI;
61}
62
193df183 63static int rt2x00link_antenna_get_rssi_history(struct rt2x00_dev *rt2x00dev)
84e3196f
ID
64{
65 struct link_ant *ant = &rt2x00dev->link.ant;
66
193df183
LE
67 if (ant->rssi_history)
68 return ant->rssi_history;
84e3196f
ID
69 return DEFAULT_RSSI;
70}
84e3196f
ID
71
72static void rt2x00link_antenna_update_rssi_history(struct rt2x00_dev *rt2x00dev,
84e3196f
ID
73 int rssi)
74{
75 struct link_ant *ant = &rt2x00dev->link.ant;
193df183 76 ant->rssi_history = rssi;
84e3196f 77}
84e3196f
ID
78
79static void rt2x00link_antenna_reset(struct rt2x00_dev *rt2x00dev)
80{
1dc254ac
GJ
81 ewma_init(&rt2x00dev->link.ant.rssi_ant, RT2X00_EWMA_FACTOR,
82 RT2X00_EWMA_WEIGHT);
84e3196f
ID
83}
84
85static void rt2x00lib_antenna_diversity_sample(struct rt2x00_dev *rt2x00dev)
86{
87 struct link_ant *ant = &rt2x00dev->link.ant;
88 struct antenna_setup new_ant;
193df183
LE
89 int other_antenna;
90
91 int sample_current = rt2x00link_antenna_get_link_rssi(rt2x00dev);
92 int sample_other = rt2x00link_antenna_get_rssi_history(rt2x00dev);
84e3196f
ID
93
94 memcpy(&new_ant, &ant->active, sizeof(new_ant));
95
96 /*
97 * We are done sampling. Now we should evaluate the results.
98 */
99 ant->flags &= ~ANTENNA_MODE_SAMPLE;
100
101 /*
102 * During the last period we have sampled the RSSI
49513481 103 * from both antennas. It now is time to determine
84e3196f
ID
104 * which antenna demonstrated the best performance.
105 * When we are already on the antenna with the best
193df183
LE
106 * performance, just create a good starting point
107 * for the history and we are done.
84e3196f 108 */
193df183
LE
109 if (sample_current >= sample_other) {
110 rt2x00link_antenna_update_rssi_history(rt2x00dev,
111 sample_current);
84e3196f 112 return;
193df183
LE
113 }
114
115 other_antenna = (ant->active.rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
84e3196f
ID
116
117 if (ant->flags & ANTENNA_RX_DIVERSITY)
193df183 118 new_ant.rx = other_antenna;
84e3196f
ID
119
120 if (ant->flags & ANTENNA_TX_DIVERSITY)
193df183 121 new_ant.tx = other_antenna;
84e3196f 122
eb87eaac 123 rt2x00lib_config_antenna(rt2x00dev, new_ant);
84e3196f
ID
124}
125
126static void rt2x00lib_antenna_diversity_eval(struct rt2x00_dev *rt2x00dev)
127{
128 struct link_ant *ant = &rt2x00dev->link.ant;
129 struct antenna_setup new_ant;
130 int rssi_curr;
131 int rssi_old;
132
133 memcpy(&new_ant, &ant->active, sizeof(new_ant));
134
135 /*
136 * Get current RSSI value along with the historical value,
137 * after that update the history with the current value.
138 */
139 rssi_curr = rt2x00link_antenna_get_link_rssi(rt2x00dev);
193df183
LE
140 rssi_old = rt2x00link_antenna_get_rssi_history(rt2x00dev);
141 rt2x00link_antenna_update_rssi_history(rt2x00dev, rssi_curr);
84e3196f
ID
142
143 /*
144 * Legacy driver indicates that we should swap antenna's
145 * when the difference in RSSI is greater that 5. This
146 * also should be done when the RSSI was actually better
147 * then the previous sample.
148 * When the difference exceeds the threshold we should
149 * sample the rssi from the other antenna to make a valid
150 * comparison between the 2 antennas.
151 */
152 if (abs(rssi_curr - rssi_old) < 5)
153 return;
154
155 ant->flags |= ANTENNA_MODE_SAMPLE;
156
157 if (ant->flags & ANTENNA_RX_DIVERSITY)
158 new_ant.rx = (new_ant.rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
159
160 if (ant->flags & ANTENNA_TX_DIVERSITY)
161 new_ant.tx = (new_ant.tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
162
eb87eaac 163 rt2x00lib_config_antenna(rt2x00dev, new_ant);
84e3196f
ID
164}
165
193df183 166static bool rt2x00lib_antenna_diversity(struct rt2x00_dev *rt2x00dev)
84e3196f
ID
167{
168 struct link_ant *ant = &rt2x00dev->link.ant;
169
170 /*
171 * Determine if software diversity is enabled for
172 * either the TX or RX antenna (or both).
84e3196f 173 */
84e3196f
ID
174 if (!(ant->flags & ANTENNA_RX_DIVERSITY) &&
175 !(ant->flags & ANTENNA_TX_DIVERSITY)) {
176 ant->flags = 0;
193df183 177 return true;
84e3196f
ID
178 }
179
180 /*
181 * If we have only sampled the data over the last period
182 * we should now harvest the data. Otherwise just evaluate
183 * the data. The latter should only be performed once
184 * every 2 seconds.
185 */
193df183 186 if (ant->flags & ANTENNA_MODE_SAMPLE) {
84e3196f 187 rt2x00lib_antenna_diversity_sample(rt2x00dev);
193df183
LE
188 return true;
189 } else if (rt2x00dev->link.count & 1) {
84e3196f 190 rt2x00lib_antenna_diversity_eval(rt2x00dev);
193df183
LE
191 return true;
192 }
193
194 return false;
84e3196f
ID
195}
196
197void rt2x00link_update_stats(struct rt2x00_dev *rt2x00dev,
198 struct sk_buff *skb,
199 struct rxdone_entry_desc *rxdesc)
200{
5352ff65 201 struct link *link = &rt2x00dev->link;
84e3196f
ID
202 struct link_qual *qual = &rt2x00dev->link.qual;
203 struct link_ant *ant = &rt2x00dev->link.ant;
204 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
84e3196f 205
a23c2208
HS
206 /*
207 * No need to update the stats for !=STA interfaces
208 */
209 if (!rt2x00dev->intf_sta_count)
210 return;
211
84e3196f
ID
212 /*
213 * Frame was received successfully since non-succesfull
214 * frames would have been dropped by the hardware.
215 */
216 qual->rx_success++;
217
218 /*
219 * We are only interested in quality statistics from
220 * beacons which came from the BSS which we are
221 * associated with.
222 */
223 if (!ieee80211_is_beacon(hdr->frame_control) ||
224 !(rxdesc->dev_flags & RXDONE_MY_BSS))
225 return;
226
227 /*
228 * Update global RSSI
229 */
1dc254ac 230 ewma_add(&link->avg_rssi, -rxdesc->rssi);
84e3196f
ID
231
232 /*
233 * Update antenna RSSI
234 */
1dc254ac 235 ewma_add(&ant->rssi_ant, -rxdesc->rssi);
84e3196f
ID
236}
237
84e3196f
ID
238void rt2x00link_start_tuner(struct rt2x00_dev *rt2x00dev)
239{
5352ff65 240 struct link *link = &rt2x00dev->link;
84e3196f
ID
241
242 /*
243 * Link tuning should only be performed when
8654b79f
HS
244 * an active sta interface exists. AP interfaces
245 * don't need link tuning and monitor mode interfaces
246 * should never have to work with link tuners.
84e3196f 247 */
8654b79f 248 if (!rt2x00dev->intf_sta_count)
84e3196f
ID
249 return;
250
d8147f9d
ID
251 /**
252 * While scanning, link tuning is disabled. By default
253 * the most sensitive settings will be used to make sure
25985edc 254 * that all beacons and probe responses will be received
d8147f9d
ID
255 * during the scan.
256 */
257 if (test_bit(DEVICE_STATE_SCANNING, &rt2x00dev->flags))
258 return;
259
84e3196f
ID
260 rt2x00link_reset_tuner(rt2x00dev, false);
261
66f84d65
SC
262 if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
263 ieee80211_queue_delayed_work(rt2x00dev->hw,
264 &link->work, LINK_TUNE_INTERVAL);
84e3196f
ID
265}
266
267void rt2x00link_stop_tuner(struct rt2x00_dev *rt2x00dev)
268{
269 cancel_delayed_work_sync(&rt2x00dev->link.work);
270}
271
272void rt2x00link_reset_tuner(struct rt2x00_dev *rt2x00dev, bool antenna)
273{
5352ff65 274 struct link_qual *qual = &rt2x00dev->link.qual;
223dcc26 275 u8 vgc_level = qual->vgc_level_reg;
5352ff65 276
84e3196f
ID
277 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
278 return;
279
280 /*
281 * Reset link information.
282 * Both the currently active vgc level as well as
283 * the link tuner counter should be reset. Resetting
284 * the counter is important for devices where the
285 * device should only perform link tuning during the
286 * first minute after being enabled.
287 */
288 rt2x00dev->link.count = 0;
5352ff65 289 memset(qual, 0, sizeof(*qual));
1dc254ac
GJ
290 ewma_init(&rt2x00dev->link.avg_rssi, RT2X00_EWMA_FACTOR,
291 RT2X00_EWMA_WEIGHT);
84e3196f 292
223dcc26
ID
293 /*
294 * Restore the VGC level as stored in the registers,
295 * the driver can use this to determine if the register
296 * must be updated during reset or not.
297 */
298 qual->vgc_level_reg = vgc_level;
299
84e3196f
ID
300 /*
301 * Reset the link tuner.
302 */
5352ff65 303 rt2x00dev->ops->lib->reset_tuner(rt2x00dev, qual);
84e3196f
ID
304
305 if (antenna)
306 rt2x00link_antenna_reset(rt2x00dev);
307}
308
a57e2e84 309static void rt2x00link_reset_qual(struct rt2x00_dev *rt2x00dev)
64abd803
ID
310{
311 struct link_qual *qual = &rt2x00dev->link.qual;
312
313 qual->rx_success = 0;
314 qual->rx_failed = 0;
315 qual->tx_success = 0;
316 qual->tx_failed = 0;
317}
318
84e3196f
ID
319static void rt2x00link_tuner(struct work_struct *work)
320{
321 struct rt2x00_dev *rt2x00dev =
322 container_of(work, struct rt2x00_dev, link.work.work);
5352ff65 323 struct link *link = &rt2x00dev->link;
84e3196f
ID
324 struct link_qual *qual = &rt2x00dev->link.qual;
325
326 /*
327 * When the radio is shutting down we should
328 * immediately cease all link tuning.
329 */
d8147f9d
ID
330 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags) ||
331 test_bit(DEVICE_STATE_SCANNING, &rt2x00dev->flags))
84e3196f
ID
332 return;
333
334 /*
335 * Update statistics.
336 */
337 rt2x00dev->ops->lib->link_stats(rt2x00dev, qual);
338 rt2x00dev->low_level_stats.dot11FCSErrorCount += qual->rx_failed;
339
5352ff65
ID
340 /*
341 * Update quality RSSI for link tuning,
342 * when we have received some frames and we managed to
343 * collect the RSSI data we could use this. Otherwise we
344 * must fallback to the default RSSI value.
345 */
1dc254ac 346 if (!qual->rx_success)
5352ff65
ID
347 qual->rssi = DEFAULT_RSSI;
348 else
1dc254ac 349 qual->rssi = rt2x00link_get_avg_rssi(&link->avg_rssi);
5352ff65 350
84e3196f 351 /*
27df2a9c
ID
352 * Check if link tuning is supported by the hardware, some hardware
353 * do not support link tuning at all, while other devices can disable
354 * the feature from the EEPROM.
84e3196f 355 */
7b8a00dc 356 if (rt2x00_has_cap_link_tuning(rt2x00dev))
5352ff65 357 rt2x00dev->ops->lib->link_tuner(rt2x00dev, qual, link->count);
84e3196f 358
84e3196f
ID
359 /*
360 * Send a signal to the led to update the led signal strength.
361 */
66679a65 362 rt2x00leds_led_quality(rt2x00dev, qual->rssi);
84e3196f
ID
363
364 /*
193df183
LE
365 * Evaluate antenna setup, make this the last step when
366 * rt2x00lib_antenna_diversity made changes the quality
367 * statistics will be reset.
64abd803 368 */
193df183
LE
369 if (rt2x00lib_antenna_diversity(rt2x00dev))
370 rt2x00link_reset_qual(rt2x00dev);
64abd803 371
84e3196f
ID
372 /*
373 * Increase tuner counter, and reschedule the next link tuner run.
374 */
5352ff65 375 link->count++;
66f84d65
SC
376
377 if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
378 ieee80211_queue_delayed_work(rt2x00dev->hw,
379 &link->work, LINK_TUNE_INTERVAL);
84e3196f
ID
380}
381
c965c74b
ID
382void rt2x00link_start_watchdog(struct rt2x00_dev *rt2x00dev)
383{
384 struct link *link = &rt2x00dev->link;
385
aca7305b
HS
386 if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) &&
387 rt2x00dev->ops->lib->watchdog)
388 ieee80211_queue_delayed_work(rt2x00dev->hw,
389 &link->watchdog_work,
390 WATCHDOG_INTERVAL);
c965c74b
ID
391}
392
393void rt2x00link_stop_watchdog(struct rt2x00_dev *rt2x00dev)
394{
395 cancel_delayed_work_sync(&rt2x00dev->link.watchdog_work);
396}
397
398static void rt2x00link_watchdog(struct work_struct *work)
399{
400 struct rt2x00_dev *rt2x00dev =
401 container_of(work, struct rt2x00_dev, link.watchdog_work.work);
402 struct link *link = &rt2x00dev->link;
403
404 /*
405 * When the radio is shutting down we should
406 * immediately cease the watchdog monitoring.
407 */
408 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
409 return;
410
411 rt2x00dev->ops->lib->watchdog(rt2x00dev);
412
413 if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
0439f536
ID
414 ieee80211_queue_delayed_work(rt2x00dev->hw,
415 &link->watchdog_work,
416 WATCHDOG_INTERVAL);
c965c74b
ID
417}
418
9e33a355
HS
419void rt2x00link_start_agc(struct rt2x00_dev *rt2x00dev)
420{
421 struct link *link = &rt2x00dev->link;
422
423 if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) &&
424 rt2x00dev->ops->lib->gain_calibration)
425 ieee80211_queue_delayed_work(rt2x00dev->hw,
426 &link->agc_work,
427 AGC_INTERVAL);
428}
429
2e9c43dd
JL
430void rt2x00link_start_vcocal(struct rt2x00_dev *rt2x00dev)
431{
432 struct link *link = &rt2x00dev->link;
433
434 if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) &&
435 rt2x00dev->ops->lib->vco_calibration)
436 ieee80211_queue_delayed_work(rt2x00dev->hw,
437 &link->vco_work,
438 VCO_INTERVAL);
439}
440
9e33a355
HS
441void rt2x00link_stop_agc(struct rt2x00_dev *rt2x00dev)
442{
443 cancel_delayed_work_sync(&rt2x00dev->link.agc_work);
444}
445
2e9c43dd
JL
446void rt2x00link_stop_vcocal(struct rt2x00_dev *rt2x00dev)
447{
448 cancel_delayed_work_sync(&rt2x00dev->link.vco_work);
449}
450
9e33a355
HS
451static void rt2x00link_agc(struct work_struct *work)
452{
453 struct rt2x00_dev *rt2x00dev =
454 container_of(work, struct rt2x00_dev, link.agc_work.work);
455 struct link *link = &rt2x00dev->link;
456
457 /*
458 * When the radio is shutting down we should
459 * immediately cease the watchdog monitoring.
460 */
461 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
462 return;
463
464 rt2x00dev->ops->lib->gain_calibration(rt2x00dev);
465
466 if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
467 ieee80211_queue_delayed_work(rt2x00dev->hw,
468 &link->agc_work,
469 AGC_INTERVAL);
470}
471
2e9c43dd
JL
472static void rt2x00link_vcocal(struct work_struct *work)
473{
474 struct rt2x00_dev *rt2x00dev =
475 container_of(work, struct rt2x00_dev, link.vco_work.work);
476 struct link *link = &rt2x00dev->link;
477
478 /*
479 * When the radio is shutting down we should
480 * immediately cease the VCO calibration.
481 */
482 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
483 return;
484
485 rt2x00dev->ops->lib->vco_calibration(rt2x00dev);
486
487 if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
488 ieee80211_queue_delayed_work(rt2x00dev->hw,
489 &link->vco_work,
490 VCO_INTERVAL);
491}
492
84e3196f
ID
493void rt2x00link_register(struct rt2x00_dev *rt2x00dev)
494{
9e33a355 495 INIT_DELAYED_WORK(&rt2x00dev->link.agc_work, rt2x00link_agc);
7b8a00dc 496 if (rt2x00_has_cap_vco_recalibration(rt2x00dev))
2e9c43dd 497 INIT_DELAYED_WORK(&rt2x00dev->link.vco_work, rt2x00link_vcocal);
c965c74b 498 INIT_DELAYED_WORK(&rt2x00dev->link.watchdog_work, rt2x00link_watchdog);
84e3196f
ID
499 INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00link_tuner);
500}
This page took 0.540026 seconds and 5 git commands to generate.