rt2x00dev.c 27 KB

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