rt2x00dev.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  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. struct ieee80211_bss_conf bss_conf;
  402. bss_conf.use_short_preamble =
  403. test_bit(CONFIG_SHORT_PREAMBLE, &rt2x00dev->flags);
  404. /*
  405. * FIXME: shouldn't invoke it this way because all other contents
  406. * of bss_conf is invalid.
  407. */
  408. rt2x00mac_bss_info_changed(rt2x00dev->hw, rt2x00dev->interface.id,
  409. &bss_conf, BSS_CHANGED_ERP_PREAMBLE);
  410. }
  411. /*
  412. * Interrupt context handlers.
  413. */
  414. static void rt2x00lib_beacondone_scheduled(struct work_struct *work)
  415. {
  416. struct rt2x00_dev *rt2x00dev =
  417. container_of(work, struct rt2x00_dev, beacon_work);
  418. struct data_ring *ring =
  419. rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_BEACON);
  420. struct data_entry *entry = rt2x00_get_data_entry(ring);
  421. struct sk_buff *skb;
  422. skb = ieee80211_beacon_get(rt2x00dev->hw,
  423. rt2x00dev->interface.id,
  424. &entry->tx_status.control);
  425. if (!skb)
  426. return;
  427. rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw, skb,
  428. &entry->tx_status.control);
  429. dev_kfree_skb(skb);
  430. }
  431. void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
  432. {
  433. if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  434. return;
  435. queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->beacon_work);
  436. }
  437. EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
  438. void rt2x00lib_txdone(struct data_entry *entry,
  439. const int status, const int retry)
  440. {
  441. struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
  442. struct ieee80211_tx_status *tx_status = &entry->tx_status;
  443. struct ieee80211_low_level_stats *stats = &rt2x00dev->low_level_stats;
  444. int success = !!(status == TX_SUCCESS || status == TX_SUCCESS_RETRY);
  445. int fail = !!(status == TX_FAIL_RETRY || status == TX_FAIL_INVALID ||
  446. status == TX_FAIL_OTHER);
  447. /*
  448. * Update TX statistics.
  449. */
  450. tx_status->flags = 0;
  451. tx_status->ack_signal = 0;
  452. tx_status->excessive_retries = (status == TX_FAIL_RETRY);
  453. tx_status->retry_count = retry;
  454. rt2x00dev->link.qual.tx_success += success;
  455. rt2x00dev->link.qual.tx_failed += retry + fail;
  456. if (!(tx_status->control.flags & IEEE80211_TXCTL_NO_ACK)) {
  457. if (success)
  458. tx_status->flags |= IEEE80211_TX_STATUS_ACK;
  459. else
  460. stats->dot11ACKFailureCount++;
  461. }
  462. tx_status->queue_length = entry->ring->stats.limit;
  463. tx_status->queue_number = tx_status->control.queue;
  464. if (tx_status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) {
  465. if (success)
  466. stats->dot11RTSSuccessCount++;
  467. else
  468. stats->dot11RTSFailureCount++;
  469. }
  470. /*
  471. * Send the tx_status to mac80211 & debugfs.
  472. * mac80211 will clean up the skb structure.
  473. */
  474. get_skb_desc(entry->skb)->frame_type = DUMP_FRAME_TXDONE;
  475. rt2x00debug_dump_frame(rt2x00dev, entry->skb);
  476. ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb, tx_status);
  477. entry->skb = NULL;
  478. }
  479. EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
  480. void rt2x00lib_rxdone(struct data_entry *entry, struct sk_buff *skb,
  481. struct rxdata_entry_desc *desc)
  482. {
  483. struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
  484. struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
  485. struct ieee80211_hw_mode *mode;
  486. struct ieee80211_rate *rate;
  487. struct ieee80211_hdr *hdr;
  488. unsigned int i;
  489. int val = 0;
  490. u16 fc;
  491. /*
  492. * Update RX statistics.
  493. */
  494. mode = &rt2x00dev->hwmodes[rt2x00dev->curr_hwmode];
  495. for (i = 0; i < mode->num_rates; i++) {
  496. rate = &mode->rates[i];
  497. /*
  498. * When frame was received with an OFDM bitrate,
  499. * the signal is the PLCP value. If it was received with
  500. * a CCK bitrate the signal is the rate in 0.5kbit/s.
  501. */
  502. if (!desc->ofdm)
  503. val = DEVICE_GET_RATE_FIELD(rate->val, RATE);
  504. else
  505. val = DEVICE_GET_RATE_FIELD(rate->val, PLCP);
  506. if (val == desc->signal) {
  507. val = rate->val;
  508. break;
  509. }
  510. }
  511. /*
  512. * Only update link status if this is a beacon frame carrying our bssid.
  513. */
  514. hdr = (struct ieee80211_hdr*)skb->data;
  515. fc = le16_to_cpu(hdr->frame_control);
  516. if (is_beacon(fc) && desc->my_bss)
  517. rt2x00lib_update_link_stats(&rt2x00dev->link, desc->rssi);
  518. rt2x00dev->link.qual.rx_success++;
  519. rx_status->rate = val;
  520. rx_status->signal =
  521. rt2x00lib_calculate_link_signal(rt2x00dev, desc->rssi);
  522. rx_status->ssi = desc->rssi;
  523. rx_status->flag = desc->flags;
  524. rx_status->antenna = rt2x00dev->link.ant.active.rx;
  525. /*
  526. * Send frame to mac80211 & debugfs
  527. */
  528. get_skb_desc(skb)->frame_type = DUMP_FRAME_RXDONE;
  529. rt2x00debug_dump_frame(rt2x00dev, skb);
  530. ieee80211_rx_irqsafe(rt2x00dev->hw, skb, rx_status);
  531. }
  532. EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
  533. /*
  534. * TX descriptor initializer
  535. */
  536. void rt2x00lib_write_tx_desc(struct rt2x00_dev *rt2x00dev,
  537. struct sk_buff *skb,
  538. struct ieee80211_tx_control *control)
  539. {
  540. struct txdata_entry_desc desc;
  541. struct skb_desc *skbdesc = get_skb_desc(skb);
  542. struct ieee80211_hdr *ieee80211hdr = skbdesc->data;
  543. int tx_rate;
  544. int bitrate;
  545. int length;
  546. int duration;
  547. int residual;
  548. u16 frame_control;
  549. u16 seq_ctrl;
  550. memset(&desc, 0, sizeof(desc));
  551. desc.cw_min = skbdesc->ring->tx_params.cw_min;
  552. desc.cw_max = skbdesc->ring->tx_params.cw_max;
  553. desc.aifs = skbdesc->ring->tx_params.aifs;
  554. /*
  555. * Identify queue
  556. */
  557. if (control->queue < rt2x00dev->hw->queues)
  558. desc.queue = control->queue;
  559. else if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
  560. control->queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
  561. desc.queue = QUEUE_MGMT;
  562. else
  563. desc.queue = QUEUE_OTHER;
  564. /*
  565. * Read required fields from ieee80211 header.
  566. */
  567. frame_control = le16_to_cpu(ieee80211hdr->frame_control);
  568. seq_ctrl = le16_to_cpu(ieee80211hdr->seq_ctrl);
  569. tx_rate = control->tx_rate;
  570. /*
  571. * Check whether this frame is to be acked
  572. */
  573. if (!(control->flags & IEEE80211_TXCTL_NO_ACK))
  574. __set_bit(ENTRY_TXD_ACK, &desc.flags);
  575. /*
  576. * Check if this is a RTS/CTS frame
  577. */
  578. if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
  579. __set_bit(ENTRY_TXD_BURST, &desc.flags);
  580. if (is_rts_frame(frame_control)) {
  581. __set_bit(ENTRY_TXD_RTS_FRAME, &desc.flags);
  582. __set_bit(ENTRY_TXD_ACK, &desc.flags);
  583. } else
  584. __clear_bit(ENTRY_TXD_ACK, &desc.flags);
  585. if (control->rts_cts_rate)
  586. tx_rate = control->rts_cts_rate;
  587. }
  588. /*
  589. * Check for OFDM
  590. */
  591. if (DEVICE_GET_RATE_FIELD(tx_rate, RATEMASK) & DEV_OFDM_RATEMASK)
  592. __set_bit(ENTRY_TXD_OFDM_RATE, &desc.flags);
  593. /*
  594. * Check if more fragments are pending
  595. */
  596. if (ieee80211_get_morefrag(ieee80211hdr)) {
  597. __set_bit(ENTRY_TXD_BURST, &desc.flags);
  598. __set_bit(ENTRY_TXD_MORE_FRAG, &desc.flags);
  599. }
  600. /*
  601. * Beacons and probe responses require the tsf timestamp
  602. * to be inserted into the frame.
  603. */
  604. if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
  605. is_probe_resp(frame_control))
  606. __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &desc.flags);
  607. /*
  608. * Determine with what IFS priority this frame should be send.
  609. * Set ifs to IFS_SIFS when the this is not the first fragment,
  610. * or this fragment came after RTS/CTS.
  611. */
  612. if ((seq_ctrl & IEEE80211_SCTL_FRAG) > 0 ||
  613. test_bit(ENTRY_TXD_RTS_FRAME, &desc.flags))
  614. desc.ifs = IFS_SIFS;
  615. else
  616. desc.ifs = IFS_BACKOFF;
  617. /*
  618. * PLCP setup
  619. * Length calculation depends on OFDM/CCK rate.
  620. */
  621. desc.signal = DEVICE_GET_RATE_FIELD(tx_rate, PLCP);
  622. desc.service = 0x04;
  623. length = skbdesc->data_len + FCS_LEN;
  624. if (test_bit(ENTRY_TXD_OFDM_RATE, &desc.flags)) {
  625. desc.length_high = (length >> 6) & 0x3f;
  626. desc.length_low = length & 0x3f;
  627. } else {
  628. bitrate = DEVICE_GET_RATE_FIELD(tx_rate, RATE);
  629. /*
  630. * Convert length to microseconds.
  631. */
  632. residual = get_duration_res(length, bitrate);
  633. duration = get_duration(length, bitrate);
  634. if (residual != 0) {
  635. duration++;
  636. /*
  637. * Check if we need to set the Length Extension
  638. */
  639. if (bitrate == 110 && residual <= 30)
  640. desc.service |= 0x80;
  641. }
  642. desc.length_high = (duration >> 8) & 0xff;
  643. desc.length_low = duration & 0xff;
  644. /*
  645. * When preamble is enabled we should set the
  646. * preamble bit for the signal.
  647. */
  648. if (DEVICE_GET_RATE_FIELD(tx_rate, PREAMBLE))
  649. desc.signal |= 0x08;
  650. }
  651. rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, skb, &desc, control);
  652. /*
  653. * Update ring entry.
  654. */
  655. skbdesc->entry->skb = skb;
  656. memcpy(&skbdesc->entry->tx_status.control, control, sizeof(*control));
  657. /*
  658. * The frame has been completely initialized and ready
  659. * for sending to the device. The caller will push the
  660. * frame to the device, but we are going to push the
  661. * frame to debugfs here.
  662. */
  663. skbdesc->frame_type = DUMP_FRAME_TX;
  664. rt2x00debug_dump_frame(rt2x00dev, skb);
  665. }
  666. EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc);
  667. /*
  668. * Driver initialization handlers.
  669. */
  670. static void rt2x00lib_channel(struct ieee80211_channel *entry,
  671. const int channel, const int tx_power,
  672. const int value)
  673. {
  674. entry->chan = channel;
  675. if (channel <= 14)
  676. entry->freq = 2407 + (5 * channel);
  677. else
  678. entry->freq = 5000 + (5 * channel);
  679. entry->val = value;
  680. entry->flag =
  681. IEEE80211_CHAN_W_IBSS |
  682. IEEE80211_CHAN_W_ACTIVE_SCAN |
  683. IEEE80211_CHAN_W_SCAN;
  684. entry->power_level = tx_power;
  685. entry->antenna_max = 0xff;
  686. }
  687. static void rt2x00lib_rate(struct ieee80211_rate *entry,
  688. const int rate, const int mask,
  689. const int plcp, const int flags)
  690. {
  691. entry->rate = rate;
  692. entry->val =
  693. DEVICE_SET_RATE_FIELD(rate, RATE) |
  694. DEVICE_SET_RATE_FIELD(mask, RATEMASK) |
  695. DEVICE_SET_RATE_FIELD(plcp, PLCP);
  696. entry->flags = flags;
  697. entry->val2 = entry->val;
  698. if (entry->flags & IEEE80211_RATE_PREAMBLE2)
  699. entry->val2 |= DEVICE_SET_RATE_FIELD(1, PREAMBLE);
  700. entry->min_rssi_ack = 0;
  701. entry->min_rssi_ack_delta = 0;
  702. }
  703. static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
  704. struct hw_mode_spec *spec)
  705. {
  706. struct ieee80211_hw *hw = rt2x00dev->hw;
  707. struct ieee80211_hw_mode *hwmodes;
  708. struct ieee80211_channel *channels;
  709. struct ieee80211_rate *rates;
  710. unsigned int i;
  711. unsigned char tx_power;
  712. hwmodes = kzalloc(sizeof(*hwmodes) * spec->num_modes, GFP_KERNEL);
  713. if (!hwmodes)
  714. goto exit;
  715. channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
  716. if (!channels)
  717. goto exit_free_modes;
  718. rates = kzalloc(sizeof(*rates) * spec->num_rates, GFP_KERNEL);
  719. if (!rates)
  720. goto exit_free_channels;
  721. /*
  722. * Initialize Rate list.
  723. */
  724. rt2x00lib_rate(&rates[0], 10, DEV_RATEMASK_1MB,
  725. 0x00, IEEE80211_RATE_CCK);
  726. rt2x00lib_rate(&rates[1], 20, DEV_RATEMASK_2MB,
  727. 0x01, IEEE80211_RATE_CCK_2);
  728. rt2x00lib_rate(&rates[2], 55, DEV_RATEMASK_5_5MB,
  729. 0x02, IEEE80211_RATE_CCK_2);
  730. rt2x00lib_rate(&rates[3], 110, DEV_RATEMASK_11MB,
  731. 0x03, IEEE80211_RATE_CCK_2);
  732. if (spec->num_rates > 4) {
  733. rt2x00lib_rate(&rates[4], 60, DEV_RATEMASK_6MB,
  734. 0x0b, IEEE80211_RATE_OFDM);
  735. rt2x00lib_rate(&rates[5], 90, DEV_RATEMASK_9MB,
  736. 0x0f, IEEE80211_RATE_OFDM);
  737. rt2x00lib_rate(&rates[6], 120, DEV_RATEMASK_12MB,
  738. 0x0a, IEEE80211_RATE_OFDM);
  739. rt2x00lib_rate(&rates[7], 180, DEV_RATEMASK_18MB,
  740. 0x0e, IEEE80211_RATE_OFDM);
  741. rt2x00lib_rate(&rates[8], 240, DEV_RATEMASK_24MB,
  742. 0x09, IEEE80211_RATE_OFDM);
  743. rt2x00lib_rate(&rates[9], 360, DEV_RATEMASK_36MB,
  744. 0x0d, IEEE80211_RATE_OFDM);
  745. rt2x00lib_rate(&rates[10], 480, DEV_RATEMASK_48MB,
  746. 0x08, IEEE80211_RATE_OFDM);
  747. rt2x00lib_rate(&rates[11], 540, DEV_RATEMASK_54MB,
  748. 0x0c, IEEE80211_RATE_OFDM);
  749. }
  750. /*
  751. * Initialize Channel list.
  752. */
  753. for (i = 0; i < spec->num_channels; i++) {
  754. if (spec->channels[i].channel <= 14)
  755. tx_power = spec->tx_power_bg[i];
  756. else if (spec->tx_power_a)
  757. tx_power = spec->tx_power_a[i];
  758. else
  759. tx_power = spec->tx_power_default;
  760. rt2x00lib_channel(&channels[i],
  761. spec->channels[i].channel, tx_power, i);
  762. }
  763. /*
  764. * Intitialize 802.11b
  765. * Rates: CCK.
  766. * Channels: OFDM.
  767. */
  768. if (spec->num_modes > HWMODE_B) {
  769. hwmodes[HWMODE_B].mode = MODE_IEEE80211B;
  770. hwmodes[HWMODE_B].num_channels = 14;
  771. hwmodes[HWMODE_B].num_rates = 4;
  772. hwmodes[HWMODE_B].channels = channels;
  773. hwmodes[HWMODE_B].rates = rates;
  774. }
  775. /*
  776. * Intitialize 802.11g
  777. * Rates: CCK, OFDM.
  778. * Channels: OFDM.
  779. */
  780. if (spec->num_modes > HWMODE_G) {
  781. hwmodes[HWMODE_G].mode = MODE_IEEE80211G;
  782. hwmodes[HWMODE_G].num_channels = 14;
  783. hwmodes[HWMODE_G].num_rates = spec->num_rates;
  784. hwmodes[HWMODE_G].channels = channels;
  785. hwmodes[HWMODE_G].rates = rates;
  786. }
  787. /*
  788. * Intitialize 802.11a
  789. * Rates: OFDM.
  790. * Channels: OFDM, UNII, HiperLAN2.
  791. */
  792. if (spec->num_modes > HWMODE_A) {
  793. hwmodes[HWMODE_A].mode = MODE_IEEE80211A;
  794. hwmodes[HWMODE_A].num_channels = spec->num_channels - 14;
  795. hwmodes[HWMODE_A].num_rates = spec->num_rates - 4;
  796. hwmodes[HWMODE_A].channels = &channels[14];
  797. hwmodes[HWMODE_A].rates = &rates[4];
  798. }
  799. if (spec->num_modes > HWMODE_G &&
  800. ieee80211_register_hwmode(hw, &hwmodes[HWMODE_G]))
  801. goto exit_free_rates;
  802. if (spec->num_modes > HWMODE_B &&
  803. ieee80211_register_hwmode(hw, &hwmodes[HWMODE_B]))
  804. goto exit_free_rates;
  805. if (spec->num_modes > HWMODE_A &&
  806. ieee80211_register_hwmode(hw, &hwmodes[HWMODE_A]))
  807. goto exit_free_rates;
  808. rt2x00dev->hwmodes = hwmodes;
  809. return 0;
  810. exit_free_rates:
  811. kfree(rates);
  812. exit_free_channels:
  813. kfree(channels);
  814. exit_free_modes:
  815. kfree(hwmodes);
  816. exit:
  817. ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
  818. return -ENOMEM;
  819. }
  820. static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
  821. {
  822. if (test_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags))
  823. ieee80211_unregister_hw(rt2x00dev->hw);
  824. if (likely(rt2x00dev->hwmodes)) {
  825. kfree(rt2x00dev->hwmodes->channels);
  826. kfree(rt2x00dev->hwmodes->rates);
  827. kfree(rt2x00dev->hwmodes);
  828. rt2x00dev->hwmodes = NULL;
  829. }
  830. }
  831. static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
  832. {
  833. struct hw_mode_spec *spec = &rt2x00dev->spec;
  834. int status;
  835. /*
  836. * Initialize HW modes.
  837. */
  838. status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
  839. if (status)
  840. return status;
  841. /*
  842. * Register HW.
  843. */
  844. status = ieee80211_register_hw(rt2x00dev->hw);
  845. if (status) {
  846. rt2x00lib_remove_hw(rt2x00dev);
  847. return status;
  848. }
  849. __set_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags);
  850. return 0;
  851. }
  852. /*
  853. * Initialization/uninitialization handlers.
  854. */
  855. static int rt2x00lib_alloc_entries(struct data_ring *ring,
  856. const u16 max_entries, const u16 data_size,
  857. const u16 desc_size)
  858. {
  859. struct data_entry *entry;
  860. unsigned int i;
  861. ring->stats.limit = max_entries;
  862. ring->data_size = data_size;
  863. ring->desc_size = desc_size;
  864. /*
  865. * Allocate all ring entries.
  866. */
  867. entry = kzalloc(ring->stats.limit * sizeof(*entry), GFP_KERNEL);
  868. if (!entry)
  869. return -ENOMEM;
  870. for (i = 0; i < ring->stats.limit; i++) {
  871. entry[i].flags = 0;
  872. entry[i].ring = ring;
  873. entry[i].skb = NULL;
  874. entry[i].entry_idx = i;
  875. }
  876. ring->entry = entry;
  877. return 0;
  878. }
  879. static int rt2x00lib_alloc_ring_entries(struct rt2x00_dev *rt2x00dev)
  880. {
  881. struct data_ring *ring;
  882. /*
  883. * Allocate the RX ring.
  884. */
  885. if (rt2x00lib_alloc_entries(rt2x00dev->rx, RX_ENTRIES, DATA_FRAME_SIZE,
  886. rt2x00dev->ops->rxd_size))
  887. return -ENOMEM;
  888. /*
  889. * First allocate the TX rings.
  890. */
  891. txring_for_each(rt2x00dev, ring) {
  892. if (rt2x00lib_alloc_entries(ring, TX_ENTRIES, DATA_FRAME_SIZE,
  893. rt2x00dev->ops->txd_size))
  894. return -ENOMEM;
  895. }
  896. if (!test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
  897. return 0;
  898. /*
  899. * Allocate the BEACON ring.
  900. */
  901. if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[0], BEACON_ENTRIES,
  902. MGMT_FRAME_SIZE, rt2x00dev->ops->txd_size))
  903. return -ENOMEM;
  904. /*
  905. * Allocate the Atim ring.
  906. */
  907. if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[1], ATIM_ENTRIES,
  908. DATA_FRAME_SIZE, rt2x00dev->ops->txd_size))
  909. return -ENOMEM;
  910. return 0;
  911. }
  912. static void rt2x00lib_free_ring_entries(struct rt2x00_dev *rt2x00dev)
  913. {
  914. struct data_ring *ring;
  915. ring_for_each(rt2x00dev, ring) {
  916. kfree(ring->entry);
  917. ring->entry = NULL;
  918. }
  919. }
  920. static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
  921. {
  922. if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
  923. return;
  924. /*
  925. * Unregister rfkill.
  926. */
  927. rt2x00rfkill_unregister(rt2x00dev);
  928. /*
  929. * Allow the HW to uninitialize.
  930. */
  931. rt2x00dev->ops->lib->uninitialize(rt2x00dev);
  932. /*
  933. * Free allocated ring entries.
  934. */
  935. rt2x00lib_free_ring_entries(rt2x00dev);
  936. }
  937. static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
  938. {
  939. int status;
  940. if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
  941. return 0;
  942. /*
  943. * Allocate all ring entries.
  944. */
  945. status = rt2x00lib_alloc_ring_entries(rt2x00dev);
  946. if (status) {
  947. ERROR(rt2x00dev, "Ring entries allocation failed.\n");
  948. return status;
  949. }
  950. /*
  951. * Initialize the device.
  952. */
  953. status = rt2x00dev->ops->lib->initialize(rt2x00dev);
  954. if (status)
  955. goto exit;
  956. __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
  957. /*
  958. * Register the rfkill handler.
  959. */
  960. status = rt2x00rfkill_register(rt2x00dev);
  961. if (status)
  962. goto exit_unitialize;
  963. return 0;
  964. exit_unitialize:
  965. rt2x00lib_uninitialize(rt2x00dev);
  966. exit:
  967. rt2x00lib_free_ring_entries(rt2x00dev);
  968. return status;
  969. }
  970. int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
  971. {
  972. int retval;
  973. if (test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  974. return 0;
  975. /*
  976. * If this is the first interface which is added,
  977. * we should load the firmware now.
  978. */
  979. if (test_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags)) {
  980. retval = rt2x00lib_load_firmware(rt2x00dev);
  981. if (retval)
  982. return retval;
  983. }
  984. /*
  985. * Initialize the device.
  986. */
  987. retval = rt2x00lib_initialize(rt2x00dev);
  988. if (retval)
  989. return retval;
  990. /*
  991. * Enable radio.
  992. */
  993. retval = rt2x00lib_enable_radio(rt2x00dev);
  994. if (retval) {
  995. rt2x00lib_uninitialize(rt2x00dev);
  996. return retval;
  997. }
  998. __set_bit(DEVICE_STARTED, &rt2x00dev->flags);
  999. return 0;
  1000. }
  1001. void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
  1002. {
  1003. if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  1004. return;
  1005. /*
  1006. * Perhaps we can add something smarter here,
  1007. * but for now just disabling the radio should do.
  1008. */
  1009. rt2x00lib_disable_radio(rt2x00dev);
  1010. __clear_bit(DEVICE_STARTED, &rt2x00dev->flags);
  1011. }
  1012. /*
  1013. * driver allocation handlers.
  1014. */
  1015. static int rt2x00lib_alloc_rings(struct rt2x00_dev *rt2x00dev)
  1016. {
  1017. struct data_ring *ring;
  1018. unsigned int index;
  1019. /*
  1020. * We need the following rings:
  1021. * RX: 1
  1022. * TX: hw->queues
  1023. * Beacon: 1 (if required)
  1024. * Atim: 1 (if required)
  1025. */
  1026. rt2x00dev->data_rings = 1 + rt2x00dev->hw->queues +
  1027. (2 * test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags));
  1028. ring = kzalloc(rt2x00dev->data_rings * sizeof(*ring), GFP_KERNEL);
  1029. if (!ring) {
  1030. ERROR(rt2x00dev, "Ring allocation failed.\n");
  1031. return -ENOMEM;
  1032. }
  1033. /*
  1034. * Initialize pointers
  1035. */
  1036. rt2x00dev->rx = ring;
  1037. rt2x00dev->tx = &rt2x00dev->rx[1];
  1038. if (test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
  1039. rt2x00dev->bcn = &rt2x00dev->tx[rt2x00dev->hw->queues];
  1040. /*
  1041. * Initialize ring parameters.
  1042. * RX: queue_idx = 0
  1043. * TX: queue_idx = IEEE80211_TX_QUEUE_DATA0 + index
  1044. * TX: cw_min: 2^5 = 32.
  1045. * TX: cw_max: 2^10 = 1024.
  1046. */
  1047. rt2x00dev->rx->rt2x00dev = rt2x00dev;
  1048. rt2x00dev->rx->queue_idx = 0;
  1049. index = IEEE80211_TX_QUEUE_DATA0;
  1050. txring_for_each(rt2x00dev, ring) {
  1051. ring->rt2x00dev = rt2x00dev;
  1052. ring->queue_idx = index++;
  1053. ring->tx_params.aifs = 2;
  1054. ring->tx_params.cw_min = 5;
  1055. ring->tx_params.cw_max = 10;
  1056. }
  1057. return 0;
  1058. }
  1059. static void rt2x00lib_free_rings(struct rt2x00_dev *rt2x00dev)
  1060. {
  1061. kfree(rt2x00dev->rx);
  1062. rt2x00dev->rx = NULL;
  1063. rt2x00dev->tx = NULL;
  1064. rt2x00dev->bcn = NULL;
  1065. }
  1066. int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
  1067. {
  1068. int retval = -ENOMEM;
  1069. /*
  1070. * Let the driver probe the device to detect the capabilities.
  1071. */
  1072. retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
  1073. if (retval) {
  1074. ERROR(rt2x00dev, "Failed to allocate device.\n");
  1075. goto exit;
  1076. }
  1077. /*
  1078. * Initialize configuration work.
  1079. */
  1080. INIT_WORK(&rt2x00dev->beacon_work, rt2x00lib_beacondone_scheduled);
  1081. INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
  1082. INIT_WORK(&rt2x00dev->config_work, rt2x00lib_configuration_scheduled);
  1083. INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
  1084. /*
  1085. * Reset current working type.
  1086. */
  1087. rt2x00dev->interface.type = IEEE80211_IF_TYPE_INVALID;
  1088. /*
  1089. * Allocate ring array.
  1090. */
  1091. retval = rt2x00lib_alloc_rings(rt2x00dev);
  1092. if (retval)
  1093. goto exit;
  1094. /*
  1095. * Initialize ieee80211 structure.
  1096. */
  1097. retval = rt2x00lib_probe_hw(rt2x00dev);
  1098. if (retval) {
  1099. ERROR(rt2x00dev, "Failed to initialize hw.\n");
  1100. goto exit;
  1101. }
  1102. /*
  1103. * Allocatie rfkill.
  1104. */
  1105. retval = rt2x00rfkill_allocate(rt2x00dev);
  1106. if (retval)
  1107. goto exit;
  1108. /*
  1109. * Open the debugfs entry.
  1110. */
  1111. rt2x00debug_register(rt2x00dev);
  1112. __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  1113. return 0;
  1114. exit:
  1115. rt2x00lib_remove_dev(rt2x00dev);
  1116. return retval;
  1117. }
  1118. EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
  1119. void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
  1120. {
  1121. __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  1122. /*
  1123. * Disable radio.
  1124. */
  1125. rt2x00lib_disable_radio(rt2x00dev);
  1126. /*
  1127. * Uninitialize device.
  1128. */
  1129. rt2x00lib_uninitialize(rt2x00dev);
  1130. /*
  1131. * Close debugfs entry.
  1132. */
  1133. rt2x00debug_deregister(rt2x00dev);
  1134. /*
  1135. * Free rfkill
  1136. */
  1137. rt2x00rfkill_free(rt2x00dev);
  1138. /*
  1139. * Free ieee80211_hw memory.
  1140. */
  1141. rt2x00lib_remove_hw(rt2x00dev);
  1142. /*
  1143. * Free firmware image.
  1144. */
  1145. rt2x00lib_free_firmware(rt2x00dev);
  1146. /*
  1147. * Free ring structures.
  1148. */
  1149. rt2x00lib_free_rings(rt2x00dev);
  1150. }
  1151. EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
  1152. /*
  1153. * Device state handlers
  1154. */
  1155. #ifdef CONFIG_PM
  1156. int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
  1157. {
  1158. int retval;
  1159. NOTICE(rt2x00dev, "Going to sleep.\n");
  1160. __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  1161. /*
  1162. * Only continue if mac80211 has open interfaces.
  1163. */
  1164. if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  1165. goto exit;
  1166. __set_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags);
  1167. /*
  1168. * Disable radio and unitialize all items
  1169. * that must be recreated on resume.
  1170. */
  1171. rt2x00lib_stop(rt2x00dev);
  1172. rt2x00lib_uninitialize(rt2x00dev);
  1173. rt2x00debug_deregister(rt2x00dev);
  1174. exit:
  1175. /*
  1176. * Set device mode to sleep for power management.
  1177. */
  1178. retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
  1179. if (retval)
  1180. return retval;
  1181. return 0;
  1182. }
  1183. EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
  1184. int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
  1185. {
  1186. struct interface *intf = &rt2x00dev->interface;
  1187. int retval;
  1188. NOTICE(rt2x00dev, "Waking up.\n");
  1189. /*
  1190. * Open the debugfs entry.
  1191. */
  1192. rt2x00debug_register(rt2x00dev);
  1193. /*
  1194. * Only continue if mac80211 had open interfaces.
  1195. */
  1196. if (!__test_and_clear_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags))
  1197. return 0;
  1198. /*
  1199. * Reinitialize device and all active interfaces.
  1200. */
  1201. retval = rt2x00lib_start(rt2x00dev);
  1202. if (retval)
  1203. goto exit;
  1204. /*
  1205. * Reconfigure device.
  1206. */
  1207. rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1);
  1208. if (!rt2x00dev->hw->conf.radio_enabled)
  1209. rt2x00lib_disable_radio(rt2x00dev);
  1210. rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
  1211. rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
  1212. rt2x00lib_config_type(rt2x00dev, intf->type);
  1213. /*
  1214. * We are ready again to receive requests from mac80211.
  1215. */
  1216. __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  1217. /*
  1218. * It is possible that during that mac80211 has attempted
  1219. * to send frames while we were suspending or resuming.
  1220. * In that case we have disabled the TX queue and should
  1221. * now enable it again
  1222. */
  1223. ieee80211_start_queues(rt2x00dev->hw);
  1224. /*
  1225. * When in Master or Ad-hoc mode,
  1226. * restart Beacon transmitting by faking a beacondone event.
  1227. */
  1228. if (intf->type == IEEE80211_IF_TYPE_AP ||
  1229. intf->type == IEEE80211_IF_TYPE_IBSS)
  1230. rt2x00lib_beacondone(rt2x00dev);
  1231. return 0;
  1232. exit:
  1233. rt2x00lib_disable_radio(rt2x00dev);
  1234. rt2x00lib_uninitialize(rt2x00dev);
  1235. rt2x00debug_deregister(rt2x00dev);
  1236. return retval;
  1237. }
  1238. EXPORT_SYMBOL_GPL(rt2x00lib_resume);
  1239. #endif /* CONFIG_PM */
  1240. /*
  1241. * rt2x00lib module information.
  1242. */
  1243. MODULE_AUTHOR(DRV_PROJECT);
  1244. MODULE_VERSION(DRV_VERSION);
  1245. MODULE_DESCRIPTION("rt2x00 library");
  1246. MODULE_LICENSE("GPL");