rt2x00link.c 13 KB

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