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