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.
27 * Set enviroment defines for rt2x00.h
29 #define DRV_NAME "rt2x00lib"
31 #include <linux/kernel.h>
32 #include <linux/module.h>
35 #include "rt2x00lib.h"
37 static int rt2x00mac_tx_rts_cts(struct rt2x00_dev *rt2x00dev,
38 struct data_ring *ring,
39 struct sk_buff *frag_skb,
40 struct ieee80211_tx_control *control)
45 if (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
46 size = sizeof(struct ieee80211_cts);
48 size = sizeof(struct ieee80211_rts);
50 skb = dev_alloc_skb(size + rt2x00dev->hw->extra_tx_headroom);
52 WARNING(rt2x00dev, "Failed to create RTS/CTS frame.\n");
53 return NETDEV_TX_BUSY;
56 skb_reserve(skb, rt2x00dev->hw->extra_tx_headroom);
59 if (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
60 ieee80211_ctstoself_get(rt2x00dev->hw, rt2x00dev->interface.id,
61 frag_skb->data, frag_skb->len, control,
62 (struct ieee80211_cts *)(skb->data));
64 ieee80211_rts_get(rt2x00dev->hw, rt2x00dev->interface.id,
65 frag_skb->data, frag_skb->len, control,
66 (struct ieee80211_rts *)(skb->data));
68 if (rt2x00dev->ops->lib->write_tx_data(rt2x00dev, ring, skb, control)) {
69 WARNING(rt2x00dev, "Failed to send RTS/CTS frame.\n");
70 return NETDEV_TX_BUSY;
76 int rt2x00mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
77 struct ieee80211_tx_control *control)
79 struct rt2x00_dev *rt2x00dev = hw->priv;
80 struct ieee80211_hdr *ieee80211hdr = (struct ieee80211_hdr *)skb->data;
81 struct data_ring *ring;
85 * Determine which ring to put packet on.
87 ring = rt2x00lib_get_ring(rt2x00dev, control->queue);
88 if (unlikely(!ring)) {
90 "Attempt to send packet over invalid queue %d.\n"
91 "Please file bug report to %s.\n",
92 control->queue, DRV_PROJECT);
93 dev_kfree_skb_any(skb);
98 * If CTS/RTS is required. and this frame is not CTS or RTS,
99 * create and queue that frame first. But make sure we have
100 * at least enough entries available to send this CTS/RTS
101 * frame as well as the data frame.
103 frame_control = le16_to_cpu(ieee80211hdr->frame_control);
104 if (!is_rts_frame(frame_control) && !is_cts_frame(frame_control) &&
105 (control->flags & (IEEE80211_TXCTL_USE_RTS_CTS |
106 IEEE80211_TXCTL_USE_CTS_PROTECT))) {
107 if (rt2x00_ring_free(ring) <= 1)
108 return NETDEV_TX_BUSY;
110 if (rt2x00mac_tx_rts_cts(rt2x00dev, ring, skb, control))
111 return NETDEV_TX_BUSY;
114 if (rt2x00dev->ops->lib->write_tx_data(rt2x00dev, ring, skb, control))
115 return NETDEV_TX_BUSY;
117 if (rt2x00dev->ops->lib->kick_tx_queue)
118 rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, control->queue);
122 EXPORT_SYMBOL_GPL(rt2x00mac_tx);
124 int rt2x00mac_start(struct ieee80211_hw *hw)
126 struct rt2x00_dev *rt2x00dev = hw->priv;
129 if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
133 * If this is the first interface which is added,
134 * we should load the firmware now.
136 if (test_bit(REQUIRE_FIRMWARE, &rt2x00dev->flags)) {
137 status = rt2x00lib_load_firmware(rt2x00dev);
143 * Initialize the device.
145 status = rt2x00lib_initialize(rt2x00dev);
152 status = rt2x00lib_enable_radio(rt2x00dev);
154 rt2x00lib_uninitialize(rt2x00dev);
160 EXPORT_SYMBOL_GPL(rt2x00mac_start);
162 void rt2x00mac_stop(struct ieee80211_hw *hw)
164 struct rt2x00_dev *rt2x00dev = hw->priv;
167 * Perhaps we can add something smarter here,
168 * but for now just disabling the radio should do.
170 rt2x00lib_disable_radio(rt2x00dev);
172 EXPORT_SYMBOL_GPL(rt2x00mac_stop);
174 int rt2x00mac_add_interface(struct ieee80211_hw *hw,
175 struct ieee80211_if_init_conf *conf)
177 struct rt2x00_dev *rt2x00dev = hw->priv;
178 struct interface *intf = &rt2x00dev->interface;
181 * We only support 1 non-monitor interface.
183 if (is_interface_present(intf))
186 intf->id = conf->if_id;
187 intf->type = conf->type;
188 if (conf->type == IEEE80211_IF_TYPE_AP)
189 memcpy(&intf->bssid, conf->mac_addr, ETH_ALEN);
190 memcpy(&intf->mac, conf->mac_addr, ETH_ALEN);
193 * The MAC adddress must be configured after the device
194 * has been initialized. Otherwise the device can reset
197 rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
198 rt2x00lib_config_type(rt2x00dev, conf->type);
202 EXPORT_SYMBOL_GPL(rt2x00mac_add_interface);
204 void rt2x00mac_remove_interface(struct ieee80211_hw *hw,
205 struct ieee80211_if_init_conf *conf)
207 struct rt2x00_dev *rt2x00dev = hw->priv;
208 struct interface *intf = &rt2x00dev->interface;
211 * We only support 1 non-monitor interface.
213 if (!is_interface_present(intf))
217 intf->type = INVALID_INTERFACE;
218 memset(&intf->bssid, 0x00, ETH_ALEN);
219 memset(&intf->mac, 0x00, ETH_ALEN);
222 * Make sure the bssid and mac address registers
223 * are cleared to prevent false ACKing of frames.
225 rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
226 rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
227 rt2x00lib_config_type(rt2x00dev, intf->type);
229 EXPORT_SYMBOL_GPL(rt2x00mac_remove_interface);
231 int rt2x00mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
233 struct rt2x00_dev *rt2x00dev = hw->priv;
236 * If the device is not initialized we shouldn't accept
237 * any configuration changes. Mac80211 might be calling
238 * this function while we are trying to remove the device
239 * or perhaps suspending it.
241 if (!test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
245 * Check if we need to disable the radio,
246 * if this is not the case, at least the RX must be disabled.
248 if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags)) {
249 if (!conf->radio_enabled)
250 rt2x00lib_disable_radio(rt2x00dev);
252 rt2x00lib_toggle_rx(rt2x00dev, 0);
255 rt2x00lib_config(rt2x00dev, conf);
258 * Reenable RX only if the radio should be on.
260 if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
261 rt2x00lib_toggle_rx(rt2x00dev, 1);
262 else if (conf->radio_enabled)
263 return rt2x00lib_enable_radio(rt2x00dev);
267 EXPORT_SYMBOL_GPL(rt2x00mac_config);
269 int rt2x00mac_config_interface(struct ieee80211_hw *hw, int if_id,
270 struct ieee80211_if_conf *conf)
272 struct rt2x00_dev *rt2x00dev = hw->priv;
273 struct interface *intf = &rt2x00dev->interface;
277 * If the device is not initialized we shouldn't accept
278 * any configuration changes. Mac80211 might be calling
279 * this function while we are trying to remove the device
280 * or perhaps suspending it.
282 if (!test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
286 * If the given type does not match the configured type,
287 * there has been a problem.
289 if (conf->type != intf->type)
293 * If the interface does not work in master mode,
294 * then the bssid value in the interface structure
297 if (conf->type != IEEE80211_IF_TYPE_AP)
298 memcpy(&intf->bssid, conf->bssid, ETH_ALEN);
299 rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
302 * We only need to initialize the beacon when master mode is enabled.
304 if (conf->type != IEEE80211_IF_TYPE_AP || !conf->beacon)
307 status = rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw,
309 conf->beacon_control);
311 dev_kfree_skb(conf->beacon);
315 EXPORT_SYMBOL_GPL(rt2x00mac_config_interface);
317 int rt2x00mac_get_stats(struct ieee80211_hw *hw,
318 struct ieee80211_low_level_stats *stats)
320 struct rt2x00_dev *rt2x00dev = hw->priv;
323 * The dot11ACKFailureCount, dot11RTSFailureCount and
324 * dot11RTSSuccessCount are updated in interrupt time.
325 * dot11FCSErrorCount is updated in the link tuner.
327 memcpy(stats, &rt2x00dev->low_level_stats, sizeof(*stats));
331 EXPORT_SYMBOL_GPL(rt2x00mac_get_stats);
333 int rt2x00mac_get_tx_stats(struct ieee80211_hw *hw,
334 struct ieee80211_tx_queue_stats *stats)
336 struct rt2x00_dev *rt2x00dev = hw->priv;
339 for (i = 0; i < hw->queues; i++)
340 memcpy(&stats->data[i], &rt2x00dev->tx[i].stats,
341 sizeof(rt2x00dev->tx[i].stats));
345 EXPORT_SYMBOL_GPL(rt2x00mac_get_tx_stats);
347 int rt2x00mac_conf_tx(struct ieee80211_hw *hw, int queue,
348 const struct ieee80211_tx_queue_params *params)
350 struct rt2x00_dev *rt2x00dev = hw->priv;
351 struct data_ring *ring;
353 ring = rt2x00lib_get_ring(rt2x00dev, queue);
358 * The passed variables are stored as real value ((2^n)-1).
359 * Ralink registers require to know the bit number 'n'.
362 ring->tx_params.cw_min = fls(params->cw_min);
364 ring->tx_params.cw_min = 5; /* cw_min: 2^5 = 32. */
367 ring->tx_params.cw_max = fls(params->cw_max);
369 ring->tx_params.cw_max = 10; /* cw_min: 2^10 = 1024. */
372 ring->tx_params.aifs = params->aifs;
374 ring->tx_params.aifs = 2;
377 "Configured TX ring %d - CWmin: %d, CWmax: %d, Aifs: %d.\n",
378 queue, ring->tx_params.cw_min, ring->tx_params.cw_max,
379 ring->tx_params.aifs);
383 EXPORT_SYMBOL_GPL(rt2x00mac_conf_tx);