rt2x00: Replace DRV_NAME with KBUILD_MODNAME
[linux-2.6/verdex.git] / drivers / net / wireless / rt2x00 / rt2x00dev.c
blobe8949785c662981a4032d2c735737080a0f7f20b
1 /*
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.
22 Module: rt2x00lib
23 Abstract: rt2x00 generic device routines.
26 #include <linux/kernel.h>
27 #include <linux/module.h>
29 #include "rt2x00.h"
30 #include "rt2x00lib.h"
33 * Ring handler.
35 struct data_ring *rt2x00lib_get_ring(struct rt2x00_dev *rt2x00dev,
36 const unsigned int queue)
38 int beacon = test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags);
41 * Check if we are requesting a reqular TX ring,
42 * or if we are requesting a Beacon or Atim ring.
43 * For Atim rings, we should check if it is supported.
45 if (queue < rt2x00dev->hw->queues && rt2x00dev->tx)
46 return &rt2x00dev->tx[queue];
48 if (!rt2x00dev->bcn || !beacon)
49 return NULL;
51 if (queue == IEEE80211_TX_QUEUE_BEACON)
52 return &rt2x00dev->bcn[0];
53 else if (queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
54 return &rt2x00dev->bcn[1];
56 return NULL;
58 EXPORT_SYMBOL_GPL(rt2x00lib_get_ring);
61 * Link tuning handlers
63 static void rt2x00lib_start_link_tuner(struct rt2x00_dev *rt2x00dev)
65 rt2x00dev->link.count = 0;
66 rt2x00dev->link.vgc_level = 0;
68 memset(&rt2x00dev->link.qual, 0, sizeof(rt2x00dev->link.qual));
71 * The RX and TX percentage should start at 50%
72 * this will assure we will get at least get some
73 * decent value when the link tuner starts.
74 * The value will be dropped and overwritten with
75 * the correct (measured )value anyway during the
76 * first run of the link tuner.
78 rt2x00dev->link.qual.rx_percentage = 50;
79 rt2x00dev->link.qual.tx_percentage = 50;
82 * Reset the link tuner.
84 rt2x00dev->ops->lib->reset_tuner(rt2x00dev);
86 queue_delayed_work(rt2x00dev->hw->workqueue,
87 &rt2x00dev->link.work, LINK_TUNE_INTERVAL);
90 static void rt2x00lib_stop_link_tuner(struct rt2x00_dev *rt2x00dev)
92 cancel_delayed_work_sync(&rt2x00dev->link.work);
95 void rt2x00lib_reset_link_tuner(struct rt2x00_dev *rt2x00dev)
97 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
98 return;
100 rt2x00lib_stop_link_tuner(rt2x00dev);
101 rt2x00lib_start_link_tuner(rt2x00dev);
105 * Radio control handlers.
107 int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
109 int status;
112 * Don't enable the radio twice.
113 * And check if the hardware button has been disabled.
115 if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
116 test_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags))
117 return 0;
120 * Enable radio.
122 status = rt2x00dev->ops->lib->set_device_state(rt2x00dev,
123 STATE_RADIO_ON);
124 if (status)
125 return status;
127 __set_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags);
130 * Enable RX.
132 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
135 * Start the TX queues.
137 ieee80211_start_queues(rt2x00dev->hw);
139 return 0;
142 void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
144 if (!__test_and_clear_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
145 return;
148 * Stop all scheduled work.
150 if (work_pending(&rt2x00dev->beacon_work))
151 cancel_work_sync(&rt2x00dev->beacon_work);
152 if (work_pending(&rt2x00dev->filter_work))
153 cancel_work_sync(&rt2x00dev->filter_work);
154 if (work_pending(&rt2x00dev->config_work))
155 cancel_work_sync(&rt2x00dev->config_work);
158 * Stop the TX queues.
160 ieee80211_stop_queues(rt2x00dev->hw);
163 * Disable RX.
165 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
168 * Disable radio.
170 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
173 void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, enum dev_state state)
176 * When we are disabling the RX, we should also stop the link tuner.
178 if (state == STATE_RADIO_RX_OFF)
179 rt2x00lib_stop_link_tuner(rt2x00dev);
181 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
184 * When we are enabling the RX, we should also start the link tuner.
186 if (state == STATE_RADIO_RX_ON &&
187 is_interface_present(&rt2x00dev->interface))
188 rt2x00lib_start_link_tuner(rt2x00dev);
191 static void rt2x00lib_evaluate_antenna_sample(struct rt2x00_dev *rt2x00dev)
193 enum antenna rx = rt2x00dev->link.ant.active.rx;
194 enum antenna tx = rt2x00dev->link.ant.active.tx;
195 int sample_a =
196 rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_A);
197 int sample_b =
198 rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_B);
201 * We are done sampling. Now we should evaluate the results.
203 rt2x00dev->link.ant.flags &= ~ANTENNA_MODE_SAMPLE;
206 * During the last period we have sampled the RSSI
207 * from both antenna's. It now is time to determine
208 * which antenna demonstrated the best performance.
209 * When we are already on the antenna with the best
210 * performance, then there really is nothing for us
211 * left to do.
213 if (sample_a == sample_b)
214 return;
216 if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY) {
217 if (sample_a > sample_b && rx == ANTENNA_B)
218 rx = ANTENNA_A;
219 else if (rx == ANTENNA_A)
220 rx = ANTENNA_B;
223 if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY) {
224 if (sample_a > sample_b && tx == ANTENNA_B)
225 tx = ANTENNA_A;
226 else if (tx == ANTENNA_A)
227 tx = ANTENNA_B;
230 rt2x00lib_config_antenna(rt2x00dev, rx, tx);
233 static void rt2x00lib_evaluate_antenna_eval(struct rt2x00_dev *rt2x00dev)
235 enum antenna rx = rt2x00dev->link.ant.active.rx;
236 enum antenna tx = rt2x00dev->link.ant.active.tx;
237 int rssi_curr = rt2x00_get_link_ant_rssi(&rt2x00dev->link);
238 int rssi_old = rt2x00_update_ant_rssi(&rt2x00dev->link, rssi_curr);
241 * Legacy driver indicates that we should swap antenna's
242 * when the difference in RSSI is greater that 5. This
243 * also should be done when the RSSI was actually better
244 * then the previous sample.
245 * When the difference exceeds the threshold we should
246 * sample the rssi from the other antenna to make a valid
247 * comparison between the 2 antennas.
249 if ((rssi_curr - rssi_old) > -5 || (rssi_curr - rssi_old) < 5)
250 return;
252 rt2x00dev->link.ant.flags |= ANTENNA_MODE_SAMPLE;
254 if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY)
255 rx = (rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
257 if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)
258 tx = (tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
260 rt2x00lib_config_antenna(rt2x00dev, rx, tx);
263 static void rt2x00lib_evaluate_antenna(struct rt2x00_dev *rt2x00dev)
266 * Determine if software diversity is enabled for
267 * either the TX or RX antenna (or both).
268 * Always perform this check since within the link
269 * tuner interval the configuration might have changed.
271 rt2x00dev->link.ant.flags &= ~ANTENNA_RX_DIVERSITY;
272 rt2x00dev->link.ant.flags &= ~ANTENNA_TX_DIVERSITY;
274 if (rt2x00dev->hw->conf.antenna_sel_rx == 0 &&
275 rt2x00dev->default_ant.rx != ANTENNA_SW_DIVERSITY)
276 rt2x00dev->link.ant.flags |= ANTENNA_RX_DIVERSITY;
277 if (rt2x00dev->hw->conf.antenna_sel_tx == 0 &&
278 rt2x00dev->default_ant.tx != ANTENNA_SW_DIVERSITY)
279 rt2x00dev->link.ant.flags |= ANTENNA_TX_DIVERSITY;
281 if (!(rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY) &&
282 !(rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)) {
283 rt2x00dev->link.ant.flags &= ~ANTENNA_MODE_SAMPLE;
284 return;
288 * If we have only sampled the data over the last period
289 * we should now harvest the data. Otherwise just evaluate
290 * the data. The latter should only be performed once
291 * every 2 seconds.
293 if (rt2x00dev->link.ant.flags & ANTENNA_MODE_SAMPLE)
294 rt2x00lib_evaluate_antenna_sample(rt2x00dev);
295 else if (rt2x00dev->link.count & 1)
296 rt2x00lib_evaluate_antenna_eval(rt2x00dev);
299 static void rt2x00lib_update_link_stats(struct link *link, int rssi)
301 int avg_rssi = rssi;
304 * Update global RSSI
306 if (link->qual.avg_rssi)
307 avg_rssi = MOVING_AVERAGE(link->qual.avg_rssi, rssi, 8);
308 link->qual.avg_rssi = avg_rssi;
311 * Update antenna RSSI
313 if (link->ant.rssi_ant)
314 rssi = MOVING_AVERAGE(link->ant.rssi_ant, rssi, 8);
315 link->ant.rssi_ant = rssi;
318 static void rt2x00lib_precalculate_link_signal(struct link_qual *qual)
320 if (qual->rx_failed || qual->rx_success)
321 qual->rx_percentage =
322 (qual->rx_success * 100) /
323 (qual->rx_failed + qual->rx_success);
324 else
325 qual->rx_percentage = 50;
327 if (qual->tx_failed || qual->tx_success)
328 qual->tx_percentage =
329 (qual->tx_success * 100) /
330 (qual->tx_failed + qual->tx_success);
331 else
332 qual->tx_percentage = 50;
334 qual->rx_success = 0;
335 qual->rx_failed = 0;
336 qual->tx_success = 0;
337 qual->tx_failed = 0;
340 static int rt2x00lib_calculate_link_signal(struct rt2x00_dev *rt2x00dev,
341 int rssi)
343 int rssi_percentage = 0;
344 int signal;
347 * We need a positive value for the RSSI.
349 if (rssi < 0)
350 rssi += rt2x00dev->rssi_offset;
353 * Calculate the different percentages,
354 * which will be used for the signal.
356 if (rt2x00dev->rssi_offset)
357 rssi_percentage = (rssi * 100) / rt2x00dev->rssi_offset;
360 * Add the individual percentages and use the WEIGHT
361 * defines to calculate the current link signal.
363 signal = ((WEIGHT_RSSI * rssi_percentage) +
364 (WEIGHT_TX * rt2x00dev->link.qual.tx_percentage) +
365 (WEIGHT_RX * rt2x00dev->link.qual.rx_percentage)) / 100;
367 return (signal > 100) ? 100 : signal;
370 static void rt2x00lib_link_tuner(struct work_struct *work)
372 struct rt2x00_dev *rt2x00dev =
373 container_of(work, struct rt2x00_dev, link.work.work);
376 * When the radio is shutting down we should
377 * immediately cease all link tuning.
379 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
380 return;
383 * Update statistics.
385 rt2x00dev->ops->lib->link_stats(rt2x00dev, &rt2x00dev->link.qual);
386 rt2x00dev->low_level_stats.dot11FCSErrorCount +=
387 rt2x00dev->link.qual.rx_failed;
390 * Only perform the link tuning when Link tuning
391 * has been enabled (This could have been disabled from the EEPROM).
393 if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
394 rt2x00dev->ops->lib->link_tuner(rt2x00dev);
397 * Evaluate antenna setup.
399 rt2x00lib_evaluate_antenna(rt2x00dev);
402 * Precalculate a portion of the link signal which is
403 * in based on the tx/rx success/failure counters.
405 rt2x00lib_precalculate_link_signal(&rt2x00dev->link.qual);
408 * Increase tuner counter, and reschedule the next link tuner run.
410 rt2x00dev->link.count++;
411 queue_delayed_work(rt2x00dev->hw->workqueue, &rt2x00dev->link.work,
412 LINK_TUNE_INTERVAL);
415 static void rt2x00lib_packetfilter_scheduled(struct work_struct *work)
417 struct rt2x00_dev *rt2x00dev =
418 container_of(work, struct rt2x00_dev, filter_work);
419 unsigned int filter = rt2x00dev->interface.filter;
422 * Since we had stored the filter inside interface.filter,
423 * we should now clear that field. Otherwise the driver will
424 * assume nothing has changed (*total_flags will be compared
425 * to interface.filter to determine if any action is required).
427 rt2x00dev->interface.filter = 0;
429 rt2x00dev->ops->hw->configure_filter(rt2x00dev->hw,
430 filter, &filter, 0, NULL);
433 static void rt2x00lib_configuration_scheduled(struct work_struct *work)
435 struct rt2x00_dev *rt2x00dev =
436 container_of(work, struct rt2x00_dev, config_work);
437 int preamble = !test_bit(CONFIG_SHORT_PREAMBLE, &rt2x00dev->flags);
439 rt2x00mac_erp_ie_changed(rt2x00dev->hw,
440 IEEE80211_ERP_CHANGE_PREAMBLE, 0, preamble);
444 * Interrupt context handlers.
446 static void rt2x00lib_beacondone_scheduled(struct work_struct *work)
448 struct rt2x00_dev *rt2x00dev =
449 container_of(work, struct rt2x00_dev, beacon_work);
450 struct data_ring *ring =
451 rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_BEACON);
452 struct data_entry *entry = rt2x00_get_data_entry(ring);
453 struct sk_buff *skb;
455 skb = ieee80211_beacon_get(rt2x00dev->hw,
456 rt2x00dev->interface.id,
457 &entry->tx_status.control);
458 if (!skb)
459 return;
461 rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw, skb,
462 &entry->tx_status.control);
464 dev_kfree_skb(skb);
467 void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
469 if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
470 return;
472 queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->beacon_work);
474 EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
476 void rt2x00lib_txdone(struct data_entry *entry,
477 const int status, const int retry)
479 struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
480 struct ieee80211_tx_status *tx_status = &entry->tx_status;
481 struct ieee80211_low_level_stats *stats = &rt2x00dev->low_level_stats;
482 int success = !!(status == TX_SUCCESS || status == TX_SUCCESS_RETRY);
483 int fail = !!(status == TX_FAIL_RETRY || status == TX_FAIL_INVALID ||
484 status == TX_FAIL_OTHER);
487 * Update TX statistics.
489 tx_status->flags = 0;
490 tx_status->ack_signal = 0;
491 tx_status->excessive_retries = (status == TX_FAIL_RETRY);
492 tx_status->retry_count = retry;
493 rt2x00dev->link.qual.tx_success += success;
494 rt2x00dev->link.qual.tx_failed += retry + fail;
496 if (!(tx_status->control.flags & IEEE80211_TXCTL_NO_ACK)) {
497 if (success)
498 tx_status->flags |= IEEE80211_TX_STATUS_ACK;
499 else
500 stats->dot11ACKFailureCount++;
503 tx_status->queue_length = entry->ring->stats.limit;
504 tx_status->queue_number = tx_status->control.queue;
506 if (tx_status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) {
507 if (success)
508 stats->dot11RTSSuccessCount++;
509 else
510 stats->dot11RTSFailureCount++;
514 * Send the tx_status to mac80211,
515 * that method also cleans up the skb structure.
517 ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb, tx_status);
518 entry->skb = NULL;
520 EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
522 void rt2x00lib_rxdone(struct data_entry *entry, struct sk_buff *skb,
523 struct rxdata_entry_desc *desc)
525 struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
526 struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
527 struct ieee80211_hw_mode *mode;
528 struct ieee80211_rate *rate;
529 unsigned int i;
530 int val = 0;
533 * Update RX statistics.
535 mode = &rt2x00dev->hwmodes[rt2x00dev->curr_hwmode];
536 for (i = 0; i < mode->num_rates; i++) {
537 rate = &mode->rates[i];
540 * When frame was received with an OFDM bitrate,
541 * the signal is the PLCP value. If it was received with
542 * a CCK bitrate the signal is the rate in 0.5kbit/s.
544 if (!desc->ofdm)
545 val = DEVICE_GET_RATE_FIELD(rate->val, RATE);
546 else
547 val = DEVICE_GET_RATE_FIELD(rate->val, PLCP);
549 if (val == desc->signal) {
550 val = rate->val;
551 break;
555 rt2x00lib_update_link_stats(&rt2x00dev->link, desc->rssi);
556 rt2x00dev->link.qual.rx_success++;
558 rx_status->rate = val;
559 rx_status->signal =
560 rt2x00lib_calculate_link_signal(rt2x00dev, desc->rssi);
561 rx_status->ssi = desc->rssi;
562 rx_status->flag = desc->flags;
563 rx_status->antenna = rt2x00dev->link.ant.active.rx;
566 * Send frame to mac80211
568 ieee80211_rx_irqsafe(rt2x00dev->hw, skb, rx_status);
570 EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
573 * TX descriptor initializer
575 void rt2x00lib_write_tx_desc(struct rt2x00_dev *rt2x00dev,
576 __le32 *txd,
577 struct ieee80211_hdr *ieee80211hdr,
578 unsigned int length,
579 struct ieee80211_tx_control *control)
581 struct txdata_entry_desc desc;
582 struct data_ring *ring;
583 int tx_rate;
584 int bitrate;
585 int duration;
586 int residual;
587 u16 frame_control;
588 u16 seq_ctrl;
591 * Make sure the descriptor is properly cleared.
593 memset(&desc, 0x00, sizeof(desc));
596 * Get ring pointer, if we fail to obtain the
597 * correct ring, then use the first TX ring.
599 ring = rt2x00lib_get_ring(rt2x00dev, control->queue);
600 if (!ring)
601 ring = rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_DATA0);
603 desc.cw_min = ring->tx_params.cw_min;
604 desc.cw_max = ring->tx_params.cw_max;
605 desc.aifs = ring->tx_params.aifs;
608 * Identify queue
610 if (control->queue < rt2x00dev->hw->queues)
611 desc.queue = control->queue;
612 else if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
613 control->queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
614 desc.queue = QUEUE_MGMT;
615 else
616 desc.queue = QUEUE_OTHER;
619 * Read required fields from ieee80211 header.
621 frame_control = le16_to_cpu(ieee80211hdr->frame_control);
622 seq_ctrl = le16_to_cpu(ieee80211hdr->seq_ctrl);
624 tx_rate = control->tx_rate;
627 * Check whether this frame is to be acked
629 if (!(control->flags & IEEE80211_TXCTL_NO_ACK))
630 __set_bit(ENTRY_TXD_ACK, &desc.flags);
633 * Check if this is a RTS/CTS frame
635 if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
636 __set_bit(ENTRY_TXD_BURST, &desc.flags);
637 if (is_rts_frame(frame_control)) {
638 __set_bit(ENTRY_TXD_RTS_FRAME, &desc.flags);
639 __set_bit(ENTRY_TXD_ACK, &desc.flags);
640 } else
641 __clear_bit(ENTRY_TXD_ACK, &desc.flags);
642 if (control->rts_cts_rate)
643 tx_rate = control->rts_cts_rate;
647 * Check for OFDM
649 if (DEVICE_GET_RATE_FIELD(tx_rate, RATEMASK) & DEV_OFDM_RATEMASK)
650 __set_bit(ENTRY_TXD_OFDM_RATE, &desc.flags);
653 * Check if more fragments are pending
655 if (ieee80211_get_morefrag(ieee80211hdr)) {
656 __set_bit(ENTRY_TXD_BURST, &desc.flags);
657 __set_bit(ENTRY_TXD_MORE_FRAG, &desc.flags);
661 * Beacons and probe responses require the tsf timestamp
662 * to be inserted into the frame.
664 if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
665 is_probe_resp(frame_control))
666 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &desc.flags);
669 * Determine with what IFS priority this frame should be send.
670 * Set ifs to IFS_SIFS when the this is not the first fragment,
671 * or this fragment came after RTS/CTS.
673 if ((seq_ctrl & IEEE80211_SCTL_FRAG) > 0 ||
674 test_bit(ENTRY_TXD_RTS_FRAME, &desc.flags))
675 desc.ifs = IFS_SIFS;
676 else
677 desc.ifs = IFS_BACKOFF;
680 * PLCP setup
681 * Length calculation depends on OFDM/CCK rate.
683 desc.signal = DEVICE_GET_RATE_FIELD(tx_rate, PLCP);
684 desc.service = 0x04;
686 if (test_bit(ENTRY_TXD_OFDM_RATE, &desc.flags)) {
687 desc.length_high = ((length + FCS_LEN) >> 6) & 0x3f;
688 desc.length_low = ((length + FCS_LEN) & 0x3f);
689 } else {
690 bitrate = DEVICE_GET_RATE_FIELD(tx_rate, RATE);
693 * Convert length to microseconds.
695 residual = get_duration_res(length + FCS_LEN, bitrate);
696 duration = get_duration(length + FCS_LEN, bitrate);
698 if (residual != 0) {
699 duration++;
702 * Check if we need to set the Length Extension
704 if (bitrate == 110 && residual <= 30)
705 desc.service |= 0x80;
708 desc.length_high = (duration >> 8) & 0xff;
709 desc.length_low = duration & 0xff;
712 * When preamble is enabled we should set the
713 * preamble bit for the signal.
715 if (DEVICE_GET_RATE_FIELD(tx_rate, PREAMBLE))
716 desc.signal |= 0x08;
719 rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, txd, &desc,
720 ieee80211hdr, length, control);
722 EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc);
725 * Driver initialization handlers.
727 static void rt2x00lib_channel(struct ieee80211_channel *entry,
728 const int channel, const int tx_power,
729 const int value)
731 entry->chan = channel;
732 if (channel <= 14)
733 entry->freq = 2407 + (5 * channel);
734 else
735 entry->freq = 5000 + (5 * channel);
736 entry->val = value;
737 entry->flag =
738 IEEE80211_CHAN_W_IBSS |
739 IEEE80211_CHAN_W_ACTIVE_SCAN |
740 IEEE80211_CHAN_W_SCAN;
741 entry->power_level = tx_power;
742 entry->antenna_max = 0xff;
745 static void rt2x00lib_rate(struct ieee80211_rate *entry,
746 const int rate, const int mask,
747 const int plcp, const int flags)
749 entry->rate = rate;
750 entry->val =
751 DEVICE_SET_RATE_FIELD(rate, RATE) |
752 DEVICE_SET_RATE_FIELD(mask, RATEMASK) |
753 DEVICE_SET_RATE_FIELD(plcp, PLCP);
754 entry->flags = flags;
755 entry->val2 = entry->val;
756 if (entry->flags & IEEE80211_RATE_PREAMBLE2)
757 entry->val2 |= DEVICE_SET_RATE_FIELD(1, PREAMBLE);
758 entry->min_rssi_ack = 0;
759 entry->min_rssi_ack_delta = 0;
762 static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
763 struct hw_mode_spec *spec)
765 struct ieee80211_hw *hw = rt2x00dev->hw;
766 struct ieee80211_hw_mode *hwmodes;
767 struct ieee80211_channel *channels;
768 struct ieee80211_rate *rates;
769 unsigned int i;
770 unsigned char tx_power;
772 hwmodes = kzalloc(sizeof(*hwmodes) * spec->num_modes, GFP_KERNEL);
773 if (!hwmodes)
774 goto exit;
776 channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
777 if (!channels)
778 goto exit_free_modes;
780 rates = kzalloc(sizeof(*rates) * spec->num_rates, GFP_KERNEL);
781 if (!rates)
782 goto exit_free_channels;
785 * Initialize Rate list.
787 rt2x00lib_rate(&rates[0], 10, DEV_RATEMASK_1MB,
788 0x00, IEEE80211_RATE_CCK);
789 rt2x00lib_rate(&rates[1], 20, DEV_RATEMASK_2MB,
790 0x01, IEEE80211_RATE_CCK_2);
791 rt2x00lib_rate(&rates[2], 55, DEV_RATEMASK_5_5MB,
792 0x02, IEEE80211_RATE_CCK_2);
793 rt2x00lib_rate(&rates[3], 110, DEV_RATEMASK_11MB,
794 0x03, IEEE80211_RATE_CCK_2);
796 if (spec->num_rates > 4) {
797 rt2x00lib_rate(&rates[4], 60, DEV_RATEMASK_6MB,
798 0x0b, IEEE80211_RATE_OFDM);
799 rt2x00lib_rate(&rates[5], 90, DEV_RATEMASK_9MB,
800 0x0f, IEEE80211_RATE_OFDM);
801 rt2x00lib_rate(&rates[6], 120, DEV_RATEMASK_12MB,
802 0x0a, IEEE80211_RATE_OFDM);
803 rt2x00lib_rate(&rates[7], 180, DEV_RATEMASK_18MB,
804 0x0e, IEEE80211_RATE_OFDM);
805 rt2x00lib_rate(&rates[8], 240, DEV_RATEMASK_24MB,
806 0x09, IEEE80211_RATE_OFDM);
807 rt2x00lib_rate(&rates[9], 360, DEV_RATEMASK_36MB,
808 0x0d, IEEE80211_RATE_OFDM);
809 rt2x00lib_rate(&rates[10], 480, DEV_RATEMASK_48MB,
810 0x08, IEEE80211_RATE_OFDM);
811 rt2x00lib_rate(&rates[11], 540, DEV_RATEMASK_54MB,
812 0x0c, IEEE80211_RATE_OFDM);
816 * Initialize Channel list.
818 for (i = 0; i < spec->num_channels; i++) {
819 if (spec->channels[i].channel <= 14)
820 tx_power = spec->tx_power_bg[i];
821 else if (spec->tx_power_a)
822 tx_power = spec->tx_power_a[i];
823 else
824 tx_power = spec->tx_power_default;
826 rt2x00lib_channel(&channels[i],
827 spec->channels[i].channel, tx_power, i);
831 * Intitialize 802.11b
832 * Rates: CCK.
833 * Channels: OFDM.
835 if (spec->num_modes > HWMODE_B) {
836 hwmodes[HWMODE_B].mode = MODE_IEEE80211B;
837 hwmodes[HWMODE_B].num_channels = 14;
838 hwmodes[HWMODE_B].num_rates = 4;
839 hwmodes[HWMODE_B].channels = channels;
840 hwmodes[HWMODE_B].rates = rates;
844 * Intitialize 802.11g
845 * Rates: CCK, OFDM.
846 * Channels: OFDM.
848 if (spec->num_modes > HWMODE_G) {
849 hwmodes[HWMODE_G].mode = MODE_IEEE80211G;
850 hwmodes[HWMODE_G].num_channels = 14;
851 hwmodes[HWMODE_G].num_rates = spec->num_rates;
852 hwmodes[HWMODE_G].channels = channels;
853 hwmodes[HWMODE_G].rates = rates;
857 * Intitialize 802.11a
858 * Rates: OFDM.
859 * Channels: OFDM, UNII, HiperLAN2.
861 if (spec->num_modes > HWMODE_A) {
862 hwmodes[HWMODE_A].mode = MODE_IEEE80211A;
863 hwmodes[HWMODE_A].num_channels = spec->num_channels - 14;
864 hwmodes[HWMODE_A].num_rates = spec->num_rates - 4;
865 hwmodes[HWMODE_A].channels = &channels[14];
866 hwmodes[HWMODE_A].rates = &rates[4];
869 if (spec->num_modes > HWMODE_G &&
870 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_G]))
871 goto exit_free_rates;
873 if (spec->num_modes > HWMODE_B &&
874 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_B]))
875 goto exit_free_rates;
877 if (spec->num_modes > HWMODE_A &&
878 ieee80211_register_hwmode(hw, &hwmodes[HWMODE_A]))
879 goto exit_free_rates;
881 rt2x00dev->hwmodes = hwmodes;
883 return 0;
885 exit_free_rates:
886 kfree(rates);
888 exit_free_channels:
889 kfree(channels);
891 exit_free_modes:
892 kfree(hwmodes);
894 exit:
895 ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
896 return -ENOMEM;
899 static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
901 if (test_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags))
902 ieee80211_unregister_hw(rt2x00dev->hw);
904 if (likely(rt2x00dev->hwmodes)) {
905 kfree(rt2x00dev->hwmodes->channels);
906 kfree(rt2x00dev->hwmodes->rates);
907 kfree(rt2x00dev->hwmodes);
908 rt2x00dev->hwmodes = NULL;
912 static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
914 struct hw_mode_spec *spec = &rt2x00dev->spec;
915 int status;
918 * Initialize HW modes.
920 status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
921 if (status)
922 return status;
925 * Register HW.
927 status = ieee80211_register_hw(rt2x00dev->hw);
928 if (status) {
929 rt2x00lib_remove_hw(rt2x00dev);
930 return status;
933 __set_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags);
935 return 0;
939 * Initialization/uninitialization handlers.
941 static int rt2x00lib_alloc_entries(struct data_ring *ring,
942 const u16 max_entries, const u16 data_size,
943 const u16 desc_size)
945 struct data_entry *entry;
946 unsigned int i;
948 ring->stats.limit = max_entries;
949 ring->data_size = data_size;
950 ring->desc_size = desc_size;
953 * Allocate all ring entries.
955 entry = kzalloc(ring->stats.limit * sizeof(*entry), GFP_KERNEL);
956 if (!entry)
957 return -ENOMEM;
959 for (i = 0; i < ring->stats.limit; i++) {
960 entry[i].flags = 0;
961 entry[i].ring = ring;
962 entry[i].skb = NULL;
965 ring->entry = entry;
967 return 0;
970 static int rt2x00lib_alloc_ring_entries(struct rt2x00_dev *rt2x00dev)
972 struct data_ring *ring;
975 * Allocate the RX ring.
977 if (rt2x00lib_alloc_entries(rt2x00dev->rx, RX_ENTRIES, DATA_FRAME_SIZE,
978 rt2x00dev->ops->rxd_size))
979 return -ENOMEM;
982 * First allocate the TX rings.
984 txring_for_each(rt2x00dev, ring) {
985 if (rt2x00lib_alloc_entries(ring, TX_ENTRIES, DATA_FRAME_SIZE,
986 rt2x00dev->ops->txd_size))
987 return -ENOMEM;
990 if (!test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
991 return 0;
994 * Allocate the BEACON ring.
996 if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[0], BEACON_ENTRIES,
997 MGMT_FRAME_SIZE, rt2x00dev->ops->txd_size))
998 return -ENOMEM;
1001 * Allocate the Atim ring.
1003 if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[1], ATIM_ENTRIES,
1004 DATA_FRAME_SIZE, rt2x00dev->ops->txd_size))
1005 return -ENOMEM;
1007 return 0;
1010 static void rt2x00lib_free_ring_entries(struct rt2x00_dev *rt2x00dev)
1012 struct data_ring *ring;
1014 ring_for_each(rt2x00dev, ring) {
1015 kfree(ring->entry);
1016 ring->entry = NULL;
1020 void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
1022 if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
1023 return;
1026 * Unregister rfkill.
1028 rt2x00rfkill_unregister(rt2x00dev);
1031 * Allow the HW to uninitialize.
1033 rt2x00dev->ops->lib->uninitialize(rt2x00dev);
1036 * Free allocated ring entries.
1038 rt2x00lib_free_ring_entries(rt2x00dev);
1041 int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
1043 int status;
1045 if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
1046 return 0;
1049 * Allocate all ring entries.
1051 status = rt2x00lib_alloc_ring_entries(rt2x00dev);
1052 if (status) {
1053 ERROR(rt2x00dev, "Ring entries allocation failed.\n");
1054 return status;
1058 * Initialize the device.
1060 status = rt2x00dev->ops->lib->initialize(rt2x00dev);
1061 if (status)
1062 goto exit;
1064 __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
1067 * Register the rfkill handler.
1069 status = rt2x00rfkill_register(rt2x00dev);
1070 if (status)
1071 goto exit_unitialize;
1073 return 0;
1075 exit_unitialize:
1076 rt2x00lib_uninitialize(rt2x00dev);
1078 exit:
1079 rt2x00lib_free_ring_entries(rt2x00dev);
1081 return status;
1085 * driver allocation handlers.
1087 static int rt2x00lib_alloc_rings(struct rt2x00_dev *rt2x00dev)
1089 struct data_ring *ring;
1092 * We need the following rings:
1093 * RX: 1
1094 * TX: hw->queues
1095 * Beacon: 1 (if required)
1096 * Atim: 1 (if required)
1098 rt2x00dev->data_rings = 1 + rt2x00dev->hw->queues +
1099 (2 * test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags));
1101 ring = kzalloc(rt2x00dev->data_rings * sizeof(*ring), GFP_KERNEL);
1102 if (!ring) {
1103 ERROR(rt2x00dev, "Ring allocation failed.\n");
1104 return -ENOMEM;
1108 * Initialize pointers
1110 rt2x00dev->rx = ring;
1111 rt2x00dev->tx = &rt2x00dev->rx[1];
1112 if (test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
1113 rt2x00dev->bcn = &rt2x00dev->tx[rt2x00dev->hw->queues];
1116 * Initialize ring parameters.
1117 * cw_min: 2^5 = 32.
1118 * cw_max: 2^10 = 1024.
1120 ring_for_each(rt2x00dev, ring) {
1121 ring->rt2x00dev = rt2x00dev;
1122 ring->tx_params.aifs = 2;
1123 ring->tx_params.cw_min = 5;
1124 ring->tx_params.cw_max = 10;
1127 return 0;
1130 static void rt2x00lib_free_rings(struct rt2x00_dev *rt2x00dev)
1132 kfree(rt2x00dev->rx);
1133 rt2x00dev->rx = NULL;
1134 rt2x00dev->tx = NULL;
1135 rt2x00dev->bcn = NULL;
1138 int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
1140 int retval = -ENOMEM;
1143 * Let the driver probe the device to detect the capabilities.
1145 retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
1146 if (retval) {
1147 ERROR(rt2x00dev, "Failed to allocate device.\n");
1148 goto exit;
1152 * Initialize configuration work.
1154 INIT_WORK(&rt2x00dev->beacon_work, rt2x00lib_beacondone_scheduled);
1155 INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
1156 INIT_WORK(&rt2x00dev->config_work, rt2x00lib_configuration_scheduled);
1157 INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
1160 * Reset current working type.
1162 rt2x00dev->interface.type = INVALID_INTERFACE;
1165 * Allocate ring array.
1167 retval = rt2x00lib_alloc_rings(rt2x00dev);
1168 if (retval)
1169 goto exit;
1172 * Initialize ieee80211 structure.
1174 retval = rt2x00lib_probe_hw(rt2x00dev);
1175 if (retval) {
1176 ERROR(rt2x00dev, "Failed to initialize hw.\n");
1177 goto exit;
1181 * Allocatie rfkill.
1183 retval = rt2x00rfkill_allocate(rt2x00dev);
1184 if (retval)
1185 goto exit;
1188 * Open the debugfs entry.
1190 rt2x00debug_register(rt2x00dev);
1192 __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1194 return 0;
1196 exit:
1197 rt2x00lib_remove_dev(rt2x00dev);
1199 return retval;
1201 EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
1203 void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
1205 __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1208 * Disable radio.
1210 rt2x00lib_disable_radio(rt2x00dev);
1213 * Uninitialize device.
1215 rt2x00lib_uninitialize(rt2x00dev);
1218 * Close debugfs entry.
1220 rt2x00debug_deregister(rt2x00dev);
1223 * Free rfkill
1225 rt2x00rfkill_free(rt2x00dev);
1228 * Free ieee80211_hw memory.
1230 rt2x00lib_remove_hw(rt2x00dev);
1233 * Free firmware image.
1235 rt2x00lib_free_firmware(rt2x00dev);
1238 * Free ring structures.
1240 rt2x00lib_free_rings(rt2x00dev);
1242 EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
1245 * Device state handlers
1247 #ifdef CONFIG_PM
1248 int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
1250 int retval;
1252 NOTICE(rt2x00dev, "Going to sleep.\n");
1253 __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1256 * Only continue if mac80211 has open interfaces.
1258 if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1259 goto exit;
1260 __set_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags);
1263 * Disable radio and unitialize all items
1264 * that must be recreated on resume.
1266 rt2x00mac_stop(rt2x00dev->hw);
1267 rt2x00lib_uninitialize(rt2x00dev);
1268 rt2x00debug_deregister(rt2x00dev);
1270 exit:
1272 * Set device mode to sleep for power management.
1274 retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
1275 if (retval)
1276 return retval;
1278 return 0;
1280 EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1282 int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1284 struct interface *intf = &rt2x00dev->interface;
1285 int retval;
1287 NOTICE(rt2x00dev, "Waking up.\n");
1288 __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1291 * Open the debugfs entry.
1293 rt2x00debug_register(rt2x00dev);
1296 * Only continue if mac80211 had open interfaces.
1298 if (!__test_and_clear_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags))
1299 return 0;
1302 * Reinitialize device and all active interfaces.
1304 retval = rt2x00mac_start(rt2x00dev->hw);
1305 if (retval)
1306 goto exit;
1309 * Reconfigure device.
1311 rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1);
1312 if (!rt2x00dev->hw->conf.radio_enabled)
1313 rt2x00lib_disable_radio(rt2x00dev);
1315 rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
1316 rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
1317 rt2x00lib_config_type(rt2x00dev, intf->type);
1320 * It is possible that during that mac80211 has attempted
1321 * to send frames while we were suspending or resuming.
1322 * In that case we have disabled the TX queue and should
1323 * now enable it again
1325 ieee80211_start_queues(rt2x00dev->hw);
1328 * When in Master or Ad-hoc mode,
1329 * restart Beacon transmitting by faking a beacondone event.
1331 if (intf->type == IEEE80211_IF_TYPE_AP ||
1332 intf->type == IEEE80211_IF_TYPE_IBSS)
1333 rt2x00lib_beacondone(rt2x00dev);
1335 return 0;
1337 exit:
1338 rt2x00lib_disable_radio(rt2x00dev);
1339 rt2x00lib_uninitialize(rt2x00dev);
1340 rt2x00debug_deregister(rt2x00dev);
1342 return retval;
1344 EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1345 #endif /* CONFIG_PM */
1348 * rt2x00lib module information.
1350 MODULE_AUTHOR(DRV_PROJECT);
1351 MODULE_VERSION(DRV_VERSION);
1352 MODULE_DESCRIPTION("rt2x00 library");
1353 MODULE_LICENSE("GPL");