rt2x00dev.c 38 KB

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