rt2x00mac.c 24 KB

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