rt2x00dev.c 35 KB

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