rt2x00dev.c 33 KB

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