rt2x00dev.c 33 KB

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