rt2x00: Protect queue control with mutex
[linux-2.6.git] / drivers / net / wireless / rt2x00 / rt2x00dev.c
1 /*
2         Copyright (C) 2010 Willow Garage <http://www.willowgarage.com>
3         Copyright (C) 2004 - 2010 Ivo van Doorn <IvDoorn@gmail.com>
4         <http://rt2x00.serialmonkey.com>
5
6         This program is free software; you can redistribute it and/or modify
7         it under the terms of the GNU General Public License as published by
8         the Free Software Foundation; either version 2 of the License, or
9         (at your option) any later version.
10
11         This program is distributed in the hope that it will be useful,
12         but WITHOUT ANY WARRANTY; without even the implied warranty of
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14         GNU General Public License for more details.
15
16         You should have received a copy of the GNU General Public License
17         along with this program; if not, write to the
18         Free Software Foundation, Inc.,
19         59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21
22 /*
23         Module: rt2x00lib
24         Abstract: rt2x00 generic device routines.
25  */
26
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
30
31 #include "rt2x00.h"
32 #include "rt2x00lib.h"
33
34 /*
35  * Radio control handlers.
36  */
37 int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
38 {
39         int status;
40
41         /*
42          * Don't enable the radio twice.
43          * And check if the hardware button has been disabled.
44          */
45         if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
46                 return 0;
47
48         /*
49          * Initialize all data queues.
50          */
51         rt2x00queue_init_queues(rt2x00dev);
52
53         /*
54          * Enable radio.
55          */
56         status =
57             rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_ON);
58         if (status)
59                 return status;
60
61         rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_ON);
62
63         rt2x00leds_led_radio(rt2x00dev, true);
64         rt2x00led_led_activity(rt2x00dev, true);
65
66         set_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags);
67
68         /*
69          * Enable queues.
70          */
71         rt2x00queue_start_queues(rt2x00dev);
72         rt2x00link_start_tuner(rt2x00dev);
73
74         /*
75          * Start watchdog monitoring.
76          */
77         rt2x00link_start_watchdog(rt2x00dev);
78
79         return 0;
80 }
81
82 void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
83 {
84         if (!test_and_clear_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
85                 return;
86
87         /*
88          * Stop watchdog monitoring.
89          */
90         rt2x00link_stop_watchdog(rt2x00dev);
91
92         /*
93          * Stop all queues
94          */
95         rt2x00link_stop_tuner(rt2x00dev);
96         rt2x00queue_stop_queues(rt2x00dev);
97
98         /*
99          * Disable radio.
100          */
101         rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
102         rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_OFF);
103         rt2x00led_led_activity(rt2x00dev, false);
104         rt2x00leds_led_radio(rt2x00dev, false);
105 }
106
107 static void rt2x00lib_intf_scheduled_iter(void *data, u8 *mac,
108                                           struct ieee80211_vif *vif)
109 {
110         struct rt2x00_dev *rt2x00dev = data;
111         struct rt2x00_intf *intf = vif_to_intf(vif);
112         int delayed_flags;
113
114         /*
115          * Copy all data we need during this action under the protection
116          * of a spinlock. Otherwise race conditions might occur which results
117          * into an invalid configuration.
118          */
119         spin_lock(&intf->lock);
120
121         delayed_flags = intf->delayed_flags;
122         intf->delayed_flags = 0;
123
124         spin_unlock(&intf->lock);
125
126         /*
127          * It is possible the radio was disabled while the work had been
128          * scheduled. If that happens we should return here immediately,
129          * note that in the spinlock protected area above the delayed_flags
130          * have been cleared correctly.
131          */
132         if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
133                 return;
134
135         if (delayed_flags & DELAYED_UPDATE_BEACON)
136                 rt2x00queue_update_beacon(rt2x00dev, vif, true);
137 }
138
139 static void rt2x00lib_intf_scheduled(struct work_struct *work)
140 {
141         struct rt2x00_dev *rt2x00dev =
142             container_of(work, struct rt2x00_dev, intf_work);
143
144         /*
145          * Iterate over each interface and perform the
146          * requested configurations.
147          */
148         ieee80211_iterate_active_interfaces(rt2x00dev->hw,
149                                             rt2x00lib_intf_scheduled_iter,
150                                             rt2x00dev);
151 }
152
153 /*
154  * Interrupt context handlers.
155  */
156 static void rt2x00lib_bc_buffer_iter(void *data, u8 *mac,
157                                      struct ieee80211_vif *vif)
158 {
159         struct rt2x00_dev *rt2x00dev = data;
160         struct sk_buff *skb;
161
162         /*
163          * Only AP mode interfaces do broad- and multicast buffering
164          */
165         if (vif->type != NL80211_IFTYPE_AP)
166                 return;
167
168         /*
169          * Send out buffered broad- and multicast frames
170          */
171         skb = ieee80211_get_buffered_bc(rt2x00dev->hw, vif);
172         while (skb) {
173                 rt2x00mac_tx(rt2x00dev->hw, skb);
174                 skb = ieee80211_get_buffered_bc(rt2x00dev->hw, vif);
175         }
176 }
177
178 static void rt2x00lib_beaconupdate_iter(void *data, u8 *mac,
179                                         struct ieee80211_vif *vif)
180 {
181         struct rt2x00_dev *rt2x00dev = data;
182
183         if (vif->type != NL80211_IFTYPE_AP &&
184             vif->type != NL80211_IFTYPE_ADHOC &&
185             vif->type != NL80211_IFTYPE_MESH_POINT &&
186             vif->type != NL80211_IFTYPE_WDS)
187                 return;
188
189         rt2x00queue_update_beacon(rt2x00dev, vif, true);
190 }
191
192 void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
193 {
194         if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
195                 return;
196
197         /* send buffered bc/mc frames out for every bssid */
198         ieee80211_iterate_active_interfaces(rt2x00dev->hw,
199                                             rt2x00lib_bc_buffer_iter,
200                                             rt2x00dev);
201         /*
202          * Devices with pre tbtt interrupt don't need to update the beacon
203          * here as they will fetch the next beacon directly prior to
204          * transmission.
205          */
206         if (test_bit(DRIVER_SUPPORT_PRE_TBTT_INTERRUPT, &rt2x00dev->flags))
207                 return;
208
209         /* fetch next beacon */
210         ieee80211_iterate_active_interfaces(rt2x00dev->hw,
211                                             rt2x00lib_beaconupdate_iter,
212                                             rt2x00dev);
213 }
214 EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
215
216 void rt2x00lib_pretbtt(struct rt2x00_dev *rt2x00dev)
217 {
218         if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
219                 return;
220
221         /* fetch next beacon */
222         ieee80211_iterate_active_interfaces(rt2x00dev->hw,
223                                             rt2x00lib_beaconupdate_iter,
224                                             rt2x00dev);
225 }
226 EXPORT_SYMBOL_GPL(rt2x00lib_pretbtt);
227
228 void rt2x00lib_dmadone(struct queue_entry *entry)
229 {
230         clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
231         rt2x00queue_index_inc(entry->queue, Q_INDEX_DMA_DONE);
232 }
233 EXPORT_SYMBOL_GPL(rt2x00lib_dmadone);
234
235 void rt2x00lib_txdone(struct queue_entry *entry,
236                       struct txdone_entry_desc *txdesc)
237 {
238         struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
239         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
240         struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
241         unsigned int header_length, i;
242         u8 rate_idx, rate_flags, retry_rates;
243         u8 skbdesc_flags = skbdesc->flags;
244         bool success;
245
246         /*
247          * Unmap the skb.
248          */
249         rt2x00queue_unmap_skb(entry);
250
251         /*
252          * Remove the extra tx headroom from the skb.
253          */
254         skb_pull(entry->skb, rt2x00dev->ops->extra_tx_headroom);
255
256         /*
257          * Signal that the TX descriptor is no longer in the skb.
258          */
259         skbdesc->flags &= ~SKBDESC_DESC_IN_SKB;
260
261         /*
262          * Determine the length of 802.11 header.
263          */
264         header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
265
266         /*
267          * Remove L2 padding which was added during
268          */
269         if (test_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags))
270                 rt2x00queue_remove_l2pad(entry->skb, header_length);
271
272         /*
273          * If the IV/EIV data was stripped from the frame before it was
274          * passed to the hardware, we should now reinsert it again because
275          * mac80211 will expect the same data to be present it the
276          * frame as it was passed to us.
277          */
278         if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags))
279                 rt2x00crypto_tx_insert_iv(entry->skb, header_length);
280
281         /*
282          * Send frame to debugfs immediately, after this call is completed
283          * we are going to overwrite the skb->cb array.
284          */
285         rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TXDONE, entry->skb);
286
287         /*
288          * Determine if the frame has been successfully transmitted.
289          */
290         success =
291             test_bit(TXDONE_SUCCESS, &txdesc->flags) ||
292             test_bit(TXDONE_UNKNOWN, &txdesc->flags);
293
294         /*
295          * Update TX statistics.
296          */
297         rt2x00dev->link.qual.tx_success += success;
298         rt2x00dev->link.qual.tx_failed += !success;
299
300         rate_idx = skbdesc->tx_rate_idx;
301         rate_flags = skbdesc->tx_rate_flags;
302         retry_rates = test_bit(TXDONE_FALLBACK, &txdesc->flags) ?
303             (txdesc->retry + 1) : 1;
304
305         /*
306          * Initialize TX status
307          */
308         memset(&tx_info->status, 0, sizeof(tx_info->status));
309         tx_info->status.ack_signal = 0;
310
311         /*
312          * Frame was send with retries, hardware tried
313          * different rates to send out the frame, at each
314          * retry it lowered the rate 1 step except when the
315          * lowest rate was used.
316          */
317         for (i = 0; i < retry_rates && i < IEEE80211_TX_MAX_RATES; i++) {
318                 tx_info->status.rates[i].idx = rate_idx - i;
319                 tx_info->status.rates[i].flags = rate_flags;
320
321                 if (rate_idx - i == 0) {
322                         /*
323                          * The lowest rate (index 0) was used until the
324                          * number of max retries was reached.
325                          */
326                         tx_info->status.rates[i].count = retry_rates - i;
327                         i++;
328                         break;
329                 }
330                 tx_info->status.rates[i].count = 1;
331         }
332         if (i < (IEEE80211_TX_MAX_RATES - 1))
333                 tx_info->status.rates[i].idx = -1; /* terminate */
334
335         if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK)) {
336                 if (success)
337                         tx_info->flags |= IEEE80211_TX_STAT_ACK;
338                 else
339                         rt2x00dev->low_level_stats.dot11ACKFailureCount++;
340         }
341
342         /*
343          * Every single frame has it's own tx status, hence report
344          * every frame as ampdu of size 1.
345          *
346          * TODO: if we can find out how many frames were aggregated
347          * by the hw we could provide the real ampdu_len to mac80211
348          * which would allow the rc algorithm to better decide on
349          * which rates are suitable.
350          */
351         if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
352                 tx_info->flags |= IEEE80211_TX_STAT_AMPDU;
353                 tx_info->status.ampdu_len = 1;
354                 tx_info->status.ampdu_ack_len = success ? 1 : 0;
355         }
356
357         if (rate_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
358                 if (success)
359                         rt2x00dev->low_level_stats.dot11RTSSuccessCount++;
360                 else
361                         rt2x00dev->low_level_stats.dot11RTSFailureCount++;
362         }
363
364         /*
365          * Only send the status report to mac80211 when it's a frame
366          * that originated in mac80211. If this was a extra frame coming
367          * through a mac80211 library call (RTS/CTS) then we should not
368          * send the status report back.
369          */
370         if (!(skbdesc_flags & SKBDESC_NOT_MAC80211)) {
371                 if (test_bit(DRIVER_REQUIRE_TASKLET_CONTEXT, &rt2x00dev->flags))
372                         ieee80211_tx_status(rt2x00dev->hw, entry->skb);
373                 else
374                         ieee80211_tx_status_ni(rt2x00dev->hw, entry->skb);
375         } else
376                 dev_kfree_skb_any(entry->skb);
377
378         /*
379          * Make this entry available for reuse.
380          */
381         entry->skb = NULL;
382         entry->flags = 0;
383
384         rt2x00dev->ops->lib->clear_entry(entry);
385
386         rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
387
388         /*
389          * If the data queue was below the threshold before the txdone
390          * handler we must make sure the packet queue in the mac80211 stack
391          * is reenabled when the txdone handler has finished.
392          */
393         if (!rt2x00queue_threshold(entry->queue))
394                 rt2x00queue_unpause_queue(entry->queue);
395 }
396 EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
397
398 void rt2x00lib_txdone_noinfo(struct queue_entry *entry, u32 status)
399 {
400         struct txdone_entry_desc txdesc;
401
402         txdesc.flags = 0;
403         __set_bit(status, &txdesc.flags);
404         txdesc.retry = 0;
405
406         rt2x00lib_txdone(entry, &txdesc);
407 }
408 EXPORT_SYMBOL_GPL(rt2x00lib_txdone_noinfo);
409
410 static int rt2x00lib_rxdone_read_signal(struct rt2x00_dev *rt2x00dev,
411                                         struct rxdone_entry_desc *rxdesc)
412 {
413         struct ieee80211_supported_band *sband;
414         const struct rt2x00_rate *rate;
415         unsigned int i;
416         int signal = rxdesc->signal;
417         int type = (rxdesc->dev_flags & RXDONE_SIGNAL_MASK);
418
419         switch (rxdesc->rate_mode) {
420         case RATE_MODE_CCK:
421         case RATE_MODE_OFDM:
422                 /*
423                  * For non-HT rates the MCS value needs to contain the
424                  * actually used rate modulation (CCK or OFDM).
425                  */
426                 if (rxdesc->dev_flags & RXDONE_SIGNAL_MCS)
427                         signal = RATE_MCS(rxdesc->rate_mode, signal);
428
429                 sband = &rt2x00dev->bands[rt2x00dev->curr_band];
430                 for (i = 0; i < sband->n_bitrates; i++) {
431                         rate = rt2x00_get_rate(sband->bitrates[i].hw_value);
432                         if (((type == RXDONE_SIGNAL_PLCP) &&
433                              (rate->plcp == signal)) ||
434                             ((type == RXDONE_SIGNAL_BITRATE) &&
435                               (rate->bitrate == signal)) ||
436                             ((type == RXDONE_SIGNAL_MCS) &&
437                               (rate->mcs == signal))) {
438                                 return i;
439                         }
440                 }
441                 break;
442         case RATE_MODE_HT_MIX:
443         case RATE_MODE_HT_GREENFIELD:
444                 if (signal >= 0 && signal <= 76)
445                         return signal;
446                 break;
447         default:
448                 break;
449         }
450
451         WARNING(rt2x00dev, "Frame received with unrecognized signal, "
452                 "mode=0x%.4x, signal=0x%.4x, type=%d.\n",
453                 rxdesc->rate_mode, signal, type);
454         return 0;
455 }
456
457 void rt2x00lib_rxdone(struct queue_entry *entry)
458 {
459         struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
460         struct rxdone_entry_desc rxdesc;
461         struct sk_buff *skb;
462         struct ieee80211_rx_status *rx_status;
463         unsigned int header_length;
464         int rate_idx;
465
466         if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) ||
467             !test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
468                 goto submit_entry;
469
470         if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags))
471                 goto submit_entry;
472
473         /*
474          * Allocate a new sk_buffer. If no new buffer available, drop the
475          * received frame and reuse the existing buffer.
476          */
477         skb = rt2x00queue_alloc_rxskb(entry);
478         if (!skb)
479                 goto submit_entry;
480
481         /*
482          * Unmap the skb.
483          */
484         rt2x00queue_unmap_skb(entry);
485
486         /*
487          * Extract the RXD details.
488          */
489         memset(&rxdesc, 0, sizeof(rxdesc));
490         rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
491
492         /*
493          * The data behind the ieee80211 header must be
494          * aligned on a 4 byte boundary.
495          */
496         header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
497
498         /*
499          * Hardware might have stripped the IV/EIV/ICV data,
500          * in that case it is possible that the data was
501          * provided separately (through hardware descriptor)
502          * in which case we should reinsert the data into the frame.
503          */
504         if ((rxdesc.dev_flags & RXDONE_CRYPTO_IV) &&
505             (rxdesc.flags & RX_FLAG_IV_STRIPPED))
506                 rt2x00crypto_rx_insert_iv(entry->skb, header_length,
507                                           &rxdesc);
508         else if (header_length &&
509                  (rxdesc.size > header_length) &&
510                  (rxdesc.dev_flags & RXDONE_L2PAD))
511                 rt2x00queue_remove_l2pad(entry->skb, header_length);
512         else
513                 rt2x00queue_align_payload(entry->skb, header_length);
514
515         /* Trim buffer to correct size */
516         skb_trim(entry->skb, rxdesc.size);
517
518         /*
519          * Translate the signal to the correct bitrate index.
520          */
521         rate_idx = rt2x00lib_rxdone_read_signal(rt2x00dev, &rxdesc);
522         if (rxdesc.rate_mode == RATE_MODE_HT_MIX ||
523             rxdesc.rate_mode == RATE_MODE_HT_GREENFIELD)
524                 rxdesc.flags |= RX_FLAG_HT;
525
526         /*
527          * Update extra components
528          */
529         rt2x00link_update_stats(rt2x00dev, entry->skb, &rxdesc);
530         rt2x00debug_update_crypto(rt2x00dev, &rxdesc);
531         rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_RXDONE, entry->skb);
532
533         /*
534          * Initialize RX status information, and send frame
535          * to mac80211.
536          */
537         rx_status = IEEE80211_SKB_RXCB(entry->skb);
538         rx_status->mactime = rxdesc.timestamp;
539         rx_status->band = rt2x00dev->curr_band;
540         rx_status->freq = rt2x00dev->curr_freq;
541         rx_status->rate_idx = rate_idx;
542         rx_status->signal = rxdesc.rssi;
543         rx_status->flag = rxdesc.flags;
544         rx_status->antenna = rt2x00dev->link.ant.active.rx;
545
546         ieee80211_rx_ni(rt2x00dev->hw, entry->skb);
547
548         /*
549          * Replace the skb with the freshly allocated one.
550          */
551         entry->skb = skb;
552
553 submit_entry:
554         entry->flags = 0;
555         rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
556         if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) &&
557             test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags)) {
558                 rt2x00dev->ops->lib->clear_entry(entry);
559                 rt2x00queue_index_inc(entry->queue, Q_INDEX);
560         }
561 }
562 EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
563
564 /*
565  * Driver initialization handlers.
566  */
567 const struct rt2x00_rate rt2x00_supported_rates[12] = {
568         {
569                 .flags = DEV_RATE_CCK,
570                 .bitrate = 10,
571                 .ratemask = BIT(0),
572                 .plcp = 0x00,
573                 .mcs = RATE_MCS(RATE_MODE_CCK, 0),
574         },
575         {
576                 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
577                 .bitrate = 20,
578                 .ratemask = BIT(1),
579                 .plcp = 0x01,
580                 .mcs = RATE_MCS(RATE_MODE_CCK, 1),
581         },
582         {
583                 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
584                 .bitrate = 55,
585                 .ratemask = BIT(2),
586                 .plcp = 0x02,
587                 .mcs = RATE_MCS(RATE_MODE_CCK, 2),
588         },
589         {
590                 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
591                 .bitrate = 110,
592                 .ratemask = BIT(3),
593                 .plcp = 0x03,
594                 .mcs = RATE_MCS(RATE_MODE_CCK, 3),
595         },
596         {
597                 .flags = DEV_RATE_OFDM,
598                 .bitrate = 60,
599                 .ratemask = BIT(4),
600                 .plcp = 0x0b,
601                 .mcs = RATE_MCS(RATE_MODE_OFDM, 0),
602         },
603         {
604                 .flags = DEV_RATE_OFDM,
605                 .bitrate = 90,
606                 .ratemask = BIT(5),
607                 .plcp = 0x0f,
608                 .mcs = RATE_MCS(RATE_MODE_OFDM, 1),
609         },
610         {
611                 .flags = DEV_RATE_OFDM,
612                 .bitrate = 120,
613                 .ratemask = BIT(6),
614                 .plcp = 0x0a,
615                 .mcs = RATE_MCS(RATE_MODE_OFDM, 2),
616         },
617         {
618                 .flags = DEV_RATE_OFDM,
619                 .bitrate = 180,
620                 .ratemask = BIT(7),
621                 .plcp = 0x0e,
622                 .mcs = RATE_MCS(RATE_MODE_OFDM, 3),
623         },
624         {
625                 .flags = DEV_RATE_OFDM,
626                 .bitrate = 240,
627                 .ratemask = BIT(8),
628                 .plcp = 0x09,
629                 .mcs = RATE_MCS(RATE_MODE_OFDM, 4),
630         },
631         {
632                 .flags = DEV_RATE_OFDM,
633                 .bitrate = 360,
634                 .ratemask = BIT(9),
635                 .plcp = 0x0d,
636                 .mcs = RATE_MCS(RATE_MODE_OFDM, 5),
637         },
638         {
639                 .flags = DEV_RATE_OFDM,
640                 .bitrate = 480,
641                 .ratemask = BIT(10),
642                 .plcp = 0x08,
643                 .mcs = RATE_MCS(RATE_MODE_OFDM, 6),
644         },
645         {
646                 .flags = DEV_RATE_OFDM,
647                 .bitrate = 540,
648                 .ratemask = BIT(11),
649                 .plcp = 0x0c,
650                 .mcs = RATE_MCS(RATE_MODE_OFDM, 7),
651         },
652 };
653
654 static void rt2x00lib_channel(struct ieee80211_channel *entry,
655                               const int channel, const int tx_power,
656                               const int value)
657 {
658         entry->center_freq = ieee80211_channel_to_frequency(channel);
659         entry->hw_value = value;
660         entry->max_power = tx_power;
661         entry->max_antenna_gain = 0xff;
662 }
663
664 static void rt2x00lib_rate(struct ieee80211_rate *entry,
665                            const u16 index, const struct rt2x00_rate *rate)
666 {
667         entry->flags = 0;
668         entry->bitrate = rate->bitrate;
669         entry->hw_value = index;
670         entry->hw_value_short = index;
671
672         if (rate->flags & DEV_RATE_SHORT_PREAMBLE)
673                 entry->flags |= IEEE80211_RATE_SHORT_PREAMBLE;
674 }
675
676 static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
677                                     struct hw_mode_spec *spec)
678 {
679         struct ieee80211_hw *hw = rt2x00dev->hw;
680         struct ieee80211_channel *channels;
681         struct ieee80211_rate *rates;
682         unsigned int num_rates;
683         unsigned int i;
684
685         num_rates = 0;
686         if (spec->supported_rates & SUPPORT_RATE_CCK)
687                 num_rates += 4;
688         if (spec->supported_rates & SUPPORT_RATE_OFDM)
689                 num_rates += 8;
690
691         channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
692         if (!channels)
693                 return -ENOMEM;
694
695         rates = kzalloc(sizeof(*rates) * num_rates, GFP_KERNEL);
696         if (!rates)
697                 goto exit_free_channels;
698
699         /*
700          * Initialize Rate list.
701          */
702         for (i = 0; i < num_rates; i++)
703                 rt2x00lib_rate(&rates[i], i, rt2x00_get_rate(i));
704
705         /*
706          * Initialize Channel list.
707          */
708         for (i = 0; i < spec->num_channels; i++) {
709                 rt2x00lib_channel(&channels[i],
710                                   spec->channels[i].channel,
711                                   spec->channels_info[i].max_power, i);
712         }
713
714         /*
715          * Intitialize 802.11b, 802.11g
716          * Rates: CCK, OFDM.
717          * Channels: 2.4 GHz
718          */
719         if (spec->supported_bands & SUPPORT_BAND_2GHZ) {
720                 rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_channels = 14;
721                 rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_bitrates = num_rates;
722                 rt2x00dev->bands[IEEE80211_BAND_2GHZ].channels = channels;
723                 rt2x00dev->bands[IEEE80211_BAND_2GHZ].bitrates = rates;
724                 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
725                     &rt2x00dev->bands[IEEE80211_BAND_2GHZ];
726                 memcpy(&rt2x00dev->bands[IEEE80211_BAND_2GHZ].ht_cap,
727                        &spec->ht, sizeof(spec->ht));
728         }
729
730         /*
731          * Intitialize 802.11a
732          * Rates: OFDM.
733          * Channels: OFDM, UNII, HiperLAN2.
734          */
735         if (spec->supported_bands & SUPPORT_BAND_5GHZ) {
736                 rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_channels =
737                     spec->num_channels - 14;
738                 rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_bitrates =
739                     num_rates - 4;
740                 rt2x00dev->bands[IEEE80211_BAND_5GHZ].channels = &channels[14];
741                 rt2x00dev->bands[IEEE80211_BAND_5GHZ].bitrates = &rates[4];
742                 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
743                     &rt2x00dev->bands[IEEE80211_BAND_5GHZ];
744                 memcpy(&rt2x00dev->bands[IEEE80211_BAND_5GHZ].ht_cap,
745                        &spec->ht, sizeof(spec->ht));
746         }
747
748         return 0;
749
750  exit_free_channels:
751         kfree(channels);
752         ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
753         return -ENOMEM;
754 }
755
756 static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
757 {
758         if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
759                 ieee80211_unregister_hw(rt2x00dev->hw);
760
761         if (likely(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ])) {
762                 kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->channels);
763                 kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->bitrates);
764                 rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
765                 rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
766         }
767
768         kfree(rt2x00dev->spec.channels_info);
769 }
770
771 static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
772 {
773         struct hw_mode_spec *spec = &rt2x00dev->spec;
774         int status;
775
776         if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
777                 return 0;
778
779         /*
780          * Initialize HW modes.
781          */
782         status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
783         if (status)
784                 return status;
785
786         /*
787          * Initialize HW fields.
788          */
789         rt2x00dev->hw->queues = rt2x00dev->ops->tx_queues;
790
791         /*
792          * Initialize extra TX headroom required.
793          */
794         rt2x00dev->hw->extra_tx_headroom =
795                 max_t(unsigned int, IEEE80211_TX_STATUS_HEADROOM,
796                       rt2x00dev->ops->extra_tx_headroom);
797
798         /*
799          * Take TX headroom required for alignment into account.
800          */
801         if (test_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags))
802                 rt2x00dev->hw->extra_tx_headroom += RT2X00_L2PAD_SIZE;
803         else if (test_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags))
804                 rt2x00dev->hw->extra_tx_headroom += RT2X00_ALIGN_SIZE;
805
806         /*
807          * Allocate tx status FIFO for driver use.
808          */
809         if (test_bit(DRIVER_REQUIRE_TXSTATUS_FIFO, &rt2x00dev->flags) &&
810             rt2x00dev->ops->lib->txstatus_tasklet) {
811                 /*
812                  * Allocate txstatus fifo and tasklet, we use a size of 512
813                  * for the kfifo which is big enough to store 512/4=128 tx
814                  * status reports. In the worst case (tx status for all tx
815                  * queues gets reported before we've got a chance to handle
816                  * them) 24*4=384 tx status reports need to be cached.
817                  */
818                 status = kfifo_alloc(&rt2x00dev->txstatus_fifo, 512,
819                                      GFP_KERNEL);
820                 if (status)
821                         return status;
822
823                 /* tasklet for processing the tx status reports. */
824                 tasklet_init(&rt2x00dev->txstatus_tasklet,
825                              rt2x00dev->ops->lib->txstatus_tasklet,
826                              (unsigned long)rt2x00dev);
827
828         }
829
830         /*
831          * Register HW.
832          */
833         status = ieee80211_register_hw(rt2x00dev->hw);
834         if (status)
835                 return status;
836
837         set_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags);
838
839         return 0;
840 }
841
842 /*
843  * Initialization/uninitialization handlers.
844  */
845 static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
846 {
847         if (!test_and_clear_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
848                 return;
849
850         /*
851          * Unregister extra components.
852          */
853         rt2x00rfkill_unregister(rt2x00dev);
854
855         /*
856          * Allow the HW to uninitialize.
857          */
858         rt2x00dev->ops->lib->uninitialize(rt2x00dev);
859
860         /*
861          * Free allocated queue entries.
862          */
863         rt2x00queue_uninitialize(rt2x00dev);
864 }
865
866 static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
867 {
868         int status;
869
870         if (test_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
871                 return 0;
872
873         /*
874          * Allocate all queue entries.
875          */
876         status = rt2x00queue_initialize(rt2x00dev);
877         if (status)
878                 return status;
879
880         /*
881          * Initialize the device.
882          */
883         status = rt2x00dev->ops->lib->initialize(rt2x00dev);
884         if (status) {
885                 rt2x00queue_uninitialize(rt2x00dev);
886                 return status;
887         }
888
889         set_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags);
890
891         /*
892          * Register the extra components.
893          */
894         rt2x00rfkill_register(rt2x00dev);
895
896         return 0;
897 }
898
899 int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
900 {
901         int retval;
902
903         if (test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
904                 return 0;
905
906         /*
907          * If this is the first interface which is added,
908          * we should load the firmware now.
909          */
910         retval = rt2x00lib_load_firmware(rt2x00dev);
911         if (retval)
912                 return retval;
913
914         /*
915          * Initialize the device.
916          */
917         retval = rt2x00lib_initialize(rt2x00dev);
918         if (retval)
919                 return retval;
920
921         rt2x00dev->intf_ap_count = 0;
922         rt2x00dev->intf_sta_count = 0;
923         rt2x00dev->intf_associated = 0;
924
925         /* Enable the radio */
926         retval = rt2x00lib_enable_radio(rt2x00dev);
927         if (retval)
928                 return retval;
929
930         set_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags);
931
932         return 0;
933 }
934
935 void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
936 {
937         if (!test_and_clear_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
938                 return;
939
940         /*
941          * Perhaps we can add something smarter here,
942          * but for now just disabling the radio should do.
943          */
944         rt2x00lib_disable_radio(rt2x00dev);
945
946         rt2x00dev->intf_ap_count = 0;
947         rt2x00dev->intf_sta_count = 0;
948         rt2x00dev->intf_associated = 0;
949 }
950
951 /*
952  * driver allocation handlers.
953  */
954 int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
955 {
956         int retval = -ENOMEM;
957
958         mutex_init(&rt2x00dev->csr_mutex);
959
960         set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
961
962         /*
963          * Make room for rt2x00_intf inside the per-interface
964          * structure ieee80211_vif.
965          */
966         rt2x00dev->hw->vif_data_size = sizeof(struct rt2x00_intf);
967
968         /*
969          * Determine which operating modes are supported, all modes
970          * which require beaconing, depend on the availability of
971          * beacon entries.
972          */
973         rt2x00dev->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
974         if (rt2x00dev->ops->bcn->entry_num > 0)
975                 rt2x00dev->hw->wiphy->interface_modes |=
976                     BIT(NL80211_IFTYPE_ADHOC) |
977                     BIT(NL80211_IFTYPE_AP) |
978                     BIT(NL80211_IFTYPE_MESH_POINT) |
979                     BIT(NL80211_IFTYPE_WDS);
980
981         /*
982          * Initialize configuration work.
983          */
984         INIT_WORK(&rt2x00dev->intf_work, rt2x00lib_intf_scheduled);
985
986         /*
987          * Let the driver probe the device to detect the capabilities.
988          */
989         retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
990         if (retval) {
991                 ERROR(rt2x00dev, "Failed to allocate device.\n");
992                 goto exit;
993         }
994
995         /*
996          * Allocate queue array.
997          */
998         retval = rt2x00queue_allocate(rt2x00dev);
999         if (retval)
1000                 goto exit;
1001
1002         /*
1003          * Initialize ieee80211 structure.
1004          */
1005         retval = rt2x00lib_probe_hw(rt2x00dev);
1006         if (retval) {
1007                 ERROR(rt2x00dev, "Failed to initialize hw.\n");
1008                 goto exit;
1009         }
1010
1011         /*
1012          * Register extra components.
1013          */
1014         rt2x00link_register(rt2x00dev);
1015         rt2x00leds_register(rt2x00dev);
1016         rt2x00debug_register(rt2x00dev);
1017
1018         return 0;
1019
1020 exit:
1021         rt2x00lib_remove_dev(rt2x00dev);
1022
1023         return retval;
1024 }
1025 EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
1026
1027 void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
1028 {
1029         clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
1030
1031         /*
1032          * Disable radio.
1033          */
1034         rt2x00lib_disable_radio(rt2x00dev);
1035
1036         /*
1037          * Stop all work.
1038          */
1039         cancel_work_sync(&rt2x00dev->intf_work);
1040         cancel_work_sync(&rt2x00dev->rxdone_work);
1041         cancel_work_sync(&rt2x00dev->txdone_work);
1042
1043         /*
1044          * Free the tx status fifo.
1045          */
1046         kfifo_free(&rt2x00dev->txstatus_fifo);
1047
1048         /*
1049          * Kill the tx status tasklet.
1050          */
1051         tasklet_kill(&rt2x00dev->txstatus_tasklet);
1052
1053         /*
1054          * Uninitialize device.
1055          */
1056         rt2x00lib_uninitialize(rt2x00dev);
1057
1058         /*
1059          * Free extra components
1060          */
1061         rt2x00debug_deregister(rt2x00dev);
1062         rt2x00leds_unregister(rt2x00dev);
1063
1064         /*
1065          * Free ieee80211_hw memory.
1066          */
1067         rt2x00lib_remove_hw(rt2x00dev);
1068
1069         /*
1070          * Free firmware image.
1071          */
1072         rt2x00lib_free_firmware(rt2x00dev);
1073
1074         /*
1075          * Free queue structures.
1076          */
1077         rt2x00queue_free(rt2x00dev);
1078 }
1079 EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
1080
1081 /*
1082  * Device state handlers
1083  */
1084 #ifdef CONFIG_PM
1085 int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
1086 {
1087         NOTICE(rt2x00dev, "Going to sleep.\n");
1088
1089         /*
1090          * Prevent mac80211 from accessing driver while suspended.
1091          */
1092         if (!test_and_clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
1093                 return 0;
1094
1095         /*
1096          * Cleanup as much as possible.
1097          */
1098         rt2x00lib_uninitialize(rt2x00dev);
1099
1100         /*
1101          * Suspend/disable extra components.
1102          */
1103         rt2x00leds_suspend(rt2x00dev);
1104         rt2x00debug_deregister(rt2x00dev);
1105
1106         /*
1107          * Set device mode to sleep for power management,
1108          * on some hardware this call seems to consistently fail.
1109          * From the specifications it is hard to tell why it fails,
1110          * and if this is a "bad thing".
1111          * Overall it is safe to just ignore the failure and
1112          * continue suspending. The only downside is that the
1113          * device will not be in optimal power save mode, but with
1114          * the radio and the other components already disabled the
1115          * device is as good as disabled.
1116          */
1117         if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP))
1118                 WARNING(rt2x00dev, "Device failed to enter sleep state, "
1119                         "continue suspending.\n");
1120
1121         return 0;
1122 }
1123 EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1124
1125 int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1126 {
1127         NOTICE(rt2x00dev, "Waking up.\n");
1128
1129         /*
1130          * Restore/enable extra components.
1131          */
1132         rt2x00debug_register(rt2x00dev);
1133         rt2x00leds_resume(rt2x00dev);
1134
1135         /*
1136          * We are ready again to receive requests from mac80211.
1137          */
1138         set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
1139
1140         return 0;
1141 }
1142 EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1143 #endif /* CONFIG_PM */
1144
1145 /*
1146  * rt2x00lib module information.
1147  */
1148 MODULE_AUTHOR(DRV_PROJECT);
1149 MODULE_VERSION(DRV_VERSION);
1150 MODULE_DESCRIPTION("rt2x00 library");
1151 MODULE_LICENSE("GPL");