rt2x00dev.c 29 KB

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