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