rt2x00dev.c 34 KB

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