rtlwifi: Fix some warnings/bugs
[deliverable/linux.git] / drivers / net / wireless / rt2x00 / rt2x00dev.c
CommitLineData
95ea3627 1/*
7e613e16
ID
2 Copyright (C) 2010 Willow Garage <http://www.willowgarage.com>
3 Copyright (C) 2004 - 2010 Ivo van Doorn <IvDoorn@gmail.com>
95ea3627
ID
4 <http://rt2x00.serialmonkey.com>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the
18 Free Software Foundation, Inc.,
19 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
22/*
23 Module: rt2x00lib
24 Abstract: rt2x00 generic device routines.
25 */
26
95ea3627
ID
27#include <linux/kernel.h>
28#include <linux/module.h>
5a0e3ad6 29#include <linux/slab.h>
95ea3627
ID
30
31#include "rt2x00.h"
32#include "rt2x00lib.h"
33
95ea3627
ID
34/*
35 * Radio control handlers.
36 */
37int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
38{
39 int status;
40
41 /*
42 * Don't enable the radio twice.
43 * And check if the hardware button has been disabled.
44 */
4b9631a4 45 if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
95ea3627
ID
46 return 0;
47
837e7f24 48 /*
181d6902 49 * Initialize all data queues.
837e7f24 50 */
798b7adb 51 rt2x00queue_init_queues(rt2x00dev);
837e7f24 52
95ea3627
ID
53 /*
54 * Enable radio.
55 */
a2e1d52a
ID
56 status =
57 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_ON);
95ea3627
ID
58 if (status)
59 return status;
60
2b08da3f
ID
61 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_ON);
62
a2e1d52a 63 rt2x00leds_led_radio(rt2x00dev, true);
61c2b682 64 rt2x00led_led_activity(rt2x00dev, true);
a2e1d52a 65
0262ab0d 66 set_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags);
95ea3627
ID
67
68 /*
0b7fde54 69 * Enable queues.
95ea3627 70 */
0b7fde54 71 rt2x00queue_start_queues(rt2x00dev);
ea175ee2 72 rt2x00link_start_tuner(rt2x00dev);
95ea3627 73
c965c74b
ID
74 /*
75 * Start watchdog monitoring.
76 */
77 rt2x00link_start_watchdog(rt2x00dev);
78
95ea3627
ID
79 return 0;
80}
81
82void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
83{
0262ab0d 84 if (!test_and_clear_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
95ea3627
ID
85 return;
86
c965c74b
ID
87 /*
88 * Stop watchdog monitoring.
89 */
90 rt2x00link_stop_watchdog(rt2x00dev);
91
95ea3627 92 /*
0b7fde54 93 * Stop all queues
95ea3627 94 */
ea175ee2 95 rt2x00link_stop_tuner(rt2x00dev);
0b7fde54 96 rt2x00queue_stop_queues(rt2x00dev);
5be65609 97 rt2x00queue_flush_queues(rt2x00dev, true);
95ea3627
ID
98
99 /*
100 * Disable radio.
101 */
102 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
2b08da3f 103 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_OFF);
61c2b682 104 rt2x00led_led_activity(rt2x00dev, false);
a2e1d52a 105 rt2x00leds_led_radio(rt2x00dev, false);
95ea3627
ID
106}
107
6bb40dd1
ID
108static void rt2x00lib_intf_scheduled_iter(void *data, u8 *mac,
109 struct ieee80211_vif *vif)
5c58ee51 110{
6bb40dd1
ID
111 struct rt2x00_dev *rt2x00dev = data;
112 struct rt2x00_intf *intf = vif_to_intf(vif);
6bb40dd1 113
980dfcb9
ID
114 /*
115 * It is possible the radio was disabled while the work had been
116 * scheduled. If that happens we should return here immediately,
117 * note that in the spinlock protected area above the delayed_flags
118 * have been cleared correctly.
119 */
0262ab0d 120 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
980dfcb9
ID
121 return;
122
bfe6a15d 123 if (test_and_clear_bit(DELAYED_UPDATE_BEACON, &intf->delayed_flags))
69cf36a4 124 rt2x00queue_update_beacon(rt2x00dev, vif);
6bb40dd1 125}
5c58ee51 126
6bb40dd1
ID
127static void rt2x00lib_intf_scheduled(struct work_struct *work)
128{
129 struct rt2x00_dev *rt2x00dev =
130 container_of(work, struct rt2x00_dev, intf_work);
471b3efd
JB
131
132 /*
6bb40dd1
ID
133 * Iterate over each interface and perform the
134 * requested configurations.
471b3efd 135 */
6bb40dd1
ID
136 ieee80211_iterate_active_interfaces(rt2x00dev->hw,
137 rt2x00lib_intf_scheduled_iter,
138 rt2x00dev);
5c58ee51
ID
139}
140
95ea3627
ID
141/*
142 * Interrupt context handlers.
143 */
07896fe2
HS
144static void rt2x00lib_bc_buffer_iter(void *data, u8 *mac,
145 struct ieee80211_vif *vif)
146{
147 struct rt2x00_dev *rt2x00dev = data;
148 struct sk_buff *skb;
149
150 /*
151 * Only AP mode interfaces do broad- and multicast buffering
152 */
153 if (vif->type != NL80211_IFTYPE_AP)
154 return;
155
156 /*
157 * Send out buffered broad- and multicast frames
158 */
159 skb = ieee80211_get_buffered_bc(rt2x00dev->hw, vif);
160 while (skb) {
161 rt2x00mac_tx(rt2x00dev->hw, skb);
162 skb = ieee80211_get_buffered_bc(rt2x00dev->hw, vif);
163 }
164}
165
9f926fb5
HS
166static void rt2x00lib_beaconupdate_iter(void *data, u8 *mac,
167 struct ieee80211_vif *vif)
95ea3627 168{
4dee32f5 169 struct rt2x00_dev *rt2x00dev = data;
95ea3627 170
05c914fe 171 if (vif->type != NL80211_IFTYPE_AP &&
a07dbea2 172 vif->type != NL80211_IFTYPE_ADHOC &&
ce292a64
ID
173 vif->type != NL80211_IFTYPE_MESH_POINT &&
174 vif->type != NL80211_IFTYPE_WDS)
95ea3627
ID
175 return;
176
8d59c4e9
HS
177 /*
178 * Update the beacon without locking. This is safe on PCI devices
179 * as they only update the beacon periodically here. This should
180 * never be called for USB devices.
181 */
182 WARN_ON(rt2x00_is_usb(rt2x00dev));
183 rt2x00queue_update_beacon_locked(rt2x00dev, vif);
95ea3627
ID
184}
185
186void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
187{
0262ab0d 188 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
95ea3627
ID
189 return;
190
07896fe2 191 /* send buffered bc/mc frames out for every bssid */
8d59c4e9
HS
192 ieee80211_iterate_active_interfaces_atomic(rt2x00dev->hw,
193 rt2x00lib_bc_buffer_iter,
194 rt2x00dev);
9f926fb5
HS
195 /*
196 * Devices with pre tbtt interrupt don't need to update the beacon
197 * here as they will fetch the next beacon directly prior to
198 * transmission.
199 */
200 if (test_bit(DRIVER_SUPPORT_PRE_TBTT_INTERRUPT, &rt2x00dev->flags))
201 return;
07896fe2
HS
202
203 /* fetch next beacon */
8d59c4e9
HS
204 ieee80211_iterate_active_interfaces_atomic(rt2x00dev->hw,
205 rt2x00lib_beaconupdate_iter,
206 rt2x00dev);
95ea3627
ID
207}
208EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
209
9f926fb5
HS
210void rt2x00lib_pretbtt(struct rt2x00_dev *rt2x00dev)
211{
212 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
213 return;
214
215 /* fetch next beacon */
8d59c4e9
HS
216 ieee80211_iterate_active_interfaces_atomic(rt2x00dev->hw,
217 rt2x00lib_beaconupdate_iter,
218 rt2x00dev);
9f926fb5
HS
219}
220EXPORT_SYMBOL_GPL(rt2x00lib_pretbtt);
221
64e7d723
ID
222void rt2x00lib_dmastart(struct queue_entry *entry)
223{
224 set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
225 rt2x00queue_index_inc(entry->queue, Q_INDEX);
226}
227EXPORT_SYMBOL_GPL(rt2x00lib_dmastart);
228
652a9dd2
ID
229void rt2x00lib_dmadone(struct queue_entry *entry)
230{
dba5dc1a 231 set_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags);
a13c8f31 232 clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
652a9dd2
ID
233 rt2x00queue_index_inc(entry->queue, Q_INDEX_DMA_DONE);
234}
235EXPORT_SYMBOL_GPL(rt2x00lib_dmadone);
236
181d6902
ID
237void rt2x00lib_txdone(struct queue_entry *entry,
238 struct txdone_entry_desc *txdesc)
95ea3627 239{
181d6902 240 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
e039fa4a 241 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
e6a9854b 242 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
f8eaec65 243 unsigned int header_length, i;
92ed48e5 244 u8 rate_idx, rate_flags, retry_rates;
7351c6bd 245 u8 skbdesc_flags = skbdesc->flags;
2e27cff8 246 bool success;
d74f5ba4 247
e513a0b6
GW
248 /*
249 * Unmap the skb.
250 */
fa69560f 251 rt2x00queue_unmap_skb(entry);
e513a0b6
GW
252
253 /*
254 * Remove the extra tx headroom from the skb.
255 */
256 skb_pull(entry->skb, rt2x00dev->ops->extra_tx_headroom);
257
258 /*
259 * Signal that the TX descriptor is no longer in the skb.
260 */
261 skbdesc->flags &= ~SKBDESC_DESC_IN_SKB;
262
f8eaec65
RJH
263 /*
264 * Determine the length of 802.11 header.
265 */
266 header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
267
9f166171
ID
268 /*
269 * Remove L2 padding which was added during
270 */
271 if (test_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags))
daee6c09 272 rt2x00queue_remove_l2pad(entry->skb, header_length);
9f166171 273
2bb057d0
ID
274 /*
275 * If the IV/EIV data was stripped from the frame before it was
276 * passed to the hardware, we should now reinsert it again because
77c2061d 277 * mac80211 will expect the same data to be present it the
2bb057d0
ID
278 * frame as it was passed to us.
279 */
280 if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags))
9f166171 281 rt2x00crypto_tx_insert_iv(entry->skb, header_length);
2bb057d0 282
e039fa4a
JB
283 /*
284 * Send frame to debugfs immediately, after this call is completed
285 * we are going to overwrite the skb->cb array.
286 */
287 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TXDONE, entry->skb);
95ea3627
ID
288
289 /*
2e27cff8 290 * Determine if the frame has been successfully transmitted.
95ea3627 291 */
2e27cff8 292 success =
ce4c45e0 293 test_bit(TXDONE_SUCCESS, &txdesc->flags) ||
fd6dcb88 294 test_bit(TXDONE_UNKNOWN, &txdesc->flags);
2e27cff8
ID
295
296 /*
297 * Update TX statistics.
298 */
299 rt2x00dev->link.qual.tx_success += success;
300 rt2x00dev->link.qual.tx_failed += !success;
95ea3627 301
e6a9854b
JB
302 rate_idx = skbdesc->tx_rate_idx;
303 rate_flags = skbdesc->tx_rate_flags;
92ed48e5
BP
304 retry_rates = test_bit(TXDONE_FALLBACK, &txdesc->flags) ?
305 (txdesc->retry + 1) : 1;
e6a9854b 306
181d6902
ID
307 /*
308 * Initialize TX status
309 */
e039fa4a
JB
310 memset(&tx_info->status, 0, sizeof(tx_info->status));
311 tx_info->status.ack_signal = 0;
92ed48e5
BP
312
313 /*
314 * Frame was send with retries, hardware tried
315 * different rates to send out the frame, at each
3d2bc103
HS
316 * retry it lowered the rate 1 step except when the
317 * lowest rate was used.
92ed48e5
BP
318 */
319 for (i = 0; i < retry_rates && i < IEEE80211_TX_MAX_RATES; i++) {
320 tx_info->status.rates[i].idx = rate_idx - i;
321 tx_info->status.rates[i].flags = rate_flags;
3d2bc103
HS
322
323 if (rate_idx - i == 0) {
324 /*
325 * The lowest rate (index 0) was used until the
326 * number of max retries was reached.
327 */
328 tx_info->status.rates[i].count = retry_rates - i;
329 i++;
330 break;
331 }
92ed48e5
BP
332 tx_info->status.rates[i].count = 1;
333 }
2e27cff8 334 if (i < (IEEE80211_TX_MAX_RATES - 1))
92ed48e5 335 tx_info->status.rates[i].idx = -1; /* terminate */
181d6902 336
e039fa4a 337 if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK)) {
2e27cff8 338 if (success)
e039fa4a 339 tx_info->flags |= IEEE80211_TX_STAT_ACK;
2e27cff8 340 else
181d6902 341 rt2x00dev->low_level_stats.dot11ACKFailureCount++;
95ea3627
ID
342 }
343
1df90809
HS
344 /*
345 * Every single frame has it's own tx status, hence report
346 * every frame as ampdu of size 1.
347 *
348 * TODO: if we can find out how many frames were aggregated
349 * by the hw we could provide the real ampdu_len to mac80211
350 * which would allow the rc algorithm to better decide on
351 * which rates are suitable.
352 */
353 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
354 tx_info->flags |= IEEE80211_TX_STAT_AMPDU;
355 tx_info->status.ampdu_len = 1;
356 tx_info->status.ampdu_ack_len = success ? 1 : 0;
357 }
358
e6a9854b 359 if (rate_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
2e27cff8 360 if (success)
181d6902 361 rt2x00dev->low_level_stats.dot11RTSSuccessCount++;
2e27cff8 362 else
181d6902 363 rt2x00dev->low_level_stats.dot11RTSFailureCount++;
95ea3627
ID
364 }
365
366 /*
7351c6bd
JB
367 * Only send the status report to mac80211 when it's a frame
368 * that originated in mac80211. If this was a extra frame coming
369 * through a mac80211 library call (RTS/CTS) then we should not
370 * send the status report back.
95ea3627 371 */
20ed3166
JS
372 if (!(skbdesc_flags & SKBDESC_NOT_MAC80211)) {
373 if (test_bit(DRIVER_REQUIRE_TASKLET_CONTEXT, &rt2x00dev->flags))
374 ieee80211_tx_status(rt2x00dev->hw, entry->skb);
375 else
376 ieee80211_tx_status_ni(rt2x00dev->hw, entry->skb);
377 } else
78e256c9 378 dev_kfree_skb_any(entry->skb);
d74f5ba4
ID
379
380 /*
381 * Make this entry available for reuse.
382 */
95ea3627 383 entry->skb = NULL;
d74f5ba4
ID
384 entry->flags = 0;
385
798b7adb 386 rt2x00dev->ops->lib->clear_entry(entry);
d74f5ba4 387
d74f5ba4
ID
388 rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
389
390 /*
391 * If the data queue was below the threshold before the txdone
392 * handler we must make sure the packet queue in the mac80211 stack
393 * is reenabled when the txdone handler has finished.
394 */
395 if (!rt2x00queue_threshold(entry->queue))
0b7fde54 396 rt2x00queue_unpause_queue(entry->queue);
95ea3627
ID
397}
398EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
3392bece
ID
399
400void rt2x00lib_txdone_noinfo(struct queue_entry *entry, u32 status)
401{
402 struct txdone_entry_desc txdesc;
403
404 txdesc.flags = 0;
405 __set_bit(status, &txdesc.flags);
406 txdesc.retry = 0;
407
408 rt2x00lib_txdone(entry, &txdesc);
409}
410EXPORT_SYMBOL_GPL(rt2x00lib_txdone_noinfo);
95ea3627 411
35f00cfc
ID
412static int rt2x00lib_rxdone_read_signal(struct rt2x00_dev *rt2x00dev,
413 struct rxdone_entry_desc *rxdesc)
414{
415 struct ieee80211_supported_band *sband;
416 const struct rt2x00_rate *rate;
417 unsigned int i;
3590eea4
ID
418 int signal = rxdesc->signal;
419 int type = (rxdesc->dev_flags & RXDONE_SIGNAL_MASK);
420
421 switch (rxdesc->rate_mode) {
422 case RATE_MODE_CCK:
423 case RATE_MODE_OFDM:
424 /*
425 * For non-HT rates the MCS value needs to contain the
426 * actually used rate modulation (CCK or OFDM).
427 */
428 if (rxdesc->dev_flags & RXDONE_SIGNAL_MCS)
429 signal = RATE_MCS(rxdesc->rate_mode, signal);
430
431 sband = &rt2x00dev->bands[rt2x00dev->curr_band];
432 for (i = 0; i < sband->n_bitrates; i++) {
433 rate = rt2x00_get_rate(sband->bitrates[i].hw_value);
434 if (((type == RXDONE_SIGNAL_PLCP) &&
435 (rate->plcp == signal)) ||
436 ((type == RXDONE_SIGNAL_BITRATE) &&
437 (rate->bitrate == signal)) ||
438 ((type == RXDONE_SIGNAL_MCS) &&
439 (rate->mcs == signal))) {
440 return i;
441 }
35f00cfc 442 }
3590eea4
ID
443 break;
444 case RATE_MODE_HT_MIX:
445 case RATE_MODE_HT_GREENFIELD:
446 if (signal >= 0 && signal <= 76)
447 return signal;
448 break;
449 default:
450 break;
35f00cfc
ID
451 }
452
453 WARNING(rt2x00dev, "Frame received with unrecognized signal, "
3590eea4
ID
454 "mode=0x%.4x, signal=0x%.4x, type=%d.\n",
455 rxdesc->rate_mode, signal, type);
35f00cfc
ID
456 return 0;
457}
458
fa69560f 459void rt2x00lib_rxdone(struct queue_entry *entry)
95ea3627 460{
fa69560f 461 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
c4da0048
GW
462 struct rxdone_entry_desc rxdesc;
463 struct sk_buff *skb;
e5ef5bad 464 struct ieee80211_rx_status *rx_status;
2bb057d0 465 unsigned int header_length;
35f00cfc 466 int rate_idx;
7e613e16 467
070192dd
ID
468 if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) ||
469 !test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
470 goto submit_entry;
471
7e613e16
ID
472 if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags))
473 goto submit_entry;
474
c4da0048
GW
475 /*
476 * Allocate a new sk_buffer. If no new buffer available, drop the
477 * received frame and reuse the existing buffer.
478 */
fa69560f 479 skb = rt2x00queue_alloc_rxskb(entry);
c4da0048 480 if (!skb)
1550c8ef 481 goto submit_entry;
c4da0048
GW
482
483 /*
484 * Unmap the skb.
485 */
fa69560f 486 rt2x00queue_unmap_skb(entry);
c4da0048
GW
487
488 /*
489 * Extract the RXD details.
490 */
491 memset(&rxdesc, 0, sizeof(rxdesc));
492 rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
95ea3627 493
239c249d
GW
494 /*
495 * The data behind the ieee80211 header must be
a9f853dd 496 * aligned on a 4 byte boundary.
239c249d 497 */
2bb057d0 498 header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
239c249d 499
2bb057d0
ID
500 /*
501 * Hardware might have stripped the IV/EIV/ICV data,
502 * in that case it is possible that the data was
3ad2f3fb 503 * provided separately (through hardware descriptor)
2bb057d0
ID
504 * in which case we should reinsert the data into the frame.
505 */
74415edb 506 if ((rxdesc.dev_flags & RXDONE_CRYPTO_IV) &&
9f166171 507 (rxdesc.flags & RX_FLAG_IV_STRIPPED))
daee6c09 508 rt2x00crypto_rx_insert_iv(entry->skb, header_length,
9f166171 509 &rxdesc);
b7340833
GW
510 else if (header_length &&
511 (rxdesc.size > header_length) &&
512 (rxdesc.dev_flags & RXDONE_L2PAD))
daee6c09 513 rt2x00queue_remove_l2pad(entry->skb, header_length);
9f166171 514 else
daee6c09 515 rt2x00queue_align_payload(entry->skb, header_length);
239c249d 516
1398d458
AB
517 /* Trim buffer to correct size */
518 skb_trim(entry->skb, rxdesc.size);
519
95ea3627 520 /*
3590eea4 521 * Translate the signal to the correct bitrate index.
95ea3627 522 */
3590eea4
ID
523 rate_idx = rt2x00lib_rxdone_read_signal(rt2x00dev, &rxdesc);
524 if (rxdesc.rate_mode == RATE_MODE_HT_MIX ||
525 rxdesc.rate_mode == RATE_MODE_HT_GREENFIELD)
35f00cfc 526 rxdesc.flags |= RX_FLAG_HT;
866a0503 527
61af43c5 528 /*
84e3196f 529 * Update extra components
61af43c5 530 */
84e3196f
ID
531 rt2x00link_update_stats(rt2x00dev, entry->skb, &rxdesc);
532 rt2x00debug_update_crypto(rt2x00dev, &rxdesc);
e5ef5bad 533 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_RXDONE, entry->skb);
69f81a2c 534
e5ef5bad
ID
535 /*
536 * Initialize RX status information, and send frame
537 * to mac80211.
538 */
539 rx_status = IEEE80211_SKB_RXCB(entry->skb);
ae73e58e 540 rx_status->mactime = rxdesc.timestamp;
e5ef5bad
ID
541 rx_status->band = rt2x00dev->curr_band;
542 rx_status->freq = rt2x00dev->curr_freq;
35f00cfc 543 rx_status->rate_idx = rate_idx;
c4da0048
GW
544 rx_status->signal = rxdesc.rssi;
545 rx_status->flag = rxdesc.flags;
69f81a2c 546 rx_status->antenna = rt2x00dev->link.ant.active.rx;
95ea3627 547
7e613e16 548 ieee80211_rx_ni(rt2x00dev->hw, entry->skb);
c4da0048
GW
549
550 /*
551 * Replace the skb with the freshly allocated one.
552 */
553 entry->skb = skb;
d74f5ba4 554
7e613e16 555submit_entry:
070192dd 556 entry->flags = 0;
7e613e16 557 rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
070192dd 558 if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) &&
64e7d723 559 test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
070192dd 560 rt2x00dev->ops->lib->clear_entry(entry);
95ea3627
ID
561}
562EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
563
95ea3627
ID
564/*
565 * Driver initialization handlers.
566 */
70e2fed4
ID
567const struct rt2x00_rate rt2x00_supported_rates[12] = {
568 {
3d8606a6 569 .flags = DEV_RATE_CCK,
70e2fed4 570 .bitrate = 10,
aa776721 571 .ratemask = BIT(0),
70e2fed4 572 .plcp = 0x00,
35f00cfc 573 .mcs = RATE_MCS(RATE_MODE_CCK, 0),
70e2fed4
ID
574 },
575 {
3d8606a6 576 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
70e2fed4 577 .bitrate = 20,
aa776721 578 .ratemask = BIT(1),
70e2fed4 579 .plcp = 0x01,
35f00cfc 580 .mcs = RATE_MCS(RATE_MODE_CCK, 1),
70e2fed4
ID
581 },
582 {
3d8606a6 583 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
70e2fed4 584 .bitrate = 55,
aa776721 585 .ratemask = BIT(2),
70e2fed4 586 .plcp = 0x02,
35f00cfc 587 .mcs = RATE_MCS(RATE_MODE_CCK, 2),
70e2fed4
ID
588 },
589 {
3d8606a6 590 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
70e2fed4 591 .bitrate = 110,
aa776721 592 .ratemask = BIT(3),
70e2fed4 593 .plcp = 0x03,
35f00cfc 594 .mcs = RATE_MCS(RATE_MODE_CCK, 3),
70e2fed4
ID
595 },
596 {
3d8606a6 597 .flags = DEV_RATE_OFDM,
70e2fed4 598 .bitrate = 60,
aa776721 599 .ratemask = BIT(4),
70e2fed4 600 .plcp = 0x0b,
35f00cfc 601 .mcs = RATE_MCS(RATE_MODE_OFDM, 0),
70e2fed4
ID
602 },
603 {
604 .flags = DEV_RATE_OFDM,
605 .bitrate = 90,
aa776721 606 .ratemask = BIT(5),
70e2fed4 607 .plcp = 0x0f,
35f00cfc 608 .mcs = RATE_MCS(RATE_MODE_OFDM, 1),
70e2fed4
ID
609 },
610 {
3d8606a6 611 .flags = DEV_RATE_OFDM,
70e2fed4 612 .bitrate = 120,
aa776721 613 .ratemask = BIT(6),
70e2fed4 614 .plcp = 0x0a,
35f00cfc 615 .mcs = RATE_MCS(RATE_MODE_OFDM, 2),
70e2fed4
ID
616 },
617 {
618 .flags = DEV_RATE_OFDM,
619 .bitrate = 180,
aa776721 620 .ratemask = BIT(7),
70e2fed4 621 .plcp = 0x0e,
35f00cfc 622 .mcs = RATE_MCS(RATE_MODE_OFDM, 3),
70e2fed4
ID
623 },
624 {
3d8606a6 625 .flags = DEV_RATE_OFDM,
70e2fed4 626 .bitrate = 240,
aa776721 627 .ratemask = BIT(8),
70e2fed4 628 .plcp = 0x09,
35f00cfc 629 .mcs = RATE_MCS(RATE_MODE_OFDM, 4),
70e2fed4
ID
630 },
631 {
632 .flags = DEV_RATE_OFDM,
633 .bitrate = 360,
aa776721 634 .ratemask = BIT(9),
70e2fed4 635 .plcp = 0x0d,
35f00cfc 636 .mcs = RATE_MCS(RATE_MODE_OFDM, 5),
70e2fed4
ID
637 },
638 {
639 .flags = DEV_RATE_OFDM,
640 .bitrate = 480,
aa776721 641 .ratemask = BIT(10),
70e2fed4 642 .plcp = 0x08,
35f00cfc 643 .mcs = RATE_MCS(RATE_MODE_OFDM, 6),
70e2fed4
ID
644 },
645 {
646 .flags = DEV_RATE_OFDM,
647 .bitrate = 540,
aa776721 648 .ratemask = BIT(11),
70e2fed4 649 .plcp = 0x0c,
35f00cfc 650 .mcs = RATE_MCS(RATE_MODE_OFDM, 7),
70e2fed4
ID
651 },
652};
653
95ea3627
ID
654static void rt2x00lib_channel(struct ieee80211_channel *entry,
655 const int channel, const int tx_power,
656 const int value)
657{
59eb21a6
BR
658 /* XXX: this assumption about the band is wrong for 802.11j */
659 entry->band = channel <= 14 ? IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
660 entry->center_freq = ieee80211_channel_to_frequency(channel,
661 entry->band);
8318d78a
JB
662 entry->hw_value = value;
663 entry->max_power = tx_power;
664 entry->max_antenna_gain = 0xff;
95ea3627
ID
665}
666
667static void rt2x00lib_rate(struct ieee80211_rate *entry,
70e2fed4 668 const u16 index, const struct rt2x00_rate *rate)
95ea3627 669{
70e2fed4
ID
670 entry->flags = 0;
671 entry->bitrate = rate->bitrate;
c2361bae 672 entry->hw_value = index;
3ea96463 673 entry->hw_value_short = index;
70e2fed4 674
3ea96463 675 if (rate->flags & DEV_RATE_SHORT_PREAMBLE)
70e2fed4 676 entry->flags |= IEEE80211_RATE_SHORT_PREAMBLE;
95ea3627
ID
677}
678
679static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
680 struct hw_mode_spec *spec)
681{
682 struct ieee80211_hw *hw = rt2x00dev->hw;
95ea3627
ID
683 struct ieee80211_channel *channels;
684 struct ieee80211_rate *rates;
31562e80 685 unsigned int num_rates;
95ea3627 686 unsigned int i;
95ea3627 687
31562e80
ID
688 num_rates = 0;
689 if (spec->supported_rates & SUPPORT_RATE_CCK)
690 num_rates += 4;
691 if (spec->supported_rates & SUPPORT_RATE_OFDM)
692 num_rates += 8;
95ea3627
ID
693
694 channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
695 if (!channels)
8318d78a 696 return -ENOMEM;
95ea3627 697
31562e80 698 rates = kzalloc(sizeof(*rates) * num_rates, GFP_KERNEL);
95ea3627
ID
699 if (!rates)
700 goto exit_free_channels;
701
702 /*
703 * Initialize Rate list.
704 */
31562e80 705 for (i = 0; i < num_rates; i++)
8f5fa7f0 706 rt2x00lib_rate(&rates[i], i, rt2x00_get_rate(i));
95ea3627
ID
707
708 /*
709 * Initialize Channel list.
710 */
711 for (i = 0; i < spec->num_channels; i++) {
95ea3627 712 rt2x00lib_channel(&channels[i],
8c5e7a5f 713 spec->channels[i].channel,
8d1331b3 714 spec->channels_info[i].max_power, i);
95ea3627
ID
715 }
716
717 /*
31562e80 718 * Intitialize 802.11b, 802.11g
95ea3627 719 * Rates: CCK, OFDM.
8318d78a 720 * Channels: 2.4 GHz
95ea3627 721 */
47ac2683 722 if (spec->supported_bands & SUPPORT_BAND_2GHZ) {
31562e80
ID
723 rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_channels = 14;
724 rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_bitrates = num_rates;
725 rt2x00dev->bands[IEEE80211_BAND_2GHZ].channels = channels;
726 rt2x00dev->bands[IEEE80211_BAND_2GHZ].bitrates = rates;
727 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
728 &rt2x00dev->bands[IEEE80211_BAND_2GHZ];
35f00cfc
ID
729 memcpy(&rt2x00dev->bands[IEEE80211_BAND_2GHZ].ht_cap,
730 &spec->ht, sizeof(spec->ht));
95ea3627
ID
731 }
732
733 /*
734 * Intitialize 802.11a
735 * Rates: OFDM.
736 * Channels: OFDM, UNII, HiperLAN2.
737 */
47ac2683 738 if (spec->supported_bands & SUPPORT_BAND_5GHZ) {
31562e80
ID
739 rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_channels =
740 spec->num_channels - 14;
741 rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_bitrates =
742 num_rates - 4;
743 rt2x00dev->bands[IEEE80211_BAND_5GHZ].channels = &channels[14];
744 rt2x00dev->bands[IEEE80211_BAND_5GHZ].bitrates = &rates[4];
745 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
746 &rt2x00dev->bands[IEEE80211_BAND_5GHZ];
35f00cfc
ID
747 memcpy(&rt2x00dev->bands[IEEE80211_BAND_5GHZ].ht_cap,
748 &spec->ht, sizeof(spec->ht));
95ea3627
ID
749 }
750
95ea3627
ID
751 return 0;
752
8318d78a 753 exit_free_channels:
95ea3627 754 kfree(channels);
95ea3627
ID
755 ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
756 return -ENOMEM;
757}
758
759static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
760{
0262ab0d 761 if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
95ea3627
ID
762 ieee80211_unregister_hw(rt2x00dev->hw);
763
8318d78a
JB
764 if (likely(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ])) {
765 kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->channels);
766 kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->bitrates);
767 rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
768 rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
95ea3627 769 }
8c5e7a5f
ID
770
771 kfree(rt2x00dev->spec.channels_info);
95ea3627
ID
772}
773
774static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
775{
776 struct hw_mode_spec *spec = &rt2x00dev->spec;
777 int status;
778
0262ab0d
ID
779 if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
780 return 0;
781
95ea3627
ID
782 /*
783 * Initialize HW modes.
784 */
785 status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
786 if (status)
787 return status;
788
61448f88
GW
789 /*
790 * Initialize HW fields.
791 */
792 rt2x00dev->hw->queues = rt2x00dev->ops->tx_queues;
793
e6218cc4
GW
794 /*
795 * Initialize extra TX headroom required.
796 */
7a4a77b7
GW
797 rt2x00dev->hw->extra_tx_headroom =
798 max_t(unsigned int, IEEE80211_TX_STATUS_HEADROOM,
799 rt2x00dev->ops->extra_tx_headroom);
800
801 /*
802 * Take TX headroom required for alignment into account.
803 */
804 if (test_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags))
805 rt2x00dev->hw->extra_tx_headroom += RT2X00_L2PAD_SIZE;
806 else if (test_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags))
807 rt2x00dev->hw->extra_tx_headroom += RT2X00_ALIGN_SIZE;
e6218cc4 808
96c3da7d
HS
809 /*
810 * Allocate tx status FIFO for driver use.
811 */
5235189c 812 if (test_bit(DRIVER_REQUIRE_TXSTATUS_FIFO, &rt2x00dev->flags)) {
96c3da7d
HS
813 /*
814 * Allocate txstatus fifo and tasklet, we use a size of 512
815 * for the kfifo which is big enough to store 512/4=128 tx
816 * status reports. In the worst case (tx status for all tx
817 * queues gets reported before we've got a chance to handle
818 * them) 24*4=384 tx status reports need to be cached.
819 */
820 status = kfifo_alloc(&rt2x00dev->txstatus_fifo, 512,
821 GFP_KERNEL);
822 if (status)
823 return status;
96c3da7d
HS
824 }
825
c5c65761
HS
826 /*
827 * Initialize tasklets if used by the driver. Tasklets are
828 * disabled until the interrupts are turned on. The driver
829 * has to handle that.
830 */
831#define RT2X00_TASKLET_INIT(taskletname) \
832 if (rt2x00dev->ops->lib->taskletname) { \
833 tasklet_init(&rt2x00dev->taskletname, \
834 rt2x00dev->ops->lib->taskletname, \
835 (unsigned long)rt2x00dev); \
836 tasklet_disable(&rt2x00dev->taskletname); \
837 }
838
c8e15a1e 839 RT2X00_TASKLET_INIT(txstatus_tasklet);
c5c65761
HS
840 RT2X00_TASKLET_INIT(pretbtt_tasklet);
841 RT2X00_TASKLET_INIT(tbtt_tasklet);
842 RT2X00_TASKLET_INIT(rxdone_tasklet);
843 RT2X00_TASKLET_INIT(autowake_tasklet);
844
845#undef RT2X00_TASKLET_INIT
846
95ea3627
ID
847 /*
848 * Register HW.
849 */
850 status = ieee80211_register_hw(rt2x00dev->hw);
f05faa31 851 if (status)
95ea3627 852 return status;
95ea3627 853
0262ab0d 854 set_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags);
95ea3627
ID
855
856 return 0;
857}
858
859/*
860 * Initialization/uninitialization handlers.
861 */
e37ea213 862static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
95ea3627 863{
0262ab0d 864 if (!test_and_clear_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
95ea3627
ID
865 return;
866
867 /*
1682fe6d 868 * Unregister extra components.
95ea3627
ID
869 */
870 rt2x00rfkill_unregister(rt2x00dev);
871
872 /*
873 * Allow the HW to uninitialize.
874 */
875 rt2x00dev->ops->lib->uninitialize(rt2x00dev);
876
877 /*
181d6902 878 * Free allocated queue entries.
95ea3627 879 */
181d6902 880 rt2x00queue_uninitialize(rt2x00dev);
95ea3627
ID
881}
882
e37ea213 883static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
95ea3627
ID
884{
885 int status;
886
0262ab0d 887 if (test_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
95ea3627
ID
888 return 0;
889
890 /*
181d6902 891 * Allocate all queue entries.
95ea3627 892 */
181d6902
ID
893 status = rt2x00queue_initialize(rt2x00dev);
894 if (status)
95ea3627 895 return status;
95ea3627
ID
896
897 /*
898 * Initialize the device.
899 */
900 status = rt2x00dev->ops->lib->initialize(rt2x00dev);
ed499983
ID
901 if (status) {
902 rt2x00queue_uninitialize(rt2x00dev);
903 return status;
904 }
95ea3627 905
0262ab0d 906 set_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags);
95ea3627
ID
907
908 /*
1682fe6d 909 * Register the extra components.
95ea3627 910 */
1682fe6d 911 rt2x00rfkill_register(rt2x00dev);
95ea3627
ID
912
913 return 0;
95ea3627
ID
914}
915
e37ea213
ID
916int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
917{
918 int retval;
919
0262ab0d 920 if (test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
e37ea213
ID
921 return 0;
922
923 /*
924 * If this is the first interface which is added,
925 * we should load the firmware now.
926 */
9404ef34
ID
927 retval = rt2x00lib_load_firmware(rt2x00dev);
928 if (retval)
929 return retval;
e37ea213
ID
930
931 /*
932 * Initialize the device.
933 */
934 retval = rt2x00lib_initialize(rt2x00dev);
935 if (retval)
936 return retval;
937
6bb40dd1
ID
938 rt2x00dev->intf_ap_count = 0;
939 rt2x00dev->intf_sta_count = 0;
940 rt2x00dev->intf_associated = 0;
941
bdfa500b
ID
942 /* Enable the radio */
943 retval = rt2x00lib_enable_radio(rt2x00dev);
1f0280cb 944 if (retval)
bdfa500b 945 return retval;
bdfa500b 946
0262ab0d 947 set_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags);
e37ea213
ID
948
949 return 0;
950}
951
952void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
953{
0262ab0d 954 if (!test_and_clear_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
e37ea213
ID
955 return;
956
957 /*
958 * Perhaps we can add something smarter here,
959 * but for now just disabling the radio should do.
960 */
961 rt2x00lib_disable_radio(rt2x00dev);
962
6bb40dd1
ID
963 rt2x00dev->intf_ap_count = 0;
964 rt2x00dev->intf_sta_count = 0;
965 rt2x00dev->intf_associated = 0;
e37ea213
ID
966}
967
95ea3627
ID
968/*
969 * driver allocation handlers.
970 */
95ea3627
ID
971int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
972{
973 int retval = -ENOMEM;
974
c5c65761 975 spin_lock_init(&rt2x00dev->irqmask_lock);
8ff48a8b
ID
976 mutex_init(&rt2x00dev->csr_mutex);
977
66f84d65
SC
978 set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
979
6bb40dd1
ID
980 /*
981 * Make room for rt2x00_intf inside the per-interface
982 * structure ieee80211_vif.
983 */
984 rt2x00dev->hw->vif_data_size = sizeof(struct rt2x00_intf);
985
3514a441
ID
986 /*
987 * Determine which operating modes are supported, all modes
988 * which require beaconing, depend on the availability of
989 * beacon entries.
990 */
991 rt2x00dev->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
992 if (rt2x00dev->ops->bcn->entry_num > 0)
993 rt2x00dev->hw->wiphy->interface_modes |=
994 BIT(NL80211_IFTYPE_ADHOC) |
a07dbea2 995 BIT(NL80211_IFTYPE_AP) |
ce292a64
ID
996 BIT(NL80211_IFTYPE_MESH_POINT) |
997 BIT(NL80211_IFTYPE_WDS);
f59ac048 998
9acd56d3 999 /*
0439f536 1000 * Initialize work.
9acd56d3 1001 */
0439f536
ID
1002 rt2x00dev->workqueue =
1003 alloc_ordered_workqueue(wiphy_name(rt2x00dev->hw->wiphy), 0);
1004 if (!rt2x00dev->workqueue) {
1005 retval = -ENOMEM;
1006 goto exit;
1007 }
1008
9acd56d3
SB
1009 INIT_WORK(&rt2x00dev->intf_work, rt2x00lib_intf_scheduled);
1010
95ea3627
ID
1011 /*
1012 * Let the driver probe the device to detect the capabilities.
1013 */
1014 retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
1015 if (retval) {
1016 ERROR(rt2x00dev, "Failed to allocate device.\n");
1017 goto exit;
1018 }
1019
95ea3627 1020 /*
181d6902 1021 * Allocate queue array.
95ea3627 1022 */
181d6902 1023 retval = rt2x00queue_allocate(rt2x00dev);
95ea3627
ID
1024 if (retval)
1025 goto exit;
1026
1027 /*
1028 * Initialize ieee80211 structure.
1029 */
1030 retval = rt2x00lib_probe_hw(rt2x00dev);
1031 if (retval) {
1032 ERROR(rt2x00dev, "Failed to initialize hw.\n");
1033 goto exit;
1034 }
1035
a9450b70 1036 /*
1682fe6d 1037 * Register extra components.
a9450b70 1038 */
84e3196f 1039 rt2x00link_register(rt2x00dev);
a9450b70 1040 rt2x00leds_register(rt2x00dev);
95ea3627
ID
1041 rt2x00debug_register(rt2x00dev);
1042
1043 return 0;
1044
1045exit:
1046 rt2x00lib_remove_dev(rt2x00dev);
1047
1048 return retval;
1049}
1050EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
1051
1052void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
1053{
0262ab0d 1054 clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
066cb637 1055
95ea3627
ID
1056 /*
1057 * Disable radio.
1058 */
1059 rt2x00lib_disable_radio(rt2x00dev);
1060
d8cc8926
PR
1061 /*
1062 * Stop all work.
1063 */
d8cc8926 1064 cancel_work_sync(&rt2x00dev->intf_work);
7e613e16
ID
1065 cancel_work_sync(&rt2x00dev->rxdone_work);
1066 cancel_work_sync(&rt2x00dev->txdone_work);
0439f536 1067 destroy_workqueue(rt2x00dev->workqueue);
d8cc8926 1068
96c3da7d
HS
1069 /*
1070 * Free the tx status fifo.
1071 */
1072 kfifo_free(&rt2x00dev->txstatus_fifo);
1073
1074 /*
1075 * Kill the tx status tasklet.
1076 */
1077 tasklet_kill(&rt2x00dev->txstatus_tasklet);
e1f4e808
ID
1078 tasklet_kill(&rt2x00dev->pretbtt_tasklet);
1079 tasklet_kill(&rt2x00dev->tbtt_tasklet);
1080 tasklet_kill(&rt2x00dev->rxdone_tasklet);
1081 tasklet_kill(&rt2x00dev->autowake_tasklet);
96c3da7d 1082
95ea3627
ID
1083 /*
1084 * Uninitialize device.
1085 */
1086 rt2x00lib_uninitialize(rt2x00dev);
1087
1088 /*
1682fe6d 1089 * Free extra components
95ea3627
ID
1090 */
1091 rt2x00debug_deregister(rt2x00dev);
a9450b70
ID
1092 rt2x00leds_unregister(rt2x00dev);
1093
95ea3627
ID
1094 /*
1095 * Free ieee80211_hw memory.
1096 */
1097 rt2x00lib_remove_hw(rt2x00dev);
1098
1099 /*
1100 * Free firmware image.
1101 */
1102 rt2x00lib_free_firmware(rt2x00dev);
1103
1104 /*
181d6902 1105 * Free queue structures.
95ea3627 1106 */
181d6902 1107 rt2x00queue_free(rt2x00dev);
95ea3627
ID
1108}
1109EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
1110
1111/*
1112 * Device state handlers
1113 */
1114#ifdef CONFIG_PM
1115int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
1116{
95ea3627 1117 NOTICE(rt2x00dev, "Going to sleep.\n");
066cb637
ID
1118
1119 /*
07126127 1120 * Prevent mac80211 from accessing driver while suspended.
066cb637 1121 */
07126127
ID
1122 if (!test_and_clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
1123 return 0;
95ea3627
ID
1124
1125 /*
07126127 1126 * Cleanup as much as possible.
95ea3627 1127 */
95ea3627 1128 rt2x00lib_uninitialize(rt2x00dev);
1682fe6d
ID
1129
1130 /*
1131 * Suspend/disable extra components.
1132 */
a9450b70 1133 rt2x00leds_suspend(rt2x00dev);
95ea3627
ID
1134 rt2x00debug_deregister(rt2x00dev);
1135
1136 /*
9896322a
ID
1137 * Set device mode to sleep for power management,
1138 * on some hardware this call seems to consistently fail.
1139 * From the specifications it is hard to tell why it fails,
1140 * and if this is a "bad thing".
1141 * Overall it is safe to just ignore the failure and
1142 * continue suspending. The only downside is that the
1143 * device will not be in optimal power save mode, but with
1144 * the radio and the other components already disabled the
1145 * device is as good as disabled.
95ea3627 1146 */
07126127 1147 if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP))
9896322a
ID
1148 WARNING(rt2x00dev, "Device failed to enter sleep state, "
1149 "continue suspending.\n");
95ea3627
ID
1150
1151 return 0;
1152}
1153EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1154
1155int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1156{
95ea3627 1157 NOTICE(rt2x00dev, "Waking up.\n");
95ea3627
ID
1158
1159 /*
1682fe6d 1160 * Restore/enable extra components.
95ea3627
ID
1161 */
1162 rt2x00debug_register(rt2x00dev);
a9450b70 1163 rt2x00leds_resume(rt2x00dev);
95ea3627 1164
e37ea213
ID
1165 /*
1166 * We are ready again to receive requests from mac80211.
1167 */
0262ab0d 1168 set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
e37ea213 1169
95ea3627 1170 return 0;
95ea3627
ID
1171}
1172EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1173#endif /* CONFIG_PM */
1174
1175/*
1176 * rt2x00lib module information.
1177 */
1178MODULE_AUTHOR(DRV_PROJECT);
1179MODULE_VERSION(DRV_VERSION);
1180MODULE_DESCRIPTION("rt2x00 library");
1181MODULE_LICENSE("GPL");
This page took 0.632931 seconds and 5 git commands to generate.