ar7: remove unneeded packed and array initialization
[openwrt/staging/florian.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: rt2x00mac
23 Abstract: rt2x00 generic mac80211 routines.
24 */
25
26 /*
27 * Set enviroment defines for rt2x00.h
28 */
29 #define DRV_NAME "rt2x00lib"
30
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33
34 #include "rt2x00.h"
35 #include "rt2x00lib.h"
36
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)
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, rt2x00dev->interface.id,
61 frag_skb->data, frag_skb->len, control,
62 (struct ieee80211_cts *)(skb->data));
63 else
64 ieee80211_rts_get(rt2x00dev->hw, rt2x00dev->interface.id,
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 rt2x00mac_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 * Mac80211 might be calling this function while we are trying
86 * to remove the device or perhaps suspending it.
87 * Note that we can only stop the TX queues inside the TX path
88 * due to possible race conditions in mac80211.
89 */
90 if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags)) {
91 ieee80211_stop_queues(hw);
92 return 0;
93 }
94
95 /*
96 * Determine which ring to put packet on.
97 */
98 ring = rt2x00lib_get_ring(rt2x00dev, control->queue);
99 if (unlikely(!ring)) {
100 ERROR(rt2x00dev,
101 "Attempt to send packet over invalid queue %d.\n"
102 "Please file bug report to %s.\n",
103 control->queue, DRV_PROJECT);
104 dev_kfree_skb_any(skb);
105 return NETDEV_TX_OK;
106 }
107
108 /*
109 * If CTS/RTS is required. and this frame is not CTS or RTS,
110 * create and queue that frame first. But make sure we have
111 * at least enough entries available to send this CTS/RTS
112 * frame as well as the data frame.
113 */
114 frame_control = le16_to_cpu(ieee80211hdr->frame_control);
115 if (!is_rts_frame(frame_control) && !is_cts_frame(frame_control) &&
116 (control->flags & (IEEE80211_TXCTL_USE_RTS_CTS |
117 IEEE80211_TXCTL_USE_CTS_PROTECT))) {
118 if (rt2x00_ring_free(ring) <= 1)
119 return NETDEV_TX_BUSY;
120
121 if (rt2x00mac_tx_rts_cts(rt2x00dev, ring, skb, control))
122 return NETDEV_TX_BUSY;
123 }
124
125 if (rt2x00dev->ops->lib->write_tx_data(rt2x00dev, ring, skb, control))
126 return NETDEV_TX_BUSY;
127
128 if (rt2x00dev->ops->lib->kick_tx_queue)
129 rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, control->queue);
130
131 return NETDEV_TX_OK;
132 }
133 EXPORT_SYMBOL_GPL(rt2x00mac_tx);
134
135 int rt2x00mac_start(struct ieee80211_hw *hw)
136 {
137 struct rt2x00_dev *rt2x00dev = hw->priv;
138 int status;
139
140 if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags) ||
141 test_bit(DEVICE_STARTED, &rt2x00dev->flags))
142 return 0;
143
144 /*
145 * If this is the first interface which is added,
146 * we should load the firmware now.
147 */
148 if (test_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags)) {
149 status = rt2x00lib_load_firmware(rt2x00dev);
150 if (status)
151 return status;
152 }
153
154 /*
155 * Initialize the device.
156 */
157 status = rt2x00lib_initialize(rt2x00dev);
158 if (status)
159 return status;
160
161 /*
162 * Enable radio.
163 */
164 status = rt2x00lib_enable_radio(rt2x00dev);
165 if (status) {
166 rt2x00lib_uninitialize(rt2x00dev);
167 return status;
168 }
169
170 __set_bit(DEVICE_STARTED, &rt2x00dev->flags);
171
172 return 0;
173 }
174 EXPORT_SYMBOL_GPL(rt2x00mac_start);
175
176 void rt2x00mac_stop(struct ieee80211_hw *hw)
177 {
178 struct rt2x00_dev *rt2x00dev = hw->priv;
179
180 if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
181 return;
182
183 /*
184 * Perhaps we can add something smarter here,
185 * but for now just disabling the radio should do.
186 */
187 rt2x00lib_disable_radio(rt2x00dev);
188
189 __clear_bit(DEVICE_STARTED, &rt2x00dev->flags);
190 }
191 EXPORT_SYMBOL_GPL(rt2x00mac_stop);
192
193 int rt2x00mac_add_interface(struct ieee80211_hw *hw,
194 struct ieee80211_if_init_conf *conf)
195 {
196 struct rt2x00_dev *rt2x00dev = hw->priv;
197 struct interface *intf = &rt2x00dev->interface;
198
199 /* FIXME: Beaconing is broken in rt2x00. */
200 if (conf->type == IEEE80211_IF_TYPE_IBSS ||
201 conf->type == IEEE80211_IF_TYPE_AP) {
202 ERROR(rt2x00dev,
203 "rt2x00 does not support Adhoc or Master mode");
204 return -EOPNOTSUPP;
205 }
206
207 /*
208 * Don't allow interfaces to be added while
209 * either the device has disappeared or when
210 * another interface is already present.
211 */
212 if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags) ||
213 is_interface_present(intf))
214 return -ENOBUFS;
215
216 intf->id = conf->if_id;
217 intf->type = conf->type;
218 if (conf->type == IEEE80211_IF_TYPE_AP)
219 memcpy(&intf->bssid, conf->mac_addr, ETH_ALEN);
220 memcpy(&intf->mac, conf->mac_addr, ETH_ALEN);
221
222 /*
223 * The MAC adddress must be configured after the device
224 * has been initialized. Otherwise the device can reset
225 * the MAC registers.
226 */
227 rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
228 rt2x00lib_config_type(rt2x00dev, conf->type);
229
230 return 0;
231 }
232 EXPORT_SYMBOL_GPL(rt2x00mac_add_interface);
233
234 void rt2x00mac_remove_interface(struct ieee80211_hw *hw,
235 struct ieee80211_if_init_conf *conf)
236 {
237 struct rt2x00_dev *rt2x00dev = hw->priv;
238 struct interface *intf = &rt2x00dev->interface;
239
240 /*
241 * Don't allow interfaces to be remove while
242 * either the device has disappeared or when
243 * no interface is present.
244 */
245 if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags) ||
246 !is_interface_present(intf))
247 return;
248
249 intf->id = 0;
250 intf->type = INVALID_INTERFACE;
251 memset(&intf->bssid, 0x00, ETH_ALEN);
252 memset(&intf->mac, 0x00, ETH_ALEN);
253
254 /*
255 * Make sure the bssid and mac address registers
256 * are cleared to prevent false ACKing of frames.
257 */
258 rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
259 rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
260 rt2x00lib_config_type(rt2x00dev, intf->type);
261 }
262 EXPORT_SYMBOL_GPL(rt2x00mac_remove_interface);
263
264 int rt2x00mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
265 {
266 struct rt2x00_dev *rt2x00dev = hw->priv;
267
268 /*
269 * Mac80211 might be calling this function while we are trying
270 * to remove the device or perhaps suspending it.
271 */
272 if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
273 return 0;
274
275 /*
276 * Check if we need to disable the radio,
277 * if this is not the case, at least the RX must be disabled.
278 */
279 if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags)) {
280 if (!conf->radio_enabled)
281 rt2x00lib_disable_radio(rt2x00dev);
282 else
283 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
284 }
285
286 rt2x00lib_config(rt2x00dev, conf, 0);
287
288 /*
289 * Reenable RX only if the radio should be on.
290 */
291 if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
292 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
293 else if (conf->radio_enabled)
294 return rt2x00lib_enable_radio(rt2x00dev);
295
296 return 0;
297 }
298 EXPORT_SYMBOL_GPL(rt2x00mac_config);
299
300 int rt2x00mac_config_interface(struct ieee80211_hw *hw, int if_id,
301 struct ieee80211_if_conf *conf)
302 {
303 struct rt2x00_dev *rt2x00dev = hw->priv;
304 struct interface *intf = &rt2x00dev->interface;
305 int status;
306
307 /*
308 * Mac80211 might be calling this function while we are trying
309 * to remove the device or perhaps suspending it.
310 */
311 if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
312 return 0;
313
314 /*
315 * If the given type does not match the configured type,
316 * there has been a problem.
317 */
318 if (conf->type != intf->type)
319 return -EINVAL;
320
321 /*
322 * If the interface does not work in master mode,
323 * then the bssid value in the interface structure
324 * should now be set.
325 */
326 if (conf->type != IEEE80211_IF_TYPE_AP)
327 memcpy(&intf->bssid, conf->bssid, ETH_ALEN);
328 rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
329
330 /*
331 * We only need to initialize the beacon when master mode is enabled.
332 */
333 if (conf->type != IEEE80211_IF_TYPE_AP || !conf->beacon)
334 return 0;
335
336 status = rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw,
337 conf->beacon,
338 conf->beacon_control);
339 if (status)
340 dev_kfree_skb(conf->beacon);
341
342 return status;
343 }
344 EXPORT_SYMBOL_GPL(rt2x00mac_config_interface);
345
346 int rt2x00mac_get_stats(struct ieee80211_hw *hw,
347 struct ieee80211_low_level_stats *stats)
348 {
349 struct rt2x00_dev *rt2x00dev = hw->priv;
350
351 /*
352 * The dot11ACKFailureCount, dot11RTSFailureCount and
353 * dot11RTSSuccessCount are updated in interrupt time.
354 * dot11FCSErrorCount is updated in the link tuner.
355 */
356 memcpy(stats, &rt2x00dev->low_level_stats, sizeof(*stats));
357
358 return 0;
359 }
360 EXPORT_SYMBOL_GPL(rt2x00mac_get_stats);
361
362 int rt2x00mac_get_tx_stats(struct ieee80211_hw *hw,
363 struct ieee80211_tx_queue_stats *stats)
364 {
365 struct rt2x00_dev *rt2x00dev = hw->priv;
366 unsigned int i;
367
368 for (i = 0; i < hw->queues; i++)
369 memcpy(&stats->data[i], &rt2x00dev->tx[i].stats,
370 sizeof(rt2x00dev->tx[i].stats));
371
372 return 0;
373 }
374 EXPORT_SYMBOL_GPL(rt2x00mac_get_tx_stats);
375
376 void rt2x00mac_erp_ie_changed(struct ieee80211_hw *hw, u8 changes,
377 int cts_protection, int preamble)
378 {
379 struct rt2x00_dev *rt2x00dev = hw->priv;
380 int short_preamble;
381 int ack_timeout;
382 int ack_consume_time;
383 int difs;
384
385 /*
386 * We only support changing preamble mode.
387 */
388 if (!(changes & IEEE80211_ERP_CHANGE_PREAMBLE))
389 return;
390
391 short_preamble = !preamble;
392 preamble = !!(preamble) ? PREAMBLE : SHORT_PREAMBLE;
393
394 difs = (hw->conf.flags & IEEE80211_CONF_SHORT_SLOT_TIME) ?
395 SHORT_DIFS : DIFS;
396 ack_timeout = difs + PLCP + preamble + get_duration(ACK_SIZE, 10);
397
398 ack_consume_time = SIFS + PLCP + preamble + get_duration(ACK_SIZE, 10);
399
400 if (short_preamble)
401 __set_bit(CONFIG_SHORT_PREAMBLE, &rt2x00dev->flags);
402 else
403 __clear_bit(CONFIG_SHORT_PREAMBLE, &rt2x00dev->flags);
404
405 rt2x00dev->ops->lib->config_preamble(rt2x00dev, short_preamble,
406 ack_timeout, ack_consume_time);
407 }
408 EXPORT_SYMBOL_GPL(rt2x00mac_erp_ie_changed);
409
410 int rt2x00mac_conf_tx(struct ieee80211_hw *hw, int queue,
411 const struct ieee80211_tx_queue_params *params)
412 {
413 struct rt2x00_dev *rt2x00dev = hw->priv;
414 struct data_ring *ring;
415
416 ring = rt2x00lib_get_ring(rt2x00dev, queue);
417 if (unlikely(!ring))
418 return -EINVAL;
419
420 /*
421 * The passed variables are stored as real value ((2^n)-1).
422 * Ralink registers require to know the bit number 'n'.
423 */
424 if (params->cw_min)
425 ring->tx_params.cw_min = fls(params->cw_min);
426 else
427 ring->tx_params.cw_min = 5; /* cw_min: 2^5 = 32. */
428
429 if (params->cw_max)
430 ring->tx_params.cw_max = fls(params->cw_max);
431 else
432 ring->tx_params.cw_max = 10; /* cw_min: 2^10 = 1024. */
433
434 if (params->aifs)
435 ring->tx_params.aifs = params->aifs;
436 else
437 ring->tx_params.aifs = 2;
438
439 INFO(rt2x00dev,
440 "Configured TX ring %d - CWmin: %d, CWmax: %d, Aifs: %d.\n",
441 queue, ring->tx_params.cw_min, ring->tx_params.cw_max,
442 ring->tx_params.aifs);
443
444 return 0;
445 }
446 EXPORT_SYMBOL_GPL(rt2x00mac_conf_tx);