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 /* XXX: this assumption about the band is wrong for 802.11j */
653 entry
->band
= channel
<= 14 ? IEEE80211_BAND_2GHZ
: IEEE80211_BAND_5GHZ
;
654 entry
->center_freq
= ieee80211_channel_to_frequency(channel
,
656 entry
->hw_value
= value
;
657 entry
->max_power
= tx_power
;
658 entry
->max_antenna_gain
= 0xff;
661 static void rt2x00lib_rate(struct ieee80211_rate
*entry
,
662 const u16 index
, const struct rt2x00_rate
*rate
)
665 entry
->bitrate
= rate
->bitrate
;
666 entry
->hw_value
= index
;
667 entry
->hw_value_short
= index
;
669 if (rate
->flags
& DEV_RATE_SHORT_PREAMBLE
)
670 entry
->flags
|= IEEE80211_RATE_SHORT_PREAMBLE
;
673 static int rt2x00lib_probe_hw_modes(struct rt2x00_dev
*rt2x00dev
,
674 struct hw_mode_spec
*spec
)
676 struct ieee80211_hw
*hw
= rt2x00dev
->hw
;
677 struct ieee80211_channel
*channels
;
678 struct ieee80211_rate
*rates
;
679 unsigned int num_rates
;
683 if (spec
->supported_rates
& SUPPORT_RATE_CCK
)
685 if (spec
->supported_rates
& SUPPORT_RATE_OFDM
)
688 channels
= kzalloc(sizeof(*channels
) * spec
->num_channels
, GFP_KERNEL
);
692 rates
= kzalloc(sizeof(*rates
) * num_rates
, GFP_KERNEL
);
694 goto exit_free_channels
;
697 * Initialize Rate list.
699 for (i
= 0; i
< num_rates
; i
++)
700 rt2x00lib_rate(&rates
[i
], i
, rt2x00_get_rate(i
));
703 * Initialize Channel list.
705 for (i
= 0; i
< spec
->num_channels
; i
++) {
706 rt2x00lib_channel(&channels
[i
],
707 spec
->channels
[i
].channel
,
708 spec
->channels_info
[i
].max_power
, i
);
712 * Intitialize 802.11b, 802.11g
716 if (spec
->supported_bands
& SUPPORT_BAND_2GHZ
) {
717 rt2x00dev
->bands
[IEEE80211_BAND_2GHZ
].n_channels
= 14;
718 rt2x00dev
->bands
[IEEE80211_BAND_2GHZ
].n_bitrates
= num_rates
;
719 rt2x00dev
->bands
[IEEE80211_BAND_2GHZ
].channels
= channels
;
720 rt2x00dev
->bands
[IEEE80211_BAND_2GHZ
].bitrates
= rates
;
721 hw
->wiphy
->bands
[IEEE80211_BAND_2GHZ
] =
722 &rt2x00dev
->bands
[IEEE80211_BAND_2GHZ
];
723 memcpy(&rt2x00dev
->bands
[IEEE80211_BAND_2GHZ
].ht_cap
,
724 &spec
->ht
, sizeof(spec
->ht
));
728 * Intitialize 802.11a
730 * Channels: OFDM, UNII, HiperLAN2.
732 if (spec
->supported_bands
& SUPPORT_BAND_5GHZ
) {
733 rt2x00dev
->bands
[IEEE80211_BAND_5GHZ
].n_channels
=
734 spec
->num_channels
- 14;
735 rt2x00dev
->bands
[IEEE80211_BAND_5GHZ
].n_bitrates
=
737 rt2x00dev
->bands
[IEEE80211_BAND_5GHZ
].channels
= &channels
[14];
738 rt2x00dev
->bands
[IEEE80211_BAND_5GHZ
].bitrates
= &rates
[4];
739 hw
->wiphy
->bands
[IEEE80211_BAND_5GHZ
] =
740 &rt2x00dev
->bands
[IEEE80211_BAND_5GHZ
];
741 memcpy(&rt2x00dev
->bands
[IEEE80211_BAND_5GHZ
].ht_cap
,
742 &spec
->ht
, sizeof(spec
->ht
));
749 ERROR(rt2x00dev
, "Allocation ieee80211 modes failed.\n");
753 static void rt2x00lib_remove_hw(struct rt2x00_dev
*rt2x00dev
)
755 if (test_bit(DEVICE_STATE_REGISTERED_HW
, &rt2x00dev
->flags
))
756 ieee80211_unregister_hw(rt2x00dev
->hw
);
758 if (likely(rt2x00dev
->hw
->wiphy
->bands
[IEEE80211_BAND_2GHZ
])) {
759 kfree(rt2x00dev
->hw
->wiphy
->bands
[IEEE80211_BAND_2GHZ
]->channels
);
760 kfree(rt2x00dev
->hw
->wiphy
->bands
[IEEE80211_BAND_2GHZ
]->bitrates
);
761 rt2x00dev
->hw
->wiphy
->bands
[IEEE80211_BAND_2GHZ
] = NULL
;
762 rt2x00dev
->hw
->wiphy
->bands
[IEEE80211_BAND_5GHZ
] = NULL
;
765 kfree(rt2x00dev
->spec
.channels_info
);
768 static int rt2x00lib_probe_hw(struct rt2x00_dev
*rt2x00dev
)
770 struct hw_mode_spec
*spec
= &rt2x00dev
->spec
;
773 if (test_bit(DEVICE_STATE_REGISTERED_HW
, &rt2x00dev
->flags
))
777 * Initialize HW modes.
779 status
= rt2x00lib_probe_hw_modes(rt2x00dev
, spec
);
784 * Initialize HW fields.
786 rt2x00dev
->hw
->queues
= rt2x00dev
->ops
->tx_queues
;
789 * Initialize extra TX headroom required.
791 rt2x00dev
->hw
->extra_tx_headroom
=
792 max_t(unsigned int, IEEE80211_TX_STATUS_HEADROOM
,
793 rt2x00dev
->ops
->extra_tx_headroom
);
796 * Take TX headroom required for alignment into account.
798 if (test_bit(DRIVER_REQUIRE_L2PAD
, &rt2x00dev
->flags
))
799 rt2x00dev
->hw
->extra_tx_headroom
+= RT2X00_L2PAD_SIZE
;
800 else if (test_bit(DRIVER_REQUIRE_DMA
, &rt2x00dev
->flags
))
801 rt2x00dev
->hw
->extra_tx_headroom
+= RT2X00_ALIGN_SIZE
;
804 * Allocate tx status FIFO for driver use.
806 if (test_bit(DRIVER_REQUIRE_TXSTATUS_FIFO
, &rt2x00dev
->flags
)) {
808 * Allocate txstatus fifo and tasklet, we use a size of 512
809 * for the kfifo which is big enough to store 512/4=128 tx
810 * status reports. In the worst case (tx status for all tx
811 * queues gets reported before we've got a chance to handle
812 * them) 24*4=384 tx status reports need to be cached.
814 status
= kfifo_alloc(&rt2x00dev
->txstatus_fifo
, 512,
819 /* tasklet for processing the tx status reports. */
820 if (rt2x00dev
->ops
->lib
->txstatus_tasklet
)
821 tasklet_init(&rt2x00dev
->txstatus_tasklet
,
822 rt2x00dev
->ops
->lib
->txstatus_tasklet
,
823 (unsigned long)rt2x00dev
);
830 status
= ieee80211_register_hw(rt2x00dev
->hw
);
834 set_bit(DEVICE_STATE_REGISTERED_HW
, &rt2x00dev
->flags
);
840 * Initialization/uninitialization handlers.
842 static void rt2x00lib_uninitialize(struct rt2x00_dev
*rt2x00dev
)
844 if (!test_and_clear_bit(DEVICE_STATE_INITIALIZED
, &rt2x00dev
->flags
))
848 * Unregister extra components.
850 rt2x00rfkill_unregister(rt2x00dev
);
853 * Allow the HW to uninitialize.
855 rt2x00dev
->ops
->lib
->uninitialize(rt2x00dev
);
858 * Free allocated queue entries.
860 rt2x00queue_uninitialize(rt2x00dev
);
863 static int rt2x00lib_initialize(struct rt2x00_dev
*rt2x00dev
)
867 if (test_bit(DEVICE_STATE_INITIALIZED
, &rt2x00dev
->flags
))
871 * Allocate all queue entries.
873 status
= rt2x00queue_initialize(rt2x00dev
);
878 * Initialize the device.
880 status
= rt2x00dev
->ops
->lib
->initialize(rt2x00dev
);
882 rt2x00queue_uninitialize(rt2x00dev
);
886 set_bit(DEVICE_STATE_INITIALIZED
, &rt2x00dev
->flags
);
889 * Register the extra components.
891 rt2x00rfkill_register(rt2x00dev
);
896 int rt2x00lib_start(struct rt2x00_dev
*rt2x00dev
)
900 if (test_bit(DEVICE_STATE_STARTED
, &rt2x00dev
->flags
))
904 * If this is the first interface which is added,
905 * we should load the firmware now.
907 retval
= rt2x00lib_load_firmware(rt2x00dev
);
912 * Initialize the device.
914 retval
= rt2x00lib_initialize(rt2x00dev
);
918 rt2x00dev
->intf_ap_count
= 0;
919 rt2x00dev
->intf_sta_count
= 0;
920 rt2x00dev
->intf_associated
= 0;
922 /* Enable the radio */
923 retval
= rt2x00lib_enable_radio(rt2x00dev
);
927 set_bit(DEVICE_STATE_STARTED
, &rt2x00dev
->flags
);
932 void rt2x00lib_stop(struct rt2x00_dev
*rt2x00dev
)
934 if (!test_and_clear_bit(DEVICE_STATE_STARTED
, &rt2x00dev
->flags
))
938 * Perhaps we can add something smarter here,
939 * but for now just disabling the radio should do.
941 rt2x00lib_disable_radio(rt2x00dev
);
943 rt2x00dev
->intf_ap_count
= 0;
944 rt2x00dev
->intf_sta_count
= 0;
945 rt2x00dev
->intf_associated
= 0;
949 * driver allocation handlers.
951 int rt2x00lib_probe_dev(struct rt2x00_dev
*rt2x00dev
)
953 int retval
= -ENOMEM
;
955 mutex_init(&rt2x00dev
->csr_mutex
);
957 set_bit(DEVICE_STATE_PRESENT
, &rt2x00dev
->flags
);
960 * Make room for rt2x00_intf inside the per-interface
961 * structure ieee80211_vif.
963 rt2x00dev
->hw
->vif_data_size
= sizeof(struct rt2x00_intf
);
966 * Determine which operating modes are supported, all modes
967 * which require beaconing, depend on the availability of
970 rt2x00dev
->hw
->wiphy
->interface_modes
= BIT(NL80211_IFTYPE_STATION
);
971 if (rt2x00dev
->ops
->bcn
->entry_num
> 0)
972 rt2x00dev
->hw
->wiphy
->interface_modes
|=
973 BIT(NL80211_IFTYPE_ADHOC
) |
974 BIT(NL80211_IFTYPE_AP
) |
975 BIT(NL80211_IFTYPE_MESH_POINT
) |
976 BIT(NL80211_IFTYPE_WDS
);
979 * Initialize configuration work.
981 INIT_WORK(&rt2x00dev
->intf_work
, rt2x00lib_intf_scheduled
);
984 * Let the driver probe the device to detect the capabilities.
986 retval
= rt2x00dev
->ops
->lib
->probe_hw(rt2x00dev
);
988 ERROR(rt2x00dev
, "Failed to allocate device.\n");
993 * Allocate queue array.
995 retval
= rt2x00queue_allocate(rt2x00dev
);
1000 * Initialize ieee80211 structure.
1002 retval
= rt2x00lib_probe_hw(rt2x00dev
);
1004 ERROR(rt2x00dev
, "Failed to initialize hw.\n");
1009 * Register extra components.
1011 rt2x00link_register(rt2x00dev
);
1012 rt2x00leds_register(rt2x00dev
);
1013 rt2x00debug_register(rt2x00dev
);
1018 rt2x00lib_remove_dev(rt2x00dev
);
1022 EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev
);
1024 void rt2x00lib_remove_dev(struct rt2x00_dev
*rt2x00dev
)
1026 clear_bit(DEVICE_STATE_PRESENT
, &rt2x00dev
->flags
);
1031 rt2x00lib_disable_radio(rt2x00dev
);
1036 cancel_work_sync(&rt2x00dev
->intf_work
);
1037 cancel_work_sync(&rt2x00dev
->rxdone_work
);
1038 cancel_work_sync(&rt2x00dev
->txdone_work
);
1041 * Free the tx status fifo.
1043 kfifo_free(&rt2x00dev
->txstatus_fifo
);
1046 * Kill the tx status tasklet.
1048 tasklet_kill(&rt2x00dev
->txstatus_tasklet
);
1051 * Uninitialize device.
1053 rt2x00lib_uninitialize(rt2x00dev
);
1056 * Free extra components
1058 rt2x00debug_deregister(rt2x00dev
);
1059 rt2x00leds_unregister(rt2x00dev
);
1062 * Free ieee80211_hw memory.
1064 rt2x00lib_remove_hw(rt2x00dev
);
1067 * Free firmware image.
1069 rt2x00lib_free_firmware(rt2x00dev
);
1072 * Free queue structures.
1074 rt2x00queue_free(rt2x00dev
);
1076 EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev
);
1079 * Device state handlers
1082 int rt2x00lib_suspend(struct rt2x00_dev
*rt2x00dev
, pm_message_t state
)
1084 NOTICE(rt2x00dev
, "Going to sleep.\n");
1087 * Prevent mac80211 from accessing driver while suspended.
1089 if (!test_and_clear_bit(DEVICE_STATE_PRESENT
, &rt2x00dev
->flags
))
1093 * Cleanup as much as possible.
1095 rt2x00lib_uninitialize(rt2x00dev
);
1098 * Suspend/disable extra components.
1100 rt2x00leds_suspend(rt2x00dev
);
1101 rt2x00debug_deregister(rt2x00dev
);
1104 * Set device mode to sleep for power management,
1105 * on some hardware this call seems to consistently fail.
1106 * From the specifications it is hard to tell why it fails,
1107 * and if this is a "bad thing".
1108 * Overall it is safe to just ignore the failure and
1109 * continue suspending. The only downside is that the
1110 * device will not be in optimal power save mode, but with
1111 * the radio and the other components already disabled the
1112 * device is as good as disabled.
1114 if (rt2x00dev
->ops
->lib
->set_device_state(rt2x00dev
, STATE_SLEEP
))
1115 WARNING(rt2x00dev
, "Device failed to enter sleep state, "
1116 "continue suspending.\n");
1120 EXPORT_SYMBOL_GPL(rt2x00lib_suspend
);
1122 int rt2x00lib_resume(struct rt2x00_dev
*rt2x00dev
)
1124 NOTICE(rt2x00dev
, "Waking up.\n");
1127 * Restore/enable extra components.
1129 rt2x00debug_register(rt2x00dev
);
1130 rt2x00leds_resume(rt2x00dev
);
1133 * We are ready again to receive requests from mac80211.
1135 set_bit(DEVICE_STATE_PRESENT
, &rt2x00dev
->flags
);
1139 EXPORT_SYMBOL_GPL(rt2x00lib_resume
);
1140 #endif /* CONFIG_PM */
1143 * rt2x00lib module information.
1145 MODULE_AUTHOR(DRV_PROJECT
);
1146 MODULE_VERSION(DRV_VERSION
);
1147 MODULE_DESCRIPTION("rt2x00 library");
1148 MODULE_LICENSE("GPL");