rt2x00dev.c 34 KB

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