rt2x00dev.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  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. int delayed_flags;
  93. /*
  94. * Copy all data we need during this action under the protection
  95. * of a spinlock. Otherwise race conditions might occur which results
  96. * into an invalid configuration.
  97. */
  98. spin_lock(&intf->lock);
  99. delayed_flags = intf->delayed_flags;
  100. intf->delayed_flags = 0;
  101. spin_unlock(&intf->lock);
  102. /*
  103. * It is possible the radio was disabled while the work had been
  104. * scheduled. If that happens we should return here immediately,
  105. * note that in the spinlock protected area above the delayed_flags
  106. * have been cleared correctly.
  107. */
  108. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  109. return;
  110. if (delayed_flags & DELAYED_UPDATE_BEACON)
  111. rt2x00queue_update_beacon(rt2x00dev, vif, true);
  112. }
  113. static void rt2x00lib_intf_scheduled(struct work_struct *work)
  114. {
  115. struct rt2x00_dev *rt2x00dev =
  116. container_of(work, struct rt2x00_dev, intf_work);
  117. /*
  118. * Iterate over each interface and perform the
  119. * requested configurations.
  120. */
  121. ieee80211_iterate_active_interfaces(rt2x00dev->hw,
  122. rt2x00lib_intf_scheduled_iter,
  123. rt2x00dev);
  124. }
  125. /*
  126. * Interrupt context handlers.
  127. */
  128. static void rt2x00lib_bc_buffer_iter(void *data, u8 *mac,
  129. struct ieee80211_vif *vif)
  130. {
  131. struct rt2x00_dev *rt2x00dev = data;
  132. struct sk_buff *skb;
  133. /*
  134. * Only AP mode interfaces do broad- and multicast buffering
  135. */
  136. if (vif->type != NL80211_IFTYPE_AP)
  137. return;
  138. /*
  139. * Send out buffered broad- and multicast frames
  140. */
  141. skb = ieee80211_get_buffered_bc(rt2x00dev->hw, vif);
  142. while (skb) {
  143. rt2x00mac_tx(rt2x00dev->hw, skb);
  144. skb = ieee80211_get_buffered_bc(rt2x00dev->hw, vif);
  145. }
  146. }
  147. static void rt2x00lib_beaconupdate_iter(void *data, u8 *mac,
  148. struct ieee80211_vif *vif)
  149. {
  150. struct rt2x00_dev *rt2x00dev = data;
  151. if (vif->type != NL80211_IFTYPE_AP &&
  152. vif->type != NL80211_IFTYPE_ADHOC &&
  153. vif->type != NL80211_IFTYPE_MESH_POINT &&
  154. vif->type != NL80211_IFTYPE_WDS)
  155. return;
  156. rt2x00queue_update_beacon(rt2x00dev, vif, true);
  157. }
  158. void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
  159. {
  160. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  161. return;
  162. /* send buffered bc/mc frames out for every bssid */
  163. ieee80211_iterate_active_interfaces(rt2x00dev->hw,
  164. rt2x00lib_bc_buffer_iter,
  165. rt2x00dev);
  166. /*
  167. * Devices with pre tbtt interrupt don't need to update the beacon
  168. * here as they will fetch the next beacon directly prior to
  169. * transmission.
  170. */
  171. if (test_bit(DRIVER_SUPPORT_PRE_TBTT_INTERRUPT, &rt2x00dev->flags))
  172. return;
  173. /* fetch next beacon */
  174. ieee80211_iterate_active_interfaces(rt2x00dev->hw,
  175. rt2x00lib_beaconupdate_iter,
  176. rt2x00dev);
  177. }
  178. EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
  179. void rt2x00lib_pretbtt(struct rt2x00_dev *rt2x00dev)
  180. {
  181. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  182. return;
  183. /* fetch next beacon */
  184. ieee80211_iterate_active_interfaces(rt2x00dev->hw,
  185. rt2x00lib_beaconupdate_iter,
  186. rt2x00dev);
  187. }
  188. EXPORT_SYMBOL_GPL(rt2x00lib_pretbtt);
  189. void rt2x00lib_dmastart(struct queue_entry *entry)
  190. {
  191. set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  192. rt2x00queue_index_inc(entry->queue, Q_INDEX);
  193. }
  194. EXPORT_SYMBOL_GPL(rt2x00lib_dmastart);
  195. void rt2x00lib_dmadone(struct queue_entry *entry)
  196. {
  197. set_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags);
  198. clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  199. rt2x00queue_index_inc(entry->queue, Q_INDEX_DMA_DONE);
  200. }
  201. EXPORT_SYMBOL_GPL(rt2x00lib_dmadone);
  202. void rt2x00lib_txdone(struct queue_entry *entry,
  203. struct txdone_entry_desc *txdesc)
  204. {
  205. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  206. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
  207. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  208. unsigned int header_length, i;
  209. u8 rate_idx, rate_flags, retry_rates;
  210. u8 skbdesc_flags = skbdesc->flags;
  211. bool success;
  212. /*
  213. * Unmap the skb.
  214. */
  215. rt2x00queue_unmap_skb(entry);
  216. /*
  217. * Remove the extra tx headroom from the skb.
  218. */
  219. skb_pull(entry->skb, rt2x00dev->ops->extra_tx_headroom);
  220. /*
  221. * Signal that the TX descriptor is no longer in the skb.
  222. */
  223. skbdesc->flags &= ~SKBDESC_DESC_IN_SKB;
  224. /*
  225. * Determine the length of 802.11 header.
  226. */
  227. header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
  228. /*
  229. * Remove L2 padding which was added during
  230. */
  231. if (test_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags))
  232. rt2x00queue_remove_l2pad(entry->skb, header_length);
  233. /*
  234. * If the IV/EIV data was stripped from the frame before it was
  235. * passed to the hardware, we should now reinsert it again because
  236. * mac80211 will expect the same data to be present it the
  237. * frame as it was passed to us.
  238. */
  239. if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags))
  240. rt2x00crypto_tx_insert_iv(entry->skb, header_length);
  241. /*
  242. * Send frame to debugfs immediately, after this call is completed
  243. * we are going to overwrite the skb->cb array.
  244. */
  245. rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TXDONE, entry->skb);
  246. /*
  247. * Determine if the frame has been successfully transmitted.
  248. */
  249. success =
  250. test_bit(TXDONE_SUCCESS, &txdesc->flags) ||
  251. test_bit(TXDONE_UNKNOWN, &txdesc->flags);
  252. /*
  253. * Update TX statistics.
  254. */
  255. rt2x00dev->link.qual.tx_success += success;
  256. rt2x00dev->link.qual.tx_failed += !success;
  257. rate_idx = skbdesc->tx_rate_idx;
  258. rate_flags = skbdesc->tx_rate_flags;
  259. retry_rates = test_bit(TXDONE_FALLBACK, &txdesc->flags) ?
  260. (txdesc->retry + 1) : 1;
  261. /*
  262. * Initialize TX status
  263. */
  264. memset(&tx_info->status, 0, sizeof(tx_info->status));
  265. tx_info->status.ack_signal = 0;
  266. /*
  267. * Frame was send with retries, hardware tried
  268. * different rates to send out the frame, at each
  269. * retry it lowered the rate 1 step except when the
  270. * lowest rate was used.
  271. */
  272. for (i = 0; i < retry_rates && i < IEEE80211_TX_MAX_RATES; i++) {
  273. tx_info->status.rates[i].idx = rate_idx - i;
  274. tx_info->status.rates[i].flags = rate_flags;
  275. if (rate_idx - i == 0) {
  276. /*
  277. * The lowest rate (index 0) was used until the
  278. * number of max retries was reached.
  279. */
  280. tx_info->status.rates[i].count = retry_rates - i;
  281. i++;
  282. break;
  283. }
  284. tx_info->status.rates[i].count = 1;
  285. }
  286. if (i < (IEEE80211_TX_MAX_RATES - 1))
  287. tx_info->status.rates[i].idx = -1; /* terminate */
  288. if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  289. if (success)
  290. tx_info->flags |= IEEE80211_TX_STAT_ACK;
  291. else
  292. rt2x00dev->low_level_stats.dot11ACKFailureCount++;
  293. }
  294. /*
  295. * Every single frame has it's own tx status, hence report
  296. * every frame as ampdu of size 1.
  297. *
  298. * TODO: if we can find out how many frames were aggregated
  299. * by the hw we could provide the real ampdu_len to mac80211
  300. * which would allow the rc algorithm to better decide on
  301. * which rates are suitable.
  302. */
  303. if (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. }
  308. if (rate_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  309. if (success)
  310. rt2x00dev->low_level_stats.dot11RTSSuccessCount++;
  311. else
  312. rt2x00dev->low_level_stats.dot11RTSFailureCount++;
  313. }
  314. /*
  315. * Only send the status report to mac80211 when it's a frame
  316. * that originated in mac80211. If this was a extra frame coming
  317. * through a mac80211 library call (RTS/CTS) then we should not
  318. * send the status report back.
  319. */
  320. if (!(skbdesc_flags & SKBDESC_NOT_MAC80211)) {
  321. if (test_bit(DRIVER_REQUIRE_TASKLET_CONTEXT, &rt2x00dev->flags))
  322. ieee80211_tx_status(rt2x00dev->hw, entry->skb);
  323. else
  324. ieee80211_tx_status_ni(rt2x00dev->hw, entry->skb);
  325. } else
  326. dev_kfree_skb_any(entry->skb);
  327. /*
  328. * Make this entry available for reuse.
  329. */
  330. entry->skb = NULL;
  331. entry->flags = 0;
  332. rt2x00dev->ops->lib->clear_entry(entry);
  333. rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
  334. /*
  335. * If the data queue was below the threshold before the txdone
  336. * handler we must make sure the packet queue in the mac80211 stack
  337. * is reenabled when the txdone handler has finished.
  338. */
  339. if (!rt2x00queue_threshold(entry->queue))
  340. rt2x00queue_unpause_queue(entry->queue);
  341. }
  342. EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
  343. void rt2x00lib_txdone_noinfo(struct queue_entry *entry, u32 status)
  344. {
  345. struct txdone_entry_desc txdesc;
  346. txdesc.flags = 0;
  347. __set_bit(status, &txdesc.flags);
  348. txdesc.retry = 0;
  349. rt2x00lib_txdone(entry, &txdesc);
  350. }
  351. EXPORT_SYMBOL_GPL(rt2x00lib_txdone_noinfo);
  352. static int rt2x00lib_rxdone_read_signal(struct rt2x00_dev *rt2x00dev,
  353. struct rxdone_entry_desc *rxdesc)
  354. {
  355. struct ieee80211_supported_band *sband;
  356. const struct rt2x00_rate *rate;
  357. unsigned int i;
  358. int signal = rxdesc->signal;
  359. int type = (rxdesc->dev_flags & RXDONE_SIGNAL_MASK);
  360. switch (rxdesc->rate_mode) {
  361. case RATE_MODE_CCK:
  362. case RATE_MODE_OFDM:
  363. /*
  364. * For non-HT rates the MCS value needs to contain the
  365. * actually used rate modulation (CCK or OFDM).
  366. */
  367. if (rxdesc->dev_flags & RXDONE_SIGNAL_MCS)
  368. signal = RATE_MCS(rxdesc->rate_mode, signal);
  369. sband = &rt2x00dev->bands[rt2x00dev->curr_band];
  370. for (i = 0; i < sband->n_bitrates; i++) {
  371. rate = rt2x00_get_rate(sband->bitrates[i].hw_value);
  372. if (((type == RXDONE_SIGNAL_PLCP) &&
  373. (rate->plcp == signal)) ||
  374. ((type == RXDONE_SIGNAL_BITRATE) &&
  375. (rate->bitrate == signal)) ||
  376. ((type == RXDONE_SIGNAL_MCS) &&
  377. (rate->mcs == signal))) {
  378. return i;
  379. }
  380. }
  381. break;
  382. case RATE_MODE_HT_MIX:
  383. case RATE_MODE_HT_GREENFIELD:
  384. if (signal >= 0 && signal <= 76)
  385. return signal;
  386. break;
  387. default:
  388. break;
  389. }
  390. WARNING(rt2x00dev, "Frame received with unrecognized signal, "
  391. "mode=0x%.4x, signal=0x%.4x, type=%d.\n",
  392. rxdesc->rate_mode, signal, type);
  393. return 0;
  394. }
  395. void rt2x00lib_rxdone(struct queue_entry *entry)
  396. {
  397. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  398. struct rxdone_entry_desc rxdesc;
  399. struct sk_buff *skb;
  400. struct ieee80211_rx_status *rx_status;
  401. unsigned int header_length;
  402. int rate_idx;
  403. if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) ||
  404. !test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  405. goto submit_entry;
  406. if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags))
  407. goto submit_entry;
  408. /*
  409. * Allocate a new sk_buffer. If no new buffer available, drop the
  410. * received frame and reuse the existing buffer.
  411. */
  412. skb = rt2x00queue_alloc_rxskb(entry);
  413. if (!skb)
  414. goto submit_entry;
  415. /*
  416. * Unmap the skb.
  417. */
  418. rt2x00queue_unmap_skb(entry);
  419. /*
  420. * Extract the RXD details.
  421. */
  422. memset(&rxdesc, 0, sizeof(rxdesc));
  423. rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
  424. /*
  425. * The data behind the ieee80211 header must be
  426. * aligned on a 4 byte boundary.
  427. */
  428. header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
  429. /*
  430. * Hardware might have stripped the IV/EIV/ICV data,
  431. * in that case it is possible that the data was
  432. * provided separately (through hardware descriptor)
  433. * in which case we should reinsert the data into the frame.
  434. */
  435. if ((rxdesc.dev_flags & RXDONE_CRYPTO_IV) &&
  436. (rxdesc.flags & RX_FLAG_IV_STRIPPED))
  437. rt2x00crypto_rx_insert_iv(entry->skb, header_length,
  438. &rxdesc);
  439. else if (header_length &&
  440. (rxdesc.size > header_length) &&
  441. (rxdesc.dev_flags & RXDONE_L2PAD))
  442. rt2x00queue_remove_l2pad(entry->skb, header_length);
  443. else
  444. rt2x00queue_align_payload(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->queue, 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. entry->center_freq = ieee80211_channel_to_frequency(channel);
  579. entry->hw_value = value;
  580. entry->max_power = tx_power;
  581. entry->max_antenna_gain = 0xff;
  582. }
  583. static void rt2x00lib_rate(struct ieee80211_rate *entry,
  584. const u16 index, const struct rt2x00_rate *rate)
  585. {
  586. entry->flags = 0;
  587. entry->bitrate = rate->bitrate;
  588. entry->hw_value = index;
  589. entry->hw_value_short = index;
  590. if (rate->flags & DEV_RATE_SHORT_PREAMBLE)
  591. entry->flags |= IEEE80211_RATE_SHORT_PREAMBLE;
  592. }
  593. static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
  594. struct hw_mode_spec *spec)
  595. {
  596. struct ieee80211_hw *hw = rt2x00dev->hw;
  597. struct ieee80211_channel *channels;
  598. struct ieee80211_rate *rates;
  599. unsigned int num_rates;
  600. unsigned int i;
  601. num_rates = 0;
  602. if (spec->supported_rates & SUPPORT_RATE_CCK)
  603. num_rates += 4;
  604. if (spec->supported_rates & SUPPORT_RATE_OFDM)
  605. num_rates += 8;
  606. channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
  607. if (!channels)
  608. return -ENOMEM;
  609. rates = kzalloc(sizeof(*rates) * num_rates, GFP_KERNEL);
  610. if (!rates)
  611. goto exit_free_channels;
  612. /*
  613. * Initialize Rate list.
  614. */
  615. for (i = 0; i < num_rates; i++)
  616. rt2x00lib_rate(&rates[i], i, rt2x00_get_rate(i));
  617. /*
  618. * Initialize Channel list.
  619. */
  620. for (i = 0; i < spec->num_channels; i++) {
  621. rt2x00lib_channel(&channels[i],
  622. spec->channels[i].channel,
  623. spec->channels_info[i].max_power, i);
  624. }
  625. /*
  626. * Intitialize 802.11b, 802.11g
  627. * Rates: CCK, OFDM.
  628. * Channels: 2.4 GHz
  629. */
  630. if (spec->supported_bands & SUPPORT_BAND_2GHZ) {
  631. rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_channels = 14;
  632. rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_bitrates = num_rates;
  633. rt2x00dev->bands[IEEE80211_BAND_2GHZ].channels = channels;
  634. rt2x00dev->bands[IEEE80211_BAND_2GHZ].bitrates = rates;
  635. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  636. &rt2x00dev->bands[IEEE80211_BAND_2GHZ];
  637. memcpy(&rt2x00dev->bands[IEEE80211_BAND_2GHZ].ht_cap,
  638. &spec->ht, sizeof(spec->ht));
  639. }
  640. /*
  641. * Intitialize 802.11a
  642. * Rates: OFDM.
  643. * Channels: OFDM, UNII, HiperLAN2.
  644. */
  645. if (spec->supported_bands & SUPPORT_BAND_5GHZ) {
  646. rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_channels =
  647. spec->num_channels - 14;
  648. rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_bitrates =
  649. num_rates - 4;
  650. rt2x00dev->bands[IEEE80211_BAND_5GHZ].channels = &channels[14];
  651. rt2x00dev->bands[IEEE80211_BAND_5GHZ].bitrates = &rates[4];
  652. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  653. &rt2x00dev->bands[IEEE80211_BAND_5GHZ];
  654. memcpy(&rt2x00dev->bands[IEEE80211_BAND_5GHZ].ht_cap,
  655. &spec->ht, sizeof(spec->ht));
  656. }
  657. return 0;
  658. exit_free_channels:
  659. kfree(channels);
  660. ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
  661. return -ENOMEM;
  662. }
  663. static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
  664. {
  665. if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
  666. ieee80211_unregister_hw(rt2x00dev->hw);
  667. if (likely(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ])) {
  668. kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->channels);
  669. kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->bitrates);
  670. rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
  671. rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
  672. }
  673. kfree(rt2x00dev->spec.channels_info);
  674. }
  675. static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
  676. {
  677. struct hw_mode_spec *spec = &rt2x00dev->spec;
  678. int status;
  679. if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
  680. return 0;
  681. /*
  682. * Initialize HW modes.
  683. */
  684. status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
  685. if (status)
  686. return status;
  687. /*
  688. * Initialize HW fields.
  689. */
  690. rt2x00dev->hw->queues = rt2x00dev->ops->tx_queues;
  691. /*
  692. * Initialize extra TX headroom required.
  693. */
  694. rt2x00dev->hw->extra_tx_headroom =
  695. max_t(unsigned int, IEEE80211_TX_STATUS_HEADROOM,
  696. rt2x00dev->ops->extra_tx_headroom);
  697. /*
  698. * Take TX headroom required for alignment into account.
  699. */
  700. if (test_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags))
  701. rt2x00dev->hw->extra_tx_headroom += RT2X00_L2PAD_SIZE;
  702. else if (test_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags))
  703. rt2x00dev->hw->extra_tx_headroom += RT2X00_ALIGN_SIZE;
  704. /*
  705. * Allocate tx status FIFO for driver use.
  706. */
  707. if (test_bit(DRIVER_REQUIRE_TXSTATUS_FIFO, &rt2x00dev->flags) &&
  708. rt2x00dev->ops->lib->txstatus_tasklet) {
  709. /*
  710. * Allocate txstatus fifo and tasklet, we use a size of 512
  711. * for the kfifo which is big enough to store 512/4=128 tx
  712. * status reports. In the worst case (tx status for all tx
  713. * queues gets reported before we've got a chance to handle
  714. * them) 24*4=384 tx status reports need to be cached.
  715. */
  716. status = kfifo_alloc(&rt2x00dev->txstatus_fifo, 512,
  717. GFP_KERNEL);
  718. if (status)
  719. return status;
  720. /* tasklet for processing the tx status reports. */
  721. tasklet_init(&rt2x00dev->txstatus_tasklet,
  722. rt2x00dev->ops->lib->txstatus_tasklet,
  723. (unsigned long)rt2x00dev);
  724. }
  725. /*
  726. * Register HW.
  727. */
  728. status = ieee80211_register_hw(rt2x00dev->hw);
  729. if (status)
  730. return status;
  731. set_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags);
  732. return 0;
  733. }
  734. /*
  735. * Initialization/uninitialization handlers.
  736. */
  737. static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
  738. {
  739. if (!test_and_clear_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
  740. return;
  741. /*
  742. * Unregister extra components.
  743. */
  744. rt2x00rfkill_unregister(rt2x00dev);
  745. /*
  746. * Allow the HW to uninitialize.
  747. */
  748. rt2x00dev->ops->lib->uninitialize(rt2x00dev);
  749. /*
  750. * Free allocated queue entries.
  751. */
  752. rt2x00queue_uninitialize(rt2x00dev);
  753. }
  754. static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
  755. {
  756. int status;
  757. if (test_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
  758. return 0;
  759. /*
  760. * Allocate all queue entries.
  761. */
  762. status = rt2x00queue_initialize(rt2x00dev);
  763. if (status)
  764. return status;
  765. /*
  766. * Initialize the device.
  767. */
  768. status = rt2x00dev->ops->lib->initialize(rt2x00dev);
  769. if (status) {
  770. rt2x00queue_uninitialize(rt2x00dev);
  771. return status;
  772. }
  773. set_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags);
  774. /*
  775. * Register the extra components.
  776. */
  777. rt2x00rfkill_register(rt2x00dev);
  778. return 0;
  779. }
  780. int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
  781. {
  782. int retval;
  783. if (test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
  784. return 0;
  785. /*
  786. * If this is the first interface which is added,
  787. * we should load the firmware now.
  788. */
  789. retval = rt2x00lib_load_firmware(rt2x00dev);
  790. if (retval)
  791. return retval;
  792. /*
  793. * Initialize the device.
  794. */
  795. retval = rt2x00lib_initialize(rt2x00dev);
  796. if (retval)
  797. return retval;
  798. rt2x00dev->intf_ap_count = 0;
  799. rt2x00dev->intf_sta_count = 0;
  800. rt2x00dev->intf_associated = 0;
  801. /* Enable the radio */
  802. retval = rt2x00lib_enable_radio(rt2x00dev);
  803. if (retval)
  804. return retval;
  805. set_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags);
  806. return 0;
  807. }
  808. void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
  809. {
  810. if (!test_and_clear_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
  811. return;
  812. /*
  813. * Perhaps we can add something smarter here,
  814. * but for now just disabling the radio should do.
  815. */
  816. rt2x00lib_disable_radio(rt2x00dev);
  817. rt2x00dev->intf_ap_count = 0;
  818. rt2x00dev->intf_sta_count = 0;
  819. rt2x00dev->intf_associated = 0;
  820. }
  821. /*
  822. * driver allocation handlers.
  823. */
  824. int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
  825. {
  826. int retval = -ENOMEM;
  827. mutex_init(&rt2x00dev->csr_mutex);
  828. set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  829. /*
  830. * Make room for rt2x00_intf inside the per-interface
  831. * structure ieee80211_vif.
  832. */
  833. rt2x00dev->hw->vif_data_size = sizeof(struct rt2x00_intf);
  834. /*
  835. * Determine which operating modes are supported, all modes
  836. * which require beaconing, depend on the availability of
  837. * beacon entries.
  838. */
  839. rt2x00dev->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  840. if (rt2x00dev->ops->bcn->entry_num > 0)
  841. rt2x00dev->hw->wiphy->interface_modes |=
  842. BIT(NL80211_IFTYPE_ADHOC) |
  843. BIT(NL80211_IFTYPE_AP) |
  844. BIT(NL80211_IFTYPE_MESH_POINT) |
  845. BIT(NL80211_IFTYPE_WDS);
  846. /*
  847. * Initialize configuration work.
  848. */
  849. INIT_WORK(&rt2x00dev->intf_work, rt2x00lib_intf_scheduled);
  850. /*
  851. * Let the driver probe the device to detect the capabilities.
  852. */
  853. retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
  854. if (retval) {
  855. ERROR(rt2x00dev, "Failed to allocate device.\n");
  856. goto exit;
  857. }
  858. /*
  859. * Allocate queue array.
  860. */
  861. retval = rt2x00queue_allocate(rt2x00dev);
  862. if (retval)
  863. goto exit;
  864. /*
  865. * Initialize ieee80211 structure.
  866. */
  867. retval = rt2x00lib_probe_hw(rt2x00dev);
  868. if (retval) {
  869. ERROR(rt2x00dev, "Failed to initialize hw.\n");
  870. goto exit;
  871. }
  872. /*
  873. * Register extra components.
  874. */
  875. rt2x00link_register(rt2x00dev);
  876. rt2x00leds_register(rt2x00dev);
  877. rt2x00debug_register(rt2x00dev);
  878. return 0;
  879. exit:
  880. rt2x00lib_remove_dev(rt2x00dev);
  881. return retval;
  882. }
  883. EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
  884. void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
  885. {
  886. clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  887. /*
  888. * Disable radio.
  889. */
  890. rt2x00lib_disable_radio(rt2x00dev);
  891. /*
  892. * Stop all work.
  893. */
  894. cancel_work_sync(&rt2x00dev->intf_work);
  895. cancel_work_sync(&rt2x00dev->rxdone_work);
  896. cancel_work_sync(&rt2x00dev->txdone_work);
  897. /*
  898. * Free the tx status fifo.
  899. */
  900. kfifo_free(&rt2x00dev->txstatus_fifo);
  901. /*
  902. * Kill the tx status tasklet.
  903. */
  904. tasklet_kill(&rt2x00dev->txstatus_tasklet);
  905. /*
  906. * Uninitialize device.
  907. */
  908. rt2x00lib_uninitialize(rt2x00dev);
  909. /*
  910. * Free extra components
  911. */
  912. rt2x00debug_deregister(rt2x00dev);
  913. rt2x00leds_unregister(rt2x00dev);
  914. /*
  915. * Free ieee80211_hw memory.
  916. */
  917. rt2x00lib_remove_hw(rt2x00dev);
  918. /*
  919. * Free firmware image.
  920. */
  921. rt2x00lib_free_firmware(rt2x00dev);
  922. /*
  923. * Free queue structures.
  924. */
  925. rt2x00queue_free(rt2x00dev);
  926. }
  927. EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
  928. /*
  929. * Device state handlers
  930. */
  931. #ifdef CONFIG_PM
  932. int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
  933. {
  934. NOTICE(rt2x00dev, "Going to sleep.\n");
  935. /*
  936. * Prevent mac80211 from accessing driver while suspended.
  937. */
  938. if (!test_and_clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  939. return 0;
  940. /*
  941. * Cleanup as much as possible.
  942. */
  943. rt2x00lib_uninitialize(rt2x00dev);
  944. /*
  945. * Suspend/disable extra components.
  946. */
  947. rt2x00leds_suspend(rt2x00dev);
  948. rt2x00debug_deregister(rt2x00dev);
  949. /*
  950. * Set device mode to sleep for power management,
  951. * on some hardware this call seems to consistently fail.
  952. * From the specifications it is hard to tell why it fails,
  953. * and if this is a "bad thing".
  954. * Overall it is safe to just ignore the failure and
  955. * continue suspending. The only downside is that the
  956. * device will not be in optimal power save mode, but with
  957. * the radio and the other components already disabled the
  958. * device is as good as disabled.
  959. */
  960. if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP))
  961. WARNING(rt2x00dev, "Device failed to enter sleep state, "
  962. "continue suspending.\n");
  963. return 0;
  964. }
  965. EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
  966. int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
  967. {
  968. NOTICE(rt2x00dev, "Waking up.\n");
  969. /*
  970. * Restore/enable extra components.
  971. */
  972. rt2x00debug_register(rt2x00dev);
  973. rt2x00leds_resume(rt2x00dev);
  974. /*
  975. * We are ready again to receive requests from mac80211.
  976. */
  977. set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  978. return 0;
  979. }
  980. EXPORT_SYMBOL_GPL(rt2x00lib_resume);
  981. #endif /* CONFIG_PM */
  982. /*
  983. * rt2x00lib module information.
  984. */
  985. MODULE_AUTHOR(DRV_PROJECT);
  986. MODULE_VERSION(DRV_VERSION);
  987. MODULE_DESCRIPTION("rt2x00 library");
  988. MODULE_LICENSE("GPL");