rt2x00dev.c 39 KB

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