blob: b03e6e41dc1edb1331e67f5b0854797b78b12dcf [file] [log] [blame]
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001/*
Ivo van Doorn7e613e12010-08-06 20:45:38 +02002 Copyright (C) 2010 Willow Garage <http://www.willowgarage.com>
3 Copyright (C) 2004 - 2010 Ivo van Doorn <IvDoorn@gmail.com>
Ivo van Doorn95ea3622007-09-25 17:57:13 -07004 <http://rt2x00.serialmonkey.com>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the
18 Free Software Foundation, Inc.,
19 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
22/*
23 Module: rt2x00lib
24 Abstract: rt2x00 generic device routines.
25 */
26
Ivo van Doorn95ea3622007-09-25 17:57:13 -070027#include <linux/kernel.h>
28#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Ivo van Doorn95ea3622007-09-25 17:57:13 -070030
31#include "rt2x00.h"
32#include "rt2x00lib.h"
33
34/*
Ivo van Doorn95ea3622007-09-25 17:57:13 -070035 * Radio control handlers.
36 */
37int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
38{
39 int status;
40
41 /*
42 * Don't enable the radio twice.
43 * And check if the hardware button has been disabled.
44 */
Ivo van Doorn4b9631a2009-07-11 18:00:19 +020045 if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -070046 return 0;
47
48 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -050049 * Initialize all data queues.
Ivo van Doorn837e7f22008-01-06 23:41:45 +010050 */
Ivo van Doorn798b7ad2008-11-08 15:25:33 +010051 rt2x00queue_init_queues(rt2x00dev);
Ivo van Doorn837e7f22008-01-06 23:41:45 +010052
53 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -070054 * Enable radio.
55 */
Ivo van Doorna2e1d522008-03-31 15:53:44 +020056 status =
57 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_ON);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070058 if (status)
59 return status;
60
Ivo van Doorn2b08da32008-06-03 18:58:56 +020061 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_ON);
62
Ivo van Doorna2e1d522008-03-31 15:53:44 +020063 rt2x00leds_led_radio(rt2x00dev, true);
Ivo van Doorn61c2b682008-04-21 19:01:09 +020064 rt2x00led_led_activity(rt2x00dev, true);
Ivo van Doorna2e1d522008-03-31 15:53:44 +020065
Ivo van Doorn0262ab02008-08-29 21:04:26 +020066 set_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070067
68 /*
69 * Enable RX.
70 */
Ivo van Doorn5cbf8302007-10-06 14:16:09 +020071 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070072
73 /*
Ivo van Doornc965c742010-07-11 12:25:46 +020074 * Start watchdog monitoring.
75 */
76 rt2x00link_start_watchdog(rt2x00dev);
77
78 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -070079 * Start the TX queues.
80 */
Johannes Berg36d68252008-05-15 12:55:26 +020081 ieee80211_wake_queues(rt2x00dev->hw);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070082
83 return 0;
84}
85
86void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
87{
Ivo van Doorn0262ab02008-08-29 21:04:26 +020088 if (!test_and_clear_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -070089 return;
90
91 /*
Ivo van Doorna2c9b652009-01-28 00:32:33 +010092 * Stop the TX queues in mac80211.
Ivo van Doorn95ea3622007-09-25 17:57:13 -070093 */
94 ieee80211_stop_queues(rt2x00dev->hw);
Ivo van Doorna2c9b652009-01-28 00:32:33 +010095 rt2x00queue_stop_queues(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070096
97 /*
Ivo van Doornc965c742010-07-11 12:25:46 +020098 * Stop watchdog monitoring.
99 */
100 rt2x00link_stop_watchdog(rt2x00dev);
101
102 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700103 * Disable RX.
104 */
Ivo van Doorn5cbf8302007-10-06 14:16:09 +0200105 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700106
107 /*
108 * Disable radio.
109 */
110 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
Ivo van Doorn2b08da32008-06-03 18:58:56 +0200111 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_OFF);
Ivo van Doorn61c2b682008-04-21 19:01:09 +0200112 rt2x00led_led_activity(rt2x00dev, false);
Ivo van Doorna2e1d522008-03-31 15:53:44 +0200113 rt2x00leds_led_radio(rt2x00dev, false);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700114}
115
Ivo van Doorn5cbf8302007-10-06 14:16:09 +0200116void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, enum dev_state state)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700117{
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700118 /*
119 * When we are disabling the RX, we should also stop the link tuner.
120 */
Ivo van Doorn5cbf8302007-10-06 14:16:09 +0200121 if (state == STATE_RADIO_RX_OFF)
Ivo van Doorn84e31962008-12-20 10:53:29 +0100122 rt2x00link_stop_tuner(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700123
124 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
125
126 /*
127 * When we are enabling the RX, we should also start the link tuner.
128 */
Ivo van Doorn84e31962008-12-20 10:53:29 +0100129 if (state == STATE_RADIO_RX_ON)
130 rt2x00link_start_tuner(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700131}
132
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100133static void rt2x00lib_intf_scheduled_iter(void *data, u8 *mac,
134 struct ieee80211_vif *vif)
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200135{
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100136 struct rt2x00_dev *rt2x00dev = data;
137 struct rt2x00_intf *intf = vif_to_intf(vif);
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100138 int delayed_flags;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100139
140 /*
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100141 * Copy all data we need during this action under the protection
142 * of a spinlock. Otherwise race conditions might occur which results
143 * into an invalid configuration.
Johannes Berg471b3ef2007-12-28 14:32:58 +0100144 */
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100145 spin_lock(&intf->lock);
146
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100147 delayed_flags = intf->delayed_flags;
148 intf->delayed_flags = 0;
149
150 spin_unlock(&intf->lock);
151
Ivo van Doorn980dfcb2008-06-25 21:27:00 +0200152 /*
153 * It is possible the radio was disabled while the work had been
154 * scheduled. If that happens we should return here immediately,
155 * note that in the spinlock protected area above the delayed_flags
156 * have been cleared correctly.
157 */
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200158 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
Ivo van Doorn980dfcb2008-06-25 21:27:00 +0200159 return;
160
Ivo van Doornbd88a782008-07-09 15:12:44 +0200161 if (delayed_flags & DELAYED_UPDATE_BEACON)
Ivo van Doorna2c9b652009-01-28 00:32:33 +0100162 rt2x00queue_update_beacon(rt2x00dev, vif, true);
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100163}
164
165static void rt2x00lib_intf_scheduled(struct work_struct *work)
166{
167 struct rt2x00_dev *rt2x00dev =
168 container_of(work, struct rt2x00_dev, intf_work);
169
170 /*
171 * Iterate over each interface and perform the
172 * requested configurations.
173 */
174 ieee80211_iterate_active_interfaces(rt2x00dev->hw,
175 rt2x00lib_intf_scheduled_iter,
176 rt2x00dev);
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200177}
178
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700179/*
180 * Interrupt context handlers.
181 */
Helmut Schaa07896fe2010-07-11 12:27:58 +0200182static void rt2x00lib_bc_buffer_iter(void *data, u8 *mac,
183 struct ieee80211_vif *vif)
184{
185 struct rt2x00_dev *rt2x00dev = data;
186 struct sk_buff *skb;
187
188 /*
189 * Only AP mode interfaces do broad- and multicast buffering
190 */
191 if (vif->type != NL80211_IFTYPE_AP)
192 return;
193
194 /*
195 * Send out buffered broad- and multicast frames
196 */
197 skb = ieee80211_get_buffered_bc(rt2x00dev->hw, vif);
198 while (skb) {
199 rt2x00mac_tx(rt2x00dev->hw, skb);
200 skb = ieee80211_get_buffered_bc(rt2x00dev->hw, vif);
201 }
202}
203
Helmut Schaa9f926fb2010-07-11 12:28:23 +0200204static void rt2x00lib_beaconupdate_iter(void *data, u8 *mac,
205 struct ieee80211_vif *vif)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700206{
Helmut Schaa4dee32f2010-07-11 12:27:26 +0200207 struct rt2x00_dev *rt2x00dev = data;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700208
Johannes Berg05c914f2008-09-11 00:01:58 +0200209 if (vif->type != NL80211_IFTYPE_AP &&
Andrey Yurovskya07dbea2008-12-20 10:55:34 +0100210 vif->type != NL80211_IFTYPE_ADHOC &&
Ivo van Doornce292a62008-12-20 10:57:02 +0100211 vif->type != NL80211_IFTYPE_MESH_POINT &&
212 vif->type != NL80211_IFTYPE_WDS)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700213 return;
214
Helmut Schaa4dee32f2010-07-11 12:27:26 +0200215 rt2x00queue_update_beacon(rt2x00dev, vif, true);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700216}
217
218void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
219{
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200220 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700221 return;
222
Helmut Schaa07896fe2010-07-11 12:27:58 +0200223 /* send buffered bc/mc frames out for every bssid */
Helmut Schaa4dee32f2010-07-11 12:27:26 +0200224 ieee80211_iterate_active_interfaces(rt2x00dev->hw,
Helmut Schaa07896fe2010-07-11 12:27:58 +0200225 rt2x00lib_bc_buffer_iter,
226 rt2x00dev);
Helmut Schaa9f926fb2010-07-11 12:28:23 +0200227 /*
228 * Devices with pre tbtt interrupt don't need to update the beacon
229 * here as they will fetch the next beacon directly prior to
230 * transmission.
231 */
232 if (test_bit(DRIVER_SUPPORT_PRE_TBTT_INTERRUPT, &rt2x00dev->flags))
233 return;
Helmut Schaa07896fe2010-07-11 12:27:58 +0200234
235 /* fetch next beacon */
236 ieee80211_iterate_active_interfaces(rt2x00dev->hw,
Helmut Schaa9f926fb2010-07-11 12:28:23 +0200237 rt2x00lib_beaconupdate_iter,
Helmut Schaa07896fe2010-07-11 12:27:58 +0200238 rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700239}
240EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
241
Helmut Schaa9f926fb2010-07-11 12:28:23 +0200242void rt2x00lib_pretbtt(struct rt2x00_dev *rt2x00dev)
243{
244 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
245 return;
246
247 /* fetch next beacon */
248 ieee80211_iterate_active_interfaces(rt2x00dev->hw,
249 rt2x00lib_beaconupdate_iter,
250 rt2x00dev);
251}
252EXPORT_SYMBOL_GPL(rt2x00lib_pretbtt);
253
Ivo van Doorn652a9dd2010-08-30 21:15:19 +0200254void rt2x00lib_dmadone(struct queue_entry *entry)
255{
256 rt2x00queue_index_inc(entry->queue, Q_INDEX_DMA_DONE);
257}
258EXPORT_SYMBOL_GPL(rt2x00lib_dmadone);
259
Ivo van Doorn181d6902008-02-05 16:42:23 -0500260void rt2x00lib_txdone(struct queue_entry *entry,
261 struct txdone_entry_desc *txdesc)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700262{
Ivo van Doorn181d6902008-02-05 16:42:23 -0500263 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
Johannes Berge039fa42008-05-15 12:55:29 +0200264 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
Johannes Berge6a98542008-10-21 12:40:02 +0200265 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200266 enum data_queue_qid qid = skb_get_queue_mapping(entry->skb);
Ivo van Doorn9f166172009-04-26 16:08:50 +0200267 unsigned int header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
Benoit PAPILLAULT92ed48e2009-08-17 18:56:10 +0200268 u8 rate_idx, rate_flags, retry_rates;
Johannes Berg7351c6b2009-11-19 01:08:30 +0100269 u8 skbdesc_flags = skbdesc->flags;
Benoit PAPILLAULT92ed48e2009-08-17 18:56:10 +0200270 unsigned int i;
Ivo van Doorn2e27cff2009-08-29 19:10:14 +0200271 bool success;
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200272
273 /*
Gertjan van Wingerdee513a0b2010-06-29 21:41:40 +0200274 * Unmap the skb.
275 */
276 rt2x00queue_unmap_skb(rt2x00dev, entry->skb);
277
278 /*
279 * Remove the extra tx headroom from the skb.
280 */
281 skb_pull(entry->skb, rt2x00dev->ops->extra_tx_headroom);
282
283 /*
284 * Signal that the TX descriptor is no longer in the skb.
285 */
286 skbdesc->flags &= ~SKBDESC_DESC_IN_SKB;
287
288 /*
Ivo van Doorn9f166172009-04-26 16:08:50 +0200289 * Remove L2 padding which was added during
290 */
291 if (test_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags))
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200292 rt2x00queue_remove_l2pad(entry->skb, header_length);
Ivo van Doorn9f166172009-04-26 16:08:50 +0200293
294 /*
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200295 * If the IV/EIV data was stripped from the frame before it was
296 * passed to the hardware, we should now reinsert it again because
Walter Goldens77c20612010-05-18 04:44:54 -0700297 * mac80211 will expect the same data to be present it the
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200298 * frame as it was passed to us.
299 */
300 if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags))
Ivo van Doorn9f166172009-04-26 16:08:50 +0200301 rt2x00crypto_tx_insert_iv(entry->skb, header_length);
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200302
303 /*
Johannes Berge039fa42008-05-15 12:55:29 +0200304 * Send frame to debugfs immediately, after this call is completed
305 * we are going to overwrite the skb->cb array.
306 */
307 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TXDONE, entry->skb);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700308
309 /*
Ivo van Doorn2e27cff2009-08-29 19:10:14 +0200310 * Determine if the frame has been successfully transmitted.
311 */
312 success =
313 test_bit(TXDONE_SUCCESS, &txdesc->flags) ||
Helmut Schaafd6dcb82010-06-14 22:09:09 +0200314 test_bit(TXDONE_UNKNOWN, &txdesc->flags);
Ivo van Doorn2e27cff2009-08-29 19:10:14 +0200315
316 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700317 * Update TX statistics.
318 */
Ivo van Doorn2e27cff2009-08-29 19:10:14 +0200319 rt2x00dev->link.qual.tx_success += success;
320 rt2x00dev->link.qual.tx_failed += !success;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700321
Johannes Berge6a98542008-10-21 12:40:02 +0200322 rate_idx = skbdesc->tx_rate_idx;
323 rate_flags = skbdesc->tx_rate_flags;
Benoit PAPILLAULT92ed48e2009-08-17 18:56:10 +0200324 retry_rates = test_bit(TXDONE_FALLBACK, &txdesc->flags) ?
325 (txdesc->retry + 1) : 1;
Johannes Berge6a98542008-10-21 12:40:02 +0200326
Ivo van Doorn181d6902008-02-05 16:42:23 -0500327 /*
328 * Initialize TX status
329 */
Johannes Berge039fa42008-05-15 12:55:29 +0200330 memset(&tx_info->status, 0, sizeof(tx_info->status));
331 tx_info->status.ack_signal = 0;
Benoit PAPILLAULT92ed48e2009-08-17 18:56:10 +0200332
333 /*
334 * Frame was send with retries, hardware tried
335 * different rates to send out the frame, at each
Helmut Schaa3d2bc102010-06-14 22:12:26 +0200336 * retry it lowered the rate 1 step except when the
337 * lowest rate was used.
Benoit PAPILLAULT92ed48e2009-08-17 18:56:10 +0200338 */
339 for (i = 0; i < retry_rates && i < IEEE80211_TX_MAX_RATES; i++) {
340 tx_info->status.rates[i].idx = rate_idx - i;
341 tx_info->status.rates[i].flags = rate_flags;
Helmut Schaa3d2bc102010-06-14 22:12:26 +0200342
343 if (rate_idx - i == 0) {
344 /*
345 * The lowest rate (index 0) was used until the
346 * number of max retries was reached.
347 */
348 tx_info->status.rates[i].count = retry_rates - i;
349 i++;
350 break;
351 }
Benoit PAPILLAULT92ed48e2009-08-17 18:56:10 +0200352 tx_info->status.rates[i].count = 1;
353 }
Ivo van Doorn2e27cff2009-08-29 19:10:14 +0200354 if (i < (IEEE80211_TX_MAX_RATES - 1))
Benoit PAPILLAULT92ed48e2009-08-17 18:56:10 +0200355 tx_info->status.rates[i].idx = -1; /* terminate */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500356
Johannes Berge039fa42008-05-15 12:55:29 +0200357 if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK)) {
Ivo van Doorn2e27cff2009-08-29 19:10:14 +0200358 if (success)
Johannes Berge039fa42008-05-15 12:55:29 +0200359 tx_info->flags |= IEEE80211_TX_STAT_ACK;
Ivo van Doorn2e27cff2009-08-29 19:10:14 +0200360 else
Ivo van Doorn181d6902008-02-05 16:42:23 -0500361 rt2x00dev->low_level_stats.dot11ACKFailureCount++;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700362 }
363
Helmut Schaa1df90802010-06-29 21:38:12 +0200364 /*
365 * Every single frame has it's own tx status, hence report
366 * every frame as ampdu of size 1.
367 *
368 * TODO: if we can find out how many frames were aggregated
369 * by the hw we could provide the real ampdu_len to mac80211
370 * which would allow the rc algorithm to better decide on
371 * which rates are suitable.
372 */
373 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
374 tx_info->flags |= IEEE80211_TX_STAT_AMPDU;
375 tx_info->status.ampdu_len = 1;
376 tx_info->status.ampdu_ack_len = success ? 1 : 0;
377 }
378
Johannes Berge6a98542008-10-21 12:40:02 +0200379 if (rate_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
Ivo van Doorn2e27cff2009-08-29 19:10:14 +0200380 if (success)
Ivo van Doorn181d6902008-02-05 16:42:23 -0500381 rt2x00dev->low_level_stats.dot11RTSSuccessCount++;
Ivo van Doorn2e27cff2009-08-29 19:10:14 +0200382 else
Ivo van Doorn181d6902008-02-05 16:42:23 -0500383 rt2x00dev->low_level_stats.dot11RTSFailureCount++;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700384 }
385
386 /*
Johannes Berg7351c6b2009-11-19 01:08:30 +0100387 * Only send the status report to mac80211 when it's a frame
388 * that originated in mac80211. If this was a extra frame coming
389 * through a mac80211 library call (RTS/CTS) then we should not
390 * send the status report back.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700391 */
Johannes Berg7351c6b2009-11-19 01:08:30 +0100392 if (!(skbdesc_flags & SKBDESC_NOT_MAC80211))
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200393 ieee80211_tx_status(rt2x00dev->hw, entry->skb);
Ivo van Doornbaf26a72008-02-17 17:32:08 +0100394 else
Helmut Schaa78e256c2010-07-11 12:26:48 +0200395 dev_kfree_skb_any(entry->skb);
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200396
397 /*
398 * Make this entry available for reuse.
399 */
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700400 entry->skb = NULL;
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200401 entry->flags = 0;
402
Ivo van Doorn798b7ad2008-11-08 15:25:33 +0100403 rt2x00dev->ops->lib->clear_entry(entry);
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200404
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200405 rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
406
407 /*
408 * If the data queue was below the threshold before the txdone
409 * handler we must make sure the packet queue in the mac80211 stack
410 * is reenabled when the txdone handler has finished.
411 */
412 if (!rt2x00queue_threshold(entry->queue))
413 ieee80211_wake_queue(rt2x00dev->hw, qid);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700414}
415EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
416
Ivo van Doorn3392bec2010-08-06 20:46:53 +0200417void rt2x00lib_txdone_noinfo(struct queue_entry *entry, u32 status)
418{
419 struct txdone_entry_desc txdesc;
420
421 txdesc.flags = 0;
422 __set_bit(status, &txdesc.flags);
423 txdesc.retry = 0;
424
425 rt2x00lib_txdone(entry, &txdesc);
426}
427EXPORT_SYMBOL_GPL(rt2x00lib_txdone_noinfo);
428
Ivo van Doorn35f00cf2009-04-26 16:09:32 +0200429static int rt2x00lib_rxdone_read_signal(struct rt2x00_dev *rt2x00dev,
430 struct rxdone_entry_desc *rxdesc)
431{
432 struct ieee80211_supported_band *sband;
433 const struct rt2x00_rate *rate;
434 unsigned int i;
435 int signal;
436 int type;
437
438 /*
439 * For non-HT rates the MCS value needs to contain the
440 * actually used rate modulation (CCK or OFDM).
441 */
442 if (rxdesc->dev_flags & RXDONE_SIGNAL_MCS)
443 signal = RATE_MCS(rxdesc->rate_mode, rxdesc->signal);
444 else
445 signal = rxdesc->signal;
446
447 type = (rxdesc->dev_flags & RXDONE_SIGNAL_MASK);
448
449 sband = &rt2x00dev->bands[rt2x00dev->curr_band];
450 for (i = 0; i < sband->n_bitrates; i++) {
451 rate = rt2x00_get_rate(sband->bitrates[i].hw_value);
452
453 if (((type == RXDONE_SIGNAL_PLCP) &&
454 (rate->plcp == signal)) ||
455 ((type == RXDONE_SIGNAL_BITRATE) &&
456 (rate->bitrate == signal)) ||
457 ((type == RXDONE_SIGNAL_MCS) &&
458 (rate->mcs == signal))) {
459 return i;
460 }
461 }
462
463 WARNING(rt2x00dev, "Frame received with unrecognized signal, "
464 "signal=0x%.4x, type=%d.\n", signal, type);
465 return 0;
466}
467
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200468void rt2x00lib_rxdone(struct rt2x00_dev *rt2x00dev,
469 struct queue_entry *entry)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700470{
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200471 struct rxdone_entry_desc rxdesc;
472 struct sk_buff *skb;
Ivo van Doorne5ef5ba2010-08-06 20:49:27 +0200473 struct ieee80211_rx_status *rx_status;
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200474 unsigned int header_length;
Ivo van Doorn35f00cf2009-04-26 16:09:32 +0200475 int rate_idx;
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200476
477 if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags))
478 goto submit_entry;
479
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700480 /*
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200481 * Allocate a new sk_buffer. If no new buffer available, drop the
482 * received frame and reuse the existing buffer.
483 */
484 skb = rt2x00queue_alloc_rxskb(rt2x00dev, entry);
485 if (!skb)
486 return;
487
488 /*
489 * Unmap the skb.
490 */
491 rt2x00queue_unmap_skb(rt2x00dev, entry->skb);
492
493 /*
494 * Extract the RXD details.
495 */
496 memset(&rxdesc, 0, sizeof(rxdesc));
497 rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700498
499 /*
Gertjan van Wingerde239c2492008-06-06 22:54:12 +0200500 * The data behind the ieee80211 header must be
Gertjan van Wingerdea9f853d2008-06-07 16:57:09 +0200501 * aligned on a 4 byte boundary.
Gertjan van Wingerde239c2492008-06-06 22:54:12 +0200502 */
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200503 header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
Gertjan van Wingerde239c2492008-06-06 22:54:12 +0200504
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200505 /*
506 * Hardware might have stripped the IV/EIV/ICV data,
507 * in that case it is possible that the data was
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +0800508 * provided separately (through hardware descriptor)
Ivo van Doorn2bb057d2008-08-04 16:37:44 +0200509 * in which case we should reinsert the data into the frame.
510 */
Ivo van Doorn74415ed2008-12-02 22:50:33 +0100511 if ((rxdesc.dev_flags & RXDONE_CRYPTO_IV) &&
Ivo van Doorn9f166172009-04-26 16:08:50 +0200512 (rxdesc.flags & RX_FLAG_IV_STRIPPED))
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200513 rt2x00crypto_rx_insert_iv(entry->skb, header_length,
Ivo van Doorn9f166172009-04-26 16:08:50 +0200514 &rxdesc);
Gertjan van Wingerdeb7340832009-12-04 23:47:04 +0100515 else if (header_length &&
516 (rxdesc.size > header_length) &&
517 (rxdesc.dev_flags & RXDONE_L2PAD))
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200518 rt2x00queue_remove_l2pad(entry->skb, header_length);
Ivo van Doorn9f166172009-04-26 16:08:50 +0200519 else
Ivo van Doorndaee6c02009-08-29 20:30:45 +0200520 rt2x00queue_align_payload(entry->skb, header_length);
Gertjan van Wingerde239c2492008-06-06 22:54:12 +0200521
Alban Browaeys1398d452009-12-04 23:47:00 +0100522 /* Trim buffer to correct size */
523 skb_trim(entry->skb, rxdesc.size);
524
Gertjan van Wingerde239c2492008-06-06 22:54:12 +0200525 /*
Ivo van Doorn35f00cf2009-04-26 16:09:32 +0200526 * Check if the frame was received using HT. In that case,
527 * the rate is the MCS index and should be passed to mac80211
528 * directly. Otherwise we need to translate the signal to
529 * the correct bitrate index.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700530 */
Ivo van Doorn35f00cf2009-04-26 16:09:32 +0200531 if (rxdesc.rate_mode == RATE_MODE_CCK ||
532 rxdesc.rate_mode == RATE_MODE_OFDM) {
533 rate_idx = rt2x00lib_rxdone_read_signal(rt2x00dev, &rxdesc);
534 } else {
535 rxdesc.flags |= RX_FLAG_HT;
536 rate_idx = rxdesc.signal;
Ivo van Doorn866a0502008-03-25 14:12:45 +0100537 }
538
Mattias Nissler61af43c2007-11-27 21:50:26 +0100539 /*
Ivo van Doorn84e31962008-12-20 10:53:29 +0100540 * Update extra components
Mattias Nissler61af43c2007-11-27 21:50:26 +0100541 */
Ivo van Doorn84e31962008-12-20 10:53:29 +0100542 rt2x00link_update_stats(rt2x00dev, entry->skb, &rxdesc);
543 rt2x00debug_update_crypto(rt2x00dev, &rxdesc);
Ivo van Doorne5ef5ba2010-08-06 20:49:27 +0200544 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_RXDONE, entry->skb);
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200545
Ivo van Doorne5ef5ba2010-08-06 20:49:27 +0200546 /*
547 * Initialize RX status information, and send frame
548 * to mac80211.
549 */
550 rx_status = IEEE80211_SKB_RXCB(entry->skb);
Ivo van Doornae73e582008-07-04 16:14:59 +0200551 rx_status->mactime = rxdesc.timestamp;
Ivo van Doorne5ef5ba2010-08-06 20:49:27 +0200552 rx_status->band = rt2x00dev->curr_band;
553 rx_status->freq = rt2x00dev->curr_freq;
Ivo van Doorn35f00cf2009-04-26 16:09:32 +0200554 rx_status->rate_idx = rate_idx;
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200555 rx_status->signal = rxdesc.rssi;
556 rx_status->flag = rxdesc.flags;
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200557 rx_status->antenna = rt2x00dev->link.ant.active.rx;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700558
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200559 ieee80211_rx_ni(rt2x00dev->hw, entry->skb);
Gertjan van Wingerdec4da0042008-06-16 19:56:31 +0200560
561 /*
562 * Replace the skb with the freshly allocated one.
563 */
564 entry->skb = skb;
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200565
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200566submit_entry:
Ivo van Doorn798b7ad2008-11-08 15:25:33 +0100567 rt2x00dev->ops->lib->clear_entry(entry);
Ivo van Doornd74f5ba2008-06-16 19:56:54 +0200568 rt2x00queue_index_inc(entry->queue, Q_INDEX);
Ivo van Doorn7e613e12010-08-06 20:45:38 +0200569 rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700570}
571EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
572
573/*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700574 * Driver initialization handlers.
575 */
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100576const struct rt2x00_rate rt2x00_supported_rates[12] = {
577 {
Ivo van Doorn3d8606a2008-11-02 00:36:40 +0100578 .flags = DEV_RATE_CCK,
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100579 .bitrate = 10,
Ivo van Doornaa776722008-03-09 22:48:08 +0100580 .ratemask = BIT(0),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100581 .plcp = 0x00,
Ivo van Doorn35f00cf2009-04-26 16:09:32 +0200582 .mcs = RATE_MCS(RATE_MODE_CCK, 0),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100583 },
584 {
Ivo van Doorn3d8606a2008-11-02 00:36:40 +0100585 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100586 .bitrate = 20,
Ivo van Doornaa776722008-03-09 22:48:08 +0100587 .ratemask = BIT(1),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100588 .plcp = 0x01,
Ivo van Doorn35f00cf2009-04-26 16:09:32 +0200589 .mcs = RATE_MCS(RATE_MODE_CCK, 1),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100590 },
591 {
Ivo van Doorn3d8606a2008-11-02 00:36:40 +0100592 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100593 .bitrate = 55,
Ivo van Doornaa776722008-03-09 22:48:08 +0100594 .ratemask = BIT(2),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100595 .plcp = 0x02,
Ivo van Doorn35f00cf2009-04-26 16:09:32 +0200596 .mcs = RATE_MCS(RATE_MODE_CCK, 2),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100597 },
598 {
Ivo van Doorn3d8606a2008-11-02 00:36:40 +0100599 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100600 .bitrate = 110,
Ivo van Doornaa776722008-03-09 22:48:08 +0100601 .ratemask = BIT(3),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100602 .plcp = 0x03,
Ivo van Doorn35f00cf2009-04-26 16:09:32 +0200603 .mcs = RATE_MCS(RATE_MODE_CCK, 3),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100604 },
605 {
Ivo van Doorn3d8606a2008-11-02 00:36:40 +0100606 .flags = DEV_RATE_OFDM,
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100607 .bitrate = 60,
Ivo van Doornaa776722008-03-09 22:48:08 +0100608 .ratemask = BIT(4),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100609 .plcp = 0x0b,
Ivo van Doorn35f00cf2009-04-26 16:09:32 +0200610 .mcs = RATE_MCS(RATE_MODE_OFDM, 0),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100611 },
612 {
613 .flags = DEV_RATE_OFDM,
614 .bitrate = 90,
Ivo van Doornaa776722008-03-09 22:48:08 +0100615 .ratemask = BIT(5),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100616 .plcp = 0x0f,
Ivo van Doorn35f00cf2009-04-26 16:09:32 +0200617 .mcs = RATE_MCS(RATE_MODE_OFDM, 1),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100618 },
619 {
Ivo van Doorn3d8606a2008-11-02 00:36:40 +0100620 .flags = DEV_RATE_OFDM,
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100621 .bitrate = 120,
Ivo van Doornaa776722008-03-09 22:48:08 +0100622 .ratemask = BIT(6),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100623 .plcp = 0x0a,
Ivo van Doorn35f00cf2009-04-26 16:09:32 +0200624 .mcs = RATE_MCS(RATE_MODE_OFDM, 2),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100625 },
626 {
627 .flags = DEV_RATE_OFDM,
628 .bitrate = 180,
Ivo van Doornaa776722008-03-09 22:48:08 +0100629 .ratemask = BIT(7),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100630 .plcp = 0x0e,
Ivo van Doorn35f00cf2009-04-26 16:09:32 +0200631 .mcs = RATE_MCS(RATE_MODE_OFDM, 3),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100632 },
633 {
Ivo van Doorn3d8606a2008-11-02 00:36:40 +0100634 .flags = DEV_RATE_OFDM,
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100635 .bitrate = 240,
Ivo van Doornaa776722008-03-09 22:48:08 +0100636 .ratemask = BIT(8),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100637 .plcp = 0x09,
Ivo van Doorn35f00cf2009-04-26 16:09:32 +0200638 .mcs = RATE_MCS(RATE_MODE_OFDM, 4),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100639 },
640 {
641 .flags = DEV_RATE_OFDM,
642 .bitrate = 360,
Ivo van Doornaa776722008-03-09 22:48:08 +0100643 .ratemask = BIT(9),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100644 .plcp = 0x0d,
Ivo van Doorn35f00cf2009-04-26 16:09:32 +0200645 .mcs = RATE_MCS(RATE_MODE_OFDM, 5),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100646 },
647 {
648 .flags = DEV_RATE_OFDM,
649 .bitrate = 480,
Ivo van Doornaa776722008-03-09 22:48:08 +0100650 .ratemask = BIT(10),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100651 .plcp = 0x08,
Ivo van Doorn35f00cf2009-04-26 16:09:32 +0200652 .mcs = RATE_MCS(RATE_MODE_OFDM, 6),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100653 },
654 {
655 .flags = DEV_RATE_OFDM,
656 .bitrate = 540,
Ivo van Doornaa776722008-03-09 22:48:08 +0100657 .ratemask = BIT(11),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100658 .plcp = 0x0c,
Ivo van Doorn35f00cf2009-04-26 16:09:32 +0200659 .mcs = RATE_MCS(RATE_MODE_OFDM, 7),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100660 },
661};
662
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700663static void rt2x00lib_channel(struct ieee80211_channel *entry,
664 const int channel, const int tx_power,
665 const int value)
666{
Ivo van Doornf2a3c7f2008-02-03 15:52:45 +0100667 entry->center_freq = ieee80211_channel_to_frequency(channel);
Johannes Berg8318d782008-01-24 19:38:38 +0100668 entry->hw_value = value;
669 entry->max_power = tx_power;
670 entry->max_antenna_gain = 0xff;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700671}
672
673static void rt2x00lib_rate(struct ieee80211_rate *entry,
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100674 const u16 index, const struct rt2x00_rate *rate)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700675{
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100676 entry->flags = 0;
677 entry->bitrate = rate->bitrate;
Ivo van Doorn3ea96462009-01-04 17:33:25 +0100678 entry->hw_value =index;
679 entry->hw_value_short = index;
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100680
Ivo van Doorn3ea96462009-01-04 17:33:25 +0100681 if (rate->flags & DEV_RATE_SHORT_PREAMBLE)
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100682 entry->flags |= IEEE80211_RATE_SHORT_PREAMBLE;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700683}
684
685static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
686 struct hw_mode_spec *spec)
687{
688 struct ieee80211_hw *hw = rt2x00dev->hw;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700689 struct ieee80211_channel *channels;
690 struct ieee80211_rate *rates;
Ivo van Doorn31562e82008-02-17 17:35:05 +0100691 unsigned int num_rates;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700692 unsigned int i;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700693
Ivo van Doorn31562e82008-02-17 17:35:05 +0100694 num_rates = 0;
695 if (spec->supported_rates & SUPPORT_RATE_CCK)
696 num_rates += 4;
697 if (spec->supported_rates & SUPPORT_RATE_OFDM)
698 num_rates += 8;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700699
700 channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
701 if (!channels)
Johannes Berg8318d782008-01-24 19:38:38 +0100702 return -ENOMEM;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700703
Ivo van Doorn31562e82008-02-17 17:35:05 +0100704 rates = kzalloc(sizeof(*rates) * num_rates, GFP_KERNEL);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700705 if (!rates)
706 goto exit_free_channels;
707
708 /*
709 * Initialize Rate list.
710 */
Ivo van Doorn31562e82008-02-17 17:35:05 +0100711 for (i = 0; i < num_rates; i++)
Ivo van Doorn8f5fa7f2008-02-03 15:54:34 +0100712 rt2x00lib_rate(&rates[i], i, rt2x00_get_rate(i));
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700713
714 /*
715 * Initialize Channel list.
716 */
717 for (i = 0; i < spec->num_channels; i++) {
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700718 rt2x00lib_channel(&channels[i],
Ivo van Doorn8c5e7a52008-08-04 16:38:47 +0200719 spec->channels[i].channel,
Ivo van Doorn8d1331b2010-08-23 19:56:07 +0200720 spec->channels_info[i].max_power, i);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700721 }
722
723 /*
Ivo van Doorn31562e82008-02-17 17:35:05 +0100724 * Intitialize 802.11b, 802.11g
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700725 * Rates: CCK, OFDM.
Johannes Berg8318d782008-01-24 19:38:38 +0100726 * Channels: 2.4 GHz
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700727 */
Gertjan van Wingerde47ac2682008-02-17 17:35:55 +0100728 if (spec->supported_bands & SUPPORT_BAND_2GHZ) {
Ivo van Doorn31562e82008-02-17 17:35:05 +0100729 rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_channels = 14;
730 rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_bitrates = num_rates;
731 rt2x00dev->bands[IEEE80211_BAND_2GHZ].channels = channels;
732 rt2x00dev->bands[IEEE80211_BAND_2GHZ].bitrates = rates;
733 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
734 &rt2x00dev->bands[IEEE80211_BAND_2GHZ];
Ivo van Doorn35f00cf2009-04-26 16:09:32 +0200735 memcpy(&rt2x00dev->bands[IEEE80211_BAND_2GHZ].ht_cap,
736 &spec->ht, sizeof(spec->ht));
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700737 }
738
739 /*
740 * Intitialize 802.11a
741 * Rates: OFDM.
742 * Channels: OFDM, UNII, HiperLAN2.
743 */
Gertjan van Wingerde47ac2682008-02-17 17:35:55 +0100744 if (spec->supported_bands & SUPPORT_BAND_5GHZ) {
Ivo van Doorn31562e82008-02-17 17:35:05 +0100745 rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_channels =
746 spec->num_channels - 14;
747 rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_bitrates =
748 num_rates - 4;
749 rt2x00dev->bands[IEEE80211_BAND_5GHZ].channels = &channels[14];
750 rt2x00dev->bands[IEEE80211_BAND_5GHZ].bitrates = &rates[4];
751 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
752 &rt2x00dev->bands[IEEE80211_BAND_5GHZ];
Ivo van Doorn35f00cf2009-04-26 16:09:32 +0200753 memcpy(&rt2x00dev->bands[IEEE80211_BAND_5GHZ].ht_cap,
754 &spec->ht, sizeof(spec->ht));
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700755 }
756
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700757 return 0;
758
Johannes Berg8318d782008-01-24 19:38:38 +0100759 exit_free_channels:
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700760 kfree(channels);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700761 ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
762 return -ENOMEM;
763}
764
765static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
766{
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200767 if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700768 ieee80211_unregister_hw(rt2x00dev->hw);
769
Johannes Berg8318d782008-01-24 19:38:38 +0100770 if (likely(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ])) {
771 kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->channels);
772 kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->bitrates);
773 rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
774 rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700775 }
Ivo van Doorn8c5e7a52008-08-04 16:38:47 +0200776
777 kfree(rt2x00dev->spec.channels_info);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700778}
779
780static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
781{
782 struct hw_mode_spec *spec = &rt2x00dev->spec;
783 int status;
784
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200785 if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
786 return 0;
787
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700788 /*
789 * Initialize HW modes.
790 */
791 status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
792 if (status)
793 return status;
794
795 /*
Gertjan van Wingerde61448f82008-05-10 13:43:33 +0200796 * Initialize HW fields.
797 */
798 rt2x00dev->hw->queues = rt2x00dev->ops->tx_queues;
799
800 /*
Gertjan van Wingerdee6218cc2009-11-23 22:44:52 +0100801 * Initialize extra TX headroom required.
802 */
Gertjan van Wingerde7a4a77b2009-12-30 11:36:30 +0100803 rt2x00dev->hw->extra_tx_headroom =
804 max_t(unsigned int, IEEE80211_TX_STATUS_HEADROOM,
805 rt2x00dev->ops->extra_tx_headroom);
806
807 /*
808 * Take TX headroom required for alignment into account.
809 */
810 if (test_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags))
811 rt2x00dev->hw->extra_tx_headroom += RT2X00_L2PAD_SIZE;
812 else if (test_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags))
813 rt2x00dev->hw->extra_tx_headroom += RT2X00_ALIGN_SIZE;
Gertjan van Wingerdee6218cc2009-11-23 22:44:52 +0100814
815 /*
Helmut Schaa96c3da72010-10-02 11:27:35 +0200816 * Allocate tx status FIFO for driver use.
817 */
818 if (test_bit(DRIVER_REQUIRE_TXSTATUS_FIFO, &rt2x00dev->flags) &&
819 rt2x00dev->ops->lib->txstatus_tasklet) {
820 /*
821 * Allocate txstatus fifo and tasklet, we use a size of 512
822 * for the kfifo which is big enough to store 512/4=128 tx
823 * status reports. In the worst case (tx status for all tx
824 * queues gets reported before we've got a chance to handle
825 * them) 24*4=384 tx status reports need to be cached.
826 */
827 status = kfifo_alloc(&rt2x00dev->txstatus_fifo, 512,
828 GFP_KERNEL);
829 if (status)
830 return status;
831
832 /* tasklet for processing the tx status reports. */
833 tasklet_init(&rt2x00dev->txstatus_tasklet,
834 rt2x00dev->ops->lib->txstatus_tasklet,
835 (unsigned long)rt2x00dev);
836
837 }
838
839 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700840 * Register HW.
841 */
842 status = ieee80211_register_hw(rt2x00dev->hw);
Herton Ronaldo Krzesinskif05faa32009-04-10 18:05:14 -0300843 if (status)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700844 return status;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700845
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200846 set_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700847
848 return 0;
849}
850
851/*
852 * Initialization/uninitialization handlers.
853 */
Ivo van Doorne37ea212008-01-06 23:40:07 +0100854static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700855{
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200856 if (!test_and_clear_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700857 return;
858
859 /*
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100860 * Unregister extra components.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700861 */
862 rt2x00rfkill_unregister(rt2x00dev);
863
864 /*
865 * Allow the HW to uninitialize.
866 */
867 rt2x00dev->ops->lib->uninitialize(rt2x00dev);
868
869 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500870 * Free allocated queue entries.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700871 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500872 rt2x00queue_uninitialize(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700873}
874
Ivo van Doorne37ea212008-01-06 23:40:07 +0100875static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700876{
877 int status;
878
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200879 if (test_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700880 return 0;
881
882 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500883 * Allocate all queue entries.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700884 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500885 status = rt2x00queue_initialize(rt2x00dev);
886 if (status)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700887 return status;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700888
889 /*
890 * Initialize the device.
891 */
892 status = rt2x00dev->ops->lib->initialize(rt2x00dev);
Ivo van Doorned499982008-05-05 17:23:47 +0200893 if (status) {
894 rt2x00queue_uninitialize(rt2x00dev);
895 return status;
896 }
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700897
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200898 set_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700899
900 /*
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100901 * Register the extra components.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700902 */
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100903 rt2x00rfkill_register(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700904
905 return 0;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700906}
907
Ivo van Doorne37ea212008-01-06 23:40:07 +0100908int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
909{
910 int retval;
911
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200912 if (test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
Ivo van Doorne37ea212008-01-06 23:40:07 +0100913 return 0;
914
915 /*
916 * If this is the first interface which is added,
917 * we should load the firmware now.
918 */
Ivo van Doorn9404ef32008-02-03 15:48:38 +0100919 retval = rt2x00lib_load_firmware(rt2x00dev);
920 if (retval)
921 return retval;
Ivo van Doorne37ea212008-01-06 23:40:07 +0100922
923 /*
924 * Initialize the device.
925 */
926 retval = rt2x00lib_initialize(rt2x00dev);
927 if (retval)
928 return retval;
929
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100930 rt2x00dev->intf_ap_count = 0;
931 rt2x00dev->intf_sta_count = 0;
932 rt2x00dev->intf_associated = 0;
933
Ivo van Doornbdfa5002009-08-08 23:53:04 +0200934 /* Enable the radio */
935 retval = rt2x00lib_enable_radio(rt2x00dev);
936 if (retval) {
937 rt2x00queue_uninitialize(rt2x00dev);
938 return retval;
939 }
940
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200941 set_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags);
Ivo van Doorne37ea212008-01-06 23:40:07 +0100942
943 return 0;
944}
945
946void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
947{
Ivo van Doorn0262ab02008-08-29 21:04:26 +0200948 if (!test_and_clear_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
Ivo van Doorne37ea212008-01-06 23:40:07 +0100949 return;
950
951 /*
952 * Perhaps we can add something smarter here,
953 * but for now just disabling the radio should do.
954 */
955 rt2x00lib_disable_radio(rt2x00dev);
956
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100957 rt2x00dev->intf_ap_count = 0;
958 rt2x00dev->intf_sta_count = 0;
959 rt2x00dev->intf_associated = 0;
Ivo van Doorne37ea212008-01-06 23:40:07 +0100960}
961
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700962/*
963 * driver allocation handlers.
964 */
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700965int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
966{
967 int retval = -ENOMEM;
968
Ivo van Doorn8ff48a82008-11-09 23:40:46 +0100969 mutex_init(&rt2x00dev->csr_mutex);
970
Sean Cross66f84d62009-11-05 20:22:03 +0100971 set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
972
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700973 /*
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100974 * Make room for rt2x00_intf inside the per-interface
975 * structure ieee80211_vif.
976 */
977 rt2x00dev->hw->vif_data_size = sizeof(struct rt2x00_intf);
978
Ivo van Doorn3514a442008-10-29 17:18:46 +0100979 /*
980 * Determine which operating modes are supported, all modes
981 * which require beaconing, depend on the availability of
982 * beacon entries.
983 */
984 rt2x00dev->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
985 if (rt2x00dev->ops->bcn->entry_num > 0)
986 rt2x00dev->hw->wiphy->interface_modes |=
987 BIT(NL80211_IFTYPE_ADHOC) |
Andrey Yurovskya07dbea2008-12-20 10:55:34 +0100988 BIT(NL80211_IFTYPE_AP) |
Ivo van Doornce292a62008-12-20 10:57:02 +0100989 BIT(NL80211_IFTYPE_MESH_POINT) |
990 BIT(NL80211_IFTYPE_WDS);
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700991
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100992 /*
Stephen Boyd9acd56d2010-07-16 09:50:10 -0700993 * Initialize configuration work.
994 */
995 INIT_WORK(&rt2x00dev->intf_work, rt2x00lib_intf_scheduled);
996
997 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700998 * Let the driver probe the device to detect the capabilities.
999 */
1000 retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
1001 if (retval) {
1002 ERROR(rt2x00dev, "Failed to allocate device.\n");
1003 goto exit;
1004 }
1005
1006 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -05001007 * Allocate queue array.
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001008 */
Ivo van Doorn181d6902008-02-05 16:42:23 -05001009 retval = rt2x00queue_allocate(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001010 if (retval)
1011 goto exit;
1012
1013 /*
1014 * Initialize ieee80211 structure.
1015 */
1016 retval = rt2x00lib_probe_hw(rt2x00dev);
1017 if (retval) {
1018 ERROR(rt2x00dev, "Failed to initialize hw.\n");
1019 goto exit;
1020 }
1021
1022 /*
Ivo van Doorn1682fe62008-03-13 15:38:03 +01001023 * Register extra components.
Ivo van Doorna9450b72008-02-03 15:53:40 +01001024 */
Ivo van Doorn84e31962008-12-20 10:53:29 +01001025 rt2x00link_register(rt2x00dev);
Ivo van Doorna9450b72008-02-03 15:53:40 +01001026 rt2x00leds_register(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001027 rt2x00debug_register(rt2x00dev);
1028
1029 return 0;
1030
1031exit:
1032 rt2x00lib_remove_dev(rt2x00dev);
1033
1034 return retval;
1035}
1036EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
1037
1038void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
1039{
Ivo van Doorn0262ab02008-08-29 21:04:26 +02001040 clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
Ivo van Doorn066cb632007-09-25 20:55:39 +02001041
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001042 /*
1043 * Disable radio.
1044 */
1045 rt2x00lib_disable_radio(rt2x00dev);
1046
1047 /*
Pavel Roskind8cc8922009-08-02 14:30:15 -04001048 * Stop all work.
1049 */
Pavel Roskind8cc8922009-08-02 14:30:15 -04001050 cancel_work_sync(&rt2x00dev->intf_work);
Ivo van Doorn7e613e12010-08-06 20:45:38 +02001051 cancel_work_sync(&rt2x00dev->rxdone_work);
1052 cancel_work_sync(&rt2x00dev->txdone_work);
Pavel Roskind8cc8922009-08-02 14:30:15 -04001053
1054 /*
Helmut Schaa96c3da72010-10-02 11:27:35 +02001055 * Free the tx status fifo.
1056 */
1057 kfifo_free(&rt2x00dev->txstatus_fifo);
1058
1059 /*
1060 * Kill the tx status tasklet.
1061 */
1062 tasklet_kill(&rt2x00dev->txstatus_tasklet);
1063
1064 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001065 * Uninitialize device.
1066 */
1067 rt2x00lib_uninitialize(rt2x00dev);
1068
1069 /*
Ivo van Doorn1682fe62008-03-13 15:38:03 +01001070 * Free extra components
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001071 */
1072 rt2x00debug_deregister(rt2x00dev);
Ivo van Doorna9450b72008-02-03 15:53:40 +01001073 rt2x00leds_unregister(rt2x00dev);
1074
1075 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001076 * Free ieee80211_hw memory.
1077 */
1078 rt2x00lib_remove_hw(rt2x00dev);
1079
1080 /*
1081 * Free firmware image.
1082 */
1083 rt2x00lib_free_firmware(rt2x00dev);
1084
1085 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -05001086 * Free queue structures.
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001087 */
Ivo van Doorn181d6902008-02-05 16:42:23 -05001088 rt2x00queue_free(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001089}
1090EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
1091
1092/*
1093 * Device state handlers
1094 */
1095#ifdef CONFIG_PM
1096int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
1097{
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001098 NOTICE(rt2x00dev, "Going to sleep.\n");
Ivo van Doorn066cb632007-09-25 20:55:39 +02001099
1100 /*
Ivo van Doorn07126122009-01-23 17:04:05 +01001101 * Prevent mac80211 from accessing driver while suspended.
Ivo van Doorn066cb632007-09-25 20:55:39 +02001102 */
Ivo van Doorn07126122009-01-23 17:04:05 +01001103 if (!test_and_clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
1104 return 0;
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001105
1106 /*
Ivo van Doorn07126122009-01-23 17:04:05 +01001107 * Cleanup as much as possible.
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001108 */
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001109 rt2x00lib_uninitialize(rt2x00dev);
Ivo van Doorn1682fe62008-03-13 15:38:03 +01001110
1111 /*
1112 * Suspend/disable extra components.
1113 */
Ivo van Doorna9450b72008-02-03 15:53:40 +01001114 rt2x00leds_suspend(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001115 rt2x00debug_deregister(rt2x00dev);
1116
1117 /*
Ivo van Doorn98963222008-03-27 17:15:24 +01001118 * Set device mode to sleep for power management,
1119 * on some hardware this call seems to consistently fail.
1120 * From the specifications it is hard to tell why it fails,
1121 * and if this is a "bad thing".
1122 * Overall it is safe to just ignore the failure and
1123 * continue suspending. The only downside is that the
1124 * device will not be in optimal power save mode, but with
1125 * the radio and the other components already disabled the
1126 * device is as good as disabled.
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001127 */
Ivo van Doorn07126122009-01-23 17:04:05 +01001128 if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP))
Ivo van Doorn98963222008-03-27 17:15:24 +01001129 WARNING(rt2x00dev, "Device failed to enter sleep state, "
1130 "continue suspending.\n");
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001131
1132 return 0;
1133}
1134EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1135
1136int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1137{
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001138 NOTICE(rt2x00dev, "Waking up.\n");
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001139
1140 /*
Ivo van Doorn1682fe62008-03-13 15:38:03 +01001141 * Restore/enable extra components.
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001142 */
1143 rt2x00debug_register(rt2x00dev);
Ivo van Doorna9450b72008-02-03 15:53:40 +01001144 rt2x00leds_resume(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001145
1146 /*
Ivo van Doorne37ea212008-01-06 23:40:07 +01001147 * We are ready again to receive requests from mac80211.
1148 */
Ivo van Doorn0262ab02008-08-29 21:04:26 +02001149 set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
Ivo van Doorne37ea212008-01-06 23:40:07 +01001150
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001151 return 0;
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001152}
1153EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1154#endif /* CONFIG_PM */
1155
1156/*
1157 * rt2x00lib module information.
1158 */
1159MODULE_AUTHOR(DRV_PROJECT);
1160MODULE_VERSION(DRV_VERSION);
1161MODULE_DESCRIPTION("rt2x00 library");
1162MODULE_LICENSE("GPL");