2 Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
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.
23 Abstract: rt2x00 generic device routines.
27 * Set enviroment defines for rt2x00.h
29 #define DRV_NAME "rt2x00lib"
31 #include <linux/kernel.h>
32 #include <linux/module.h>
35 #include "rt2x00lib.h"
40 struct data_ring *rt2x00lib_get_ring(struct rt2x00_dev *rt2x00dev,
41 const unsigned int queue)
43 int beacon = test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags);
46 * Check if we are requesting a reqular TX ring,
47 * or if we are requesting a Beacon or Atim ring.
48 * For Atim rings, we should check if it is supported.
50 if (queue < rt2x00dev->hw->queues && rt2x00dev->tx)
51 return &rt2x00dev->tx[queue];
53 if (!rt2x00dev->bcn || !beacon)
56 if (queue == IEEE80211_TX_QUEUE_BEACON)
57 return &rt2x00dev->bcn[0];
58 else if (queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
59 return &rt2x00dev->bcn[1];
63 EXPORT_SYMBOL_GPL(rt2x00lib_get_ring);
66 * Link tuning handlers
68 static void rt2x00lib_start_link_tuner(struct rt2x00_dev *rt2x00dev)
70 rt2x00_clear_link(&rt2x00dev->link);
73 * Reset the link tuner.
75 rt2x00dev->ops->lib->reset_tuner(rt2x00dev);
77 queue_delayed_work(rt2x00dev->hw->workqueue,
78 &rt2x00dev->link.work, LINK_TUNE_INTERVAL);
81 static void rt2x00lib_stop_link_tuner(struct rt2x00_dev *rt2x00dev)
83 cancel_delayed_work_sync(&rt2x00dev->link.work);
86 void rt2x00lib_reset_link_tuner(struct rt2x00_dev *rt2x00dev)
88 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
91 rt2x00lib_stop_link_tuner(rt2x00dev);
92 rt2x00lib_start_link_tuner(rt2x00dev);
96 * Radio control handlers.
98 int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
103 * Don't enable the radio twice.
104 * And check if the hardware button has been disabled.
106 if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
107 (test_bit(CONFIG_SUPPORT_HW_BUTTON, &rt2x00dev->flags) &&
108 !test_bit(DEVICE_ENABLED_RADIO_HW, &rt2x00dev->flags)))
114 status = rt2x00dev->ops->lib->set_device_state(rt2x00dev,
119 __set_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags);
124 rt2x00lib_toggle_rx(rt2x00dev, 1);
127 * Start the TX queues.
129 ieee80211_start_queues(rt2x00dev->hw);
134 void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
136 if (!__test_and_clear_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
140 * Stop all scheduled work.
142 if (work_pending(&rt2x00dev->beacon_work))
143 cancel_work_sync(&rt2x00dev->beacon_work);
144 if (work_pending(&rt2x00dev->filter_work))
145 cancel_work_sync(&rt2x00dev->filter_work);
148 * Stop the TX queues.
150 ieee80211_stop_queues(rt2x00dev->hw);
155 rt2x00lib_toggle_rx(rt2x00dev, 0);
160 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
163 void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, int enable)
165 enum dev_state state = enable ? STATE_RADIO_RX_ON : STATE_RADIO_RX_OFF;
168 * When we are disabling the RX, we should also stop the link tuner.
171 rt2x00lib_stop_link_tuner(rt2x00dev);
173 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
176 * When we are enabling the RX, we should also start the link tuner.
178 if (enable && is_interface_present(&rt2x00dev->interface))
179 rt2x00lib_start_link_tuner(rt2x00dev);
182 static void rt2x00lib_precalculate_link_signal(struct link *link)
184 if (link->rx_failed || link->rx_success)
185 link->rx_percentage =
186 (link->rx_success * 100) /
187 (link->rx_failed + link->rx_success);
189 link->rx_percentage = 50;
191 if (link->tx_failed || link->tx_success)
192 link->tx_percentage =
193 (link->tx_success * 100) /
194 (link->tx_failed + link->tx_success);
196 link->tx_percentage = 50;
198 link->rx_success = 0;
200 link->tx_success = 0;
204 static int rt2x00lib_calculate_link_signal(struct rt2x00_dev *rt2x00dev,
207 int rssi_percentage = 0;
211 * We need a positive value for the RSSI.
214 rssi += rt2x00dev->rssi_offset;
217 * Calculate the different percentages,
218 * which will be used for the signal.
220 if (rt2x00dev->rssi_offset)
221 rssi_percentage = (rssi * 100) / rt2x00dev->rssi_offset;
224 * Add the individual percentages and use the WEIGHT
225 * defines to calculate the current link signal.
227 signal = ((WEIGHT_RSSI * rssi_percentage) +
228 (WEIGHT_TX * rt2x00dev->link.tx_percentage) +
229 (WEIGHT_RX * rt2x00dev->link.rx_percentage)) / 100;
231 return (signal > 100) ? 100 : signal;
234 static void rt2x00lib_link_tuner(struct work_struct *work)
236 struct rt2x00_dev *rt2x00dev =
237 container_of(work, struct rt2x00_dev, link.work.work);
240 * When the radio is shutting down we should
241 * immediately cease all link tuning.
243 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
249 rt2x00dev->ops->lib->link_stats(rt2x00dev);
251 rt2x00dev->low_level_stats.dot11FCSErrorCount +=
252 rt2x00dev->link.rx_failed;
255 * Only perform the link tuning when Link tuning
256 * has been enabled (This could have been disabled from the EEPROM).
258 if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
259 rt2x00dev->ops->lib->link_tuner(rt2x00dev);
262 * Precalculate a portion of the link signal which is
263 * in based on the tx/rx success/failure counters.
265 rt2x00lib_precalculate_link_signal(&rt2x00dev->link);
268 * Increase tuner counter, and reschedule the next link tuner run.
270 rt2x00dev->link.count++;
271 queue_delayed_work(rt2x00dev->hw->workqueue, &rt2x00dev->link.work,
275 static void rt2x00lib_packetfilter_scheduled(struct work_struct *work)
277 struct rt2x00_dev *rt2x00dev =
278 container_of(work, struct rt2x00_dev, filter_work);
280 rt2x00dev->ops->hw->configure_filter(rt2x00dev->hw,
281 rt2x00dev->interface.filter,
282 &rt2x00dev->interface.filter,
287 * Interrupt context handlers.
289 static void rt2x00lib_beacondone_scheduled(struct work_struct *work)
291 struct rt2x00_dev *rt2x00dev =
292 container_of(work, struct rt2x00_dev, beacon_work);
293 struct data_ring *ring =
294 rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_BEACON);
295 struct data_entry *entry = rt2x00_get_data_entry(ring);
298 skb = ieee80211_beacon_get(rt2x00dev->hw,
299 rt2x00dev->interface.id,
300 &entry->tx_status.control);
304 rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw, skb,
305 &entry->tx_status.control);
310 void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
312 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
315 queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->beacon_work);
317 EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
319 void rt2x00lib_txdone(struct data_entry *entry,
320 const int status, const int retry)
322 struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
323 struct ieee80211_tx_status *tx_status = &entry->tx_status;
324 struct ieee80211_low_level_stats *stats = &rt2x00dev->low_level_stats;
325 int success = !!(status == TX_SUCCESS || status == TX_SUCCESS_RETRY);
326 int fail = !!(status == TX_FAIL_RETRY || status == TX_FAIL_INVALID ||
327 status == TX_FAIL_OTHER);
330 * Update TX statistics.
332 tx_status->flags = 0;
333 tx_status->ack_signal = 0;
334 tx_status->excessive_retries = (status == TX_FAIL_RETRY);
335 tx_status->retry_count = retry;
336 rt2x00dev->link.tx_success += success;
337 rt2x00dev->link.tx_failed += retry + fail;
339 if (!(tx_status->control.flags & IEEE80211_TXCTL_NO_ACK)) {
341 tx_status->flags |= IEEE80211_TX_STATUS_ACK;
343 stats->dot11ACKFailureCount++;
346 tx_status->queue_length = entry->ring->stats.limit;
347 tx_status->queue_number = tx_status->control.queue;
349 if (tx_status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) {
351 stats->dot11RTSSuccessCount++;
353 stats->dot11RTSFailureCount++;
357 * Send the tx_status to mac80211,
358 * that method also cleans up the skb structure.
360 ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb, tx_status);
363 EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
365 void rt2x00lib_rxdone(struct data_entry *entry, struct sk_buff *skb,
366 struct rxdata_entry_desc *desc)
368 struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
369 struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
370 struct ieee80211_hw_mode *mode;
371 struct ieee80211_rate *rate;
376 * Update RX statistics.
378 mode = &rt2x00dev->hwmodes[rt2x00dev->curr_hwmode];
379 for (i = 0; i < mode->num_rates; i++) {
380 rate = &mode->rates[i];
383 * When frame was received with an OFDM bitrate,
384 * the signal is the PLCP value. If it was received with
385 * a CCK bitrate the signal is the rate in 0.5kbit/s.
388 val = DEVICE_GET_RATE_FIELD(rate->val, RATE);
390 val = DEVICE_GET_RATE_FIELD(rate->val, PLCP);
392 if (val == desc->signal) {
398 rt2x00_update_link_rssi(&rt2x00dev->link, desc->rssi);
399 rt2x00dev->link.rx_success++;
400 rx_status->rate = val;
402 rt2x00lib_calculate_link_signal(rt2x00dev, desc->rssi);
403 rx_status->ssi = desc->rssi;
404 rx_status->flag = desc->flags;
407 * Send frame to mac80211
409 ieee80211_rx_irqsafe(rt2x00dev->hw, skb, rx_status);
411 EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
414 * TX descriptor initializer
416 void rt2x00lib_write_tx_desc(struct rt2x00_dev *rt2x00dev,
417 struct data_desc *txd,
418 struct ieee80211_hdr *ieee80211hdr,
420 struct ieee80211_tx_control *control)
422 struct txdata_entry_desc desc;
423 struct data_ring *ring;
432 * Make sure the descriptor is properly cleared.
434 memset(&desc, 0x00, sizeof(desc));
437 * Get ring pointer, if we fail to obtain the
438 * correct ring, then use the first TX ring.
440 ring = rt2x00lib_get_ring(rt2x00dev, control->queue);
442 ring = rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_DATA0);
444 desc.cw_min = ring->tx_params.cw_min;
445 desc.cw_max = ring->tx_params.cw_max;
446 desc.aifs = ring->tx_params.aifs;
451 if (control->queue < rt2x00dev->hw->queues)
452 desc.queue = control->queue;
453 else if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
454 control->queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
455 desc.queue = QUEUE_MGMT;
457 desc.queue = QUEUE_OTHER;
460 * Read required fields from ieee80211 header.
462 frame_control = le16_to_cpu(ieee80211hdr->frame_control);
463 seq_ctrl = le16_to_cpu(ieee80211hdr->seq_ctrl);
465 tx_rate = control->tx_rate;
468 * Check if this is a RTS/CTS frame
470 if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
471 __set_bit(ENTRY_TXD_BURST, &desc.flags);
472 if (is_rts_frame(frame_control))
473 __set_bit(ENTRY_TXD_RTS_FRAME, &desc.flags);
474 if (control->rts_cts_rate)
475 tx_rate = control->rts_cts_rate;
481 if (DEVICE_GET_RATE_FIELD(tx_rate, RATEMASK) & DEV_OFDM_RATEMASK)
482 __set_bit(ENTRY_TXD_OFDM_RATE, &desc.flags);
485 * Check if more fragments are pending
487 if (ieee80211_get_morefrag(ieee80211hdr)) {
488 __set_bit(ENTRY_TXD_BURST, &desc.flags);
489 __set_bit(ENTRY_TXD_MORE_FRAG, &desc.flags);
493 * Beacons and probe responses require the tsf timestamp
494 * to be inserted into the frame.
496 if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
497 is_probe_resp(frame_control))
498 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &desc.flags);
501 * Determine with what IFS priority this frame should be send.
502 * Set ifs to IFS_SIFS when the this is not the first fragment,
503 * or this fragment came after RTS/CTS.
505 if ((seq_ctrl & IEEE80211_SCTL_FRAG) > 0 ||
506 test_bit(ENTRY_TXD_RTS_FRAME, &desc.flags))
509 desc.ifs = IFS_BACKOFF;
513 * Length calculation depends on OFDM/CCK rate.
515 desc.signal = DEVICE_GET_RATE_FIELD(tx_rate, PLCP);
518 if (test_bit(ENTRY_TXD_OFDM_RATE, &desc.flags)) {
519 desc.length_high = ((length + FCS_LEN) >> 6) & 0x3f;
520 desc.length_low = ((length + FCS_LEN) & 0x3f);
522 bitrate = DEVICE_GET_RATE_FIELD(tx_rate, RATE);
525 * Convert length to microseconds.
527 residual = get_duration_res(length + FCS_LEN, bitrate);
528 duration = get_duration(length + FCS_LEN, bitrate);
534 * Check if we need to set the Length Extension
536 if (bitrate == 110 && residual <= 3)
537 desc.service |= 0x80;
540 desc.length_high = (duration >> 8) & 0xff;
541 desc.length_low = duration & 0xff;
544 * When preamble is enabled we should set the
545 * preamble bit for the signal.
547 if (DEVICE_GET_RATE_FIELD(tx_rate, PREAMBLE))
551 rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, txd, &desc,
552 ieee80211hdr, length, control);
554 EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc);
557 * Driver initialization handlers.
559 static void rt2x00lib_channel(struct ieee80211_channel *entry,
560 const int channel, const int tx_power,
563 entry->chan = channel;
565 entry->freq = 2407 + (5 * channel);
567 entry->freq = 5000 + (5 * channel);
570 IEEE80211_CHAN_W_IBSS |
571 IEEE80211_CHAN_W_ACTIVE_SCAN |
572 IEEE80211_CHAN_W_SCAN;
573 entry->power_level = tx_power;
574 entry->antenna_max = 0xff;
577 static void rt2x00lib_rate(struct ieee80211_rate *entry,
578 const int rate, const int mask,
579 const int plcp, const int flags)
583 DEVICE_SET_RATE_FIELD(rate, RATE) |
584 DEVICE_SET_RATE_FIELD(mask, RATEMASK) |
585 DEVICE_SET_RATE_FIELD(plcp, PLCP);
586 entry->flags = flags;
587 entry->val2 = entry->val;
588 if (entry->flags & IEEE80211_RATE_PREAMBLE2)
589 entry->val2 |= DEVICE_SET_RATE_FIELD(1, PREAMBLE);
590 entry->min_rssi_ack = 0;
591 entry->min_rssi_ack_delta = 0;
594 static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
595 struct hw_mode_spec *spec)
597 struct ieee80211_hw *hw = rt2x00dev->hw;
598 struct ieee80211_hw_mode *hwmodes;
599 struct ieee80211_channel *channels;
600 struct ieee80211_rate *rates;
602 unsigned char tx_power;
604 hwmodes = kzalloc(sizeof(*hwmodes) * spec->num_modes, GFP_KERNEL);
608 channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
610 goto exit_free_modes;
612 rates = kzalloc(sizeof(*rates) * spec->num_rates, GFP_KERNEL);
614 goto exit_free_channels;
617 * Initialize Rate list.
619 rt2x00lib_rate(&rates[0], 10, DEV_RATEMASK_1MB,
620 0x00, IEEE80211_RATE_CCK);
621 rt2x00lib_rate(&rates[1], 20, DEV_RATEMASK_2MB,
622 0x01, IEEE80211_RATE_CCK_2);
623 rt2x00lib_rate(&rates[2], 55, DEV_RATEMASK_5_5MB,
624 0x02, IEEE80211_RATE_CCK_2);
625 rt2x00lib_rate(&rates[3], 110, DEV_RATEMASK_11MB,
626 0x03, IEEE80211_RATE_CCK_2);
628 if (spec->num_rates > 4) {
629 rt2x00lib_rate(&rates[4], 60, DEV_RATEMASK_6MB,
630 0x0b, IEEE80211_RATE_OFDM);
631 rt2x00lib_rate(&rates[5], 90, DEV_RATEMASK_9MB,
632 0x0f, IEEE80211_RATE_OFDM);
633 rt2x00lib_rate(&rates[6], 120, DEV_RATEMASK_12MB,
634 0x0a, IEEE80211_RATE_OFDM);
635 rt2x00lib_rate(&rates[7], 180, DEV_RATEMASK_18MB,
636 0x0e, IEEE80211_RATE_OFDM);
637 rt2x00lib_rate(&rates[8], 240, DEV_RATEMASK_24MB,
638 0x09, IEEE80211_RATE_OFDM);
639 rt2x00lib_rate(&rates[9], 360, DEV_RATEMASK_36MB,
640 0x0d, IEEE80211_RATE_OFDM);
641 rt2x00lib_rate(&rates[10], 480, DEV_RATEMASK_48MB,
642 0x08, IEEE80211_RATE_OFDM);
643 rt2x00lib_rate(&rates[11], 540, DEV_RATEMASK_54MB,
644 0x0c, IEEE80211_RATE_OFDM);
648 * Initialize Channel list.
650 for (i = 0; i < spec->num_channels; i++) {
651 if (spec->channels[i].channel <= 14)
652 tx_power = spec->tx_power_bg[i];
653 else if (spec->tx_power_a)
654 tx_power = spec->tx_power_a[i];
656 tx_power = spec->tx_power_default;
658 rt2x00lib_channel(&channels[i],
659 spec->channels[i].channel, tx_power, i);
663 * Intitialize 802.11b
667 if (spec->num_modes > HWMODE_B) {
668 hwmodes[HWMODE_B].mode = MODE_IEEE80211B;
669 hwmodes[HWMODE_B].num_channels = 14;
670 hwmodes[HWMODE_B].num_rates = 4;
671 hwmodes[HWMODE_B].channels = channels;
672 hwmodes[HWMODE_B].rates = rates;
676 * Intitialize 802.11g
680 if (spec->num_modes > HWMODE_G) {
681 hwmodes[HWMODE_G].mode = MODE_IEEE80211G;
682 hwmodes[HWMODE_G].num_channels = 14;
683 hwmodes[HWMODE_G].num_rates = spec->num_rates;
684 hwmodes[HWMODE_G].channels = channels;
685 hwmodes[HWMODE_G].rates = rates;
689 * Intitialize 802.11a
691 * Channels: OFDM, UNII, HiperLAN2.
693 if (spec->num_modes > HWMODE_A) {
694 hwmodes[HWMODE_A].mode = MODE_IEEE80211A;
695 hwmodes[HWMODE_A].num_channels = spec->num_channels - 14;
696 hwmodes[HWMODE_A].num_rates = spec->num_rates - 4;
697 hwmodes[HWMODE_A].channels = &channels[14];
698 hwmodes[HWMODE_A].rates = &rates[4];
701 if (spec->num_modes > HWMODE_G &&
702 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_G]))
703 goto exit_free_rates;
705 if (spec->num_modes > HWMODE_B &&
706 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_B]))
707 goto exit_free_rates;
709 if (spec->num_modes > HWMODE_A &&
710 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_A]))
711 goto exit_free_rates;
713 rt2x00dev->hwmodes = hwmodes;
727 ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
731 static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
733 if (test_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags))
734 ieee80211_unregister_hw(rt2x00dev->hw);
736 if (likely(rt2x00dev->hwmodes)) {
737 kfree(rt2x00dev->hwmodes->channels);
738 kfree(rt2x00dev->hwmodes->rates);
739 kfree(rt2x00dev->hwmodes);
740 rt2x00dev->hwmodes = NULL;
744 static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
746 struct hw_mode_spec *spec = &rt2x00dev->spec;
750 * Initialize HW modes.
752 status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
759 status = ieee80211_register_hw(rt2x00dev->hw);
761 rt2x00lib_remove_hw(rt2x00dev);
765 __set_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags);
771 * Initialization/uninitialization handlers.
773 static int rt2x00lib_alloc_entries(struct data_ring *ring,
774 const u16 max_entries, const u16 data_size,
777 struct data_entry *entry;
780 ring->stats.limit = max_entries;
781 ring->data_size = data_size;
782 ring->desc_size = desc_size;
785 * Allocate all ring entries.
787 entry = kzalloc(ring->stats.limit * sizeof(*entry), GFP_KERNEL);
791 for (i = 0; i < ring->stats.limit; i++) {
793 entry[i].ring = ring;
802 static int rt2x00lib_alloc_ring_entries(struct rt2x00_dev *rt2x00dev)
804 struct data_ring *ring;
807 * Allocate the RX ring.
809 if (rt2x00lib_alloc_entries(rt2x00dev->rx, RX_ENTRIES, DATA_FRAME_SIZE,
810 rt2x00dev->ops->rxd_size))
814 * First allocate the TX rings.
816 txring_for_each(rt2x00dev, ring) {
817 if (rt2x00lib_alloc_entries(ring, TX_ENTRIES, DATA_FRAME_SIZE,
818 rt2x00dev->ops->txd_size))
822 if (!test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
826 * Allocate the BEACON ring.
828 if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[0], BEACON_ENTRIES,
829 MGMT_FRAME_SIZE, rt2x00dev->ops->txd_size))
833 * Allocate the Atim ring.
835 if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[1], ATIM_ENTRIES,
836 DATA_FRAME_SIZE, rt2x00dev->ops->txd_size))
842 static void rt2x00lib_free_ring_entries(struct rt2x00_dev *rt2x00dev)
844 struct data_ring *ring;
846 ring_for_each(rt2x00dev, ring) {
852 void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
854 if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
860 rt2x00rfkill_unregister(rt2x00dev);
863 * Allow the HW to uninitialize.
865 rt2x00dev->ops->lib->uninitialize(rt2x00dev);
868 * Free allocated ring entries.
870 rt2x00lib_free_ring_entries(rt2x00dev);
873 int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
877 if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
881 * Allocate all ring entries.
883 status = rt2x00lib_alloc_ring_entries(rt2x00dev);
885 ERROR(rt2x00dev, "Ring entries allocation failed.\n");
890 * Initialize the device.
892 status = rt2x00dev->ops->lib->initialize(rt2x00dev);
896 __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
899 * Register the rfkill handler.
901 status = rt2x00rfkill_register(rt2x00dev);
903 goto exit_unitialize;
908 rt2x00lib_uninitialize(rt2x00dev);
911 rt2x00lib_free_ring_entries(rt2x00dev);
917 * driver allocation handlers.
919 static int rt2x00lib_alloc_rings(struct rt2x00_dev *rt2x00dev)
921 struct data_ring *ring;
924 * We need the following rings:
927 * Beacon: 1 (if required)
928 * Atim: 1 (if required)
930 rt2x00dev->data_rings = 1 + rt2x00dev->hw->queues +
931 (2 * test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags));
933 ring = kzalloc(rt2x00dev->data_rings * sizeof(*ring), GFP_KERNEL);
935 ERROR(rt2x00dev, "Ring allocation failed.\n");
940 * Initialize pointers
942 rt2x00dev->rx = ring;
943 rt2x00dev->tx = &rt2x00dev->rx[1];
944 if (test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
945 rt2x00dev->bcn = &rt2x00dev->tx[rt2x00dev->hw->queues];
948 * Initialize ring parameters.
950 * cw_max: 2^10 = 1024.
952 ring_for_each(rt2x00dev, ring) {
953 ring->rt2x00dev = rt2x00dev;
954 ring->tx_params.aifs = 2;
955 ring->tx_params.cw_min = 5;
956 ring->tx_params.cw_max = 10;
962 static void rt2x00lib_free_rings(struct rt2x00_dev *rt2x00dev)
964 kfree(rt2x00dev->rx);
965 rt2x00dev->rx = NULL;
966 rt2x00dev->tx = NULL;
967 rt2x00dev->bcn = NULL;
970 int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
972 int retval = -ENOMEM;
975 * Let the driver probe the device to detect the capabilities.
977 retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
979 ERROR(rt2x00dev, "Failed to allocate device.\n");
984 * Initialize configuration work.
986 INIT_WORK(&rt2x00dev->beacon_work, rt2x00lib_beacondone_scheduled);
987 INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
988 INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
991 * Reset current working type.
993 rt2x00dev->interface.type = INVALID_INTERFACE;
996 * Allocate ring array.
998 retval = rt2x00lib_alloc_rings(rt2x00dev);
1003 * Initialize ieee80211 structure.
1005 retval = rt2x00lib_probe_hw(rt2x00dev);
1007 ERROR(rt2x00dev, "Failed to initialize hw.\n");
1014 retval = rt2x00rfkill_allocate(rt2x00dev);
1019 * Open the debugfs entry.
1021 rt2x00debug_register(rt2x00dev);
1023 __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1028 rt2x00lib_remove_dev(rt2x00dev);
1032 EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
1034 void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
1036 __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1041 rt2x00lib_disable_radio(rt2x00dev);
1044 * Uninitialize device.
1046 rt2x00lib_uninitialize(rt2x00dev);
1049 * Close debugfs entry.
1051 rt2x00debug_deregister(rt2x00dev);
1056 rt2x00rfkill_free(rt2x00dev);
1059 * Free ieee80211_hw memory.
1061 rt2x00lib_remove_hw(rt2x00dev);
1064 * Free firmware image.
1066 rt2x00lib_free_firmware(rt2x00dev);
1069 * Free ring structures.
1071 rt2x00lib_free_rings(rt2x00dev);
1073 EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
1076 * Device state handlers
1079 int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
1083 NOTICE(rt2x00dev, "Going to sleep.\n");
1084 __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1087 * Only continue if mac80211 has open interfaces.
1089 if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1093 * Disable radio and unitialize all items
1094 * that must be recreated on resume.
1096 rt2x00lib_disable_radio(rt2x00dev);
1097 rt2x00lib_uninitialize(rt2x00dev);
1098 rt2x00debug_deregister(rt2x00dev);
1102 * Set device mode to sleep for power management.
1104 retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
1110 EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1112 int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1114 struct interface *intf = &rt2x00dev->interface;
1117 NOTICE(rt2x00dev, "Waking up.\n");
1118 __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1121 * Open the debugfs entry.
1123 rt2x00debug_register(rt2x00dev);
1126 * Only continue if mac80211 has open interfaces.
1128 if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1132 * Reinitialize device and all active interfaces.
1134 retval = rt2x00mac_start(rt2x00dev->hw);
1139 * Reconfigure device.
1141 rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1);
1142 if (!rt2x00dev->hw->conf.radio_enabled)
1143 rt2x00lib_disable_radio(rt2x00dev);
1145 rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
1146 rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
1147 rt2x00lib_config_type(rt2x00dev, intf->type);
1150 * It is possible that during that mac80211 has attempted
1151 * to send frames while we were suspending or resuming.
1152 * In that case we have disabled the TX queue and should
1153 * now enable it again
1155 ieee80211_start_queues(rt2x00dev->hw);
1158 * When in Master or Ad-hoc mode,
1159 * restart Beacon transmitting by faking a beacondone event.
1161 if (intf->type == IEEE80211_IF_TYPE_AP ||
1162 intf->type == IEEE80211_IF_TYPE_IBSS)
1163 rt2x00lib_beacondone(rt2x00dev);
1168 rt2x00lib_disable_radio(rt2x00dev);
1169 rt2x00lib_uninitialize(rt2x00dev);
1170 rt2x00debug_deregister(rt2x00dev);
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");