rt2x00dev.c 38 KB

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