rt2x00mac.c 12 KB

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