rt2x00dev.c 27 KB

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