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