[PATCH] rt2x00: Fix rfkill handling
[linux-2.6] / drivers / net / wireless / rt2x00 / rt2x00dev.c
1 /*
2         Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
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 /*
27  * Set enviroment defines for rt2x00.h
28  */
29 #define DRV_NAME "rt2x00lib"
30
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33
34 #include "rt2x00.h"
35 #include "rt2x00lib.h"
36
37 /*
38  * Ring handler.
39  */
40 struct data_ring *rt2x00lib_get_ring(struct rt2x00_dev *rt2x00dev,
41                                      const unsigned int queue)
42 {
43         int beacon = test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags);
44
45         /*
46          * Check if we are requesting a reqular TX ring,
47          * or if we are requesting a Beacon or Atim ring.
48          * For Atim rings, we should check if it is supported.
49          */
50         if (queue < rt2x00dev->hw->queues && rt2x00dev->tx)
51                 return &rt2x00dev->tx[queue];
52
53         if (!rt2x00dev->bcn || !beacon)
54                 return NULL;
55
56         if (queue == IEEE80211_TX_QUEUE_BEACON)
57                 return &rt2x00dev->bcn[0];
58         else if (queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
59                 return &rt2x00dev->bcn[1];
60
61         return NULL;
62 }
63 EXPORT_SYMBOL_GPL(rt2x00lib_get_ring);
64
65 /*
66  * Link tuning handlers
67  */
68 static void rt2x00lib_start_link_tuner(struct rt2x00_dev *rt2x00dev)
69 {
70         rt2x00_clear_link(&rt2x00dev->link);
71
72         /*
73          * Reset the link tuner.
74          */
75         rt2x00dev->ops->lib->reset_tuner(rt2x00dev);
76
77         queue_delayed_work(rt2x00dev->hw->workqueue,
78                            &rt2x00dev->link.work, LINK_TUNE_INTERVAL);
79 }
80
81 static void rt2x00lib_stop_link_tuner(struct rt2x00_dev *rt2x00dev)
82 {
83         cancel_delayed_work_sync(&rt2x00dev->link.work);
84 }
85
86 void rt2x00lib_reset_link_tuner(struct rt2x00_dev *rt2x00dev)
87 {
88         if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
89                 return;
90
91         rt2x00lib_stop_link_tuner(rt2x00dev);
92         rt2x00lib_start_link_tuner(rt2x00dev);
93 }
94
95 /*
96  * Radio control handlers.
97  */
98 int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
99 {
100         int status;
101
102         /*
103          * Don't enable the radio twice.
104          * And check if the hardware button has been disabled.
105          */
106         if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
107             test_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags))
108                 return 0;
109
110         /*
111          * Enable radio.
112          */
113         status = rt2x00dev->ops->lib->set_device_state(rt2x00dev,
114                                                        STATE_RADIO_ON);
115         if (status)
116                 return status;
117
118         __set_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags);
119
120         /*
121          * Enable RX.
122          */
123         rt2x00lib_toggle_rx(rt2x00dev, 1);
124
125         /*
126          * Start the TX queues.
127          */
128         ieee80211_start_queues(rt2x00dev->hw);
129
130         return 0;
131 }
132
133 void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
134 {
135         if (!__test_and_clear_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
136                 return;
137
138         /*
139          * Stop all scheduled work.
140          */
141         if (work_pending(&rt2x00dev->beacon_work))
142                 cancel_work_sync(&rt2x00dev->beacon_work);
143         if (work_pending(&rt2x00dev->filter_work))
144                 cancel_work_sync(&rt2x00dev->filter_work);
145
146         /*
147          * Stop the TX queues.
148          */
149         ieee80211_stop_queues(rt2x00dev->hw);
150
151         /*
152          * Disable RX.
153          */
154         rt2x00lib_toggle_rx(rt2x00dev, 0);
155
156         /*
157          * Disable radio.
158          */
159         rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
160 }
161
162 void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, int enable)
163 {
164         enum dev_state state = enable ? STATE_RADIO_RX_ON : STATE_RADIO_RX_OFF;
165
166         /*
167          * When we are disabling the RX, we should also stop the link tuner.
168          */
169         if (!enable)
170                 rt2x00lib_stop_link_tuner(rt2x00dev);
171
172         rt2x00dev->ops->lib->set_device_state(rt2x00dev, state);
173
174         /*
175          * When we are enabling the RX, we should also start the link tuner.
176          */
177         if (enable && is_interface_present(&rt2x00dev->interface))
178                 rt2x00lib_start_link_tuner(rt2x00dev);
179 }
180
181 static void rt2x00lib_precalculate_link_signal(struct link *link)
182 {
183         if (link->rx_failed || link->rx_success)
184                 link->rx_percentage =
185                     (link->rx_success * 100) /
186                     (link->rx_failed + link->rx_success);
187         else
188                 link->rx_percentage = 50;
189
190         if (link->tx_failed || link->tx_success)
191                 link->tx_percentage =
192                     (link->tx_success * 100) /
193                     (link->tx_failed + link->tx_success);
194         else
195                 link->tx_percentage = 50;
196
197         link->rx_success = 0;
198         link->rx_failed = 0;
199         link->tx_success = 0;
200         link->tx_failed = 0;
201 }
202
203 static int rt2x00lib_calculate_link_signal(struct rt2x00_dev *rt2x00dev,
204                                            int rssi)
205 {
206         int rssi_percentage = 0;
207         int signal;
208
209         /*
210          * We need a positive value for the RSSI.
211          */
212         if (rssi < 0)
213                 rssi += rt2x00dev->rssi_offset;
214
215         /*
216          * Calculate the different percentages,
217          * which will be used for the signal.
218          */
219         if (rt2x00dev->rssi_offset)
220                 rssi_percentage = (rssi * 100) / rt2x00dev->rssi_offset;
221
222         /*
223          * Add the individual percentages and use the WEIGHT
224          * defines to calculate the current link signal.
225          */
226         signal = ((WEIGHT_RSSI * rssi_percentage) +
227                   (WEIGHT_TX * rt2x00dev->link.tx_percentage) +
228                   (WEIGHT_RX * rt2x00dev->link.rx_percentage)) / 100;
229
230         return (signal > 100) ? 100 : signal;
231 }
232
233 static void rt2x00lib_link_tuner(struct work_struct *work)
234 {
235         struct rt2x00_dev *rt2x00dev =
236             container_of(work, struct rt2x00_dev, link.work.work);
237
238         /*
239          * When the radio is shutting down we should
240          * immediately cease all link tuning.
241          */
242         if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
243                 return;
244
245         /*
246          * Update statistics.
247          */
248         rt2x00dev->ops->lib->link_stats(rt2x00dev);
249
250         rt2x00dev->low_level_stats.dot11FCSErrorCount +=
251             rt2x00dev->link.rx_failed;
252
253         /*
254          * Only perform the link tuning when Link tuning
255          * has been enabled (This could have been disabled from the EEPROM).
256          */
257         if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
258                 rt2x00dev->ops->lib->link_tuner(rt2x00dev);
259
260         /*
261          * Precalculate a portion of the link signal which is
262          * in based on the tx/rx success/failure counters.
263          */
264         rt2x00lib_precalculate_link_signal(&rt2x00dev->link);
265
266         /*
267          * Increase tuner counter, and reschedule the next link tuner run.
268          */
269         rt2x00dev->link.count++;
270         queue_delayed_work(rt2x00dev->hw->workqueue, &rt2x00dev->link.work,
271                            LINK_TUNE_INTERVAL);
272 }
273
274 static void rt2x00lib_packetfilter_scheduled(struct work_struct *work)
275 {
276         struct rt2x00_dev *rt2x00dev =
277             container_of(work, struct rt2x00_dev, filter_work);
278         unsigned int filter = rt2x00dev->interface.filter;
279
280         /*
281          * Since we had stored the filter inside interface.filter,
282          * we should now clear that field. Otherwise the driver will
283          * assume nothing has changed (*total_flags will be compared
284          * to interface.filter to determine if any action is required).
285          */
286         rt2x00dev->interface.filter = 0;
287
288         rt2x00dev->ops->hw->configure_filter(rt2x00dev->hw,
289                                              filter, &filter, 0, NULL);
290 }
291
292 /*
293  * Interrupt context handlers.
294  */
295 static void rt2x00lib_beacondone_scheduled(struct work_struct *work)
296 {
297         struct rt2x00_dev *rt2x00dev =
298             container_of(work, struct rt2x00_dev, beacon_work);
299         struct data_ring *ring =
300             rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_BEACON);
301         struct data_entry *entry = rt2x00_get_data_entry(ring);
302         struct sk_buff *skb;
303
304         skb = ieee80211_beacon_get(rt2x00dev->hw,
305                                    rt2x00dev->interface.id,
306                                    &entry->tx_status.control);
307         if (!skb)
308                 return;
309
310         rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw, skb,
311                                           &entry->tx_status.control);
312
313         dev_kfree_skb(skb);
314 }
315
316 void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
317 {
318         if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
319                 return;
320
321         queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->beacon_work);
322 }
323 EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
324
325 void rt2x00lib_txdone(struct data_entry *entry,
326                       const int status, const int retry)
327 {
328         struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
329         struct ieee80211_tx_status *tx_status = &entry->tx_status;
330         struct ieee80211_low_level_stats *stats = &rt2x00dev->low_level_stats;
331         int success = !!(status == TX_SUCCESS || status == TX_SUCCESS_RETRY);
332         int fail = !!(status == TX_FAIL_RETRY || status == TX_FAIL_INVALID ||
333                       status == TX_FAIL_OTHER);
334
335         /*
336          * Update TX statistics.
337          */
338         tx_status->flags = 0;
339         tx_status->ack_signal = 0;
340         tx_status->excessive_retries = (status == TX_FAIL_RETRY);
341         tx_status->retry_count = retry;
342         rt2x00dev->link.tx_success += success;
343         rt2x00dev->link.tx_failed += retry + fail;
344
345         if (!(tx_status->control.flags & IEEE80211_TXCTL_NO_ACK)) {
346                 if (success)
347                         tx_status->flags |= IEEE80211_TX_STATUS_ACK;
348                 else
349                         stats->dot11ACKFailureCount++;
350         }
351
352         tx_status->queue_length = entry->ring->stats.limit;
353         tx_status->queue_number = tx_status->control.queue;
354
355         if (tx_status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) {
356                 if (success)
357                         stats->dot11RTSSuccessCount++;
358                 else
359                         stats->dot11RTSFailureCount++;
360         }
361
362         /*
363          * Send the tx_status to mac80211,
364          * that method also cleans up the skb structure.
365          */
366         ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb, tx_status);
367         entry->skb = NULL;
368 }
369 EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
370
371 void rt2x00lib_rxdone(struct data_entry *entry, struct sk_buff *skb,
372                       struct rxdata_entry_desc *desc)
373 {
374         struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
375         struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
376         struct ieee80211_hw_mode *mode;
377         struct ieee80211_rate *rate;
378         unsigned int i;
379         int val = 0;
380
381         /*
382          * Update RX statistics.
383          */
384         mode = &rt2x00dev->hwmodes[rt2x00dev->curr_hwmode];
385         for (i = 0; i < mode->num_rates; i++) {
386                 rate = &mode->rates[i];
387
388                 /*
389                  * When frame was received with an OFDM bitrate,
390                  * the signal is the PLCP value. If it was received with
391                  * a CCK bitrate the signal is the rate in 0.5kbit/s.
392                  */
393                 if (!desc->ofdm)
394                         val = DEVICE_GET_RATE_FIELD(rate->val, RATE);
395                 else
396                         val = DEVICE_GET_RATE_FIELD(rate->val, PLCP);
397
398                 if (val == desc->signal) {
399                         val = rate->val;
400                         break;
401                 }
402         }
403
404         rt2x00_update_link_rssi(&rt2x00dev->link, desc->rssi);
405         rt2x00dev->link.rx_success++;
406         rx_status->rate = val;
407         rx_status->signal =
408             rt2x00lib_calculate_link_signal(rt2x00dev, desc->rssi);
409         rx_status->ssi = desc->rssi;
410         rx_status->flag = desc->flags;
411
412         /*
413          * Send frame to mac80211
414          */
415         ieee80211_rx_irqsafe(rt2x00dev->hw, skb, rx_status);
416 }
417 EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
418
419 /*
420  * TX descriptor initializer
421  */
422 void rt2x00lib_write_tx_desc(struct rt2x00_dev *rt2x00dev,
423                              struct data_desc *txd,
424                              struct ieee80211_hdr *ieee80211hdr,
425                              unsigned int length,
426                              struct ieee80211_tx_control *control)
427 {
428         struct txdata_entry_desc desc;
429         struct data_ring *ring;
430         int tx_rate;
431         int bitrate;
432         int duration;
433         int residual;
434         u16 frame_control;
435         u16 seq_ctrl;
436
437         /*
438          * Make sure the descriptor is properly cleared.
439          */
440         memset(&desc, 0x00, sizeof(desc));
441
442         /*
443          * Get ring pointer, if we fail to obtain the
444          * correct ring, then use the first TX ring.
445          */
446         ring = rt2x00lib_get_ring(rt2x00dev, control->queue);
447         if (!ring)
448                 ring = rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_DATA0);
449
450         desc.cw_min = ring->tx_params.cw_min;
451         desc.cw_max = ring->tx_params.cw_max;
452         desc.aifs = ring->tx_params.aifs;
453
454         /*
455          * Identify queue
456          */
457         if (control->queue < rt2x00dev->hw->queues)
458                 desc.queue = control->queue;
459         else if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
460                  control->queue == IEEE80211_TX_QUEUE_AFTER_BEACON)
461                 desc.queue = QUEUE_MGMT;
462         else
463                 desc.queue = QUEUE_OTHER;
464
465         /*
466          * Read required fields from ieee80211 header.
467          */
468         frame_control = le16_to_cpu(ieee80211hdr->frame_control);
469         seq_ctrl = le16_to_cpu(ieee80211hdr->seq_ctrl);
470
471         tx_rate = control->tx_rate;
472
473         /*
474          * Check if this is a RTS/CTS frame
475          */
476         if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) {
477                 __set_bit(ENTRY_TXD_BURST, &desc.flags);
478                 if (is_rts_frame(frame_control))
479                         __set_bit(ENTRY_TXD_RTS_FRAME, &desc.flags);
480                 if (control->rts_cts_rate)
481                         tx_rate = control->rts_cts_rate;
482         }
483
484         /*
485          * Check for OFDM
486          */
487         if (DEVICE_GET_RATE_FIELD(tx_rate, RATEMASK) & DEV_OFDM_RATEMASK)
488                 __set_bit(ENTRY_TXD_OFDM_RATE, &desc.flags);
489
490         /*
491          * Check if more fragments are pending
492          */
493         if (ieee80211_get_morefrag(ieee80211hdr)) {
494                 __set_bit(ENTRY_TXD_BURST, &desc.flags);
495                 __set_bit(ENTRY_TXD_MORE_FRAG, &desc.flags);
496         }
497
498         /*
499          * Beacons and probe responses require the tsf timestamp
500          * to be inserted into the frame.
501          */
502         if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
503             is_probe_resp(frame_control))
504                 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &desc.flags);
505
506         /*
507          * Determine with what IFS priority this frame should be send.
508          * Set ifs to IFS_SIFS when the this is not the first fragment,
509          * or this fragment came after RTS/CTS.
510          */
511         if ((seq_ctrl & IEEE80211_SCTL_FRAG) > 0 ||
512             test_bit(ENTRY_TXD_RTS_FRAME, &desc.flags))
513                 desc.ifs = IFS_SIFS;
514         else
515                 desc.ifs = IFS_BACKOFF;
516
517         /*
518          * PLCP setup
519          * Length calculation depends on OFDM/CCK rate.
520          */
521         desc.signal = DEVICE_GET_RATE_FIELD(tx_rate, PLCP);
522         desc.service = 0x04;
523
524         if (test_bit(ENTRY_TXD_OFDM_RATE, &desc.flags)) {
525                 desc.length_high = ((length + FCS_LEN) >> 6) & 0x3f;
526                 desc.length_low = ((length + FCS_LEN) & 0x3f);
527         } else {
528                 bitrate = DEVICE_GET_RATE_FIELD(tx_rate, RATE);
529
530                 /*
531                  * Convert length to microseconds.
532                  */
533                 residual = get_duration_res(length + FCS_LEN, bitrate);
534                 duration = get_duration(length + FCS_LEN, bitrate);
535
536                 if (residual != 0) {
537                         duration++;
538
539                         /*
540                          * Check if we need to set the Length Extension
541                          */
542                         if (bitrate == 110 && residual <= 3)
543                                 desc.service |= 0x80;
544                 }
545
546                 desc.length_high = (duration >> 8) & 0xff;
547                 desc.length_low = duration & 0xff;
548
549                 /*
550                  * When preamble is enabled we should set the
551                  * preamble bit for the signal.
552                  */
553                 if (DEVICE_GET_RATE_FIELD(tx_rate, PREAMBLE))
554                         desc.signal |= 0x08;
555         }
556
557         rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, txd, &desc,
558                                            ieee80211hdr, length, control);
559 }
560 EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc);
561
562 /*
563  * Driver initialization handlers.
564  */
565 static void rt2x00lib_channel(struct ieee80211_channel *entry,
566                               const int channel, const int tx_power,
567                               const int value)
568 {
569         entry->chan = channel;
570         if (channel <= 14)
571                 entry->freq = 2407 + (5 * channel);
572         else
573                 entry->freq = 5000 + (5 * channel);
574         entry->val = value;
575         entry->flag =
576             IEEE80211_CHAN_W_IBSS |
577             IEEE80211_CHAN_W_ACTIVE_SCAN |
578             IEEE80211_CHAN_W_SCAN;
579         entry->power_level = tx_power;
580         entry->antenna_max = 0xff;
581 }
582
583 static void rt2x00lib_rate(struct ieee80211_rate *entry,
584                            const int rate, const int mask,
585                            const int plcp, const int flags)
586 {
587         entry->rate = rate;
588         entry->val =
589             DEVICE_SET_RATE_FIELD(rate, RATE) |
590             DEVICE_SET_RATE_FIELD(mask, RATEMASK) |
591             DEVICE_SET_RATE_FIELD(plcp, PLCP);
592         entry->flags = flags;
593         entry->val2 = entry->val;
594         if (entry->flags & IEEE80211_RATE_PREAMBLE2)
595                 entry->val2 |= DEVICE_SET_RATE_FIELD(1, PREAMBLE);
596         entry->min_rssi_ack = 0;
597         entry->min_rssi_ack_delta = 0;
598 }
599
600 static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
601                                     struct hw_mode_spec *spec)
602 {
603         struct ieee80211_hw *hw = rt2x00dev->hw;
604         struct ieee80211_hw_mode *hwmodes;
605         struct ieee80211_channel *channels;
606         struct ieee80211_rate *rates;
607         unsigned int i;
608         unsigned char tx_power;
609
610         hwmodes = kzalloc(sizeof(*hwmodes) * spec->num_modes, GFP_KERNEL);
611         if (!hwmodes)
612                 goto exit;
613
614         channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
615         if (!channels)
616                 goto exit_free_modes;
617
618         rates = kzalloc(sizeof(*rates) * spec->num_rates, GFP_KERNEL);
619         if (!rates)
620                 goto exit_free_channels;
621
622         /*
623          * Initialize Rate list.
624          */
625         rt2x00lib_rate(&rates[0], 10, DEV_RATEMASK_1MB,
626                        0x00, IEEE80211_RATE_CCK);
627         rt2x00lib_rate(&rates[1], 20, DEV_RATEMASK_2MB,
628                        0x01, IEEE80211_RATE_CCK_2);
629         rt2x00lib_rate(&rates[2], 55, DEV_RATEMASK_5_5MB,
630                        0x02, IEEE80211_RATE_CCK_2);
631         rt2x00lib_rate(&rates[3], 110, DEV_RATEMASK_11MB,
632                        0x03, IEEE80211_RATE_CCK_2);
633
634         if (spec->num_rates > 4) {
635                 rt2x00lib_rate(&rates[4], 60, DEV_RATEMASK_6MB,
636                                0x0b, IEEE80211_RATE_OFDM);
637                 rt2x00lib_rate(&rates[5], 90, DEV_RATEMASK_9MB,
638                                0x0f, IEEE80211_RATE_OFDM);
639                 rt2x00lib_rate(&rates[6], 120, DEV_RATEMASK_12MB,
640                                0x0a, IEEE80211_RATE_OFDM);
641                 rt2x00lib_rate(&rates[7], 180, DEV_RATEMASK_18MB,
642                                0x0e, IEEE80211_RATE_OFDM);
643                 rt2x00lib_rate(&rates[8], 240, DEV_RATEMASK_24MB,
644                                0x09, IEEE80211_RATE_OFDM);
645                 rt2x00lib_rate(&rates[9], 360, DEV_RATEMASK_36MB,
646                                0x0d, IEEE80211_RATE_OFDM);
647                 rt2x00lib_rate(&rates[10], 480, DEV_RATEMASK_48MB,
648                                0x08, IEEE80211_RATE_OFDM);
649                 rt2x00lib_rate(&rates[11], 540, DEV_RATEMASK_54MB,
650                                0x0c, IEEE80211_RATE_OFDM);
651         }
652
653         /*
654          * Initialize Channel list.
655          */
656         for (i = 0; i < spec->num_channels; i++) {
657                 if (spec->channels[i].channel <= 14)
658                         tx_power = spec->tx_power_bg[i];
659                 else if (spec->tx_power_a)
660                         tx_power = spec->tx_power_a[i];
661                 else
662                         tx_power = spec->tx_power_default;
663
664                 rt2x00lib_channel(&channels[i],
665                                   spec->channels[i].channel, tx_power, i);
666         }
667
668         /*
669          * Intitialize 802.11b
670          * Rates: CCK.
671          * Channels: OFDM.
672          */
673         if (spec->num_modes > HWMODE_B) {
674                 hwmodes[HWMODE_B].mode = MODE_IEEE80211B;
675                 hwmodes[HWMODE_B].num_channels = 14;
676                 hwmodes[HWMODE_B].num_rates = 4;
677                 hwmodes[HWMODE_B].channels = channels;
678                 hwmodes[HWMODE_B].rates = rates;
679         }
680
681         /*
682          * Intitialize 802.11g
683          * Rates: CCK, OFDM.
684          * Channels: OFDM.
685          */
686         if (spec->num_modes > HWMODE_G) {
687                 hwmodes[HWMODE_G].mode = MODE_IEEE80211G;
688                 hwmodes[HWMODE_G].num_channels = 14;
689                 hwmodes[HWMODE_G].num_rates = spec->num_rates;
690                 hwmodes[HWMODE_G].channels = channels;
691                 hwmodes[HWMODE_G].rates = rates;
692         }
693
694         /*
695          * Intitialize 802.11a
696          * Rates: OFDM.
697          * Channels: OFDM, UNII, HiperLAN2.
698          */
699         if (spec->num_modes > HWMODE_A) {
700                 hwmodes[HWMODE_A].mode = MODE_IEEE80211A;
701                 hwmodes[HWMODE_A].num_channels = spec->num_channels - 14;
702                 hwmodes[HWMODE_A].num_rates = spec->num_rates - 4;
703                 hwmodes[HWMODE_A].channels = &channels[14];
704                 hwmodes[HWMODE_A].rates = &rates[4];
705         }
706
707         if (spec->num_modes > HWMODE_G &&
708             ieee80211_register_hwmode(hw, &hwmodes[HWMODE_G]))
709                 goto exit_free_rates;
710
711         if (spec->num_modes > HWMODE_B &&
712             ieee80211_register_hwmode(hw, &hwmodes[HWMODE_B]))
713                 goto exit_free_rates;
714
715         if (spec->num_modes > HWMODE_A &&
716             ieee80211_register_hwmode(hw, &hwmodes[HWMODE_A]))
717                 goto exit_free_rates;
718
719         rt2x00dev->hwmodes = hwmodes;
720
721         return 0;
722
723 exit_free_rates:
724         kfree(rates);
725
726 exit_free_channels:
727         kfree(channels);
728
729 exit_free_modes:
730         kfree(hwmodes);
731
732 exit:
733         ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
734         return -ENOMEM;
735 }
736
737 static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
738 {
739         if (test_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags))
740                 ieee80211_unregister_hw(rt2x00dev->hw);
741
742         if (likely(rt2x00dev->hwmodes)) {
743                 kfree(rt2x00dev->hwmodes->channels);
744                 kfree(rt2x00dev->hwmodes->rates);
745                 kfree(rt2x00dev->hwmodes);
746                 rt2x00dev->hwmodes = NULL;
747         }
748 }
749
750 static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
751 {
752         struct hw_mode_spec *spec = &rt2x00dev->spec;
753         int status;
754
755         /*
756          * Initialize HW modes.
757          */
758         status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
759         if (status)
760                 return status;
761
762         /*
763          * Register HW.
764          */
765         status = ieee80211_register_hw(rt2x00dev->hw);
766         if (status) {
767                 rt2x00lib_remove_hw(rt2x00dev);
768                 return status;
769         }
770
771         __set_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags);
772
773         return 0;
774 }
775
776 /*
777  * Initialization/uninitialization handlers.
778  */
779 static int rt2x00lib_alloc_entries(struct data_ring *ring,
780                                    const u16 max_entries, const u16 data_size,
781                                    const u16 desc_size)
782 {
783         struct data_entry *entry;
784         unsigned int i;
785
786         ring->stats.limit = max_entries;
787         ring->data_size = data_size;
788         ring->desc_size = desc_size;
789
790         /*
791          * Allocate all ring entries.
792          */
793         entry = kzalloc(ring->stats.limit * sizeof(*entry), GFP_KERNEL);
794         if (!entry)
795                 return -ENOMEM;
796
797         for (i = 0; i < ring->stats.limit; i++) {
798                 entry[i].flags = 0;
799                 entry[i].ring = ring;
800                 entry[i].skb = NULL;
801         }
802
803         ring->entry = entry;
804
805         return 0;
806 }
807
808 static int rt2x00lib_alloc_ring_entries(struct rt2x00_dev *rt2x00dev)
809 {
810         struct data_ring *ring;
811
812         /*
813          * Allocate the RX ring.
814          */
815         if (rt2x00lib_alloc_entries(rt2x00dev->rx, RX_ENTRIES, DATA_FRAME_SIZE,
816                                     rt2x00dev->ops->rxd_size))
817                 return -ENOMEM;
818
819         /*
820          * First allocate the TX rings.
821          */
822         txring_for_each(rt2x00dev, ring) {
823                 if (rt2x00lib_alloc_entries(ring, TX_ENTRIES, DATA_FRAME_SIZE,
824                                             rt2x00dev->ops->txd_size))
825                         return -ENOMEM;
826         }
827
828         if (!test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
829                 return 0;
830
831         /*
832          * Allocate the BEACON ring.
833          */
834         if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[0], BEACON_ENTRIES,
835                                     MGMT_FRAME_SIZE, rt2x00dev->ops->txd_size))
836                 return -ENOMEM;
837
838         /*
839          * Allocate the Atim ring.
840          */
841         if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[1], ATIM_ENTRIES,
842                                     DATA_FRAME_SIZE, rt2x00dev->ops->txd_size))
843                 return -ENOMEM;
844
845         return 0;
846 }
847
848 static void rt2x00lib_free_ring_entries(struct rt2x00_dev *rt2x00dev)
849 {
850         struct data_ring *ring;
851
852         ring_for_each(rt2x00dev, ring) {
853                 kfree(ring->entry);
854                 ring->entry = NULL;
855         }
856 }
857
858 void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
859 {
860         if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
861                 return;
862
863         /*
864          * Unregister rfkill.
865          */
866         rt2x00rfkill_unregister(rt2x00dev);
867
868         /*
869          * Allow the HW to uninitialize.
870          */
871         rt2x00dev->ops->lib->uninitialize(rt2x00dev);
872
873         /*
874          * Free allocated ring entries.
875          */
876         rt2x00lib_free_ring_entries(rt2x00dev);
877 }
878
879 int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
880 {
881         int status;
882
883         if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
884                 return 0;
885
886         /*
887          * Allocate all ring entries.
888          */
889         status = rt2x00lib_alloc_ring_entries(rt2x00dev);
890         if (status) {
891                 ERROR(rt2x00dev, "Ring entries allocation failed.\n");
892                 return status;
893         }
894
895         /*
896          * Initialize the device.
897          */
898         status = rt2x00dev->ops->lib->initialize(rt2x00dev);
899         if (status)
900                 goto exit;
901
902         __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
903
904         /*
905          * Register the rfkill handler.
906          */
907         status = rt2x00rfkill_register(rt2x00dev);
908         if (status)
909                 goto exit_unitialize;
910
911         return 0;
912
913 exit_unitialize:
914         rt2x00lib_uninitialize(rt2x00dev);
915
916 exit:
917         rt2x00lib_free_ring_entries(rt2x00dev);
918
919         return status;
920 }
921
922 /*
923  * driver allocation handlers.
924  */
925 static int rt2x00lib_alloc_rings(struct rt2x00_dev *rt2x00dev)
926 {
927         struct data_ring *ring;
928
929         /*
930          * We need the following rings:
931          * RX: 1
932          * TX: hw->queues
933          * Beacon: 1 (if required)
934          * Atim: 1 (if required)
935          */
936         rt2x00dev->data_rings = 1 + rt2x00dev->hw->queues +
937             (2 * test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags));
938
939         ring = kzalloc(rt2x00dev->data_rings * sizeof(*ring), GFP_KERNEL);
940         if (!ring) {
941                 ERROR(rt2x00dev, "Ring allocation failed.\n");
942                 return -ENOMEM;
943         }
944
945         /*
946          * Initialize pointers
947          */
948         rt2x00dev->rx = ring;
949         rt2x00dev->tx = &rt2x00dev->rx[1];
950         if (test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags))
951                 rt2x00dev->bcn = &rt2x00dev->tx[rt2x00dev->hw->queues];
952
953         /*
954          * Initialize ring parameters.
955          * cw_min: 2^5 = 32.
956          * cw_max: 2^10 = 1024.
957          */
958         ring_for_each(rt2x00dev, ring) {
959                 ring->rt2x00dev = rt2x00dev;
960                 ring->tx_params.aifs = 2;
961                 ring->tx_params.cw_min = 5;
962                 ring->tx_params.cw_max = 10;
963         }
964
965         return 0;
966 }
967
968 static void rt2x00lib_free_rings(struct rt2x00_dev *rt2x00dev)
969 {
970         kfree(rt2x00dev->rx);
971         rt2x00dev->rx = NULL;
972         rt2x00dev->tx = NULL;
973         rt2x00dev->bcn = NULL;
974 }
975
976 int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
977 {
978         int retval = -ENOMEM;
979
980         /*
981          * Let the driver probe the device to detect the capabilities.
982          */
983         retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
984         if (retval) {
985                 ERROR(rt2x00dev, "Failed to allocate device.\n");
986                 goto exit;
987         }
988
989         /*
990          * Initialize configuration work.
991          */
992         INIT_WORK(&rt2x00dev->beacon_work, rt2x00lib_beacondone_scheduled);
993         INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled);
994         INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
995
996         /*
997          * Reset current working type.
998          */
999         rt2x00dev->interface.type = INVALID_INTERFACE;
1000
1001         /*
1002          * Allocate ring array.
1003          */
1004         retval = rt2x00lib_alloc_rings(rt2x00dev);
1005         if (retval)
1006                 goto exit;
1007
1008         /*
1009          * Initialize ieee80211 structure.
1010          */
1011         retval = rt2x00lib_probe_hw(rt2x00dev);
1012         if (retval) {
1013                 ERROR(rt2x00dev, "Failed to initialize hw.\n");
1014                 goto exit;
1015         }
1016
1017         /*
1018          * Allocatie rfkill.
1019          */
1020         retval = rt2x00rfkill_allocate(rt2x00dev);
1021         if (retval)
1022                 goto exit;
1023
1024         /*
1025          * Open the debugfs entry.
1026          */
1027         rt2x00debug_register(rt2x00dev);
1028
1029         __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1030
1031         return 0;
1032
1033 exit:
1034         rt2x00lib_remove_dev(rt2x00dev);
1035
1036         return retval;
1037 }
1038 EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
1039
1040 void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
1041 {
1042         __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1043
1044         /*
1045          * Disable radio.
1046          */
1047         rt2x00lib_disable_radio(rt2x00dev);
1048
1049         /*
1050          * Uninitialize device.
1051          */
1052         rt2x00lib_uninitialize(rt2x00dev);
1053
1054         /*
1055          * Close debugfs entry.
1056          */
1057         rt2x00debug_deregister(rt2x00dev);
1058
1059         /*
1060          * Free rfkill
1061          */
1062         rt2x00rfkill_free(rt2x00dev);
1063
1064         /*
1065          * Free ieee80211_hw memory.
1066          */
1067         rt2x00lib_remove_hw(rt2x00dev);
1068
1069         /*
1070          * Free firmware image.
1071          */
1072         rt2x00lib_free_firmware(rt2x00dev);
1073
1074         /*
1075          * Free ring structures.
1076          */
1077         rt2x00lib_free_rings(rt2x00dev);
1078 }
1079 EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
1080
1081 /*
1082  * Device state handlers
1083  */
1084 #ifdef CONFIG_PM
1085 int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state)
1086 {
1087         int retval;
1088
1089         NOTICE(rt2x00dev, "Going to sleep.\n");
1090         __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1091
1092         /*
1093          * Only continue if mac80211 has open interfaces.
1094          */
1095         if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags))
1096                 goto exit;
1097         __set_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags);
1098
1099         /*
1100          * Disable radio and unitialize all items
1101          * that must be recreated on resume.
1102          */
1103         rt2x00mac_stop(rt2x00dev->hw);
1104         rt2x00lib_uninitialize(rt2x00dev);
1105         rt2x00debug_deregister(rt2x00dev);
1106
1107 exit:
1108         /*
1109          * Set device mode to sleep for power management.
1110          */
1111         retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
1112         if (retval)
1113                 return retval;
1114
1115         return 0;
1116 }
1117 EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
1118
1119 int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
1120 {
1121         struct interface *intf = &rt2x00dev->interface;
1122         int retval;
1123
1124         NOTICE(rt2x00dev, "Waking up.\n");
1125         __set_bit(DEVICE_PRESENT, &rt2x00dev->flags);
1126
1127         /*
1128          * Open the debugfs entry.
1129          */
1130         rt2x00debug_register(rt2x00dev);
1131
1132         /*
1133          * Only continue if mac80211 had open interfaces.
1134          */
1135         if (!__test_and_clear_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags))
1136                 return 0;
1137
1138         /*
1139          * Reinitialize device and all active interfaces.
1140          */
1141         retval = rt2x00mac_start(rt2x00dev->hw);
1142         if (retval)
1143                 goto exit;
1144
1145         /*
1146          * Reconfigure device.
1147          */
1148         rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1);
1149         if (!rt2x00dev->hw->conf.radio_enabled)
1150                 rt2x00lib_disable_radio(rt2x00dev);
1151
1152         rt2x00lib_config_mac_addr(rt2x00dev, intf->mac);
1153         rt2x00lib_config_bssid(rt2x00dev, intf->bssid);
1154         rt2x00lib_config_type(rt2x00dev, intf->type);
1155
1156         /*
1157          * It is possible that during that mac80211 has attempted
1158          * to send frames while we were suspending or resuming.
1159          * In that case we have disabled the TX queue and should
1160          * now enable it again
1161          */
1162         ieee80211_start_queues(rt2x00dev->hw);
1163
1164         /*
1165          * When in Master or Ad-hoc mode,
1166          * restart Beacon transmitting by faking a beacondone event.
1167          */
1168         if (intf->type == IEEE80211_IF_TYPE_AP ||
1169             intf->type == IEEE80211_IF_TYPE_IBSS)
1170                 rt2x00lib_beacondone(rt2x00dev);
1171
1172         return 0;
1173
1174 exit:
1175         rt2x00lib_disable_radio(rt2x00dev);
1176         rt2x00lib_uninitialize(rt2x00dev);
1177         rt2x00debug_deregister(rt2x00dev);
1178
1179         return retval;
1180 }
1181 EXPORT_SYMBOL_GPL(rt2x00lib_resume);
1182 #endif /* CONFIG_PM */
1183
1184 /*
1185  * rt2x00lib module information.
1186  */
1187 MODULE_AUTHOR(DRV_PROJECT);
1188 MODULE_VERSION(DRV_VERSION);
1189 MODULE_DESCRIPTION("rt2x00 library");
1190 MODULE_LICENSE("GPL");