blob: a771a092434b776fc281280c7c4fa4ec75ff7afe [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 */
64static void rt2x00lib_start_link_tuner(struct rt2x00_dev *rt2x00dev)
65{
Ivo van Doorn8de8c512007-10-13 16:26:32 +020066 rt2x00dev->link.count = 0;
67 rt2x00dev->link.vgc_level = 0;
68
69 memset(&rt2x00dev->link.qual, 0, sizeof(rt2x00dev->link.qual));
70
71 /*
72 * The RX and TX percentage should start at 50%
73 * this will assure we will get at least get some
74 * decent value when the link tuner starts.
75 * The value will be dropped and overwritten with
76 * the correct (measured )value anyway during the
77 * first run of the link tuner.
78 */
79 rt2x00dev->link.qual.rx_percentage = 50;
80 rt2x00dev->link.qual.tx_percentage = 50;
Ivo van Doorn95ea3622007-09-25 17:57:13 -070081
82 /*
83 * Reset the link tuner.
84 */
85 rt2x00dev->ops->lib->reset_tuner(rt2x00dev);
86
87 queue_delayed_work(rt2x00dev->hw->workqueue,
88 &rt2x00dev->link.work, LINK_TUNE_INTERVAL);
89}
90
91static void rt2x00lib_stop_link_tuner(struct rt2x00_dev *rt2x00dev)
92{
Ivo van Doorn3e309682007-09-25 20:56:36 +020093 cancel_delayed_work_sync(&rt2x00dev->link.work);
Ivo van Doorn95ea3622007-09-25 17:57:13 -070094}
95
96void rt2x00lib_reset_link_tuner(struct rt2x00_dev *rt2x00dev)
97{
Ivo van Doornfdd0abc2007-09-25 20:57:49 +020098 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
99 return;
100
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700101 rt2x00lib_stop_link_tuner(rt2x00dev);
102 rt2x00lib_start_link_tuner(rt2x00dev);
103}
104
105/*
106 * Radio control handlers.
107 */
108int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
109{
110 int status;
111
112 /*
113 * Don't enable the radio twice.
114 * And check if the hardware button has been disabled.
115 */
116 if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
Ivo van Doorn81873e92007-10-06 14:14:06 +0200117 test_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700118 return 0;
119
120 /*
121 * Enable radio.
122 */
123 status = rt2x00dev->ops->lib->set_device_state(rt2x00dev,
124 STATE_RADIO_ON);
125 if (status)
126 return status;
127
128 __set_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags);
129
130 /*
131 * Enable RX.
132 */
Ivo van Doorn5cbf8302007-10-06 14:16:09 +0200133 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700134
135 /*
136 * Start the TX queues.
137 */
138 ieee80211_start_queues(rt2x00dev->hw);
139
140 return 0;
141}
142
143void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
144{
145 if (!__test_and_clear_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
146 return;
147
148 /*
Johannes Berg4150c572007-09-17 01:29:23 -0400149 * Stop all scheduled work.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700150 */
151 if (work_pending(&rt2x00dev->beacon_work))
152 cancel_work_sync(&rt2x00dev->beacon_work);
Johannes Berg4150c572007-09-17 01:29:23 -0400153 if (work_pending(&rt2x00dev->filter_work))
154 cancel_work_sync(&rt2x00dev->filter_work);
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200155 if (work_pending(&rt2x00dev->config_work))
156 cancel_work_sync(&rt2x00dev->config_work);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700157
158 /*
159 * Stop the TX queues.
160 */
161 ieee80211_stop_queues(rt2x00dev->hw);
162
163 /*
164 * Disable RX.
165 */
Ivo van Doorn5cbf8302007-10-06 14:16:09 +0200166 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700167
168 /*
169 * Disable radio.
170 */
171 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
172}
173
Ivo van Doorn5cbf8302007-10-06 14:16:09 +0200174void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, enum dev_state state)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700175{
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700176 /*
177 * When we are disabling the RX, we should also stop the link tuner.
178 */
Ivo van Doorn5cbf8302007-10-06 14:16:09 +0200179 if (state == STATE_RADIO_RX_OFF)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700180 rt2x00lib_stop_link_tuner(rt2x00dev);
181
182 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
183
184 /*
185 * When we are enabling the RX, we should also start the link tuner.
186 */
Ivo van Doorn5cbf8302007-10-06 14:16:09 +0200187 if (state == STATE_RADIO_RX_ON &&
188 is_interface_present(&rt2x00dev->interface))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700189 rt2x00lib_start_link_tuner(rt2x00dev);
190}
191
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200192static void rt2x00lib_evaluate_antenna_sample(struct rt2x00_dev *rt2x00dev)
193{
194 enum antenna rx = rt2x00dev->link.ant.active.rx;
195 enum antenna tx = rt2x00dev->link.ant.active.tx;
196 int sample_a =
197 rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_A);
198 int sample_b =
199 rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_B);
200
201 /*
202 * We are done sampling. Now we should evaluate the results.
203 */
204 rt2x00dev->link.ant.flags &= ~ANTENNA_MODE_SAMPLE;
205
206 /*
207 * During the last period we have sampled the RSSI
208 * from both antenna's. It now is time to determine
209 * which antenna demonstrated the best performance.
210 * When we are already on the antenna with the best
211 * performance, then there really is nothing for us
212 * left to do.
213 */
214 if (sample_a == sample_b)
215 return;
216
217 if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY) {
218 if (sample_a > sample_b && rx == ANTENNA_B)
219 rx = ANTENNA_A;
220 else if (rx == ANTENNA_A)
221 rx = ANTENNA_B;
222 }
223
224 if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY) {
225 if (sample_a > sample_b && tx == ANTENNA_B)
226 tx = ANTENNA_A;
227 else if (tx == ANTENNA_A)
228 tx = ANTENNA_B;
229 }
230
231 rt2x00lib_config_antenna(rt2x00dev, rx, tx);
232}
233
234static void rt2x00lib_evaluate_antenna_eval(struct rt2x00_dev *rt2x00dev)
235{
236 enum antenna rx = rt2x00dev->link.ant.active.rx;
237 enum antenna tx = rt2x00dev->link.ant.active.tx;
238 int rssi_curr = rt2x00_get_link_ant_rssi(&rt2x00dev->link);
239 int rssi_old = rt2x00_update_ant_rssi(&rt2x00dev->link, rssi_curr);
240
241 /*
242 * Legacy driver indicates that we should swap antenna's
243 * when the difference in RSSI is greater that 5. This
244 * also should be done when the RSSI was actually better
245 * then the previous sample.
246 * When the difference exceeds the threshold we should
247 * sample the rssi from the other antenna to make a valid
248 * comparison between the 2 antennas.
249 */
250 if ((rssi_curr - rssi_old) > -5 || (rssi_curr - rssi_old) < 5)
251 return;
252
253 rt2x00dev->link.ant.flags |= ANTENNA_MODE_SAMPLE;
254
255 if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY)
256 rx = (rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
257
258 if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)
259 tx = (tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
260
261 rt2x00lib_config_antenna(rt2x00dev, rx, tx);
262}
263
264static void rt2x00lib_evaluate_antenna(struct rt2x00_dev *rt2x00dev)
265{
266 /*
267 * Determine if software diversity is enabled for
268 * either the TX or RX antenna (or both).
269 * Always perform this check since within the link
270 * tuner interval the configuration might have changed.
271 */
272 rt2x00dev->link.ant.flags &= ~ANTENNA_RX_DIVERSITY;
273 rt2x00dev->link.ant.flags &= ~ANTENNA_TX_DIVERSITY;
274
275 if (rt2x00dev->hw->conf.antenna_sel_rx == 0 &&
276 rt2x00dev->default_ant.rx != ANTENNA_SW_DIVERSITY)
277 rt2x00dev->link.ant.flags |= ANTENNA_RX_DIVERSITY;
278 if (rt2x00dev->hw->conf.antenna_sel_tx == 0 &&
279 rt2x00dev->default_ant.tx != ANTENNA_SW_DIVERSITY)
280 rt2x00dev->link.ant.flags |= ANTENNA_TX_DIVERSITY;
281
282 if (!(rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY) &&
283 !(rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)) {
284 rt2x00dev->link.ant.flags &= ~ANTENNA_MODE_SAMPLE;
285 return;
286 }
287
288 /*
289 * If we have only sampled the data over the last period
290 * we should now harvest the data. Otherwise just evaluate
291 * the data. The latter should only be performed once
292 * every 2 seconds.
293 */
294 if (rt2x00dev->link.ant.flags & ANTENNA_MODE_SAMPLE)
295 rt2x00lib_evaluate_antenna_sample(rt2x00dev);
296 else if (rt2x00dev->link.count & 1)
297 rt2x00lib_evaluate_antenna_eval(rt2x00dev);
298}
299
300static void rt2x00lib_update_link_stats(struct link *link, int rssi)
301{
302 int avg_rssi = rssi;
303
304 /*
305 * Update global RSSI
306 */
307 if (link->qual.avg_rssi)
308 avg_rssi = MOVING_AVERAGE(link->qual.avg_rssi, rssi, 8);
309 link->qual.avg_rssi = avg_rssi;
310
311 /*
312 * Update antenna RSSI
313 */
314 if (link->ant.rssi_ant)
315 rssi = MOVING_AVERAGE(link->ant.rssi_ant, rssi, 8);
316 link->ant.rssi_ant = rssi;
317}
318
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200319static void rt2x00lib_precalculate_link_signal(struct link_qual *qual)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700320{
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200321 if (qual->rx_failed || qual->rx_success)
322 qual->rx_percentage =
323 (qual->rx_success * 100) /
324 (qual->rx_failed + qual->rx_success);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700325 else
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200326 qual->rx_percentage = 50;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700327
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200328 if (qual->tx_failed || qual->tx_success)
329 qual->tx_percentage =
330 (qual->tx_success * 100) /
331 (qual->tx_failed + qual->tx_success);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700332 else
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200333 qual->tx_percentage = 50;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700334
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200335 qual->rx_success = 0;
336 qual->rx_failed = 0;
337 qual->tx_success = 0;
338 qual->tx_failed = 0;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700339}
340
341static int rt2x00lib_calculate_link_signal(struct rt2x00_dev *rt2x00dev,
342 int rssi)
343{
344 int rssi_percentage = 0;
345 int signal;
346
347 /*
348 * We need a positive value for the RSSI.
349 */
350 if (rssi < 0)
351 rssi += rt2x00dev->rssi_offset;
352
353 /*
354 * Calculate the different percentages,
355 * which will be used for the signal.
356 */
357 if (rt2x00dev->rssi_offset)
358 rssi_percentage = (rssi * 100) / rt2x00dev->rssi_offset;
359
360 /*
361 * Add the individual percentages and use the WEIGHT
362 * defines to calculate the current link signal.
363 */
364 signal = ((WEIGHT_RSSI * rssi_percentage) +
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200365 (WEIGHT_TX * rt2x00dev->link.qual.tx_percentage) +
366 (WEIGHT_RX * rt2x00dev->link.qual.rx_percentage)) / 100;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700367
368 return (signal > 100) ? 100 : signal;
369}
370
371static void rt2x00lib_link_tuner(struct work_struct *work)
372{
373 struct rt2x00_dev *rt2x00dev =
374 container_of(work, struct rt2x00_dev, link.work.work);
375
376 /*
Ivo van Doorn25ab0022007-09-25 20:57:04 +0200377 * When the radio is shutting down we should
378 * immediately cease all link tuning.
379 */
380 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
381 return;
382
383 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700384 * Update statistics.
385 */
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200386 rt2x00dev->ops->lib->link_stats(rt2x00dev, &rt2x00dev->link.qual);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700387 rt2x00dev->low_level_stats.dot11FCSErrorCount +=
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200388 rt2x00dev->link.qual.rx_failed;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700389
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700390 /*
391 * Only perform the link tuning when Link tuning
392 * has been enabled (This could have been disabled from the EEPROM).
393 */
394 if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
395 rt2x00dev->ops->lib->link_tuner(rt2x00dev);
396
397 /*
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200398 * Evaluate antenna setup.
399 */
400 rt2x00lib_evaluate_antenna(rt2x00dev);
401
402 /*
Ivo van Doorn725d99d2007-09-25 20:53:20 +0200403 * Precalculate a portion of the link signal which is
404 * in based on the tx/rx success/failure counters.
405 */
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200406 rt2x00lib_precalculate_link_signal(&rt2x00dev->link.qual);
Ivo van Doorn725d99d2007-09-25 20:53:20 +0200407
408 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700409 * Increase tuner counter, and reschedule the next link tuner run.
410 */
411 rt2x00dev->link.count++;
412 queue_delayed_work(rt2x00dev->hw->workqueue, &rt2x00dev->link.work,
413 LINK_TUNE_INTERVAL);
414}
415
Johannes Berg4150c572007-09-17 01:29:23 -0400416static void rt2x00lib_packetfilter_scheduled(struct work_struct *work)
417{
418 struct rt2x00_dev *rt2x00dev =
419 container_of(work, struct rt2x00_dev, filter_work);
Ivo van Doorn5886d0d2007-10-06 14:13:38 +0200420 unsigned int filter = rt2x00dev->interface.filter;
421
422 /*
423 * Since we had stored the filter inside interface.filter,
424 * we should now clear that field. Otherwise the driver will
425 * assume nothing has changed (*total_flags will be compared
426 * to interface.filter to determine if any action is required).
427 */
428 rt2x00dev->interface.filter = 0;
Johannes Berg4150c572007-09-17 01:29:23 -0400429
430 rt2x00dev->ops->hw->configure_filter(rt2x00dev->hw,
Ivo van Doorn5886d0d2007-10-06 14:13:38 +0200431 filter, &filter, 0, NULL);
Johannes Berg4150c572007-09-17 01:29:23 -0400432}
433
Ivo van Doorn5c58ee52007-10-06 13:34:52 +0200434static void rt2x00lib_configuration_scheduled(struct work_struct *work)
435{
436 struct rt2x00_dev *rt2x00dev =
437 container_of(work, struct rt2x00_dev, config_work);
438 int preamble = !test_bit(CONFIG_SHORT_PREAMBLE, &rt2x00dev->flags);
439
440 rt2x00mac_erp_ie_changed(rt2x00dev->hw,
441 IEEE80211_ERP_CHANGE_PREAMBLE, 0, preamble);
442}
443
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700444/*
445 * Interrupt context handlers.
446 */
447static void rt2x00lib_beacondone_scheduled(struct work_struct *work)
448{
449 struct rt2x00_dev *rt2x00dev =
450 container_of(work, struct rt2x00_dev, beacon_work);
451 struct data_ring *ring =
452 rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_BEACON);
453 struct data_entry *entry = rt2x00_get_data_entry(ring);
454 struct sk_buff *skb;
455
456 skb = ieee80211_beacon_get(rt2x00dev->hw,
457 rt2x00dev->interface.id,
458 &entry->tx_status.control);
459 if (!skb)
460 return;
461
462 rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw, skb,
463 &entry->tx_status.control);
464
465 dev_kfree_skb(skb);
466}
467
468void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
469{
470 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
471 return;
472
473 queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->beacon_work);
474}
475EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
476
477void rt2x00lib_txdone(struct data_entry *entry,
478 const int status, const int retry)
479{
480 struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
481 struct ieee80211_tx_status *tx_status = &entry->tx_status;
482 struct ieee80211_low_level_stats *stats = &rt2x00dev->low_level_stats;
483 int success = !!(status == TX_SUCCESS || status == TX_SUCCESS_RETRY);
484 int fail = !!(status == TX_FAIL_RETRY || status == TX_FAIL_INVALID ||
485 status == TX_FAIL_OTHER);
486
487 /*
488 * Update TX statistics.
489 */
490 tx_status->flags = 0;
491 tx_status->ack_signal = 0;
492 tx_status->excessive_retries = (status == TX_FAIL_RETRY);
493 tx_status->retry_count = retry;
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200494 rt2x00dev->link.qual.tx_success += success;
495 rt2x00dev->link.qual.tx_failed += retry + fail;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700496
497 if (!(tx_status->control.flags & IEEE80211_TXCTL_NO_ACK)) {
498 if (success)
499 tx_status->flags |= IEEE80211_TX_STATUS_ACK;
500 else
501 stats->dot11ACKFailureCount++;
502 }
503
504 tx_status->queue_length = entry->ring->stats.limit;
505 tx_status->queue_number = tx_status->control.queue;
506
507 if (tx_status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) {
508 if (success)
509 stats->dot11RTSSuccessCount++;
510 else
511 stats->dot11RTSFailureCount++;
512 }
513
514 /*
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100515 * Send the tx_status to mac80211 & debugfs.
516 * mac80211 will clean up the skb structure.
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700517 */
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100518 get_skb_desc(entry->skb)->frame_type = DUMP_FRAME_TXDONE;
519 rt2x00debug_dump_frame(rt2x00dev, entry->skb);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700520 ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb, tx_status);
521 entry->skb = NULL;
522}
523EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
524
525void rt2x00lib_rxdone(struct data_entry *entry, struct sk_buff *skb,
Johannes Berg4150c572007-09-17 01:29:23 -0400526 struct rxdata_entry_desc *desc)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700527{
528 struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
Mattias Nissler61af43c2007-11-27 21:50:26 +0100529 struct interface *intf = &rt2x00dev->interface;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700530 struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
531 struct ieee80211_hw_mode *mode;
532 struct ieee80211_rate *rate;
Mattias Nissler61af43c2007-11-27 21:50:26 +0100533 struct ieee80211_hdr *hdr;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700534 unsigned int i;
535 int val = 0;
Mattias Nissler61af43c2007-11-27 21:50:26 +0100536 u16 fc;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700537
538 /*
539 * Update RX statistics.
540 */
541 mode = &rt2x00dev->hwmodes[rt2x00dev->curr_hwmode];
542 for (i = 0; i < mode->num_rates; i++) {
543 rate = &mode->rates[i];
544
545 /*
546 * When frame was received with an OFDM bitrate,
547 * the signal is the PLCP value. If it was received with
548 * a CCK bitrate the signal is the rate in 0.5kbit/s.
549 */
Johannes Berg4150c572007-09-17 01:29:23 -0400550 if (!desc->ofdm)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700551 val = DEVICE_GET_RATE_FIELD(rate->val, RATE);
552 else
553 val = DEVICE_GET_RATE_FIELD(rate->val, PLCP);
554
Johannes Berg4150c572007-09-17 01:29:23 -0400555 if (val == desc->signal) {
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700556 val = rate->val;
557 break;
558 }
559 }
560
Mattias Nissler61af43c2007-11-27 21:50:26 +0100561 /*
562 * Only update link status if this is a beacon frame carrying our
563 * bssid.
564 */
565 hdr = (struct ieee80211_hdr *) skb->data;
566 if (skb->len >= sizeof(struct ieee80211_hdr *)) {
567 fc = le16_to_cpu(hdr->frame_control);
568 if ((intf->type == IEEE80211_IF_TYPE_STA
569 || intf->type == IEEE80211_IF_TYPE_IBSS)
570 && is_beacon(fc)
571 && compare_ether_addr(hdr->addr3, intf->bssid) == 0)
572 rt2x00lib_update_link_stats(&rt2x00dev->link,
573 desc->rssi);
574 }
575
Ivo van Doornebcf26d2007-10-13 16:26:12 +0200576 rt2x00dev->link.qual.rx_success++;
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200577
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700578 rx_status->rate = val;
Johannes Berg4150c572007-09-17 01:29:23 -0400579 rx_status->signal =
580 rt2x00lib_calculate_link_signal(rt2x00dev, desc->rssi);
581 rx_status->ssi = desc->rssi;
582 rx_status->flag = desc->flags;
Ivo van Doorn69f81a22007-10-13 16:26:36 +0200583 rx_status->antenna = rt2x00dev->link.ant.active.rx;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700584
585 /*
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100586 * Send frame to mac80211 & debugfs
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700587 */
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100588 get_skb_desc(skb)->frame_type = DUMP_FRAME_RXDONE;
589 rt2x00debug_dump_frame(rt2x00dev, skb);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700590 ieee80211_rx_irqsafe(rt2x00dev->hw, skb, rx_status);
591}
592EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
593
594/*
595 * TX descriptor initializer
596 */
597void rt2x00lib_write_tx_desc(struct rt2x00_dev *rt2x00dev,
Ivo van Doorn08992f72008-01-24 01:56:25 -0800598 struct sk_buff *skb,
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700599 struct ieee80211_tx_control *control)
600{
Johannes Berg4150c572007-09-17 01:29:23 -0400601 struct txdata_entry_desc desc;
Ivo van Doorn08992f72008-01-24 01:56:25 -0800602 struct skb_desc *skbdesc = get_skb_desc(skb);
603 struct ieee80211_hdr *ieee80211hdr = skbdesc->data;
604 __le32 *txd = skbdesc->desc;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700605 int tx_rate;
606 int bitrate;
Ivo van Doorn08992f72008-01-24 01:56:25 -0800607 int length;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700608 int duration;
609 int residual;
610 u16 frame_control;
611 u16 seq_ctrl;
612
Ivo van Doorn08992f72008-01-24 01:56:25 -0800613 memset(&desc, 0, sizeof(desc));
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700614
Ivo van Doorn08992f72008-01-24 01:56:25 -0800615 desc.cw_min = skbdesc->ring->tx_params.cw_min;
616 desc.cw_max = skbdesc->ring->tx_params.cw_max;
617 desc.aifs = skbdesc->ring->tx_params.aifs;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700618
619 /*
620 * Identify queue
621 */
622 if (control->queue < rt2x00dev->hw->queues)
623 desc.queue = control->queue;
624 else if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
625 control->queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
626 desc.queue = QUEUE_MGMT;
627 else
628 desc.queue = QUEUE_OTHER;
629
630 /*
631 * Read required fields from ieee80211 header.
632 */
633 frame_control = le16_to_cpu(ieee80211hdr->frame_control);
634 seq_ctrl = le16_to_cpu(ieee80211hdr->seq_ctrl);
635
636 tx_rate = control->tx_rate;
637
638 /*
Mattias Nissler2700f8b2007-10-27 13:43:49 +0200639 * Check whether this frame is to be acked
640 */
641 if (!(control->flags & IEEE80211_TXCTL_NO_ACK))
642 __set_bit(ENTRY_TXD_ACK, &desc.flags);
643
644 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700645 * Check if this is a RTS/CTS frame
646 */
647 if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
648 __set_bit(ENTRY_TXD_BURST, &desc.flags);
Mattias Nissler2700f8b2007-10-27 13:43:49 +0200649 if (is_rts_frame(frame_control)) {
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700650 __set_bit(ENTRY_TXD_RTS_FRAME, &desc.flags);
Mattias Nissler2700f8b2007-10-27 13:43:49 +0200651 __set_bit(ENTRY_TXD_ACK, &desc.flags);
652 } else
653 __clear_bit(ENTRY_TXD_ACK, &desc.flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700654 if (control->rts_cts_rate)
655 tx_rate = control->rts_cts_rate;
656 }
657
658 /*
659 * Check for OFDM
660 */
661 if (DEVICE_GET_RATE_FIELD(tx_rate, RATEMASK) & DEV_OFDM_RATEMASK)
662 __set_bit(ENTRY_TXD_OFDM_RATE, &desc.flags);
663
664 /*
665 * Check if more fragments are pending
666 */
667 if (ieee80211_get_morefrag(ieee80211hdr)) {
668 __set_bit(ENTRY_TXD_BURST, &desc.flags);
669 __set_bit(ENTRY_TXD_MORE_FRAG, &desc.flags);
670 }
671
672 /*
673 * Beacons and probe responses require the tsf timestamp
674 * to be inserted into the frame.
675 */
676 if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
677 is_probe_resp(frame_control))
678 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &desc.flags);
679
680 /*
681 * Determine with what IFS priority this frame should be send.
682 * Set ifs to IFS_SIFS when the this is not the first fragment,
683 * or this fragment came after RTS/CTS.
684 */
685 if ((seq_ctrl & IEEE80211_SCTL_FRAG) > 0 ||
686 test_bit(ENTRY_TXD_RTS_FRAME, &desc.flags))
687 desc.ifs = IFS_SIFS;
688 else
689 desc.ifs = IFS_BACKOFF;
690
691 /*
692 * PLCP setup
693 * Length calculation depends on OFDM/CCK rate.
694 */
695 desc.signal = DEVICE_GET_RATE_FIELD(tx_rate, PLCP);
696 desc.service = 0x04;
697
Ivo van Doorn08992f72008-01-24 01:56:25 -0800698 length = skbdesc->data_len + FCS_LEN;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700699 if (test_bit(ENTRY_TXD_OFDM_RATE, &desc.flags)) {
Ivo van Doorn08992f72008-01-24 01:56:25 -0800700 desc.length_high = (length >> 6) & 0x3f;
701 desc.length_low = length & 0x3f;
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700702 } else {
703 bitrate = DEVICE_GET_RATE_FIELD(tx_rate, RATE);
704
705 /*
706 * Convert length to microseconds.
707 */
Ivo van Doorn08992f72008-01-24 01:56:25 -0800708 residual = get_duration_res(length, bitrate);
709 duration = get_duration(length, bitrate);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700710
711 if (residual != 0) {
712 duration++;
713
714 /*
715 * Check if we need to set the Length Extension
716 */
Mattias Nisslerdb151782007-10-13 16:26:52 +0200717 if (bitrate == 110 && residual <= 30)
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700718 desc.service |= 0x80;
719 }
720
721 desc.length_high = (duration >> 8) & 0xff;
722 desc.length_low = duration & 0xff;
723
724 /*
725 * When preamble is enabled we should set the
726 * preamble bit for the signal.
727 */
728 if (DEVICE_GET_RATE_FIELD(tx_rate, PREAMBLE))
729 desc.signal |= 0x08;
730 }
731
Ivo van Doorn08992f72008-01-24 01:56:25 -0800732 rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, txd, &desc, ieee80211hdr,
733 skbdesc->data_len, control);
734
735 /*
736 * Update ring entry.
737 */
738 skbdesc->entry->skb = skb;
739 memcpy(&skbdesc->entry->tx_status.control, control, sizeof(*control));
Ivo van Doorn4d8dd662007-11-27 21:49:29 +0100740
741 /*
742 * The frame has been completely initialized and ready
743 * for sending to the device. The caller will push the
744 * frame to the device, but we are going to push the
745 * frame to debugfs here.
746 */
747 skbdesc->frame_type = DUMP_FRAME_TX;
748 rt2x00debug_dump_frame(rt2x00dev, skb);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700749}
750EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc);
751
752/*
753 * Driver initialization handlers.
754 */
755static void rt2x00lib_channel(struct ieee80211_channel *entry,
756 const int channel, const int tx_power,
757 const int value)
758{
759 entry->chan = channel;
760 if (channel <= 14)
761 entry->freq = 2407 + (5 * channel);
762 else
763 entry->freq = 5000 + (5 * channel);
764 entry->val = value;
765 entry->flag =
766 IEEE80211_CHAN_W_IBSS |
767 IEEE80211_CHAN_W_ACTIVE_SCAN |
768 IEEE80211_CHAN_W_SCAN;
769 entry->power_level = tx_power;
770 entry->antenna_max = 0xff;
771}
772
773static void rt2x00lib_rate(struct ieee80211_rate *entry,
774 const int rate, const int mask,
775 const int plcp, const int flags)
776{
777 entry->rate = rate;
778 entry->val =
779 DEVICE_SET_RATE_FIELD(rate, RATE) |
780 DEVICE_SET_RATE_FIELD(mask, RATEMASK) |
781 DEVICE_SET_RATE_FIELD(plcp, PLCP);
782 entry->flags = flags;
783 entry->val2 = entry->val;
784 if (entry->flags & IEEE80211_RATE_PREAMBLE2)
785 entry->val2 |= DEVICE_SET_RATE_FIELD(1, PREAMBLE);
786 entry->min_rssi_ack = 0;
787 entry->min_rssi_ack_delta = 0;
788}
789
790static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
791 struct hw_mode_spec *spec)
792{
793 struct ieee80211_hw *hw = rt2x00dev->hw;
794 struct ieee80211_hw_mode *hwmodes;
795 struct ieee80211_channel *channels;
796 struct ieee80211_rate *rates;
797 unsigned int i;
798 unsigned char tx_power;
799
800 hwmodes = kzalloc(sizeof(*hwmodes) * spec->num_modes, GFP_KERNEL);
801 if (!hwmodes)
802 goto exit;
803
804 channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
805 if (!channels)
806 goto exit_free_modes;
807
808 rates = kzalloc(sizeof(*rates) * spec->num_rates, GFP_KERNEL);
809 if (!rates)
810 goto exit_free_channels;
811
812 /*
813 * Initialize Rate list.
814 */
815 rt2x00lib_rate(&rates[0], 10, DEV_RATEMASK_1MB,
816 0x00, IEEE80211_RATE_CCK);
817 rt2x00lib_rate(&rates[1], 20, DEV_RATEMASK_2MB,
818 0x01, IEEE80211_RATE_CCK_2);
819 rt2x00lib_rate(&rates[2], 55, DEV_RATEMASK_5_5MB,
820 0x02, IEEE80211_RATE_CCK_2);
821 rt2x00lib_rate(&rates[3], 110, DEV_RATEMASK_11MB,
822 0x03, IEEE80211_RATE_CCK_2);
823
824 if (spec->num_rates > 4) {
825 rt2x00lib_rate(&rates[4], 60, DEV_RATEMASK_6MB,
826 0x0b, IEEE80211_RATE_OFDM);
827 rt2x00lib_rate(&rates[5], 90, DEV_RATEMASK_9MB,
828 0x0f, IEEE80211_RATE_OFDM);
829 rt2x00lib_rate(&rates[6], 120, DEV_RATEMASK_12MB,
830 0x0a, IEEE80211_RATE_OFDM);
831 rt2x00lib_rate(&rates[7], 180, DEV_RATEMASK_18MB,
832 0x0e, IEEE80211_RATE_OFDM);
833 rt2x00lib_rate(&rates[8], 240, DEV_RATEMASK_24MB,
834 0x09, IEEE80211_RATE_OFDM);
835 rt2x00lib_rate(&rates[9], 360, DEV_RATEMASK_36MB,
836 0x0d, IEEE80211_RATE_OFDM);
837 rt2x00lib_rate(&rates[10], 480, DEV_RATEMASK_48MB,
838 0x08, IEEE80211_RATE_OFDM);
839 rt2x00lib_rate(&rates[11], 540, DEV_RATEMASK_54MB,
840 0x0c, IEEE80211_RATE_OFDM);
841 }
842
843 /*
844 * Initialize Channel list.
845 */
846 for (i = 0; i < spec->num_channels; i++) {
847 if (spec->channels[i].channel <= 14)
848 tx_power = spec->tx_power_bg[i];
849 else if (spec->tx_power_a)
850 tx_power = spec->tx_power_a[i];
851 else
852 tx_power = spec->tx_power_default;
853
854 rt2x00lib_channel(&channels[i],
855 spec->channels[i].channel, tx_power, i);
856 }
857
858 /*
859 * Intitialize 802.11b
860 * Rates: CCK.
861 * Channels: OFDM.
862 */
863 if (spec->num_modes > HWMODE_B) {
864 hwmodes[HWMODE_B].mode = MODE_IEEE80211B;
865 hwmodes[HWMODE_B].num_channels = 14;
866 hwmodes[HWMODE_B].num_rates = 4;
867 hwmodes[HWMODE_B].channels = channels;
868 hwmodes[HWMODE_B].rates = rates;
869 }
870
871 /*
872 * Intitialize 802.11g
873 * Rates: CCK, OFDM.
874 * Channels: OFDM.
875 */
876 if (spec->num_modes > HWMODE_G) {
877 hwmodes[HWMODE_G].mode = MODE_IEEE80211G;
878 hwmodes[HWMODE_G].num_channels = 14;
879 hwmodes[HWMODE_G].num_rates = spec->num_rates;
880 hwmodes[HWMODE_G].channels = channels;
881 hwmodes[HWMODE_G].rates = rates;
882 }
883
884 /*
885 * Intitialize 802.11a
886 * Rates: OFDM.
887 * Channels: OFDM, UNII, HiperLAN2.
888 */
889 if (spec->num_modes > HWMODE_A) {
890 hwmodes[HWMODE_A].mode = MODE_IEEE80211A;
891 hwmodes[HWMODE_A].num_channels = spec->num_channels - 14;
892 hwmodes[HWMODE_A].num_rates = spec->num_rates - 4;
893 hwmodes[HWMODE_A].channels = &channels[14];
894 hwmodes[HWMODE_A].rates = &rates[4];
895 }
896
897 if (spec->num_modes > HWMODE_G &&
898 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_G]))
899 goto exit_free_rates;
900
901 if (spec->num_modes > HWMODE_B &&
902 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_B]))
903 goto exit_free_rates;
904
905 if (spec->num_modes > HWMODE_A &&
906 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_A]))
907 goto exit_free_rates;
908
909 rt2x00dev->hwmodes = hwmodes;
910
911 return 0;
912
913exit_free_rates:
914 kfree(rates);
915
916exit_free_channels:
917 kfree(channels);
918
919exit_free_modes:
920 kfree(hwmodes);
921
922exit:
923 ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
924 return -ENOMEM;
925}
926
927static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
928{
Ivo van Doorn066cb632007-09-25 20:55:39 +0200929 if (test_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700930 ieee80211_unregister_hw(rt2x00dev->hw);
931
932 if (likely(rt2x00dev->hwmodes)) {
933 kfree(rt2x00dev->hwmodes->channels);
934 kfree(rt2x00dev->hwmodes->rates);
935 kfree(rt2x00dev->hwmodes);
936 rt2x00dev->hwmodes = NULL;
937 }
938}
939
940static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
941{
942 struct hw_mode_spec *spec = &rt2x00dev->spec;
943 int status;
944
945 /*
946 * Initialize HW modes.
947 */
948 status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
949 if (status)
950 return status;
951
952 /*
953 * Register HW.
954 */
955 status = ieee80211_register_hw(rt2x00dev->hw);
956 if (status) {
957 rt2x00lib_remove_hw(rt2x00dev);
958 return status;
959 }
960
Ivo van Doorn066cb632007-09-25 20:55:39 +0200961 __set_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -0700962
963 return 0;
964}
965
966/*
967 * Initialization/uninitialization handlers.
968 */
969static int rt2x00lib_alloc_entries(struct data_ring *ring,
970 const u16 max_entries, const u16 data_size,
971 const u16 desc_size)
972{
973 struct data_entry *entry;
974 unsigned int i;
975
976 ring->stats.limit = max_entries;
977 ring->data_size = data_size;
978 ring->desc_size = desc_size;
979
980 /*
981 * Allocate all ring entries.
982 */
983 entry = kzalloc(ring->stats.limit * sizeof(*entry), GFP_KERNEL);
984 if (!entry)
985 return -ENOMEM;
986
987 for (i = 0; i < ring->stats.limit; i++) {
988 entry[i].flags = 0;
989 entry[i].ring = ring;
990 entry[i].skb = NULL;
991 }
992
993 ring->entry = entry;
994
995 return 0;
996}
997
998static int rt2x00lib_alloc_ring_entries(struct rt2x00_dev *rt2x00dev)
999{
1000 struct data_ring *ring;
1001
1002 /*
1003 * Allocate the RX ring.
1004 */
1005 if (rt2x00lib_alloc_entries(rt2x00dev->rx, RX_ENTRIES, DATA_FRAME_SIZE,
1006 rt2x00dev->ops->rxd_size))
1007 return -ENOMEM;
1008
1009 /*
1010 * First allocate the TX rings.
1011 */
1012 txring_for_each(rt2x00dev, ring) {
1013 if (rt2x00lib_alloc_entries(ring, TX_ENTRIES, DATA_FRAME_SIZE,
1014 rt2x00dev->ops->txd_size))
1015 return -ENOMEM;
1016 }
1017
Ivo van Doorn066cb632007-09-25 20:55:39 +02001018 if (!test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001019 return 0;
1020
1021 /*
1022 * Allocate the BEACON ring.
1023 */
1024 if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[0], BEACON_ENTRIES,
1025 MGMT_FRAME_SIZE, rt2x00dev->ops->txd_size))
1026 return -ENOMEM;
1027
1028 /*
1029 * Allocate the Atim ring.
1030 */
1031 if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[1], ATIM_ENTRIES,
1032 DATA_FRAME_SIZE, rt2x00dev->ops->txd_size))
1033 return -ENOMEM;
1034
1035 return 0;
1036}
1037
1038static void rt2x00lib_free_ring_entries(struct rt2x00_dev *rt2x00dev)
1039{
1040 struct data_ring *ring;
1041
1042 ring_for_each(rt2x00dev, ring) {
1043 kfree(ring->entry);
1044 ring->entry = NULL;
1045 }
1046}
1047
1048void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
1049{
1050 if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
1051 return;
1052
1053 /*
1054 * Unregister rfkill.
1055 */
1056 rt2x00rfkill_unregister(rt2x00dev);
1057
1058 /*
1059 * Allow the HW to uninitialize.
1060 */
1061 rt2x00dev->ops->lib->uninitialize(rt2x00dev);
1062
1063 /*
1064 * Free allocated ring entries.
1065 */
1066 rt2x00lib_free_ring_entries(rt2x00dev);
1067}
1068
1069int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
1070{
1071 int status;
1072
1073 if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
1074 return 0;
1075
1076 /*
1077 * Allocate all ring entries.
1078 */
1079 status = rt2x00lib_alloc_ring_entries(rt2x00dev);
1080 if (status) {
1081 ERROR(rt2x00dev, "Ring entries allocation failed.\n");
1082 return status;
1083 }
1084
1085 /*
1086 * Initialize the device.
1087 */
1088 status = rt2x00dev->ops->lib->initialize(rt2x00dev);
1089 if (status)
1090 goto exit;
1091
1092 __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
1093
1094 /*
1095 * Register the rfkill handler.
1096 */
1097 status = rt2x00rfkill_register(rt2x00dev);
1098 if (status)
1099 goto exit_unitialize;
1100
1101 return 0;
1102
1103exit_unitialize:
1104 rt2x00lib_uninitialize(rt2x00dev);
1105
1106exit:
1107 rt2x00lib_free_ring_entries(rt2x00dev);
1108
1109 return status;
1110}
1111
1112/*
1113 * driver allocation handlers.
1114 */
1115static int rt2x00lib_alloc_rings(struct rt2x00_dev *rt2x00dev)
1116{
1117 struct data_ring *ring;
1118
1119 /*
1120 * We need the following rings:
1121 * RX: 1
1122 * TX: hw->queues
1123 * Beacon: 1 (if required)
1124 * Atim: 1 (if required)
1125 */
1126 rt2x00dev->data_rings = 1 + rt2x00dev->hw->queues +
Ivo van Doorn066cb632007-09-25 20:55:39 +02001127 (2 * test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags));
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001128
1129 ring = kzalloc(rt2x00dev->data_rings * sizeof(*ring), GFP_KERNEL);
1130 if (!ring) {
1131 ERROR(rt2x00dev, "Ring allocation failed.\n");
1132 return -ENOMEM;
1133 }
1134
1135 /*
1136 * Initialize pointers
1137 */
1138 rt2x00dev->rx = ring;
1139 rt2x00dev->tx = &rt2x00dev->rx[1];
Ivo van Doorn066cb632007-09-25 20:55:39 +02001140 if (test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001141 rt2x00dev->bcn = &rt2x00dev->tx[rt2x00dev->hw->queues];
1142
1143 /*
1144 * Initialize ring parameters.
1145 * cw_min: 2^5 = 32.
1146 * cw_max: 2^10 = 1024.
1147 */
1148 ring_for_each(rt2x00dev, ring) {
1149 ring->rt2x00dev = rt2x00dev;
1150 ring->tx_params.aifs = 2;
1151 ring->tx_params.cw_min = 5;
1152 ring->tx_params.cw_max = 10;
1153 }
1154
1155 return 0;
1156}
1157
1158static void rt2x00lib_free_rings(struct rt2x00_dev *rt2x00dev)
1159{
1160 kfree(rt2x00dev->rx);
1161 rt2x00dev->rx = NULL;
1162 rt2x00dev->tx = NULL;
1163 rt2x00dev->bcn = NULL;
1164}
1165
1166int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
1167{
1168 int retval = -ENOMEM;
1169
1170 /*
1171 * Let the driver probe the device to detect the capabilities.
1172 */
1173 retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
1174 if (retval) {
1175 ERROR(rt2x00dev, "Failed to allocate device.\n");
1176 goto exit;
1177 }
1178
1179 /*
1180 * Initialize configuration work.
1181 */
1182 INIT_WORK(&rt2x00dev->beacon_work, rt2x00lib_beacondone_scheduled);
Johannes Berg4150c572007-09-17 01:29:23 -04001183 INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
Ivo van Doorn5c58ee52007-10-06 13:34:52 +02001184 INIT_WORK(&rt2x00dev->config_work, rt2x00lib_configuration_scheduled);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001185 INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
1186
1187 /*
1188 * Reset current working type.
1189 */
Ivo van Doornd28c2562007-11-27 21:49:50 +01001190 rt2x00dev->interface.type = IEEE80211_IF_TYPE_INVALID;
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001191
1192 /*
1193 * Allocate ring array.
1194 */
1195 retval = rt2x00lib_alloc_rings(rt2x00dev);
1196 if (retval)
1197 goto exit;
1198
1199 /*
1200 * Initialize ieee80211 structure.
1201 */
1202 retval = rt2x00lib_probe_hw(rt2x00dev);
1203 if (retval) {
1204 ERROR(rt2x00dev, "Failed to initialize hw.\n");
1205 goto exit;
1206 }
1207
1208 /*
1209 * Allocatie rfkill.
1210 */
1211 retval = rt2x00rfkill_allocate(rt2x00dev);
1212 if (retval)
1213 goto exit;
1214
1215 /*
1216 * Open the debugfs entry.
1217 */
1218 rt2x00debug_register(rt2x00dev);
1219
Ivo van Doorn066cb632007-09-25 20:55:39 +02001220 __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1221
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001222 return 0;
1223
1224exit:
1225 rt2x00lib_remove_dev(rt2x00dev);
1226
1227 return retval;
1228}
1229EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
1230
1231void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
1232{
Ivo van Doorn066cb632007-09-25 20:55:39 +02001233 __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1234
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001235 /*
1236 * Disable radio.
1237 */
1238 rt2x00lib_disable_radio(rt2x00dev);
1239
1240 /*
1241 * Uninitialize device.
1242 */
1243 rt2x00lib_uninitialize(rt2x00dev);
1244
1245 /*
1246 * Close debugfs entry.
1247 */
1248 rt2x00debug_deregister(rt2x00dev);
1249
1250 /*
1251 * Free rfkill
1252 */
1253 rt2x00rfkill_free(rt2x00dev);
1254
1255 /*
1256 * Free ieee80211_hw memory.
1257 */
1258 rt2x00lib_remove_hw(rt2x00dev);
1259
1260 /*
1261 * Free firmware image.
1262 */
1263 rt2x00lib_free_firmware(rt2x00dev);
1264
1265 /*
1266 * Free ring structures.
1267 */
1268 rt2x00lib_free_rings(rt2x00dev);
1269}
1270EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
1271
1272/*
1273 * Device state handlers
1274 */
1275#ifdef CONFIG_PM
1276int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
1277{
1278 int retval;
1279
1280 NOTICE(rt2x00dev, "Going to sleep.\n");
Ivo van Doorn066cb632007-09-25 20:55:39 +02001281 __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1282
1283 /*
1284 * Only continue if mac80211 has open interfaces.
1285 */
1286 if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1287 goto exit;
Ivo van Doorn6d7f9872007-10-06 14:12:42 +02001288 __set_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001289
1290 /*
1291 * Disable radio and unitialize all items
1292 * that must be recreated on resume.
1293 */
Ivo van Doorn6d7f9872007-10-06 14:12:42 +02001294 rt2x00mac_stop(rt2x00dev->hw);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001295 rt2x00lib_uninitialize(rt2x00dev);
1296 rt2x00debug_deregister(rt2x00dev);
1297
Ivo van Doorn066cb632007-09-25 20:55:39 +02001298exit:
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001299 /*
1300 * Set device mode to sleep for power management.
1301 */
1302 retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
1303 if (retval)
1304 return retval;
1305
1306 return 0;
1307}
1308EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1309
1310int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1311{
1312 struct interface *intf = &rt2x00dev->interface;
1313 int retval;
1314
1315 NOTICE(rt2x00dev, "Waking up.\n");
Ivo van Doorn066cb632007-09-25 20:55:39 +02001316 __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001317
1318 /*
1319 * Open the debugfs entry.
1320 */
1321 rt2x00debug_register(rt2x00dev);
1322
1323 /*
Ivo van Doorn6d7f9872007-10-06 14:12:42 +02001324 * Only continue if mac80211 had open interfaces.
Ivo van Doorn066cb632007-09-25 20:55:39 +02001325 */
Ivo van Doorn6d7f9872007-10-06 14:12:42 +02001326 if (!__test_and_clear_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags))
Ivo van Doorn066cb632007-09-25 20:55:39 +02001327 return 0;
1328
1329 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001330 * Reinitialize device and all active interfaces.
1331 */
1332 retval = rt2x00mac_start(rt2x00dev->hw);
1333 if (retval)
1334 goto exit;
1335
1336 /*
1337 * Reconfigure device.
1338 */
Ivo van Doorn066cb632007-09-25 20:55:39 +02001339 rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1);
1340 if (!rt2x00dev->hw->conf.radio_enabled)
1341 rt2x00lib_disable_radio(rt2x00dev);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001342
1343 rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
1344 rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
1345 rt2x00lib_config_type(rt2x00dev, intf->type);
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001346
1347 /*
Ivo van Doorn066cb632007-09-25 20:55:39 +02001348 * It is possible that during that mac80211 has attempted
1349 * to send frames while we were suspending or resuming.
1350 * In that case we have disabled the TX queue and should
1351 * now enable it again
1352 */
1353 ieee80211_start_queues(rt2x00dev->hw);
1354
1355 /*
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001356 * When in Master or Ad-hoc mode,
1357 * restart Beacon transmitting by faking a beacondone event.
1358 */
1359 if (intf->type == IEEE80211_IF_TYPE_AP ||
1360 intf->type == IEEE80211_IF_TYPE_IBSS)
1361 rt2x00lib_beacondone(rt2x00dev);
1362
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001363 return 0;
1364
1365exit:
1366 rt2x00lib_disable_radio(rt2x00dev);
1367 rt2x00lib_uninitialize(rt2x00dev);
1368 rt2x00debug_deregister(rt2x00dev);
1369
Ivo van Doorn95ea3622007-09-25 17:57:13 -07001370 return retval;
1371}
1372EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1373#endif /* CONFIG_PM */
1374
1375/*
1376 * rt2x00lib module information.
1377 */
1378MODULE_AUTHOR(DRV_PROJECT);
1379MODULE_VERSION(DRV_VERSION);
1380MODULE_DESCRIPTION("rt2x00 library");
1381MODULE_LICENSE("GPL");