blob: b8e0c4c3ed0a92a59b35963a8c9d13b42a75b12f [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 * Link tuning handlers
34 */
Ivo van Doorn53b3f8e2008-02-25 23:15:13 +010035void rt2x00lib_reset_link_tuner(struct rt2x00_dev *rt2x00dev)
Ivo van Doorn95ea3622007-09-25 17:57:13 -070036{
Ivo van Doorn53b3f8e2008-02-25 23:15:13 +010037 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
38 return;
39
40 /*
41 * Reset link information.
42 * Both the currently active vgc level as well as
43 * the link tuner counter should be reset. Resetting
44 * the counter is important for devices where the
45 * device should only perform link tuning during the
46 * first minute after being enabled.
47 */
Ivo van Doorn8de8c512007-10-13 16:26:32 +020048 rt2x00dev->link.count = 0;
49 rt2x00dev->link.vgc_level = 0;
50
Ivo van Doorn53b3f8e2008-02-25 23:15:13 +010051 /*
52 * Reset the link tuner.
53 */
54 rt2x00dev->ops->lib->reset_tuner(rt2x00dev);
55}
56
57static void rt2x00lib_start_link_tuner(struct rt2x00_dev *rt2x00dev)
58{
59 /*
60 * Clear all (possibly) pre-existing quality statistics.
61 */
Ivo van Doorn8de8c512007-10-13 16:26:32 +020062 memset(&rt2x00dev->link.qual, 0, sizeof(rt2x00dev->link.qual));
63
64 /*
65 * The RX and TX percentage should start at 50%
66 * this will assure we will get at least get some
67 * decent value when the link tuner starts.
68 * The value will be dropped and overwritten with
69 * the correct (measured )value anyway during the
70 * first run of the link tuner.
71 */
72 rt2x00dev->link.qual.rx_percentage = 50;
73 rt2x00dev->link.qual.tx_percentage = 50;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070074
Ivo van Doorn53b3f8e2008-02-25 23:15:13 +010075 rt2x00lib_reset_link_tuner(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070076
77 queue_delayed_work(rt2x00dev->hw->workqueue,
78 &rt2x00dev->link.work, LINK_TUNE_INTERVAL);
79}
80
81static void rt2x00lib_stop_link_tuner(struct rt2x00_dev *rt2x00dev)
82{
Ivo van Doorn3e309682007-09-25 20:56:36 +020083 cancel_delayed_work_sync(&rt2x00dev->link.work);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070084}
85
Ivo van Doorn95ea3622007-09-25 17:57:13 -070086/*
87 * Radio control handlers.
88 */
89int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
90{
91 int status;
92
93 /*
94 * Don't enable the radio twice.
95 * And check if the hardware button has been disabled.
96 */
97 if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
Ivo van Doorn81873e92007-10-06 14:14:06 +020098 test_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -070099 return 0;
100
101 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500102 * Initialize all data queues.
Ivo van Doorn837e7f22008-01-06 23:41:45 +0100103 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500104 rt2x00queue_init_rx(rt2x00dev);
105 rt2x00queue_init_tx(rt2x00dev);
Ivo van Doorn837e7f22008-01-06 23:41:45 +0100106
107 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700108 * Enable radio.
109 */
Ivo van Doorna2e1d522008-03-31 15:53:44 +0200110 status =
111 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_ON);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700112 if (status)
113 return status;
114
Ivo van Doorn2b08da32008-06-03 18:58:56 +0200115 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_ON);
116
Ivo van Doorna2e1d522008-03-31 15:53:44 +0200117 rt2x00leds_led_radio(rt2x00dev, true);
Ivo van Doorn61c2b682008-04-21 19:01:09 +0200118 rt2x00led_led_activity(rt2x00dev, true);
Ivo van Doorna2e1d522008-03-31 15:53:44 +0200119
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700120 __set_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags);
121
122 /*
123 * Enable RX.
124 */
Ivo van Doorn5cbf8302007-10-06 14:16:09 +0200125 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700126
127 /*
128 * Start the TX queues.
129 */
Johannes Berg36d68252008-05-15 12:55:26 +0200130 ieee80211_wake_queues(rt2x00dev->hw);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700131
132 return 0;
133}
134
135void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
136{
137 if (!__test_and_clear_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
138 return;
139
140 /*
Johannes Berg4150c572007-09-17 01:29:23 -0400141 * Stop all scheduled work.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700142 */
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100143 if (work_pending(&rt2x00dev->intf_work))
144 cancel_work_sync(&rt2x00dev->intf_work);
Johannes Berg4150c572007-09-17 01:29:23 -0400145 if (work_pending(&rt2x00dev->filter_work))
146 cancel_work_sync(&rt2x00dev->filter_work);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700147
148 /*
149 * Stop the TX queues.
150 */
151 ieee80211_stop_queues(rt2x00dev->hw);
152
153 /*
154 * Disable RX.
155 */
Ivo van Doorn5cbf8302007-10-06 14:16:09 +0200156 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700157
158 /*
159 * Disable radio.
160 */
161 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
Ivo van Doorn2b08da32008-06-03 18:58:56 +0200162 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_OFF);
Ivo van Doorn61c2b682008-04-21 19:01:09 +0200163 rt2x00led_led_activity(rt2x00dev, false);
Ivo van Doorna2e1d522008-03-31 15:53:44 +0200164 rt2x00leds_led_radio(rt2x00dev, false);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700165}
166
Ivo van Doorn5cbf8302007-10-06 14:16:09 +0200167void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, enum dev_state state)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700168{
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700169 /*
170 * When we are disabling the RX, we should also stop the link tuner.
171 */
Ivo van Doorn5cbf8302007-10-06 14:16:09 +0200172 if (state == STATE_RADIO_RX_OFF)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700173 rt2x00lib_stop_link_tuner(rt2x00dev);
174
175 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
176
177 /*
178 * When we are enabling the RX, we should also start the link tuner.
179 */
Ivo van Doorn5cbf8302007-10-06 14:16:09 +0200180 if (state == STATE_RADIO_RX_ON &&
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100181 (rt2x00dev->intf_ap_count || rt2x00dev->intf_sta_count))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700182 rt2x00lib_start_link_tuner(rt2x00dev);
183}
184
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200185static void rt2x00lib_evaluate_antenna_sample(struct rt2x00_dev *rt2x00dev)
186{
187 enum antenna rx = rt2x00dev->link.ant.active.rx;
188 enum antenna tx = rt2x00dev->link.ant.active.tx;
189 int sample_a =
190 rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_A);
191 int sample_b =
192 rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_B);
193
194 /*
195 * We are done sampling. Now we should evaluate the results.
196 */
197 rt2x00dev->link.ant.flags &= ~ANTENNA_MODE_SAMPLE;
198
199 /*
200 * During the last period we have sampled the RSSI
201 * from both antenna's. It now is time to determine
202 * which antenna demonstrated the best performance.
203 * When we are already on the antenna with the best
204 * performance, then there really is nothing for us
205 * left to do.
206 */
207 if (sample_a == sample_b)
208 return;
209
Ivo van Doorn05253c92008-02-25 23:15:08 +0100210 if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY)
211 rx = (sample_a > sample_b) ? ANTENNA_A : ANTENNA_B;
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200212
Ivo van Doorn05253c92008-02-25 23:15:08 +0100213 if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)
214 tx = (sample_a > sample_b) ? ANTENNA_A : ANTENNA_B;
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200215
216 rt2x00lib_config_antenna(rt2x00dev, rx, tx);
217}
218
219static void rt2x00lib_evaluate_antenna_eval(struct rt2x00_dev *rt2x00dev)
220{
221 enum antenna rx = rt2x00dev->link.ant.active.rx;
222 enum antenna tx = rt2x00dev->link.ant.active.tx;
223 int rssi_curr = rt2x00_get_link_ant_rssi(&rt2x00dev->link);
224 int rssi_old = rt2x00_update_ant_rssi(&rt2x00dev->link, rssi_curr);
225
226 /*
227 * Legacy driver indicates that we should swap antenna's
228 * when the difference in RSSI is greater that 5. This
229 * also should be done when the RSSI was actually better
230 * then the previous sample.
231 * When the difference exceeds the threshold we should
232 * sample the rssi from the other antenna to make a valid
233 * comparison between the 2 antennas.
234 */
Ivo van Doornb290d432008-02-25 23:15:01 +0100235 if (abs(rssi_curr - rssi_old) < 5)
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200236 return;
237
238 rt2x00dev->link.ant.flags |= ANTENNA_MODE_SAMPLE;
239
240 if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY)
241 rx = (rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
242
243 if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)
244 tx = (tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
245
246 rt2x00lib_config_antenna(rt2x00dev, rx, tx);
247}
248
249static void rt2x00lib_evaluate_antenna(struct rt2x00_dev *rt2x00dev)
250{
251 /*
252 * Determine if software diversity is enabled for
253 * either the TX or RX antenna (or both).
254 * Always perform this check since within the link
255 * tuner interval the configuration might have changed.
256 */
257 rt2x00dev->link.ant.flags &= ~ANTENNA_RX_DIVERSITY;
258 rt2x00dev->link.ant.flags &= ~ANTENNA_TX_DIVERSITY;
259
260 if (rt2x00dev->hw->conf.antenna_sel_rx == 0 &&
Ivo van Doornb290d432008-02-25 23:15:01 +0100261 rt2x00dev->default_ant.rx == ANTENNA_SW_DIVERSITY)
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200262 rt2x00dev->link.ant.flags |= ANTENNA_RX_DIVERSITY;
263 if (rt2x00dev->hw->conf.antenna_sel_tx == 0 &&
Ivo van Doornb290d432008-02-25 23:15:01 +0100264 rt2x00dev->default_ant.tx == ANTENNA_SW_DIVERSITY)
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200265 rt2x00dev->link.ant.flags |= ANTENNA_TX_DIVERSITY;
266
267 if (!(rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY) &&
268 !(rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)) {
Ivo van Doorn05253c92008-02-25 23:15:08 +0100269 rt2x00dev->link.ant.flags = 0;
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200270 return;
271 }
272
273 /*
274 * If we have only sampled the data over the last period
275 * we should now harvest the data. Otherwise just evaluate
276 * the data. The latter should only be performed once
277 * every 2 seconds.
278 */
279 if (rt2x00dev->link.ant.flags & ANTENNA_MODE_SAMPLE)
280 rt2x00lib_evaluate_antenna_sample(rt2x00dev);
281 else if (rt2x00dev->link.count & 1)
282 rt2x00lib_evaluate_antenna_eval(rt2x00dev);
283}
284
285static void rt2x00lib_update_link_stats(struct link *link, int rssi)
286{
287 int avg_rssi = rssi;
288
289 /*
290 * Update global RSSI
291 */
292 if (link->qual.avg_rssi)
293 avg_rssi = MOVING_AVERAGE(link->qual.avg_rssi, rssi, 8);
294 link->qual.avg_rssi = avg_rssi;
295
296 /*
297 * Update antenna RSSI
298 */
299 if (link->ant.rssi_ant)
300 rssi = MOVING_AVERAGE(link->ant.rssi_ant, rssi, 8);
301 link->ant.rssi_ant = rssi;
302}
303
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200304static void rt2x00lib_precalculate_link_signal(struct link_qual *qual)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700305{
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200306 if (qual->rx_failed || qual->rx_success)
307 qual->rx_percentage =
308 (qual->rx_success * 100) /
309 (qual->rx_failed + qual->rx_success);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700310 else
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200311 qual->rx_percentage = 50;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700312
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200313 if (qual->tx_failed || qual->tx_success)
314 qual->tx_percentage =
315 (qual->tx_success * 100) /
316 (qual->tx_failed + qual->tx_success);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700317 else
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200318 qual->tx_percentage = 50;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700319
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200320 qual->rx_success = 0;
321 qual->rx_failed = 0;
322 qual->tx_success = 0;
323 qual->tx_failed = 0;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700324}
325
326static int rt2x00lib_calculate_link_signal(struct rt2x00_dev *rt2x00dev,
327 int rssi)
328{
329 int rssi_percentage = 0;
330 int signal;
331
332 /*
333 * We need a positive value for the RSSI.
334 */
335 if (rssi < 0)
336 rssi += rt2x00dev->rssi_offset;
337
338 /*
339 * Calculate the different percentages,
340 * which will be used for the signal.
341 */
342 if (rt2x00dev->rssi_offset)
343 rssi_percentage = (rssi * 100) / rt2x00dev->rssi_offset;
344
345 /*
346 * Add the individual percentages and use the WEIGHT
347 * defines to calculate the current link signal.
348 */
349 signal = ((WEIGHT_RSSI * rssi_percentage) +
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200350 (WEIGHT_TX * rt2x00dev->link.qual.tx_percentage) +
351 (WEIGHT_RX * rt2x00dev->link.qual.rx_percentage)) / 100;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700352
353 return (signal > 100) ? 100 : signal;
354}
355
356static void rt2x00lib_link_tuner(struct work_struct *work)
357{
358 struct rt2x00_dev *rt2x00dev =
359 container_of(work, struct rt2x00_dev, link.work.work);
360
361 /*
Ivo van Doorn25ab0022007-09-25 20:57:04 +0200362 * When the radio is shutting down we should
363 * immediately cease all link tuning.
364 */
365 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
366 return;
367
368 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700369 * Update statistics.
370 */
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200371 rt2x00dev->ops->lib->link_stats(rt2x00dev, &rt2x00dev->link.qual);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700372 rt2x00dev->low_level_stats.dot11FCSErrorCount +=
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200373 rt2x00dev->link.qual.rx_failed;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700374
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700375 /*
376 * Only perform the link tuning when Link tuning
377 * has been enabled (This could have been disabled from the EEPROM).
378 */
379 if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
380 rt2x00dev->ops->lib->link_tuner(rt2x00dev);
381
382 /*
Ivo van Doorn725d99d2007-09-25 20:53:20 +0200383 * Precalculate a portion of the link signal which is
384 * in based on the tx/rx success/failure counters.
385 */
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200386 rt2x00lib_precalculate_link_signal(&rt2x00dev->link.qual);
Ivo van Doorn725d99d2007-09-25 20:53:20 +0200387
388 /*
Ivo van Doorna9450b72008-02-03 15:53:40 +0100389 * Send a signal to the led to update the led signal strength.
390 */
391 rt2x00leds_led_quality(rt2x00dev, rt2x00dev->link.qual.avg_rssi);
392
393 /*
Ivo van Doorn53b3f8e2008-02-25 23:15:13 +0100394 * Evaluate antenna setup, make this the last step since this could
395 * possibly reset some statistics.
396 */
397 rt2x00lib_evaluate_antenna(rt2x00dev);
398
399 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700400 * Increase tuner counter, and reschedule the next link tuner run.
401 */
402 rt2x00dev->link.count++;
403 queue_delayed_work(rt2x00dev->hw->workqueue, &rt2x00dev->link.work,
404 LINK_TUNE_INTERVAL);
405}
406
Johannes Berg4150c572007-09-17 01:29:23 -0400407static void rt2x00lib_packetfilter_scheduled(struct work_struct *work)
408{
409 struct rt2x00_dev *rt2x00dev =
410 container_of(work, struct rt2x00_dev, filter_work);
Ivo van Doorn5886d0d2007-10-06 14:13:38 +0200411
Ivo van Doorn133adf02008-04-04 00:01:43 +0200412 rt2x00dev->ops->lib->config_filter(rt2x00dev, rt2x00dev->packet_filter);
Johannes Berg4150c572007-09-17 01:29:23 -0400413}
414
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100415static void rt2x00lib_intf_scheduled_iter(void *data, u8 *mac,
416 struct ieee80211_vif *vif)
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200417{
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100418 struct rt2x00_dev *rt2x00dev = data;
419 struct rt2x00_intf *intf = vif_to_intf(vif);
420 struct sk_buff *skb;
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100421 struct ieee80211_bss_conf conf;
422 int delayed_flags;
Johannes Berg471b3ef2007-12-28 14:32:58 +0100423
424 /*
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100425 * Copy all data we need during this action under the protection
426 * of a spinlock. Otherwise race conditions might occur which results
427 * into an invalid configuration.
Johannes Berg471b3ef2007-12-28 14:32:58 +0100428 */
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100429 spin_lock(&intf->lock);
430
431 memcpy(&conf, &intf->conf, sizeof(conf));
432 delayed_flags = intf->delayed_flags;
433 intf->delayed_flags = 0;
434
435 spin_unlock(&intf->lock);
436
437 if (delayed_flags & DELAYED_UPDATE_BEACON) {
Johannes Berge039fa42008-05-15 12:55:29 +0200438 skb = ieee80211_beacon_get(rt2x00dev->hw, vif);
439 if (skb &&
440 rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw, skb))
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100441 dev_kfree_skb(skb);
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100442 }
443
Ivo van Doorn72810372008-03-09 22:46:18 +0100444 if (delayed_flags & DELAYED_CONFIG_ERP)
445 rt2x00lib_config_erp(rt2x00dev, intf, &intf->conf);
Ivo van Doorna2e1d522008-03-31 15:53:44 +0200446
447 if (delayed_flags & DELAYED_LED_ASSOC)
448 rt2x00leds_led_assoc(rt2x00dev, !!rt2x00dev->intf_associated);
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100449}
450
451static void rt2x00lib_intf_scheduled(struct work_struct *work)
452{
453 struct rt2x00_dev *rt2x00dev =
454 container_of(work, struct rt2x00_dev, intf_work);
455
456 /*
457 * Iterate over each interface and perform the
458 * requested configurations.
459 */
460 ieee80211_iterate_active_interfaces(rt2x00dev->hw,
461 rt2x00lib_intf_scheduled_iter,
462 rt2x00dev);
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200463}
464
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700465/*
466 * Interrupt context handlers.
467 */
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100468static void rt2x00lib_beacondone_iter(void *data, u8 *mac,
469 struct ieee80211_vif *vif)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700470{
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100471 struct rt2x00_intf *intf = vif_to_intf(vif);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700472
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100473 if (vif->type != IEEE80211_IF_TYPE_AP &&
474 vif->type != IEEE80211_IF_TYPE_IBSS)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700475 return;
476
Gertjan van Wingerdec95edf52008-06-16 19:55:18 +0200477 /*
478 * Clean up the beacon skb.
479 */
480 dev_kfree_skb_irq(intf->beacon->skb);
481 intf->beacon->skb = NULL;
482
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100483 spin_lock(&intf->lock);
484 intf->delayed_flags |= DELAYED_UPDATE_BEACON;
485 spin_unlock(&intf->lock);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700486}
487
488void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
489{
490 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
491 return;
492
Ivo van Doorn633257d2008-05-23 18:14:02 +0200493 ieee80211_iterate_active_interfaces_atomic(rt2x00dev->hw,
494 rt2x00lib_beacondone_iter,
495 rt2x00dev);
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100496
497 queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->intf_work);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700498}
499EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
500
Ivo van Doorn181d6902008-02-05 16:42:23 -0500501void rt2x00lib_txdone(struct queue_entry *entry,
502 struct txdone_entry_desc *txdesc)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700503{
Ivo van Doorn181d6902008-02-05 16:42:23 -0500504 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
Johannes Berge039fa42008-05-15 12:55:29 +0200505 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
506
507 /*
508 * Send frame to debugfs immediately, after this call is completed
509 * we are going to overwrite the skb->cb array.
510 */
511 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TXDONE, entry->skb);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700512
513 /*
514 * Update TX statistics.
515 */
Ivo van Doornfb55f4d12008-05-10 13:42:06 +0200516 rt2x00dev->link.qual.tx_success +=
517 test_bit(TXDONE_SUCCESS, &txdesc->flags);
518 rt2x00dev->link.qual.tx_failed +=
John W. Linvillecb14cb72008-06-10 09:06:52 -0400519 test_bit(TXDONE_FAILURE, &txdesc->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700520
Ivo van Doorn181d6902008-02-05 16:42:23 -0500521 /*
522 * Initialize TX status
523 */
Johannes Berge039fa42008-05-15 12:55:29 +0200524 memset(&tx_info->status, 0, sizeof(tx_info->status));
525 tx_info->status.ack_signal = 0;
526 tx_info->status.excessive_retries =
Ivo van Doornfb55f4d12008-05-10 13:42:06 +0200527 test_bit(TXDONE_EXCESSIVE_RETRY, &txdesc->flags);
Johannes Berge039fa42008-05-15 12:55:29 +0200528 tx_info->status.retry_count = txdesc->retry;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500529
Johannes Berge039fa42008-05-15 12:55:29 +0200530 if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK)) {
Ivo van Doornfb55f4d12008-05-10 13:42:06 +0200531 if (test_bit(TXDONE_SUCCESS, &txdesc->flags))
Johannes Berge039fa42008-05-15 12:55:29 +0200532 tx_info->flags |= IEEE80211_TX_STAT_ACK;
Ivo van Doornfb55f4d12008-05-10 13:42:06 +0200533 else if (test_bit(TXDONE_FAILURE, &txdesc->flags))
Ivo van Doorn181d6902008-02-05 16:42:23 -0500534 rt2x00dev->low_level_stats.dot11ACKFailureCount++;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700535 }
536
Johannes Berge039fa42008-05-15 12:55:29 +0200537 if (tx_info->flags & IEEE80211_TX_CTL_USE_RTS_CTS) {
Ivo van Doornfb55f4d12008-05-10 13:42:06 +0200538 if (test_bit(TXDONE_SUCCESS, &txdesc->flags))
Ivo van Doorn181d6902008-02-05 16:42:23 -0500539 rt2x00dev->low_level_stats.dot11RTSSuccessCount++;
Ivo van Doornfb55f4d12008-05-10 13:42:06 +0200540 else if (test_bit(TXDONE_FAILURE, &txdesc->flags))
Ivo van Doorn181d6902008-02-05 16:42:23 -0500541 rt2x00dev->low_level_stats.dot11RTSFailureCount++;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700542 }
543
544 /*
Johannes Berge039fa42008-05-15 12:55:29 +0200545 * Only send the status report to mac80211 when TX status was
546 * requested by it. If this was a extra frame coming through
547 * a mac80211 library call (RTS/CTS) then we should not send the
548 * status report back.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700549 */
Johannes Berge039fa42008-05-15 12:55:29 +0200550 if (tx_info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
551 ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb);
Ivo van Doornbaf26a72008-02-17 17:32:08 +0100552 else
Ivo van Doornfb55f4d12008-05-10 13:42:06 +0200553 dev_kfree_skb_irq(entry->skb);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700554 entry->skb = NULL;
555}
556EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
557
Ivo van Doorn181d6902008-02-05 16:42:23 -0500558void rt2x00lib_rxdone(struct queue_entry *entry,
559 struct rxdone_entry_desc *rxdesc)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700560{
Ivo van Doorn181d6902008-02-05 16:42:23 -0500561 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700562 struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
Gertjan van Wingerde239c2492008-06-06 22:54:12 +0200563 unsigned int header_size = ieee80211_get_hdrlen_from_skb(entry->skb);
Johannes Berg8318d782008-01-24 19:38:38 +0100564 struct ieee80211_supported_band *sband;
Mattias Nissler61af43c2007-11-27 21:50:26 +0100565 struct ieee80211_hdr *hdr;
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100566 const struct rt2x00_rate *rate;
Gertjan van Wingerde239c2492008-06-06 22:54:12 +0200567 unsigned int align;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700568 unsigned int i;
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100569 int idx = -1;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700570
571 /*
Gertjan van Wingerde239c2492008-06-06 22:54:12 +0200572 * The data behind the ieee80211 header must be
Gertjan van Wingerdea9f853d2008-06-07 16:57:09 +0200573 * aligned on a 4 byte boundary.
Gertjan van Wingerde239c2492008-06-06 22:54:12 +0200574 */
Gertjan van Wingerdea9f853d2008-06-07 16:57:09 +0200575 align = ((unsigned long)(entry->skb->data + header_size)) & 3;
Gertjan van Wingerde239c2492008-06-06 22:54:12 +0200576
577 if (align) {
578 skb_push(entry->skb, align);
579 /* Move entire frame in 1 command */
580 memmove(entry->skb->data, entry->skb->data + align,
581 rxdesc->size);
582 }
583
584 /* Update data pointers, trim buffer to correct size */
585 skb_trim(entry->skb, rxdesc->size);
586
587 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700588 * Update RX statistics.
589 */
Johannes Berg8318d782008-01-24 19:38:38 +0100590 sband = &rt2x00dev->bands[rt2x00dev->curr_band];
591 for (i = 0; i < sband->n_bitrates; i++) {
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100592 rate = rt2x00_get_rate(sband->bitrates[i].hw_value);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700593
Ivo van Doorn19d30e02008-03-15 21:38:07 +0100594 if (((rxdesc->dev_flags & RXDONE_SIGNAL_PLCP) &&
595 (rate->plcp == rxdesc->signal)) ||
596 (!(rxdesc->dev_flags & RXDONE_SIGNAL_PLCP) &&
597 (rate->bitrate == rxdesc->signal))) {
Johannes Berg8318d782008-01-24 19:38:38 +0100598 idx = i;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700599 break;
600 }
601 }
602
Ivo van Doorn866a0502008-03-25 14:12:45 +0100603 if (idx < 0) {
604 WARNING(rt2x00dev, "Frame received with unrecognized signal,"
605 "signal=0x%.2x, plcp=%d.\n", rxdesc->signal,
606 !!(rxdesc->dev_flags & RXDONE_SIGNAL_PLCP));
607 idx = 0;
608 }
609
Mattias Nissler61af43c2007-11-27 21:50:26 +0100610 /*
Ivo van Doorn7e56d382008-01-06 23:41:28 +0100611 * Only update link status if this is a beacon frame carrying our bssid.
Mattias Nissler61af43c2007-11-27 21:50:26 +0100612 */
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100613 hdr = (struct ieee80211_hdr *)entry->skb->data;
Ivo van Doornac104462008-06-16 19:54:57 +0200614 if (ieee80211_is_beacon(hdr->frame_control) &&
615 (rxdesc->dev_flags & RXDONE_MY_BSS))
Ivo van Doorn181d6902008-02-05 16:42:23 -0500616 rt2x00lib_update_link_stats(&rt2x00dev->link, rxdesc->rssi);
Mattias Nissler61af43c2007-11-27 21:50:26 +0100617
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200618 rt2x00dev->link.qual.rx_success++;
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200619
Johannes Berg8318d782008-01-24 19:38:38 +0100620 rx_status->rate_idx = idx;
Bruno Randolf566bfe52008-05-08 19:15:40 +0200621 rx_status->qual =
Ivo van Doorn181d6902008-02-05 16:42:23 -0500622 rt2x00lib_calculate_link_signal(rt2x00dev, rxdesc->rssi);
Bruno Randolf566bfe52008-05-08 19:15:40 +0200623 rx_status->signal = rxdesc->rssi;
Ivo van Doorn181d6902008-02-05 16:42:23 -0500624 rx_status->flag = rxdesc->flags;
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200625 rx_status->antenna = rt2x00dev->link.ant.active.rx;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700626
627 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500628 * Send frame to mac80211 & debugfs.
629 * mac80211 will clean up the skb structure.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700630 */
Ivo van Doorn5a6e5992008-05-10 13:41:32 +0200631 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_RXDONE, entry->skb);
Ivo van Doorn181d6902008-02-05 16:42:23 -0500632 ieee80211_rx_irqsafe(rt2x00dev->hw, entry->skb, rx_status);
633 entry->skb = NULL;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700634}
635EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
636
637/*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700638 * Driver initialization handlers.
639 */
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100640const struct rt2x00_rate rt2x00_supported_rates[12] = {
641 {
Ivo van Doornaa776722008-03-09 22:48:08 +0100642 .flags = DEV_RATE_CCK | DEV_RATE_BASIC,
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100643 .bitrate = 10,
Ivo van Doornaa776722008-03-09 22:48:08 +0100644 .ratemask = BIT(0),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100645 .plcp = 0x00,
646 },
647 {
Ivo van Doornaa776722008-03-09 22:48:08 +0100648 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE | DEV_RATE_BASIC,
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100649 .bitrate = 20,
Ivo van Doornaa776722008-03-09 22:48:08 +0100650 .ratemask = BIT(1),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100651 .plcp = 0x01,
652 },
653 {
Ivo van Doornaa776722008-03-09 22:48:08 +0100654 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE | DEV_RATE_BASIC,
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100655 .bitrate = 55,
Ivo van Doornaa776722008-03-09 22:48:08 +0100656 .ratemask = BIT(2),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100657 .plcp = 0x02,
658 },
659 {
Ivo van Doornaa776722008-03-09 22:48:08 +0100660 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE | DEV_RATE_BASIC,
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100661 .bitrate = 110,
Ivo van Doornaa776722008-03-09 22:48:08 +0100662 .ratemask = BIT(3),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100663 .plcp = 0x03,
664 },
665 {
Ivo van Doornaa776722008-03-09 22:48:08 +0100666 .flags = DEV_RATE_OFDM | DEV_RATE_BASIC,
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100667 .bitrate = 60,
Ivo van Doornaa776722008-03-09 22:48:08 +0100668 .ratemask = BIT(4),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100669 .plcp = 0x0b,
670 },
671 {
672 .flags = DEV_RATE_OFDM,
673 .bitrate = 90,
Ivo van Doornaa776722008-03-09 22:48:08 +0100674 .ratemask = BIT(5),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100675 .plcp = 0x0f,
676 },
677 {
Ivo van Doornaa776722008-03-09 22:48:08 +0100678 .flags = DEV_RATE_OFDM | DEV_RATE_BASIC,
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100679 .bitrate = 120,
Ivo van Doornaa776722008-03-09 22:48:08 +0100680 .ratemask = BIT(6),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100681 .plcp = 0x0a,
682 },
683 {
684 .flags = DEV_RATE_OFDM,
685 .bitrate = 180,
Ivo van Doornaa776722008-03-09 22:48:08 +0100686 .ratemask = BIT(7),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100687 .plcp = 0x0e,
688 },
689 {
Ivo van Doornaa776722008-03-09 22:48:08 +0100690 .flags = DEV_RATE_OFDM | DEV_RATE_BASIC,
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100691 .bitrate = 240,
Ivo van Doornaa776722008-03-09 22:48:08 +0100692 .ratemask = BIT(8),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100693 .plcp = 0x09,
694 },
695 {
696 .flags = DEV_RATE_OFDM,
697 .bitrate = 360,
Ivo van Doornaa776722008-03-09 22:48:08 +0100698 .ratemask = BIT(9),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100699 .plcp = 0x0d,
700 },
701 {
702 .flags = DEV_RATE_OFDM,
703 .bitrate = 480,
Ivo van Doornaa776722008-03-09 22:48:08 +0100704 .ratemask = BIT(10),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100705 .plcp = 0x08,
706 },
707 {
708 .flags = DEV_RATE_OFDM,
709 .bitrate = 540,
Ivo van Doornaa776722008-03-09 22:48:08 +0100710 .ratemask = BIT(11),
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100711 .plcp = 0x0c,
712 },
713};
714
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700715static void rt2x00lib_channel(struct ieee80211_channel *entry,
716 const int channel, const int tx_power,
717 const int value)
718{
Ivo van Doornf2a3c7f2008-02-03 15:52:45 +0100719 entry->center_freq = ieee80211_channel_to_frequency(channel);
Johannes Berg8318d782008-01-24 19:38:38 +0100720 entry->hw_value = value;
721 entry->max_power = tx_power;
722 entry->max_antenna_gain = 0xff;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700723}
724
725static void rt2x00lib_rate(struct ieee80211_rate *entry,
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100726 const u16 index, const struct rt2x00_rate *rate)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700727{
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100728 entry->flags = 0;
729 entry->bitrate = rate->bitrate;
730 entry->hw_value = rt2x00_create_rate_hw_value(index, 0);
Johannes Berg8318d782008-01-24 19:38:38 +0100731 entry->hw_value_short = entry->hw_value;
Ivo van Doorn70e2fed2008-02-03 15:50:40 +0100732
733 if (rate->flags & DEV_RATE_SHORT_PREAMBLE) {
734 entry->flags |= IEEE80211_RATE_SHORT_PREAMBLE;
735 entry->hw_value_short |= rt2x00_create_rate_hw_value(index, 1);
736 }
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700737}
738
739static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
740 struct hw_mode_spec *spec)
741{
742 struct ieee80211_hw *hw = rt2x00dev->hw;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700743 struct ieee80211_channel *channels;
744 struct ieee80211_rate *rates;
Ivo van Doorn31562e82008-02-17 17:35:05 +0100745 unsigned int num_rates;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700746 unsigned int i;
747 unsigned char tx_power;
748
Ivo van Doorn31562e82008-02-17 17:35:05 +0100749 num_rates = 0;
750 if (spec->supported_rates & SUPPORT_RATE_CCK)
751 num_rates += 4;
752 if (spec->supported_rates & SUPPORT_RATE_OFDM)
753 num_rates += 8;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700754
755 channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
756 if (!channels)
Johannes Berg8318d782008-01-24 19:38:38 +0100757 return -ENOMEM;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700758
Ivo van Doorn31562e82008-02-17 17:35:05 +0100759 rates = kzalloc(sizeof(*rates) * num_rates, GFP_KERNEL);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700760 if (!rates)
761 goto exit_free_channels;
762
763 /*
764 * Initialize Rate list.
765 */
Ivo van Doorn31562e82008-02-17 17:35:05 +0100766 for (i = 0; i < num_rates; i++)
Ivo van Doorn8f5fa7f2008-02-03 15:54:34 +0100767 rt2x00lib_rate(&rates[i], i, rt2x00_get_rate(i));
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700768
769 /*
770 * Initialize Channel list.
771 */
772 for (i = 0; i < spec->num_channels; i++) {
Ivo van Doorn31562e82008-02-17 17:35:05 +0100773 if (spec->channels[i].channel <= 14) {
774 if (spec->tx_power_bg)
775 tx_power = spec->tx_power_bg[i];
776 else
777 tx_power = spec->tx_power_default;
778 } else {
779 if (spec->tx_power_a)
780 tx_power = spec->tx_power_a[i];
781 else
782 tx_power = spec->tx_power_default;
783 }
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700784
785 rt2x00lib_channel(&channels[i],
786 spec->channels[i].channel, tx_power, i);
787 }
788
789 /*
Ivo van Doorn31562e82008-02-17 17:35:05 +0100790 * Intitialize 802.11b, 802.11g
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700791 * Rates: CCK, OFDM.
Johannes Berg8318d782008-01-24 19:38:38 +0100792 * Channels: 2.4 GHz
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700793 */
Gertjan van Wingerde47ac2682008-02-17 17:35:55 +0100794 if (spec->supported_bands & SUPPORT_BAND_2GHZ) {
Ivo van Doorn31562e82008-02-17 17:35:05 +0100795 rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_channels = 14;
796 rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_bitrates = num_rates;
797 rt2x00dev->bands[IEEE80211_BAND_2GHZ].channels = channels;
798 rt2x00dev->bands[IEEE80211_BAND_2GHZ].bitrates = rates;
799 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
800 &rt2x00dev->bands[IEEE80211_BAND_2GHZ];
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700801 }
802
803 /*
804 * Intitialize 802.11a
805 * Rates: OFDM.
806 * Channels: OFDM, UNII, HiperLAN2.
807 */
Gertjan van Wingerde47ac2682008-02-17 17:35:55 +0100808 if (spec->supported_bands & SUPPORT_BAND_5GHZ) {
Ivo van Doorn31562e82008-02-17 17:35:05 +0100809 rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_channels =
810 spec->num_channels - 14;
811 rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_bitrates =
812 num_rates - 4;
813 rt2x00dev->bands[IEEE80211_BAND_5GHZ].channels = &channels[14];
814 rt2x00dev->bands[IEEE80211_BAND_5GHZ].bitrates = &rates[4];
815 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
816 &rt2x00dev->bands[IEEE80211_BAND_5GHZ];
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700817 }
818
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700819 return 0;
820
Johannes Berg8318d782008-01-24 19:38:38 +0100821 exit_free_channels:
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700822 kfree(channels);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700823 ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
824 return -ENOMEM;
825}
826
827static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
828{
Ivo van Doorn066cb632007-09-25 20:55:39 +0200829 if (test_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700830 ieee80211_unregister_hw(rt2x00dev->hw);
831
Johannes Berg8318d782008-01-24 19:38:38 +0100832 if (likely(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ])) {
833 kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->channels);
834 kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->bitrates);
835 rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
836 rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700837 }
838}
839
840static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
841{
842 struct hw_mode_spec *spec = &rt2x00dev->spec;
843 int status;
844
845 /*
846 * Initialize HW modes.
847 */
848 status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
849 if (status)
850 return status;
851
852 /*
Gertjan van Wingerde61448f82008-05-10 13:43:33 +0200853 * Initialize HW fields.
854 */
855 rt2x00dev->hw->queues = rt2x00dev->ops->tx_queues;
856
857 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700858 * Register HW.
859 */
860 status = ieee80211_register_hw(rt2x00dev->hw);
861 if (status) {
862 rt2x00lib_remove_hw(rt2x00dev);
863 return status;
864 }
865
Ivo van Doorn066cb632007-09-25 20:55:39 +0200866 __set_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700867
868 return 0;
869}
870
871/*
872 * Initialization/uninitialization handlers.
873 */
Ivo van Doorne37ea212008-01-06 23:40:07 +0100874static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700875{
876 if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
877 return;
878
879 /*
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100880 * Unregister extra components.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700881 */
882 rt2x00rfkill_unregister(rt2x00dev);
883
884 /*
885 * Allow the HW to uninitialize.
886 */
887 rt2x00dev->ops->lib->uninitialize(rt2x00dev);
888
889 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500890 * Free allocated queue entries.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700891 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500892 rt2x00queue_uninitialize(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700893}
894
Ivo van Doorne37ea212008-01-06 23:40:07 +0100895static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700896{
897 int status;
898
899 if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
900 return 0;
901
902 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -0500903 * Allocate all queue entries.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700904 */
Ivo van Doorn181d6902008-02-05 16:42:23 -0500905 status = rt2x00queue_initialize(rt2x00dev);
906 if (status)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700907 return status;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700908
909 /*
910 * Initialize the device.
911 */
912 status = rt2x00dev->ops->lib->initialize(rt2x00dev);
Ivo van Doorned499982008-05-05 17:23:47 +0200913 if (status) {
914 rt2x00queue_uninitialize(rt2x00dev);
915 return status;
916 }
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700917
918 __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
919
920 /*
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100921 * Register the extra components.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700922 */
Ivo van Doorn1682fe62008-03-13 15:38:03 +0100923 rt2x00rfkill_register(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700924
925 return 0;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700926}
927
Ivo van Doorne37ea212008-01-06 23:40:07 +0100928int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
929{
930 int retval;
931
932 if (test_bit(DEVICE_STARTED, &rt2x00dev->flags))
933 return 0;
934
935 /*
936 * If this is the first interface which is added,
937 * we should load the firmware now.
938 */
Ivo van Doorn9404ef32008-02-03 15:48:38 +0100939 retval = rt2x00lib_load_firmware(rt2x00dev);
940 if (retval)
941 return retval;
Ivo van Doorne37ea212008-01-06 23:40:07 +0100942
943 /*
944 * Initialize the device.
945 */
946 retval = rt2x00lib_initialize(rt2x00dev);
947 if (retval)
948 return retval;
949
950 /*
951 * Enable radio.
952 */
953 retval = rt2x00lib_enable_radio(rt2x00dev);
954 if (retval) {
955 rt2x00lib_uninitialize(rt2x00dev);
956 return retval;
957 }
958
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100959 rt2x00dev->intf_ap_count = 0;
960 rt2x00dev->intf_sta_count = 0;
961 rt2x00dev->intf_associated = 0;
962
Ivo van Doorne37ea212008-01-06 23:40:07 +0100963 __set_bit(DEVICE_STARTED, &rt2x00dev->flags);
964
965 return 0;
966}
967
968void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
969{
970 if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
971 return;
972
973 /*
974 * Perhaps we can add something smarter here,
975 * but for now just disabling the radio should do.
976 */
977 rt2x00lib_disable_radio(rt2x00dev);
978
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100979 rt2x00dev->intf_ap_count = 0;
980 rt2x00dev->intf_sta_count = 0;
981 rt2x00dev->intf_associated = 0;
982
Ivo van Doorne37ea212008-01-06 23:40:07 +0100983 __clear_bit(DEVICE_STARTED, &rt2x00dev->flags);
984}
985
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700986/*
987 * driver allocation handlers.
988 */
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700989int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
990{
991 int retval = -ENOMEM;
992
993 /*
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +0100994 * Make room for rt2x00_intf inside the per-interface
995 * structure ieee80211_vif.
996 */
997 rt2x00dev->hw->vif_data_size = sizeof(struct rt2x00_intf);
998
999 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001000 * Let the driver probe the device to detect the capabilities.
1001 */
1002 retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
1003 if (retval) {
1004 ERROR(rt2x00dev, "Failed to allocate device.\n");
1005 goto exit;
1006 }
1007
1008 /*
1009 * Initialize configuration work.
1010 */
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +01001011 INIT_WORK(&rt2x00dev->intf_work, rt2x00lib_intf_scheduled);
Johannes Berg4150c572007-09-17 01:29:23 -04001012 INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001013 INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
1014
1015 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -05001016 * Allocate queue array.
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001017 */
Ivo van Doorn181d6902008-02-05 16:42:23 -05001018 retval = rt2x00queue_allocate(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001019 if (retval)
1020 goto exit;
1021
1022 /*
1023 * Initialize ieee80211 structure.
1024 */
1025 retval = rt2x00lib_probe_hw(rt2x00dev);
1026 if (retval) {
1027 ERROR(rt2x00dev, "Failed to initialize hw.\n");
1028 goto exit;
1029 }
1030
1031 /*
Ivo van Doorn1682fe62008-03-13 15:38:03 +01001032 * Register extra components.
Ivo van Doorna9450b72008-02-03 15:53:40 +01001033 */
1034 rt2x00leds_register(rt2x00dev);
Ivo van Doorn1682fe62008-03-13 15:38:03 +01001035 rt2x00rfkill_allocate(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001036 rt2x00debug_register(rt2x00dev);
1037
Ivo van Doorn066cb632007-09-25 20:55:39 +02001038 __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1039
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001040 return 0;
1041
1042exit:
1043 rt2x00lib_remove_dev(rt2x00dev);
1044
1045 return retval;
1046}
1047EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
1048
1049void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
1050{
Ivo van Doorn066cb632007-09-25 20:55:39 +02001051 __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1052
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001053 /*
1054 * Disable radio.
1055 */
1056 rt2x00lib_disable_radio(rt2x00dev);
1057
1058 /*
1059 * Uninitialize device.
1060 */
1061 rt2x00lib_uninitialize(rt2x00dev);
1062
1063 /*
Ivo van Doorn1682fe62008-03-13 15:38:03 +01001064 * Free extra components
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001065 */
1066 rt2x00debug_deregister(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001067 rt2x00rfkill_free(rt2x00dev);
Ivo van Doorna9450b72008-02-03 15:53:40 +01001068 rt2x00leds_unregister(rt2x00dev);
1069
1070 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001071 * Free ieee80211_hw memory.
1072 */
1073 rt2x00lib_remove_hw(rt2x00dev);
1074
1075 /*
1076 * Free firmware image.
1077 */
1078 rt2x00lib_free_firmware(rt2x00dev);
1079
1080 /*
Ivo van Doorn181d6902008-02-05 16:42:23 -05001081 * Free queue structures.
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001082 */
Ivo van Doorn181d6902008-02-05 16:42:23 -05001083 rt2x00queue_free(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001084}
1085EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
1086
1087/*
1088 * Device state handlers
1089 */
1090#ifdef CONFIG_PM
1091int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
1092{
1093 int retval;
1094
1095 NOTICE(rt2x00dev, "Going to sleep.\n");
Ivo van Doorn066cb632007-09-25 20:55:39 +02001096 __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1097
1098 /*
1099 * Only continue if mac80211 has open interfaces.
1100 */
1101 if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1102 goto exit;
Ivo van Doorn6d7f9872007-10-06 14:12:42 +02001103 __set_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001104
1105 /*
Ivo van Doorn1682fe62008-03-13 15:38:03 +01001106 * Disable radio.
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001107 */
Ivo van Doorne37ea212008-01-06 23:40:07 +01001108 rt2x00lib_stop(rt2x00dev);
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 Doorn1682fe62008-03-13 15:38:03 +01001115 rt2x00rfkill_suspend(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001116 rt2x00debug_deregister(rt2x00dev);
1117
Ivo van Doorn066cb632007-09-25 20:55:39 +02001118exit:
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001119 /*
Ivo van Doorn98963222008-03-27 17:15:24 +01001120 * Set device mode to sleep for power management,
1121 * on some hardware this call seems to consistently fail.
1122 * From the specifications it is hard to tell why it fails,
1123 * and if this is a "bad thing".
1124 * Overall it is safe to just ignore the failure and
1125 * continue suspending. The only downside is that the
1126 * device will not be in optimal power save mode, but with
1127 * the radio and the other components already disabled the
1128 * device is as good as disabled.
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001129 */
1130 retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
1131 if (retval)
Ivo van Doorn98963222008-03-27 17:15:24 +01001132 WARNING(rt2x00dev, "Device failed to enter sleep state, "
1133 "continue suspending.\n");
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001134
1135 return 0;
1136}
1137EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1138
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +01001139static void rt2x00lib_resume_intf(void *data, u8 *mac,
1140 struct ieee80211_vif *vif)
1141{
1142 struct rt2x00_dev *rt2x00dev = data;
1143 struct rt2x00_intf *intf = vif_to_intf(vif);
1144
1145 spin_lock(&intf->lock);
1146
1147 rt2x00lib_config_intf(rt2x00dev, intf,
1148 vif->type, intf->mac, intf->bssid);
1149
1150
1151 /*
1152 * Master or Ad-hoc mode require a new beacon update.
1153 */
1154 if (vif->type == IEEE80211_IF_TYPE_AP ||
1155 vif->type == IEEE80211_IF_TYPE_IBSS)
1156 intf->delayed_flags |= DELAYED_UPDATE_BEACON;
1157
1158 spin_unlock(&intf->lock);
1159}
1160
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001161int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1162{
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001163 int retval;
1164
1165 NOTICE(rt2x00dev, "Waking up.\n");
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001166
1167 /*
Ivo van Doorn1682fe62008-03-13 15:38:03 +01001168 * Restore/enable extra components.
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001169 */
1170 rt2x00debug_register(rt2x00dev);
Ivo van Doorn1682fe62008-03-13 15:38:03 +01001171 rt2x00rfkill_resume(rt2x00dev);
Ivo van Doorna9450b72008-02-03 15:53:40 +01001172 rt2x00leds_resume(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001173
1174 /*
Ivo van Doorn6d7f9872007-10-06 14:12:42 +02001175 * Only continue if mac80211 had open interfaces.
Ivo van Doorn066cb632007-09-25 20:55:39 +02001176 */
Ivo van Doorn6d7f9872007-10-06 14:12:42 +02001177 if (!__test_and_clear_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags))
Ivo van Doorn066cb632007-09-25 20:55:39 +02001178 return 0;
1179
1180 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001181 * Reinitialize device and all active interfaces.
1182 */
Ivo van Doorne37ea212008-01-06 23:40:07 +01001183 retval = rt2x00lib_start(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001184 if (retval)
1185 goto exit;
1186
1187 /*
1188 * Reconfigure device.
1189 */
Ivo van Doorn066cb632007-09-25 20:55:39 +02001190 rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1);
1191 if (!rt2x00dev->hw->conf.radio_enabled)
1192 rt2x00lib_disable_radio(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001193
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +01001194 /*
1195 * Iterator over each active interface to
1196 * reconfigure the hardware.
1197 */
1198 ieee80211_iterate_active_interfaces(rt2x00dev->hw,
1199 rt2x00lib_resume_intf, rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001200
1201 /*
Ivo van Doorne37ea212008-01-06 23:40:07 +01001202 * We are ready again to receive requests from mac80211.
1203 */
1204 __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1205
1206 /*
Ivo van Doorn066cb632007-09-25 20:55:39 +02001207 * It is possible that during that mac80211 has attempted
1208 * to send frames while we were suspending or resuming.
1209 * In that case we have disabled the TX queue and should
1210 * now enable it again
1211 */
Johannes Berg36d68252008-05-15 12:55:26 +02001212 ieee80211_wake_queues(rt2x00dev->hw);
Ivo van Doorn066cb632007-09-25 20:55:39 +02001213
1214 /*
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +01001215 * During interface iteration we might have changed the
1216 * delayed_flags, time to handles the event by calling
1217 * the work handler directly.
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001218 */
Ivo van Doorn6bb40dd2008-02-03 15:49:59 +01001219 rt2x00lib_intf_scheduled(&rt2x00dev->intf_work);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001220
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001221 return 0;
1222
1223exit:
1224 rt2x00lib_disable_radio(rt2x00dev);
1225 rt2x00lib_uninitialize(rt2x00dev);
1226 rt2x00debug_deregister(rt2x00dev);
1227
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001228 return retval;
1229}
1230EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1231#endif /* CONFIG_PM */
1232
1233/*
1234 * rt2x00lib module information.
1235 */
1236MODULE_AUTHOR(DRV_PROJECT);
1237MODULE_VERSION(DRV_VERSION);
1238MODULE_DESCRIPTION("rt2x00 library");
1239MODULE_LICENSE("GPL");