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,
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 whether this frame is to be acked
634 if (!(control->flags & IEEE80211_TXCTL_NO_ACK))
635 __set_bit(ENTRY_TXD_ACK, &desc.flags);
638 * Check if this is a RTS/CTS frame
640 if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
641 __set_bit(ENTRY_TXD_BURST, &desc.flags);
642 if (is_rts_frame(frame_control)) {
643 __set_bit(ENTRY_TXD_RTS_FRAME, &desc.flags);
644 __set_bit(ENTRY_TXD_ACK, &desc.flags);
646 __clear_bit(ENTRY_TXD_ACK, &desc.flags);
647 if (control->rts_cts_rate)
648 tx_rate = control->rts_cts_rate;
654 if (DEVICE_GET_RATE_FIELD(tx_rate, RATEMASK) & DEV_OFDM_RATEMASK)
655 __set_bit(ENTRY_TXD_OFDM_RATE, &desc.flags);
658 * Check if more fragments are pending
660 if (ieee80211_get_morefrag(ieee80211hdr)) {
661 __set_bit(ENTRY_TXD_BURST, &desc.flags);
662 __set_bit(ENTRY_TXD_MORE_FRAG, &desc.flags);
666 * Beacons and probe responses require the tsf timestamp
667 * to be inserted into the frame.
669 if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
670 is_probe_resp(frame_control))
671 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &desc.flags);
674 * Determine with what IFS priority this frame should be send.
675 * Set ifs to IFS_SIFS when the this is not the first fragment,
676 * or this fragment came after RTS/CTS.
678 if ((seq_ctrl & IEEE80211_SCTL_FRAG) > 0 ||
679 test_bit(ENTRY_TXD_RTS_FRAME, &desc.flags))
682 desc.ifs = IFS_BACKOFF;
686 * Length calculation depends on OFDM/CCK rate.
688 desc.signal = DEVICE_GET_RATE_FIELD(tx_rate, PLCP);
691 if (test_bit(ENTRY_TXD_OFDM_RATE, &desc.flags)) {
692 desc.length_high = ((length + FCS_LEN) >> 6) & 0x3f;
693 desc.length_low = ((length + FCS_LEN) & 0x3f);
695 bitrate = DEVICE_GET_RATE_FIELD(tx_rate, RATE);
698 * Convert length to microseconds.
700 residual = get_duration_res(length + FCS_LEN, bitrate);
701 duration = get_duration(length + FCS_LEN, bitrate);
707 * Check if we need to set the Length Extension
709 if (bitrate == 110 && residual <= 30)
710 desc.service |= 0x80;
713 desc.length_high = (duration >> 8) & 0xff;
714 desc.length_low = duration & 0xff;
717 * When preamble is enabled we should set the
718 * preamble bit for the signal.
720 if (DEVICE_GET_RATE_FIELD(tx_rate, PREAMBLE))
724 rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, txd, &desc,
725 ieee80211hdr, length, control);
727 EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc);
730 * Driver initialization handlers.
732 static void rt2x00lib_channel(struct ieee80211_channel *entry,
733 const int channel, const int tx_power,
736 entry->chan = channel;
738 entry->freq = 2407 + (5 * channel);
740 entry->freq = 5000 + (5 * channel);
743 IEEE80211_CHAN_W_IBSS |
744 IEEE80211_CHAN_W_ACTIVE_SCAN |
745 IEEE80211_CHAN_W_SCAN;
746 entry->power_level = tx_power;
747 entry->antenna_max = 0xff;
750 static void rt2x00lib_rate(struct ieee80211_rate *entry,
751 const int rate, const int mask,
752 const int plcp, const int flags)
756 DEVICE_SET_RATE_FIELD(rate, RATE) |
757 DEVICE_SET_RATE_FIELD(mask, RATEMASK) |
758 DEVICE_SET_RATE_FIELD(plcp, PLCP);
759 entry->flags = flags;
760 entry->val2 = entry->val;
761 if (entry->flags & IEEE80211_RATE_PREAMBLE2)
762 entry->val2 |= DEVICE_SET_RATE_FIELD(1, PREAMBLE);
763 entry->min_rssi_ack = 0;
764 entry->min_rssi_ack_delta = 0;
767 static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
768 struct hw_mode_spec *spec)
770 struct ieee80211_hw *hw = rt2x00dev->hw;
771 struct ieee80211_hw_mode *hwmodes;
772 struct ieee80211_channel *channels;
773 struct ieee80211_rate *rates;
775 unsigned char tx_power;
777 hwmodes = kzalloc(sizeof(*hwmodes) * spec->num_modes, GFP_KERNEL);
781 channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
783 goto exit_free_modes;
785 rates = kzalloc(sizeof(*rates) * spec->num_rates, GFP_KERNEL);
787 goto exit_free_channels;
790 * Initialize Rate list.
792 rt2x00lib_rate(&rates[0], 10, DEV_RATEMASK_1MB,
793 0x00, IEEE80211_RATE_CCK);
794 rt2x00lib_rate(&rates[1], 20, DEV_RATEMASK_2MB,
795 0x01, IEEE80211_RATE_CCK_2);
796 rt2x00lib_rate(&rates[2], 55, DEV_RATEMASK_5_5MB,
797 0x02, IEEE80211_RATE_CCK_2);
798 rt2x00lib_rate(&rates[3], 110, DEV_RATEMASK_11MB,
799 0x03, IEEE80211_RATE_CCK_2);
801 if (spec->num_rates > 4) {
802 rt2x00lib_rate(&rates[4], 60, DEV_RATEMASK_6MB,
803 0x0b, IEEE80211_RATE_OFDM);
804 rt2x00lib_rate(&rates[5], 90, DEV_RATEMASK_9MB,
805 0x0f, IEEE80211_RATE_OFDM);
806 rt2x00lib_rate(&rates[6], 120, DEV_RATEMASK_12MB,
807 0x0a, IEEE80211_RATE_OFDM);
808 rt2x00lib_rate(&rates[7], 180, DEV_RATEMASK_18MB,
809 0x0e, IEEE80211_RATE_OFDM);
810 rt2x00lib_rate(&rates[8], 240, DEV_RATEMASK_24MB,
811 0x09, IEEE80211_RATE_OFDM);
812 rt2x00lib_rate(&rates[9], 360, DEV_RATEMASK_36MB,
813 0x0d, IEEE80211_RATE_OFDM);
814 rt2x00lib_rate(&rates[10], 480, DEV_RATEMASK_48MB,
815 0x08, IEEE80211_RATE_OFDM);
816 rt2x00lib_rate(&rates[11], 540, DEV_RATEMASK_54MB,
817 0x0c, IEEE80211_RATE_OFDM);
821 * Initialize Channel list.
823 for (i = 0; i < spec->num_channels; i++) {
824 if (spec->channels[i].channel <= 14)
825 tx_power = spec->tx_power_bg[i];
826 else if (spec->tx_power_a)
827 tx_power = spec->tx_power_a[i];
829 tx_power = spec->tx_power_default;
831 rt2x00lib_channel(&channels[i],
832 spec->channels[i].channel, tx_power, i);
836 * Intitialize 802.11b
840 if (spec->num_modes > HWMODE_B) {
841 hwmodes[HWMODE_B].mode = MODE_IEEE80211B;
842 hwmodes[HWMODE_B].num_channels = 14;
843 hwmodes[HWMODE_B].num_rates = 4;
844 hwmodes[HWMODE_B].channels = channels;
845 hwmodes[HWMODE_B].rates = rates;
849 * Intitialize 802.11g
853 if (spec->num_modes > HWMODE_G) {
854 hwmodes[HWMODE_G].mode = MODE_IEEE80211G;
855 hwmodes[HWMODE_G].num_channels = 14;
856 hwmodes[HWMODE_G].num_rates = spec->num_rates;
857 hwmodes[HWMODE_G].channels = channels;
858 hwmodes[HWMODE_G].rates = rates;
862 * Intitialize 802.11a
864 * Channels: OFDM, UNII, HiperLAN2.
866 if (spec->num_modes > HWMODE_A) {
867 hwmodes[HWMODE_A].mode = MODE_IEEE80211A;
868 hwmodes[HWMODE_A].num_channels = spec->num_channels - 14;
869 hwmodes[HWMODE_A].num_rates = spec->num_rates - 4;
870 hwmodes[HWMODE_A].channels = &channels[14];
871 hwmodes[HWMODE_A].rates = &rates[4];
874 if (spec->num_modes > HWMODE_G &&
875 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_G]))
876 goto exit_free_rates;
878 if (spec->num_modes > HWMODE_B &&
879 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_B]))
880 goto exit_free_rates;
882 if (spec->num_modes > HWMODE_A &&
883 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_A]))
884 goto exit_free_rates;
886 rt2x00dev->hwmodes = hwmodes;
900 ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
904 static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
906 if (test_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags))
907 ieee80211_unregister_hw(rt2x00dev->hw);
909 if (likely(rt2x00dev->hwmodes)) {
910 kfree(rt2x00dev->hwmodes->channels);
911 kfree(rt2x00dev->hwmodes->rates);
912 kfree(rt2x00dev->hwmodes);
913 rt2x00dev->hwmodes = NULL;
917 static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
919 struct hw_mode_spec *spec = &rt2x00dev->spec;
923 * Initialize HW modes.
925 status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
932 status = ieee80211_register_hw(rt2x00dev->hw);
934 rt2x00lib_remove_hw(rt2x00dev);
938 __set_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags);
944 * Initialization/uninitialization handlers.
946 static int rt2x00lib_alloc_entries(struct data_ring *ring,
947 const u16 max_entries, const u16 data_size,
950 struct data_entry *entry;
953 ring->stats.limit = max_entries;
954 ring->data_size = data_size;
955 ring->desc_size = desc_size;
958 * Allocate all ring entries.
960 entry = kzalloc(ring->stats.limit * sizeof(*entry), GFP_KERNEL);
964 for (i = 0; i < ring->stats.limit; i++) {
966 entry[i].ring = ring;
975 static int rt2x00lib_alloc_ring_entries(struct rt2x00_dev *rt2x00dev)
977 struct data_ring *ring;
980 * Allocate the RX ring.
982 if (rt2x00lib_alloc_entries(rt2x00dev->rx, RX_ENTRIES, DATA_FRAME_SIZE,
983 rt2x00dev->ops->rxd_size))
987 * First allocate the TX rings.
989 txring_for_each(rt2x00dev, ring) {
990 if (rt2x00lib_alloc_entries(ring, TX_ENTRIES, DATA_FRAME_SIZE,
991 rt2x00dev->ops->txd_size))
995 if (!test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
999 * Allocate the BEACON ring.
1001 if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[0], BEACON_ENTRIES,
1002 MGMT_FRAME_SIZE, rt2x00dev->ops->txd_size))
1006 * Allocate the Atim ring.
1008 if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[1], ATIM_ENTRIES,
1009 DATA_FRAME_SIZE, rt2x00dev->ops->txd_size))
1015 static void rt2x00lib_free_ring_entries(struct rt2x00_dev *rt2x00dev)
1017 struct data_ring *ring;
1019 ring_for_each(rt2x00dev, ring) {
1025 void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
1027 if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
1031 * Unregister rfkill.
1033 rt2x00rfkill_unregister(rt2x00dev);
1036 * Allow the HW to uninitialize.
1038 rt2x00dev->ops->lib->uninitialize(rt2x00dev);
1041 * Free allocated ring entries.
1043 rt2x00lib_free_ring_entries(rt2x00dev);
1046 int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
1050 if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
1054 * Allocate all ring entries.
1056 status = rt2x00lib_alloc_ring_entries(rt2x00dev);
1058 ERROR(rt2x00dev, "Ring entries allocation failed.\n");
1063 * Initialize the device.
1065 status = rt2x00dev->ops->lib->initialize(rt2x00dev);
1069 __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
1072 * Register the rfkill handler.
1074 status = rt2x00rfkill_register(rt2x00dev);
1076 goto exit_unitialize;
1081 rt2x00lib_uninitialize(rt2x00dev);
1084 rt2x00lib_free_ring_entries(rt2x00dev);
1090 * driver allocation handlers.
1092 static int rt2x00lib_alloc_rings(struct rt2x00_dev *rt2x00dev)
1094 struct data_ring *ring;
1097 * We need the following rings:
1100 * Beacon: 1 (if required)
1101 * Atim: 1 (if required)
1103 rt2x00dev->data_rings = 1 + rt2x00dev->hw->queues +
1104 (2 * test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags));
1106 ring = kzalloc(rt2x00dev->data_rings * sizeof(*ring), GFP_KERNEL);
1108 ERROR(rt2x00dev, "Ring allocation failed.\n");
1113 * Initialize pointers
1115 rt2x00dev->rx = ring;
1116 rt2x00dev->tx = &rt2x00dev->rx[1];
1117 if (test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
1118 rt2x00dev->bcn = &rt2x00dev->tx[rt2x00dev->hw->queues];
1121 * Initialize ring parameters.
1123 * cw_max: 2^10 = 1024.
1125 ring_for_each(rt2x00dev, ring) {
1126 ring->rt2x00dev = rt2x00dev;
1127 ring->tx_params.aifs = 2;
1128 ring->tx_params.cw_min = 5;
1129 ring->tx_params.cw_max = 10;
1135 static void rt2x00lib_free_rings(struct rt2x00_dev *rt2x00dev)
1137 kfree(rt2x00dev->rx);
1138 rt2x00dev->rx = NULL;
1139 rt2x00dev->tx = NULL;
1140 rt2x00dev->bcn = NULL;
1143 int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
1145 int retval = -ENOMEM;
1148 * Let the driver probe the device to detect the capabilities.
1150 retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
1152 ERROR(rt2x00dev, "Failed to allocate device.\n");
1157 * Initialize configuration work.
1159 INIT_WORK(&rt2x00dev->beacon_work, rt2x00lib_beacondone_scheduled);
1160 INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
1161 INIT_WORK(&rt2x00dev->config_work, rt2x00lib_configuration_scheduled);
1162 INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
1165 * Reset current working type.
1167 rt2x00dev->interface.type = INVALID_INTERFACE;
1170 * Allocate ring array.
1172 retval = rt2x00lib_alloc_rings(rt2x00dev);
1177 * Initialize ieee80211 structure.
1179 retval = rt2x00lib_probe_hw(rt2x00dev);
1181 ERROR(rt2x00dev, "Failed to initialize hw.\n");
1188 retval = rt2x00rfkill_allocate(rt2x00dev);
1193 * Open the debugfs entry.
1195 rt2x00debug_register(rt2x00dev);
1197 __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1202 rt2x00lib_remove_dev(rt2x00dev);
1206 EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
1208 void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
1210 __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1215 rt2x00lib_disable_radio(rt2x00dev);
1218 * Uninitialize device.
1220 rt2x00lib_uninitialize(rt2x00dev);
1223 * Close debugfs entry.
1225 rt2x00debug_deregister(rt2x00dev);
1230 rt2x00rfkill_free(rt2x00dev);
1233 * Free ieee80211_hw memory.
1235 rt2x00lib_remove_hw(rt2x00dev);
1238 * Free firmware image.
1240 rt2x00lib_free_firmware(rt2x00dev);
1243 * Free ring structures.
1245 rt2x00lib_free_rings(rt2x00dev);
1247 EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
1250 * Device state handlers
1253 int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
1257 NOTICE(rt2x00dev, "Going to sleep.\n");
1258 __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1261 * Only continue if mac80211 has open interfaces.
1263 if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1265 __set_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags);
1268 * Disable radio and unitialize all items
1269 * that must be recreated on resume.
1271 rt2x00mac_stop(rt2x00dev->hw);
1272 rt2x00lib_uninitialize(rt2x00dev);
1273 rt2x00debug_deregister(rt2x00dev);
1277 * Set device mode to sleep for power management.
1279 retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
1285 EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1287 int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1289 struct interface *intf = &rt2x00dev->interface;
1292 NOTICE(rt2x00dev, "Waking up.\n");
1293 __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1296 * Open the debugfs entry.
1298 rt2x00debug_register(rt2x00dev);
1301 * Only continue if mac80211 had open interfaces.
1303 if (!__test_and_clear_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags))
1307 * Reinitialize device and all active interfaces.
1309 retval = rt2x00mac_start(rt2x00dev->hw);
1314 * Reconfigure device.
1316 rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1);
1317 if (!rt2x00dev->hw->conf.radio_enabled)
1318 rt2x00lib_disable_radio(rt2x00dev);
1320 rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
1321 rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
1322 rt2x00lib_config_type(rt2x00dev, intf->type);
1325 * It is possible that during that mac80211 has attempted
1326 * to send frames while we were suspending or resuming.
1327 * In that case we have disabled the TX queue and should
1328 * now enable it again
1330 ieee80211_start_queues(rt2x00dev->hw);
1333 * When in Master or Ad-hoc mode,
1334 * restart Beacon transmitting by faking a beacondone event.
1336 if (intf->type == IEEE80211_IF_TYPE_AP ||
1337 intf->type == IEEE80211_IF_TYPE_IBSS)
1338 rt2x00lib_beacondone(rt2x00dev);
1343 rt2x00lib_disable_radio(rt2x00dev);
1344 rt2x00lib_uninitialize(rt2x00dev);
1345 rt2x00debug_deregister(rt2x00dev);
1349 EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1350 #endif /* CONFIG_PM */
1353 * rt2x00lib module information.
1355 MODULE_AUTHOR(DRV_PROJECT);
1356 MODULE_VERSION(DRV_VERSION);
1357 MODULE_DESCRIPTION("rt2x00 library");
1358 MODULE_LICENSE("GPL");