rt2x00dev.c 35 KB

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