rt2x00mac.c 12 KB

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