aboutsummaryrefslogtreecommitdiff
path: root/drivers/net/wireless/iwlwifi/iwl-helpers.h
blob: e2a8d95ad9cd8a5eaa8e9beb13b807594d29a17d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/******************************************************************************
 *
 * Copyright(c) 2003 - 2007 Intel Corporation. All rights reserved.
 *
 * Portions of this file are derived from the ipw3945 project, as well
 * as portions of the ieee80211 subsystem header files.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
 *
 * The full GNU General Public License is included in this distribution in the
 * file called LICENSE.
 *
 * Contact Information:
 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 *
 *****************************************************************************/

#ifndef __iwl_helpers_h__
#define __iwl_helpers_h__

#include <linux/ctype.h>

/*
 * The structures defined by the hardware/uCode interface
 * have bit-wise operations.  For each bit-field there is
 * a data symbol in the structure, the start bit position
 * and the length of the bit-field.
 *
 * iwl_get_bits and iwl_set_bits will return or set the
 * appropriate bits on a 32-bit value.
 *
 * IWL_GET_BITS and IWL_SET_BITS use symbol expansion to
 * expand out to the appropriate call to iwl_get_bits
 * and iwl_set_bits without having to reference all of the
 * numerical constants and defines provided in the hardware
 * definition
 */

/**
 * iwl_get_bits - Extract a hardware bit-field value
 * @src: source hardware value (__le32)
 * @pos: bit-position (0-based) of first bit of value
 * @len: length of bit-field
 *
 * iwl_get_bits will return the bit-field in cpu endian ordering.
 *
 * NOTE:  If used from IWL_GET_BITS then pos and len are compile-constants and
 *        will collapse to minimal code by the compiler.
 */
static inline u32 iwl_get_bits(__le32 src, u8 pos, u8 len)
{
	u32 tmp = le32_to_cpu(src);

	tmp >>= pos;
	tmp &= (1UL << len) - 1;
	return tmp;
}

/**
 * iwl_set_bits - Set a hardware bit-field value
 * @dst: Address of __le32 hardware value
 * @pos: bit-position (0-based) of first bit of value
 * @len: length of bit-field
 * @val: cpu endian value to encode into the bit-field
 *
 * iwl_set_bits will encode val into dst, masked to be len bits long at bit
 * position pos.
 *
 * NOTE:  If used IWL_SET_BITS pos and len will be compile-constants and
 *        will collapse to minimal code by the compiler.
 */
static inline void iwl_set_bits(__le32 *dst, u8 pos, u8 len, int val)
{
	u32 tmp = le32_to_cpu(*dst);

	tmp &= ~(((1UL << len) - 1) << pos);
	tmp |= (val & ((1UL << len) - 1)) << pos;
	*dst = cpu_to_le32(tmp);
}

static inline void iwl_set_bits16(__le16 *dst, u8 pos, u8 len, int val)
{
	u16 tmp = le16_to_cpu(*dst);

	tmp &= ~((1UL << (pos + len)) - (1UL << pos));
	tmp |= (val & ((1UL << len) - 1)) << pos;
	*dst = cpu_to_le16(tmp);
}

/*
 * The bit-field definitions in iwl-xxxx-hw.h are in the form of:
 *
 * struct example {
 *         __le32 val1;
 * #define IWL_name_POS 8
 * #define IWL_name_LEN 4
 * #define IWL_name_SYM val1
 * };
 *
 * The IWL_SET_BITS and IWL_GET_BITS macros are provided to allow the driver
 * to call:
 *
 * struct example bar;
 * u32 val = IWL_GET_BITS(bar, name);
 * val = val * 2;
 * IWL_SET_BITS(bar, name, val);
 *
 * All cpu / host ordering, masking, and shifts are performed by the macros
 * and iwl_{get,set}_bits.
 *
 */
#define IWL_SET_BITS(s, sym, v) \
	iwl_set_bits(&(s).IWL_ ## sym ## _SYM, IWL_ ## sym ## _POS, \
		     IWL_ ## sym ## _LEN, (v))

#define IWL_SET_BITS16(s, sym, v) \
	iwl_set_bits16(&(s).IWL_ ## sym ## _SYM, IWL_ ## sym ## _POS, \
		       IWL_ ## sym ## _LEN, (v))

#define IWL_GET_BITS(s, sym) \
	iwl_get_bits((s).IWL_ ## sym ## _SYM, IWL_ ## sym ## _POS, \
		      IWL_ ## sym ## _LEN)


#define KELVIN_TO_CELSIUS(x) ((x)-273)
#define CELSIUS_TO_KELVIN(x) ((x)+273)

#define IEEE80211_CHAN_W_RADAR_DETECT 0x00000010

static inline struct ieee80211_conf *ieee80211_get_hw_conf(
	struct ieee80211_hw *hw)
{
	return &hw->conf;
}

#define QOS_CONTROL_LEN 2

#define IEEE80211_STYPE_BACK_REQ	0x0080
#define IEEE80211_STYPE_BACK		0x0090


static inline int ieee80211_is_management(u16 fc)
{
	return (fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT;
}

static inline int ieee80211_is_control(u16 fc)
{
	return (fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL;
}

static inline int ieee80211_is_data(u16 fc)
{
	return (fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA;
}

static inline int ieee80211_is_back_request(u16 fc)
{
	return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL) &&
	       ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BACK_REQ);
}

static inline int ieee80211_is_probe_response(u16 fc)
{
	return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
	       ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP);
}

static inline int ieee80211_is_probe_request(u16 fc)
{
	return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
	       ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_REQ);
}

static inline int ieee80211_is_beacon(u16 fc)
{
	return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
	       ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON);
}

static inline int ieee80211_is_atim(u16 fc)
{
	return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
	       ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ATIM);
}

static inline int ieee80211_is_assoc_request(u16 fc)
{
	return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
	       ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ);
}

static inline int ieee80211_is_assoc_response(u16 fc)
{
	return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
	       ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_RESP);
}

static inline int ieee80211_is_auth(u16 fc)
{
	return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
	       ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ);
}

static inline int ieee80211_is_deauth(u16 fc)
{
	return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
	       ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ);
}

static inline int ieee80211_is_disassoc(u16 fc)
{
	return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
	       ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ);
}

static inline int ieee80211_is_reassoc_request(u16 fc)
{
	return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
	       ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ);
}

static inline int ieee80211_is_reassoc_response(u16 fc)
{
	return ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) &&
	       ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_RESP);
}

static inline int iwl_check_bits(unsigned long field, unsigned long mask)
{
	return ((field & mask) == mask) ? 1 : 0;
}

static inline unsigned long elapsed_jiffies(unsigned long start,
					    unsigned long end)
{
	if (end > start)
		return end - start;

	return end + (MAX_JIFFY_OFFSET - start);
}

#endif				/* __iwl_helpers_h__ */