Add rt2x00-mac80211 snapshot (#1916)
[openwrt/svn-archive/archive.git] / package / rt2x00 / src / rt2x00mac.c
1 /*
2 Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
3 <http://rt2x00.serialmonkey.com>
4
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.
9
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.
14
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.
19 */
20
21 /*
22 Module: rt2x00lib
23 Abstract: rt2x00 generic mac80211 routines.
24 Supported chipsets: RT2460, RT2560, RT2570,
25 rt2561, rt2561s, rt2661, rt2571W & rt2671.
26 */
27
28 /*
29 * Set enviroment defines for rt2x00.h
30 */
31 #define DRV_NAME "rt2x00lib"
32
33 #include <linux/netdevice.h>
34
35 #include "rt2x00.h"
36 #include "rt2x00dev.h"
37
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)
41 {
42 struct sk_buff *skb;
43 int size;
44
45 if (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
46 size = sizeof(struct ieee80211_cts);
47 else
48 size = sizeof(struct ieee80211_rts);
49
50 skb = dev_alloc_skb(size + rt2x00dev->hw->extra_tx_headroom);
51 if (!skb) {
52 WARNING(rt2x00dev, "Failed to create RTS/CTS frame.\n");
53 return NETDEV_TX_BUSY;
54 }
55
56 skb_reserve(skb, rt2x00dev->hw->extra_tx_headroom);
57 skb_put(skb, size);
58
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));
63 else
64 ieee80211_rts_get(rt2x00dev->hw,
65 frag_skb->data, frag_skb->len, control,
66 (struct ieee80211_rts*)(skb->data));
67
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;
71 }
72
73 return NETDEV_TX_OK;
74 }
75
76 int rt2x00lib_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
77 struct ieee80211_tx_control *control)
78 {
79 struct rt2x00_dev *rt2x00dev = hw->priv;
80 struct ieee80211_hdr *ieee80211hdr = (struct ieee80211_hdr*)skb->data;
81 struct data_ring *ring;
82 u16 frame_control;
83
84 /*
85 * Determine which ring to put packet on.
86 */
87 ring = rt2x00_get_ring(rt2x00dev, control->queue);
88 if (unlikely(!ring)) {
89 ERROR(rt2x00dev,
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);
94 return NETDEV_TX_OK;
95 }
96
97 /*
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.
102 */
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;
108
109 if (rt2x00_tx_rts_cts(rt2x00dev, ring, skb, control))
110 return NETDEV_TX_BUSY;
111 }
112
113 if (rt2x00dev->ops->lib->write_tx_data(rt2x00dev, ring, skb, control))
114 return NETDEV_TX_BUSY;
115
116 if (rt2x00dev->ops->lib->kick_tx_queue)
117 rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, control->queue);
118
119 return NETDEV_TX_OK;
120 }
121 EXPORT_SYMBOL_GPL(rt2x00lib_tx);
122
123 int rt2x00lib_reset(struct ieee80211_hw *hw)
124 {
125 struct rt2x00_dev *rt2x00dev = hw->priv;
126
127 rt2x00lib_disable_radio(rt2x00dev);
128 return rt2x00lib_enable_radio(rt2x00dev);
129 }
130 EXPORT_SYMBOL_GPL(rt2x00lib_reset);
131
132 int rt2x00lib_open(struct ieee80211_hw *hw)
133 {
134 struct rt2x00_dev *rt2x00dev = hw->priv;
135 int status;
136
137 /*
138 * We must wait on the firmware before
139 * we can safely continue.
140 */
141 status = rt2x00lib_load_firmware_wait(rt2x00dev);
142 if (status)
143 return status;
144
145 /*
146 * Initialize the device.
147 */
148 status = rt2x00lib_initialize(rt2x00dev);
149 if (status)
150 return status;
151
152 /*
153 * Enable radio.
154 */
155 status = rt2x00lib_enable_radio(rt2x00dev);
156 if (status) {
157 rt2x00lib_uninitialize(rt2x00dev);
158 return status;
159 }
160
161 return 0;
162 }
163 EXPORT_SYMBOL_GPL(rt2x00lib_open);
164
165 int rt2x00lib_stop(struct ieee80211_hw *hw)
166 {
167 struct rt2x00_dev *rt2x00dev = hw->priv;
168
169 rt2x00lib_disable_radio(rt2x00dev);
170
171 return 0;
172 }
173 EXPORT_SYMBOL_GPL(rt2x00lib_stop);
174
175 int rt2x00lib_add_interface(struct ieee80211_hw *hw,
176 struct ieee80211_if_init_conf *conf)
177 {
178 struct rt2x00_dev *rt2x00dev = hw->priv;
179 struct interface *intf = &rt2x00dev->interface;
180
181 /*
182 * We only support 1 non-monitor interface.
183 */
184 if (conf->type != IEEE80211_IF_TYPE_MNTR &&
185 is_interface_present(&rt2x00dev->interface))
186 return -ENOBUFS;
187
188 /*
189 * We support muliple monitor mode interfaces.
190 * All we need to do is increase the monitor_count.
191 */
192 if (conf->type == IEEE80211_IF_TYPE_MNTR) {
193 intf->monitor_count++;
194 } else {
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);
199 intf->promisc = 0;
200 }
201
202 /*
203 * If this is the first interface which is being added,
204 * we should write the MAC address to the device.
205 */
206 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
207 rt2x00dev->ops->lib->config_mac_addr(rt2x00dev, conf->mac_addr);
208
209 /*
210 * Enable periodic link tuning if this is a non-monitor interface.
211 */
212 if (conf->type != IEEE80211_IF_TYPE_MNTR)
213 rt2x00_start_link_tune(rt2x00dev);
214
215 return 0;
216 }
217 EXPORT_SYMBOL_GPL(rt2x00lib_add_interface);
218
219 void rt2x00lib_remove_interface(struct ieee80211_hw *hw,
220 struct ieee80211_if_init_conf *conf)
221 {
222 struct rt2x00_dev *rt2x00dev = hw->priv;
223 struct interface *intf = &rt2x00dev->interface;
224
225 /*
226 * We only support 1 non-monitor interface.
227 */
228 if (conf->type != IEEE80211_IF_TYPE_MNTR &&
229 !is_interface_present(&rt2x00dev->interface))
230 return;
231
232 /*
233 * We support muliple monitor mode interfaces.
234 * All we need to do is decrease the monitor_count.
235 */
236 if (conf->type == IEEE80211_IF_TYPE_MNTR) {
237 intf->monitor_count--;
238 } else if (intf->type == conf->type) {
239 intf->id = 0;
240 intf->type = -EINVAL;
241 memset(&intf->bssid, 0x00, ETH_ALEN);
242 intf->promisc = 0;
243 }
244
245 /*
246 * When this is a non-monitor mode, stop the periodic link tuning.
247 */
248 if (conf->type != IEEE80211_IF_TYPE_MNTR)
249 rt2x00_stop_link_tune(rt2x00dev);
250
251 /*
252 * Check if we still have 1 non-monitor or a monitor
253 * interface enabled. In that case we should update the
254 * registers.
255 */
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);
261 else
262 rt2x00lib_config_type(rt2x00dev,
263 IEEE80211_IF_TYPE_MNTR);
264 }
265
266 /*
267 * Check which interfaces have been disabled.
268 */
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);
273 }
274 EXPORT_SYMBOL_GPL(rt2x00lib_remove_interface);
275
276 int rt2x00lib_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
277 {
278 struct rt2x00_dev *rt2x00dev = hw->priv;
279
280 /*
281 * Check if we need to disable the radio,
282 * if this is not the case, at least the RX must be disabled.
283 */
284 if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags)) {
285 if (!conf->radio_enabled)
286 rt2x00lib_disable_radio(rt2x00dev);
287 else
288 rt2x00lib_toggle_rx(rt2x00dev, 0);
289 }
290
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),
299 conf->beacon_int);
300
301 /*
302 * Reenable RX only if the radio should be on.
303 */
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);
308
309 return 0;
310 }
311 EXPORT_SYMBOL_GPL(rt2x00lib_config);
312
313 int rt2x00lib_config_interface(struct ieee80211_hw *hw, int if_id,
314 struct ieee80211_if_conf *conf)
315 {
316 struct rt2x00_dev *rt2x00dev = hw->priv;
317 struct interface *intf = &rt2x00dev->interface;
318 int status;
319
320 /*
321 * Monitor mode does not need configuring.
322 * If the given type does not match the configured type,
323 * there has been a problem.
324 */
325 if (conf->type == IEEE80211_IF_TYPE_MNTR)
326 return 0;
327 else if (conf->type != intf->type)
328 return -EINVAL;
329
330 /*
331 * If the interface does not work in master mode,
332 * then the bssid value in the interface structure
333 * should now be set.
334 */
335 if (conf->type != IEEE80211_IF_TYPE_AP)
336 memcpy(&intf->bssid, conf->bssid, ETH_ALEN);
337
338 /*
339 * Enable configuration.
340 * For Monitor mode, promisc mode will be forced on.
341 */
342 rt2x00lib_config_type(rt2x00dev, conf->type);
343 rt2x00lib_config_promisc(rt2x00dev, rt2x00dev->interface.promisc);
344 rt2x00dev->ops->lib->config_bssid(rt2x00dev, intf->bssid);
345
346 /*
347 * We only need to initialize the beacon when master mode is enabled.
348 */
349 if (conf->type != IEEE80211_IF_TYPE_AP || !conf->beacon)
350 return 0;
351
352 status = rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw,
353 conf->beacon, conf->beacon_control);
354 if (status)
355 dev_kfree_skb(conf->beacon);
356
357 return status;
358 }
359 EXPORT_SYMBOL_GPL(rt2x00lib_config_interface);
360
361 void rt2x00lib_set_multicast_list(struct ieee80211_hw *hw,
362 unsigned short flags, int mc_count)
363 {
364 struct rt2x00_dev *rt2x00dev = hw->priv;
365
366 /*
367 * Promisc mode is forced on for Monitor interfaces.
368 */
369 if (is_monitor_present(&rt2x00dev->interface))
370 return;
371
372 /*
373 * Check if the new state is different then the old state.
374 */
375 if (test_bit(INTERFACE_ENABLED_PROMISC, &rt2x00dev->flags) ==
376 (flags & IFF_PROMISC))
377 return;
378
379 rt2x00dev->interface.promisc = (flags & IFF_PROMISC);
380
381 /*
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.
385 */
386 if (!work_pending(&rt2x00dev->link.work.work))
387 queue_work(rt2x00dev->workqueue, &rt2x00dev->link.work.work);
388 }
389 EXPORT_SYMBOL_GPL(rt2x00lib_set_multicast_list);
390
391 int rt2x00lib_get_tx_stats(struct ieee80211_hw *hw,
392 struct ieee80211_tx_queue_stats *stats)
393 {
394 struct rt2x00_dev *rt2x00dev = hw->priv;
395 unsigned int i;
396
397 for (i = 0; i < hw->queues; i++)
398 memcpy(&stats->data[i], &rt2x00dev->tx[i].stats,
399 sizeof(rt2x00dev->tx[i].stats));
400
401 return 0;
402 }
403 EXPORT_SYMBOL_GPL(rt2x00lib_get_tx_stats);
404
405 int rt2x00lib_conf_tx(struct ieee80211_hw *hw, int queue,
406 const struct ieee80211_tx_queue_params *params)
407 {
408 struct rt2x00_dev *rt2x00dev = hw->priv;
409 struct data_ring *ring;
410
411 ring = rt2x00_get_ring(rt2x00dev, queue);
412 if (unlikely(!ring))
413 return -EINVAL;
414
415 /*
416 * The passed variables are stored as real value ((2^n)-1).
417 * Ralink registers require to know the bit number 'n'.
418 */
419 if (params->cw_min)
420 ring->tx_params.cw_min = fls(params->cw_min);
421 else
422 ring->tx_params.cw_min = 5; /* cw_min: 2^5 = 32. */
423
424 if (params->cw_max)
425 ring->tx_params.cw_max = fls(params->cw_max);
426 else
427 ring->tx_params.cw_max = 10; /* cw_min: 2^10 = 1024. */
428
429 if (params->aifs)
430 ring->tx_params.aifs = params->aifs;
431 else
432 ring->tx_params.aifs = 2;
433
434 INFO(rt2x00dev,
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);
438
439 return 0;
440 }
441 EXPORT_SYMBOL_GPL(rt2x00lib_conf_tx);