Add rt2x00-mac80211 snapshot (#1916)
[openwrt.git] / package / rt2x00 / src / 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         Supported chipsets: RT2460, RT2560, RT2570,
25         rt2561, rt2561s, rt2661, rt2571W & rt2671.
26  */
27
28 /*
29  * Set enviroment defines for rt2x00.h
30  */
31 #define DRV_NAME "rt2x00lib"
32
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/version.h>
36 #include <linux/init.h>
37 #include <linux/delay.h>
38 #include <linux/etherdevice.h>
39
40 #include "rt2x00.h"
41 #include "rt2x00dev.h"
42
43 /*
44  * Radio control handlers.
45  */
46 int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
47 {
48         int status;
49
50         /*
51          * Don't enable the radio twice.
52          * or if the hardware button has been disabled.
53          */
54         if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
55             (test_bit(DEVICE_SUPPORT_HW_BUTTON, &rt2x00dev->flags) &&
56              !test_bit(DEVICE_ENABLED_RADIO_HW, &rt2x00dev->flags)))
57                 return 0;
58
59         status = rt2x00dev->ops->lib->set_device_state(
60                 rt2x00dev, STATE_RADIO_ON);
61         if (status)
62                 return status;
63
64         __set_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags);
65
66         rt2x00lib_toggle_rx(rt2x00dev, 1);
67
68         ieee80211_start_queues(rt2x00dev->hw);
69
70         return 0;
71 }
72
73 void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
74 {
75         if (!__test_and_clear_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
76                 return;
77
78         ieee80211_stop_queues(rt2x00dev->hw);
79
80         rt2x00lib_toggle_rx(rt2x00dev, 0);
81
82         rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
83 }
84
85 void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, int enable)
86 {
87         /*
88          * When we are disabling the rx, we should also stop the link tuner.
89          */
90         if (!enable && work_pending(&rt2x00dev->link.work.work))
91                 rt2x00_stop_link_tune(rt2x00dev);
92
93         rt2x00dev->ops->lib->set_device_state(rt2x00dev,
94                 enable ? STATE_RADIO_RX_ON : STATE_RADIO_RX_OFF);
95
96         /*
97          * When we are enabling the rx, we should also start the link tuner.
98          */
99         if (enable)
100                 rt2x00_start_link_tune(rt2x00dev);
101 }
102
103 static void rt2x00lib_link_tuner(struct work_struct *work)
104 {
105         struct rt2x00_dev *rt2x00dev =
106                 container_of(work, struct rt2x00_dev, link.work.work);
107         int rssi;
108
109         /*
110          * Update promisc mode (this function will first check
111          * if updating is really required).
112          */
113         rt2x00lib_config_promisc(rt2x00dev, rt2x00dev->interface.promisc);
114
115         /*
116          * Cancel all link tuning if the eeprom has indicated
117          * it is not required.
118          */
119         if (test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
120                 return;
121
122         /*
123          * Retrieve link quality.
124          * Also convert rssi to dBm using the max_rssi value.
125          */
126         rssi = rt2x00_get_link_rssi(&rt2x00dev->link);
127         rssi -= rt2x00dev->hw->max_rssi;
128
129         rt2x00dev->ops->lib->link_tuner(rt2x00dev, rssi);
130
131         /*
132          * Increase tuner counter, and reschedule the next link tuner run.
133          */
134         rt2x00dev->link.count++;
135         queue_delayed_work(rt2x00dev->workqueue, &rt2x00dev->link.work,
136                 LINK_TUNE_INTERVAL);
137 }
138
139 /*
140  * Config handlers
141  */
142 void rt2x00lib_config_type(struct rt2x00_dev *rt2x00dev, const int type)
143 {
144         if (!(is_interface_present(&rt2x00dev->interface) ^
145               test_bit(INTERFACE_ENABLED, &rt2x00dev->flags)) &&
146             !(is_monitor_present(&rt2x00dev->interface) ^
147               test_bit(INTERFACE_ENABLED_MONITOR, &rt2x00dev->flags)))
148                 return;
149
150         rt2x00dev->ops->lib->config_type(rt2x00dev, type);
151
152         if (type != IEEE80211_IF_TYPE_MNTR) {
153                 if (is_interface_present(&rt2x00dev->interface))
154                         __set_bit(INTERFACE_ENABLED, &rt2x00dev->flags);
155                 else
156                         __clear_bit(INTERFACE_ENABLED, &rt2x00dev->flags);
157         } else {
158                 if (is_monitor_present(&rt2x00dev->interface))
159                         __set_bit(INTERFACE_ENABLED_MONITOR,
160                                 &rt2x00dev->flags);
161                 else
162                         __clear_bit(INTERFACE_ENABLED_MONITOR,
163                                 &rt2x00dev->flags);
164         }
165 }
166
167 void rt2x00lib_config_phymode(struct rt2x00_dev *rt2x00dev, const int phymode)
168 {
169         if (rt2x00dev->rx_status.phymode == phymode)
170                 return;
171
172         rt2x00dev->ops->lib->config_phymode(rt2x00dev, phymode);
173
174         rt2x00dev->rx_status.phymode = phymode;
175 }
176
177 void rt2x00lib_config_channel(struct rt2x00_dev *rt2x00dev, const int value,
178         const int channel, const int freq, const int txpower)
179 {
180         if (channel == rt2x00dev->rx_status.channel)
181                 return;
182
183         rt2x00dev->ops->lib->config_channel(rt2x00dev, value, channel, txpower);
184
185         INFO(rt2x00dev, "Switching channel. "
186                 "RF1: 0x%08x, RF2: 0x%08x, RF3: 0x%08x, RF3: 0x%08x.\n",
187                 rt2x00dev->rf1, rt2x00dev->rf2,
188                 rt2x00dev->rf3, rt2x00dev->rf4);
189
190         rt2x00dev->rx_status.freq = freq;
191         rt2x00dev->rx_status.channel = channel;
192 }
193
194 void rt2x00lib_config_promisc(struct rt2x00_dev *rt2x00dev, const int promisc)
195 {
196         /*
197          * Monitor mode implies promisc mode enabled.
198          * In all other instances, check if we need to toggle promisc mode.
199          */
200         if (is_monitor_present(&rt2x00dev->interface) &&
201             !test_bit(INTERFACE_ENABLED_PROMISC, &rt2x00dev->flags)) {
202                 rt2x00dev->ops->lib->config_promisc(rt2x00dev, 1);
203                 __set_bit(INTERFACE_ENABLED_PROMISC, &rt2x00dev->flags);
204         }
205
206         if (test_bit(INTERFACE_ENABLED_PROMISC, &rt2x00dev->flags) != promisc) {
207                 rt2x00dev->ops->lib->config_promisc(rt2x00dev, promisc);
208                 __change_bit(INTERFACE_ENABLED_PROMISC, &rt2x00dev->flags);
209         }
210 }
211
212 void rt2x00lib_config_txpower(struct rt2x00_dev *rt2x00dev, const int txpower)
213 {
214         if (txpower == rt2x00dev->tx_power)
215                 return;
216
217         rt2x00dev->ops->lib->config_txpower(rt2x00dev, txpower);
218
219         rt2x00dev->tx_power = txpower;
220 }
221
222 void rt2x00lib_config_antenna(struct rt2x00_dev *rt2x00dev,
223         const int antenna_tx, const int antenna_rx)
224 {
225         if (rt2x00dev->rx_status.antenna == antenna_rx)
226                 return;
227
228         rt2x00dev->ops->lib->config_antenna(rt2x00dev, antenna_tx, antenna_rx);
229
230         rt2x00dev->rx_status.antenna = antenna_rx;
231 }
232
233 /*
234  * Driver initialization handlers.
235  */
236 static void rt2x00lib_channel(struct ieee80211_channel *entry,
237         const int channel, const int tx_power, const int value)
238 {
239         entry->chan = channel;
240         if (channel <= 14)
241                 entry->freq = 2407 + (5 * channel);
242         else
243                 entry->freq = 5000 + (5 * channel);
244         entry->val = value;
245         entry->flag =
246                 IEEE80211_CHAN_W_IBSS |
247                 IEEE80211_CHAN_W_ACTIVE_SCAN |
248                 IEEE80211_CHAN_W_SCAN;
249         entry->power_level = tx_power;
250         entry->antenna_max = 0xff;
251 }
252
253 static void rt2x00lib_rate(struct ieee80211_rate *entry,
254         const int rate,const int mask, const int plcp, const int flags)
255 {
256         entry->rate = rate;
257         entry->val =
258                 DEVICE_SET_RATE_FIELD(rate, RATE) |
259                 DEVICE_SET_RATE_FIELD(mask, RATEMASK) |
260                 DEVICE_SET_RATE_FIELD(plcp, PLCP);
261         entry->flags = flags;
262         entry->val2 = entry->val;
263         if (entry->flags & IEEE80211_RATE_PREAMBLE2)
264                 entry->val2 |= DEVICE_SET_RATE_FIELD(1, PREAMBLE);
265         entry->min_rssi_ack = 0;
266         entry->min_rssi_ack_delta = 0;
267 }
268
269 static int rt2x00lib_init_hw_modes(struct rt2x00_dev *rt2x00dev,
270         struct hw_mode_spec *spec)
271 {
272         struct ieee80211_hw *hw = rt2x00dev->hw;
273         struct ieee80211_hw_mode *hwmodes;
274         struct ieee80211_channel *channels;
275         struct ieee80211_rate *rates;
276         unsigned int i;
277         unsigned char tx_power;
278
279         hwmodes = kzalloc(sizeof(*hwmodes) * spec->num_modes, GFP_KERNEL);
280         if (!hwmodes)
281                 goto exit;
282
283         channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL);
284         if (!channels)
285                 goto exit_free_modes;
286
287         rates = kzalloc(sizeof(*rates) * spec->num_rates, GFP_KERNEL);
288         if (!rates)
289                 goto exit_free_channels;
290
291         /*
292          * Initialize Rate list.
293          */
294         rt2x00lib_rate(&rates[0], 10, 0x001, 0x00, IEEE80211_RATE_CCK);
295         rt2x00lib_rate(&rates[1], 20, 0x003, 0x01, IEEE80211_RATE_CCK_2);
296         rt2x00lib_rate(&rates[2], 55, 0x007, 0x02, IEEE80211_RATE_CCK_2);
297         rt2x00lib_rate(&rates[3], 110, 0x00f, 0x03, IEEE80211_RATE_CCK_2);
298
299         if (spec->num_rates > 4) {
300                 rt2x00lib_rate(&rates[4], 60, 0x01f, 0x0b, IEEE80211_RATE_OFDM);
301                 rt2x00lib_rate(&rates[5], 90, 0x03f, 0x0f, IEEE80211_RATE_OFDM);
302                 rt2x00lib_rate(&rates[6], 120, 0x07f, 0x0a, IEEE80211_RATE_OFDM);
303                 rt2x00lib_rate(&rates[7], 180, 0x0ff, 0x0e, IEEE80211_RATE_OFDM);
304                 rt2x00lib_rate(&rates[8], 240, 0x1ff, 0x09, IEEE80211_RATE_OFDM);
305                 rt2x00lib_rate(&rates[9], 360, 0x3ff, 0x0d, IEEE80211_RATE_OFDM);
306                 rt2x00lib_rate(&rates[10], 480, 0x7ff, 0x08, IEEE80211_RATE_OFDM);
307                 rt2x00lib_rate(&rates[11], 540, 0xfff, 0x0c, IEEE80211_RATE_OFDM);
308         }
309
310         /*
311          * Initialize Channel list.
312          */
313         for (i = 0; i < 14; i++)
314                 rt2x00lib_channel(&channels[i], i + 1,
315                         spec->tx_power_bg[i], spec->chan_val_bg[i]);
316
317         if (spec->num_channels > 14) {
318                 for (i = 14; i < spec->num_channels; i++) {
319                         if (i < 22)
320                                 channels[i].chan = 36;
321                         else if (i < 33)
322                                 channels[i].chan = 100;
323                         else
324                                 channels[i].chan = 149;
325                         channels[i].chan += ((i - 14) * 4);
326
327                         if (spec->tx_power_a)
328                                 tx_power = spec->tx_power_a[i];
329                         else
330                                 tx_power = spec->tx_power_default;
331
332                         rt2x00lib_channel(&channels[i],
333                                 channels[i].chan, tx_power,
334                                 spec->chan_val_a[i]);
335                 }
336         }
337
338         /*
339          * Intitialize 802.11b
340          * Rates: CCK.
341          * Channels: OFDM.
342          */
343         if (spec->num_modes > HWMODE_B) {
344                 hwmodes[HWMODE_B].mode = MODE_IEEE80211B;
345                 hwmodes[HWMODE_B].num_channels = 14;
346                 hwmodes[HWMODE_B].num_rates = 4;
347                 hwmodes[HWMODE_B].channels = channels;
348                 hwmodes[HWMODE_B].rates = rates;
349         }
350
351         /*
352          * Intitialize 802.11g
353          * Rates: CCK, OFDM.
354          * Channels: OFDM.
355          */
356         if (spec->num_modes > HWMODE_G) {
357                 hwmodes[HWMODE_G].mode = MODE_IEEE80211G;
358                 hwmodes[HWMODE_G].num_channels = 14;
359                 hwmodes[HWMODE_G].num_rates = spec->num_rates;
360                 hwmodes[HWMODE_G].channels = channels;
361                 hwmodes[HWMODE_G].rates = rates;
362         }
363
364         /*
365          * Intitialize 802.11a
366          * Rates: OFDM.
367          * Channels: OFDM, UNII, HiperLAN2.
368          */
369         if (spec->num_modes > HWMODE_A) {
370                 hwmodes[HWMODE_A].mode = MODE_IEEE80211A;
371                 hwmodes[HWMODE_A].num_channels = spec->num_channels - 14;
372                 hwmodes[HWMODE_A].num_rates = spec->num_rates - 4;
373                 hwmodes[HWMODE_A].channels = &channels[14];
374                 hwmodes[HWMODE_A].rates = &rates[4];
375         }
376
377         if (spec->num_modes > HWMODE_G &&
378             ieee80211_register_hwmode(hw, &hwmodes[HWMODE_G]))
379                 goto exit_free_rates;
380
381         if (spec->num_modes > HWMODE_B &&
382             ieee80211_register_hwmode(hw, &hwmodes[HWMODE_B]))
383                 goto exit_free_rates;
384
385         if (spec->num_modes > HWMODE_A &&
386             ieee80211_register_hwmode(hw, &hwmodes[HWMODE_A]))
387                 goto exit_free_rates;
388
389         rt2x00dev->hwmodes = hwmodes;
390
391         return 0;
392
393 exit_free_rates:
394         kfree(rates);
395
396 exit_free_channels:
397         kfree(channels);
398
399 exit_free_modes:
400         kfree(hwmodes);
401
402 exit:
403         ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n");
404         return -ENOMEM;
405 }
406
407 static void rt2x00lib_deinit_hw(struct rt2x00_dev *rt2x00dev)
408 {
409         if (test_bit(DEVICE_INITIALIZED_HW, &rt2x00dev->flags))
410                 ieee80211_unregister_hw(rt2x00dev->hw);
411
412         if (likely(rt2x00dev->hwmodes)) {
413                 kfree(rt2x00dev->hwmodes->channels);
414                 kfree(rt2x00dev->hwmodes->rates);
415                 kfree(rt2x00dev->hwmodes);
416                 rt2x00dev->hwmodes = NULL;
417         }
418 }
419
420 static int rt2x00lib_init_hw(struct rt2x00_dev *rt2x00dev)
421 {
422         struct hw_mode_spec *spec = &rt2x00dev->spec;
423         int status;
424
425         /*
426          * Initialize device.
427          */
428         SET_IEEE80211_DEV(rt2x00dev->hw, rt2x00dev->device);
429
430         /*
431          * Initialize MAC address.
432          */
433         if (!is_valid_ether_addr(spec->mac_addr)) {
434                 ERROR(rt2x00dev, "Invalid MAC addr: " MAC_FMT ".\n",
435                         MAC_ARG(spec->mac_addr));
436                 return -EINVAL;
437         }
438
439         rt2x00dev->ops->lib->config_mac_addr(rt2x00dev, spec->mac_addr);
440         SET_IEEE80211_PERM_ADDR(rt2x00dev->hw, spec->mac_addr);
441
442         /*
443          * Initialize HW modes.
444          */
445         status = rt2x00lib_init_hw_modes(rt2x00dev, spec);
446         if (status)
447                 return status;
448
449         /*
450          * Register HW.
451          */
452         status = ieee80211_register_hw(rt2x00dev->hw);
453         if (status) {
454                 rt2x00lib_deinit_hw(rt2x00dev);
455                 return status;
456         }
457
458         __set_bit(DEVICE_INITIALIZED_HW, &rt2x00dev->flags);
459
460         return 0;
461 }
462
463 /*
464  * Initialization/uninitialization handlers.
465  */
466 static int rt2x00lib_alloc_ring(struct data_ring *ring,
467         const u16 max_entries, const u16 data_size, const u16 desc_size)
468 {
469         struct data_entry *entry;
470         unsigned int i;
471
472         ring->stats.limit = max_entries;
473         ring->data_size = data_size;
474         ring->desc_size = desc_size;
475
476         /*
477          * Allocate all ring entries.
478          */
479         entry = kzalloc(ring->stats.limit * sizeof(*entry), GFP_KERNEL);
480         if (!entry)
481                 return -ENOMEM;
482
483         for (i = 0; i < ring->stats.limit; i++) {
484                 entry[i].flags = 0;
485                 entry[i].ring = ring;
486                 entry[i].skb = NULL;
487         }
488
489         ring->entry = entry;
490
491         return 0;
492 }
493
494 static int rt2x00lib_allocate_rings(struct rt2x00_dev *rt2x00dev)
495 {
496         struct data_ring *ring;
497
498         /*
499          * Allocate the RX ring.
500          */
501         if (rt2x00lib_alloc_ring(rt2x00dev->rx,
502                 RX_ENTRIES, DATA_FRAME_SIZE, rt2x00dev->ops->rxd_size))
503                 return -ENOMEM;
504
505         /*
506          * First allocate the TX rings.
507          */
508         txring_for_each(rt2x00dev, ring) {
509                 if (rt2x00lib_alloc_ring(ring,
510                         TX_ENTRIES, DATA_FRAME_SIZE, rt2x00dev->ops->txd_size))
511                         return -ENOMEM;
512         }
513
514         /*
515          * Allocate the BEACON ring.
516          */
517         if (rt2x00lib_alloc_ring(&rt2x00dev->bcn[0],
518                 BEACON_ENTRIES, MGMT_FRAME_SIZE, rt2x00dev->ops->txd_size))
519                 return -ENOMEM;
520
521         /*
522          * Allocate the Atim ring.
523          */
524         if (test_bit(DEVICE_SUPPORT_ATIM, &rt2x00dev->flags)) {
525                 if (rt2x00lib_alloc_ring(&rt2x00dev->bcn[1],
526                         ATIM_ENTRIES, DATA_FRAME_SIZE, rt2x00dev->ops->txd_size))
527                         return -ENOMEM;
528         }
529
530         return 0;
531 }
532
533 static void rt2x00lib_free_rings(struct rt2x00_dev *rt2x00dev)
534 {
535         struct data_ring *ring;
536
537         ring_for_each(rt2x00dev, ring) {
538                 kfree(ring->entry);
539                 ring->entry = NULL;
540         }
541 }
542
543 int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
544 {
545         int status;
546
547         if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
548                 return 0;
549
550         /*
551          * Allocate all data rings.
552          */
553         status = rt2x00lib_allocate_rings(rt2x00dev);
554         if (status) {
555                 ERROR(rt2x00dev, "DMA allocation failed.\n");
556                 return status;
557         }
558
559         /*
560          * Initialize the device.
561          */
562         status = rt2x00dev->ops->lib->initialize(rt2x00dev);
563         if (status)
564                 goto exit;
565
566         __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags);
567
568         /*
569          * Register the rfkill handler.
570          */
571         status = rt2x00lib_register_rfkill(rt2x00dev);
572         if (status)
573                 goto exit_unitialize;
574
575         return 0;
576
577 exit_unitialize:
578         rt2x00lib_uninitialize(rt2x00dev);
579
580 exit:
581         rt2x00lib_free_rings(rt2x00dev);
582
583         return status;
584 }
585
586 void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
587 {
588         if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags))
589                 return;
590
591         /*
592          * Flush out all pending work.
593          */
594         flush_workqueue(rt2x00dev->workqueue);
595
596         /*
597          * Unregister rfkill.
598          */
599         rt2x00lib_unregister_rfkill(rt2x00dev);
600
601         /*
602          * Allow the HW to uninitialize.
603          */
604         rt2x00dev->ops->lib->uninitialize(rt2x00dev);
605
606         /*
607          * Free allocated datarings.
608          */
609         rt2x00lib_free_rings(rt2x00dev);
610 }
611
612 /*
613  * driver allocation handlers.
614  */
615 static int rt2x00lib_alloc_rings(struct rt2x00_dev *rt2x00dev)
616 {
617         struct data_ring *ring;
618         unsigned int ring_num;
619
620         /*
621          * We need the following rings:
622          * RX: 1
623          * TX: hw->queues
624          * Beacon: 1
625          * Atim: 1 (if supported)
626          */
627         ring_num = 2 + rt2x00dev->hw->queues +
628                 test_bit(DEVICE_SUPPORT_ATIM, &rt2x00dev->flags);
629
630         ring = kzalloc(sizeof(*ring) * ring_num, GFP_KERNEL);
631         if (!ring) {
632                 ERROR(rt2x00dev, "Ring allocation failed.\n");
633                 return -ENOMEM;
634         }
635
636         /*
637          * Initialize pointers
638          */
639         rt2x00dev->rx = &ring[0];
640         rt2x00dev->tx = &ring[1];
641         rt2x00dev->bcn = &ring[1 + rt2x00dev->hw->queues];
642
643         /*
644          * Initialize ring parameters.
645          * cw_min: 2^5 = 32.
646          * cw_max: 2^10 = 1024.
647          */
648         ring_for_each(rt2x00dev, ring) {
649                 ring->rt2x00dev = rt2x00dev;
650                 ring->tx_params.aifs = 2;
651                 ring->tx_params.cw_min = 5;
652                 ring->tx_params.cw_max = 10;
653         }
654
655         return 0;
656 }
657
658 int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
659 {
660         int retval = -ENOMEM;
661
662         /*
663          * Create workqueue.
664          */
665         rt2x00dev->workqueue = create_singlethread_workqueue(DRV_NAME);
666         if (!rt2x00dev->workqueue)
667                 goto exit;
668
669         /*
670          * Let the driver probe the device to detect the capabilities.
671          */
672         retval = rt2x00dev->ops->lib->init_hw(rt2x00dev);
673         if (retval) {
674                 ERROR(rt2x00dev, "Failed to allocate device.\n");
675                 goto exit;
676         }
677
678         /*
679          * Initialize configuration work.
680          */
681         INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner);
682
683         /*
684          * Reset current working type.
685          */
686         rt2x00dev->interface.type = -EINVAL;
687
688         /*
689          * Allocate ring array.
690          */
691         retval = rt2x00lib_alloc_rings(rt2x00dev);
692         if (retval)
693                 goto exit;
694
695         /*
696          * Initialize ieee80211 structure.
697          */
698         retval = rt2x00lib_init_hw(rt2x00dev);
699         if (retval) {
700                 ERROR(rt2x00dev, "Failed to initialize hw.\n");
701                 goto exit;
702         }
703
704         /*
705          * Allocatie rfkill.
706          */
707         retval = rt2x00lib_allocate_rfkill(rt2x00dev);
708         if (retval)
709                 goto exit;
710
711         /*
712          * Open the debugfs entry.
713          */
714         rt2x00debug_register(rt2x00dev);
715
716         /*
717          * Check if we need to load the firmware.
718          */
719         if (test_bit(FIRMWARE_REQUIRED, &rt2x00dev->flags)) {
720                 /*
721                  * Request firmware and wait with further
722                  * initializing of the card until the firmware
723                  * has been loaded.
724                  */
725                 retval = rt2x00lib_load_firmware(rt2x00dev);
726                 if (retval)
727                         goto exit;
728         }
729
730         return 0;
731
732 exit:
733         rt2x00lib_remove_dev(rt2x00dev);
734
735         return retval;
736 }
737 EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
738
739 void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
740 {
741         /*
742          * Disable radio.
743          */
744         rt2x00lib_disable_radio(rt2x00dev);
745
746         /*
747          * Uninitialize device.
748          */
749         rt2x00lib_uninitialize(rt2x00dev);
750
751         /*
752          * Close debugfs entry.
753          */
754         rt2x00debug_deregister(rt2x00dev);
755
756         /*
757          * Free rfkill
758          */
759         rt2x00lib_free_rfkill(rt2x00dev);
760
761         /*
762          * Free ieee80211_hw memory.
763          */
764         rt2x00lib_deinit_hw(rt2x00dev);
765
766         /*
767          * Free workqueue.
768          */
769         if (likely(rt2x00dev->workqueue)) {
770                 destroy_workqueue(rt2x00dev->workqueue);
771                 rt2x00dev->workqueue = NULL;
772         }
773
774         /*
775          * Free ring structures.
776          */
777         kfree(rt2x00dev->rx);
778         rt2x00dev->rx = NULL;
779         rt2x00dev->tx = NULL;
780         rt2x00dev->bcn = NULL;
781
782         /*
783          * Free EEPROM memory.
784          */
785         kfree(rt2x00dev->eeprom);
786         rt2x00dev->eeprom = NULL;
787 }
788 EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
789
790 /*
791  * Device state handlers
792  */
793 int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev,
794         pm_message_t state)
795 {
796         int retval;
797
798         NOTICE(rt2x00dev, "Going to sleep.\n");
799
800         rt2x00lib_disable_radio(rt2x00dev);
801
802         /*
803          * Set device mode to sleep for power management.
804          */
805         retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP);
806         if (retval)
807                 return retval;
808
809         rt2x00lib_remove_dev(rt2x00dev);
810
811         return 0;
812 }
813 EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
814
815 int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
816 {
817         int retval;
818
819         NOTICE(rt2x00dev, "Waking up.\n");
820
821         retval = rt2x00lib_probe_dev(rt2x00dev);
822         if (retval) {
823                 ERROR(rt2x00dev, "Failed to allocate device.\n");
824                 return retval;
825         }
826
827         /*
828          * Set device mode to awake for power management.
829          */
830         retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_AWAKE);
831         if (retval)
832                 return retval;
833
834         return 0;
835 }
836 EXPORT_SYMBOL_GPL(rt2x00lib_resume);
837
838 /*
839  * Interrupt context handlers.
840  */
841 void rt2x00lib_txdone(struct data_entry *entry,
842         const int status, const int retry)
843 {
844         struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
845         struct ieee80211_tx_status *tx_status = &entry->tx_status;
846         struct ieee80211_low_level_stats *stats = &rt2x00dev->low_level_stats;
847
848         /*
849          * Update TX statistics.
850          */
851         tx_status->flags = 0;
852         tx_status->ack_signal = 0;
853         tx_status->excessive_retries = (status == TX_FAIL_RETRY);
854         tx_status->retry_count = retry;
855
856         if (!(tx_status->control.flags & IEEE80211_TXCTL_NO_ACK)) {
857                 if (status == TX_SUCCESS || status == TX_SUCCESS_RETRY)
858                         tx_status->flags |= IEEE80211_TX_STATUS_ACK;
859                 else
860                         stats->dot11ACKFailureCount++;
861         }
862
863         tx_status->queue_length = entry->ring->stats.limit;
864         tx_status->queue_number = tx_status->control.queue;
865
866         if (tx_status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) {
867                 if (status == TX_SUCCESS || status == TX_SUCCESS_RETRY)
868                         stats->dot11RTSSuccessCount++;
869                 else
870                         stats->dot11RTSFailureCount++;
871         }
872
873         /*
874          * Send the tx_status to mac80211,
875          * that method also cleans up the skb structure.
876          */
877         ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb, tx_status);
878
879         entry->skb = NULL;
880 }
881 EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
882
883 void rt2x00lib_rxdone(struct data_entry *entry, char *data,
884         const int size, const int signal, const int rssi, const int ofdm)
885 {
886         struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev;
887         struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status;
888         struct ieee80211_hw_mode *mode;
889         struct ieee80211_rate *rate;
890         struct sk_buff *skb;
891         unsigned int i;
892         int val = 0;
893
894         /*
895          * Update RX statistics.
896          */
897         mode = &rt2x00dev->hwmodes[rt2x00dev->curr_hwmode];
898         for (i = 0; i < mode->num_rates; i++) {
899                 rate = &mode->rates[i];
900
901                 /*
902                  * When frame was received with an OFDM bitrate,
903                  * the signal is the PLCP value. If it was received with
904                  * a CCK bitrate the signal is the rate in 0.5kbit/s.
905                  */
906                 if (!ofdm)
907                         val = DEVICE_GET_RATE_FIELD(rate->val, RATE);
908                 else
909                         val = DEVICE_GET_RATE_FIELD(rate->val, PLCP);
910
911                 if (val == signal) {
912                         /*
913                          * Check for preamble bit.
914                          */
915                         if (signal & 0x08)
916                                 val = rate->val2;
917                         val = rate->val;
918                         break;
919                 }
920         }
921
922         rx_status->rate = val;
923         rx_status->ssi = rssi;
924         rx_status->noise = rt2x00dev->link.curr_noise;
925         rt2x00_update_link_rssi(&rt2x00dev->link, rssi);
926
927         /*
928          * Let's allocate a sk_buff where we can store the received data in,
929          * note that if data is NULL, we still have to allocate a sk_buff
930          * but that we should use that to replace the sk_buff which is already
931          * inside the entry.
932          */
933         skb = dev_alloc_skb(size + NET_IP_ALIGN);
934         if (!skb)
935                 return;
936
937         skb_reserve(skb, NET_IP_ALIGN);
938         skb_put(skb, size);
939
940         if (data) {
941                 memcpy(skb->data, data, size);
942                 entry->skb = skb;
943                 skb = NULL;
944         }
945
946         ieee80211_rx_irqsafe(rt2x00dev->hw, entry->skb, rx_status);
947         entry->skb = skb;
948 }
949 EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
950
951 /*
952  * TX descriptor initializer
953  */
954 void rt2x00lib_write_tx_desc(struct rt2x00_dev *rt2x00dev,
955         struct data_entry *entry, struct data_desc *txd,
956         struct ieee80211_hdr *ieee80211hdr, unsigned int length,
957         struct ieee80211_tx_control *control)
958 {
959         struct data_entry_desc desc;
960         int tx_rate;
961         int bitrate;
962         int duration;
963         int residual;
964         u16 frame_control;
965         u16 seq_ctrl;
966
967         /*
968          * Identify queue
969          */
970         if (control->queue < rt2x00dev->hw->queues)
971                 desc.queue = control->queue;
972         else
973                 desc.queue = 15;
974
975         /*
976          * Read required fields from ieee80211 header.
977          */
978         frame_control = le16_to_cpu(ieee80211hdr->frame_control);
979         seq_ctrl = le16_to_cpu(ieee80211hdr->seq_ctrl);
980
981         tx_rate = control->tx_rate;
982
983         /*
984          * Check if this is a rts frame
985          */
986         if (is_rts_frame(frame_control)) {
987                 __set_bit(ENTRY_TXD_RTS_FRAME, &entry->flags);
988                 if (control->rts_cts_rate)
989                         tx_rate = control->rts_cts_rate;
990         }
991
992         /*
993          * Check for OFDM
994          */
995         if (DEVICE_GET_RATE_FIELD(tx_rate, RATEMASK) & DEV_OFDM_RATE)
996                 __set_bit(ENTRY_TXD_OFDM_RATE, &entry->flags);
997
998         /*
999          * Check if more fragments are pending
1000          */
1001         if (ieee80211_get_morefrag(ieee80211hdr))
1002                 __set_bit(ENTRY_TXD_MORE_FRAG, &entry->flags);
1003
1004         /*
1005          * Check if this is a new sequence
1006          */
1007         if ((seq_ctrl & IEEE80211_SCTL_FRAG) == 0)
1008                 __set_bit(ENTRY_TXD_NEW_SEQ, &entry->flags);
1009
1010         /*
1011          * Beacons and probe responses require the tsf timestamp
1012          * to be inserted into the frame.
1013          */
1014         if (control->queue == IEEE80211_TX_QUEUE_BEACON ||
1015             is_probe_resp(frame_control))
1016                 __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &entry->flags);
1017
1018         /*
1019          * Check if ACK is required
1020          */
1021         if (!(control->flags & IEEE80211_TXCTL_NO_ACK))
1022                 __set_bit(ENTRY_TXD_REQ_ACK, &entry->flags);
1023
1024         /*
1025          * Determine with what IFS priority this frame should be send.
1026          * Set ifs to IFS_SIFS when the this is not the first fragment,
1027          * or this fragment came after RTS/CTS.
1028          */
1029         if ((seq_ctrl & IEEE80211_SCTL_FRAG) > 0 ||
1030             test_bit(ENTRY_TXD_RTS_FRAME, &entry->flags))
1031                 desc.ifs = IFS_SIFS;
1032         else
1033                 desc.ifs = IFS_BACKOFF;
1034
1035         /*
1036          * How the length should be processed depends
1037          * on if we are working with OFDM rates or not.
1038          */
1039         if (test_bit(ENTRY_TXD_OFDM_RATE, &entry->flags)) {
1040                 residual = 0;
1041                 desc.length_high = ((length + FCS_LEN) >> 6) & 0x3f;
1042                 desc.length_low = ((length + FCS_LEN) & 0x3f);
1043
1044         } else {
1045                 bitrate = DEVICE_GET_RATE_FIELD(tx_rate, RATE);
1046
1047                 /*
1048                  * Convert length to microseconds.
1049                  */
1050                 residual = get_duration_res(length + FCS_LEN, bitrate);
1051                 duration = get_duration(length + FCS_LEN, bitrate);
1052
1053                 if (residual != 0)
1054                         duration++;
1055
1056                 desc.length_high = duration >> 8;
1057                 desc.length_low = duration & 0xff;
1058         }
1059
1060         /*
1061          * Create the signal and service values.
1062          */
1063         desc.signal = DEVICE_GET_RATE_FIELD(tx_rate, PLCP);
1064         if (DEVICE_GET_RATE_FIELD(tx_rate, PREAMBLE))
1065                 desc.signal |= 0x08;
1066
1067         desc.service = 0x04;
1068         if (residual <= (8 % 11))
1069                 desc.service |= 0x80;
1070
1071         rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, entry, txd, &desc,
1072                 ieee80211hdr, length, control);
1073 }
1074 EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc);
1075
1076 /*
1077  * rt2x00lib module information.
1078  */
1079 MODULE_AUTHOR(DRV_PROJECT);
1080 MODULE_VERSION(DRV_VERSION);
1081 MODULE_DESCRIPTION("rt2x00 library");
1082 MODULE_LICENSE("GPL");