rt2x00dev.c 36 KB

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