rt2x00mac.c 23 KB

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