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.
26 #include <linux/kernel.h>
27 #include <linux/module.h>
30 #include "rt2x00lib.h"
35 struct data_ring *rt2x00lib_get_ring(struct rt2x00_dev *rt2x00dev,
36 const unsigned int queue)
38 int beacon = test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags);
41 * Check if we are requesting a reqular TX ring,
42 * or if we are requesting a Beacon or Atim ring.
43 * For Atim rings, we should check if it is supported.
45 if (queue < rt2x00dev->hw->queues && rt2x00dev->tx)
46 return &rt2x00dev->tx[queue];
48 if (!rt2x00dev->bcn || !beacon)
51 if (queue == IEEE80211_TX_QUEUE_BEACON)
52 return &rt2x00dev->bcn[0];
53 else if (queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
54 return &rt2x00dev->bcn[1];
58 EXPORT_SYMBOL_GPL(rt2x00lib_get_ring);
61 * Link tuning handlers
63 static void rt2x00lib_start_link_tuner(struct rt2x00_dev *rt2x00dev)
65 rt2x00dev->link.count = 0;
66 rt2x00dev->link.vgc_level = 0;
68 memset(&rt2x00dev->link.qual, 0, sizeof(rt2x00dev->link.qual));
71 * The RX and TX percentage should start at 50%
72 * this will assure we will get at least get some
73 * decent value when the link tuner starts.
74 * The value will be dropped and overwritten with
75 * the correct (measured )value anyway during the
76 * first run of the link tuner.
78 rt2x00dev->link.qual.rx_percentage = 50;
79 rt2x00dev->link.qual.tx_percentage = 50;
82 * Reset the link tuner.
84 rt2x00dev->ops->lib->reset_tuner(rt2x00dev);
86 queue_delayed_work(rt2x00dev->hw->workqueue,
87 &rt2x00dev->link.work, LINK_TUNE_INTERVAL);
90 static void rt2x00lib_stop_link_tuner(struct rt2x00_dev *rt2x00dev)
92 cancel_delayed_work_sync(&rt2x00dev->link.work);
95 void rt2x00lib_reset_link_tuner(struct rt2x00_dev *rt2x00dev)
97 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
100 rt2x00lib_stop_link_tuner(rt2x00dev);
101 rt2x00lib_start_link_tuner(rt2x00dev);
105 * Radio control handlers.
107 int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
112 * Don't enable the radio twice.
113 * And check if the hardware button has been disabled.
115 if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
116 test_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags))
122 status = rt2x00dev->ops->lib->set_device_state(rt2x00dev,
127 __set_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags);
132 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
135 * Start the TX queues.
137 ieee80211_start_queues(rt2x00dev->hw);
142 void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
144 if (!__test_and_clear_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
148 * Stop all scheduled work.
150 if (work_pending(&rt2x00dev->beacon_work))
151 cancel_work_sync(&rt2x00dev->beacon_work);
152 if (work_pending(&rt2x00dev->filter_work))
153 cancel_work_sync(&rt2x00dev->filter_work);
154 if (work_pending(&rt2x00dev->config_work))
155 cancel_work_sync(&rt2x00dev->config_work);
158 * Stop the TX queues.
160 ieee80211_stop_queues(rt2x00dev->hw);
165 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
170 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
173 void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, enum dev_state state)
176 * When we are disabling the RX, we should also stop the link tuner.
178 if (state == STATE_RADIO_RX_OFF)
179 rt2x00lib_stop_link_tuner(rt2x00dev);
181 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
184 * When we are enabling the RX, we should also start the link tuner.
186 if (state == STATE_RADIO_RX_ON &&
187 is_interface_present(&rt2x00dev->interface))
188 rt2x00lib_start_link_tuner(rt2x00dev);
191 static void rt2x00lib_evaluate_antenna_sample(struct rt2x00_dev *rt2x00dev)
193 enum antenna rx = rt2x00dev->link.ant.active.rx;
194 enum antenna tx = rt2x00dev->link.ant.active.tx;
196 rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_A);
198 rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_B);
201 * We are done sampling. Now we should evaluate the results.
203 rt2x00dev->link.ant.flags &= ~ANTENNA_MODE_SAMPLE;
206 * During the last period we have sampled the RSSI
207 * from both antenna's. It now is time to determine
208 * which antenna demonstrated the best performance.
209 * When we are already on the antenna with the best
210 * performance, then there really is nothing for us
213 if (sample_a == sample_b)
216 if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY) {
217 if (sample_a > sample_b && rx == ANTENNA_B)
219 else if (rx == ANTENNA_A)
223 if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY) {
224 if (sample_a > sample_b && tx == ANTENNA_B)
226 else if (tx == ANTENNA_A)
230 rt2x00lib_config_antenna(rt2x00dev, rx, tx);
233 static void rt2x00lib_evaluate_antenna_eval(struct rt2x00_dev *rt2x00dev)
235 enum antenna rx = rt2x00dev->link.ant.active.rx;
236 enum antenna tx = rt2x00dev->link.ant.active.tx;
237 int rssi_curr = rt2x00_get_link_ant_rssi(&rt2x00dev->link);
238 int rssi_old = rt2x00_update_ant_rssi(&rt2x00dev->link, rssi_curr);
241 * Legacy driver indicates that we should swap antenna's
242 * when the difference in RSSI is greater that 5. This
243 * also should be done when the RSSI was actually better
244 * then the previous sample.
245 * When the difference exceeds the threshold we should
246 * sample the rssi from the other antenna to make a valid
247 * comparison between the 2 antennas.
249 if ((rssi_curr - rssi_old) > -5 || (rssi_curr - rssi_old) < 5)
252 rt2x00dev->link.ant.flags |= ANTENNA_MODE_SAMPLE;
254 if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY)
255 rx = (rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
257 if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)
258 tx = (tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
260 rt2x00lib_config_antenna(rt2x00dev, rx, tx);
263 static void rt2x00lib_evaluate_antenna(struct rt2x00_dev *rt2x00dev)
266 * Determine if software diversity is enabled for
267 * either the TX or RX antenna (or both).
268 * Always perform this check since within the link
269 * tuner interval the configuration might have changed.
271 rt2x00dev->link.ant.flags &= ~ANTENNA_RX_DIVERSITY;
272 rt2x00dev->link.ant.flags &= ~ANTENNA_TX_DIVERSITY;
274 if (rt2x00dev->hw->conf.antenna_sel_rx == 0 &&
275 rt2x00dev->default_ant.rx != ANTENNA_SW_DIVERSITY)
276 rt2x00dev->link.ant.flags |= ANTENNA_RX_DIVERSITY;
277 if (rt2x00dev->hw->conf.antenna_sel_tx == 0 &&
278 rt2x00dev->default_ant.tx != ANTENNA_SW_DIVERSITY)
279 rt2x00dev->link.ant.flags |= ANTENNA_TX_DIVERSITY;
281 if (!(rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY) &&
282 !(rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)) {
283 rt2x00dev->link.ant.flags &= ~ANTENNA_MODE_SAMPLE;
288 * If we have only sampled the data over the last period
289 * we should now harvest the data. Otherwise just evaluate
290 * the data. The latter should only be performed once
293 if (rt2x00dev->link.ant.flags & ANTENNA_MODE_SAMPLE)
294 rt2x00lib_evaluate_antenna_sample(rt2x00dev);
295 else if (rt2x00dev->link.count & 1)
296 rt2x00lib_evaluate_antenna_eval(rt2x00dev);
299 static void rt2x00lib_update_link_stats(struct link *link, int rssi)
306 if (link->qual.avg_rssi)
307 avg_rssi = MOVING_AVERAGE(link->qual.avg_rssi, rssi, 8);
308 link->qual.avg_rssi = avg_rssi;
311 * Update antenna RSSI
313 if (link->ant.rssi_ant)
314 rssi = MOVING_AVERAGE(link->ant.rssi_ant, rssi, 8);
315 link->ant.rssi_ant = rssi;
318 static void rt2x00lib_precalculate_link_signal(struct link_qual *qual)
320 if (qual->rx_failed || qual->rx_success)
321 qual->rx_percentage =
322 (qual->rx_success * 100) /
323 (qual->rx_failed + qual->rx_success);
325 qual->rx_percentage = 50;
327 if (qual->tx_failed || qual->tx_success)
328 qual->tx_percentage =
329 (qual->tx_success * 100) /
330 (qual->tx_failed + qual->tx_success);
332 qual->tx_percentage = 50;
334 qual->rx_success = 0;
336 qual->tx_success = 0;
340 static int rt2x00lib_calculate_link_signal(struct rt2x00_dev *rt2x00dev,
343 int rssi_percentage = 0;
347 * We need a positive value for the RSSI.
350 rssi += rt2x00dev->rssi_offset;
353 * Calculate the different percentages,
354 * which will be used for the signal.
356 if (rt2x00dev->rssi_offset)
357 rssi_percentage = (rssi * 100) / rt2x00dev->rssi_offset;
360 * Add the individual percentages and use the WEIGHT
361 * defines to calculate the current link signal.
363 signal = ((WEIGHT_RSSI * rssi_percentage) +
364 (WEIGHT_TX * rt2x00dev->link.qual.tx_percentage) +
365 (WEIGHT_RX * rt2x00dev->link.qual.rx_percentage)) / 100;
367 return (signal > 100) ? 100 : signal;
370 static void rt2x00lib_link_tuner(struct work_struct *work)
372 struct rt2x00_dev *rt2x00dev =
373 container_of(work, struct rt2x00_dev, link.work.work);
376 * When the radio is shutting down we should
377 * immediately cease all link tuning.
379 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
385 rt2x00dev->ops->lib->link_stats(rt2x00dev, &rt2x00dev->link.qual);
386 rt2x00dev->low_level_stats.dot11FCSErrorCount +=
387 rt2x00dev->link.qual.rx_failed;
390 * Only perform the link tuning when Link tuning
391 * has been enabled (This could have been disabled from the EEPROM).
393 if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
394 rt2x00dev->ops->lib->link_tuner(rt2x00dev);
397 * Evaluate antenna setup.
399 rt2x00lib_evaluate_antenna(rt2x00dev);
402 * Precalculate a portion of the link signal which is
403 * in based on the tx/rx success/failure counters.
405 rt2x00lib_precalculate_link_signal(&rt2x00dev->link.qual);
408 * Increase tuner counter, and reschedule the next link tuner run.
410 rt2x00dev->link.count++;
411 queue_delayed_work(rt2x00dev->hw->workqueue, &rt2x00dev->link.work,
415 static void rt2x00lib_packetfilter_scheduled(struct work_struct *work)
417 struct rt2x00_dev *rt2x00dev =
418 container_of(work, struct rt2x00_dev, filter_work);
419 unsigned int filter = rt2x00dev->interface.filter;
422 * Since we had stored the filter inside interface.filter,
423 * we should now clear that field. Otherwise the driver will
424 * assume nothing has changed (*total_flags will be compared
425 * to interface.filter to determine if any action is required).
427 rt2x00dev->interface.filter = 0;
429 rt2x00dev->ops->hw->configure_filter(rt2x00dev->hw,
430 filter, &filter, 0, NULL);
433 static void rt2x00lib_configuration_scheduled(struct work_struct *work)
435 struct rt2x00_dev *rt2x00dev =
436 container_of(work, struct rt2x00_dev, config_work);
437 int preamble = !test_bit(CONFIG_SHORT_PREAMBLE, &rt2x00dev->flags);
439 rt2x00mac_erp_ie_changed(rt2x00dev->hw,
440 IEEE80211_ERP_CHANGE_PREAMBLE, 0, preamble);
444 * Interrupt context handlers.
446 static void rt2x00lib_beacondone_scheduled(struct work_struct *work)
448 struct rt2x00_dev *rt2x00dev =
449 container_of(work, struct rt2x00_dev, beacon_work);
450 struct data_ring *ring =
451 rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_BEACON);
452 struct data_entry *entry = rt2x00_get_data_entry(ring);
455 skb = ieee80211_beacon_get(rt2x00dev->hw,
456 rt2x00dev->interface.id,
457 &entry->tx_status.control);
461 rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw, skb,
462 &entry->tx_status.control);
467 void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
469 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
472 queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->beacon_work);
474 EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
476 void rt2x00lib_txdone(struct data_entry *entry,
477 const int status, const int retry)
479 struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
480 struct ieee80211_tx_status *tx_status = &entry->tx_status;
481 struct ieee80211_low_level_stats *stats = &rt2x00dev->low_level_stats;
482 int success = !!(status == TX_SUCCESS || status == TX_SUCCESS_RETRY);
483 int fail = !!(status == TX_FAIL_RETRY || status == TX_FAIL_INVALID ||
484 status == TX_FAIL_OTHER);
487 * Update TX statistics.
489 tx_status->flags = 0;
490 tx_status->ack_signal = 0;
491 tx_status->excessive_retries = (status == TX_FAIL_RETRY);
492 tx_status->retry_count = retry;
493 rt2x00dev->link.qual.tx_success += success;
494 rt2x00dev->link.qual.tx_failed += retry + fail;
496 if (!(tx_status->control.flags & IEEE80211_TXCTL_NO_ACK)) {
498 tx_status->flags |= IEEE80211_TX_STATUS_ACK;
500 stats->dot11ACKFailureCount++;
503 tx_status->queue_length = entry->ring->stats.limit;
504 tx_status->queue_number = tx_status->control.queue;
506 if (tx_status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) {
508 stats->dot11RTSSuccessCount++;
510 stats->dot11RTSFailureCount++;
514 * Send the tx_status to mac80211,
515 * that method also cleans up the skb structure.
517 ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb, tx_status);
520 EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
522 void rt2x00lib_rxdone(struct data_entry *entry, struct sk_buff *skb,
523 struct rxdata_entry_desc *desc)
525 struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
526 struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
527 struct ieee80211_hw_mode *mode;
528 struct ieee80211_rate *rate;
533 * Update RX statistics.
535 mode = &rt2x00dev->hwmodes[rt2x00dev->curr_hwmode];
536 for (i = 0; i < mode->num_rates; i++) {
537 rate = &mode->rates[i];
540 * When frame was received with an OFDM bitrate,
541 * the signal is the PLCP value. If it was received with
542 * a CCK bitrate the signal is the rate in 0.5kbit/s.
545 val = DEVICE_GET_RATE_FIELD(rate->val, RATE);
547 val = DEVICE_GET_RATE_FIELD(rate->val, PLCP);
549 if (val == desc->signal) {
555 rt2x00lib_update_link_stats(&rt2x00dev->link, desc->rssi);
556 rt2x00dev->link.qual.rx_success++;
558 rx_status->rate = val;
560 rt2x00lib_calculate_link_signal(rt2x00dev, desc->rssi);
561 rx_status->ssi = desc->rssi;
562 rx_status->flag = desc->flags;
563 rx_status->antenna = rt2x00dev->link.ant.active.rx;
566 * Send frame to mac80211
568 ieee80211_rx_irqsafe(rt2x00dev->hw, skb, rx_status);
570 EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
573 * TX descriptor initializer
575 void rt2x00lib_write_tx_desc(struct rt2x00_dev *rt2x00dev,
577 struct ieee80211_tx_control *control)
579 struct txdata_entry_desc desc;
580 struct skb_desc *skbdesc = get_skb_desc(skb);
581 struct ieee80211_hdr *ieee80211hdr = skbdesc->data;
582 __le32 *txd = skbdesc->desc;
591 memset(&desc, 0, sizeof(desc));
593 desc.cw_min = skbdesc->ring->tx_params.cw_min;
594 desc.cw_max = skbdesc->ring->tx_params.cw_max;
595 desc.aifs = skbdesc->ring->tx_params.aifs;
600 if (control->queue < rt2x00dev->hw->queues)
601 desc.queue = control->queue;
602 else if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
603 control->queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
604 desc.queue = QUEUE_MGMT;
606 desc.queue = QUEUE_OTHER;
609 * Read required fields from ieee80211 header.
611 frame_control = le16_to_cpu(ieee80211hdr->frame_control);
612 seq_ctrl = le16_to_cpu(ieee80211hdr->seq_ctrl);
614 tx_rate = control->tx_rate;
617 * Check whether this frame is to be acked
619 if (!(control->flags & IEEE80211_TXCTL_NO_ACK))
620 __set_bit(ENTRY_TXD_ACK, &desc.flags);
623 * Check if this is a RTS/CTS frame
625 if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
626 __set_bit(ENTRY_TXD_BURST, &desc.flags);
627 if (is_rts_frame(frame_control)) {
628 __set_bit(ENTRY_TXD_RTS_FRAME, &desc.flags);
629 __set_bit(ENTRY_TXD_ACK, &desc.flags);
631 __clear_bit(ENTRY_TXD_ACK, &desc.flags);
632 if (control->rts_cts_rate)
633 tx_rate = control->rts_cts_rate;
639 if (DEVICE_GET_RATE_FIELD(tx_rate, RATEMASK) & DEV_OFDM_RATEMASK)
640 __set_bit(ENTRY_TXD_OFDM_RATE, &desc.flags);
643 * Check if more fragments are pending
645 if (ieee80211_get_morefrag(ieee80211hdr)) {
646 __set_bit(ENTRY_TXD_BURST, &desc.flags);
647 __set_bit(ENTRY_TXD_MORE_FRAG, &desc.flags);
651 * Beacons and probe responses require the tsf timestamp
652 * to be inserted into the frame.
654 if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
655 is_probe_resp(frame_control))
656 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &desc.flags);
659 * Determine with what IFS priority this frame should be send.
660 * Set ifs to IFS_SIFS when the this is not the first fragment,
661 * or this fragment came after RTS/CTS.
663 if ((seq_ctrl & IEEE80211_SCTL_FRAG) > 0 ||
664 test_bit(ENTRY_TXD_RTS_FRAME, &desc.flags))
667 desc.ifs = IFS_BACKOFF;
671 * Length calculation depends on OFDM/CCK rate.
673 desc.signal = DEVICE_GET_RATE_FIELD(tx_rate, PLCP);
676 length = skbdesc->data_len + FCS_LEN;
677 if (test_bit(ENTRY_TXD_OFDM_RATE, &desc.flags)) {
678 desc.length_high = (length >> 6) & 0x3f;
679 desc.length_low = length & 0x3f;
681 bitrate = DEVICE_GET_RATE_FIELD(tx_rate, RATE);
684 * Convert length to microseconds.
686 residual = get_duration_res(length, bitrate);
687 duration = get_duration(length, bitrate);
693 * Check if we need to set the Length Extension
695 if (bitrate == 110 && residual <= 30)
696 desc.service |= 0x80;
699 desc.length_high = (duration >> 8) & 0xff;
700 desc.length_low = duration & 0xff;
703 * When preamble is enabled we should set the
704 * preamble bit for the signal.
706 if (DEVICE_GET_RATE_FIELD(tx_rate, PREAMBLE))
710 rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, txd, &desc, ieee80211hdr,
711 skbdesc->data_len, control);
716 skbdesc->entry->skb = skb;
717 memcpy(&skbdesc->entry->tx_status.control, control, sizeof(*control));
719 EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc);
722 * Driver initialization handlers.
724 static void rt2x00lib_channel(struct ieee80211_channel *entry,
725 const int channel, const int tx_power,
728 entry->chan = channel;
730 entry->freq = 2407 + (5 * channel);
732 entry->freq = 5000 + (5 * channel);
735 IEEE80211_CHAN_W_IBSS |
736 IEEE80211_CHAN_W_ACTIVE_SCAN |
737 IEEE80211_CHAN_W_SCAN;
738 entry->power_level = tx_power;
739 entry->antenna_max = 0xff;
742 static void rt2x00lib_rate(struct ieee80211_rate *entry,
743 const int rate, const int mask,
744 const int plcp, const int flags)
748 DEVICE_SET_RATE_FIELD(rate, RATE) |
749 DEVICE_SET_RATE_FIELD(mask, RATEMASK) |
750 DEVICE_SET_RATE_FIELD(plcp, PLCP);
751 entry->flags = flags;
752 entry->val2 = entry->val;
753 if (entry->flags & IEEE80211_RATE_PREAMBLE2)
754 entry->val2 |= DEVICE_SET_RATE_FIELD(1, PREAMBLE);
755 entry->min_rssi_ack = 0;
756 entry->min_rssi_ack_delta = 0;
759 static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
760 struct hw_mode_spec *spec)
762 struct ieee80211_hw *hw = rt2x00dev->hw;
763 struct ieee80211_hw_mode *hwmodes;
764 struct ieee80211_channel *channels;
765 struct ieee80211_rate *rates;
767 unsigned char tx_power;
769 hwmodes = kzalloc(sizeof(*hwmodes) * spec->num_modes, GFP_KERNEL);
773 channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
775 goto exit_free_modes;
777 rates = kzalloc(sizeof(*rates) * spec->num_rates, GFP_KERNEL);
779 goto exit_free_channels;
782 * Initialize Rate list.
784 rt2x00lib_rate(&rates[0], 10, DEV_RATEMASK_1MB,
785 0x00, IEEE80211_RATE_CCK);
786 rt2x00lib_rate(&rates[1], 20, DEV_RATEMASK_2MB,
787 0x01, IEEE80211_RATE_CCK_2);
788 rt2x00lib_rate(&rates[2], 55, DEV_RATEMASK_5_5MB,
789 0x02, IEEE80211_RATE_CCK_2);
790 rt2x00lib_rate(&rates[3], 110, DEV_RATEMASK_11MB,
791 0x03, IEEE80211_RATE_CCK_2);
793 if (spec->num_rates > 4) {
794 rt2x00lib_rate(&rates[4], 60, DEV_RATEMASK_6MB,
795 0x0b, IEEE80211_RATE_OFDM);
796 rt2x00lib_rate(&rates[5], 90, DEV_RATEMASK_9MB,
797 0x0f, IEEE80211_RATE_OFDM);
798 rt2x00lib_rate(&rates[6], 120, DEV_RATEMASK_12MB,
799 0x0a, IEEE80211_RATE_OFDM);
800 rt2x00lib_rate(&rates[7], 180, DEV_RATEMASK_18MB,
801 0x0e, IEEE80211_RATE_OFDM);
802 rt2x00lib_rate(&rates[8], 240, DEV_RATEMASK_24MB,
803 0x09, IEEE80211_RATE_OFDM);
804 rt2x00lib_rate(&rates[9], 360, DEV_RATEMASK_36MB,
805 0x0d, IEEE80211_RATE_OFDM);
806 rt2x00lib_rate(&rates[10], 480, DEV_RATEMASK_48MB,
807 0x08, IEEE80211_RATE_OFDM);
808 rt2x00lib_rate(&rates[11], 540, DEV_RATEMASK_54MB,
809 0x0c, IEEE80211_RATE_OFDM);
813 * Initialize Channel list.
815 for (i = 0; i < spec->num_channels; i++) {
816 if (spec->channels[i].channel <= 14)
817 tx_power = spec->tx_power_bg[i];
818 else if (spec->tx_power_a)
819 tx_power = spec->tx_power_a[i];
821 tx_power = spec->tx_power_default;
823 rt2x00lib_channel(&channels[i],
824 spec->channels[i].channel, tx_power, i);
828 * Intitialize 802.11b
832 if (spec->num_modes > HWMODE_B) {
833 hwmodes[HWMODE_B].mode = MODE_IEEE80211B;
834 hwmodes[HWMODE_B].num_channels = 14;
835 hwmodes[HWMODE_B].num_rates = 4;
836 hwmodes[HWMODE_B].channels = channels;
837 hwmodes[HWMODE_B].rates = rates;
841 * Intitialize 802.11g
845 if (spec->num_modes > HWMODE_G) {
846 hwmodes[HWMODE_G].mode = MODE_IEEE80211G;
847 hwmodes[HWMODE_G].num_channels = 14;
848 hwmodes[HWMODE_G].num_rates = spec->num_rates;
849 hwmodes[HWMODE_G].channels = channels;
850 hwmodes[HWMODE_G].rates = rates;
854 * Intitialize 802.11a
856 * Channels: OFDM, UNII, HiperLAN2.
858 if (spec->num_modes > HWMODE_A) {
859 hwmodes[HWMODE_A].mode = MODE_IEEE80211A;
860 hwmodes[HWMODE_A].num_channels = spec->num_channels - 14;
861 hwmodes[HWMODE_A].num_rates = spec->num_rates - 4;
862 hwmodes[HWMODE_A].channels = &channels[14];
863 hwmodes[HWMODE_A].rates = &rates[4];
866 if (spec->num_modes > HWMODE_G &&
867 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_G]))
868 goto exit_free_rates;
870 if (spec->num_modes > HWMODE_B &&
871 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_B]))
872 goto exit_free_rates;
874 if (spec->num_modes > HWMODE_A &&
875 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_A]))
876 goto exit_free_rates;
878 rt2x00dev->hwmodes = hwmodes;
892 ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
896 static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
898 if (test_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags))
899 ieee80211_unregister_hw(rt2x00dev->hw);
901 if (likely(rt2x00dev->hwmodes)) {
902 kfree(rt2x00dev->hwmodes->channels);
903 kfree(rt2x00dev->hwmodes->rates);
904 kfree(rt2x00dev->hwmodes);
905 rt2x00dev->hwmodes = NULL;
909 static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
911 struct hw_mode_spec *spec = &rt2x00dev->spec;
915 * Initialize HW modes.
917 status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
924 status = ieee80211_register_hw(rt2x00dev->hw);
926 rt2x00lib_remove_hw(rt2x00dev);
930 __set_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags);
936 * Initialization/uninitialization handlers.
938 static int rt2x00lib_alloc_entries(struct data_ring *ring,
939 const u16 max_entries, const u16 data_size,
942 struct data_entry *entry;
945 ring->stats.limit = max_entries;
946 ring->data_size = data_size;
947 ring->desc_size = desc_size;
950 * Allocate all ring entries.
952 entry = kzalloc(ring->stats.limit * sizeof(*entry), GFP_KERNEL);
956 for (i = 0; i < ring->stats.limit; i++) {
958 entry[i].ring = ring;
967 static int rt2x00lib_alloc_ring_entries(struct rt2x00_dev *rt2x00dev)
969 struct data_ring *ring;
972 * Allocate the RX ring.
974 if (rt2x00lib_alloc_entries(rt2x00dev->rx, RX_ENTRIES, DATA_FRAME_SIZE,
975 rt2x00dev->ops->rxd_size))
979 * First allocate the TX rings.
981 txring_for_each(rt2x00dev, ring) {
982 if (rt2x00lib_alloc_entries(ring, TX_ENTRIES, DATA_FRAME_SIZE,
983 rt2x00dev->ops->txd_size))
987 if (!test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
991 * Allocate the BEACON ring.
993 if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[0], BEACON_ENTRIES,
994 MGMT_FRAME_SIZE, rt2x00dev->ops->txd_size))
998 * Allocate the Atim ring.
1000 if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[1], ATIM_ENTRIES,
1001 DATA_FRAME_SIZE, rt2x00dev->ops->txd_size))
1007 static void rt2x00lib_free_ring_entries(struct rt2x00_dev *rt2x00dev)
1009 struct data_ring *ring;
1011 ring_for_each(rt2x00dev, ring) {
1017 void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
1019 if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
1023 * Unregister rfkill.
1025 rt2x00rfkill_unregister(rt2x00dev);
1028 * Allow the HW to uninitialize.
1030 rt2x00dev->ops->lib->uninitialize(rt2x00dev);
1033 * Free allocated ring entries.
1035 rt2x00lib_free_ring_entries(rt2x00dev);
1038 int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
1042 if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
1046 * Allocate all ring entries.
1048 status = rt2x00lib_alloc_ring_entries(rt2x00dev);
1050 ERROR(rt2x00dev, "Ring entries allocation failed.\n");
1055 * Initialize the device.
1057 status = rt2x00dev->ops->lib->initialize(rt2x00dev);
1061 __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
1064 * Register the rfkill handler.
1066 status = rt2x00rfkill_register(rt2x00dev);
1068 goto exit_unitialize;
1073 rt2x00lib_uninitialize(rt2x00dev);
1076 rt2x00lib_free_ring_entries(rt2x00dev);
1082 * driver allocation handlers.
1084 static int rt2x00lib_alloc_rings(struct rt2x00_dev *rt2x00dev)
1086 struct data_ring *ring;
1089 * We need the following rings:
1092 * Beacon: 1 (if required)
1093 * Atim: 1 (if required)
1095 rt2x00dev->data_rings = 1 + rt2x00dev->hw->queues +
1096 (2 * test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags));
1098 ring = kzalloc(rt2x00dev->data_rings * sizeof(*ring), GFP_KERNEL);
1100 ERROR(rt2x00dev, "Ring allocation failed.\n");
1105 * Initialize pointers
1107 rt2x00dev->rx = ring;
1108 rt2x00dev->tx = &rt2x00dev->rx[1];
1109 if (test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
1110 rt2x00dev->bcn = &rt2x00dev->tx[rt2x00dev->hw->queues];
1113 * Initialize ring parameters.
1115 * cw_max: 2^10 = 1024.
1117 ring_for_each(rt2x00dev, ring) {
1118 ring->rt2x00dev = rt2x00dev;
1119 ring->tx_params.aifs = 2;
1120 ring->tx_params.cw_min = 5;
1121 ring->tx_params.cw_max = 10;
1127 static void rt2x00lib_free_rings(struct rt2x00_dev *rt2x00dev)
1129 kfree(rt2x00dev->rx);
1130 rt2x00dev->rx = NULL;
1131 rt2x00dev->tx = NULL;
1132 rt2x00dev->bcn = NULL;
1135 int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
1137 int retval = -ENOMEM;
1140 * Let the driver probe the device to detect the capabilities.
1142 retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
1144 ERROR(rt2x00dev, "Failed to allocate device.\n");
1149 * Initialize configuration work.
1151 INIT_WORK(&rt2x00dev->beacon_work, rt2x00lib_beacondone_scheduled);
1152 INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
1153 INIT_WORK(&rt2x00dev->config_work, rt2x00lib_configuration_scheduled);
1154 INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
1157 * Reset current working type.
1159 rt2x00dev->interface.type = INVALID_INTERFACE;
1162 * Allocate ring array.
1164 retval = rt2x00lib_alloc_rings(rt2x00dev);
1169 * Initialize ieee80211 structure.
1171 retval = rt2x00lib_probe_hw(rt2x00dev);
1173 ERROR(rt2x00dev, "Failed to initialize hw.\n");
1180 retval = rt2x00rfkill_allocate(rt2x00dev);
1185 * Open the debugfs entry.
1187 rt2x00debug_register(rt2x00dev);
1189 __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1194 rt2x00lib_remove_dev(rt2x00dev);
1198 EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
1200 void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
1202 __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1207 rt2x00lib_disable_radio(rt2x00dev);
1210 * Uninitialize device.
1212 rt2x00lib_uninitialize(rt2x00dev);
1215 * Close debugfs entry.
1217 rt2x00debug_deregister(rt2x00dev);
1222 rt2x00rfkill_free(rt2x00dev);
1225 * Free ieee80211_hw memory.
1227 rt2x00lib_remove_hw(rt2x00dev);
1230 * Free firmware image.
1232 rt2x00lib_free_firmware(rt2x00dev);
1235 * Free ring structures.
1237 rt2x00lib_free_rings(rt2x00dev);
1239 EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
1242 * Device state handlers
1245 int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
1249 NOTICE(rt2x00dev, "Going to sleep.\n");
1250 __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1253 * Only continue if mac80211 has open interfaces.
1255 if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1257 __set_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags);
1260 * Disable radio and unitialize all items
1261 * that must be recreated on resume.
1263 rt2x00mac_stop(rt2x00dev->hw);
1264 rt2x00lib_uninitialize(rt2x00dev);
1265 rt2x00debug_deregister(rt2x00dev);
1269 * Set device mode to sleep for power management.
1271 retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
1277 EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1279 int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1281 struct interface *intf = &rt2x00dev->interface;
1284 NOTICE(rt2x00dev, "Waking up.\n");
1285 __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1288 * Open the debugfs entry.
1290 rt2x00debug_register(rt2x00dev);
1293 * Only continue if mac80211 had open interfaces.
1295 if (!__test_and_clear_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags))
1299 * Reinitialize device and all active interfaces.
1301 retval = rt2x00mac_start(rt2x00dev->hw);
1306 * Reconfigure device.
1308 rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1);
1309 if (!rt2x00dev->hw->conf.radio_enabled)
1310 rt2x00lib_disable_radio(rt2x00dev);
1312 rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
1313 rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
1314 rt2x00lib_config_type(rt2x00dev, intf->type);
1317 * It is possible that during that mac80211 has attempted
1318 * to send frames while we were suspending or resuming.
1319 * In that case we have disabled the TX queue and should
1320 * now enable it again
1322 ieee80211_start_queues(rt2x00dev->hw);
1325 * When in Master or Ad-hoc mode,
1326 * restart Beacon transmitting by faking a beacondone event.
1328 if (intf->type == IEEE80211_IF_TYPE_AP ||
1329 intf->type == IEEE80211_IF_TYPE_IBSS)
1330 rt2x00lib_beacondone(rt2x00dev);
1335 rt2x00lib_disable_radio(rt2x00dev);
1336 rt2x00lib_uninitialize(rt2x00dev);
1337 rt2x00debug_deregister(rt2x00dev);
1341 EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1342 #endif /* CONFIG_PM */
1345 * rt2x00lib module information.
1347 MODULE_AUTHOR(DRV_PROJECT);
1348 MODULE_VERSION(DRV_VERSION);
1349 MODULE_DESCRIPTION("rt2x00 library");
1350 MODULE_LICENSE("GPL");