rt2x00mac.c 25 KB

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