rt2x00dev.c 33 KB

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