rt2x00mac.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. /*
  22. * Set enviroment defines for rt2x00.h
  23. */
  24. #define DRV_NAME "rt2x00lib"
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include "rt2x00.h"
  28. #include "rt2x00lib.h"
  29. static int rt2x00mac_tx_rts_cts(struct rt2x00_dev *rt2x00dev,
  30. struct data_ring *ring,
  31. struct sk_buff *frag_skb,
  32. struct ieee80211_tx_control *control)
  33. {
  34. struct sk_buff *skb;
  35. int size;
  36. if (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
  37. size = sizeof(struct ieee80211_cts);
  38. else
  39. size = sizeof(struct ieee80211_rts);
  40. skb = dev_alloc_skb(size + rt2x00dev->hw->extra_tx_headroom);
  41. if (!skb) {
  42. WARNING(rt2x00dev, "Failed to create RTS/CTS frame.\n");
  43. return NETDEV_TX_BUSY;
  44. }
  45. skb_reserve(skb, rt2x00dev->hw->extra_tx_headroom);
  46. skb_put(skb, size);
  47. if (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
  48. ieee80211_ctstoself_get(rt2x00dev->hw, rt2x00dev->interface.id,
  49. frag_skb->data, frag_skb->len, control,
  50. (struct ieee80211_cts *)(skb->data));
  51. else
  52. ieee80211_rts_get(rt2x00dev->hw, rt2x00dev->interface.id,
  53. frag_skb->data, frag_skb->len, control,
  54. (struct ieee80211_rts *)(skb->data));
  55. if (rt2x00dev->ops->lib->write_tx_data(rt2x00dev, ring, skb, control)) {
  56. WARNING(rt2x00dev, "Failed to send RTS/CTS frame.\n");
  57. return NETDEV_TX_BUSY;
  58. }
  59. return NETDEV_TX_OK;
  60. }
  61. int rt2x00mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
  62. struct ieee80211_tx_control *control)
  63. {
  64. struct rt2x00_dev *rt2x00dev = hw->priv;
  65. struct ieee80211_hdr *ieee80211hdr = (struct ieee80211_hdr *)skb->data;
  66. struct data_ring *ring;
  67. u16 frame_control;
  68. /*
  69. * Determine which ring to put packet on.
  70. */
  71. ring = rt2x00lib_get_ring(rt2x00dev, control->queue);
  72. if (unlikely(!ring)) {
  73. ERROR(rt2x00dev,
  74. "Attempt to send packet over invalid queue %d.\n"
  75. "Please file bug report to %s.\n",
  76. control->queue, DRV_PROJECT);
  77. dev_kfree_skb_any(skb);
  78. return NETDEV_TX_OK;
  79. }
  80. /*
  81. * If CTS/RTS is required. and this frame is not CTS or RTS,
  82. * create and queue that frame first. But make sure we have
  83. * at least enough entries available to send this CTS/RTS
  84. * frame as well as the data frame.
  85. */
  86. frame_control = le16_to_cpu(ieee80211hdr->frame_control);
  87. if (!is_rts_frame(frame_control) && !is_cts_frame(frame_control) &&
  88. (control->flags & (IEEE80211_TXCTL_USE_RTS_CTS |
  89. IEEE80211_TXCTL_USE_CTS_PROTECT))) {
  90. if (rt2x00_ring_free(ring) <= 1)
  91. return NETDEV_TX_BUSY;
  92. if (rt2x00mac_tx_rts_cts(rt2x00dev, ring, skb, control))
  93. return NETDEV_TX_BUSY;
  94. }
  95. if (rt2x00dev->ops->lib->write_tx_data(rt2x00dev, ring, skb, control))
  96. return NETDEV_TX_BUSY;
  97. if (rt2x00dev->ops->lib->kick_tx_queue)
  98. rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, control->queue);
  99. return NETDEV_TX_OK;
  100. }
  101. EXPORT_SYMBOL_GPL(rt2x00mac_tx);
  102. int rt2x00mac_start(struct ieee80211_hw *hw)
  103. {
  104. struct rt2x00_dev *rt2x00dev = hw->priv;
  105. int status;
  106. if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
  107. return 0;
  108. /*
  109. * If this is the first interface which is added,
  110. * we should load the firmware now.
  111. */
  112. if (test_bit(REQUIRE_FIRMWARE, &rt2x00dev->flags)) {
  113. status = rt2x00lib_load_firmware(rt2x00dev);
  114. if (status)
  115. return status;
  116. }
  117. /*
  118. * Initialize the device.
  119. */
  120. status = rt2x00lib_initialize(rt2x00dev);
  121. if (status)
  122. return status;
  123. /*
  124. * Enable radio.
  125. */
  126. status = rt2x00lib_enable_radio(rt2x00dev);
  127. if (status) {
  128. rt2x00lib_uninitialize(rt2x00dev);
  129. return status;
  130. }
  131. return 0;
  132. }
  133. EXPORT_SYMBOL_GPL(rt2x00mac_start);
  134. void rt2x00mac_stop(struct ieee80211_hw *hw)
  135. {
  136. struct rt2x00_dev *rt2x00dev = hw->priv;
  137. /*
  138. * Perhaps we can add something smarter here,
  139. * but for now just disabling the radio should do.
  140. */
  141. rt2x00lib_disable_radio(rt2x00dev);
  142. }
  143. EXPORT_SYMBOL_GPL(rt2x00mac_stop);
  144. int rt2x00mac_add_interface(struct ieee80211_hw *hw,
  145. struct ieee80211_if_init_conf *conf)
  146. {
  147. struct rt2x00_dev *rt2x00dev = hw->priv;
  148. struct interface *intf = &rt2x00dev->interface;
  149. /*
  150. * We only support 1 non-monitor interface.
  151. */
  152. if (is_interface_present(intf))
  153. return -ENOBUFS;
  154. intf->id = conf->if_id;
  155. intf->type = conf->type;
  156. if (conf->type == IEEE80211_IF_TYPE_AP)
  157. memcpy(&intf->bssid, conf->mac_addr, ETH_ALEN);
  158. memcpy(&intf->mac, conf->mac_addr, ETH_ALEN);
  159. /*
  160. * The MAC adddress must be configured after the device
  161. * has been initialized. Otherwise the device can reset
  162. * the MAC registers.
  163. */
  164. rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
  165. rt2x00lib_config_type(rt2x00dev, conf->type);
  166. return 0;
  167. }
  168. EXPORT_SYMBOL_GPL(rt2x00mac_add_interface);
  169. void rt2x00mac_remove_interface(struct ieee80211_hw *hw,
  170. struct ieee80211_if_init_conf *conf)
  171. {
  172. struct rt2x00_dev *rt2x00dev = hw->priv;
  173. struct interface *intf = &rt2x00dev->interface;
  174. /*
  175. * We only support 1 non-monitor interface.
  176. */
  177. if (!is_interface_present(intf))
  178. return;
  179. intf->id = 0;
  180. intf->type = INVALID_INTERFACE;
  181. memset(&intf->bssid, 0x00, ETH_ALEN);
  182. memset(&intf->mac, 0x00, ETH_ALEN);
  183. /*
  184. * Make sure the bssid and mac address registers
  185. * are cleared to prevent false ACKing of frames.
  186. */
  187. rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
  188. rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
  189. rt2x00lib_config_type(rt2x00dev, intf->type);
  190. }
  191. EXPORT_SYMBOL_GPL(rt2x00mac_remove_interface);
  192. int rt2x00mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
  193. {
  194. struct rt2x00_dev *rt2x00dev = hw->priv;
  195. /*
  196. * If the device is not initialized we shouldn't accept
  197. * any configuration changes. Mac80211 might be calling
  198. * this function while we are trying to remove the device
  199. * or perhaps suspending it.
  200. */
  201. if (!test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
  202. return 0;
  203. /*
  204. * Check if we need to disable the radio,
  205. * if this is not the case, at least the RX must be disabled.
  206. */
  207. if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags)) {
  208. if (!conf->radio_enabled)
  209. rt2x00lib_disable_radio(rt2x00dev);
  210. else
  211. rt2x00lib_toggle_rx(rt2x00dev, 0);
  212. }
  213. rt2x00lib_config(rt2x00dev, conf);
  214. /*
  215. * Reenable RX only if the radio should be on.
  216. */
  217. if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  218. rt2x00lib_toggle_rx(rt2x00dev, 1);
  219. else if (conf->radio_enabled)
  220. return rt2x00lib_enable_radio(rt2x00dev);
  221. return 0;
  222. }
  223. EXPORT_SYMBOL_GPL(rt2x00mac_config);
  224. int rt2x00mac_config_interface(struct ieee80211_hw *hw, int if_id,
  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. * If the device is not initialized we shouldn't accept
  232. * any configuration changes. Mac80211 might be calling
  233. * this function while we are trying to remove the device
  234. * or perhaps suspending it.
  235. */
  236. if (!test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
  237. return 0;
  238. /*
  239. * If the given type does not match the configured type,
  240. * there has been a problem.
  241. */
  242. if (conf->type != intf->type)
  243. return -EINVAL;
  244. /*
  245. * If the interface does not work in master mode,
  246. * then the bssid value in the interface structure
  247. * should now be set.
  248. */
  249. if (conf->type != IEEE80211_IF_TYPE_AP)
  250. memcpy(&intf->bssid, conf->bssid, ETH_ALEN);
  251. rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
  252. /*
  253. * We only need to initialize the beacon when master mode is enabled.
  254. */
  255. if (conf->type != IEEE80211_IF_TYPE_AP || !conf->beacon)
  256. return 0;
  257. status = rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw,
  258. conf->beacon,
  259. conf->beacon_control);
  260. if (status)
  261. dev_kfree_skb(conf->beacon);
  262. return status;
  263. }
  264. EXPORT_SYMBOL_GPL(rt2x00mac_config_interface);
  265. int rt2x00mac_get_stats(struct ieee80211_hw *hw,
  266. struct ieee80211_low_level_stats *stats)
  267. {
  268. struct rt2x00_dev *rt2x00dev = hw->priv;
  269. /*
  270. * The dot11ACKFailureCount, dot11RTSFailureCount and
  271. * dot11RTSSuccessCount are updated in interrupt time.
  272. * dot11FCSErrorCount is updated in the link tuner.
  273. */
  274. memcpy(stats, &rt2x00dev->low_level_stats, sizeof(*stats));
  275. return 0;
  276. }
  277. EXPORT_SYMBOL_GPL(rt2x00mac_get_stats);
  278. int rt2x00mac_get_tx_stats(struct ieee80211_hw *hw,
  279. struct ieee80211_tx_queue_stats *stats)
  280. {
  281. struct rt2x00_dev *rt2x00dev = hw->priv;
  282. unsigned int i;
  283. for (i = 0; i < hw->queues; i++)
  284. memcpy(&stats->data[i], &rt2x00dev->tx[i].stats,
  285. sizeof(rt2x00dev->tx[i].stats));
  286. return 0;
  287. }
  288. EXPORT_SYMBOL_GPL(rt2x00mac_get_tx_stats);
  289. int rt2x00mac_conf_tx(struct ieee80211_hw *hw, int queue,
  290. const struct ieee80211_tx_queue_params *params)
  291. {
  292. struct rt2x00_dev *rt2x00dev = hw->priv;
  293. struct data_ring *ring;
  294. ring = rt2x00lib_get_ring(rt2x00dev, queue);
  295. if (unlikely(!ring))
  296. return -EINVAL;
  297. /*
  298. * The passed variables are stored as real value ((2^n)-1).
  299. * Ralink registers require to know the bit number 'n'.
  300. */
  301. if (params->cw_min)
  302. ring->tx_params.cw_min = fls(params->cw_min);
  303. else
  304. ring->tx_params.cw_min = 5; /* cw_min: 2^5 = 32. */
  305. if (params->cw_max)
  306. ring->tx_params.cw_max = fls(params->cw_max);
  307. else
  308. ring->tx_params.cw_max = 10; /* cw_min: 2^10 = 1024. */
  309. if (params->aifs)
  310. ring->tx_params.aifs = params->aifs;
  311. else
  312. ring->tx_params.aifs = 2;
  313. INFO(rt2x00dev,
  314. "Configured TX ring %d - CWmin: %d, CWmax: %d, Aifs: %d.\n",
  315. queue, ring->tx_params.cw_min, ring->tx_params.cw_max,
  316. ring->tx_params.aifs);
  317. return 0;
  318. }
  319. EXPORT_SYMBOL_GPL(rt2x00mac_conf_tx);