rt2x00dev.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492
  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 rfkill.
  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 rfkill handler.
  962. */
  963. status = rt2x00rfkill_register(rt2x00dev);
  964. if (status)
  965. goto exit_unitialize;
  966. return 0;
  967. exit_unitialize:
  968. rt2x00lib_uninitialize(rt2x00dev);
  969. exit:
  970. rt2x00lib_free_ring_entries(rt2x00dev);
  971. return status;
  972. }
  973. int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
  974. {
  975. int retval;
  976. if (test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  977. return 0;
  978. /*
  979. * If this is the first interface which is added,
  980. * we should load the firmware now.
  981. */
  982. if (test_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags)) {
  983. retval = rt2x00lib_load_firmware(rt2x00dev);
  984. if (retval)
  985. return retval;
  986. }
  987. /*
  988. * Initialize the device.
  989. */
  990. retval = rt2x00lib_initialize(rt2x00dev);
  991. if (retval)
  992. return retval;
  993. /*
  994. * Enable radio.
  995. */
  996. retval = rt2x00lib_enable_radio(rt2x00dev);
  997. if (retval) {
  998. rt2x00lib_uninitialize(rt2x00dev);
  999. return retval;
  1000. }
  1001. __set_bit(DEVICE_STARTED, &rt2x00dev->flags);
  1002. return 0;
  1003. }
  1004. void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
  1005. {
  1006. if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  1007. return;
  1008. /*
  1009. * Perhaps we can add something smarter here,
  1010. * but for now just disabling the radio should do.
  1011. */
  1012. rt2x00lib_disable_radio(rt2x00dev);
  1013. __clear_bit(DEVICE_STARTED, &rt2x00dev->flags);
  1014. }
  1015. /*
  1016. * driver allocation handlers.
  1017. */
  1018. static int rt2x00lib_alloc_rings(struct rt2x00_dev *rt2x00dev)
  1019. {
  1020. struct data_ring *ring;
  1021. unsigned int index;
  1022. /*
  1023. * We need the following rings:
  1024. * RX: 1
  1025. * TX: hw->queues
  1026. * Beacon: 1 (if required)
  1027. * Atim: 1 (if required)
  1028. */
  1029. rt2x00dev->data_rings = 1 + rt2x00dev->hw->queues +
  1030. (2 * test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags));
  1031. ring = kzalloc(rt2x00dev->data_rings * sizeof(*ring), GFP_KERNEL);
  1032. if (!ring) {
  1033. ERROR(rt2x00dev, "Ring allocation failed.\n");
  1034. return -ENOMEM;
  1035. }
  1036. /*
  1037. * Initialize pointers
  1038. */
  1039. rt2x00dev->rx = ring;
  1040. rt2x00dev->tx = &rt2x00dev->rx[1];
  1041. if (test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
  1042. rt2x00dev->bcn = &rt2x00dev->tx[rt2x00dev->hw->queues];
  1043. /*
  1044. * Initialize ring parameters.
  1045. * RX: queue_idx = 0
  1046. * TX: queue_idx = IEEE80211_TX_QUEUE_DATA0 + index
  1047. * TX: cw_min: 2^5 = 32.
  1048. * TX: cw_max: 2^10 = 1024.
  1049. */
  1050. rt2x00dev->rx->rt2x00dev = rt2x00dev;
  1051. rt2x00dev->rx->queue_idx = 0;
  1052. index = IEEE80211_TX_QUEUE_DATA0;
  1053. txring_for_each(rt2x00dev, ring) {
  1054. ring->rt2x00dev = rt2x00dev;
  1055. ring->queue_idx = index++;
  1056. ring->tx_params.aifs = 2;
  1057. ring->tx_params.cw_min = 5;
  1058. ring->tx_params.cw_max = 10;
  1059. }
  1060. return 0;
  1061. }
  1062. static void rt2x00lib_free_rings(struct rt2x00_dev *rt2x00dev)
  1063. {
  1064. kfree(rt2x00dev->rx);
  1065. rt2x00dev->rx = NULL;
  1066. rt2x00dev->tx = NULL;
  1067. rt2x00dev->bcn = NULL;
  1068. }
  1069. int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
  1070. {
  1071. int retval = -ENOMEM;
  1072. /*
  1073. * Let the driver probe the device to detect the capabilities.
  1074. */
  1075. retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
  1076. if (retval) {
  1077. ERROR(rt2x00dev, "Failed to allocate device.\n");
  1078. goto exit;
  1079. }
  1080. /*
  1081. * Initialize configuration work.
  1082. */
  1083. INIT_WORK(&rt2x00dev->beacon_work, rt2x00lib_beacondone_scheduled);
  1084. INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
  1085. INIT_WORK(&rt2x00dev->config_work, rt2x00lib_configuration_scheduled);
  1086. INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
  1087. /*
  1088. * Reset current working type.
  1089. */
  1090. rt2x00dev->interface.type = IEEE80211_IF_TYPE_INVALID;
  1091. /*
  1092. * Allocate ring array.
  1093. */
  1094. retval = rt2x00lib_alloc_rings(rt2x00dev);
  1095. if (retval)
  1096. goto exit;
  1097. /*
  1098. * Initialize ieee80211 structure.
  1099. */
  1100. retval = rt2x00lib_probe_hw(rt2x00dev);
  1101. if (retval) {
  1102. ERROR(rt2x00dev, "Failed to initialize hw.\n");
  1103. goto exit;
  1104. }
  1105. /*
  1106. * Allocatie rfkill.
  1107. */
  1108. retval = rt2x00rfkill_allocate(rt2x00dev);
  1109. if (retval)
  1110. goto exit;
  1111. /*
  1112. * Open the debugfs entry.
  1113. */
  1114. rt2x00debug_register(rt2x00dev);
  1115. __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  1116. return 0;
  1117. exit:
  1118. rt2x00lib_remove_dev(rt2x00dev);
  1119. return retval;
  1120. }
  1121. EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
  1122. void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
  1123. {
  1124. __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  1125. /*
  1126. * Disable radio.
  1127. */
  1128. rt2x00lib_disable_radio(rt2x00dev);
  1129. /*
  1130. * Uninitialize device.
  1131. */
  1132. rt2x00lib_uninitialize(rt2x00dev);
  1133. /*
  1134. * Close debugfs entry.
  1135. */
  1136. rt2x00debug_deregister(rt2x00dev);
  1137. /*
  1138. * Free rfkill
  1139. */
  1140. rt2x00rfkill_free(rt2x00dev);
  1141. /*
  1142. * Free ieee80211_hw memory.
  1143. */
  1144. rt2x00lib_remove_hw(rt2x00dev);
  1145. /*
  1146. * Free firmware image.
  1147. */
  1148. rt2x00lib_free_firmware(rt2x00dev);
  1149. /*
  1150. * Free ring structures.
  1151. */
  1152. rt2x00lib_free_rings(rt2x00dev);
  1153. }
  1154. EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
  1155. /*
  1156. * Device state handlers
  1157. */
  1158. #ifdef CONFIG_PM
  1159. int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
  1160. {
  1161. int retval;
  1162. NOTICE(rt2x00dev, "Going to sleep.\n");
  1163. __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  1164. /*
  1165. * Only continue if mac80211 has open interfaces.
  1166. */
  1167. if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  1168. goto exit;
  1169. __set_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags);
  1170. /*
  1171. * Disable radio and unitialize all items
  1172. * that must be recreated on resume.
  1173. */
  1174. rt2x00lib_stop(rt2x00dev);
  1175. rt2x00lib_uninitialize(rt2x00dev);
  1176. rt2x00debug_deregister(rt2x00dev);
  1177. exit:
  1178. /*
  1179. * Set device mode to sleep for power management.
  1180. */
  1181. retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
  1182. if (retval)
  1183. return retval;
  1184. return 0;
  1185. }
  1186. EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
  1187. int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
  1188. {
  1189. struct interface *intf = &rt2x00dev->interface;
  1190. int retval;
  1191. NOTICE(rt2x00dev, "Waking up.\n");
  1192. /*
  1193. * Open the debugfs entry.
  1194. */
  1195. rt2x00debug_register(rt2x00dev);
  1196. /*
  1197. * Only continue if mac80211 had open interfaces.
  1198. */
  1199. if (!__test_and_clear_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags))
  1200. return 0;
  1201. /*
  1202. * Reinitialize device and all active interfaces.
  1203. */
  1204. retval = rt2x00lib_start(rt2x00dev);
  1205. if (retval)
  1206. goto exit;
  1207. /*
  1208. * Reconfigure device.
  1209. */
  1210. rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1);
  1211. if (!rt2x00dev->hw->conf.radio_enabled)
  1212. rt2x00lib_disable_radio(rt2x00dev);
  1213. rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
  1214. rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
  1215. rt2x00lib_config_type(rt2x00dev, intf->type);
  1216. /*
  1217. * We are ready again to receive requests from mac80211.
  1218. */
  1219. __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  1220. /*
  1221. * It is possible that during that mac80211 has attempted
  1222. * to send frames while we were suspending or resuming.
  1223. * In that case we have disabled the TX queue and should
  1224. * now enable it again
  1225. */
  1226. ieee80211_start_queues(rt2x00dev->hw);
  1227. /*
  1228. * When in Master or Ad-hoc mode,
  1229. * restart Beacon transmitting by faking a beacondone event.
  1230. */
  1231. if (intf->type == IEEE80211_IF_TYPE_AP ||
  1232. intf->type == IEEE80211_IF_TYPE_IBSS)
  1233. rt2x00lib_beacondone(rt2x00dev);
  1234. return 0;
  1235. exit:
  1236. rt2x00lib_disable_radio(rt2x00dev);
  1237. rt2x00lib_uninitialize(rt2x00dev);
  1238. rt2x00debug_deregister(rt2x00dev);
  1239. return retval;
  1240. }
  1241. EXPORT_SYMBOL_GPL(rt2x00lib_resume);
  1242. #endif /* CONFIG_PM */
  1243. /*
  1244. * rt2x00lib module information.
  1245. */
  1246. MODULE_AUTHOR(DRV_PROJECT);
  1247. MODULE_VERSION(DRV_VERSION);
  1248. MODULE_DESCRIPTION("rt2x00 library");
  1249. MODULE_LICENSE("GPL");