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