rt2x00dev.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. /*
  2. Copyright (C) 2004 - 2008 rt2x00 SourceForge Project
  3. <http://rt2x00.serialmonkey.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the
  14. Free Software Foundation, Inc.,
  15. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. /*
  18. Module: rt2x00lib
  19. Abstract: rt2x00 generic device routines.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include "rt2x00.h"
  24. #include "rt2x00lib.h"
  25. #include "rt2x00dump.h"
  26. /*
  27. * Link tuning handlers
  28. */
  29. void rt2x00lib_reset_link_tuner(struct rt2x00_dev *rt2x00dev)
  30. {
  31. if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  32. return;
  33. /*
  34. * Reset link information.
  35. * Both the currently active vgc level as well as
  36. * the link tuner counter should be reset. Resetting
  37. * the counter is important for devices where the
  38. * device should only perform link tuning during the
  39. * first minute after being enabled.
  40. */
  41. rt2x00dev->link.count = 0;
  42. rt2x00dev->link.vgc_level = 0;
  43. /*
  44. * Reset the link tuner.
  45. */
  46. rt2x00dev->ops->lib->reset_tuner(rt2x00dev);
  47. }
  48. static void rt2x00lib_start_link_tuner(struct rt2x00_dev *rt2x00dev)
  49. {
  50. /*
  51. * Clear all (possibly) pre-existing quality statistics.
  52. */
  53. memset(&rt2x00dev->link.qual, 0, sizeof(rt2x00dev->link.qual));
  54. /*
  55. * The RX and TX percentage should start at 50%
  56. * this will assure we will get at least get some
  57. * decent value when the link tuner starts.
  58. * The value will be dropped and overwritten with
  59. * the correct (measured )value anyway during the
  60. * first run of the link tuner.
  61. */
  62. rt2x00dev->link.qual.rx_percentage = 50;
  63. rt2x00dev->link.qual.tx_percentage = 50;
  64. rt2x00lib_reset_link_tuner(rt2x00dev);
  65. queue_delayed_work(rt2x00dev->hw->workqueue,
  66. &rt2x00dev->link.work, LINK_TUNE_INTERVAL);
  67. }
  68. static void rt2x00lib_stop_link_tuner(struct rt2x00_dev *rt2x00dev)
  69. {
  70. cancel_delayed_work_sync(&rt2x00dev->link.work);
  71. }
  72. /*
  73. * Radio control handlers.
  74. */
  75. int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
  76. {
  77. int status;
  78. /*
  79. * Don't enable the radio twice.
  80. * And check if the hardware button has been disabled.
  81. */
  82. if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
  83. test_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags))
  84. return 0;
  85. /*
  86. * Initialize all data queues.
  87. */
  88. rt2x00queue_init_rx(rt2x00dev);
  89. rt2x00queue_init_tx(rt2x00dev);
  90. /*
  91. * Enable radio.
  92. */
  93. status = rt2x00dev->ops->lib->set_device_state(rt2x00dev,
  94. STATE_RADIO_ON);
  95. if (status)
  96. return status;
  97. __set_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags);
  98. /*
  99. * Enable RX.
  100. */
  101. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
  102. /*
  103. * Start the TX queues.
  104. */
  105. ieee80211_start_queues(rt2x00dev->hw);
  106. return 0;
  107. }
  108. void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
  109. {
  110. if (!__test_and_clear_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  111. return;
  112. /*
  113. * Stop all scheduled work.
  114. */
  115. if (work_pending(&rt2x00dev->intf_work))
  116. cancel_work_sync(&rt2x00dev->intf_work);
  117. if (work_pending(&rt2x00dev->filter_work))
  118. cancel_work_sync(&rt2x00dev->filter_work);
  119. /*
  120. * Stop the TX queues.
  121. */
  122. ieee80211_stop_queues(rt2x00dev->hw);
  123. /*
  124. * Disable RX.
  125. */
  126. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
  127. /*
  128. * Disable radio.
  129. */
  130. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
  131. }
  132. void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, enum dev_state state)
  133. {
  134. /*
  135. * When we are disabling the RX, we should also stop the link tuner.
  136. */
  137. if (state == STATE_RADIO_RX_OFF)
  138. rt2x00lib_stop_link_tuner(rt2x00dev);
  139. rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
  140. /*
  141. * When we are enabling the RX, we should also start the link tuner.
  142. */
  143. if (state == STATE_RADIO_RX_ON &&
  144. (rt2x00dev->intf_ap_count || rt2x00dev->intf_sta_count))
  145. rt2x00lib_start_link_tuner(rt2x00dev);
  146. }
  147. static void rt2x00lib_evaluate_antenna_sample(struct rt2x00_dev *rt2x00dev)
  148. {
  149. enum antenna rx = rt2x00dev->link.ant.active.rx;
  150. enum antenna tx = rt2x00dev->link.ant.active.tx;
  151. int sample_a =
  152. rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_A);
  153. int sample_b =
  154. rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_B);
  155. /*
  156. * We are done sampling. Now we should evaluate the results.
  157. */
  158. rt2x00dev->link.ant.flags &= ~ANTENNA_MODE_SAMPLE;
  159. /*
  160. * During the last period we have sampled the RSSI
  161. * from both antenna's. It now is time to determine
  162. * which antenna demonstrated the best performance.
  163. * When we are already on the antenna with the best
  164. * performance, then there really is nothing for us
  165. * left to do.
  166. */
  167. if (sample_a == sample_b)
  168. return;
  169. if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY)
  170. rx = (sample_a > sample_b) ? ANTENNA_A : ANTENNA_B;
  171. if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)
  172. tx = (sample_a > sample_b) ? ANTENNA_A : ANTENNA_B;
  173. rt2x00lib_config_antenna(rt2x00dev, rx, tx);
  174. }
  175. static void rt2x00lib_evaluate_antenna_eval(struct rt2x00_dev *rt2x00dev)
  176. {
  177. enum antenna rx = rt2x00dev->link.ant.active.rx;
  178. enum antenna tx = rt2x00dev->link.ant.active.tx;
  179. int rssi_curr = rt2x00_get_link_ant_rssi(&rt2x00dev->link);
  180. int rssi_old = rt2x00_update_ant_rssi(&rt2x00dev->link, rssi_curr);
  181. /*
  182. * Legacy driver indicates that we should swap antenna's
  183. * when the difference in RSSI is greater that 5. This
  184. * also should be done when the RSSI was actually better
  185. * then the previous sample.
  186. * When the difference exceeds the threshold we should
  187. * sample the rssi from the other antenna to make a valid
  188. * comparison between the 2 antennas.
  189. */
  190. if (abs(rssi_curr - rssi_old) < 5)
  191. return;
  192. rt2x00dev->link.ant.flags |= ANTENNA_MODE_SAMPLE;
  193. if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY)
  194. rx = (rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
  195. if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)
  196. tx = (tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
  197. rt2x00lib_config_antenna(rt2x00dev, rx, tx);
  198. }
  199. static void rt2x00lib_evaluate_antenna(struct rt2x00_dev *rt2x00dev)
  200. {
  201. /*
  202. * Determine if software diversity is enabled for
  203. * either the TX or RX antenna (or both).
  204. * Always perform this check since within the link
  205. * tuner interval the configuration might have changed.
  206. */
  207. rt2x00dev->link.ant.flags &= ~ANTENNA_RX_DIVERSITY;
  208. rt2x00dev->link.ant.flags &= ~ANTENNA_TX_DIVERSITY;
  209. if (rt2x00dev->hw->conf.antenna_sel_rx == 0 &&
  210. rt2x00dev->default_ant.rx == ANTENNA_SW_DIVERSITY)
  211. rt2x00dev->link.ant.flags |= ANTENNA_RX_DIVERSITY;
  212. if (rt2x00dev->hw->conf.antenna_sel_tx == 0 &&
  213. rt2x00dev->default_ant.tx == ANTENNA_SW_DIVERSITY)
  214. rt2x00dev->link.ant.flags |= ANTENNA_TX_DIVERSITY;
  215. if (!(rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY) &&
  216. !(rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)) {
  217. rt2x00dev->link.ant.flags = 0;
  218. return;
  219. }
  220. /*
  221. * If we have only sampled the data over the last period
  222. * we should now harvest the data. Otherwise just evaluate
  223. * the data. The latter should only be performed once
  224. * every 2 seconds.
  225. */
  226. if (rt2x00dev->link.ant.flags & ANTENNA_MODE_SAMPLE)
  227. rt2x00lib_evaluate_antenna_sample(rt2x00dev);
  228. else if (rt2x00dev->link.count & 1)
  229. rt2x00lib_evaluate_antenna_eval(rt2x00dev);
  230. }
  231. static void rt2x00lib_update_link_stats(struct link *link, int rssi)
  232. {
  233. int avg_rssi = rssi;
  234. /*
  235. * Update global RSSI
  236. */
  237. if (link->qual.avg_rssi)
  238. avg_rssi = MOVING_AVERAGE(link->qual.avg_rssi, rssi, 8);
  239. link->qual.avg_rssi = avg_rssi;
  240. /*
  241. * Update antenna RSSI
  242. */
  243. if (link->ant.rssi_ant)
  244. rssi = MOVING_AVERAGE(link->ant.rssi_ant, rssi, 8);
  245. link->ant.rssi_ant = rssi;
  246. }
  247. static void rt2x00lib_precalculate_link_signal(struct link_qual *qual)
  248. {
  249. if (qual->rx_failed || qual->rx_success)
  250. qual->rx_percentage =
  251. (qual->rx_success * 100) /
  252. (qual->rx_failed + qual->rx_success);
  253. else
  254. qual->rx_percentage = 50;
  255. if (qual->tx_failed || qual->tx_success)
  256. qual->tx_percentage =
  257. (qual->tx_success * 100) /
  258. (qual->tx_failed + qual->tx_success);
  259. else
  260. qual->tx_percentage = 50;
  261. qual->rx_success = 0;
  262. qual->rx_failed = 0;
  263. qual->tx_success = 0;
  264. qual->tx_failed = 0;
  265. }
  266. static int rt2x00lib_calculate_link_signal(struct rt2x00_dev *rt2x00dev,
  267. int rssi)
  268. {
  269. int rssi_percentage = 0;
  270. int signal;
  271. /*
  272. * We need a positive value for the RSSI.
  273. */
  274. if (rssi < 0)
  275. rssi += rt2x00dev->rssi_offset;
  276. /*
  277. * Calculate the different percentages,
  278. * which will be used for the signal.
  279. */
  280. if (rt2x00dev->rssi_offset)
  281. rssi_percentage = (rssi * 100) / rt2x00dev->rssi_offset;
  282. /*
  283. * Add the individual percentages and use the WEIGHT
  284. * defines to calculate the current link signal.
  285. */
  286. signal = ((WEIGHT_RSSI * rssi_percentage) +
  287. (WEIGHT_TX * rt2x00dev->link.qual.tx_percentage) +
  288. (WEIGHT_RX * rt2x00dev->link.qual.rx_percentage)) / 100;
  289. return (signal > 100) ? 100 : signal;
  290. }
  291. static void rt2x00lib_link_tuner(struct work_struct *work)
  292. {
  293. struct rt2x00_dev *rt2x00dev =
  294. container_of(work, struct rt2x00_dev, link.work.work);
  295. /*
  296. * When the radio is shutting down we should
  297. * immediately cease all link tuning.
  298. */
  299. if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  300. return;
  301. /*
  302. * Update statistics.
  303. */
  304. rt2x00dev->ops->lib->link_stats(rt2x00dev, &rt2x00dev->link.qual);
  305. rt2x00dev->low_level_stats.dot11FCSErrorCount +=
  306. rt2x00dev->link.qual.rx_failed;
  307. /*
  308. * Only perform the link tuning when Link tuning
  309. * has been enabled (This could have been disabled from the EEPROM).
  310. */
  311. if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
  312. rt2x00dev->ops->lib->link_tuner(rt2x00dev);
  313. /*
  314. * Precalculate a portion of the link signal which is
  315. * in based on the tx/rx success/failure counters.
  316. */
  317. rt2x00lib_precalculate_link_signal(&rt2x00dev->link.qual);
  318. /*
  319. * Evaluate antenna setup, make this the last step since this could
  320. * possibly reset some statistics.
  321. */
  322. rt2x00lib_evaluate_antenna(rt2x00dev);
  323. /*
  324. * Increase tuner counter, and reschedule the next link tuner run.
  325. */
  326. rt2x00dev->link.count++;
  327. queue_delayed_work(rt2x00dev->hw->workqueue, &rt2x00dev->link.work,
  328. LINK_TUNE_INTERVAL);
  329. }
  330. static void rt2x00lib_packetfilter_scheduled(struct work_struct *work)
  331. {
  332. struct rt2x00_dev *rt2x00dev =
  333. container_of(work, struct rt2x00_dev, filter_work);
  334. unsigned int filter = rt2x00dev->packet_filter;
  335. /*
  336. * Since we had stored the filter inside rt2x00dev->packet_filter,
  337. * we should now clear that field. Otherwise the driver will
  338. * assume nothing has changed (*total_flags will be compared
  339. * to rt2x00dev->packet_filter to determine if any action is required).
  340. */
  341. rt2x00dev->packet_filter = 0;
  342. rt2x00dev->ops->hw->configure_filter(rt2x00dev->hw,
  343. filter, &filter, 0, NULL);
  344. }
  345. static void rt2x00lib_intf_scheduled_iter(void *data, u8 *mac,
  346. struct ieee80211_vif *vif)
  347. {
  348. struct rt2x00_dev *rt2x00dev = data;
  349. struct rt2x00_intf *intf = vif_to_intf(vif);
  350. struct sk_buff *skb;
  351. struct ieee80211_tx_control control;
  352. struct ieee80211_bss_conf conf;
  353. int delayed_flags;
  354. /*
  355. * Copy all data we need during this action under the protection
  356. * of a spinlock. Otherwise race conditions might occur which results
  357. * into an invalid configuration.
  358. */
  359. spin_lock(&intf->lock);
  360. memcpy(&conf, &intf->conf, sizeof(conf));
  361. delayed_flags = intf->delayed_flags;
  362. intf->delayed_flags = 0;
  363. spin_unlock(&intf->lock);
  364. if (delayed_flags & DELAYED_UPDATE_BEACON) {
  365. skb = ieee80211_beacon_get(rt2x00dev->hw, vif, &control);
  366. if (skb) {
  367. rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw, skb,
  368. &control);
  369. dev_kfree_skb(skb);
  370. }
  371. }
  372. if (delayed_flags & DELAYED_CONFIG_PREAMBLE)
  373. rt2x00lib_config_preamble(rt2x00dev, intf,
  374. intf->conf.use_short_preamble);
  375. }
  376. static void rt2x00lib_intf_scheduled(struct work_struct *work)
  377. {
  378. struct rt2x00_dev *rt2x00dev =
  379. container_of(work, struct rt2x00_dev, intf_work);
  380. /*
  381. * Iterate over each interface and perform the
  382. * requested configurations.
  383. */
  384. ieee80211_iterate_active_interfaces(rt2x00dev->hw,
  385. rt2x00lib_intf_scheduled_iter,
  386. rt2x00dev);
  387. }
  388. /*
  389. * Interrupt context handlers.
  390. */
  391. static void rt2x00lib_beacondone_iter(void *data, u8 *mac,
  392. struct ieee80211_vif *vif)
  393. {
  394. struct rt2x00_intf *intf = vif_to_intf(vif);
  395. if (vif->type != IEEE80211_IF_TYPE_AP &&
  396. vif->type != IEEE80211_IF_TYPE_IBSS)
  397. return;
  398. spin_lock(&intf->lock);
  399. intf->delayed_flags |= DELAYED_UPDATE_BEACON;
  400. spin_unlock(&intf->lock);
  401. }
  402. void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
  403. {
  404. if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  405. return;
  406. ieee80211_iterate_active_interfaces(rt2x00dev->hw,
  407. rt2x00lib_beacondone_iter,
  408. rt2x00dev);
  409. queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->intf_work);
  410. }
  411. EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
  412. void rt2x00lib_txdone(struct queue_entry *entry,
  413. struct txdone_entry_desc *txdesc)
  414. {
  415. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  416. struct ieee80211_tx_status tx_status;
  417. int success = !!(txdesc->status == TX_SUCCESS ||
  418. txdesc->status == TX_SUCCESS_RETRY);
  419. int fail = !!(txdesc->status == TX_FAIL_RETRY ||
  420. txdesc->status == TX_FAIL_INVALID ||
  421. txdesc->status == TX_FAIL_OTHER);
  422. /*
  423. * Update TX statistics.
  424. */
  425. rt2x00dev->link.qual.tx_success += success;
  426. rt2x00dev->link.qual.tx_failed += txdesc->retry + fail;
  427. /*
  428. * Initialize TX status
  429. */
  430. tx_status.flags = 0;
  431. tx_status.ack_signal = 0;
  432. tx_status.excessive_retries = (txdesc->status == TX_FAIL_RETRY);
  433. tx_status.retry_count = txdesc->retry;
  434. memcpy(&tx_status.control, txdesc->control, sizeof(txdesc->control));
  435. if (!(tx_status.control.flags & IEEE80211_TXCTL_NO_ACK)) {
  436. if (success)
  437. tx_status.flags |= IEEE80211_TX_STATUS_ACK;
  438. else
  439. rt2x00dev->low_level_stats.dot11ACKFailureCount++;
  440. }
  441. tx_status.queue_length = entry->queue->limit;
  442. tx_status.queue_number = tx_status.control.queue;
  443. if (tx_status.control.flags & IEEE80211_TXCTL_USE_RTS_CTS) {
  444. if (success)
  445. rt2x00dev->low_level_stats.dot11RTSSuccessCount++;
  446. else
  447. rt2x00dev->low_level_stats.dot11RTSFailureCount++;
  448. }
  449. /*
  450. * Send the tx_status to mac80211 & debugfs.
  451. * mac80211 will clean up the skb structure.
  452. */
  453. get_skb_frame_desc(entry->skb)->frame_type = DUMP_FRAME_TXDONE;
  454. rt2x00debug_dump_frame(rt2x00dev, entry->skb);
  455. ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb, &tx_status);
  456. entry->skb = NULL;
  457. }
  458. EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
  459. void rt2x00lib_rxdone(struct queue_entry *entry,
  460. struct rxdone_entry_desc *rxdesc)
  461. {
  462. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  463. struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
  464. struct ieee80211_supported_band *sband;
  465. struct ieee80211_hdr *hdr;
  466. const struct rt2x00_rate *rate;
  467. unsigned int i;
  468. int idx = -1;
  469. u16 fc;
  470. /*
  471. * Update RX statistics.
  472. */
  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. /*
  477. * When frame was received with an OFDM bitrate,
  478. * the signal is the PLCP value. If it was received with
  479. * a CCK bitrate the signal is the rate in 100kbit/s.
  480. */
  481. if ((rxdesc->ofdm && rate->plcp == rxdesc->signal) ||
  482. (!rxdesc->ofdm && rate->bitrate == rxdesc->signal)) {
  483. idx = i;
  484. break;
  485. }
  486. }
  487. /*
  488. * Only update link status if this is a beacon frame carrying our bssid.
  489. */
  490. hdr = (struct ieee80211_hdr *)entry->skb->data;
  491. fc = le16_to_cpu(hdr->frame_control);
  492. if (is_beacon(fc) && rxdesc->my_bss)
  493. rt2x00lib_update_link_stats(&rt2x00dev->link, rxdesc->rssi);
  494. rt2x00dev->link.qual.rx_success++;
  495. rx_status->rate_idx = idx;
  496. rx_status->signal =
  497. rt2x00lib_calculate_link_signal(rt2x00dev, rxdesc->rssi);
  498. rx_status->ssi = rxdesc->rssi;
  499. rx_status->flag = rxdesc->flags;
  500. rx_status->antenna = rt2x00dev->link.ant.active.rx;
  501. /*
  502. * Send frame to mac80211 & debugfs.
  503. * mac80211 will clean up the skb structure.
  504. */
  505. get_skb_frame_desc(entry->skb)->frame_type = DUMP_FRAME_RXDONE;
  506. rt2x00debug_dump_frame(rt2x00dev, entry->skb);
  507. ieee80211_rx_irqsafe(rt2x00dev->hw, entry->skb, rx_status);
  508. entry->skb = NULL;
  509. }
  510. EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
  511. /*
  512. * TX descriptor initializer
  513. */
  514. void rt2x00lib_write_tx_desc(struct rt2x00_dev *rt2x00dev,
  515. struct sk_buff *skb,
  516. struct ieee80211_tx_control *control)
  517. {
  518. struct txentry_desc txdesc;
  519. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  520. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  521. const struct rt2x00_rate *rate;
  522. int tx_rate;
  523. int length;
  524. int duration;
  525. int residual;
  526. u16 frame_control;
  527. u16 seq_ctrl;
  528. memset(&txdesc, 0, sizeof(txdesc));
  529. txdesc.queue = skbdesc->entry->queue->qid;
  530. txdesc.cw_min = skbdesc->entry->queue->cw_min;
  531. txdesc.cw_max = skbdesc->entry->queue->cw_max;
  532. txdesc.aifs = skbdesc->entry->queue->aifs;
  533. /*
  534. * Read required fields from ieee80211 header.
  535. */
  536. frame_control = le16_to_cpu(hdr->frame_control);
  537. seq_ctrl = le16_to_cpu(hdr->seq_ctrl);
  538. tx_rate = control->tx_rate->hw_value;
  539. /*
  540. * Check whether this frame is to be acked
  541. */
  542. if (!(control->flags & IEEE80211_TXCTL_NO_ACK))
  543. __set_bit(ENTRY_TXD_ACK, &txdesc.flags);
  544. /*
  545. * Check if this is a RTS/CTS frame
  546. */
  547. if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
  548. __set_bit(ENTRY_TXD_BURST, &txdesc.flags);
  549. if (is_rts_frame(frame_control)) {
  550. __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc.flags);
  551. __set_bit(ENTRY_TXD_ACK, &txdesc.flags);
  552. } else
  553. __clear_bit(ENTRY_TXD_ACK, &txdesc.flags);
  554. if (control->rts_cts_rate)
  555. tx_rate = control->rts_cts_rate->hw_value;
  556. }
  557. rate = rt2x00_get_rate(tx_rate);
  558. /*
  559. * Check if more fragments are pending
  560. */
  561. if (ieee80211_get_morefrag(hdr)) {
  562. __set_bit(ENTRY_TXD_BURST, &txdesc.flags);
  563. __set_bit(ENTRY_TXD_MORE_FRAG, &txdesc.flags);
  564. }
  565. /*
  566. * Beacons and probe responses require the tsf timestamp
  567. * to be inserted into the frame.
  568. */
  569. if (control->queue == RT2X00_BCN_QUEUE_BEACON ||
  570. is_probe_resp(frame_control))
  571. __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc.flags);
  572. /*
  573. * Determine with what IFS priority this frame should be send.
  574. * Set ifs to IFS_SIFS when the this is not the first fragment,
  575. * or this fragment came after RTS/CTS.
  576. */
  577. if ((seq_ctrl & IEEE80211_SCTL_FRAG) > 0 ||
  578. test_bit(ENTRY_TXD_RTS_FRAME, &txdesc.flags))
  579. txdesc.ifs = IFS_SIFS;
  580. else
  581. txdesc.ifs = IFS_BACKOFF;
  582. /*
  583. * PLCP setup
  584. * Length calculation depends on OFDM/CCK rate.
  585. */
  586. txdesc.signal = rate->plcp;
  587. txdesc.service = 0x04;
  588. length = skb->len + FCS_LEN;
  589. if (rate->flags & DEV_RATE_OFDM) {
  590. __set_bit(ENTRY_TXD_OFDM_RATE, &txdesc.flags);
  591. txdesc.length_high = (length >> 6) & 0x3f;
  592. txdesc.length_low = length & 0x3f;
  593. } else {
  594. /*
  595. * Convert length to microseconds.
  596. */
  597. residual = get_duration_res(length, rate->bitrate);
  598. duration = get_duration(length, rate->bitrate);
  599. if (residual != 0) {
  600. duration++;
  601. /*
  602. * Check if we need to set the Length Extension
  603. */
  604. if (rate->bitrate == 110 && residual <= 30)
  605. txdesc.service |= 0x80;
  606. }
  607. txdesc.length_high = (duration >> 8) & 0xff;
  608. txdesc.length_low = duration & 0xff;
  609. /*
  610. * When preamble is enabled we should set the
  611. * preamble bit for the signal.
  612. */
  613. if (rt2x00_get_rate_preamble(tx_rate))
  614. txdesc.signal |= 0x08;
  615. }
  616. rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, skb, &txdesc, control);
  617. /*
  618. * Update queue entry.
  619. */
  620. skbdesc->entry->skb = skb;
  621. /*
  622. * The frame has been completely initialized and ready
  623. * for sending to the device. The caller will push the
  624. * frame to the device, but we are going to push the
  625. * frame to debugfs here.
  626. */
  627. skbdesc->frame_type = DUMP_FRAME_TX;
  628. rt2x00debug_dump_frame(rt2x00dev, skb);
  629. }
  630. EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc);
  631. /*
  632. * Driver initialization handlers.
  633. */
  634. const struct rt2x00_rate rt2x00_supported_rates[12] = {
  635. {
  636. .flags = 0,
  637. .bitrate = 10,
  638. .ratemask = DEV_RATEMASK_1MB,
  639. .plcp = 0x00,
  640. },
  641. {
  642. .flags = DEV_RATE_SHORT_PREAMBLE,
  643. .bitrate = 20,
  644. .ratemask = DEV_RATEMASK_2MB,
  645. .plcp = 0x01,
  646. },
  647. {
  648. .flags = DEV_RATE_SHORT_PREAMBLE,
  649. .bitrate = 55,
  650. .ratemask = DEV_RATEMASK_5_5MB,
  651. .plcp = 0x02,
  652. },
  653. {
  654. .flags = DEV_RATE_SHORT_PREAMBLE,
  655. .bitrate = 110,
  656. .ratemask = DEV_RATEMASK_11MB,
  657. .plcp = 0x03,
  658. },
  659. {
  660. .flags = DEV_RATE_OFDM,
  661. .bitrate = 60,
  662. .ratemask = DEV_RATEMASK_6MB,
  663. .plcp = 0x0b,
  664. },
  665. {
  666. .flags = DEV_RATE_OFDM,
  667. .bitrate = 90,
  668. .ratemask = DEV_RATEMASK_9MB,
  669. .plcp = 0x0f,
  670. },
  671. {
  672. .flags = DEV_RATE_OFDM,
  673. .bitrate = 120,
  674. .ratemask = DEV_RATEMASK_12MB,
  675. .plcp = 0x0a,
  676. },
  677. {
  678. .flags = DEV_RATE_OFDM,
  679. .bitrate = 180,
  680. .ratemask = DEV_RATEMASK_18MB,
  681. .plcp = 0x0e,
  682. },
  683. {
  684. .flags = DEV_RATE_OFDM,
  685. .bitrate = 240,
  686. .ratemask = DEV_RATEMASK_24MB,
  687. .plcp = 0x09,
  688. },
  689. {
  690. .flags = DEV_RATE_OFDM,
  691. .bitrate = 360,
  692. .ratemask = DEV_RATEMASK_36MB,
  693. .plcp = 0x0d,
  694. },
  695. {
  696. .flags = DEV_RATE_OFDM,
  697. .bitrate = 480,
  698. .ratemask = DEV_RATEMASK_48MB,
  699. .plcp = 0x08,
  700. },
  701. {
  702. .flags = DEV_RATE_OFDM,
  703. .bitrate = 540,
  704. .ratemask = DEV_RATEMASK_54MB,
  705. .plcp = 0x0c,
  706. },
  707. };
  708. static void rt2x00lib_channel(struct ieee80211_channel *entry,
  709. const int channel, const int tx_power,
  710. const int value)
  711. {
  712. if (channel <= 14)
  713. entry->center_freq = 2407 + (5 * channel);
  714. else
  715. entry->center_freq = 5000 + (5 * channel);
  716. entry->hw_value = value;
  717. entry->max_power = tx_power;
  718. entry->max_antenna_gain = 0xff;
  719. }
  720. static void rt2x00lib_rate(struct ieee80211_rate *entry,
  721. const u16 index, const struct rt2x00_rate *rate)
  722. {
  723. entry->flags = 0;
  724. entry->bitrate = rate->bitrate;
  725. entry->hw_value = rt2x00_create_rate_hw_value(index, 0);
  726. entry->hw_value_short = entry->hw_value;
  727. if (rate->flags & DEV_RATE_SHORT_PREAMBLE) {
  728. entry->flags |= IEEE80211_RATE_SHORT_PREAMBLE;
  729. entry->hw_value_short |= rt2x00_create_rate_hw_value(index, 1);
  730. }
  731. }
  732. static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
  733. struct hw_mode_spec *spec)
  734. {
  735. struct ieee80211_hw *hw = rt2x00dev->hw;
  736. struct ieee80211_supported_band *sbands;
  737. struct ieee80211_channel *channels;
  738. struct ieee80211_rate *rates;
  739. unsigned int i;
  740. unsigned char tx_power;
  741. sbands = &rt2x00dev->bands[0];
  742. channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
  743. if (!channels)
  744. return -ENOMEM;
  745. rates = kzalloc(sizeof(*rates) * spec->num_rates, GFP_KERNEL);
  746. if (!rates)
  747. goto exit_free_channels;
  748. /*
  749. * Initialize Rate list.
  750. */
  751. for (i = 0; i < spec->num_rates; i++)
  752. rt2x00lib_rate(&rates[0], i, rt2x00_get_rate(i));
  753. /*
  754. * Initialize Channel list.
  755. */
  756. for (i = 0; i < spec->num_channels; i++) {
  757. if (spec->channels[i].channel <= 14)
  758. tx_power = spec->tx_power_bg[i];
  759. else if (spec->tx_power_a)
  760. tx_power = spec->tx_power_a[i];
  761. else
  762. tx_power = spec->tx_power_default;
  763. rt2x00lib_channel(&channels[i],
  764. spec->channels[i].channel, tx_power, i);
  765. }
  766. /*
  767. * Intitialize 802.11b
  768. * Rates: CCK.
  769. * Channels: 2.4 GHz
  770. */
  771. if (spec->num_modes > 0) {
  772. sbands[IEEE80211_BAND_2GHZ].n_channels = 14;
  773. sbands[IEEE80211_BAND_2GHZ].n_bitrates = 4;
  774. sbands[IEEE80211_BAND_2GHZ].channels = channels;
  775. sbands[IEEE80211_BAND_2GHZ].bitrates = rates;
  776. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &rt2x00dev->bands[IEEE80211_BAND_2GHZ];
  777. }
  778. /*
  779. * Intitialize 802.11g
  780. * Rates: CCK, OFDM.
  781. * Channels: 2.4 GHz
  782. */
  783. if (spec->num_modes > 1) {
  784. sbands[IEEE80211_BAND_2GHZ].n_channels = 14;
  785. sbands[IEEE80211_BAND_2GHZ].n_bitrates = spec->num_rates;
  786. sbands[IEEE80211_BAND_2GHZ].channels = channels;
  787. sbands[IEEE80211_BAND_2GHZ].bitrates = rates;
  788. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &rt2x00dev->bands[IEEE80211_BAND_2GHZ];
  789. }
  790. /*
  791. * Intitialize 802.11a
  792. * Rates: OFDM.
  793. * Channels: OFDM, UNII, HiperLAN2.
  794. */
  795. if (spec->num_modes > 2) {
  796. sbands[IEEE80211_BAND_5GHZ].n_channels = spec->num_channels - 14;
  797. sbands[IEEE80211_BAND_5GHZ].n_bitrates = spec->num_rates - 4;
  798. sbands[IEEE80211_BAND_5GHZ].channels = &channels[14];
  799. sbands[IEEE80211_BAND_5GHZ].bitrates = &rates[4];
  800. hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &rt2x00dev->bands[IEEE80211_BAND_5GHZ];
  801. }
  802. return 0;
  803. exit_free_channels:
  804. kfree(channels);
  805. ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
  806. return -ENOMEM;
  807. }
  808. static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
  809. {
  810. if (test_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags))
  811. ieee80211_unregister_hw(rt2x00dev->hw);
  812. if (likely(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ])) {
  813. kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->channels);
  814. kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->bitrates);
  815. rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
  816. rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
  817. }
  818. }
  819. static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
  820. {
  821. struct hw_mode_spec *spec = &rt2x00dev->spec;
  822. int status;
  823. /*
  824. * Initialize HW modes.
  825. */
  826. status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
  827. if (status)
  828. return status;
  829. /*
  830. * Register HW.
  831. */
  832. status = ieee80211_register_hw(rt2x00dev->hw);
  833. if (status) {
  834. rt2x00lib_remove_hw(rt2x00dev);
  835. return status;
  836. }
  837. __set_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags);
  838. return 0;
  839. }
  840. /*
  841. * Initialization/uninitialization handlers.
  842. */
  843. static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
  844. {
  845. if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
  846. return;
  847. /*
  848. * Unregister rfkill.
  849. */
  850. rt2x00rfkill_unregister(rt2x00dev);
  851. /*
  852. * Allow the HW to uninitialize.
  853. */
  854. rt2x00dev->ops->lib->uninitialize(rt2x00dev);
  855. /*
  856. * Free allocated queue entries.
  857. */
  858. rt2x00queue_uninitialize(rt2x00dev);
  859. }
  860. static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
  861. {
  862. int status;
  863. if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
  864. return 0;
  865. /*
  866. * Allocate all queue entries.
  867. */
  868. status = rt2x00queue_initialize(rt2x00dev);
  869. if (status)
  870. return status;
  871. /*
  872. * Initialize the device.
  873. */
  874. status = rt2x00dev->ops->lib->initialize(rt2x00dev);
  875. if (status)
  876. goto exit;
  877. __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
  878. /*
  879. * Register the rfkill handler.
  880. */
  881. status = rt2x00rfkill_register(rt2x00dev);
  882. if (status)
  883. goto exit;
  884. return 0;
  885. exit:
  886. rt2x00lib_uninitialize(rt2x00dev);
  887. return status;
  888. }
  889. int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
  890. {
  891. int retval;
  892. if (test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  893. return 0;
  894. /*
  895. * If this is the first interface which is added,
  896. * we should load the firmware now.
  897. */
  898. retval = rt2x00lib_load_firmware(rt2x00dev);
  899. if (retval)
  900. return retval;
  901. /*
  902. * Initialize the device.
  903. */
  904. retval = rt2x00lib_initialize(rt2x00dev);
  905. if (retval)
  906. return retval;
  907. /*
  908. * Enable radio.
  909. */
  910. retval = rt2x00lib_enable_radio(rt2x00dev);
  911. if (retval) {
  912. rt2x00lib_uninitialize(rt2x00dev);
  913. return retval;
  914. }
  915. rt2x00dev->intf_ap_count = 0;
  916. rt2x00dev->intf_sta_count = 0;
  917. rt2x00dev->intf_associated = 0;
  918. __set_bit(DEVICE_STARTED, &rt2x00dev->flags);
  919. return 0;
  920. }
  921. void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
  922. {
  923. if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  924. return;
  925. /*
  926. * Perhaps we can add something smarter here,
  927. * but for now just disabling the radio should do.
  928. */
  929. rt2x00lib_disable_radio(rt2x00dev);
  930. rt2x00dev->intf_ap_count = 0;
  931. rt2x00dev->intf_sta_count = 0;
  932. rt2x00dev->intf_associated = 0;
  933. __clear_bit(DEVICE_STARTED, &rt2x00dev->flags);
  934. }
  935. /*
  936. * driver allocation handlers.
  937. */
  938. int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
  939. {
  940. int retval = -ENOMEM;
  941. /*
  942. * Make room for rt2x00_intf inside the per-interface
  943. * structure ieee80211_vif.
  944. */
  945. rt2x00dev->hw->vif_data_size = sizeof(struct rt2x00_intf);
  946. /*
  947. * Let the driver probe the device to detect the capabilities.
  948. */
  949. retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
  950. if (retval) {
  951. ERROR(rt2x00dev, "Failed to allocate device.\n");
  952. goto exit;
  953. }
  954. /*
  955. * Initialize configuration work.
  956. */
  957. INIT_WORK(&rt2x00dev->intf_work, rt2x00lib_intf_scheduled);
  958. INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
  959. INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
  960. /*
  961. * Allocate queue array.
  962. */
  963. retval = rt2x00queue_allocate(rt2x00dev);
  964. if (retval)
  965. goto exit;
  966. /*
  967. * Initialize ieee80211 structure.
  968. */
  969. retval = rt2x00lib_probe_hw(rt2x00dev);
  970. if (retval) {
  971. ERROR(rt2x00dev, "Failed to initialize hw.\n");
  972. goto exit;
  973. }
  974. /*
  975. * Allocatie rfkill.
  976. */
  977. retval = rt2x00rfkill_allocate(rt2x00dev);
  978. if (retval)
  979. goto exit;
  980. /*
  981. * Open the debugfs entry.
  982. */
  983. rt2x00debug_register(rt2x00dev);
  984. __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  985. return 0;
  986. exit:
  987. rt2x00lib_remove_dev(rt2x00dev);
  988. return retval;
  989. }
  990. EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
  991. void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
  992. {
  993. __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  994. /*
  995. * Disable radio.
  996. */
  997. rt2x00lib_disable_radio(rt2x00dev);
  998. /*
  999. * Uninitialize device.
  1000. */
  1001. rt2x00lib_uninitialize(rt2x00dev);
  1002. /*
  1003. * Close debugfs entry.
  1004. */
  1005. rt2x00debug_deregister(rt2x00dev);
  1006. /*
  1007. * Free rfkill
  1008. */
  1009. rt2x00rfkill_free(rt2x00dev);
  1010. /*
  1011. * Free ieee80211_hw memory.
  1012. */
  1013. rt2x00lib_remove_hw(rt2x00dev);
  1014. /*
  1015. * Free firmware image.
  1016. */
  1017. rt2x00lib_free_firmware(rt2x00dev);
  1018. /*
  1019. * Free queue structures.
  1020. */
  1021. rt2x00queue_free(rt2x00dev);
  1022. }
  1023. EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
  1024. /*
  1025. * Device state handlers
  1026. */
  1027. #ifdef CONFIG_PM
  1028. int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
  1029. {
  1030. int retval;
  1031. NOTICE(rt2x00dev, "Going to sleep.\n");
  1032. __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  1033. /*
  1034. * Only continue if mac80211 has open interfaces.
  1035. */
  1036. if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  1037. goto exit;
  1038. __set_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags);
  1039. /*
  1040. * Disable radio and unitialize all items
  1041. * that must be recreated on resume.
  1042. */
  1043. rt2x00lib_stop(rt2x00dev);
  1044. rt2x00lib_uninitialize(rt2x00dev);
  1045. rt2x00debug_deregister(rt2x00dev);
  1046. exit:
  1047. /*
  1048. * Set device mode to sleep for power management.
  1049. */
  1050. retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
  1051. if (retval)
  1052. return retval;
  1053. return 0;
  1054. }
  1055. EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
  1056. static void rt2x00lib_resume_intf(void *data, u8 *mac,
  1057. struct ieee80211_vif *vif)
  1058. {
  1059. struct rt2x00_dev *rt2x00dev = data;
  1060. struct rt2x00_intf *intf = vif_to_intf(vif);
  1061. spin_lock(&intf->lock);
  1062. rt2x00lib_config_intf(rt2x00dev, intf,
  1063. vif->type, intf->mac, intf->bssid);
  1064. /*
  1065. * Master or Ad-hoc mode require a new beacon update.
  1066. */
  1067. if (vif->type == IEEE80211_IF_TYPE_AP ||
  1068. vif->type == IEEE80211_IF_TYPE_IBSS)
  1069. intf->delayed_flags |= DELAYED_UPDATE_BEACON;
  1070. spin_unlock(&intf->lock);
  1071. }
  1072. int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
  1073. {
  1074. int retval;
  1075. NOTICE(rt2x00dev, "Waking up.\n");
  1076. /*
  1077. * Open the debugfs entry.
  1078. */
  1079. rt2x00debug_register(rt2x00dev);
  1080. /*
  1081. * Only continue if mac80211 had open interfaces.
  1082. */
  1083. if (!__test_and_clear_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags))
  1084. return 0;
  1085. /*
  1086. * Reinitialize device and all active interfaces.
  1087. */
  1088. retval = rt2x00lib_start(rt2x00dev);
  1089. if (retval)
  1090. goto exit;
  1091. /*
  1092. * Reconfigure device.
  1093. */
  1094. rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1);
  1095. if (!rt2x00dev->hw->conf.radio_enabled)
  1096. rt2x00lib_disable_radio(rt2x00dev);
  1097. /*
  1098. * Iterator over each active interface to
  1099. * reconfigure the hardware.
  1100. */
  1101. ieee80211_iterate_active_interfaces(rt2x00dev->hw,
  1102. rt2x00lib_resume_intf, rt2x00dev);
  1103. /*
  1104. * We are ready again to receive requests from mac80211.
  1105. */
  1106. __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  1107. /*
  1108. * It is possible that during that mac80211 has attempted
  1109. * to send frames while we were suspending or resuming.
  1110. * In that case we have disabled the TX queue and should
  1111. * now enable it again
  1112. */
  1113. ieee80211_start_queues(rt2x00dev->hw);
  1114. /*
  1115. * During interface iteration we might have changed the
  1116. * delayed_flags, time to handles the event by calling
  1117. * the work handler directly.
  1118. */
  1119. rt2x00lib_intf_scheduled(&rt2x00dev->intf_work);
  1120. return 0;
  1121. exit:
  1122. rt2x00lib_disable_radio(rt2x00dev);
  1123. rt2x00lib_uninitialize(rt2x00dev);
  1124. rt2x00debug_deregister(rt2x00dev);
  1125. return retval;
  1126. }
  1127. EXPORT_SYMBOL_GPL(rt2x00lib_resume);
  1128. #endif /* CONFIG_PM */
  1129. /*
  1130. * rt2x00lib module information.
  1131. */
  1132. MODULE_AUTHOR(DRV_PROJECT);
  1133. MODULE_VERSION(DRV_VERSION);
  1134. MODULE_DESCRIPTION("rt2x00 library");
  1135. MODULE_LICENSE("GPL");