rt2x00dev.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  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. This has to be
  365. * serialized with rt2x00mac_tx(), otherwise we can wake up queue
  366. * before it was stopped.
  367. */
  368. spin_lock_bh(&entry->queue->tx_lock);
  369. if (!rt2x00queue_threshold(entry->queue))
  370. rt2x00queue_unpause_queue(entry->queue);
  371. spin_unlock_bh(&entry->queue->tx_lock);
  372. }
  373. EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
  374. void rt2x00lib_txdone_noinfo(struct queue_entry *entry, u32 status)
  375. {
  376. struct txdone_entry_desc txdesc;
  377. txdesc.flags = 0;
  378. __set_bit(status, &txdesc.flags);
  379. txdesc.retry = 0;
  380. rt2x00lib_txdone(entry, &txdesc);
  381. }
  382. EXPORT_SYMBOL_GPL(rt2x00lib_txdone_noinfo);
  383. static u8 *rt2x00lib_find_ie(u8 *data, unsigned int len, u8 ie)
  384. {
  385. struct ieee80211_mgmt *mgmt = (void *)data;
  386. u8 *pos, *end;
  387. pos = (u8 *)mgmt->u.beacon.variable;
  388. end = data + len;
  389. while (pos < end) {
  390. if (pos + 2 + pos[1] > end)
  391. return NULL;
  392. if (pos[0] == ie)
  393. return pos;
  394. pos += 2 + pos[1];
  395. }
  396. return NULL;
  397. }
  398. static void rt2x00lib_sleep(struct work_struct *work)
  399. {
  400. struct rt2x00_dev *rt2x00dev =
  401. container_of(work, struct rt2x00_dev, sleep_work);
  402. if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  403. return;
  404. /*
  405. * Check again is powersaving is enabled, to prevent races from delayed
  406. * work execution.
  407. */
  408. if (!test_bit(CONFIG_POWERSAVING, &rt2x00dev->flags))
  409. rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf,
  410. IEEE80211_CONF_CHANGE_PS);
  411. }
  412. static void rt2x00lib_rxdone_check_ps(struct rt2x00_dev *rt2x00dev,
  413. struct sk_buff *skb,
  414. struct rxdone_entry_desc *rxdesc)
  415. {
  416. struct ieee80211_hdr *hdr = (void *) skb->data;
  417. struct ieee80211_tim_ie *tim_ie;
  418. u8 *tim;
  419. u8 tim_len;
  420. bool cam;
  421. /* If this is not a beacon, or if mac80211 has no powersaving
  422. * configured, or if the device is already in powersaving mode
  423. * we can exit now. */
  424. if (likely(!ieee80211_is_beacon(hdr->frame_control) ||
  425. !(rt2x00dev->hw->conf.flags & IEEE80211_CONF_PS)))
  426. return;
  427. /* min. beacon length + FCS_LEN */
  428. if (skb->len <= 40 + FCS_LEN)
  429. return;
  430. /* and only beacons from the associated BSSID, please */
  431. if (!(rxdesc->dev_flags & RXDONE_MY_BSS) ||
  432. !rt2x00dev->aid)
  433. return;
  434. rt2x00dev->last_beacon = jiffies;
  435. tim = rt2x00lib_find_ie(skb->data, skb->len - FCS_LEN, WLAN_EID_TIM);
  436. if (!tim)
  437. return;
  438. if (tim[1] < sizeof(*tim_ie))
  439. return;
  440. tim_len = tim[1];
  441. tim_ie = (struct ieee80211_tim_ie *) &tim[2];
  442. /* Check whenever the PHY can be turned off again. */
  443. /* 1. What about buffered unicast traffic for our AID? */
  444. cam = ieee80211_check_tim(tim_ie, tim_len, rt2x00dev->aid);
  445. /* 2. Maybe the AP wants to send multicast/broadcast data? */
  446. cam |= (tim_ie->bitmap_ctrl & 0x01);
  447. if (!cam && !test_bit(CONFIG_POWERSAVING, &rt2x00dev->flags))
  448. queue_work(rt2x00dev->workqueue, &rt2x00dev->sleep_work);
  449. }
  450. static int rt2x00lib_rxdone_read_signal(struct rt2x00_dev *rt2x00dev,
  451. struct rxdone_entry_desc *rxdesc)
  452. {
  453. struct ieee80211_supported_band *sband;
  454. const struct rt2x00_rate *rate;
  455. unsigned int i;
  456. int signal = rxdesc->signal;
  457. int type = (rxdesc->dev_flags & RXDONE_SIGNAL_MASK);
  458. switch (rxdesc->rate_mode) {
  459. case RATE_MODE_CCK:
  460. case RATE_MODE_OFDM:
  461. /*
  462. * For non-HT rates the MCS value needs to contain the
  463. * actually used rate modulation (CCK or OFDM).
  464. */
  465. if (rxdesc->dev_flags & RXDONE_SIGNAL_MCS)
  466. signal = RATE_MCS(rxdesc->rate_mode, signal);
  467. sband = &rt2x00dev->bands[rt2x00dev->curr_band];
  468. for (i = 0; i < sband->n_bitrates; i++) {
  469. rate = rt2x00_get_rate(sband->bitrates[i].hw_value);
  470. if (((type == RXDONE_SIGNAL_PLCP) &&
  471. (rate->plcp == signal)) ||
  472. ((type == RXDONE_SIGNAL_BITRATE) &&
  473. (rate->bitrate == signal)) ||
  474. ((type == RXDONE_SIGNAL_MCS) &&
  475. (rate->mcs == signal))) {
  476. return i;
  477. }
  478. }
  479. break;
  480. case RATE_MODE_HT_MIX:
  481. case RATE_MODE_HT_GREENFIELD:
  482. if (signal >= 0 && signal <= 76)
  483. return signal;
  484. break;
  485. default:
  486. break;
  487. }
  488. WARNING(rt2x00dev, "Frame received with unrecognized signal, "
  489. "mode=0x%.4x, signal=0x%.4x, type=%d.\n",
  490. rxdesc->rate_mode, signal, type);
  491. return 0;
  492. }
  493. void rt2x00lib_rxdone(struct queue_entry *entry)
  494. {
  495. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  496. struct rxdone_entry_desc rxdesc;
  497. struct sk_buff *skb;
  498. struct ieee80211_rx_status *rx_status;
  499. unsigned int header_length;
  500. int rate_idx;
  501. if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) ||
  502. !test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  503. goto submit_entry;
  504. if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags))
  505. goto submit_entry;
  506. /*
  507. * Allocate a new sk_buffer. If no new buffer available, drop the
  508. * received frame and reuse the existing buffer.
  509. */
  510. skb = rt2x00queue_alloc_rxskb(entry);
  511. if (!skb)
  512. goto submit_entry;
  513. /*
  514. * Unmap the skb.
  515. */
  516. rt2x00queue_unmap_skb(entry);
  517. /*
  518. * Extract the RXD details.
  519. */
  520. memset(&rxdesc, 0, sizeof(rxdesc));
  521. rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
  522. /*
  523. * Check for valid size in case we get corrupted descriptor from
  524. * hardware.
  525. */
  526. if (unlikely(rxdesc.size == 0 ||
  527. rxdesc.size > entry->queue->data_size)) {
  528. WARNING(rt2x00dev, "Wrong frame size %d max %d.\n",
  529. rxdesc.size, entry->queue->data_size);
  530. dev_kfree_skb(entry->skb);
  531. goto renew_skb;
  532. }
  533. /*
  534. * The data behind the ieee80211 header must be
  535. * aligned on a 4 byte boundary.
  536. */
  537. header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
  538. /*
  539. * Hardware might have stripped the IV/EIV/ICV data,
  540. * in that case it is possible that the data was
  541. * provided separately (through hardware descriptor)
  542. * in which case we should reinsert the data into the frame.
  543. */
  544. if ((rxdesc.dev_flags & RXDONE_CRYPTO_IV) &&
  545. (rxdesc.flags & RX_FLAG_IV_STRIPPED))
  546. rt2x00crypto_rx_insert_iv(entry->skb, header_length,
  547. &rxdesc);
  548. else if (header_length &&
  549. (rxdesc.size > header_length) &&
  550. (rxdesc.dev_flags & RXDONE_L2PAD))
  551. rt2x00queue_remove_l2pad(entry->skb, header_length);
  552. /* Trim buffer to correct size */
  553. skb_trim(entry->skb, rxdesc.size);
  554. /*
  555. * Translate the signal to the correct bitrate index.
  556. */
  557. rate_idx = rt2x00lib_rxdone_read_signal(rt2x00dev, &rxdesc);
  558. if (rxdesc.rate_mode == RATE_MODE_HT_MIX ||
  559. rxdesc.rate_mode == RATE_MODE_HT_GREENFIELD)
  560. rxdesc.flags |= RX_FLAG_HT;
  561. /*
  562. * Check if this is a beacon, and more frames have been
  563. * buffered while we were in powersaving mode.
  564. */
  565. rt2x00lib_rxdone_check_ps(rt2x00dev, entry->skb, &rxdesc);
  566. /*
  567. * Update extra components
  568. */
  569. rt2x00link_update_stats(rt2x00dev, entry->skb, &rxdesc);
  570. rt2x00debug_update_crypto(rt2x00dev, &rxdesc);
  571. rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_RXDONE, entry->skb);
  572. /*
  573. * Initialize RX status information, and send frame
  574. * to mac80211.
  575. */
  576. rx_status = IEEE80211_SKB_RXCB(entry->skb);
  577. rx_status->mactime = rxdesc.timestamp;
  578. rx_status->band = rt2x00dev->curr_band;
  579. rx_status->freq = rt2x00dev->curr_freq;
  580. rx_status->rate_idx = rate_idx;
  581. rx_status->signal = rxdesc.rssi;
  582. rx_status->flag = rxdesc.flags;
  583. rx_status->antenna = rt2x00dev->link.ant.active.rx;
  584. ieee80211_rx_ni(rt2x00dev->hw, entry->skb);
  585. renew_skb:
  586. /*
  587. * Replace the skb with the freshly allocated one.
  588. */
  589. entry->skb = skb;
  590. submit_entry:
  591. entry->flags = 0;
  592. rt2x00queue_index_inc(entry, Q_INDEX_DONE);
  593. if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) &&
  594. test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  595. rt2x00dev->ops->lib->clear_entry(entry);
  596. }
  597. EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
  598. /*
  599. * Driver initialization handlers.
  600. */
  601. const struct rt2x00_rate rt2x00_supported_rates[12] = {
  602. {
  603. .flags = DEV_RATE_CCK,
  604. .bitrate = 10,
  605. .ratemask = BIT(0),
  606. .plcp = 0x00,
  607. .mcs = RATE_MCS(RATE_MODE_CCK, 0),
  608. },
  609. {
  610. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
  611. .bitrate = 20,
  612. .ratemask = BIT(1),
  613. .plcp = 0x01,
  614. .mcs = RATE_MCS(RATE_MODE_CCK, 1),
  615. },
  616. {
  617. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
  618. .bitrate = 55,
  619. .ratemask = BIT(2),
  620. .plcp = 0x02,
  621. .mcs = RATE_MCS(RATE_MODE_CCK, 2),
  622. },
  623. {
  624. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
  625. .bitrate = 110,
  626. .ratemask = BIT(3),
  627. .plcp = 0x03,
  628. .mcs = RATE_MCS(RATE_MODE_CCK, 3),
  629. },
  630. {
  631. .flags = DEV_RATE_OFDM,
  632. .bitrate = 60,
  633. .ratemask = BIT(4),
  634. .plcp = 0x0b,
  635. .mcs = RATE_MCS(RATE_MODE_OFDM, 0),
  636. },
  637. {
  638. .flags = DEV_RATE_OFDM,
  639. .bitrate = 90,
  640. .ratemask = BIT(5),
  641. .plcp = 0x0f,
  642. .mcs = RATE_MCS(RATE_MODE_OFDM, 1),
  643. },
  644. {
  645. .flags = DEV_RATE_OFDM,
  646. .bitrate = 120,
  647. .ratemask = BIT(6),
  648. .plcp = 0x0a,
  649. .mcs = RATE_MCS(RATE_MODE_OFDM, 2),
  650. },
  651. {
  652. .flags = DEV_RATE_OFDM,
  653. .bitrate = 180,
  654. .ratemask = BIT(7),
  655. .plcp = 0x0e,
  656. .mcs = RATE_MCS(RATE_MODE_OFDM, 3),
  657. },
  658. {
  659. .flags = DEV_RATE_OFDM,
  660. .bitrate = 240,
  661. .ratemask = BIT(8),
  662. .plcp = 0x09,
  663. .mcs = RATE_MCS(RATE_MODE_OFDM, 4),
  664. },
  665. {
  666. .flags = DEV_RATE_OFDM,
  667. .bitrate = 360,
  668. .ratemask = BIT(9),
  669. .plcp = 0x0d,
  670. .mcs = RATE_MCS(RATE_MODE_OFDM, 5),
  671. },
  672. {
  673. .flags = DEV_RATE_OFDM,
  674. .bitrate = 480,
  675. .ratemask = BIT(10),
  676. .plcp = 0x08,
  677. .mcs = RATE_MCS(RATE_MODE_OFDM, 6),
  678. },
  679. {
  680. .flags = DEV_RATE_OFDM,
  681. .bitrate = 540,
  682. .ratemask = BIT(11),
  683. .plcp = 0x0c,
  684. .mcs = RATE_MCS(RATE_MODE_OFDM, 7),
  685. },
  686. };
  687. static void rt2x00lib_channel(struct ieee80211_channel *entry,
  688. const int channel, const int tx_power,
  689. const int value)
  690. {
  691. /* XXX: this assumption about the band is wrong for 802.11j */
  692. entry->band = channel <= 14 ? IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
  693. entry->center_freq = ieee80211_channel_to_frequency(channel,
  694. entry->band);
  695. entry->hw_value = value;
  696. entry->max_power = tx_power;
  697. entry->max_antenna_gain = 0xff;
  698. }
  699. static void rt2x00lib_rate(struct ieee80211_rate *entry,
  700. const u16 index, const struct rt2x00_rate *rate)
  701. {
  702. entry->flags = 0;
  703. entry->bitrate = rate->bitrate;
  704. entry->hw_value = index;
  705. entry->hw_value_short = index;
  706. if (rate->flags & DEV_RATE_SHORT_PREAMBLE)
  707. entry->flags |= IEEE80211_RATE_SHORT_PREAMBLE;
  708. }
  709. static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
  710. struct hw_mode_spec *spec)
  711. {
  712. struct ieee80211_hw *hw = rt2x00dev->hw;
  713. struct ieee80211_channel *channels;
  714. struct ieee80211_rate *rates;
  715. unsigned int num_rates;
  716. unsigned int i;
  717. num_rates = 0;
  718. if (spec->supported_rates & SUPPORT_RATE_CCK)
  719. num_rates += 4;
  720. if (spec->supported_rates & SUPPORT_RATE_OFDM)
  721. num_rates += 8;
  722. channels = kcalloc(spec->num_channels, sizeof(*channels), GFP_KERNEL);
  723. if (!channels)
  724. return -ENOMEM;
  725. rates = kcalloc(num_rates, sizeof(*rates), GFP_KERNEL);
  726. if (!rates)
  727. goto exit_free_channels;
  728. /*
  729. * Initialize Rate list.
  730. */
  731. for (i = 0; i < num_rates; i++)
  732. rt2x00lib_rate(&rates[i], i, rt2x00_get_rate(i));
  733. /*
  734. * Initialize Channel list.
  735. */
  736. for (i = 0; i < spec->num_channels; i++) {
  737. rt2x00lib_channel(&channels[i],
  738. spec->channels[i].channel,
  739. spec->channels_info[i].max_power, i);
  740. }
  741. /*
  742. * Intitialize 802.11b, 802.11g
  743. * Rates: CCK, OFDM.
  744. * Channels: 2.4 GHz
  745. */
  746. if (spec->supported_bands & SUPPORT_BAND_2GHZ) {
  747. rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_channels = 14;
  748. rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_bitrates = num_rates;
  749. rt2x00dev->bands[IEEE80211_BAND_2GHZ].channels = channels;
  750. rt2x00dev->bands[IEEE80211_BAND_2GHZ].bitrates = rates;
  751. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  752. &rt2x00dev->bands[IEEE80211_BAND_2GHZ];
  753. memcpy(&rt2x00dev->bands[IEEE80211_BAND_2GHZ].ht_cap,
  754. &spec->ht, sizeof(spec->ht));
  755. }
  756. /*
  757. * Intitialize 802.11a
  758. * Rates: OFDM.
  759. * Channels: OFDM, UNII, HiperLAN2.
  760. */
  761. if (spec->supported_bands & SUPPORT_BAND_5GHZ) {
  762. rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_channels =
  763. spec->num_channels - 14;
  764. rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_bitrates =
  765. num_rates - 4;
  766. rt2x00dev->bands[IEEE80211_BAND_5GHZ].channels = &channels[14];
  767. rt2x00dev->bands[IEEE80211_BAND_5GHZ].bitrates = &rates[4];
  768. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  769. &rt2x00dev->bands[IEEE80211_BAND_5GHZ];
  770. memcpy(&rt2x00dev->bands[IEEE80211_BAND_5GHZ].ht_cap,
  771. &spec->ht, sizeof(spec->ht));
  772. }
  773. return 0;
  774. exit_free_channels:
  775. kfree(channels);
  776. ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
  777. return -ENOMEM;
  778. }
  779. static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
  780. {
  781. if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
  782. ieee80211_unregister_hw(rt2x00dev->hw);
  783. if (likely(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ])) {
  784. kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->channels);
  785. kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->bitrates);
  786. rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
  787. rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
  788. }
  789. kfree(rt2x00dev->spec.channels_info);
  790. }
  791. static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
  792. {
  793. struct hw_mode_spec *spec = &rt2x00dev->spec;
  794. int status;
  795. if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
  796. return 0;
  797. /*
  798. * Initialize HW modes.
  799. */
  800. status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
  801. if (status)
  802. return status;
  803. /*
  804. * Initialize HW fields.
  805. */
  806. rt2x00dev->hw->queues = rt2x00dev->ops->tx_queues;
  807. /*
  808. * Initialize extra TX headroom required.
  809. */
  810. rt2x00dev->hw->extra_tx_headroom =
  811. max_t(unsigned int, IEEE80211_TX_STATUS_HEADROOM,
  812. rt2x00dev->ops->extra_tx_headroom);
  813. /*
  814. * Take TX headroom required for alignment into account.
  815. */
  816. if (test_bit(REQUIRE_L2PAD, &rt2x00dev->cap_flags))
  817. rt2x00dev->hw->extra_tx_headroom += RT2X00_L2PAD_SIZE;
  818. else if (test_bit(REQUIRE_DMA, &rt2x00dev->cap_flags))
  819. rt2x00dev->hw->extra_tx_headroom += RT2X00_ALIGN_SIZE;
  820. /*
  821. * Tell mac80211 about the size of our private STA structure.
  822. */
  823. rt2x00dev->hw->sta_data_size = sizeof(struct rt2x00_sta);
  824. /*
  825. * Allocate tx status FIFO for driver use.
  826. */
  827. if (test_bit(REQUIRE_TXSTATUS_FIFO, &rt2x00dev->cap_flags)) {
  828. /*
  829. * Allocate the txstatus fifo. In the worst case the tx
  830. * status fifo has to hold the tx status of all entries
  831. * in all tx queues. Hence, calculate the kfifo size as
  832. * tx_queues * entry_num and round up to the nearest
  833. * power of 2.
  834. */
  835. int kfifo_size =
  836. roundup_pow_of_two(rt2x00dev->ops->tx_queues *
  837. rt2x00dev->ops->tx->entry_num *
  838. sizeof(u32));
  839. status = kfifo_alloc(&rt2x00dev->txstatus_fifo, kfifo_size,
  840. GFP_KERNEL);
  841. if (status)
  842. return status;
  843. }
  844. /*
  845. * Initialize tasklets if used by the driver. Tasklets are
  846. * disabled until the interrupts are turned on. The driver
  847. * has to handle that.
  848. */
  849. #define RT2X00_TASKLET_INIT(taskletname) \
  850. if (rt2x00dev->ops->lib->taskletname) { \
  851. tasklet_init(&rt2x00dev->taskletname, \
  852. rt2x00dev->ops->lib->taskletname, \
  853. (unsigned long)rt2x00dev); \
  854. }
  855. RT2X00_TASKLET_INIT(txstatus_tasklet);
  856. RT2X00_TASKLET_INIT(pretbtt_tasklet);
  857. RT2X00_TASKLET_INIT(tbtt_tasklet);
  858. RT2X00_TASKLET_INIT(rxdone_tasklet);
  859. RT2X00_TASKLET_INIT(autowake_tasklet);
  860. #undef RT2X00_TASKLET_INIT
  861. /*
  862. * Register HW.
  863. */
  864. status = ieee80211_register_hw(rt2x00dev->hw);
  865. if (status)
  866. return status;
  867. set_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags);
  868. return 0;
  869. }
  870. /*
  871. * Initialization/uninitialization handlers.
  872. */
  873. static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
  874. {
  875. if (!test_and_clear_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
  876. return;
  877. /*
  878. * Unregister extra components.
  879. */
  880. rt2x00rfkill_unregister(rt2x00dev);
  881. /*
  882. * Allow the HW to uninitialize.
  883. */
  884. rt2x00dev->ops->lib->uninitialize(rt2x00dev);
  885. /*
  886. * Free allocated queue entries.
  887. */
  888. rt2x00queue_uninitialize(rt2x00dev);
  889. }
  890. static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
  891. {
  892. int status;
  893. if (test_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
  894. return 0;
  895. /*
  896. * Allocate all queue entries.
  897. */
  898. status = rt2x00queue_initialize(rt2x00dev);
  899. if (status)
  900. return status;
  901. /*
  902. * Initialize the device.
  903. */
  904. status = rt2x00dev->ops->lib->initialize(rt2x00dev);
  905. if (status) {
  906. rt2x00queue_uninitialize(rt2x00dev);
  907. return status;
  908. }
  909. set_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags);
  910. /*
  911. * Register the extra components.
  912. */
  913. rt2x00rfkill_register(rt2x00dev);
  914. return 0;
  915. }
  916. int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
  917. {
  918. int retval;
  919. if (test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
  920. return 0;
  921. /*
  922. * If this is the first interface which is added,
  923. * we should load the firmware now.
  924. */
  925. retval = rt2x00lib_load_firmware(rt2x00dev);
  926. if (retval)
  927. return retval;
  928. /*
  929. * Initialize the device.
  930. */
  931. retval = rt2x00lib_initialize(rt2x00dev);
  932. if (retval)
  933. return retval;
  934. rt2x00dev->intf_ap_count = 0;
  935. rt2x00dev->intf_sta_count = 0;
  936. rt2x00dev->intf_associated = 0;
  937. /* Enable the radio */
  938. retval = rt2x00lib_enable_radio(rt2x00dev);
  939. if (retval)
  940. return retval;
  941. set_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags);
  942. return 0;
  943. }
  944. void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
  945. {
  946. if (!test_and_clear_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
  947. return;
  948. /*
  949. * Perhaps we can add something smarter here,
  950. * but for now just disabling the radio should do.
  951. */
  952. rt2x00lib_disable_radio(rt2x00dev);
  953. rt2x00dev->intf_ap_count = 0;
  954. rt2x00dev->intf_sta_count = 0;
  955. rt2x00dev->intf_associated = 0;
  956. }
  957. /*
  958. * driver allocation handlers.
  959. */
  960. int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
  961. {
  962. int retval = -ENOMEM;
  963. spin_lock_init(&rt2x00dev->irqmask_lock);
  964. mutex_init(&rt2x00dev->csr_mutex);
  965. set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  966. /*
  967. * Make room for rt2x00_intf inside the per-interface
  968. * structure ieee80211_vif.
  969. */
  970. rt2x00dev->hw->vif_data_size = sizeof(struct rt2x00_intf);
  971. /*
  972. * Determine which operating modes are supported, all modes
  973. * which require beaconing, depend on the availability of
  974. * beacon entries.
  975. */
  976. rt2x00dev->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  977. if (rt2x00dev->ops->bcn->entry_num > 0)
  978. rt2x00dev->hw->wiphy->interface_modes |=
  979. BIT(NL80211_IFTYPE_ADHOC) |
  980. BIT(NL80211_IFTYPE_AP) |
  981. BIT(NL80211_IFTYPE_MESH_POINT) |
  982. BIT(NL80211_IFTYPE_WDS);
  983. /*
  984. * Initialize work.
  985. */
  986. rt2x00dev->workqueue =
  987. alloc_ordered_workqueue(wiphy_name(rt2x00dev->hw->wiphy), 0);
  988. if (!rt2x00dev->workqueue) {
  989. retval = -ENOMEM;
  990. goto exit;
  991. }
  992. INIT_WORK(&rt2x00dev->intf_work, rt2x00lib_intf_scheduled);
  993. INIT_DELAYED_WORK(&rt2x00dev->autowakeup_work, rt2x00lib_autowakeup);
  994. INIT_WORK(&rt2x00dev->sleep_work, rt2x00lib_sleep);
  995. /*
  996. * Let the driver probe the device to detect the capabilities.
  997. */
  998. retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
  999. if (retval) {
  1000. ERROR(rt2x00dev, "Failed to allocate device.\n");
  1001. goto exit;
  1002. }
  1003. /*
  1004. * Allocate queue array.
  1005. */
  1006. retval = rt2x00queue_allocate(rt2x00dev);
  1007. if (retval)
  1008. goto exit;
  1009. /*
  1010. * Initialize ieee80211 structure.
  1011. */
  1012. retval = rt2x00lib_probe_hw(rt2x00dev);
  1013. if (retval) {
  1014. ERROR(rt2x00dev, "Failed to initialize hw.\n");
  1015. goto exit;
  1016. }
  1017. /*
  1018. * Register extra components.
  1019. */
  1020. rt2x00link_register(rt2x00dev);
  1021. rt2x00leds_register(rt2x00dev);
  1022. rt2x00debug_register(rt2x00dev);
  1023. return 0;
  1024. exit:
  1025. rt2x00lib_remove_dev(rt2x00dev);
  1026. return retval;
  1027. }
  1028. EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
  1029. void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
  1030. {
  1031. clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  1032. /*
  1033. * Disable radio.
  1034. */
  1035. rt2x00lib_disable_radio(rt2x00dev);
  1036. /*
  1037. * Stop all work.
  1038. */
  1039. cancel_work_sync(&rt2x00dev->intf_work);
  1040. cancel_delayed_work_sync(&rt2x00dev->autowakeup_work);
  1041. cancel_work_sync(&rt2x00dev->sleep_work);
  1042. if (rt2x00_is_usb(rt2x00dev)) {
  1043. del_timer_sync(&rt2x00dev->txstatus_timer);
  1044. cancel_work_sync(&rt2x00dev->rxdone_work);
  1045. cancel_work_sync(&rt2x00dev->txdone_work);
  1046. }
  1047. if (rt2x00dev->workqueue)
  1048. destroy_workqueue(rt2x00dev->workqueue);
  1049. /*
  1050. * Free the tx status fifo.
  1051. */
  1052. kfifo_free(&rt2x00dev->txstatus_fifo);
  1053. /*
  1054. * Kill the tx status tasklet.
  1055. */
  1056. tasklet_kill(&rt2x00dev->txstatus_tasklet);
  1057. tasklet_kill(&rt2x00dev->pretbtt_tasklet);
  1058. tasklet_kill(&rt2x00dev->tbtt_tasklet);
  1059. tasklet_kill(&rt2x00dev->rxdone_tasklet);
  1060. tasklet_kill(&rt2x00dev->autowake_tasklet);
  1061. /*
  1062. * Uninitialize device.
  1063. */
  1064. rt2x00lib_uninitialize(rt2x00dev);
  1065. /*
  1066. * Free extra components
  1067. */
  1068. rt2x00debug_deregister(rt2x00dev);
  1069. rt2x00leds_unregister(rt2x00dev);
  1070. /*
  1071. * Free ieee80211_hw memory.
  1072. */
  1073. rt2x00lib_remove_hw(rt2x00dev);
  1074. /*
  1075. * Free firmware image.
  1076. */
  1077. rt2x00lib_free_firmware(rt2x00dev);
  1078. /*
  1079. * Free queue structures.
  1080. */
  1081. rt2x00queue_free(rt2x00dev);
  1082. }
  1083. EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
  1084. /*
  1085. * Device state handlers
  1086. */
  1087. #ifdef CONFIG_PM
  1088. int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
  1089. {
  1090. NOTICE(rt2x00dev, "Going to sleep.\n");
  1091. /*
  1092. * Prevent mac80211 from accessing driver while suspended.
  1093. */
  1094. if (!test_and_clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  1095. return 0;
  1096. /*
  1097. * Cleanup as much as possible.
  1098. */
  1099. rt2x00lib_uninitialize(rt2x00dev);
  1100. /*
  1101. * Suspend/disable extra components.
  1102. */
  1103. rt2x00leds_suspend(rt2x00dev);
  1104. rt2x00debug_deregister(rt2x00dev);
  1105. /*
  1106. * Set device mode to sleep for power management,
  1107. * on some hardware this call seems to consistently fail.
  1108. * From the specifications it is hard to tell why it fails,
  1109. * and if this is a "bad thing".
  1110. * Overall it is safe to just ignore the failure and
  1111. * continue suspending. The only downside is that the
  1112. * device will not be in optimal power save mode, but with
  1113. * the radio and the other components already disabled the
  1114. * device is as good as disabled.
  1115. */
  1116. if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP))
  1117. WARNING(rt2x00dev, "Device failed to enter sleep state, "
  1118. "continue suspending.\n");
  1119. return 0;
  1120. }
  1121. EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
  1122. int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
  1123. {
  1124. NOTICE(rt2x00dev, "Waking up.\n");
  1125. /*
  1126. * Restore/enable extra components.
  1127. */
  1128. rt2x00debug_register(rt2x00dev);
  1129. rt2x00leds_resume(rt2x00dev);
  1130. /*
  1131. * We are ready again to receive requests from mac80211.
  1132. */
  1133. set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  1134. return 0;
  1135. }
  1136. EXPORT_SYMBOL_GPL(rt2x00lib_resume);
  1137. #endif /* CONFIG_PM */
  1138. /*
  1139. * rt2x00lib module information.
  1140. */
  1141. MODULE_AUTHOR(DRV_PROJECT);
  1142. MODULE_VERSION(DRV_VERSION);
  1143. MODULE_DESCRIPTION("rt2x00 library");
  1144. MODULE_LICENSE("GPL");