rt2x00dev.c 25 KB

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