Thomas Gleixner | 1ccea77 | 2019-05-19 15:51:43 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 2 | /* |
Gertjan van Wingerde | 9c9a0d1 | 2009-11-08 16:39:55 +0100 | [diff] [blame] | 3 | Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com> |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 4 | <http://rt2x00.serialmonkey.com> |
| 5 | |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
| 9 | Module: rt2x00lib |
| 10 | Abstract: rt2x00 generic link tuning routines. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
| 15 | |
| 16 | #include "rt2x00.h" |
| 17 | #include "rt2x00lib.h" |
| 18 | |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 19 | /* |
| 20 | * When we lack RSSI information return something less then -80 to |
| 21 | * tell the driver to tune the device to maximum sensitivity. |
| 22 | */ |
| 23 | #define DEFAULT_RSSI -128 |
| 24 | |
Johannes Berg | 11ab35e | 2015-08-19 09:46:21 +0200 | [diff] [blame] | 25 | static inline int rt2x00link_get_avg_rssi(struct ewma_rssi *ewma) |
Gabor Juhos | 1dc254a | 2013-10-09 11:00:38 +0200 | [diff] [blame] | 26 | { |
| 27 | unsigned long avg; |
| 28 | |
Johannes Berg | 11ab35e | 2015-08-19 09:46:21 +0200 | [diff] [blame] | 29 | avg = ewma_rssi_read(ewma); |
Gabor Juhos | 1dc254a | 2013-10-09 11:00:38 +0200 | [diff] [blame] | 30 | if (avg) |
| 31 | return -avg; |
| 32 | |
| 33 | return DEFAULT_RSSI; |
| 34 | } |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 35 | |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 36 | static int rt2x00link_antenna_get_link_rssi(struct rt2x00_dev *rt2x00dev) |
| 37 | { |
| 38 | struct link_ant *ant = &rt2x00dev->link.ant; |
| 39 | |
Gabor Juhos | 1dc254a | 2013-10-09 11:00:38 +0200 | [diff] [blame] | 40 | if (rt2x00dev->link.qual.rx_success) |
| 41 | return rt2x00link_get_avg_rssi(&ant->rssi_ant); |
| 42 | |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 43 | return DEFAULT_RSSI; |
| 44 | } |
| 45 | |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 46 | static int rt2x00link_antenna_get_rssi_history(struct rt2x00_dev *rt2x00dev) |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 47 | { |
| 48 | struct link_ant *ant = &rt2x00dev->link.ant; |
| 49 | |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 50 | if (ant->rssi_history) |
| 51 | return ant->rssi_history; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 52 | return DEFAULT_RSSI; |
| 53 | } |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 54 | |
| 55 | static void rt2x00link_antenna_update_rssi_history(struct rt2x00_dev *rt2x00dev, |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 56 | int rssi) |
| 57 | { |
| 58 | struct link_ant *ant = &rt2x00dev->link.ant; |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 59 | ant->rssi_history = rssi; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 60 | } |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 61 | |
| 62 | static void rt2x00link_antenna_reset(struct rt2x00_dev *rt2x00dev) |
| 63 | { |
Johannes Berg | 11ab35e | 2015-08-19 09:46:21 +0200 | [diff] [blame] | 64 | ewma_rssi_init(&rt2x00dev->link.ant.rssi_ant); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static void rt2x00lib_antenna_diversity_sample(struct rt2x00_dev *rt2x00dev) |
| 68 | { |
| 69 | struct link_ant *ant = &rt2x00dev->link.ant; |
| 70 | struct antenna_setup new_ant; |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 71 | int other_antenna; |
| 72 | |
| 73 | int sample_current = rt2x00link_antenna_get_link_rssi(rt2x00dev); |
| 74 | int sample_other = rt2x00link_antenna_get_rssi_history(rt2x00dev); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 75 | |
| 76 | memcpy(&new_ant, &ant->active, sizeof(new_ant)); |
| 77 | |
| 78 | /* |
| 79 | * We are done sampling. Now we should evaluate the results. |
| 80 | */ |
| 81 | ant->flags &= ~ANTENNA_MODE_SAMPLE; |
| 82 | |
| 83 | /* |
| 84 | * During the last period we have sampled the RSSI |
Luis Correia | 4951348 | 2009-07-17 21:39:19 +0200 | [diff] [blame] | 85 | * from both antennas. It now is time to determine |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 86 | * which antenna demonstrated the best performance. |
| 87 | * When we are already on the antenna with the best |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 88 | * performance, just create a good starting point |
| 89 | * for the history and we are done. |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 90 | */ |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 91 | if (sample_current >= sample_other) { |
| 92 | rt2x00link_antenna_update_rssi_history(rt2x00dev, |
| 93 | sample_current); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 94 | return; |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | other_antenna = (ant->active.rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 98 | |
| 99 | if (ant->flags & ANTENNA_RX_DIVERSITY) |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 100 | new_ant.rx = other_antenna; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 101 | |
| 102 | if (ant->flags & ANTENNA_TX_DIVERSITY) |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 103 | new_ant.tx = other_antenna; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 104 | |
Lars Ericsson | eb87eaa | 2009-07-18 20:21:52 +0200 | [diff] [blame] | 105 | rt2x00lib_config_antenna(rt2x00dev, new_ant); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static void rt2x00lib_antenna_diversity_eval(struct rt2x00_dev *rt2x00dev) |
| 109 | { |
| 110 | struct link_ant *ant = &rt2x00dev->link.ant; |
| 111 | struct antenna_setup new_ant; |
| 112 | int rssi_curr; |
| 113 | int rssi_old; |
| 114 | |
| 115 | memcpy(&new_ant, &ant->active, sizeof(new_ant)); |
| 116 | |
| 117 | /* |
| 118 | * Get current RSSI value along with the historical value, |
| 119 | * after that update the history with the current value. |
| 120 | */ |
| 121 | rssi_curr = rt2x00link_antenna_get_link_rssi(rt2x00dev); |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 122 | rssi_old = rt2x00link_antenna_get_rssi_history(rt2x00dev); |
| 123 | rt2x00link_antenna_update_rssi_history(rt2x00dev, rssi_curr); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 124 | |
| 125 | /* |
| 126 | * Legacy driver indicates that we should swap antenna's |
| 127 | * when the difference in RSSI is greater that 5. This |
| 128 | * also should be done when the RSSI was actually better |
| 129 | * then the previous sample. |
| 130 | * When the difference exceeds the threshold we should |
| 131 | * sample the rssi from the other antenna to make a valid |
| 132 | * comparison between the 2 antennas. |
| 133 | */ |
| 134 | if (abs(rssi_curr - rssi_old) < 5) |
| 135 | return; |
| 136 | |
| 137 | ant->flags |= ANTENNA_MODE_SAMPLE; |
| 138 | |
| 139 | if (ant->flags & ANTENNA_RX_DIVERSITY) |
| 140 | new_ant.rx = (new_ant.rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A; |
| 141 | |
| 142 | if (ant->flags & ANTENNA_TX_DIVERSITY) |
| 143 | new_ant.tx = (new_ant.tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A; |
| 144 | |
Lars Ericsson | eb87eaa | 2009-07-18 20:21:52 +0200 | [diff] [blame] | 145 | rt2x00lib_config_antenna(rt2x00dev, new_ant); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 146 | } |
| 147 | |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 148 | static bool rt2x00lib_antenna_diversity(struct rt2x00_dev *rt2x00dev) |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 149 | { |
| 150 | struct link_ant *ant = &rt2x00dev->link.ant; |
| 151 | |
| 152 | /* |
| 153 | * Determine if software diversity is enabled for |
| 154 | * either the TX or RX antenna (or both). |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 155 | */ |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 156 | if (!(ant->flags & ANTENNA_RX_DIVERSITY) && |
| 157 | !(ant->flags & ANTENNA_TX_DIVERSITY)) { |
| 158 | ant->flags = 0; |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 159 | return true; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /* |
| 163 | * If we have only sampled the data over the last period |
| 164 | * we should now harvest the data. Otherwise just evaluate |
| 165 | * the data. The latter should only be performed once |
| 166 | * every 2 seconds. |
| 167 | */ |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 168 | if (ant->flags & ANTENNA_MODE_SAMPLE) { |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 169 | rt2x00lib_antenna_diversity_sample(rt2x00dev); |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 170 | return true; |
| 171 | } else if (rt2x00dev->link.count & 1) { |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 172 | rt2x00lib_antenna_diversity_eval(rt2x00dev); |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 173 | return true; |
| 174 | } |
| 175 | |
| 176 | return false; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void rt2x00link_update_stats(struct rt2x00_dev *rt2x00dev, |
| 180 | struct sk_buff *skb, |
| 181 | struct rxdone_entry_desc *rxdesc) |
| 182 | { |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 183 | struct link *link = &rt2x00dev->link; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 184 | struct link_qual *qual = &rt2x00dev->link.qual; |
| 185 | struct link_ant *ant = &rt2x00dev->link.ant; |
| 186 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 187 | |
| 188 | /* |
Helmut Schaa | a23c220 | 2010-10-09 13:33:16 +0200 | [diff] [blame] | 189 | * No need to update the stats for !=STA interfaces |
| 190 | */ |
| 191 | if (!rt2x00dev->intf_sta_count) |
| 192 | return; |
| 193 | |
| 194 | /* |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 195 | * Frame was received successfully since non-succesfull |
| 196 | * frames would have been dropped by the hardware. |
| 197 | */ |
| 198 | qual->rx_success++; |
| 199 | |
| 200 | /* |
| 201 | * We are only interested in quality statistics from |
| 202 | * beacons which came from the BSS which we are |
| 203 | * associated with. |
| 204 | */ |
| 205 | if (!ieee80211_is_beacon(hdr->frame_control) || |
| 206 | !(rxdesc->dev_flags & RXDONE_MY_BSS)) |
| 207 | return; |
| 208 | |
| 209 | /* |
| 210 | * Update global RSSI |
| 211 | */ |
Johannes Berg | 11ab35e | 2015-08-19 09:46:21 +0200 | [diff] [blame] | 212 | ewma_rssi_add(&link->avg_rssi, -rxdesc->rssi); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 213 | |
| 214 | /* |
| 215 | * Update antenna RSSI |
| 216 | */ |
Johannes Berg | 11ab35e | 2015-08-19 09:46:21 +0200 | [diff] [blame] | 217 | ewma_rssi_add(&ant->rssi_ant, -rxdesc->rssi); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 218 | } |
| 219 | |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 220 | void rt2x00link_start_tuner(struct rt2x00_dev *rt2x00dev) |
| 221 | { |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 222 | struct link *link = &rt2x00dev->link; |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 223 | |
| 224 | /* |
Stanislaw Gruszka | d963247 | 2016-12-19 11:52:54 +0100 | [diff] [blame] | 225 | * Single monitor mode interfaces should never have |
| 226 | * work with link tuners. |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 227 | */ |
Stanislaw Gruszka | d963247 | 2016-12-19 11:52:54 +0100 | [diff] [blame] | 228 | if (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count) |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 229 | return; |
| 230 | |
Stanislaw Gruszka | d963247 | 2016-12-19 11:52:54 +0100 | [diff] [blame] | 231 | /* |
Ivo van Doorn | d8147f9 | 2010-07-11 12:24:47 +0200 | [diff] [blame] | 232 | * While scanning, link tuning is disabled. By default |
| 233 | * the most sensitive settings will be used to make sure |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 234 | * that all beacons and probe responses will be received |
Ivo van Doorn | d8147f9 | 2010-07-11 12:24:47 +0200 | [diff] [blame] | 235 | * during the scan. |
| 236 | */ |
| 237 | if (test_bit(DEVICE_STATE_SCANNING, &rt2x00dev->flags)) |
| 238 | return; |
| 239 | |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 240 | rt2x00link_reset_tuner(rt2x00dev, false); |
| 241 | |
Sean Cross | 66f84d6 | 2009-11-05 20:22:03 +0100 | [diff] [blame] | 242 | if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags)) |
| 243 | ieee80211_queue_delayed_work(rt2x00dev->hw, |
| 244 | &link->work, LINK_TUNE_INTERVAL); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void rt2x00link_stop_tuner(struct rt2x00_dev *rt2x00dev) |
| 248 | { |
| 249 | cancel_delayed_work_sync(&rt2x00dev->link.work); |
| 250 | } |
| 251 | |
| 252 | void rt2x00link_reset_tuner(struct rt2x00_dev *rt2x00dev, bool antenna) |
| 253 | { |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 254 | struct link_qual *qual = &rt2x00dev->link.qual; |
Ivo van Doorn | 223dcc2 | 2010-07-11 12:25:17 +0200 | [diff] [blame] | 255 | u8 vgc_level = qual->vgc_level_reg; |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 256 | |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 257 | if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags)) |
| 258 | return; |
| 259 | |
| 260 | /* |
| 261 | * Reset link information. |
| 262 | * Both the currently active vgc level as well as |
| 263 | * the link tuner counter should be reset. Resetting |
| 264 | * the counter is important for devices where the |
| 265 | * device should only perform link tuning during the |
| 266 | * first minute after being enabled. |
| 267 | */ |
| 268 | rt2x00dev->link.count = 0; |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 269 | memset(qual, 0, sizeof(*qual)); |
Johannes Berg | 11ab35e | 2015-08-19 09:46:21 +0200 | [diff] [blame] | 270 | ewma_rssi_init(&rt2x00dev->link.avg_rssi); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 271 | |
| 272 | /* |
Ivo van Doorn | 223dcc2 | 2010-07-11 12:25:17 +0200 | [diff] [blame] | 273 | * Restore the VGC level as stored in the registers, |
| 274 | * the driver can use this to determine if the register |
| 275 | * must be updated during reset or not. |
| 276 | */ |
| 277 | qual->vgc_level_reg = vgc_level; |
| 278 | |
| 279 | /* |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 280 | * Reset the link tuner. |
| 281 | */ |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 282 | rt2x00dev->ops->lib->reset_tuner(rt2x00dev, qual); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 283 | |
| 284 | if (antenna) |
| 285 | rt2x00link_antenna_reset(rt2x00dev); |
| 286 | } |
| 287 | |
Ivo van Doorn | a57e2e8 | 2009-03-28 20:51:41 +0100 | [diff] [blame] | 288 | static void rt2x00link_reset_qual(struct rt2x00_dev *rt2x00dev) |
Ivo van Doorn | 64abd80 | 2009-03-01 17:42:00 +0100 | [diff] [blame] | 289 | { |
| 290 | struct link_qual *qual = &rt2x00dev->link.qual; |
| 291 | |
| 292 | qual->rx_success = 0; |
| 293 | qual->rx_failed = 0; |
| 294 | qual->tx_success = 0; |
| 295 | qual->tx_failed = 0; |
| 296 | } |
| 297 | |
Stanislaw Gruszka | d963247 | 2016-12-19 11:52:54 +0100 | [diff] [blame] | 298 | static void rt2x00link_tuner_sta(struct rt2x00_dev *rt2x00dev, struct link *link) |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 299 | { |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 300 | struct link_qual *qual = &rt2x00dev->link.qual; |
| 301 | |
| 302 | /* |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 303 | * Update statistics. |
| 304 | */ |
| 305 | rt2x00dev->ops->lib->link_stats(rt2x00dev, qual); |
| 306 | rt2x00dev->low_level_stats.dot11FCSErrorCount += qual->rx_failed; |
| 307 | |
| 308 | /* |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 309 | * Update quality RSSI for link tuning, |
| 310 | * when we have received some frames and we managed to |
| 311 | * collect the RSSI data we could use this. Otherwise we |
| 312 | * must fallback to the default RSSI value. |
| 313 | */ |
Gabor Juhos | 1dc254a | 2013-10-09 11:00:38 +0200 | [diff] [blame] | 314 | if (!qual->rx_success) |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 315 | qual->rssi = DEFAULT_RSSI; |
| 316 | else |
Gabor Juhos | 1dc254a | 2013-10-09 11:00:38 +0200 | [diff] [blame] | 317 | qual->rssi = rt2x00link_get_avg_rssi(&link->avg_rssi); |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 318 | |
| 319 | /* |
Ivo van Doorn | 27df2a9 | 2010-07-11 12:24:22 +0200 | [diff] [blame] | 320 | * Check if link tuning is supported by the hardware, some hardware |
| 321 | * do not support link tuning at all, while other devices can disable |
| 322 | * the feature from the EEPROM. |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 323 | */ |
Gabor Juhos | 7b8a00d | 2013-10-11 13:18:41 +0200 | [diff] [blame] | 324 | if (rt2x00_has_cap_link_tuning(rt2x00dev)) |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 325 | rt2x00dev->ops->lib->link_tuner(rt2x00dev, qual, link->count); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 326 | |
| 327 | /* |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 328 | * Send a signal to the led to update the led signal strength. |
| 329 | */ |
Lars Ericsson | 66679a6 | 2009-08-08 23:54:51 +0200 | [diff] [blame] | 330 | rt2x00leds_led_quality(rt2x00dev, qual->rssi); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 331 | |
| 332 | /* |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 333 | * Evaluate antenna setup, make this the last step when |
| 334 | * rt2x00lib_antenna_diversity made changes the quality |
| 335 | * statistics will be reset. |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 336 | */ |
Lars Ericsson | 193df18 | 2009-08-08 23:54:24 +0200 | [diff] [blame] | 337 | if (rt2x00lib_antenna_diversity(rt2x00dev)) |
| 338 | rt2x00link_reset_qual(rt2x00dev); |
Stanislaw Gruszka | d963247 | 2016-12-19 11:52:54 +0100 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | static void rt2x00link_tuner(struct work_struct *work) |
| 342 | { |
| 343 | struct rt2x00_dev *rt2x00dev = |
| 344 | container_of(work, struct rt2x00_dev, link.work.work); |
| 345 | struct link *link = &rt2x00dev->link; |
| 346 | |
| 347 | /* |
| 348 | * When the radio is shutting down we should |
| 349 | * immediately cease all link tuning. |
| 350 | */ |
| 351 | if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags) || |
| 352 | test_bit(DEVICE_STATE_SCANNING, &rt2x00dev->flags)) |
| 353 | return; |
| 354 | |
Stanislaw Gruszka | c7d1c77 | 2016-12-19 11:52:57 +0100 | [diff] [blame] | 355 | /* Do not race with rt2x00mac_config(). */ |
| 356 | mutex_lock(&rt2x00dev->conf_mutex); |
| 357 | |
Stanislaw Gruszka | d963247 | 2016-12-19 11:52:54 +0100 | [diff] [blame] | 358 | if (rt2x00dev->intf_sta_count) |
| 359 | rt2x00link_tuner_sta(rt2x00dev, link); |
| 360 | |
| 361 | if (rt2x00dev->ops->lib->gain_calibration && |
| 362 | (link->count % (AGC_SECONDS / LINK_TUNE_SECONDS)) == 0) |
| 363 | rt2x00dev->ops->lib->gain_calibration(rt2x00dev); |
| 364 | |
| 365 | if (rt2x00dev->ops->lib->vco_calibration && |
| 366 | rt2x00_has_cap_vco_recalibration(rt2x00dev) && |
| 367 | (link->count % (VCO_SECONDS / LINK_TUNE_SECONDS)) == 0) |
| 368 | rt2x00dev->ops->lib->vco_calibration(rt2x00dev); |
Ivo van Doorn | 64abd80 | 2009-03-01 17:42:00 +0100 | [diff] [blame] | 369 | |
Stanislaw Gruszka | c7d1c77 | 2016-12-19 11:52:57 +0100 | [diff] [blame] | 370 | mutex_unlock(&rt2x00dev->conf_mutex); |
| 371 | |
Ivo van Doorn | 64abd80 | 2009-03-01 17:42:00 +0100 | [diff] [blame] | 372 | /* |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 373 | * Increase tuner counter, and reschedule the next link tuner run. |
| 374 | */ |
Ivo van Doorn | 5352ff6 | 2008-12-20 10:54:54 +0100 | [diff] [blame] | 375 | link->count++; |
Sean Cross | 66f84d6 | 2009-11-05 20:22:03 +0100 | [diff] [blame] | 376 | |
| 377 | if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags)) |
| 378 | ieee80211_queue_delayed_work(rt2x00dev->hw, |
| 379 | &link->work, LINK_TUNE_INTERVAL); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 380 | } |
| 381 | |
Ivo van Doorn | c965c74 | 2010-07-11 12:25:46 +0200 | [diff] [blame] | 382 | void rt2x00link_start_watchdog(struct rt2x00_dev *rt2x00dev) |
| 383 | { |
| 384 | struct link *link = &rt2x00dev->link; |
| 385 | |
Helmut Schaa | aca7305b | 2011-03-28 13:30:59 +0200 | [diff] [blame] | 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); |
Ivo van Doorn | c965c74 | 2010-07-11 12:25:46 +0200 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | void rt2x00link_stop_watchdog(struct rt2x00_dev *rt2x00dev) |
| 394 | { |
| 395 | cancel_delayed_work_sync(&rt2x00dev->link.watchdog_work); |
| 396 | } |
| 397 | |
| 398 | static 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)) |
Ivo van Doorn | 0439f53 | 2011-01-30 13:24:05 +0100 | [diff] [blame] | 414 | ieee80211_queue_delayed_work(rt2x00dev->hw, |
| 415 | &link->watchdog_work, |
| 416 | WATCHDOG_INTERVAL); |
Ivo van Doorn | c965c74 | 2010-07-11 12:25:46 +0200 | [diff] [blame] | 417 | } |
| 418 | |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 419 | void rt2x00link_register(struct rt2x00_dev *rt2x00dev) |
| 420 | { |
Ivo van Doorn | c965c74 | 2010-07-11 12:25:46 +0200 | [diff] [blame] | 421 | INIT_DELAYED_WORK(&rt2x00dev->link.watchdog_work, rt2x00link_watchdog); |
Ivo van Doorn | 84e3196 | 2008-12-20 10:53:29 +0100 | [diff] [blame] | 422 | INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00link_tuner); |
| 423 | } |