rt2x00dev.c 30 KB

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