rt2x00dev.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  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. }
  819. RT2X00_TASKLET_INIT(txstatus_tasklet);
  820. RT2X00_TASKLET_INIT(pretbtt_tasklet);
  821. RT2X00_TASKLET_INIT(tbtt_tasklet);
  822. RT2X00_TASKLET_INIT(rxdone_tasklet);
  823. RT2X00_TASKLET_INIT(autowake_tasklet);
  824. #undef RT2X00_TASKLET_INIT
  825. /*
  826. * Register HW.
  827. */
  828. status = ieee80211_register_hw(rt2x00dev->hw);
  829. if (status)
  830. return status;
  831. set_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags);
  832. return 0;
  833. }
  834. /*
  835. * Initialization/uninitialization handlers.
  836. */
  837. static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
  838. {
  839. if (!test_and_clear_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
  840. return;
  841. /*
  842. * Unregister extra components.
  843. */
  844. rt2x00rfkill_unregister(rt2x00dev);
  845. /*
  846. * Allow the HW to uninitialize.
  847. */
  848. rt2x00dev->ops->lib->uninitialize(rt2x00dev);
  849. /*
  850. * Free allocated queue entries.
  851. */
  852. rt2x00queue_uninitialize(rt2x00dev);
  853. }
  854. static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
  855. {
  856. int status;
  857. if (test_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
  858. return 0;
  859. /*
  860. * Allocate all queue entries.
  861. */
  862. status = rt2x00queue_initialize(rt2x00dev);
  863. if (status)
  864. return status;
  865. /*
  866. * Initialize the device.
  867. */
  868. status = rt2x00dev->ops->lib->initialize(rt2x00dev);
  869. if (status) {
  870. rt2x00queue_uninitialize(rt2x00dev);
  871. return status;
  872. }
  873. set_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags);
  874. /*
  875. * Register the extra components.
  876. */
  877. rt2x00rfkill_register(rt2x00dev);
  878. return 0;
  879. }
  880. int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
  881. {
  882. int retval;
  883. if (test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
  884. return 0;
  885. /*
  886. * If this is the first interface which is added,
  887. * we should load the firmware now.
  888. */
  889. retval = rt2x00lib_load_firmware(rt2x00dev);
  890. if (retval)
  891. return retval;
  892. /*
  893. * Initialize the device.
  894. */
  895. retval = rt2x00lib_initialize(rt2x00dev);
  896. if (retval)
  897. return retval;
  898. rt2x00dev->intf_ap_count = 0;
  899. rt2x00dev->intf_sta_count = 0;
  900. rt2x00dev->intf_associated = 0;
  901. /* Enable the radio */
  902. retval = rt2x00lib_enable_radio(rt2x00dev);
  903. if (retval)
  904. return retval;
  905. set_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags);
  906. return 0;
  907. }
  908. void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
  909. {
  910. if (!test_and_clear_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
  911. return;
  912. /*
  913. * Perhaps we can add something smarter here,
  914. * but for now just disabling the radio should do.
  915. */
  916. rt2x00lib_disable_radio(rt2x00dev);
  917. rt2x00dev->intf_ap_count = 0;
  918. rt2x00dev->intf_sta_count = 0;
  919. rt2x00dev->intf_associated = 0;
  920. }
  921. /*
  922. * driver allocation handlers.
  923. */
  924. int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
  925. {
  926. int retval = -ENOMEM;
  927. spin_lock_init(&rt2x00dev->irqmask_lock);
  928. mutex_init(&rt2x00dev->csr_mutex);
  929. set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  930. /*
  931. * Make room for rt2x00_intf inside the per-interface
  932. * structure ieee80211_vif.
  933. */
  934. rt2x00dev->hw->vif_data_size = sizeof(struct rt2x00_intf);
  935. /*
  936. * Determine which operating modes are supported, all modes
  937. * which require beaconing, depend on the availability of
  938. * beacon entries.
  939. */
  940. rt2x00dev->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  941. if (rt2x00dev->ops->bcn->entry_num > 0)
  942. rt2x00dev->hw->wiphy->interface_modes |=
  943. BIT(NL80211_IFTYPE_ADHOC) |
  944. BIT(NL80211_IFTYPE_AP) |
  945. BIT(NL80211_IFTYPE_MESH_POINT) |
  946. BIT(NL80211_IFTYPE_WDS);
  947. /*
  948. * Initialize work.
  949. */
  950. rt2x00dev->workqueue =
  951. alloc_ordered_workqueue(wiphy_name(rt2x00dev->hw->wiphy), 0);
  952. if (!rt2x00dev->workqueue) {
  953. retval = -ENOMEM;
  954. goto exit;
  955. }
  956. INIT_WORK(&rt2x00dev->intf_work, rt2x00lib_intf_scheduled);
  957. INIT_DELAYED_WORK(&rt2x00dev->autowakeup_work, rt2x00lib_autowakeup);
  958. /*
  959. * Let the driver probe the device to detect the capabilities.
  960. */
  961. retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
  962. if (retval) {
  963. ERROR(rt2x00dev, "Failed to allocate device.\n");
  964. goto exit;
  965. }
  966. /*
  967. * Allocate queue array.
  968. */
  969. retval = rt2x00queue_allocate(rt2x00dev);
  970. if (retval)
  971. goto exit;
  972. /*
  973. * Initialize ieee80211 structure.
  974. */
  975. retval = rt2x00lib_probe_hw(rt2x00dev);
  976. if (retval) {
  977. ERROR(rt2x00dev, "Failed to initialize hw.\n");
  978. goto exit;
  979. }
  980. /*
  981. * Register extra components.
  982. */
  983. rt2x00link_register(rt2x00dev);
  984. rt2x00leds_register(rt2x00dev);
  985. rt2x00debug_register(rt2x00dev);
  986. return 0;
  987. exit:
  988. rt2x00lib_remove_dev(rt2x00dev);
  989. return retval;
  990. }
  991. EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
  992. void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
  993. {
  994. clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  995. /*
  996. * Disable radio.
  997. */
  998. rt2x00lib_disable_radio(rt2x00dev);
  999. /*
  1000. * Stop all work.
  1001. */
  1002. cancel_work_sync(&rt2x00dev->intf_work);
  1003. cancel_delayed_work_sync(&rt2x00dev->autowakeup_work);
  1004. if (rt2x00_is_usb(rt2x00dev)) {
  1005. del_timer_sync(&rt2x00dev->txstatus_timer);
  1006. cancel_work_sync(&rt2x00dev->rxdone_work);
  1007. cancel_work_sync(&rt2x00dev->txdone_work);
  1008. }
  1009. destroy_workqueue(rt2x00dev->workqueue);
  1010. /*
  1011. * Free the tx status fifo.
  1012. */
  1013. kfifo_free(&rt2x00dev->txstatus_fifo);
  1014. /*
  1015. * Kill the tx status tasklet.
  1016. */
  1017. tasklet_kill(&rt2x00dev->txstatus_tasklet);
  1018. tasklet_kill(&rt2x00dev->pretbtt_tasklet);
  1019. tasklet_kill(&rt2x00dev->tbtt_tasklet);
  1020. tasklet_kill(&rt2x00dev->rxdone_tasklet);
  1021. tasklet_kill(&rt2x00dev->autowake_tasklet);
  1022. /*
  1023. * Uninitialize device.
  1024. */
  1025. rt2x00lib_uninitialize(rt2x00dev);
  1026. /*
  1027. * Free extra components
  1028. */
  1029. rt2x00debug_deregister(rt2x00dev);
  1030. rt2x00leds_unregister(rt2x00dev);
  1031. /*
  1032. * Free ieee80211_hw memory.
  1033. */
  1034. rt2x00lib_remove_hw(rt2x00dev);
  1035. /*
  1036. * Free firmware image.
  1037. */
  1038. rt2x00lib_free_firmware(rt2x00dev);
  1039. /*
  1040. * Free queue structures.
  1041. */
  1042. rt2x00queue_free(rt2x00dev);
  1043. }
  1044. EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
  1045. /*
  1046. * Device state handlers
  1047. */
  1048. #ifdef CONFIG_PM
  1049. int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
  1050. {
  1051. NOTICE(rt2x00dev, "Going to sleep.\n");
  1052. /*
  1053. * Prevent mac80211 from accessing driver while suspended.
  1054. */
  1055. if (!test_and_clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  1056. return 0;
  1057. /*
  1058. * Cleanup as much as possible.
  1059. */
  1060. rt2x00lib_uninitialize(rt2x00dev);
  1061. /*
  1062. * Suspend/disable extra components.
  1063. */
  1064. rt2x00leds_suspend(rt2x00dev);
  1065. rt2x00debug_deregister(rt2x00dev);
  1066. /*
  1067. * Set device mode to sleep for power management,
  1068. * on some hardware this call seems to consistently fail.
  1069. * From the specifications it is hard to tell why it fails,
  1070. * and if this is a "bad thing".
  1071. * Overall it is safe to just ignore the failure and
  1072. * continue suspending. The only downside is that the
  1073. * device will not be in optimal power save mode, but with
  1074. * the radio and the other components already disabled the
  1075. * device is as good as disabled.
  1076. */
  1077. if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP))
  1078. WARNING(rt2x00dev, "Device failed to enter sleep state, "
  1079. "continue suspending.\n");
  1080. return 0;
  1081. }
  1082. EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
  1083. int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
  1084. {
  1085. NOTICE(rt2x00dev, "Waking up.\n");
  1086. /*
  1087. * Restore/enable extra components.
  1088. */
  1089. rt2x00debug_register(rt2x00dev);
  1090. rt2x00leds_resume(rt2x00dev);
  1091. /*
  1092. * We are ready again to receive requests from mac80211.
  1093. */
  1094. set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  1095. return 0;
  1096. }
  1097. EXPORT_SYMBOL_GPL(rt2x00lib_resume);
  1098. #endif /* CONFIG_PM */
  1099. /*
  1100. * rt2x00lib module information.
  1101. */
  1102. MODULE_AUTHOR(DRV_PROJECT);
  1103. MODULE_VERSION(DRV_VERSION);
  1104. MODULE_DESCRIPTION("rt2x00 library");
  1105. MODULE_LICENSE("GPL");