rt2x00dev.c 33 KB

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