rt2x00mac.c 12 KB

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