Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 1 | /* |
Ivo van Doorn | 4e54c71 | 2009-01-17 20:42:32 +0100 | [diff] [blame] | 2 | Copyright (C) 2004 - 2009 rt2x00 SourceForge Project |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 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 | |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 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 | /* |
| 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 Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 97 | static 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 | |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame^] | 106 | static int rt2x00link_antenna_get_rssi_history(struct rt2x00_dev *rt2x00dev) |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 107 | { |
| 108 | struct link_ant *ant = &rt2x00dev->link.ant; |
| 109 | |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame^] | 110 | if (ant->rssi_history) |
| 111 | return ant->rssi_history; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 112 | return DEFAULT_RSSI; |
| 113 | } |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 114 | |
| 115 | static void rt2x00link_antenna_update_rssi_history(struct rt2x00_dev *rt2x00dev, |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 116 | int rssi) |
| 117 | { |
| 118 | struct link_ant *ant = &rt2x00dev->link.ant; |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame^] | 119 | ant->rssi_history = rssi; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 120 | } |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 121 | |
| 122 | static void rt2x00link_antenna_reset(struct rt2x00_dev *rt2x00dev) |
| 123 | { |
| 124 | rt2x00dev->link.ant.rssi_ant = 0; |
| 125 | } |
| 126 | |
| 127 | static void rt2x00lib_antenna_diversity_sample(struct rt2x00_dev *rt2x00dev) |
| 128 | { |
| 129 | struct link_ant *ant = &rt2x00dev->link.ant; |
| 130 | struct antenna_setup new_ant; |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame^] | 131 | int other_antenna; |
| 132 | |
| 133 | int sample_current = rt2x00link_antenna_get_link_rssi(rt2x00dev); |
| 134 | int sample_other = rt2x00link_antenna_get_rssi_history(rt2x00dev); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 135 | |
| 136 | memcpy(&new_ant, &ant->active, sizeof(new_ant)); |
| 137 | |
| 138 | /* |
| 139 | * We are done sampling. Now we should evaluate the results. |
| 140 | */ |
| 141 | ant->flags &= ~ANTENNA_MODE_SAMPLE; |
| 142 | |
| 143 | /* |
| 144 | * During the last period we have sampled the RSSI |
Luis Correia | 4951348 | 2009-07-17 21:39:19 +0200 | [diff] [blame] | 145 | * from both antennas. It now is time to determine |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 146 | * which antenna demonstrated the best performance. |
| 147 | * When we are already on the antenna with the best |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame^] | 148 | * performance, just create a good starting point |
| 149 | * for the history and we are done. |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 150 | */ |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame^] | 151 | if (sample_current >= sample_other) { |
| 152 | rt2x00link_antenna_update_rssi_history(rt2x00dev, |
| 153 | sample_current); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 154 | return; |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame^] | 155 | } |
| 156 | |
| 157 | other_antenna = (ant->active.rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 158 | |
| 159 | if (ant->flags & ANTENNA_RX_DIVERSITY) |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame^] | 160 | new_ant.rx = other_antenna; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 161 | |
| 162 | if (ant->flags & ANTENNA_TX_DIVERSITY) |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame^] | 163 | new_ant.tx = other_antenna; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 164 | |
Lars Ericsson | eb87eaa | 2009-07-18 20:21:52 +0200 | [diff] [blame] | 165 | rt2x00lib_config_antenna(rt2x00dev, new_ant); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static void rt2x00lib_antenna_diversity_eval(struct rt2x00_dev *rt2x00dev) |
| 169 | { |
| 170 | struct link_ant *ant = &rt2x00dev->link.ant; |
| 171 | struct antenna_setup new_ant; |
| 172 | int rssi_curr; |
| 173 | int rssi_old; |
| 174 | |
| 175 | memcpy(&new_ant, &ant->active, sizeof(new_ant)); |
| 176 | |
| 177 | /* |
| 178 | * Get current RSSI value along with the historical value, |
| 179 | * after that update the history with the current value. |
| 180 | */ |
| 181 | rssi_curr = rt2x00link_antenna_get_link_rssi(rt2x00dev); |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame^] | 182 | rssi_old = rt2x00link_antenna_get_rssi_history(rt2x00dev); |
| 183 | rt2x00link_antenna_update_rssi_history(rt2x00dev, rssi_curr); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 184 | |
| 185 | /* |
| 186 | * Legacy driver indicates that we should swap antenna's |
| 187 | * when the difference in RSSI is greater that 5. This |
| 188 | * also should be done when the RSSI was actually better |
| 189 | * then the previous sample. |
| 190 | * When the difference exceeds the threshold we should |
| 191 | * sample the rssi from the other antenna to make a valid |
| 192 | * comparison between the 2 antennas. |
| 193 | */ |
| 194 | if (abs(rssi_curr - rssi_old) < 5) |
| 195 | return; |
| 196 | |
| 197 | ant->flags |= ANTENNA_MODE_SAMPLE; |
| 198 | |
| 199 | if (ant->flags & ANTENNA_RX_DIVERSITY) |
| 200 | new_ant.rx = (new_ant.rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A; |
| 201 | |
| 202 | if (ant->flags & ANTENNA_TX_DIVERSITY) |
| 203 | new_ant.tx = (new_ant.tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A; |
| 204 | |
Lars Ericsson | eb87eaa | 2009-07-18 20:21:52 +0200 | [diff] [blame] | 205 | rt2x00lib_config_antenna(rt2x00dev, new_ant); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 206 | } |
| 207 | |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame^] | 208 | static bool rt2x00lib_antenna_diversity(struct rt2x00_dev *rt2x00dev) |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 209 | { |
| 210 | struct link_ant *ant = &rt2x00dev->link.ant; |
Ivo van Doorn | bdfa500 | 2009-08-08 23:53:04 +0200 | [diff] [blame] | 211 | unsigned int flags = ant->flags; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 212 | |
| 213 | /* |
| 214 | * Determine if software diversity is enabled for |
| 215 | * either the TX or RX antenna (or both). |
| 216 | * Always perform this check since within the link |
| 217 | * tuner interval the configuration might have changed. |
| 218 | */ |
Ivo van Doorn | bdfa500 | 2009-08-08 23:53:04 +0200 | [diff] [blame] | 219 | flags &= ~ANTENNA_RX_DIVERSITY; |
| 220 | flags &= ~ANTENNA_TX_DIVERSITY; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 221 | |
| 222 | if (rt2x00dev->default_ant.rx == ANTENNA_SW_DIVERSITY) |
Ivo van Doorn | bdfa500 | 2009-08-08 23:53:04 +0200 | [diff] [blame] | 223 | flags |= ANTENNA_RX_DIVERSITY; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 224 | if (rt2x00dev->default_ant.tx == ANTENNA_SW_DIVERSITY) |
Ivo van Doorn | bdfa500 | 2009-08-08 23:53:04 +0200 | [diff] [blame] | 225 | flags |= ANTENNA_TX_DIVERSITY; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 226 | |
| 227 | if (!(ant->flags & ANTENNA_RX_DIVERSITY) && |
| 228 | !(ant->flags & ANTENNA_TX_DIVERSITY)) { |
| 229 | ant->flags = 0; |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame^] | 230 | return true; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 231 | } |
| 232 | |
Ivo van Doorn | bdfa500 | 2009-08-08 23:53:04 +0200 | [diff] [blame] | 233 | /* Update flags */ |
| 234 | ant->flags = flags; |
| 235 | |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 236 | /* |
| 237 | * If we have only sampled the data over the last period |
| 238 | * we should now harvest the data. Otherwise just evaluate |
| 239 | * the data. The latter should only be performed once |
| 240 | * every 2 seconds. |
| 241 | */ |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame^] | 242 | if (ant->flags & ANTENNA_MODE_SAMPLE) { |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 243 | rt2x00lib_antenna_diversity_sample(rt2x00dev); |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame^] | 244 | return true; |
| 245 | } else if (rt2x00dev->link.count & 1) { |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 246 | rt2x00lib_antenna_diversity_eval(rt2x00dev); |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame^] | 247 | return true; |
| 248 | } |
| 249 | |
| 250 | return false; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void rt2x00link_update_stats(struct rt2x00_dev *rt2x00dev, |
| 254 | struct sk_buff *skb, |
| 255 | struct rxdone_entry_desc *rxdesc) |
| 256 | { |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 257 | struct link *link = &rt2x00dev->link; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 258 | struct link_qual *qual = &rt2x00dev->link.qual; |
| 259 | struct link_ant *ant = &rt2x00dev->link.ant; |
| 260 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; |
| 261 | int avg_rssi = rxdesc->rssi; |
| 262 | int ant_rssi = rxdesc->rssi; |
| 263 | |
| 264 | /* |
| 265 | * Frame was received successfully since non-succesfull |
| 266 | * frames would have been dropped by the hardware. |
| 267 | */ |
| 268 | qual->rx_success++; |
| 269 | |
| 270 | /* |
| 271 | * We are only interested in quality statistics from |
| 272 | * beacons which came from the BSS which we are |
| 273 | * associated with. |
| 274 | */ |
| 275 | if (!ieee80211_is_beacon(hdr->frame_control) || |
| 276 | !(rxdesc->dev_flags & RXDONE_MY_BSS)) |
| 277 | return; |
| 278 | |
| 279 | /* |
| 280 | * Update global RSSI |
| 281 | */ |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 282 | if (link->avg_rssi) |
| 283 | avg_rssi = MOVING_AVERAGE(link->avg_rssi, rxdesc->rssi, 8); |
| 284 | link->avg_rssi = avg_rssi; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 285 | |
| 286 | /* |
| 287 | * Update antenna RSSI |
| 288 | */ |
| 289 | if (ant->rssi_ant) |
| 290 | ant_rssi = MOVING_AVERAGE(ant->rssi_ant, rxdesc->rssi, 8); |
| 291 | ant->rssi_ant = ant_rssi; |
| 292 | } |
| 293 | |
| 294 | static void rt2x00link_precalculate_signal(struct rt2x00_dev *rt2x00dev) |
| 295 | { |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 296 | struct link *link = &rt2x00dev->link; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 297 | struct link_qual *qual = &rt2x00dev->link.qual; |
| 298 | |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 299 | link->rx_percentage = |
| 300 | PERCENTAGE(qual->rx_success, qual->rx_failed + qual->rx_success); |
| 301 | link->tx_percentage = |
| 302 | PERCENTAGE(qual->tx_success, qual->tx_failed + qual->tx_success); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | int rt2x00link_calculate_signal(struct rt2x00_dev *rt2x00dev, int rssi) |
| 306 | { |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 307 | struct link *link = &rt2x00dev->link; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 308 | int rssi_percentage = 0; |
| 309 | int signal; |
| 310 | |
| 311 | /* |
| 312 | * We need a positive value for the RSSI. |
| 313 | */ |
| 314 | if (rssi < 0) |
| 315 | rssi += rt2x00dev->rssi_offset; |
| 316 | |
| 317 | /* |
| 318 | * Calculate the different percentages, |
| 319 | * which will be used for the signal. |
| 320 | */ |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 321 | rssi_percentage = PERCENTAGE(rssi, rt2x00dev->rssi_offset); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 322 | |
| 323 | /* |
| 324 | * Add the individual percentages and use the WEIGHT |
| 325 | * defines to calculate the current link signal. |
| 326 | */ |
| 327 | signal = ((WEIGHT_RSSI * rssi_percentage) + |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 328 | (WEIGHT_TX * link->tx_percentage) + |
| 329 | (WEIGHT_RX * link->rx_percentage)) / 100; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 330 | |
| 331 | return max_t(int, signal, 100); |
| 332 | } |
| 333 | |
| 334 | void rt2x00link_start_tuner(struct rt2x00_dev *rt2x00dev) |
| 335 | { |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 336 | struct link *link = &rt2x00dev->link; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 337 | |
| 338 | /* |
| 339 | * Link tuning should only be performed when |
| 340 | * an active sta or master interface exists. |
| 341 | * Single monitor mode interfaces should never have |
| 342 | * work with link tuners. |
| 343 | */ |
| 344 | if (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count) |
| 345 | return; |
| 346 | |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 347 | link->rx_percentage = DEFAULT_PERCENTAGE; |
| 348 | link->tx_percentage = DEFAULT_PERCENTAGE; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 349 | |
| 350 | rt2x00link_reset_tuner(rt2x00dev, false); |
| 351 | |
Luis R. Rodriguez | 42935ec | 2009-07-29 20:08:07 -0400 | [diff] [blame] | 352 | ieee80211_queue_delayed_work(rt2x00dev->hw, |
| 353 | &link->work, LINK_TUNE_INTERVAL); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | void rt2x00link_stop_tuner(struct rt2x00_dev *rt2x00dev) |
| 357 | { |
| 358 | cancel_delayed_work_sync(&rt2x00dev->link.work); |
| 359 | } |
| 360 | |
| 361 | void rt2x00link_reset_tuner(struct rt2x00_dev *rt2x00dev, bool antenna) |
| 362 | { |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 363 | struct link_qual *qual = &rt2x00dev->link.qual; |
| 364 | |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 365 | if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags)) |
| 366 | return; |
| 367 | |
| 368 | /* |
| 369 | * Reset link information. |
| 370 | * Both the currently active vgc level as well as |
| 371 | * the link tuner counter should be reset. Resetting |
| 372 | * the counter is important for devices where the |
| 373 | * device should only perform link tuning during the |
| 374 | * first minute after being enabled. |
| 375 | */ |
| 376 | rt2x00dev->link.count = 0; |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 377 | memset(qual, 0, sizeof(*qual)); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 378 | |
| 379 | /* |
| 380 | * Reset the link tuner. |
| 381 | */ |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 382 | rt2x00dev->ops->lib->reset_tuner(rt2x00dev, qual); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 383 | |
| 384 | if (antenna) |
| 385 | rt2x00link_antenna_reset(rt2x00dev); |
| 386 | } |
| 387 | |
Ivo van Doorn | a57e2e8 | 2009-03-28 20:51:41 +0100 | [diff] [blame] | 388 | static void rt2x00link_reset_qual(struct rt2x00_dev *rt2x00dev) |
Ivo van Doorn | 64abd80 | 2009-03-01 17:42:00 +0100 | [diff] [blame] | 389 | { |
| 390 | struct link_qual *qual = &rt2x00dev->link.qual; |
| 391 | |
| 392 | qual->rx_success = 0; |
| 393 | qual->rx_failed = 0; |
| 394 | qual->tx_success = 0; |
| 395 | qual->tx_failed = 0; |
| 396 | } |
| 397 | |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 398 | static void rt2x00link_tuner(struct work_struct *work) |
| 399 | { |
| 400 | struct rt2x00_dev *rt2x00dev = |
| 401 | container_of(work, struct rt2x00_dev, link.work.work); |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 402 | struct link *link = &rt2x00dev->link; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 403 | struct link_qual *qual = &rt2x00dev->link.qual; |
| 404 | |
| 405 | /* |
| 406 | * When the radio is shutting down we should |
| 407 | * immediately cease all link tuning. |
| 408 | */ |
| 409 | if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags)) |
| 410 | return; |
| 411 | |
| 412 | /* |
| 413 | * Update statistics. |
| 414 | */ |
| 415 | rt2x00dev->ops->lib->link_stats(rt2x00dev, qual); |
| 416 | rt2x00dev->low_level_stats.dot11FCSErrorCount += qual->rx_failed; |
| 417 | |
| 418 | /* |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 419 | * Update quality RSSI for link tuning, |
| 420 | * when we have received some frames and we managed to |
| 421 | * collect the RSSI data we could use this. Otherwise we |
| 422 | * must fallback to the default RSSI value. |
| 423 | */ |
| 424 | if (!link->avg_rssi || !qual->rx_success) |
| 425 | qual->rssi = DEFAULT_RSSI; |
| 426 | else |
| 427 | qual->rssi = link->avg_rssi; |
| 428 | |
| 429 | /* |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 430 | * Only perform the link tuning when Link tuning |
| 431 | * has been enabled (This could have been disabled from the EEPROM). |
| 432 | */ |
| 433 | if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags)) |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 434 | rt2x00dev->ops->lib->link_tuner(rt2x00dev, qual, link->count); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 435 | |
| 436 | /* |
| 437 | * Precalculate a portion of the link signal which is |
| 438 | * in based on the tx/rx success/failure counters. |
| 439 | */ |
| 440 | rt2x00link_precalculate_signal(rt2x00dev); |
| 441 | |
| 442 | /* |
| 443 | * Send a signal to the led to update the led signal strength. |
| 444 | */ |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 445 | rt2x00leds_led_quality(rt2x00dev, link->avg_rssi); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 446 | |
| 447 | /* |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame^] | 448 | * Evaluate antenna setup, make this the last step when |
| 449 | * rt2x00lib_antenna_diversity made changes the quality |
| 450 | * statistics will be reset. |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 451 | */ |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame^] | 452 | if (rt2x00lib_antenna_diversity(rt2x00dev)) |
| 453 | rt2x00link_reset_qual(rt2x00dev); |
Ivo van Doorn | 64abd80 | 2009-03-01 17:42:00 +0100 | [diff] [blame] | 454 | |
| 455 | /* |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 456 | * Increase tuner counter, and reschedule the next link tuner run. |
| 457 | */ |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 458 | link->count++; |
Luis R. Rodriguez | 42935ec | 2009-07-29 20:08:07 -0400 | [diff] [blame] | 459 | ieee80211_queue_delayed_work(rt2x00dev->hw, |
| 460 | &link->work, LINK_TUNE_INTERVAL); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | void rt2x00link_register(struct rt2x00_dev *rt2x00dev) |
| 464 | { |
| 465 | INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00link_tuner); |
| 466 | } |