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_evaluate_antenna_sample(struct rt2x00_dev *rt2x00dev)
198 enum antenna rx = rt2x00dev->link.ant.active.rx;
199 enum antenna tx = rt2x00dev->link.ant.active.tx;
201 rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_A);
203 rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_B);
206 * We are done sampling. Now we should evaluate the results.
208 rt2x00dev->link.ant.flags &= ~ANTENNA_MODE_SAMPLE;
211 * During the last period we have sampled the RSSI
212 * from both antenna's. It now is time to determine
213 * which antenna demonstrated the best performance.
214 * When we are already on the antenna with the best
215 * performance, then there really is nothing for us
218 if (sample_a == sample_b)
221 if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY) {
222 if (sample_a > sample_b && rx == ANTENNA_B)
224 else if (rx == ANTENNA_A)
228 if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY) {
229 if (sample_a > sample_b && tx == ANTENNA_B)
231 else if (tx == ANTENNA_A)
235 rt2x00lib_config_antenna(rt2x00dev, rx, tx);
238 static void rt2x00lib_evaluate_antenna_eval(struct rt2x00_dev *rt2x00dev)
240 enum antenna rx = rt2x00dev->link.ant.active.rx;
241 enum antenna tx = rt2x00dev->link.ant.active.tx;
242 int rssi_curr = rt2x00_get_link_ant_rssi(&rt2x00dev->link);
243 int rssi_old = rt2x00_update_ant_rssi(&rt2x00dev->link, rssi_curr);
246 * Legacy driver indicates that we should swap antenna's
247 * when the difference in RSSI is greater that 5. This
248 * also should be done when the RSSI was actually better
249 * then the previous sample.
250 * When the difference exceeds the threshold we should
251 * sample the rssi from the other antenna to make a valid
252 * comparison between the 2 antennas.
254 if ((rssi_curr - rssi_old) > -5 || (rssi_curr - rssi_old) < 5)
257 rt2x00dev->link.ant.flags |= ANTENNA_MODE_SAMPLE;
259 if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY)
260 rx = (rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
262 if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)
263 tx = (tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
265 rt2x00lib_config_antenna(rt2x00dev, rx, tx);
268 static void rt2x00lib_evaluate_antenna(struct rt2x00_dev *rt2x00dev)
271 * Determine if software diversity is enabled for
272 * either the TX or RX antenna (or both).
273 * Always perform this check since within the link
274 * tuner interval the configuration might have changed.
276 rt2x00dev->link.ant.flags &= ~ANTENNA_RX_DIVERSITY;
277 rt2x00dev->link.ant.flags &= ~ANTENNA_TX_DIVERSITY;
279 if (rt2x00dev->hw->conf.antenna_sel_rx == 0 &&
280 rt2x00dev->default_ant.rx != ANTENNA_SW_DIVERSITY)
281 rt2x00dev->link.ant.flags |= ANTENNA_RX_DIVERSITY;
282 if (rt2x00dev->hw->conf.antenna_sel_tx == 0 &&
283 rt2x00dev->default_ant.tx != ANTENNA_SW_DIVERSITY)
284 rt2x00dev->link.ant.flags |= ANTENNA_TX_DIVERSITY;
286 if (!(rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY) &&
287 !(rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)) {
288 rt2x00dev->link.ant.flags &= ~ANTENNA_MODE_SAMPLE;
293 * If we have only sampled the data over the last period
294 * we should now harvest the data. Otherwise just evaluate
295 * the data. The latter should only be performed once
298 if (rt2x00dev->link.ant.flags & ANTENNA_MODE_SAMPLE)
299 rt2x00lib_evaluate_antenna_sample(rt2x00dev);
300 else if (rt2x00dev->link.count & 1)
301 rt2x00lib_evaluate_antenna_eval(rt2x00dev);
304 static void rt2x00lib_update_link_stats(struct link *link, int rssi)
311 if (link->qual.avg_rssi)
312 avg_rssi = MOVING_AVERAGE(link->qual.avg_rssi, rssi, 8);
313 link->qual.avg_rssi = avg_rssi;
316 * Update antenna RSSI
318 if (link->ant.rssi_ant)
319 rssi = MOVING_AVERAGE(link->ant.rssi_ant, rssi, 8);
320 link->ant.rssi_ant = rssi;
323 static void rt2x00lib_precalculate_link_signal(struct link_qual *qual)
325 if (qual->rx_failed || qual->rx_success)
326 qual->rx_percentage =
327 (qual->rx_success * 100) /
328 (qual->rx_failed + qual->rx_success);
330 qual->rx_percentage = 50;
332 if (qual->tx_failed || qual->tx_success)
333 qual->tx_percentage =
334 (qual->tx_success * 100) /
335 (qual->tx_failed + qual->tx_success);
337 qual->tx_percentage = 50;
339 qual->rx_success = 0;
341 qual->tx_success = 0;
345 static int rt2x00lib_calculate_link_signal(struct rt2x00_dev *rt2x00dev,
348 int rssi_percentage = 0;
352 * We need a positive value for the RSSI.
355 rssi += rt2x00dev->rssi_offset;
358 * Calculate the different percentages,
359 * which will be used for the signal.
361 if (rt2x00dev->rssi_offset)
362 rssi_percentage = (rssi * 100) / rt2x00dev->rssi_offset;
365 * Add the individual percentages and use the WEIGHT
366 * defines to calculate the current link signal.
368 signal = ((WEIGHT_RSSI * rssi_percentage) +
369 (WEIGHT_TX * rt2x00dev->link.qual.tx_percentage) +
370 (WEIGHT_RX * rt2x00dev->link.qual.rx_percentage)) / 100;
372 return (signal > 100) ? 100 : signal;
375 static void rt2x00lib_link_tuner(struct work_struct *work)
377 struct rt2x00_dev *rt2x00dev =
378 container_of(work, struct rt2x00_dev, link.work.work);
381 * When the radio is shutting down we should
382 * immediately cease all link tuning.
384 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
390 rt2x00dev->ops->lib->link_stats(rt2x00dev, &rt2x00dev->link.qual);
391 rt2x00dev->low_level_stats.dot11FCSErrorCount +=
392 rt2x00dev->link.qual.rx_failed;
395 * Only perform the link tuning when Link tuning
396 * has been enabled (This could have been disabled from the EEPROM).
398 if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
399 rt2x00dev->ops->lib->link_tuner(rt2x00dev);
402 * Evaluate antenna setup.
404 rt2x00lib_evaluate_antenna(rt2x00dev);
407 * Precalculate a portion of the link signal which is
408 * in based on the tx/rx success/failure counters.
410 rt2x00lib_precalculate_link_signal(&rt2x00dev->link.qual);
413 * Increase tuner counter, and reschedule the next link tuner run.
415 rt2x00dev->link.count++;
416 queue_delayed_work(rt2x00dev->hw->workqueue, &rt2x00dev->link.work,
420 static void rt2x00lib_packetfilter_scheduled(struct work_struct *work)
422 struct rt2x00_dev *rt2x00dev =
423 container_of(work, struct rt2x00_dev, filter_work);
424 unsigned int filter = rt2x00dev->interface.filter;
427 * Since we had stored the filter inside interface.filter,
428 * we should now clear that field. Otherwise the driver will
429 * assume nothing has changed (*total_flags will be compared
430 * to interface.filter to determine if any action is required).
432 rt2x00dev->interface.filter = 0;
434 rt2x00dev->ops->hw->configure_filter(rt2x00dev->hw,
435 filter, &filter, 0, NULL);
438 static void rt2x00lib_configuration_scheduled(struct work_struct *work)
440 struct rt2x00_dev *rt2x00dev =
441 container_of(work, struct rt2x00_dev, config_work);
442 int preamble = !test_bit(CONFIG_SHORT_PREAMBLE, &rt2x00dev->flags);
444 rt2x00mac_erp_ie_changed(rt2x00dev->hw,
445 IEEE80211_ERP_CHANGE_PREAMBLE, 0, preamble);
449 * Interrupt context handlers.
451 static void rt2x00lib_beacondone_scheduled(struct work_struct *work)
453 struct rt2x00_dev *rt2x00dev =
454 container_of(work, struct rt2x00_dev, beacon_work);
455 struct data_ring *ring =
456 rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_BEACON);
457 struct data_entry *entry = rt2x00_get_data_entry(ring);
460 skb = ieee80211_beacon_get(rt2x00dev->hw,
461 rt2x00dev->interface.id,
462 &entry->tx_status.control);
466 rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw, skb,
467 &entry->tx_status.control);
472 void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
474 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
477 queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->beacon_work);
479 EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
481 void rt2x00lib_txdone(struct data_entry *entry,
482 const int status, const int retry)
484 struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
485 struct ieee80211_tx_status *tx_status = &entry->tx_status;
486 struct ieee80211_low_level_stats *stats = &rt2x00dev->low_level_stats;
487 int success = !!(status == TX_SUCCESS || status == TX_SUCCESS_RETRY);
488 int fail = !!(status == TX_FAIL_RETRY || status == TX_FAIL_INVALID ||
489 status == TX_FAIL_OTHER);
492 * Update TX statistics.
494 tx_status->flags = 0;
495 tx_status->ack_signal = 0;
496 tx_status->excessive_retries = (status == TX_FAIL_RETRY);
497 tx_status->retry_count = retry;
498 rt2x00dev->link.qual.tx_success += success;
499 rt2x00dev->link.qual.tx_failed += retry + fail;
501 if (!(tx_status->control.flags & IEEE80211_TXCTL_NO_ACK)) {
503 tx_status->flags |= IEEE80211_TX_STATUS_ACK;
505 stats->dot11ACKFailureCount++;
508 tx_status->queue_length = entry->ring->stats.limit;
509 tx_status->queue_number = tx_status->control.queue;
511 if (tx_status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) {
513 stats->dot11RTSSuccessCount++;
515 stats->dot11RTSFailureCount++;
519 * Send the tx_status to mac80211,
520 * that method also cleans up the skb structure.
522 ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb, tx_status);
525 EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
527 void rt2x00lib_rxdone(struct data_entry *entry, struct sk_buff *skb,
528 struct rxdata_entry_desc *desc)
530 struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
531 struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
532 struct ieee80211_hw_mode *mode;
533 struct ieee80211_rate *rate;
538 * Update RX statistics.
540 mode = &rt2x00dev->hwmodes[rt2x00dev->curr_hwmode];
541 for (i = 0; i < mode->num_rates; i++) {
542 rate = &mode->rates[i];
545 * When frame was received with an OFDM bitrate,
546 * the signal is the PLCP value. If it was received with
547 * a CCK bitrate the signal is the rate in 0.5kbit/s.
550 val = DEVICE_GET_RATE_FIELD(rate->val, RATE);
552 val = DEVICE_GET_RATE_FIELD(rate->val, PLCP);
554 if (val == desc->signal) {
560 rt2x00lib_update_link_stats(&rt2x00dev->link, desc->rssi);
561 rt2x00dev->link.qual.rx_success++;
563 rx_status->rate = val;
565 rt2x00lib_calculate_link_signal(rt2x00dev, desc->rssi);
566 rx_status->ssi = desc->rssi;
567 rx_status->flag = desc->flags;
568 rx_status->antenna = rt2x00dev->link.ant.active.rx;
571 * Send frame to mac80211
573 ieee80211_rx_irqsafe(rt2x00dev->hw, skb, rx_status);
575 EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
578 * TX descriptor initializer
580 void rt2x00lib_write_tx_desc(struct rt2x00_dev *rt2x00dev,
581 struct data_desc *txd,
582 struct ieee80211_hdr *ieee80211hdr,
584 struct ieee80211_tx_control *control)
586 struct txdata_entry_desc desc;
587 struct data_ring *ring;
596 * Make sure the descriptor is properly cleared.
598 memset(&desc, 0x00, sizeof(desc));
601 * Get ring pointer, if we fail to obtain the
602 * correct ring, then use the first TX ring.
604 ring = rt2x00lib_get_ring(rt2x00dev, control->queue);
606 ring = rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_DATA0);
608 desc.cw_min = ring->tx_params.cw_min;
609 desc.cw_max = ring->tx_params.cw_max;
610 desc.aifs = ring->tx_params.aifs;
615 if (control->queue < rt2x00dev->hw->queues)
616 desc.queue = control->queue;
617 else if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
618 control->queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
619 desc.queue = QUEUE_MGMT;
621 desc.queue = QUEUE_OTHER;
624 * Read required fields from ieee80211 header.
626 frame_control = le16_to_cpu(ieee80211hdr->frame_control);
627 seq_ctrl = le16_to_cpu(ieee80211hdr->seq_ctrl);
629 tx_rate = control->tx_rate;
632 * Check if this is a RTS/CTS frame
634 if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
635 __set_bit(ENTRY_TXD_BURST, &desc.flags);
636 if (is_rts_frame(frame_control))
637 __set_bit(ENTRY_TXD_RTS_FRAME, &desc.flags);
638 if (control->rts_cts_rate)
639 tx_rate = control->rts_cts_rate;
645 if (DEVICE_GET_RATE_FIELD(tx_rate, RATEMASK) & DEV_OFDM_RATEMASK)
646 __set_bit(ENTRY_TXD_OFDM_RATE, &desc.flags);
649 * Check if more fragments are pending
651 if (ieee80211_get_morefrag(ieee80211hdr)) {
652 __set_bit(ENTRY_TXD_BURST, &desc.flags);
653 __set_bit(ENTRY_TXD_MORE_FRAG, &desc.flags);
657 * Beacons and probe responses require the tsf timestamp
658 * to be inserted into the frame.
660 if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
661 is_probe_resp(frame_control))
662 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &desc.flags);
665 * Determine with what IFS priority this frame should be send.
666 * Set ifs to IFS_SIFS when the this is not the first fragment,
667 * or this fragment came after RTS/CTS.
669 if ((seq_ctrl & IEEE80211_SCTL_FRAG) > 0 ||
670 test_bit(ENTRY_TXD_RTS_FRAME, &desc.flags))
673 desc.ifs = IFS_BACKOFF;
677 * Length calculation depends on OFDM/CCK rate.
679 desc.signal = DEVICE_GET_RATE_FIELD(tx_rate, PLCP);
682 if (test_bit(ENTRY_TXD_OFDM_RATE, &desc.flags)) {
683 desc.length_high = ((length + FCS_LEN) >> 6) & 0x3f;
684 desc.length_low = ((length + FCS_LEN) & 0x3f);
686 bitrate = DEVICE_GET_RATE_FIELD(tx_rate, RATE);
689 * Convert length to microseconds.
691 residual = get_duration_res(length + FCS_LEN, bitrate);
692 duration = get_duration(length + FCS_LEN, bitrate);
698 * Check if we need to set the Length Extension
700 if (bitrate == 110 && residual <= 30)
701 desc.service |= 0x80;
704 desc.length_high = (duration >> 8) & 0xff;
705 desc.length_low = duration & 0xff;
708 * When preamble is enabled we should set the
709 * preamble bit for the signal.
711 if (DEVICE_GET_RATE_FIELD(tx_rate, PREAMBLE))
715 rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, txd, &desc,
716 ieee80211hdr, length, control);
718 EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc);
721 * Driver initialization handlers.
723 static void rt2x00lib_channel(struct ieee80211_channel *entry,
724 const int channel, const int tx_power,
727 entry->chan = channel;
729 entry->freq = 2407 + (5 * channel);
731 entry->freq = 5000 + (5 * channel);
734 IEEE80211_CHAN_W_IBSS |
735 IEEE80211_CHAN_W_ACTIVE_SCAN |
736 IEEE80211_CHAN_W_SCAN;
737 entry->power_level = tx_power;
738 entry->antenna_max = 0xff;
741 static void rt2x00lib_rate(struct ieee80211_rate *entry,
742 const int rate, const int mask,
743 const int plcp, const int flags)
747 DEVICE_SET_RATE_FIELD(rate, RATE) |
748 DEVICE_SET_RATE_FIELD(mask, RATEMASK) |
749 DEVICE_SET_RATE_FIELD(plcp, PLCP);
750 entry->flags = flags;
751 entry->val2 = entry->val;
752 if (entry->flags & IEEE80211_RATE_PREAMBLE2)
753 entry->val2 |= DEVICE_SET_RATE_FIELD(1, PREAMBLE);
754 entry->min_rssi_ack = 0;
755 entry->min_rssi_ack_delta = 0;
758 static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
759 struct hw_mode_spec *spec)
761 struct ieee80211_hw *hw = rt2x00dev->hw;
762 struct ieee80211_hw_mode *hwmodes;
763 struct ieee80211_channel *channels;
764 struct ieee80211_rate *rates;
766 unsigned char tx_power;
768 hwmodes = kzalloc(sizeof(*hwmodes) * spec->num_modes, GFP_KERNEL);
772 channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
774 goto exit_free_modes;
776 rates = kzalloc(sizeof(*rates) * spec->num_rates, GFP_KERNEL);
778 goto exit_free_channels;
781 * Initialize Rate list.
783 rt2x00lib_rate(&rates[0], 10, DEV_RATEMASK_1MB,
784 0x00, IEEE80211_RATE_CCK);
785 rt2x00lib_rate(&rates[1], 20, DEV_RATEMASK_2MB,
786 0x01, IEEE80211_RATE_CCK_2);
787 rt2x00lib_rate(&rates[2], 55, DEV_RATEMASK_5_5MB,
788 0x02, IEEE80211_RATE_CCK_2);
789 rt2x00lib_rate(&rates[3], 110, DEV_RATEMASK_11MB,
790 0x03, IEEE80211_RATE_CCK_2);
792 if (spec->num_rates > 4) {
793 rt2x00lib_rate(&rates[4], 60, DEV_RATEMASK_6MB,
794 0x0b, IEEE80211_RATE_OFDM);
795 rt2x00lib_rate(&rates[5], 90, DEV_RATEMASK_9MB,
796 0x0f, IEEE80211_RATE_OFDM);
797 rt2x00lib_rate(&rates[6], 120, DEV_RATEMASK_12MB,
798 0x0a, IEEE80211_RATE_OFDM);
799 rt2x00lib_rate(&rates[7], 180, DEV_RATEMASK_18MB,
800 0x0e, IEEE80211_RATE_OFDM);
801 rt2x00lib_rate(&rates[8], 240, DEV_RATEMASK_24MB,
802 0x09, IEEE80211_RATE_OFDM);
803 rt2x00lib_rate(&rates[9], 360, DEV_RATEMASK_36MB,
804 0x0d, IEEE80211_RATE_OFDM);
805 rt2x00lib_rate(&rates[10], 480, DEV_RATEMASK_48MB,
806 0x08, IEEE80211_RATE_OFDM);
807 rt2x00lib_rate(&rates[11], 540, DEV_RATEMASK_54MB,
808 0x0c, IEEE80211_RATE_OFDM);
812 * Initialize Channel list.
814 for (i = 0; i < spec->num_channels; i++) {
815 if (spec->channels[i].channel <= 14)
816 tx_power = spec->tx_power_bg[i];
817 else if (spec->tx_power_a)
818 tx_power = spec->tx_power_a[i];
820 tx_power = spec->tx_power_default;
822 rt2x00lib_channel(&channels[i],
823 spec->channels[i].channel, tx_power, i);
827 * Intitialize 802.11b
831 if (spec->num_modes > HWMODE_B) {
832 hwmodes[HWMODE_B].mode = MODE_IEEE80211B;
833 hwmodes[HWMODE_B].num_channels = 14;
834 hwmodes[HWMODE_B].num_rates = 4;
835 hwmodes[HWMODE_B].channels = channels;
836 hwmodes[HWMODE_B].rates = rates;
840 * Intitialize 802.11g
844 if (spec->num_modes > HWMODE_G) {
845 hwmodes[HWMODE_G].mode = MODE_IEEE80211G;
846 hwmodes[HWMODE_G].num_channels = 14;
847 hwmodes[HWMODE_G].num_rates = spec->num_rates;
848 hwmodes[HWMODE_G].channels = channels;
849 hwmodes[HWMODE_G].rates = rates;
853 * Intitialize 802.11a
855 * Channels: OFDM, UNII, HiperLAN2.
857 if (spec->num_modes > HWMODE_A) {
858 hwmodes[HWMODE_A].mode = MODE_IEEE80211A;
859 hwmodes[HWMODE_A].num_channels = spec->num_channels - 14;
860 hwmodes[HWMODE_A].num_rates = spec->num_rates - 4;
861 hwmodes[HWMODE_A].channels = &channels[14];
862 hwmodes[HWMODE_A].rates = &rates[4];
865 if (spec->num_modes > HWMODE_G &&
866 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_G]))
867 goto exit_free_rates;
869 if (spec->num_modes > HWMODE_B &&
870 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_B]))
871 goto exit_free_rates;
873 if (spec->num_modes > HWMODE_A &&
874 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_A]))
875 goto exit_free_rates;
877 rt2x00dev->hwmodes = hwmodes;
891 ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
895 static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
897 if (test_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags))
898 ieee80211_unregister_hw(rt2x00dev->hw);
900 if (likely(rt2x00dev->hwmodes)) {
901 kfree(rt2x00dev->hwmodes->channels);
902 kfree(rt2x00dev->hwmodes->rates);
903 kfree(rt2x00dev->hwmodes);
904 rt2x00dev->hwmodes = NULL;
908 static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
910 struct hw_mode_spec *spec = &rt2x00dev->spec;
914 * Initialize HW modes.
916 status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
923 status = ieee80211_register_hw(rt2x00dev->hw);
925 rt2x00lib_remove_hw(rt2x00dev);
929 __set_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags);
935 * Initialization/uninitialization handlers.
937 static int rt2x00lib_alloc_entries(struct data_ring *ring,
938 const u16 max_entries, const u16 data_size,
941 struct data_entry *entry;
944 ring->stats.limit = max_entries;
945 ring->data_size = data_size;
946 ring->desc_size = desc_size;
949 * Allocate all ring entries.
951 entry = kzalloc(ring->stats.limit * sizeof(*entry), GFP_KERNEL);
955 for (i = 0; i < ring->stats.limit; i++) {
957 entry[i].ring = ring;
966 static int rt2x00lib_alloc_ring_entries(struct rt2x00_dev *rt2x00dev)
968 struct data_ring *ring;
971 * Allocate the RX ring.
973 if (rt2x00lib_alloc_entries(rt2x00dev->rx, RX_ENTRIES, DATA_FRAME_SIZE,
974 rt2x00dev->ops->rxd_size))
978 * First allocate the TX rings.
980 txring_for_each(rt2x00dev, ring) {
981 if (rt2x00lib_alloc_entries(ring, TX_ENTRIES, DATA_FRAME_SIZE,
982 rt2x00dev->ops->txd_size))
986 if (!test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
990 * Allocate the BEACON ring.
992 if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[0], BEACON_ENTRIES,
993 MGMT_FRAME_SIZE, rt2x00dev->ops->txd_size))
997 * Allocate the Atim ring.
999 if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[1], ATIM_ENTRIES,
1000 DATA_FRAME_SIZE, rt2x00dev->ops->txd_size))
1006 static void rt2x00lib_free_ring_entries(struct rt2x00_dev *rt2x00dev)
1008 struct data_ring *ring;
1010 ring_for_each(rt2x00dev, ring) {
1016 void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
1018 if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
1022 * Unregister rfkill.
1024 rt2x00rfkill_unregister(rt2x00dev);
1027 * Allow the HW to uninitialize.
1029 rt2x00dev->ops->lib->uninitialize(rt2x00dev);
1032 * Free allocated ring entries.
1034 rt2x00lib_free_ring_entries(rt2x00dev);
1037 int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
1041 if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
1045 * Allocate all ring entries.
1047 status = rt2x00lib_alloc_ring_entries(rt2x00dev);
1049 ERROR(rt2x00dev, "Ring entries allocation failed.\n");
1054 * Initialize the device.
1056 status = rt2x00dev->ops->lib->initialize(rt2x00dev);
1060 __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
1063 * Register the rfkill handler.
1065 status = rt2x00rfkill_register(rt2x00dev);
1067 goto exit_unitialize;
1072 rt2x00lib_uninitialize(rt2x00dev);
1075 rt2x00lib_free_ring_entries(rt2x00dev);
1081 * driver allocation handlers.
1083 static int rt2x00lib_alloc_rings(struct rt2x00_dev *rt2x00dev)
1085 struct data_ring *ring;
1088 * We need the following rings:
1091 * Beacon: 1 (if required)
1092 * Atim: 1 (if required)
1094 rt2x00dev->data_rings = 1 + rt2x00dev->hw->queues +
1095 (2 * test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags));
1097 ring = kzalloc(rt2x00dev->data_rings * sizeof(*ring), GFP_KERNEL);
1099 ERROR(rt2x00dev, "Ring allocation failed.\n");
1104 * Initialize pointers
1106 rt2x00dev->rx = ring;
1107 rt2x00dev->tx = &rt2x00dev->rx[1];
1108 if (test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
1109 rt2x00dev->bcn = &rt2x00dev->tx[rt2x00dev->hw->queues];
1112 * Initialize ring parameters.
1114 * cw_max: 2^10 = 1024.
1116 ring_for_each(rt2x00dev, ring) {
1117 ring->rt2x00dev = rt2x00dev;
1118 ring->tx_params.aifs = 2;
1119 ring->tx_params.cw_min = 5;
1120 ring->tx_params.cw_max = 10;
1126 static void rt2x00lib_free_rings(struct rt2x00_dev *rt2x00dev)
1128 kfree(rt2x00dev->rx);
1129 rt2x00dev->rx = NULL;
1130 rt2x00dev->tx = NULL;
1131 rt2x00dev->bcn = NULL;
1134 int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
1136 int retval = -ENOMEM;
1139 * Let the driver probe the device to detect the capabilities.
1141 retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
1143 ERROR(rt2x00dev, "Failed to allocate device.\n");
1148 * Initialize configuration work.
1150 INIT_WORK(&rt2x00dev->beacon_work, rt2x00lib_beacondone_scheduled);
1151 INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
1152 INIT_WORK(&rt2x00dev->config_work, rt2x00lib_configuration_scheduled);
1153 INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
1156 * Reset current working type.
1158 rt2x00dev->interface.type = INVALID_INTERFACE;
1161 * Allocate ring array.
1163 retval = rt2x00lib_alloc_rings(rt2x00dev);
1168 * Initialize ieee80211 structure.
1170 retval = rt2x00lib_probe_hw(rt2x00dev);
1172 ERROR(rt2x00dev, "Failed to initialize hw.\n");
1179 retval = rt2x00rfkill_allocate(rt2x00dev);
1184 * Open the debugfs entry.
1186 rt2x00debug_register(rt2x00dev);
1188 __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1193 rt2x00lib_remove_dev(rt2x00dev);
1197 EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
1199 void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
1201 __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1206 rt2x00lib_disable_radio(rt2x00dev);
1209 * Uninitialize device.
1211 rt2x00lib_uninitialize(rt2x00dev);
1214 * Close debugfs entry.
1216 rt2x00debug_deregister(rt2x00dev);
1221 rt2x00rfkill_free(rt2x00dev);
1224 * Free ieee80211_hw memory.
1226 rt2x00lib_remove_hw(rt2x00dev);
1229 * Free firmware image.
1231 rt2x00lib_free_firmware(rt2x00dev);
1234 * Free ring structures.
1236 rt2x00lib_free_rings(rt2x00dev);
1238 EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
1241 * Device state handlers
1244 int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
1248 NOTICE(rt2x00dev, "Going to sleep.\n");
1249 __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1252 * Only continue if mac80211 has open interfaces.
1254 if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1256 __set_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags);
1259 * Disable radio and unitialize all items
1260 * that must be recreated on resume.
1262 rt2x00mac_stop(rt2x00dev->hw);
1263 rt2x00lib_uninitialize(rt2x00dev);
1264 rt2x00debug_deregister(rt2x00dev);
1268 * Set device mode to sleep for power management.
1270 retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
1276 EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1278 int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1280 struct interface *intf = &rt2x00dev->interface;
1283 NOTICE(rt2x00dev, "Waking up.\n");
1284 __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1287 * Open the debugfs entry.
1289 rt2x00debug_register(rt2x00dev);
1292 * Only continue if mac80211 had open interfaces.
1294 if (!__test_and_clear_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags))
1298 * Reinitialize device and all active interfaces.
1300 retval = rt2x00mac_start(rt2x00dev->hw);
1305 * Reconfigure device.
1307 rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1);
1308 if (!rt2x00dev->hw->conf.radio_enabled)
1309 rt2x00lib_disable_radio(rt2x00dev);
1311 rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
1312 rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
1313 rt2x00lib_config_type(rt2x00dev, intf->type);
1316 * It is possible that during that mac80211 has attempted
1317 * to send frames while we were suspending or resuming.
1318 * In that case we have disabled the TX queue and should
1319 * now enable it again
1321 ieee80211_start_queues(rt2x00dev->hw);
1324 * When in Master or Ad-hoc mode,
1325 * restart Beacon transmitting by faking a beacondone event.
1327 if (intf->type == IEEE80211_IF_TYPE_AP ||
1328 intf->type == IEEE80211_IF_TYPE_IBSS)
1329 rt2x00lib_beacondone(rt2x00dev);
1334 rt2x00lib_disable_radio(rt2x00dev);
1335 rt2x00lib_uninitialize(rt2x00dev);
1336 rt2x00debug_deregister(rt2x00dev);
1340 EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1341 #endif /* CONFIG_PM */
1344 * rt2x00lib module information.
1346 MODULE_AUTHOR(DRV_PROJECT);
1347 MODULE_VERSION(DRV_VERSION);
1348 MODULE_DESCRIPTION("rt2x00 library");
1349 MODULE_LICENSE("GPL");