rt2x00mac.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
  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_ring *ring,
  27. struct sk_buff *frag_skb,
  28. struct ieee80211_tx_control *control)
  29. {
  30. struct sk_buff *skb;
  31. int size;
  32. if (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
  33. size = sizeof(struct ieee80211_cts);
  34. else
  35. size = sizeof(struct ieee80211_rts);
  36. skb = dev_alloc_skb(size + rt2x00dev->hw->extra_tx_headroom);
  37. if (!skb) {
  38. WARNING(rt2x00dev, "Failed to create RTS/CTS frame.\n");
  39. return NETDEV_TX_BUSY;
  40. }
  41. skb_reserve(skb, rt2x00dev->hw->extra_tx_headroom);
  42. skb_put(skb, size);
  43. if (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
  44. ieee80211_ctstoself_get(rt2x00dev->hw, rt2x00dev->interface.id,
  45. frag_skb->data, frag_skb->len, control,
  46. (struct ieee80211_cts *)(skb->data));
  47. else
  48. ieee80211_rts_get(rt2x00dev->hw, rt2x00dev->interface.id,
  49. frag_skb->data, frag_skb->len, control,
  50. (struct ieee80211_rts *)(skb->data));
  51. if (rt2x00dev->ops->lib->write_tx_data(rt2x00dev, ring, skb, control)) {
  52. WARNING(rt2x00dev, "Failed to send RTS/CTS frame.\n");
  53. return NETDEV_TX_BUSY;
  54. }
  55. return NETDEV_TX_OK;
  56. }
  57. int rt2x00mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
  58. struct ieee80211_tx_control *control)
  59. {
  60. struct rt2x00_dev *rt2x00dev = hw->priv;
  61. struct ieee80211_hdr *ieee80211hdr = (struct ieee80211_hdr *)skb->data;
  62. struct data_ring *ring;
  63. u16 frame_control;
  64. /*
  65. * Mac80211 might be calling this function while we are trying
  66. * to remove the device or perhaps suspending it.
  67. * Note that we can only stop the TX queues inside the TX path
  68. * due to possible race conditions in mac80211.
  69. */
  70. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags)) {
  71. ieee80211_stop_queues(hw);
  72. return NETDEV_TX_OK;
  73. }
  74. /*
  75. * Determine which ring to put packet on.
  76. */
  77. ring = rt2x00lib_get_ring(rt2x00dev, control->queue);
  78. if (unlikely(!ring)) {
  79. ERROR(rt2x00dev,
  80. "Attempt to send packet over invalid queue %d.\n"
  81. "Please file bug report to %s.\n",
  82. control->queue, DRV_PROJECT);
  83. dev_kfree_skb_any(skb);
  84. return NETDEV_TX_OK;
  85. }
  86. /*
  87. * If CTS/RTS is required. and this frame is not CTS or RTS,
  88. * create and queue that frame first. But make sure we have
  89. * at least enough entries available to send this CTS/RTS
  90. * frame as well as the data frame.
  91. */
  92. frame_control = le16_to_cpu(ieee80211hdr->frame_control);
  93. if (!is_rts_frame(frame_control) && !is_cts_frame(frame_control) &&
  94. (control->flags & (IEEE80211_TXCTL_USE_RTS_CTS |
  95. IEEE80211_TXCTL_USE_CTS_PROTECT))) {
  96. if (rt2x00_ring_free(ring) <= 1) {
  97. ieee80211_stop_queue(rt2x00dev->hw, control->queue);
  98. return NETDEV_TX_BUSY;
  99. }
  100. if (rt2x00mac_tx_rts_cts(rt2x00dev, ring, skb, control)) {
  101. ieee80211_stop_queue(rt2x00dev->hw, control->queue);
  102. return NETDEV_TX_BUSY;
  103. }
  104. }
  105. if (rt2x00dev->ops->lib->write_tx_data(rt2x00dev, ring, skb, control)) {
  106. ieee80211_stop_queue(rt2x00dev->hw, control->queue);
  107. return NETDEV_TX_BUSY;
  108. }
  109. if (rt2x00_ring_full(ring))
  110. ieee80211_stop_queue(rt2x00dev->hw, control->queue);
  111. if (rt2x00dev->ops->lib->kick_tx_queue)
  112. rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, control->queue);
  113. return NETDEV_TX_OK;
  114. }
  115. EXPORT_SYMBOL_GPL(rt2x00mac_tx);
  116. int rt2x00mac_start(struct ieee80211_hw *hw)
  117. {
  118. struct rt2x00_dev *rt2x00dev = hw->priv;
  119. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
  120. return 0;
  121. return rt2x00lib_start(rt2x00dev);
  122. }
  123. EXPORT_SYMBOL_GPL(rt2x00mac_start);
  124. void rt2x00mac_stop(struct ieee80211_hw *hw)
  125. {
  126. struct rt2x00_dev *rt2x00dev = hw->priv;
  127. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
  128. return;
  129. rt2x00lib_stop(rt2x00dev);
  130. }
  131. EXPORT_SYMBOL_GPL(rt2x00mac_stop);
  132. int rt2x00mac_add_interface(struct ieee80211_hw *hw,
  133. struct ieee80211_if_init_conf *conf)
  134. {
  135. struct rt2x00_dev *rt2x00dev = hw->priv;
  136. struct interface *intf = &rt2x00dev->interface;
  137. /* FIXME: Beaconing is broken in rt2x00. */
  138. if (conf->type == IEEE80211_IF_TYPE_IBSS ||
  139. conf->type == IEEE80211_IF_TYPE_AP) {
  140. ERROR(rt2x00dev,
  141. "rt2x00 does not support Adhoc or Master mode");
  142. return -EOPNOTSUPP;
  143. }
  144. /*
  145. * Don't allow interfaces to be added while
  146. * either the device has disappeared or when
  147. * another interface is already present.
  148. */
  149. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags) ||
  150. is_interface_present(intf))
  151. return -ENOBUFS;
  152. intf->id = conf->vif;
  153. intf->type = conf->type;
  154. if (conf->type == IEEE80211_IF_TYPE_AP)
  155. memcpy(&intf->bssid, conf->mac_addr, ETH_ALEN);
  156. memcpy(&intf->mac, conf->mac_addr, ETH_ALEN);
  157. /*
  158. * The MAC adddress must be configured after the device
  159. * has been initialized. Otherwise the device can reset
  160. * the MAC registers.
  161. */
  162. rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
  163. rt2x00lib_config_type(rt2x00dev, conf->type);
  164. return 0;
  165. }
  166. EXPORT_SYMBOL_GPL(rt2x00mac_add_interface);
  167. void rt2x00mac_remove_interface(struct ieee80211_hw *hw,
  168. struct ieee80211_if_init_conf *conf)
  169. {
  170. struct rt2x00_dev *rt2x00dev = hw->priv;
  171. struct interface *intf = &rt2x00dev->interface;
  172. /*
  173. * Don't allow interfaces to be remove while
  174. * either the device has disappeared or when
  175. * no interface is present.
  176. */
  177. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags) ||
  178. !is_interface_present(intf))
  179. return;
  180. intf->id = 0;
  181. intf->type = IEEE80211_IF_TYPE_INVALID;
  182. memset(&intf->bssid, 0x00, ETH_ALEN);
  183. memset(&intf->mac, 0x00, ETH_ALEN);
  184. /*
  185. * Make sure the bssid and mac address registers
  186. * are cleared to prevent false ACKing of frames.
  187. */
  188. rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
  189. rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
  190. rt2x00lib_config_type(rt2x00dev, intf->type);
  191. }
  192. EXPORT_SYMBOL_GPL(rt2x00mac_remove_interface);
  193. int rt2x00mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
  194. {
  195. struct rt2x00_dev *rt2x00dev = hw->priv;
  196. /*
  197. * Mac80211 might be calling this function while we are trying
  198. * to remove the device or perhaps suspending it.
  199. */
  200. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
  201. return 0;
  202. /*
  203. * Check if we need to disable the radio,
  204. * if this is not the case, at least the RX must be disabled.
  205. */
  206. if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags)) {
  207. if (!conf->radio_enabled)
  208. rt2x00lib_disable_radio(rt2x00dev);
  209. else
  210. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
  211. }
  212. rt2x00lib_config(rt2x00dev, conf, 0);
  213. /*
  214. * Reenable RX only if the radio should be on.
  215. */
  216. if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  217. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
  218. else if (conf->radio_enabled)
  219. return rt2x00lib_enable_radio(rt2x00dev);
  220. return 0;
  221. }
  222. EXPORT_SYMBOL_GPL(rt2x00mac_config);
  223. int rt2x00mac_config_interface(struct ieee80211_hw *hw,
  224. struct ieee80211_vif *vif,
  225. struct ieee80211_if_conf *conf)
  226. {
  227. struct rt2x00_dev *rt2x00dev = hw->priv;
  228. struct interface *intf = &rt2x00dev->interface;
  229. int status;
  230. /*
  231. * Mac80211 might be calling this function while we are trying
  232. * to remove the device or perhaps suspending it.
  233. */
  234. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
  235. return 0;
  236. /*
  237. * If the given type does not match the configured type,
  238. * there has been a problem.
  239. */
  240. if (conf->type != intf->type)
  241. return -EINVAL;
  242. /*
  243. * If the interface does not work in master mode,
  244. * then the bssid value in the interface structure
  245. * should now be set.
  246. */
  247. if (conf->type != IEEE80211_IF_TYPE_AP)
  248. memcpy(&intf->bssid, conf->bssid, ETH_ALEN);
  249. rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
  250. /*
  251. * We only need to initialize the beacon when master mode is enabled.
  252. */
  253. if (conf->type != IEEE80211_IF_TYPE_AP || !conf->beacon)
  254. return 0;
  255. status = rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw,
  256. conf->beacon,
  257. conf->beacon_control);
  258. if (status)
  259. dev_kfree_skb(conf->beacon);
  260. return status;
  261. }
  262. EXPORT_SYMBOL_GPL(rt2x00mac_config_interface);
  263. int rt2x00mac_get_stats(struct ieee80211_hw *hw,
  264. struct ieee80211_low_level_stats *stats)
  265. {
  266. struct rt2x00_dev *rt2x00dev = hw->priv;
  267. /*
  268. * The dot11ACKFailureCount, dot11RTSFailureCount and
  269. * dot11RTSSuccessCount are updated in interrupt time.
  270. * dot11FCSErrorCount is updated in the link tuner.
  271. */
  272. memcpy(stats, &rt2x00dev->low_level_stats, sizeof(*stats));
  273. return 0;
  274. }
  275. EXPORT_SYMBOL_GPL(rt2x00mac_get_stats);
  276. int rt2x00mac_get_tx_stats(struct ieee80211_hw *hw,
  277. struct ieee80211_tx_queue_stats *stats)
  278. {
  279. struct rt2x00_dev *rt2x00dev = hw->priv;
  280. unsigned int i;
  281. for (i = 0; i < hw->queues; i++)
  282. memcpy(&stats->data[i], &rt2x00dev->tx[i].stats,
  283. sizeof(rt2x00dev->tx[i].stats));
  284. return 0;
  285. }
  286. EXPORT_SYMBOL_GPL(rt2x00mac_get_tx_stats);
  287. void rt2x00mac_bss_info_changed(struct ieee80211_hw *hw,
  288. struct ieee80211_vif *vif,
  289. struct ieee80211_bss_conf *bss_conf,
  290. u32 changes)
  291. {
  292. struct rt2x00_dev *rt2x00dev = hw->priv;
  293. int short_preamble;
  294. int ack_timeout;
  295. int ack_consume_time;
  296. int difs;
  297. int preamble;
  298. /*
  299. * We only support changing preamble mode.
  300. */
  301. if (!(changes & BSS_CHANGED_ERP_PREAMBLE))
  302. return;
  303. short_preamble = bss_conf->use_short_preamble;
  304. preamble = bss_conf->use_short_preamble ?
  305. SHORT_PREAMBLE : PREAMBLE;
  306. difs = (hw->conf.flags & IEEE80211_CONF_SHORT_SLOT_TIME) ?
  307. SHORT_DIFS : DIFS;
  308. ack_timeout = difs + PLCP + preamble + get_duration(ACK_SIZE, 10);
  309. ack_consume_time = SIFS + PLCP + preamble + get_duration(ACK_SIZE, 10);
  310. if (short_preamble)
  311. __set_bit(CONFIG_SHORT_PREAMBLE, &rt2x00dev->flags);
  312. else
  313. __clear_bit(CONFIG_SHORT_PREAMBLE, &rt2x00dev->flags);
  314. rt2x00dev->ops->lib->config_preamble(rt2x00dev, short_preamble,
  315. ack_timeout, ack_consume_time);
  316. }
  317. EXPORT_SYMBOL_GPL(rt2x00mac_bss_info_changed);
  318. int rt2x00mac_conf_tx(struct ieee80211_hw *hw, int queue,
  319. const struct ieee80211_tx_queue_params *params)
  320. {
  321. struct rt2x00_dev *rt2x00dev = hw->priv;
  322. struct data_ring *ring;
  323. ring = rt2x00lib_get_ring(rt2x00dev, queue);
  324. if (unlikely(!ring))
  325. return -EINVAL;
  326. /*
  327. * The passed variables are stored as real value ((2^n)-1).
  328. * Ralink registers require to know the bit number 'n'.
  329. */
  330. if (params->cw_min)
  331. ring->tx_params.cw_min = fls(params->cw_min);
  332. else
  333. ring->tx_params.cw_min = 5; /* cw_min: 2^5 = 32. */
  334. if (params->cw_max)
  335. ring->tx_params.cw_max = fls(params->cw_max);
  336. else
  337. ring->tx_params.cw_max = 10; /* cw_min: 2^10 = 1024. */
  338. if (params->aifs)
  339. ring->tx_params.aifs = params->aifs;
  340. else
  341. ring->tx_params.aifs = 2;
  342. INFO(rt2x00dev,
  343. "Configured TX ring %d - CWmin: %d, CWmax: %d, Aifs: %d.\n",
  344. queue, ring->tx_params.cw_min, ring->tx_params.cw_max,
  345. ring->tx_params.aifs);
  346. return 0;
  347. }
  348. EXPORT_SYMBOL_GPL(rt2x00mac_conf_tx);