rt2x00: Add chipset version to chipset debugfs entry
[linux-2.6] / drivers / net / wireless / rt2x00 / 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 #include <linux/kernel.h>
27 #include <linux/module.h>
28
29 #include "rt2x00.h"
30 #include "rt2x00lib.h"
31
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)
36 {
37         struct sk_buff *skb;
38         int size;
39
40         if (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
41                 size = sizeof(struct ieee80211_cts);
42         else
43                 size = sizeof(struct ieee80211_rts);
44
45         skb = dev_alloc_skb(size + rt2x00dev->hw->extra_tx_headroom);
46         if (!skb) {
47                 WARNING(rt2x00dev, "Failed to create RTS/CTS frame.\n");
48                 return NETDEV_TX_BUSY;
49         }
50
51         skb_reserve(skb, rt2x00dev->hw->extra_tx_headroom);
52         skb_put(skb, size);
53
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));
58         else
59                 ieee80211_rts_get(rt2x00dev->hw, rt2x00dev->interface.id,
60                                   frag_skb->data, frag_skb->len, control,
61                                   (struct ieee80211_rts *)(skb->data));
62
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;
66         }
67
68         return NETDEV_TX_OK;
69 }
70
71 int rt2x00mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
72                  struct ieee80211_tx_control *control)
73 {
74         struct rt2x00_dev *rt2x00dev = hw->priv;
75         struct ieee80211_hdr *ieee80211hdr = (struct ieee80211_hdr *)skb->data;
76         struct data_ring *ring;
77         u16 frame_control;
78
79         /*
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.
84          */
85         if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags)) {
86                 ieee80211_stop_queues(hw);
87                 return 0;
88         }
89
90         /*
91          * Determine which ring to put packet on.
92          */
93         ring = rt2x00lib_get_ring(rt2x00dev, control->queue);
94         if (unlikely(!ring)) {
95                 ERROR(rt2x00dev,
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);
100                 return NETDEV_TX_OK;
101         }
102
103         /*
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.
108          */
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;
115
116                 if (rt2x00mac_tx_rts_cts(rt2x00dev, ring, skb, control))
117                         return NETDEV_TX_BUSY;
118         }
119
120         if (rt2x00dev->ops->lib->write_tx_data(rt2x00dev, ring, skb, control))
121                 return NETDEV_TX_BUSY;
122
123         if (rt2x00dev->ops->lib->kick_tx_queue)
124                 rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, control->queue);
125
126         return NETDEV_TX_OK;
127 }
128 EXPORT_SYMBOL_GPL(rt2x00mac_tx);
129
130 int rt2x00mac_start(struct ieee80211_hw *hw)
131 {
132         struct rt2x00_dev *rt2x00dev = hw->priv;
133         int status;
134
135         if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags) ||
136             test_bit(DEVICE_STARTED, &rt2x00dev->flags))
137                 return 0;
138
139         /*
140          * If this is the first interface which is added,
141          * we should load the firmware now.
142          */
143         if (test_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags)) {
144                 status = rt2x00lib_load_firmware(rt2x00dev);
145                 if (status)
146                         return status;
147         }
148
149         /*
150          * Initialize the device.
151          */
152         status = rt2x00lib_initialize(rt2x00dev);
153         if (status)
154                 return status;
155
156         /*
157          * Enable radio.
158          */
159         status = rt2x00lib_enable_radio(rt2x00dev);
160         if (status) {
161                 rt2x00lib_uninitialize(rt2x00dev);
162                 return status;
163         }
164
165         __set_bit(DEVICE_STARTED, &rt2x00dev->flags);
166
167         return 0;
168 }
169 EXPORT_SYMBOL_GPL(rt2x00mac_start);
170
171 void rt2x00mac_stop(struct ieee80211_hw *hw)
172 {
173         struct rt2x00_dev *rt2x00dev = hw->priv;
174
175         if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
176                 return;
177
178         /*
179          * Perhaps we can add something smarter here,
180          * but for now just disabling the radio should do.
181          */
182         rt2x00lib_disable_radio(rt2x00dev);
183
184         __clear_bit(DEVICE_STARTED, &rt2x00dev->flags);
185 }
186 EXPORT_SYMBOL_GPL(rt2x00mac_stop);
187
188 int rt2x00mac_add_interface(struct ieee80211_hw *hw,
189                             struct ieee80211_if_init_conf *conf)
190 {
191         struct rt2x00_dev *rt2x00dev = hw->priv;
192         struct interface *intf = &rt2x00dev->interface;
193
194         /* FIXME: Beaconing is broken in rt2x00. */
195         if (conf->type == IEEE80211_IF_TYPE_IBSS ||
196             conf->type == IEEE80211_IF_TYPE_AP) {
197                 ERROR(rt2x00dev,
198                       "rt2x00 does not support Adhoc or Master mode");
199                 return -EOPNOTSUPP;
200         }
201
202         /*
203          * Don't allow interfaces to be added while
204          * either the device has disappeared or when
205          * another interface is already present.
206          */
207         if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags) ||
208             is_interface_present(intf))
209                 return -ENOBUFS;
210
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);
216
217         /*
218          * The MAC adddress must be configured after the device
219          * has been initialized. Otherwise the device can reset
220          * the MAC registers.
221          */
222         rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
223         rt2x00lib_config_type(rt2x00dev, conf->type);
224
225         return 0;
226 }
227 EXPORT_SYMBOL_GPL(rt2x00mac_add_interface);
228
229 void rt2x00mac_remove_interface(struct ieee80211_hw *hw,
230                                 struct ieee80211_if_init_conf *conf)
231 {
232         struct rt2x00_dev *rt2x00dev = hw->priv;
233         struct interface *intf = &rt2x00dev->interface;
234
235         /*
236          * Don't allow interfaces to be remove while
237          * either the device has disappeared or when
238          * no interface is present.
239          */
240         if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags) ||
241             !is_interface_present(intf))
242                 return;
243
244         intf->id = 0;
245         intf->type = INVALID_INTERFACE;
246         memset(&intf->bssid, 0x00, ETH_ALEN);
247         memset(&intf->mac, 0x00, ETH_ALEN);
248
249         /*
250          * Make sure the bssid and mac address registers
251          * are cleared to prevent false ACKing of frames.
252          */
253         rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
254         rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
255         rt2x00lib_config_type(rt2x00dev, intf->type);
256 }
257 EXPORT_SYMBOL_GPL(rt2x00mac_remove_interface);
258
259 int rt2x00mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
260 {
261         struct rt2x00_dev *rt2x00dev = hw->priv;
262
263         /*
264          * Mac80211 might be calling this function while we are trying
265          * to remove the device or perhaps suspending it.
266          */
267         if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
268                 return 0;
269
270         /*
271          * Check if we need to disable the radio,
272          * if this is not the case, at least the RX must be disabled.
273          */
274         if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags)) {
275                 if (!conf->radio_enabled)
276                         rt2x00lib_disable_radio(rt2x00dev);
277                 else
278                         rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
279         }
280
281         rt2x00lib_config(rt2x00dev, conf, 0);
282
283         /*
284          * Reenable RX only if the radio should be on.
285          */
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);
290
291         return 0;
292 }
293 EXPORT_SYMBOL_GPL(rt2x00mac_config);
294
295 int rt2x00mac_config_interface(struct ieee80211_hw *hw, int if_id,
296                                struct ieee80211_if_conf *conf)
297 {
298         struct rt2x00_dev *rt2x00dev = hw->priv;
299         struct interface *intf = &rt2x00dev->interface;
300         int status;
301
302         /*
303          * Mac80211 might be calling this function while we are trying
304          * to remove the device or perhaps suspending it.
305          */
306         if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
307                 return 0;
308
309         /*
310          * If the given type does not match the configured type,
311          * there has been a problem.
312          */
313         if (conf->type != intf->type)
314                 return -EINVAL;
315
316         /*
317          * If the interface does not work in master mode,
318          * then the bssid value in the interface structure
319          * should now be set.
320          */
321         if (conf->type != IEEE80211_IF_TYPE_AP)
322                 memcpy(&intf->bssid, conf->bssid, ETH_ALEN);
323         rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
324
325         /*
326          * We only need to initialize the beacon when master mode is enabled.
327          */
328         if (conf->type != IEEE80211_IF_TYPE_AP || !conf->beacon)
329                 return 0;
330
331         status = rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw,
332                                                    conf->beacon,
333                                                    conf->beacon_control);
334         if (status)
335                 dev_kfree_skb(conf->beacon);
336
337         return status;
338 }
339 EXPORT_SYMBOL_GPL(rt2x00mac_config_interface);
340
341 int rt2x00mac_get_stats(struct ieee80211_hw *hw,
342                         struct ieee80211_low_level_stats *stats)
343 {
344         struct rt2x00_dev *rt2x00dev = hw->priv;
345
346         /*
347          * The dot11ACKFailureCount, dot11RTSFailureCount and
348          * dot11RTSSuccessCount are updated in interrupt time.
349          * dot11FCSErrorCount is updated in the link tuner.
350          */
351         memcpy(stats, &rt2x00dev->low_level_stats, sizeof(*stats));
352
353         return 0;
354 }
355 EXPORT_SYMBOL_GPL(rt2x00mac_get_stats);
356
357 int rt2x00mac_get_tx_stats(struct ieee80211_hw *hw,
358                            struct ieee80211_tx_queue_stats *stats)
359 {
360         struct rt2x00_dev *rt2x00dev = hw->priv;
361         unsigned int i;
362
363         for (i = 0; i < hw->queues; i++)
364                 memcpy(&stats->data[i], &rt2x00dev->tx[i].stats,
365                        sizeof(rt2x00dev->tx[i].stats));
366
367         return 0;
368 }
369 EXPORT_SYMBOL_GPL(rt2x00mac_get_tx_stats);
370
371 void rt2x00mac_erp_ie_changed(struct ieee80211_hw *hw, u8 changes,
372                               int cts_protection, int preamble)
373 {
374         struct rt2x00_dev *rt2x00dev = hw->priv;
375         int short_preamble;
376         int ack_timeout;
377         int ack_consume_time;
378         int difs;
379
380         /*
381          * We only support changing preamble mode.
382          */
383         if (!(changes & IEEE80211_ERP_CHANGE_PREAMBLE))
384                 return;
385
386         short_preamble = !preamble;
387         preamble = !!(preamble) ? PREAMBLE : SHORT_PREAMBLE;
388
389         difs = (hw->conf.flags & IEEE80211_CONF_SHORT_SLOT_TIME) ?
390                 SHORT_DIFS : DIFS;
391         ack_timeout = difs + PLCP + preamble + get_duration(ACK_SIZE, 10);
392
393         ack_consume_time = SIFS + PLCP + preamble + get_duration(ACK_SIZE, 10);
394
395         if (short_preamble)
396                 __set_bit(CONFIG_SHORT_PREAMBLE, &rt2x00dev->flags);
397         else
398                 __clear_bit(CONFIG_SHORT_PREAMBLE, &rt2x00dev->flags);
399
400         rt2x00dev->ops->lib->config_preamble(rt2x00dev, short_preamble,
401                                              ack_timeout, ack_consume_time);
402 }
403 EXPORT_SYMBOL_GPL(rt2x00mac_erp_ie_changed);
404
405 int rt2x00mac_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 = rt2x00lib_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(rt2x00mac_conf_tx);