aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/wireless/rt2x00/rt2x00link.c
diff options
context:
space:
mode:
authorIvo van Doorn <ivdoorn@gmail.com>2008-12-20 04:53:29 -0500
committerJohn W. Linville <linville@tuxdriver.com>2009-01-29 15:58:34 -0500
commit84e3196ff867c623056eea02c11a45e046490d89 (patch)
treea2f5c1d69a63b22f202713a501762a777e04d19a /drivers/net/wireless/rt2x00/rt2x00link.c
parent7d7f19ccb777946df0a8fb7c83189ba2ae08b02e (diff)
rt2x00: Move link tuning into seperate file
Move link and antenna tuning into a seperate file named rt2x00link.c, this makes the interface to the link tuner a lot cleaner. Signed-off-by: Ivo van Doorn <IvDoorn@gmail.com> Signed-off-by: John W. Linville <linville@tuxdriver.com>
Diffstat (limited to 'drivers/net/wireless/rt2x00/rt2x00link.c')
-rw-r--r--drivers/net/wireless/rt2x00/rt2x00link.c402
1 files changed, 402 insertions, 0 deletions
diff --git a/drivers/net/wireless/rt2x00/rt2x00link.c b/drivers/net/wireless/rt2x00/rt2x00link.c
new file mode 100644
index 000000000000..0462d5ab6e97
--- /dev/null
+++ b/drivers/net/wireless/rt2x00/rt2x00link.c
@@ -0,0 +1,402 @@
1/*
2 Copyright (C) 2004 - 2008 rt2x00 SourceForge Project
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
32static int rt2x00link_antenna_get_link_rssi(struct rt2x00_dev *rt2x00dev)
33{
34 struct link_ant *ant = &rt2x00dev->link.ant;
35
36 if (ant->rssi_ant && rt2x00dev->link.qual.rx_success)
37 return ant->rssi_ant;
38 return DEFAULT_RSSI;
39}
40
41static int rt2x00link_antenna_get_rssi_history(struct rt2x00_dev *rt2x00dev,
42 enum antenna antenna)
43{
44 struct link_ant *ant = &rt2x00dev->link.ant;
45
46 if (ant->rssi_history[antenna - ANTENNA_A])
47 return ant->rssi_history[antenna - ANTENNA_A];
48 return DEFAULT_RSSI;
49}
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)
57
58static void rt2x00link_antenna_update_rssi_history(struct rt2x00_dev *rt2x00dev,
59 enum antenna antenna,
60 int rssi)
61{
62 struct link_ant *ant = &rt2x00dev->link.ant;
63 ant->rssi_history[ant->active.rx - ANTENNA_A] = rssi;
64}
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, \
69 (__rssi))
70#define rt2x00link_antenna_update_rssi_tx_history(__dev, __rssi) \
71 rt2x00link_antenna_update_rssi_history((__dev), \
72 (__dev)->link.ant.active.tx, \
73 (__rssi))
74
75static void rt2x00link_antenna_reset(struct rt2x00_dev *rt2x00dev)
76{
77 rt2x00dev->link.ant.rssi_ant = 0;
78}
79
80static void rt2x00lib_antenna_diversity_sample(struct rt2x00_dev *rt2x00dev)
81{
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);
86
87 memcpy(&new_ant, &ant->active, sizeof(new_ant));
88
89 /*
90 * We are done sampling. Now we should evaluate the results.
91 */
92 ant->flags &= ~ANTENNA_MODE_SAMPLE;
93
94 /*
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
100 * left to do.
101 */
102 if (sample_a == sample_b)
103 return;
104
105 if (ant->flags & ANTENNA_RX_DIVERSITY)
106 new_ant.rx = (sample_a > sample_b) ? ANTENNA_A : ANTENNA_B;
107
108 if (ant->flags & ANTENNA_TX_DIVERSITY)
109 new_ant.tx = (sample_a > sample_b) ? ANTENNA_A : ANTENNA_B;
110
111 rt2x00lib_config_antenna(rt2x00dev, &new_ant);
112}
113
114static void rt2x00lib_antenna_diversity_eval(struct rt2x00_dev *rt2x00dev)
115{
116 struct link_ant *ant = &rt2x00dev->link.ant;
117 struct antenna_setup new_ant;
118 int rssi_curr;
119 int rssi_old;
120
121 memcpy(&new_ant, &ant->active, sizeof(new_ant));
122
123 /*
124 * Get current RSSI value along with the historical value,
125 * after that update the history with the current value.
126 */
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);
130
131 /*
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.
139 */
140 if (abs(rssi_curr - rssi_old) < 5)
141 return;
142
143 ant->flags |= ANTENNA_MODE_SAMPLE;
144
145 if (ant->flags & ANTENNA_RX_DIVERSITY)
146 new_ant.rx = (new_ant.rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
147
148 if (ant->flags & ANTENNA_TX_DIVERSITY)
149 new_ant.tx = (new_ant.tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
150
151 rt2x00lib_config_antenna(rt2x00dev, &new_ant);
152}
153
154static void rt2x00lib_antenna_diversity(struct rt2x00_dev *rt2x00dev)
155{
156 struct link_ant *ant = &rt2x00dev->link.ant;
157
158 /*
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.
163 */
164 ant->flags &= ~ANTENNA_RX_DIVERSITY;
165 ant->flags &= ~ANTENNA_TX_DIVERSITY;
166
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;
171
172 if (!(ant->flags & ANTENNA_RX_DIVERSITY) &&
173 !(ant->flags & ANTENNA_TX_DIVERSITY)) {
174 ant->flags = 0;
175 return;
176 }
177
178 /*
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
182 * every 2 seconds.
183 */
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);
188}
189
190void rt2x00link_update_stats(struct rt2x00_dev *rt2x00dev,
191 struct sk_buff *skb,
192 struct rxdone_entry_desc *rxdesc)
193{
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;
199
200 /*
201 * Frame was received successfully since non-succesfull
202 * frames would have been dropped by the hardware.
203 */
204 qual->rx_success++;
205
206 /*
207 * We are only interested in quality statistics from
208 * beacons which came from the BSS which we are
209 * associated with.
210 */
211 if (!ieee80211_is_beacon(hdr->frame_control) ||
212 !(rxdesc->dev_flags & RXDONE_MY_BSS))
213 return;
214
215 /*
216 * Update global RSSI
217 */
218 if (qual->avg_rssi)
219 avg_rssi = MOVING_AVERAGE(qual->avg_rssi, rxdesc->rssi, 8);
220 qual->avg_rssi = avg_rssi;
221
222 /*
223 * Update antenna RSSI
224 */
225 if (ant->rssi_ant)
226 ant_rssi = MOVING_AVERAGE(ant->rssi_ant, rxdesc->rssi, 8);
227 ant->rssi_ant = ant_rssi;
228}
229
230static void rt2x00link_precalculate_signal(struct rt2x00_dev *rt2x00dev)
231{
232 struct link_qual *qual = &rt2x00dev->link.qual;
233
234 if (qual->rx_failed || qual->rx_success)
235 qual->rx_percentage =
236 (qual->rx_success * 100) /
237 (qual->rx_failed + qual->rx_success);
238 else
239 qual->rx_percentage = 50;
240
241 if (qual->tx_failed || qual->tx_success)
242 qual->tx_percentage =
243 (qual->tx_success * 100) /
244 (qual->tx_failed + qual->tx_success);
245 else
246 qual->tx_percentage = 50;
247
248 qual->rx_success = 0;
249 qual->rx_failed = 0;
250 qual->tx_success = 0;
251 qual->tx_failed = 0;
252}
253
254int rt2x00link_calculate_signal(struct rt2x00_dev *rt2x00dev, int rssi)
255{
256 struct link_qual *qual = &rt2x00dev->link.qual;
257 int rssi_percentage = 0;
258 int signal;
259
260 /*
261 * We need a positive value for the RSSI.
262 */
263 if (rssi < 0)
264 rssi += rt2x00dev->rssi_offset;
265
266 /*
267 * Calculate the different percentages,
268 * which will be used for the signal.
269 */
270 rssi_percentage = (rssi * 100) / rt2x00dev->rssi_offset;
271
272 /*
273 * Add the individual percentages and use the WEIGHT
274 * defines to calculate the current link signal.
275 */
276 signal = ((WEIGHT_RSSI * rssi_percentage) +
277 (WEIGHT_TX * qual->tx_percentage) +
278 (WEIGHT_RX * qual->rx_percentage)) / 100;
279
280 return max_t(int, signal, 100);
281}
282
283void rt2x00link_start_tuner(struct rt2x00_dev *rt2x00dev)
284{
285 struct link_qual *qual = &rt2x00dev->link.qual;
286
287 /*
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.
292 */
293 if (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count)
294 return;
295
296 /*
297 * Clear all (possibly) pre-existing quality statistics.
298 */
299 memset(qual, 0, sizeof(*qual));
300
301 /*
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.
308 */
309 qual->rx_percentage = 50;
310 qual->tx_percentage = 50;
311
312 rt2x00link_reset_tuner(rt2x00dev, false);
313
314 queue_delayed_work(rt2x00dev->hw->workqueue,
315 &rt2x00dev->link.work, LINK_TUNE_INTERVAL);
316}
317
318void rt2x00link_stop_tuner(struct rt2x00_dev *rt2x00dev)
319{
320 cancel_delayed_work_sync(&rt2x00dev->link.work);
321}
322
323void rt2x00link_reset_tuner(struct rt2x00_dev *rt2x00dev, bool antenna)
324{
325 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
326 return;
327
328 /*
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.
335 */
336 rt2x00dev->link.count = 0;
337 rt2x00dev->link.vgc_level = 0;
338
339 /*
340 * Reset the link tuner.
341 */
342 rt2x00dev->ops->lib->reset_tuner(rt2x00dev);
343
344 if (antenna)
345 rt2x00link_antenna_reset(rt2x00dev);
346}
347
348static void rt2x00link_tuner(struct work_struct *work)
349{
350 struct rt2x00_dev *rt2x00dev =
351 container_of(work, struct rt2x00_dev, link.work.work);
352 struct link_qual *qual = &rt2x00dev->link.qual;
353
354 /*
355 * When the radio is shutting down we should
356 * immediately cease all link tuning.
357 */
358 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
359 return;
360
361 /*
362 * Update statistics.
363 */
364 rt2x00dev->ops->lib->link_stats(rt2x00dev, qual);
365 rt2x00dev->low_level_stats.dot11FCSErrorCount += qual->rx_failed;
366
367 /*
368 * Only perform the link tuning when Link tuning
369 * has been enabled (This could have been disabled from the EEPROM).
370 */
371 if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
372 rt2x00dev->ops->lib->link_tuner(rt2x00dev);
373
374 /*
375 * Precalculate a portion of the link signal which is
376 * in based on the tx/rx success/failure counters.
377 */
378 rt2x00link_precalculate_signal(rt2x00dev);
379
380 /*
381 * Send a signal to the led to update the led signal strength.
382 */
383 rt2x00leds_led_quality(rt2x00dev, qual->avg_rssi);
384
385 /*
386 * Evaluate antenna setup, make this the last step since this could
387 * possibly reset some statistics.
388 */
389 rt2x00lib_antenna_diversity(rt2x00dev);
390
391 /*
392 * Increase tuner counter, and reschedule the next link tuner run.
393 */
394 rt2x00dev->link.count++;
395 queue_delayed_work(rt2x00dev->hw->workqueue,
396 &rt2x00dev->link.work, LINK_TUNE_INTERVAL);
397}
398
399void rt2x00link_register(struct rt2x00_dev *rt2x00dev)
400{
401 INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00link_tuner);
402}