rt2x00: Add TX/RX frame dumping facility
[safe/jmp/linux-2.6] / drivers / net / wireless / rt2x00 / rt2x00dev.c
1 /*
2         Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
3         <http://rt2x00.serialmonkey.com>
4
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.
9
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.
14
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.
19  */
20
21 /*
22         Module: rt2x00lib
23         Abstract: rt2x00 generic device routines.
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28
29 #include "rt2x00.h"
30 #include "rt2x00lib.h"
31 #include "rt2x00dump.h"
32
33 /*
34  * Ring handler.
35  */
36 struct data_ring *rt2x00lib_get_ring(struct rt2x00_dev *rt2x00dev,
37                                      const unsigned int queue)
38 {
39         int beacon = test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags);
40
41         /*
42          * Check if we are requesting a reqular TX ring,
43          * or if we are requesting a Beacon or Atim ring.
44          * For Atim rings, we should check if it is supported.
45          */
46         if (queue < rt2x00dev->hw->queues && rt2x00dev->tx)
47                 return &rt2x00dev->tx[queue];
48
49         if (!rt2x00dev->bcn || !beacon)
50                 return NULL;
51
52         if (queue == IEEE80211_TX_QUEUE_BEACON)
53                 return &rt2x00dev->bcn[0];
54         else if (queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
55                 return &rt2x00dev->bcn[1];
56
57         return NULL;
58 }
59 EXPORT_SYMBOL_GPL(rt2x00lib_get_ring);
60
61 /*
62  * Link tuning handlers
63  */
64 static void rt2x00lib_start_link_tuner(struct rt2x00_dev *rt2x00dev)
65 {
66         rt2x00dev->link.count = 0;
67         rt2x00dev->link.vgc_level = 0;
68
69         memset(&rt2x00dev->link.qual, 0, sizeof(rt2x00dev->link.qual));
70
71         /*
72          * The RX and TX percentage should start at 50%
73          * this will assure we will get at least get some
74          * decent value when the link tuner starts.
75          * The value will be dropped and overwritten with
76          * the correct (measured )value anyway during the
77          * first run of the link tuner.
78          */
79         rt2x00dev->link.qual.rx_percentage = 50;
80         rt2x00dev->link.qual.tx_percentage = 50;
81
82         /*
83          * Reset the link tuner.
84          */
85         rt2x00dev->ops->lib->reset_tuner(rt2x00dev);
86
87         queue_delayed_work(rt2x00dev->hw->workqueue,
88                            &rt2x00dev->link.work, LINK_TUNE_INTERVAL);
89 }
90
91 static void rt2x00lib_stop_link_tuner(struct rt2x00_dev *rt2x00dev)
92 {
93         cancel_delayed_work_sync(&rt2x00dev->link.work);
94 }
95
96 void rt2x00lib_reset_link_tuner(struct rt2x00_dev *rt2x00dev)
97 {
98         if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
99                 return;
100
101         rt2x00lib_stop_link_tuner(rt2x00dev);
102         rt2x00lib_start_link_tuner(rt2x00dev);
103 }
104
105 /*
106  * Radio control handlers.
107  */
108 int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
109 {
110         int status;
111
112         /*
113          * Don't enable the radio twice.
114          * And check if the hardware button has been disabled.
115          */
116         if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
117             test_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags))
118                 return 0;
119
120         /*
121          * Enable radio.
122          */
123         status = rt2x00dev->ops->lib->set_device_state(rt2x00dev,
124                                                        STATE_RADIO_ON);
125         if (status)
126                 return status;
127
128         __set_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags);
129
130         /*
131          * Enable RX.
132          */
133         rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
134
135         /*
136          * Start the TX queues.
137          */
138         ieee80211_start_queues(rt2x00dev->hw);
139
140         return 0;
141 }
142
143 void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
144 {
145         if (!__test_and_clear_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
146                 return;
147
148         /*
149          * Stop all scheduled work.
150          */
151         if (work_pending(&rt2x00dev->beacon_work))
152                 cancel_work_sync(&rt2x00dev->beacon_work);
153         if (work_pending(&rt2x00dev->filter_work))
154                 cancel_work_sync(&rt2x00dev->filter_work);
155         if (work_pending(&rt2x00dev->config_work))
156                 cancel_work_sync(&rt2x00dev->config_work);
157
158         /*
159          * Stop the TX queues.
160          */
161         ieee80211_stop_queues(rt2x00dev->hw);
162
163         /*
164          * Disable RX.
165          */
166         rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
167
168         /*
169          * Disable radio.
170          */
171         rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
172 }
173
174 void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, enum dev_state state)
175 {
176         /*
177          * When we are disabling the RX, we should also stop the link tuner.
178          */
179         if (state == STATE_RADIO_RX_OFF)
180                 rt2x00lib_stop_link_tuner(rt2x00dev);
181
182         rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
183
184         /*
185          * When we are enabling the RX, we should also start the link tuner.
186          */
187         if (state == STATE_RADIO_RX_ON &&
188             is_interface_present(&rt2x00dev->interface))
189                 rt2x00lib_start_link_tuner(rt2x00dev);
190 }
191
192 static void rt2x00lib_evaluate_antenna_sample(struct rt2x00_dev *rt2x00dev)
193 {
194         enum antenna rx = rt2x00dev->link.ant.active.rx;
195         enum antenna tx = rt2x00dev->link.ant.active.tx;
196         int sample_a =
197             rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_A);
198         int sample_b =
199             rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_B);
200
201         /*
202          * We are done sampling. Now we should evaluate the results.
203          */
204         rt2x00dev->link.ant.flags &= ~ANTENNA_MODE_SAMPLE;
205
206         /*
207          * During the last period we have sampled the RSSI
208          * from both antenna's. It now is time to determine
209          * which antenna demonstrated the best performance.
210          * When we are already on the antenna with the best
211          * performance, then there really is nothing for us
212          * left to do.
213          */
214         if (sample_a == sample_b)
215                 return;
216
217         if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY) {
218                 if (sample_a > sample_b && rx == ANTENNA_B)
219                         rx = ANTENNA_A;
220                 else if (rx == ANTENNA_A)
221                         rx = ANTENNA_B;
222         }
223
224         if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY) {
225                 if (sample_a > sample_b && tx == ANTENNA_B)
226                         tx = ANTENNA_A;
227                 else if (tx == ANTENNA_A)
228                         tx = ANTENNA_B;
229         }
230
231         rt2x00lib_config_antenna(rt2x00dev, rx, tx);
232 }
233
234 static void rt2x00lib_evaluate_antenna_eval(struct rt2x00_dev *rt2x00dev)
235 {
236         enum antenna rx = rt2x00dev->link.ant.active.rx;
237         enum antenna tx = rt2x00dev->link.ant.active.tx;
238         int rssi_curr = rt2x00_get_link_ant_rssi(&rt2x00dev->link);
239         int rssi_old = rt2x00_update_ant_rssi(&rt2x00dev->link, rssi_curr);
240
241         /*
242          * Legacy driver indicates that we should swap antenna's
243          * when the difference in RSSI is greater that 5. This
244          * also should be done when the RSSI was actually better
245          * then the previous sample.
246          * When the difference exceeds the threshold we should
247          * sample the rssi from the other antenna to make a valid
248          * comparison between the 2 antennas.
249          */
250         if ((rssi_curr - rssi_old) > -5 || (rssi_curr - rssi_old) < 5)
251                 return;
252
253         rt2x00dev->link.ant.flags |= ANTENNA_MODE_SAMPLE;
254
255         if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY)
256                 rx = (rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
257
258         if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)
259                 tx = (tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
260
261         rt2x00lib_config_antenna(rt2x00dev, rx, tx);
262 }
263
264 static void rt2x00lib_evaluate_antenna(struct rt2x00_dev *rt2x00dev)
265 {
266         /*
267          * Determine if software diversity is enabled for
268          * either the TX or RX antenna (or both).
269          * Always perform this check since within the link
270          * tuner interval the configuration might have changed.
271          */
272         rt2x00dev->link.ant.flags &= ~ANTENNA_RX_DIVERSITY;
273         rt2x00dev->link.ant.flags &= ~ANTENNA_TX_DIVERSITY;
274
275         if (rt2x00dev->hw->conf.antenna_sel_rx == 0 &&
276             rt2x00dev->default_ant.rx != ANTENNA_SW_DIVERSITY)
277                 rt2x00dev->link.ant.flags |= ANTENNA_RX_DIVERSITY;
278         if (rt2x00dev->hw->conf.antenna_sel_tx == 0 &&
279             rt2x00dev->default_ant.tx != ANTENNA_SW_DIVERSITY)
280                 rt2x00dev->link.ant.flags |= ANTENNA_TX_DIVERSITY;
281
282         if (!(rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY) &&
283             !(rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)) {
284                 rt2x00dev->link.ant.flags &= ~ANTENNA_MODE_SAMPLE;
285                 return;
286         }
287
288         /*
289          * If we have only sampled the data over the last period
290          * we should now harvest the data. Otherwise just evaluate
291          * the data. The latter should only be performed once
292          * every 2 seconds.
293          */
294         if (rt2x00dev->link.ant.flags & ANTENNA_MODE_SAMPLE)
295                 rt2x00lib_evaluate_antenna_sample(rt2x00dev);
296         else if (rt2x00dev->link.count & 1)
297                 rt2x00lib_evaluate_antenna_eval(rt2x00dev);
298 }
299
300 static void rt2x00lib_update_link_stats(struct link *link, int rssi)
301 {
302         int avg_rssi = rssi;
303
304         /*
305          * Update global RSSI
306          */
307         if (link->qual.avg_rssi)
308                 avg_rssi = MOVING_AVERAGE(link->qual.avg_rssi, rssi, 8);
309         link->qual.avg_rssi = avg_rssi;
310
311         /*
312          * Update antenna RSSI
313          */
314         if (link->ant.rssi_ant)
315                 rssi = MOVING_AVERAGE(link->ant.rssi_ant, rssi, 8);
316         link->ant.rssi_ant = rssi;
317 }
318
319 static void rt2x00lib_precalculate_link_signal(struct link_qual *qual)
320 {
321         if (qual->rx_failed || qual->rx_success)
322                 qual->rx_percentage =
323                     (qual->rx_success * 100) /
324                     (qual->rx_failed + qual->rx_success);
325         else
326                 qual->rx_percentage = 50;
327
328         if (qual->tx_failed || qual->tx_success)
329                 qual->tx_percentage =
330                     (qual->tx_success * 100) /
331                     (qual->tx_failed + qual->tx_success);
332         else
333                 qual->tx_percentage = 50;
334
335         qual->rx_success = 0;
336         qual->rx_failed = 0;
337         qual->tx_success = 0;
338         qual->tx_failed = 0;
339 }
340
341 static int rt2x00lib_calculate_link_signal(struct rt2x00_dev *rt2x00dev,
342                                            int rssi)
343 {
344         int rssi_percentage = 0;
345         int signal;
346
347         /*
348          * We need a positive value for the RSSI.
349          */
350         if (rssi < 0)
351                 rssi += rt2x00dev->rssi_offset;
352
353         /*
354          * Calculate the different percentages,
355          * which will be used for the signal.
356          */
357         if (rt2x00dev->rssi_offset)
358                 rssi_percentage = (rssi * 100) / rt2x00dev->rssi_offset;
359
360         /*
361          * Add the individual percentages and use the WEIGHT
362          * defines to calculate the current link signal.
363          */
364         signal = ((WEIGHT_RSSI * rssi_percentage) +
365                   (WEIGHT_TX * rt2x00dev->link.qual.tx_percentage) +
366                   (WEIGHT_RX * rt2x00dev->link.qual.rx_percentage)) / 100;
367
368         return (signal > 100) ? 100 : signal;
369 }
370
371 static void rt2x00lib_link_tuner(struct work_struct *work)
372 {
373         struct rt2x00_dev *rt2x00dev =
374             container_of(work, struct rt2x00_dev, link.work.work);
375
376         /*
377          * When the radio is shutting down we should
378          * immediately cease all link tuning.
379          */
380         if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
381                 return;
382
383         /*
384          * Update statistics.
385          */
386         rt2x00dev->ops->lib->link_stats(rt2x00dev, &rt2x00dev->link.qual);
387         rt2x00dev->low_level_stats.dot11FCSErrorCount +=
388             rt2x00dev->link.qual.rx_failed;
389
390         /*
391          * Only perform the link tuning when Link tuning
392          * has been enabled (This could have been disabled from the EEPROM).
393          */
394         if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
395                 rt2x00dev->ops->lib->link_tuner(rt2x00dev);
396
397         /*
398          * Evaluate antenna setup.
399          */
400         rt2x00lib_evaluate_antenna(rt2x00dev);
401
402         /*
403          * Precalculate a portion of the link signal which is
404          * in based on the tx/rx success/failure counters.
405          */
406         rt2x00lib_precalculate_link_signal(&rt2x00dev->link.qual);
407
408         /*
409          * Increase tuner counter, and reschedule the next link tuner run.
410          */
411         rt2x00dev->link.count++;
412         queue_delayed_work(rt2x00dev->hw->workqueue, &rt2x00dev->link.work,
413                            LINK_TUNE_INTERVAL);
414 }
415
416 static void rt2x00lib_packetfilter_scheduled(struct work_struct *work)
417 {
418         struct rt2x00_dev *rt2x00dev =
419             container_of(work, struct rt2x00_dev, filter_work);
420         unsigned int filter = rt2x00dev->interface.filter;
421
422         /*
423          * Since we had stored the filter inside interface.filter,
424          * we should now clear that field. Otherwise the driver will
425          * assume nothing has changed (*total_flags will be compared
426          * to interface.filter to determine if any action is required).
427          */
428         rt2x00dev->interface.filter = 0;
429
430         rt2x00dev->ops->hw->configure_filter(rt2x00dev->hw,
431                                              filter, &filter, 0, NULL);
432 }
433
434 static void rt2x00lib_configuration_scheduled(struct work_struct *work)
435 {
436         struct rt2x00_dev *rt2x00dev =
437             container_of(work, struct rt2x00_dev, config_work);
438         int preamble = !test_bit(CONFIG_SHORT_PREAMBLE, &rt2x00dev->flags);
439
440         rt2x00mac_erp_ie_changed(rt2x00dev->hw,
441                                  IEEE80211_ERP_CHANGE_PREAMBLE, 0, preamble);
442 }
443
444 /*
445  * Interrupt context handlers.
446  */
447 static void rt2x00lib_beacondone_scheduled(struct work_struct *work)
448 {
449         struct rt2x00_dev *rt2x00dev =
450             container_of(work, struct rt2x00_dev, beacon_work);
451         struct data_ring *ring =
452             rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_BEACON);
453         struct data_entry *entry = rt2x00_get_data_entry(ring);
454         struct sk_buff *skb;
455
456         skb = ieee80211_beacon_get(rt2x00dev->hw,
457                                    rt2x00dev->interface.id,
458                                    &entry->tx_status.control);
459         if (!skb)
460                 return;
461
462         rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw, skb,
463                                           &entry->tx_status.control);
464
465         dev_kfree_skb(skb);
466 }
467
468 void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
469 {
470         if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
471                 return;
472
473         queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->beacon_work);
474 }
475 EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
476
477 void rt2x00lib_txdone(struct data_entry *entry,
478                       const int status, const int retry)
479 {
480         struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
481         struct ieee80211_tx_status *tx_status = &entry->tx_status;
482         struct ieee80211_low_level_stats *stats = &rt2x00dev->low_level_stats;
483         int success = !!(status == TX_SUCCESS || status == TX_SUCCESS_RETRY);
484         int fail = !!(status == TX_FAIL_RETRY || status == TX_FAIL_INVALID ||
485                       status == TX_FAIL_OTHER);
486
487         /*
488          * Update TX statistics.
489          */
490         tx_status->flags = 0;
491         tx_status->ack_signal = 0;
492         tx_status->excessive_retries = (status == TX_FAIL_RETRY);
493         tx_status->retry_count = retry;
494         rt2x00dev->link.qual.tx_success += success;
495         rt2x00dev->link.qual.tx_failed += retry + fail;
496
497         if (!(tx_status->control.flags & IEEE80211_TXCTL_NO_ACK)) {
498                 if (success)
499                         tx_status->flags |= IEEE80211_TX_STATUS_ACK;
500                 else
501                         stats->dot11ACKFailureCount++;
502         }
503
504         tx_status->queue_length = entry->ring->stats.limit;
505         tx_status->queue_number = tx_status->control.queue;
506
507         if (tx_status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) {
508                 if (success)
509                         stats->dot11RTSSuccessCount++;
510                 else
511                         stats->dot11RTSFailureCount++;
512         }
513
514         /*
515          * Send the tx_status to mac80211 & debugfs.
516          * mac80211 will clean up the skb structure.
517          */
518         get_skb_desc(entry->skb)->frame_type = DUMP_FRAME_TXDONE;
519         rt2x00debug_dump_frame(rt2x00dev, entry->skb);
520         ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb, tx_status);
521         entry->skb = NULL;
522 }
523 EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
524
525 void rt2x00lib_rxdone(struct data_entry *entry, struct sk_buff *skb,
526                       struct rxdata_entry_desc *desc)
527 {
528         struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
529         struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
530         struct ieee80211_hw_mode *mode;
531         struct ieee80211_rate *rate;
532         unsigned int i;
533         int val = 0;
534
535         /*
536          * Update RX statistics.
537          */
538         mode = &rt2x00dev->hwmodes[rt2x00dev->curr_hwmode];
539         for (i = 0; i < mode->num_rates; i++) {
540                 rate = &mode->rates[i];
541
542                 /*
543                  * When frame was received with an OFDM bitrate,
544                  * the signal is the PLCP value. If it was received with
545                  * a CCK bitrate the signal is the rate in 0.5kbit/s.
546                  */
547                 if (!desc->ofdm)
548                         val = DEVICE_GET_RATE_FIELD(rate->val, RATE);
549                 else
550                         val = DEVICE_GET_RATE_FIELD(rate->val, PLCP);
551
552                 if (val == desc->signal) {
553                         val = rate->val;
554                         break;
555                 }
556         }
557
558         rt2x00lib_update_link_stats(&rt2x00dev->link, desc->rssi);
559         rt2x00dev->link.qual.rx_success++;
560
561         rx_status->rate = val;
562         rx_status->signal =
563             rt2x00lib_calculate_link_signal(rt2x00dev, desc->rssi);
564         rx_status->ssi = desc->rssi;
565         rx_status->flag = desc->flags;
566         rx_status->antenna = rt2x00dev->link.ant.active.rx;
567
568         /*
569          * Send frame to mac80211 & debugfs
570          */
571         get_skb_desc(skb)->frame_type = DUMP_FRAME_RXDONE;
572         rt2x00debug_dump_frame(rt2x00dev, skb);
573         ieee80211_rx_irqsafe(rt2x00dev->hw, skb, rx_status);
574 }
575 EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
576
577 /*
578  * TX descriptor initializer
579  */
580 void rt2x00lib_write_tx_desc(struct rt2x00_dev *rt2x00dev,
581                              struct sk_buff *skb,
582                              struct ieee80211_tx_control *control)
583 {
584         struct txdata_entry_desc desc;
585         struct skb_desc *skbdesc = get_skb_desc(skb);
586         struct ieee80211_hdr *ieee80211hdr = skbdesc->data;
587         __le32 *txd = skbdesc->desc;
588         int tx_rate;
589         int bitrate;
590         int length;
591         int duration;
592         int residual;
593         u16 frame_control;
594         u16 seq_ctrl;
595
596         memset(&desc, 0, sizeof(desc));
597
598         desc.cw_min = skbdesc->ring->tx_params.cw_min;
599         desc.cw_max = skbdesc->ring->tx_params.cw_max;
600         desc.aifs = skbdesc->ring->tx_params.aifs;
601
602         /*
603          * Identify queue
604          */
605         if (control->queue < rt2x00dev->hw->queues)
606                 desc.queue = control->queue;
607         else if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
608                  control->queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
609                 desc.queue = QUEUE_MGMT;
610         else
611                 desc.queue = QUEUE_OTHER;
612
613         /*
614          * Read required fields from ieee80211 header.
615          */
616         frame_control = le16_to_cpu(ieee80211hdr->frame_control);
617         seq_ctrl = le16_to_cpu(ieee80211hdr->seq_ctrl);
618
619         tx_rate = control->tx_rate;
620
621         /*
622          * Check whether this frame is to be acked
623          */
624         if (!(control->flags & IEEE80211_TXCTL_NO_ACK))
625                 __set_bit(ENTRY_TXD_ACK, &desc.flags);
626
627         /*
628          * Check if this is a RTS/CTS frame
629          */
630         if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
631                 __set_bit(ENTRY_TXD_BURST, &desc.flags);
632                 if (is_rts_frame(frame_control)) {
633                         __set_bit(ENTRY_TXD_RTS_FRAME, &desc.flags);
634                         __set_bit(ENTRY_TXD_ACK, &desc.flags);
635                 } else
636                         __clear_bit(ENTRY_TXD_ACK, &desc.flags);
637                 if (control->rts_cts_rate)
638                         tx_rate = control->rts_cts_rate;
639         }
640
641         /*
642          * Check for OFDM
643          */
644         if (DEVICE_GET_RATE_FIELD(tx_rate, RATEMASK) & DEV_OFDM_RATEMASK)
645                 __set_bit(ENTRY_TXD_OFDM_RATE, &desc.flags);
646
647         /*
648          * Check if more fragments are pending
649          */
650         if (ieee80211_get_morefrag(ieee80211hdr)) {
651                 __set_bit(ENTRY_TXD_BURST, &desc.flags);
652                 __set_bit(ENTRY_TXD_MORE_FRAG, &desc.flags);
653         }
654
655         /*
656          * Beacons and probe responses require the tsf timestamp
657          * to be inserted into the frame.
658          */
659         if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
660             is_probe_resp(frame_control))
661                 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &desc.flags);
662
663         /*
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.
667          */
668         if ((seq_ctrl & IEEE80211_SCTL_FRAG) > 0 ||
669             test_bit(ENTRY_TXD_RTS_FRAME, &desc.flags))
670                 desc.ifs = IFS_SIFS;
671         else
672                 desc.ifs = IFS_BACKOFF;
673
674         /*
675          * PLCP setup
676          * Length calculation depends on OFDM/CCK rate.
677          */
678         desc.signal = DEVICE_GET_RATE_FIELD(tx_rate, PLCP);
679         desc.service = 0x04;
680
681         length = skbdesc->data_len + FCS_LEN;
682         if (test_bit(ENTRY_TXD_OFDM_RATE, &desc.flags)) {
683                 desc.length_high = (length >> 6) & 0x3f;
684                 desc.length_low = length & 0x3f;
685         } else {
686                 bitrate = DEVICE_GET_RATE_FIELD(tx_rate, RATE);
687
688                 /*
689                  * Convert length to microseconds.
690                  */
691                 residual = get_duration_res(length, bitrate);
692                 duration = get_duration(length, bitrate);
693
694                 if (residual != 0) {
695                         duration++;
696
697                         /*
698                          * Check if we need to set the Length Extension
699                          */
700                         if (bitrate == 110 && residual <= 30)
701                                 desc.service |= 0x80;
702                 }
703
704                 desc.length_high = (duration >> 8) & 0xff;
705                 desc.length_low = duration & 0xff;
706
707                 /*
708                  * When preamble is enabled we should set the
709                  * preamble bit for the signal.
710                  */
711                 if (DEVICE_GET_RATE_FIELD(tx_rate, PREAMBLE))
712                         desc.signal |= 0x08;
713         }
714
715         rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, txd, &desc, ieee80211hdr,
716                                            skbdesc->data_len, control);
717
718         /*
719          * Update ring entry.
720          */
721         skbdesc->entry->skb = skb;
722         memcpy(&skbdesc->entry->tx_status.control, control, sizeof(*control));
723
724         /*
725          * The frame has been completely initialized and ready
726          * for sending to the device. The caller will push the
727          * frame to the device, but we are going to push the
728          * frame to debugfs here.
729          */
730         skbdesc->frame_type = DUMP_FRAME_TX;
731         rt2x00debug_dump_frame(rt2x00dev, skb);
732 }
733 EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc);
734
735 /*
736  * Driver initialization handlers.
737  */
738 static void rt2x00lib_channel(struct ieee80211_channel *entry,
739                               const int channel, const int tx_power,
740                               const int value)
741 {
742         entry->chan = channel;
743         if (channel <= 14)
744                 entry->freq = 2407 + (5 * channel);
745         else
746                 entry->freq = 5000 + (5 * channel);
747         entry->val = value;
748         entry->flag =
749             IEEE80211_CHAN_W_IBSS |
750             IEEE80211_CHAN_W_ACTIVE_SCAN |
751             IEEE80211_CHAN_W_SCAN;
752         entry->power_level = tx_power;
753         entry->antenna_max = 0xff;
754 }
755
756 static void rt2x00lib_rate(struct ieee80211_rate *entry,
757                            const int rate, const int mask,
758                            const int plcp, const int flags)
759 {
760         entry->rate = rate;
761         entry->val =
762             DEVICE_SET_RATE_FIELD(rate, RATE) |
763             DEVICE_SET_RATE_FIELD(mask, RATEMASK) |
764             DEVICE_SET_RATE_FIELD(plcp, PLCP);
765         entry->flags = flags;
766         entry->val2 = entry->val;
767         if (entry->flags & IEEE80211_RATE_PREAMBLE2)
768                 entry->val2 |= DEVICE_SET_RATE_FIELD(1, PREAMBLE);
769         entry->min_rssi_ack = 0;
770         entry->min_rssi_ack_delta = 0;
771 }
772
773 static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
774                                     struct hw_mode_spec *spec)
775 {
776         struct ieee80211_hw *hw = rt2x00dev->hw;
777         struct ieee80211_hw_mode *hwmodes;
778         struct ieee80211_channel *channels;
779         struct ieee80211_rate *rates;
780         unsigned int i;
781         unsigned char tx_power;
782
783         hwmodes = kzalloc(sizeof(*hwmodes) * spec->num_modes, GFP_KERNEL);
784         if (!hwmodes)
785                 goto exit;
786
787         channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
788         if (!channels)
789                 goto exit_free_modes;
790
791         rates = kzalloc(sizeof(*rates) * spec->num_rates, GFP_KERNEL);
792         if (!rates)
793                 goto exit_free_channels;
794
795         /*
796          * Initialize Rate list.
797          */
798         rt2x00lib_rate(&rates[0], 10, DEV_RATEMASK_1MB,
799                        0x00, IEEE80211_RATE_CCK);
800         rt2x00lib_rate(&rates[1], 20, DEV_RATEMASK_2MB,
801                        0x01, IEEE80211_RATE_CCK_2);
802         rt2x00lib_rate(&rates[2], 55, DEV_RATEMASK_5_5MB,
803                        0x02, IEEE80211_RATE_CCK_2);
804         rt2x00lib_rate(&rates[3], 110, DEV_RATEMASK_11MB,
805                        0x03, IEEE80211_RATE_CCK_2);
806
807         if (spec->num_rates > 4) {
808                 rt2x00lib_rate(&rates[4], 60, DEV_RATEMASK_6MB,
809                                0x0b, IEEE80211_RATE_OFDM);
810                 rt2x00lib_rate(&rates[5], 90, DEV_RATEMASK_9MB,
811                                0x0f, IEEE80211_RATE_OFDM);
812                 rt2x00lib_rate(&rates[6], 120, DEV_RATEMASK_12MB,
813                                0x0a, IEEE80211_RATE_OFDM);
814                 rt2x00lib_rate(&rates[7], 180, DEV_RATEMASK_18MB,
815                                0x0e, IEEE80211_RATE_OFDM);
816                 rt2x00lib_rate(&rates[8], 240, DEV_RATEMASK_24MB,
817                                0x09, IEEE80211_RATE_OFDM);
818                 rt2x00lib_rate(&rates[9], 360, DEV_RATEMASK_36MB,
819                                0x0d, IEEE80211_RATE_OFDM);
820                 rt2x00lib_rate(&rates[10], 480, DEV_RATEMASK_48MB,
821                                0x08, IEEE80211_RATE_OFDM);
822                 rt2x00lib_rate(&rates[11], 540, DEV_RATEMASK_54MB,
823                                0x0c, IEEE80211_RATE_OFDM);
824         }
825
826         /*
827          * Initialize Channel list.
828          */
829         for (i = 0; i < spec->num_channels; i++) {
830                 if (spec->channels[i].channel <= 14)
831                         tx_power = spec->tx_power_bg[i];
832                 else if (spec->tx_power_a)
833                         tx_power = spec->tx_power_a[i];
834                 else
835                         tx_power = spec->tx_power_default;
836
837                 rt2x00lib_channel(&channels[i],
838                                   spec->channels[i].channel, tx_power, i);
839         }
840
841         /*
842          * Intitialize 802.11b
843          * Rates: CCK.
844          * Channels: OFDM.
845          */
846         if (spec->num_modes > HWMODE_B) {
847                 hwmodes[HWMODE_B].mode = MODE_IEEE80211B;
848                 hwmodes[HWMODE_B].num_channels = 14;
849                 hwmodes[HWMODE_B].num_rates = 4;
850                 hwmodes[HWMODE_B].channels = channels;
851                 hwmodes[HWMODE_B].rates = rates;
852         }
853
854         /*
855          * Intitialize 802.11g
856          * Rates: CCK, OFDM.
857          * Channels: OFDM.
858          */
859         if (spec->num_modes > HWMODE_G) {
860                 hwmodes[HWMODE_G].mode = MODE_IEEE80211G;
861                 hwmodes[HWMODE_G].num_channels = 14;
862                 hwmodes[HWMODE_G].num_rates = spec->num_rates;
863                 hwmodes[HWMODE_G].channels = channels;
864                 hwmodes[HWMODE_G].rates = rates;
865         }
866
867         /*
868          * Intitialize 802.11a
869          * Rates: OFDM.
870          * Channels: OFDM, UNII, HiperLAN2.
871          */
872         if (spec->num_modes > HWMODE_A) {
873                 hwmodes[HWMODE_A].mode = MODE_IEEE80211A;
874                 hwmodes[HWMODE_A].num_channels = spec->num_channels - 14;
875                 hwmodes[HWMODE_A].num_rates = spec->num_rates - 4;
876                 hwmodes[HWMODE_A].channels = &channels[14];
877                 hwmodes[HWMODE_A].rates = &rates[4];
878         }
879
880         if (spec->num_modes > HWMODE_G &&
881             ieee80211_register_hwmode(hw, &hwmodes[HWMODE_G]))
882                 goto exit_free_rates;
883
884         if (spec->num_modes > HWMODE_B &&
885             ieee80211_register_hwmode(hw, &hwmodes[HWMODE_B]))
886                 goto exit_free_rates;
887
888         if (spec->num_modes > HWMODE_A &&
889             ieee80211_register_hwmode(hw, &hwmodes[HWMODE_A]))
890                 goto exit_free_rates;
891
892         rt2x00dev->hwmodes = hwmodes;
893
894         return 0;
895
896 exit_free_rates:
897         kfree(rates);
898
899 exit_free_channels:
900         kfree(channels);
901
902 exit_free_modes:
903         kfree(hwmodes);
904
905 exit:
906         ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
907         return -ENOMEM;
908 }
909
910 static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
911 {
912         if (test_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags))
913                 ieee80211_unregister_hw(rt2x00dev->hw);
914
915         if (likely(rt2x00dev->hwmodes)) {
916                 kfree(rt2x00dev->hwmodes->channels);
917                 kfree(rt2x00dev->hwmodes->rates);
918                 kfree(rt2x00dev->hwmodes);
919                 rt2x00dev->hwmodes = NULL;
920         }
921 }
922
923 static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
924 {
925         struct hw_mode_spec *spec = &rt2x00dev->spec;
926         int status;
927
928         /*
929          * Initialize HW modes.
930          */
931         status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
932         if (status)
933                 return status;
934
935         /*
936          * Register HW.
937          */
938         status = ieee80211_register_hw(rt2x00dev->hw);
939         if (status) {
940                 rt2x00lib_remove_hw(rt2x00dev);
941                 return status;
942         }
943
944         __set_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags);
945
946         return 0;
947 }
948
949 /*
950  * Initialization/uninitialization handlers.
951  */
952 static int rt2x00lib_alloc_entries(struct data_ring *ring,
953                                    const u16 max_entries, const u16 data_size,
954                                    const u16 desc_size)
955 {
956         struct data_entry *entry;
957         unsigned int i;
958
959         ring->stats.limit = max_entries;
960         ring->data_size = data_size;
961         ring->desc_size = desc_size;
962
963         /*
964          * Allocate all ring entries.
965          */
966         entry = kzalloc(ring->stats.limit * sizeof(*entry), GFP_KERNEL);
967         if (!entry)
968                 return -ENOMEM;
969
970         for (i = 0; i < ring->stats.limit; i++) {
971                 entry[i].flags = 0;
972                 entry[i].ring = ring;
973                 entry[i].skb = NULL;
974         }
975
976         ring->entry = entry;
977
978         return 0;
979 }
980
981 static int rt2x00lib_alloc_ring_entries(struct rt2x00_dev *rt2x00dev)
982 {
983         struct data_ring *ring;
984
985         /*
986          * Allocate the RX ring.
987          */
988         if (rt2x00lib_alloc_entries(rt2x00dev->rx, RX_ENTRIES, DATA_FRAME_SIZE,
989                                     rt2x00dev->ops->rxd_size))
990                 return -ENOMEM;
991
992         /*
993          * First allocate the TX rings.
994          */
995         txring_for_each(rt2x00dev, ring) {
996                 if (rt2x00lib_alloc_entries(ring, TX_ENTRIES, DATA_FRAME_SIZE,
997                                             rt2x00dev->ops->txd_size))
998                         return -ENOMEM;
999         }
1000
1001         if (!test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
1002                 return 0;
1003
1004         /*
1005          * Allocate the BEACON ring.
1006          */
1007         if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[0], BEACON_ENTRIES,
1008                                     MGMT_FRAME_SIZE, rt2x00dev->ops->txd_size))
1009                 return -ENOMEM;
1010
1011         /*
1012          * Allocate the Atim ring.
1013          */
1014         if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[1], ATIM_ENTRIES,
1015                                     DATA_FRAME_SIZE, rt2x00dev->ops->txd_size))
1016                 return -ENOMEM;
1017
1018         return 0;
1019 }
1020
1021 static void rt2x00lib_free_ring_entries(struct rt2x00_dev *rt2x00dev)
1022 {
1023         struct data_ring *ring;
1024
1025         ring_for_each(rt2x00dev, ring) {
1026                 kfree(ring->entry);
1027                 ring->entry = NULL;
1028         }
1029 }
1030
1031 void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
1032 {
1033         if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
1034                 return;
1035
1036         /*
1037          * Unregister rfkill.
1038          */
1039         rt2x00rfkill_unregister(rt2x00dev);
1040
1041         /*
1042          * Allow the HW to uninitialize.
1043          */
1044         rt2x00dev->ops->lib->uninitialize(rt2x00dev);
1045
1046         /*
1047          * Free allocated ring entries.
1048          */
1049         rt2x00lib_free_ring_entries(rt2x00dev);
1050 }
1051
1052 int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
1053 {
1054         int status;
1055
1056         if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
1057                 return 0;
1058
1059         /*
1060          * Allocate all ring entries.
1061          */
1062         status = rt2x00lib_alloc_ring_entries(rt2x00dev);
1063         if (status) {
1064                 ERROR(rt2x00dev, "Ring entries allocation failed.\n");
1065                 return status;
1066         }
1067
1068         /*
1069          * Initialize the device.
1070          */
1071         status = rt2x00dev->ops->lib->initialize(rt2x00dev);
1072         if (status)
1073                 goto exit;
1074
1075         __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
1076
1077         /*
1078          * Register the rfkill handler.
1079          */
1080         status = rt2x00rfkill_register(rt2x00dev);
1081         if (status)
1082                 goto exit_unitialize;
1083
1084         return 0;
1085
1086 exit_unitialize:
1087         rt2x00lib_uninitialize(rt2x00dev);
1088
1089 exit:
1090         rt2x00lib_free_ring_entries(rt2x00dev);
1091
1092         return status;
1093 }
1094
1095 /*
1096  * driver allocation handlers.
1097  */
1098 static int rt2x00lib_alloc_rings(struct rt2x00_dev *rt2x00dev)
1099 {
1100         struct data_ring *ring;
1101
1102         /*
1103          * We need the following rings:
1104          * RX: 1
1105          * TX: hw->queues
1106          * Beacon: 1 (if required)
1107          * Atim: 1 (if required)
1108          */
1109         rt2x00dev->data_rings = 1 + rt2x00dev->hw->queues +
1110             (2 * test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags));
1111
1112         ring = kzalloc(rt2x00dev->data_rings * sizeof(*ring), GFP_KERNEL);
1113         if (!ring) {
1114                 ERROR(rt2x00dev, "Ring allocation failed.\n");
1115                 return -ENOMEM;
1116         }
1117
1118         /*
1119          * Initialize pointers
1120          */
1121         rt2x00dev->rx = ring;
1122         rt2x00dev->tx = &rt2x00dev->rx[1];
1123         if (test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
1124                 rt2x00dev->bcn = &rt2x00dev->tx[rt2x00dev->hw->queues];
1125
1126         /*
1127          * Initialize ring parameters.
1128          * cw_min: 2^5 = 32.
1129          * cw_max: 2^10 = 1024.
1130          */
1131         ring_for_each(rt2x00dev, ring) {
1132                 ring->rt2x00dev = rt2x00dev;
1133                 ring->tx_params.aifs = 2;
1134                 ring->tx_params.cw_min = 5;
1135                 ring->tx_params.cw_max = 10;
1136         }
1137
1138         return 0;
1139 }
1140
1141 static void rt2x00lib_free_rings(struct rt2x00_dev *rt2x00dev)
1142 {
1143         kfree(rt2x00dev->rx);
1144         rt2x00dev->rx = NULL;
1145         rt2x00dev->tx = NULL;
1146         rt2x00dev->bcn = NULL;
1147 }
1148
1149 int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
1150 {
1151         int retval = -ENOMEM;
1152
1153         /*
1154          * Let the driver probe the device to detect the capabilities.
1155          */
1156         retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
1157         if (retval) {
1158                 ERROR(rt2x00dev, "Failed to allocate device.\n");
1159                 goto exit;
1160         }
1161
1162         /*
1163          * Initialize configuration work.
1164          */
1165         INIT_WORK(&rt2x00dev->beacon_work, rt2x00lib_beacondone_scheduled);
1166         INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
1167         INIT_WORK(&rt2x00dev->config_work, rt2x00lib_configuration_scheduled);
1168         INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
1169
1170         /*
1171          * Reset current working type.
1172          */
1173         rt2x00dev->interface.type = INVALID_INTERFACE;
1174
1175         /*
1176          * Allocate ring array.
1177          */
1178         retval = rt2x00lib_alloc_rings(rt2x00dev);
1179         if (retval)
1180                 goto exit;
1181
1182         /*
1183          * Initialize ieee80211 structure.
1184          */
1185         retval = rt2x00lib_probe_hw(rt2x00dev);
1186         if (retval) {
1187                 ERROR(rt2x00dev, "Failed to initialize hw.\n");
1188                 goto exit;
1189         }
1190
1191         /*
1192          * Allocatie rfkill.
1193          */
1194         retval = rt2x00rfkill_allocate(rt2x00dev);
1195         if (retval)
1196                 goto exit;
1197
1198         /*
1199          * Open the debugfs entry.
1200          */
1201         rt2x00debug_register(rt2x00dev);
1202
1203         __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1204
1205         return 0;
1206
1207 exit:
1208         rt2x00lib_remove_dev(rt2x00dev);
1209
1210         return retval;
1211 }
1212 EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
1213
1214 void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
1215 {
1216         __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1217
1218         /*
1219          * Disable radio.
1220          */
1221         rt2x00lib_disable_radio(rt2x00dev);
1222
1223         /*
1224          * Uninitialize device.
1225          */
1226         rt2x00lib_uninitialize(rt2x00dev);
1227
1228         /*
1229          * Close debugfs entry.
1230          */
1231         rt2x00debug_deregister(rt2x00dev);
1232
1233         /*
1234          * Free rfkill
1235          */
1236         rt2x00rfkill_free(rt2x00dev);
1237
1238         /*
1239          * Free ieee80211_hw memory.
1240          */
1241         rt2x00lib_remove_hw(rt2x00dev);
1242
1243         /*
1244          * Free firmware image.
1245          */
1246         rt2x00lib_free_firmware(rt2x00dev);
1247
1248         /*
1249          * Free ring structures.
1250          */
1251         rt2x00lib_free_rings(rt2x00dev);
1252 }
1253 EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
1254
1255 /*
1256  * Device state handlers
1257  */
1258 #ifdef CONFIG_PM
1259 int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
1260 {
1261         int retval;
1262
1263         NOTICE(rt2x00dev, "Going to sleep.\n");
1264         __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1265
1266         /*
1267          * Only continue if mac80211 has open interfaces.
1268          */
1269         if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1270                 goto exit;
1271         __set_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags);
1272
1273         /*
1274          * Disable radio and unitialize all items
1275          * that must be recreated on resume.
1276          */
1277         rt2x00mac_stop(rt2x00dev->hw);
1278         rt2x00lib_uninitialize(rt2x00dev);
1279         rt2x00debug_deregister(rt2x00dev);
1280
1281 exit:
1282         /*
1283          * Set device mode to sleep for power management.
1284          */
1285         retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
1286         if (retval)
1287                 return retval;
1288
1289         return 0;
1290 }
1291 EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1292
1293 int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1294 {
1295         struct interface *intf = &rt2x00dev->interface;
1296         int retval;
1297
1298         NOTICE(rt2x00dev, "Waking up.\n");
1299         __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1300
1301         /*
1302          * Open the debugfs entry.
1303          */
1304         rt2x00debug_register(rt2x00dev);
1305
1306         /*
1307          * Only continue if mac80211 had open interfaces.
1308          */
1309         if (!__test_and_clear_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags))
1310                 return 0;
1311
1312         /*
1313          * Reinitialize device and all active interfaces.
1314          */
1315         retval = rt2x00mac_start(rt2x00dev->hw);
1316         if (retval)
1317                 goto exit;
1318
1319         /*
1320          * Reconfigure device.
1321          */
1322         rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1);
1323         if (!rt2x00dev->hw->conf.radio_enabled)
1324                 rt2x00lib_disable_radio(rt2x00dev);
1325
1326         rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
1327         rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
1328         rt2x00lib_config_type(rt2x00dev, intf->type);
1329
1330         /*
1331          * It is possible that during that mac80211 has attempted
1332          * to send frames while we were suspending or resuming.
1333          * In that case we have disabled the TX queue and should
1334          * now enable it again
1335          */
1336         ieee80211_start_queues(rt2x00dev->hw);
1337
1338         /*
1339          * When in Master or Ad-hoc mode,
1340          * restart Beacon transmitting by faking a beacondone event.
1341          */
1342         if (intf->type == IEEE80211_IF_TYPE_AP ||
1343             intf->type == IEEE80211_IF_TYPE_IBSS)
1344                 rt2x00lib_beacondone(rt2x00dev);
1345
1346         return 0;
1347
1348 exit:
1349         rt2x00lib_disable_radio(rt2x00dev);
1350         rt2x00lib_uninitialize(rt2x00dev);
1351         rt2x00debug_deregister(rt2x00dev);
1352
1353         return retval;
1354 }
1355 EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1356 #endif /* CONFIG_PM */
1357
1358 /*
1359  * rt2x00lib module information.
1360  */
1361 MODULE_AUTHOR(DRV_PROJECT);
1362 MODULE_VERSION(DRV_VERSION);
1363 MODULE_DESCRIPTION("rt2x00 library");
1364 MODULE_LICENSE("GPL");