rt2x00dev.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  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 if this is a RTS/CTS frame
  532. */
  533. if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
  534. __set_bit(ENTRY_TXD_BURST, &desc.flags);
  535. if (is_rts_frame(frame_control))
  536. __set_bit(ENTRY_TXD_RTS_FRAME, &desc.flags);
  537. if (control->rts_cts_rate)
  538. tx_rate = control->rts_cts_rate;
  539. }
  540. /*
  541. * Check for OFDM
  542. */
  543. if (DEVICE_GET_RATE_FIELD(tx_rate, RATEMASK) & DEV_OFDM_RATEMASK)
  544. __set_bit(ENTRY_TXD_OFDM_RATE, &desc.flags);
  545. /*
  546. * Check if more fragments are pending
  547. */
  548. if (ieee80211_get_morefrag(ieee80211hdr)) {
  549. __set_bit(ENTRY_TXD_BURST, &desc.flags);
  550. __set_bit(ENTRY_TXD_MORE_FRAG, &desc.flags);
  551. }
  552. /*
  553. * Beacons and probe responses require the tsf timestamp
  554. * to be inserted into the frame.
  555. */
  556. if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
  557. is_probe_resp(frame_control))
  558. __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &desc.flags);
  559. /*
  560. * Determine with what IFS priority this frame should be send.
  561. * Set ifs to IFS_SIFS when the this is not the first fragment,
  562. * or this fragment came after RTS/CTS.
  563. */
  564. if ((seq_ctrl & IEEE80211_SCTL_FRAG) > 0 ||
  565. test_bit(ENTRY_TXD_RTS_FRAME, &desc.flags))
  566. desc.ifs = IFS_SIFS;
  567. else
  568. desc.ifs = IFS_BACKOFF;
  569. /*
  570. * PLCP setup
  571. * Length calculation depends on OFDM/CCK rate.
  572. */
  573. desc.signal = DEVICE_GET_RATE_FIELD(tx_rate, PLCP);
  574. desc.service = 0x04;
  575. if (test_bit(ENTRY_TXD_OFDM_RATE, &desc.flags)) {
  576. desc.length_high = ((length + FCS_LEN) >> 6) & 0x3f;
  577. desc.length_low = ((length + FCS_LEN) & 0x3f);
  578. } else {
  579. bitrate = DEVICE_GET_RATE_FIELD(tx_rate, RATE);
  580. /*
  581. * Convert length to microseconds.
  582. */
  583. residual = get_duration_res(length + FCS_LEN, bitrate);
  584. duration = get_duration(length + FCS_LEN, bitrate);
  585. if (residual != 0) {
  586. duration++;
  587. /*
  588. * Check if we need to set the Length Extension
  589. */
  590. if (bitrate == 110 && residual <= 30)
  591. desc.service |= 0x80;
  592. }
  593. desc.length_high = (duration >> 8) & 0xff;
  594. desc.length_low = duration & 0xff;
  595. /*
  596. * When preamble is enabled we should set the
  597. * preamble bit for the signal.
  598. */
  599. if (DEVICE_GET_RATE_FIELD(tx_rate, PREAMBLE))
  600. desc.signal |= 0x08;
  601. }
  602. rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, txd, &desc,
  603. ieee80211hdr, length, control);
  604. }
  605. EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc);
  606. /*
  607. * Driver initialization handlers.
  608. */
  609. static void rt2x00lib_channel(struct ieee80211_channel *entry,
  610. const int channel, const int tx_power,
  611. const int value)
  612. {
  613. entry->chan = channel;
  614. if (channel <= 14)
  615. entry->freq = 2407 + (5 * channel);
  616. else
  617. entry->freq = 5000 + (5 * channel);
  618. entry->val = value;
  619. entry->flag =
  620. IEEE80211_CHAN_W_IBSS |
  621. IEEE80211_CHAN_W_ACTIVE_SCAN |
  622. IEEE80211_CHAN_W_SCAN;
  623. entry->power_level = tx_power;
  624. entry->antenna_max = 0xff;
  625. }
  626. static void rt2x00lib_rate(struct ieee80211_rate *entry,
  627. const int rate, const int mask,
  628. const int plcp, const int flags)
  629. {
  630. entry->rate = rate;
  631. entry->val =
  632. DEVICE_SET_RATE_FIELD(rate, RATE) |
  633. DEVICE_SET_RATE_FIELD(mask, RATEMASK) |
  634. DEVICE_SET_RATE_FIELD(plcp, PLCP);
  635. entry->flags = flags;
  636. entry->val2 = entry->val;
  637. if (entry->flags & IEEE80211_RATE_PREAMBLE2)
  638. entry->val2 |= DEVICE_SET_RATE_FIELD(1, PREAMBLE);
  639. entry->min_rssi_ack = 0;
  640. entry->min_rssi_ack_delta = 0;
  641. }
  642. static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
  643. struct hw_mode_spec *spec)
  644. {
  645. struct ieee80211_hw *hw = rt2x00dev->hw;
  646. struct ieee80211_hw_mode *hwmodes;
  647. struct ieee80211_channel *channels;
  648. struct ieee80211_rate *rates;
  649. unsigned int i;
  650. unsigned char tx_power;
  651. hwmodes = kzalloc(sizeof(*hwmodes) * spec->num_modes, GFP_KERNEL);
  652. if (!hwmodes)
  653. goto exit;
  654. channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
  655. if (!channels)
  656. goto exit_free_modes;
  657. rates = kzalloc(sizeof(*rates) * spec->num_rates, GFP_KERNEL);
  658. if (!rates)
  659. goto exit_free_channels;
  660. /*
  661. * Initialize Rate list.
  662. */
  663. rt2x00lib_rate(&rates[0], 10, DEV_RATEMASK_1MB,
  664. 0x00, IEEE80211_RATE_CCK);
  665. rt2x00lib_rate(&rates[1], 20, DEV_RATEMASK_2MB,
  666. 0x01, IEEE80211_RATE_CCK_2);
  667. rt2x00lib_rate(&rates[2], 55, DEV_RATEMASK_5_5MB,
  668. 0x02, IEEE80211_RATE_CCK_2);
  669. rt2x00lib_rate(&rates[3], 110, DEV_RATEMASK_11MB,
  670. 0x03, IEEE80211_RATE_CCK_2);
  671. if (spec->num_rates > 4) {
  672. rt2x00lib_rate(&rates[4], 60, DEV_RATEMASK_6MB,
  673. 0x0b, IEEE80211_RATE_OFDM);
  674. rt2x00lib_rate(&rates[5], 90, DEV_RATEMASK_9MB,
  675. 0x0f, IEEE80211_RATE_OFDM);
  676. rt2x00lib_rate(&rates[6], 120, DEV_RATEMASK_12MB,
  677. 0x0a, IEEE80211_RATE_OFDM);
  678. rt2x00lib_rate(&rates[7], 180, DEV_RATEMASK_18MB,
  679. 0x0e, IEEE80211_RATE_OFDM);
  680. rt2x00lib_rate(&rates[8], 240, DEV_RATEMASK_24MB,
  681. 0x09, IEEE80211_RATE_OFDM);
  682. rt2x00lib_rate(&rates[9], 360, DEV_RATEMASK_36MB,
  683. 0x0d, IEEE80211_RATE_OFDM);
  684. rt2x00lib_rate(&rates[10], 480, DEV_RATEMASK_48MB,
  685. 0x08, IEEE80211_RATE_OFDM);
  686. rt2x00lib_rate(&rates[11], 540, DEV_RATEMASK_54MB,
  687. 0x0c, IEEE80211_RATE_OFDM);
  688. }
  689. /*
  690. * Initialize Channel list.
  691. */
  692. for (i = 0; i < spec->num_channels; i++) {
  693. if (spec->channels[i].channel <= 14)
  694. tx_power = spec->tx_power_bg[i];
  695. else if (spec->tx_power_a)
  696. tx_power = spec->tx_power_a[i];
  697. else
  698. tx_power = spec->tx_power_default;
  699. rt2x00lib_channel(&channels[i],
  700. spec->channels[i].channel, tx_power, i);
  701. }
  702. /*
  703. * Intitialize 802.11b
  704. * Rates: CCK.
  705. * Channels: OFDM.
  706. */
  707. if (spec->num_modes > HWMODE_B) {
  708. hwmodes[HWMODE_B].mode = MODE_IEEE80211B;
  709. hwmodes[HWMODE_B].num_channels = 14;
  710. hwmodes[HWMODE_B].num_rates = 4;
  711. hwmodes[HWMODE_B].channels = channels;
  712. hwmodes[HWMODE_B].rates = rates;
  713. }
  714. /*
  715. * Intitialize 802.11g
  716. * Rates: CCK, OFDM.
  717. * Channels: OFDM.
  718. */
  719. if (spec->num_modes > HWMODE_G) {
  720. hwmodes[HWMODE_G].mode = MODE_IEEE80211G;
  721. hwmodes[HWMODE_G].num_channels = 14;
  722. hwmodes[HWMODE_G].num_rates = spec->num_rates;
  723. hwmodes[HWMODE_G].channels = channels;
  724. hwmodes[HWMODE_G].rates = rates;
  725. }
  726. /*
  727. * Intitialize 802.11a
  728. * Rates: OFDM.
  729. * Channels: OFDM, UNII, HiperLAN2.
  730. */
  731. if (spec->num_modes > HWMODE_A) {
  732. hwmodes[HWMODE_A].mode = MODE_IEEE80211A;
  733. hwmodes[HWMODE_A].num_channels = spec->num_channels - 14;
  734. hwmodes[HWMODE_A].num_rates = spec->num_rates - 4;
  735. hwmodes[HWMODE_A].channels = &channels[14];
  736. hwmodes[HWMODE_A].rates = &rates[4];
  737. }
  738. if (spec->num_modes > HWMODE_G &&
  739. ieee80211_register_hwmode(hw, &hwmodes[HWMODE_G]))
  740. goto exit_free_rates;
  741. if (spec->num_modes > HWMODE_B &&
  742. ieee80211_register_hwmode(hw, &hwmodes[HWMODE_B]))
  743. goto exit_free_rates;
  744. if (spec->num_modes > HWMODE_A &&
  745. ieee80211_register_hwmode(hw, &hwmodes[HWMODE_A]))
  746. goto exit_free_rates;
  747. rt2x00dev->hwmodes = hwmodes;
  748. return 0;
  749. exit_free_rates:
  750. kfree(rates);
  751. exit_free_channels:
  752. kfree(channels);
  753. exit_free_modes:
  754. kfree(hwmodes);
  755. exit:
  756. ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
  757. return -ENOMEM;
  758. }
  759. static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
  760. {
  761. if (test_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags))
  762. ieee80211_unregister_hw(rt2x00dev->hw);
  763. if (likely(rt2x00dev->hwmodes)) {
  764. kfree(rt2x00dev->hwmodes->channels);
  765. kfree(rt2x00dev->hwmodes->rates);
  766. kfree(rt2x00dev->hwmodes);
  767. rt2x00dev->hwmodes = NULL;
  768. }
  769. }
  770. static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
  771. {
  772. struct hw_mode_spec *spec = &rt2x00dev->spec;
  773. int status;
  774. /*
  775. * Initialize HW modes.
  776. */
  777. status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
  778. if (status)
  779. return status;
  780. /*
  781. * Register HW.
  782. */
  783. status = ieee80211_register_hw(rt2x00dev->hw);
  784. if (status) {
  785. rt2x00lib_remove_hw(rt2x00dev);
  786. return status;
  787. }
  788. __set_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags);
  789. return 0;
  790. }
  791. /*
  792. * Initialization/uninitialization handlers.
  793. */
  794. static int rt2x00lib_alloc_entries(struct data_ring *ring,
  795. const u16 max_entries, const u16 data_size,
  796. const u16 desc_size)
  797. {
  798. struct data_entry *entry;
  799. unsigned int i;
  800. ring->stats.limit = max_entries;
  801. ring->data_size = data_size;
  802. ring->desc_size = desc_size;
  803. /*
  804. * Allocate all ring entries.
  805. */
  806. entry = kzalloc(ring->stats.limit * sizeof(*entry), GFP_KERNEL);
  807. if (!entry)
  808. return -ENOMEM;
  809. for (i = 0; i < ring->stats.limit; i++) {
  810. entry[i].flags = 0;
  811. entry[i].ring = ring;
  812. entry[i].skb = NULL;
  813. }
  814. ring->entry = entry;
  815. return 0;
  816. }
  817. static int rt2x00lib_alloc_ring_entries(struct rt2x00_dev *rt2x00dev)
  818. {
  819. struct data_ring *ring;
  820. /*
  821. * Allocate the RX ring.
  822. */
  823. if (rt2x00lib_alloc_entries(rt2x00dev->rx, RX_ENTRIES, DATA_FRAME_SIZE,
  824. rt2x00dev->ops->rxd_size))
  825. return -ENOMEM;
  826. /*
  827. * First allocate the TX rings.
  828. */
  829. txring_for_each(rt2x00dev, ring) {
  830. if (rt2x00lib_alloc_entries(ring, TX_ENTRIES, DATA_FRAME_SIZE,
  831. rt2x00dev->ops->txd_size))
  832. return -ENOMEM;
  833. }
  834. if (!test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
  835. return 0;
  836. /*
  837. * Allocate the BEACON ring.
  838. */
  839. if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[0], BEACON_ENTRIES,
  840. MGMT_FRAME_SIZE, rt2x00dev->ops->txd_size))
  841. return -ENOMEM;
  842. /*
  843. * Allocate the Atim ring.
  844. */
  845. if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[1], ATIM_ENTRIES,
  846. DATA_FRAME_SIZE, rt2x00dev->ops->txd_size))
  847. return -ENOMEM;
  848. return 0;
  849. }
  850. static void rt2x00lib_free_ring_entries(struct rt2x00_dev *rt2x00dev)
  851. {
  852. struct data_ring *ring;
  853. ring_for_each(rt2x00dev, ring) {
  854. kfree(ring->entry);
  855. ring->entry = NULL;
  856. }
  857. }
  858. void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
  859. {
  860. if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
  861. return;
  862. /*
  863. * Unregister rfkill.
  864. */
  865. rt2x00rfkill_unregister(rt2x00dev);
  866. /*
  867. * Allow the HW to uninitialize.
  868. */
  869. rt2x00dev->ops->lib->uninitialize(rt2x00dev);
  870. /*
  871. * Free allocated ring entries.
  872. */
  873. rt2x00lib_free_ring_entries(rt2x00dev);
  874. }
  875. int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
  876. {
  877. int status;
  878. if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
  879. return 0;
  880. /*
  881. * Allocate all ring entries.
  882. */
  883. status = rt2x00lib_alloc_ring_entries(rt2x00dev);
  884. if (status) {
  885. ERROR(rt2x00dev, "Ring entries allocation failed.\n");
  886. return status;
  887. }
  888. /*
  889. * Initialize the device.
  890. */
  891. status = rt2x00dev->ops->lib->initialize(rt2x00dev);
  892. if (status)
  893. goto exit;
  894. __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
  895. /*
  896. * Register the rfkill handler.
  897. */
  898. status = rt2x00rfkill_register(rt2x00dev);
  899. if (status)
  900. goto exit_unitialize;
  901. return 0;
  902. exit_unitialize:
  903. rt2x00lib_uninitialize(rt2x00dev);
  904. exit:
  905. rt2x00lib_free_ring_entries(rt2x00dev);
  906. return status;
  907. }
  908. /*
  909. * driver allocation handlers.
  910. */
  911. static int rt2x00lib_alloc_rings(struct rt2x00_dev *rt2x00dev)
  912. {
  913. struct data_ring *ring;
  914. /*
  915. * We need the following rings:
  916. * RX: 1
  917. * TX: hw->queues
  918. * Beacon: 1 (if required)
  919. * Atim: 1 (if required)
  920. */
  921. rt2x00dev->data_rings = 1 + rt2x00dev->hw->queues +
  922. (2 * test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags));
  923. ring = kzalloc(rt2x00dev->data_rings * sizeof(*ring), GFP_KERNEL);
  924. if (!ring) {
  925. ERROR(rt2x00dev, "Ring allocation failed.\n");
  926. return -ENOMEM;
  927. }
  928. /*
  929. * Initialize pointers
  930. */
  931. rt2x00dev->rx = ring;
  932. rt2x00dev->tx = &rt2x00dev->rx[1];
  933. if (test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
  934. rt2x00dev->bcn = &rt2x00dev->tx[rt2x00dev->hw->queues];
  935. /*
  936. * Initialize ring parameters.
  937. * cw_min: 2^5 = 32.
  938. * cw_max: 2^10 = 1024.
  939. */
  940. ring_for_each(rt2x00dev, ring) {
  941. ring->rt2x00dev = rt2x00dev;
  942. ring->tx_params.aifs = 2;
  943. ring->tx_params.cw_min = 5;
  944. ring->tx_params.cw_max = 10;
  945. }
  946. return 0;
  947. }
  948. static void rt2x00lib_free_rings(struct rt2x00_dev *rt2x00dev)
  949. {
  950. kfree(rt2x00dev->rx);
  951. rt2x00dev->rx = NULL;
  952. rt2x00dev->tx = NULL;
  953. rt2x00dev->bcn = NULL;
  954. }
  955. int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
  956. {
  957. int retval = -ENOMEM;
  958. /*
  959. * Let the driver probe the device to detect the capabilities.
  960. */
  961. retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
  962. if (retval) {
  963. ERROR(rt2x00dev, "Failed to allocate device.\n");
  964. goto exit;
  965. }
  966. /*
  967. * Initialize configuration work.
  968. */
  969. INIT_WORK(&rt2x00dev->beacon_work, rt2x00lib_beacondone_scheduled);
  970. INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
  971. INIT_WORK(&rt2x00dev->config_work, rt2x00lib_configuration_scheduled);
  972. INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
  973. /*
  974. * Reset current working type.
  975. */
  976. rt2x00dev->interface.type = INVALID_INTERFACE;
  977. /*
  978. * Allocate ring array.
  979. */
  980. retval = rt2x00lib_alloc_rings(rt2x00dev);
  981. if (retval)
  982. goto exit;
  983. /*
  984. * Initialize ieee80211 structure.
  985. */
  986. retval = rt2x00lib_probe_hw(rt2x00dev);
  987. if (retval) {
  988. ERROR(rt2x00dev, "Failed to initialize hw.\n");
  989. goto exit;
  990. }
  991. /*
  992. * Allocatie rfkill.
  993. */
  994. retval = rt2x00rfkill_allocate(rt2x00dev);
  995. if (retval)
  996. goto exit;
  997. /*
  998. * Open the debugfs entry.
  999. */
  1000. rt2x00debug_register(rt2x00dev);
  1001. __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  1002. return 0;
  1003. exit:
  1004. rt2x00lib_remove_dev(rt2x00dev);
  1005. return retval;
  1006. }
  1007. EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
  1008. void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
  1009. {
  1010. __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  1011. /*
  1012. * Disable radio.
  1013. */
  1014. rt2x00lib_disable_radio(rt2x00dev);
  1015. /*
  1016. * Uninitialize device.
  1017. */
  1018. rt2x00lib_uninitialize(rt2x00dev);
  1019. /*
  1020. * Close debugfs entry.
  1021. */
  1022. rt2x00debug_deregister(rt2x00dev);
  1023. /*
  1024. * Free rfkill
  1025. */
  1026. rt2x00rfkill_free(rt2x00dev);
  1027. /*
  1028. * Free ieee80211_hw memory.
  1029. */
  1030. rt2x00lib_remove_hw(rt2x00dev);
  1031. /*
  1032. * Free firmware image.
  1033. */
  1034. rt2x00lib_free_firmware(rt2x00dev);
  1035. /*
  1036. * Free ring structures.
  1037. */
  1038. rt2x00lib_free_rings(rt2x00dev);
  1039. }
  1040. EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
  1041. /*
  1042. * Device state handlers
  1043. */
  1044. #ifdef CONFIG_PM
  1045. int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
  1046. {
  1047. int retval;
  1048. NOTICE(rt2x00dev, "Going to sleep.\n");
  1049. __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  1050. /*
  1051. * Only continue if mac80211 has open interfaces.
  1052. */
  1053. if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  1054. goto exit;
  1055. __set_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags);
  1056. /*
  1057. * Disable radio and unitialize all items
  1058. * that must be recreated on resume.
  1059. */
  1060. rt2x00mac_stop(rt2x00dev->hw);
  1061. rt2x00lib_uninitialize(rt2x00dev);
  1062. rt2x00debug_deregister(rt2x00dev);
  1063. exit:
  1064. /*
  1065. * Set device mode to sleep for power management.
  1066. */
  1067. retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
  1068. if (retval)
  1069. return retval;
  1070. return 0;
  1071. }
  1072. EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
  1073. int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
  1074. {
  1075. struct interface *intf = &rt2x00dev->interface;
  1076. int retval;
  1077. NOTICE(rt2x00dev, "Waking up.\n");
  1078. __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
  1079. /*
  1080. * Open the debugfs entry.
  1081. */
  1082. rt2x00debug_register(rt2x00dev);
  1083. /*
  1084. * Only continue if mac80211 had open interfaces.
  1085. */
  1086. if (!__test_and_clear_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags))
  1087. return 0;
  1088. /*
  1089. * Reinitialize device and all active interfaces.
  1090. */
  1091. retval = rt2x00mac_start(rt2x00dev->hw);
  1092. if (retval)
  1093. goto exit;
  1094. /*
  1095. * Reconfigure device.
  1096. */
  1097. rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1);
  1098. if (!rt2x00dev->hw->conf.radio_enabled)
  1099. rt2x00lib_disable_radio(rt2x00dev);
  1100. rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
  1101. rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
  1102. rt2x00lib_config_type(rt2x00dev, intf->type);
  1103. /*
  1104. * It is possible that during that mac80211 has attempted
  1105. * to send frames while we were suspending or resuming.
  1106. * In that case we have disabled the TX queue and should
  1107. * now enable it again
  1108. */
  1109. ieee80211_start_queues(rt2x00dev->hw);
  1110. /*
  1111. * When in Master or Ad-hoc mode,
  1112. * restart Beacon transmitting by faking a beacondone event.
  1113. */
  1114. if (intf->type == IEEE80211_IF_TYPE_AP ||
  1115. intf->type == IEEE80211_IF_TYPE_IBSS)
  1116. rt2x00lib_beacondone(rt2x00dev);
  1117. return 0;
  1118. exit:
  1119. rt2x00lib_disable_radio(rt2x00dev);
  1120. rt2x00lib_uninitialize(rt2x00dev);
  1121. rt2x00debug_deregister(rt2x00dev);
  1122. return retval;
  1123. }
  1124. EXPORT_SYMBOL_GPL(rt2x00lib_resume);
  1125. #endif /* CONFIG_PM */
  1126. /*
  1127. * rt2x00lib module information.
  1128. */
  1129. MODULE_AUTHOR(DRV_PROJECT);
  1130. MODULE_VERSION(DRV_VERSION);
  1131. MODULE_DESCRIPTION("rt2x00 library");
  1132. MODULE_LICENSE("GPL");