rt2x00mac.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. Copyright (C) 2004 - 2009 Ivo van Doorn <[email protected]>
  4. <http://rt2x00.serialmonkey.com>
  5. */
  6. /*
  7. Module: rt2x00mac
  8. Abstract: rt2x00 generic mac80211 routines.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include "rt2x00.h"
  13. #include "rt2x00lib.h"
  14. static int rt2x00mac_tx_rts_cts(struct rt2x00_dev *rt2x00dev,
  15. struct data_queue *queue,
  16. struct sk_buff *frag_skb)
  17. {
  18. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(frag_skb);
  19. struct ieee80211_tx_info *rts_info;
  20. struct sk_buff *skb;
  21. unsigned int data_length;
  22. int retval = 0;
  23. if (tx_info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
  24. data_length = sizeof(struct ieee80211_cts);
  25. else
  26. data_length = sizeof(struct ieee80211_rts);
  27. skb = dev_alloc_skb(data_length + rt2x00dev->hw->extra_tx_headroom);
  28. if (unlikely(!skb)) {
  29. rt2x00_warn(rt2x00dev, "Failed to create RTS/CTS frame\n");
  30. return -ENOMEM;
  31. }
  32. skb_reserve(skb, rt2x00dev->hw->extra_tx_headroom);
  33. skb_put(skb, data_length);
  34. /*
  35. * Copy TX information over from original frame to
  36. * RTS/CTS frame. Note that we set the no encryption flag
  37. * since we don't want this frame to be encrypted.
  38. * RTS frames should be acked, while CTS-to-self frames
  39. * should not. The ready for TX flag is cleared to prevent
  40. * it being automatically send when the descriptor is
  41. * written to the hardware.
  42. */
  43. memcpy(skb->cb, frag_skb->cb, sizeof(skb->cb));
  44. rts_info = IEEE80211_SKB_CB(skb);
  45. rts_info->control.rates[0].flags &= ~IEEE80211_TX_RC_USE_RTS_CTS;
  46. rts_info->control.rates[0].flags &= ~IEEE80211_TX_RC_USE_CTS_PROTECT;
  47. if (tx_info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
  48. rts_info->flags |= IEEE80211_TX_CTL_NO_ACK;
  49. else
  50. rts_info->flags &= ~IEEE80211_TX_CTL_NO_ACK;
  51. /* Disable hardware encryption */
  52. rts_info->control.hw_key = NULL;
  53. /*
  54. * RTS/CTS frame should use the length of the frame plus any
  55. * encryption overhead that will be added by the hardware.
  56. */
  57. data_length += rt2x00crypto_tx_overhead(rt2x00dev, skb);
  58. if (tx_info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
  59. ieee80211_ctstoself_get(rt2x00dev->hw, tx_info->control.vif,
  60. frag_skb->data, data_length, tx_info,
  61. (struct ieee80211_cts *)(skb->data));
  62. else
  63. ieee80211_rts_get(rt2x00dev->hw, tx_info->control.vif,
  64. frag_skb->data, data_length, tx_info,
  65. (struct ieee80211_rts *)(skb->data));
  66. retval = rt2x00queue_write_tx_frame(queue, skb, NULL, true);
  67. if (retval) {
  68. dev_kfree_skb_any(skb);
  69. rt2x00_warn(rt2x00dev, "Failed to send RTS/CTS frame\n");
  70. }
  71. return retval;
  72. }
  73. void rt2x00mac_tx(struct ieee80211_hw *hw,
  74. struct ieee80211_tx_control *control,
  75. struct sk_buff *skb)
  76. {
  77. struct rt2x00_dev *rt2x00dev = hw->priv;
  78. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  79. enum data_queue_qid qid = skb_get_queue_mapping(skb);
  80. struct data_queue *queue = NULL;
  81. /*
  82. * Mac80211 might be calling this function while we are trying
  83. * to remove the device or perhaps suspending it.
  84. * Note that we can only stop the TX queues inside the TX path
  85. * due to possible race conditions in mac80211.
  86. */
  87. if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  88. goto exit_free_skb;
  89. /*
  90. * Use the ATIM queue if appropriate and present.
  91. */
  92. if (tx_info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM &&
  93. rt2x00_has_cap_flag(rt2x00dev, REQUIRE_ATIM_QUEUE))
  94. qid = QID_ATIM;
  95. queue = rt2x00queue_get_tx_queue(rt2x00dev, qid);
  96. if (unlikely(!queue)) {
  97. rt2x00_err(rt2x00dev,
  98. "Attempt to send packet over invalid queue %d\n"
  99. "Please file bug report to %s\n", qid, DRV_PROJECT);
  100. goto exit_free_skb;
  101. }
  102. /*
  103. * If CTS/RTS is required. create and queue that frame first.
  104. * Make sure we have at least enough entries available to send
  105. * this CTS/RTS frame as well as the data frame.
  106. * Note that when the driver has set the set_rts_threshold()
  107. * callback function it doesn't need software generation of
  108. * either RTS or CTS-to-self frame and handles everything
  109. * inside the hardware.
  110. */
  111. if (!rt2x00dev->ops->hw->set_rts_threshold &&
  112. (tx_info->control.rates[0].flags & (IEEE80211_TX_RC_USE_RTS_CTS |
  113. IEEE80211_TX_RC_USE_CTS_PROTECT))) {
  114. if (rt2x00queue_available(queue) <= 1) {
  115. /*
  116. * Recheck for full queue under lock to avoid race
  117. * conditions with rt2x00lib_txdone().
  118. */
  119. spin_lock(&queue->tx_lock);
  120. if (rt2x00queue_threshold(queue))
  121. rt2x00queue_pause_queue(queue);
  122. spin_unlock(&queue->tx_lock);
  123. goto exit_free_skb;
  124. }
  125. if (rt2x00mac_tx_rts_cts(rt2x00dev, queue, skb))
  126. goto exit_free_skb;
  127. }
  128. if (unlikely(rt2x00queue_write_tx_frame(queue, skb, control->sta, false)))
  129. goto exit_free_skb;
  130. return;
  131. exit_free_skb:
  132. ieee80211_free_txskb(hw, skb);
  133. }
  134. EXPORT_SYMBOL_GPL(rt2x00mac_tx);
  135. int rt2x00mac_start(struct ieee80211_hw *hw)
  136. {
  137. struct rt2x00_dev *rt2x00dev = hw->priv;
  138. if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  139. return 0;
  140. if (test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags)) {
  141. /*
  142. * This is special case for ieee80211_restart_hw(), otherwise
  143. * mac80211 never call start() two times in row without stop();
  144. */
  145. set_bit(DEVICE_STATE_RESET, &rt2x00dev->flags);
  146. rt2x00dev->ops->lib->pre_reset_hw(rt2x00dev);
  147. rt2x00lib_stop(rt2x00dev);
  148. }
  149. return rt2x00lib_start(rt2x00dev);
  150. }
  151. EXPORT_SYMBOL_GPL(rt2x00mac_start);
  152. void rt2x00mac_stop(struct ieee80211_hw *hw)
  153. {
  154. struct rt2x00_dev *rt2x00dev = hw->priv;
  155. if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  156. return;
  157. rt2x00lib_stop(rt2x00dev);
  158. }
  159. EXPORT_SYMBOL_GPL(rt2x00mac_stop);
  160. void
  161. rt2x00mac_reconfig_complete(struct ieee80211_hw *hw,
  162. enum ieee80211_reconfig_type reconfig_type)
  163. {
  164. struct rt2x00_dev *rt2x00dev = hw->priv;
  165. if (reconfig_type == IEEE80211_RECONFIG_TYPE_RESTART)
  166. clear_bit(DEVICE_STATE_RESET, &rt2x00dev->flags);
  167. }
  168. EXPORT_SYMBOL_GPL(rt2x00mac_reconfig_complete);
  169. int rt2x00mac_add_interface(struct ieee80211_hw *hw,
  170. struct ieee80211_vif *vif)
  171. {
  172. struct rt2x00_dev *rt2x00dev = hw->priv;
  173. struct rt2x00_intf *intf = vif_to_intf(vif);
  174. struct data_queue *queue = rt2x00dev->bcn;
  175. struct queue_entry *entry = NULL;
  176. unsigned int i;
  177. /*
  178. * Don't allow interfaces to be added
  179. * the device has disappeared.
  180. */
  181. if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) ||
  182. !test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
  183. return -ENODEV;
  184. /*
  185. * Loop through all beacon queues to find a free
  186. * entry. Since there are as much beacon entries
  187. * as the maximum interfaces, this search shouldn't
  188. * fail.
  189. */
  190. for (i = 0; i < queue->limit; i++) {
  191. entry = &queue->entries[i];
  192. if (!test_and_set_bit(ENTRY_BCN_ASSIGNED, &entry->flags))
  193. break;
  194. }
  195. if (unlikely(i == queue->limit))
  196. return -ENOBUFS;
  197. /*
  198. * We are now absolutely sure the interface can be created,
  199. * increase interface count and start initialization.
  200. */
  201. if (vif->type == NL80211_IFTYPE_AP)
  202. rt2x00dev->intf_ap_count++;
  203. else
  204. rt2x00dev->intf_sta_count++;
  205. mutex_init(&intf->beacon_skb_mutex);
  206. intf->beacon = entry;
  207. /*
  208. * The MAC address must be configured after the device
  209. * has been initialized. Otherwise the device can reset
  210. * the MAC registers.
  211. * The BSSID address must only be configured in AP mode,
  212. * however we should not send an empty BSSID address for
  213. * STA interfaces at this time, since this can cause
  214. * invalid behavior in the device.
  215. */
  216. rt2x00lib_config_intf(rt2x00dev, intf, vif->type,
  217. vif->addr, NULL);
  218. /*
  219. * Some filters depend on the current working mode. We can force
  220. * an update during the next configure_filter() run by mac80211 by
  221. * resetting the current packet_filter state.
  222. */
  223. rt2x00dev->packet_filter = 0;
  224. return 0;
  225. }
  226. EXPORT_SYMBOL_GPL(rt2x00mac_add_interface);
  227. void rt2x00mac_remove_interface(struct ieee80211_hw *hw,
  228. struct ieee80211_vif *vif)
  229. {
  230. struct rt2x00_dev *rt2x00dev = hw->priv;
  231. struct rt2x00_intf *intf = vif_to_intf(vif);
  232. /*
  233. * Don't allow interfaces to be remove while
  234. * either the device has disappeared or when
  235. * no interface is present.
  236. */
  237. if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) ||
  238. (vif->type == NL80211_IFTYPE_AP && !rt2x00dev->intf_ap_count) ||
  239. (vif->type != NL80211_IFTYPE_AP && !rt2x00dev->intf_sta_count))
  240. return;
  241. if (vif->type == NL80211_IFTYPE_AP)
  242. rt2x00dev->intf_ap_count--;
  243. else
  244. rt2x00dev->intf_sta_count--;
  245. /*
  246. * Release beacon entry so it is available for
  247. * new interfaces again.
  248. */
  249. clear_bit(ENTRY_BCN_ASSIGNED, &intf->beacon->flags);
  250. /*
  251. * Make sure the bssid and mac address registers
  252. * are cleared to prevent false ACKing of frames.
  253. */
  254. rt2x00lib_config_intf(rt2x00dev, intf,
  255. NL80211_IFTYPE_UNSPECIFIED, NULL, NULL);
  256. }
  257. EXPORT_SYMBOL_GPL(rt2x00mac_remove_interface);
  258. int rt2x00mac_config(struct ieee80211_hw *hw, u32 changed)
  259. {
  260. struct rt2x00_dev *rt2x00dev = hw->priv;
  261. struct ieee80211_conf *conf = &hw->conf;
  262. /*
  263. * mac80211 might be calling this function while we are trying
  264. * to remove the device or perhaps suspending it.
  265. */
  266. if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  267. return 0;
  268. /*
  269. * Some configuration parameters (e.g. channel and antenna values) can
  270. * only be set when the radio is enabled, but do require the RX to
  271. * be off. During this period we should keep link tuning enabled,
  272. * if for any reason the link tuner must be reset, this will be
  273. * handled by rt2x00lib_config().
  274. */
  275. rt2x00queue_stop_queue(rt2x00dev->rx);
  276. /* Do not race with link tuner. */
  277. mutex_lock(&rt2x00dev->conf_mutex);
  278. /*
  279. * When we've just turned on the radio, we want to reprogram
  280. * everything to ensure a consistent state
  281. */
  282. rt2x00lib_config(rt2x00dev, conf, changed);
  283. /*
  284. * After the radio has been enabled we need to configure
  285. * the antenna to the default settings. rt2x00lib_config_antenna()
  286. * should determine if any action should be taken based on
  287. * checking if diversity has been enabled or no antenna changes
  288. * have been made since the last configuration change.
  289. */
  290. rt2x00lib_config_antenna(rt2x00dev, rt2x00dev->default_ant);
  291. mutex_unlock(&rt2x00dev->conf_mutex);
  292. /* Turn RX back on */
  293. rt2x00queue_start_queue(rt2x00dev->rx);
  294. return 0;
  295. }
  296. EXPORT_SYMBOL_GPL(rt2x00mac_config);
  297. void rt2x00mac_configure_filter(struct ieee80211_hw *hw,
  298. unsigned int changed_flags,
  299. unsigned int *total_flags,
  300. u64 multicast)
  301. {
  302. struct rt2x00_dev *rt2x00dev = hw->priv;
  303. /*
  304. * Mask off any flags we are going to ignore
  305. * from the total_flags field.
  306. */
  307. *total_flags &=
  308. FIF_ALLMULTI |
  309. FIF_FCSFAIL |
  310. FIF_PLCPFAIL |
  311. FIF_CONTROL |
  312. FIF_PSPOLL |
  313. FIF_OTHER_BSS;
  314. /*
  315. * Apply some rules to the filters:
  316. * - Some filters imply different filters to be set.
  317. * - Some things we can't filter out at all.
  318. * - Multicast filter seems to kill broadcast traffic so never use it.
  319. */
  320. *total_flags |= FIF_ALLMULTI;
  321. /*
  322. * If the device has a single filter for all control frames,
  323. * FIF_CONTROL and FIF_PSPOLL flags imply each other.
  324. * And if the device has more than one filter for control frames
  325. * of different types, but has no a separate filter for PS Poll frames,
  326. * FIF_CONTROL flag implies FIF_PSPOLL.
  327. */
  328. if (!rt2x00_has_cap_control_filters(rt2x00dev)) {
  329. if (*total_flags & FIF_CONTROL || *total_flags & FIF_PSPOLL)
  330. *total_flags |= FIF_CONTROL | FIF_PSPOLL;
  331. }
  332. if (!rt2x00_has_cap_control_filter_pspoll(rt2x00dev)) {
  333. if (*total_flags & FIF_CONTROL)
  334. *total_flags |= FIF_PSPOLL;
  335. }
  336. rt2x00dev->packet_filter = *total_flags;
  337. rt2x00dev->ops->lib->config_filter(rt2x00dev, *total_flags);
  338. }
  339. EXPORT_SYMBOL_GPL(rt2x00mac_configure_filter);
  340. static void rt2x00mac_set_tim_iter(void *data, u8 *mac,
  341. struct ieee80211_vif *vif)
  342. {
  343. struct rt2x00_intf *intf = vif_to_intf(vif);
  344. if (vif->type != NL80211_IFTYPE_AP &&
  345. vif->type != NL80211_IFTYPE_ADHOC &&
  346. vif->type != NL80211_IFTYPE_MESH_POINT)
  347. return;
  348. set_bit(DELAYED_UPDATE_BEACON, &intf->delayed_flags);
  349. }
  350. int rt2x00mac_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
  351. bool set)
  352. {
  353. struct rt2x00_dev *rt2x00dev = hw->priv;
  354. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  355. return 0;
  356. ieee80211_iterate_active_interfaces_atomic(
  357. rt2x00dev->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  358. rt2x00mac_set_tim_iter, rt2x00dev);
  359. /* queue work to upodate the beacon template */
  360. ieee80211_queue_work(rt2x00dev->hw, &rt2x00dev->intf_work);
  361. return 0;
  362. }
  363. EXPORT_SYMBOL_GPL(rt2x00mac_set_tim);
  364. #ifdef CONFIG_RT2X00_LIB_CRYPTO
  365. static void memcpy_tkip(struct rt2x00lib_crypto *crypto, u8 *key, u8 key_len)
  366. {
  367. if (key_len > NL80211_TKIP_DATA_OFFSET_ENCR_KEY)
  368. memcpy(crypto->key,
  369. &key[NL80211_TKIP_DATA_OFFSET_ENCR_KEY],
  370. sizeof(crypto->key));
  371. if (key_len > NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY)
  372. memcpy(crypto->tx_mic,
  373. &key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
  374. sizeof(crypto->tx_mic));
  375. if (key_len > NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY)
  376. memcpy(crypto->rx_mic,
  377. &key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
  378. sizeof(crypto->rx_mic));
  379. }
  380. int rt2x00mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  381. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  382. struct ieee80211_key_conf *key)
  383. {
  384. struct rt2x00_dev *rt2x00dev = hw->priv;
  385. int (*set_key) (struct rt2x00_dev *rt2x00dev,
  386. struct rt2x00lib_crypto *crypto,
  387. struct ieee80211_key_conf *key);
  388. struct rt2x00lib_crypto crypto;
  389. static const u8 bcast_addr[ETH_ALEN] =
  390. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, };
  391. struct rt2x00_sta *sta_priv = NULL;
  392. if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  393. return 0;
  394. /* The hardware can't do MFP */
  395. if (!rt2x00_has_cap_hw_crypto(rt2x00dev) || (sta && sta->mfp))
  396. return -EOPNOTSUPP;
  397. /*
  398. * To support IBSS RSN, don't program group keys in IBSS, the
  399. * hardware will then not attempt to decrypt the frames.
  400. */
  401. if (vif->type == NL80211_IFTYPE_ADHOC &&
  402. !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  403. return -EOPNOTSUPP;
  404. if (key->keylen > 32)
  405. return -ENOSPC;
  406. memset(&crypto, 0, sizeof(crypto));
  407. crypto.bssidx = rt2x00lib_get_bssidx(rt2x00dev, vif);
  408. crypto.cipher = rt2x00crypto_key_to_cipher(key);
  409. if (crypto.cipher == CIPHER_NONE)
  410. return -EOPNOTSUPP;
  411. if (crypto.cipher == CIPHER_TKIP && rt2x00_is_usb(rt2x00dev))
  412. return -EOPNOTSUPP;
  413. crypto.cmd = cmd;
  414. if (sta) {
  415. crypto.address = sta->addr;
  416. sta_priv = sta_to_rt2x00_sta(sta);
  417. crypto.wcid = sta_priv->wcid;
  418. } else
  419. crypto.address = bcast_addr;
  420. if (crypto.cipher == CIPHER_TKIP)
  421. memcpy_tkip(&crypto, &key->key[0], key->keylen);
  422. else
  423. memcpy(crypto.key, &key->key[0], key->keylen);
  424. /*
  425. * Each BSS has a maximum of 4 shared keys.
  426. * Shared key index values:
  427. * 0) BSS0 key0
  428. * 1) BSS0 key1
  429. * ...
  430. * 4) BSS1 key0
  431. * ...
  432. * 8) BSS2 key0
  433. * ...
  434. * Both pairwise as shared key indeces are determined by
  435. * driver. This is required because the hardware requires
  436. * keys to be assigned in correct order (When key 1 is
  437. * provided but key 0 is not, then the key is not found
  438. * by the hardware during RX).
  439. */
  440. if (cmd == SET_KEY)
  441. key->hw_key_idx = 0;
  442. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  443. set_key = rt2x00dev->ops->lib->config_pairwise_key;
  444. else
  445. set_key = rt2x00dev->ops->lib->config_shared_key;
  446. if (!set_key)
  447. return -EOPNOTSUPP;
  448. return set_key(rt2x00dev, &crypto, key);
  449. }
  450. EXPORT_SYMBOL_GPL(rt2x00mac_set_key);
  451. #endif /* CONFIG_RT2X00_LIB_CRYPTO */
  452. void rt2x00mac_sw_scan_start(struct ieee80211_hw *hw,
  453. struct ieee80211_vif *vif,
  454. const u8 *mac_addr)
  455. {
  456. struct rt2x00_dev *rt2x00dev = hw->priv;
  457. set_bit(DEVICE_STATE_SCANNING, &rt2x00dev->flags);
  458. rt2x00link_stop_tuner(rt2x00dev);
  459. }
  460. EXPORT_SYMBOL_GPL(rt2x00mac_sw_scan_start);
  461. void rt2x00mac_sw_scan_complete(struct ieee80211_hw *hw,
  462. struct ieee80211_vif *vif)
  463. {
  464. struct rt2x00_dev *rt2x00dev = hw->priv;
  465. clear_bit(DEVICE_STATE_SCANNING, &rt2x00dev->flags);
  466. rt2x00link_start_tuner(rt2x00dev);
  467. }
  468. EXPORT_SYMBOL_GPL(rt2x00mac_sw_scan_complete);
  469. int rt2x00mac_get_stats(struct ieee80211_hw *hw,
  470. struct ieee80211_low_level_stats *stats)
  471. {
  472. struct rt2x00_dev *rt2x00dev = hw->priv;
  473. /*
  474. * The dot11ACKFailureCount, dot11RTSFailureCount and
  475. * dot11RTSSuccessCount are updated in interrupt time.
  476. * dot11FCSErrorCount is updated in the link tuner.
  477. */
  478. memcpy(stats, &rt2x00dev->low_level_stats, sizeof(*stats));
  479. return 0;
  480. }
  481. EXPORT_SYMBOL_GPL(rt2x00mac_get_stats);
  482. void rt2x00mac_bss_info_changed(struct ieee80211_hw *hw,
  483. struct ieee80211_vif *vif,
  484. struct ieee80211_bss_conf *bss_conf,
  485. u64 changes)
  486. {
  487. struct rt2x00_dev *rt2x00dev = hw->priv;
  488. struct rt2x00_intf *intf = vif_to_intf(vif);
  489. /*
  490. * mac80211 might be calling this function while we are trying
  491. * to remove the device or perhaps suspending it.
  492. */
  493. if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  494. return;
  495. /*
  496. * Update the BSSID.
  497. */
  498. if (changes & BSS_CHANGED_BSSID)
  499. rt2x00lib_config_intf(rt2x00dev, intf, vif->type, NULL,
  500. bss_conf->bssid);
  501. /*
  502. * Start/stop beaconing.
  503. */
  504. if (changes & BSS_CHANGED_BEACON_ENABLED) {
  505. mutex_lock(&intf->beacon_skb_mutex);
  506. if (!bss_conf->enable_beacon && intf->enable_beacon) {
  507. rt2x00dev->intf_beaconing--;
  508. intf->enable_beacon = false;
  509. if (rt2x00dev->intf_beaconing == 0) {
  510. /*
  511. * Last beaconing interface disabled
  512. * -> stop beacon queue.
  513. */
  514. rt2x00queue_stop_queue(rt2x00dev->bcn);
  515. }
  516. /*
  517. * Clear beacon in the H/W for this vif. This is needed
  518. * to disable beaconing on this particular interface
  519. * and keep it running on other interfaces.
  520. */
  521. rt2x00queue_clear_beacon(rt2x00dev, vif);
  522. } else if (bss_conf->enable_beacon && !intf->enable_beacon) {
  523. rt2x00dev->intf_beaconing++;
  524. intf->enable_beacon = true;
  525. /*
  526. * Upload beacon to the H/W. This is only required on
  527. * USB devices. PCI devices fetch beacons periodically.
  528. */
  529. if (rt2x00_is_usb(rt2x00dev))
  530. rt2x00queue_update_beacon(rt2x00dev, vif);
  531. if (rt2x00dev->intf_beaconing == 1) {
  532. /*
  533. * First beaconing interface enabled
  534. * -> start beacon queue.
  535. */
  536. rt2x00queue_start_queue(rt2x00dev->bcn);
  537. }
  538. }
  539. mutex_unlock(&intf->beacon_skb_mutex);
  540. }
  541. /*
  542. * When the association status has changed we must reset the link
  543. * tuner counter. This is because some drivers determine if they
  544. * should perform link tuning based on the number of seconds
  545. * while associated or not associated.
  546. */
  547. if (changes & BSS_CHANGED_ASSOC) {
  548. rt2x00dev->link.count = 0;
  549. if (vif->cfg.assoc)
  550. rt2x00dev->intf_associated++;
  551. else
  552. rt2x00dev->intf_associated--;
  553. rt2x00leds_led_assoc(rt2x00dev, !!rt2x00dev->intf_associated);
  554. }
  555. /*
  556. * When the erp information has changed, we should perform
  557. * additional configuration steps. For all other changes we are done.
  558. */
  559. if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_ERP_PREAMBLE |
  560. BSS_CHANGED_ERP_SLOT | BSS_CHANGED_BASIC_RATES |
  561. BSS_CHANGED_BEACON_INT | BSS_CHANGED_HT))
  562. rt2x00lib_config_erp(rt2x00dev, intf, bss_conf, changes);
  563. }
  564. EXPORT_SYMBOL_GPL(rt2x00mac_bss_info_changed);
  565. int rt2x00mac_conf_tx(struct ieee80211_hw *hw,
  566. struct ieee80211_vif *vif,
  567. unsigned int link_id, u16 queue_idx,
  568. const struct ieee80211_tx_queue_params *params)
  569. {
  570. struct rt2x00_dev *rt2x00dev = hw->priv;
  571. struct data_queue *queue;
  572. queue = rt2x00queue_get_tx_queue(rt2x00dev, queue_idx);
  573. if (unlikely(!queue))
  574. return -EINVAL;
  575. /*
  576. * The passed variables are stored as real value ((2^n)-1).
  577. * Ralink registers require to know the bit number 'n'.
  578. */
  579. if (params->cw_min > 0)
  580. queue->cw_min = fls(params->cw_min);
  581. else
  582. queue->cw_min = 5; /* cw_min: 2^5 = 32. */
  583. if (params->cw_max > 0)
  584. queue->cw_max = fls(params->cw_max);
  585. else
  586. queue->cw_max = 10; /* cw_min: 2^10 = 1024. */
  587. queue->aifs = params->aifs;
  588. queue->txop = params->txop;
  589. rt2x00_dbg(rt2x00dev,
  590. "Configured TX queue %d - CWmin: %d, CWmax: %d, Aifs: %d, TXop: %d\n",
  591. queue_idx, queue->cw_min, queue->cw_max, queue->aifs,
  592. queue->txop);
  593. return 0;
  594. }
  595. EXPORT_SYMBOL_GPL(rt2x00mac_conf_tx);
  596. void rt2x00mac_rfkill_poll(struct ieee80211_hw *hw)
  597. {
  598. struct rt2x00_dev *rt2x00dev = hw->priv;
  599. bool active = !!rt2x00dev->ops->lib->rfkill_poll(rt2x00dev);
  600. wiphy_rfkill_set_hw_state(hw->wiphy, !active);
  601. }
  602. EXPORT_SYMBOL_GPL(rt2x00mac_rfkill_poll);
  603. void rt2x00mac_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  604. u32 queues, bool drop)
  605. {
  606. struct rt2x00_dev *rt2x00dev = hw->priv;
  607. struct data_queue *queue;
  608. if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  609. return;
  610. set_bit(DEVICE_STATE_FLUSHING, &rt2x00dev->flags);
  611. tx_queue_for_each(rt2x00dev, queue)
  612. rt2x00queue_flush_queue(queue, drop);
  613. clear_bit(DEVICE_STATE_FLUSHING, &rt2x00dev->flags);
  614. }
  615. EXPORT_SYMBOL_GPL(rt2x00mac_flush);
  616. int rt2x00mac_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
  617. {
  618. struct rt2x00_dev *rt2x00dev = hw->priv;
  619. struct link_ant *ant = &rt2x00dev->link.ant;
  620. struct antenna_setup *def = &rt2x00dev->default_ant;
  621. struct antenna_setup setup;
  622. // The antenna value is not supposed to be 0,
  623. // or exceed the maximum number of antenna's.
  624. if (!tx_ant || (tx_ant & ~3) || !rx_ant || (rx_ant & ~3))
  625. return -EINVAL;
  626. // When the client tried to configure the antenna to or from
  627. // diversity mode, we must reset the default antenna as well
  628. // as that controls the diversity switch.
  629. if (ant->flags & ANTENNA_TX_DIVERSITY && tx_ant != 3)
  630. ant->flags &= ~ANTENNA_TX_DIVERSITY;
  631. if (ant->flags & ANTENNA_RX_DIVERSITY && rx_ant != 3)
  632. ant->flags &= ~ANTENNA_RX_DIVERSITY;
  633. // If diversity is being enabled, check if we need hardware
  634. // or software diversity. In the latter case, reset the value,
  635. // and make sure we update the antenna flags to have the
  636. // link tuner pick up the diversity tuning.
  637. if (tx_ant == 3 && def->tx == ANTENNA_SW_DIVERSITY) {
  638. tx_ant = ANTENNA_SW_DIVERSITY;
  639. ant->flags |= ANTENNA_TX_DIVERSITY;
  640. }
  641. if (rx_ant == 3 && def->rx == ANTENNA_SW_DIVERSITY) {
  642. rx_ant = ANTENNA_SW_DIVERSITY;
  643. ant->flags |= ANTENNA_RX_DIVERSITY;
  644. }
  645. setup.tx = tx_ant;
  646. setup.rx = rx_ant;
  647. setup.rx_chain_num = 0;
  648. setup.tx_chain_num = 0;
  649. rt2x00lib_config_antenna(rt2x00dev, setup);
  650. return 0;
  651. }
  652. EXPORT_SYMBOL_GPL(rt2x00mac_set_antenna);
  653. int rt2x00mac_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
  654. {
  655. struct rt2x00_dev *rt2x00dev = hw->priv;
  656. struct link_ant *ant = &rt2x00dev->link.ant;
  657. struct antenna_setup *active = &rt2x00dev->link.ant.active;
  658. // When software diversity is active, we must report this to the
  659. // client and not the current active antenna state.
  660. if (ant->flags & ANTENNA_TX_DIVERSITY)
  661. *tx_ant = ANTENNA_HW_DIVERSITY;
  662. else
  663. *tx_ant = active->tx;
  664. if (ant->flags & ANTENNA_RX_DIVERSITY)
  665. *rx_ant = ANTENNA_HW_DIVERSITY;
  666. else
  667. *rx_ant = active->rx;
  668. return 0;
  669. }
  670. EXPORT_SYMBOL_GPL(rt2x00mac_get_antenna);
  671. void rt2x00mac_get_ringparam(struct ieee80211_hw *hw,
  672. u32 *tx, u32 *tx_max, u32 *rx, u32 *rx_max)
  673. {
  674. struct rt2x00_dev *rt2x00dev = hw->priv;
  675. struct data_queue *queue;
  676. tx_queue_for_each(rt2x00dev, queue) {
  677. *tx += queue->length;
  678. *tx_max += queue->limit;
  679. }
  680. *rx = rt2x00dev->rx->length;
  681. *rx_max = rt2x00dev->rx->limit;
  682. }
  683. EXPORT_SYMBOL_GPL(rt2x00mac_get_ringparam);
  684. bool rt2x00mac_tx_frames_pending(struct ieee80211_hw *hw)
  685. {
  686. struct rt2x00_dev *rt2x00dev = hw->priv;
  687. struct data_queue *queue;
  688. tx_queue_for_each(rt2x00dev, queue) {
  689. if (!rt2x00queue_empty(queue))
  690. return true;
  691. }
  692. return false;
  693. }
  694. EXPORT_SYMBOL_GPL(rt2x00mac_tx_frames_pending);