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