blob: 0d51f478bcdfcd7ba842288548c1b5b43346daa3 [file] [log] [blame]
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001/*
2 Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
3 <http://rt2x00.serialmonkey.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the
17 Free Software Foundation, Inc.,
18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21/*
22 Module: 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"
Ivo van Doorn4d8dd662007-11-27 21:49:29 +010031#include "rt2x00dump.h"
Ivo van Doorn95ea3622007-09-25 17:57:13 -070032
33/*
34 * Ring handler.
35 */
36struct data_ring *rt2x00lib_get_ring(struct rt2x00_dev *rt2x00dev,
37 const unsigned int queue)
38{
Ivo van Doorn066cb632007-09-25 20:55:39 +020039 int beacon = test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070040
41 /*
42 * Check if we are requesting a reqular TX ring,
43 * or if we are requesting a Beacon or Atim ring.
44 * For Atim rings, we should check if it is supported.
45 */
46 if (queue < rt2x00dev->hw->queues && rt2x00dev->tx)
47 return &rt2x00dev->tx[queue];
48
49 if (!rt2x00dev->bcn || !beacon)
50 return NULL;
51
52 if (queue == IEEE80211_TX_QUEUE_BEACON)
53 return &rt2x00dev->bcn[0];
54 else if (queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
55 return &rt2x00dev->bcn[1];
56
57 return NULL;
58}
59EXPORT_SYMBOL_GPL(rt2x00lib_get_ring);
60
61/*
62 * Link tuning handlers
63 */
Ivo van Doorn53b3f8e2008-02-25 23:15:13 +010064void rt2x00lib_reset_link_tuner(struct rt2x00_dev *rt2x00dev)
Ivo van Doorn95ea3622007-09-25 17:57:13 -070065{
Ivo van Doorn53b3f8e2008-02-25 23:15:13 +010066 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
67 return;
68
69 /*
70 * Reset link information.
71 * Both the currently active vgc level as well as
72 * the link tuner counter should be reset. Resetting
73 * the counter is important for devices where the
74 * device should only perform link tuning during the
75 * first minute after being enabled.
76 */
Ivo van Doorn8de8c512007-10-13 16:26:32 +020077 rt2x00dev->link.count = 0;
78 rt2x00dev->link.vgc_level = 0;
79
Ivo van Doorn53b3f8e2008-02-25 23:15:13 +010080 /*
81 * Reset the link tuner.
82 */
83 rt2x00dev->ops->lib->reset_tuner(rt2x00dev);
84}
85
86static void rt2x00lib_start_link_tuner(struct rt2x00_dev *rt2x00dev)
87{
88 /*
89 * Clear all (possibly) pre-existing quality statistics.
90 */
Ivo van Doorn8de8c512007-10-13 16:26:32 +020091 memset(&rt2x00dev->link.qual, 0, sizeof(rt2x00dev->link.qual));
92
93 /*
94 * The RX and TX percentage should start at 50%
95 * this will assure we will get at least get some
96 * decent value when the link tuner starts.
97 * The value will be dropped and overwritten with
98 * the correct (measured )value anyway during the
99 * first run of the link tuner.
100 */
101 rt2x00dev->link.qual.rx_percentage = 50;
102 rt2x00dev->link.qual.tx_percentage = 50;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700103
Ivo van Doorn53b3f8e2008-02-25 23:15:13 +0100104 rt2x00lib_reset_link_tuner(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700105
106 queue_delayed_work(rt2x00dev->hw->workqueue,
107 &rt2x00dev->link.work, LINK_TUNE_INTERVAL);
108}
109
110static void rt2x00lib_stop_link_tuner(struct rt2x00_dev *rt2x00dev)
111{
Ivo van Doorn3e309682007-09-25 20:56:36 +0200112 cancel_delayed_work_sync(&rt2x00dev->link.work);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700113}
114
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700115/*
Ivo van Doorn837e7f22008-01-06 23:41:45 +0100116 * Ring initialization
117 */
118static void rt2x00lib_init_rxrings(struct rt2x00_dev *rt2x00dev)
119{
120 struct data_ring *ring = rt2x00dev->rx;
121 unsigned int i;
122
123 if (!rt2x00dev->ops->lib->init_rxentry)
124 return;
125
126 if (ring->data_addr)
127 memset(ring->data_addr, 0, rt2x00_get_ring_size(ring));
128
129 for (i = 0; i < ring->stats.limit; i++)
130 rt2x00dev->ops->lib->init_rxentry(rt2x00dev, &ring->entry[i]);
131
132 rt2x00_ring_index_clear(ring);
133}
134
135static void rt2x00lib_init_txrings(struct rt2x00_dev *rt2x00dev)
136{
137 struct data_ring *ring;
138 unsigned int i;
139
140 if (!rt2x00dev->ops->lib->init_txentry)
141 return;
142
143 txringall_for_each(rt2x00dev, ring) {
144 if (ring->data_addr)
145 memset(ring->data_addr, 0, rt2x00_get_ring_size(ring));
146
147 for (i = 0; i < ring->stats.limit; i++)
148 rt2x00dev->ops->lib->init_txentry(rt2x00dev,
149 &ring->entry[i]);
150
151 rt2x00_ring_index_clear(ring);
152 }
153}
154
155/*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700156 * Radio control handlers.
157 */
158int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
159{
160 int status;
161
162 /*
163 * Don't enable the radio twice.
164 * And check if the hardware button has been disabled.
165 */
166 if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
Ivo van Doorn81873e92007-10-06 14:14:06 +0200167 test_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700168 return 0;
169
170 /*
Ivo van Doorn837e7f22008-01-06 23:41:45 +0100171 * Initialize all data rings.
172 */
173 rt2x00lib_init_rxrings(rt2x00dev);
174 rt2x00lib_init_txrings(rt2x00dev);
175
176 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700177 * Enable radio.
178 */
179 status = rt2x00dev->ops->lib->set_device_state(rt2x00dev,
180 STATE_RADIO_ON);
181 if (status)
182 return status;
183
184 __set_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags);
185
186 /*
187 * Enable RX.
188 */
Ivo van Doorn5cbf8302007-10-06 14:16:09 +0200189 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700190
191 /*
192 * Start the TX queues.
193 */
194 ieee80211_start_queues(rt2x00dev->hw);
195
196 return 0;
197}
198
199void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
200{
201 if (!__test_and_clear_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
202 return;
203
204 /*
Johannes Berg4150c572007-09-17 01:29:23 -0400205 * Stop all scheduled work.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700206 */
207 if (work_pending(&rt2x00dev->beacon_work))
208 cancel_work_sync(&rt2x00dev->beacon_work);
Johannes Berg4150c572007-09-17 01:29:23 -0400209 if (work_pending(&rt2x00dev->filter_work))
210 cancel_work_sync(&rt2x00dev->filter_work);
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200211 if (work_pending(&rt2x00dev->config_work))
212 cancel_work_sync(&rt2x00dev->config_work);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700213
214 /*
215 * Stop the TX queues.
216 */
217 ieee80211_stop_queues(rt2x00dev->hw);
218
219 /*
220 * Disable RX.
221 */
Ivo van Doorn5cbf8302007-10-06 14:16:09 +0200222 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700223
224 /*
225 * Disable radio.
226 */
227 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
228}
229
Ivo van Doorn5cbf8302007-10-06 14:16:09 +0200230void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, enum dev_state state)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700231{
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700232 /*
233 * When we are disabling the RX, we should also stop the link tuner.
234 */
Ivo van Doorn5cbf8302007-10-06 14:16:09 +0200235 if (state == STATE_RADIO_RX_OFF)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700236 rt2x00lib_stop_link_tuner(rt2x00dev);
237
238 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
239
240 /*
241 * When we are enabling the RX, we should also start the link tuner.
242 */
Ivo van Doorn5cbf8302007-10-06 14:16:09 +0200243 if (state == STATE_RADIO_RX_ON &&
244 is_interface_present(&rt2x00dev->interface))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700245 rt2x00lib_start_link_tuner(rt2x00dev);
246}
247
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200248static void rt2x00lib_evaluate_antenna_sample(struct rt2x00_dev *rt2x00dev)
249{
250 enum antenna rx = rt2x00dev->link.ant.active.rx;
251 enum antenna tx = rt2x00dev->link.ant.active.tx;
252 int sample_a =
253 rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_A);
254 int sample_b =
255 rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_B);
256
257 /*
258 * We are done sampling. Now we should evaluate the results.
259 */
260 rt2x00dev->link.ant.flags &= ~ANTENNA_MODE_SAMPLE;
261
262 /*
263 * During the last period we have sampled the RSSI
264 * from both antenna's. It now is time to determine
265 * which antenna demonstrated the best performance.
266 * When we are already on the antenna with the best
267 * performance, then there really is nothing for us
268 * left to do.
269 */
270 if (sample_a == sample_b)
271 return;
272
Ivo van Doorn05253c92008-02-25 23:15:08 +0100273 if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY)
274 rx = (sample_a > sample_b) ? ANTENNA_A : ANTENNA_B;
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200275
Ivo van Doorn05253c92008-02-25 23:15:08 +0100276 if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)
277 tx = (sample_a > sample_b) ? ANTENNA_A : ANTENNA_B;
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200278
279 rt2x00lib_config_antenna(rt2x00dev, rx, tx);
280}
281
282static void rt2x00lib_evaluate_antenna_eval(struct rt2x00_dev *rt2x00dev)
283{
284 enum antenna rx = rt2x00dev->link.ant.active.rx;
285 enum antenna tx = rt2x00dev->link.ant.active.tx;
286 int rssi_curr = rt2x00_get_link_ant_rssi(&rt2x00dev->link);
287 int rssi_old = rt2x00_update_ant_rssi(&rt2x00dev->link, rssi_curr);
288
289 /*
290 * Legacy driver indicates that we should swap antenna's
291 * when the difference in RSSI is greater that 5. This
292 * also should be done when the RSSI was actually better
293 * then the previous sample.
294 * When the difference exceeds the threshold we should
295 * sample the rssi from the other antenna to make a valid
296 * comparison between the 2 antennas.
297 */
Ivo van Doornb290d432008-02-25 23:15:01 +0100298 if (abs(rssi_curr - rssi_old) < 5)
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200299 return;
300
301 rt2x00dev->link.ant.flags |= ANTENNA_MODE_SAMPLE;
302
303 if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY)
304 rx = (rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
305
306 if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)
307 tx = (tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
308
309 rt2x00lib_config_antenna(rt2x00dev, rx, tx);
310}
311
312static void rt2x00lib_evaluate_antenna(struct rt2x00_dev *rt2x00dev)
313{
314 /*
315 * Determine if software diversity is enabled for
316 * either the TX or RX antenna (or both).
317 * Always perform this check since within the link
318 * tuner interval the configuration might have changed.
319 */
320 rt2x00dev->link.ant.flags &= ~ANTENNA_RX_DIVERSITY;
321 rt2x00dev->link.ant.flags &= ~ANTENNA_TX_DIVERSITY;
322
323 if (rt2x00dev->hw->conf.antenna_sel_rx == 0 &&
Ivo van Doornb290d432008-02-25 23:15:01 +0100324 rt2x00dev->default_ant.rx == ANTENNA_SW_DIVERSITY)
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200325 rt2x00dev->link.ant.flags |= ANTENNA_RX_DIVERSITY;
326 if (rt2x00dev->hw->conf.antenna_sel_tx == 0 &&
Ivo van Doornb290d432008-02-25 23:15:01 +0100327 rt2x00dev->default_ant.tx == ANTENNA_SW_DIVERSITY)
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200328 rt2x00dev->link.ant.flags |= ANTENNA_TX_DIVERSITY;
329
330 if (!(rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY) &&
331 !(rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)) {
Ivo van Doorn05253c92008-02-25 23:15:08 +0100332 rt2x00dev->link.ant.flags = 0;
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200333 return;
334 }
335
336 /*
337 * If we have only sampled the data over the last period
338 * we should now harvest the data. Otherwise just evaluate
339 * the data. The latter should only be performed once
340 * every 2 seconds.
341 */
342 if (rt2x00dev->link.ant.flags & ANTENNA_MODE_SAMPLE)
343 rt2x00lib_evaluate_antenna_sample(rt2x00dev);
344 else if (rt2x00dev->link.count & 1)
345 rt2x00lib_evaluate_antenna_eval(rt2x00dev);
346}
347
348static void rt2x00lib_update_link_stats(struct link *link, int rssi)
349{
350 int avg_rssi = rssi;
351
352 /*
353 * Update global RSSI
354 */
355 if (link->qual.avg_rssi)
356 avg_rssi = MOVING_AVERAGE(link->qual.avg_rssi, rssi, 8);
357 link->qual.avg_rssi = avg_rssi;
358
359 /*
360 * Update antenna RSSI
361 */
362 if (link->ant.rssi_ant)
363 rssi = MOVING_AVERAGE(link->ant.rssi_ant, rssi, 8);
364 link->ant.rssi_ant = rssi;
365}
366
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200367static void rt2x00lib_precalculate_link_signal(struct link_qual *qual)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700368{
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200369 if (qual->rx_failed || qual->rx_success)
370 qual->rx_percentage =
371 (qual->rx_success * 100) /
372 (qual->rx_failed + qual->rx_success);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700373 else
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200374 qual->rx_percentage = 50;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700375
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200376 if (qual->tx_failed || qual->tx_success)
377 qual->tx_percentage =
378 (qual->tx_success * 100) /
379 (qual->tx_failed + qual->tx_success);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700380 else
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200381 qual->tx_percentage = 50;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700382
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200383 qual->rx_success = 0;
384 qual->rx_failed = 0;
385 qual->tx_success = 0;
386 qual->tx_failed = 0;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700387}
388
389static int rt2x00lib_calculate_link_signal(struct rt2x00_dev *rt2x00dev,
390 int rssi)
391{
392 int rssi_percentage = 0;
393 int signal;
394
395 /*
396 * We need a positive value for the RSSI.
397 */
398 if (rssi < 0)
399 rssi += rt2x00dev->rssi_offset;
400
401 /*
402 * Calculate the different percentages,
403 * which will be used for the signal.
404 */
405 if (rt2x00dev->rssi_offset)
406 rssi_percentage = (rssi * 100) / rt2x00dev->rssi_offset;
407
408 /*
409 * Add the individual percentages and use the WEIGHT
410 * defines to calculate the current link signal.
411 */
412 signal = ((WEIGHT_RSSI * rssi_percentage) +
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200413 (WEIGHT_TX * rt2x00dev->link.qual.tx_percentage) +
414 (WEIGHT_RX * rt2x00dev->link.qual.rx_percentage)) / 100;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700415
416 return (signal > 100) ? 100 : signal;
417}
418
419static void rt2x00lib_link_tuner(struct work_struct *work)
420{
421 struct rt2x00_dev *rt2x00dev =
422 container_of(work, struct rt2x00_dev, link.work.work);
423
424 /*
Ivo van Doorn25ab0022007-09-25 20:57:04 +0200425 * When the radio is shutting down we should
426 * immediately cease all link tuning.
427 */
428 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
429 return;
430
431 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700432 * Update statistics.
433 */
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200434 rt2x00dev->ops->lib->link_stats(rt2x00dev, &rt2x00dev->link.qual);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700435 rt2x00dev->low_level_stats.dot11FCSErrorCount +=
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200436 rt2x00dev->link.qual.rx_failed;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700437
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700438 /*
439 * Only perform the link tuning when Link tuning
440 * has been enabled (This could have been disabled from the EEPROM).
441 */
442 if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
443 rt2x00dev->ops->lib->link_tuner(rt2x00dev);
444
445 /*
Ivo van Doorn725d99d2007-09-25 20:53:20 +0200446 * Precalculate a portion of the link signal which is
447 * in based on the tx/rx success/failure counters.
448 */
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200449 rt2x00lib_precalculate_link_signal(&rt2x00dev->link.qual);
Ivo van Doorn725d99d2007-09-25 20:53:20 +0200450
451 /*
Ivo van Doorn53b3f8e2008-02-25 23:15:13 +0100452 * Evaluate antenna setup, make this the last step since this could
453 * possibly reset some statistics.
454 */
455 rt2x00lib_evaluate_antenna(rt2x00dev);
456
457 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700458 * Increase tuner counter, and reschedule the next link tuner run.
459 */
460 rt2x00dev->link.count++;
461 queue_delayed_work(rt2x00dev->hw->workqueue, &rt2x00dev->link.work,
462 LINK_TUNE_INTERVAL);
463}
464
Johannes Berg4150c572007-09-17 01:29:23 -0400465static void rt2x00lib_packetfilter_scheduled(struct work_struct *work)
466{
467 struct rt2x00_dev *rt2x00dev =
468 container_of(work, struct rt2x00_dev, filter_work);
Ivo van Doorn3c4f2082008-01-06 23:40:49 +0100469 unsigned int filter = rt2x00dev->packet_filter;
Ivo van Doorn5886d0d2007-10-06 14:13:38 +0200470
471 /*
472 * Since we had stored the filter inside interface.filter,
473 * we should now clear that field. Otherwise the driver will
474 * assume nothing has changed (*total_flags will be compared
475 * to interface.filter to determine if any action is required).
476 */
Ivo van Doorn3c4f2082008-01-06 23:40:49 +0100477 rt2x00dev->packet_filter = 0;
Johannes Berg4150c572007-09-17 01:29:23 -0400478
479 rt2x00dev->ops->hw->configure_filter(rt2x00dev->hw,
Ivo van Doorn5886d0d2007-10-06 14:13:38 +0200480 filter, &filter, 0, NULL);
Johannes Berg4150c572007-09-17 01:29:23 -0400481}
482
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200483static void rt2x00lib_configuration_scheduled(struct work_struct *work)
484{
485 struct rt2x00_dev *rt2x00dev =
486 container_of(work, struct rt2x00_dev, config_work);
Johannes Berg471b3ef2007-12-28 14:32:58 +0100487 struct ieee80211_bss_conf bss_conf;
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200488
Johannes Berg471b3ef2007-12-28 14:32:58 +0100489 bss_conf.use_short_preamble =
490 test_bit(CONFIG_SHORT_PREAMBLE, &rt2x00dev->flags);
491
492 /*
493 * FIXME: shouldn't invoke it this way because all other contents
494 * of bss_conf is invalid.
495 */
496 rt2x00mac_bss_info_changed(rt2x00dev->hw, rt2x00dev->interface.id,
497 &bss_conf, BSS_CHANGED_ERP_PREAMBLE);
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200498}
499
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700500/*
501 * Interrupt context handlers.
502 */
503static void rt2x00lib_beacondone_scheduled(struct work_struct *work)
504{
505 struct rt2x00_dev *rt2x00dev =
506 container_of(work, struct rt2x00_dev, beacon_work);
507 struct data_ring *ring =
508 rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_BEACON);
509 struct data_entry *entry = rt2x00_get_data_entry(ring);
510 struct sk_buff *skb;
511
512 skb = ieee80211_beacon_get(rt2x00dev->hw,
513 rt2x00dev->interface.id,
514 &entry->tx_status.control);
515 if (!skb)
516 return;
517
518 rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw, skb,
519 &entry->tx_status.control);
520
521 dev_kfree_skb(skb);
522}
523
524void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
525{
526 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
527 return;
528
529 queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->beacon_work);
530}
531EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
532
533void rt2x00lib_txdone(struct data_entry *entry,
534 const int status, const int retry)
535{
536 struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
537 struct ieee80211_tx_status *tx_status = &entry->tx_status;
538 struct ieee80211_low_level_stats *stats = &rt2x00dev->low_level_stats;
539 int success = !!(status == TX_SUCCESS || status == TX_SUCCESS_RETRY);
540 int fail = !!(status == TX_FAIL_RETRY || status == TX_FAIL_INVALID ||
541 status == TX_FAIL_OTHER);
542
543 /*
544 * Update TX statistics.
545 */
546 tx_status->flags = 0;
547 tx_status->ack_signal = 0;
548 tx_status->excessive_retries = (status == TX_FAIL_RETRY);
549 tx_status->retry_count = retry;
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200550 rt2x00dev->link.qual.tx_success += success;
551 rt2x00dev->link.qual.tx_failed += retry + fail;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700552
553 if (!(tx_status->control.flags & IEEE80211_TXCTL_NO_ACK)) {
554 if (success)
555 tx_status->flags |= IEEE80211_TX_STATUS_ACK;
556 else
557 stats->dot11ACKFailureCount++;
558 }
559
560 tx_status->queue_length = entry->ring->stats.limit;
561 tx_status->queue_number = tx_status->control.queue;
562
563 if (tx_status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) {
564 if (success)
565 stats->dot11RTSSuccessCount++;
566 else
567 stats->dot11RTSFailureCount++;
568 }
569
570 /*
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100571 * Send the tx_status to mac80211 & debugfs.
572 * mac80211 will clean up the skb structure.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700573 */
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100574 get_skb_desc(entry->skb)->frame_type = DUMP_FRAME_TXDONE;
575 rt2x00debug_dump_frame(rt2x00dev, entry->skb);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700576 ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb, tx_status);
577 entry->skb = NULL;
578}
579EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
580
581void rt2x00lib_rxdone(struct data_entry *entry, struct sk_buff *skb,
Johannes Berg4150c572007-09-17 01:29:23 -0400582 struct rxdata_entry_desc *desc)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700583{
584 struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
585 struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
586 struct ieee80211_hw_mode *mode;
587 struct ieee80211_rate *rate;
Mattias Nissler61af43c2007-11-27 21:50:26 +0100588 struct ieee80211_hdr *hdr;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700589 unsigned int i;
590 int val = 0;
Mattias Nissler61af43c2007-11-27 21:50:26 +0100591 u16 fc;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700592
593 /*
594 * Update RX statistics.
595 */
596 mode = &rt2x00dev->hwmodes[rt2x00dev->curr_hwmode];
597 for (i = 0; i < mode->num_rates; i++) {
598 rate = &mode->rates[i];
599
600 /*
601 * When frame was received with an OFDM bitrate,
602 * the signal is the PLCP value. If it was received with
603 * a CCK bitrate the signal is the rate in 0.5kbit/s.
604 */
Johannes Berg4150c572007-09-17 01:29:23 -0400605 if (!desc->ofdm)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700606 val = DEVICE_GET_RATE_FIELD(rate->val, RATE);
607 else
608 val = DEVICE_GET_RATE_FIELD(rate->val, PLCP);
609
Johannes Berg4150c572007-09-17 01:29:23 -0400610 if (val == desc->signal) {
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700611 val = rate->val;
612 break;
613 }
614 }
615
Mattias Nissler61af43c2007-11-27 21:50:26 +0100616 /*
Ivo van Doorn7e56d382008-01-06 23:41:28 +0100617 * Only update link status if this is a beacon frame carrying our bssid.
Mattias Nissler61af43c2007-11-27 21:50:26 +0100618 */
Ivo van Doorn7e56d382008-01-06 23:41:28 +0100619 hdr = (struct ieee80211_hdr*)skb->data;
620 fc = le16_to_cpu(hdr->frame_control);
621 if (is_beacon(fc) && desc->my_bss)
622 rt2x00lib_update_link_stats(&rt2x00dev->link, desc->rssi);
Mattias Nissler61af43c2007-11-27 21:50:26 +0100623
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200624 rt2x00dev->link.qual.rx_success++;
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200625
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700626 rx_status->rate = val;
Johannes Berg4150c572007-09-17 01:29:23 -0400627 rx_status->signal =
628 rt2x00lib_calculate_link_signal(rt2x00dev, desc->rssi);
629 rx_status->ssi = desc->rssi;
630 rx_status->flag = desc->flags;
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200631 rx_status->antenna = rt2x00dev->link.ant.active.rx;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700632
633 /*
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100634 * Send frame to mac80211 & debugfs
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700635 */
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100636 get_skb_desc(skb)->frame_type = DUMP_FRAME_RXDONE;
637 rt2x00debug_dump_frame(rt2x00dev, skb);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700638 ieee80211_rx_irqsafe(rt2x00dev->hw, skb, rx_status);
639}
640EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
641
642/*
643 * TX descriptor initializer
644 */
645void rt2x00lib_write_tx_desc(struct rt2x00_dev *rt2x00dev,
Ivo van Doorn08992f72008-01-24 01:56:25 -0800646 struct sk_buff *skb,
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700647 struct ieee80211_tx_control *control)
648{
Johannes Berg4150c572007-09-17 01:29:23 -0400649 struct txdata_entry_desc desc;
Ivo van Doorn08992f72008-01-24 01:56:25 -0800650 struct skb_desc *skbdesc = get_skb_desc(skb);
651 struct ieee80211_hdr *ieee80211hdr = skbdesc->data;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700652 int tx_rate;
653 int bitrate;
Ivo van Doorn08992f72008-01-24 01:56:25 -0800654 int length;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700655 int duration;
656 int residual;
657 u16 frame_control;
658 u16 seq_ctrl;
659
Ivo van Doorn08992f72008-01-24 01:56:25 -0800660 memset(&desc, 0, sizeof(desc));
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700661
Ivo van Doorn08992f72008-01-24 01:56:25 -0800662 desc.cw_min = skbdesc->ring->tx_params.cw_min;
663 desc.cw_max = skbdesc->ring->tx_params.cw_max;
664 desc.aifs = skbdesc->ring->tx_params.aifs;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700665
666 /*
667 * Identify queue
668 */
669 if (control->queue < rt2x00dev->hw->queues)
670 desc.queue = control->queue;
671 else if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
672 control->queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
673 desc.queue = QUEUE_MGMT;
674 else
675 desc.queue = QUEUE_OTHER;
676
677 /*
678 * Read required fields from ieee80211 header.
679 */
680 frame_control = le16_to_cpu(ieee80211hdr->frame_control);
681 seq_ctrl = le16_to_cpu(ieee80211hdr->seq_ctrl);
682
683 tx_rate = control->tx_rate;
684
685 /*
Mattias Nissler2700f8b2007-10-27 13:43:49 +0200686 * Check whether this frame is to be acked
687 */
688 if (!(control->flags & IEEE80211_TXCTL_NO_ACK))
689 __set_bit(ENTRY_TXD_ACK, &desc.flags);
690
691 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700692 * Check if this is a RTS/CTS frame
693 */
694 if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
695 __set_bit(ENTRY_TXD_BURST, &desc.flags);
Mattias Nissler2700f8b2007-10-27 13:43:49 +0200696 if (is_rts_frame(frame_control)) {
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700697 __set_bit(ENTRY_TXD_RTS_FRAME, &desc.flags);
Mattias Nissler2700f8b2007-10-27 13:43:49 +0200698 __set_bit(ENTRY_TXD_ACK, &desc.flags);
699 } else
700 __clear_bit(ENTRY_TXD_ACK, &desc.flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700701 if (control->rts_cts_rate)
702 tx_rate = control->rts_cts_rate;
703 }
704
705 /*
706 * Check for OFDM
707 */
708 if (DEVICE_GET_RATE_FIELD(tx_rate, RATEMASK) & DEV_OFDM_RATEMASK)
709 __set_bit(ENTRY_TXD_OFDM_RATE, &desc.flags);
710
711 /*
712 * Check if more fragments are pending
713 */
714 if (ieee80211_get_morefrag(ieee80211hdr)) {
715 __set_bit(ENTRY_TXD_BURST, &desc.flags);
716 __set_bit(ENTRY_TXD_MORE_FRAG, &desc.flags);
717 }
718
719 /*
720 * Beacons and probe responses require the tsf timestamp
721 * to be inserted into the frame.
722 */
723 if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
724 is_probe_resp(frame_control))
725 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &desc.flags);
726
727 /*
728 * Determine with what IFS priority this frame should be send.
729 * Set ifs to IFS_SIFS when the this is not the first fragment,
730 * or this fragment came after RTS/CTS.
731 */
732 if ((seq_ctrl & IEEE80211_SCTL_FRAG) > 0 ||
733 test_bit(ENTRY_TXD_RTS_FRAME, &desc.flags))
734 desc.ifs = IFS_SIFS;
735 else
736 desc.ifs = IFS_BACKOFF;
737
738 /*
739 * PLCP setup
740 * Length calculation depends on OFDM/CCK rate.
741 */
742 desc.signal = DEVICE_GET_RATE_FIELD(tx_rate, PLCP);
743 desc.service = 0x04;
744
Ivo van Doorn08992f72008-01-24 01:56:25 -0800745 length = skbdesc->data_len + FCS_LEN;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700746 if (test_bit(ENTRY_TXD_OFDM_RATE, &desc.flags)) {
Ivo van Doorn08992f72008-01-24 01:56:25 -0800747 desc.length_high = (length >> 6) & 0x3f;
748 desc.length_low = length & 0x3f;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700749 } else {
750 bitrate = DEVICE_GET_RATE_FIELD(tx_rate, RATE);
751
752 /*
753 * Convert length to microseconds.
754 */
Ivo van Doorn08992f72008-01-24 01:56:25 -0800755 residual = get_duration_res(length, bitrate);
756 duration = get_duration(length, bitrate);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700757
758 if (residual != 0) {
759 duration++;
760
761 /*
762 * Check if we need to set the Length Extension
763 */
Mattias Nisslerdb151782007-10-13 16:26:52 +0200764 if (bitrate == 110 && residual <= 30)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700765 desc.service |= 0x80;
766 }
767
768 desc.length_high = (duration >> 8) & 0xff;
769 desc.length_low = duration & 0xff;
770
771 /*
772 * When preamble is enabled we should set the
773 * preamble bit for the signal.
774 */
775 if (DEVICE_GET_RATE_FIELD(tx_rate, PREAMBLE))
776 desc.signal |= 0x08;
777 }
778
Ivo van Doorndd3193e2008-01-06 23:41:10 +0100779 rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, skb, &desc, control);
Ivo van Doorn08992f72008-01-24 01:56:25 -0800780
781 /*
782 * Update ring entry.
783 */
784 skbdesc->entry->skb = skb;
785 memcpy(&skbdesc->entry->tx_status.control, control, sizeof(*control));
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100786
787 /*
788 * The frame has been completely initialized and ready
789 * for sending to the device. The caller will push the
790 * frame to the device, but we are going to push the
791 * frame to debugfs here.
792 */
793 skbdesc->frame_type = DUMP_FRAME_TX;
794 rt2x00debug_dump_frame(rt2x00dev, skb);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700795}
796EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc);
797
798/*
799 * Driver initialization handlers.
800 */
801static void rt2x00lib_channel(struct ieee80211_channel *entry,
802 const int channel, const int tx_power,
803 const int value)
804{
805 entry->chan = channel;
806 if (channel <= 14)
807 entry->freq = 2407 + (5 * channel);
808 else
809 entry->freq = 5000 + (5 * channel);
810 entry->val = value;
811 entry->flag =
812 IEEE80211_CHAN_W_IBSS |
813 IEEE80211_CHAN_W_ACTIVE_SCAN |
814 IEEE80211_CHAN_W_SCAN;
815 entry->power_level = tx_power;
816 entry->antenna_max = 0xff;
817}
818
819static void rt2x00lib_rate(struct ieee80211_rate *entry,
820 const int rate, const int mask,
821 const int plcp, const int flags)
822{
823 entry->rate = rate;
824 entry->val =
825 DEVICE_SET_RATE_FIELD(rate, RATE) |
826 DEVICE_SET_RATE_FIELD(mask, RATEMASK) |
827 DEVICE_SET_RATE_FIELD(plcp, PLCP);
828 entry->flags = flags;
829 entry->val2 = entry->val;
830 if (entry->flags & IEEE80211_RATE_PREAMBLE2)
831 entry->val2 |= DEVICE_SET_RATE_FIELD(1, PREAMBLE);
832 entry->min_rssi_ack = 0;
833 entry->min_rssi_ack_delta = 0;
834}
835
836static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
837 struct hw_mode_spec *spec)
838{
839 struct ieee80211_hw *hw = rt2x00dev->hw;
840 struct ieee80211_hw_mode *hwmodes;
841 struct ieee80211_channel *channels;
842 struct ieee80211_rate *rates;
843 unsigned int i;
844 unsigned char tx_power;
845
846 hwmodes = kzalloc(sizeof(*hwmodes) * spec->num_modes, GFP_KERNEL);
847 if (!hwmodes)
848 goto exit;
849
850 channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
851 if (!channels)
852 goto exit_free_modes;
853
854 rates = kzalloc(sizeof(*rates) * spec->num_rates, GFP_KERNEL);
855 if (!rates)
856 goto exit_free_channels;
857
858 /*
859 * Initialize Rate list.
860 */
861 rt2x00lib_rate(&rates[0], 10, DEV_RATEMASK_1MB,
862 0x00, IEEE80211_RATE_CCK);
863 rt2x00lib_rate(&rates[1], 20, DEV_RATEMASK_2MB,
864 0x01, IEEE80211_RATE_CCK_2);
865 rt2x00lib_rate(&rates[2], 55, DEV_RATEMASK_5_5MB,
866 0x02, IEEE80211_RATE_CCK_2);
867 rt2x00lib_rate(&rates[3], 110, DEV_RATEMASK_11MB,
868 0x03, IEEE80211_RATE_CCK_2);
869
870 if (spec->num_rates > 4) {
871 rt2x00lib_rate(&rates[4], 60, DEV_RATEMASK_6MB,
872 0x0b, IEEE80211_RATE_OFDM);
873 rt2x00lib_rate(&rates[5], 90, DEV_RATEMASK_9MB,
874 0x0f, IEEE80211_RATE_OFDM);
875 rt2x00lib_rate(&rates[6], 120, DEV_RATEMASK_12MB,
876 0x0a, IEEE80211_RATE_OFDM);
877 rt2x00lib_rate(&rates[7], 180, DEV_RATEMASK_18MB,
878 0x0e, IEEE80211_RATE_OFDM);
879 rt2x00lib_rate(&rates[8], 240, DEV_RATEMASK_24MB,
880 0x09, IEEE80211_RATE_OFDM);
881 rt2x00lib_rate(&rates[9], 360, DEV_RATEMASK_36MB,
882 0x0d, IEEE80211_RATE_OFDM);
883 rt2x00lib_rate(&rates[10], 480, DEV_RATEMASK_48MB,
884 0x08, IEEE80211_RATE_OFDM);
885 rt2x00lib_rate(&rates[11], 540, DEV_RATEMASK_54MB,
886 0x0c, IEEE80211_RATE_OFDM);
887 }
888
889 /*
890 * Initialize Channel list.
891 */
892 for (i = 0; i < spec->num_channels; i++) {
893 if (spec->channels[i].channel <= 14)
894 tx_power = spec->tx_power_bg[i];
895 else if (spec->tx_power_a)
896 tx_power = spec->tx_power_a[i];
897 else
898 tx_power = spec->tx_power_default;
899
900 rt2x00lib_channel(&channels[i],
901 spec->channels[i].channel, tx_power, i);
902 }
903
904 /*
905 * Intitialize 802.11b
906 * Rates: CCK.
907 * Channels: OFDM.
908 */
909 if (spec->num_modes > HWMODE_B) {
910 hwmodes[HWMODE_B].mode = MODE_IEEE80211B;
911 hwmodes[HWMODE_B].num_channels = 14;
912 hwmodes[HWMODE_B].num_rates = 4;
913 hwmodes[HWMODE_B].channels = channels;
914 hwmodes[HWMODE_B].rates = rates;
915 }
916
917 /*
918 * Intitialize 802.11g
919 * Rates: CCK, OFDM.
920 * Channels: OFDM.
921 */
922 if (spec->num_modes > HWMODE_G) {
923 hwmodes[HWMODE_G].mode = MODE_IEEE80211G;
924 hwmodes[HWMODE_G].num_channels = 14;
925 hwmodes[HWMODE_G].num_rates = spec->num_rates;
926 hwmodes[HWMODE_G].channels = channels;
927 hwmodes[HWMODE_G].rates = rates;
928 }
929
930 /*
931 * Intitialize 802.11a
932 * Rates: OFDM.
933 * Channels: OFDM, UNII, HiperLAN2.
934 */
935 if (spec->num_modes > HWMODE_A) {
936 hwmodes[HWMODE_A].mode = MODE_IEEE80211A;
937 hwmodes[HWMODE_A].num_channels = spec->num_channels - 14;
938 hwmodes[HWMODE_A].num_rates = spec->num_rates - 4;
939 hwmodes[HWMODE_A].channels = &channels[14];
940 hwmodes[HWMODE_A].rates = &rates[4];
941 }
942
943 if (spec->num_modes > HWMODE_G &&
944 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_G]))
945 goto exit_free_rates;
946
947 if (spec->num_modes > HWMODE_B &&
948 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_B]))
949 goto exit_free_rates;
950
951 if (spec->num_modes > HWMODE_A &&
952 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_A]))
953 goto exit_free_rates;
954
955 rt2x00dev->hwmodes = hwmodes;
956
957 return 0;
958
959exit_free_rates:
960 kfree(rates);
961
962exit_free_channels:
963 kfree(channels);
964
965exit_free_modes:
966 kfree(hwmodes);
967
968exit:
969 ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
970 return -ENOMEM;
971}
972
973static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
974{
Ivo van Doorn066cb632007-09-25 20:55:39 +0200975 if (test_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700976 ieee80211_unregister_hw(rt2x00dev->hw);
977
978 if (likely(rt2x00dev->hwmodes)) {
979 kfree(rt2x00dev->hwmodes->channels);
980 kfree(rt2x00dev->hwmodes->rates);
981 kfree(rt2x00dev->hwmodes);
982 rt2x00dev->hwmodes = NULL;
983 }
984}
985
986static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
987{
988 struct hw_mode_spec *spec = &rt2x00dev->spec;
989 int status;
990
991 /*
992 * Initialize HW modes.
993 */
994 status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
995 if (status)
996 return status;
997
998 /*
999 * Register HW.
1000 */
1001 status = ieee80211_register_hw(rt2x00dev->hw);
1002 if (status) {
1003 rt2x00lib_remove_hw(rt2x00dev);
1004 return status;
1005 }
1006
Ivo van Doorn066cb632007-09-25 20:55:39 +02001007 __set_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001008
1009 return 0;
1010}
1011
1012/*
1013 * Initialization/uninitialization handlers.
1014 */
1015static int rt2x00lib_alloc_entries(struct data_ring *ring,
1016 const u16 max_entries, const u16 data_size,
1017 const u16 desc_size)
1018{
1019 struct data_entry *entry;
1020 unsigned int i;
1021
1022 ring->stats.limit = max_entries;
1023 ring->data_size = data_size;
1024 ring->desc_size = desc_size;
1025
1026 /*
1027 * Allocate all ring entries.
1028 */
1029 entry = kzalloc(ring->stats.limit * sizeof(*entry), GFP_KERNEL);
1030 if (!entry)
1031 return -ENOMEM;
1032
1033 for (i = 0; i < ring->stats.limit; i++) {
1034 entry[i].flags = 0;
1035 entry[i].ring = ring;
1036 entry[i].skb = NULL;
Ivo van Doorn04267102008-01-06 23:39:25 +01001037 entry[i].entry_idx = i;
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001038 }
1039
1040 ring->entry = entry;
1041
1042 return 0;
1043}
1044
1045static int rt2x00lib_alloc_ring_entries(struct rt2x00_dev *rt2x00dev)
1046{
1047 struct data_ring *ring;
1048
1049 /*
1050 * Allocate the RX ring.
1051 */
1052 if (rt2x00lib_alloc_entries(rt2x00dev->rx, RX_ENTRIES, DATA_FRAME_SIZE,
1053 rt2x00dev->ops->rxd_size))
1054 return -ENOMEM;
1055
1056 /*
1057 * First allocate the TX rings.
1058 */
1059 txring_for_each(rt2x00dev, ring) {
1060 if (rt2x00lib_alloc_entries(ring, TX_ENTRIES, DATA_FRAME_SIZE,
1061 rt2x00dev->ops->txd_size))
1062 return -ENOMEM;
1063 }
1064
Ivo van Doorn066cb632007-09-25 20:55:39 +02001065 if (!test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001066 return 0;
1067
1068 /*
1069 * Allocate the BEACON ring.
1070 */
1071 if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[0], BEACON_ENTRIES,
1072 MGMT_FRAME_SIZE, rt2x00dev->ops->txd_size))
1073 return -ENOMEM;
1074
1075 /*
1076 * Allocate the Atim ring.
1077 */
1078 if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[1], ATIM_ENTRIES,
1079 DATA_FRAME_SIZE, rt2x00dev->ops->txd_size))
1080 return -ENOMEM;
1081
1082 return 0;
1083}
1084
1085static void rt2x00lib_free_ring_entries(struct rt2x00_dev *rt2x00dev)
1086{
1087 struct data_ring *ring;
1088
1089 ring_for_each(rt2x00dev, ring) {
1090 kfree(ring->entry);
1091 ring->entry = NULL;
1092 }
1093}
1094
Ivo van Doorne37ea212008-01-06 23:40:07 +01001095static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001096{
1097 if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
1098 return;
1099
1100 /*
1101 * Unregister rfkill.
1102 */
1103 rt2x00rfkill_unregister(rt2x00dev);
1104
1105 /*
1106 * Allow the HW to uninitialize.
1107 */
1108 rt2x00dev->ops->lib->uninitialize(rt2x00dev);
1109
1110 /*
1111 * Free allocated ring entries.
1112 */
1113 rt2x00lib_free_ring_entries(rt2x00dev);
1114}
1115
Ivo van Doorne37ea212008-01-06 23:40:07 +01001116static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001117{
1118 int status;
1119
1120 if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
1121 return 0;
1122
1123 /*
1124 * Allocate all ring entries.
1125 */
1126 status = rt2x00lib_alloc_ring_entries(rt2x00dev);
1127 if (status) {
1128 ERROR(rt2x00dev, "Ring entries allocation failed.\n");
1129 return status;
1130 }
1131
1132 /*
1133 * Initialize the device.
1134 */
1135 status = rt2x00dev->ops->lib->initialize(rt2x00dev);
1136 if (status)
1137 goto exit;
1138
1139 __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
1140
1141 /*
1142 * Register the rfkill handler.
1143 */
1144 status = rt2x00rfkill_register(rt2x00dev);
1145 if (status)
1146 goto exit_unitialize;
1147
1148 return 0;
1149
1150exit_unitialize:
1151 rt2x00lib_uninitialize(rt2x00dev);
1152
1153exit:
1154 rt2x00lib_free_ring_entries(rt2x00dev);
1155
1156 return status;
1157}
1158
Ivo van Doorne37ea212008-01-06 23:40:07 +01001159int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
1160{
1161 int retval;
1162
1163 if (test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1164 return 0;
1165
1166 /*
1167 * If this is the first interface which is added,
1168 * we should load the firmware now.
1169 */
1170 if (test_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags)) {
1171 retval = rt2x00lib_load_firmware(rt2x00dev);
1172 if (retval)
1173 return retval;
1174 }
1175
1176 /*
1177 * Initialize the device.
1178 */
1179 retval = rt2x00lib_initialize(rt2x00dev);
1180 if (retval)
1181 return retval;
1182
1183 /*
1184 * Enable radio.
1185 */
1186 retval = rt2x00lib_enable_radio(rt2x00dev);
1187 if (retval) {
1188 rt2x00lib_uninitialize(rt2x00dev);
1189 return retval;
1190 }
1191
1192 __set_bit(DEVICE_STARTED, &rt2x00dev->flags);
1193
1194 return 0;
1195}
1196
1197void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
1198{
1199 if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1200 return;
1201
1202 /*
1203 * Perhaps we can add something smarter here,
1204 * but for now just disabling the radio should do.
1205 */
1206 rt2x00lib_disable_radio(rt2x00dev);
1207
1208 __clear_bit(DEVICE_STARTED, &rt2x00dev->flags);
1209}
1210
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001211/*
1212 * driver allocation handlers.
1213 */
1214static int rt2x00lib_alloc_rings(struct rt2x00_dev *rt2x00dev)
1215{
1216 struct data_ring *ring;
Ivo van Doorn04267102008-01-06 23:39:25 +01001217 unsigned int index;
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001218
1219 /*
1220 * We need the following rings:
1221 * RX: 1
1222 * TX: hw->queues
1223 * Beacon: 1 (if required)
1224 * Atim: 1 (if required)
1225 */
1226 rt2x00dev->data_rings = 1 + rt2x00dev->hw->queues +
Ivo van Doorn066cb632007-09-25 20:55:39 +02001227 (2 * test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags));
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001228
1229 ring = kzalloc(rt2x00dev->data_rings * sizeof(*ring), GFP_KERNEL);
1230 if (!ring) {
1231 ERROR(rt2x00dev, "Ring allocation failed.\n");
1232 return -ENOMEM;
1233 }
1234
1235 /*
1236 * Initialize pointers
1237 */
1238 rt2x00dev->rx = ring;
1239 rt2x00dev->tx = &rt2x00dev->rx[1];
Ivo van Doorn066cb632007-09-25 20:55:39 +02001240 if (test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001241 rt2x00dev->bcn = &rt2x00dev->tx[rt2x00dev->hw->queues];
1242
1243 /*
1244 * Initialize ring parameters.
Ivo van Doorn89539eb2008-01-10 22:40:13 +01001245 * RX: queue_idx = 0
1246 * TX: queue_idx = IEEE80211_TX_QUEUE_DATA0 + index
1247 * TX: cw_min: 2^5 = 32.
1248 * TX: cw_max: 2^10 = 1024.
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001249 */
Ivo van Doorn89539eb2008-01-10 22:40:13 +01001250 rt2x00dev->rx->rt2x00dev = rt2x00dev;
1251 rt2x00dev->rx->queue_idx = 0;
1252
1253 index = IEEE80211_TX_QUEUE_DATA0;
1254 txring_for_each(rt2x00dev, ring) {
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001255 ring->rt2x00dev = rt2x00dev;
Ivo van Doorn04267102008-01-06 23:39:25 +01001256 ring->queue_idx = index++;
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001257 ring->tx_params.aifs = 2;
1258 ring->tx_params.cw_min = 5;
1259 ring->tx_params.cw_max = 10;
1260 }
1261
1262 return 0;
1263}
1264
1265static void rt2x00lib_free_rings(struct rt2x00_dev *rt2x00dev)
1266{
1267 kfree(rt2x00dev->rx);
1268 rt2x00dev->rx = NULL;
1269 rt2x00dev->tx = NULL;
1270 rt2x00dev->bcn = NULL;
1271}
1272
1273int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
1274{
1275 int retval = -ENOMEM;
1276
1277 /*
1278 * Let the driver probe the device to detect the capabilities.
1279 */
1280 retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
1281 if (retval) {
1282 ERROR(rt2x00dev, "Failed to allocate device.\n");
1283 goto exit;
1284 }
1285
1286 /*
1287 * Initialize configuration work.
1288 */
1289 INIT_WORK(&rt2x00dev->beacon_work, rt2x00lib_beacondone_scheduled);
Johannes Berg4150c572007-09-17 01:29:23 -04001290 INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
Ivo van Doorn5c58ee52007-10-06 13:34:52 +02001291 INIT_WORK(&rt2x00dev->config_work, rt2x00lib_configuration_scheduled);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001292 INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
1293
1294 /*
1295 * Reset current working type.
1296 */
Ivo van Doornd28c2562007-11-27 21:49:50 +01001297 rt2x00dev->interface.type = IEEE80211_IF_TYPE_INVALID;
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001298
1299 /*
1300 * Allocate ring array.
1301 */
1302 retval = rt2x00lib_alloc_rings(rt2x00dev);
1303 if (retval)
1304 goto exit;
1305
1306 /*
1307 * Initialize ieee80211 structure.
1308 */
1309 retval = rt2x00lib_probe_hw(rt2x00dev);
1310 if (retval) {
1311 ERROR(rt2x00dev, "Failed to initialize hw.\n");
1312 goto exit;
1313 }
1314
1315 /*
1316 * Allocatie rfkill.
1317 */
1318 retval = rt2x00rfkill_allocate(rt2x00dev);
1319 if (retval)
1320 goto exit;
1321
1322 /*
1323 * Open the debugfs entry.
1324 */
1325 rt2x00debug_register(rt2x00dev);
1326
Ivo van Doorn066cb632007-09-25 20:55:39 +02001327 __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1328
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001329 return 0;
1330
1331exit:
1332 rt2x00lib_remove_dev(rt2x00dev);
1333
1334 return retval;
1335}
1336EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
1337
1338void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
1339{
Ivo van Doorn066cb632007-09-25 20:55:39 +02001340 __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1341
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001342 /*
1343 * Disable radio.
1344 */
1345 rt2x00lib_disable_radio(rt2x00dev);
1346
1347 /*
1348 * Uninitialize device.
1349 */
1350 rt2x00lib_uninitialize(rt2x00dev);
1351
1352 /*
1353 * Close debugfs entry.
1354 */
1355 rt2x00debug_deregister(rt2x00dev);
1356
1357 /*
1358 * Free rfkill
1359 */
1360 rt2x00rfkill_free(rt2x00dev);
1361
1362 /*
1363 * Free ieee80211_hw memory.
1364 */
1365 rt2x00lib_remove_hw(rt2x00dev);
1366
1367 /*
1368 * Free firmware image.
1369 */
1370 rt2x00lib_free_firmware(rt2x00dev);
1371
1372 /*
1373 * Free ring structures.
1374 */
1375 rt2x00lib_free_rings(rt2x00dev);
1376}
1377EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
1378
1379/*
1380 * Device state handlers
1381 */
1382#ifdef CONFIG_PM
1383int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
1384{
1385 int retval;
1386
1387 NOTICE(rt2x00dev, "Going to sleep.\n");
Ivo van Doorn066cb632007-09-25 20:55:39 +02001388 __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1389
1390 /*
1391 * Only continue if mac80211 has open interfaces.
1392 */
1393 if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1394 goto exit;
Ivo van Doorn6d7f9872007-10-06 14:12:42 +02001395 __set_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001396
1397 /*
1398 * Disable radio and unitialize all items
1399 * that must be recreated on resume.
1400 */
Ivo van Doorne37ea212008-01-06 23:40:07 +01001401 rt2x00lib_stop(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001402 rt2x00lib_uninitialize(rt2x00dev);
1403 rt2x00debug_deregister(rt2x00dev);
1404
Ivo van Doorn066cb632007-09-25 20:55:39 +02001405exit:
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001406 /*
1407 * Set device mode to sleep for power management.
1408 */
1409 retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
1410 if (retval)
1411 return retval;
1412
1413 return 0;
1414}
1415EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1416
1417int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1418{
1419 struct interface *intf = &rt2x00dev->interface;
1420 int retval;
1421
1422 NOTICE(rt2x00dev, "Waking up.\n");
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001423
1424 /*
1425 * Open the debugfs entry.
1426 */
1427 rt2x00debug_register(rt2x00dev);
1428
1429 /*
Ivo van Doorn6d7f9872007-10-06 14:12:42 +02001430 * Only continue if mac80211 had open interfaces.
Ivo van Doorn066cb632007-09-25 20:55:39 +02001431 */
Ivo van Doorn6d7f9872007-10-06 14:12:42 +02001432 if (!__test_and_clear_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags))
Ivo van Doorn066cb632007-09-25 20:55:39 +02001433 return 0;
1434
1435 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001436 * Reinitialize device and all active interfaces.
1437 */
Ivo van Doorne37ea212008-01-06 23:40:07 +01001438 retval = rt2x00lib_start(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001439 if (retval)
1440 goto exit;
1441
1442 /*
1443 * Reconfigure device.
1444 */
Ivo van Doorn066cb632007-09-25 20:55:39 +02001445 rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1);
1446 if (!rt2x00dev->hw->conf.radio_enabled)
1447 rt2x00lib_disable_radio(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001448
1449 rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
1450 rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
1451 rt2x00lib_config_type(rt2x00dev, intf->type);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001452
1453 /*
Ivo van Doorne37ea212008-01-06 23:40:07 +01001454 * We are ready again to receive requests from mac80211.
1455 */
1456 __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1457
1458 /*
Ivo van Doorn066cb632007-09-25 20:55:39 +02001459 * It is possible that during that mac80211 has attempted
1460 * to send frames while we were suspending or resuming.
1461 * In that case we have disabled the TX queue and should
1462 * now enable it again
1463 */
1464 ieee80211_start_queues(rt2x00dev->hw);
1465
1466 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001467 * When in Master or Ad-hoc mode,
1468 * restart Beacon transmitting by faking a beacondone event.
1469 */
1470 if (intf->type == IEEE80211_IF_TYPE_AP ||
1471 intf->type == IEEE80211_IF_TYPE_IBSS)
1472 rt2x00lib_beacondone(rt2x00dev);
1473
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001474 return 0;
1475
1476exit:
1477 rt2x00lib_disable_radio(rt2x00dev);
1478 rt2x00lib_uninitialize(rt2x00dev);
1479 rt2x00debug_deregister(rt2x00dev);
1480
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001481 return retval;
1482}
1483EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1484#endif /* CONFIG_PM */
1485
1486/*
1487 * rt2x00lib module information.
1488 */
1489MODULE_AUTHOR(DRV_PROJECT);
1490MODULE_VERSION(DRV_VERSION);
1491MODULE_DESCRIPTION("rt2x00 library");
1492MODULE_LICENSE("GPL");