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.
24 Supported chipsets: RT2460, RT2560, RT2570,
25 rt2561, rt2561s, rt2661, rt2571W & rt2671.
29 * Set enviroment defines for rt2x00.h
31 #define DRV_NAME "rt2x00lib"
33 #include <linux/netdevice.h>
36 #include "rt2x00dev.h"
38 static int rt2x00_tx_rts_cts(struct rt2x00_dev *rt2x00dev,
39 struct data_ring *ring, 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,
61 frag_skb->data, frag_skb->len, control,
62 (struct ieee80211_cts*)(skb->data));
64 ieee80211_rts_get(rt2x00dev->hw,
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 rt2x00lib_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 = rt2x00_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 (control->flags & IEEE80211_TXCTL_USE_RTS_CTS &&
105 !is_cts_frame(frame_control) && !is_rts_frame(frame_control)) {
106 if (rt2x00_ring_free(ring) <= 1)
107 return NETDEV_TX_BUSY;
109 if (rt2x00_tx_rts_cts(rt2x00dev, ring, skb, control))
110 return NETDEV_TX_BUSY;
113 if (rt2x00dev->ops->lib->write_tx_data(rt2x00dev, ring, skb, control))
114 return NETDEV_TX_BUSY;
116 if (rt2x00dev->ops->lib->kick_tx_queue)
117 rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, control->queue);
121 EXPORT_SYMBOL_GPL(rt2x00lib_tx);
123 int rt2x00lib_reset(struct ieee80211_hw *hw)
125 struct rt2x00_dev *rt2x00dev = hw->priv;
127 rt2x00lib_disable_radio(rt2x00dev);
128 return rt2x00lib_enable_radio(rt2x00dev);
130 EXPORT_SYMBOL_GPL(rt2x00lib_reset);
132 int rt2x00lib_open(struct ieee80211_hw *hw)
134 struct rt2x00_dev *rt2x00dev = hw->priv;
138 * We must wait on the firmware before
139 * we can safely continue.
141 status = rt2x00lib_load_firmware_wait(rt2x00dev);
146 * Initialize the device.
148 status = rt2x00lib_initialize(rt2x00dev);
155 status = rt2x00lib_enable_radio(rt2x00dev);
157 rt2x00lib_uninitialize(rt2x00dev);
163 EXPORT_SYMBOL_GPL(rt2x00lib_open);
165 int rt2x00lib_stop(struct ieee80211_hw *hw)
167 struct rt2x00_dev *rt2x00dev = hw->priv;
169 rt2x00lib_disable_radio(rt2x00dev);
173 EXPORT_SYMBOL_GPL(rt2x00lib_stop);
175 int rt2x00lib_add_interface(struct ieee80211_hw *hw,
176 struct ieee80211_if_init_conf *conf)
178 struct rt2x00_dev *rt2x00dev = hw->priv;
179 struct interface *intf = &rt2x00dev->interface;
182 * We only support 1 non-monitor interface.
184 if (conf->type != IEEE80211_IF_TYPE_MNTR &&
185 is_interface_present(&rt2x00dev->interface))
189 * We support muliple monitor mode interfaces.
190 * All we need to do is increase the monitor_count.
192 if (conf->type == IEEE80211_IF_TYPE_MNTR) {
193 intf->monitor_count++;
195 intf->id = conf->if_id;
196 intf->type = conf->type;
197 if (conf->type == IEEE80211_IF_TYPE_AP)
198 memcpy(&intf->bssid, conf->mac_addr, ETH_ALEN);
203 * If this is the first interface which is being added,
204 * we should write the MAC address to the device.
206 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
207 rt2x00dev->ops->lib->config_mac_addr(rt2x00dev, conf->mac_addr);
210 * Enable periodic link tuning if this is a non-monitor interface.
212 if (conf->type != IEEE80211_IF_TYPE_MNTR)
213 rt2x00_start_link_tune(rt2x00dev);
217 EXPORT_SYMBOL_GPL(rt2x00lib_add_interface);
219 void rt2x00lib_remove_interface(struct ieee80211_hw *hw,
220 struct ieee80211_if_init_conf *conf)
222 struct rt2x00_dev *rt2x00dev = hw->priv;
223 struct interface *intf = &rt2x00dev->interface;
226 * We only support 1 non-monitor interface.
228 if (conf->type != IEEE80211_IF_TYPE_MNTR &&
229 !is_interface_present(&rt2x00dev->interface))
233 * We support muliple monitor mode interfaces.
234 * All we need to do is decrease the monitor_count.
236 if (conf->type == IEEE80211_IF_TYPE_MNTR) {
237 intf->monitor_count--;
238 } else if (intf->type == conf->type) {
240 intf->type = -EINVAL;
241 memset(&intf->bssid, 0x00, ETH_ALEN);
246 * When this is a non-monitor mode, stop the periodic link tuning.
248 if (conf->type != IEEE80211_IF_TYPE_MNTR)
249 rt2x00_stop_link_tune(rt2x00dev);
252 * Check if we still have 1 non-monitor or a monitor
253 * interface enabled. In that case we should update the
256 if (is_monitor_present(&rt2x00dev->interface) ^
257 is_interface_present(&rt2x00dev->interface)) {
258 if (is_interface_present(&rt2x00dev->interface))
259 rt2x00lib_config_type(rt2x00dev,
260 rt2x00dev->interface.type);
262 rt2x00lib_config_type(rt2x00dev,
263 IEEE80211_IF_TYPE_MNTR);
267 * Check which interfaces have been disabled.
269 if (!is_interface_present(&rt2x00dev->interface))
270 __clear_bit(INTERFACE_ENABLED, &rt2x00dev->flags);
271 else if (!is_monitor_present(&rt2x00dev->interface))
272 __clear_bit(INTERFACE_ENABLED_MONITOR, &rt2x00dev->flags);
274 EXPORT_SYMBOL_GPL(rt2x00lib_remove_interface);
276 int rt2x00lib_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
278 struct rt2x00_dev *rt2x00dev = hw->priv;
281 * Check if we need to disable the radio,
282 * if this is not the case, at least the RX must be disabled.
284 if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags)) {
285 if (!conf->radio_enabled)
286 rt2x00lib_disable_radio(rt2x00dev);
288 rt2x00lib_toggle_rx(rt2x00dev, 0);
291 rt2x00lib_config_phymode(rt2x00dev, conf->phymode);
292 rt2x00lib_config_channel(rt2x00dev, conf->channel_val,
293 conf->channel, conf->freq, conf->power_level);
294 rt2x00lib_config_txpower(rt2x00dev, conf->power_level);
295 rt2x00lib_config_antenna(rt2x00dev,
296 conf->antenna_sel_tx, conf->antenna_sel_rx);
297 rt2x00dev->ops->lib->config_duration(rt2x00dev,
298 (conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME),
302 * Reenable RX only if the radio should be on.
304 if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
305 rt2x00lib_toggle_rx(rt2x00dev, 1);
306 else if (conf->radio_enabled)
307 return rt2x00lib_enable_radio(rt2x00dev);
311 EXPORT_SYMBOL_GPL(rt2x00lib_config);
313 int rt2x00lib_config_interface(struct ieee80211_hw *hw, int if_id,
314 struct ieee80211_if_conf *conf)
316 struct rt2x00_dev *rt2x00dev = hw->priv;
317 struct interface *intf = &rt2x00dev->interface;
321 * Monitor mode does not need configuring.
322 * If the given type does not match the configured type,
323 * there has been a problem.
325 if (conf->type == IEEE80211_IF_TYPE_MNTR)
327 else if (conf->type != intf->type)
331 * If the interface does not work in master mode,
332 * then the bssid value in the interface structure
335 if (conf->type != IEEE80211_IF_TYPE_AP)
336 memcpy(&intf->bssid, conf->bssid, ETH_ALEN);
339 * Enable configuration.
340 * For Monitor mode, promisc mode will be forced on.
342 rt2x00lib_config_type(rt2x00dev, conf->type);
343 rt2x00lib_config_promisc(rt2x00dev, rt2x00dev->interface.promisc);
344 rt2x00dev->ops->lib->config_bssid(rt2x00dev, intf->bssid);
347 * We only need to initialize the beacon when master mode is enabled.
349 if (conf->type != IEEE80211_IF_TYPE_AP || !conf->beacon)
352 status = rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw,
353 conf->beacon, conf->beacon_control);
355 dev_kfree_skb(conf->beacon);
359 EXPORT_SYMBOL_GPL(rt2x00lib_config_interface);
361 void rt2x00lib_set_multicast_list(struct ieee80211_hw *hw,
362 unsigned short flags, int mc_count)
364 struct rt2x00_dev *rt2x00dev = hw->priv;
367 * Promisc mode is forced on for Monitor interfaces.
369 if (is_monitor_present(&rt2x00dev->interface))
373 * Check if the new state is different then the old state.
375 if (test_bit(INTERFACE_ENABLED_PROMISC, &rt2x00dev->flags) ==
376 (flags & IFF_PROMISC))
379 rt2x00dev->interface.promisc = (flags & IFF_PROMISC);
382 * Schedule the link tuner if this does not run
383 * automatically. The link tuner will be automatically
384 * switched off when it is not required.
386 if (!work_pending(&rt2x00dev->link.work.work))
387 queue_work(rt2x00dev->workqueue, &rt2x00dev->link.work.work);
389 EXPORT_SYMBOL_GPL(rt2x00lib_set_multicast_list);
391 int rt2x00lib_get_tx_stats(struct ieee80211_hw *hw,
392 struct ieee80211_tx_queue_stats *stats)
394 struct rt2x00_dev *rt2x00dev = hw->priv;
397 for (i = 0; i < hw->queues; i++)
398 memcpy(&stats->data[i], &rt2x00dev->tx[i].stats,
399 sizeof(rt2x00dev->tx[i].stats));
403 EXPORT_SYMBOL_GPL(rt2x00lib_get_tx_stats);
405 int rt2x00lib_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 = rt2x00_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(rt2x00lib_conf_tx);