[PATCH] ipw2200: remove the MAC timestamp present field from radiotap head
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / net / wireless / ipw2200.c
CommitLineData
43f66a6c 1/******************************************************************************
bf79451e 2
171e7b2f 3 Copyright(c) 2003 - 2006 Intel Corporation. All rights reserved.
43f66a6c
JK
4
5 802.11 status code portion of this file from ethereal-0.10.6:
6 Copyright 2000, Axis Communications AB
7 Ethereal - Network traffic analyzer
8 By Gerald Combs <gerald@ethereal.com>
9 Copyright 1998 Gerald Combs
10
bf79451e
JG
11 This program is free software; you can redistribute it and/or modify it
12 under the terms of version 2 of the GNU General Public License as
43f66a6c 13 published by the Free Software Foundation.
bf79451e
JG
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
43f66a6c 18 more details.
bf79451e 19
43f66a6c 20 You should have received a copy of the GNU General Public License along with
bf79451e 21 this program; if not, write to the Free Software Foundation, Inc., 59
43f66a6c 22 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
bf79451e 23
43f66a6c
JK
24 The full GNU General Public License is included in this distribution in the
25 file called LICENSE.
bf79451e 26
43f66a6c
JK
27 Contact Information:
28 James P. Ketrenos <ipw2100-admin@linux.intel.com>
29 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30
31******************************************************************************/
32
33#include "ipw2200.h"
733482e4 34#include <linux/version.h>
43f66a6c 35
ae4af61f
ZY
36
37#ifndef KBUILD_EXTMOD
38#define VK "k"
39#else
40#define VK
41#endif
42
43#ifdef CONFIG_IPW2200_DEBUG
44#define VD "d"
45#else
46#define VD
47#endif
48
49#ifdef CONFIG_IPW2200_MONITOR
50#define VM "m"
51#else
52#define VM
53#endif
54
55#ifdef CONFIG_IPW2200_PROMISCUOUS
56#define VP "p"
57#else
58#define VP
59#endif
60
459d4087 61#ifdef CONFIG_IPW2200_RADIOTAP
ae4af61f
ZY
62#define VR "r"
63#else
64#define VR
65#endif
66
67#ifdef CONFIG_IPW2200_QOS
68#define VQ "q"
69#else
70#define VQ
71#endif
72
555fd918 73#define IPW2200_VERSION "1.1.2" VK VD VM VP VR VQ
43f66a6c 74#define DRV_DESCRIPTION "Intel(R) PRO/Wireless 2200/2915 Network Driver"
171e7b2f 75#define DRV_COPYRIGHT "Copyright(c) 2003-2006 Intel Corporation"
43f66a6c
JK
76#define DRV_VERSION IPW2200_VERSION
77
b095c381
JK
78#define ETH_P_80211_STATS (ETH_P_80211_RAW + 1)
79
43f66a6c
JK
80MODULE_DESCRIPTION(DRV_DESCRIPTION);
81MODULE_VERSION(DRV_VERSION);
82MODULE_AUTHOR(DRV_COPYRIGHT);
83MODULE_LICENSE("GPL");
84
f6c5cb7c 85static int cmdlog = 0;
5dc81c30 86#ifdef CONFIG_IPW2200_DEBUG
43f66a6c 87static int debug = 0;
5dc81c30 88#endif
43f66a6c 89static int channel = 0;
43f66a6c
JK
90static int mode = 0;
91
92static u32 ipw_debug_level;
93static int associate = 1;
94static int auto_create = 1;
a613bffd 95static int led = 0;
43f66a6c 96static int disable = 0;
810dabd4 97static int bt_coexist = 0;
bde37d03 98static int hwcrypto = 0;
4bfdb91d 99static int roaming = 1;
43f66a6c
JK
100static const char ipw_modes[] = {
101 'a', 'b', 'g', '?'
102};
d2b83e12 103static int antenna = CFG_SYS_ANTENNA_BOTH;
43f66a6c 104
d685b8c2
ZY
105#ifdef CONFIG_IPW2200_PROMISCUOUS
106static int rtap_iface = 0; /* def: 0 -- do not create rtap interface */
107#endif
108
109
e43e3c1e 110#ifdef CONFIG_IPW2200_QOS
b095c381
JK
111static int qos_enable = 0;
112static int qos_burst_enable = 0;
113static int qos_no_ack_mask = 0;
114static int burst_duration_CCK = 0;
115static int burst_duration_OFDM = 0;
116
117static struct ieee80211_qos_parameters def_qos_parameters_OFDM = {
118 {QOS_TX0_CW_MIN_OFDM, QOS_TX1_CW_MIN_OFDM, QOS_TX2_CW_MIN_OFDM,
119 QOS_TX3_CW_MIN_OFDM},
120 {QOS_TX0_CW_MAX_OFDM, QOS_TX1_CW_MAX_OFDM, QOS_TX2_CW_MAX_OFDM,
121 QOS_TX3_CW_MAX_OFDM},
122 {QOS_TX0_AIFS, QOS_TX1_AIFS, QOS_TX2_AIFS, QOS_TX3_AIFS},
123 {QOS_TX0_ACM, QOS_TX1_ACM, QOS_TX2_ACM, QOS_TX3_ACM},
124 {QOS_TX0_TXOP_LIMIT_OFDM, QOS_TX1_TXOP_LIMIT_OFDM,
125 QOS_TX2_TXOP_LIMIT_OFDM, QOS_TX3_TXOP_LIMIT_OFDM}
126};
127
128static struct ieee80211_qos_parameters def_qos_parameters_CCK = {
129 {QOS_TX0_CW_MIN_CCK, QOS_TX1_CW_MIN_CCK, QOS_TX2_CW_MIN_CCK,
130 QOS_TX3_CW_MIN_CCK},
131 {QOS_TX0_CW_MAX_CCK, QOS_TX1_CW_MAX_CCK, QOS_TX2_CW_MAX_CCK,
132 QOS_TX3_CW_MAX_CCK},
133 {QOS_TX0_AIFS, QOS_TX1_AIFS, QOS_TX2_AIFS, QOS_TX3_AIFS},
134 {QOS_TX0_ACM, QOS_TX1_ACM, QOS_TX2_ACM, QOS_TX3_ACM},
135 {QOS_TX0_TXOP_LIMIT_CCK, QOS_TX1_TXOP_LIMIT_CCK, QOS_TX2_TXOP_LIMIT_CCK,
136 QOS_TX3_TXOP_LIMIT_CCK}
137};
138
139static struct ieee80211_qos_parameters def_parameters_OFDM = {
140 {DEF_TX0_CW_MIN_OFDM, DEF_TX1_CW_MIN_OFDM, DEF_TX2_CW_MIN_OFDM,
141 DEF_TX3_CW_MIN_OFDM},
142 {DEF_TX0_CW_MAX_OFDM, DEF_TX1_CW_MAX_OFDM, DEF_TX2_CW_MAX_OFDM,
143 DEF_TX3_CW_MAX_OFDM},
144 {DEF_TX0_AIFS, DEF_TX1_AIFS, DEF_TX2_AIFS, DEF_TX3_AIFS},
145 {DEF_TX0_ACM, DEF_TX1_ACM, DEF_TX2_ACM, DEF_TX3_ACM},
146 {DEF_TX0_TXOP_LIMIT_OFDM, DEF_TX1_TXOP_LIMIT_OFDM,
147 DEF_TX2_TXOP_LIMIT_OFDM, DEF_TX3_TXOP_LIMIT_OFDM}
148};
149
150static struct ieee80211_qos_parameters def_parameters_CCK = {
151 {DEF_TX0_CW_MIN_CCK, DEF_TX1_CW_MIN_CCK, DEF_TX2_CW_MIN_CCK,
152 DEF_TX3_CW_MIN_CCK},
153 {DEF_TX0_CW_MAX_CCK, DEF_TX1_CW_MAX_CCK, DEF_TX2_CW_MAX_CCK,
154 DEF_TX3_CW_MAX_CCK},
155 {DEF_TX0_AIFS, DEF_TX1_AIFS, DEF_TX2_AIFS, DEF_TX3_AIFS},
156 {DEF_TX0_ACM, DEF_TX1_ACM, DEF_TX2_ACM, DEF_TX3_ACM},
157 {DEF_TX0_TXOP_LIMIT_CCK, DEF_TX1_TXOP_LIMIT_CCK, DEF_TX2_TXOP_LIMIT_CCK,
158 DEF_TX3_TXOP_LIMIT_CCK}
159};
160
161static u8 qos_oui[QOS_OUI_LEN] = { 0x00, 0x50, 0xF2 };
162
163static int from_priority_to_tx_queue[] = {
164 IPW_TX_QUEUE_1, IPW_TX_QUEUE_2, IPW_TX_QUEUE_2, IPW_TX_QUEUE_1,
165 IPW_TX_QUEUE_3, IPW_TX_QUEUE_3, IPW_TX_QUEUE_4, IPW_TX_QUEUE_4
166};
167
168static u32 ipw_qos_get_burst_duration(struct ipw_priv *priv);
169
170static int ipw_send_qos_params_command(struct ipw_priv *priv, struct ieee80211_qos_parameters
171 *qos_param);
172static int ipw_send_qos_info_command(struct ipw_priv *priv, struct ieee80211_qos_information_element
173 *qos_param);
e43e3c1e 174#endif /* CONFIG_IPW2200_QOS */
b095c381 175
97a78ca9 176static struct iw_statistics *ipw_get_wireless_stats(struct net_device *dev);
b095c381 177static void ipw_remove_current_network(struct ipw_priv *priv);
43f66a6c 178static void ipw_rx(struct ipw_priv *priv);
bf79451e 179static int ipw_queue_tx_reclaim(struct ipw_priv *priv,
43f66a6c
JK
180 struct clx2_tx_queue *txq, int qindex);
181static int ipw_queue_reset(struct ipw_priv *priv);
182
183static int ipw_queue_tx_hcmd(struct ipw_priv *priv, int hcmd, void *buf,
184 int len, int sync);
185
186static void ipw_tx_queue_free(struct ipw_priv *);
187
188static struct ipw_rx_queue *ipw_rx_queue_alloc(struct ipw_priv *);
189static void ipw_rx_queue_free(struct ipw_priv *, struct ipw_rx_queue *);
190static void ipw_rx_queue_replenish(void *);
43f66a6c 191static int ipw_up(struct ipw_priv *);
c848d0af 192static void ipw_bg_up(void *);
43f66a6c 193static void ipw_down(struct ipw_priv *);
c848d0af 194static void ipw_bg_down(void *);
43f66a6c 195static int ipw_config(struct ipw_priv *);
0edd5b44
JG
196static int init_supported_rates(struct ipw_priv *priv,
197 struct ipw_supported_rates *prates);
b095c381
JK
198static void ipw_set_hwcrypto_keys(struct ipw_priv *);
199static void ipw_send_wep_keys(struct ipw_priv *, int);
43f66a6c 200
f6c5cb7c
JK
201static int snprint_line(char *buf, size_t count,
202 const u8 * data, u32 len, u32 ofs)
43f66a6c
JK
203{
204 int out, i, j, l;
205 char c;
bf79451e 206
43f66a6c
JK
207 out = snprintf(buf, count, "%08X", ofs);
208
209 for (l = 0, i = 0; i < 2; i++) {
210 out += snprintf(buf + out, count - out, " ");
bf79451e
JG
211 for (j = 0; j < 8 && l < len; j++, l++)
212 out += snprintf(buf + out, count - out, "%02X ",
43f66a6c
JK
213 data[(i * 8 + j)]);
214 for (; j < 8; j++)
215 out += snprintf(buf + out, count - out, " ");
216 }
bf79451e 217
43f66a6c
JK
218 out += snprintf(buf + out, count - out, " ");
219 for (l = 0, i = 0; i < 2; i++) {
220 out += snprintf(buf + out, count - out, " ");
221 for (j = 0; j < 8 && l < len; j++, l++) {
222 c = data[(i * 8 + j)];
223 if (!isascii(c) || !isprint(c))
224 c = '.';
bf79451e 225
43f66a6c
JK
226 out += snprintf(buf + out, count - out, "%c", c);
227 }
228
229 for (; j < 8; j++)
230 out += snprintf(buf + out, count - out, " ");
231 }
bf79451e 232
f6c5cb7c 233 return out;
43f66a6c
JK
234}
235
0edd5b44 236static void printk_buf(int level, const u8 * data, u32 len)
43f66a6c
JK
237{
238 char line[81];
239 u32 ofs = 0;
240 if (!(ipw_debug_level & level))
241 return;
242
243 while (len) {
f6c5cb7c
JK
244 snprint_line(line, sizeof(line), &data[ofs],
245 min(len, 16U), ofs);
246 printk(KERN_DEBUG "%s\n", line);
43f66a6c
JK
247 ofs += 16;
248 len -= min(len, 16U);
249 }
250}
251
f6c5cb7c
JK
252static int snprintk_buf(u8 * output, size_t size, const u8 * data, size_t len)
253{
254 size_t out = size;
255 u32 ofs = 0;
256 int total = 0;
257
258 while (size && len) {
259 out = snprint_line(output, size, &data[ofs],
260 min_t(size_t, len, 16U), ofs);
261
262 ofs += 16;
263 output += out;
264 size -= out;
265 len -= min_t(size_t, len, 16U);
266 total += out;
267 }
268 return total;
269}
270
c8fe6679 271/* alias for 32-bit indirect read (for SRAM/reg above 4K), with debug wrapper */
43f66a6c
JK
272static u32 _ipw_read_reg32(struct ipw_priv *priv, u32 reg);
273#define ipw_read_reg32(a, b) _ipw_read_reg32(a, b)
274
c8fe6679 275/* alias for 8-bit indirect read (for SRAM/reg above 4K), with debug wrapper */
43f66a6c
JK
276static u8 _ipw_read_reg8(struct ipw_priv *ipw, u32 reg);
277#define ipw_read_reg8(a, b) _ipw_read_reg8(a, b)
278
c8fe6679 279/* 8-bit indirect write (for SRAM/reg above 4K), with debug wrapper */
43f66a6c
JK
280static void _ipw_write_reg8(struct ipw_priv *priv, u32 reg, u8 value);
281static inline void ipw_write_reg8(struct ipw_priv *a, u32 b, u8 c)
282{
0edd5b44
JG
283 IPW_DEBUG_IO("%s %d: write_indirect8(0x%08X, 0x%08X)\n", __FILE__,
284 __LINE__, (u32) (b), (u32) (c));
43f66a6c
JK
285 _ipw_write_reg8(a, b, c);
286}
287
c8fe6679 288/* 16-bit indirect write (for SRAM/reg above 4K), with debug wrapper */
43f66a6c
JK
289static void _ipw_write_reg16(struct ipw_priv *priv, u32 reg, u16 value);
290static inline void ipw_write_reg16(struct ipw_priv *a, u32 b, u16 c)
291{
0edd5b44
JG
292 IPW_DEBUG_IO("%s %d: write_indirect16(0x%08X, 0x%08X)\n", __FILE__,
293 __LINE__, (u32) (b), (u32) (c));
43f66a6c
JK
294 _ipw_write_reg16(a, b, c);
295}
296
c8fe6679 297/* 32-bit indirect write (for SRAM/reg above 4K), with debug wrapper */
43f66a6c
JK
298static void _ipw_write_reg32(struct ipw_priv *priv, u32 reg, u32 value);
299static inline void ipw_write_reg32(struct ipw_priv *a, u32 b, u32 c)
300{
0edd5b44
JG
301 IPW_DEBUG_IO("%s %d: write_indirect32(0x%08X, 0x%08X)\n", __FILE__,
302 __LINE__, (u32) (b), (u32) (c));
43f66a6c
JK
303 _ipw_write_reg32(a, b, c);
304}
305
c8fe6679 306/* 8-bit direct write (low 4K) */
43f66a6c 307#define _ipw_write8(ipw, ofs, val) writeb((val), (ipw)->hw_base + (ofs))
c8fe6679
ZY
308
309/* 8-bit direct write (for low 4K of SRAM/regs), with debug wrapper */
43f66a6c
JK
310#define ipw_write8(ipw, ofs, val) \
311 IPW_DEBUG_IO("%s %d: write_direct8(0x%08X, 0x%08X)\n", __FILE__, __LINE__, (u32)(ofs), (u32)(val)); \
312 _ipw_write8(ipw, ofs, val)
313
c8fe6679 314/* 16-bit direct write (low 4K) */
43f66a6c 315#define _ipw_write16(ipw, ofs, val) writew((val), (ipw)->hw_base + (ofs))
c8fe6679
ZY
316
317/* 16-bit direct write (for low 4K of SRAM/regs), with debug wrapper */
43f66a6c
JK
318#define ipw_write16(ipw, ofs, val) \
319 IPW_DEBUG_IO("%s %d: write_direct16(0x%08X, 0x%08X)\n", __FILE__, __LINE__, (u32)(ofs), (u32)(val)); \
320 _ipw_write16(ipw, ofs, val)
321
c8fe6679 322/* 32-bit direct write (low 4K) */
43f66a6c 323#define _ipw_write32(ipw, ofs, val) writel((val), (ipw)->hw_base + (ofs))
c8fe6679
ZY
324
325/* 32-bit direct write (for low 4K of SRAM/regs), with debug wrapper */
43f66a6c
JK
326#define ipw_write32(ipw, ofs, val) \
327 IPW_DEBUG_IO("%s %d: write_direct32(0x%08X, 0x%08X)\n", __FILE__, __LINE__, (u32)(ofs), (u32)(val)); \
328 _ipw_write32(ipw, ofs, val)
329
c8fe6679 330/* 8-bit direct read (low 4K) */
43f66a6c 331#define _ipw_read8(ipw, ofs) readb((ipw)->hw_base + (ofs))
c8fe6679
ZY
332
333/* 8-bit direct read (low 4K), with debug wrapper */
0edd5b44
JG
334static inline u8 __ipw_read8(char *f, u32 l, struct ipw_priv *ipw, u32 ofs)
335{
336 IPW_DEBUG_IO("%s %d: read_direct8(0x%08X)\n", f, l, (u32) (ofs));
43f66a6c
JK
337 return _ipw_read8(ipw, ofs);
338}
0edd5b44 339
c8fe6679 340/* alias to 8-bit direct read (low 4K of SRAM/regs), with debug wrapper */
43f66a6c
JK
341#define ipw_read8(ipw, ofs) __ipw_read8(__FILE__, __LINE__, ipw, ofs)
342
c8fe6679 343/* 16-bit direct read (low 4K) */
43f66a6c 344#define _ipw_read16(ipw, ofs) readw((ipw)->hw_base + (ofs))
c8fe6679
ZY
345
346/* 16-bit direct read (low 4K), with debug wrapper */
0edd5b44
JG
347static inline u16 __ipw_read16(char *f, u32 l, struct ipw_priv *ipw, u32 ofs)
348{
349 IPW_DEBUG_IO("%s %d: read_direct16(0x%08X)\n", f, l, (u32) (ofs));
43f66a6c
JK
350 return _ipw_read16(ipw, ofs);
351}
0edd5b44 352
c8fe6679 353/* alias to 16-bit direct read (low 4K of SRAM/regs), with debug wrapper */
43f66a6c
JK
354#define ipw_read16(ipw, ofs) __ipw_read16(__FILE__, __LINE__, ipw, ofs)
355
c8fe6679 356/* 32-bit direct read (low 4K) */
43f66a6c 357#define _ipw_read32(ipw, ofs) readl((ipw)->hw_base + (ofs))
c8fe6679
ZY
358
359/* 32-bit direct read (low 4K), with debug wrapper */
0edd5b44
JG
360static inline u32 __ipw_read32(char *f, u32 l, struct ipw_priv *ipw, u32 ofs)
361{
362 IPW_DEBUG_IO("%s %d: read_direct32(0x%08X)\n", f, l, (u32) (ofs));
43f66a6c
JK
363 return _ipw_read32(ipw, ofs);
364}
0edd5b44 365
c8fe6679 366/* alias to 32-bit direct read (low 4K of SRAM/regs), with debug wrapper */
43f66a6c
JK
367#define ipw_read32(ipw, ofs) __ipw_read32(__FILE__, __LINE__, ipw, ofs)
368
c8fe6679 369/* multi-byte read (above 4K), with debug wrapper */
43f66a6c 370static void _ipw_read_indirect(struct ipw_priv *, u32, u8 *, int);
f6c5cb7c
JK
371static inline void __ipw_read_indirect(const char *f, int l,
372 struct ipw_priv *a, u32 b, u8 * c, int d)
373{
374 IPW_DEBUG_IO("%s %d: read_indirect(0x%08X) %d bytes\n", f, l, (u32) (b),
375 d);
376 _ipw_read_indirect(a, b, c, d);
377}
378
c8fe6679 379/* alias to multi-byte read (SRAM/regs above 4K), with debug wrapper */
f6c5cb7c 380#define ipw_read_indirect(a, b, c, d) __ipw_read_indirect(__FILE__, __LINE__, a, b, c, d)
43f66a6c 381
c8fe6679 382/* alias to multi-byte read (SRAM/regs above 4K), with debug wrapper */
0edd5b44
JG
383static void _ipw_write_indirect(struct ipw_priv *priv, u32 addr, u8 * data,
384 int num);
43f66a6c
JK
385#define ipw_write_indirect(a, b, c, d) \
386 IPW_DEBUG_IO("%s %d: write_indirect(0x%08X) %d bytes\n", __FILE__, __LINE__, (u32)(b), d); \
afbf30a2 387 _ipw_write_indirect(a, b, c, d)
43f66a6c 388
c8fe6679 389/* 32-bit indirect write (above 4K) */
0edd5b44 390static void _ipw_write_reg32(struct ipw_priv *priv, u32 reg, u32 value)
43f66a6c 391{
0edd5b44 392 IPW_DEBUG_IO(" %p : reg = 0x%8X : value = 0x%8X\n", priv, reg, value);
b095c381
JK
393 _ipw_write32(priv, IPW_INDIRECT_ADDR, reg);
394 _ipw_write32(priv, IPW_INDIRECT_DATA, value);
43f66a6c
JK
395}
396
c8fe6679 397/* 8-bit indirect write (above 4K) */
43f66a6c
JK
398static void _ipw_write_reg8(struct ipw_priv *priv, u32 reg, u8 value)
399{
2638bc39 400 u32 aligned_addr = reg & IPW_INDIRECT_ADDR_MASK; /* dword align */
c8fe6679
ZY
401 u32 dif_len = reg - aligned_addr;
402
43f66a6c 403 IPW_DEBUG_IO(" reg = 0x%8X : value = 0x%8X\n", reg, value);
c8fe6679
ZY
404 _ipw_write32(priv, IPW_INDIRECT_ADDR, aligned_addr);
405 _ipw_write8(priv, IPW_INDIRECT_DATA + dif_len, value);
43f66a6c
JK
406}
407
c8fe6679 408/* 16-bit indirect write (above 4K) */
0edd5b44 409static void _ipw_write_reg16(struct ipw_priv *priv, u32 reg, u16 value)
43f66a6c 410{
2638bc39 411 u32 aligned_addr = reg & IPW_INDIRECT_ADDR_MASK; /* dword align */
c8fe6679
ZY
412 u32 dif_len = (reg - aligned_addr) & (~0x1ul);
413
43f66a6c 414 IPW_DEBUG_IO(" reg = 0x%8X : value = 0x%8X\n", reg, value);
c8fe6679
ZY
415 _ipw_write32(priv, IPW_INDIRECT_ADDR, aligned_addr);
416 _ipw_write16(priv, IPW_INDIRECT_DATA + dif_len, value);
43f66a6c
JK
417}
418
c8fe6679 419/* 8-bit indirect read (above 4K) */
43f66a6c
JK
420static u8 _ipw_read_reg8(struct ipw_priv *priv, u32 reg)
421{
422 u32 word;
b095c381 423 _ipw_write32(priv, IPW_INDIRECT_ADDR, reg & IPW_INDIRECT_ADDR_MASK);
43f66a6c 424 IPW_DEBUG_IO(" reg = 0x%8X : \n", reg);
b095c381 425 word = _ipw_read32(priv, IPW_INDIRECT_DATA);
0edd5b44 426 return (word >> ((reg & 0x3) * 8)) & 0xff;
43f66a6c
JK
427}
428
c8fe6679 429/* 32-bit indirect read (above 4K) */
43f66a6c
JK
430static u32 _ipw_read_reg32(struct ipw_priv *priv, u32 reg)
431{
432 u32 value;
433
434 IPW_DEBUG_IO("%p : reg = 0x%08x\n", priv, reg);
435
b095c381
JK
436 _ipw_write32(priv, IPW_INDIRECT_ADDR, reg);
437 value = _ipw_read32(priv, IPW_INDIRECT_DATA);
43f66a6c
JK
438 IPW_DEBUG_IO(" reg = 0x%4X : value = 0x%4x \n", reg, value);
439 return value;
440}
441
c8fe6679
ZY
442/* General purpose, no alignment requirement, iterative (multi-byte) read, */
443/* for area above 1st 4K of SRAM/reg space */
43f66a6c
JK
444static void _ipw_read_indirect(struct ipw_priv *priv, u32 addr, u8 * buf,
445 int num)
446{
2638bc39 447 u32 aligned_addr = addr & IPW_INDIRECT_ADDR_MASK; /* dword align */
43f66a6c 448 u32 dif_len = addr - aligned_addr;
43f66a6c 449 u32 i;
bf79451e 450
43f66a6c
JK
451 IPW_DEBUG_IO("addr = %i, buf = %p, num = %i\n", addr, buf, num);
452
ea2b26e0
JK
453 if (num <= 0) {
454 return;
455 }
456
c8fe6679 457 /* Read the first dword (or portion) byte by byte */
43f66a6c 458 if (unlikely(dif_len)) {
b095c381 459 _ipw_write32(priv, IPW_INDIRECT_ADDR, aligned_addr);
43f66a6c 460 /* Start reading at aligned_addr + dif_len */
ea2b26e0 461 for (i = dif_len; ((i < 4) && (num > 0)); i++, num--)
b095c381 462 *buf++ = _ipw_read8(priv, IPW_INDIRECT_DATA + i);
43f66a6c
JK
463 aligned_addr += 4;
464 }
465
c8fe6679 466 /* Read all of the middle dwords as dwords, with auto-increment */
b095c381 467 _ipw_write32(priv, IPW_AUTOINC_ADDR, aligned_addr);
ea2b26e0 468 for (; num >= 4; buf += 4, aligned_addr += 4, num -= 4)
b095c381 469 *(u32 *) buf = _ipw_read32(priv, IPW_AUTOINC_DATA);
bf79451e 470
c8fe6679 471 /* Read the last dword (or portion) byte by byte */
ea2b26e0 472 if (unlikely(num)) {
b095c381 473 _ipw_write32(priv, IPW_INDIRECT_ADDR, aligned_addr);
ea2b26e0 474 for (i = 0; num > 0; i++, num--)
b095c381 475 *buf++ = ipw_read8(priv, IPW_INDIRECT_DATA + i);
ea2b26e0 476 }
43f66a6c
JK
477}
478
c8fe6679
ZY
479/* General purpose, no alignment requirement, iterative (multi-byte) write, */
480/* for area above 1st 4K of SRAM/reg space */
0edd5b44 481static void _ipw_write_indirect(struct ipw_priv *priv, u32 addr, u8 * buf,
43f66a6c
JK
482 int num)
483{
2638bc39 484 u32 aligned_addr = addr & IPW_INDIRECT_ADDR_MASK; /* dword align */
43f66a6c 485 u32 dif_len = addr - aligned_addr;
43f66a6c 486 u32 i;
bf79451e 487
43f66a6c 488 IPW_DEBUG_IO("addr = %i, buf = %p, num = %i\n", addr, buf, num);
bf79451e 489
ea2b26e0
JK
490 if (num <= 0) {
491 return;
492 }
493
c8fe6679 494 /* Write the first dword (or portion) byte by byte */
43f66a6c 495 if (unlikely(dif_len)) {
b095c381 496 _ipw_write32(priv, IPW_INDIRECT_ADDR, aligned_addr);
c8fe6679 497 /* Start writing at aligned_addr + dif_len */
ea2b26e0 498 for (i = dif_len; ((i < 4) && (num > 0)); i++, num--, buf++)
b095c381 499 _ipw_write8(priv, IPW_INDIRECT_DATA + i, *buf);
43f66a6c
JK
500 aligned_addr += 4;
501 }
bf79451e 502
c8fe6679 503 /* Write all of the middle dwords as dwords, with auto-increment */
b095c381 504 _ipw_write32(priv, IPW_AUTOINC_ADDR, aligned_addr);
ea2b26e0 505 for (; num >= 4; buf += 4, aligned_addr += 4, num -= 4)
b095c381 506 _ipw_write32(priv, IPW_AUTOINC_DATA, *(u32 *) buf);
bf79451e 507
c8fe6679 508 /* Write the last dword (or portion) byte by byte */
ea2b26e0 509 if (unlikely(num)) {
b095c381 510 _ipw_write32(priv, IPW_INDIRECT_ADDR, aligned_addr);
ea2b26e0 511 for (i = 0; num > 0; i++, num--, buf++)
b095c381 512 _ipw_write8(priv, IPW_INDIRECT_DATA + i, *buf);
ea2b26e0 513 }
43f66a6c
JK
514}
515
c8fe6679
ZY
516/* General purpose, no alignment requirement, iterative (multi-byte) write, */
517/* for 1st 4K of SRAM/regs space */
bf79451e 518static void ipw_write_direct(struct ipw_priv *priv, u32 addr, void *buf,
43f66a6c
JK
519 int num)
520{
521 memcpy_toio((priv->hw_base + addr), buf, num);
522}
523
c8fe6679 524/* Set bit(s) in low 4K of SRAM/regs */
43f66a6c
JK
525static inline void ipw_set_bit(struct ipw_priv *priv, u32 reg, u32 mask)
526{
527 ipw_write32(priv, reg, ipw_read32(priv, reg) | mask);
528}
529
c8fe6679 530/* Clear bit(s) in low 4K of SRAM/regs */
43f66a6c
JK
531static inline void ipw_clear_bit(struct ipw_priv *priv, u32 reg, u32 mask)
532{
533 ipw_write32(priv, reg, ipw_read32(priv, reg) & ~mask);
534}
535
89c318ed 536static inline void __ipw_enable_interrupts(struct ipw_priv *priv)
43f66a6c
JK
537{
538 if (priv->status & STATUS_INT_ENABLED)
539 return;
540 priv->status |= STATUS_INT_ENABLED;
b095c381 541 ipw_write32(priv, IPW_INTA_MASK_R, IPW_INTA_MASK_ALL);
43f66a6c
JK
542}
543
89c318ed 544static inline void __ipw_disable_interrupts(struct ipw_priv *priv)
43f66a6c
JK
545{
546 if (!(priv->status & STATUS_INT_ENABLED))
547 return;
548 priv->status &= ~STATUS_INT_ENABLED;
b095c381 549 ipw_write32(priv, IPW_INTA_MASK_R, ~IPW_INTA_MASK_ALL);
43f66a6c
JK
550}
551
89c318ed
ZY
552static inline void ipw_enable_interrupts(struct ipw_priv *priv)
553{
554 unsigned long flags;
555
556 spin_lock_irqsave(&priv->irq_lock, flags);
557 __ipw_enable_interrupts(priv);
558 spin_unlock_irqrestore(&priv->irq_lock, flags);
559}
560
561static inline void ipw_disable_interrupts(struct ipw_priv *priv)
562{
563 unsigned long flags;
564
565 spin_lock_irqsave(&priv->irq_lock, flags);
566 __ipw_disable_interrupts(priv);
567 spin_unlock_irqrestore(&priv->irq_lock, flags);
568}
569
43f66a6c
JK
570static char *ipw_error_desc(u32 val)
571{
572 switch (val) {
bf79451e 573 case IPW_FW_ERROR_OK:
43f66a6c 574 return "ERROR_OK";
bf79451e 575 case IPW_FW_ERROR_FAIL:
43f66a6c 576 return "ERROR_FAIL";
bf79451e 577 case IPW_FW_ERROR_MEMORY_UNDERFLOW:
43f66a6c 578 return "MEMORY_UNDERFLOW";
bf79451e 579 case IPW_FW_ERROR_MEMORY_OVERFLOW:
43f66a6c 580 return "MEMORY_OVERFLOW";
bf79451e 581 case IPW_FW_ERROR_BAD_PARAM:
b095c381 582 return "BAD_PARAM";
bf79451e 583 case IPW_FW_ERROR_BAD_CHECKSUM:
b095c381 584 return "BAD_CHECKSUM";
bf79451e 585 case IPW_FW_ERROR_NMI_INTERRUPT:
b095c381 586 return "NMI_INTERRUPT";
bf79451e 587 case IPW_FW_ERROR_BAD_DATABASE:
b095c381 588 return "BAD_DATABASE";
bf79451e 589 case IPW_FW_ERROR_ALLOC_FAIL:
b095c381 590 return "ALLOC_FAIL";
bf79451e 591 case IPW_FW_ERROR_DMA_UNDERRUN:
b095c381 592 return "DMA_UNDERRUN";
bf79451e 593 case IPW_FW_ERROR_DMA_STATUS:
b095c381
JK
594 return "DMA_STATUS";
595 case IPW_FW_ERROR_DINO_ERROR:
596 return "DINO_ERROR";
597 case IPW_FW_ERROR_EEPROM_ERROR:
598 return "EEPROM_ERROR";
bf79451e 599 case IPW_FW_ERROR_SYSASSERT:
b095c381 600 return "SYSASSERT";
bf79451e 601 case IPW_FW_ERROR_FATAL_ERROR:
b095c381 602 return "FATAL_ERROR";
bf79451e 603 default:
b095c381 604 return "UNKNOWN_ERROR";
43f66a6c
JK
605 }
606}
607
b39860c6
JK
608static void ipw_dump_error_log(struct ipw_priv *priv,
609 struct ipw_fw_error *error)
43f66a6c 610{
b39860c6 611 u32 i;
bf79451e 612
b39860c6
JK
613 if (!error) {
614 IPW_ERROR("Error allocating and capturing error log. "
615 "Nothing to dump.\n");
616 return;
43f66a6c
JK
617 }
618
b39860c6
JK
619 IPW_ERROR("Start IPW Error Log Dump:\n");
620 IPW_ERROR("Status: 0x%08X, Config: %08X\n",
621 error->status, error->config);
43f66a6c 622
b39860c6 623 for (i = 0; i < error->elem_len; i++)
0edd5b44 624 IPW_ERROR("%s %i 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
b39860c6
JK
625 ipw_error_desc(error->elem[i].desc),
626 error->elem[i].time,
627 error->elem[i].blink1,
628 error->elem[i].blink2,
629 error->elem[i].link1,
630 error->elem[i].link2, error->elem[i].data);
631 for (i = 0; i < error->log_len; i++)
632 IPW_ERROR("%i\t0x%08x\t%i\n",
633 error->log[i].time,
286568ab 634 error->log[i].data, error->log[i].event);
43f66a6c
JK
635}
636
c848d0af 637static inline int ipw_is_init(struct ipw_priv *priv)
43f66a6c 638{
c848d0af 639 return (priv->status & STATUS_INIT) ? 1 : 0;
43f66a6c
JK
640}
641
0edd5b44 642static int ipw_get_ordinal(struct ipw_priv *priv, u32 ord, void *val, u32 * len)
43f66a6c
JK
643{
644 u32 addr, field_info, field_len, field_count, total_len;
645
646 IPW_DEBUG_ORD("ordinal = %i\n", ord);
647
648 if (!priv || !val || !len) {
649 IPW_DEBUG_ORD("Invalid argument\n");
650 return -EINVAL;
651 }
bf79451e 652
43f66a6c
JK
653 /* verify device ordinal tables have been initialized */
654 if (!priv->table0_addr || !priv->table1_addr || !priv->table2_addr) {
655 IPW_DEBUG_ORD("Access ordinals before initialization\n");
656 return -EINVAL;
657 }
658
659 switch (IPW_ORD_TABLE_ID_MASK & ord) {
660 case IPW_ORD_TABLE_0_MASK:
661 /*
662 * TABLE 0: Direct access to a table of 32 bit values
663 *
bf79451e 664 * This is a very simple table with the data directly
43f66a6c
JK
665 * read from the table
666 */
667
668 /* remove the table id from the ordinal */
669 ord &= IPW_ORD_TABLE_VALUE_MASK;
670
671 /* boundary check */
672 if (ord > priv->table0_len) {
673 IPW_DEBUG_ORD("ordinal value (%i) longer then "
674 "max (%i)\n", ord, priv->table0_len);
675 return -EINVAL;
676 }
677
678 /* verify we have enough room to store the value */
679 if (*len < sizeof(u32)) {
680 IPW_DEBUG_ORD("ordinal buffer length too small, "
aaa4d308 681 "need %zd\n", sizeof(u32));
43f66a6c
JK
682 return -EINVAL;
683 }
684
685 IPW_DEBUG_ORD("Reading TABLE0[%i] from offset 0x%08x\n",
0edd5b44 686 ord, priv->table0_addr + (ord << 2));
43f66a6c
JK
687
688 *len = sizeof(u32);
689 ord <<= 2;
0edd5b44 690 *((u32 *) val) = ipw_read32(priv, priv->table0_addr + ord);
43f66a6c
JK
691 break;
692
693 case IPW_ORD_TABLE_1_MASK:
694 /*
695 * TABLE 1: Indirect access to a table of 32 bit values
bf79451e
JG
696 *
697 * This is a fairly large table of u32 values each
43f66a6c
JK
698 * representing starting addr for the data (which is
699 * also a u32)
700 */
701
702 /* remove the table id from the ordinal */
703 ord &= IPW_ORD_TABLE_VALUE_MASK;
bf79451e 704
43f66a6c
JK
705 /* boundary check */
706 if (ord > priv->table1_len) {
707 IPW_DEBUG_ORD("ordinal value too long\n");
708 return -EINVAL;
709 }
710
711 /* verify we have enough room to store the value */
712 if (*len < sizeof(u32)) {
713 IPW_DEBUG_ORD("ordinal buffer length too small, "
aaa4d308 714 "need %zd\n", sizeof(u32));
43f66a6c
JK
715 return -EINVAL;
716 }
717
0edd5b44
JG
718 *((u32 *) val) =
719 ipw_read_reg32(priv, (priv->table1_addr + (ord << 2)));
43f66a6c
JK
720 *len = sizeof(u32);
721 break;
722
723 case IPW_ORD_TABLE_2_MASK:
724 /*
725 * TABLE 2: Indirect access to a table of variable sized values
726 *
727 * This table consist of six values, each containing
728 * - dword containing the starting offset of the data
729 * - dword containing the lengh in the first 16bits
730 * and the count in the second 16bits
731 */
732
733 /* remove the table id from the ordinal */
734 ord &= IPW_ORD_TABLE_VALUE_MASK;
735
736 /* boundary check */
737 if (ord > priv->table2_len) {
738 IPW_DEBUG_ORD("ordinal value too long\n");
739 return -EINVAL;
740 }
741
742 /* get the address of statistic */
743 addr = ipw_read_reg32(priv, priv->table2_addr + (ord << 3));
bf79451e
JG
744
745 /* get the second DW of statistics ;
43f66a6c 746 * two 16-bit words - first is length, second is count */
0edd5b44
JG
747 field_info =
748 ipw_read_reg32(priv,
749 priv->table2_addr + (ord << 3) +
750 sizeof(u32));
bf79451e 751
43f66a6c 752 /* get each entry length */
0edd5b44 753 field_len = *((u16 *) & field_info);
bf79451e 754
43f66a6c 755 /* get number of entries */
0edd5b44 756 field_count = *(((u16 *) & field_info) + 1);
bf79451e 757
43f66a6c
JK
758 /* abort if not enought memory */
759 total_len = field_len * field_count;
760 if (total_len > *len) {
761 *len = total_len;
762 return -EINVAL;
763 }
bf79451e 764
43f66a6c
JK
765 *len = total_len;
766 if (!total_len)
767 return 0;
768
769 IPW_DEBUG_ORD("addr = 0x%08x, total_len = %i, "
bf79451e 770 "field_info = 0x%08x\n",
43f66a6c
JK
771 addr, total_len, field_info);
772 ipw_read_indirect(priv, addr, val, total_len);
773 break;
774
775 default:
776 IPW_DEBUG_ORD("Invalid ordinal!\n");
777 return -EINVAL;
778
779 }
780
43f66a6c
JK
781 return 0;
782}
783
784static void ipw_init_ordinals(struct ipw_priv *priv)
785{
786 priv->table0_addr = IPW_ORDINALS_TABLE_LOWER;
bf79451e 787 priv->table0_len = ipw_read32(priv, priv->table0_addr);
43f66a6c
JK
788
789 IPW_DEBUG_ORD("table 0 offset at 0x%08x, len = %i\n",
790 priv->table0_addr, priv->table0_len);
791
792 priv->table1_addr = ipw_read32(priv, IPW_ORDINALS_TABLE_1);
793 priv->table1_len = ipw_read_reg32(priv, priv->table1_addr);
794
795 IPW_DEBUG_ORD("table 1 offset at 0x%08x, len = %i\n",
796 priv->table1_addr, priv->table1_len);
797
798 priv->table2_addr = ipw_read32(priv, IPW_ORDINALS_TABLE_2);
799 priv->table2_len = ipw_read_reg32(priv, priv->table2_addr);
0edd5b44 800 priv->table2_len &= 0x0000ffff; /* use first two bytes */
43f66a6c
JK
801
802 IPW_DEBUG_ORD("table 2 offset at 0x%08x, len = %i\n",
803 priv->table2_addr, priv->table2_len);
804
805}
806
a73e22b2 807static u32 ipw_register_toggle(u32 reg)
a613bffd 808{
b095c381
JK
809 reg &= ~IPW_START_STANDBY;
810 if (reg & IPW_GATE_ODMA)
811 reg &= ~IPW_GATE_ODMA;
812 if (reg & IPW_GATE_IDMA)
813 reg &= ~IPW_GATE_IDMA;
814 if (reg & IPW_GATE_ADMA)
815 reg &= ~IPW_GATE_ADMA;
a613bffd
JK
816 return reg;
817}
818
819/*
820 * LED behavior:
821 * - On radio ON, turn on any LEDs that require to be on during start
822 * - On initialization, start unassociated blink
823 * - On association, disable unassociated blink
824 * - On disassociation, start unassociated blink
825 * - On radio OFF, turn off any LEDs started during radio on
826 *
827 */
ede6111c
ZY
828#define LD_TIME_LINK_ON msecs_to_jiffies(300)
829#define LD_TIME_LINK_OFF msecs_to_jiffies(2700)
830#define LD_TIME_ACT_ON msecs_to_jiffies(250)
a613bffd 831
a73e22b2 832static void ipw_led_link_on(struct ipw_priv *priv)
a613bffd
JK
833{
834 unsigned long flags;
835 u32 led;
836
837 /* If configured to not use LEDs, or nic_type is 1,
838 * then we don't toggle a LINK led */
839 if (priv->config & CFG_NO_LED || priv->nic_type == EEPROM_NIC_TYPE_1)
840 return;
841
842 spin_lock_irqsave(&priv->lock, flags);
843
844 if (!(priv->status & STATUS_RF_KILL_MASK) &&
845 !(priv->status & STATUS_LED_LINK_ON)) {
846 IPW_DEBUG_LED("Link LED On\n");
b095c381 847 led = ipw_read_reg32(priv, IPW_EVENT_REG);
a613bffd
JK
848 led |= priv->led_association_on;
849
850 led = ipw_register_toggle(led);
851
852 IPW_DEBUG_LED("Reg: 0x%08X\n", led);
b095c381 853 ipw_write_reg32(priv, IPW_EVENT_REG, led);
a613bffd
JK
854
855 priv->status |= STATUS_LED_LINK_ON;
856
857 /* If we aren't associated, schedule turning the LED off */
858 if (!(priv->status & STATUS_ASSOCIATED))
859 queue_delayed_work(priv->workqueue,
860 &priv->led_link_off,
861 LD_TIME_LINK_ON);
862 }
863
864 spin_unlock_irqrestore(&priv->lock, flags);
865}
866
c848d0af
JK
867static void ipw_bg_led_link_on(void *data)
868{
869 struct ipw_priv *priv = data;
4644151b 870 mutex_lock(&priv->mutex);
c848d0af 871 ipw_led_link_on(data);
4644151b 872 mutex_unlock(&priv->mutex);
c848d0af
JK
873}
874
a73e22b2 875static void ipw_led_link_off(struct ipw_priv *priv)
a613bffd
JK
876{
877 unsigned long flags;
878 u32 led;
879
880 /* If configured not to use LEDs, or nic type is 1,
881 * then we don't goggle the LINK led. */
882 if (priv->config & CFG_NO_LED || priv->nic_type == EEPROM_NIC_TYPE_1)
883 return;
884
885 spin_lock_irqsave(&priv->lock, flags);
886
887 if (priv->status & STATUS_LED_LINK_ON) {
b095c381 888 led = ipw_read_reg32(priv, IPW_EVENT_REG);
a613bffd
JK
889 led &= priv->led_association_off;
890 led = ipw_register_toggle(led);
891
892 IPW_DEBUG_LED("Reg: 0x%08X\n", led);
b095c381 893 ipw_write_reg32(priv, IPW_EVENT_REG, led);
a613bffd
JK
894
895 IPW_DEBUG_LED("Link LED Off\n");
896
897 priv->status &= ~STATUS_LED_LINK_ON;
898
899 /* If we aren't associated and the radio is on, schedule
900 * turning the LED on (blink while unassociated) */
901 if (!(priv->status & STATUS_RF_KILL_MASK) &&
902 !(priv->status & STATUS_ASSOCIATED))
903 queue_delayed_work(priv->workqueue, &priv->led_link_on,
904 LD_TIME_LINK_OFF);
905
906 }
907
908 spin_unlock_irqrestore(&priv->lock, flags);
909}
910
c848d0af
JK
911static void ipw_bg_led_link_off(void *data)
912{
913 struct ipw_priv *priv = data;
4644151b 914 mutex_lock(&priv->mutex);
c848d0af 915 ipw_led_link_off(data);
4644151b 916 mutex_unlock(&priv->mutex);
c848d0af
JK
917}
918
858119e1 919static void __ipw_led_activity_on(struct ipw_priv *priv)
a613bffd 920{
a613bffd
JK
921 u32 led;
922
923 if (priv->config & CFG_NO_LED)
924 return;
925
b095c381 926 if (priv->status & STATUS_RF_KILL_MASK)
a613bffd 927 return;
a613bffd
JK
928
929 if (!(priv->status & STATUS_LED_ACT_ON)) {
b095c381 930 led = ipw_read_reg32(priv, IPW_EVENT_REG);
a613bffd
JK
931 led |= priv->led_activity_on;
932
933 led = ipw_register_toggle(led);
934
935 IPW_DEBUG_LED("Reg: 0x%08X\n", led);
b095c381 936 ipw_write_reg32(priv, IPW_EVENT_REG, led);
a613bffd
JK
937
938 IPW_DEBUG_LED("Activity LED On\n");
939
940 priv->status |= STATUS_LED_ACT_ON;
941
c848d0af 942 cancel_delayed_work(&priv->led_act_off);
a613bffd
JK
943 queue_delayed_work(priv->workqueue, &priv->led_act_off,
944 LD_TIME_ACT_ON);
945 } else {
946 /* Reschedule LED off for full time period */
947 cancel_delayed_work(&priv->led_act_off);
948 queue_delayed_work(priv->workqueue, &priv->led_act_off,
949 LD_TIME_ACT_ON);
950 }
b095c381 951}
a613bffd 952
a73e22b2 953#if 0
b095c381
JK
954void ipw_led_activity_on(struct ipw_priv *priv)
955{
956 unsigned long flags;
957 spin_lock_irqsave(&priv->lock, flags);
958 __ipw_led_activity_on(priv);
a613bffd
JK
959 spin_unlock_irqrestore(&priv->lock, flags);
960}
a73e22b2 961#endif /* 0 */
a613bffd 962
a73e22b2 963static void ipw_led_activity_off(struct ipw_priv *priv)
a613bffd
JK
964{
965 unsigned long flags;
966 u32 led;
967
968 if (priv->config & CFG_NO_LED)
969 return;
970
971 spin_lock_irqsave(&priv->lock, flags);
972
973 if (priv->status & STATUS_LED_ACT_ON) {
b095c381 974 led = ipw_read_reg32(priv, IPW_EVENT_REG);
a613bffd
JK
975 led &= priv->led_activity_off;
976
977 led = ipw_register_toggle(led);
978
979 IPW_DEBUG_LED("Reg: 0x%08X\n", led);
b095c381 980 ipw_write_reg32(priv, IPW_EVENT_REG, led);
a613bffd
JK
981
982 IPW_DEBUG_LED("Activity LED Off\n");
983
984 priv->status &= ~STATUS_LED_ACT_ON;
985 }
986
987 spin_unlock_irqrestore(&priv->lock, flags);
988}
989
c848d0af
JK
990static void ipw_bg_led_activity_off(void *data)
991{
992 struct ipw_priv *priv = data;
4644151b 993 mutex_lock(&priv->mutex);
c848d0af 994 ipw_led_activity_off(data);
4644151b 995 mutex_unlock(&priv->mutex);
c848d0af
JK
996}
997
a73e22b2 998static void ipw_led_band_on(struct ipw_priv *priv)
a613bffd
JK
999{
1000 unsigned long flags;
1001 u32 led;
1002
1003 /* Only nic type 1 supports mode LEDs */
c848d0af
JK
1004 if (priv->config & CFG_NO_LED ||
1005 priv->nic_type != EEPROM_NIC_TYPE_1 || !priv->assoc_network)
a613bffd
JK
1006 return;
1007
1008 spin_lock_irqsave(&priv->lock, flags);
1009
b095c381 1010 led = ipw_read_reg32(priv, IPW_EVENT_REG);
a613bffd
JK
1011 if (priv->assoc_network->mode == IEEE_A) {
1012 led |= priv->led_ofdm_on;
1013 led &= priv->led_association_off;
1014 IPW_DEBUG_LED("Mode LED On: 802.11a\n");
1015 } else if (priv->assoc_network->mode == IEEE_G) {
1016 led |= priv->led_ofdm_on;
1017 led |= priv->led_association_on;
1018 IPW_DEBUG_LED("Mode LED On: 802.11g\n");
1019 } else {
1020 led &= priv->led_ofdm_off;
1021 led |= priv->led_association_on;
1022 IPW_DEBUG_LED("Mode LED On: 802.11b\n");
1023 }
1024
1025 led = ipw_register_toggle(led);
1026
1027 IPW_DEBUG_LED("Reg: 0x%08X\n", led);
b095c381 1028 ipw_write_reg32(priv, IPW_EVENT_REG, led);
a613bffd
JK
1029
1030 spin_unlock_irqrestore(&priv->lock, flags);
1031}
1032
a73e22b2 1033static void ipw_led_band_off(struct ipw_priv *priv)
a613bffd
JK
1034{
1035 unsigned long flags;
1036 u32 led;
1037
1038 /* Only nic type 1 supports mode LEDs */
1039 if (priv->config & CFG_NO_LED || priv->nic_type != EEPROM_NIC_TYPE_1)
1040 return;
1041
1042 spin_lock_irqsave(&priv->lock, flags);
1043
b095c381 1044 led = ipw_read_reg32(priv, IPW_EVENT_REG);
a613bffd
JK
1045 led &= priv->led_ofdm_off;
1046 led &= priv->led_association_off;
1047
1048 led = ipw_register_toggle(led);
1049
1050 IPW_DEBUG_LED("Reg: 0x%08X\n", led);
b095c381 1051 ipw_write_reg32(priv, IPW_EVENT_REG, led);
a613bffd
JK
1052
1053 spin_unlock_irqrestore(&priv->lock, flags);
1054}
1055
a73e22b2 1056static void ipw_led_radio_on(struct ipw_priv *priv)
a613bffd
JK
1057{
1058 ipw_led_link_on(priv);
1059}
1060
a73e22b2 1061static void ipw_led_radio_off(struct ipw_priv *priv)
a613bffd
JK
1062{
1063 ipw_led_activity_off(priv);
1064 ipw_led_link_off(priv);
1065}
1066
a73e22b2 1067static void ipw_led_link_up(struct ipw_priv *priv)
a613bffd
JK
1068{
1069 /* Set the Link Led on for all nic types */
1070 ipw_led_link_on(priv);
1071}
1072
a73e22b2 1073static void ipw_led_link_down(struct ipw_priv *priv)
a613bffd
JK
1074{
1075 ipw_led_activity_off(priv);
1076 ipw_led_link_off(priv);
1077
1078 if (priv->status & STATUS_RF_KILL_MASK)
1079 ipw_led_radio_off(priv);
1080}
1081
a73e22b2 1082static void ipw_led_init(struct ipw_priv *priv)
a613bffd
JK
1083{
1084 priv->nic_type = priv->eeprom[EEPROM_NIC_TYPE];
1085
1086 /* Set the default PINs for the link and activity leds */
b095c381
JK
1087 priv->led_activity_on = IPW_ACTIVITY_LED;
1088 priv->led_activity_off = ~(IPW_ACTIVITY_LED);
a613bffd 1089
b095c381
JK
1090 priv->led_association_on = IPW_ASSOCIATED_LED;
1091 priv->led_association_off = ~(IPW_ASSOCIATED_LED);
a613bffd
JK
1092
1093 /* Set the default PINs for the OFDM leds */
b095c381
JK
1094 priv->led_ofdm_on = IPW_OFDM_LED;
1095 priv->led_ofdm_off = ~(IPW_OFDM_LED);
a613bffd
JK
1096
1097 switch (priv->nic_type) {
1098 case EEPROM_NIC_TYPE_1:
1099 /* In this NIC type, the LEDs are reversed.... */
b095c381
JK
1100 priv->led_activity_on = IPW_ASSOCIATED_LED;
1101 priv->led_activity_off = ~(IPW_ASSOCIATED_LED);
1102 priv->led_association_on = IPW_ACTIVITY_LED;
1103 priv->led_association_off = ~(IPW_ACTIVITY_LED);
a613bffd
JK
1104
1105 if (!(priv->config & CFG_NO_LED))
1106 ipw_led_band_on(priv);
1107
1108 /* And we don't blink link LEDs for this nic, so
1109 * just return here */
1110 return;
1111
1112 case EEPROM_NIC_TYPE_3:
1113 case EEPROM_NIC_TYPE_2:
1114 case EEPROM_NIC_TYPE_4:
1115 case EEPROM_NIC_TYPE_0:
1116 break;
1117
1118 default:
1119 IPW_DEBUG_INFO("Unknown NIC type from EEPROM: %d\n",
1120 priv->nic_type);
1121 priv->nic_type = EEPROM_NIC_TYPE_0;
1122 break;
1123 }
1124
1125 if (!(priv->config & CFG_NO_LED)) {
1126 if (priv->status & STATUS_ASSOCIATED)
1127 ipw_led_link_on(priv);
1128 else
1129 ipw_led_link_off(priv);
1130 }
1131}
1132
a73e22b2 1133static void ipw_led_shutdown(struct ipw_priv *priv)
a613bffd 1134{
a613bffd
JK
1135 ipw_led_activity_off(priv);
1136 ipw_led_link_off(priv);
1137 ipw_led_band_off(priv);
afbf30a2
JK
1138 cancel_delayed_work(&priv->led_link_on);
1139 cancel_delayed_work(&priv->led_link_off);
1140 cancel_delayed_work(&priv->led_act_off);
a613bffd
JK
1141}
1142
43f66a6c
JK
1143/*
1144 * The following adds a new attribute to the sysfs representation
1145 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/ipw/)
1146 * used for controling the debug level.
bf79451e 1147 *
43f66a6c
JK
1148 * See the level definitions in ipw for details.
1149 */
1150static ssize_t show_debug_level(struct device_driver *d, char *buf)
1151{
1152 return sprintf(buf, "0x%08X\n", ipw_debug_level);
1153}
a613bffd
JK
1154
1155static ssize_t store_debug_level(struct device_driver *d, const char *buf,
1156 size_t count)
43f66a6c
JK
1157{
1158 char *p = (char *)buf;
1159 u32 val;
1160
1161 if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
1162 p++;
1163 if (p[0] == 'x' || p[0] == 'X')
1164 p++;
1165 val = simple_strtoul(p, &p, 16);
1166 } else
1167 val = simple_strtoul(p, &p, 10);
bf79451e
JG
1168 if (p == buf)
1169 printk(KERN_INFO DRV_NAME
43f66a6c
JK
1170 ": %s is not in hex or decimal form.\n", buf);
1171 else
1172 ipw_debug_level = val;
1173
1174 return strnlen(buf, count);
1175}
1176
bf79451e 1177static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
43f66a6c
JK
1178 show_debug_level, store_debug_level);
1179
b39860c6 1180static inline u32 ipw_get_event_log_len(struct ipw_priv *priv)
43f66a6c 1181{
c8fe6679 1182 /* length = 1st dword in log */
b39860c6 1183 return ipw_read_reg32(priv, ipw_read32(priv, IPW_EVENT_LOG));
43f66a6c 1184}
0edd5b44 1185
b39860c6
JK
1186static void ipw_capture_event_log(struct ipw_priv *priv,
1187 u32 log_len, struct ipw_event *log)
43f66a6c 1188{
b39860c6 1189 u32 base;
0edd5b44 1190
b39860c6
JK
1191 if (log_len) {
1192 base = ipw_read32(priv, IPW_EVENT_LOG);
1193 ipw_read_indirect(priv, base + sizeof(base) + sizeof(u32),
1194 (u8 *) log, sizeof(*log) * log_len);
1195 }
1196}
43f66a6c 1197
b39860c6 1198static struct ipw_fw_error *ipw_alloc_error_log(struct ipw_priv *priv)
43f66a6c 1199{
b39860c6
JK
1200 struct ipw_fw_error *error;
1201 u32 log_len = ipw_get_event_log_len(priv);
1202 u32 base = ipw_read32(priv, IPW_ERROR_LOG);
1203 u32 elem_len = ipw_read_reg32(priv, base);
43f66a6c 1204
b39860c6
JK
1205 error = kmalloc(sizeof(*error) +
1206 sizeof(*error->elem) * elem_len +
1207 sizeof(*error->log) * log_len, GFP_ATOMIC);
1208 if (!error) {
1209 IPW_ERROR("Memory allocation for firmware error log "
1210 "failed.\n");
1211 return NULL;
43f66a6c 1212 }
f6c5cb7c 1213 error->jiffies = jiffies;
b39860c6
JK
1214 error->status = priv->status;
1215 error->config = priv->config;
1216 error->elem_len = elem_len;
1217 error->log_len = log_len;
1218 error->elem = (struct ipw_error_elem *)error->payload;
3b26b110 1219 error->log = (struct ipw_event *)(error->elem + elem_len);
b39860c6
JK
1220
1221 ipw_capture_event_log(priv, log_len, error->log);
bf79451e 1222
b39860c6
JK
1223 if (elem_len)
1224 ipw_read_indirect(priv, base + sizeof(base), (u8 *) error->elem,
1225 sizeof(*error->elem) * elem_len);
1226
1227 return error;
43f66a6c 1228}
0edd5b44 1229
b39860c6
JK
1230static ssize_t show_event_log(struct device *d,
1231 struct device_attribute *attr, char *buf)
43f66a6c 1232{
b39860c6
JK
1233 struct ipw_priv *priv = dev_get_drvdata(d);
1234 u32 log_len = ipw_get_event_log_len(priv);
1235 struct ipw_event log[log_len];
1236 u32 len = 0, i;
43f66a6c 1237
b39860c6 1238 ipw_capture_event_log(priv, log_len, log);
43f66a6c 1239
b39860c6
JK
1240 len += snprintf(buf + len, PAGE_SIZE - len, "%08X", log_len);
1241 for (i = 0; i < log_len; i++)
1242 len += snprintf(buf + len, PAGE_SIZE - len,
1243 "\n%08X%08X%08X",
1244 log[i].time, log[i].event, log[i].data);
1245 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
1246 return len;
43f66a6c 1247}
0edd5b44 1248
b39860c6 1249static DEVICE_ATTR(event_log, S_IRUGO, show_event_log, NULL);
43f66a6c 1250
b39860c6
JK
1251static ssize_t show_error(struct device *d,
1252 struct device_attribute *attr, char *buf)
43f66a6c 1253{
b39860c6
JK
1254 struct ipw_priv *priv = dev_get_drvdata(d);
1255 u32 len = 0, i;
1256 if (!priv->error)
1257 return 0;
1258 len += snprintf(buf + len, PAGE_SIZE - len,
f6c5cb7c
JK
1259 "%08lX%08X%08X%08X",
1260 priv->error->jiffies,
b39860c6
JK
1261 priv->error->status,
1262 priv->error->config, priv->error->elem_len);
1263 for (i = 0; i < priv->error->elem_len; i++)
1264 len += snprintf(buf + len, PAGE_SIZE - len,
1265 "\n%08X%08X%08X%08X%08X%08X%08X",
1266 priv->error->elem[i].time,
1267 priv->error->elem[i].desc,
1268 priv->error->elem[i].blink1,
1269 priv->error->elem[i].blink2,
1270 priv->error->elem[i].link1,
1271 priv->error->elem[i].link2,
1272 priv->error->elem[i].data);
1273
1274 len += snprintf(buf + len, PAGE_SIZE - len,
1275 "\n%08X", priv->error->log_len);
1276 for (i = 0; i < priv->error->log_len; i++)
1277 len += snprintf(buf + len, PAGE_SIZE - len,
1278 "\n%08X%08X%08X",
1279 priv->error->log[i].time,
1280 priv->error->log[i].event,
1281 priv->error->log[i].data);
1282 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
1283 return len;
1284}
1285
1286static ssize_t clear_error(struct device *d,
1287 struct device_attribute *attr,
1288 const char *buf, size_t count)
1289{
1290 struct ipw_priv *priv = dev_get_drvdata(d);
8f760780
JJ
1291
1292 kfree(priv->error);
1293 priv->error = NULL;
b39860c6
JK
1294 return count;
1295}
43f66a6c 1296
b39860c6 1297static DEVICE_ATTR(error, S_IRUGO | S_IWUSR, show_error, clear_error);
43f66a6c 1298
f6c5cb7c
JK
1299static ssize_t show_cmd_log(struct device *d,
1300 struct device_attribute *attr, char *buf)
1301{
1302 struct ipw_priv *priv = dev_get_drvdata(d);
1303 u32 len = 0, i;
1304 if (!priv->cmdlog)
1305 return 0;
1306 for (i = (priv->cmdlog_pos + 1) % priv->cmdlog_len;
1307 (i != priv->cmdlog_pos) && (PAGE_SIZE - len);
1308 i = (i + 1) % priv->cmdlog_len) {
1309 len +=
1310 snprintf(buf + len, PAGE_SIZE - len,
1311 "\n%08lX%08X%08X%08X\n", priv->cmdlog[i].jiffies,
1312 priv->cmdlog[i].retcode, priv->cmdlog[i].cmd.cmd,
1313 priv->cmdlog[i].cmd.len);
1314 len +=
1315 snprintk_buf(buf + len, PAGE_SIZE - len,
1316 (u8 *) priv->cmdlog[i].cmd.param,
1317 priv->cmdlog[i].cmd.len);
1318 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
1319 }
1320 len += snprintf(buf + len, PAGE_SIZE - len, "\n");
1321 return len;
43f66a6c 1322}
0edd5b44 1323
f6c5cb7c 1324static DEVICE_ATTR(cmd_log, S_IRUGO, show_cmd_log, NULL);
43f66a6c 1325
d685b8c2
ZY
1326#ifdef CONFIG_IPW2200_PROMISCUOUS
1327static void ipw_prom_free(struct ipw_priv *priv);
1328static int ipw_prom_alloc(struct ipw_priv *priv);
1329static ssize_t store_rtap_iface(struct device *d,
1330 struct device_attribute *attr,
1331 const char *buf, size_t count)
1332{
1333 struct ipw_priv *priv = dev_get_drvdata(d);
1334 int rc = 0;
1335
1336 if (count < 1)
1337 return -EINVAL;
1338
1339 switch (buf[0]) {
1340 case '0':
1341 if (!rtap_iface)
1342 return count;
1343
1344 if (netif_running(priv->prom_net_dev)) {
1345 IPW_WARNING("Interface is up. Cannot unregister.\n");
1346 return count;
1347 }
1348
1349 ipw_prom_free(priv);
1350 rtap_iface = 0;
1351 break;
1352
1353 case '1':
1354 if (rtap_iface)
1355 return count;
1356
1357 rc = ipw_prom_alloc(priv);
1358 if (!rc)
1359 rtap_iface = 1;
1360 break;
1361
1362 default:
1363 return -EINVAL;
1364 }
1365
1366 if (rc) {
1367 IPW_ERROR("Failed to register promiscuous network "
1368 "device (error %d).\n", rc);
1369 }
1370
1371 return count;
1372}
1373
1374static ssize_t show_rtap_iface(struct device *d,
1375 struct device_attribute *attr,
1376 char *buf)
1377{
1378 struct ipw_priv *priv = dev_get_drvdata(d);
1379 if (rtap_iface)
1380 return sprintf(buf, "%s", priv->prom_net_dev->name);
1381 else {
1382 buf[0] = '-';
1383 buf[1] = '1';
1384 buf[2] = '\0';
1385 return 3;
1386 }
1387}
1388
1389static DEVICE_ATTR(rtap_iface, S_IWUSR | S_IRUSR, show_rtap_iface,
1390 store_rtap_iface);
1391
1392static ssize_t store_rtap_filter(struct device *d,
1393 struct device_attribute *attr,
1394 const char *buf, size_t count)
1395{
1396 struct ipw_priv *priv = dev_get_drvdata(d);
1397
1398 if (!priv->prom_priv) {
1399 IPW_ERROR("Attempting to set filter without "
1400 "rtap_iface enabled.\n");
1401 return -EPERM;
1402 }
1403
1404 priv->prom_priv->filter = simple_strtol(buf, NULL, 0);
1405
1406 IPW_DEBUG_INFO("Setting rtap filter to " BIT_FMT16 "\n",
1407 BIT_ARG16(priv->prom_priv->filter));
1408
1409 return count;
1410}
1411
1412static ssize_t show_rtap_filter(struct device *d,
1413 struct device_attribute *attr,
1414 char *buf)
1415{
1416 struct ipw_priv *priv = dev_get_drvdata(d);
1417 return sprintf(buf, "0x%04X",
1418 priv->prom_priv ? priv->prom_priv->filter : 0);
1419}
1420
1421static DEVICE_ATTR(rtap_filter, S_IWUSR | S_IRUSR, show_rtap_filter,
1422 store_rtap_filter);
1423#endif
1424
a613bffd
JK
1425static ssize_t show_scan_age(struct device *d, struct device_attribute *attr,
1426 char *buf)
43f66a6c 1427{
a613bffd
JK
1428 struct ipw_priv *priv = dev_get_drvdata(d);
1429 return sprintf(buf, "%d\n", priv->ieee->scan_age);
1430}
1431
1432static ssize_t store_scan_age(struct device *d, struct device_attribute *attr,
1433 const char *buf, size_t count)
1434{
1435 struct ipw_priv *priv = dev_get_drvdata(d);
1436 struct net_device *dev = priv->net_dev;
1437 char buffer[] = "00000000";
1438 unsigned long len =
1439 (sizeof(buffer) - 1) > count ? count : sizeof(buffer) - 1;
1440 unsigned long val;
1441 char *p = buffer;
1442
1443 IPW_DEBUG_INFO("enter\n");
1444
1445 strncpy(buffer, buf, len);
1446 buffer[len] = 0;
1447
1448 if (p[1] == 'x' || p[1] == 'X' || p[0] == 'x' || p[0] == 'X') {
1449 p++;
1450 if (p[0] == 'x' || p[0] == 'X')
1451 p++;
1452 val = simple_strtoul(p, &p, 16);
1453 } else
1454 val = simple_strtoul(p, &p, 10);
1455 if (p == buffer) {
1456 IPW_DEBUG_INFO("%s: user supplied invalid value.\n", dev->name);
1457 } else {
1458 priv->ieee->scan_age = val;
1459 IPW_DEBUG_INFO("set scan_age = %u\n", priv->ieee->scan_age);
1460 }
1461
1462 IPW_DEBUG_INFO("exit\n");
1463 return len;
1464}
1465
1466static DEVICE_ATTR(scan_age, S_IWUSR | S_IRUGO, show_scan_age, store_scan_age);
1467
1468static ssize_t show_led(struct device *d, struct device_attribute *attr,
1469 char *buf)
1470{
1471 struct ipw_priv *priv = dev_get_drvdata(d);
1472 return sprintf(buf, "%d\n", (priv->config & CFG_NO_LED) ? 0 : 1);
1473}
1474
1475static ssize_t store_led(struct device *d, struct device_attribute *attr,
1476 const char *buf, size_t count)
1477{
1478 struct ipw_priv *priv = dev_get_drvdata(d);
1479
1480 IPW_DEBUG_INFO("enter\n");
1481
1482 if (count == 0)
1483 return 0;
1484
1485 if (*buf == 0) {
1486 IPW_DEBUG_LED("Disabling LED control.\n");
1487 priv->config |= CFG_NO_LED;
1488 ipw_led_shutdown(priv);
1489 } else {
1490 IPW_DEBUG_LED("Enabling LED control.\n");
1491 priv->config &= ~CFG_NO_LED;
1492 ipw_led_init(priv);
1493 }
1494
1495 IPW_DEBUG_INFO("exit\n");
1496 return count;
1497}
1498
1499static DEVICE_ATTR(led, S_IWUSR | S_IRUGO, show_led, store_led);
1500
ad3fee56 1501static ssize_t show_status(struct device *d,
0edd5b44 1502 struct device_attribute *attr, char *buf)
43f66a6c 1503{
ad3fee56 1504 struct ipw_priv *p = d->driver_data;
43f66a6c
JK
1505 return sprintf(buf, "0x%08x\n", (int)p->status);
1506}
0edd5b44 1507
43f66a6c
JK
1508static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
1509
ad3fee56
AM
1510static ssize_t show_cfg(struct device *d, struct device_attribute *attr,
1511 char *buf)
43f66a6c 1512{
ad3fee56 1513 struct ipw_priv *p = d->driver_data;
43f66a6c
JK
1514 return sprintf(buf, "0x%08x\n", (int)p->config);
1515}
0edd5b44 1516
43f66a6c
JK
1517static DEVICE_ATTR(cfg, S_IRUGO, show_cfg, NULL);
1518
ad3fee56 1519static ssize_t show_nic_type(struct device *d,
0edd5b44 1520 struct device_attribute *attr, char *buf)
43f66a6c 1521{
a613bffd
JK
1522 struct ipw_priv *priv = d->driver_data;
1523 return sprintf(buf, "TYPE: %d\n", priv->nic_type);
43f66a6c 1524}
0edd5b44 1525
43f66a6c
JK
1526static DEVICE_ATTR(nic_type, S_IRUGO, show_nic_type, NULL);
1527
ad3fee56 1528static ssize_t show_ucode_version(struct device *d,
0edd5b44 1529 struct device_attribute *attr, char *buf)
43f66a6c
JK
1530{
1531 u32 len = sizeof(u32), tmp = 0;
ad3fee56 1532 struct ipw_priv *p = d->driver_data;
43f66a6c 1533
0edd5b44 1534 if (ipw_get_ordinal(p, IPW_ORD_STAT_UCODE_VERSION, &tmp, &len))
43f66a6c
JK
1535 return 0;
1536
1537 return sprintf(buf, "0x%08x\n", tmp);
1538}
0edd5b44
JG
1539
1540static DEVICE_ATTR(ucode_version, S_IWUSR | S_IRUGO, show_ucode_version, NULL);
43f66a6c 1541
ad3fee56
AM
1542static ssize_t show_rtc(struct device *d, struct device_attribute *attr,
1543 char *buf)
43f66a6c
JK
1544{
1545 u32 len = sizeof(u32), tmp = 0;
ad3fee56 1546 struct ipw_priv *p = d->driver_data;
43f66a6c 1547
0edd5b44 1548 if (ipw_get_ordinal(p, IPW_ORD_STAT_RTC, &tmp, &len))
43f66a6c
JK
1549 return 0;
1550
1551 return sprintf(buf, "0x%08x\n", tmp);
1552}
0edd5b44
JG
1553
1554static DEVICE_ATTR(rtc, S_IWUSR | S_IRUGO, show_rtc, NULL);
43f66a6c
JK
1555
1556/*
1557 * Add a device attribute to view/control the delay between eeprom
1558 * operations.
1559 */
ad3fee56 1560static ssize_t show_eeprom_delay(struct device *d,
0edd5b44 1561 struct device_attribute *attr, char *buf)
43f66a6c 1562{
0edd5b44 1563 int n = ((struct ipw_priv *)d->driver_data)->eeprom_delay;
43f66a6c
JK
1564 return sprintf(buf, "%i\n", n);
1565}
ad3fee56 1566static ssize_t store_eeprom_delay(struct device *d,
0edd5b44
JG
1567 struct device_attribute *attr,
1568 const char *buf, size_t count)
43f66a6c 1569{
ad3fee56 1570 struct ipw_priv *p = d->driver_data;
43f66a6c
JK
1571 sscanf(buf, "%i", &p->eeprom_delay);
1572 return strnlen(buf, count);
1573}
0edd5b44
JG
1574
1575static DEVICE_ATTR(eeprom_delay, S_IWUSR | S_IRUGO,
1576 show_eeprom_delay, store_eeprom_delay);
43f66a6c 1577
ad3fee56 1578static ssize_t show_command_event_reg(struct device *d,
0edd5b44 1579 struct device_attribute *attr, char *buf)
43f66a6c
JK
1580{
1581 u32 reg = 0;
ad3fee56 1582 struct ipw_priv *p = d->driver_data;
43f66a6c 1583
b095c381 1584 reg = ipw_read_reg32(p, IPW_INTERNAL_CMD_EVENT);
43f66a6c
JK
1585 return sprintf(buf, "0x%08x\n", reg);
1586}
ad3fee56 1587static ssize_t store_command_event_reg(struct device *d,
0edd5b44
JG
1588 struct device_attribute *attr,
1589 const char *buf, size_t count)
43f66a6c
JK
1590{
1591 u32 reg;
ad3fee56 1592 struct ipw_priv *p = d->driver_data;
43f66a6c
JK
1593
1594 sscanf(buf, "%x", &reg);
b095c381 1595 ipw_write_reg32(p, IPW_INTERNAL_CMD_EVENT, reg);
43f66a6c
JK
1596 return strnlen(buf, count);
1597}
0edd5b44
JG
1598
1599static DEVICE_ATTR(command_event_reg, S_IWUSR | S_IRUGO,
1600 show_command_event_reg, store_command_event_reg);
43f66a6c 1601
ad3fee56 1602static ssize_t show_mem_gpio_reg(struct device *d,
0edd5b44 1603 struct device_attribute *attr, char *buf)
43f66a6c
JK
1604{
1605 u32 reg = 0;
ad3fee56 1606 struct ipw_priv *p = d->driver_data;
43f66a6c
JK
1607
1608 reg = ipw_read_reg32(p, 0x301100);
1609 return sprintf(buf, "0x%08x\n", reg);
1610}
ad3fee56 1611static ssize_t store_mem_gpio_reg(struct device *d,
0edd5b44
JG
1612 struct device_attribute *attr,
1613 const char *buf, size_t count)
43f66a6c
JK
1614{
1615 u32 reg;
ad3fee56 1616 struct ipw_priv *p = d->driver_data;
43f66a6c
JK
1617
1618 sscanf(buf, "%x", &reg);
1619 ipw_write_reg32(p, 0x301100, reg);
1620 return strnlen(buf, count);
1621}
0edd5b44
JG
1622
1623static DEVICE_ATTR(mem_gpio_reg, S_IWUSR | S_IRUGO,
1624 show_mem_gpio_reg, store_mem_gpio_reg);
43f66a6c 1625
ad3fee56 1626static ssize_t show_indirect_dword(struct device *d,
0edd5b44 1627 struct device_attribute *attr, char *buf)
43f66a6c
JK
1628{
1629 u32 reg = 0;
ad3fee56 1630 struct ipw_priv *priv = d->driver_data;
afbf30a2 1631
bf79451e 1632 if (priv->status & STATUS_INDIRECT_DWORD)
43f66a6c 1633 reg = ipw_read_reg32(priv, priv->indirect_dword);
bf79451e 1634 else
43f66a6c 1635 reg = 0;
bf79451e 1636
43f66a6c
JK
1637 return sprintf(buf, "0x%08x\n", reg);
1638}
ad3fee56 1639static ssize_t store_indirect_dword(struct device *d,
0edd5b44
JG
1640 struct device_attribute *attr,
1641 const char *buf, size_t count)
43f66a6c 1642{
ad3fee56 1643 struct ipw_priv *priv = d->driver_data;
43f66a6c
JK
1644
1645 sscanf(buf, "%x", &priv->indirect_dword);
1646 priv->status |= STATUS_INDIRECT_DWORD;
1647 return strnlen(buf, count);
1648}
0edd5b44
JG
1649
1650static DEVICE_ATTR(indirect_dword, S_IWUSR | S_IRUGO,
1651 show_indirect_dword, store_indirect_dword);
43f66a6c 1652
ad3fee56 1653static ssize_t show_indirect_byte(struct device *d,
0edd5b44 1654 struct device_attribute *attr, char *buf)
43f66a6c
JK
1655{
1656 u8 reg = 0;
ad3fee56 1657 struct ipw_priv *priv = d->driver_data;
afbf30a2 1658
bf79451e 1659 if (priv->status & STATUS_INDIRECT_BYTE)
43f66a6c 1660 reg = ipw_read_reg8(priv, priv->indirect_byte);
bf79451e 1661 else
43f66a6c
JK
1662 reg = 0;
1663
1664 return sprintf(buf, "0x%02x\n", reg);
1665}
ad3fee56 1666static ssize_t store_indirect_byte(struct device *d,
0edd5b44
JG
1667 struct device_attribute *attr,
1668 const char *buf, size_t count)
43f66a6c 1669{
ad3fee56 1670 struct ipw_priv *priv = d->driver_data;
43f66a6c
JK
1671
1672 sscanf(buf, "%x", &priv->indirect_byte);
1673 priv->status |= STATUS_INDIRECT_BYTE;
1674 return strnlen(buf, count);
1675}
0edd5b44
JG
1676
1677static DEVICE_ATTR(indirect_byte, S_IWUSR | S_IRUGO,
43f66a6c
JK
1678 show_indirect_byte, store_indirect_byte);
1679
ad3fee56 1680static ssize_t show_direct_dword(struct device *d,
0edd5b44 1681 struct device_attribute *attr, char *buf)
43f66a6c
JK
1682{
1683 u32 reg = 0;
ad3fee56 1684 struct ipw_priv *priv = d->driver_data;
43f66a6c 1685
bf79451e 1686 if (priv->status & STATUS_DIRECT_DWORD)
43f66a6c 1687 reg = ipw_read32(priv, priv->direct_dword);
bf79451e 1688 else
43f66a6c
JK
1689 reg = 0;
1690
1691 return sprintf(buf, "0x%08x\n", reg);
1692}
ad3fee56 1693static ssize_t store_direct_dword(struct device *d,
0edd5b44
JG
1694 struct device_attribute *attr,
1695 const char *buf, size_t count)
43f66a6c 1696{
ad3fee56 1697 struct ipw_priv *priv = d->driver_data;
43f66a6c
JK
1698
1699 sscanf(buf, "%x", &priv->direct_dword);
1700 priv->status |= STATUS_DIRECT_DWORD;
1701 return strnlen(buf, count);
1702}
43f66a6c 1703
0edd5b44
JG
1704static DEVICE_ATTR(direct_dword, S_IWUSR | S_IRUGO,
1705 show_direct_dword, store_direct_dword);
43f66a6c 1706
858119e1 1707static int rf_kill_active(struct ipw_priv *priv)
43f66a6c
JK
1708{
1709 if (0 == (ipw_read32(priv, 0x30) & 0x10000))
1710 priv->status |= STATUS_RF_KILL_HW;
1711 else
1712 priv->status &= ~STATUS_RF_KILL_HW;
1713
1714 return (priv->status & STATUS_RF_KILL_HW) ? 1 : 0;
1715}
1716
ad3fee56 1717static ssize_t show_rf_kill(struct device *d, struct device_attribute *attr,
0edd5b44 1718 char *buf)
43f66a6c
JK
1719{
1720 /* 0 - RF kill not enabled
bf79451e 1721 1 - SW based RF kill active (sysfs)
43f66a6c
JK
1722 2 - HW based RF kill active
1723 3 - Both HW and SW baed RF kill active */
ad3fee56 1724 struct ipw_priv *priv = d->driver_data;
43f66a6c 1725 int val = ((priv->status & STATUS_RF_KILL_SW) ? 0x1 : 0x0) |
0edd5b44 1726 (rf_kill_active(priv) ? 0x2 : 0x0);
43f66a6c
JK
1727 return sprintf(buf, "%i\n", val);
1728}
1729
1730static int ipw_radio_kill_sw(struct ipw_priv *priv, int disable_radio)
1731{
bf79451e 1732 if ((disable_radio ? 1 : 0) ==
ea2b26e0 1733 ((priv->status & STATUS_RF_KILL_SW) ? 1 : 0))
0edd5b44 1734 return 0;
43f66a6c
JK
1735
1736 IPW_DEBUG_RF_KILL("Manual SW RF Kill set to: RADIO %s\n",
1737 disable_radio ? "OFF" : "ON");
1738
1739 if (disable_radio) {
1740 priv->status |= STATUS_RF_KILL_SW;
1741
a613bffd 1742 if (priv->workqueue)
43f66a6c 1743 cancel_delayed_work(&priv->request_scan);
43f66a6c
JK
1744 queue_work(priv->workqueue, &priv->down);
1745 } else {
1746 priv->status &= ~STATUS_RF_KILL_SW;
1747 if (rf_kill_active(priv)) {
1748 IPW_DEBUG_RF_KILL("Can not turn radio back on - "
1749 "disabled by HW switch\n");
1750 /* Make sure the RF_KILL check timer is running */
1751 cancel_delayed_work(&priv->rf_kill);
bf79451e 1752 queue_delayed_work(priv->workqueue, &priv->rf_kill,
43f66a6c 1753 2 * HZ);
bf79451e 1754 } else
43f66a6c
JK
1755 queue_work(priv->workqueue, &priv->up);
1756 }
1757
1758 return 1;
1759}
1760
0edd5b44
JG
1761static ssize_t store_rf_kill(struct device *d, struct device_attribute *attr,
1762 const char *buf, size_t count)
43f66a6c 1763{
ad3fee56 1764 struct ipw_priv *priv = d->driver_data;
bf79451e 1765
43f66a6c
JK
1766 ipw_radio_kill_sw(priv, buf[0] == '1');
1767
1768 return count;
1769}
0edd5b44
JG
1770
1771static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
43f66a6c 1772
b095c381
JK
1773static ssize_t show_speed_scan(struct device *d, struct device_attribute *attr,
1774 char *buf)
1775{
1776 struct ipw_priv *priv = (struct ipw_priv *)d->driver_data;
1777 int pos = 0, len = 0;
1778 if (priv->config & CFG_SPEED_SCAN) {
1779 while (priv->speed_scan[pos] != 0)
1780 len += sprintf(&buf[len], "%d ",
1781 priv->speed_scan[pos++]);
1782 return len + sprintf(&buf[len], "\n");
1783 }
1784
1785 return sprintf(buf, "0\n");
1786}
1787
1788static ssize_t store_speed_scan(struct device *d, struct device_attribute *attr,
1789 const char *buf, size_t count)
1790{
1791 struct ipw_priv *priv = (struct ipw_priv *)d->driver_data;
1792 int channel, pos = 0;
1793 const char *p = buf;
1794
1795 /* list of space separated channels to scan, optionally ending with 0 */
1796 while ((channel = simple_strtol(p, NULL, 0))) {
1797 if (pos == MAX_SPEED_SCAN - 1) {
1798 priv->speed_scan[pos] = 0;
1799 break;
1800 }
1801
1867b117 1802 if (ieee80211_is_valid_channel(priv->ieee, channel))
b095c381
JK
1803 priv->speed_scan[pos++] = channel;
1804 else
1805 IPW_WARNING("Skipping invalid channel request: %d\n",
1806 channel);
1807 p = strchr(p, ' ');
1808 if (!p)
1809 break;
1810 while (*p == ' ' || *p == '\t')
1811 p++;
1812 }
1813
1814 if (pos == 0)
1815 priv->config &= ~CFG_SPEED_SCAN;
1816 else {
1817 priv->speed_scan_pos = 0;
1818 priv->config |= CFG_SPEED_SCAN;
1819 }
1820
1821 return count;
1822}
1823
1824static DEVICE_ATTR(speed_scan, S_IWUSR | S_IRUGO, show_speed_scan,
1825 store_speed_scan);
1826
1827static ssize_t show_net_stats(struct device *d, struct device_attribute *attr,
1828 char *buf)
1829{
1830 struct ipw_priv *priv = (struct ipw_priv *)d->driver_data;
1831 return sprintf(buf, "%c\n", (priv->config & CFG_NET_STATS) ? '1' : '0');
1832}
1833
1834static ssize_t store_net_stats(struct device *d, struct device_attribute *attr,
1835 const char *buf, size_t count)
1836{
1837 struct ipw_priv *priv = (struct ipw_priv *)d->driver_data;
1838 if (buf[0] == '1')
1839 priv->config |= CFG_NET_STATS;
1840 else
1841 priv->config &= ~CFG_NET_STATS;
1842
1843 return count;
1844}
1845
afbf30a2
JK
1846static DEVICE_ATTR(net_stats, S_IWUSR | S_IRUGO,
1847 show_net_stats, store_net_stats);
b095c381 1848
ea2b26e0
JK
1849static void notify_wx_assoc_event(struct ipw_priv *priv)
1850{
1851 union iwreq_data wrqu;
1852 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1853 if (priv->status & STATUS_ASSOCIATED)
1854 memcpy(wrqu.ap_addr.sa_data, priv->bssid, ETH_ALEN);
1855 else
1856 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
1857 wireless_send_event(priv->net_dev, SIOCGIWAP, &wrqu, NULL);
1858}
1859
43f66a6c
JK
1860static void ipw_irq_tasklet(struct ipw_priv *priv)
1861{
1862 u32 inta, inta_mask, handled = 0;
1863 unsigned long flags;
1864 int rc = 0;
1865
89c318ed 1866 spin_lock_irqsave(&priv->irq_lock, flags);
43f66a6c 1867
b095c381
JK
1868 inta = ipw_read32(priv, IPW_INTA_RW);
1869 inta_mask = ipw_read32(priv, IPW_INTA_MASK_R);
1870 inta &= (IPW_INTA_MASK_ALL & inta_mask);
43f66a6c
JK
1871
1872 /* Add any cached INTA values that need to be handled */
1873 inta |= priv->isr_inta;
1874
89c318ed
ZY
1875 spin_unlock_irqrestore(&priv->irq_lock, flags);
1876
1877 spin_lock_irqsave(&priv->lock, flags);
1878
43f66a6c 1879 /* handle all the justifications for the interrupt */
b095c381 1880 if (inta & IPW_INTA_BIT_RX_TRANSFER) {
43f66a6c 1881 ipw_rx(priv);
b095c381 1882 handled |= IPW_INTA_BIT_RX_TRANSFER;
43f66a6c
JK
1883 }
1884
b095c381 1885 if (inta & IPW_INTA_BIT_TX_CMD_QUEUE) {
43f66a6c 1886 IPW_DEBUG_HC("Command completed.\n");
0edd5b44 1887 rc = ipw_queue_tx_reclaim(priv, &priv->txq_cmd, -1);
43f66a6c
JK
1888 priv->status &= ~STATUS_HCMD_ACTIVE;
1889 wake_up_interruptible(&priv->wait_command_queue);
b095c381 1890 handled |= IPW_INTA_BIT_TX_CMD_QUEUE;
43f66a6c
JK
1891 }
1892
b095c381 1893 if (inta & IPW_INTA_BIT_TX_QUEUE_1) {
43f66a6c 1894 IPW_DEBUG_TX("TX_QUEUE_1\n");
0edd5b44 1895 rc = ipw_queue_tx_reclaim(priv, &priv->txq[0], 0);
b095c381 1896 handled |= IPW_INTA_BIT_TX_QUEUE_1;
43f66a6c
JK
1897 }
1898
b095c381 1899 if (inta & IPW_INTA_BIT_TX_QUEUE_2) {
43f66a6c 1900 IPW_DEBUG_TX("TX_QUEUE_2\n");
0edd5b44 1901 rc = ipw_queue_tx_reclaim(priv, &priv->txq[1], 1);
b095c381 1902 handled |= IPW_INTA_BIT_TX_QUEUE_2;
43f66a6c
JK
1903 }
1904
b095c381 1905 if (inta & IPW_INTA_BIT_TX_QUEUE_3) {
43f66a6c 1906 IPW_DEBUG_TX("TX_QUEUE_3\n");
0edd5b44 1907 rc = ipw_queue_tx_reclaim(priv, &priv->txq[2], 2);
b095c381 1908 handled |= IPW_INTA_BIT_TX_QUEUE_3;
43f66a6c
JK
1909 }
1910
b095c381 1911 if (inta & IPW_INTA_BIT_TX_QUEUE_4) {
43f66a6c 1912 IPW_DEBUG_TX("TX_QUEUE_4\n");
0edd5b44 1913 rc = ipw_queue_tx_reclaim(priv, &priv->txq[3], 3);
b095c381 1914 handled |= IPW_INTA_BIT_TX_QUEUE_4;
43f66a6c
JK
1915 }
1916
b095c381 1917 if (inta & IPW_INTA_BIT_STATUS_CHANGE) {
43f66a6c 1918 IPW_WARNING("STATUS_CHANGE\n");
b095c381 1919 handled |= IPW_INTA_BIT_STATUS_CHANGE;
43f66a6c
JK
1920 }
1921
b095c381 1922 if (inta & IPW_INTA_BIT_BEACON_PERIOD_EXPIRED) {
43f66a6c 1923 IPW_WARNING("TX_PERIOD_EXPIRED\n");
b095c381 1924 handled |= IPW_INTA_BIT_BEACON_PERIOD_EXPIRED;
43f66a6c
JK
1925 }
1926
b095c381 1927 if (inta & IPW_INTA_BIT_SLAVE_MODE_HOST_CMD_DONE) {
43f66a6c 1928 IPW_WARNING("HOST_CMD_DONE\n");
b095c381 1929 handled |= IPW_INTA_BIT_SLAVE_MODE_HOST_CMD_DONE;
43f66a6c
JK
1930 }
1931
b095c381 1932 if (inta & IPW_INTA_BIT_FW_INITIALIZATION_DONE) {
43f66a6c 1933 IPW_WARNING("FW_INITIALIZATION_DONE\n");
b095c381 1934 handled |= IPW_INTA_BIT_FW_INITIALIZATION_DONE;
43f66a6c
JK
1935 }
1936
b095c381 1937 if (inta & IPW_INTA_BIT_FW_CARD_DISABLE_PHY_OFF_DONE) {
43f66a6c 1938 IPW_WARNING("PHY_OFF_DONE\n");
b095c381 1939 handled |= IPW_INTA_BIT_FW_CARD_DISABLE_PHY_OFF_DONE;
43f66a6c
JK
1940 }
1941
b095c381 1942 if (inta & IPW_INTA_BIT_RF_KILL_DONE) {
43f66a6c
JK
1943 IPW_DEBUG_RF_KILL("RF_KILL_DONE\n");
1944 priv->status |= STATUS_RF_KILL_HW;
1945 wake_up_interruptible(&priv->wait_command_queue);
ea2b26e0 1946 priv->status &= ~(STATUS_ASSOCIATED | STATUS_ASSOCIATING);
43f66a6c 1947 cancel_delayed_work(&priv->request_scan);
a613bffd 1948 schedule_work(&priv->link_down);
43f66a6c 1949 queue_delayed_work(priv->workqueue, &priv->rf_kill, 2 * HZ);
b095c381 1950 handled |= IPW_INTA_BIT_RF_KILL_DONE;
43f66a6c 1951 }
bf79451e 1952
b095c381 1953 if (inta & IPW_INTA_BIT_FATAL_ERROR) {
1d1b09eb 1954 IPW_WARNING("Firmware error detected. Restarting.\n");
b39860c6 1955 if (priv->error) {
1d1b09eb 1956 IPW_DEBUG_FW("Sysfs 'error' log already exists.\n");
b39860c6
JK
1957 if (ipw_debug_level & IPW_DL_FW_ERRORS) {
1958 struct ipw_fw_error *error =
1959 ipw_alloc_error_log(priv);
1960 ipw_dump_error_log(priv, error);
8f760780 1961 kfree(error);
b39860c6 1962 }
b39860c6
JK
1963 } else {
1964 priv->error = ipw_alloc_error_log(priv);
1965 if (priv->error)
1d1b09eb 1966 IPW_DEBUG_FW("Sysfs 'error' log captured.\n");
b39860c6 1967 else
1d1b09eb
ZY
1968 IPW_DEBUG_FW("Error allocating sysfs 'error' "
1969 "log.\n");
b39860c6
JK
1970 if (ipw_debug_level & IPW_DL_FW_ERRORS)
1971 ipw_dump_error_log(priv, priv->error);
b39860c6
JK
1972 }
1973
b095c381
JK
1974 /* XXX: If hardware encryption is for WPA/WPA2,
1975 * we have to notify the supplicant. */
1976 if (priv->ieee->sec.encrypt) {
1977 priv->status &= ~STATUS_ASSOCIATED;
1978 notify_wx_assoc_event(priv);
1979 }
1980
1981 /* Keep the restart process from trying to send host
1982 * commands by clearing the INIT status bit */
1983 priv->status &= ~STATUS_INIT;
afbf30a2
JK
1984
1985 /* Cancel currently queued command. */
1986 priv->status &= ~STATUS_HCMD_ACTIVE;
1987 wake_up_interruptible(&priv->wait_command_queue);
1988
43f66a6c 1989 queue_work(priv->workqueue, &priv->adapter_restart);
b095c381 1990 handled |= IPW_INTA_BIT_FATAL_ERROR;
43f66a6c
JK
1991 }
1992
b095c381 1993 if (inta & IPW_INTA_BIT_PARITY_ERROR) {
43f66a6c 1994 IPW_ERROR("Parity error\n");
b095c381 1995 handled |= IPW_INTA_BIT_PARITY_ERROR;
43f66a6c
JK
1996 }
1997
1998 if (handled != inta) {
0edd5b44 1999 IPW_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
43f66a6c
JK
2000 }
2001
89c318ed
ZY
2002 spin_unlock_irqrestore(&priv->lock, flags);
2003
43f66a6c
JK
2004 /* enable all interrupts */
2005 ipw_enable_interrupts(priv);
43f66a6c 2006}
bf79451e 2007
43f66a6c
JK
2008#define IPW_CMD(x) case IPW_CMD_ ## x : return #x
2009static char *get_cmd_string(u8 cmd)
2010{
2011 switch (cmd) {
2012 IPW_CMD(HOST_COMPLETE);
bf79451e
JG
2013 IPW_CMD(POWER_DOWN);
2014 IPW_CMD(SYSTEM_CONFIG);
2015 IPW_CMD(MULTICAST_ADDRESS);
2016 IPW_CMD(SSID);
2017 IPW_CMD(ADAPTER_ADDRESS);
2018 IPW_CMD(PORT_TYPE);
2019 IPW_CMD(RTS_THRESHOLD);
2020 IPW_CMD(FRAG_THRESHOLD);
2021 IPW_CMD(POWER_MODE);
2022 IPW_CMD(WEP_KEY);
2023 IPW_CMD(TGI_TX_KEY);
2024 IPW_CMD(SCAN_REQUEST);
2025 IPW_CMD(SCAN_REQUEST_EXT);
2026 IPW_CMD(ASSOCIATE);
2027 IPW_CMD(SUPPORTED_RATES);
2028 IPW_CMD(SCAN_ABORT);
2029 IPW_CMD(TX_FLUSH);
2030 IPW_CMD(QOS_PARAMETERS);
2031 IPW_CMD(DINO_CONFIG);
2032 IPW_CMD(RSN_CAPABILITIES);
2033 IPW_CMD(RX_KEY);
2034 IPW_CMD(CARD_DISABLE);
2035 IPW_CMD(SEED_NUMBER);
2036 IPW_CMD(TX_POWER);
2037 IPW_CMD(COUNTRY_INFO);
2038 IPW_CMD(AIRONET_INFO);
2039 IPW_CMD(AP_TX_POWER);
2040 IPW_CMD(CCKM_INFO);
2041 IPW_CMD(CCX_VER_INFO);
2042 IPW_CMD(SET_CALIBRATION);
2043 IPW_CMD(SENSITIVITY_CALIB);
2044 IPW_CMD(RETRY_LIMIT);
2045 IPW_CMD(IPW_PRE_POWER_DOWN);
2046 IPW_CMD(VAP_BEACON_TEMPLATE);
2047 IPW_CMD(VAP_DTIM_PERIOD);
2048 IPW_CMD(EXT_SUPPORTED_RATES);
2049 IPW_CMD(VAP_LOCAL_TX_PWR_CONSTRAINT);
2050 IPW_CMD(VAP_QUIET_INTERVALS);
2051 IPW_CMD(VAP_CHANNEL_SWITCH);
2052 IPW_CMD(VAP_MANDATORY_CHANNELS);
2053 IPW_CMD(VAP_CELL_PWR_LIMIT);
2054 IPW_CMD(VAP_CF_PARAM_SET);
2055 IPW_CMD(VAP_SET_BEACONING_STATE);
2056 IPW_CMD(MEASUREMENT);
2057 IPW_CMD(POWER_CAPABILITY);
2058 IPW_CMD(SUPPORTED_CHANNELS);
2059 IPW_CMD(TPC_REPORT);
2060 IPW_CMD(WME_INFO);
2061 IPW_CMD(PRODUCTION_COMMAND);
2062 default:
43f66a6c
JK
2063 return "UNKNOWN";
2064 }
2065}
43f66a6c
JK
2066
2067#define HOST_COMPLETE_TIMEOUT HZ
0a7bcf26
ZY
2068
2069static int __ipw_send_cmd(struct ipw_priv *priv, struct host_cmd *cmd)
43f66a6c
JK
2070{
2071 int rc = 0;
a613bffd 2072 unsigned long flags;
43f66a6c 2073
a613bffd 2074 spin_lock_irqsave(&priv->lock, flags);
43f66a6c 2075 if (priv->status & STATUS_HCMD_ACTIVE) {
9ddf84f6
JK
2076 IPW_ERROR("Failed to send %s: Already sending a command.\n",
2077 get_cmd_string(cmd->cmd));
a613bffd 2078 spin_unlock_irqrestore(&priv->lock, flags);
9ddf84f6 2079 return -EAGAIN;
43f66a6c
JK
2080 }
2081
2082 priv->status |= STATUS_HCMD_ACTIVE;
bf79451e 2083
f6c5cb7c
JK
2084 if (priv->cmdlog) {
2085 priv->cmdlog[priv->cmdlog_pos].jiffies = jiffies;
2086 priv->cmdlog[priv->cmdlog_pos].cmd.cmd = cmd->cmd;
2087 priv->cmdlog[priv->cmdlog_pos].cmd.len = cmd->len;
2088 memcpy(priv->cmdlog[priv->cmdlog_pos].cmd.param, cmd->param,
2089 cmd->len);
2090 priv->cmdlog[priv->cmdlog_pos].retcode = -1;
2091 }
2092
b095c381
JK
2093 IPW_DEBUG_HC("%s command (#%d) %d bytes: 0x%08X\n",
2094 get_cmd_string(cmd->cmd), cmd->cmd, cmd->len,
2095 priv->status);
f516dbcd
ZY
2096
2097#ifndef DEBUG_CMD_WEP_KEY
2098 if (cmd->cmd == IPW_CMD_WEP_KEY)
2099 IPW_DEBUG_HC("WEP_KEY command masked out for secure.\n");
2100 else
2101#endif
2102 printk_buf(IPW_DL_HOST_COMMAND, (u8 *) cmd->param, cmd->len);
2103
0a7bcf26 2104 rc = ipw_queue_tx_hcmd(priv, cmd->cmd, cmd->param, cmd->len, 0);
a613bffd
JK
2105 if (rc) {
2106 priv->status &= ~STATUS_HCMD_ACTIVE;
9ddf84f6
JK
2107 IPW_ERROR("Failed to send %s: Reason %d\n",
2108 get_cmd_string(cmd->cmd), rc);
a613bffd 2109 spin_unlock_irqrestore(&priv->lock, flags);
f6c5cb7c 2110 goto exit;
a613bffd
JK
2111 }
2112 spin_unlock_irqrestore(&priv->lock, flags);
43f66a6c 2113
0edd5b44
JG
2114 rc = wait_event_interruptible_timeout(priv->wait_command_queue,
2115 !(priv->
2116 status & STATUS_HCMD_ACTIVE),
2117 HOST_COMPLETE_TIMEOUT);
43f66a6c 2118 if (rc == 0) {
a613bffd
JK
2119 spin_lock_irqsave(&priv->lock, flags);
2120 if (priv->status & STATUS_HCMD_ACTIVE) {
9ddf84f6
JK
2121 IPW_ERROR("Failed to send %s: Command timed out.\n",
2122 get_cmd_string(cmd->cmd));
a613bffd
JK
2123 priv->status &= ~STATUS_HCMD_ACTIVE;
2124 spin_unlock_irqrestore(&priv->lock, flags);
f6c5cb7c
JK
2125 rc = -EIO;
2126 goto exit;
a613bffd
JK
2127 }
2128 spin_unlock_irqrestore(&priv->lock, flags);
3b9990cb
JK
2129 } else
2130 rc = 0;
a613bffd 2131
b095c381 2132 if (priv->status & STATUS_RF_KILL_HW) {
9ddf84f6
JK
2133 IPW_ERROR("Failed to send %s: Aborted due to RF kill switch.\n",
2134 get_cmd_string(cmd->cmd));
f6c5cb7c
JK
2135 rc = -EIO;
2136 goto exit;
43f66a6c
JK
2137 }
2138
2638bc39 2139 exit:
f6c5cb7c
JK
2140 if (priv->cmdlog) {
2141 priv->cmdlog[priv->cmdlog_pos++].retcode = rc;
2142 priv->cmdlog_pos %= priv->cmdlog_len;
2143 }
2144 return rc;
43f66a6c
JK
2145}
2146
0a7bcf26
ZY
2147static int ipw_send_cmd_simple(struct ipw_priv *priv, u8 command)
2148{
2149 struct host_cmd cmd = {
2150 .cmd = command,
2151 };
2152
2153 return __ipw_send_cmd(priv, &cmd);
2154}
2155
2156static int ipw_send_cmd_pdu(struct ipw_priv *priv, u8 command, u8 len,
2157 void *data)
43f66a6c
JK
2158{
2159 struct host_cmd cmd = {
0a7bcf26
ZY
2160 .cmd = command,
2161 .len = len,
2162 .param = data,
43f66a6c
JK
2163 };
2164
0a7bcf26
ZY
2165 return __ipw_send_cmd(priv, &cmd);
2166}
2167
2168static int ipw_send_host_complete(struct ipw_priv *priv)
2169{
43f66a6c
JK
2170 if (!priv) {
2171 IPW_ERROR("Invalid args\n");
2172 return -1;
2173 }
2174
0a7bcf26 2175 return ipw_send_cmd_simple(priv, IPW_CMD_HOST_COMPLETE);
43f66a6c
JK
2176}
2177
d685b8c2 2178static int ipw_send_system_config(struct ipw_priv *priv)
43f66a6c 2179{
d685b8c2
ZY
2180 return ipw_send_cmd_pdu(priv, IPW_CMD_SYSTEM_CONFIG,
2181 sizeof(priv->sys_config),
2182 &priv->sys_config);
43f66a6c
JK
2183}
2184
0edd5b44 2185static int ipw_send_ssid(struct ipw_priv *priv, u8 * ssid, int len)
43f66a6c 2186{
43f66a6c
JK
2187 if (!priv || !ssid) {
2188 IPW_ERROR("Invalid args\n");
2189 return -1;
2190 }
2191
0a7bcf26 2192 return ipw_send_cmd_pdu(priv, IPW_CMD_SSID, min(len, IW_ESSID_MAX_SIZE),
2638bc39 2193 ssid);
43f66a6c
JK
2194}
2195
0edd5b44 2196static int ipw_send_adapter_address(struct ipw_priv *priv, u8 * mac)
43f66a6c 2197{
43f66a6c
JK
2198 if (!priv || !mac) {
2199 IPW_ERROR("Invalid args\n");
2200 return -1;
2201 }
2202
2203 IPW_DEBUG_INFO("%s: Setting MAC to " MAC_FMT "\n",
2204 priv->net_dev->name, MAC_ARG(mac));
2205
2638bc39 2206 return ipw_send_cmd_pdu(priv, IPW_CMD_ADAPTER_ADDRESS, ETH_ALEN, mac);
43f66a6c
JK
2207}
2208
a613bffd
JK
2209/*
2210 * NOTE: This must be executed from our workqueue as it results in udelay
2211 * being called which may corrupt the keyboard if executed on default
2212 * workqueue
2213 */
43f66a6c
JK
2214static void ipw_adapter_restart(void *adapter)
2215{
2216 struct ipw_priv *priv = adapter;
2217
2218 if (priv->status & STATUS_RF_KILL_MASK)
2219 return;
2220
2221 ipw_down(priv);
b095c381
JK
2222
2223 if (priv->assoc_network &&
2224 (priv->assoc_network->capability & WLAN_CAPABILITY_IBSS))
2225 ipw_remove_current_network(priv);
2226
43f66a6c
JK
2227 if (ipw_up(priv)) {
2228 IPW_ERROR("Failed to up device\n");
2229 return;
2230 }
2231}
2232
c848d0af
JK
2233static void ipw_bg_adapter_restart(void *data)
2234{
2235 struct ipw_priv *priv = data;
4644151b 2236 mutex_lock(&priv->mutex);
c848d0af 2237 ipw_adapter_restart(data);
4644151b 2238 mutex_unlock(&priv->mutex);
c848d0af
JK
2239}
2240
43f66a6c
JK
2241#define IPW_SCAN_CHECK_WATCHDOG (5 * HZ)
2242
2243static void ipw_scan_check(void *data)
2244{
2245 struct ipw_priv *priv = data;
2246 if (priv->status & (STATUS_SCANNING | STATUS_SCAN_ABORTING)) {
2247 IPW_DEBUG_SCAN("Scan completion watchdog resetting "
c7b6a674
ZY
2248 "adapter after (%dms).\n",
2249 jiffies_to_msecs(IPW_SCAN_CHECK_WATCHDOG));
a613bffd 2250 queue_work(priv->workqueue, &priv->adapter_restart);
43f66a6c
JK
2251 }
2252}
2253
c848d0af
JK
2254static void ipw_bg_scan_check(void *data)
2255{
2256 struct ipw_priv *priv = data;
4644151b 2257 mutex_lock(&priv->mutex);
c848d0af 2258 ipw_scan_check(data);
4644151b 2259 mutex_unlock(&priv->mutex);
c848d0af
JK
2260}
2261
43f66a6c
JK
2262static int ipw_send_scan_request_ext(struct ipw_priv *priv,
2263 struct ipw_scan_request_ext *request)
2264{
0a7bcf26 2265 return ipw_send_cmd_pdu(priv, IPW_CMD_SCAN_REQUEST_EXT,
2638bc39 2266 sizeof(*request), request);
43f66a6c
JK
2267}
2268
2269static int ipw_send_scan_abort(struct ipw_priv *priv)
2270{
43f66a6c
JK
2271 if (!priv) {
2272 IPW_ERROR("Invalid args\n");
2273 return -1;
2274 }
2275
0a7bcf26 2276 return ipw_send_cmd_simple(priv, IPW_CMD_SCAN_ABORT);
43f66a6c
JK
2277}
2278
2279static int ipw_set_sensitivity(struct ipw_priv *priv, u16 sens)
2280{
0a7bcf26 2281 struct ipw_sensitivity_calib calib = {
851ca268 2282 .beacon_rssi_raw = cpu_to_le16(sens),
43f66a6c 2283 };
0a7bcf26
ZY
2284
2285 return ipw_send_cmd_pdu(priv, IPW_CMD_SENSITIVITY_CALIB, sizeof(calib),
2638bc39 2286 &calib);
43f66a6c
JK
2287}
2288
2289static int ipw_send_associate(struct ipw_priv *priv,
2290 struct ipw_associate *associate)
2291{
a613bffd 2292 struct ipw_associate tmp_associate;
0a7bcf26
ZY
2293
2294 if (!priv || !associate) {
2295 IPW_ERROR("Invalid args\n");
2296 return -1;
2297 }
2298
a613bffd
JK
2299 memcpy(&tmp_associate, associate, sizeof(*associate));
2300 tmp_associate.policy_support =
2301 cpu_to_le16(tmp_associate.policy_support);
2302 tmp_associate.assoc_tsf_msw = cpu_to_le32(tmp_associate.assoc_tsf_msw);
2303 tmp_associate.assoc_tsf_lsw = cpu_to_le32(tmp_associate.assoc_tsf_lsw);
2304 tmp_associate.capability = cpu_to_le16(tmp_associate.capability);
2305 tmp_associate.listen_interval =
2306 cpu_to_le16(tmp_associate.listen_interval);
2307 tmp_associate.beacon_interval =
2308 cpu_to_le16(tmp_associate.beacon_interval);
2309 tmp_associate.atim_window = cpu_to_le16(tmp_associate.atim_window);
2310
0a7bcf26 2311 return ipw_send_cmd_pdu(priv, IPW_CMD_ASSOCIATE, sizeof(tmp_associate),
2638bc39 2312 &tmp_associate);
43f66a6c
JK
2313}
2314
2315static int ipw_send_supported_rates(struct ipw_priv *priv,
2316 struct ipw_supported_rates *rates)
2317{
43f66a6c
JK
2318 if (!priv || !rates) {
2319 IPW_ERROR("Invalid args\n");
2320 return -1;
2321 }
2322
0a7bcf26 2323 return ipw_send_cmd_pdu(priv, IPW_CMD_SUPPORTED_RATES, sizeof(*rates),
2638bc39 2324 rates);
43f66a6c
JK
2325}
2326
2327static int ipw_set_random_seed(struct ipw_priv *priv)
2328{
0a7bcf26 2329 u32 val;
43f66a6c
JK
2330
2331 if (!priv) {
2332 IPW_ERROR("Invalid args\n");
2333 return -1;
2334 }
2335
0a7bcf26 2336 get_random_bytes(&val, sizeof(val));
43f66a6c 2337
0a7bcf26 2338 return ipw_send_cmd_pdu(priv, IPW_CMD_SEED_NUMBER, sizeof(val), &val);
43f66a6c
JK
2339}
2340
43f66a6c
JK
2341static int ipw_send_card_disable(struct ipw_priv *priv, u32 phy_off)
2342{
43f66a6c
JK
2343 if (!priv) {
2344 IPW_ERROR("Invalid args\n");
2345 return -1;
2346 }
2347
851ca268 2348 phy_off = cpu_to_le32(phy_off);
0a7bcf26 2349 return ipw_send_cmd_pdu(priv, IPW_CMD_CARD_DISABLE, sizeof(phy_off),
2638bc39 2350 &phy_off);
43f66a6c 2351}
43f66a6c 2352
0edd5b44 2353static int ipw_send_tx_power(struct ipw_priv *priv, struct ipw_tx_power *power)
43f66a6c 2354{
43f66a6c
JK
2355 if (!priv || !power) {
2356 IPW_ERROR("Invalid args\n");
2357 return -1;
2358 }
2359
2638bc39 2360 return ipw_send_cmd_pdu(priv, IPW_CMD_TX_POWER, sizeof(*power), power);
43f66a6c
JK
2361}
2362
6de9f7f2
ZY
2363static int ipw_set_tx_power(struct ipw_priv *priv)
2364{
1867b117 2365 const struct ieee80211_geo *geo = ieee80211_get_geo(priv->ieee);
6de9f7f2
ZY
2366 struct ipw_tx_power tx_power;
2367 s8 max_power;
2368 int i;
2369
2370 memset(&tx_power, 0, sizeof(tx_power));
2371
2372 /* configure device for 'G' band */
2373 tx_power.ieee_mode = IPW_G_MODE;
2374 tx_power.num_channels = geo->bg_channels;
2375 for (i = 0; i < geo->bg_channels; i++) {
2376 max_power = geo->bg[i].max_power;
2377 tx_power.channels_tx_power[i].channel_number =
2378 geo->bg[i].channel;
2379 tx_power.channels_tx_power[i].tx_power = max_power ?
2380 min(max_power, priv->tx_power) : priv->tx_power;
43f66a6c 2381 }
6de9f7f2
ZY
2382 if (ipw_send_tx_power(priv, &tx_power))
2383 return -EIO;
2384
2385 /* configure device to also handle 'B' band */
2386 tx_power.ieee_mode = IPW_B_MODE;
2387 if (ipw_send_tx_power(priv, &tx_power))
2388 return -EIO;
bf79451e 2389
6de9f7f2
ZY
2390 /* configure device to also handle 'A' band */
2391 if (priv->ieee->abg_true) {
2392 tx_power.ieee_mode = IPW_A_MODE;
2393 tx_power.num_channels = geo->a_channels;
2394 for (i = 0; i < tx_power.num_channels; i++) {
2395 max_power = geo->a[i].max_power;
2396 tx_power.channels_tx_power[i].channel_number =
2397 geo->a[i].channel;
2398 tx_power.channels_tx_power[i].tx_power = max_power ?
2399 min(max_power, priv->tx_power) : priv->tx_power;
2400 }
2401 if (ipw_send_tx_power(priv, &tx_power))
2402 return -EIO;
2403 }
43f66a6c
JK
2404 return 0;
2405}
2406
2407static int ipw_send_rts_threshold(struct ipw_priv *priv, u16 rts)
2408{
2409 struct ipw_rts_threshold rts_threshold = {
851ca268 2410 .rts_threshold = cpu_to_le16(rts),
43f66a6c 2411 };
43f66a6c
JK
2412
2413 if (!priv) {
2414 IPW_ERROR("Invalid args\n");
2415 return -1;
2416 }
2417
0a7bcf26
ZY
2418 return ipw_send_cmd_pdu(priv, IPW_CMD_RTS_THRESHOLD,
2419 sizeof(rts_threshold), &rts_threshold);
43f66a6c
JK
2420}
2421
2422static int ipw_send_frag_threshold(struct ipw_priv *priv, u16 frag)
2423{
2424 struct ipw_frag_threshold frag_threshold = {
851ca268 2425 .frag_threshold = cpu_to_le16(frag),
43f66a6c 2426 };
43f66a6c
JK
2427
2428 if (!priv) {
2429 IPW_ERROR("Invalid args\n");
2430 return -1;
2431 }
2432
0a7bcf26
ZY
2433 return ipw_send_cmd_pdu(priv, IPW_CMD_FRAG_THRESHOLD,
2434 sizeof(frag_threshold), &frag_threshold);
43f66a6c
JK
2435}
2436
2437static int ipw_send_power_mode(struct ipw_priv *priv, u32 mode)
2438{
0a7bcf26 2439 u32 param;
43f66a6c
JK
2440
2441 if (!priv) {
2442 IPW_ERROR("Invalid args\n");
2443 return -1;
2444 }
bf79451e 2445
43f66a6c
JK
2446 /* If on battery, set to 3, if AC set to CAM, else user
2447 * level */
2448 switch (mode) {
2449 case IPW_POWER_BATTERY:
0a7bcf26 2450 param = IPW_POWER_INDEX_3;
43f66a6c
JK
2451 break;
2452 case IPW_POWER_AC:
0a7bcf26 2453 param = IPW_POWER_MODE_CAM;
43f66a6c
JK
2454 break;
2455 default:
0a7bcf26 2456 param = mode;
43f66a6c
JK
2457 break;
2458 }
2459
851ca268 2460 param = cpu_to_le32(mode);
0a7bcf26 2461 return ipw_send_cmd_pdu(priv, IPW_CMD_POWER_MODE, sizeof(param),
2638bc39 2462 &param);
43f66a6c
JK
2463}
2464
afbf30a2
JK
2465static int ipw_send_retry_limit(struct ipw_priv *priv, u8 slimit, u8 llimit)
2466{
2467 struct ipw_retry_limit retry_limit = {
2468 .short_retry_limit = slimit,
2469 .long_retry_limit = llimit
2470 };
afbf30a2
JK
2471
2472 if (!priv) {
2473 IPW_ERROR("Invalid args\n");
2474 return -1;
2475 }
2476
0a7bcf26 2477 return ipw_send_cmd_pdu(priv, IPW_CMD_RETRY_LIMIT, sizeof(retry_limit),
2638bc39 2478 &retry_limit);
afbf30a2
JK
2479}
2480
43f66a6c
JK
2481/*
2482 * The IPW device contains a Microwire compatible EEPROM that stores
2483 * various data like the MAC address. Usually the firmware has exclusive
2484 * access to the eeprom, but during device initialization (before the
2485 * device driver has sent the HostComplete command to the firmware) the
2486 * device driver has read access to the EEPROM by way of indirect addressing
2487 * through a couple of memory mapped registers.
2488 *
2489 * The following is a simplified implementation for pulling data out of the
2490 * the eeprom, along with some helper functions to find information in
2491 * the per device private data's copy of the eeprom.
2492 *
2493 * NOTE: To better understand how these functions work (i.e what is a chip
2494 * select and why do have to keep driving the eeprom clock?), read
2495 * just about any data sheet for a Microwire compatible EEPROM.
2496 */
2497
2498/* write a 32 bit value into the indirect accessor register */
2499static inline void eeprom_write_reg(struct ipw_priv *p, u32 data)
2500{
2501 ipw_write_reg32(p, FW_MEM_REG_EEPROM_ACCESS, data);
bf79451e 2502
43f66a6c
JK
2503 /* the eeprom requires some time to complete the operation */
2504 udelay(p->eeprom_delay);
2505
2506 return;
2507}
2508
2509/* perform a chip select operation */
858119e1 2510static void eeprom_cs(struct ipw_priv *priv)
43f66a6c 2511{
0edd5b44
JG
2512 eeprom_write_reg(priv, 0);
2513 eeprom_write_reg(priv, EEPROM_BIT_CS);
2514 eeprom_write_reg(priv, EEPROM_BIT_CS | EEPROM_BIT_SK);
2515 eeprom_write_reg(priv, EEPROM_BIT_CS);
43f66a6c
JK
2516}
2517
2518/* perform a chip select operation */
858119e1 2519static void eeprom_disable_cs(struct ipw_priv *priv)
43f66a6c 2520{
0edd5b44
JG
2521 eeprom_write_reg(priv, EEPROM_BIT_CS);
2522 eeprom_write_reg(priv, 0);
2523 eeprom_write_reg(priv, EEPROM_BIT_SK);
43f66a6c
JK
2524}
2525
2526/* push a single bit down to the eeprom */
0edd5b44 2527static inline void eeprom_write_bit(struct ipw_priv *p, u8 bit)
43f66a6c 2528{
0edd5b44
JG
2529 int d = (bit ? EEPROM_BIT_DI : 0);
2530 eeprom_write_reg(p, EEPROM_BIT_CS | d);
2531 eeprom_write_reg(p, EEPROM_BIT_CS | d | EEPROM_BIT_SK);
43f66a6c
JK
2532}
2533
2534/* push an opcode followed by an address down to the eeprom */
0edd5b44 2535static void eeprom_op(struct ipw_priv *priv, u8 op, u8 addr)
43f66a6c
JK
2536{
2537 int i;
2538
2539 eeprom_cs(priv);
0edd5b44
JG
2540 eeprom_write_bit(priv, 1);
2541 eeprom_write_bit(priv, op & 2);
2542 eeprom_write_bit(priv, op & 1);
2543 for (i = 7; i >= 0; i--) {
2544 eeprom_write_bit(priv, addr & (1 << i));
43f66a6c
JK
2545 }
2546}
2547
2548/* pull 16 bits off the eeprom, one bit at a time */
0edd5b44 2549static u16 eeprom_read_u16(struct ipw_priv *priv, u8 addr)
43f66a6c
JK
2550{
2551 int i;
0edd5b44 2552 u16 r = 0;
bf79451e 2553
43f66a6c 2554 /* Send READ Opcode */
0edd5b44 2555 eeprom_op(priv, EEPROM_CMD_READ, addr);
43f66a6c
JK
2556
2557 /* Send dummy bit */
0edd5b44 2558 eeprom_write_reg(priv, EEPROM_BIT_CS);
43f66a6c
JK
2559
2560 /* Read the byte off the eeprom one bit at a time */
0edd5b44 2561 for (i = 0; i < 16; i++) {
43f66a6c 2562 u32 data = 0;
0edd5b44
JG
2563 eeprom_write_reg(priv, EEPROM_BIT_CS | EEPROM_BIT_SK);
2564 eeprom_write_reg(priv, EEPROM_BIT_CS);
2565 data = ipw_read_reg32(priv, FW_MEM_REG_EEPROM_ACCESS);
2566 r = (r << 1) | ((data & EEPROM_BIT_DO) ? 1 : 0);
43f66a6c 2567 }
bf79451e 2568
43f66a6c 2569 /* Send another dummy bit */
0edd5b44 2570 eeprom_write_reg(priv, 0);
43f66a6c 2571 eeprom_disable_cs(priv);
bf79451e 2572
43f66a6c
JK
2573 return r;
2574}
2575
2576/* helper function for pulling the mac address out of the private */
2577/* data's copy of the eeprom data */
0edd5b44 2578static void eeprom_parse_mac(struct ipw_priv *priv, u8 * mac)
43f66a6c 2579{
afbf30a2 2580 memcpy(mac, &priv->eeprom[EEPROM_MAC_ADDRESS], 6);
43f66a6c
JK
2581}
2582
2583/*
2584 * Either the device driver (i.e. the host) or the firmware can
2585 * load eeprom data into the designated region in SRAM. If neither
2586 * happens then the FW will shutdown with a fatal error.
2587 *
2588 * In order to signal the FW to load the EEPROM, the EEPROM_LOAD_DISABLE
2589 * bit needs region of shared SRAM needs to be non-zero.
2590 */
2591static void ipw_eeprom_init_sram(struct ipw_priv *priv)
2592{
2593 int i;
0edd5b44 2594 u16 *eeprom = (u16 *) priv->eeprom;
bf79451e 2595
43f66a6c
JK
2596 IPW_DEBUG_TRACE(">>\n");
2597
2598 /* read entire contents of eeprom into private buffer */
0edd5b44 2599 for (i = 0; i < 128; i++)
a613bffd 2600 eeprom[i] = le16_to_cpu(eeprom_read_u16(priv, (u8) i));
43f66a6c 2601
bf79451e
JG
2602 /*
2603 If the data looks correct, then copy it to our private
43f66a6c 2604 copy. Otherwise let the firmware know to perform the operation
c7b6a674 2605 on its own.
0edd5b44 2606 */
386093ef 2607 if (priv->eeprom[EEPROM_VERSION] != 0) {
43f66a6c
JK
2608 IPW_DEBUG_INFO("Writing EEPROM data into SRAM\n");
2609
2610 /* write the eeprom data to sram */
b095c381 2611 for (i = 0; i < IPW_EEPROM_IMAGE_SIZE; i++)
0edd5b44 2612 ipw_write8(priv, IPW_EEPROM_DATA + i, priv->eeprom[i]);
43f66a6c
JK
2613
2614 /* Do not load eeprom data on fatal error or suspend */
2615 ipw_write32(priv, IPW_EEPROM_LOAD_DISABLE, 0);
2616 } else {
2617 IPW_DEBUG_INFO("Enabling FW initializationg of SRAM\n");
2618
2619 /* Load eeprom data on fatal error or suspend */
2620 ipw_write32(priv, IPW_EEPROM_LOAD_DISABLE, 1);
2621 }
2622
2623 IPW_DEBUG_TRACE("<<\n");
2624}
2625
858119e1 2626static void ipw_zero_memory(struct ipw_priv *priv, u32 start, u32 count)
43f66a6c
JK
2627{
2628 count >>= 2;
0edd5b44
JG
2629 if (!count)
2630 return;
b095c381 2631 _ipw_write32(priv, IPW_AUTOINC_ADDR, start);
bf79451e 2632 while (count--)
b095c381 2633 _ipw_write32(priv, IPW_AUTOINC_DATA, 0);
43f66a6c
JK
2634}
2635
2636static inline void ipw_fw_dma_reset_command_blocks(struct ipw_priv *priv)
2637{
b095c381 2638 ipw_zero_memory(priv, IPW_SHARED_SRAM_DMA_CONTROL,
bf79451e 2639 CB_NUMBER_OF_ELEMENTS_SMALL *
43f66a6c
JK
2640 sizeof(struct command_block));
2641}
2642
2643static int ipw_fw_dma_enable(struct ipw_priv *priv)
0edd5b44 2644{ /* start dma engine but no transfers yet */
43f66a6c
JK
2645
2646 IPW_DEBUG_FW(">> : \n");
bf79451e 2647
43f66a6c
JK
2648 /* Start the dma */
2649 ipw_fw_dma_reset_command_blocks(priv);
bf79451e 2650
43f66a6c 2651 /* Write CB base address */
b095c381 2652 ipw_write_reg32(priv, IPW_DMA_I_CB_BASE, IPW_SHARED_SRAM_DMA_CONTROL);
43f66a6c
JK
2653
2654 IPW_DEBUG_FW("<< : \n");
2655 return 0;
2656}
2657
2658static void ipw_fw_dma_abort(struct ipw_priv *priv)
2659{
2660 u32 control = 0;
2661
2662 IPW_DEBUG_FW(">> :\n");
bf79451e 2663
67fd6b45 2664 /* set the Stop and Abort bit */
43f66a6c 2665 control = DMA_CONTROL_SMALL_CB_CONST_VALUE | DMA_CB_STOP_AND_ABORT;
b095c381 2666 ipw_write_reg32(priv, IPW_DMA_I_DMA_CONTROL, control);
43f66a6c 2667 priv->sram_desc.last_cb_index = 0;
bf79451e 2668
43f66a6c
JK
2669 IPW_DEBUG_FW("<< \n");
2670}
2671
0edd5b44
JG
2672static int ipw_fw_dma_write_command_block(struct ipw_priv *priv, int index,
2673 struct command_block *cb)
43f66a6c 2674{
0edd5b44 2675 u32 address =
b095c381 2676 IPW_SHARED_SRAM_DMA_CONTROL +
0edd5b44 2677 (sizeof(struct command_block) * index);
43f66a6c
JK
2678 IPW_DEBUG_FW(">> :\n");
2679
0edd5b44
JG
2680 ipw_write_indirect(priv, address, (u8 *) cb,
2681 (int)sizeof(struct command_block));
43f66a6c
JK
2682
2683 IPW_DEBUG_FW("<< :\n");
2684 return 0;
2685
2686}
2687
2688static int ipw_fw_dma_kick(struct ipw_priv *priv)
2689{
2690 u32 control = 0;
0edd5b44 2691 u32 index = 0;
43f66a6c
JK
2692
2693 IPW_DEBUG_FW(">> :\n");
bf79451e 2694
43f66a6c 2695 for (index = 0; index < priv->sram_desc.last_cb_index; index++)
0edd5b44
JG
2696 ipw_fw_dma_write_command_block(priv, index,
2697 &priv->sram_desc.cb_list[index]);
43f66a6c
JK
2698
2699 /* Enable the DMA in the CSR register */
b095c381
JK
2700 ipw_clear_bit(priv, IPW_RESET_REG,
2701 IPW_RESET_REG_MASTER_DISABLED |
2702 IPW_RESET_REG_STOP_MASTER);
bf79451e 2703
0edd5b44 2704 /* Set the Start bit. */
43f66a6c 2705 control = DMA_CONTROL_SMALL_CB_CONST_VALUE | DMA_CB_START;
b095c381 2706 ipw_write_reg32(priv, IPW_DMA_I_DMA_CONTROL, control);
43f66a6c
JK
2707
2708 IPW_DEBUG_FW("<< :\n");
2709 return 0;
2710}
2711
2712static void ipw_fw_dma_dump_command_block(struct ipw_priv *priv)
2713{
2714 u32 address;
0edd5b44
JG
2715 u32 register_value = 0;
2716 u32 cb_fields_address = 0;
43f66a6c
JK
2717
2718 IPW_DEBUG_FW(">> :\n");
b095c381 2719 address = ipw_read_reg32(priv, IPW_DMA_I_CURRENT_CB);
0edd5b44 2720 IPW_DEBUG_FW_INFO("Current CB is 0x%x \n", address);
43f66a6c
JK
2721
2722 /* Read the DMA Controlor register */
b095c381
JK
2723 register_value = ipw_read_reg32(priv, IPW_DMA_I_DMA_CONTROL);
2724 IPW_DEBUG_FW_INFO("IPW_DMA_I_DMA_CONTROL is 0x%x \n", register_value);
43f66a6c 2725
0edd5b44 2726 /* Print the CB values */
43f66a6c
JK
2727 cb_fields_address = address;
2728 register_value = ipw_read_reg32(priv, cb_fields_address);
0edd5b44 2729 IPW_DEBUG_FW_INFO("Current CB ControlField is 0x%x \n", register_value);
43f66a6c
JK
2730
2731 cb_fields_address += sizeof(u32);
2732 register_value = ipw_read_reg32(priv, cb_fields_address);
0edd5b44 2733 IPW_DEBUG_FW_INFO("Current CB Source Field is 0x%x \n", register_value);
43f66a6c
JK
2734
2735 cb_fields_address += sizeof(u32);
2736 register_value = ipw_read_reg32(priv, cb_fields_address);
2737 IPW_DEBUG_FW_INFO("Current CB Destination Field is 0x%x \n",
2738 register_value);
2739
2740 cb_fields_address += sizeof(u32);
2741 register_value = ipw_read_reg32(priv, cb_fields_address);
0edd5b44 2742 IPW_DEBUG_FW_INFO("Current CB Status Field is 0x%x \n", register_value);
43f66a6c
JK
2743
2744 IPW_DEBUG_FW(">> :\n");
2745}
2746
2747static int ipw_fw_dma_command_block_index(struct ipw_priv *priv)
2748{
2749 u32 current_cb_address = 0;
2750 u32 current_cb_index = 0;
2751
2752 IPW_DEBUG_FW("<< :\n");
b095c381 2753 current_cb_address = ipw_read_reg32(priv, IPW_DMA_I_CURRENT_CB);
bf79451e 2754
b095c381 2755 current_cb_index = (current_cb_address - IPW_SHARED_SRAM_DMA_CONTROL) /
0edd5b44 2756 sizeof(struct command_block);
bf79451e 2757
43f66a6c 2758 IPW_DEBUG_FW_INFO("Current CB index 0x%x address = 0x%X \n",
0edd5b44 2759 current_cb_index, current_cb_address);
43f66a6c
JK
2760
2761 IPW_DEBUG_FW(">> :\n");
2762 return current_cb_index;
2763
2764}
2765
2766static int ipw_fw_dma_add_command_block(struct ipw_priv *priv,
2767 u32 src_address,
2768 u32 dest_address,
2769 u32 length,
0edd5b44 2770 int interrupt_enabled, int is_last)
43f66a6c
JK
2771{
2772
bf79451e 2773 u32 control = CB_VALID | CB_SRC_LE | CB_DEST_LE | CB_SRC_AUTOINC |
0edd5b44
JG
2774 CB_SRC_IO_GATED | CB_DEST_AUTOINC | CB_SRC_SIZE_LONG |
2775 CB_DEST_SIZE_LONG;
43f66a6c 2776 struct command_block *cb;
0edd5b44 2777 u32 last_cb_element = 0;
43f66a6c
JK
2778
2779 IPW_DEBUG_FW_INFO("src_address=0x%x dest_address=0x%x length=0x%x\n",
2780 src_address, dest_address, length);
2781
2782 if (priv->sram_desc.last_cb_index >= CB_NUMBER_OF_ELEMENTS_SMALL)
2783 return -1;
2784
2785 last_cb_element = priv->sram_desc.last_cb_index;
2786 cb = &priv->sram_desc.cb_list[last_cb_element];
2787 priv->sram_desc.last_cb_index++;
2788
2789 /* Calculate the new CB control word */
0edd5b44 2790 if (interrupt_enabled)
43f66a6c
JK
2791 control |= CB_INT_ENABLED;
2792
2793 if (is_last)
2794 control |= CB_LAST_VALID;
bf79451e 2795
43f66a6c
JK
2796 control |= length;
2797
2798 /* Calculate the CB Element's checksum value */
0edd5b44 2799 cb->status = control ^ src_address ^ dest_address;
43f66a6c
JK
2800
2801 /* Copy the Source and Destination addresses */
2802 cb->dest_addr = dest_address;
2803 cb->source_addr = src_address;
2804
2805 /* Copy the Control Word last */
2806 cb->control = control;
2807
2808 return 0;
2809}
2810
2811static int ipw_fw_dma_add_buffer(struct ipw_priv *priv,
0edd5b44 2812 u32 src_phys, u32 dest_address, u32 length)
43f66a6c
JK
2813{
2814 u32 bytes_left = length;
0edd5b44
JG
2815 u32 src_offset = 0;
2816 u32 dest_offset = 0;
43f66a6c
JK
2817 int status = 0;
2818 IPW_DEBUG_FW(">> \n");
2819 IPW_DEBUG_FW_INFO("src_phys=0x%x dest_address=0x%x length=0x%x\n",
2820 src_phys, dest_address, length);
2821 while (bytes_left > CB_MAX_LENGTH) {
0edd5b44
JG
2822 status = ipw_fw_dma_add_command_block(priv,
2823 src_phys + src_offset,
2824 dest_address +
2825 dest_offset,
2826 CB_MAX_LENGTH, 0, 0);
43f66a6c
JK
2827 if (status) {
2828 IPW_DEBUG_FW_INFO(": Failed\n");
2829 return -1;
bf79451e 2830 } else
43f66a6c
JK
2831 IPW_DEBUG_FW_INFO(": Added new cb\n");
2832
2833 src_offset += CB_MAX_LENGTH;
2834 dest_offset += CB_MAX_LENGTH;
2835 bytes_left -= CB_MAX_LENGTH;
2836 }
2837
2838 /* add the buffer tail */
2839 if (bytes_left > 0) {
0edd5b44
JG
2840 status =
2841 ipw_fw_dma_add_command_block(priv, src_phys + src_offset,
2842 dest_address + dest_offset,
2843 bytes_left, 0, 0);
43f66a6c
JK
2844 if (status) {
2845 IPW_DEBUG_FW_INFO(": Failed on the buffer tail\n");
2846 return -1;
bf79451e 2847 } else
0edd5b44
JG
2848 IPW_DEBUG_FW_INFO
2849 (": Adding new cb - the buffer tail\n");
43f66a6c 2850 }
bf79451e 2851
43f66a6c
JK
2852 IPW_DEBUG_FW("<< \n");
2853 return 0;
2854}
2855
2856static int ipw_fw_dma_wait(struct ipw_priv *priv)
2857{
397ae121 2858 u32 current_index = 0, previous_index;
43f66a6c
JK
2859 u32 watchdog = 0;
2860
2861 IPW_DEBUG_FW(">> : \n");
2862
2863 current_index = ipw_fw_dma_command_block_index(priv);
397ae121 2864 IPW_DEBUG_FW_INFO("sram_desc.last_cb_index:0x%08X\n",
0edd5b44 2865 (int)priv->sram_desc.last_cb_index);
43f66a6c
JK
2866
2867 while (current_index < priv->sram_desc.last_cb_index) {
2868 udelay(50);
397ae121 2869 previous_index = current_index;
43f66a6c
JK
2870 current_index = ipw_fw_dma_command_block_index(priv);
2871
397ae121
ZY
2872 if (previous_index < current_index) {
2873 watchdog = 0;
2874 continue;
2875 }
2876 if (++watchdog > 400) {
43f66a6c
JK
2877 IPW_DEBUG_FW_INFO("Timeout\n");
2878 ipw_fw_dma_dump_command_block(priv);
2879 ipw_fw_dma_abort(priv);
2880 return -1;
2881 }
2882 }
2883
2884 ipw_fw_dma_abort(priv);
2885
0edd5b44 2886 /*Disable the DMA in the CSR register */
b095c381
JK
2887 ipw_set_bit(priv, IPW_RESET_REG,
2888 IPW_RESET_REG_MASTER_DISABLED | IPW_RESET_REG_STOP_MASTER);
43f66a6c
JK
2889
2890 IPW_DEBUG_FW("<< dmaWaitSync \n");
2891 return 0;
2892}
2893
bf79451e 2894static void ipw_remove_current_network(struct ipw_priv *priv)
43f66a6c
JK
2895{
2896 struct list_head *element, *safe;
bf79451e 2897 struct ieee80211_network *network = NULL;
a613bffd
JK
2898 unsigned long flags;
2899
2900 spin_lock_irqsave(&priv->ieee->lock, flags);
43f66a6c
JK
2901 list_for_each_safe(element, safe, &priv->ieee->network_list) {
2902 network = list_entry(element, struct ieee80211_network, list);
2903 if (!memcmp(network->bssid, priv->bssid, ETH_ALEN)) {
2904 list_del(element);
bf79451e 2905 list_add_tail(&network->list,
43f66a6c
JK
2906 &priv->ieee->network_free_list);
2907 }
2908 }
a613bffd 2909 spin_unlock_irqrestore(&priv->ieee->lock, flags);
43f66a6c
JK
2910}
2911
2912/**
bf79451e 2913 * Check that card is still alive.
43f66a6c
JK
2914 * Reads debug register from domain0.
2915 * If card is present, pre-defined value should
2916 * be found there.
bf79451e 2917 *
43f66a6c
JK
2918 * @param priv
2919 * @return 1 if card is present, 0 otherwise
2920 */
2921static inline int ipw_alive(struct ipw_priv *priv)
2922{
2923 return ipw_read32(priv, 0x90) == 0xd55555d5;
2924}
2925
c7b6a674 2926/* timeout in msec, attempted in 10-msec quanta */
858119e1 2927static int ipw_poll_bit(struct ipw_priv *priv, u32 addr, u32 mask,
43f66a6c
JK
2928 int timeout)
2929{
2930 int i = 0;
2931
2932 do {
bf79451e 2933 if ((ipw_read32(priv, addr) & mask) == mask)
43f66a6c
JK
2934 return i;
2935 mdelay(10);
2936 i += 10;
2937 } while (i < timeout);
bf79451e 2938
43f66a6c
JK
2939 return -ETIME;
2940}
2941
bf79451e 2942/* These functions load the firmware and micro code for the operation of
43f66a6c
JK
2943 * the ipw hardware. It assumes the buffer has all the bits for the
2944 * image and the caller is handling the memory allocation and clean up.
2945 */
2946
0edd5b44 2947static int ipw_stop_master(struct ipw_priv *priv)
43f66a6c
JK
2948{
2949 int rc;
bf79451e 2950
43f66a6c
JK
2951 IPW_DEBUG_TRACE(">> \n");
2952 /* stop master. typical delay - 0 */
b095c381 2953 ipw_set_bit(priv, IPW_RESET_REG, IPW_RESET_REG_STOP_MASTER);
43f66a6c 2954
c7b6a674 2955 /* timeout is in msec, polled in 10-msec quanta */
b095c381
JK
2956 rc = ipw_poll_bit(priv, IPW_RESET_REG,
2957 IPW_RESET_REG_MASTER_DISABLED, 100);
43f66a6c 2958 if (rc < 0) {
c7b6a674 2959 IPW_ERROR("wait for stop master failed after 100ms\n");
43f66a6c
JK
2960 return -1;
2961 }
2962
2963 IPW_DEBUG_INFO("stop master %dms\n", rc);
2964
2965 return rc;
2966}
2967
2968static void ipw_arc_release(struct ipw_priv *priv)
2969{
2970 IPW_DEBUG_TRACE(">> \n");
2971 mdelay(5);
2972
b095c381 2973 ipw_clear_bit(priv, IPW_RESET_REG, CBD_RESET_REG_PRINCETON_RESET);
43f66a6c
JK
2974
2975 /* no one knows timing, for safety add some delay */
2976 mdelay(5);
2977}
2978
43f66a6c
JK
2979struct fw_chunk {
2980 u32 address;
2981 u32 length;
2982};
2983
0edd5b44 2984static int ipw_load_ucode(struct ipw_priv *priv, u8 * data, size_t len)
43f66a6c
JK
2985{
2986 int rc = 0, i, addr;
2987 u8 cr = 0;
2988 u16 *image;
2989
0edd5b44 2990 image = (u16 *) data;
bf79451e 2991
43f66a6c
JK
2992 IPW_DEBUG_TRACE(">> \n");
2993
2994 rc = ipw_stop_master(priv);
2995
2996 if (rc < 0)
2997 return rc;
bf79451e 2998
b095c381
JK
2999 for (addr = IPW_SHARED_LOWER_BOUND;
3000 addr < IPW_REGISTER_DOMAIN1_END; addr += 4) {
43f66a6c
JK
3001 ipw_write32(priv, addr, 0);
3002 }
3003
3004 /* no ucode (yet) */
3005 memset(&priv->dino_alive, 0, sizeof(priv->dino_alive));
3006 /* destroy DMA queues */
3007 /* reset sequence */
3008
b095c381 3009 ipw_write_reg32(priv, IPW_MEM_HALT_AND_RESET, IPW_BIT_HALT_RESET_ON);
43f66a6c 3010 ipw_arc_release(priv);
b095c381 3011 ipw_write_reg32(priv, IPW_MEM_HALT_AND_RESET, IPW_BIT_HALT_RESET_OFF);
43f66a6c
JK
3012 mdelay(1);
3013
3014 /* reset PHY */
b095c381 3015 ipw_write_reg32(priv, IPW_INTERNAL_CMD_EVENT, IPW_BASEBAND_POWER_DOWN);
43f66a6c 3016 mdelay(1);
bf79451e 3017
b095c381 3018 ipw_write_reg32(priv, IPW_INTERNAL_CMD_EVENT, 0);
43f66a6c 3019 mdelay(1);
bf79451e 3020
43f66a6c 3021 /* enable ucode store */
c8fe6679
ZY
3022 ipw_write_reg8(priv, IPW_BASEBAND_CONTROL_STATUS, 0x0);
3023 ipw_write_reg8(priv, IPW_BASEBAND_CONTROL_STATUS, DINO_ENABLE_CS);
43f66a6c
JK
3024 mdelay(1);
3025
3026 /* write ucode */
3027 /**
3028 * @bug
3029 * Do NOT set indirect address register once and then
3030 * store data to indirect data register in the loop.
3031 * It seems very reasonable, but in this case DINO do not
3032 * accept ucode. It is essential to set address each time.
3033 */
3034 /* load new ipw uCode */
3035 for (i = 0; i < len / 2; i++)
b095c381 3036 ipw_write_reg16(priv, IPW_BASEBAND_CONTROL_STORE,
a613bffd 3037 cpu_to_le16(image[i]));
43f66a6c 3038
43f66a6c 3039 /* enable DINO */
b095c381
JK
3040 ipw_write_reg8(priv, IPW_BASEBAND_CONTROL_STATUS, 0);
3041 ipw_write_reg8(priv, IPW_BASEBAND_CONTROL_STATUS, DINO_ENABLE_SYSTEM);
43f66a6c 3042
0edd5b44 3043 /* this is where the igx / win driver deveates from the VAP driver. */
43f66a6c
JK
3044
3045 /* wait for alive response */
3046 for (i = 0; i < 100; i++) {
3047 /* poll for incoming data */
b095c381 3048 cr = ipw_read_reg8(priv, IPW_BASEBAND_CONTROL_STATUS);
43f66a6c
JK
3049 if (cr & DINO_RXFIFO_DATA)
3050 break;
3051 mdelay(1);
3052 }
3053
3054 if (cr & DINO_RXFIFO_DATA) {
3055 /* alive_command_responce size is NOT multiple of 4 */
3056 u32 response_buffer[(sizeof(priv->dino_alive) + 3) / 4];
bf79451e
JG
3057
3058 for (i = 0; i < ARRAY_SIZE(response_buffer); i++)
43f66a6c 3059 response_buffer[i] =
a613bffd 3060 le32_to_cpu(ipw_read_reg32(priv,
b095c381 3061 IPW_BASEBAND_RX_FIFO_READ));
43f66a6c
JK
3062 memcpy(&priv->dino_alive, response_buffer,
3063 sizeof(priv->dino_alive));
3064 if (priv->dino_alive.alive_command == 1
3065 && priv->dino_alive.ucode_valid == 1) {
3066 rc = 0;
0edd5b44
JG
3067 IPW_DEBUG_INFO
3068 ("Microcode OK, rev. %d (0x%x) dev. %d (0x%x) "
3069 "of %02d/%02d/%02d %02d:%02d\n",
3070 priv->dino_alive.software_revision,
3071 priv->dino_alive.software_revision,
3072 priv->dino_alive.device_identifier,
3073 priv->dino_alive.device_identifier,
3074 priv->dino_alive.time_stamp[0],
3075 priv->dino_alive.time_stamp[1],
3076 priv->dino_alive.time_stamp[2],
3077 priv->dino_alive.time_stamp[3],
3078 priv->dino_alive.time_stamp[4]);
43f66a6c
JK
3079 } else {
3080 IPW_DEBUG_INFO("Microcode is not alive\n");
3081 rc = -EINVAL;
3082 }
3083 } else {
3084 IPW_DEBUG_INFO("No alive response from DINO\n");
3085 rc = -ETIME;
3086 }
3087
3088 /* disable DINO, otherwise for some reason
3089 firmware have problem getting alive resp. */
b095c381 3090 ipw_write_reg8(priv, IPW_BASEBAND_CONTROL_STATUS, 0);
43f66a6c 3091
43f66a6c
JK
3092 return rc;
3093}
3094
0edd5b44 3095static int ipw_load_firmware(struct ipw_priv *priv, u8 * data, size_t len)
43f66a6c
JK
3096{
3097 int rc = -1;
3098 int offset = 0;
3099 struct fw_chunk *chunk;
3100 dma_addr_t shared_phys;
3101 u8 *shared_virt;
3102
3103 IPW_DEBUG_TRACE("<< : \n");
3104 shared_virt = pci_alloc_consistent(priv->pci_dev, len, &shared_phys);
3105
3106 if (!shared_virt)
3107 return -ENOMEM;
3108
3109 memmove(shared_virt, data, len);
3110
3111 /* Start the Dma */
3112 rc = ipw_fw_dma_enable(priv);
3113
3114 if (priv->sram_desc.last_cb_index > 0) {
3115 /* the DMA is already ready this would be a bug. */
3116 BUG();
3117 goto out;
3118 }
3119
3120 do {
3121 chunk = (struct fw_chunk *)(data + offset);
3122 offset += sizeof(struct fw_chunk);
3123 /* build DMA packet and queue up for sending */
bf79451e 3124 /* dma to chunk->address, the chunk->length bytes from data +
43f66a6c
JK
3125 * offeset*/
3126 /* Dma loading */
3127 rc = ipw_fw_dma_add_buffer(priv, shared_phys + offset,
a613bffd
JK
3128 le32_to_cpu(chunk->address),
3129 le32_to_cpu(chunk->length));
43f66a6c
JK
3130 if (rc) {
3131 IPW_DEBUG_INFO("dmaAddBuffer Failed\n");
3132 goto out;
3133 }
bf79451e 3134
a613bffd 3135 offset += le32_to_cpu(chunk->length);
43f66a6c
JK
3136 } while (offset < len);
3137
0edd5b44 3138 /* Run the DMA and wait for the answer */
43f66a6c
JK
3139 rc = ipw_fw_dma_kick(priv);
3140 if (rc) {
3141 IPW_ERROR("dmaKick Failed\n");
3142 goto out;
3143 }
3144
3145 rc = ipw_fw_dma_wait(priv);
3146 if (rc) {
3147 IPW_ERROR("dmaWaitSync Failed\n");
3148 goto out;
3149 }
0edd5b44
JG
3150 out:
3151 pci_free_consistent(priv->pci_dev, len, shared_virt, shared_phys);
43f66a6c
JK
3152 return rc;
3153}
3154
3155/* stop nic */
3156static int ipw_stop_nic(struct ipw_priv *priv)
3157{
3158 int rc = 0;
3159
0edd5b44 3160 /* stop */
b095c381 3161 ipw_write32(priv, IPW_RESET_REG, IPW_RESET_REG_STOP_MASTER);
bf79451e 3162
b095c381
JK
3163 rc = ipw_poll_bit(priv, IPW_RESET_REG,
3164 IPW_RESET_REG_MASTER_DISABLED, 500);
43f66a6c 3165 if (rc < 0) {
c7b6a674 3166 IPW_ERROR("wait for reg master disabled failed after 500ms\n");
43f66a6c 3167 return rc;
bf79451e 3168 }
43f66a6c 3169
b095c381 3170 ipw_set_bit(priv, IPW_RESET_REG, CBD_RESET_REG_PRINCETON_RESET);
bf79451e 3171
43f66a6c
JK
3172 return rc;
3173}
3174
3175static void ipw_start_nic(struct ipw_priv *priv)
3176{
3177 IPW_DEBUG_TRACE(">>\n");
3178
0edd5b44 3179 /* prvHwStartNic release ARC */
b095c381
JK
3180 ipw_clear_bit(priv, IPW_RESET_REG,
3181 IPW_RESET_REG_MASTER_DISABLED |
3182 IPW_RESET_REG_STOP_MASTER |
43f66a6c 3183 CBD_RESET_REG_PRINCETON_RESET);
bf79451e 3184
43f66a6c 3185 /* enable power management */
b095c381
JK
3186 ipw_set_bit(priv, IPW_GP_CNTRL_RW,
3187 IPW_GP_CNTRL_BIT_HOST_ALLOWS_STANDBY);
43f66a6c
JK
3188
3189 IPW_DEBUG_TRACE("<<\n");
3190}
bf79451e 3191
43f66a6c
JK
3192static int ipw_init_nic(struct ipw_priv *priv)
3193{
3194 int rc;
3195
3196 IPW_DEBUG_TRACE(">>\n");
bf79451e 3197 /* reset */
43f66a6c
JK
3198 /*prvHwInitNic */
3199 /* set "initialization complete" bit to move adapter to D0 state */
b095c381 3200 ipw_set_bit(priv, IPW_GP_CNTRL_RW, IPW_GP_CNTRL_BIT_INIT_DONE);
43f66a6c
JK
3201
3202 /* low-level PLL activation */
b095c381
JK
3203 ipw_write32(priv, IPW_READ_INT_REGISTER,
3204 IPW_BIT_INT_HOST_SRAM_READ_INT_REGISTER);
43f66a6c
JK
3205
3206 /* wait for clock stabilization */
b095c381
JK
3207 rc = ipw_poll_bit(priv, IPW_GP_CNTRL_RW,
3208 IPW_GP_CNTRL_BIT_CLOCK_READY, 250);
0edd5b44 3209 if (rc < 0)
43f66a6c
JK
3210 IPW_DEBUG_INFO("FAILED wait for clock stablization\n");
3211
3212 /* assert SW reset */
b095c381 3213 ipw_set_bit(priv, IPW_RESET_REG, IPW_RESET_REG_SW_RESET);
43f66a6c
JK
3214
3215 udelay(10);
3216
3217 /* set "initialization complete" bit to move adapter to D0 state */
b095c381 3218 ipw_set_bit(priv, IPW_GP_CNTRL_RW, IPW_GP_CNTRL_BIT_INIT_DONE);
43f66a6c
JK
3219
3220 IPW_DEBUG_TRACE(">>\n");
3221 return 0;
3222}
3223
bf79451e 3224/* Call this function from process context, it will sleep in request_firmware.
43f66a6c
JK
3225 * Probe is an ok place to call this from.
3226 */
3227static int ipw_reset_nic(struct ipw_priv *priv)
3228{
3229 int rc = 0;
a613bffd 3230 unsigned long flags;
43f66a6c
JK
3231
3232 IPW_DEBUG_TRACE(">>\n");
bf79451e 3233
43f66a6c 3234 rc = ipw_init_nic(priv);
bf79451e 3235
a613bffd 3236 spin_lock_irqsave(&priv->lock, flags);
43f66a6c
JK
3237 /* Clear the 'host command active' bit... */
3238 priv->status &= ~STATUS_HCMD_ACTIVE;
3239 wake_up_interruptible(&priv->wait_command_queue);
afbf30a2
JK
3240 priv->status &= ~(STATUS_SCANNING | STATUS_SCAN_ABORTING);
3241 wake_up_interruptible(&priv->wait_state);
a613bffd 3242 spin_unlock_irqrestore(&priv->lock, flags);
43f66a6c
JK
3243
3244 IPW_DEBUG_TRACE("<<\n");
3245 return rc;
bf79451e 3246}
43f66a6c 3247
9006ea75
JK
3248
3249struct ipw_fw {
0070f8c7
ZY
3250 __le32 ver;
3251 __le32 boot_size;
3252 __le32 ucode_size;
3253 __le32 fw_size;
9006ea75
JK
3254 u8 data[0];
3255};
3256
bf79451e 3257static int ipw_get_fw(struct ipw_priv *priv,
9006ea75 3258 const struct firmware **raw, const char *name)
43f66a6c 3259{
9006ea75 3260 struct ipw_fw *fw;
43f66a6c
JK
3261 int rc;
3262
3263 /* ask firmware_class module to get the boot firmware off disk */
9006ea75 3264 rc = request_firmware(raw, name, &priv->pci_dev->dev);
43f66a6c 3265 if (rc < 0) {
9006ea75 3266 IPW_ERROR("%s request_firmware failed: Reason %d\n", name, rc);
43f66a6c 3267 return rc;
bf79451e 3268 }
43f66a6c 3269
9006ea75
JK
3270 if ((*raw)->size < sizeof(*fw)) {
3271 IPW_ERROR("%s is too small (%zd)\n", name, (*raw)->size);
3272 return -EINVAL;
3273 }
3274
3275 fw = (void *)(*raw)->data;
3276
0070f8c7
ZY
3277 if ((*raw)->size < sizeof(*fw) + le32_to_cpu(fw->boot_size) +
3278 le32_to_cpu(fw->ucode_size) + le32_to_cpu(fw->fw_size)) {
9006ea75
JK
3279 IPW_ERROR("%s is too small or corrupt (%zd)\n",
3280 name, (*raw)->size);
43f66a6c
JK
3281 return -EINVAL;
3282 }
3283
9006ea75 3284 IPW_DEBUG_INFO("Read firmware '%s' image v%d.%d (%zd bytes)\n",
43f66a6c 3285 name,
9006ea75
JK
3286 le32_to_cpu(fw->ver) >> 16,
3287 le32_to_cpu(fw->ver) & 0xff,
3288 (*raw)->size - sizeof(*fw));
43f66a6c
JK
3289 return 0;
3290}
3291
b095c381 3292#define IPW_RX_BUF_SIZE (3000)
43f66a6c 3293
858119e1 3294static void ipw_rx_queue_reset(struct ipw_priv *priv,
43f66a6c
JK
3295 struct ipw_rx_queue *rxq)
3296{
3297 unsigned long flags;
3298 int i;
3299
3300 spin_lock_irqsave(&rxq->lock, flags);
3301
3302 INIT_LIST_HEAD(&rxq->rx_free);
3303 INIT_LIST_HEAD(&rxq->rx_used);
3304
3305 /* Fill the rx_used queue with _all_ of the Rx buffers */
3306 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
3307 /* In the reset function, these buffers may have been allocated
3308 * to an SKB, so we need to unmap and free potential storage */
3309 if (rxq->pool[i].skb != NULL) {
3310 pci_unmap_single(priv->pci_dev, rxq->pool[i].dma_addr,
b095c381 3311 IPW_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
43f66a6c 3312 dev_kfree_skb(rxq->pool[i].skb);
a613bffd 3313 rxq->pool[i].skb = NULL;
43f66a6c
JK
3314 }
3315 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
3316 }
bf79451e 3317
43f66a6c
JK
3318 /* Set us so that we have processed and used all buffers, but have
3319 * not restocked the Rx queue with fresh buffers */
3320 rxq->read = rxq->write = 0;
3321 rxq->processed = RX_QUEUE_SIZE - 1;
3322 rxq->free_count = 0;
3323 spin_unlock_irqrestore(&rxq->lock, flags);
3324}
3325
3326#ifdef CONFIG_PM
3327static int fw_loaded = 0;
9006ea75 3328static const struct firmware *raw = NULL;
afbf30a2
JK
3329
3330static void free_firmware(void)
3331{
3332 if (fw_loaded) {
9006ea75
JK
3333 release_firmware(raw);
3334 raw = NULL;
afbf30a2
JK
3335 fw_loaded = 0;
3336 }
3337}
3338#else
3339#define free_firmware() do {} while (0)
43f66a6c
JK
3340#endif
3341
3342static int ipw_load(struct ipw_priv *priv)
3343{
3344#ifndef CONFIG_PM
9006ea75 3345 const struct firmware *raw = NULL;
43f66a6c 3346#endif
9006ea75
JK
3347 struct ipw_fw *fw;
3348 u8 *boot_img, *ucode_img, *fw_img;
3349 u8 *name = NULL;
43f66a6c
JK
3350 int rc = 0, retries = 3;
3351
397ae121
ZY
3352 switch (priv->ieee->iw_mode) {
3353 case IW_MODE_ADHOC:
9006ea75 3354 name = "ipw2200-ibss.fw";
397ae121 3355 break;
b095c381 3356#ifdef CONFIG_IPW2200_MONITOR
397ae121 3357 case IW_MODE_MONITOR:
9006ea75 3358 name = "ipw2200-sniffer.fw";
397ae121 3359 break;
43f66a6c 3360#endif
397ae121 3361 case IW_MODE_INFRA:
9006ea75 3362 name = "ipw2200-bss.fw";
397ae121 3363 break;
9006ea75
JK
3364 }
3365
3366 if (!name) {
397ae121 3367 rc = -EINVAL;
9006ea75
JK
3368 goto error;
3369 }
3370
3371#ifdef CONFIG_PM
3372 if (!fw_loaded) {
3373#endif
3374 rc = ipw_get_fw(priv, &raw, name);
3375 if (rc < 0)
3376 goto error;
3377#ifdef CONFIG_PM
43f66a6c 3378 }
9006ea75
JK
3379#endif
3380
3381 fw = (void *)raw->data;
3382 boot_img = &fw->data[0];
0070f8c7
ZY
3383 ucode_img = &fw->data[le32_to_cpu(fw->boot_size)];
3384 fw_img = &fw->data[le32_to_cpu(fw->boot_size) +
3385 le32_to_cpu(fw->ucode_size)];
397ae121
ZY
3386
3387 if (rc < 0)
3388 goto error;
43f66a6c
JK
3389
3390 if (!priv->rxq)
3391 priv->rxq = ipw_rx_queue_alloc(priv);
3392 else
3393 ipw_rx_queue_reset(priv, priv->rxq);
3394 if (!priv->rxq) {
3395 IPW_ERROR("Unable to initialize Rx queue\n");
3396 goto error;
3397 }
3398
0edd5b44 3399 retry:
43f66a6c 3400 /* Ensure interrupts are disabled */
b095c381 3401 ipw_write32(priv, IPW_INTA_MASK_R, ~IPW_INTA_MASK_ALL);
43f66a6c
JK
3402 priv->status &= ~STATUS_INT_ENABLED;
3403
3404 /* ack pending interrupts */
b095c381 3405 ipw_write32(priv, IPW_INTA_RW, IPW_INTA_MASK_ALL);
bf79451e 3406
43f66a6c
JK
3407 ipw_stop_nic(priv);
3408
3409 rc = ipw_reset_nic(priv);
397ae121 3410 if (rc < 0) {
43f66a6c
JK
3411 IPW_ERROR("Unable to reset NIC\n");
3412 goto error;
3413 }
3414
b095c381
JK
3415 ipw_zero_memory(priv, IPW_NIC_SRAM_LOWER_BOUND,
3416 IPW_NIC_SRAM_UPPER_BOUND - IPW_NIC_SRAM_LOWER_BOUND);
43f66a6c
JK
3417
3418 /* DMA the initial boot firmware into the device */
0070f8c7 3419 rc = ipw_load_firmware(priv, boot_img, le32_to_cpu(fw->boot_size));
43f66a6c 3420 if (rc < 0) {
a4f6bbb3 3421 IPW_ERROR("Unable to load boot firmware: %d\n", rc);
43f66a6c
JK
3422 goto error;
3423 }
3424
3425 /* kick start the device */
3426 ipw_start_nic(priv);
3427
c7b6a674 3428 /* wait for the device to finish its initial startup sequence */
b095c381
JK
3429 rc = ipw_poll_bit(priv, IPW_INTA_RW,
3430 IPW_INTA_BIT_FW_INITIALIZATION_DONE, 500);
43f66a6c
JK
3431 if (rc < 0) {
3432 IPW_ERROR("device failed to boot initial fw image\n");
3433 goto error;
3434 }
3435 IPW_DEBUG_INFO("initial device response after %dms\n", rc);
3436
bf79451e 3437 /* ack fw init done interrupt */
b095c381 3438 ipw_write32(priv, IPW_INTA_RW, IPW_INTA_BIT_FW_INITIALIZATION_DONE);
43f66a6c
JK
3439
3440 /* DMA the ucode into the device */
0070f8c7 3441 rc = ipw_load_ucode(priv, ucode_img, le32_to_cpu(fw->ucode_size));
43f66a6c 3442 if (rc < 0) {
a4f6bbb3 3443 IPW_ERROR("Unable to load ucode: %d\n", rc);
43f66a6c
JK
3444 goto error;
3445 }
bf79451e 3446
43f66a6c
JK
3447 /* stop nic */
3448 ipw_stop_nic(priv);
3449
3450 /* DMA bss firmware into the device */
0070f8c7 3451 rc = ipw_load_firmware(priv, fw_img, le32_to_cpu(fw->fw_size));
0edd5b44 3452 if (rc < 0) {
a4f6bbb3 3453 IPW_ERROR("Unable to load firmware: %d\n", rc);
43f66a6c
JK
3454 goto error;
3455 }
397ae121
ZY
3456#ifdef CONFIG_PM
3457 fw_loaded = 1;
3458#endif
3459
43f66a6c
JK
3460 ipw_write32(priv, IPW_EEPROM_LOAD_DISABLE, 0);
3461
3462 rc = ipw_queue_reset(priv);
397ae121 3463 if (rc < 0) {
43f66a6c
JK
3464 IPW_ERROR("Unable to initialize queues\n");
3465 goto error;
3466 }
3467
3468 /* Ensure interrupts are disabled */
b095c381 3469 ipw_write32(priv, IPW_INTA_MASK_R, ~IPW_INTA_MASK_ALL);
c848d0af 3470 /* ack pending interrupts */
b095c381 3471 ipw_write32(priv, IPW_INTA_RW, IPW_INTA_MASK_ALL);
bf79451e 3472
43f66a6c
JK
3473 /* kick start the device */
3474 ipw_start_nic(priv);
3475
b095c381 3476 if (ipw_read32(priv, IPW_INTA_RW) & IPW_INTA_BIT_PARITY_ERROR) {
43f66a6c
JK
3477 if (retries > 0) {
3478 IPW_WARNING("Parity error. Retrying init.\n");
3479 retries--;
3480 goto retry;
3481 }
3482
3483 IPW_ERROR("TODO: Handle parity error -- schedule restart?\n");
3484 rc = -EIO;
3485 goto error;
3486 }
3487
3488 /* wait for the device */
b095c381
JK
3489 rc = ipw_poll_bit(priv, IPW_INTA_RW,
3490 IPW_INTA_BIT_FW_INITIALIZATION_DONE, 500);
43f66a6c 3491 if (rc < 0) {
c7b6a674 3492 IPW_ERROR("device failed to start within 500ms\n");
43f66a6c
JK
3493 goto error;
3494 }
3495 IPW_DEBUG_INFO("device response after %dms\n", rc);
3496
3497 /* ack fw init done interrupt */
b095c381 3498 ipw_write32(priv, IPW_INTA_RW, IPW_INTA_BIT_FW_INITIALIZATION_DONE);
43f66a6c
JK
3499
3500 /* read eeprom data and initialize the eeprom region of sram */
3501 priv->eeprom_delay = 1;
bf79451e 3502 ipw_eeprom_init_sram(priv);
43f66a6c
JK
3503
3504 /* enable interrupts */
3505 ipw_enable_interrupts(priv);
3506
3507 /* Ensure our queue has valid packets */
3508 ipw_rx_queue_replenish(priv);
3509
b095c381 3510 ipw_write32(priv, IPW_RX_READ_INDEX, priv->rxq->read);
43f66a6c
JK
3511
3512 /* ack pending interrupts */
b095c381 3513 ipw_write32(priv, IPW_INTA_RW, IPW_INTA_MASK_ALL);
43f66a6c
JK
3514
3515#ifndef CONFIG_PM
9006ea75 3516 release_firmware(raw);
43f66a6c
JK
3517#endif
3518 return 0;
3519
0edd5b44 3520 error:
43f66a6c
JK
3521 if (priv->rxq) {
3522 ipw_rx_queue_free(priv, priv->rxq);
3523 priv->rxq = NULL;
3524 }
3525 ipw_tx_queue_free(priv);
9006ea75
JK
3526 if (raw)
3527 release_firmware(raw);
43f66a6c
JK
3528#ifdef CONFIG_PM
3529 fw_loaded = 0;
9006ea75 3530 raw = NULL;
43f66a6c
JK
3531#endif
3532
3533 return rc;
3534}
3535
bf79451e 3536/**
43f66a6c
JK
3537 * DMA services
3538 *
3539 * Theory of operation
3540 *
3541 * A queue is a circular buffers with 'Read' and 'Write' pointers.
3542 * 2 empty entries always kept in the buffer to protect from overflow.
3543 *
3544 * For Tx queue, there are low mark and high mark limits. If, after queuing
bf79451e
JG
3545 * the packet for Tx, free space become < low mark, Tx queue stopped. When
3546 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
43f66a6c
JK
3547 * Tx queue resumed.
3548 *
3549 * The IPW operates with six queues, one receive queue in the device's
3550 * sram, one transmit queue for sending commands to the device firmware,
bf79451e 3551 * and four transmit queues for data.
43f66a6c 3552 *
bf79451e 3553 * The four transmit queues allow for performing quality of service (qos)
43f66a6c 3554 * transmissions as per the 802.11 protocol. Currently Linux does not
bf79451e 3555 * provide a mechanism to the user for utilizing prioritized queues, so
43f66a6c
JK
3556 * we only utilize the first data transmit queue (queue1).
3557 */
3558
3559/**
3560 * Driver allocates buffers of this size for Rx
3561 */
3562
3563static inline int ipw_queue_space(const struct clx2_queue *q)
3564{
3565 int s = q->last_used - q->first_empty;
3566 if (s <= 0)
3567 s += q->n_bd;
3568 s -= 2; /* keep some reserve to not confuse empty and full situations */
3569 if (s < 0)
3570 s = 0;
3571 return s;
3572}
3573
3574static inline int ipw_queue_inc_wrap(int index, int n_bd)
3575{
3576 return (++index == n_bd) ? 0 : index;
3577}
3578
3579/**
3580 * Initialize common DMA queue structure
bf79451e 3581 *
43f66a6c
JK
3582 * @param q queue to init
3583 * @param count Number of BD's to allocate. Should be power of 2
3584 * @param read_register Address for 'read' register
3585 * (not offset within BAR, full address)
3586 * @param write_register Address for 'write' register
3587 * (not offset within BAR, full address)
3588 * @param base_register Address for 'base' register
3589 * (not offset within BAR, full address)
3590 * @param size Address for 'size' register
3591 * (not offset within BAR, full address)
3592 */
bf79451e 3593static void ipw_queue_init(struct ipw_priv *priv, struct clx2_queue *q,
0edd5b44 3594 int count, u32 read, u32 write, u32 base, u32 size)
43f66a6c
JK
3595{
3596 q->n_bd = count;
3597
3598 q->low_mark = q->n_bd / 4;
3599 if (q->low_mark < 4)
3600 q->low_mark = 4;
3601
3602 q->high_mark = q->n_bd / 8;
3603 if (q->high_mark < 2)
3604 q->high_mark = 2;
3605
3606 q->first_empty = q->last_used = 0;
3607 q->reg_r = read;
3608 q->reg_w = write;
3609
3610 ipw_write32(priv, base, q->dma_addr);
3611 ipw_write32(priv, size, count);
3612 ipw_write32(priv, read, 0);
3613 ipw_write32(priv, write, 0);
3614
3615 _ipw_read32(priv, 0x90);
3616}
3617
bf79451e 3618static int ipw_queue_tx_init(struct ipw_priv *priv,
43f66a6c 3619 struct clx2_tx_queue *q,
0edd5b44 3620 int count, u32 read, u32 write, u32 base, u32 size)
43f66a6c
JK
3621{
3622 struct pci_dev *dev = priv->pci_dev;
3623
3624 q->txb = kmalloc(sizeof(q->txb[0]) * count, GFP_KERNEL);
3625 if (!q->txb) {
3626 IPW_ERROR("vmalloc for auxilary BD structures failed\n");
3627 return -ENOMEM;
3628 }
3629
0edd5b44
JG
3630 q->bd =
3631 pci_alloc_consistent(dev, sizeof(q->bd[0]) * count, &q->q.dma_addr);
43f66a6c 3632 if (!q->bd) {
aaa4d308 3633 IPW_ERROR("pci_alloc_consistent(%zd) failed\n",
0edd5b44 3634 sizeof(q->bd[0]) * count);
43f66a6c
JK
3635 kfree(q->txb);
3636 q->txb = NULL;
3637 return -ENOMEM;
3638 }
3639
3640 ipw_queue_init(priv, &q->q, count, read, write, base, size);
3641 return 0;
3642}
3643
3644/**
3645 * Free one TFD, those at index [txq->q.last_used].
3646 * Do NOT advance any indexes
bf79451e 3647 *
43f66a6c
JK
3648 * @param dev
3649 * @param txq
3650 */
3651static void ipw_queue_tx_free_tfd(struct ipw_priv *priv,
3652 struct clx2_tx_queue *txq)
3653{
3654 struct tfd_frame *bd = &txq->bd[txq->q.last_used];
3655 struct pci_dev *dev = priv->pci_dev;
3656 int i;
bf79451e 3657
43f66a6c
JK
3658 /* classify bd */
3659 if (bd->control_flags.message_type == TX_HOST_COMMAND_TYPE)
3660 /* nothing to cleanup after for host commands */
3661 return;
3662
3663 /* sanity check */
a613bffd
JK
3664 if (le32_to_cpu(bd->u.data.num_chunks) > NUM_TFD_CHUNKS) {
3665 IPW_ERROR("Too many chunks: %i\n",
3666 le32_to_cpu(bd->u.data.num_chunks));
43f66a6c
JK
3667 /** @todo issue fatal error, it is quite serious situation */
3668 return;
3669 }
3670
3671 /* unmap chunks if any */
a613bffd
JK
3672 for (i = 0; i < le32_to_cpu(bd->u.data.num_chunks); i++) {
3673 pci_unmap_single(dev, le32_to_cpu(bd->u.data.chunk_ptr[i]),
3674 le16_to_cpu(bd->u.data.chunk_len[i]),
3675 PCI_DMA_TODEVICE);
43f66a6c
JK
3676 if (txq->txb[txq->q.last_used]) {
3677 ieee80211_txb_free(txq->txb[txq->q.last_used]);
3678 txq->txb[txq->q.last_used] = NULL;
3679 }
3680 }
3681}
3682
3683/**
3684 * Deallocate DMA queue.
bf79451e 3685 *
43f66a6c
JK
3686 * Empty queue by removing and destroying all BD's.
3687 * Free all buffers.
bf79451e 3688 *
43f66a6c
JK
3689 * @param dev
3690 * @param q
3691 */
0edd5b44 3692static void ipw_queue_tx_free(struct ipw_priv *priv, struct clx2_tx_queue *txq)
43f66a6c
JK
3693{
3694 struct clx2_queue *q = &txq->q;
3695 struct pci_dev *dev = priv->pci_dev;
3696
bf79451e
JG
3697 if (q->n_bd == 0)
3698 return;
43f66a6c
JK
3699
3700 /* first, empty all BD's */
3701 for (; q->first_empty != q->last_used;
3702 q->last_used = ipw_queue_inc_wrap(q->last_used, q->n_bd)) {
3703 ipw_queue_tx_free_tfd(priv, txq);
3704 }
bf79451e 3705
43f66a6c 3706 /* free buffers belonging to queue itself */
0edd5b44 3707 pci_free_consistent(dev, sizeof(txq->bd[0]) * q->n_bd, txq->bd,
43f66a6c
JK
3708 q->dma_addr);
3709 kfree(txq->txb);
3710
3711 /* 0 fill whole structure */
3712 memset(txq, 0, sizeof(*txq));
3713}
3714
43f66a6c
JK
3715/**
3716 * Destroy all DMA queues and structures
bf79451e 3717 *
43f66a6c
JK
3718 * @param priv
3719 */
3720static void ipw_tx_queue_free(struct ipw_priv *priv)
3721{
3722 /* Tx CMD queue */
3723 ipw_queue_tx_free(priv, &priv->txq_cmd);
3724
3725 /* Tx queues */
3726 ipw_queue_tx_free(priv, &priv->txq[0]);
3727 ipw_queue_tx_free(priv, &priv->txq[1]);
3728 ipw_queue_tx_free(priv, &priv->txq[2]);
3729 ipw_queue_tx_free(priv, &priv->txq[3]);
3730}
3731
858119e1 3732static void ipw_create_bssid(struct ipw_priv *priv, u8 * bssid)
43f66a6c
JK
3733{
3734 /* First 3 bytes are manufacturer */
3735 bssid[0] = priv->mac_addr[0];
3736 bssid[1] = priv->mac_addr[1];
3737 bssid[2] = priv->mac_addr[2];
3738
3739 /* Last bytes are random */
0edd5b44 3740 get_random_bytes(&bssid[3], ETH_ALEN - 3);
43f66a6c 3741
0edd5b44
JG
3742 bssid[0] &= 0xfe; /* clear multicast bit */
3743 bssid[0] |= 0x02; /* set local assignment bit (IEEE802) */
43f66a6c
JK
3744}
3745
858119e1 3746static u8 ipw_add_station(struct ipw_priv *priv, u8 * bssid)
43f66a6c
JK
3747{
3748 struct ipw_station_entry entry;
3749 int i;
3750
3751 for (i = 0; i < priv->num_stations; i++) {
3752 if (!memcmp(priv->stations[i], bssid, ETH_ALEN)) {
3753 /* Another node is active in network */
3754 priv->missed_adhoc_beacons = 0;
3755 if (!(priv->config & CFG_STATIC_CHANNEL))
3756 /* when other nodes drop out, we drop out */
3757 priv->config &= ~CFG_ADHOC_PERSIST;
3758
3759 return i;
3760 }
3761 }
3762
3763 if (i == MAX_STATIONS)
3764 return IPW_INVALID_STATION;
3765
3766 IPW_DEBUG_SCAN("Adding AdHoc station: " MAC_FMT "\n", MAC_ARG(bssid));
3767
3768 entry.reserved = 0;
3769 entry.support_mode = 0;
3770 memcpy(entry.mac_addr, bssid, ETH_ALEN);
3771 memcpy(priv->stations[i], bssid, ETH_ALEN);
3772 ipw_write_direct(priv, IPW_STATION_TABLE_LOWER + i * sizeof(entry),
0edd5b44 3773 &entry, sizeof(entry));
43f66a6c
JK
3774 priv->num_stations++;
3775
3776 return i;
3777}
3778
858119e1 3779static u8 ipw_find_station(struct ipw_priv *priv, u8 * bssid)
43f66a6c
JK
3780{
3781 int i;
3782
bf79451e
JG
3783 for (i = 0; i < priv->num_stations; i++)
3784 if (!memcmp(priv->stations[i], bssid, ETH_ALEN))
43f66a6c
JK
3785 return i;
3786
3787 return IPW_INVALID_STATION;
3788}
3789
3790static void ipw_send_disassociate(struct ipw_priv *priv, int quiet)
3791{
3792 int err;
3793
7b99659f
HL
3794 if (priv->status & STATUS_ASSOCIATING) {
3795 IPW_DEBUG_ASSOC("Disassociating while associating.\n");
3796 queue_work(priv->workqueue, &priv->disassociate);
3797 return;
3798 }
3799
3800 if (!(priv->status & STATUS_ASSOCIATED)) {
43f66a6c
JK
3801 IPW_DEBUG_ASSOC("Disassociating while not associated.\n");
3802 return;
3803 }
3804
3805 IPW_DEBUG_ASSOC("Disassocation attempt from " MAC_FMT " "
3806 "on channel %d.\n",
bf79451e 3807 MAC_ARG(priv->assoc_request.bssid),
43f66a6c
JK
3808 priv->assoc_request.channel);
3809
3810 priv->status &= ~(STATUS_ASSOCIATING | STATUS_ASSOCIATED);
3811 priv->status |= STATUS_DISASSOCIATING;
3812
3813 if (quiet)
3814 priv->assoc_request.assoc_type = HC_DISASSOC_QUIET;
3815 else
3816 priv->assoc_request.assoc_type = HC_DISASSOCIATE;
e6324726 3817
43f66a6c
JK
3818 err = ipw_send_associate(priv, &priv->assoc_request);
3819 if (err) {
3820 IPW_DEBUG_HC("Attempt to send [dis]associate command "
3821 "failed.\n");
3822 return;
3823 }
3824
3825}
3826
c848d0af 3827static int ipw_disassociate(void *data)
43f66a6c 3828{
c848d0af
JK
3829 struct ipw_priv *priv = data;
3830 if (!(priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)))
3831 return 0;
43f66a6c 3832 ipw_send_disassociate(data, 0);
c848d0af 3833 return 1;
43f66a6c
JK
3834}
3835
c848d0af 3836static void ipw_bg_disassociate(void *data)
43f66a6c 3837{
c848d0af 3838 struct ipw_priv *priv = data;
4644151b 3839 mutex_lock(&priv->mutex);
c848d0af 3840 ipw_disassociate(data);
4644151b 3841 mutex_unlock(&priv->mutex);
43f66a6c
JK
3842}
3843
d8bad6df
ZY
3844static void ipw_system_config(void *data)
3845{
3846 struct ipw_priv *priv = data;
d685b8c2
ZY
3847
3848#ifdef CONFIG_IPW2200_PROMISCUOUS
3849 if (priv->prom_net_dev && netif_running(priv->prom_net_dev)) {
3850 priv->sys_config.accept_all_data_frames = 1;
3851 priv->sys_config.accept_non_directed_frames = 1;
3852 priv->sys_config.accept_all_mgmt_bcpr = 1;
3853 priv->sys_config.accept_all_mgmt_frames = 1;
3854 }
3855#endif
3856
3857 ipw_send_system_config(priv);
43f66a6c
JK
3858}
3859
3860struct ipw_status_code {
3861 u16 status;
3862 const char *reason;
3863};
3864
3865static const struct ipw_status_code ipw_status_codes[] = {
3866 {0x00, "Successful"},
3867 {0x01, "Unspecified failure"},
3868 {0x0A, "Cannot support all requested capabilities in the "
3869 "Capability information field"},
3870 {0x0B, "Reassociation denied due to inability to confirm that "
3871 "association exists"},
3872 {0x0C, "Association denied due to reason outside the scope of this "
3873 "standard"},
0edd5b44
JG
3874 {0x0D,
3875 "Responding station does not support the specified authentication "
43f66a6c 3876 "algorithm"},
0edd5b44
JG
3877 {0x0E,
3878 "Received an Authentication frame with authentication sequence "
43f66a6c
JK
3879 "transaction sequence number out of expected sequence"},
3880 {0x0F, "Authentication rejected because of challenge failure"},
3881 {0x10, "Authentication rejected due to timeout waiting for next "
3882 "frame in sequence"},
3883 {0x11, "Association denied because AP is unable to handle additional "
3884 "associated stations"},
0edd5b44
JG
3885 {0x12,
3886 "Association denied due to requesting station not supporting all "
43f66a6c 3887 "of the datarates in the BSSBasicServiceSet Parameter"},
0edd5b44
JG
3888 {0x13,
3889 "Association denied due to requesting station not supporting "
43f66a6c 3890 "short preamble operation"},
0edd5b44
JG
3891 {0x14,
3892 "Association denied due to requesting station not supporting "
43f66a6c 3893 "PBCC encoding"},
0edd5b44
JG
3894 {0x15,
3895 "Association denied due to requesting station not supporting "
43f66a6c 3896 "channel agility"},
0edd5b44
JG
3897 {0x19,
3898 "Association denied due to requesting station not supporting "
43f66a6c 3899 "short slot operation"},
0edd5b44
JG
3900 {0x1A,
3901 "Association denied due to requesting station not supporting "
43f66a6c
JK
3902 "DSSS-OFDM operation"},
3903 {0x28, "Invalid Information Element"},
3904 {0x29, "Group Cipher is not valid"},
3905 {0x2A, "Pairwise Cipher is not valid"},
3906 {0x2B, "AKMP is not valid"},
3907 {0x2C, "Unsupported RSN IE version"},
3908 {0x2D, "Invalid RSN IE Capabilities"},
3909 {0x2E, "Cipher suite is rejected per security policy"},
3910};
3911
bf79451e 3912static const char *ipw_get_status_code(u16 status)
43f66a6c
JK
3913{
3914 int i;
bf79451e 3915 for (i = 0; i < ARRAY_SIZE(ipw_status_codes); i++)
ea2b26e0 3916 if (ipw_status_codes[i].status == (status & 0xff))
43f66a6c
JK
3917 return ipw_status_codes[i].reason;
3918 return "Unknown status value.";
3919}
43f66a6c
JK
3920
3921static void inline average_init(struct average *avg)
3922{
3923 memset(avg, 0, sizeof(*avg));
3924}
3925
00d21de5
ZY
3926#define DEPTH_RSSI 8
3927#define DEPTH_NOISE 16
3928static s16 exponential_average(s16 prev_avg, s16 val, u8 depth)
3929{
3930 return ((depth-1)*prev_avg + val)/depth;
3931}
3932
858119e1 3933static void average_add(struct average *avg, s16 val)
43f66a6c
JK
3934{
3935 avg->sum -= avg->entries[avg->pos];
3936 avg->sum += val;
3937 avg->entries[avg->pos++] = val;
3938 if (unlikely(avg->pos == AVG_ENTRIES)) {
3939 avg->init = 1;
3940 avg->pos = 0;
3941 }
3942}
3943
858119e1 3944static s16 average_value(struct average *avg)
43f66a6c
JK
3945{
3946 if (!unlikely(avg->init)) {
3947 if (avg->pos)
3948 return avg->sum / avg->pos;
3949 return 0;
3950 }
3951
3952 return avg->sum / AVG_ENTRIES;
3953}
3954
3955static void ipw_reset_stats(struct ipw_priv *priv)
3956{
3957 u32 len = sizeof(u32);
3958
3959 priv->quality = 0;
3960
3961 average_init(&priv->average_missed_beacons);
00d21de5
ZY
3962 priv->exp_avg_rssi = -60;
3963 priv->exp_avg_noise = -85 + 0x100;
43f66a6c
JK
3964
3965 priv->last_rate = 0;
3966 priv->last_missed_beacons = 0;
3967 priv->last_rx_packets = 0;
3968 priv->last_tx_packets = 0;
3969 priv->last_tx_failures = 0;
bf79451e 3970
43f66a6c
JK
3971 /* Firmware managed, reset only when NIC is restarted, so we have to
3972 * normalize on the current value */
bf79451e 3973 ipw_get_ordinal(priv, IPW_ORD_STAT_RX_ERR_CRC,
43f66a6c 3974 &priv->last_rx_err, &len);
bf79451e 3975 ipw_get_ordinal(priv, IPW_ORD_STAT_TX_FAILURE,
43f66a6c
JK
3976 &priv->last_tx_failures, &len);
3977
3978 /* Driver managed, reset with each association */
3979 priv->missed_adhoc_beacons = 0;
3980 priv->missed_beacons = 0;
3981 priv->tx_packets = 0;
3982 priv->rx_packets = 0;
3983
3984}
3985
858119e1 3986static u32 ipw_get_max_rate(struct ipw_priv *priv)
43f66a6c
JK
3987{
3988 u32 i = 0x80000000;
3989 u32 mask = priv->rates_mask;
3990 /* If currently associated in B mode, restrict the maximum
3991 * rate match to B rates */
3992 if (priv->assoc_request.ieee_mode == IPW_B_MODE)
3993 mask &= IEEE80211_CCK_RATES_MASK;
3994
3995 /* TODO: Verify that the rate is supported by the current rates
3996 * list. */
3997
0edd5b44
JG
3998 while (i && !(mask & i))
3999 i >>= 1;
43f66a6c 4000 switch (i) {
ea2b26e0
JK
4001 case IEEE80211_CCK_RATE_1MB_MASK:
4002 return 1000000;
4003 case IEEE80211_CCK_RATE_2MB_MASK:
4004 return 2000000;
4005 case IEEE80211_CCK_RATE_5MB_MASK:
4006 return 5500000;
4007 case IEEE80211_OFDM_RATE_6MB_MASK:
4008 return 6000000;
4009 case IEEE80211_OFDM_RATE_9MB_MASK:
4010 return 9000000;
4011 case IEEE80211_CCK_RATE_11MB_MASK:
4012 return 11000000;
4013 case IEEE80211_OFDM_RATE_12MB_MASK:
4014 return 12000000;
4015 case IEEE80211_OFDM_RATE_18MB_MASK:
4016 return 18000000;
4017 case IEEE80211_OFDM_RATE_24MB_MASK:
4018 return 24000000;
4019 case IEEE80211_OFDM_RATE_36MB_MASK:
4020 return 36000000;
4021 case IEEE80211_OFDM_RATE_48MB_MASK:
4022 return 48000000;
4023 case IEEE80211_OFDM_RATE_54MB_MASK:
4024 return 54000000;
43f66a6c
JK
4025 }
4026
bf79451e 4027 if (priv->ieee->mode == IEEE_B)
43f66a6c
JK
4028 return 11000000;
4029 else
4030 return 54000000;
4031}
4032
4033static u32 ipw_get_current_rate(struct ipw_priv *priv)
4034{
4035 u32 rate, len = sizeof(rate);
4036 int err;
4037
bf79451e 4038 if (!(priv->status & STATUS_ASSOCIATED))
43f66a6c
JK
4039 return 0;
4040
4041 if (priv->tx_packets > IPW_REAL_RATE_RX_PACKET_THRESHOLD) {
bf79451e 4042 err = ipw_get_ordinal(priv, IPW_ORD_STAT_TX_CURR_RATE, &rate,
43f66a6c
JK
4043 &len);
4044 if (err) {
4045 IPW_DEBUG_INFO("failed querying ordinals.\n");
4046 return 0;
4047 }
bf79451e 4048 } else
43f66a6c
JK
4049 return ipw_get_max_rate(priv);
4050
4051 switch (rate) {
ea2b26e0
JK
4052 case IPW_TX_RATE_1MB:
4053 return 1000000;
4054 case IPW_TX_RATE_2MB:
4055 return 2000000;
4056 case IPW_TX_RATE_5MB:
4057 return 5500000;
4058 case IPW_TX_RATE_6MB:
4059 return 6000000;
4060 case IPW_TX_RATE_9MB:
4061 return 9000000;
4062 case IPW_TX_RATE_11MB:
4063 return 11000000;
4064 case IPW_TX_RATE_12MB:
4065 return 12000000;
4066 case IPW_TX_RATE_18MB:
4067 return 18000000;
4068 case IPW_TX_RATE_24MB:
4069 return 24000000;
4070 case IPW_TX_RATE_36MB:
4071 return 36000000;
4072 case IPW_TX_RATE_48MB:
4073 return 48000000;
4074 case IPW_TX_RATE_54MB:
4075 return 54000000;
43f66a6c
JK
4076 }
4077
4078 return 0;
4079}
4080
43f66a6c
JK
4081#define IPW_STATS_INTERVAL (2 * HZ)
4082static void ipw_gather_stats(struct ipw_priv *priv)
4083{
4084 u32 rx_err, rx_err_delta, rx_packets_delta;
4085 u32 tx_failures, tx_failures_delta, tx_packets_delta;
4086 u32 missed_beacons_percent, missed_beacons_delta;
4087 u32 quality = 0;
4088 u32 len = sizeof(u32);
4089 s16 rssi;
bf79451e 4090 u32 beacon_quality, signal_quality, tx_quality, rx_quality,
0edd5b44 4091 rate_quality;
ea2b26e0 4092 u32 max_rate;
43f66a6c
JK
4093
4094 if (!(priv->status & STATUS_ASSOCIATED)) {
4095 priv->quality = 0;
4096 return;
4097 }
4098
4099 /* Update the statistics */
bf79451e 4100 ipw_get_ordinal(priv, IPW_ORD_STAT_MISSED_BEACONS,
43f66a6c 4101 &priv->missed_beacons, &len);
0edd5b44 4102 missed_beacons_delta = priv->missed_beacons - priv->last_missed_beacons;
43f66a6c
JK
4103 priv->last_missed_beacons = priv->missed_beacons;
4104 if (priv->assoc_request.beacon_interval) {
4105 missed_beacons_percent = missed_beacons_delta *
0edd5b44
JG
4106 (HZ * priv->assoc_request.beacon_interval) /
4107 (IPW_STATS_INTERVAL * 10);
43f66a6c
JK
4108 } else {
4109 missed_beacons_percent = 0;
4110 }
4111 average_add(&priv->average_missed_beacons, missed_beacons_percent);
4112
4113 ipw_get_ordinal(priv, IPW_ORD_STAT_RX_ERR_CRC, &rx_err, &len);
4114 rx_err_delta = rx_err - priv->last_rx_err;
4115 priv->last_rx_err = rx_err;
4116
4117 ipw_get_ordinal(priv, IPW_ORD_STAT_TX_FAILURE, &tx_failures, &len);
4118 tx_failures_delta = tx_failures - priv->last_tx_failures;
4119 priv->last_tx_failures = tx_failures;
4120
4121 rx_packets_delta = priv->rx_packets - priv->last_rx_packets;
4122 priv->last_rx_packets = priv->rx_packets;
4123
4124 tx_packets_delta = priv->tx_packets - priv->last_tx_packets;
4125 priv->last_tx_packets = priv->tx_packets;
4126
4127 /* Calculate quality based on the following:
bf79451e 4128 *
43f66a6c
JK
4129 * Missed beacon: 100% = 0, 0% = 70% missed
4130 * Rate: 60% = 1Mbs, 100% = Max
4131 * Rx and Tx errors represent a straight % of total Rx/Tx
4132 * RSSI: 100% = > -50, 0% = < -80
4133 * Rx errors: 100% = 0, 0% = 50% missed
bf79451e 4134 *
43f66a6c
JK
4135 * The lowest computed quality is used.
4136 *
4137 */
4138#define BEACON_THRESHOLD 5
4139 beacon_quality = 100 - missed_beacons_percent;
4140 if (beacon_quality < BEACON_THRESHOLD)
4141 beacon_quality = 0;
4142 else
bf79451e 4143 beacon_quality = (beacon_quality - BEACON_THRESHOLD) * 100 /
0edd5b44 4144 (100 - BEACON_THRESHOLD);
bf79451e 4145 IPW_DEBUG_STATS("Missed beacon: %3d%% (%d%%)\n",
43f66a6c 4146 beacon_quality, missed_beacons_percent);
bf79451e 4147
43f66a6c 4148 priv->last_rate = ipw_get_current_rate(priv);
ea2b26e0
JK
4149 max_rate = ipw_get_max_rate(priv);
4150 rate_quality = priv->last_rate * 40 / max_rate + 60;
43f66a6c
JK
4151 IPW_DEBUG_STATS("Rate quality : %3d%% (%dMbs)\n",
4152 rate_quality, priv->last_rate / 1000000);
bf79451e 4153
0edd5b44 4154 if (rx_packets_delta > 100 && rx_packets_delta + rx_err_delta)
bf79451e 4155 rx_quality = 100 - (rx_err_delta * 100) /
0edd5b44 4156 (rx_packets_delta + rx_err_delta);
43f66a6c
JK
4157 else
4158 rx_quality = 100;
4159 IPW_DEBUG_STATS("Rx quality : %3d%% (%u errors, %u packets)\n",
4160 rx_quality, rx_err_delta, rx_packets_delta);
bf79451e 4161
0edd5b44 4162 if (tx_packets_delta > 100 && tx_packets_delta + tx_failures_delta)
bf79451e 4163 tx_quality = 100 - (tx_failures_delta * 100) /
0edd5b44 4164 (tx_packets_delta + tx_failures_delta);
43f66a6c
JK
4165 else
4166 tx_quality = 100;
4167 IPW_DEBUG_STATS("Tx quality : %3d%% (%u errors, %u packets)\n",
4168 tx_quality, tx_failures_delta, tx_packets_delta);
bf79451e 4169
00d21de5 4170 rssi = priv->exp_avg_rssi;
c848d0af
JK
4171 signal_quality =
4172 (100 *
4173 (priv->ieee->perfect_rssi - priv->ieee->worst_rssi) *
4174 (priv->ieee->perfect_rssi - priv->ieee->worst_rssi) -
4175 (priv->ieee->perfect_rssi - rssi) *
4176 (15 * (priv->ieee->perfect_rssi - priv->ieee->worst_rssi) +
4177 62 * (priv->ieee->perfect_rssi - rssi))) /
4178 ((priv->ieee->perfect_rssi - priv->ieee->worst_rssi) *
4179 (priv->ieee->perfect_rssi - priv->ieee->worst_rssi));
4180 if (signal_quality > 100)
43f66a6c 4181 signal_quality = 100;
c848d0af 4182 else if (signal_quality < 1)
43f66a6c 4183 signal_quality = 0;
ea2b26e0 4184
61fb9ed9 4185 IPW_DEBUG_STATS("Signal level : %3d%% (%d dBm)\n",
43f66a6c 4186 signal_quality, rssi);
bf79451e
JG
4187
4188 quality = min(beacon_quality,
43f66a6c
JK
4189 min(rate_quality,
4190 min(tx_quality, min(rx_quality, signal_quality))));
4191 if (quality == beacon_quality)
0edd5b44
JG
4192 IPW_DEBUG_STATS("Quality (%d%%): Clamped to missed beacons.\n",
4193 quality);
43f66a6c 4194 if (quality == rate_quality)
0edd5b44
JG
4195 IPW_DEBUG_STATS("Quality (%d%%): Clamped to rate quality.\n",
4196 quality);
43f66a6c 4197 if (quality == tx_quality)
0edd5b44
JG
4198 IPW_DEBUG_STATS("Quality (%d%%): Clamped to Tx quality.\n",
4199 quality);
43f66a6c 4200 if (quality == rx_quality)
0edd5b44
JG
4201 IPW_DEBUG_STATS("Quality (%d%%): Clamped to Rx quality.\n",
4202 quality);
43f66a6c 4203 if (quality == signal_quality)
0edd5b44
JG
4204 IPW_DEBUG_STATS("Quality (%d%%): Clamped to signal quality.\n",
4205 quality);
43f66a6c
JK
4206
4207 priv->quality = quality;
bf79451e
JG
4208
4209 queue_delayed_work(priv->workqueue, &priv->gather_stats,
43f66a6c
JK
4210 IPW_STATS_INTERVAL);
4211}
4212
c848d0af
JK
4213static void ipw_bg_gather_stats(void *data)
4214{
4215 struct ipw_priv *priv = data;
4644151b 4216 mutex_lock(&priv->mutex);
c848d0af 4217 ipw_gather_stats(data);
4644151b 4218 mutex_unlock(&priv->mutex);
c848d0af
JK
4219}
4220
e7582561
BC
4221/* Missed beacon behavior:
4222 * 1st missed -> roaming_threshold, just wait, don't do any scan/roam.
4223 * roaming_threshold -> disassociate_threshold, scan and roam for better signal.
4224 * Above disassociate threshold, give up and stop scanning.
4225 * Roaming is disabled if disassociate_threshold <= roaming_threshold */
858119e1 4226static void ipw_handle_missed_beacon(struct ipw_priv *priv,
ea2b26e0
JK
4227 int missed_count)
4228{
4229 priv->notif_missed_beacons = missed_count;
4230
afbf30a2 4231 if (missed_count > priv->disassociate_threshold &&
ea2b26e0
JK
4232 priv->status & STATUS_ASSOCIATED) {
4233 /* If associated and we've hit the missed
4234 * beacon threshold, disassociate, turn
4235 * off roaming, and abort any active scans */
4236 IPW_DEBUG(IPW_DL_INFO | IPW_DL_NOTIF |
afbf30a2 4237 IPW_DL_STATE | IPW_DL_ASSOC,
ea2b26e0
JK
4238 "Missed beacon: %d - disassociate\n", missed_count);
4239 priv->status &= ~STATUS_ROAMING;
a613bffd
JK
4240 if (priv->status & STATUS_SCANNING) {
4241 IPW_DEBUG(IPW_DL_INFO | IPW_DL_NOTIF |
4242 IPW_DL_STATE,
4243 "Aborting scan with missed beacon.\n");
ea2b26e0 4244 queue_work(priv->workqueue, &priv->abort_scan);
a613bffd
JK
4245 }
4246
ea2b26e0
JK
4247 queue_work(priv->workqueue, &priv->disassociate);
4248 return;
4249 }
4250
4251 if (priv->status & STATUS_ROAMING) {
4252 /* If we are currently roaming, then just
4253 * print a debug statement... */
4254 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE,
4255 "Missed beacon: %d - roam in progress\n",
4256 missed_count);
4257 return;
4258 }
4259
4bfdb91d
ZY
4260 if (roaming &&
4261 (missed_count > priv->roaming_threshold &&
4262 missed_count <= priv->disassociate_threshold)) {
ea2b26e0 4263 /* If we are not already roaming, set the ROAM
e7582561
BC
4264 * bit in the status and kick off a scan.
4265 * This can happen several times before we reach
4266 * disassociate_threshold. */
ea2b26e0
JK
4267 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE,
4268 "Missed beacon: %d - initiate "
4269 "roaming\n", missed_count);
4270 if (!(priv->status & STATUS_ROAMING)) {
4271 priv->status |= STATUS_ROAMING;
4272 if (!(priv->status & STATUS_SCANNING))
4273 queue_work(priv->workqueue,
4274 &priv->request_scan);
4275 }
4276 return;
4277 }
4278
4279 if (priv->status & STATUS_SCANNING) {
4280 /* Stop scan to keep fw from getting
4281 * stuck (only if we aren't roaming --
4282 * otherwise we'll never scan more than 2 or 3
4283 * channels..) */
b095c381
JK
4284 IPW_DEBUG(IPW_DL_INFO | IPW_DL_NOTIF | IPW_DL_STATE,
4285 "Aborting scan with missed beacon.\n");
ea2b26e0
JK
4286 queue_work(priv->workqueue, &priv->abort_scan);
4287 }
4288
4289 IPW_DEBUG_NOTIF("Missed beacon: %d\n", missed_count);
ea2b26e0
JK
4290}
4291
43f66a6c
JK
4292/**
4293 * Handle host notification packet.
4294 * Called from interrupt routine
4295 */
858119e1 4296static void ipw_rx_notification(struct ipw_priv *priv,
43f66a6c
JK
4297 struct ipw_rx_notification *notif)
4298{
a613bffd
JK
4299 notif->size = le16_to_cpu(notif->size);
4300
0edd5b44 4301 IPW_DEBUG_NOTIF("type = %i (%d bytes)\n", notif->subtype, notif->size);
bf79451e 4302
43f66a6c 4303 switch (notif->subtype) {
0edd5b44
JG
4304 case HOST_NOTIFICATION_STATUS_ASSOCIATED:{
4305 struct notif_association *assoc = &notif->u.assoc;
4306
4307 switch (assoc->state) {
4308 case CMAS_ASSOCIATED:{
4309 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4310 IPW_DL_ASSOC,
4311 "associated: '%s' " MAC_FMT
4312 " \n",
4313 escape_essid(priv->essid,
4314 priv->essid_len),
4315 MAC_ARG(priv->bssid));
4316
4317 switch (priv->ieee->iw_mode) {
4318 case IW_MODE_INFRA:
4319 memcpy(priv->ieee->bssid,
4320 priv->bssid, ETH_ALEN);
4321 break;
4322
4323 case IW_MODE_ADHOC:
4324 memcpy(priv->ieee->bssid,
4325 priv->bssid, ETH_ALEN);
4326
4327 /* clear out the station table */
4328 priv->num_stations = 0;
4329
4330 IPW_DEBUG_ASSOC
4331 ("queueing adhoc check\n");
4332 queue_delayed_work(priv->
4333 workqueue,
4334 &priv->
4335 adhoc_check,
4336 priv->
4337 assoc_request.
4338 beacon_interval);
4339 break;
4340 }
4341
4342 priv->status &= ~STATUS_ASSOCIATING;
4343 priv->status |= STATUS_ASSOCIATED;
d8bad6df
ZY
4344 queue_work(priv->workqueue,
4345 &priv->system_config);
0edd5b44 4346
e43e3c1e 4347#ifdef CONFIG_IPW2200_QOS
afbf30a2
JK
4348#define IPW_GET_PACKET_STYPE(x) WLAN_FC_GET_STYPE( \
4349 le16_to_cpu(((struct ieee80211_hdr *)(x))->frame_ctl))
4350 if ((priv->status & STATUS_AUTH) &&
4351 (IPW_GET_PACKET_STYPE(&notif->u.raw)
4352 == IEEE80211_STYPE_ASSOC_RESP)) {
b095c381
JK
4353 if ((sizeof
4354 (struct
2b184d5b 4355 ieee80211_assoc_response)
b095c381
JK
4356 <= notif->size)
4357 && (notif->size <= 2314)) {
4358 struct
4359 ieee80211_rx_stats
4360 stats = {
4361 .len =
4362 notif->
4363 size - 1,
4364 };
4365
4366 IPW_DEBUG_QOS
4367 ("QoS Associate "
4368 "size %d\n",
4369 notif->size);
4370 ieee80211_rx_mgt(priv->
4371 ieee,
4372 (struct
2b184d5b 4373 ieee80211_hdr_4addr
b095c381
JK
4374 *)
4375 &notif->u.raw, &stats);
4376 }
0edd5b44 4377 }
b095c381 4378#endif
0edd5b44 4379
a613bffd 4380 schedule_work(&priv->link_up);
43f66a6c 4381
0edd5b44
JG
4382 break;
4383 }
bf79451e 4384
0edd5b44
JG
4385 case CMAS_AUTHENTICATED:{
4386 if (priv->
4387 status & (STATUS_ASSOCIATED |
4388 STATUS_AUTH)) {
0edd5b44
JG
4389 struct notif_authenticate *auth
4390 = &notif->u.auth;
4391 IPW_DEBUG(IPW_DL_NOTIF |
4392 IPW_DL_STATE |
4393 IPW_DL_ASSOC,
4394 "deauthenticated: '%s' "
4395 MAC_FMT
4396 ": (0x%04X) - %s \n",
4397 escape_essid(priv->
4398 essid,
4399 priv->
4400 essid_len),
4401 MAC_ARG(priv->bssid),
4402 ntohs(auth->status),
4403 ipw_get_status_code
4404 (ntohs
4405 (auth->status)));
43f66a6c 4406
0edd5b44
JG
4407 priv->status &=
4408 ~(STATUS_ASSOCIATING |
4409 STATUS_AUTH |
4410 STATUS_ASSOCIATED);
4411
a613bffd 4412 schedule_work(&priv->link_down);
0edd5b44
JG
4413 break;
4414 }
4415
4416 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4417 IPW_DL_ASSOC,
4418 "authenticated: '%s' " MAC_FMT
4419 "\n",
4420 escape_essid(priv->essid,
4421 priv->essid_len),
4422 MAC_ARG(priv->bssid));
4423 break;
4424 }
4425
4426 case CMAS_INIT:{
ea2b26e0
JK
4427 if (priv->status & STATUS_AUTH) {
4428 struct
4429 ieee80211_assoc_response
4430 *resp;
4431 resp =
4432 (struct
4433 ieee80211_assoc_response
4434 *)&notif->u.raw;
4435 IPW_DEBUG(IPW_DL_NOTIF |
4436 IPW_DL_STATE |
4437 IPW_DL_ASSOC,
4438 "association failed (0x%04X): %s\n",
4439 ntohs(resp->status),
4440 ipw_get_status_code
4441 (ntohs
4442 (resp->status)));
4443 }
4444
0edd5b44
JG
4445 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4446 IPW_DL_ASSOC,
4447 "disassociated: '%s' " MAC_FMT
4448 " \n",
4449 escape_essid(priv->essid,
4450 priv->essid_len),
4451 MAC_ARG(priv->bssid));
4452
4453 priv->status &=
4454 ~(STATUS_DISASSOCIATING |
4455 STATUS_ASSOCIATING |
4456 STATUS_ASSOCIATED | STATUS_AUTH);
b095c381
JK
4457 if (priv->assoc_network
4458 && (priv->assoc_network->
4459 capability &
4460 WLAN_CAPABILITY_IBSS))
4461 ipw_remove_current_network
4462 (priv);
0edd5b44 4463
a613bffd 4464 schedule_work(&priv->link_down);
0edd5b44 4465
0edd5b44
JG
4466 break;
4467 }
43f66a6c 4468
b095c381
JK
4469 case CMAS_RX_ASSOC_RESP:
4470 break;
4471
0edd5b44
JG
4472 default:
4473 IPW_ERROR("assoc: unknown (%d)\n",
4474 assoc->state);
43f66a6c 4475 break;
bf79451e 4476 }
43f66a6c 4477
43f66a6c
JK
4478 break;
4479 }
bf79451e 4480
0edd5b44
JG
4481 case HOST_NOTIFICATION_STATUS_AUTHENTICATE:{
4482 struct notif_authenticate *auth = &notif->u.auth;
4483 switch (auth->state) {
4484 case CMAS_AUTHENTICATED:
4485 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE,
4486 "authenticated: '%s' " MAC_FMT " \n",
4487 escape_essid(priv->essid,
4488 priv->essid_len),
4489 MAC_ARG(priv->bssid));
4490 priv->status |= STATUS_AUTH;
4491 break;
43f66a6c 4492
0edd5b44
JG
4493 case CMAS_INIT:
4494 if (priv->status & STATUS_AUTH) {
4495 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4496 IPW_DL_ASSOC,
4497 "authentication failed (0x%04X): %s\n",
4498 ntohs(auth->status),
4499 ipw_get_status_code(ntohs
4500 (auth->
4501 status)));
4502 }
4503 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4504 IPW_DL_ASSOC,
4505 "deauthenticated: '%s' " MAC_FMT "\n",
4506 escape_essid(priv->essid,
4507 priv->essid_len),
4508 MAC_ARG(priv->bssid));
bf79451e 4509
0edd5b44
JG
4510 priv->status &= ~(STATUS_ASSOCIATING |
4511 STATUS_AUTH |
4512 STATUS_ASSOCIATED);
43f66a6c 4513
a613bffd 4514 schedule_work(&priv->link_down);
0edd5b44 4515 break;
43f66a6c 4516
0edd5b44
JG
4517 case CMAS_TX_AUTH_SEQ_1:
4518 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4519 IPW_DL_ASSOC, "AUTH_SEQ_1\n");
4520 break;
4521 case CMAS_RX_AUTH_SEQ_2:
4522 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4523 IPW_DL_ASSOC, "AUTH_SEQ_2\n");
4524 break;
4525 case CMAS_AUTH_SEQ_1_PASS:
4526 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4527 IPW_DL_ASSOC, "AUTH_SEQ_1_PASS\n");
4528 break;
4529 case CMAS_AUTH_SEQ_1_FAIL:
4530 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4531 IPW_DL_ASSOC, "AUTH_SEQ_1_FAIL\n");
4532 break;
4533 case CMAS_TX_AUTH_SEQ_3:
4534 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4535 IPW_DL_ASSOC, "AUTH_SEQ_3\n");
4536 break;
4537 case CMAS_RX_AUTH_SEQ_4:
4538 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4539 IPW_DL_ASSOC, "RX_AUTH_SEQ_4\n");
4540 break;
4541 case CMAS_AUTH_SEQ_2_PASS:
4542 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4543 IPW_DL_ASSOC, "AUTH_SEQ_2_PASS\n");
4544 break;
4545 case CMAS_AUTH_SEQ_2_FAIL:
4546 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4547 IPW_DL_ASSOC, "AUT_SEQ_2_FAIL\n");
4548 break;
4549 case CMAS_TX_ASSOC:
4550 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4551 IPW_DL_ASSOC, "TX_ASSOC\n");
4552 break;
4553 case CMAS_RX_ASSOC_RESP:
4554 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4555 IPW_DL_ASSOC, "RX_ASSOC_RESP\n");
b095c381 4556
0edd5b44
JG
4557 break;
4558 case CMAS_ASSOCIATED:
4559 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE |
4560 IPW_DL_ASSOC, "ASSOCIATED\n");
4561 break;
4562 default:
4563 IPW_DEBUG_NOTIF("auth: failure - %d\n",
4564 auth->state);
4565 break;
43f66a6c 4566 }
43f66a6c
JK
4567 break;
4568 }
4569
0edd5b44
JG
4570 case HOST_NOTIFICATION_STATUS_SCAN_CHANNEL_RESULT:{
4571 struct notif_channel_result *x =
4572 &notif->u.channel_result;
43f66a6c 4573
0edd5b44
JG
4574 if (notif->size == sizeof(*x)) {
4575 IPW_DEBUG_SCAN("Scan result for channel %d\n",
4576 x->channel_num);
4577 } else {
4578 IPW_DEBUG_SCAN("Scan result of wrong size %d "
4579 "(should be %zd)\n",
4580 notif->size, sizeof(*x));
bf79451e 4581 }
43f66a6c
JK
4582 break;
4583 }
43f66a6c 4584
0edd5b44
JG
4585 case HOST_NOTIFICATION_STATUS_SCAN_COMPLETED:{
4586 struct notif_scan_complete *x = &notif->u.scan_complete;
4587 if (notif->size == sizeof(*x)) {
4588 IPW_DEBUG_SCAN
4589 ("Scan completed: type %d, %d channels, "
4590 "%d status\n", x->scan_type,
4591 x->num_channels, x->status);
4592 } else {
4593 IPW_ERROR("Scan completed of wrong size %d "
4594 "(should be %zd)\n",
4595 notif->size, sizeof(*x));
4596 }
43f66a6c 4597
0edd5b44
JG
4598 priv->status &=
4599 ~(STATUS_SCANNING | STATUS_SCAN_ABORTING);
4600
a0e04ab3 4601 wake_up_interruptible(&priv->wait_state);
0edd5b44
JG
4602 cancel_delayed_work(&priv->scan_check);
4603
b095c381
JK
4604 if (priv->status & STATUS_EXIT_PENDING)
4605 break;
4606
4607 priv->ieee->scans++;
4608
4609#ifdef CONFIG_IPW2200_MONITOR
4610 if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
afbf30a2 4611 priv->status |= STATUS_SCAN_FORCED;
b095c381
JK
4612 queue_work(priv->workqueue,
4613 &priv->request_scan);
4614 break;
4615 }
afbf30a2 4616 priv->status &= ~STATUS_SCAN_FORCED;
b095c381
JK
4617#endif /* CONFIG_IPW2200_MONITOR */
4618
0edd5b44
JG
4619 if (!(priv->status & (STATUS_ASSOCIATED |
4620 STATUS_ASSOCIATING |
4621 STATUS_ROAMING |
4622 STATUS_DISASSOCIATING)))
4623 queue_work(priv->workqueue, &priv->associate);
4624 else if (priv->status & STATUS_ROAMING) {
e7582561
BC
4625 if (x->status == SCAN_COMPLETED_STATUS_COMPLETE)
4626 /* If a scan completed and we are in roam mode, then
4627 * the scan that completed was the one requested as a
4628 * result of entering roam... so, schedule the
4629 * roam work */
4630 queue_work(priv->workqueue,
4631 &priv->roam);
4632 else
4633 /* Don't schedule if we aborted the scan */
4634 priv->status &= ~STATUS_ROAMING;
0edd5b44
JG
4635 } else if (priv->status & STATUS_SCAN_PENDING)
4636 queue_work(priv->workqueue,
4637 &priv->request_scan);
a613bffd
JK
4638 else if (priv->config & CFG_BACKGROUND_SCAN
4639 && priv->status & STATUS_ASSOCIATED)
4640 queue_delayed_work(priv->workqueue,
4641 &priv->request_scan, HZ);
07f02e46
ZY
4642
4643 /* Send an empty event to user space.
4644 * We don't send the received data on the event because
4645 * it would require us to do complex transcoding, and
4646 * we want to minimise the work done in the irq handler
4647 * Use a request to extract the data.
4648 * Also, we generate this even for any scan, regardless
4649 * on how the scan was initiated. User space can just
4650 * sync on periodic scan to get fresh data...
4651 * Jean II */
4652 if (x->status == SCAN_COMPLETED_STATUS_COMPLETE) {
4653 union iwreq_data wrqu;
4654
4655 wrqu.data.length = 0;
4656 wrqu.data.flags = 0;
4657 wireless_send_event(priv->net_dev, SIOCGIWSCAN,
4658 &wrqu, NULL);
4659 }
0edd5b44 4660 break;
43f66a6c 4661 }
43f66a6c 4662
0edd5b44
JG
4663 case HOST_NOTIFICATION_STATUS_FRAG_LENGTH:{
4664 struct notif_frag_length *x = &notif->u.frag_len;
43f66a6c 4665
a613bffd
JK
4666 if (notif->size == sizeof(*x))
4667 IPW_ERROR("Frag length: %d\n",
4668 le16_to_cpu(x->frag_length));
4669 else
0edd5b44
JG
4670 IPW_ERROR("Frag length of wrong size %d "
4671 "(should be %zd)\n",
4672 notif->size, sizeof(*x));
0edd5b44 4673 break;
43f66a6c 4674 }
43f66a6c 4675
0edd5b44
JG
4676 case HOST_NOTIFICATION_STATUS_LINK_DETERIORATION:{
4677 struct notif_link_deterioration *x =
4678 &notif->u.link_deterioration;
afbf30a2 4679
0edd5b44
JG
4680 if (notif->size == sizeof(*x)) {
4681 IPW_DEBUG(IPW_DL_NOTIF | IPW_DL_STATE,
12977154
BC
4682 "link deterioration: type %d, cnt %d\n",
4683 x->silence_notification_type,
4684 x->silence_count);
0edd5b44
JG
4685 memcpy(&priv->last_link_deterioration, x,
4686 sizeof(*x));
4687 } else {
4688 IPW_ERROR("Link Deterioration of wrong size %d "
4689 "(should be %zd)\n",
4690 notif->size, sizeof(*x));
4691 }
43f66a6c
JK
4692 break;
4693 }
4694
0edd5b44
JG
4695 case HOST_NOTIFICATION_DINO_CONFIG_RESPONSE:{
4696 IPW_ERROR("Dino config\n");
4697 if (priv->hcmd
a613bffd 4698 && priv->hcmd->cmd != HOST_CMD_DINO_CONFIG)
0edd5b44 4699 IPW_ERROR("Unexpected DINO_CONFIG_RESPONSE\n");
a613bffd 4700
0edd5b44
JG
4701 break;
4702 }
43f66a6c 4703
0edd5b44
JG
4704 case HOST_NOTIFICATION_STATUS_BEACON_STATE:{
4705 struct notif_beacon_state *x = &notif->u.beacon_state;
4706 if (notif->size != sizeof(*x)) {
4707 IPW_ERROR
4708 ("Beacon state of wrong size %d (should "
4709 "be %zd)\n", notif->size, sizeof(*x));
4710 break;
43f66a6c
JK
4711 }
4712
a613bffd
JK
4713 if (le32_to_cpu(x->state) ==
4714 HOST_NOTIFICATION_STATUS_BEACON_MISSING)
4715 ipw_handle_missed_beacon(priv,
4716 le32_to_cpu(x->
4717 number));
43f66a6c 4718
0edd5b44
JG
4719 break;
4720 }
43f66a6c 4721
0edd5b44
JG
4722 case HOST_NOTIFICATION_STATUS_TGI_TX_KEY:{
4723 struct notif_tgi_tx_key *x = &notif->u.tgi_tx_key;
4724 if (notif->size == sizeof(*x)) {
4725 IPW_ERROR("TGi Tx Key: state 0x%02x sec type "
4726 "0x%02x station %d\n",
4727 x->key_state, x->security_type,
4728 x->station_index);
4729 break;
4730 }
43f66a6c 4731
0edd5b44
JG
4732 IPW_ERROR
4733 ("TGi Tx Key of wrong size %d (should be %zd)\n",
4734 notif->size, sizeof(*x));
43f66a6c 4735 break;
bf79451e 4736 }
43f66a6c 4737
0edd5b44
JG
4738 case HOST_NOTIFICATION_CALIB_KEEP_RESULTS:{
4739 struct notif_calibration *x = &notif->u.calibration;
43f66a6c 4740
0edd5b44
JG
4741 if (notif->size == sizeof(*x)) {
4742 memcpy(&priv->calib, x, sizeof(*x));
4743 IPW_DEBUG_INFO("TODO: Calibration\n");
4744 break;
4745 }
43f66a6c 4746
0edd5b44
JG
4747 IPW_ERROR
4748 ("Calibration of wrong size %d (should be %zd)\n",
4749 notif->size, sizeof(*x));
43f66a6c 4750 break;
bf79451e
JG
4751 }
4752
0edd5b44
JG
4753 case HOST_NOTIFICATION_NOISE_STATS:{
4754 if (notif->size == sizeof(u32)) {
00d21de5
ZY
4755 priv->exp_avg_noise =
4756 exponential_average(priv->exp_avg_noise,
4757 (u8) (le32_to_cpu(notif->u.noise.value) & 0xff),
4758 DEPTH_NOISE);
0edd5b44
JG
4759 break;
4760 }
43f66a6c 4761
0edd5b44
JG
4762 IPW_ERROR
4763 ("Noise stat is wrong size %d (should be %zd)\n",
4764 notif->size, sizeof(u32));
43f66a6c
JK
4765 break;
4766 }
4767
43f66a6c 4768 default:
1dd31b6c
ZY
4769 IPW_DEBUG_NOTIF("Unknown notification: "
4770 "subtype=%d,flags=0x%2x,size=%d\n",
4771 notif->subtype, notif->flags, notif->size);
43f66a6c
JK
4772 }
4773}
4774
4775/**
4776 * Destroys all DMA structures and initialise them again
bf79451e 4777 *
43f66a6c
JK
4778 * @param priv
4779 * @return error code
4780 */
4781static int ipw_queue_reset(struct ipw_priv *priv)
4782{
4783 int rc = 0;
4784 /** @todo customize queue sizes */
4785 int nTx = 64, nTxCmd = 8;
4786 ipw_tx_queue_free(priv);
4787 /* Tx CMD queue */
4788 rc = ipw_queue_tx_init(priv, &priv->txq_cmd, nTxCmd,
b095c381
JK
4789 IPW_TX_CMD_QUEUE_READ_INDEX,
4790 IPW_TX_CMD_QUEUE_WRITE_INDEX,
4791 IPW_TX_CMD_QUEUE_BD_BASE,
4792 IPW_TX_CMD_QUEUE_BD_SIZE);
43f66a6c
JK
4793 if (rc) {
4794 IPW_ERROR("Tx Cmd queue init failed\n");
4795 goto error;
4796 }
4797 /* Tx queue(s) */
4798 rc = ipw_queue_tx_init(priv, &priv->txq[0], nTx,
b095c381
JK
4799 IPW_TX_QUEUE_0_READ_INDEX,
4800 IPW_TX_QUEUE_0_WRITE_INDEX,
4801 IPW_TX_QUEUE_0_BD_BASE, IPW_TX_QUEUE_0_BD_SIZE);
43f66a6c
JK
4802 if (rc) {
4803 IPW_ERROR("Tx 0 queue init failed\n");
4804 goto error;
4805 }
4806 rc = ipw_queue_tx_init(priv, &priv->txq[1], nTx,
b095c381
JK
4807 IPW_TX_QUEUE_1_READ_INDEX,
4808 IPW_TX_QUEUE_1_WRITE_INDEX,
4809 IPW_TX_QUEUE_1_BD_BASE, IPW_TX_QUEUE_1_BD_SIZE);
43f66a6c
JK
4810 if (rc) {
4811 IPW_ERROR("Tx 1 queue init failed\n");
4812 goto error;
4813 }
4814 rc = ipw_queue_tx_init(priv, &priv->txq[2], nTx,
b095c381
JK
4815 IPW_TX_QUEUE_2_READ_INDEX,
4816 IPW_TX_QUEUE_2_WRITE_INDEX,
4817 IPW_TX_QUEUE_2_BD_BASE, IPW_TX_QUEUE_2_BD_SIZE);
43f66a6c
JK
4818 if (rc) {
4819 IPW_ERROR("Tx 2 queue init failed\n");
4820 goto error;
4821 }
4822 rc = ipw_queue_tx_init(priv, &priv->txq[3], nTx,
b095c381
JK
4823 IPW_TX_QUEUE_3_READ_INDEX,
4824 IPW_TX_QUEUE_3_WRITE_INDEX,
4825 IPW_TX_QUEUE_3_BD_BASE, IPW_TX_QUEUE_3_BD_SIZE);
43f66a6c
JK
4826 if (rc) {
4827 IPW_ERROR("Tx 3 queue init failed\n");
4828 goto error;
4829 }
4830 /* statistics */
4831 priv->rx_bufs_min = 0;
4832 priv->rx_pend_max = 0;
4833 return rc;
4834
0edd5b44 4835 error:
43f66a6c
JK
4836 ipw_tx_queue_free(priv);
4837 return rc;
4838}
4839
4840/**
4841 * Reclaim Tx queue entries no more used by NIC.
bf79451e 4842 *
43f66a6c
JK
4843 * When FW adwances 'R' index, all entries between old and
4844 * new 'R' index need to be reclaimed. As result, some free space
4845 * forms. If there is enough free space (> low mark), wake Tx queue.
bf79451e 4846 *
43f66a6c
JK
4847 * @note Need to protect against garbage in 'R' index
4848 * @param priv
4849 * @param txq
4850 * @param qindex
4851 * @return Number of used entries remains in the queue
4852 */
bf79451e 4853static int ipw_queue_tx_reclaim(struct ipw_priv *priv,
43f66a6c
JK
4854 struct clx2_tx_queue *txq, int qindex)
4855{
4856 u32 hw_tail;
4857 int used;
4858 struct clx2_queue *q = &txq->q;
4859
4860 hw_tail = ipw_read32(priv, q->reg_r);
4861 if (hw_tail >= q->n_bd) {
4862 IPW_ERROR
0edd5b44
JG
4863 ("Read index for DMA queue (%d) is out of range [0-%d)\n",
4864 hw_tail, q->n_bd);
43f66a6c
JK
4865 goto done;
4866 }
4867 for (; q->last_used != hw_tail;
4868 q->last_used = ipw_queue_inc_wrap(q->last_used, q->n_bd)) {
4869 ipw_queue_tx_free_tfd(priv, txq);
4870 priv->tx_packets++;
4871 }
0edd5b44 4872 done:
9ddf84f6
JK
4873 if ((ipw_queue_space(q) > q->low_mark) &&
4874 (qindex >= 0) &&
4875 (priv->status & STATUS_ASSOCIATED) && netif_running(priv->net_dev))
4876 netif_wake_queue(priv->net_dev);
43f66a6c
JK
4877 used = q->first_empty - q->last_used;
4878 if (used < 0)
4879 used += q->n_bd;
4880
4881 return used;
4882}
4883
4884static int ipw_queue_tx_hcmd(struct ipw_priv *priv, int hcmd, void *buf,
4885 int len, int sync)
4886{
4887 struct clx2_tx_queue *txq = &priv->txq_cmd;
4888 struct clx2_queue *q = &txq->q;
4889 struct tfd_frame *tfd;
4890
4891 if (ipw_queue_space(q) < (sync ? 1 : 2)) {
4892 IPW_ERROR("No space for Tx\n");
4893 return -EBUSY;
4894 }
4895
4896 tfd = &txq->bd[q->first_empty];
4897 txq->txb[q->first_empty] = NULL;
4898
4899 memset(tfd, 0, sizeof(*tfd));
4900 tfd->control_flags.message_type = TX_HOST_COMMAND_TYPE;
4901 tfd->control_flags.control_bits = TFD_NEED_IRQ_MASK;
4902 priv->hcmd_seq++;
4903 tfd->u.cmd.index = hcmd;
4904 tfd->u.cmd.length = len;
4905 memcpy(tfd->u.cmd.payload, buf, len);
4906 q->first_empty = ipw_queue_inc_wrap(q->first_empty, q->n_bd);
4907 ipw_write32(priv, q->reg_w, q->first_empty);
4908 _ipw_read32(priv, 0x90);
4909
4910 return 0;
4911}
4912
bf79451e 4913/*
43f66a6c
JK
4914 * Rx theory of operation
4915 *
4916 * The host allocates 32 DMA target addresses and passes the host address
b095c381 4917 * to the firmware at register IPW_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
43f66a6c
JK
4918 * 0 to 31
4919 *
4920 * Rx Queue Indexes
4921 * The host/firmware share two index registers for managing the Rx buffers.
4922 *
bf79451e
JG
4923 * The READ index maps to the first position that the firmware may be writing
4924 * to -- the driver can read up to (but not including) this position and get
4925 * good data.
43f66a6c
JK
4926 * The READ index is managed by the firmware once the card is enabled.
4927 *
4928 * The WRITE index maps to the last position the driver has read from -- the
4929 * position preceding WRITE is the last slot the firmware can place a packet.
4930 *
4931 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
bf79451e 4932 * WRITE = READ.
43f66a6c 4933 *
bf79451e 4934 * During initialization the host sets up the READ queue position to the first
43f66a6c
JK
4935 * INDEX position, and WRITE to the last (READ - 1 wrapped)
4936 *
4937 * When the firmware places a packet in a buffer it will advance the READ index
4938 * and fire the RX interrupt. The driver can then query the READ index and
4939 * process as many packets as possible, moving the WRITE index forward as it
4940 * resets the Rx queue buffers with new memory.
bf79451e 4941 *
43f66a6c 4942 * The management in the driver is as follows:
bf79451e 4943 * + A list of pre-allocated SKBs is stored in ipw->rxq->rx_free. When
43f66a6c 4944 * ipw->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
bf79451e 4945 * to replensish the ipw->rxq->rx_free.
43f66a6c
JK
4946 * + In ipw_rx_queue_replenish (scheduled) if 'processed' != 'read' then the
4947 * ipw->rxq is replenished and the READ INDEX is updated (updating the
4948 * 'processed' and 'read' driver indexes as well)
4949 * + A received packet is processed and handed to the kernel network stack,
4950 * detached from the ipw->rxq. The driver 'processed' index is updated.
4951 * + The Host/Firmware ipw->rxq is replenished at tasklet time from the rx_free
bf79451e
JG
4952 * list. If there are no allocated buffers in ipw->rxq->rx_free, the READ
4953 * INDEX is not incremented and ipw->status(RX_STALLED) is set. If there
43f66a6c
JK
4954 * were enough free buffers and RX_STALLED is set it is cleared.
4955 *
4956 *
4957 * Driver sequence:
4958 *
bf79451e 4959 * ipw_rx_queue_alloc() Allocates rx_free
43f66a6c
JK
4960 * ipw_rx_queue_replenish() Replenishes rx_free list from rx_used, and calls
4961 * ipw_rx_queue_restock
4962 * ipw_rx_queue_restock() Moves available buffers from rx_free into Rx
4963 * queue, updates firmware pointers, and updates
4964 * the WRITE index. If insufficient rx_free buffers
4965 * are available, schedules ipw_rx_queue_replenish
4966 *
4967 * -- enable interrupts --
4968 * ISR - ipw_rx() Detach ipw_rx_mem_buffers from pool up to the
bf79451e 4969 * READ INDEX, detaching the SKB from the pool.
43f66a6c
JK
4970 * Moves the packet buffer from queue to rx_used.
4971 * Calls ipw_rx_queue_restock to refill any empty
4972 * slots.
4973 * ...
4974 *
4975 */
4976
bf79451e 4977/*
43f66a6c
JK
4978 * If there are slots in the RX queue that need to be restocked,
4979 * and we have free pre-allocated buffers, fill the ranks as much
4980 * as we can pulling from rx_free.
4981 *
4982 * This moves the 'write' index forward to catch up with 'processed', and
4983 * also updates the memory address in the firmware to reference the new
4984 * target buffer.
4985 */
4986static void ipw_rx_queue_restock(struct ipw_priv *priv)
4987{
4988 struct ipw_rx_queue *rxq = priv->rxq;
4989 struct list_head *element;
4990 struct ipw_rx_mem_buffer *rxb;
4991 unsigned long flags;
4992 int write;
4993
4994 spin_lock_irqsave(&rxq->lock, flags);
4995 write = rxq->write;
4996 while ((rxq->write != rxq->processed) && (rxq->free_count)) {
4997 element = rxq->rx_free.next;
4998 rxb = list_entry(element, struct ipw_rx_mem_buffer, list);
4999 list_del(element);
5000
b095c381 5001 ipw_write32(priv, IPW_RFDS_TABLE_LOWER + rxq->write * RFD_SIZE,
43f66a6c
JK
5002 rxb->dma_addr);
5003 rxq->queue[rxq->write] = rxb;
5004 rxq->write = (rxq->write + 1) % RX_QUEUE_SIZE;
5005 rxq->free_count--;
5006 }
5007 spin_unlock_irqrestore(&rxq->lock, flags);
5008
bf79451e 5009 /* If the pre-allocated buffer pool is dropping low, schedule to
43f66a6c
JK
5010 * refill it */
5011 if (rxq->free_count <= RX_LOW_WATERMARK)
5012 queue_work(priv->workqueue, &priv->rx_replenish);
5013
5014 /* If we've added more space for the firmware to place data, tell it */
bf79451e 5015 if (write != rxq->write)
b095c381 5016 ipw_write32(priv, IPW_RX_WRITE_INDEX, rxq->write);
43f66a6c
JK
5017}
5018
5019/*
5020 * Move all used packet from rx_used to rx_free, allocating a new SKB for each.
bf79451e
JG
5021 * Also restock the Rx queue via ipw_rx_queue_restock.
5022 *
43f66a6c
JK
5023 * This is called as a scheduled work item (except for during intialization)
5024 */
5025static void ipw_rx_queue_replenish(void *data)
5026{
5027 struct ipw_priv *priv = data;
5028 struct ipw_rx_queue *rxq = priv->rxq;
5029 struct list_head *element;
5030 struct ipw_rx_mem_buffer *rxb;
5031 unsigned long flags;
5032
5033 spin_lock_irqsave(&rxq->lock, flags);
5034 while (!list_empty(&rxq->rx_used)) {
5035 element = rxq->rx_used.next;
5036 rxb = list_entry(element, struct ipw_rx_mem_buffer, list);
b095c381 5037 rxb->skb = alloc_skb(IPW_RX_BUF_SIZE, GFP_ATOMIC);
43f66a6c
JK
5038 if (!rxb->skb) {
5039 printk(KERN_CRIT "%s: Can not allocate SKB buffers.\n",
5040 priv->net_dev->name);
5041 /* We don't reschedule replenish work here -- we will
5042 * call the restock method and if it still needs
5043 * more buffers it will schedule replenish */
5044 break;
5045 }
5046 list_del(element);
bf79451e 5047
0edd5b44
JG
5048 rxb->dma_addr =
5049 pci_map_single(priv->pci_dev, rxb->skb->data,
b095c381 5050 IPW_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
bf79451e 5051
43f66a6c
JK
5052 list_add_tail(&rxb->list, &rxq->rx_free);
5053 rxq->free_count++;
5054 }
5055 spin_unlock_irqrestore(&rxq->lock, flags);
5056
5057 ipw_rx_queue_restock(priv);
5058}
5059
c848d0af
JK
5060static void ipw_bg_rx_queue_replenish(void *data)
5061{
5062 struct ipw_priv *priv = data;
4644151b 5063 mutex_lock(&priv->mutex);
c848d0af 5064 ipw_rx_queue_replenish(data);
4644151b 5065 mutex_unlock(&priv->mutex);
c848d0af
JK
5066}
5067
43f66a6c 5068/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
c7b6a674 5069 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
bf79451e 5070 * This free routine walks the list of POOL entries and if SKB is set to
43f66a6c
JK
5071 * non NULL it is unmapped and freed
5072 */
0edd5b44 5073static void ipw_rx_queue_free(struct ipw_priv *priv, struct ipw_rx_queue *rxq)
43f66a6c
JK
5074{
5075 int i;
5076
5077 if (!rxq)
5078 return;
bf79451e 5079
43f66a6c
JK
5080 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
5081 if (rxq->pool[i].skb != NULL) {
5082 pci_unmap_single(priv->pci_dev, rxq->pool[i].dma_addr,
b095c381 5083 IPW_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
43f66a6c
JK
5084 dev_kfree_skb(rxq->pool[i].skb);
5085 }
5086 }
5087
5088 kfree(rxq);
5089}
5090
5091static struct ipw_rx_queue *ipw_rx_queue_alloc(struct ipw_priv *priv)
5092{
5093 struct ipw_rx_queue *rxq;
5094 int i;
5095
c75f4742 5096 rxq = kzalloc(sizeof(*rxq), GFP_KERNEL);
ad18b0ea
PI
5097 if (unlikely(!rxq)) {
5098 IPW_ERROR("memory allocation failed\n");
5099 return NULL;
5100 }
43f66a6c
JK
5101 spin_lock_init(&rxq->lock);
5102 INIT_LIST_HEAD(&rxq->rx_free);
5103 INIT_LIST_HEAD(&rxq->rx_used);
5104
5105 /* Fill the rx_used queue with _all_ of the Rx buffers */
bf79451e 5106 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
43f66a6c
JK
5107 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
5108
5109 /* Set us so that we have processed and used all buffers, but have
5110 * not restocked the Rx queue with fresh buffers */
5111 rxq->read = rxq->write = 0;
5112 rxq->processed = RX_QUEUE_SIZE - 1;
5113 rxq->free_count = 0;
5114
5115 return rxq;
5116}
5117
5118static int ipw_is_rate_in_mask(struct ipw_priv *priv, int ieee_mode, u8 rate)
5119{
5120 rate &= ~IEEE80211_BASIC_RATE_MASK;
5121 if (ieee_mode == IEEE_A) {
5122 switch (rate) {
bf79451e
JG
5123 case IEEE80211_OFDM_RATE_6MB:
5124 return priv->rates_mask & IEEE80211_OFDM_RATE_6MB_MASK ?
0edd5b44 5125 1 : 0;
bf79451e
JG
5126 case IEEE80211_OFDM_RATE_9MB:
5127 return priv->rates_mask & IEEE80211_OFDM_RATE_9MB_MASK ?
0edd5b44 5128 1 : 0;
bf79451e 5129 case IEEE80211_OFDM_RATE_12MB:
0edd5b44
JG
5130 return priv->
5131 rates_mask & IEEE80211_OFDM_RATE_12MB_MASK ? 1 : 0;
bf79451e 5132 case IEEE80211_OFDM_RATE_18MB:
0edd5b44
JG
5133 return priv->
5134 rates_mask & IEEE80211_OFDM_RATE_18MB_MASK ? 1 : 0;
bf79451e 5135 case IEEE80211_OFDM_RATE_24MB:
0edd5b44
JG
5136 return priv->
5137 rates_mask & IEEE80211_OFDM_RATE_24MB_MASK ? 1 : 0;
bf79451e 5138 case IEEE80211_OFDM_RATE_36MB:
0edd5b44
JG
5139 return priv->
5140 rates_mask & IEEE80211_OFDM_RATE_36MB_MASK ? 1 : 0;
bf79451e 5141 case IEEE80211_OFDM_RATE_48MB:
0edd5b44
JG
5142 return priv->
5143 rates_mask & IEEE80211_OFDM_RATE_48MB_MASK ? 1 : 0;
bf79451e 5144 case IEEE80211_OFDM_RATE_54MB:
0edd5b44
JG
5145 return priv->
5146 rates_mask & IEEE80211_OFDM_RATE_54MB_MASK ? 1 : 0;
43f66a6c
JK
5147 default:
5148 return 0;
5149 }
5150 }
bf79451e 5151
43f66a6c
JK
5152 /* B and G mixed */
5153 switch (rate) {
bf79451e 5154 case IEEE80211_CCK_RATE_1MB:
43f66a6c 5155 return priv->rates_mask & IEEE80211_CCK_RATE_1MB_MASK ? 1 : 0;
bf79451e 5156 case IEEE80211_CCK_RATE_2MB:
43f66a6c 5157 return priv->rates_mask & IEEE80211_CCK_RATE_2MB_MASK ? 1 : 0;
bf79451e 5158 case IEEE80211_CCK_RATE_5MB:
43f66a6c 5159 return priv->rates_mask & IEEE80211_CCK_RATE_5MB_MASK ? 1 : 0;
bf79451e 5160 case IEEE80211_CCK_RATE_11MB:
43f66a6c
JK
5161 return priv->rates_mask & IEEE80211_CCK_RATE_11MB_MASK ? 1 : 0;
5162 }
5163
5164 /* If we are limited to B modulations, bail at this point */
5165 if (ieee_mode == IEEE_B)
5166 return 0;
5167
5168 /* G */
5169 switch (rate) {
bf79451e 5170 case IEEE80211_OFDM_RATE_6MB:
43f66a6c 5171 return priv->rates_mask & IEEE80211_OFDM_RATE_6MB_MASK ? 1 : 0;
bf79451e 5172 case IEEE80211_OFDM_RATE_9MB:
43f66a6c 5173 return priv->rates_mask & IEEE80211_OFDM_RATE_9MB_MASK ? 1 : 0;
bf79451e 5174 case IEEE80211_OFDM_RATE_12MB:
43f66a6c 5175 return priv->rates_mask & IEEE80211_OFDM_RATE_12MB_MASK ? 1 : 0;
bf79451e 5176 case IEEE80211_OFDM_RATE_18MB:
43f66a6c 5177 return priv->rates_mask & IEEE80211_OFDM_RATE_18MB_MASK ? 1 : 0;
bf79451e 5178 case IEEE80211_OFDM_RATE_24MB:
43f66a6c 5179 return priv->rates_mask & IEEE80211_OFDM_RATE_24MB_MASK ? 1 : 0;
bf79451e 5180 case IEEE80211_OFDM_RATE_36MB:
43f66a6c 5181 return priv->rates_mask & IEEE80211_OFDM_RATE_36MB_MASK ? 1 : 0;
bf79451e 5182 case IEEE80211_OFDM_RATE_48MB:
43f66a6c 5183 return priv->rates_mask & IEEE80211_OFDM_RATE_48MB_MASK ? 1 : 0;
bf79451e 5184 case IEEE80211_OFDM_RATE_54MB:
43f66a6c
JK
5185 return priv->rates_mask & IEEE80211_OFDM_RATE_54MB_MASK ? 1 : 0;
5186 }
5187
5188 return 0;
5189}
5190
bf79451e 5191static int ipw_compatible_rates(struct ipw_priv *priv,
43f66a6c
JK
5192 const struct ieee80211_network *network,
5193 struct ipw_supported_rates *rates)
5194{
5195 int num_rates, i;
5196
5197 memset(rates, 0, sizeof(*rates));
0edd5b44 5198 num_rates = min(network->rates_len, (u8) IPW_MAX_RATES);
43f66a6c
JK
5199 rates->num_rates = 0;
5200 for (i = 0; i < num_rates; i++) {
a613bffd
JK
5201 if (!ipw_is_rate_in_mask(priv, network->mode,
5202 network->rates[i])) {
5203
ea2b26e0 5204 if (network->rates[i] & IEEE80211_BASIC_RATE_MASK) {
a613bffd
JK
5205 IPW_DEBUG_SCAN("Adding masked mandatory "
5206 "rate %02X\n",
5207 network->rates[i]);
5208 rates->supported_rates[rates->num_rates++] =
5209 network->rates[i];
5210 continue;
ea2b26e0
JK
5211 }
5212
43f66a6c
JK
5213 IPW_DEBUG_SCAN("Rate %02X masked : 0x%08X\n",
5214 network->rates[i], priv->rates_mask);
5215 continue;
5216 }
bf79451e 5217
43f66a6c
JK
5218 rates->supported_rates[rates->num_rates++] = network->rates[i];
5219 }
5220
a613bffd
JK
5221 num_rates = min(network->rates_ex_len,
5222 (u8) (IPW_MAX_RATES - num_rates));
43f66a6c 5223 for (i = 0; i < num_rates; i++) {
a613bffd
JK
5224 if (!ipw_is_rate_in_mask(priv, network->mode,
5225 network->rates_ex[i])) {
ea2b26e0 5226 if (network->rates_ex[i] & IEEE80211_BASIC_RATE_MASK) {
a613bffd
JK
5227 IPW_DEBUG_SCAN("Adding masked mandatory "
5228 "rate %02X\n",
5229 network->rates_ex[i]);
5230 rates->supported_rates[rates->num_rates++] =
5231 network->rates[i];
5232 continue;
ea2b26e0
JK
5233 }
5234
43f66a6c
JK
5235 IPW_DEBUG_SCAN("Rate %02X masked : 0x%08X\n",
5236 network->rates_ex[i], priv->rates_mask);
5237 continue;
5238 }
bf79451e 5239
0edd5b44
JG
5240 rates->supported_rates[rates->num_rates++] =
5241 network->rates_ex[i];
43f66a6c
JK
5242 }
5243
ea2b26e0 5244 return 1;
43f66a6c
JK
5245}
5246
858119e1 5247static void ipw_copy_rates(struct ipw_supported_rates *dest,
43f66a6c
JK
5248 const struct ipw_supported_rates *src)
5249{
5250 u8 i;
5251 for (i = 0; i < src->num_rates; i++)
5252 dest->supported_rates[i] = src->supported_rates[i];
5253 dest->num_rates = src->num_rates;
5254}
5255
5256/* TODO: Look at sniffed packets in the air to determine if the basic rate
5257 * mask should ever be used -- right now all callers to add the scan rates are
5258 * set with the modulation = CCK, so BASIC_RATE_MASK is never set... */
5259static void ipw_add_cck_scan_rates(struct ipw_supported_rates *rates,
0edd5b44 5260 u8 modulation, u32 rate_mask)
43f66a6c 5261{
bf79451e 5262 u8 basic_mask = (IEEE80211_OFDM_MODULATION == modulation) ?
0edd5b44 5263 IEEE80211_BASIC_RATE_MASK : 0;
bf79451e 5264
43f66a6c 5265 if (rate_mask & IEEE80211_CCK_RATE_1MB_MASK)
bf79451e 5266 rates->supported_rates[rates->num_rates++] =
0edd5b44 5267 IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
43f66a6c
JK
5268
5269 if (rate_mask & IEEE80211_CCK_RATE_2MB_MASK)
bf79451e 5270 rates->supported_rates[rates->num_rates++] =
0edd5b44 5271 IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
43f66a6c
JK
5272
5273 if (rate_mask & IEEE80211_CCK_RATE_5MB_MASK)
bf79451e 5274 rates->supported_rates[rates->num_rates++] = basic_mask |
0edd5b44 5275 IEEE80211_CCK_RATE_5MB;
43f66a6c
JK
5276
5277 if (rate_mask & IEEE80211_CCK_RATE_11MB_MASK)
bf79451e 5278 rates->supported_rates[rates->num_rates++] = basic_mask |
0edd5b44 5279 IEEE80211_CCK_RATE_11MB;
43f66a6c
JK
5280}
5281
5282static void ipw_add_ofdm_scan_rates(struct ipw_supported_rates *rates,
0edd5b44 5283 u8 modulation, u32 rate_mask)
43f66a6c 5284{
bf79451e 5285 u8 basic_mask = (IEEE80211_OFDM_MODULATION == modulation) ?
0edd5b44 5286 IEEE80211_BASIC_RATE_MASK : 0;
43f66a6c
JK
5287
5288 if (rate_mask & IEEE80211_OFDM_RATE_6MB_MASK)
bf79451e 5289 rates->supported_rates[rates->num_rates++] = basic_mask |
0edd5b44 5290 IEEE80211_OFDM_RATE_6MB;
43f66a6c
JK
5291
5292 if (rate_mask & IEEE80211_OFDM_RATE_9MB_MASK)
bf79451e 5293 rates->supported_rates[rates->num_rates++] =
0edd5b44 5294 IEEE80211_OFDM_RATE_9MB;
43f66a6c
JK
5295
5296 if (rate_mask & IEEE80211_OFDM_RATE_12MB_MASK)
bf79451e 5297 rates->supported_rates[rates->num_rates++] = basic_mask |
0edd5b44 5298 IEEE80211_OFDM_RATE_12MB;
43f66a6c
JK
5299
5300 if (rate_mask & IEEE80211_OFDM_RATE_18MB_MASK)
bf79451e 5301 rates->supported_rates[rates->num_rates++] =
0edd5b44 5302 IEEE80211_OFDM_RATE_18MB;
43f66a6c
JK
5303
5304 if (rate_mask & IEEE80211_OFDM_RATE_24MB_MASK)
bf79451e 5305 rates->supported_rates[rates->num_rates++] = basic_mask |
0edd5b44 5306 IEEE80211_OFDM_RATE_24MB;
43f66a6c
JK
5307
5308 if (rate_mask & IEEE80211_OFDM_RATE_36MB_MASK)
bf79451e 5309 rates->supported_rates[rates->num_rates++] =
0edd5b44 5310 IEEE80211_OFDM_RATE_36MB;
43f66a6c
JK
5311
5312 if (rate_mask & IEEE80211_OFDM_RATE_48MB_MASK)
bf79451e 5313 rates->supported_rates[rates->num_rates++] =
0edd5b44 5314 IEEE80211_OFDM_RATE_48MB;
43f66a6c
JK
5315
5316 if (rate_mask & IEEE80211_OFDM_RATE_54MB_MASK)
bf79451e 5317 rates->supported_rates[rates->num_rates++] =
0edd5b44 5318 IEEE80211_OFDM_RATE_54MB;
43f66a6c
JK
5319}
5320
5321struct ipw_network_match {
5322 struct ieee80211_network *network;
5323 struct ipw_supported_rates rates;
5324};
5325
c848d0af
JK
5326static int ipw_find_adhoc_network(struct ipw_priv *priv,
5327 struct ipw_network_match *match,
5328 struct ieee80211_network *network,
5329 int roaming)
43f66a6c
JK
5330{
5331 struct ipw_supported_rates rates;
5332
5333 /* Verify that this network's capability is compatible with the
5334 * current mode (AdHoc or Infrastructure) */
c848d0af 5335 if ((priv->ieee->iw_mode == IW_MODE_ADHOC &&
43f66a6c 5336 !(network->capability & WLAN_CAPABILITY_IBSS))) {
c848d0af 5337 IPW_DEBUG_MERGE("Network '%s (" MAC_FMT ")' excluded due to "
bf79451e 5338 "capability mismatch.\n",
43f66a6c
JK
5339 escape_essid(network->ssid, network->ssid_len),
5340 MAC_ARG(network->bssid));
5341 return 0;
5342 }
5343
5344 /* If we do not have an ESSID for this AP, we can not associate with
5345 * it */
5346 if (network->flags & NETWORK_EMPTY_ESSID) {
c848d0af 5347 IPW_DEBUG_MERGE("Network '%s (" MAC_FMT ")' excluded "
43f66a6c
JK
5348 "because of hidden ESSID.\n",
5349 escape_essid(network->ssid, network->ssid_len),
5350 MAC_ARG(network->bssid));
5351 return 0;
5352 }
bf79451e 5353
43f66a6c
JK
5354 if (unlikely(roaming)) {
5355 /* If we are roaming, then ensure check if this is a valid
5356 * network to try and roam to */
5357 if ((network->ssid_len != match->network->ssid_len) ||
bf79451e 5358 memcmp(network->ssid, match->network->ssid,
43f66a6c 5359 network->ssid_len)) {
c848d0af 5360 IPW_DEBUG_MERGE("Netowrk '%s (" MAC_FMT ")' excluded "
43f66a6c 5361 "because of non-network ESSID.\n",
bf79451e 5362 escape_essid(network->ssid,
43f66a6c
JK
5363 network->ssid_len),
5364 MAC_ARG(network->bssid));
5365 return 0;
5366 }
5367 } else {
bf79451e
JG
5368 /* If an ESSID has been configured then compare the broadcast
5369 * ESSID to ours */
5370 if ((priv->config & CFG_STATIC_ESSID) &&
43f66a6c 5371 ((network->ssid_len != priv->essid_len) ||
bf79451e 5372 memcmp(network->ssid, priv->essid,
43f66a6c
JK
5373 min(network->ssid_len, priv->essid_len)))) {
5374 char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
afbf30a2 5375
0edd5b44
JG
5376 strncpy(escaped,
5377 escape_essid(network->ssid, network->ssid_len),
43f66a6c 5378 sizeof(escaped));
c848d0af 5379 IPW_DEBUG_MERGE("Network '%s (" MAC_FMT ")' excluded "
bf79451e 5380 "because of ESSID mismatch: '%s'.\n",
43f66a6c 5381 escaped, MAC_ARG(network->bssid),
0edd5b44
JG
5382 escape_essid(priv->essid,
5383 priv->essid_len));
43f66a6c
JK
5384 return 0;
5385 }
5386 }
5387
5388 /* If the old network rate is better than this one, don't bother
5389 * testing everything else. */
c848d0af
JK
5390
5391 if (network->time_stamp[0] < match->network->time_stamp[0]) {
afbf30a2
JK
5392 IPW_DEBUG_MERGE("Network '%s excluded because newer than "
5393 "current network.\n",
43f66a6c 5394 escape_essid(match->network->ssid,
afbf30a2 5395 match->network->ssid_len));
43f66a6c 5396 return 0;
c848d0af 5397 } else if (network->time_stamp[1] < match->network->time_stamp[1]) {
afbf30a2
JK
5398 IPW_DEBUG_MERGE("Network '%s excluded because newer than "
5399 "current network.\n",
5400 escape_essid(match->network->ssid,
5401 match->network->ssid_len));
43f66a6c
JK
5402 return 0;
5403 }
5404
5405 /* Now go through and see if the requested network is valid... */
bf79451e 5406 if (priv->ieee->scan_age != 0 &&
c848d0af
JK
5407 time_after(jiffies, network->last_scanned + priv->ieee->scan_age)) {
5408 IPW_DEBUG_MERGE("Network '%s (" MAC_FMT ")' excluded "
c7b6a674 5409 "because of age: %ums.\n",
43f66a6c
JK
5410 escape_essid(network->ssid, network->ssid_len),
5411 MAC_ARG(network->bssid),
2638bc39
ZY
5412 jiffies_to_msecs(jiffies -
5413 network->last_scanned));
43f66a6c 5414 return 0;
bf79451e 5415 }
43f66a6c 5416
bf79451e 5417 if ((priv->config & CFG_STATIC_CHANNEL) &&
43f66a6c 5418 (network->channel != priv->channel)) {
c848d0af 5419 IPW_DEBUG_MERGE("Network '%s (" MAC_FMT ")' excluded "
43f66a6c
JK
5420 "because of channel mismatch: %d != %d.\n",
5421 escape_essid(network->ssid, network->ssid_len),
5422 MAC_ARG(network->bssid),
5423 network->channel, priv->channel);
5424 return 0;
5425 }
bf79451e 5426
43f66a6c 5427 /* Verify privacy compatability */
bf79451e 5428 if (((priv->capability & CAP_PRIVACY_ON) ? 1 : 0) !=
43f66a6c 5429 ((network->capability & WLAN_CAPABILITY_PRIVACY) ? 1 : 0)) {
c848d0af 5430 IPW_DEBUG_MERGE("Network '%s (" MAC_FMT ")' excluded "
43f66a6c
JK
5431 "because of privacy mismatch: %s != %s.\n",
5432 escape_essid(network->ssid, network->ssid_len),
5433 MAC_ARG(network->bssid),
afbf30a2
JK
5434 priv->
5435 capability & CAP_PRIVACY_ON ? "on" : "off",
5436 network->
5437 capability & WLAN_CAPABILITY_PRIVACY ? "on" :
5438 "off");
43f66a6c
JK
5439 return 0;
5440 }
bf79451e 5441
c848d0af
JK
5442 if (!memcmp(network->bssid, priv->bssid, ETH_ALEN)) {
5443 IPW_DEBUG_MERGE("Network '%s (" MAC_FMT ")' excluded "
5444 "because of the same BSSID match: " MAC_FMT
5445 ".\n", escape_essid(network->ssid,
5446 network->ssid_len),
0edd5b44 5447 MAC_ARG(network->bssid), MAC_ARG(priv->bssid));
43f66a6c
JK
5448 return 0;
5449 }
bf79451e 5450
43f66a6c
JK
5451 /* Filter out any incompatible freq / mode combinations */
5452 if (!ieee80211_is_valid_mode(priv->ieee, network->mode)) {
c848d0af 5453 IPW_DEBUG_MERGE("Network '%s (" MAC_FMT ")' excluded "
43f66a6c
JK
5454 "because of invalid frequency/mode "
5455 "combination.\n",
5456 escape_essid(network->ssid, network->ssid_len),
5457 MAC_ARG(network->bssid));
5458 return 0;
5459 }
bf79451e 5460
c848d0af
JK
5461 /* Ensure that the rates supported by the driver are compatible with
5462 * this AP, including verification of basic rates (mandatory) */
5463 if (!ipw_compatible_rates(priv, network, &rates)) {
5464 IPW_DEBUG_MERGE("Network '%s (" MAC_FMT ")' excluded "
5465 "because configured rate mask excludes "
5466 "AP mandatory rate.\n",
5467 escape_essid(network->ssid, network->ssid_len),
5468 MAC_ARG(network->bssid));
5469 return 0;
5470 }
5471
43f66a6c 5472 if (rates.num_rates == 0) {
c848d0af 5473 IPW_DEBUG_MERGE("Network '%s (" MAC_FMT ")' excluded "
43f66a6c
JK
5474 "because of no compatible rates.\n",
5475 escape_essid(network->ssid, network->ssid_len),
5476 MAC_ARG(network->bssid));
5477 return 0;
5478 }
bf79451e 5479
43f66a6c
JK
5480 /* TODO: Perform any further minimal comparititive tests. We do not
5481 * want to put too much policy logic here; intelligent scan selection
5482 * should occur within a generic IEEE 802.11 user space tool. */
5483
5484 /* Set up 'new' AP to this network */
5485 ipw_copy_rates(&match->rates, &rates);
5486 match->network = network;
c848d0af 5487 IPW_DEBUG_MERGE("Network '%s (" MAC_FMT ")' is a viable match.\n",
43f66a6c
JK
5488 escape_essid(network->ssid, network->ssid_len),
5489 MAC_ARG(network->bssid));
5490
5491 return 1;
5492}
5493
c848d0af 5494static void ipw_merge_adhoc_network(void *data)
43f66a6c 5495{
c848d0af
JK
5496 struct ipw_priv *priv = data;
5497 struct ieee80211_network *network = NULL;
5498 struct ipw_network_match match = {
5499 .network = priv->assoc_network
5500 };
5501
afbf30a2
JK
5502 if ((priv->status & STATUS_ASSOCIATED) &&
5503 (priv->ieee->iw_mode == IW_MODE_ADHOC)) {
c848d0af
JK
5504 /* First pass through ROAM process -- look for a better
5505 * network */
5506 unsigned long flags;
5507
5508 spin_lock_irqsave(&priv->ieee->lock, flags);
5509 list_for_each_entry(network, &priv->ieee->network_list, list) {
5510 if (network != priv->assoc_network)
5511 ipw_find_adhoc_network(priv, &match, network,
5512 1);
5513 }
5514 spin_unlock_irqrestore(&priv->ieee->lock, flags);
5515
5516 if (match.network == priv->assoc_network) {
5517 IPW_DEBUG_MERGE("No better ADHOC in this network to "
5518 "merge to.\n");
5519 return;
5520 }
5521
4644151b 5522 mutex_lock(&priv->mutex);
c848d0af
JK
5523 if ((priv->ieee->iw_mode == IW_MODE_ADHOC)) {
5524 IPW_DEBUG_MERGE("remove network %s\n",
5525 escape_essid(priv->essid,
5526 priv->essid_len));
5527 ipw_remove_current_network(priv);
43f66a6c 5528 }
c848d0af
JK
5529
5530 ipw_disassociate(priv);
5531 priv->assoc_network = match.network;
4644151b 5532 mutex_unlock(&priv->mutex);
c848d0af 5533 return;
43f66a6c 5534 }
c848d0af 5535}
43f66a6c 5536
0edd5b44
JG
5537static int ipw_best_network(struct ipw_priv *priv,
5538 struct ipw_network_match *match,
5539 struct ieee80211_network *network, int roaming)
43f66a6c
JK
5540{
5541 struct ipw_supported_rates rates;
5542
5543 /* Verify that this network's capability is compatible with the
5544 * current mode (AdHoc or Infrastructure) */
5545 if ((priv->ieee->iw_mode == IW_MODE_INFRA &&
2474385e 5546 !(network->capability & WLAN_CAPABILITY_ESS)) ||
43f66a6c
JK
5547 (priv->ieee->iw_mode == IW_MODE_ADHOC &&
5548 !(network->capability & WLAN_CAPABILITY_IBSS))) {
5549 IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded due to "
bf79451e 5550 "capability mismatch.\n",
43f66a6c
JK
5551 escape_essid(network->ssid, network->ssid_len),
5552 MAC_ARG(network->bssid));
5553 return 0;
5554 }
5555
5556 /* If we do not have an ESSID for this AP, we can not associate with
5557 * it */
5558 if (network->flags & NETWORK_EMPTY_ESSID) {
5559 IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
5560 "because of hidden ESSID.\n",
5561 escape_essid(network->ssid, network->ssid_len),
5562 MAC_ARG(network->bssid));
5563 return 0;
5564 }
bf79451e 5565
43f66a6c
JK
5566 if (unlikely(roaming)) {
5567 /* If we are roaming, then ensure check if this is a valid
5568 * network to try and roam to */
5569 if ((network->ssid_len != match->network->ssid_len) ||
bf79451e 5570 memcmp(network->ssid, match->network->ssid,
43f66a6c
JK
5571 network->ssid_len)) {
5572 IPW_DEBUG_ASSOC("Netowrk '%s (" MAC_FMT ")' excluded "
5573 "because of non-network ESSID.\n",
bf79451e 5574 escape_essid(network->ssid,
43f66a6c
JK
5575 network->ssid_len),
5576 MAC_ARG(network->bssid));
5577 return 0;
5578 }
5579 } else {
bf79451e
JG
5580 /* If an ESSID has been configured then compare the broadcast
5581 * ESSID to ours */
5582 if ((priv->config & CFG_STATIC_ESSID) &&
43f66a6c 5583 ((network->ssid_len != priv->essid_len) ||
bf79451e 5584 memcmp(network->ssid, priv->essid,
43f66a6c
JK
5585 min(network->ssid_len, priv->essid_len)))) {
5586 char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
0edd5b44
JG
5587 strncpy(escaped,
5588 escape_essid(network->ssid, network->ssid_len),
43f66a6c
JK
5589 sizeof(escaped));
5590 IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
bf79451e 5591 "because of ESSID mismatch: '%s'.\n",
43f66a6c 5592 escaped, MAC_ARG(network->bssid),
0edd5b44
JG
5593 escape_essid(priv->essid,
5594 priv->essid_len));
43f66a6c
JK
5595 return 0;
5596 }
5597 }
5598
5599 /* If the old network rate is better than this one, don't bother
5600 * testing everything else. */
0edd5b44 5601 if (match->network && match->network->stats.rssi > network->stats.rssi) {
43f66a6c 5602 char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
bf79451e
JG
5603 strncpy(escaped,
5604 escape_essid(network->ssid, network->ssid_len),
43f66a6c
JK
5605 sizeof(escaped));
5606 IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded because "
5607 "'%s (" MAC_FMT ")' has a stronger signal.\n",
5608 escaped, MAC_ARG(network->bssid),
5609 escape_essid(match->network->ssid,
5610 match->network->ssid_len),
5611 MAC_ARG(match->network->bssid));
5612 return 0;
5613 }
bf79451e 5614
43f66a6c
JK
5615 /* If this network has already had an association attempt within the
5616 * last 3 seconds, do not try and associate again... */
5617 if (network->last_associate &&
ea2b26e0 5618 time_after(network->last_associate + (HZ * 3UL), jiffies)) {
43f66a6c 5619 IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
c7b6a674 5620 "because of storming (%ums since last "
43f66a6c
JK
5621 "assoc attempt).\n",
5622 escape_essid(network->ssid, network->ssid_len),
5623 MAC_ARG(network->bssid),
2638bc39
ZY
5624 jiffies_to_msecs(jiffies -
5625 network->last_associate));
43f66a6c
JK
5626 return 0;
5627 }
5628
5629 /* Now go through and see if the requested network is valid... */
bf79451e 5630 if (priv->ieee->scan_age != 0 &&
ea2b26e0 5631 time_after(jiffies, network->last_scanned + priv->ieee->scan_age)) {
43f66a6c 5632 IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
c7b6a674 5633 "because of age: %ums.\n",
43f66a6c
JK
5634 escape_essid(network->ssid, network->ssid_len),
5635 MAC_ARG(network->bssid),
2638bc39
ZY
5636 jiffies_to_msecs(jiffies -
5637 network->last_scanned));
43f66a6c 5638 return 0;
bf79451e 5639 }
43f66a6c 5640
bf79451e 5641 if ((priv->config & CFG_STATIC_CHANNEL) &&
43f66a6c
JK
5642 (network->channel != priv->channel)) {
5643 IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
5644 "because of channel mismatch: %d != %d.\n",
5645 escape_essid(network->ssid, network->ssid_len),
5646 MAC_ARG(network->bssid),
5647 network->channel, priv->channel);
5648 return 0;
5649 }
bf79451e 5650
43f66a6c 5651 /* Verify privacy compatability */
bf79451e 5652 if (((priv->capability & CAP_PRIVACY_ON) ? 1 : 0) !=
43f66a6c
JK
5653 ((network->capability & WLAN_CAPABILITY_PRIVACY) ? 1 : 0)) {
5654 IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
5655 "because of privacy mismatch: %s != %s.\n",
5656 escape_essid(network->ssid, network->ssid_len),
5657 MAC_ARG(network->bssid),
bf79451e 5658 priv->capability & CAP_PRIVACY_ON ? "on" :
43f66a6c 5659 "off",
bf79451e 5660 network->capability &
0edd5b44 5661 WLAN_CAPABILITY_PRIVACY ? "on" : "off");
43f66a6c
JK
5662 return 0;
5663 }
bf79451e
JG
5664
5665 if ((priv->config & CFG_STATIC_BSSID) &&
43f66a6c
JK
5666 memcmp(network->bssid, priv->bssid, ETH_ALEN)) {
5667 IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
5668 "because of BSSID mismatch: " MAC_FMT ".\n",
5669 escape_essid(network->ssid, network->ssid_len),
0edd5b44 5670 MAC_ARG(network->bssid), MAC_ARG(priv->bssid));
43f66a6c
JK
5671 return 0;
5672 }
bf79451e 5673
43f66a6c
JK
5674 /* Filter out any incompatible freq / mode combinations */
5675 if (!ieee80211_is_valid_mode(priv->ieee, network->mode)) {
5676 IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
5677 "because of invalid frequency/mode "
5678 "combination.\n",
5679 escape_essid(network->ssid, network->ssid_len),
5680 MAC_ARG(network->bssid));
5681 return 0;
5682 }
bf79451e 5683
1fe0adb4 5684 /* Filter out invalid channel in current GEO */
1867b117 5685 if (!ieee80211_is_valid_channel(priv->ieee, network->channel)) {
1fe0adb4
LH
5686 IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
5687 "because of invalid channel in current GEO\n",
5688 escape_essid(network->ssid, network->ssid_len),
5689 MAC_ARG(network->bssid));
5690 return 0;
5691 }
5692
ea2b26e0
JK
5693 /* Ensure that the rates supported by the driver are compatible with
5694 * this AP, including verification of basic rates (mandatory) */
5695 if (!ipw_compatible_rates(priv, network, &rates)) {
5696 IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
5697 "because configured rate mask excludes "
5698 "AP mandatory rate.\n",
5699 escape_essid(network->ssid, network->ssid_len),
5700 MAC_ARG(network->bssid));
5701 return 0;
5702 }
5703
43f66a6c
JK
5704 if (rates.num_rates == 0) {
5705 IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' excluded "
5706 "because of no compatible rates.\n",
5707 escape_essid(network->ssid, network->ssid_len),
5708 MAC_ARG(network->bssid));
5709 return 0;
5710 }
bf79451e 5711
43f66a6c
JK
5712 /* TODO: Perform any further minimal comparititive tests. We do not
5713 * want to put too much policy logic here; intelligent scan selection
5714 * should occur within a generic IEEE 802.11 user space tool. */
5715
5716 /* Set up 'new' AP to this network */
5717 ipw_copy_rates(&match->rates, &rates);
5718 match->network = network;
5719
5720 IPW_DEBUG_ASSOC("Network '%s (" MAC_FMT ")' is a viable match.\n",
5721 escape_essid(network->ssid, network->ssid_len),
5722 MAC_ARG(network->bssid));
5723
5724 return 1;
5725}
5726
bf79451e 5727static void ipw_adhoc_create(struct ipw_priv *priv,
0edd5b44 5728 struct ieee80211_network *network)
43f66a6c 5729{
1867b117 5730 const struct ieee80211_geo *geo = ieee80211_get_geo(priv->ieee);
afbf30a2
JK
5731 int i;
5732
43f66a6c
JK
5733 /*
5734 * For the purposes of scanning, we can set our wireless mode
5735 * to trigger scans across combinations of bands, but when it
5736 * comes to creating a new ad-hoc network, we have tell the FW
5737 * exactly which band to use.
5738 *
bf79451e 5739 * We also have the possibility of an invalid channel for the
43f66a6c
JK
5740 * chossen band. Attempting to create a new ad-hoc network
5741 * with an invalid channel for wireless mode will trigger a
5742 * FW fatal error.
afbf30a2 5743 *
43f66a6c 5744 */
1867b117 5745 switch (ieee80211_is_valid_channel(priv->ieee, priv->channel)) {
afbf30a2
JK
5746 case IEEE80211_52GHZ_BAND:
5747 network->mode = IEEE_A;
1867b117 5748 i = ieee80211_channel_to_index(priv->ieee, priv->channel);
5d9428de 5749 BUG_ON(i == -1);
afbf30a2
JK
5750 if (geo->a[i].flags & IEEE80211_CH_PASSIVE_ONLY) {
5751 IPW_WARNING("Overriding invalid channel\n");
5752 priv->channel = geo->a[0].channel;
5753 }
5754 break;
5755
5756 case IEEE80211_24GHZ_BAND:
5757 if (priv->ieee->mode & IEEE_G)
5758 network->mode = IEEE_G;
5759 else
5760 network->mode = IEEE_B;
1867b117 5761 i = ieee80211_channel_to_index(priv->ieee, priv->channel);
5d9428de 5762 BUG_ON(i == -1);
1fe0adb4
LH
5763 if (geo->bg[i].flags & IEEE80211_CH_PASSIVE_ONLY) {
5764 IPW_WARNING("Overriding invalid channel\n");
5765 priv->channel = geo->bg[0].channel;
5766 }
afbf30a2
JK
5767 break;
5768
5769 default:
43f66a6c
JK
5770 IPW_WARNING("Overriding invalid channel\n");
5771 if (priv->ieee->mode & IEEE_A) {
5772 network->mode = IEEE_A;
b095c381 5773 priv->channel = geo->a[0].channel;
43f66a6c
JK
5774 } else if (priv->ieee->mode & IEEE_G) {
5775 network->mode = IEEE_G;
b095c381 5776 priv->channel = geo->bg[0].channel;
43f66a6c
JK
5777 } else {
5778 network->mode = IEEE_B;
b095c381 5779 priv->channel = geo->bg[0].channel;
43f66a6c 5780 }
afbf30a2
JK
5781 break;
5782 }
43f66a6c
JK
5783
5784 network->channel = priv->channel;
5785 priv->config |= CFG_ADHOC_PERSIST;
5786 ipw_create_bssid(priv, network->bssid);
5787 network->ssid_len = priv->essid_len;
5788 memcpy(network->ssid, priv->essid, priv->essid_len);
5789 memset(&network->stats, 0, sizeof(network->stats));
5790 network->capability = WLAN_CAPABILITY_IBSS;
ea2b26e0
JK
5791 if (!(priv->config & CFG_PREAMBLE_LONG))
5792 network->capability |= WLAN_CAPABILITY_SHORT_PREAMBLE;
43f66a6c
JK
5793 if (priv->capability & CAP_PRIVACY_ON)
5794 network->capability |= WLAN_CAPABILITY_PRIVACY;
5795 network->rates_len = min(priv->rates.num_rates, MAX_RATES_LENGTH);
0edd5b44 5796 memcpy(network->rates, priv->rates.supported_rates, network->rates_len);
43f66a6c 5797 network->rates_ex_len = priv->rates.num_rates - network->rates_len;
bf79451e 5798 memcpy(network->rates_ex,
43f66a6c
JK
5799 &priv->rates.supported_rates[network->rates_len],
5800 network->rates_ex_len);
5801 network->last_scanned = 0;
5802 network->flags = 0;
5803 network->last_associate = 0;
5804 network->time_stamp[0] = 0;
5805 network->time_stamp[1] = 0;
0edd5b44
JG
5806 network->beacon_interval = 100; /* Default */
5807 network->listen_interval = 10; /* Default */
5808 network->atim_window = 0; /* Default */
43f66a6c
JK
5809 network->wpa_ie_len = 0;
5810 network->rsn_ie_len = 0;
43f66a6c
JK
5811}
5812
b095c381
JK
5813static void ipw_send_tgi_tx_key(struct ipw_priv *priv, int type, int index)
5814{
0a7bcf26 5815 struct ipw_tgi_tx_key key;
b095c381
JK
5816
5817 if (!(priv->ieee->sec.flags & (1 << index)))
5818 return;
5819
0a7bcf26
ZY
5820 key.key_id = index;
5821 memcpy(key.key, priv->ieee->sec.keys[index], SCM_TEMPORAL_KEY_LENGTH);
5822 key.security_type = type;
5823 key.station_index = 0; /* always 0 for BSS */
5824 key.flags = 0;
b095c381 5825 /* 0 for new key; previous value of counter (after fatal error) */
851ca268
ZY
5826 key.tx_counter[0] = cpu_to_le32(0);
5827 key.tx_counter[1] = cpu_to_le32(0);
b095c381 5828
0a7bcf26 5829 ipw_send_cmd_pdu(priv, IPW_CMD_TGI_TX_KEY, sizeof(key), &key);
b095c381
JK
5830}
5831
5832static void ipw_send_wep_keys(struct ipw_priv *priv, int type)
43f66a6c 5833{
0a7bcf26 5834 struct ipw_wep_key key;
43f66a6c 5835 int i;
43f66a6c 5836
0a7bcf26
ZY
5837 key.cmd_id = DINO_CMD_WEP_KEY;
5838 key.seq_num = 0;
43f66a6c 5839
b095c381
JK
5840 /* Note: AES keys cannot be set for multiple times.
5841 * Only set it at the first time. */
bf79451e 5842 for (i = 0; i < 4; i++) {
0a7bcf26 5843 key.key_index = i | type;
b095c381 5844 if (!(priv->ieee->sec.flags & (1 << i))) {
0a7bcf26 5845 key.key_size = 0;
b095c381 5846 continue;
43f66a6c
JK
5847 }
5848
0a7bcf26
ZY
5849 key.key_size = priv->ieee->sec.key_sizes[i];
5850 memcpy(key.key, priv->ieee->sec.keys[i], key.key_size);
b095c381 5851
0a7bcf26 5852 ipw_send_cmd_pdu(priv, IPW_CMD_WEP_KEY, sizeof(key), &key);
bf79451e 5853 }
43f66a6c
JK
5854}
5855
1fbfea54 5856static void ipw_set_hw_decrypt_unicast(struct ipw_priv *priv, int level)
43f66a6c 5857{
1fbfea54 5858 if (priv->ieee->host_encrypt)
43f66a6c 5859 return;
43f66a6c 5860
1fbfea54
ZY
5861 switch (level) {
5862 case SEC_LEVEL_3:
5863 priv->sys_config.disable_unicast_decryption = 0;
5864 priv->ieee->host_decrypt = 0;
5865 break;
5866 case SEC_LEVEL_2:
5867 priv->sys_config.disable_unicast_decryption = 1;
5868 priv->ieee->host_decrypt = 1;
5869 break;
5870 case SEC_LEVEL_1:
5871 priv->sys_config.disable_unicast_decryption = 0;
5872 priv->ieee->host_decrypt = 0;
5873 break;
5874 case SEC_LEVEL_0:
5875 priv->sys_config.disable_unicast_decryption = 1;
5876 break;
5877 default:
5878 break;
5879 }
5880}
5881
5882static void ipw_set_hw_decrypt_multicast(struct ipw_priv *priv, int level)
5883{
5884 if (priv->ieee->host_encrypt)
5885 return;
5886
5887 switch (level) {
5888 case SEC_LEVEL_3:
5889 priv->sys_config.disable_multicast_decryption = 0;
5890 break;
5891 case SEC_LEVEL_2:
5892 priv->sys_config.disable_multicast_decryption = 1;
5893 break;
5894 case SEC_LEVEL_1:
5895 priv->sys_config.disable_multicast_decryption = 0;
5896 break;
5897 case SEC_LEVEL_0:
5898 priv->sys_config.disable_multicast_decryption = 1;
5899 break;
5900 default:
5901 break;
5902 }
5903}
5904
b095c381
JK
5905static void ipw_set_hwcrypto_keys(struct ipw_priv *priv)
5906{
5907 switch (priv->ieee->sec.level) {
5908 case SEC_LEVEL_3:
d8bad6df
ZY
5909 if (priv->ieee->sec.flags & SEC_ACTIVE_KEY)
5910 ipw_send_tgi_tx_key(priv,
5911 DCT_FLAG_EXT_SECURITY_CCM,
5912 priv->ieee->sec.active_key);
afbf30a2 5913
567deaf6
HL
5914 if (!priv->ieee->host_mc_decrypt)
5915 ipw_send_wep_keys(priv, DCW_WEP_KEY_SEC_TYPE_CCM);
b095c381
JK
5916 break;
5917 case SEC_LEVEL_2:
d8bad6df
ZY
5918 if (priv->ieee->sec.flags & SEC_ACTIVE_KEY)
5919 ipw_send_tgi_tx_key(priv,
5920 DCT_FLAG_EXT_SECURITY_TKIP,
5921 priv->ieee->sec.active_key);
b095c381
JK
5922 break;
5923 case SEC_LEVEL_1:
5924 ipw_send_wep_keys(priv, DCW_WEP_KEY_SEC_TYPE_WEP);
29cb843e
HL
5925 ipw_set_hw_decrypt_unicast(priv, priv->ieee->sec.level);
5926 ipw_set_hw_decrypt_multicast(priv, priv->ieee->sec.level);
b095c381
JK
5927 break;
5928 case SEC_LEVEL_0:
5929 default:
5930 break;
5931 }
5932}
5933
43f66a6c
JK
5934static void ipw_adhoc_check(void *data)
5935{
5936 struct ipw_priv *priv = data;
bf79451e 5937
afbf30a2 5938 if (priv->missed_adhoc_beacons++ > priv->disassociate_threshold &&
43f66a6c 5939 !(priv->config & CFG_ADHOC_PERSIST)) {
afbf30a2
JK
5940 IPW_DEBUG(IPW_DL_INFO | IPW_DL_NOTIF |
5941 IPW_DL_STATE | IPW_DL_ASSOC,
5942 "Missed beacon: %d - disassociate\n",
5943 priv->missed_adhoc_beacons);
43f66a6c
JK
5944 ipw_remove_current_network(priv);
5945 ipw_disassociate(priv);
5946 return;
5947 }
5948
bf79451e 5949 queue_delayed_work(priv->workqueue, &priv->adhoc_check,
43f66a6c
JK
5950 priv->assoc_request.beacon_interval);
5951}
5952
c848d0af
JK
5953static void ipw_bg_adhoc_check(void *data)
5954{
5955 struct ipw_priv *priv = data;
4644151b 5956 mutex_lock(&priv->mutex);
c848d0af 5957 ipw_adhoc_check(data);
4644151b 5958 mutex_unlock(&priv->mutex);
c848d0af
JK
5959}
5960
43f66a6c
JK
5961static void ipw_debug_config(struct ipw_priv *priv)
5962{
5963 IPW_DEBUG_INFO("Scan completed, no valid APs matched "
5964 "[CFG 0x%08X]\n", priv->config);
5965 if (priv->config & CFG_STATIC_CHANNEL)
0edd5b44 5966 IPW_DEBUG_INFO("Channel locked to %d\n", priv->channel);
43f66a6c
JK
5967 else
5968 IPW_DEBUG_INFO("Channel unlocked.\n");
5969 if (priv->config & CFG_STATIC_ESSID)
bf79451e 5970 IPW_DEBUG_INFO("ESSID locked to '%s'\n",
0edd5b44 5971 escape_essid(priv->essid, priv->essid_len));
43f66a6c
JK
5972 else
5973 IPW_DEBUG_INFO("ESSID unlocked.\n");
5974 if (priv->config & CFG_STATIC_BSSID)
ea2b26e0
JK
5975 IPW_DEBUG_INFO("BSSID locked to " MAC_FMT "\n",
5976 MAC_ARG(priv->bssid));
43f66a6c
JK
5977 else
5978 IPW_DEBUG_INFO("BSSID unlocked.\n");
5979 if (priv->capability & CAP_PRIVACY_ON)
5980 IPW_DEBUG_INFO("PRIVACY on\n");
5981 else
5982 IPW_DEBUG_INFO("PRIVACY off\n");
5983 IPW_DEBUG_INFO("RATE MASK: 0x%08X\n", priv->rates_mask);
5984}
43f66a6c 5985
858119e1 5986static void ipw_set_fixed_rate(struct ipw_priv *priv, int mode)
43f66a6c
JK
5987{
5988 /* TODO: Verify that this works... */
5989 struct ipw_fixed_rate fr = {
5990 .tx_rates = priv->rates_mask
5991 };
5992 u32 reg;
5993 u16 mask = 0;
5994
bf79451e 5995 /* Identify 'current FW band' and match it with the fixed
43f66a6c 5996 * Tx rates */
bf79451e 5997
43f66a6c 5998 switch (priv->ieee->freq_band) {
0edd5b44 5999 case IEEE80211_52GHZ_BAND: /* A only */
43f66a6c
JK
6000 /* IEEE_A */
6001 if (priv->rates_mask & ~IEEE80211_OFDM_RATES_MASK) {
6002 /* Invalid fixed rate mask */
ea2b26e0
JK
6003 IPW_DEBUG_WX
6004 ("invalid fixed rate mask in ipw_set_fixed_rate\n");
43f66a6c
JK
6005 fr.tx_rates = 0;
6006 break;
6007 }
bf79451e 6008
43f66a6c
JK
6009 fr.tx_rates >>= IEEE80211_OFDM_SHIFT_MASK_A;
6010 break;
6011
0edd5b44 6012 default: /* 2.4Ghz or Mixed */
43f66a6c 6013 /* IEEE_B */
b095c381 6014 if (mode == IEEE_B) {
43f66a6c
JK
6015 if (fr.tx_rates & ~IEEE80211_CCK_RATES_MASK) {
6016 /* Invalid fixed rate mask */
ea2b26e0
JK
6017 IPW_DEBUG_WX
6018 ("invalid fixed rate mask in ipw_set_fixed_rate\n");
43f66a6c
JK
6019 fr.tx_rates = 0;
6020 }
6021 break;
bf79451e 6022 }
43f66a6c
JK
6023
6024 /* IEEE_G */
6025 if (fr.tx_rates & ~(IEEE80211_CCK_RATES_MASK |
6026 IEEE80211_OFDM_RATES_MASK)) {
6027 /* Invalid fixed rate mask */
ea2b26e0
JK
6028 IPW_DEBUG_WX
6029 ("invalid fixed rate mask in ipw_set_fixed_rate\n");
43f66a6c
JK
6030 fr.tx_rates = 0;
6031 break;
6032 }
bf79451e 6033
43f66a6c
JK
6034 if (IEEE80211_OFDM_RATE_6MB_MASK & fr.tx_rates) {
6035 mask |= (IEEE80211_OFDM_RATE_6MB_MASK >> 1);
6036 fr.tx_rates &= ~IEEE80211_OFDM_RATE_6MB_MASK;
6037 }
bf79451e 6038
43f66a6c
JK
6039 if (IEEE80211_OFDM_RATE_9MB_MASK & fr.tx_rates) {
6040 mask |= (IEEE80211_OFDM_RATE_9MB_MASK >> 1);
6041 fr.tx_rates &= ~IEEE80211_OFDM_RATE_9MB_MASK;
6042 }
bf79451e 6043
43f66a6c
JK
6044 if (IEEE80211_OFDM_RATE_12MB_MASK & fr.tx_rates) {
6045 mask |= (IEEE80211_OFDM_RATE_12MB_MASK >> 1);
6046 fr.tx_rates &= ~IEEE80211_OFDM_RATE_12MB_MASK;
6047 }
bf79451e 6048
43f66a6c
JK
6049 fr.tx_rates |= mask;
6050 break;
6051 }
6052
6053 reg = ipw_read32(priv, IPW_MEM_FIXED_OVERRIDE);
0edd5b44 6054 ipw_write_reg32(priv, reg, *(u32 *) & fr);
43f66a6c
JK
6055}
6056
ea2b26e0 6057static void ipw_abort_scan(struct ipw_priv *priv)
43f66a6c
JK
6058{
6059 int err;
6060
ea2b26e0
JK
6061 if (priv->status & STATUS_SCAN_ABORTING) {
6062 IPW_DEBUG_HC("Ignoring concurrent scan abort request.\n");
6063 return;
6064 }
6065 priv->status |= STATUS_SCAN_ABORTING;
43f66a6c 6066
ea2b26e0
JK
6067 err = ipw_send_scan_abort(priv);
6068 if (err)
6069 IPW_DEBUG_HC("Request to abort scan failed.\n");
6070}
6071
afbf30a2
JK
6072static void ipw_add_scan_channels(struct ipw_priv *priv,
6073 struct ipw_scan_request_ext *scan,
6074 int scan_type)
ea2b26e0 6075{
ea2b26e0 6076 int channel_index = 0;
b095c381 6077 const struct ieee80211_geo *geo;
afbf30a2 6078 int i;
b095c381 6079
1867b117 6080 geo = ieee80211_get_geo(priv->ieee);
43f66a6c 6081
afbf30a2
JK
6082 if (priv->ieee->freq_band & IEEE80211_52GHZ_BAND) {
6083 int start = channel_index;
6084 for (i = 0; i < geo->a_channels; i++) {
6085 if ((priv->status & STATUS_ASSOCIATED) &&
6086 geo->a[i].channel == priv->channel)
6087 continue;
6088 channel_index++;
6089 scan->channels_list[channel_index] = geo->a[i].channel;
1fe0adb4
LH
6090 ipw_set_scan_type(scan, channel_index,
6091 geo->a[i].
6092 flags & IEEE80211_CH_PASSIVE_ONLY ?
6093 IPW_SCAN_PASSIVE_FULL_DWELL_SCAN :
6094 scan_type);
afbf30a2
JK
6095 }
6096
6097 if (start != channel_index) {
6098 scan->channels_list[start] = (u8) (IPW_A_MODE << 6) |
6099 (channel_index - start);
6100 channel_index++;
6101 }
6102 }
6103
6104 if (priv->ieee->freq_band & IEEE80211_24GHZ_BAND) {
6105 int start = channel_index;
6106 if (priv->config & CFG_SPEED_SCAN) {
1fe0adb4 6107 int index;
afbf30a2
JK
6108 u8 channels[IEEE80211_24GHZ_CHANNELS] = {
6109 /* nop out the list */
6110 [0] = 0
6111 };
6112
6113 u8 channel;
6114 while (channel_index < IPW_SCAN_CHANNELS) {
6115 channel =
6116 priv->speed_scan[priv->speed_scan_pos];
6117 if (channel == 0) {
6118 priv->speed_scan_pos = 0;
6119 channel = priv->speed_scan[0];
6120 }
6121 if ((priv->status & STATUS_ASSOCIATED) &&
6122 channel == priv->channel) {
6123 priv->speed_scan_pos++;
6124 continue;
6125 }
6126
6127 /* If this channel has already been
6128 * added in scan, break from loop
6129 * and this will be the first channel
6130 * in the next scan.
6131 */
6132 if (channels[channel - 1] != 0)
6133 break;
6134
6135 channels[channel - 1] = 1;
6136 priv->speed_scan_pos++;
6137 channel_index++;
6138 scan->channels_list[channel_index] = channel;
1fe0adb4 6139 index =
1867b117 6140 ieee80211_channel_to_index(priv->ieee, channel);
afbf30a2 6141 ipw_set_scan_type(scan, channel_index,
1fe0adb4
LH
6142 geo->bg[index].
6143 flags &
6144 IEEE80211_CH_PASSIVE_ONLY ?
6145 IPW_SCAN_PASSIVE_FULL_DWELL_SCAN
6146 : scan_type);
afbf30a2
JK
6147 }
6148 } else {
6149 for (i = 0; i < geo->bg_channels; i++) {
6150 if ((priv->status & STATUS_ASSOCIATED) &&
6151 geo->bg[i].channel == priv->channel)
6152 continue;
6153 channel_index++;
6154 scan->channels_list[channel_index] =
6155 geo->bg[i].channel;
6156 ipw_set_scan_type(scan, channel_index,
1fe0adb4
LH
6157 geo->bg[i].
6158 flags &
6159 IEEE80211_CH_PASSIVE_ONLY ?
6160 IPW_SCAN_PASSIVE_FULL_DWELL_SCAN
6161 : scan_type);
afbf30a2
JK
6162 }
6163 }
6164
6165 if (start != channel_index) {
6166 scan->channels_list[start] = (u8) (IPW_B_MODE << 6) |
6167 (channel_index - start);
6168 }
6169 }
6170}
6171
6172static int ipw_request_scan(struct ipw_priv *priv)
6173{
6174 struct ipw_scan_request_ext scan;
6175 int err = 0, scan_type;
6176
6177 if (!(priv->status & STATUS_INIT) ||
6178 (priv->status & STATUS_EXIT_PENDING))
6179 return 0;
6180
4644151b 6181 mutex_lock(&priv->mutex);
afbf30a2 6182
ea2b26e0 6183 if (priv->status & STATUS_SCANNING) {
a613bffd 6184 IPW_DEBUG_HC("Concurrent scan requested. Ignoring.\n");
ea2b26e0 6185 priv->status |= STATUS_SCAN_PENDING;
b095c381 6186 goto done;
ea2b26e0 6187 }
43f66a6c 6188
afbf30a2
JK
6189 if (!(priv->status & STATUS_SCAN_FORCED) &&
6190 priv->status & STATUS_SCAN_ABORTING) {
ea2b26e0
JK
6191 IPW_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6192 priv->status |= STATUS_SCAN_PENDING;
b095c381 6193 goto done;
43f66a6c
JK
6194 }
6195
ea2b26e0
JK
6196 if (priv->status & STATUS_RF_KILL_MASK) {
6197 IPW_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6198 priv->status |= STATUS_SCAN_PENDING;
b095c381 6199 goto done;
ea2b26e0 6200 }
43f66a6c 6201
ea2b26e0 6202 memset(&scan, 0, sizeof(scan));
43f66a6c 6203
b095c381
JK
6204 if (priv->config & CFG_SPEED_SCAN)
6205 scan.dwell_time[IPW_SCAN_ACTIVE_BROADCAST_SCAN] =
6206 cpu_to_le16(30);
6207 else
6208 scan.dwell_time[IPW_SCAN_ACTIVE_BROADCAST_SCAN] =
6209 cpu_to_le16(20);
6210
a613bffd
JK
6211 scan.dwell_time[IPW_SCAN_ACTIVE_BROADCAST_AND_DIRECT_SCAN] =
6212 cpu_to_le16(20);
1fe0adb4 6213 scan.dwell_time[IPW_SCAN_PASSIVE_FULL_DWELL_SCAN] = cpu_to_le16(120);
43f66a6c 6214
a613bffd 6215 scan.full_scan_index = cpu_to_le32(ieee80211_get_scans(priv->ieee));
43f66a6c 6216
b095c381 6217#ifdef CONFIG_IPW2200_MONITOR
ea2b26e0 6218 if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
afbf30a2 6219 u8 channel;
b095c381 6220 u8 band = 0;
43f66a6c 6221
1867b117 6222 switch (ieee80211_is_valid_channel(priv->ieee, priv->channel)) {
b095c381 6223 case IEEE80211_52GHZ_BAND:
ea2b26e0 6224 band = (u8) (IPW_A_MODE << 6) | 1;
b095c381
JK
6225 channel = priv->channel;
6226 break;
ea2b26e0 6227
b095c381 6228 case IEEE80211_24GHZ_BAND:
ea2b26e0 6229 band = (u8) (IPW_B_MODE << 6) | 1;
b095c381
JK
6230 channel = priv->channel;
6231 break;
ea2b26e0 6232
b095c381 6233 default:
ea2b26e0
JK
6234 band = (u8) (IPW_B_MODE << 6) | 1;
6235 channel = 9;
b095c381 6236 break;
ea2b26e0
JK
6237 }
6238
b095c381
JK
6239 scan.channels_list[0] = band;
6240 scan.channels_list[1] = channel;
6241 ipw_set_scan_type(&scan, 1, IPW_SCAN_PASSIVE_FULL_DWELL_SCAN);
ea2b26e0 6242
b095c381
JK
6243 /* NOTE: The card will sit on this channel for this time
6244 * period. Scan aborts are timing sensitive and frequently
6245 * result in firmware restarts. As such, it is best to
6246 * set a small dwell_time here and just keep re-issuing
6247 * scans. Otherwise fast channel hopping will not actually
6248 * hop channels.
6249 *
6250 * TODO: Move SPEED SCAN support to all modes and bands */
a613bffd
JK
6251 scan.dwell_time[IPW_SCAN_PASSIVE_FULL_DWELL_SCAN] =
6252 cpu_to_le16(2000);
43f66a6c 6253 } else {
b095c381
JK
6254#endif /* CONFIG_IPW2200_MONITOR */
6255 /* If we are roaming, then make this a directed scan for the
6256 * current network. Otherwise, ensure that every other scan
6257 * is a fast channel hop scan */
6258 if ((priv->status & STATUS_ROAMING)
6259 || (!(priv->status & STATUS_ASSOCIATED)
6260 && (priv->config & CFG_STATIC_ESSID)
6261 && (le32_to_cpu(scan.full_scan_index) % 2))) {
ea2b26e0
JK
6262 err = ipw_send_ssid(priv, priv->essid, priv->essid_len);
6263 if (err) {
b095c381
JK
6264 IPW_DEBUG_HC("Attempt to send SSID command "
6265 "failed.\n");
6266 goto done;
ea2b26e0 6267 }
43f66a6c 6268
ea2b26e0 6269 scan_type = IPW_SCAN_ACTIVE_BROADCAST_AND_DIRECT_SCAN;
afbf30a2 6270 } else
ea2b26e0 6271 scan_type = IPW_SCAN_ACTIVE_BROADCAST_SCAN;
ea2b26e0 6272
afbf30a2 6273 ipw_add_scan_channels(priv, &scan, scan_type);
b095c381 6274#ifdef CONFIG_IPW2200_MONITOR
43f66a6c 6275 }
ea2b26e0 6276#endif
bf79451e 6277
ea2b26e0 6278 err = ipw_send_scan_request_ext(priv, &scan);
43f66a6c 6279 if (err) {
ea2b26e0 6280 IPW_DEBUG_HC("Sending scan command failed: %08X\n", err);
b095c381 6281 goto done;
43f66a6c
JK
6282 }
6283
ea2b26e0
JK
6284 priv->status |= STATUS_SCANNING;
6285 priv->status &= ~STATUS_SCAN_PENDING;
afbf30a2
JK
6286 queue_delayed_work(priv->workqueue, &priv->scan_check,
6287 IPW_SCAN_CHECK_WATCHDOG);
b095c381 6288 done:
4644151b 6289 mutex_unlock(&priv->mutex);
b095c381 6290 return err;
c848d0af
JK
6291}
6292
6293static void ipw_bg_abort_scan(void *data)
6294{
6295 struct ipw_priv *priv = data;
4644151b 6296 mutex_lock(&priv->mutex);
c848d0af 6297 ipw_abort_scan(data);
4644151b 6298 mutex_unlock(&priv->mutex);
c848d0af
JK
6299}
6300
ea2b26e0
JK
6301static int ipw_wpa_enable(struct ipw_priv *priv, int value)
6302{
b095c381
JK
6303 /* This is called when wpa_supplicant loads and closes the driver
6304 * interface. */
cdd1fa1e 6305 priv->ieee->wpa_enabled = value;
b095c381 6306 return 0;
ea2b26e0
JK
6307}
6308
ea2b26e0
JK
6309static int ipw_wpa_set_auth_algs(struct ipw_priv *priv, int value)
6310{
6311 struct ieee80211_device *ieee = priv->ieee;
6312 struct ieee80211_security sec = {
6313 .flags = SEC_AUTH_MODE,
6314 };
6315 int ret = 0;
6316
afbf30a2 6317 if (value & IW_AUTH_ALG_SHARED_KEY) {
ea2b26e0
JK
6318 sec.auth_mode = WLAN_AUTH_SHARED_KEY;
6319 ieee->open_wep = 0;
afbf30a2 6320 } else if (value & IW_AUTH_ALG_OPEN_SYSTEM) {
ea2b26e0
JK
6321 sec.auth_mode = WLAN_AUTH_OPEN;
6322 ieee->open_wep = 1;
3e234b4e
ZY
6323 } else if (value & IW_AUTH_ALG_LEAP) {
6324 sec.auth_mode = WLAN_AUTH_LEAP;
6325 ieee->open_wep = 1;
afbf30a2
JK
6326 } else
6327 return -EINVAL;
ea2b26e0
JK
6328
6329 if (ieee->set_security)
6330 ieee->set_security(ieee->dev, &sec);
6331 else
6332 ret = -EOPNOTSUPP;
6333
6334 return ret;
6335}
6336
a73e22b2
AB
6337static void ipw_wpa_assoc_frame(struct ipw_priv *priv, char *wpa_ie,
6338 int wpa_ie_len)
afbf30a2
JK
6339{
6340 /* make sure WPA is enabled */
6341 ipw_wpa_enable(priv, 1);
afbf30a2
JK
6342}
6343
6344static int ipw_set_rsn_capa(struct ipw_priv *priv,
6345 char *capabilities, int length)
6346{
afbf30a2
JK
6347 IPW_DEBUG_HC("HOST_CMD_RSN_CAPABILITIES\n");
6348
0a7bcf26 6349 return ipw_send_cmd_pdu(priv, IPW_CMD_RSN_CAPABILITIES, length,
2638bc39 6350 capabilities);
afbf30a2
JK
6351}
6352
b095c381 6353/*
afbf30a2
JK
6354 * WE-18 support
6355 */
6356
6357/* SIOCSIWGENIE */
6358static int ipw_wx_set_genie(struct net_device *dev,
6359 struct iw_request_info *info,
6360 union iwreq_data *wrqu, char *extra)
ea2b26e0 6361{
afbf30a2
JK
6362 struct ipw_priv *priv = ieee80211_priv(dev);
6363 struct ieee80211_device *ieee = priv->ieee;
6364 u8 *buf;
6365 int err = 0;
ea2b26e0 6366
afbf30a2
JK
6367 if (wrqu->data.length > MAX_WPA_IE_LEN ||
6368 (wrqu->data.length && extra == NULL))
6369 return -EINVAL;
ea2b26e0 6370
afbf30a2
JK
6371 if (wrqu->data.length) {
6372 buf = kmalloc(wrqu->data.length, GFP_KERNEL);
6373 if (buf == NULL) {
6374 err = -ENOMEM;
6375 goto out;
6376 }
6377
6378 memcpy(buf, extra, wrqu->data.length);
6379 kfree(ieee->wpa_ie);
6380 ieee->wpa_ie = buf;
6381 ieee->wpa_ie_len = wrqu->data.length;
b095c381 6382 } else {
afbf30a2
JK
6383 kfree(ieee->wpa_ie);
6384 ieee->wpa_ie = NULL;
6385 ieee->wpa_ie_len = 0;
ea2b26e0 6386 }
afbf30a2
JK
6387
6388 ipw_wpa_assoc_frame(priv, ieee->wpa_ie, ieee->wpa_ie_len);
6389 out:
afbf30a2
JK
6390 return err;
6391}
6392
6393/* SIOCGIWGENIE */
6394static int ipw_wx_get_genie(struct net_device *dev,
6395 struct iw_request_info *info,
6396 union iwreq_data *wrqu, char *extra)
6397{
6398 struct ipw_priv *priv = ieee80211_priv(dev);
6399 struct ieee80211_device *ieee = priv->ieee;
6400 int err = 0;
6401
afbf30a2
JK
6402 if (ieee->wpa_ie_len == 0 || ieee->wpa_ie == NULL) {
6403 wrqu->data.length = 0;
6404 goto out;
6405 }
6406
6407 if (wrqu->data.length < ieee->wpa_ie_len) {
6408 err = -E2BIG;
6409 goto out;
6410 }
6411
6412 wrqu->data.length = ieee->wpa_ie_len;
6413 memcpy(extra, ieee->wpa_ie, ieee->wpa_ie_len);
6414
6415 out:
afbf30a2
JK
6416 return err;
6417}
6418
1fbfea54
ZY
6419static int wext_cipher2level(int cipher)
6420{
6421 switch (cipher) {
6422 case IW_AUTH_CIPHER_NONE:
6423 return SEC_LEVEL_0;
6424 case IW_AUTH_CIPHER_WEP40:
6425 case IW_AUTH_CIPHER_WEP104:
6426 return SEC_LEVEL_1;
6427 case IW_AUTH_CIPHER_TKIP:
6428 return SEC_LEVEL_2;
6429 case IW_AUTH_CIPHER_CCMP:
6430 return SEC_LEVEL_3;
6431 default:
6432 return -1;
6433 }
6434}
6435
afbf30a2
JK
6436/* SIOCSIWAUTH */
6437static int ipw_wx_set_auth(struct net_device *dev,
6438 struct iw_request_info *info,
6439 union iwreq_data *wrqu, char *extra)
6440{
6441 struct ipw_priv *priv = ieee80211_priv(dev);
6442 struct ieee80211_device *ieee = priv->ieee;
6443 struct iw_param *param = &wrqu->param;
6444 struct ieee80211_crypt_data *crypt;
6445 unsigned long flags;
6446 int ret = 0;
6447
6448 switch (param->flags & IW_AUTH_INDEX) {
6449 case IW_AUTH_WPA_VERSION:
1fbfea54 6450 break;
afbf30a2 6451 case IW_AUTH_CIPHER_PAIRWISE:
1fbfea54
ZY
6452 ipw_set_hw_decrypt_unicast(priv,
6453 wext_cipher2level(param->value));
6454 break;
afbf30a2 6455 case IW_AUTH_CIPHER_GROUP:
1fbfea54
ZY
6456 ipw_set_hw_decrypt_multicast(priv,
6457 wext_cipher2level(param->value));
6458 break;
afbf30a2
JK
6459 case IW_AUTH_KEY_MGMT:
6460 /*
6461 * ipw2200 does not use these parameters
6462 */
6463 break;
6464
6465 case IW_AUTH_TKIP_COUNTERMEASURES:
6466 crypt = priv->ieee->crypt[priv->ieee->tx_keyidx];
991d1cc5 6467 if (!crypt || !crypt->ops->set_flags || !crypt->ops->get_flags)
afbf30a2 6468 break;
afbf30a2
JK
6469
6470 flags = crypt->ops->get_flags(crypt->priv);
6471
6472 if (param->value)
6473 flags |= IEEE80211_CRYPTO_TKIP_COUNTERMEASURES;
6474 else
6475 flags &= ~IEEE80211_CRYPTO_TKIP_COUNTERMEASURES;
6476
6477 crypt->ops->set_flags(flags, crypt->priv);
6478
6479 break;
6480
6481 case IW_AUTH_DROP_UNENCRYPTED:{
6482 /* HACK:
6483 *
6484 * wpa_supplicant calls set_wpa_enabled when the driver
6485 * is loaded and unloaded, regardless of if WPA is being
6486 * used. No other calls are made which can be used to
6487 * determine if encryption will be used or not prior to
6488 * association being expected. If encryption is not being
6489 * used, drop_unencrypted is set to false, else true -- we
6490 * can use this to determine if the CAP_PRIVACY_ON bit should
6491 * be set.
6492 */
6493 struct ieee80211_security sec = {
6494 .flags = SEC_ENABLED,
6495 .enabled = param->value,
6496 };
6497 priv->ieee->drop_unencrypted = param->value;
6498 /* We only change SEC_LEVEL for open mode. Others
6499 * are set by ipw_wpa_set_encryption.
6500 */
6501 if (!param->value) {
6502 sec.flags |= SEC_LEVEL;
6503 sec.level = SEC_LEVEL_0;
6504 } else {
6505 sec.flags |= SEC_LEVEL;
6506 sec.level = SEC_LEVEL_1;
6507 }
6508 if (priv->ieee->set_security)
6509 priv->ieee->set_security(priv->ieee->dev, &sec);
6510 break;
6511 }
6512
6513 case IW_AUTH_80211_AUTH_ALG:
6514 ret = ipw_wpa_set_auth_algs(priv, param->value);
6515 break;
6516
6517 case IW_AUTH_WPA_ENABLED:
6518 ret = ipw_wpa_enable(priv, param->value);
e3c5a64e 6519 ipw_disassociate(priv);
afbf30a2
JK
6520 break;
6521
6522 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
6523 ieee->ieee802_1x = param->value;
6524 break;
6525
afbf30a2
JK
6526 case IW_AUTH_PRIVACY_INVOKED:
6527 ieee->privacy_invoked = param->value;
6528 break;
6529
6530 default:
6531 return -EOPNOTSUPP;
6532 }
6533 return ret;
6534}
6535
6536/* SIOCGIWAUTH */
6537static int ipw_wx_get_auth(struct net_device *dev,
6538 struct iw_request_info *info,
6539 union iwreq_data *wrqu, char *extra)
6540{
6541 struct ipw_priv *priv = ieee80211_priv(dev);
6542 struct ieee80211_device *ieee = priv->ieee;
6543 struct ieee80211_crypt_data *crypt;
6544 struct iw_param *param = &wrqu->param;
6545 int ret = 0;
6546
6547 switch (param->flags & IW_AUTH_INDEX) {
6548 case IW_AUTH_WPA_VERSION:
6549 case IW_AUTH_CIPHER_PAIRWISE:
6550 case IW_AUTH_CIPHER_GROUP:
6551 case IW_AUTH_KEY_MGMT:
6552 /*
6553 * wpa_supplicant will control these internally
6554 */
6555 ret = -EOPNOTSUPP;
6556 break;
6557
6558 case IW_AUTH_TKIP_COUNTERMEASURES:
6559 crypt = priv->ieee->crypt[priv->ieee->tx_keyidx];
991d1cc5 6560 if (!crypt || !crypt->ops->get_flags)
afbf30a2 6561 break;
afbf30a2
JK
6562
6563 param->value = (crypt->ops->get_flags(crypt->priv) &
6564 IEEE80211_CRYPTO_TKIP_COUNTERMEASURES) ? 1 : 0;
6565
6566 break;
6567
6568 case IW_AUTH_DROP_UNENCRYPTED:
6569 param->value = ieee->drop_unencrypted;
6570 break;
6571
6572 case IW_AUTH_80211_AUTH_ALG:
6573 param->value = ieee->sec.auth_mode;
6574 break;
6575
6576 case IW_AUTH_WPA_ENABLED:
6577 param->value = ieee->wpa_enabled;
6578 break;
6579
6580 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
6581 param->value = ieee->ieee802_1x;
6582 break;
6583
6584 case IW_AUTH_ROAMING_CONTROL:
6585 case IW_AUTH_PRIVACY_INVOKED:
6586 param->value = ieee->privacy_invoked;
6587 break;
6588
6589 default:
6590 return -EOPNOTSUPP;
6591 }
6592 return 0;
6593}
6594
6595/* SIOCSIWENCODEEXT */
6596static int ipw_wx_set_encodeext(struct net_device *dev,
6597 struct iw_request_info *info,
6598 union iwreq_data *wrqu, char *extra)
6599{
6600 struct ipw_priv *priv = ieee80211_priv(dev);
6601 struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
6602
6603 if (hwcrypto) {
afbf30a2 6604 if (ext->alg == IW_ENCODE_ALG_TKIP) {
567deaf6
HL
6605 /* IPW HW can't build TKIP MIC,
6606 host decryption still needed */
6607 if (ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY)
6608 priv->ieee->host_mc_decrypt = 1;
6609 else {
6610 priv->ieee->host_encrypt = 0;
6611 priv->ieee->host_encrypt_msdu = 1;
6612 priv->ieee->host_decrypt = 1;
6613 }
afbf30a2
JK
6614 } else {
6615 priv->ieee->host_encrypt = 0;
6616 priv->ieee->host_encrypt_msdu = 0;
6617 priv->ieee->host_decrypt = 0;
567deaf6 6618 priv->ieee->host_mc_decrypt = 0;
afbf30a2
JK
6619 }
6620 }
6621
6622 return ieee80211_wx_set_encodeext(priv->ieee, info, wrqu, extra);
6623}
6624
6625/* SIOCGIWENCODEEXT */
6626static int ipw_wx_get_encodeext(struct net_device *dev,
6627 struct iw_request_info *info,
6628 union iwreq_data *wrqu, char *extra)
6629{
6630 struct ipw_priv *priv = ieee80211_priv(dev);
6631 return ieee80211_wx_get_encodeext(priv->ieee, info, wrqu, extra);
6632}
6633
6634/* SIOCSIWMLME */
6635static int ipw_wx_set_mlme(struct net_device *dev,
6636 struct iw_request_info *info,
6637 union iwreq_data *wrqu, char *extra)
6638{
6639 struct ipw_priv *priv = ieee80211_priv(dev);
6640 struct iw_mlme *mlme = (struct iw_mlme *)extra;
6641 u16 reason;
6642
6643 reason = cpu_to_le16(mlme->reason_code);
6644
6645 switch (mlme->cmd) {
6646 case IW_MLME_DEAUTH:
67fd6b45 6647 /* silently ignore */
afbf30a2
JK
6648 break;
6649
6650 case IW_MLME_DISASSOC:
6651 ipw_disassociate(priv);
6652 break;
6653
6654 default:
6655 return -EOPNOTSUPP;
6656 }
6657 return 0;
6658}
afbf30a2 6659
e43e3c1e 6660#ifdef CONFIG_IPW2200_QOS
afbf30a2
JK
6661
6662/* QoS */
6663/*
6664* get the modulation type of the current network or
6665* the card current mode
6666*/
53d0bcf8 6667static u8 ipw_qos_current_mode(struct ipw_priv * priv)
afbf30a2
JK
6668{
6669 u8 mode = 0;
6670
6671 if (priv->status & STATUS_ASSOCIATED) {
6672 unsigned long flags;
6673
6674 spin_lock_irqsave(&priv->ieee->lock, flags);
6675 mode = priv->assoc_network->mode;
6676 spin_unlock_irqrestore(&priv->ieee->lock, flags);
6677 } else {
6678 mode = priv->ieee->mode;
6679 }
6680 IPW_DEBUG_QOS("QoS network/card mode %d \n", mode);
6681 return mode;
b095c381 6682}
ea2b26e0 6683
b095c381
JK
6684/*
6685* Handle management frame beacon and probe response
6686*/
3b9990cb
JK
6687static int ipw_qos_handle_probe_response(struct ipw_priv *priv,
6688 int active_network,
6689 struct ieee80211_network *network)
b095c381
JK
6690{
6691 u32 size = sizeof(struct ieee80211_qos_parameters);
6692
afbf30a2 6693 if (network->capability & WLAN_CAPABILITY_IBSS)
b095c381
JK
6694 network->qos_data.active = network->qos_data.supported;
6695
6696 if (network->flags & NETWORK_HAS_QOS_MASK) {
afbf30a2
JK
6697 if (active_network &&
6698 (network->flags & NETWORK_HAS_QOS_PARAMETERS))
b095c381
JK
6699 network->qos_data.active = network->qos_data.supported;
6700
6701 if ((network->qos_data.active == 1) && (active_network == 1) &&
6702 (network->flags & NETWORK_HAS_QOS_PARAMETERS) &&
6703 (network->qos_data.old_param_count !=
6704 network->qos_data.param_count)) {
6705 network->qos_data.old_param_count =
6706 network->qos_data.param_count;
6707 schedule_work(&priv->qos_activate);
afbf30a2
JK
6708 IPW_DEBUG_QOS("QoS parameters change call "
6709 "qos_activate\n");
b095c381 6710 }
ea2b26e0 6711 } else {
afbf30a2
JK
6712 if ((priv->ieee->mode == IEEE_B) || (network->mode == IEEE_B))
6713 memcpy(&network->qos_data.parameters,
b095c381 6714 &def_parameters_CCK, size);
afbf30a2
JK
6715 else
6716 memcpy(&network->qos_data.parameters,
b095c381 6717 &def_parameters_OFDM, size);
afbf30a2 6718
b095c381
JK
6719 if ((network->qos_data.active == 1) && (active_network == 1)) {
6720 IPW_DEBUG_QOS("QoS was disabled call qos_activate \n");
6721 schedule_work(&priv->qos_activate);
6722 }
6723
6724 network->qos_data.active = 0;
6725 network->qos_data.supported = 0;
ea2b26e0 6726 }
afbf30a2
JK
6727 if ((priv->status & STATUS_ASSOCIATED) &&
6728 (priv->ieee->iw_mode == IW_MODE_ADHOC) && (active_network == 0)) {
6729 if (memcmp(network->bssid, priv->bssid, ETH_ALEN))
6730 if ((network->capability & WLAN_CAPABILITY_IBSS) &&
6731 !(network->flags & NETWORK_EMPTY_ESSID))
b095c381 6732 if ((network->ssid_len ==
afbf30a2
JK
6733 priv->assoc_network->ssid_len) &&
6734 !memcmp(network->ssid,
6735 priv->assoc_network->ssid,
6736 network->ssid_len)) {
b095c381
JK
6737 queue_work(priv->workqueue,
6738 &priv->merge_networks);
6739 }
b095c381 6740 }
ea2b26e0 6741
b095c381
JK
6742 return 0;
6743}
6744
6745/*
6746* This function set up the firmware to support QoS. It sends
6747* IPW_CMD_QOS_PARAMETERS and IPW_CMD_WME_INFO
6748*/
6749static int ipw_qos_activate(struct ipw_priv *priv,
6750 struct ieee80211_qos_data *qos_network_data)
6751{
6752 int err;
6753 struct ieee80211_qos_parameters qos_parameters[QOS_QOS_SETS];
6754 struct ieee80211_qos_parameters *active_one = NULL;
6755 u32 size = sizeof(struct ieee80211_qos_parameters);
6756 u32 burst_duration;
6757 int i;
6758 u8 type;
6759
6760 type = ipw_qos_current_mode(priv);
6761
6762 active_one = &(qos_parameters[QOS_PARAM_SET_DEF_CCK]);
6763 memcpy(active_one, priv->qos_data.def_qos_parm_CCK, size);
6764 active_one = &(qos_parameters[QOS_PARAM_SET_DEF_OFDM]);
6765 memcpy(active_one, priv->qos_data.def_qos_parm_OFDM, size);
6766
6767 if (qos_network_data == NULL) {
6768 if (type == IEEE_B) {
6769 IPW_DEBUG_QOS("QoS activate network mode %d\n", type);
6770 active_one = &def_parameters_CCK;
6771 } else
6772 active_one = &def_parameters_OFDM;
6773
afbf30a2 6774 memcpy(&qos_parameters[QOS_PARAM_SET_ACTIVE], active_one, size);
b095c381
JK
6775 burst_duration = ipw_qos_get_burst_duration(priv);
6776 for (i = 0; i < QOS_QUEUE_NUM; i++)
afbf30a2 6777 qos_parameters[QOS_PARAM_SET_ACTIVE].tx_op_limit[i] =
851ca268 6778 (u16)burst_duration;
afbf30a2 6779 } else if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
b095c381
JK
6780 if (type == IEEE_B) {
6781 IPW_DEBUG_QOS("QoS activate IBSS nework mode %d\n",
6782 type);
6783 if (priv->qos_data.qos_enable == 0)
6784 active_one = &def_parameters_CCK;
6785 else
6786 active_one = priv->qos_data.def_qos_parm_CCK;
6787 } else {
6788 if (priv->qos_data.qos_enable == 0)
6789 active_one = &def_parameters_OFDM;
6790 else
6791 active_one = priv->qos_data.def_qos_parm_OFDM;
6792 }
afbf30a2 6793 memcpy(&qos_parameters[QOS_PARAM_SET_ACTIVE], active_one, size);
b095c381
JK
6794 } else {
6795 unsigned long flags;
6796 int active;
6797
6798 spin_lock_irqsave(&priv->ieee->lock, flags);
6799 active_one = &(qos_network_data->parameters);
6800 qos_network_data->old_param_count =
6801 qos_network_data->param_count;
afbf30a2 6802 memcpy(&qos_parameters[QOS_PARAM_SET_ACTIVE], active_one, size);
b095c381
JK
6803 active = qos_network_data->supported;
6804 spin_unlock_irqrestore(&priv->ieee->lock, flags);
6805
6806 if (active == 0) {
6807 burst_duration = ipw_qos_get_burst_duration(priv);
6808 for (i = 0; i < QOS_QUEUE_NUM; i++)
6809 qos_parameters[QOS_PARAM_SET_ACTIVE].
851ca268 6810 tx_op_limit[i] = (u16)burst_duration;
b095c381
JK
6811 }
6812 }
6813
6814 IPW_DEBUG_QOS("QoS sending IPW_CMD_QOS_PARAMETERS\n");
851ca268
ZY
6815 for (i = 0; i < 3; i++) {
6816 int j;
6817 for (j = 0; j < QOS_QUEUE_NUM; j++) {
6818 qos_parameters[i].cw_min[j] = cpu_to_le16(qos_parameters[i].cw_min[j]);
6819 qos_parameters[i].cw_max[j] = cpu_to_le16(qos_parameters[i].cw_max[j]);
6820 qos_parameters[i].tx_op_limit[j] = cpu_to_le16(qos_parameters[i].tx_op_limit[j]);
6821 }
6822 }
6823
afbf30a2
JK
6824 err = ipw_send_qos_params_command(priv,
6825 (struct ieee80211_qos_parameters *)
6826 &(qos_parameters[0]));
b095c381
JK
6827 if (err)
6828 IPW_DEBUG_QOS("QoS IPW_CMD_QOS_PARAMETERS failed\n");
6829
6830 return err;
6831}
6832
6833/*
6834* send IPW_CMD_WME_INFO to the firmware
6835*/
6836static int ipw_qos_set_info_element(struct ipw_priv *priv)
6837{
6838 int ret = 0;
6839 struct ieee80211_qos_information_element qos_info;
6840
6841 if (priv == NULL)
6842 return -1;
6843
6844 qos_info.elementID = QOS_ELEMENT_ID;
6845 qos_info.length = sizeof(struct ieee80211_qos_information_element) - 2;
6846
6847 qos_info.version = QOS_VERSION_1;
6848 qos_info.ac_info = 0;
6849
6850 memcpy(qos_info.qui, qos_oui, QOS_OUI_LEN);
6851 qos_info.qui_type = QOS_OUI_TYPE;
6852 qos_info.qui_subtype = QOS_OUI_INFO_SUB_TYPE;
6853
6854 ret = ipw_send_qos_info_command(priv, &qos_info);
6855 if (ret != 0) {
6856 IPW_DEBUG_QOS("QoS error calling ipw_send_qos_info_command\n");
6857 }
6858 return ret;
6859}
6860
6861/*
6862* Set the QoS parameter with the association request structure
6863*/
6864static int ipw_qos_association(struct ipw_priv *priv,
6865 struct ieee80211_network *network)
6866{
6867 int err = 0;
6868 struct ieee80211_qos_data *qos_data = NULL;
6869 struct ieee80211_qos_data ibss_data = {
6870 .supported = 1,
6871 .active = 1,
6872 };
6873
6874 switch (priv->ieee->iw_mode) {
6875 case IW_MODE_ADHOC:
5d9428de 6876 BUG_ON(!(network->capability & WLAN_CAPABILITY_IBSS));
b095c381
JK
6877
6878 qos_data = &ibss_data;
6879 break;
6880
6881 case IW_MODE_INFRA:
6882 qos_data = &network->qos_data;
6883 break;
6884
6885 default:
6886 BUG();
6887 break;
6888 }
6889
6890 err = ipw_qos_activate(priv, qos_data);
6891 if (err) {
6892 priv->assoc_request.policy_support &= ~HC_QOS_SUPPORT_ASSOC;
6893 return err;
6894 }
6895
6896 if (priv->qos_data.qos_enable && qos_data->supported) {
6897 IPW_DEBUG_QOS("QoS will be enabled for this association\n");
6898 priv->assoc_request.policy_support |= HC_QOS_SUPPORT_ASSOC;
6899 return ipw_qos_set_info_element(priv);
6900 }
6901
6902 return 0;
6903}
6904
6905/*
6906* handling the beaconing responces. if we get different QoS setting
6907* of the network from the the associated setting adjust the QoS
6908* setting
6909*/
6910static int ipw_qos_association_resp(struct ipw_priv *priv,
6911 struct ieee80211_network *network)
6912{
6913 int ret = 0;
6914 unsigned long flags;
6915 u32 size = sizeof(struct ieee80211_qos_parameters);
6916 int set_qos_param = 0;
6917
afbf30a2
JK
6918 if ((priv == NULL) || (network == NULL) ||
6919 (priv->assoc_network == NULL))
b095c381
JK
6920 return ret;
6921
6922 if (!(priv->status & STATUS_ASSOCIATED))
6923 return ret;
6924
afbf30a2 6925 if ((priv->ieee->iw_mode != IW_MODE_INFRA))
b095c381 6926 return ret;
b095c381
JK
6927
6928 spin_lock_irqsave(&priv->ieee->lock, flags);
6929 if (network->flags & NETWORK_HAS_QOS_PARAMETERS) {
afbf30a2 6930 memcpy(&priv->assoc_network->qos_data, &network->qos_data,
b095c381
JK
6931 sizeof(struct ieee80211_qos_data));
6932 priv->assoc_network->qos_data.active = 1;
6933 if ((network->qos_data.old_param_count !=
6934 network->qos_data.param_count)) {
6935 set_qos_param = 1;
6936 network->qos_data.old_param_count =
6937 network->qos_data.param_count;
6938 }
6939
6940 } else {
afbf30a2
JK
6941 if ((network->mode == IEEE_B) || (priv->ieee->mode == IEEE_B))
6942 memcpy(&priv->assoc_network->qos_data.parameters,
b095c381 6943 &def_parameters_CCK, size);
afbf30a2
JK
6944 else
6945 memcpy(&priv->assoc_network->qos_data.parameters,
b095c381 6946 &def_parameters_OFDM, size);
b095c381
JK
6947 priv->assoc_network->qos_data.active = 0;
6948 priv->assoc_network->qos_data.supported = 0;
6949 set_qos_param = 1;
6950 }
6951
6952 spin_unlock_irqrestore(&priv->ieee->lock, flags);
6953
6954 if (set_qos_param == 1)
6955 schedule_work(&priv->qos_activate);
6956
6957 return ret;
6958}
6959
6960static u32 ipw_qos_get_burst_duration(struct ipw_priv *priv)
6961{
6962 u32 ret = 0;
6963
6964 if ((priv == NULL))
6965 return 0;
6966
afbf30a2 6967 if (!(priv->ieee->modulation & IEEE80211_OFDM_MODULATION))
b095c381 6968 ret = priv->qos_data.burst_duration_CCK;
afbf30a2 6969 else
b095c381 6970 ret = priv->qos_data.burst_duration_OFDM;
afbf30a2 6971
b095c381
JK
6972 return ret;
6973}
6974
6975/*
6976* Initialize the setting of QoS global
6977*/
6978static void ipw_qos_init(struct ipw_priv *priv, int enable,
6979 int burst_enable, u32 burst_duration_CCK,
6980 u32 burst_duration_OFDM)
6981{
6982 priv->qos_data.qos_enable = enable;
6983
6984 if (priv->qos_data.qos_enable) {
6985 priv->qos_data.def_qos_parm_CCK = &def_qos_parameters_CCK;
6986 priv->qos_data.def_qos_parm_OFDM = &def_qos_parameters_OFDM;
6987 IPW_DEBUG_QOS("QoS is enabled\n");
6988 } else {
6989 priv->qos_data.def_qos_parm_CCK = &def_parameters_CCK;
6990 priv->qos_data.def_qos_parm_OFDM = &def_parameters_OFDM;
6991 IPW_DEBUG_QOS("QoS is not enabled\n");
6992 }
6993
6994 priv->qos_data.burst_enable = burst_enable;
6995
6996 if (burst_enable) {
6997 priv->qos_data.burst_duration_CCK = burst_duration_CCK;
6998 priv->qos_data.burst_duration_OFDM = burst_duration_OFDM;
6999 } else {
7000 priv->qos_data.burst_duration_CCK = 0;
7001 priv->qos_data.burst_duration_OFDM = 0;
7002 }
7003}
7004
7005/*
7006* map the packet priority to the right TX Queue
7007*/
7008static int ipw_get_tx_queue_number(struct ipw_priv *priv, u16 priority)
7009{
7010 if (priority > 7 || !priv->qos_data.qos_enable)
7011 priority = 0;
7012
7013 return from_priority_to_tx_queue[priority] - 1;
7014}
7015
a5cf4fe6
ZY
7016static int ipw_is_qos_active(struct net_device *dev,
7017 struct sk_buff *skb)
b095c381 7018{
a5cf4fe6 7019 struct ipw_priv *priv = ieee80211_priv(dev);
b095c381
JK
7020 struct ieee80211_qos_data *qos_data = NULL;
7021 int active, supported;
a5cf4fe6
ZY
7022 u8 *daddr = skb->data + ETH_ALEN;
7023 int unicast = !is_multicast_ether_addr(daddr);
b095c381
JK
7024
7025 if (!(priv->status & STATUS_ASSOCIATED))
7026 return 0;
7027
7028 qos_data = &priv->assoc_network->qos_data;
7029
b095c381
JK
7030 if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
7031 if (unicast == 0)
7032 qos_data->active = 0;
7033 else
7034 qos_data->active = qos_data->supported;
7035 }
b095c381
JK
7036 active = qos_data->active;
7037 supported = qos_data->supported;
afbf30a2
JK
7038 IPW_DEBUG_QOS("QoS %d network is QoS active %d supported %d "
7039 "unicast %d\n",
7040 priv->qos_data.qos_enable, active, supported, unicast);
a5cf4fe6
ZY
7041 if (active && priv->qos_data.qos_enable)
7042 return 1;
b095c381 7043
a5cf4fe6
ZY
7044 return 0;
7045
7046}
7047/*
7048* add QoS parameter to the TX command
7049*/
7050static int ipw_qos_set_tx_queue_command(struct ipw_priv *priv,
7051 u16 priority,
7052 struct tfd_data *tfd)
7053{
7054 int tx_queue_id = 0;
7055
7056
7057 tx_queue_id = from_priority_to_tx_queue[priority] - 1;
7058 tfd->tx_flags_ext |= DCT_FLAG_EXT_QOS_ENABLED;
7059
7060 if (priv->qos_data.qos_no_ack_mask & (1UL << tx_queue_id)) {
7061 tfd->tx_flags &= ~DCT_FLAG_ACK_REQD;
851ca268 7062 tfd->tfd.tfd_26.mchdr.qos_ctrl |= cpu_to_le16(CTRL_QOS_NO_ACK);
a5cf4fe6
ZY
7063 }
7064 return 0;
b095c381
JK
7065}
7066
7067/*
7068* background support to run QoS activate functionality
7069*/
7070static void ipw_bg_qos_activate(void *data)
7071{
7072 struct ipw_priv *priv = data;
7073
7074 if (priv == NULL)
7075 return;
7076
4644151b 7077 mutex_lock(&priv->mutex);
b095c381
JK
7078
7079 if (priv->status & STATUS_ASSOCIATED)
7080 ipw_qos_activate(priv, &(priv->assoc_network->qos_data));
7081
4644151b 7082 mutex_unlock(&priv->mutex);
b095c381
JK
7083}
7084
3b9990cb
JK
7085static int ipw_handle_probe_response(struct net_device *dev,
7086 struct ieee80211_probe_response *resp,
7087 struct ieee80211_network *network)
b095c381
JK
7088{
7089 struct ipw_priv *priv = ieee80211_priv(dev);
3b9990cb
JK
7090 int active_network = ((priv->status & STATUS_ASSOCIATED) &&
7091 (network == priv->assoc_network));
43f66a6c 7092
3b9990cb 7093 ipw_qos_handle_probe_response(priv, active_network, network);
43f66a6c 7094
3b9990cb
JK
7095 return 0;
7096}
43f66a6c 7097
3b9990cb
JK
7098static int ipw_handle_beacon(struct net_device *dev,
7099 struct ieee80211_beacon *resp,
7100 struct ieee80211_network *network)
7101{
7102 struct ipw_priv *priv = ieee80211_priv(dev);
7103 int active_network = ((priv->status & STATUS_ASSOCIATED) &&
7104 (network == priv->assoc_network));
bf79451e 7105
3b9990cb 7106 ipw_qos_handle_probe_response(priv, active_network, network);
bf79451e 7107
b095c381
JK
7108 return 0;
7109}
bf79451e 7110
3b9990cb
JK
7111static int ipw_handle_assoc_response(struct net_device *dev,
7112 struct ieee80211_assoc_response *resp,
7113 struct ieee80211_network *network)
7114{
7115 struct ipw_priv *priv = ieee80211_priv(dev);
7116 ipw_qos_association_resp(priv, network);
7117 return 0;
7118}
43f66a6c 7119
b095c381
JK
7120static int ipw_send_qos_params_command(struct ipw_priv *priv, struct ieee80211_qos_parameters
7121 *qos_param)
7122{
4e22699f
ZY
7123 return ipw_send_cmd_pdu(priv, IPW_CMD_QOS_PARAMETERS,
7124 sizeof(*qos_param) * 3, qos_param);
b095c381
JK
7125}
7126
7127static int ipw_send_qos_info_command(struct ipw_priv *priv, struct ieee80211_qos_information_element
7128 *qos_param)
7129{
4e22699f
ZY
7130 return ipw_send_cmd_pdu(priv, IPW_CMD_WME_INFO, sizeof(*qos_param),
7131 qos_param);
43f66a6c
JK
7132}
7133
e43e3c1e 7134#endif /* CONFIG_IPW2200_QOS */
b095c381 7135
43f66a6c
JK
7136static int ipw_associate_network(struct ipw_priv *priv,
7137 struct ieee80211_network *network,
0edd5b44 7138 struct ipw_supported_rates *rates, int roaming)
43f66a6c
JK
7139{
7140 int err;
7141
7142 if (priv->config & CFG_FIXED_RATE)
b095c381 7143 ipw_set_fixed_rate(priv, network->mode);
43f66a6c
JK
7144
7145 if (!(priv->config & CFG_STATIC_ESSID)) {
bf79451e 7146 priv->essid_len = min(network->ssid_len,
0edd5b44 7147 (u8) IW_ESSID_MAX_SIZE);
43f66a6c
JK
7148 memcpy(priv->essid, network->ssid, priv->essid_len);
7149 }
7150
7151 network->last_associate = jiffies;
7152
7153 memset(&priv->assoc_request, 0, sizeof(priv->assoc_request));
7154 priv->assoc_request.channel = network->channel;
3e234b4e
ZY
7155 priv->assoc_request.auth_key = 0;
7156
43f66a6c 7157 if ((priv->capability & CAP_PRIVACY_ON) &&
3e234b4e 7158 (priv->ieee->sec.auth_mode == WLAN_AUTH_SHARED_KEY)) {
43f66a6c 7159 priv->assoc_request.auth_type = AUTH_SHARED_KEY;
b095c381
JK
7160 priv->assoc_request.auth_key = priv->ieee->sec.active_key;
7161
1ba61e05 7162 if (priv->ieee->sec.level == SEC_LEVEL_1)
b095c381 7163 ipw_send_wep_keys(priv, DCW_WEP_KEY_SEC_TYPE_WEP);
3e234b4e
ZY
7164
7165 } else if ((priv->capability & CAP_PRIVACY_ON) &&
7166 (priv->ieee->sec.auth_mode == WLAN_AUTH_LEAP))
7167 priv->assoc_request.auth_type = AUTH_LEAP;
7168 else
43f66a6c 7169 priv->assoc_request.auth_type = AUTH_OPEN;
43f66a6c 7170
b095c381 7171 if (priv->ieee->wpa_ie_len) {
ea2b26e0
JK
7172 priv->assoc_request.policy_support = 0x02; /* RSN active */
7173 ipw_set_rsn_capa(priv, priv->ieee->wpa_ie,
7174 priv->ieee->wpa_ie_len);
7175 }
43f66a6c 7176
bf79451e
JG
7177 /*
7178 * It is valid for our ieee device to support multiple modes, but
7179 * when it comes to associating to a given network we have to choose
43f66a6c
JK
7180 * just one mode.
7181 */
7182 if (network->mode & priv->ieee->mode & IEEE_A)
7183 priv->assoc_request.ieee_mode = IPW_A_MODE;
7184 else if (network->mode & priv->ieee->mode & IEEE_G)
7185 priv->assoc_request.ieee_mode = IPW_G_MODE;
7186 else if (network->mode & priv->ieee->mode & IEEE_B)
7187 priv->assoc_request.ieee_mode = IPW_B_MODE;
7188
ea2b26e0
JK
7189 priv->assoc_request.capability = network->capability;
7190 if ((network->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
7191 && !(priv->config & CFG_PREAMBLE_LONG)) {
7192 priv->assoc_request.preamble_length = DCT_FLAG_SHORT_PREAMBLE;
7193 } else {
7194 priv->assoc_request.preamble_length = DCT_FLAG_LONG_PREAMBLE;
7195
7196 /* Clear the short preamble if we won't be supporting it */
7197 priv->assoc_request.capability &=
7198 ~WLAN_CAPABILITY_SHORT_PREAMBLE;
7199 }
7200
afbf30a2
JK
7201 /* Clear capability bits that aren't used in Ad Hoc */
7202 if (priv->ieee->iw_mode == IW_MODE_ADHOC)
7203 priv->assoc_request.capability &=
7204 ~WLAN_CAPABILITY_SHORT_SLOT_TIME;
7205
43f66a6c 7206 IPW_DEBUG_ASSOC("%sssocation attempt: '%s', channel %d, "
ea2b26e0 7207 "802.11%c [%d], %s[:%s], enc=%s%s%s%c%c\n",
43f66a6c 7208 roaming ? "Rea" : "A",
bf79451e
JG
7209 escape_essid(priv->essid, priv->essid_len),
7210 network->channel,
7211 ipw_modes[priv->assoc_request.ieee_mode],
7212 rates->num_rates,
ea2b26e0
JK
7213 (priv->assoc_request.preamble_length ==
7214 DCT_FLAG_LONG_PREAMBLE) ? "long" : "short",
7215 network->capability &
7216 WLAN_CAPABILITY_SHORT_PREAMBLE ? "short" : "long",
43f66a6c 7217 priv->capability & CAP_PRIVACY_ON ? "on " : "off",
bf79451e
JG
7218 priv->capability & CAP_PRIVACY_ON ?
7219 (priv->capability & CAP_SHARED_KEY ? "(shared)" :
43f66a6c
JK
7220 "(open)") : "",
7221 priv->capability & CAP_PRIVACY_ON ? " key=" : "",
bf79451e 7222 priv->capability & CAP_PRIVACY_ON ?
b095c381 7223 '1' + priv->ieee->sec.active_key : '.',
0edd5b44 7224 priv->capability & CAP_PRIVACY_ON ? '.' : ' ');
43f66a6c
JK
7225
7226 priv->assoc_request.beacon_interval = network->beacon_interval;
7227 if ((priv->ieee->iw_mode == IW_MODE_ADHOC) &&
0edd5b44 7228 (network->time_stamp[0] == 0) && (network->time_stamp[1] == 0)) {
43f66a6c
JK
7229 priv->assoc_request.assoc_type = HC_IBSS_START;
7230 priv->assoc_request.assoc_tsf_msw = 0;
7231 priv->assoc_request.assoc_tsf_lsw = 0;
7232 } else {
7233 if (unlikely(roaming))
7234 priv->assoc_request.assoc_type = HC_REASSOCIATE;
7235 else
7236 priv->assoc_request.assoc_type = HC_ASSOCIATE;
7237 priv->assoc_request.assoc_tsf_msw = network->time_stamp[1];
7238 priv->assoc_request.assoc_tsf_lsw = network->time_stamp[0];
7239 }
7240
afbf30a2 7241 memcpy(priv->assoc_request.bssid, network->bssid, ETH_ALEN);
43f66a6c
JK
7242
7243 if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
7244 memset(&priv->assoc_request.dest, 0xFF, ETH_ALEN);
7245 priv->assoc_request.atim_window = network->atim_window;
7246 } else {
afbf30a2 7247 memcpy(priv->assoc_request.dest, network->bssid, ETH_ALEN);
43f66a6c
JK
7248 priv->assoc_request.atim_window = 0;
7249 }
7250
43f66a6c 7251 priv->assoc_request.listen_interval = network->listen_interval;
bf79451e 7252
43f66a6c
JK
7253 err = ipw_send_ssid(priv, priv->essid, priv->essid_len);
7254 if (err) {
7255 IPW_DEBUG_HC("Attempt to send SSID command failed.\n");
7256 return err;
7257 }
7258
7259 rates->ieee_mode = priv->assoc_request.ieee_mode;
7260 rates->purpose = IPW_RATE_CONNECT;
7261 ipw_send_supported_rates(priv, rates);
bf79451e 7262
43f66a6c
JK
7263 if (priv->assoc_request.ieee_mode == IPW_G_MODE)
7264 priv->sys_config.dot11g_auto_detection = 1;
7265 else
7266 priv->sys_config.dot11g_auto_detection = 0;
c848d0af
JK
7267
7268 if (priv->ieee->iw_mode == IW_MODE_ADHOC)
7269 priv->sys_config.answer_broadcast_ssid_probe = 1;
7270 else
7271 priv->sys_config.answer_broadcast_ssid_probe = 0;
7272
d685b8c2 7273 err = ipw_send_system_config(priv);
43f66a6c
JK
7274 if (err) {
7275 IPW_DEBUG_HC("Attempt to send sys config command failed.\n");
7276 return err;
7277 }
bf79451e 7278
43f66a6c 7279 IPW_DEBUG_ASSOC("Association sensitivity: %d\n", network->stats.rssi);
ea2b26e0 7280 err = ipw_set_sensitivity(priv, network->stats.rssi + IPW_RSSI_TO_DBM);
43f66a6c
JK
7281 if (err) {
7282 IPW_DEBUG_HC("Attempt to send associate command failed.\n");
7283 return err;
7284 }
7285
7286 /*
7287 * If preemption is enabled, it is possible for the association
7288 * to complete before we return from ipw_send_associate. Therefore
7289 * we have to be sure and update our priviate data first.
7290 */
7291 priv->channel = network->channel;
7292 memcpy(priv->bssid, network->bssid, ETH_ALEN);
bf79451e 7293 priv->status |= STATUS_ASSOCIATING;
43f66a6c
JK
7294 priv->status &= ~STATUS_SECURITY_UPDATED;
7295
7296 priv->assoc_network = network;
7297
e43e3c1e 7298#ifdef CONFIG_IPW2200_QOS
b095c381
JK
7299 ipw_qos_association(priv, network);
7300#endif
7301
43f66a6c
JK
7302 err = ipw_send_associate(priv, &priv->assoc_request);
7303 if (err) {
7304 IPW_DEBUG_HC("Attempt to send associate command failed.\n");
7305 return err;
7306 }
bf79451e
JG
7307
7308 IPW_DEBUG(IPW_DL_STATE, "associating: '%s' " MAC_FMT " \n",
43f66a6c
JK
7309 escape_essid(priv->essid, priv->essid_len),
7310 MAC_ARG(priv->bssid));
7311
7312 return 0;
7313}
7314
7315static void ipw_roam(void *data)
7316{
7317 struct ipw_priv *priv = data;
7318 struct ieee80211_network *network = NULL;
7319 struct ipw_network_match match = {
7320 .network = priv->assoc_network
7321 };
7322
7323 /* The roaming process is as follows:
bf79451e
JG
7324 *
7325 * 1. Missed beacon threshold triggers the roaming process by
43f66a6c
JK
7326 * setting the status ROAM bit and requesting a scan.
7327 * 2. When the scan completes, it schedules the ROAM work
7328 * 3. The ROAM work looks at all of the known networks for one that
7329 * is a better network than the currently associated. If none
7330 * found, the ROAM process is over (ROAM bit cleared)
7331 * 4. If a better network is found, a disassociation request is
7332 * sent.
7333 * 5. When the disassociation completes, the roam work is again
7334 * scheduled. The second time through, the driver is no longer
7335 * associated, and the newly selected network is sent an
bf79451e 7336 * association request.
43f66a6c
JK
7337 * 6. At this point ,the roaming process is complete and the ROAM
7338 * status bit is cleared.
7339 */
7340
7341 /* If we are no longer associated, and the roaming bit is no longer
7342 * set, then we are not actively roaming, so just return */
7343 if (!(priv->status & (STATUS_ASSOCIATED | STATUS_ROAMING)))
7344 return;
bf79451e 7345
43f66a6c 7346 if (priv->status & STATUS_ASSOCIATED) {
bf79451e 7347 /* First pass through ROAM process -- look for a better
43f66a6c 7348 * network */
a613bffd 7349 unsigned long flags;
43f66a6c
JK
7350 u8 rssi = priv->assoc_network->stats.rssi;
7351 priv->assoc_network->stats.rssi = -128;
a613bffd 7352 spin_lock_irqsave(&priv->ieee->lock, flags);
43f66a6c
JK
7353 list_for_each_entry(network, &priv->ieee->network_list, list) {
7354 if (network != priv->assoc_network)
7355 ipw_best_network(priv, &match, network, 1);
7356 }
a613bffd 7357 spin_unlock_irqrestore(&priv->ieee->lock, flags);
43f66a6c 7358 priv->assoc_network->stats.rssi = rssi;
bf79451e 7359
43f66a6c
JK
7360 if (match.network == priv->assoc_network) {
7361 IPW_DEBUG_ASSOC("No better APs in this network to "
7362 "roam to.\n");
7363 priv->status &= ~STATUS_ROAMING;
7364 ipw_debug_config(priv);
7365 return;
7366 }
bf79451e 7367
43f66a6c
JK
7368 ipw_send_disassociate(priv, 1);
7369 priv->assoc_network = match.network;
7370
7371 return;
bf79451e 7372 }
43f66a6c
JK
7373
7374 /* Second pass through ROAM process -- request association */
7375 ipw_compatible_rates(priv, priv->assoc_network, &match.rates);
7376 ipw_associate_network(priv, priv->assoc_network, &match.rates, 1);
7377 priv->status &= ~STATUS_ROAMING;
7378}
7379
c848d0af
JK
7380static void ipw_bg_roam(void *data)
7381{
7382 struct ipw_priv *priv = data;
4644151b 7383 mutex_lock(&priv->mutex);
c848d0af 7384 ipw_roam(data);
4644151b 7385 mutex_unlock(&priv->mutex);
c848d0af
JK
7386}
7387
7388static int ipw_associate(void *data)
43f66a6c
JK
7389{
7390 struct ipw_priv *priv = data;
7391
7392 struct ieee80211_network *network = NULL;
7393 struct ipw_network_match match = {
7394 .network = NULL
7395 };
7396 struct ipw_supported_rates *rates;
7397 struct list_head *element;
a613bffd 7398 unsigned long flags;
43f66a6c 7399
b095c381
JK
7400 if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
7401 IPW_DEBUG_ASSOC("Not attempting association (monitor mode)\n");
7402 return 0;
7403 }
7404
c848d0af 7405 if (priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) {
afbf30a2
JK
7406 IPW_DEBUG_ASSOC("Not attempting association (already in "
7407 "progress)\n");
c848d0af
JK
7408 return 0;
7409 }
7410
e6324726
HL
7411 if (priv->status & STATUS_DISASSOCIATING) {
7412 IPW_DEBUG_ASSOC("Not attempting association (in "
7413 "disassociating)\n ");
7414 queue_work(priv->workqueue, &priv->associate);
7415 return 0;
7416 }
7417
c848d0af 7418 if (!ipw_is_init(priv) || (priv->status & STATUS_SCANNING)) {
afbf30a2
JK
7419 IPW_DEBUG_ASSOC("Not attempting association (scanning or not "
7420 "initialized)\n");
c848d0af
JK
7421 return 0;
7422 }
43f66a6c
JK
7423
7424 if (!(priv->config & CFG_ASSOCIATE) &&
7425 !(priv->config & (CFG_STATIC_ESSID |
0edd5b44 7426 CFG_STATIC_CHANNEL | CFG_STATIC_BSSID))) {
43f66a6c 7427 IPW_DEBUG_ASSOC("Not attempting association (associate=0)\n");
c848d0af 7428 return 0;
43f66a6c
JK
7429 }
7430
a613bffd
JK
7431 /* Protect our use of the network_list */
7432 spin_lock_irqsave(&priv->ieee->lock, flags);
bf79451e 7433 list_for_each_entry(network, &priv->ieee->network_list, list)
0edd5b44 7434 ipw_best_network(priv, &match, network, 0);
43f66a6c
JK
7435
7436 network = match.network;
7437 rates = &match.rates;
7438
7439 if (network == NULL &&
7440 priv->ieee->iw_mode == IW_MODE_ADHOC &&
7441 priv->config & CFG_ADHOC_CREATE &&
7442 priv->config & CFG_STATIC_ESSID &&
a613bffd 7443 priv->config & CFG_STATIC_CHANNEL &&
43f66a6c
JK
7444 !list_empty(&priv->ieee->network_free_list)) {
7445 element = priv->ieee->network_free_list.next;
0edd5b44 7446 network = list_entry(element, struct ieee80211_network, list);
43f66a6c
JK
7447 ipw_adhoc_create(priv, network);
7448 rates = &priv->rates;
7449 list_del(element);
7450 list_add_tail(&network->list, &priv->ieee->network_list);
7451 }
a613bffd 7452 spin_unlock_irqrestore(&priv->ieee->lock, flags);
bf79451e 7453
43f66a6c
JK
7454 /* If we reached the end of the list, then we don't have any valid
7455 * matching APs */
7456 if (!network) {
7457 ipw_debug_config(priv);
7458
b095c381
JK
7459 if (!(priv->status & STATUS_SCANNING)) {
7460 if (!(priv->config & CFG_SPEED_SCAN))
7461 queue_delayed_work(priv->workqueue,
7462 &priv->request_scan,
7463 SCAN_INTERVAL);
7464 else
7465 queue_work(priv->workqueue,
7466 &priv->request_scan);
7467 }
bf79451e 7468
c848d0af 7469 return 0;
43f66a6c
JK
7470 }
7471
7472 ipw_associate_network(priv, network, rates, 0);
c848d0af
JK
7473
7474 return 1;
7475}
7476
7477static void ipw_bg_associate(void *data)
7478{
7479 struct ipw_priv *priv = data;
4644151b 7480 mutex_lock(&priv->mutex);
c848d0af 7481 ipw_associate(data);
4644151b 7482 mutex_unlock(&priv->mutex);
43f66a6c 7483}
bf79451e 7484
b095c381
JK
7485static void ipw_rebuild_decrypted_skb(struct ipw_priv *priv,
7486 struct sk_buff *skb)
7487{
7488 struct ieee80211_hdr *hdr;
7489 u16 fc;
7490
7491 hdr = (struct ieee80211_hdr *)skb->data;
7492 fc = le16_to_cpu(hdr->frame_ctl);
7493 if (!(fc & IEEE80211_FCTL_PROTECTED))
7494 return;
7495
7496 fc &= ~IEEE80211_FCTL_PROTECTED;
7497 hdr->frame_ctl = cpu_to_le16(fc);
7498 switch (priv->ieee->sec.level) {
7499 case SEC_LEVEL_3:
7500 /* Remove CCMP HDR */
7501 memmove(skb->data + IEEE80211_3ADDR_LEN,
7502 skb->data + IEEE80211_3ADDR_LEN + 8,
7503 skb->len - IEEE80211_3ADDR_LEN - 8);
f4ff497d 7504 skb_trim(skb, skb->len - 16); /* CCMP_HDR_LEN + CCMP_MIC_LEN */
b095c381
JK
7505 break;
7506 case SEC_LEVEL_2:
7507 break;
7508 case SEC_LEVEL_1:
7509 /* Remove IV */
7510 memmove(skb->data + IEEE80211_3ADDR_LEN,
7511 skb->data + IEEE80211_3ADDR_LEN + 4,
7512 skb->len - IEEE80211_3ADDR_LEN - 4);
f4ff497d 7513 skb_trim(skb, skb->len - 8); /* IV + ICV */
b095c381
JK
7514 break;
7515 case SEC_LEVEL_0:
7516 break;
7517 default:
7518 printk(KERN_ERR "Unknow security level %d\n",
7519 priv->ieee->sec.level);
7520 break;
7521 }
43f66a6c 7522}
bf79451e 7523
b095c381
JK
7524static void ipw_handle_data_packet(struct ipw_priv *priv,
7525 struct ipw_rx_mem_buffer *rxb,
7526 struct ieee80211_rx_stats *stats)
43f66a6c 7527{
567deaf6 7528 struct ieee80211_hdr_4addr *hdr;
43f66a6c
JK
7529 struct ipw_rx_packet *pkt = (struct ipw_rx_packet *)rxb->skb->data;
7530
7531 /* We received data from the HW, so stop the watchdog */
7532 priv->net_dev->trans_start = jiffies;
7533
bf79451e 7534 /* We only process data packets if the
43f66a6c 7535 * interface is open */
a613bffd 7536 if (unlikely((le16_to_cpu(pkt->u.frame.length) + IPW_RX_FRAME_SIZE) >
43f66a6c
JK
7537 skb_tailroom(rxb->skb))) {
7538 priv->ieee->stats.rx_errors++;
7539 priv->wstats.discard.misc++;
7540 IPW_DEBUG_DROP("Corruption detected! Oh no!\n");
7541 return;
7542 } else if (unlikely(!netif_running(priv->net_dev))) {
7543 priv->ieee->stats.rx_dropped++;
7544 priv->wstats.discard.misc++;
7545 IPW_DEBUG_DROP("Dropping packet while interface is not up.\n");
7546 return;
7547 }
7548
7549 /* Advance skb->data to the start of the actual payload */
aaa4d308 7550 skb_reserve(rxb->skb, offsetof(struct ipw_rx_packet, u.frame.data));
43f66a6c
JK
7551
7552 /* Set the size of the skb to the size of the frame */
a613bffd 7553 skb_put(rxb->skb, le16_to_cpu(pkt->u.frame.length));
43f66a6c
JK
7554
7555 IPW_DEBUG_RX("Rx packet of %d bytes.\n", rxb->skb->len);
7556
b095c381 7557 /* HW decrypt will not clear the WEP bit, MIC, PN, etc. */
567deaf6
HL
7558 hdr = (struct ieee80211_hdr_4addr *)rxb->skb->data;
7559 if (priv->ieee->iw_mode != IW_MODE_MONITOR &&
3c19065a 7560 (is_multicast_ether_addr(hdr->addr1) ?
567deaf6 7561 !priv->ieee->host_mc_decrypt : !priv->ieee->host_decrypt))
b095c381
JK
7562 ipw_rebuild_decrypted_skb(priv, rxb->skb);
7563
bf79451e 7564 if (!ieee80211_rx(priv->ieee, rxb->skb, stats))
43f66a6c 7565 priv->ieee->stats.rx_errors++;
a613bffd 7566 else { /* ieee80211_rx succeeded, so it now owns the SKB */
43f66a6c 7567 rxb->skb = NULL;
b095c381 7568 __ipw_led_activity_on(priv);
a613bffd 7569 }
43f66a6c
JK
7570}
7571
459d4087 7572#ifdef CONFIG_IPW2200_RADIOTAP
24a47dbd
MK
7573static void ipw_handle_data_packet_monitor(struct ipw_priv *priv,
7574 struct ipw_rx_mem_buffer *rxb,
7575 struct ieee80211_rx_stats *stats)
7576{
7577 struct ipw_rx_packet *pkt = (struct ipw_rx_packet *)rxb->skb->data;
7578 struct ipw_rx_frame *frame = &pkt->u.frame;
7579
7580 /* initial pull of some data */
7581 u16 received_channel = frame->received_channel;
7582 u8 antennaAndPhy = frame->antennaAndPhy;
7583 s8 antsignal = frame->rssi_dbm - IPW_RSSI_TO_DBM; /* call it signed anyhow */
7584 u16 pktrate = frame->rate;
7585
7586 /* Magic struct that slots into the radiotap header -- no reason
7587 * to build this manually element by element, we can write it much
7588 * more efficiently than we can parse it. ORDER MATTERS HERE */
d685b8c2 7589 struct ipw_rt_hdr *ipw_rt;
24a47dbd
MK
7590
7591 short len = le16_to_cpu(pkt->u.frame.length);
7592
7593 /* We received data from the HW, so stop the watchdog */
7594 priv->net_dev->trans_start = jiffies;
7595
7596 /* We only process data packets if the
7597 * interface is open */
7598 if (unlikely((le16_to_cpu(pkt->u.frame.length) + IPW_RX_FRAME_SIZE) >
7599 skb_tailroom(rxb->skb))) {
7600 priv->ieee->stats.rx_errors++;
7601 priv->wstats.discard.misc++;
7602 IPW_DEBUG_DROP("Corruption detected! Oh no!\n");
7603 return;
7604 } else if (unlikely(!netif_running(priv->net_dev))) {
7605 priv->ieee->stats.rx_dropped++;
7606 priv->wstats.discard.misc++;
7607 IPW_DEBUG_DROP("Dropping packet while interface is not up.\n");
7608 return;
7609 }
7610
7611 /* Libpcap 0.9.3+ can handle variable length radiotap, so we'll use
7612 * that now */
7613 if (len > IPW_RX_BUF_SIZE - sizeof(struct ipw_rt_hdr)) {
7614 /* FIXME: Should alloc bigger skb instead */
7615 priv->ieee->stats.rx_dropped++;
7616 priv->wstats.discard.misc++;
7617 IPW_DEBUG_DROP("Dropping too large packet in monitor\n");
7618 return;
7619 }
7620
7621 /* copy the frame itself */
7622 memmove(rxb->skb->data + sizeof(struct ipw_rt_hdr),
7623 rxb->skb->data + IPW_RX_FRAME_SIZE, len);
7624
7625 /* Zero the radiotap static buffer ... We only need to zero the bytes NOT
7626 * part of our real header, saves a little time.
7627 *
7628 * No longer necessary since we fill in all our data. Purge before merging
7629 * patch officially.
7630 * memset(rxb->skb->data + sizeof(struct ipw_rt_hdr), 0,
7631 * IEEE80211_RADIOTAP_HDRLEN - sizeof(struct ipw_rt_hdr));
7632 */
7633
7634 ipw_rt = (struct ipw_rt_hdr *)rxb->skb->data;
7635
7636 ipw_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
7637 ipw_rt->rt_hdr.it_pad = 0; /* always good to zero */
7638 ipw_rt->rt_hdr.it_len = sizeof(struct ipw_rt_hdr); /* total header+data */
7639
7640 /* Big bitfield of all the fields we provide in radiotap */
7641 ipw_rt->rt_hdr.it_present =
7642 ((1 << IEEE80211_RADIOTAP_FLAGS) |
7643 (1 << IEEE80211_RADIOTAP_RATE) |
7644 (1 << IEEE80211_RADIOTAP_CHANNEL) |
7645 (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
d685b8c2 7646 (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
24a47dbd
MK
7647 (1 << IEEE80211_RADIOTAP_ANTENNA));
7648
7649 /* Zero the flags, we'll add to them as we go */
7650 ipw_rt->rt_flags = 0;
88a93df4 7651 ipw_rt->rt_tsf = 0ULL;
24a47dbd
MK
7652
7653 /* Convert signal to DBM */
7654 ipw_rt->rt_dbmsignal = antsignal;
7655
7656 /* Convert the channel data and set the flags */
7657 ipw_rt->rt_channel = cpu_to_le16(ieee80211chan2mhz(received_channel));
7658 if (received_channel > 14) { /* 802.11a */
7659 ipw_rt->rt_chbitmask =
7660 cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ));
7661 } else if (antennaAndPhy & 32) { /* 802.11b */
7662 ipw_rt->rt_chbitmask =
7663 cpu_to_le16((IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ));
7664 } else { /* 802.11g */
7665 ipw_rt->rt_chbitmask =
7666 (IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ);
7667 }
7668
7669 /* set the rate in multiples of 500k/s */
7670 switch (pktrate) {
7671 case IPW_TX_RATE_1MB:
7672 ipw_rt->rt_rate = 2;
7673 break;
7674 case IPW_TX_RATE_2MB:
7675 ipw_rt->rt_rate = 4;
7676 break;
7677 case IPW_TX_RATE_5MB:
7678 ipw_rt->rt_rate = 10;
7679 break;
7680 case IPW_TX_RATE_6MB:
7681 ipw_rt->rt_rate = 12;
7682 break;
7683 case IPW_TX_RATE_9MB:
7684 ipw_rt->rt_rate = 18;
7685 break;
7686 case IPW_TX_RATE_11MB:
7687 ipw_rt->rt_rate = 22;
7688 break;
7689 case IPW_TX_RATE_12MB:
7690 ipw_rt->rt_rate = 24;
7691 break;
7692 case IPW_TX_RATE_18MB:
7693 ipw_rt->rt_rate = 36;
7694 break;
7695 case IPW_TX_RATE_24MB:
7696 ipw_rt->rt_rate = 48;
7697 break;
7698 case IPW_TX_RATE_36MB:
7699 ipw_rt->rt_rate = 72;
7700 break;
7701 case IPW_TX_RATE_48MB:
7702 ipw_rt->rt_rate = 96;
7703 break;
7704 case IPW_TX_RATE_54MB:
7705 ipw_rt->rt_rate = 108;
7706 break;
7707 default:
7708 ipw_rt->rt_rate = 0;
7709 break;
7710 }
7711
7712 /* antenna number */
7713 ipw_rt->rt_antenna = (antennaAndPhy & 3); /* Is this right? */
7714
7715 /* set the preamble flag if we have it */
7716 if ((antennaAndPhy & 64))
7717 ipw_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
7718
7719 /* Set the size of the skb to the size of the frame */
7720 skb_put(rxb->skb, len + sizeof(struct ipw_rt_hdr));
43f66a6c
JK
7721
7722 IPW_DEBUG_RX("Rx packet of %d bytes.\n", rxb->skb->len);
7723
bf79451e 7724 if (!ieee80211_rx(priv->ieee, rxb->skb, stats))
43f66a6c 7725 priv->ieee->stats.rx_errors++;
24a47dbd
MK
7726 else { /* ieee80211_rx succeeded, so it now owns the SKB */
7727 rxb->skb = NULL;
7728 /* no LED during capture */
7729 }
7730}
7731#endif
7732
d685b8c2
ZY
7733#ifdef CONFIG_IPW2200_PROMISCUOUS
7734#define ieee80211_is_probe_response(fc) \
7735 ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT && \
7736 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP )
7737
7738#define ieee80211_is_management(fc) \
7739 ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
7740
7741#define ieee80211_is_control(fc) \
7742 ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL)
7743
7744#define ieee80211_is_data(fc) \
7745 ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)
7746
7747#define ieee80211_is_assoc_request(fc) \
7748 ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ)
7749
7750#define ieee80211_is_reassoc_request(fc) \
7751 ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
7752
7753static void ipw_handle_promiscuous_rx(struct ipw_priv *priv,
7754 struct ipw_rx_mem_buffer *rxb,
7755 struct ieee80211_rx_stats *stats)
7756{
7757 struct ipw_rx_packet *pkt = (struct ipw_rx_packet *)rxb->skb->data;
7758 struct ipw_rx_frame *frame = &pkt->u.frame;
7759 struct ipw_rt_hdr *ipw_rt;
7760
7761 /* First cache any information we need before we overwrite
7762 * the information provided in the skb from the hardware */
7763 struct ieee80211_hdr *hdr;
7764 u16 channel = frame->received_channel;
7765 u8 phy_flags = frame->antennaAndPhy;
7766 s8 signal = frame->rssi_dbm - IPW_RSSI_TO_DBM;
7767 s8 noise = frame->noise;
7768 u8 rate = frame->rate;
7769 short len = le16_to_cpu(pkt->u.frame.length);
d685b8c2
ZY
7770 struct sk_buff *skb;
7771 int hdr_only = 0;
7772 u16 filter = priv->prom_priv->filter;
7773
7774 /* If the filter is set to not include Rx frames then return */
7775 if (filter & IPW_PROM_NO_RX)
7776 return;
7777
d685b8c2
ZY
7778 /* We received data from the HW, so stop the watchdog */
7779 priv->prom_net_dev->trans_start = jiffies;
7780
7781 if (unlikely((len + IPW_RX_FRAME_SIZE) > skb_tailroom(rxb->skb))) {
7782 priv->prom_priv->ieee->stats.rx_errors++;
7783 IPW_DEBUG_DROP("Corruption detected! Oh no!\n");
7784 return;
7785 }
7786
7787 /* We only process data packets if the interface is open */
7788 if (unlikely(!netif_running(priv->prom_net_dev))) {
7789 priv->prom_priv->ieee->stats.rx_dropped++;
7790 IPW_DEBUG_DROP("Dropping packet while interface is not up.\n");
7791 return;
7792 }
7793
7794 /* Libpcap 0.9.3+ can handle variable length radiotap, so we'll use
7795 * that now */
7796 if (len > IPW_RX_BUF_SIZE - sizeof(struct ipw_rt_hdr)) {
7797 /* FIXME: Should alloc bigger skb instead */
7798 priv->prom_priv->ieee->stats.rx_dropped++;
7799 IPW_DEBUG_DROP("Dropping too large packet in monitor\n");
7800 return;
7801 }
7802
7803 hdr = (void *)rxb->skb->data + IPW_RX_FRAME_SIZE;
851ca268 7804 if (ieee80211_is_management(le16_to_cpu(hdr->frame_ctl))) {
d685b8c2
ZY
7805 if (filter & IPW_PROM_NO_MGMT)
7806 return;
7807 if (filter & IPW_PROM_MGMT_HEADER_ONLY)
7808 hdr_only = 1;
851ca268 7809 } else if (ieee80211_is_control(le16_to_cpu(hdr->frame_ctl))) {
d685b8c2
ZY
7810 if (filter & IPW_PROM_NO_CTL)
7811 return;
7812 if (filter & IPW_PROM_CTL_HEADER_ONLY)
7813 hdr_only = 1;
851ca268 7814 } else if (ieee80211_is_data(le16_to_cpu(hdr->frame_ctl))) {
d685b8c2
ZY
7815 if (filter & IPW_PROM_NO_DATA)
7816 return;
7817 if (filter & IPW_PROM_DATA_HEADER_ONLY)
7818 hdr_only = 1;
7819 }
7820
7821 /* Copy the SKB since this is for the promiscuous side */
7822 skb = skb_copy(rxb->skb, GFP_ATOMIC);
7823 if (skb == NULL) {
7824 IPW_ERROR("skb_clone failed for promiscuous copy.\n");
7825 return;
7826 }
7827
7828 /* copy the frame data to write after where the radiotap header goes */
7829 ipw_rt = (void *)skb->data;
7830
7831 if (hdr_only)
851ca268 7832 len = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
d685b8c2
ZY
7833
7834 memcpy(ipw_rt->payload, hdr, len);
7835
7836 /* Zero the radiotap static buffer ... We only need to zero the bytes
7837 * NOT part of our real header, saves a little time.
7838 *
7839 * No longer necessary since we fill in all our data. Purge before
7840 * merging patch officially.
7841 * memset(rxb->skb->data + sizeof(struct ipw_rt_hdr), 0,
7842 * IEEE80211_RADIOTAP_HDRLEN - sizeof(struct ipw_rt_hdr));
7843 */
7844
7845 ipw_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
7846 ipw_rt->rt_hdr.it_pad = 0; /* always good to zero */
7847 ipw_rt->rt_hdr.it_len = sizeof(*ipw_rt); /* total header+data */
7848
7849 /* Set the size of the skb to the size of the frame */
7850 skb_put(skb, ipw_rt->rt_hdr.it_len + len);
7851
7852 /* Big bitfield of all the fields we provide in radiotap */
7853 ipw_rt->rt_hdr.it_present =
7854 ((1 << IEEE80211_RADIOTAP_FLAGS) |
d685b8c2
ZY
7855 (1 << IEEE80211_RADIOTAP_RATE) |
7856 (1 << IEEE80211_RADIOTAP_CHANNEL) |
7857 (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
7858 (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
7859 (1 << IEEE80211_RADIOTAP_ANTENNA));
7860
7861 /* Zero the flags, we'll add to them as we go */
7862 ipw_rt->rt_flags = 0;
88a93df4 7863 ipw_rt->rt_tsf = 0ULL;
d685b8c2
ZY
7864
7865 /* Convert to DBM */
7866 ipw_rt->rt_dbmsignal = signal;
7867 ipw_rt->rt_dbmnoise = noise;
7868
7869 /* Convert the channel data and set the flags */
7870 ipw_rt->rt_channel = cpu_to_le16(ieee80211chan2mhz(channel));
7871 if (channel > 14) { /* 802.11a */
7872 ipw_rt->rt_chbitmask =
7873 cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ));
7874 } else if (phy_flags & (1 << 5)) { /* 802.11b */
7875 ipw_rt->rt_chbitmask =
7876 cpu_to_le16((IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ));
7877 } else { /* 802.11g */
7878 ipw_rt->rt_chbitmask =
7879 (IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ);
7880 }
7881
7882 /* set the rate in multiples of 500k/s */
7883 switch (rate) {
7884 case IPW_TX_RATE_1MB:
7885 ipw_rt->rt_rate = 2;
7886 break;
7887 case IPW_TX_RATE_2MB:
7888 ipw_rt->rt_rate = 4;
7889 break;
7890 case IPW_TX_RATE_5MB:
7891 ipw_rt->rt_rate = 10;
7892 break;
7893 case IPW_TX_RATE_6MB:
7894 ipw_rt->rt_rate = 12;
7895 break;
7896 case IPW_TX_RATE_9MB:
7897 ipw_rt->rt_rate = 18;
7898 break;
7899 case IPW_TX_RATE_11MB:
7900 ipw_rt->rt_rate = 22;
7901 break;
7902 case IPW_TX_RATE_12MB:
7903 ipw_rt->rt_rate = 24;
7904 break;
7905 case IPW_TX_RATE_18MB:
7906 ipw_rt->rt_rate = 36;
7907 break;
7908 case IPW_TX_RATE_24MB:
7909 ipw_rt->rt_rate = 48;
7910 break;
7911 case IPW_TX_RATE_36MB:
7912 ipw_rt->rt_rate = 72;
7913 break;
7914 case IPW_TX_RATE_48MB:
7915 ipw_rt->rt_rate = 96;
7916 break;
7917 case IPW_TX_RATE_54MB:
7918 ipw_rt->rt_rate = 108;
7919 break;
7920 default:
7921 ipw_rt->rt_rate = 0;
7922 break;
7923 }
7924
7925 /* antenna number */
7926 ipw_rt->rt_antenna = (phy_flags & 3);
7927
7928 /* set the preamble flag if we have it */
7929 if (phy_flags & (1 << 6))
7930 ipw_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
7931
7932 IPW_DEBUG_RX("Rx packet of %d bytes.\n", skb->len);
7933
7934 if (!ieee80211_rx(priv->prom_priv->ieee, skb, stats)) {
7935 priv->prom_priv->ieee->stats.rx_errors++;
7936 dev_kfree_skb_any(skb);
7937 }
7938}
7939#endif
7940
858119e1 7941static int is_network_packet(struct ipw_priv *priv,
ea2b26e0
JK
7942 struct ieee80211_hdr_4addr *header)
7943{
7944 /* Filter incoming packets to determine if they are targetted toward
7945 * this network, discarding packets coming from ourselves */
7946 switch (priv->ieee->iw_mode) {
a613bffd 7947 case IW_MODE_ADHOC: /* Header: Dest. | Source | BSSID */
c848d0af
JK
7948 /* packets from our adapter are dropped (echo) */
7949 if (!memcmp(header->addr2, priv->net_dev->dev_addr, ETH_ALEN))
7950 return 0;
7951
90700fd9 7952 /* {broad,multi}cast packets to our BSSID go through */
3c19065a 7953 if (is_multicast_ether_addr(header->addr1))
ea2b26e0 7954 return !memcmp(header->addr3, priv->bssid, ETH_ALEN);
a613bffd
JK
7955
7956 /* packets to our adapter go through */
7957 return !memcmp(header->addr1, priv->net_dev->dev_addr,
7958 ETH_ALEN);
a613bffd 7959
90700fd9 7960 case IW_MODE_INFRA: /* Header: Dest. | BSSID | Source */
c848d0af
JK
7961 /* packets from our adapter are dropped (echo) */
7962 if (!memcmp(header->addr3, priv->net_dev->dev_addr, ETH_ALEN))
7963 return 0;
7964
90700fd9 7965 /* {broad,multi}cast packets to our BSS go through */
3c19065a 7966 if (is_multicast_ether_addr(header->addr1))
a613bffd
JK
7967 return !memcmp(header->addr2, priv->bssid, ETH_ALEN);
7968
7969 /* packets to our adapter go through */
7970 return !memcmp(header->addr1, priv->net_dev->dev_addr,
7971 ETH_ALEN);
ea2b26e0 7972 }
a613bffd 7973
ea2b26e0
JK
7974 return 1;
7975}
7976
afbf30a2
JK
7977#define IPW_PACKET_RETRY_TIME HZ
7978
858119e1 7979static int is_duplicate_packet(struct ipw_priv *priv,
afbf30a2
JK
7980 struct ieee80211_hdr_4addr *header)
7981{
afbf30a2
JK
7982 u16 sc = le16_to_cpu(header->seq_ctl);
7983 u16 seq = WLAN_GET_SEQ_SEQ(sc);
7984 u16 frag = WLAN_GET_SEQ_FRAG(sc);
7985 u16 *last_seq, *last_frag;
7986 unsigned long *last_time;
7987
7988 switch (priv->ieee->iw_mode) {
7989 case IW_MODE_ADHOC:
7990 {
7991 struct list_head *p;
7992 struct ipw_ibss_seq *entry = NULL;
7993 u8 *mac = header->addr2;
7994 int index = mac[5] % IPW_IBSS_MAC_HASH_SIZE;
7995
7996 __list_for_each(p, &priv->ibss_mac_hash[index]) {
7997 entry =
7998 list_entry(p, struct ipw_ibss_seq, list);
7999 if (!memcmp(entry->mac, mac, ETH_ALEN))
8000 break;
8001 }
8002 if (p == &priv->ibss_mac_hash[index]) {
8003 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
8004 if (!entry) {
8005 IPW_ERROR
8006 ("Cannot malloc new mac entry\n");
8007 return 0;
8008 }
8009 memcpy(entry->mac, mac, ETH_ALEN);
8010 entry->seq_num = seq;
8011 entry->frag_num = frag;
8012 entry->packet_time = jiffies;
8013 list_add(&entry->list,
8014 &priv->ibss_mac_hash[index]);
8015 return 0;
8016 }
8017 last_seq = &entry->seq_num;
8018 last_frag = &entry->frag_num;
8019 last_time = &entry->packet_time;
8020 break;
8021 }
8022 case IW_MODE_INFRA:
8023 last_seq = &priv->last_seq_num;
8024 last_frag = &priv->last_frag_num;
8025 last_time = &priv->last_packet_time;
8026 break;
8027 default:
8028 return 0;
8029 }
8030 if ((*last_seq == seq) &&
8031 time_after(*last_time + IPW_PACKET_RETRY_TIME, jiffies)) {
8032 if (*last_frag == frag)
8033 goto drop;
8034 if (*last_frag + 1 != frag)
8035 /* out-of-order fragment */
8036 goto drop;
afbf30a2
JK
8037 } else
8038 *last_seq = seq;
8039
f57ce7ce 8040 *last_frag = frag;
afbf30a2
JK
8041 *last_time = jiffies;
8042 return 0;
8043
8044 drop:
87b016cb
ZY
8045 /* Comment this line now since we observed the card receives
8046 * duplicate packets but the FCTL_RETRY bit is not set in the
8047 * IBSS mode with fragmentation enabled.
8048 BUG_ON(!(le16_to_cpu(header->frame_ctl) & IEEE80211_FCTL_RETRY)); */
afbf30a2
JK
8049 return 1;
8050}
8051
b095c381
JK
8052static void ipw_handle_mgmt_packet(struct ipw_priv *priv,
8053 struct ipw_rx_mem_buffer *rxb,
8054 struct ieee80211_rx_stats *stats)
8055{
8056 struct sk_buff *skb = rxb->skb;
8057 struct ipw_rx_packet *pkt = (struct ipw_rx_packet *)skb->data;
8058 struct ieee80211_hdr_4addr *header = (struct ieee80211_hdr_4addr *)
8059 (skb->data + IPW_RX_FRAME_SIZE);
8060
8061 ieee80211_rx_mgt(priv->ieee, header, stats);
8062
8063 if (priv->ieee->iw_mode == IW_MODE_ADHOC &&
8064 ((WLAN_FC_GET_STYPE(le16_to_cpu(header->frame_ctl)) ==
8065 IEEE80211_STYPE_PROBE_RESP) ||
8066 (WLAN_FC_GET_STYPE(le16_to_cpu(header->frame_ctl)) ==
8067 IEEE80211_STYPE_BEACON))) {
8068 if (!memcmp(header->addr3, priv->bssid, ETH_ALEN))
8069 ipw_add_station(priv, header->addr2);
8070 }
8071
8072 if (priv->config & CFG_NET_STATS) {
8073 IPW_DEBUG_HC("sending stat packet\n");
8074
8075 /* Set the size of the skb to the size of the full
8076 * ipw header and 802.11 frame */
8077 skb_put(skb, le16_to_cpu(pkt->u.frame.length) +
8078 IPW_RX_FRAME_SIZE);
8079
8080 /* Advance past the ipw packet header to the 802.11 frame */
8081 skb_pull(skb, IPW_RX_FRAME_SIZE);
8082
8083 /* Push the ieee80211_rx_stats before the 802.11 frame */
8084 memcpy(skb_push(skb, sizeof(*stats)), stats, sizeof(*stats));
8085
8086 skb->dev = priv->ieee->dev;
8087
8088 /* Point raw at the ieee80211_stats */
8089 skb->mac.raw = skb->data;
8090
8091 skb->pkt_type = PACKET_OTHERHOST;
8092 skb->protocol = __constant_htons(ETH_P_80211_STATS);
8093 memset(skb->cb, 0, sizeof(rxb->skb->cb));
8094 netif_rx(skb);
43f66a6c 8095 rxb->skb = NULL;
b095c381 8096 }
43f66a6c
JK
8097}
8098
43f66a6c
JK
8099/*
8100 * Main entry function for recieving a packet with 80211 headers. This
8101 * should be called when ever the FW has notified us that there is a new
8102 * skb in the recieve queue.
8103 */
8104static void ipw_rx(struct ipw_priv *priv)
8105{
8106 struct ipw_rx_mem_buffer *rxb;
8107 struct ipw_rx_packet *pkt;
0dacca1f 8108 struct ieee80211_hdr_4addr *header;
43f66a6c
JK
8109 u32 r, w, i;
8110 u8 network_packet;
8111
b095c381
JK
8112 r = ipw_read32(priv, IPW_RX_READ_INDEX);
8113 w = ipw_read32(priv, IPW_RX_WRITE_INDEX);
43f66a6c
JK
8114 i = (priv->rxq->processed + 1) % RX_QUEUE_SIZE;
8115
8116 while (i != r) {
8117 rxb = priv->rxq->queue[i];
43f66a6c
JK
8118 if (unlikely(rxb == NULL)) {
8119 printk(KERN_CRIT "Queue not allocated!\n");
8120 break;
8121 }
43f66a6c
JK
8122 priv->rxq->queue[i] = NULL;
8123
8124 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
b095c381 8125 IPW_RX_BUF_SIZE,
43f66a6c
JK
8126 PCI_DMA_FROMDEVICE);
8127
8128 pkt = (struct ipw_rx_packet *)rxb->skb->data;
8129 IPW_DEBUG_RX("Packet: type=%02X seq=%02X bits=%02X\n",
8130 pkt->header.message_type,
0edd5b44 8131 pkt->header.rx_seq_num, pkt->header.control_bits);
43f66a6c
JK
8132
8133 switch (pkt->header.message_type) {
0edd5b44
JG
8134 case RX_FRAME_TYPE: /* 802.11 frame */ {
8135 struct ieee80211_rx_stats stats = {
851ca268 8136 .rssi = pkt->u.frame.rssi_dbm -
0edd5b44 8137 IPW_RSSI_TO_DBM,
c848d0af 8138 .signal =
b191608a
BM
8139 le16_to_cpu(pkt->u.frame.rssi_dbm) -
8140 IPW_RSSI_TO_DBM + 0x100,
c848d0af
JK
8141 .noise =
8142 le16_to_cpu(pkt->u.frame.noise),
0edd5b44
JG
8143 .rate = pkt->u.frame.rate,
8144 .mac_time = jiffies,
8145 .received_channel =
8146 pkt->u.frame.received_channel,
8147 .freq =
8148 (pkt->u.frame.
8149 control & (1 << 0)) ?
8150 IEEE80211_24GHZ_BAND :
8151 IEEE80211_52GHZ_BAND,
a613bffd 8152 .len = le16_to_cpu(pkt->u.frame.length),
0edd5b44
JG
8153 };
8154
8155 if (stats.rssi != 0)
8156 stats.mask |= IEEE80211_STATMASK_RSSI;
8157 if (stats.signal != 0)
8158 stats.mask |= IEEE80211_STATMASK_SIGNAL;
c848d0af
JK
8159 if (stats.noise != 0)
8160 stats.mask |= IEEE80211_STATMASK_NOISE;
0edd5b44
JG
8161 if (stats.rate != 0)
8162 stats.mask |= IEEE80211_STATMASK_RATE;
8163
8164 priv->rx_packets++;
43f66a6c 8165
d685b8c2
ZY
8166#ifdef CONFIG_IPW2200_PROMISCUOUS
8167 if (priv->prom_net_dev && netif_running(priv->prom_net_dev))
8168 ipw_handle_promiscuous_rx(priv, rxb, &stats);
8169#endif
8170
b095c381 8171#ifdef CONFIG_IPW2200_MONITOR
0edd5b44 8172 if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
459d4087 8173#ifdef CONFIG_IPW2200_RADIOTAP
d685b8c2
ZY
8174
8175 ipw_handle_data_packet_monitor(priv,
8176 rxb,
8177 &stats);
24a47dbd 8178#else
d685b8c2
ZY
8179 ipw_handle_data_packet(priv, rxb,
8180 &stats);
24a47dbd 8181#endif
0edd5b44
JG
8182 break;
8183 }
43f66a6c 8184#endif
bf79451e 8185
0edd5b44 8186 header =
0dacca1f
JK
8187 (struct ieee80211_hdr_4addr *)(rxb->skb->
8188 data +
8189 IPW_RX_FRAME_SIZE);
43f66a6c
JK
8190 /* TODO: Check Ad-Hoc dest/source and make sure
8191 * that we are actually parsing these packets
bf79451e 8192 * correctly -- we should probably use the
43f66a6c
JK
8193 * frame control of the packet and disregard
8194 * the current iw_mode */
0edd5b44 8195
ea2b26e0
JK
8196 network_packet =
8197 is_network_packet(priv, header);
0edd5b44
JG
8198 if (network_packet && priv->assoc_network) {
8199 priv->assoc_network->stats.rssi =
8200 stats.rssi;
00d21de5
ZY
8201 priv->exp_avg_rssi =
8202 exponential_average(priv->exp_avg_rssi,
8203 stats.rssi, DEPTH_RSSI);
0edd5b44
JG
8204 }
8205
8206 IPW_DEBUG_RX("Frame: len=%u\n",
a613bffd 8207 le16_to_cpu(pkt->u.frame.length));
0edd5b44 8208
a613bffd 8209 if (le16_to_cpu(pkt->u.frame.length) <
9d0be03a
ZY
8210 ieee80211_get_hdrlen(le16_to_cpu(
8211 header->frame_ctl))) {
0edd5b44
JG
8212 IPW_DEBUG_DROP
8213 ("Received packet is too small. "
8214 "Dropping.\n");
8215 priv->ieee->stats.rx_errors++;
8216 priv->wstats.discard.misc++;
8217 break;
8218 }
8219
a613bffd
JK
8220 switch (WLAN_FC_GET_TYPE
8221 (le16_to_cpu(header->frame_ctl))) {
b095c381 8222
0edd5b44 8223 case IEEE80211_FTYPE_MGMT:
b095c381
JK
8224 ipw_handle_mgmt_packet(priv, rxb,
8225 &stats);
0edd5b44
JG
8226 break;
8227
8228 case IEEE80211_FTYPE_CTL:
8229 break;
8230
8231 case IEEE80211_FTYPE_DATA:
afbf30a2
JK
8232 if (unlikely(!network_packet ||
8233 is_duplicate_packet(priv,
8234 header)))
8235 {
0edd5b44
JG
8236 IPW_DEBUG_DROP("Dropping: "
8237 MAC_FMT ", "
8238 MAC_FMT ", "
8239 MAC_FMT "\n",
8240 MAC_ARG(header->
8241 addr1),
8242 MAC_ARG(header->
8243 addr2),
8244 MAC_ARG(header->
8245 addr3));
b095c381
JK
8246 break;
8247 }
8248
8249 ipw_handle_data_packet(priv, rxb,
8250 &stats);
8251
0edd5b44
JG
8252 break;
8253 }
43f66a6c
JK
8254 break;
8255 }
bf79451e 8256
0edd5b44
JG
8257 case RX_HOST_NOTIFICATION_TYPE:{
8258 IPW_DEBUG_RX
8259 ("Notification: subtype=%02X flags=%02X size=%d\n",
43f66a6c
JK
8260 pkt->u.notification.subtype,
8261 pkt->u.notification.flags,
8262 pkt->u.notification.size);
0edd5b44
JG
8263 ipw_rx_notification(priv, &pkt->u.notification);
8264 break;
8265 }
43f66a6c
JK
8266
8267 default:
8268 IPW_DEBUG_RX("Bad Rx packet of type %d\n",
8269 pkt->header.message_type);
8270 break;
8271 }
bf79451e
JG
8272
8273 /* For now we just don't re-use anything. We can tweak this
8274 * later to try and re-use notification packets and SKBs that
43f66a6c
JK
8275 * fail to Rx correctly */
8276 if (rxb->skb != NULL) {
8277 dev_kfree_skb_any(rxb->skb);
8278 rxb->skb = NULL;
8279 }
bf79451e 8280
43f66a6c 8281 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
b095c381 8282 IPW_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
43f66a6c 8283 list_add_tail(&rxb->list, &priv->rxq->rx_used);
bf79451e 8284
43f66a6c
JK
8285 i = (i + 1) % RX_QUEUE_SIZE;
8286 }
8287
8288 /* Backtrack one entry */
8289 priv->rxq->processed = (i ? i : RX_QUEUE_SIZE) - 1;
8290
8291 ipw_rx_queue_restock(priv);
8292}
8293
afbf30a2
JK
8294#define DEFAULT_RTS_THRESHOLD 2304U
8295#define MIN_RTS_THRESHOLD 1U
8296#define MAX_RTS_THRESHOLD 2304U
8297#define DEFAULT_BEACON_INTERVAL 100U
8298#define DEFAULT_SHORT_RETRY_LIMIT 7U
8299#define DEFAULT_LONG_RETRY_LIMIT 4U
8300
d6d5b5c1
ZY
8301/**
8302 * ipw_sw_reset
8303 * @option: options to control different reset behaviour
8304 * 0 = reset everything except the 'disable' module_param
8305 * 1 = reset everything and print out driver info (for probe only)
8306 * 2 = reset everything
8307 */
8308static int ipw_sw_reset(struct ipw_priv *priv, int option)
43f66a6c 8309{
afbf30a2
JK
8310 int band, modulation;
8311 int old_mode = priv->ieee->iw_mode;
43f66a6c 8312
afbf30a2
JK
8313 /* Initialize module parameter values here */
8314 priv->config = 0;
43f66a6c 8315
afbf30a2
JK
8316 /* We default to disabling the LED code as right now it causes
8317 * too many systems to lock up... */
8318 if (!led)
8319 priv->config |= CFG_NO_LED;
43f66a6c 8320
afbf30a2
JK
8321 if (associate)
8322 priv->config |= CFG_ASSOCIATE;
8323 else
8324 IPW_DEBUG_INFO("Auto associate disabled.\n");
bf79451e 8325
afbf30a2
JK
8326 if (auto_create)
8327 priv->config |= CFG_ADHOC_CREATE;
8328 else
8329 IPW_DEBUG_INFO("Auto adhoc creation disabled.\n");
43f66a6c 8330
17ed081d
ZY
8331 priv->config &= ~CFG_STATIC_ESSID;
8332 priv->essid_len = 0;
8333 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
8334
d6d5b5c1 8335 if (disable && option) {
afbf30a2
JK
8336 priv->status |= STATUS_RF_KILL_SW;
8337 IPW_DEBUG_INFO("Radio disabled.\n");
43f66a6c 8338 }
bf79451e 8339
afbf30a2
JK
8340 if (channel != 0) {
8341 priv->config |= CFG_STATIC_CHANNEL;
8342 priv->channel = channel;
8343 IPW_DEBUG_INFO("Bind to static channel %d\n", channel);
8344 /* TODO: Validate that provided channel is in range */
43f66a6c 8345 }
e43e3c1e 8346#ifdef CONFIG_IPW2200_QOS
afbf30a2
JK
8347 ipw_qos_init(priv, qos_enable, qos_burst_enable,
8348 burst_duration_CCK, burst_duration_OFDM);
e43e3c1e 8349#endif /* CONFIG_IPW2200_QOS */
43f66a6c 8350
afbf30a2
JK
8351 switch (mode) {
8352 case 1:
8353 priv->ieee->iw_mode = IW_MODE_ADHOC;
8354 priv->net_dev->type = ARPHRD_ETHER;
8355
8356 break;
8357#ifdef CONFIG_IPW2200_MONITOR
8358 case 2:
8359 priv->ieee->iw_mode = IW_MODE_MONITOR;
459d4087 8360#ifdef CONFIG_IPW2200_RADIOTAP
24a47dbd
MK
8361 priv->net_dev->type = ARPHRD_IEEE80211_RADIOTAP;
8362#else
afbf30a2 8363 priv->net_dev->type = ARPHRD_IEEE80211;
24a47dbd 8364#endif
afbf30a2
JK
8365 break;
8366#endif
8367 default:
8368 case 0:
8369 priv->net_dev->type = ARPHRD_ETHER;
8370 priv->ieee->iw_mode = IW_MODE_INFRA;
8371 break;
43f66a6c
JK
8372 }
8373
afbf30a2
JK
8374 if (hwcrypto) {
8375 priv->ieee->host_encrypt = 0;
8376 priv->ieee->host_encrypt_msdu = 0;
8377 priv->ieee->host_decrypt = 0;
567deaf6 8378 priv->ieee->host_mc_decrypt = 0;
afbf30a2
JK
8379 }
8380 IPW_DEBUG_INFO("Hardware crypto [%s]\n", hwcrypto ? "on" : "off");
43f66a6c 8381
e402c937
ZY
8382 /* IPW2200/2915 is abled to do hardware fragmentation. */
8383 priv->ieee->host_open_frag = 0;
bf79451e 8384
afbf30a2
JK
8385 if ((priv->pci_dev->device == 0x4223) ||
8386 (priv->pci_dev->device == 0x4224)) {
e8c69e27 8387 if (option == 1)
afbf30a2
JK
8388 printk(KERN_INFO DRV_NAME
8389 ": Detected Intel PRO/Wireless 2915ABG Network "
8390 "Connection\n");
8391 priv->ieee->abg_true = 1;
8392 band = IEEE80211_52GHZ_BAND | IEEE80211_24GHZ_BAND;
8393 modulation = IEEE80211_OFDM_MODULATION |
8394 IEEE80211_CCK_MODULATION;
8395 priv->adapter = IPW_2915ABG;
8396 priv->ieee->mode = IEEE_A | IEEE_G | IEEE_B;
43f66a6c 8397 } else {
e8c69e27 8398 if (option == 1)
afbf30a2
JK
8399 printk(KERN_INFO DRV_NAME
8400 ": Detected Intel PRO/Wireless 2200BG Network "
8401 "Connection\n");
bf79451e 8402
afbf30a2
JK
8403 priv->ieee->abg_true = 0;
8404 band = IEEE80211_24GHZ_BAND;
8405 modulation = IEEE80211_OFDM_MODULATION |
8406 IEEE80211_CCK_MODULATION;
8407 priv->adapter = IPW_2200BG;
8408 priv->ieee->mode = IEEE_G | IEEE_B;
43f66a6c
JK
8409 }
8410
afbf30a2
JK
8411 priv->ieee->freq_band = band;
8412 priv->ieee->modulation = modulation;
43f66a6c 8413
afbf30a2 8414 priv->rates_mask = IEEE80211_DEFAULT_RATES_MASK;
bf79451e 8415
afbf30a2
JK
8416 priv->disassociate_threshold = IPW_MB_DISASSOCIATE_THRESHOLD_DEFAULT;
8417 priv->roaming_threshold = IPW_MB_ROAMING_THRESHOLD_DEFAULT;
43f66a6c 8418
afbf30a2
JK
8419 priv->rts_threshold = DEFAULT_RTS_THRESHOLD;
8420 priv->short_retry_limit = DEFAULT_SHORT_RETRY_LIMIT;
8421 priv->long_retry_limit = DEFAULT_LONG_RETRY_LIMIT;
43f66a6c 8422
afbf30a2
JK
8423 /* If power management is turned on, default to AC mode */
8424 priv->power_mode = IPW_POWER_AC;
8425 priv->tx_power = IPW_TX_POWER_DEFAULT;
8426
0ece35b5 8427 return old_mode == priv->ieee->iw_mode;
43f66a6c
JK
8428}
8429
8430/*
8431 * This file defines the Wireless Extension handlers. It does not
8432 * define any methods of hardware manipulation and relies on the
8433 * functions defined in ipw_main to provide the HW interaction.
bf79451e
JG
8434 *
8435 * The exception to this is the use of the ipw_get_ordinal()
43f66a6c
JK
8436 * function used to poll the hardware vs. making unecessary calls.
8437 *
8438 */
8439
bf79451e
JG
8440static int ipw_wx_get_name(struct net_device *dev,
8441 struct iw_request_info *info,
43f66a6c
JK
8442 union iwreq_data *wrqu, char *extra)
8443{
8444 struct ipw_priv *priv = ieee80211_priv(dev);
4644151b 8445 mutex_lock(&priv->mutex);
c848d0af 8446 if (priv->status & STATUS_RF_KILL_MASK)
a613bffd 8447 strcpy(wrqu->name, "radio off");
c848d0af 8448 else if (!(priv->status & STATUS_ASSOCIATED))
43f66a6c 8449 strcpy(wrqu->name, "unassociated");
bf79451e 8450 else
43f66a6c
JK
8451 snprintf(wrqu->name, IFNAMSIZ, "IEEE 802.11%c",
8452 ipw_modes[priv->assoc_request.ieee_mode]);
8453 IPW_DEBUG_WX("Name: %s\n", wrqu->name);
4644151b 8454 mutex_unlock(&priv->mutex);
43f66a6c
JK
8455 return 0;
8456}
8457
8458static int ipw_set_channel(struct ipw_priv *priv, u8 channel)
8459{
8460 if (channel == 0) {
8461 IPW_DEBUG_INFO("Setting channel to ANY (0)\n");
8462 priv->config &= ~CFG_STATIC_CHANNEL;
c848d0af
JK
8463 IPW_DEBUG_ASSOC("Attempting to associate with new "
8464 "parameters.\n");
8465 ipw_associate(priv);
43f66a6c
JK
8466 return 0;
8467 }
8468
8469 priv->config |= CFG_STATIC_CHANNEL;
8470
8471 if (priv->channel == channel) {
0edd5b44
JG
8472 IPW_DEBUG_INFO("Request to set channel to current value (%d)\n",
8473 channel);
43f66a6c
JK
8474 return 0;
8475 }
8476
8477 IPW_DEBUG_INFO("Setting channel to %i\n", (int)channel);
8478 priv->channel = channel;
8479
b095c381
JK
8480#ifdef CONFIG_IPW2200_MONITOR
8481 if (priv->ieee->iw_mode == IW_MODE_MONITOR) {
afbf30a2 8482 int i;
b095c381 8483 if (priv->status & STATUS_SCANNING) {
afbf30a2 8484 IPW_DEBUG_SCAN("Scan abort triggered due to "
b095c381 8485 "channel change.\n");
afbf30a2 8486 ipw_abort_scan(priv);
b095c381
JK
8487 }
8488
8489 for (i = 1000; i && (priv->status & STATUS_SCANNING); i--)
8490 udelay(10);
8491
8492 if (priv->status & STATUS_SCANNING)
8493 IPW_DEBUG_SCAN("Still scanning...\n");
8494 else
8495 IPW_DEBUG_SCAN("Took %dms to abort current scan\n",
8496 1000 - i);
8497
8498 return 0;
43f66a6c 8499 }
b095c381
JK
8500#endif /* CONFIG_IPW2200_MONITOR */
8501
c848d0af
JK
8502 /* Network configuration changed -- force [re]association */
8503 IPW_DEBUG_ASSOC("[re]association triggered due to channel change.\n");
8504 if (!ipw_disassociate(priv))
43f66a6c 8505 ipw_associate(priv);
43f66a6c
JK
8506
8507 return 0;
8508}
8509
bf79451e
JG
8510static int ipw_wx_set_freq(struct net_device *dev,
8511 struct iw_request_info *info,
8512 union iwreq_data *wrqu, char *extra)
43f66a6c
JK
8513{
8514 struct ipw_priv *priv = ieee80211_priv(dev);
1867b117 8515 const struct ieee80211_geo *geo = ieee80211_get_geo(priv->ieee);
43f66a6c 8516 struct iw_freq *fwrq = &wrqu->freq;
afbf30a2 8517 int ret = 0, i;
1fe0adb4
LH
8518 u8 channel, flags;
8519 int band;
b095c381
JK
8520
8521 if (fwrq->m == 0) {
8522 IPW_DEBUG_WX("SET Freq/Channel -> any\n");
4644151b 8523 mutex_lock(&priv->mutex);
b095c381 8524 ret = ipw_set_channel(priv, 0);
4644151b 8525 mutex_unlock(&priv->mutex);
b095c381
JK
8526 return ret;
8527 }
43f66a6c
JK
8528 /* if setting by freq convert to channel */
8529 if (fwrq->e == 1) {
1867b117 8530 channel = ieee80211_freq_to_channel(priv->ieee, fwrq->m);
b095c381
JK
8531 if (channel == 0)
8532 return -EINVAL;
8533 } else
8534 channel = fwrq->m;
bf79451e 8535
1867b117 8536 if (!(band = ieee80211_is_valid_channel(priv->ieee, channel)))
b095c381 8537 return -EINVAL;
43f66a6c 8538
1fe0adb4 8539 if (priv->ieee->iw_mode == IW_MODE_ADHOC) {
1867b117 8540 i = ieee80211_channel_to_index(priv->ieee, channel);
afbf30a2
JK
8541 if (i == -1)
8542 return -EINVAL;
bf79451e 8543
1fe0adb4
LH
8544 flags = (band == IEEE80211_24GHZ_BAND) ?
8545 geo->bg[i].flags : geo->a[i].flags;
8546 if (flags & IEEE80211_CH_PASSIVE_ONLY) {
afbf30a2
JK
8547 IPW_DEBUG_WX("Invalid Ad-Hoc channel for 802.11a\n");
8548 return -EINVAL;
43f66a6c
JK
8549 }
8550 }
bf79451e 8551
43f66a6c 8552 IPW_DEBUG_WX("SET Freq/Channel -> %d \n", fwrq->m);
4644151b 8553 mutex_lock(&priv->mutex);
b095c381 8554 ret = ipw_set_channel(priv, channel);
4644151b 8555 mutex_unlock(&priv->mutex);
c848d0af 8556 return ret;
43f66a6c
JK
8557}
8558
bf79451e
JG
8559static int ipw_wx_get_freq(struct net_device *dev,
8560 struct iw_request_info *info,
43f66a6c
JK
8561 union iwreq_data *wrqu, char *extra)
8562{
8563 struct ipw_priv *priv = ieee80211_priv(dev);
8564
8565 wrqu->freq.e = 0;
8566
8567 /* If we are associated, trying to associate, or have a statically
8568 * configured CHANNEL then return that; otherwise return ANY */
4644151b 8569 mutex_lock(&priv->mutex);
43f66a6c 8570 if (priv->config & CFG_STATIC_CHANNEL ||
c580f67f
ZY
8571 priv->status & (STATUS_ASSOCIATING | STATUS_ASSOCIATED)) {
8572 int i;
8573
8574 i = ieee80211_channel_to_index(priv->ieee, priv->channel);
8575 BUG_ON(i == -1);
8576 wrqu->freq.e = 1;
8577
8578 switch (ieee80211_is_valid_channel(priv->ieee, priv->channel)) {
8579 case IEEE80211_52GHZ_BAND:
8580 wrqu->freq.m = priv->ieee->geo.a[i].freq * 100000;
8581 break;
8582
8583 case IEEE80211_24GHZ_BAND:
8584 wrqu->freq.m = priv->ieee->geo.bg[i].freq * 100000;
8585 break;
8586
8587 default:
8588 BUG();
8589 }
8590 } else
43f66a6c
JK
8591 wrqu->freq.m = 0;
8592
4644151b 8593 mutex_unlock(&priv->mutex);
43f66a6c
JK
8594 IPW_DEBUG_WX("GET Freq/Channel -> %d \n", priv->channel);
8595 return 0;
8596}
8597
bf79451e
JG
8598static int ipw_wx_set_mode(struct net_device *dev,
8599 struct iw_request_info *info,
43f66a6c
JK
8600 union iwreq_data *wrqu, char *extra)
8601{
8602 struct ipw_priv *priv = ieee80211_priv(dev);
8603 int err = 0;
8604
8605 IPW_DEBUG_WX("Set MODE: %d\n", wrqu->mode);
8606
43f66a6c 8607 switch (wrqu->mode) {
b095c381 8608#ifdef CONFIG_IPW2200_MONITOR
43f66a6c
JK
8609 case IW_MODE_MONITOR:
8610#endif
8611 case IW_MODE_ADHOC:
8612 case IW_MODE_INFRA:
8613 break;
8614 case IW_MODE_AUTO:
8615 wrqu->mode = IW_MODE_INFRA;
8616 break;
8617 default:
8618 return -EINVAL;
8619 }
b095c381
JK
8620 if (wrqu->mode == priv->ieee->iw_mode)
8621 return 0;
43f66a6c 8622
4644151b 8623 mutex_lock(&priv->mutex);
43f66a6c 8624
afbf30a2
JK
8625 ipw_sw_reset(priv, 0);
8626
b095c381 8627#ifdef CONFIG_IPW2200_MONITOR
bf79451e 8628 if (priv->ieee->iw_mode == IW_MODE_MONITOR)
43f66a6c 8629 priv->net_dev->type = ARPHRD_ETHER;
bf79451e
JG
8630
8631 if (wrqu->mode == IW_MODE_MONITOR)
459d4087 8632#ifdef CONFIG_IPW2200_RADIOTAP
24a47dbd
MK
8633 priv->net_dev->type = ARPHRD_IEEE80211_RADIOTAP;
8634#else
43f66a6c 8635 priv->net_dev->type = ARPHRD_IEEE80211;
24a47dbd 8636#endif
b095c381 8637#endif /* CONFIG_IPW2200_MONITOR */
bf79451e 8638
bf79451e 8639 /* Free the existing firmware and reset the fw_loaded
43f66a6c 8640 * flag so ipw_load() will bring in the new firmawre */
afbf30a2 8641 free_firmware();
43f66a6c
JK
8642
8643 priv->ieee->iw_mode = wrqu->mode;
bf79451e 8644
c848d0af 8645 queue_work(priv->workqueue, &priv->adapter_restart);
4644151b 8646 mutex_unlock(&priv->mutex);
0edd5b44 8647 return err;
43f66a6c
JK
8648}
8649
bf79451e 8650static int ipw_wx_get_mode(struct net_device *dev,
0edd5b44
JG
8651 struct iw_request_info *info,
8652 union iwreq_data *wrqu, char *extra)
43f66a6c
JK
8653{
8654 struct ipw_priv *priv = ieee80211_priv(dev);
4644151b 8655 mutex_lock(&priv->mutex);
43f66a6c
JK
8656 wrqu->mode = priv->ieee->iw_mode;
8657 IPW_DEBUG_WX("Get MODE -> %d\n", wrqu->mode);
4644151b 8658 mutex_unlock(&priv->mutex);
43f66a6c
JK
8659 return 0;
8660}
8661
43f66a6c
JK
8662/* Values are in microsecond */
8663static const s32 timeout_duration[] = {
8664 350000,
8665 250000,
8666 75000,
8667 37000,
8668 25000,
8669};
8670
8671static const s32 period_duration[] = {
8672 400000,
8673 700000,
8674 1000000,
8675 1000000,
8676 1000000
8677};
8678
bf79451e
JG
8679static int ipw_wx_get_range(struct net_device *dev,
8680 struct iw_request_info *info,
43f66a6c
JK
8681 union iwreq_data *wrqu, char *extra)
8682{
8683 struct ipw_priv *priv = ieee80211_priv(dev);
8684 struct iw_range *range = (struct iw_range *)extra;
1867b117 8685 const struct ieee80211_geo *geo = ieee80211_get_geo(priv->ieee);
b095c381 8686 int i = 0, j;
43f66a6c
JK
8687
8688 wrqu->data.length = sizeof(*range);
8689 memset(range, 0, sizeof(*range));
8690
8691 /* 54Mbs == ~27 Mb/s real (802.11g) */
bf79451e 8692 range->throughput = 27 * 1000 * 1000;
43f66a6c
JK
8693
8694 range->max_qual.qual = 100;
8695 /* TODO: Find real max RSSI and stick here */
8696 range->max_qual.level = 0;
b191608a 8697 range->max_qual.noise = 0;
0edd5b44 8698 range->max_qual.updated = 7; /* Updated all three */
43f66a6c
JK
8699
8700 range->avg_qual.qual = 70;
8701 /* TODO: Find real 'good' to 'bad' threshol value for RSSI */
0edd5b44 8702 range->avg_qual.level = 0; /* FIXME to real average level */
43f66a6c 8703 range->avg_qual.noise = 0;
0edd5b44 8704 range->avg_qual.updated = 7; /* Updated all three */
4644151b 8705 mutex_lock(&priv->mutex);
0edd5b44 8706 range->num_bitrates = min(priv->rates.num_rates, (u8) IW_MAX_BITRATES);
43f66a6c 8707
bf79451e
JG
8708 for (i = 0; i < range->num_bitrates; i++)
8709 range->bitrate[i] = (priv->rates.supported_rates[i] & 0x7F) *
0edd5b44 8710 500000;
bf79451e 8711
43f66a6c
JK
8712 range->max_rts = DEFAULT_RTS_THRESHOLD;
8713 range->min_frag = MIN_FRAG_THRESHOLD;
8714 range->max_frag = MAX_FRAG_THRESHOLD;
8715
8716 range->encoding_size[0] = 5;
bf79451e 8717 range->encoding_size[1] = 13;
43f66a6c
JK
8718 range->num_encoding_sizes = 2;
8719 range->max_encoding_tokens = WEP_KEYS;
8720
8721 /* Set the Wireless Extension versions */
8722 range->we_version_compiled = WIRELESS_EXT;
f1b50863 8723 range->we_version_source = 18;
43f66a6c 8724
b095c381
JK
8725 i = 0;
8726 if (priv->ieee->mode & (IEEE_B | IEEE_G)) {
e815de42
ZY
8727 for (j = 0; j < geo->bg_channels && i < IW_MAX_FREQUENCIES; j++) {
8728 if ((priv->ieee->iw_mode == IW_MODE_ADHOC) &&
8729 (geo->bg[j].flags & IEEE80211_CH_PASSIVE_ONLY))
8730 continue;
8731
b095c381
JK
8732 range->freq[i].i = geo->bg[j].channel;
8733 range->freq[i].m = geo->bg[j].freq * 100000;
8734 range->freq[i].e = 1;
e815de42 8735 i++;
b095c381
JK
8736 }
8737 }
43f66a6c 8738
b095c381 8739 if (priv->ieee->mode & IEEE_A) {
e815de42
ZY
8740 for (j = 0; j < geo->a_channels && i < IW_MAX_FREQUENCIES; j++) {
8741 if ((priv->ieee->iw_mode == IW_MODE_ADHOC) &&
8742 (geo->a[j].flags & IEEE80211_CH_PASSIVE_ONLY))
8743 continue;
8744
b095c381
JK
8745 range->freq[i].i = geo->a[j].channel;
8746 range->freq[i].m = geo->a[j].freq * 100000;
8747 range->freq[i].e = 1;
e815de42 8748 i++;
b095c381 8749 }
43f66a6c 8750 }
b095c381
JK
8751
8752 range->num_channels = i;
8753 range->num_frequency = i;
8754
4644151b 8755 mutex_unlock(&priv->mutex);
97a78ca9
BB
8756
8757 /* Event capability (kernel + driver) */
8758 range->event_capa[0] = (IW_EVENT_CAPA_K_0 |
8759 IW_EVENT_CAPA_MASK(SIOCGIWTHRSPY) |
07f02e46
ZY
8760 IW_EVENT_CAPA_MASK(SIOCGIWAP) |
8761 IW_EVENT_CAPA_MASK(SIOCGIWSCAN));
97a78ca9 8762 range->event_capa[1] = IW_EVENT_CAPA_K_1;
43f66a6c 8763
f1b50863
DW
8764 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
8765 IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
8766
43f66a6c
JK
8767 IPW_DEBUG_WX("GET Range\n");
8768 return 0;
8769}
8770
bf79451e
JG
8771static int ipw_wx_set_wap(struct net_device *dev,
8772 struct iw_request_info *info,
43f66a6c
JK
8773 union iwreq_data *wrqu, char *extra)
8774{
8775 struct ipw_priv *priv = ieee80211_priv(dev);
8776
8777 static const unsigned char any[] = {
8778 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
8779 };
8780 static const unsigned char off[] = {
8781 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
8782 };
8783
bf79451e 8784 if (wrqu->ap_addr.sa_family != ARPHRD_ETHER)
43f66a6c 8785 return -EINVAL;
4644151b 8786 mutex_lock(&priv->mutex);
43f66a6c
JK
8787 if (!memcmp(any, wrqu->ap_addr.sa_data, ETH_ALEN) ||
8788 !memcmp(off, wrqu->ap_addr.sa_data, ETH_ALEN)) {
8789 /* we disable mandatory BSSID association */
8790 IPW_DEBUG_WX("Setting AP BSSID to ANY\n");
8791 priv->config &= ~CFG_STATIC_BSSID;
c848d0af
JK
8792 IPW_DEBUG_ASSOC("Attempting to associate with new "
8793 "parameters.\n");
8794 ipw_associate(priv);
4644151b 8795 mutex_unlock(&priv->mutex);
43f66a6c
JK
8796 return 0;
8797 }
8798
8799 priv->config |= CFG_STATIC_BSSID;
8800 if (!memcmp(priv->bssid, wrqu->ap_addr.sa_data, ETH_ALEN)) {
8801 IPW_DEBUG_WX("BSSID set to current BSSID.\n");
4644151b 8802 mutex_unlock(&priv->mutex);
43f66a6c
JK
8803 return 0;
8804 }
8805
8806 IPW_DEBUG_WX("Setting mandatory BSSID to " MAC_FMT "\n",
8807 MAC_ARG(wrqu->ap_addr.sa_data));
8808
8809 memcpy(priv->bssid, wrqu->ap_addr.sa_data, ETH_ALEN);
8810
c848d0af
JK
8811 /* Network configuration changed -- force [re]association */
8812 IPW_DEBUG_ASSOC("[re]association triggered due to BSSID change.\n");
8813 if (!ipw_disassociate(priv))
43f66a6c 8814 ipw_associate(priv);
43f66a6c 8815
4644151b 8816 mutex_unlock(&priv->mutex);
43f66a6c
JK
8817 return 0;
8818}
8819
bf79451e
JG
8820static int ipw_wx_get_wap(struct net_device *dev,
8821 struct iw_request_info *info,
43f66a6c
JK
8822 union iwreq_data *wrqu, char *extra)
8823{
8824 struct ipw_priv *priv = ieee80211_priv(dev);
8825 /* If we are associated, trying to associate, or have a statically
8826 * configured BSSID then return that; otherwise return ANY */
4644151b 8827 mutex_lock(&priv->mutex);
bf79451e 8828 if (priv->config & CFG_STATIC_BSSID ||
43f66a6c
JK
8829 priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) {
8830 wrqu->ap_addr.sa_family = ARPHRD_ETHER;
afbf30a2 8831 memcpy(wrqu->ap_addr.sa_data, priv->bssid, ETH_ALEN);
43f66a6c
JK
8832 } else
8833 memset(wrqu->ap_addr.sa_data, 0, ETH_ALEN);
8834
8835 IPW_DEBUG_WX("Getting WAP BSSID: " MAC_FMT "\n",
8836 MAC_ARG(wrqu->ap_addr.sa_data));
4644151b 8837 mutex_unlock(&priv->mutex);
43f66a6c
JK
8838 return 0;
8839}
8840
bf79451e
JG
8841static int ipw_wx_set_essid(struct net_device *dev,
8842 struct iw_request_info *info,
43f66a6c
JK
8843 union iwreq_data *wrqu, char *extra)
8844{
8845 struct ipw_priv *priv = ieee80211_priv(dev);
ab644b0b
ZY
8846 int length;
8847
8848 mutex_lock(&priv->mutex);
8849
8850 if (!wrqu->essid.flags)
8851 {
8852 IPW_DEBUG_WX("Setting ESSID to ANY\n");
8853 ipw_disassociate(priv);
8854 priv->config &= ~CFG_STATIC_ESSID;
8855 ipw_associate(priv);
8856 mutex_unlock(&priv->mutex);
8857 return 0;
8858 }
43f66a6c 8859
a9f0d423
ZY
8860 length = min((int)wrqu->essid.length, IW_ESSID_MAX_SIZE);
8861 if (!extra[length - 1])
8862 length--;
43f66a6c
JK
8863
8864 priv->config |= CFG_STATIC_ESSID;
8865
a9f0d423
ZY
8866 if (priv->essid_len == length && !memcmp(priv->essid, extra, length)
8867 && (priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING))) {
43f66a6c 8868 IPW_DEBUG_WX("ESSID set to current ESSID.\n");
4644151b 8869 mutex_unlock(&priv->mutex);
43f66a6c
JK
8870 return 0;
8871 }
8872
ab644b0b 8873 IPW_DEBUG_WX("Setting ESSID: '%s' (%d)\n", escape_essid(extra, length),
43f66a6c
JK
8874 length);
8875
8876 priv->essid_len = length;
a9f0d423 8877 memcpy(priv->essid, extra, priv->essid_len);
bf79451e 8878
c848d0af
JK
8879 /* Network configuration changed -- force [re]association */
8880 IPW_DEBUG_ASSOC("[re]association triggered due to ESSID change.\n");
8881 if (!ipw_disassociate(priv))
43f66a6c 8882 ipw_associate(priv);
43f66a6c 8883
4644151b 8884 mutex_unlock(&priv->mutex);
43f66a6c
JK
8885 return 0;
8886}
8887
bf79451e
JG
8888static int ipw_wx_get_essid(struct net_device *dev,
8889 struct iw_request_info *info,
43f66a6c
JK
8890 union iwreq_data *wrqu, char *extra)
8891{
8892 struct ipw_priv *priv = ieee80211_priv(dev);
8893
8894 /* If we are associated, trying to associate, or have a statically
8895 * configured ESSID then return that; otherwise return ANY */
4644151b 8896 mutex_lock(&priv->mutex);
43f66a6c 8897 if (priv->config & CFG_STATIC_ESSID ||
bf79451e
JG
8898 priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) {
8899 IPW_DEBUG_WX("Getting essid: '%s'\n",
43f66a6c 8900 escape_essid(priv->essid, priv->essid_len));
bf79451e 8901 memcpy(extra, priv->essid, priv->essid_len);
43f66a6c 8902 wrqu->essid.length = priv->essid_len;
0edd5b44 8903 wrqu->essid.flags = 1; /* active */
43f66a6c
JK
8904 } else {
8905 IPW_DEBUG_WX("Getting essid: ANY\n");
8906 wrqu->essid.length = 0;
0edd5b44 8907 wrqu->essid.flags = 0; /* active */
43f66a6c 8908 }
4644151b 8909 mutex_unlock(&priv->mutex);
43f66a6c
JK
8910 return 0;
8911}
8912
bf79451e
JG
8913static int ipw_wx_set_nick(struct net_device *dev,
8914 struct iw_request_info *info,
43f66a6c 8915 union iwreq_data *wrqu, char *extra)
bf79451e 8916{
43f66a6c
JK
8917 struct ipw_priv *priv = ieee80211_priv(dev);
8918
8919 IPW_DEBUG_WX("Setting nick to '%s'\n", extra);
8920 if (wrqu->data.length > IW_ESSID_MAX_SIZE)
8921 return -E2BIG;
4644151b 8922 mutex_lock(&priv->mutex);
0edd5b44 8923 wrqu->data.length = min((size_t) wrqu->data.length, sizeof(priv->nick));
43f66a6c 8924 memset(priv->nick, 0, sizeof(priv->nick));
0edd5b44 8925 memcpy(priv->nick, extra, wrqu->data.length);
43f66a6c 8926 IPW_DEBUG_TRACE("<<\n");
4644151b 8927 mutex_unlock(&priv->mutex);
43f66a6c
JK
8928 return 0;
8929
8930}
8931
bf79451e
JG
8932static int ipw_wx_get_nick(struct net_device *dev,
8933 struct iw_request_info *info,
43f66a6c 8934 union iwreq_data *wrqu, char *extra)
bf79451e 8935{
43f66a6c
JK
8936 struct ipw_priv *priv = ieee80211_priv(dev);
8937 IPW_DEBUG_WX("Getting nick\n");
4644151b 8938 mutex_lock(&priv->mutex);
43f66a6c
JK
8939 wrqu->data.length = strlen(priv->nick) + 1;
8940 memcpy(extra, priv->nick, wrqu->data.length);
0edd5b44 8941 wrqu->data.flags = 1; /* active */
4644151b 8942 mutex_unlock(&priv->mutex);
43f66a6c
JK
8943 return 0;
8944}
8945
651be26f
OH
8946static int ipw_wx_set_sens(struct net_device *dev,
8947 struct iw_request_info *info,
8948 union iwreq_data *wrqu, char *extra)
8949{
8950 struct ipw_priv *priv = ieee80211_priv(dev);
8951 int err = 0;
8952
8953 IPW_DEBUG_WX("Setting roaming threshold to %d\n", wrqu->sens.value);
8954 IPW_DEBUG_WX("Setting disassociate threshold to %d\n", 3*wrqu->sens.value);
8955 mutex_lock(&priv->mutex);
8956
8957 if (wrqu->sens.fixed == 0)
8958 {
8959 priv->roaming_threshold = IPW_MB_ROAMING_THRESHOLD_DEFAULT;
8960 priv->disassociate_threshold = IPW_MB_DISASSOCIATE_THRESHOLD_DEFAULT;
8961 goto out;
8962 }
8963 if ((wrqu->sens.value > IPW_MB_ROAMING_THRESHOLD_MAX) ||
8964 (wrqu->sens.value < IPW_MB_ROAMING_THRESHOLD_MIN)) {
8965 err = -EINVAL;
8966 goto out;
8967 }
8968
8969 priv->roaming_threshold = wrqu->sens.value;
8970 priv->disassociate_threshold = 3*wrqu->sens.value;
8971 out:
8972 mutex_unlock(&priv->mutex);
8973 return err;
8974}
8975
8976static int ipw_wx_get_sens(struct net_device *dev,
8977 struct iw_request_info *info,
8978 union iwreq_data *wrqu, char *extra)
8979{
8980 struct ipw_priv *priv = ieee80211_priv(dev);
8981 mutex_lock(&priv->mutex);
8982 wrqu->sens.fixed = 1;
8983 wrqu->sens.value = priv->roaming_threshold;
8984 mutex_unlock(&priv->mutex);
8985
8986 IPW_DEBUG_WX("GET roaming threshold -> %s %d \n",
8987 wrqu->power.disabled ? "OFF" : "ON", wrqu->power.value);
8988
8989 return 0;
8990}
8991
43f66a6c
JK
8992static int ipw_wx_set_rate(struct net_device *dev,
8993 struct iw_request_info *info,
8994 union iwreq_data *wrqu, char *extra)
bf79451e 8995{
ea2b26e0
JK
8996 /* TODO: We should use semaphores or locks for access to priv */
8997 struct ipw_priv *priv = ieee80211_priv(dev);
8998 u32 target_rate = wrqu->bitrate.value;
8999 u32 fixed, mask;
9000
9001 /* value = -1, fixed = 0 means auto only, so we should use all rates offered by AP */
9002 /* value = X, fixed = 1 means only rate X */
9003 /* value = X, fixed = 0 means all rates lower equal X */
9004
9005 if (target_rate == -1) {
9006 fixed = 0;
9007 mask = IEEE80211_DEFAULT_RATES_MASK;
9008 /* Now we should reassociate */
9009 goto apply;
9010 }
9011
9012 mask = 0;
9013 fixed = wrqu->bitrate.fixed;
9014
9015 if (target_rate == 1000000 || !fixed)
9016 mask |= IEEE80211_CCK_RATE_1MB_MASK;
9017 if (target_rate == 1000000)
9018 goto apply;
9019
9020 if (target_rate == 2000000 || !fixed)
9021 mask |= IEEE80211_CCK_RATE_2MB_MASK;
9022 if (target_rate == 2000000)
9023 goto apply;
9024
9025 if (target_rate == 5500000 || !fixed)
9026 mask |= IEEE80211_CCK_RATE_5MB_MASK;
9027 if (target_rate == 5500000)
9028 goto apply;
9029
9030 if (target_rate == 6000000 || !fixed)
9031 mask |= IEEE80211_OFDM_RATE_6MB_MASK;
9032 if (target_rate == 6000000)
9033 goto apply;
9034
9035 if (target_rate == 9000000 || !fixed)
9036 mask |= IEEE80211_OFDM_RATE_9MB_MASK;
9037 if (target_rate == 9000000)
9038 goto apply;
9039
9040 if (target_rate == 11000000 || !fixed)
9041 mask |= IEEE80211_CCK_RATE_11MB_MASK;
9042 if (target_rate == 11000000)
9043 goto apply;
9044
9045 if (target_rate == 12000000 || !fixed)
9046 mask |= IEEE80211_OFDM_RATE_12MB_MASK;
9047 if (target_rate == 12000000)
9048 goto apply;
9049
9050 if (target_rate == 18000000 || !fixed)
9051 mask |= IEEE80211_OFDM_RATE_18MB_MASK;
9052 if (target_rate == 18000000)
9053 goto apply;
9054
9055 if (target_rate == 24000000 || !fixed)
9056 mask |= IEEE80211_OFDM_RATE_24MB_MASK;
9057 if (target_rate == 24000000)
9058 goto apply;
9059
9060 if (target_rate == 36000000 || !fixed)
9061 mask |= IEEE80211_OFDM_RATE_36MB_MASK;
9062 if (target_rate == 36000000)
9063 goto apply;
9064
9065 if (target_rate == 48000000 || !fixed)
9066 mask |= IEEE80211_OFDM_RATE_48MB_MASK;
9067 if (target_rate == 48000000)
9068 goto apply;
9069
9070 if (target_rate == 54000000 || !fixed)
9071 mask |= IEEE80211_OFDM_RATE_54MB_MASK;
9072 if (target_rate == 54000000)
9073 goto apply;
9074
9075 IPW_DEBUG_WX("invalid rate specified, returning error\n");
9076 return -EINVAL;
9077
9078 apply:
9079 IPW_DEBUG_WX("Setting rate mask to 0x%08X [%s]\n",
9080 mask, fixed ? "fixed" : "sub-rates");
4644151b 9081 mutex_lock(&priv->mutex);
b095c381 9082 if (mask == IEEE80211_DEFAULT_RATES_MASK) {
ea2b26e0 9083 priv->config &= ~CFG_FIXED_RATE;
b095c381
JK
9084 ipw_set_fixed_rate(priv, priv->ieee->mode);
9085 } else
ea2b26e0
JK
9086 priv->config |= CFG_FIXED_RATE;
9087
c848d0af
JK
9088 if (priv->rates_mask == mask) {
9089 IPW_DEBUG_WX("Mask set to current mask.\n");
4644151b 9090 mutex_unlock(&priv->mutex);
c848d0af 9091 return 0;
ea2b26e0
JK
9092 }
9093
c848d0af
JK
9094 priv->rates_mask = mask;
9095
9096 /* Network configuration changed -- force [re]association */
9097 IPW_DEBUG_ASSOC("[re]association triggered due to rates change.\n");
9098 if (!ipw_disassociate(priv))
9099 ipw_associate(priv);
9100
4644151b 9101 mutex_unlock(&priv->mutex);
ea2b26e0 9102 return 0;
43f66a6c
JK
9103}
9104
bf79451e
JG
9105static int ipw_wx_get_rate(struct net_device *dev,
9106 struct iw_request_info *info,
43f66a6c 9107 union iwreq_data *wrqu, char *extra)
bf79451e 9108{
0edd5b44 9109 struct ipw_priv *priv = ieee80211_priv(dev);
4644151b 9110 mutex_lock(&priv->mutex);
43f66a6c 9111 wrqu->bitrate.value = priv->last_rate;
455936c7 9112 wrqu->bitrate.fixed = (priv->config & CFG_FIXED_RATE) ? 1 : 0;
4644151b 9113 mutex_unlock(&priv->mutex);
43f66a6c
JK
9114 IPW_DEBUG_WX("GET Rate -> %d \n", wrqu->bitrate.value);
9115 return 0;
9116}
9117
bf79451e
JG
9118static int ipw_wx_set_rts(struct net_device *dev,
9119 struct iw_request_info *info,
43f66a6c 9120 union iwreq_data *wrqu, char *extra)
bf79451e 9121{
43f66a6c 9122 struct ipw_priv *priv = ieee80211_priv(dev);
4644151b 9123 mutex_lock(&priv->mutex);
43f66a6c
JK
9124 if (wrqu->rts.disabled)
9125 priv->rts_threshold = DEFAULT_RTS_THRESHOLD;
9126 else {
9127 if (wrqu->rts.value < MIN_RTS_THRESHOLD ||
c848d0af 9128 wrqu->rts.value > MAX_RTS_THRESHOLD) {
4644151b 9129 mutex_unlock(&priv->mutex);
43f66a6c 9130 return -EINVAL;
c848d0af 9131 }
43f66a6c
JK
9132 priv->rts_threshold = wrqu->rts.value;
9133 }
9134
9135 ipw_send_rts_threshold(priv, priv->rts_threshold);
4644151b 9136 mutex_unlock(&priv->mutex);
43f66a6c
JK
9137 IPW_DEBUG_WX("SET RTS Threshold -> %d \n", priv->rts_threshold);
9138 return 0;
9139}
9140
bf79451e
JG
9141static int ipw_wx_get_rts(struct net_device *dev,
9142 struct iw_request_info *info,
43f66a6c 9143 union iwreq_data *wrqu, char *extra)
bf79451e 9144{
43f66a6c 9145 struct ipw_priv *priv = ieee80211_priv(dev);
4644151b 9146 mutex_lock(&priv->mutex);
43f66a6c
JK
9147 wrqu->rts.value = priv->rts_threshold;
9148 wrqu->rts.fixed = 0; /* no auto select */
0edd5b44 9149 wrqu->rts.disabled = (wrqu->rts.value == DEFAULT_RTS_THRESHOLD);
4644151b 9150 mutex_unlock(&priv->mutex);
43f66a6c
JK
9151 IPW_DEBUG_WX("GET RTS Threshold -> %d \n", wrqu->rts.value);
9152 return 0;
9153}
9154
bf79451e
JG
9155static int ipw_wx_set_txpow(struct net_device *dev,
9156 struct iw_request_info *info,
43f66a6c 9157 union iwreq_data *wrqu, char *extra)
bf79451e 9158{
43f66a6c 9159 struct ipw_priv *priv = ieee80211_priv(dev);
6de9f7f2 9160 int err = 0;
43f66a6c 9161
4644151b 9162 mutex_lock(&priv->mutex);
c848d0af 9163 if (ipw_radio_kill_sw(priv, wrqu->power.disabled)) {
6de9f7f2
ZY
9164 err = -EINPROGRESS;
9165 goto out;
43f66a6c 9166 }
43f66a6c 9167
b095c381
JK
9168 if (!wrqu->power.fixed)
9169 wrqu->power.value = IPW_TX_POWER_DEFAULT;
9170
c848d0af 9171 if (wrqu->power.flags != IW_TXPOW_DBM) {
6de9f7f2
ZY
9172 err = -EINVAL;
9173 goto out;
c848d0af 9174 }
43f66a6c 9175
b095c381 9176 if ((wrqu->power.value > IPW_TX_POWER_MAX) ||
afbf30a2 9177 (wrqu->power.value < IPW_TX_POWER_MIN)) {
6de9f7f2
ZY
9178 err = -EINVAL;
9179 goto out;
c848d0af 9180 }
43f66a6c 9181
43f66a6c 9182 priv->tx_power = wrqu->power.value;
6de9f7f2
ZY
9183 err = ipw_set_tx_power(priv);
9184 out:
4644151b 9185 mutex_unlock(&priv->mutex);
6de9f7f2 9186 return err;
43f66a6c
JK
9187}
9188
bf79451e
JG
9189static int ipw_wx_get_txpow(struct net_device *dev,
9190 struct iw_request_info *info,
43f66a6c 9191 union iwreq_data *wrqu, char *extra)
bf79451e 9192{
43f66a6c 9193 struct ipw_priv *priv = ieee80211_priv(dev);
4644151b 9194 mutex_lock(&priv->mutex);
43f66a6c
JK
9195 wrqu->power.value = priv->tx_power;
9196 wrqu->power.fixed = 1;
9197 wrqu->power.flags = IW_TXPOW_DBM;
9198 wrqu->power.disabled = (priv->status & STATUS_RF_KILL_MASK) ? 1 : 0;
4644151b 9199 mutex_unlock(&priv->mutex);
43f66a6c 9200
bf79451e 9201 IPW_DEBUG_WX("GET TX Power -> %s %d \n",
22501c8e 9202 wrqu->power.disabled ? "OFF" : "ON", wrqu->power.value);
43f66a6c
JK
9203
9204 return 0;
9205}
9206
bf79451e 9207static int ipw_wx_set_frag(struct net_device *dev,
0edd5b44
JG
9208 struct iw_request_info *info,
9209 union iwreq_data *wrqu, char *extra)
43f66a6c
JK
9210{
9211 struct ipw_priv *priv = ieee80211_priv(dev);
4644151b 9212 mutex_lock(&priv->mutex);
43f66a6c
JK
9213 if (wrqu->frag.disabled)
9214 priv->ieee->fts = DEFAULT_FTS;
9215 else {
9216 if (wrqu->frag.value < MIN_FRAG_THRESHOLD ||
b095c381 9217 wrqu->frag.value > MAX_FRAG_THRESHOLD) {
4644151b 9218 mutex_unlock(&priv->mutex);
43f66a6c 9219 return -EINVAL;
b095c381 9220 }
bf79451e 9221
43f66a6c
JK
9222 priv->ieee->fts = wrqu->frag.value & ~0x1;
9223 }
9224
9225 ipw_send_frag_threshold(priv, wrqu->frag.value);
4644151b 9226 mutex_unlock(&priv->mutex);
43f66a6c
JK
9227 IPW_DEBUG_WX("SET Frag Threshold -> %d \n", wrqu->frag.value);
9228 return 0;
9229}
9230
bf79451e 9231static int ipw_wx_get_frag(struct net_device *dev,
0edd5b44
JG
9232 struct iw_request_info *info,
9233 union iwreq_data *wrqu, char *extra)
43f66a6c
JK
9234{
9235 struct ipw_priv *priv = ieee80211_priv(dev);
4644151b 9236 mutex_lock(&priv->mutex);
43f66a6c
JK
9237 wrqu->frag.value = priv->ieee->fts;
9238 wrqu->frag.fixed = 0; /* no auto select */
0edd5b44 9239 wrqu->frag.disabled = (wrqu->frag.value == DEFAULT_FTS);
4644151b 9240 mutex_unlock(&priv->mutex);
43f66a6c
JK
9241 IPW_DEBUG_WX("GET Frag Threshold -> %d \n", wrqu->frag.value);
9242
9243 return 0;
9244}
9245
bf79451e
JG
9246static int ipw_wx_set_retry(struct net_device *dev,
9247 struct iw_request_info *info,
43f66a6c 9248 union iwreq_data *wrqu, char *extra)
bf79451e 9249{
afbf30a2
JK
9250 struct ipw_priv *priv = ieee80211_priv(dev);
9251
9252 if (wrqu->retry.flags & IW_RETRY_LIFETIME || wrqu->retry.disabled)
9253 return -EINVAL;
9254
9255 if (!(wrqu->retry.flags & IW_RETRY_LIMIT))
9256 return 0;
9257
9258 if (wrqu->retry.value < 0 || wrqu->retry.value > 255)
9259 return -EINVAL;
9260
4644151b 9261 mutex_lock(&priv->mutex);
afbf30a2
JK
9262 if (wrqu->retry.flags & IW_RETRY_MIN)
9263 priv->short_retry_limit = (u8) wrqu->retry.value;
9264 else if (wrqu->retry.flags & IW_RETRY_MAX)
9265 priv->long_retry_limit = (u8) wrqu->retry.value;
9266 else {
9267 priv->short_retry_limit = (u8) wrqu->retry.value;
9268 priv->long_retry_limit = (u8) wrqu->retry.value;
9269 }
9270
9271 ipw_send_retry_limit(priv, priv->short_retry_limit,
9272 priv->long_retry_limit);
4644151b 9273 mutex_unlock(&priv->mutex);
afbf30a2
JK
9274 IPW_DEBUG_WX("SET retry limit -> short:%d long:%d\n",
9275 priv->short_retry_limit, priv->long_retry_limit);
9276 return 0;
43f66a6c
JK
9277}
9278
bf79451e
JG
9279static int ipw_wx_get_retry(struct net_device *dev,
9280 struct iw_request_info *info,
43f66a6c 9281 union iwreq_data *wrqu, char *extra)
bf79451e 9282{
afbf30a2
JK
9283 struct ipw_priv *priv = ieee80211_priv(dev);
9284
4644151b 9285 mutex_lock(&priv->mutex);
afbf30a2
JK
9286 wrqu->retry.disabled = 0;
9287
9288 if ((wrqu->retry.flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME) {
4644151b 9289 mutex_unlock(&priv->mutex);
afbf30a2
JK
9290 return -EINVAL;
9291 }
9292
9293 if (wrqu->retry.flags & IW_RETRY_MAX) {
9294 wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
9295 wrqu->retry.value = priv->long_retry_limit;
9296 } else if (wrqu->retry.flags & IW_RETRY_MIN) {
9297 wrqu->retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
9298 wrqu->retry.value = priv->short_retry_limit;
9299 } else {
9300 wrqu->retry.flags = IW_RETRY_LIMIT;
9301 wrqu->retry.value = priv->short_retry_limit;
9302 }
4644151b 9303 mutex_unlock(&priv->mutex);
afbf30a2
JK
9304
9305 IPW_DEBUG_WX("GET retry -> %d \n", wrqu->retry.value);
9306
9307 return 0;
9308}
9309
afbf30a2
JK
9310static int ipw_request_direct_scan(struct ipw_priv *priv, char *essid,
9311 int essid_len)
9312{
9313 struct ipw_scan_request_ext scan;
9314 int err = 0, scan_type;
9315
efb3442c
PE
9316 if (!(priv->status & STATUS_INIT) ||
9317 (priv->status & STATUS_EXIT_PENDING))
9318 return 0;
9319
4644151b 9320 mutex_lock(&priv->mutex);
afbf30a2
JK
9321
9322 if (priv->status & STATUS_RF_KILL_MASK) {
9323 IPW_DEBUG_HC("Aborting scan due to RF kill activation\n");
9324 priv->status |= STATUS_SCAN_PENDING;
9325 goto done;
9326 }
9327
9328 IPW_DEBUG_HC("starting request direct scan!\n");
9329
9330 if (priv->status & (STATUS_SCANNING | STATUS_SCAN_ABORTING)) {
d834a41c
OK
9331 /* We should not sleep here; otherwise we will block most
9332 * of the system (for instance, we hold rtnl_lock when we
9333 * get here).
9334 */
9335 err = -EAGAIN;
9336 goto done;
afbf30a2
JK
9337 }
9338 memset(&scan, 0, sizeof(scan));
9339
9340 if (priv->config & CFG_SPEED_SCAN)
9341 scan.dwell_time[IPW_SCAN_ACTIVE_BROADCAST_SCAN] =
9342 cpu_to_le16(30);
9343 else
9344 scan.dwell_time[IPW_SCAN_ACTIVE_BROADCAST_SCAN] =
9345 cpu_to_le16(20);
9346
9347 scan.dwell_time[IPW_SCAN_ACTIVE_BROADCAST_AND_DIRECT_SCAN] =
9348 cpu_to_le16(20);
1fe0adb4 9349 scan.dwell_time[IPW_SCAN_PASSIVE_FULL_DWELL_SCAN] = cpu_to_le16(120);
afbf30a2
JK
9350 scan.dwell_time[IPW_SCAN_ACTIVE_DIRECT_SCAN] = cpu_to_le16(20);
9351
9352 scan.full_scan_index = cpu_to_le32(ieee80211_get_scans(priv->ieee));
9353
9354 err = ipw_send_ssid(priv, essid, essid_len);
9355 if (err) {
9356 IPW_DEBUG_HC("Attempt to send SSID command failed\n");
9357 goto done;
9358 }
9359 scan_type = IPW_SCAN_ACTIVE_BROADCAST_AND_DIRECT_SCAN;
9360
9361 ipw_add_scan_channels(priv, &scan, scan_type);
9362
9363 err = ipw_send_scan_request_ext(priv, &scan);
9364 if (err) {
9365 IPW_DEBUG_HC("Sending scan command failed: %08X\n", err);
9366 goto done;
9367 }
9368
9369 priv->status |= STATUS_SCANNING;
9370
9371 done:
4644151b 9372 mutex_unlock(&priv->mutex);
afbf30a2 9373 return err;
43f66a6c
JK
9374}
9375
bf79451e
JG
9376static int ipw_wx_set_scan(struct net_device *dev,
9377 struct iw_request_info *info,
43f66a6c
JK
9378 union iwreq_data *wrqu, char *extra)
9379{
9380 struct ipw_priv *priv = ieee80211_priv(dev);
afbf30a2
JK
9381 struct iw_scan_req *req = NULL;
9382 if (wrqu->data.length
9383 && wrqu->data.length == sizeof(struct iw_scan_req)) {
9384 req = (struct iw_scan_req *)extra;
9385 if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
9386 ipw_request_direct_scan(priv, req->essid,
9387 req->essid_len);
9388 return 0;
9389 }
9390 }
8935f39e 9391
43f66a6c 9392 IPW_DEBUG_WX("Start scan\n");
b095c381
JK
9393
9394 queue_work(priv->workqueue, &priv->request_scan);
9395
43f66a6c
JK
9396 return 0;
9397}
9398
bf79451e
JG
9399static int ipw_wx_get_scan(struct net_device *dev,
9400 struct iw_request_info *info,
43f66a6c 9401 union iwreq_data *wrqu, char *extra)
bf79451e 9402{
43f66a6c
JK
9403 struct ipw_priv *priv = ieee80211_priv(dev);
9404 return ieee80211_wx_get_scan(priv->ieee, info, wrqu, extra);
9405}
9406
bf79451e 9407static int ipw_wx_set_encode(struct net_device *dev,
0edd5b44
JG
9408 struct iw_request_info *info,
9409 union iwreq_data *wrqu, char *key)
43f66a6c
JK
9410{
9411 struct ipw_priv *priv = ieee80211_priv(dev);
afbf30a2 9412 int ret;
caeff81b 9413 u32 cap = priv->capability;
afbf30a2 9414
4644151b 9415 mutex_lock(&priv->mutex);
afbf30a2 9416 ret = ieee80211_wx_set_encode(priv->ieee, info, wrqu, key);
afbf30a2 9417
caeff81b
HL
9418 /* In IBSS mode, we need to notify the firmware to update
9419 * the beacon info after we changed the capability. */
9420 if (cap != priv->capability &&
9421 priv->ieee->iw_mode == IW_MODE_ADHOC &&
9422 priv->status & STATUS_ASSOCIATED)
9423 ipw_disassociate(priv);
9424
4644151b 9425 mutex_unlock(&priv->mutex);
afbf30a2 9426 return ret;
43f66a6c
JK
9427}
9428
bf79451e 9429static int ipw_wx_get_encode(struct net_device *dev,
0edd5b44
JG
9430 struct iw_request_info *info,
9431 union iwreq_data *wrqu, char *key)
43f66a6c
JK
9432{
9433 struct ipw_priv *priv = ieee80211_priv(dev);
9434 return ieee80211_wx_get_encode(priv->ieee, info, wrqu, key);
9435}
9436
bf79451e 9437static int ipw_wx_set_power(struct net_device *dev,
0edd5b44
JG
9438 struct iw_request_info *info,
9439 union iwreq_data *wrqu, char *extra)
43f66a6c
JK
9440{
9441 struct ipw_priv *priv = ieee80211_priv(dev);
9442 int err;
4644151b 9443 mutex_lock(&priv->mutex);
43f66a6c
JK
9444 if (wrqu->power.disabled) {
9445 priv->power_mode = IPW_POWER_LEVEL(priv->power_mode);
9446 err = ipw_send_power_mode(priv, IPW_POWER_MODE_CAM);
9447 if (err) {
9448 IPW_DEBUG_WX("failed setting power mode.\n");
4644151b 9449 mutex_unlock(&priv->mutex);
43f66a6c
JK
9450 return err;
9451 }
43f66a6c 9452 IPW_DEBUG_WX("SET Power Management Mode -> off\n");
4644151b 9453 mutex_unlock(&priv->mutex);
43f66a6c 9454 return 0;
bf79451e 9455 }
43f66a6c
JK
9456
9457 switch (wrqu->power.flags & IW_POWER_MODE) {
0edd5b44
JG
9458 case IW_POWER_ON: /* If not specified */
9459 case IW_POWER_MODE: /* If set all mask */
9460 case IW_POWER_ALL_R: /* If explicitely state all */
43f66a6c 9461 break;
0edd5b44 9462 default: /* Otherwise we don't support it */
43f66a6c
JK
9463 IPW_DEBUG_WX("SET PM Mode: %X not supported.\n",
9464 wrqu->power.flags);
4644151b 9465 mutex_unlock(&priv->mutex);
bf79451e 9466 return -EOPNOTSUPP;
43f66a6c 9467 }
bf79451e 9468
43f66a6c
JK
9469 /* If the user hasn't specified a power management mode yet, default
9470 * to BATTERY */
0edd5b44 9471 if (IPW_POWER_LEVEL(priv->power_mode) == IPW_POWER_AC)
43f66a6c 9472 priv->power_mode = IPW_POWER_ENABLED | IPW_POWER_BATTERY;
bf79451e 9473 else
43f66a6c
JK
9474 priv->power_mode = IPW_POWER_ENABLED | priv->power_mode;
9475 err = ipw_send_power_mode(priv, IPW_POWER_LEVEL(priv->power_mode));
9476 if (err) {
9477 IPW_DEBUG_WX("failed setting power mode.\n");
4644151b 9478 mutex_unlock(&priv->mutex);
43f66a6c
JK
9479 return err;
9480 }
9481
0edd5b44 9482 IPW_DEBUG_WX("SET Power Management Mode -> 0x%02X\n", priv->power_mode);
4644151b 9483 mutex_unlock(&priv->mutex);
43f66a6c
JK
9484 return 0;
9485}
9486
bf79451e 9487static int ipw_wx_get_power(struct net_device *dev,
0edd5b44
JG
9488 struct iw_request_info *info,
9489 union iwreq_data *wrqu, char *extra)
43f66a6c
JK
9490{
9491 struct ipw_priv *priv = ieee80211_priv(dev);
4644151b 9492 mutex_lock(&priv->mutex);
a613bffd 9493 if (!(priv->power_mode & IPW_POWER_ENABLED))
43f66a6c 9494 wrqu->power.disabled = 1;
a613bffd 9495 else
43f66a6c 9496 wrqu->power.disabled = 0;
43f66a6c 9497
4644151b 9498 mutex_unlock(&priv->mutex);
43f66a6c 9499 IPW_DEBUG_WX("GET Power Management Mode -> %02X\n", priv->power_mode);
bf79451e 9500
43f66a6c
JK
9501 return 0;
9502}
9503
bf79451e 9504static int ipw_wx_set_powermode(struct net_device *dev,
0edd5b44
JG
9505 struct iw_request_info *info,
9506 union iwreq_data *wrqu, char *extra)
43f66a6c
JK
9507{
9508 struct ipw_priv *priv = ieee80211_priv(dev);
9509 int mode = *(int *)extra;
9510 int err;
4644151b 9511 mutex_lock(&priv->mutex);
43f66a6c
JK
9512 if ((mode < 1) || (mode > IPW_POWER_LIMIT)) {
9513 mode = IPW_POWER_AC;
9514 priv->power_mode = mode;
9515 } else {
9516 priv->power_mode = IPW_POWER_ENABLED | mode;
9517 }
bf79451e 9518
43f66a6c
JK
9519 if (priv->power_mode != mode) {
9520 err = ipw_send_power_mode(priv, mode);
bf79451e 9521
43f66a6c
JK
9522 if (err) {
9523 IPW_DEBUG_WX("failed setting power mode.\n");
4644151b 9524 mutex_unlock(&priv->mutex);
43f66a6c
JK
9525 return err;
9526 }
9527 }
4644151b 9528 mutex_unlock(&priv->mutex);
43f66a6c
JK
9529 return 0;
9530}
9531
9532#define MAX_WX_STRING 80
bf79451e 9533static int ipw_wx_get_powermode(struct net_device *dev,
0edd5b44
JG
9534 struct iw_request_info *info,
9535 union iwreq_data *wrqu, char *extra)
43f66a6c
JK
9536{
9537 struct ipw_priv *priv = ieee80211_priv(dev);
9538 int level = IPW_POWER_LEVEL(priv->power_mode);
9539 char *p = extra;
9540
9541 p += snprintf(p, MAX_WX_STRING, "Power save level: %d ", level);
9542
9543 switch (level) {
9544 case IPW_POWER_AC:
9545 p += snprintf(p, MAX_WX_STRING - (p - extra), "(AC)");
9546 break;
9547 case IPW_POWER_BATTERY:
9548 p += snprintf(p, MAX_WX_STRING - (p - extra), "(BATTERY)");
9549 break;
9550 default:
9551 p += snprintf(p, MAX_WX_STRING - (p - extra),
bf79451e 9552 "(Timeout %dms, Period %dms)",
43f66a6c
JK
9553 timeout_duration[level - 1] / 1000,
9554 period_duration[level - 1] / 1000);
9555 }
9556
9557 if (!(priv->power_mode & IPW_POWER_ENABLED))
0edd5b44 9558 p += snprintf(p, MAX_WX_STRING - (p - extra), " OFF");
43f66a6c
JK
9559
9560 wrqu->data.length = p - extra + 1;
9561
9562 return 0;
9563}
9564
9565static int ipw_wx_set_wireless_mode(struct net_device *dev,
0edd5b44
JG
9566 struct iw_request_info *info,
9567 union iwreq_data *wrqu, char *extra)
43f66a6c 9568{
0edd5b44 9569 struct ipw_priv *priv = ieee80211_priv(dev);
43f66a6c
JK
9570 int mode = *(int *)extra;
9571 u8 band = 0, modulation = 0;
9572
9573 if (mode == 0 || mode & ~IEEE_MODE_MASK) {
0edd5b44 9574 IPW_WARNING("Attempt to set invalid wireless mode: %d\n", mode);
43f66a6c
JK
9575 return -EINVAL;
9576 }
4644151b 9577 mutex_lock(&priv->mutex);
43f66a6c 9578 if (priv->adapter == IPW_2915ABG) {
a33a1982 9579 priv->ieee->abg_true = 1;
43f66a6c
JK
9580 if (mode & IEEE_A) {
9581 band |= IEEE80211_52GHZ_BAND;
9582 modulation |= IEEE80211_OFDM_MODULATION;
9583 } else
a33a1982 9584 priv->ieee->abg_true = 0;
43f66a6c
JK
9585 } else {
9586 if (mode & IEEE_A) {
9587 IPW_WARNING("Attempt to set 2200BG into "
9588 "802.11a mode\n");
4644151b 9589 mutex_unlock(&priv->mutex);
43f66a6c
JK
9590 return -EINVAL;
9591 }
9592
a33a1982 9593 priv->ieee->abg_true = 0;
43f66a6c
JK
9594 }
9595
9596 if (mode & IEEE_B) {
9597 band |= IEEE80211_24GHZ_BAND;
9598 modulation |= IEEE80211_CCK_MODULATION;
9599 } else
a33a1982 9600 priv->ieee->abg_true = 0;
bf79451e 9601
43f66a6c
JK
9602 if (mode & IEEE_G) {
9603 band |= IEEE80211_24GHZ_BAND;
9604 modulation |= IEEE80211_OFDM_MODULATION;
9605 } else
a33a1982 9606 priv->ieee->abg_true = 0;
43f66a6c
JK
9607
9608 priv->ieee->mode = mode;
9609 priv->ieee->freq_band = band;
9610 priv->ieee->modulation = modulation;
0edd5b44 9611 init_supported_rates(priv, &priv->rates);
43f66a6c 9612
c848d0af
JK
9613 /* Network configuration changed -- force [re]association */
9614 IPW_DEBUG_ASSOC("[re]association triggered due to mode change.\n");
9615 if (!ipw_disassociate(priv)) {
43f66a6c 9616 ipw_send_supported_rates(priv, &priv->rates);
c848d0af
JK
9617 ipw_associate(priv);
9618 }
43f66a6c 9619
a613bffd
JK
9620 /* Update the band LEDs */
9621 ipw_led_band_on(priv);
43f66a6c 9622
bf79451e 9623 IPW_DEBUG_WX("PRIV SET MODE: %c%c%c\n",
43f66a6c 9624 mode & IEEE_A ? 'a' : '.',
0edd5b44 9625 mode & IEEE_B ? 'b' : '.', mode & IEEE_G ? 'g' : '.');
4644151b 9626 mutex_unlock(&priv->mutex);
43f66a6c
JK
9627 return 0;
9628}
9629
9630static int ipw_wx_get_wireless_mode(struct net_device *dev,
0edd5b44
JG
9631 struct iw_request_info *info,
9632 union iwreq_data *wrqu, char *extra)
43f66a6c 9633{
0edd5b44 9634 struct ipw_priv *priv = ieee80211_priv(dev);
4644151b 9635 mutex_lock(&priv->mutex);
ea2b26e0
JK
9636 switch (priv->ieee->mode) {
9637 case IEEE_A:
43f66a6c
JK
9638 strncpy(extra, "802.11a (1)", MAX_WX_STRING);
9639 break;
ea2b26e0
JK
9640 case IEEE_B:
9641 strncpy(extra, "802.11b (2)", MAX_WX_STRING);
9642 break;
9643 case IEEE_A | IEEE_B:
9644 strncpy(extra, "802.11ab (3)", MAX_WX_STRING);
9645 break;
9646 case IEEE_G:
9647 strncpy(extra, "802.11g (4)", MAX_WX_STRING);
9648 break;
9649 case IEEE_A | IEEE_G:
9650 strncpy(extra, "802.11ag (5)", MAX_WX_STRING);
9651 break;
9652 case IEEE_B | IEEE_G:
9653 strncpy(extra, "802.11bg (6)", MAX_WX_STRING);
9654 break;
9655 case IEEE_A | IEEE_B | IEEE_G:
9656 strncpy(extra, "802.11abg (7)", MAX_WX_STRING);
9657 break;
9658 default:
9659 strncpy(extra, "unknown", MAX_WX_STRING);
43f66a6c 9660 break;
bf79451e
JG
9661 }
9662
43f66a6c
JK
9663 IPW_DEBUG_WX("PRIV GET MODE: %s\n", extra);
9664
0edd5b44 9665 wrqu->data.length = strlen(extra) + 1;
4644151b 9666 mutex_unlock(&priv->mutex);
b095c381
JK
9667
9668 return 0;
9669}
9670
9671static int ipw_wx_set_preamble(struct net_device *dev,
9672 struct iw_request_info *info,
9673 union iwreq_data *wrqu, char *extra)
9674{
9675 struct ipw_priv *priv = ieee80211_priv(dev);
9676 int mode = *(int *)extra;
4644151b 9677 mutex_lock(&priv->mutex);
b095c381
JK
9678 /* Switching from SHORT -> LONG requires a disassociation */
9679 if (mode == 1) {
9680 if (!(priv->config & CFG_PREAMBLE_LONG)) {
9681 priv->config |= CFG_PREAMBLE_LONG;
9682
9683 /* Network configuration changed -- force [re]association */
9684 IPW_DEBUG_ASSOC
9685 ("[re]association triggered due to preamble change.\n");
9686 if (!ipw_disassociate(priv))
9687 ipw_associate(priv);
9688 }
9689 goto done;
9690 }
43f66a6c 9691
b095c381
JK
9692 if (mode == 0) {
9693 priv->config &= ~CFG_PREAMBLE_LONG;
9694 goto done;
9695 }
4644151b 9696 mutex_unlock(&priv->mutex);
b095c381
JK
9697 return -EINVAL;
9698
9699 done:
4644151b 9700 mutex_unlock(&priv->mutex);
b095c381
JK
9701 return 0;
9702}
9703
9704static int ipw_wx_get_preamble(struct net_device *dev,
9705 struct iw_request_info *info,
9706 union iwreq_data *wrqu, char *extra)
9707{
9708 struct ipw_priv *priv = ieee80211_priv(dev);
4644151b 9709 mutex_lock(&priv->mutex);
b095c381
JK
9710 if (priv->config & CFG_PREAMBLE_LONG)
9711 snprintf(wrqu->name, IFNAMSIZ, "long (1)");
9712 else
9713 snprintf(wrqu->name, IFNAMSIZ, "auto (0)");
4644151b 9714 mutex_unlock(&priv->mutex);
0edd5b44 9715 return 0;
43f66a6c
JK
9716}
9717
b095c381
JK
9718#ifdef CONFIG_IPW2200_MONITOR
9719static int ipw_wx_set_monitor(struct net_device *dev,
bf79451e 9720 struct iw_request_info *info,
43f66a6c 9721 union iwreq_data *wrqu, char *extra)
bf79451e 9722{
43f66a6c
JK
9723 struct ipw_priv *priv = ieee80211_priv(dev);
9724 int *parms = (int *)extra;
9725 int enable = (parms[0] > 0);
4644151b 9726 mutex_lock(&priv->mutex);
b095c381 9727 IPW_DEBUG_WX("SET MONITOR: %d %d\n", enable, parms[1]);
43f66a6c
JK
9728 if (enable) {
9729 if (priv->ieee->iw_mode != IW_MODE_MONITOR) {
459d4087 9730#ifdef CONFIG_IPW2200_RADIOTAP
24a47dbd
MK
9731 priv->net_dev->type = ARPHRD_IEEE80211_RADIOTAP;
9732#else
43f66a6c 9733 priv->net_dev->type = ARPHRD_IEEE80211;
24a47dbd 9734#endif
b095c381 9735 queue_work(priv->workqueue, &priv->adapter_restart);
43f66a6c 9736 }
bf79451e 9737
43f66a6c
JK
9738 ipw_set_channel(priv, parms[1]);
9739 } else {
b095c381 9740 if (priv->ieee->iw_mode != IW_MODE_MONITOR) {
4644151b 9741 mutex_unlock(&priv->mutex);
43f66a6c 9742 return 0;
b095c381 9743 }
43f66a6c 9744 priv->net_dev->type = ARPHRD_ETHER;
b095c381 9745 queue_work(priv->workqueue, &priv->adapter_restart);
43f66a6c 9746 }
4644151b 9747 mutex_unlock(&priv->mutex);
43f66a6c
JK
9748 return 0;
9749}
9750
67fd6b45 9751#endif /* CONFIG_IPW2200_MONITOR */
b095c381 9752
bf79451e
JG
9753static int ipw_wx_reset(struct net_device *dev,
9754 struct iw_request_info *info,
43f66a6c 9755 union iwreq_data *wrqu, char *extra)
bf79451e 9756{
43f66a6c
JK
9757 struct ipw_priv *priv = ieee80211_priv(dev);
9758 IPW_DEBUG_WX("RESET\n");
b095c381
JK
9759 queue_work(priv->workqueue, &priv->adapter_restart);
9760 return 0;
9761}
9762
b095c381
JK
9763static int ipw_wx_sw_reset(struct net_device *dev,
9764 struct iw_request_info *info,
9765 union iwreq_data *wrqu, char *extra)
ea2b26e0
JK
9766{
9767 struct ipw_priv *priv = ieee80211_priv(dev);
b095c381
JK
9768 union iwreq_data wrqu_sec = {
9769 .encoding = {
9770 .flags = IW_ENCODE_DISABLED,
9771 },
9772 };
afbf30a2 9773 int ret;
c848d0af 9774
b095c381 9775 IPW_DEBUG_WX("SW_RESET\n");
ea2b26e0 9776
4644151b 9777 mutex_lock(&priv->mutex);
ea2b26e0 9778
d6d5b5c1 9779 ret = ipw_sw_reset(priv, 2);
afbf30a2
JK
9780 if (!ret) {
9781 free_firmware();
9782 ipw_adapter_restart(priv);
9783 }
ea2b26e0 9784
b095c381
JK
9785 /* The SW reset bit might have been toggled on by the 'disable'
9786 * module parameter, so take appropriate action */
9787 ipw_radio_kill_sw(priv, priv->status & STATUS_RF_KILL_SW);
ea2b26e0 9788
4644151b 9789 mutex_unlock(&priv->mutex);
b095c381 9790 ieee80211_wx_set_encode(priv->ieee, info, &wrqu_sec, NULL);
4644151b 9791 mutex_lock(&priv->mutex);
bf79451e 9792
b095c381
JK
9793 if (!(priv->status & STATUS_RF_KILL_MASK)) {
9794 /* Configuration likely changed -- force [re]association */
9795 IPW_DEBUG_ASSOC("[re]association triggered due to sw "
9796 "reset.\n");
9797 if (!ipw_disassociate(priv))
9798 ipw_associate(priv);
43f66a6c 9799 }
b095c381 9800
4644151b 9801 mutex_unlock(&priv->mutex);
43f66a6c 9802
43f66a6c
JK
9803 return 0;
9804}
43f66a6c
JK
9805
9806/* Rebase the WE IOCTLs to zero for the handler array */
9807#define IW_IOCTL(x) [(x)-SIOCSIWCOMMIT]
0edd5b44 9808static iw_handler ipw_wx_handlers[] = {
ea2b26e0
JK
9809 IW_IOCTL(SIOCGIWNAME) = ipw_wx_get_name,
9810 IW_IOCTL(SIOCSIWFREQ) = ipw_wx_set_freq,
9811 IW_IOCTL(SIOCGIWFREQ) = ipw_wx_get_freq,
9812 IW_IOCTL(SIOCSIWMODE) = ipw_wx_set_mode,
9813 IW_IOCTL(SIOCGIWMODE) = ipw_wx_get_mode,
651be26f
OH
9814 IW_IOCTL(SIOCSIWSENS) = ipw_wx_set_sens,
9815 IW_IOCTL(SIOCGIWSENS) = ipw_wx_get_sens,
ea2b26e0
JK
9816 IW_IOCTL(SIOCGIWRANGE) = ipw_wx_get_range,
9817 IW_IOCTL(SIOCSIWAP) = ipw_wx_set_wap,
9818 IW_IOCTL(SIOCGIWAP) = ipw_wx_get_wap,
9819 IW_IOCTL(SIOCSIWSCAN) = ipw_wx_set_scan,
9820 IW_IOCTL(SIOCGIWSCAN) = ipw_wx_get_scan,
9821 IW_IOCTL(SIOCSIWESSID) = ipw_wx_set_essid,
9822 IW_IOCTL(SIOCGIWESSID) = ipw_wx_get_essid,
9823 IW_IOCTL(SIOCSIWNICKN) = ipw_wx_set_nick,
9824 IW_IOCTL(SIOCGIWNICKN) = ipw_wx_get_nick,
9825 IW_IOCTL(SIOCSIWRATE) = ipw_wx_set_rate,
9826 IW_IOCTL(SIOCGIWRATE) = ipw_wx_get_rate,
9827 IW_IOCTL(SIOCSIWRTS) = ipw_wx_set_rts,
9828 IW_IOCTL(SIOCGIWRTS) = ipw_wx_get_rts,
9829 IW_IOCTL(SIOCSIWFRAG) = ipw_wx_set_frag,
9830 IW_IOCTL(SIOCGIWFRAG) = ipw_wx_get_frag,
9831 IW_IOCTL(SIOCSIWTXPOW) = ipw_wx_set_txpow,
9832 IW_IOCTL(SIOCGIWTXPOW) = ipw_wx_get_txpow,
9833 IW_IOCTL(SIOCSIWRETRY) = ipw_wx_set_retry,
9834 IW_IOCTL(SIOCGIWRETRY) = ipw_wx_get_retry,
9835 IW_IOCTL(SIOCSIWENCODE) = ipw_wx_set_encode,
9836 IW_IOCTL(SIOCGIWENCODE) = ipw_wx_get_encode,
9837 IW_IOCTL(SIOCSIWPOWER) = ipw_wx_set_power,
9838 IW_IOCTL(SIOCGIWPOWER) = ipw_wx_get_power,
a613bffd
JK
9839 IW_IOCTL(SIOCSIWSPY) = iw_handler_set_spy,
9840 IW_IOCTL(SIOCGIWSPY) = iw_handler_get_spy,
9841 IW_IOCTL(SIOCSIWTHRSPY) = iw_handler_set_thrspy,
9842 IW_IOCTL(SIOCGIWTHRSPY) = iw_handler_get_thrspy,
afbf30a2
JK
9843 IW_IOCTL(SIOCSIWGENIE) = ipw_wx_set_genie,
9844 IW_IOCTL(SIOCGIWGENIE) = ipw_wx_get_genie,
9845 IW_IOCTL(SIOCSIWMLME) = ipw_wx_set_mlme,
9846 IW_IOCTL(SIOCSIWAUTH) = ipw_wx_set_auth,
9847 IW_IOCTL(SIOCGIWAUTH) = ipw_wx_get_auth,
9848 IW_IOCTL(SIOCSIWENCODEEXT) = ipw_wx_set_encodeext,
9849 IW_IOCTL(SIOCGIWENCODEEXT) = ipw_wx_get_encodeext,
43f66a6c
JK
9850};
9851
b095c381
JK
9852enum {
9853 IPW_PRIV_SET_POWER = SIOCIWFIRSTPRIV,
9854 IPW_PRIV_GET_POWER,
9855 IPW_PRIV_SET_MODE,
9856 IPW_PRIV_GET_MODE,
9857 IPW_PRIV_SET_PREAMBLE,
9858 IPW_PRIV_GET_PREAMBLE,
9859 IPW_PRIV_RESET,
9860 IPW_PRIV_SW_RESET,
9861#ifdef CONFIG_IPW2200_MONITOR
9862 IPW_PRIV_SET_MONITOR,
9863#endif
9864};
43f66a6c 9865
bf79451e 9866static struct iw_priv_args ipw_priv_args[] = {
43f66a6c 9867 {
0edd5b44
JG
9868 .cmd = IPW_PRIV_SET_POWER,
9869 .set_args = IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
9870 .name = "set_power"},
43f66a6c 9871 {
0edd5b44
JG
9872 .cmd = IPW_PRIV_GET_POWER,
9873 .get_args = IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | MAX_WX_STRING,
9874 .name = "get_power"},
43f66a6c 9875 {
0edd5b44
JG
9876 .cmd = IPW_PRIV_SET_MODE,
9877 .set_args = IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
9878 .name = "set_mode"},
43f66a6c 9879 {
0edd5b44
JG
9880 .cmd = IPW_PRIV_GET_MODE,
9881 .get_args = IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | MAX_WX_STRING,
9882 .name = "get_mode"},
43f66a6c 9883 {
ea2b26e0
JK
9884 .cmd = IPW_PRIV_SET_PREAMBLE,
9885 .set_args = IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
9886 .name = "set_preamble"},
9887 {
9888 .cmd = IPW_PRIV_GET_PREAMBLE,
9889 .get_args = IW_PRIV_TYPE_CHAR | IW_PRIV_SIZE_FIXED | IFNAMSIZ,
9890 .name = "get_preamble"},
43f66a6c 9891 {
0edd5b44
JG
9892 IPW_PRIV_RESET,
9893 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 0, 0, "reset"},
b095c381
JK
9894 {
9895 IPW_PRIV_SW_RESET,
9896 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 0, 0, "sw_reset"},
9897#ifdef CONFIG_IPW2200_MONITOR
9898 {
9899 IPW_PRIV_SET_MONITOR,
9900 IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 2, 0, "monitor"},
9901#endif /* CONFIG_IPW2200_MONITOR */
43f66a6c
JK
9902};
9903
9904static iw_handler ipw_priv_handler[] = {
9905 ipw_wx_set_powermode,
9906 ipw_wx_get_powermode,
9907 ipw_wx_set_wireless_mode,
9908 ipw_wx_get_wireless_mode,
ea2b26e0
JK
9909 ipw_wx_set_preamble,
9910 ipw_wx_get_preamble,
bf79451e 9911 ipw_wx_reset,
b095c381
JK
9912 ipw_wx_sw_reset,
9913#ifdef CONFIG_IPW2200_MONITOR
9914 ipw_wx_set_monitor,
43f66a6c
JK
9915#endif
9916};
9917
0edd5b44 9918static struct iw_handler_def ipw_wx_handler_def = {
ea2b26e0
JK
9919 .standard = ipw_wx_handlers,
9920 .num_standard = ARRAY_SIZE(ipw_wx_handlers),
9921 .num_private = ARRAY_SIZE(ipw_priv_handler),
9922 .num_private_args = ARRAY_SIZE(ipw_priv_args),
9923 .private = ipw_priv_handler,
9924 .private_args = ipw_priv_args,
97a78ca9 9925 .get_wireless_stats = ipw_get_wireless_stats,
43f66a6c
JK
9926};
9927
43f66a6c
JK
9928/*
9929 * Get wireless statistics.
9930 * Called by /proc/net/wireless
9931 * Also called by SIOCGIWSTATS
9932 */
0edd5b44 9933static struct iw_statistics *ipw_get_wireless_stats(struct net_device *dev)
43f66a6c
JK
9934{
9935 struct ipw_priv *priv = ieee80211_priv(dev);
9936 struct iw_statistics *wstats;
bf79451e 9937
43f66a6c
JK
9938 wstats = &priv->wstats;
9939
ea2b26e0 9940 /* if hw is disabled, then ipw_get_ordinal() can't be called.
afbf30a2 9941 * netdev->get_wireless_stats seems to be called before fw is
43f66a6c
JK
9942 * initialized. STATUS_ASSOCIATED will only be set if the hw is up
9943 * and associated; if not associcated, the values are all meaningless
9944 * anyway, so set them all to NULL and INVALID */
9945 if (!(priv->status & STATUS_ASSOCIATED)) {
9946 wstats->miss.beacon = 0;
9947 wstats->discard.retries = 0;
9948 wstats->qual.qual = 0;
9949 wstats->qual.level = 0;
9950 wstats->qual.noise = 0;
9951 wstats->qual.updated = 7;
9952 wstats->qual.updated |= IW_QUAL_NOISE_INVALID |
0edd5b44 9953 IW_QUAL_QUAL_INVALID | IW_QUAL_LEVEL_INVALID;
43f66a6c 9954 return wstats;
bf79451e 9955 }
43f66a6c
JK
9956
9957 wstats->qual.qual = priv->quality;
00d21de5
ZY
9958 wstats->qual.level = priv->exp_avg_rssi;
9959 wstats->qual.noise = priv->exp_avg_noise;
43f66a6c 9960 wstats->qual.updated = IW_QUAL_QUAL_UPDATED | IW_QUAL_LEVEL_UPDATED |
b191608a 9961 IW_QUAL_NOISE_UPDATED | IW_QUAL_DBM;
43f66a6c
JK
9962
9963 wstats->miss.beacon = average_value(&priv->average_missed_beacons);
9964 wstats->discard.retries = priv->last_tx_failures;
9965 wstats->discard.code = priv->ieee->ieee_stats.rx_discards_undecryptable;
bf79451e 9966
43f66a6c
JK
9967/* if (ipw_get_ordinal(priv, IPW_ORD_STAT_TX_RETRY, &tx_retry, &len))
9968 goto fail_get_ordinal;
9969 wstats->discard.retries += tx_retry; */
bf79451e 9970
43f66a6c
JK
9971 return wstats;
9972}
9973
43f66a6c
JK
9974/* net device stuff */
9975
858119e1 9976static void init_sys_config(struct ipw_sys_config *sys_config)
43f66a6c 9977{
0edd5b44 9978 memset(sys_config, 0, sizeof(struct ipw_sys_config));
810dabd4 9979 sys_config->bt_coexistence = 0;
43f66a6c
JK
9980 sys_config->answer_broadcast_ssid_probe = 0;
9981 sys_config->accept_all_data_frames = 0;
9982 sys_config->accept_non_directed_frames = 1;
9983 sys_config->exclude_unicast_unencrypted = 0;
9984 sys_config->disable_unicast_decryption = 1;
9985 sys_config->exclude_multicast_unencrypted = 0;
9986 sys_config->disable_multicast_decryption = 1;
d2b83e12
ZY
9987 if (antenna < CFG_SYS_ANTENNA_BOTH || antenna > CFG_SYS_ANTENNA_B)
9988 antenna = CFG_SYS_ANTENNA_BOTH;
9989 sys_config->antenna_diversity = antenna;
0edd5b44 9990 sys_config->pass_crc_to_host = 0; /* TODO: See if 1 gives us FCS */
43f66a6c 9991 sys_config->dot11g_auto_detection = 0;
bf79451e 9992 sys_config->enable_cts_to_self = 0;
43f66a6c 9993 sys_config->bt_coexist_collision_thr = 0;
67fd6b45 9994 sys_config->pass_noise_stats_to_host = 1; /* 1 -- fix for 256 */
12977154 9995 sys_config->silence_threshold = 0x1e;
43f66a6c
JK
9996}
9997
9998static int ipw_net_open(struct net_device *dev)
9999{
10000 struct ipw_priv *priv = ieee80211_priv(dev);
10001 IPW_DEBUG_INFO("dev->open\n");
10002 /* we should be verifying the device is ready to be opened */
4644151b 10003 mutex_lock(&priv->mutex);
bf79451e
JG
10004 if (!(priv->status & STATUS_RF_KILL_MASK) &&
10005 (priv->status & STATUS_ASSOCIATED))
43f66a6c 10006 netif_start_queue(dev);
4644151b 10007 mutex_unlock(&priv->mutex);
43f66a6c
JK
10008 return 0;
10009}
10010
10011static int ipw_net_stop(struct net_device *dev)
10012{
10013 IPW_DEBUG_INFO("dev->close\n");
10014 netif_stop_queue(dev);
10015 return 0;
10016}
10017
10018/*
10019todo:
10020
10021modify to send one tfd per fragment instead of using chunking. otherwise
10022we need to heavily modify the ieee80211_skb_to_txb.
10023*/
10024
858119e1 10025static int ipw_tx_skb(struct ipw_priv *priv, struct ieee80211_txb *txb,
227d2dc1 10026 int pri)
43f66a6c 10027{
a5cf4fe6 10028 struct ieee80211_hdr_3addrqos *hdr = (struct ieee80211_hdr_3addrqos *)
0edd5b44 10029 txb->fragments[0]->data;
43f66a6c
JK
10030 int i = 0;
10031 struct tfd_frame *tfd;
e43e3c1e 10032#ifdef CONFIG_IPW2200_QOS
b095c381
JK
10033 int tx_id = ipw_get_tx_queue_number(priv, pri);
10034 struct clx2_tx_queue *txq = &priv->txq[tx_id];
10035#else
43f66a6c 10036 struct clx2_tx_queue *txq = &priv->txq[0];
b095c381 10037#endif
43f66a6c
JK
10038 struct clx2_queue *q = &txq->q;
10039 u8 id, hdr_len, unicast;
10040 u16 remaining_bytes;
c848d0af 10041 int fc;
43f66a6c 10042
a5cf4fe6 10043 hdr_len = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
43f66a6c
JK
10044 switch (priv->ieee->iw_mode) {
10045 case IW_MODE_ADHOC:
3c19065a 10046 unicast = !is_multicast_ether_addr(hdr->addr1);
43f66a6c
JK
10047 id = ipw_find_station(priv, hdr->addr1);
10048 if (id == IPW_INVALID_STATION) {
10049 id = ipw_add_station(priv, hdr->addr1);
10050 if (id == IPW_INVALID_STATION) {
10051 IPW_WARNING("Attempt to send data to "
bf79451e 10052 "invalid cell: " MAC_FMT "\n",
43f66a6c
JK
10053 MAC_ARG(hdr->addr1));
10054 goto drop;
10055 }
10056 }
10057 break;
10058
10059 case IW_MODE_INFRA:
10060 default:
3c19065a 10061 unicast = !is_multicast_ether_addr(hdr->addr3);
43f66a6c
JK
10062 id = 0;
10063 break;
10064 }
10065
10066 tfd = &txq->bd[q->first_empty];
10067 txq->txb[q->first_empty] = txb;
10068 memset(tfd, 0, sizeof(*tfd));
10069 tfd->u.data.station_number = id;
10070
10071 tfd->control_flags.message_type = TX_FRAME_TYPE;
10072 tfd->control_flags.control_bits = TFD_NEED_IRQ_MASK;
10073
10074 tfd->u.data.cmd_id = DINO_CMD_TX;
a613bffd 10075 tfd->u.data.len = cpu_to_le16(txb->payload_size);
43f66a6c 10076 remaining_bytes = txb->payload_size;
bf79451e 10077
43f66a6c 10078 if (priv->assoc_request.ieee_mode == IPW_B_MODE)
b095c381 10079 tfd->u.data.tx_flags_ext |= DCT_FLAG_EXT_MODE_CCK;
43f66a6c 10080 else
b095c381 10081 tfd->u.data.tx_flags_ext |= DCT_FLAG_EXT_MODE_OFDM;
43f66a6c 10082
ea2b26e0
JK
10083 if (priv->assoc_request.preamble_length == DCT_FLAG_SHORT_PREAMBLE)
10084 tfd->u.data.tx_flags |= DCT_FLAG_SHORT_PREAMBLE;
43f66a6c 10085
c848d0af
JK
10086 fc = le16_to_cpu(hdr->frame_ctl);
10087 hdr->frame_ctl = cpu_to_le16(fc & ~IEEE80211_FCTL_MOREFRAGS);
43f66a6c
JK
10088
10089 memcpy(&tfd->u.data.tfd.tfd_24.mchdr, hdr, hdr_len);
10090
b095c381
JK
10091 if (likely(unicast))
10092 tfd->u.data.tx_flags |= DCT_FLAG_ACK_REQD;
10093
10094 if (txb->encrypted && !priv->ieee->host_encrypt) {
10095 switch (priv->ieee->sec.level) {
10096 case SEC_LEVEL_3:
10097 tfd->u.data.tfd.tfd_24.mchdr.frame_ctl |=
851ca268 10098 cpu_to_le16(IEEE80211_FCTL_PROTECTED);
b095c381
JK
10099 /* XXX: ACK flag must be set for CCMP even if it
10100 * is a multicast/broadcast packet, because CCMP
10101 * group communication encrypted by GTK is
10102 * actually done by the AP. */
10103 if (!unicast)
10104 tfd->u.data.tx_flags |= DCT_FLAG_ACK_REQD;
10105
10106 tfd->u.data.tx_flags &= ~DCT_FLAG_NO_WEP;
10107 tfd->u.data.tx_flags_ext |= DCT_FLAG_EXT_SECURITY_CCM;
10108 tfd->u.data.key_index = 0;
10109 tfd->u.data.key_index |= DCT_WEP_INDEX_USE_IMMEDIATE;
10110 break;
10111 case SEC_LEVEL_2:
10112 tfd->u.data.tfd.tfd_24.mchdr.frame_ctl |=
851ca268 10113 cpu_to_le16(IEEE80211_FCTL_PROTECTED);
b095c381
JK
10114 tfd->u.data.tx_flags &= ~DCT_FLAG_NO_WEP;
10115 tfd->u.data.tx_flags_ext |= DCT_FLAG_EXT_SECURITY_TKIP;
10116 tfd->u.data.key_index = DCT_WEP_INDEX_USE_IMMEDIATE;
10117 break;
10118 case SEC_LEVEL_1:
10119 tfd->u.data.tfd.tfd_24.mchdr.frame_ctl |=
851ca268 10120 cpu_to_le16(IEEE80211_FCTL_PROTECTED);
b095c381
JK
10121 tfd->u.data.key_index = priv->ieee->tx_keyidx;
10122 if (priv->ieee->sec.key_sizes[priv->ieee->tx_keyidx] <=
10123 40)
10124 tfd->u.data.key_index |= DCT_WEP_KEY_64Bit;
10125 else
10126 tfd->u.data.key_index |= DCT_WEP_KEY_128Bit;
10127 break;
10128 case SEC_LEVEL_0:
10129 break;
10130 default:
10131 printk(KERN_ERR "Unknow security level %d\n",
10132 priv->ieee->sec.level);
10133 break;
10134 }
10135 } else
10136 /* No hardware encryption */
10137 tfd->u.data.tx_flags |= DCT_FLAG_NO_WEP;
10138
e43e3c1e 10139#ifdef CONFIG_IPW2200_QOS
a5cf4fe6
ZY
10140 if (fc & IEEE80211_STYPE_QOS_DATA)
10141 ipw_qos_set_tx_queue_command(priv, pri, &(tfd->u.data));
e43e3c1e 10142#endif /* CONFIG_IPW2200_QOS */
b095c381 10143
43f66a6c 10144 /* payload */
a613bffd
JK
10145 tfd->u.data.num_chunks = cpu_to_le32(min((u8) (NUM_TFD_CHUNKS - 2),
10146 txb->nr_frags));
10147 IPW_DEBUG_FRAG("%i fragments being sent as %i chunks.\n",
10148 txb->nr_frags, le32_to_cpu(tfd->u.data.num_chunks));
10149 for (i = 0; i < le32_to_cpu(tfd->u.data.num_chunks); i++) {
10150 IPW_DEBUG_FRAG("Adding fragment %i of %i (%d bytes).\n",
10151 i, le32_to_cpu(tfd->u.data.num_chunks),
10152 txb->fragments[i]->len - hdr_len);
bf79451e 10153 IPW_DEBUG_TX("Dumping TX packet frag %i of %i (%d bytes):\n",
43f66a6c
JK
10154 i, tfd->u.data.num_chunks,
10155 txb->fragments[i]->len - hdr_len);
bf79451e 10156 printk_buf(IPW_DL_TX, txb->fragments[i]->data + hdr_len,
43f66a6c
JK
10157 txb->fragments[i]->len - hdr_len);
10158
0edd5b44 10159 tfd->u.data.chunk_ptr[i] =
a613bffd
JK
10160 cpu_to_le32(pci_map_single
10161 (priv->pci_dev,
10162 txb->fragments[i]->data + hdr_len,
10163 txb->fragments[i]->len - hdr_len,
10164 PCI_DMA_TODEVICE));
10165 tfd->u.data.chunk_len[i] =
10166 cpu_to_le16(txb->fragments[i]->len - hdr_len);
43f66a6c
JK
10167 }
10168
10169 if (i != txb->nr_frags) {
10170 struct sk_buff *skb;
10171 u16 remaining_bytes = 0;
10172 int j;
10173
10174 for (j = i; j < txb->nr_frags; j++)
10175 remaining_bytes += txb->fragments[j]->len - hdr_len;
10176
10177 printk(KERN_INFO "Trying to reallocate for %d bytes\n",
10178 remaining_bytes);
10179 skb = alloc_skb(remaining_bytes, GFP_ATOMIC);
10180 if (skb != NULL) {
a613bffd 10181 tfd->u.data.chunk_len[i] = cpu_to_le16(remaining_bytes);
43f66a6c
JK
10182 for (j = i; j < txb->nr_frags; j++) {
10183 int size = txb->fragments[j]->len - hdr_len;
afbf30a2 10184
43f66a6c 10185 printk(KERN_INFO "Adding frag %d %d...\n",
0edd5b44 10186 j, size);
43f66a6c 10187 memcpy(skb_put(skb, size),
0edd5b44 10188 txb->fragments[j]->data + hdr_len, size);
43f66a6c
JK
10189 }
10190 dev_kfree_skb_any(txb->fragments[i]);
10191 txb->fragments[i] = skb;
0edd5b44 10192 tfd->u.data.chunk_ptr[i] =
a613bffd
JK
10193 cpu_to_le32(pci_map_single
10194 (priv->pci_dev, skb->data,
10195 tfd->u.data.chunk_len[i],
10196 PCI_DMA_TODEVICE));
10197
10198 tfd->u.data.num_chunks =
10199 cpu_to_le32(le32_to_cpu(tfd->u.data.num_chunks) +
10200 1);
bf79451e 10201 }
43f66a6c
JK
10202 }
10203
10204 /* kick DMA */
10205 q->first_empty = ipw_queue_inc_wrap(q->first_empty, q->n_bd);
10206 ipw_write32(priv, q->reg_w, q->first_empty);
10207
f697014a
JK
10208 if (ipw_queue_space(q) < q->high_mark)
10209 netif_stop_queue(priv->net_dev);
10210
227d2dc1 10211 return NETDEV_TX_OK;
43f66a6c 10212
0edd5b44 10213 drop:
43f66a6c
JK
10214 IPW_DEBUG_DROP("Silently dropping Tx packet.\n");
10215 ieee80211_txb_free(txb);
227d2dc1
JK
10216 return NETDEV_TX_OK;
10217}
10218
10219static int ipw_net_is_queue_full(struct net_device *dev, int pri)
10220{
10221 struct ipw_priv *priv = ieee80211_priv(dev);
e43e3c1e 10222#ifdef CONFIG_IPW2200_QOS
227d2dc1
JK
10223 int tx_id = ipw_get_tx_queue_number(priv, pri);
10224 struct clx2_tx_queue *txq = &priv->txq[tx_id];
10225#else
10226 struct clx2_tx_queue *txq = &priv->txq[0];
e43e3c1e 10227#endif /* CONFIG_IPW2200_QOS */
227d2dc1
JK
10228
10229 if (ipw_queue_space(&txq->q) < txq->q.high_mark)
10230 return 1;
10231
10232 return 0;
43f66a6c
JK
10233}
10234
d685b8c2
ZY
10235#ifdef CONFIG_IPW2200_PROMISCUOUS
10236static void ipw_handle_promiscuous_tx(struct ipw_priv *priv,
10237 struct ieee80211_txb *txb)
10238{
10239 struct ieee80211_rx_stats dummystats;
10240 struct ieee80211_hdr *hdr;
10241 u8 n;
10242 u16 filter = priv->prom_priv->filter;
10243 int hdr_only = 0;
10244
10245 if (filter & IPW_PROM_NO_TX)
10246 return;
10247
10248 memset(&dummystats, 0, sizeof(dummystats));
10249
10250 /* Filtering of fragment chains is done agains the first fragment */
10251 hdr = (void *)txb->fragments[0]->data;
851ca268 10252 if (ieee80211_is_management(le16_to_cpu(hdr->frame_ctl))) {
d685b8c2
ZY
10253 if (filter & IPW_PROM_NO_MGMT)
10254 return;
10255 if (filter & IPW_PROM_MGMT_HEADER_ONLY)
10256 hdr_only = 1;
851ca268 10257 } else if (ieee80211_is_control(le16_to_cpu(hdr->frame_ctl))) {
d685b8c2
ZY
10258 if (filter & IPW_PROM_NO_CTL)
10259 return;
10260 if (filter & IPW_PROM_CTL_HEADER_ONLY)
10261 hdr_only = 1;
851ca268 10262 } else if (ieee80211_is_data(le16_to_cpu(hdr->frame_ctl))) {
d685b8c2
ZY
10263 if (filter & IPW_PROM_NO_DATA)
10264 return;
10265 if (filter & IPW_PROM_DATA_HEADER_ONLY)
10266 hdr_only = 1;
10267 }
10268
10269 for(n=0; n<txb->nr_frags; ++n) {
10270 struct sk_buff *src = txb->fragments[n];
10271 struct sk_buff *dst;
10272 struct ieee80211_radiotap_header *rt_hdr;
10273 int len;
10274
10275 if (hdr_only) {
10276 hdr = (void *)src->data;
851ca268 10277 len = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
d685b8c2
ZY
10278 } else
10279 len = src->len;
10280
10281 dst = alloc_skb(
10282 len + IEEE80211_RADIOTAP_HDRLEN, GFP_ATOMIC);
10283 if (!dst) continue;
10284
10285 rt_hdr = (void *)skb_put(dst, sizeof(*rt_hdr));
10286
10287 rt_hdr->it_version = PKTHDR_RADIOTAP_VERSION;
10288 rt_hdr->it_pad = 0;
10289 rt_hdr->it_present = 0; /* after all, it's just an idea */
10290 rt_hdr->it_present |= (1 << IEEE80211_RADIOTAP_CHANNEL);
10291
10292 *(u16*)skb_put(dst, sizeof(u16)) = cpu_to_le16(
10293 ieee80211chan2mhz(priv->channel));
10294 if (priv->channel > 14) /* 802.11a */
10295 *(u16*)skb_put(dst, sizeof(u16)) =
10296 cpu_to_le16(IEEE80211_CHAN_OFDM |
10297 IEEE80211_CHAN_5GHZ);
10298 else if (priv->ieee->mode == IEEE_B) /* 802.11b */
10299 *(u16*)skb_put(dst, sizeof(u16)) =
10300 cpu_to_le16(IEEE80211_CHAN_CCK |
10301 IEEE80211_CHAN_2GHZ);
10302 else /* 802.11g */
10303 *(u16*)skb_put(dst, sizeof(u16)) =
10304 cpu_to_le16(IEEE80211_CHAN_OFDM |
10305 IEEE80211_CHAN_2GHZ);
10306
10307 rt_hdr->it_len = dst->len;
10308
10309 memcpy(skb_put(dst, len), src->data, len);
10310
10311 if (!ieee80211_rx(priv->prom_priv->ieee, dst, &dummystats))
10312 dev_kfree_skb_any(dst);
10313 }
10314}
10315#endif
10316
43f66a6c 10317static int ipw_net_hard_start_xmit(struct ieee80211_txb *txb,
c8d42d1a 10318 struct net_device *dev, int pri)
43f66a6c
JK
10319{
10320 struct ipw_priv *priv = ieee80211_priv(dev);
10321 unsigned long flags;
227d2dc1 10322 int ret;
43f66a6c
JK
10323
10324 IPW_DEBUG_TX("dev->xmit(%d bytes)\n", txb->payload_size);
43f66a6c
JK
10325 spin_lock_irqsave(&priv->lock, flags);
10326
10327 if (!(priv->status & STATUS_ASSOCIATED)) {
10328 IPW_DEBUG_INFO("Tx attempt while not associated.\n");
10329 priv->ieee->stats.tx_carrier_errors++;
10330 netif_stop_queue(dev);
10331 goto fail_unlock;
10332 }
10333
d685b8c2
ZY
10334#ifdef CONFIG_IPW2200_PROMISCUOUS
10335 if (rtap_iface && netif_running(priv->prom_net_dev))
10336 ipw_handle_promiscuous_tx(priv, txb);
10337#endif
10338
227d2dc1
JK
10339 ret = ipw_tx_skb(priv, txb, pri);
10340 if (ret == NETDEV_TX_OK)
10341 __ipw_led_activity_on(priv);
43f66a6c 10342 spin_unlock_irqrestore(&priv->lock, flags);
43f66a6c 10343
227d2dc1 10344 return ret;
43f66a6c 10345
0edd5b44 10346 fail_unlock:
43f66a6c
JK
10347 spin_unlock_irqrestore(&priv->lock, flags);
10348 return 1;
10349}
10350
10351static struct net_device_stats *ipw_net_get_stats(struct net_device *dev)
10352{
10353 struct ipw_priv *priv = ieee80211_priv(dev);
bf79451e 10354
43f66a6c
JK
10355 priv->ieee->stats.tx_packets = priv->tx_packets;
10356 priv->ieee->stats.rx_packets = priv->rx_packets;
10357 return &priv->ieee->stats;
10358}
10359
10360static void ipw_net_set_multicast_list(struct net_device *dev)
10361{
10362
10363}
10364
10365static int ipw_net_set_mac_address(struct net_device *dev, void *p)
10366{
10367 struct ipw_priv *priv = ieee80211_priv(dev);
10368 struct sockaddr *addr = p;
10369 if (!is_valid_ether_addr(addr->sa_data))
10370 return -EADDRNOTAVAIL;
4644151b 10371 mutex_lock(&priv->mutex);
43f66a6c
JK
10372 priv->config |= CFG_CUSTOM_MAC;
10373 memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
10374 printk(KERN_INFO "%s: Setting MAC to " MAC_FMT "\n",
10375 priv->net_dev->name, MAC_ARG(priv->mac_addr));
a613bffd 10376 queue_work(priv->workqueue, &priv->adapter_restart);
4644151b 10377 mutex_unlock(&priv->mutex);
43f66a6c
JK
10378 return 0;
10379}
10380
bf79451e 10381static void ipw_ethtool_get_drvinfo(struct net_device *dev,
43f66a6c
JK
10382 struct ethtool_drvinfo *info)
10383{
10384 struct ipw_priv *p = ieee80211_priv(dev);
10385 char vers[64];
10386 char date[32];
10387 u32 len;
10388
10389 strcpy(info->driver, DRV_NAME);
10390 strcpy(info->version, DRV_VERSION);
10391
10392 len = sizeof(vers);
10393 ipw_get_ordinal(p, IPW_ORD_STAT_FW_VERSION, vers, &len);
10394 len = sizeof(date);
10395 ipw_get_ordinal(p, IPW_ORD_STAT_FW_DATE, date, &len);
10396
0edd5b44 10397 snprintf(info->fw_version, sizeof(info->fw_version), "%s (%s)",
43f66a6c
JK
10398 vers, date);
10399 strcpy(info->bus_info, pci_name(p->pci_dev));
b095c381 10400 info->eedump_len = IPW_EEPROM_IMAGE_SIZE;
43f66a6c
JK
10401}
10402
10403static u32 ipw_ethtool_get_link(struct net_device *dev)
10404{
10405 struct ipw_priv *priv = ieee80211_priv(dev);
10406 return (priv->status & STATUS_ASSOCIATED) != 0;
10407}
10408
10409static int ipw_ethtool_get_eeprom_len(struct net_device *dev)
10410{
b095c381 10411 return IPW_EEPROM_IMAGE_SIZE;
43f66a6c
JK
10412}
10413
10414static int ipw_ethtool_get_eeprom(struct net_device *dev,
0edd5b44 10415 struct ethtool_eeprom *eeprom, u8 * bytes)
43f66a6c
JK
10416{
10417 struct ipw_priv *p = ieee80211_priv(dev);
10418
b095c381 10419 if (eeprom->offset + eeprom->len > IPW_EEPROM_IMAGE_SIZE)
43f66a6c 10420 return -EINVAL;
4644151b 10421 mutex_lock(&p->mutex);
afbf30a2 10422 memcpy(bytes, &p->eeprom[eeprom->offset], eeprom->len);
4644151b 10423 mutex_unlock(&p->mutex);
43f66a6c
JK
10424 return 0;
10425}
10426
10427static int ipw_ethtool_set_eeprom(struct net_device *dev,
0edd5b44 10428 struct ethtool_eeprom *eeprom, u8 * bytes)
43f66a6c
JK
10429{
10430 struct ipw_priv *p = ieee80211_priv(dev);
10431 int i;
10432
b095c381 10433 if (eeprom->offset + eeprom->len > IPW_EEPROM_IMAGE_SIZE)
43f66a6c 10434 return -EINVAL;
4644151b 10435 mutex_lock(&p->mutex);
afbf30a2 10436 memcpy(&p->eeprom[eeprom->offset], bytes, eeprom->len);
71e585fc
AB
10437 for (i = 0; i < IPW_EEPROM_IMAGE_SIZE; i++)
10438 ipw_write8(p, i + IPW_EEPROM_DATA, p->eeprom[i]);
4644151b 10439 mutex_unlock(&p->mutex);
43f66a6c
JK
10440 return 0;
10441}
10442
10443static struct ethtool_ops ipw_ethtool_ops = {
ea2b26e0
JK
10444 .get_link = ipw_ethtool_get_link,
10445 .get_drvinfo = ipw_ethtool_get_drvinfo,
10446 .get_eeprom_len = ipw_ethtool_get_eeprom_len,
10447 .get_eeprom = ipw_ethtool_get_eeprom,
10448 .set_eeprom = ipw_ethtool_set_eeprom,
43f66a6c
JK
10449};
10450
10451static irqreturn_t ipw_isr(int irq, void *data, struct pt_regs *regs)
10452{
10453 struct ipw_priv *priv = data;
10454 u32 inta, inta_mask;
bf79451e 10455
43f66a6c
JK
10456 if (!priv)
10457 return IRQ_NONE;
10458
89c318ed 10459 spin_lock(&priv->irq_lock);
43f66a6c
JK
10460
10461 if (!(priv->status & STATUS_INT_ENABLED)) {
10462 /* Shared IRQ */
10463 goto none;
10464 }
10465
b095c381
JK
10466 inta = ipw_read32(priv, IPW_INTA_RW);
10467 inta_mask = ipw_read32(priv, IPW_INTA_MASK_R);
bf79451e 10468
43f66a6c
JK
10469 if (inta == 0xFFFFFFFF) {
10470 /* Hardware disappeared */
10471 IPW_WARNING("IRQ INTA == 0xFFFFFFFF\n");
10472 goto none;
10473 }
10474
b095c381 10475 if (!(inta & (IPW_INTA_MASK_ALL & inta_mask))) {
43f66a6c
JK
10476 /* Shared interrupt */
10477 goto none;
10478 }
10479
10480 /* tell the device to stop sending interrupts */
89c318ed 10481 __ipw_disable_interrupts(priv);
bf79451e 10482
43f66a6c 10483 /* ack current interrupts */
b095c381
JK
10484 inta &= (IPW_INTA_MASK_ALL & inta_mask);
10485 ipw_write32(priv, IPW_INTA_RW, inta);
bf79451e 10486
43f66a6c
JK
10487 /* Cache INTA value for our tasklet */
10488 priv->isr_inta = inta;
10489
10490 tasklet_schedule(&priv->irq_tasklet);
10491
89c318ed 10492 spin_unlock(&priv->irq_lock);
43f66a6c
JK
10493
10494 return IRQ_HANDLED;
0edd5b44 10495 none:
89c318ed 10496 spin_unlock(&priv->irq_lock);
43f66a6c
JK
10497 return IRQ_NONE;
10498}
10499
10500static void ipw_rf_kill(void *adapter)
10501{
10502 struct ipw_priv *priv = adapter;
10503 unsigned long flags;
bf79451e 10504
43f66a6c
JK
10505 spin_lock_irqsave(&priv->lock, flags);
10506
10507 if (rf_kill_active(priv)) {
10508 IPW_DEBUG_RF_KILL("RF Kill active, rescheduling GPIO check\n");
10509 if (priv->workqueue)
10510 queue_delayed_work(priv->workqueue,
10511 &priv->rf_kill, 2 * HZ);
10512 goto exit_unlock;
10513 }
10514
10515 /* RF Kill is now disabled, so bring the device back up */
10516
10517 if (!(priv->status & STATUS_RF_KILL_MASK)) {
10518 IPW_DEBUG_RF_KILL("HW RF Kill no longer active, restarting "
10519 "device\n");
10520
10521 /* we can not do an adapter restart while inside an irq lock */
10522 queue_work(priv->workqueue, &priv->adapter_restart);
bf79451e 10523 } else
43f66a6c
JK
10524 IPW_DEBUG_RF_KILL("HW RF Kill deactivated. SW RF Kill still "
10525 "enabled\n");
10526
0edd5b44 10527 exit_unlock:
43f66a6c
JK
10528 spin_unlock_irqrestore(&priv->lock, flags);
10529}
10530
c848d0af
JK
10531static void ipw_bg_rf_kill(void *data)
10532{
10533 struct ipw_priv *priv = data;
4644151b 10534 mutex_lock(&priv->mutex);
c848d0af 10535 ipw_rf_kill(data);
4644151b 10536 mutex_unlock(&priv->mutex);
c848d0af
JK
10537}
10538
a73e22b2 10539static void ipw_link_up(struct ipw_priv *priv)
a613bffd 10540{
afbf30a2
JK
10541 priv->last_seq_num = -1;
10542 priv->last_frag_num = -1;
10543 priv->last_packet_time = 0;
10544
a613bffd
JK
10545 netif_carrier_on(priv->net_dev);
10546 if (netif_queue_stopped(priv->net_dev)) {
10547 IPW_DEBUG_NOTIF("waking queue\n");
10548 netif_wake_queue(priv->net_dev);
10549 } else {
10550 IPW_DEBUG_NOTIF("starting queue\n");
10551 netif_start_queue(priv->net_dev);
10552 }
10553
c848d0af 10554 cancel_delayed_work(&priv->request_scan);
a613bffd
JK
10555 ipw_reset_stats(priv);
10556 /* Ensure the rate is updated immediately */
10557 priv->last_rate = ipw_get_current_rate(priv);
10558 ipw_gather_stats(priv);
10559 ipw_led_link_up(priv);
10560 notify_wx_assoc_event(priv);
10561
10562 if (priv->config & CFG_BACKGROUND_SCAN)
10563 queue_delayed_work(priv->workqueue, &priv->request_scan, HZ);
10564}
10565
c848d0af
JK
10566static void ipw_bg_link_up(void *data)
10567{
10568 struct ipw_priv *priv = data;
4644151b 10569 mutex_lock(&priv->mutex);
c848d0af 10570 ipw_link_up(data);
4644151b 10571 mutex_unlock(&priv->mutex);
c848d0af
JK
10572}
10573
a73e22b2 10574static void ipw_link_down(struct ipw_priv *priv)
a613bffd
JK
10575{
10576 ipw_led_link_down(priv);
10577 netif_carrier_off(priv->net_dev);
10578 netif_stop_queue(priv->net_dev);
10579 notify_wx_assoc_event(priv);
10580
10581 /* Cancel any queued work ... */
10582 cancel_delayed_work(&priv->request_scan);
10583 cancel_delayed_work(&priv->adhoc_check);
10584 cancel_delayed_work(&priv->gather_stats);
10585
10586 ipw_reset_stats(priv);
10587
afbf30a2
JK
10588 if (!(priv->status & STATUS_EXIT_PENDING)) {
10589 /* Queue up another scan... */
10590 queue_work(priv->workqueue, &priv->request_scan);
10591 }
a613bffd
JK
10592}
10593
c848d0af
JK
10594static void ipw_bg_link_down(void *data)
10595{
10596 struct ipw_priv *priv = data;
4644151b 10597 mutex_lock(&priv->mutex);
c848d0af 10598 ipw_link_down(data);
4644151b 10599 mutex_unlock(&priv->mutex);
43f66a6c
JK
10600}
10601
10602static int ipw_setup_deferred_work(struct ipw_priv *priv)
10603{
10604 int ret = 0;
10605
43f66a6c 10606 priv->workqueue = create_workqueue(DRV_NAME);
43f66a6c 10607 init_waitqueue_head(&priv->wait_command_queue);
afbf30a2 10608 init_waitqueue_head(&priv->wait_state);
43f66a6c 10609
c848d0af
JK
10610 INIT_WORK(&priv->adhoc_check, ipw_bg_adhoc_check, priv);
10611 INIT_WORK(&priv->associate, ipw_bg_associate, priv);
10612 INIT_WORK(&priv->disassociate, ipw_bg_disassociate, priv);
d8bad6df 10613 INIT_WORK(&priv->system_config, ipw_system_config, priv);
c848d0af
JK
10614 INIT_WORK(&priv->rx_replenish, ipw_bg_rx_queue_replenish, priv);
10615 INIT_WORK(&priv->adapter_restart, ipw_bg_adapter_restart, priv);
10616 INIT_WORK(&priv->rf_kill, ipw_bg_rf_kill, priv);
10617 INIT_WORK(&priv->up, (void (*)(void *))ipw_bg_up, priv);
10618 INIT_WORK(&priv->down, (void (*)(void *))ipw_bg_down, priv);
bf79451e 10619 INIT_WORK(&priv->request_scan,
43f66a6c 10620 (void (*)(void *))ipw_request_scan, priv);
bf79451e 10621 INIT_WORK(&priv->gather_stats,
c848d0af
JK
10622 (void (*)(void *))ipw_bg_gather_stats, priv);
10623 INIT_WORK(&priv->abort_scan, (void (*)(void *))ipw_bg_abort_scan, priv);
10624 INIT_WORK(&priv->roam, ipw_bg_roam, priv);
10625 INIT_WORK(&priv->scan_check, ipw_bg_scan_check, priv);
10626 INIT_WORK(&priv->link_up, (void (*)(void *))ipw_bg_link_up, priv);
10627 INIT_WORK(&priv->link_down, (void (*)(void *))ipw_bg_link_down, priv);
10628 INIT_WORK(&priv->led_link_on, (void (*)(void *))ipw_bg_led_link_on,
10629 priv);
10630 INIT_WORK(&priv->led_link_off, (void (*)(void *))ipw_bg_led_link_off,
a613bffd 10631 priv);
c848d0af 10632 INIT_WORK(&priv->led_act_off, (void (*)(void *))ipw_bg_led_activity_off,
a613bffd 10633 priv);
c848d0af
JK
10634 INIT_WORK(&priv->merge_networks,
10635 (void (*)(void *))ipw_merge_adhoc_network, priv);
43f66a6c 10636
e43e3c1e 10637#ifdef CONFIG_IPW2200_QOS
b095c381
JK
10638 INIT_WORK(&priv->qos_activate, (void (*)(void *))ipw_bg_qos_activate,
10639 priv);
e43e3c1e 10640#endif /* CONFIG_IPW2200_QOS */
43f66a6c
JK
10641
10642 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
10643 ipw_irq_tasklet, (unsigned long)priv);
10644
10645 return ret;
10646}
10647
43f66a6c
JK
10648static void shim__set_security(struct net_device *dev,
10649 struct ieee80211_security *sec)
10650{
10651 struct ipw_priv *priv = ieee80211_priv(dev);
10652 int i;
bf79451e 10653 for (i = 0; i < 4; i++) {
43f66a6c 10654 if (sec->flags & (1 << i)) {
afbf30a2 10655 priv->ieee->sec.encode_alg[i] = sec->encode_alg[i];
b095c381 10656 priv->ieee->sec.key_sizes[i] = sec->key_sizes[i];
43f66a6c 10657 if (sec->key_sizes[i] == 0)
b095c381
JK
10658 priv->ieee->sec.flags &= ~(1 << i);
10659 else {
10660 memcpy(priv->ieee->sec.keys[i], sec->keys[i],
43f66a6c 10661 sec->key_sizes[i]);
b095c381
JK
10662 priv->ieee->sec.flags |= (1 << i);
10663 }
43f66a6c 10664 priv->status |= STATUS_SECURITY_UPDATED;
b095c381
JK
10665 } else if (sec->level != SEC_LEVEL_1)
10666 priv->ieee->sec.flags &= ~(1 << i);
43f66a6c
JK
10667 }
10668
b095c381 10669 if (sec->flags & SEC_ACTIVE_KEY) {
43f66a6c 10670 if (sec->active_key <= 3) {
b095c381
JK
10671 priv->ieee->sec.active_key = sec->active_key;
10672 priv->ieee->sec.flags |= SEC_ACTIVE_KEY;
bf79451e 10673 } else
b095c381 10674 priv->ieee->sec.flags &= ~SEC_ACTIVE_KEY;
43f66a6c 10675 priv->status |= STATUS_SECURITY_UPDATED;
b095c381
JK
10676 } else
10677 priv->ieee->sec.flags &= ~SEC_ACTIVE_KEY;
43f66a6c
JK
10678
10679 if ((sec->flags & SEC_AUTH_MODE) &&
b095c381
JK
10680 (priv->ieee->sec.auth_mode != sec->auth_mode)) {
10681 priv->ieee->sec.auth_mode = sec->auth_mode;
10682 priv->ieee->sec.flags |= SEC_AUTH_MODE;
43f66a6c
JK
10683 if (sec->auth_mode == WLAN_AUTH_SHARED_KEY)
10684 priv->capability |= CAP_SHARED_KEY;
10685 else
10686 priv->capability &= ~CAP_SHARED_KEY;
10687 priv->status |= STATUS_SECURITY_UPDATED;
10688 }
bf79451e 10689
b095c381
JK
10690 if (sec->flags & SEC_ENABLED && priv->ieee->sec.enabled != sec->enabled) {
10691 priv->ieee->sec.flags |= SEC_ENABLED;
10692 priv->ieee->sec.enabled = sec->enabled;
43f66a6c 10693 priv->status |= STATUS_SECURITY_UPDATED;
bf79451e 10694 if (sec->enabled)
43f66a6c
JK
10695 priv->capability |= CAP_PRIVACY_ON;
10696 else
10697 priv->capability &= ~CAP_PRIVACY_ON;
10698 }
bf79451e 10699
afbf30a2
JK
10700 if (sec->flags & SEC_ENCRYPT)
10701 priv->ieee->sec.encrypt = sec->encrypt;
bf79451e 10702
b095c381
JK
10703 if (sec->flags & SEC_LEVEL && priv->ieee->sec.level != sec->level) {
10704 priv->ieee->sec.level = sec->level;
10705 priv->ieee->sec.flags |= SEC_LEVEL;
43f66a6c
JK
10706 priv->status |= STATUS_SECURITY_UPDATED;
10707 }
10708
1fbfea54
ZY
10709 if (!priv->ieee->host_encrypt && (sec->flags & SEC_ENCRYPT))
10710 ipw_set_hwcrypto_keys(priv);
10711
bf79451e
JG
10712 /* To match current functionality of ipw2100 (which works well w/
10713 * various supplicants, we don't force a disassociate if the
43f66a6c
JK
10714 * privacy capability changes ... */
10715#if 0
10716 if ((priv->status & (STATUS_ASSOCIATED | STATUS_ASSOCIATING)) &&
bf79451e 10717 (((priv->assoc_request.capability &
43f66a6c 10718 WLAN_CAPABILITY_PRIVACY) && !sec->enabled) ||
bf79451e 10719 (!(priv->assoc_request.capability &
0edd5b44 10720 WLAN_CAPABILITY_PRIVACY) && sec->enabled))) {
43f66a6c
JK
10721 IPW_DEBUG_ASSOC("Disassociating due to capability "
10722 "change.\n");
10723 ipw_disassociate(priv);
10724 }
10725#endif
10726}
10727
bf79451e 10728static int init_supported_rates(struct ipw_priv *priv,
43f66a6c
JK
10729 struct ipw_supported_rates *rates)
10730{
10731 /* TODO: Mask out rates based on priv->rates_mask */
10732
10733 memset(rates, 0, sizeof(*rates));
0edd5b44 10734 /* configure supported rates */
43f66a6c
JK
10735 switch (priv->ieee->freq_band) {
10736 case IEEE80211_52GHZ_BAND:
10737 rates->ieee_mode = IPW_A_MODE;
10738 rates->purpose = IPW_RATE_CAPABILITIES;
10739 ipw_add_ofdm_scan_rates(rates, IEEE80211_CCK_MODULATION,
10740 IEEE80211_OFDM_DEFAULT_RATES_MASK);
10741 break;
10742
0edd5b44 10743 default: /* Mixed or 2.4Ghz */
43f66a6c
JK
10744 rates->ieee_mode = IPW_G_MODE;
10745 rates->purpose = IPW_RATE_CAPABILITIES;
10746 ipw_add_cck_scan_rates(rates, IEEE80211_CCK_MODULATION,
10747 IEEE80211_CCK_DEFAULT_RATES_MASK);
10748 if (priv->ieee->modulation & IEEE80211_OFDM_MODULATION) {
10749 ipw_add_ofdm_scan_rates(rates, IEEE80211_CCK_MODULATION,
10750 IEEE80211_OFDM_DEFAULT_RATES_MASK);
10751 }
10752 break;
10753 }
10754
10755 return 0;
10756}
10757
bf79451e 10758static int ipw_config(struct ipw_priv *priv)
43f66a6c 10759{
43f66a6c
JK
10760 /* This is only called from ipw_up, which resets/reloads the firmware
10761 so, we don't need to first disable the card before we configure
10762 it */
6de9f7f2 10763 if (ipw_set_tx_power(priv))
43f66a6c
JK
10764 goto error;
10765
10766 /* initialize adapter address */
10767 if (ipw_send_adapter_address(priv, priv->net_dev->dev_addr))
10768 goto error;
10769
10770 /* set basic system config settings */
10771 init_sys_config(&priv->sys_config);
810dabd4
ZY
10772
10773 /* Support Bluetooth if we have BT h/w on board, and user wants to.
10774 * Does not support BT priority yet (don't abort or defer our Tx) */
10775 if (bt_coexist) {
2638bc39 10776 unsigned char bt_caps = priv->eeprom[EEPROM_SKU_CAPABILITY];
810dabd4
ZY
10777
10778 if (bt_caps & EEPROM_SKU_CAP_BT_CHANNEL_SIG)
10779 priv->sys_config.bt_coexistence
2638bc39 10780 |= CFG_BT_COEXISTENCE_SIGNAL_CHNL;
810dabd4
ZY
10781 if (bt_caps & EEPROM_SKU_CAP_BT_OOB)
10782 priv->sys_config.bt_coexistence
2638bc39 10783 |= CFG_BT_COEXISTENCE_OOB;
810dabd4
ZY
10784 }
10785
d685b8c2
ZY
10786#ifdef CONFIG_IPW2200_PROMISCUOUS
10787 if (priv->prom_net_dev && netif_running(priv->prom_net_dev)) {
10788 priv->sys_config.accept_all_data_frames = 1;
10789 priv->sys_config.accept_non_directed_frames = 1;
10790 priv->sys_config.accept_all_mgmt_bcpr = 1;
10791 priv->sys_config.accept_all_mgmt_frames = 1;
10792 }
10793#endif
10794
c848d0af
JK
10795 if (priv->ieee->iw_mode == IW_MODE_ADHOC)
10796 priv->sys_config.answer_broadcast_ssid_probe = 1;
10797 else
10798 priv->sys_config.answer_broadcast_ssid_probe = 0;
10799
d685b8c2 10800 if (ipw_send_system_config(priv))
43f66a6c
JK
10801 goto error;
10802
0edd5b44
JG
10803 init_supported_rates(priv, &priv->rates);
10804 if (ipw_send_supported_rates(priv, &priv->rates))
43f66a6c
JK
10805 goto error;
10806
10807 /* Set request-to-send threshold */
10808 if (priv->rts_threshold) {
10809 if (ipw_send_rts_threshold(priv, priv->rts_threshold))
10810 goto error;
10811 }
e43e3c1e 10812#ifdef CONFIG_IPW2200_QOS
b095c381
JK
10813 IPW_DEBUG_QOS("QoS: call ipw_qos_activate\n");
10814 ipw_qos_activate(priv, NULL);
e43e3c1e 10815#endif /* CONFIG_IPW2200_QOS */
43f66a6c
JK
10816
10817 if (ipw_set_random_seed(priv))
10818 goto error;
bf79451e 10819
43f66a6c
JK
10820 /* final state transition to the RUN state */
10821 if (ipw_send_host_complete(priv))
10822 goto error;
10823
e666619e
JK
10824 priv->status |= STATUS_INIT;
10825
10826 ipw_led_init(priv);
10827 ipw_led_radio_on(priv);
10828 priv->notif_missed_beacons = 0;
10829
10830 /* Set hardware WEP key if it is configured. */
10831 if ((priv->capability & CAP_PRIVACY_ON) &&
10832 (priv->ieee->sec.level == SEC_LEVEL_1) &&
10833 !(priv->ieee->host_encrypt || priv->ieee->host_decrypt))
10834 ipw_set_hwcrypto_keys(priv);
43f66a6c
JK
10835
10836 return 0;
bf79451e 10837
0edd5b44 10838 error:
43f66a6c
JK
10839 return -EIO;
10840}
10841
4f36f808
JK
10842/*
10843 * NOTE:
10844 *
10845 * These tables have been tested in conjunction with the
10846 * Intel PRO/Wireless 2200BG and 2915ABG Network Connection Adapters.
10847 *
10848 * Altering this values, using it on other hardware, or in geographies
10849 * not intended for resale of the above mentioned Intel adapters has
10850 * not been tested.
10851 *
48a84770
HBA
10852 * Remember to update the table in README.ipw2200 when changing this
10853 * table.
10854 *
4f36f808
JK
10855 */
10856static const struct ieee80211_geo ipw_geos[] = {
10857 { /* Restricted */
10858 "---",
10859 .bg_channels = 11,
10860 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
10861 {2427, 4}, {2432, 5}, {2437, 6},
10862 {2442, 7}, {2447, 8}, {2452, 9},
10863 {2457, 10}, {2462, 11}},
10864 },
10865
10866 { /* Custom US/Canada */
10867 "ZZF",
10868 .bg_channels = 11,
10869 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
10870 {2427, 4}, {2432, 5}, {2437, 6},
10871 {2442, 7}, {2447, 8}, {2452, 9},
10872 {2457, 10}, {2462, 11}},
10873 .a_channels = 8,
10874 .a = {{5180, 36},
10875 {5200, 40},
10876 {5220, 44},
10877 {5240, 48},
10878 {5260, 52, IEEE80211_CH_PASSIVE_ONLY},
10879 {5280, 56, IEEE80211_CH_PASSIVE_ONLY},
10880 {5300, 60, IEEE80211_CH_PASSIVE_ONLY},
10881 {5320, 64, IEEE80211_CH_PASSIVE_ONLY}},
10882 },
10883
10884 { /* Rest of World */
10885 "ZZD",
10886 .bg_channels = 13,
10887 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
10888 {2427, 4}, {2432, 5}, {2437, 6},
10889 {2442, 7}, {2447, 8}, {2452, 9},
10890 {2457, 10}, {2462, 11}, {2467, 12},
10891 {2472, 13}},
10892 },
10893
10894 { /* Custom USA & Europe & High */
10895 "ZZA",
10896 .bg_channels = 11,
10897 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
10898 {2427, 4}, {2432, 5}, {2437, 6},
10899 {2442, 7}, {2447, 8}, {2452, 9},
10900 {2457, 10}, {2462, 11}},
10901 .a_channels = 13,
10902 .a = {{5180, 36},
10903 {5200, 40},
10904 {5220, 44},
10905 {5240, 48},
10906 {5260, 52, IEEE80211_CH_PASSIVE_ONLY},
10907 {5280, 56, IEEE80211_CH_PASSIVE_ONLY},
10908 {5300, 60, IEEE80211_CH_PASSIVE_ONLY},
10909 {5320, 64, IEEE80211_CH_PASSIVE_ONLY},
10910 {5745, 149},
10911 {5765, 153},
10912 {5785, 157},
10913 {5805, 161},
10914 {5825, 165}},
10915 },
10916
10917 { /* Custom NA & Europe */
10918 "ZZB",
10919 .bg_channels = 11,
10920 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
10921 {2427, 4}, {2432, 5}, {2437, 6},
10922 {2442, 7}, {2447, 8}, {2452, 9},
10923 {2457, 10}, {2462, 11}},
10924 .a_channels = 13,
10925 .a = {{5180, 36},
10926 {5200, 40},
10927 {5220, 44},
10928 {5240, 48},
10929 {5260, 52, IEEE80211_CH_PASSIVE_ONLY},
10930 {5280, 56, IEEE80211_CH_PASSIVE_ONLY},
10931 {5300, 60, IEEE80211_CH_PASSIVE_ONLY},
10932 {5320, 64, IEEE80211_CH_PASSIVE_ONLY},
10933 {5745, 149, IEEE80211_CH_PASSIVE_ONLY},
10934 {5765, 153, IEEE80211_CH_PASSIVE_ONLY},
10935 {5785, 157, IEEE80211_CH_PASSIVE_ONLY},
10936 {5805, 161, IEEE80211_CH_PASSIVE_ONLY},
10937 {5825, 165, IEEE80211_CH_PASSIVE_ONLY}},
10938 },
10939
10940 { /* Custom Japan */
10941 "ZZC",
10942 .bg_channels = 11,
10943 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
10944 {2427, 4}, {2432, 5}, {2437, 6},
10945 {2442, 7}, {2447, 8}, {2452, 9},
10946 {2457, 10}, {2462, 11}},
10947 .a_channels = 4,
10948 .a = {{5170, 34}, {5190, 38},
10949 {5210, 42}, {5230, 46}},
10950 },
10951
10952 { /* Custom */
10953 "ZZM",
10954 .bg_channels = 11,
10955 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
10956 {2427, 4}, {2432, 5}, {2437, 6},
10957 {2442, 7}, {2447, 8}, {2452, 9},
10958 {2457, 10}, {2462, 11}},
10959 },
10960
10961 { /* Europe */
10962 "ZZE",
10963 .bg_channels = 13,
10964 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
10965 {2427, 4}, {2432, 5}, {2437, 6},
10966 {2442, 7}, {2447, 8}, {2452, 9},
10967 {2457, 10}, {2462, 11}, {2467, 12},
10968 {2472, 13}},
10969 .a_channels = 19,
10970 .a = {{5180, 36},
10971 {5200, 40},
10972 {5220, 44},
10973 {5240, 48},
10974 {5260, 52, IEEE80211_CH_PASSIVE_ONLY},
10975 {5280, 56, IEEE80211_CH_PASSIVE_ONLY},
10976 {5300, 60, IEEE80211_CH_PASSIVE_ONLY},
10977 {5320, 64, IEEE80211_CH_PASSIVE_ONLY},
10978 {5500, 100, IEEE80211_CH_PASSIVE_ONLY},
10979 {5520, 104, IEEE80211_CH_PASSIVE_ONLY},
10980 {5540, 108, IEEE80211_CH_PASSIVE_ONLY},
10981 {5560, 112, IEEE80211_CH_PASSIVE_ONLY},
10982 {5580, 116, IEEE80211_CH_PASSIVE_ONLY},
10983 {5600, 120, IEEE80211_CH_PASSIVE_ONLY},
10984 {5620, 124, IEEE80211_CH_PASSIVE_ONLY},
10985 {5640, 128, IEEE80211_CH_PASSIVE_ONLY},
10986 {5660, 132, IEEE80211_CH_PASSIVE_ONLY},
10987 {5680, 136, IEEE80211_CH_PASSIVE_ONLY},
10988 {5700, 140, IEEE80211_CH_PASSIVE_ONLY}},
10989 },
10990
10991 { /* Custom Japan */
10992 "ZZJ",
10993 .bg_channels = 14,
10994 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
10995 {2427, 4}, {2432, 5}, {2437, 6},
10996 {2442, 7}, {2447, 8}, {2452, 9},
10997 {2457, 10}, {2462, 11}, {2467, 12},
10998 {2472, 13}, {2484, 14, IEEE80211_CH_B_ONLY}},
10999 .a_channels = 4,
11000 .a = {{5170, 34}, {5190, 38},
11001 {5210, 42}, {5230, 46}},
11002 },
11003
03520576
JK
11004 { /* Rest of World */
11005 "ZZR",
11006 .bg_channels = 14,
11007 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
11008 {2427, 4}, {2432, 5}, {2437, 6},
11009 {2442, 7}, {2447, 8}, {2452, 9},
11010 {2457, 10}, {2462, 11}, {2467, 12},
11011 {2472, 13}, {2484, 14, IEEE80211_CH_B_ONLY |
11012 IEEE80211_CH_PASSIVE_ONLY}},
11013 },
11014
4f36f808
JK
11015 { /* High Band */
11016 "ZZH",
11017 .bg_channels = 13,
11018 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
11019 {2427, 4}, {2432, 5}, {2437, 6},
11020 {2442, 7}, {2447, 8}, {2452, 9},
11021 {2457, 10}, {2462, 11},
11022 {2467, 12, IEEE80211_CH_PASSIVE_ONLY},
11023 {2472, 13, IEEE80211_CH_PASSIVE_ONLY}},
11024 .a_channels = 4,
11025 .a = {{5745, 149}, {5765, 153},
11026 {5785, 157}, {5805, 161}},
11027 },
11028
11029 { /* Custom Europe */
11030 "ZZG",
11031 .bg_channels = 13,
11032 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
11033 {2427, 4}, {2432, 5}, {2437, 6},
11034 {2442, 7}, {2447, 8}, {2452, 9},
11035 {2457, 10}, {2462, 11},
11036 {2467, 12}, {2472, 13}},
11037 .a_channels = 4,
11038 .a = {{5180, 36}, {5200, 40},
11039 {5220, 44}, {5240, 48}},
11040 },
11041
11042 { /* Europe */
11043 "ZZK",
11044 .bg_channels = 13,
11045 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
11046 {2427, 4}, {2432, 5}, {2437, 6},
11047 {2442, 7}, {2447, 8}, {2452, 9},
11048 {2457, 10}, {2462, 11},
11049 {2467, 12, IEEE80211_CH_PASSIVE_ONLY},
11050 {2472, 13, IEEE80211_CH_PASSIVE_ONLY}},
11051 .a_channels = 24,
11052 .a = {{5180, 36, IEEE80211_CH_PASSIVE_ONLY},
11053 {5200, 40, IEEE80211_CH_PASSIVE_ONLY},
11054 {5220, 44, IEEE80211_CH_PASSIVE_ONLY},
11055 {5240, 48, IEEE80211_CH_PASSIVE_ONLY},
11056 {5260, 52, IEEE80211_CH_PASSIVE_ONLY},
11057 {5280, 56, IEEE80211_CH_PASSIVE_ONLY},
11058 {5300, 60, IEEE80211_CH_PASSIVE_ONLY},
11059 {5320, 64, IEEE80211_CH_PASSIVE_ONLY},
11060 {5500, 100, IEEE80211_CH_PASSIVE_ONLY},
11061 {5520, 104, IEEE80211_CH_PASSIVE_ONLY},
11062 {5540, 108, IEEE80211_CH_PASSIVE_ONLY},
11063 {5560, 112, IEEE80211_CH_PASSIVE_ONLY},
11064 {5580, 116, IEEE80211_CH_PASSIVE_ONLY},
11065 {5600, 120, IEEE80211_CH_PASSIVE_ONLY},
11066 {5620, 124, IEEE80211_CH_PASSIVE_ONLY},
11067 {5640, 128, IEEE80211_CH_PASSIVE_ONLY},
11068 {5660, 132, IEEE80211_CH_PASSIVE_ONLY},
11069 {5680, 136, IEEE80211_CH_PASSIVE_ONLY},
11070 {5700, 140, IEEE80211_CH_PASSIVE_ONLY},
11071 {5745, 149, IEEE80211_CH_PASSIVE_ONLY},
11072 {5765, 153, IEEE80211_CH_PASSIVE_ONLY},
11073 {5785, 157, IEEE80211_CH_PASSIVE_ONLY},
11074 {5805, 161, IEEE80211_CH_PASSIVE_ONLY},
11075 {5825, 165, IEEE80211_CH_PASSIVE_ONLY}},
11076 },
11077
11078 { /* Europe */
11079 "ZZL",
11080 .bg_channels = 11,
11081 .bg = {{2412, 1}, {2417, 2}, {2422, 3},
11082 {2427, 4}, {2432, 5}, {2437, 6},
11083 {2442, 7}, {2447, 8}, {2452, 9},
11084 {2457, 10}, {2462, 11}},
11085 .a_channels = 13,
11086 .a = {{5180, 36, IEEE80211_CH_PASSIVE_ONLY},
11087 {5200, 40, IEEE80211_CH_PASSIVE_ONLY},
11088 {5220, 44, IEEE80211_CH_PASSIVE_ONLY},
11089 {5240, 48, IEEE80211_CH_PASSIVE_ONLY},
11090 {5260, 52, IEEE80211_CH_PASSIVE_ONLY},
11091 {5280, 56, IEEE80211_CH_PASSIVE_ONLY},
11092 {5300, 60, IEEE80211_CH_PASSIVE_ONLY},
11093 {5320, 64, IEEE80211_CH_PASSIVE_ONLY},
11094 {5745, 149, IEEE80211_CH_PASSIVE_ONLY},
11095 {5765, 153, IEEE80211_CH_PASSIVE_ONLY},
11096 {5785, 157, IEEE80211_CH_PASSIVE_ONLY},
11097 {5805, 161, IEEE80211_CH_PASSIVE_ONLY},
11098 {5825, 165, IEEE80211_CH_PASSIVE_ONLY}},
11099 }
afbf30a2
JK
11100};
11101
43f66a6c
JK
11102#define MAX_HW_RESTARTS 5
11103static int ipw_up(struct ipw_priv *priv)
11104{
4f36f808 11105 int rc, i, j;
43f66a6c
JK
11106
11107 if (priv->status & STATUS_EXIT_PENDING)
11108 return -EIO;
11109
f6c5cb7c
JK
11110 if (cmdlog && !priv->cmdlog) {
11111 priv->cmdlog = kmalloc(sizeof(*priv->cmdlog) * cmdlog,
11112 GFP_KERNEL);
11113 if (priv->cmdlog == NULL) {
11114 IPW_ERROR("Error allocating %d command log entries.\n",
11115 cmdlog);
d0b526b7 11116 return -ENOMEM;
f6c5cb7c
JK
11117 } else {
11118 memset(priv->cmdlog, 0, sizeof(*priv->cmdlog) * cmdlog);
11119 priv->cmdlog_len = cmdlog;
11120 }
11121 }
11122
0edd5b44 11123 for (i = 0; i < MAX_HW_RESTARTS; i++) {
bf79451e 11124 /* Load the microcode, firmware, and eeprom.
43f66a6c
JK
11125 * Also start the clocks. */
11126 rc = ipw_load(priv);
11127 if (rc) {
a4f6bbb3 11128 IPW_ERROR("Unable to load firmware: %d\n", rc);
43f66a6c
JK
11129 return rc;
11130 }
11131
11132 ipw_init_ordinals(priv);
11133 if (!(priv->config & CFG_CUSTOM_MAC))
11134 eeprom_parse_mac(priv, priv->mac_addr);
11135 memcpy(priv->net_dev->dev_addr, priv->mac_addr, ETH_ALEN);
11136
4f36f808
JK
11137 for (j = 0; j < ARRAY_SIZE(ipw_geos); j++) {
11138 if (!memcmp(&priv->eeprom[EEPROM_COUNTRY_CODE],
11139 ipw_geos[j].name, 3))
11140 break;
11141 }
03520576
JK
11142 if (j == ARRAY_SIZE(ipw_geos)) {
11143 IPW_WARNING("SKU [%c%c%c] not recognized.\n",
11144 priv->eeprom[EEPROM_COUNTRY_CODE + 0],
11145 priv->eeprom[EEPROM_COUNTRY_CODE + 1],
11146 priv->eeprom[EEPROM_COUNTRY_CODE + 2]);
4f36f808 11147 j = 0;
03520576 11148 }
1867b117 11149 if (ieee80211_set_geo(priv->ieee, &ipw_geos[j])) {
4f36f808
JK
11150 IPW_WARNING("Could not set geography.");
11151 return 0;
11152 }
11153
b095c381
JK
11154 if (priv->status & STATUS_RF_KILL_SW) {
11155 IPW_WARNING("Radio disabled by module parameter.\n");
11156 return 0;
11157 } else if (rf_kill_active(priv)) {
11158 IPW_WARNING("Radio Frequency Kill Switch is On:\n"
11159 "Kill switch must be turned off for "
11160 "wireless networking to work.\n");
11161 queue_delayed_work(priv->workqueue, &priv->rf_kill,
11162 2 * HZ);
43f66a6c 11163 return 0;
c848d0af 11164 }
43f66a6c
JK
11165
11166 rc = ipw_config(priv);
11167 if (!rc) {
11168 IPW_DEBUG_INFO("Configured device on count %i\n", i);
e666619e
JK
11169
11170 /* If configure to try and auto-associate, kick
11171 * off a scan. */
11172 queue_work(priv->workqueue, &priv->request_scan);
afbf30a2 11173
43f66a6c 11174 return 0;
43f66a6c 11175 }
bf79451e 11176
c848d0af 11177 IPW_DEBUG_INFO("Device configuration failed: 0x%08X\n", rc);
43f66a6c
JK
11178 IPW_DEBUG_INFO("Failed to config device on retry %d of %d\n",
11179 i, MAX_HW_RESTARTS);
11180
11181 /* We had an error bringing up the hardware, so take it
11182 * all the way back down so we can try again */
11183 ipw_down(priv);
11184 }
11185
bf79451e 11186 /* tried to restart and config the device for as long as our
43f66a6c 11187 * patience could withstand */
0edd5b44 11188 IPW_ERROR("Unable to initialize device after %d attempts.\n", i);
c848d0af 11189
43f66a6c
JK
11190 return -EIO;
11191}
11192
c848d0af
JK
11193static void ipw_bg_up(void *data)
11194{
11195 struct ipw_priv *priv = data;
4644151b 11196 mutex_lock(&priv->mutex);
c848d0af 11197 ipw_up(data);
4644151b 11198 mutex_unlock(&priv->mutex);
c848d0af
JK
11199}
11200
b095c381 11201static void ipw_deinit(struct ipw_priv *priv)
43f66a6c 11202{
b095c381
JK
11203 int i;
11204
11205 if (priv->status & STATUS_SCANNING) {
11206 IPW_DEBUG_INFO("Aborting scan during shutdown.\n");
11207 ipw_abort_scan(priv);
11208 }
11209
11210 if (priv->status & STATUS_ASSOCIATED) {
11211 IPW_DEBUG_INFO("Disassociating during shutdown.\n");
11212 ipw_disassociate(priv);
11213 }
11214
11215 ipw_led_shutdown(priv);
11216
11217 /* Wait up to 1s for status to change to not scanning and not
11218 * associated (disassociation can take a while for a ful 802.11
11219 * exchange */
11220 for (i = 1000; i && (priv->status &
11221 (STATUS_DISASSOCIATING |
11222 STATUS_ASSOCIATED | STATUS_SCANNING)); i--)
11223 udelay(10);
11224
11225 if (priv->status & (STATUS_DISASSOCIATING |
11226 STATUS_ASSOCIATED | STATUS_SCANNING))
11227 IPW_DEBUG_INFO("Still associated or scanning...\n");
11228 else
11229 IPW_DEBUG_INFO("Took %dms to de-init\n", 1000 - i);
11230
43f66a6c 11231 /* Attempt to disable the card */
43f66a6c 11232 ipw_send_card_disable(priv, 0);
b095c381
JK
11233
11234 priv->status &= ~STATUS_INIT;
11235}
11236
11237static void ipw_down(struct ipw_priv *priv)
11238{
11239 int exit_pending = priv->status & STATUS_EXIT_PENDING;
11240
11241 priv->status |= STATUS_EXIT_PENDING;
11242
11243 if (ipw_is_init(priv))
11244 ipw_deinit(priv);
11245
11246 /* Wipe out the EXIT_PENDING status bit if we are not actually
11247 * exiting the module */
11248 if (!exit_pending)
11249 priv->status &= ~STATUS_EXIT_PENDING;
43f66a6c
JK
11250
11251 /* tell the device to stop sending interrupts */
11252 ipw_disable_interrupts(priv);
11253
11254 /* Clear all bits but the RF Kill */
b095c381 11255 priv->status &= STATUS_RF_KILL_MASK | STATUS_EXIT_PENDING;
43f66a6c
JK
11256 netif_carrier_off(priv->net_dev);
11257 netif_stop_queue(priv->net_dev);
11258
11259 ipw_stop_nic(priv);
a613bffd
JK
11260
11261 ipw_led_radio_off(priv);
43f66a6c
JK
11262}
11263
c848d0af
JK
11264static void ipw_bg_down(void *data)
11265{
11266 struct ipw_priv *priv = data;
4644151b 11267 mutex_lock(&priv->mutex);
c848d0af 11268 ipw_down(data);
4644151b 11269 mutex_unlock(&priv->mutex);
43f66a6c
JK
11270}
11271
11272/* Called by register_netdev() */
11273static int ipw_net_init(struct net_device *dev)
11274{
11275 struct ipw_priv *priv = ieee80211_priv(dev);
4644151b 11276 mutex_lock(&priv->mutex);
43f66a6c 11277
c848d0af 11278 if (ipw_up(priv)) {
4644151b 11279 mutex_unlock(&priv->mutex);
43f66a6c 11280 return -EIO;
c848d0af 11281 }
43f66a6c 11282
4644151b 11283 mutex_unlock(&priv->mutex);
43f66a6c
JK
11284 return 0;
11285}
11286
11287/* PCI driver stuff */
11288static struct pci_device_id card_ids[] = {
11289 {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2701, 0, 0, 0},
11290 {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2702, 0, 0, 0},
11291 {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2711, 0, 0, 0},
11292 {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2712, 0, 0, 0},
11293 {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2721, 0, 0, 0},
11294 {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2722, 0, 0, 0},
11295 {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2731, 0, 0, 0},
11296 {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2732, 0, 0, 0},
11297 {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2741, 0, 0, 0},
11298 {PCI_VENDOR_ID_INTEL, 0x1043, 0x103c, 0x2741, 0, 0, 0},
11299 {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2742, 0, 0, 0},
11300 {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2751, 0, 0, 0},
11301 {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2752, 0, 0, 0},
11302 {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2753, 0, 0, 0},
11303 {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2754, 0, 0, 0},
11304 {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2761, 0, 0, 0},
11305 {PCI_VENDOR_ID_INTEL, 0x1043, 0x8086, 0x2762, 0, 0, 0},
11306 {PCI_VENDOR_ID_INTEL, 0x104f, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
0edd5b44 11307 {PCI_VENDOR_ID_INTEL, 0x4220, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, /* BG */
a613bffd 11308 {PCI_VENDOR_ID_INTEL, 0x4221, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, /* BG */
0edd5b44
JG
11309 {PCI_VENDOR_ID_INTEL, 0x4223, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, /* ABG */
11310 {PCI_VENDOR_ID_INTEL, 0x4224, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, /* ABG */
bf79451e 11311
43f66a6c
JK
11312 /* required last entry */
11313 {0,}
11314};
11315
11316MODULE_DEVICE_TABLE(pci, card_ids);
11317
11318static struct attribute *ipw_sysfs_entries[] = {
11319 &dev_attr_rf_kill.attr,
11320 &dev_attr_direct_dword.attr,
11321 &dev_attr_indirect_byte.attr,
11322 &dev_attr_indirect_dword.attr,
11323 &dev_attr_mem_gpio_reg.attr,
11324 &dev_attr_command_event_reg.attr,
11325 &dev_attr_nic_type.attr,
11326 &dev_attr_status.attr,
11327 &dev_attr_cfg.attr,
b39860c6
JK
11328 &dev_attr_error.attr,
11329 &dev_attr_event_log.attr,
f6c5cb7c 11330 &dev_attr_cmd_log.attr,
43f66a6c
JK
11331 &dev_attr_eeprom_delay.attr,
11332 &dev_attr_ucode_version.attr,
11333 &dev_attr_rtc.attr,
a613bffd
JK
11334 &dev_attr_scan_age.attr,
11335 &dev_attr_led.attr,
b095c381
JK
11336 &dev_attr_speed_scan.attr,
11337 &dev_attr_net_stats.attr,
d685b8c2
ZY
11338#ifdef CONFIG_IPW2200_PROMISCUOUS
11339 &dev_attr_rtap_iface.attr,
11340 &dev_attr_rtap_filter.attr,
11341#endif
43f66a6c
JK
11342 NULL
11343};
11344
11345static struct attribute_group ipw_attribute_group = {
11346 .name = NULL, /* put in device directory */
0edd5b44 11347 .attrs = ipw_sysfs_entries,
43f66a6c
JK
11348};
11349
d685b8c2
ZY
11350#ifdef CONFIG_IPW2200_PROMISCUOUS
11351static int ipw_prom_open(struct net_device *dev)
11352{
11353 struct ipw_prom_priv *prom_priv = ieee80211_priv(dev);
11354 struct ipw_priv *priv = prom_priv->priv;
11355
11356 IPW_DEBUG_INFO("prom dev->open\n");
11357 netif_carrier_off(dev);
11358 netif_stop_queue(dev);
11359
11360 if (priv->ieee->iw_mode != IW_MODE_MONITOR) {
11361 priv->sys_config.accept_all_data_frames = 1;
11362 priv->sys_config.accept_non_directed_frames = 1;
11363 priv->sys_config.accept_all_mgmt_bcpr = 1;
11364 priv->sys_config.accept_all_mgmt_frames = 1;
11365
11366 ipw_send_system_config(priv);
11367 }
11368
11369 return 0;
11370}
11371
11372static int ipw_prom_stop(struct net_device *dev)
11373{
11374 struct ipw_prom_priv *prom_priv = ieee80211_priv(dev);
11375 struct ipw_priv *priv = prom_priv->priv;
11376
11377 IPW_DEBUG_INFO("prom dev->stop\n");
11378
11379 if (priv->ieee->iw_mode != IW_MODE_MONITOR) {
11380 priv->sys_config.accept_all_data_frames = 0;
11381 priv->sys_config.accept_non_directed_frames = 0;
11382 priv->sys_config.accept_all_mgmt_bcpr = 0;
11383 priv->sys_config.accept_all_mgmt_frames = 0;
11384
11385 ipw_send_system_config(priv);
11386 }
11387
11388 return 0;
11389}
11390
11391static int ipw_prom_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
11392{
11393 IPW_DEBUG_INFO("prom dev->xmit\n");
11394 netif_stop_queue(dev);
11395 return -EOPNOTSUPP;
11396}
11397
11398static struct net_device_stats *ipw_prom_get_stats(struct net_device *dev)
11399{
11400 struct ipw_prom_priv *prom_priv = ieee80211_priv(dev);
11401 return &prom_priv->ieee->stats;
11402}
11403
11404static int ipw_prom_alloc(struct ipw_priv *priv)
11405{
11406 int rc = 0;
11407
11408 if (priv->prom_net_dev)
11409 return -EPERM;
11410
11411 priv->prom_net_dev = alloc_ieee80211(sizeof(struct ipw_prom_priv));
11412 if (priv->prom_net_dev == NULL)
11413 return -ENOMEM;
11414
11415 priv->prom_priv = ieee80211_priv(priv->prom_net_dev);
11416 priv->prom_priv->ieee = netdev_priv(priv->prom_net_dev);
11417 priv->prom_priv->priv = priv;
11418
11419 strcpy(priv->prom_net_dev->name, "rtap%d");
11420
11421 priv->prom_net_dev->type = ARPHRD_IEEE80211_RADIOTAP;
11422 priv->prom_net_dev->open = ipw_prom_open;
11423 priv->prom_net_dev->stop = ipw_prom_stop;
11424 priv->prom_net_dev->get_stats = ipw_prom_get_stats;
11425 priv->prom_net_dev->hard_start_xmit = ipw_prom_hard_start_xmit;
11426
11427 priv->prom_priv->ieee->iw_mode = IW_MODE_MONITOR;
11428
11429 rc = register_netdev(priv->prom_net_dev);
11430 if (rc) {
11431 free_ieee80211(priv->prom_net_dev);
11432 priv->prom_net_dev = NULL;
11433 return rc;
11434 }
11435
11436 return 0;
11437}
11438
11439static void ipw_prom_free(struct ipw_priv *priv)
11440{
11441 if (!priv->prom_net_dev)
11442 return;
11443
11444 unregister_netdev(priv->prom_net_dev);
11445 free_ieee80211(priv->prom_net_dev);
11446
11447 priv->prom_net_dev = NULL;
11448}
11449
11450#endif
11451
11452
0edd5b44 11453static int ipw_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
43f66a6c
JK
11454{
11455 int err = 0;
11456 struct net_device *net_dev;
11457 void __iomem *base;
11458 u32 length, val;
11459 struct ipw_priv *priv;
afbf30a2 11460 int i;
43f66a6c
JK
11461
11462 net_dev = alloc_ieee80211(sizeof(struct ipw_priv));
11463 if (net_dev == NULL) {
11464 err = -ENOMEM;
11465 goto out;
11466 }
11467
11468 priv = ieee80211_priv(net_dev);
11469 priv->ieee = netdev_priv(net_dev);
a613bffd 11470
43f66a6c
JK
11471 priv->net_dev = net_dev;
11472 priv->pci_dev = pdev;
43f66a6c 11473 ipw_debug_level = debug;
89c318ed 11474 spin_lock_init(&priv->irq_lock);
43f66a6c 11475 spin_lock_init(&priv->lock);
afbf30a2
JK
11476 for (i = 0; i < IPW_IBSS_MAC_HASH_SIZE; i++)
11477 INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
43f66a6c 11478
4644151b 11479 mutex_init(&priv->mutex);
43f66a6c
JK
11480 if (pci_enable_device(pdev)) {
11481 err = -ENODEV;
11482 goto out_free_ieee80211;
11483 }
11484
11485 pci_set_master(pdev);
11486
0e08b44e 11487 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
bf79451e 11488 if (!err)
0e08b44e 11489 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
43f66a6c
JK
11490 if (err) {
11491 printk(KERN_WARNING DRV_NAME ": No suitable DMA available.\n");
11492 goto out_pci_disable_device;
11493 }
11494
11495 pci_set_drvdata(pdev, priv);
11496
11497 err = pci_request_regions(pdev, DRV_NAME);
bf79451e 11498 if (err)
43f66a6c
JK
11499 goto out_pci_disable_device;
11500
bf79451e 11501 /* We disable the RETRY_TIMEOUT register (0x41) to keep
43f66a6c 11502 * PCI Tx retries from interfering with C3 CPU state */
bf79451e
JG
11503 pci_read_config_dword(pdev, 0x40, &val);
11504 if ((val & 0x0000ff00) != 0)
43f66a6c 11505 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
bf79451e 11506
43f66a6c
JK
11507 length = pci_resource_len(pdev, 0);
11508 priv->hw_len = length;
bf79451e 11509
43f66a6c
JK
11510 base = ioremap_nocache(pci_resource_start(pdev, 0), length);
11511 if (!base) {
11512 err = -ENODEV;
11513 goto out_pci_release_regions;
11514 }
11515
11516 priv->hw_base = base;
11517 IPW_DEBUG_INFO("pci_resource_len = 0x%08x\n", length);
11518 IPW_DEBUG_INFO("pci_resource_base = %p\n", base);
11519
11520 err = ipw_setup_deferred_work(priv);
11521 if (err) {
11522 IPW_ERROR("Unable to setup deferred work\n");
11523 goto out_iounmap;
11524 }
11525
b095c381 11526 ipw_sw_reset(priv, 1);
43f66a6c 11527
1fb9df5d 11528 err = request_irq(pdev->irq, ipw_isr, IRQF_SHARED, DRV_NAME, priv);
43f66a6c
JK
11529 if (err) {
11530 IPW_ERROR("Error allocating IRQ %d\n", pdev->irq);
11531 goto out_destroy_workqueue;
11532 }
11533
11534 SET_MODULE_OWNER(net_dev);
11535 SET_NETDEV_DEV(net_dev, &pdev->dev);
11536
4644151b 11537 mutex_lock(&priv->mutex);
c848d0af 11538
43f66a6c
JK
11539 priv->ieee->hard_start_xmit = ipw_net_hard_start_xmit;
11540 priv->ieee->set_security = shim__set_security;
227d2dc1 11541 priv->ieee->is_queue_full = ipw_net_is_queue_full;
43f66a6c 11542
e43e3c1e 11543#ifdef CONFIG_IPW2200_QOS
a5cf4fe6 11544 priv->ieee->is_qos_active = ipw_is_qos_active;
3b9990cb
JK
11545 priv->ieee->handle_probe_response = ipw_handle_beacon;
11546 priv->ieee->handle_beacon = ipw_handle_probe_response;
11547 priv->ieee->handle_assoc_response = ipw_handle_assoc_response;
e43e3c1e 11548#endif /* CONFIG_IPW2200_QOS */
b095c381 11549
c848d0af
JK
11550 priv->ieee->perfect_rssi = -20;
11551 priv->ieee->worst_rssi = -85;
43f66a6c
JK
11552
11553 net_dev->open = ipw_net_open;
11554 net_dev->stop = ipw_net_stop;
11555 net_dev->init = ipw_net_init;
11556 net_dev->get_stats = ipw_net_get_stats;
11557 net_dev->set_multicast_list = ipw_net_set_multicast_list;
11558 net_dev->set_mac_address = ipw_net_set_mac_address;
97a78ca9 11559 priv->wireless_data.spy_data = &priv->ieee->spy_data;
97a78ca9 11560 net_dev->wireless_data = &priv->wireless_data;
43f66a6c
JK
11561 net_dev->wireless_handlers = &ipw_wx_handler_def;
11562 net_dev->ethtool_ops = &ipw_ethtool_ops;
11563 net_dev->irq = pdev->irq;
0edd5b44 11564 net_dev->base_addr = (unsigned long)priv->hw_base;
43f66a6c
JK
11565 net_dev->mem_start = pci_resource_start(pdev, 0);
11566 net_dev->mem_end = net_dev->mem_start + pci_resource_len(pdev, 0) - 1;
11567
11568 err = sysfs_create_group(&pdev->dev.kobj, &ipw_attribute_group);
11569 if (err) {
11570 IPW_ERROR("failed to create sysfs device attributes\n");
4644151b 11571 mutex_unlock(&priv->mutex);
43f66a6c
JK
11572 goto out_release_irq;
11573 }
11574
4644151b 11575 mutex_unlock(&priv->mutex);
43f66a6c
JK
11576 err = register_netdev(net_dev);
11577 if (err) {
11578 IPW_ERROR("failed to register network device\n");
a613bffd 11579 goto out_remove_sysfs;
43f66a6c 11580 }
48a84770 11581
d685b8c2
ZY
11582#ifdef CONFIG_IPW2200_PROMISCUOUS
11583 if (rtap_iface) {
11584 err = ipw_prom_alloc(priv);
11585 if (err) {
11586 IPW_ERROR("Failed to register promiscuous network "
11587 "device (error %d).\n", err);
11588 unregister_netdev(priv->net_dev);
11589 goto out_remove_sysfs;
11590 }
11591 }
11592#endif
11593
48a84770
HBA
11594 printk(KERN_INFO DRV_NAME ": Detected geography %s (%d 802.11bg "
11595 "channels, %d 802.11a channels)\n",
11596 priv->ieee->geo.name, priv->ieee->geo.bg_channels,
11597 priv->ieee->geo.a_channels);
11598
43f66a6c
JK
11599 return 0;
11600
a613bffd 11601 out_remove_sysfs:
43f66a6c 11602 sysfs_remove_group(&pdev->dev.kobj, &ipw_attribute_group);
0edd5b44 11603 out_release_irq:
43f66a6c 11604 free_irq(pdev->irq, priv);
0edd5b44 11605 out_destroy_workqueue:
43f66a6c
JK
11606 destroy_workqueue(priv->workqueue);
11607 priv->workqueue = NULL;
0edd5b44 11608 out_iounmap:
43f66a6c 11609 iounmap(priv->hw_base);
0edd5b44 11610 out_pci_release_regions:
43f66a6c 11611 pci_release_regions(pdev);
0edd5b44 11612 out_pci_disable_device:
43f66a6c
JK
11613 pci_disable_device(pdev);
11614 pci_set_drvdata(pdev, NULL);
0edd5b44 11615 out_free_ieee80211:
43f66a6c 11616 free_ieee80211(priv->net_dev);
0edd5b44 11617 out:
43f66a6c
JK
11618 return err;
11619}
11620
11621static void ipw_pci_remove(struct pci_dev *pdev)
11622{
11623 struct ipw_priv *priv = pci_get_drvdata(pdev);
afbf30a2
JK
11624 struct list_head *p, *q;
11625 int i;
b095c381 11626
43f66a6c
JK
11627 if (!priv)
11628 return;
11629
4644151b 11630 mutex_lock(&priv->mutex);
43f66a6c 11631
afbf30a2 11632 priv->status |= STATUS_EXIT_PENDING;
43f66a6c 11633 ipw_down(priv);
43f66a6c
JK
11634 sysfs_remove_group(&pdev->dev.kobj, &ipw_attribute_group);
11635
4644151b 11636 mutex_unlock(&priv->mutex);
43f66a6c
JK
11637
11638 unregister_netdev(priv->net_dev);
11639
11640 if (priv->rxq) {
11641 ipw_rx_queue_free(priv, priv->rxq);
11642 priv->rxq = NULL;
11643 }
11644 ipw_tx_queue_free(priv);
11645
f6c5cb7c
JK
11646 if (priv->cmdlog) {
11647 kfree(priv->cmdlog);
11648 priv->cmdlog = NULL;
11649 }
43f66a6c
JK
11650 /* ipw_down will ensure that there is no more pending work
11651 * in the workqueue's, so we can safely remove them now. */
a613bffd
JK
11652 cancel_delayed_work(&priv->adhoc_check);
11653 cancel_delayed_work(&priv->gather_stats);
11654 cancel_delayed_work(&priv->request_scan);
11655 cancel_delayed_work(&priv->rf_kill);
11656 cancel_delayed_work(&priv->scan_check);
11657 destroy_workqueue(priv->workqueue);
11658 priv->workqueue = NULL;
43f66a6c 11659
afbf30a2
JK
11660 /* Free MAC hash list for ADHOC */
11661 for (i = 0; i < IPW_IBSS_MAC_HASH_SIZE; i++) {
11662 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
afbf30a2 11663 list_del(p);
489f4458 11664 kfree(list_entry(p, struct ipw_ibss_seq, list));
afbf30a2
JK
11665 }
11666 }
11667
8f760780
JJ
11668 kfree(priv->error);
11669 priv->error = NULL;
43f66a6c 11670
d685b8c2
ZY
11671#ifdef CONFIG_IPW2200_PROMISCUOUS
11672 ipw_prom_free(priv);
11673#endif
11674
43f66a6c
JK
11675 free_irq(pdev->irq, priv);
11676 iounmap(priv->hw_base);
11677 pci_release_regions(pdev);
11678 pci_disable_device(pdev);
11679 pci_set_drvdata(pdev, NULL);
11680 free_ieee80211(priv->net_dev);
afbf30a2 11681 free_firmware();
43f66a6c
JK
11682}
11683
43f66a6c 11684#ifdef CONFIG_PM
583a4e88 11685static int ipw_pci_suspend(struct pci_dev *pdev, pm_message_t state)
43f66a6c
JK
11686{
11687 struct ipw_priv *priv = pci_get_drvdata(pdev);
11688 struct net_device *dev = priv->net_dev;
11689
11690 printk(KERN_INFO "%s: Going into suspend...\n", dev->name);
11691
0edd5b44 11692 /* Take down the device; powers it off, etc. */
43f66a6c
JK
11693 ipw_down(priv);
11694
11695 /* Remove the PRESENT state of the device */
11696 netif_device_detach(dev);
11697
43f66a6c 11698 pci_save_state(pdev);
43f66a6c 11699 pci_disable_device(pdev);
583a4e88 11700 pci_set_power_state(pdev, pci_choose_state(pdev, state));
bf79451e 11701
43f66a6c
JK
11702 return 0;
11703}
11704
11705static int ipw_pci_resume(struct pci_dev *pdev)
11706{
11707 struct ipw_priv *priv = pci_get_drvdata(pdev);
11708 struct net_device *dev = priv->net_dev;
11709 u32 val;
bf79451e 11710
43f66a6c
JK
11711 printk(KERN_INFO "%s: Coming out of suspend...\n", dev->name);
11712
ea2b26e0 11713 pci_set_power_state(pdev, PCI_D0);
43f66a6c 11714 pci_enable_device(pdev);
43f66a6c 11715 pci_restore_state(pdev);
ea2b26e0 11716
43f66a6c
JK
11717 /*
11718 * Suspend/Resume resets the PCI configuration space, so we have to
11719 * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
11720 * from interfering with C3 CPU state. pci_restore_state won't help
11721 * here since it only restores the first 64 bytes pci config header.
11722 */
bf79451e
JG
11723 pci_read_config_dword(pdev, 0x40, &val);
11724 if ((val & 0x0000ff00) != 0)
43f66a6c
JK
11725 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
11726
11727 /* Set the device back into the PRESENT state; this will also wake
11728 * the queue of needed */
11729 netif_device_attach(dev);
11730
11731 /* Bring the device back up */
11732 queue_work(priv->workqueue, &priv->up);
bf79451e 11733
43f66a6c
JK
11734 return 0;
11735}
11736#endif
11737
11738/* driver initialization stuff */
11739static struct pci_driver ipw_driver = {
11740 .name = DRV_NAME,
11741 .id_table = card_ids,
11742 .probe = ipw_pci_probe,
11743 .remove = __devexit_p(ipw_pci_remove),
11744#ifdef CONFIG_PM
11745 .suspend = ipw_pci_suspend,
11746 .resume = ipw_pci_resume,
11747#endif
11748};
11749
11750static int __init ipw_init(void)
11751{
11752 int ret;
11753
11754 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
11755 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
11756
11757 ret = pci_module_init(&ipw_driver);
11758 if (ret) {
11759 IPW_ERROR("Unable to initialize PCI module\n");
11760 return ret;
11761 }
11762
0edd5b44 11763 ret = driver_create_file(&ipw_driver.driver, &driver_attr_debug_level);
43f66a6c
JK
11764 if (ret) {
11765 IPW_ERROR("Unable to create driver sysfs file\n");
11766 pci_unregister_driver(&ipw_driver);
11767 return ret;
11768 }
11769
11770 return ret;
11771}
11772
11773static void __exit ipw_exit(void)
11774{
11775 driver_remove_file(&ipw_driver.driver, &driver_attr_debug_level);
11776 pci_unregister_driver(&ipw_driver);
11777}
11778
11779module_param(disable, int, 0444);
11780MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
11781
11782module_param(associate, int, 0444);
11783MODULE_PARM_DESC(associate, "auto associate when scanning (default on)");
11784
11785module_param(auto_create, int, 0444);
11786MODULE_PARM_DESC(auto_create, "auto create adhoc network (default on)");
11787
a613bffd 11788module_param(led, int, 0444);
c848d0af 11789MODULE_PARM_DESC(led, "enable led control on some systems (default 0 off)\n");
a613bffd 11790
3e1555ba 11791#ifdef CONFIG_IPW2200_DEBUG
43f66a6c
JK
11792module_param(debug, int, 0444);
11793MODULE_PARM_DESC(debug, "debug output mask");
3e1555ba 11794#endif
43f66a6c
JK
11795
11796module_param(channel, int, 0444);
bf79451e 11797MODULE_PARM_DESC(channel, "channel to limit associate to (default 0 [ANY])");
43f66a6c 11798
d685b8c2
ZY
11799#ifdef CONFIG_IPW2200_PROMISCUOUS
11800module_param(rtap_iface, int, 0444);
11801MODULE_PARM_DESC(rtap_iface, "create the rtap interface (1 - create, default 0)");
11802#endif
11803
e43e3c1e 11804#ifdef CONFIG_IPW2200_QOS
b095c381
JK
11805module_param(qos_enable, int, 0444);
11806MODULE_PARM_DESC(qos_enable, "enable all QoS functionalitis");
11807
11808module_param(qos_burst_enable, int, 0444);
11809MODULE_PARM_DESC(qos_burst_enable, "enable QoS burst mode");
11810
11811module_param(qos_no_ack_mask, int, 0444);
11812MODULE_PARM_DESC(qos_no_ack_mask, "mask Tx_Queue to no ack");
43f66a6c 11813
b095c381
JK
11814module_param(burst_duration_CCK, int, 0444);
11815MODULE_PARM_DESC(burst_duration_CCK, "set CCK burst value");
11816
11817module_param(burst_duration_OFDM, int, 0444);
11818MODULE_PARM_DESC(burst_duration_OFDM, "set OFDM burst value");
e43e3c1e 11819#endif /* CONFIG_IPW2200_QOS */
b095c381
JK
11820
11821#ifdef CONFIG_IPW2200_MONITOR
43f66a6c
JK
11822module_param(mode, int, 0444);
11823MODULE_PARM_DESC(mode, "network mode (0=BSS,1=IBSS,2=Monitor)");
11824#else
11825module_param(mode, int, 0444);
11826MODULE_PARM_DESC(mode, "network mode (0=BSS,1=IBSS)");
11827#endif
11828
810dabd4
ZY
11829module_param(bt_coexist, int, 0444);
11830MODULE_PARM_DESC(bt_coexist, "enable bluetooth coexistence (default off)");
11831
b095c381 11832module_param(hwcrypto, int, 0444);
bde37d03 11833MODULE_PARM_DESC(hwcrypto, "enable hardware crypto (default off)");
b095c381 11834
f6c5cb7c
JK
11835module_param(cmdlog, int, 0444);
11836MODULE_PARM_DESC(cmdlog,
11837 "allocate a ring buffer for logging firmware commands");
11838
4bfdb91d
ZY
11839module_param(roaming, int, 0444);
11840MODULE_PARM_DESC(roaming, "enable roaming support (default on)");
11841
d2b83e12
ZY
11842module_param(antenna, int, 0444);
11843MODULE_PARM_DESC(antenna, "select antenna 1=Main, 3=Aux, default 0 [both], 2=slow_diversity (choose the one with lower background noise)");
11844
43f66a6c
JK
11845module_exit(ipw_exit);
11846module_init(ipw_init);