GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / net / wireless / rt2x00 / rt2x00dev.c
blob19f86ce13df559d243c870f702559c28c1c8ff64
1 /*
2 Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
3 <http://rt2x00.serialmonkey.com>
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.
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.
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.
22 Module: rt2x00lib
23 Abstract: rt2x00 generic device routines.
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
30 #include "rt2x00.h"
31 #include "rt2x00lib.h"
34 * Radio control handlers.
36 int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
38 int status;
41 * Don't enable the radio twice.
42 * And check if the hardware button has been disabled.
44 if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
45 return 0;
48 * Initialize all data queues.
50 rt2x00queue_init_queues(rt2x00dev);
53 * Enable radio.
55 status =
56 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_ON);
57 if (status)
58 return status;
60 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_ON);
62 rt2x00leds_led_radio(rt2x00dev, true);
63 rt2x00led_led_activity(rt2x00dev, true);
65 set_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags);
68 * Enable RX.
70 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
73 * Start watchdog monitoring.
75 rt2x00link_start_watchdog(rt2x00dev);
78 * Start the TX queues.
80 ieee80211_wake_queues(rt2x00dev->hw);
82 return 0;
85 void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
87 if (!test_and_clear_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
88 return;
91 * Stop the TX queues in mac80211.
93 ieee80211_stop_queues(rt2x00dev->hw);
94 rt2x00queue_stop_queues(rt2x00dev);
97 * Stop watchdog monitoring.
99 rt2x00link_stop_watchdog(rt2x00dev);
102 * Disable RX.
104 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
107 * Disable radio.
109 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
110 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_OFF);
111 rt2x00led_led_activity(rt2x00dev, false);
112 rt2x00leds_led_radio(rt2x00dev, false);
115 void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, enum dev_state state)
118 * When we are disabling the RX, we should also stop the link tuner.
120 if (state == STATE_RADIO_RX_OFF)
121 rt2x00link_stop_tuner(rt2x00dev);
123 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
126 * When we are enabling the RX, we should also start the link tuner.
128 if (state == STATE_RADIO_RX_ON)
129 rt2x00link_start_tuner(rt2x00dev);
132 static void rt2x00lib_intf_scheduled_iter(void *data, u8 *mac,
133 struct ieee80211_vif *vif)
135 struct rt2x00_dev *rt2x00dev = data;
136 struct rt2x00_intf *intf = vif_to_intf(vif);
137 int delayed_flags;
140 * Copy all data we need during this action under the protection
141 * of a spinlock. Otherwise race conditions might occur which results
142 * into an invalid configuration.
144 spin_lock(&intf->lock);
146 delayed_flags = intf->delayed_flags;
147 intf->delayed_flags = 0;
149 spin_unlock(&intf->lock);
152 * It is possible the radio was disabled while the work had been
153 * scheduled. If that happens we should return here immediately,
154 * note that in the spinlock protected area above the delayed_flags
155 * have been cleared correctly.
157 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
158 return;
160 if (delayed_flags & DELAYED_UPDATE_BEACON)
161 rt2x00queue_update_beacon(rt2x00dev, vif, true);
164 static void rt2x00lib_intf_scheduled(struct work_struct *work)
166 struct rt2x00_dev *rt2x00dev =
167 container_of(work, struct rt2x00_dev, intf_work);
170 * Iterate over each interface and perform the
171 * requested configurations.
173 ieee80211_iterate_active_interfaces(rt2x00dev->hw,
174 rt2x00lib_intf_scheduled_iter,
175 rt2x00dev);
179 * Interrupt context handlers.
181 static void rt2x00lib_bc_buffer_iter(void *data, u8 *mac,
182 struct ieee80211_vif *vif)
184 struct rt2x00_dev *rt2x00dev = data;
185 struct sk_buff *skb;
188 * Only AP mode interfaces do broad- and multicast buffering
190 if (vif->type != NL80211_IFTYPE_AP)
191 return;
194 * Send out buffered broad- and multicast frames
196 skb = ieee80211_get_buffered_bc(rt2x00dev->hw, vif);
197 while (skb) {
198 rt2x00mac_tx(rt2x00dev->hw, skb);
199 skb = ieee80211_get_buffered_bc(rt2x00dev->hw, vif);
203 static void rt2x00lib_beaconupdate_iter(void *data, u8 *mac,
204 struct ieee80211_vif *vif)
206 struct rt2x00_dev *rt2x00dev = data;
208 if (vif->type != NL80211_IFTYPE_AP &&
209 vif->type != NL80211_IFTYPE_ADHOC &&
210 vif->type != NL80211_IFTYPE_MESH_POINT &&
211 vif->type != NL80211_IFTYPE_WDS)
212 return;
214 rt2x00queue_update_beacon(rt2x00dev, vif, true);
217 void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
219 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
220 return;
222 /* send buffered bc/mc frames out for every bssid */
223 ieee80211_iterate_active_interfaces(rt2x00dev->hw,
224 rt2x00lib_bc_buffer_iter,
225 rt2x00dev);
227 * Devices with pre tbtt interrupt don't need to update the beacon
228 * here as they will fetch the next beacon directly prior to
229 * transmission.
231 if (test_bit(DRIVER_SUPPORT_PRE_TBTT_INTERRUPT, &rt2x00dev->flags))
232 return;
234 /* fetch next beacon */
235 ieee80211_iterate_active_interfaces(rt2x00dev->hw,
236 rt2x00lib_beaconupdate_iter,
237 rt2x00dev);
239 EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
241 void rt2x00lib_pretbtt(struct rt2x00_dev *rt2x00dev)
243 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
244 return;
246 /* fetch next beacon */
247 ieee80211_iterate_active_interfaces(rt2x00dev->hw,
248 rt2x00lib_beaconupdate_iter,
249 rt2x00dev);
251 EXPORT_SYMBOL_GPL(rt2x00lib_pretbtt);
253 void rt2x00lib_txdone(struct queue_entry *entry,
254 struct txdone_entry_desc *txdesc)
256 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
257 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
258 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
259 enum data_queue_qid qid = skb_get_queue_mapping(entry->skb);
260 unsigned int header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
261 u8 rate_idx, rate_flags, retry_rates;
262 u8 skbdesc_flags = skbdesc->flags;
263 unsigned int i;
264 bool success;
267 * Unmap the skb.
269 rt2x00queue_unmap_skb(rt2x00dev, entry->skb);
272 * Remove the extra tx headroom from the skb.
274 skb_pull(entry->skb, rt2x00dev->ops->extra_tx_headroom);
277 * Signal that the TX descriptor is no longer in the skb.
279 skbdesc->flags &= ~SKBDESC_DESC_IN_SKB;
282 * Remove L2 padding which was added during
284 if (test_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags))
285 rt2x00queue_remove_l2pad(entry->skb, header_length);
288 * If the IV/EIV data was stripped from the frame before it was
289 * passed to the hardware, we should now reinsert it again because
290 * mac80211 will expect the same data to be present it the
291 * frame as it was passed to us.
293 if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags))
294 rt2x00crypto_tx_insert_iv(entry->skb, header_length);
297 * Send frame to debugfs immediately, after this call is completed
298 * we are going to overwrite the skb->cb array.
300 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TXDONE, entry->skb);
303 * Determine if the frame has been successfully transmitted.
305 success =
306 test_bit(TXDONE_SUCCESS, &txdesc->flags) ||
307 test_bit(TXDONE_UNKNOWN, &txdesc->flags);
310 * Update TX statistics.
312 rt2x00dev->link.qual.tx_success += success;
313 rt2x00dev->link.qual.tx_failed += !success;
315 rate_idx = skbdesc->tx_rate_idx;
316 rate_flags = skbdesc->tx_rate_flags;
317 retry_rates = test_bit(TXDONE_FALLBACK, &txdesc->flags) ?
318 (txdesc->retry + 1) : 1;
321 * Initialize TX status
323 memset(&tx_info->status, 0, sizeof(tx_info->status));
324 tx_info->status.ack_signal = 0;
327 * Frame was send with retries, hardware tried
328 * different rates to send out the frame, at each
329 * retry it lowered the rate 1 step except when the
330 * lowest rate was used.
332 for (i = 0; i < retry_rates && i < IEEE80211_TX_MAX_RATES; i++) {
333 tx_info->status.rates[i].idx = rate_idx - i;
334 tx_info->status.rates[i].flags = rate_flags;
336 if (rate_idx - i == 0) {
338 * The lowest rate (index 0) was used until the
339 * number of max retries was reached.
341 tx_info->status.rates[i].count = retry_rates - i;
342 i++;
343 break;
345 tx_info->status.rates[i].count = 1;
347 if (i < (IEEE80211_TX_MAX_RATES - 1))
348 tx_info->status.rates[i].idx = -1; /* terminate */
350 if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK)) {
351 if (success)
352 tx_info->flags |= IEEE80211_TX_STAT_ACK;
353 else
354 rt2x00dev->low_level_stats.dot11ACKFailureCount++;
358 * Every single frame has it's own tx status, hence report
359 * every frame as ampdu of size 1.
361 * TODO: if we can find out how many frames were aggregated
362 * by the hw we could provide the real ampdu_len to mac80211
363 * which would allow the rc algorithm to better decide on
364 * which rates are suitable.
366 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
367 tx_info->flags |= IEEE80211_TX_STAT_AMPDU;
368 tx_info->status.ampdu_len = 1;
369 tx_info->status.ampdu_ack_len = success ? 1 : 0;
372 if (rate_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
373 if (success)
374 rt2x00dev->low_level_stats.dot11RTSSuccessCount++;
375 else
376 rt2x00dev->low_level_stats.dot11RTSFailureCount++;
380 * Only send the status report to mac80211 when it's a frame
381 * that originated in mac80211. If this was a extra frame coming
382 * through a mac80211 library call (RTS/CTS) then we should not
383 * send the status report back.
385 if (!(skbdesc_flags & SKBDESC_NOT_MAC80211))
387 * Only PCI and SOC devices process the tx status in process
388 * context. Hence use ieee80211_tx_status for PCI and SOC
389 * devices and stick to ieee80211_tx_status_irqsafe for USB.
391 if (rt2x00_is_usb(rt2x00dev))
392 ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb);
393 else
394 ieee80211_tx_status(rt2x00dev->hw, entry->skb);
395 else
396 dev_kfree_skb_any(entry->skb);
399 * Make this entry available for reuse.
401 entry->skb = NULL;
402 entry->flags = 0;
404 rt2x00dev->ops->lib->clear_entry(entry);
406 clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
407 rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
410 * If the data queue was below the threshold before the txdone
411 * handler we must make sure the packet queue in the mac80211 stack
412 * is reenabled when the txdone handler has finished.
414 if (!rt2x00queue_threshold(entry->queue))
415 ieee80211_wake_queue(rt2x00dev->hw, qid);
417 EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
419 static int rt2x00lib_rxdone_read_signal(struct rt2x00_dev *rt2x00dev,
420 struct rxdone_entry_desc *rxdesc)
422 struct ieee80211_supported_band *sband;
423 const struct rt2x00_rate *rate;
424 unsigned int i;
425 int signal;
426 int type;
429 * For non-HT rates the MCS value needs to contain the
430 * actually used rate modulation (CCK or OFDM).
432 if (rxdesc->dev_flags & RXDONE_SIGNAL_MCS)
433 signal = RATE_MCS(rxdesc->rate_mode, rxdesc->signal);
434 else
435 signal = rxdesc->signal;
437 type = (rxdesc->dev_flags & RXDONE_SIGNAL_MASK);
439 sband = &rt2x00dev->bands[rt2x00dev->curr_band];
440 for (i = 0; i < sband->n_bitrates; i++) {
441 rate = rt2x00_get_rate(sband->bitrates[i].hw_value);
443 if (((type == RXDONE_SIGNAL_PLCP) &&
444 (rate->plcp == signal)) ||
445 ((type == RXDONE_SIGNAL_BITRATE) &&
446 (rate->bitrate == signal)) ||
447 ((type == RXDONE_SIGNAL_MCS) &&
448 (rate->mcs == signal))) {
449 return i;
453 WARNING(rt2x00dev, "Frame received with unrecognized signal, "
454 "signal=0x%.4x, type=%d.\n", signal, type);
455 return 0;
458 void rt2x00lib_rxdone(struct rt2x00_dev *rt2x00dev,
459 struct queue_entry *entry)
461 struct rxdone_entry_desc rxdesc;
462 struct sk_buff *skb;
463 struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
464 unsigned int header_length;
465 int rate_idx;
467 * Allocate a new sk_buffer. If no new buffer available, drop the
468 * received frame and reuse the existing buffer.
470 skb = rt2x00queue_alloc_rxskb(rt2x00dev, entry);
471 if (!skb)
472 return;
475 * Unmap the skb.
477 rt2x00queue_unmap_skb(rt2x00dev, entry->skb);
480 * Extract the RXD details.
482 memset(&rxdesc, 0, sizeof(rxdesc));
483 rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
486 * The data behind the ieee80211 header must be
487 * aligned on a 4 byte boundary.
489 header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
492 * Hardware might have stripped the IV/EIV/ICV data,
493 * in that case it is possible that the data was
494 * provided separately (through hardware descriptor)
495 * in which case we should reinsert the data into the frame.
497 if ((rxdesc.dev_flags & RXDONE_CRYPTO_IV) &&
498 (rxdesc.flags & RX_FLAG_IV_STRIPPED))
499 rt2x00crypto_rx_insert_iv(entry->skb, header_length,
500 &rxdesc);
501 else if (header_length &&
502 (rxdesc.size > header_length) &&
503 (rxdesc.dev_flags & RXDONE_L2PAD))
504 rt2x00queue_remove_l2pad(entry->skb, header_length);
505 else
506 rt2x00queue_align_payload(entry->skb, header_length);
508 /* Trim buffer to correct size */
509 skb_trim(entry->skb, rxdesc.size);
512 * Check if the frame was received using HT. In that case,
513 * the rate is the MCS index and should be passed to mac80211
514 * directly. Otherwise we need to translate the signal to
515 * the correct bitrate index.
517 if (rxdesc.rate_mode == RATE_MODE_CCK ||
518 rxdesc.rate_mode == RATE_MODE_OFDM) {
519 rate_idx = rt2x00lib_rxdone_read_signal(rt2x00dev, &rxdesc);
520 } else {
521 rxdesc.flags |= RX_FLAG_HT;
522 rate_idx = rxdesc.signal;
526 * Update extra components
528 rt2x00link_update_stats(rt2x00dev, entry->skb, &rxdesc);
529 rt2x00debug_update_crypto(rt2x00dev, &rxdesc);
531 rx_status->mactime = rxdesc.timestamp;
532 rx_status->rate_idx = rate_idx;
533 rx_status->signal = rxdesc.rssi;
534 rx_status->flag = rxdesc.flags;
535 rx_status->antenna = rt2x00dev->link.ant.active.rx;
538 * Send frame to mac80211 & debugfs.
539 * mac80211 will clean up the skb structure.
541 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_RXDONE, entry->skb);
542 memcpy(IEEE80211_SKB_RXCB(entry->skb), rx_status, sizeof(*rx_status));
545 * Currently only PCI and SOC devices handle rx interrupts in process
546 * context. Hence, use ieee80211_rx_irqsafe for USB and ieee80211_rx_ni
547 * for PCI and SOC devices.
549 if (rt2x00_is_usb(rt2x00dev))
550 ieee80211_rx_irqsafe(rt2x00dev->hw, entry->skb);
551 else
552 ieee80211_rx_ni(rt2x00dev->hw, entry->skb);
555 * Replace the skb with the freshly allocated one.
557 entry->skb = skb;
558 entry->flags = 0;
560 rt2x00dev->ops->lib->clear_entry(entry);
562 rt2x00queue_index_inc(entry->queue, Q_INDEX);
564 EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
567 * Driver initialization handlers.
569 const struct rt2x00_rate rt2x00_supported_rates[12] = {
571 .flags = DEV_RATE_CCK,
572 .bitrate = 10,
573 .ratemask = BIT(0),
574 .plcp = 0x00,
575 .mcs = RATE_MCS(RATE_MODE_CCK, 0),
578 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
579 .bitrate = 20,
580 .ratemask = BIT(1),
581 .plcp = 0x01,
582 .mcs = RATE_MCS(RATE_MODE_CCK, 1),
585 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
586 .bitrate = 55,
587 .ratemask = BIT(2),
588 .plcp = 0x02,
589 .mcs = RATE_MCS(RATE_MODE_CCK, 2),
592 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
593 .bitrate = 110,
594 .ratemask = BIT(3),
595 .plcp = 0x03,
596 .mcs = RATE_MCS(RATE_MODE_CCK, 3),
599 .flags = DEV_RATE_OFDM,
600 .bitrate = 60,
601 .ratemask = BIT(4),
602 .plcp = 0x0b,
603 .mcs = RATE_MCS(RATE_MODE_OFDM, 0),
606 .flags = DEV_RATE_OFDM,
607 .bitrate = 90,
608 .ratemask = BIT(5),
609 .plcp = 0x0f,
610 .mcs = RATE_MCS(RATE_MODE_OFDM, 1),
613 .flags = DEV_RATE_OFDM,
614 .bitrate = 120,
615 .ratemask = BIT(6),
616 .plcp = 0x0a,
617 .mcs = RATE_MCS(RATE_MODE_OFDM, 2),
620 .flags = DEV_RATE_OFDM,
621 .bitrate = 180,
622 .ratemask = BIT(7),
623 .plcp = 0x0e,
624 .mcs = RATE_MCS(RATE_MODE_OFDM, 3),
627 .flags = DEV_RATE_OFDM,
628 .bitrate = 240,
629 .ratemask = BIT(8),
630 .plcp = 0x09,
631 .mcs = RATE_MCS(RATE_MODE_OFDM, 4),
634 .flags = DEV_RATE_OFDM,
635 .bitrate = 360,
636 .ratemask = BIT(9),
637 .plcp = 0x0d,
638 .mcs = RATE_MCS(RATE_MODE_OFDM, 5),
641 .flags = DEV_RATE_OFDM,
642 .bitrate = 480,
643 .ratemask = BIT(10),
644 .plcp = 0x08,
645 .mcs = RATE_MCS(RATE_MODE_OFDM, 6),
648 .flags = DEV_RATE_OFDM,
649 .bitrate = 540,
650 .ratemask = BIT(11),
651 .plcp = 0x0c,
652 .mcs = RATE_MCS(RATE_MODE_OFDM, 7),
656 static void rt2x00lib_channel(struct ieee80211_channel *entry,
657 const int channel, const int tx_power,
658 const int value)
660 entry->center_freq = ieee80211_channel_to_frequency(channel);
661 entry->hw_value = value;
662 entry->max_power = tx_power;
663 entry->max_antenna_gain = 0xff;
666 static void rt2x00lib_rate(struct ieee80211_rate *entry,
667 const u16 index, const struct rt2x00_rate *rate)
669 entry->flags = 0;
670 entry->bitrate = rate->bitrate;
671 entry->hw_value =index;
672 entry->hw_value_short = index;
674 if (rate->flags & DEV_RATE_SHORT_PREAMBLE)
675 entry->flags |= IEEE80211_RATE_SHORT_PREAMBLE;
678 static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
679 struct hw_mode_spec *spec)
681 struct ieee80211_hw *hw = rt2x00dev->hw;
682 struct ieee80211_channel *channels;
683 struct ieee80211_rate *rates;
684 unsigned int num_rates;
685 unsigned int i;
687 num_rates = 0;
688 if (spec->supported_rates & SUPPORT_RATE_CCK)
689 num_rates += 4;
690 if (spec->supported_rates & SUPPORT_RATE_OFDM)
691 num_rates += 8;
693 channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
694 if (!channels)
695 return -ENOMEM;
697 rates = kzalloc(sizeof(*rates) * num_rates, GFP_KERNEL);
698 if (!rates)
699 goto exit_free_channels;
702 * Initialize Rate list.
704 for (i = 0; i < num_rates; i++)
705 rt2x00lib_rate(&rates[i], i, rt2x00_get_rate(i));
708 * Initialize Channel list.
710 for (i = 0; i < spec->num_channels; i++) {
711 rt2x00lib_channel(&channels[i],
712 spec->channels[i].channel,
713 spec->channels_info[i].max_power, i);
717 * Intitialize 802.11b, 802.11g
718 * Rates: CCK, OFDM.
719 * Channels: 2.4 GHz
721 if (spec->supported_bands & SUPPORT_BAND_2GHZ) {
722 rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_channels = 14;
723 rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_bitrates = num_rates;
724 rt2x00dev->bands[IEEE80211_BAND_2GHZ].channels = channels;
725 rt2x00dev->bands[IEEE80211_BAND_2GHZ].bitrates = rates;
726 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
727 &rt2x00dev->bands[IEEE80211_BAND_2GHZ];
728 memcpy(&rt2x00dev->bands[IEEE80211_BAND_2GHZ].ht_cap,
729 &spec->ht, sizeof(spec->ht));
733 * Intitialize 802.11a
734 * Rates: OFDM.
735 * Channels: OFDM, UNII, HiperLAN2.
737 if (spec->supported_bands & SUPPORT_BAND_5GHZ) {
738 rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_channels =
739 spec->num_channels - 14;
740 rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_bitrates =
741 num_rates - 4;
742 rt2x00dev->bands[IEEE80211_BAND_5GHZ].channels = &channels[14];
743 rt2x00dev->bands[IEEE80211_BAND_5GHZ].bitrates = &rates[4];
744 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
745 &rt2x00dev->bands[IEEE80211_BAND_5GHZ];
746 memcpy(&rt2x00dev->bands[IEEE80211_BAND_5GHZ].ht_cap,
747 &spec->ht, sizeof(spec->ht));
750 return 0;
752 exit_free_channels:
753 kfree(channels);
754 ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
755 return -ENOMEM;
758 static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
760 if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
761 ieee80211_unregister_hw(rt2x00dev->hw);
763 if (likely(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ])) {
764 kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->channels);
765 kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->bitrates);
766 rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
767 rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
770 kfree(rt2x00dev->spec.channels_info);
773 static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
775 struct hw_mode_spec *spec = &rt2x00dev->spec;
776 int status;
778 if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
779 return 0;
782 * Initialize HW modes.
784 status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
785 if (status)
786 return status;
789 * Initialize HW fields.
791 rt2x00dev->hw->queues = rt2x00dev->ops->tx_queues;
794 * Initialize extra TX headroom required.
796 rt2x00dev->hw->extra_tx_headroom =
797 max_t(unsigned int, IEEE80211_TX_STATUS_HEADROOM,
798 rt2x00dev->ops->extra_tx_headroom);
801 * Take TX headroom required for alignment into account.
803 if (test_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags))
804 rt2x00dev->hw->extra_tx_headroom += RT2X00_L2PAD_SIZE;
805 else if (test_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags))
806 rt2x00dev->hw->extra_tx_headroom += RT2X00_ALIGN_SIZE;
809 * Register HW.
811 status = ieee80211_register_hw(rt2x00dev->hw);
812 if (status)
813 return status;
815 set_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags);
817 return 0;
821 * Initialization/uninitialization handlers.
823 static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
825 if (!test_and_clear_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
826 return;
829 * Unregister extra components.
831 rt2x00rfkill_unregister(rt2x00dev);
834 * Allow the HW to uninitialize.
836 rt2x00dev->ops->lib->uninitialize(rt2x00dev);
839 * Free allocated queue entries.
841 rt2x00queue_uninitialize(rt2x00dev);
844 static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
846 int status;
848 if (test_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
849 return 0;
852 * Allocate all queue entries.
854 status = rt2x00queue_initialize(rt2x00dev);
855 if (status)
856 return status;
859 * Initialize the device.
861 status = rt2x00dev->ops->lib->initialize(rt2x00dev);
862 if (status) {
863 rt2x00queue_uninitialize(rt2x00dev);
864 return status;
867 set_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags);
870 * Register the extra components.
872 rt2x00rfkill_register(rt2x00dev);
874 return 0;
877 int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
879 int retval;
881 if (test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
882 return 0;
885 * If this is the first interface which is added,
886 * we should load the firmware now.
888 retval = rt2x00lib_load_firmware(rt2x00dev);
889 if (retval)
890 return retval;
893 * Initialize the device.
895 retval = rt2x00lib_initialize(rt2x00dev);
896 if (retval)
897 return retval;
899 rt2x00dev->intf_ap_count = 0;
900 rt2x00dev->intf_sta_count = 0;
901 rt2x00dev->intf_associated = 0;
903 /* Enable the radio */
904 retval = rt2x00lib_enable_radio(rt2x00dev);
905 if (retval) {
906 rt2x00queue_uninitialize(rt2x00dev);
907 return retval;
910 set_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags);
912 return 0;
915 void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
917 if (!test_and_clear_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
918 return;
921 * Perhaps we can add something smarter here,
922 * but for now just disabling the radio should do.
924 rt2x00lib_disable_radio(rt2x00dev);
926 rt2x00dev->intf_ap_count = 0;
927 rt2x00dev->intf_sta_count = 0;
928 rt2x00dev->intf_associated = 0;
932 * driver allocation handlers.
934 int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
936 int retval = -ENOMEM;
938 mutex_init(&rt2x00dev->csr_mutex);
940 set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
943 * Make room for rt2x00_intf inside the per-interface
944 * structure ieee80211_vif.
946 rt2x00dev->hw->vif_data_size = sizeof(struct rt2x00_intf);
949 * Determine which operating modes are supported, all modes
950 * which require beaconing, depend on the availability of
951 * beacon entries.
953 rt2x00dev->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
954 if (rt2x00dev->ops->bcn->entry_num > 0)
955 rt2x00dev->hw->wiphy->interface_modes |=
956 BIT(NL80211_IFTYPE_ADHOC) |
957 BIT(NL80211_IFTYPE_AP) |
958 BIT(NL80211_IFTYPE_MESH_POINT) |
959 BIT(NL80211_IFTYPE_WDS);
962 * Initialize configuration work.
964 INIT_WORK(&rt2x00dev->intf_work, rt2x00lib_intf_scheduled);
967 * Let the driver probe the device to detect the capabilities.
969 retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
970 if (retval) {
971 ERROR(rt2x00dev, "Failed to allocate device.\n");
972 goto exit;
976 * Allocate queue array.
978 retval = rt2x00queue_allocate(rt2x00dev);
979 if (retval)
980 goto exit;
983 * Initialize ieee80211 structure.
985 retval = rt2x00lib_probe_hw(rt2x00dev);
986 if (retval) {
987 ERROR(rt2x00dev, "Failed to initialize hw.\n");
988 goto exit;
992 * Register extra components.
994 rt2x00link_register(rt2x00dev);
995 rt2x00leds_register(rt2x00dev);
996 rt2x00debug_register(rt2x00dev);
998 return 0;
1000 exit:
1001 rt2x00lib_remove_dev(rt2x00dev);
1003 return retval;
1005 EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
1007 void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
1009 clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
1012 * Disable radio.
1014 rt2x00lib_disable_radio(rt2x00dev);
1017 * Stop all work.
1019 cancel_work_sync(&rt2x00dev->intf_work);
1022 * Uninitialize device.
1024 rt2x00lib_uninitialize(rt2x00dev);
1027 * Free extra components
1029 rt2x00debug_deregister(rt2x00dev);
1030 rt2x00leds_unregister(rt2x00dev);
1033 * Free ieee80211_hw memory.
1035 rt2x00lib_remove_hw(rt2x00dev);
1038 * Free firmware image.
1040 rt2x00lib_free_firmware(rt2x00dev);
1043 * Free queue structures.
1045 rt2x00queue_free(rt2x00dev);
1047 EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
1050 * Device state handlers
1052 #ifdef CONFIG_PM
1053 int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
1055 NOTICE(rt2x00dev, "Going to sleep.\n");
1058 * Prevent mac80211 from accessing driver while suspended.
1060 if (!test_and_clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
1061 return 0;
1064 * Cleanup as much as possible.
1066 rt2x00lib_uninitialize(rt2x00dev);
1069 * Suspend/disable extra components.
1071 rt2x00leds_suspend(rt2x00dev);
1072 rt2x00debug_deregister(rt2x00dev);
1075 * Set device mode to sleep for power management,
1076 * on some hardware this call seems to consistently fail.
1077 * From the specifications it is hard to tell why it fails,
1078 * and if this is a "bad thing".
1079 * Overall it is safe to just ignore the failure and
1080 * continue suspending. The only downside is that the
1081 * device will not be in optimal power save mode, but with
1082 * the radio and the other components already disabled the
1083 * device is as good as disabled.
1085 if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP))
1086 WARNING(rt2x00dev, "Device failed to enter sleep state, "
1087 "continue suspending.\n");
1089 return 0;
1091 EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1093 int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1095 NOTICE(rt2x00dev, "Waking up.\n");
1098 * Restore/enable extra components.
1100 rt2x00debug_register(rt2x00dev);
1101 rt2x00leds_resume(rt2x00dev);
1104 * We are ready again to receive requests from mac80211.
1106 set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
1108 return 0;
1110 EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1111 #endif /* CONFIG_PM */
1114 * rt2x00lib module information.
1116 MODULE_AUTHOR(DRV_PROJECT);
1117 MODULE_VERSION(DRV_VERSION);
1118 MODULE_DESCRIPTION("rt2x00 library");
1119 MODULE_LICENSE("GPL");