rt2x00dev.c 33 KB

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