rt2x00link.c 14 KB

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