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