rt2x00dev.c 32 KB

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