rt2x00dev.c 33 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_hw_mode *mode;
  465. struct ieee80211_rate *rate;
  466. struct ieee80211_hdr *hdr;
  467. unsigned int i;
  468. int val = 0;
  469. u16 fc;
  470. /*
  471. * Update RX statistics.
  472. */
  473. mode = &rt2x00dev->hwmodes[rt2x00dev->curr_hwmode];
  474. for (i = 0; i < mode->num_rates; i++) {
  475. rate = &mode->rates[i];
  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 0.5kbit/s.
  480. */
  481. if (!rxdesc->ofdm)
  482. val = DEVICE_GET_RATE_FIELD(rate->val, RATE);
  483. else
  484. val = DEVICE_GET_RATE_FIELD(rate->val, PLCP);
  485. if (val == rxdesc->signal) {
  486. val = rate->val;
  487. break;
  488. }
  489. }
  490. /*
  491. * Only update link status if this is a beacon frame carrying our bssid.
  492. */
  493. hdr = (struct ieee80211_hdr*)entry->skb->data;
  494. fc = le16_to_cpu(hdr->frame_control);
  495. if (is_beacon(fc) && rxdesc->my_bss)
  496. rt2x00lib_update_link_stats(&rt2x00dev->link, rxdesc->rssi);
  497. rt2x00dev->link.qual.rx_success++;
  498. rx_status->rate = val;
  499. rx_status->signal =
  500. rt2x00lib_calculate_link_signal(rt2x00dev, rxdesc->rssi);
  501. rx_status->ssi = rxdesc->rssi;
  502. rx_status->flag = rxdesc->flags;
  503. rx_status->antenna = rt2x00dev->link.ant.active.rx;
  504. /*
  505. * Send frame to mac80211 & debugfs.
  506. * mac80211 will clean up the skb structure.
  507. */
  508. get_skb_frame_desc(entry->skb)->frame_type = DUMP_FRAME_RXDONE;
  509. rt2x00debug_dump_frame(rt2x00dev, entry->skb);
  510. ieee80211_rx_irqsafe(rt2x00dev->hw, entry->skb, rx_status);
  511. entry->skb = NULL;
  512. }
  513. EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
  514. /*
  515. * TX descriptor initializer
  516. */
  517. void rt2x00lib_write_tx_desc(struct rt2x00_dev *rt2x00dev,
  518. struct sk_buff *skb,
  519. struct ieee80211_tx_control *control)
  520. {
  521. struct txentry_desc txdesc;
  522. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  523. struct ieee80211_hdr *ieee80211hdr = (struct ieee80211_hdr *)skb->data;
  524. int tx_rate;
  525. int bitrate;
  526. int length;
  527. int duration;
  528. int residual;
  529. u16 frame_control;
  530. u16 seq_ctrl;
  531. memset(&txdesc, 0, sizeof(txdesc));
  532. txdesc.cw_min = skbdesc->entry->queue->cw_min;
  533. txdesc.cw_max = skbdesc->entry->queue->cw_max;
  534. txdesc.aifs = skbdesc->entry->queue->aifs;
  535. /*
  536. * Identify queue
  537. */
  538. if (control->queue < rt2x00dev->hw->queues)
  539. txdesc.queue = control->queue;
  540. else if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
  541. control->queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
  542. txdesc.queue = QID_MGMT;
  543. else
  544. txdesc.queue = QID_OTHER;
  545. /*
  546. * Read required fields from ieee80211 header.
  547. */
  548. frame_control = le16_to_cpu(ieee80211hdr->frame_control);
  549. seq_ctrl = le16_to_cpu(ieee80211hdr->seq_ctrl);
  550. tx_rate = control->tx_rate;
  551. /*
  552. * Check whether this frame is to be acked
  553. */
  554. if (!(control->flags & IEEE80211_TXCTL_NO_ACK))
  555. __set_bit(ENTRY_TXD_ACK, &txdesc.flags);
  556. /*
  557. * Check if this is a RTS/CTS frame
  558. */
  559. if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
  560. __set_bit(ENTRY_TXD_BURST, &txdesc.flags);
  561. if (is_rts_frame(frame_control)) {
  562. __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc.flags);
  563. __set_bit(ENTRY_TXD_ACK, &txdesc.flags);
  564. } else
  565. __clear_bit(ENTRY_TXD_ACK, &txdesc.flags);
  566. if (control->rts_cts_rate)
  567. tx_rate = control->rts_cts_rate;
  568. }
  569. /*
  570. * Check for OFDM
  571. */
  572. if (DEVICE_GET_RATE_FIELD(tx_rate, RATEMASK) & DEV_OFDM_RATEMASK)
  573. __set_bit(ENTRY_TXD_OFDM_RATE, &txdesc.flags);
  574. /*
  575. * Check if more fragments are pending
  576. */
  577. if (ieee80211_get_morefrag(ieee80211hdr)) {
  578. __set_bit(ENTRY_TXD_BURST, &txdesc.flags);
  579. __set_bit(ENTRY_TXD_MORE_FRAG, &txdesc.flags);
  580. }
  581. /*
  582. * Beacons and probe responses require the tsf timestamp
  583. * to be inserted into the frame.
  584. */
  585. if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
  586. is_probe_resp(frame_control))
  587. __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc.flags);
  588. /*
  589. * Determine with what IFS priority this frame should be send.
  590. * Set ifs to IFS_SIFS when the this is not the first fragment,
  591. * or this fragment came after RTS/CTS.
  592. */
  593. if ((seq_ctrl & IEEE80211_SCTL_FRAG) > 0 ||
  594. test_bit(ENTRY_TXD_RTS_FRAME, &txdesc.flags))
  595. txdesc.ifs = IFS_SIFS;
  596. else
  597. txdesc.ifs = IFS_BACKOFF;
  598. /*
  599. * PLCP setup
  600. * Length calculation depends on OFDM/CCK rate.
  601. */
  602. txdesc.signal = DEVICE_GET_RATE_FIELD(tx_rate, PLCP);
  603. txdesc.service = 0x04;
  604. length = skb->len + FCS_LEN;
  605. if (test_bit(ENTRY_TXD_OFDM_RATE, &txdesc.flags)) {
  606. txdesc.length_high = (length >> 6) & 0x3f;
  607. txdesc.length_low = length & 0x3f;
  608. } else {
  609. bitrate = DEVICE_GET_RATE_FIELD(tx_rate, RATE);
  610. /*
  611. * Convert length to microseconds.
  612. */
  613. residual = get_duration_res(length, bitrate);
  614. duration = get_duration(length, bitrate);
  615. if (residual != 0) {
  616. duration++;
  617. /*
  618. * Check if we need to set the Length Extension
  619. */
  620. if (bitrate == 110 && residual <= 30)
  621. txdesc.service |= 0x80;
  622. }
  623. txdesc.length_high = (duration >> 8) & 0xff;
  624. txdesc.length_low = duration & 0xff;
  625. /*
  626. * When preamble is enabled we should set the
  627. * preamble bit for the signal.
  628. */
  629. if (DEVICE_GET_RATE_FIELD(tx_rate, PREAMBLE))
  630. txdesc.signal |= 0x08;
  631. }
  632. rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, skb, &txdesc, control);
  633. /*
  634. * Update queue entry.
  635. */
  636. skbdesc->entry->skb = skb;
  637. /*
  638. * The frame has been completely initialized and ready
  639. * for sending to the device. The caller will push the
  640. * frame to the device, but we are going to push the
  641. * frame to debugfs here.
  642. */
  643. skbdesc->frame_type = DUMP_FRAME_TX;
  644. rt2x00debug_dump_frame(rt2x00dev, skb);
  645. }
  646. EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc);
  647. /*
  648. * Driver initialization handlers.
  649. */
  650. static void rt2x00lib_channel(struct ieee80211_channel *entry,
  651. const int channel, const int tx_power,
  652. const int value)
  653. {
  654. entry->chan = channel;
  655. if (channel <= 14)
  656. entry->freq = 2407 + (5 * channel);
  657. else
  658. entry->freq = 5000 + (5 * channel);
  659. entry->val = value;
  660. entry->flag =
  661. IEEE80211_CHAN_W_IBSS |
  662. IEEE80211_CHAN_W_ACTIVE_SCAN |
  663. IEEE80211_CHAN_W_SCAN;
  664. entry->power_level = tx_power;
  665. entry->antenna_max = 0xff;
  666. }
  667. static void rt2x00lib_rate(struct ieee80211_rate *entry,
  668. const int rate, const int mask,
  669. const int plcp, const int flags)
  670. {
  671. entry->rate = rate;
  672. entry->val =
  673. DEVICE_SET_RATE_FIELD(rate, RATE) |
  674. DEVICE_SET_RATE_FIELD(mask, RATEMASK) |
  675. DEVICE_SET_RATE_FIELD(plcp, PLCP);
  676. entry->flags = flags;
  677. entry->val2 = entry->val;
  678. if (entry->flags & IEEE80211_RATE_PREAMBLE2)
  679. entry->val2 |= DEVICE_SET_RATE_FIELD(1, PREAMBLE);
  680. entry->min_rssi_ack = 0;
  681. entry->min_rssi_ack_delta = 0;
  682. }
  683. static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
  684. struct hw_mode_spec *spec)
  685. {
  686. struct ieee80211_hw *hw = rt2x00dev->hw;
  687. struct ieee80211_hw_mode *hwmodes;
  688. struct ieee80211_channel *channels;
  689. struct ieee80211_rate *rates;
  690. unsigned int i;
  691. unsigned char tx_power;
  692. hwmodes = kzalloc(sizeof(*hwmodes) * spec->num_modes, GFP_KERNEL);
  693. if (!hwmodes)
  694. goto exit;
  695. channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
  696. if (!channels)
  697. goto exit_free_modes;
  698. rates = kzalloc(sizeof(*rates) * spec->num_rates, GFP_KERNEL);
  699. if (!rates)
  700. goto exit_free_channels;
  701. /*
  702. * Initialize Rate list.
  703. */
  704. rt2x00lib_rate(&rates[0], 10, DEV_RATEMASK_1MB,
  705. 0x00, IEEE80211_RATE_CCK);
  706. rt2x00lib_rate(&rates[1], 20, DEV_RATEMASK_2MB,
  707. 0x01, IEEE80211_RATE_CCK_2);
  708. rt2x00lib_rate(&rates[2], 55, DEV_RATEMASK_5_5MB,
  709. 0x02, IEEE80211_RATE_CCK_2);
  710. rt2x00lib_rate(&rates[3], 110, DEV_RATEMASK_11MB,
  711. 0x03, IEEE80211_RATE_CCK_2);
  712. if (spec->num_rates > 4) {
  713. rt2x00lib_rate(&rates[4], 60, DEV_RATEMASK_6MB,
  714. 0x0b, IEEE80211_RATE_OFDM);
  715. rt2x00lib_rate(&rates[5], 90, DEV_RATEMASK_9MB,
  716. 0x0f, IEEE80211_RATE_OFDM);
  717. rt2x00lib_rate(&rates[6], 120, DEV_RATEMASK_12MB,
  718. 0x0a, IEEE80211_RATE_OFDM);
  719. rt2x00lib_rate(&rates[7], 180, DEV_RATEMASK_18MB,
  720. 0x0e, IEEE80211_RATE_OFDM);
  721. rt2x00lib_rate(&rates[8], 240, DEV_RATEMASK_24MB,
  722. 0x09, IEEE80211_RATE_OFDM);
  723. rt2x00lib_rate(&rates[9], 360, DEV_RATEMASK_36MB,
  724. 0x0d, IEEE80211_RATE_OFDM);
  725. rt2x00lib_rate(&rates[10], 480, DEV_RATEMASK_48MB,
  726. 0x08, IEEE80211_RATE_OFDM);
  727. rt2x00lib_rate(&rates[11], 540, DEV_RATEMASK_54MB,
  728. 0x0c, IEEE80211_RATE_OFDM);
  729. }
  730. /*
  731. * Initialize Channel list.
  732. */
  733. for (i = 0; i < spec->num_channels; i++) {
  734. if (spec->channels[i].channel <= 14)
  735. tx_power = spec->tx_power_bg[i];
  736. else if (spec->tx_power_a)
  737. tx_power = spec->tx_power_a[i];
  738. else
  739. tx_power = spec->tx_power_default;
  740. rt2x00lib_channel(&channels[i],
  741. spec->channels[i].channel, tx_power, i);
  742. }
  743. /*
  744. * Intitialize 802.11b
  745. * Rates: CCK.
  746. * Channels: OFDM.
  747. */
  748. if (spec->num_modes > HWMODE_B) {
  749. hwmodes[HWMODE_B].mode = MODE_IEEE80211B;
  750. hwmodes[HWMODE_B].num_channels = 14;
  751. hwmodes[HWMODE_B].num_rates = 4;
  752. hwmodes[HWMODE_B].channels = channels;
  753. hwmodes[HWMODE_B].rates = rates;
  754. }
  755. /*
  756. * Intitialize 802.11g
  757. * Rates: CCK, OFDM.
  758. * Channels: OFDM.
  759. */
  760. if (spec->num_modes > HWMODE_G) {
  761. hwmodes[HWMODE_G].mode = MODE_IEEE80211G;
  762. hwmodes[HWMODE_G].num_channels = 14;
  763. hwmodes[HWMODE_G].num_rates = spec->num_rates;
  764. hwmodes[HWMODE_G].channels = channels;
  765. hwmodes[HWMODE_G].rates = rates;
  766. }
  767. /*
  768. * Intitialize 802.11a
  769. * Rates: OFDM.
  770. * Channels: OFDM, UNII, HiperLAN2.
  771. */
  772. if (spec->num_modes > HWMODE_A) {
  773. hwmodes[HWMODE_A].mode = MODE_IEEE80211A;
  774. hwmodes[HWMODE_A].num_channels = spec->num_channels - 14;
  775. hwmodes[HWMODE_A].num_rates = spec->num_rates - 4;
  776. hwmodes[HWMODE_A].channels = &channels[14];
  777. hwmodes[HWMODE_A].rates = &rates[4];
  778. }
  779. if (spec->num_modes > HWMODE_G &&
  780. ieee80211_register_hwmode(hw, &hwmodes[HWMODE_G]))
  781. goto exit_free_rates;
  782. if (spec->num_modes > HWMODE_B &&
  783. ieee80211_register_hwmode(hw, &hwmodes[HWMODE_B]))
  784. goto exit_free_rates;
  785. if (spec->num_modes > HWMODE_A &&
  786. ieee80211_register_hwmode(hw, &hwmodes[HWMODE_A]))
  787. goto exit_free_rates;
  788. rt2x00dev->hwmodes = hwmodes;
  789. return 0;
  790. exit_free_rates:
  791. kfree(rates);
  792. exit_free_channels:
  793. kfree(channels);
  794. exit_free_modes:
  795. kfree(hwmodes);
  796. exit:
  797. ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
  798. return -ENOMEM;
  799. }
  800. static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
  801. {
  802. if (test_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags))
  803. ieee80211_unregister_hw(rt2x00dev->hw);
  804. if (likely(rt2x00dev->hwmodes)) {
  805. kfree(rt2x00dev->hwmodes->channels);
  806. kfree(rt2x00dev->hwmodes->rates);
  807. kfree(rt2x00dev->hwmodes);
  808. rt2x00dev->hwmodes = NULL;
  809. }
  810. }
  811. static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
  812. {
  813. struct hw_mode_spec *spec = &rt2x00dev->spec;
  814. int status;
  815. /*
  816. * Initialize HW modes.
  817. */
  818. status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
  819. if (status)
  820. return status;
  821. /*
  822. * Register HW.
  823. */
  824. status = ieee80211_register_hw(rt2x00dev->hw);
  825. if (status) {
  826. rt2x00lib_remove_hw(rt2x00dev);
  827. return status;
  828. }
  829. __set_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags);
  830. return 0;
  831. }
  832. /*
  833. * Initialization/uninitialization handlers.
  834. */
  835. static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
  836. {
  837. if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
  838. return;
  839. /*
  840. * Unregister rfkill.
  841. */
  842. rt2x00rfkill_unregister(rt2x00dev);
  843. /*
  844. * Allow the HW to uninitialize.
  845. */
  846. rt2x00dev->ops->lib->uninitialize(rt2x00dev);
  847. /*
  848. * Free allocated queue entries.
  849. */
  850. rt2x00queue_uninitialize(rt2x00dev);
  851. }
  852. static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
  853. {
  854. int status;
  855. if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
  856. return 0;
  857. /*
  858. * Allocate all queue entries.
  859. */
  860. status = rt2x00queue_initialize(rt2x00dev);
  861. if (status)
  862. return status;
  863. /*
  864. * Initialize the device.
  865. */
  866. status = rt2x00dev->ops->lib->initialize(rt2x00dev);
  867. if (status)
  868. goto exit;
  869. __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
  870. /*
  871. * Register the rfkill handler.
  872. */
  873. status = rt2x00rfkill_register(rt2x00dev);
  874. if (status)
  875. goto exit;
  876. return 0;
  877. exit:
  878. rt2x00lib_uninitialize(rt2x00dev);
  879. return status;
  880. }
  881. int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
  882. {
  883. int retval;
  884. if (test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  885. return 0;
  886. /*
  887. * If this is the first interface which is added,
  888. * we should load the firmware now.
  889. */
  890. retval = rt2x00lib_load_firmware(rt2x00dev);
  891. if (retval)
  892. return retval;
  893. /*
  894. * Initialize the device.
  895. */
  896. retval = rt2x00lib_initialize(rt2x00dev);
  897. if (retval)
  898. return retval;
  899. /*
  900. * Enable radio.
  901. */
  902. retval = rt2x00lib_enable_radio(rt2x00dev);
  903. if (retval) {
  904. rt2x00lib_uninitialize(rt2x00dev);
  905. return retval;
  906. }
  907. rt2x00dev->intf_ap_count = 0;
  908. rt2x00dev->intf_sta_count = 0;
  909. rt2x00dev->intf_associated = 0;
  910. __set_bit(DEVICE_STARTED, &rt2x00dev->flags);
  911. return 0;
  912. }
  913. void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
  914. {
  915. if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  916. return;
  917. /*
  918. * Perhaps we can add something smarter here,
  919. * but for now just disabling the radio should do.
  920. */
  921. rt2x00lib_disable_radio(rt2x00dev);
  922. rt2x00dev->intf_ap_count = 0;
  923. rt2x00dev->intf_sta_count = 0;
  924. rt2x00dev->intf_associated = 0;
  925. __clear_bit(DEVICE_STARTED, &rt2x00dev->flags);
  926. }
  927. /*
  928. * driver allocation handlers.
  929. */
  930. int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
  931. {
  932. int retval = -ENOMEM;
  933. /*
  934. * Make room for rt2x00_intf inside the per-interface
  935. * structure ieee80211_vif.
  936. */
  937. rt2x00dev->hw->vif_data_size = sizeof(struct rt2x00_intf);
  938. /*
  939. * Let the driver probe the device to detect the capabilities.
  940. */
  941. retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
  942. if (retval) {
  943. ERROR(rt2x00dev, "Failed to allocate device.\n");
  944. goto exit;
  945. }
  946. /*
  947. * Initialize configuration work.
  948. */
  949. INIT_WORK(&rt2x00dev->intf_work, rt2x00lib_intf_scheduled);
  950. INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
  951. INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
  952. /*
  953. * Allocate queue array.
  954. */
  955. retval = rt2x00queue_allocate(rt2x00dev);
  956. if (retval)
  957. goto exit;
  958. /*
  959. * Initialize ieee80211 structure.
  960. */
  961. retval = rt2x00lib_probe_hw(rt2x00dev);
  962. if (retval) {
  963. ERROR(rt2x00dev, "Failed to initialize hw.\n");
  964. goto exit;
  965. }
  966. /*
  967. * Allocatie rfkill.
  968. */
  969. retval = rt2x00rfkill_allocate(rt2x00dev);
  970. if (retval)
  971. goto exit;
  972. /*
  973. * Open the debugfs entry.
  974. */
  975. rt2x00debug_register(rt2x00dev);
  976. __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  977. return 0;
  978. exit:
  979. rt2x00lib_remove_dev(rt2x00dev);
  980. return retval;
  981. }
  982. EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
  983. void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
  984. {
  985. __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  986. /*
  987. * Disable radio.
  988. */
  989. rt2x00lib_disable_radio(rt2x00dev);
  990. /*
  991. * Uninitialize device.
  992. */
  993. rt2x00lib_uninitialize(rt2x00dev);
  994. /*
  995. * Close debugfs entry.
  996. */
  997. rt2x00debug_deregister(rt2x00dev);
  998. /*
  999. * Free rfkill
  1000. */
  1001. rt2x00rfkill_free(rt2x00dev);
  1002. /*
  1003. * Free ieee80211_hw memory.
  1004. */
  1005. rt2x00lib_remove_hw(rt2x00dev);
  1006. /*
  1007. * Free firmware image.
  1008. */
  1009. rt2x00lib_free_firmware(rt2x00dev);
  1010. /*
  1011. * Free queue structures.
  1012. */
  1013. rt2x00queue_free(rt2x00dev);
  1014. }
  1015. EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
  1016. /*
  1017. * Device state handlers
  1018. */
  1019. #ifdef CONFIG_PM
  1020. int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
  1021. {
  1022. int retval;
  1023. NOTICE(rt2x00dev, "Going to sleep.\n");
  1024. __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  1025. /*
  1026. * Only continue if mac80211 has open interfaces.
  1027. */
  1028. if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  1029. goto exit;
  1030. __set_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags);
  1031. /*
  1032. * Disable radio and unitialize all items
  1033. * that must be recreated on resume.
  1034. */
  1035. rt2x00lib_stop(rt2x00dev);
  1036. rt2x00lib_uninitialize(rt2x00dev);
  1037. rt2x00debug_deregister(rt2x00dev);
  1038. exit:
  1039. /*
  1040. * Set device mode to sleep for power management.
  1041. */
  1042. retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
  1043. if (retval)
  1044. return retval;
  1045. return 0;
  1046. }
  1047. EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
  1048. static void rt2x00lib_resume_intf(void *data, u8 *mac,
  1049. struct ieee80211_vif *vif)
  1050. {
  1051. struct rt2x00_dev *rt2x00dev = data;
  1052. struct rt2x00_intf *intf = vif_to_intf(vif);
  1053. spin_lock(&intf->lock);
  1054. rt2x00lib_config_intf(rt2x00dev, intf,
  1055. vif->type, intf->mac, intf->bssid);
  1056. /*
  1057. * Master or Ad-hoc mode require a new beacon update.
  1058. */
  1059. if (vif->type == IEEE80211_IF_TYPE_AP ||
  1060. vif->type == IEEE80211_IF_TYPE_IBSS)
  1061. intf->delayed_flags |= DELAYED_UPDATE_BEACON;
  1062. spin_unlock(&intf->lock);
  1063. }
  1064. int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
  1065. {
  1066. int retval;
  1067. NOTICE(rt2x00dev, "Waking up.\n");
  1068. /*
  1069. * Open the debugfs entry.
  1070. */
  1071. rt2x00debug_register(rt2x00dev);
  1072. /*
  1073. * Only continue if mac80211 had open interfaces.
  1074. */
  1075. if (!__test_and_clear_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags))
  1076. return 0;
  1077. /*
  1078. * Reinitialize device and all active interfaces.
  1079. */
  1080. retval = rt2x00lib_start(rt2x00dev);
  1081. if (retval)
  1082. goto exit;
  1083. /*
  1084. * Reconfigure device.
  1085. */
  1086. rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1);
  1087. if (!rt2x00dev->hw->conf.radio_enabled)
  1088. rt2x00lib_disable_radio(rt2x00dev);
  1089. /*
  1090. * Iterator over each active interface to
  1091. * reconfigure the hardware.
  1092. */
  1093. ieee80211_iterate_active_interfaces(rt2x00dev->hw,
  1094. rt2x00lib_resume_intf, rt2x00dev);
  1095. /*
  1096. * We are ready again to receive requests from mac80211.
  1097. */
  1098. __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  1099. /*
  1100. * It is possible that during that mac80211 has attempted
  1101. * to send frames while we were suspending or resuming.
  1102. * In that case we have disabled the TX queue and should
  1103. * now enable it again
  1104. */
  1105. ieee80211_start_queues(rt2x00dev->hw);
  1106. /*
  1107. * During interface iteration we might have changed the
  1108. * delayed_flags, time to handles the event by calling
  1109. * the work handler directly.
  1110. */
  1111. rt2x00lib_intf_scheduled(&rt2x00dev->intf_work);
  1112. return 0;
  1113. exit:
  1114. rt2x00lib_disable_radio(rt2x00dev);
  1115. rt2x00lib_uninitialize(rt2x00dev);
  1116. rt2x00debug_deregister(rt2x00dev);
  1117. return retval;
  1118. }
  1119. EXPORT_SYMBOL_GPL(rt2x00lib_resume);
  1120. #endif /* CONFIG_PM */
  1121. /*
  1122. * rt2x00lib module information.
  1123. */
  1124. MODULE_AUTHOR(DRV_PROJECT);
  1125. MODULE_VERSION(DRV_VERSION);
  1126. MODULE_DESCRIPTION("rt2x00 library");
  1127. MODULE_LICENSE("GPL");