rt2x00dev.c 39 KB

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