rt2x00dev.c 33 KB

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