2 Copyright (C) 2004 - 2007 rt2x00 SourceForge Project
3 <http://rt2x00.serialmonkey.com>
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.
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.
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.
23 Abstract: rt2x00 global information.
29 #include <linux/bitops.h>
30 #include <linux/prefetch.h>
31 #include <linux/skbuff.h>
32 #include <linux/workqueue.h>
33 #include <linux/firmware.h>
35 #include <net/mac80211.h>
37 #include "rt2x00debug.h"
38 #include "rt2x00reg.h"
39 #include "rt2x00ring.h"
43 * DRV_NAME should be set within the individual module source files.
45 #define DRV_VERSION "2.0.8"
46 #define DRV_PROJECT "http://rt2x00.serialmonkey.com"
50 * Debug output has to be enabled during compile time.
52 #define DEBUG_PRINTK_MSG(__dev, __kernlvl, __lvl, __msg, __args...) \
53 printk(__kernlvl "%s -> %s: %s - " __msg, \
54 wiphy_name((__dev)->hw->wiphy), __FUNCTION__, __lvl, ##__args)
56 #define DEBUG_PRINTK_PROBE(__kernlvl, __lvl, __msg, __args...) \
57 printk(__kernlvl "%s -> %s: %s - " __msg, \
58 DRV_NAME, __FUNCTION__, __lvl, ##__args)
60 #ifdef CONFIG_RT2X00_DEBUG
61 #define DEBUG_PRINTK(__dev, __kernlvl, __lvl, __msg, __args...) \
62 DEBUG_PRINTK_MSG(__dev, __kernlvl, __lvl, __msg, ##__args);
64 #define DEBUG_PRINTK(__dev, __kernlvl, __lvl, __msg, __args...) \
66 #endif /* CONFIG_RT2X00_DEBUG */
69 * Various debug levels.
70 * The debug levels PANIC and ERROR both indicate serious problems,
71 * for this reason they should never be ignored.
72 * The special ERROR_PROBE message is for messages that are generated
73 * when the rt2x00_dev is not yet initialized.
75 #define PANIC(__dev, __msg, __args...) \
76 DEBUG_PRINTK_MSG(__dev, KERN_CRIT, "Panic", __msg, ##__args)
77 #define ERROR(__dev, __msg, __args...) \
78 DEBUG_PRINTK_MSG(__dev, KERN_ERR, "Error", __msg, ##__args)
79 #define ERROR_PROBE(__msg, __args...) \
80 DEBUG_PRINTK_PROBE(KERN_ERR, "Error", __msg, ##__args)
81 #define WARNING(__dev, __msg, __args...) \
82 DEBUG_PRINTK(__dev, KERN_WARNING, "Warning", __msg, ##__args)
83 #define NOTICE(__dev, __msg, __args...) \
84 DEBUG_PRINTK(__dev, KERN_NOTICE, "Notice", __msg, ##__args)
85 #define INFO(__dev, __msg, __args...) \
86 DEBUG_PRINTK(__dev, KERN_INFO, "Info", __msg, ##__args)
87 #define DEBUG(__dev, __msg, __args...) \
88 DEBUG_PRINTK(__dev, KERN_DEBUG, "Debug", __msg, ##__args)
89 #define EEPROM(__dev, __msg, __args...) \
90 DEBUG_PRINTK(__dev, KERN_DEBUG, "EEPROM recovery", __msg, ##__args)
94 * Ralink PCI devices demand the Frame size to be a multiple of 128 bytes.
95 * DATA_FRAME_SIZE is used for TX, RX, ATIM and PRIO rings.
96 * MGMT_FRAME_SIZE is used for the BEACON ring.
98 #define DATA_FRAME_SIZE 2432
99 #define MGMT_FRAME_SIZE 256
102 * Number of entries in a packet ring.
103 * PCI devices only need 1 Beacon entry,
104 * but USB devices require a second because they
105 * have to send a Guardian byte first.
107 #define RX_ENTRIES 12
108 #define TX_ENTRIES 12
109 #define ATIM_ENTRIES 1
110 #define BEACON_ENTRIES 2
113 * Standard timing and size defines.
114 * These values should follow the ieee80211 specifications.
117 #define IEEE80211_HEADER 24
121 #define SHORT_PREAMBLE 72
123 #define SHORT_SLOT_TIME 9
125 #define PIFS ( SIFS + SLOT_TIME )
126 #define SHORT_PIFS ( SIFS + SHORT_SLOT_TIME )
127 #define DIFS ( PIFS + SLOT_TIME )
128 #define SHORT_DIFS ( SHORT_PIFS + SHORT_SLOT_TIME )
129 #define EIFS ( SIFS + (8 * (IEEE80211_HEADER + ACK_SIZE)) )
132 * IEEE802.11 header defines
134 static inline int is_rts_frame(u16 fc
)
136 return !!(((fc
& IEEE80211_FCTL_FTYPE
) == IEEE80211_FTYPE_CTL
) &&
137 ((fc
& IEEE80211_FCTL_STYPE
) == IEEE80211_STYPE_RTS
));
140 static inline int is_cts_frame(u16 fc
)
142 return !!(((fc
& IEEE80211_FCTL_FTYPE
) == IEEE80211_FTYPE_CTL
) &&
143 ((fc
& IEEE80211_FCTL_STYPE
) == IEEE80211_STYPE_CTS
));
146 static inline int is_probe_resp(u16 fc
)
148 return !!(((fc
& IEEE80211_FCTL_FTYPE
) == IEEE80211_FTYPE_MGMT
) &&
149 ((fc
& IEEE80211_FCTL_STYPE
) == IEEE80211_STYPE_PROBE_RESP
));
153 * Chipset identification
154 * The chipset on the device is composed of a RT and RF chip.
155 * The chipset combination is important for determining device capabilities.
159 #define RT2460 0x0101
160 #define RT2560 0x0201
161 #define RT2570 0x1201
162 #define RT2561 0x0301
163 #define RT2561s 0x0302
164 #define RT2661 0x0401
165 #define RT2571 0x1300
172 * RF register values that belong to a particular channel.
183 * To optimize the quality of the link we need to store
184 * the quality of received frames and periodically
190 * The number of times the link has been tuned
191 * since the radio has been switched on.
196 * Statistics required for Link tuning.
197 * For the average RSSI value we use the "Walking average" approach.
198 * When adding RSSI to the average value the following calculation
201 * avg_rssi = ((avg_rssi * 7) + rssi) / 8;
203 * The advantage of this approach is that we only need 1 variable
204 * to store the average in (No need for a count and a total).
205 * But more importantly, normal average values will over time
206 * move less and less towards newly added values this results
207 * that with link tuning, the device can have a very good RSSI
208 * for a few minutes but when the device is moved away from the AP
209 * the average will not decrease fast enough to compensate.
210 * The walking average compensates this and will move towards
211 * the new values correctly allowing a effective link tuning.
218 * Statistics required for Signal quality calculation.
219 * For calculating the Signal quality we have to determine
220 * the total number of success and failed RX and TX frames.
221 * After that we also use the average RSSI value to help
222 * determining the signal quality.
223 * For the calculation we will use the following algorithm:
225 * rssi_percentage = (avg_rssi * 100) / rssi_offset
226 * rx_percentage = (rx_success * 100) / rx_total
227 * tx_percentage = (tx_success * 100) / tx_total
228 * avg_signal = ((WEIGHT_RSSI * avg_rssi) +
229 * (WEIGHT_TX * tx_percentage) +
230 * (WEIGHT_RX * rx_percentage)) / 100
232 * This value should then be checked to not be greated then 100.
240 #define WEIGHT_RSSI 20
245 * Work structure for scheduling periodic link tuning.
247 struct delayed_work work
;
251 * Clear all counters inside the link structure.
252 * This can be easiest achieved by memsetting everything
253 * except for the work structure at the end.
255 static inline void rt2x00_clear_link(struct link
*link
)
257 memset(link
, 0x00, sizeof(*link
) - sizeof(link
->work
));
258 link
->rx_percentage
= 50;
259 link
->tx_percentage
= 50;
263 * Update the rssi using the walking average approach.
265 static inline void rt2x00_update_link_rssi(struct link
*link
, int rssi
)
268 link
->avg_rssi
= rssi
;
270 link
->avg_rssi
= ((link
->avg_rssi
* 7) + rssi
) / 8;
274 * When the avg_rssi is unset or no frames have been received),
275 * we need to return the default value which needs to be less
276 * than -80 so the device will select the maximum sensitivity.
278 static inline int rt2x00_get_link_rssi(struct link
*link
)
280 return (link
->avg_rssi
&& link
->rx_success
) ? link
->avg_rssi
: -128;
284 * Interface structure
285 * Configuration details about the current interface.
289 * Interface identification. The value is assigned
290 * to us by the 80211 stack, and is used to request
296 * Current working type (IEEE80211_IF_TYPE_*).
297 * This excludes the type IEEE80211_IF_TYPE_MNTR
298 * since that is counted seperately in the monitor_count
300 * When set to INVALID_INTERFACE, no interface is configured.
303 #define INVALID_INTERFACE IEEE80211_IF_TYPE_MGMT
311 * BBSID of the AP to associate with.
316 * Store the packet filter mode for the current interface.
317 * monitor mode always disabled filtering. But in such
318 * cases we still need to store the value here in case
319 * the monitor mode interfaces are removed, while a
320 * non-monitor mode interface remains.
322 unsigned short filter
;
325 * Monitor mode count, the number of interfaces
326 * in monitor mode that that have been added.
328 unsigned short monitor_count
;
331 static inline int is_interface_present(struct interface
*intf
)
336 static inline int is_monitor_present(struct interface
*intf
)
338 return !!intf
->monitor_count
;
342 * Details about the supported modes, rates and channels
343 * of a particular chipset. This is used by rt2x00lib
344 * to build the ieee80211_hw_mode array for mac80211.
346 struct hw_mode_spec
{
348 * Number of modes, rates and channels.
357 const u8
*tx_power_a
;
358 const u8
*tx_power_bg
;
362 * Device/chipset specific value.
364 const struct rf_channel
*channels
;
368 * rt2x00lib callback functions.
370 struct rt2x00lib_ops
{
372 * Interrupt handlers.
374 irq_handler_t irq_handler
;
377 * Device init handlers.
379 int (*probe_hw
) (struct rt2x00_dev
*rt2x00dev
);
380 char *(*get_firmware_name
) (struct rt2x00_dev
*rt2x00dev
);
381 int (*load_firmware
) (struct rt2x00_dev
*rt2x00dev
, void *data
,
385 * Device initialization/deinitialization handlers.
387 int (*initialize
) (struct rt2x00_dev
*rt2x00dev
);
388 void (*uninitialize
) (struct rt2x00_dev
*rt2x00dev
);
391 * Radio control handlers.
393 int (*set_device_state
) (struct rt2x00_dev
*rt2x00dev
,
394 enum dev_state state
);
395 int (*rfkill_poll
) (struct rt2x00_dev
*rt2x00dev
);
396 void (*link_stats
) (struct rt2x00_dev
*rt2x00dev
);
397 void (*reset_tuner
) (struct rt2x00_dev
*rt2x00dev
);
398 void (*link_tuner
) (struct rt2x00_dev
*rt2x00dev
);
401 * TX control handlers
403 void (*write_tx_desc
) (struct rt2x00_dev
*rt2x00dev
,
404 struct data_desc
*txd
,
405 struct data_entry_desc
*desc
,
406 struct ieee80211_hdr
*ieee80211hdr
,
408 struct ieee80211_tx_control
*control
);
409 int (*write_tx_data
) (struct rt2x00_dev
*rt2x00dev
,
410 struct data_ring
*ring
, struct sk_buff
*skb
,
411 struct ieee80211_tx_control
*control
);
412 void (*kick_tx_queue
) (struct rt2x00_dev
*rt2x00dev
,
416 * RX control handlers
418 int (*fill_rxdone
) (struct data_entry
*entry
,
419 int *signal
, int *rssi
, int *ofdm
, int *size
);
422 * Configuration handlers.
424 void (*config_mac_addr
) (struct rt2x00_dev
*rt2x00dev
, u8
*mac
);
425 void (*config_bssid
) (struct rt2x00_dev
*rt2x00dev
, u8
*bssid
);
426 void (*config_packet_filter
) (struct rt2x00_dev
*rt2x00dev
,
427 const unsigned int filter
);
428 void (*config_type
) (struct rt2x00_dev
*rt2x00dev
, const int type
);
429 void (*config
) (struct rt2x00_dev
*rt2x00dev
, const unsigned int flags
,
430 struct ieee80211_conf
*conf
);
431 #define CONFIG_UPDATE_PHYMODE ( 1 << 1 )
432 #define CONFIG_UPDATE_CHANNEL ( 1 << 2 )
433 #define CONFIG_UPDATE_TXPOWER ( 1 << 3 )
434 #define CONFIG_UPDATE_ANTENNA ( 1 << 4 )
435 #define CONFIG_UPDATE_SLOT_TIME ( 1 << 5 )
436 #define CONFIG_UPDATE_BEACON_INT ( 1 << 6 )
437 #define CONFIG_UPDATE_ALL 0xffff
441 * rt2x00 driver callback operation structure.
445 const unsigned int rxd_size
;
446 const unsigned int txd_size
;
447 const unsigned int eeprom_size
;
448 const unsigned int rf_size
;
449 const struct rt2x00lib_ops
*lib
;
450 const struct ieee80211_ops
*hw
;
451 #ifdef CONFIG_RT2X00_LIB_DEBUGFS
452 const struct rt2x00debug
*debugfs
;
453 #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
457 * rt2x00 device structure.
462 * The structure stored in here depends on the
463 * system bus (PCI or USB).
464 * When accessing this variable, the rt2x00dev_{pci,usb}
465 * macro's should be used for correct typecasting.
468 #define rt2x00dev_pci(__dev) ( (struct pci_dev*)(__dev)->dev )
469 #define rt2x00dev_usb(__dev) ( (struct usb_interface*)(__dev)->dev )
472 * Callback functions.
474 const struct rt2x00_ops
*ops
;
477 * IEEE80211 control structure.
479 struct ieee80211_hw
*hw
;
480 struct ieee80211_hw_mode
*hwmodes
;
481 unsigned int curr_hwmode
;
487 * rfkill structure for RF state switching support.
488 * This will only be compiled in when required.
490 #ifdef CONFIG_RT2X00_LIB_RFKILL
491 struct rfkill
*rfkill
;
492 struct input_polled_dev
*poll_dev
;
493 #endif /* CONFIG_RT2X00_LIB_RFKILL */
496 * If enabled, the debugfs interface structures
497 * required for deregistration of debugfs.
499 #ifdef CONFIG_RT2X00_LIB_DEBUGFS
500 const struct rt2x00debug_intf
*debugfs_intf
;
501 #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
505 * In these flags the current status and some
506 * of the device capabilities are stored.
509 #define DEVICE_ENABLED_RADIO 1
510 #define DEVICE_ENABLED_RADIO_HW 2
511 #define DEVICE_INITIALIZED 3
512 #define DEVICE_INITIALIZED_HW 4
513 #define REQUIRE_FIRMWARE 5
514 #define PACKET_FILTER_SCHEDULED 6
515 #define PACKET_FILTER_PENDING 7
516 #define INTERFACE_RESUME 8
517 #define INTERFACE_ENABLED 9
518 #define INTERFACE_ENABLED_MONITOR 10
519 #define REQUIRE_BEACON_RING 11
520 #define DEVICE_SUPPORT_HW_BUTTON 12
521 #define CONFIG_FRAME_TYPE 13
522 #define CONFIG_RF_SEQUENCE 14
523 /* Hole: Add new Flag here */
524 #define CONFIG_EXTERNAL_LNA_A 16
525 #define CONFIG_EXTERNAL_LNA_BG 17
526 #define CONFIG_DOUBLE_ANTENNA 18
527 #define CONFIG_DISABLE_LINK_TUNING 19
530 * Chipset identification.
532 struct rt2x00_chip chip
;
535 * hw capability specifications.
537 struct hw_mode_spec spec
;
541 * csr_addr: Base register address. (PCI)
542 * csr_cache: CSR cache for usb_control_msg. (USB)
544 void __iomem
*csr_addr
;
548 * Interface configuration.
550 struct interface interface
;
563 * Active RF register values.
564 * These are stored here so we don't need
565 * to read the rf registers and can directly
566 * use this value instead.
567 * This field should be accessed by using
568 * rt2x00_rf_read() and rt2x00_rf_write().
573 * Current TX power value.
578 * LED register (for rt61pci & rt73usb).
583 * Led mode (LED_MODE_*)
588 * Rssi <-> Dbm offset
593 * Frequency offset (for rt61pci & rt73usb).
598 * Low level statistics which will have
599 * to be kept up to date while device is running.
601 struct ieee80211_low_level_stats low_level_stats
;
604 * RX configuration information.
606 struct ieee80211_rx_status rx_status
;
609 * Beacon scheduled work.
611 struct work_struct beacon_work
;
614 * Data ring arrays for RX, TX and Beacon.
615 * The Beacon array also contains the Atim ring
616 * if that is supported by the device.
619 struct data_ring
*rx
;
620 struct data_ring
*tx
;
621 struct data_ring
*bcn
;
626 const struct firmware
*fw
;
630 * For-each loop for the ring array.
631 * All rings have been allocated as a single array,
632 * this means we can create a very simply loop macro
633 * that is capable of looping through all rings.
634 * ring_end(), txring_end() and ring_loop() are helper macro's which
635 * should not be used directly. Instead the following should be used:
636 * ring_for_each() - Loops through all rings (RX, TX, Beacon & Atim)
637 * txring_for_each() - Loops through TX data rings (TX only)
638 * txringall_for_each() - Loops through all TX rings (TX, Beacon & Atim)
640 #define ring_end(__dev) \
641 &(__dev)->rx[(__dev)->data_rings]
643 #define txring_end(__dev) \
644 &(__dev)->tx[(__dev)->hw->queues]
646 #define ring_loop(__entry, __start, __end) \
647 for ((__entry) = (__start); \
648 prefetch(&(__entry)[1]), (__entry) != (__end); \
649 (__entry) = &(__entry)[1])
651 #define ring_for_each(__dev, __entry) \
652 ring_loop(__entry, (__dev)->rx, ring_end(__dev))
654 #define txring_for_each(__dev, __entry) \
655 ring_loop(__entry, (__dev)->tx, txring_end(__dev))
657 #define txringall_for_each(__dev, __entry) \
658 ring_loop(__entry, (__dev)->tx, ring_end(__dev))
662 * The RF is being accessed by word index.
664 static inline void rt2x00_rf_read(const struct rt2x00_dev
*rt2x00dev
,
665 const unsigned int word
, u32
*data
)
667 *data
= rt2x00dev
->rf
[word
];
670 static inline void rt2x00_rf_write(const struct rt2x00_dev
*rt2x00dev
,
671 const unsigned int word
, u32 data
)
673 rt2x00dev
->rf
[word
] = data
;
677 * Generic EEPROM access.
678 * The EEPROM is being accessed by word index.
680 static inline void *rt2x00_eeprom_addr(const struct rt2x00_dev
*rt2x00dev
,
681 const unsigned int word
)
683 return (void *)&rt2x00dev
->eeprom
[word
];
686 static inline void rt2x00_eeprom_read(const struct rt2x00_dev
*rt2x00dev
,
687 const unsigned int word
, u16
*data
)
689 *data
= le16_to_cpu(rt2x00dev
->eeprom
[word
]);
692 static inline void rt2x00_eeprom_write(const struct rt2x00_dev
*rt2x00dev
,
693 const unsigned int word
, u16 data
)
695 rt2x00dev
->eeprom
[word
] = cpu_to_le16(data
);
701 static inline void rt2x00_set_chip(struct rt2x00_dev
*rt2x00dev
,
702 const u16 rt
, const u16 rf
, const u32 rev
)
705 "Chipset detected - rt: %04x, rf: %04x, rev: %08x.\n",
708 rt2x00dev
->chip
.rt
= rt
;
709 rt2x00dev
->chip
.rf
= rf
;
710 rt2x00dev
->chip
.rev
= rev
;
713 static inline char rt2x00_rt(const struct rt2x00_chip
*chipset
, const u16 chip
)
715 return (chipset
->rt
== chip
);
718 static inline char rt2x00_rf(const struct rt2x00_chip
*chipset
, const u16 chip
)
720 return (chipset
->rf
== chip
);
723 static inline u16
rt2x00_get_rev(const struct rt2x00_chip
*chipset
)
728 static inline u16
rt2x00_rev(const struct rt2x00_chip
*chipset
, const u32 mask
)
730 return chipset
->rev
& mask
;
734 * Duration calculations
735 * The rate variable passed is: 100kbs.
736 * To convert from bytes to bits we multiply size with 8,
737 * then the size is multiplied with 10 to make the
738 * real rate -> rate argument correction.
740 static inline u16
get_duration(const unsigned int size
, const u8 rate
)
742 return ((size
* 8 * 10) / rate
);
745 static inline u16
get_duration_res(const unsigned int size
, const u8 rate
)
747 return ((size
* 8 * 10) % rate
);
753 struct data_ring
*rt2x00lib_get_ring(struct rt2x00_dev
*rt2x00dev
,
754 const unsigned int queue
);
757 * Interrupt context handlers.
759 void rt2x00lib_beacondone(struct rt2x00_dev
*rt2x00dev
);
760 void rt2x00lib_txdone(struct data_entry
*entry
,
761 const int status
, const int retry
);
762 void rt2x00lib_rxdone(struct data_entry
*entry
, struct sk_buff
*skb
,
763 const int signal
, const int rssi
, const int ofdm
);
766 * TX descriptor initializer
768 void rt2x00lib_write_tx_desc(struct rt2x00_dev
*rt2x00dev
,
769 struct data_desc
*txd
,
770 struct ieee80211_hdr
*ieee80211hdr
,
772 struct ieee80211_tx_control
*control
);
777 int rt2x00mac_tx(struct ieee80211_hw
*hw
, struct sk_buff
*skb
,
778 struct ieee80211_tx_control
*control
);
779 int rt2x00mac_start(struct ieee80211_hw
*hw
);
780 void rt2x00mac_stop(struct ieee80211_hw
*hw
);
781 int rt2x00mac_add_interface(struct ieee80211_hw
*hw
,
782 struct ieee80211_if_init_conf
*conf
);
783 void rt2x00mac_remove_interface(struct ieee80211_hw
*hw
,
784 struct ieee80211_if_init_conf
*conf
);
785 int rt2x00mac_config(struct ieee80211_hw
*hw
, struct ieee80211_conf
*conf
);
786 int rt2x00mac_config_interface(struct ieee80211_hw
*hw
, int if_id
,
787 struct ieee80211_if_conf
*conf
);
788 void rt2x00mac_set_multicast_list(struct ieee80211_hw
*hw
,
789 unsigned short flags
, int mc_count
);
790 int rt2x00mac_get_stats(struct ieee80211_hw
*hw
,
791 struct ieee80211_low_level_stats
*stats
);
792 int rt2x00mac_get_tx_stats(struct ieee80211_hw
*hw
,
793 struct ieee80211_tx_queue_stats
*stats
);
794 int rt2x00mac_conf_tx(struct ieee80211_hw
*hw
, int queue
,
795 const struct ieee80211_tx_queue_params
*params
);
798 * Driver allocation handlers.
800 int rt2x00lib_probe_dev(struct rt2x00_dev
*rt2x00dev
);
801 void rt2x00lib_remove_dev(struct rt2x00_dev
*rt2x00dev
);
803 int rt2x00lib_suspend(struct rt2x00_dev
*rt2x00dev
, pm_message_t state
);
804 int rt2x00lib_resume(struct rt2x00_dev
*rt2x00dev
);
805 #endif /* CONFIG_PM */
807 #endif /* RT2X00_H */