rt2x00mac.c 12 KB

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