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"
31 #include "rt2x00dump.h"
36 struct data_ring *rt2x00lib_get_ring(struct rt2x00_dev *rt2x00dev,
37 const unsigned int queue)
39 int beacon = test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags);
42 * Check if we are requesting a reqular TX ring,
43 * or if we are requesting a Beacon or Atim ring.
44 * For Atim rings, we should check if it is supported.
46 if (queue < rt2x00dev->hw->queues && rt2x00dev->tx)
47 return &rt2x00dev->tx[queue];
49 if (!rt2x00dev->bcn || !beacon)
52 if (queue == IEEE80211_TX_QUEUE_BEACON)
53 return &rt2x00dev->bcn[0];
54 else if (queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
55 return &rt2x00dev->bcn[1];
59 EXPORT_SYMBOL_GPL(rt2x00lib_get_ring);
62 * Link tuning handlers
64 static void rt2x00lib_start_link_tuner(struct rt2x00_dev *rt2x00dev)
66 rt2x00dev->link.count = 0;
67 rt2x00dev->link.vgc_level = 0;
69 memset(&rt2x00dev->link.qual, 0, sizeof(rt2x00dev->link.qual));
72 * The RX and TX percentage should start at 50%
73 * this will assure we will get at least get some
74 * decent value when the link tuner starts.
75 * The value will be dropped and overwritten with
76 * the correct (measured )value anyway during the
77 * first run of the link tuner.
79 rt2x00dev->link.qual.rx_percentage = 50;
80 rt2x00dev->link.qual.tx_percentage = 50;
83 * Reset the link tuner.
85 rt2x00dev->ops->lib->reset_tuner(rt2x00dev);
87 queue_delayed_work(rt2x00dev->hw->workqueue,
88 &rt2x00dev->link.work, LINK_TUNE_INTERVAL);
91 static void rt2x00lib_stop_link_tuner(struct rt2x00_dev *rt2x00dev)
93 cancel_delayed_work_sync(&rt2x00dev->link.work);
96 void rt2x00lib_reset_link_tuner(struct rt2x00_dev *rt2x00dev)
98 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
101 rt2x00lib_stop_link_tuner(rt2x00dev);
102 rt2x00lib_start_link_tuner(rt2x00dev);
106 * Radio control handlers.
108 int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
113 * Don't enable the radio twice.
114 * And check if the hardware button has been disabled.
116 if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
117 test_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags))
123 status = rt2x00dev->ops->lib->set_device_state(rt2x00dev,
128 __set_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags);
133 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
136 * Start the TX queues.
138 ieee80211_start_queues(rt2x00dev->hw);
143 void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
145 if (!__test_and_clear_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
149 * Stop all scheduled work.
151 if (work_pending(&rt2x00dev->beacon_work))
152 cancel_work_sync(&rt2x00dev->beacon_work);
153 if (work_pending(&rt2x00dev->filter_work))
154 cancel_work_sync(&rt2x00dev->filter_work);
155 if (work_pending(&rt2x00dev->config_work))
156 cancel_work_sync(&rt2x00dev->config_work);
159 * Stop the TX queues.
161 ieee80211_stop_queues(rt2x00dev->hw);
166 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
171 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
174 void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, enum dev_state state)
177 * When we are disabling the RX, we should also stop the link tuner.
179 if (state == STATE_RADIO_RX_OFF)
180 rt2x00lib_stop_link_tuner(rt2x00dev);
182 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
185 * When we are enabling the RX, we should also start the link tuner.
187 if (state == STATE_RADIO_RX_ON &&
188 is_interface_present(&rt2x00dev->interface))
189 rt2x00lib_start_link_tuner(rt2x00dev);
192 static void rt2x00lib_evaluate_antenna_sample(struct rt2x00_dev *rt2x00dev)
194 enum antenna rx = rt2x00dev->link.ant.active.rx;
195 enum antenna tx = rt2x00dev->link.ant.active.tx;
197 rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_A);
199 rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_B);
202 * We are done sampling. Now we should evaluate the results.
204 rt2x00dev->link.ant.flags &= ~ANTENNA_MODE_SAMPLE;
207 * During the last period we have sampled the RSSI
208 * from both antenna's. It now is time to determine
209 * which antenna demonstrated the best performance.
210 * When we are already on the antenna with the best
211 * performance, then there really is nothing for us
214 if (sample_a == sample_b)
217 if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY) {
218 if (sample_a > sample_b && rx == ANTENNA_B)
220 else if (rx == ANTENNA_A)
224 if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY) {
225 if (sample_a > sample_b && tx == ANTENNA_B)
227 else if (tx == ANTENNA_A)
231 rt2x00lib_config_antenna(rt2x00dev, rx, tx);
234 static void rt2x00lib_evaluate_antenna_eval(struct rt2x00_dev *rt2x00dev)
236 enum antenna rx = rt2x00dev->link.ant.active.rx;
237 enum antenna tx = rt2x00dev->link.ant.active.tx;
238 int rssi_curr = rt2x00_get_link_ant_rssi(&rt2x00dev->link);
239 int rssi_old = rt2x00_update_ant_rssi(&rt2x00dev->link, rssi_curr);
242 * Legacy driver indicates that we should swap antenna's
243 * when the difference in RSSI is greater that 5. This
244 * also should be done when the RSSI was actually better
245 * then the previous sample.
246 * When the difference exceeds the threshold we should
247 * sample the rssi from the other antenna to make a valid
248 * comparison between the 2 antennas.
250 if ((rssi_curr - rssi_old) > -5 || (rssi_curr - rssi_old) < 5)
253 rt2x00dev->link.ant.flags |= ANTENNA_MODE_SAMPLE;
255 if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY)
256 rx = (rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
258 if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)
259 tx = (tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
261 rt2x00lib_config_antenna(rt2x00dev, rx, tx);
264 static void rt2x00lib_evaluate_antenna(struct rt2x00_dev *rt2x00dev)
267 * Determine if software diversity is enabled for
268 * either the TX or RX antenna (or both).
269 * Always perform this check since within the link
270 * tuner interval the configuration might have changed.
272 rt2x00dev->link.ant.flags &= ~ANTENNA_RX_DIVERSITY;
273 rt2x00dev->link.ant.flags &= ~ANTENNA_TX_DIVERSITY;
275 if (rt2x00dev->hw->conf.antenna_sel_rx == 0 &&
276 rt2x00dev->default_ant.rx != ANTENNA_SW_DIVERSITY)
277 rt2x00dev->link.ant.flags |= ANTENNA_RX_DIVERSITY;
278 if (rt2x00dev->hw->conf.antenna_sel_tx == 0 &&
279 rt2x00dev->default_ant.tx != ANTENNA_SW_DIVERSITY)
280 rt2x00dev->link.ant.flags |= ANTENNA_TX_DIVERSITY;
282 if (!(rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY) &&
283 !(rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)) {
284 rt2x00dev->link.ant.flags &= ~ANTENNA_MODE_SAMPLE;
289 * If we have only sampled the data over the last period
290 * we should now harvest the data. Otherwise just evaluate
291 * the data. The latter should only be performed once
294 if (rt2x00dev->link.ant.flags & ANTENNA_MODE_SAMPLE)
295 rt2x00lib_evaluate_antenna_sample(rt2x00dev);
296 else if (rt2x00dev->link.count & 1)
297 rt2x00lib_evaluate_antenna_eval(rt2x00dev);
300 static void rt2x00lib_update_link_stats(struct link *link, int rssi)
307 if (link->qual.avg_rssi)
308 avg_rssi = MOVING_AVERAGE(link->qual.avg_rssi, rssi, 8);
309 link->qual.avg_rssi = avg_rssi;
312 * Update antenna RSSI
314 if (link->ant.rssi_ant)
315 rssi = MOVING_AVERAGE(link->ant.rssi_ant, rssi, 8);
316 link->ant.rssi_ant = rssi;
319 static void rt2x00lib_precalculate_link_signal(struct link_qual *qual)
321 if (qual->rx_failed || qual->rx_success)
322 qual->rx_percentage =
323 (qual->rx_success * 100) /
324 (qual->rx_failed + qual->rx_success);
326 qual->rx_percentage = 50;
328 if (qual->tx_failed || qual->tx_success)
329 qual->tx_percentage =
330 (qual->tx_success * 100) /
331 (qual->tx_failed + qual->tx_success);
333 qual->tx_percentage = 50;
335 qual->rx_success = 0;
337 qual->tx_success = 0;
341 static int rt2x00lib_calculate_link_signal(struct rt2x00_dev *rt2x00dev,
344 int rssi_percentage = 0;
348 * We need a positive value for the RSSI.
351 rssi += rt2x00dev->rssi_offset;
354 * Calculate the different percentages,
355 * which will be used for the signal.
357 if (rt2x00dev->rssi_offset)
358 rssi_percentage = (rssi * 100) / rt2x00dev->rssi_offset;
361 * Add the individual percentages and use the WEIGHT
362 * defines to calculate the current link signal.
364 signal = ((WEIGHT_RSSI * rssi_percentage) +
365 (WEIGHT_TX * rt2x00dev->link.qual.tx_percentage) +
366 (WEIGHT_RX * rt2x00dev->link.qual.rx_percentage)) / 100;
368 return (signal > 100) ? 100 : signal;
371 static void rt2x00lib_link_tuner(struct work_struct *work)
373 struct rt2x00_dev *rt2x00dev =
374 container_of(work, struct rt2x00_dev, link.work.work);
377 * When the radio is shutting down we should
378 * immediately cease all link tuning.
380 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
386 rt2x00dev->ops->lib->link_stats(rt2x00dev, &rt2x00dev->link.qual);
387 rt2x00dev->low_level_stats.dot11FCSErrorCount +=
388 rt2x00dev->link.qual.rx_failed;
391 * Only perform the link tuning when Link tuning
392 * has been enabled (This could have been disabled from the EEPROM).
394 if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
395 rt2x00dev->ops->lib->link_tuner(rt2x00dev);
398 * Evaluate antenna setup.
400 rt2x00lib_evaluate_antenna(rt2x00dev);
403 * Precalculate a portion of the link signal which is
404 * in based on the tx/rx success/failure counters.
406 rt2x00lib_precalculate_link_signal(&rt2x00dev->link.qual);
409 * Increase tuner counter, and reschedule the next link tuner run.
411 rt2x00dev->link.count++;
412 queue_delayed_work(rt2x00dev->hw->workqueue, &rt2x00dev->link.work,
416 static void rt2x00lib_packetfilter_scheduled(struct work_struct *work)
418 struct rt2x00_dev *rt2x00dev =
419 container_of(work, struct rt2x00_dev, filter_work);
420 unsigned int filter = rt2x00dev->packet_filter;
423 * Since we had stored the filter inside interface.filter,
424 * we should now clear that field. Otherwise the driver will
425 * assume nothing has changed (*total_flags will be compared
426 * to interface.filter to determine if any action is required).
428 rt2x00dev->packet_filter = 0;
430 rt2x00dev->ops->hw->configure_filter(rt2x00dev->hw,
431 filter, &filter, 0, NULL);
434 static void rt2x00lib_configuration_scheduled(struct work_struct *work)
436 struct rt2x00_dev *rt2x00dev =
437 container_of(work, struct rt2x00_dev, config_work);
438 int preamble = !test_bit(CONFIG_SHORT_PREAMBLE, &rt2x00dev->flags);
440 rt2x00mac_erp_ie_changed(rt2x00dev->hw,
441 IEEE80211_ERP_CHANGE_PREAMBLE, 0, preamble);
445 * Interrupt context handlers.
447 static void rt2x00lib_beacondone_scheduled(struct work_struct *work)
449 struct rt2x00_dev *rt2x00dev =
450 container_of(work, struct rt2x00_dev, beacon_work);
451 struct data_ring *ring =
452 rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_BEACON);
453 struct data_entry *entry = rt2x00_get_data_entry(ring);
456 skb = ieee80211_beacon_get(rt2x00dev->hw,
457 rt2x00dev->interface.id,
458 &entry->tx_status.control);
462 rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw, skb,
463 &entry->tx_status.control);
468 void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
470 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
473 queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->beacon_work);
475 EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
477 void rt2x00lib_txdone(struct data_entry *entry,
478 const int status, const int retry)
480 struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
481 struct ieee80211_tx_status *tx_status = &entry->tx_status;
482 struct ieee80211_low_level_stats *stats = &rt2x00dev->low_level_stats;
483 int success = !!(status == TX_SUCCESS || status == TX_SUCCESS_RETRY);
484 int fail = !!(status == TX_FAIL_RETRY || status == TX_FAIL_INVALID ||
485 status == TX_FAIL_OTHER);
488 * Update TX statistics.
490 tx_status->flags = 0;
491 tx_status->ack_signal = 0;
492 tx_status->excessive_retries = (status == TX_FAIL_RETRY);
493 tx_status->retry_count = retry;
494 rt2x00dev->link.qual.tx_success += success;
495 rt2x00dev->link.qual.tx_failed += retry + fail;
497 if (!(tx_status->control.flags & IEEE80211_TXCTL_NO_ACK)) {
499 tx_status->flags |= IEEE80211_TX_STATUS_ACK;
501 stats->dot11ACKFailureCount++;
504 tx_status->queue_length = entry->ring->stats.limit;
505 tx_status->queue_number = tx_status->control.queue;
507 if (tx_status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) {
509 stats->dot11RTSSuccessCount++;
511 stats->dot11RTSFailureCount++;
515 * Send the tx_status to mac80211 & debugfs.
516 * mac80211 will clean up the skb structure.
518 get_skb_desc(entry->skb)->frame_type = DUMP_FRAME_TXDONE;
519 rt2x00debug_dump_frame(rt2x00dev, entry->skb);
520 ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb, tx_status);
523 EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
525 void rt2x00lib_rxdone(struct data_entry *entry, struct sk_buff *skb,
526 struct rxdata_entry_desc *desc)
528 struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
529 struct interface *intf = &rt2x00dev->interface;
530 struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
531 struct ieee80211_hw_mode *mode;
532 struct ieee80211_rate *rate;
533 struct ieee80211_hdr *hdr;
539 * Update RX statistics.
541 mode = &rt2x00dev->hwmodes[rt2x00dev->curr_hwmode];
542 for (i = 0; i < mode->num_rates; i++) {
543 rate = &mode->rates[i];
546 * When frame was received with an OFDM bitrate,
547 * the signal is the PLCP value. If it was received with
548 * a CCK bitrate the signal is the rate in 0.5kbit/s.
551 val = DEVICE_GET_RATE_FIELD(rate->val, RATE);
553 val = DEVICE_GET_RATE_FIELD(rate->val, PLCP);
555 if (val == desc->signal) {
562 * Only update link status if this is a beacon frame carrying our
565 hdr = (struct ieee80211_hdr *) skb->data;
566 if (skb->len >= sizeof(struct ieee80211_hdr *)) {
567 fc = le16_to_cpu(hdr->frame_control);
568 if ((intf->type == IEEE80211_IF_TYPE_STA
569 || intf->type == IEEE80211_IF_TYPE_IBSS)
571 && compare_ether_addr(hdr->addr3, intf->bssid) == 0)
572 rt2x00lib_update_link_stats(&rt2x00dev->link,
576 rt2x00dev->link.qual.rx_success++;
578 rx_status->rate = val;
580 rt2x00lib_calculate_link_signal(rt2x00dev, desc->rssi);
581 rx_status->ssi = desc->rssi;
582 rx_status->flag = desc->flags;
583 rx_status->antenna = rt2x00dev->link.ant.active.rx;
586 * Send frame to mac80211 & debugfs
588 get_skb_desc(skb)->frame_type = DUMP_FRAME_RXDONE;
589 rt2x00debug_dump_frame(rt2x00dev, skb);
590 ieee80211_rx_irqsafe(rt2x00dev->hw, skb, rx_status);
592 EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
595 * TX descriptor initializer
597 void rt2x00lib_write_tx_desc(struct rt2x00_dev *rt2x00dev,
599 struct ieee80211_tx_control *control)
601 struct txdata_entry_desc desc;
602 struct skb_desc *skbdesc = get_skb_desc(skb);
603 struct ieee80211_hdr *ieee80211hdr = skbdesc->data;
612 memset(&desc, 0, sizeof(desc));
614 desc.cw_min = skbdesc->ring->tx_params.cw_min;
615 desc.cw_max = skbdesc->ring->tx_params.cw_max;
616 desc.aifs = skbdesc->ring->tx_params.aifs;
621 if (control->queue < rt2x00dev->hw->queues)
622 desc.queue = control->queue;
623 else if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
624 control->queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
625 desc.queue = QUEUE_MGMT;
627 desc.queue = QUEUE_OTHER;
630 * Read required fields from ieee80211 header.
632 frame_control = le16_to_cpu(ieee80211hdr->frame_control);
633 seq_ctrl = le16_to_cpu(ieee80211hdr->seq_ctrl);
635 tx_rate = control->tx_rate;
638 * Check whether this frame is to be acked
640 if (!(control->flags & IEEE80211_TXCTL_NO_ACK))
641 __set_bit(ENTRY_TXD_ACK, &desc.flags);
644 * Check if this is a RTS/CTS frame
646 if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
647 __set_bit(ENTRY_TXD_BURST, &desc.flags);
648 if (is_rts_frame(frame_control)) {
649 __set_bit(ENTRY_TXD_RTS_FRAME, &desc.flags);
650 __set_bit(ENTRY_TXD_ACK, &desc.flags);
652 __clear_bit(ENTRY_TXD_ACK, &desc.flags);
653 if (control->rts_cts_rate)
654 tx_rate = control->rts_cts_rate;
660 if (DEVICE_GET_RATE_FIELD(tx_rate, RATEMASK) & DEV_OFDM_RATEMASK)
661 __set_bit(ENTRY_TXD_OFDM_RATE, &desc.flags);
664 * Check if more fragments are pending
666 if (ieee80211_get_morefrag(ieee80211hdr)) {
667 __set_bit(ENTRY_TXD_BURST, &desc.flags);
668 __set_bit(ENTRY_TXD_MORE_FRAG, &desc.flags);
672 * Beacons and probe responses require the tsf timestamp
673 * to be inserted into the frame.
675 if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
676 is_probe_resp(frame_control))
677 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &desc.flags);
680 * Determine with what IFS priority this frame should be send.
681 * Set ifs to IFS_SIFS when the this is not the first fragment,
682 * or this fragment came after RTS/CTS.
684 if ((seq_ctrl & IEEE80211_SCTL_FRAG) > 0 ||
685 test_bit(ENTRY_TXD_RTS_FRAME, &desc.flags))
688 desc.ifs = IFS_BACKOFF;
692 * Length calculation depends on OFDM/CCK rate.
694 desc.signal = DEVICE_GET_RATE_FIELD(tx_rate, PLCP);
697 length = skbdesc->data_len + FCS_LEN;
698 if (test_bit(ENTRY_TXD_OFDM_RATE, &desc.flags)) {
699 desc.length_high = (length >> 6) & 0x3f;
700 desc.length_low = length & 0x3f;
702 bitrate = DEVICE_GET_RATE_FIELD(tx_rate, RATE);
705 * Convert length to microseconds.
707 residual = get_duration_res(length, bitrate);
708 duration = get_duration(length, bitrate);
714 * Check if we need to set the Length Extension
716 if (bitrate == 110 && residual <= 30)
717 desc.service |= 0x80;
720 desc.length_high = (duration >> 8) & 0xff;
721 desc.length_low = duration & 0xff;
724 * When preamble is enabled we should set the
725 * preamble bit for the signal.
727 if (DEVICE_GET_RATE_FIELD(tx_rate, PREAMBLE))
731 rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, skb, &desc, control);
736 skbdesc->entry->skb = skb;
737 memcpy(&skbdesc->entry->tx_status.control, control, sizeof(*control));
740 * The frame has been completely initialized and ready
741 * for sending to the device. The caller will push the
742 * frame to the device, but we are going to push the
743 * frame to debugfs here.
745 skbdesc->frame_type = DUMP_FRAME_TX;
746 rt2x00debug_dump_frame(rt2x00dev, skb);
748 EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc);
751 * Driver initialization handlers.
753 static void rt2x00lib_channel(struct ieee80211_channel *entry,
754 const int channel, const int tx_power,
757 entry->chan = channel;
759 entry->freq = 2407 + (5 * channel);
761 entry->freq = 5000 + (5 * channel);
764 IEEE80211_CHAN_W_IBSS |
765 IEEE80211_CHAN_W_ACTIVE_SCAN |
766 IEEE80211_CHAN_W_SCAN;
767 entry->power_level = tx_power;
768 entry->antenna_max = 0xff;
771 static void rt2x00lib_rate(struct ieee80211_rate *entry,
772 const int rate, const int mask,
773 const int plcp, const int flags)
777 DEVICE_SET_RATE_FIELD(rate, RATE) |
778 DEVICE_SET_RATE_FIELD(mask, RATEMASK) |
779 DEVICE_SET_RATE_FIELD(plcp, PLCP);
780 entry->flags = flags;
781 entry->val2 = entry->val;
782 if (entry->flags & IEEE80211_RATE_PREAMBLE2)
783 entry->val2 |= DEVICE_SET_RATE_FIELD(1, PREAMBLE);
784 entry->min_rssi_ack = 0;
785 entry->min_rssi_ack_delta = 0;
788 static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
789 struct hw_mode_spec *spec)
791 struct ieee80211_hw *hw = rt2x00dev->hw;
792 struct ieee80211_hw_mode *hwmodes;
793 struct ieee80211_channel *channels;
794 struct ieee80211_rate *rates;
796 unsigned char tx_power;
798 hwmodes = kzalloc(sizeof(*hwmodes) * spec->num_modes, GFP_KERNEL);
802 channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
804 goto exit_free_modes;
806 rates = kzalloc(sizeof(*rates) * spec->num_rates, GFP_KERNEL);
808 goto exit_free_channels;
811 * Initialize Rate list.
813 rt2x00lib_rate(&rates[0], 10, DEV_RATEMASK_1MB,
814 0x00, IEEE80211_RATE_CCK);
815 rt2x00lib_rate(&rates[1], 20, DEV_RATEMASK_2MB,
816 0x01, IEEE80211_RATE_CCK_2);
817 rt2x00lib_rate(&rates[2], 55, DEV_RATEMASK_5_5MB,
818 0x02, IEEE80211_RATE_CCK_2);
819 rt2x00lib_rate(&rates[3], 110, DEV_RATEMASK_11MB,
820 0x03, IEEE80211_RATE_CCK_2);
822 if (spec->num_rates > 4) {
823 rt2x00lib_rate(&rates[4], 60, DEV_RATEMASK_6MB,
824 0x0b, IEEE80211_RATE_OFDM);
825 rt2x00lib_rate(&rates[5], 90, DEV_RATEMASK_9MB,
826 0x0f, IEEE80211_RATE_OFDM);
827 rt2x00lib_rate(&rates[6], 120, DEV_RATEMASK_12MB,
828 0x0a, IEEE80211_RATE_OFDM);
829 rt2x00lib_rate(&rates[7], 180, DEV_RATEMASK_18MB,
830 0x0e, IEEE80211_RATE_OFDM);
831 rt2x00lib_rate(&rates[8], 240, DEV_RATEMASK_24MB,
832 0x09, IEEE80211_RATE_OFDM);
833 rt2x00lib_rate(&rates[9], 360, DEV_RATEMASK_36MB,
834 0x0d, IEEE80211_RATE_OFDM);
835 rt2x00lib_rate(&rates[10], 480, DEV_RATEMASK_48MB,
836 0x08, IEEE80211_RATE_OFDM);
837 rt2x00lib_rate(&rates[11], 540, DEV_RATEMASK_54MB,
838 0x0c, IEEE80211_RATE_OFDM);
842 * Initialize Channel list.
844 for (i = 0; i < spec->num_channels; i++) {
845 if (spec->channels[i].channel <= 14)
846 tx_power = spec->tx_power_bg[i];
847 else if (spec->tx_power_a)
848 tx_power = spec->tx_power_a[i];
850 tx_power = spec->tx_power_default;
852 rt2x00lib_channel(&channels[i],
853 spec->channels[i].channel, tx_power, i);
857 * Intitialize 802.11b
861 if (spec->num_modes > HWMODE_B) {
862 hwmodes[HWMODE_B].mode = MODE_IEEE80211B;
863 hwmodes[HWMODE_B].num_channels = 14;
864 hwmodes[HWMODE_B].num_rates = 4;
865 hwmodes[HWMODE_B].channels = channels;
866 hwmodes[HWMODE_B].rates = rates;
870 * Intitialize 802.11g
874 if (spec->num_modes > HWMODE_G) {
875 hwmodes[HWMODE_G].mode = MODE_IEEE80211G;
876 hwmodes[HWMODE_G].num_channels = 14;
877 hwmodes[HWMODE_G].num_rates = spec->num_rates;
878 hwmodes[HWMODE_G].channels = channels;
879 hwmodes[HWMODE_G].rates = rates;
883 * Intitialize 802.11a
885 * Channels: OFDM, UNII, HiperLAN2.
887 if (spec->num_modes > HWMODE_A) {
888 hwmodes[HWMODE_A].mode = MODE_IEEE80211A;
889 hwmodes[HWMODE_A].num_channels = spec->num_channels - 14;
890 hwmodes[HWMODE_A].num_rates = spec->num_rates - 4;
891 hwmodes[HWMODE_A].channels = &channels[14];
892 hwmodes[HWMODE_A].rates = &rates[4];
895 if (spec->num_modes > HWMODE_G &&
896 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_G]))
897 goto exit_free_rates;
899 if (spec->num_modes > HWMODE_B &&
900 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_B]))
901 goto exit_free_rates;
903 if (spec->num_modes > HWMODE_A &&
904 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_A]))
905 goto exit_free_rates;
907 rt2x00dev->hwmodes = hwmodes;
921 ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
925 static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
927 if (test_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags))
928 ieee80211_unregister_hw(rt2x00dev->hw);
930 if (likely(rt2x00dev->hwmodes)) {
931 kfree(rt2x00dev->hwmodes->channels);
932 kfree(rt2x00dev->hwmodes->rates);
933 kfree(rt2x00dev->hwmodes);
934 rt2x00dev->hwmodes = NULL;
938 static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
940 struct hw_mode_spec *spec = &rt2x00dev->spec;
944 * Initialize HW modes.
946 status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
953 status = ieee80211_register_hw(rt2x00dev->hw);
955 rt2x00lib_remove_hw(rt2x00dev);
959 __set_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags);
965 * Initialization/uninitialization handlers.
967 static int rt2x00lib_alloc_entries(struct data_ring *ring,
968 const u16 max_entries, const u16 data_size,
971 struct data_entry *entry;
974 ring->stats.limit = max_entries;
975 ring->data_size = data_size;
976 ring->desc_size = desc_size;
979 * Allocate all ring entries.
981 entry = kzalloc(ring->stats.limit * sizeof(*entry), GFP_KERNEL);
985 for (i = 0; i < ring->stats.limit; i++) {
987 entry[i].ring = ring;
989 entry[i].entry_idx = i;
997 static int rt2x00lib_alloc_ring_entries(struct rt2x00_dev *rt2x00dev)
999 struct data_ring *ring;
1002 * Allocate the RX ring.
1004 if (rt2x00lib_alloc_entries(rt2x00dev->rx, RX_ENTRIES, DATA_FRAME_SIZE,
1005 rt2x00dev->ops->rxd_size))
1009 * First allocate the TX rings.
1011 txring_for_each(rt2x00dev, ring) {
1012 if (rt2x00lib_alloc_entries(ring, TX_ENTRIES, DATA_FRAME_SIZE,
1013 rt2x00dev->ops->txd_size))
1017 if (!test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
1021 * Allocate the BEACON ring.
1023 if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[0], BEACON_ENTRIES,
1024 MGMT_FRAME_SIZE, rt2x00dev->ops->txd_size))
1028 * Allocate the Atim ring.
1030 if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[1], ATIM_ENTRIES,
1031 DATA_FRAME_SIZE, rt2x00dev->ops->txd_size))
1037 static void rt2x00lib_free_ring_entries(struct rt2x00_dev *rt2x00dev)
1039 struct data_ring *ring;
1041 ring_for_each(rt2x00dev, ring) {
1047 static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
1049 if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
1053 * Unregister rfkill.
1055 rt2x00rfkill_unregister(rt2x00dev);
1058 * Allow the HW to uninitialize.
1060 rt2x00dev->ops->lib->uninitialize(rt2x00dev);
1063 * Free allocated ring entries.
1065 rt2x00lib_free_ring_entries(rt2x00dev);
1068 static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
1072 if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
1076 * Allocate all ring entries.
1078 status = rt2x00lib_alloc_ring_entries(rt2x00dev);
1080 ERROR(rt2x00dev, "Ring entries allocation failed.\n");
1085 * Initialize the device.
1087 status = rt2x00dev->ops->lib->initialize(rt2x00dev);
1091 __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
1094 * Register the rfkill handler.
1096 status = rt2x00rfkill_register(rt2x00dev);
1098 goto exit_unitialize;
1103 rt2x00lib_uninitialize(rt2x00dev);
1106 rt2x00lib_free_ring_entries(rt2x00dev);
1111 int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
1115 if (test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1119 * If this is the first interface which is added,
1120 * we should load the firmware now.
1122 if (test_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags)) {
1123 retval = rt2x00lib_load_firmware(rt2x00dev);
1129 * Initialize the device.
1131 retval = rt2x00lib_initialize(rt2x00dev);
1138 retval = rt2x00lib_enable_radio(rt2x00dev);
1140 rt2x00lib_uninitialize(rt2x00dev);
1144 __set_bit(DEVICE_STARTED, &rt2x00dev->flags);
1149 void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
1151 if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1155 * Perhaps we can add something smarter here,
1156 * but for now just disabling the radio should do.
1158 rt2x00lib_disable_radio(rt2x00dev);
1160 __clear_bit(DEVICE_STARTED, &rt2x00dev->flags);
1164 * driver allocation handlers.
1166 static int rt2x00lib_alloc_rings(struct rt2x00_dev *rt2x00dev)
1168 struct data_ring *ring;
1172 * We need the following rings:
1175 * Beacon: 1 (if required)
1176 * Atim: 1 (if required)
1178 rt2x00dev->data_rings = 1 + rt2x00dev->hw->queues +
1179 (2 * test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags));
1181 ring = kzalloc(rt2x00dev->data_rings * sizeof(*ring), GFP_KERNEL);
1183 ERROR(rt2x00dev, "Ring allocation failed.\n");
1188 * Initialize pointers
1190 rt2x00dev->rx = ring;
1191 rt2x00dev->tx = &rt2x00dev->rx[1];
1192 if (test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
1193 rt2x00dev->bcn = &rt2x00dev->tx[rt2x00dev->hw->queues];
1196 * Initialize ring parameters.
1198 * cw_max: 2^10 = 1024.
1201 ring_for_each(rt2x00dev, ring) {
1202 ring->rt2x00dev = rt2x00dev;
1203 ring->queue_idx = index++;
1204 ring->tx_params.aifs = 2;
1205 ring->tx_params.cw_min = 5;
1206 ring->tx_params.cw_max = 10;
1212 static void rt2x00lib_free_rings(struct rt2x00_dev *rt2x00dev)
1214 kfree(rt2x00dev->rx);
1215 rt2x00dev->rx = NULL;
1216 rt2x00dev->tx = NULL;
1217 rt2x00dev->bcn = NULL;
1220 int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
1222 int retval = -ENOMEM;
1225 * Let the driver probe the device to detect the capabilities.
1227 retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
1229 ERROR(rt2x00dev, "Failed to allocate device.\n");
1234 * Initialize configuration work.
1236 INIT_WORK(&rt2x00dev->beacon_work, rt2x00lib_beacondone_scheduled);
1237 INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
1238 INIT_WORK(&rt2x00dev->config_work, rt2x00lib_configuration_scheduled);
1239 INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
1242 * Reset current working type.
1244 rt2x00dev->interface.type = IEEE80211_IF_TYPE_INVALID;
1247 * Allocate ring array.
1249 retval = rt2x00lib_alloc_rings(rt2x00dev);
1254 * Initialize ieee80211 structure.
1256 retval = rt2x00lib_probe_hw(rt2x00dev);
1258 ERROR(rt2x00dev, "Failed to initialize hw.\n");
1265 retval = rt2x00rfkill_allocate(rt2x00dev);
1270 * Open the debugfs entry.
1272 rt2x00debug_register(rt2x00dev);
1274 __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1279 rt2x00lib_remove_dev(rt2x00dev);
1283 EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
1285 void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
1287 __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1292 rt2x00lib_disable_radio(rt2x00dev);
1295 * Uninitialize device.
1297 rt2x00lib_uninitialize(rt2x00dev);
1300 * Close debugfs entry.
1302 rt2x00debug_deregister(rt2x00dev);
1307 rt2x00rfkill_free(rt2x00dev);
1310 * Free ieee80211_hw memory.
1312 rt2x00lib_remove_hw(rt2x00dev);
1315 * Free firmware image.
1317 rt2x00lib_free_firmware(rt2x00dev);
1320 * Free ring structures.
1322 rt2x00lib_free_rings(rt2x00dev);
1324 EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
1327 * Device state handlers
1330 int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
1334 NOTICE(rt2x00dev, "Going to sleep.\n");
1335 __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1338 * Only continue if mac80211 has open interfaces.
1340 if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1342 __set_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags);
1345 * Disable radio and unitialize all items
1346 * that must be recreated on resume.
1348 rt2x00lib_stop(rt2x00dev);
1349 rt2x00lib_uninitialize(rt2x00dev);
1350 rt2x00debug_deregister(rt2x00dev);
1354 * Set device mode to sleep for power management.
1356 retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
1362 EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1364 int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1366 struct interface *intf = &rt2x00dev->interface;
1369 NOTICE(rt2x00dev, "Waking up.\n");
1372 * Open the debugfs entry.
1374 rt2x00debug_register(rt2x00dev);
1377 * Only continue if mac80211 had open interfaces.
1379 if (!__test_and_clear_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags))
1383 * Reinitialize device and all active interfaces.
1385 retval = rt2x00lib_start(rt2x00dev);
1390 * Reconfigure device.
1392 rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1);
1393 if (!rt2x00dev->hw->conf.radio_enabled)
1394 rt2x00lib_disable_radio(rt2x00dev);
1396 rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
1397 rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
1398 rt2x00lib_config_type(rt2x00dev, intf->type);
1401 * We are ready again to receive requests from mac80211.
1403 __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1406 * It is possible that during that mac80211 has attempted
1407 * to send frames while we were suspending or resuming.
1408 * In that case we have disabled the TX queue and should
1409 * now enable it again
1411 ieee80211_start_queues(rt2x00dev->hw);
1414 * When in Master or Ad-hoc mode,
1415 * restart Beacon transmitting by faking a beacondone event.
1417 if (intf->type == IEEE80211_IF_TYPE_AP ||
1418 intf->type == IEEE80211_IF_TYPE_IBSS)
1419 rt2x00lib_beacondone(rt2x00dev);
1424 rt2x00lib_disable_radio(rt2x00dev);
1425 rt2x00lib_uninitialize(rt2x00dev);
1426 rt2x00debug_deregister(rt2x00dev);
1430 EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1431 #endif /* CONFIG_PM */
1434 * rt2x00lib module information.
1436 MODULE_AUTHOR(DRV_PROJECT);
1437 MODULE_VERSION(DRV_VERSION);
1438 MODULE_DESCRIPTION("rt2x00 library");
1439 MODULE_LICENSE("GPL");