rt2x00dev.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. /*
  2. Copyright (C) 2010 Willow Garage <http://www.willowgarage.com>
  3. Copyright (C) 2004 - 2010 Ivo van Doorn <IvDoorn@gmail.com>
  4. <http://rt2x00.serialmonkey.com>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the
  15. Free Software Foundation, Inc.,
  16. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. /*
  19. Module: rt2x00lib
  20. Abstract: rt2x00 generic device routines.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/log2.h>
  26. #include "rt2x00.h"
  27. #include "rt2x00lib.h"
  28. /*
  29. * Radio control handlers.
  30. */
  31. int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
  32. {
  33. int status;
  34. /*
  35. * Don't enable the radio twice.
  36. * And check if the hardware button has been disabled.
  37. */
  38. if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  39. return 0;
  40. /*
  41. * Initialize all data queues.
  42. */
  43. rt2x00queue_init_queues(rt2x00dev);
  44. /*
  45. * Enable radio.
  46. */
  47. status =
  48. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_ON);
  49. if (status)
  50. return status;
  51. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_ON);
  52. rt2x00leds_led_radio(rt2x00dev, true);
  53. rt2x00led_led_activity(rt2x00dev, true);
  54. set_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags);
  55. /*
  56. * Enable queues.
  57. */
  58. rt2x00queue_start_queues(rt2x00dev);
  59. rt2x00link_start_tuner(rt2x00dev);
  60. rt2x00link_start_agc(rt2x00dev);
  61. /*
  62. * Start watchdog monitoring.
  63. */
  64. rt2x00link_start_watchdog(rt2x00dev);
  65. return 0;
  66. }
  67. void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
  68. {
  69. if (!test_and_clear_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  70. return;
  71. /*
  72. * Stop watchdog monitoring.
  73. */
  74. rt2x00link_stop_watchdog(rt2x00dev);
  75. /*
  76. * Stop all queues
  77. */
  78. rt2x00link_stop_agc(rt2x00dev);
  79. rt2x00link_stop_tuner(rt2x00dev);
  80. rt2x00queue_stop_queues(rt2x00dev);
  81. rt2x00queue_flush_queues(rt2x00dev, true);
  82. /*
  83. * Disable radio.
  84. */
  85. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
  86. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_OFF);
  87. rt2x00led_led_activity(rt2x00dev, false);
  88. rt2x00leds_led_radio(rt2x00dev, false);
  89. }
  90. static void rt2x00lib_intf_scheduled_iter(void *data, u8 *mac,
  91. struct ieee80211_vif *vif)
  92. {
  93. struct rt2x00_dev *rt2x00dev = data;
  94. struct rt2x00_intf *intf = vif_to_intf(vif);
  95. /*
  96. * It is possible the radio was disabled while the work had been
  97. * scheduled. If that happens we should return here immediately,
  98. * note that in the spinlock protected area above the delayed_flags
  99. * have been cleared correctly.
  100. */
  101. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  102. return;
  103. if (test_and_clear_bit(DELAYED_UPDATE_BEACON, &intf->delayed_flags))
  104. rt2x00queue_update_beacon(rt2x00dev, vif);
  105. }
  106. static void rt2x00lib_intf_scheduled(struct work_struct *work)
  107. {
  108. struct rt2x00_dev *rt2x00dev =
  109. container_of(work, struct rt2x00_dev, intf_work);
  110. /*
  111. * Iterate over each interface and perform the
  112. * requested configurations.
  113. */
  114. ieee80211_iterate_active_interfaces(rt2x00dev->hw,
  115. rt2x00lib_intf_scheduled_iter,
  116. rt2x00dev);
  117. }
  118. static void rt2x00lib_autowakeup(struct work_struct *work)
  119. {
  120. struct rt2x00_dev *rt2x00dev =
  121. container_of(work, struct rt2x00_dev, autowakeup_work.work);
  122. if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_AWAKE))
  123. ERROR(rt2x00dev, "Device failed to wakeup.\n");
  124. clear_bit(CONFIG_POWERSAVING, &rt2x00dev->flags);
  125. }
  126. /*
  127. * Interrupt context handlers.
  128. */
  129. static void rt2x00lib_bc_buffer_iter(void *data, u8 *mac,
  130. struct ieee80211_vif *vif)
  131. {
  132. struct rt2x00_dev *rt2x00dev = data;
  133. struct sk_buff *skb;
  134. /*
  135. * Only AP mode interfaces do broad- and multicast buffering
  136. */
  137. if (vif->type != NL80211_IFTYPE_AP)
  138. return;
  139. /*
  140. * Send out buffered broad- and multicast frames
  141. */
  142. skb = ieee80211_get_buffered_bc(rt2x00dev->hw, vif);
  143. while (skb) {
  144. rt2x00mac_tx(rt2x00dev->hw, skb);
  145. skb = ieee80211_get_buffered_bc(rt2x00dev->hw, vif);
  146. }
  147. }
  148. static void rt2x00lib_beaconupdate_iter(void *data, u8 *mac,
  149. struct ieee80211_vif *vif)
  150. {
  151. struct rt2x00_dev *rt2x00dev = data;
  152. if (vif->type != NL80211_IFTYPE_AP &&
  153. vif->type != NL80211_IFTYPE_ADHOC &&
  154. vif->type != NL80211_IFTYPE_MESH_POINT &&
  155. vif->type != NL80211_IFTYPE_WDS)
  156. return;
  157. /*
  158. * Update the beacon without locking. This is safe on PCI devices
  159. * as they only update the beacon periodically here. This should
  160. * never be called for USB devices.
  161. */
  162. WARN_ON(rt2x00_is_usb(rt2x00dev));
  163. rt2x00queue_update_beacon_locked(rt2x00dev, vif);
  164. }
  165. void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
  166. {
  167. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  168. return;
  169. /* send buffered bc/mc frames out for every bssid */
  170. ieee80211_iterate_active_interfaces_atomic(rt2x00dev->hw,
  171. rt2x00lib_bc_buffer_iter,
  172. rt2x00dev);
  173. /*
  174. * Devices with pre tbtt interrupt don't need to update the beacon
  175. * here as they will fetch the next beacon directly prior to
  176. * transmission.
  177. */
  178. if (test_bit(CAPABILITY_PRE_TBTT_INTERRUPT, &rt2x00dev->cap_flags))
  179. return;
  180. /* fetch next beacon */
  181. ieee80211_iterate_active_interfaces_atomic(rt2x00dev->hw,
  182. rt2x00lib_beaconupdate_iter,
  183. rt2x00dev);
  184. }
  185. EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
  186. void rt2x00lib_pretbtt(struct rt2x00_dev *rt2x00dev)
  187. {
  188. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  189. return;
  190. /* fetch next beacon */
  191. ieee80211_iterate_active_interfaces_atomic(rt2x00dev->hw,
  192. rt2x00lib_beaconupdate_iter,
  193. rt2x00dev);
  194. }
  195. EXPORT_SYMBOL_GPL(rt2x00lib_pretbtt);
  196. void rt2x00lib_dmastart(struct queue_entry *entry)
  197. {
  198. set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  199. rt2x00queue_index_inc(entry, Q_INDEX);
  200. }
  201. EXPORT_SYMBOL_GPL(rt2x00lib_dmastart);
  202. void rt2x00lib_dmadone(struct queue_entry *entry)
  203. {
  204. set_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags);
  205. clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  206. rt2x00queue_index_inc(entry, Q_INDEX_DMA_DONE);
  207. }
  208. EXPORT_SYMBOL_GPL(rt2x00lib_dmadone);
  209. void rt2x00lib_txdone(struct queue_entry *entry,
  210. struct txdone_entry_desc *txdesc)
  211. {
  212. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  213. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
  214. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  215. unsigned int header_length, i;
  216. u8 rate_idx, rate_flags, retry_rates;
  217. u8 skbdesc_flags = skbdesc->flags;
  218. bool success;
  219. /*
  220. * Unmap the skb.
  221. */
  222. rt2x00queue_unmap_skb(entry);
  223. /*
  224. * Remove the extra tx headroom from the skb.
  225. */
  226. skb_pull(entry->skb, rt2x00dev->ops->extra_tx_headroom);
  227. /*
  228. * Signal that the TX descriptor is no longer in the skb.
  229. */
  230. skbdesc->flags &= ~SKBDESC_DESC_IN_SKB;
  231. /*
  232. * Determine the length of 802.11 header.
  233. */
  234. header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
  235. /*
  236. * Remove L2 padding which was added during
  237. */
  238. if (test_bit(REQUIRE_L2PAD, &rt2x00dev->cap_flags))
  239. rt2x00queue_remove_l2pad(entry->skb, header_length);
  240. /*
  241. * If the IV/EIV data was stripped from the frame before it was
  242. * passed to the hardware, we should now reinsert it again because
  243. * mac80211 will expect the same data to be present it the
  244. * frame as it was passed to us.
  245. */
  246. if (test_bit(CAPABILITY_HW_CRYPTO, &rt2x00dev->cap_flags))
  247. rt2x00crypto_tx_insert_iv(entry->skb, header_length);
  248. /*
  249. * Send frame to debugfs immediately, after this call is completed
  250. * we are going to overwrite the skb->cb array.
  251. */
  252. rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TXDONE, entry->skb);
  253. /*
  254. * Determine if the frame has been successfully transmitted.
  255. */
  256. success =
  257. test_bit(TXDONE_SUCCESS, &txdesc->flags) ||
  258. test_bit(TXDONE_UNKNOWN, &txdesc->flags);
  259. /*
  260. * Update TX statistics.
  261. */
  262. rt2x00dev->link.qual.tx_success += success;
  263. rt2x00dev->link.qual.tx_failed += !success;
  264. rate_idx = skbdesc->tx_rate_idx;
  265. rate_flags = skbdesc->tx_rate_flags;
  266. retry_rates = test_bit(TXDONE_FALLBACK, &txdesc->flags) ?
  267. (txdesc->retry + 1) : 1;
  268. /*
  269. * Initialize TX status
  270. */
  271. memset(&tx_info->status, 0, sizeof(tx_info->status));
  272. tx_info->status.ack_signal = 0;
  273. /*
  274. * Frame was send with retries, hardware tried
  275. * different rates to send out the frame, at each
  276. * retry it lowered the rate 1 step except when the
  277. * lowest rate was used.
  278. */
  279. for (i = 0; i < retry_rates && i < IEEE80211_TX_MAX_RATES; i++) {
  280. tx_info->status.rates[i].idx = rate_idx - i;
  281. tx_info->status.rates[i].flags = rate_flags;
  282. if (rate_idx - i == 0) {
  283. /*
  284. * The lowest rate (index 0) was used until the
  285. * number of max retries was reached.
  286. */
  287. tx_info->status.rates[i].count = retry_rates - i;
  288. i++;
  289. break;
  290. }
  291. tx_info->status.rates[i].count = 1;
  292. }
  293. if (i < (IEEE80211_TX_MAX_RATES - 1))
  294. tx_info->status.rates[i].idx = -1; /* terminate */
  295. if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  296. if (success)
  297. tx_info->flags |= IEEE80211_TX_STAT_ACK;
  298. else
  299. rt2x00dev->low_level_stats.dot11ACKFailureCount++;
  300. }
  301. /*
  302. * Every single frame has it's own tx status, hence report
  303. * every frame as ampdu of size 1.
  304. *
  305. * TODO: if we can find out how many frames were aggregated
  306. * by the hw we could provide the real ampdu_len to mac80211
  307. * which would allow the rc algorithm to better decide on
  308. * which rates are suitable.
  309. */
  310. if (test_bit(TXDONE_AMPDU, &txdesc->flags) ||
  311. tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
  312. tx_info->flags |= IEEE80211_TX_STAT_AMPDU;
  313. tx_info->status.ampdu_len = 1;
  314. tx_info->status.ampdu_ack_len = success ? 1 : 0;
  315. if (!success)
  316. tx_info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
  317. }
  318. if (rate_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  319. if (success)
  320. rt2x00dev->low_level_stats.dot11RTSSuccessCount++;
  321. else
  322. rt2x00dev->low_level_stats.dot11RTSFailureCount++;
  323. }
  324. /*
  325. * Only send the status report to mac80211 when it's a frame
  326. * that originated in mac80211. If this was a extra frame coming
  327. * through a mac80211 library call (RTS/CTS) then we should not
  328. * send the status report back.
  329. */
  330. if (!(skbdesc_flags & SKBDESC_NOT_MAC80211)) {
  331. if (test_bit(REQUIRE_TASKLET_CONTEXT, &rt2x00dev->cap_flags))
  332. ieee80211_tx_status(rt2x00dev->hw, entry->skb);
  333. else
  334. ieee80211_tx_status_ni(rt2x00dev->hw, entry->skb);
  335. } else
  336. dev_kfree_skb_any(entry->skb);
  337. /*
  338. * Make this entry available for reuse.
  339. */
  340. entry->skb = NULL;
  341. entry->flags = 0;
  342. rt2x00dev->ops->lib->clear_entry(entry);
  343. rt2x00queue_index_inc(entry, Q_INDEX_DONE);
  344. /*
  345. * If the data queue was below the threshold before the txdone
  346. * handler we must make sure the packet queue in the mac80211 stack
  347. * is reenabled when the txdone handler has finished.
  348. */
  349. if (!rt2x00queue_threshold(entry->queue))
  350. rt2x00queue_unpause_queue(entry->queue);
  351. }
  352. EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
  353. void rt2x00lib_txdone_noinfo(struct queue_entry *entry, u32 status)
  354. {
  355. struct txdone_entry_desc txdesc;
  356. txdesc.flags = 0;
  357. __set_bit(status, &txdesc.flags);
  358. txdesc.retry = 0;
  359. rt2x00lib_txdone(entry, &txdesc);
  360. }
  361. EXPORT_SYMBOL_GPL(rt2x00lib_txdone_noinfo);
  362. static u8 *rt2x00lib_find_ie(u8 *data, unsigned int len, u8 ie)
  363. {
  364. struct ieee80211_mgmt *mgmt = (void *)data;
  365. u8 *pos, *end;
  366. pos = (u8 *)mgmt->u.beacon.variable;
  367. end = data + len;
  368. while (pos < end) {
  369. if (pos + 2 + pos[1] > end)
  370. return NULL;
  371. if (pos[0] == ie)
  372. return pos;
  373. pos += 2 + pos[1];
  374. }
  375. return NULL;
  376. }
  377. static void rt2x00lib_rxdone_check_ps(struct rt2x00_dev *rt2x00dev,
  378. struct sk_buff *skb,
  379. struct rxdone_entry_desc *rxdesc)
  380. {
  381. struct ieee80211_hdr *hdr = (void *) skb->data;
  382. struct ieee80211_tim_ie *tim_ie;
  383. u8 *tim;
  384. u8 tim_len;
  385. bool cam;
  386. /* If this is not a beacon, or if mac80211 has no powersaving
  387. * configured, or if the device is already in powersaving mode
  388. * we can exit now. */
  389. if (likely(!ieee80211_is_beacon(hdr->frame_control) ||
  390. !(rt2x00dev->hw->conf.flags & IEEE80211_CONF_PS)))
  391. return;
  392. /* min. beacon length + FCS_LEN */
  393. if (skb->len <= 40 + FCS_LEN)
  394. return;
  395. /* and only beacons from the associated BSSID, please */
  396. if (!(rxdesc->dev_flags & RXDONE_MY_BSS) ||
  397. !rt2x00dev->aid)
  398. return;
  399. rt2x00dev->last_beacon = jiffies;
  400. tim = rt2x00lib_find_ie(skb->data, skb->len - FCS_LEN, WLAN_EID_TIM);
  401. if (!tim)
  402. return;
  403. if (tim[1] < sizeof(*tim_ie))
  404. return;
  405. tim_len = tim[1];
  406. tim_ie = (struct ieee80211_tim_ie *) &tim[2];
  407. /* Check whenever the PHY can be turned off again. */
  408. /* 1. What about buffered unicast traffic for our AID? */
  409. cam = ieee80211_check_tim(tim_ie, tim_len, rt2x00dev->aid);
  410. /* 2. Maybe the AP wants to send multicast/broadcast data? */
  411. cam |= (tim_ie->bitmap_ctrl & 0x01);
  412. if (!cam && !test_bit(CONFIG_POWERSAVING, &rt2x00dev->flags))
  413. rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf,
  414. IEEE80211_CONF_CHANGE_PS);
  415. }
  416. static int rt2x00lib_rxdone_read_signal(struct rt2x00_dev *rt2x00dev,
  417. struct rxdone_entry_desc *rxdesc)
  418. {
  419. struct ieee80211_supported_band *sband;
  420. const struct rt2x00_rate *rate;
  421. unsigned int i;
  422. int signal = rxdesc->signal;
  423. int type = (rxdesc->dev_flags & RXDONE_SIGNAL_MASK);
  424. switch (rxdesc->rate_mode) {
  425. case RATE_MODE_CCK:
  426. case RATE_MODE_OFDM:
  427. /*
  428. * For non-HT rates the MCS value needs to contain the
  429. * actually used rate modulation (CCK or OFDM).
  430. */
  431. if (rxdesc->dev_flags & RXDONE_SIGNAL_MCS)
  432. signal = RATE_MCS(rxdesc->rate_mode, signal);
  433. sband = &rt2x00dev->bands[rt2x00dev->curr_band];
  434. for (i = 0; i < sband->n_bitrates; i++) {
  435. rate = rt2x00_get_rate(sband->bitrates[i].hw_value);
  436. if (((type == RXDONE_SIGNAL_PLCP) &&
  437. (rate->plcp == signal)) ||
  438. ((type == RXDONE_SIGNAL_BITRATE) &&
  439. (rate->bitrate == signal)) ||
  440. ((type == RXDONE_SIGNAL_MCS) &&
  441. (rate->mcs == signal))) {
  442. return i;
  443. }
  444. }
  445. break;
  446. case RATE_MODE_HT_MIX:
  447. case RATE_MODE_HT_GREENFIELD:
  448. if (signal >= 0 && signal <= 76)
  449. return signal;
  450. break;
  451. default:
  452. break;
  453. }
  454. WARNING(rt2x00dev, "Frame received with unrecognized signal, "
  455. "mode=0x%.4x, signal=0x%.4x, type=%d.\n",
  456. rxdesc->rate_mode, signal, type);
  457. return 0;
  458. }
  459. void rt2x00lib_rxdone(struct queue_entry *entry)
  460. {
  461. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  462. struct rxdone_entry_desc rxdesc;
  463. struct sk_buff *skb;
  464. struct ieee80211_rx_status *rx_status;
  465. unsigned int header_length;
  466. int rate_idx;
  467. if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) ||
  468. !test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  469. goto submit_entry;
  470. if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags))
  471. goto submit_entry;
  472. /*
  473. * Allocate a new sk_buffer. If no new buffer available, drop the
  474. * received frame and reuse the existing buffer.
  475. */
  476. skb = rt2x00queue_alloc_rxskb(entry);
  477. if (!skb)
  478. goto submit_entry;
  479. /*
  480. * Unmap the skb.
  481. */
  482. rt2x00queue_unmap_skb(entry);
  483. /*
  484. * Extract the RXD details.
  485. */
  486. memset(&rxdesc, 0, sizeof(rxdesc));
  487. rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
  488. /*
  489. * The data behind the ieee80211 header must be
  490. * aligned on a 4 byte boundary.
  491. */
  492. header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
  493. /*
  494. * Hardware might have stripped the IV/EIV/ICV data,
  495. * in that case it is possible that the data was
  496. * provided separately (through hardware descriptor)
  497. * in which case we should reinsert the data into the frame.
  498. */
  499. if ((rxdesc.dev_flags & RXDONE_CRYPTO_IV) &&
  500. (rxdesc.flags & RX_FLAG_IV_STRIPPED))
  501. rt2x00crypto_rx_insert_iv(entry->skb, header_length,
  502. &rxdesc);
  503. else if (header_length &&
  504. (rxdesc.size > header_length) &&
  505. (rxdesc.dev_flags & RXDONE_L2PAD))
  506. rt2x00queue_remove_l2pad(entry->skb, header_length);
  507. /* Trim buffer to correct size */
  508. skb_trim(entry->skb, rxdesc.size);
  509. /*
  510. * Translate the signal to the correct bitrate index.
  511. */
  512. rate_idx = rt2x00lib_rxdone_read_signal(rt2x00dev, &rxdesc);
  513. if (rxdesc.rate_mode == RATE_MODE_HT_MIX ||
  514. rxdesc.rate_mode == RATE_MODE_HT_GREENFIELD)
  515. rxdesc.flags |= RX_FLAG_HT;
  516. /*
  517. * Check if this is a beacon, and more frames have been
  518. * buffered while we were in powersaving mode.
  519. */
  520. rt2x00lib_rxdone_check_ps(rt2x00dev, entry->skb, &rxdesc);
  521. /*
  522. * Update extra components
  523. */
  524. rt2x00link_update_stats(rt2x00dev, entry->skb, &rxdesc);
  525. rt2x00debug_update_crypto(rt2x00dev, &rxdesc);
  526. rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_RXDONE, entry->skb);
  527. /*
  528. * Initialize RX status information, and send frame
  529. * to mac80211.
  530. */
  531. rx_status = IEEE80211_SKB_RXCB(entry->skb);
  532. rx_status->mactime = rxdesc.timestamp;
  533. rx_status->band = rt2x00dev->curr_band;
  534. rx_status->freq = rt2x00dev->curr_freq;
  535. rx_status->rate_idx = rate_idx;
  536. rx_status->signal = rxdesc.rssi;
  537. rx_status->flag = rxdesc.flags;
  538. rx_status->antenna = rt2x00dev->link.ant.active.rx;
  539. ieee80211_rx_ni(rt2x00dev->hw, entry->skb);
  540. /*
  541. * Replace the skb with the freshly allocated one.
  542. */
  543. entry->skb = skb;
  544. submit_entry:
  545. entry->flags = 0;
  546. rt2x00queue_index_inc(entry, Q_INDEX_DONE);
  547. if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) &&
  548. test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  549. rt2x00dev->ops->lib->clear_entry(entry);
  550. }
  551. EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
  552. /*
  553. * Driver initialization handlers.
  554. */
  555. const struct rt2x00_rate rt2x00_supported_rates[12] = {
  556. {
  557. .flags = DEV_RATE_CCK,
  558. .bitrate = 10,
  559. .ratemask = BIT(0),
  560. .plcp = 0x00,
  561. .mcs = RATE_MCS(RATE_MODE_CCK, 0),
  562. },
  563. {
  564. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
  565. .bitrate = 20,
  566. .ratemask = BIT(1),
  567. .plcp = 0x01,
  568. .mcs = RATE_MCS(RATE_MODE_CCK, 1),
  569. },
  570. {
  571. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
  572. .bitrate = 55,
  573. .ratemask = BIT(2),
  574. .plcp = 0x02,
  575. .mcs = RATE_MCS(RATE_MODE_CCK, 2),
  576. },
  577. {
  578. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
  579. .bitrate = 110,
  580. .ratemask = BIT(3),
  581. .plcp = 0x03,
  582. .mcs = RATE_MCS(RATE_MODE_CCK, 3),
  583. },
  584. {
  585. .flags = DEV_RATE_OFDM,
  586. .bitrate = 60,
  587. .ratemask = BIT(4),
  588. .plcp = 0x0b,
  589. .mcs = RATE_MCS(RATE_MODE_OFDM, 0),
  590. },
  591. {
  592. .flags = DEV_RATE_OFDM,
  593. .bitrate = 90,
  594. .ratemask = BIT(5),
  595. .plcp = 0x0f,
  596. .mcs = RATE_MCS(RATE_MODE_OFDM, 1),
  597. },
  598. {
  599. .flags = DEV_RATE_OFDM,
  600. .bitrate = 120,
  601. .ratemask = BIT(6),
  602. .plcp = 0x0a,
  603. .mcs = RATE_MCS(RATE_MODE_OFDM, 2),
  604. },
  605. {
  606. .flags = DEV_RATE_OFDM,
  607. .bitrate = 180,
  608. .ratemask = BIT(7),
  609. .plcp = 0x0e,
  610. .mcs = RATE_MCS(RATE_MODE_OFDM, 3),
  611. },
  612. {
  613. .flags = DEV_RATE_OFDM,
  614. .bitrate = 240,
  615. .ratemask = BIT(8),
  616. .plcp = 0x09,
  617. .mcs = RATE_MCS(RATE_MODE_OFDM, 4),
  618. },
  619. {
  620. .flags = DEV_RATE_OFDM,
  621. .bitrate = 360,
  622. .ratemask = BIT(9),
  623. .plcp = 0x0d,
  624. .mcs = RATE_MCS(RATE_MODE_OFDM, 5),
  625. },
  626. {
  627. .flags = DEV_RATE_OFDM,
  628. .bitrate = 480,
  629. .ratemask = BIT(10),
  630. .plcp = 0x08,
  631. .mcs = RATE_MCS(RATE_MODE_OFDM, 6),
  632. },
  633. {
  634. .flags = DEV_RATE_OFDM,
  635. .bitrate = 540,
  636. .ratemask = BIT(11),
  637. .plcp = 0x0c,
  638. .mcs = RATE_MCS(RATE_MODE_OFDM, 7),
  639. },
  640. };
  641. static void rt2x00lib_channel(struct ieee80211_channel *entry,
  642. const int channel, const int tx_power,
  643. const int value)
  644. {
  645. /* XXX: this assumption about the band is wrong for 802.11j */
  646. entry->band = channel <= 14 ? IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
  647. entry->center_freq = ieee80211_channel_to_frequency(channel,
  648. entry->band);
  649. entry->hw_value = value;
  650. entry->max_power = tx_power;
  651. entry->max_antenna_gain = 0xff;
  652. }
  653. static void rt2x00lib_rate(struct ieee80211_rate *entry,
  654. const u16 index, const struct rt2x00_rate *rate)
  655. {
  656. entry->flags = 0;
  657. entry->bitrate = rate->bitrate;
  658. entry->hw_value = index;
  659. entry->hw_value_short = index;
  660. if (rate->flags & DEV_RATE_SHORT_PREAMBLE)
  661. entry->flags |= IEEE80211_RATE_SHORT_PREAMBLE;
  662. }
  663. static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
  664. struct hw_mode_spec *spec)
  665. {
  666. struct ieee80211_hw *hw = rt2x00dev->hw;
  667. struct ieee80211_channel *channels;
  668. struct ieee80211_rate *rates;
  669. unsigned int num_rates;
  670. unsigned int i;
  671. num_rates = 0;
  672. if (spec->supported_rates & SUPPORT_RATE_CCK)
  673. num_rates += 4;
  674. if (spec->supported_rates & SUPPORT_RATE_OFDM)
  675. num_rates += 8;
  676. channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
  677. if (!channels)
  678. return -ENOMEM;
  679. rates = kzalloc(sizeof(*rates) * num_rates, GFP_KERNEL);
  680. if (!rates)
  681. goto exit_free_channels;
  682. /*
  683. * Initialize Rate list.
  684. */
  685. for (i = 0; i < num_rates; i++)
  686. rt2x00lib_rate(&rates[i], i, rt2x00_get_rate(i));
  687. /*
  688. * Initialize Channel list.
  689. */
  690. for (i = 0; i < spec->num_channels; i++) {
  691. rt2x00lib_channel(&channels[i],
  692. spec->channels[i].channel,
  693. spec->channels_info[i].max_power, i);
  694. }
  695. /*
  696. * Intitialize 802.11b, 802.11g
  697. * Rates: CCK, OFDM.
  698. * Channels: 2.4 GHz
  699. */
  700. if (spec->supported_bands & SUPPORT_BAND_2GHZ) {
  701. rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_channels = 14;
  702. rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_bitrates = num_rates;
  703. rt2x00dev->bands[IEEE80211_BAND_2GHZ].channels = channels;
  704. rt2x00dev->bands[IEEE80211_BAND_2GHZ].bitrates = rates;
  705. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  706. &rt2x00dev->bands[IEEE80211_BAND_2GHZ];
  707. memcpy(&rt2x00dev->bands[IEEE80211_BAND_2GHZ].ht_cap,
  708. &spec->ht, sizeof(spec->ht));
  709. }
  710. /*
  711. * Intitialize 802.11a
  712. * Rates: OFDM.
  713. * Channels: OFDM, UNII, HiperLAN2.
  714. */
  715. if (spec->supported_bands & SUPPORT_BAND_5GHZ) {
  716. rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_channels =
  717. spec->num_channels - 14;
  718. rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_bitrates =
  719. num_rates - 4;
  720. rt2x00dev->bands[IEEE80211_BAND_5GHZ].channels = &channels[14];
  721. rt2x00dev->bands[IEEE80211_BAND_5GHZ].bitrates = &rates[4];
  722. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  723. &rt2x00dev->bands[IEEE80211_BAND_5GHZ];
  724. memcpy(&rt2x00dev->bands[IEEE80211_BAND_5GHZ].ht_cap,
  725. &spec->ht, sizeof(spec->ht));
  726. }
  727. return 0;
  728. exit_free_channels:
  729. kfree(channels);
  730. ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
  731. return -ENOMEM;
  732. }
  733. static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
  734. {
  735. if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
  736. ieee80211_unregister_hw(rt2x00dev->hw);
  737. if (likely(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ])) {
  738. kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->channels);
  739. kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->bitrates);
  740. rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
  741. rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
  742. }
  743. kfree(rt2x00dev->spec.channels_info);
  744. }
  745. static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
  746. {
  747. struct hw_mode_spec *spec = &rt2x00dev->spec;
  748. int status;
  749. if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
  750. return 0;
  751. /*
  752. * Initialize HW modes.
  753. */
  754. status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
  755. if (status)
  756. return status;
  757. /*
  758. * Initialize HW fields.
  759. */
  760. rt2x00dev->hw->queues = rt2x00dev->ops->tx_queues;
  761. /*
  762. * Initialize extra TX headroom required.
  763. */
  764. rt2x00dev->hw->extra_tx_headroom =
  765. max_t(unsigned int, IEEE80211_TX_STATUS_HEADROOM,
  766. rt2x00dev->ops->extra_tx_headroom);
  767. /*
  768. * Take TX headroom required for alignment into account.
  769. */
  770. if (test_bit(REQUIRE_L2PAD, &rt2x00dev->cap_flags))
  771. rt2x00dev->hw->extra_tx_headroom += RT2X00_L2PAD_SIZE;
  772. else if (test_bit(REQUIRE_DMA, &rt2x00dev->cap_flags))
  773. rt2x00dev->hw->extra_tx_headroom += RT2X00_ALIGN_SIZE;
  774. /*
  775. * Allocate tx status FIFO for driver use.
  776. */
  777. if (test_bit(REQUIRE_TXSTATUS_FIFO, &rt2x00dev->cap_flags)) {
  778. /*
  779. * Allocate the txstatus fifo. In the worst case the tx
  780. * status fifo has to hold the tx status of all entries
  781. * in all tx queues. Hence, calculate the kfifo size as
  782. * tx_queues * entry_num and round up to the nearest
  783. * power of 2.
  784. */
  785. int kfifo_size =
  786. roundup_pow_of_two(rt2x00dev->ops->tx_queues *
  787. rt2x00dev->ops->tx->entry_num *
  788. sizeof(u32));
  789. status = kfifo_alloc(&rt2x00dev->txstatus_fifo, kfifo_size,
  790. GFP_KERNEL);
  791. if (status)
  792. return status;
  793. }
  794. /*
  795. * Initialize tasklets if used by the driver. Tasklets are
  796. * disabled until the interrupts are turned on. The driver
  797. * has to handle that.
  798. */
  799. #define RT2X00_TASKLET_INIT(taskletname) \
  800. if (rt2x00dev->ops->lib->taskletname) { \
  801. tasklet_init(&rt2x00dev->taskletname, \
  802. rt2x00dev->ops->lib->taskletname, \
  803. (unsigned long)rt2x00dev); \
  804. tasklet_disable(&rt2x00dev->taskletname); \
  805. }
  806. RT2X00_TASKLET_INIT(txstatus_tasklet);
  807. RT2X00_TASKLET_INIT(pretbtt_tasklet);
  808. RT2X00_TASKLET_INIT(tbtt_tasklet);
  809. RT2X00_TASKLET_INIT(rxdone_tasklet);
  810. RT2X00_TASKLET_INIT(autowake_tasklet);
  811. #undef RT2X00_TASKLET_INIT
  812. /*
  813. * Register HW.
  814. */
  815. status = ieee80211_register_hw(rt2x00dev->hw);
  816. if (status)
  817. return status;
  818. set_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags);
  819. return 0;
  820. }
  821. /*
  822. * Initialization/uninitialization handlers.
  823. */
  824. static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
  825. {
  826. if (!test_and_clear_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
  827. return;
  828. /*
  829. * Unregister extra components.
  830. */
  831. rt2x00rfkill_unregister(rt2x00dev);
  832. /*
  833. * Allow the HW to uninitialize.
  834. */
  835. rt2x00dev->ops->lib->uninitialize(rt2x00dev);
  836. /*
  837. * Free allocated queue entries.
  838. */
  839. rt2x00queue_uninitialize(rt2x00dev);
  840. }
  841. static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
  842. {
  843. int status;
  844. if (test_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
  845. return 0;
  846. /*
  847. * Allocate all queue entries.
  848. */
  849. status = rt2x00queue_initialize(rt2x00dev);
  850. if (status)
  851. return status;
  852. /*
  853. * Initialize the device.
  854. */
  855. status = rt2x00dev->ops->lib->initialize(rt2x00dev);
  856. if (status) {
  857. rt2x00queue_uninitialize(rt2x00dev);
  858. return status;
  859. }
  860. set_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags);
  861. /*
  862. * Register the extra components.
  863. */
  864. rt2x00rfkill_register(rt2x00dev);
  865. return 0;
  866. }
  867. int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
  868. {
  869. int retval;
  870. if (test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
  871. return 0;
  872. /*
  873. * If this is the first interface which is added,
  874. * we should load the firmware now.
  875. */
  876. retval = rt2x00lib_load_firmware(rt2x00dev);
  877. if (retval)
  878. return retval;
  879. /*
  880. * Initialize the device.
  881. */
  882. retval = rt2x00lib_initialize(rt2x00dev);
  883. if (retval)
  884. return retval;
  885. rt2x00dev->intf_ap_count = 0;
  886. rt2x00dev->intf_sta_count = 0;
  887. rt2x00dev->intf_associated = 0;
  888. /* Enable the radio */
  889. retval = rt2x00lib_enable_radio(rt2x00dev);
  890. if (retval)
  891. return retval;
  892. set_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags);
  893. return 0;
  894. }
  895. void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
  896. {
  897. if (!test_and_clear_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
  898. return;
  899. /*
  900. * Perhaps we can add something smarter here,
  901. * but for now just disabling the radio should do.
  902. */
  903. rt2x00lib_disable_radio(rt2x00dev);
  904. rt2x00dev->intf_ap_count = 0;
  905. rt2x00dev->intf_sta_count = 0;
  906. rt2x00dev->intf_associated = 0;
  907. }
  908. /*
  909. * driver allocation handlers.
  910. */
  911. int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
  912. {
  913. int retval = -ENOMEM;
  914. spin_lock_init(&rt2x00dev->irqmask_lock);
  915. mutex_init(&rt2x00dev->csr_mutex);
  916. set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  917. /*
  918. * Make room for rt2x00_intf inside the per-interface
  919. * structure ieee80211_vif.
  920. */
  921. rt2x00dev->hw->vif_data_size = sizeof(struct rt2x00_intf);
  922. /*
  923. * Determine which operating modes are supported, all modes
  924. * which require beaconing, depend on the availability of
  925. * beacon entries.
  926. */
  927. rt2x00dev->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  928. if (rt2x00dev->ops->bcn->entry_num > 0)
  929. rt2x00dev->hw->wiphy->interface_modes |=
  930. BIT(NL80211_IFTYPE_ADHOC) |
  931. BIT(NL80211_IFTYPE_AP) |
  932. BIT(NL80211_IFTYPE_MESH_POINT) |
  933. BIT(NL80211_IFTYPE_WDS);
  934. /*
  935. * Initialize work.
  936. */
  937. rt2x00dev->workqueue =
  938. alloc_ordered_workqueue(wiphy_name(rt2x00dev->hw->wiphy), 0);
  939. if (!rt2x00dev->workqueue) {
  940. retval = -ENOMEM;
  941. goto exit;
  942. }
  943. INIT_WORK(&rt2x00dev->intf_work, rt2x00lib_intf_scheduled);
  944. INIT_DELAYED_WORK(&rt2x00dev->autowakeup_work, rt2x00lib_autowakeup);
  945. /*
  946. * Let the driver probe the device to detect the capabilities.
  947. */
  948. retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
  949. if (retval) {
  950. ERROR(rt2x00dev, "Failed to allocate device.\n");
  951. goto exit;
  952. }
  953. /*
  954. * Allocate queue array.
  955. */
  956. retval = rt2x00queue_allocate(rt2x00dev);
  957. if (retval)
  958. goto exit;
  959. /*
  960. * Initialize ieee80211 structure.
  961. */
  962. retval = rt2x00lib_probe_hw(rt2x00dev);
  963. if (retval) {
  964. ERROR(rt2x00dev, "Failed to initialize hw.\n");
  965. goto exit;
  966. }
  967. /*
  968. * Register extra components.
  969. */
  970. rt2x00link_register(rt2x00dev);
  971. rt2x00leds_register(rt2x00dev);
  972. rt2x00debug_register(rt2x00dev);
  973. return 0;
  974. exit:
  975. rt2x00lib_remove_dev(rt2x00dev);
  976. return retval;
  977. }
  978. EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
  979. void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
  980. {
  981. clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  982. /*
  983. * Disable radio.
  984. */
  985. rt2x00lib_disable_radio(rt2x00dev);
  986. /*
  987. * Stop all work.
  988. */
  989. cancel_work_sync(&rt2x00dev->intf_work);
  990. if (rt2x00_is_usb(rt2x00dev)) {
  991. del_timer_sync(&rt2x00dev->txstatus_timer);
  992. cancel_work_sync(&rt2x00dev->rxdone_work);
  993. cancel_work_sync(&rt2x00dev->txdone_work);
  994. }
  995. destroy_workqueue(rt2x00dev->workqueue);
  996. /*
  997. * Free the tx status fifo.
  998. */
  999. kfifo_free(&rt2x00dev->txstatus_fifo);
  1000. /*
  1001. * Kill the tx status tasklet.
  1002. */
  1003. tasklet_kill(&rt2x00dev->txstatus_tasklet);
  1004. tasklet_kill(&rt2x00dev->pretbtt_tasklet);
  1005. tasklet_kill(&rt2x00dev->tbtt_tasklet);
  1006. tasklet_kill(&rt2x00dev->rxdone_tasklet);
  1007. tasklet_kill(&rt2x00dev->autowake_tasklet);
  1008. /*
  1009. * Uninitialize device.
  1010. */
  1011. rt2x00lib_uninitialize(rt2x00dev);
  1012. /*
  1013. * Free extra components
  1014. */
  1015. rt2x00debug_deregister(rt2x00dev);
  1016. rt2x00leds_unregister(rt2x00dev);
  1017. /*
  1018. * Free ieee80211_hw memory.
  1019. */
  1020. rt2x00lib_remove_hw(rt2x00dev);
  1021. /*
  1022. * Free firmware image.
  1023. */
  1024. rt2x00lib_free_firmware(rt2x00dev);
  1025. /*
  1026. * Free queue structures.
  1027. */
  1028. rt2x00queue_free(rt2x00dev);
  1029. }
  1030. EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
  1031. /*
  1032. * Device state handlers
  1033. */
  1034. #ifdef CONFIG_PM
  1035. int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
  1036. {
  1037. NOTICE(rt2x00dev, "Going to sleep.\n");
  1038. /*
  1039. * Prevent mac80211 from accessing driver while suspended.
  1040. */
  1041. if (!test_and_clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  1042. return 0;
  1043. /*
  1044. * Cleanup as much as possible.
  1045. */
  1046. rt2x00lib_uninitialize(rt2x00dev);
  1047. /*
  1048. * Suspend/disable extra components.
  1049. */
  1050. rt2x00leds_suspend(rt2x00dev);
  1051. rt2x00debug_deregister(rt2x00dev);
  1052. /*
  1053. * Set device mode to sleep for power management,
  1054. * on some hardware this call seems to consistently fail.
  1055. * From the specifications it is hard to tell why it fails,
  1056. * and if this is a "bad thing".
  1057. * Overall it is safe to just ignore the failure and
  1058. * continue suspending. The only downside is that the
  1059. * device will not be in optimal power save mode, but with
  1060. * the radio and the other components already disabled the
  1061. * device is as good as disabled.
  1062. */
  1063. if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP))
  1064. WARNING(rt2x00dev, "Device failed to enter sleep state, "
  1065. "continue suspending.\n");
  1066. return 0;
  1067. }
  1068. EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
  1069. int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
  1070. {
  1071. NOTICE(rt2x00dev, "Waking up.\n");
  1072. /*
  1073. * Restore/enable extra components.
  1074. */
  1075. rt2x00debug_register(rt2x00dev);
  1076. rt2x00leds_resume(rt2x00dev);
  1077. /*
  1078. * We are ready again to receive requests from mac80211.
  1079. */
  1080. set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  1081. return 0;
  1082. }
  1083. EXPORT_SYMBOL_GPL(rt2x00lib_resume);
  1084. #endif /* CONFIG_PM */
  1085. /*
  1086. * rt2x00lib module information.
  1087. */
  1088. MODULE_AUTHOR(DRV_PROJECT);
  1089. MODULE_VERSION(DRV_VERSION);
  1090. MODULE_DESCRIPTION("rt2x00 library");
  1091. MODULE_LICENSE("GPL");