rt2x00link.c 13 KB

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