rt2x00dev.c 39 KB

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