2 Copyright (C) 2004 - 2008 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"
34 * Link tuning handlers
36 void rt2x00lib_reset_link_tuner(struct rt2x00_dev *rt2x00dev)
38 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
42 * Reset link information.
43 * Both the currently active vgc level as well as
44 * the link tuner counter should be reset. Resetting
45 * the counter is important for devices where the
46 * device should only perform link tuning during the
47 * first minute after being enabled.
49 rt2x00dev->link.count = 0;
50 rt2x00dev->link.vgc_level = 0;
53 * Reset the link tuner.
55 rt2x00dev->ops->lib->reset_tuner(rt2x00dev);
58 static void rt2x00lib_start_link_tuner(struct rt2x00_dev *rt2x00dev)
61 * Clear all (possibly) pre-existing quality statistics.
63 memset(&rt2x00dev->link.qual, 0, sizeof(rt2x00dev->link.qual));
66 * The RX and TX percentage should start at 50%
67 * this will assure we will get at least get some
68 * decent value when the link tuner starts.
69 * The value will be dropped and overwritten with
70 * the correct (measured )value anyway during the
71 * first run of the link tuner.
73 rt2x00dev->link.qual.rx_percentage = 50;
74 rt2x00dev->link.qual.tx_percentage = 50;
76 rt2x00lib_reset_link_tuner(rt2x00dev);
78 queue_delayed_work(rt2x00dev->hw->workqueue,
79 &rt2x00dev->link.work, LINK_TUNE_INTERVAL);
82 static void rt2x00lib_stop_link_tuner(struct rt2x00_dev *rt2x00dev)
84 cancel_delayed_work_sync(&rt2x00dev->link.work);
88 * Radio control handlers.
90 int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
95 * Don't enable the radio twice.
96 * And check if the hardware button has been disabled.
98 if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
99 test_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags))
103 * Initialize all data queues.
105 rt2x00queue_init_rx(rt2x00dev);
106 rt2x00queue_init_tx(rt2x00dev);
111 status = rt2x00dev->ops->lib->set_device_state(rt2x00dev,
116 __set_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags);
121 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
124 * Start the TX queues.
126 ieee80211_start_queues(rt2x00dev->hw);
131 void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
133 if (!__test_and_clear_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
137 * Stop all scheduled work.
139 if (work_pending(&rt2x00dev->beacon_work))
140 cancel_work_sync(&rt2x00dev->beacon_work);
141 if (work_pending(&rt2x00dev->filter_work))
142 cancel_work_sync(&rt2x00dev->filter_work);
143 if (work_pending(&rt2x00dev->config_work))
144 cancel_work_sync(&rt2x00dev->config_work);
147 * Stop the TX queues.
149 ieee80211_stop_queues(rt2x00dev->hw);
154 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
159 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
162 void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, enum dev_state state)
165 * When we are disabling the RX, we should also stop the link tuner.
167 if (state == STATE_RADIO_RX_OFF)
168 rt2x00lib_stop_link_tuner(rt2x00dev);
170 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
173 * When we are enabling the RX, we should also start the link tuner.
175 if (state == STATE_RADIO_RX_ON &&
176 is_interface_present(&rt2x00dev->interface))
177 rt2x00lib_start_link_tuner(rt2x00dev);
180 static void rt2x00lib_evaluate_antenna_sample(struct rt2x00_dev *rt2x00dev)
182 enum antenna rx = rt2x00dev->link.ant.active.rx;
183 enum antenna tx = rt2x00dev->link.ant.active.tx;
185 rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_A);
187 rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_B);
190 * We are done sampling. Now we should evaluate the results.
192 rt2x00dev->link.ant.flags &= ~ANTENNA_MODE_SAMPLE;
195 * During the last period we have sampled the RSSI
196 * from both antenna's. It now is time to determine
197 * which antenna demonstrated the best performance.
198 * When we are already on the antenna with the best
199 * performance, then there really is nothing for us
202 if (sample_a == sample_b)
205 if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY)
206 rx = (sample_a > sample_b) ? ANTENNA_A : ANTENNA_B;
208 if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)
209 tx = (sample_a > sample_b) ? ANTENNA_A : ANTENNA_B;
211 rt2x00lib_config_antenna(rt2x00dev, rx, tx);
214 static void rt2x00lib_evaluate_antenna_eval(struct rt2x00_dev *rt2x00dev)
216 enum antenna rx = rt2x00dev->link.ant.active.rx;
217 enum antenna tx = rt2x00dev->link.ant.active.tx;
218 int rssi_curr = rt2x00_get_link_ant_rssi(&rt2x00dev->link);
219 int rssi_old = rt2x00_update_ant_rssi(&rt2x00dev->link, rssi_curr);
222 * Legacy driver indicates that we should swap antenna's
223 * when the difference in RSSI is greater that 5. This
224 * also should be done when the RSSI was actually better
225 * then the previous sample.
226 * When the difference exceeds the threshold we should
227 * sample the rssi from the other antenna to make a valid
228 * comparison between the 2 antennas.
230 if (abs(rssi_curr - rssi_old) < 5)
233 rt2x00dev->link.ant.flags |= ANTENNA_MODE_SAMPLE;
235 if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY)
236 rx = (rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
238 if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)
239 tx = (tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
241 rt2x00lib_config_antenna(rt2x00dev, rx, tx);
244 static void rt2x00lib_evaluate_antenna(struct rt2x00_dev *rt2x00dev)
247 * Determine if software diversity is enabled for
248 * either the TX or RX antenna (or both).
249 * Always perform this check since within the link
250 * tuner interval the configuration might have changed.
252 rt2x00dev->link.ant.flags &= ~ANTENNA_RX_DIVERSITY;
253 rt2x00dev->link.ant.flags &= ~ANTENNA_TX_DIVERSITY;
255 if (rt2x00dev->hw->conf.antenna_sel_rx == 0 &&
256 rt2x00dev->default_ant.rx == ANTENNA_SW_DIVERSITY)
257 rt2x00dev->link.ant.flags |= ANTENNA_RX_DIVERSITY;
258 if (rt2x00dev->hw->conf.antenna_sel_tx == 0 &&
259 rt2x00dev->default_ant.tx == ANTENNA_SW_DIVERSITY)
260 rt2x00dev->link.ant.flags |= ANTENNA_TX_DIVERSITY;
262 if (!(rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY) &&
263 !(rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)) {
264 rt2x00dev->link.ant.flags = 0;
269 * If we have only sampled the data over the last period
270 * we should now harvest the data. Otherwise just evaluate
271 * the data. The latter should only be performed once
274 if (rt2x00dev->link.ant.flags & ANTENNA_MODE_SAMPLE)
275 rt2x00lib_evaluate_antenna_sample(rt2x00dev);
276 else if (rt2x00dev->link.count & 1)
277 rt2x00lib_evaluate_antenna_eval(rt2x00dev);
280 static void rt2x00lib_update_link_stats(struct link *link, int rssi)
287 if (link->qual.avg_rssi)
288 avg_rssi = MOVING_AVERAGE(link->qual.avg_rssi, rssi, 8);
289 link->qual.avg_rssi = avg_rssi;
292 * Update antenna RSSI
294 if (link->ant.rssi_ant)
295 rssi = MOVING_AVERAGE(link->ant.rssi_ant, rssi, 8);
296 link->ant.rssi_ant = rssi;
299 static void rt2x00lib_precalculate_link_signal(struct link_qual *qual)
301 if (qual->rx_failed || qual->rx_success)
302 qual->rx_percentage =
303 (qual->rx_success * 100) /
304 (qual->rx_failed + qual->rx_success);
306 qual->rx_percentage = 50;
308 if (qual->tx_failed || qual->tx_success)
309 qual->tx_percentage =
310 (qual->tx_success * 100) /
311 (qual->tx_failed + qual->tx_success);
313 qual->tx_percentage = 50;
315 qual->rx_success = 0;
317 qual->tx_success = 0;
321 static int rt2x00lib_calculate_link_signal(struct rt2x00_dev *rt2x00dev,
324 int rssi_percentage = 0;
328 * We need a positive value for the RSSI.
331 rssi += rt2x00dev->rssi_offset;
334 * Calculate the different percentages,
335 * which will be used for the signal.
337 if (rt2x00dev->rssi_offset)
338 rssi_percentage = (rssi * 100) / rt2x00dev->rssi_offset;
341 * Add the individual percentages and use the WEIGHT
342 * defines to calculate the current link signal.
344 signal = ((WEIGHT_RSSI * rssi_percentage) +
345 (WEIGHT_TX * rt2x00dev->link.qual.tx_percentage) +
346 (WEIGHT_RX * rt2x00dev->link.qual.rx_percentage)) / 100;
348 return (signal > 100) ? 100 : signal;
351 static void rt2x00lib_link_tuner(struct work_struct *work)
353 struct rt2x00_dev *rt2x00dev =
354 container_of(work, struct rt2x00_dev, link.work.work);
357 * When the radio is shutting down we should
358 * immediately cease all link tuning.
360 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
366 rt2x00dev->ops->lib->link_stats(rt2x00dev, &rt2x00dev->link.qual);
367 rt2x00dev->low_level_stats.dot11FCSErrorCount +=
368 rt2x00dev->link.qual.rx_failed;
371 * Only perform the link tuning when Link tuning
372 * has been enabled (This could have been disabled from the EEPROM).
374 if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
375 rt2x00dev->ops->lib->link_tuner(rt2x00dev);
378 * Precalculate a portion of the link signal which is
379 * in based on the tx/rx success/failure counters.
381 rt2x00lib_precalculate_link_signal(&rt2x00dev->link.qual);
384 * Evaluate antenna setup, make this the last step since this could
385 * possibly reset some statistics.
387 rt2x00lib_evaluate_antenna(rt2x00dev);
390 * Increase tuner counter, and reschedule the next link tuner run.
392 rt2x00dev->link.count++;
393 queue_delayed_work(rt2x00dev->hw->workqueue, &rt2x00dev->link.work,
397 static void rt2x00lib_packetfilter_scheduled(struct work_struct *work)
399 struct rt2x00_dev *rt2x00dev =
400 container_of(work, struct rt2x00_dev, filter_work);
401 unsigned int filter = rt2x00dev->packet_filter;
404 * Since we had stored the filter inside interface.filter,
405 * we should now clear that field. Otherwise the driver will
406 * assume nothing has changed (*total_flags will be compared
407 * to interface.filter to determine if any action is required).
409 rt2x00dev->packet_filter = 0;
411 rt2x00dev->ops->hw->configure_filter(rt2x00dev->hw,
412 filter, &filter, 0, NULL);
415 static void rt2x00lib_configuration_scheduled(struct work_struct *work)
417 struct rt2x00_dev *rt2x00dev =
418 container_of(work, struct rt2x00_dev, config_work);
419 struct ieee80211_bss_conf bss_conf;
421 bss_conf.use_short_preamble =
422 test_bit(CONFIG_SHORT_PREAMBLE, &rt2x00dev->flags);
425 * FIXME: shouldn't invoke it this way because all other contents
426 * of bss_conf is invalid.
428 rt2x00mac_bss_info_changed(rt2x00dev->hw, rt2x00dev->interface.id,
429 &bss_conf, BSS_CHANGED_ERP_PREAMBLE);
433 * Interrupt context handlers.
435 static void rt2x00lib_beacondone_scheduled(struct work_struct *work)
437 struct rt2x00_dev *rt2x00dev =
438 container_of(work, struct rt2x00_dev, beacon_work);
439 struct ieee80211_tx_control control;
442 skb = ieee80211_beacon_get(rt2x00dev->hw,
443 rt2x00dev->interface.id, &control);
447 rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw, skb, &control);
452 void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
454 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
457 queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->beacon_work);
459 EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
461 void rt2x00lib_txdone(struct queue_entry *entry,
462 struct txdone_entry_desc *txdesc)
464 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
465 struct ieee80211_tx_status tx_status;
466 int success = !!(txdesc->status == TX_SUCCESS ||
467 txdesc->status == TX_SUCCESS_RETRY);
468 int fail = !!(txdesc->status == TX_FAIL_RETRY ||
469 txdesc->status == TX_FAIL_INVALID ||
470 txdesc->status == TX_FAIL_OTHER);
473 * Update TX statistics.
475 rt2x00dev->link.qual.tx_success += success;
476 rt2x00dev->link.qual.tx_failed += txdesc->retry + fail;
479 * Initialize TX status
482 tx_status.ack_signal = 0;
483 tx_status.excessive_retries = (txdesc->status == TX_FAIL_RETRY);
484 tx_status.retry_count = txdesc->retry;
485 memcpy(&tx_status.control, txdesc->control, sizeof(txdesc->control));
487 if (!(tx_status.control.flags & IEEE80211_TXCTL_NO_ACK)) {
489 tx_status.flags |= IEEE80211_TX_STATUS_ACK;
491 rt2x00dev->low_level_stats.dot11ACKFailureCount++;
494 tx_status.queue_length = entry->queue->limit;
495 tx_status.queue_number = tx_status.control.queue;
497 if (tx_status.control.flags & IEEE80211_TXCTL_USE_RTS_CTS) {
499 rt2x00dev->low_level_stats.dot11RTSSuccessCount++;
501 rt2x00dev->low_level_stats.dot11RTSFailureCount++;
505 * Send the tx_status to mac80211 & debugfs.
506 * mac80211 will clean up the skb structure.
508 get_skb_frame_desc(entry->skb)->frame_type = DUMP_FRAME_TXDONE;
509 rt2x00debug_dump_frame(rt2x00dev, entry->skb);
510 ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb, &tx_status);
513 EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
515 void rt2x00lib_rxdone(struct queue_entry *entry,
516 struct rxdone_entry_desc *rxdesc)
518 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
519 struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
520 struct ieee80211_hw_mode *mode;
521 struct ieee80211_rate *rate;
522 struct ieee80211_hdr *hdr;
528 * Update RX statistics.
530 mode = &rt2x00dev->hwmodes[rt2x00dev->curr_hwmode];
531 for (i = 0; i < mode->num_rates; i++) {
532 rate = &mode->rates[i];
535 * When frame was received with an OFDM bitrate,
536 * the signal is the PLCP value. If it was received with
537 * a CCK bitrate the signal is the rate in 0.5kbit/s.
540 val = DEVICE_GET_RATE_FIELD(rate->val, RATE);
542 val = DEVICE_GET_RATE_FIELD(rate->val, PLCP);
544 if (val == rxdesc->signal) {
551 * Only update link status if this is a beacon frame carrying our bssid.
553 hdr = (struct ieee80211_hdr*)entry->skb->data;
554 fc = le16_to_cpu(hdr->frame_control);
555 if (is_beacon(fc) && rxdesc->my_bss)
556 rt2x00lib_update_link_stats(&rt2x00dev->link, rxdesc->rssi);
558 rt2x00dev->link.qual.rx_success++;
560 rx_status->rate = val;
562 rt2x00lib_calculate_link_signal(rt2x00dev, rxdesc->rssi);
563 rx_status->ssi = rxdesc->rssi;
564 rx_status->flag = rxdesc->flags;
565 rx_status->antenna = rt2x00dev->link.ant.active.rx;
568 * Send frame to mac80211 & debugfs.
569 * mac80211 will clean up the skb structure.
571 get_skb_frame_desc(entry->skb)->frame_type = DUMP_FRAME_RXDONE;
572 rt2x00debug_dump_frame(rt2x00dev, entry->skb);
573 ieee80211_rx_irqsafe(rt2x00dev->hw, entry->skb, rx_status);
576 EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
579 * TX descriptor initializer
581 void rt2x00lib_write_tx_desc(struct rt2x00_dev *rt2x00dev,
583 struct ieee80211_tx_control *control)
585 struct txentry_desc txdesc;
586 struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
587 struct ieee80211_hdr *ieee80211hdr = (struct ieee80211_hdr *)skb->data;
596 memset(&txdesc, 0, sizeof(txdesc));
598 txdesc.cw_min = skbdesc->entry->queue->cw_min;
599 txdesc.cw_max = skbdesc->entry->queue->cw_max;
600 txdesc.aifs = skbdesc->entry->queue->aifs;
605 if (control->queue < rt2x00dev->hw->queues)
606 txdesc.queue = control->queue;
607 else if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
608 control->queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
609 txdesc.queue = QID_MGMT;
611 txdesc.queue = QID_OTHER;
614 * Read required fields from ieee80211 header.
616 frame_control = le16_to_cpu(ieee80211hdr->frame_control);
617 seq_ctrl = le16_to_cpu(ieee80211hdr->seq_ctrl);
619 tx_rate = control->tx_rate;
622 * Check whether this frame is to be acked
624 if (!(control->flags & IEEE80211_TXCTL_NO_ACK))
625 __set_bit(ENTRY_TXD_ACK, &txdesc.flags);
628 * Check if this is a RTS/CTS frame
630 if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
631 __set_bit(ENTRY_TXD_BURST, &txdesc.flags);
632 if (is_rts_frame(frame_control)) {
633 __set_bit(ENTRY_TXD_RTS_FRAME, &txdesc.flags);
634 __set_bit(ENTRY_TXD_ACK, &txdesc.flags);
636 __clear_bit(ENTRY_TXD_ACK, &txdesc.flags);
637 if (control->rts_cts_rate)
638 tx_rate = control->rts_cts_rate;
644 if (DEVICE_GET_RATE_FIELD(tx_rate, RATEMASK) & DEV_OFDM_RATEMASK)
645 __set_bit(ENTRY_TXD_OFDM_RATE, &txdesc.flags);
648 * Check if more fragments are pending
650 if (ieee80211_get_morefrag(ieee80211hdr)) {
651 __set_bit(ENTRY_TXD_BURST, &txdesc.flags);
652 __set_bit(ENTRY_TXD_MORE_FRAG, &txdesc.flags);
656 * Beacons and probe responses require the tsf timestamp
657 * to be inserted into the frame.
659 if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
660 is_probe_resp(frame_control))
661 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc.flags);
664 * Determine with what IFS priority this frame should be send.
665 * Set ifs to IFS_SIFS when the this is not the first fragment,
666 * or this fragment came after RTS/CTS.
668 if ((seq_ctrl & IEEE80211_SCTL_FRAG) > 0 ||
669 test_bit(ENTRY_TXD_RTS_FRAME, &txdesc.flags))
670 txdesc.ifs = IFS_SIFS;
672 txdesc.ifs = IFS_BACKOFF;
676 * Length calculation depends on OFDM/CCK rate.
678 txdesc.signal = DEVICE_GET_RATE_FIELD(tx_rate, PLCP);
679 txdesc.service = 0x04;
681 length = skb->len + FCS_LEN;
682 if (test_bit(ENTRY_TXD_OFDM_RATE, &txdesc.flags)) {
683 txdesc.length_high = (length >> 6) & 0x3f;
684 txdesc.length_low = length & 0x3f;
686 bitrate = DEVICE_GET_RATE_FIELD(tx_rate, RATE);
689 * Convert length to microseconds.
691 residual = get_duration_res(length, bitrate);
692 duration = get_duration(length, bitrate);
698 * Check if we need to set the Length Extension
700 if (bitrate == 110 && residual <= 30)
701 txdesc.service |= 0x80;
704 txdesc.length_high = (duration >> 8) & 0xff;
705 txdesc.length_low = duration & 0xff;
708 * When preamble is enabled we should set the
709 * preamble bit for the signal.
711 if (DEVICE_GET_RATE_FIELD(tx_rate, PREAMBLE))
712 txdesc.signal |= 0x08;
715 rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, skb, &txdesc, control);
718 * Update queue entry.
720 skbdesc->entry->skb = skb;
723 * The frame has been completely initialized and ready
724 * for sending to the device. The caller will push the
725 * frame to the device, but we are going to push the
726 * frame to debugfs here.
728 skbdesc->frame_type = DUMP_FRAME_TX;
729 rt2x00debug_dump_frame(rt2x00dev, skb);
731 EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc);
734 * Driver initialization handlers.
736 static void rt2x00lib_channel(struct ieee80211_channel *entry,
737 const int channel, const int tx_power,
740 entry->chan = channel;
742 entry->freq = 2407 + (5 * channel);
744 entry->freq = 5000 + (5 * channel);
747 IEEE80211_CHAN_W_IBSS |
748 IEEE80211_CHAN_W_ACTIVE_SCAN |
749 IEEE80211_CHAN_W_SCAN;
750 entry->power_level = tx_power;
751 entry->antenna_max = 0xff;
754 static void rt2x00lib_rate(struct ieee80211_rate *entry,
755 const int rate, const int mask,
756 const int plcp, const int flags)
760 DEVICE_SET_RATE_FIELD(rate, RATE) |
761 DEVICE_SET_RATE_FIELD(mask, RATEMASK) |
762 DEVICE_SET_RATE_FIELD(plcp, PLCP);
763 entry->flags = flags;
764 entry->val2 = entry->val;
765 if (entry->flags & IEEE80211_RATE_PREAMBLE2)
766 entry->val2 |= DEVICE_SET_RATE_FIELD(1, PREAMBLE);
767 entry->min_rssi_ack = 0;
768 entry->min_rssi_ack_delta = 0;
771 static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
772 struct hw_mode_spec *spec)
774 struct ieee80211_hw *hw = rt2x00dev->hw;
775 struct ieee80211_hw_mode *hwmodes;
776 struct ieee80211_channel *channels;
777 struct ieee80211_rate *rates;
779 unsigned char tx_power;
781 hwmodes = kzalloc(sizeof(*hwmodes) * spec->num_modes, GFP_KERNEL);
785 channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
787 goto exit_free_modes;
789 rates = kzalloc(sizeof(*rates) * spec->num_rates, GFP_KERNEL);
791 goto exit_free_channels;
794 * Initialize Rate list.
796 rt2x00lib_rate(&rates[0], 10, DEV_RATEMASK_1MB,
797 0x00, IEEE80211_RATE_CCK);
798 rt2x00lib_rate(&rates[1], 20, DEV_RATEMASK_2MB,
799 0x01, IEEE80211_RATE_CCK_2);
800 rt2x00lib_rate(&rates[2], 55, DEV_RATEMASK_5_5MB,
801 0x02, IEEE80211_RATE_CCK_2);
802 rt2x00lib_rate(&rates[3], 110, DEV_RATEMASK_11MB,
803 0x03, IEEE80211_RATE_CCK_2);
805 if (spec->num_rates > 4) {
806 rt2x00lib_rate(&rates[4], 60, DEV_RATEMASK_6MB,
807 0x0b, IEEE80211_RATE_OFDM);
808 rt2x00lib_rate(&rates[5], 90, DEV_RATEMASK_9MB,
809 0x0f, IEEE80211_RATE_OFDM);
810 rt2x00lib_rate(&rates[6], 120, DEV_RATEMASK_12MB,
811 0x0a, IEEE80211_RATE_OFDM);
812 rt2x00lib_rate(&rates[7], 180, DEV_RATEMASK_18MB,
813 0x0e, IEEE80211_RATE_OFDM);
814 rt2x00lib_rate(&rates[8], 240, DEV_RATEMASK_24MB,
815 0x09, IEEE80211_RATE_OFDM);
816 rt2x00lib_rate(&rates[9], 360, DEV_RATEMASK_36MB,
817 0x0d, IEEE80211_RATE_OFDM);
818 rt2x00lib_rate(&rates[10], 480, DEV_RATEMASK_48MB,
819 0x08, IEEE80211_RATE_OFDM);
820 rt2x00lib_rate(&rates[11], 540, DEV_RATEMASK_54MB,
821 0x0c, IEEE80211_RATE_OFDM);
825 * Initialize Channel list.
827 for (i = 0; i < spec->num_channels; i++) {
828 if (spec->channels[i].channel <= 14)
829 tx_power = spec->tx_power_bg[i];
830 else if (spec->tx_power_a)
831 tx_power = spec->tx_power_a[i];
833 tx_power = spec->tx_power_default;
835 rt2x00lib_channel(&channels[i],
836 spec->channels[i].channel, tx_power, i);
840 * Intitialize 802.11b
844 if (spec->num_modes > HWMODE_B) {
845 hwmodes[HWMODE_B].mode = MODE_IEEE80211B;
846 hwmodes[HWMODE_B].num_channels = 14;
847 hwmodes[HWMODE_B].num_rates = 4;
848 hwmodes[HWMODE_B].channels = channels;
849 hwmodes[HWMODE_B].rates = rates;
853 * Intitialize 802.11g
857 if (spec->num_modes > HWMODE_G) {
858 hwmodes[HWMODE_G].mode = MODE_IEEE80211G;
859 hwmodes[HWMODE_G].num_channels = 14;
860 hwmodes[HWMODE_G].num_rates = spec->num_rates;
861 hwmodes[HWMODE_G].channels = channels;
862 hwmodes[HWMODE_G].rates = rates;
866 * Intitialize 802.11a
868 * Channels: OFDM, UNII, HiperLAN2.
870 if (spec->num_modes > HWMODE_A) {
871 hwmodes[HWMODE_A].mode = MODE_IEEE80211A;
872 hwmodes[HWMODE_A].num_channels = spec->num_channels - 14;
873 hwmodes[HWMODE_A].num_rates = spec->num_rates - 4;
874 hwmodes[HWMODE_A].channels = &channels[14];
875 hwmodes[HWMODE_A].rates = &rates[4];
878 if (spec->num_modes > HWMODE_G &&
879 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_G]))
880 goto exit_free_rates;
882 if (spec->num_modes > HWMODE_B &&
883 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_B]))
884 goto exit_free_rates;
886 if (spec->num_modes > HWMODE_A &&
887 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_A]))
888 goto exit_free_rates;
890 rt2x00dev->hwmodes = hwmodes;
904 ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
908 static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
910 if (test_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags))
911 ieee80211_unregister_hw(rt2x00dev->hw);
913 if (likely(rt2x00dev->hwmodes)) {
914 kfree(rt2x00dev->hwmodes->channels);
915 kfree(rt2x00dev->hwmodes->rates);
916 kfree(rt2x00dev->hwmodes);
917 rt2x00dev->hwmodes = NULL;
921 static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
923 struct hw_mode_spec *spec = &rt2x00dev->spec;
927 * Initialize HW modes.
929 status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
936 status = ieee80211_register_hw(rt2x00dev->hw);
938 rt2x00lib_remove_hw(rt2x00dev);
942 __set_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags);
948 * Initialization/uninitialization handlers.
950 static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
952 if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
958 rt2x00rfkill_unregister(rt2x00dev);
961 * Allow the HW to uninitialize.
963 rt2x00dev->ops->lib->uninitialize(rt2x00dev);
966 * Free allocated queue entries.
968 rt2x00queue_uninitialize(rt2x00dev);
971 static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
975 if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
979 * Allocate all queue entries.
981 status = rt2x00queue_initialize(rt2x00dev);
986 * Initialize the device.
988 status = rt2x00dev->ops->lib->initialize(rt2x00dev);
992 __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
995 * Register the rfkill handler.
997 status = rt2x00rfkill_register(rt2x00dev);
1004 rt2x00lib_uninitialize(rt2x00dev);
1009 int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
1013 if (test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1017 * If this is the first interface which is added,
1018 * we should load the firmware now.
1020 if (test_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags)) {
1021 retval = rt2x00lib_load_firmware(rt2x00dev);
1027 * Initialize the device.
1029 retval = rt2x00lib_initialize(rt2x00dev);
1036 retval = rt2x00lib_enable_radio(rt2x00dev);
1038 rt2x00lib_uninitialize(rt2x00dev);
1042 __set_bit(DEVICE_STARTED, &rt2x00dev->flags);
1047 void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
1049 if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1053 * Perhaps we can add something smarter here,
1054 * but for now just disabling the radio should do.
1056 rt2x00lib_disable_radio(rt2x00dev);
1058 __clear_bit(DEVICE_STARTED, &rt2x00dev->flags);
1062 * driver allocation handlers.
1064 int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
1066 int retval = -ENOMEM;
1069 * Let the driver probe the device to detect the capabilities.
1071 retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
1073 ERROR(rt2x00dev, "Failed to allocate device.\n");
1078 * Initialize configuration work.
1080 INIT_WORK(&rt2x00dev->beacon_work, rt2x00lib_beacondone_scheduled);
1081 INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
1082 INIT_WORK(&rt2x00dev->config_work, rt2x00lib_configuration_scheduled);
1083 INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
1086 * Reset current working type.
1088 rt2x00dev->interface.type = IEEE80211_IF_TYPE_INVALID;
1091 * Allocate queue array.
1093 retval = rt2x00queue_allocate(rt2x00dev);
1098 * Initialize ieee80211 structure.
1100 retval = rt2x00lib_probe_hw(rt2x00dev);
1102 ERROR(rt2x00dev, "Failed to initialize hw.\n");
1109 retval = rt2x00rfkill_allocate(rt2x00dev);
1114 * Open the debugfs entry.
1116 rt2x00debug_register(rt2x00dev);
1118 __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1123 rt2x00lib_remove_dev(rt2x00dev);
1127 EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
1129 void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
1131 __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1136 rt2x00lib_disable_radio(rt2x00dev);
1139 * Uninitialize device.
1141 rt2x00lib_uninitialize(rt2x00dev);
1144 * Close debugfs entry.
1146 rt2x00debug_deregister(rt2x00dev);
1151 rt2x00rfkill_free(rt2x00dev);
1154 * Free ieee80211_hw memory.
1156 rt2x00lib_remove_hw(rt2x00dev);
1159 * Free firmware image.
1161 rt2x00lib_free_firmware(rt2x00dev);
1164 * Free queue structures.
1166 rt2x00queue_free(rt2x00dev);
1168 EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
1171 * Device state handlers
1174 int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
1178 NOTICE(rt2x00dev, "Going to sleep.\n");
1179 __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1182 * Only continue if mac80211 has open interfaces.
1184 if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1186 __set_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags);
1189 * Disable radio and unitialize all items
1190 * that must be recreated on resume.
1192 rt2x00lib_stop(rt2x00dev);
1193 rt2x00lib_uninitialize(rt2x00dev);
1194 rt2x00debug_deregister(rt2x00dev);
1198 * Set device mode to sleep for power management.
1200 retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
1206 EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1208 int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1210 struct interface *intf = &rt2x00dev->interface;
1213 NOTICE(rt2x00dev, "Waking up.\n");
1216 * Open the debugfs entry.
1218 rt2x00debug_register(rt2x00dev);
1221 * Only continue if mac80211 had open interfaces.
1223 if (!__test_and_clear_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags))
1227 * Reinitialize device and all active interfaces.
1229 retval = rt2x00lib_start(rt2x00dev);
1234 * Reconfigure device.
1236 rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1);
1237 if (!rt2x00dev->hw->conf.radio_enabled)
1238 rt2x00lib_disable_radio(rt2x00dev);
1240 rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
1241 rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
1242 rt2x00lib_config_type(rt2x00dev, intf->type);
1245 * We are ready again to receive requests from mac80211.
1247 __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1250 * It is possible that during that mac80211 has attempted
1251 * to send frames while we were suspending or resuming.
1252 * In that case we have disabled the TX queue and should
1253 * now enable it again
1255 ieee80211_start_queues(rt2x00dev->hw);
1258 * When in Master or Ad-hoc mode,
1259 * restart Beacon transmitting by faking a beacondone event.
1261 if (intf->type == IEEE80211_IF_TYPE_AP ||
1262 intf->type == IEEE80211_IF_TYPE_IBSS)
1263 rt2x00lib_beacondone(rt2x00dev);
1268 rt2x00lib_disable_radio(rt2x00dev);
1269 rt2x00lib_uninitialize(rt2x00dev);
1270 rt2x00debug_deregister(rt2x00dev);
1274 EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1275 #endif /* CONFIG_PM */
1278 * rt2x00lib module information.
1280 MODULE_AUTHOR(DRV_PROJECT);
1281 MODULE_VERSION(DRV_VERSION);
1282 MODULE_DESCRIPTION("rt2x00 library");
1283 MODULE_LICENSE("GPL");