rt2x00dev.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  1. /*
  2. Copyright (C) 2004 - 2008 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 device routines.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include "rt2x00.h"
  24. #include "rt2x00lib.h"
  25. /*
  26. * Link tuning handlers
  27. */
  28. void rt2x00lib_reset_link_tuner(struct rt2x00_dev *rt2x00dev)
  29. {
  30. if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  31. return;
  32. /*
  33. * Reset link information.
  34. * Both the currently active vgc level as well as
  35. * the link tuner counter should be reset. Resetting
  36. * the counter is important for devices where the
  37. * device should only perform link tuning during the
  38. * first minute after being enabled.
  39. */
  40. rt2x00dev->link.count = 0;
  41. rt2x00dev->link.vgc_level = 0;
  42. /*
  43. * Reset the link tuner.
  44. */
  45. rt2x00dev->ops->lib->reset_tuner(rt2x00dev);
  46. }
  47. static void rt2x00lib_start_link_tuner(struct rt2x00_dev *rt2x00dev)
  48. {
  49. /*
  50. * Clear all (possibly) pre-existing quality statistics.
  51. */
  52. memset(&rt2x00dev->link.qual, 0, sizeof(rt2x00dev->link.qual));
  53. /*
  54. * The RX and TX percentage should start at 50%
  55. * this will assure we will get at least get some
  56. * decent value when the link tuner starts.
  57. * The value will be dropped and overwritten with
  58. * the correct (measured )value anyway during the
  59. * first run of the link tuner.
  60. */
  61. rt2x00dev->link.qual.rx_percentage = 50;
  62. rt2x00dev->link.qual.tx_percentage = 50;
  63. rt2x00lib_reset_link_tuner(rt2x00dev);
  64. queue_delayed_work(rt2x00dev->hw->workqueue,
  65. &rt2x00dev->link.work, LINK_TUNE_INTERVAL);
  66. }
  67. static void rt2x00lib_stop_link_tuner(struct rt2x00_dev *rt2x00dev)
  68. {
  69. cancel_delayed_work_sync(&rt2x00dev->link.work);
  70. }
  71. /*
  72. * Radio control handlers.
  73. */
  74. int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
  75. {
  76. int status;
  77. /*
  78. * Don't enable the radio twice.
  79. * And check if the hardware button has been disabled.
  80. */
  81. if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
  82. test_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags))
  83. return 0;
  84. /*
  85. * Initialize all data queues.
  86. */
  87. rt2x00queue_init_rx(rt2x00dev);
  88. rt2x00queue_init_tx(rt2x00dev);
  89. /*
  90. * Enable radio.
  91. */
  92. status =
  93. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_ON);
  94. if (status)
  95. return status;
  96. rt2x00leds_led_radio(rt2x00dev, true);
  97. rt2x00led_led_activity(rt2x00dev, true);
  98. __set_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags);
  99. /*
  100. * Enable RX.
  101. */
  102. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
  103. /*
  104. * Start the TX queues.
  105. */
  106. ieee80211_wake_queues(rt2x00dev->hw);
  107. return 0;
  108. }
  109. void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
  110. {
  111. if (!__test_and_clear_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  112. return;
  113. /*
  114. * Stop all scheduled work.
  115. */
  116. if (work_pending(&rt2x00dev->intf_work))
  117. cancel_work_sync(&rt2x00dev->intf_work);
  118. if (work_pending(&rt2x00dev->filter_work))
  119. cancel_work_sync(&rt2x00dev->filter_work);
  120. /*
  121. * Stop the TX queues.
  122. */
  123. ieee80211_stop_queues(rt2x00dev->hw);
  124. /*
  125. * Disable RX.
  126. */
  127. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
  128. /*
  129. * Disable radio.
  130. */
  131. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
  132. rt2x00led_led_activity(rt2x00dev, false);
  133. rt2x00leds_led_radio(rt2x00dev, false);
  134. }
  135. void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, enum dev_state state)
  136. {
  137. /*
  138. * When we are disabling the RX, we should also stop the link tuner.
  139. */
  140. if (state == STATE_RADIO_RX_OFF)
  141. rt2x00lib_stop_link_tuner(rt2x00dev);
  142. rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
  143. /*
  144. * When we are enabling the RX, we should also start the link tuner.
  145. */
  146. if (state == STATE_RADIO_RX_ON &&
  147. (rt2x00dev->intf_ap_count || rt2x00dev->intf_sta_count))
  148. rt2x00lib_start_link_tuner(rt2x00dev);
  149. }
  150. static void rt2x00lib_evaluate_antenna_sample(struct rt2x00_dev *rt2x00dev)
  151. {
  152. enum antenna rx = rt2x00dev->link.ant.active.rx;
  153. enum antenna tx = rt2x00dev->link.ant.active.tx;
  154. int sample_a =
  155. rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_A);
  156. int sample_b =
  157. rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_B);
  158. /*
  159. * We are done sampling. Now we should evaluate the results.
  160. */
  161. rt2x00dev->link.ant.flags &= ~ANTENNA_MODE_SAMPLE;
  162. /*
  163. * During the last period we have sampled the RSSI
  164. * from both antenna's. It now is time to determine
  165. * which antenna demonstrated the best performance.
  166. * When we are already on the antenna with the best
  167. * performance, then there really is nothing for us
  168. * left to do.
  169. */
  170. if (sample_a == sample_b)
  171. return;
  172. if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY)
  173. rx = (sample_a > sample_b) ? ANTENNA_A : ANTENNA_B;
  174. if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)
  175. tx = (sample_a > sample_b) ? ANTENNA_A : ANTENNA_B;
  176. rt2x00lib_config_antenna(rt2x00dev, rx, tx);
  177. }
  178. static void rt2x00lib_evaluate_antenna_eval(struct rt2x00_dev *rt2x00dev)
  179. {
  180. enum antenna rx = rt2x00dev->link.ant.active.rx;
  181. enum antenna tx = rt2x00dev->link.ant.active.tx;
  182. int rssi_curr = rt2x00_get_link_ant_rssi(&rt2x00dev->link);
  183. int rssi_old = rt2x00_update_ant_rssi(&rt2x00dev->link, rssi_curr);
  184. /*
  185. * Legacy driver indicates that we should swap antenna's
  186. * when the difference in RSSI is greater that 5. This
  187. * also should be done when the RSSI was actually better
  188. * then the previous sample.
  189. * When the difference exceeds the threshold we should
  190. * sample the rssi from the other antenna to make a valid
  191. * comparison between the 2 antennas.
  192. */
  193. if (abs(rssi_curr - rssi_old) < 5)
  194. return;
  195. rt2x00dev->link.ant.flags |= ANTENNA_MODE_SAMPLE;
  196. if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY)
  197. rx = (rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
  198. if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)
  199. tx = (tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
  200. rt2x00lib_config_antenna(rt2x00dev, rx, tx);
  201. }
  202. static void rt2x00lib_evaluate_antenna(struct rt2x00_dev *rt2x00dev)
  203. {
  204. /*
  205. * Determine if software diversity is enabled for
  206. * either the TX or RX antenna (or both).
  207. * Always perform this check since within the link
  208. * tuner interval the configuration might have changed.
  209. */
  210. rt2x00dev->link.ant.flags &= ~ANTENNA_RX_DIVERSITY;
  211. rt2x00dev->link.ant.flags &= ~ANTENNA_TX_DIVERSITY;
  212. if (rt2x00dev->hw->conf.antenna_sel_rx == 0 &&
  213. rt2x00dev->default_ant.rx == ANTENNA_SW_DIVERSITY)
  214. rt2x00dev->link.ant.flags |= ANTENNA_RX_DIVERSITY;
  215. if (rt2x00dev->hw->conf.antenna_sel_tx == 0 &&
  216. rt2x00dev->default_ant.tx == ANTENNA_SW_DIVERSITY)
  217. rt2x00dev->link.ant.flags |= ANTENNA_TX_DIVERSITY;
  218. if (!(rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY) &&
  219. !(rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)) {
  220. rt2x00dev->link.ant.flags = 0;
  221. return;
  222. }
  223. /*
  224. * If we have only sampled the data over the last period
  225. * we should now harvest the data. Otherwise just evaluate
  226. * the data. The latter should only be performed once
  227. * every 2 seconds.
  228. */
  229. if (rt2x00dev->link.ant.flags & ANTENNA_MODE_SAMPLE)
  230. rt2x00lib_evaluate_antenna_sample(rt2x00dev);
  231. else if (rt2x00dev->link.count & 1)
  232. rt2x00lib_evaluate_antenna_eval(rt2x00dev);
  233. }
  234. static void rt2x00lib_update_link_stats(struct link *link, int rssi)
  235. {
  236. int avg_rssi = rssi;
  237. /*
  238. * Update global RSSI
  239. */
  240. if (link->qual.avg_rssi)
  241. avg_rssi = MOVING_AVERAGE(link->qual.avg_rssi, rssi, 8);
  242. link->qual.avg_rssi = avg_rssi;
  243. /*
  244. * Update antenna RSSI
  245. */
  246. if (link->ant.rssi_ant)
  247. rssi = MOVING_AVERAGE(link->ant.rssi_ant, rssi, 8);
  248. link->ant.rssi_ant = rssi;
  249. }
  250. static void rt2x00lib_precalculate_link_signal(struct link_qual *qual)
  251. {
  252. if (qual->rx_failed || qual->rx_success)
  253. qual->rx_percentage =
  254. (qual->rx_success * 100) /
  255. (qual->rx_failed + qual->rx_success);
  256. else
  257. qual->rx_percentage = 50;
  258. if (qual->tx_failed || qual->tx_success)
  259. qual->tx_percentage =
  260. (qual->tx_success * 100) /
  261. (qual->tx_failed + qual->tx_success);
  262. else
  263. qual->tx_percentage = 50;
  264. qual->rx_success = 0;
  265. qual->rx_failed = 0;
  266. qual->tx_success = 0;
  267. qual->tx_failed = 0;
  268. }
  269. static int rt2x00lib_calculate_link_signal(struct rt2x00_dev *rt2x00dev,
  270. int rssi)
  271. {
  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. if (rt2x00dev->rssi_offset)
  284. rssi_percentage = (rssi * 100) / rt2x00dev->rssi_offset;
  285. /*
  286. * Add the individual percentages and use the WEIGHT
  287. * defines to calculate the current link signal.
  288. */
  289. signal = ((WEIGHT_RSSI * rssi_percentage) +
  290. (WEIGHT_TX * rt2x00dev->link.qual.tx_percentage) +
  291. (WEIGHT_RX * rt2x00dev->link.qual.rx_percentage)) / 100;
  292. return (signal > 100) ? 100 : signal;
  293. }
  294. static void rt2x00lib_link_tuner(struct work_struct *work)
  295. {
  296. struct rt2x00_dev *rt2x00dev =
  297. container_of(work, struct rt2x00_dev, link.work.work);
  298. /*
  299. * When the radio is shutting down we should
  300. * immediately cease all link tuning.
  301. */
  302. if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  303. return;
  304. /*
  305. * Update statistics.
  306. */
  307. rt2x00dev->ops->lib->link_stats(rt2x00dev, &rt2x00dev->link.qual);
  308. rt2x00dev->low_level_stats.dot11FCSErrorCount +=
  309. rt2x00dev->link.qual.rx_failed;
  310. /*
  311. * Only perform the link tuning when Link tuning
  312. * has been enabled (This could have been disabled from the EEPROM).
  313. */
  314. if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
  315. rt2x00dev->ops->lib->link_tuner(rt2x00dev);
  316. /*
  317. * Precalculate a portion of the link signal which is
  318. * in based on the tx/rx success/failure counters.
  319. */
  320. rt2x00lib_precalculate_link_signal(&rt2x00dev->link.qual);
  321. /*
  322. * Send a signal to the led to update the led signal strength.
  323. */
  324. rt2x00leds_led_quality(rt2x00dev, rt2x00dev->link.qual.avg_rssi);
  325. /*
  326. * Evaluate antenna setup, make this the last step since this could
  327. * possibly reset some statistics.
  328. */
  329. rt2x00lib_evaluate_antenna(rt2x00dev);
  330. /*
  331. * Increase tuner counter, and reschedule the next link tuner run.
  332. */
  333. rt2x00dev->link.count++;
  334. queue_delayed_work(rt2x00dev->hw->workqueue, &rt2x00dev->link.work,
  335. LINK_TUNE_INTERVAL);
  336. }
  337. static void rt2x00lib_packetfilter_scheduled(struct work_struct *work)
  338. {
  339. struct rt2x00_dev *rt2x00dev =
  340. container_of(work, struct rt2x00_dev, filter_work);
  341. rt2x00dev->ops->lib->config_filter(rt2x00dev, rt2x00dev->packet_filter);
  342. }
  343. static void rt2x00lib_intf_scheduled_iter(void *data, u8 *mac,
  344. struct ieee80211_vif *vif)
  345. {
  346. struct rt2x00_dev *rt2x00dev = data;
  347. struct rt2x00_intf *intf = vif_to_intf(vif);
  348. struct sk_buff *skb;
  349. struct ieee80211_tx_control control;
  350. struct ieee80211_bss_conf conf;
  351. int delayed_flags;
  352. /*
  353. * Copy all data we need during this action under the protection
  354. * of a spinlock. Otherwise race conditions might occur which results
  355. * into an invalid configuration.
  356. */
  357. spin_lock(&intf->lock);
  358. memcpy(&conf, &intf->conf, sizeof(conf));
  359. delayed_flags = intf->delayed_flags;
  360. intf->delayed_flags = 0;
  361. spin_unlock(&intf->lock);
  362. if (delayed_flags & DELAYED_UPDATE_BEACON) {
  363. skb = ieee80211_beacon_get(rt2x00dev->hw, vif, &control);
  364. if (skb && rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw,
  365. skb, &control))
  366. dev_kfree_skb(skb);
  367. }
  368. if (delayed_flags & DELAYED_CONFIG_ERP)
  369. rt2x00lib_config_erp(rt2x00dev, intf, &intf->conf);
  370. if (delayed_flags & DELAYED_LED_ASSOC)
  371. rt2x00leds_led_assoc(rt2x00dev, !!rt2x00dev->intf_associated);
  372. }
  373. static void rt2x00lib_intf_scheduled(struct work_struct *work)
  374. {
  375. struct rt2x00_dev *rt2x00dev =
  376. container_of(work, struct rt2x00_dev, intf_work);
  377. /*
  378. * Iterate over each interface and perform the
  379. * requested configurations.
  380. */
  381. ieee80211_iterate_active_interfaces(rt2x00dev->hw,
  382. rt2x00lib_intf_scheduled_iter,
  383. rt2x00dev);
  384. }
  385. /*
  386. * Interrupt context handlers.
  387. */
  388. static void rt2x00lib_beacondone_iter(void *data, u8 *mac,
  389. struct ieee80211_vif *vif)
  390. {
  391. struct rt2x00_intf *intf = vif_to_intf(vif);
  392. if (vif->type != IEEE80211_IF_TYPE_AP &&
  393. vif->type != IEEE80211_IF_TYPE_IBSS)
  394. return;
  395. spin_lock(&intf->lock);
  396. intf->delayed_flags |= DELAYED_UPDATE_BEACON;
  397. spin_unlock(&intf->lock);
  398. }
  399. void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
  400. {
  401. if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  402. return;
  403. ieee80211_iterate_active_interfaces(rt2x00dev->hw,
  404. rt2x00lib_beacondone_iter,
  405. rt2x00dev);
  406. queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->intf_work);
  407. }
  408. EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
  409. void rt2x00lib_txdone(struct queue_entry *entry,
  410. struct txdone_entry_desc *txdesc)
  411. {
  412. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  413. struct skb_frame_desc *skbdesc;
  414. struct ieee80211_tx_status tx_status;
  415. /*
  416. * Update TX statistics.
  417. */
  418. rt2x00dev->link.qual.tx_success +=
  419. test_bit(TXDONE_SUCCESS, &txdesc->flags);
  420. rt2x00dev->link.qual.tx_failed +=
  421. txdesc->retry + !!test_bit(TXDONE_FAILURE, &txdesc->flags);
  422. /*
  423. * Initialize TX status
  424. */
  425. tx_status.flags = 0;
  426. tx_status.ack_signal = 0;
  427. tx_status.excessive_retries =
  428. test_bit(TXDONE_EXCESSIVE_RETRY, &txdesc->flags);
  429. tx_status.retry_count = txdesc->retry;
  430. memcpy(&tx_status.control, txdesc->control, sizeof(*txdesc->control));
  431. if (!(tx_status.control.flags & IEEE80211_TXCTL_NO_ACK)) {
  432. if (test_bit(TXDONE_SUCCESS, &txdesc->flags))
  433. tx_status.flags |= IEEE80211_TX_STATUS_ACK;
  434. else if (test_bit(TXDONE_FAILURE, &txdesc->flags))
  435. rt2x00dev->low_level_stats.dot11ACKFailureCount++;
  436. }
  437. if (tx_status.control.flags & IEEE80211_TXCTL_USE_RTS_CTS) {
  438. if (test_bit(TXDONE_SUCCESS, &txdesc->flags))
  439. rt2x00dev->low_level_stats.dot11RTSSuccessCount++;
  440. else if (test_bit(TXDONE_FAILURE, &txdesc->flags))
  441. rt2x00dev->low_level_stats.dot11RTSFailureCount++;
  442. }
  443. /*
  444. * Send the tx_status to debugfs. Only send the status report
  445. * to mac80211 when the frame originated from there. If this was
  446. * a extra frame coming through a mac80211 library call (RTS/CTS)
  447. * then we should not send the status report back.
  448. * If send to mac80211, mac80211 will clean up the skb structure,
  449. * otherwise we have to do it ourself.
  450. */
  451. rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TXDONE, entry->skb);
  452. skbdesc = get_skb_frame_desc(entry->skb);
  453. if (!(skbdesc->flags & FRAME_DESC_DRIVER_GENERATED))
  454. ieee80211_tx_status_irqsafe(rt2x00dev->hw,
  455. entry->skb, &tx_status);
  456. else
  457. dev_kfree_skb_irq(entry->skb);
  458. entry->skb = NULL;
  459. }
  460. EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
  461. void rt2x00lib_rxdone(struct queue_entry *entry,
  462. struct rxdone_entry_desc *rxdesc)
  463. {
  464. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  465. struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
  466. struct ieee80211_supported_band *sband;
  467. struct ieee80211_hdr *hdr;
  468. const struct rt2x00_rate *rate;
  469. unsigned int i;
  470. int idx = -1;
  471. u16 fc;
  472. /*
  473. * Update RX statistics.
  474. */
  475. sband = &rt2x00dev->bands[rt2x00dev->curr_band];
  476. for (i = 0; i < sband->n_bitrates; i++) {
  477. rate = rt2x00_get_rate(sband->bitrates[i].hw_value);
  478. if (((rxdesc->dev_flags & RXDONE_SIGNAL_PLCP) &&
  479. (rate->plcp == rxdesc->signal)) ||
  480. (!(rxdesc->dev_flags & RXDONE_SIGNAL_PLCP) &&
  481. (rate->bitrate == rxdesc->signal))) {
  482. idx = i;
  483. break;
  484. }
  485. }
  486. if (idx < 0) {
  487. WARNING(rt2x00dev, "Frame received with unrecognized signal,"
  488. "signal=0x%.2x, plcp=%d.\n", rxdesc->signal,
  489. !!(rxdesc->dev_flags & RXDONE_SIGNAL_PLCP));
  490. idx = 0;
  491. }
  492. /*
  493. * Only update link status if this is a beacon frame carrying our bssid.
  494. */
  495. hdr = (struct ieee80211_hdr *)entry->skb->data;
  496. fc = le16_to_cpu(hdr->frame_control);
  497. if (is_beacon(fc) && (rxdesc->dev_flags & RXDONE_MY_BSS))
  498. rt2x00lib_update_link_stats(&rt2x00dev->link, rxdesc->rssi);
  499. rt2x00dev->link.qual.rx_success++;
  500. rx_status->rate_idx = idx;
  501. rx_status->qual =
  502. rt2x00lib_calculate_link_signal(rt2x00dev, rxdesc->rssi);
  503. rx_status->signal = rxdesc->rssi;
  504. rx_status->flag = rxdesc->flags;
  505. rx_status->antenna = rt2x00dev->link.ant.active.rx;
  506. /*
  507. * Send frame to mac80211 & debugfs.
  508. * mac80211 will clean up the skb structure.
  509. */
  510. rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_RXDONE, entry->skb);
  511. ieee80211_rx_irqsafe(rt2x00dev->hw, entry->skb, rx_status);
  512. entry->skb = NULL;
  513. }
  514. EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
  515. /*
  516. * Driver initialization handlers.
  517. */
  518. const struct rt2x00_rate rt2x00_supported_rates[12] = {
  519. {
  520. .flags = DEV_RATE_CCK | DEV_RATE_BASIC,
  521. .bitrate = 10,
  522. .ratemask = BIT(0),
  523. .plcp = 0x00,
  524. },
  525. {
  526. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE | DEV_RATE_BASIC,
  527. .bitrate = 20,
  528. .ratemask = BIT(1),
  529. .plcp = 0x01,
  530. },
  531. {
  532. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE | DEV_RATE_BASIC,
  533. .bitrate = 55,
  534. .ratemask = BIT(2),
  535. .plcp = 0x02,
  536. },
  537. {
  538. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE | DEV_RATE_BASIC,
  539. .bitrate = 110,
  540. .ratemask = BIT(3),
  541. .plcp = 0x03,
  542. },
  543. {
  544. .flags = DEV_RATE_OFDM | DEV_RATE_BASIC,
  545. .bitrate = 60,
  546. .ratemask = BIT(4),
  547. .plcp = 0x0b,
  548. },
  549. {
  550. .flags = DEV_RATE_OFDM,
  551. .bitrate = 90,
  552. .ratemask = BIT(5),
  553. .plcp = 0x0f,
  554. },
  555. {
  556. .flags = DEV_RATE_OFDM | DEV_RATE_BASIC,
  557. .bitrate = 120,
  558. .ratemask = BIT(6),
  559. .plcp = 0x0a,
  560. },
  561. {
  562. .flags = DEV_RATE_OFDM,
  563. .bitrate = 180,
  564. .ratemask = BIT(7),
  565. .plcp = 0x0e,
  566. },
  567. {
  568. .flags = DEV_RATE_OFDM | DEV_RATE_BASIC,
  569. .bitrate = 240,
  570. .ratemask = BIT(8),
  571. .plcp = 0x09,
  572. },
  573. {
  574. .flags = DEV_RATE_OFDM,
  575. .bitrate = 360,
  576. .ratemask = BIT(9),
  577. .plcp = 0x0d,
  578. },
  579. {
  580. .flags = DEV_RATE_OFDM,
  581. .bitrate = 480,
  582. .ratemask = BIT(10),
  583. .plcp = 0x08,
  584. },
  585. {
  586. .flags = DEV_RATE_OFDM,
  587. .bitrate = 540,
  588. .ratemask = BIT(11),
  589. .plcp = 0x0c,
  590. },
  591. };
  592. static void rt2x00lib_channel(struct ieee80211_channel *entry,
  593. const int channel, const int tx_power,
  594. const int value)
  595. {
  596. entry->center_freq = ieee80211_channel_to_frequency(channel);
  597. entry->hw_value = value;
  598. entry->max_power = tx_power;
  599. entry->max_antenna_gain = 0xff;
  600. }
  601. static void rt2x00lib_rate(struct ieee80211_rate *entry,
  602. const u16 index, const struct rt2x00_rate *rate)
  603. {
  604. entry->flags = 0;
  605. entry->bitrate = rate->bitrate;
  606. entry->hw_value = rt2x00_create_rate_hw_value(index, 0);
  607. entry->hw_value_short = entry->hw_value;
  608. if (rate->flags & DEV_RATE_SHORT_PREAMBLE) {
  609. entry->flags |= IEEE80211_RATE_SHORT_PREAMBLE;
  610. entry->hw_value_short |= rt2x00_create_rate_hw_value(index, 1);
  611. }
  612. }
  613. static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
  614. struct hw_mode_spec *spec)
  615. {
  616. struct ieee80211_hw *hw = rt2x00dev->hw;
  617. struct ieee80211_channel *channels;
  618. struct ieee80211_rate *rates;
  619. unsigned int num_rates;
  620. unsigned int i;
  621. unsigned char tx_power;
  622. num_rates = 0;
  623. if (spec->supported_rates & SUPPORT_RATE_CCK)
  624. num_rates += 4;
  625. if (spec->supported_rates & SUPPORT_RATE_OFDM)
  626. num_rates += 8;
  627. channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
  628. if (!channels)
  629. return -ENOMEM;
  630. rates = kzalloc(sizeof(*rates) * num_rates, GFP_KERNEL);
  631. if (!rates)
  632. goto exit_free_channels;
  633. /*
  634. * Initialize Rate list.
  635. */
  636. for (i = 0; i < num_rates; i++)
  637. rt2x00lib_rate(&rates[i], i, rt2x00_get_rate(i));
  638. /*
  639. * Initialize Channel list.
  640. */
  641. for (i = 0; i < spec->num_channels; i++) {
  642. if (spec->channels[i].channel <= 14) {
  643. if (spec->tx_power_bg)
  644. tx_power = spec->tx_power_bg[i];
  645. else
  646. tx_power = spec->tx_power_default;
  647. } else {
  648. if (spec->tx_power_a)
  649. tx_power = spec->tx_power_a[i];
  650. else
  651. tx_power = spec->tx_power_default;
  652. }
  653. rt2x00lib_channel(&channels[i],
  654. spec->channels[i].channel, tx_power, i);
  655. }
  656. /*
  657. * Intitialize 802.11b, 802.11g
  658. * Rates: CCK, OFDM.
  659. * Channels: 2.4 GHz
  660. */
  661. if (spec->supported_bands & SUPPORT_BAND_2GHZ) {
  662. rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_channels = 14;
  663. rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_bitrates = num_rates;
  664. rt2x00dev->bands[IEEE80211_BAND_2GHZ].channels = channels;
  665. rt2x00dev->bands[IEEE80211_BAND_2GHZ].bitrates = rates;
  666. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  667. &rt2x00dev->bands[IEEE80211_BAND_2GHZ];
  668. }
  669. /*
  670. * Intitialize 802.11a
  671. * Rates: OFDM.
  672. * Channels: OFDM, UNII, HiperLAN2.
  673. */
  674. if (spec->supported_bands & SUPPORT_BAND_5GHZ) {
  675. rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_channels =
  676. spec->num_channels - 14;
  677. rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_bitrates =
  678. num_rates - 4;
  679. rt2x00dev->bands[IEEE80211_BAND_5GHZ].channels = &channels[14];
  680. rt2x00dev->bands[IEEE80211_BAND_5GHZ].bitrates = &rates[4];
  681. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  682. &rt2x00dev->bands[IEEE80211_BAND_5GHZ];
  683. }
  684. return 0;
  685. exit_free_channels:
  686. kfree(channels);
  687. ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
  688. return -ENOMEM;
  689. }
  690. static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
  691. {
  692. if (test_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags))
  693. ieee80211_unregister_hw(rt2x00dev->hw);
  694. if (likely(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ])) {
  695. kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->channels);
  696. kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->bitrates);
  697. rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
  698. rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
  699. }
  700. }
  701. static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
  702. {
  703. struct hw_mode_spec *spec = &rt2x00dev->spec;
  704. int status;
  705. /*
  706. * Initialize HW modes.
  707. */
  708. status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
  709. if (status)
  710. return status;
  711. /*
  712. * Initialize HW fields.
  713. */
  714. rt2x00dev->hw->queues = rt2x00dev->ops->tx_queues;
  715. /*
  716. * Register HW.
  717. */
  718. status = ieee80211_register_hw(rt2x00dev->hw);
  719. if (status) {
  720. rt2x00lib_remove_hw(rt2x00dev);
  721. return status;
  722. }
  723. __set_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags);
  724. return 0;
  725. }
  726. /*
  727. * Initialization/uninitialization handlers.
  728. */
  729. static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
  730. {
  731. if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
  732. return;
  733. /*
  734. * Unregister extra components.
  735. */
  736. rt2x00rfkill_unregister(rt2x00dev);
  737. /*
  738. * Allow the HW to uninitialize.
  739. */
  740. rt2x00dev->ops->lib->uninitialize(rt2x00dev);
  741. /*
  742. * Free allocated queue entries.
  743. */
  744. rt2x00queue_uninitialize(rt2x00dev);
  745. }
  746. static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
  747. {
  748. int status;
  749. if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
  750. return 0;
  751. /*
  752. * Allocate all queue entries.
  753. */
  754. status = rt2x00queue_initialize(rt2x00dev);
  755. if (status)
  756. return status;
  757. /*
  758. * Initialize the device.
  759. */
  760. status = rt2x00dev->ops->lib->initialize(rt2x00dev);
  761. if (status) {
  762. rt2x00queue_uninitialize(rt2x00dev);
  763. return status;
  764. }
  765. __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
  766. /*
  767. * Register the extra components.
  768. */
  769. rt2x00rfkill_register(rt2x00dev);
  770. return 0;
  771. }
  772. int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
  773. {
  774. int retval;
  775. if (test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  776. return 0;
  777. /*
  778. * If this is the first interface which is added,
  779. * we should load the firmware now.
  780. */
  781. retval = rt2x00lib_load_firmware(rt2x00dev);
  782. if (retval)
  783. return retval;
  784. /*
  785. * Initialize the device.
  786. */
  787. retval = rt2x00lib_initialize(rt2x00dev);
  788. if (retval)
  789. return retval;
  790. /*
  791. * Enable radio.
  792. */
  793. retval = rt2x00lib_enable_radio(rt2x00dev);
  794. if (retval) {
  795. rt2x00lib_uninitialize(rt2x00dev);
  796. return retval;
  797. }
  798. rt2x00dev->intf_ap_count = 0;
  799. rt2x00dev->intf_sta_count = 0;
  800. rt2x00dev->intf_associated = 0;
  801. __set_bit(DEVICE_STARTED, &rt2x00dev->flags);
  802. return 0;
  803. }
  804. void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
  805. {
  806. if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  807. return;
  808. /*
  809. * Perhaps we can add something smarter here,
  810. * but for now just disabling the radio should do.
  811. */
  812. rt2x00lib_disable_radio(rt2x00dev);
  813. rt2x00dev->intf_ap_count = 0;
  814. rt2x00dev->intf_sta_count = 0;
  815. rt2x00dev->intf_associated = 0;
  816. __clear_bit(DEVICE_STARTED, &rt2x00dev->flags);
  817. }
  818. /*
  819. * driver allocation handlers.
  820. */
  821. int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
  822. {
  823. int retval = -ENOMEM;
  824. /*
  825. * Make room for rt2x00_intf inside the per-interface
  826. * structure ieee80211_vif.
  827. */
  828. rt2x00dev->hw->vif_data_size = sizeof(struct rt2x00_intf);
  829. /*
  830. * Let the driver probe the device to detect the capabilities.
  831. */
  832. retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
  833. if (retval) {
  834. ERROR(rt2x00dev, "Failed to allocate device.\n");
  835. goto exit;
  836. }
  837. /*
  838. * Initialize configuration work.
  839. */
  840. INIT_WORK(&rt2x00dev->intf_work, rt2x00lib_intf_scheduled);
  841. INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
  842. INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
  843. /*
  844. * Allocate queue array.
  845. */
  846. retval = rt2x00queue_allocate(rt2x00dev);
  847. if (retval)
  848. goto exit;
  849. /*
  850. * Initialize ieee80211 structure.
  851. */
  852. retval = rt2x00lib_probe_hw(rt2x00dev);
  853. if (retval) {
  854. ERROR(rt2x00dev, "Failed to initialize hw.\n");
  855. goto exit;
  856. }
  857. /*
  858. * Register extra components.
  859. */
  860. rt2x00leds_register(rt2x00dev);
  861. rt2x00rfkill_allocate(rt2x00dev);
  862. rt2x00debug_register(rt2x00dev);
  863. __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  864. return 0;
  865. exit:
  866. rt2x00lib_remove_dev(rt2x00dev);
  867. return retval;
  868. }
  869. EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
  870. void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
  871. {
  872. __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  873. /*
  874. * Disable radio.
  875. */
  876. rt2x00lib_disable_radio(rt2x00dev);
  877. /*
  878. * Uninitialize device.
  879. */
  880. rt2x00lib_uninitialize(rt2x00dev);
  881. /*
  882. * Free extra components
  883. */
  884. rt2x00debug_deregister(rt2x00dev);
  885. rt2x00rfkill_free(rt2x00dev);
  886. rt2x00leds_unregister(rt2x00dev);
  887. /*
  888. * Free ieee80211_hw memory.
  889. */
  890. rt2x00lib_remove_hw(rt2x00dev);
  891. /*
  892. * Free firmware image.
  893. */
  894. rt2x00lib_free_firmware(rt2x00dev);
  895. /*
  896. * Free queue structures.
  897. */
  898. rt2x00queue_free(rt2x00dev);
  899. }
  900. EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
  901. /*
  902. * Device state handlers
  903. */
  904. #ifdef CONFIG_PM
  905. int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
  906. {
  907. int retval;
  908. NOTICE(rt2x00dev, "Going to sleep.\n");
  909. __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  910. /*
  911. * Only continue if mac80211 has open interfaces.
  912. */
  913. if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  914. goto exit;
  915. __set_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags);
  916. /*
  917. * Disable radio.
  918. */
  919. rt2x00lib_stop(rt2x00dev);
  920. rt2x00lib_uninitialize(rt2x00dev);
  921. /*
  922. * Suspend/disable extra components.
  923. */
  924. rt2x00leds_suspend(rt2x00dev);
  925. rt2x00rfkill_suspend(rt2x00dev);
  926. rt2x00debug_deregister(rt2x00dev);
  927. exit:
  928. /*
  929. * Set device mode to sleep for power management,
  930. * on some hardware this call seems to consistently fail.
  931. * From the specifications it is hard to tell why it fails,
  932. * and if this is a "bad thing".
  933. * Overall it is safe to just ignore the failure and
  934. * continue suspending. The only downside is that the
  935. * device will not be in optimal power save mode, but with
  936. * the radio and the other components already disabled the
  937. * device is as good as disabled.
  938. */
  939. retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
  940. if (retval)
  941. WARNING(rt2x00dev, "Device failed to enter sleep state, "
  942. "continue suspending.\n");
  943. return 0;
  944. }
  945. EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
  946. static void rt2x00lib_resume_intf(void *data, u8 *mac,
  947. struct ieee80211_vif *vif)
  948. {
  949. struct rt2x00_dev *rt2x00dev = data;
  950. struct rt2x00_intf *intf = vif_to_intf(vif);
  951. spin_lock(&intf->lock);
  952. rt2x00lib_config_intf(rt2x00dev, intf,
  953. vif->type, intf->mac, intf->bssid);
  954. /*
  955. * Master or Ad-hoc mode require a new beacon update.
  956. */
  957. if (vif->type == IEEE80211_IF_TYPE_AP ||
  958. vif->type == IEEE80211_IF_TYPE_IBSS)
  959. intf->delayed_flags |= DELAYED_UPDATE_BEACON;
  960. spin_unlock(&intf->lock);
  961. }
  962. int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
  963. {
  964. int retval;
  965. NOTICE(rt2x00dev, "Waking up.\n");
  966. /*
  967. * Restore/enable extra components.
  968. */
  969. rt2x00debug_register(rt2x00dev);
  970. rt2x00rfkill_resume(rt2x00dev);
  971. rt2x00leds_resume(rt2x00dev);
  972. /*
  973. * Only continue if mac80211 had open interfaces.
  974. */
  975. if (!__test_and_clear_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags))
  976. return 0;
  977. /*
  978. * Reinitialize device and all active interfaces.
  979. */
  980. retval = rt2x00lib_start(rt2x00dev);
  981. if (retval)
  982. goto exit;
  983. /*
  984. * Reconfigure device.
  985. */
  986. rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1);
  987. if (!rt2x00dev->hw->conf.radio_enabled)
  988. rt2x00lib_disable_radio(rt2x00dev);
  989. /*
  990. * Iterator over each active interface to
  991. * reconfigure the hardware.
  992. */
  993. ieee80211_iterate_active_interfaces(rt2x00dev->hw,
  994. rt2x00lib_resume_intf, rt2x00dev);
  995. /*
  996. * We are ready again to receive requests from mac80211.
  997. */
  998. __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  999. /*
  1000. * It is possible that during that mac80211 has attempted
  1001. * to send frames while we were suspending or resuming.
  1002. * In that case we have disabled the TX queue and should
  1003. * now enable it again
  1004. */
  1005. ieee80211_wake_queues(rt2x00dev->hw);
  1006. /*
  1007. * During interface iteration we might have changed the
  1008. * delayed_flags, time to handles the event by calling
  1009. * the work handler directly.
  1010. */
  1011. rt2x00lib_intf_scheduled(&rt2x00dev->intf_work);
  1012. return 0;
  1013. exit:
  1014. rt2x00lib_disable_radio(rt2x00dev);
  1015. rt2x00lib_uninitialize(rt2x00dev);
  1016. rt2x00debug_deregister(rt2x00dev);
  1017. return retval;
  1018. }
  1019. EXPORT_SYMBOL_GPL(rt2x00lib_resume);
  1020. #endif /* CONFIG_PM */
  1021. /*
  1022. * rt2x00lib module information.
  1023. */
  1024. MODULE_AUTHOR(DRV_PROJECT);
  1025. MODULE_VERSION(DRV_VERSION);
  1026. MODULE_DESCRIPTION("rt2x00 library");
  1027. MODULE_LICENSE("GPL");