rt2x00: Fix rmmod hang of rt2800pci
[linux-2.6/btrfs-unstable.git] / drivers / net / wireless / rt2x00 / rt2x00dev.c
blobc018d67aab8e40b7a5c719d04f14eb52a4853d9b
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>
30 #include <linux/log2.h>
32 #include "rt2x00.h"
33 #include "rt2x00lib.h"
36 * Radio control handlers.
38 int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
40 int status;
43 * Don't enable the radio twice.
44 * And check if the hardware button has been disabled.
46 if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
47 return 0;
50 * Initialize all data queues.
52 rt2x00queue_init_queues(rt2x00dev);
55 * Enable radio.
57 status =
58 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_ON);
59 if (status)
60 return status;
62 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_ON);
64 rt2x00leds_led_radio(rt2x00dev, true);
65 rt2x00led_led_activity(rt2x00dev, true);
67 set_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags);
70 * Enable queues.
72 rt2x00queue_start_queues(rt2x00dev);
73 rt2x00link_start_tuner(rt2x00dev);
74 rt2x00link_start_agc(rt2x00dev);
77 * Start watchdog monitoring.
79 rt2x00link_start_watchdog(rt2x00dev);
81 return 0;
84 void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
86 if (!test_and_clear_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
87 return;
90 * Stop watchdog monitoring.
92 rt2x00link_stop_watchdog(rt2x00dev);
95 * Stop all queues
97 rt2x00link_stop_agc(rt2x00dev);
98 rt2x00link_stop_tuner(rt2x00dev);
99 rt2x00queue_stop_queues(rt2x00dev);
100 rt2x00queue_flush_queues(rt2x00dev, true);
103 * Disable radio.
105 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
106 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_OFF);
107 rt2x00led_led_activity(rt2x00dev, false);
108 rt2x00leds_led_radio(rt2x00dev, false);
111 static void rt2x00lib_intf_scheduled_iter(void *data, u8 *mac,
112 struct ieee80211_vif *vif)
114 struct rt2x00_dev *rt2x00dev = data;
115 struct rt2x00_intf *intf = vif_to_intf(vif);
118 * It is possible the radio was disabled while the work had been
119 * scheduled. If that happens we should return here immediately,
120 * note that in the spinlock protected area above the delayed_flags
121 * have been cleared correctly.
123 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
124 return;
126 if (test_and_clear_bit(DELAYED_UPDATE_BEACON, &intf->delayed_flags))
127 rt2x00queue_update_beacon(rt2x00dev, vif);
130 static void rt2x00lib_intf_scheduled(struct work_struct *work)
132 struct rt2x00_dev *rt2x00dev =
133 container_of(work, struct rt2x00_dev, intf_work);
136 * Iterate over each interface and perform the
137 * requested configurations.
139 ieee80211_iterate_active_interfaces(rt2x00dev->hw,
140 rt2x00lib_intf_scheduled_iter,
141 rt2x00dev);
144 static void rt2x00lib_autowakeup(struct work_struct *work)
146 struct rt2x00_dev *rt2x00dev =
147 container_of(work, struct rt2x00_dev, autowakeup_work.work);
149 if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_AWAKE))
150 ERROR(rt2x00dev, "Device failed to wakeup.\n");
151 clear_bit(CONFIG_POWERSAVING, &rt2x00dev->flags);
155 * Interrupt context handlers.
157 static void rt2x00lib_bc_buffer_iter(void *data, u8 *mac,
158 struct ieee80211_vif *vif)
160 struct rt2x00_dev *rt2x00dev = data;
161 struct sk_buff *skb;
164 * Only AP mode interfaces do broad- and multicast buffering
166 if (vif->type != NL80211_IFTYPE_AP)
167 return;
170 * Send out buffered broad- and multicast frames
172 skb = ieee80211_get_buffered_bc(rt2x00dev->hw, vif);
173 while (skb) {
174 rt2x00mac_tx(rt2x00dev->hw, skb);
175 skb = ieee80211_get_buffered_bc(rt2x00dev->hw, vif);
179 static void rt2x00lib_beaconupdate_iter(void *data, u8 *mac,
180 struct ieee80211_vif *vif)
182 struct rt2x00_dev *rt2x00dev = data;
184 if (vif->type != NL80211_IFTYPE_AP &&
185 vif->type != NL80211_IFTYPE_ADHOC &&
186 vif->type != NL80211_IFTYPE_MESH_POINT &&
187 vif->type != NL80211_IFTYPE_WDS)
188 return;
191 * Update the beacon without locking. This is safe on PCI devices
192 * as they only update the beacon periodically here. This should
193 * never be called for USB devices.
195 WARN_ON(rt2x00_is_usb(rt2x00dev));
196 rt2x00queue_update_beacon_locked(rt2x00dev, vif);
199 void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
201 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
202 return;
204 /* send buffered bc/mc frames out for every bssid */
205 ieee80211_iterate_active_interfaces_atomic(rt2x00dev->hw,
206 rt2x00lib_bc_buffer_iter,
207 rt2x00dev);
209 * Devices with pre tbtt interrupt don't need to update the beacon
210 * here as they will fetch the next beacon directly prior to
211 * transmission.
213 if (test_bit(CAPABILITY_PRE_TBTT_INTERRUPT, &rt2x00dev->cap_flags))
214 return;
216 /* fetch next beacon */
217 ieee80211_iterate_active_interfaces_atomic(rt2x00dev->hw,
218 rt2x00lib_beaconupdate_iter,
219 rt2x00dev);
221 EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
223 void rt2x00lib_pretbtt(struct rt2x00_dev *rt2x00dev)
225 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
226 return;
228 /* fetch next beacon */
229 ieee80211_iterate_active_interfaces_atomic(rt2x00dev->hw,
230 rt2x00lib_beaconupdate_iter,
231 rt2x00dev);
233 EXPORT_SYMBOL_GPL(rt2x00lib_pretbtt);
235 void rt2x00lib_dmastart(struct queue_entry *entry)
237 set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
238 rt2x00queue_index_inc(entry, Q_INDEX);
240 EXPORT_SYMBOL_GPL(rt2x00lib_dmastart);
242 void rt2x00lib_dmadone(struct queue_entry *entry)
244 set_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags);
245 clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
246 rt2x00queue_index_inc(entry, Q_INDEX_DMA_DONE);
248 EXPORT_SYMBOL_GPL(rt2x00lib_dmadone);
250 void rt2x00lib_txdone(struct queue_entry *entry,
251 struct txdone_entry_desc *txdesc)
253 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
254 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
255 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
256 unsigned int header_length, i;
257 u8 rate_idx, rate_flags, retry_rates;
258 u8 skbdesc_flags = skbdesc->flags;
259 bool success;
262 * Unmap the skb.
264 rt2x00queue_unmap_skb(entry);
267 * Remove the extra tx headroom from the skb.
269 skb_pull(entry->skb, rt2x00dev->ops->extra_tx_headroom);
272 * Signal that the TX descriptor is no longer in the skb.
274 skbdesc->flags &= ~SKBDESC_DESC_IN_SKB;
277 * Determine the length of 802.11 header.
279 header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
282 * Remove L2 padding which was added during
284 if (test_bit(REQUIRE_L2PAD, &rt2x00dev->cap_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(CAPABILITY_HW_CRYPTO, &rt2x00dev->cap_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 (test_bit(TXDONE_AMPDU, &txdesc->flags) ||
367 tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
368 tx_info->flags |= IEEE80211_TX_STAT_AMPDU;
369 tx_info->status.ampdu_len = 1;
370 tx_info->status.ampdu_ack_len = success ? 1 : 0;
372 if (!success)
373 tx_info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
376 if (rate_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
377 if (success)
378 rt2x00dev->low_level_stats.dot11RTSSuccessCount++;
379 else
380 rt2x00dev->low_level_stats.dot11RTSFailureCount++;
384 * Only send the status report to mac80211 when it's a frame
385 * that originated in mac80211. If this was a extra frame coming
386 * through a mac80211 library call (RTS/CTS) then we should not
387 * send the status report back.
389 if (!(skbdesc_flags & SKBDESC_NOT_MAC80211)) {
390 if (test_bit(REQUIRE_TASKLET_CONTEXT, &rt2x00dev->cap_flags))
391 ieee80211_tx_status(rt2x00dev->hw, entry->skb);
392 else
393 ieee80211_tx_status_ni(rt2x00dev->hw, entry->skb);
394 } else
395 dev_kfree_skb_any(entry->skb);
398 * Make this entry available for reuse.
400 entry->skb = NULL;
401 entry->flags = 0;
403 rt2x00dev->ops->lib->clear_entry(entry);
405 rt2x00queue_index_inc(entry, Q_INDEX_DONE);
408 * If the data queue was below the threshold before the txdone
409 * handler we must make sure the packet queue in the mac80211 stack
410 * is reenabled when the txdone handler has finished.
412 if (!rt2x00queue_threshold(entry->queue))
413 rt2x00queue_unpause_queue(entry->queue);
415 EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
417 void rt2x00lib_txdone_noinfo(struct queue_entry *entry, u32 status)
419 struct txdone_entry_desc txdesc;
421 txdesc.flags = 0;
422 __set_bit(status, &txdesc.flags);
423 txdesc.retry = 0;
425 rt2x00lib_txdone(entry, &txdesc);
427 EXPORT_SYMBOL_GPL(rt2x00lib_txdone_noinfo);
429 static u8 *rt2x00lib_find_ie(u8 *data, unsigned int len, u8 ie)
431 struct ieee80211_mgmt *mgmt = (void *)data;
432 u8 *pos, *end;
434 pos = (u8 *)mgmt->u.beacon.variable;
435 end = data + len;
436 while (pos < end) {
437 if (pos + 2 + pos[1] > end)
438 return NULL;
440 if (pos[0] == ie)
441 return pos;
443 pos += 2 + pos[1];
446 return NULL;
449 static void rt2x00lib_rxdone_check_ps(struct rt2x00_dev *rt2x00dev,
450 struct sk_buff *skb,
451 struct rxdone_entry_desc *rxdesc)
453 struct ieee80211_hdr *hdr = (void *) skb->data;
454 struct ieee80211_tim_ie *tim_ie;
455 u8 *tim;
456 u8 tim_len;
457 bool cam;
459 /* If this is not a beacon, or if mac80211 has no powersaving
460 * configured, or if the device is already in powersaving mode
461 * we can exit now. */
462 if (likely(!ieee80211_is_beacon(hdr->frame_control) ||
463 !(rt2x00dev->hw->conf.flags & IEEE80211_CONF_PS)))
464 return;
466 /* min. beacon length + FCS_LEN */
467 if (skb->len <= 40 + FCS_LEN)
468 return;
470 /* and only beacons from the associated BSSID, please */
471 if (!(rxdesc->dev_flags & RXDONE_MY_BSS) ||
472 !rt2x00dev->aid)
473 return;
475 rt2x00dev->last_beacon = jiffies;
477 tim = rt2x00lib_find_ie(skb->data, skb->len - FCS_LEN, WLAN_EID_TIM);
478 if (!tim)
479 return;
481 if (tim[1] < sizeof(*tim_ie))
482 return;
484 tim_len = tim[1];
485 tim_ie = (struct ieee80211_tim_ie *) &tim[2];
487 /* Check whenever the PHY can be turned off again. */
489 /* 1. What about buffered unicast traffic for our AID? */
490 cam = ieee80211_check_tim(tim_ie, tim_len, rt2x00dev->aid);
492 /* 2. Maybe the AP wants to send multicast/broadcast data? */
493 cam |= (tim_ie->bitmap_ctrl & 0x01);
495 if (!cam && !test_bit(CONFIG_POWERSAVING, &rt2x00dev->flags))
496 rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf,
497 IEEE80211_CONF_CHANGE_PS);
500 static int rt2x00lib_rxdone_read_signal(struct rt2x00_dev *rt2x00dev,
501 struct rxdone_entry_desc *rxdesc)
503 struct ieee80211_supported_band *sband;
504 const struct rt2x00_rate *rate;
505 unsigned int i;
506 int signal = rxdesc->signal;
507 int type = (rxdesc->dev_flags & RXDONE_SIGNAL_MASK);
509 switch (rxdesc->rate_mode) {
510 case RATE_MODE_CCK:
511 case RATE_MODE_OFDM:
513 * For non-HT rates the MCS value needs to contain the
514 * actually used rate modulation (CCK or OFDM).
516 if (rxdesc->dev_flags & RXDONE_SIGNAL_MCS)
517 signal = RATE_MCS(rxdesc->rate_mode, signal);
519 sband = &rt2x00dev->bands[rt2x00dev->curr_band];
520 for (i = 0; i < sband->n_bitrates; i++) {
521 rate = rt2x00_get_rate(sband->bitrates[i].hw_value);
522 if (((type == RXDONE_SIGNAL_PLCP) &&
523 (rate->plcp == signal)) ||
524 ((type == RXDONE_SIGNAL_BITRATE) &&
525 (rate->bitrate == signal)) ||
526 ((type == RXDONE_SIGNAL_MCS) &&
527 (rate->mcs == signal))) {
528 return i;
531 break;
532 case RATE_MODE_HT_MIX:
533 case RATE_MODE_HT_GREENFIELD:
534 if (signal >= 0 && signal <= 76)
535 return signal;
536 break;
537 default:
538 break;
541 WARNING(rt2x00dev, "Frame received with unrecognized signal, "
542 "mode=0x%.4x, signal=0x%.4x, type=%d.\n",
543 rxdesc->rate_mode, signal, type);
544 return 0;
547 void rt2x00lib_rxdone(struct queue_entry *entry)
549 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
550 struct rxdone_entry_desc rxdesc;
551 struct sk_buff *skb;
552 struct ieee80211_rx_status *rx_status;
553 unsigned int header_length;
554 int rate_idx;
556 if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) ||
557 !test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
558 goto submit_entry;
560 if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags))
561 goto submit_entry;
564 * Allocate a new sk_buffer. If no new buffer available, drop the
565 * received frame and reuse the existing buffer.
567 skb = rt2x00queue_alloc_rxskb(entry);
568 if (!skb)
569 goto submit_entry;
572 * Unmap the skb.
574 rt2x00queue_unmap_skb(entry);
577 * Extract the RXD details.
579 memset(&rxdesc, 0, sizeof(rxdesc));
580 rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
583 * The data behind the ieee80211 header must be
584 * aligned on a 4 byte boundary.
586 header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
589 * Hardware might have stripped the IV/EIV/ICV data,
590 * in that case it is possible that the data was
591 * provided separately (through hardware descriptor)
592 * in which case we should reinsert the data into the frame.
594 if ((rxdesc.dev_flags & RXDONE_CRYPTO_IV) &&
595 (rxdesc.flags & RX_FLAG_IV_STRIPPED))
596 rt2x00crypto_rx_insert_iv(entry->skb, header_length,
597 &rxdesc);
598 else if (header_length &&
599 (rxdesc.size > header_length) &&
600 (rxdesc.dev_flags & RXDONE_L2PAD))
601 rt2x00queue_remove_l2pad(entry->skb, header_length);
603 /* Trim buffer to correct size */
604 skb_trim(entry->skb, rxdesc.size);
607 * Translate the signal to the correct bitrate index.
609 rate_idx = rt2x00lib_rxdone_read_signal(rt2x00dev, &rxdesc);
610 if (rxdesc.rate_mode == RATE_MODE_HT_MIX ||
611 rxdesc.rate_mode == RATE_MODE_HT_GREENFIELD)
612 rxdesc.flags |= RX_FLAG_HT;
615 * Check if this is a beacon, and more frames have been
616 * buffered while we were in powersaving mode.
618 rt2x00lib_rxdone_check_ps(rt2x00dev, entry->skb, &rxdesc);
621 * Update extra components
623 rt2x00link_update_stats(rt2x00dev, entry->skb, &rxdesc);
624 rt2x00debug_update_crypto(rt2x00dev, &rxdesc);
625 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_RXDONE, entry->skb);
628 * Initialize RX status information, and send frame
629 * to mac80211.
631 rx_status = IEEE80211_SKB_RXCB(entry->skb);
632 rx_status->mactime = rxdesc.timestamp;
633 rx_status->band = rt2x00dev->curr_band;
634 rx_status->freq = rt2x00dev->curr_freq;
635 rx_status->rate_idx = rate_idx;
636 rx_status->signal = rxdesc.rssi;
637 rx_status->flag = rxdesc.flags;
638 rx_status->antenna = rt2x00dev->link.ant.active.rx;
640 ieee80211_rx_ni(rt2x00dev->hw, entry->skb);
643 * Replace the skb with the freshly allocated one.
645 entry->skb = skb;
647 submit_entry:
648 entry->flags = 0;
649 rt2x00queue_index_inc(entry, Q_INDEX_DONE);
650 if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) &&
651 test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
652 rt2x00dev->ops->lib->clear_entry(entry);
654 EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
657 * Driver initialization handlers.
659 const struct rt2x00_rate rt2x00_supported_rates[12] = {
661 .flags = DEV_RATE_CCK,
662 .bitrate = 10,
663 .ratemask = BIT(0),
664 .plcp = 0x00,
665 .mcs = RATE_MCS(RATE_MODE_CCK, 0),
668 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
669 .bitrate = 20,
670 .ratemask = BIT(1),
671 .plcp = 0x01,
672 .mcs = RATE_MCS(RATE_MODE_CCK, 1),
675 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
676 .bitrate = 55,
677 .ratemask = BIT(2),
678 .plcp = 0x02,
679 .mcs = RATE_MCS(RATE_MODE_CCK, 2),
682 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
683 .bitrate = 110,
684 .ratemask = BIT(3),
685 .plcp = 0x03,
686 .mcs = RATE_MCS(RATE_MODE_CCK, 3),
689 .flags = DEV_RATE_OFDM,
690 .bitrate = 60,
691 .ratemask = BIT(4),
692 .plcp = 0x0b,
693 .mcs = RATE_MCS(RATE_MODE_OFDM, 0),
696 .flags = DEV_RATE_OFDM,
697 .bitrate = 90,
698 .ratemask = BIT(5),
699 .plcp = 0x0f,
700 .mcs = RATE_MCS(RATE_MODE_OFDM, 1),
703 .flags = DEV_RATE_OFDM,
704 .bitrate = 120,
705 .ratemask = BIT(6),
706 .plcp = 0x0a,
707 .mcs = RATE_MCS(RATE_MODE_OFDM, 2),
710 .flags = DEV_RATE_OFDM,
711 .bitrate = 180,
712 .ratemask = BIT(7),
713 .plcp = 0x0e,
714 .mcs = RATE_MCS(RATE_MODE_OFDM, 3),
717 .flags = DEV_RATE_OFDM,
718 .bitrate = 240,
719 .ratemask = BIT(8),
720 .plcp = 0x09,
721 .mcs = RATE_MCS(RATE_MODE_OFDM, 4),
724 .flags = DEV_RATE_OFDM,
725 .bitrate = 360,
726 .ratemask = BIT(9),
727 .plcp = 0x0d,
728 .mcs = RATE_MCS(RATE_MODE_OFDM, 5),
731 .flags = DEV_RATE_OFDM,
732 .bitrate = 480,
733 .ratemask = BIT(10),
734 .plcp = 0x08,
735 .mcs = RATE_MCS(RATE_MODE_OFDM, 6),
738 .flags = DEV_RATE_OFDM,
739 .bitrate = 540,
740 .ratemask = BIT(11),
741 .plcp = 0x0c,
742 .mcs = RATE_MCS(RATE_MODE_OFDM, 7),
746 static void rt2x00lib_channel(struct ieee80211_channel *entry,
747 const int channel, const int tx_power,
748 const int value)
750 /* XXX: this assumption about the band is wrong for 802.11j */
751 entry->band = channel <= 14 ? IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
752 entry->center_freq = ieee80211_channel_to_frequency(channel,
753 entry->band);
754 entry->hw_value = value;
755 entry->max_power = tx_power;
756 entry->max_antenna_gain = 0xff;
759 static void rt2x00lib_rate(struct ieee80211_rate *entry,
760 const u16 index, const struct rt2x00_rate *rate)
762 entry->flags = 0;
763 entry->bitrate = rate->bitrate;
764 entry->hw_value = index;
765 entry->hw_value_short = index;
767 if (rate->flags & DEV_RATE_SHORT_PREAMBLE)
768 entry->flags |= IEEE80211_RATE_SHORT_PREAMBLE;
771 static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
772 struct hw_mode_spec *spec)
774 struct ieee80211_hw *hw = rt2x00dev->hw;
775 struct ieee80211_channel *channels;
776 struct ieee80211_rate *rates;
777 unsigned int num_rates;
778 unsigned int i;
780 num_rates = 0;
781 if (spec->supported_rates & SUPPORT_RATE_CCK)
782 num_rates += 4;
783 if (spec->supported_rates & SUPPORT_RATE_OFDM)
784 num_rates += 8;
786 channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
787 if (!channels)
788 return -ENOMEM;
790 rates = kzalloc(sizeof(*rates) * num_rates, GFP_KERNEL);
791 if (!rates)
792 goto exit_free_channels;
795 * Initialize Rate list.
797 for (i = 0; i < num_rates; i++)
798 rt2x00lib_rate(&rates[i], i, rt2x00_get_rate(i));
801 * Initialize Channel list.
803 for (i = 0; i < spec->num_channels; i++) {
804 rt2x00lib_channel(&channels[i],
805 spec->channels[i].channel,
806 spec->channels_info[i].max_power, i);
810 * Intitialize 802.11b, 802.11g
811 * Rates: CCK, OFDM.
812 * Channels: 2.4 GHz
814 if (spec->supported_bands & SUPPORT_BAND_2GHZ) {
815 rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_channels = 14;
816 rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_bitrates = num_rates;
817 rt2x00dev->bands[IEEE80211_BAND_2GHZ].channels = channels;
818 rt2x00dev->bands[IEEE80211_BAND_2GHZ].bitrates = rates;
819 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
820 &rt2x00dev->bands[IEEE80211_BAND_2GHZ];
821 memcpy(&rt2x00dev->bands[IEEE80211_BAND_2GHZ].ht_cap,
822 &spec->ht, sizeof(spec->ht));
826 * Intitialize 802.11a
827 * Rates: OFDM.
828 * Channels: OFDM, UNII, HiperLAN2.
830 if (spec->supported_bands & SUPPORT_BAND_5GHZ) {
831 rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_channels =
832 spec->num_channels - 14;
833 rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_bitrates =
834 num_rates - 4;
835 rt2x00dev->bands[IEEE80211_BAND_5GHZ].channels = &channels[14];
836 rt2x00dev->bands[IEEE80211_BAND_5GHZ].bitrates = &rates[4];
837 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
838 &rt2x00dev->bands[IEEE80211_BAND_5GHZ];
839 memcpy(&rt2x00dev->bands[IEEE80211_BAND_5GHZ].ht_cap,
840 &spec->ht, sizeof(spec->ht));
843 return 0;
845 exit_free_channels:
846 kfree(channels);
847 ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
848 return -ENOMEM;
851 static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
853 if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
854 ieee80211_unregister_hw(rt2x00dev->hw);
856 if (likely(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ])) {
857 kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->channels);
858 kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->bitrates);
859 rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
860 rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
863 kfree(rt2x00dev->spec.channels_info);
866 static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
868 struct hw_mode_spec *spec = &rt2x00dev->spec;
869 int status;
871 if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
872 return 0;
875 * Initialize HW modes.
877 status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
878 if (status)
879 return status;
882 * Initialize HW fields.
884 rt2x00dev->hw->queues = rt2x00dev->ops->tx_queues;
887 * Initialize extra TX headroom required.
889 rt2x00dev->hw->extra_tx_headroom =
890 max_t(unsigned int, IEEE80211_TX_STATUS_HEADROOM,
891 rt2x00dev->ops->extra_tx_headroom);
894 * Take TX headroom required for alignment into account.
896 if (test_bit(REQUIRE_L2PAD, &rt2x00dev->cap_flags))
897 rt2x00dev->hw->extra_tx_headroom += RT2X00_L2PAD_SIZE;
898 else if (test_bit(REQUIRE_DMA, &rt2x00dev->cap_flags))
899 rt2x00dev->hw->extra_tx_headroom += RT2X00_ALIGN_SIZE;
902 * Allocate tx status FIFO for driver use.
904 if (test_bit(REQUIRE_TXSTATUS_FIFO, &rt2x00dev->cap_flags)) {
906 * Allocate the txstatus fifo. In the worst case the tx
907 * status fifo has to hold the tx status of all entries
908 * in all tx queues. Hence, calculate the kfifo size as
909 * tx_queues * entry_num and round up to the nearest
910 * power of 2.
912 int kfifo_size =
913 roundup_pow_of_two(rt2x00dev->ops->tx_queues *
914 rt2x00dev->ops->tx->entry_num *
915 sizeof(u32));
917 status = kfifo_alloc(&rt2x00dev->txstatus_fifo, kfifo_size,
918 GFP_KERNEL);
919 if (status)
920 return status;
924 * Initialize tasklets if used by the driver. Tasklets are
925 * disabled until the interrupts are turned on. The driver
926 * has to handle that.
928 #define RT2X00_TASKLET_INIT(taskletname) \
929 if (rt2x00dev->ops->lib->taskletname) { \
930 tasklet_init(&rt2x00dev->taskletname, \
931 rt2x00dev->ops->lib->taskletname, \
932 (unsigned long)rt2x00dev); \
933 tasklet_disable(&rt2x00dev->taskletname); \
936 RT2X00_TASKLET_INIT(txstatus_tasklet);
937 RT2X00_TASKLET_INIT(pretbtt_tasklet);
938 RT2X00_TASKLET_INIT(tbtt_tasklet);
939 RT2X00_TASKLET_INIT(rxdone_tasklet);
940 RT2X00_TASKLET_INIT(autowake_tasklet);
942 #undef RT2X00_TASKLET_INIT
945 * Register HW.
947 status = ieee80211_register_hw(rt2x00dev->hw);
948 if (status)
949 return status;
951 set_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags);
953 return 0;
957 * Initialization/uninitialization handlers.
959 static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
961 if (!test_and_clear_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
962 return;
965 * Unregister extra components.
967 rt2x00rfkill_unregister(rt2x00dev);
970 * Allow the HW to uninitialize.
972 rt2x00dev->ops->lib->uninitialize(rt2x00dev);
975 * Free allocated queue entries.
977 rt2x00queue_uninitialize(rt2x00dev);
980 static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
982 int status;
984 if (test_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
985 return 0;
988 * Allocate all queue entries.
990 status = rt2x00queue_initialize(rt2x00dev);
991 if (status)
992 return status;
995 * Initialize the device.
997 status = rt2x00dev->ops->lib->initialize(rt2x00dev);
998 if (status) {
999 rt2x00queue_uninitialize(rt2x00dev);
1000 return status;
1003 set_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags);
1006 * Register the extra components.
1008 rt2x00rfkill_register(rt2x00dev);
1010 return 0;
1013 int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
1015 int retval;
1017 if (test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
1018 return 0;
1021 * If this is the first interface which is added,
1022 * we should load the firmware now.
1024 retval = rt2x00lib_load_firmware(rt2x00dev);
1025 if (retval)
1026 return retval;
1029 * Initialize the device.
1031 retval = rt2x00lib_initialize(rt2x00dev);
1032 if (retval)
1033 return retval;
1035 rt2x00dev->intf_ap_count = 0;
1036 rt2x00dev->intf_sta_count = 0;
1037 rt2x00dev->intf_associated = 0;
1039 /* Enable the radio */
1040 retval = rt2x00lib_enable_radio(rt2x00dev);
1041 if (retval)
1042 return retval;
1044 set_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags);
1046 return 0;
1049 void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
1051 if (!test_and_clear_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
1052 return;
1055 * Perhaps we can add something smarter here,
1056 * but for now just disabling the radio should do.
1058 rt2x00lib_disable_radio(rt2x00dev);
1060 rt2x00dev->intf_ap_count = 0;
1061 rt2x00dev->intf_sta_count = 0;
1062 rt2x00dev->intf_associated = 0;
1066 * driver allocation handlers.
1068 int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
1070 int retval = -ENOMEM;
1072 spin_lock_init(&rt2x00dev->irqmask_lock);
1073 mutex_init(&rt2x00dev->csr_mutex);
1075 set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
1078 * Make room for rt2x00_intf inside the per-interface
1079 * structure ieee80211_vif.
1081 rt2x00dev->hw->vif_data_size = sizeof(struct rt2x00_intf);
1084 * Determine which operating modes are supported, all modes
1085 * which require beaconing, depend on the availability of
1086 * beacon entries.
1088 rt2x00dev->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1089 if (rt2x00dev->ops->bcn->entry_num > 0)
1090 rt2x00dev->hw->wiphy->interface_modes |=
1091 BIT(NL80211_IFTYPE_ADHOC) |
1092 BIT(NL80211_IFTYPE_AP) |
1093 BIT(NL80211_IFTYPE_MESH_POINT) |
1094 BIT(NL80211_IFTYPE_WDS);
1097 * Initialize work.
1099 rt2x00dev->workqueue =
1100 alloc_ordered_workqueue(wiphy_name(rt2x00dev->hw->wiphy), 0);
1101 if (!rt2x00dev->workqueue) {
1102 retval = -ENOMEM;
1103 goto exit;
1106 INIT_WORK(&rt2x00dev->intf_work, rt2x00lib_intf_scheduled);
1107 INIT_DELAYED_WORK(&rt2x00dev->autowakeup_work, rt2x00lib_autowakeup);
1110 * Let the driver probe the device to detect the capabilities.
1112 retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
1113 if (retval) {
1114 ERROR(rt2x00dev, "Failed to allocate device.\n");
1115 goto exit;
1119 * Allocate queue array.
1121 retval = rt2x00queue_allocate(rt2x00dev);
1122 if (retval)
1123 goto exit;
1126 * Initialize ieee80211 structure.
1128 retval = rt2x00lib_probe_hw(rt2x00dev);
1129 if (retval) {
1130 ERROR(rt2x00dev, "Failed to initialize hw.\n");
1131 goto exit;
1135 * Register extra components.
1137 rt2x00link_register(rt2x00dev);
1138 rt2x00leds_register(rt2x00dev);
1139 rt2x00debug_register(rt2x00dev);
1141 return 0;
1143 exit:
1144 rt2x00lib_remove_dev(rt2x00dev);
1146 return retval;
1148 EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
1150 void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
1152 clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
1155 * Disable radio.
1157 rt2x00lib_disable_radio(rt2x00dev);
1160 * Stop all work.
1162 cancel_work_sync(&rt2x00dev->intf_work);
1163 if (rt2x00_is_usb(rt2x00dev)) {
1164 del_timer_sync(&rt2x00dev->txstatus_timer);
1165 cancel_work_sync(&rt2x00dev->rxdone_work);
1166 cancel_work_sync(&rt2x00dev->txdone_work);
1168 destroy_workqueue(rt2x00dev->workqueue);
1171 * Free the tx status fifo.
1173 kfifo_free(&rt2x00dev->txstatus_fifo);
1176 * Kill the tx status tasklet.
1178 tasklet_kill(&rt2x00dev->txstatus_tasklet);
1179 tasklet_kill(&rt2x00dev->pretbtt_tasklet);
1180 tasklet_kill(&rt2x00dev->tbtt_tasklet);
1181 tasklet_kill(&rt2x00dev->rxdone_tasklet);
1182 tasklet_kill(&rt2x00dev->autowake_tasklet);
1185 * Uninitialize device.
1187 rt2x00lib_uninitialize(rt2x00dev);
1190 * Free extra components
1192 rt2x00debug_deregister(rt2x00dev);
1193 rt2x00leds_unregister(rt2x00dev);
1196 * Free ieee80211_hw memory.
1198 rt2x00lib_remove_hw(rt2x00dev);
1201 * Free firmware image.
1203 rt2x00lib_free_firmware(rt2x00dev);
1206 * Free queue structures.
1208 rt2x00queue_free(rt2x00dev);
1210 EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
1213 * Device state handlers
1215 #ifdef CONFIG_PM
1216 int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
1218 NOTICE(rt2x00dev, "Going to sleep.\n");
1221 * Prevent mac80211 from accessing driver while suspended.
1223 if (!test_and_clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
1224 return 0;
1227 * Cleanup as much as possible.
1229 rt2x00lib_uninitialize(rt2x00dev);
1232 * Suspend/disable extra components.
1234 rt2x00leds_suspend(rt2x00dev);
1235 rt2x00debug_deregister(rt2x00dev);
1238 * Set device mode to sleep for power management,
1239 * on some hardware this call seems to consistently fail.
1240 * From the specifications it is hard to tell why it fails,
1241 * and if this is a "bad thing".
1242 * Overall it is safe to just ignore the failure and
1243 * continue suspending. The only downside is that the
1244 * device will not be in optimal power save mode, but with
1245 * the radio and the other components already disabled the
1246 * device is as good as disabled.
1248 if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP))
1249 WARNING(rt2x00dev, "Device failed to enter sleep state, "
1250 "continue suspending.\n");
1252 return 0;
1254 EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1256 int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1258 NOTICE(rt2x00dev, "Waking up.\n");
1261 * Restore/enable extra components.
1263 rt2x00debug_register(rt2x00dev);
1264 rt2x00leds_resume(rt2x00dev);
1267 * We are ready again to receive requests from mac80211.
1269 set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
1271 return 0;
1273 EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1274 #endif /* CONFIG_PM */
1277 * rt2x00lib module information.
1279 MODULE_AUTHOR(DRV_PROJECT);
1280 MODULE_VERSION(DRV_VERSION);
1281 MODULE_DESCRIPTION("rt2x00 library");
1282 MODULE_LICENSE("GPL");