]>
Commit | Line | Data |
---|---|---|
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 | ||
66679a65 LE |
38 | /* |
39 | * Helper struct and macro to work with moving/walking averages. | |
5352ff65 ID |
40 | * When adding a value to the average value the following calculation |
41 | * is needed: | |
42 | * | |
43 | * avg_rssi = ((avg_rssi * 7) + rssi) / 8; | |
44 | * | |
45 | * The advantage of this approach is that we only need 1 variable | |
46 | * to store the average in (No need for a count and a total). | |
47 | * But more importantly, normal average values will over time | |
48 | * move less and less towards newly added values this results | |
49 | * that with link tuning, the device can have a very good RSSI | |
50 | * for a few minutes but when the device is moved away from the AP | |
51 | * the average will not decrease fast enough to compensate. | |
52 | * The walking average compensates this and will move towards | |
66679a65 LE |
53 | * the new values correctly allowing a effective link tuning, |
54 | * the speed of the average moving towards other values depends | |
55 | * on the value for the number of samples. The higher the number | |
56 | * of samples, the slower the average will move. | |
57 | * We use two variables to keep track of the average value to | |
58 | * compensate for the rounding errors. This can be a significant | |
59 | * error (>5dBm) if the factor is too low. | |
5352ff65 | 60 | */ |
66679a65 LE |
61 | #define AVG_SAMPLES 8 |
62 | #define AVG_FACTOR 1000 | |
63 | #define MOVING_AVERAGE(__avg, __val) \ | |
64 | ({ \ | |
65 | struct avg_val __new; \ | |
66 | __new.avg_weight = \ | |
67 | (__avg).avg_weight ? \ | |
68 | ((((__avg).avg_weight * ((AVG_SAMPLES) - 1)) + \ | |
69 | ((__val) * (AVG_FACTOR))) / \ | |
70 | (AVG_SAMPLES) ) : \ | |
71 | ((__val) * (AVG_FACTOR)); \ | |
72 | __new.avg = __new.avg_weight / (AVG_FACTOR); \ | |
73 | __new; \ | |
74 | }) | |
5352ff65 | 75 | |
84e3196f ID |
76 | static int rt2x00link_antenna_get_link_rssi(struct rt2x00_dev *rt2x00dev) |
77 | { | |
78 | struct link_ant *ant = &rt2x00dev->link.ant; | |
79 | ||
66679a65 LE |
80 | if (ant->rssi_ant.avg && rt2x00dev->link.qual.rx_success) |
81 | return ant->rssi_ant.avg; | |
84e3196f ID |
82 | return DEFAULT_RSSI; |
83 | } | |
84 | ||
193df183 | 85 | static int rt2x00link_antenna_get_rssi_history(struct rt2x00_dev *rt2x00dev) |
84e3196f ID |
86 | { |
87 | struct link_ant *ant = &rt2x00dev->link.ant; | |
88 | ||
193df183 LE |
89 | if (ant->rssi_history) |
90 | return ant->rssi_history; | |
84e3196f ID |
91 | return DEFAULT_RSSI; |
92 | } | |
84e3196f ID |
93 | |
94 | static void rt2x00link_antenna_update_rssi_history(struct rt2x00_dev *rt2x00dev, | |
84e3196f ID |
95 | int rssi) |
96 | { | |
97 | struct link_ant *ant = &rt2x00dev->link.ant; | |
193df183 | 98 | ant->rssi_history = rssi; |
84e3196f | 99 | } |
84e3196f ID |
100 | |
101 | static void rt2x00link_antenna_reset(struct rt2x00_dev *rt2x00dev) | |
102 | { | |
66679a65 LE |
103 | rt2x00dev->link.ant.rssi_ant.avg = 0; |
104 | rt2x00dev->link.ant.rssi_ant.avg_weight = 0; | |
84e3196f ID |
105 | } |
106 | ||
107 | static void rt2x00lib_antenna_diversity_sample(struct rt2x00_dev *rt2x00dev) | |
108 | { | |
109 | struct link_ant *ant = &rt2x00dev->link.ant; | |
110 | struct antenna_setup new_ant; | |
193df183 LE |
111 | int other_antenna; |
112 | ||
113 | int sample_current = rt2x00link_antenna_get_link_rssi(rt2x00dev); | |
114 | int sample_other = rt2x00link_antenna_get_rssi_history(rt2x00dev); | |
84e3196f ID |
115 | |
116 | memcpy(&new_ant, &ant->active, sizeof(new_ant)); | |
117 | ||
118 | /* | |
119 | * We are done sampling. Now we should evaluate the results. | |
120 | */ | |
121 | ant->flags &= ~ANTENNA_MODE_SAMPLE; | |
122 | ||
123 | /* | |
124 | * During the last period we have sampled the RSSI | |
49513481 | 125 | * from both antennas. It now is time to determine |
84e3196f ID |
126 | * which antenna demonstrated the best performance. |
127 | * When we are already on the antenna with the best | |
193df183 LE |
128 | * performance, just create a good starting point |
129 | * for the history and we are done. | |
84e3196f | 130 | */ |
193df183 LE |
131 | if (sample_current >= sample_other) { |
132 | rt2x00link_antenna_update_rssi_history(rt2x00dev, | |
133 | sample_current); | |
84e3196f | 134 | return; |
193df183 LE |
135 | } |
136 | ||
137 | other_antenna = (ant->active.rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A; | |
84e3196f ID |
138 | |
139 | if (ant->flags & ANTENNA_RX_DIVERSITY) | |
193df183 | 140 | new_ant.rx = other_antenna; |
84e3196f ID |
141 | |
142 | if (ant->flags & ANTENNA_TX_DIVERSITY) | |
193df183 | 143 | new_ant.tx = other_antenna; |
84e3196f | 144 | |
eb87eaac | 145 | rt2x00lib_config_antenna(rt2x00dev, new_ant); |
84e3196f ID |
146 | } |
147 | ||
148 | static void rt2x00lib_antenna_diversity_eval(struct rt2x00_dev *rt2x00dev) | |
149 | { | |
150 | struct link_ant *ant = &rt2x00dev->link.ant; | |
151 | struct antenna_setup new_ant; | |
152 | int rssi_curr; | |
153 | int rssi_old; | |
154 | ||
155 | memcpy(&new_ant, &ant->active, sizeof(new_ant)); | |
156 | ||
157 | /* | |
158 | * Get current RSSI value along with the historical value, | |
159 | * after that update the history with the current value. | |
160 | */ | |
161 | rssi_curr = rt2x00link_antenna_get_link_rssi(rt2x00dev); | |
193df183 LE |
162 | rssi_old = rt2x00link_antenna_get_rssi_history(rt2x00dev); |
163 | rt2x00link_antenna_update_rssi_history(rt2x00dev, rssi_curr); | |
84e3196f ID |
164 | |
165 | /* | |
166 | * Legacy driver indicates that we should swap antenna's | |
167 | * when the difference in RSSI is greater that 5. This | |
168 | * also should be done when the RSSI was actually better | |
169 | * then the previous sample. | |
170 | * When the difference exceeds the threshold we should | |
171 | * sample the rssi from the other antenna to make a valid | |
172 | * comparison between the 2 antennas. | |
173 | */ | |
174 | if (abs(rssi_curr - rssi_old) < 5) | |
175 | return; | |
176 | ||
177 | ant->flags |= ANTENNA_MODE_SAMPLE; | |
178 | ||
179 | if (ant->flags & ANTENNA_RX_DIVERSITY) | |
180 | new_ant.rx = (new_ant.rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A; | |
181 | ||
182 | if (ant->flags & ANTENNA_TX_DIVERSITY) | |
183 | new_ant.tx = (new_ant.tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A; | |
184 | ||
eb87eaac | 185 | rt2x00lib_config_antenna(rt2x00dev, new_ant); |
84e3196f ID |
186 | } |
187 | ||
193df183 | 188 | static bool rt2x00lib_antenna_diversity(struct rt2x00_dev *rt2x00dev) |
84e3196f ID |
189 | { |
190 | struct link_ant *ant = &rt2x00dev->link.ant; | |
bdfa500b | 191 | unsigned int flags = ant->flags; |
84e3196f ID |
192 | |
193 | /* | |
194 | * Determine if software diversity is enabled for | |
195 | * either the TX or RX antenna (or both). | |
196 | * Always perform this check since within the link | |
197 | * tuner interval the configuration might have changed. | |
198 | */ | |
bdfa500b ID |
199 | flags &= ~ANTENNA_RX_DIVERSITY; |
200 | flags &= ~ANTENNA_TX_DIVERSITY; | |
84e3196f ID |
201 | |
202 | if (rt2x00dev->default_ant.rx == ANTENNA_SW_DIVERSITY) | |
bdfa500b | 203 | flags |= ANTENNA_RX_DIVERSITY; |
84e3196f | 204 | if (rt2x00dev->default_ant.tx == ANTENNA_SW_DIVERSITY) |
bdfa500b | 205 | flags |= ANTENNA_TX_DIVERSITY; |
84e3196f ID |
206 | |
207 | if (!(ant->flags & ANTENNA_RX_DIVERSITY) && | |
208 | !(ant->flags & ANTENNA_TX_DIVERSITY)) { | |
209 | ant->flags = 0; | |
193df183 | 210 | return true; |
84e3196f ID |
211 | } |
212 | ||
bdfa500b ID |
213 | /* Update flags */ |
214 | ant->flags = flags; | |
215 | ||
84e3196f ID |
216 | /* |
217 | * If we have only sampled the data over the last period | |
218 | * we should now harvest the data. Otherwise just evaluate | |
219 | * the data. The latter should only be performed once | |
220 | * every 2 seconds. | |
221 | */ | |
193df183 | 222 | if (ant->flags & ANTENNA_MODE_SAMPLE) { |
84e3196f | 223 | rt2x00lib_antenna_diversity_sample(rt2x00dev); |
193df183 LE |
224 | return true; |
225 | } else if (rt2x00dev->link.count & 1) { | |
84e3196f | 226 | rt2x00lib_antenna_diversity_eval(rt2x00dev); |
193df183 LE |
227 | return true; |
228 | } | |
229 | ||
230 | return false; | |
84e3196f ID |
231 | } |
232 | ||
233 | void rt2x00link_update_stats(struct rt2x00_dev *rt2x00dev, | |
234 | struct sk_buff *skb, | |
235 | struct rxdone_entry_desc *rxdesc) | |
236 | { | |
5352ff65 | 237 | struct link *link = &rt2x00dev->link; |
84e3196f ID |
238 | struct link_qual *qual = &rt2x00dev->link.qual; |
239 | struct link_ant *ant = &rt2x00dev->link.ant; | |
240 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; | |
84e3196f ID |
241 | |
242 | /* | |
243 | * Frame was received successfully since non-succesfull | |
244 | * frames would have been dropped by the hardware. | |
245 | */ | |
246 | qual->rx_success++; | |
247 | ||
248 | /* | |
249 | * We are only interested in quality statistics from | |
250 | * beacons which came from the BSS which we are | |
251 | * associated with. | |
252 | */ | |
253 | if (!ieee80211_is_beacon(hdr->frame_control) || | |
254 | !(rxdesc->dev_flags & RXDONE_MY_BSS)) | |
255 | return; | |
256 | ||
257 | /* | |
258 | * Update global RSSI | |
259 | */ | |
66679a65 | 260 | link->avg_rssi = MOVING_AVERAGE(link->avg_rssi, rxdesc->rssi); |
84e3196f ID |
261 | |
262 | /* | |
263 | * Update antenna RSSI | |
264 | */ | |
66679a65 | 265 | ant->rssi_ant = MOVING_AVERAGE(ant->rssi_ant, rxdesc->rssi); |
84e3196f ID |
266 | } |
267 | ||
84e3196f ID |
268 | void rt2x00link_start_tuner(struct rt2x00_dev *rt2x00dev) |
269 | { | |
5352ff65 | 270 | struct link *link = &rt2x00dev->link; |
84e3196f ID |
271 | |
272 | /* | |
273 | * Link tuning should only be performed when | |
274 | * an active sta or master interface exists. | |
275 | * Single monitor mode interfaces should never have | |
276 | * work with link tuners. | |
277 | */ | |
278 | if (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count) | |
279 | return; | |
280 | ||
84e3196f ID |
281 | rt2x00link_reset_tuner(rt2x00dev, false); |
282 | ||
66f84d65 SC |
283 | if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags)) |
284 | ieee80211_queue_delayed_work(rt2x00dev->hw, | |
285 | &link->work, LINK_TUNE_INTERVAL); | |
84e3196f ID |
286 | } |
287 | ||
288 | void rt2x00link_stop_tuner(struct rt2x00_dev *rt2x00dev) | |
289 | { | |
290 | cancel_delayed_work_sync(&rt2x00dev->link.work); | |
291 | } | |
292 | ||
293 | void rt2x00link_reset_tuner(struct rt2x00_dev *rt2x00dev, bool antenna) | |
294 | { | |
5352ff65 ID |
295 | struct link_qual *qual = &rt2x00dev->link.qual; |
296 | ||
84e3196f ID |
297 | if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags)) |
298 | return; | |
299 | ||
300 | /* | |
301 | * Reset link information. | |
302 | * Both the currently active vgc level as well as | |
303 | * the link tuner counter should be reset. Resetting | |
304 | * the counter is important for devices where the | |
305 | * device should only perform link tuning during the | |
306 | * first minute after being enabled. | |
307 | */ | |
308 | rt2x00dev->link.count = 0; | |
5352ff65 | 309 | memset(qual, 0, sizeof(*qual)); |
84e3196f ID |
310 | |
311 | /* | |
312 | * Reset the link tuner. | |
313 | */ | |
5352ff65 | 314 | rt2x00dev->ops->lib->reset_tuner(rt2x00dev, qual); |
84e3196f ID |
315 | |
316 | if (antenna) | |
317 | rt2x00link_antenna_reset(rt2x00dev); | |
318 | } | |
319 | ||
a57e2e84 | 320 | static void rt2x00link_reset_qual(struct rt2x00_dev *rt2x00dev) |
64abd803 ID |
321 | { |
322 | struct link_qual *qual = &rt2x00dev->link.qual; | |
323 | ||
324 | qual->rx_success = 0; | |
325 | qual->rx_failed = 0; | |
326 | qual->tx_success = 0; | |
327 | qual->tx_failed = 0; | |
328 | } | |
329 | ||
84e3196f ID |
330 | static void rt2x00link_tuner(struct work_struct *work) |
331 | { | |
332 | struct rt2x00_dev *rt2x00dev = | |
333 | container_of(work, struct rt2x00_dev, link.work.work); | |
5352ff65 | 334 | struct link *link = &rt2x00dev->link; |
84e3196f ID |
335 | struct link_qual *qual = &rt2x00dev->link.qual; |
336 | ||
337 | /* | |
338 | * When the radio is shutting down we should | |
339 | * immediately cease all link tuning. | |
340 | */ | |
341 | if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags)) | |
342 | return; | |
343 | ||
344 | /* | |
345 | * Update statistics. | |
346 | */ | |
347 | rt2x00dev->ops->lib->link_stats(rt2x00dev, qual); | |
348 | rt2x00dev->low_level_stats.dot11FCSErrorCount += qual->rx_failed; | |
349 | ||
5352ff65 ID |
350 | /* |
351 | * Update quality RSSI for link tuning, | |
352 | * when we have received some frames and we managed to | |
353 | * collect the RSSI data we could use this. Otherwise we | |
354 | * must fallback to the default RSSI value. | |
355 | */ | |
66679a65 | 356 | if (!link->avg_rssi.avg || !qual->rx_success) |
5352ff65 ID |
357 | qual->rssi = DEFAULT_RSSI; |
358 | else | |
66679a65 | 359 | qual->rssi = link->avg_rssi.avg; |
5352ff65 | 360 | |
84e3196f ID |
361 | /* |
362 | * Only perform the link tuning when Link tuning | |
363 | * has been enabled (This could have been disabled from the EEPROM). | |
364 | */ | |
365 | if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags)) | |
5352ff65 | 366 | rt2x00dev->ops->lib->link_tuner(rt2x00dev, qual, link->count); |
84e3196f | 367 | |
84e3196f ID |
368 | /* |
369 | * Send a signal to the led to update the led signal strength. | |
370 | */ | |
66679a65 | 371 | rt2x00leds_led_quality(rt2x00dev, qual->rssi); |
84e3196f ID |
372 | |
373 | /* | |
193df183 LE |
374 | * Evaluate antenna setup, make this the last step when |
375 | * rt2x00lib_antenna_diversity made changes the quality | |
376 | * statistics will be reset. | |
64abd803 | 377 | */ |
193df183 LE |
378 | if (rt2x00lib_antenna_diversity(rt2x00dev)) |
379 | rt2x00link_reset_qual(rt2x00dev); | |
64abd803 | 380 | |
84e3196f ID |
381 | /* |
382 | * Increase tuner counter, and reschedule the next link tuner run. | |
383 | */ | |
5352ff65 | 384 | link->count++; |
66f84d65 SC |
385 | |
386 | if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags)) | |
387 | ieee80211_queue_delayed_work(rt2x00dev->hw, | |
388 | &link->work, LINK_TUNE_INTERVAL); | |
84e3196f ID |
389 | } |
390 | ||
391 | void rt2x00link_register(struct rt2x00_dev *rt2x00dev) | |
392 | { | |
393 | INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00link_tuner); | |
394 | } |