rt2x00dev.c 34 KB

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