2 Copyright (C) 2010 Willow Garage <http://www.willowgarage.com>
3 Copyright (C) 2004 - 2010 Ivo van Doorn <IvDoorn@gmail.com>
4 <http://rt2x00.serialmonkey.com>
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.
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.
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.
24 Abstract: rt2x00 generic device routines.
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
32 #include "rt2x00lib.h"
35 * Radio control handlers.
37 int rt2x00lib_enable_radio(struct rt2x00_dev
*rt2x00dev
)
42 * Don't enable the radio twice.
43 * And check if the hardware button has been disabled.
45 if (test_bit(DEVICE_STATE_ENABLED_RADIO
, &rt2x00dev
->flags
))
49 * Initialize all data queues.
51 rt2x00queue_init_queues(rt2x00dev
);
57 rt2x00dev
->ops
->lib
->set_device_state(rt2x00dev
, STATE_RADIO_ON
);
61 rt2x00dev
->ops
->lib
->set_device_state(rt2x00dev
, STATE_RADIO_IRQ_ON
);
63 rt2x00leds_led_radio(rt2x00dev
, true);
64 rt2x00led_led_activity(rt2x00dev
, true);
66 set_bit(DEVICE_STATE_ENABLED_RADIO
, &rt2x00dev
->flags
);
71 rt2x00queue_start_queues(rt2x00dev
);
72 rt2x00link_start_tuner(rt2x00dev
);
75 * Start watchdog monitoring.
77 rt2x00link_start_watchdog(rt2x00dev
);
82 void rt2x00lib_disable_radio(struct rt2x00_dev
*rt2x00dev
)
84 if (!test_and_clear_bit(DEVICE_STATE_ENABLED_RADIO
, &rt2x00dev
->flags
))
88 * Stop watchdog monitoring.
90 rt2x00link_stop_watchdog(rt2x00dev
);
95 rt2x00link_stop_tuner(rt2x00dev
);
96 rt2x00queue_stop_queues(rt2x00dev
);
97 rt2x00queue_flush_queues(rt2x00dev
, true);
102 rt2x00dev
->ops
->lib
->set_device_state(rt2x00dev
, STATE_RADIO_OFF
);
103 rt2x00dev
->ops
->lib
->set_device_state(rt2x00dev
, STATE_RADIO_IRQ_OFF
);
104 rt2x00led_led_activity(rt2x00dev
, false);
105 rt2x00leds_led_radio(rt2x00dev
, false);
108 static void rt2x00lib_intf_scheduled_iter(void *data
, u8
*mac
,
109 struct ieee80211_vif
*vif
)
111 struct rt2x00_dev
*rt2x00dev
= data
;
112 struct rt2x00_intf
*intf
= vif_to_intf(vif
);
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.
120 if (!test_bit(DEVICE_STATE_ENABLED_RADIO
, &rt2x00dev
->flags
))
123 if (test_and_clear_bit(DELAYED_UPDATE_BEACON
, &intf
->delayed_flags
))
124 rt2x00queue_update_beacon(rt2x00dev
, vif
, true);
127 static void rt2x00lib_intf_scheduled(struct work_struct
*work
)
129 struct rt2x00_dev
*rt2x00dev
=
130 container_of(work
, struct rt2x00_dev
, intf_work
);
133 * Iterate over each interface and perform the
134 * requested configurations.
136 ieee80211_iterate_active_interfaces(rt2x00dev
->hw
,
137 rt2x00lib_intf_scheduled_iter
,
142 * Interrupt context handlers.
144 static void rt2x00lib_bc_buffer_iter(void *data
, u8
*mac
,
145 struct ieee80211_vif
*vif
)
147 struct rt2x00_dev
*rt2x00dev
= data
;
151 * Only AP mode interfaces do broad- and multicast buffering
153 if (vif
->type
!= NL80211_IFTYPE_AP
)
157 * Send out buffered broad- and multicast frames
159 skb
= ieee80211_get_buffered_bc(rt2x00dev
->hw
, vif
);
161 rt2x00mac_tx(rt2x00dev
->hw
, skb
);
162 skb
= ieee80211_get_buffered_bc(rt2x00dev
->hw
, vif
);
166 static void rt2x00lib_beaconupdate_iter(void *data
, u8
*mac
,
167 struct ieee80211_vif
*vif
)
169 struct rt2x00_dev
*rt2x00dev
= data
;
171 if (vif
->type
!= NL80211_IFTYPE_AP
&&
172 vif
->type
!= NL80211_IFTYPE_ADHOC
&&
173 vif
->type
!= NL80211_IFTYPE_MESH_POINT
&&
174 vif
->type
!= NL80211_IFTYPE_WDS
)
177 rt2x00queue_update_beacon(rt2x00dev
, vif
, true);
180 void rt2x00lib_beacondone(struct rt2x00_dev
*rt2x00dev
)
182 if (!test_bit(DEVICE_STATE_ENABLED_RADIO
, &rt2x00dev
->flags
))
185 /* send buffered bc/mc frames out for every bssid */
186 ieee80211_iterate_active_interfaces(rt2x00dev
->hw
,
187 rt2x00lib_bc_buffer_iter
,
190 * Devices with pre tbtt interrupt don't need to update the beacon
191 * here as they will fetch the next beacon directly prior to
194 if (test_bit(DRIVER_SUPPORT_PRE_TBTT_INTERRUPT
, &rt2x00dev
->flags
))
197 /* fetch next beacon */
198 ieee80211_iterate_active_interfaces(rt2x00dev
->hw
,
199 rt2x00lib_beaconupdate_iter
,
202 EXPORT_SYMBOL_GPL(rt2x00lib_beacondone
);
204 void rt2x00lib_pretbtt(struct rt2x00_dev
*rt2x00dev
)
206 if (!test_bit(DEVICE_STATE_ENABLED_RADIO
, &rt2x00dev
->flags
))
209 /* fetch next beacon */
210 ieee80211_iterate_active_interfaces(rt2x00dev
->hw
,
211 rt2x00lib_beaconupdate_iter
,
214 EXPORT_SYMBOL_GPL(rt2x00lib_pretbtt
);
216 void rt2x00lib_dmastart(struct queue_entry
*entry
)
218 set_bit(ENTRY_OWNER_DEVICE_DATA
, &entry
->flags
);
219 rt2x00queue_index_inc(entry
->queue
, Q_INDEX
);
221 EXPORT_SYMBOL_GPL(rt2x00lib_dmastart
);
223 void rt2x00lib_dmadone(struct queue_entry
*entry
)
225 set_bit(ENTRY_DATA_STATUS_PENDING
, &entry
->flags
);
226 clear_bit(ENTRY_OWNER_DEVICE_DATA
, &entry
->flags
);
227 rt2x00queue_index_inc(entry
->queue
, Q_INDEX_DMA_DONE
);
229 EXPORT_SYMBOL_GPL(rt2x00lib_dmadone
);
231 void rt2x00lib_txdone(struct queue_entry
*entry
,
232 struct txdone_entry_desc
*txdesc
)
234 struct rt2x00_dev
*rt2x00dev
= entry
->queue
->rt2x00dev
;
235 struct ieee80211_tx_info
*tx_info
= IEEE80211_SKB_CB(entry
->skb
);
236 struct skb_frame_desc
*skbdesc
= get_skb_frame_desc(entry
->skb
);
237 unsigned int header_length
, i
;
238 u8 rate_idx
, rate_flags
, retry_rates
;
239 u8 skbdesc_flags
= skbdesc
->flags
;
245 rt2x00queue_unmap_skb(entry
);
248 * Remove the extra tx headroom from the skb.
250 skb_pull(entry
->skb
, rt2x00dev
->ops
->extra_tx_headroom
);
253 * Signal that the TX descriptor is no longer in the skb.
255 skbdesc
->flags
&= ~SKBDESC_DESC_IN_SKB
;
258 * Determine the length of 802.11 header.
260 header_length
= ieee80211_get_hdrlen_from_skb(entry
->skb
);
263 * Remove L2 padding which was added during
265 if (test_bit(DRIVER_REQUIRE_L2PAD
, &rt2x00dev
->flags
))
266 rt2x00queue_remove_l2pad(entry
->skb
, header_length
);
269 * If the IV/EIV data was stripped from the frame before it was
270 * passed to the hardware, we should now reinsert it again because
271 * mac80211 will expect the same data to be present it the
272 * frame as it was passed to us.
274 if (test_bit(CONFIG_SUPPORT_HW_CRYPTO
, &rt2x00dev
->flags
))
275 rt2x00crypto_tx_insert_iv(entry
->skb
, header_length
);
278 * Send frame to debugfs immediately, after this call is completed
279 * we are going to overwrite the skb->cb array.
281 rt2x00debug_dump_frame(rt2x00dev
, DUMP_FRAME_TXDONE
, entry
->skb
);
284 * Determine if the frame has been successfully transmitted.
287 test_bit(TXDONE_SUCCESS
, &txdesc
->flags
) ||
288 test_bit(TXDONE_UNKNOWN
, &txdesc
->flags
);
291 * Update TX statistics.
293 rt2x00dev
->link
.qual
.tx_success
+= success
;
294 rt2x00dev
->link
.qual
.tx_failed
+= !success
;
296 rate_idx
= skbdesc
->tx_rate_idx
;
297 rate_flags
= skbdesc
->tx_rate_flags
;
298 retry_rates
= test_bit(TXDONE_FALLBACK
, &txdesc
->flags
) ?
299 (txdesc
->retry
+ 1) : 1;
302 * Initialize TX status
304 memset(&tx_info
->status
, 0, sizeof(tx_info
->status
));
305 tx_info
->status
.ack_signal
= 0;
308 * Frame was send with retries, hardware tried
309 * different rates to send out the frame, at each
310 * retry it lowered the rate 1 step except when the
311 * lowest rate was used.
313 for (i
= 0; i
< retry_rates
&& i
< IEEE80211_TX_MAX_RATES
; i
++) {
314 tx_info
->status
.rates
[i
].idx
= rate_idx
- i
;
315 tx_info
->status
.rates
[i
].flags
= rate_flags
;
317 if (rate_idx
- i
== 0) {
319 * The lowest rate (index 0) was used until the
320 * number of max retries was reached.
322 tx_info
->status
.rates
[i
].count
= retry_rates
- i
;
326 tx_info
->status
.rates
[i
].count
= 1;
328 if (i
< (IEEE80211_TX_MAX_RATES
- 1))
329 tx_info
->status
.rates
[i
].idx
= -1; /* terminate */
331 if (!(tx_info
->flags
& IEEE80211_TX_CTL_NO_ACK
)) {
333 tx_info
->flags
|= IEEE80211_TX_STAT_ACK
;
335 rt2x00dev
->low_level_stats
.dot11ACKFailureCount
++;
339 * Every single frame has it's own tx status, hence report
340 * every frame as ampdu of size 1.
342 * TODO: if we can find out how many frames were aggregated
343 * by the hw we could provide the real ampdu_len to mac80211
344 * which would allow the rc algorithm to better decide on
345 * which rates are suitable.
347 if (tx_info
->flags
& IEEE80211_TX_CTL_AMPDU
) {
348 tx_info
->flags
|= IEEE80211_TX_STAT_AMPDU
;
349 tx_info
->status
.ampdu_len
= 1;
350 tx_info
->status
.ampdu_ack_len
= success
? 1 : 0;
353 if (rate_flags
& IEEE80211_TX_RC_USE_RTS_CTS
) {
355 rt2x00dev
->low_level_stats
.dot11RTSSuccessCount
++;
357 rt2x00dev
->low_level_stats
.dot11RTSFailureCount
++;
361 * Only send the status report to mac80211 when it's a frame
362 * that originated in mac80211. If this was a extra frame coming
363 * through a mac80211 library call (RTS/CTS) then we should not
364 * send the status report back.
366 if (!(skbdesc_flags
& SKBDESC_NOT_MAC80211
)) {
367 if (test_bit(DRIVER_REQUIRE_TASKLET_CONTEXT
, &rt2x00dev
->flags
))
368 ieee80211_tx_status(rt2x00dev
->hw
, entry
->skb
);
370 ieee80211_tx_status_ni(rt2x00dev
->hw
, entry
->skb
);
372 dev_kfree_skb_any(entry
->skb
);
375 * Make this entry available for reuse.
380 rt2x00dev
->ops
->lib
->clear_entry(entry
);
382 rt2x00queue_index_inc(entry
->queue
, Q_INDEX_DONE
);
385 * If the data queue was below the threshold before the txdone
386 * handler we must make sure the packet queue in the mac80211 stack
387 * is reenabled when the txdone handler has finished.
389 if (!rt2x00queue_threshold(entry
->queue
))
390 rt2x00queue_unpause_queue(entry
->queue
);
392 EXPORT_SYMBOL_GPL(rt2x00lib_txdone
);
394 void rt2x00lib_txdone_noinfo(struct queue_entry
*entry
, u32 status
)
396 struct txdone_entry_desc txdesc
;
399 __set_bit(status
, &txdesc
.flags
);
402 rt2x00lib_txdone(entry
, &txdesc
);
404 EXPORT_SYMBOL_GPL(rt2x00lib_txdone_noinfo
);
406 static int rt2x00lib_rxdone_read_signal(struct rt2x00_dev
*rt2x00dev
,
407 struct rxdone_entry_desc
*rxdesc
)
409 struct ieee80211_supported_band
*sband
;
410 const struct rt2x00_rate
*rate
;
412 int signal
= rxdesc
->signal
;
413 int type
= (rxdesc
->dev_flags
& RXDONE_SIGNAL_MASK
);
415 switch (rxdesc
->rate_mode
) {
419 * For non-HT rates the MCS value needs to contain the
420 * actually used rate modulation (CCK or OFDM).
422 if (rxdesc
->dev_flags
& RXDONE_SIGNAL_MCS
)
423 signal
= RATE_MCS(rxdesc
->rate_mode
, signal
);
425 sband
= &rt2x00dev
->bands
[rt2x00dev
->curr_band
];
426 for (i
= 0; i
< sband
->n_bitrates
; i
++) {
427 rate
= rt2x00_get_rate(sband
->bitrates
[i
].hw_value
);
428 if (((type
== RXDONE_SIGNAL_PLCP
) &&
429 (rate
->plcp
== signal
)) ||
430 ((type
== RXDONE_SIGNAL_BITRATE
) &&
431 (rate
->bitrate
== signal
)) ||
432 ((type
== RXDONE_SIGNAL_MCS
) &&
433 (rate
->mcs
== signal
))) {
438 case RATE_MODE_HT_MIX
:
439 case RATE_MODE_HT_GREENFIELD
:
440 if (signal
>= 0 && signal
<= 76)
447 WARNING(rt2x00dev
, "Frame received with unrecognized signal, "
448 "mode=0x%.4x, signal=0x%.4x, type=%d.\n",
449 rxdesc
->rate_mode
, signal
, type
);
453 void rt2x00lib_rxdone(struct queue_entry
*entry
)
455 struct rt2x00_dev
*rt2x00dev
= entry
->queue
->rt2x00dev
;
456 struct rxdone_entry_desc rxdesc
;
458 struct ieee80211_rx_status
*rx_status
;
459 unsigned int header_length
;
462 if (!test_bit(DEVICE_STATE_PRESENT
, &rt2x00dev
->flags
) ||
463 !test_bit(DEVICE_STATE_ENABLED_RADIO
, &rt2x00dev
->flags
))
466 if (test_bit(ENTRY_DATA_IO_FAILED
, &entry
->flags
))
470 * Allocate a new sk_buffer. If no new buffer available, drop the
471 * received frame and reuse the existing buffer.
473 skb
= rt2x00queue_alloc_rxskb(entry
);
480 rt2x00queue_unmap_skb(entry
);
483 * Extract the RXD details.
485 memset(&rxdesc
, 0, sizeof(rxdesc
));
486 rt2x00dev
->ops
->lib
->fill_rxdone(entry
, &rxdesc
);
489 * The data behind the ieee80211 header must be
490 * aligned on a 4 byte boundary.
492 header_length
= ieee80211_get_hdrlen_from_skb(entry
->skb
);
495 * Hardware might have stripped the IV/EIV/ICV data,
496 * in that case it is possible that the data was
497 * provided separately (through hardware descriptor)
498 * in which case we should reinsert the data into the frame.
500 if ((rxdesc
.dev_flags
& RXDONE_CRYPTO_IV
) &&
501 (rxdesc
.flags
& RX_FLAG_IV_STRIPPED
))
502 rt2x00crypto_rx_insert_iv(entry
->skb
, header_length
,
504 else if (header_length
&&
505 (rxdesc
.size
> header_length
) &&
506 (rxdesc
.dev_flags
& RXDONE_L2PAD
))
507 rt2x00queue_remove_l2pad(entry
->skb
, header_length
);
509 rt2x00queue_align_payload(entry
->skb
, header_length
);
511 /* Trim buffer to correct size */
512 skb_trim(entry
->skb
, rxdesc
.size
);
515 * Translate the signal to the correct bitrate index.
517 rate_idx
= rt2x00lib_rxdone_read_signal(rt2x00dev
, &rxdesc
);
518 if (rxdesc
.rate_mode
== RATE_MODE_HT_MIX
||
519 rxdesc
.rate_mode
== RATE_MODE_HT_GREENFIELD
)
520 rxdesc
.flags
|= RX_FLAG_HT
;
523 * Update extra components
525 rt2x00link_update_stats(rt2x00dev
, entry
->skb
, &rxdesc
);
526 rt2x00debug_update_crypto(rt2x00dev
, &rxdesc
);
527 rt2x00debug_dump_frame(rt2x00dev
, DUMP_FRAME_RXDONE
, entry
->skb
);
530 * Initialize RX status information, and send frame
533 rx_status
= IEEE80211_SKB_RXCB(entry
->skb
);
534 rx_status
->mactime
= rxdesc
.timestamp
;
535 rx_status
->band
= rt2x00dev
->curr_band
;
536 rx_status
->freq
= rt2x00dev
->curr_freq
;
537 rx_status
->rate_idx
= rate_idx
;
538 rx_status
->signal
= rxdesc
.rssi
;
539 rx_status
->flag
= rxdesc
.flags
;
540 rx_status
->antenna
= rt2x00dev
->link
.ant
.active
.rx
;
542 ieee80211_rx_ni(rt2x00dev
->hw
, entry
->skb
);
545 * Replace the skb with the freshly allocated one.
551 rt2x00queue_index_inc(entry
->queue
, Q_INDEX_DONE
);
552 if (test_bit(DEVICE_STATE_PRESENT
, &rt2x00dev
->flags
) &&
553 test_bit(DEVICE_STATE_ENABLED_RADIO
, &rt2x00dev
->flags
))
554 rt2x00dev
->ops
->lib
->clear_entry(entry
);
556 EXPORT_SYMBOL_GPL(rt2x00lib_rxdone
);
559 * Driver initialization handlers.
561 const struct rt2x00_rate rt2x00_supported_rates
[12] = {
563 .flags
= DEV_RATE_CCK
,
567 .mcs
= RATE_MCS(RATE_MODE_CCK
, 0),
570 .flags
= DEV_RATE_CCK
| DEV_RATE_SHORT_PREAMBLE
,
574 .mcs
= RATE_MCS(RATE_MODE_CCK
, 1),
577 .flags
= DEV_RATE_CCK
| DEV_RATE_SHORT_PREAMBLE
,
581 .mcs
= RATE_MCS(RATE_MODE_CCK
, 2),
584 .flags
= DEV_RATE_CCK
| DEV_RATE_SHORT_PREAMBLE
,
588 .mcs
= RATE_MCS(RATE_MODE_CCK
, 3),
591 .flags
= DEV_RATE_OFDM
,
595 .mcs
= RATE_MCS(RATE_MODE_OFDM
, 0),
598 .flags
= DEV_RATE_OFDM
,
602 .mcs
= RATE_MCS(RATE_MODE_OFDM
, 1),
605 .flags
= DEV_RATE_OFDM
,
609 .mcs
= RATE_MCS(RATE_MODE_OFDM
, 2),
612 .flags
= DEV_RATE_OFDM
,
616 .mcs
= RATE_MCS(RATE_MODE_OFDM
, 3),
619 .flags
= DEV_RATE_OFDM
,
623 .mcs
= RATE_MCS(RATE_MODE_OFDM
, 4),
626 .flags
= DEV_RATE_OFDM
,
630 .mcs
= RATE_MCS(RATE_MODE_OFDM
, 5),
633 .flags
= DEV_RATE_OFDM
,
637 .mcs
= RATE_MCS(RATE_MODE_OFDM
, 6),
640 .flags
= DEV_RATE_OFDM
,
644 .mcs
= RATE_MCS(RATE_MODE_OFDM
, 7),
648 static void rt2x00lib_channel(struct ieee80211_channel
*entry
,
649 const int channel
, const int tx_power
,
652 entry
->center_freq
= ieee80211_channel_to_frequency(channel
);
653 entry
->hw_value
= value
;
654 entry
->max_power
= tx_power
;
655 entry
->max_antenna_gain
= 0xff;
658 static void rt2x00lib_rate(struct ieee80211_rate
*entry
,
659 const u16 index
, const struct rt2x00_rate
*rate
)
662 entry
->bitrate
= rate
->bitrate
;
663 entry
->hw_value
= index
;
664 entry
->hw_value_short
= index
;
666 if (rate
->flags
& DEV_RATE_SHORT_PREAMBLE
)
667 entry
->flags
|= IEEE80211_RATE_SHORT_PREAMBLE
;
670 static int rt2x00lib_probe_hw_modes(struct rt2x00_dev
*rt2x00dev
,
671 struct hw_mode_spec
*spec
)
673 struct ieee80211_hw
*hw
= rt2x00dev
->hw
;
674 struct ieee80211_channel
*channels
;
675 struct ieee80211_rate
*rates
;
676 unsigned int num_rates
;
680 if (spec
->supported_rates
& SUPPORT_RATE_CCK
)
682 if (spec
->supported_rates
& SUPPORT_RATE_OFDM
)
685 channels
= kzalloc(sizeof(*channels
) * spec
->num_channels
, GFP_KERNEL
);
689 rates
= kzalloc(sizeof(*rates
) * num_rates
, GFP_KERNEL
);
691 goto exit_free_channels
;
694 * Initialize Rate list.
696 for (i
= 0; i
< num_rates
; i
++)
697 rt2x00lib_rate(&rates
[i
], i
, rt2x00_get_rate(i
));
700 * Initialize Channel list.
702 for (i
= 0; i
< spec
->num_channels
; i
++) {
703 rt2x00lib_channel(&channels
[i
],
704 spec
->channels
[i
].channel
,
705 spec
->channels_info
[i
].max_power
, i
);
709 * Intitialize 802.11b, 802.11g
713 if (spec
->supported_bands
& SUPPORT_BAND_2GHZ
) {
714 rt2x00dev
->bands
[IEEE80211_BAND_2GHZ
].n_channels
= 14;
715 rt2x00dev
->bands
[IEEE80211_BAND_2GHZ
].n_bitrates
= num_rates
;
716 rt2x00dev
->bands
[IEEE80211_BAND_2GHZ
].channels
= channels
;
717 rt2x00dev
->bands
[IEEE80211_BAND_2GHZ
].bitrates
= rates
;
718 hw
->wiphy
->bands
[IEEE80211_BAND_2GHZ
] =
719 &rt2x00dev
->bands
[IEEE80211_BAND_2GHZ
];
720 memcpy(&rt2x00dev
->bands
[IEEE80211_BAND_2GHZ
].ht_cap
,
721 &spec
->ht
, sizeof(spec
->ht
));
725 * Intitialize 802.11a
727 * Channels: OFDM, UNII, HiperLAN2.
729 if (spec
->supported_bands
& SUPPORT_BAND_5GHZ
) {
730 rt2x00dev
->bands
[IEEE80211_BAND_5GHZ
].n_channels
=
731 spec
->num_channels
- 14;
732 rt2x00dev
->bands
[IEEE80211_BAND_5GHZ
].n_bitrates
=
734 rt2x00dev
->bands
[IEEE80211_BAND_5GHZ
].channels
= &channels
[14];
735 rt2x00dev
->bands
[IEEE80211_BAND_5GHZ
].bitrates
= &rates
[4];
736 hw
->wiphy
->bands
[IEEE80211_BAND_5GHZ
] =
737 &rt2x00dev
->bands
[IEEE80211_BAND_5GHZ
];
738 memcpy(&rt2x00dev
->bands
[IEEE80211_BAND_5GHZ
].ht_cap
,
739 &spec
->ht
, sizeof(spec
->ht
));
746 ERROR(rt2x00dev
, "Allocation ieee80211 modes failed.\n");
750 static void rt2x00lib_remove_hw(struct rt2x00_dev
*rt2x00dev
)
752 if (test_bit(DEVICE_STATE_REGISTERED_HW
, &rt2x00dev
->flags
))
753 ieee80211_unregister_hw(rt2x00dev
->hw
);
755 if (likely(rt2x00dev
->hw
->wiphy
->bands
[IEEE80211_BAND_2GHZ
])) {
756 kfree(rt2x00dev
->hw
->wiphy
->bands
[IEEE80211_BAND_2GHZ
]->channels
);
757 kfree(rt2x00dev
->hw
->wiphy
->bands
[IEEE80211_BAND_2GHZ
]->bitrates
);
758 rt2x00dev
->hw
->wiphy
->bands
[IEEE80211_BAND_2GHZ
] = NULL
;
759 rt2x00dev
->hw
->wiphy
->bands
[IEEE80211_BAND_5GHZ
] = NULL
;
762 kfree(rt2x00dev
->spec
.channels_info
);
765 static int rt2x00lib_probe_hw(struct rt2x00_dev
*rt2x00dev
)
767 struct hw_mode_spec
*spec
= &rt2x00dev
->spec
;
770 if (test_bit(DEVICE_STATE_REGISTERED_HW
, &rt2x00dev
->flags
))
774 * Initialize HW modes.
776 status
= rt2x00lib_probe_hw_modes(rt2x00dev
, spec
);
781 * Initialize HW fields.
783 rt2x00dev
->hw
->queues
= rt2x00dev
->ops
->tx_queues
;
786 * Initialize extra TX headroom required.
788 rt2x00dev
->hw
->extra_tx_headroom
=
789 max_t(unsigned int, IEEE80211_TX_STATUS_HEADROOM
,
790 rt2x00dev
->ops
->extra_tx_headroom
);
793 * Take TX headroom required for alignment into account.
795 if (test_bit(DRIVER_REQUIRE_L2PAD
, &rt2x00dev
->flags
))
796 rt2x00dev
->hw
->extra_tx_headroom
+= RT2X00_L2PAD_SIZE
;
797 else if (test_bit(DRIVER_REQUIRE_DMA
, &rt2x00dev
->flags
))
798 rt2x00dev
->hw
->extra_tx_headroom
+= RT2X00_ALIGN_SIZE
;
801 * Allocate tx status FIFO for driver use.
803 if (test_bit(DRIVER_REQUIRE_TXSTATUS_FIFO
, &rt2x00dev
->flags
)) {
805 * Allocate txstatus fifo and tasklet, we use a size of 512
806 * for the kfifo which is big enough to store 512/4=128 tx
807 * status reports. In the worst case (tx status for all tx
808 * queues gets reported before we've got a chance to handle
809 * them) 24*4=384 tx status reports need to be cached.
811 status
= kfifo_alloc(&rt2x00dev
->txstatus_fifo
, 512,
816 /* tasklet for processing the tx status reports. */
817 if (rt2x00dev
->ops
->lib
->txstatus_tasklet
)
818 tasklet_init(&rt2x00dev
->txstatus_tasklet
,
819 rt2x00dev
->ops
->lib
->txstatus_tasklet
,
820 (unsigned long)rt2x00dev
);
827 status
= ieee80211_register_hw(rt2x00dev
->hw
);
831 set_bit(DEVICE_STATE_REGISTERED_HW
, &rt2x00dev
->flags
);
837 * Initialization/uninitialization handlers.
839 static void rt2x00lib_uninitialize(struct rt2x00_dev
*rt2x00dev
)
841 if (!test_and_clear_bit(DEVICE_STATE_INITIALIZED
, &rt2x00dev
->flags
))
845 * Unregister extra components.
847 rt2x00rfkill_unregister(rt2x00dev
);
850 * Allow the HW to uninitialize.
852 rt2x00dev
->ops
->lib
->uninitialize(rt2x00dev
);
855 * Free allocated queue entries.
857 rt2x00queue_uninitialize(rt2x00dev
);
860 static int rt2x00lib_initialize(struct rt2x00_dev
*rt2x00dev
)
864 if (test_bit(DEVICE_STATE_INITIALIZED
, &rt2x00dev
->flags
))
868 * Allocate all queue entries.
870 status
= rt2x00queue_initialize(rt2x00dev
);
875 * Initialize the device.
877 status
= rt2x00dev
->ops
->lib
->initialize(rt2x00dev
);
879 rt2x00queue_uninitialize(rt2x00dev
);
883 set_bit(DEVICE_STATE_INITIALIZED
, &rt2x00dev
->flags
);
886 * Register the extra components.
888 rt2x00rfkill_register(rt2x00dev
);
893 int rt2x00lib_start(struct rt2x00_dev
*rt2x00dev
)
897 if (test_bit(DEVICE_STATE_STARTED
, &rt2x00dev
->flags
))
901 * If this is the first interface which is added,
902 * we should load the firmware now.
904 retval
= rt2x00lib_load_firmware(rt2x00dev
);
909 * Initialize the device.
911 retval
= rt2x00lib_initialize(rt2x00dev
);
915 rt2x00dev
->intf_ap_count
= 0;
916 rt2x00dev
->intf_sta_count
= 0;
917 rt2x00dev
->intf_associated
= 0;
919 /* Enable the radio */
920 retval
= rt2x00lib_enable_radio(rt2x00dev
);
924 set_bit(DEVICE_STATE_STARTED
, &rt2x00dev
->flags
);
929 void rt2x00lib_stop(struct rt2x00_dev
*rt2x00dev
)
931 if (!test_and_clear_bit(DEVICE_STATE_STARTED
, &rt2x00dev
->flags
))
935 * Perhaps we can add something smarter here,
936 * but for now just disabling the radio should do.
938 rt2x00lib_disable_radio(rt2x00dev
);
940 rt2x00dev
->intf_ap_count
= 0;
941 rt2x00dev
->intf_sta_count
= 0;
942 rt2x00dev
->intf_associated
= 0;
946 * driver allocation handlers.
948 int rt2x00lib_probe_dev(struct rt2x00_dev
*rt2x00dev
)
950 int retval
= -ENOMEM
;
952 mutex_init(&rt2x00dev
->csr_mutex
);
954 set_bit(DEVICE_STATE_PRESENT
, &rt2x00dev
->flags
);
957 * Make room for rt2x00_intf inside the per-interface
958 * structure ieee80211_vif.
960 rt2x00dev
->hw
->vif_data_size
= sizeof(struct rt2x00_intf
);
963 * Determine which operating modes are supported, all modes
964 * which require beaconing, depend on the availability of
967 rt2x00dev
->hw
->wiphy
->interface_modes
= BIT(NL80211_IFTYPE_STATION
);
968 if (rt2x00dev
->ops
->bcn
->entry_num
> 0)
969 rt2x00dev
->hw
->wiphy
->interface_modes
|=
970 BIT(NL80211_IFTYPE_ADHOC
) |
971 BIT(NL80211_IFTYPE_AP
) |
972 BIT(NL80211_IFTYPE_MESH_POINT
) |
973 BIT(NL80211_IFTYPE_WDS
);
976 * Initialize configuration work.
978 INIT_WORK(&rt2x00dev
->intf_work
, rt2x00lib_intf_scheduled
);
981 * Let the driver probe the device to detect the capabilities.
983 retval
= rt2x00dev
->ops
->lib
->probe_hw(rt2x00dev
);
985 ERROR(rt2x00dev
, "Failed to allocate device.\n");
990 * Allocate queue array.
992 retval
= rt2x00queue_allocate(rt2x00dev
);
997 * Initialize ieee80211 structure.
999 retval
= rt2x00lib_probe_hw(rt2x00dev
);
1001 ERROR(rt2x00dev
, "Failed to initialize hw.\n");
1006 * Register extra components.
1008 rt2x00link_register(rt2x00dev
);
1009 rt2x00leds_register(rt2x00dev
);
1010 rt2x00debug_register(rt2x00dev
);
1015 rt2x00lib_remove_dev(rt2x00dev
);
1019 EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev
);
1021 void rt2x00lib_remove_dev(struct rt2x00_dev
*rt2x00dev
)
1023 clear_bit(DEVICE_STATE_PRESENT
, &rt2x00dev
->flags
);
1028 rt2x00lib_disable_radio(rt2x00dev
);
1033 cancel_work_sync(&rt2x00dev
->intf_work
);
1034 cancel_work_sync(&rt2x00dev
->rxdone_work
);
1035 cancel_work_sync(&rt2x00dev
->txdone_work
);
1038 * Free the tx status fifo.
1040 kfifo_free(&rt2x00dev
->txstatus_fifo
);
1043 * Kill the tx status tasklet.
1045 tasklet_kill(&rt2x00dev
->txstatus_tasklet
);
1048 * Uninitialize device.
1050 rt2x00lib_uninitialize(rt2x00dev
);
1053 * Free extra components
1055 rt2x00debug_deregister(rt2x00dev
);
1056 rt2x00leds_unregister(rt2x00dev
);
1059 * Free ieee80211_hw memory.
1061 rt2x00lib_remove_hw(rt2x00dev
);
1064 * Free firmware image.
1066 rt2x00lib_free_firmware(rt2x00dev
);
1069 * Free queue structures.
1071 rt2x00queue_free(rt2x00dev
);
1073 EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev
);
1076 * Device state handlers
1079 int rt2x00lib_suspend(struct rt2x00_dev
*rt2x00dev
, pm_message_t state
)
1081 NOTICE(rt2x00dev
, "Going to sleep.\n");
1084 * Prevent mac80211 from accessing driver while suspended.
1086 if (!test_and_clear_bit(DEVICE_STATE_PRESENT
, &rt2x00dev
->flags
))
1090 * Cleanup as much as possible.
1092 rt2x00lib_uninitialize(rt2x00dev
);
1095 * Suspend/disable extra components.
1097 rt2x00leds_suspend(rt2x00dev
);
1098 rt2x00debug_deregister(rt2x00dev
);
1101 * Set device mode to sleep for power management,
1102 * on some hardware this call seems to consistently fail.
1103 * From the specifications it is hard to tell why it fails,
1104 * and if this is a "bad thing".
1105 * Overall it is safe to just ignore the failure and
1106 * continue suspending. The only downside is that the
1107 * device will not be in optimal power save mode, but with
1108 * the radio and the other components already disabled the
1109 * device is as good as disabled.
1111 if (rt2x00dev
->ops
->lib
->set_device_state(rt2x00dev
, STATE_SLEEP
))
1112 WARNING(rt2x00dev
, "Device failed to enter sleep state, "
1113 "continue suspending.\n");
1117 EXPORT_SYMBOL_GPL(rt2x00lib_suspend
);
1119 int rt2x00lib_resume(struct rt2x00_dev
*rt2x00dev
)
1121 NOTICE(rt2x00dev
, "Waking up.\n");
1124 * Restore/enable extra components.
1126 rt2x00debug_register(rt2x00dev
);
1127 rt2x00leds_resume(rt2x00dev
);
1130 * We are ready again to receive requests from mac80211.
1132 set_bit(DEVICE_STATE_PRESENT
, &rt2x00dev
->flags
);
1136 EXPORT_SYMBOL_GPL(rt2x00lib_resume
);
1137 #endif /* CONFIG_PM */
1140 * rt2x00lib module information.
1142 MODULE_AUTHOR(DRV_PROJECT
);
1143 MODULE_VERSION(DRV_VERSION
);
1144 MODULE_DESCRIPTION("rt2x00 library");
1145 MODULE_LICENSE("GPL");