]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/wireless/rt2x00/rt2x00dev.c
rt2x00: Don't switch to antenna with low rssi
[mirror_ubuntu-bionic-kernel.git] / drivers / net / wireless / rt2x00 / rt2x00dev.c
CommitLineData
95ea3627
ID
1/*
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
95ea3627
ID
26#include <linux/kernel.h>
27#include <linux/module.h>
28
29#include "rt2x00.h"
30#include "rt2x00lib.h"
4d8dd66c 31#include "rt2x00dump.h"
95ea3627
ID
32
33/*
34 * Ring handler.
35 */
36struct data_ring *rt2x00lib_get_ring(struct rt2x00_dev *rt2x00dev,
37 const unsigned int queue)
38{
066cb637 39 int beacon = test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags);
95ea3627
ID
40
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{
8de8c516
ID
66 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;
95ea3627
ID
81
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{
3e30968e 93 cancel_delayed_work_sync(&rt2x00dev->link.work);
95ea3627
ID
94}
95
96void rt2x00lib_reset_link_tuner(struct rt2x00_dev *rt2x00dev)
97{
fdd0abc8
ID
98 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
99 return;
100
95ea3627
ID
101 rt2x00lib_stop_link_tuner(rt2x00dev);
102 rt2x00lib_start_link_tuner(rt2x00dev);
103}
104
837e7f24
ID
105/*
106 * Ring initialization
107 */
108static void rt2x00lib_init_rxrings(struct rt2x00_dev *rt2x00dev)
109{
110 struct data_ring *ring = rt2x00dev->rx;
111 unsigned int i;
112
113 if (!rt2x00dev->ops->lib->init_rxentry)
114 return;
115
116 if (ring->data_addr)
117 memset(ring->data_addr, 0, rt2x00_get_ring_size(ring));
118
119 for (i = 0; i < ring->stats.limit; i++)
120 rt2x00dev->ops->lib->init_rxentry(rt2x00dev, &ring->entry[i]);
121
122 rt2x00_ring_index_clear(ring);
123}
124
125static void rt2x00lib_init_txrings(struct rt2x00_dev *rt2x00dev)
126{
127 struct data_ring *ring;
128 unsigned int i;
129
130 if (!rt2x00dev->ops->lib->init_txentry)
131 return;
132
133 txringall_for_each(rt2x00dev, ring) {
134 if (ring->data_addr)
135 memset(ring->data_addr, 0, rt2x00_get_ring_size(ring));
136
137 for (i = 0; i < ring->stats.limit; i++)
138 rt2x00dev->ops->lib->init_txentry(rt2x00dev,
139 &ring->entry[i]);
140
141 rt2x00_ring_index_clear(ring);
142 }
143}
144
95ea3627
ID
145/*
146 * Radio control handlers.
147 */
148int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
149{
150 int status;
151
152 /*
153 * Don't enable the radio twice.
154 * And check if the hardware button has been disabled.
155 */
156 if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
81873e9c 157 test_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags))
95ea3627
ID
158 return 0;
159
837e7f24
ID
160 /*
161 * Initialize all data rings.
162 */
163 rt2x00lib_init_rxrings(rt2x00dev);
164 rt2x00lib_init_txrings(rt2x00dev);
165
95ea3627
ID
166 /*
167 * Enable radio.
168 */
169 status = rt2x00dev->ops->lib->set_device_state(rt2x00dev,
170 STATE_RADIO_ON);
171 if (status)
172 return status;
173
174 __set_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags);
175
176 /*
177 * Enable RX.
178 */
5cbf830e 179 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
95ea3627
ID
180
181 /*
182 * Start the TX queues.
183 */
184 ieee80211_start_queues(rt2x00dev->hw);
185
186 return 0;
187}
188
189void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
190{
191 if (!__test_and_clear_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
192 return;
193
194 /*
4150c572 195 * Stop all scheduled work.
95ea3627
ID
196 */
197 if (work_pending(&rt2x00dev->beacon_work))
198 cancel_work_sync(&rt2x00dev->beacon_work);
4150c572
JB
199 if (work_pending(&rt2x00dev->filter_work))
200 cancel_work_sync(&rt2x00dev->filter_work);
5c58ee51
ID
201 if (work_pending(&rt2x00dev->config_work))
202 cancel_work_sync(&rt2x00dev->config_work);
95ea3627
ID
203
204 /*
205 * Stop the TX queues.
206 */
207 ieee80211_stop_queues(rt2x00dev->hw);
208
209 /*
210 * Disable RX.
211 */
5cbf830e 212 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
95ea3627
ID
213
214 /*
215 * Disable radio.
216 */
217 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
218}
219
5cbf830e 220void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, enum dev_state state)
95ea3627 221{
95ea3627
ID
222 /*
223 * When we are disabling the RX, we should also stop the link tuner.
224 */
5cbf830e 225 if (state == STATE_RADIO_RX_OFF)
95ea3627
ID
226 rt2x00lib_stop_link_tuner(rt2x00dev);
227
228 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
229
230 /*
231 * When we are enabling the RX, we should also start the link tuner.
232 */
5cbf830e
ID
233 if (state == STATE_RADIO_RX_ON &&
234 is_interface_present(&rt2x00dev->interface))
95ea3627
ID
235 rt2x00lib_start_link_tuner(rt2x00dev);
236}
237
69f81a2c
ID
238static void rt2x00lib_evaluate_antenna_sample(struct rt2x00_dev *rt2x00dev)
239{
240 enum antenna rx = rt2x00dev->link.ant.active.rx;
241 enum antenna tx = rt2x00dev->link.ant.active.tx;
242 int sample_a =
243 rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_A);
244 int sample_b =
245 rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_B);
246
247 /*
248 * We are done sampling. Now we should evaluate the results.
249 */
250 rt2x00dev->link.ant.flags &= ~ANTENNA_MODE_SAMPLE;
251
252 /*
253 * During the last period we have sampled the RSSI
254 * from both antenna's. It now is time to determine
255 * which antenna demonstrated the best performance.
256 * When we are already on the antenna with the best
257 * performance, then there really is nothing for us
258 * left to do.
259 */
260 if (sample_a == sample_b)
261 return;
262
05253c93
ID
263 if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY)
264 rx = (sample_a > sample_b) ? ANTENNA_A : ANTENNA_B;
69f81a2c 265
05253c93
ID
266 if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)
267 tx = (sample_a > sample_b) ? ANTENNA_A : ANTENNA_B;
69f81a2c
ID
268
269 rt2x00lib_config_antenna(rt2x00dev, rx, tx);
270}
271
272static void rt2x00lib_evaluate_antenna_eval(struct rt2x00_dev *rt2x00dev)
273{
274 enum antenna rx = rt2x00dev->link.ant.active.rx;
275 enum antenna tx = rt2x00dev->link.ant.active.tx;
276 int rssi_curr = rt2x00_get_link_ant_rssi(&rt2x00dev->link);
277 int rssi_old = rt2x00_update_ant_rssi(&rt2x00dev->link, rssi_curr);
278
279 /*
280 * Legacy driver indicates that we should swap antenna's
281 * when the difference in RSSI is greater that 5. This
282 * also should be done when the RSSI was actually better
283 * then the previous sample.
284 * When the difference exceeds the threshold we should
285 * sample the rssi from the other antenna to make a valid
286 * comparison between the 2 antennas.
287 */
b290d433 288 if (abs(rssi_curr - rssi_old) < 5)
69f81a2c
ID
289 return;
290
291 rt2x00dev->link.ant.flags |= ANTENNA_MODE_SAMPLE;
292
293 if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY)
294 rx = (rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
295
296 if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)
297 tx = (tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
298
299 rt2x00lib_config_antenna(rt2x00dev, rx, tx);
300}
301
302static void rt2x00lib_evaluate_antenna(struct rt2x00_dev *rt2x00dev)
303{
304 /*
305 * Determine if software diversity is enabled for
306 * either the TX or RX antenna (or both).
307 * Always perform this check since within the link
308 * tuner interval the configuration might have changed.
309 */
310 rt2x00dev->link.ant.flags &= ~ANTENNA_RX_DIVERSITY;
311 rt2x00dev->link.ant.flags &= ~ANTENNA_TX_DIVERSITY;
312
313 if (rt2x00dev->hw->conf.antenna_sel_rx == 0 &&
b290d433 314 rt2x00dev->default_ant.rx == ANTENNA_SW_DIVERSITY)
69f81a2c
ID
315 rt2x00dev->link.ant.flags |= ANTENNA_RX_DIVERSITY;
316 if (rt2x00dev->hw->conf.antenna_sel_tx == 0 &&
b290d433 317 rt2x00dev->default_ant.tx == ANTENNA_SW_DIVERSITY)
69f81a2c
ID
318 rt2x00dev->link.ant.flags |= ANTENNA_TX_DIVERSITY;
319
320 if (!(rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY) &&
321 !(rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)) {
05253c93 322 rt2x00dev->link.ant.flags = 0;
69f81a2c
ID
323 return;
324 }
325
326 /*
327 * If we have only sampled the data over the last period
328 * we should now harvest the data. Otherwise just evaluate
329 * the data. The latter should only be performed once
330 * every 2 seconds.
331 */
332 if (rt2x00dev->link.ant.flags & ANTENNA_MODE_SAMPLE)
333 rt2x00lib_evaluate_antenna_sample(rt2x00dev);
334 else if (rt2x00dev->link.count & 1)
335 rt2x00lib_evaluate_antenna_eval(rt2x00dev);
336}
337
338static void rt2x00lib_update_link_stats(struct link *link, int rssi)
339{
340 int avg_rssi = rssi;
341
342 /*
343 * Update global RSSI
344 */
345 if (link->qual.avg_rssi)
346 avg_rssi = MOVING_AVERAGE(link->qual.avg_rssi, rssi, 8);
347 link->qual.avg_rssi = avg_rssi;
348
349 /*
350 * Update antenna RSSI
351 */
352 if (link->ant.rssi_ant)
353 rssi = MOVING_AVERAGE(link->ant.rssi_ant, rssi, 8);
354 link->ant.rssi_ant = rssi;
355}
356
ebcf26da 357static void rt2x00lib_precalculate_link_signal(struct link_qual *qual)
95ea3627 358{
ebcf26da
ID
359 if (qual->rx_failed || qual->rx_success)
360 qual->rx_percentage =
361 (qual->rx_success * 100) /
362 (qual->rx_failed + qual->rx_success);
95ea3627 363 else
ebcf26da 364 qual->rx_percentage = 50;
95ea3627 365
ebcf26da
ID
366 if (qual->tx_failed || qual->tx_success)
367 qual->tx_percentage =
368 (qual->tx_success * 100) /
369 (qual->tx_failed + qual->tx_success);
95ea3627 370 else
ebcf26da 371 qual->tx_percentage = 50;
95ea3627 372
ebcf26da
ID
373 qual->rx_success = 0;
374 qual->rx_failed = 0;
375 qual->tx_success = 0;
376 qual->tx_failed = 0;
95ea3627
ID
377}
378
379static int rt2x00lib_calculate_link_signal(struct rt2x00_dev *rt2x00dev,
380 int rssi)
381{
382 int rssi_percentage = 0;
383 int signal;
384
385 /*
386 * We need a positive value for the RSSI.
387 */
388 if (rssi < 0)
389 rssi += rt2x00dev->rssi_offset;
390
391 /*
392 * Calculate the different percentages,
393 * which will be used for the signal.
394 */
395 if (rt2x00dev->rssi_offset)
396 rssi_percentage = (rssi * 100) / rt2x00dev->rssi_offset;
397
398 /*
399 * Add the individual percentages and use the WEIGHT
400 * defines to calculate the current link signal.
401 */
402 signal = ((WEIGHT_RSSI * rssi_percentage) +
ebcf26da
ID
403 (WEIGHT_TX * rt2x00dev->link.qual.tx_percentage) +
404 (WEIGHT_RX * rt2x00dev->link.qual.rx_percentage)) / 100;
95ea3627
ID
405
406 return (signal > 100) ? 100 : signal;
407}
408
409static void rt2x00lib_link_tuner(struct work_struct *work)
410{
411 struct rt2x00_dev *rt2x00dev =
412 container_of(work, struct rt2x00_dev, link.work.work);
413
25ab002f
ID
414 /*
415 * When the radio is shutting down we should
416 * immediately cease all link tuning.
417 */
418 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
419 return;
420
95ea3627
ID
421 /*
422 * Update statistics.
423 */
ebcf26da 424 rt2x00dev->ops->lib->link_stats(rt2x00dev, &rt2x00dev->link.qual);
95ea3627 425 rt2x00dev->low_level_stats.dot11FCSErrorCount +=
ebcf26da 426 rt2x00dev->link.qual.rx_failed;
95ea3627 427
95ea3627
ID
428 /*
429 * Only perform the link tuning when Link tuning
430 * has been enabled (This could have been disabled from the EEPROM).
431 */
432 if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
433 rt2x00dev->ops->lib->link_tuner(rt2x00dev);
434
69f81a2c
ID
435 /*
436 * Evaluate antenna setup.
437 */
438 rt2x00lib_evaluate_antenna(rt2x00dev);
439
725d99d4
ID
440 /*
441 * Precalculate a portion of the link signal which is
442 * in based on the tx/rx success/failure counters.
443 */
ebcf26da 444 rt2x00lib_precalculate_link_signal(&rt2x00dev->link.qual);
725d99d4 445
95ea3627
ID
446 /*
447 * Increase tuner counter, and reschedule the next link tuner run.
448 */
449 rt2x00dev->link.count++;
450 queue_delayed_work(rt2x00dev->hw->workqueue, &rt2x00dev->link.work,
451 LINK_TUNE_INTERVAL);
452}
453
4150c572
JB
454static void rt2x00lib_packetfilter_scheduled(struct work_struct *work)
455{
456 struct rt2x00_dev *rt2x00dev =
457 container_of(work, struct rt2x00_dev, filter_work);
3c4f2085 458 unsigned int filter = rt2x00dev->packet_filter;
5886d0db
ID
459
460 /*
461 * Since we had stored the filter inside interface.filter,
462 * we should now clear that field. Otherwise the driver will
463 * assume nothing has changed (*total_flags will be compared
464 * to interface.filter to determine if any action is required).
465 */
3c4f2085 466 rt2x00dev->packet_filter = 0;
4150c572
JB
467
468 rt2x00dev->ops->hw->configure_filter(rt2x00dev->hw,
5886d0db 469 filter, &filter, 0, NULL);
4150c572
JB
470}
471
5c58ee51
ID
472static void rt2x00lib_configuration_scheduled(struct work_struct *work)
473{
474 struct rt2x00_dev *rt2x00dev =
475 container_of(work, struct rt2x00_dev, config_work);
471b3efd 476 struct ieee80211_bss_conf bss_conf;
5c58ee51 477
471b3efd
JB
478 bss_conf.use_short_preamble =
479 test_bit(CONFIG_SHORT_PREAMBLE, &rt2x00dev->flags);
480
481 /*
482 * FIXME: shouldn't invoke it this way because all other contents
483 * of bss_conf is invalid.
484 */
485 rt2x00mac_bss_info_changed(rt2x00dev->hw, rt2x00dev->interface.id,
486 &bss_conf, BSS_CHANGED_ERP_PREAMBLE);
5c58ee51
ID
487}
488
95ea3627
ID
489/*
490 * Interrupt context handlers.
491 */
492static void rt2x00lib_beacondone_scheduled(struct work_struct *work)
493{
494 struct rt2x00_dev *rt2x00dev =
495 container_of(work, struct rt2x00_dev, beacon_work);
496 struct data_ring *ring =
497 rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_BEACON);
498 struct data_entry *entry = rt2x00_get_data_entry(ring);
499 struct sk_buff *skb;
500
501 skb = ieee80211_beacon_get(rt2x00dev->hw,
502 rt2x00dev->interface.id,
503 &entry->tx_status.control);
504 if (!skb)
505 return;
506
507 rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw, skb,
508 &entry->tx_status.control);
509
510 dev_kfree_skb(skb);
511}
512
513void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
514{
515 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
516 return;
517
518 queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->beacon_work);
519}
520EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
521
522void rt2x00lib_txdone(struct data_entry *entry,
523 const int status, const int retry)
524{
525 struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
526 struct ieee80211_tx_status *tx_status = &entry->tx_status;
527 struct ieee80211_low_level_stats *stats = &rt2x00dev->low_level_stats;
528 int success = !!(status == TX_SUCCESS || status == TX_SUCCESS_RETRY);
529 int fail = !!(status == TX_FAIL_RETRY || status == TX_FAIL_INVALID ||
530 status == TX_FAIL_OTHER);
531
532 /*
533 * Update TX statistics.
534 */
535 tx_status->flags = 0;
536 tx_status->ack_signal = 0;
537 tx_status->excessive_retries = (status == TX_FAIL_RETRY);
538 tx_status->retry_count = retry;
ebcf26da
ID
539 rt2x00dev->link.qual.tx_success += success;
540 rt2x00dev->link.qual.tx_failed += retry + fail;
95ea3627
ID
541
542 if (!(tx_status->control.flags & IEEE80211_TXCTL_NO_ACK)) {
543 if (success)
544 tx_status->flags |= IEEE80211_TX_STATUS_ACK;
545 else
546 stats->dot11ACKFailureCount++;
547 }
548
549 tx_status->queue_length = entry->ring->stats.limit;
550 tx_status->queue_number = tx_status->control.queue;
551
552 if (tx_status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) {
553 if (success)
554 stats->dot11RTSSuccessCount++;
555 else
556 stats->dot11RTSFailureCount++;
557 }
558
559 /*
4d8dd66c
ID
560 * Send the tx_status to mac80211 & debugfs.
561 * mac80211 will clean up the skb structure.
95ea3627 562 */
4d8dd66c
ID
563 get_skb_desc(entry->skb)->frame_type = DUMP_FRAME_TXDONE;
564 rt2x00debug_dump_frame(rt2x00dev, entry->skb);
95ea3627
ID
565 ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb, tx_status);
566 entry->skb = NULL;
567}
568EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
569
570void rt2x00lib_rxdone(struct data_entry *entry, struct sk_buff *skb,
4150c572 571 struct rxdata_entry_desc *desc)
95ea3627
ID
572{
573 struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
574 struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
575 struct ieee80211_hw_mode *mode;
576 struct ieee80211_rate *rate;
61af43c5 577 struct ieee80211_hdr *hdr;
95ea3627
ID
578 unsigned int i;
579 int val = 0;
61af43c5 580 u16 fc;
95ea3627
ID
581
582 /*
583 * Update RX statistics.
584 */
585 mode = &rt2x00dev->hwmodes[rt2x00dev->curr_hwmode];
586 for (i = 0; i < mode->num_rates; i++) {
587 rate = &mode->rates[i];
588
589 /*
590 * When frame was received with an OFDM bitrate,
591 * the signal is the PLCP value. If it was received with
592 * a CCK bitrate the signal is the rate in 0.5kbit/s.
593 */
4150c572 594 if (!desc->ofdm)
95ea3627
ID
595 val = DEVICE_GET_RATE_FIELD(rate->val, RATE);
596 else
597 val = DEVICE_GET_RATE_FIELD(rate->val, PLCP);
598
4150c572 599 if (val == desc->signal) {
95ea3627
ID
600 val = rate->val;
601 break;
602 }
603 }
604
61af43c5 605 /*
7e56d38d 606 * Only update link status if this is a beacon frame carrying our bssid.
61af43c5 607 */
7e56d38d
ID
608 hdr = (struct ieee80211_hdr*)skb->data;
609 fc = le16_to_cpu(hdr->frame_control);
610 if (is_beacon(fc) && desc->my_bss)
611 rt2x00lib_update_link_stats(&rt2x00dev->link, desc->rssi);
61af43c5 612
ebcf26da 613 rt2x00dev->link.qual.rx_success++;
69f81a2c 614
95ea3627 615 rx_status->rate = val;
4150c572
JB
616 rx_status->signal =
617 rt2x00lib_calculate_link_signal(rt2x00dev, desc->rssi);
618 rx_status->ssi = desc->rssi;
619 rx_status->flag = desc->flags;
69f81a2c 620 rx_status->antenna = rt2x00dev->link.ant.active.rx;
95ea3627
ID
621
622 /*
4d8dd66c 623 * Send frame to mac80211 & debugfs
95ea3627 624 */
4d8dd66c
ID
625 get_skb_desc(skb)->frame_type = DUMP_FRAME_RXDONE;
626 rt2x00debug_dump_frame(rt2x00dev, skb);
95ea3627
ID
627 ieee80211_rx_irqsafe(rt2x00dev->hw, skb, rx_status);
628}
629EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
630
631/*
632 * TX descriptor initializer
633 */
634void rt2x00lib_write_tx_desc(struct rt2x00_dev *rt2x00dev,
08992f7f 635 struct sk_buff *skb,
95ea3627
ID
636 struct ieee80211_tx_control *control)
637{
4150c572 638 struct txdata_entry_desc desc;
08992f7f
ID
639 struct skb_desc *skbdesc = get_skb_desc(skb);
640 struct ieee80211_hdr *ieee80211hdr = skbdesc->data;
95ea3627
ID
641 int tx_rate;
642 int bitrate;
08992f7f 643 int length;
95ea3627
ID
644 int duration;
645 int residual;
646 u16 frame_control;
647 u16 seq_ctrl;
648
08992f7f 649 memset(&desc, 0, sizeof(desc));
95ea3627 650
08992f7f
ID
651 desc.cw_min = skbdesc->ring->tx_params.cw_min;
652 desc.cw_max = skbdesc->ring->tx_params.cw_max;
653 desc.aifs = skbdesc->ring->tx_params.aifs;
95ea3627
ID
654
655 /*
656 * Identify queue
657 */
658 if (control->queue < rt2x00dev->hw->queues)
659 desc.queue = control->queue;
660 else if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
661 control->queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
662 desc.queue = QUEUE_MGMT;
663 else
664 desc.queue = QUEUE_OTHER;
665
666 /*
667 * Read required fields from ieee80211 header.
668 */
669 frame_control = le16_to_cpu(ieee80211hdr->frame_control);
670 seq_ctrl = le16_to_cpu(ieee80211hdr->seq_ctrl);
671
672 tx_rate = control->tx_rate;
673
2700f8b0
MN
674 /*
675 * Check whether this frame is to be acked
676 */
677 if (!(control->flags & IEEE80211_TXCTL_NO_ACK))
678 __set_bit(ENTRY_TXD_ACK, &desc.flags);
679
95ea3627
ID
680 /*
681 * Check if this is a RTS/CTS frame
682 */
683 if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
684 __set_bit(ENTRY_TXD_BURST, &desc.flags);
2700f8b0 685 if (is_rts_frame(frame_control)) {
95ea3627 686 __set_bit(ENTRY_TXD_RTS_FRAME, &desc.flags);
2700f8b0
MN
687 __set_bit(ENTRY_TXD_ACK, &desc.flags);
688 } else
689 __clear_bit(ENTRY_TXD_ACK, &desc.flags);
95ea3627
ID
690 if (control->rts_cts_rate)
691 tx_rate = control->rts_cts_rate;
692 }
693
694 /*
695 * Check for OFDM
696 */
697 if (DEVICE_GET_RATE_FIELD(tx_rate, RATEMASK) & DEV_OFDM_RATEMASK)
698 __set_bit(ENTRY_TXD_OFDM_RATE, &desc.flags);
699
700 /*
701 * Check if more fragments are pending
702 */
703 if (ieee80211_get_morefrag(ieee80211hdr)) {
704 __set_bit(ENTRY_TXD_BURST, &desc.flags);
705 __set_bit(ENTRY_TXD_MORE_FRAG, &desc.flags);
706 }
707
708 /*
709 * Beacons and probe responses require the tsf timestamp
710 * to be inserted into the frame.
711 */
712 if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
713 is_probe_resp(frame_control))
714 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &desc.flags);
715
716 /*
717 * Determine with what IFS priority this frame should be send.
718 * Set ifs to IFS_SIFS when the this is not the first fragment,
719 * or this fragment came after RTS/CTS.
720 */
721 if ((seq_ctrl & IEEE80211_SCTL_FRAG) > 0 ||
722 test_bit(ENTRY_TXD_RTS_FRAME, &desc.flags))
723 desc.ifs = IFS_SIFS;
724 else
725 desc.ifs = IFS_BACKOFF;
726
727 /*
728 * PLCP setup
729 * Length calculation depends on OFDM/CCK rate.
730 */
731 desc.signal = DEVICE_GET_RATE_FIELD(tx_rate, PLCP);
732 desc.service = 0x04;
733
08992f7f 734 length = skbdesc->data_len + FCS_LEN;
95ea3627 735 if (test_bit(ENTRY_TXD_OFDM_RATE, &desc.flags)) {
08992f7f
ID
736 desc.length_high = (length >> 6) & 0x3f;
737 desc.length_low = length & 0x3f;
95ea3627
ID
738 } else {
739 bitrate = DEVICE_GET_RATE_FIELD(tx_rate, RATE);
740
741 /*
742 * Convert length to microseconds.
743 */
08992f7f
ID
744 residual = get_duration_res(length, bitrate);
745 duration = get_duration(length, bitrate);
95ea3627
ID
746
747 if (residual != 0) {
748 duration++;
749
750 /*
751 * Check if we need to set the Length Extension
752 */
db151787 753 if (bitrate == 110 && residual <= 30)
95ea3627
ID
754 desc.service |= 0x80;
755 }
756
757 desc.length_high = (duration >> 8) & 0xff;
758 desc.length_low = duration & 0xff;
759
760 /*
761 * When preamble is enabled we should set the
762 * preamble bit for the signal.
763 */
764 if (DEVICE_GET_RATE_FIELD(tx_rate, PREAMBLE))
765 desc.signal |= 0x08;
766 }
767
dd3193e1 768 rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, skb, &desc, control);
08992f7f
ID
769
770 /*
771 * Update ring entry.
772 */
773 skbdesc->entry->skb = skb;
774 memcpy(&skbdesc->entry->tx_status.control, control, sizeof(*control));
4d8dd66c
ID
775
776 /*
777 * The frame has been completely initialized and ready
778 * for sending to the device. The caller will push the
779 * frame to the device, but we are going to push the
780 * frame to debugfs here.
781 */
782 skbdesc->frame_type = DUMP_FRAME_TX;
783 rt2x00debug_dump_frame(rt2x00dev, skb);
95ea3627
ID
784}
785EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc);
786
787/*
788 * Driver initialization handlers.
789 */
790static void rt2x00lib_channel(struct ieee80211_channel *entry,
791 const int channel, const int tx_power,
792 const int value)
793{
794 entry->chan = channel;
795 if (channel <= 14)
796 entry->freq = 2407 + (5 * channel);
797 else
798 entry->freq = 5000 + (5 * channel);
799 entry->val = value;
800 entry->flag =
801 IEEE80211_CHAN_W_IBSS |
802 IEEE80211_CHAN_W_ACTIVE_SCAN |
803 IEEE80211_CHAN_W_SCAN;
804 entry->power_level = tx_power;
805 entry->antenna_max = 0xff;
806}
807
808static void rt2x00lib_rate(struct ieee80211_rate *entry,
809 const int rate, const int mask,
810 const int plcp, const int flags)
811{
812 entry->rate = rate;
813 entry->val =
814 DEVICE_SET_RATE_FIELD(rate, RATE) |
815 DEVICE_SET_RATE_FIELD(mask, RATEMASK) |
816 DEVICE_SET_RATE_FIELD(plcp, PLCP);
817 entry->flags = flags;
818 entry->val2 = entry->val;
819 if (entry->flags & IEEE80211_RATE_PREAMBLE2)
820 entry->val2 |= DEVICE_SET_RATE_FIELD(1, PREAMBLE);
821 entry->min_rssi_ack = 0;
822 entry->min_rssi_ack_delta = 0;
823}
824
825static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
826 struct hw_mode_spec *spec)
827{
828 struct ieee80211_hw *hw = rt2x00dev->hw;
829 struct ieee80211_hw_mode *hwmodes;
830 struct ieee80211_channel *channels;
831 struct ieee80211_rate *rates;
832 unsigned int i;
833 unsigned char tx_power;
834
835 hwmodes = kzalloc(sizeof(*hwmodes) * spec->num_modes, GFP_KERNEL);
836 if (!hwmodes)
837 goto exit;
838
839 channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
840 if (!channels)
841 goto exit_free_modes;
842
843 rates = kzalloc(sizeof(*rates) * spec->num_rates, GFP_KERNEL);
844 if (!rates)
845 goto exit_free_channels;
846
847 /*
848 * Initialize Rate list.
849 */
850 rt2x00lib_rate(&rates[0], 10, DEV_RATEMASK_1MB,
851 0x00, IEEE80211_RATE_CCK);
852 rt2x00lib_rate(&rates[1], 20, DEV_RATEMASK_2MB,
853 0x01, IEEE80211_RATE_CCK_2);
854 rt2x00lib_rate(&rates[2], 55, DEV_RATEMASK_5_5MB,
855 0x02, IEEE80211_RATE_CCK_2);
856 rt2x00lib_rate(&rates[3], 110, DEV_RATEMASK_11MB,
857 0x03, IEEE80211_RATE_CCK_2);
858
859 if (spec->num_rates > 4) {
860 rt2x00lib_rate(&rates[4], 60, DEV_RATEMASK_6MB,
861 0x0b, IEEE80211_RATE_OFDM);
862 rt2x00lib_rate(&rates[5], 90, DEV_RATEMASK_9MB,
863 0x0f, IEEE80211_RATE_OFDM);
864 rt2x00lib_rate(&rates[6], 120, DEV_RATEMASK_12MB,
865 0x0a, IEEE80211_RATE_OFDM);
866 rt2x00lib_rate(&rates[7], 180, DEV_RATEMASK_18MB,
867 0x0e, IEEE80211_RATE_OFDM);
868 rt2x00lib_rate(&rates[8], 240, DEV_RATEMASK_24MB,
869 0x09, IEEE80211_RATE_OFDM);
870 rt2x00lib_rate(&rates[9], 360, DEV_RATEMASK_36MB,
871 0x0d, IEEE80211_RATE_OFDM);
872 rt2x00lib_rate(&rates[10], 480, DEV_RATEMASK_48MB,
873 0x08, IEEE80211_RATE_OFDM);
874 rt2x00lib_rate(&rates[11], 540, DEV_RATEMASK_54MB,
875 0x0c, IEEE80211_RATE_OFDM);
876 }
877
878 /*
879 * Initialize Channel list.
880 */
881 for (i = 0; i < spec->num_channels; i++) {
882 if (spec->channels[i].channel <= 14)
883 tx_power = spec->tx_power_bg[i];
884 else if (spec->tx_power_a)
885 tx_power = spec->tx_power_a[i];
886 else
887 tx_power = spec->tx_power_default;
888
889 rt2x00lib_channel(&channels[i],
890 spec->channels[i].channel, tx_power, i);
891 }
892
893 /*
894 * Intitialize 802.11b
895 * Rates: CCK.
896 * Channels: OFDM.
897 */
898 if (spec->num_modes > HWMODE_B) {
899 hwmodes[HWMODE_B].mode = MODE_IEEE80211B;
900 hwmodes[HWMODE_B].num_channels = 14;
901 hwmodes[HWMODE_B].num_rates = 4;
902 hwmodes[HWMODE_B].channels = channels;
903 hwmodes[HWMODE_B].rates = rates;
904 }
905
906 /*
907 * Intitialize 802.11g
908 * Rates: CCK, OFDM.
909 * Channels: OFDM.
910 */
911 if (spec->num_modes > HWMODE_G) {
912 hwmodes[HWMODE_G].mode = MODE_IEEE80211G;
913 hwmodes[HWMODE_G].num_channels = 14;
914 hwmodes[HWMODE_G].num_rates = spec->num_rates;
915 hwmodes[HWMODE_G].channels = channels;
916 hwmodes[HWMODE_G].rates = rates;
917 }
918
919 /*
920 * Intitialize 802.11a
921 * Rates: OFDM.
922 * Channels: OFDM, UNII, HiperLAN2.
923 */
924 if (spec->num_modes > HWMODE_A) {
925 hwmodes[HWMODE_A].mode = MODE_IEEE80211A;
926 hwmodes[HWMODE_A].num_channels = spec->num_channels - 14;
927 hwmodes[HWMODE_A].num_rates = spec->num_rates - 4;
928 hwmodes[HWMODE_A].channels = &channels[14];
929 hwmodes[HWMODE_A].rates = &rates[4];
930 }
931
932 if (spec->num_modes > HWMODE_G &&
933 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_G]))
934 goto exit_free_rates;
935
936 if (spec->num_modes > HWMODE_B &&
937 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_B]))
938 goto exit_free_rates;
939
940 if (spec->num_modes > HWMODE_A &&
941 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_A]))
942 goto exit_free_rates;
943
944 rt2x00dev->hwmodes = hwmodes;
945
946 return 0;
947
948exit_free_rates:
949 kfree(rates);
950
951exit_free_channels:
952 kfree(channels);
953
954exit_free_modes:
955 kfree(hwmodes);
956
957exit:
958 ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
959 return -ENOMEM;
960}
961
962static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
963{
066cb637 964 if (test_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags))
95ea3627
ID
965 ieee80211_unregister_hw(rt2x00dev->hw);
966
967 if (likely(rt2x00dev->hwmodes)) {
968 kfree(rt2x00dev->hwmodes->channels);
969 kfree(rt2x00dev->hwmodes->rates);
970 kfree(rt2x00dev->hwmodes);
971 rt2x00dev->hwmodes = NULL;
972 }
973}
974
975static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
976{
977 struct hw_mode_spec *spec = &rt2x00dev->spec;
978 int status;
979
980 /*
981 * Initialize HW modes.
982 */
983 status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
984 if (status)
985 return status;
986
987 /*
988 * Register HW.
989 */
990 status = ieee80211_register_hw(rt2x00dev->hw);
991 if (status) {
992 rt2x00lib_remove_hw(rt2x00dev);
993 return status;
994 }
995
066cb637 996 __set_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags);
95ea3627
ID
997
998 return 0;
999}
1000
1001/*
1002 * Initialization/uninitialization handlers.
1003 */
1004static int rt2x00lib_alloc_entries(struct data_ring *ring,
1005 const u16 max_entries, const u16 data_size,
1006 const u16 desc_size)
1007{
1008 struct data_entry *entry;
1009 unsigned int i;
1010
1011 ring->stats.limit = max_entries;
1012 ring->data_size = data_size;
1013 ring->desc_size = desc_size;
1014
1015 /*
1016 * Allocate all ring entries.
1017 */
1018 entry = kzalloc(ring->stats.limit * sizeof(*entry), GFP_KERNEL);
1019 if (!entry)
1020 return -ENOMEM;
1021
1022 for (i = 0; i < ring->stats.limit; i++) {
1023 entry[i].flags = 0;
1024 entry[i].ring = ring;
1025 entry[i].skb = NULL;
04267104 1026 entry[i].entry_idx = i;
95ea3627
ID
1027 }
1028
1029 ring->entry = entry;
1030
1031 return 0;
1032}
1033
1034static int rt2x00lib_alloc_ring_entries(struct rt2x00_dev *rt2x00dev)
1035{
1036 struct data_ring *ring;
1037
1038 /*
1039 * Allocate the RX ring.
1040 */
1041 if (rt2x00lib_alloc_entries(rt2x00dev->rx, RX_ENTRIES, DATA_FRAME_SIZE,
1042 rt2x00dev->ops->rxd_size))
1043 return -ENOMEM;
1044
1045 /*
1046 * First allocate the TX rings.
1047 */
1048 txring_for_each(rt2x00dev, ring) {
1049 if (rt2x00lib_alloc_entries(ring, TX_ENTRIES, DATA_FRAME_SIZE,
1050 rt2x00dev->ops->txd_size))
1051 return -ENOMEM;
1052 }
1053
066cb637 1054 if (!test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
95ea3627
ID
1055 return 0;
1056
1057 /*
1058 * Allocate the BEACON ring.
1059 */
1060 if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[0], BEACON_ENTRIES,
1061 MGMT_FRAME_SIZE, rt2x00dev->ops->txd_size))
1062 return -ENOMEM;
1063
1064 /*
1065 * Allocate the Atim ring.
1066 */
1067 if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[1], ATIM_ENTRIES,
1068 DATA_FRAME_SIZE, rt2x00dev->ops->txd_size))
1069 return -ENOMEM;
1070
1071 return 0;
1072}
1073
1074static void rt2x00lib_free_ring_entries(struct rt2x00_dev *rt2x00dev)
1075{
1076 struct data_ring *ring;
1077
1078 ring_for_each(rt2x00dev, ring) {
1079 kfree(ring->entry);
1080 ring->entry = NULL;
1081 }
1082}
1083
e37ea213 1084static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
95ea3627
ID
1085{
1086 if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
1087 return;
1088
1089 /*
1090 * Unregister rfkill.
1091 */
1092 rt2x00rfkill_unregister(rt2x00dev);
1093
1094 /*
1095 * Allow the HW to uninitialize.
1096 */
1097 rt2x00dev->ops->lib->uninitialize(rt2x00dev);
1098
1099 /*
1100 * Free allocated ring entries.
1101 */
1102 rt2x00lib_free_ring_entries(rt2x00dev);
1103}
1104
e37ea213 1105static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
95ea3627
ID
1106{
1107 int status;
1108
1109 if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
1110 return 0;
1111
1112 /*
1113 * Allocate all ring entries.
1114 */
1115 status = rt2x00lib_alloc_ring_entries(rt2x00dev);
1116 if (status) {
1117 ERROR(rt2x00dev, "Ring entries allocation failed.\n");
1118 return status;
1119 }
1120
1121 /*
1122 * Initialize the device.
1123 */
1124 status = rt2x00dev->ops->lib->initialize(rt2x00dev);
1125 if (status)
1126 goto exit;
1127
1128 __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
1129
1130 /*
1131 * Register the rfkill handler.
1132 */
1133 status = rt2x00rfkill_register(rt2x00dev);
1134 if (status)
1135 goto exit_unitialize;
1136
1137 return 0;
1138
1139exit_unitialize:
1140 rt2x00lib_uninitialize(rt2x00dev);
1141
1142exit:
1143 rt2x00lib_free_ring_entries(rt2x00dev);
1144
1145 return status;
1146}
1147
e37ea213
ID
1148int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
1149{
1150 int retval;
1151
1152 if (test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1153 return 0;
1154
1155 /*
1156 * If this is the first interface which is added,
1157 * we should load the firmware now.
1158 */
1159 if (test_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags)) {
1160 retval = rt2x00lib_load_firmware(rt2x00dev);
1161 if (retval)
1162 return retval;
1163 }
1164
1165 /*
1166 * Initialize the device.
1167 */
1168 retval = rt2x00lib_initialize(rt2x00dev);
1169 if (retval)
1170 return retval;
1171
1172 /*
1173 * Enable radio.
1174 */
1175 retval = rt2x00lib_enable_radio(rt2x00dev);
1176 if (retval) {
1177 rt2x00lib_uninitialize(rt2x00dev);
1178 return retval;
1179 }
1180
1181 __set_bit(DEVICE_STARTED, &rt2x00dev->flags);
1182
1183 return 0;
1184}
1185
1186void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
1187{
1188 if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1189 return;
1190
1191 /*
1192 * Perhaps we can add something smarter here,
1193 * but for now just disabling the radio should do.
1194 */
1195 rt2x00lib_disable_radio(rt2x00dev);
1196
1197 __clear_bit(DEVICE_STARTED, &rt2x00dev->flags);
1198}
1199
95ea3627
ID
1200/*
1201 * driver allocation handlers.
1202 */
1203static int rt2x00lib_alloc_rings(struct rt2x00_dev *rt2x00dev)
1204{
1205 struct data_ring *ring;
04267104 1206 unsigned int index;
95ea3627
ID
1207
1208 /*
1209 * We need the following rings:
1210 * RX: 1
1211 * TX: hw->queues
1212 * Beacon: 1 (if required)
1213 * Atim: 1 (if required)
1214 */
1215 rt2x00dev->data_rings = 1 + rt2x00dev->hw->queues +
066cb637 1216 (2 * test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags));
95ea3627
ID
1217
1218 ring = kzalloc(rt2x00dev->data_rings * sizeof(*ring), GFP_KERNEL);
1219 if (!ring) {
1220 ERROR(rt2x00dev, "Ring allocation failed.\n");
1221 return -ENOMEM;
1222 }
1223
1224 /*
1225 * Initialize pointers
1226 */
1227 rt2x00dev->rx = ring;
1228 rt2x00dev->tx = &rt2x00dev->rx[1];
066cb637 1229 if (test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
95ea3627
ID
1230 rt2x00dev->bcn = &rt2x00dev->tx[rt2x00dev->hw->queues];
1231
1232 /*
1233 * Initialize ring parameters.
89539ebe
ID
1234 * RX: queue_idx = 0
1235 * TX: queue_idx = IEEE80211_TX_QUEUE_DATA0 + index
1236 * TX: cw_min: 2^5 = 32.
1237 * TX: cw_max: 2^10 = 1024.
95ea3627 1238 */
89539ebe
ID
1239 rt2x00dev->rx->rt2x00dev = rt2x00dev;
1240 rt2x00dev->rx->queue_idx = 0;
1241
1242 index = IEEE80211_TX_QUEUE_DATA0;
1243 txring_for_each(rt2x00dev, ring) {
95ea3627 1244 ring->rt2x00dev = rt2x00dev;
04267104 1245 ring->queue_idx = index++;
95ea3627
ID
1246 ring->tx_params.aifs = 2;
1247 ring->tx_params.cw_min = 5;
1248 ring->tx_params.cw_max = 10;
1249 }
1250
1251 return 0;
1252}
1253
1254static void rt2x00lib_free_rings(struct rt2x00_dev *rt2x00dev)
1255{
1256 kfree(rt2x00dev->rx);
1257 rt2x00dev->rx = NULL;
1258 rt2x00dev->tx = NULL;
1259 rt2x00dev->bcn = NULL;
1260}
1261
1262int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
1263{
1264 int retval = -ENOMEM;
1265
1266 /*
1267 * Let the driver probe the device to detect the capabilities.
1268 */
1269 retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
1270 if (retval) {
1271 ERROR(rt2x00dev, "Failed to allocate device.\n");
1272 goto exit;
1273 }
1274
1275 /*
1276 * Initialize configuration work.
1277 */
1278 INIT_WORK(&rt2x00dev->beacon_work, rt2x00lib_beacondone_scheduled);
4150c572 1279 INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
5c58ee51 1280 INIT_WORK(&rt2x00dev->config_work, rt2x00lib_configuration_scheduled);
95ea3627
ID
1281 INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
1282
1283 /*
1284 * Reset current working type.
1285 */
d28c2561 1286 rt2x00dev->interface.type = IEEE80211_IF_TYPE_INVALID;
95ea3627
ID
1287
1288 /*
1289 * Allocate ring array.
1290 */
1291 retval = rt2x00lib_alloc_rings(rt2x00dev);
1292 if (retval)
1293 goto exit;
1294
1295 /*
1296 * Initialize ieee80211 structure.
1297 */
1298 retval = rt2x00lib_probe_hw(rt2x00dev);
1299 if (retval) {
1300 ERROR(rt2x00dev, "Failed to initialize hw.\n");
1301 goto exit;
1302 }
1303
1304 /*
1305 * Allocatie rfkill.
1306 */
1307 retval = rt2x00rfkill_allocate(rt2x00dev);
1308 if (retval)
1309 goto exit;
1310
1311 /*
1312 * Open the debugfs entry.
1313 */
1314 rt2x00debug_register(rt2x00dev);
1315
066cb637
ID
1316 __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1317
95ea3627
ID
1318 return 0;
1319
1320exit:
1321 rt2x00lib_remove_dev(rt2x00dev);
1322
1323 return retval;
1324}
1325EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
1326
1327void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
1328{
066cb637
ID
1329 __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1330
95ea3627
ID
1331 /*
1332 * Disable radio.
1333 */
1334 rt2x00lib_disable_radio(rt2x00dev);
1335
1336 /*
1337 * Uninitialize device.
1338 */
1339 rt2x00lib_uninitialize(rt2x00dev);
1340
1341 /*
1342 * Close debugfs entry.
1343 */
1344 rt2x00debug_deregister(rt2x00dev);
1345
1346 /*
1347 * Free rfkill
1348 */
1349 rt2x00rfkill_free(rt2x00dev);
1350
1351 /*
1352 * Free ieee80211_hw memory.
1353 */
1354 rt2x00lib_remove_hw(rt2x00dev);
1355
1356 /*
1357 * Free firmware image.
1358 */
1359 rt2x00lib_free_firmware(rt2x00dev);
1360
1361 /*
1362 * Free ring structures.
1363 */
1364 rt2x00lib_free_rings(rt2x00dev);
1365}
1366EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
1367
1368/*
1369 * Device state handlers
1370 */
1371#ifdef CONFIG_PM
1372int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
1373{
1374 int retval;
1375
1376 NOTICE(rt2x00dev, "Going to sleep.\n");
066cb637
ID
1377 __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1378
1379 /*
1380 * Only continue if mac80211 has open interfaces.
1381 */
1382 if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1383 goto exit;
6d7f9877 1384 __set_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags);
95ea3627
ID
1385
1386 /*
1387 * Disable radio and unitialize all items
1388 * that must be recreated on resume.
1389 */
e37ea213 1390 rt2x00lib_stop(rt2x00dev);
95ea3627
ID
1391 rt2x00lib_uninitialize(rt2x00dev);
1392 rt2x00debug_deregister(rt2x00dev);
1393
066cb637 1394exit:
95ea3627
ID
1395 /*
1396 * Set device mode to sleep for power management.
1397 */
1398 retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
1399 if (retval)
1400 return retval;
1401
1402 return 0;
1403}
1404EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1405
1406int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1407{
1408 struct interface *intf = &rt2x00dev->interface;
1409 int retval;
1410
1411 NOTICE(rt2x00dev, "Waking up.\n");
95ea3627
ID
1412
1413 /*
1414 * Open the debugfs entry.
1415 */
1416 rt2x00debug_register(rt2x00dev);
1417
066cb637 1418 /*
6d7f9877 1419 * Only continue if mac80211 had open interfaces.
066cb637 1420 */
6d7f9877 1421 if (!__test_and_clear_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags))
066cb637
ID
1422 return 0;
1423
95ea3627
ID
1424 /*
1425 * Reinitialize device and all active interfaces.
1426 */
e37ea213 1427 retval = rt2x00lib_start(rt2x00dev);
95ea3627
ID
1428 if (retval)
1429 goto exit;
1430
1431 /*
1432 * Reconfigure device.
1433 */
066cb637
ID
1434 rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1);
1435 if (!rt2x00dev->hw->conf.radio_enabled)
1436 rt2x00lib_disable_radio(rt2x00dev);
95ea3627
ID
1437
1438 rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
1439 rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
1440 rt2x00lib_config_type(rt2x00dev, intf->type);
95ea3627 1441
e37ea213
ID
1442 /*
1443 * We are ready again to receive requests from mac80211.
1444 */
1445 __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1446
066cb637
ID
1447 /*
1448 * It is possible that during that mac80211 has attempted
1449 * to send frames while we were suspending or resuming.
1450 * In that case we have disabled the TX queue and should
1451 * now enable it again
1452 */
1453 ieee80211_start_queues(rt2x00dev->hw);
1454
95ea3627
ID
1455 /*
1456 * When in Master or Ad-hoc mode,
1457 * restart Beacon transmitting by faking a beacondone event.
1458 */
1459 if (intf->type == IEEE80211_IF_TYPE_AP ||
1460 intf->type == IEEE80211_IF_TYPE_IBSS)
1461 rt2x00lib_beacondone(rt2x00dev);
1462
95ea3627
ID
1463 return 0;
1464
1465exit:
1466 rt2x00lib_disable_radio(rt2x00dev);
1467 rt2x00lib_uninitialize(rt2x00dev);
1468 rt2x00debug_deregister(rt2x00dev);
1469
95ea3627
ID
1470 return retval;
1471}
1472EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1473#endif /* CONFIG_PM */
1474
1475/*
1476 * rt2x00lib module information.
1477 */
1478MODULE_AUTHOR(DRV_PROJECT);
1479MODULE_VERSION(DRV_VERSION);
1480MODULE_DESCRIPTION("rt2x00 library");
1481MODULE_LICENSE("GPL");