rt2x00: fix cancelling uninitialized work
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / rt2x00 / rt2x00dev.c
blob84eb6ad3637752c686a9cd8d1be0e058cacda162
1 /*
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.
23 Module: rt2x00lib
24 Abstract: rt2x00 generic device routines.
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
31 #include "rt2x00.h"
32 #include "rt2x00lib.h"
35 * Radio control handlers.
37 int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
39 int status;
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))
46 return 0;
49 * Initialize all data queues.
51 rt2x00queue_init_queues(rt2x00dev);
54 * Enable radio.
56 status =
57 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_ON);
58 if (status)
59 return status;
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);
69 * Enable queues.
71 rt2x00queue_start_queues(rt2x00dev);
72 rt2x00link_start_tuner(rt2x00dev);
75 * Start watchdog monitoring.
77 rt2x00link_start_watchdog(rt2x00dev);
79 return 0;
82 void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
84 if (!test_and_clear_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
85 return;
88 * Stop watchdog monitoring.
90 rt2x00link_stop_watchdog(rt2x00dev);
93 * Stop all queues
95 rt2x00link_stop_tuner(rt2x00dev);
96 rt2x00queue_stop_queues(rt2x00dev);
97 rt2x00queue_flush_queues(rt2x00dev, true);
100 * Disable radio.
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))
121 return;
123 if (test_and_clear_bit(DELAYED_UPDATE_BEACON, &intf->delayed_flags))
124 rt2x00queue_update_beacon(rt2x00dev, vif);
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,
138 rt2x00dev);
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;
148 struct sk_buff *skb;
151 * Only AP mode interfaces do broad- and multicast buffering
153 if (vif->type != NL80211_IFTYPE_AP)
154 return;
157 * Send out buffered broad- and multicast frames
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);
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)
175 return;
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.
182 WARN_ON(rt2x00_is_usb(rt2x00dev));
183 rt2x00queue_update_beacon_locked(rt2x00dev, vif);
186 void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
188 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
189 return;
191 /* send buffered bc/mc frames out for every bssid */
192 ieee80211_iterate_active_interfaces_atomic(rt2x00dev->hw,
193 rt2x00lib_bc_buffer_iter,
194 rt2x00dev);
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.
200 if (test_bit(DRIVER_SUPPORT_PRE_TBTT_INTERRUPT, &rt2x00dev->flags))
201 return;
203 /* fetch next beacon */
204 ieee80211_iterate_active_interfaces_atomic(rt2x00dev->hw,
205 rt2x00lib_beaconupdate_iter,
206 rt2x00dev);
208 EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
210 void rt2x00lib_pretbtt(struct rt2x00_dev *rt2x00dev)
212 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
213 return;
215 /* fetch next beacon */
216 ieee80211_iterate_active_interfaces_atomic(rt2x00dev->hw,
217 rt2x00lib_beaconupdate_iter,
218 rt2x00dev);
220 EXPORT_SYMBOL_GPL(rt2x00lib_pretbtt);
222 void rt2x00lib_dmastart(struct queue_entry *entry)
224 set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
225 rt2x00queue_index_inc(entry->queue, Q_INDEX);
227 EXPORT_SYMBOL_GPL(rt2x00lib_dmastart);
229 void rt2x00lib_dmadone(struct queue_entry *entry)
231 set_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags);
232 clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
233 rt2x00queue_index_inc(entry->queue, Q_INDEX_DMA_DONE);
235 EXPORT_SYMBOL_GPL(rt2x00lib_dmadone);
237 void rt2x00lib_txdone(struct queue_entry *entry,
238 struct txdone_entry_desc *txdesc)
240 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
241 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
242 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
243 unsigned int header_length, i;
244 u8 rate_idx, rate_flags, retry_rates;
245 u8 skbdesc_flags = skbdesc->flags;
246 bool success;
249 * Unmap the skb.
251 rt2x00queue_unmap_skb(entry);
254 * Remove the extra tx headroom from the skb.
256 skb_pull(entry->skb, rt2x00dev->ops->extra_tx_headroom);
259 * Signal that the TX descriptor is no longer in the skb.
261 skbdesc->flags &= ~SKBDESC_DESC_IN_SKB;
264 * Determine the length of 802.11 header.
266 header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
269 * Remove L2 padding which was added during
271 if (test_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags))
272 rt2x00queue_remove_l2pad(entry->skb, header_length);
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
277 * mac80211 will expect the same data to be present it the
278 * frame as it was passed to us.
280 if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags))
281 rt2x00crypto_tx_insert_iv(entry->skb, header_length);
284 * Send frame to debugfs immediately, after this call is completed
285 * we are going to overwrite the skb->cb array.
287 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TXDONE, entry->skb);
290 * Determine if the frame has been successfully transmitted.
292 success =
293 test_bit(TXDONE_SUCCESS, &txdesc->flags) ||
294 test_bit(TXDONE_UNKNOWN, &txdesc->flags);
297 * Update TX statistics.
299 rt2x00dev->link.qual.tx_success += success;
300 rt2x00dev->link.qual.tx_failed += !success;
302 rate_idx = skbdesc->tx_rate_idx;
303 rate_flags = skbdesc->tx_rate_flags;
304 retry_rates = test_bit(TXDONE_FALLBACK, &txdesc->flags) ?
305 (txdesc->retry + 1) : 1;
308 * Initialize TX status
310 memset(&tx_info->status, 0, sizeof(tx_info->status));
311 tx_info->status.ack_signal = 0;
314 * Frame was send with retries, hardware tried
315 * different rates to send out the frame, at each
316 * retry it lowered the rate 1 step except when the
317 * lowest rate was used.
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;
323 if (rate_idx - i == 0) {
325 * The lowest rate (index 0) was used until the
326 * number of max retries was reached.
328 tx_info->status.rates[i].count = retry_rates - i;
329 i++;
330 break;
332 tx_info->status.rates[i].count = 1;
334 if (i < (IEEE80211_TX_MAX_RATES - 1))
335 tx_info->status.rates[i].idx = -1; /* terminate */
337 if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK)) {
338 if (success)
339 tx_info->flags |= IEEE80211_TX_STAT_ACK;
340 else
341 rt2x00dev->low_level_stats.dot11ACKFailureCount++;
345 * Every single frame has it's own tx status, hence report
346 * every frame as ampdu of size 1.
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.
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;
359 if (rate_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
360 if (success)
361 rt2x00dev->low_level_stats.dot11RTSSuccessCount++;
362 else
363 rt2x00dev->low_level_stats.dot11RTSFailureCount++;
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.
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
378 dev_kfree_skb_any(entry->skb);
381 * Make this entry available for reuse.
383 entry->skb = NULL;
384 entry->flags = 0;
386 rt2x00dev->ops->lib->clear_entry(entry);
388 rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
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.
395 if (!rt2x00queue_threshold(entry->queue))
396 rt2x00queue_unpause_queue(entry->queue);
398 EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
400 void rt2x00lib_txdone_noinfo(struct queue_entry *entry, u32 status)
402 struct txdone_entry_desc txdesc;
404 txdesc.flags = 0;
405 __set_bit(status, &txdesc.flags);
406 txdesc.retry = 0;
408 rt2x00lib_txdone(entry, &txdesc);
410 EXPORT_SYMBOL_GPL(rt2x00lib_txdone_noinfo);
412 static int rt2x00lib_rxdone_read_signal(struct rt2x00_dev *rt2x00dev,
413 struct rxdone_entry_desc *rxdesc)
415 struct ieee80211_supported_band *sband;
416 const struct rt2x00_rate *rate;
417 unsigned int i;
418 int signal = rxdesc->signal;
419 int type = (rxdesc->dev_flags & RXDONE_SIGNAL_MASK);
421 switch (rxdesc->rate_mode) {
422 case RATE_MODE_CCK:
423 case RATE_MODE_OFDM:
425 * For non-HT rates the MCS value needs to contain the
426 * actually used rate modulation (CCK or OFDM).
428 if (rxdesc->dev_flags & RXDONE_SIGNAL_MCS)
429 signal = RATE_MCS(rxdesc->rate_mode, signal);
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;
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;
453 WARNING(rt2x00dev, "Frame received with unrecognized signal, "
454 "mode=0x%.4x, signal=0x%.4x, type=%d.\n",
455 rxdesc->rate_mode, signal, type);
456 return 0;
459 void rt2x00lib_rxdone(struct queue_entry *entry)
461 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
462 struct rxdone_entry_desc rxdesc;
463 struct sk_buff *skb;
464 struct ieee80211_rx_status *rx_status;
465 unsigned int header_length;
466 int rate_idx;
468 if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) ||
469 !test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
470 goto submit_entry;
472 if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags))
473 goto submit_entry;
476 * Allocate a new sk_buffer. If no new buffer available, drop the
477 * received frame and reuse the existing buffer.
479 skb = rt2x00queue_alloc_rxskb(entry);
480 if (!skb)
481 goto submit_entry;
484 * Unmap the skb.
486 rt2x00queue_unmap_skb(entry);
489 * Extract the RXD details.
491 memset(&rxdesc, 0, sizeof(rxdesc));
492 rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
495 * The data behind the ieee80211 header must be
496 * aligned on a 4 byte boundary.
498 header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
501 * Hardware might have stripped the IV/EIV/ICV data,
502 * in that case it is possible that the data was
503 * provided separately (through hardware descriptor)
504 * in which case we should reinsert the data into the frame.
506 if ((rxdesc.dev_flags & RXDONE_CRYPTO_IV) &&
507 (rxdesc.flags & RX_FLAG_IV_STRIPPED))
508 rt2x00crypto_rx_insert_iv(entry->skb, header_length,
509 &rxdesc);
510 else if (header_length &&
511 (rxdesc.size > header_length) &&
512 (rxdesc.dev_flags & RXDONE_L2PAD))
513 rt2x00queue_remove_l2pad(entry->skb, header_length);
514 else
515 rt2x00queue_align_payload(entry->skb, header_length);
517 /* Trim buffer to correct size */
518 skb_trim(entry->skb, rxdesc.size);
521 * Translate the signal to the correct bitrate index.
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)
526 rxdesc.flags |= RX_FLAG_HT;
529 * Update extra components
531 rt2x00link_update_stats(rt2x00dev, entry->skb, &rxdesc);
532 rt2x00debug_update_crypto(rt2x00dev, &rxdesc);
533 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_RXDONE, entry->skb);
536 * Initialize RX status information, and send frame
537 * to mac80211.
539 rx_status = IEEE80211_SKB_RXCB(entry->skb);
540 rx_status->mactime = rxdesc.timestamp;
541 rx_status->band = rt2x00dev->curr_band;
542 rx_status->freq = rt2x00dev->curr_freq;
543 rx_status->rate_idx = rate_idx;
544 rx_status->signal = rxdesc.rssi;
545 rx_status->flag = rxdesc.flags;
546 rx_status->antenna = rt2x00dev->link.ant.active.rx;
548 ieee80211_rx_ni(rt2x00dev->hw, entry->skb);
551 * Replace the skb with the freshly allocated one.
553 entry->skb = skb;
555 submit_entry:
556 entry->flags = 0;
557 rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
558 if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) &&
559 test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
560 rt2x00dev->ops->lib->clear_entry(entry);
562 EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
565 * Driver initialization handlers.
567 const struct rt2x00_rate rt2x00_supported_rates[12] = {
569 .flags = DEV_RATE_CCK,
570 .bitrate = 10,
571 .ratemask = BIT(0),
572 .plcp = 0x00,
573 .mcs = RATE_MCS(RATE_MODE_CCK, 0),
576 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
577 .bitrate = 20,
578 .ratemask = BIT(1),
579 .plcp = 0x01,
580 .mcs = RATE_MCS(RATE_MODE_CCK, 1),
583 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
584 .bitrate = 55,
585 .ratemask = BIT(2),
586 .plcp = 0x02,
587 .mcs = RATE_MCS(RATE_MODE_CCK, 2),
590 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
591 .bitrate = 110,
592 .ratemask = BIT(3),
593 .plcp = 0x03,
594 .mcs = RATE_MCS(RATE_MODE_CCK, 3),
597 .flags = DEV_RATE_OFDM,
598 .bitrate = 60,
599 .ratemask = BIT(4),
600 .plcp = 0x0b,
601 .mcs = RATE_MCS(RATE_MODE_OFDM, 0),
604 .flags = DEV_RATE_OFDM,
605 .bitrate = 90,
606 .ratemask = BIT(5),
607 .plcp = 0x0f,
608 .mcs = RATE_MCS(RATE_MODE_OFDM, 1),
611 .flags = DEV_RATE_OFDM,
612 .bitrate = 120,
613 .ratemask = BIT(6),
614 .plcp = 0x0a,
615 .mcs = RATE_MCS(RATE_MODE_OFDM, 2),
618 .flags = DEV_RATE_OFDM,
619 .bitrate = 180,
620 .ratemask = BIT(7),
621 .plcp = 0x0e,
622 .mcs = RATE_MCS(RATE_MODE_OFDM, 3),
625 .flags = DEV_RATE_OFDM,
626 .bitrate = 240,
627 .ratemask = BIT(8),
628 .plcp = 0x09,
629 .mcs = RATE_MCS(RATE_MODE_OFDM, 4),
632 .flags = DEV_RATE_OFDM,
633 .bitrate = 360,
634 .ratemask = BIT(9),
635 .plcp = 0x0d,
636 .mcs = RATE_MCS(RATE_MODE_OFDM, 5),
639 .flags = DEV_RATE_OFDM,
640 .bitrate = 480,
641 .ratemask = BIT(10),
642 .plcp = 0x08,
643 .mcs = RATE_MCS(RATE_MODE_OFDM, 6),
646 .flags = DEV_RATE_OFDM,
647 .bitrate = 540,
648 .ratemask = BIT(11),
649 .plcp = 0x0c,
650 .mcs = RATE_MCS(RATE_MODE_OFDM, 7),
654 static void rt2x00lib_channel(struct ieee80211_channel *entry,
655 const int channel, const int tx_power,
656 const int value)
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);
662 entry->hw_value = value;
663 entry->max_power = tx_power;
664 entry->max_antenna_gain = 0xff;
667 static void rt2x00lib_rate(struct ieee80211_rate *entry,
668 const u16 index, const struct rt2x00_rate *rate)
670 entry->flags = 0;
671 entry->bitrate = rate->bitrate;
672 entry->hw_value = index;
673 entry->hw_value_short = index;
675 if (rate->flags & DEV_RATE_SHORT_PREAMBLE)
676 entry->flags |= IEEE80211_RATE_SHORT_PREAMBLE;
679 static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
680 struct hw_mode_spec *spec)
682 struct ieee80211_hw *hw = rt2x00dev->hw;
683 struct ieee80211_channel *channels;
684 struct ieee80211_rate *rates;
685 unsigned int num_rates;
686 unsigned int i;
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;
694 channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
695 if (!channels)
696 return -ENOMEM;
698 rates = kzalloc(sizeof(*rates) * num_rates, GFP_KERNEL);
699 if (!rates)
700 goto exit_free_channels;
703 * Initialize Rate list.
705 for (i = 0; i < num_rates; i++)
706 rt2x00lib_rate(&rates[i], i, rt2x00_get_rate(i));
709 * Initialize Channel list.
711 for (i = 0; i < spec->num_channels; i++) {
712 rt2x00lib_channel(&channels[i],
713 spec->channels[i].channel,
714 spec->channels_info[i].max_power, i);
718 * Intitialize 802.11b, 802.11g
719 * Rates: CCK, OFDM.
720 * Channels: 2.4 GHz
722 if (spec->supported_bands & SUPPORT_BAND_2GHZ) {
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];
729 memcpy(&rt2x00dev->bands[IEEE80211_BAND_2GHZ].ht_cap,
730 &spec->ht, sizeof(spec->ht));
734 * Intitialize 802.11a
735 * Rates: OFDM.
736 * Channels: OFDM, UNII, HiperLAN2.
738 if (spec->supported_bands & SUPPORT_BAND_5GHZ) {
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];
747 memcpy(&rt2x00dev->bands[IEEE80211_BAND_5GHZ].ht_cap,
748 &spec->ht, sizeof(spec->ht));
751 return 0;
753 exit_free_channels:
754 kfree(channels);
755 ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
756 return -ENOMEM;
759 static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
761 if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
762 ieee80211_unregister_hw(rt2x00dev->hw);
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;
771 kfree(rt2x00dev->spec.channels_info);
774 static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
776 struct hw_mode_spec *spec = &rt2x00dev->spec;
777 int status;
779 if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
780 return 0;
783 * Initialize HW modes.
785 status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
786 if (status)
787 return status;
790 * Initialize HW fields.
792 rt2x00dev->hw->queues = rt2x00dev->ops->tx_queues;
795 * Initialize extra TX headroom required.
797 rt2x00dev->hw->extra_tx_headroom =
798 max_t(unsigned int, IEEE80211_TX_STATUS_HEADROOM,
799 rt2x00dev->ops->extra_tx_headroom);
802 * Take TX headroom required for alignment into account.
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;
810 * Allocate tx status FIFO for driver use.
812 if (test_bit(DRIVER_REQUIRE_TXSTATUS_FIFO, &rt2x00dev->flags)) {
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.
820 status = kfifo_alloc(&rt2x00dev->txstatus_fifo, 512,
821 GFP_KERNEL);
822 if (status)
823 return status;
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.
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); \
839 RT2X00_TASKLET_INIT(txstatus_tasklet);
840 RT2X00_TASKLET_INIT(pretbtt_tasklet);
841 RT2X00_TASKLET_INIT(tbtt_tasklet);
842 RT2X00_TASKLET_INIT(rxdone_tasklet);
843 RT2X00_TASKLET_INIT(autowake_tasklet);
845 #undef RT2X00_TASKLET_INIT
848 * Register HW.
850 status = ieee80211_register_hw(rt2x00dev->hw);
851 if (status)
852 return status;
854 set_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags);
856 return 0;
860 * Initialization/uninitialization handlers.
862 static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
864 if (!test_and_clear_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
865 return;
868 * Unregister extra components.
870 rt2x00rfkill_unregister(rt2x00dev);
873 * Allow the HW to uninitialize.
875 rt2x00dev->ops->lib->uninitialize(rt2x00dev);
878 * Free allocated queue entries.
880 rt2x00queue_uninitialize(rt2x00dev);
883 static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
885 int status;
887 if (test_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
888 return 0;
891 * Allocate all queue entries.
893 status = rt2x00queue_initialize(rt2x00dev);
894 if (status)
895 return status;
898 * Initialize the device.
900 status = rt2x00dev->ops->lib->initialize(rt2x00dev);
901 if (status) {
902 rt2x00queue_uninitialize(rt2x00dev);
903 return status;
906 set_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags);
909 * Register the extra components.
911 rt2x00rfkill_register(rt2x00dev);
913 return 0;
916 int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
918 int retval;
920 if (test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
921 return 0;
924 * If this is the first interface which is added,
925 * we should load the firmware now.
927 retval = rt2x00lib_load_firmware(rt2x00dev);
928 if (retval)
929 return retval;
932 * Initialize the device.
934 retval = rt2x00lib_initialize(rt2x00dev);
935 if (retval)
936 return retval;
938 rt2x00dev->intf_ap_count = 0;
939 rt2x00dev->intf_sta_count = 0;
940 rt2x00dev->intf_associated = 0;
942 /* Enable the radio */
943 retval = rt2x00lib_enable_radio(rt2x00dev);
944 if (retval)
945 return retval;
947 set_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags);
949 return 0;
952 void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
954 if (!test_and_clear_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
955 return;
958 * Perhaps we can add something smarter here,
959 * but for now just disabling the radio should do.
961 rt2x00lib_disable_radio(rt2x00dev);
963 rt2x00dev->intf_ap_count = 0;
964 rt2x00dev->intf_sta_count = 0;
965 rt2x00dev->intf_associated = 0;
969 * driver allocation handlers.
971 int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
973 int retval = -ENOMEM;
975 spin_lock_init(&rt2x00dev->irqmask_lock);
976 mutex_init(&rt2x00dev->csr_mutex);
978 set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
981 * Make room for rt2x00_intf inside the per-interface
982 * structure ieee80211_vif.
984 rt2x00dev->hw->vif_data_size = sizeof(struct rt2x00_intf);
987 * Determine which operating modes are supported, all modes
988 * which require beaconing, depend on the availability of
989 * beacon entries.
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) |
995 BIT(NL80211_IFTYPE_AP) |
996 BIT(NL80211_IFTYPE_MESH_POINT) |
997 BIT(NL80211_IFTYPE_WDS);
1000 * Initialize work.
1002 rt2x00dev->workqueue =
1003 alloc_ordered_workqueue(wiphy_name(rt2x00dev->hw->wiphy), 0);
1004 if (!rt2x00dev->workqueue) {
1005 retval = -ENOMEM;
1006 goto exit;
1009 INIT_WORK(&rt2x00dev->intf_work, rt2x00lib_intf_scheduled);
1012 * Let the driver probe the device to detect the capabilities.
1014 retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
1015 if (retval) {
1016 ERROR(rt2x00dev, "Failed to allocate device.\n");
1017 goto exit;
1021 * Allocate queue array.
1023 retval = rt2x00queue_allocate(rt2x00dev);
1024 if (retval)
1025 goto exit;
1028 * Initialize ieee80211 structure.
1030 retval = rt2x00lib_probe_hw(rt2x00dev);
1031 if (retval) {
1032 ERROR(rt2x00dev, "Failed to initialize hw.\n");
1033 goto exit;
1037 * Register extra components.
1039 rt2x00link_register(rt2x00dev);
1040 rt2x00leds_register(rt2x00dev);
1041 rt2x00debug_register(rt2x00dev);
1043 return 0;
1045 exit:
1046 rt2x00lib_remove_dev(rt2x00dev);
1048 return retval;
1050 EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
1052 void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
1054 clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
1057 * Disable radio.
1059 rt2x00lib_disable_radio(rt2x00dev);
1062 * Stop all work.
1064 cancel_work_sync(&rt2x00dev->intf_work);
1065 if (rt2x00_is_usb(rt2x00dev)) {
1066 cancel_work_sync(&rt2x00dev->rxdone_work);
1067 cancel_work_sync(&rt2x00dev->txdone_work);
1069 destroy_workqueue(rt2x00dev->workqueue);
1072 * Free the tx status fifo.
1074 kfifo_free(&rt2x00dev->txstatus_fifo);
1077 * Kill the tx status tasklet.
1079 tasklet_kill(&rt2x00dev->txstatus_tasklet);
1080 tasklet_kill(&rt2x00dev->pretbtt_tasklet);
1081 tasklet_kill(&rt2x00dev->tbtt_tasklet);
1082 tasklet_kill(&rt2x00dev->rxdone_tasklet);
1083 tasklet_kill(&rt2x00dev->autowake_tasklet);
1086 * Uninitialize device.
1088 rt2x00lib_uninitialize(rt2x00dev);
1091 * Free extra components
1093 rt2x00debug_deregister(rt2x00dev);
1094 rt2x00leds_unregister(rt2x00dev);
1097 * Free ieee80211_hw memory.
1099 rt2x00lib_remove_hw(rt2x00dev);
1102 * Free firmware image.
1104 rt2x00lib_free_firmware(rt2x00dev);
1107 * Free queue structures.
1109 rt2x00queue_free(rt2x00dev);
1111 EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
1114 * Device state handlers
1116 #ifdef CONFIG_PM
1117 int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
1119 NOTICE(rt2x00dev, "Going to sleep.\n");
1122 * Prevent mac80211 from accessing driver while suspended.
1124 if (!test_and_clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
1125 return 0;
1128 * Cleanup as much as possible.
1130 rt2x00lib_uninitialize(rt2x00dev);
1133 * Suspend/disable extra components.
1135 rt2x00leds_suspend(rt2x00dev);
1136 rt2x00debug_deregister(rt2x00dev);
1139 * Set device mode to sleep for power management,
1140 * on some hardware this call seems to consistently fail.
1141 * From the specifications it is hard to tell why it fails,
1142 * and if this is a "bad thing".
1143 * Overall it is safe to just ignore the failure and
1144 * continue suspending. The only downside is that the
1145 * device will not be in optimal power save mode, but with
1146 * the radio and the other components already disabled the
1147 * device is as good as disabled.
1149 if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP))
1150 WARNING(rt2x00dev, "Device failed to enter sleep state, "
1151 "continue suspending.\n");
1153 return 0;
1155 EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1157 int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1159 NOTICE(rt2x00dev, "Waking up.\n");
1162 * Restore/enable extra components.
1164 rt2x00debug_register(rt2x00dev);
1165 rt2x00leds_resume(rt2x00dev);
1168 * We are ready again to receive requests from mac80211.
1170 set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
1172 return 0;
1174 EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1175 #endif /* CONFIG_PM */
1178 * rt2x00lib module information.
1180 MODULE_AUTHOR(DRV_PROJECT);
1181 MODULE_VERSION(DRV_VERSION);
1182 MODULE_DESCRIPTION("rt2x00 library");
1183 MODULE_LICENSE("GPL");