rt2x00dev.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  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. * Send a signal to the led to update the led signal strength.
  320. */
  321. rt2x00leds_led_quality(rt2x00dev, rt2x00dev->link.qual.avg_rssi);
  322. /*
  323. * Evaluate antenna setup, make this the last step since this could
  324. * possibly reset some statistics.
  325. */
  326. rt2x00lib_evaluate_antenna(rt2x00dev);
  327. /*
  328. * Increase tuner counter, and reschedule the next link tuner run.
  329. */
  330. rt2x00dev->link.count++;
  331. queue_delayed_work(rt2x00dev->hw->workqueue, &rt2x00dev->link.work,
  332. LINK_TUNE_INTERVAL);
  333. }
  334. static void rt2x00lib_packetfilter_scheduled(struct work_struct *work)
  335. {
  336. struct rt2x00_dev *rt2x00dev =
  337. container_of(work, struct rt2x00_dev, filter_work);
  338. unsigned int filter = rt2x00dev->packet_filter;
  339. /*
  340. * Since we had stored the filter inside rt2x00dev->packet_filter,
  341. * we should now clear that field. Otherwise the driver will
  342. * assume nothing has changed (*total_flags will be compared
  343. * to rt2x00dev->packet_filter to determine if any action is required).
  344. */
  345. rt2x00dev->packet_filter = 0;
  346. rt2x00dev->ops->hw->configure_filter(rt2x00dev->hw,
  347. filter, &filter, 0, NULL);
  348. }
  349. static void rt2x00lib_intf_scheduled_iter(void *data, u8 *mac,
  350. struct ieee80211_vif *vif)
  351. {
  352. struct rt2x00_dev *rt2x00dev = data;
  353. struct rt2x00_intf *intf = vif_to_intf(vif);
  354. struct sk_buff *skb;
  355. struct ieee80211_tx_control control;
  356. struct ieee80211_bss_conf conf;
  357. int delayed_flags;
  358. /*
  359. * Copy all data we need during this action under the protection
  360. * of a spinlock. Otherwise race conditions might occur which results
  361. * into an invalid configuration.
  362. */
  363. spin_lock(&intf->lock);
  364. memcpy(&conf, &intf->conf, sizeof(conf));
  365. delayed_flags = intf->delayed_flags;
  366. intf->delayed_flags = 0;
  367. spin_unlock(&intf->lock);
  368. if (delayed_flags & DELAYED_UPDATE_BEACON) {
  369. skb = ieee80211_beacon_get(rt2x00dev->hw, vif, &control);
  370. if (skb) {
  371. rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw, skb,
  372. &control);
  373. dev_kfree_skb(skb);
  374. }
  375. }
  376. if (delayed_flags & DELAYED_CONFIG_ERP)
  377. rt2x00lib_config_erp(rt2x00dev, intf, &intf->conf);
  378. }
  379. static void rt2x00lib_intf_scheduled(struct work_struct *work)
  380. {
  381. struct rt2x00_dev *rt2x00dev =
  382. container_of(work, struct rt2x00_dev, intf_work);
  383. /*
  384. * Iterate over each interface and perform the
  385. * requested configurations.
  386. */
  387. ieee80211_iterate_active_interfaces(rt2x00dev->hw,
  388. rt2x00lib_intf_scheduled_iter,
  389. rt2x00dev);
  390. }
  391. /*
  392. * Interrupt context handlers.
  393. */
  394. static void rt2x00lib_beacondone_iter(void *data, u8 *mac,
  395. struct ieee80211_vif *vif)
  396. {
  397. struct rt2x00_intf *intf = vif_to_intf(vif);
  398. if (vif->type != IEEE80211_IF_TYPE_AP &&
  399. vif->type != IEEE80211_IF_TYPE_IBSS)
  400. return;
  401. spin_lock(&intf->lock);
  402. intf->delayed_flags |= DELAYED_UPDATE_BEACON;
  403. spin_unlock(&intf->lock);
  404. }
  405. void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
  406. {
  407. if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  408. return;
  409. ieee80211_iterate_active_interfaces(rt2x00dev->hw,
  410. rt2x00lib_beacondone_iter,
  411. rt2x00dev);
  412. queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->intf_work);
  413. }
  414. EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
  415. void rt2x00lib_txdone(struct queue_entry *entry,
  416. struct txdone_entry_desc *txdesc)
  417. {
  418. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  419. struct skb_frame_desc *skbdesc;
  420. struct ieee80211_tx_status tx_status;
  421. int success = !!(txdesc->status == TX_SUCCESS ||
  422. txdesc->status == TX_SUCCESS_RETRY);
  423. int fail = !!(txdesc->status == TX_FAIL_RETRY ||
  424. txdesc->status == TX_FAIL_INVALID ||
  425. txdesc->status == TX_FAIL_OTHER);
  426. /*
  427. * Update TX statistics.
  428. */
  429. rt2x00dev->link.qual.tx_success += success;
  430. rt2x00dev->link.qual.tx_failed += txdesc->retry + fail;
  431. /*
  432. * Initialize TX status
  433. */
  434. tx_status.flags = 0;
  435. tx_status.ack_signal = 0;
  436. tx_status.excessive_retries = (txdesc->status == TX_FAIL_RETRY);
  437. tx_status.retry_count = txdesc->retry;
  438. memcpy(&tx_status.control, txdesc->control, sizeof(*txdesc->control));
  439. if (!(tx_status.control.flags & IEEE80211_TXCTL_NO_ACK)) {
  440. if (success)
  441. tx_status.flags |= IEEE80211_TX_STATUS_ACK;
  442. else
  443. rt2x00dev->low_level_stats.dot11ACKFailureCount++;
  444. }
  445. tx_status.queue_length = entry->queue->limit;
  446. tx_status.queue_number = tx_status.control.queue;
  447. if (tx_status.control.flags & IEEE80211_TXCTL_USE_RTS_CTS) {
  448. if (success)
  449. rt2x00dev->low_level_stats.dot11RTSSuccessCount++;
  450. else
  451. rt2x00dev->low_level_stats.dot11RTSFailureCount++;
  452. }
  453. /*
  454. * Send the tx_status to debugfs. Only send the status report
  455. * to mac80211 when the frame originated from there. If this was
  456. * a extra frame coming through a mac80211 library call (RTS/CTS)
  457. * then we should not send the status report back.
  458. * If send to mac80211, mac80211 will clean up the skb structure,
  459. * otherwise we have to do it ourself.
  460. */
  461. skbdesc = get_skb_frame_desc(entry->skb);
  462. skbdesc->frame_type = DUMP_FRAME_TXDONE;
  463. rt2x00debug_dump_frame(rt2x00dev, entry->skb);
  464. if (!(skbdesc->flags & FRAME_DESC_DRIVER_GENERATED))
  465. ieee80211_tx_status_irqsafe(rt2x00dev->hw,
  466. entry->skb, &tx_status);
  467. else
  468. dev_kfree_skb(entry->skb);
  469. entry->skb = NULL;
  470. }
  471. EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
  472. void rt2x00lib_rxdone(struct queue_entry *entry,
  473. struct rxdone_entry_desc *rxdesc)
  474. {
  475. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  476. struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
  477. struct ieee80211_supported_band *sband;
  478. struct ieee80211_hdr *hdr;
  479. const struct rt2x00_rate *rate;
  480. unsigned int i;
  481. int idx = -1;
  482. u16 fc;
  483. /*
  484. * Update RX statistics.
  485. */
  486. sband = &rt2x00dev->bands[rt2x00dev->curr_band];
  487. for (i = 0; i < sband->n_bitrates; i++) {
  488. rate = rt2x00_get_rate(sband->bitrates[i].hw_value);
  489. if (((rxdesc->dev_flags & RXDONE_SIGNAL_PLCP) &&
  490. (rate->plcp == rxdesc->signal)) ||
  491. (!(rxdesc->dev_flags & RXDONE_SIGNAL_PLCP) &&
  492. (rate->bitrate == rxdesc->signal))) {
  493. idx = i;
  494. break;
  495. }
  496. }
  497. if (idx < 0) {
  498. WARNING(rt2x00dev, "Frame received with unrecognized signal,"
  499. "signal=0x%.2x, plcp=%d.\n", rxdesc->signal,
  500. !!(rxdesc->dev_flags & RXDONE_SIGNAL_PLCP));
  501. idx = 0;
  502. }
  503. /*
  504. * Only update link status if this is a beacon frame carrying our bssid.
  505. */
  506. hdr = (struct ieee80211_hdr *)entry->skb->data;
  507. fc = le16_to_cpu(hdr->frame_control);
  508. if (is_beacon(fc) && (rxdesc->dev_flags & RXDONE_MY_BSS))
  509. rt2x00lib_update_link_stats(&rt2x00dev->link, rxdesc->rssi);
  510. rt2x00dev->link.qual.rx_success++;
  511. rx_status->rate_idx = idx;
  512. rx_status->signal =
  513. rt2x00lib_calculate_link_signal(rt2x00dev, rxdesc->rssi);
  514. rx_status->ssi = rxdesc->rssi;
  515. rx_status->flag = rxdesc->flags;
  516. rx_status->antenna = rt2x00dev->link.ant.active.rx;
  517. /*
  518. * Send frame to mac80211 & debugfs.
  519. * mac80211 will clean up the skb structure.
  520. */
  521. get_skb_frame_desc(entry->skb)->frame_type = DUMP_FRAME_RXDONE;
  522. rt2x00debug_dump_frame(rt2x00dev, entry->skb);
  523. ieee80211_rx_irqsafe(rt2x00dev->hw, entry->skb, rx_status);
  524. entry->skb = NULL;
  525. }
  526. EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
  527. /*
  528. * TX descriptor initializer
  529. */
  530. void rt2x00lib_write_tx_desc(struct rt2x00_dev *rt2x00dev,
  531. struct sk_buff *skb,
  532. struct ieee80211_tx_control *control)
  533. {
  534. struct txentry_desc txdesc;
  535. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  536. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skbdesc->data;
  537. const struct rt2x00_rate *rate;
  538. int tx_rate;
  539. int length;
  540. int duration;
  541. int residual;
  542. u16 frame_control;
  543. u16 seq_ctrl;
  544. memset(&txdesc, 0, sizeof(txdesc));
  545. txdesc.queue = skbdesc->entry->queue->qid;
  546. txdesc.cw_min = skbdesc->entry->queue->cw_min;
  547. txdesc.cw_max = skbdesc->entry->queue->cw_max;
  548. txdesc.aifs = skbdesc->entry->queue->aifs;
  549. /*
  550. * Read required fields from ieee80211 header.
  551. */
  552. frame_control = le16_to_cpu(hdr->frame_control);
  553. seq_ctrl = le16_to_cpu(hdr->seq_ctrl);
  554. tx_rate = control->tx_rate->hw_value;
  555. /*
  556. * Check whether this frame is to be acked
  557. */
  558. if (!(control->flags & IEEE80211_TXCTL_NO_ACK))
  559. __set_bit(ENTRY_TXD_ACK, &txdesc.flags);
  560. /*
  561. * Check if this is a RTS/CTS frame
  562. */
  563. if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
  564. __set_bit(ENTRY_TXD_BURST, &txdesc.flags);
  565. if (is_rts_frame(frame_control)) {
  566. __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc.flags);
  567. __set_bit(ENTRY_TXD_ACK, &txdesc.flags);
  568. } else
  569. __clear_bit(ENTRY_TXD_ACK, &txdesc.flags);
  570. if (control->rts_cts_rate)
  571. tx_rate = control->rts_cts_rate->hw_value;
  572. }
  573. rate = rt2x00_get_rate(tx_rate);
  574. /*
  575. * Check if more fragments are pending
  576. */
  577. if (ieee80211_get_morefrag(hdr)) {
  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 == RT2X00_BCN_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 = rate->plcp;
  603. txdesc.service = 0x04;
  604. length = skbdesc->data_len + FCS_LEN;
  605. if (rate->flags & DEV_RATE_OFDM) {
  606. __set_bit(ENTRY_TXD_OFDM_RATE, &txdesc.flags);
  607. txdesc.length_high = (length >> 6) & 0x3f;
  608. txdesc.length_low = length & 0x3f;
  609. } else {
  610. /*
  611. * Convert length to microseconds.
  612. */
  613. residual = get_duration_res(length, rate->bitrate);
  614. duration = get_duration(length, rate->bitrate);
  615. if (residual != 0) {
  616. duration++;
  617. /*
  618. * Check if we need to set the Length Extension
  619. */
  620. if (rate->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 (rt2x00_get_rate_preamble(tx_rate))
  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. const struct rt2x00_rate rt2x00_supported_rates[12] = {
  651. {
  652. .flags = DEV_RATE_CCK | DEV_RATE_BASIC,
  653. .bitrate = 10,
  654. .ratemask = BIT(0),
  655. .plcp = 0x00,
  656. },
  657. {
  658. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE | DEV_RATE_BASIC,
  659. .bitrate = 20,
  660. .ratemask = BIT(1),
  661. .plcp = 0x01,
  662. },
  663. {
  664. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE | DEV_RATE_BASIC,
  665. .bitrate = 55,
  666. .ratemask = BIT(2),
  667. .plcp = 0x02,
  668. },
  669. {
  670. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE | DEV_RATE_BASIC,
  671. .bitrate = 110,
  672. .ratemask = BIT(3),
  673. .plcp = 0x03,
  674. },
  675. {
  676. .flags = DEV_RATE_OFDM | DEV_RATE_BASIC,
  677. .bitrate = 60,
  678. .ratemask = BIT(4),
  679. .plcp = 0x0b,
  680. },
  681. {
  682. .flags = DEV_RATE_OFDM,
  683. .bitrate = 90,
  684. .ratemask = BIT(5),
  685. .plcp = 0x0f,
  686. },
  687. {
  688. .flags = DEV_RATE_OFDM | DEV_RATE_BASIC,
  689. .bitrate = 120,
  690. .ratemask = BIT(6),
  691. .plcp = 0x0a,
  692. },
  693. {
  694. .flags = DEV_RATE_OFDM,
  695. .bitrate = 180,
  696. .ratemask = BIT(7),
  697. .plcp = 0x0e,
  698. },
  699. {
  700. .flags = DEV_RATE_OFDM | DEV_RATE_BASIC,
  701. .bitrate = 240,
  702. .ratemask = BIT(8),
  703. .plcp = 0x09,
  704. },
  705. {
  706. .flags = DEV_RATE_OFDM,
  707. .bitrate = 360,
  708. .ratemask = BIT(9),
  709. .plcp = 0x0d,
  710. },
  711. {
  712. .flags = DEV_RATE_OFDM,
  713. .bitrate = 480,
  714. .ratemask = BIT(10),
  715. .plcp = 0x08,
  716. },
  717. {
  718. .flags = DEV_RATE_OFDM,
  719. .bitrate = 540,
  720. .ratemask = BIT(11),
  721. .plcp = 0x0c,
  722. },
  723. };
  724. static void rt2x00lib_channel(struct ieee80211_channel *entry,
  725. const int channel, const int tx_power,
  726. const int value)
  727. {
  728. entry->center_freq = ieee80211_channel_to_frequency(channel);
  729. entry->hw_value = value;
  730. entry->max_power = tx_power;
  731. entry->max_antenna_gain = 0xff;
  732. }
  733. static void rt2x00lib_rate(struct ieee80211_rate *entry,
  734. const u16 index, const struct rt2x00_rate *rate)
  735. {
  736. entry->flags = 0;
  737. entry->bitrate = rate->bitrate;
  738. entry->hw_value = rt2x00_create_rate_hw_value(index, 0);
  739. entry->hw_value_short = entry->hw_value;
  740. if (rate->flags & DEV_RATE_SHORT_PREAMBLE) {
  741. entry->flags |= IEEE80211_RATE_SHORT_PREAMBLE;
  742. entry->hw_value_short |= rt2x00_create_rate_hw_value(index, 1);
  743. }
  744. }
  745. static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
  746. struct hw_mode_spec *spec)
  747. {
  748. struct ieee80211_hw *hw = rt2x00dev->hw;
  749. struct ieee80211_channel *channels;
  750. struct ieee80211_rate *rates;
  751. unsigned int num_rates;
  752. unsigned int i;
  753. unsigned char tx_power;
  754. num_rates = 0;
  755. if (spec->supported_rates & SUPPORT_RATE_CCK)
  756. num_rates += 4;
  757. if (spec->supported_rates & SUPPORT_RATE_OFDM)
  758. num_rates += 8;
  759. channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
  760. if (!channels)
  761. return -ENOMEM;
  762. rates = kzalloc(sizeof(*rates) * num_rates, GFP_KERNEL);
  763. if (!rates)
  764. goto exit_free_channels;
  765. /*
  766. * Initialize Rate list.
  767. */
  768. for (i = 0; i < num_rates; i++)
  769. rt2x00lib_rate(&rates[i], i, rt2x00_get_rate(i));
  770. /*
  771. * Initialize Channel list.
  772. */
  773. for (i = 0; i < spec->num_channels; i++) {
  774. if (spec->channels[i].channel <= 14) {
  775. if (spec->tx_power_bg)
  776. tx_power = spec->tx_power_bg[i];
  777. else
  778. tx_power = spec->tx_power_default;
  779. } else {
  780. if (spec->tx_power_a)
  781. tx_power = spec->tx_power_a[i];
  782. else
  783. tx_power = spec->tx_power_default;
  784. }
  785. rt2x00lib_channel(&channels[i],
  786. spec->channels[i].channel, tx_power, i);
  787. }
  788. /*
  789. * Intitialize 802.11b, 802.11g
  790. * Rates: CCK, OFDM.
  791. * Channels: 2.4 GHz
  792. */
  793. if (spec->supported_bands & SUPPORT_BAND_2GHZ) {
  794. rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_channels = 14;
  795. rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_bitrates = num_rates;
  796. rt2x00dev->bands[IEEE80211_BAND_2GHZ].channels = channels;
  797. rt2x00dev->bands[IEEE80211_BAND_2GHZ].bitrates = rates;
  798. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  799. &rt2x00dev->bands[IEEE80211_BAND_2GHZ];
  800. }
  801. /*
  802. * Intitialize 802.11a
  803. * Rates: OFDM.
  804. * Channels: OFDM, UNII, HiperLAN2.
  805. */
  806. if (spec->supported_bands & SUPPORT_BAND_5GHZ) {
  807. rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_channels =
  808. spec->num_channels - 14;
  809. rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_bitrates =
  810. num_rates - 4;
  811. rt2x00dev->bands[IEEE80211_BAND_5GHZ].channels = &channels[14];
  812. rt2x00dev->bands[IEEE80211_BAND_5GHZ].bitrates = &rates[4];
  813. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  814. &rt2x00dev->bands[IEEE80211_BAND_5GHZ];
  815. }
  816. return 0;
  817. exit_free_channels:
  818. kfree(channels);
  819. ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
  820. return -ENOMEM;
  821. }
  822. static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
  823. {
  824. if (test_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags))
  825. ieee80211_unregister_hw(rt2x00dev->hw);
  826. if (likely(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ])) {
  827. kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->channels);
  828. kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->bitrates);
  829. rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
  830. rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
  831. }
  832. }
  833. static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
  834. {
  835. struct hw_mode_spec *spec = &rt2x00dev->spec;
  836. int status;
  837. /*
  838. * Initialize HW modes.
  839. */
  840. status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
  841. if (status)
  842. return status;
  843. /*
  844. * Register HW.
  845. */
  846. status = ieee80211_register_hw(rt2x00dev->hw);
  847. if (status) {
  848. rt2x00lib_remove_hw(rt2x00dev);
  849. return status;
  850. }
  851. __set_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags);
  852. return 0;
  853. }
  854. /*
  855. * Initialization/uninitialization handlers.
  856. */
  857. static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
  858. {
  859. if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
  860. return;
  861. /*
  862. * Unregister extra components.
  863. */
  864. rt2x00rfkill_unregister(rt2x00dev);
  865. /*
  866. * Allow the HW to uninitialize.
  867. */
  868. rt2x00dev->ops->lib->uninitialize(rt2x00dev);
  869. /*
  870. * Free allocated queue entries.
  871. */
  872. rt2x00queue_uninitialize(rt2x00dev);
  873. }
  874. static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
  875. {
  876. int status;
  877. if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
  878. return 0;
  879. /*
  880. * Allocate all queue entries.
  881. */
  882. status = rt2x00queue_initialize(rt2x00dev);
  883. if (status)
  884. return status;
  885. /*
  886. * Initialize the device.
  887. */
  888. status = rt2x00dev->ops->lib->initialize(rt2x00dev);
  889. if (status)
  890. goto exit;
  891. __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
  892. /*
  893. * Register the extra components.
  894. */
  895. rt2x00rfkill_register(rt2x00dev);
  896. return 0;
  897. exit:
  898. rt2x00lib_uninitialize(rt2x00dev);
  899. return status;
  900. }
  901. int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
  902. {
  903. int retval;
  904. if (test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  905. return 0;
  906. /*
  907. * If this is the first interface which is added,
  908. * we should load the firmware now.
  909. */
  910. retval = rt2x00lib_load_firmware(rt2x00dev);
  911. if (retval)
  912. return retval;
  913. /*
  914. * Initialize the device.
  915. */
  916. retval = rt2x00lib_initialize(rt2x00dev);
  917. if (retval)
  918. return retval;
  919. /*
  920. * Enable radio.
  921. */
  922. retval = rt2x00lib_enable_radio(rt2x00dev);
  923. if (retval) {
  924. rt2x00lib_uninitialize(rt2x00dev);
  925. return retval;
  926. }
  927. rt2x00dev->intf_ap_count = 0;
  928. rt2x00dev->intf_sta_count = 0;
  929. rt2x00dev->intf_associated = 0;
  930. __set_bit(DEVICE_STARTED, &rt2x00dev->flags);
  931. return 0;
  932. }
  933. void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
  934. {
  935. if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  936. return;
  937. /*
  938. * Perhaps we can add something smarter here,
  939. * but for now just disabling the radio should do.
  940. */
  941. rt2x00lib_disable_radio(rt2x00dev);
  942. rt2x00dev->intf_ap_count = 0;
  943. rt2x00dev->intf_sta_count = 0;
  944. rt2x00dev->intf_associated = 0;
  945. __clear_bit(DEVICE_STARTED, &rt2x00dev->flags);
  946. }
  947. /*
  948. * driver allocation handlers.
  949. */
  950. int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
  951. {
  952. int retval = -ENOMEM;
  953. /*
  954. * Make room for rt2x00_intf inside the per-interface
  955. * structure ieee80211_vif.
  956. */
  957. rt2x00dev->hw->vif_data_size = sizeof(struct rt2x00_intf);
  958. /*
  959. * Let the driver probe the device to detect the capabilities.
  960. */
  961. retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
  962. if (retval) {
  963. ERROR(rt2x00dev, "Failed to allocate device.\n");
  964. goto exit;
  965. }
  966. /*
  967. * Initialize configuration work.
  968. */
  969. INIT_WORK(&rt2x00dev->intf_work, rt2x00lib_intf_scheduled);
  970. INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
  971. INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
  972. /*
  973. * Allocate queue array.
  974. */
  975. retval = rt2x00queue_allocate(rt2x00dev);
  976. if (retval)
  977. goto exit;
  978. /*
  979. * Initialize ieee80211 structure.
  980. */
  981. retval = rt2x00lib_probe_hw(rt2x00dev);
  982. if (retval) {
  983. ERROR(rt2x00dev, "Failed to initialize hw.\n");
  984. goto exit;
  985. }
  986. /*
  987. * Register extra components.
  988. */
  989. rt2x00leds_register(rt2x00dev);
  990. rt2x00rfkill_allocate(rt2x00dev);
  991. rt2x00debug_register(rt2x00dev);
  992. __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  993. return 0;
  994. exit:
  995. rt2x00lib_remove_dev(rt2x00dev);
  996. return retval;
  997. }
  998. EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
  999. void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
  1000. {
  1001. __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  1002. /*
  1003. * Disable radio.
  1004. */
  1005. rt2x00lib_disable_radio(rt2x00dev);
  1006. /*
  1007. * Uninitialize device.
  1008. */
  1009. rt2x00lib_uninitialize(rt2x00dev);
  1010. /*
  1011. * Free extra components
  1012. */
  1013. rt2x00debug_deregister(rt2x00dev);
  1014. rt2x00rfkill_free(rt2x00dev);
  1015. /*
  1016. * Free LED.
  1017. */
  1018. rt2x00leds_unregister(rt2x00dev);
  1019. /*
  1020. * Free ieee80211_hw memory.
  1021. */
  1022. rt2x00lib_remove_hw(rt2x00dev);
  1023. /*
  1024. * Free firmware image.
  1025. */
  1026. rt2x00lib_free_firmware(rt2x00dev);
  1027. /*
  1028. * Free queue structures.
  1029. */
  1030. rt2x00queue_free(rt2x00dev);
  1031. }
  1032. EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
  1033. /*
  1034. * Device state handlers
  1035. */
  1036. #ifdef CONFIG_PM
  1037. int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
  1038. {
  1039. int retval;
  1040. NOTICE(rt2x00dev, "Going to sleep.\n");
  1041. __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  1042. /*
  1043. * Only continue if mac80211 has open interfaces.
  1044. */
  1045. if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  1046. goto exit;
  1047. __set_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags);
  1048. /*
  1049. * Disable radio.
  1050. */
  1051. rt2x00lib_stop(rt2x00dev);
  1052. rt2x00lib_uninitialize(rt2x00dev);
  1053. /*
  1054. * Suspend/disable extra components.
  1055. */
  1056. rt2x00leds_suspend(rt2x00dev);
  1057. rt2x00rfkill_suspend(rt2x00dev);
  1058. rt2x00debug_deregister(rt2x00dev);
  1059. exit:
  1060. /*
  1061. * Set device mode to sleep for power management.
  1062. */
  1063. retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
  1064. if (retval)
  1065. return retval;
  1066. return 0;
  1067. }
  1068. EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
  1069. static void rt2x00lib_resume_intf(void *data, u8 *mac,
  1070. struct ieee80211_vif *vif)
  1071. {
  1072. struct rt2x00_dev *rt2x00dev = data;
  1073. struct rt2x00_intf *intf = vif_to_intf(vif);
  1074. spin_lock(&intf->lock);
  1075. rt2x00lib_config_intf(rt2x00dev, intf,
  1076. vif->type, intf->mac, intf->bssid);
  1077. /*
  1078. * Master or Ad-hoc mode require a new beacon update.
  1079. */
  1080. if (vif->type == IEEE80211_IF_TYPE_AP ||
  1081. vif->type == IEEE80211_IF_TYPE_IBSS)
  1082. intf->delayed_flags |= DELAYED_UPDATE_BEACON;
  1083. spin_unlock(&intf->lock);
  1084. }
  1085. int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
  1086. {
  1087. int retval;
  1088. NOTICE(rt2x00dev, "Waking up.\n");
  1089. /*
  1090. * Restore/enable extra components.
  1091. */
  1092. rt2x00debug_register(rt2x00dev);
  1093. rt2x00rfkill_resume(rt2x00dev);
  1094. rt2x00leds_resume(rt2x00dev);
  1095. /*
  1096. * Only continue if mac80211 had open interfaces.
  1097. */
  1098. if (!__test_and_clear_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags))
  1099. return 0;
  1100. /*
  1101. * Reinitialize device and all active interfaces.
  1102. */
  1103. retval = rt2x00lib_start(rt2x00dev);
  1104. if (retval)
  1105. goto exit;
  1106. /*
  1107. * Reconfigure device.
  1108. */
  1109. rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1);
  1110. if (!rt2x00dev->hw->conf.radio_enabled)
  1111. rt2x00lib_disable_radio(rt2x00dev);
  1112. /*
  1113. * Iterator over each active interface to
  1114. * reconfigure the hardware.
  1115. */
  1116. ieee80211_iterate_active_interfaces(rt2x00dev->hw,
  1117. rt2x00lib_resume_intf, rt2x00dev);
  1118. /*
  1119. * We are ready again to receive requests from mac80211.
  1120. */
  1121. __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  1122. /*
  1123. * It is possible that during that mac80211 has attempted
  1124. * to send frames while we were suspending or resuming.
  1125. * In that case we have disabled the TX queue and should
  1126. * now enable it again
  1127. */
  1128. ieee80211_start_queues(rt2x00dev->hw);
  1129. /*
  1130. * During interface iteration we might have changed the
  1131. * delayed_flags, time to handles the event by calling
  1132. * the work handler directly.
  1133. */
  1134. rt2x00lib_intf_scheduled(&rt2x00dev->intf_work);
  1135. return 0;
  1136. exit:
  1137. rt2x00lib_disable_radio(rt2x00dev);
  1138. rt2x00lib_uninitialize(rt2x00dev);
  1139. rt2x00debug_deregister(rt2x00dev);
  1140. return retval;
  1141. }
  1142. EXPORT_SYMBOL_GPL(rt2x00lib_resume);
  1143. #endif /* CONFIG_PM */
  1144. /*
  1145. * rt2x00lib module information.
  1146. */
  1147. MODULE_AUTHOR(DRV_PROJECT);
  1148. MODULE_VERSION(DRV_VERSION);
  1149. MODULE_DESCRIPTION("rt2x00 library");
  1150. MODULE_LICENSE("GPL");