rt2x00dev.c 33 KB

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