blob: 6a5712c6614cca9767fdfa6fbd4acccebaefa04b [file] [log] [blame]
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001/*
Ivo van Doorn811aa9c2008-02-03 15:42:53 +01002 Copyright (C) 2004 - 2008 rt2x00 SourceForge Project
Ivo van Doorn95ea3622007-09-25 17:57:13 -07003 <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 device routines.
24 */
25
Ivo van Doorn95ea3622007-09-25 17:57:13 -070026#include <linux/kernel.h>
27#include <linux/module.h>
28
29#include "rt2x00.h"
30#include "rt2x00lib.h"
31
32/*
Ivo van Doorn95ea3622007-09-25 17:57:13 -070033 * Radio control handlers.
34 */
35int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
36{
37 int status;
38
39 /*
40 * Don't enable the radio twice.
41 * And check if the hardware button has been disabled.
42 */
Ivo van Doorn0262ab02008-08-29 21:04:26 +020043 if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags) ||
44 test_bit(DEVICE_STATE_DISABLED_RADIO_HW, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -070045 return 0;
46
47 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -050048 * Initialize all data queues.
Ivo van Doorn837e7f22008-01-06 23:41:45 +010049 */
Ivo van Doorn798b7ad2008-11-08 15:25:33 +010050 rt2x00queue_init_queues(rt2x00dev);
Ivo van Doorn837e7f22008-01-06 23:41:45 +010051
52 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -070053 * Enable radio.
54 */
Ivo van Doorna2e1d522008-03-31 15:53:44 +020055 status =
56 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_ON);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070057 if (status)
58 return status;
59
Ivo van Doorn2b08da32008-06-03 18:58:56 +020060 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_ON);
61
Ivo van Doorna2e1d522008-03-31 15:53:44 +020062 rt2x00leds_led_radio(rt2x00dev, true);
Ivo van Doorn61c2b682008-04-21 19:01:09 +020063 rt2x00led_led_activity(rt2x00dev, true);
Ivo van Doorna2e1d522008-03-31 15:53:44 +020064
Ivo van Doorn0262ab02008-08-29 21:04:26 +020065 set_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070066
67 /*
68 * Enable RX.
69 */
Ivo van Doorn5cbf8302007-10-06 14:16:09 +020070 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070071
72 /*
73 * Start the TX queues.
74 */
Johannes Berg36d68252008-05-15 12:55:26 +020075 ieee80211_wake_queues(rt2x00dev->hw);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070076
77 return 0;
78}
79
80void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
81{
Ivo van Doorn0262ab02008-08-29 21:04:26 +020082 if (!test_and_clear_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -070083 return;
84
85 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -070086 * Stop the TX queues.
87 */
88 ieee80211_stop_queues(rt2x00dev->hw);
89
90 /*
91 * Disable RX.
92 */
Ivo van Doorn5cbf8302007-10-06 14:16:09 +020093 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070094
95 /*
96 * Disable radio.
97 */
98 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
Ivo van Doorn2b08da32008-06-03 18:58:56 +020099 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_OFF);
Ivo van Doorn61c2b682008-04-21 19:01:09 +0200100 rt2x00led_led_activity(rt2x00dev, false);
Ivo van Doorna2e1d522008-03-31 15:53:44 +0200101 rt2x00leds_led_radio(rt2x00dev, false);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700102}
103
Ivo van Doorn5cbf8302007-10-06 14:16:09 +0200104void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, enum dev_state state)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700105{
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700106 /*
107 * When we are disabling the RX, we should also stop the link tuner.
108 */
Ivo van Doorn5cbf8302007-10-06 14:16:09 +0200109 if (state == STATE_RADIO_RX_OFF)
Ivo van Doorn84e31962008-12-20 10:53:29 +0100110 rt2x00link_stop_tuner(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700111
112 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
113
114 /*
115 * When we are enabling the RX, we should also start the link tuner.
116 */
Ivo van Doorn84e31962008-12-20 10:53:29 +0100117 if (state == STATE_RADIO_RX_ON)
118 rt2x00link_start_tuner(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700119}
120
Johannes Berg4150c572007-09-17 01:29:23 -0400121static void rt2x00lib_packetfilter_scheduled(struct work_struct *work)
122{
123 struct rt2x00_dev *rt2x00dev =
124 container_of(work, struct rt2x00_dev, filter_work);
Ivo van Doorn5886d0d2007-10-06 14:13:38 +0200125
Ivo van Doorn133adf02008-04-04 00:01:43 +0200126 rt2x00dev->ops->lib->config_filter(rt2x00dev, rt2x00dev->packet_filter);
Johannes Berg4150c572007-09-17 01:29:23 -0400127}
128
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100129static void rt2x00lib_intf_scheduled_iter(void *data, u8 *mac,
130 struct ieee80211_vif *vif)
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200131{
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100132 struct rt2x00_dev *rt2x00dev = data;
133 struct rt2x00_intf *intf = vif_to_intf(vif);
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100134 struct ieee80211_bss_conf conf;
135 int delayed_flags;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100136
137 /*
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100138 * Copy all data we need during this action under the protection
139 * of a spinlock. Otherwise race conditions might occur which results
140 * into an invalid configuration.
Johannes Berg471b3ef2007-12-28 14:32:58 +0100141 */
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100142 spin_lock(&intf->lock);
143
Ivo van Doornc3fd7b42008-10-29 17:18:22 +0100144 memcpy(&conf, &vif->bss_conf, sizeof(conf));
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100145 delayed_flags = intf->delayed_flags;
146 intf->delayed_flags = 0;
147
148 spin_unlock(&intf->lock);
149
Ivo van Doorn980dfcb2008-06-25 21:27:00 +0200150 /*
151 * It is possible the radio was disabled while the work had been
152 * scheduled. If that happens we should return here immediately,
153 * note that in the spinlock protected area above the delayed_flags
154 * have been cleared correctly.
155 */
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200156 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
Ivo van Doorn980dfcb2008-06-25 21:27:00 +0200157 return;
158
Ivo van Doornbd88a782008-07-09 15:12:44 +0200159 if (delayed_flags & DELAYED_UPDATE_BEACON)
160 rt2x00queue_update_beacon(rt2x00dev, vif);
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100161
Ivo van Doorn72810372008-03-09 22:46:18 +0100162 if (delayed_flags & DELAYED_CONFIG_ERP)
Ivo van Doorn980dfcb2008-06-25 21:27:00 +0200163 rt2x00lib_config_erp(rt2x00dev, intf, &conf);
Ivo van Doorna2e1d522008-03-31 15:53:44 +0200164
165 if (delayed_flags & DELAYED_LED_ASSOC)
166 rt2x00leds_led_assoc(rt2x00dev, !!rt2x00dev->intf_associated);
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100167}
168
169static void rt2x00lib_intf_scheduled(struct work_struct *work)
170{
171 struct rt2x00_dev *rt2x00dev =
172 container_of(work, struct rt2x00_dev, intf_work);
173
174 /*
175 * Iterate over each interface and perform the
176 * requested configurations.
177 */
178 ieee80211_iterate_active_interfaces(rt2x00dev->hw,
179 rt2x00lib_intf_scheduled_iter,
180 rt2x00dev);
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200181}
182
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700183/*
184 * Interrupt context handlers.
185 */
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100186static void rt2x00lib_beacondone_iter(void *data, u8 *mac,
187 struct ieee80211_vif *vif)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700188{
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200189 struct rt2x00_dev *rt2x00dev = data;
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100190 struct rt2x00_intf *intf = vif_to_intf(vif);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700191
Johannes Berg05c914f2008-09-11 00:01:58 +0200192 if (vif->type != NL80211_IFTYPE_AP &&
Andrey Yurovskya07dbea2008-12-20 10:55:34 +0100193 vif->type != NL80211_IFTYPE_ADHOC &&
194 vif->type != NL80211_IFTYPE_MESH_POINT)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700195 return;
196
Gertjan van Wingerdec95edf52008-06-16 19:55:18 +0200197 /*
198 * Clean up the beacon skb.
199 */
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200200 rt2x00queue_free_skb(rt2x00dev, intf->beacon->skb);
Gertjan van Wingerdec95edf52008-06-16 19:55:18 +0200201 intf->beacon->skb = NULL;
202
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100203 spin_lock(&intf->lock);
204 intf->delayed_flags |= DELAYED_UPDATE_BEACON;
205 spin_unlock(&intf->lock);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700206}
207
208void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
209{
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200210 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700211 return;
212
Ivo van Doorn633257d2008-05-23 18:14:02 +0200213 ieee80211_iterate_active_interfaces_atomic(rt2x00dev->hw,
214 rt2x00lib_beacondone_iter,
215 rt2x00dev);
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100216
Ivo van Doorn8e260c22008-07-04 13:41:31 +0200217 schedule_work(&rt2x00dev->intf_work);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700218}
219EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
220
Ivo van Doorn181d6902008-02-05 16:42:23 -0500221void rt2x00lib_txdone(struct queue_entry *entry,
222 struct txdone_entry_desc *txdesc)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700223{
Ivo van Doorn181d6902008-02-05 16:42:23 -0500224 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
Johannes Berge039fa42008-05-15 12:55:29 +0200225 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
Johannes Berge6a98542008-10-21 12:40:02 +0200226 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200227 enum data_queue_qid qid = skb_get_queue_mapping(entry->skb);
Johannes Berge6a98542008-10-21 12:40:02 +0200228 u8 rate_idx, rate_flags;
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200229
230 /*
231 * Unmap the skb.
232 */
233 rt2x00queue_unmap_skb(rt2x00dev, entry->skb);
Johannes Berge039fa42008-05-15 12:55:29 +0200234
235 /*
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200236 * If the IV/EIV data was stripped from the frame before it was
237 * passed to the hardware, we should now reinsert it again because
238 * mac80211 will expect the the same data to be present it the
239 * frame as it was passed to us.
240 */
241 if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags))
242 rt2x00crypto_tx_insert_iv(entry->skb);
243
244 /*
Johannes Berge039fa42008-05-15 12:55:29 +0200245 * Send frame to debugfs immediately, after this call is completed
246 * we are going to overwrite the skb->cb array.
247 */
248 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TXDONE, entry->skb);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700249
250 /*
251 * Update TX statistics.
252 */
Ivo van Doornfb55f4d2008-05-10 13:42:06 +0200253 rt2x00dev->link.qual.tx_success +=
254 test_bit(TXDONE_SUCCESS, &txdesc->flags);
255 rt2x00dev->link.qual.tx_failed +=
John W. Linvillecb14cb72008-06-10 09:06:52 -0400256 test_bit(TXDONE_FAILURE, &txdesc->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700257
Johannes Berge6a98542008-10-21 12:40:02 +0200258 rate_idx = skbdesc->tx_rate_idx;
259 rate_flags = skbdesc->tx_rate_flags;
260
Ivo van Doorn181d6902008-02-05 16:42:23 -0500261 /*
262 * Initialize TX status
263 */
Johannes Berge039fa42008-05-15 12:55:29 +0200264 memset(&tx_info->status, 0, sizeof(tx_info->status));
265 tx_info->status.ack_signal = 0;
Johannes Berge6a98542008-10-21 12:40:02 +0200266 tx_info->status.rates[0].idx = rate_idx;
267 tx_info->status.rates[0].flags = rate_flags;
268 tx_info->status.rates[0].count = txdesc->retry + 1;
269 tx_info->status.rates[1].idx = -1; /* terminate */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500270
Johannes Berge039fa42008-05-15 12:55:29 +0200271 if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK)) {
Ivo van Doornfb55f4d2008-05-10 13:42:06 +0200272 if (test_bit(TXDONE_SUCCESS, &txdesc->flags))
Johannes Berge039fa42008-05-15 12:55:29 +0200273 tx_info->flags |= IEEE80211_TX_STAT_ACK;
Ivo van Doornfb55f4d2008-05-10 13:42:06 +0200274 else if (test_bit(TXDONE_FAILURE, &txdesc->flags))
Ivo van Doorn181d6902008-02-05 16:42:23 -0500275 rt2x00dev->low_level_stats.dot11ACKFailureCount++;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700276 }
277
Johannes Berge6a98542008-10-21 12:40:02 +0200278 if (rate_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
Ivo van Doornfb55f4d2008-05-10 13:42:06 +0200279 if (test_bit(TXDONE_SUCCESS, &txdesc->flags))
Ivo van Doorn181d6902008-02-05 16:42:23 -0500280 rt2x00dev->low_level_stats.dot11RTSSuccessCount++;
Ivo van Doornfb55f4d2008-05-10 13:42:06 +0200281 else if (test_bit(TXDONE_FAILURE, &txdesc->flags))
Ivo van Doorn181d6902008-02-05 16:42:23 -0500282 rt2x00dev->low_level_stats.dot11RTSFailureCount++;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700283 }
284
285 /*
Johannes Berge039fa42008-05-15 12:55:29 +0200286 * Only send the status report to mac80211 when TX status was
287 * requested by it. If this was a extra frame coming through
288 * a mac80211 library call (RTS/CTS) then we should not send the
289 * status report back.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700290 */
Johannes Berge039fa42008-05-15 12:55:29 +0200291 if (tx_info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
292 ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb);
Ivo van Doornbaf26a72008-02-17 17:32:08 +0100293 else
Ivo van Doornfb55f4d2008-05-10 13:42:06 +0200294 dev_kfree_skb_irq(entry->skb);
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200295
296 /*
297 * Make this entry available for reuse.
298 */
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700299 entry->skb = NULL;
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200300 entry->flags = 0;
301
Ivo van Doorn798b7ad2008-11-08 15:25:33 +0100302 rt2x00dev->ops->lib->clear_entry(entry);
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200303
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200304 clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200305 rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
306
307 /*
308 * If the data queue was below the threshold before the txdone
309 * handler we must make sure the packet queue in the mac80211 stack
310 * is reenabled when the txdone handler has finished.
311 */
312 if (!rt2x00queue_threshold(entry->queue))
313 ieee80211_wake_queue(rt2x00dev->hw, qid);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700314}
315EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
316
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200317void rt2x00lib_rxdone(struct rt2x00_dev *rt2x00dev,
318 struct queue_entry *entry)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700319{
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200320 struct rxdone_entry_desc rxdesc;
321 struct sk_buff *skb;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700322 struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
Johannes Berg8318d782008-01-24 19:38:38 +0100323 struct ieee80211_supported_band *sband;
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100324 const struct rt2x00_rate *rate;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200325 unsigned int header_length;
Gertjan van Wingerde239c2492008-06-06 22:54:12 +0200326 unsigned int align;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700327 unsigned int i;
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100328 int idx = -1;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700329
330 /*
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200331 * Allocate a new sk_buffer. If no new buffer available, drop the
332 * received frame and reuse the existing buffer.
333 */
334 skb = rt2x00queue_alloc_rxskb(rt2x00dev, entry);
335 if (!skb)
336 return;
337
338 /*
339 * Unmap the skb.
340 */
341 rt2x00queue_unmap_skb(rt2x00dev, entry->skb);
342
343 /*
344 * Extract the RXD details.
345 */
346 memset(&rxdesc, 0, sizeof(rxdesc));
347 rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700348
349 /*
Gertjan van Wingerde239c2492008-06-06 22:54:12 +0200350 * The data behind the ieee80211 header must be
Gertjan van Wingerdea9f853d2008-06-07 16:57:09 +0200351 * aligned on a 4 byte boundary.
Gertjan van Wingerde239c2492008-06-06 22:54:12 +0200352 */
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200353 header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
354 align = ((unsigned long)(entry->skb->data + header_length)) & 3;
Gertjan van Wingerde239c2492008-06-06 22:54:12 +0200355
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200356 /*
357 * Hardware might have stripped the IV/EIV/ICV data,
358 * in that case it is possible that the data was
359 * provided seperately (through hardware descriptor)
360 * in which case we should reinsert the data into the frame.
361 */
Ivo van Doorn74415ed2008-12-02 22:50:33 +0100362 if ((rxdesc.dev_flags & RXDONE_CRYPTO_IV) &&
363 (rxdesc.flags & RX_FLAG_IV_STRIPPED)) {
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200364 rt2x00crypto_rx_insert_iv(entry->skb, align,
365 header_length, &rxdesc);
366 } else if (align) {
Gertjan van Wingerde239c2492008-06-06 22:54:12 +0200367 skb_push(entry->skb, align);
368 /* Move entire frame in 1 command */
369 memmove(entry->skb->data, entry->skb->data + align,
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200370 rxdesc.size);
Gertjan van Wingerde239c2492008-06-06 22:54:12 +0200371 }
372
373 /* Update data pointers, trim buffer to correct size */
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200374 skb_trim(entry->skb, rxdesc.size);
Gertjan van Wingerde239c2492008-06-06 22:54:12 +0200375
376 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700377 * Update RX statistics.
378 */
Johannes Berg8318d782008-01-24 19:38:38 +0100379 sband = &rt2x00dev->bands[rt2x00dev->curr_band];
380 for (i = 0; i < sband->n_bitrates; i++) {
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100381 rate = rt2x00_get_rate(sband->bitrates[i].hw_value);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700382
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200383 if (((rxdesc.dev_flags & RXDONE_SIGNAL_PLCP) &&
384 (rate->plcp == rxdesc.signal)) ||
Ivo van Doorn6c6aa3c2008-08-29 21:07:16 +0200385 ((rxdesc.dev_flags & RXDONE_SIGNAL_BITRATE) &&
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200386 (rate->bitrate == rxdesc.signal))) {
Johannes Berg8318d782008-01-24 19:38:38 +0100387 idx = i;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700388 break;
389 }
390 }
391
Ivo van Doorn866a0502008-03-25 14:12:45 +0100392 if (idx < 0) {
393 WARNING(rt2x00dev, "Frame received with unrecognized signal,"
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200394 "signal=0x%.2x, plcp=%d.\n", rxdesc.signal,
395 !!(rxdesc.dev_flags & RXDONE_SIGNAL_PLCP));
Ivo van Doorn866a0502008-03-25 14:12:45 +0100396 idx = 0;
397 }
398
Mattias Nissler61af43c2007-11-27 21:50:26 +0100399 /*
Ivo van Doorn84e31962008-12-20 10:53:29 +0100400 * Update extra components
Mattias Nissler61af43c2007-11-27 21:50:26 +0100401 */
Ivo van Doorn84e31962008-12-20 10:53:29 +0100402 rt2x00link_update_stats(rt2x00dev, entry->skb, &rxdesc);
403 rt2x00debug_update_crypto(rt2x00dev, &rxdesc);
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200404
Ivo van Doornae73e582008-07-04 16:14:59 +0200405 rx_status->mactime = rxdesc.timestamp;
Johannes Berg8318d782008-01-24 19:38:38 +0100406 rx_status->rate_idx = idx;
Ivo van Doorn84e31962008-12-20 10:53:29 +0100407 rx_status->qual = rt2x00link_calculate_signal(rt2x00dev, rxdesc.rssi);
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200408 rx_status->signal = rxdesc.rssi;
409 rx_status->flag = rxdesc.flags;
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200410 rx_status->antenna = rt2x00dev->link.ant.active.rx;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700411
412 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500413 * Send frame to mac80211 & debugfs.
414 * mac80211 will clean up the skb structure.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700415 */
Ivo van Doorn5a6e5992008-05-10 13:41:32 +0200416 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_RXDONE, entry->skb);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500417 ieee80211_rx_irqsafe(rt2x00dev->hw, entry->skb, rx_status);
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200418
419 /*
420 * Replace the skb with the freshly allocated one.
421 */
422 entry->skb = skb;
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200423 entry->flags = 0;
424
Ivo van Doorn798b7ad2008-11-08 15:25:33 +0100425 rt2x00dev->ops->lib->clear_entry(entry);
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200426
427 rt2x00queue_index_inc(entry->queue, Q_INDEX);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700428}
429EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
430
431/*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700432 * Driver initialization handlers.
433 */
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100434const struct rt2x00_rate rt2x00_supported_rates[12] = {
435 {
Ivo van Doorn3d8606a2008-11-02 00:36:40 +0100436 .flags = DEV_RATE_CCK,
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100437 .bitrate = 10,
Ivo van Doornaa776722008-03-09 22:48:08 +0100438 .ratemask = BIT(0),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100439 .plcp = 0x00,
440 },
441 {
Ivo van Doorn3d8606a2008-11-02 00:36:40 +0100442 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100443 .bitrate = 20,
Ivo van Doornaa776722008-03-09 22:48:08 +0100444 .ratemask = BIT(1),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100445 .plcp = 0x01,
446 },
447 {
Ivo van Doorn3d8606a2008-11-02 00:36:40 +0100448 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100449 .bitrate = 55,
Ivo van Doornaa776722008-03-09 22:48:08 +0100450 .ratemask = BIT(2),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100451 .plcp = 0x02,
452 },
453 {
Ivo van Doorn3d8606a2008-11-02 00:36:40 +0100454 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100455 .bitrate = 110,
Ivo van Doornaa776722008-03-09 22:48:08 +0100456 .ratemask = BIT(3),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100457 .plcp = 0x03,
458 },
459 {
Ivo van Doorn3d8606a2008-11-02 00:36:40 +0100460 .flags = DEV_RATE_OFDM,
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100461 .bitrate = 60,
Ivo van Doornaa776722008-03-09 22:48:08 +0100462 .ratemask = BIT(4),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100463 .plcp = 0x0b,
464 },
465 {
466 .flags = DEV_RATE_OFDM,
467 .bitrate = 90,
Ivo van Doornaa776722008-03-09 22:48:08 +0100468 .ratemask = BIT(5),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100469 .plcp = 0x0f,
470 },
471 {
Ivo van Doorn3d8606a2008-11-02 00:36:40 +0100472 .flags = DEV_RATE_OFDM,
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100473 .bitrate = 120,
Ivo van Doornaa776722008-03-09 22:48:08 +0100474 .ratemask = BIT(6),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100475 .plcp = 0x0a,
476 },
477 {
478 .flags = DEV_RATE_OFDM,
479 .bitrate = 180,
Ivo van Doornaa776722008-03-09 22:48:08 +0100480 .ratemask = BIT(7),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100481 .plcp = 0x0e,
482 },
483 {
Ivo van Doorn3d8606a2008-11-02 00:36:40 +0100484 .flags = DEV_RATE_OFDM,
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100485 .bitrate = 240,
Ivo van Doornaa776722008-03-09 22:48:08 +0100486 .ratemask = BIT(8),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100487 .plcp = 0x09,
488 },
489 {
490 .flags = DEV_RATE_OFDM,
491 .bitrate = 360,
Ivo van Doornaa776722008-03-09 22:48:08 +0100492 .ratemask = BIT(9),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100493 .plcp = 0x0d,
494 },
495 {
496 .flags = DEV_RATE_OFDM,
497 .bitrate = 480,
Ivo van Doornaa776722008-03-09 22:48:08 +0100498 .ratemask = BIT(10),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100499 .plcp = 0x08,
500 },
501 {
502 .flags = DEV_RATE_OFDM,
503 .bitrate = 540,
Ivo van Doornaa776722008-03-09 22:48:08 +0100504 .ratemask = BIT(11),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100505 .plcp = 0x0c,
506 },
507};
508
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700509static void rt2x00lib_channel(struct ieee80211_channel *entry,
510 const int channel, const int tx_power,
511 const int value)
512{
Ivo van Doornf2a3c7f2008-02-03 15:52:45 +0100513 entry->center_freq = ieee80211_channel_to_frequency(channel);
Johannes Berg8318d782008-01-24 19:38:38 +0100514 entry->hw_value = value;
515 entry->max_power = tx_power;
516 entry->max_antenna_gain = 0xff;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700517}
518
519static void rt2x00lib_rate(struct ieee80211_rate *entry,
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100520 const u16 index, const struct rt2x00_rate *rate)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700521{
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100522 entry->flags = 0;
523 entry->bitrate = rate->bitrate;
Ivo van Doorn3ea96462009-01-04 17:33:25 +0100524 entry->hw_value =index;
525 entry->hw_value_short = index;
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100526
Ivo van Doorn3ea96462009-01-04 17:33:25 +0100527 if (rate->flags & DEV_RATE_SHORT_PREAMBLE)
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100528 entry->flags |= IEEE80211_RATE_SHORT_PREAMBLE;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700529}
530
531static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
532 struct hw_mode_spec *spec)
533{
534 struct ieee80211_hw *hw = rt2x00dev->hw;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700535 struct ieee80211_channel *channels;
536 struct ieee80211_rate *rates;
Ivo van Doorn31562e82008-02-17 17:35:05 +0100537 unsigned int num_rates;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700538 unsigned int i;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700539
Ivo van Doorn31562e82008-02-17 17:35:05 +0100540 num_rates = 0;
541 if (spec->supported_rates & SUPPORT_RATE_CCK)
542 num_rates += 4;
543 if (spec->supported_rates & SUPPORT_RATE_OFDM)
544 num_rates += 8;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700545
546 channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
547 if (!channels)
Johannes Berg8318d782008-01-24 19:38:38 +0100548 return -ENOMEM;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700549
Ivo van Doorn31562e82008-02-17 17:35:05 +0100550 rates = kzalloc(sizeof(*rates) * num_rates, GFP_KERNEL);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700551 if (!rates)
552 goto exit_free_channels;
553
554 /*
555 * Initialize Rate list.
556 */
Ivo van Doorn31562e82008-02-17 17:35:05 +0100557 for (i = 0; i < num_rates; i++)
Ivo van Doorn8f5fa7f2008-02-03 15:54:34 +0100558 rt2x00lib_rate(&rates[i], i, rt2x00_get_rate(i));
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700559
560 /*
561 * Initialize Channel list.
562 */
563 for (i = 0; i < spec->num_channels; i++) {
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700564 rt2x00lib_channel(&channels[i],
Ivo van Doorn8c5e7a52008-08-04 16:38:47 +0200565 spec->channels[i].channel,
566 spec->channels_info[i].tx_power1, i);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700567 }
568
569 /*
Ivo van Doorn31562e82008-02-17 17:35:05 +0100570 * Intitialize 802.11b, 802.11g
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700571 * Rates: CCK, OFDM.
Johannes Berg8318d782008-01-24 19:38:38 +0100572 * Channels: 2.4 GHz
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700573 */
Gertjan van Wingerde47ac2682008-02-17 17:35:55 +0100574 if (spec->supported_bands & SUPPORT_BAND_2GHZ) {
Ivo van Doorn31562e82008-02-17 17:35:05 +0100575 rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_channels = 14;
576 rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_bitrates = num_rates;
577 rt2x00dev->bands[IEEE80211_BAND_2GHZ].channels = channels;
578 rt2x00dev->bands[IEEE80211_BAND_2GHZ].bitrates = rates;
579 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
580 &rt2x00dev->bands[IEEE80211_BAND_2GHZ];
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700581 }
582
583 /*
584 * Intitialize 802.11a
585 * Rates: OFDM.
586 * Channels: OFDM, UNII, HiperLAN2.
587 */
Gertjan van Wingerde47ac2682008-02-17 17:35:55 +0100588 if (spec->supported_bands & SUPPORT_BAND_5GHZ) {
Ivo van Doorn31562e82008-02-17 17:35:05 +0100589 rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_channels =
590 spec->num_channels - 14;
591 rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_bitrates =
592 num_rates - 4;
593 rt2x00dev->bands[IEEE80211_BAND_5GHZ].channels = &channels[14];
594 rt2x00dev->bands[IEEE80211_BAND_5GHZ].bitrates = &rates[4];
595 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
596 &rt2x00dev->bands[IEEE80211_BAND_5GHZ];
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700597 }
598
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700599 return 0;
600
Johannes Berg8318d782008-01-24 19:38:38 +0100601 exit_free_channels:
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700602 kfree(channels);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700603 ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
604 return -ENOMEM;
605}
606
607static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
608{
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200609 if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700610 ieee80211_unregister_hw(rt2x00dev->hw);
611
Johannes Berg8318d782008-01-24 19:38:38 +0100612 if (likely(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ])) {
613 kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->channels);
614 kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->bitrates);
615 rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
616 rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700617 }
Ivo van Doorn8c5e7a52008-08-04 16:38:47 +0200618
619 kfree(rt2x00dev->spec.channels_info);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700620}
621
622static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
623{
624 struct hw_mode_spec *spec = &rt2x00dev->spec;
625 int status;
626
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200627 if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
628 return 0;
629
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700630 /*
631 * Initialize HW modes.
632 */
633 status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
634 if (status)
635 return status;
636
637 /*
Gertjan van Wingerde61448f82008-05-10 13:43:33 +0200638 * Initialize HW fields.
639 */
640 rt2x00dev->hw->queues = rt2x00dev->ops->tx_queues;
641
642 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700643 * Register HW.
644 */
645 status = ieee80211_register_hw(rt2x00dev->hw);
646 if (status) {
647 rt2x00lib_remove_hw(rt2x00dev);
648 return status;
649 }
650
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200651 set_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700652
653 return 0;
654}
655
656/*
657 * Initialization/uninitialization handlers.
658 */
Ivo van Doorne37ea212008-01-06 23:40:07 +0100659static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700660{
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200661 if (!test_and_clear_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700662 return;
663
664 /*
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100665 * Unregister extra components.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700666 */
667 rt2x00rfkill_unregister(rt2x00dev);
668
669 /*
670 * Allow the HW to uninitialize.
671 */
672 rt2x00dev->ops->lib->uninitialize(rt2x00dev);
673
674 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500675 * Free allocated queue entries.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700676 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500677 rt2x00queue_uninitialize(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700678}
679
Ivo van Doorne37ea212008-01-06 23:40:07 +0100680static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700681{
682 int status;
683
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200684 if (test_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700685 return 0;
686
687 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500688 * Allocate all queue entries.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700689 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500690 status = rt2x00queue_initialize(rt2x00dev);
691 if (status)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700692 return status;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700693
694 /*
695 * Initialize the device.
696 */
697 status = rt2x00dev->ops->lib->initialize(rt2x00dev);
Ivo van Doorned499982008-05-05 17:23:47 +0200698 if (status) {
699 rt2x00queue_uninitialize(rt2x00dev);
700 return status;
701 }
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700702
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200703 set_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700704
705 /*
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100706 * Register the extra components.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700707 */
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100708 rt2x00rfkill_register(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700709
710 return 0;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700711}
712
Ivo van Doorne37ea212008-01-06 23:40:07 +0100713int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
714{
715 int retval;
716
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200717 if (test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
Ivo van Doorne37ea212008-01-06 23:40:07 +0100718 return 0;
719
720 /*
721 * If this is the first interface which is added,
722 * we should load the firmware now.
723 */
Ivo van Doorn9404ef32008-02-03 15:48:38 +0100724 retval = rt2x00lib_load_firmware(rt2x00dev);
725 if (retval)
726 return retval;
Ivo van Doorne37ea212008-01-06 23:40:07 +0100727
728 /*
729 * Initialize the device.
730 */
731 retval = rt2x00lib_initialize(rt2x00dev);
732 if (retval)
733 return retval;
734
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100735 rt2x00dev->intf_ap_count = 0;
736 rt2x00dev->intf_sta_count = 0;
737 rt2x00dev->intf_associated = 0;
738
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200739 set_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags);
Ivo van Doorne37ea212008-01-06 23:40:07 +0100740
741 return 0;
742}
743
744void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
745{
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200746 if (!test_and_clear_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
Ivo van Doorne37ea212008-01-06 23:40:07 +0100747 return;
748
749 /*
750 * Perhaps we can add something smarter here,
751 * but for now just disabling the radio should do.
752 */
753 rt2x00lib_disable_radio(rt2x00dev);
754
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100755 rt2x00dev->intf_ap_count = 0;
756 rt2x00dev->intf_sta_count = 0;
757 rt2x00dev->intf_associated = 0;
Ivo van Doorne37ea212008-01-06 23:40:07 +0100758}
759
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700760/*
761 * driver allocation handlers.
762 */
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700763int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
764{
765 int retval = -ENOMEM;
766
Ivo van Doorn8ff48a82008-11-09 23:40:46 +0100767 mutex_init(&rt2x00dev->csr_mutex);
768
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700769 /*
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100770 * Make room for rt2x00_intf inside the per-interface
771 * structure ieee80211_vif.
772 */
773 rt2x00dev->hw->vif_data_size = sizeof(struct rt2x00_intf);
774
Ivo van Doorn3514a442008-10-29 17:18:46 +0100775 /*
776 * Determine which operating modes are supported, all modes
777 * which require beaconing, depend on the availability of
778 * beacon entries.
779 */
780 rt2x00dev->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
781 if (rt2x00dev->ops->bcn->entry_num > 0)
782 rt2x00dev->hw->wiphy->interface_modes |=
783 BIT(NL80211_IFTYPE_ADHOC) |
Andrey Yurovskya07dbea2008-12-20 10:55:34 +0100784 BIT(NL80211_IFTYPE_AP) |
785 BIT(NL80211_IFTYPE_MESH_POINT);
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700786
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100787 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700788 * Let the driver probe the device to detect the capabilities.
789 */
790 retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
791 if (retval) {
792 ERROR(rt2x00dev, "Failed to allocate device.\n");
793 goto exit;
794 }
795
796 /*
797 * Initialize configuration work.
798 */
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100799 INIT_WORK(&rt2x00dev->intf_work, rt2x00lib_intf_scheduled);
Johannes Berg4150c572007-09-17 01:29:23 -0400800 INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700801
802 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500803 * Allocate queue array.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700804 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500805 retval = rt2x00queue_allocate(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700806 if (retval)
807 goto exit;
808
809 /*
810 * Initialize ieee80211 structure.
811 */
812 retval = rt2x00lib_probe_hw(rt2x00dev);
813 if (retval) {
814 ERROR(rt2x00dev, "Failed to initialize hw.\n");
815 goto exit;
816 }
817
818 /*
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100819 * Register extra components.
Ivo van Doorna9450b72008-02-03 15:53:40 +0100820 */
Ivo van Doorn84e31962008-12-20 10:53:29 +0100821 rt2x00link_register(rt2x00dev);
Ivo van Doorna9450b72008-02-03 15:53:40 +0100822 rt2x00leds_register(rt2x00dev);
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100823 rt2x00rfkill_allocate(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700824 rt2x00debug_register(rt2x00dev);
825
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200826 set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
Ivo van Doorn066cb632007-09-25 20:55:39 +0200827
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700828 return 0;
829
830exit:
831 rt2x00lib_remove_dev(rt2x00dev);
832
833 return retval;
834}
835EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
836
837void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
838{
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200839 clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
Ivo van Doorn066cb632007-09-25 20:55:39 +0200840
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700841 /*
842 * Disable radio.
843 */
844 rt2x00lib_disable_radio(rt2x00dev);
845
846 /*
847 * Uninitialize device.
848 */
849 rt2x00lib_uninitialize(rt2x00dev);
850
851 /*
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100852 * Free extra components
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700853 */
854 rt2x00debug_deregister(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700855 rt2x00rfkill_free(rt2x00dev);
Ivo van Doorna9450b72008-02-03 15:53:40 +0100856 rt2x00leds_unregister(rt2x00dev);
857
858 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700859 * Free ieee80211_hw memory.
860 */
861 rt2x00lib_remove_hw(rt2x00dev);
862
863 /*
864 * Free firmware image.
865 */
866 rt2x00lib_free_firmware(rt2x00dev);
867
868 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500869 * Free queue structures.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700870 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500871 rt2x00queue_free(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700872}
873EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
874
875/*
876 * Device state handlers
877 */
878#ifdef CONFIG_PM
879int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
880{
881 int retval;
882
883 NOTICE(rt2x00dev, "Going to sleep.\n");
Ivo van Doorn066cb632007-09-25 20:55:39 +0200884
885 /*
886 * Only continue if mac80211 has open interfaces.
887 */
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200888 if (!test_and_clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) ||
889 !test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
Ivo van Doorn066cb632007-09-25 20:55:39 +0200890 goto exit;
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200891
892 set_bit(DEVICE_STATE_STARTED_SUSPEND, &rt2x00dev->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700893
894 /*
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100895 * Disable radio.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700896 */
Ivo van Doorne37ea212008-01-06 23:40:07 +0100897 rt2x00lib_stop(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700898 rt2x00lib_uninitialize(rt2x00dev);
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100899
900 /*
901 * Suspend/disable extra components.
902 */
Ivo van Doorna9450b72008-02-03 15:53:40 +0100903 rt2x00leds_suspend(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700904 rt2x00debug_deregister(rt2x00dev);
905
Ivo van Doorn066cb632007-09-25 20:55:39 +0200906exit:
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700907 /*
Ivo van Doorn98963222008-03-27 17:15:24 +0100908 * Set device mode to sleep for power management,
909 * on some hardware this call seems to consistently fail.
910 * From the specifications it is hard to tell why it fails,
911 * and if this is a "bad thing".
912 * Overall it is safe to just ignore the failure and
913 * continue suspending. The only downside is that the
914 * device will not be in optimal power save mode, but with
915 * the radio and the other components already disabled the
916 * device is as good as disabled.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700917 */
918 retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
919 if (retval)
Ivo van Doorn98963222008-03-27 17:15:24 +0100920 WARNING(rt2x00dev, "Device failed to enter sleep state, "
921 "continue suspending.\n");
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700922
923 return 0;
924}
925EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
926
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100927static void rt2x00lib_resume_intf(void *data, u8 *mac,
928 struct ieee80211_vif *vif)
929{
930 struct rt2x00_dev *rt2x00dev = data;
931 struct rt2x00_intf *intf = vif_to_intf(vif);
932
933 spin_lock(&intf->lock);
934
935 rt2x00lib_config_intf(rt2x00dev, intf,
936 vif->type, intf->mac, intf->bssid);
937
938
939 /*
Andrey Yurovskya07dbea2008-12-20 10:55:34 +0100940 * AP, Ad-hoc, and Mesh Point mode require a new beacon update.
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100941 */
Johannes Berg05c914f2008-09-11 00:01:58 +0200942 if (vif->type == NL80211_IFTYPE_AP ||
Andrey Yurovskya07dbea2008-12-20 10:55:34 +0100943 vif->type == NL80211_IFTYPE_ADHOC ||
944 vif->type == NL80211_IFTYPE_MESH_POINT)
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100945 intf->delayed_flags |= DELAYED_UPDATE_BEACON;
946
947 spin_unlock(&intf->lock);
948}
949
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700950int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
951{
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700952 int retval;
953
954 NOTICE(rt2x00dev, "Waking up.\n");
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700955
956 /*
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100957 * Restore/enable extra components.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700958 */
959 rt2x00debug_register(rt2x00dev);
Ivo van Doorna9450b72008-02-03 15:53:40 +0100960 rt2x00leds_resume(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700961
962 /*
Ivo van Doorn6d7f9872007-10-06 14:12:42 +0200963 * Only continue if mac80211 had open interfaces.
Ivo van Doorn066cb632007-09-25 20:55:39 +0200964 */
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200965 if (!test_and_clear_bit(DEVICE_STATE_STARTED_SUSPEND, &rt2x00dev->flags))
Ivo van Doorn066cb632007-09-25 20:55:39 +0200966 return 0;
967
968 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700969 * Reinitialize device and all active interfaces.
970 */
Ivo van Doorne37ea212008-01-06 23:40:07 +0100971 retval = rt2x00lib_start(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700972 if (retval)
973 goto exit;
974
975 /*
976 * Reconfigure device.
977 */
Johannes Berge8975582008-10-09 12:18:51 +0200978 retval = rt2x00mac_config(rt2x00dev->hw, ~0);
Ivo van Doorn74c0ee92008-07-21 11:52:44 +0200979 if (retval)
980 goto exit;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700981
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100982 /*
983 * Iterator over each active interface to
984 * reconfigure the hardware.
985 */
986 ieee80211_iterate_active_interfaces(rt2x00dev->hw,
987 rt2x00lib_resume_intf, rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700988
989 /*
Ivo van Doorne37ea212008-01-06 23:40:07 +0100990 * We are ready again to receive requests from mac80211.
991 */
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200992 set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
Ivo van Doorne37ea212008-01-06 23:40:07 +0100993
994 /*
Ivo van Doorn066cb632007-09-25 20:55:39 +0200995 * It is possible that during that mac80211 has attempted
996 * to send frames while we were suspending or resuming.
997 * In that case we have disabled the TX queue and should
998 * now enable it again
999 */
Johannes Berg36d68252008-05-15 12:55:26 +02001000 ieee80211_wake_queues(rt2x00dev->hw);
Ivo van Doorn066cb632007-09-25 20:55:39 +02001001
1002 /*
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +01001003 * During interface iteration we might have changed the
1004 * delayed_flags, time to handles the event by calling
1005 * the work handler directly.
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001006 */
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +01001007 rt2x00lib_intf_scheduled(&rt2x00dev->intf_work);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001008
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001009 return 0;
1010
1011exit:
Ivo van Doorn0262ab02008-08-29 21:04:26 +02001012 rt2x00lib_stop(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001013 rt2x00lib_uninitialize(rt2x00dev);
1014 rt2x00debug_deregister(rt2x00dev);
1015
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001016 return retval;
1017}
1018EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1019#endif /* CONFIG_PM */
1020
1021/*
1022 * rt2x00lib module information.
1023 */
1024MODULE_AUTHOR(DRV_PROJECT);
1025MODULE_VERSION(DRV_VERSION);
1026MODULE_DESCRIPTION("rt2x00 library");
1027MODULE_LICENSE("GPL");