2 Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
3 <http://rt2x00.serialmonkey.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the
17 Free Software Foundation, Inc.,
18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 Abstract: rt2x00 generic mac80211 routines.
26 #include <linux/kernel.h>
27 #include <linux/module.h>
30 #include "rt2x00lib.h"
32 static int rt2x00mac_tx_rts_cts(struct rt2x00_dev *rt2x00dev,
33 struct data_ring *ring,
34 struct sk_buff *frag_skb,
35 struct ieee80211_tx_control *control)
40 if (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
41 size = sizeof(struct ieee80211_cts);
43 size = sizeof(struct ieee80211_rts);
45 skb = dev_alloc_skb(size + rt2x00dev->hw->extra_tx_headroom);
47 WARNING(rt2x00dev, "Failed to create RTS/CTS frame.\n");
48 return NETDEV_TX_BUSY;
51 skb_reserve(skb, rt2x00dev->hw->extra_tx_headroom);
54 if (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
55 ieee80211_ctstoself_get(rt2x00dev->hw, rt2x00dev->interface.id,
56 frag_skb->data, frag_skb->len, control,
57 (struct ieee80211_cts *)(skb->data));
59 ieee80211_rts_get(rt2x00dev->hw, rt2x00dev->interface.id,
60 frag_skb->data, frag_skb->len, control,
61 (struct ieee80211_rts *)(skb->data));
63 if (rt2x00dev->ops->lib->write_tx_data(rt2x00dev, ring, skb, control)) {
64 WARNING(rt2x00dev, "Failed to send RTS/CTS frame.\n");
65 return NETDEV_TX_BUSY;
71 int rt2x00mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
72 struct ieee80211_tx_control *control)
74 struct rt2x00_dev *rt2x00dev = hw->priv;
75 struct ieee80211_hdr *ieee80211hdr = (struct ieee80211_hdr *)skb->data;
76 struct data_ring *ring;
80 * Mac80211 might be calling this function while we are trying
81 * to remove the device or perhaps suspending it.
82 * Note that we can only stop the TX queues inside the TX path
83 * due to possible race conditions in mac80211.
85 if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags)) {
86 ieee80211_stop_queues(hw);
91 * Determine which ring to put packet on.
93 ring = rt2x00lib_get_ring(rt2x00dev, control->queue);
94 if (unlikely(!ring)) {
96 "Attempt to send packet over invalid queue %d.\n"
97 "Please file bug report to %s.\n",
98 control->queue, DRV_PROJECT);
99 dev_kfree_skb_any(skb);
104 * If CTS/RTS is required. and this frame is not CTS or RTS,
105 * create and queue that frame first. But make sure we have
106 * at least enough entries available to send this CTS/RTS
107 * frame as well as the data frame.
109 frame_control = le16_to_cpu(ieee80211hdr->frame_control);
110 if (!is_rts_frame(frame_control) && !is_cts_frame(frame_control) &&
111 (control->flags & (IEEE80211_TXCTL_USE_RTS_CTS |
112 IEEE80211_TXCTL_USE_CTS_PROTECT))) {
113 if (rt2x00_ring_free(ring) <= 1)
114 return NETDEV_TX_BUSY;
116 if (rt2x00mac_tx_rts_cts(rt2x00dev, ring, skb, control))
117 return NETDEV_TX_BUSY;
120 if (rt2x00dev->ops->lib->write_tx_data(rt2x00dev, ring, skb, control))
121 return NETDEV_TX_BUSY;
123 if (rt2x00dev->ops->lib->kick_tx_queue)
124 rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, control->queue);
128 EXPORT_SYMBOL_GPL(rt2x00mac_tx);
130 int rt2x00mac_start(struct ieee80211_hw *hw)
132 struct rt2x00_dev *rt2x00dev = hw->priv;
135 if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags) ||
136 test_bit(DEVICE_STARTED, &rt2x00dev->flags))
140 * If this is the first interface which is added,
141 * we should load the firmware now.
143 if (test_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags)) {
144 status = rt2x00lib_load_firmware(rt2x00dev);
150 * Initialize the device.
152 status = rt2x00lib_initialize(rt2x00dev);
159 status = rt2x00lib_enable_radio(rt2x00dev);
161 rt2x00lib_uninitialize(rt2x00dev);
165 __set_bit(DEVICE_STARTED, &rt2x00dev->flags);
169 EXPORT_SYMBOL_GPL(rt2x00mac_start);
171 void rt2x00mac_stop(struct ieee80211_hw *hw)
173 struct rt2x00_dev *rt2x00dev = hw->priv;
175 if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
179 * Perhaps we can add something smarter here,
180 * but for now just disabling the radio should do.
182 rt2x00lib_disable_radio(rt2x00dev);
184 __clear_bit(DEVICE_STARTED, &rt2x00dev->flags);
186 EXPORT_SYMBOL_GPL(rt2x00mac_stop);
188 int rt2x00mac_add_interface(struct ieee80211_hw *hw,
189 struct ieee80211_if_init_conf *conf)
191 struct rt2x00_dev *rt2x00dev = hw->priv;
192 struct interface *intf = &rt2x00dev->interface;
194 /* FIXME: Beaconing is broken in rt2x00. */
195 if (conf->type == IEEE80211_IF_TYPE_IBSS ||
196 conf->type == IEEE80211_IF_TYPE_AP) {
198 "rt2x00 does not support Adhoc or Master mode");
203 * Don't allow interfaces to be added while
204 * either the device has disappeared or when
205 * another interface is already present.
207 if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags) ||
208 is_interface_present(intf))
211 intf->id = conf->if_id;
212 intf->type = conf->type;
213 if (conf->type == IEEE80211_IF_TYPE_AP)
214 memcpy(&intf->bssid, conf->mac_addr, ETH_ALEN);
215 memcpy(&intf->mac, conf->mac_addr, ETH_ALEN);
218 * The MAC adddress must be configured after the device
219 * has been initialized. Otherwise the device can reset
222 rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
223 rt2x00lib_config_type(rt2x00dev, conf->type);
227 EXPORT_SYMBOL_GPL(rt2x00mac_add_interface);
229 void rt2x00mac_remove_interface(struct ieee80211_hw *hw,
230 struct ieee80211_if_init_conf *conf)
232 struct rt2x00_dev *rt2x00dev = hw->priv;
233 struct interface *intf = &rt2x00dev->interface;
236 * Don't allow interfaces to be remove while
237 * either the device has disappeared or when
238 * no interface is present.
240 if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags) ||
241 !is_interface_present(intf))
245 intf->type = INVALID_INTERFACE;
246 memset(&intf->bssid, 0x00, ETH_ALEN);
247 memset(&intf->mac, 0x00, ETH_ALEN);
250 * Make sure the bssid and mac address registers
251 * are cleared to prevent false ACKing of frames.
253 rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
254 rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
255 rt2x00lib_config_type(rt2x00dev, intf->type);
257 EXPORT_SYMBOL_GPL(rt2x00mac_remove_interface);
259 int rt2x00mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
261 struct rt2x00_dev *rt2x00dev = hw->priv;
264 * Mac80211 might be calling this function while we are trying
265 * to remove the device or perhaps suspending it.
267 if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
271 * Check if we need to disable the radio,
272 * if this is not the case, at least the RX must be disabled.
274 if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags)) {
275 if (!conf->radio_enabled)
276 rt2x00lib_disable_radio(rt2x00dev);
278 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
281 rt2x00lib_config(rt2x00dev, conf, 0);
284 * Reenable RX only if the radio should be on.
286 if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
287 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
288 else if (conf->radio_enabled)
289 return rt2x00lib_enable_radio(rt2x00dev);
293 EXPORT_SYMBOL_GPL(rt2x00mac_config);
295 int rt2x00mac_config_interface(struct ieee80211_hw *hw, int if_id,
296 struct ieee80211_if_conf *conf)
298 struct rt2x00_dev *rt2x00dev = hw->priv;
299 struct interface *intf = &rt2x00dev->interface;
303 * Mac80211 might be calling this function while we are trying
304 * to remove the device or perhaps suspending it.
306 if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
310 * If the given type does not match the configured type,
311 * there has been a problem.
313 if (conf->type != intf->type)
317 * If the interface does not work in master mode,
318 * then the bssid value in the interface structure
321 if (conf->type != IEEE80211_IF_TYPE_AP)
322 memcpy(&intf->bssid, conf->bssid, ETH_ALEN);
323 rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
326 * We only need to initialize the beacon when master mode is enabled.
328 if (conf->type != IEEE80211_IF_TYPE_AP || !conf->beacon)
331 status = rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw,
333 conf->beacon_control);
335 dev_kfree_skb(conf->beacon);
339 EXPORT_SYMBOL_GPL(rt2x00mac_config_interface);
341 int rt2x00mac_get_stats(struct ieee80211_hw *hw,
342 struct ieee80211_low_level_stats *stats)
344 struct rt2x00_dev *rt2x00dev = hw->priv;
347 * The dot11ACKFailureCount, dot11RTSFailureCount and
348 * dot11RTSSuccessCount are updated in interrupt time.
349 * dot11FCSErrorCount is updated in the link tuner.
351 memcpy(stats, &rt2x00dev->low_level_stats, sizeof(*stats));
355 EXPORT_SYMBOL_GPL(rt2x00mac_get_stats);
357 int rt2x00mac_get_tx_stats(struct ieee80211_hw *hw,
358 struct ieee80211_tx_queue_stats *stats)
360 struct rt2x00_dev *rt2x00dev = hw->priv;
363 for (i = 0; i < hw->queues; i++)
364 memcpy(&stats->data[i], &rt2x00dev->tx[i].stats,
365 sizeof(rt2x00dev->tx[i].stats));
369 EXPORT_SYMBOL_GPL(rt2x00mac_get_tx_stats);
371 void rt2x00mac_erp_ie_changed(struct ieee80211_hw *hw, u8 changes,
372 int cts_protection, int preamble)
374 struct rt2x00_dev *rt2x00dev = hw->priv;
377 int ack_consume_time;
381 * We only support changing preamble mode.
383 if (!(changes & IEEE80211_ERP_CHANGE_PREAMBLE))
386 short_preamble = !preamble;
387 preamble = !!(preamble) ? PREAMBLE : SHORT_PREAMBLE;
389 difs = (hw->conf.flags & IEEE80211_CONF_SHORT_SLOT_TIME) ?
391 ack_timeout = difs + PLCP + preamble + get_duration(ACK_SIZE, 10);
393 ack_consume_time = SIFS + PLCP + preamble + get_duration(ACK_SIZE, 10);
396 __set_bit(CONFIG_SHORT_PREAMBLE, &rt2x00dev->flags);
398 __clear_bit(CONFIG_SHORT_PREAMBLE, &rt2x00dev->flags);
400 rt2x00dev->ops->lib->config_preamble(rt2x00dev, short_preamble,
401 ack_timeout, ack_consume_time);
403 EXPORT_SYMBOL_GPL(rt2x00mac_erp_ie_changed);
405 int rt2x00mac_conf_tx(struct ieee80211_hw *hw, int queue,
406 const struct ieee80211_tx_queue_params *params)
408 struct rt2x00_dev *rt2x00dev = hw->priv;
409 struct data_ring *ring;
411 ring = rt2x00lib_get_ring(rt2x00dev, queue);
416 * The passed variables are stored as real value ((2^n)-1).
417 * Ralink registers require to know the bit number 'n'.
420 ring->tx_params.cw_min = fls(params->cw_min);
422 ring->tx_params.cw_min = 5; /* cw_min: 2^5 = 32. */
425 ring->tx_params.cw_max = fls(params->cw_max);
427 ring->tx_params.cw_max = 10; /* cw_min: 2^10 = 1024. */
430 ring->tx_params.aifs = params->aifs;
432 ring->tx_params.aifs = 2;
435 "Configured TX ring %d - CWmin: %d, CWmax: %d, Aifs: %d.\n",
436 queue, ring->tx_params.cw_min, ring->tx_params.cw_max,
437 ring->tx_params.aifs);
441 EXPORT_SYMBOL_GPL(rt2x00mac_conf_tx);