rt2x00dev.c 33 KB

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