rt2x00dev.c 27 KB

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