always include kernel config for kernel packages
[openwrt.git] / package / rt2x00 / src / rt2x00.h
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: rt2x00
23         Abstract: rt2x00 global information.
24         Supported chipsets: RT2460, RT2560, RT2570,
25         rt2561, rt2561s, rt2661, rt2571W & rt2671.
26  */
27
28 #ifndef RT2X00_H
29 #define RT2X00_H
30
31 #include <linux/bitops.h>
32 #include <linux/prefetch.h>
33 #include <linux/skbuff.h>
34 #include <linux/workqueue.h>
35
36 #include <net/mac80211.h>
37
38 /*
39  * Module information.
40  * DRV_NAME should be set within the individual module source files.
41  */
42 #define DRV_VERSION     "2.0.2"
43 #define DRV_PROJECT     "http://rt2x00.serialmonkey.com"
44
45 /*
46  * Debug definitions.
47  * Debug output has to be enabled during compile time.
48  */
49 #define DEBUG_PRINTK_MSG(__dev, __kernlvl, __lvl, __msg, __args...)     \
50         printk(__kernlvl "%s -> %s: %s - " __msg,                       \
51                 wiphy_name(rt2x00dev->hw->wiphy),                       \
52                 __FUNCTION__, __lvl, ##__args)
53
54 #define DEBUG_PRINTK_PROBE(__kernlvl, __lvl, __msg, __args...)          \
55         printk(__kernlvl "%s -> %s: %s - " __msg,                       \
56                 DRV_NAME, __FUNCTION__, __lvl,  ##__args)
57
58 #ifdef CONFIG_RT2X00_DEBUG
59 #define DEBUG_PRINTK(__dev, __kernlvl, __lvl, __msg, __args...) \
60         DEBUG_PRINTK_MSG(__dev, __kernlvl, __lvl, __msg, ##__args);
61 #else /* CONFIG_RT2X00_DEBUG */
62 #define DEBUG_PRINTK(__dev, __kernlvl, __lvl, __msg, __args...) \
63         do { } while (0)
64 #endif /* CONFIG_RT2X00_DEBUG */
65
66 /*
67  * Various debug levels.
68  * The debug levels PANIC and ERROR both indicate serious problems,
69  * for this reason they should never be ignored.
70  * The special ERROR_PROBE message is for messages that are generated
71  * when the rt2x00_dev is not yet initialized.
72  */
73 #define PANIC(__dev, __msg, __args...) \
74         DEBUG_PRINTK_MSG(__dev, KERN_CRIT, "Panic", __msg, ##__args)
75 #define ERROR(__dev, __msg, __args...)  \
76         DEBUG_PRINTK_MSG(__dev, KERN_ERR, "Error", __msg, ##__args)
77 #define ERROR_PROBE(__msg, __args...) \
78         DEBUG_PRINTK_PROBE(KERN_ERR, "Error", __msg, ##__args)
79 #define WARNING(__dev, __msg, __args...) \
80         DEBUG_PRINTK(__dev, KERN_WARNING, "Warning", __msg, ##__args)
81 #define NOTICE(__dev, __msg, __args...) \
82         DEBUG_PRINTK(__dev, KERN_NOTICE, "Notice", __msg, ##__args)
83 #define INFO(__dev, __msg, __args...) \
84         DEBUG_PRINTK(__dev, KERN_INFO, "Info", __msg, ##__args)
85 #define DEBUG(__dev, __msg, __args...) \
86         DEBUG_PRINTK(__dev, KERN_DEBUG, "Debug", __msg, ##__args)
87 #define EEPROM(__dev, __msg, __args...) \
88         DEBUG_PRINTK(__dev, KERN_DEBUG, "EEPROM recovery", __msg, ##__args)
89
90 /*
91  * Ring sizes.
92  * Ralink PCI devices demand the Frame size to be a multiple of 128 bytes.
93  * DATA_FRAME_SIZE is used for TX, RX, ATIM and PRIO rings.
94  * MGMT_FRAME_SIZE is used for the BEACON ring.
95  */
96 #define DATA_FRAME_SIZE 2432
97 #define MGMT_FRAME_SIZE 256
98
99 /*
100  * Number of entries in a packet ring.
101  */
102 #define RX_ENTRIES      12
103 #define TX_ENTRIES      12
104 #define ATIM_ENTRIES    1
105 #define BEACON_ENTRIES  1
106
107 /*
108  * Standard timing and size defines.
109  */
110 #define ACK_SIZE                14
111 #define IEEE80211_HEADER        24
112 #define PLCP                    48
113 #define BEACON                  100
114 #define PREAMBLE                144
115 #define SHORT_PREAMBLE          72
116 #define SLOT_TIME               20
117 #define SHORT_SLOT_TIME         9
118 #define SIFS                    10
119 #define PIFS                    ( SIFS + SLOT_TIME )
120 #define SHORT_PIFS              ( SIFS + SHORT_SLOT_TIME )
121 #define DIFS                    ( PIFS + SLOT_TIME )
122 #define SHORT_DIFS              ( SHORT_PIFS + SHORT_SLOT_TIME )
123 #define EIFS                    ( SIFS + (8 * (IEEE80211_HEADER + ACK_SIZE)) )
124
125 /*
126  * IEEE802.11 header defines
127  */
128 #define is_rts_frame(__fc) \
129         ( !!((((__fc) &  IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL) && \
130              (((__fc) &  IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_RTS)) )
131 #define is_cts_frame(__fc) \
132         ( !!((((__fc) &  IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL) && \
133              (((__fc) &  IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_CTS)) )
134 #define is_probe_resp(__fc) \
135         ( !!((((__fc) & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) && \
136              (((__fc) & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP)) )
137
138 /*
139  * Interval defines
140  */
141 #define LINK_TUNE_INTERVAL      ( round_jiffies(HZ) )
142 #define RFKILL_POLL_INTERVAL    ( HZ / 4 )
143
144 /*
145  * TX result flags.
146  */
147 enum TX_STATUS {
148         TX_SUCCESS = 0,
149         TX_SUCCESS_RETRY = 1,
150         TX_FAIL_RETRY = 2,
151         TX_FAIL_INVALID = 3,
152         TX_FAIL_OTHER = 4,
153 };
154
155 /*
156  * Antenna values
157  */
158 enum antenna {
159         ANTENNA_DIVERSITY = 0,
160         ANTENNA_A = 1,
161         ANTENNA_B = 2,
162 };
163
164 /*
165  * Led mode values.
166  */
167 enum led_mode {
168         LED_MODE_DEFAULT = 0,
169         LED_MODE_TXRX_ACTIVITY = 1,
170         LED_MODE_SIGNAL_STRENGTH = 2,
171         LED_MODE_ASUS = 3,
172         LED_MODE_ALPHA = 4,
173 };
174
175 /*
176  * Device states
177  */
178 enum dev_state {
179         STATE_DEEP_SLEEP = 0,
180         STATE_SLEEP = 1,
181         STATE_STANDBY = 2,
182         STATE_AWAKE = 3,
183
184 /*
185  * Additional device states, these values are
186  * not strict since they are not directly passed
187  * into the device.
188  */
189         STATE_RADIO_ON,
190         STATE_RADIO_OFF,
191         STATE_RADIO_RX_ON,
192         STATE_RADIO_RX_OFF,
193 };
194
195 /*
196  * IFS backoff values
197  */
198 enum ifs {
199         IFS_BACKOFF = 0,
200         IFS_SIFS = 1,
201         IFS_NEW_BACKOFF = 2,
202         IFS_NONE = 3,
203 };
204
205 /*
206  * Cipher types for hardware encryption
207  */
208 enum cipher {
209         CIPHER_NONE = 0,
210         CIPHER_WEP64 = 1,
211         CIPHER_WEP128 = 2,
212         CIPHER_TKIP = 3,
213         CIPHER_AES = 4,
214 /*
215  * The following fields were added by rt61pci and rt73usb.
216  */
217         CIPHER_CKIP64 = 5,
218         CIPHER_CKIP128 = 6,
219         CIPHER_TKIP_NO_MIC = 7,
220 };
221
222 /*
223  * Register handlers.
224  * We store the position of a register field inside a field structure,
225  * This will simplify the process of setting and reading a certain field
226  * inside the register while making sure the process remains byte order safe.
227  */
228 struct rt2x00_field8 {
229         u8 bit_offset;
230         u8 bit_mask;
231 };
232
233 struct rt2x00_field16 {
234         u16 bit_offset;
235         u16 bit_mask;
236 };
237
238 struct rt2x00_field32 {
239         u32 bit_offset;
240         u32 bit_mask;
241 };
242
243 /*
244  * Power of two check from Linus Torvalds,
245  * this will check if the mask that has been
246  * given contains and contiguous set of bits.
247  */
248 #define is_power_of_two(x)      ( !((x) & ((x)-1)) )
249 #define low_bit_mask(x)         ( ((x)-1) & ~(x) )
250 #define is_valid_mask(x)        is_power_of_two(1 + (x) + low_bit_mask(x))
251
252 #define FIELD8(__mask)                          \
253 ({                                              \
254         BUILD_BUG_ON(!(__mask) ||               \
255                      !is_valid_mask(__mask) ||  \
256                      (__mask) != (u8)(__mask)); \
257         (struct rt2x00_field8) {                \
258                 __ffs(__mask), (__mask)         \
259         };                                      \
260 })
261
262 #define FIELD16(__mask)                         \
263 ({                                              \
264         BUILD_BUG_ON(!(__mask) ||               \
265                      !is_valid_mask(__mask) ||  \
266                      (__mask) != (u16)(__mask));\
267         (struct rt2x00_field16) {               \
268                 __ffs(__mask), (__mask)         \
269         };                                      \
270 })
271
272 #define FIELD32(__mask)                         \
273 ({                                              \
274         BUILD_BUG_ON(!(__mask) ||               \
275                      !is_valid_mask(__mask) ||  \
276                      (__mask) != (u32)(__mask));\
277         (struct rt2x00_field32) {               \
278                 __ffs(__mask), (__mask)         \
279         };                                      \
280 })
281
282 static inline void rt2x00_set_field32(u32 *reg,
283         const struct rt2x00_field32 field, const u32 value)
284 {
285         *reg &= ~(field.bit_mask);
286         *reg |= (value << field.bit_offset) & field.bit_mask;
287 }
288
289 static inline u32 rt2x00_get_field32(const u32 reg,
290         const struct rt2x00_field32 field)
291 {
292         return (reg & field.bit_mask) >> field.bit_offset;
293 }
294
295 static inline void rt2x00_set_field16(u16 *reg,
296         const struct rt2x00_field16 field, const u16 value)
297 {
298         *reg &= ~(field.bit_mask);
299         *reg |= (value << field.bit_offset) & field.bit_mask;
300 }
301
302 static inline u16 rt2x00_get_field16(const u16 reg,
303         const struct rt2x00_field16 field)
304 {
305         return (reg & field.bit_mask) >> field.bit_offset;
306 }
307
308 static inline void rt2x00_set_field8(u8 *reg,
309         const struct rt2x00_field8 field, const u8 value)
310 {
311         *reg &= ~(field.bit_mask);
312         *reg |= (value << field.bit_offset) & field.bit_mask;
313 }
314
315 static inline u8 rt2x00_get_field8(const u8 reg,
316         const struct rt2x00_field8 field)
317 {
318         return (reg & field.bit_mask) >> field.bit_offset;
319 }
320
321 /*
322  * Chipset identification
323  * The chipset on the device is composed of a RT and RF chip.
324  * The chipset combination is important for determining device capabilities.
325  */
326 struct rt2x00_chip {
327         u16 rt;
328 #define RT2460          0x0101
329 #define RT2560          0x0201
330 #define RT2570          0x1201
331 #define RT2561          0x0301
332 #define RT2561s         0x0302
333 #define RT2661          0x0401
334 #define RT2571          0x1300
335
336         u16 rf;
337         u32 rev;
338 };
339
340 /*
341  * data_desc
342  * Each data entry also contains a descriptor which is used by the
343  * device to determine what should be done with the packet and
344  * what the current status is.
345  * This structure is greatly simplified, but the descriptors
346  * are basically a list of little endian 32 bit values.
347  * Make the array by default 1 word big, this will allow us
348  * to use sizeof() correctly.
349  */
350 struct data_desc {
351         __le32 word[1];
352 };
353
354 /*
355  * data_entry_desc
356  * Summary of information that should be written into the
357  * descriptor for sending a TX frame.
358  */
359 struct data_entry_desc {
360         /*
361          * PLCP values.
362          */
363         u16 length_high;
364         u16 length_low;
365         u16 signal;
366         u16 service;
367
368         int queue;
369         int ifs;
370 };
371
372 /*
373  * data_entry
374  * The data ring is a list of data entries.
375  * Each entry holds a reference to the descriptor
376  * and the data buffer. For TX rings the reference to the
377  * sk_buff of the packet being transmitted is also stored here.
378  */
379 struct data_entry {
380         /*
381          * Status flags
382          */
383         unsigned long flags;
384 #define ENTRY_OWNER_NIC         1
385 #define ENTRY_TXDONE            2
386 #define ENTRY_TXD_RTS_FRAME     3
387 #define ENTRY_TXD_OFDM_RATE     4
388 #define ENTRY_TXD_MORE_FRAG     5
389 #define ENTRY_TXD_REQ_TIMESTAMP 6
390 #define ENTRY_TXD_REQ_ACK       7
391
392         /*
393          * Ring we belong to.
394          */
395         struct data_ring *ring;
396
397         /*
398          * sk_buff for the packet which is being transmitted
399          * in this entry (Only used with TX related rings).
400          */
401         struct sk_buff *skb;
402
403         /*
404          * Store a ieee80211_tx_status structure in each
405          * ring entry, this will optimize the txdone
406          * handler.
407          */
408         struct ieee80211_tx_status tx_status;
409
410         /*
411          * private pointer specific to driver.
412          */
413         void *priv;
414
415         /*
416          * Data address for this entry.
417          */
418         void *data_addr;
419         dma_addr_t data_dma;
420 };
421
422 /*
423  * data_ring
424  * Data rings are used by the device to send and receive packets.
425  * The data_addr is the base address of the data memory.
426  * To determine at which point in the ring we are,
427  * have to use the rt2x00_ring_index_*() functions.
428  */
429 struct data_ring {
430         /*
431          * Pointer to main rt2x00dev structure where this
432          * ring belongs to.
433          */
434         struct rt2x00_dev *rt2x00dev;
435
436         /*
437          * Base address for the device specific data entries.
438          */
439         struct data_entry *entry;
440
441         /*
442          * TX queue statistic info.
443          */
444         struct ieee80211_tx_queue_stats_data stats;
445
446         /*
447          * TX Queue parameters.
448          */
449         struct ieee80211_tx_queue_params tx_params;
450
451         /*
452          * Base address for data ring.
453          */
454         dma_addr_t data_dma;
455         void *data_addr;
456
457         /*
458          * Index variables.
459          */
460         u16 index;
461         u16 index_done;
462
463         /*
464          * Size of packet and descriptor in bytes.
465          */
466         u16 data_size;
467         u16 desc_size;
468 };
469
470 /*
471  * Handlers to determine the address of the current device specific
472  * data entry, where either index or index_done points to.
473  */
474 static inline struct data_entry* rt2x00_get_data_entry(
475         struct data_ring *ring)
476 {
477         return &ring->entry[ring->index];
478 }
479
480 static inline struct data_entry* rt2x00_get_data_entry_done(
481         struct data_ring *ring)
482 {
483         return &ring->entry[ring->index_done];
484 }
485
486 /*
487  * Total ring memory
488  */
489 static inline int rt2x00_get_ring_size(struct data_ring *ring)
490 {
491         return ring->stats.limit * (ring->desc_size + ring->data_size);
492 }
493
494 /*
495  * Ring index manipulation functions.
496  */
497 static inline void rt2x00_ring_index_inc(struct data_ring *ring)
498 {
499         ring->index++;
500         if (ring->index >= ring->stats.limit)
501                 ring->index = 0;
502         ring->stats.len++;
503 }
504
505 static inline void rt2x00_ring_index_done_inc(struct data_ring *ring)
506 {
507         ring->index_done++;
508         if (ring->index_done >= ring->stats.limit)
509                 ring->index_done = 0;
510         ring->stats.len--;
511         ring->stats.count++;
512 }
513
514 static inline void rt2x00_ring_index_clear(struct data_ring *ring)
515 {
516         ring->index = 0;
517         ring->index_done = 0;
518         ring->stats.len = 0;
519         ring->stats.count = 0;
520 }
521
522 static inline int rt2x00_ring_empty(struct data_ring *ring)
523 {
524         return ring->stats.len == 0;
525 }
526
527 static inline int rt2x00_ring_full(struct data_ring *ring)
528 {
529         return ring->stats.len == ring->stats.limit;
530 }
531
532 static inline int rt2x00_ring_free(struct data_ring *ring)
533 {
534         if (ring->index_done >= ring->index)
535                 return ring->index_done - ring->index;
536         return ring->stats.len - (ring->index - ring->index_done);
537 }
538
539 /*
540  * TX/RX Descriptor access functions.
541  */
542 static inline void rt2x00_desc_read(struct data_desc *desc,
543         const u8 word, u32 *value)
544 {
545         *value = le32_to_cpu(desc->word[word]);
546 }
547
548 static inline void rt2x00_desc_write(struct data_desc *desc,
549         const u8 word, const u32 value)
550 {
551         desc->word[word] = cpu_to_le32(value);
552 }
553
554 /*
555  * To optimize the quality of the link we need to store
556  * the quality of received frames and periodically
557  * optimize the link.
558  */
559 struct link {
560         /*
561          * Link tuner counter
562          * The number of times the link has been tuned
563          * since the radio has been switched on.
564          */
565         u32 count;
566
567         /*
568          * Misc statistics.
569          * For the average RSSI value we use the "Walking average" approach.
570          * When adding RSSI to the average value the following calculation
571          * is needed:
572          *
573          *      avg_rssi = ((avg_rssi * 7) + rssi) / 8;
574          *
575          * The advantage of this approach is that we only need 1 variable
576          * to store the average in (No need for a count and a total).
577          * But more importantly, normal average values will over time
578          * move less and less towards newly added values.
579          * This means that with link tuning, the device can have a very
580          * good RSSI for a few minutes but when the device is moved away
581          * from the AP the average will not decrease fast enough to
582          * compensate. The walking average compensates this and will
583          * move towards the new values correctly.
584          */
585         int avg_rssi;
586         int false_cca;
587
588         /*
589          * Work structure for scheduling periodic link tuning.
590          */
591         struct delayed_work work;
592 };
593
594 /*
595  * Interface structure
596  * Configuration details about the current interface.
597  */
598 struct interface {
599         /*
600          * Interface identification. The value is assigned
601          * to us by the 80211 stack, and is used to request
602          * new beacons.
603          */
604         int id;
605
606         /*
607          * Current working type (IEEE80211_IF_TYPE_*).
608          * This excludes the type IEEE80211_IF_TYPE_MNTR
609          * since that is counted seperately in the monitor_count
610          * field.
611          */
612         int type;
613
614         /*
615          * BBSID of the AP to associate with.
616          */
617         u8 bssid[ETH_ALEN];
618
619         /*
620          * Store the promisc mode for the current interface.
621          * monitor mode always forces promisc mode to be enabled,
622          * so we need to store the promisc mode seperately.
623          */
624         short promisc;
625
626         /*
627          * Monitor mode count, the number of interfaces
628          * in monitor mode that that have been added.
629          */
630         short monitor_count;
631 };
632
633 static inline int is_interface_present(struct interface *intf)
634 {
635         return !!intf->id;
636 }
637
638 static inline int is_monitor_present(struct interface *intf)
639 {
640         return !!intf->monitor_count;
641 }
642
643 /*
644  * Details about the supported modes, rates and channels
645  * of a particular chipset. This is used by rt2x00lib
646  * to build the ieee80211_hw_mode array for mac80211.
647  */
648 struct hw_mode_spec {
649         /*
650          * Number of modes, rates and channels.
651          */
652         int num_modes;
653         int num_rates;
654         int num_channels;
655
656         /*
657          * txpower values.
658          */
659         const u8 *tx_power_a;
660         const u8 *tx_power_bg;
661         u8 tx_power_default;
662
663         /*
664          * Device/chipset specific value.
665          */
666         const u32 *chan_val_a;
667         const u32 *chan_val_bg;
668 };
669
670 /*
671  * rt2x00lib callback functions.
672  */
673 struct rt2x00lib_ops {
674         /*
675          * Interrupt handlers.
676          */
677         irq_handler_t irq_handler;
678
679         /*
680          * Device init handlers.
681          */
682         int (*init_hw)(struct rt2x00_dev *rt2x00dev);
683         char* (*get_fw_name)(struct rt2x00_dev *rt2x00dev);
684         int (*load_firmware)(struct rt2x00_dev *rt2x00dev, void *data,
685                 const size_t len);
686
687         /*
688          * Device initialization/deinitialization handlers.
689          */
690         int (*initialize)(struct rt2x00_dev *rt2x00dev);
691         void (*uninitialize)(struct rt2x00_dev *rt2x00dev);
692
693         /*
694          * Radio control handlers.
695          */
696         int (*set_device_state)(struct rt2x00_dev *rt2x00dev,
697                 enum dev_state state);
698         int (*rfkill_poll)(struct rt2x00_dev *rt2x00dev);
699         void (*link_tuner)(struct rt2x00_dev *rt2x00dev);
700
701         /*
702          * TX control handlers
703          */
704         void (*write_tx_desc)(struct rt2x00_dev *rt2x00dev,
705                 struct data_entry *entry, struct data_desc *txd,
706                 struct data_entry_desc *desc,
707                 struct ieee80211_hdr *ieee80211hdr, unsigned int length,
708                 struct ieee80211_tx_control *control);
709         int (*write_tx_data)(struct rt2x00_dev *rt2x00dev,
710                 struct data_ring *ring, struct sk_buff *skb,
711                 struct ieee80211_tx_control *control);
712         void (*kick_tx_queue)(struct rt2x00_dev *rt2x00dev, int queue);
713
714         /*
715          * RX control handlers
716          */
717         int (*fill_rxdone)(struct data_entry *entry,
718                 int *signal, int *rssi, int *ofdm);
719
720         /*
721          * Configuration handlers.
722          */
723         void (*config_type)(struct rt2x00_dev *rt2x00dev, const int type);
724         void (*config_phymode)(struct rt2x00_dev *rt2x00dev, const int phy);
725         void (*config_channel)(struct rt2x00_dev *rt2x00dev, const int value,
726                 const int channel, const int txpower);
727         void (*config_mac_addr)(struct rt2x00_dev *rt2x00dev, u8 *mac);
728         void (*config_bssid)(struct rt2x00_dev *rt2x00dev, u8 *bssid);
729         void (*config_promisc)(struct rt2x00_dev *rt2x00dev, const int promisc);
730         void (*config_txpower)(struct rt2x00_dev *rt2x00dev, const int txpower);
731         void (*config_antenna)(struct rt2x00_dev *rt2x00dev,
732                 const int antenna_tx, const int antenna_rx);
733         void (*config_duration)(struct rt2x00_dev *rt2x00dev,
734                 const int short_slot_time, const int beacon_int);
735 };
736
737 /*
738  * rt2x00 driver callback operation structure.
739  */
740 struct rt2x00_ops {
741         const char *name;
742         const unsigned int rxd_size;
743         const unsigned int txd_size;
744         const struct rt2x00lib_ops *lib;
745         const struct ieee80211_ops *hw;
746 #ifdef CONFIG_RT2X00_LIB_DEBUGFS
747         const struct rt2x00debug *debugfs;
748 #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
749 };
750
751 /*
752  * rt2x00 device structure.
753  */
754 struct rt2x00_dev {
755         /*
756          * Device structure.
757          * The structure stored in here depends on the
758          * system bus (PCI or USB).
759          * When accessing this variable, the rt2x00dev_{pci,usb}
760          * macro's should be used for correct typecasting.
761          */
762         void *dev;
763 #define rt2x00dev_pci(__dev)    ( (struct pci_dev*)(__dev)->dev )
764 #define rt2x00dev_usb(__dev)    ( (struct usb_interface*)(__dev)->dev )
765
766         /*
767          * Callback functions.
768          */
769         const struct rt2x00_ops *ops;
770
771         /*
772          * IEEE80211 control structure.
773          */
774         struct ieee80211_hw *hw;
775         struct ieee80211_hw_mode *hwmodes;
776         unsigned int curr_hwmode;
777 #define HWMODE_B        0
778 #define HWMODE_G        1
779 #define HWMODE_A        2
780
781         /*
782          * rfkill structure for RF state switching support.
783          * This will only be compiled in when required.
784          */
785 #ifdef CONFIG_RT2X00_LIB_RFKILL
786         struct rfkill *rfkill;
787         struct delayed_work rfkill_work;
788 #endif /* CONFIG_RT2X00_LIB_RFKILL */
789
790         /*
791          * Device flags.
792          * In these flags the current status and some
793          * of the device capabilities are stored.
794          */
795         unsigned long flags;
796 #define DEVICE_ENABLED_RADIO            1
797 #define DEVICE_ENABLED_RADIO_HW         2
798 #define DEVICE_INITIALIZED              3
799 #define DEVICE_INITIALIZED_HW           4
800 #define FIRMWARE_REQUIRED               5
801 #define FIRMWARE_LOADED                 6
802 #define FIRMWARE_FAILED                 7
803 #define INTERFACE_ENABLED               8
804 #define INTERFACE_ENABLED_MONITOR       9
805 #define INTERFACE_ENABLED_PROMISC       10
806 #define DEVICE_SUPPORT_ATIM             11
807 #define DEVICE_SUPPORT_HW_BUTTON        12
808 #define CONFIG_FRAME_TYPE               13
809 #define CONFIG_RF_SEQUENCE              14
810 #define CONFIG_EXTERNAL_LNA             15
811 #define CONFIG_EXTERNAL_LNA_A           16
812 #define CONFIG_EXTERNAL_LNA_BG          17
813 #define CONFIG_DOUBLE_ANTENNA           18
814 #define CONFIG_DISABLE_LINK_TUNING      19
815
816         /*
817          * Chipset identification.
818          */
819         struct rt2x00_chip chip;
820
821         /*
822          * hw capability specifications.
823          */
824         struct hw_mode_spec spec;
825
826         /*
827          * Base address of device registers (PCI devices only).
828          */
829         void __iomem *csr_addr;
830
831         /*
832          * If enabled, the debugfs interface structures
833          * required for deregistration of debugfs.
834          */
835 #ifdef CONFIG_RT2X00_LIB_DEBUGFS
836         const struct rt2x00debug_intf *debugfs_intf;
837 #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
838
839         /*
840          * Interface configuration.
841          */
842         struct interface interface;
843
844         /*
845          * Link quality
846          */
847         struct link link;
848
849         /*
850          * EEPROM data.
851          */
852         __le16 *eeprom;
853
854         /*
855          * Active RF register values.
856          * These are stored here for easier working
857          * with the rf registers.
858          */
859         u32 rf1;
860         u32 rf2;
861         u32 rf3;
862         u32 rf4;
863
864         /*
865          * Current TX power value.
866          */
867         u16 tx_power;
868
869         /*
870          * LED register (for rt61pci & rt73usb).
871          */
872         u16 led_reg;
873
874         /*
875          * Led mode (LED_MODE_*)
876          */
877         u8 led_mode;
878
879         /*
880          * Rssi <-> Dbm offset
881          */
882         u8 rssi_offset;
883
884         /*
885          * Frequency offset (for rt61pci & rt73usb).
886          */
887         u8 freq_offset;
888
889         /*
890          * Low level statistics which will have
891          * to be kept up to date while device is running.
892          */
893         struct ieee80211_low_level_stats low_level_stats;
894
895         /*
896          * RX configuration information.
897          */
898         struct ieee80211_rx_status rx_status;
899
900         /*
901          * Data ring arrays for RX, TX and Beacon.
902          * The Beacon array also contains the Atim ring
903          * if that is supported by the device.
904          */
905         struct data_ring *rx;
906         struct data_ring *tx;
907         struct data_ring *bcn;
908 };
909
910 static inline struct data_ring* rt2x00_get_ring(
911         struct rt2x00_dev *rt2x00dev, const unsigned int queue)
912 {
913         int atim = test_bit(DEVICE_SUPPORT_ATIM, &rt2x00dev->flags);
914
915         /*
916          * Check if the rings have been allocated.
917          */
918         if (!rt2x00dev->tx || !rt2x00dev->bcn)
919                 return NULL;
920
921         /*
922          * Check if we are requesting a reqular TX ring,
923          * or if we are requesting a Beacon or Atim ring.
924          * For Atim rings, we should check if it is supported.
925          */
926         if (queue < rt2x00dev->hw->queues)
927                 return &rt2x00dev->tx[queue];
928         else if (queue == IEEE80211_TX_QUEUE_BEACON)
929                 return &rt2x00dev->bcn[0];
930         else if (queue == IEEE80211_TX_QUEUE_AFTER_BEACON && atim)
931                 return &rt2x00dev->bcn[1];
932
933         return NULL;
934 }
935
936 /*
937  * For-each loop for the ring array.
938  * Since the all rings are allocated as a single array,
939  * we can start at the rx pointer and move forward to the tx rings.
940  * The 1 + Atim check will assure that the address directly after
941  * the ring array is obtained and the for-each loop exits correctly.
942  */
943 #define ring_end(__dev) \
944         &(__dev)->bcn[1 + test_bit(DEVICE_SUPPORT_ATIM, &rt2x00dev->flags)]
945
946 #define ring_loop(__entry, __start, __end)                      \
947         for ((__entry) = (__start);                             \
948              prefetch(&(__entry)[1]), (__entry) != (__end);     \
949              (__entry) = &(__entry)[1])
950
951 #define ring_for_each(__dev, __entry) \
952         ring_loop(__entry, (__dev)->rx, ring_end(__dev))
953
954 #define txring_for_each(__dev, __entry) \
955         ring_loop(__entry, (__dev)->tx, (__dev)->bcn)
956
957 #define txringall_for_each(__dev, __entry) \
958         ring_loop(__entry, (__dev)->tx, ring_end(__dev))
959
960 /*
961  * EEPROM access.
962  * The EEPROM is being accessed by word index.
963  */
964 static inline void* rt2x00_eeprom_addr(const struct rt2x00_dev *rt2x00dev,
965         const u8 word)
966 {
967         return (void*)&rt2x00dev->eeprom[word];
968 }
969
970 static inline void rt2x00_eeprom_read(const struct rt2x00_dev *rt2x00dev,
971         const u8 word, u16 *data)
972 {
973         *data = le16_to_cpu(rt2x00dev->eeprom[word]);
974 }
975
976 static inline void rt2x00_eeprom_write(const struct rt2x00_dev *rt2x00dev,
977         const u8 word, u16 data)
978 {
979         rt2x00dev->eeprom[word] = cpu_to_le16(data);
980 }
981
982 /*
983  * Link tuning handlers
984  */
985 static inline void rt2x00_start_link_tune(struct rt2x00_dev *rt2x00dev)
986 {
987         rt2x00dev->link.count = 0;
988         rt2x00dev->link.avg_rssi = 0;
989         rt2x00dev->link.false_cca = 0;
990
991         queue_delayed_work(rt2x00dev->hw->workqueue,
992                 &rt2x00dev->link.work, LINK_TUNE_INTERVAL);
993 }
994
995 static inline void rt2x00_stop_link_tune(struct rt2x00_dev *rt2x00dev)
996 {
997         if (work_pending(&rt2x00dev->link.work.work))
998                 cancel_rearming_delayed_workqueue(
999                         rt2x00dev->hw->workqueue, &rt2x00dev->link.work);
1000 }
1001
1002 static inline void rt2x00_update_link_rssi(struct link *link, int rssi)
1003 {
1004         if (!link->avg_rssi)
1005                 link->avg_rssi = rssi;
1006         else
1007                 link->avg_rssi = ((link->avg_rssi * 7) + rssi) / 8;
1008 }
1009
1010 static inline int rt2x00_get_link_rssi(struct link *link)
1011 {
1012         return link->avg_rssi;
1013 }
1014
1015 /*
1016  * Chipset handlers
1017  */
1018 static inline void rt2x00_set_chip(struct rt2x00_dev *rt2x00dev,
1019         const u16 rt, const u16 rf, const u32 rev)
1020 {
1021         INFO(rt2x00dev,
1022                 "Chipset detected - rt: %04x, rf: %04x, rev: %08x.\n",
1023                 rt, rf, rev);
1024
1025         rt2x00dev->chip.rt = rt;
1026         rt2x00dev->chip.rf = rf;
1027         rt2x00dev->chip.rev = rev;
1028 }
1029
1030 static inline char rt2x00_rt(const struct rt2x00_chip *chipset, const u16 chip)
1031 {
1032         return (chipset->rt == chip);
1033 }
1034
1035 static inline char rt2x00_rf(const struct rt2x00_chip *chipset, const u16 chip)
1036 {
1037         return (chipset->rf == chip);
1038 }
1039
1040 static inline u16 rt2x00_rev(const struct rt2x00_chip *chipset)
1041 {
1042         return chipset->rev;
1043 }
1044
1045 /*
1046  * Device specific rate value.
1047  * We will have to create the device specific rate value
1048  * passed to the ieee80211 kernel. We need to make it a consist of
1049  * multiple fields because we want to store more then 1 device specific
1050  * values inside the value.
1051  *      1 - rate, stored as 100 kbit/s.
1052  *      2 - preamble, short_preamble enabled flag.
1053  *      3 - MASK_RATE, which rates are enabled in this mode, this mask
1054  *      corresponds with the TX register format for the current device.
1055  *      4 - plcp, 802.11b rates are device specific,
1056  *      802.11g rates are set according to the ieee802.11a-1999 p.14.
1057  * The bit to enable preamble is set in a seperate define.
1058  */
1059 #define DEV_RATE        FIELD32(0x000007ff)
1060 #define DEV_PREAMBLE    FIELD32(0x00000800)
1061 #define DEV_RATEMASK    FIELD32(0x00fff000)
1062 #define DEV_PLCP        FIELD32(0xff000000)
1063
1064 /*
1065  * Bitmask for MASK_RATE
1066  */
1067 #define DEV_RATE_1MB    0x00000001
1068 #define DEV_RATE_2MB    0x00000002
1069 #define DEV_RATE_5_5MB  0x00000004
1070 #define DEV_RATE_11MB   0x00000008
1071 #define DEV_RATE_6MB    0x00000010
1072 #define DEV_RATE_9MB    0x00000020
1073 #define DEV_RATE_12MB   0x00000040
1074 #define DEV_RATE_18MB   0x00000080
1075 #define DEV_RATE_24MB   0x00000100
1076 #define DEV_RATE_36MB   0x00000200
1077 #define DEV_RATE_48MB   0x00000400
1078 #define DEV_RATE_54MB   0x00000800
1079
1080 /*
1081  * Bitmask groups of bitrates
1082  */
1083 #define DEV_BASIC_RATE \
1084         ( DEV_RATE_1MB | DEV_RATE_2MB | DEV_RATE_5_5MB | DEV_RATE_11MB | \
1085           DEV_RATE_6MB | DEV_RATE_12MB | DEV_RATE_24MB )
1086
1087 #define DEV_CCK_RATE \
1088         ( DEV_RATE_1MB | DEV_RATE_2MB | DEV_RATE_5_5MB | DEV_RATE_11MB )
1089
1090 #define DEV_OFDM_RATE \
1091         ( DEV_RATE_6MB | DEV_RATE_9MB | DEV_RATE_12MB | DEV_RATE_18MB | \
1092           DEV_RATE_24MB | DEV_RATE_36MB | DEV_RATE_48MB | DEV_RATE_54MB )
1093
1094 /*
1095  * Macro's to set and get specific fields from the device specific val and val2
1096  * fields inside the ieee80211_rate entry.
1097  */
1098 #define DEVICE_SET_RATE_FIELD(__value, __mask) \
1099         (int)( ((__value) << DEV_##__mask.bit_offset) & DEV_##__mask.bit_mask )
1100
1101 #define DEVICE_GET_RATE_FIELD(__value, __mask) \
1102         (int)( ((__value) & DEV_##__mask.bit_mask) >> DEV_##__mask.bit_offset )
1103
1104 /*
1105  * Duration calculations
1106  * The rate variable passed is: 100kbs.
1107  * To convert from bytes to bits we multiply size with 8,
1108  * then the size is multiplied with 10 to make the
1109  * real rate -> rate argument correction.
1110  */
1111 static inline u16 get_duration(const unsigned int size, const u8 rate)
1112 {
1113         return ((size * 8 * 10) / rate);
1114 }
1115
1116 static inline u16 get_duration_res(const unsigned int size, const u8 rate)
1117 {
1118         return ((size * 8 * 10) % rate);
1119 }
1120
1121 #endif /* RT2X00_H */