rt2x00dev.c 33 KB

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