rt2x00dev.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. /*
  2. Copyright (C) 2010 Willow Garage <http://www.willowgarage.com>
  3. Copyright (C) 2004 - 2010 Ivo van Doorn <IvDoorn@gmail.com>
  4. <http://rt2x00.serialmonkey.com>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the
  15. Free Software Foundation, Inc.,
  16. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. /*
  19. Module: rt2x00lib
  20. Abstract: rt2x00 generic device routines.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include "rt2x00.h"
  26. #include "rt2x00lib.h"
  27. /*
  28. * Radio control handlers.
  29. */
  30. int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
  31. {
  32. int status;
  33. /*
  34. * Don't enable the radio twice.
  35. * And check if the hardware button has been disabled.
  36. */
  37. if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  38. return 0;
  39. /*
  40. * Initialize all data queues.
  41. */
  42. rt2x00queue_init_queues(rt2x00dev);
  43. /*
  44. * Enable radio.
  45. */
  46. status =
  47. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_ON);
  48. if (status)
  49. return status;
  50. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_ON);
  51. rt2x00leds_led_radio(rt2x00dev, true);
  52. rt2x00led_led_activity(rt2x00dev, true);
  53. set_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags);
  54. /*
  55. * Enable queues.
  56. */
  57. rt2x00queue_start_queues(rt2x00dev);
  58. rt2x00link_start_tuner(rt2x00dev);
  59. /*
  60. * Start watchdog monitoring.
  61. */
  62. rt2x00link_start_watchdog(rt2x00dev);
  63. return 0;
  64. }
  65. void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
  66. {
  67. if (!test_and_clear_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  68. return;
  69. /*
  70. * Stop watchdog monitoring.
  71. */
  72. rt2x00link_stop_watchdog(rt2x00dev);
  73. /*
  74. * Stop all queues
  75. */
  76. rt2x00link_stop_tuner(rt2x00dev);
  77. rt2x00queue_stop_queues(rt2x00dev);
  78. rt2x00queue_flush_queues(rt2x00dev, true);
  79. /*
  80. * Disable radio.
  81. */
  82. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
  83. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_OFF);
  84. rt2x00led_led_activity(rt2x00dev, false);
  85. rt2x00leds_led_radio(rt2x00dev, false);
  86. }
  87. static void rt2x00lib_intf_scheduled_iter(void *data, u8 *mac,
  88. struct ieee80211_vif *vif)
  89. {
  90. struct rt2x00_dev *rt2x00dev = data;
  91. struct rt2x00_intf *intf = vif_to_intf(vif);
  92. /*
  93. * It is possible the radio was disabled while the work had been
  94. * scheduled. If that happens we should return here immediately,
  95. * note that in the spinlock protected area above the delayed_flags
  96. * have been cleared correctly.
  97. */
  98. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  99. return;
  100. if (test_and_clear_bit(DELAYED_UPDATE_BEACON, &intf->delayed_flags))
  101. rt2x00queue_update_beacon(rt2x00dev, vif);
  102. }
  103. static void rt2x00lib_intf_scheduled(struct work_struct *work)
  104. {
  105. struct rt2x00_dev *rt2x00dev =
  106. container_of(work, struct rt2x00_dev, intf_work);
  107. /*
  108. * Iterate over each interface and perform the
  109. * requested configurations.
  110. */
  111. ieee80211_iterate_active_interfaces(rt2x00dev->hw,
  112. rt2x00lib_intf_scheduled_iter,
  113. rt2x00dev);
  114. }
  115. /*
  116. * Interrupt context handlers.
  117. */
  118. static void rt2x00lib_bc_buffer_iter(void *data, u8 *mac,
  119. struct ieee80211_vif *vif)
  120. {
  121. struct rt2x00_dev *rt2x00dev = data;
  122. struct sk_buff *skb;
  123. /*
  124. * Only AP mode interfaces do broad- and multicast buffering
  125. */
  126. if (vif->type != NL80211_IFTYPE_AP)
  127. return;
  128. /*
  129. * Send out buffered broad- and multicast frames
  130. */
  131. skb = ieee80211_get_buffered_bc(rt2x00dev->hw, vif);
  132. while (skb) {
  133. rt2x00mac_tx(rt2x00dev->hw, skb);
  134. skb = ieee80211_get_buffered_bc(rt2x00dev->hw, vif);
  135. }
  136. }
  137. static void rt2x00lib_beaconupdate_iter(void *data, u8 *mac,
  138. struct ieee80211_vif *vif)
  139. {
  140. struct rt2x00_dev *rt2x00dev = data;
  141. if (vif->type != NL80211_IFTYPE_AP &&
  142. vif->type != NL80211_IFTYPE_ADHOC &&
  143. vif->type != NL80211_IFTYPE_MESH_POINT &&
  144. vif->type != NL80211_IFTYPE_WDS)
  145. return;
  146. /*
  147. * Update the beacon without locking. This is safe on PCI devices
  148. * as they only update the beacon periodically here. This should
  149. * never be called for USB devices.
  150. */
  151. WARN_ON(rt2x00_is_usb(rt2x00dev));
  152. rt2x00queue_update_beacon_locked(rt2x00dev, vif);
  153. }
  154. void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
  155. {
  156. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  157. return;
  158. /* send buffered bc/mc frames out for every bssid */
  159. ieee80211_iterate_active_interfaces_atomic(rt2x00dev->hw,
  160. rt2x00lib_bc_buffer_iter,
  161. rt2x00dev);
  162. /*
  163. * Devices with pre tbtt interrupt don't need to update the beacon
  164. * here as they will fetch the next beacon directly prior to
  165. * transmission.
  166. */
  167. if (test_bit(DRIVER_SUPPORT_PRE_TBTT_INTERRUPT, &rt2x00dev->flags))
  168. return;
  169. /* fetch next beacon */
  170. ieee80211_iterate_active_interfaces_atomic(rt2x00dev->hw,
  171. rt2x00lib_beaconupdate_iter,
  172. rt2x00dev);
  173. }
  174. EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
  175. void rt2x00lib_pretbtt(struct rt2x00_dev *rt2x00dev)
  176. {
  177. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  178. return;
  179. /* fetch next beacon */
  180. ieee80211_iterate_active_interfaces_atomic(rt2x00dev->hw,
  181. rt2x00lib_beaconupdate_iter,
  182. rt2x00dev);
  183. }
  184. EXPORT_SYMBOL_GPL(rt2x00lib_pretbtt);
  185. void rt2x00lib_dmastart(struct queue_entry *entry)
  186. {
  187. set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  188. rt2x00queue_index_inc(entry->queue, Q_INDEX);
  189. }
  190. EXPORT_SYMBOL_GPL(rt2x00lib_dmastart);
  191. void rt2x00lib_dmadone(struct queue_entry *entry)
  192. {
  193. set_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags);
  194. clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  195. rt2x00queue_index_inc(entry->queue, Q_INDEX_DMA_DONE);
  196. }
  197. EXPORT_SYMBOL_GPL(rt2x00lib_dmadone);
  198. void rt2x00lib_txdone(struct queue_entry *entry,
  199. struct txdone_entry_desc *txdesc)
  200. {
  201. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  202. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
  203. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  204. unsigned int header_length, i;
  205. u8 rate_idx, rate_flags, retry_rates;
  206. u8 skbdesc_flags = skbdesc->flags;
  207. bool success;
  208. /*
  209. * Unmap the skb.
  210. */
  211. rt2x00queue_unmap_skb(entry);
  212. /*
  213. * Remove the extra tx headroom from the skb.
  214. */
  215. skb_pull(entry->skb, rt2x00dev->ops->extra_tx_headroom);
  216. /*
  217. * Signal that the TX descriptor is no longer in the skb.
  218. */
  219. skbdesc->flags &= ~SKBDESC_DESC_IN_SKB;
  220. /*
  221. * Determine the length of 802.11 header.
  222. */
  223. header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
  224. /*
  225. * Remove L2 padding which was added during
  226. */
  227. if (test_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags))
  228. rt2x00queue_remove_l2pad(entry->skb, header_length);
  229. /*
  230. * If the IV/EIV data was stripped from the frame before it was
  231. * passed to the hardware, we should now reinsert it again because
  232. * mac80211 will expect the same data to be present it the
  233. * frame as it was passed to us.
  234. */
  235. if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags))
  236. rt2x00crypto_tx_insert_iv(entry->skb, header_length);
  237. /*
  238. * Send frame to debugfs immediately, after this call is completed
  239. * we are going to overwrite the skb->cb array.
  240. */
  241. rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TXDONE, entry->skb);
  242. /*
  243. * Determine if the frame has been successfully transmitted.
  244. */
  245. success =
  246. test_bit(TXDONE_SUCCESS, &txdesc->flags) ||
  247. test_bit(TXDONE_UNKNOWN, &txdesc->flags);
  248. /*
  249. * Update TX statistics.
  250. */
  251. rt2x00dev->link.qual.tx_success += success;
  252. rt2x00dev->link.qual.tx_failed += !success;
  253. rate_idx = skbdesc->tx_rate_idx;
  254. rate_flags = skbdesc->tx_rate_flags;
  255. retry_rates = test_bit(TXDONE_FALLBACK, &txdesc->flags) ?
  256. (txdesc->retry + 1) : 1;
  257. /*
  258. * Initialize TX status
  259. */
  260. memset(&tx_info->status, 0, sizeof(tx_info->status));
  261. tx_info->status.ack_signal = 0;
  262. /*
  263. * Frame was send with retries, hardware tried
  264. * different rates to send out the frame, at each
  265. * retry it lowered the rate 1 step except when the
  266. * lowest rate was used.
  267. */
  268. for (i = 0; i < retry_rates && i < IEEE80211_TX_MAX_RATES; i++) {
  269. tx_info->status.rates[i].idx = rate_idx - i;
  270. tx_info->status.rates[i].flags = rate_flags;
  271. if (rate_idx - i == 0) {
  272. /*
  273. * The lowest rate (index 0) was used until the
  274. * number of max retries was reached.
  275. */
  276. tx_info->status.rates[i].count = retry_rates - i;
  277. i++;
  278. break;
  279. }
  280. tx_info->status.rates[i].count = 1;
  281. }
  282. if (i < (IEEE80211_TX_MAX_RATES - 1))
  283. tx_info->status.rates[i].idx = -1; /* terminate */
  284. if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  285. if (success)
  286. tx_info->flags |= IEEE80211_TX_STAT_ACK;
  287. else
  288. rt2x00dev->low_level_stats.dot11ACKFailureCount++;
  289. }
  290. /*
  291. * Every single frame has it's own tx status, hence report
  292. * every frame as ampdu of size 1.
  293. *
  294. * TODO: if we can find out how many frames were aggregated
  295. * by the hw we could provide the real ampdu_len to mac80211
  296. * which would allow the rc algorithm to better decide on
  297. * which rates are suitable.
  298. */
  299. if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
  300. tx_info->flags |= IEEE80211_TX_STAT_AMPDU;
  301. tx_info->status.ampdu_len = 1;
  302. tx_info->status.ampdu_ack_len = success ? 1 : 0;
  303. }
  304. if (rate_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  305. if (success)
  306. rt2x00dev->low_level_stats.dot11RTSSuccessCount++;
  307. else
  308. rt2x00dev->low_level_stats.dot11RTSFailureCount++;
  309. }
  310. /*
  311. * Only send the status report to mac80211 when it's a frame
  312. * that originated in mac80211. If this was a extra frame coming
  313. * through a mac80211 library call (RTS/CTS) then we should not
  314. * send the status report back.
  315. */
  316. if (!(skbdesc_flags & SKBDESC_NOT_MAC80211)) {
  317. if (test_bit(DRIVER_REQUIRE_TASKLET_CONTEXT, &rt2x00dev->flags))
  318. ieee80211_tx_status(rt2x00dev->hw, entry->skb);
  319. else
  320. ieee80211_tx_status_ni(rt2x00dev->hw, entry->skb);
  321. } else
  322. dev_kfree_skb_any(entry->skb);
  323. /*
  324. * Make this entry available for reuse.
  325. */
  326. entry->skb = NULL;
  327. entry->flags = 0;
  328. rt2x00dev->ops->lib->clear_entry(entry);
  329. rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
  330. /*
  331. * If the data queue was below the threshold before the txdone
  332. * handler we must make sure the packet queue in the mac80211 stack
  333. * is reenabled when the txdone handler has finished.
  334. */
  335. if (!rt2x00queue_threshold(entry->queue))
  336. rt2x00queue_unpause_queue(entry->queue);
  337. }
  338. EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
  339. void rt2x00lib_txdone_noinfo(struct queue_entry *entry, u32 status)
  340. {
  341. struct txdone_entry_desc txdesc;
  342. txdesc.flags = 0;
  343. __set_bit(status, &txdesc.flags);
  344. txdesc.retry = 0;
  345. rt2x00lib_txdone(entry, &txdesc);
  346. }
  347. EXPORT_SYMBOL_GPL(rt2x00lib_txdone_noinfo);
  348. static int rt2x00lib_rxdone_read_signal(struct rt2x00_dev *rt2x00dev,
  349. struct rxdone_entry_desc *rxdesc)
  350. {
  351. struct ieee80211_supported_band *sband;
  352. const struct rt2x00_rate *rate;
  353. unsigned int i;
  354. int signal = rxdesc->signal;
  355. int type = (rxdesc->dev_flags & RXDONE_SIGNAL_MASK);
  356. switch (rxdesc->rate_mode) {
  357. case RATE_MODE_CCK:
  358. case RATE_MODE_OFDM:
  359. /*
  360. * For non-HT rates the MCS value needs to contain the
  361. * actually used rate modulation (CCK or OFDM).
  362. */
  363. if (rxdesc->dev_flags & RXDONE_SIGNAL_MCS)
  364. signal = RATE_MCS(rxdesc->rate_mode, signal);
  365. sband = &rt2x00dev->bands[rt2x00dev->curr_band];
  366. for (i = 0; i < sband->n_bitrates; i++) {
  367. rate = rt2x00_get_rate(sband->bitrates[i].hw_value);
  368. if (((type == RXDONE_SIGNAL_PLCP) &&
  369. (rate->plcp == signal)) ||
  370. ((type == RXDONE_SIGNAL_BITRATE) &&
  371. (rate->bitrate == signal)) ||
  372. ((type == RXDONE_SIGNAL_MCS) &&
  373. (rate->mcs == signal))) {
  374. return i;
  375. }
  376. }
  377. break;
  378. case RATE_MODE_HT_MIX:
  379. case RATE_MODE_HT_GREENFIELD:
  380. if (signal >= 0 && signal <= 76)
  381. return signal;
  382. break;
  383. default:
  384. break;
  385. }
  386. WARNING(rt2x00dev, "Frame received with unrecognized signal, "
  387. "mode=0x%.4x, signal=0x%.4x, type=%d.\n",
  388. rxdesc->rate_mode, signal, type);
  389. return 0;
  390. }
  391. void rt2x00lib_rxdone(struct queue_entry *entry)
  392. {
  393. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  394. struct rxdone_entry_desc rxdesc;
  395. struct sk_buff *skb;
  396. struct ieee80211_rx_status *rx_status;
  397. unsigned int header_length;
  398. int rate_idx;
  399. if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) ||
  400. !test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  401. goto submit_entry;
  402. if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags))
  403. goto submit_entry;
  404. /*
  405. * Allocate a new sk_buffer. If no new buffer available, drop the
  406. * received frame and reuse the existing buffer.
  407. */
  408. skb = rt2x00queue_alloc_rxskb(entry);
  409. if (!skb)
  410. goto submit_entry;
  411. /*
  412. * Unmap the skb.
  413. */
  414. rt2x00queue_unmap_skb(entry);
  415. /*
  416. * Extract the RXD details.
  417. */
  418. memset(&rxdesc, 0, sizeof(rxdesc));
  419. rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
  420. /*
  421. * The data behind the ieee80211 header must be
  422. * aligned on a 4 byte boundary.
  423. */
  424. header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
  425. /*
  426. * Hardware might have stripped the IV/EIV/ICV data,
  427. * in that case it is possible that the data was
  428. * provided separately (through hardware descriptor)
  429. * in which case we should reinsert the data into the frame.
  430. */
  431. if ((rxdesc.dev_flags & RXDONE_CRYPTO_IV) &&
  432. (rxdesc.flags & RX_FLAG_IV_STRIPPED))
  433. rt2x00crypto_rx_insert_iv(entry->skb, header_length,
  434. &rxdesc);
  435. else if (header_length &&
  436. (rxdesc.size > header_length) &&
  437. (rxdesc.dev_flags & RXDONE_L2PAD))
  438. rt2x00queue_remove_l2pad(entry->skb, header_length);
  439. else
  440. rt2x00queue_align_payload(entry->skb, header_length);
  441. /* Trim buffer to correct size */
  442. skb_trim(entry->skb, rxdesc.size);
  443. /*
  444. * Translate the signal to the correct bitrate index.
  445. */
  446. rate_idx = rt2x00lib_rxdone_read_signal(rt2x00dev, &rxdesc);
  447. if (rxdesc.rate_mode == RATE_MODE_HT_MIX ||
  448. rxdesc.rate_mode == RATE_MODE_HT_GREENFIELD)
  449. rxdesc.flags |= RX_FLAG_HT;
  450. /*
  451. * Update extra components
  452. */
  453. rt2x00link_update_stats(rt2x00dev, entry->skb, &rxdesc);
  454. rt2x00debug_update_crypto(rt2x00dev, &rxdesc);
  455. rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_RXDONE, entry->skb);
  456. /*
  457. * Initialize RX status information, and send frame
  458. * to mac80211.
  459. */
  460. rx_status = IEEE80211_SKB_RXCB(entry->skb);
  461. rx_status->mactime = rxdesc.timestamp;
  462. rx_status->band = rt2x00dev->curr_band;
  463. rx_status->freq = rt2x00dev->curr_freq;
  464. rx_status->rate_idx = rate_idx;
  465. rx_status->signal = rxdesc.rssi;
  466. rx_status->flag = rxdesc.flags;
  467. rx_status->antenna = rt2x00dev->link.ant.active.rx;
  468. ieee80211_rx_ni(rt2x00dev->hw, entry->skb);
  469. /*
  470. * Replace the skb with the freshly allocated one.
  471. */
  472. entry->skb = skb;
  473. submit_entry:
  474. entry->flags = 0;
  475. rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
  476. if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) &&
  477. test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  478. rt2x00dev->ops->lib->clear_entry(entry);
  479. }
  480. EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
  481. /*
  482. * Driver initialization handlers.
  483. */
  484. const struct rt2x00_rate rt2x00_supported_rates[12] = {
  485. {
  486. .flags = DEV_RATE_CCK,
  487. .bitrate = 10,
  488. .ratemask = BIT(0),
  489. .plcp = 0x00,
  490. .mcs = RATE_MCS(RATE_MODE_CCK, 0),
  491. },
  492. {
  493. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
  494. .bitrate = 20,
  495. .ratemask = BIT(1),
  496. .plcp = 0x01,
  497. .mcs = RATE_MCS(RATE_MODE_CCK, 1),
  498. },
  499. {
  500. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
  501. .bitrate = 55,
  502. .ratemask = BIT(2),
  503. .plcp = 0x02,
  504. .mcs = RATE_MCS(RATE_MODE_CCK, 2),
  505. },
  506. {
  507. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
  508. .bitrate = 110,
  509. .ratemask = BIT(3),
  510. .plcp = 0x03,
  511. .mcs = RATE_MCS(RATE_MODE_CCK, 3),
  512. },
  513. {
  514. .flags = DEV_RATE_OFDM,
  515. .bitrate = 60,
  516. .ratemask = BIT(4),
  517. .plcp = 0x0b,
  518. .mcs = RATE_MCS(RATE_MODE_OFDM, 0),
  519. },
  520. {
  521. .flags = DEV_RATE_OFDM,
  522. .bitrate = 90,
  523. .ratemask = BIT(5),
  524. .plcp = 0x0f,
  525. .mcs = RATE_MCS(RATE_MODE_OFDM, 1),
  526. },
  527. {
  528. .flags = DEV_RATE_OFDM,
  529. .bitrate = 120,
  530. .ratemask = BIT(6),
  531. .plcp = 0x0a,
  532. .mcs = RATE_MCS(RATE_MODE_OFDM, 2),
  533. },
  534. {
  535. .flags = DEV_RATE_OFDM,
  536. .bitrate = 180,
  537. .ratemask = BIT(7),
  538. .plcp = 0x0e,
  539. .mcs = RATE_MCS(RATE_MODE_OFDM, 3),
  540. },
  541. {
  542. .flags = DEV_RATE_OFDM,
  543. .bitrate = 240,
  544. .ratemask = BIT(8),
  545. .plcp = 0x09,
  546. .mcs = RATE_MCS(RATE_MODE_OFDM, 4),
  547. },
  548. {
  549. .flags = DEV_RATE_OFDM,
  550. .bitrate = 360,
  551. .ratemask = BIT(9),
  552. .plcp = 0x0d,
  553. .mcs = RATE_MCS(RATE_MODE_OFDM, 5),
  554. },
  555. {
  556. .flags = DEV_RATE_OFDM,
  557. .bitrate = 480,
  558. .ratemask = BIT(10),
  559. .plcp = 0x08,
  560. .mcs = RATE_MCS(RATE_MODE_OFDM, 6),
  561. },
  562. {
  563. .flags = DEV_RATE_OFDM,
  564. .bitrate = 540,
  565. .ratemask = BIT(11),
  566. .plcp = 0x0c,
  567. .mcs = RATE_MCS(RATE_MODE_OFDM, 7),
  568. },
  569. };
  570. static void rt2x00lib_channel(struct ieee80211_channel *entry,
  571. const int channel, const int tx_power,
  572. const int value)
  573. {
  574. /* XXX: this assumption about the band is wrong for 802.11j */
  575. entry->band = channel <= 14 ? IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
  576. entry->center_freq = ieee80211_channel_to_frequency(channel,
  577. entry->band);
  578. entry->hw_value = value;
  579. entry->max_power = tx_power;
  580. entry->max_antenna_gain = 0xff;
  581. }
  582. static void rt2x00lib_rate(struct ieee80211_rate *entry,
  583. const u16 index, const struct rt2x00_rate *rate)
  584. {
  585. entry->flags = 0;
  586. entry->bitrate = rate->bitrate;
  587. entry->hw_value = index;
  588. entry->hw_value_short = index;
  589. if (rate->flags & DEV_RATE_SHORT_PREAMBLE)
  590. entry->flags |= IEEE80211_RATE_SHORT_PREAMBLE;
  591. }
  592. static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
  593. struct hw_mode_spec *spec)
  594. {
  595. struct ieee80211_hw *hw = rt2x00dev->hw;
  596. struct ieee80211_channel *channels;
  597. struct ieee80211_rate *rates;
  598. unsigned int num_rates;
  599. unsigned int i;
  600. num_rates = 0;
  601. if (spec->supported_rates & SUPPORT_RATE_CCK)
  602. num_rates += 4;
  603. if (spec->supported_rates & SUPPORT_RATE_OFDM)
  604. num_rates += 8;
  605. channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
  606. if (!channels)
  607. return -ENOMEM;
  608. rates = kzalloc(sizeof(*rates) * num_rates, GFP_KERNEL);
  609. if (!rates)
  610. goto exit_free_channels;
  611. /*
  612. * Initialize Rate list.
  613. */
  614. for (i = 0; i < num_rates; i++)
  615. rt2x00lib_rate(&rates[i], i, rt2x00_get_rate(i));
  616. /*
  617. * Initialize Channel list.
  618. */
  619. for (i = 0; i < spec->num_channels; i++) {
  620. rt2x00lib_channel(&channels[i],
  621. spec->channels[i].channel,
  622. spec->channels_info[i].max_power, i);
  623. }
  624. /*
  625. * Intitialize 802.11b, 802.11g
  626. * Rates: CCK, OFDM.
  627. * Channels: 2.4 GHz
  628. */
  629. if (spec->supported_bands & SUPPORT_BAND_2GHZ) {
  630. rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_channels = 14;
  631. rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_bitrates = num_rates;
  632. rt2x00dev->bands[IEEE80211_BAND_2GHZ].channels = channels;
  633. rt2x00dev->bands[IEEE80211_BAND_2GHZ].bitrates = rates;
  634. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  635. &rt2x00dev->bands[IEEE80211_BAND_2GHZ];
  636. memcpy(&rt2x00dev->bands[IEEE80211_BAND_2GHZ].ht_cap,
  637. &spec->ht, sizeof(spec->ht));
  638. }
  639. /*
  640. * Intitialize 802.11a
  641. * Rates: OFDM.
  642. * Channels: OFDM, UNII, HiperLAN2.
  643. */
  644. if (spec->supported_bands & SUPPORT_BAND_5GHZ) {
  645. rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_channels =
  646. spec->num_channels - 14;
  647. rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_bitrates =
  648. num_rates - 4;
  649. rt2x00dev->bands[IEEE80211_BAND_5GHZ].channels = &channels[14];
  650. rt2x00dev->bands[IEEE80211_BAND_5GHZ].bitrates = &rates[4];
  651. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  652. &rt2x00dev->bands[IEEE80211_BAND_5GHZ];
  653. memcpy(&rt2x00dev->bands[IEEE80211_BAND_5GHZ].ht_cap,
  654. &spec->ht, sizeof(spec->ht));
  655. }
  656. return 0;
  657. exit_free_channels:
  658. kfree(channels);
  659. ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
  660. return -ENOMEM;
  661. }
  662. static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
  663. {
  664. if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
  665. ieee80211_unregister_hw(rt2x00dev->hw);
  666. if (likely(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ])) {
  667. kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->channels);
  668. kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->bitrates);
  669. rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
  670. rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
  671. }
  672. kfree(rt2x00dev->spec.channels_info);
  673. }
  674. static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
  675. {
  676. struct hw_mode_spec *spec = &rt2x00dev->spec;
  677. int status;
  678. if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
  679. return 0;
  680. /*
  681. * Initialize HW modes.
  682. */
  683. status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
  684. if (status)
  685. return status;
  686. /*
  687. * Initialize HW fields.
  688. */
  689. rt2x00dev->hw->queues = rt2x00dev->ops->tx_queues;
  690. /*
  691. * Initialize extra TX headroom required.
  692. */
  693. rt2x00dev->hw->extra_tx_headroom =
  694. max_t(unsigned int, IEEE80211_TX_STATUS_HEADROOM,
  695. rt2x00dev->ops->extra_tx_headroom);
  696. /*
  697. * Take TX headroom required for alignment into account.
  698. */
  699. if (test_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags))
  700. rt2x00dev->hw->extra_tx_headroom += RT2X00_L2PAD_SIZE;
  701. else if (test_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags))
  702. rt2x00dev->hw->extra_tx_headroom += RT2X00_ALIGN_SIZE;
  703. /*
  704. * Allocate tx status FIFO for driver use.
  705. */
  706. if (test_bit(DRIVER_REQUIRE_TXSTATUS_FIFO, &rt2x00dev->flags)) {
  707. /*
  708. * Allocate txstatus fifo and tasklet, we use a size of 512
  709. * for the kfifo which is big enough to store 512/4=128 tx
  710. * status reports. In the worst case (tx status for all tx
  711. * queues gets reported before we've got a chance to handle
  712. * them) 24*4=384 tx status reports need to be cached.
  713. */
  714. status = kfifo_alloc(&rt2x00dev->txstatus_fifo, 512,
  715. GFP_KERNEL);
  716. if (status)
  717. return status;
  718. }
  719. /*
  720. * Initialize tasklets if used by the driver. Tasklets are
  721. * disabled until the interrupts are turned on. The driver
  722. * has to handle that.
  723. */
  724. #define RT2X00_TASKLET_INIT(taskletname) \
  725. if (rt2x00dev->ops->lib->taskletname) { \
  726. tasklet_init(&rt2x00dev->taskletname, \
  727. rt2x00dev->ops->lib->taskletname, \
  728. (unsigned long)rt2x00dev); \
  729. tasklet_disable(&rt2x00dev->taskletname); \
  730. }
  731. RT2X00_TASKLET_INIT(txstatus_tasklet);
  732. RT2X00_TASKLET_INIT(pretbtt_tasklet);
  733. RT2X00_TASKLET_INIT(tbtt_tasklet);
  734. RT2X00_TASKLET_INIT(rxdone_tasklet);
  735. RT2X00_TASKLET_INIT(autowake_tasklet);
  736. #undef RT2X00_TASKLET_INIT
  737. /*
  738. * Register HW.
  739. */
  740. status = ieee80211_register_hw(rt2x00dev->hw);
  741. if (status)
  742. return status;
  743. set_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags);
  744. return 0;
  745. }
  746. /*
  747. * Initialization/uninitialization handlers.
  748. */
  749. static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
  750. {
  751. if (!test_and_clear_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
  752. return;
  753. /*
  754. * Unregister extra components.
  755. */
  756. rt2x00rfkill_unregister(rt2x00dev);
  757. /*
  758. * Allow the HW to uninitialize.
  759. */
  760. rt2x00dev->ops->lib->uninitialize(rt2x00dev);
  761. /*
  762. * Free allocated queue entries.
  763. */
  764. rt2x00queue_uninitialize(rt2x00dev);
  765. }
  766. static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
  767. {
  768. int status;
  769. if (test_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
  770. return 0;
  771. /*
  772. * Allocate all queue entries.
  773. */
  774. status = rt2x00queue_initialize(rt2x00dev);
  775. if (status)
  776. return status;
  777. /*
  778. * Initialize the device.
  779. */
  780. status = rt2x00dev->ops->lib->initialize(rt2x00dev);
  781. if (status) {
  782. rt2x00queue_uninitialize(rt2x00dev);
  783. return status;
  784. }
  785. set_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags);
  786. /*
  787. * Register the extra components.
  788. */
  789. rt2x00rfkill_register(rt2x00dev);
  790. return 0;
  791. }
  792. int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
  793. {
  794. int retval;
  795. if (test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
  796. return 0;
  797. /*
  798. * If this is the first interface which is added,
  799. * we should load the firmware now.
  800. */
  801. retval = rt2x00lib_load_firmware(rt2x00dev);
  802. if (retval)
  803. return retval;
  804. /*
  805. * Initialize the device.
  806. */
  807. retval = rt2x00lib_initialize(rt2x00dev);
  808. if (retval)
  809. return retval;
  810. rt2x00dev->intf_ap_count = 0;
  811. rt2x00dev->intf_sta_count = 0;
  812. rt2x00dev->intf_associated = 0;
  813. /* Enable the radio */
  814. retval = rt2x00lib_enable_radio(rt2x00dev);
  815. if (retval)
  816. return retval;
  817. set_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags);
  818. return 0;
  819. }
  820. void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
  821. {
  822. if (!test_and_clear_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
  823. return;
  824. /*
  825. * Perhaps we can add something smarter here,
  826. * but for now just disabling the radio should do.
  827. */
  828. rt2x00lib_disable_radio(rt2x00dev);
  829. rt2x00dev->intf_ap_count = 0;
  830. rt2x00dev->intf_sta_count = 0;
  831. rt2x00dev->intf_associated = 0;
  832. }
  833. /*
  834. * driver allocation handlers.
  835. */
  836. int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
  837. {
  838. int retval = -ENOMEM;
  839. spin_lock_init(&rt2x00dev->irqmask_lock);
  840. mutex_init(&rt2x00dev->csr_mutex);
  841. set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  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. * Determine which operating modes are supported, all modes
  849. * which require beaconing, depend on the availability of
  850. * beacon entries.
  851. */
  852. rt2x00dev->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  853. if (rt2x00dev->ops->bcn->entry_num > 0)
  854. rt2x00dev->hw->wiphy->interface_modes |=
  855. BIT(NL80211_IFTYPE_ADHOC) |
  856. BIT(NL80211_IFTYPE_AP) |
  857. BIT(NL80211_IFTYPE_MESH_POINT) |
  858. BIT(NL80211_IFTYPE_WDS);
  859. /*
  860. * Initialize work.
  861. */
  862. rt2x00dev->workqueue =
  863. alloc_ordered_workqueue(wiphy_name(rt2x00dev->hw->wiphy), 0);
  864. if (!rt2x00dev->workqueue) {
  865. retval = -ENOMEM;
  866. goto exit;
  867. }
  868. INIT_WORK(&rt2x00dev->intf_work, rt2x00lib_intf_scheduled);
  869. /*
  870. * Let the driver probe the device to detect the capabilities.
  871. */
  872. retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
  873. if (retval) {
  874. ERROR(rt2x00dev, "Failed to allocate device.\n");
  875. goto exit;
  876. }
  877. /*
  878. * Allocate queue array.
  879. */
  880. retval = rt2x00queue_allocate(rt2x00dev);
  881. if (retval)
  882. goto exit;
  883. /*
  884. * Initialize ieee80211 structure.
  885. */
  886. retval = rt2x00lib_probe_hw(rt2x00dev);
  887. if (retval) {
  888. ERROR(rt2x00dev, "Failed to initialize hw.\n");
  889. goto exit;
  890. }
  891. /*
  892. * Register extra components.
  893. */
  894. rt2x00link_register(rt2x00dev);
  895. rt2x00leds_register(rt2x00dev);
  896. rt2x00debug_register(rt2x00dev);
  897. return 0;
  898. exit:
  899. rt2x00lib_remove_dev(rt2x00dev);
  900. return retval;
  901. }
  902. EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
  903. void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
  904. {
  905. clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  906. /*
  907. * Disable radio.
  908. */
  909. rt2x00lib_disable_radio(rt2x00dev);
  910. /*
  911. * Stop all work.
  912. */
  913. cancel_work_sync(&rt2x00dev->intf_work);
  914. if (rt2x00_is_usb(rt2x00dev)) {
  915. cancel_work_sync(&rt2x00dev->rxdone_work);
  916. cancel_work_sync(&rt2x00dev->txdone_work);
  917. }
  918. destroy_workqueue(rt2x00dev->workqueue);
  919. /*
  920. * Free the tx status fifo.
  921. */
  922. kfifo_free(&rt2x00dev->txstatus_fifo);
  923. /*
  924. * Kill the tx status tasklet.
  925. */
  926. tasklet_kill(&rt2x00dev->txstatus_tasklet);
  927. tasklet_kill(&rt2x00dev->pretbtt_tasklet);
  928. tasklet_kill(&rt2x00dev->tbtt_tasklet);
  929. tasklet_kill(&rt2x00dev->rxdone_tasklet);
  930. tasklet_kill(&rt2x00dev->autowake_tasklet);
  931. /*
  932. * Uninitialize device.
  933. */
  934. rt2x00lib_uninitialize(rt2x00dev);
  935. /*
  936. * Free extra components
  937. */
  938. rt2x00debug_deregister(rt2x00dev);
  939. rt2x00leds_unregister(rt2x00dev);
  940. /*
  941. * Free ieee80211_hw memory.
  942. */
  943. rt2x00lib_remove_hw(rt2x00dev);
  944. /*
  945. * Free firmware image.
  946. */
  947. rt2x00lib_free_firmware(rt2x00dev);
  948. /*
  949. * Free queue structures.
  950. */
  951. rt2x00queue_free(rt2x00dev);
  952. }
  953. EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
  954. /*
  955. * Device state handlers
  956. */
  957. #ifdef CONFIG_PM
  958. int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
  959. {
  960. NOTICE(rt2x00dev, "Going to sleep.\n");
  961. /*
  962. * Prevent mac80211 from accessing driver while suspended.
  963. */
  964. if (!test_and_clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  965. return 0;
  966. /*
  967. * Cleanup as much as possible.
  968. */
  969. rt2x00lib_uninitialize(rt2x00dev);
  970. /*
  971. * Suspend/disable extra components.
  972. */
  973. rt2x00leds_suspend(rt2x00dev);
  974. rt2x00debug_deregister(rt2x00dev);
  975. /*
  976. * Set device mode to sleep for power management,
  977. * on some hardware this call seems to consistently fail.
  978. * From the specifications it is hard to tell why it fails,
  979. * and if this is a "bad thing".
  980. * Overall it is safe to just ignore the failure and
  981. * continue suspending. The only downside is that the
  982. * device will not be in optimal power save mode, but with
  983. * the radio and the other components already disabled the
  984. * device is as good as disabled.
  985. */
  986. if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP))
  987. WARNING(rt2x00dev, "Device failed to enter sleep state, "
  988. "continue suspending.\n");
  989. return 0;
  990. }
  991. EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
  992. int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
  993. {
  994. NOTICE(rt2x00dev, "Waking up.\n");
  995. /*
  996. * Restore/enable extra components.
  997. */
  998. rt2x00debug_register(rt2x00dev);
  999. rt2x00leds_resume(rt2x00dev);
  1000. /*
  1001. * We are ready again to receive requests from mac80211.
  1002. */
  1003. set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  1004. return 0;
  1005. }
  1006. EXPORT_SYMBOL_GPL(rt2x00lib_resume);
  1007. #endif /* CONFIG_PM */
  1008. /*
  1009. * rt2x00lib module information.
  1010. */
  1011. MODULE_AUTHOR(DRV_PROJECT);
  1012. MODULE_VERSION(DRV_VERSION);
  1013. MODULE_DESCRIPTION("rt2x00 library");
  1014. MODULE_LICENSE("GPL");