rt2x00dev.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  1. /*
  2. Copyright (C) 2004 - 2007 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. * Ring handler.
  28. */
  29. struct data_ring *rt2x00lib_get_ring(struct rt2x00_dev *rt2x00dev,
  30. const unsigned int queue)
  31. {
  32. int beacon = test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags);
  33. /*
  34. * Check if we are requesting a reqular TX ring,
  35. * or if we are requesting a Beacon or Atim ring.
  36. * For Atim rings, we should check if it is supported.
  37. */
  38. if (queue < rt2x00dev->hw->queues && rt2x00dev->tx)
  39. return &rt2x00dev->tx[queue];
  40. if (!rt2x00dev->bcn || !beacon)
  41. return NULL;
  42. if (queue == IEEE80211_TX_QUEUE_BEACON)
  43. return &rt2x00dev->bcn[0];
  44. else if (queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
  45. return &rt2x00dev->bcn[1];
  46. return NULL;
  47. }
  48. EXPORT_SYMBOL_GPL(rt2x00lib_get_ring);
  49. /*
  50. * Link tuning handlers
  51. */
  52. static void rt2x00lib_start_link_tuner(struct rt2x00_dev *rt2x00dev)
  53. {
  54. rt2x00dev->link.count = 0;
  55. rt2x00dev->link.vgc_level = 0;
  56. memset(&rt2x00dev->link.qual, 0, sizeof(rt2x00dev->link.qual));
  57. /*
  58. * The RX and TX percentage should start at 50%
  59. * this will assure we will get at least get some
  60. * decent value when the link tuner starts.
  61. * The value will be dropped and overwritten with
  62. * the correct (measured )value anyway during the
  63. * first run of the link tuner.
  64. */
  65. rt2x00dev->link.qual.rx_percentage = 50;
  66. rt2x00dev->link.qual.tx_percentage = 50;
  67. /*
  68. * Reset the link tuner.
  69. */
  70. rt2x00dev->ops->lib->reset_tuner(rt2x00dev);
  71. queue_delayed_work(rt2x00dev->hw->workqueue,
  72. &rt2x00dev->link.work, LINK_TUNE_INTERVAL);
  73. }
  74. static void rt2x00lib_stop_link_tuner(struct rt2x00_dev *rt2x00dev)
  75. {
  76. cancel_delayed_work_sync(&rt2x00dev->link.work);
  77. }
  78. void rt2x00lib_reset_link_tuner(struct rt2x00_dev *rt2x00dev)
  79. {
  80. if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  81. return;
  82. rt2x00lib_stop_link_tuner(rt2x00dev);
  83. rt2x00lib_start_link_tuner(rt2x00dev);
  84. }
  85. /*
  86. * Radio control handlers.
  87. */
  88. int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
  89. {
  90. int status;
  91. /*
  92. * Don't enable the radio twice.
  93. * And check if the hardware button has been disabled.
  94. */
  95. if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
  96. test_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags))
  97. return 0;
  98. /*
  99. * Enable radio.
  100. */
  101. status = rt2x00dev->ops->lib->set_device_state(rt2x00dev,
  102. STATE_RADIO_ON);
  103. if (status)
  104. return status;
  105. __set_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags);
  106. /*
  107. * Enable RX.
  108. */
  109. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
  110. /*
  111. * Start the TX queues.
  112. */
  113. ieee80211_start_queues(rt2x00dev->hw);
  114. return 0;
  115. }
  116. void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
  117. {
  118. if (!__test_and_clear_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  119. return;
  120. /*
  121. * Stop all scheduled work.
  122. */
  123. if (work_pending(&rt2x00dev->beacon_work))
  124. cancel_work_sync(&rt2x00dev->beacon_work);
  125. if (work_pending(&rt2x00dev->filter_work))
  126. cancel_work_sync(&rt2x00dev->filter_work);
  127. if (work_pending(&rt2x00dev->config_work))
  128. cancel_work_sync(&rt2x00dev->config_work);
  129. /*
  130. * Stop the TX queues.
  131. */
  132. ieee80211_stop_queues(rt2x00dev->hw);
  133. /*
  134. * Disable RX.
  135. */
  136. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
  137. /*
  138. * Disable radio.
  139. */
  140. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
  141. }
  142. void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, enum dev_state state)
  143. {
  144. /*
  145. * When we are disabling the RX, we should also stop the link tuner.
  146. */
  147. if (state == STATE_RADIO_RX_OFF)
  148. rt2x00lib_stop_link_tuner(rt2x00dev);
  149. rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
  150. /*
  151. * When we are enabling the RX, we should also start the link tuner.
  152. */
  153. if (state == STATE_RADIO_RX_ON &&
  154. is_interface_present(&rt2x00dev->interface))
  155. rt2x00lib_start_link_tuner(rt2x00dev);
  156. }
  157. static void rt2x00lib_evaluate_antenna_sample(struct rt2x00_dev *rt2x00dev)
  158. {
  159. enum antenna rx = rt2x00dev->link.ant.active.rx;
  160. enum antenna tx = rt2x00dev->link.ant.active.tx;
  161. int sample_a =
  162. rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_A);
  163. int sample_b =
  164. rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_B);
  165. /*
  166. * We are done sampling. Now we should evaluate the results.
  167. */
  168. rt2x00dev->link.ant.flags &= ~ANTENNA_MODE_SAMPLE;
  169. /*
  170. * During the last period we have sampled the RSSI
  171. * from both antenna's. It now is time to determine
  172. * which antenna demonstrated the best performance.
  173. * When we are already on the antenna with the best
  174. * performance, then there really is nothing for us
  175. * left to do.
  176. */
  177. if (sample_a == sample_b)
  178. return;
  179. if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY) {
  180. if (sample_a > sample_b && rx == ANTENNA_B)
  181. rx = ANTENNA_A;
  182. else if (rx == ANTENNA_A)
  183. rx = ANTENNA_B;
  184. }
  185. if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY) {
  186. if (sample_a > sample_b && tx == ANTENNA_B)
  187. tx = ANTENNA_A;
  188. else if (tx == ANTENNA_A)
  189. tx = ANTENNA_B;
  190. }
  191. rt2x00lib_config_antenna(rt2x00dev, rx, tx);
  192. }
  193. static void rt2x00lib_evaluate_antenna_eval(struct rt2x00_dev *rt2x00dev)
  194. {
  195. enum antenna rx = rt2x00dev->link.ant.active.rx;
  196. enum antenna tx = rt2x00dev->link.ant.active.tx;
  197. int rssi_curr = rt2x00_get_link_ant_rssi(&rt2x00dev->link);
  198. int rssi_old = rt2x00_update_ant_rssi(&rt2x00dev->link, rssi_curr);
  199. /*
  200. * Legacy driver indicates that we should swap antenna's
  201. * when the difference in RSSI is greater that 5. This
  202. * also should be done when the RSSI was actually better
  203. * then the previous sample.
  204. * When the difference exceeds the threshold we should
  205. * sample the rssi from the other antenna to make a valid
  206. * comparison between the 2 antennas.
  207. */
  208. if ((rssi_curr - rssi_old) > -5 || (rssi_curr - rssi_old) < 5)
  209. return;
  210. rt2x00dev->link.ant.flags |= ANTENNA_MODE_SAMPLE;
  211. if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY)
  212. rx = (rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
  213. if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)
  214. tx = (tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
  215. rt2x00lib_config_antenna(rt2x00dev, rx, tx);
  216. }
  217. static void rt2x00lib_evaluate_antenna(struct rt2x00_dev *rt2x00dev)
  218. {
  219. /*
  220. * Determine if software diversity is enabled for
  221. * either the TX or RX antenna (or both).
  222. * Always perform this check since within the link
  223. * tuner interval the configuration might have changed.
  224. */
  225. rt2x00dev->link.ant.flags &= ~ANTENNA_RX_DIVERSITY;
  226. rt2x00dev->link.ant.flags &= ~ANTENNA_TX_DIVERSITY;
  227. if (rt2x00dev->hw->conf.antenna_sel_rx == 0 &&
  228. rt2x00dev->default_ant.rx != ANTENNA_SW_DIVERSITY)
  229. rt2x00dev->link.ant.flags |= ANTENNA_RX_DIVERSITY;
  230. if (rt2x00dev->hw->conf.antenna_sel_tx == 0 &&
  231. rt2x00dev->default_ant.tx != ANTENNA_SW_DIVERSITY)
  232. rt2x00dev->link.ant.flags |= ANTENNA_TX_DIVERSITY;
  233. if (!(rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY) &&
  234. !(rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)) {
  235. rt2x00dev->link.ant.flags &= ~ANTENNA_MODE_SAMPLE;
  236. return;
  237. }
  238. /*
  239. * If we have only sampled the data over the last period
  240. * we should now harvest the data. Otherwise just evaluate
  241. * the data. The latter should only be performed once
  242. * every 2 seconds.
  243. */
  244. if (rt2x00dev->link.ant.flags & ANTENNA_MODE_SAMPLE)
  245. rt2x00lib_evaluate_antenna_sample(rt2x00dev);
  246. else if (rt2x00dev->link.count & 1)
  247. rt2x00lib_evaluate_antenna_eval(rt2x00dev);
  248. }
  249. static void rt2x00lib_update_link_stats(struct link *link, int rssi)
  250. {
  251. int avg_rssi = rssi;
  252. /*
  253. * Update global RSSI
  254. */
  255. if (link->qual.avg_rssi)
  256. avg_rssi = MOVING_AVERAGE(link->qual.avg_rssi, rssi, 8);
  257. link->qual.avg_rssi = avg_rssi;
  258. /*
  259. * Update antenna RSSI
  260. */
  261. if (link->ant.rssi_ant)
  262. rssi = MOVING_AVERAGE(link->ant.rssi_ant, rssi, 8);
  263. link->ant.rssi_ant = rssi;
  264. }
  265. static void rt2x00lib_precalculate_link_signal(struct link_qual *qual)
  266. {
  267. if (qual->rx_failed || qual->rx_success)
  268. qual->rx_percentage =
  269. (qual->rx_success * 100) /
  270. (qual->rx_failed + qual->rx_success);
  271. else
  272. qual->rx_percentage = 50;
  273. if (qual->tx_failed || qual->tx_success)
  274. qual->tx_percentage =
  275. (qual->tx_success * 100) /
  276. (qual->tx_failed + qual->tx_success);
  277. else
  278. qual->tx_percentage = 50;
  279. qual->rx_success = 0;
  280. qual->rx_failed = 0;
  281. qual->tx_success = 0;
  282. qual->tx_failed = 0;
  283. }
  284. static int rt2x00lib_calculate_link_signal(struct rt2x00_dev *rt2x00dev,
  285. int rssi)
  286. {
  287. int rssi_percentage = 0;
  288. int signal;
  289. /*
  290. * We need a positive value for the RSSI.
  291. */
  292. if (rssi < 0)
  293. rssi += rt2x00dev->rssi_offset;
  294. /*
  295. * Calculate the different percentages,
  296. * which will be used for the signal.
  297. */
  298. if (rt2x00dev->rssi_offset)
  299. rssi_percentage = (rssi * 100) / rt2x00dev->rssi_offset;
  300. /*
  301. * Add the individual percentages and use the WEIGHT
  302. * defines to calculate the current link signal.
  303. */
  304. signal = ((WEIGHT_RSSI * rssi_percentage) +
  305. (WEIGHT_TX * rt2x00dev->link.qual.tx_percentage) +
  306. (WEIGHT_RX * rt2x00dev->link.qual.rx_percentage)) / 100;
  307. return (signal > 100) ? 100 : signal;
  308. }
  309. static void rt2x00lib_link_tuner(struct work_struct *work)
  310. {
  311. struct rt2x00_dev *rt2x00dev =
  312. container_of(work, struct rt2x00_dev, link.work.work);
  313. /*
  314. * When the radio is shutting down we should
  315. * immediately cease all link tuning.
  316. */
  317. if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  318. return;
  319. /*
  320. * Update statistics.
  321. */
  322. rt2x00dev->ops->lib->link_stats(rt2x00dev, &rt2x00dev->link.qual);
  323. rt2x00dev->low_level_stats.dot11FCSErrorCount +=
  324. rt2x00dev->link.qual.rx_failed;
  325. /*
  326. * Only perform the link tuning when Link tuning
  327. * has been enabled (This could have been disabled from the EEPROM).
  328. */
  329. if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
  330. rt2x00dev->ops->lib->link_tuner(rt2x00dev);
  331. /*
  332. * Evaluate antenna setup.
  333. */
  334. rt2x00lib_evaluate_antenna(rt2x00dev);
  335. /*
  336. * Precalculate a portion of the link signal which is
  337. * in based on the tx/rx success/failure counters.
  338. */
  339. rt2x00lib_precalculate_link_signal(&rt2x00dev->link.qual);
  340. /*
  341. * Increase tuner counter, and reschedule the next link tuner run.
  342. */
  343. rt2x00dev->link.count++;
  344. queue_delayed_work(rt2x00dev->hw->workqueue, &rt2x00dev->link.work,
  345. LINK_TUNE_INTERVAL);
  346. }
  347. static void rt2x00lib_packetfilter_scheduled(struct work_struct *work)
  348. {
  349. struct rt2x00_dev *rt2x00dev =
  350. container_of(work, struct rt2x00_dev, filter_work);
  351. unsigned int filter = rt2x00dev->packet_filter;
  352. /*
  353. * Since we had stored the filter inside interface.filter,
  354. * we should now clear that field. Otherwise the driver will
  355. * assume nothing has changed (*total_flags will be compared
  356. * to interface.filter to determine if any action is required).
  357. */
  358. rt2x00dev->packet_filter = 0;
  359. rt2x00dev->ops->hw->configure_filter(rt2x00dev->hw,
  360. filter, &filter, 0, NULL);
  361. }
  362. static void rt2x00lib_configuration_scheduled(struct work_struct *work)
  363. {
  364. struct rt2x00_dev *rt2x00dev =
  365. container_of(work, struct rt2x00_dev, config_work);
  366. int preamble = !test_bit(CONFIG_SHORT_PREAMBLE, &rt2x00dev->flags);
  367. rt2x00mac_erp_ie_changed(rt2x00dev->hw,
  368. IEEE80211_ERP_CHANGE_PREAMBLE, 0, preamble);
  369. }
  370. /*
  371. * Interrupt context handlers.
  372. */
  373. static void rt2x00lib_beacondone_scheduled(struct work_struct *work)
  374. {
  375. struct rt2x00_dev *rt2x00dev =
  376. container_of(work, struct rt2x00_dev, beacon_work);
  377. struct data_ring *ring =
  378. rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_BEACON);
  379. struct data_entry *entry = rt2x00_get_data_entry(ring);
  380. struct sk_buff *skb;
  381. skb = ieee80211_beacon_get(rt2x00dev->hw,
  382. rt2x00dev->interface.id,
  383. &entry->tx_status.control);
  384. if (!skb)
  385. return;
  386. rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw, skb,
  387. &entry->tx_status.control);
  388. dev_kfree_skb(skb);
  389. }
  390. void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
  391. {
  392. if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  393. return;
  394. queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->beacon_work);
  395. }
  396. EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
  397. void rt2x00lib_txdone(struct data_entry *entry,
  398. const int status, const int retry)
  399. {
  400. struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
  401. struct ieee80211_tx_status *tx_status = &entry->tx_status;
  402. struct ieee80211_low_level_stats *stats = &rt2x00dev->low_level_stats;
  403. int success = !!(status == TX_SUCCESS || status == TX_SUCCESS_RETRY);
  404. int fail = !!(status == TX_FAIL_RETRY || status == TX_FAIL_INVALID ||
  405. status == TX_FAIL_OTHER);
  406. /*
  407. * Update TX statistics.
  408. */
  409. tx_status->flags = 0;
  410. tx_status->ack_signal = 0;
  411. tx_status->excessive_retries = (status == TX_FAIL_RETRY);
  412. tx_status->retry_count = retry;
  413. rt2x00dev->link.qual.tx_success += success;
  414. rt2x00dev->link.qual.tx_failed += retry + fail;
  415. if (!(tx_status->control.flags & IEEE80211_TXCTL_NO_ACK)) {
  416. if (success)
  417. tx_status->flags |= IEEE80211_TX_STATUS_ACK;
  418. else
  419. stats->dot11ACKFailureCount++;
  420. }
  421. tx_status->queue_length = entry->ring->stats.limit;
  422. tx_status->queue_number = tx_status->control.queue;
  423. if (tx_status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) {
  424. if (success)
  425. stats->dot11RTSSuccessCount++;
  426. else
  427. stats->dot11RTSFailureCount++;
  428. }
  429. /*
  430. * Send the tx_status to mac80211 & debugfs.
  431. * mac80211 will clean up the skb structure.
  432. */
  433. get_skb_desc(entry->skb)->frame_type = DUMP_FRAME_TXDONE;
  434. rt2x00debug_dump_frame(rt2x00dev, entry->skb);
  435. ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb, tx_status);
  436. entry->skb = NULL;
  437. }
  438. EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
  439. void rt2x00lib_rxdone(struct data_entry *entry, struct sk_buff *skb,
  440. struct rxdata_entry_desc *desc)
  441. {
  442. struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
  443. struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
  444. struct ieee80211_hw_mode *mode;
  445. struct ieee80211_rate *rate;
  446. struct ieee80211_hdr *hdr;
  447. unsigned int i;
  448. int val = 0;
  449. u16 fc;
  450. /*
  451. * Update RX statistics.
  452. */
  453. mode = &rt2x00dev->hwmodes[rt2x00dev->curr_hwmode];
  454. for (i = 0; i < mode->num_rates; i++) {
  455. rate = &mode->rates[i];
  456. /*
  457. * When frame was received with an OFDM bitrate,
  458. * the signal is the PLCP value. If it was received with
  459. * a CCK bitrate the signal is the rate in 0.5kbit/s.
  460. */
  461. if (!desc->ofdm)
  462. val = DEVICE_GET_RATE_FIELD(rate->val, RATE);
  463. else
  464. val = DEVICE_GET_RATE_FIELD(rate->val, PLCP);
  465. if (val == desc->signal) {
  466. val = rate->val;
  467. break;
  468. }
  469. }
  470. /*
  471. * Only update link status if this is a beacon frame carrying our bssid.
  472. */
  473. hdr = (struct ieee80211_hdr*)skb->data;
  474. fc = le16_to_cpu(hdr->frame_control);
  475. if (is_beacon(fc) && desc->my_bss)
  476. rt2x00lib_update_link_stats(&rt2x00dev->link, desc->rssi);
  477. rt2x00dev->link.qual.rx_success++;
  478. rx_status->rate = val;
  479. rx_status->signal =
  480. rt2x00lib_calculate_link_signal(rt2x00dev, desc->rssi);
  481. rx_status->ssi = desc->rssi;
  482. rx_status->flag = desc->flags;
  483. rx_status->antenna = rt2x00dev->link.ant.active.rx;
  484. /*
  485. * Send frame to mac80211 & debugfs
  486. */
  487. get_skb_desc(skb)->frame_type = DUMP_FRAME_RXDONE;
  488. rt2x00debug_dump_frame(rt2x00dev, skb);
  489. ieee80211_rx_irqsafe(rt2x00dev->hw, skb, rx_status);
  490. }
  491. EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
  492. /*
  493. * TX descriptor initializer
  494. */
  495. void rt2x00lib_write_tx_desc(struct rt2x00_dev *rt2x00dev,
  496. struct sk_buff *skb,
  497. struct ieee80211_tx_control *control)
  498. {
  499. struct txdata_entry_desc desc;
  500. struct skb_desc *skbdesc = get_skb_desc(skb);
  501. struct ieee80211_hdr *ieee80211hdr = skbdesc->data;
  502. int tx_rate;
  503. int bitrate;
  504. int length;
  505. int duration;
  506. int residual;
  507. u16 frame_control;
  508. u16 seq_ctrl;
  509. memset(&desc, 0, sizeof(desc));
  510. desc.cw_min = skbdesc->ring->tx_params.cw_min;
  511. desc.cw_max = skbdesc->ring->tx_params.cw_max;
  512. desc.aifs = skbdesc->ring->tx_params.aifs;
  513. /*
  514. * Identify queue
  515. */
  516. if (control->queue < rt2x00dev->hw->queues)
  517. desc.queue = control->queue;
  518. else if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
  519. control->queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
  520. desc.queue = QUEUE_MGMT;
  521. else
  522. desc.queue = QUEUE_OTHER;
  523. /*
  524. * Read required fields from ieee80211 header.
  525. */
  526. frame_control = le16_to_cpu(ieee80211hdr->frame_control);
  527. seq_ctrl = le16_to_cpu(ieee80211hdr->seq_ctrl);
  528. tx_rate = control->tx_rate;
  529. /*
  530. * Check whether this frame is to be acked
  531. */
  532. if (!(control->flags & IEEE80211_TXCTL_NO_ACK))
  533. __set_bit(ENTRY_TXD_ACK, &desc.flags);
  534. /*
  535. * Check if this is a RTS/CTS frame
  536. */
  537. if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
  538. __set_bit(ENTRY_TXD_BURST, &desc.flags);
  539. if (is_rts_frame(frame_control)) {
  540. __set_bit(ENTRY_TXD_RTS_FRAME, &desc.flags);
  541. __set_bit(ENTRY_TXD_ACK, &desc.flags);
  542. } else
  543. __clear_bit(ENTRY_TXD_ACK, &desc.flags);
  544. if (control->rts_cts_rate)
  545. tx_rate = control->rts_cts_rate;
  546. }
  547. /*
  548. * Check for OFDM
  549. */
  550. if (DEVICE_GET_RATE_FIELD(tx_rate, RATEMASK) & DEV_OFDM_RATEMASK)
  551. __set_bit(ENTRY_TXD_OFDM_RATE, &desc.flags);
  552. /*
  553. * Check if more fragments are pending
  554. */
  555. if (ieee80211_get_morefrag(ieee80211hdr)) {
  556. __set_bit(ENTRY_TXD_BURST, &desc.flags);
  557. __set_bit(ENTRY_TXD_MORE_FRAG, &desc.flags);
  558. }
  559. /*
  560. * Beacons and probe responses require the tsf timestamp
  561. * to be inserted into the frame.
  562. */
  563. if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
  564. is_probe_resp(frame_control))
  565. __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &desc.flags);
  566. /*
  567. * Determine with what IFS priority this frame should be send.
  568. * Set ifs to IFS_SIFS when the this is not the first fragment,
  569. * or this fragment came after RTS/CTS.
  570. */
  571. if ((seq_ctrl & IEEE80211_SCTL_FRAG) > 0 ||
  572. test_bit(ENTRY_TXD_RTS_FRAME, &desc.flags))
  573. desc.ifs = IFS_SIFS;
  574. else
  575. desc.ifs = IFS_BACKOFF;
  576. /*
  577. * PLCP setup
  578. * Length calculation depends on OFDM/CCK rate.
  579. */
  580. desc.signal = DEVICE_GET_RATE_FIELD(tx_rate, PLCP);
  581. desc.service = 0x04;
  582. length = skbdesc->data_len + FCS_LEN;
  583. if (test_bit(ENTRY_TXD_OFDM_RATE, &desc.flags)) {
  584. desc.length_high = (length >> 6) & 0x3f;
  585. desc.length_low = length & 0x3f;
  586. } else {
  587. bitrate = DEVICE_GET_RATE_FIELD(tx_rate, RATE);
  588. /*
  589. * Convert length to microseconds.
  590. */
  591. residual = get_duration_res(length, bitrate);
  592. duration = get_duration(length, bitrate);
  593. if (residual != 0) {
  594. duration++;
  595. /*
  596. * Check if we need to set the Length Extension
  597. */
  598. if (bitrate == 110 && residual <= 30)
  599. desc.service |= 0x80;
  600. }
  601. desc.length_high = (duration >> 8) & 0xff;
  602. desc.length_low = duration & 0xff;
  603. /*
  604. * When preamble is enabled we should set the
  605. * preamble bit for the signal.
  606. */
  607. if (DEVICE_GET_RATE_FIELD(tx_rate, PREAMBLE))
  608. desc.signal |= 0x08;
  609. }
  610. rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, skb, &desc, control);
  611. /*
  612. * Update ring entry.
  613. */
  614. skbdesc->entry->skb = skb;
  615. memcpy(&skbdesc->entry->tx_status.control, control, sizeof(*control));
  616. /*
  617. * The frame has been completely initialized and ready
  618. * for sending to the device. The caller will push the
  619. * frame to the device, but we are going to push the
  620. * frame to debugfs here.
  621. */
  622. skbdesc->frame_type = DUMP_FRAME_TX;
  623. rt2x00debug_dump_frame(rt2x00dev, skb);
  624. }
  625. EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc);
  626. /*
  627. * Driver initialization handlers.
  628. */
  629. static void rt2x00lib_channel(struct ieee80211_channel *entry,
  630. const int channel, const int tx_power,
  631. const int value)
  632. {
  633. entry->chan = channel;
  634. if (channel <= 14)
  635. entry->freq = 2407 + (5 * channel);
  636. else
  637. entry->freq = 5000 + (5 * channel);
  638. entry->val = value;
  639. entry->flag =
  640. IEEE80211_CHAN_W_IBSS |
  641. IEEE80211_CHAN_W_ACTIVE_SCAN |
  642. IEEE80211_CHAN_W_SCAN;
  643. entry->power_level = tx_power;
  644. entry->antenna_max = 0xff;
  645. }
  646. static void rt2x00lib_rate(struct ieee80211_rate *entry,
  647. const int rate, const int mask,
  648. const int plcp, const int flags)
  649. {
  650. entry->rate = rate;
  651. entry->val =
  652. DEVICE_SET_RATE_FIELD(rate, RATE) |
  653. DEVICE_SET_RATE_FIELD(mask, RATEMASK) |
  654. DEVICE_SET_RATE_FIELD(plcp, PLCP);
  655. entry->flags = flags;
  656. entry->val2 = entry->val;
  657. if (entry->flags & IEEE80211_RATE_PREAMBLE2)
  658. entry->val2 |= DEVICE_SET_RATE_FIELD(1, PREAMBLE);
  659. entry->min_rssi_ack = 0;
  660. entry->min_rssi_ack_delta = 0;
  661. }
  662. static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
  663. struct hw_mode_spec *spec)
  664. {
  665. struct ieee80211_hw *hw = rt2x00dev->hw;
  666. struct ieee80211_hw_mode *hwmodes;
  667. struct ieee80211_channel *channels;
  668. struct ieee80211_rate *rates;
  669. unsigned int i;
  670. unsigned char tx_power;
  671. hwmodes = kzalloc(sizeof(*hwmodes) * spec->num_modes, GFP_KERNEL);
  672. if (!hwmodes)
  673. goto exit;
  674. channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
  675. if (!channels)
  676. goto exit_free_modes;
  677. rates = kzalloc(sizeof(*rates) * spec->num_rates, GFP_KERNEL);
  678. if (!rates)
  679. goto exit_free_channels;
  680. /*
  681. * Initialize Rate list.
  682. */
  683. rt2x00lib_rate(&rates[0], 10, DEV_RATEMASK_1MB,
  684. 0x00, IEEE80211_RATE_CCK);
  685. rt2x00lib_rate(&rates[1], 20, DEV_RATEMASK_2MB,
  686. 0x01, IEEE80211_RATE_CCK_2);
  687. rt2x00lib_rate(&rates[2], 55, DEV_RATEMASK_5_5MB,
  688. 0x02, IEEE80211_RATE_CCK_2);
  689. rt2x00lib_rate(&rates[3], 110, DEV_RATEMASK_11MB,
  690. 0x03, IEEE80211_RATE_CCK_2);
  691. if (spec->num_rates > 4) {
  692. rt2x00lib_rate(&rates[4], 60, DEV_RATEMASK_6MB,
  693. 0x0b, IEEE80211_RATE_OFDM);
  694. rt2x00lib_rate(&rates[5], 90, DEV_RATEMASK_9MB,
  695. 0x0f, IEEE80211_RATE_OFDM);
  696. rt2x00lib_rate(&rates[6], 120, DEV_RATEMASK_12MB,
  697. 0x0a, IEEE80211_RATE_OFDM);
  698. rt2x00lib_rate(&rates[7], 180, DEV_RATEMASK_18MB,
  699. 0x0e, IEEE80211_RATE_OFDM);
  700. rt2x00lib_rate(&rates[8], 240, DEV_RATEMASK_24MB,
  701. 0x09, IEEE80211_RATE_OFDM);
  702. rt2x00lib_rate(&rates[9], 360, DEV_RATEMASK_36MB,
  703. 0x0d, IEEE80211_RATE_OFDM);
  704. rt2x00lib_rate(&rates[10], 480, DEV_RATEMASK_48MB,
  705. 0x08, IEEE80211_RATE_OFDM);
  706. rt2x00lib_rate(&rates[11], 540, DEV_RATEMASK_54MB,
  707. 0x0c, IEEE80211_RATE_OFDM);
  708. }
  709. /*
  710. * Initialize Channel list.
  711. */
  712. for (i = 0; i < spec->num_channels; i++) {
  713. if (spec->channels[i].channel <= 14)
  714. tx_power = spec->tx_power_bg[i];
  715. else if (spec->tx_power_a)
  716. tx_power = spec->tx_power_a[i];
  717. else
  718. tx_power = spec->tx_power_default;
  719. rt2x00lib_channel(&channels[i],
  720. spec->channels[i].channel, tx_power, i);
  721. }
  722. /*
  723. * Intitialize 802.11b
  724. * Rates: CCK.
  725. * Channels: OFDM.
  726. */
  727. if (spec->num_modes > HWMODE_B) {
  728. hwmodes[HWMODE_B].mode = MODE_IEEE80211B;
  729. hwmodes[HWMODE_B].num_channels = 14;
  730. hwmodes[HWMODE_B].num_rates = 4;
  731. hwmodes[HWMODE_B].channels = channels;
  732. hwmodes[HWMODE_B].rates = rates;
  733. }
  734. /*
  735. * Intitialize 802.11g
  736. * Rates: CCK, OFDM.
  737. * Channels: OFDM.
  738. */
  739. if (spec->num_modes > HWMODE_G) {
  740. hwmodes[HWMODE_G].mode = MODE_IEEE80211G;
  741. hwmodes[HWMODE_G].num_channels = 14;
  742. hwmodes[HWMODE_G].num_rates = spec->num_rates;
  743. hwmodes[HWMODE_G].channels = channels;
  744. hwmodes[HWMODE_G].rates = rates;
  745. }
  746. /*
  747. * Intitialize 802.11a
  748. * Rates: OFDM.
  749. * Channels: OFDM, UNII, HiperLAN2.
  750. */
  751. if (spec->num_modes > HWMODE_A) {
  752. hwmodes[HWMODE_A].mode = MODE_IEEE80211A;
  753. hwmodes[HWMODE_A].num_channels = spec->num_channels - 14;
  754. hwmodes[HWMODE_A].num_rates = spec->num_rates - 4;
  755. hwmodes[HWMODE_A].channels = &channels[14];
  756. hwmodes[HWMODE_A].rates = &rates[4];
  757. }
  758. if (spec->num_modes > HWMODE_G &&
  759. ieee80211_register_hwmode(hw, &hwmodes[HWMODE_G]))
  760. goto exit_free_rates;
  761. if (spec->num_modes > HWMODE_B &&
  762. ieee80211_register_hwmode(hw, &hwmodes[HWMODE_B]))
  763. goto exit_free_rates;
  764. if (spec->num_modes > HWMODE_A &&
  765. ieee80211_register_hwmode(hw, &hwmodes[HWMODE_A]))
  766. goto exit_free_rates;
  767. rt2x00dev->hwmodes = hwmodes;
  768. return 0;
  769. exit_free_rates:
  770. kfree(rates);
  771. exit_free_channels:
  772. kfree(channels);
  773. exit_free_modes:
  774. kfree(hwmodes);
  775. exit:
  776. ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
  777. return -ENOMEM;
  778. }
  779. static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
  780. {
  781. if (test_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags))
  782. ieee80211_unregister_hw(rt2x00dev->hw);
  783. if (likely(rt2x00dev->hwmodes)) {
  784. kfree(rt2x00dev->hwmodes->channels);
  785. kfree(rt2x00dev->hwmodes->rates);
  786. kfree(rt2x00dev->hwmodes);
  787. rt2x00dev->hwmodes = NULL;
  788. }
  789. }
  790. static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
  791. {
  792. struct hw_mode_spec *spec = &rt2x00dev->spec;
  793. int status;
  794. /*
  795. * Initialize HW modes.
  796. */
  797. status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
  798. if (status)
  799. return status;
  800. /*
  801. * Register HW.
  802. */
  803. status = ieee80211_register_hw(rt2x00dev->hw);
  804. if (status) {
  805. rt2x00lib_remove_hw(rt2x00dev);
  806. return status;
  807. }
  808. __set_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags);
  809. return 0;
  810. }
  811. /*
  812. * Initialization/uninitialization handlers.
  813. */
  814. static int rt2x00lib_alloc_entries(struct data_ring *ring,
  815. const u16 max_entries, const u16 data_size,
  816. const u16 desc_size)
  817. {
  818. struct data_entry *entry;
  819. unsigned int i;
  820. ring->stats.limit = max_entries;
  821. ring->data_size = data_size;
  822. ring->desc_size = desc_size;
  823. /*
  824. * Allocate all ring entries.
  825. */
  826. entry = kzalloc(ring->stats.limit * sizeof(*entry), GFP_KERNEL);
  827. if (!entry)
  828. return -ENOMEM;
  829. for (i = 0; i < ring->stats.limit; i++) {
  830. entry[i].flags = 0;
  831. entry[i].ring = ring;
  832. entry[i].skb = NULL;
  833. entry[i].entry_idx = i;
  834. }
  835. ring->entry = entry;
  836. return 0;
  837. }
  838. static int rt2x00lib_alloc_ring_entries(struct rt2x00_dev *rt2x00dev)
  839. {
  840. struct data_ring *ring;
  841. /*
  842. * Allocate the RX ring.
  843. */
  844. if (rt2x00lib_alloc_entries(rt2x00dev->rx, RX_ENTRIES, DATA_FRAME_SIZE,
  845. rt2x00dev->ops->rxd_size))
  846. return -ENOMEM;
  847. /*
  848. * First allocate the TX rings.
  849. */
  850. txring_for_each(rt2x00dev, ring) {
  851. if (rt2x00lib_alloc_entries(ring, TX_ENTRIES, DATA_FRAME_SIZE,
  852. rt2x00dev->ops->txd_size))
  853. return -ENOMEM;
  854. }
  855. if (!test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
  856. return 0;
  857. /*
  858. * Allocate the BEACON ring.
  859. */
  860. if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[0], BEACON_ENTRIES,
  861. MGMT_FRAME_SIZE, rt2x00dev->ops->txd_size))
  862. return -ENOMEM;
  863. /*
  864. * Allocate the Atim ring.
  865. */
  866. if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[1], ATIM_ENTRIES,
  867. DATA_FRAME_SIZE, rt2x00dev->ops->txd_size))
  868. return -ENOMEM;
  869. return 0;
  870. }
  871. static void rt2x00lib_free_ring_entries(struct rt2x00_dev *rt2x00dev)
  872. {
  873. struct data_ring *ring;
  874. ring_for_each(rt2x00dev, ring) {
  875. kfree(ring->entry);
  876. ring->entry = NULL;
  877. }
  878. }
  879. static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
  880. {
  881. if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
  882. return;
  883. /*
  884. * Unregister rfkill.
  885. */
  886. rt2x00rfkill_unregister(rt2x00dev);
  887. /*
  888. * Allow the HW to uninitialize.
  889. */
  890. rt2x00dev->ops->lib->uninitialize(rt2x00dev);
  891. /*
  892. * Free allocated ring entries.
  893. */
  894. rt2x00lib_free_ring_entries(rt2x00dev);
  895. }
  896. static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
  897. {
  898. int status;
  899. if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
  900. return 0;
  901. /*
  902. * Allocate all ring entries.
  903. */
  904. status = rt2x00lib_alloc_ring_entries(rt2x00dev);
  905. if (status) {
  906. ERROR(rt2x00dev, "Ring entries allocation failed.\n");
  907. return status;
  908. }
  909. /*
  910. * Initialize the device.
  911. */
  912. status = rt2x00dev->ops->lib->initialize(rt2x00dev);
  913. if (status)
  914. goto exit;
  915. __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
  916. /*
  917. * Register the rfkill handler.
  918. */
  919. status = rt2x00rfkill_register(rt2x00dev);
  920. if (status)
  921. goto exit_unitialize;
  922. return 0;
  923. exit_unitialize:
  924. rt2x00lib_uninitialize(rt2x00dev);
  925. exit:
  926. rt2x00lib_free_ring_entries(rt2x00dev);
  927. return status;
  928. }
  929. int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
  930. {
  931. int retval;
  932. if (test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  933. return 0;
  934. /*
  935. * If this is the first interface which is added,
  936. * we should load the firmware now.
  937. */
  938. if (test_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags)) {
  939. retval = rt2x00lib_load_firmware(rt2x00dev);
  940. if (retval)
  941. return retval;
  942. }
  943. /*
  944. * Initialize the device.
  945. */
  946. retval = rt2x00lib_initialize(rt2x00dev);
  947. if (retval)
  948. return retval;
  949. /*
  950. * Enable radio.
  951. */
  952. retval = rt2x00lib_enable_radio(rt2x00dev);
  953. if (retval) {
  954. rt2x00lib_uninitialize(rt2x00dev);
  955. return retval;
  956. }
  957. __set_bit(DEVICE_STARTED, &rt2x00dev->flags);
  958. return 0;
  959. }
  960. void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
  961. {
  962. if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  963. return;
  964. /*
  965. * Perhaps we can add something smarter here,
  966. * but for now just disabling the radio should do.
  967. */
  968. rt2x00lib_disable_radio(rt2x00dev);
  969. __clear_bit(DEVICE_STARTED, &rt2x00dev->flags);
  970. }
  971. /*
  972. * driver allocation handlers.
  973. */
  974. static int rt2x00lib_alloc_rings(struct rt2x00_dev *rt2x00dev)
  975. {
  976. struct data_ring *ring;
  977. unsigned int index;
  978. /*
  979. * We need the following rings:
  980. * RX: 1
  981. * TX: hw->queues
  982. * Beacon: 1 (if required)
  983. * Atim: 1 (if required)
  984. */
  985. rt2x00dev->data_rings = 1 + rt2x00dev->hw->queues +
  986. (2 * test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags));
  987. ring = kzalloc(rt2x00dev->data_rings * sizeof(*ring), GFP_KERNEL);
  988. if (!ring) {
  989. ERROR(rt2x00dev, "Ring allocation failed.\n");
  990. return -ENOMEM;
  991. }
  992. /*
  993. * Initialize pointers
  994. */
  995. rt2x00dev->rx = ring;
  996. rt2x00dev->tx = &rt2x00dev->rx[1];
  997. if (test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
  998. rt2x00dev->bcn = &rt2x00dev->tx[rt2x00dev->hw->queues];
  999. /*
  1000. * Initialize ring parameters.
  1001. * cw_min: 2^5 = 32.
  1002. * cw_max: 2^10 = 1024.
  1003. */
  1004. index = 0;
  1005. ring_for_each(rt2x00dev, ring) {
  1006. ring->rt2x00dev = rt2x00dev;
  1007. ring->queue_idx = index++;
  1008. ring->tx_params.aifs = 2;
  1009. ring->tx_params.cw_min = 5;
  1010. ring->tx_params.cw_max = 10;
  1011. }
  1012. return 0;
  1013. }
  1014. static void rt2x00lib_free_rings(struct rt2x00_dev *rt2x00dev)
  1015. {
  1016. kfree(rt2x00dev->rx);
  1017. rt2x00dev->rx = NULL;
  1018. rt2x00dev->tx = NULL;
  1019. rt2x00dev->bcn = NULL;
  1020. }
  1021. int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
  1022. {
  1023. int retval = -ENOMEM;
  1024. /*
  1025. * Let the driver probe the device to detect the capabilities.
  1026. */
  1027. retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
  1028. if (retval) {
  1029. ERROR(rt2x00dev, "Failed to allocate device.\n");
  1030. goto exit;
  1031. }
  1032. /*
  1033. * Initialize configuration work.
  1034. */
  1035. INIT_WORK(&rt2x00dev->beacon_work, rt2x00lib_beacondone_scheduled);
  1036. INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
  1037. INIT_WORK(&rt2x00dev->config_work, rt2x00lib_configuration_scheduled);
  1038. INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
  1039. /*
  1040. * Reset current working type.
  1041. */
  1042. rt2x00dev->interface.type = IEEE80211_IF_TYPE_INVALID;
  1043. /*
  1044. * Allocate ring array.
  1045. */
  1046. retval = rt2x00lib_alloc_rings(rt2x00dev);
  1047. if (retval)
  1048. goto exit;
  1049. /*
  1050. * Initialize ieee80211 structure.
  1051. */
  1052. retval = rt2x00lib_probe_hw(rt2x00dev);
  1053. if (retval) {
  1054. ERROR(rt2x00dev, "Failed to initialize hw.\n");
  1055. goto exit;
  1056. }
  1057. /*
  1058. * Allocatie rfkill.
  1059. */
  1060. retval = rt2x00rfkill_allocate(rt2x00dev);
  1061. if (retval)
  1062. goto exit;
  1063. /*
  1064. * Open the debugfs entry.
  1065. */
  1066. rt2x00debug_register(rt2x00dev);
  1067. __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  1068. return 0;
  1069. exit:
  1070. rt2x00lib_remove_dev(rt2x00dev);
  1071. return retval;
  1072. }
  1073. EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
  1074. void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
  1075. {
  1076. __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  1077. /*
  1078. * Disable radio.
  1079. */
  1080. rt2x00lib_disable_radio(rt2x00dev);
  1081. /*
  1082. * Uninitialize device.
  1083. */
  1084. rt2x00lib_uninitialize(rt2x00dev);
  1085. /*
  1086. * Close debugfs entry.
  1087. */
  1088. rt2x00debug_deregister(rt2x00dev);
  1089. /*
  1090. * Free rfkill
  1091. */
  1092. rt2x00rfkill_free(rt2x00dev);
  1093. /*
  1094. * Free ieee80211_hw memory.
  1095. */
  1096. rt2x00lib_remove_hw(rt2x00dev);
  1097. /*
  1098. * Free firmware image.
  1099. */
  1100. rt2x00lib_free_firmware(rt2x00dev);
  1101. /*
  1102. * Free ring structures.
  1103. */
  1104. rt2x00lib_free_rings(rt2x00dev);
  1105. }
  1106. EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
  1107. /*
  1108. * Device state handlers
  1109. */
  1110. #ifdef CONFIG_PM
  1111. int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
  1112. {
  1113. int retval;
  1114. NOTICE(rt2x00dev, "Going to sleep.\n");
  1115. __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  1116. /*
  1117. * Only continue if mac80211 has open interfaces.
  1118. */
  1119. if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  1120. goto exit;
  1121. __set_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags);
  1122. /*
  1123. * Disable radio and unitialize all items
  1124. * that must be recreated on resume.
  1125. */
  1126. rt2x00lib_stop(rt2x00dev);
  1127. rt2x00lib_uninitialize(rt2x00dev);
  1128. rt2x00debug_deregister(rt2x00dev);
  1129. exit:
  1130. /*
  1131. * Set device mode to sleep for power management.
  1132. */
  1133. retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
  1134. if (retval)
  1135. return retval;
  1136. return 0;
  1137. }
  1138. EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
  1139. int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
  1140. {
  1141. struct interface *intf = &rt2x00dev->interface;
  1142. int retval;
  1143. NOTICE(rt2x00dev, "Waking up.\n");
  1144. /*
  1145. * Open the debugfs entry.
  1146. */
  1147. rt2x00debug_register(rt2x00dev);
  1148. /*
  1149. * Only continue if mac80211 had open interfaces.
  1150. */
  1151. if (!__test_and_clear_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags))
  1152. return 0;
  1153. /*
  1154. * Reinitialize device and all active interfaces.
  1155. */
  1156. retval = rt2x00lib_start(rt2x00dev);
  1157. if (retval)
  1158. goto exit;
  1159. /*
  1160. * Reconfigure device.
  1161. */
  1162. rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1);
  1163. if (!rt2x00dev->hw->conf.radio_enabled)
  1164. rt2x00lib_disable_radio(rt2x00dev);
  1165. rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
  1166. rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
  1167. rt2x00lib_config_type(rt2x00dev, intf->type);
  1168. /*
  1169. * We are ready again to receive requests from mac80211.
  1170. */
  1171. __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  1172. /*
  1173. * It is possible that during that mac80211 has attempted
  1174. * to send frames while we were suspending or resuming.
  1175. * In that case we have disabled the TX queue and should
  1176. * now enable it again
  1177. */
  1178. ieee80211_start_queues(rt2x00dev->hw);
  1179. /*
  1180. * When in Master or Ad-hoc mode,
  1181. * restart Beacon transmitting by faking a beacondone event.
  1182. */
  1183. if (intf->type == IEEE80211_IF_TYPE_AP ||
  1184. intf->type == IEEE80211_IF_TYPE_IBSS)
  1185. rt2x00lib_beacondone(rt2x00dev);
  1186. return 0;
  1187. exit:
  1188. rt2x00lib_disable_radio(rt2x00dev);
  1189. rt2x00lib_uninitialize(rt2x00dev);
  1190. rt2x00debug_deregister(rt2x00dev);
  1191. return retval;
  1192. }
  1193. EXPORT_SYMBOL_GPL(rt2x00lib_resume);
  1194. #endif /* CONFIG_PM */
  1195. /*
  1196. * rt2x00lib module information.
  1197. */
  1198. MODULE_AUTHOR(DRV_PROJECT);
  1199. MODULE_VERSION(DRV_VERSION);
  1200. MODULE_DESCRIPTION("rt2x00 library");
  1201. MODULE_LICENSE("GPL");