rt2x00link.c 13 KB

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