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