rt2x00dev.c 33 KB

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