rt2x00link.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
  3. <http://rt2x00.serialmonkey.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the
  14. Free Software Foundation, Inc.,
  15. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. /*
  18. Module: rt2x00lib
  19. Abstract: rt2x00 generic link tuning routines.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include "rt2x00.h"
  24. #include "rt2x00lib.h"
  25. /*
  26. * When we lack RSSI information return something less then -80 to
  27. * tell the driver to tune the device to maximum sensitivity.
  28. */
  29. #define DEFAULT_RSSI -128
  30. /*
  31. * Helper struct and macro to work with moving/walking averages.
  32. * When adding a value to the average value the following calculation
  33. * is needed:
  34. *
  35. * avg_rssi = ((avg_rssi * 7) + rssi) / 8;
  36. *
  37. * The advantage of this approach is that we only need 1 variable
  38. * to store the average in (No need for a count and a total).
  39. * But more importantly, normal average values will over time
  40. * move less and less towards newly added values this results
  41. * that with link tuning, the device can have a very good RSSI
  42. * for a few minutes but when the device is moved away from the AP
  43. * the average will not decrease fast enough to compensate.
  44. * The walking average compensates this and will move towards
  45. * the new values correctly allowing a effective link tuning,
  46. * the speed of the average moving towards other values depends
  47. * on the value for the number of samples. The higher the number
  48. * of samples, the slower the average will move.
  49. * We use two variables to keep track of the average value to
  50. * compensate for the rounding errors. This can be a significant
  51. * error (>5dBm) if the factor is too low.
  52. */
  53. #define AVG_SAMPLES 8
  54. #define AVG_FACTOR 1000
  55. #define MOVING_AVERAGE(__avg, __val) \
  56. ({ \
  57. struct avg_val __new; \
  58. __new.avg_weight = \
  59. (__avg).avg_weight ? \
  60. ((((__avg).avg_weight * ((AVG_SAMPLES) - 1)) + \
  61. ((__val) * (AVG_FACTOR))) / \
  62. (AVG_SAMPLES) ) : \
  63. ((__val) * (AVG_FACTOR)); \
  64. __new.avg = __new.avg_weight / (AVG_FACTOR); \
  65. __new; \
  66. })
  67. static int rt2x00link_antenna_get_link_rssi(struct rt2x00_dev *rt2x00dev)
  68. {
  69. struct link_ant *ant = &rt2x00dev->link.ant;
  70. if (ant->rssi_ant.avg && rt2x00dev->link.qual.rx_success)
  71. return ant->rssi_ant.avg;
  72. return DEFAULT_RSSI;
  73. }
  74. static int rt2x00link_antenna_get_rssi_history(struct rt2x00_dev *rt2x00dev)
  75. {
  76. struct link_ant *ant = &rt2x00dev->link.ant;
  77. if (ant->rssi_history)
  78. return ant->rssi_history;
  79. return DEFAULT_RSSI;
  80. }
  81. static void rt2x00link_antenna_update_rssi_history(struct rt2x00_dev *rt2x00dev,
  82. int rssi)
  83. {
  84. struct link_ant *ant = &rt2x00dev->link.ant;
  85. ant->rssi_history = rssi;
  86. }
  87. static void rt2x00link_antenna_reset(struct rt2x00_dev *rt2x00dev)
  88. {
  89. rt2x00dev->link.ant.rssi_ant.avg = 0;
  90. rt2x00dev->link.ant.rssi_ant.avg_weight = 0;
  91. }
  92. static void rt2x00lib_antenna_diversity_sample(struct rt2x00_dev *rt2x00dev)
  93. {
  94. struct link_ant *ant = &rt2x00dev->link.ant;
  95. struct antenna_setup new_ant;
  96. int other_antenna;
  97. int sample_current = rt2x00link_antenna_get_link_rssi(rt2x00dev);
  98. int sample_other = rt2x00link_antenna_get_rssi_history(rt2x00dev);
  99. memcpy(&new_ant, &ant->active, sizeof(new_ant));
  100. /*
  101. * We are done sampling. Now we should evaluate the results.
  102. */
  103. ant->flags &= ~ANTENNA_MODE_SAMPLE;
  104. /*
  105. * During the last period we have sampled the RSSI
  106. * from both antennas. It now is time to determine
  107. * which antenna demonstrated the best performance.
  108. * When we are already on the antenna with the best
  109. * performance, just create a good starting point
  110. * for the history and we are done.
  111. */
  112. if (sample_current >= sample_other) {
  113. rt2x00link_antenna_update_rssi_history(rt2x00dev,
  114. sample_current);
  115. return;
  116. }
  117. other_antenna = (ant->active.rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
  118. if (ant->flags & ANTENNA_RX_DIVERSITY)
  119. new_ant.rx = other_antenna;
  120. if (ant->flags & ANTENNA_TX_DIVERSITY)
  121. new_ant.tx = other_antenna;
  122. rt2x00lib_config_antenna(rt2x00dev, new_ant);
  123. }
  124. static void rt2x00lib_antenna_diversity_eval(struct rt2x00_dev *rt2x00dev)
  125. {
  126. struct link_ant *ant = &rt2x00dev->link.ant;
  127. struct antenna_setup new_ant;
  128. int rssi_curr;
  129. int rssi_old;
  130. memcpy(&new_ant, &ant->active, sizeof(new_ant));
  131. /*
  132. * Get current RSSI value along with the historical value,
  133. * after that update the history with the current value.
  134. */
  135. rssi_curr = rt2x00link_antenna_get_link_rssi(rt2x00dev);
  136. rssi_old = rt2x00link_antenna_get_rssi_history(rt2x00dev);
  137. rt2x00link_antenna_update_rssi_history(rt2x00dev, rssi_curr);
  138. /*
  139. * Legacy driver indicates that we should swap antenna's
  140. * when the difference in RSSI is greater that 5. This
  141. * also should be done when the RSSI was actually better
  142. * then the previous sample.
  143. * When the difference exceeds the threshold we should
  144. * sample the rssi from the other antenna to make a valid
  145. * comparison between the 2 antennas.
  146. */
  147. if (abs(rssi_curr - rssi_old) < 5)
  148. return;
  149. ant->flags |= ANTENNA_MODE_SAMPLE;
  150. if (ant->flags & ANTENNA_RX_DIVERSITY)
  151. new_ant.rx = (new_ant.rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
  152. if (ant->flags & ANTENNA_TX_DIVERSITY)
  153. new_ant.tx = (new_ant.tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
  154. rt2x00lib_config_antenna(rt2x00dev, new_ant);
  155. }
  156. static bool rt2x00lib_antenna_diversity(struct rt2x00_dev *rt2x00dev)
  157. {
  158. struct link_ant *ant = &rt2x00dev->link.ant;
  159. unsigned int flags = ant->flags;
  160. /*
  161. * Determine if software diversity is enabled for
  162. * either the TX or RX antenna (or both).
  163. * Always perform this check since within the link
  164. * tuner interval the configuration might have changed.
  165. */
  166. flags &= ~ANTENNA_RX_DIVERSITY;
  167. flags &= ~ANTENNA_TX_DIVERSITY;
  168. if (rt2x00dev->default_ant.rx == ANTENNA_SW_DIVERSITY)
  169. flags |= ANTENNA_RX_DIVERSITY;
  170. if (rt2x00dev->default_ant.tx == ANTENNA_SW_DIVERSITY)
  171. flags |= ANTENNA_TX_DIVERSITY;
  172. if (!(ant->flags & ANTENNA_RX_DIVERSITY) &&
  173. !(ant->flags & ANTENNA_TX_DIVERSITY)) {
  174. ant->flags = 0;
  175. return true;
  176. }
  177. /* Update flags */
  178. ant->flags = flags;
  179. /*
  180. * If we have only sampled the data over the last period
  181. * we should now harvest the data. Otherwise just evaluate
  182. * the data. The latter should only be performed once
  183. * every 2 seconds.
  184. */
  185. if (ant->flags & ANTENNA_MODE_SAMPLE) {
  186. rt2x00lib_antenna_diversity_sample(rt2x00dev);
  187. return true;
  188. } else if (rt2x00dev->link.count & 1) {
  189. rt2x00lib_antenna_diversity_eval(rt2x00dev);
  190. return true;
  191. }
  192. return false;
  193. }
  194. void rt2x00link_update_stats(struct rt2x00_dev *rt2x00dev,
  195. struct sk_buff *skb,
  196. struct rxdone_entry_desc *rxdesc)
  197. {
  198. struct link *link = &rt2x00dev->link;
  199. struct link_qual *qual = &rt2x00dev->link.qual;
  200. struct link_ant *ant = &rt2x00dev->link.ant;
  201. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  202. /*
  203. * Frame was received successfully since non-succesfull
  204. * frames would have been dropped by the hardware.
  205. */
  206. qual->rx_success++;
  207. /*
  208. * We are only interested in quality statistics from
  209. * beacons which came from the BSS which we are
  210. * associated with.
  211. */
  212. if (!ieee80211_is_beacon(hdr->frame_control) ||
  213. !(rxdesc->dev_flags & RXDONE_MY_BSS))
  214. return;
  215. /*
  216. * Update global RSSI
  217. */
  218. link->avg_rssi = MOVING_AVERAGE(link->avg_rssi, rxdesc->rssi);
  219. /*
  220. * Update antenna RSSI
  221. */
  222. ant->rssi_ant = MOVING_AVERAGE(ant->rssi_ant, rxdesc->rssi);
  223. }
  224. void rt2x00link_start_tuner(struct rt2x00_dev *rt2x00dev)
  225. {
  226. struct link *link = &rt2x00dev->link;
  227. /*
  228. * Link tuning should only be performed when
  229. * an active sta interface exists. AP interfaces
  230. * don't need link tuning and monitor mode interfaces
  231. * should never have to work with link tuners.
  232. */
  233. if (!rt2x00dev->intf_sta_count)
  234. return;
  235. /**
  236. * While scanning, link tuning is disabled. By default
  237. * the most sensitive settings will be used to make sure
  238. * that all beacons and probe responses will be recieved
  239. * during the scan.
  240. */
  241. if (test_bit(DEVICE_STATE_SCANNING, &rt2x00dev->flags))
  242. return;
  243. rt2x00link_reset_tuner(rt2x00dev, false);
  244. if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  245. ieee80211_queue_delayed_work(rt2x00dev->hw,
  246. &link->work, LINK_TUNE_INTERVAL);
  247. }
  248. void rt2x00link_stop_tuner(struct rt2x00_dev *rt2x00dev)
  249. {
  250. cancel_delayed_work_sync(&rt2x00dev->link.work);
  251. }
  252. void rt2x00link_reset_tuner(struct rt2x00_dev *rt2x00dev, bool antenna)
  253. {
  254. struct link_qual *qual = &rt2x00dev->link.qual;
  255. u8 vgc_level = qual->vgc_level_reg;
  256. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  257. return;
  258. /*
  259. * Reset link information.
  260. * Both the currently active vgc level as well as
  261. * the link tuner counter should be reset. Resetting
  262. * the counter is important for devices where the
  263. * device should only perform link tuning during the
  264. * first minute after being enabled.
  265. */
  266. rt2x00dev->link.count = 0;
  267. memset(qual, 0, sizeof(*qual));
  268. /*
  269. * Restore the VGC level as stored in the registers,
  270. * the driver can use this to determine if the register
  271. * must be updated during reset or not.
  272. */
  273. qual->vgc_level_reg = vgc_level;
  274. /*
  275. * Reset the link tuner.
  276. */
  277. rt2x00dev->ops->lib->reset_tuner(rt2x00dev, qual);
  278. if (antenna)
  279. rt2x00link_antenna_reset(rt2x00dev);
  280. }
  281. static void rt2x00link_reset_qual(struct rt2x00_dev *rt2x00dev)
  282. {
  283. struct link_qual *qual = &rt2x00dev->link.qual;
  284. qual->rx_success = 0;
  285. qual->rx_failed = 0;
  286. qual->tx_success = 0;
  287. qual->tx_failed = 0;
  288. }
  289. static void rt2x00link_tuner(struct work_struct *work)
  290. {
  291. struct rt2x00_dev *rt2x00dev =
  292. container_of(work, struct rt2x00_dev, link.work.work);
  293. struct link *link = &rt2x00dev->link;
  294. struct link_qual *qual = &rt2x00dev->link.qual;
  295. /*
  296. * When the radio is shutting down we should
  297. * immediately cease all link tuning.
  298. */
  299. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags) ||
  300. test_bit(DEVICE_STATE_SCANNING, &rt2x00dev->flags))
  301. return;
  302. /*
  303. * Update statistics.
  304. */
  305. rt2x00dev->ops->lib->link_stats(rt2x00dev, qual);
  306. rt2x00dev->low_level_stats.dot11FCSErrorCount += qual->rx_failed;
  307. /*
  308. * Update quality RSSI for link tuning,
  309. * when we have received some frames and we managed to
  310. * collect the RSSI data we could use this. Otherwise we
  311. * must fallback to the default RSSI value.
  312. */
  313. if (!link->avg_rssi.avg || !qual->rx_success)
  314. qual->rssi = DEFAULT_RSSI;
  315. else
  316. qual->rssi = link->avg_rssi.avg;
  317. /*
  318. * Check if link tuning is supported by the hardware, some hardware
  319. * do not support link tuning at all, while other devices can disable
  320. * the feature from the EEPROM.
  321. */
  322. if (test_bit(DRIVER_SUPPORT_LINK_TUNING, &rt2x00dev->flags))
  323. rt2x00dev->ops->lib->link_tuner(rt2x00dev, qual, link->count);
  324. /*
  325. * Send a signal to the led to update the led signal strength.
  326. */
  327. rt2x00leds_led_quality(rt2x00dev, qual->rssi);
  328. /*
  329. * Evaluate antenna setup, make this the last step when
  330. * rt2x00lib_antenna_diversity made changes the quality
  331. * statistics will be reset.
  332. */
  333. if (rt2x00lib_antenna_diversity(rt2x00dev))
  334. rt2x00link_reset_qual(rt2x00dev);
  335. /*
  336. * Increase tuner counter, and reschedule the next link tuner run.
  337. */
  338. link->count++;
  339. if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  340. ieee80211_queue_delayed_work(rt2x00dev->hw,
  341. &link->work, LINK_TUNE_INTERVAL);
  342. }
  343. void rt2x00link_start_watchdog(struct rt2x00_dev *rt2x00dev)
  344. {
  345. struct link *link = &rt2x00dev->link;
  346. if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) ||
  347. !test_bit(DRIVER_SUPPORT_WATCHDOG, &rt2x00dev->flags))
  348. return;
  349. ieee80211_queue_delayed_work(rt2x00dev->hw,
  350. &link->watchdog_work, WATCHDOG_INTERVAL);
  351. }
  352. void rt2x00link_stop_watchdog(struct rt2x00_dev *rt2x00dev)
  353. {
  354. cancel_delayed_work_sync(&rt2x00dev->link.watchdog_work);
  355. }
  356. static void rt2x00link_watchdog(struct work_struct *work)
  357. {
  358. struct rt2x00_dev *rt2x00dev =
  359. container_of(work, struct rt2x00_dev, link.watchdog_work.work);
  360. struct link *link = &rt2x00dev->link;
  361. /*
  362. * When the radio is shutting down we should
  363. * immediately cease the watchdog monitoring.
  364. */
  365. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  366. return;
  367. rt2x00dev->ops->lib->watchdog(rt2x00dev);
  368. if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  369. ieee80211_queue_delayed_work(rt2x00dev->hw,
  370. &link->watchdog_work, WATCHDOG_INTERVAL);
  371. }
  372. void rt2x00link_register(struct rt2x00_dev *rt2x00dev)
  373. {
  374. INIT_DELAYED_WORK(&rt2x00dev->link.watchdog_work, rt2x00link_watchdog);
  375. INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00link_tuner);
  376. }