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 | /* |
Lars Ericsson | 66679a6 | 2009-08-08 23:54:51 +0200 | [diff] [blame] | 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. |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 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 |
Lars Ericsson | 66679a6 | 2009-08-08 23:54:51 +0200 | [diff] [blame] | 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. |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 78 | */ |
Lars Ericsson | 66679a6 | 2009-08-08 23:54:51 +0200 | [diff] [blame] | 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 | }) |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 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 | |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 115 | static int rt2x00link_antenna_get_link_rssi(struct rt2x00_dev *rt2x00dev) |
| 116 | { |
| 117 | struct link_ant *ant = &rt2x00dev->link.ant; |
| 118 | |
Lars Ericsson | 66679a6 | 2009-08-08 23:54:51 +0200 | [diff] [blame] | 119 | if (ant->rssi_ant.avg && rt2x00dev->link.qual.rx_success) |
| 120 | return ant->rssi_ant.avg; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 121 | return DEFAULT_RSSI; |
| 122 | } |
| 123 | |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 124 | static int rt2x00link_antenna_get_rssi_history(struct rt2x00_dev *rt2x00dev) |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 125 | { |
| 126 | struct link_ant *ant = &rt2x00dev->link.ant; |
| 127 | |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 128 | if (ant->rssi_history) |
| 129 | return ant->rssi_history; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 130 | return DEFAULT_RSSI; |
| 131 | } |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 132 | |
| 133 | static void rt2x00link_antenna_update_rssi_history(struct rt2x00_dev *rt2x00dev, |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 134 | int rssi) |
| 135 | { |
| 136 | struct link_ant *ant = &rt2x00dev->link.ant; |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 137 | ant->rssi_history = rssi; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 138 | } |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 139 | |
| 140 | static void rt2x00link_antenna_reset(struct rt2x00_dev *rt2x00dev) |
| 141 | { |
Lars Ericsson | 66679a6 | 2009-08-08 23:54:51 +0200 | [diff] [blame] | 142 | rt2x00dev->link.ant.rssi_ant.avg = 0; |
| 143 | rt2x00dev->link.ant.rssi_ant.avg_weight = 0; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static void rt2x00lib_antenna_diversity_sample(struct rt2x00_dev *rt2x00dev) |
| 147 | { |
| 148 | struct link_ant *ant = &rt2x00dev->link.ant; |
| 149 | struct antenna_setup new_ant; |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 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); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 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 |
Luis Correia | 4951348 | 2009-07-17 21:39:19 +0200 | [diff] [blame] | 164 | * from both antennas. It now is time to determine |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 165 | * which antenna demonstrated the best performance. |
| 166 | * When we are already on the antenna with the best |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 167 | * performance, just create a good starting point |
| 168 | * for the history and we are done. |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 169 | */ |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 170 | if (sample_current >= sample_other) { |
| 171 | rt2x00link_antenna_update_rssi_history(rt2x00dev, |
| 172 | sample_current); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 173 | return; |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | other_antenna = (ant->active.rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 177 | |
| 178 | if (ant->flags & ANTENNA_RX_DIVERSITY) |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 179 | new_ant.rx = other_antenna; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 180 | |
| 181 | if (ant->flags & ANTENNA_TX_DIVERSITY) |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 182 | new_ant.tx = other_antenna; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 183 | |
Lars Ericsson | eb87eaa | 2009-07-18 20:21:52 +0200 | [diff] [blame] | 184 | rt2x00lib_config_antenna(rt2x00dev, new_ant); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static 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); |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 201 | rssi_old = rt2x00link_antenna_get_rssi_history(rt2x00dev); |
| 202 | rt2x00link_antenna_update_rssi_history(rt2x00dev, rssi_curr); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 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 | |
Lars Ericsson | eb87eaa | 2009-07-18 20:21:52 +0200 | [diff] [blame] | 224 | rt2x00lib_config_antenna(rt2x00dev, new_ant); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 225 | } |
| 226 | |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 227 | static bool rt2x00lib_antenna_diversity(struct rt2x00_dev *rt2x00dev) |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 228 | { |
| 229 | struct link_ant *ant = &rt2x00dev->link.ant; |
Ivo van Doorn | bdfa500 | 2009-08-08 23:53:04 +0200 | [diff] [blame] | 230 | unsigned int flags = ant->flags; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 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 | */ |
Ivo van Doorn | bdfa500 | 2009-08-08 23:53:04 +0200 | [diff] [blame] | 238 | flags &= ~ANTENNA_RX_DIVERSITY; |
| 239 | flags &= ~ANTENNA_TX_DIVERSITY; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 240 | |
| 241 | if (rt2x00dev->default_ant.rx == ANTENNA_SW_DIVERSITY) |
Ivo van Doorn | bdfa500 | 2009-08-08 23:53:04 +0200 | [diff] [blame] | 242 | flags |= ANTENNA_RX_DIVERSITY; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 243 | if (rt2x00dev->default_ant.tx == ANTENNA_SW_DIVERSITY) |
Ivo van Doorn | bdfa500 | 2009-08-08 23:53:04 +0200 | [diff] [blame] | 244 | flags |= ANTENNA_TX_DIVERSITY; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 245 | |
| 246 | if (!(ant->flags & ANTENNA_RX_DIVERSITY) && |
| 247 | !(ant->flags & ANTENNA_TX_DIVERSITY)) { |
| 248 | ant->flags = 0; |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 249 | return true; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 250 | } |
| 251 | |
Ivo van Doorn | bdfa500 | 2009-08-08 23:53:04 +0200 | [diff] [blame] | 252 | /* Update flags */ |
| 253 | ant->flags = flags; |
| 254 | |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 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 | */ |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 261 | if (ant->flags & ANTENNA_MODE_SAMPLE) { |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 262 | rt2x00lib_antenna_diversity_sample(rt2x00dev); |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 263 | return true; |
| 264 | } else if (rt2x00dev->link.count & 1) { |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 265 | rt2x00lib_antenna_diversity_eval(rt2x00dev); |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 266 | return true; |
| 267 | } |
| 268 | |
| 269 | return false; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | void rt2x00link_update_stats(struct rt2x00_dev *rt2x00dev, |
| 273 | struct sk_buff *skb, |
| 274 | struct rxdone_entry_desc *rxdesc) |
| 275 | { |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 276 | struct link *link = &rt2x00dev->link; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 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; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 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 | */ |
Lars Ericsson | 66679a6 | 2009-08-08 23:54:51 +0200 | [diff] [blame] | 299 | link->avg_rssi = MOVING_AVERAGE(link->avg_rssi, rxdesc->rssi); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 300 | |
| 301 | /* |
| 302 | * Update antenna RSSI |
| 303 | */ |
Lars Ericsson | 66679a6 | 2009-08-08 23:54:51 +0200 | [diff] [blame] | 304 | ant->rssi_ant = MOVING_AVERAGE(ant->rssi_ant, rxdesc->rssi); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | static void rt2x00link_precalculate_signal(struct rt2x00_dev *rt2x00dev) |
| 308 | { |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 309 | struct link *link = &rt2x00dev->link; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 310 | struct link_qual *qual = &rt2x00dev->link.qual; |
| 311 | |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 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); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | int rt2x00link_calculate_signal(struct rt2x00_dev *rt2x00dev, int rssi) |
| 319 | { |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 320 | struct link *link = &rt2x00dev->link; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 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 | */ |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 334 | rssi_percentage = PERCENTAGE(rssi, rt2x00dev->rssi_offset); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 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) + |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 341 | (WEIGHT_TX * link->tx_percentage) + |
| 342 | (WEIGHT_RX * link->rx_percentage)) / 100; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 343 | |
| 344 | return max_t(int, signal, 100); |
| 345 | } |
| 346 | |
| 347 | void rt2x00link_start_tuner(struct rt2x00_dev *rt2x00dev) |
| 348 | { |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 349 | struct link *link = &rt2x00dev->link; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 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 | |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 360 | link->rx_percentage = DEFAULT_PERCENTAGE; |
| 361 | link->tx_percentage = DEFAULT_PERCENTAGE; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 362 | |
| 363 | rt2x00link_reset_tuner(rt2x00dev, false); |
| 364 | |
Sean Cross | 66f84d6 | 2009-11-05 20:22:03 +0100 | [diff] [blame] | 365 | if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags)) |
| 366 | ieee80211_queue_delayed_work(rt2x00dev->hw, |
| 367 | &link->work, LINK_TUNE_INTERVAL); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | void rt2x00link_stop_tuner(struct rt2x00_dev *rt2x00dev) |
| 371 | { |
| 372 | cancel_delayed_work_sync(&rt2x00dev->link.work); |
| 373 | } |
| 374 | |
| 375 | void rt2x00link_reset_tuner(struct rt2x00_dev *rt2x00dev, bool antenna) |
| 376 | { |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 377 | struct link_qual *qual = &rt2x00dev->link.qual; |
| 378 | |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 379 | if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags)) |
| 380 | return; |
| 381 | |
| 382 | /* |
| 383 | * Reset link information. |
| 384 | * Both the currently active vgc level as well as |
| 385 | * the link tuner counter should be reset. Resetting |
| 386 | * the counter is important for devices where the |
| 387 | * device should only perform link tuning during the |
| 388 | * first minute after being enabled. |
| 389 | */ |
| 390 | rt2x00dev->link.count = 0; |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 391 | memset(qual, 0, sizeof(*qual)); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 392 | |
| 393 | /* |
| 394 | * Reset the link tuner. |
| 395 | */ |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 396 | rt2x00dev->ops->lib->reset_tuner(rt2x00dev, qual); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 397 | |
| 398 | if (antenna) |
| 399 | rt2x00link_antenna_reset(rt2x00dev); |
| 400 | } |
| 401 | |
Ivo van Doorn | a57e2e8 | 2009-03-28 20:51:41 +0100 | [diff] [blame] | 402 | static void rt2x00link_reset_qual(struct rt2x00_dev *rt2x00dev) |
Ivo van Doorn | 64abd80 | 2009-03-01 17:42:00 +0100 | [diff] [blame] | 403 | { |
| 404 | struct link_qual *qual = &rt2x00dev->link.qual; |
| 405 | |
| 406 | qual->rx_success = 0; |
| 407 | qual->rx_failed = 0; |
| 408 | qual->tx_success = 0; |
| 409 | qual->tx_failed = 0; |
| 410 | } |
| 411 | |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 412 | static void rt2x00link_tuner(struct work_struct *work) |
| 413 | { |
| 414 | struct rt2x00_dev *rt2x00dev = |
| 415 | container_of(work, struct rt2x00_dev, link.work.work); |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 416 | struct link *link = &rt2x00dev->link; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 417 | struct link_qual *qual = &rt2x00dev->link.qual; |
| 418 | |
| 419 | /* |
| 420 | * When the radio is shutting down we should |
| 421 | * immediately cease all link tuning. |
| 422 | */ |
| 423 | if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags)) |
| 424 | return; |
| 425 | |
| 426 | /* |
| 427 | * Update statistics. |
| 428 | */ |
| 429 | rt2x00dev->ops->lib->link_stats(rt2x00dev, qual); |
| 430 | rt2x00dev->low_level_stats.dot11FCSErrorCount += qual->rx_failed; |
| 431 | |
| 432 | /* |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 433 | * Update quality RSSI for link tuning, |
| 434 | * when we have received some frames and we managed to |
| 435 | * collect the RSSI data we could use this. Otherwise we |
| 436 | * must fallback to the default RSSI value. |
| 437 | */ |
Lars Ericsson | 66679a6 | 2009-08-08 23:54:51 +0200 | [diff] [blame] | 438 | if (!link->avg_rssi.avg || !qual->rx_success) |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 439 | qual->rssi = DEFAULT_RSSI; |
| 440 | else |
Lars Ericsson | 66679a6 | 2009-08-08 23:54:51 +0200 | [diff] [blame] | 441 | qual->rssi = link->avg_rssi.avg; |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 442 | |
| 443 | /* |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 444 | * Only perform the link tuning when Link tuning |
| 445 | * has been enabled (This could have been disabled from the EEPROM). |
| 446 | */ |
| 447 | if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags)) |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 448 | rt2x00dev->ops->lib->link_tuner(rt2x00dev, qual, link->count); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 449 | |
| 450 | /* |
| 451 | * Precalculate a portion of the link signal which is |
| 452 | * in based on the tx/rx success/failure counters. |
| 453 | */ |
| 454 | rt2x00link_precalculate_signal(rt2x00dev); |
| 455 | |
| 456 | /* |
| 457 | * Send a signal to the led to update the led signal strength. |
| 458 | */ |
Lars Ericsson | 66679a6 | 2009-08-08 23:54:51 +0200 | [diff] [blame] | 459 | rt2x00leds_led_quality(rt2x00dev, qual->rssi); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 460 | |
| 461 | /* |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 462 | * Evaluate antenna setup, make this the last step when |
| 463 | * rt2x00lib_antenna_diversity made changes the quality |
| 464 | * statistics will be reset. |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 465 | */ |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 466 | if (rt2x00lib_antenna_diversity(rt2x00dev)) |
| 467 | rt2x00link_reset_qual(rt2x00dev); |
Ivo van Doorn | 64abd80 | 2009-03-01 17:42:00 +0100 | [diff] [blame] | 468 | |
| 469 | /* |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 470 | * Increase tuner counter, and reschedule the next link tuner run. |
| 471 | */ |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 472 | link->count++; |
Sean Cross | 66f84d6 | 2009-11-05 20:22:03 +0100 | [diff] [blame] | 473 | |
| 474 | if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags)) |
| 475 | ieee80211_queue_delayed_work(rt2x00dev->hw, |
| 476 | &link->work, LINK_TUNE_INTERVAL); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | void rt2x00link_register(struct rt2x00_dev *rt2x00dev) |
| 480 | { |
| 481 | INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00link_tuner); |
| 482 | } |