rt2x00dev.c 26 KB

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