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