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 rt2x00dev->link.count = 0;
71 rt2x00dev->link.vgc_level = 0;
73 memset(&rt2x00dev->link.qual, 0, sizeof(rt2x00dev->link.qual));
76 * The RX and TX percentage should start at 50%
77 * this will assure we will get at least get some
78 * decent value when the link tuner starts.
79 * The value will be dropped and overwritten with
80 * the correct (measured )value anyway during the
81 * first run of the link tuner.
83 rt2x00dev->link.qual.rx_percentage = 50;
84 rt2x00dev->link.qual.tx_percentage = 50;
87 * Reset the link tuner.
89 rt2x00dev->ops->lib->reset_tuner(rt2x00dev);
91 queue_delayed_work(rt2x00dev->hw->workqueue,
92 &rt2x00dev->link.work, LINK_TUNE_INTERVAL);
95 static void rt2x00lib_stop_link_tuner(struct rt2x00_dev *rt2x00dev)
97 cancel_delayed_work_sync(&rt2x00dev->link.work);
100 void rt2x00lib_reset_link_tuner(struct rt2x00_dev *rt2x00dev)
102 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
105 rt2x00lib_stop_link_tuner(rt2x00dev);
106 rt2x00lib_start_link_tuner(rt2x00dev);
110 * Radio control handlers.
112 int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
117 * Don't enable the radio twice.
118 * And check if the hardware button has been disabled.
120 if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
121 test_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags))
127 status = rt2x00dev->ops->lib->set_device_state(rt2x00dev,
132 __set_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags);
137 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
140 * Start the TX queues.
142 ieee80211_start_queues(rt2x00dev->hw);
147 void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
149 if (!__test_and_clear_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
153 * Stop all scheduled work.
155 if (work_pending(&rt2x00dev->beacon_work))
156 cancel_work_sync(&rt2x00dev->beacon_work);
157 if (work_pending(&rt2x00dev->filter_work))
158 cancel_work_sync(&rt2x00dev->filter_work);
159 if (work_pending(&rt2x00dev->config_work))
160 cancel_work_sync(&rt2x00dev->config_work);
163 * Stop the TX queues.
165 ieee80211_stop_queues(rt2x00dev->hw);
170 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
175 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
178 void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, enum dev_state state)
181 * When we are disabling the RX, we should also stop the link tuner.
183 if (state == STATE_RADIO_RX_OFF)
184 rt2x00lib_stop_link_tuner(rt2x00dev);
186 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
189 * When we are enabling the RX, we should also start the link tuner.
191 if (state == STATE_RADIO_RX_ON &&
192 is_interface_present(&rt2x00dev->interface))
193 rt2x00lib_start_link_tuner(rt2x00dev);
196 static void rt2x00lib_precalculate_link_signal(struct link_qual *qual)
198 if (qual->rx_failed || qual->rx_success)
199 qual->rx_percentage =
200 (qual->rx_success * 100) /
201 (qual->rx_failed + qual->rx_success);
203 qual->rx_percentage = 50;
205 if (qual->tx_failed || qual->tx_success)
206 qual->tx_percentage =
207 (qual->tx_success * 100) /
208 (qual->tx_failed + qual->tx_success);
210 qual->tx_percentage = 50;
212 qual->rx_success = 0;
214 qual->tx_success = 0;
218 static int rt2x00lib_calculate_link_signal(struct rt2x00_dev *rt2x00dev,
221 int rssi_percentage = 0;
225 * We need a positive value for the RSSI.
228 rssi += rt2x00dev->rssi_offset;
231 * Calculate the different percentages,
232 * which will be used for the signal.
234 if (rt2x00dev->rssi_offset)
235 rssi_percentage = (rssi * 100) / rt2x00dev->rssi_offset;
238 * Add the individual percentages and use the WEIGHT
239 * defines to calculate the current link signal.
241 signal = ((WEIGHT_RSSI * rssi_percentage) +
242 (WEIGHT_TX * rt2x00dev->link.qual.tx_percentage) +
243 (WEIGHT_RX * rt2x00dev->link.qual.rx_percentage)) / 100;
245 return (signal > 100) ? 100 : signal;
248 static void rt2x00lib_link_tuner(struct work_struct *work)
250 struct rt2x00_dev *rt2x00dev =
251 container_of(work, struct rt2x00_dev, link.work.work);
254 * When the radio is shutting down we should
255 * immediately cease all link tuning.
257 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
263 rt2x00dev->ops->lib->link_stats(rt2x00dev, &rt2x00dev->link.qual);
265 rt2x00dev->low_level_stats.dot11FCSErrorCount +=
266 rt2x00dev->link.qual.rx_failed;
269 * Only perform the link tuning when Link tuning
270 * has been enabled (This could have been disabled from the EEPROM).
272 if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
273 rt2x00dev->ops->lib->link_tuner(rt2x00dev);
276 * Precalculate a portion of the link signal which is
277 * in based on the tx/rx success/failure counters.
279 rt2x00lib_precalculate_link_signal(&rt2x00dev->link.qual);
282 * Increase tuner counter, and reschedule the next link tuner run.
284 rt2x00dev->link.count++;
285 queue_delayed_work(rt2x00dev->hw->workqueue, &rt2x00dev->link.work,
289 static void rt2x00lib_packetfilter_scheduled(struct work_struct *work)
291 struct rt2x00_dev *rt2x00dev =
292 container_of(work, struct rt2x00_dev, filter_work);
293 unsigned int filter = rt2x00dev->interface.filter;
296 * Since we had stored the filter inside interface.filter,
297 * we should now clear that field. Otherwise the driver will
298 * assume nothing has changed (*total_flags will be compared
299 * to interface.filter to determine if any action is required).
301 rt2x00dev->interface.filter = 0;
303 rt2x00dev->ops->hw->configure_filter(rt2x00dev->hw,
304 filter, &filter, 0, NULL);
307 static void rt2x00lib_configuration_scheduled(struct work_struct *work)
309 struct rt2x00_dev *rt2x00dev =
310 container_of(work, struct rt2x00_dev, config_work);
311 int preamble = !test_bit(CONFIG_SHORT_PREAMBLE, &rt2x00dev->flags);
313 rt2x00mac_erp_ie_changed(rt2x00dev->hw,
314 IEEE80211_ERP_CHANGE_PREAMBLE, 0, preamble);
318 * Interrupt context handlers.
320 static void rt2x00lib_beacondone_scheduled(struct work_struct *work)
322 struct rt2x00_dev *rt2x00dev =
323 container_of(work, struct rt2x00_dev, beacon_work);
324 struct data_ring *ring =
325 rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_BEACON);
326 struct data_entry *entry = rt2x00_get_data_entry(ring);
329 skb = ieee80211_beacon_get(rt2x00dev->hw,
330 rt2x00dev->interface.id,
331 &entry->tx_status.control);
335 rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw, skb,
336 &entry->tx_status.control);
341 void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
343 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
346 queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->beacon_work);
348 EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
350 void rt2x00lib_txdone(struct data_entry *entry,
351 const int status, const int retry)
353 struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
354 struct ieee80211_tx_status *tx_status = &entry->tx_status;
355 struct ieee80211_low_level_stats *stats = &rt2x00dev->low_level_stats;
356 int success = !!(status == TX_SUCCESS || status == TX_SUCCESS_RETRY);
357 int fail = !!(status == TX_FAIL_RETRY || status == TX_FAIL_INVALID ||
358 status == TX_FAIL_OTHER);
361 * Update TX statistics.
363 tx_status->flags = 0;
364 tx_status->ack_signal = 0;
365 tx_status->excessive_retries = (status == TX_FAIL_RETRY);
366 tx_status->retry_count = retry;
367 rt2x00dev->link.qual.tx_success += success;
368 rt2x00dev->link.qual.tx_failed += retry + fail;
370 if (!(tx_status->control.flags & IEEE80211_TXCTL_NO_ACK)) {
372 tx_status->flags |= IEEE80211_TX_STATUS_ACK;
374 stats->dot11ACKFailureCount++;
377 tx_status->queue_length = entry->ring->stats.limit;
378 tx_status->queue_number = tx_status->control.queue;
380 if (tx_status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) {
382 stats->dot11RTSSuccessCount++;
384 stats->dot11RTSFailureCount++;
388 * Send the tx_status to mac80211,
389 * that method also cleans up the skb structure.
391 ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb, tx_status);
394 EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
396 void rt2x00lib_rxdone(struct data_entry *entry, struct sk_buff *skb,
397 struct rxdata_entry_desc *desc)
399 struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
400 struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
401 struct ieee80211_hw_mode *mode;
402 struct ieee80211_rate *rate;
407 * Update RX statistics.
409 mode = &rt2x00dev->hwmodes[rt2x00dev->curr_hwmode];
410 for (i = 0; i < mode->num_rates; i++) {
411 rate = &mode->rates[i];
414 * When frame was received with an OFDM bitrate,
415 * the signal is the PLCP value. If it was received with
416 * a CCK bitrate the signal is the rate in 0.5kbit/s.
419 val = DEVICE_GET_RATE_FIELD(rate->val, RATE);
421 val = DEVICE_GET_RATE_FIELD(rate->val, PLCP);
423 if (val == desc->signal) {
429 rt2x00_update_link_rssi(&rt2x00dev->link, desc->rssi);
430 rt2x00dev->link.qual.rx_success++;
431 rx_status->rate = val;
433 rt2x00lib_calculate_link_signal(rt2x00dev, desc->rssi);
434 rx_status->ssi = desc->rssi;
435 rx_status->flag = desc->flags;
436 rx_status->antenna = rt2x00dev->link.active_ant.rx;
439 * Send frame to mac80211
441 ieee80211_rx_irqsafe(rt2x00dev->hw, skb, rx_status);
443 EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
446 * TX descriptor initializer
448 void rt2x00lib_write_tx_desc(struct rt2x00_dev *rt2x00dev,
449 struct data_desc *txd,
450 struct ieee80211_hdr *ieee80211hdr,
452 struct ieee80211_tx_control *control)
454 struct txdata_entry_desc desc;
455 struct data_ring *ring;
464 * Make sure the descriptor is properly cleared.
466 memset(&desc, 0x00, sizeof(desc));
469 * Get ring pointer, if we fail to obtain the
470 * correct ring, then use the first TX ring.
472 ring = rt2x00lib_get_ring(rt2x00dev, control->queue);
474 ring = rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_DATA0);
476 desc.cw_min = ring->tx_params.cw_min;
477 desc.cw_max = ring->tx_params.cw_max;
478 desc.aifs = ring->tx_params.aifs;
483 if (control->queue < rt2x00dev->hw->queues)
484 desc.queue = control->queue;
485 else if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
486 control->queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
487 desc.queue = QUEUE_MGMT;
489 desc.queue = QUEUE_OTHER;
492 * Read required fields from ieee80211 header.
494 frame_control = le16_to_cpu(ieee80211hdr->frame_control);
495 seq_ctrl = le16_to_cpu(ieee80211hdr->seq_ctrl);
497 tx_rate = control->tx_rate;
500 * Check if this is a RTS/CTS frame
502 if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
503 __set_bit(ENTRY_TXD_BURST, &desc.flags);
504 if (is_rts_frame(frame_control))
505 __set_bit(ENTRY_TXD_RTS_FRAME, &desc.flags);
506 if (control->rts_cts_rate)
507 tx_rate = control->rts_cts_rate;
513 if (DEVICE_GET_RATE_FIELD(tx_rate, RATEMASK) & DEV_OFDM_RATEMASK)
514 __set_bit(ENTRY_TXD_OFDM_RATE, &desc.flags);
517 * Check if more fragments are pending
519 if (ieee80211_get_morefrag(ieee80211hdr)) {
520 __set_bit(ENTRY_TXD_BURST, &desc.flags);
521 __set_bit(ENTRY_TXD_MORE_FRAG, &desc.flags);
525 * Beacons and probe responses require the tsf timestamp
526 * to be inserted into the frame.
528 if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
529 is_probe_resp(frame_control))
530 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &desc.flags);
533 * Determine with what IFS priority this frame should be send.
534 * Set ifs to IFS_SIFS when the this is not the first fragment,
535 * or this fragment came after RTS/CTS.
537 if ((seq_ctrl & IEEE80211_SCTL_FRAG) > 0 ||
538 test_bit(ENTRY_TXD_RTS_FRAME, &desc.flags))
541 desc.ifs = IFS_BACKOFF;
545 * Length calculation depends on OFDM/CCK rate.
547 desc.signal = DEVICE_GET_RATE_FIELD(tx_rate, PLCP);
550 if (test_bit(ENTRY_TXD_OFDM_RATE, &desc.flags)) {
551 desc.length_high = ((length + FCS_LEN) >> 6) & 0x3f;
552 desc.length_low = ((length + FCS_LEN) & 0x3f);
554 bitrate = DEVICE_GET_RATE_FIELD(tx_rate, RATE);
557 * Convert length to microseconds.
559 residual = get_duration_res(length + FCS_LEN, bitrate);
560 duration = get_duration(length + FCS_LEN, bitrate);
566 * Check if we need to set the Length Extension
568 if (bitrate == 110 && residual <= 30)
569 desc.service |= 0x80;
572 desc.length_high = (duration >> 8) & 0xff;
573 desc.length_low = duration & 0xff;
576 * When preamble is enabled we should set the
577 * preamble bit for the signal.
579 if (DEVICE_GET_RATE_FIELD(tx_rate, PREAMBLE))
583 rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, txd, &desc,
584 ieee80211hdr, length, control);
586 EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc);
589 * Driver initialization handlers.
591 static void rt2x00lib_channel(struct ieee80211_channel *entry,
592 const int channel, const int tx_power,
595 entry->chan = channel;
597 entry->freq = 2407 + (5 * channel);
599 entry->freq = 5000 + (5 * channel);
602 IEEE80211_CHAN_W_IBSS |
603 IEEE80211_CHAN_W_ACTIVE_SCAN |
604 IEEE80211_CHAN_W_SCAN;
605 entry->power_level = tx_power;
606 entry->antenna_max = 0xff;
609 static void rt2x00lib_rate(struct ieee80211_rate *entry,
610 const int rate, const int mask,
611 const int plcp, const int flags)
615 DEVICE_SET_RATE_FIELD(rate, RATE) |
616 DEVICE_SET_RATE_FIELD(mask, RATEMASK) |
617 DEVICE_SET_RATE_FIELD(plcp, PLCP);
618 entry->flags = flags;
619 entry->val2 = entry->val;
620 if (entry->flags & IEEE80211_RATE_PREAMBLE2)
621 entry->val2 |= DEVICE_SET_RATE_FIELD(1, PREAMBLE);
622 entry->min_rssi_ack = 0;
623 entry->min_rssi_ack_delta = 0;
626 static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
627 struct hw_mode_spec *spec)
629 struct ieee80211_hw *hw = rt2x00dev->hw;
630 struct ieee80211_hw_mode *hwmodes;
631 struct ieee80211_channel *channels;
632 struct ieee80211_rate *rates;
634 unsigned char tx_power;
636 hwmodes = kzalloc(sizeof(*hwmodes) * spec->num_modes, GFP_KERNEL);
640 channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
642 goto exit_free_modes;
644 rates = kzalloc(sizeof(*rates) * spec->num_rates, GFP_KERNEL);
646 goto exit_free_channels;
649 * Initialize Rate list.
651 rt2x00lib_rate(&rates[0], 10, DEV_RATEMASK_1MB,
652 0x00, IEEE80211_RATE_CCK);
653 rt2x00lib_rate(&rates[1], 20, DEV_RATEMASK_2MB,
654 0x01, IEEE80211_RATE_CCK_2);
655 rt2x00lib_rate(&rates[2], 55, DEV_RATEMASK_5_5MB,
656 0x02, IEEE80211_RATE_CCK_2);
657 rt2x00lib_rate(&rates[3], 110, DEV_RATEMASK_11MB,
658 0x03, IEEE80211_RATE_CCK_2);
660 if (spec->num_rates > 4) {
661 rt2x00lib_rate(&rates[4], 60, DEV_RATEMASK_6MB,
662 0x0b, IEEE80211_RATE_OFDM);
663 rt2x00lib_rate(&rates[5], 90, DEV_RATEMASK_9MB,
664 0x0f, IEEE80211_RATE_OFDM);
665 rt2x00lib_rate(&rates[6], 120, DEV_RATEMASK_12MB,
666 0x0a, IEEE80211_RATE_OFDM);
667 rt2x00lib_rate(&rates[7], 180, DEV_RATEMASK_18MB,
668 0x0e, IEEE80211_RATE_OFDM);
669 rt2x00lib_rate(&rates[8], 240, DEV_RATEMASK_24MB,
670 0x09, IEEE80211_RATE_OFDM);
671 rt2x00lib_rate(&rates[9], 360, DEV_RATEMASK_36MB,
672 0x0d, IEEE80211_RATE_OFDM);
673 rt2x00lib_rate(&rates[10], 480, DEV_RATEMASK_48MB,
674 0x08, IEEE80211_RATE_OFDM);
675 rt2x00lib_rate(&rates[11], 540, DEV_RATEMASK_54MB,
676 0x0c, IEEE80211_RATE_OFDM);
680 * Initialize Channel list.
682 for (i = 0; i < spec->num_channels; i++) {
683 if (spec->channels[i].channel <= 14)
684 tx_power = spec->tx_power_bg[i];
685 else if (spec->tx_power_a)
686 tx_power = spec->tx_power_a[i];
688 tx_power = spec->tx_power_default;
690 rt2x00lib_channel(&channels[i],
691 spec->channels[i].channel, tx_power, i);
695 * Intitialize 802.11b
699 if (spec->num_modes > HWMODE_B) {
700 hwmodes[HWMODE_B].mode = MODE_IEEE80211B;
701 hwmodes[HWMODE_B].num_channels = 14;
702 hwmodes[HWMODE_B].num_rates = 4;
703 hwmodes[HWMODE_B].channels = channels;
704 hwmodes[HWMODE_B].rates = rates;
708 * Intitialize 802.11g
712 if (spec->num_modes > HWMODE_G) {
713 hwmodes[HWMODE_G].mode = MODE_IEEE80211G;
714 hwmodes[HWMODE_G].num_channels = 14;
715 hwmodes[HWMODE_G].num_rates = spec->num_rates;
716 hwmodes[HWMODE_G].channels = channels;
717 hwmodes[HWMODE_G].rates = rates;
721 * Intitialize 802.11a
723 * Channels: OFDM, UNII, HiperLAN2.
725 if (spec->num_modes > HWMODE_A) {
726 hwmodes[HWMODE_A].mode = MODE_IEEE80211A;
727 hwmodes[HWMODE_A].num_channels = spec->num_channels - 14;
728 hwmodes[HWMODE_A].num_rates = spec->num_rates - 4;
729 hwmodes[HWMODE_A].channels = &channels[14];
730 hwmodes[HWMODE_A].rates = &rates[4];
733 if (spec->num_modes > HWMODE_G &&
734 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_G]))
735 goto exit_free_rates;
737 if (spec->num_modes > HWMODE_B &&
738 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_B]))
739 goto exit_free_rates;
741 if (spec->num_modes > HWMODE_A &&
742 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_A]))
743 goto exit_free_rates;
745 rt2x00dev->hwmodes = hwmodes;
759 ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
763 static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
765 if (test_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags))
766 ieee80211_unregister_hw(rt2x00dev->hw);
768 if (likely(rt2x00dev->hwmodes)) {
769 kfree(rt2x00dev->hwmodes->channels);
770 kfree(rt2x00dev->hwmodes->rates);
771 kfree(rt2x00dev->hwmodes);
772 rt2x00dev->hwmodes = NULL;
776 static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
778 struct hw_mode_spec *spec = &rt2x00dev->spec;
782 * Initialize HW modes.
784 status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
791 status = ieee80211_register_hw(rt2x00dev->hw);
793 rt2x00lib_remove_hw(rt2x00dev);
797 __set_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags);
803 * Initialization/uninitialization handlers.
805 static int rt2x00lib_alloc_entries(struct data_ring *ring,
806 const u16 max_entries, const u16 data_size,
809 struct data_entry *entry;
812 ring->stats.limit = max_entries;
813 ring->data_size = data_size;
814 ring->desc_size = desc_size;
817 * Allocate all ring entries.
819 entry = kzalloc(ring->stats.limit * sizeof(*entry), GFP_KERNEL);
823 for (i = 0; i < ring->stats.limit; i++) {
825 entry[i].ring = ring;
834 static int rt2x00lib_alloc_ring_entries(struct rt2x00_dev *rt2x00dev)
836 struct data_ring *ring;
839 * Allocate the RX ring.
841 if (rt2x00lib_alloc_entries(rt2x00dev->rx, RX_ENTRIES, DATA_FRAME_SIZE,
842 rt2x00dev->ops->rxd_size))
846 * First allocate the TX rings.
848 txring_for_each(rt2x00dev, ring) {
849 if (rt2x00lib_alloc_entries(ring, TX_ENTRIES, DATA_FRAME_SIZE,
850 rt2x00dev->ops->txd_size))
854 if (!test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
858 * Allocate the BEACON ring.
860 if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[0], BEACON_ENTRIES,
861 MGMT_FRAME_SIZE, rt2x00dev->ops->txd_size))
865 * Allocate the Atim ring.
867 if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[1], ATIM_ENTRIES,
868 DATA_FRAME_SIZE, rt2x00dev->ops->txd_size))
874 static void rt2x00lib_free_ring_entries(struct rt2x00_dev *rt2x00dev)
876 struct data_ring *ring;
878 ring_for_each(rt2x00dev, ring) {
884 void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
886 if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
892 rt2x00rfkill_unregister(rt2x00dev);
895 * Allow the HW to uninitialize.
897 rt2x00dev->ops->lib->uninitialize(rt2x00dev);
900 * Free allocated ring entries.
902 rt2x00lib_free_ring_entries(rt2x00dev);
905 int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
909 if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
913 * Allocate all ring entries.
915 status = rt2x00lib_alloc_ring_entries(rt2x00dev);
917 ERROR(rt2x00dev, "Ring entries allocation failed.\n");
922 * Initialize the device.
924 status = rt2x00dev->ops->lib->initialize(rt2x00dev);
928 __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
931 * Register the rfkill handler.
933 status = rt2x00rfkill_register(rt2x00dev);
935 goto exit_unitialize;
940 rt2x00lib_uninitialize(rt2x00dev);
943 rt2x00lib_free_ring_entries(rt2x00dev);
949 * driver allocation handlers.
951 static int rt2x00lib_alloc_rings(struct rt2x00_dev *rt2x00dev)
953 struct data_ring *ring;
956 * We need the following rings:
959 * Beacon: 1 (if required)
960 * Atim: 1 (if required)
962 rt2x00dev->data_rings = 1 + rt2x00dev->hw->queues +
963 (2 * test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags));
965 ring = kzalloc(rt2x00dev->data_rings * sizeof(*ring), GFP_KERNEL);
967 ERROR(rt2x00dev, "Ring allocation failed.\n");
972 * Initialize pointers
974 rt2x00dev->rx = ring;
975 rt2x00dev->tx = &rt2x00dev->rx[1];
976 if (test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
977 rt2x00dev->bcn = &rt2x00dev->tx[rt2x00dev->hw->queues];
980 * Initialize ring parameters.
982 * cw_max: 2^10 = 1024.
984 ring_for_each(rt2x00dev, ring) {
985 ring->rt2x00dev = rt2x00dev;
986 ring->tx_params.aifs = 2;
987 ring->tx_params.cw_min = 5;
988 ring->tx_params.cw_max = 10;
994 static void rt2x00lib_free_rings(struct rt2x00_dev *rt2x00dev)
996 kfree(rt2x00dev->rx);
997 rt2x00dev->rx = NULL;
998 rt2x00dev->tx = NULL;
999 rt2x00dev->bcn = NULL;
1002 int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
1004 int retval = -ENOMEM;
1007 * Let the driver probe the device to detect the capabilities.
1009 retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
1011 ERROR(rt2x00dev, "Failed to allocate device.\n");
1016 * Initialize configuration work.
1018 INIT_WORK(&rt2x00dev->beacon_work, rt2x00lib_beacondone_scheduled);
1019 INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
1020 INIT_WORK(&rt2x00dev->config_work, rt2x00lib_configuration_scheduled);
1021 INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
1024 * Reset current working type.
1026 rt2x00dev->interface.type = INVALID_INTERFACE;
1029 * Allocate ring array.
1031 retval = rt2x00lib_alloc_rings(rt2x00dev);
1036 * Initialize ieee80211 structure.
1038 retval = rt2x00lib_probe_hw(rt2x00dev);
1040 ERROR(rt2x00dev, "Failed to initialize hw.\n");
1047 retval = rt2x00rfkill_allocate(rt2x00dev);
1052 * Open the debugfs entry.
1054 rt2x00debug_register(rt2x00dev);
1056 __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1061 rt2x00lib_remove_dev(rt2x00dev);
1065 EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
1067 void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
1069 __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1074 rt2x00lib_disable_radio(rt2x00dev);
1077 * Uninitialize device.
1079 rt2x00lib_uninitialize(rt2x00dev);
1082 * Close debugfs entry.
1084 rt2x00debug_deregister(rt2x00dev);
1089 rt2x00rfkill_free(rt2x00dev);
1092 * Free ieee80211_hw memory.
1094 rt2x00lib_remove_hw(rt2x00dev);
1097 * Free firmware image.
1099 rt2x00lib_free_firmware(rt2x00dev);
1102 * Free ring structures.
1104 rt2x00lib_free_rings(rt2x00dev);
1106 EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
1109 * Device state handlers
1112 int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
1116 NOTICE(rt2x00dev, "Going to sleep.\n");
1117 __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1120 * Only continue if mac80211 has open interfaces.
1122 if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1124 __set_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags);
1127 * Disable radio and unitialize all items
1128 * that must be recreated on resume.
1130 rt2x00mac_stop(rt2x00dev->hw);
1131 rt2x00lib_uninitialize(rt2x00dev);
1132 rt2x00debug_deregister(rt2x00dev);
1136 * Set device mode to sleep for power management.
1138 retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
1144 EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1146 int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1148 struct interface *intf = &rt2x00dev->interface;
1151 NOTICE(rt2x00dev, "Waking up.\n");
1152 __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1155 * Open the debugfs entry.
1157 rt2x00debug_register(rt2x00dev);
1160 * Only continue if mac80211 had open interfaces.
1162 if (!__test_and_clear_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags))
1166 * Reinitialize device and all active interfaces.
1168 retval = rt2x00mac_start(rt2x00dev->hw);
1173 * Reconfigure device.
1175 rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1);
1176 if (!rt2x00dev->hw->conf.radio_enabled)
1177 rt2x00lib_disable_radio(rt2x00dev);
1179 rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
1180 rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
1181 rt2x00lib_config_type(rt2x00dev, intf->type);
1184 * It is possible that during that mac80211 has attempted
1185 * to send frames while we were suspending or resuming.
1186 * In that case we have disabled the TX queue and should
1187 * now enable it again
1189 ieee80211_start_queues(rt2x00dev->hw);
1192 * When in Master or Ad-hoc mode,
1193 * restart Beacon transmitting by faking a beacondone event.
1195 if (intf->type == IEEE80211_IF_TYPE_AP ||
1196 intf->type == IEEE80211_IF_TYPE_IBSS)
1197 rt2x00lib_beacondone(rt2x00dev);
1202 rt2x00lib_disable_radio(rt2x00dev);
1203 rt2x00lib_uninitialize(rt2x00dev);
1204 rt2x00debug_deregister(rt2x00dev);
1208 EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1209 #endif /* CONFIG_PM */
1212 * rt2x00lib module information.
1214 MODULE_AUTHOR(DRV_PROJECT);
1215 MODULE_VERSION(DRV_VERSION);
1216 MODULE_DESCRIPTION("rt2x00 library");
1217 MODULE_LICENSE("GPL");